Add ability to use `{EXPECT|ASSERT}_THAT` with Diagnostics.
This CL adds the ability to use the `EXPECT_THAT` and `ASSERT_THAT`
macros with a `Diagnostics::List` object. The diagnostic string will be
emitted as part of the error message.
The `Resolver` is updated to provide access to the `Diagnostics` and the
`AppendVector` tests are updated to use the resolvers diagnostics
instead of the `error()` method.
Change-Id: I94682311fe6f135b7fceae9ec7a5108379fd32be
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/125060
Commit-Queue: Dan Sinclair <dsinclair@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
diff --git a/src/tint/diagnostic/diagnostic.cc b/src/tint/diagnostic/diagnostic.cc
index a87e43b..eb9a14f 100644
--- a/src/tint/diagnostic/diagnostic.cc
+++ b/src/tint/diagnostic/diagnostic.cc
@@ -43,4 +43,9 @@
return Formatter{style}.format(*this);
}
+std::ostream& operator<<(std::ostream& out, const List& list) {
+ out << list.str();
+ return out;
+}
+
} // namespace tint::diag
diff --git a/src/tint/diagnostic/diagnostic.h b/src/tint/diagnostic/diagnostic.h
index bebdd7c..b56bd8d 100644
--- a/src/tint/diagnostic/diagnostic.h
+++ b/src/tint/diagnostic/diagnostic.h
@@ -16,6 +16,7 @@
#define SRC_TINT_DIAGNOSTIC_DIAGNOSTIC_H_
#include <memory>
+#include <ostream>
#include <string>
#include <utility>
#include <vector>
@@ -231,6 +232,10 @@
size_t error_count() const { return error_count_; }
/// @returns the number of entries in the list.
size_t count() const { return entries_.size(); }
+ /// @returns true if the diagnostics list is empty
+ bool empty() const { return entries_.empty(); }
+ /// @returns the number of entrise in the diagnostics list
+ size_t size() const { return entries_.size(); }
/// @returns the first diagnostic in the list.
iterator begin() const { return entries_.begin(); }
/// @returns the last diagnostic in the list.
@@ -244,6 +249,12 @@
size_t error_count_ = 0;
};
+/// Write the diagnostic list to the given stream
+/// @param out the output stream
+/// @param list the list to emit
+/// @returns the output stream
+std::ostream& operator<<(std::ostream& out, const List& list);
+
} // namespace tint::diag
#endif // SRC_TINT_DIAGNOSTIC_DIAGNOSTIC_H_
diff --git a/src/tint/resolver/resolver.h b/src/tint/resolver/resolver.h
index 266f3a2..edc088a 100644
--- a/src/tint/resolver/resolver.h
+++ b/src/tint/resolver/resolver.h
@@ -88,6 +88,9 @@
/// @returns error messages from the resolver
std::string error() const { return diagnostics_.str(); }
+ /// @returns the list of diagnostics raised by the generator.
+ const diag::List& Diagnostics() const { return diagnostics_; }
+
/// @returns true if the resolver was successful
bool Resolve();
diff --git a/src/tint/writer/append_vector_test.cc b/src/tint/writer/append_vector_test.cc
index 0303c3c..d2a77df 100644
--- a/src/tint/writer/append_vector_test.cc
+++ b/src/tint/writer/append_vector_test.cc
@@ -18,7 +18,7 @@
#include "src/tint/resolver/resolver.h"
#include "src/tint/sem/value_constructor.h"
-#include "gtest/gtest.h"
+#include "gmock/gmock.h"
using namespace tint::number_suffixes; // NOLINT
@@ -36,7 +36,8 @@
WrapInFunction(vec_12, scalar_3);
resolver::Resolver resolver(this);
- ASSERT_TRUE(resolver.Resolve()) << resolver.error();
+ EXPECT_TRUE(resolver.Resolve());
+ ASSERT_THAT(resolver.Diagnostics(), testing::IsEmpty());
auto* append = AppendVector(this, vec_12, scalar_3);
@@ -76,7 +77,8 @@
WrapInFunction(vec_12, scalar_3);
resolver::Resolver resolver(this);
- ASSERT_TRUE(resolver.Resolve()) << resolver.error();
+ EXPECT_TRUE(resolver.Resolve());
+ ASSERT_THAT(resolver.Diagnostics(), testing::IsEmpty());
auto* append = AppendVector(this, vec_12, scalar_3);
@@ -123,7 +125,8 @@
WrapInFunction(vec_12, scalar_3);
resolver::Resolver resolver(this);
- ASSERT_TRUE(resolver.Resolve()) << resolver.error();
+ EXPECT_TRUE(resolver.Resolve());
+ ASSERT_THAT(resolver.Diagnostics(), testing::IsEmpty());
auto* append = AppendVector(this, vec_12, scalar_3);
@@ -171,7 +174,8 @@
WrapInFunction(vec_12, scalar_3);
resolver::Resolver resolver(this);
- ASSERT_TRUE(resolver.Resolve()) << resolver.error();
+ EXPECT_TRUE(resolver.Resolve());
+ ASSERT_THAT(resolver.Diagnostics(), testing::IsEmpty());
auto* append = AppendVector(this, vec_12, scalar_3);
@@ -216,7 +220,8 @@
WrapInFunction(vec_123, scalar_4);
resolver::Resolver resolver(this);
- ASSERT_TRUE(resolver.Resolve()) << resolver.error();
+ EXPECT_TRUE(resolver.Resolve());
+ ASSERT_THAT(resolver.Diagnostics(), testing::IsEmpty());
auto* append = AppendVector(this, vec_123, scalar_4);
@@ -258,7 +263,8 @@
WrapInFunction(vec_12, scalar_3);
resolver::Resolver resolver(this);
- ASSERT_TRUE(resolver.Resolve()) << resolver.error();
+ EXPECT_TRUE(resolver.Resolve());
+ ASSERT_THAT(resolver.Diagnostics(), testing::IsEmpty());
auto* append = AppendVector(this, vec_12, scalar_3);
@@ -296,7 +302,8 @@
WrapInFunction(vec_12, scalar_3);
resolver::Resolver resolver(this);
- ASSERT_TRUE(resolver.Resolve()) << resolver.error();
+ EXPECT_TRUE(resolver.Resolve());
+ ASSERT_THAT(resolver.Diagnostics(), testing::IsEmpty());
auto* append = AppendVector(this, vec_12, scalar_3);
@@ -336,7 +343,8 @@
WrapInFunction(vec_12, scalar_3);
resolver::Resolver resolver(this);
- ASSERT_TRUE(resolver.Resolve()) << resolver.error();
+ EXPECT_TRUE(resolver.Resolve());
+ ASSERT_THAT(resolver.Diagnostics(), testing::IsEmpty());
auto* append = AppendVector(this, vec_12, scalar_3);
@@ -373,7 +381,8 @@
WrapInFunction(vec_12, scalar_3);
resolver::Resolver resolver(this);
- ASSERT_TRUE(resolver.Resolve()) << resolver.error();
+ EXPECT_TRUE(resolver.Resolve());
+ ASSERT_THAT(resolver.Diagnostics(), testing::IsEmpty());
auto* append = AppendVector(this, vec_12, scalar_3);
@@ -414,7 +423,8 @@
WrapInFunction(vec_12, scalar_3);
resolver::Resolver resolver(this);
- ASSERT_TRUE(resolver.Resolve()) << resolver.error();
+ EXPECT_TRUE(resolver.Resolve());
+ ASSERT_THAT(resolver.Diagnostics(), testing::IsEmpty());
auto* append = AppendVector(this, vec_12, scalar_3);
@@ -449,7 +459,8 @@
WrapInFunction(vec000, scalar);
resolver::Resolver resolver(this);
- ASSERT_TRUE(resolver.Resolve()) << resolver.error();
+ EXPECT_TRUE(resolver.Resolve());
+ ASSERT_THAT(resolver.Diagnostics(), testing::IsEmpty());
auto* append = AppendVector(this, vec000, scalar);