Accept one-shot callables in InvokeArgument.

PiperOrigin-RevId: 611467660
Change-Id: Ic89ffc986141bee61f835cb60088aee92eb8bad9
This commit is contained in:
Abseil Team 2024-02-29 07:41:40 -08:00 committed by Copybara-Service
parent e15c5a51b8
commit e4fdb87e76
2 changed files with 13 additions and 2 deletions

View File

@ -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>

View File

@ -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
@ -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