diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ac6c913691..52c53c6f8f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 diff --git a/src/ast/unary_op_expression.cc b/src/ast/unary_op_expression.cc index da31e8ed4b..33db0c037f 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 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; diff --git a/src/ast/unary_op_expression.h b/src/ast/unary_op_expression.h index b77c185b44..934a95f5ca 100644 --- a/src/ast/unary_op_expression.h +++ b/src/ast/unary_op_expression.h @@ -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 diff --git a/src/ast/unary_op_expression_test.cc b/src/ast/unary_op_expression_test.cc new file mode 100644 index 0000000000..fef07d5ca3 --- /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 + +#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("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("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("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(""); + UnaryOpExpression u(UnaryOp::kNot, std::move(ident)); + EXPECT_FALSE(u.IsValid()); +} + +TEST_F(UnaryOpExpressionTest, ToStr) { + auto ident = std::make_unique("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