From 06bb8d4d6dcfd1a6111794467676500d955cb144 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Mon, 10 Dec 2018 22:46:47 -0500 Subject: Googletest export The gmock matchers have a concept of MatchAndExpain; where the details of the matching are written to a "result listener". A matcher can avoid creating expensive debug info by checking result_listener->IsInterested(); but, unfortunately, the default matcher code (called from EXPECT_THAT) is always "interested". This change implements EXPECT_THAT matching to first run the matcher in a "not interested" mode; and then run it a second time ("interested") only if the match fails. PiperOrigin-RevId: 224929783 --- googlemock/include/gmock/gmock-matchers.h | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'googlemock/include') diff --git a/googlemock/include/gmock/gmock-matchers.h b/googlemock/include/gmock/gmock-matchers.h index b859f1aa..68278bea 100644 --- a/googlemock/include/gmock/gmock-matchers.h +++ b/googlemock/include/gmock/gmock-matchers.h @@ -1296,14 +1296,24 @@ class PredicateFormatterFromMatcher { // We don't write MatcherCast either, as that allows // potentially unsafe downcasting of the matcher argument. const Matcher matcher = SafeMatcherCast(matcher_); - StringMatchResultListener listener; - if (MatchPrintAndExplain(x, matcher, &listener)) + + // 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)) { return AssertionSuccess(); + } ::std::stringstream ss; ss << "Value of: " << value_text << "\n" << "Expected: "; matcher.DescribeTo(&ss); + + // Rerun the matcher to "PrintAndExain" 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: " << listener.str(); return AssertionFailure() << ss.str(); } -- cgit v1.2.3