Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +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/tint/ast/assignment_statement.h" |
| 16 | |
| 17 | #include "gtest/gtest-spi.h" |
| 18 | #include "src/tint/ast/test_helper.h" |
| 19 | |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 20 | using namespace tint::number_suffixes; // NOLINT |
| 21 | |
dan sinclair | 34323ac | 2022-04-07 18:39:35 +0000 | [diff] [blame] | 22 | namespace tint::ast { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 23 | namespace { |
| 24 | |
| 25 | using AssignmentStatementTest = TestHelper; |
| 26 | |
| 27 | TEST_F(AssignmentStatementTest, Creation) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 28 | auto* lhs = Expr("lhs"); |
| 29 | auto* rhs = Expr("rhs"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 30 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 31 | auto* stmt = create<AssignmentStatement>(lhs, rhs); |
| 32 | EXPECT_EQ(stmt->lhs, lhs); |
| 33 | EXPECT_EQ(stmt->rhs, rhs); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | TEST_F(AssignmentStatementTest, CreationWithSource) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 37 | auto* lhs = Expr("lhs"); |
| 38 | auto* rhs = Expr("rhs"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 39 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 40 | auto* stmt = create<AssignmentStatement>(Source{Source::Location{20, 2}}, lhs, rhs); |
| 41 | auto src = stmt->source; |
| 42 | EXPECT_EQ(src.range.begin.line, 20u); |
| 43 | EXPECT_EQ(src.range.begin.column, 2u); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | TEST_F(AssignmentStatementTest, IsAssign) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 47 | auto* lhs = Expr("lhs"); |
| 48 | auto* rhs = Expr("rhs"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 49 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 50 | auto* stmt = create<AssignmentStatement>(lhs, rhs); |
| 51 | EXPECT_TRUE(stmt->Is<AssignmentStatement>()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | TEST_F(AssignmentStatementTest, Assert_Null_LHS) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 55 | EXPECT_FATAL_FAILURE( |
| 56 | { |
| 57 | ProgramBuilder b; |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 58 | b.create<AssignmentStatement>(nullptr, b.Expr(1_i)); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 59 | }, |
| 60 | "internal compiler error"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | TEST_F(AssignmentStatementTest, Assert_Null_RHS) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 64 | EXPECT_FATAL_FAILURE( |
| 65 | { |
| 66 | ProgramBuilder b; |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 67 | b.create<AssignmentStatement>(b.Expr(1_i), nullptr); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 68 | }, |
| 69 | "internal compiler error"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | TEST_F(AssignmentStatementTest, Assert_DifferentProgramID_LHS) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 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"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | TEST_F(AssignmentStatementTest, Assert_DifferentProgramID_RHS) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 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"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | } // namespace |
dan sinclair | 34323ac | 2022-04-07 18:39:35 +0000 | [diff] [blame] | 93 | } // namespace tint::ast |