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 | |
| 15 | #include "gtest/gtest.h" |
| 16 | #include "src/ast/bool_literal.h" |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 17 | #include "src/ast/identifier_expression.h" |
| 18 | #include "src/ast/relational_expression.h" |
dan sinclair | a322f5d | 2020-03-30 22:46:06 +0000 | [diff] [blame^] | 19 | #include "src/ast/scalar_constructor_expression.h" |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 20 | #include "src/reader/wgsl/parser_impl.h" |
dan sinclair | 9981b63 | 2020-03-25 19:16:36 +0000 | [diff] [blame] | 21 | #include "src/reader/wgsl/parser_impl_test_helper.h" |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 22 | |
| 23 | namespace tint { |
| 24 | namespace reader { |
| 25 | namespace wgsl { |
dan sinclair | 989cee6 | 2020-03-26 15:31:43 +0000 | [diff] [blame] | 26 | namespace { |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 27 | |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 28 | TEST_F(ParserImplTest, AndExpression_Parses) { |
dan sinclair | 9981b63 | 2020-03-25 19:16:36 +0000 | [diff] [blame] | 29 | auto p = parser("a & true"); |
| 30 | auto e = p->and_expression(); |
| 31 | ASSERT_FALSE(p->has_error()) << p->error(); |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 32 | ASSERT_NE(e, nullptr); |
| 33 | |
| 34 | ASSERT_TRUE(e->IsRelational()); |
| 35 | auto rel = e->AsRelational(); |
| 36 | EXPECT_EQ(ast::Relation::kAnd, rel->relation()); |
| 37 | |
| 38 | ASSERT_TRUE(rel->lhs()->IsIdentifier()); |
| 39 | auto ident = rel->lhs()->AsIdentifier(); |
| 40 | ASSERT_EQ(ident->name().size(), 1); |
| 41 | EXPECT_EQ(ident->name()[0], "a"); |
| 42 | |
dan sinclair | a322f5d | 2020-03-30 22:46:06 +0000 | [diff] [blame^] | 43 | ASSERT_TRUE(rel->rhs()->IsConstructor()); |
| 44 | ASSERT_TRUE(rel->rhs()->AsConstructor()->IsScalarConstructor()); |
| 45 | auto init = rel->rhs()->AsConstructor()->AsScalarConstructor(); |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 46 | ASSERT_TRUE(init->literal()->IsBool()); |
| 47 | ASSERT_TRUE(init->literal()->AsBool()->IsTrue()); |
| 48 | } |
| 49 | |
| 50 | TEST_F(ParserImplTest, AndExpression_InvalidLHS) { |
dan sinclair | 9981b63 | 2020-03-25 19:16:36 +0000 | [diff] [blame] | 51 | auto p = parser("if (a) {} & true"); |
| 52 | auto e = p->and_expression(); |
| 53 | ASSERT_FALSE(p->has_error()) << p->error(); |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 54 | ASSERT_EQ(e, nullptr); |
| 55 | } |
| 56 | |
| 57 | TEST_F(ParserImplTest, AndExpression_InvalidRHS) { |
dan sinclair | 9981b63 | 2020-03-25 19:16:36 +0000 | [diff] [blame] | 58 | auto p = parser("true & if (a) {}"); |
| 59 | auto e = p->and_expression(); |
| 60 | ASSERT_TRUE(p->has_error()); |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 61 | ASSERT_EQ(e, nullptr); |
dan sinclair | 9981b63 | 2020-03-25 19:16:36 +0000 | [diff] [blame] | 62 | 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] | 63 | } |
| 64 | |
| 65 | TEST_F(ParserImplTest, AndExpression_NoOr_ReturnsLHS) { |
dan sinclair | 9981b63 | 2020-03-25 19:16:36 +0000 | [diff] [blame] | 66 | auto p = parser("a true"); |
| 67 | auto e = p->and_expression(); |
| 68 | ASSERT_FALSE(p->has_error()) << p->error(); |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 69 | ASSERT_NE(e, nullptr); |
| 70 | ASSERT_TRUE(e->IsIdentifier()); |
| 71 | } |
| 72 | |
dan sinclair | 989cee6 | 2020-03-26 15:31:43 +0000 | [diff] [blame] | 73 | } // namespace |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 74 | } // namespace wgsl |
| 75 | } // namespace reader |
| 76 | } // namespace tint |