1f92157deSopenharmony_ci# Matchers Reference 2f92157deSopenharmony_ci 3f92157deSopenharmony_ciA **matcher** matches a *single* argument. You can use it inside `ON_CALL()` or 4f92157deSopenharmony_ci`EXPECT_CALL()`, or use it to validate a value directly using two macros: 5f92157deSopenharmony_ci 6f92157deSopenharmony_ci| Macro | Description | 7f92157deSopenharmony_ci| :----------------------------------- | :------------------------------------ | 8f92157deSopenharmony_ci| `EXPECT_THAT(actual_value, matcher)` | Asserts that `actual_value` matches `matcher`. | 9f92157deSopenharmony_ci| `ASSERT_THAT(actual_value, matcher)` | The same as `EXPECT_THAT(actual_value, matcher)`, except that it generates a **fatal** failure. | 10f92157deSopenharmony_ci 11f92157deSopenharmony_ci{: .callout .warning} 12f92157deSopenharmony_ci**WARNING:** Equality matching via `EXPECT_THAT(actual_value, expected_value)` 13f92157deSopenharmony_ciis supported, however note that implicit conversions can cause surprising 14f92157deSopenharmony_ciresults. For example, `EXPECT_THAT(some_bool, "some string")` will compile and 15f92157deSopenharmony_cimay pass unintentionally. 16f92157deSopenharmony_ci 17f92157deSopenharmony_ci**BEST PRACTICE:** Prefer to make the comparison explicit via 18f92157deSopenharmony_ci`EXPECT_THAT(actual_value, Eq(expected_value))` or `EXPECT_EQ(actual_value, 19f92157deSopenharmony_ciexpected_value)`. 20f92157deSopenharmony_ci 21f92157deSopenharmony_ciBuilt-in matchers (where `argument` is the function argument, e.g. 22f92157deSopenharmony_ci`actual_value` in the example above, or when used in the context of 23f92157deSopenharmony_ci`EXPECT_CALL(mock_object, method(matchers))`, the arguments of `method`) are 24f92157deSopenharmony_cidivided into several categories. All matchers are defined in the `::testing` 25f92157deSopenharmony_cinamespace unless otherwise noted. 26f92157deSopenharmony_ci 27f92157deSopenharmony_ci## Wildcard 28f92157deSopenharmony_ci 29f92157deSopenharmony_ciMatcher | Description 30f92157deSopenharmony_ci:-------------------------- | :----------------------------------------------- 31f92157deSopenharmony_ci`_` | `argument` can be any value of the correct type. 32f92157deSopenharmony_ci`A<type>()` or `An<type>()` | `argument` can be any value of type `type`. 33f92157deSopenharmony_ci 34f92157deSopenharmony_ci## Generic Comparison 35f92157deSopenharmony_ci 36f92157deSopenharmony_ci| Matcher | Description | 37f92157deSopenharmony_ci| :--------------------- | :-------------------------------------------------- | 38f92157deSopenharmony_ci| `Eq(value)` or `value` | `argument == value` | 39f92157deSopenharmony_ci| `Ge(value)` | `argument >= value` | 40f92157deSopenharmony_ci| `Gt(value)` | `argument > value` | 41f92157deSopenharmony_ci| `Le(value)` | `argument <= value` | 42f92157deSopenharmony_ci| `Lt(value)` | `argument < value` | 43f92157deSopenharmony_ci| `Ne(value)` | `argument != value` | 44f92157deSopenharmony_ci| `IsFalse()` | `argument` evaluates to `false` in a Boolean context. | 45f92157deSopenharmony_ci| `IsTrue()` | `argument` evaluates to `true` in a Boolean context. | 46f92157deSopenharmony_ci| `IsNull()` | `argument` is a `NULL` pointer (raw or smart). | 47f92157deSopenharmony_ci| `NotNull()` | `argument` is a non-null pointer (raw or smart). | 48f92157deSopenharmony_ci| `Optional(m)` | `argument` is `optional<>` that contains a value matching `m`. (For testing whether an `optional<>` is set, check for equality with `nullopt`. You may need to use `Eq(nullopt)` if the inner type doesn't have `==`.)| 49f92157deSopenharmony_ci| `VariantWith<T>(m)` | `argument` is `variant<>` that holds the alternative of type T with a value matching `m`. | 50f92157deSopenharmony_ci| `Ref(variable)` | `argument` is a reference to `variable`. | 51f92157deSopenharmony_ci| `TypedEq<type>(value)` | `argument` has type `type` and is equal to `value`. You may need to use this instead of `Eq(value)` when the mock function is overloaded. | 52f92157deSopenharmony_ci 53f92157deSopenharmony_ciExcept `Ref()`, these matchers make a *copy* of `value` in case it's modified or 54f92157deSopenharmony_cidestructed later. If the compiler complains that `value` doesn't have a public 55f92157deSopenharmony_cicopy constructor, try wrap it in `std::ref()`, e.g. 56f92157deSopenharmony_ci`Eq(std::ref(non_copyable_value))`. If you do that, make sure 57f92157deSopenharmony_ci`non_copyable_value` is not changed afterwards, or the meaning of your matcher 58f92157deSopenharmony_ciwill be changed. 59f92157deSopenharmony_ci 60f92157deSopenharmony_ci`IsTrue` and `IsFalse` are useful when you need to use a matcher, or for types 61f92157deSopenharmony_cithat can be explicitly converted to Boolean, but are not implicitly converted to 62f92157deSopenharmony_ciBoolean. In other cases, you can use the basic 63f92157deSopenharmony_ci[`EXPECT_TRUE` and `EXPECT_FALSE`](assertions.md#boolean) assertions. 64f92157deSopenharmony_ci 65f92157deSopenharmony_ci## Floating-Point Matchers {#FpMatchers} 66f92157deSopenharmony_ci 67f92157deSopenharmony_ci| Matcher | Description | 68f92157deSopenharmony_ci| :------------------------------- | :--------------------------------- | 69f92157deSopenharmony_ci| `DoubleEq(a_double)` | `argument` is a `double` value approximately equal to `a_double`, treating two NaNs as unequal. | 70f92157deSopenharmony_ci| `FloatEq(a_float)` | `argument` is a `float` value approximately equal to `a_float`, treating two NaNs as unequal. | 71f92157deSopenharmony_ci| `NanSensitiveDoubleEq(a_double)` | `argument` is a `double` value approximately equal to `a_double`, treating two NaNs as equal. | 72f92157deSopenharmony_ci| `NanSensitiveFloatEq(a_float)` | `argument` is a `float` value approximately equal to `a_float`, treating two NaNs as equal. | 73f92157deSopenharmony_ci| `IsNan()` | `argument` is any floating-point type with a NaN value. | 74f92157deSopenharmony_ci 75f92157deSopenharmony_ciThe above matchers use ULP-based comparison (the same as used in googletest). 76f92157deSopenharmony_ciThey automatically pick a reasonable error bound based on the absolute value of 77f92157deSopenharmony_cithe expected value. `DoubleEq()` and `FloatEq()` conform to the IEEE standard, 78f92157deSopenharmony_ciwhich requires comparing two NaNs for equality to return false. The 79f92157deSopenharmony_ci`NanSensitive*` version instead treats two NaNs as equal, which is often what a 80f92157deSopenharmony_ciuser wants. 81f92157deSopenharmony_ci 82f92157deSopenharmony_ci| Matcher | Description | 83f92157deSopenharmony_ci| :------------------------------------------------ | :----------------------- | 84f92157deSopenharmony_ci| `DoubleNear(a_double, max_abs_error)` | `argument` is a `double` value close to `a_double` (absolute error <= `max_abs_error`), treating two NaNs as unequal. | 85f92157deSopenharmony_ci| `FloatNear(a_float, max_abs_error)` | `argument` is a `float` value close to `a_float` (absolute error <= `max_abs_error`), treating two NaNs as unequal. | 86f92157deSopenharmony_ci| `NanSensitiveDoubleNear(a_double, max_abs_error)` | `argument` is a `double` value close to `a_double` (absolute error <= `max_abs_error`), treating two NaNs as equal. | 87f92157deSopenharmony_ci| `NanSensitiveFloatNear(a_float, max_abs_error)` | `argument` is a `float` value close to `a_float` (absolute error <= `max_abs_error`), treating two NaNs as equal. | 88f92157deSopenharmony_ci 89f92157deSopenharmony_ci## String Matchers 90f92157deSopenharmony_ci 91f92157deSopenharmony_ciThe `argument` can be either a C string or a C++ string object: 92f92157deSopenharmony_ci 93f92157deSopenharmony_ci| Matcher | Description | 94f92157deSopenharmony_ci| :---------------------- | :------------------------------------------------- | 95f92157deSopenharmony_ci| `ContainsRegex(string)` | `argument` matches the given regular expression. | 96f92157deSopenharmony_ci| `EndsWith(suffix)` | `argument` ends with string `suffix`. | 97f92157deSopenharmony_ci| `HasSubstr(string)` | `argument` contains `string` as a sub-string. | 98f92157deSopenharmony_ci| `IsEmpty()` | `argument` is an empty string. | 99f92157deSopenharmony_ci| `MatchesRegex(string)` | `argument` matches the given regular expression with the match starting at the first character and ending at the last character. | 100f92157deSopenharmony_ci| `StartsWith(prefix)` | `argument` starts with string `prefix`. | 101f92157deSopenharmony_ci| `StrCaseEq(string)` | `argument` is equal to `string`, ignoring case. | 102f92157deSopenharmony_ci| `StrCaseNe(string)` | `argument` is not equal to `string`, ignoring case. | 103f92157deSopenharmony_ci| `StrEq(string)` | `argument` is equal to `string`. | 104f92157deSopenharmony_ci| `StrNe(string)` | `argument` is not equal to `string`. | 105f92157deSopenharmony_ci| `WhenBase64Unescaped(m)` | `argument` is a base-64 escaped string whose unescaped string matches `m`. | 106f92157deSopenharmony_ci 107f92157deSopenharmony_ci`ContainsRegex()` and `MatchesRegex()` take ownership of the `RE` object. They 108f92157deSopenharmony_ciuse the regular expression syntax defined 109f92157deSopenharmony_ci[here](../advanced.md#regular-expression-syntax). All of these matchers, except 110f92157deSopenharmony_ci`ContainsRegex()` and `MatchesRegex()` work for wide strings as well. 111f92157deSopenharmony_ci 112f92157deSopenharmony_ci## Container Matchers 113f92157deSopenharmony_ci 114f92157deSopenharmony_ciMost STL-style containers support `==`, so you can use `Eq(expected_container)` 115f92157deSopenharmony_cior simply `expected_container` to match a container exactly. If you want to 116f92157deSopenharmony_ciwrite the elements in-line, match them more flexibly, or get more informative 117f92157deSopenharmony_cimessages, you can use: 118f92157deSopenharmony_ci 119f92157deSopenharmony_ci| Matcher | Description | 120f92157deSopenharmony_ci| :---------------------------------------- | :------------------------------- | 121f92157deSopenharmony_ci| `BeginEndDistanceIs(m)` | `argument` is a container whose `begin()` and `end()` iterators are separated by a number of increments matching `m`. E.g. `BeginEndDistanceIs(2)` or `BeginEndDistanceIs(Lt(2))`. For containers that define a `size()` method, `SizeIs(m)` may be more efficient. | 122f92157deSopenharmony_ci| `ContainerEq(container)` | The same as `Eq(container)` except that the failure message also includes which elements are in one container but not the other. | 123f92157deSopenharmony_ci| `Contains(e)` | `argument` contains an element that matches `e`, which can be either a value or a matcher. | 124f92157deSopenharmony_ci| `Contains(e).Times(n)` | `argument` contains elements that match `e`, which can be either a value or a matcher, and the number of matches is `n`, which can be either a value or a matcher. Unlike the plain `Contains` and `Each` this allows to check for arbitrary occurrences including testing for absence with `Contains(e).Times(0)`. | 125f92157deSopenharmony_ci| `Each(e)` | `argument` is a container where *every* element matches `e`, which can be either a value or a matcher. | 126f92157deSopenharmony_ci| `ElementsAre(e0, e1, ..., en)` | `argument` has `n + 1` elements, where the *i*-th element matches `ei`, which can be a value or a matcher. | 127f92157deSopenharmony_ci| `ElementsAreArray({e0, e1, ..., en})`, `ElementsAreArray(a_container)`, `ElementsAreArray(begin, end)`, `ElementsAreArray(array)`, or `ElementsAreArray(array, count)` | The same as `ElementsAre()` except that the expected element values/matchers come from an initializer list, STL-style container, iterator range, or C-style array. | 128f92157deSopenharmony_ci| `IsEmpty()` | `argument` is an empty container (`container.empty()`). | 129f92157deSopenharmony_ci| `IsSubsetOf({e0, e1, ..., en})`, `IsSubsetOf(a_container)`, `IsSubsetOf(begin, end)`, `IsSubsetOf(array)`, or `IsSubsetOf(array, count)` | `argument` matches `UnorderedElementsAre(x0, x1, ..., xk)` for some subset `{x0, x1, ..., xk}` of the expected matchers. | 130f92157deSopenharmony_ci| `IsSupersetOf({e0, e1, ..., en})`, `IsSupersetOf(a_container)`, `IsSupersetOf(begin, end)`, `IsSupersetOf(array)`, or `IsSupersetOf(array, count)` | Some subset of `argument` matches `UnorderedElementsAre(`expected matchers`)`. | 131f92157deSopenharmony_ci| `Pointwise(m, container)`, `Pointwise(m, {e0, e1, ..., en})` | `argument` contains the same number of elements as in `container`, and for all i, (the i-th element in `argument`, the i-th element in `container`) match `m`, which is a matcher on 2-tuples. E.g. `Pointwise(Le(), upper_bounds)` verifies that each element in `argument` doesn't exceed the corresponding element in `upper_bounds`. See more detail below. | 132f92157deSopenharmony_ci| `SizeIs(m)` | `argument` is a container whose size matches `m`. E.g. `SizeIs(2)` or `SizeIs(Lt(2))`. | 133f92157deSopenharmony_ci| `UnorderedElementsAre(e0, e1, ..., en)` | `argument` has `n + 1` elements, and under *some* permutation of the elements, each element matches an `ei` (for a different `i`), which can be a value or a matcher. | 134f92157deSopenharmony_ci| `UnorderedElementsAreArray({e0, e1, ..., en})`, `UnorderedElementsAreArray(a_container)`, `UnorderedElementsAreArray(begin, end)`, `UnorderedElementsAreArray(array)`, or `UnorderedElementsAreArray(array, count)` | The same as `UnorderedElementsAre()` except that the expected element values/matchers come from an initializer list, STL-style container, iterator range, or C-style array. | 135f92157deSopenharmony_ci| `UnorderedPointwise(m, container)`, `UnorderedPointwise(m, {e0, e1, ..., en})` | Like `Pointwise(m, container)`, but ignores the order of elements. | 136f92157deSopenharmony_ci| `WhenSorted(m)` | When `argument` is sorted using the `<` operator, it matches container matcher `m`. E.g. `WhenSorted(ElementsAre(1, 2, 3))` verifies that `argument` contains elements 1, 2, and 3, ignoring order. | 137f92157deSopenharmony_ci| `WhenSortedBy(comparator, m)` | The same as `WhenSorted(m)`, except that the given comparator instead of `<` is used to sort `argument`. E.g. `WhenSortedBy(std::greater(), ElementsAre(3, 2, 1))`. | 138f92157deSopenharmony_ci 139f92157deSopenharmony_ci**Notes:** 140f92157deSopenharmony_ci 141f92157deSopenharmony_ci* These matchers can also match: 142f92157deSopenharmony_ci 1. a native array passed by reference (e.g. in `Foo(const int (&a)[5])`), 143f92157deSopenharmony_ci and 144f92157deSopenharmony_ci 2. an array passed as a pointer and a count (e.g. in `Bar(const T* buffer, 145f92157deSopenharmony_ci int len)` -- see [Multi-argument Matchers](#MultiArgMatchers)). 146f92157deSopenharmony_ci* The array being matched may be multi-dimensional (i.e. its elements can be 147f92157deSopenharmony_ci arrays). 148f92157deSopenharmony_ci* `m` in `Pointwise(m, ...)` and `UnorderedPointwise(m, ...)` should be a 149f92157deSopenharmony_ci matcher for `::std::tuple<T, U>` where `T` and `U` are the element type of 150f92157deSopenharmony_ci the actual container and the expected container, respectively. For example, 151f92157deSopenharmony_ci to compare two `Foo` containers where `Foo` doesn't support `operator==`, 152f92157deSopenharmony_ci one might write: 153f92157deSopenharmony_ci 154f92157deSopenharmony_ci ```cpp 155f92157deSopenharmony_ci MATCHER(FooEq, "") { 156f92157deSopenharmony_ci return std::get<0>(arg).Equals(std::get<1>(arg)); 157f92157deSopenharmony_ci } 158f92157deSopenharmony_ci ... 159f92157deSopenharmony_ci EXPECT_THAT(actual_foos, Pointwise(FooEq(), expected_foos)); 160f92157deSopenharmony_ci ``` 161f92157deSopenharmony_ci 162f92157deSopenharmony_ci## Member Matchers 163f92157deSopenharmony_ci 164f92157deSopenharmony_ci| Matcher | Description | 165f92157deSopenharmony_ci| :------------------------------ | :----------------------------------------- | 166f92157deSopenharmony_ci| `Field(&class::field, m)` | `argument.field` (or `argument->field` when `argument` is a plain pointer) matches matcher `m`, where `argument` is an object of type _class_. | 167f92157deSopenharmony_ci| `Field(field_name, &class::field, m)` | The same as the two-parameter version, but provides a better error message. | 168f92157deSopenharmony_ci| `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`. | 169f92157deSopenharmony_ci| `Pair(m1, m2)` | `argument` is an `std::pair` whose `first` field matches `m1` and `second` field matches `m2`. | 170f92157deSopenharmony_ci| `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. | 171f92157deSopenharmony_ci| `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`. | 172f92157deSopenharmony_ci| `Property(property_name, &class::property, m)` | The same as the two-parameter version, but provides a better error message. 173f92157deSopenharmony_ci 174f92157deSopenharmony_ci**Notes:** 175f92157deSopenharmony_ci 176f92157deSopenharmony_ci* You can use `FieldsAre()` to match any type that supports structured 177f92157deSopenharmony_ci bindings, such as `std::tuple`, `std::pair`, `std::array`, and aggregate 178f92157deSopenharmony_ci types. For example: 179f92157deSopenharmony_ci 180f92157deSopenharmony_ci ```cpp 181f92157deSopenharmony_ci std::tuple<int, std::string> my_tuple{7, "hello world"}; 182f92157deSopenharmony_ci EXPECT_THAT(my_tuple, FieldsAre(Ge(0), HasSubstr("hello"))); 183f92157deSopenharmony_ci 184f92157deSopenharmony_ci struct MyStruct { 185f92157deSopenharmony_ci int value = 42; 186f92157deSopenharmony_ci std::string greeting = "aloha"; 187f92157deSopenharmony_ci }; 188f92157deSopenharmony_ci MyStruct s; 189f92157deSopenharmony_ci EXPECT_THAT(s, FieldsAre(42, "aloha")); 190f92157deSopenharmony_ci ``` 191f92157deSopenharmony_ci 192f92157deSopenharmony_ci* Don't use `Property()` against member functions that you do not own, because 193f92157deSopenharmony_ci taking addresses of functions is fragile and generally not part of the 194f92157deSopenharmony_ci contract of the function. 195f92157deSopenharmony_ci 196f92157deSopenharmony_ci## Matching the Result of a Function, Functor, or Callback 197f92157deSopenharmony_ci 198f92157deSopenharmony_ci| Matcher | Description | 199f92157deSopenharmony_ci| :--------------- | :------------------------------------------------ | 200f92157deSopenharmony_ci| `ResultOf(f, m)` | `f(argument)` matches matcher `m`, where `f` is a function or functor. | 201f92157deSopenharmony_ci| `ResultOf(result_description, f, m)` | The same as the two-parameter version, but provides a better error message. 202f92157deSopenharmony_ci 203f92157deSopenharmony_ci## Pointer Matchers 204f92157deSopenharmony_ci 205f92157deSopenharmony_ci| Matcher | Description | 206f92157deSopenharmony_ci| :------------------------ | :---------------------------------------------- | 207f92157deSopenharmony_ci| `Address(m)` | the result of `std::addressof(argument)` matches `m`. | 208f92157deSopenharmony_ci| `Pointee(m)` | `argument` (either a smart pointer or a raw pointer) points to a value that matches matcher `m`. | 209f92157deSopenharmony_ci| `Pointer(m)` | `argument` (either a smart pointer or a raw pointer) contains a pointer that matches `m`. `m` will match against the raw pointer regardless of the type of `argument`. | 210f92157deSopenharmony_ci| `WhenDynamicCastTo<T>(m)` | when `argument` is passed through `dynamic_cast<T>()`, it matches matcher `m`. | 211f92157deSopenharmony_ci 212f92157deSopenharmony_ci## Multi-argument Matchers {#MultiArgMatchers} 213f92157deSopenharmony_ci 214f92157deSopenharmony_ciTechnically, all matchers match a *single* value. A "multi-argument" matcher is 215f92157deSopenharmony_cijust one that matches a *tuple*. The following matchers can be used to match a 216f92157deSopenharmony_cituple `(x, y)`: 217f92157deSopenharmony_ci 218f92157deSopenharmony_ciMatcher | Description 219f92157deSopenharmony_ci:------ | :---------- 220f92157deSopenharmony_ci`Eq()` | `x == y` 221f92157deSopenharmony_ci`Ge()` | `x >= y` 222f92157deSopenharmony_ci`Gt()` | `x > y` 223f92157deSopenharmony_ci`Le()` | `x <= y` 224f92157deSopenharmony_ci`Lt()` | `x < y` 225f92157deSopenharmony_ci`Ne()` | `x != y` 226f92157deSopenharmony_ci 227f92157deSopenharmony_ciYou can use the following selectors to pick a subset of the arguments (or 228f92157deSopenharmony_cireorder them) to participate in the matching: 229f92157deSopenharmony_ci 230f92157deSopenharmony_ci| Matcher | Description | 231f92157deSopenharmony_ci| :------------------------- | :---------------------------------------------- | 232f92157deSopenharmony_ci| `AllArgs(m)` | Equivalent to `m`. Useful as syntactic sugar in `.With(AllArgs(m))`. | 233f92157deSopenharmony_ci| `Args<N1, N2, ..., Nk>(m)` | The tuple of the `k` selected (using 0-based indices) arguments matches `m`, e.g. `Args<1, 2>(Eq())`. | 234f92157deSopenharmony_ci 235f92157deSopenharmony_ci## Composite Matchers 236f92157deSopenharmony_ci 237f92157deSopenharmony_ciYou can make a matcher from one or more other matchers: 238f92157deSopenharmony_ci 239f92157deSopenharmony_ci| Matcher | Description | 240f92157deSopenharmony_ci| :------------------------------- | :-------------------------------------- | 241f92157deSopenharmony_ci| `AllOf(m1, m2, ..., mn)` | `argument` matches all of the matchers `m1` to `mn`. | 242f92157deSopenharmony_ci| `AllOfArray({m0, m1, ..., mn})`, `AllOfArray(a_container)`, `AllOfArray(begin, end)`, `AllOfArray(array)`, or `AllOfArray(array, count)` | The same as `AllOf()` except that the matchers come from an initializer list, STL-style container, iterator range, or C-style array. | 243f92157deSopenharmony_ci| `AnyOf(m1, m2, ..., mn)` | `argument` matches at least one of the matchers `m1` to `mn`. | 244f92157deSopenharmony_ci| `AnyOfArray({m0, m1, ..., mn})`, `AnyOfArray(a_container)`, `AnyOfArray(begin, end)`, `AnyOfArray(array)`, or `AnyOfArray(array, count)` | The same as `AnyOf()` except that the matchers come from an initializer list, STL-style container, iterator range, or C-style array. | 245f92157deSopenharmony_ci| `Not(m)` | `argument` doesn't match matcher `m`. | 246f92157deSopenharmony_ci| `Conditional(cond, m1, m2)` | Matches matcher `m1` if `cond` evaluates to true, else matches `m2`.| 247f92157deSopenharmony_ci 248f92157deSopenharmony_ci## Adapters for Matchers 249f92157deSopenharmony_ci 250f92157deSopenharmony_ci| Matcher | Description | 251f92157deSopenharmony_ci| :---------------------- | :------------------------------------ | 252f92157deSopenharmony_ci| `MatcherCast<T>(m)` | casts matcher `m` to type `Matcher<T>`. | 253f92157deSopenharmony_ci| `SafeMatcherCast<T>(m)` | [safely casts](../gmock_cook_book.md#SafeMatcherCast) matcher `m` to type `Matcher<T>`. | 254f92157deSopenharmony_ci| `Truly(predicate)` | `predicate(argument)` returns something considered by C++ to be true, where `predicate` is a function or functor. | 255f92157deSopenharmony_ci 256f92157deSopenharmony_ci`AddressSatisfies(callback)` and `Truly(callback)` take ownership of `callback`, 257f92157deSopenharmony_ciwhich must be a permanent callback. 258f92157deSopenharmony_ci 259f92157deSopenharmony_ci## Using Matchers as Predicates {#MatchersAsPredicatesCheat} 260f92157deSopenharmony_ci 261f92157deSopenharmony_ci| Matcher | Description | 262f92157deSopenharmony_ci| :---------------------------- | :------------------------------------------ | 263f92157deSopenharmony_ci| `Matches(m)(value)` | evaluates to `true` if `value` matches `m`. You can use `Matches(m)` alone as a unary functor. | 264f92157deSopenharmony_ci| `ExplainMatchResult(m, value, result_listener)` | evaluates to `true` if `value` matches `m`, explaining the result to `result_listener`. | 265f92157deSopenharmony_ci| `Value(value, m)` | evaluates to `true` if `value` matches `m`. | 266f92157deSopenharmony_ci 267f92157deSopenharmony_ci## Defining Matchers 268f92157deSopenharmony_ci 269f92157deSopenharmony_ci| Macro | Description | 270f92157deSopenharmony_ci| :----------------------------------- | :------------------------------------ | 271f92157deSopenharmony_ci| `MATCHER(IsEven, "") { return (arg % 2) == 0; }` | Defines a matcher `IsEven()` to match an even number. | 272f92157deSopenharmony_ci| `MATCHER_P(IsDivisibleBy, n, "") { *result_listener << "where the remainder is " << (arg % n); return (arg % n) == 0; }` | Defines a matcher `IsDivisibleBy(n)` to match a number divisible by `n`. | 273f92157deSopenharmony_ci| `MATCHER_P2(IsBetween, a, b, absl::StrCat(negation ? "isn't" : "is", " between ", PrintToString(a), " and ", PrintToString(b))) { return a <= arg && arg <= b; }` | Defines a matcher `IsBetween(a, b)` to match a value in the range [`a`, `b`]. | 274f92157deSopenharmony_ci 275f92157deSopenharmony_ci**Notes:** 276f92157deSopenharmony_ci 277f92157deSopenharmony_ci1. The `MATCHER*` macros cannot be used inside a function or class. 278f92157deSopenharmony_ci2. The matcher body must be *purely functional* (i.e. it cannot have any side 279f92157deSopenharmony_ci effect, and the result must not depend on anything other than the value 280f92157deSopenharmony_ci being matched and the matcher parameters). 281f92157deSopenharmony_ci3. You can use `PrintToString(x)` to convert a value `x` of any type to a 282f92157deSopenharmony_ci string. 283f92157deSopenharmony_ci4. You can use `ExplainMatchResult()` in a custom matcher to wrap another 284f92157deSopenharmony_ci matcher, for example: 285f92157deSopenharmony_ci 286f92157deSopenharmony_ci ```cpp 287f92157deSopenharmony_ci MATCHER_P(NestedPropertyMatches, matcher, "") { 288f92157deSopenharmony_ci return ExplainMatchResult(matcher, arg.nested().property(), result_listener); 289f92157deSopenharmony_ci } 290f92157deSopenharmony_ci ``` 291