• 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,AdditiveExpression_Parses_Plus)22 TEST_F(ParserImplTest, AdditiveExpression_Parses_Plus) {
23   auto p = parser("a + true");
24   auto e = p->additive_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::kAdd, 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().Get("a"));
37 
38   ASSERT_TRUE(rel->rhs->Is<ast::BoolLiteralExpression>());
39   ASSERT_TRUE(rel->rhs->As<ast::BoolLiteralExpression>()->value);
40 }
41 
TEST_F(ParserImplTest,AdditiveExpression_Parses_Minus)42 TEST_F(ParserImplTest, AdditiveExpression_Parses_Minus) {
43   auto p = parser("a - true");
44   auto e = p->additive_expression();
45   EXPECT_TRUE(e.matched);
46   EXPECT_FALSE(e.errored);
47   EXPECT_FALSE(p->has_error()) << p->error();
48   ASSERT_NE(e.value, nullptr);
49 
50   ASSERT_TRUE(e->Is<ast::BinaryExpression>());
51   auto* rel = e->As<ast::BinaryExpression>();
52   EXPECT_EQ(ast::BinaryOp::kSubtract, rel->op);
53 
54   ASSERT_TRUE(rel->lhs->Is<ast::IdentifierExpression>());
55   auto* ident = rel->lhs->As<ast::IdentifierExpression>();
56   EXPECT_EQ(ident->symbol, p->builder().Symbols().Get("a"));
57 
58   ASSERT_TRUE(rel->rhs->Is<ast::BoolLiteralExpression>());
59   ASSERT_TRUE(rel->rhs->As<ast::BoolLiteralExpression>()->value);
60 }
61 
TEST_F(ParserImplTest,AdditiveExpression_InvalidLHS)62 TEST_F(ParserImplTest, AdditiveExpression_InvalidLHS) {
63   auto p = parser("if (a) {} + true");
64   auto e = p->additive_expression();
65   EXPECT_FALSE(e.matched);
66   EXPECT_FALSE(e.errored);
67   EXPECT_FALSE(p->has_error()) << p->error();
68   EXPECT_EQ(e.value, nullptr);
69 }
70 
TEST_F(ParserImplTest,AdditiveExpression_InvalidRHS)71 TEST_F(ParserImplTest, AdditiveExpression_InvalidRHS) {
72   auto p = parser("true + if (a) {}");
73   auto e = p->additive_expression();
74   EXPECT_FALSE(e.matched);
75   EXPECT_TRUE(e.errored);
76   EXPECT_EQ(e.value, nullptr);
77   EXPECT_TRUE(p->has_error());
78   EXPECT_EQ(p->error(), "1:8: unable to parse right side of + expression");
79 }
80 
TEST_F(ParserImplTest,AdditiveExpression_NoOr_ReturnsLHS)81 TEST_F(ParserImplTest, AdditiveExpression_NoOr_ReturnsLHS) {
82   auto p = parser("a true");
83   auto e = p->additive_expression();
84   EXPECT_TRUE(e.matched);
85   EXPECT_FALSE(e.errored);
86   EXPECT_FALSE(p->has_error()) << p->error();
87   ASSERT_NE(e.value, nullptr);
88   ASSERT_TRUE(e->Is<ast::IdentifierExpression>());
89 }
90 
91 }  // namespace
92 }  // namespace wgsl
93 }  // namespace reader
94 }  // namespace tint
95