diff --git a/docs/reference/actions.md b/docs/reference/actions.md index ab81a129..0ebdc1e8 100644 --- a/docs/reference/actions.md +++ b/docs/reference/actions.md @@ -24,7 +24,8 @@ provided by GoogleTest. All actions are defined in the `::testing` namespace. | :--------------------------------- | :-------------------------------------- | | `Assign(&variable, value)` | Assign `value` to variable. | | `DeleteArg()` | Delete the `N`-th (0-based) argument, which must be a pointer. | -| `SaveArg(pointer)` | Save the `N`-th (0-based) argument to `*pointer`. | +| `SaveArg(pointer)` | Save the `N`-th (0-based) argument to `*pointer` by copy-assignment. | +| `SaveArgByMove(pointer)` | Save the `N`-th (0-based) argument to `*pointer` by move-assignment. | | `SaveArgPointee(pointer)` | Save the value pointed to by the `N`-th (0-based) argument to `*pointer`. | | `SetArgReferee(value)` | Assign `value` to the variable referenced by the `N`-th (0-based) argument. | | `SetArgPointee(value)` | Assign `value` to the variable pointed by the `N`-th (0-based) argument. | diff --git a/googlemock/include/gmock/gmock-actions.h b/googlemock/include/gmock/gmock-actions.h index aa470799..84d673f1 100644 --- a/googlemock/include/gmock/gmock-actions.h +++ b/googlemock/include/gmock/gmock-actions.h @@ -1720,6 +1720,16 @@ struct SaveArgAction { } }; +template +struct SaveArgByMoveAction { + Ptr pointer; + + template + void operator()(Args&&... args) const { + *pointer = std::move(std::get(std::tie(args...))); + } +}; + template struct SaveArgPointeeAction { Ptr pointer; @@ -2070,6 +2080,13 @@ internal::SaveArgAction SaveArg(Ptr pointer) { return {pointer}; } +// Action SaveArgByMove(pointer) moves the k-th (0-based) argument of the +// mock function into *pointer. +template +internal::SaveArgByMoveAction SaveArgByMove(Ptr pointer) { + return {pointer}; +} + // Action SaveArgPointee(pointer) saves the value pointed to // by the k-th (0-based) argument of the mock function to *pointer. template diff --git a/googlemock/test/gmock-more-actions_test.cc b/googlemock/test/gmock-more-actions_test.cc index 354a79b1..5b8b566f 100644 --- a/googlemock/test/gmock-more-actions_test.cc +++ b/googlemock/test/gmock-more-actions_test.cc @@ -59,6 +59,7 @@ using testing::Invoke; using testing::ReturnArg; using testing::ReturnPointee; using testing::SaveArg; +using testing::SaveArgByMove; using testing::SaveArgPointee; using testing::SetArgReferee; using testing::Unused; @@ -492,6 +493,34 @@ TEST(SaveArgActionTest, WorksForCompatibleType) { EXPECT_EQ('a', result); } +struct MoveOnly { + explicit MoveOnly(int v) : i(v) {} + MoveOnly(MoveOnly&& o) { + i = o.i; + o.i = -1; + } + MoveOnly& operator=(MoveOnly&& o) { + i = o.i; + o.i = -1; + return *this; + } + int i; +}; + +TEST(SaveArgByMoveActionTest, WorksForSameType) { + MoveOnly result{0}; + const Action a1 = SaveArgByMove<0>(&result); + a1.Perform(std::make_tuple(MoveOnly{5})); + EXPECT_EQ(5, result.i); +} + +TEST(SaveArgByMoveActionTest, WorksForCompatibleType) { + MoveOnly result{0}; + const Action a1 = SaveArgByMove<1>(&result); + a1.Perform(std::make_tuple(true, MoveOnly{7})); + EXPECT_EQ(7, result.i); +} + TEST(SaveArgPointeeActionTest, WorksForSameType) { int result = 0; const int value = 5; @@ -756,34 +785,34 @@ TEST(InvokeArgumentTest, Functor6) { // Tests using InvokeArgument with a 7-ary function. TEST(InvokeArgumentTest, Function7) { - Action + Action a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7"); EXPECT_EQ("1234567", a.Perform(std::make_tuple(&Concat7))); } // Tests using InvokeArgument with a 8-ary function. TEST(InvokeArgumentTest, Function8) { - Action + Action a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8"); EXPECT_EQ("12345678", a.Perform(std::make_tuple(&Concat8))); } // Tests using InvokeArgument with a 9-ary function. TEST(InvokeArgumentTest, Function9) { - Action + Action a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9"); EXPECT_EQ("123456789", a.Perform(std::make_tuple(&Concat9))); } // Tests using InvokeArgument with a 10-ary function. TEST(InvokeArgumentTest, Function10) { - Action a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9", "0");