blob: 6c1a716d9ae4c7e1cdff96504d32129bf38759ff [file] [log] [blame]
Dan Sinclair796f65f2020-03-05 20:20:39 +00001// 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 Clayton8454d822021-03-10 11:41:49 +000017#include "gtest/gtest-spi.h"
Ben Clayton10d5c6a2020-11-13 21:58:28 +000018#include "src/ast/test_helper.h"
Dan Sinclair796f65f2020-03-05 20:20:39 +000019
20namespace tint {
21namespace ast {
dan sinclair989cee62020-03-26 15:31:43 +000022namespace {
Dan Sinclair796f65f2020-03-05 20:20:39 +000023
Ben Clayton10d5c6a2020-11-13 21:58:28 +000024using AssignmentStatementTest = TestHelper;
Dan Sinclair796f65f2020-03-05 20:20:39 +000025
26TEST_F(AssignmentStatementTest, Creation) {
Ben Clayton1523f5c2020-12-14 22:30:57 +000027 auto* lhs = Expr("lhs");
28 auto* rhs = Expr("rhs");
Dan Sinclair796f65f2020-03-05 20:20:39 +000029
Ben Clayton1523f5c2020-12-14 22:30:57 +000030 auto* stmt = create<AssignmentStatement>(lhs, rhs);
Ben Clayton4f3ff572021-10-15 17:33:10 +000031 EXPECT_EQ(stmt->lhs, lhs);
32 EXPECT_EQ(stmt->rhs, rhs);
Dan Sinclair796f65f2020-03-05 20:20:39 +000033}
34
35TEST_F(AssignmentStatementTest, CreationWithSource) {
Ben Clayton1523f5c2020-12-14 22:30:57 +000036 auto* lhs = Expr("lhs");
37 auto* rhs = Expr("rhs");
Dan Sinclair796f65f2020-03-05 20:20:39 +000038
Ben Clayton1523f5c2020-12-14 22:30:57 +000039 auto* stmt =
40 create<AssignmentStatement>(Source{Source::Location{20, 2}}, lhs, rhs);
Ben Clayton4f3ff572021-10-15 17:33:10 +000041 auto src = stmt->source;
Ben Clayton5bee67f2020-10-30 20:44:53 +000042 EXPECT_EQ(src.range.begin.line, 20u);
43 EXPECT_EQ(src.range.begin.column, 2u);
Dan Sinclair796f65f2020-03-05 20:20:39 +000044}
45
46TEST_F(AssignmentStatementTest, IsAssign) {
Ben Clayton1523f5c2020-12-14 22:30:57 +000047 auto* lhs = Expr("lhs");
48 auto* rhs = Expr("rhs");
Dan Sinclair796f65f2020-03-05 20:20:39 +000049
Ben Clayton1523f5c2020-12-14 22:30:57 +000050 auto* stmt = create<AssignmentStatement>(lhs, rhs);
51 EXPECT_TRUE(stmt->Is<AssignmentStatement>());
Dan Sinclair796f65f2020-03-05 20:20:39 +000052}
53
Ben Claytonf0c816a2021-04-15 17:47:23 +000054TEST_F(AssignmentStatementTest, Assert_Null_LHS) {
Ben Clayton8454d822021-03-10 11:41:49 +000055 EXPECT_FATAL_FAILURE(
56 {
57 ProgramBuilder b;
58 b.create<AssignmentStatement>(nullptr, b.Expr(1));
59 },
60 "internal compiler error");
Dan Sinclair796f65f2020-03-05 20:20:39 +000061}
62
Ben Claytonf0c816a2021-04-15 17:47:23 +000063TEST_F(AssignmentStatementTest, Assert_Null_RHS) {
Ben Clayton8454d822021-03-10 11:41:49 +000064 EXPECT_FATAL_FAILURE(
65 {
66 ProgramBuilder b;
67 b.create<AssignmentStatement>(b.Expr(1), nullptr);
68 },
69 "internal compiler error");
Dan Sinclair796f65f2020-03-05 20:20:39 +000070}
71
Ben Claytonf0c816a2021-04-15 17:47:23 +000072TEST_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
82TEST_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 sinclair989cee62020-03-26 15:31:43 +000092} // namespace
Dan Sinclair796f65f2020-03-05 20:20:39 +000093} // namespace ast
94} // namespace tint