blob: fd90460124febb67a0da52d117d2b069f388df5b [file] [log] [blame]
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001// 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/tint/reader/wgsl/parser_impl_test_helper.h"
16
dan sinclair30a03d92022-04-07 17:55:04 +000017namespace tint::reader::wgsl {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000018namespace {
19
20TEST_F(ParserImplTest, AndExpression_Parses) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000021 auto p = parser("a & true");
22 auto e = p->and_expression();
23 EXPECT_TRUE(e.matched);
24 EXPECT_FALSE(e.errored);
25 EXPECT_FALSE(p->has_error()) << p->error();
26 ASSERT_NE(e.value, nullptr);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000027
James Price53547db2022-05-12 11:43:37 +000028 EXPECT_EQ(e->source.range.begin.line, 1u);
29 EXPECT_EQ(e->source.range.begin.column, 3u);
30 EXPECT_EQ(e->source.range.end.line, 1u);
31 EXPECT_EQ(e->source.range.end.column, 4u);
32
dan sinclair41e4d9a2022-05-01 14:40:55 +000033 ASSERT_TRUE(e->Is<ast::BinaryExpression>());
34 auto* rel = e->As<ast::BinaryExpression>();
35 EXPECT_EQ(ast::BinaryOp::kAnd, rel->op);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000036
dan sinclair41e4d9a2022-05-01 14:40:55 +000037 ASSERT_TRUE(rel->lhs->Is<ast::IdentifierExpression>());
38 auto* ident = rel->lhs->As<ast::IdentifierExpression>();
39 EXPECT_EQ(ident->symbol, p->builder().Symbols().Register("a"));
Ryan Harrisondbc13af2022-02-21 15:19:07 +000040
dan sinclair41e4d9a2022-05-01 14:40:55 +000041 ASSERT_TRUE(rel->rhs->Is<ast::BoolLiteralExpression>());
42 ASSERT_TRUE(rel->rhs->As<ast::BoolLiteralExpression>()->value);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000043}
44
45TEST_F(ParserImplTest, AndExpression_InvalidLHS) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000046 auto p = parser("if (a) {} & true");
47 auto e = p->and_expression();
48 EXPECT_FALSE(e.matched);
49 EXPECT_FALSE(e.errored);
50 EXPECT_FALSE(p->has_error()) << p->error();
51 EXPECT_EQ(e.value, nullptr);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000052}
53
54TEST_F(ParserImplTest, AndExpression_InvalidRHS) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000055 auto p = parser("true & if (a) {}");
56 auto e = p->and_expression();
57 EXPECT_FALSE(e.matched);
58 EXPECT_TRUE(e.errored);
59 EXPECT_EQ(e.value, nullptr);
60 EXPECT_TRUE(p->has_error());
61 EXPECT_EQ(p->error(), "1:8: unable to parse right side of & expression");
Ryan Harrisondbc13af2022-02-21 15:19:07 +000062}
63
64TEST_F(ParserImplTest, AndExpression_NoOr_ReturnsLHS) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000065 auto p = parser("a true");
66 auto e = p->and_expression();
67 EXPECT_TRUE(e.matched);
68 EXPECT_FALSE(e.errored);
69 EXPECT_FALSE(p->has_error()) << p->error();
70 ASSERT_NE(e.value, nullptr);
71 ASSERT_TRUE(e->Is<ast::IdentifierExpression>());
Ryan Harrisondbc13af2022-02-21 15:19:07 +000072}
73
74} // namespace
dan sinclair30a03d92022-04-07 17:55:04 +000075} // namespace tint::reader::wgsl