Skip to content

Commit

Permalink
Accept move-only callables in InvokeArguments
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 594223533
Change-Id: I491fae7d851d4e0df07fb3627416949071fec8d6
  • Loading branch information
Abseil Team authored and copybara-github committed Dec 28, 2023
1 parent 96eadf6 commit dddb219
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion googlemock/include/gmock/gmock-more-actions.h
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ struct InvokeArgumentAction {
internal::FlatTuple<Args &&...> args_tuple(FlatTupleConstructTag{},
std::forward<Args>(args)...);
return params.Apply([&](const Params &...unpacked_params) {
auto &&callable = args_tuple.template Get<index>();
auto &&callable = std::move(args_tuple.template Get<index>());
return internal::InvokeArgument(
std::forward<decltype(callable)>(callable), unpacked_params...);
});
Expand Down
14 changes: 13 additions & 1 deletion googlemock/test/gmock-more-actions_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ struct UnaryFunctor {
int operator()(bool x) { return x ? 1 : -1; }
};

struct UnaryMoveOnlyFunctor : UnaryFunctor {
UnaryMoveOnlyFunctor() = default;
UnaryMoveOnlyFunctor(const UnaryMoveOnlyFunctor&) = delete;
UnaryMoveOnlyFunctor(UnaryMoveOnlyFunctor&&) = default;
};

const char* Binary(const char* input, short n) { return input + n; } // NOLINT

int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT
Expand Down Expand Up @@ -698,12 +704,18 @@ TEST(InvokeArgumentTest, Function0) {
EXPECT_EQ(1, a.Perform(std::make_tuple(2, &Nullary)));
}

// Tests using InvokeArgument with a unary function.
// Tests using InvokeArgument with a unary functor.
TEST(InvokeArgumentTest, Functor1) {
Action<int(UnaryFunctor)> a = InvokeArgument<0>(true); // NOLINT
EXPECT_EQ(1, a.Perform(std::make_tuple(UnaryFunctor())));
}

// Tests using InvokeArgument with a unary move-only functor.
TEST(InvokeArgumentTest, Functor1MoveOnly) {
Action<int(UnaryMoveOnlyFunctor)> a = InvokeArgument<0>(true); // NOLINT
EXPECT_EQ(1, a.Perform(std::make_tuple(UnaryMoveOnlyFunctor())));
}

// Tests using InvokeArgument with a 5-ary function.
TEST(InvokeArgumentTest, Function5) {
Action<int(int (*)(int, int, int, int, int))> a = // NOLINT
Expand Down

0 comments on commit dddb219

Please sign in to comment.