diff options
author | ajwong@chromium.org <ajwong@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-06-22 18:32:21 +0000 |
---|---|---|
committer | ajwong@chromium.org <ajwong@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-06-22 18:32:21 +0000 |
commit | 22015c33fe44a1cc36596e0b6de37a48dc13b131 (patch) | |
tree | 643966e3ad01ab48b4f1d0ab48793261c68cee02 /testing/gmock/test | |
parent | ad764d54ec391e95e4ea239aa8eed548ce45cb61 (diff) | |
download | chromium_src-22015c33fe44a1cc36596e0b6de37a48dc13b131.zip chromium_src-22015c33fe44a1cc36596e0b6de37a48dc13b131.tar.gz chromium_src-22015c33fe44a1cc36596e0b6de37a48dc13b131.tar.bz2 |
Upgrade gtest to r267 and gmock to r173.
This is step1 into removing the boost + tr1 dependency in windows. It also includes a hack to avoid brining in tr1/functional on gcc, which will move us closer to enabling -fno-rtti.
This CL has passed the try servers. I've also tried compiling gmock, gmock_main, base, base_unittests, and webcore modules in vs2008 express editions.
Review URL: http://codereview.chromium.org/140003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@18923 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'testing/gmock/test')
-rw-r--r-- | testing/gmock/test/gmock-generated-actions_test.cc | 96 | ||||
-rw-r--r-- | testing/gmock/test/gmock-generated-matchers_test.cc | 228 | ||||
-rw-r--r-- | testing/gmock/test/gmock-internal-utils_test.cc | 261 | ||||
-rw-r--r-- | testing/gmock/test/gmock-matchers_test.cc | 123 | ||||
-rw-r--r-- | testing/gmock/test/gmock-printers_test.cc | 99 | ||||
-rw-r--r-- | testing/gmock/test/gmock-spec-builders_test.cc | 86 | ||||
-rw-r--r-- | testing/gmock/test/gmock_link_test.h | 1 | ||||
-rw-r--r-- | testing/gmock/test/gmock_output_test_.cc | 12 | ||||
-rw-r--r-- | testing/gmock/test/gmock_output_test_golden.txt | 20 |
9 files changed, 807 insertions, 119 deletions
diff --git a/testing/gmock/test/gmock-generated-actions_test.cc b/testing/gmock/test/gmock-generated-actions_test.cc index 84e5a41..d0b2ddc 100644 --- a/testing/gmock/test/gmock-generated-actions_test.cc +++ b/testing/gmock/test/gmock-generated-actions_test.cc @@ -269,13 +269,19 @@ TEST(InvokeTest, FunctionThatTakes6Arguments) { 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("1", "2", "3", "4", "5", "6", "7"))); + 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. @@ -284,7 +290,9 @@ TEST(InvokeTest, FunctionThatTakes8Arguments) { const char*, const char*, const char*, const char*)> a = Invoke(Concat8); EXPECT_EQ("12345678", - a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7", "8"))); + 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. @@ -293,7 +301,9 @@ TEST(InvokeTest, FunctionThatTakes9Arguments) { const char*, const char*, const char*, const char*, const char*)> a = Invoke(Concat9); EXPECT_EQ("123456789", - a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7", "8", "9"))); + 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. @@ -301,15 +311,18 @@ 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("1", "2", "3", "4", "5", "6", - "7", "8", "9", "0"))); + 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, "hi"))); + EXPECT_EQ(12, a1.Perform(make_tuple(10, 2, 5.6, CharPtr("hi")))); Action<int(int, int, bool, int*)> a2 = Invoke(SumOfFirst2); @@ -321,7 +334,7 @@ TEST(InvokeTest, MethodWithUnusedParameters) { Foo foo; Action<int(string, bool, int, int)> a1 = Invoke(&foo, &Foo::SumOfLast2); - EXPECT_EQ(12, a1.Perform(make_tuple("hi", true, 10, 2))); + EXPECT_EQ(12, a1.Perform(make_tuple(CharPtr("hi"), true, 10, 2))); Action<int(char, double, int, int)> a2 = Invoke(&foo, &Foo::SumOfLast2); @@ -400,7 +413,9 @@ TEST(InvokeMethodTest, MethodThatTakes7Arguments) { const char*, const char*, const char*)> a = Invoke(&foo, &Foo::Concat7); EXPECT_EQ("1234567", - a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7"))); + 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. @@ -410,7 +425,9 @@ TEST(InvokeMethodTest, MethodThatTakes8Arguments) { const char*, const char*, const char*, const char*)> a = Invoke(&foo, &Foo::Concat8); EXPECT_EQ("12345678", - a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7", "8"))); + 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. @@ -420,7 +437,9 @@ TEST(InvokeMethodTest, MethodThatTakes9Arguments) { const char*, const char*, const char*, const char*, const char*)> a = Invoke(&foo, &Foo::Concat9); EXPECT_EQ("123456789", - a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7", "8", "9"))); + 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. @@ -429,8 +448,11 @@ TEST(InvokeMethodTest, MethodThatTakes10Arguments) { 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("1", "2", "3", "4", "5", "6", - "7", "8", "9", "0"))); + 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. @@ -665,7 +687,7 @@ TEST(WithArgsTest, TwoArgs) { Action<const char*(const char* s, double x, int n)> a = WithArgs<0, 2>(Invoke(Binary)); const char s[] = "Hello"; - EXPECT_EQ(s + 2, a.Perform(make_tuple(s, 0.5, 2))); + EXPECT_EQ(s + 2, a.Perform(make_tuple(CharPtr(s), 0.5, 2))); } // Tests using WithArgs with an action that takes 3 arguments. @@ -679,7 +701,8 @@ TEST(WithArgsTest, ThreeArgs) { TEST(WithArgsTest, FourArgs) { Action<string(const char*, const char*, double, const char*, const char*)> a = WithArgs<4, 3, 1, 0>(Invoke(Concat4)); - EXPECT_EQ("4310", a.Perform(make_tuple("0", "1", 2.5, "3", "4"))); + EXPECT_EQ("4310", a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), 2.5, + CharPtr("3"), CharPtr("4")))); } // Tests using WithArgs with an action that takes 5 arguments. @@ -687,42 +710,53 @@ TEST(WithArgsTest, FiveArgs) { Action<string(const char*, const char*, const char*, const char*, const char*)> a = WithArgs<4, 3, 2, 1, 0>(Invoke(Concat5)); - EXPECT_EQ("43210", a.Perform(make_tuple("0", "1", "2", "3", "4"))); + EXPECT_EQ("43210", + a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"), + CharPtr("3"), CharPtr("4")))); } // Tests using WithArgs with an action that takes 6 arguments. TEST(WithArgsTest, SixArgs) { Action<string(const char*, const char*, const char*)> a = WithArgs<0, 1, 2, 2, 1, 0>(Invoke(Concat6)); - EXPECT_EQ("012210", a.Perform(make_tuple("0", "1", "2"))); + EXPECT_EQ("012210", + a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2")))); } // Tests using WithArgs with an action that takes 7 arguments. TEST(WithArgsTest, SevenArgs) { Action<string(const char*, const char*, const char*, const char*)> a = WithArgs<0, 1, 2, 3, 2, 1, 0>(Invoke(Concat7)); - EXPECT_EQ("0123210", a.Perform(make_tuple("0", "1", "2", "3"))); + EXPECT_EQ("0123210", + a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"), + CharPtr("3")))); } // Tests using WithArgs with an action that takes 8 arguments. TEST(WithArgsTest, EightArgs) { Action<string(const char*, const char*, const char*, const char*)> a = WithArgs<0, 1, 2, 3, 0, 1, 2, 3>(Invoke(Concat8)); - EXPECT_EQ("01230123", a.Perform(make_tuple("0", "1", "2", "3"))); + EXPECT_EQ("01230123", + a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"), + CharPtr("3")))); } // Tests using WithArgs with an action that takes 9 arguments. TEST(WithArgsTest, NineArgs) { Action<string(const char*, const char*, const char*, const char*)> a = WithArgs<0, 1, 2, 3, 1, 2, 3, 2, 3>(Invoke(Concat9)); - EXPECT_EQ("012312323", a.Perform(make_tuple("0", "1", "2", "3"))); + EXPECT_EQ("012312323", + a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"), + CharPtr("3")))); } // Tests using WithArgs with an action that takes 10 arguments. TEST(WithArgsTest, TenArgs) { Action<string(const char*, const char*, const char*, const char*)> a = WithArgs<0, 1, 2, 3, 2, 1, 0, 1, 2, 3>(Invoke(Concat10)); - EXPECT_EQ("0123210123", a.Perform(make_tuple("0", "1", "2", "3"))); + EXPECT_EQ("0123210123", + a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"), + CharPtr("3")))); } // Tests using WithArgs with an action that is not Invoke(). @@ -736,7 +770,7 @@ class SubstractAction : public ActionInterface<int(int, int)> { // NOLINT TEST(WithArgsTest, NonInvokeAction) { Action<int(const string&, int, int)> a = // NOLINT WithArgs<2, 1>(MakeAction(new SubstractAction)); - EXPECT_EQ(8, a.Perform(make_tuple("hi", 2, 10))); + EXPECT_EQ(8, a.Perform(make_tuple(CharPtr("hi"), 2, 10))); } // Tests using WithArgs to pass all original arguments in the original order. @@ -758,7 +792,7 @@ TEST(WithArgsTest, ReversedArgumentOrder) { Action<const char*(short n, const char* input)> a = // NOLINT WithArgs<1, 0>(Invoke(Binary)); const char s[] = "Hello"; - EXPECT_EQ(s + 2, a.Perform(make_tuple(2, s))); + EXPECT_EQ(s + 2, a.Perform(make_tuple(2, CharPtr(s)))); } // Tests using WithArgs with compatible, but not identical, argument types. @@ -1123,16 +1157,16 @@ TEST(ActionMacroTest, CanDefineOverloadedActions) { typedef Action<const char*(bool, const char*)> MyAction; const MyAction a1 = OverloadedAction(); - EXPECT_STREQ("hello", a1.Perform(make_tuple(false, "world"))); - EXPECT_STREQ("world", a1.Perform(make_tuple(true, "world"))); + EXPECT_STREQ("hello", a1.Perform(make_tuple(false, CharPtr("world")))); + EXPECT_STREQ("world", a1.Perform(make_tuple(true, CharPtr("world")))); const MyAction a2 = OverloadedAction("hi"); - EXPECT_STREQ("hi", a2.Perform(make_tuple(false, "world"))); - EXPECT_STREQ("world", a2.Perform(make_tuple(true, "world"))); + EXPECT_STREQ("hi", a2.Perform(make_tuple(false, CharPtr("world")))); + EXPECT_STREQ("world", a2.Perform(make_tuple(true, CharPtr("world")))); const MyAction a3 = OverloadedAction("hi", "you"); - EXPECT_STREQ("hi", a3.Perform(make_tuple(true, "world"))); - EXPECT_STREQ("you", a3.Perform(make_tuple(false, "world"))); + EXPECT_STREQ("hi", a3.Perform(make_tuple(true, CharPtr("world")))); + EXPECT_STREQ("you", a3.Perform(make_tuple(false, CharPtr("world")))); } // Tests ACTION_Pn where n >= 3. @@ -1224,8 +1258,8 @@ TEST(ActionPnMacroTest, SimpleTypePromotion) { PadArgument(std::string("foo"), 'r'); Action<std::string(const char*)> promo = PadArgument("foo", static_cast<int>('r')); - EXPECT_EQ("foobar", no_promo.Perform(make_tuple("ba"))); - EXPECT_EQ("foobar", promo.Perform(make_tuple("ba"))); + EXPECT_EQ("foobar", no_promo.Perform(make_tuple(CharPtr("ba")))); + EXPECT_EQ("foobar", promo.Perform(make_tuple(CharPtr("ba")))); } // Tests that we can partially restrict parameter types using a @@ -1470,7 +1504,7 @@ TEST(DeleteArgActionTest, TenArgs) { 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, "hi", false, 7, 8, 9, 10, t)); + a1.Perform(make_tuple(true, 5, 6, CharPtr("hi"), false, 7, 8, 9, 10, t)); EXPECT_TRUE(is_deleted); } diff --git a/testing/gmock/test/gmock-generated-matchers_test.cc b/testing/gmock/test/gmock-generated-matchers_test.cc index 669652b..19024d0 100644 --- a/testing/gmock/test/gmock-generated-matchers_test.cc +++ b/testing/gmock/test/gmock-generated-matchers_test.cc @@ -53,13 +53,18 @@ using std::pair; using std::set; using std::stringstream; using std::vector; +using std::tr1::get; +using std::tr1::make_tuple; +using std::tr1::tuple; using testing::_; +using testing::Args; using testing::Contains; using testing::ElementsAre; using testing::ElementsAreArray; using testing::Eq; using testing::Ge; using testing::Gt; +using testing::Lt; using testing::MakeMatcher; using testing::Matcher; using testing::MatcherInterface; @@ -69,6 +74,7 @@ using testing::Pointee; using testing::Ref; using testing::StaticAssertTypeEq; using testing::StrEq; +using testing::Value; using testing::internal::string; // Returns the description of the given matcher. @@ -95,6 +101,107 @@ string Explain(const MatcherType& m, const Value& x) { return ss.str(); } +// Tests Args<k0, ..., kn>(m). + +TEST(ArgsTest, AcceptsZeroTemplateArg) { + const tuple<int, bool> t(5, true); + EXPECT_THAT(t, Args<>(Eq(tuple<>()))); + EXPECT_THAT(t, Not(Args<>(Ne(tuple<>())))); +} + +TEST(ArgsTest, AcceptsOneTemplateArg) { + const tuple<int, bool> t(5, true); + EXPECT_THAT(t, Args<0>(Eq(make_tuple(5)))); + EXPECT_THAT(t, Args<1>(Eq(make_tuple(true)))); + EXPECT_THAT(t, Not(Args<1>(Eq(make_tuple(false))))); +} + +TEST(ArgsTest, AcceptsTwoTemplateArgs) { + const tuple<short, int, long> t(4, 5, 6L); // NOLINT + + EXPECT_THAT(t, (Args<0, 1>(Lt()))); + EXPECT_THAT(t, (Args<1, 2>(Lt()))); + EXPECT_THAT(t, Not(Args<0, 2>(Gt()))); +} + +TEST(ArgsTest, AcceptsRepeatedTemplateArgs) { + const tuple<short, int, long> t(4, 5, 6L); // NOLINT + EXPECT_THAT(t, (Args<0, 0>(Eq()))); + EXPECT_THAT(t, Not(Args<1, 1>(Ne()))); +} + +TEST(ArgsTest, AcceptsDecreasingTemplateArgs) { + const tuple<short, int, long> t(4, 5, 6L); // NOLINT + EXPECT_THAT(t, (Args<2, 0>(Gt()))); + EXPECT_THAT(t, Not(Args<2, 1>(Lt()))); +} + +MATCHER(SumIsZero, "") { + return get<0>(arg) + get<1>(arg) + get<2>(arg) == 0; +} + +TEST(ArgsTest, AcceptsMoreTemplateArgsThanArityOfOriginalTuple) { + EXPECT_THAT(make_tuple(-1, 2), (Args<0, 0, 1>(SumIsZero()))); + EXPECT_THAT(make_tuple(1, 2), Not(Args<0, 0, 1>(SumIsZero()))); +} + +TEST(ArgsTest, CanBeNested) { + const tuple<short, int, long, int> t(4, 5, 6L, 6); // NOLINT + EXPECT_THAT(t, (Args<1, 2, 3>(Args<1, 2>(Eq())))); + EXPECT_THAT(t, (Args<0, 1, 3>(Args<0, 2>(Lt())))); +} + +TEST(ArgsTest, CanMatchTupleByValue) { + typedef tuple<char, int, int> Tuple3; + const Matcher<Tuple3> m = Args<1, 2>(Lt()); + EXPECT_TRUE(m.Matches(Tuple3('a', 1, 2))); + EXPECT_FALSE(m.Matches(Tuple3('b', 2, 2))); +} + +TEST(ArgsTest, CanMatchTupleByReference) { + typedef tuple<char, char, int> Tuple3; + const Matcher<const Tuple3&> m = Args<0, 1>(Lt()); + EXPECT_TRUE(m.Matches(Tuple3('a', 'b', 2))); + EXPECT_FALSE(m.Matches(Tuple3('b', 'b', 2))); +} + +// Validates that arg is printed as str. +MATCHER_P(PrintsAs, str, "") { + typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(arg_type)) RawTuple; + return + testing::internal::UniversalPrinter<RawTuple>::PrintToString(arg) == str; +} + +TEST(ArgsTest, AcceptsTenTemplateArgs) { + EXPECT_THAT(make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9), + (Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>( + PrintsAs("(9, 8, 7, 6, 5, 4, 3, 2, 1, 0)")))); + EXPECT_THAT(make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9), + Not(Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>( + PrintsAs("(0, 8, 7, 6, 5, 4, 3, 2, 1, 0)")))); +} + +TEST(ArgsTest, DescirbesSelfCorrectly) { + const Matcher<tuple<int, bool, char> > m = Args<2, 0>(Lt()); + EXPECT_EQ("are a tuple whose fields (#2, #0) are a pair (x, y) where x < y", + Describe(m)); +} + +TEST(ArgsTest, DescirbesNestedArgsCorrectly) { + const Matcher<const tuple<int, bool, char, int>&> m = + Args<0, 2, 3>(Args<2, 0>(Lt())); + EXPECT_EQ("are a tuple whose fields (#0, #2, #3) are a tuple " + "whose fields (#2, #0) are a pair (x, y) where x < y", + Describe(m)); +} + +TEST(ArgsTest, DescribesNegationCorrectly) { + const Matcher<tuple<int, char> > m = Args<1, 0>(Gt()); + EXPECT_EQ("are a tuple whose fields (#1, #0) are a pair (x, y) " + "where x > y is false", + DescribeNegation(m)); +} + // For testing ExplainMatchResultTo(). class GreaterThanMatcher : public MatcherInterface<int> { public: @@ -330,6 +437,39 @@ TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) { EXPECT_THAT(&v, Not(Pointee(ElementsAre(0, _, 3)))); } +TEST(ElementsAreTest, WorksWithNativeArrayPassedByReference) { + int array[] = { 0, 1, 2 }; + EXPECT_THAT(array, ElementsAre(0, 1, _)); + EXPECT_THAT(array, Not(ElementsAre(1, _, _))); + EXPECT_THAT(array, Not(ElementsAre(0, _))); +} + +class NativeArrayPassedAsPointerAndSize { + public: + MOCK_METHOD2(Helper, void(int* array, int size)); +}; + +TEST(ElementsAreTest, WorksWithNativeArrayPassedAsPointerAndSize) { + int array[] = { 0, 1 }; + ::std::tr1::tuple<int*, size_t> array_as_tuple(array, 2); + EXPECT_THAT(array_as_tuple, ElementsAre(0, 1)); + EXPECT_THAT(array_as_tuple, Not(ElementsAre(0))); + + NativeArrayPassedAsPointerAndSize helper; + EXPECT_CALL(helper, Helper(_, _)) + .With(ElementsAre(0, 1)); + helper.Helper(array, 2); +} + +TEST(ElementsAreTest, WorksWithTwoDimensionalNativeArray) { + const char a2[][3] = { "hi", "lo" }; + EXPECT_THAT(a2, ElementsAre(ElementsAre('h', 'i', '\0'), + ElementsAre('l', 'o', '\0'))); + EXPECT_THAT(a2, ElementsAre(StrEq("hi"), StrEq("lo"))); + EXPECT_THAT(a2, ElementsAre(Not(ElementsAre('h', 'o', '\0')), + ElementsAre('l', 'o', '\0'))); +} + // Tests for ElementsAreArray(). Since ElementsAreArray() shares most // of the implementation with ElementsAre(), we don't test it as // thoroughly here. @@ -379,6 +519,17 @@ TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) { EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray))); } +// Since ElementsAre() and ElementsAreArray() share much of the +// implementation, we only do a sanity test for native arrays here. +TEST(ElementsAreArrayTest, WorksWithNativeArray) { + ::std::string a[] = { "hi", "ho" }; + ::std::string b[] = { "hi", "ho" }; + + EXPECT_THAT(a, ElementsAreArray(b)); + EXPECT_THAT(a, ElementsAreArray(b, 2)); + EXPECT_THAT(a, Not(ElementsAreArray(b, 1))); +} + // Tests for the MATCHER*() macro family. // Tests that a simple MATCHER() definition works. @@ -443,12 +594,23 @@ namespace matcher_test { MATCHER(IsOdd, "") { return (arg % 2) != 0; } } // namespace matcher_test -TEST(MatcherTest, WorksInNamespace) { +TEST(MatcherMacroTest, WorksInNamespace) { Matcher<int> m = matcher_test::IsOdd(); EXPECT_FALSE(m.Matches(4)); EXPECT_TRUE(m.Matches(5)); } +// Tests that Value() can be used to compose matchers. +MATCHER(IsPositiveOdd, "") { + return Value(arg, matcher_test::IsOdd()) && arg > 0; +} + +TEST(MatcherMacroTest, CanBeComposedUsingValue) { + EXPECT_THAT(3, IsPositiveOdd()); + EXPECT_THAT(4, Not(IsPositiveOdd())); + EXPECT_THAT(-1, Not(IsPositiveOdd())); +} + // Tests that a simple MATCHER_P() definition works. MATCHER_P(IsGreaterThan32And, n, "") { return arg > 32 && arg > n; } @@ -742,14 +904,31 @@ TEST(MatcherPnMacroTest, TypesAreCorrect) { EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, 9, '0'); } +// Tests that matcher-typed parameters can be used in Value() inside a +// MATCHER_Pn definition. + +// Succeeds if arg matches exactly 2 of the 3 matchers. +MATCHER_P3(TwoOf, m1, m2, m3, "") { + const int count = static_cast<int>(Value(arg, m1)) + + static_cast<int>(Value(arg, m2)) + static_cast<int>(Value(arg, m3)); + return count == 2; +} + +TEST(MatcherPnMacroTest, CanUseMatcherTypedParameterInValue) { + EXPECT_THAT(42, TwoOf(Gt(0), Lt(50), Eq(10))); + EXPECT_THAT(0, Not(TwoOf(Gt(-1), Lt(1), Eq(0)))); +} + +// Tests Contains(). + TEST(ContainsTest, ListMatchesWhenElementIsInContainer) { list<int> some_list; some_list.push_back(3); some_list.push_back(1); some_list.push_back(2); EXPECT_THAT(some_list, Contains(1)); - EXPECT_THAT(some_list, Contains(3.0)); - EXPECT_THAT(some_list, Contains(2.0f)); + EXPECT_THAT(some_list, Contains(Gt(2.5))); + EXPECT_THAT(some_list, Contains(Eq(2.0f))); list<string> another_list; another_list.push_back("fee"); @@ -771,8 +950,8 @@ TEST(ContainsTest, SetMatchesWhenElementIsInContainer) { some_set.insert(3); some_set.insert(1); some_set.insert(2); - EXPECT_THAT(some_set, Contains(1.0)); - EXPECT_THAT(some_set, Contains(3.0f)); + EXPECT_THAT(some_set, Contains(Eq(1.0))); + EXPECT_THAT(some_set, Contains(Eq(3.0f))); EXPECT_THAT(some_set, Contains(2)); set<const char*> another_set; @@ -780,7 +959,7 @@ TEST(ContainsTest, SetMatchesWhenElementIsInContainer) { another_set.insert("fie"); another_set.insert("foe"); another_set.insert("fum"); - EXPECT_THAT(another_set, Contains(string("fum"))); + EXPECT_THAT(another_set, Contains(Eq(string("fum")))); } TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) { @@ -795,8 +974,20 @@ TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) { } TEST(ContainsTest, DescribesItselfCorrectly) { + const int a[2] = { 1, 2 }; + Matcher<const int(&)[2]> m = Contains(2); + EXPECT_EQ("element 1 matches", Explain(m, a)); + + m = Contains(3); + EXPECT_EQ("", Explain(m, a)); +} + +TEST(ContainsTest, ExplainsMatchResultCorrectly) { Matcher<vector<int> > m = Contains(1); - EXPECT_EQ("contains 1", Describe(m)); + EXPECT_EQ("contains at least one element that is equal to 1", Describe(m)); + + Matcher<vector<int> > m2 = Not(m); + EXPECT_EQ("doesn't contain any element that is equal to 1", Describe(m2)); } TEST(ContainsTest, MapMatchesWhenElementIsInContainer) { @@ -823,7 +1014,7 @@ TEST(ContainsTest, MapDoesNotMatchWhenElementIsNotInContainer) { TEST(ContainsTest, ArrayMatchesWhenElementIsInContainer) { const char* string_array[] = { "fee", "fie", "foe", "fum" }; - EXPECT_THAT(string_array, Contains(string("fum"))); + EXPECT_THAT(string_array, Contains(Eq(string("fum")))); } TEST(ContainsTest, ArrayDoesNotMatchWhenElementIsNotInContainer) { @@ -831,4 +1022,25 @@ TEST(ContainsTest, ArrayDoesNotMatchWhenElementIsNotInContainer) { EXPECT_THAT(int_array, Not(Contains(5))); } +TEST(ContainsTest, AcceptsMatcher) { + const int a[] = { 1, 2, 3 }; + EXPECT_THAT(a, Contains(Gt(2))); + EXPECT_THAT(a, Not(Contains(Gt(4)))); +} + +TEST(ContainsTest, WorksForNativeArrayAsTuple) { + const int a[] = { 1, 2 }; + const int* const pointer = a; + EXPECT_THAT(make_tuple(pointer, 2), Contains(1)); + EXPECT_THAT(make_tuple(pointer, 2), Not(Contains(Gt(3)))); +} + +TEST(ContainsTest, WorksForTwoDimensionalNativeArray) { + int a[][3] = { { 1, 2, 3 }, { 4, 5, 6 } }; + EXPECT_THAT(a, Contains(ElementsAre(4, 5, 6))); + EXPECT_THAT(a, Contains(Contains(5))); + EXPECT_THAT(a, Not(Contains(ElementsAre(3, 4, 5)))); + EXPECT_THAT(a, Contains(Not(Contains(5)))); +} + } // namespace diff --git a/testing/gmock/test/gmock-internal-utils_test.cc b/testing/gmock/test/gmock-internal-utils_test.cc index 5e4dc03..9ab15af 100644 --- a/testing/gmock/test/gmock-internal-utils_test.cc +++ b/testing/gmock/test/gmock-internal-utils_test.cc @@ -53,6 +53,7 @@ namespace internal { namespace { +using ::std::tr1::make_tuple; using ::std::tr1::tuple; TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsNoWord) { @@ -130,6 +131,8 @@ TEST(RemoveConstTest, DoesNotAffectNonConstType) { // Tests that RemoveConst removes const from const types. TEST(RemoveConstTest, RemovesConst) { CompileAssertTypesEqual<int, RemoveConst<const int>::type>(); + CompileAssertTypesEqual<char[2], RemoveConst<const char[2]>::type>(); + CompileAssertTypesEqual<char[2][3], RemoveConst<const char[2][3]>::type>(); } // Tests GMOCK_REMOVE_CONST_. @@ -494,6 +497,34 @@ TEST(ExpectTest, FailsNonfatallyOnFalse) { }, "Expectation failed"); } +// Tests LogIsVisible(). + +class LogIsVisibleTest : public ::testing::Test { + protected: + virtual void SetUp() { original_verbose_ = GMOCK_FLAG(verbose); } + virtual void TearDown() { GMOCK_FLAG(verbose) = original_verbose_; } + + string original_verbose_; +}; + +TEST_F(LogIsVisibleTest, AlwaysReturnsTrueIfVerbosityIsInfo) { + GMOCK_FLAG(verbose) = kInfoVerbosity; + EXPECT_TRUE(LogIsVisible(INFO)); + EXPECT_TRUE(LogIsVisible(WARNING)); +} + +TEST_F(LogIsVisibleTest, AlwaysReturnsFalseIfVerbosityIsError) { + GMOCK_FLAG(verbose) = kErrorVerbosity; + EXPECT_FALSE(LogIsVisible(INFO)); + EXPECT_FALSE(LogIsVisible(WARNING)); +} + +TEST_F(LogIsVisibleTest, WorksWhenVerbosityIsWarning) { + GMOCK_FLAG(verbose) = kWarningVerbosity; + EXPECT_FALSE(LogIsVisible(INFO)); + EXPECT_TRUE(LogIsVisible(WARNING)); +} + // TODO(wan@google.com): find a way to re-enable these tests. #if 0 @@ -693,6 +724,236 @@ TEST(OnCallTest, LogsAnythingArgument) { #endif // 0 +// Tests ArrayEq(). + +TEST(ArrayEqTest, WorksForDegeneratedArrays) { + EXPECT_TRUE(ArrayEq(5, 5L)); + EXPECT_FALSE(ArrayEq('a', 0)); +} + +TEST(ArrayEqTest, WorksForOneDimensionalArrays) { + const int a[] = { 0, 1 }; + long b[] = { 0, 1 }; + EXPECT_TRUE(ArrayEq(a, b)); + EXPECT_TRUE(ArrayEq(a, 2, b)); + + b[0] = 2; + EXPECT_FALSE(ArrayEq(a, b)); + EXPECT_FALSE(ArrayEq(a, 1, b)); +} + +TEST(ArrayEqTest, WorksForTwoDimensionalArrays) { + const char a[][3] = { "hi", "lo" }; + const char b[][3] = { "hi", "lo" }; + const char c[][3] = { "hi", "li" }; + + EXPECT_TRUE(ArrayEq(a, b)); + EXPECT_TRUE(ArrayEq(a, 2, b)); + + EXPECT_FALSE(ArrayEq(a, c)); + EXPECT_FALSE(ArrayEq(a, 2, c)); +} + +// Tests ArrayAwareFind(). + +TEST(ArrayAwareFindTest, WorksForOneDimensionalArray) { + const char a[] = "hello"; + EXPECT_EQ(a + 4, ArrayAwareFind(a, a + 5, 'o')); + EXPECT_EQ(a + 5, ArrayAwareFind(a, a + 5, 'x')); +} + +TEST(ArrayAwareFindTest, WorksForTwoDimensionalArray) { + int a[][2] = { { 0, 1 }, { 2, 3 }, { 4, 5 } }; + const int b[2] = { 2, 3 }; + EXPECT_EQ(a + 1, ArrayAwareFind(a, a + 3, b)); + + const int c[2] = { 6, 7 }; + EXPECT_EQ(a + 3, ArrayAwareFind(a, a + 3, c)); +} + +// Tests CopyArray(). + +TEST(CopyArrayTest, WorksForDegeneratedArrays) { + int n = 0; + CopyArray('a', &n); + EXPECT_EQ('a', n); +} + +TEST(CopyArrayTest, WorksForOneDimensionalArrays) { + const char a[3] = "hi"; + int b[3]; + CopyArray(a, &b); + EXPECT_TRUE(ArrayEq(a, b)); + + int c[3]; + CopyArray(a, 3, c); + EXPECT_TRUE(ArrayEq(a, c)); +} + +TEST(CopyArrayTest, WorksForTwoDimensionalArrays) { + const int a[2][3] = { { 0, 1, 2 }, { 3, 4, 5 } }; + int b[2][3]; + CopyArray(a, &b); + EXPECT_TRUE(ArrayEq(a, b)); + + int c[2][3]; + CopyArray(a, 2, c); + EXPECT_TRUE(ArrayEq(a, c)); +} + +// Tests NativeArray. + +TEST(NativeArrayTest, ConstructorFromArrayReferenceWorks) { + const int a[3] = { 0, 1, 2 }; + NativeArray<int> na(a, 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); + EXPECT_NE(*a, na.begin()); + delete[] a; + EXPECT_EQ(0, na.begin()[0]); + EXPECT_EQ(1, na.begin()[1]); + + // We rely on the heap checker to verify that na deletes the copy of + // array. +} + +TEST(NativeArrayTest, TypeMembersAreCorrect) { + StaticAssertTypeEq<char, NativeArray<char>::value_type>(); + StaticAssertTypeEq<int[2], NativeArray<int[2]>::value_type>(); + + StaticAssertTypeEq<const char*, NativeArray<char>::const_iterator>(); + StaticAssertTypeEq<const bool(*)[2], NativeArray<bool[2]>::const_iterator>(); +} + +TEST(NativeArrayTest, MethodsWork) { + const int a[] = { 0, 1, 2 }; + NativeArray<int> na(a, kCopy); + ASSERT_EQ(3, na.size()); + EXPECT_EQ(3, na.end() - na.begin()); + + NativeArray<int>::const_iterator it = na.begin(); + EXPECT_EQ(0, *it); + ++it; + EXPECT_EQ(1, *it); + it++; + EXPECT_EQ(2, *it); + ++it; + EXPECT_EQ(na.end(), it); + + EXPECT_THAT(na, Eq(na)); + + NativeArray<int> na2(a, 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)))); +} + +TEST(NativeArrayTest, WorksForTwoDimensionalArray) { + const char a[2][3] = { "hi", "lo" }; + NativeArray<char[3]> na(a, kReference); + ASSERT_EQ(2, na.size()); + EXPECT_EQ(a, na.begin()); +} + +// Tests StlContainerView. + +TEST(StlContainerViewTest, WorksForStlContainer) { + StaticAssertTypeEq<std::vector<int>, + StlContainerView<std::vector<int> >::type>(); + StaticAssertTypeEq<const std::vector<double>&, + StlContainerView<std::vector<double> >::const_reference>(); + + typedef std::vector<char> Chars; + Chars v1; + const Chars& v2(StlContainerView<Chars>::ConstReference(v1)); + EXPECT_EQ(&v1, &v2); + + v1.push_back('a'); + Chars v3 = StlContainerView<Chars>::Copy(v1); + EXPECT_THAT(v3, Eq(v3)); +} + +TEST(StlContainerViewTest, WorksForStaticNativeArray) { + StaticAssertTypeEq<NativeArray<int>, + StlContainerView<int[3]>::type>(); + StaticAssertTypeEq<NativeArray<double>, + StlContainerView<const double[4]>::type>(); + StaticAssertTypeEq<NativeArray<char[3]>, + StlContainerView<const char[2][3]>::type>(); + + StaticAssertTypeEq<const NativeArray<int>, + StlContainerView<int[2]>::const_reference>(); + + int a1[3] = { 0, 1, 2 }; + NativeArray<int> a2 = StlContainerView<int[3]>::ConstReference(a1); + EXPECT_EQ(3, a2.size()); + EXPECT_EQ(a1, a2.begin()); + + const NativeArray<int> a3 = StlContainerView<int[3]>::Copy(a1); + ASSERT_EQ(3, a3.size()); + EXPECT_EQ(0, a3.begin()[0]); + EXPECT_EQ(1, a3.begin()[1]); + EXPECT_EQ(2, a3.begin()[2]); + + // Makes sure a1 and a3 aren't aliases. + a1[0] = 3; + EXPECT_EQ(0, a3.begin()[0]); +} + +TEST(StlContainerViewTest, WorksForDynamicNativeArray) { + StaticAssertTypeEq<NativeArray<int>, + StlContainerView<tuple<const int*, size_t> >::type>(); + StaticAssertTypeEq<NativeArray<double>, + StlContainerView<tuple<linked_ptr<double>, int> >::type>(); + + StaticAssertTypeEq<const NativeArray<int>, + StlContainerView<tuple<const int*, int> >::const_reference>(); + + int a1[3] = { 0, 1, 2 }; + const int* const p1 = a1; + NativeArray<int> a2 = StlContainerView<tuple<const int*, int> >:: + ConstReference(make_tuple(p1, 3)); + EXPECT_EQ(3, a2.size()); + EXPECT_EQ(a1, a2.begin()); + + const NativeArray<int> a3 = StlContainerView<tuple<int*, size_t> >:: + Copy(make_tuple(static_cast<int*>(a1), 3)); + ASSERT_EQ(3, a3.size()); + EXPECT_EQ(0, a3.begin()[0]); + EXPECT_EQ(1, a3.begin()[1]); + EXPECT_EQ(2, a3.begin()[2]); + + // Makes sure a1 and a3 aren't aliases. + a1[0] = 3; + EXPECT_EQ(0, a3.begin()[0]); +} + } // namespace } // namespace internal } // namespace testing diff --git a/testing/gmock/test/gmock-matchers_test.cc b/testing/gmock/test/gmock-matchers_test.cc index e770901..3541eef 100644 --- a/testing/gmock/test/gmock-matchers_test.cc +++ b/testing/gmock/test/gmock-matchers_test.cc @@ -60,7 +60,9 @@ bool SkipPrefix(const char* prefix, const char** pstr); namespace gmock_matchers_test { using std::stringstream; +using std::tr1::make_tuple; using testing::A; +using testing::AllArgs; using testing::AllOf; using testing::An; using testing::AnyOf; @@ -98,6 +100,7 @@ using testing::StrEq; using testing::StrNe; using testing::Truly; using testing::TypedEq; +using testing::Value; using testing::_; using testing::internal::FloatingEqMatcher; using testing::internal::FormatMatcherDescriptionSyntaxError; @@ -1333,7 +1336,7 @@ TEST(Eq2Test, MatchesEqualArguments) { // Tests that Eq() describes itself properly. TEST(Eq2Test, CanDescribeSelf) { Matcher<const Tuple2&> m = Eq(); - EXPECT_EQ("argument #0 is equal to argument #1", Describe(m)); + EXPECT_EQ("are a pair (x, y) where x == y", Describe(m)); } // Tests that Ge() matches a 2-tuple where the first field >= the @@ -1348,8 +1351,7 @@ TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) { // Tests that Ge() describes itself properly. TEST(Ge2Test, CanDescribeSelf) { Matcher<const Tuple2&> m = Ge(); - EXPECT_EQ("argument #0 is greater than or equal to argument #1", - Describe(m)); + EXPECT_EQ("are a pair (x, y) where x >= y", Describe(m)); } // Tests that Gt() matches a 2-tuple where the first field > the @@ -1364,7 +1366,7 @@ TEST(Gt2Test, MatchesGreaterThanArguments) { // Tests that Gt() describes itself properly. TEST(Gt2Test, CanDescribeSelf) { Matcher<const Tuple2&> m = Gt(); - EXPECT_EQ("argument #0 is greater than argument #1", Describe(m)); + EXPECT_EQ("are a pair (x, y) where x > y", Describe(m)); } // Tests that Le() matches a 2-tuple where the first field <= the @@ -1379,8 +1381,7 @@ TEST(Le2Test, MatchesLessThanOrEqualArguments) { // Tests that Le() describes itself properly. TEST(Le2Test, CanDescribeSelf) { Matcher<const Tuple2&> m = Le(); - EXPECT_EQ("argument #0 is less than or equal to argument #1", - Describe(m)); + EXPECT_EQ("are a pair (x, y) where x <= y", Describe(m)); } // Tests that Lt() matches a 2-tuple where the first field < the @@ -1395,7 +1396,7 @@ TEST(Lt2Test, MatchesLessThanArguments) { // Tests that Lt() describes itself properly. TEST(Lt2Test, CanDescribeSelf) { Matcher<const Tuple2&> m = Lt(); - EXPECT_EQ("argument #0 is less than argument #1", Describe(m)); + EXPECT_EQ("are a pair (x, y) where x < y", Describe(m)); } // Tests that Ne() matches a 2-tuple where the first field != the @@ -1410,7 +1411,7 @@ TEST(Ne2Test, MatchesUnequalArguments) { // Tests that Ne() describes itself properly. TEST(Ne2Test, CanDescribeSelf) { Matcher<const Tuple2&> m = Ne(); - EXPECT_EQ("argument #0 is not equal to argument #1", Describe(m)); + EXPECT_EQ("are a pair (x, y) where x != y", Describe(m)); } // Tests that Not(m) matches any value that doesn't match m. @@ -1670,6 +1671,54 @@ TEST(MatchesTest, WorksWithMatcherOnNonRefType) { EXPECT_FALSE(Matches(eq5)(2)); } +// Tests Value(value, matcher). Since Value() is a simple wrapper for +// Matches(), which has been tested already, we don't spend a lot of +// effort on testing Value(). +TEST(ValueTest, WorksWithPolymorphicMatcher) { + EXPECT_TRUE(Value("hi", StartsWith("h"))); + EXPECT_FALSE(Value(5, Gt(10))); +} + +TEST(ValueTest, WorksWithMonomorphicMatcher) { + const Matcher<int> is_zero = Eq(0); + EXPECT_TRUE(Value(0, is_zero)); + EXPECT_FALSE(Value('a', is_zero)); + + int n = 0; + const Matcher<const int&> ref_n = Ref(n); + EXPECT_TRUE(Value(n, ref_n)); + EXPECT_FALSE(Value(1, ref_n)); +} + +TEST(AllArgsTest, WorksForTuple) { + EXPECT_THAT(make_tuple(1, 2L), AllArgs(Lt())); + EXPECT_THAT(make_tuple(2L, 1), Not(AllArgs(Lt()))); +} + +TEST(AllArgsTest, WorksForNonTuple) { + EXPECT_THAT(42, AllArgs(Gt(0))); + EXPECT_THAT('a', Not(AllArgs(Eq('b')))); +} + +class AllArgsHelper { + public: + MOCK_METHOD2(Helper, int(char x, int y)); +}; + +TEST(AllArgsTest, WorksInWithClause) { + AllArgsHelper helper; + ON_CALL(helper, Helper(_, _)) + .With(AllArgs(Lt())) + .WillByDefault(Return(1)); + EXPECT_CALL(helper, Helper(_, _)); + EXPECT_CALL(helper, Helper(_, _)) + .With(AllArgs(Gt())) + .WillOnce(Return(2)); + + EXPECT_EQ(1, helper.Helper('\1', 2)); + EXPECT_EQ(2, helper.Helper('a', 1)); +} + // Tests that ASSERT_THAT() and EXPECT_THAT() work when the value // matches the matcher. TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) { @@ -2765,9 +2814,7 @@ TEST(ByRefTest, AllowsNotCopyableValueInMatchers) { // different element types. template <typename T> -class ContainerEqTest : public testing::Test { - public: -}; +class ContainerEqTest : public testing::Test {}; typedef testing::Types< std::set<int>, @@ -2901,6 +2948,60 @@ TEST(ContainerEqExtraTest, WorksForMaps) { Explain(m, test_map)); } +TEST(ContainerEqExtraTest, WorksForNativeArray) { + int a1[] = { 1, 2, 3 }; + int a2[] = { 1, 2, 3 }; + int b[] = { 1, 2, 4 }; + + EXPECT_THAT(a1, ContainerEq(a2)); + EXPECT_THAT(a1, Not(ContainerEq(b))); +} + +TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) { + const char a1[][3] = { "hi", "lo" }; + const char a2[][3] = { "hi", "lo" }; + const char b[][3] = { "lo", "hi" }; + + // Tests using ContainerEq() in the first dimension. + EXPECT_THAT(a1, ContainerEq(a2)); + EXPECT_THAT(a1, Not(ContainerEq(b))); + + // Tests using ContainerEq() in the second dimension. + EXPECT_THAT(a1, ElementsAre(ContainerEq(a2[0]), ContainerEq(a2[1]))); + EXPECT_THAT(a1, ElementsAre(Not(ContainerEq(b[0])), ContainerEq(a2[1]))); +} + +TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) { + const int a1[] = { 1, 2, 3 }; + const int a2[] = { 1, 2, 3 }; + const int b[] = { 1, 2, 3, 4 }; + + const int* const p1 = a1; + EXPECT_THAT(make_tuple(p1, 3), ContainerEq(a2)); + EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(b))); + + const int c[] = { 1, 3, 2 }; + EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(c))); +} + +TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) { + std::string a1[][3] = { + { "hi", "hello", "ciao" }, + { "bye", "see you", "ciao" } + }; + + std::string a2[][3] = { + { "hi", "hello", "ciao" }, + { "bye", "see you", "ciao" } + }; + + const Matcher<const std::string(&)[2][3]> m = ContainerEq(a2); + EXPECT_THAT(a1, m); + + a2[0][0] = "ha"; + EXPECT_THAT(a1, m); +} + // Tests GetParamIndex(). TEST(GetParamIndexTest, WorksForEmptyParamList) { diff --git a/testing/gmock/test/gmock-printers_test.cc b/testing/gmock/test/gmock-printers_test.cc index 29a0db8..8c03ec4 100644 --- a/testing/gmock/test/gmock-printers_test.cc +++ b/testing/gmock/test/gmock-printers_test.cc @@ -154,10 +154,13 @@ using ::std::tr1::tuple; using ::std::vector; using ::testing::ElementsAre; using ::testing::StartsWith; +using ::testing::internal::NativeArray; using ::testing::internal::Strings; using ::testing::internal::UniversalTersePrint; +using ::testing::internal::UniversalPrint; using ::testing::internal::UniversalTersePrintTupleFieldsToStrings; using ::testing::internal::UniversalPrinter; +using ::testing::internal::kReference; using ::testing::internal::string; #if GTEST_OS_WINDOWS @@ -485,75 +488,58 @@ TEST(PrintPointerTest, MemberFunctionPointer) { // Tests printing C arrays. -// One-dimensional array. - -void ArrayHelper1(int (&a)[5]) { // NOLINT - EXPECT_EQ("{ 1, 2, 3, 4, 5 }", Print(a)); +// The difference between this and Print() is that it ensures that the +// argument is a reference to an array. +template <typename T, size_t N> +string PrintArrayHelper(T (&a)[N]) { + return Print(a); } +// One-dimensional array. TEST(PrintArrayTest, OneDimensionalArray) { int a[5] = { 1, 2, 3, 4, 5 }; - ArrayHelper1(a); + EXPECT_EQ("{ 1, 2, 3, 4, 5 }", PrintArrayHelper(a)); } // Two-dimensional array. - -void ArrayHelper2(int (&a)[2][5]) { // NOLINT - EXPECT_EQ("{ { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 0 } }", Print(a)); -} - TEST(PrintArrayTest, TwoDimensionalArray) { int a[2][5] = { { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 0 } }; - ArrayHelper2(a); + EXPECT_EQ("{ { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 0 } }", PrintArrayHelper(a)); } // Array of const elements. - -void ArrayHelper3(const bool (&a)[1]) { // NOLINT - EXPECT_EQ("{ false }", Print(a)); -} - TEST(PrintArrayTest, ConstArray) { const bool a[1] = { false }; - ArrayHelper3(a); + EXPECT_EQ("{ false }", PrintArrayHelper(a)); } // Char array. - -void ArrayHelper4(char (&a)[3]) { // NOLINT - EXPECT_EQ(PrintPointer(a) + " pointing to \"Hi\"", Print(a)); -} - TEST(PrintArrayTest, CharArray) { - char a[3] = "Hi"; - ArrayHelper4(a); + // Array a contains '\0' in the middle and doesn't end with '\0'. + char a[3] = { 'H', '\0', 'i' }; + EXPECT_EQ("\"H\\0i\"", PrintArrayHelper(a)); } // Const char array. - -void ArrayHelper5(const char (&a)[3]) { // NOLINT - EXPECT_EQ(Print(a), PrintPointer(a) + " pointing to \"Hi\""); -} - TEST(PrintArrayTest, ConstCharArray) { - const char a[3] = "Hi"; - ArrayHelper5(a); + const char a[4] = "\0Hi"; + EXPECT_EQ("\"\\0Hi\\0\"", PrintArrayHelper(a)); } // Array of objects. TEST(PrintArrayTest, ObjectArray) { string a[3] = { "Hi", "Hello", "Ni hao" }; - EXPECT_EQ("{ \"Hi\", \"Hello\", \"Ni hao\" }", Print(a)); + EXPECT_EQ("{ \"Hi\", \"Hello\", \"Ni hao\" }", PrintArrayHelper(a)); } // Array with many elements. TEST(PrintArrayTest, BigArray) { int a[100] = { 1, 2, 3 }; EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0 }", - Print(a)); + PrintArrayHelper(a)); } // Tests printing ::string and ::std::string. @@ -803,6 +789,17 @@ TEST(PrintStlContainerTest, NestedContainer) { EXPECT_EQ("{ { 1, 2 }, { 3, 4, 5 } }", Print(v)); } +TEST(PrintStlContainerTest, OneDimensionalNativeArray) { + const int a[] = { 1, 2, 3 }; + NativeArray<int> b(a, 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); + EXPECT_EQ("{ { 1, 2, 3 }, { 4, 5, 6 } }", Print(b)); +} // Tests printing tuples. @@ -995,6 +992,11 @@ TEST(PrintToStringTest, WorksForReference) { UniversalPrinter<const int&>::PrintToString(n)); } +TEST(PrintToStringTest, WorksForArray) { + int n[3] = { 1, 2, 3 }; + EXPECT_EQ("{ 1, 2, 3 }", UniversalPrinter<int[3]>::PrintToString(n)); +} + TEST(UniversalTersePrintTest, WorksForNonReference) { ::std::stringstream ss; UniversalTersePrint(123, &ss); @@ -1025,6 +1027,37 @@ TEST(UniversalTersePrintTest, WorksForCString) { EXPECT_EQ("NULL", ss3.str()); } +TEST(UniversalPrintTest, WorksForNonReference) { + ::std::stringstream ss; + UniversalPrint(123, &ss); + EXPECT_EQ("123", ss.str()); +} + +TEST(UniversalPrintTest, WorksForReference) { + const int& n = 123; + ::std::stringstream ss; + UniversalPrint(n, &ss); + EXPECT_EQ("123", ss.str()); +} + +TEST(UniversalPrintTest, WorksForCString) { + const char* s1 = "abc"; + ::std::stringstream ss1; + UniversalPrint(s1, &ss1); + EXPECT_EQ(PrintPointer(s1) + " pointing to \"abc\"", string(ss1.str())); + + char* s2 = const_cast<char*>(s1); + ::std::stringstream ss2; + UniversalPrint(s2, &ss2); + EXPECT_EQ(PrintPointer(s2) + " pointing to \"abc\"", string(ss2.str())); + + const char* s3 = NULL; + ::std::stringstream ss3; + UniversalPrint(s3, &ss3); + EXPECT_EQ("NULL", ss3.str()); +} + + TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsEmptyTuple) { EXPECT_THAT(UniversalTersePrintTupleFieldsToStrings(make_tuple()), ElementsAre()); diff --git a/testing/gmock/test/gmock-spec-builders_test.cc b/testing/gmock/test/gmock-spec-builders_test.cc index e8c3902..058d343c 100644 --- a/testing/gmock/test/gmock-spec-builders_test.cc +++ b/testing/gmock/test/gmock-spec-builders_test.cc @@ -72,6 +72,7 @@ using testing::Const; using testing::DoAll; using testing::DoDefault; using testing::GMOCK_FLAG(verbose); +using testing::Gt; using testing::InSequence; using testing::Invoke; using testing::InvokeWithoutArgs; @@ -96,6 +97,7 @@ class MockA { MOCK_METHOD1(DoA, void(int n)); // NOLINT MOCK_METHOD1(ReturnResult, Result(int n)); // NOLINT MOCK_METHOD2(Binary, bool(int x, int y)); // NOLINT + MOCK_METHOD2(ReturnInt, int(int x, int y)); // NOLINT }; class MockB { @@ -171,25 +173,25 @@ TEST(OnCallSyntaxTest, EvaluatesSecondArgumentOnce) { // Tests that the syntax of ON_CALL() is enforced at run time. -TEST(OnCallSyntaxTest, WithArgumentsIsOptional) { +TEST(OnCallSyntaxTest, WithIsOptional) { MockA a; ON_CALL(a, DoA(5)) .WillByDefault(Return()); ON_CALL(a, DoA(_)) - .WithArguments(_) + .With(_) .WillByDefault(Return()); } -TEST(OnCallSyntaxTest, WithArgumentsCanAppearAtMostOnce) { +TEST(OnCallSyntaxTest, WithCanAppearAtMostOnce) { MockA a; EXPECT_NONFATAL_FAILURE({ // NOLINT ON_CALL(a, ReturnResult(_)) - .WithArguments(_) - .WithArguments(_) + .With(_) + .With(_) .WillByDefault(Return(Result())); - }, ".WithArguments() cannot appear more than once in an ON_CALL()"); + }, ".With() cannot appear more than once in an ON_CALL()"); } #if GTEST_HAS_DEATH_TEST @@ -237,47 +239,44 @@ TEST(ExpectCallSyntaxTest, EvaluatesSecondArgumentOnce) { // Tests that the syntax of EXPECT_CALL() is enforced at run time. -TEST(ExpectCallSyntaxTest, WithArgumentsIsOptional) { +TEST(ExpectCallSyntaxTest, WithIsOptional) { MockA a; EXPECT_CALL(a, DoA(5)) .Times(0); EXPECT_CALL(a, DoA(6)) - .WithArguments(_) + .With(_) .Times(0); } -TEST(ExpectCallSyntaxTest, WithArgumentsCanAppearAtMostOnce) { +TEST(ExpectCallSyntaxTest, WithCanAppearAtMostOnce) { MockA a; EXPECT_NONFATAL_FAILURE({ // NOLINT EXPECT_CALL(a, DoA(6)) - .WithArguments(_) - .WithArguments(_); - }, ".WithArguments() cannot appear more than once in " - "an EXPECT_CALL()"); + .With(_) + .With(_); + }, ".With() cannot appear more than once in an EXPECT_CALL()"); a.DoA(6); } -TEST(ExpectCallSyntaxTest, WithArgumentsMustBeFirstClause) { +TEST(ExpectCallSyntaxTest, WithMustBeFirstClause) { MockA a; EXPECT_NONFATAL_FAILURE({ // NOLINT EXPECT_CALL(a, DoA(1)) .Times(1) - .WithArguments(_); - }, ".WithArguments() must be the first clause in an " - "EXPECT_CALL()"); + .With(_); + }, ".With() must be the first clause in an EXPECT_CALL()"); a.DoA(1); EXPECT_NONFATAL_FAILURE({ // NOLINT EXPECT_CALL(a, DoA(2)) .WillOnce(Return()) - .WithArguments(_); - }, ".WithArguments() must be the first clause in an " - "EXPECT_CALL()"); + .With(_); + }, ".With() must be the first clause in an EXPECT_CALL()"); a.DoA(2); } @@ -1612,6 +1611,53 @@ TEST_F(GMockVerboseFlagTest, InvalidFlagIsTreatedAsWarning) { #endif // 0 +// A helper class that generates a failure when printed. We use it to +// ensure that Google Mock doesn't print a value (even to an internal +// buffer) when it is not supposed to do so. +class PrintMeNot {}; + +void PrintTo(PrintMeNot /* dummy */, ::std::ostream* /* os */) { + ADD_FAILURE() << "Google Mock is printing a value that shouldn't be " + << "printed even to an internal buffer."; +} + +class LogTestHelper { + public: + MOCK_METHOD1(Foo, PrintMeNot(PrintMeNot)); +}; + +class GMockLogTest : public ::testing::Test { + protected: + virtual void SetUp() { original_verbose_ = GMOCK_FLAG(verbose); } + virtual void TearDown() { GMOCK_FLAG(verbose) = original_verbose_; } + + LogTestHelper helper_; + string original_verbose_; +}; + +TEST_F(GMockLogTest, DoesNotPrintGoodCallInternallyIfVerbosityIsWarning) { + GMOCK_FLAG(verbose) = kWarningVerbosity; + EXPECT_CALL(helper_, Foo(_)) + .WillOnce(Return(PrintMeNot())); + helper_.Foo(PrintMeNot()); // This is an expected call. +} + +TEST_F(GMockLogTest, DoesNotPrintGoodCallInternallyIfVerbosityIsError) { + GMOCK_FLAG(verbose) = kErrorVerbosity; + EXPECT_CALL(helper_, Foo(_)) + .WillOnce(Return(PrintMeNot())); + helper_.Foo(PrintMeNot()); // This is an expected call. +} + +TEST_F(GMockLogTest, DoesNotPrintWarningInternallyIfVerbosityIsError) { + GMOCK_FLAG(verbose) = kErrorVerbosity; + ON_CALL(helper_, Foo(_)) + .WillByDefault(Return(PrintMeNot())); + helper_.Foo(PrintMeNot()); // This should generate a warning. +} + +// Tests Mock::AllowLeak(). + TEST(AllowLeakTest, AllowsLeakingUnusedMockObject) { MockA* a = new MockA; Mock::AllowLeak(a); diff --git a/testing/gmock/test/gmock_link_test.h b/testing/gmock/test/gmock_link_test.h index 4e0adb7..b903f3c 100644 --- a/testing/gmock/test/gmock_link_test.h +++ b/testing/gmock/test/gmock_link_test.h @@ -120,6 +120,7 @@ #include <errno.h> #endif +#include <gmock/internal/gmock-port.h> #include <gtest/gtest.h> #include <iostream> #include <vector> diff --git a/testing/gmock/test/gmock_output_test_.cc b/testing/gmock/test/gmock_output_test_.cc index 97619af..8244f10 100644 --- a/testing/gmock/test/gmock_output_test_.cc +++ b/testing/gmock/test/gmock_output_test_.cc @@ -177,19 +177,19 @@ TEST_F(GMockOutputTest, MismatchArguments) { foo_.Bar(s, 0, 0); } -TEST_F(GMockOutputTest, MismatchWithArguments) { +TEST_F(GMockOutputTest, MismatchWith) { EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1))) - .WithArguments(Ge()); + .With(Ge()); - foo_.Bar2(2, 3); // Mismatch WithArguments() + foo_.Bar2(2, 3); // Mismatch With() foo_.Bar2(2, 1); } -TEST_F(GMockOutputTest, MismatchArgumentsAndWithArguments) { +TEST_F(GMockOutputTest, MismatchArgumentsAndWith) { EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1))) - .WithArguments(Ge()); + .With(Ge()); - foo_.Bar2(1, 3); // Mismatch arguments and mismatch WithArguments() + foo_.Bar2(1, 3); // Mismatch arguments and mismatch With() foo_.Bar2(2, 1); } diff --git a/testing/gmock/test/gmock_output_test_golden.txt b/testing/gmock/test/gmock_output_test_golden.txt index 887b7be..aeec660 100644 --- a/testing/gmock/test/gmock_output_test_golden.txt +++ b/testing/gmock/test/gmock_output_test_golden.txt @@ -174,7 +174,7 @@ FILE:#: Expected: to be called once Actual: never called - unsatisfied and active [ FAILED ] GMockOutputTest.MismatchArguments -[ RUN ] GMockOutputTest.MismatchWithArguments +[ RUN ] GMockOutputTest.MismatchWith unknown file: Failure Unexpected mock function call - returning default value. @@ -183,12 +183,12 @@ Unexpected mock function call - returning default value. Google Mock tried the following 1 expectation, but it didn't match: FILE:#: - Expected: argument #0 is greater than or equal to argument #1 - Actual: false + Expected args: are a pair (x, y) where x >= y + Actual: don't match Expected: to be called once Actual: never called - unsatisfied and active -[ FAILED ] GMockOutputTest.MismatchWithArguments -[ RUN ] GMockOutputTest.MismatchArgumentsAndWithArguments +[ FAILED ] GMockOutputTest.MismatchWith +[ RUN ] GMockOutputTest.MismatchArgumentsAndWith unknown file: Failure Unexpected mock function call - returning default value. @@ -199,11 +199,11 @@ Google Mock tried the following 1 expectation, but it didn't match: FILE:#: Expected arg #0: is greater than or equal to 2 Actual: 1 - Expected: argument #0 is greater than or equal to argument #1 - Actual: false + Expected args: are a pair (x, y) where x >= y + Actual: don't match Expected: to be called once Actual: never called - unsatisfied and active -[ FAILED ] GMockOutputTest.MismatchArgumentsAndWithArguments +[ FAILED ] GMockOutputTest.MismatchArgumentsAndWith [ RUN ] GMockOutputTest.UnexpectedCallWithDefaultAction unknown file: Failure @@ -290,8 +290,8 @@ Stack trace: [ FAILED ] GMockOutputTest.UnsatisfiedPrerequisites [ FAILED ] GMockOutputTest.UnsatisfiedExpectation [ FAILED ] GMockOutputTest.MismatchArguments -[ FAILED ] GMockOutputTest.MismatchWithArguments -[ FAILED ] GMockOutputTest.MismatchArgumentsAndWithArguments +[ FAILED ] GMockOutputTest.MismatchWith +[ FAILED ] GMockOutputTest.MismatchArgumentsAndWith [ FAILED ] GMockOutputTest.UnexpectedCallWithDefaultAction [ FAILED ] GMockOutputTest.ExcessiveCallWithDefaultAction |