Adds a free function MatchAndExplain().

This commit is contained in:
zhanyong.wan 2010-03-05 21:23:23 +00:00
parent 5905ba00fe
commit 34b034c21e
2 changed files with 34 additions and 2 deletions

View File

@ -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()));

View File

@ -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())));