@@ -3812,35 +3812,74 @@ Cardinality EvenNumber() {
38123812 .Times(EvenNumber());
38133813```
38143814
3815- ### Writing New Actions Quickly {#QuickNewActions}
3815+ ### Writing New Actions {#QuickNewActions}
38163816
38173817If the built-in actions don't work for you, you can easily define your own one.
3818- Just define a functor class with a (possibly templated) call operator, matching
3819- the signature of your action.
3818+ All you need is a call operator with a signature compatible with the mocked
3819+ function. So you can use a lambda:
38203820
3821- ```cpp
3822- struct Increment {
3823- template <typename T>
3824- T operator()(T* arg) {
3825- return ++(*arg);
3826- }
3827- }
3821+ ```
3822+ MockFunction<int(int)> mock;
3823+ EXPECT_CALL(mock, Call).WillOnce([](const int input) { return input * 7; });
3824+ EXPECT_EQ(14, mock.AsStdFunction()(2));
38283825```
38293826
3830- The same approach works with stateful functors (or any callable, really ):
3827+ Or a struct with a call operator (even a templated one ):
38313828
38323829```
38333830struct MultiplyBy {
38343831 template <typename T>
38353832 T operator()(T arg) { return arg * multiplier; }
38363833
38373834 int multiplier;
3838- }
3835+ };
38393836
38403837// Then use:
38413838// EXPECT_CALL(...).WillOnce(MultiplyBy{7});
38423839```
38433840
3841+ It's also fine for the callable to take no arguments, ignoring the arguments
3842+ supplied to the mock function:
3843+
3844+ ```
3845+ MockFunction<int(int)> mock;
3846+ EXPECT_CALL(mock, Call).WillOnce([] { return 17; });
3847+ EXPECT_EQ(17, mock.AsStdFunction()(0));
3848+ ```
3849+
3850+ When used with `WillOnce`, the callable can assume it will be called at most
3851+ once and is allowed to be a move-only type:
3852+
3853+ ```
3854+ // An action that contains move-only types and has an &&-qualified operator,
3855+ // demanding in the type system that it be called at most once. This can be
3856+ // used with WillOnce, but the compiler will reject it if handed to
3857+ // WillRepeatedly.
3858+ struct MoveOnlyAction {
3859+ std::unique_ptr<int> move_only_state;
3860+ std::unique_ptr<int> operator()() && { return std::move(move_only_state); }
3861+ };
3862+
3863+ MockFunction<std::unique_ptr<int>()> mock;
3864+ EXPECT_CALL(mock, Call).WillOnce(MoveOnlyAction{std::make_unique<int>(17)});
3865+ EXPECT_THAT(mock.AsStdFunction()(), Pointee(Eq(17)));
3866+ ```
3867+
3868+ More generally, to use with a mock function whose signature is `R(Args...)` the
3869+ object can be anything convertible to `OnceAction<R(Args...)>` or
3870+ `Action<R(Args...)`>. The difference between the two is that `OnceAction` has
3871+ weaker requirements (`Action` requires a copy-constructible input that can be
3872+ called repeatedly whereas `OnceAction` requires only move-constructible and
3873+ supports `&&`-qualified call operators), but can be used only with `WillOnce`.
3874+ `OnceAction` is typically relevant only when supporting move-only types or
3875+ actions that want a type-system guarantee that they will be called at most once.
3876+
3877+ Typically the `OnceAction` and `Action` templates need not be referenced
3878+ directly in your actions: a struct or class with a call operator is sufficient,
3879+ as in the examples above. But fancier polymorphic actions that need to know the
3880+ specific return type of the mock function can define templated conversion
3881+ operators to make that possible. See `gmock-actions.h` for examples.
3882+
38443883#### Legacy macro-based Actions
38453884
38463885Before C++11, the functor-based actions were not supported; the old way of
0 commit comments