Skip to content

Commit a05c091

Browse files
Abseil Teamcopybara-github
authored andcommitted
Deprecate single-argument DoAll and Invoke.
PiperOrigin-RevId: 795969677 Change-Id: I56d88ec715475d91fb527a9281bc62574fb4608b
1 parent 244cec8 commit a05c091

File tree

4 files changed

+59
-51
lines changed

4 files changed

+59
-51
lines changed

googlemock/include/gmock/gmock-actions.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1866,6 +1866,13 @@ struct RethrowAction {
18661866
// EXPECT_CALL(mock, Bar(5, _, _)).WillOnce(Invoke(DistanceToOrigin));
18671867
typedef internal::IgnoredValue Unused;
18681868

1869+
// Deprecated single-argument DoAll.
1870+
template <typename Action>
1871+
GMOCK_DEPRECATE_AND_INLINE()
1872+
typename std::decay<Action>::type DoAll(Action&& action) {
1873+
return std::forward<Action>(action);
1874+
}
1875+
18691876
// Creates an action that does actions a1, a2, ..., sequentially in
18701877
// each invocation. All but the last action will have a readonly view of the
18711878
// arguments.
@@ -2035,6 +2042,7 @@ PolymorphicAction<internal::SetErrnoAndReturnAction<T>> SetErrnoAndReturn(
20352042
// wrapper objects.
20362043
// This function exists for backwards compatibility.
20372044
template <typename FunctionImpl>
2045+
GMOCK_DEPRECATE_AND_INLINE()
20382046
typename std::decay<FunctionImpl>::type Invoke(FunctionImpl&& function_impl) {
20392047
return std::forward<FunctionImpl>(function_impl);
20402048
}

googlemock/include/gmock/internal/gmock-port.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,19 @@
5757
#include "gmock/internal/custom/gmock-port.h"
5858
#include "gtest/internal/gtest-port.h"
5959

60-
#if defined(GTEST_HAS_ABSL) && !defined(GTEST_NO_ABSL_FLAGS)
60+
#if defined(GTEST_HAS_ABSL)
61+
#include "absl/base/macros.h"
62+
63+
#define GMOCK_DEPRECATE_AND_INLINE() ABSL_DEPRECATE_AND_INLINE()
64+
65+
#if !defined(GTEST_NO_ABSL_FLAGS)
6166
#include "absl/flags/declare.h"
6267
#include "absl/flags/flag.h"
63-
#endif
68+
#endif // !defined(GTEST_NO_ABSL_FLAGS)
69+
70+
#else // defined(GTEST_HAS_ABSL)
71+
#define GMOCK_DEPRECATE_AND_INLINE()
72+
#endif // defined(GTEST_HAS_ABSL)
6473

6574
// For MS Visual C++, check the compiler version. At least VS 2015 is
6675
// required to compile Google Mock.

googlemock/test/gmock-actions_test.cc

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,8 +1030,7 @@ void VoidFunc(bool /* flag */) {}
10301030

10311031
TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) {
10321032
MockClass mock;
1033-
EXPECT_CALL(mock, IntFunc(_))
1034-
.WillRepeatedly(DoAll(Invoke(VoidFunc), DoDefault()));
1033+
EXPECT_CALL(mock, IntFunc(_)).WillRepeatedly(DoAll(VoidFunc, DoDefault()));
10351034

10361035
// Ideally we should verify the error message as well. Sadly,
10371036
// EXPECT_DEATH() can only capture stderr, while Google Mock's
@@ -1282,7 +1281,7 @@ int ReturnOne() {
12821281

12831282
TEST(IgnoreResultTest, MonomorphicAction) {
12841283
g_done = false;
1285-
Action<void()> a = IgnoreResult(Invoke(ReturnOne));
1284+
Action<void()> a = IgnoreResult(&ReturnOne);
12861285
a.Perform(std::make_tuple());
12871286
EXPECT_TRUE(g_done);
12881287
}
@@ -1297,7 +1296,7 @@ MyNonDefaultConstructible ReturnMyNonDefaultConstructible(double /* x */) {
12971296
TEST(IgnoreResultTest, ActionReturningClass) {
12981297
g_done = false;
12991298
Action<void(int)> a =
1300-
IgnoreResult(Invoke(ReturnMyNonDefaultConstructible)); // NOLINT
1299+
IgnoreResult(&ReturnMyNonDefaultConstructible); // NOLINT
13011300
a.Perform(std::make_tuple(2));
13021301
EXPECT_TRUE(g_done);
13031302
}
@@ -1477,12 +1476,15 @@ TEST(DoAll, SupportsTypeErasedActions) {
14771476
}
14781477
}
14791478

1480-
// A DoAll action should be convertible to a OnceAction, even when its component
1481-
// sub-actions are user-provided types that define only an Action conversion
1482-
// operator. If they supposed being called more than once then they also support
1483-
// being called at most once.
1479+
// A multi-action DoAll action should be convertible to a OnceAction, even when
1480+
// its component sub-actions are user-provided types that define only an Action
1481+
// conversion operator. If they supposed being called more than once then they
1482+
// also support being called at most once.
1483+
//
1484+
// Single-arg DoAll just returns its argument, so will prefer the Action<F>
1485+
// overload for WillOnce.
14841486
TEST(DoAll, ConvertibleToOnceActionWithUserProvidedActionConversion) {
1485-
// Simplest case: only one sub-action.
1487+
// Final action.
14861488
struct CustomFinal final {
14871489
operator Action<int()>() { // NOLINT
14881490
return Return(17);
@@ -1493,17 +1495,7 @@ TEST(DoAll, ConvertibleToOnceActionWithUserProvidedActionConversion) {
14931495
}
14941496
};
14951497

1496-
{
1497-
OnceAction<int()> action = DoAll(CustomFinal{});
1498-
EXPECT_EQ(17, std::move(action).Call());
1499-
}
1500-
1501-
{
1502-
OnceAction<int(int, char)> action = DoAll(CustomFinal{});
1503-
EXPECT_EQ(19, std::move(action).Call(0, 0));
1504-
}
1505-
1506-
// It should also work with multiple sub-actions.
1498+
// Sub-actions.
15071499
struct CustomInitial final {
15081500
operator Action<void()>() { // NOLINT
15091501
return [] {};
@@ -1527,15 +1519,15 @@ TEST(DoAll, ConvertibleToOnceActionWithUserProvidedActionConversion) {
15271519

15281520
// Tests using WithArgs and with an action that takes 1 argument.
15291521
TEST(WithArgsTest, OneArg) {
1530-
Action<bool(double x, int n)> a = WithArgs<1>(Invoke(Unary)); // NOLINT
1522+
Action<bool(double x, int n)> a = WithArgs<1>(Unary);
15311523
EXPECT_TRUE(a.Perform(std::make_tuple(1.5, -1)));
15321524
EXPECT_FALSE(a.Perform(std::make_tuple(1.5, 1)));
15331525
}
15341526

15351527
// Tests using WithArgs with an action that takes 2 arguments.
15361528
TEST(WithArgsTest, TwoArgs) {
15371529
Action<const char*(const char* s, double x, short n)> a = // NOLINT
1538-
WithArgs<0, 2>(Invoke(Binary));
1530+
WithArgs<0, 2>(Binary);
15391531
const char s[] = "Hello";
15401532
EXPECT_EQ(s + 2, a.Perform(std::make_tuple(CharPtr(s), 0.5, Short(2))));
15411533
}
@@ -1551,7 +1543,7 @@ struct ConcatAll {
15511543
// Tests using WithArgs with an action that takes 10 arguments.
15521544
TEST(WithArgsTest, TenArgs) {
15531545
Action<std::string(const char*, const char*, const char*, const char*)> a =
1554-
WithArgs<0, 1, 2, 3, 2, 1, 0, 1, 2, 3>(Invoke(ConcatAll{}));
1546+
WithArgs<0, 1, 2, 3, 2, 1, 0, 1, 2, 3>(ConcatAll{});
15551547
EXPECT_EQ("0123210123",
15561548
a.Perform(std::make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"),
15571549
CharPtr("3"))));
@@ -1576,36 +1568,36 @@ TEST(WithArgsTest, NonInvokeAction) {
15761568
// Tests using WithArgs to pass all original arguments in the original order.
15771569
TEST(WithArgsTest, Identity) {
15781570
Action<int(int x, char y, short z)> a = // NOLINT
1579-
WithArgs<0, 1, 2>(Invoke(Ternary));
1571+
WithArgs<0, 1, 2>(Ternary);
15801572
EXPECT_EQ(123, a.Perform(std::make_tuple(100, Char(20), Short(3))));
15811573
}
15821574

15831575
// Tests using WithArgs with repeated arguments.
15841576
TEST(WithArgsTest, RepeatedArguments) {
15851577
Action<int(bool, int m, int n)> a = // NOLINT
1586-
WithArgs<1, 1, 1, 1>(Invoke(SumOf4));
1578+
WithArgs<1, 1, 1, 1>(SumOf4);
15871579
EXPECT_EQ(4, a.Perform(std::make_tuple(false, 1, 10)));
15881580
}
15891581

15901582
// Tests using WithArgs with reversed argument order.
15911583
TEST(WithArgsTest, ReversedArgumentOrder) {
15921584
Action<const char*(short n, const char* input)> a = // NOLINT
1593-
WithArgs<1, 0>(Invoke(Binary));
1585+
WithArgs<1, 0>(Binary);
15941586
const char s[] = "Hello";
15951587
EXPECT_EQ(s + 2, a.Perform(std::make_tuple(Short(2), CharPtr(s))));
15961588
}
15971589

15981590
// Tests using WithArgs with compatible, but not identical, argument types.
15991591
TEST(WithArgsTest, ArgsOfCompatibleTypes) {
16001592
Action<long(short x, char y, double z, char c)> a = // NOLINT
1601-
WithArgs<0, 1, 3>(Invoke(Ternary));
1593+
WithArgs<0, 1, 3>(Ternary);
16021594
EXPECT_EQ(123,
16031595
a.Perform(std::make_tuple(Short(100), Char(20), 5.6, Char(3))));
16041596
}
16051597

16061598
// Tests using WithArgs with an action that returns void.
16071599
TEST(WithArgsTest, VoidAction) {
1608-
Action<void(double x, char c, int n)> a = WithArgs<2, 1>(Invoke(VoidBinary));
1600+
Action<void(double x, char c, int n)> a = WithArgs<2, 1>(VoidBinary);
16091601
g_done = false;
16101602
a.Perform(std::make_tuple(1.5, 'a', 3));
16111603
EXPECT_TRUE(g_done);
@@ -1872,9 +1864,8 @@ TEST(MockMethodTest, CanReturnMoveOnlyValue_Invoke) {
18721864
[] { return std::make_unique<int>(42); });
18731865
EXPECT_EQ(42, *mock.MakeUnique());
18741866

1875-
EXPECT_CALL(mock, MakeUnique()).WillRepeatedly(Invoke(UniquePtrSource));
1876-
EXPECT_CALL(mock, MakeVectorUnique())
1877-
.WillRepeatedly(Invoke(VectorUniquePtrSource));
1867+
EXPECT_CALL(mock, MakeUnique()).WillRepeatedly(UniquePtrSource);
1868+
EXPECT_CALL(mock, MakeVectorUnique()).WillRepeatedly(VectorUniquePtrSource);
18781869
std::unique_ptr<int> result1 = mock.MakeUnique();
18791870
EXPECT_EQ(19, *result1);
18801871
std::unique_ptr<int> result2 = mock.MakeUnique();
@@ -1896,7 +1887,7 @@ TEST(MockMethodTest, CanTakeMoveOnlyValue) {
18961887
});
18971888
// DoAll() does not compile, since it would move from its arguments twice.
18981889
// EXPECT_CALL(mock, TakeUnique(_, _))
1899-
// .WillRepeatedly(DoAll(Invoke([](std::unique_ptr<int> j) {}),
1890+
// .WillRepeatedly(DoAll([](std::unique_ptr<int> j) {})),
19001891
// Return(1)));
19011892
EXPECT_CALL(mock, TakeUnique(testing::Pointee(7)))
19021893
.WillOnce(Return(-7))

googlemock/test/gmock-more-actions_test.cc

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -202,45 +202,45 @@ class Foo {
202202

203203
// Tests using Invoke() with a nullary function.
204204
TEST(InvokeTest, Nullary) {
205-
Action<int()> a = Invoke(Nullary); // NOLINT
205+
Action<int()> a = &Nullary;
206206
EXPECT_EQ(1, a.Perform(std::make_tuple()));
207207
}
208208

209209
// Tests using Invoke() with a unary function.
210210
TEST(InvokeTest, Unary) {
211-
Action<bool(int)> a = Invoke(Unary); // NOLINT
211+
Action<bool(int)> a = &Unary;
212212
EXPECT_FALSE(a.Perform(std::make_tuple(1)));
213213
EXPECT_TRUE(a.Perform(std::make_tuple(-1)));
214214
}
215215

216216
// Tests using Invoke() with a binary function.
217217
TEST(InvokeTest, Binary) {
218-
Action<const char*(const char*, short)> a = Invoke(Binary); // NOLINT
218+
Action<const char*(const char*, short)> a = &Binary; // NOLINT
219219
const char* p = "Hello";
220220
EXPECT_EQ(p + 2, a.Perform(std::make_tuple(p, Short(2))));
221221
}
222222

223223
// Tests using Invoke() with a ternary function.
224224
TEST(InvokeTest, Ternary) {
225-
Action<int(int, char, short)> a = Invoke(Ternary); // NOLINT
225+
Action<int(int, char, short)> a = &Ternary; // NOLINT
226226
EXPECT_EQ(6, a.Perform(std::make_tuple(1, '\2', Short(3))));
227227
}
228228

229229
// Tests using Invoke() with a 4-argument function.
230230
TEST(InvokeTest, FunctionThatTakes4Arguments) {
231-
Action<int(int, int, int, int)> a = Invoke(SumOf4); // NOLINT
231+
Action<int(int, int, int, int)> a = &SumOf4;
232232
EXPECT_EQ(1234, a.Perform(std::make_tuple(1000, 200, 30, 4)));
233233
}
234234

235235
// Tests using Invoke() with a 5-argument function.
236236
TEST(InvokeTest, FunctionThatTakes5Arguments) {
237-
Action<int(int, int, int, int, int)> a = Invoke(SumOf5); // NOLINT
237+
Action<int(int, int, int, int, int)> a = &SumOf5;
238238
EXPECT_EQ(12345, a.Perform(std::make_tuple(10000, 2000, 300, 40, 5)));
239239
}
240240

241241
// Tests using Invoke() with a 6-argument function.
242242
TEST(InvokeTest, FunctionThatTakes6Arguments) {
243-
Action<int(int, int, int, int, int, int)> a = Invoke(SumOf6); // NOLINT
243+
Action<int(int, int, int, int, int, int)> a = &SumOf6;
244244
EXPECT_EQ(123456,
245245
a.Perform(std::make_tuple(100000, 20000, 3000, 400, 50, 6)));
246246
}
@@ -253,7 +253,7 @@ inline const char* CharPtr(const char* s) { return s; }
253253
TEST(InvokeTest, FunctionThatTakes7Arguments) {
254254
Action<std::string(const char*, const char*, const char*, const char*,
255255
const char*, const char*, const char*)>
256-
a = Invoke(Concat7);
256+
a = &Concat7;
257257
EXPECT_EQ("1234567",
258258
a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
259259
CharPtr("4"), CharPtr("5"), CharPtr("6"),
@@ -264,7 +264,7 @@ TEST(InvokeTest, FunctionThatTakes7Arguments) {
264264
TEST(InvokeTest, FunctionThatTakes8Arguments) {
265265
Action<std::string(const char*, const char*, const char*, const char*,
266266
const char*, const char*, const char*, const char*)>
267-
a = Invoke(Concat8);
267+
a = &Concat8;
268268
EXPECT_EQ("12345678",
269269
a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
270270
CharPtr("4"), CharPtr("5"), CharPtr("6"),
@@ -276,7 +276,7 @@ TEST(InvokeTest, FunctionThatTakes9Arguments) {
276276
Action<std::string(const char*, const char*, const char*, const char*,
277277
const char*, const char*, const char*, const char*,
278278
const char*)>
279-
a = Invoke(Concat9);
279+
a = &Concat9;
280280
EXPECT_EQ("123456789", a.Perform(std::make_tuple(
281281
CharPtr("1"), CharPtr("2"), CharPtr("3"),
282282
CharPtr("4"), CharPtr("5"), CharPtr("6"),
@@ -288,7 +288,7 @@ TEST(InvokeTest, FunctionThatTakes10Arguments) {
288288
Action<std::string(const char*, const char*, const char*, const char*,
289289
const char*, const char*, const char*, const char*,
290290
const char*, const char*)>
291-
a = Invoke(Concat10);
291+
a = &Concat10;
292292
EXPECT_EQ("1234567890",
293293
a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
294294
CharPtr("4"), CharPtr("5"), CharPtr("6"),
@@ -298,12 +298,12 @@ TEST(InvokeTest, FunctionThatTakes10Arguments) {
298298

299299
// Tests using Invoke() with functions with parameters declared as Unused.
300300
TEST(InvokeTest, FunctionWithUnusedParameters) {
301-
Action<int(int, int, double, const std::string&)> a1 = Invoke(SumOfFirst2);
301+
Action<int(int, int, double, const std::string&)> a1 = &SumOfFirst2;
302302
std::tuple<int, int, double, std::string> dummy =
303303
std::make_tuple(10, 2, 5.6, std::string("hi"));
304304
EXPECT_EQ(12, a1.Perform(dummy));
305305

306-
Action<int(int, int, bool, int*)> a2 = Invoke(SumOfFirst2);
306+
Action<int(int, int, bool, int*)> a2 = &SumOfFirst2;
307307
EXPECT_EQ(
308308
23, a2.Perform(std::make_tuple(20, 3, true, static_cast<int*>(nullptr))));
309309
}
@@ -320,13 +320,13 @@ TEST(InvokeTest, MethodWithUnusedParameters) {
320320

321321
// Tests using Invoke() with a functor.
322322
TEST(InvokeTest, Functor) {
323-
Action<long(long, int)> a = Invoke(plus<long>()); // NOLINT
323+
Action<long(long, int)> a = plus<long>(); // NOLINT
324324
EXPECT_EQ(3L, a.Perform(std::make_tuple(1, 2)));
325325
}
326326

327327
// Tests using Invoke(f) as an action of a compatible type.
328328
TEST(InvokeTest, FunctionWithCompatibleType) {
329-
Action<long(int, short, char, bool)> a = Invoke(SumOf4); // NOLINT
329+
Action<long(int, short, char, bool)> a = &SumOf4; // NOLINT
330330
EXPECT_EQ(4321, a.Perform(std::make_tuple(4000, Short(300), Char(20), true)));
331331
}
332332

@@ -447,13 +447,13 @@ TEST(InvokeMethodTest, MethodWithCompatibleType) {
447447

448448
// Tests using WithoutArgs with an action that takes no argument.
449449
TEST(WithoutArgsTest, NoArg) {
450-
Action<int(int n)> a = WithoutArgs(Invoke(Nullary)); // NOLINT
450+
Action<int(int n)> a = WithoutArgs(&Nullary); // NOLINT
451451
EXPECT_EQ(1, a.Perform(std::make_tuple(2)));
452452
}
453453

454454
// Tests using WithArg with an action that takes 1 argument.
455455
TEST(WithArgTest, OneArg) {
456-
Action<bool(double x, int n)> b = WithArg<1>(Invoke(Unary)); // NOLINT
456+
Action<bool(double x, int n)> b = WithArg<1>(&Unary); // NOLINT
457457
EXPECT_TRUE(b.Perform(std::make_tuple(1.5, -1)));
458458
EXPECT_FALSE(b.Perform(std::make_tuple(1.5, 1)));
459459
}

0 commit comments

Comments
 (0)