Implements the new matcher API.
This commit is contained in:
@@ -136,6 +136,16 @@ TEST(ArgsTest, AcceptsDecreasingTemplateArgs) {
|
||||
EXPECT_THAT(t, Not(Args<2, 1>(Lt())));
|
||||
}
|
||||
|
||||
// The MATCHER*() macros trigger warning C4100 (unreferenced formal
|
||||
// parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
|
||||
// the macro definition, as the warnings are generated when the macro
|
||||
// is expanded and macro expansion cannot contain #pragma. Therefore
|
||||
// we suppress them here.
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable:4100)
|
||||
#endif
|
||||
|
||||
MATCHER(SumIsZero, "") {
|
||||
return get<0>(arg) + get<1>(arg) + get<2>(arg) == 0;
|
||||
}
|
||||
@@ -553,6 +563,44 @@ TEST(MatcherMacroTest, Works) {
|
||||
EXPECT_EQ("", Explain(m, 7));
|
||||
}
|
||||
|
||||
// Tests explaining match result in a MATCHER* macro.
|
||||
|
||||
MATCHER(IsEven2, "is even") {
|
||||
if ((arg % 2) == 0) {
|
||||
// Verifies that we can stream to result_listener, a listener
|
||||
// supplied by the MATCHER macro implicitly.
|
||||
*result_listener << "OK";
|
||||
return true;
|
||||
} else {
|
||||
*result_listener << "% 2 == " << (arg % 2);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
MATCHER_P2(EqSumOf, x, y, "") {
|
||||
if (arg == (x + y)) {
|
||||
*result_listener << "OK";
|
||||
return true;
|
||||
} else {
|
||||
// Verifies that we can stream to the underlying stream of
|
||||
// result_listener.
|
||||
if (result_listener->stream() != NULL) {
|
||||
*result_listener->stream() << "diff == " << (x + y - arg);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
TEST(MatcherMacroTest, CanExplainMatchResult) {
|
||||
const Matcher<int> m1 = IsEven2();
|
||||
EXPECT_EQ("OK", Explain(m1, 4));
|
||||
EXPECT_EQ("% 2 == 1", Explain(m1, 5));
|
||||
|
||||
const Matcher<int> m2 = EqSumOf(1, 2);
|
||||
EXPECT_EQ("OK", Explain(m2, 3));
|
||||
EXPECT_EQ("diff == -1", Explain(m2, 4));
|
||||
}
|
||||
|
||||
// Tests that the description string supplied to MATCHER() must be
|
||||
// valid.
|
||||
|
||||
@@ -1052,4 +1100,8 @@ TEST(ContainsTest, WorksForTwoDimensionalNativeArray) {
|
||||
EXPECT_THAT(a, Contains(Not(Contains(5))));
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user