Skip to content

Commit

Permalink
Accept one-shot callables in InvokeArgument.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 611467660
Change-Id: Ic89ffc986141bee61f835cb60088aee92eb8bad9
  • Loading branch information
Abseil Team authored and copybara-github committed Feb 29, 2024
1 parent e15c5a5 commit e4fdb87
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
5 changes: 3 additions & 2 deletions googlemock/include/gmock/gmock-more-actions.h
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,9 @@ namespace internal {
// Overloads for other custom-callables are provided in the
// internal/custom/gmock-generated-actions.h header.
template <typename F, typename... Args>
auto InvokeArgument(F f, Args... args) -> decltype(f(args...)) {
return f(args...);
auto InvokeArgument(F &&f,
Args... args) -> decltype(std::forward<F>(f)(args...)) {
return std::forward<F>(f)(args...);
}

template <std::size_t index, typename... Params>
Expand Down
10 changes: 10 additions & 0 deletions googlemock/test/gmock-more-actions_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ struct UnaryMoveOnlyFunctor : UnaryFunctor {
UnaryMoveOnlyFunctor(UnaryMoveOnlyFunctor&&) = default;
};

struct OneShotUnaryFunctor {
int operator()(bool x) && { return x ? 1 : -1; }
};

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 @@ -716,6 +720,12 @@ TEST(InvokeArgumentTest, Functor1MoveOnly) {
EXPECT_EQ(1, a.Perform(std::make_tuple(UnaryMoveOnlyFunctor())));
}

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

// 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 e4fdb87

Please sign in to comment.