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/discard_statement.h"
16 #include "src/reader/wgsl/parser_impl_test_helper.h"
17
18 namespace tint {
19 namespace reader {
20 namespace wgsl {
21 namespace {
22
TEST_F(ParserImplTest,BodyStmt)23 TEST_F(ParserImplTest, BodyStmt) {
24 auto p = parser(R"({
25 discard;
26 return 1 + b / 2;
27 })");
28 auto e = p->expect_body_stmt();
29 ASSERT_FALSE(p->has_error()) << p->error();
30 ASSERT_FALSE(e.errored);
31 ASSERT_EQ(e->statements.size(), 2u);
32 EXPECT_TRUE(e->statements[0]->Is<ast::DiscardStatement>());
33 EXPECT_TRUE(e->statements[1]->Is<ast::ReturnStatement>());
34 }
35
TEST_F(ParserImplTest,BodyStmt_Empty)36 TEST_F(ParserImplTest, BodyStmt_Empty) {
37 auto p = parser("{}");
38 auto e = p->expect_body_stmt();
39 ASSERT_FALSE(p->has_error()) << p->error();
40 ASSERT_FALSE(e.errored);
41 EXPECT_EQ(e->statements.size(), 0u);
42 }
43
TEST_F(ParserImplTest,BodyStmt_InvalidStmt)44 TEST_F(ParserImplTest, BodyStmt_InvalidStmt) {
45 auto p = parser("{fn main() {}}");
46 auto e = p->expect_body_stmt();
47 ASSERT_TRUE(p->has_error());
48 ASSERT_TRUE(e.errored);
49 EXPECT_EQ(p->error(), "1:2: expected '}'");
50 }
51
TEST_F(ParserImplTest,BodyStmt_MissingRightParen)52 TEST_F(ParserImplTest, BodyStmt_MissingRightParen) {
53 auto p = parser("{return;");
54 auto e = p->expect_body_stmt();
55 ASSERT_TRUE(p->has_error());
56 ASSERT_TRUE(e.errored);
57 EXPECT_EQ(p->error(), "1:9: expected '}'");
58 }
59
60 } // namespace
61 } // namespace wgsl
62 } // namespace reader
63 } // namespace tint
64