Add ContinueStatement unit test

This CL adds tests for the continue statement AST node.

Bug: tint:11
Change-Id: I6339947c338b4b035f7947cb45e33bf2dcb6fe3e
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/16464
Commit-Queue: Sarah Mashayekhi <sarahmashay@google.com>
Reviewed-by: Sarah Mashayekhi <sarahmashay@google.com>
This commit is contained in:
Dan Sinclair 2020-03-09 13:23:22 +00:00 committed by dan sinclair
parent f981455a1c
commit b893115c17
4 changed files with 129 additions and 5 deletions

View File

@ -204,6 +204,7 @@ set(TINT_TEST_SRCS
ast/case_statement_test.cc ast/case_statement_test.cc
ast/cast_expression_test.cc ast/cast_expression_test.cc
ast/const_initializer_expression_test.cc ast/const_initializer_expression_test.cc
ast/continue_statement_test.cc
ast/entry_point_test.cc ast/entry_point_test.cc
ast/import_test.cc ast/import_test.cc
ast/int_literal_test.cc ast/int_literal_test.cc

View File

@ -17,6 +17,11 @@
namespace tint { namespace tint {
namespace ast { namespace ast {
ContinueStatement::ContinueStatement() : Statement() {}
ContinueStatement::ContinueStatement(const Source& source)
: Statement(source) {}
ContinueStatement::ContinueStatement(StatementCondition condition, ContinueStatement::ContinueStatement(StatementCondition condition,
std::unique_ptr<Expression> conditional) std::unique_ptr<Expression> conditional)
: Statement(), : Statement(),
@ -33,24 +38,26 @@ ContinueStatement::ContinueStatement(const Source& source,
ContinueStatement::~ContinueStatement() = default; ContinueStatement::~ContinueStatement() = default;
bool ContinueStatement::IsValid() const { bool ContinueStatement::IsValid() const {
return condition_ == StatementCondition::kNone || conditional_ != nullptr; if (condition_ == StatementCondition::kNone)
return conditional_ == nullptr;
return conditional_ && conditional_->IsValid();
} }
void ContinueStatement::to_str(std::ostream& out, size_t indent) const { void ContinueStatement::to_str(std::ostream& out, size_t indent) const {
make_indent(out, indent); make_indent(out, indent);
out << "Continue"; out << "Continue{";
if (condition_ != StatementCondition::kNone) { if (condition_ != StatementCondition::kNone) {
out << "{" << std::endl; out << std::endl;
make_indent(out, indent + 2); make_indent(out, indent + 2);
out << condition_ << std::endl; out << condition_ << std::endl;
conditional_->to_str(out, indent + 2); conditional_->to_str(out, indent + 2);
make_indent(out, indent); make_indent(out, indent);
out << "}";
} }
out << std::endl; out << "}" << std::endl;
} }
} // namespace ast } // namespace ast

View File

@ -31,6 +31,9 @@ class ContinueStatement : public Statement {
/// Constructor /// Constructor
ContinueStatement(); ContinueStatement();
/// Constructor /// Constructor
/// @param source the initializer source
explicit ContinueStatement(const Source& source);
/// Constructor
/// @param condition the condition type /// @param condition the condition type
/// @param conditional the condition expression /// @param conditional the condition expression
ContinueStatement(StatementCondition condition, ContinueStatement(StatementCondition condition,

View File

@ -0,0 +1,113 @@
// 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/continue_statement.h"
#include "gtest/gtest.h"
#include "src/ast/identifier_expression.h"
#include "src/ast/statement_condition.h"
namespace tint {
namespace ast {
using ContinueStatementTest = testing::Test;
TEST_F(ContinueStatementTest, Creation) {
ContinueStatement stmt;
EXPECT_EQ(stmt.condition(), StatementCondition::kNone);
EXPECT_EQ(stmt.conditional(), nullptr);
}
TEST_F(ContinueStatementTest, CreationWithConditional) {
auto expr = std::make_unique<IdentifierExpression>("expr");
auto expr_ptr = expr.get();
ContinueStatement stmt(StatementCondition::kIf, std::move(expr));
EXPECT_EQ(stmt.condition(), StatementCondition::kIf);
EXPECT_EQ(stmt.conditional(), expr_ptr);
}
TEST_F(ContinueStatementTest, Creation_WithSource) {
ContinueStatement stmt(Source{20, 2});
auto src = stmt.source();
EXPECT_EQ(src.line, 20);
EXPECT_EQ(src.column, 2);
}
TEST_F(ContinueStatementTest, Creation_WithSourceAndCondition) {
auto expr = std::make_unique<IdentifierExpression>("expr");
ContinueStatement stmt(Source{20, 2}, StatementCondition::kUnless,
std::move(expr));
auto src = stmt.source();
EXPECT_EQ(src.line, 20);
EXPECT_EQ(src.column, 2);
}
TEST_F(ContinueStatementTest, IsContinue) {
ContinueStatement stmt;
EXPECT_TRUE(stmt.IsContinue());
}
TEST_F(ContinueStatementTest, IsValid_WithoutCondition) {
ContinueStatement stmt;
EXPECT_TRUE(stmt.IsValid());
}
TEST_F(ContinueStatementTest, IsValid_WithCondition) {
auto expr = std::make_unique<IdentifierExpression>("expr");
ContinueStatement stmt(StatementCondition::kIf, std::move(expr));
EXPECT_TRUE(stmt.IsValid());
}
TEST_F(ContinueStatementTest, IsValid_InvalidConditional) {
auto expr = std::make_unique<IdentifierExpression>("");
ContinueStatement stmt(StatementCondition::kIf, std::move(expr));
EXPECT_FALSE(stmt.IsValid());
}
TEST_F(ContinueStatementTest, IsValid_NoneConditionWithConditional) {
auto expr = std::make_unique<IdentifierExpression>("expr");
ContinueStatement stmt(StatementCondition::kNone, std::move(expr));
EXPECT_FALSE(stmt.IsValid());
}
TEST_F(ContinueStatementTest, IsValid_WithCondition_MissingConditional) {
ContinueStatement stmt;
stmt.set_condition(StatementCondition::kIf);
EXPECT_FALSE(stmt.IsValid());
}
TEST_F(ContinueStatementTest, ToStr_WithoutCondition) {
ContinueStatement stmt;
std::ostringstream out;
stmt.to_str(out, 2);
EXPECT_EQ(out.str(), R"( Continue{}
)");
}
TEST_F(ContinueStatementTest, ToStr_WithCondition) {
auto expr = std::make_unique<IdentifierExpression>("expr");
ContinueStatement stmt(StatementCondition::kUnless, std::move(expr));
std::ostringstream out;
stmt.to_str(out, 2);
EXPECT_EQ(out.str(), R"( Continue{
unless
Identifier{expr}
}
)");
}
} // namespace ast
} // namespace tint