• 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/reader/wgsl/parser_impl_test_helper.h"
16 
17 namespace tint {
18 namespace reader {
19 namespace wgsl {
20 namespace {
21 
TEST_F(ParserImplTest,ElseIfStmt)22 TEST_F(ParserImplTest, ElseIfStmt) {
23   auto p = parser("elseif (a == 4) { a = b; c = d; }");
24   auto e = p->elseif_stmt();
25   EXPECT_TRUE(e.matched);
26   EXPECT_FALSE(e.errored);
27   EXPECT_FALSE(p->has_error()) << p->error();
28   ASSERT_EQ(e.value.size(), 1u);
29 
30   ASSERT_TRUE(e.value[0]->Is<ast::ElseStatement>());
31   ASSERT_NE(e.value[0]->condition, nullptr);
32   ASSERT_TRUE(e.value[0]->condition->Is<ast::BinaryExpression>());
33   EXPECT_EQ(e.value[0]->body->statements.size(), 2u);
34 }
35 
TEST_F(ParserImplTest,ElseIfStmt_Multiple)36 TEST_F(ParserImplTest, ElseIfStmt_Multiple) {
37   auto p = parser("elseif (a == 4) { a = b; c = d; } elseif(c) { d = 2; }");
38   auto e = p->elseif_stmt();
39   EXPECT_TRUE(e.matched);
40   EXPECT_FALSE(e.errored);
41   EXPECT_FALSE(p->has_error()) << p->error();
42   ASSERT_EQ(e.value.size(), 2u);
43 
44   ASSERT_TRUE(e.value[0]->Is<ast::ElseStatement>());
45   ASSERT_NE(e.value[0]->condition, nullptr);
46   ASSERT_TRUE(e.value[0]->condition->Is<ast::BinaryExpression>());
47   EXPECT_EQ(e.value[0]->body->statements.size(), 2u);
48 
49   ASSERT_TRUE(e.value[1]->Is<ast::ElseStatement>());
50   ASSERT_NE(e.value[1]->condition, nullptr);
51   ASSERT_TRUE(e.value[1]->condition->Is<ast::IdentifierExpression>());
52   EXPECT_EQ(e.value[1]->body->statements.size(), 1u);
53 }
54 
TEST_F(ParserImplTest,ElseIfStmt_InvalidBody)55 TEST_F(ParserImplTest, ElseIfStmt_InvalidBody) {
56   auto p = parser("elseif (true) { fn main() {}}");
57   auto e = p->elseif_stmt();
58   EXPECT_FALSE(e.matched);
59   EXPECT_TRUE(e.errored);
60   EXPECT_TRUE(p->has_error());
61   EXPECT_EQ(p->error(), "1:17: expected '}'");
62 }
63 
TEST_F(ParserImplTest,ElseIfStmt_MissingBody)64 TEST_F(ParserImplTest, ElseIfStmt_MissingBody) {
65   auto p = parser("elseif (true)");
66   auto e = p->elseif_stmt();
67   EXPECT_FALSE(e.matched);
68   EXPECT_TRUE(e.errored);
69   EXPECT_TRUE(p->has_error());
70   EXPECT_EQ(p->error(), "1:14: expected '{'");
71 }
72 
73 }  // namespace
74 }  // namespace wgsl
75 }  // namespace reader
76 }  // namespace tint
77