From ca54b673034f6f182ff22ac554efcd1176f5808c Mon Sep 17 00:00:00 2001 From: Gennadiy Civil Date: Mon, 9 Apr 2018 22:10:12 -0400 Subject: Revert "gmock actions 2" --- googlemock/include/gmock/gmock-actions.h | 90 +++--------- googlemock/include/gmock/gmock-generated-actions.h | 4 +- .../include/gmock/gmock-generated-actions.h.pump | 2 +- .../include/gmock/gmock-generated-matchers.h.pump | 4 +- .../gmock/gmock-generated-nice-strict.h.pump | 4 +- googlemock/include/gmock/gmock-more-matchers.h | 8 -- .../internal/custom/gmock-generated-actions.h.pump | 2 +- .../include/gmock/internal/gmock-internal-utils.h | 16 +-- googlemock/src/gmock-internal-utils.cc | 2 +- googlemock/src/gmock-spec-builders.cc | 15 -- googlemock/test/gmock-actions_test.cc | 160 +-------------------- googlemock/test/gmock_output_test_.cc | 10 -- googletest/include/gtest/gtest.h | 13 -- 13 files changed, 27 insertions(+), 303 deletions(-) diff --git a/googlemock/include/gmock/gmock-actions.h b/googlemock/include/gmock/gmock-actions.h index a2784f6..90fd2ea 100644 --- a/googlemock/include/gmock/gmock-actions.h +++ b/googlemock/include/gmock/gmock-actions.h @@ -360,21 +360,15 @@ class Action { // Constructs a null Action. Needed for storing Action objects in // STL containers. - Action() {} + Action() : impl_(NULL) {} -#if GTEST_LANG_CXX11 - // Construct an Action from a specified callable. - // This cannot take std::function directly, because then Action would not be - // directly constructible from lambda (it would require two conversions). - template , G>::value>::type> - Action(G&& fun) : fun_(::std::forward(fun)) {} // NOLINT -#endif - - // Constructs an Action from its implementation. + // Constructs an Action from its implementation. A NULL impl is + // used to represent the "do-default" action. explicit Action(ActionInterface* impl) : impl_(impl) {} + // Copy constructor. + Action(const Action& action) : impl_(action.impl_) {} + // This constructor allows us to turn an Action object into an // Action, as long as F's arguments can be implicitly converted // to Func's and Func's return type can be implicitly converted to @@ -383,13 +377,7 @@ class Action { explicit Action(const Action& action); // Returns true iff this is the DoDefault() action. - bool IsDoDefault() const { -#if GTEST_LANG_CXX11 - return impl_ == nullptr && fun_ == nullptr; -#else - return impl_ == NULL; -#endif - } + bool IsDoDefault() const { return impl_.get() == NULL; } // Performs the action. Note that this method is const even though // the corresponding method in ActionInterface is not. The reason @@ -397,15 +385,14 @@ class Action { // another concrete action, not that the concrete action it binds to // cannot change state. (Think of the difference between a const // pointer and a pointer to const.) - Result Perform(ArgumentTuple args) const { - if (IsDoDefault()) { - internal::IllegalDoDefault(__FILE__, __LINE__); - } -#if GTEST_LANG_CXX11 - if (fun_ != nullptr) { - return internal::Apply(fun_, ::std::move(args)); - } -#endif + Result Perform(const ArgumentTuple& args) const { + internal::Assert( + !IsDoDefault(), __FILE__, __LINE__, + "You are using DoDefault() inside a composite action like " + "DoAll() or WithArgs(). This is not supported for technical " + "reasons. Please instead spell out the default action, or " + "assign the default action to an Action variable and use " + "the variable in various places."); return impl_->Perform(args); } @@ -413,18 +400,6 @@ class Action { template friend class internal::ActionAdaptor; - template - friend class Action; - - // In C++11, Action can be implemented either as a generic functor (through - // std::function), or legacy ActionInterface. In C++98, only ActionInterface - // is available. The invariants are as follows: - // * in C++98, impl_ is null iff this is the default action - // * in C++11, at most one of fun_ & impl_ may be nonnull; both are null iff - // this is the default action -#if GTEST_LANG_CXX11 - ::std::function fun_; -#endif internal::linked_ptr > impl_; }; @@ -556,9 +531,6 @@ struct ByMoveWrapper { // statement, and conversion of the result of Return to Action is a // good place for that. // -// The real life example of the above scenario happens when an invocation -// of gtl::Container() is passed into Return. -// template class ReturnAction { public: @@ -778,7 +750,7 @@ class DoDefaultAction { // This template type conversion operator allows DoDefault() to be // used in any function. template - operator Action() const { return Action(); } // NOLINT + operator Action() const { return Action(NULL); } }; // Implements the Assign action to set a given pointer referent to a @@ -914,28 +886,6 @@ class InvokeMethodWithoutArgsAction { GTEST_DISALLOW_ASSIGN_(InvokeMethodWithoutArgsAction); }; -// Implements the InvokeWithoutArgs(callback) action. -template -class InvokeCallbackWithoutArgsAction { - public: - // The c'tor takes ownership of the callback. - explicit InvokeCallbackWithoutArgsAction(CallbackType* callback) - : callback_(callback) { - callback->CheckIsRepeatable(); // Makes sure the callback is permanent. - } - - // This type conversion operator template allows Invoke(callback) to - // be used wherever the callback's return type can be implicitly - // converted to that of the mock function. - template - Result Perform(const ArgumentTuple&) const { return callback_->Run(); } - - private: - const internal::linked_ptr callback_; - - GTEST_DISALLOW_ASSIGN_(InvokeCallbackWithoutArgsAction); -}; - // Implements the IgnoreResult(action) action. template class IgnoreResultAction { @@ -1103,13 +1053,7 @@ typedef internal::IgnoredValue Unused; template template Action::Action(const Action& from) - : -#if GTEST_LANG_CXX11 - fun_(from.fun_), -#endif - impl_(from.impl_ == NULL ? NULL - : new internal::ActionAdaptor(from)) { -} + : impl_(new internal::ActionAdaptor(from)) {} // Creates an action that returns 'value'. 'value' is passed by value // instead of const reference - otherwise Return("string literal") diff --git a/googlemock/include/gmock/gmock-generated-actions.h b/googlemock/include/gmock/gmock-generated-actions.h index b35303e..be4ebe4 100644 --- a/googlemock/include/gmock/gmock-generated-actions.h +++ b/googlemock/include/gmock/gmock-generated-actions.h @@ -1,6 +1,4 @@ -// This file was GENERATED by command: -// pump.py gmock-generated-actions.h.pump -// DO NOT EDIT BY HAND!!! +// This file was GENERATED by a script. DO NOT EDIT BY HAND!!! // Copyright 2007, Google Inc. // All rights reserved. diff --git a/googlemock/include/gmock/gmock-generated-actions.h.pump b/googlemock/include/gmock/gmock-generated-actions.h.pump index e0c2135..712f65d 100644 --- a/googlemock/include/gmock/gmock-generated-actions.h.pump +++ b/googlemock/include/gmock/gmock-generated-actions.h.pump @@ -1,5 +1,5 @@ $$ -*- mode: c++; -*- -$$ This is a Pump source file. Please use Pump to convert it to +$$ This is a Pump source file. Please use Pump to convert it to $$ gmock-generated-actions.h. $$ $var n = 10 $$ The maximum arity we support. diff --git a/googlemock/include/gmock/gmock-generated-matchers.h.pump b/googlemock/include/gmock/gmock-generated-matchers.h.pump index 4fe0a61..25d2da9 100644 --- a/googlemock/include/gmock/gmock-generated-matchers.h.pump +++ b/googlemock/include/gmock/gmock-generated-matchers.h.pump @@ -1,6 +1,6 @@ $$ -*- mode: c++; -*- -$$ This is a Pump source file. Please use Pump to convert -$$ it to gmock-generated-matchers.h. +$$ This is a Pump source file. Please use Pump to convert it to +$$ gmock-generated-actions.h. $$ $var n = 10 $$ The maximum arity we support. $$ }} This line fixes auto-indentation of the following code in Emacs. diff --git a/googlemock/include/gmock/gmock-generated-nice-strict.h.pump b/googlemock/include/gmock/gmock-generated-nice-strict.h.pump index 378c40f..4973c35 100644 --- a/googlemock/include/gmock/gmock-generated-nice-strict.h.pump +++ b/googlemock/include/gmock/gmock-generated-nice-strict.h.pump @@ -1,6 +1,6 @@ $$ -*- mode: c++; -*- -$$ This is a Pump source file. Please use Pump to convert -$$ it to gmock-generated-nice-strict.h. +$$ This is a Pump source file. Please use Pump to convert it to +$$ gmock-generated-nice-strict.h. $$ $var n = 10 $$ The maximum arity we support. // Copyright 2008, Google Inc. diff --git a/googlemock/include/gmock/gmock-more-matchers.h b/googlemock/include/gmock/gmock-more-matchers.h index 6d810eb..01298cf 100644 --- a/googlemock/include/gmock/gmock-more-matchers.h +++ b/googlemock/include/gmock/gmock-more-matchers.h @@ -46,11 +46,8 @@ namespace testing { // Silence C4100 (unreferenced formal // parameter) for MSVC #ifdef _MSC_VER -# pragma warning(push) # pragma warning(disable:4100) #if (_MSC_VER == 1900) -// and silence C4800 (C4800: 'int *const ': forcing value -// to bool 'true' or 'false') for MSVC 14 # pragma warning(disable:4800) #endif #endif @@ -81,11 +78,6 @@ MATCHER(IsFalse, negation ? "is true" : "is false") { return !static_cast(arg); } -#ifdef _MSC_VER -# pragma warning(pop) -#endif - - } // namespace testing #endif // GMOCK_GMOCK_MORE_MATCHERS_H_ diff --git a/googlemock/include/gmock/internal/custom/gmock-generated-actions.h.pump b/googlemock/include/gmock/internal/custom/gmock-generated-actions.h.pump index 03cfd8c..d26c8a0 100644 --- a/googlemock/include/gmock/internal/custom/gmock-generated-actions.h.pump +++ b/googlemock/include/gmock/internal/custom/gmock-generated-actions.h.pump @@ -1,5 +1,5 @@ $$ -*- mode: c++; -*- -$$ This is a Pump source file. Please use Pump to convert +$$ This is a Pump source file (http://go/pump). Please use Pump to convert $$ it to callback-actions.h. $$ $var max_callback_arity = 5 diff --git a/googlemock/include/gmock/internal/gmock-internal-utils.h b/googlemock/include/gmock/internal/gmock-internal-utils.h index 20c95c6..37ceb54 100644 --- a/googlemock/include/gmock/internal/gmock-internal-utils.h +++ b/googlemock/include/gmock/internal/gmock-internal-utils.h @@ -48,14 +48,6 @@ namespace testing { namespace internal { -// Silence MSVC C4100 (unreferenced formal parameter) and -// C4805('==': unsafe mix of type 'const int' and type 'const bool') -#ifdef _MSC_VER -# pragma warning(push) -# pragma warning(disable:4100) -# pragma warning(disable:4805) -#endif - // Joins a vector of strings as if they are fields of a tuple; returns // the joined string. GTEST_API_ std::string JoinAsTuple(const Strings& fields); @@ -518,7 +510,7 @@ struct BooleanConstant {}; // Emit an assertion failure due to incorrect DoDefault() usage. Out-of-lined to // reduce code size. -GTEST_API_ void IllegalDoDefault(const char* file, int line); +void IllegalDoDefault(const char* file, int line); #if GTEST_LANG_CXX11 // Helper types for Apply() below. @@ -547,12 +539,6 @@ auto Apply(F&& f, Tuple&& args) make_int_pack::value>()); } #endif - - -#ifdef _MSC_VER -# pragma warning(pop) -#endif - } // namespace internal } // namespace testing diff --git a/googlemock/src/gmock-internal-utils.cc b/googlemock/src/gmock-internal-utils.cc index 3fca3f2..20c5a8d 100644 --- a/googlemock/src/gmock-internal-utils.cc +++ b/googlemock/src/gmock-internal-utils.cc @@ -188,7 +188,7 @@ GTEST_API_ void Log(LogSeverity severity, const std::string& message, std::cout << ::std::flush; } -GTEST_API_ void IllegalDoDefault(const char* file, int line) { +void IllegalDoDefault(const char* file, int line) { internal::Assert( false, file, line, "You are using DoDefault() inside a composite action like " diff --git a/googlemock/src/gmock-spec-builders.cc b/googlemock/src/gmock-spec-builders.cc index 22d002f..b97bad0 100644 --- a/googlemock/src/gmock-spec-builders.cc +++ b/googlemock/src/gmock-spec-builders.cc @@ -49,15 +49,6 @@ # include // NOLINT #endif -// Silence C4800 (C4800: 'int *const ': forcing value -// to bool 'true' or 'false') for MSVC 14,15 -#ifdef _MSC_VER -#if _MSC_VER <= 1900 -# pragma warning(push) -# pragma warning(disable:4800) -#endif -#endif - namespace testing { namespace internal { @@ -875,9 +866,3 @@ InSequence::~InSequence() { } } // namespace testing - -#ifdef _MSC_VER -#if _MSC_VER <= 1900 -# pragma warning(pop) -#endif -#endif diff --git a/googlemock/test/gmock-actions_test.cc b/googlemock/test/gmock-actions_test.cc index 5dd4846..4601157 100644 --- a/googlemock/test/gmock-actions_test.cc +++ b/googlemock/test/gmock-actions_test.cc @@ -33,15 +33,6 @@ // // This file tests the built-in actions. -// Silence C4800 (C4800: 'int *const ': forcing value -// to bool 'true' or 'false') for MSVC 14,15 -#ifdef _MSC_VER -#if _MSC_VER <= 1900 -# pragma warning(push) -# pragma warning(disable:4800) -#endif -#endif - #include "gmock/gmock-actions.h" #include #include @@ -74,7 +65,6 @@ using testing::ReturnRef; using testing::ReturnRefOfCopy; using testing::SetArgPointee; using testing::SetArgumentPointee; -using testing::Unused; using testing::_; using testing::get; using testing::internal::BuiltInDefaultValue; @@ -715,8 +705,6 @@ class MockClass { MOCK_METHOD0(MakeUniqueBase, std::unique_ptr()); MOCK_METHOD0(MakeVectorUnique, std::vector>()); MOCK_METHOD1(TakeUnique, int(std::unique_ptr)); - MOCK_METHOD2(TakeUnique, - int(const std::unique_ptr&, std::unique_ptr)); #endif private: @@ -768,7 +756,7 @@ TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) { } // Tests that DoDefault() returns the default value set by -// DefaultValue::Set() when it's not overriden by an ON_CALL(). +// DefaultValue::Set() when it's not overridden by an ON_CALL(). TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) { DefaultValue::Set(1); MockClass mock; @@ -1423,152 +1411,6 @@ TEST(MockMethodTest, CanReturnMoveOnlyValue_Invoke) { EXPECT_EQ(7, *vresult[0]); } -TEST(MockMethodTest, CanTakeMoveOnlyValue) { - MockClass mock; - auto make = [](int i) { return std::unique_ptr(new int(i)); }; - - EXPECT_CALL(mock, TakeUnique(_)).WillRepeatedly([](std::unique_ptr i) { - return *i; - }); - // DoAll() does not compile, since it would move from its arguments twice. - // EXPECT_CALL(mock, TakeUnique(_, _)) - // .WillRepeatedly(DoAll(Invoke([](std::unique_ptr j) {}), - // Return(1))); - EXPECT_CALL(mock, TakeUnique(testing::Pointee(7))) - .WillOnce(Return(-7)) - .RetiresOnSaturation(); - EXPECT_CALL(mock, TakeUnique(testing::IsNull())) - .WillOnce(Return(-1)) - .RetiresOnSaturation(); - - EXPECT_EQ(5, mock.TakeUnique(make(5))); - EXPECT_EQ(-7, mock.TakeUnique(make(7))); - EXPECT_EQ(7, mock.TakeUnique(make(7))); - EXPECT_EQ(7, mock.TakeUnique(make(7))); - EXPECT_EQ(-1, mock.TakeUnique({})); - - // Some arguments are moved, some passed by reference. - auto lvalue = make(6); - EXPECT_CALL(mock, TakeUnique(_, _)) - .WillOnce([](const std::unique_ptr& i, std::unique_ptr j) { - return *i * *j; - }); - EXPECT_EQ(42, mock.TakeUnique(lvalue, make(7))); - - // The unique_ptr can be saved by the action. - std::unique_ptr saved; - EXPECT_CALL(mock, TakeUnique(_)).WillOnce([&saved](std::unique_ptr i) { - saved = std::move(i); - return 0; - }); - EXPECT_EQ(0, mock.TakeUnique(make(42))); - EXPECT_EQ(42, *saved); -} - #endif // GTEST_HAS_STD_UNIQUE_PTR_ -#if GTEST_LANG_CXX11 -// Tests for std::function based action. - -int Add(int val, int& ref, int* ptr) { // NOLINT - int result = val + ref + *ptr; - ref = 42; - *ptr = 43; - return result; -} - -int Deref(std::unique_ptr ptr) { return *ptr; } - -struct Double { - template - T operator()(T t) { return 2 * t; } -}; - -std::unique_ptr UniqueInt(int i) { - return std::unique_ptr(new int(i)); -} - -TEST(FunctorActionTest, ActionFromFunction) { - Action a = &Add; - int x = 1, y = 2, z = 3; - EXPECT_EQ(6, a.Perform(std::forward_as_tuple(x, y, &z))); - EXPECT_EQ(42, y); - EXPECT_EQ(43, z); - - Action)> a1 = &Deref; - EXPECT_EQ(7, a1.Perform(std::make_tuple(UniqueInt(7)))); -} - -TEST(FunctorActionTest, ActionFromLambda) { - Action a1 = [](bool b, int i) { return b ? i : 0; }; - EXPECT_EQ(5, a1.Perform(make_tuple(true, 5))); - EXPECT_EQ(0, a1.Perform(make_tuple(false, 5))); - - std::unique_ptr saved; - Action)> a2 = [&saved](std::unique_ptr p) { - saved = std::move(p); - }; - a2.Perform(make_tuple(UniqueInt(5))); - EXPECT_EQ(5, *saved); -} - -TEST(FunctorActionTest, PolymorphicFunctor) { - Action ai = Double(); - EXPECT_EQ(2, ai.Perform(make_tuple(1))); - Action ad = Double(); // Double? Double double! - EXPECT_EQ(3.0, ad.Perform(make_tuple(1.5))); -} - -TEST(FunctorActionTest, TypeConversion) { - // Numeric promotions are allowed. - const Action a1 = [](int i) { return i > 1; }; - const Action a2 = Action(a1); - EXPECT_EQ(1, a1.Perform(make_tuple(42))); - EXPECT_EQ(0, a2.Perform(make_tuple(42))); - - // Implicit constructors are allowed. - const Action s1 = [](std::string s) { return !s.empty(); }; - const Action s2 = Action(s1); - EXPECT_EQ(0, s2.Perform(make_tuple(""))); - EXPECT_EQ(1, s2.Perform(make_tuple("hello"))); - - // Also between the lambda and the action itself. - const Action x = [](Unused) { return 42; }; - EXPECT_TRUE(x.Perform(make_tuple("hello"))); -} - -TEST(FunctorActionTest, UnusedArguments) { - // Verify that users can ignore uninteresting arguments. - Action, const int&)> a = - [](int i, Unused, Unused) { return 2 * i; }; - EXPECT_EQ(6, a.Perform(make_tuple(3, UniqueInt(7), 9))); -} - -// Test that basic built-in actions work with move-only arguments. -// TODO(rburny): Currently, almost all ActionInterface-based actions will not -// work, even if they only try to use other, copyable arguments. Implement them -// if necessary (but note that DoAll cannot work on non-copyable types anyway - -// so maybe it's better to make users use lambdas instead. -TEST(MoveOnlyArgumentsTest, ReturningActions) { - Action)> a = Return(1); - EXPECT_EQ(1, a.Perform(make_tuple(nullptr))); - - a = testing::WithoutArgs([]() { return 7; }); - EXPECT_EQ(7, a.Perform(make_tuple(nullptr))); - - Action, int*)> a2 = testing::SetArgPointee<1>(3); - int x = 0; - a2.Perform(make_tuple(nullptr, &x)); - EXPECT_EQ(x, 3); -} - -#endif // GTEST_LANG_CXX11 - } // Unnamed namespace - -#ifdef _MSC_VER -#if _MSC_VER == 1900 -# pragma warning(pop) -#endif -#endif - diff --git a/googlemock/test/gmock_output_test_.cc b/googlemock/test/gmock_output_test_.cc index 1b59eb3..ca628df 100644 --- a/googlemock/test/gmock_output_test_.cc +++ b/googlemock/test/gmock_output_test_.cc @@ -39,12 +39,6 @@ #include "gtest/gtest.h" -// Silence C4100 (unreferenced formal parameter) -#ifdef _MSC_VER -# pragma warning(push) -# pragma warning(disable:4100) -#endif - using testing::_; using testing::AnyNumber; using testing::Ge; @@ -304,7 +298,3 @@ int main(int argc, char **argv) { TestCatchesLeakedMocksInAdHocTests(); return RUN_ALL_TESTS(); } - -#ifdef _MSC_VER -# pragma warning(pop) -#endif diff --git a/googletest/include/gtest/gtest.h b/googletest/include/gtest/gtest.h index 1c39310..26e787d 100644 --- a/googletest/include/gtest/gtest.h +++ b/googletest/include/gtest/gtest.h @@ -82,15 +82,6 @@ namespace testing { -// Silence C4100 (unreferenced formal parameter) and 4805 -// unsafe mix of type 'const int' and type 'const bool' -#ifdef _MSC_VER -# pragma warning(push) -# pragma warning(disable:4805) -# pragma warning(disable:4100) -#endif - - // Declares the flags. // This flag temporary enables the disabled tests. @@ -2307,10 +2298,6 @@ bool StaticAssertTypeEq() { // Tries to determine an appropriate directory for the platform. GTEST_API_ std::string TempDir(); -#ifdef _MSC_VER -# pragma warning(pop) -#endif - } // namespace testing // Use this function in main() to run all tests. It returns 0 if all -- cgit v0.12