Add Variable test for invalid initializer.

This CL adds a test to the variable AST node to verify the initializer
is valid.

Bug: tint:11
Change-Id: I95553e8572124e8e2e861b451003e5c9bf6358f1
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/16743
Reviewed-by: Sarah Mashayekhi <sarahmashay@google.com>
This commit is contained in:
Dan Sinclair 2020-03-17 14:59:06 +00:00 committed by Sarah Mashayekhi
parent 02d94b2903
commit 1e31f161db
2 changed files with 18 additions and 0 deletions

View File

@ -44,6 +44,9 @@ bool Variable::IsValid() const {
if (type_ == nullptr) {
return false;
}
if (initializer_ && !initializer_->IsValid()) {
return false;
}
return true;
}

View File

@ -15,6 +15,7 @@
#include "src/ast/variable.h"
#include "gtest/gtest.h"
#include "src/ast/identifier_expression.h"
#include "src/ast/type/f32_type.h"
#include "src/ast/type/i32_type.h"
@ -69,6 +70,13 @@ TEST_F(VariableTest, IsValid) {
EXPECT_TRUE(v.IsValid());
}
TEST_F(VariableTest, IsValid_WithInitializer) {
type::I32Type t;
Variable v{"my_var", StorageClass::kNone, &t};
v.set_initializer(std::make_unique<IdentifierExpression>("ident"));
EXPECT_TRUE(v.IsValid());
}
TEST_F(VariableTest, IsValid_MissinName) {
type::I32Type t;
Variable v{"", StorageClass::kNone, &t};
@ -85,6 +93,13 @@ TEST_F(VariableTest, IsValid_MissingBoth) {
EXPECT_FALSE(v.IsValid());
}
TEST_F(VariableTest, IsValid_InvalidInitializer) {
type::I32Type t;
Variable v{"my_var", StorageClass::kNone, &t};
v.set_initializer(std::make_unique<IdentifierExpression>(""));
EXPECT_FALSE(v.IsValid());
}
TEST_F(VariableTest, to_str) {
type::F32Type t;
Variable v{"my_var", StorageClass::kFunction, &t};