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