Skip to content

Commit e90fe24

Browse files
jacobsacopybara-github
authored andcommitted
gmock-actions: add a missing conversion operator to WithArgsAction.
PiperOrigin-RevId: 745802500 Change-Id: I8cf7b69f89af8615e9c405b1e6552d3e12ebf93d
1 parent 50a9511 commit e90fe24

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

googlemock/include/gmock/gmock-actions.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,6 +1455,30 @@ struct WithArgsAction {
14551455
return OA{std::move(inner_action)};
14561456
}
14571457

1458+
// As above, but in the case where we want to create a OnceAction from a const
1459+
// WithArgsAction. This is fine as long as the inner action doesn't need to
1460+
// move any of its state to create a OnceAction.
1461+
template <
1462+
typename R, typename... Args,
1463+
typename std::enable_if<
1464+
std::is_convertible<const InnerAction&,
1465+
OnceAction<R(internal::TupleElement<
1466+
I, std::tuple<Args...>>...)>>::value,
1467+
int>::type = 0>
1468+
operator OnceAction<R(Args...)>() const& { // NOLINT
1469+
struct OA {
1470+
OnceAction<InnerSignature<R, Args...>> inner_action;
1471+
1472+
R operator()(Args&&... args) && {
1473+
return std::move(inner_action)
1474+
.Call(std::get<I>(
1475+
std::forward_as_tuple(std::forward<Args>(args)...))...);
1476+
}
1477+
};
1478+
1479+
return OA{inner_action};
1480+
}
1481+
14581482
template <
14591483
typename R, typename... Args,
14601484
typename std::enable_if<

googlemock/test/gmock-actions_test.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,6 +1645,22 @@ TEST(WithArgsTest, RefQualifiedInnerAction) {
16451645
EXPECT_EQ(19, mock.AsStdFunction()(0, 17));
16461646
}
16471647

1648+
// It should be fine to provide an lvalue WithArgsAction to WillOnce, even when
1649+
// the inner action only wants to convert to OnceAction.
1650+
TEST(WithArgsTest, ProvideAsLvalueToWillOnce) {
1651+
struct SomeAction {
1652+
operator OnceAction<int(int)>() const { // NOLINT
1653+
return [](const int arg) { return arg + 2; };
1654+
}
1655+
};
1656+
1657+
const auto wa = WithArg<1>(SomeAction{});
1658+
1659+
MockFunction<int(int, int)> mock;
1660+
EXPECT_CALL(mock, Call).WillOnce(wa);
1661+
EXPECT_EQ(19, mock.AsStdFunction()(0, 17));
1662+
}
1663+
16481664
#ifndef GTEST_OS_WINDOWS_MOBILE
16491665

16501666
class SetErrnoAndReturnTest : public testing::Test {

0 commit comments

Comments
 (0)