Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 1 | // Copyright 2020 The Tint Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Ben Clayton | 8454d82 | 2021-03-10 11:41:49 +0000 | [diff] [blame] | 15 | #include "gtest/gtest-spi.h" |
Ben Clayton | 90da745 | 2021-02-02 15:06:05 +0000 | [diff] [blame] | 16 | #include "src/ast/return_statement.h" |
Ben Clayton | 10d5c6a | 2020-11-13 21:58:28 +0000 | [diff] [blame] | 17 | #include "src/ast/test_helper.h" |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 18 | |
| 19 | namespace tint { |
dan sinclair | 989cee6 | 2020-03-26 15:31:43 +0000 | [diff] [blame] | 20 | namespace { |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 21 | |
Ben Clayton | c40f627 | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 22 | using ProgramTest = ast::TestHelper; |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 23 | |
Ben Clayton | 527a0bb | 2021-02-19 19:04:18 +0000 | [diff] [blame] | 24 | TEST_F(ProgramTest, Unbuilt) { |
| 25 | Program program; |
| 26 | EXPECT_FALSE(program.IsValid()); |
| 27 | } |
| 28 | |
Ben Clayton | c40f627 | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 29 | TEST_F(ProgramTest, Creation) { |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 30 | Program program(std::move(*this)); |
| 31 | EXPECT_EQ(program.AST().Functions().size(), 0u); |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 32 | } |
| 33 | |
Ben Clayton | c40f627 | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 34 | TEST_F(ProgramTest, ToStrEmitsPreambleAndPostamble) { |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 35 | Program program(std::move(*this)); |
| 36 | const auto str = program.to_str(); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 37 | auto* const expected = "Module{\n}\n"; |
David Neto | 0afb943 | 2020-03-18 20:09:44 +0000 | [diff] [blame] | 38 | EXPECT_EQ(str, expected); |
| 39 | } |
| 40 | |
Ben Clayton | 8454d82 | 2021-03-10 11:41:49 +0000 | [diff] [blame] | 41 | TEST_F(ProgramTest, EmptyIsValid) { |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 42 | Program program(std::move(*this)); |
| 43 | EXPECT_TRUE(program.IsValid()); |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 44 | } |
| 45 | |
Ben Clayton | e6995de | 2021-04-13 23:27:27 +0000 | [diff] [blame] | 46 | TEST_F(ProgramTest, IDsAreUnique) { |
| 47 | Program program_a(ProgramBuilder{}); |
| 48 | Program program_b(ProgramBuilder{}); |
| 49 | Program program_c(ProgramBuilder{}); |
| 50 | EXPECT_NE(program_a.ID(), program_b.ID()); |
| 51 | EXPECT_NE(program_b.ID(), program_c.ID()); |
| 52 | EXPECT_NE(program_c.ID(), program_a.ID()); |
| 53 | } |
| 54 | |
Ben Clayton | 8454d82 | 2021-03-10 11:41:49 +0000 | [diff] [blame] | 55 | TEST_F(ProgramTest, Assert_GlobalVariable) { |
James Price | 14c0b8a | 2021-06-24 15:53:26 +0000 | [diff] [blame] | 56 | Global("var", ty.f32(), ast::StorageClass::kPrivate); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 57 | |
| 58 | Program program(std::move(*this)); |
| 59 | EXPECT_TRUE(program.IsValid()); |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 60 | } |
| 61 | |
Ben Clayton | 8454d82 | 2021-03-10 11:41:49 +0000 | [diff] [blame] | 62 | TEST_F(ProgramTest, Assert_NullGlobalVariable) { |
| 63 | EXPECT_FATAL_FAILURE( |
| 64 | { |
| 65 | ProgramBuilder b; |
| 66 | b.AST().AddGlobalVariable(nullptr); |
| 67 | }, |
| 68 | "internal compiler error"); |
Dan Sinclair | 3eaf827 | 2020-03-16 14:17:52 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Ben Clayton | 950809f | 2021-06-09 14:32:14 +0000 | [diff] [blame] | 71 | TEST_F(ProgramTest, Assert_NullTypeDecl) { |
Ben Clayton | 8454d82 | 2021-03-10 11:41:49 +0000 | [diff] [blame] | 72 | EXPECT_FATAL_FAILURE( |
| 73 | { |
| 74 | ProgramBuilder b; |
Ben Clayton | 950809f | 2021-06-09 14:32:14 +0000 | [diff] [blame] | 75 | b.AST().AddTypeDecl(nullptr); |
Ben Clayton | 8454d82 | 2021-03-10 11:41:49 +0000 | [diff] [blame] | 76 | }, |
| 77 | "internal compiler error"); |
Dan Sinclair | 3eaf827 | 2020-03-16 14:17:52 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Ben Clayton | 8454d82 | 2021-03-10 11:41:49 +0000 | [diff] [blame] | 80 | TEST_F(ProgramTest, Assert_Null_Function) { |
| 81 | EXPECT_FATAL_FAILURE( |
| 82 | { |
| 83 | ProgramBuilder b; |
| 84 | b.AST().AddFunction(nullptr); |
| 85 | }, |
| 86 | "internal compiler error"); |
Ben Clayton | 844217f | 2021-01-27 18:49:05 +0000 | [diff] [blame] | 87 | } |
Ben Clayton | ce9229c | 2021-02-24 13:51:13 +0000 | [diff] [blame] | 88 | |
| 89 | TEST_F(ProgramTest, DiagnosticsMove) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 90 | Diagnostics().add_error(diag::System::Program, "an error message"); |
Ben Clayton | ce9229c | 2021-02-24 13:51:13 +0000 | [diff] [blame] | 91 | |
| 92 | Program program_a(std::move(*this)); |
| 93 | EXPECT_FALSE(program_a.IsValid()); |
| 94 | EXPECT_EQ(program_a.Diagnostics().count(), 1u); |
| 95 | EXPECT_EQ(program_a.Diagnostics().error_count(), 1u); |
| 96 | EXPECT_EQ(program_a.Diagnostics().begin()->message, "an error message"); |
| 97 | |
| 98 | Program program_b(std::move(program_a)); |
| 99 | EXPECT_FALSE(program_b.IsValid()); |
| 100 | EXPECT_EQ(program_b.Diagnostics().count(), 1u); |
| 101 | EXPECT_EQ(program_b.Diagnostics().error_count(), 1u); |
| 102 | EXPECT_EQ(program_b.Diagnostics().begin()->message, "an error message"); |
| 103 | } |
| 104 | |
dan sinclair | 989cee6 | 2020-03-26 15:31:43 +0000 | [diff] [blame] | 105 | } // namespace |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 106 | } // namespace tint |