From a05c0915074bcd1b82f232e081da9bb6c205c28d Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Sat, 16 Aug 2025 20:07:31 -0700 Subject: Deprecate single-argument DoAll and Invoke. PiperOrigin-RevId: 795969677 Change-Id: I56d88ec715475d91fb527a9281bc62574fb4608b --- googlemock/include/gmock/gmock-actions.h | 8 ++++ googlemock/include/gmock/internal/gmock-port.h | 13 +++++- googlemock/test/gmock-actions_test.cc | 55 +++++++++++--------------- googlemock/test/gmock-more-actions_test.cc | 34 ++++++++-------- 4 files changed, 59 insertions(+), 51 deletions(-) diff --git a/googlemock/include/gmock/gmock-actions.h b/googlemock/include/gmock/gmock-actions.h index 5fe50e3..2746edd 100644 --- a/googlemock/include/gmock/gmock-actions.h +++ b/googlemock/include/gmock/gmock-actions.h @@ -1866,6 +1866,13 @@ struct RethrowAction { // EXPECT_CALL(mock, Bar(5, _, _)).WillOnce(Invoke(DistanceToOrigin)); typedef internal::IgnoredValue Unused; +// Deprecated single-argument DoAll. +template +GMOCK_DEPRECATE_AND_INLINE() +typename std::decay::type DoAll(Action&& action) { + return std::forward(action); +} + // Creates an action that does actions a1, a2, ..., sequentially in // each invocation. All but the last action will have a readonly view of the // arguments. @@ -2035,6 +2042,7 @@ PolymorphicAction> SetErrnoAndReturn( // wrapper objects. // This function exists for backwards compatibility. template +GMOCK_DEPRECATE_AND_INLINE() typename std::decay::type Invoke(FunctionImpl&& function_impl) { return std::forward(function_impl); } diff --git a/googlemock/include/gmock/internal/gmock-port.h b/googlemock/include/gmock/internal/gmock-port.h index 42d36d2..c23b69d 100644 --- a/googlemock/include/gmock/internal/gmock-port.h +++ b/googlemock/include/gmock/internal/gmock-port.h @@ -57,10 +57,19 @@ #include "gmock/internal/custom/gmock-port.h" #include "gtest/internal/gtest-port.h" -#if defined(GTEST_HAS_ABSL) && !defined(GTEST_NO_ABSL_FLAGS) +#if defined(GTEST_HAS_ABSL) +#include "absl/base/macros.h" + +#define GMOCK_DEPRECATE_AND_INLINE() ABSL_DEPRECATE_AND_INLINE() + +#if !defined(GTEST_NO_ABSL_FLAGS) #include "absl/flags/declare.h" #include "absl/flags/flag.h" -#endif +#endif // !defined(GTEST_NO_ABSL_FLAGS) + +#else // defined(GTEST_HAS_ABSL) +#define GMOCK_DEPRECATE_AND_INLINE() +#endif // defined(GTEST_HAS_ABSL) // For MS Visual C++, check the compiler version. At least VS 2015 is // required to compile Google Mock. diff --git a/googlemock/test/gmock-actions_test.cc b/googlemock/test/gmock-actions_test.cc index 8fa3fba..9751705 100644 --- a/googlemock/test/gmock-actions_test.cc +++ b/googlemock/test/gmock-actions_test.cc @@ -1030,8 +1030,7 @@ void VoidFunc(bool /* flag */) {} TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) { MockClass mock; - EXPECT_CALL(mock, IntFunc(_)) - .WillRepeatedly(DoAll(Invoke(VoidFunc), DoDefault())); + EXPECT_CALL(mock, IntFunc(_)).WillRepeatedly(DoAll(VoidFunc, DoDefault())); // Ideally we should verify the error message as well. Sadly, // EXPECT_DEATH() can only capture stderr, while Google Mock's @@ -1282,7 +1281,7 @@ int ReturnOne() { TEST(IgnoreResultTest, MonomorphicAction) { g_done = false; - Action a = IgnoreResult(Invoke(ReturnOne)); + Action a = IgnoreResult(&ReturnOne); a.Perform(std::make_tuple()); EXPECT_TRUE(g_done); } @@ -1297,7 +1296,7 @@ MyNonDefaultConstructible ReturnMyNonDefaultConstructible(double /* x */) { TEST(IgnoreResultTest, ActionReturningClass) { g_done = false; Action a = - IgnoreResult(Invoke(ReturnMyNonDefaultConstructible)); // NOLINT + IgnoreResult(&ReturnMyNonDefaultConstructible); // NOLINT a.Perform(std::make_tuple(2)); EXPECT_TRUE(g_done); } @@ -1477,12 +1476,15 @@ TEST(DoAll, SupportsTypeErasedActions) { } } -// A DoAll action should be convertible to a OnceAction, even when its component -// sub-actions are user-provided types that define only an Action conversion -// operator. If they supposed being called more than once then they also support -// being called at most once. +// A multi-action DoAll action should be convertible to a OnceAction, even when +// its component sub-actions are user-provided types that define only an Action +// conversion operator. If they supposed being called more than once then they +// also support being called at most once. +// +// Single-arg DoAll just returns its argument, so will prefer the Action +// overload for WillOnce. TEST(DoAll, ConvertibleToOnceActionWithUserProvidedActionConversion) { - // Simplest case: only one sub-action. + // Final action. struct CustomFinal final { operator Action() { // NOLINT return Return(17); @@ -1493,17 +1495,7 @@ TEST(DoAll, ConvertibleToOnceActionWithUserProvidedActionConversion) { } }; - { - OnceAction action = DoAll(CustomFinal{}); - EXPECT_EQ(17, std::move(action).Call()); - } - - { - OnceAction action = DoAll(CustomFinal{}); - EXPECT_EQ(19, std::move(action).Call(0, 0)); - } - - // It should also work with multiple sub-actions. + // Sub-actions. struct CustomInitial final { operator Action() { // NOLINT return [] {}; @@ -1527,7 +1519,7 @@ TEST(DoAll, ConvertibleToOnceActionWithUserProvidedActionConversion) { // Tests using WithArgs and with an action that takes 1 argument. TEST(WithArgsTest, OneArg) { - Action a = WithArgs<1>(Invoke(Unary)); // NOLINT + Action a = WithArgs<1>(Unary); EXPECT_TRUE(a.Perform(std::make_tuple(1.5, -1))); EXPECT_FALSE(a.Perform(std::make_tuple(1.5, 1))); } @@ -1535,7 +1527,7 @@ TEST(WithArgsTest, OneArg) { // Tests using WithArgs with an action that takes 2 arguments. TEST(WithArgsTest, TwoArgs) { Action a = // NOLINT - WithArgs<0, 2>(Invoke(Binary)); + WithArgs<0, 2>(Binary); const char s[] = "Hello"; EXPECT_EQ(s + 2, a.Perform(std::make_tuple(CharPtr(s), 0.5, Short(2)))); } @@ -1551,7 +1543,7 @@ struct ConcatAll { // Tests using WithArgs with an action that takes 10 arguments. TEST(WithArgsTest, TenArgs) { Action a = - WithArgs<0, 1, 2, 3, 2, 1, 0, 1, 2, 3>(Invoke(ConcatAll{})); + WithArgs<0, 1, 2, 3, 2, 1, 0, 1, 2, 3>(ConcatAll{}); EXPECT_EQ("0123210123", a.Perform(std::make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"), CharPtr("3")))); @@ -1576,21 +1568,21 @@ TEST(WithArgsTest, NonInvokeAction) { // Tests using WithArgs to pass all original arguments in the original order. TEST(WithArgsTest, Identity) { Action a = // NOLINT - WithArgs<0, 1, 2>(Invoke(Ternary)); + WithArgs<0, 1, 2>(Ternary); EXPECT_EQ(123, a.Perform(std::make_tuple(100, Char(20), Short(3)))); } // Tests using WithArgs with repeated arguments. TEST(WithArgsTest, RepeatedArguments) { Action a = // NOLINT - WithArgs<1, 1, 1, 1>(Invoke(SumOf4)); + WithArgs<1, 1, 1, 1>(SumOf4); EXPECT_EQ(4, a.Perform(std::make_tuple(false, 1, 10))); } // Tests using WithArgs with reversed argument order. TEST(WithArgsTest, ReversedArgumentOrder) { Action a = // NOLINT - WithArgs<1, 0>(Invoke(Binary)); + WithArgs<1, 0>(Binary); const char s[] = "Hello"; EXPECT_EQ(s + 2, a.Perform(std::make_tuple(Short(2), CharPtr(s)))); } @@ -1598,14 +1590,14 @@ TEST(WithArgsTest, ReversedArgumentOrder) { // Tests using WithArgs with compatible, but not identical, argument types. TEST(WithArgsTest, ArgsOfCompatibleTypes) { Action a = // NOLINT - WithArgs<0, 1, 3>(Invoke(Ternary)); + WithArgs<0, 1, 3>(Ternary); EXPECT_EQ(123, a.Perform(std::make_tuple(Short(100), Char(20), 5.6, Char(3)))); } // Tests using WithArgs with an action that returns void. TEST(WithArgsTest, VoidAction) { - Action a = WithArgs<2, 1>(Invoke(VoidBinary)); + Action a = WithArgs<2, 1>(VoidBinary); g_done = false; a.Perform(std::make_tuple(1.5, 'a', 3)); EXPECT_TRUE(g_done); @@ -1872,9 +1864,8 @@ TEST(MockMethodTest, CanReturnMoveOnlyValue_Invoke) { [] { return std::make_unique(42); }); EXPECT_EQ(42, *mock.MakeUnique()); - EXPECT_CALL(mock, MakeUnique()).WillRepeatedly(Invoke(UniquePtrSource)); - EXPECT_CALL(mock, MakeVectorUnique()) - .WillRepeatedly(Invoke(VectorUniquePtrSource)); + EXPECT_CALL(mock, MakeUnique()).WillRepeatedly(UniquePtrSource); + EXPECT_CALL(mock, MakeVectorUnique()).WillRepeatedly(VectorUniquePtrSource); std::unique_ptr result1 = mock.MakeUnique(); EXPECT_EQ(19, *result1); std::unique_ptr result2 = mock.MakeUnique(); @@ -1896,7 +1887,7 @@ TEST(MockMethodTest, CanTakeMoveOnlyValue) { }); // DoAll() does not compile, since it would move from its arguments twice. // EXPECT_CALL(mock, TakeUnique(_, _)) - // .WillRepeatedly(DoAll(Invoke([](std::unique_ptr j) {}), + // .WillRepeatedly(DoAll([](std::unique_ptr j) {})), // Return(1))); EXPECT_CALL(mock, TakeUnique(testing::Pointee(7))) .WillOnce(Return(-7)) diff --git a/googlemock/test/gmock-more-actions_test.cc b/googlemock/test/gmock-more-actions_test.cc index 5b8b566..7c44eb0 100644 --- a/googlemock/test/gmock-more-actions_test.cc +++ b/googlemock/test/gmock-more-actions_test.cc @@ -202,45 +202,45 @@ class Foo { // Tests using Invoke() with a nullary function. TEST(InvokeTest, Nullary) { - Action a = Invoke(Nullary); // NOLINT + Action a = &Nullary; EXPECT_EQ(1, a.Perform(std::make_tuple())); } // Tests using Invoke() with a unary function. TEST(InvokeTest, Unary) { - Action a = Invoke(Unary); // NOLINT + Action a = &Unary; EXPECT_FALSE(a.Perform(std::make_tuple(1))); EXPECT_TRUE(a.Perform(std::make_tuple(-1))); } // Tests using Invoke() with a binary function. TEST(InvokeTest, Binary) { - Action a = Invoke(Binary); // NOLINT + Action a = &Binary; // NOLINT const char* p = "Hello"; EXPECT_EQ(p + 2, a.Perform(std::make_tuple(p, Short(2)))); } // Tests using Invoke() with a ternary function. TEST(InvokeTest, Ternary) { - Action a = Invoke(Ternary); // NOLINT + Action a = &Ternary; // NOLINT EXPECT_EQ(6, a.Perform(std::make_tuple(1, '\2', Short(3)))); } // Tests using Invoke() with a 4-argument function. TEST(InvokeTest, FunctionThatTakes4Arguments) { - Action a = Invoke(SumOf4); // NOLINT + Action a = &SumOf4; EXPECT_EQ(1234, a.Perform(std::make_tuple(1000, 200, 30, 4))); } // Tests using Invoke() with a 5-argument function. TEST(InvokeTest, FunctionThatTakes5Arguments) { - Action a = Invoke(SumOf5); // NOLINT + Action a = &SumOf5; EXPECT_EQ(12345, a.Perform(std::make_tuple(10000, 2000, 300, 40, 5))); } // Tests using Invoke() with a 6-argument function. TEST(InvokeTest, FunctionThatTakes6Arguments) { - Action a = Invoke(SumOf6); // NOLINT + Action a = &SumOf6; EXPECT_EQ(123456, a.Perform(std::make_tuple(100000, 20000, 3000, 400, 50, 6))); } @@ -253,7 +253,7 @@ inline const char* CharPtr(const char* s) { return s; } TEST(InvokeTest, FunctionThatTakes7Arguments) { Action - a = Invoke(Concat7); + a = &Concat7; EXPECT_EQ("1234567", a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), CharPtr("4"), CharPtr("5"), CharPtr("6"), @@ -264,7 +264,7 @@ TEST(InvokeTest, FunctionThatTakes7Arguments) { TEST(InvokeTest, FunctionThatTakes8Arguments) { Action - a = Invoke(Concat8); + a = &Concat8; EXPECT_EQ("12345678", a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), CharPtr("4"), CharPtr("5"), CharPtr("6"), @@ -276,7 +276,7 @@ TEST(InvokeTest, FunctionThatTakes9Arguments) { Action - a = Invoke(Concat9); + a = &Concat9; EXPECT_EQ("123456789", a.Perform(std::make_tuple( CharPtr("1"), CharPtr("2"), CharPtr("3"), CharPtr("4"), CharPtr("5"), CharPtr("6"), @@ -288,7 +288,7 @@ TEST(InvokeTest, FunctionThatTakes10Arguments) { Action - a = Invoke(Concat10); + a = &Concat10; EXPECT_EQ("1234567890", a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), CharPtr("4"), CharPtr("5"), CharPtr("6"), @@ -298,12 +298,12 @@ TEST(InvokeTest, FunctionThatTakes10Arguments) { // Tests using Invoke() with functions with parameters declared as Unused. TEST(InvokeTest, FunctionWithUnusedParameters) { - Action a1 = Invoke(SumOfFirst2); + Action a1 = &SumOfFirst2; std::tuple dummy = std::make_tuple(10, 2, 5.6, std::string("hi")); EXPECT_EQ(12, a1.Perform(dummy)); - Action a2 = Invoke(SumOfFirst2); + Action a2 = &SumOfFirst2; EXPECT_EQ( 23, a2.Perform(std::make_tuple(20, 3, true, static_cast(nullptr)))); } @@ -320,13 +320,13 @@ TEST(InvokeTest, MethodWithUnusedParameters) { // Tests using Invoke() with a functor. TEST(InvokeTest, Functor) { - Action a = Invoke(plus()); // NOLINT + Action a = plus(); // NOLINT EXPECT_EQ(3L, a.Perform(std::make_tuple(1, 2))); } // Tests using Invoke(f) as an action of a compatible type. TEST(InvokeTest, FunctionWithCompatibleType) { - Action a = Invoke(SumOf4); // NOLINT + Action a = &SumOf4; // NOLINT EXPECT_EQ(4321, a.Perform(std::make_tuple(4000, Short(300), Char(20), true))); } @@ -447,13 +447,13 @@ TEST(InvokeMethodTest, MethodWithCompatibleType) { // Tests using WithoutArgs with an action that takes no argument. TEST(WithoutArgsTest, NoArg) { - Action a = WithoutArgs(Invoke(Nullary)); // NOLINT + Action a = WithoutArgs(&Nullary); // NOLINT EXPECT_EQ(1, a.Perform(std::make_tuple(2))); } // Tests using WithArg with an action that takes 1 argument. TEST(WithArgTest, OneArg) { - Action b = WithArg<1>(Invoke(Unary)); // NOLINT + Action b = WithArg<1>(&Unary); // NOLINT EXPECT_TRUE(b.Perform(std::make_tuple(1.5, -1))); EXPECT_FALSE(b.Perform(std::make_tuple(1.5, 1))); } -- cgit v0.12