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 <sarahmashay@google.com>
This commit is contained in:
Dan Sinclair 2020-03-17 14:57:16 +00:00 committed by Sarah Mashayekhi
parent 750d0e7bd1
commit 02d94b2903
4 changed files with 148 additions and 4 deletions

View File

@ -248,6 +248,7 @@ set(TINT_TEST_SRCS
ast/unary_derivative_expression_test.cc ast/unary_derivative_expression_test.cc
ast/unary_method_expression_test.cc ast/unary_method_expression_test.cc
ast/unary_op_expression_test.cc ast/unary_op_expression_test.cc
ast/unless_statement_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 {
UnlessStatement::UnlessStatement() : Statement() {}
UnlessStatement::UnlessStatement(std::unique_ptr<Expression> condition, UnlessStatement::UnlessStatement(std::unique_ptr<Expression> condition,
std::vector<std::unique_ptr<Statement>> body) std::vector<std::unique_ptr<Statement>> body)
: Statement(), condition_(std::move(condition)), body_(std::move(body)) {} : Statement(), condition_(std::move(condition)), body_(std::move(body)) {}
@ -31,22 +33,37 @@ UnlessStatement::UnlessStatement(const Source& source,
UnlessStatement::~UnlessStatement() = default; UnlessStatement::~UnlessStatement() = default;
bool UnlessStatement::IsValid() const { 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 { void UnlessStatement::to_str(std::ostream& out, size_t indent) const {
make_indent(out, indent); make_indent(out, indent);
out << "Unless{" << std::endl; out << "Unless{" << std::endl;
condition_->to_str(out, indent + 2); make_indent(out, indent + 2);
make_indent(out, indent); 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; out << "{" << std::endl;
for (const auto& stmt : body_) for (const auto& stmt : body_)
stmt->to_str(out, indent + 4); stmt->to_str(out, indent + 4);
make_indent(out, indent + 2); make_indent(out, indent + 2);
out << "}"; out << "}" << std::endl;
make_indent(out, indent); make_indent(out, indent);
out << "}" << std::endl; out << "}" << std::endl;

View File

@ -28,6 +28,8 @@ namespace ast {
/// A unless statement /// A unless statement
class UnlessStatement : public Statement { class UnlessStatement : public Statement {
public: public:
/// Constructor
UnlessStatement();
/// Constructor /// Constructor
/// @param condition the condition expression /// @param condition the condition expression
/// @param body the body statements /// @param body the body statements

View File

@ -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<IdentifierExpression>("ident");
std::vector<std::unique_ptr<Statement>> body;
body.push_back(std::make_unique<NopStatement>());
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<IdentifierExpression>("ident");
std::vector<std::unique_ptr<Statement>> body;
body.push_back(std::make_unique<NopStatement>());
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<IdentifierExpression>("ident");
std::vector<std::unique_ptr<Statement>> body;
body.push_back(std::make_unique<NopStatement>());
UnlessStatement u(std::move(ident), std::move(body));
EXPECT_TRUE(u.IsValid());
}
TEST_F(UnlessStatementTest, IsValid_NullCondition) {
std::vector<std::unique_ptr<Statement>> body;
body.push_back(std::make_unique<NopStatement>());
UnlessStatement u;
u.set_body(std::move(body));
EXPECT_FALSE(u.IsValid());
}
TEST_F(UnlessStatementTest, IsValid_InvalidCondition) {
auto ident = std::make_unique<IdentifierExpression>("");
std::vector<std::unique_ptr<Statement>> body;
body.push_back(std::make_unique<NopStatement>());
UnlessStatement u(std::move(ident), std::move(body));
EXPECT_FALSE(u.IsValid());
}
TEST_F(UnlessStatementTest, IsValid_NullBodyStatement) {
auto ident = std::make_unique<IdentifierExpression>("ident");
std::vector<std::unique_ptr<Statement>> body;
body.push_back(std::make_unique<NopStatement>());
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<IdentifierExpression>("ident");
std::vector<std::unique_ptr<Statement>> body;
body.push_back(std::make_unique<NopStatement>());
body.push_back(std::make_unique<IfStatement>());
UnlessStatement u(std::move(ident), std::move(body));
EXPECT_FALSE(u.IsValid());
}
TEST_F(UnlessStatementTest, ToStr) {
auto ident = std::make_unique<IdentifierExpression>("ident");
std::vector<std::unique_ptr<Statement>> body;
body.push_back(std::make_unique<NopStatement>());
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