mirror of
https://github.com/google/googletest.git
synced 2025-03-28 09:13:57 +00:00
Clarify the polymorphic matcher concept and explain how to define a composite matcher.
Many gtest users aren't aware that a polymorphic matcher is not actually a `testing::Matcher<>` and thus doesn't know how to describe itself. Clarify this. Also adds a recipe on how to define a composite matcher. In particular, explain how the composite matcher can describe itself in terms of the description(s) of its sub-matcher(s). PiperOrigin-RevId: 735840759 Change-Id: I26cff6179349aa739fc7fdd528a2f5308d18c189
This commit is contained in:
parent
4ee4b17bf5
commit
4902ea2d7c
@ -3567,10 +3567,15 @@ just based on the number of parameters).
|
|||||||
|
|
||||||
### Writing New Monomorphic Matchers
|
### Writing New Monomorphic Matchers
|
||||||
|
|
||||||
A matcher of argument type `T` implements the matcher interface for `T` and does
|
A matcher of type `testing::Matcher<T>` implements the matcher interface for `T`
|
||||||
two things: it tests whether a value of type `T` matches the matcher, and can
|
and does two things: it tests whether a value of type `T` matches the matcher,
|
||||||
describe what kind of values it matches. The latter ability is used for
|
and can describe what kind of values it matches. The latter ability is used for
|
||||||
generating readable error messages when expectations are violated.
|
generating readable error messages when expectations are violated. Some matchers
|
||||||
|
can even explain why it matches or doesn't match a certain value, which can be
|
||||||
|
helpful when the reason isn't obvious.
|
||||||
|
|
||||||
|
Because a matcher of type `testing::Matcher<T>` for a particular type `T` can
|
||||||
|
only be used to match a value of type `T`, we call it "monomorphic."
|
||||||
|
|
||||||
A matcher of `T` must declare a typedef like:
|
A matcher of `T` must declare a typedef like:
|
||||||
|
|
||||||
@ -3662,8 +3667,16 @@ instead of `std::ostream*`.
|
|||||||
|
|
||||||
### Writing New Polymorphic Matchers
|
### Writing New Polymorphic Matchers
|
||||||
|
|
||||||
Expanding what we learned above to *polymorphic* matchers is now just as simple
|
Unlike a monomorphic matcher, which can only be used to match a value of a
|
||||||
as adding templates in the right place.
|
particular type, a *polymorphic* matcher is one that can be used to match values
|
||||||
|
of multiple types. For example, `Eq(5)` is a polymorhpic matcher as it can be
|
||||||
|
used to match an `int`, a `double`, a `float`, and so on. You should think of a
|
||||||
|
polymorphic matcher as a *matcher factory* as opposed to a
|
||||||
|
`testing::Matcher<SomeType>` - itself is not an actual matcher, but can be
|
||||||
|
implicitly converted to a `testing::Matcher<SomeType>` depending on the context.
|
||||||
|
|
||||||
|
Expanding what we learned above to polymorphic matchers is now as simple as
|
||||||
|
adding templates in the right place.
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
|
|
||||||
@ -3789,6 +3802,26 @@ virtual.
|
|||||||
Like in a monomorphic matcher, you may explain the match result by streaming
|
Like in a monomorphic matcher, you may explain the match result by streaming
|
||||||
additional information to the `listener` argument in `MatchAndExplain()`.
|
additional information to the `listener` argument in `MatchAndExplain()`.
|
||||||
|
|
||||||
|
### Implementing Composite Matchers {#CompositeMatchers}
|
||||||
|
|
||||||
|
Sometimes we want to define a matcher that takes other matchers as parameters.
|
||||||
|
For example, `DistanceFrom(target, m)` is a polymorphic matcher that takes a
|
||||||
|
matcher `m` as a parameter. It tests that the distance from `target` to the
|
||||||
|
value being matched satisfies sub-matcher `m`.
|
||||||
|
|
||||||
|
If you are implementing such a composite matcher, you'll need to generate the
|
||||||
|
description of the matcher based on the description(s) of its sub-matcher(s).
|
||||||
|
You can see the implementation of `DistanceFrom()` in
|
||||||
|
`googlemock/include/gmock/gmock-matchers.h` for an example. In particular, pay
|
||||||
|
attention to `DistanceFromMatcherImpl`. Notice that it stores the sub-matcher as
|
||||||
|
a `const Matcher<const Distance&> distance_matcher_` instead of a polymorphic
|
||||||
|
matcher - this allows it to call `distance_matcher_.DescribeTo(os)` to describe
|
||||||
|
the sub-matcher. If the sub-matcher is stored as a polymorphic matcher instead,
|
||||||
|
it would not be possible to get its description as in general polymorphic
|
||||||
|
matchers don't know how to describe themselves - they are matcher factories
|
||||||
|
instead of actual matchers; only after being converted to `Matcher<SomeType>`
|
||||||
|
can they be described.
|
||||||
|
|
||||||
### Writing New Cardinalities
|
### Writing New Cardinalities
|
||||||
|
|
||||||
A cardinality is used in `Times()` to tell gMock how many times you expect a
|
A cardinality is used in `Times()` to tell gMock how many times you expect a
|
||||||
|
Loading…
x
Reference in New Issue
Block a user