From 86216f5180543721e72806d115944d55ad141d81 Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Wed, 22 Mar 2023 16:47:44 +0000 Subject: [PATCH] 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 Kokoro: Kokoro Reviewed-by: Ben Clayton --- src/tint/diagnostic/diagnostic.cc | 5 ++++ src/tint/diagnostic/diagnostic.h | 11 +++++++++ src/tint/resolver/resolver.h | 3 +++ src/tint/writer/append_vector_test.cc | 35 ++++++++++++++++++--------- 4 files changed, 42 insertions(+), 12 deletions(-) diff --git a/src/tint/diagnostic/diagnostic.cc b/src/tint/diagnostic/diagnostic.cc index a87e43b7a3..eb9a14fccd 100644 --- a/src/tint/diagnostic/diagnostic.cc +++ b/src/tint/diagnostic/diagnostic.cc @@ -43,4 +43,9 @@ std::string List::str() const { 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 bebdd7cd07..b56bd8d2e2 100644 --- a/src/tint/diagnostic/diagnostic.h +++ b/src/tint/diagnostic/diagnostic.h @@ -16,6 +16,7 @@ #define SRC_TINT_DIAGNOSTIC_DIAGNOSTIC_H_ #include +#include #include #include #include @@ -231,6 +232,10 @@ class List { 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 @@ class List { 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 266f3a2dd9..edc088a676 100644 --- a/src/tint/resolver/resolver.h +++ b/src/tint/resolver/resolver.h @@ -88,6 +88,9 @@ class Resolver { /// @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 0303c3c08c..d2a77df396 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 @@ TEST_F(AppendVectorTest, Vec2i32_i32) { 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 @@ TEST_F(AppendVectorTest, Vec2i32_u32) { 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 @@ TEST_F(AppendVectorTest, Vec2i32FromVec2u32_u32) { 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 @@ TEST_F(AppendVectorTest, Vec2i32_f32) { 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 @@ TEST_F(AppendVectorTest, Vec3i32_i32) { 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 @@ TEST_F(AppendVectorTest, Vec2i32Var_i32) { 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 @@ TEST_F(AppendVectorTest, Vec2i32_i32Var) { 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 @@ TEST_F(AppendVectorTest, Vec2i32Var_i32Var) { 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 @@ TEST_F(AppendVectorTest, Vec2i32Var_f32Var) { 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 @@ TEST_F(AppendVectorTest, Vec2boolVar_boolVar) { 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 @@ TEST_F(AppendVectorTest, ZeroVec3i32_i32) { 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);