diff options
Diffstat (limited to 'testing/gmock/test')
-rw-r--r-- | testing/gmock/test/gmock-actions_test.cc | 219 | ||||
-rw-r--r-- | testing/gmock/test/gmock-generated-actions_test.cc | 497 | ||||
-rw-r--r-- | testing/gmock/test/gmock-generated-function-mockers_test.cc | 44 | ||||
-rw-r--r-- | testing/gmock/test/gmock-internal-utils_test.cc | 66 | ||||
-rw-r--r-- | testing/gmock/test/gmock-matchers_test.cc | 297 | ||||
-rw-r--r-- | testing/gmock/test/gmock-more-actions_test.cc | 664 | ||||
-rw-r--r-- | testing/gmock/test/gmock-nice-strict_test.cc | 39 | ||||
-rw-r--r-- | testing/gmock/test/gmock-port_test.cc | 58 | ||||
-rw-r--r-- | testing/gmock/test/gmock-printers_test.cc | 43 | ||||
-rw-r--r-- | testing/gmock/test/gmock-spec-builders_test.cc | 370 | ||||
-rw-r--r--[-rwxr-xr-x] | testing/gmock/test/gmock_leak_test.py | 0 | ||||
-rw-r--r-- | testing/gmock/test/gmock_link_test.h | 22 | ||||
-rw-r--r--[-rwxr-xr-x] | testing/gmock/test/gmock_output_test.py | 0 | ||||
-rw-r--r--[-rwxr-xr-x] | testing/gmock/test/gmock_test_utils.py | 2 |
14 files changed, 1540 insertions, 781 deletions
diff --git a/testing/gmock/test/gmock-actions_test.cc b/testing/gmock/test/gmock-actions_test.cc index 2446260..d3d96c6 100644 --- a/testing/gmock/test/gmock-actions_test.cc +++ b/testing/gmock/test/gmock-actions_test.cc @@ -56,6 +56,7 @@ using testing::_; using testing::Action; using testing::ActionInterface; using testing::Assign; +using testing::ByRef; using testing::DefaultValue; using testing::DoDefault; using testing::IgnoreResult; @@ -68,11 +69,10 @@ using testing::Return; using testing::ReturnNull; using testing::ReturnRef; using testing::SetArgumentPointee; -using testing::SetArrayArgument; -#ifndef _WIN32_WCE +#if !GTEST_OS_WINDOWS_MOBILE using testing::SetErrnoAndReturn; -#endif // _WIN32_WCE +#endif #if GMOCK_HAS_PROTOBUF_ using testing::internal::TestMessage; @@ -98,11 +98,13 @@ TEST(BuiltInDefaultValueTest, IsZeroForNumericTypes) { EXPECT_EQ(0, BuiltInDefaultValue<unsigned char>::Get()); EXPECT_EQ(0, BuiltInDefaultValue<signed char>::Get()); EXPECT_EQ(0, BuiltInDefaultValue<char>::Get()); -#if !GTEST_OS_WINDOWS +#if GMOCK_HAS_SIGNED_WCHAR_T_ EXPECT_EQ(0, BuiltInDefaultValue<unsigned wchar_t>::Get()); EXPECT_EQ(0, BuiltInDefaultValue<signed wchar_t>::Get()); -#endif // !GTEST_OS_WINDOWS +#endif +#if GMOCK_WCHAR_T_IS_NATIVE_ EXPECT_EQ(0, BuiltInDefaultValue<wchar_t>::Get()); +#endif EXPECT_EQ(0, BuiltInDefaultValue<unsigned short>::Get()); // NOLINT EXPECT_EQ(0, BuiltInDefaultValue<signed short>::Get()); // NOLINT EXPECT_EQ(0, BuiltInDefaultValue<short>::Get()); // NOLINT @@ -124,11 +126,13 @@ TEST(BuiltInDefaultValueTest, ExistsForNumericTypes) { EXPECT_TRUE(BuiltInDefaultValue<unsigned char>::Exists()); EXPECT_TRUE(BuiltInDefaultValue<signed char>::Exists()); EXPECT_TRUE(BuiltInDefaultValue<char>::Exists()); -#if !GTEST_OS_WINDOWS +#if GMOCK_HAS_SIGNED_WCHAR_T_ EXPECT_TRUE(BuiltInDefaultValue<unsigned wchar_t>::Exists()); EXPECT_TRUE(BuiltInDefaultValue<signed wchar_t>::Exists()); -#endif // !GTEST_OS_WINDOWS +#endif +#if GMOCK_WCHAR_T_IS_NATIVE_ EXPECT_TRUE(BuiltInDefaultValue<wchar_t>::Exists()); +#endif EXPECT_TRUE(BuiltInDefaultValue<unsigned short>::Exists()); // NOLINT EXPECT_TRUE(BuiltInDefaultValue<signed short>::Exists()); // NOLINT EXPECT_TRUE(BuiltInDefaultValue<short>::Exists()); // NOLINT @@ -199,26 +203,22 @@ TEST(BuiltInDefaultValueTest, UserTypeHasNoDefault) { EXPECT_FALSE(BuiltInDefaultValue<UserType>::Exists()); } -#if GTEST_HAS_DEATH_TEST - // Tests that BuiltInDefaultValue<T&>::Get() aborts the program. TEST(BuiltInDefaultValueDeathTest, IsUndefinedForReferences) { - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ BuiltInDefaultValue<int&>::Get(); }, ""); - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ BuiltInDefaultValue<const char&>::Get(); }, ""); } TEST(BuiltInDefaultValueDeathTest, IsUndefinedForUserTypes) { - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ BuiltInDefaultValue<UserType>::Get(); }, ""); } -#endif // GTEST_HAS_DEATH_TEST - // Tests that DefaultValue<T>::IsSet() is false initially. TEST(DefaultValueTest, IsInitiallyUnset) { EXPECT_FALSE(DefaultValue<int>::IsSet()); @@ -260,11 +260,9 @@ TEST(DefaultValueDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) { EXPECT_EQ(0, DefaultValue<int>::Get()); -#if GTEST_HAS_DEATH_TEST - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ DefaultValue<UserType>::Get(); }, ""); -#endif // GTEST_HAS_DEATH_TEST } // Tests that DefaultValue<void>::Get() returns void. @@ -316,14 +314,12 @@ TEST(DefaultValueOfReferenceDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) { EXPECT_FALSE(DefaultValue<int&>::IsSet()); EXPECT_FALSE(DefaultValue<UserType&>::IsSet()); -#if GTEST_HAS_DEATH_TEST - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ DefaultValue<int&>::Get(); }, ""); - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ DefaultValue<UserType>::Get(); }, ""); -#endif // GTEST_HAS_DEATH_TEST } // Tests that ActionInterface can be implemented by defining the @@ -403,12 +399,19 @@ 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(make_tuple('a'))); EXPECT_EQ(0, a2.Perform(make_tuple('\0'))); } +#endif // !GTEST_OS_SYMBIAN // The following two classes are for testing MakePolymorphicAction(). @@ -559,15 +562,13 @@ TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) { EXPECT_EQ(0, mock.IntFunc(true)); } -#if GTEST_HAS_DEATH_TEST - // Tests that DoDefault() aborts the process when there is no built-in // default value for the return type. TEST(DoDefaultDeathTest, DiesForUnknowType) { MockClass mock; EXPECT_CALL(mock, Foo()) .WillRepeatedly(DoDefault()); - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ mock.Foo(); }, ""); } @@ -587,13 +588,11 @@ TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) { // EXPECT_DEATH() can only capture stderr, while Google Mock's // errors are printed on stdout. Therefore we have to settle for // not verifying the message. - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ mock.IntFunc(true); }, ""); } -#endif // GTEST_HAS_DEATH_TEST - // Tests that DoDefault() returns the default value set by // DefaultValue<T>::Set() when it's not overriden by an ON_CALL(). TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) { @@ -743,85 +742,6 @@ TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) { #endif // GMOCK_HAS_PROTOBUF_ -// Tests that SetArrayArgument<N>(first, last) sets the elements of the array -// pointed to by the N-th (0-based) argument to values in range [first, last). -TEST(SetArrayArgumentTest, SetsTheNthArray) { - typedef void MyFunction(bool, int*, char*); - int numbers[] = { 1, 2, 3 }; - Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers + 3); - - int n[4] = {}; - int* pn = n; - char ch[4] = {}; - char* pch = ch; - a.Perform(make_tuple(true, pn, pch)); - EXPECT_EQ(1, n[0]); - EXPECT_EQ(2, n[1]); - EXPECT_EQ(3, n[2]); - EXPECT_EQ(0, n[3]); - EXPECT_EQ('\0', ch[0]); - EXPECT_EQ('\0', ch[1]); - EXPECT_EQ('\0', ch[2]); - EXPECT_EQ('\0', ch[3]); - - // Tests first and last are iterators. - std::string letters = "abc"; - a = SetArrayArgument<2>(letters.begin(), letters.end()); - std::fill_n(n, 4, 0); - std::fill_n(ch, 4, '\0'); - a.Perform(make_tuple(true, pn, pch)); - EXPECT_EQ(0, n[0]); - EXPECT_EQ(0, n[1]); - EXPECT_EQ(0, n[2]); - EXPECT_EQ(0, n[3]); - EXPECT_EQ('a', ch[0]); - EXPECT_EQ('b', ch[1]); - EXPECT_EQ('c', ch[2]); - EXPECT_EQ('\0', ch[3]); -} - -// Tests SetArrayArgument<N>(first, last) where first == last. -TEST(SetArrayArgumentTest, SetsTheNthArrayWithEmptyRange) { - typedef void MyFunction(bool, int*); - int numbers[] = { 1, 2, 3 }; - Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers); - - int n[4] = {}; - int* pn = n; - a.Perform(make_tuple(true, pn)); - EXPECT_EQ(0, n[0]); - EXPECT_EQ(0, n[1]); - EXPECT_EQ(0, n[2]); - EXPECT_EQ(0, n[3]); -} - -// Tests SetArrayArgument<N>(first, last) where *first is convertible -// (but not equal) to the argument type. -TEST(SetArrayArgumentTest, SetsTheNthArrayWithConvertibleType) { - typedef void MyFunction(bool, char*); - int codes[] = { 97, 98, 99 }; - Action<MyFunction> a = SetArrayArgument<1>(codes, codes + 3); - - char ch[4] = {}; - char* pch = ch; - a.Perform(make_tuple(true, pch)); - EXPECT_EQ('a', ch[0]); - EXPECT_EQ('b', ch[1]); - EXPECT_EQ('c', ch[2]); - EXPECT_EQ('\0', ch[3]); -} - -// Test SetArrayArgument<N>(first, last) with iterator as argument. -TEST(SetArrayArgumentTest, SetsTheNthArrayWithIteratorArgument) { - typedef void MyFunction(bool, std::back_insert_iterator<std::string>); - std::string letters = "abc"; - Action<MyFunction> a = SetArrayArgument<1>(letters.begin(), letters.end()); - - std::string s; - a.Perform(make_tuple(true, back_inserter(s))); - EXPECT_EQ(letters, s); -} - // Sample functions and functors for testing Invoke() and etc. int Nullary() { return 1; } @@ -1002,7 +922,7 @@ TEST(AssignTest, CompatibleTypes) { EXPECT_DOUBLE_EQ(5, x); } -#ifndef _WIN32_WCE +#if !GTEST_OS_WINDOWS_MOBILE class SetErrnoAndReturnTest : public testing::Test { protected: @@ -1029,6 +949,89 @@ TEST_F(SetErrnoAndReturnTest, CompatibleTypes) { EXPECT_EQ(EINVAL, errno); } -#endif // _WIN32_WCE +#endif // !GTEST_OS_WINDOWS_MOBILE + +// Tests ByRef(). + +// Tests that ReferenceWrapper<T> 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); + const std::string& r1 = ref_wrapper; + EXPECT_EQ(&s1, &r1); + + // Assigns a new value to ref_wrapper. + ref_wrapper = ByRef(s2); + const std::string& r2 = ref_wrapper; + EXPECT_EQ(&s2, &r2); + + ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper1 = ByRef(s1); + // Copies ref_wrapper1 to ref_wrapper. + ref_wrapper = ref_wrapper1; + const std::string& r3 = ref_wrapper; + EXPECT_EQ(&s1, &r3); +} + +// Tests using ByRef() on a const value. +TEST(ByRefTest, ConstValue) { + const int n = 0; + // int& ref = ByRef(n); // This shouldn't compile - we have a + // negative compilation test to catch it. + const int& const_ref = ByRef(n); + EXPECT_EQ(&n, &const_ref); +} + +// Tests using ByRef() on a non-const value. +TEST(ByRefTest, NonConstValue) { + int n = 0; + + // ByRef(n) can be used as either an int&, + int& ref = ByRef(n); + EXPECT_EQ(&n, &ref); + + // or a const int&. + const int& const_ref = ByRef(n); + EXPECT_EQ(&n, &const_ref); +} + +// Tests explicitly specifying the type when using ByRef(). +TEST(ByRefTest, ExplicitType) { + int n = 0; + const int& r1 = ByRef<const int>(n); + EXPECT_EQ(&n, &r1); + + // ByRef<char>(n); // This shouldn't compile - we have a negative + // compilation test to catch it. + + Derived d; + Derived& r2 = ByRef<Derived>(d); + EXPECT_EQ(&d, &r2); + + const Derived& r3 = ByRef<const Derived>(d); + EXPECT_EQ(&d, &r3); + + Base& r4 = ByRef<Base>(d); + EXPECT_EQ(&d, &r4); + + const Base& r5 = ByRef<const Base>(d); + EXPECT_EQ(&d, &r5); + + // The following shouldn't compile - we have a negative compilation + // test for it. + // + // Base b; + // ByRef<Derived>(b); +} + +// Tests that Google Mock prints expression ByRef(x) as a reference to x. +TEST(ByRefTest, PrintsCorrectly) { + int n = 42; + ::std::stringstream expected, actual; + testing::internal::UniversalPrinter<const int&>::Print(n, &expected); + testing::internal::UniversalPrint(ByRef(n), &actual); + EXPECT_EQ(expected.str(), actual.str()); +} } // Unnamed namespace diff --git a/testing/gmock/test/gmock-generated-actions_test.cc b/testing/gmock/test/gmock-generated-actions_test.cc index d0b2ddc..2e6fa0b 100644 --- a/testing/gmock/test/gmock-generated-actions_test.cc +++ b/testing/gmock/test/gmock-generated-actions_test.cc @@ -36,6 +36,7 @@ #include <gmock/gmock-generated-actions.h> #include <functional> +#include <sstream> #include <string> #include <gmock/gmock.h> #include <gtest/gtest.h> @@ -53,22 +54,16 @@ using testing::_; using testing::Action; using testing::ActionInterface; using testing::ByRef; -using testing::DeleteArg; using testing::DoAll; using testing::Invoke; -using testing::InvokeArgument; using testing::Return; using testing::ReturnNew; -using testing::SaveArg; -using testing::SetArgReferee; using testing::SetArgumentPointee; using testing::StaticAssertTypeEq; using testing::Unused; -using testing::WithArg; using testing::WithArgs; -using testing::WithoutArgs; -// Sample functions and functors for testing Invoke() and etc. +// Sample functions and functors for testing various actions. int Nullary() { return 1; } class NullaryFunctor { @@ -77,19 +72,11 @@ class NullaryFunctor { }; bool g_done = false; -void VoidNullary() { g_done = true; } - -class VoidNullaryFunctor { - public: - void operator()() { g_done = true; } -}; bool Unary(int x) { return x < 0; } const char* Plus1(const char* s) { return s + 1; } -void VoidUnary(int n) { g_done = true; } - bool ByConstRef(const string& s) { return s == "Hi"; } const double g_double = 0; @@ -111,10 +98,6 @@ void VoidTernary(int, char, bool) { g_done = true; } int SumOf4(int a, int b, int c, int d) { return a + b + c + d; } -int SumOfFirst2(int a, int b, Unused, Unused) { return a + b; } - -void VoidFunctionWithFourArguments(char, int, float, double) { g_done = true; } - string Concat4(const char* s1, const char* s2, const char* s3, const char* s4) { return string(s1) + s2 + s3 + s4; @@ -173,379 +156,10 @@ string Concat10(const char* s1, const char* s2, const char* s3, return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10; } -class Foo { - public: - Foo() : value_(123) {} - - int Nullary() const { return value_; } - - short Unary(long x) { return static_cast<short>(value_ + x); } // NOLINT - - string Binary(const string& str, char c) const { return str + c; } - - int Ternary(int x, bool y, char z) { return value_ + x + y*z; } - - int SumOf4(int a, int b, int c, int d) const { - return a + b + c + d + value_; - } - - int SumOfLast2(Unused, Unused, int a, int b) const { return a + b; } - - int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; } - - int SumOf6(int a, int b, int c, int d, int e, int f) { - return a + b + c + d + e + f; - } - - string Concat7(const char* s1, const char* s2, const char* s3, - const char* s4, const char* s5, const char* s6, - const char* s7) { - return string(s1) + s2 + s3 + s4 + s5 + s6 + s7; - } - - string Concat8(const char* s1, const char* s2, const char* s3, - const char* s4, const char* s5, const char* s6, - const char* s7, const char* s8) { - return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8; - } - - string Concat9(const char* s1, const char* s2, const char* s3, - const char* s4, const char* s5, const char* s6, - const char* s7, const char* s8, const char* s9) { - return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9; - } - - string Concat10(const char* s1, const char* s2, const char* s3, - const char* s4, const char* s5, const char* s6, - const char* s7, const char* s8, const char* s9, - const char* s10) { - return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10; - } - private: - int value_; -}; - -// Tests using Invoke() with a nullary function. -TEST(InvokeTest, Nullary) { - Action<int()> a = Invoke(Nullary); // NOLINT - EXPECT_EQ(1, a.Perform(make_tuple())); -} - -// Tests using Invoke() with a unary function. -TEST(InvokeTest, Unary) { - Action<bool(int)> a = Invoke(Unary); // NOLINT - EXPECT_FALSE(a.Perform(make_tuple(1))); - EXPECT_TRUE(a.Perform(make_tuple(-1))); -} - -// Tests using Invoke() with a binary function. -TEST(InvokeTest, Binary) { - Action<const char*(const char*, short)> a = Invoke(Binary); // NOLINT - const char* p = "Hello"; - EXPECT_EQ(p + 2, a.Perform(make_tuple(p, 2))); -} - -// Tests using Invoke() with a ternary function. -TEST(InvokeTest, Ternary) { - Action<int(int, char, short)> a = Invoke(Ternary); // NOLINT - EXPECT_EQ(6, a.Perform(make_tuple(1, '\2', 3))); -} - -// Tests using Invoke() with a 4-argument function. -TEST(InvokeTest, FunctionThatTakes4Arguments) { - Action<int(int, int, int, int)> a = Invoke(SumOf4); // NOLINT - EXPECT_EQ(1234, a.Perform(make_tuple(1000, 200, 30, 4))); -} - -// Tests using Invoke() with a 5-argument function. -TEST(InvokeTest, FunctionThatTakes5Arguments) { - Action<int(int, int, int, int, int)> a = Invoke(SumOf5); // NOLINT - EXPECT_EQ(12345, a.Perform(make_tuple(10000, 2000, 300, 40, 5))); -} - -// Tests using Invoke() with a 6-argument function. -TEST(InvokeTest, FunctionThatTakes6Arguments) { - Action<int(int, int, int, int, int, int)> a = Invoke(SumOf6); // NOLINT - EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6))); -} - // A helper that turns the type of a C-string literal from const // char[N] to const char*. inline const char* CharPtr(const char* s) { return s; } -// Tests using Invoke() with a 7-argument function. -TEST(InvokeTest, FunctionThatTakes7Arguments) { - Action<string(const char*, const char*, const char*, const char*, - const char*, const char*, const char*)> a = - Invoke(Concat7); - EXPECT_EQ("1234567", - a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), - CharPtr("4"), CharPtr("5"), CharPtr("6"), - CharPtr("7")))); -} - -// Tests using Invoke() with a 8-argument function. -TEST(InvokeTest, FunctionThatTakes8Arguments) { - Action<string(const char*, const char*, const char*, const char*, - const char*, const char*, const char*, const char*)> a = - Invoke(Concat8); - EXPECT_EQ("12345678", - a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), - CharPtr("4"), CharPtr("5"), CharPtr("6"), - CharPtr("7"), CharPtr("8")))); -} - -// Tests using Invoke() with a 9-argument function. -TEST(InvokeTest, FunctionThatTakes9Arguments) { - Action<string(const char*, const char*, const char*, const char*, - const char*, const char*, const char*, const char*, - const char*)> a = Invoke(Concat9); - EXPECT_EQ("123456789", - a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), - CharPtr("4"), CharPtr("5"), CharPtr("6"), - CharPtr("7"), CharPtr("8"), CharPtr("9")))); -} - -// Tests using Invoke() with a 10-argument function. -TEST(InvokeTest, FunctionThatTakes10Arguments) { - Action<string(const char*, const char*, const char*, const char*, - const char*, const char*, const char*, const char*, - const char*, const char*)> a = Invoke(Concat10); - EXPECT_EQ("1234567890", - a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), - CharPtr("4"), CharPtr("5"), CharPtr("6"), - CharPtr("7"), CharPtr("8"), CharPtr("9"), - CharPtr("0")))); -} - -// Tests using Invoke() with functions with parameters declared as Unused. -TEST(InvokeTest, FunctionWithUnusedParameters) { - Action<int(int, int, double, const string&)> a1 = - Invoke(SumOfFirst2); - EXPECT_EQ(12, a1.Perform(make_tuple(10, 2, 5.6, CharPtr("hi")))); - - Action<int(int, int, bool, int*)> a2 = - Invoke(SumOfFirst2); - EXPECT_EQ(23, a2.Perform(make_tuple(20, 3, true, static_cast<int*>(NULL)))); -} - -// Tests using Invoke() with methods with parameters declared as Unused. -TEST(InvokeTest, MethodWithUnusedParameters) { - Foo foo; - Action<int(string, bool, int, int)> a1 = - Invoke(&foo, &Foo::SumOfLast2); - EXPECT_EQ(12, a1.Perform(make_tuple(CharPtr("hi"), true, 10, 2))); - - Action<int(char, double, int, int)> a2 = - Invoke(&foo, &Foo::SumOfLast2); - EXPECT_EQ(23, a2.Perform(make_tuple('a', 2.5, 20, 3))); -} - -// Tests using Invoke() with a functor. -TEST(InvokeTest, Functor) { - Action<int(short, char)> a = Invoke(plus<short>()); // NOLINT - EXPECT_EQ(3, a.Perform(make_tuple(1, 2))); -} - -// Tests using Invoke(f) as an action of a compatible type. -TEST(InvokeTest, FunctionWithCompatibleType) { - Action<long(int, short, char, bool)> a = Invoke(SumOf4); // NOLINT - EXPECT_EQ(4321, a.Perform(make_tuple(4000, 300, 20, true))); -} - -// Tests using Invoke() with an object pointer and a method pointer. - -// Tests using Invoke() with a nullary method. -TEST(InvokeMethodTest, Nullary) { - Foo foo; - Action<int()> a = Invoke(&foo, &Foo::Nullary); // NOLINT - EXPECT_EQ(123, a.Perform(make_tuple())); -} - -// Tests using Invoke() with a unary method. -TEST(InvokeMethodTest, Unary) { - Foo foo; - Action<short(long)> a = Invoke(&foo, &Foo::Unary); // NOLINT - EXPECT_EQ(4123, a.Perform(make_tuple(4000))); -} - -// Tests using Invoke() with a binary method. -TEST(InvokeMethodTest, Binary) { - Foo foo; - Action<string(const string&, char)> a = Invoke(&foo, &Foo::Binary); - string s("Hell"); - EXPECT_EQ("Hello", a.Perform(make_tuple(s, 'o'))); -} - -// Tests using Invoke() with a ternary method. -TEST(InvokeMethodTest, Ternary) { - Foo foo; - Action<int(int, bool, char)> a = Invoke(&foo, &Foo::Ternary); // NOLINT - EXPECT_EQ(1124, a.Perform(make_tuple(1000, true, 1))); -} - -// Tests using Invoke() with a 4-argument method. -TEST(InvokeMethodTest, MethodThatTakes4Arguments) { - Foo foo; - Action<int(int, int, int, int)> a = Invoke(&foo, &Foo::SumOf4); // NOLINT - EXPECT_EQ(1357, a.Perform(make_tuple(1000, 200, 30, 4))); -} - -// Tests using Invoke() with a 5-argument method. -TEST(InvokeMethodTest, MethodThatTakes5Arguments) { - Foo foo; - Action<int(int, int, int, int, int)> a = Invoke(&foo, &Foo::SumOf5); // NOLINT - EXPECT_EQ(12345, a.Perform(make_tuple(10000, 2000, 300, 40, 5))); -} - -// Tests using Invoke() with a 6-argument method. -TEST(InvokeMethodTest, MethodThatTakes6Arguments) { - Foo foo; - Action<int(int, int, int, int, int, int)> a = // NOLINT - Invoke(&foo, &Foo::SumOf6); - EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6))); -} - -// Tests using Invoke() with a 7-argument method. -TEST(InvokeMethodTest, MethodThatTakes7Arguments) { - Foo foo; - Action<string(const char*, const char*, const char*, const char*, - const char*, const char*, const char*)> a = - Invoke(&foo, &Foo::Concat7); - EXPECT_EQ("1234567", - a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), - CharPtr("4"), CharPtr("5"), CharPtr("6"), - CharPtr("7")))); -} - -// Tests using Invoke() with a 8-argument method. -TEST(InvokeMethodTest, MethodThatTakes8Arguments) { - Foo foo; - Action<string(const char*, const char*, const char*, const char*, - const char*, const char*, const char*, const char*)> a = - Invoke(&foo, &Foo::Concat8); - EXPECT_EQ("12345678", - a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), - CharPtr("4"), CharPtr("5"), CharPtr("6"), - CharPtr("7"), CharPtr("8")))); -} - -// Tests using Invoke() with a 9-argument method. -TEST(InvokeMethodTest, MethodThatTakes9Arguments) { - Foo foo; - Action<string(const char*, const char*, const char*, const char*, - const char*, const char*, const char*, const char*, - const char*)> a = Invoke(&foo, &Foo::Concat9); - EXPECT_EQ("123456789", - a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), - CharPtr("4"), CharPtr("5"), CharPtr("6"), - CharPtr("7"), CharPtr("8"), CharPtr("9")))); -} - -// Tests using Invoke() with a 10-argument method. -TEST(InvokeMethodTest, MethodThatTakes10Arguments) { - Foo foo; - Action<string(const char*, const char*, const char*, const char*, - const char*, const char*, const char*, const char*, - const char*, const char*)> a = Invoke(&foo, &Foo::Concat10); - EXPECT_EQ("1234567890", - a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), - CharPtr("4"), CharPtr("5"), CharPtr("6"), - CharPtr("7"), CharPtr("8"), CharPtr("9"), - CharPtr("0")))); -} - -// Tests using Invoke(f) as an action of a compatible type. -TEST(InvokeMethodTest, MethodWithCompatibleType) { - Foo foo; - Action<long(int, short, char, bool)> a = // NOLINT - Invoke(&foo, &Foo::SumOf4); - EXPECT_EQ(4444, a.Perform(make_tuple(4000, 300, 20, true))); -} - -// Tests ByRef(). - -// Tests that ReferenceWrapper<T> is copyable. -TEST(ByRefTest, IsCopyable) { - const string s1 = "Hi"; - const string s2 = "Hello"; - - ::testing::internal::ReferenceWrapper<const string> ref_wrapper = ByRef(s1); - const string& r1 = ref_wrapper; - EXPECT_EQ(&s1, &r1); - - // Assigns a new value to ref_wrapper. - ref_wrapper = ByRef(s2); - const string& r2 = ref_wrapper; - EXPECT_EQ(&s2, &r2); - - ::testing::internal::ReferenceWrapper<const string> ref_wrapper1 = ByRef(s1); - // Copies ref_wrapper1 to ref_wrapper. - ref_wrapper = ref_wrapper1; - const string& r3 = ref_wrapper; - EXPECT_EQ(&s1, &r3); -} - -// Tests using ByRef() on a const value. -TEST(ByRefTest, ConstValue) { - const int n = 0; - // int& ref = ByRef(n); // This shouldn't compile - we have a - // negative compilation test to catch it. - const int& const_ref = ByRef(n); - EXPECT_EQ(&n, &const_ref); -} - -// Tests using ByRef() on a non-const value. -TEST(ByRefTest, NonConstValue) { - int n = 0; - - // ByRef(n) can be used as either an int&, - int& ref = ByRef(n); - EXPECT_EQ(&n, &ref); - - // or a const int&. - const int& const_ref = ByRef(n); - EXPECT_EQ(&n, &const_ref); -} - -struct Base { - bool operator==(const Base&) { return true; } -}; - -struct Derived : public Base { - bool operator==(const Derived&) { return true; } -}; - -// Tests explicitly specifying the type when using ByRef(). -TEST(ByRefTest, ExplicitType) { - int n = 0; - const int& r1 = ByRef<const int>(n); - EXPECT_EQ(&n, &r1); - - // ByRef<char>(n); // This shouldn't compile - we have a negative - // compilation test to catch it. - - - Derived d; - Derived& r2 = ByRef<Derived>(d); - EXPECT_EQ(&d, &r2); - - const Derived& r3 = ByRef<const Derived>(d); - EXPECT_EQ(&d, &r3); - - Base& r4 = ByRef<Base>(d); - EXPECT_EQ(&d, &r4); - - const Base& r5 = ByRef<const Base>(d); - EXPECT_EQ(&d, &r5); - - // The following shouldn't compile - we have a negative compilation - // test for it. - // - // Base b; - // ByRef<Derived>(b); -} - // Tests InvokeArgument<N>(...). // Tests using InvokeArgument with a nullary function. @@ -663,23 +277,11 @@ TEST(InvokeArgumentTest, ByExplicitConstReferenceFunction) { EXPECT_FALSE(a.Perform(make_tuple(&ReferencesGlobalDouble))); } -// Tests using WithoutArgs with an action that takes no argument. -TEST(WithoutArgsTest, NoArg) { - Action<int(int n)> a = WithoutArgs(Invoke(Nullary)); // NOLINT - EXPECT_EQ(1, a.Perform(make_tuple(2))); -} - -// Tests using WithArgs and WithArg with an action that takes 1 argument. +// Tests using WithArgs and with an action that takes 1 argument. TEST(WithArgsTest, OneArg) { Action<bool(double x, int n)> a = WithArgs<1>(Invoke(Unary)); // NOLINT EXPECT_TRUE(a.Perform(make_tuple(1.5, -1))); EXPECT_FALSE(a.Perform(make_tuple(1.5, 1))); - - // Also tests the synonym WithArg. - Action<bool(double x, int n)> b = WithArg<1>(Invoke(Unary)); // NOLINT - EXPECT_TRUE(a.Perform(make_tuple(1.5, -1))); - EXPECT_FALSE(a.Perform(make_tuple(1.5, 1))); - } // Tests using WithArgs with an action that takes 2 arguments. @@ -1372,41 +974,6 @@ TEST(ActionPnMacroTest, CanExplicitlyInstantiateWithReferenceTypes) { EXPECT_EQ(55, a.Perform(empty)); } -TEST(SaveArgActionTest, WorksForSameType) { - int result = 0; - const Action<void(int n)> a1 = SaveArg<0>(&result); - a1.Perform(make_tuple(5)); - EXPECT_EQ(5, result); -} - -TEST(SaveArgActionTest, WorksForCompatibleType) { - int result = 0; - const Action<void(bool, char)> a1 = SaveArg<1>(&result); - a1.Perform(make_tuple(true, 'a')); - EXPECT_EQ('a', result); -} - -TEST(SetArgRefereeActionTest, WorksForSameType) { - int value = 0; - const Action<void(int&)> a1 = SetArgReferee<0>(1); - a1.Perform(tuple<int&>(value)); - EXPECT_EQ(1, value); -} - -TEST(SetArgRefereeActionTest, WorksForCompatibleType) { - int value = 0; - const Action<void(int, int&)> a1 = SetArgReferee<1>('a'); - a1.Perform(tuple<int, int&>(0, value)); - EXPECT_EQ('a', value); -} - -TEST(SetArgRefereeActionTest, WorksWithExtraArguments) { - int value = 0; - const Action<void(bool, int, int&, const char*)> a1 = SetArgReferee<2>('a'); - a1.Perform(tuple<bool, int, int&, const char*>(true, 0, value, "hi")); - EXPECT_EQ('a', value); -} - class NullaryConstructorClass { public: NullaryConstructorClass() : value_(123) {} @@ -1471,64 +1038,6 @@ TEST(ReturnNewTest, ConstructorThatTakes10Arguments) { delete c; } -// A class that can be used to verify that its destructor is called: it will set -// the bool provided to the constructor to true when destroyed. -class DeletionTester { - public: - explicit DeletionTester(bool* is_deleted) - : is_deleted_(is_deleted) { - // Make sure the bit is set to false. - *is_deleted_ = false; - } - - ~DeletionTester() { - *is_deleted_ = true; - } - - private: - bool* is_deleted_; -}; - -TEST(DeleteArgActionTest, OneArg) { - bool is_deleted = false; - DeletionTester* t = new DeletionTester(&is_deleted); - const Action<void(DeletionTester*)> a1 = DeleteArg<0>(); // NOLINT - EXPECT_FALSE(is_deleted); - a1.Perform(make_tuple(t)); - EXPECT_TRUE(is_deleted); -} - -TEST(DeleteArgActionTest, TenArgs) { - bool is_deleted = false; - DeletionTester* t = new DeletionTester(&is_deleted); - const Action<void(bool, int, int, const char*, bool, - int, int, int, int, DeletionTester*)> a1 = DeleteArg<9>(); - EXPECT_FALSE(is_deleted); - a1.Perform(make_tuple(true, 5, 6, CharPtr("hi"), false, 7, 8, 9, 10, t)); - EXPECT_TRUE(is_deleted); -} - -#if GTEST_HAS_EXCEPTIONS - -TEST(ThrowActionTest, ThrowsGivenExceptionInVoidFunction) { - const Action<void(int n)> a = Throw('a'); - EXPECT_THROW(a.Perform(make_tuple(0)), char); -} - -class MyException {}; - -TEST(ThrowActionTest, ThrowsGivenExceptionInNonVoidFunction) { - const Action<double(char ch)> a = Throw(MyException()); - EXPECT_THROW(a.Perform(make_tuple('0')), MyException); -} - -TEST(ThrowActionTest, ThrowsGivenExceptionInNullaryFunction) { - const Action<double()> a = Throw(MyException()); - EXPECT_THROW(a.Perform(make_tuple()), MyException); -} - -#endif // GTEST_HAS_EXCEPTIONS - // Tests that ACTION_TEMPLATE works when there is no value parameter. ACTION_TEMPLATE(CreateNew, HAS_1_TEMPLATE_PARAMS(typename, T), diff --git a/testing/gmock/test/gmock-generated-function-mockers_test.cc b/testing/gmock/test/gmock-generated-function-mockers_test.cc index 7267c10..1ce8c45 100644 --- a/testing/gmock/test/gmock-generated-function-mockers_test.cc +++ b/testing/gmock/test/gmock-generated-function-mockers_test.cc @@ -66,6 +66,7 @@ using testing::Const; using testing::DoDefault; using testing::Eq; using testing::Lt; +using testing::MockFunction; using testing::Ref; using testing::Return; using testing::ReturnRef; @@ -462,5 +463,48 @@ TEST(OverloadedMockMethodTest, CanOverloadOnConstnessInMacroBody) { EXPECT_EQ(3, const_mock->Overloaded(1)); } +TEST(MockFunctionTest, WorksForVoidNullary) { + MockFunction<void()> foo; + EXPECT_CALL(foo, Call()); + foo.Call(); +} + +TEST(MockFunctionTest, WorksForNonVoidNullary) { + MockFunction<int()> foo; + EXPECT_CALL(foo, Call()) + .WillOnce(Return(1)) + .WillOnce(Return(2)); + EXPECT_EQ(1, foo.Call()); + EXPECT_EQ(2, foo.Call()); +} + +TEST(MockFunctionTest, WorksForVoidUnary) { + MockFunction<void(int)> foo; + EXPECT_CALL(foo, Call(1)); + foo.Call(1); +} + +TEST(MockFunctionTest, WorksForNonVoidBinary) { + MockFunction<int(bool, int)> foo; + EXPECT_CALL(foo, Call(false, 42)) + .WillOnce(Return(1)) + .WillOnce(Return(2)); + EXPECT_CALL(foo, Call(true, Ge(100))) + .WillOnce(Return(3)); + EXPECT_EQ(1, foo.Call(false, 42)); + EXPECT_EQ(2, foo.Call(false, 42)); + EXPECT_EQ(3, foo.Call(true, 120)); +} + +TEST(MockFunctionTest, WorksFor10Arguments) { + MockFunction<int(bool a0, char a1, int a2, int a3, int a4, + int a5, int a6, char a7, int a8, bool a9)> foo; + EXPECT_CALL(foo, Call(_, 'a', _, _, _, _, _, _, _, _)) + .WillOnce(Return(1)) + .WillOnce(Return(2)); + EXPECT_EQ(1, foo.Call(false, 'a', 0, 0, 0, 0, 0, 'b', 0, true)); + EXPECT_EQ(2, foo.Call(true, 'a', 0, 0, 0, 0, 0, 'b', 1, false)); +} + } // namespace gmock_generated_function_mockers_test } // namespace testing diff --git a/testing/gmock/test/gmock-internal-utils_test.cc b/testing/gmock/test/gmock-internal-utils_test.cc index 9ab15af..ac3b2dd 100644 --- a/testing/gmock/test/gmock-internal-utils_test.cc +++ b/testing/gmock/test/gmock-internal-utils_test.cc @@ -48,6 +48,12 @@ #include <sys/types.h> // For ssize_t. NOLINT #endif +class ProtocolMessage; + +namespace proto2 { +class Message; +} // namespace proto2 + namespace testing { namespace internal { @@ -205,7 +211,8 @@ TEST(GetRawPointerTest, WorksForSmartPointers) { TEST(GetRawPointerTest, WorksForRawPointers) { int* p = NULL; - EXPECT_EQ(NULL, GetRawPointer(p)); + // Don't use EXPECT_EQ as no NULL-testing magic on Symbian. + EXPECT_TRUE(NULL == GetRawPointer(p)); int n = 1; EXPECT_EQ(&n, GetRawPointer(&n)); } @@ -384,6 +391,7 @@ TEST(IsAProtocolMessageTest, ValueIsCompileTimeConstant) { // Tests that IsAProtocolMessage<T>::value is true when T is // ProtocolMessage or a sub-class of it. TEST(IsAProtocolMessageTest, ValueIsTrueWhenTypeIsAProtocolMessage) { + EXPECT_TRUE(IsAProtocolMessage< ::proto2::Message>::value); EXPECT_TRUE(IsAProtocolMessage<ProtocolMessage>::value); #if GMOCK_HAS_PROTOBUF_ EXPECT_TRUE(IsAProtocolMessage<const TestMessage>::value); @@ -465,21 +473,17 @@ TEST(AssertTest, SucceedsOnTrue) { Assert(true, __FILE__, __LINE__); // This should succeed too. } -#if GTEST_HAS_DEATH_TEST - // Tests that Assert(false, ...) generates a fatal failure. TEST(AssertTest, FailsFatallyOnFalse) { - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ Assert(false, __FILE__, __LINE__, "This should fail."); }, ""); - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ Assert(false, __FILE__, __LINE__); }, ""); } -#endif // GTEST_HAS_DEATH_TEST - // Tests that Expect(true, ...) succeeds. TEST(ExpectTest, SucceedsOnTrue) { Expect(true, __FILE__, __LINE__, "This should succeed."); @@ -501,7 +505,16 @@ TEST(ExpectTest, FailsNonfatallyOnFalse) { class LogIsVisibleTest : public ::testing::Test { protected: - virtual void SetUp() { original_verbose_ = GMOCK_FLAG(verbose); } + virtual void SetUp() { + // The code needs to work when both ::string and ::std::string are + // defined and the flag is implemented as a + // testing::internal::String. In this case, without the call to + // c_str(), the compiler will complain that it cannot figure out + // whether the String flag should be converted to a ::string or an + // ::std::string before being assigned to original_verbose_. + original_verbose_ = GMOCK_FLAG(verbose).c_str(); + } + virtual void TearDown() { GMOCK_FLAG(verbose) = original_verbose_; } string original_verbose_; @@ -803,34 +816,19 @@ TEST(CopyArrayTest, WorksForTwoDimensionalArrays) { // Tests NativeArray. -TEST(NativeArrayTest, ConstructorFromArrayReferenceWorks) { +TEST(NativeArrayTest, ConstructorFromArrayWorks) { const int a[3] = { 0, 1, 2 }; - NativeArray<int> na(a, kReference); + NativeArray<int> na(a, 3, kReference); EXPECT_EQ(3, na.size()); EXPECT_EQ(a, na.begin()); } -TEST(NativeArrayTest, ConstructorFromTupleWorks) { - int a[3] = { 0, 1, 2 }; - int* const p = a; - // Tests with a plain pointer. - NativeArray<int> na(make_tuple(p, 3U), kReference); - EXPECT_EQ(a, na.begin()); - - const linked_ptr<char> b(new char); - *b = 'a'; - // Tests with a smart pointer. - NativeArray<char> nb(make_tuple(b, 1), kCopy); - EXPECT_NE(b.get(), nb.begin()); - EXPECT_EQ('a', nb.begin()[0]); -} - TEST(NativeArrayTest, CreatesAndDeletesCopyOfArrayWhenAskedTo) { typedef int Array[2]; Array* a = new Array[1]; (*a)[0] = 0; (*a)[1] = 1; - NativeArray<int> na(*a, kCopy); + NativeArray<int> na(*a, 2, kCopy); EXPECT_NE(*a, na.begin()); delete[] a; EXPECT_EQ(0, na.begin()[0]); @@ -849,8 +847,8 @@ TEST(NativeArrayTest, TypeMembersAreCorrect) { } TEST(NativeArrayTest, MethodsWork) { - const int a[] = { 0, 1, 2 }; - NativeArray<int> na(a, kCopy); + const int a[3] = { 0, 1, 2 }; + NativeArray<int> na(a, 3, kCopy); ASSERT_EQ(3, na.size()); EXPECT_EQ(3, na.end() - na.begin()); @@ -865,18 +863,18 @@ TEST(NativeArrayTest, MethodsWork) { EXPECT_THAT(na, Eq(na)); - NativeArray<int> na2(a, kReference); + NativeArray<int> na2(a, 3, kReference); EXPECT_THAT(na, Eq(na2)); - const int b1[] = { 0, 1, 1 }; - const int b2[] = { 0, 1, 2, 3 }; - EXPECT_THAT(na, Not(Eq(NativeArray<int>(b1, kReference)))); - EXPECT_THAT(na, Not(Eq(NativeArray<int>(b2, kCopy)))); + const int b1[3] = { 0, 1, 1 }; + const int b2[4] = { 0, 1, 2, 3 }; + EXPECT_THAT(na, Not(Eq(NativeArray<int>(b1, 3, kReference)))); + EXPECT_THAT(na, Not(Eq(NativeArray<int>(b2, 4, kCopy)))); } TEST(NativeArrayTest, WorksForTwoDimensionalArray) { const char a[2][3] = { "hi", "lo" }; - NativeArray<char[3]> na(a, kReference); + NativeArray<char[3]> na(a, 2, kReference); ASSERT_EQ(2, na.size()); EXPECT_EQ(a, na.begin()); } diff --git a/testing/gmock/test/gmock-matchers_test.cc b/testing/gmock/test/gmock-matchers_test.cc index 3541eef..20b9387 100644 --- a/testing/gmock/test/gmock-matchers_test.cc +++ b/testing/gmock/test/gmock-matchers_test.cc @@ -42,6 +42,7 @@ #include <set> #include <sstream> #include <string> +#include <utility> #include <vector> #include <gmock/gmock.h> #include <gtest/gtest.h> @@ -59,6 +60,8 @@ bool SkipPrefix(const char* prefix, const char** pstr); namespace gmock_matchers_test { +using std::map; +using std::multimap; using std::stringstream; using std::tr1::make_tuple; using testing::A; @@ -75,6 +78,8 @@ using testing::FloatEq; using testing::Ge; using testing::Gt; using testing::HasSubstr; +using testing::IsNull; +using testing::Key; using testing::Le; using testing::Lt; using testing::MakeMatcher; @@ -88,6 +93,7 @@ using testing::NanSensitiveFloatEq; using testing::Ne; using testing::Not; using testing::NotNull; +using testing::Pair; using testing::Pointee; using testing::PolymorphicMatcher; using testing::Property; @@ -123,6 +129,35 @@ using testing::MatchesRegex; using testing::internal::RE; #endif // GMOCK_HAS_REGEX +// For testing ExplainMatchResultTo(). +class GreaterThanMatcher : public MatcherInterface<int> { + public: + explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {} + + virtual bool Matches(int lhs) const { return lhs > rhs_; } + + virtual void DescribeTo(::std::ostream* os) const { + *os << "is greater than " << rhs_; + } + + virtual void ExplainMatchResultTo(int lhs, ::std::ostream* os) const { + const int diff = lhs - rhs_; + if (diff > 0) { + *os << "is " << diff << " more than " << rhs_; + } else if (diff == 0) { + *os << "is the same as " << rhs_; + } else { + *os << "is " << -diff << " less than " << rhs_; + } + } + private: + const int rhs_; +}; + +Matcher<int> GreaterThan(int n) { + return MakeMatcher(new GreaterThanMatcher(n)); +} + // Returns the description of the given matcher. template <typename T> string Describe(const Matcher<T>& m) { @@ -651,6 +686,42 @@ TEST(NeTest, CanDescribeSelf) { EXPECT_EQ("is not equal to 5", Describe(m)); } +// Tests that IsNull() matches any NULL pointer of any type. +TEST(IsNullTest, MatchesNullPointer) { + Matcher<int*> m1 = IsNull(); + int* p1 = NULL; + int n = 0; + EXPECT_TRUE(m1.Matches(p1)); + EXPECT_FALSE(m1.Matches(&n)); + + Matcher<const char*> m2 = IsNull(); + const char* p2 = NULL; + 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 = NULL; + EXPECT_TRUE(m3.Matches(p3)); + EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef))); +#endif +} + +// Tests that IsNull() describes itself properly. +TEST(IsNullTest, CanDescribeSelf) { + Matcher<int*> m = IsNull(); + EXPECT_EQ("is NULL", Describe(m)); + EXPECT_EQ("is not NULL", DescribeNegation(m)); +} + // Tests that NotNull() matches any non-NULL pointer of any type. TEST(NotNullTest, MatchesNonNullPointer) { Matcher<int*> m1 = NotNull(); @@ -850,6 +921,136 @@ TEST(HasSubstrTest, CanDescribeSelf) { EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m)); } +TEST(KeyTest, CanDescribeSelf) { + Matcher<const std::pair<std::string, int>&> m = Key("foo"); + EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m)); +} + +TEST(KeyTest, MatchesCorrectly) { + std::pair<int, std::string> p(25, "foo"); + EXPECT_THAT(p, Key(25)); + EXPECT_THAT(p, Not(Key(42))); + EXPECT_THAT(p, Key(Ge(20))); + EXPECT_THAT(p, Not(Key(Lt(25)))); +} + +TEST(KeyTest, SafelyCastsInnerMatcher) { + Matcher<int> is_positive = Gt(0); + Matcher<int> is_negative = Lt(0); + std::pair<char, bool> p('a', true); + EXPECT_THAT(p, Key(is_positive)); + EXPECT_THAT(p, Not(Key(is_negative))); +} + +TEST(KeyTest, InsideContainsUsingMap) { + std::map<int, char> container; + container.insert(std::make_pair(1, 'a')); + container.insert(std::make_pair(2, 'b')); + container.insert(std::make_pair(4, 'c')); + EXPECT_THAT(container, Contains(Key(1))); + EXPECT_THAT(container, Not(Contains(Key(3)))); +} + +TEST(KeyTest, InsideContainsUsingMultimap) { + std::multimap<int, char> container; + container.insert(std::make_pair(1, 'a')); + container.insert(std::make_pair(2, 'b')); + container.insert(std::make_pair(4, 'c')); + + EXPECT_THAT(container, Not(Contains(Key(25)))); + container.insert(std::make_pair(25, 'd')); + EXPECT_THAT(container, Contains(Key(25))); + container.insert(std::make_pair(25, 'e')); + EXPECT_THAT(container, Contains(Key(25))); + + EXPECT_THAT(container, Contains(Key(1))); + EXPECT_THAT(container, Not(Contains(Key(3)))); +} + +TEST(PairTest, Typing) { + // Test verifies the following type conversions can be compiled. + Matcher<const std::pair<const char*, int>&> m1 = Pair("foo", 42); + Matcher<const std::pair<const char*, int> > m2 = Pair("foo", 42); + Matcher<std::pair<const char*, int> > m3 = Pair("foo", 42); + + Matcher<std::pair<int, const std::string> > m4 = Pair(25, "42"); + Matcher<std::pair<const std::string, int> > m5 = Pair("25", 42); +} + +TEST(PairTest, CanDescribeSelf) { + Matcher<const std::pair<std::string, int>&> m1 = Pair("foo", 42); + EXPECT_EQ("has a first field that is equal to \"foo\"" + ", and has a second field that is equal to 42", + Describe(m1)); + EXPECT_EQ("has a first field that is not equal to \"foo\"" + ", or has a second field that is not equal to 42", + DescribeNegation(m1)); + // Double and triple negation (1 or 2 times not and description of negation). + Matcher<const std::pair<int, int>&> m2 = Not(Pair(Not(13), 42)); + EXPECT_EQ("has a first field that is not equal to 13" + ", and has a second field that is equal to 42", + DescribeNegation(m2)); +} + +TEST(PairTest, CanExplainMatchResultTo) { + const Matcher<std::pair<int, int> > m0 = Pair(0, 0); + EXPECT_EQ("", Explain(m0, std::make_pair(25, 42))); + + const Matcher<std::pair<int, int> > m1 = Pair(GreaterThan(0), 0); + EXPECT_EQ("the first field is 25 more than 0", + Explain(m1, std::make_pair(25, 42))); + + const Matcher<std::pair<int, int> > m2 = Pair(0, GreaterThan(0)); + EXPECT_EQ("the second field is 42 more than 0", + Explain(m2, std::make_pair(25, 42))); + + const Matcher<std::pair<int, int> > m3 = Pair(GreaterThan(0), GreaterThan(0)); + EXPECT_EQ("the first field is 25 more than 0" + ", and the second field is 42 more than 0", + Explain(m3, std::make_pair(25, 42))); +} + +TEST(PairTest, MatchesCorrectly) { + std::pair<int, std::string> p(25, "foo"); + + // Both fields match. + EXPECT_THAT(p, Pair(25, "foo")); + EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o"))); + + // 'first' doesnt' match, but 'second' matches. + EXPECT_THAT(p, Not(Pair(42, "foo"))); + EXPECT_THAT(p, Not(Pair(Lt(25), "foo"))); + + // 'first' matches, but 'second' doesn't match. + EXPECT_THAT(p, Not(Pair(25, "bar"))); + EXPECT_THAT(p, Not(Pair(25, Not("foo")))); + + // Neither field matches. + EXPECT_THAT(p, Not(Pair(13, "bar"))); + EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a")))); +} + +TEST(PairTest, SafelyCastsInnerMatchers) { + Matcher<int> is_positive = Gt(0); + Matcher<int> is_negative = Lt(0); + std::pair<char, bool> p('a', true); + EXPECT_THAT(p, Pair(is_positive, _)); + EXPECT_THAT(p, Not(Pair(is_negative, _))); + EXPECT_THAT(p, Pair(_, is_positive)); + EXPECT_THAT(p, Not(Pair(_, is_negative))); +} + +TEST(PairTest, InsideContainsUsingMap) { + std::map<int, char> container; + container.insert(std::make_pair(1, 'a')); + container.insert(std::make_pair(2, 'b')); + container.insert(std::make_pair(4, 'c')); + EXPECT_THAT(container, Contains(Pair(1, 'a'))); + EXPECT_THAT(container, Contains(Pair(1, _))); + EXPECT_THAT(container, Contains(Pair(_, 'a'))); + EXPECT_THAT(container, Not(Contains(Pair(3, _)))); +} + // Tests StartsWith(s). TEST(StartsWithTest, MatchesStringWithGivenPrefix) { @@ -1735,17 +1936,23 @@ TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) { // which cannot reference auto variables. static int n; n = 5; - EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Gt(10)) << "This should fail.", + + // VC++ prior to version 8.0 SP1 has a bug where it will not see any + // functions declared in the namespace scope from within nested classes. + // EXPECT/ASSERT_(NON)FATAL_FAILURE macros use nested classes so that all + // namespace-level functions invoked inside them need to be explicitly + // resolved. + EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Gt(10)), "Value of: n\n" "Expected: is greater than 10\n" - " Actual: 5\n" - "This should fail."); + " Actual: 5"); n = 0; - EXPECT_NONFATAL_FAILURE(EXPECT_THAT(n, AllOf(Le(7), Ge(5))), - "Value of: n\n" - "Expected: (is less than or equal to 7) and " - "(is greater than or equal to 5)\n" - " Actual: 0"); + EXPECT_NONFATAL_FAILURE( + EXPECT_THAT(n, ::testing::AllOf(::testing::Le(7), ::testing::Ge(5))), + "Value of: n\n" + "Expected: (is less than or equal to 7) and " + "(is greater than or equal to 5)\n" + " Actual: 0"); } // Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument @@ -1756,16 +1963,28 @@ TEST(MatcherAssertionTest, WorksForByRefArguments) { static int n; n = 0; EXPECT_THAT(n, AllOf(Le(7), Ref(n))); - EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Not(Ref(n))), + EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))), "Value of: n\n" "Expected: does not reference the variable @"); // Tests the "Actual" part. - EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Not(Ref(n))), + EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))), "Actual: 0 (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 Matches(T x) const { +// return source_matcher_.Matches(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); @@ -1779,6 +1998,7 @@ TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) { "Expected: is greater than 5\n" " Actual: 5"); } +#endif // !GTEST_OS_SYMBIAN // Tests floating-point matchers. template <typename RawType> @@ -2095,35 +2315,6 @@ TEST(PointeeTest, CanDescribeSelf) { DescribeNegation(m)); } -// For testing ExplainMatchResultTo(). -class GreaterThanMatcher : public MatcherInterface<int> { - public: - explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {} - - virtual bool Matches(int lhs) const { return lhs > rhs_; } - - virtual void DescribeTo(::std::ostream* os) const { - *os << "is greater than " << rhs_; - } - - virtual void ExplainMatchResultTo(int lhs, ::std::ostream* os) const { - const int diff = lhs - rhs_; - if (diff > 0) { - *os << "is " << diff << " more than " << rhs_; - } else if (diff == 0) { - *os << "is the same as " << rhs_; - } else { - *os << "is " << -diff << " less than " << rhs_; - } - } - private: - const int rhs_; -}; - -Matcher<int> GreaterThan(int n) { - return MakeMatcher(new GreaterThanMatcher(n)); -} - TEST(PointeeTest, CanExplainMatchResult) { const Matcher<const string*> m = Pointee(StartsWith("Hi")); @@ -2623,15 +2814,13 @@ TEST(ResultOfTest, WorksForCompatibleMatcherTypes) { EXPECT_FALSE(matcher.Matches(42)); } -#if GTEST_HAS_DEATH_TEST // Tests that the program aborts when ResultOf is passed // a NULL function pointer. TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) { - EXPECT_DEATH( + EXPECT_DEATH_IF_SUPPORTED( ResultOf(static_cast<string(*)(int)>(NULL), Eq(string("foo"))), "NULL function pointer is passed into ResultOf\\(\\)\\."); } -#endif // GTEST_HAS_DEATH_TEST // Tests that ResultOf(f, ...) compiles and works as expected when f is a // function reference. @@ -2696,7 +2885,6 @@ TEST(ResultOfTest, WorksForReferencingCallables) { EXPECT_FALSE(matcher3.Matches(n2)); } - class DivisibleByImpl { public: explicit DivisibleByImpl(int divider) : divider_(divider) {} @@ -2714,9 +2902,11 @@ class DivisibleByImpl { *os << "is not divisible by " << divider_; } + void set_divider(int divider) { divider_ = divider; } int divider() const { return divider_; } + private: - const int divider_; + int divider_; }; // For testing using ExplainMatchResultTo() with polymorphic matchers. @@ -2810,6 +3000,7 @@ TEST(ByRefTest, AllowsNotCopyableValueInMatchers) { EXPECT_TRUE(m.Matches(n2)); } +#if GTEST_HAS_TYPED_TEST // Tests ContainerEq with different container types, and // different element types. @@ -2878,6 +3069,7 @@ TYPED_TEST(ContainerEqTest, DuplicateDifference) { // But in any case there should be no explanation. EXPECT_EQ("", Explain(m, test_set)); } +#endif // GTEST_HAS_TYPED_TEST // Tests that mutliple missing values are reported. // Using just vector here, so order is predicatble. @@ -3296,5 +3488,22 @@ TEST(FormatMatcherDescriptionTest, Strings(params, params + 1))); } +// Tests PolymorphicMatcher::mutable_impl(). +TEST(PolymorphicMatcherTest, CanAccessMutableImpl) { + PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42)); + DivisibleByImpl& impl = m.mutable_impl(); + EXPECT_EQ(42, impl.divider()); + + impl.set_divider(0); + EXPECT_EQ(0, m.mutable_impl().divider()); +} + +// Tests PolymorphicMatcher::impl(). +TEST(PolymorphicMatcherTest, CanAccessImpl) { + const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42)); + const DivisibleByImpl& impl = m.impl(); + EXPECT_EQ(42, impl.divider()); +} + } // namespace gmock_matchers_test } // namespace testing diff --git a/testing/gmock/test/gmock-more-actions_test.cc b/testing/gmock/test/gmock-more-actions_test.cc new file mode 100644 index 0000000..f5dab5b --- /dev/null +++ b/testing/gmock/test/gmock-more-actions_test.cc @@ -0,0 +1,664 @@ +// 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. +// +// Author: wan@google.com (Zhanyong Wan) + +// Google Mock - a framework for writing C++ mock classes. +// +// This file tests the built-in actions in gmock-more-actions.h. + +#include <gmock/gmock-more-actions.h> + +#include <functional> +#include <sstream> +#include <string> +#include <gmock/gmock.h> +#include <gtest/gtest.h> + +namespace testing { +namespace gmock_more_actions_test { + +using ::std::plus; +using ::std::string; +using ::std::tr1::get; +using ::std::tr1::make_tuple; +using ::std::tr1::tuple; +using ::std::tr1::tuple_element; +using testing::_; +using testing::Action; +using testing::ActionInterface; +using testing::DeleteArg; +using testing::Invoke; +using testing::Return; +using testing::ReturnArg; +using testing::SaveArg; +using testing::SetArgReferee; +using testing::SetArgumentPointee; +using testing::StaticAssertTypeEq; +using testing::Unused; +using testing::WithArg; +using testing::WithoutArgs; + +// Sample functions and functors for testing Invoke() and etc. +int Nullary() { return 1; } + +class NullaryFunctor { + public: + int operator()() { return 2; } +}; + +bool g_done = false; +void VoidNullary() { g_done = true; } + +class VoidNullaryFunctor { + public: + void operator()() { g_done = true; } +}; + +bool Unary(int x) { return x < 0; } + +const char* Plus1(const char* s) { return s + 1; } + +void VoidUnary(int n) { g_done = true; } + +bool ByConstRef(const string& s) { return s == "Hi"; } + +const double g_double = 0; +bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; } + +string ByNonConstRef(string& s) { return s += "+"; } // NOLINT + +struct UnaryFunctor { + int operator()(bool x) { return x ? 1 : -1; } +}; + +const char* Binary(const char* input, short n) { return input + n; } // NOLINT + +void VoidBinary(int, char) { g_done = true; } + +int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT + +void VoidTernary(int, char, bool) { g_done = true; } + +int SumOf4(int a, int b, int c, int d) { return a + b + c + d; } + +int SumOfFirst2(int a, int b, Unused, Unused) { return a + b; } + +void VoidFunctionWithFourArguments(char, int, float, double) { g_done = true; } + +string Concat4(const char* s1, const char* s2, const char* s3, + const char* s4) { + return string(s1) + s2 + s3 + s4; +} + +int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; } + +struct SumOf5Functor { + int operator()(int a, int b, int c, int d, int e) { + return a + b + c + d + e; + } +}; + +string Concat5(const char* s1, const char* s2, const char* s3, + const char* s4, const char* s5) { + return string(s1) + s2 + s3 + s4 + s5; +} + +int SumOf6(int a, int b, int c, int d, int e, int f) { + return a + b + c + d + e + f; +} + +struct SumOf6Functor { + int operator()(int a, int b, int c, int d, int e, int f) { + return a + b + c + d + e + f; + } +}; + +string Concat6(const char* s1, const char* s2, const char* s3, + const char* s4, const char* s5, const char* s6) { + return string(s1) + s2 + s3 + s4 + s5 + s6; +} + +string Concat7(const char* s1, const char* s2, const char* s3, + const char* s4, const char* s5, const char* s6, + const char* s7) { + return string(s1) + s2 + s3 + s4 + s5 + s6 + s7; +} + +string Concat8(const char* s1, const char* s2, const char* s3, + const char* s4, const char* s5, const char* s6, + const char* s7, const char* s8) { + return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8; +} + +string Concat9(const char* s1, const char* s2, const char* s3, + const char* s4, const char* s5, const char* s6, + const char* s7, const char* s8, const char* s9) { + return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9; +} + +string Concat10(const char* s1, const char* s2, const char* s3, + const char* s4, const char* s5, const char* s6, + const char* s7, const char* s8, const char* s9, + const char* s10) { + return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10; +} + +class Foo { + public: + Foo() : value_(123) {} + + int Nullary() const { return value_; } + + short Unary(long x) { return static_cast<short>(value_ + x); } // NOLINT + + string Binary(const string& str, char c) const { return str + c; } + + int Ternary(int x, bool y, char z) { return value_ + x + y*z; } + + int SumOf4(int a, int b, int c, int d) const { + return a + b + c + d + value_; + } + + int SumOfLast2(Unused, Unused, int a, int b) const { return a + b; } + + int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; } + + int SumOf6(int a, int b, int c, int d, int e, int f) { + return a + b + c + d + e + f; + } + + string Concat7(const char* s1, const char* s2, const char* s3, + const char* s4, const char* s5, const char* s6, + const char* s7) { + return string(s1) + s2 + s3 + s4 + s5 + s6 + s7; + } + + string Concat8(const char* s1, const char* s2, const char* s3, + const char* s4, const char* s5, const char* s6, + const char* s7, const char* s8) { + return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8; + } + + string Concat9(const char* s1, const char* s2, const char* s3, + const char* s4, const char* s5, const char* s6, + const char* s7, const char* s8, const char* s9) { + return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9; + } + + string Concat10(const char* s1, const char* s2, const char* s3, + const char* s4, const char* s5, const char* s6, + const char* s7, const char* s8, const char* s9, + const char* s10) { + return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10; + } + private: + int value_; +}; + +// Tests using Invoke() with a nullary function. +TEST(InvokeTest, Nullary) { + Action<int()> a = Invoke(Nullary); // NOLINT + EXPECT_EQ(1, a.Perform(make_tuple())); +} + +// Tests using Invoke() with a unary function. +TEST(InvokeTest, Unary) { + Action<bool(int)> a = Invoke(Unary); // NOLINT + EXPECT_FALSE(a.Perform(make_tuple(1))); + EXPECT_TRUE(a.Perform(make_tuple(-1))); +} + +// Tests using Invoke() with a binary function. +TEST(InvokeTest, Binary) { + Action<const char*(const char*, short)> a = Invoke(Binary); // NOLINT + const char* p = "Hello"; + EXPECT_EQ(p + 2, a.Perform(make_tuple(p, 2))); +} + +// Tests using Invoke() with a ternary function. +TEST(InvokeTest, Ternary) { + Action<int(int, char, short)> a = Invoke(Ternary); // NOLINT + EXPECT_EQ(6, a.Perform(make_tuple(1, '\2', 3))); +} + +// Tests using Invoke() with a 4-argument function. +TEST(InvokeTest, FunctionThatTakes4Arguments) { + Action<int(int, int, int, int)> a = Invoke(SumOf4); // NOLINT + EXPECT_EQ(1234, a.Perform(make_tuple(1000, 200, 30, 4))); +} + +// Tests using Invoke() with a 5-argument function. +TEST(InvokeTest, FunctionThatTakes5Arguments) { + Action<int(int, int, int, int, int)> a = Invoke(SumOf5); // NOLINT + EXPECT_EQ(12345, a.Perform(make_tuple(10000, 2000, 300, 40, 5))); +} + +// Tests using Invoke() with a 6-argument function. +TEST(InvokeTest, FunctionThatTakes6Arguments) { + Action<int(int, int, int, int, int, int)> a = Invoke(SumOf6); // NOLINT + EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6))); +} + +// A helper that turns the type of a C-string literal from const +// char[N] to const char*. +inline const char* CharPtr(const char* s) { return s; } + +// Tests using Invoke() with a 7-argument function. +TEST(InvokeTest, FunctionThatTakes7Arguments) { + Action<string(const char*, const char*, const char*, const char*, + const char*, const char*, const char*)> a = + Invoke(Concat7); + EXPECT_EQ("1234567", + a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), + CharPtr("4"), CharPtr("5"), CharPtr("6"), + CharPtr("7")))); +} + +// Tests using Invoke() with a 8-argument function. +TEST(InvokeTest, FunctionThatTakes8Arguments) { + Action<string(const char*, const char*, const char*, const char*, + const char*, const char*, const char*, const char*)> a = + Invoke(Concat8); + EXPECT_EQ("12345678", + a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), + CharPtr("4"), CharPtr("5"), CharPtr("6"), + CharPtr("7"), CharPtr("8")))); +} + +// Tests using Invoke() with a 9-argument function. +TEST(InvokeTest, FunctionThatTakes9Arguments) { + Action<string(const char*, const char*, const char*, const char*, + const char*, const char*, const char*, const char*, + const char*)> a = Invoke(Concat9); + EXPECT_EQ("123456789", + a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), + CharPtr("4"), CharPtr("5"), CharPtr("6"), + CharPtr("7"), CharPtr("8"), CharPtr("9")))); +} + +// Tests using Invoke() with a 10-argument function. +TEST(InvokeTest, FunctionThatTakes10Arguments) { + Action<string(const char*, const char*, const char*, const char*, + const char*, const char*, const char*, const char*, + const char*, const char*)> a = Invoke(Concat10); + EXPECT_EQ("1234567890", + a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), + CharPtr("4"), CharPtr("5"), CharPtr("6"), + CharPtr("7"), CharPtr("8"), CharPtr("9"), + CharPtr("0")))); +} + +// Tests using Invoke() with functions with parameters declared as Unused. +TEST(InvokeTest, FunctionWithUnusedParameters) { + Action<int(int, int, double, const string&)> a1 = + Invoke(SumOfFirst2); + EXPECT_EQ(12, a1.Perform(make_tuple(10, 2, 5.6, CharPtr("hi")))); + + Action<int(int, int, bool, int*)> a2 = + Invoke(SumOfFirst2); + EXPECT_EQ(23, a2.Perform(make_tuple(20, 3, true, static_cast<int*>(NULL)))); +} + +// Tests using Invoke() with methods with parameters declared as Unused. +TEST(InvokeTest, MethodWithUnusedParameters) { + Foo foo; + Action<int(string, bool, int, int)> a1 = + Invoke(&foo, &Foo::SumOfLast2); + EXPECT_EQ(12, a1.Perform(make_tuple(CharPtr("hi"), true, 10, 2))); + + Action<int(char, double, int, int)> a2 = + Invoke(&foo, &Foo::SumOfLast2); + EXPECT_EQ(23, a2.Perform(make_tuple('a', 2.5, 20, 3))); +} + +// Tests using Invoke() with a functor. +TEST(InvokeTest, Functor) { + Action<int(short, char)> a = Invoke(plus<short>()); // NOLINT + EXPECT_EQ(3, a.Perform(make_tuple(1, 2))); +} + +// Tests using Invoke(f) as an action of a compatible type. +TEST(InvokeTest, FunctionWithCompatibleType) { + Action<long(int, short, char, bool)> a = Invoke(SumOf4); // NOLINT + EXPECT_EQ(4321, a.Perform(make_tuple(4000, 300, 20, true))); +} + +// Tests using Invoke() with an object pointer and a method pointer. + +// Tests using Invoke() with a nullary method. +TEST(InvokeMethodTest, Nullary) { + Foo foo; + Action<int()> a = Invoke(&foo, &Foo::Nullary); // NOLINT + EXPECT_EQ(123, a.Perform(make_tuple())); +} + +// Tests using Invoke() with a unary method. +TEST(InvokeMethodTest, Unary) { + Foo foo; + Action<short(long)> a = Invoke(&foo, &Foo::Unary); // NOLINT + EXPECT_EQ(4123, a.Perform(make_tuple(4000))); +} + +// Tests using Invoke() with a binary method. +TEST(InvokeMethodTest, Binary) { + Foo foo; + Action<string(const string&, char)> a = Invoke(&foo, &Foo::Binary); + string s("Hell"); + EXPECT_EQ("Hello", a.Perform(make_tuple(s, 'o'))); +} + +// Tests using Invoke() with a ternary method. +TEST(InvokeMethodTest, Ternary) { + Foo foo; + Action<int(int, bool, char)> a = Invoke(&foo, &Foo::Ternary); // NOLINT + EXPECT_EQ(1124, a.Perform(make_tuple(1000, true, 1))); +} + +// Tests using Invoke() with a 4-argument method. +TEST(InvokeMethodTest, MethodThatTakes4Arguments) { + Foo foo; + Action<int(int, int, int, int)> a = Invoke(&foo, &Foo::SumOf4); // NOLINT + EXPECT_EQ(1357, a.Perform(make_tuple(1000, 200, 30, 4))); +} + +// Tests using Invoke() with a 5-argument method. +TEST(InvokeMethodTest, MethodThatTakes5Arguments) { + Foo foo; + Action<int(int, int, int, int, int)> a = Invoke(&foo, &Foo::SumOf5); // NOLINT + EXPECT_EQ(12345, a.Perform(make_tuple(10000, 2000, 300, 40, 5))); +} + +// Tests using Invoke() with a 6-argument method. +TEST(InvokeMethodTest, MethodThatTakes6Arguments) { + Foo foo; + Action<int(int, int, int, int, int, int)> a = // NOLINT + Invoke(&foo, &Foo::SumOf6); + EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6))); +} + +// Tests using Invoke() with a 7-argument method. +TEST(InvokeMethodTest, MethodThatTakes7Arguments) { + Foo foo; + Action<string(const char*, const char*, const char*, const char*, + const char*, const char*, const char*)> a = + Invoke(&foo, &Foo::Concat7); + EXPECT_EQ("1234567", + a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), + CharPtr("4"), CharPtr("5"), CharPtr("6"), + CharPtr("7")))); +} + +// Tests using Invoke() with a 8-argument method. +TEST(InvokeMethodTest, MethodThatTakes8Arguments) { + Foo foo; + Action<string(const char*, const char*, const char*, const char*, + const char*, const char*, const char*, const char*)> a = + Invoke(&foo, &Foo::Concat8); + EXPECT_EQ("12345678", + a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), + CharPtr("4"), CharPtr("5"), CharPtr("6"), + CharPtr("7"), CharPtr("8")))); +} + +// Tests using Invoke() with a 9-argument method. +TEST(InvokeMethodTest, MethodThatTakes9Arguments) { + Foo foo; + Action<string(const char*, const char*, const char*, const char*, + const char*, const char*, const char*, const char*, + const char*)> a = Invoke(&foo, &Foo::Concat9); + EXPECT_EQ("123456789", + a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), + CharPtr("4"), CharPtr("5"), CharPtr("6"), + CharPtr("7"), CharPtr("8"), CharPtr("9")))); +} + +// Tests using Invoke() with a 10-argument method. +TEST(InvokeMethodTest, MethodThatTakes10Arguments) { + Foo foo; + Action<string(const char*, const char*, const char*, const char*, + const char*, const char*, const char*, const char*, + const char*, const char*)> a = Invoke(&foo, &Foo::Concat10); + EXPECT_EQ("1234567890", + a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), + CharPtr("4"), CharPtr("5"), CharPtr("6"), + CharPtr("7"), CharPtr("8"), CharPtr("9"), + CharPtr("0")))); +} + +// Tests using Invoke(f) as an action of a compatible type. +TEST(InvokeMethodTest, MethodWithCompatibleType) { + Foo foo; + Action<long(int, short, char, bool)> a = // NOLINT + Invoke(&foo, &Foo::SumOf4); + EXPECT_EQ(4444, a.Perform(make_tuple(4000, 300, 20, true))); +} + +// Tests using WithoutArgs with an action that takes no argument. +TEST(WithoutArgsTest, NoArg) { + Action<int(int n)> a = WithoutArgs(Invoke(Nullary)); // NOLINT + EXPECT_EQ(1, a.Perform(make_tuple(2))); +} + +// Tests using WithArg with an action that takes 1 argument. +TEST(WithArgTest, OneArg) { + Action<bool(double x, int n)> b = WithArg<1>(Invoke(Unary)); // NOLINT + EXPECT_TRUE(b.Perform(make_tuple(1.5, -1))); + EXPECT_FALSE(b.Perform(make_tuple(1.5, 1))); +} + +TEST(ReturnArgActionTest, WorksForOneArgIntArg0) { + const Action<int(int)> a = ReturnArg<0>(); + EXPECT_EQ(5, a.Perform(make_tuple(5))); +} + +TEST(ReturnArgActionTest, WorksForMultiArgBoolArg0) { + const Action<bool(bool, bool, bool)> a = ReturnArg<0>(); + EXPECT_TRUE(a.Perform(make_tuple(true, false, false))); +} + +TEST(ReturnArgActionTest, WorksForMultiArgStringArg2) { + const Action<string(int, int, string, int)> a = ReturnArg<2>(); + EXPECT_EQ("seven", a.Perform(make_tuple(5, 6, string("seven"), 8))); +} + +TEST(SaveArgActionTest, WorksForSameType) { + int result = 0; + const Action<void(int n)> a1 = SaveArg<0>(&result); + a1.Perform(make_tuple(5)); + EXPECT_EQ(5, result); +} + +TEST(SaveArgActionTest, WorksForCompatibleType) { + int result = 0; + const Action<void(bool, char)> a1 = SaveArg<1>(&result); + a1.Perform(make_tuple(true, 'a')); + EXPECT_EQ('a', result); +} + +TEST(SetArgRefereeActionTest, WorksForSameType) { + int value = 0; + const Action<void(int&)> a1 = SetArgReferee<0>(1); + a1.Perform(tuple<int&>(value)); + EXPECT_EQ(1, value); +} + +TEST(SetArgRefereeActionTest, WorksForCompatibleType) { + int value = 0; + const Action<void(int, int&)> a1 = SetArgReferee<1>('a'); + a1.Perform(tuple<int, int&>(0, value)); + EXPECT_EQ('a', value); +} + +TEST(SetArgRefereeActionTest, WorksWithExtraArguments) { + int value = 0; + const Action<void(bool, int, int&, const char*)> a1 = SetArgReferee<2>('a'); + a1.Perform(tuple<bool, int, int&, const char*>(true, 0, value, "hi")); + EXPECT_EQ('a', value); +} + +// A class that can be used to verify that its destructor is called: it will set +// the bool provided to the constructor to true when destroyed. +class DeletionTester { + public: + explicit DeletionTester(bool* is_deleted) + : is_deleted_(is_deleted) { + // Make sure the bit is set to false. + *is_deleted_ = false; + } + + ~DeletionTester() { + *is_deleted_ = true; + } + + private: + bool* is_deleted_; +}; + +TEST(DeleteArgActionTest, OneArg) { + bool is_deleted = false; + DeletionTester* t = new DeletionTester(&is_deleted); + const Action<void(DeletionTester*)> a1 = DeleteArg<0>(); // NOLINT + EXPECT_FALSE(is_deleted); + a1.Perform(make_tuple(t)); + EXPECT_TRUE(is_deleted); +} + +TEST(DeleteArgActionTest, TenArgs) { + bool is_deleted = false; + DeletionTester* t = new DeletionTester(&is_deleted); + const Action<void(bool, int, int, const char*, bool, + int, int, int, int, DeletionTester*)> a1 = DeleteArg<9>(); + EXPECT_FALSE(is_deleted); + a1.Perform(make_tuple(true, 5, 6, CharPtr("hi"), false, 7, 8, 9, 10, t)); + EXPECT_TRUE(is_deleted); +} + +#if GTEST_HAS_EXCEPTIONS + +TEST(ThrowActionTest, ThrowsGivenExceptionInVoidFunction) { + const Action<void(int n)> a = Throw('a'); + EXPECT_THROW(a.Perform(make_tuple(0)), char); +} + +class MyException {}; + +TEST(ThrowActionTest, ThrowsGivenExceptionInNonVoidFunction) { + const Action<double(char ch)> a = Throw(MyException()); + EXPECT_THROW(a.Perform(make_tuple('0')), MyException); +} + +TEST(ThrowActionTest, ThrowsGivenExceptionInNullaryFunction) { + const Action<double()> a = Throw(MyException()); + EXPECT_THROW(a.Perform(make_tuple()), MyException); +} + +#endif // GTEST_HAS_EXCEPTIONS + +// Tests that SetArrayArgument<N>(first, last) sets the elements of the array +// pointed to by the N-th (0-based) argument to values in range [first, last). +TEST(SetArrayArgumentTest, SetsTheNthArray) { + typedef void MyFunction(bool, int*, char*); + int numbers[] = { 1, 2, 3 }; + Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers + 3); + + int n[4] = {}; + int* pn = n; + char ch[4] = {}; + char* pch = ch; + a.Perform(make_tuple(true, pn, pch)); + EXPECT_EQ(1, n[0]); + EXPECT_EQ(2, n[1]); + EXPECT_EQ(3, n[2]); + EXPECT_EQ(0, n[3]); + EXPECT_EQ('\0', ch[0]); + EXPECT_EQ('\0', ch[1]); + EXPECT_EQ('\0', ch[2]); + EXPECT_EQ('\0', ch[3]); + + // Tests first and last are iterators. + std::string letters = "abc"; + a = SetArrayArgument<2>(letters.begin(), letters.end()); + std::fill_n(n, 4, 0); + std::fill_n(ch, 4, '\0'); + a.Perform(make_tuple(true, pn, pch)); + EXPECT_EQ(0, n[0]); + EXPECT_EQ(0, n[1]); + EXPECT_EQ(0, n[2]); + EXPECT_EQ(0, n[3]); + EXPECT_EQ('a', ch[0]); + EXPECT_EQ('b', ch[1]); + EXPECT_EQ('c', ch[2]); + EXPECT_EQ('\0', ch[3]); +} + +// Tests SetArrayArgument<N>(first, last) where first == last. +TEST(SetArrayArgumentTest, SetsTheNthArrayWithEmptyRange) { + typedef void MyFunction(bool, int*); + int numbers[] = { 1, 2, 3 }; + Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers); + + int n[4] = {}; + int* pn = n; + a.Perform(make_tuple(true, pn)); + EXPECT_EQ(0, n[0]); + EXPECT_EQ(0, n[1]); + EXPECT_EQ(0, n[2]); + EXPECT_EQ(0, n[3]); +} + +// Tests SetArrayArgument<N>(first, last) where *first is convertible +// (but not equal) to the argument type. +TEST(SetArrayArgumentTest, SetsTheNthArrayWithConvertibleType) { + typedef void MyFunction(bool, char*); + int codes[] = { 97, 98, 99 }; + Action<MyFunction> a = SetArrayArgument<1>(codes, codes + 3); + + char ch[4] = {}; + char* pch = ch; + a.Perform(make_tuple(true, pch)); + EXPECT_EQ('a', ch[0]); + EXPECT_EQ('b', ch[1]); + EXPECT_EQ('c', ch[2]); + EXPECT_EQ('\0', ch[3]); +} + +// Test SetArrayArgument<N>(first, last) with iterator as argument. +TEST(SetArrayArgumentTest, SetsTheNthArrayWithIteratorArgument) { + typedef void MyFunction(bool, std::back_insert_iterator<std::string>); + std::string letters = "abc"; + Action<MyFunction> a = SetArrayArgument<1>(letters.begin(), letters.end()); + + std::string s; + a.Perform(make_tuple(true, back_inserter(s))); + EXPECT_EQ(letters, s); +} + +} // namespace gmock_generated_actions_test +} // namespace testing diff --git a/testing/gmock/test/gmock-nice-strict_test.cc b/testing/gmock/test/gmock-nice-strict_test.cc index 955961c..0dc7106 100644 --- a/testing/gmock/test/gmock-nice-strict_test.cc +++ b/testing/gmock/test/gmock-nice-strict_test.cc @@ -36,6 +36,13 @@ #include <gtest/gtest.h> #include <gtest/gtest-spi.h> +// This must not be defined inside the ::testing namespace, or it will +// clash with ::testing::Mock. +class Mock { + public: + MOCK_METHOD0(DoThis, void()); +}; + namespace testing { namespace gmock_nice_strict_test { @@ -166,6 +173,22 @@ TEST(NiceMockTest, NonDefaultConstructor10) { nice_bar.That(5, true); } +#if !GTEST_OS_SYMBIAN +// Tests that NiceMock<Mock> compiles where Mock is a user-defined +// class (as opposed to ::testing::Mock). We had to workaround 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, as it causes the program to +// crash there, for reasons unclear to us yet. +TEST(NiceMockTest, AcceptsClassNamedMock) { + NiceMock< ::Mock> nice; + EXPECT_CALL(nice, DoThis()); + nice.DoThis(); +} +#endif // !GTEST_OS_SYMBIAN + // Tests that a strict mock allows expected calls. TEST(StrictMockTest, AllowsExpectedCall) { StrictMock<MockFoo> strict_foo; @@ -224,5 +247,21 @@ TEST(StrictMockTest, NonDefaultConstructor10) { "Uninteresting mock function call"); } +#if !GTEST_OS_SYMBIAN +// Tests that StrictMock<Mock> compiles where Mock is a user-defined +// class (as opposed to ::testing::Mock). We had to workaround 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, as it causes the program to +// crash there, for reasons unclear to us yet. +TEST(StrictMockTest, AcceptsClassNamedMock) { + StrictMock< ::Mock> strict; + EXPECT_CALL(strict, DoThis()); + strict.DoThis(); +} +#endif // !GTEST_OS_SYMBIAN + } // namespace gmock_nice_strict_test } // namespace testing diff --git a/testing/gmock/test/gmock-port_test.cc b/testing/gmock/test/gmock-port_test.cc index 2e85bcc..9a64ec33e 100644 --- a/testing/gmock/test/gmock-port_test.cc +++ b/testing/gmock/test/gmock-port_test.cc @@ -36,60 +36,4 @@ #include <gmock/internal/gmock-port.h> #include <gtest/gtest.h> -TEST(GmockCheckSyntaxTest, BehavesLikeASingleStatement) { - if (false) - GMOCK_CHECK_(false) << "This should never be executed; " - "It's a compilation test only."; - - if (true) - GMOCK_CHECK_(true); - else - ; - - if (false) - ; - else - GMOCK_CHECK_(true) << ""; -} - -TEST(GmockCheckSyntaxTest, WorksWithSwitch) { - switch (0) { - case 1: - break; - default: - GMOCK_CHECK_(true); - } - - switch(0) - case 0: - GMOCK_CHECK_(true) << "Check failed in switch case"; -} - -#if GTEST_HAS_DEATH_TEST - -TEST(GmockCheckDeathTest, DiesWithCorrectOutputOnFailure) { - const bool a_false_condition = false; - // MSVC and gcc use different formats to print source file locations. - // Google Mock's failure messages use the same format as used by the - // compiler, in order for the IDE to recognize them. Therefore we look - // for different patterns here depending on the compiler. - const char regex[] = -#ifdef _MSC_VER - "gmock-port_test\\.cc\\(\\d+\\):" -#else - "gmock-port_test\\.cc:[0-9]+" -#endif // _MSC_VER - ".*a_false_condition.*Extra info"; - - EXPECT_DEATH(GMOCK_CHECK_(a_false_condition) << "Extra info", regex); -} - -TEST(GmockCheckDeathTest, LivesSilentlyOnSuccess) { - EXPECT_EXIT({ - GMOCK_CHECK_(true) << "Extra info"; - ::std::cerr << "Success\n"; - exit(0); }, - ::testing::ExitedWithCode(0), "Success"); -} - -#endif // GTEST_HAS_DEATH_TEST +// This file intentionally contains no test at this moment. diff --git a/testing/gmock/test/gmock-printers_test.cc b/testing/gmock/test/gmock-printers_test.cc index 8c03ec4..c72e3d3 100644 --- a/testing/gmock/test/gmock-printers_test.cc +++ b/testing/gmock/test/gmock-printers_test.cc @@ -705,7 +705,7 @@ TEST(PrintStlContainerTest, HashMultiSet) { std::vector<int> numbers; for (size_t i = 0; i != result.length(); i++) { if (expected_pattern[i] == 'd') { - ASSERT_TRUE(isdigit(result[i])); + ASSERT_TRUE(isdigit(result[i]) != 0); numbers.push_back(result[i] - '0'); } else { EXPECT_EQ(expected_pattern[i], result[i]) << " where result is " @@ -790,14 +790,14 @@ TEST(PrintStlContainerTest, NestedContainer) { } TEST(PrintStlContainerTest, OneDimensionalNativeArray) { - const int a[] = { 1, 2, 3 }; - NativeArray<int> b(a, kReference); + const int a[3] = { 1, 2, 3 }; + NativeArray<int> b(a, 3, kReference); EXPECT_EQ("{ 1, 2, 3 }", Print(b)); } TEST(PrintStlContainerTest, TwoDimensionalNativeArray) { - const int a[][3] = { { 1, 2, 3 }, { 4, 5, 6 } }; - NativeArray<int[3]> b(a, kReference); + const int a[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } }; + NativeArray<int[3]> b(a, 2, kReference); EXPECT_EQ("{ { 1, 2, 3 }, { 4, 5, 6 } }", Print(b)); } @@ -840,16 +840,16 @@ TEST(PrintTupleTest, VariousSizes) { const char* const str = "8"; tuple<bool, char, short, testing::internal::Int32, // NOLINT testing::internal::Int64, float, double, const char*, void*, string> - t10(false, 'a', 3, 4, 5, 6.5F, 7.5, str, NULL, "10"); - EXPECT_EQ("(false, 'a' (97), 3, 4, 5, 6.5, 7.5, " + PrintPointer(str) + + t10(false, 'a', 3, 4, 5, 1.5F, -2.5, str, NULL, "10"); + EXPECT_EQ("(false, 'a' (97), 3, 4, 5, 1.5, -2.5, " + PrintPointer(str) + " pointing to \"8\", NULL, \"10\")", Print(t10)); } // Nested tuples. TEST(PrintTupleTest, NestedTuple) { - tuple<tuple<int, double>, char> nested(make_tuple(5, 9.5), 'a'); - EXPECT_EQ("((5, 9.5), 'a' (97))", Print(nested)); + tuple<tuple<int, bool>, char> nested(make_tuple(5, true), 'a'); + EXPECT_EQ("((5, true), 'a' (97))", Print(nested)); } // Tests printing user-defined unprintable types. @@ -919,12 +919,31 @@ TEST(PrintProtocolMessageTest, PrintsShortDebugString) { EXPECT_EQ("<member:\"yes\">", Print(msg)); } -// Tests printing a proto2 message. -TEST(PrintProto2MessageTest, PrintsShortDebugString) { +// Tests printing a short proto2 message. +TEST(PrintProto2MessageTest, PrintsShortDebugStringWhenItIsShort) { testing::internal::FooMessage msg; msg.set_int_field(2); + msg.set_string_field("hello"); EXPECT_PRED2(RE::FullMatch, Print(msg), - "<int_field:\\s*2\\s*>"); + "<int_field:\\s*2\\s+string_field:\\s*\"hello\">"); +} + +// Tests printing a long proto2 message. +TEST(PrintProto2MessageTest, PrintsDebugStringWhenItIsLong) { + testing::internal::FooMessage msg; + msg.set_int_field(2); + msg.set_string_field("hello"); + msg.add_names("peter"); + msg.add_names("paul"); + msg.add_names("mary"); + EXPECT_PRED2(RE::FullMatch, Print(msg), + "<\n" + "int_field:\\s*2\n" + "string_field:\\s*\"hello\"\n" + "names:\\s*\"peter\"\n" + "names:\\s*\"paul\"\n" + "names:\\s*\"mary\"\n" + ">"); } #endif // GMOCK_HAS_PROTOBUF_ diff --git a/testing/gmock/test/gmock-spec-builders_test.cc b/testing/gmock/test/gmock-spec-builders_test.cc index 058d343c..707d896 100644 --- a/testing/gmock/test/gmock-spec-builders_test.cc +++ b/testing/gmock/test/gmock-spec-builders_test.cc @@ -71,6 +71,9 @@ using testing::CardinalityInterface; using testing::Const; using testing::DoAll; using testing::DoDefault; +using testing::Eq; +using testing::Expectation; +using testing::ExpectationSet; using testing::GMOCK_FLAG(verbose); using testing::Gt; using testing::InSequence; @@ -80,13 +83,13 @@ using testing::IsSubstring; using testing::Lt; using testing::Message; using testing::Mock; +using testing::Ne; using testing::Return; using testing::Sequence; using testing::internal::g_gmock_mutex; using testing::internal::kErrorVerbosity; using testing::internal::kInfoVerbosity; using testing::internal::kWarningVerbosity; -using testing::internal::Expectation; using testing::internal::ExpectationTester; using testing::internal::string; @@ -194,19 +197,15 @@ TEST(OnCallSyntaxTest, WithCanAppearAtMostOnce) { }, ".With() cannot appear more than once in an ON_CALL()"); } -#if GTEST_HAS_DEATH_TEST - TEST(OnCallSyntaxTest, WillByDefaultIsMandatory) { MockA a; - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ ON_CALL(a, DoA(5)); a.DoA(5); }, ""); } -#endif // GTEST_HAS_DEATH_TEST - TEST(OnCallSyntaxTest, WillByDefaultCanAppearAtMostOnce) { MockA a; @@ -345,7 +344,22 @@ TEST(ExpectCallSyntaxTest, InSequenceCanAppearMultipleTimes) { a.DoA(1); } -TEST(ExpectCallSyntaxTest, InSequenceMustBeBeforeWill) { +TEST(ExpectCallSyntaxTest, InSequenceMustBeBeforeAfter) { + MockA a; + Sequence s; + + Expectation e = EXPECT_CALL(a, DoA(1)) + .Times(AnyNumber()); + EXPECT_NONFATAL_FAILURE({ // NOLINT + EXPECT_CALL(a, DoA(2)) + .After(e) + .InSequence(s); + }, ".InSequence() cannot appear after "); + + a.DoA(2); +} + +TEST(ExpectCallSyntaxTest, InSequenceMustBeBeforeWillOnce) { MockA a; Sequence s; @@ -358,6 +372,20 @@ TEST(ExpectCallSyntaxTest, InSequenceMustBeBeforeWill) { a.DoA(1); } +TEST(ExpectCallSyntaxTest, AfterMustBeBeforeWillOnce) { + MockA a; + + Expectation e = EXPECT_CALL(a, DoA(1)); + EXPECT_NONFATAL_FAILURE({ + EXPECT_CALL(a, DoA(2)) + .WillOnce(Return()) + .After(e); + }, ".After() cannot appear after "); + + a.DoA(1); + a.DoA(2); +} + TEST(ExpectCallSyntaxTest, WillIsOptional) { MockA a; @@ -957,7 +985,7 @@ TEST(UnexpectedCallTest, UnsatisifiedPrerequisites) { // There should be one non-fatal failure. ASSERT_EQ(1, failures.size()); const ::testing::TestPartResult& r = failures.GetTestPartResult(0); - EXPECT_EQ(::testing::TPRT_NONFATAL_FAILURE, r.type()); + EXPECT_EQ(::testing::TestPartResult::kNonFatalFailure, r.type()); // Verifies that the failure message contains the two unsatisfied // pre-requisites but not the satisfied one. @@ -986,18 +1014,14 @@ TEST(UnexpectedCallTest, UnsatisifiedPrerequisites) { #endif // GMOCK_HAS_REGEX -#if GTEST_HAS_DEATH_TEST - TEST(UndefinedReturnValueTest, ReturnValueIsMandatory) { MockA a; // TODO(wan@google.com): We should really verify the output message, // but we cannot yet due to that EXPECT_DEATH only captures stderr // while Google Mock logs to stdout. - EXPECT_DEATH(a.ReturnResult(1), ""); + EXPECT_DEATH_IF_SUPPORTED(a.ReturnResult(1), ""); } -#endif // GTEST_HAS_DEATH_TEST - // Tests that an excessive call (one whose arguments match the // matchers but is called too many times) performs the default action. TEST(ExcessiveCallTest, DoesDefaultAction) { @@ -1142,8 +1166,6 @@ TEST(SequenceTest, AnyOrderIsOkByDefault) { } } -#if GTEST_HAS_DEATH_TEST - // Tests that the calls must be in strict order when a complete order // is specified. TEST(SequenceTest, CallsMustBeInStrictOrderWhenSaidSo) { @@ -1162,13 +1184,13 @@ TEST(SequenceTest, CallsMustBeInStrictOrderWhenSaidSo) { .InSequence(s) .WillOnce(Return(Result())); - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ a.ReturnResult(1); a.ReturnResult(3); a.ReturnResult(2); }, ""); - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ a.ReturnResult(2); a.ReturnResult(1); a.ReturnResult(3); @@ -1201,21 +1223,21 @@ TEST(SequenceTest, CallsMustConformToSpecifiedDag) { .InSequence(x) .WillOnce(Return(Result())); - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ a.ReturnResult(1); b.DoB(); a.ReturnResult(2); }, ""); - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ a.ReturnResult(2); }, ""); - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ a.ReturnResult(3); }, ""); - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ a.ReturnResult(1); b.DoB(); b.DoB(); @@ -1229,8 +1251,6 @@ TEST(SequenceTest, CallsMustConformToSpecifiedDag) { a.ReturnResult(3); } -#endif // GTEST_HAS_DEATH_TEST - TEST(SequenceTest, Retirement) { MockA a; Sequence s; @@ -1248,6 +1268,292 @@ TEST(SequenceTest, Retirement) { a.DoA(1); } +// Tests Expectation. + +TEST(ExpectationTest, ConstrutorsWork) { + MockA a; + Expectation e1; // Default ctor. + Expectation e2 = EXPECT_CALL(a, DoA(1)); // Ctor from EXPECT_CALL. + Expectation e3 = e2; // Copy ctor. + + EXPECT_THAT(e1, Ne(e2)); + EXPECT_THAT(e2, Eq(e3)); + a.DoA(1); +} + +TEST(ExpectationTest, AssignmentWorks) { + MockA a; + Expectation e1; + Expectation e2 = EXPECT_CALL(a, DoA(1)); + + EXPECT_THAT(e1, Ne(e2)); + + e1 = e2; + EXPECT_THAT(e1, Eq(e2)); + + a.DoA(1); +} + +// Tests ExpectationSet. + +TEST(ExpectationSetTest, MemberTypesAreCorrect) { + ::testing::StaticAssertTypeEq<Expectation, ExpectationSet::value_type>(); +} + +TEST(ExpectationSetTest, ConstructorsWork) { + MockA a; + + Expectation e1; + const Expectation e2; + ExpectationSet es1; // Default ctor. + ExpectationSet es2 = EXPECT_CALL(a, DoA(1)); // Ctor from EXPECT_CALL. + ExpectationSet es3 = e1; // Ctor from Expectation. + ExpectationSet es4(e1); // Ctor from Expectation; alternative syntax. + ExpectationSet es5 = e2; // Ctor from const Expectation. + ExpectationSet es6(e2); // Ctor from const Expectation; alternative syntax. + ExpectationSet es7 = es2; // Copy ctor. + + EXPECT_EQ(0, es1.size()); + EXPECT_EQ(1, es2.size()); + EXPECT_EQ(1, es3.size()); + EXPECT_EQ(1, es4.size()); + EXPECT_EQ(1, es5.size()); + EXPECT_EQ(1, es6.size()); + EXPECT_EQ(1, es7.size()); + + EXPECT_THAT(es3, Ne(es2)); + EXPECT_THAT(es4, Eq(es3)); + EXPECT_THAT(es5, Eq(es4)); + EXPECT_THAT(es6, Eq(es5)); + EXPECT_THAT(es7, Eq(es2)); + a.DoA(1); +} + +TEST(ExpectationSetTest, AssignmentWorks) { + ExpectationSet es1; + ExpectationSet es2 = Expectation(); + + es1 = es2; + EXPECT_EQ(1, es1.size()); + EXPECT_THAT(*(es1.begin()), Eq(Expectation())); + EXPECT_THAT(es1, Eq(es2)); +} + +TEST(ExpectationSetTest, InsertionWorks) { + ExpectationSet es1; + Expectation e1; + es1 += e1; + EXPECT_EQ(1, es1.size()); + EXPECT_THAT(*(es1.begin()), Eq(e1)); + + MockA a; + Expectation e2 = EXPECT_CALL(a, DoA(1)); + es1 += e2; + EXPECT_EQ(2, es1.size()); + + ExpectationSet::const_iterator it1 = es1.begin(); + ExpectationSet::const_iterator it2 = it1; + ++it2; + EXPECT_TRUE(*it1 == e1 || *it2 == e1); // e1 must be in the set. + EXPECT_TRUE(*it1 == e2 || *it2 == e2); // e2 must be in the set too. + a.DoA(1); +} + +TEST(ExpectationSetTest, SizeWorks) { + ExpectationSet es; + EXPECT_EQ(0, es.size()); + + es += Expectation(); + EXPECT_EQ(1, es.size()); + + MockA a; + es += EXPECT_CALL(a, DoA(1)); + EXPECT_EQ(2, es.size()); + + a.DoA(1); +} + +TEST(ExpectationSetTest, IsEnumerable) { + ExpectationSet es; + EXPECT_THAT(es.begin(), Eq(es.end())); + + es += Expectation(); + ExpectationSet::const_iterator it = es.begin(); + EXPECT_THAT(it, Ne(es.end())); + EXPECT_THAT(*it, Eq(Expectation())); + ++it; + EXPECT_THAT(it, Eq(es.end())); +} + +// Tests the .After() clause. + +TEST(AfterTest, SucceedsWhenPartialOrderIsSatisfied) { + MockA a; + ExpectationSet es; + es += EXPECT_CALL(a, DoA(1)); + es += EXPECT_CALL(a, DoA(2)); + EXPECT_CALL(a, DoA(3)) + .After(es); + + a.DoA(1); + a.DoA(2); + a.DoA(3); +} + +TEST(AfterTest, SucceedsWhenTotalOrderIsSatisfied) { + MockA a; + MockB b; + // The following also verifies that const Expectation objects work + // too. Do not remove the const modifiers. + const Expectation e1 = EXPECT_CALL(a, DoA(1)); + const Expectation e2 = EXPECT_CALL(b, DoB()) + .Times(2) + .After(e1); + EXPECT_CALL(a, DoA(2)).After(e2); + + a.DoA(1); + b.DoB(); + b.DoB(); + a.DoA(2); +} + +// Calls must be in strict order when specified so. +TEST(AfterDeathTest, CallsMustBeInStrictOrderWhenSpecifiedSo) { + MockA a; + MockB b; + Expectation e1 = EXPECT_CALL(a, DoA(1)); + Expectation e2 = EXPECT_CALL(b, DoB()) + .Times(2) + .After(e1); + EXPECT_CALL(a, ReturnResult(2)) + .After(e2) + .WillOnce(Return(Result())); + + a.DoA(1); + // If a call to ReturnResult() violates the specified order, no + // matching expectation will be found, and thus the default action + // will be done. Since the return type of ReturnResult() is not a + // built-in type, gmock won't know what to return and will thus + // abort the program. Therefore a death test can tell us whether + // gmock catches the order violation correctly. + // + // gtest and gmock print messages to stdout, which isn't captured by + // death tests. Therefore we have to match with an empty regular + // expression in all the EXPECT_DEATH()s. + EXPECT_DEATH_IF_SUPPORTED(a.ReturnResult(2), ""); + + b.DoB(); + EXPECT_DEATH_IF_SUPPORTED(a.ReturnResult(2), ""); + + b.DoB(); + a.ReturnResult(2); +} + +// Calls must satisfy the partial order when specified so. +TEST(AfterDeathTest, CallsMustSatisfyPartialOrderWhenSpecifiedSo) { + MockA a; + Expectation e = EXPECT_CALL(a, DoA(1)); + const ExpectationSet es = EXPECT_CALL(a, DoA(2)); + EXPECT_CALL(a, ReturnResult(3)) + .After(e, es) + .WillOnce(Return(Result())); + + EXPECT_DEATH_IF_SUPPORTED(a.ReturnResult(3), ""); + + a.DoA(2); + EXPECT_DEATH_IF_SUPPORTED(a.ReturnResult(3), ""); + + a.DoA(1); + a.ReturnResult(3); +} + +// .After() can be combined with .InSequence(). +TEST(AfterDeathTest, CanBeUsedWithInSequence) { + MockA a; + Sequence s; + Expectation e = EXPECT_CALL(a, DoA(1)); + EXPECT_CALL(a, DoA(2)).InSequence(s); + EXPECT_CALL(a, ReturnResult(3)) + .InSequence(s).After(e) + .WillOnce(Return(Result())); + + a.DoA(1); + EXPECT_DEATH_IF_SUPPORTED(a.ReturnResult(3), ""); + + a.DoA(2); + a.ReturnResult(3); +} + +// .After() can be called multiple times. +TEST(AfterTest, CanBeCalledManyTimes) { + MockA a; + Expectation e1 = EXPECT_CALL(a, DoA(1)); + Expectation e2 = EXPECT_CALL(a, DoA(2)); + Expectation e3 = EXPECT_CALL(a, DoA(3)); + EXPECT_CALL(a, DoA(4)) + .After(e1) + .After(e2) + .After(e3); + + a.DoA(3); + a.DoA(1); + a.DoA(2); + a.DoA(4); +} + +// .After() accepts up to 5 arguments. +TEST(AfterTest, AcceptsUpToFiveArguments) { + MockA a; + Expectation e1 = EXPECT_CALL(a, DoA(1)); + Expectation e2 = EXPECT_CALL(a, DoA(2)); + Expectation e3 = EXPECT_CALL(a, DoA(3)); + ExpectationSet es1 = EXPECT_CALL(a, DoA(4)); + ExpectationSet es2 = EXPECT_CALL(a, DoA(5)); + EXPECT_CALL(a, DoA(6)) + .After(e1, e2, e3, es1, es2); + + a.DoA(5); + a.DoA(2); + a.DoA(4); + a.DoA(1); + a.DoA(3); + a.DoA(6); +} + +// .After() allows input to contain duplicated Expectations. +TEST(AfterTest, AcceptsDuplicatedInput) { + MockA a; + Expectation e1 = EXPECT_CALL(a, DoA(1)); + Expectation e2 = EXPECT_CALL(a, DoA(2)); + ExpectationSet es; + es += e1; + es += e2; + EXPECT_CALL(a, ReturnResult(3)) + .After(e1, e2, es, e1) + .WillOnce(Return(Result())); + + a.DoA(1); + EXPECT_DEATH_IF_SUPPORTED(a.ReturnResult(3), ""); + + a.DoA(2); + a.ReturnResult(3); +} + +// An Expectation added to an ExpectationSet after it has been used in +// an .After() has no effect. +TEST(AfterTest, ChangesToExpectationSetHaveNoEffectAfterwards) { + MockA a; + ExpectationSet es1 = EXPECT_CALL(a, DoA(1)); + Expectation e2 = EXPECT_CALL(a, DoA(2)); + EXPECT_CALL(a, DoA(3)) + .After(es1); + es1 += e2; + + a.DoA(1); + a.DoA(3); + a.DoA(2); +} + // Tests that Google Mock correctly handles calls to mock functions // after a mock object owning one of their pre-requisites has died. @@ -1628,7 +1934,16 @@ class LogTestHelper { class GMockLogTest : public ::testing::Test { protected: - virtual void SetUp() { original_verbose_ = GMOCK_FLAG(verbose); } + virtual void SetUp() { + // The code needs to work when both ::string and ::std::string are + // defined and the flag is implemented as a + // testing::internal::String. In this case, without the call to + // c_str(), the compiler will complain that it cannot figure out + // whether the String flag should be converted to a ::string or an + // ::std::string before being assigned to original_verbose_. + original_verbose_ = GMOCK_FLAG(verbose).c_str(); + } + virtual void TearDown() { GMOCK_FLAG(verbose) = original_verbose_; } LogTestHelper helper_; @@ -2000,7 +2315,14 @@ void Helper(MockC* c) { } // namespace +// Allows the user to define his own main and then invoke gmock_main +// from it. This might be necessary on some platforms which require +// specific setup and teardown. +#if GMOCK_RENAME_MAIN +int gmock_main(int argc, char **argv) { +#else int main(int argc, char **argv) { +#endif // GMOCK_RENAME_MAIN testing::InitGoogleMock(&argc, argv); // Ensures that the tests pass no matter what value of diff --git a/testing/gmock/test/gmock_leak_test.py b/testing/gmock/test/gmock_leak_test.py index 1337e0b..1337e0b 100755..100644 --- a/testing/gmock/test/gmock_leak_test.py +++ b/testing/gmock/test/gmock_leak_test.py diff --git a/testing/gmock/test/gmock_link_test.h b/testing/gmock/test/gmock_link_test.h index b903f3c..40554bf 100644 --- a/testing/gmock/test/gmock_link_test.h +++ b/testing/gmock/test/gmock_link_test.h @@ -116,7 +116,7 @@ #include <gmock/gmock.h> -#ifndef _WIN32_WCE +#if !GTEST_OS_WINDOWS_MOBILE #include <errno.h> #endif @@ -147,6 +147,7 @@ using testing::IgnoreResult; using testing::Invoke; using testing::InvokeArgument; using testing::InvokeWithoutArgs; +using testing::IsNull; using testing::Le; using testing::Lt; using testing::Matcher; @@ -176,18 +177,18 @@ using testing::WithArg; using testing::WithArgs; using testing::WithoutArgs; -#ifndef _WIN32_WCE +#if !GTEST_OS_WINDOWS_MOBILE using testing::SetErrnoAndReturn; -#endif // _WIN32_WCE +#endif #if GTEST_HAS_EXCEPTIONS using testing::Throw; -#endif // GTEST_HAS_EXCEPTIONS +#endif #if GMOCK_HAS_REGEX using testing::ContainsRegex; using testing::MatchesRegex; -#endif // GMOCK_HAS_REGEX +#endif class Interface { public: @@ -297,7 +298,7 @@ TEST(LinkTest, TestSetArrayArgument) { mock.VoidFromString(&ch); } -#ifndef _WIN32_WCE +#if !GTEST_OS_WINDOWS_MOBILE // Tests the linkage of the SetErrnoAndReturn action. TEST(LinkTest, TestSetErrnoAndReturn) { @@ -309,7 +310,7 @@ TEST(LinkTest, TestSetErrnoAndReturn) { errno = saved_errno; } -#endif // _WIN32_WCE +#endif // !GTEST_OS_WINDOWS_MOBILE // Tests the linkage of the Invoke(function) and Invoke(object, method) actions. TEST(LinkTest, TestInvoke) { @@ -491,6 +492,13 @@ TEST(LinkTest, TestMatcherNotNull) { ON_CALL(mock, VoidFromString(NotNull())).WillByDefault(Return()); } +// Tests the linkage of the IsNull matcher. +TEST(LinkTest, TestMatcherIsNull) { + Mock mock; + + ON_CALL(mock, VoidFromString(IsNull())).WillByDefault(Return()); +} + // Tests the linkage of the Ref matcher. TEST(LinkTest, TestMatcherRef) { Mock mock; diff --git a/testing/gmock/test/gmock_output_test.py b/testing/gmock/test/gmock_output_test.py index f43f707..f43f707 100755..100644 --- a/testing/gmock/test/gmock_output_test.py +++ b/testing/gmock/test/gmock_output_test.py diff --git a/testing/gmock/test/gmock_test_utils.py b/testing/gmock/test/gmock_test_utils.py index 4c09e39..2fda138 100755..100644 --- a/testing/gmock/test/gmock_test_utils.py +++ b/testing/gmock/test/gmock_test_utils.py @@ -1,4 +1,4 @@ -#!/usr/bin/python2.4 +#!/usr/bin/env python # # Copyright 2006, Google Inc. # All rights reserved. |