xref: /freebsd/contrib/googletest/docs/reference/actions.md (revision 46333229c6a0187ebf231805682ee0bceed704d1)
1# Actions Reference
2
3[**Actions**](../gmock_for_dummies.md#actions-what-should-it-do) specify what a
4mock function should do when invoked. This page lists the built-in actions
5provided by GoogleTest. All actions are defined in the `::testing` namespace.
6
7## Returning a Value
8
9| Action                            | Description                                   |
10| :-------------------------------- | :-------------------------------------------- |
11| `Return()`                        | Return from a `void` mock function.           |
12| `Return(value)`                   | Return `value`. If the type of `value` is     different to the mock function's return type, `value` is converted to the latter type <i>at the time the expectation is set</i>, not when the action is executed. |
13| `ReturnArg<N>()`                  | Return the `N`-th (0-based) argument.         |
14| `ReturnNew<T>(a1, ..., ak)`       | Return `new T(a1, ..., ak)`; a different      object is created each time. |
15| `ReturnNull()`                    | Return a null pointer.                        |
16| `ReturnPointee(ptr)`              | Return the value pointed to by `ptr`.         |
17| `ReturnRef(variable)`             | Return a reference to `variable`.             |
18| `ReturnRefOfCopy(value)`          | Return a reference to a copy of `value`; the  copy lives as long as the action. |
19| `ReturnRoundRobin({a1, ..., ak})` | Each call will return the next `ai` in the list, starting at the beginning when the end of the list is reached. |
20
21## Side Effects
22
23| Action                             | Description                             |
24| :--------------------------------- | :-------------------------------------- |
25| `Assign(&variable, value)` | Assign `value` to variable. |
26| `DeleteArg<N>()` | Delete the `N`-th (0-based) argument, which must be a pointer. |
27| `SaveArg<N>(pointer)` | Save the `N`-th (0-based) argument to `*pointer` by copy-assignment. |
28| `SaveArgByMove<N>(pointer)` | Save the `N`-th (0-based) argument to `*pointer` by move-assignment. |
29| `SaveArgPointee<N>(pointer)` | Save the value pointed to by the `N`-th (0-based) argument to `*pointer`. |
30| `SetArgReferee<N>(value)` | Assign `value` to the variable referenced by the `N`-th (0-based) argument. |
31| `SetArgPointee<N>(value)` | Assign `value` to the variable pointed by the `N`-th (0-based) argument. |
32| `SetArgumentPointee<N>(value)` | Same as `SetArgPointee<N>(value)`. Deprecated. Will be removed in v1.7.0. |
33| `SetArrayArgument<N>(first, last)` | Copies the elements in source range [`first`, `last`) to the array pointed to by the `N`-th (0-based) argument, which can be either a pointer or an iterator. The action does not take ownership of the elements in the source range. |
34| `SetErrnoAndReturn(error, value)` | Set `errno` to `error` and return `value`. |
35| `Throw(exception)` | Throws the given exception, which can be any copyable value. Available since v1.1.0. |
36
37## Using a Function, Functor, or Lambda as an Action
38
39In the following, by "callable" we mean a free function, `std::function`,
40functor, or lambda.
41
42| Action                              | Description                            |
43| :---------------------------------- | :------------------------------------- |
44| `f` | Invoke `f` with the arguments passed to the mock function, where `f` is a callable. |
45| `Invoke(f)` | Invoke `f` with the arguments passed to the mock function, where `f` can be a global/static function or a functor. |
46| `Invoke(object_pointer, &class::method)` | Invoke the method on the object with the arguments passed to the mock function. |
47| `InvokeWithoutArgs(f)` | Invoke `f`, which can be a global/static function or a functor. `f` must take no arguments. |
48| `InvokeWithoutArgs(object_pointer, &class::method)` | Invoke the method on the object, which takes no arguments. |
49| `InvokeArgument<N>(arg1, arg2, ..., argk)` | Invoke the mock function's `N`-th (0-based) argument, which must be a function or a functor, with the `k` arguments. |
50
51The return value of the invoked function is used as the return value of the
52action.
53
54When defining a callable to be used with `Invoke*()`, you can declare any unused
55parameters as `Unused`:
56
57```cpp
58using ::testing::Invoke;
59double Distance(Unused, double x, double y) { return sqrt(x*x + y*y); }
60...
61EXPECT_CALL(mock, Foo("Hi", _, _)).WillOnce(Invoke(Distance));
62```
63
64`Invoke(callback)` and `InvokeWithoutArgs(callback)` take ownership of
65`callback`, which must be permanent. The type of `callback` must be a base
66callback type instead of a derived one, e.g.
67
68```cpp
69  BlockingClosure* done = new BlockingClosure;
70  ... Invoke(done) ...;  // This won't compile!
71
72  Closure* done2 = new BlockingClosure;
73  ... Invoke(done2) ...;  // This works.
74```
75
76In `InvokeArgument<N>(...)`, if an argument needs to be passed by reference,
77wrap it inside `std::ref()`. For example,
78
79```cpp
80using ::testing::InvokeArgument;
81...
82InvokeArgument<2>(5, string("Hi"), std::ref(foo))
83```
84
85calls the mock function's #2 argument, passing to it `5` and `string("Hi")` by
86value, and `foo` by reference.
87
88## Default Action
89
90| Action        | Description                                            |
91| :------------ | :----------------------------------------------------- |
92| `DoDefault()` | Do the default action (specified by `ON_CALL()` or the built-in one). |
93
94{: .callout .note}
95**Note:** due to technical reasons, `DoDefault()` cannot be used inside a
96composite action - trying to do so will result in a run-time error.
97
98## Composite Actions
99
100| Action                         | Description                                 |
101| :----------------------------- | :------------------------------------------ |
102| `DoAll(a1, a2, ..., an)`       | Do all actions `a1` to `an` and return the result of `an` in each invocation. The first `n - 1` sub-actions must return void and will receive a  readonly view of the arguments. |
103| `IgnoreResult(a)`              | Perform action `a` and ignore its result. `a` must not return void. |
104| `WithArg<N>(a)`                | Pass the `N`-th (0-based) argument of the mock function to action `a` and perform it. |
105| `WithArgs<N1, N2, ..., Nk>(a)` | Pass the selected (0-based) arguments of the mock function to action `a` and perform it. |
106| `WithoutArgs(a)`               | Perform action `a` without any arguments. |
107
108## Defining Actions
109
110| Macro                              | Description                             |
111| :--------------------------------- | :-------------------------------------- |
112| `ACTION(Sum) { return arg0 + arg1; }` | Defines an action `Sum()` to return the sum of the mock function's argument #0 and #1. |
113| `ACTION_P(Plus, n) { return arg0 + n; }` | Defines an action `Plus(n)` to return the sum of the mock function's argument #0 and `n`. |
114| `ACTION_Pk(Foo, p1, ..., pk) { statements; }` | Defines a parameterized action `Foo(p1, ..., pk)` to execute the given `statements`. |
115
116The `ACTION*` macros cannot be used inside a function or class.
117