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>
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index ac6c913..52c53c6 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -247,6 +247,7 @@
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
diff --git a/src/ast/unary_op_expression.cc b/src/ast/unary_op_expression.cc
index da31e8e..33db0c0 100644
--- a/src/ast/unary_op_expression.cc
+++ b/src/ast/unary_op_expression.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() = 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;
diff --git a/src/ast/unary_op_expression.h b/src/ast/unary_op_expression.h
index b77c185..934a95f 100644
--- a/src/ast/unary_op_expression.h
+++ b/src/ast/unary_op_expression.h
@@ -29,6 +29,8 @@
class UnaryOpExpression : public Expression {
public:
/// Constructor
+ UnaryOpExpression();
+ /// Constructor
/// @param op the op
/// @param expr the expr
UnaryOpExpression(UnaryOp op, std::unique_ptr<Expression> expr);
diff --git a/src/ast/unary_op_expression_test.cc b/src/ast/unary_op_expression_test.cc
new file mode 100644
index 0000000..fef07d5
--- /dev/null
+++ b/src/ast/unary_op_expression_test.cc
@@ -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