You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
6.7 KiB

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