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,AndExpression_Parses)22 TEST_F(ParserImplTest, AndExpression_Parses) {
23 auto p = parser("a & true");
24 auto e = p->and_expression();
25 EXPECT_TRUE(e.matched);
26 EXPECT_FALSE(e.errored);
27 EXPECT_FALSE(p->has_error()) << p->error();
28 ASSERT_NE(e.value, nullptr);
29
30 ASSERT_TRUE(e->Is<ast::BinaryExpression>());
31 auto* rel = e->As<ast::BinaryExpression>();
32 EXPECT_EQ(ast::BinaryOp::kAnd, rel->op);
33
34 ASSERT_TRUE(rel->lhs->Is<ast::IdentifierExpression>());
35 auto* ident = rel->lhs->As<ast::IdentifierExpression>();
36 EXPECT_EQ(ident->symbol, p->builder().Symbols().Register("a"));
37
38 ASSERT_TRUE(rel->rhs->Is<ast::BoolLiteralExpression>());
39 ASSERT_TRUE(rel->rhs->As<ast::BoolLiteralExpression>()->value);
40 }
41
TEST_F(ParserImplTest,AndExpression_InvalidLHS)42 TEST_F(ParserImplTest, AndExpression_InvalidLHS) {
43 auto p = parser("if (a) {} & true");
44 auto e = p->and_expression();
45 EXPECT_FALSE(e.matched);
46 EXPECT_FALSE(e.errored);
47 EXPECT_FALSE(p->has_error()) << p->error();
48 EXPECT_EQ(e.value, nullptr);
49 }
50
TEST_F(ParserImplTest,AndExpression_InvalidRHS)51 TEST_F(ParserImplTest, AndExpression_InvalidRHS) {
52 auto p = parser("true & if (a) {}");
53 auto e = p->and_expression();
54 EXPECT_FALSE(e.matched);
55 EXPECT_TRUE(e.errored);
56 EXPECT_EQ(e.value, nullptr);
57 EXPECT_TRUE(p->has_error());
58 EXPECT_EQ(p->error(), "1:8: unable to parse right side of & expression");
59 }
60
TEST_F(ParserImplTest,AndExpression_NoOr_ReturnsLHS)61 TEST_F(ParserImplTest, AndExpression_NoOr_ReturnsLHS) {
62 auto p = parser("a true");
63 auto e = p->and_expression();
64 EXPECT_TRUE(e.matched);
65 EXPECT_FALSE(e.errored);
66 EXPECT_FALSE(p->has_error()) << p->error();
67 ASSERT_NE(e.value, nullptr);
68 ASSERT_TRUE(e->Is<ast::IdentifierExpression>());
69 }
70
71 } // namespace
72 } // namespace wgsl
73 } // namespace reader
74 } // namespace tint
75