From 34b034c21ef4af7c0100194ed6f85910fc99debb Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Fri, 5 Mar 2010 21:23:23 +0000 Subject: [PATCH] Adds a free function MatchAndExplain(). --- include/gmock/gmock-matchers.h | 8 ++++++++ test/gmock-matchers_test.cc | 28 ++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/include/gmock/gmock-matchers.h b/include/gmock/gmock-matchers.h index ae7e131d..50c0d7bf 100644 --- a/include/gmock/gmock-matchers.h +++ b/include/gmock/gmock-matchers.h @@ -2850,6 +2850,14 @@ inline bool Value(const T& value, M matcher) { return testing::Matches(matcher)(value); } +// Matches the value against the given matcher and explains the match +// result to listener. +template +inline bool MatchAndExplain( + M matcher, const T& value, MatchResultListener* listener) { + return SafeMatcherCast(matcher).MatchAndExplain(value, listener); +} + // AllArgs(m) is a synonym of m. This is useful in // // EXPECT_CALL(foo, Bar(_, _)).With(AllArgs(Eq())); diff --git a/test/gmock-matchers_test.cc b/test/gmock-matchers_test.cc index b674cd8a..1eaecf9e 100644 --- a/test/gmock-matchers_test.cc +++ b/test/gmock-matchers_test.cc @@ -88,6 +88,7 @@ using testing::Matcher; using testing::MatcherCast; using testing::MatcherInterface; using testing::Matches; +using testing::MatchAndExplain; using testing::MatchResultListener; using testing::NanSensitiveDoubleEq; using testing::NanSensitiveFloatEq; @@ -118,6 +119,7 @@ using testing::internal::JoinAsTuple; using testing::internal::SkipPrefix; using testing::internal::String; using testing::internal::Strings; +using testing::internal::StringMatchResultListener; using testing::internal::ValidateMatcherDescription; using testing::internal::kInvalidInterpolation; using testing::internal::kPercentInterpolation; @@ -287,11 +289,11 @@ TEST(MatcherTest, CanDescribeItself) { // Tests Matcher::MatchAndExplain(). TEST(MatcherTest, MatchAndExplain) { Matcher m = GreaterThan(0); - ::testing::internal::StringMatchResultListener listener1; + StringMatchResultListener listener1; EXPECT_TRUE(m.MatchAndExplain(42, &listener1)); EXPECT_EQ("is 42 more than 0", listener1.str()); - ::testing::internal::StringMatchResultListener listener2; + StringMatchResultListener listener2; EXPECT_FALSE(m.MatchAndExplain(-9, &listener2)); EXPECT_EQ("is 9 less than 0", listener2.str()); } @@ -2047,6 +2049,28 @@ TEST(ValueTest, WorksWithMonomorphicMatcher) { EXPECT_FALSE(Value(1, ref_n)); } +TEST(MatchAndExplainTest, WorksWithPolymorphicMatcher) { + StringMatchResultListener listener1; + EXPECT_TRUE(MatchAndExplain(PolymorphicIsEven(), 42, &listener1)); + EXPECT_EQ("% 2 == 0", listener1.str()); + + StringMatchResultListener listener2; + EXPECT_FALSE(MatchAndExplain(Ge(42), 1.5, &listener2)); + EXPECT_EQ("", listener2.str()); +} + +TEST(MatchAndExplainTest, WorksWithMonomorphicMatcher) { + const Matcher is_even = PolymorphicIsEven(); + StringMatchResultListener listener1; + EXPECT_TRUE(MatchAndExplain(is_even, 42, &listener1)); + EXPECT_EQ("% 2 == 0", listener1.str()); + + const Matcher is_zero = Eq(0); + StringMatchResultListener listener2; + EXPECT_FALSE(MatchAndExplain(is_zero, 1.5, &listener2)); + EXPECT_EQ("", listener2.str()); +} + TEST(AllArgsTest, WorksForTuple) { EXPECT_THAT(make_tuple(1, 2L), AllArgs(Lt())); EXPECT_THAT(make_tuple(2L, 1), Not(AllArgs(Lt())));