diff options
author | Abseil Team <absl-team@google.com> | 2022-04-28 21:41:12 (GMT) |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2022-04-28 21:41:49 (GMT) |
commit | 8ded48c37be09d8cc3665af1b414c5d53c0862e7 (patch) | |
tree | 0f7623a3b9c401b3d57ba2dfd9ade41b249e71b0 | |
parent | 830fb567285c63ab5b5873e2e8b02f2249864916 (diff) | |
download | googletest-8ded48c37be09d8cc3665af1b414c5d53c0862e7.zip googletest-8ded48c37be09d8cc3665af1b414c5d53c0862e7.tar.gz googletest-8ded48c37be09d8cc3665af1b414c5d53c0862e7.tar.bz2 |
Rewrite "Testing a Certain Property of an Object" as "Defining a Custom Matcher Class", and fix the code examples.
PiperOrigin-RevId: 445252626
Change-Id: I9f038cb669d3da6743606343c2341fc59725d722
-rw-r--r-- | docs/gmock_cook_book.md | 39 |
1 files changed, 22 insertions, 17 deletions
diff --git a/docs/gmock_cook_book.md b/docs/gmock_cook_book.md index 9494f12..bf52fa8 100644 --- a/docs/gmock_cook_book.md +++ b/docs/gmock_cook_book.md @@ -1300,23 +1300,27 @@ What if you have a pointer to pointer? You guessed it - you can use nested `Pointee(Pointee(Lt(3)))` matches a pointer that points to a pointer that points to a number less than 3 (what a mouthful...). -### Testing a Certain Property of an Object +### Defining a Custom Matcher Class {#CustomMatcherClass} -Sometimes you want to specify that an object argument has a certain property, -but there is no existing matcher that does this. If you want good error -messages, you should [define a matcher](#NewMatchers). If you want to do it -quick and dirty, you could get away with writing an ordinary function. +Most matchers can be simply defined using [the MATCHER* macros](#NewMatchers), +which are terse and flexible, and produce good error messages. However, these +macros are not very explicit about the interfaces they create and are not always +suitable, especially for matchers that will be widely reused. -Let's say you have a mock function that takes an object of type `Foo`, which has -an `int bar()` method and an `int baz()` method, and you want to constrain that -the argument's `bar()` value plus its `baz()` value is a given number. Here's -how you can define a matcher to do it: +For more advanced cases, you may need to define your own matcher class. A custom +matcher allows you to test a specific invariant property of that object. Let's +take a look at how to do so. -```cpp -using ::testing::Matcher; +Imagine you have a mock function that takes an object of type `Foo`, which has +an `int bar()` method and an `int baz()` method. You want to constrain that the +argument's `bar()` value plus its `baz()` value is a given number. (This is an +invariant.) Here's how we can write and use a matcher class to do so: +```cpp class BarPlusBazEqMatcher { public: + using is_gtest_matcher = void; + explicit BarPlusBazEqMatcher(int expected_sum) : expected_sum_(expected_sum) {} @@ -1325,23 +1329,24 @@ class BarPlusBazEqMatcher { return (foo.bar() + foo.baz()) == expected_sum_; } - void DescribeTo(std::ostream& os) const { - os << "bar() + baz() equals " << expected_sum_; + void DescribeTo(std::ostream* os) const { + *os << "bar() + baz() equals " << expected_sum_; } - void DescribeNegationTo(std::ostream& os) const { - os << "bar() + baz() does not equal " << expected_sum_; + void DescribeNegationTo(std::ostream* os) const { + *os << "bar() + baz() does not equal " << expected_sum_; } private: const int expected_sum_; }; -Matcher<const Foo&> BarPlusBazEq(int expected_sum) { +::testing::Matcher<const Foo&> BarPlusBazEq(int expected_sum) { return BarPlusBazEqMatcher(expected_sum); } ... - EXPECT_CALL(..., DoThis(BarPlusBazEq(5)))...; + Foo foo; + EXPECT_CALL(foo, BarPlusBazEq(5))...; ``` ### Matching Containers |