Implement testing::Rethrow to throw exceptions more easily via std::exception_ptr

We avoid overloading or specializing `testing::Throw` as this is fundamentally a different operation than throwing the object.
However, we disable the corresponding overload of `testing::Throw` to prevent likely mistakes in the usage.

Fixes: #4412
PiperOrigin-RevId: 585745469
Change-Id: I03bb585427ce51983d914e88f2bf65a13545c920
This commit is contained in:
Abseil Team
2023-11-27 13:31:59 -08:00
committed by Copybara-Service
parent b10fad38c4
commit 76bb2afb8b
2 changed files with 30 additions and 3 deletions

View File

@@ -187,6 +187,7 @@ using testing::SetErrnoAndReturn;
#if GTEST_HAS_EXCEPTIONS
using testing::Throw;
using testing::Rethrow;
#endif
using testing::ContainsRegex;
@@ -416,6 +417,14 @@ TEST(LinkTest, TestThrow) {
EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Throw(42));
EXPECT_THROW(mock.VoidFromString(nullptr), int);
}
// Tests the linkage of the Rethrow action.
TEST(LinkTest, TestRethrow) {
Mock mock;
EXPECT_CALL(mock, VoidFromString(_))
.WillOnce(Rethrow(std::make_exception_ptr(42)));
EXPECT_THROW(mock.VoidFromString(nullptr), int);
}
#endif // GTEST_HAS_EXCEPTIONS
// The ACTION*() macros trigger warning C4100 (unreferenced formal