Dan Sinclair | 796f65f | 2020-03-05 20:20:39 +0000 | [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 | |
| 15 | #include "src/ast/assignment_statement.h" |
| 16 | |
Ben Clayton | 8454d82 | 2021-03-10 11:41:49 +0000 | [diff] [blame] | 17 | #include "gtest/gtest-spi.h" |
Ben Clayton | 10d5c6a | 2020-11-13 21:58:28 +0000 | [diff] [blame] | 18 | #include "src/ast/test_helper.h" |
Dan Sinclair | 796f65f | 2020-03-05 20:20:39 +0000 | [diff] [blame] | 19 | |
| 20 | namespace tint { |
| 21 | namespace ast { |
dan sinclair | 989cee6 | 2020-03-26 15:31:43 +0000 | [diff] [blame] | 22 | namespace { |
Dan Sinclair | 796f65f | 2020-03-05 20:20:39 +0000 | [diff] [blame] | 23 | |
Ben Clayton | 10d5c6a | 2020-11-13 21:58:28 +0000 | [diff] [blame] | 24 | using AssignmentStatementTest = TestHelper; |
Dan Sinclair | 796f65f | 2020-03-05 20:20:39 +0000 | [diff] [blame] | 25 | |
| 26 | TEST_F(AssignmentStatementTest, Creation) { |
Ben Clayton | 1523f5c | 2020-12-14 22:30:57 +0000 | [diff] [blame] | 27 | auto* lhs = Expr("lhs"); |
| 28 | auto* rhs = Expr("rhs"); |
Dan Sinclair | 796f65f | 2020-03-05 20:20:39 +0000 | [diff] [blame] | 29 | |
Ben Clayton | 1523f5c | 2020-12-14 22:30:57 +0000 | [diff] [blame] | 30 | auto* stmt = create<AssignmentStatement>(lhs, rhs); |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 31 | EXPECT_EQ(stmt->lhs, lhs); |
| 32 | EXPECT_EQ(stmt->rhs, rhs); |
Dan Sinclair | 796f65f | 2020-03-05 20:20:39 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | TEST_F(AssignmentStatementTest, CreationWithSource) { |
Ben Clayton | 1523f5c | 2020-12-14 22:30:57 +0000 | [diff] [blame] | 36 | auto* lhs = Expr("lhs"); |
| 37 | auto* rhs = Expr("rhs"); |
Dan Sinclair | 796f65f | 2020-03-05 20:20:39 +0000 | [diff] [blame] | 38 | |
Ben Clayton | 1523f5c | 2020-12-14 22:30:57 +0000 | [diff] [blame] | 39 | auto* stmt = |
| 40 | create<AssignmentStatement>(Source{Source::Location{20, 2}}, lhs, rhs); |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 41 | auto src = stmt->source; |
Ben Clayton | 5bee67f | 2020-10-30 20:44:53 +0000 | [diff] [blame] | 42 | EXPECT_EQ(src.range.begin.line, 20u); |
| 43 | EXPECT_EQ(src.range.begin.column, 2u); |
Dan Sinclair | 796f65f | 2020-03-05 20:20:39 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | TEST_F(AssignmentStatementTest, IsAssign) { |
Ben Clayton | 1523f5c | 2020-12-14 22:30:57 +0000 | [diff] [blame] | 47 | auto* lhs = Expr("lhs"); |
| 48 | auto* rhs = Expr("rhs"); |
Dan Sinclair | 796f65f | 2020-03-05 20:20:39 +0000 | [diff] [blame] | 49 | |
Ben Clayton | 1523f5c | 2020-12-14 22:30:57 +0000 | [diff] [blame] | 50 | auto* stmt = create<AssignmentStatement>(lhs, rhs); |
| 51 | EXPECT_TRUE(stmt->Is<AssignmentStatement>()); |
Dan Sinclair | 796f65f | 2020-03-05 20:20:39 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Ben Clayton | f0c816a | 2021-04-15 17:47:23 +0000 | [diff] [blame] | 54 | TEST_F(AssignmentStatementTest, Assert_Null_LHS) { |
Ben Clayton | 8454d82 | 2021-03-10 11:41:49 +0000 | [diff] [blame] | 55 | EXPECT_FATAL_FAILURE( |
| 56 | { |
| 57 | ProgramBuilder b; |
| 58 | b.create<AssignmentStatement>(nullptr, b.Expr(1)); |
| 59 | }, |
| 60 | "internal compiler error"); |
Dan Sinclair | 796f65f | 2020-03-05 20:20:39 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Ben Clayton | f0c816a | 2021-04-15 17:47:23 +0000 | [diff] [blame] | 63 | TEST_F(AssignmentStatementTest, Assert_Null_RHS) { |
Ben Clayton | 8454d82 | 2021-03-10 11:41:49 +0000 | [diff] [blame] | 64 | EXPECT_FATAL_FAILURE( |
| 65 | { |
| 66 | ProgramBuilder b; |
| 67 | b.create<AssignmentStatement>(b.Expr(1), nullptr); |
| 68 | }, |
| 69 | "internal compiler error"); |
Dan Sinclair | 796f65f | 2020-03-05 20:20:39 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Ben Clayton | f0c816a | 2021-04-15 17:47:23 +0000 | [diff] [blame] | 72 | TEST_F(AssignmentStatementTest, Assert_DifferentProgramID_LHS) { |
| 73 | EXPECT_FATAL_FAILURE( |
| 74 | { |
| 75 | ProgramBuilder b1; |
| 76 | ProgramBuilder b2; |
| 77 | b1.create<AssignmentStatement>(b2.Expr("lhs"), b1.Expr("rhs")); |
| 78 | }, |
| 79 | "internal compiler error"); |
| 80 | } |
| 81 | |
| 82 | TEST_F(AssignmentStatementTest, Assert_DifferentProgramID_RHS) { |
| 83 | EXPECT_FATAL_FAILURE( |
| 84 | { |
| 85 | ProgramBuilder b1; |
| 86 | ProgramBuilder b2; |
| 87 | b1.create<AssignmentStatement>(b1.Expr("lhs"), b2.Expr("rhs")); |
| 88 | }, |
| 89 | "internal compiler error"); |
| 90 | } |
| 91 | |
dan sinclair | 989cee6 | 2020-03-26 15:31:43 +0000 | [diff] [blame] | 92 | } // namespace |
Dan Sinclair | 796f65f | 2020-03-05 20:20:39 +0000 | [diff] [blame] | 93 | } // namespace ast |
| 94 | } // namespace tint |