• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/switch_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 SwitchStatementTest = TestHelper;
25 
TEST_F(SwitchStatementTest,Creation)26 TEST_F(SwitchStatementTest, Creation) {
27   CaseSelectorList lit;
28   lit.push_back(create<SintLiteralExpression>(1));
29 
30   auto* ident = Expr("ident");
31   CaseStatementList body;
32   auto* case_stmt = create<CaseStatement>(lit, Block());
33   body.push_back(case_stmt);
34 
35   auto* stmt = create<SwitchStatement>(ident, body);
36   EXPECT_EQ(stmt->condition, ident);
37   ASSERT_EQ(stmt->body.size(), 1u);
38   EXPECT_EQ(stmt->body[0], case_stmt);
39 }
40 
TEST_F(SwitchStatementTest,Creation_WithSource)41 TEST_F(SwitchStatementTest, Creation_WithSource) {
42   auto* ident = Expr("ident");
43 
44   auto* stmt = create<SwitchStatement>(Source{Source::Location{20, 2}}, ident,
45                                        CaseStatementList());
46   auto src = stmt->source;
47   EXPECT_EQ(src.range.begin.line, 20u);
48   EXPECT_EQ(src.range.begin.column, 2u);
49 }
50 
TEST_F(SwitchStatementTest,IsSwitch)51 TEST_F(SwitchStatementTest, IsSwitch) {
52   CaseSelectorList lit;
53   lit.push_back(create<SintLiteralExpression>(2));
54 
55   auto* ident = Expr("ident");
56   CaseStatementList body;
57   body.push_back(create<CaseStatement>(lit, Block()));
58 
59   auto* stmt = create<SwitchStatement>(ident, body);
60   EXPECT_TRUE(stmt->Is<SwitchStatement>());
61 }
62 
TEST_F(SwitchStatementTest,Assert_Null_Condition)63 TEST_F(SwitchStatementTest, Assert_Null_Condition) {
64   EXPECT_FATAL_FAILURE(
65       {
66         ProgramBuilder b;
67         CaseStatementList cases;
68         cases.push_back(
69             b.create<CaseStatement>(CaseSelectorList{b.Expr(1)}, b.Block()));
70         b.create<SwitchStatement>(nullptr, cases);
71       },
72       "internal compiler error");
73 }
74 
TEST_F(SwitchStatementTest,Assert_Null_CaseStatement)75 TEST_F(SwitchStatementTest, Assert_Null_CaseStatement) {
76   EXPECT_FATAL_FAILURE(
77       {
78         ProgramBuilder b;
79         b.create<SwitchStatement>(b.Expr(true), CaseStatementList{nullptr});
80       },
81       "internal compiler error");
82 }
83 
TEST_F(SwitchStatementTest,Assert_DifferentProgramID_Condition)84 TEST_F(SwitchStatementTest, Assert_DifferentProgramID_Condition) {
85   EXPECT_FATAL_FAILURE(
86       {
87         ProgramBuilder b1;
88         ProgramBuilder b2;
89         b1.create<SwitchStatement>(b2.Expr(true), CaseStatementList{
90                                                       b1.create<CaseStatement>(
91                                                           CaseSelectorList{
92                                                               b1.Expr(1),
93                                                           },
94                                                           b1.Block()),
95                                                   });
96       },
97       "internal compiler error");
98 }
99 
TEST_F(SwitchStatementTest,Assert_DifferentProgramID_CaseStatement)100 TEST_F(SwitchStatementTest, Assert_DifferentProgramID_CaseStatement) {
101   EXPECT_FATAL_FAILURE(
102       {
103         ProgramBuilder b1;
104         ProgramBuilder b2;
105         b1.create<SwitchStatement>(b1.Expr(true), CaseStatementList{
106                                                       b2.create<CaseStatement>(
107                                                           CaseSelectorList{
108                                                               b2.Expr(1),
109                                                           },
110                                                           b2.Block()),
111                                                   });
112       },
113       "internal compiler error");
114 }
115 
116 }  // namespace
117 }  // namespace ast
118 }  // namespace tint
119