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 "gtest/gtest-spi.h"
16 #include "src/ast/discard_statement.h"
17 #include "src/ast/if_statement.h"
18 #include "src/ast/test_helper.h"
19 
20 namespace tint {
21 namespace ast {
22 namespace {
23 
24 using ElseStatementTest = TestHelper;
25 
TEST_F(ElseStatementTest, Creation)26 TEST_F(ElseStatementTest, Creation) {
27   auto* cond = Expr(true);
28   auto* body = create<BlockStatement>(StatementList{
29       create<DiscardStatement>(),
30   });
31   auto* discard = body->statements[0];
32 
33   auto* e = create<ElseStatement>(cond, body);
34   EXPECT_EQ(e->condition, cond);
35   ASSERT_EQ(e->body->statements.size(), 1u);
36   EXPECT_EQ(e->body->statements[0], discard);
37 }
38 
TEST_F(ElseStatementTest, Creation_WithSource)39 TEST_F(ElseStatementTest, Creation_WithSource) {
40   auto* e = create<ElseStatement>(Source{Source::Location{20, 2}}, Expr(true),
41                                   Block());
42   auto src = e->source;
43   EXPECT_EQ(src.range.begin.line, 20u);
44   EXPECT_EQ(src.range.begin.column, 2u);
45 }
46 
TEST_F(ElseStatementTest, IsElse)47 TEST_F(ElseStatementTest, IsElse) {
48   auto* e = create<ElseStatement>(nullptr, Block());
49   EXPECT_TRUE(e->Is<ElseStatement>());
50 }
51 
TEST_F(ElseStatementTest, HasCondition)52 TEST_F(ElseStatementTest, HasCondition) {
53   auto* cond = Expr(true);
54   auto* e = create<ElseStatement>(cond, Block());
55   EXPECT_TRUE(e->condition);
56 }
57 
TEST_F(ElseStatementTest, HasContition_NullCondition)58 TEST_F(ElseStatementTest, HasContition_NullCondition) {
59   auto* e = create<ElseStatement>(nullptr, Block());
60   EXPECT_FALSE(e->condition);
61 }
62 
TEST_F(ElseStatementTest, Assert_Null_Body)63 TEST_F(ElseStatementTest, Assert_Null_Body) {
64   EXPECT_FATAL_FAILURE(
65       {
66         ProgramBuilder b;
67         b.create<ElseStatement>(b.Expr(true), nullptr);
68       },
69       "internal compiler error");
70 }
71 
TEST_F(ElseStatementTest, Assert_DifferentProgramID_Condition)72 TEST_F(ElseStatementTest, Assert_DifferentProgramID_Condition) {
73   EXPECT_FATAL_FAILURE(
74       {
75         ProgramBuilder b1;
76         ProgramBuilder b2;
77         b1.create<ElseStatement>(b2.Expr(true), b1.Block());
78       },
79       "internal compiler error");
80 }
81 
TEST_F(ElseStatementTest, Assert_DifferentProgramID_Body)82 TEST_F(ElseStatementTest, Assert_DifferentProgramID_Body) {
83   EXPECT_FATAL_FAILURE(
84       {
85         ProgramBuilder b1;
86         ProgramBuilder b2;
87         b1.create<ElseStatement>(b1.Expr(true), b2.Block());
88       },
89       "internal compiler error");
90 }
91 
92 }  // namespace
93 }  // namespace ast
94 }  // namespace tint
95