blob: 477501bb3753c3870fe7f591771d9111052d65d9 [file] [log] [blame]
Ryan Harrisondbc13af2022-02-21 15:19:07 +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/tint/ast/assignment_statement.h"
16
17#include "gtest/gtest-spi.h"
18#include "src/tint/ast/test_helper.h"
19
Ben Clayton0ce9ab02022-05-05 20:23:40 +000020using namespace tint::number_suffixes; // NOLINT
21
dan sinclair34323ac2022-04-07 18:39:35 +000022namespace tint::ast {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000023namespace {
24
25using AssignmentStatementTest = TestHelper;
26
27TEST_F(AssignmentStatementTest, Creation) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000028 auto* lhs = Expr("lhs");
29 auto* rhs = Expr("rhs");
Ryan Harrisondbc13af2022-02-21 15:19:07 +000030
dan sinclair41e4d9a2022-05-01 14:40:55 +000031 auto* stmt = create<AssignmentStatement>(lhs, rhs);
32 EXPECT_EQ(stmt->lhs, lhs);
33 EXPECT_EQ(stmt->rhs, rhs);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000034}
35
36TEST_F(AssignmentStatementTest, CreationWithSource) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000037 auto* lhs = Expr("lhs");
38 auto* rhs = Expr("rhs");
Ryan Harrisondbc13af2022-02-21 15:19:07 +000039
dan sinclair41e4d9a2022-05-01 14:40:55 +000040 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 Harrisondbc13af2022-02-21 15:19:07 +000044}
45
46TEST_F(AssignmentStatementTest, IsAssign) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000047 auto* lhs = Expr("lhs");
48 auto* rhs = Expr("rhs");
Ryan Harrisondbc13af2022-02-21 15:19:07 +000049
dan sinclair41e4d9a2022-05-01 14:40:55 +000050 auto* stmt = create<AssignmentStatement>(lhs, rhs);
51 EXPECT_TRUE(stmt->Is<AssignmentStatement>());
Ryan Harrisondbc13af2022-02-21 15:19:07 +000052}
53
54TEST_F(AssignmentStatementTest, Assert_Null_LHS) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000055 EXPECT_FATAL_FAILURE(
56 {
57 ProgramBuilder b;
Ben Clayton0ce9ab02022-05-05 20:23:40 +000058 b.create<AssignmentStatement>(nullptr, b.Expr(1_i));
dan sinclair41e4d9a2022-05-01 14:40:55 +000059 },
60 "internal compiler error");
Ryan Harrisondbc13af2022-02-21 15:19:07 +000061}
62
63TEST_F(AssignmentStatementTest, Assert_Null_RHS) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000064 EXPECT_FATAL_FAILURE(
65 {
66 ProgramBuilder b;
Ben Clayton0ce9ab02022-05-05 20:23:40 +000067 b.create<AssignmentStatement>(b.Expr(1_i), nullptr);
dan sinclair41e4d9a2022-05-01 14:40:55 +000068 },
69 "internal compiler error");
Ryan Harrisondbc13af2022-02-21 15:19:07 +000070}
71
72TEST_F(AssignmentStatementTest, Assert_DifferentProgramID_LHS) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000073 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 Harrisondbc13af2022-02-21 15:19:07 +000080}
81
82TEST_F(AssignmentStatementTest, Assert_DifferentProgramID_RHS) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000083 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 Harrisondbc13af2022-02-21 15:19:07 +000090}
91
92} // namespace
dan sinclair34323ac2022-04-07 18:39:35 +000093} // namespace tint::ast