diff options
author | Abseil Team <absl-team@google.com> | 2021-04-07 18:39:32 (GMT) |
---|---|---|
committer | Dino Radaković <dinor@google.com> | 2021-04-08 06:39:32 (GMT) |
commit | 8a65bc0303fc2cac63fe177f41b8348a988884cd (patch) | |
tree | 4be9d2b698220c3feffb9e2f4034b97ba0529d08 | |
parent | d0303e40c55fb4d301f3d6b7695344e9e67925bc (diff) | |
download | googletest-8a65bc0303fc2cac63fe177f41b8348a988884cd.zip googletest-8a65bc0303fc2cac63fe177f41b8348a988884cd.tar.gz googletest-8a65bc0303fc2cac63fe177f41b8348a988884cd.tar.bz2 |
Googletest export
Docs: Add examples for `FieldsAre` matcher
PiperOrigin-RevId: 367263024
-rw-r--r-- | docs/gmock_cheat_sheet.md | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/docs/gmock_cheat_sheet.md b/docs/gmock_cheat_sheet.md index ddc17cc..90be4a3 100644 --- a/docs/gmock_cheat_sheet.md +++ b/docs/gmock_cheat_sheet.md @@ -385,12 +385,28 @@ messages, you can use: | `Field(field_name, &class::field, m)` | The same as the two-parameter version, but provides a better error message. | | `Key(e)` | `argument.first` matches `e`, which can be either a value or a matcher. E.g. `Contains(Key(Le(5)))` can verify that a `map` contains a key `<= 5`. | | `Pair(m1, m2)` | `argument` is an `std::pair` whose `first` field matches `m1` and `second` field matches `m2`. | -| `FieldsAre(m...)` | `argument` is a compatible object where each field matches piecewise with `m...`. A compatible object is any that supports the `std::tuple_size<Obj>`+`get<I>(obj)` protocol. In C++17 and up this also supports types compatible with structured bindings, like aggregates. | +| `FieldsAre(m...)` | `argument` is a compatible object where each field matches piecewise with the matchers `m...`. A compatible object is any that supports the `std::tuple_size<Obj>`+`get<I>(obj)` protocol. In C++17 and up this also supports types compatible with structured bindings, like aggregates. | | `Property(&class::property, m)` | `argument.property()` (or `argument->property()` when `argument` is a plain pointer) matches matcher `m`, where `argument` is an object of type _class_. The method `property()` must take no argument and be declared as `const`. | | `Property(property_name, &class::property, m)` | The same as the two-parameter version, but provides a better error message. **Notes:** +* You can use `FieldsAre()` to match any type that supports structured + bindings, such as `std::tuple`, `std::pair`, `std::array`, and aggregate + types. For example: + + ```cpp + std::tuple<int, std::string> my_tuple{7, "hello world"}; + EXPECT_THAT(my_tuple, FieldsAre(Ge(0), HasSubstr("hello"))); + + struct MyStruct { + int value = 42; + std::string greeting = "aloha"; + }; + MyStruct s; + EXPECT_THAT(s, FieldsAre(42, "aloha")); + ``` + * Don't use `Property()` against member functions that you do not own, because taking addresses of functions is fragile and generally not part of the contract of the function. |