From 02d94b2903c54ff8e510f7ba4fe6147b46b55f92 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Tue, 17 Mar 2020 14:57:16 +0000 Subject: [PATCH] Add UnlessStatement tests. This CL adds tests for the UnlessStatement AST elements. Bug: tint:11 Change-Id: Icd003e81925d60c6681e447be2f1a5610564eda7 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/16742 Reviewed-by: Sarah Mashayekhi --- src/CMakeLists.txt | 1 + src/ast/unless_statement.cc | 25 ++++++- src/ast/unless_statement.h | 2 + src/ast/unless_statement_test.cc | 124 +++++++++++++++++++++++++++++++ 4 files changed, 148 insertions(+), 4 deletions(-) create mode 100644 src/ast/unless_statement_test.cc diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 52c53c6f8f..86166c6ad4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -248,6 +248,7 @@ set(TINT_TEST_SRCS ast/unary_derivative_expression_test.cc ast/unary_method_expression_test.cc ast/unary_op_expression_test.cc + ast/unless_statement_test.cc ast/variable_test.cc reader/wgsl/lexer_test.cc reader/wgsl/parser_test.cc diff --git a/src/ast/unless_statement.cc b/src/ast/unless_statement.cc index 7f591b07d2..10f287df16 100644 --- a/src/ast/unless_statement.cc +++ b/src/ast/unless_statement.cc @@ -17,6 +17,8 @@ namespace tint { namespace ast { +UnlessStatement::UnlessStatement() : Statement() {} + UnlessStatement::UnlessStatement(std::unique_ptr condition, std::vector> body) : Statement(), condition_(std::move(condition)), body_(std::move(body)) {} @@ -31,22 +33,37 @@ UnlessStatement::UnlessStatement(const Source& source, UnlessStatement::~UnlessStatement() = default; bool UnlessStatement::IsValid() const { - return condition_ != nullptr; + if (condition_ == nullptr || !condition_->IsValid()) { + return false; + } + for (const auto& stmt : body_) { + if (stmt == nullptr || !stmt->IsValid()) { + return false; + } + } + return true; } void UnlessStatement::to_str(std::ostream& out, size_t indent) const { make_indent(out, indent); out << "Unless{" << std::endl; - condition_->to_str(out, indent + 2); - make_indent(out, indent); + make_indent(out, indent + 2); + out << "(" << std::endl; + + condition_->to_str(out, indent + 4); + + make_indent(out, indent + 2); + out << ")" << std::endl; + + make_indent(out, indent + 2); out << "{" << std::endl; for (const auto& stmt : body_) stmt->to_str(out, indent + 4); make_indent(out, indent + 2); - out << "}"; + out << "}" << std::endl; make_indent(out, indent); out << "}" << std::endl; diff --git a/src/ast/unless_statement.h b/src/ast/unless_statement.h index bafbb43848..718eded8de 100644 --- a/src/ast/unless_statement.h +++ b/src/ast/unless_statement.h @@ -28,6 +28,8 @@ namespace ast { /// A unless statement class UnlessStatement : public Statement { public: + /// Constructor + UnlessStatement(); /// Constructor /// @param condition the condition expression /// @param body the body statements diff --git a/src/ast/unless_statement_test.cc b/src/ast/unless_statement_test.cc new file mode 100644 index 0000000000..d4f644fbab --- /dev/null +++ b/src/ast/unless_statement_test.cc @@ -0,0 +1,124 @@ +// 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/unless_statement.h" + +#include "gtest/gtest.h" +#include "src/ast/identifier_expression.h" +#include "src/ast/if_statement.h" +#include "src/ast/nop_statement.h" + +namespace tint { +namespace ast { + +using UnlessStatementTest = testing::Test; + +TEST_F(UnlessStatementTest, Creation) { + auto ident = std::make_unique("ident"); + std::vector> body; + body.push_back(std::make_unique()); + + auto ident_ptr = ident.get(); + auto nop_ptr = body[0].get(); + + UnlessStatement u(std::move(ident), std::move(body)); + EXPECT_EQ(u.condition(), ident_ptr); + ASSERT_EQ(u.body().size(), 1); + EXPECT_EQ(u.body()[0].get(), nop_ptr); +} + +TEST_F(UnlessStatementTest, Creation_WithSource) { + auto ident = std::make_unique("ident"); + std::vector> body; + body.push_back(std::make_unique()); + + UnlessStatement u(Source{20, 2}, std::move(ident), std::move(body)); + auto src = u.source(); + EXPECT_EQ(src.line, 20); + EXPECT_EQ(src.column, 2); +} + +TEST_F(UnlessStatementTest, IsUnless) { + UnlessStatement stmt; + EXPECT_TRUE(stmt.IsUnless()); +} + +TEST_F(UnlessStatementTest, IsValid) { + auto ident = std::make_unique("ident"); + std::vector> body; + body.push_back(std::make_unique()); + + UnlessStatement u(std::move(ident), std::move(body)); + EXPECT_TRUE(u.IsValid()); +} + +TEST_F(UnlessStatementTest, IsValid_NullCondition) { + std::vector> body; + body.push_back(std::make_unique()); + + UnlessStatement u; + u.set_body(std::move(body)); + EXPECT_FALSE(u.IsValid()); +} + +TEST_F(UnlessStatementTest, IsValid_InvalidCondition) { + auto ident = std::make_unique(""); + std::vector> body; + body.push_back(std::make_unique()); + + UnlessStatement u(std::move(ident), std::move(body)); + EXPECT_FALSE(u.IsValid()); +} + +TEST_F(UnlessStatementTest, IsValid_NullBodyStatement) { + auto ident = std::make_unique("ident"); + std::vector> body; + body.push_back(std::make_unique()); + body.push_back(nullptr); + + UnlessStatement u(std::move(ident), std::move(body)); + EXPECT_FALSE(u.IsValid()); +} + +TEST_F(UnlessStatementTest, IsValid_InvalidBodyStatement) { + auto ident = std::make_unique("ident"); + std::vector> body; + body.push_back(std::make_unique()); + body.push_back(std::make_unique()); + + UnlessStatement u(std::move(ident), std::move(body)); + EXPECT_FALSE(u.IsValid()); +} + +TEST_F(UnlessStatementTest, ToStr) { + auto ident = std::make_unique("ident"); + std::vector> body; + body.push_back(std::make_unique()); + + UnlessStatement u(std::move(ident), std::move(body)); + std::ostringstream out; + u.to_str(out, 2); + EXPECT_EQ(out.str(), R"( Unless{ + ( + Identifier{ident} + ) + { + Nop{} + } + } +)"); +} + +} // namespace ast +} // namespace tint