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
17 #include "gtest/gtest-spi.h"
18 #include "src/ast/test_helper.h"
19
20 namespace tint {
21 namespace ast {
22 namespace {
23
24 using AssignmentStatementTest = TestHelper;
25
TEST_F(AssignmentStatementTest,Creation)26 TEST_F(AssignmentStatementTest, Creation) {
27 auto* lhs = Expr("lhs");
28 auto* rhs = Expr("rhs");
29
30 auto* stmt = create<AssignmentStatement>(lhs, rhs);
31 EXPECT_EQ(stmt->lhs, lhs);
32 EXPECT_EQ(stmt->rhs, rhs);
33 }
34
TEST_F(AssignmentStatementTest,CreationWithSource)35 TEST_F(AssignmentStatementTest, CreationWithSource) {
36 auto* lhs = Expr("lhs");
37 auto* rhs = Expr("rhs");
38
39 auto* stmt =
40 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);
44 }
45
TEST_F(AssignmentStatementTest,IsAssign)46 TEST_F(AssignmentStatementTest, IsAssign) {
47 auto* lhs = Expr("lhs");
48 auto* rhs = Expr("rhs");
49
50 auto* stmt = create<AssignmentStatement>(lhs, rhs);
51 EXPECT_TRUE(stmt->Is<AssignmentStatement>());
52 }
53
TEST_F(AssignmentStatementTest,Assert_Null_LHS)54 TEST_F(AssignmentStatementTest, Assert_Null_LHS) {
55 EXPECT_FATAL_FAILURE(
56 {
57 ProgramBuilder b;
58 b.create<AssignmentStatement>(nullptr, b.Expr(1));
59 },
60 "internal compiler error");
61 }
62
TEST_F(AssignmentStatementTest,Assert_Null_RHS)63 TEST_F(AssignmentStatementTest, Assert_Null_RHS) {
64 EXPECT_FATAL_FAILURE(
65 {
66 ProgramBuilder b;
67 b.create<AssignmentStatement>(b.Expr(1), nullptr);
68 },
69 "internal compiler error");
70 }
71
TEST_F(AssignmentStatementTest,Assert_DifferentProgramID_LHS)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
TEST_F(AssignmentStatementTest,Assert_DifferentProgramID_RHS)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
92 } // namespace
93 } // namespace ast
94 } // namespace tint
95