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>
diff --git a/src/ast/variable.cc b/src/ast/variable.cc
index d9990b8..9b02998 100644
--- a/src/ast/variable.cc
+++ b/src/ast/variable.cc
@@ -44,6 +44,9 @@
if (type_ == nullptr) {
return false;
}
+ if (initializer_ && !initializer_->IsValid()) {
+ return false;
+ }
return true;
}
diff --git a/src/ast/variable_test.cc b/src/ast/variable_test.cc
index 8156d23..26dc515 100644
--- a/src/ast/variable_test.cc
+++ b/src/ast/variable_test.cc
@@ -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 @@
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 @@
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};