blob: 8fee841ea8c19a79e51c1eacde080c3d8db53194 [file] [log] [blame]
Dan Sinclair6e581892020-03-02 15:47:43 -05001// 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
dan sinclair9981b632020-03-25 19:16:36 +000015#include "src/reader/wgsl/parser_impl_test_helper.h"
Dan Sinclair6e581892020-03-02 15:47:43 -050016
17namespace tint {
18namespace reader {
19namespace wgsl {
dan sinclair989cee62020-03-26 15:31:43 +000020namespace {
Dan Sinclair6e581892020-03-02 15:47:43 -050021
Dan Sinclair6e581892020-03-02 15:47:43 -050022TEST_F(ParserImplTest, InclusiveOrExpression_Parses) {
Ben Clayton627732c2020-11-18 19:40:00 +000023 auto p = parser("a | true");
dan sinclair9981b632020-03-25 19:16:36 +000024 auto e = p->inclusive_or_expression();
Ben Claytonab5dfee2020-11-09 19:52:24 +000025 EXPECT_TRUE(e.matched);
26 EXPECT_FALSE(e.errored);
27 EXPECT_FALSE(p->has_error()) << p->error();
28 ASSERT_NE(e.value, nullptr);
Dan Sinclair6e581892020-03-02 15:47:43 -050029
Ben Claytond6ae9902020-11-30 23:30:58 +000030 ASSERT_TRUE(e->Is<ast::BinaryExpression>());
31 auto* rel = e->As<ast::BinaryExpression>();
dan sinclair1c9b4862020-04-07 19:27:41 +000032 EXPECT_EQ(ast::BinaryOp::kOr, rel->op());
Dan Sinclair6e581892020-03-02 15:47:43 -050033
Ben Claytond6ae9902020-11-30 23:30:58 +000034 ASSERT_TRUE(rel->lhs()->Is<ast::IdentifierExpression>());
35 auto* ident = rel->lhs()->As<ast::IdentifierExpression>();
Ben Claytona6b9a8e2021-01-26 16:57:10 +000036 EXPECT_EQ(ident->symbol(), p->builder().Symbols().Get("a"));
Dan Sinclair6e581892020-03-02 15:47:43 -050037
Ben Claytond6ae9902020-11-30 23:30:58 +000038 ASSERT_TRUE(rel->rhs()->Is<ast::ConstructorExpression>());
39 ASSERT_TRUE(rel->rhs()->Is<ast::ScalarConstructorExpression>());
40 auto* init = rel->rhs()->As<ast::ScalarConstructorExpression>();
Ben Claytonacf76432020-11-30 23:30:58 +000041 ASSERT_TRUE(init->literal()->Is<ast::BoolLiteral>());
42 ASSERT_TRUE(init->literal()->As<ast::BoolLiteral>()->IsTrue());
Dan Sinclair6e581892020-03-02 15:47:43 -050043}
44
45TEST_F(ParserImplTest, InclusiveOrExpression_InvalidLHS) {
Ben Clayton627732c2020-11-18 19:40:00 +000046 auto p = parser("if (a) {} | true");
dan sinclair9981b632020-03-25 19:16:36 +000047 auto e = p->inclusive_or_expression();
Ben Claytonab5dfee2020-11-09 19:52:24 +000048 EXPECT_FALSE(e.matched);
49 EXPECT_FALSE(e.errored);
50 EXPECT_FALSE(p->has_error()) << p->error();
51 ASSERT_EQ(e.value, nullptr);
Dan Sinclair6e581892020-03-02 15:47:43 -050052}
53
54TEST_F(ParserImplTest, InclusiveOrExpression_InvalidRHS) {
Ben Clayton627732c2020-11-18 19:40:00 +000055 auto p = parser("true | if (a) {}");
dan sinclair9981b632020-03-25 19:16:36 +000056 auto e = p->inclusive_or_expression();
Ben Claytonab5dfee2020-11-09 19:52:24 +000057 EXPECT_FALSE(e.matched);
58 EXPECT_TRUE(e.errored);
59 EXPECT_EQ(e.value, nullptr);
60 EXPECT_TRUE(p->has_error());
dan sinclair9981b632020-03-25 19:16:36 +000061 EXPECT_EQ(p->error(), "1:8: unable to parse right side of | expression");
Dan Sinclair6e581892020-03-02 15:47:43 -050062}
63
64TEST_F(ParserImplTest, InclusiveOrExpression_NoOr_ReturnsLHS) {
Ben Clayton627732c2020-11-18 19:40:00 +000065 auto p = parser("a true");
dan sinclair9981b632020-03-25 19:16:36 +000066 auto e = p->inclusive_or_expression();
Ben Claytonab5dfee2020-11-09 19:52:24 +000067 EXPECT_TRUE(e.matched);
68 EXPECT_FALSE(e.errored);
69 EXPECT_FALSE(p->has_error()) << p->error();
70 ASSERT_NE(e.value, nullptr);
Ben Claytond6ae9902020-11-30 23:30:58 +000071 ASSERT_TRUE(e->Is<ast::IdentifierExpression>());
Dan Sinclair6e581892020-03-02 15:47:43 -050072}
73
dan sinclair989cee62020-03-26 15:31:43 +000074} // namespace
Dan Sinclair6e581892020-03-02 15:47:43 -050075} // namespace wgsl
76} // namespace reader
77} // namespace tint