Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 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 | |
dan sinclair | 9981b63 | 2020-03-25 19:16:36 +0000 | [diff] [blame] | 15 | #include "src/reader/wgsl/parser_impl_test_helper.h" |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 16 | |
| 17 | namespace tint { |
| 18 | namespace reader { |
| 19 | namespace wgsl { |
dan sinclair | 989cee6 | 2020-03-26 15:31:43 +0000 | [diff] [blame] | 20 | namespace { |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 21 | |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 22 | TEST_F(ParserImplTest, InclusiveOrExpression_Parses) { |
Ben Clayton | 627732c | 2020-11-18 19:40:00 +0000 | [diff] [blame] | 23 | auto p = parser("a | true"); |
dan sinclair | 9981b63 | 2020-03-25 19:16:36 +0000 | [diff] [blame] | 24 | auto e = p->inclusive_or_expression(); |
Ben Clayton | ab5dfee | 2020-11-09 19:52:24 +0000 | [diff] [blame] | 25 | EXPECT_TRUE(e.matched); |
| 26 | EXPECT_FALSE(e.errored); |
| 27 | EXPECT_FALSE(p->has_error()) << p->error(); |
| 28 | ASSERT_NE(e.value, nullptr); |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 29 | |
Ben Clayton | d6ae990 | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 30 | ASSERT_TRUE(e->Is<ast::BinaryExpression>()); |
| 31 | auto* rel = e->As<ast::BinaryExpression>(); |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 32 | EXPECT_EQ(ast::BinaryOp::kOr, rel->op()); |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 33 | |
Ben Clayton | d6ae990 | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 34 | ASSERT_TRUE(rel->lhs()->Is<ast::IdentifierExpression>()); |
| 35 | auto* ident = rel->lhs()->As<ast::IdentifierExpression>(); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 36 | EXPECT_EQ(ident->symbol(), p->builder().Symbols().Get("a")); |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 37 | |
Ben Clayton | d6ae990 | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 38 | ASSERT_TRUE(rel->rhs()->Is<ast::ConstructorExpression>()); |
| 39 | ASSERT_TRUE(rel->rhs()->Is<ast::ScalarConstructorExpression>()); |
| 40 | auto* init = rel->rhs()->As<ast::ScalarConstructorExpression>(); |
Ben Clayton | acf7643 | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 41 | ASSERT_TRUE(init->literal()->Is<ast::BoolLiteral>()); |
| 42 | ASSERT_TRUE(init->literal()->As<ast::BoolLiteral>()->IsTrue()); |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | TEST_F(ParserImplTest, InclusiveOrExpression_InvalidLHS) { |
Ben Clayton | 627732c | 2020-11-18 19:40:00 +0000 | [diff] [blame] | 46 | auto p = parser("if (a) {} | true"); |
dan sinclair | 9981b63 | 2020-03-25 19:16:36 +0000 | [diff] [blame] | 47 | auto e = p->inclusive_or_expression(); |
Ben Clayton | ab5dfee | 2020-11-09 19:52:24 +0000 | [diff] [blame] | 48 | EXPECT_FALSE(e.matched); |
| 49 | EXPECT_FALSE(e.errored); |
| 50 | EXPECT_FALSE(p->has_error()) << p->error(); |
| 51 | ASSERT_EQ(e.value, nullptr); |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | TEST_F(ParserImplTest, InclusiveOrExpression_InvalidRHS) { |
Ben Clayton | 627732c | 2020-11-18 19:40:00 +0000 | [diff] [blame] | 55 | auto p = parser("true | if (a) {}"); |
dan sinclair | 9981b63 | 2020-03-25 19:16:36 +0000 | [diff] [blame] | 56 | auto e = p->inclusive_or_expression(); |
Ben Clayton | ab5dfee | 2020-11-09 19:52:24 +0000 | [diff] [blame] | 57 | EXPECT_FALSE(e.matched); |
| 58 | EXPECT_TRUE(e.errored); |
| 59 | EXPECT_EQ(e.value, nullptr); |
| 60 | EXPECT_TRUE(p->has_error()); |
dan sinclair | 9981b63 | 2020-03-25 19:16:36 +0000 | [diff] [blame] | 61 | EXPECT_EQ(p->error(), "1:8: unable to parse right side of | expression"); |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | TEST_F(ParserImplTest, InclusiveOrExpression_NoOr_ReturnsLHS) { |
Ben Clayton | 627732c | 2020-11-18 19:40:00 +0000 | [diff] [blame] | 65 | auto p = parser("a true"); |
dan sinclair | 9981b63 | 2020-03-25 19:16:36 +0000 | [diff] [blame] | 66 | auto e = p->inclusive_or_expression(); |
Ben Clayton | ab5dfee | 2020-11-09 19:52:24 +0000 | [diff] [blame] | 67 | EXPECT_TRUE(e.matched); |
| 68 | EXPECT_FALSE(e.errored); |
| 69 | EXPECT_FALSE(p->has_error()) << p->error(); |
| 70 | ASSERT_NE(e.value, nullptr); |
Ben Clayton | d6ae990 | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 71 | ASSERT_TRUE(e->Is<ast::IdentifierExpression>()); |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 72 | } |
| 73 | |
dan sinclair | 989cee6 | 2020-03-26 15:31:43 +0000 | [diff] [blame] | 74 | } // namespace |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 75 | } // namespace wgsl |
| 76 | } // namespace reader |
| 77 | } // namespace tint |