Add unit tests for UnaryMethodExpression.

This CL adds unit tests for the UnaryMethodExpression AST element.

Bug: tint:11
Change-Id: I5e99bb15f1333c1fa7ff34efafd86739c6a1d662
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/16740
Reviewed-by: Sarah Mashayekhi <sarahmashay@google.com>
This commit is contained in:
Dan Sinclair 2020-03-17 14:20:11 +00:00 committed by dan sinclair
parent f4434cd403
commit 8584c6f7fe
4 changed files with 114 additions and 2 deletions

View File

@ -246,6 +246,7 @@ set(TINT_TEST_SRCS
ast/type_initializer_expression_test.cc ast/type_initializer_expression_test.cc
ast/uint_literal_test.cc ast/uint_literal_test.cc
ast/unary_derivative_expression_test.cc ast/unary_derivative_expression_test.cc
ast/unary_method_expression_test.cc
ast/variable_test.cc ast/variable_test.cc
reader/wgsl/lexer_test.cc reader/wgsl/lexer_test.cc
reader/wgsl/parser_test.cc reader/wgsl/parser_test.cc

View File

@ -17,6 +17,8 @@
namespace tint { namespace tint {
namespace ast { namespace ast {
UnaryMethodExpression::UnaryMethodExpression() : Expression() {}
UnaryMethodExpression::UnaryMethodExpression( UnaryMethodExpression::UnaryMethodExpression(
UnaryMethod op, UnaryMethod op,
std::vector<std::unique_ptr<Expression>> params) std::vector<std::unique_ptr<Expression>> params)
@ -31,6 +33,14 @@ UnaryMethodExpression::UnaryMethodExpression(
UnaryMethodExpression::~UnaryMethodExpression() = default; UnaryMethodExpression::~UnaryMethodExpression() = default;
bool UnaryMethodExpression::IsValid() const { bool UnaryMethodExpression::IsValid() const {
if (params_.empty()) {
return false;
}
for (const auto& p : params_) {
if (p == nullptr || !p->IsValid()) {
return false;
}
}
return true; return true;
} }
@ -42,10 +52,9 @@ void UnaryMethodExpression::to_str(std::ostream& out, size_t indent) const {
out << op_ << std::endl; out << op_ << std::endl;
for (const auto& param : params_) { for (const auto& param : params_) {
param->to_str(out, indent + 2); param->to_str(out, indent + 2);
out << std::endl;
} }
make_indent(out, indent); make_indent(out, indent);
out << "}"; out << "}" << std::endl;
} }
} // namespace ast } // namespace ast

View File

@ -29,6 +29,8 @@ namespace ast {
/// A unary method expression /// A unary method expression
class UnaryMethodExpression : public Expression { class UnaryMethodExpression : public Expression {
public: public:
/// Constructor
UnaryMethodExpression();
/// Constructor /// Constructor
/// @param op the op /// @param op the op
/// @param params the params /// @param params the params

View File

@ -0,0 +1,100 @@
// 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_method_expression.h"
#include <sstream>
#include "gtest/gtest.h"
#include "src/ast/identifier_expression.h"
namespace tint {
namespace ast {
using UnaryMethodExpressionTest = testing::Test;
TEST_F(UnaryMethodExpressionTest, Creation) {
std::vector<std::unique_ptr<Expression>> params;
params.push_back(std::make_unique<IdentifierExpression>("ident"));
auto ident_ptr = params[0].get();
UnaryMethodExpression u(UnaryMethod::kAll, std::move(params));
EXPECT_EQ(u.op(), UnaryMethod::kAll);
ASSERT_EQ(u.params().size(), 1);
EXPECT_EQ(u.params()[0].get(), ident_ptr);
}
TEST_F(UnaryMethodExpressionTest, Creation_WithSource) {
std::vector<std::unique_ptr<Expression>> params;
params.push_back(std::make_unique<IdentifierExpression>("ident"));
UnaryMethodExpression u(Source{20, 2}, UnaryMethod::kAll, std::move(params));
auto src = u.source();
EXPECT_EQ(src.line, 20);
EXPECT_EQ(src.column, 2);
}
TEST_F(UnaryMethodExpressionTest, IsUnaryMethod) {
UnaryMethodExpression u;
EXPECT_TRUE(u.IsUnaryMethod());
}
TEST_F(UnaryMethodExpressionTest, IsValid) {
std::vector<std::unique_ptr<Expression>> params;
params.push_back(std::make_unique<IdentifierExpression>("ident"));
UnaryMethodExpression u(UnaryMethod::kAll, std::move(params));
EXPECT_TRUE(u.IsValid());
}
TEST_F(UnaryMethodExpressionTest, IsValid_NullParam) {
std::vector<std::unique_ptr<Expression>> params;
params.push_back(std::make_unique<IdentifierExpression>("ident"));
params.push_back(nullptr);
UnaryMethodExpression u(UnaryMethod::kAll, std::move(params));
EXPECT_FALSE(u.IsValid());
}
TEST_F(UnaryMethodExpressionTest, IsValid_InvalidParam) {
std::vector<std::unique_ptr<Expression>> params;
params.push_back(std::make_unique<IdentifierExpression>(""));
UnaryMethodExpression u(UnaryMethod::kAll, std::move(params));
EXPECT_FALSE(u.IsValid());
}
TEST_F(UnaryMethodExpressionTest, IsValid_EmptyParams) {
UnaryMethodExpression u;
u.set_op(UnaryMethod::kAll);
EXPECT_FALSE(u.IsValid());
}
TEST_F(UnaryMethodExpressionTest, ToStr) {
std::vector<std::unique_ptr<Expression>> params;
params.push_back(std::make_unique<IdentifierExpression>("ident"));
UnaryMethodExpression u(UnaryMethod::kAll, std::move(params));
std::ostringstream out;
u.to_str(out, 2);
EXPECT_EQ(out.str(), R"( UnaryMethod{
all
Identifier{ident}
}
)");
}
} // namespace ast
} // namespace tint