Merge f626ec47020aec8baa5d2a6a8546d53a6b3db8af into 3ff51c3e80f2c2eb105d43ecb9acab9a62e01600

This commit is contained in:
Manikandan K S 2026-07-28 16:28:53 -04:00 committed by GitHub
commit 2988866b9b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 31 additions and 27 deletions

View File

@ -1719,9 +1719,13 @@ class [[nodiscard]] PredicateFormatterFromMatcher {
// potentially unsafe downcasting of the matcher argument.
const Matcher<const T&> matcher = SafeMatcherCast<const T&>(matcher_);
// The expected path here is that the matcher should match (i.e. that most
// tests pass) so optimize for this case.
if (matcher.Matches(x)) {
// Capture the explanation during the initial match attempt so that
// side-effecting matchers (like Throws) are not invoked a second time
// when formatting the failure message.
StringMatchResultListener listener;
const bool matches = matcher.MatchAndExplain(x, &listener);
if (matches) {
return AssertionSuccess();
}
@ -1729,14 +1733,15 @@ class [[nodiscard]] PredicateFormatterFromMatcher {
ss << "Value of: " << value_text << "\n"
<< "Expected: ";
matcher.DescribeTo(&ss);
// Rerun the matcher to "PrintAndExplain" the failure.
StringMatchResultListener listener;
if (MatchPrintAndExplain(x, matcher, &listener)) {
ss << "\n The matcher failed on the initial attempt; but passed when "
"rerun to generate the explanation.";
ss << "\n Actual: ";
UniversalPrint(x, &ss);
#if GTEST_HAS_RTTI
{
const std::string& type_name = GetTypeName<T>();
if (IsReadableTypeName(type_name)) ss << " (of type " << type_name << ")";
}
ss << "\n Actual: " << listener.str();
#endif
PrintIfNotEmpty(listener.str(), &ss);
return AssertionFailure() << ss.str();
}

View File

@ -2805,9 +2805,6 @@ class PredicateFormatterFromMatcherTest : public ::testing::Test {
protected:
enum Behavior { kInitialSuccess, kAlwaysFail, kFlaky };
// A matcher that can return different results when used multiple times on the
// same input. No real matcher should do this; but this lets us test that we
// detect such behavior and fail appropriately.
class MockMatcher : public MatcherInterface<Behavior> {
public:
bool MatchAndExplain(Behavior behavior,
@ -2815,20 +2812,11 @@ class PredicateFormatterFromMatcherTest : public ::testing::Test {
*listener << "[MatchAndExplain]";
switch (behavior) {
case kInitialSuccess:
// The first call to MatchAndExplain should use a "not interested"
// listener; so this is expected to return |true|. There should be no
// subsequent calls.
return !listener->IsInterested();
return true;
case kAlwaysFail:
return false;
case kFlaky:
// The first call to MatchAndExplain should use a "not interested"
// listener; so this will return |false|. Subsequent calls should have
// an "interested" listener; so this will return |true|, thus
// simulating a flaky matcher.
return listener->IsInterested();
return false;
}
GTEST_LOG_(FATAL) << "This should never be reached";
@ -2867,13 +2855,11 @@ TEST_F(PredicateFormatterFromMatcherTest, NoShortCircuitOnFailure) {
EXPECT_EQ(expect, result.message());
}
TEST_F(PredicateFormatterFromMatcherTest, DetectsFlakyShortCircuit) {
TEST_F(PredicateFormatterFromMatcherTest, AlwaysFails) {
AssertionResult result = RunPredicateFormatter(kFlaky);
EXPECT_FALSE(result); // Implicit cast to bool.
std::string expect =
"Value of: dummy-name\nExpected: [DescribeTo]\n"
" The matcher failed on the initial attempt; but passed when rerun to "
"generate the explanation.\n"
" Actual: 2" +
OfType(internal::GetTypeName<Behavior>()) + ", [MatchAndExplain]";
EXPECT_EQ(expect, result.message());

View File

@ -1754,6 +1754,19 @@ TEST(ThrowsTest, CallableExecutedExactlyOnce) {
EXPECT_EQ(a, 4u);
}
TEST(ThrowsTest, EvaluatesCallableOnlyOnceOnFailure) {
int call_count = 0;
auto stateful_lambda = [&call_count]() {
++call_count;
};
EXPECT_NONFATAL_FAILURE(
EXPECT_THAT(stateful_lambda, Throws<std::runtime_error>()),
"does not throw any exception");
EXPECT_EQ(call_count, 1);
}
TEST(ThrowsTest, Describe) {
Matcher<std::function<void()>> matcher = Throws<std::runtime_error>();
std::stringstream ss;