blob: 4f55afe8f0dcafe00ca7b16dc86e3b224a6f3a63 [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
15#include "gtest/gtest.h"
16#include "src/ast/bool_literal.h"
Dan Sinclair6e581892020-03-02 15:47:43 -050017#include "src/ast/identifier_expression.h"
18#include "src/ast/relational_expression.h"
dan sinclaira322f5d2020-03-30 22:46:06 +000019#include "src/ast/scalar_constructor_expression.h"
Dan Sinclair6e581892020-03-02 15:47:43 -050020#include "src/reader/wgsl/parser_impl.h"
dan sinclair9981b632020-03-25 19:16:36 +000021#include "src/reader/wgsl/parser_impl_test_helper.h"
Dan Sinclair6e581892020-03-02 15:47:43 -050022
23namespace tint {
24namespace reader {
25namespace wgsl {
dan sinclair989cee62020-03-26 15:31:43 +000026namespace {
Dan Sinclair6e581892020-03-02 15:47:43 -050027
Dan Sinclair6e581892020-03-02 15:47:43 -050028TEST_F(ParserImplTest, AndExpression_Parses) {
dan sinclair9981b632020-03-25 19:16:36 +000029 auto p = parser("a & true");
30 auto e = p->and_expression();
31 ASSERT_FALSE(p->has_error()) << p->error();
Dan Sinclair6e581892020-03-02 15:47:43 -050032 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 sinclaira322f5d2020-03-30 22:46:06 +000043 ASSERT_TRUE(rel->rhs()->IsConstructor());
44 ASSERT_TRUE(rel->rhs()->AsConstructor()->IsScalarConstructor());
45 auto init = rel->rhs()->AsConstructor()->AsScalarConstructor();
Dan Sinclair6e581892020-03-02 15:47:43 -050046 ASSERT_TRUE(init->literal()->IsBool());
47 ASSERT_TRUE(init->literal()->AsBool()->IsTrue());
48}
49
50TEST_F(ParserImplTest, AndExpression_InvalidLHS) {
dan sinclair9981b632020-03-25 19:16:36 +000051 auto p = parser("if (a) {} & true");
52 auto e = p->and_expression();
53 ASSERT_FALSE(p->has_error()) << p->error();
Dan Sinclair6e581892020-03-02 15:47:43 -050054 ASSERT_EQ(e, nullptr);
55}
56
57TEST_F(ParserImplTest, AndExpression_InvalidRHS) {
dan sinclair9981b632020-03-25 19:16:36 +000058 auto p = parser("true & if (a) {}");
59 auto e = p->and_expression();
60 ASSERT_TRUE(p->has_error());
Dan Sinclair6e581892020-03-02 15:47:43 -050061 ASSERT_EQ(e, nullptr);
dan sinclair9981b632020-03-25 19:16:36 +000062 EXPECT_EQ(p->error(), "1:8: unable to parse right side of & expression");
Dan Sinclair6e581892020-03-02 15:47:43 -050063}
64
65TEST_F(ParserImplTest, AndExpression_NoOr_ReturnsLHS) {
dan sinclair9981b632020-03-25 19:16:36 +000066 auto p = parser("a true");
67 auto e = p->and_expression();
68 ASSERT_FALSE(p->has_error()) << p->error();
Dan Sinclair6e581892020-03-02 15:47:43 -050069 ASSERT_NE(e, nullptr);
70 ASSERT_TRUE(e->IsIdentifier());
71}
72
dan sinclair989cee62020-03-26 15:31:43 +000073} // namespace
Dan Sinclair6e581892020-03-02 15:47:43 -050074} // namespace wgsl
75} // namespace reader
76} // namespace tint