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/if_statement.h"
16
17 #include "gtest/gtest-spi.h"
18 #include "src/ast/discard_statement.h"
19 #include "src/ast/test_helper.h"
20
21 namespace tint {
22 namespace ast {
23 namespace {
24
25 using IfStatementTest = TestHelper;
26
TEST_F(IfStatementTest,Creation)27 TEST_F(IfStatementTest, Creation) {
28 auto* cond = Expr("cond");
29 auto* stmt = create<IfStatement>(Source{Source::Location{20, 2}}, cond,
30 Block(create<DiscardStatement>()),
31 ElseStatementList{});
32 auto src = stmt->source;
33 EXPECT_EQ(src.range.begin.line, 20u);
34 EXPECT_EQ(src.range.begin.column, 2u);
35 }
36
TEST_F(IfStatementTest,IsIf)37 TEST_F(IfStatementTest, IsIf) {
38 auto* stmt = create<IfStatement>(Expr(true), Block(), ElseStatementList{});
39 EXPECT_TRUE(stmt->Is<IfStatement>());
40 }
41
TEST_F(IfStatementTest,Assert_Null_Condition)42 TEST_F(IfStatementTest, Assert_Null_Condition) {
43 EXPECT_FATAL_FAILURE(
44 {
45 ProgramBuilder b;
46 b.create<IfStatement>(nullptr, b.Block(), ElseStatementList{});
47 },
48 "internal compiler error");
49 }
50
TEST_F(IfStatementTest,Assert_Null_Body)51 TEST_F(IfStatementTest, Assert_Null_Body) {
52 EXPECT_FATAL_FAILURE(
53 {
54 ProgramBuilder b;
55 b.create<IfStatement>(b.Expr(true), nullptr, ElseStatementList{});
56 },
57 "internal compiler error");
58 }
59
TEST_F(IfStatementTest,Assert_Null_ElseStatement)60 TEST_F(IfStatementTest, Assert_Null_ElseStatement) {
61 EXPECT_FATAL_FAILURE(
62 {
63 ProgramBuilder b;
64 auto* body = b.create<BlockStatement>(StatementList{});
65 b.create<IfStatement>(b.Expr(true), body, ElseStatementList{nullptr});
66 },
67 "internal compiler error");
68 }
69
TEST_F(IfStatementTest,Assert_DifferentProgramID_Cond)70 TEST_F(IfStatementTest, Assert_DifferentProgramID_Cond) {
71 EXPECT_FATAL_FAILURE(
72 {
73 ProgramBuilder b1;
74 ProgramBuilder b2;
75 b1.create<IfStatement>(b2.Expr(true), b1.Block(), ElseStatementList{});
76 },
77 "internal compiler error");
78 }
79
TEST_F(IfStatementTest,Assert_DifferentProgramID_Body)80 TEST_F(IfStatementTest, Assert_DifferentProgramID_Body) {
81 EXPECT_FATAL_FAILURE(
82 {
83 ProgramBuilder b1;
84 ProgramBuilder b2;
85 b1.create<IfStatement>(b1.Expr(true), b2.Block(), ElseStatementList{});
86 },
87 "internal compiler error");
88 }
89
TEST_F(IfStatementTest,Assert_DifferentProgramID_ElseStatement)90 TEST_F(IfStatementTest, Assert_DifferentProgramID_ElseStatement) {
91 EXPECT_FATAL_FAILURE(
92 {
93 ProgramBuilder b1;
94 ProgramBuilder b2;
95 b1.create<IfStatement>(
96 b1.Expr(true), b1.Block(),
97 ElseStatementList{
98 b2.create<ElseStatement>(b2.Expr("ident"), b2.Block()),
99 });
100 },
101 "internal compiler error");
102 }
103
104 } // namespace
105 } // namespace ast
106 } // namespace tint
107