From 3ffa237f0ed258bf92b28ab00dce3ecda84efffe Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Mon, 4 Apr 2022 12:55:29 -0700 Subject: [PATCH] Use ResultOf()'s result_description when explaining match result. PiperOrigin-RevId: 439389646 Change-Id: Ie34adcdd2b24378e85962efac53b7bb89ed93803 --- googlemock/include/gmock/gmock-matchers.h | 6 +++++- googlemock/test/gmock-matchers-containers_test.cc | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/googlemock/include/gmock/gmock-matchers.h b/googlemock/include/gmock/gmock-matchers.h index 958458648..9750d8415 100644 --- a/googlemock/include/gmock/gmock-matchers.h +++ b/googlemock/include/gmock/gmock-matchers.h @@ -2258,7 +2258,11 @@ class ResultOfMatcher { } bool MatchAndExplain(T obj, MatchResultListener* listener) const override { - *listener << "which is mapped by the given callable to "; + if (result_description_.empty()) { + *listener << "which is mapped by the given callable to "; + } else { + *listener << "whose " << result_description_ << " is "; + } // Cannot pass the return value directly to MatchPrintAndExplain, which // takes a non-const reference as argument. // Also, specifying template argument explicitly is needed because T could diff --git a/googlemock/test/gmock-matchers-containers_test.cc b/googlemock/test/gmock-matchers-containers_test.cc index add7a356b..74a226d3c 100644 --- a/googlemock/test/gmock-matchers-containers_test.cc +++ b/googlemock/test/gmock-matchers-containers_test.cc @@ -949,6 +949,17 @@ TEST(ResultOfTest, CanExplainMatchResult) { Explain(matcher, 36)); } +TEST(ResultOfTest, CanExplainMatchResultWithResultDescription) { + Matcher matcher = ResultOf("magic int conversion", &IntFunction, Ge(85)); + EXPECT_EQ("whose magic int conversion is 90" + OfType("int"), + Explain(matcher, 36)); + + matcher = ResultOf("magic int conversion", &IntFunction, GreaterThan(85)); + EXPECT_EQ("whose magic int conversion is 90" + OfType("int") + + ", which is 5 more than 85", + Explain(matcher, 36)); +} + // Tests that ResultOf(f, ...) compiles and works as expected when f(x) // returns a non-reference. TEST(ResultOfTest, WorksForNonReferenceResults) {