Commit d11c7617 authored by Abseil Team's avatar Abseil Team Committed by Derek Mauro
Browse files

Googletest export

Suggest using generic lambdas for composing macros.

Long chains of macros hurt legibility; generic lambdas are an easy way to abbreviate them, but are not an obvious solution to casual users.

Compare:
EXPECT_THAT(f(), ElementsAre(
    Property(&MyClass::foo, Property(&OtherClass::bar, Contains("x"))),
    Property(&MyClass::foo, Property(&OtherClass::bar, Contains("y"))));
to:
EXPECT_THAT(f(), ElementsAre(HasFooBar("x"), HasFooBar("y")));
PiperOrigin-RevId: 336870137
parent 2cf1f99b
...@@ -859,6 +859,22 @@ using ::testing::Not; ...@@ -859,6 +859,22 @@ using ::testing::Not;
NULL)); NULL));
``` ```
Matchers are function objects, and parametrized matchers can be composed just
like any other function. However because their types can be long and rarely
provide meaningful information, it can be easier to express them with C++14
generic lambdas to avoid specifying types. For example,
```cpp
using ::testing::Contains;
using ::testing::Property;
inline constexpr auto HasFoo = [](const auto& f) {
return Property(&MyClass::foo, Contains(f));
};
...
EXPECT_THAT(x, HasFoo("blah"));
```
### Casting Matchers {#SafeMatcherCast} ### Casting Matchers {#SafeMatcherCast}
gMock matchers are statically typed, meaning that the compiler can catch your gMock matchers are statically typed, meaning that the compiler can catch your
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment