Add identifier expression tests
This CL adds tests for the identifier expression AST node.
Bug: tint:11
Change-Id: Ie7dd4f1de3d8a23ac7e878285564d8c5fc9e1250
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/16502
Reviewed-by: Sarah Mashayekhi <sarahmashay@google.com>
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 6713085..70cf7b4 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -215,6 +215,7 @@
ast/entry_point_test.cc
ast/fallthrough_statement_test.cc
ast/function_test.cc
+ ast/identifier_expression_test.cc
ast/import_test.cc
ast/int_literal_test.cc
ast/location_decoration_test.cc
diff --git a/src/ast/identifier_expression.cc b/src/ast/identifier_expression.cc
index 986920a..b4d9eb7 100644
--- a/src/ast/identifier_expression.cc
+++ b/src/ast/identifier_expression.cc
@@ -34,7 +34,14 @@
IdentifierExpression::~IdentifierExpression() = default;
bool IdentifierExpression::IsValid() const {
- return name_.size() > 0 && name_[0].size() > 0;
+ if (name_.size() == 0)
+ return false;
+
+ for (const auto& name : name_) {
+ if (name.size() == 0)
+ return false;
+ }
+ return true;
}
void IdentifierExpression::to_str(std::ostream& out, size_t indent) const {
diff --git a/src/ast/identifier_expression_test.cc b/src/ast/identifier_expression_test.cc
new file mode 100644
index 0000000..ddbefd2
--- /dev/null
+++ b/src/ast/identifier_expression_test.cc
@@ -0,0 +1,84 @@
+// 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/identifier_expression.h"
+
+#include "gtest/gtest.h"
+
+namespace tint {
+namespace ast {
+
+using IdentifierExpressionTest = testing::Test;
+
+TEST_F(IdentifierExpressionTest, Creation) {
+ IdentifierExpression i("ident");
+ ASSERT_EQ(i.name().size(), 1);
+ EXPECT_EQ(i.name()[0], "ident");
+}
+
+TEST_F(IdentifierExpressionTest, Creation_WithSource) {
+ IdentifierExpression i(Source{20, 2}, {"ns1", "ns2", "ident"});
+ ASSERT_EQ(i.name().size(), 3);
+ EXPECT_EQ(i.name()[0], "ns1");
+ EXPECT_EQ(i.name()[1], "ns2");
+ EXPECT_EQ(i.name()[2], "ident");
+
+ auto src = i.source();
+ EXPECT_EQ(src.line, 20);
+ EXPECT_EQ(src.column, 2);
+}
+
+TEST_F(IdentifierExpressionTest, IsIdentifier) {
+ IdentifierExpression i("ident");
+ EXPECT_TRUE(i.IsIdentifier());
+}
+
+TEST_F(IdentifierExpressionTest, IsValid) {
+ IdentifierExpression i("ident");
+ EXPECT_TRUE(i.IsValid());
+}
+
+TEST_F(IdentifierExpressionTest, IsValid_WithNamespaces) {
+ IdentifierExpression i({"ns1", "n2", "ident"});
+ EXPECT_TRUE(i.IsValid());
+}
+
+TEST_F(IdentifierExpressionTest, IsValid_BlankName) {
+ IdentifierExpression i("");
+ EXPECT_FALSE(i.IsValid());
+}
+
+TEST_F(IdentifierExpressionTest, IsValid_BlankNamespace) {
+ IdentifierExpression i({"ns1", "", "ident"});
+ EXPECT_FALSE(i.IsValid());
+}
+
+TEST_F(IdentifierExpressionTest, ToStr) {
+ IdentifierExpression i("ident");
+ std::ostringstream out;
+ i.to_str(out, 2);
+ EXPECT_EQ(out.str(), R"( Identifier{ident}
+)");
+}
+
+TEST_F(IdentifierExpressionTest, ToStr_WithNamespace) {
+ IdentifierExpression i({"ns1", "ns2", "ident"});
+ std::ostringstream out;
+ i.to_str(out, 2);
+ EXPECT_EQ(out.str(), R"( Identifier{ns1::ns2::ident}
+)");
+}
+
+} // namespace ast
+} // namespace tint