gmock-actions: add a missing conversion operator to WithArgsAction.

PiperOrigin-RevId: 745802500
Change-Id: I8cf7b69f89af8615e9c405b1e6552d3e12ebf93d
This commit is contained in:
Aaron Jacobs 2025-04-09 17:56:54 -07:00 committed by Copybara-Service
parent 50a9511f50
commit e90fe24856
2 changed files with 40 additions and 0 deletions

View file

@ -1455,6 +1455,30 @@ struct WithArgsAction {
return OA{std::move(inner_action)};
}
// As above, but in the case where we want to create a OnceAction from a const
// WithArgsAction. This is fine as long as the inner action doesn't need to
// move any of its state to create a OnceAction.
template <
typename R, typename... Args,
typename std::enable_if<
std::is_convertible<const InnerAction&,
OnceAction<R(internal::TupleElement<
I, std::tuple<Args...>>...)>>::value,
int>::type = 0>
operator OnceAction<R(Args...)>() const& { // NOLINT
struct OA {
OnceAction<InnerSignature<R, Args...>> inner_action;
R operator()(Args&&... args) && {
return std::move(inner_action)
.Call(std::get<I>(
std::forward_as_tuple(std::forward<Args>(args)...))...);
}
};
return OA{inner_action};
}
template <
typename R, typename... Args,
typename std::enable_if<

View file

@ -1645,6 +1645,22 @@ TEST(WithArgsTest, RefQualifiedInnerAction) {
EXPECT_EQ(19, mock.AsStdFunction()(0, 17));
}
// It should be fine to provide an lvalue WithArgsAction to WillOnce, even when
// the inner action only wants to convert to OnceAction.
TEST(WithArgsTest, ProvideAsLvalueToWillOnce) {
struct SomeAction {
operator OnceAction<int(int)>() const { // NOLINT
return [](const int arg) { return arg + 2; };
}
};
const auto wa = WithArg<1>(SomeAction{});
MockFunction<int(int, int)> mock;
EXPECT_CALL(mock, Call).WillOnce(wa);
EXPECT_EQ(19, mock.AsStdFunction()(0, 17));
}
#ifndef GTEST_OS_WINDOWS_MOBILE
class SetErrnoAndReturnTest : public testing::Test {