diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3d71fc7210..30b6e04834 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -191,6 +191,7 @@ if(TINT_BUILD_SPV_PARSER) endif() set(TINT_TEST_SRCS + ast/array_accessor_expression_test.cc ast/binding_decoration_test.cc ast/bool_literal_test.cc ast/builtin_decoration_test.cc diff --git a/src/ast/array_accessor_expression.cc b/src/ast/array_accessor_expression.cc index a98b436a5c..239748462e 100644 --- a/src/ast/array_accessor_expression.cc +++ b/src/ast/array_accessor_expression.cc @@ -17,6 +17,8 @@ namespace tint { namespace ast { +ArrayAccessorExpression::ArrayAccessorExpression() : Expression() {} + ArrayAccessorExpression::ArrayAccessorExpression( std::unique_ptr array, std::unique_ptr idx_expr) diff --git a/src/ast/array_accessor_expression.h b/src/ast/array_accessor_expression.h index ddffdee458..59880855da 100644 --- a/src/ast/array_accessor_expression.h +++ b/src/ast/array_accessor_expression.h @@ -27,6 +27,8 @@ namespace ast { /// An array accessor expression class ArrayAccessorExpression : public Expression { public: + /// Constructor + ArrayAccessorExpression(); /// Constructor /// @param array the array /// @param idx_expr the index expression diff --git a/src/ast/array_accessor_expression_test.cc b/src/ast/array_accessor_expression_test.cc new file mode 100644 index 0000000000..9e293227b3 --- /dev/null +++ b/src/ast/array_accessor_expression_test.cc @@ -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("ary"); + auto idx = std::make_unique("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("ary"); + auto idx = std::make_unique("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("ary"); + auto idx = std::make_unique("idx"); + + ArrayAccessorExpression exp(std::move(ary), std::move(idx)); + EXPECT_TRUE(exp.IsValid()); +} + +TEST_F(ArrayAccessorExpressionTest, IsValid_MissingArray) { + auto idx = std::make_unique("idx"); + + ArrayAccessorExpression exp; + exp.set_idx_expr(std::move(idx)); + EXPECT_FALSE(exp.IsValid()); +} + +TEST_F(ArrayAccessorExpressionTest, IsValid_MissingIndex) { + auto ary = std::make_unique("ary"); + + ArrayAccessorExpression exp; + exp.set_array(std::move(ary)); + EXPECT_FALSE(exp.IsValid()); +} + +TEST_F(ArrayAccessorExpressionTest, ToStr) { + auto ary = std::make_unique("ary"); + auto idx = std::make_unique("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