Hi,
Matchers let you precisely express your intention in tests. For
example, you can write:
EXPECT_CALL(mock_obj, Foo(AllOf(IsPrime(), Lt(100))));
to validate that Foo() is called with a prime number less than 100,
where
AllOf(IsPrime(), Lt(100)) is the matcher that describes the
exact property you are testing. If the match fails, a matcher can
also tell you why, making it easier to understand/debug the failure.
Google Mock contains a library of frequently-used matchers
(
http://code.google.com/p/googlemock/wiki/CheatSheet#Matchers), and
allows you to define your custom matchers in case the built-in ones
aren't adequate. We just made it (much) easier to define your own
matchers.
Macros
MATCHER,
MATCHER_P, and
MATCHER_Pk (where 2 <= k <= 10) were
checked into the SVN trunk yesterday. You can try them out now if you
check out the SVN, or you can wait for the next release of Google
Mock, which will probably be in a couple of weeks.
Some examples:
MATCHER(IsEven, "") { return (arg % 2) == 0; }...
EXPECT_THAT(42, IsEven()); // Succeeds.EXPECT_THAT(13, IsEven()); // Fails.
MATCHER_p2(InClosedRange, low, hi, "") { return low <= arg && arg <= hi; }
...EXPECT_THAT(10, InClosedRange(3, 12)); // Succeeds.
EXPECT_THAT(std::string("zoo"), InClosedRange("bar", "foo")); // Fails.(So how's this different from a bool function? First, MATCHER* defines a polymorphic matcher that can be used to match values of different types (as in the InClosedRange() example above). Second, when a match fails, the values of it parameters (if any) along with a short explanation will be printed.)
For more information, please read the cookbook wiki:
http://code.google.com/p/googlemock/wiki/CookBook#Writing_New_Matchers_Quickly
Thanks to Chandler and Vlad for discussions and reviews.
--
Zhanyong