From fc1e7788993bd1418f80d64eb430bd9ac023db68 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Mon, 17 Aug 2020 12:07:02 -0400 Subject: Googletest export Fix DoAll to work with move-only sink arguments. This changes types of the first n - 1 actions so that they only get a readonly view of the arguments. The last action will accept move only objects. PiperOrigin-RevId: 327031893 --- googlemock/docs/cheat_sheet.md | 2 +- googlemock/include/gmock/gmock-actions.h | 23 ++++++++++++--------- googlemock/test/gmock-generated-actions_test.cc | 27 +++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 10 deletions(-) diff --git a/googlemock/docs/cheat_sheet.md b/googlemock/docs/cheat_sheet.md index 2500260..cc7e699 100644 --- a/googlemock/docs/cheat_sheet.md +++ b/googlemock/docs/cheat_sheet.md @@ -618,7 +618,7 @@ composite action - trying to do so will result in a run-time error. | | | | :----------------------------- | :------------------------------------------ | -| `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. | +| `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. | | `IgnoreResult(a)` | Perform action `a` and ignore its result. `a` must not return void. | | `WithArg(a)` | Pass the `N`-th (0-based) argument of the mock function to action `a` and perform it. | | `WithArgs(a)` | Pass the selected (0-based) arguments of the mock function to action `a` and perform it. | diff --git a/googlemock/include/gmock/gmock-actions.h b/googlemock/include/gmock/gmock-actions.h index 79054db..3aba9ec 100644 --- a/googlemock/include/gmock/gmock-actions.h +++ b/googlemock/include/gmock/gmock-actions.h @@ -1032,9 +1032,13 @@ struct WithArgsAction { template struct DoAllAction { private: - template - std::vector> Convert(IndexSequence) const { - return {std::get(actions)...}; + template + using NonFinalType = + typename std::conditional::value, T, const T&>::type; + + template + std::vector Convert(IndexSequence) const { + return {ActionT(std::get(actions))...}; } public: @@ -1043,17 +1047,17 @@ struct DoAllAction { template operator Action() const { // NOLINT struct Op { - std::vector> converted; + std::vector...)>> converted; Action last; R operator()(Args... args) const { - auto tuple_args = std::forward_as_tuple(std::forward(args)...); for (auto& a : converted) { - a.Perform(tuple_args); + a.Perform(std::forward_as_tuple(std::forward(args)...)); } - return last.Perform(tuple_args); + return last.Perform(std::forward_as_tuple(std::forward(args)...)); } }; - return Op{Convert(MakeIndexSequence()), + return Op{Convert...)>>( + MakeIndexSequence()), std::get(actions)}; } }; @@ -1093,7 +1097,8 @@ struct DoAllAction { typedef internal::IgnoredValue Unused; // Creates an action that does actions a1, a2, ..., sequentially in -// each invocation. +// each invocation. All but the last action will have a readonly view of the +// arguments. template internal::DoAllAction::type...> DoAll( Action&&... action) { diff --git a/googlemock/test/gmock-generated-actions_test.cc b/googlemock/test/gmock-generated-actions_test.cc index ef39dff..6490616 100644 --- a/googlemock/test/gmock-generated-actions_test.cc +++ b/googlemock/test/gmock-generated-actions_test.cc @@ -422,6 +422,33 @@ TEST(DoAllTest, TenActions) { EXPECT_EQ('g', g); } +TEST(DoAllTest, NoArgs) { + bool ran_first = false; + Action a = + DoAll([&] { ran_first = true; }, [&] { return ran_first; }); + EXPECT_TRUE(a.Perform({})); +} + +TEST(DoAllTest, MoveOnlyArgs) { + bool ran_first = false; + Action)> a = + DoAll(InvokeWithoutArgs([&] { ran_first = true; }), + [](std::unique_ptr p) { return *p; }); + EXPECT_EQ(7, a.Perform(std::make_tuple(std::unique_ptr(new int(7))))); + EXPECT_TRUE(ran_first); +} + +TEST(DoAllTest, ImplicitlyConvertsActionArguments) { + bool ran_first = false; + // Action)> isn't an + // Action&) but can be converted. + Action)> first = [&] { ran_first = true; }; + Action)> a = + DoAll(first, [](std::vector arg) { return arg.front(); }); + EXPECT_EQ(7, a.Perform(std::make_tuple(std::vector{7}))); + EXPECT_TRUE(ran_first); +} + // The ACTION*() macros trigger warning C4100 (unreferenced formal // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in // the macro definition, as the warnings are generated when the macro -- cgit v0.12