Add MemberAccessorExpression tests

This CL adds unit tests for the MemberArrayExpression.

Bug: tint:11
Change-Id: I790d27018b516b3b2b79b6ed892c1a0e00b65818
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/16681
Reviewed-by: Sarah Mashayekhi <sarahmashay@google.com>
This commit is contained in:
Dan Sinclair 2020-03-16 14:17:28 +00:00 committed by Sarah Mashayekhi
parent c99fdb84b5
commit f300756eff
6 changed files with 124 additions and 4 deletions

View File

@ -222,6 +222,7 @@ set(TINT_TEST_SRCS
ast/kill_statement_test.cc
ast/location_decoration_test.cc
ast/loop_statement_test.cc
ast/member_accessor_expression_test.cc
ast/module_test.cc
ast/set_decoration_test.cc
ast/struct_member_test.cc

View File

@ -17,8 +17,8 @@
#include <sstream>
#include "gtest/gtest.h"
#include "src/ast/kill_statement.h"
#include "src/ast/if_statement.h"
#include "src/ast/kill_statement.h"
#include "src/ast/nop_statement.h"
namespace tint {

View File

@ -17,6 +17,8 @@
namespace tint {
namespace ast {
MemberAccessorExpression::MemberAccessorExpression() = default;
MemberAccessorExpression::MemberAccessorExpression(
std::unique_ptr<Expression> structure,
std::unique_ptr<IdentifierExpression> member)
@ -33,7 +35,13 @@ MemberAccessorExpression::MemberAccessorExpression(
MemberAccessorExpression::~MemberAccessorExpression() = default;
bool MemberAccessorExpression::IsValid() const {
return struct_ != nullptr && member_ != nullptr;
if (struct_ == nullptr || !struct_->IsValid()) {
return false;
}
if (member_ == nullptr || !member_->IsValid()) {
return false;
}
return true;
}
void MemberAccessorExpression::to_str(std::ostream& out, size_t indent) const {

View File

@ -29,6 +29,8 @@ namespace ast {
/// A member accessor expression
class MemberAccessorExpression : public Expression {
public:
/// Constructor
MemberAccessorExpression();
/// Constructor
/// @param structure the structure
/// @param member the member

View File

@ -0,0 +1,109 @@
// 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/member_accessor_expression.h"
#include <sstream>
#include "gtest/gtest.h"
#include "src/ast/identifier_expression.h"
namespace tint {
namespace ast {
using MemberAccessorExpressionTest = testing::Test;
TEST_F(MemberAccessorExpressionTest, Creation) {
auto str = std::make_unique<IdentifierExpression>("structure");
auto mem = std::make_unique<IdentifierExpression>("member");
auto str_ptr = str.get();
auto mem_ptr = mem.get();
MemberAccessorExpression stmt(std::move(str), std::move(mem));
EXPECT_EQ(stmt.structure(), str_ptr);
EXPECT_EQ(stmt.member(), mem_ptr);
}
TEST_F(MemberAccessorExpressionTest, Creation_WithSource) {
auto str = std::make_unique<IdentifierExpression>("structure");
auto mem = std::make_unique<IdentifierExpression>("member");
MemberAccessorExpression stmt(Source{20, 2}, std::move(str), std::move(mem));
auto src = stmt.source();
EXPECT_EQ(src.line, 20);
EXPECT_EQ(src.column, 2);
}
TEST_F(MemberAccessorExpressionTest, IsMemberAccessor) {
MemberAccessorExpression stmt;
EXPECT_TRUE(stmt.IsMemberAccessor());
}
TEST_F(MemberAccessorExpressionTest, IsValid) {
auto str = std::make_unique<IdentifierExpression>("structure");
auto mem = std::make_unique<IdentifierExpression>("member");
MemberAccessorExpression stmt(std::move(str), std::move(mem));
EXPECT_TRUE(stmt.IsValid());
}
TEST_F(MemberAccessorExpressionTest, IsValid_NullStruct) {
auto mem = std::make_unique<IdentifierExpression>("member");
MemberAccessorExpression stmt;
stmt.set_member(std::move(mem));
EXPECT_FALSE(stmt.IsValid());
}
TEST_F(MemberAccessorExpressionTest, IsValid_InvalidStruct) {
auto str = std::make_unique<IdentifierExpression>("");
auto mem = std::make_unique<IdentifierExpression>("member");
MemberAccessorExpression stmt(std::move(str), std::move(mem));
EXPECT_FALSE(stmt.IsValid());
}
TEST_F(MemberAccessorExpressionTest, IsValid_NullMember) {
auto str = std::make_unique<IdentifierExpression>("structure");
MemberAccessorExpression stmt;
stmt.set_structure(std::move(str));
EXPECT_FALSE(stmt.IsValid());
}
TEST_F(MemberAccessorExpressionTest, IsValid_InvalidMember) {
auto str = std::make_unique<IdentifierExpression>("structure");
auto mem = std::make_unique<IdentifierExpression>("");
MemberAccessorExpression stmt(std::move(str), std::move(mem));
EXPECT_FALSE(stmt.IsValid());
}
TEST_F(MemberAccessorExpressionTest, ToStr) {
auto str = std::make_unique<IdentifierExpression>("structure");
auto mem = std::make_unique<IdentifierExpression>("member");
MemberAccessorExpression stmt(std::move(str), std::move(mem));
std::ostringstream out;
stmt.to_str(out, 2);
EXPECT_EQ(out.str(), R"( MemberAccessor{
Identifier{structure}
Identifier{member}
}
)");
}
} // namespace ast
} // namespace tint