add ast::Module::to_str
This makes it easier to see the bug.
Bug: tint:22
Change-Id: Ic5acc0b8299ef31eb73b49863bc42ac09de6e9bf
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/17203
Reviewed-by: dan sinclair <dsinclair@google.com>
diff --git a/src/ast/module.cc b/src/ast/module.cc
index dfca88a..8b69af0 100644
--- a/src/ast/module.cc
+++ b/src/ast/module.cc
@@ -59,21 +59,24 @@
std::string Module::to_str() const {
std::ostringstream out;
+ out << "Module{" << std::endl;
+ const auto indent = 2;
for (const auto& import : imports_) {
- import->to_str(out, 0);
+ import->to_str(out, indent);
}
for (const auto& var : global_variables_) {
- var->to_str(out, 0);
+ var->to_str(out, indent);
}
for (const auto& ep : entry_points_) {
- ep->to_str(out, 0);
+ ep->to_str(out, indent);
}
for (const auto& alias : alias_types_) {
out << alias->name() << " -> " << alias->type()->type_name() << std::endl;
}
for (const auto& func : functions_) {
- func->to_str(out, 0);
+ func->to_str(out, indent);
}
+ out << "}" << std::endl;
return out.str();
}
diff --git a/src/ast/module_test.cc b/src/ast/module_test.cc
index 64fd31b..7fad54b 100644
--- a/src/ast/module_test.cc
+++ b/src/ast/module_test.cc
@@ -14,9 +14,10 @@
#include "src/ast/module.h"
+#include <sstream>
#include <utility>
-#include "gtest/gtest.h"
+#include "gmock/gmock.h"
#include "src/ast/entry_point.h"
#include "src/ast/function.h"
#include "src/ast/import.h"
@@ -34,6 +35,13 @@
EXPECT_EQ(m.imports().size(), 0);
}
+TEST_F(ModuleTest, ToStrEmitsPreambleAndPostamble) {
+ Module m;
+ const auto str = m.to_str();
+ const auto expected = "Module{\n}\n";
+ EXPECT_EQ(str, expected);
+}
+
TEST_F(ModuleTest, Imports) {
Module m;
@@ -44,6 +52,16 @@
EXPECT_EQ("std::glsl", m.imports()[0]->name());
}
+TEST_F(ModuleTest, ToStrWithImport) {
+ Module m;
+ m.AddImport(std::make_unique<Import>("GLSL.std.430", "std::glsl"));
+ const auto str = m.to_str();
+ EXPECT_EQ(str, R"(Module{
+ Import{"GLSL.std.430" as std::glsl}
+}
+)");
+}
+
TEST_F(ModuleTest, LookupImport) {
Module m;