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 "src/ast/unary_op_expression.h"
16
17 #include <sstream>
18
19 #include "gtest/gtest.h"
20 #include "src/ast/identifier_expression.h"
21
22 namespace tint {
23 namespace ast {
24 namespace {
25
26 using UnaryOpExpressionTest = testing::Test;
27
TEST_F(UnaryOpExpressionTest,Creation)28 TEST_F(UnaryOpExpressionTest, Creation) {
29 auto ident = std::make_unique<IdentifierExpression>("ident");
30 auto* ident_ptr = ident.get();
31
32 UnaryOpExpression u(UnaryOp::kNot, std::move(ident));
33 EXPECT_EQ(u.op(), UnaryOp::kNot);
34 EXPECT_EQ(u.expr(), ident_ptr);
35 }
36
TEST_F(UnaryOpExpressionTest,Creation_WithSource)37 TEST_F(UnaryOpExpressionTest, Creation_WithSource) {
38 auto ident = std::make_unique<IdentifierExpression>("ident");
39 UnaryOpExpression u(Source{Source::Location{20, 2}}, UnaryOp::kNot,
40 std::move(ident));
41 auto src = u.source();
42 EXPECT_EQ(src.range.begin.line, 20u);
43 EXPECT_EQ(src.range.begin.column, 2u);
44 }
45
TEST_F(UnaryOpExpressionTest,IsUnaryOp)46 TEST_F(UnaryOpExpressionTest, IsUnaryOp) {
47 UnaryOpExpression u;
48 EXPECT_TRUE(u.IsUnaryOp());
49 }
50
TEST_F(UnaryOpExpressionTest,IsValid)51 TEST_F(UnaryOpExpressionTest, IsValid) {
52 auto ident = std::make_unique<IdentifierExpression>("ident");
53 UnaryOpExpression u(UnaryOp::kNot, std::move(ident));
54 EXPECT_TRUE(u.IsValid());
55 }
56
TEST_F(UnaryOpExpressionTest,IsValid_NullExpression)57 TEST_F(UnaryOpExpressionTest, IsValid_NullExpression) {
58 UnaryOpExpression u;
59 u.set_op(UnaryOp::kNot);
60 EXPECT_FALSE(u.IsValid());
61 }
62
TEST_F(UnaryOpExpressionTest,IsValid_InvalidExpression)63 TEST_F(UnaryOpExpressionTest, IsValid_InvalidExpression) {
64 auto ident = std::make_unique<IdentifierExpression>("");
65 UnaryOpExpression u(UnaryOp::kNot, std::move(ident));
66 EXPECT_FALSE(u.IsValid());
67 }
68
TEST_F(UnaryOpExpressionTest,ToStr)69 TEST_F(UnaryOpExpressionTest, ToStr) {
70 auto ident = std::make_unique<IdentifierExpression>("ident");
71 UnaryOpExpression u(UnaryOp::kNot, std::move(ident));
72 std::ostringstream out;
73 u.to_str(out, 2);
74 EXPECT_EQ(out.str(), R"( UnaryOp{
75 not
76 Identifier{ident}
77 }
78 )");
79 }
80
81 } // namespace
82 } // namespace ast
83 } // namespace tint
84