summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzhanyong.wan <zhanyong.wan@8415998a-534a-0410-bf83-d39667b30386>2010-03-05 21:23:23 +0000
committerzhanyong.wan <zhanyong.wan@8415998a-534a-0410-bf83-d39667b30386>2010-03-05 21:23:23 +0000
commit922a0f47eff3a416a73c1d0f70634c6584d7f239 (patch)
treee01e2f5130857b6e5b942eec61d45d2f32f35278
parent481ecf2bd66820bfb95dd99c8af76801c10036f6 (diff)
downloadgooglemock-922a0f47eff3a416a73c1d0f70634c6584d7f239.tar.gz
Adds a free function MatchAndExplain().
git-svn-id: http://googlemock.googlecode.com/svn/trunk@274 8415998a-534a-0410-bf83-d39667b30386
-rw-r--r--include/gmock/gmock-matchers.h8
-rw-r--r--test/gmock-matchers_test.cc28
2 files changed, 34 insertions, 2 deletions
diff --git a/include/gmock/gmock-matchers.h b/include/gmock/gmock-matchers.h
index ae7e131..50c0d7b 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 <typename T, typename M>
+inline bool MatchAndExplain(
+ M matcher, const T& value, MatchResultListener* listener) {
+ return SafeMatcherCast<const T&>(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 b674cd8..1eaecf9 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<T>::MatchAndExplain().
TEST(MatcherTest, MatchAndExplain) {
Matcher<int> 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<int> is_even = PolymorphicIsEven();
+ StringMatchResultListener listener1;
+ EXPECT_TRUE(MatchAndExplain(is_even, 42, &listener1));
+ EXPECT_EQ("% 2 == 0", listener1.str());
+
+ const Matcher<const double&> 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())));