Rework the IsSuccess matcher to be fully polymorphic (#5981)

Previously, this matcher mostly worked, but the `DescribeTo` functions
wouldn't compile when another polymorphic matcher was nested to match
the value.

The updated code uses the same polymorphic matcher design as used by
`Not` and others in Google Test itself.

I've added a test that uses `VariantWith` to nest matchers more deeply
with `IsSuccess`. This test doesn't compile prior to this change.
This commit is contained in:
Chandler Carruth
2025-08-25 16:28:11 +00:00
committed by GitHub
parent d49cb3ecfb
commit 74016d47f9
2 changed files with 53 additions and 16 deletions
+12
View File
@@ -19,6 +19,7 @@ using ::Carbon::Testing::IsError;
using ::Carbon::Testing::IsSuccess;
using ::testing::_;
using ::testing::Eq;
using ::testing::VariantWith;
TEST(ErrorTest, Error) {
Error err("test");
@@ -158,6 +159,17 @@ TYPED_TEST(ErrorOrTest, UnprintableValue) {
EXPECT_THAT(error, IsError(this->ErrorStr()));
}
// Note that this is more of a test of `IsSuccess` than `ErrorOr` itself.
TYPED_TEST(ErrorOrTest, NestedMatching) {
using TestErrorOr = ErrorOr<std::variant<int, float>, TypeParam>;
TestErrorOr i(42);
EXPECT_THAT(i, IsSuccess(VariantWith<int>(Eq(42))));
TestErrorOr f(0.42F);
EXPECT_THAT(f, IsSuccess(VariantWith<float>(Eq(0.42F))));
}
TYPED_TEST(ErrorOrTest, ReturnIfErrorNoError) {
using TestErrorOr = ErrorOr<Success, TypeParam>;
auto result = []() -> TestErrorOr {