Add unit tests for UnaryOpExpression
This CL adds unit tests for the UnaryOpExpression AST element. Bug: tint:11 Change-Id: I086d167c3b2ef0764a45a41268222254adba4017 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/16741 Reviewed-by: Sarah Mashayekhi <sarahmashay@google.com>
This commit is contained in:
parent
8584c6f7fe
commit
750d0e7bd1
|
@ -247,6 +247,7 @@ set(TINT_TEST_SRCS
|
|||
ast/uint_literal_test.cc
|
||||
ast/unary_derivative_expression_test.cc
|
||||
ast/unary_method_expression_test.cc
|
||||
ast/unary_op_expression_test.cc
|
||||
ast/variable_test.cc
|
||||
reader/wgsl/lexer_test.cc
|
||||
reader/wgsl/parser_test.cc
|
||||
|
|
|
@ -17,6 +17,8 @@
|
|||
namespace tint {
|
||||
namespace ast {
|
||||
|
||||
UnaryOpExpression::UnaryOpExpression() : Expression() {}
|
||||
|
||||
UnaryOpExpression::UnaryOpExpression(UnaryOp op,
|
||||
std::unique_ptr<Expression> expr)
|
||||
: Expression(), op_(op), expr_(std::move(expr)) {}
|
||||
|
@ -29,12 +31,14 @@ UnaryOpExpression::UnaryOpExpression(const Source& source,
|
|||
UnaryOpExpression::~UnaryOpExpression() = default;
|
||||
|
||||
bool UnaryOpExpression::IsValid() const {
|
||||
return expr_ != nullptr;
|
||||
return expr_ != nullptr && expr_->IsValid();
|
||||
}
|
||||
|
||||
void UnaryOpExpression::to_str(std::ostream& out, size_t indent) const {
|
||||
make_indent(out, indent);
|
||||
out << "UnaryOp{" << op_ << std::endl;
|
||||
out << "UnaryOp{" << std::endl;
|
||||
make_indent(out, indent + 2);
|
||||
out << op_ << std::endl;
|
||||
expr_->to_str(out, indent + 2);
|
||||
make_indent(out, indent);
|
||||
out << "}" << std::endl;
|
||||
|
|
|
@ -28,6 +28,8 @@ namespace ast {
|
|||
/// A unary op expression
|
||||
class UnaryOpExpression : public Expression {
|
||||
public:
|
||||
/// Constructor
|
||||
UnaryOpExpression();
|
||||
/// Constructor
|
||||
/// @param op the op
|
||||
/// @param expr the expr
|
||||
|
|
|
@ -0,0 +1,80 @@
|
|||
// Copyright 2020 The Tint Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "src/ast/unary_op_expression.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/identifier_expression.h"
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
|
||||
using UnaryOpExpressionTest = testing::Test;
|
||||
|
||||
TEST_F(UnaryOpExpressionTest, Creation) {
|
||||
auto ident = std::make_unique<IdentifierExpression>("ident");
|
||||
auto ident_ptr = ident.get();
|
||||
|
||||
UnaryOpExpression u(UnaryOp::kNot, std::move(ident));
|
||||
EXPECT_EQ(u.op(), UnaryOp::kNot);
|
||||
EXPECT_EQ(u.expr(), ident_ptr);
|
||||
}
|
||||
|
||||
TEST_F(UnaryOpExpressionTest, Creation_WithSource) {
|
||||
auto ident = std::make_unique<IdentifierExpression>("ident");
|
||||
UnaryOpExpression u(Source{20, 2}, UnaryOp::kNot, std::move(ident));
|
||||
auto src = u.source();
|
||||
EXPECT_EQ(src.line, 20);
|
||||
EXPECT_EQ(src.column, 2);
|
||||
}
|
||||
|
||||
TEST_F(UnaryOpExpressionTest, IsUnaryOp) {
|
||||
UnaryOpExpression u;
|
||||
EXPECT_TRUE(u.IsUnaryOp());
|
||||
}
|
||||
|
||||
TEST_F(UnaryOpExpressionTest, IsValid) {
|
||||
auto ident = std::make_unique<IdentifierExpression>("ident");
|
||||
UnaryOpExpression u(UnaryOp::kNot, std::move(ident));
|
||||
EXPECT_TRUE(u.IsValid());
|
||||
}
|
||||
|
||||
TEST_F(UnaryOpExpressionTest, IsValid_NullExpression) {
|
||||
UnaryOpExpression u;
|
||||
u.set_op(UnaryOp::kNot);
|
||||
EXPECT_FALSE(u.IsValid());
|
||||
}
|
||||
|
||||
TEST_F(UnaryOpExpressionTest, IsValid_InvalidExpression) {
|
||||
auto ident = std::make_unique<IdentifierExpression>("");
|
||||
UnaryOpExpression u(UnaryOp::kNot, std::move(ident));
|
||||
EXPECT_FALSE(u.IsValid());
|
||||
}
|
||||
|
||||
TEST_F(UnaryOpExpressionTest, ToStr) {
|
||||
auto ident = std::make_unique<IdentifierExpression>("ident");
|
||||
UnaryOpExpression u(UnaryOp::kNot, std::move(ident));
|
||||
std::ostringstream out;
|
||||
u.to_str(out, 2);
|
||||
EXPECT_EQ(out.str(), R"( UnaryOp{
|
||||
not
|
||||
Identifier{ident}
|
||||
}
|
||||
)");
|
||||
}
|
||||
|
||||
} // namespace ast
|
||||
} // namespace tint
|
Loading…
Reference in New Issue