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 <sarahmashay@google.com>
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 0434df8..374e44a 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -201,6 +201,7 @@
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 a591224..98e3a47 100644
--- a/src/ast/case_statement.cc
+++ b/src/ast/case_statement.cc
@@ -33,6 +33,10 @@
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 @@
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 0000000..9395b52
--- /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<BoolLiteral>(true);
+ std::vector<std::unique_ptr<Statement>> stmts;
+ stmts.push_back(std::make_unique<NopStatement>());
+
+ 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<BoolLiteral>(true);
+ std::vector<std::unique_ptr<Statement>> stmts;
+ stmts.push_back(std::make_unique<NopStatement>());
+
+ 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<std::unique_ptr<Statement>> stmts;
+ stmts.push_back(std::make_unique<NopStatement>());
+
+ CaseStatement c;
+ c.set_body(std::move(stmts));
+ EXPECT_TRUE(c.IsDefault());
+}
+
+TEST_F(CaseStatementTest, IsDefault_WithCondition) {
+ auto b = std::make_unique<BoolLiteral>(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<BoolLiteral>(true);
+ std::vector<std::unique_ptr<Statement>> stmts;
+ stmts.push_back(std::make_unique<NopStatement>());
+ 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<BoolLiteral>(true);
+ std::vector<std::unique_ptr<Statement>> stmts;
+ stmts.push_back(std::make_unique<IfStatement>());
+
+ CaseStatement c(std::move(b), std::move(stmts));
+ EXPECT_FALSE(c.IsValid());
+}
+
+TEST_F(CaseStatementTest, ToStr_WithCondition) {
+ auto b = std::make_unique<BoolLiteral>(true);
+ std::vector<std::unique_ptr<Statement>> stmts;
+ stmts.push_back(std::make_unique<NopStatement>());
+ 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<std::unique_ptr<Statement>> stmts;
+ stmts.push_back(std::make_unique<NopStatement>());
+ 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 f775f6e..f628b1c 100644
--- a/src/ast/if_statement.h
+++ b/src/ast/if_statement.h
@@ -19,6 +19,7 @@
#include <utility>
#include <vector>
+#include "src/ast/else_statement.h"
#include "src/ast/expression.h"
#include "src/ast/statement.h"