diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8b89ac31f8..6846c123d1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -204,6 +204,7 @@ set(TINT_TEST_SRCS ast/case_statement_test.cc ast/cast_expression_test.cc ast/const_initializer_expression_test.cc + ast/continue_statement_test.cc ast/entry_point_test.cc ast/import_test.cc ast/int_literal_test.cc diff --git a/src/ast/continue_statement.cc b/src/ast/continue_statement.cc index e1544558c4..0485020154 100644 --- a/src/ast/continue_statement.cc +++ b/src/ast/continue_statement.cc @@ -17,6 +17,11 @@ namespace tint { namespace ast { +ContinueStatement::ContinueStatement() : Statement() {} + +ContinueStatement::ContinueStatement(const Source& source) + : Statement(source) {} + ContinueStatement::ContinueStatement(StatementCondition condition, std::unique_ptr conditional) : Statement(), @@ -33,24 +38,26 @@ ContinueStatement::ContinueStatement(const Source& source, ContinueStatement::~ContinueStatement() = default; 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 { make_indent(out, indent); - out << "Continue"; + out << "Continue{"; if (condition_ != StatementCondition::kNone) { - out << "{" << std::endl; + out << std::endl; make_indent(out, indent + 2); out << condition_ << std::endl; conditional_->to_str(out, indent + 2); make_indent(out, indent); - out << "}"; } - out << std::endl; + out << "}" << std::endl; } } // namespace ast diff --git a/src/ast/continue_statement.h b/src/ast/continue_statement.h index 8cadbb7866..26789f9028 100644 --- a/src/ast/continue_statement.h +++ b/src/ast/continue_statement.h @@ -31,6 +31,9 @@ class ContinueStatement : public Statement { /// Constructor ContinueStatement(); /// Constructor + /// @param source the initializer source + explicit ContinueStatement(const Source& source); + /// Constructor /// @param condition the condition type /// @param conditional the condition expression ContinueStatement(StatementCondition condition, diff --git a/src/ast/continue_statement_test.cc b/src/ast/continue_statement_test.cc new file mode 100644 index 0000000000..366bb4df91 --- /dev/null +++ b/src/ast/continue_statement_test.cc @@ -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("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("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("expr"); + ContinueStatement stmt(StatementCondition::kIf, std::move(expr)); + EXPECT_TRUE(stmt.IsValid()); +} + +TEST_F(ContinueStatementTest, IsValid_InvalidConditional) { + auto expr = std::make_unique(""); + ContinueStatement stmt(StatementCondition::kIf, std::move(expr)); + EXPECT_FALSE(stmt.IsValid()); +} + +TEST_F(ContinueStatementTest, IsValid_NoneConditionWithConditional) { + auto expr = std::make_unique("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("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