Commit 0c400f67 authored by Abseil Team's avatar Abseil Team Committed by Mark Barolak
Browse files

Googletest export

GMock: Make Truly explain when it fails

I just wrote a test that had a matcher of the form
  Optional(AllOf(
    SomeMatcher,
    SomeOtherMatcher,
    Truly(SomePredicate)))

The predicate failed, the other two matchers succeeded, and I got a hard-to-interpret message saying that the value in the optional "didn't match". Didn't match what?

This change improves situations like that slightly by having Truly explain to its result listener when it fails. When there are multiple Trulys in an AllOf, there will be some ambiguity, but it will at least provide more information than right now.

PiperOrigin-RevId: 341105141
parent d89b3630
...@@ -1453,7 +1453,7 @@ class TrulyMatcher { ...@@ -1453,7 +1453,7 @@ class TrulyMatcher {
// interested in the address of the argument. // interested in the address of the argument.
template <typename T> template <typename T>
bool MatchAndExplain(T& x, // NOLINT bool MatchAndExplain(T& x, // NOLINT
MatchResultListener* /* listener */) const { MatchResultListener* listener) const {
// Without the if-statement, MSVC sometimes warns about converting // Without the if-statement, MSVC sometimes warns about converting
// a value to bool (warning 4800). // a value to bool (warning 4800).
// //
...@@ -1462,6 +1462,7 @@ class TrulyMatcher { ...@@ -1462,6 +1462,7 @@ class TrulyMatcher {
// having no operator!(). // having no operator!().
if (predicate_(x)) if (predicate_(x))
return true; return true;
*listener << "didn't satisfy the given predicate";
return false; return false;
} }
......
...@@ -2984,6 +2984,13 @@ TEST(TrulyTest, WorksForByRefArguments) { ...@@ -2984,6 +2984,13 @@ TEST(TrulyTest, WorksForByRefArguments) {
EXPECT_FALSE(m.Matches(n)); EXPECT_FALSE(m.Matches(n));
} }
// Tests that Truly(predicate) provides a helpful reason when it fails.
TEST(TrulyTest, ExplainsFailures) {
StringMatchResultListener listener;
EXPECT_FALSE(ExplainMatchResult(Truly(IsPositive), -1, &listener));
EXPECT_EQ(listener.str(), "didn't satisfy the given predicate");
}
// Tests that Matches(m) is a predicate satisfied by whatever that // Tests that Matches(m) is a predicate satisfied by whatever that
// matches matcher m. // matches matcher m.
TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) { TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment