diff options
author | Abseil Team <absl-team@google.com> | 2020-10-13 14:14:25 (GMT) |
---|---|---|
committer | Derek Mauro <dmauro@google.com> | 2020-10-14 22:25:40 (GMT) |
commit | d11c76175f24f23a1fd316fe37d3743133479e27 (patch) | |
tree | 8aa1ee355e320b5772f3b78f6bf01460015a6485 /googlemock/docs/cook_book.md | |
parent | 2cf1f99b97e5a50b5d9db3d5450a6b474e36b79b (diff) | |
download | googletest-d11c76175f24f23a1fd316fe37d3743133479e27.zip googletest-d11c76175f24f23a1fd316fe37d3743133479e27.tar.gz googletest-d11c76175f24f23a1fd316fe37d3743133479e27.tar.bz2 |
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
Diffstat (limited to 'googlemock/docs/cook_book.md')
-rw-r--r-- | googlemock/docs/cook_book.md | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/googlemock/docs/cook_book.md b/googlemock/docs/cook_book.md index f8e9a50..817d5ca 100644 --- a/googlemock/docs/cook_book.md +++ b/googlemock/docs/cook_book.md @@ -859,6 +859,22 @@ using ::testing::Not; 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} gMock matchers are statically typed, meaning that the compiler can catch your |