From a659b2113d22dc72451e1cfa7eaaefcaca381bc2 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Fri, 6 Mar 2020 14:28:01 +0000 Subject: [PATCH] Add case statement tests This Cl adds tests for the case statement class. Change-Id: I585fdff9631ed44e6fb297328ed164297f5fa8f3 Bug: tint:11 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/16421 Reviewed-by: Sarah Mashayekhi --- src/CMakeLists.txt | 1 + src/ast/case_statement.cc | 6 +- src/ast/case_statement_test.cc | 125 +++++++++++++++++++++++++++++++++ src/ast/if_statement.h | 1 + 4 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 src/ast/case_statement_test.cc diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0434df8229..374e44a9c8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -201,6 +201,7 @@ set(TINT_TEST_SRCS ast/break_statement_test.cc ast/builtin_decoration_test.cc ast/call_expression_test.cc + ast/case_statement_test.cc ast/entry_point_test.cc ast/import_test.cc ast/int_literal_test.cc diff --git a/src/ast/case_statement.cc b/src/ast/case_statement.cc index a59122457c..98e3a475ce 100644 --- a/src/ast/case_statement.cc +++ b/src/ast/case_statement.cc @@ -33,6 +33,10 @@ CaseStatement::CaseStatement(const Source& source, CaseStatement::~CaseStatement() = default; bool CaseStatement::IsValid() const { + for (const auto& stmt : body_) { + if (stmt == nullptr || !stmt->IsValid()) + return false; + } return true; } @@ -40,7 +44,7 @@ void CaseStatement::to_str(std::ostream& out, size_t indent) const { make_indent(out, indent); if (IsDefault()) { - out << "default{" << std::endl; + out << "Default{" << std::endl; } else { out << "Case " << condition_->to_str() << "{" << std::endl; } diff --git a/src/ast/case_statement_test.cc b/src/ast/case_statement_test.cc new file mode 100644 index 0000000000..9395b52a3d --- /dev/null +++ b/src/ast/case_statement_test.cc @@ -0,0 +1,125 @@ +// 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/case_statement.h" + +#include "gtest/gtest.h" +#include "src/ast/bool_literal.h" +#include "src/ast/if_statement.h" +#include "src/ast/nop_statement.h" + +namespace tint { +namespace ast { + +using CaseStatementTest = testing::Test; + +TEST_F(CaseStatementTest, Creation) { + auto b = std::make_unique(true); + std::vector> stmts; + stmts.push_back(std::make_unique()); + + auto bool_ptr = b.get(); + auto nop_ptr = stmts[0].get(); + + CaseStatement c(std::move(b), std::move(stmts)); + EXPECT_EQ(c.condition(), bool_ptr); + ASSERT_EQ(c.body().size(), 1); + EXPECT_EQ(c.body()[0].get(), nop_ptr); +} + +TEST_F(CaseStatementTest, Creation_WithSource) { + auto b = std::make_unique(true); + std::vector> stmts; + stmts.push_back(std::make_unique()); + + CaseStatement c(Source{20, 2}, std::move(b), std::move(stmts)); + auto src = c.source(); + EXPECT_EQ(src.line, 20); + EXPECT_EQ(src.column, 2); +} + +TEST_F(CaseStatementTest, IsDefault_WithoutCondition) { + std::vector> stmts; + stmts.push_back(std::make_unique()); + + CaseStatement c; + c.set_body(std::move(stmts)); + EXPECT_TRUE(c.IsDefault()); +} + +TEST_F(CaseStatementTest, IsDefault_WithCondition) { + auto b = std::make_unique(true); + CaseStatement c; + c.set_condition(std::move(b)); + EXPECT_FALSE(c.IsDefault()); +} + +TEST_F(CaseStatementTest, IsCase) { + CaseStatement c; + EXPECT_TRUE(c.IsCase()); +} + +TEST_F(CaseStatementTest, IsValid) { + CaseStatement c; + EXPECT_TRUE(c.IsValid()); +} + +TEST_F(CaseStatementTest, IsValid_NullBodyStatement) { + auto b = std::make_unique(true); + std::vector> stmts; + stmts.push_back(std::make_unique()); + stmts.push_back(nullptr); + + CaseStatement c(std::move(b), std::move(stmts)); + EXPECT_FALSE(c.IsValid()); +} + +TEST_F(CaseStatementTest, IsValid_InvalidBodyStatement) { + auto b = std::make_unique(true); + std::vector> stmts; + stmts.push_back(std::make_unique()); + + CaseStatement c(std::move(b), std::move(stmts)); + EXPECT_FALSE(c.IsValid()); +} + +TEST_F(CaseStatementTest, ToStr_WithCondition) { + auto b = std::make_unique(true); + std::vector> stmts; + stmts.push_back(std::make_unique()); + CaseStatement c(std::move(b), std::move(stmts)); + + std::ostringstream out; + c.to_str(out, 2); + EXPECT_EQ(out.str(), R"( Case true{ + Nop{} + } +)"); +} + +TEST_F(CaseStatementTest, ToStr_WithoutCondition) { + std::vector> stmts; + stmts.push_back(std::make_unique()); + CaseStatement c(nullptr, std::move(stmts)); + + std::ostringstream out; + c.to_str(out, 2); + EXPECT_EQ(out.str(), R"( Default{ + Nop{} + } +)"); +} + +} // namespace ast +} // namespace tint diff --git a/src/ast/if_statement.h b/src/ast/if_statement.h index f775f6e0b6..f628b1ca90 100644 --- a/src/ast/if_statement.h +++ b/src/ast/if_statement.h @@ -19,6 +19,7 @@ #include #include +#include "src/ast/else_statement.h" #include "src/ast/expression.h" #include "src/ast/statement.h"