diff options
Diffstat (limited to 'googlemock/test')
-rw-r--r-- | googlemock/test/BUILD.bazel | 4 | ||||
-rw-r--r-- | googlemock/test/gmock-actions_test.cc | 53 | ||||
-rw-r--r-- | googlemock/test/gmock-cardinalities_test.cc | 4 | ||||
-rw-r--r-- | googlemock/test/gmock-function-mocker_test.cc | 35 | ||||
-rw-r--r-- | googlemock/test/gmock-generated-actions_test.cc | 6 | ||||
-rw-r--r-- | googlemock/test/gmock-generated-function-mockers_test.cc | 22 | ||||
-rw-r--r-- | googlemock/test/gmock-generated-internal-utils_test.cc | 130 | ||||
-rw-r--r-- | googlemock/test/gmock-generated-matchers_test.cc | 148 | ||||
-rw-r--r-- | googlemock/test/gmock-internal-utils_test.cc | 124 | ||||
-rw-r--r-- | googlemock/test/gmock-matchers_test.cc | 683 | ||||
-rw-r--r-- | googlemock/test/gmock-nice-strict_test.cc | 60 | ||||
-rw-r--r-- | googlemock/test/gmock-spec-builders_test.cc | 7 | ||||
-rw-r--r-- | googlemock/test/gmock_all_test.cc | 1 | ||||
-rwxr-xr-x | googlemock/test/gmock_output_test.py | 4 | ||||
-rw-r--r-- | googlemock/test/gmock_output_test_golden.txt | 8 |
15 files changed, 529 insertions, 760 deletions
diff --git a/googlemock/test/BUILD.bazel b/googlemock/test/BUILD.bazel index 95a6c269..da95ed58 100644 --- a/googlemock/test/BUILD.bazel +++ b/googlemock/test/BUILD.bazel @@ -32,6 +32,9 @@ # # Bazel Build for Google C++ Testing Framework(Google Test)-googlemock +load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_test") +load("@rules_python//python:defs.bzl", "py_library", "py_test") + licenses(["notice"]) # Tests for GMock itself @@ -95,6 +98,7 @@ py_test( ":gmock_output_test_", ":gmock_output_test_golden.txt", ], + python_version = "PY2", deps = [":gmock_test_utils"], ) diff --git a/googlemock/test/gmock-actions_test.cc b/googlemock/test/gmock-actions_test.cc index 938a11bf..f63c8c5a 100644 --- a/googlemock/test/gmock-actions_test.cc +++ b/googlemock/test/gmock-actions_test.cc @@ -54,12 +54,14 @@ 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; @@ -75,7 +77,6 @@ using testing::SetArgPointee; using testing::SetArgumentPointee; using testing::Unused; using testing::WithArgs; -using testing::_; using testing::internal::BuiltInDefaultValue; using testing::internal::Int64; using testing::internal::UInt64; @@ -104,10 +105,6 @@ TEST(BuiltInDefaultValueTest, IsZeroForNumericTypes) { EXPECT_EQ(0U, BuiltInDefaultValue<unsigned char>::Get()); EXPECT_EQ(0, BuiltInDefaultValue<signed char>::Get()); EXPECT_EQ(0, BuiltInDefaultValue<char>::Get()); -#if GMOCK_HAS_SIGNED_WCHAR_T_ - EXPECT_EQ(0U, BuiltInDefaultValue<unsigned wchar_t>::Get()); - EXPECT_EQ(0, BuiltInDefaultValue<signed wchar_t>::Get()); -#endif #if GMOCK_WCHAR_T_IS_NATIVE_ #if !defined(__WCHAR_UNSIGNED__) EXPECT_EQ(0, BuiltInDefaultValue<wchar_t>::Get()); @@ -136,10 +133,6 @@ TEST(BuiltInDefaultValueTest, ExistsForNumericTypes) { EXPECT_TRUE(BuiltInDefaultValue<unsigned char>::Exists()); EXPECT_TRUE(BuiltInDefaultValue<signed char>::Exists()); EXPECT_TRUE(BuiltInDefaultValue<char>::Exists()); -#if GMOCK_HAS_SIGNED_WCHAR_T_ - EXPECT_TRUE(BuiltInDefaultValue<unsigned wchar_t>::Exists()); - EXPECT_TRUE(BuiltInDefaultValue<signed wchar_t>::Exists()); -#endif #if GMOCK_WCHAR_T_IS_NATIVE_ EXPECT_TRUE(BuiltInDefaultValue<wchar_t>::Exists()); #endif @@ -171,20 +164,12 @@ TEST(BuiltInDefaultValueTest, BoolExists) { // Tests that BuiltInDefaultValue<T>::Get() returns "" when T is a // string type. TEST(BuiltInDefaultValueTest, IsEmptyStringForString) { -#if GTEST_HAS_GLOBAL_STRING - EXPECT_EQ("", BuiltInDefaultValue< ::string>::Get()); -#endif // GTEST_HAS_GLOBAL_STRING - EXPECT_EQ("", BuiltInDefaultValue< ::std::string>::Get()); } // Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a // string type. TEST(BuiltInDefaultValueTest, ExistsForString) { -#if GTEST_HAS_GLOBAL_STRING - EXPECT_TRUE(BuiltInDefaultValue< ::string>::Exists()); -#endif // GTEST_HAS_GLOBAL_STRING - EXPECT_TRUE(BuiltInDefaultValue< ::std::string>::Exists()); } @@ -220,7 +205,6 @@ class MyNonDefaultConstructible { int value_; }; -#if GTEST_LANG_CXX11 TEST(BuiltInDefaultValueTest, ExistsForDefaultConstructibleType) { EXPECT_TRUE(BuiltInDefaultValue<MyDefaultConstructible>::Exists()); @@ -230,7 +214,6 @@ TEST(BuiltInDefaultValueTest, IsDefaultConstructedForDefaultConstructibleType) { EXPECT_EQ(42, BuiltInDefaultValue<MyDefaultConstructible>::Get().value()); } -#endif // GTEST_LANG_CXX11 TEST(BuiltInDefaultValueTest, DoesNotExistForNonDefaultConstructibleType) { EXPECT_FALSE(BuiltInDefaultValue<MyNonDefaultConstructible>::Exists()); @@ -300,7 +283,6 @@ TEST(DefaultValueDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) { }, ""); } -#if GTEST_HAS_STD_UNIQUE_PTR_ TEST(DefaultValueTest, GetWorksForMoveOnlyIfSet) { EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Exists()); EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Get() == nullptr); @@ -311,7 +293,6 @@ TEST(DefaultValueTest, GetWorksForMoveOnlyIfSet) { std::unique_ptr<int> i = DefaultValue<std::unique_ptr<int>>::Get(); EXPECT_EQ(42, *i); } -#endif // GTEST_HAS_STD_UNIQUE_PTR_ // Tests that DefaultValue<void>::Get() returns void. TEST(DefaultValueTest, GetWorksForVoid) { @@ -448,19 +429,12 @@ class IsNotZero : public ActionInterface<bool(int)> { // NOLINT } }; -#if !GTEST_OS_SYMBIAN -// Compiling this test on Nokia's Symbian compiler fails with: -// 'Result' is not a member of class 'testing::internal::Function<int>' -// (point of instantiation: '@unnamed@gmock_actions_test_cc@:: -// ActionTest_CanBeConvertedToOtherActionType_Test::TestBody()') -// with no obvious fix. TEST(ActionTest, CanBeConvertedToOtherActionType) { const Action<bool(int)> a1(new IsNotZero); // NOLINT const Action<int(char)> a2 = Action<int(char)>(a1); // NOLINT EXPECT_EQ(1, a2.Perform(std::make_tuple('a'))); EXPECT_EQ(0, a2.Perform(std::make_tuple('\0'))); } -#endif // !GTEST_OS_SYMBIAN // The following two classes are for testing MakePolymorphicAction(). @@ -643,7 +617,6 @@ TEST(ReturnNullTest, WorksInPointerReturningFunction) { EXPECT_TRUE(a2.Perform(std::make_tuple(true)) == nullptr); } -#if GTEST_HAS_STD_UNIQUE_PTR_ // Tests that ReturnNull() returns NULL for shared_ptr and unique_ptr returning // functions. TEST(ReturnNullTest, WorksInSmartPointerReturningFunction) { @@ -653,7 +626,6 @@ TEST(ReturnNullTest, WorksInSmartPointerReturningFunction) { const Action<std::shared_ptr<int>(std::string)> a2 = ReturnNull(); EXPECT_TRUE(a2.Perform(std::make_tuple("foo")) == nullptr); } -#endif // GTEST_HAS_STD_UNIQUE_PTR_ // Tests that ReturnRef(v) works for reference types. TEST(ReturnRefTest, WorksForReference) { @@ -706,14 +678,12 @@ class MockClass { MOCK_METHOD1(IntFunc, int(bool flag)); // NOLINT MOCK_METHOD0(Foo, MyNonDefaultConstructible()); -#if GTEST_HAS_STD_UNIQUE_PTR_ MOCK_METHOD0(MakeUnique, std::unique_ptr<int>()); MOCK_METHOD0(MakeUniqueBase, std::unique_ptr<Base>()); MOCK_METHOD0(MakeVectorUnique, std::vector<std::unique_ptr<int>>()); MOCK_METHOD1(TakeUnique, int(std::unique_ptr<int>)); MOCK_METHOD2(TakeUnique, int(const std::unique_ptr<int>&, std::unique_ptr<int>)); -#endif private: GTEST_DISALLOW_COPY_AND_ASSIGN_(MockClass); @@ -813,9 +783,7 @@ TEST(SetArgPointeeTest, SetsTheNthPointee) { EXPECT_EQ('a', ch); } -#if !((GTEST_GCC_VER_ && GTEST_GCC_VER_ < 40000) || GTEST_OS_SYMBIAN) // Tests that SetArgPointee<N>() accepts a string literal. -// GCC prior to v4.0 and the Symbian compiler do not support this. TEST(SetArgPointeeTest, AcceptsStringLiteral) { typedef void MyFunction(std::string*, const char**); Action<MyFunction> a = SetArgPointee<0>("hi"); @@ -849,7 +817,6 @@ TEST(SetArgPointeeTest, AcceptsWideStringLiteral) { # endif } -#endif // Tests that SetArgPointee<N>() accepts a char pointer. TEST(SetArgPointeeTest, AcceptsCharPointer) { @@ -1182,13 +1149,12 @@ TEST_F(SetErrnoAndReturnTest, CompatibleTypes) { // Tests ByRef(). -// Tests that ReferenceWrapper<T> is copyable. +// Tests that the result of ByRef() is copyable. TEST(ByRefTest, IsCopyable) { const std::string s1 = "Hi"; const std::string s2 = "Hello"; - ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper = - ByRef(s1); + auto ref_wrapper = ByRef(s1); const std::string& r1 = ref_wrapper; EXPECT_EQ(&s1, &r1); @@ -1197,8 +1163,7 @@ TEST(ByRefTest, IsCopyable) { const std::string& r2 = ref_wrapper; EXPECT_EQ(&s2, &r2); - ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper1 = - ByRef(s1); + auto ref_wrapper1 = ByRef(s1); // Copies ref_wrapper1 to ref_wrapper. ref_wrapper = ref_wrapper1; const std::string& r3 = ref_wrapper; @@ -1265,7 +1230,6 @@ TEST(ByRefTest, PrintsCorrectly) { EXPECT_EQ(expected.str(), actual.str()); } -#if GTEST_HAS_STD_UNIQUE_PTR_ std::unique_ptr<int> UniquePtrSource() { return std::unique_ptr<int>(new int(19)); @@ -1378,9 +1342,7 @@ TEST(MockMethodTest, CanTakeMoveOnlyValue) { 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 @@ -1459,10 +1421,6 @@ TEST(FunctorActionTest, UnusedArguments) { } // Test that basic built-in actions work with move-only arguments. -// FIXME: 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<int(std::unique_ptr<int>)> a = Return(1); EXPECT_EQ(1, a.Perform(std::make_tuple(nullptr))); @@ -1476,7 +1434,6 @@ TEST(MoveOnlyArgumentsTest, ReturningActions) { EXPECT_EQ(x, 3); } -#endif // GTEST_LANG_CXX11 } // Unnamed namespace diff --git a/googlemock/test/gmock-cardinalities_test.cc b/googlemock/test/gmock-cardinalities_test.cc index 60fd06a3..66042d40 100644 --- a/googlemock/test/gmock-cardinalities_test.cc +++ b/googlemock/test/gmock-cardinalities_test.cc @@ -395,12 +395,12 @@ TEST(ExactlyTest, HasCorrectBounds) { class EvenCardinality : public CardinalityInterface { public: - // Returns true iff call_count calls will satisfy this cardinality. + // Returns true if call_count calls will satisfy this cardinality. bool IsSatisfiedByCallCount(int call_count) const override { return (call_count % 2 == 0); } - // Returns true iff call_count calls will saturate this cardinality. + // Returns true if call_count calls will saturate this cardinality. bool IsSaturatedByCallCount(int /* call_count */) const override { return false; } diff --git a/googlemock/test/gmock-function-mocker_test.cc b/googlemock/test/gmock-function-mocker_test.cc index 007e86c2..fbc5d5b2 100644 --- a/googlemock/test/gmock-function-mocker_test.cc +++ b/googlemock/test/gmock-function-mocker_test.cc @@ -45,13 +45,6 @@ #include "gmock/gmock.h" #include "gtest/gtest.h" -// There is a bug in MSVC (fixed in VS 2008) that prevents creating a -// mock for a function with const arguments, so we don't test such -// cases for MSVC versions older than 2008. -#if !GTEST_OS_WINDOWS || (_MSC_VER >= 1500) -# define GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS -#endif // !GTEST_OS_WINDOWS || (_MSC_VER >= 1500) - namespace testing { namespace gmock_function_mocker_test { @@ -69,6 +62,15 @@ using testing::Return; using testing::ReturnRef; using testing::TypedEq; +template<typename T> +class TemplatedCopyable { + public: + TemplatedCopyable() {} + + template <typename U> + TemplatedCopyable(const U& other) {} // NOLINT +}; + class FooInterface { public: virtual ~FooInterface() {} @@ -84,9 +86,7 @@ class FooInterface { virtual bool TakesNonConstReference(int& n) = 0; // NOLINT virtual std::string TakesConstReference(const int& n) = 0; -#ifdef GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS virtual bool TakesConst(const int x) = 0; -#endif // GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS virtual int OverloadedOnArgumentNumber() = 0; virtual int OverloadedOnArgumentNumber(int n) = 0; @@ -99,6 +99,7 @@ class FooInterface { virtual int TypeWithHole(int (*func)()) = 0; virtual int TypeWithComma(const std::map<int, std::string>& a_map) = 0; + virtual int TypeWithTemplatedCopyCtor(const TemplatedCopyable<int>&) = 0; #if GTEST_OS_WINDOWS STDMETHOD_(int, CTNullary)() = 0; @@ -137,10 +138,7 @@ class MockFoo : public FooInterface { MOCK_METHOD(bool, TakesNonConstReference, (int&)); // NOLINT MOCK_METHOD(std::string, TakesConstReference, (const int&)); - -#ifdef GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS MOCK_METHOD(bool, TakesConst, (const int)); // NOLINT -#endif // Tests that the function return type can contain unprotected comma. MOCK_METHOD((std::map<int, std::string>), ReturnTypeWithComma, (), ()); @@ -158,6 +156,8 @@ class MockFoo : public FooInterface { MOCK_METHOD(int, TypeWithHole, (int (*)()), ()); // NOLINT MOCK_METHOD(int, TypeWithComma, ((const std::map<int, std::string>&))); + MOCK_METHOD(int, TypeWithTemplatedCopyCtor, + (const TemplatedCopyable<int>&)); // NOLINT #if GTEST_OS_WINDOWS MOCK_METHOD(int, CTNullary, (), (Calltype(STDMETHODCALLTYPE))); @@ -248,7 +248,6 @@ TEST_F(MockMethodFunctionMockerTest, MocksFunctionWithConstReferenceArgument) { EXPECT_EQ("Hello", foo_->TakesConstReference(a)); } -#ifdef GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS // Tests mocking a function that takes a const variable. TEST_F(MockMethodFunctionMockerTest, MocksFunctionWithConstArgument) { EXPECT_CALL(mock_foo_, TakesConst(Lt(10))) @@ -256,7 +255,6 @@ TEST_F(MockMethodFunctionMockerTest, MocksFunctionWithConstArgument) { EXPECT_FALSE(foo_->TakesConst(5)); } -#endif // GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS // Tests mocking functions overloaded on the number of arguments. TEST_F(MockMethodFunctionMockerTest, MocksFunctionsOverloadedOnArgumentNumber) { @@ -302,6 +300,11 @@ TEST_F(MockMethodFunctionMockerTest, MocksReturnTypeWithComma) { EXPECT_EQ(a_map, mock_foo_.ReturnTypeWithComma(42)); } +TEST_F(MockMethodFunctionMockerTest, MocksTypeWithTemplatedCopyCtor) { + EXPECT_CALL(mock_foo_, TypeWithTemplatedCopyCtor(_)).WillOnce(Return(true)); + EXPECT_TRUE(foo_->TypeWithTemplatedCopyCtor(TemplatedCopyable<int>())); +} + #if GTEST_OS_WINDOWS // Tests mocking a nullary function with calltype. TEST_F(MockMethodFunctionMockerTest, MocksNullaryFunctionWithCallType) { @@ -533,7 +536,7 @@ TEST(MockMethodOverloadedMockMethodTest, CanOverloadOnArgNumberInMacroBody) { #define MY_MOCK_METHODS2_ \ MOCK_CONST_METHOD1(Overloaded, int(int n)); \ - MOCK_METHOD1(Overloaded, int(int n)); + MOCK_METHOD1(Overloaded, int(int n)) class MockOverloadedOnConstness { public: @@ -598,7 +601,6 @@ TEST(MockMethodMockFunctionTest, WorksFor10Arguments) { EXPECT_EQ(2, foo.Call(true, 'a', 0, 0, 0, 0, 0, 'b', 1, false)); } -#if GTEST_HAS_STD_FUNCTION_ TEST(MockMethodMockFunctionTest, AsStdFunction) { MockFunction<int(int)> foo; auto call = [](const std::function<int(int)> &f, int i) { @@ -630,7 +632,6 @@ TEST(MockMethodMockFunctionTest, AsStdFunctionWithReferenceParameter) { EXPECT_EQ(-1, call(foo.AsStdFunction(), i)); } -#endif // GTEST_HAS_STD_FUNCTION_ struct MockMethodSizes0 { MOCK_METHOD(void, func, ()); diff --git a/googlemock/test/gmock-generated-actions_test.cc b/googlemock/test/gmock-generated-actions_test.cc index 0fe43ab3..4c649a7e 100644 --- a/googlemock/test/gmock-generated-actions_test.cc +++ b/googlemock/test/gmock-generated-actions_test.cc @@ -428,11 +428,12 @@ TEST(DoAllTest, TenActions) { // the macro definition, as the warnings are generated when the macro // is expanded and macro expansion cannot contain #pragma. Therefore // we suppress them here. +// Also suppress C4503 decorated name length exceeded, name was truncated #ifdef _MSC_VER # pragma warning(push) # pragma warning(disable:4100) +# pragma warning(disable:4503) #endif - // Tests the ACTION*() macro family. // Tests that ACTION() can define an action that doesn't reference the @@ -1058,9 +1059,6 @@ TEST(ActionTemplateTest, CanBeOverloadedOnNumberOfValueParameters) { EXPECT_EQ(12345, a4.Perform(std::make_tuple())); } -#ifdef _MSC_VER -# pragma warning(pop) -#endif } // namespace gmock_generated_actions_test } // namespace testing diff --git a/googlemock/test/gmock-generated-function-mockers_test.cc b/googlemock/test/gmock-generated-function-mockers_test.cc index 79130bcf..dff3a9f0 100644 --- a/googlemock/test/gmock-generated-function-mockers_test.cc +++ b/googlemock/test/gmock-generated-function-mockers_test.cc @@ -63,6 +63,15 @@ using testing::Return; using testing::ReturnRef; using testing::TypedEq; +template<typename T> +class TemplatedCopyable { + public: + TemplatedCopyable() {} + + template <typename U> + TemplatedCopyable(const U& other) {} // NOLINT +}; + class FooInterface { public: virtual ~FooInterface() {} @@ -91,6 +100,8 @@ class FooInterface { virtual int TypeWithHole(int (*func)()) = 0; virtual int TypeWithComma(const std::map<int, std::string>& a_map) = 0; + virtual int TypeWithTemplatedCopyCtor( + const TemplatedCopyable<int>& a_vector) = 0; #if GTEST_OS_WINDOWS STDMETHOD_(int, CTNullary)() = 0; @@ -146,6 +157,8 @@ class MockFoo : public FooInterface { MOCK_METHOD1(TypeWithHole, int(int (*)())); // NOLINT MOCK_METHOD1(TypeWithComma, int(const std::map<int, std::string>&)); // NOLINT + MOCK_METHOD1(TypeWithTemplatedCopyCtor, + int(const TemplatedCopyable<int>&)); // NOLINT #if GTEST_OS_WINDOWS MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, CTNullary, int()); @@ -288,6 +301,11 @@ TEST_F(FunctionMockerTest, MocksReturnTypeWithComma) { EXPECT_EQ(a_map, mock_foo_.ReturnTypeWithComma(42)); } +TEST_F(FunctionMockerTest, MocksTypeWithTemplatedCopyCtor) { + EXPECT_CALL(mock_foo_, TypeWithTemplatedCopyCtor(_)).WillOnce(Return(true)); + EXPECT_TRUE(foo_->TypeWithTemplatedCopyCtor(TemplatedCopyable<int>())); +} + #if GTEST_OS_WINDOWS // Tests mocking a nullary function with calltype. TEST_F(FunctionMockerTest, MocksNullaryFunctionWithCallType) { @@ -517,7 +535,7 @@ TEST(OverloadedMockMethodTest, CanOverloadOnArgNumberInMacroBody) { #define MY_MOCK_METHODS2_ \ MOCK_CONST_METHOD1(Overloaded, int(int n)); \ - MOCK_METHOD1(Overloaded, int(int n)); + MOCK_METHOD1(Overloaded, int(int n)) class MockOverloadedOnConstness { public: @@ -582,7 +600,6 @@ TEST(MockFunctionTest, WorksFor10Arguments) { EXPECT_EQ(2, foo.Call(true, 'a', 0, 0, 0, 0, 0, 'b', 1, false)); } -#if GTEST_HAS_STD_FUNCTION_ TEST(MockFunctionTest, AsStdFunction) { MockFunction<int(int)> foo; auto call = [](const std::function<int(int)> &f, int i) { @@ -614,7 +631,6 @@ TEST(MockFunctionTest, AsStdFunctionWithReferenceParameter) { EXPECT_EQ(-1, call(foo.AsStdFunction(), i)); } -#endif // GTEST_HAS_STD_FUNCTION_ struct MockMethodSizes0 { MOCK_METHOD0(func, void()); diff --git a/googlemock/test/gmock-generated-internal-utils_test.cc b/googlemock/test/gmock-generated-internal-utils_test.cc deleted file mode 100644 index 965cbaa6..00000000 --- a/googlemock/test/gmock-generated-internal-utils_test.cc +++ /dev/null @@ -1,130 +0,0 @@ -// Copyright 2007, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - -// Google Mock - a framework for writing C++ mock classes. -// -// This file tests the internal utilities. - -#include "gmock/internal/gmock-generated-internal-utils.h" -#include "gmock/internal/gmock-internal-utils.h" -#include "gtest/gtest.h" - -namespace { - -using ::testing::Matcher; -using ::testing::internal::CompileAssertTypesEqual; -using ::testing::internal::MatcherTuple; -using ::testing::internal::Function; -using ::testing::internal::IgnoredValue; - -// Tests the MatcherTuple template struct. - -TEST(MatcherTupleTest, ForSize0) { - CompileAssertTypesEqual<std::tuple<>, MatcherTuple<std::tuple<> >::type>(); -} - -TEST(MatcherTupleTest, ForSize1) { - CompileAssertTypesEqual<std::tuple<Matcher<int> >, - MatcherTuple<std::tuple<int> >::type>(); -} - -TEST(MatcherTupleTest, ForSize2) { - CompileAssertTypesEqual<std::tuple<Matcher<int>, Matcher<char> >, - MatcherTuple<std::tuple<int, char> >::type>(); -} - -TEST(MatcherTupleTest, ForSize5) { - CompileAssertTypesEqual< - std::tuple<Matcher<int>, Matcher<char>, Matcher<bool>, Matcher<double>, - Matcher<char*> >, - MatcherTuple<std::tuple<int, char, bool, double, char*> >::type>(); -} - -// Tests the Function template struct. - -TEST(FunctionTest, Nullary) { - typedef Function<int()> F; // NOLINT - CompileAssertTypesEqual<int, F::Result>(); - CompileAssertTypesEqual<std::tuple<>, F::ArgumentTuple>(); - CompileAssertTypesEqual<std::tuple<>, F::ArgumentMatcherTuple>(); - CompileAssertTypesEqual<void(), F::MakeResultVoid>(); - CompileAssertTypesEqual<IgnoredValue(), F::MakeResultIgnoredValue>(); -} - -TEST(FunctionTest, Unary) { - typedef Function<int(bool)> F; // NOLINT - CompileAssertTypesEqual<int, F::Result>(); - CompileAssertTypesEqual<bool, F::Argument1>(); - CompileAssertTypesEqual<std::tuple<bool>, F::ArgumentTuple>(); - CompileAssertTypesEqual<std::tuple<Matcher<bool> >, - F::ArgumentMatcherTuple>(); - CompileAssertTypesEqual<void(bool), F::MakeResultVoid>(); // NOLINT - CompileAssertTypesEqual<IgnoredValue(bool), // NOLINT - F::MakeResultIgnoredValue>(); -} - -TEST(FunctionTest, Binary) { - typedef Function<int(bool, const long&)> F; // NOLINT - CompileAssertTypesEqual<int, F::Result>(); - CompileAssertTypesEqual<bool, F::Argument1>(); - CompileAssertTypesEqual<const long&, F::Argument2>(); // NOLINT - CompileAssertTypesEqual<std::tuple<bool, const long&>, // NOLINT - F::ArgumentTuple>(); - CompileAssertTypesEqual< - std::tuple<Matcher<bool>, Matcher<const long&> >, // NOLINT - F::ArgumentMatcherTuple>(); - CompileAssertTypesEqual<void(bool, const long&), F::MakeResultVoid>(); // NOLINT - CompileAssertTypesEqual<IgnoredValue(bool, const long&), // NOLINT - F::MakeResultIgnoredValue>(); -} - -TEST(FunctionTest, LongArgumentList) { - typedef Function<char(bool, int, char*, int&, const long&)> F; // NOLINT - CompileAssertTypesEqual<char, F::Result>(); - CompileAssertTypesEqual<bool, F::Argument1>(); - CompileAssertTypesEqual<int, F::Argument2>(); - CompileAssertTypesEqual<char*, F::Argument3>(); - CompileAssertTypesEqual<int&, F::Argument4>(); - CompileAssertTypesEqual<const long&, F::Argument5>(); // NOLINT - CompileAssertTypesEqual< - std::tuple<bool, int, char*, int&, const long&>, // NOLINT - F::ArgumentTuple>(); - CompileAssertTypesEqual< - std::tuple<Matcher<bool>, Matcher<int>, Matcher<char*>, Matcher<int&>, - Matcher<const long&> >, // NOLINT - F::ArgumentMatcherTuple>(); - CompileAssertTypesEqual<void(bool, int, char*, int&, const long&), // NOLINT - F::MakeResultVoid>(); - CompileAssertTypesEqual< - IgnoredValue(bool, int, char*, int&, const long&), // NOLINT - F::MakeResultIgnoredValue>(); -} - -} // Unnamed namespace diff --git a/googlemock/test/gmock-generated-matchers_test.cc b/googlemock/test/gmock-generated-matchers_test.cc index 727c8eaa..6c4b3000 100644 --- a/googlemock/test/gmock-generated-matchers_test.cc +++ b/googlemock/test/gmock-generated-matchers_test.cc @@ -64,7 +64,9 @@ using std::stringstream; using std::vector; using testing::_; using testing::AllOf; +using testing::AllOfArray; using testing::AnyOf; +using testing::AnyOfArray; using testing::Args; using testing::Contains; using testing::ElementsAre; @@ -489,7 +491,6 @@ TEST(ElementsAreArrayTest, CanBeCreatedWithVector) { EXPECT_THAT(test_vector, Not(ElementsAreArray(expected))); } -#if GTEST_HAS_STD_INITIALIZER_LIST_ TEST(ElementsAreArrayTest, TakesInitializerList) { const int a[5] = { 1, 2, 3, 4, 5 }; @@ -525,7 +526,6 @@ TEST(ElementsAreArrayTest, { Eq(1), Ne(-2), Ge(3), Le(4), Eq(6) }))); } -#endif // GTEST_HAS_STD_INITIALIZER_LIST_ TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherVector) { const int a[] = { 1, 2, 3 }; @@ -1096,6 +1096,146 @@ TEST(ContainsTest, WorksForTwoDimensionalNativeArray) { EXPECT_THAT(a, Contains(Not(Contains(5)))); } +TEST(AllOfArrayTest, BasicForms) { + // Iterator + std::vector<int> v0{}; + std::vector<int> v1{1}; + std::vector<int> v2{2, 3}; + std::vector<int> v3{4, 4, 4}; + EXPECT_THAT(0, AllOfArray(v0.begin(), v0.end())); + EXPECT_THAT(1, AllOfArray(v1.begin(), v1.end())); + EXPECT_THAT(2, Not(AllOfArray(v1.begin(), v1.end()))); + EXPECT_THAT(3, Not(AllOfArray(v2.begin(), v2.end()))); + EXPECT_THAT(4, AllOfArray(v3.begin(), v3.end())); + // Pointer + size + int ar[6] = {1, 2, 3, 4, 4, 4}; + EXPECT_THAT(0, AllOfArray(ar, 0)); + EXPECT_THAT(1, AllOfArray(ar, 1)); + EXPECT_THAT(2, Not(AllOfArray(ar, 1))); + EXPECT_THAT(3, Not(AllOfArray(ar + 1, 3))); + EXPECT_THAT(4, AllOfArray(ar + 3, 3)); + // Array + // int ar0[0]; Not usable + int ar1[1] = {1}; + int ar2[2] = {2, 3}; + int ar3[3] = {4, 4, 4}; + // EXPECT_THAT(0, Not(AllOfArray(ar0))); // Cannot work + EXPECT_THAT(1, AllOfArray(ar1)); + EXPECT_THAT(2, Not(AllOfArray(ar1))); + EXPECT_THAT(3, Not(AllOfArray(ar2))); + EXPECT_THAT(4, AllOfArray(ar3)); + // Container + EXPECT_THAT(0, AllOfArray(v0)); + EXPECT_THAT(1, AllOfArray(v1)); + EXPECT_THAT(2, Not(AllOfArray(v1))); + EXPECT_THAT(3, Not(AllOfArray(v2))); + EXPECT_THAT(4, AllOfArray(v3)); + // Initializer + EXPECT_THAT(0, AllOfArray<int>({})); // Requires template arg. + EXPECT_THAT(1, AllOfArray({1})); + EXPECT_THAT(2, Not(AllOfArray({1}))); + EXPECT_THAT(3, Not(AllOfArray({2, 3}))); + EXPECT_THAT(4, AllOfArray({4, 4, 4})); +} + +TEST(AllOfArrayTest, Matchers) { + // vector + std::vector<Matcher<int>> matchers{Ge(1), Lt(2)}; + EXPECT_THAT(0, Not(AllOfArray(matchers))); + EXPECT_THAT(1, AllOfArray(matchers)); + EXPECT_THAT(2, Not(AllOfArray(matchers))); + // initializer_list + EXPECT_THAT(0, Not(AllOfArray({Ge(0), Ge(1)}))); + EXPECT_THAT(1, AllOfArray({Ge(0), Ge(1)})); +} + +TEST(AnyOfArrayTest, BasicForms) { + // Iterator + std::vector<int> v0{}; + std::vector<int> v1{1}; + std::vector<int> v2{2, 3}; + EXPECT_THAT(0, Not(AnyOfArray(v0.begin(), v0.end()))); + EXPECT_THAT(1, AnyOfArray(v1.begin(), v1.end())); + EXPECT_THAT(2, Not(AnyOfArray(v1.begin(), v1.end()))); + EXPECT_THAT(3, AnyOfArray(v2.begin(), v2.end())); + EXPECT_THAT(4, Not(AnyOfArray(v2.begin(), v2.end()))); + // Pointer + size + int ar[3] = {1, 2, 3}; + EXPECT_THAT(0, Not(AnyOfArray(ar, 0))); + EXPECT_THAT(1, AnyOfArray(ar, 1)); + EXPECT_THAT(2, Not(AnyOfArray(ar, 1))); + EXPECT_THAT(3, AnyOfArray(ar + 1, 2)); + EXPECT_THAT(4, Not(AnyOfArray(ar + 1, 2))); + // Array + // int ar0[0]; Not usable + int ar1[1] = {1}; + int ar2[2] = {2, 3}; + // EXPECT_THAT(0, Not(AnyOfArray(ar0))); // Cannot work + EXPECT_THAT(1, AnyOfArray(ar1)); + EXPECT_THAT(2, Not(AnyOfArray(ar1))); + EXPECT_THAT(3, AnyOfArray(ar2)); + EXPECT_THAT(4, Not(AnyOfArray(ar2))); + // Container + EXPECT_THAT(0, Not(AnyOfArray(v0))); + EXPECT_THAT(1, AnyOfArray(v1)); + EXPECT_THAT(2, Not(AnyOfArray(v1))); + EXPECT_THAT(3, AnyOfArray(v2)); + EXPECT_THAT(4, Not(AnyOfArray(v2))); + // Initializer + EXPECT_THAT(0, Not(AnyOfArray<int>({}))); // Requires template arg. + EXPECT_THAT(1, AnyOfArray({1})); + EXPECT_THAT(2, Not(AnyOfArray({1}))); + EXPECT_THAT(3, AnyOfArray({2, 3})); + EXPECT_THAT(4, Not(AnyOfArray({2, 3}))); +} + +TEST(AnyOfArrayTest, Matchers) { + // We negate test AllOfArrayTest.Matchers. + // vector + std::vector<Matcher<int>> matchers{Lt(1), Ge(2)}; + EXPECT_THAT(0, AnyOfArray(matchers)); + EXPECT_THAT(1, Not(AnyOfArray(matchers))); + EXPECT_THAT(2, AnyOfArray(matchers)); + // initializer_list + EXPECT_THAT(0, AnyOfArray({Lt(0), Lt(1)})); + EXPECT_THAT(1, Not(AllOfArray({Lt(0), Lt(1)}))); +} + +TEST(AnyOfArrayTest, ExplainsMatchResultCorrectly) { + // AnyOfArray and AllOfArry use the same underlying template-template, + // thus it is sufficient to test one here. + const std::vector<int> v0{}; + const std::vector<int> v1{1}; + const std::vector<int> v2{2, 3}; + const Matcher<int> m0 = AnyOfArray(v0); + const Matcher<int> m1 = AnyOfArray(v1); + const Matcher<int> m2 = AnyOfArray(v2); + EXPECT_EQ("", Explain(m0, 0)); + EXPECT_EQ("", Explain(m1, 1)); + EXPECT_EQ("", Explain(m1, 2)); + EXPECT_EQ("", Explain(m2, 3)); + EXPECT_EQ("", Explain(m2, 4)); + EXPECT_EQ("()", Describe(m0)); + EXPECT_EQ("(is equal to 1)", Describe(m1)); + EXPECT_EQ("(is equal to 2) or (is equal to 3)", Describe(m2)); + EXPECT_EQ("()", DescribeNegation(m0)); + EXPECT_EQ("(isn't equal to 1)", DescribeNegation(m1)); + EXPECT_EQ("(isn't equal to 2) and (isn't equal to 3)", DescribeNegation(m2)); + // Explain with matchers + const Matcher<int> g1 = AnyOfArray({GreaterThan(1)}); + const Matcher<int> g2 = AnyOfArray({GreaterThan(1), GreaterThan(2)}); + // Explains the first positiv match and all prior negative matches... + EXPECT_EQ("which is 1 less than 1", Explain(g1, 0)); + EXPECT_EQ("which is the same as 1", Explain(g1, 1)); + EXPECT_EQ("which is 1 more than 1", Explain(g1, 2)); + EXPECT_EQ("which is 1 less than 1, and which is 2 less than 2", + Explain(g2, 0)); + EXPECT_EQ("which is the same as 1, and which is 1 less than 2", + Explain(g2, 1)); + EXPECT_EQ("which is 1 more than 1", // Only the first + Explain(g2, 2)); +} + TEST(AllOfTest, HugeMatcher) { // Verify that using AllOf with many arguments doesn't cause // the compiler to exceed template instantiation depth limit. @@ -1122,7 +1262,7 @@ namespace adl_test { MATCHER(M, "") { return true; } template <typename T1, typename T2> -bool AllOf(const T1& t1, const T2& t2) { return true; } +bool AllOf(const T1& /*t1*/, const T2& /*t2*/) { return true; } TEST(AllOfTest, DoesNotCallAllOfUnqualified) { EXPECT_THAT(42, testing::AllOf( @@ -1139,7 +1279,6 @@ TEST(AnyOfTest, DoesNotCallAnyOfUnqualified) { } // namespace adl_test -#if GTEST_LANG_CXX11 TEST(AllOfTest, WorksOnMoveOnlyType) { std::unique_ptr<int> p(new int(3)); @@ -1177,7 +1316,6 @@ TEST(MatcherPMacroTest, WorksOnMoveOnlyType) { EXPECT_THAT(p, Not(UniquePointee(2))); } -#endif // GTEST_LASNG_CXX11 } // namespace diff --git a/googlemock/test/gmock-internal-utils_test.cc b/googlemock/test/gmock-internal-utils_test.cc index 48fcacc7..67b7077f 100644 --- a/googlemock/test/gmock-internal-utils_test.cc +++ b/googlemock/test/gmock-internal-utils_test.cc @@ -33,16 +33,20 @@ // This file tests the internal utilities. #include "gmock/internal/gmock-internal-utils.h" + #include <stdlib.h> + #include <map> #include <memory> -#include <string> #include <sstream> +#include <string> +#include <type_traits> #include <vector> + #include "gmock/gmock.h" #include "gmock/internal/gmock-port.h" -#include "gtest/gtest.h" #include "gtest/gtest-spi.h" +#include "gtest/gtest.h" // Indicates that this translation unit is part of Google Test's // implementation. It must come before gtest-internal-inl.h is @@ -57,8 +61,6 @@ # include <sys/types.h> // For ssize_t. NOLINT #endif -class ProtocolMessage; - namespace proto2 { class Message; } // namespace proto2 @@ -123,13 +125,9 @@ TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameIsMixture) { } TEST(PointeeOfTest, WorksForSmartPointers) { -#if GTEST_HAS_STD_UNIQUE_PTR_ CompileAssertTypesEqual<int, PointeeOf<std::unique_ptr<int> >::type>(); -#endif // GTEST_HAS_STD_UNIQUE_PTR_ -#if GTEST_HAS_STD_SHARED_PTR_ CompileAssertTypesEqual<std::string, PointeeOf<std::shared_ptr<std::string> >::type>(); -#endif // GTEST_HAS_STD_SHARED_PTR_ } TEST(PointeeOfTest, WorksForRawPointers) { @@ -139,21 +137,16 @@ TEST(PointeeOfTest, WorksForRawPointers) { } TEST(GetRawPointerTest, WorksForSmartPointers) { -#if GTEST_HAS_STD_UNIQUE_PTR_ const char* const raw_p1 = new const char('a'); // NOLINT const std::unique_ptr<const char> p1(raw_p1); EXPECT_EQ(raw_p1, GetRawPointer(p1)); -#endif // GTEST_HAS_STD_UNIQUE_PTR_ -#if GTEST_HAS_STD_SHARED_PTR_ double* const raw_p2 = new double(2.5); // NOLINT const std::shared_ptr<double> p2(raw_p2); EXPECT_EQ(raw_p2, GetRawPointer(p2)); -#endif // GTEST_HAS_STD_SHARED_PTR_ } TEST(GetRawPointerTest, WorksForRawPointers) { int* p = nullptr; - // Don't use EXPECT_EQ as no NULL-testing magic on Symbian. EXPECT_TRUE(nullptr == GetRawPointer(p)); int n = 1; EXPECT_EQ(&n, GetRawPointer(&n)); @@ -515,39 +508,12 @@ TEST(LogTest, OnlyWarningsArePrintedWhenVerbosityIsInvalid) { #endif // GTEST_HAS_STREAM_REDIRECTION -TEST(TypeTraitsTest, true_type) { - EXPECT_TRUE(true_type::value); -} - -TEST(TypeTraitsTest, false_type) { - EXPECT_FALSE(false_type::value); -} - -TEST(TypeTraitsTest, is_reference) { - EXPECT_FALSE(is_reference<int>::value); - EXPECT_FALSE(is_reference<char*>::value); - EXPECT_TRUE(is_reference<const int&>::value); -} - -TEST(TypeTraitsTest, is_pointer) { - EXPECT_FALSE(is_pointer<int>::value); - EXPECT_FALSE(is_pointer<char&>::value); - EXPECT_TRUE(is_pointer<const int*>::value); -} - -TEST(TypeTraitsTest, type_equals) { - EXPECT_FALSE((type_equals<int, const int>::value)); - EXPECT_FALSE((type_equals<int, int&>::value)); - EXPECT_FALSE((type_equals<int, double>::value)); - EXPECT_TRUE((type_equals<char, char>::value)); -} - TEST(TypeTraitsTest, remove_reference) { - EXPECT_TRUE((type_equals<char, remove_reference<char&>::type>::value)); - EXPECT_TRUE((type_equals<const int, - remove_reference<const int&>::type>::value)); - EXPECT_TRUE((type_equals<int, remove_reference<int>::type>::value)); - EXPECT_TRUE((type_equals<double*, remove_reference<double*>::type>::value)); + EXPECT_TRUE((std::is_same<char, remove_reference<char&>::type>::value)); + EXPECT_TRUE( + (std::is_same<const int, remove_reference<const int&>::type>::value)); + EXPECT_TRUE((std::is_same<int, remove_reference<int>::type>::value)); + EXPECT_TRUE((std::is_same<double*, remove_reference<double*>::type>::value)); } #if GTEST_HAS_STREAM_REDIRECTION @@ -573,7 +539,7 @@ void ExpectCallLogger() { DummyMock mock; EXPECT_CALL(mock, TestMethod()); mock.TestMethod(); -}; +} // Verifies that EXPECT_CALL logs if the --gmock_verbose flag is set to "info". TEST(ExpectCallTest, LogsWhenVerbosityIsInfo) { @@ -596,7 +562,7 @@ TEST(ExpectCallTest, DoesNotLogWhenVerbosityIsError) { void OnCallLogger() { DummyMock mock; ON_CALL(mock, TestMethod()); -}; +} // Verifies that ON_CALL logs if the --gmock_verbose flag is set to "info". TEST(OnCallTest, LogsWhenVerbosityIsInfo) { @@ -705,6 +671,70 @@ TEST(StlContainerViewTest, WorksForDynamicNativeArray) { EXPECT_EQ(0, a3.begin()[0]); } +// Tests the Function template struct. + +TEST(FunctionTest, Nullary) { + typedef Function<int()> F; // NOLINT + EXPECT_EQ(0u, F::ArgumentCount); + CompileAssertTypesEqual<int, F::Result>(); + CompileAssertTypesEqual<std::tuple<>, F::ArgumentTuple>(); + CompileAssertTypesEqual<std::tuple<>, F::ArgumentMatcherTuple>(); + CompileAssertTypesEqual<void(), F::MakeResultVoid>(); + CompileAssertTypesEqual<IgnoredValue(), F::MakeResultIgnoredValue>(); +} + +TEST(FunctionTest, Unary) { + typedef Function<int(bool)> F; // NOLINT + EXPECT_EQ(1u, F::ArgumentCount); + CompileAssertTypesEqual<int, F::Result>(); + CompileAssertTypesEqual<bool, F::Arg<0>::type>(); + CompileAssertTypesEqual<std::tuple<bool>, F::ArgumentTuple>(); + CompileAssertTypesEqual<std::tuple<Matcher<bool> >, + F::ArgumentMatcherTuple>(); + CompileAssertTypesEqual<void(bool), F::MakeResultVoid>(); // NOLINT + CompileAssertTypesEqual<IgnoredValue(bool), // NOLINT + F::MakeResultIgnoredValue>(); +} + +TEST(FunctionTest, Binary) { + typedef Function<int(bool, const long&)> F; // NOLINT + EXPECT_EQ(2u, F::ArgumentCount); + CompileAssertTypesEqual<int, F::Result>(); + CompileAssertTypesEqual<bool, F::Arg<0>::type>(); + CompileAssertTypesEqual<const long&, F::Arg<1>::type>(); // NOLINT + CompileAssertTypesEqual<std::tuple<bool, const long&>, // NOLINT + F::ArgumentTuple>(); + CompileAssertTypesEqual< + std::tuple<Matcher<bool>, Matcher<const long&> >, // NOLINT + F::ArgumentMatcherTuple>(); + CompileAssertTypesEqual<void(bool, const long&), F::MakeResultVoid>(); // NOLINT + CompileAssertTypesEqual<IgnoredValue(bool, const long&), // NOLINT + F::MakeResultIgnoredValue>(); +} + +TEST(FunctionTest, LongArgumentList) { + typedef Function<char(bool, int, char*, int&, const long&)> F; // NOLINT + EXPECT_EQ(5u, F::ArgumentCount); + CompileAssertTypesEqual<char, F::Result>(); + CompileAssertTypesEqual<bool, F::Arg<0>::type>(); + CompileAssertTypesEqual<int, F::Arg<1>::type>(); + CompileAssertTypesEqual<char*, F::Arg<2>::type>(); + CompileAssertTypesEqual<int&, F::Arg<3>::type>(); + CompileAssertTypesEqual<const long&, F::Arg<4>::type>(); // NOLINT + CompileAssertTypesEqual< + std::tuple<bool, int, char*, int&, const long&>, // NOLINT + F::ArgumentTuple>(); + CompileAssertTypesEqual< + std::tuple<Matcher<bool>, Matcher<int>, Matcher<char*>, Matcher<int&>, + Matcher<const long&> >, // NOLINT + F::ArgumentMatcherTuple>(); + CompileAssertTypesEqual<void(bool, int, char*, int&, const long&), // NOLINT + F::MakeResultVoid>(); + CompileAssertTypesEqual< + IgnoredValue(bool, int, char*, int&, const long&), // NOLINT + F::MakeResultIgnoredValue>(); +} + } // namespace } // namespace internal } // namespace testing diff --git a/googlemock/test/gmock-matchers_test.cc b/googlemock/test/gmock-matchers_test.cc index c1589a40..a61d040b 100644 --- a/googlemock/test/gmock-matchers_test.cc +++ b/googlemock/test/gmock-matchers_test.cc @@ -46,6 +46,7 @@ #include <string.h> #include <time.h> #include <deque> +#include <forward_list> #include <functional> #include <iostream> #include <iterator> @@ -56,22 +57,16 @@ #include <set> #include <sstream> #include <string> +#include <type_traits> #include <utility> #include <vector> #include "gmock/gmock.h" #include "gtest/gtest.h" #include "gtest/gtest-spi.h" -#if GTEST_HAS_STD_FORWARD_LIST_ -# include <forward_list> // NOLINT -#endif - -#if GTEST_LANG_CXX11 -# include <type_traits> -#endif - namespace testing { namespace gmock_matchers_test { +namespace { using std::greater; using std::less; @@ -85,65 +80,6 @@ using std::pair; using std::set; using std::stringstream; using std::vector; -using testing::_; -using testing::A; -using testing::AllArgs; -using testing::AllOf; -using testing::An; -using testing::AnyOf; -using testing::ByRef; -using testing::ContainsRegex; -using testing::DoubleEq; -using testing::DoubleNear; -using testing::EndsWith; -using testing::Eq; -using testing::ExplainMatchResult; -using testing::Field; -using testing::FloatEq; -using testing::FloatNear; -using testing::Ge; -using testing::Gt; -using testing::HasSubstr; -using testing::IsEmpty; -using testing::IsNull; -using testing::Key; -using testing::Le; -using testing::Lt; -using testing::MakeMatcher; -using testing::MakePolymorphicMatcher; -using testing::Matcher; -using testing::MatcherCast; -using testing::MatcherInterface; -using testing::Matches; -using testing::MatchesRegex; -using testing::MatchResultListener; -using testing::NanSensitiveDoubleEq; -using testing::NanSensitiveDoubleNear; -using testing::NanSensitiveFloatEq; -using testing::NanSensitiveFloatNear; -using testing::Ne; -using testing::Not; -using testing::NotNull; -using testing::Pair; -using testing::Pointee; -using testing::Pointwise; -using testing::PolymorphicMatcher; -using testing::Property; -using testing::Ref; -using testing::ResultOf; -using testing::SizeIs; -using testing::StartsWith; -using testing::StrCaseEq; -using testing::StrCaseNe; -using testing::StrEq; -using testing::StringMatchResultListener; -using testing::StrNe; -using testing::Truly; -using testing::TypedEq; -using testing::UnorderedPointwise; -using testing::Value; -using testing::WhenSorted; -using testing::WhenSortedBy; using testing::internal::DummyMatchResultListener; using testing::internal::ElementMatcherPair; using testing::internal::ElementMatcherPairs; @@ -155,9 +91,21 @@ using testing::internal::MatchMatrix; using testing::internal::PredicateFormatterFromMatcher; using testing::internal::RE; using testing::internal::StreamMatchResultListener; -using testing::internal::string; using testing::internal::Strings; +// Helper for testing container-valued matchers in mock method context. It is +// important to test matchers in this context, since it requires additional type +// deduction beyond what EXPECT_THAT does, thus making it more restrictive. +struct ContainerHelper { + MOCK_METHOD1(Call, void(std::vector<std::unique_ptr<int>>)); +}; + +std::vector<std::unique_ptr<int>> MakeUniquePtrs(const std::vector<int>& ints) { + std::vector<std::unique_ptr<int>> pointers; + for (int i : ints) pointers.emplace_back(new int(i)); + return pointers; +} + // For testing ExplainMatchResultTo(). class GreaterThanMatcher : public MatcherInterface<int> { public: @@ -399,58 +347,6 @@ TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) { EXPECT_FALSE(m2.Matches("hello")); } -#if GTEST_HAS_GLOBAL_STRING -// Tests that a ::string object can be implicitly converted to a -// Matcher<std::string> or Matcher<const std::string&>. -TEST(StringMatcherTest, CanBeImplicitlyConstructedFromGlobalString) { - Matcher<std::string> m1 = ::string("hi"); - EXPECT_TRUE(m1.Matches("hi")); - EXPECT_FALSE(m1.Matches("hello")); - - Matcher<const std::string&> m2 = ::string("hi"); - EXPECT_TRUE(m2.Matches("hi")); - EXPECT_FALSE(m2.Matches("hello")); -} -#endif // GTEST_HAS_GLOBAL_STRING - -#if GTEST_HAS_GLOBAL_STRING -// Tests that a C-string literal can be implicitly converted to a -// Matcher<::string> or Matcher<const ::string&>. -TEST(GlobalStringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) { - Matcher< ::string> m1 = "hi"; - EXPECT_TRUE(m1.Matches("hi")); - EXPECT_FALSE(m1.Matches("hello")); - - Matcher<const ::string&> m2 = "hi"; - EXPECT_TRUE(m2.Matches("hi")); - EXPECT_FALSE(m2.Matches("hello")); -} - -// Tests that a std::string object can be implicitly converted to a -// Matcher<::string> or Matcher<const ::string&>. -TEST(GlobalStringMatcherTest, CanBeImplicitlyConstructedFromString) { - Matcher< ::string> m1 = std::string("hi"); - EXPECT_TRUE(m1.Matches("hi")); - EXPECT_FALSE(m1.Matches("hello")); - - Matcher<const ::string&> m2 = std::string("hi"); - EXPECT_TRUE(m2.Matches("hi")); - EXPECT_FALSE(m2.Matches("hello")); -} - -// Tests that a ::string object can be implicitly converted to a -// Matcher<::string> or Matcher<const ::string&>. -TEST(GlobalStringMatcherTest, CanBeImplicitlyConstructedFromGlobalString) { - Matcher< ::string> m1 = ::string("hi"); - EXPECT_TRUE(m1.Matches("hi")); - EXPECT_FALSE(m1.Matches("hello")); - - Matcher<const ::string&> m2 = ::string("hi"); - EXPECT_TRUE(m2.Matches("hi")); - EXPECT_FALSE(m2.Matches("hello")); -} -#endif // GTEST_HAS_GLOBAL_STRING - #if GTEST_HAS_ABSL // Tests that a C-string literal can be implicitly converted to a // Matcher<absl::string_view> or Matcher<const absl::string_view&>. @@ -476,32 +372,32 @@ TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromString) { EXPECT_FALSE(m2.Matches("dogs")); } -#if GTEST_HAS_GLOBAL_STRING -// Tests that a ::string object can be implicitly converted to a +// Tests that a absl::string_view object can be implicitly converted to a // Matcher<absl::string_view> or Matcher<const absl::string_view&>. -TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromGlobalString) { - Matcher<absl::string_view> m1 = ::string("cats"); +TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromStringView) { + Matcher<absl::string_view> m1 = absl::string_view("cats"); EXPECT_TRUE(m1.Matches("cats")); EXPECT_FALSE(m1.Matches("dogs")); - Matcher<const absl::string_view&> m2 = ::string("cats"); + Matcher<const absl::string_view&> m2 = absl::string_view("cats"); EXPECT_TRUE(m2.Matches("cats")); EXPECT_FALSE(m2.Matches("dogs")); } -#endif // GTEST_HAS_GLOBAL_STRING +#endif // GTEST_HAS_ABSL -// Tests that a absl::string_view object can be implicitly converted to a -// Matcher<absl::string_view> or Matcher<const absl::string_view&>. -TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromStringView) { - Matcher<absl::string_view> m1 = absl::string_view("cats"); +// Tests that a std::reference_wrapper<std::string> object can be implicitly +// converted to a Matcher<std::string> or Matcher<const std::string&> via Eq(). +TEST(StringMatcherTest, + CanBeImplicitlyConstructedFromEqReferenceWrapperString) { + std::string value = "cats"; + Matcher<std::string> m1 = Eq(std::ref(value)); EXPECT_TRUE(m1.Matches("cats")); EXPECT_FALSE(m1.Matches("dogs")); - Matcher<const absl::string_view&> m2 = absl::string_view("cats"); + Matcher<const std::string&> m2 = Eq(std::ref(value)); EXPECT_TRUE(m2.Matches("cats")); EXPECT_FALSE(m2.Matches("dogs")); } -#endif // GTEST_HAS_ABSL // Tests that MakeMatcher() constructs a Matcher<T> from a // MatcherInterface* without requiring the user to explicitly @@ -1019,6 +915,8 @@ class Unprintable { Unprintable() : c_('a') {} bool operator==(const Unprintable& /* rhs */) const { return true; } + // -Wunused-private-field: dummy accessor for `c_`. + char dummy_c() { return c_; } private: char c_; }; @@ -1058,7 +956,7 @@ TEST(TypedEqTest, CanDescribeSelf) { // Tests that TypedEq<T>(v) has type Matcher<T>. -// Type<T>::IsTypeOf(v) compiles iff the type of value v is T, where T +// Type<T>::IsTypeOf(v) compiles if the type of value v is T, where T // is a "bare" type (i.e. not in the form of const U or U&). If v's // type is not T, the compiler will generate a message about // "undefined reference". @@ -1146,6 +1044,47 @@ TEST(NeTest, CanDescribeSelf) { EXPECT_EQ("isn't equal to 5", Describe(m)); } +class MoveOnly { + public: + explicit MoveOnly(int i) : i_(i) {} + MoveOnly(const MoveOnly&) = delete; + MoveOnly(MoveOnly&&) = default; + MoveOnly& operator=(const MoveOnly&) = delete; + MoveOnly& operator=(MoveOnly&&) = default; + + bool operator==(const MoveOnly& other) const { return i_ == other.i_; } + bool operator!=(const MoveOnly& other) const { return i_ != other.i_; } + bool operator<(const MoveOnly& other) const { return i_ < other.i_; } + bool operator<=(const MoveOnly& other) const { return i_ <= other.i_; } + bool operator>(const MoveOnly& other) const { return i_ > other.i_; } + bool operator>=(const MoveOnly& other) const { return i_ >= other.i_; } + + private: + int i_; +}; + +struct MoveHelper { + MOCK_METHOD1(Call, void(MoveOnly)); +}; + +TEST(ComparisonBaseTest, WorksWithMoveOnly) { + MoveOnly m{0}; + MoveHelper helper; + + EXPECT_CALL(helper, Call(Eq(ByRef(m)))); + helper.Call(MoveOnly(0)); + EXPECT_CALL(helper, Call(Ne(ByRef(m)))); + helper.Call(MoveOnly(1)); + EXPECT_CALL(helper, Call(Le(ByRef(m)))); + helper.Call(MoveOnly(0)); + EXPECT_CALL(helper, Call(Lt(ByRef(m)))); + helper.Call(MoveOnly(-1)); + EXPECT_CALL(helper, Call(Ge(ByRef(m)))); + helper.Call(MoveOnly(0)); + EXPECT_CALL(helper, Call(Gt(ByRef(m)))); + helper.Call(MoveOnly(1)); +} + // Tests that IsNull() matches any NULL pointer of any type. TEST(IsNullTest, MatchesNullPointer) { Matcher<int*> m1 = IsNull(); @@ -1159,30 +1098,18 @@ TEST(IsNullTest, MatchesNullPointer) { EXPECT_TRUE(m2.Matches(p2)); EXPECT_FALSE(m2.Matches("hi")); -#if !GTEST_OS_SYMBIAN - // Nokia's Symbian compiler generates: - // gmock-matchers.h: ambiguous access to overloaded function - // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(void *)' - // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(const testing:: - // MatcherInterface<void *> *)' - // gmock-matchers.h: (point of instantiation: 'testing:: - // gmock_matchers_test::IsNullTest_MatchesNullPointer_Test::TestBody()') - // gmock-matchers.h: (instantiating: 'testing::PolymorphicMatc Matcher<void*> m3 = IsNull(); void* p3 = nullptr; EXPECT_TRUE(m3.Matches(p3)); EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef))); -#endif } -#if GTEST_LANG_CXX11 TEST(IsNullTest, StdFunction) { const Matcher<std::function<void()>> m = IsNull(); EXPECT_TRUE(m.Matches(std::function<void()>())); EXPECT_FALSE(m.Matches([]{})); } -#endif // GTEST_LANG_CXX11 // Tests that IsNull() describes itself properly. TEST(IsNullTest, CanDescribeSelf) { @@ -1223,14 +1150,12 @@ TEST(NotNullTest, ReferenceToConstLinkedPtr) { EXPECT_TRUE(m.Matches(non_null_p)); } -#if GTEST_LANG_CXX11 TEST(NotNullTest, StdFunction) { const Matcher<std::function<void()>> m = NotNull(); EXPECT_TRUE(m.Matches([]{})); EXPECT_FALSE(m.Matches(std::function<void()>())); } -#endif // GTEST_LANG_CXX11 // Tests that NotNull() describes itself properly. TEST(NotNullTest, CanDescribeSelf) { @@ -1513,18 +1438,22 @@ TEST(KeyTest, MatchesCorrectly) { EXPECT_THAT(p, Not(Key(Lt(25)))); } -#if GTEST_LANG_CXX11 +TEST(KeyTest, WorksWithMoveOnly) { + pair<std::unique_ptr<int>, std::unique_ptr<int>> p; + EXPECT_THAT(p, Key(Eq(nullptr))); +} + template <size_t I> struct Tag {}; struct PairWithGet { int member_1; - string member_2; + std::string member_2; using first_type = int; - using second_type = string; + using second_type = std::string; const int& GetImpl(Tag<0>) const { return member_1; } - const string& GetImpl(Tag<1>) const { return member_2; } + const std::string& GetImpl(Tag<1>) const { return member_2; } }; template <size_t I> auto get(const PairWithGet& value) -> decltype(value.GetImpl(Tag<I>())) { @@ -1540,7 +1469,6 @@ TEST(PairTest, MatchesPairWithGetCorrectly) { std::vector<PairWithGet> v = {{11, "Foo"}, {29, "gMockIsBestMock"}}; EXPECT_THAT(v, Contains(Key(29))); } -#endif // GTEST_LANG_CXX11 TEST(KeyTest, SafelyCastsInnerMatcher) { Matcher<int> is_positive = Gt(0); @@ -1658,6 +1586,12 @@ TEST(PairTest, MatchesCorrectly) { EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a")))); } +TEST(PairTest, WorksWithMoveOnly) { + pair<std::unique_ptr<int>, std::unique_ptr<int>> p; + p.second.reset(new int(7)); + EXPECT_THAT(p, Pair(Eq(nullptr), Ne(nullptr))); +} + TEST(PairTest, SafelyCastsInnerMatchers) { Matcher<int> is_positive = Gt(0); Matcher<int> is_negative = Lt(0); @@ -1679,7 +1613,12 @@ TEST(PairTest, InsideContainsUsingMap) { EXPECT_THAT(container, Not(Contains(Pair(3, _)))); } -#if GTEST_LANG_CXX11 +TEST(ContainsTest, WorksWithMoveOnly) { + ContainerHelper helper; + EXPECT_CALL(helper, Call(Contains(Pointee(2)))); + helper.Call(MakeUniquePtrs({1, 2})); +} + TEST(PairTest, UseGetInsteadOfMembers) { PairWithGet pair{7, "ABC"}; EXPECT_THAT(pair, Pair(7, "ABC")); @@ -1687,9 +1626,9 @@ TEST(PairTest, UseGetInsteadOfMembers) { EXPECT_THAT(pair, Not(Pair(Lt(7), "ABC"))); std::vector<PairWithGet> v = {{11, "Foo"}, {29, "gMockIsBestMock"}}; - EXPECT_THAT(v, ElementsAre(Pair(11, string("Foo")), Pair(Ge(10), Not("")))); + EXPECT_THAT(v, + ElementsAre(Pair(11, std::string("Foo")), Pair(Ge(10), Not("")))); } -#endif // GTEST_LANG_CXX11 // Tests StartsWith(s). @@ -1734,15 +1673,6 @@ TEST(EndsWithTest, MatchesStringWithGivenSuffix) { EXPECT_FALSE(m2.Matches("i")); EXPECT_FALSE(m2.Matches("Hi ")); -#if GTEST_HAS_GLOBAL_STRING - const Matcher<const ::string&> m3 = EndsWith(::string("Hi")); - EXPECT_TRUE(m3.Matches("Hi")); - EXPECT_TRUE(m3.Matches("Wow Hi Hi")); - EXPECT_TRUE(m3.Matches("Super Hi")); - EXPECT_FALSE(m3.Matches("i")); - EXPECT_FALSE(m3.Matches("Hi ")); -#endif // GTEST_HAS_GLOBAL_STRING - #if GTEST_HAS_ABSL const Matcher<const absl::string_view&> m4 = EndsWith(""); EXPECT_TRUE(m4.Matches("Hi")); @@ -2025,197 +1955,6 @@ TEST(StdWideEndsWithTest, CanDescribeSelf) { #endif // GTEST_HAS_STD_WSTRING -#if GTEST_HAS_GLOBAL_WSTRING -TEST(GlobalWideStrEqTest, MatchesEqual) { - Matcher<const wchar_t*> m = StrEq(::wstring(L"Hello")); - EXPECT_TRUE(m.Matches(L"Hello")); - EXPECT_FALSE(m.Matches(L"hello")); - EXPECT_FALSE(m.Matches(nullptr)); - - Matcher<const ::wstring&> m2 = StrEq(L"Hello"); - EXPECT_TRUE(m2.Matches(L"Hello")); - EXPECT_FALSE(m2.Matches(L"Hi")); - - Matcher<const ::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D"); - EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D")); - EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E")); - - ::wstring str(L"01204500800"); - str[3] = L'\0'; - Matcher<const ::wstring&> m4 = StrEq(str); - EXPECT_TRUE(m4.Matches(str)); - str[0] = str[6] = str[7] = str[9] = str[10] = L'\0'; - Matcher<const ::wstring&> m5 = StrEq(str); - EXPECT_TRUE(m5.Matches(str)); -} - -TEST(GlobalWideStrEqTest, CanDescribeSelf) { - Matcher< ::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v"); - EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"", - Describe(m)); - - Matcher< ::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D"); - EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"", - Describe(m2)); - - ::wstring str(L"01204500800"); - str[3] = L'\0'; - Matcher<const ::wstring&> m4 = StrEq(str); - EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4)); - str[0] = str[6] = str[7] = str[9] = str[10] = L'\0'; - Matcher<const ::wstring&> m5 = StrEq(str); - EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5)); -} - -TEST(GlobalWideStrNeTest, MatchesUnequalString) { - Matcher<const wchar_t*> m = StrNe(L"Hello"); - EXPECT_TRUE(m.Matches(L"")); - EXPECT_TRUE(m.Matches(nullptr)); - EXPECT_FALSE(m.Matches(L"Hello")); - - Matcher< ::wstring> m2 = StrNe(::wstring(L"Hello")); - EXPECT_TRUE(m2.Matches(L"hello")); - EXPECT_FALSE(m2.Matches(L"Hello")); -} - -TEST(GlobalWideStrNeTest, CanDescribeSelf) { - Matcher<const wchar_t*> m = StrNe(L"Hi"); - EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m)); -} - -TEST(GlobalWideStrCaseEqTest, MatchesEqualStringIgnoringCase) { - Matcher<const wchar_t*> m = StrCaseEq(::wstring(L"Hello")); - EXPECT_TRUE(m.Matches(L"Hello")); - EXPECT_TRUE(m.Matches(L"hello")); - EXPECT_FALSE(m.Matches(L"Hi")); - EXPECT_FALSE(m.Matches(nullptr)); - - Matcher<const ::wstring&> m2 = StrCaseEq(L"Hello"); - EXPECT_TRUE(m2.Matches(L"hello")); - EXPECT_FALSE(m2.Matches(L"Hi")); -} - -TEST(GlobalWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) { - ::wstring str1(L"oabocdooeoo"); - ::wstring str2(L"OABOCDOOEOO"); - Matcher<const ::wstring&> m0 = StrCaseEq(str1); - EXPECT_FALSE(m0.Matches(str2 + ::wstring(1, L'\0'))); - - str1[3] = str2[3] = L'\0'; - Matcher<const ::wstring&> m1 = StrCaseEq(str1); - EXPECT_TRUE(m1.Matches(str2)); - - str1[0] = str1[6] = str1[7] = str1[10] = L'\0'; - str2[0] = str2[6] = str2[7] = str2[10] = L'\0'; - Matcher<const ::wstring&> m2 = StrCaseEq(str1); - str1[9] = str2[9] = L'\0'; - EXPECT_FALSE(m2.Matches(str2)); - - Matcher<const ::wstring&> m3 = StrCaseEq(str1); - EXPECT_TRUE(m3.Matches(str2)); - - EXPECT_FALSE(m3.Matches(str2 + L"x")); - str2.append(1, L'\0'); - EXPECT_FALSE(m3.Matches(str2)); - EXPECT_FALSE(m3.Matches(::wstring(str2, 0, 9))); -} - -TEST(GlobalWideStrCaseEqTest, CanDescribeSelf) { - Matcher< ::wstring> m = StrCaseEq(L"Hi"); - EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m)); -} - -TEST(GlobalWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) { - Matcher<const wchar_t*> m = StrCaseNe(L"Hello"); - EXPECT_TRUE(m.Matches(L"Hi")); - EXPECT_TRUE(m.Matches(nullptr)); - EXPECT_FALSE(m.Matches(L"Hello")); - EXPECT_FALSE(m.Matches(L"hello")); - - Matcher< ::wstring> m2 = StrCaseNe(::wstring(L"Hello")); - EXPECT_TRUE(m2.Matches(L"")); - EXPECT_FALSE(m2.Matches(L"Hello")); -} - -TEST(GlobalWideStrCaseNeTest, CanDescribeSelf) { - Matcher<const wchar_t*> m = StrCaseNe(L"Hi"); - EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m)); -} - -// Tests that HasSubstr() works for matching wstring-typed values. -TEST(GlobalWideHasSubstrTest, WorksForStringClasses) { - const Matcher< ::wstring> m1 = HasSubstr(L"foo"); - EXPECT_TRUE(m1.Matches(::wstring(L"I love food."))); - EXPECT_FALSE(m1.Matches(::wstring(L"tofo"))); - - const Matcher<const ::wstring&> m2 = HasSubstr(L"foo"); - EXPECT_TRUE(m2.Matches(::wstring(L"I love food."))); - EXPECT_FALSE(m2.Matches(::wstring(L"tofo"))); -} - -// Tests that HasSubstr() works for matching C-wide-string-typed values. -TEST(GlobalWideHasSubstrTest, WorksForCStrings) { - const Matcher<wchar_t*> m1 = HasSubstr(L"foo"); - EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food."))); - EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo"))); - EXPECT_FALSE(m1.Matches(nullptr)); - - const Matcher<const wchar_t*> m2 = HasSubstr(L"foo"); - EXPECT_TRUE(m2.Matches(L"I love food.")); - EXPECT_FALSE(m2.Matches(L"tofo")); - EXPECT_FALSE(m2.Matches(nullptr)); -} - -// Tests that HasSubstr(s) describes itself properly. -TEST(GlobalWideHasSubstrTest, CanDescribeSelf) { - Matcher< ::wstring> m = HasSubstr(L"foo\n\""); - EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m)); -} - -// Tests StartsWith(s). - -TEST(GlobalWideStartsWithTest, MatchesStringWithGivenPrefix) { - const Matcher<const wchar_t*> m1 = StartsWith(::wstring(L"")); - EXPECT_TRUE(m1.Matches(L"Hi")); - EXPECT_TRUE(m1.Matches(L"")); - EXPECT_FALSE(m1.Matches(nullptr)); - - const Matcher<const ::wstring&> m2 = StartsWith(L"Hi"); - EXPECT_TRUE(m2.Matches(L"Hi")); - EXPECT_TRUE(m2.Matches(L"Hi Hi!")); - EXPECT_TRUE(m2.Matches(L"High")); - EXPECT_FALSE(m2.Matches(L"H")); - EXPECT_FALSE(m2.Matches(L" Hi")); -} - -TEST(GlobalWideStartsWithTest, CanDescribeSelf) { - Matcher<const ::wstring> m = StartsWith(L"Hi"); - EXPECT_EQ("starts with L\"Hi\"", Describe(m)); -} - -// Tests EndsWith(s). - -TEST(GlobalWideEndsWithTest, MatchesStringWithGivenSuffix) { - const Matcher<const wchar_t*> m1 = EndsWith(L""); - EXPECT_TRUE(m1.Matches(L"Hi")); - EXPECT_TRUE(m1.Matches(L"")); - EXPECT_FALSE(m1.Matches(nullptr)); - - const Matcher<const ::wstring&> m2 = EndsWith(::wstring(L"Hi")); - EXPECT_TRUE(m2.Matches(L"Hi")); - EXPECT_TRUE(m2.Matches(L"Wow Hi Hi")); - EXPECT_TRUE(m2.Matches(L"Super Hi")); - EXPECT_FALSE(m2.Matches(L"i")); - EXPECT_FALSE(m2.Matches(L"Hi ")); -} - -TEST(GlobalWideEndsWithTest, CanDescribeSelf) { - Matcher<const ::wstring> m = EndsWith(L"Hi"); - EXPECT_EQ("ends with L\"Hi\"", Describe(m)); -} - -#endif // GTEST_HAS_GLOBAL_WSTRING - typedef ::std::tuple<long, int> Tuple2; // NOLINT // Tests that Eq() matches a 2-tuple where the first field == the @@ -2307,6 +2046,15 @@ TEST(Ne2Test, CanDescribeSelf) { EXPECT_EQ("are an unequal pair", Describe(m)); } +TEST(PairMatchBaseTest, WorksWithMoveOnly) { + using Pointers = std::tuple<std::unique_ptr<int>, std::unique_ptr<int>>; + Matcher<Pointers> matcher = Eq(); + Pointers pointers; + // Tested values don't matter; the point is that matcher does not copy the + // matched values. + EXPECT_TRUE(matcher.Matches(pointers)); +} + // Tests that FloatEq() matches a 2-tuple where // FloatEq(first field) matches the second field. TEST(FloatEq2Test, MatchesEqualArguments) { @@ -2660,7 +2408,6 @@ static void AnyOfMatches(int num, const Matcher<int>& m) { EXPECT_FALSE(m.Matches(num + 1)); } -#if GTEST_LANG_CXX11 static void AnyOfStringMatches(int num, const Matcher<std::string>& m) { SCOPED_TRACE(Describe(m)); EXPECT_FALSE(m.Matches(std::to_string(0))); @@ -2670,7 +2417,6 @@ static void AnyOfStringMatches(int num, const Matcher<std::string>& m) { } EXPECT_FALSE(m.Matches(std::to_string(num + 1))); } -#endif // Tests that AnyOf(m1, ..., mn) matches any value that matches at // least one of the given matchers. @@ -2715,7 +2461,6 @@ TEST(AnyOfTest, MatchesWhenAnyMatches) { AnyOfMatches(10, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); } -#if GTEST_LANG_CXX11 // Tests the variadic version of the AnyOfMatcher. TEST(AnyOfTest, VariadicMatchesWhenAnyMatches) { // Also make sure AnyOf is defined in the right namespace and does not depend @@ -2748,7 +2493,7 @@ TEST(ElementsAreTest, HugeMatcher) { // Tests the variadic version of the UnorderedElementsAreMatcher TEST(ElementsAreTest, HugeMatcherStr) { - vector<string> test_vector{ + vector<std::string> test_vector{ "literal_string", "", "", "", "", "", "", "", "", "", "", ""}; EXPECT_THAT(test_vector, UnorderedElementsAre("literal_string", _, _, _, _, _, @@ -2764,7 +2509,6 @@ TEST(ElementsAreTest, HugeMatcherUnordered) { Eq(3), Eq(9), Eq(12), Eq(11), Ne(122))); } -#endif // GTEST_LANG_CXX11 // Tests that AnyOf(m1, ..., mn) describes itself properly. TEST(AnyOfTest, CanDescribeSelf) { @@ -2896,7 +2640,7 @@ class IsGreaterThan { // For testing Truly(). const int foo = 0; -// This predicate returns true iff the argument references foo and has +// This predicate returns true if the argument references foo and has // a zero value. bool ReferencesFooAndIsZero(const int& n) { return (&n == &foo) && (n == 0); @@ -3166,20 +2910,8 @@ TEST(MatcherAssertionTest, WorksForByRefArguments) { "Actual: 0" + OfType("int") + ", which is located @"); } -#if !GTEST_OS_SYMBIAN // Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is // monomorphic. - -// ASSERT_THAT("hello", starts_with_he) fails to compile with Nokia's -// Symbian compiler: it tries to compile -// template<T, U> class MatcherCastImpl { ... -// virtual bool MatchAndExplain(T x, ...) const { -// return source_matcher_.MatchAndExplain(static_cast<U>(x), ...); -// with U == string and T == const char* -// With ASSERT_THAT("hello"...) changed to ASSERT_THAT(string("hello") ... ) -// the compiler silently crashes with no output. -// If MatcherCastImpl is changed to use U(x) instead of static_cast<U>(x) -// the code compiles but the converted string is bogus. TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) { Matcher<const char*> starts_with_he = StartsWith("he"); ASSERT_THAT("hello", starts_with_he); @@ -3197,7 +2929,6 @@ TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) { "Expected: is > 5\n" " Actual: 5" + OfType("int")); } -#endif // !GTEST_OS_SYMBIAN // Tests floating-point matchers. template <typename RawType> @@ -3863,7 +3594,7 @@ class Uncopyable { GTEST_DISALLOW_COPY_AND_ASSIGN_(Uncopyable); }; -// Returns true iff x.value() is positive. +// Returns true if x.value() is positive. bool ValueIsPositive(const Uncopyable& x) { return x.value() > 0; } MATCHER_P(UncopyableIs, inner_matcher, "") { @@ -4135,9 +3866,7 @@ class AClass { // A getter that returns a reference to const. const std::string& s() const { return s_; } -#if GTEST_LANG_CXX11 const std::string& s_ref() const & { return s_; } -#endif void set_s(const std::string& new_s) { s_ = new_s; } @@ -4194,7 +3923,6 @@ TEST(PropertyTest, WorksForReferenceToConstProperty) { EXPECT_FALSE(m_with_name.Matches(a)); } -#if GTEST_LANG_CXX11 // Tests that Property(&Foo::property, ...) works when property() is // ref-qualified. TEST(PropertyTest, WorksForRefQualifiedProperty) { @@ -4211,7 +3939,6 @@ TEST(PropertyTest, WorksForRefQualifiedProperty) { EXPECT_FALSE(m.Matches(a)); EXPECT_FALSE(m_with_name.Matches(a)); } -#endif // Tests that Property(&Foo::property, ...) works when property() // returns a reference to non-const. @@ -4539,8 +4266,8 @@ TEST(ResultOfTest, WorksForFunctionReferences) { // Tests that ResultOf(f, ...) compiles and works as expected when f is a // function object. -struct Functor : public ::std::unary_function<int, std::string> { - result_type operator()(argument_type input) const { +struct Functor { + std::string operator()(int input) const { return IntToStringFunction(input); } }; @@ -4574,7 +4301,6 @@ TEST(ResultOfTest, WorksForPolymorphicFunctors) { EXPECT_FALSE(matcher_string.Matches("shrt")); } -#if GTEST_LANG_CXX11 TEST(ResultOfTest, WorksForPolymorphicFunctorsIgnoringResultType) { Matcher<int*> matcher = ResultOf(PolymorphicFunctor(), "good ptr"); @@ -4584,12 +4310,14 @@ TEST(ResultOfTest, WorksForPolymorphicFunctorsIgnoringResultType) { } TEST(ResultOfTest, WorksForLambdas) { - Matcher<int> matcher = - ResultOf([](int str_len) { return std::string(str_len, 'x'); }, "xxx"); + Matcher<int> matcher = ResultOf( + [](int str_len) { + return std::string(static_cast<size_t>(str_len), 'x'); + }, + "xxx"); EXPECT_TRUE(matcher.Matches(3)); EXPECT_FALSE(matcher.Matches(1)); } -#endif const int* ReferencingFunction(const int& n) { return &n; } @@ -4752,6 +4480,12 @@ TEST(IsEmptyTest, ExplainsResult) { EXPECT_EQ("whose size is 1", Explain(m, container)); } +TEST(IsEmptyTest, WorksWithMoveOnly) { + ContainerHelper helper; + EXPECT_CALL(helper, Call(IsEmpty())); + helper.Call({}); +} + TEST(IsTrueTest, IsTrueIsFalse) { EXPECT_THAT(true, IsTrue()); EXPECT_THAT(false, IsFalse()); @@ -4774,7 +4508,6 @@ TEST(IsTrueTest, IsTrueIsFalse) { EXPECT_THAT(&a, Not(IsFalse())); EXPECT_THAT(false, Not(IsTrue())); EXPECT_THAT(true, Not(IsFalse())); -#if GTEST_LANG_CXX11 EXPECT_THAT(std::true_type(), IsTrue()); EXPECT_THAT(std::true_type(), Not(IsFalse())); EXPECT_THAT(std::false_type(), IsFalse()); @@ -4787,7 +4520,6 @@ TEST(IsTrueTest, IsTrueIsFalse) { EXPECT_THAT(null_unique, IsFalse()); EXPECT_THAT(nonnull_unique, IsTrue()); EXPECT_THAT(nonnull_unique, Not(IsFalse())); -#endif // GTEST_LANG_CXX11 } TEST(SizeIsTest, ImplementsSizeIs) { @@ -4822,6 +4554,12 @@ TEST(SizeIsTest, WorksWithReferences) { EXPECT_THAT(container, m); } +TEST(SizeIsTest, WorksWithMoveOnly) { + ContainerHelper helper; + EXPECT_CALL(helper, Call(SizeIs(3))); + helper.Call(MakeUniquePtrs({1, 2, 3})); +} + // SizeIs should work for any type that provides a size() member function. // For example, a size_type member type should not need to be provided. struct MinimalistCustomType { @@ -4873,7 +4611,7 @@ typedef testing::Types< list<int> > ContainerEqTestTypes; -TYPED_TEST_CASE(ContainerEqTest, ContainerEqTestTypes); +TYPED_TEST_SUITE(ContainerEqTest, ContainerEqTestTypes); // Tests that the filled container is equal to itself. TYPED_TEST(ContainerEqTest, EqualsSelf) { @@ -5281,7 +5019,6 @@ TEST(StreamlikeTest, Iteration) { } } -#if GTEST_HAS_STD_FORWARD_LIST_ TEST(BeginEndDistanceIsTest, WorksWithForwardList) { std::forward_list<int> container; EXPECT_THAT(container, BeginEndDistanceIs(0)); @@ -5293,7 +5030,6 @@ TEST(BeginEndDistanceIsTest, WorksWithForwardList) { EXPECT_THAT(container, Not(BeginEndDistanceIs(0))); EXPECT_THAT(container, BeginEndDistanceIs(2)); } -#endif // GTEST_HAS_STD_FORWARD_LIST_ TEST(BeginEndDistanceIsTest, WorksWithNonStdList) { const int a[5] = {1, 2, 3, 4, 5}; @@ -5308,6 +5044,12 @@ TEST(BeginEndDistanceIsTest, CanDescribeSelf) { DescribeNegation(m)); } +TEST(BeginEndDistanceIsTest, WorksWithMoveOnly) { + ContainerHelper helper; + EXPECT_CALL(helper, Call(BeginEndDistanceIs(2))); + helper.Call(MakeUniquePtrs({1, 2})); +} + TEST(BeginEndDistanceIsTest, ExplainsResult) { Matcher<vector<int> > m1 = BeginEndDistanceIs(2); Matcher<vector<int> > m2 = BeginEndDistanceIs(Lt(2)); @@ -5469,13 +5211,19 @@ TEST(IsSupersetOfTest, MatchAndExplain) { " - element #2 is matched by matcher #0")); } -#if GTEST_HAS_STD_INITIALIZER_LIST_ TEST(IsSupersetOfTest, WorksForRhsInitializerList) { const int numbers[] = {1, 3, 6, 2, 4, 5}; EXPECT_THAT(numbers, IsSupersetOf({1, 2})); EXPECT_THAT(numbers, Not(IsSupersetOf({3, 0}))); } -#endif + +TEST(IsSupersetOfTest, WorksWithMoveOnly) { + ContainerHelper helper; + EXPECT_CALL(helper, Call(IsSupersetOf({Pointee(1)}))); + helper.Call(MakeUniquePtrs({1, 2})); + EXPECT_CALL(helper, Call(Not(IsSupersetOf({Pointee(1), Pointee(2)})))); + helper.Call(MakeUniquePtrs({2})); +} TEST(IsSubsetOfTest, WorksForNativeArray) { const int subset[] = {1, 4}; @@ -5591,13 +5339,19 @@ TEST(IsSubsetOfTest, MatchAndExplain) { " - element #1 is matched by matcher #2")); } -#if GTEST_HAS_STD_INITIALIZER_LIST_ TEST(IsSubsetOfTest, WorksForRhsInitializerList) { const int numbers[] = {1, 2, 3}; EXPECT_THAT(numbers, IsSubsetOf({1, 2, 3, 4})); EXPECT_THAT(numbers, Not(IsSubsetOf({1, 2}))); } -#endif + +TEST(IsSubsetOfTest, WorksWithMoveOnly) { + ContainerHelper helper; + EXPECT_CALL(helper, Call(IsSubsetOf({Pointee(1), Pointee(2)}))); + helper.Call(MakeUniquePtrs({1})); + EXPECT_CALL(helper, Call(Not(IsSubsetOf({Pointee(1)})))); + helper.Call(MakeUniquePtrs({2})); +} // Tests using ElementsAre() and ElementsAreArray() with stream-like // "containers". @@ -5632,6 +5386,15 @@ TEST(ElementsAreTest, WorksWithUncopyable) { EXPECT_THAT(objs, ElementsAre(UncopyableIs(-3), Truly(ValueIsPositive))); } +TEST(ElementsAreTest, WorksWithMoveOnly) { + ContainerHelper helper; + EXPECT_CALL(helper, Call(ElementsAre(Pointee(1), Pointee(2)))); + helper.Call(MakeUniquePtrs({1, 2})); + + EXPECT_CALL(helper, Call(ElementsAreArray({Pointee(3), Pointee(4)}))); + helper.Call(MakeUniquePtrs({3, 4})); +} + TEST(ElementsAreTest, TakesStlContainer) { const int actual[] = {3, 1, 2}; @@ -5699,7 +5462,6 @@ TEST(UnorderedElementsAreArrayTest, TakesStlContainer) { EXPECT_THAT(actual, Not(UnorderedElementsAreArray(expected))); } -#if GTEST_HAS_STD_INITIALIZER_LIST_ TEST(UnorderedElementsAreArrayTest, TakesInitializerList) { const int a[5] = {2, 1, 4, 5, 3}; @@ -5733,7 +5495,13 @@ TEST(UnorderedElementsAreArrayTest, {Eq(1), Ne(-2), Ge(3), Le(4), Eq(6)}))); } -#endif // GTEST_HAS_STD_INITIALIZER_LIST_ + +TEST(UnorderedElementsAreArrayTest, WorksWithMoveOnly) { + ContainerHelper helper; + EXPECT_CALL(helper, + Call(UnorderedElementsAreArray({Pointee(1), Pointee(2)}))); + helper.Call(MakeUniquePtrs({2, 1})); +} class UnorderedElementsAreTest : public testing::Test { protected: @@ -5782,6 +5550,12 @@ TEST_F(UnorderedElementsAreTest, WorksForStreamlike) { EXPECT_THAT(s, Not(UnorderedElementsAre(2, 2, 3, 4, 5))); } +TEST_F(UnorderedElementsAreTest, WorksWithMoveOnly) { + ContainerHelper helper; + EXPECT_CALL(helper, Call(UnorderedElementsAre(Pointee(1), Pointee(2)))); + helper.Call(MakeUniquePtrs({2, 1})); +} + // One naive implementation of the matcher runs in O(N!) time, which is too // slow for many real-world inputs. This test shows that our matcher can match // 100 inputs very quickly (a few milliseconds). An O(100!) is 10^158 @@ -6041,11 +5815,11 @@ class BacktrackingBPMTest : public ::testing::Test { }; // Tests the MaxBipartiteMatching algorithm with square matrices. // The single int param is the # of nodes on each of the left and right sides. -class BipartiteTest : public ::testing::TestWithParam<int> { }; +class BipartiteTest : public ::testing::TestWithParam<size_t> {}; // Verify all match graphs up to some moderate number of edges. TEST_P(BipartiteTest, Exhaustive) { - int nodes = GetParam(); + size_t nodes = GetParam(); MatchMatrix graph(nodes, nodes); do { ElementMatcherPairs matches = @@ -6069,8 +5843,8 @@ TEST_P(BipartiteTest, Exhaustive) { } while (graph.NextGraph()); } -INSTANTIATE_TEST_CASE_P(AllGraphs, BipartiteTest, - ::testing::Range(0, 5)); +INSTANTIATE_TEST_SUITE_P(AllGraphs, BipartiteTest, + ::testing::Range(size_t{0}, size_t{5})); // Parameterized by a pair interpreted as (LhsSize, RhsSize). class BipartiteNonSquareTest @@ -6086,7 +5860,7 @@ TEST_F(BipartiteNonSquareTest, SimpleBacktracking) { // :.......: // 0 1 2 MatchMatrix g(4, 3); - static const int kEdges[][2] = {{0, 2}, {1, 1}, {2, 1}, {3, 0}}; + static const size_t kEdges[][2] = {{0, 2}, {1, 1}, {2, 1}, {3, 0}}; for (size_t i = 0; i < GTEST_ARRAY_SIZE_(kEdges); ++i) { g.SetEdge(kEdges[i][0], kEdges[i][1], true); } @@ -6112,7 +5886,7 @@ TEST_P(BipartiteNonSquareTest, Exhaustive) { } while (graph.NextGraph()); } -INSTANTIATE_TEST_CASE_P(AllGraphs, BipartiteNonSquareTest, +INSTANTIATE_TEST_SUITE_P(AllGraphs, BipartiteNonSquareTest, testing::Values( std::make_pair(1, 2), std::make_pair(2, 1), @@ -6131,15 +5905,15 @@ class BipartiteRandomTest TEST_P(BipartiteRandomTest, LargerNets) { int nodes = GetParam().first; int iters = GetParam().second; - MatchMatrix graph(nodes, nodes); + MatchMatrix graph(static_cast<size_t>(nodes), static_cast<size_t>(nodes)); - testing::internal::Int32 seed = GTEST_FLAG(random_seed); + auto seed = static_cast<testing::internal::UInt32>(GTEST_FLAG(random_seed)); if (seed == 0) { - seed = static_cast<testing::internal::Int32>(time(nullptr)); + seed = static_cast<testing::internal::UInt32>(time(nullptr)); } for (; iters > 0; --iters, ++seed) { - srand(static_cast<int>(seed)); + srand(static_cast<unsigned int>(seed)); graph.Randomize(); EXPECT_EQ(FindBacktrackingMaxBPM(graph).size(), internal::FindMaxBipartiteMatching(graph).size()) @@ -6150,7 +5924,7 @@ TEST_P(BipartiteRandomTest, LargerNets) { } // Test argument is a std::pair<int, int> representing (nodes, iters). -INSTANTIATE_TEST_CASE_P(Samples, BipartiteRandomTest, +INSTANTIATE_TEST_SUITE_P(Samples, BipartiteRandomTest, testing::Values( std::make_pair(5, 10000), std::make_pair(6, 5000), @@ -6332,6 +6106,12 @@ TEST(EachTest, WorksForNativeArrayAsTuple) { EXPECT_THAT(std::make_tuple(pointer, 2), Not(Each(Gt(1)))); } +TEST(EachTest, WorksWithMoveOnly) { + ContainerHelper helper; + EXPECT_CALL(helper, Call(Each(Pointee(Gt(0))))); + helper.Call(MakeUniquePtrs({1, 2})); +} + // For testing Pointwise(). class IsHalfOfMatcher { public: @@ -6419,7 +6199,6 @@ TEST(PointwiseTest, WorksForVectorOfBool) { EXPECT_THAT(lhs, Not(Pointwise(Eq(), rhs))); } -#if GTEST_HAS_STD_INITIALIZER_LIST_ TEST(PointwiseTest, WorksForRhsInitializerList) { const vector<int> lhs{2, 4, 6}; @@ -6427,7 +6206,6 @@ TEST(PointwiseTest, WorksForRhsInitializerList) { EXPECT_THAT(lhs, Not(Pointwise(Lt(), {3, 3, 7}))); } -#endif // GTEST_HAS_STD_INITIALIZER_LIST_ TEST(PointwiseTest, RejectsWrongSize) { const double lhs[2] = {1, 2}; @@ -6470,6 +6248,17 @@ TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) { EXPECT_EQ("", Explain(Pointwise(m2, rhs), lhs)); } +MATCHER(PointeeEquals, "Points to an equal value") { + return ExplainMatchResult(::testing::Pointee(::testing::get<1>(arg)), + ::testing::get<0>(arg), result_listener); +} + +TEST(PointwiseTest, WorksWithMoveOnly) { + ContainerHelper helper; + EXPECT_CALL(helper, Call(Pointwise(PointeeEquals(), std::vector<int>{1, 2}))); + helper.Call(MakeUniquePtrs({1, 2})); +} + TEST(UnorderedPointwiseTest, DescribesSelf) { vector<int> rhs; rhs.push_back(1); @@ -6530,7 +6319,6 @@ TEST(UnorderedPointwiseTest, WorksForRhsNativeArray) { EXPECT_THAT(lhs, Not(UnorderedPointwise(Lt(), rhs))); } -#if GTEST_HAS_STD_INITIALIZER_LIST_ TEST(UnorderedPointwiseTest, WorksForRhsInitializerList) { const vector<int> lhs{2, 4, 6}; @@ -6538,7 +6326,6 @@ TEST(UnorderedPointwiseTest, WorksForRhsInitializerList) { EXPECT_THAT(lhs, Not(UnorderedPointwise(Lt(), {1, 1, 7}))); } -#endif // GTEST_HAS_STD_INITIALIZER_LIST_ TEST(UnorderedPointwiseTest, RejectsWrongSize) { const double lhs[2] = {1, 2}; @@ -6584,51 +6371,62 @@ TEST(UnorderedPointwiseTest, AllowsMonomorphicInnerMatcher) { EXPECT_THAT(lhs, UnorderedPointwise(m2, rhs)); } +TEST(UnorderedPointwiseTest, WorksWithMoveOnly) { + ContainerHelper helper; + EXPECT_CALL(helper, Call(UnorderedPointwise(PointeeEquals(), + std::vector<int>{1, 2}))); + helper.Call(MakeUniquePtrs({2, 1})); +} + // Sample optional type implementation with minimal requirements for use with // Optional matcher. -class SampleOptionalInt { +template <typename T> +class SampleOptional { public: - typedef int value_type; - explicit SampleOptionalInt(int value) : value_(value), has_value_(true) {} - SampleOptionalInt() : value_(0), has_value_(false) {} - operator bool() const { - return has_value_; - } - const int& operator*() const { - return value_; - } + using value_type = T; + explicit SampleOptional(T value) + : value_(std::move(value)), has_value_(true) {} + SampleOptional() : value_(), has_value_(false) {} + operator bool() const { return has_value_; } + const T& operator*() const { return value_; } + private: - int value_; + T value_; bool has_value_; }; TEST(OptionalTest, DescribesSelf) { - const Matcher<SampleOptionalInt> m = Optional(Eq(1)); + const Matcher<SampleOptional<int>> m = Optional(Eq(1)); EXPECT_EQ("value is equal to 1", Describe(m)); } TEST(OptionalTest, ExplainsSelf) { - const Matcher<SampleOptionalInt> m = Optional(Eq(1)); - EXPECT_EQ("whose value 1 matches", Explain(m, SampleOptionalInt(1))); - EXPECT_EQ("whose value 2 doesn't match", Explain(m, SampleOptionalInt(2))); + const Matcher<SampleOptional<int>> m = Optional(Eq(1)); + EXPECT_EQ("whose value 1 matches", Explain(m, SampleOptional<int>(1))); + EXPECT_EQ("whose value 2 doesn't match", Explain(m, SampleOptional<int>(2))); } TEST(OptionalTest, MatchesNonEmptyOptional) { - const Matcher<SampleOptionalInt> m1 = Optional(1); - const Matcher<SampleOptionalInt> m2 = Optional(Eq(2)); - const Matcher<SampleOptionalInt> m3 = Optional(Lt(3)); - SampleOptionalInt opt(1); + const Matcher<SampleOptional<int>> m1 = Optional(1); + const Matcher<SampleOptional<int>> m2 = Optional(Eq(2)); + const Matcher<SampleOptional<int>> m3 = Optional(Lt(3)); + SampleOptional<int> opt(1); EXPECT_TRUE(m1.Matches(opt)); EXPECT_FALSE(m2.Matches(opt)); EXPECT_TRUE(m3.Matches(opt)); } TEST(OptionalTest, DoesNotMatchNullopt) { - const Matcher<SampleOptionalInt> m = Optional(1); - SampleOptionalInt empty; + const Matcher<SampleOptional<int>> m = Optional(1); + SampleOptional<int> empty; EXPECT_FALSE(m.Matches(empty)); } +TEST(OptionalTest, WorksWithMoveOnly) { + Matcher<SampleOptional<std::unique_ptr<int>>> m = Optional(Eq(nullptr)); + EXPECT_TRUE(m.Matches(SampleOptional<std::unique_ptr<int>>(nullptr))); +} + class SampleVariantIntString { public: SampleVariantIntString(int i) : i_(i), has_int_(true) {} @@ -6636,7 +6434,7 @@ class SampleVariantIntString { template <typename T> friend bool holds_alternative(const SampleVariantIntString& value) { - return value.has_int_ == internal::IsSame<T, int>::value; + return value.has_int_ == std::is_same<T, int>::value; } template <typename T> @@ -6724,7 +6522,6 @@ TEST(AnyWithTest, TestBadCastType) { EXPECT_FALSE(m.Matches(SampleAnyType(1))); } -#if GTEST_LANG_CXX11 TEST(AnyWithTest, TestUseInContainers) { std::vector<SampleAnyType> a; a.emplace_back(1); @@ -6741,7 +6538,6 @@ TEST(AnyWithTest, TestUseInContainers) { AnyWith<std::string>("merhaba"), AnyWith<std::string>("salut")})); } -#endif // GTEST_LANG_CXX11 TEST(AnyWithTest, TestCompare) { EXPECT_THAT(SampleAnyType(1), AnyWith<int>(Gt(0))); } @@ -6884,10 +6680,10 @@ TEST(ArgsTest, ExplainsMatchResultWithoutInnerExplanation) { // For testing Args<>'s explanation. class LessThanMatcher : public MatcherInterface<std::tuple<char, int> > { public: - virtual void DescribeTo(::std::ostream* os) const {} + void DescribeTo(::std::ostream* /*os*/) const override {} - virtual bool MatchAndExplain(std::tuple<char, int> value, - MatchResultListener* listener) const { + bool MatchAndExplain(std::tuple<char, int> value, + MatchResultListener* listener) const override { const int diff = std::get<0>(value) - std::get<1>(value); if (diff > 0) { *listener << "where the first value is " << diff @@ -6958,10 +6754,6 @@ class PredicateFormatterFromMatcherTest : public ::testing::Test { matcher); return predicate_formatter("dummy-name", behavior); } - - const std::string kMatcherType = - "testing::gmock_matchers_test::PredicateFormatterFromMatcherTest::" - "Behavior"; }; TEST_F(PredicateFormatterFromMatcherTest, ShortCircuitOnSuccess) { @@ -6976,8 +6768,7 @@ TEST_F(PredicateFormatterFromMatcherTest, NoShortCircuitOnFailure) { EXPECT_FALSE(result); // Implicit cast to bool. std::string expect = "Value of: dummy-name\nExpected: [DescribeTo]\n" - " Actual: 1" + - OfType(kMatcherType) + ", [MatchAndExplain]"; + " Actual: 1, [MatchAndExplain]"; EXPECT_EQ(expect, result.message()); } @@ -6988,11 +6779,11 @@ TEST_F(PredicateFormatterFromMatcherTest, DetectsFlakyShortCircuit) { "Value of: dummy-name\nExpected: [DescribeTo]\n" " The matcher failed on the initial attempt; but passed when rerun to " "generate the explanation.\n" - " Actual: 2" + - OfType(kMatcherType) + ", [MatchAndExplain]"; + " Actual: 2, [MatchAndExplain]"; EXPECT_EQ(expect, result.message()); } +} // namespace } // namespace gmock_matchers_test } // namespace testing diff --git a/googlemock/test/gmock-nice-strict_test.cc b/googlemock/test/gmock-nice-strict_test.cc index d00f4536..0a201ed7 100644 --- a/googlemock/test/gmock-nice-strict_test.cc +++ b/googlemock/test/gmock-nice-strict_test.cc @@ -27,8 +27,7 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -#include "gmock/gmock-generated-nice-strict.h" +#include "gmock/gmock-nice-strict.h" #include <string> #include <utility> @@ -114,23 +113,22 @@ class MockBar { GTEST_DISALLOW_COPY_AND_ASSIGN_(MockBar); }; -#if GTEST_GTEST_LANG_CXX11 class MockBaz { public: class MoveOnly { + public: MoveOnly() = default; MoveOnly(const MoveOnly&) = delete; - operator=(const MoveOnly&) = delete; + MoveOnly& operator=(const MoveOnly&) = delete; MoveOnly(MoveOnly&&) = default; - operator=(MoveOnly&&) = default; + MoveOnly& operator=(MoveOnly&&) = default; }; MockBaz(MoveOnly) {} -} -#endif // GTEST_GTEST_LANG_CXX11 && GTEST_HAS_STD_MOVE_ +}; #if GTEST_HAS_STREAM_REDIRECTION @@ -292,29 +290,17 @@ TEST(NiceMockTest, AllowLeak) { leaked->DoThis(); } -#if GTEST_GTEST_LANG_CXX11 && GTEST_HAS_STD_MOVE_ - TEST(NiceMockTest, MoveOnlyConstructor) { - NiceMock<MockBaz> nice_baz(MockBaz::MoveOnly()); + NiceMock<MockBaz> nice_baz(MockBaz::MoveOnly{}); } -#endif // GTEST_LANG_CXX11 && GTEST_HAS_STD_MOVE_ - -#if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE // Tests that NiceMock<Mock> compiles where Mock is a user-defined -// class (as opposed to ::testing::Mock). We had to work around an -// MSVC 8.0 bug that caused the symbol Mock used in the definition of -// NiceMock to be looked up in the wrong context, and this test -// ensures that our fix works. -// -// We have to skip this test on Symbian and Windows Mobile, as it -// causes the program to crash there, for reasons unclear to us yet. +// class (as opposed to ::testing::Mock). TEST(NiceMockTest, AcceptsClassNamedMock) { NiceMock< ::Mock> nice; EXPECT_CALL(nice, DoThis()); nice.DoThis(); } -#endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE TEST(NiceMockTest, IsNaggy_IsNice_IsStrict) { NiceMock<MockFoo> nice_foo; @@ -407,29 +393,17 @@ TEST(NaggyMockTest, AllowLeak) { leaked->DoThis(); } -#if GTEST_GTEST_LANG_CXX11 && GTEST_HAS_STD_MOVE_ - TEST(NaggyMockTest, MoveOnlyConstructor) { - NaggyMock<MockBaz> naggy_baz(MockBaz::MoveOnly()); + NaggyMock<MockBaz> naggy_baz(MockBaz::MoveOnly{}); } -#endif // GTEST_LANG_CXX11 && GTEST_HAS_STD_MOVE_ - -#if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE // Tests that NaggyMock<Mock> compiles where Mock is a user-defined -// class (as opposed to ::testing::Mock). We had to work around an -// MSVC 8.0 bug that caused the symbol Mock used in the definition of -// NaggyMock to be looked up in the wrong context, and this test -// ensures that our fix works. -// -// We have to skip this test on Symbian and Windows Mobile, as it -// causes the program to crash there, for reasons unclear to us yet. +// class (as opposed to ::testing::Mock). TEST(NaggyMockTest, AcceptsClassNamedMock) { NaggyMock< ::Mock> naggy; EXPECT_CALL(naggy, DoThis()); naggy.DoThis(); } -#endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE TEST(NaggyMockTest, IsNaggy_IsNice_IsStrict) { NaggyMock<MockFoo> naggy_foo; @@ -503,29 +477,17 @@ TEST(StrictMockTest, AllowLeak) { leaked->DoThis(); } -#if GTEST_GTEST_LANG_CXX11 && GTEST_HAS_STD_MOVE_ - TEST(StrictMockTest, MoveOnlyConstructor) { - StrictMock<MockBaz> strict_baz(MockBaz::MoveOnly()); + StrictMock<MockBaz> strict_baz(MockBaz::MoveOnly{}); } -#endif // GTEST_LANG_CXX11 && GTEST_HAS_STD_MOVE_ - -#if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE // Tests that StrictMock<Mock> compiles where Mock is a user-defined -// class (as opposed to ::testing::Mock). We had to work around an -// MSVC 8.0 bug that caused the symbol Mock used in the definition of -// StrictMock to be looked up in the wrong context, and this test -// ensures that our fix works. -// -// We have to skip this test on Symbian and Windows Mobile, as it -// causes the program to crash there, for reasons unclear to us yet. +// class (as opposed to ::testing::Mock). TEST(StrictMockTest, AcceptsClassNamedMock) { StrictMock< ::Mock> strict; EXPECT_CALL(strict, DoThis()); strict.DoThis(); } -#endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE TEST(StrictMockTest, IsNaggy_IsNice_IsStrict) { StrictMock<MockFoo> strict_foo; diff --git a/googlemock/test/gmock-spec-builders_test.cc b/googlemock/test/gmock-spec-builders_test.cc index 557abaea..95b4b8b7 100644 --- a/googlemock/test/gmock-spec-builders_test.cc +++ b/googlemock/test/gmock-spec-builders_test.cc @@ -78,6 +78,7 @@ using testing::Expectation; using testing::ExpectationSet; using testing::GMOCK_FLAG(verbose); using testing::Gt; +using testing::IgnoreResult; using testing::InSequence; using testing::Invoke; using testing::InvokeWithoutArgs; @@ -1951,12 +1952,12 @@ TEST(DeletingMockEarlyTest, Failure2) { class EvenNumberCardinality : public CardinalityInterface { public: - // Returns true iff call_count calls will satisfy this cardinality. + // Returns true if call_count calls will satisfy this cardinality. bool IsSatisfiedByCallCount(int call_count) const override { return call_count % 2 == 0; } - // Returns true iff call_count calls will saturate this cardinality. + // Returns true if call_count calls will saturate this cardinality. bool IsSaturatedByCallCount(int /* call_count */) const override { return false; } @@ -2177,7 +2178,7 @@ class GMockVerboseFlagTest : public VerboseFlagPreservingFixture { "an EXPECT_CALL() if you don't mean to enforce the call. " "See " "https://github.com/google/googletest/blob/master/googlemock/docs/" - "CookBook.md#" + "cook_book.md#" "knowing-when-to-expect for details."; // A void-returning function. diff --git a/googlemock/test/gmock_all_test.cc b/googlemock/test/gmock_all_test.cc index e1774fbb..b2b2027d 100644 --- a/googlemock/test/gmock_all_test.cc +++ b/googlemock/test/gmock_all_test.cc @@ -39,7 +39,6 @@ #include "test/gmock-cardinalities_test.cc" #include "test/gmock-generated-actions_test.cc" #include "test/gmock-generated-function-mockers_test.cc" -#include "test/gmock-generated-internal-utils_test.cc" #include "test/gmock-generated-matchers_test.cc" #include "test/gmock-internal-utils_test.cc" #include "test/gmock-matchers_test.cc" diff --git a/googlemock/test/gmock_output_test.py b/googlemock/test/gmock_output_test.py index 0527bd93..25f99f2b 100755 --- a/googlemock/test/gmock_output_test.py +++ b/googlemock/test/gmock_output_test.py @@ -39,6 +39,7 @@ gmock_output_test.py """ +from io import open # pylint: disable=redefined-builtin, g-importing-member import os import re import sys @@ -152,10 +153,11 @@ def GetNormalizedCommandOutputAndLeakyTests(cmd): class GMockOutputTest(gmock_test_utils.TestCase): + def testOutput(self): (output, leaky_tests) = GetNormalizedCommandOutputAndLeakyTests(COMMAND) golden_file = open(GOLDEN_PATH, 'rb') - golden = golden_file.read() + golden = golden_file.read().decode('utf-8') golden_file.close() # The normalized output should match the golden file. diff --git a/googlemock/test/gmock_output_test_golden.txt b/googlemock/test/gmock_output_test_golden.txt index dbcb2118..4c90b41a 100644 --- a/googlemock/test/gmock_output_test_golden.txt +++ b/googlemock/test/gmock_output_test_golden.txt @@ -75,14 +75,14 @@ GMOCK WARNING: Uninteresting mock function call - returning default value. Function call: Bar2(0, 1) Returns: false -NOTE: You can safely ignore the above warning unless this call should not happen. Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call. See https://github.com/google/googletest/blob/master/googlemock/docs/CookBook.md#knowing-when-to-expect for details. +NOTE: You can safely ignore the above warning unless this call should not happen. Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call. See https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md#knowing-when-to-expect for details. [ OK ] GMockOutputTest.UninterestingCall [ RUN ] GMockOutputTest.UninterestingCallToVoidFunction GMOCK WARNING: Uninteresting mock function call - returning directly. Function call: Bar3(0, 1) -NOTE: You can safely ignore the above warning unless this call should not happen. Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call. See https://github.com/google/googletest/blob/master/googlemock/docs/CookBook.md#knowing-when-to-expect for details. +NOTE: You can safely ignore the above warning unless this call should not happen. Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call. See https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md#knowing-when-to-expect for details. [ OK ] GMockOutputTest.UninterestingCallToVoidFunction [ RUN ] GMockOutputTest.RetiredExpectation unknown file: Failure @@ -266,14 +266,14 @@ Uninteresting mock function call - taking default action specified at: FILE:#: Function call: Bar2(2, 2) Returns: true -NOTE: You can safely ignore the above warning unless this call should not happen. Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call. See https://github.com/google/googletest/blob/master/googlemock/docs/CookBook.md#knowing-when-to-expect for details. +NOTE: You can safely ignore the above warning unless this call should not happen. Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call. See https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md#knowing-when-to-expect for details. GMOCK WARNING: Uninteresting mock function call - taking default action specified at: FILE:#: Function call: Bar2(1, 1) Returns: false -NOTE: You can safely ignore the above warning unless this call should not happen. Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call. See https://github.com/google/googletest/blob/master/googlemock/docs/CookBook.md#knowing-when-to-expect for details. +NOTE: You can safely ignore the above warning unless this call should not happen. Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call. See https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md#knowing-when-to-expect for details. [ OK ] GMockOutputTest.UninterestingCallWithDefaultAction [ RUN ] GMockOutputTest.ExplicitActionsRunOutWithDefaultAction |