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>
This commit is contained in:
Dan Sinclair 2020-03-16 13:26:10 +00:00 committed by Sarah Mashayekhi
parent 9bb0dc543d
commit 3d1e69767a
3 changed files with 93 additions and 1 deletions

View File

@ -215,6 +215,7 @@ set(TINT_TEST_SRCS
ast/entry_point_test.cc ast/entry_point_test.cc
ast/fallthrough_statement_test.cc ast/fallthrough_statement_test.cc
ast/function_test.cc ast/function_test.cc
ast/identifier_expression_test.cc
ast/import_test.cc ast/import_test.cc
ast/int_literal_test.cc ast/int_literal_test.cc
ast/location_decoration_test.cc ast/location_decoration_test.cc

View File

@ -34,7 +34,14 @@ IdentifierExpression::IdentifierExpression(const Source& source,
IdentifierExpression::~IdentifierExpression() = default; IdentifierExpression::~IdentifierExpression() = default;
bool IdentifierExpression::IsValid() const { 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 { void IdentifierExpression::to_str(std::ostream& out, size_t indent) const {

View File

@ -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