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