From 955552518b4e0ad0249396e6885c0f18cd4ce529 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Fri, 24 Apr 2020 05:17:47 -0400 Subject: Googletest export Rewrite ReturnNew action without using pump. PiperOrigin-RevId: 308219616 --- googlemock/include/gmock/gmock-actions.h | 26 ++++++ googlemock/include/gmock/gmock-generated-actions.h | 71 ---------------- .../include/gmock/gmock-generated-actions.h.pump | 18 ---- .../include/gmock/internal/gmock-internal-utils.h | 10 ++- googlemock/test/gmock-actions_test.cc | 99 ++++++++++++++++------ googlemock/test/gmock-generated-actions_test.cc | 55 ------------ 6 files changed, 104 insertions(+), 175 deletions(-) diff --git a/googlemock/include/gmock/gmock-actions.h b/googlemock/include/gmock/gmock-actions.h index 615651b3..1db98d7b 100644 --- a/googlemock/include/gmock/gmock-actions.h +++ b/googlemock/include/gmock/gmock-actions.h @@ -138,6 +138,7 @@ #include #include #include +#include #include #include @@ -1311,6 +1312,31 @@ inline ::std::reference_wrapper ByRef(T& l_value) { // NOLINT namespace internal { +template +struct ReturnNewAction { + T* operator()() const { + return internal::Apply( + [](const Params&... unpacked_params) { + return new T(unpacked_params...); + }, + params); + } + std::tuple params; +}; + +} // namespace internal + +// The ReturnNew(a1, a2, ..., a_k) action returns a pointer to a new +// instance of type T, constructed on the heap with constructor arguments +// a1, a2, ..., and a_k. The caller assumes ownership of the returned value. +template +internal::ReturnNewAction::type...> ReturnNew( + Params&&... params) { + return {std::forward_as_tuple(std::forward(params)...)}; +} + +namespace internal { + // A macro from the ACTION* family (defined later in gmock-generated-actions.h) // defines an action that can be used in a mock function. Typically, // these actions only care about a subset of the arguments of the mock diff --git a/googlemock/include/gmock/gmock-generated-actions.h b/googlemock/include/gmock/gmock-generated-actions.h index c78debef..201a9fdf 100644 --- a/googlemock/include/gmock/gmock-generated-actions.h +++ b/googlemock/include/gmock/gmock-generated-actions.h @@ -602,77 +602,6 @@ ACTION_TEMPLATE(InvokeArgument, p8, p9); } -// Various overloads for ReturnNew(). -// -// The ReturnNew(a1, a2, ..., a_k) action returns a pointer to a new -// instance of type T, constructed on the heap with constructor arguments -// a1, a2, ..., and a_k. The caller assumes ownership of the returned value. -ACTION_TEMPLATE(ReturnNew, - HAS_1_TEMPLATE_PARAMS(typename, T), - AND_0_VALUE_PARAMS()) { - return new T(); -} - -ACTION_TEMPLATE(ReturnNew, - HAS_1_TEMPLATE_PARAMS(typename, T), - AND_1_VALUE_PARAMS(p0)) { - return new T(p0); -} - -ACTION_TEMPLATE(ReturnNew, - HAS_1_TEMPLATE_PARAMS(typename, T), - AND_2_VALUE_PARAMS(p0, p1)) { - return new T(p0, p1); -} - -ACTION_TEMPLATE(ReturnNew, - HAS_1_TEMPLATE_PARAMS(typename, T), - AND_3_VALUE_PARAMS(p0, p1, p2)) { - return new T(p0, p1, p2); -} - -ACTION_TEMPLATE(ReturnNew, - HAS_1_TEMPLATE_PARAMS(typename, T), - AND_4_VALUE_PARAMS(p0, p1, p2, p3)) { - return new T(p0, p1, p2, p3); -} - -ACTION_TEMPLATE(ReturnNew, - HAS_1_TEMPLATE_PARAMS(typename, T), - AND_5_VALUE_PARAMS(p0, p1, p2, p3, p4)) { - return new T(p0, p1, p2, p3, p4); -} - -ACTION_TEMPLATE(ReturnNew, - HAS_1_TEMPLATE_PARAMS(typename, T), - AND_6_VALUE_PARAMS(p0, p1, p2, p3, p4, p5)) { - return new T(p0, p1, p2, p3, p4, p5); -} - -ACTION_TEMPLATE(ReturnNew, - HAS_1_TEMPLATE_PARAMS(typename, T), - AND_7_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6)) { - return new T(p0, p1, p2, p3, p4, p5, p6); -} - -ACTION_TEMPLATE(ReturnNew, - HAS_1_TEMPLATE_PARAMS(typename, T), - AND_8_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, p7)) { - return new T(p0, p1, p2, p3, p4, p5, p6, p7); -} - -ACTION_TEMPLATE(ReturnNew, - HAS_1_TEMPLATE_PARAMS(typename, T), - AND_9_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, p7, p8)) { - return new T(p0, p1, p2, p3, p4, p5, p6, p7, p8); -} - -ACTION_TEMPLATE(ReturnNew, - HAS_1_TEMPLATE_PARAMS(typename, T), - AND_10_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, p7, p8, p9)) { - return new T(p0, p1, p2, p3, p4, p5, p6, p7, p8, p9); -} - #ifdef _MSC_VER # pragma warning(pop) #endif diff --git a/googlemock/include/gmock/gmock-generated-actions.h.pump b/googlemock/include/gmock/gmock-generated-actions.h.pump index be9d99fe..2137d8d6 100644 --- a/googlemock/include/gmock/gmock-generated-actions.h.pump +++ b/googlemock/include/gmock/gmock-generated-actions.h.pump @@ -344,24 +344,6 @@ ACTION_TEMPLATE(InvokeArgument, ]] -// Various overloads for ReturnNew(). -// -// The ReturnNew(a1, a2, ..., a_k) action returns a pointer to a new -// instance of type T, constructed on the heap with constructor arguments -// a1, a2, ..., and a_k. The caller assumes ownership of the returned value. -$range i 0..n -$for i [[ -$range j 0..i-1 -$var ps = [[$for j, [[p$j]]]] - -ACTION_TEMPLATE(ReturnNew, - HAS_1_TEMPLATE_PARAMS(typename, T), - AND_$i[[]]_VALUE_PARAMS($ps)) { - return new T($ps); -} - -]] - #ifdef _MSC_VER # pragma warning(pop) #endif diff --git a/googlemock/include/gmock/internal/gmock-internal-utils.h b/googlemock/include/gmock/internal/gmock-internal-utils.h index 66cf857b..5580dcb3 100644 --- a/googlemock/include/gmock/internal/gmock-internal-utils.h +++ b/googlemock/include/gmock/internal/gmock-internal-utils.h @@ -422,11 +422,13 @@ auto ApplyImpl(F&& f, Tuple&& args, IndexSequence) -> decltype( // Apply the function to a tuple of arguments. template -auto Apply(F&& f, Tuple&& args) - -> decltype(ApplyImpl(std::forward(f), std::forward(args), - MakeIndexSequence::value>())) { +auto Apply(F&& f, Tuple&& args) -> decltype( + ApplyImpl(std::forward(f), std::forward(args), + MakeIndexSequence::type>::value>())) { return ApplyImpl(std::forward(f), std::forward(args), - MakeIndexSequence::value>()); + MakeIndexSequence::type>::value>()); } // Template struct Function, where F must be a function type, contains diff --git a/googlemock/test/gmock-actions_test.cc b/googlemock/test/gmock-actions_test.cc index d1229ac9..6cd1847c 100644 --- a/googlemock/test/gmock-actions_test.cc +++ b/googlemock/test/gmock-actions_test.cc @@ -54,35 +54,34 @@ namespace { -// This list should be kept sorted. -using testing::_; -using testing::Action; -using testing::ActionInterface; -using testing::Assign; -using testing::ByMove; -using testing::ByRef; -using testing::DefaultValue; -using testing::DoAll; -using testing::DoDefault; -using testing::IgnoreResult; -using testing::Invoke; -using testing::InvokeWithoutArgs; -using testing::MakePolymorphicAction; -using testing::Ne; -using testing::PolymorphicAction; -using testing::Return; -using testing::ReturnNull; -using testing::ReturnRef; -using testing::ReturnRefOfCopy; -using testing::ReturnRoundRobin; -using testing::SetArgPointee; -using testing::SetArgumentPointee; -using testing::Unused; -using testing::WithArgs; -using testing::internal::BuiltInDefaultValue; +using ::testing::_; +using ::testing::Action; +using ::testing::ActionInterface; +using ::testing::Assign; +using ::testing::ByMove; +using ::testing::ByRef; +using ::testing::DefaultValue; +using ::testing::DoAll; +using ::testing::DoDefault; +using ::testing::IgnoreResult; +using ::testing::Invoke; +using ::testing::InvokeWithoutArgs; +using ::testing::MakePolymorphicAction; +using ::testing::PolymorphicAction; +using ::testing::Return; +using ::testing::ReturnNew; +using ::testing::ReturnNull; +using ::testing::ReturnRef; +using ::testing::ReturnRefOfCopy; +using ::testing::ReturnRoundRobin; +using ::testing::SetArgPointee; +using ::testing::SetArgumentPointee; +using ::testing::Unused; +using ::testing::WithArgs; +using ::testing::internal::BuiltInDefaultValue; #if !GTEST_OS_WINDOWS_MOBILE -using testing::SetErrnoAndReturn; +using ::testing::SetErrnoAndReturn; #endif // Tests that BuiltInDefaultValue::Get() returns NULL. @@ -1292,6 +1291,52 @@ TEST(ByRefTest, PrintsCorrectly) { EXPECT_EQ(expected.str(), actual.str()); } +struct UnaryConstructorClass { + explicit UnaryConstructorClass(int v) : value(v) {} + int value; +}; + +// Tests using ReturnNew() with a unary constructor. +TEST(ReturnNewTest, Unary) { + Action a = ReturnNew(4000); + UnaryConstructorClass* c = a.Perform(std::make_tuple()); + EXPECT_EQ(4000, c->value); + delete c; +} + +TEST(ReturnNewTest, UnaryWorksWhenMockMethodHasArgs) { + Action a = + ReturnNew(4000); + UnaryConstructorClass* c = a.Perform(std::make_tuple(false, 5)); + EXPECT_EQ(4000, c->value); + delete c; +} + +TEST(ReturnNewTest, UnaryWorksWhenMockMethodReturnsPointerToConst) { + Action a = + ReturnNew(4000); + const UnaryConstructorClass* c = a.Perform(std::make_tuple()); + EXPECT_EQ(4000, c->value); + delete c; +} + +class TenArgConstructorClass { + public: + TenArgConstructorClass(int a1, int a2, int a3, int a4, int a5, int a6, int a7, + int a8, int a9, int a10) + : value_(a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10) {} + int value_; +}; + +// Tests using ReturnNew() with a 10-argument constructor. +TEST(ReturnNewTest, ConstructorThatTakes10Arguments) { + Action a = ReturnNew( + 1000000000, 200000000, 30000000, 4000000, 500000, 60000, 7000, 800, 90, + 0); + TenArgConstructorClass* c = a.Perform(std::make_tuple()); + EXPECT_EQ(1234567890, c->value_); + delete c; +} std::unique_ptr UniquePtrSource() { return std::unique_ptr(new int(19)); diff --git a/googlemock/test/gmock-generated-actions_test.cc b/googlemock/test/gmock-generated-actions_test.cc index 4c649a7e..ef39dff7 100644 --- a/googlemock/test/gmock-generated-actions_test.cc +++ b/googlemock/test/gmock-generated-actions_test.cc @@ -53,7 +53,6 @@ using testing::ByRef; using testing::DoAll; using testing::Invoke; using testing::Return; -using testing::ReturnNew; using testing::SetArgPointee; using testing::StaticAssertTypeEq; using testing::Unused; @@ -844,49 +843,6 @@ TEST(ActionPnMacroTest, CanExplicitlyInstantiateWithReferenceTypes) { EXPECT_EQ(55, a.Perform(empty)); } -class NullaryConstructorClass { - public: - NullaryConstructorClass() : value_(123) {} - int value_; -}; - -// Tests using ReturnNew() with a nullary constructor. -TEST(ReturnNewTest, NoArgs) { - Action a = ReturnNew(); - NullaryConstructorClass* c = a.Perform(std::make_tuple()); - EXPECT_EQ(123, c->value_); - delete c; -} - -class UnaryConstructorClass { - public: - explicit UnaryConstructorClass(int value) : value_(value) {} - int value_; -}; - -// Tests using ReturnNew() with a unary constructor. -TEST(ReturnNewTest, Unary) { - Action a = ReturnNew(4000); - UnaryConstructorClass* c = a.Perform(std::make_tuple()); - EXPECT_EQ(4000, c->value_); - delete c; -} - -TEST(ReturnNewTest, UnaryWorksWhenMockMethodHasArgs) { - Action a = - ReturnNew(4000); - UnaryConstructorClass* c = a.Perform(std::make_tuple(false, 5)); - EXPECT_EQ(4000, c->value_); - delete c; -} - -TEST(ReturnNewTest, UnaryWorksWhenMockMethodReturnsPointerToConst) { - Action a = - ReturnNew(4000); - const UnaryConstructorClass* c = a.Perform(std::make_tuple()); - EXPECT_EQ(4000, c->value_); - delete c; -} class TenArgConstructorClass { public: @@ -897,17 +853,6 @@ class TenArgConstructorClass { int value_; }; -// Tests using ReturnNew() with a 10-argument constructor. -TEST(ReturnNewTest, ConstructorThatTakes10Arguments) { - Action a = - ReturnNew(1000000000, 200000000, 30000000, - 4000000, 500000, 60000, - 7000, 800, 90, 0); - TenArgConstructorClass* c = a.Perform(std::make_tuple()); - EXPECT_EQ(1234567890, c->value_); - delete c; -} - // Tests that ACTION_TEMPLATE works when there is no value parameter. ACTION_TEMPLATE(CreateNew, HAS_1_TEMPLATE_PARAMS(typename, T), -- cgit v1.2.3