Add unit test for ArrayAccessorExpression

This CL adds unit tests for the array accessor expression class.

Change-Id: I97a7b366a2303b071040164cbcda404fe0592d90
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/16281
Reviewed-by: David Neto <dneto@google.com>
Reviewed-by: Dan Sinclair <dsinclair@google.com>
This commit is contained in:
Dan Sinclair 2020-03-03 14:09:27 +00:00
parent a00a571d81
commit e4b68bafda
4 changed files with 97 additions and 0 deletions

View File

@ -191,6 +191,7 @@ if(TINT_BUILD_SPV_PARSER)
endif() endif()
set(TINT_TEST_SRCS set(TINT_TEST_SRCS
ast/array_accessor_expression_test.cc
ast/binding_decoration_test.cc ast/binding_decoration_test.cc
ast/bool_literal_test.cc ast/bool_literal_test.cc
ast/builtin_decoration_test.cc ast/builtin_decoration_test.cc

View File

@ -17,6 +17,8 @@
namespace tint { namespace tint {
namespace ast { namespace ast {
ArrayAccessorExpression::ArrayAccessorExpression() : Expression() {}
ArrayAccessorExpression::ArrayAccessorExpression( ArrayAccessorExpression::ArrayAccessorExpression(
std::unique_ptr<Expression> array, std::unique_ptr<Expression> array,
std::unique_ptr<Expression> idx_expr) std::unique_ptr<Expression> idx_expr)

View File

@ -27,6 +27,8 @@ namespace ast {
/// An array accessor expression /// An array accessor expression
class ArrayAccessorExpression : public Expression { class ArrayAccessorExpression : public Expression {
public: public:
/// Constructor
ArrayAccessorExpression();
/// Constructor /// Constructor
/// @param array the array /// @param array the array
/// @param idx_expr the index expression /// @param idx_expr the index expression

View File

@ -0,0 +1,92 @@
// 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/array_accessor_expression.h"
#include "gtest/gtest.h"
#include "src/ast/identifier_expression.h"
namespace tint {
namespace ast {
using ArrayAccessorExpressionTest = testing::Test;
TEST_F(ArrayAccessorExpressionTest, Create) {
auto ary = std::make_unique<IdentifierExpression>("ary");
auto idx = std::make_unique<IdentifierExpression>("idx");
auto ary_ptr = ary.get();
auto idx_ptr = idx.get();
ArrayAccessorExpression exp(std::move(ary), std::move(idx));
ASSERT_EQ(exp.array(), ary_ptr);
ASSERT_EQ(exp.idx_expr(), idx_ptr);
}
TEST_F(ArrayAccessorExpressionTest, CreateWithSource) {
auto ary = std::make_unique<IdentifierExpression>("ary");
auto idx = std::make_unique<IdentifierExpression>("idx");
ArrayAccessorExpression exp(Source{20, 2}, std::move(ary), std::move(idx));
auto src = exp.source();
EXPECT_EQ(src.line, 20);
EXPECT_EQ(src.column, 2);
}
TEST_F(ArrayAccessorExpressionTest, IsArrayAccessor) {
ArrayAccessorExpression exp;
EXPECT_TRUE(exp.IsArrayAccessor());
}
TEST_F(ArrayAccessorExpressionTest, IsValid) {
auto ary = std::make_unique<IdentifierExpression>("ary");
auto idx = std::make_unique<IdentifierExpression>("idx");
ArrayAccessorExpression exp(std::move(ary), std::move(idx));
EXPECT_TRUE(exp.IsValid());
}
TEST_F(ArrayAccessorExpressionTest, IsValid_MissingArray) {
auto idx = std::make_unique<IdentifierExpression>("idx");
ArrayAccessorExpression exp;
exp.set_idx_expr(std::move(idx));
EXPECT_FALSE(exp.IsValid());
}
TEST_F(ArrayAccessorExpressionTest, IsValid_MissingIndex) {
auto ary = std::make_unique<IdentifierExpression>("ary");
ArrayAccessorExpression exp;
exp.set_array(std::move(ary));
EXPECT_FALSE(exp.IsValid());
}
TEST_F(ArrayAccessorExpressionTest, ToStr) {
auto ary = std::make_unique<IdentifierExpression>("ary");
auto idx = std::make_unique<IdentifierExpression>("idx");
ArrayAccessorExpression exp(std::move(ary), std::move(idx));
std::ostringstream out;
exp.to_str(out, 0);
EXPECT_EQ(out.str(), R"(ArrayAccessor{
Identifier{ary}
Identifier{idx}
}
)");
}
} // namespace ast
} // namespace tint