diff options
Diffstat (limited to 'googlemock/docs/cheat_sheet.md')
-rw-r--r-- | googlemock/docs/cheat_sheet.md | 239 |
1 files changed, 150 insertions, 89 deletions
diff --git a/googlemock/docs/cheat_sheet.md b/googlemock/docs/cheat_sheet.md index ea46a68c..6441a7a8 100644 --- a/googlemock/docs/cheat_sheet.md +++ b/googlemock/docs/cheat_sheet.md @@ -1,6 +1,8 @@ ## gMock Cheat Sheet -<!-- GOOGLETEST_CM0018 DO NOT DELETE --> +<!-- GOOGLETEST_CM0019 DO NOT DELETE --> + +<!-- GOOGLETEST_CM0033 DO NOT DELETE --> ### Defining a Mock Class @@ -215,7 +217,7 @@ and the default action will be taken each time. ### Matchers {#MatcherList} -<!-- GOOGLETEST_CM0019 DO NOT DELETE --> +<!-- GOOGLETEST_CM0020 DO NOT DELETE --> A **matcher** matches a *single* argument. You can use it inside `ON_CALL()` or `EXPECT_CALL()`, or use it to validate a value directly: @@ -232,7 +234,7 @@ A **matcher** matches a *single* argument. You can use it inside `ON_CALL()` or Built-in matchers (where `argument` is the function argument) are divided into several categories: -## Wildcard +#### Wildcard Matcher | Description :-------------------------- | :----------------------------------------------- @@ -340,67 +342,99 @@ or simply `expected_container` to match a container exactly. If you want to write the elements in-line, match them more flexibly, or get more informative messages, you can use: -| Matcher | Description | -| :----------------------------------- | :----------------------------------- | -| `ContainerEq(container)` | The same as `Eq(container)` except | -: : that the failure message also : -: : includes which elements are in one : -: : container but not the other. : -| `Contains(e)` | `argument` contains an element that | -: : matches `e`, which can be either a : -: : value or a matcher. : -| `Each(e)` | `argument` is a container where | -: : _every_ element matches `e`, which : -: : can be either a value or a matcher. : -| `ElementsAre(e0, e1, ..., en)` | `argument` has `n + 1` elements, | -: : where the i-th element matches `ei`, : -: : which can be a value or a matcher. 0 : -: : to 10 arguments are allowed. : -| `ElementsAreArray({ e0, e1, ..., en | The same as `ElementsAre()` except | -: })`, `ElementsAreArray(array)`, or : that the expected element : -: `ElementsAreArray(array, count)` : values/matchers come from an : -: : initializer list, STL-style : -: : container, or C-style array. : -| `IsEmpty()` | `argument` is an empty container | -: : (`container.empty()`). : -| `Pointwise(m, container)` | `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. : -| `SizeIs(m)` | `argument` is a container whose size | -: : matches `m`. E.g. `SizeIs(2)` or : -: : `SizeIs(Lt(2))`. : -| `UnorderedElementsAre(e0, e1, ..., | `argument` has `n + 1` elements, and | -: en)` : under some permutation each element : -: : matches an `ei` (for a different : -: : `i`), which can be a value or a : -: : matcher. 0 to 10 arguments are : -: : allowed. : -| `UnorderedElementsAreArray({ e0, e1, | The same as `UnorderedElementsAre()` | -: ..., en })`, : except that the expected element : -: `UnorderedElementsAreArray(array)`, : values/matchers come from an : -: or `UnorderedElementsAreArray(array, : initializer list, STL-style : -: count)` : container, or C-style array. : -| `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. : -| `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<int>(), : -: : ElementsAre(3, 2, 1))`. : +| Matcher | Description | +| :---------------------------------------- | :------------------------------- | +| `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. : +| `ContainerEq(container)` | The same as `Eq(container)` | +: : except that the failure message : +: : also includes which elements are : +: : in one container but not the : +: : other. : +| `Contains(e)` | `argument` contains an element | +: : that matches `e`, which can be : +: : either a value or a matcher. : +| `Each(e)` | `argument` is a container where | +: : *every* element matches `e`, : +: : which can be either a value or a : +: : matcher. : +| `ElementsAre(e0, e1, ..., en)` | `argument` has `n + 1` elements, | +: : where the *i*-th element matches : +: : `ei`, which can be a value or a : +: : matcher. : +| `ElementsAreArray({e0, e1, ..., en})`, | The same as `ElementsAre()` | +: `ElementsAreArray(a_container)`, : except that the expected element : +: `ElementsAreArray(begin, end)`, : values/matchers come from an : +: `ElementsAreArray(array)`, or : initializer list, STL-style : +: `ElementsAreArray(array, count)` : container, iterator range, or : +: : C-style array. : +| `IsEmpty()` | `argument` is an empty container | +: : (`container.empty()`). : +| `IsFalse()` | `argument` evaluates to `false` | +: : in a Boolean context. : +| `IsSubsetOf({e0, e1, ..., en})`, | `argument` matches | +: `IsSubsetOf(a_container)`, : `UnorderedElementsAre(x0, x1, : +: `IsSubsetOf(begin, end)`, : ..., xk)` for some subset `{x0, : +: `IsSubsetOf(array)`, or : x1, ..., xk}` of the expected : +: `IsSubsetOf(array, count)` : matchers. : +| `IsSupersetOf({e0, e1, ..., en})`, | Some subset of `argument` | +: `IsSupersetOf(a_container)`, : matches : +: `IsSupersetOf(begin, end)`, : `UnorderedElementsAre(`expected : +: `IsSupersetOf(array)`, or : matchers`)`. : +: `IsSupersetOf(array, count)` : : +| `IsTrue()` | `argument` evaluates to `true` | +: : in a Boolean context. : +| `Pointwise(m, container)`, `Pointwise(m, | `argument` contains the same | +: {e0, e1, ..., en})` : 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. : +| `SizeIs(m)` | `argument` is a container whose | +: : size matches `m`. E.g. : +: : `SizeIs(2)` or `SizeIs(Lt(2))`. : +| `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. : +| `UnorderedElementsAreArray({e0, e1, ..., | The same as | +: en})`, : `UnorderedElementsAre()` except : +: `UnorderedElementsAreArray(a_container)`, : that the expected element : +: `UnorderedElementsAreArray(begin, end)`, : values/matchers come from an : +: `UnorderedElementsAreArray(array)`, or : initializer list, STL-style : +: `UnorderedElementsAreArray(array, count)` : container, iterator range, or : +: : C-style array. : +| `UnorderedPointwise(m, container)`, | Like `Pointwise(m, container)`, | +: `UnorderedPointwise(m, {e0, e1, ..., : but ignores the order of : +: en})` : elements. : +| `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. : +| `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))`. : **Notes:** @@ -463,6 +497,10 @@ messages, you can use: | `WhenDynamicCastTo<T>(m)` | when `argument` is passed through | : : `dynamic_cast<T>()`, it matches matcher `m`. : +<!-- GOOGLETEST_CM0026 DO NOT DELETE --> + +<!-- GOOGLETEST_CM0027 DO NOT DELETE --> + #### Multi-argument Matchers {#MultiArgMatchers} Technically, all matchers match a *single* value. A "multi-argument" matcher is @@ -493,13 +531,25 @@ reorder them) to participate in the matching: You can make a matcher from one or more other matchers: -| Matcher | Description | -| :----------------------- | :---------------------------------------------- | -| `AllOf(m1, m2, ..., mn)` | `argument` matches all of the matchers `m1` to | -: : `mn`. : -| `AnyOf(m1, m2, ..., mn)` | `argument` matches at least one of the matchers | -: : `m1` to `mn`. : -| `Not(m)` | `argument` doesn't match matcher `m`. | +| Matcher | Description | +| :------------------------------- | :-------------------------------------- | +| `AllOf(m1, m2, ..., mn)` | `argument` matches all of the matchers | +: : `m1` to `mn`. : +| `AllOfArray({m0, m1, ..., mn})`, | The same as `AllOf()` except that the | +: `AllOfArray(a_container)`, : matchers come from an initializer list, : +: `AllOfArray(begin, end)`, : STL-style container, iterator range, or : +: `AllOfArray(array)`, or : C-style array. : +: `AllOfArray(array, count)` : : +| `AnyOf(m1, m2, ..., mn)` | `argument` matches at least one of the | +: : matchers `m1` to `mn`. : +| `AnyOfArray({m0, m1, ..., mn})`, | The same as `AnyOf()` except that the | +: `AnyOfArray(a_container)`, : matchers come from an initializer list, : +: `AnyOfArray(begin, end)`, : STL-style container, iterator range, or : +: `AnyOfArray(array)`, or : C-style array. : +: `AnyOfArray(array, count)` : : +| `Not(m)` | `argument` doesn't match matcher `m`. | + +<!-- GOOGLETEST_CM0028 DO NOT DELETE --> #### Adapters for Matchers @@ -518,7 +568,7 @@ You can make a matcher from one or more other matchers: `AddressSatisfies(callback)` and `Truly(callback)` take ownership of `callback`, which must be a permanent callback. -#### Matchers as Predicates {#MatchersAsPredicatesCheat} +#### Using Matchers as Predicates {#MatchersAsPredicatesCheat} | Matcher | Description | | :---------------------------- | :------------------------------------------ | @@ -555,20 +605,13 @@ which must be a permanent callback. 1. You can use `PrintToString(x)` to convert a value `x` of any type to a string. -## Matchers as Test Assertions - -Matcher | Description -:--------------------------- | :---------- -`ASSERT_THAT(expression, m)` | Generates a [fatal failure](../../googletest/docs/primer.md#assertions) if the value of `expression` doesn't match matcher `m`. -`EXPECT_THAT(expression, m)` | Generates a non-fatal failure if the value of `expression` doesn't match matcher `m`. - ### Actions {#ActionList} **Actions** specify what a mock function should do when invoked. #### Returning a Value -| Matcher | Description | +| | | | :-------------------------- | :-------------------------------------------- | | `Return()` | Return from a `void` mock function. | | `Return(value)` | Return `value`. If the type of `value` is | @@ -587,7 +630,7 @@ Matcher | Description #### Side Effects -| Matcher | Description | +| | | | :--------------------------------- | :-------------------------------------- | | `Assign(&variable, value)` | Assign `value` to variable. | | `DeleteArg<N>()` | Delete the `N`-th (0-based) argument, | @@ -620,8 +663,11 @@ Matcher | Description In the following, by "callable" we mean a free function, `std::function`, functor, lambda, or `google3`-style permanent callback. -| Matcher | Description | +| | | | :---------------------------------- | :------------------------------------- | +| `f` | Invoke f with the arguments passed to | +: : the mock function, where f is a : +: : callable (except of google3 callback). : | `Invoke(f)` | Invoke `f` with the arguments passed | : : to the mock function, where `f` can be : : : a global/static function or a functor. : @@ -676,7 +722,7 @@ InvokeArgument<2>(5, string("Hi"), ByRef(foo)) calls the mock function's #2 argument, passing to it `5` and `string("Hi")` by value, and `foo` by reference. -## Default Action +#### Default Action | Matcher | Description | | :------------ | :----------------------------------------------------- | @@ -686,9 +732,11 @@ value, and `foo` by reference. **Note:** due to technical reasons, `DoDefault()` cannot be used inside a composite action - trying to do so will result in a run-time error. -## Composite Actions +<!-- GOOGLETEST_CM0032 DO NOT DELETE --> -| Matcher | Description | +#### Composite Actions + +| | | | :----------------------------- | :------------------------------------------ | | `DoAll(a1, a2, ..., an)` | Do all actions `a1` to `an` and return the | : : result of `an` in each invocation. The : @@ -702,9 +750,22 @@ composite action - trying to do so will result in a run-time error. : : it. : | `WithoutArgs(a)` | Perform action `a` without any arguments. | -## Defining Actions - -| Matcher | Description | +#### Defining Actions + +<table border="1" cellspacing="0" cellpadding="1"> + <tr> + <td>`struct SumAction {` <br> +  `template <typename T>` <br> +  `T operator()(T x, Ty) { return x + y; }` <br> + `};` + </td> + <td> Defines a generic functor that can be used as an action summing its + arguments. </td> </tr> + <tr> + </tr> +</table> + +| | | | :--------------------------------- | :-------------------------------------- | | `ACTION(Sum) { return arg0 + arg1; | Defines an action `Sum()` to return the | : }` : sum of the mock function's argument #0 : @@ -723,7 +784,7 @@ The `ACTION*` macros cannot be used inside a function or class. These are used in `Times()` to specify how many times a mock function will be called: -| Matcher | Description | +| | | | :---------------- | :----------------------------------------------------- | | `AnyNumber()` | The function can be called any number of times. | | `AtLeast(n)` | The call is expected at least `n` times. | |