| OLD | NEW |
| 1 // Copyright 2008, Google Inc. | 1 // Copyright 2008, Google Inc. |
| 2 // All rights reserved. | 2 // All rights reserved. |
| 3 // | 3 // |
| 4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
| 6 // met: | 6 // met: |
| 7 // | 7 // |
| 8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
| 9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
| 10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 #include <gtest/gtest-spi.h> | 46 #include <gtest/gtest-spi.h> |
| 47 | 47 |
| 48 namespace { | 48 namespace { |
| 49 | 49 |
| 50 using std::list; | 50 using std::list; |
| 51 using std::map; | 51 using std::map; |
| 52 using std::pair; | 52 using std::pair; |
| 53 using std::set; | 53 using std::set; |
| 54 using std::stringstream; | 54 using std::stringstream; |
| 55 using std::vector; | 55 using std::vector; |
| 56 using std::tr1::get; |
| 57 using std::tr1::make_tuple; |
| 58 using std::tr1::tuple; |
| 56 using testing::_; | 59 using testing::_; |
| 60 using testing::Args; |
| 57 using testing::Contains; | 61 using testing::Contains; |
| 58 using testing::ElementsAre; | 62 using testing::ElementsAre; |
| 59 using testing::ElementsAreArray; | 63 using testing::ElementsAreArray; |
| 60 using testing::Eq; | 64 using testing::Eq; |
| 61 using testing::Ge; | 65 using testing::Ge; |
| 62 using testing::Gt; | 66 using testing::Gt; |
| 67 using testing::Lt; |
| 63 using testing::MakeMatcher; | 68 using testing::MakeMatcher; |
| 64 using testing::Matcher; | 69 using testing::Matcher; |
| 65 using testing::MatcherInterface; | 70 using testing::MatcherInterface; |
| 66 using testing::Ne; | 71 using testing::Ne; |
| 67 using testing::Not; | 72 using testing::Not; |
| 68 using testing::Pointee; | 73 using testing::Pointee; |
| 69 using testing::Ref; | 74 using testing::Ref; |
| 70 using testing::StaticAssertTypeEq; | 75 using testing::StaticAssertTypeEq; |
| 71 using testing::StrEq; | 76 using testing::StrEq; |
| 77 using testing::Value; |
| 72 using testing::internal::string; | 78 using testing::internal::string; |
| 73 | 79 |
| 74 // Returns the description of the given matcher. | 80 // Returns the description of the given matcher. |
| 75 template <typename T> | 81 template <typename T> |
| 76 string Describe(const Matcher<T>& m) { | 82 string Describe(const Matcher<T>& m) { |
| 77 stringstream ss; | 83 stringstream ss; |
| 78 m.DescribeTo(&ss); | 84 m.DescribeTo(&ss); |
| 79 return ss.str(); | 85 return ss.str(); |
| 80 } | 86 } |
| 81 | 87 |
| 82 // Returns the description of the negation of the given matcher. | 88 // Returns the description of the negation of the given matcher. |
| 83 template <typename T> | 89 template <typename T> |
| 84 string DescribeNegation(const Matcher<T>& m) { | 90 string DescribeNegation(const Matcher<T>& m) { |
| 85 stringstream ss; | 91 stringstream ss; |
| 86 m.DescribeNegationTo(&ss); | 92 m.DescribeNegationTo(&ss); |
| 87 return ss.str(); | 93 return ss.str(); |
| 88 } | 94 } |
| 89 | 95 |
| 90 // Returns the reason why x matches, or doesn't match, m. | 96 // Returns the reason why x matches, or doesn't match, m. |
| 91 template <typename MatcherType, typename Value> | 97 template <typename MatcherType, typename Value> |
| 92 string Explain(const MatcherType& m, const Value& x) { | 98 string Explain(const MatcherType& m, const Value& x) { |
| 93 stringstream ss; | 99 stringstream ss; |
| 94 m.ExplainMatchResultTo(x, &ss); | 100 m.ExplainMatchResultTo(x, &ss); |
| 95 return ss.str(); | 101 return ss.str(); |
| 96 } | 102 } |
| 97 | 103 |
| 104 // Tests Args<k0, ..., kn>(m). |
| 105 |
| 106 TEST(ArgsTest, AcceptsZeroTemplateArg) { |
| 107 const tuple<int, bool> t(5, true); |
| 108 EXPECT_THAT(t, Args<>(Eq(tuple<>()))); |
| 109 EXPECT_THAT(t, Not(Args<>(Ne(tuple<>())))); |
| 110 } |
| 111 |
| 112 TEST(ArgsTest, AcceptsOneTemplateArg) { |
| 113 const tuple<int, bool> t(5, true); |
| 114 EXPECT_THAT(t, Args<0>(Eq(make_tuple(5)))); |
| 115 EXPECT_THAT(t, Args<1>(Eq(make_tuple(true)))); |
| 116 EXPECT_THAT(t, Not(Args<1>(Eq(make_tuple(false))))); |
| 117 } |
| 118 |
| 119 TEST(ArgsTest, AcceptsTwoTemplateArgs) { |
| 120 const tuple<short, int, long> t(4, 5, 6L); // NOLINT |
| 121 |
| 122 EXPECT_THAT(t, (Args<0, 1>(Lt()))); |
| 123 EXPECT_THAT(t, (Args<1, 2>(Lt()))); |
| 124 EXPECT_THAT(t, Not(Args<0, 2>(Gt()))); |
| 125 } |
| 126 |
| 127 TEST(ArgsTest, AcceptsRepeatedTemplateArgs) { |
| 128 const tuple<short, int, long> t(4, 5, 6L); // NOLINT |
| 129 EXPECT_THAT(t, (Args<0, 0>(Eq()))); |
| 130 EXPECT_THAT(t, Not(Args<1, 1>(Ne()))); |
| 131 } |
| 132 |
| 133 TEST(ArgsTest, AcceptsDecreasingTemplateArgs) { |
| 134 const tuple<short, int, long> t(4, 5, 6L); // NOLINT |
| 135 EXPECT_THAT(t, (Args<2, 0>(Gt()))); |
| 136 EXPECT_THAT(t, Not(Args<2, 1>(Lt()))); |
| 137 } |
| 138 |
| 139 MATCHER(SumIsZero, "") { |
| 140 return get<0>(arg) + get<1>(arg) + get<2>(arg) == 0; |
| 141 } |
| 142 |
| 143 TEST(ArgsTest, AcceptsMoreTemplateArgsThanArityOfOriginalTuple) { |
| 144 EXPECT_THAT(make_tuple(-1, 2), (Args<0, 0, 1>(SumIsZero()))); |
| 145 EXPECT_THAT(make_tuple(1, 2), Not(Args<0, 0, 1>(SumIsZero()))); |
| 146 } |
| 147 |
| 148 TEST(ArgsTest, CanBeNested) { |
| 149 const tuple<short, int, long, int> t(4, 5, 6L, 6); // NOLINT |
| 150 EXPECT_THAT(t, (Args<1, 2, 3>(Args<1, 2>(Eq())))); |
| 151 EXPECT_THAT(t, (Args<0, 1, 3>(Args<0, 2>(Lt())))); |
| 152 } |
| 153 |
| 154 TEST(ArgsTest, CanMatchTupleByValue) { |
| 155 typedef tuple<char, int, int> Tuple3; |
| 156 const Matcher<Tuple3> m = Args<1, 2>(Lt()); |
| 157 EXPECT_TRUE(m.Matches(Tuple3('a', 1, 2))); |
| 158 EXPECT_FALSE(m.Matches(Tuple3('b', 2, 2))); |
| 159 } |
| 160 |
| 161 TEST(ArgsTest, CanMatchTupleByReference) { |
| 162 typedef tuple<char, char, int> Tuple3; |
| 163 const Matcher<const Tuple3&> m = Args<0, 1>(Lt()); |
| 164 EXPECT_TRUE(m.Matches(Tuple3('a', 'b', 2))); |
| 165 EXPECT_FALSE(m.Matches(Tuple3('b', 'b', 2))); |
| 166 } |
| 167 |
| 168 // Validates that arg is printed as str. |
| 169 MATCHER_P(PrintsAs, str, "") { |
| 170 typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(arg_type)) RawTuple; |
| 171 return |
| 172 testing::internal::UniversalPrinter<RawTuple>::PrintToString(arg) == str; |
| 173 } |
| 174 |
| 175 TEST(ArgsTest, AcceptsTenTemplateArgs) { |
| 176 EXPECT_THAT(make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9), |
| 177 (Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>( |
| 178 PrintsAs("(9, 8, 7, 6, 5, 4, 3, 2, 1, 0)")))); |
| 179 EXPECT_THAT(make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9), |
| 180 Not(Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>( |
| 181 PrintsAs("(0, 8, 7, 6, 5, 4, 3, 2, 1, 0)")))); |
| 182 } |
| 183 |
| 184 TEST(ArgsTest, DescirbesSelfCorrectly) { |
| 185 const Matcher<tuple<int, bool, char> > m = Args<2, 0>(Lt()); |
| 186 EXPECT_EQ("are a tuple whose fields (#2, #0) are a pair (x, y) where x < y", |
| 187 Describe(m)); |
| 188 } |
| 189 |
| 190 TEST(ArgsTest, DescirbesNestedArgsCorrectly) { |
| 191 const Matcher<const tuple<int, bool, char, int>&> m = |
| 192 Args<0, 2, 3>(Args<2, 0>(Lt())); |
| 193 EXPECT_EQ("are a tuple whose fields (#0, #2, #3) are a tuple " |
| 194 "whose fields (#2, #0) are a pair (x, y) where x < y", |
| 195 Describe(m)); |
| 196 } |
| 197 |
| 198 TEST(ArgsTest, DescribesNegationCorrectly) { |
| 199 const Matcher<tuple<int, char> > m = Args<1, 0>(Gt()); |
| 200 EXPECT_EQ("are a tuple whose fields (#1, #0) are a pair (x, y) " |
| 201 "where x > y is false", |
| 202 DescribeNegation(m)); |
| 203 } |
| 204 |
| 98 // For testing ExplainMatchResultTo(). | 205 // For testing ExplainMatchResultTo(). |
| 99 class GreaterThanMatcher : public MatcherInterface<int> { | 206 class GreaterThanMatcher : public MatcherInterface<int> { |
| 100 public: | 207 public: |
| 101 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {} | 208 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {} |
| 102 | 209 |
| 103 virtual bool Matches(int lhs) const { return lhs > rhs_; } | 210 virtual bool Matches(int lhs) const { return lhs > rhs_; } |
| 104 | 211 |
| 105 virtual void DescribeTo(::std::ostream* os) const { | 212 virtual void DescribeTo(::std::ostream* os) const { |
| 106 *os << "is greater than " << rhs_; | 213 *os << "is greater than " << rhs_; |
| 107 } | 214 } |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 } | 430 } |
| 324 | 431 |
| 325 TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) { | 432 TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) { |
| 326 int a[] = { 0, 1, 2 }; | 433 int a[] = { 0, 1, 2 }; |
| 327 vector<int> v(a, a + GMOCK_ARRAY_SIZE_(a)); | 434 vector<int> v(a, a + GMOCK_ARRAY_SIZE_(a)); |
| 328 | 435 |
| 329 EXPECT_THAT(&v, Pointee(ElementsAre(0, 1, _))); | 436 EXPECT_THAT(&v, Pointee(ElementsAre(0, 1, _))); |
| 330 EXPECT_THAT(&v, Not(Pointee(ElementsAre(0, _, 3)))); | 437 EXPECT_THAT(&v, Not(Pointee(ElementsAre(0, _, 3)))); |
| 331 } | 438 } |
| 332 | 439 |
| 440 TEST(ElementsAreTest, WorksWithNativeArrayPassedByReference) { |
| 441 int array[] = { 0, 1, 2 }; |
| 442 EXPECT_THAT(array, ElementsAre(0, 1, _)); |
| 443 EXPECT_THAT(array, Not(ElementsAre(1, _, _))); |
| 444 EXPECT_THAT(array, Not(ElementsAre(0, _))); |
| 445 } |
| 446 |
| 447 class NativeArrayPassedAsPointerAndSize { |
| 448 public: |
| 449 MOCK_METHOD2(Helper, void(int* array, int size)); |
| 450 }; |
| 451 |
| 452 TEST(ElementsAreTest, WorksWithNativeArrayPassedAsPointerAndSize) { |
| 453 int array[] = { 0, 1 }; |
| 454 ::std::tr1::tuple<int*, size_t> array_as_tuple(array, 2); |
| 455 EXPECT_THAT(array_as_tuple, ElementsAre(0, 1)); |
| 456 EXPECT_THAT(array_as_tuple, Not(ElementsAre(0))); |
| 457 |
| 458 NativeArrayPassedAsPointerAndSize helper; |
| 459 EXPECT_CALL(helper, Helper(_, _)) |
| 460 .With(ElementsAre(0, 1)); |
| 461 helper.Helper(array, 2); |
| 462 } |
| 463 |
| 464 TEST(ElementsAreTest, WorksWithTwoDimensionalNativeArray) { |
| 465 const char a2[][3] = { "hi", "lo" }; |
| 466 EXPECT_THAT(a2, ElementsAre(ElementsAre('h', 'i', '\0'), |
| 467 ElementsAre('l', 'o', '\0'))); |
| 468 EXPECT_THAT(a2, ElementsAre(StrEq("hi"), StrEq("lo"))); |
| 469 EXPECT_THAT(a2, ElementsAre(Not(ElementsAre('h', 'o', '\0')), |
| 470 ElementsAre('l', 'o', '\0'))); |
| 471 } |
| 472 |
| 333 // Tests for ElementsAreArray(). Since ElementsAreArray() shares most | 473 // Tests for ElementsAreArray(). Since ElementsAreArray() shares most |
| 334 // of the implementation with ElementsAre(), we don't test it as | 474 // of the implementation with ElementsAre(), we don't test it as |
| 335 // thoroughly here. | 475 // thoroughly here. |
| 336 | 476 |
| 337 TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) { | 477 TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) { |
| 338 const int a[] = { 1, 2, 3 }; | 478 const int a[] = { 1, 2, 3 }; |
| 339 | 479 |
| 340 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a)); | 480 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a)); |
| 341 EXPECT_THAT(test_vector, ElementsAreArray(a)); | 481 EXPECT_THAT(test_vector, ElementsAreArray(a)); |
| 342 | 482 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 372 vector<string> test_vector; | 512 vector<string> test_vector; |
| 373 test_vector.push_back("one"); | 513 test_vector.push_back("one"); |
| 374 test_vector.push_back("two"); | 514 test_vector.push_back("two"); |
| 375 test_vector.push_back("three"); | 515 test_vector.push_back("three"); |
| 376 EXPECT_THAT(test_vector, ElementsAreArray(kMatcherArray)); | 516 EXPECT_THAT(test_vector, ElementsAreArray(kMatcherArray)); |
| 377 | 517 |
| 378 test_vector.push_back("three"); | 518 test_vector.push_back("three"); |
| 379 EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray))); | 519 EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray))); |
| 380 } | 520 } |
| 381 | 521 |
| 522 // Since ElementsAre() and ElementsAreArray() share much of the |
| 523 // implementation, we only do a sanity test for native arrays here. |
| 524 TEST(ElementsAreArrayTest, WorksWithNativeArray) { |
| 525 ::std::string a[] = { "hi", "ho" }; |
| 526 ::std::string b[] = { "hi", "ho" }; |
| 527 |
| 528 EXPECT_THAT(a, ElementsAreArray(b)); |
| 529 EXPECT_THAT(a, ElementsAreArray(b, 2)); |
| 530 EXPECT_THAT(a, Not(ElementsAreArray(b, 1))); |
| 531 } |
| 532 |
| 382 // Tests for the MATCHER*() macro family. | 533 // Tests for the MATCHER*() macro family. |
| 383 | 534 |
| 384 // Tests that a simple MATCHER() definition works. | 535 // Tests that a simple MATCHER() definition works. |
| 385 | 536 |
| 386 MATCHER(IsEven, "") { return (arg % 2) == 0; } | 537 MATCHER(IsEven, "") { return (arg % 2) == 0; } |
| 387 | 538 |
| 388 TEST(MatcherMacroTest, Works) { | 539 TEST(MatcherMacroTest, Works) { |
| 389 const Matcher<int> m = IsEven(); | 540 const Matcher<int> m = IsEven(); |
| 390 EXPECT_TRUE(m.Matches(6)); | 541 EXPECT_TRUE(m.Matches(6)); |
| 391 EXPECT_FALSE(m.Matches(7)); | 542 EXPECT_FALSE(m.Matches(7)); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 const Matcher<const ::std::string&> m2 = IsEmptyStringByRef(); | 587 const Matcher<const ::std::string&> m2 = IsEmptyStringByRef(); |
| 437 EXPECT_TRUE(m2.Matches("")); | 588 EXPECT_TRUE(m2.Matches("")); |
| 438 } | 589 } |
| 439 | 590 |
| 440 // Tests that MATCHER() can be used in a namespace. | 591 // Tests that MATCHER() can be used in a namespace. |
| 441 | 592 |
| 442 namespace matcher_test { | 593 namespace matcher_test { |
| 443 MATCHER(IsOdd, "") { return (arg % 2) != 0; } | 594 MATCHER(IsOdd, "") { return (arg % 2) != 0; } |
| 444 } // namespace matcher_test | 595 } // namespace matcher_test |
| 445 | 596 |
| 446 TEST(MatcherTest, WorksInNamespace) { | 597 TEST(MatcherMacroTest, WorksInNamespace) { |
| 447 Matcher<int> m = matcher_test::IsOdd(); | 598 Matcher<int> m = matcher_test::IsOdd(); |
| 448 EXPECT_FALSE(m.Matches(4)); | 599 EXPECT_FALSE(m.Matches(4)); |
| 449 EXPECT_TRUE(m.Matches(5)); | 600 EXPECT_TRUE(m.Matches(5)); |
| 450 } | 601 } |
| 451 | 602 |
| 603 // Tests that Value() can be used to compose matchers. |
| 604 MATCHER(IsPositiveOdd, "") { |
| 605 return Value(arg, matcher_test::IsOdd()) && arg > 0; |
| 606 } |
| 607 |
| 608 TEST(MatcherMacroTest, CanBeComposedUsingValue) { |
| 609 EXPECT_THAT(3, IsPositiveOdd()); |
| 610 EXPECT_THAT(4, Not(IsPositiveOdd())); |
| 611 EXPECT_THAT(-1, Not(IsPositiveOdd())); |
| 612 } |
| 613 |
| 452 // Tests that a simple MATCHER_P() definition works. | 614 // Tests that a simple MATCHER_P() definition works. |
| 453 | 615 |
| 454 MATCHER_P(IsGreaterThan32And, n, "") { return arg > 32 && arg > n; } | 616 MATCHER_P(IsGreaterThan32And, n, "") { return arg > 32 && arg > n; } |
| 455 | 617 |
| 456 TEST(MatcherPMacroTest, Works) { | 618 TEST(MatcherPMacroTest, Works) { |
| 457 const Matcher<int> m = IsGreaterThan32And(5); | 619 const Matcher<int> m = IsGreaterThan32And(5); |
| 458 EXPECT_TRUE(m.Matches(36)); | 620 EXPECT_TRUE(m.Matches(36)); |
| 459 EXPECT_FALSE(m.Matches(5)); | 621 EXPECT_FALSE(m.Matches(5)); |
| 460 | 622 |
| 461 EXPECT_EQ("is greater than 32 and 5", Describe(m)); | 623 EXPECT_EQ("is greater than 32 and 5", Describe(m)); |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 735 EqualsSumOfMatcherP7<int, int, int, int, int, int, char> a7 = | 897 EqualsSumOfMatcherP7<int, int, int, int, int, int, char> a7 = |
| 736 EqualsSumOf(1, 2, 3, 4, 5, 6, '7'); | 898 EqualsSumOf(1, 2, 3, 4, 5, 6, '7'); |
| 737 EqualsSumOfMatcherP8<int, int, int, int, int, int, int, char> a8 = | 899 EqualsSumOfMatcherP8<int, int, int, int, int, int, int, char> a8 = |
| 738 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, '8'); | 900 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, '8'); |
| 739 EqualsSumOfMatcherP9<int, int, int, int, int, int, int, int, char> a9 = | 901 EqualsSumOfMatcherP9<int, int, int, int, int, int, int, int, char> a9 = |
| 740 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, '9'); | 902 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, '9'); |
| 741 EqualsSumOfMatcherP10<int, int, int, int, int, int, int, int, int, char> a10 = | 903 EqualsSumOfMatcherP10<int, int, int, int, int, int, int, int, int, char> a10 = |
| 742 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, 9, '0'); | 904 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, 9, '0'); |
| 743 } | 905 } |
| 744 | 906 |
| 907 // Tests that matcher-typed parameters can be used in Value() inside a |
| 908 // MATCHER_Pn definition. |
| 909 |
| 910 // Succeeds if arg matches exactly 2 of the 3 matchers. |
| 911 MATCHER_P3(TwoOf, m1, m2, m3, "") { |
| 912 const int count = static_cast<int>(Value(arg, m1)) |
| 913 + static_cast<int>(Value(arg, m2)) + static_cast<int>(Value(arg, m3)); |
| 914 return count == 2; |
| 915 } |
| 916 |
| 917 TEST(MatcherPnMacroTest, CanUseMatcherTypedParameterInValue) { |
| 918 EXPECT_THAT(42, TwoOf(Gt(0), Lt(50), Eq(10))); |
| 919 EXPECT_THAT(0, Not(TwoOf(Gt(-1), Lt(1), Eq(0)))); |
| 920 } |
| 921 |
| 922 // Tests Contains(). |
| 923 |
| 745 TEST(ContainsTest, ListMatchesWhenElementIsInContainer) { | 924 TEST(ContainsTest, ListMatchesWhenElementIsInContainer) { |
| 746 list<int> some_list; | 925 list<int> some_list; |
| 747 some_list.push_back(3); | 926 some_list.push_back(3); |
| 748 some_list.push_back(1); | 927 some_list.push_back(1); |
| 749 some_list.push_back(2); | 928 some_list.push_back(2); |
| 750 EXPECT_THAT(some_list, Contains(1)); | 929 EXPECT_THAT(some_list, Contains(1)); |
| 751 EXPECT_THAT(some_list, Contains(3.0)); | 930 EXPECT_THAT(some_list, Contains(Gt(2.5))); |
| 752 EXPECT_THAT(some_list, Contains(2.0f)); | 931 EXPECT_THAT(some_list, Contains(Eq(2.0f))); |
| 753 | 932 |
| 754 list<string> another_list; | 933 list<string> another_list; |
| 755 another_list.push_back("fee"); | 934 another_list.push_back("fee"); |
| 756 another_list.push_back("fie"); | 935 another_list.push_back("fie"); |
| 757 another_list.push_back("foe"); | 936 another_list.push_back("foe"); |
| 758 another_list.push_back("fum"); | 937 another_list.push_back("fum"); |
| 759 EXPECT_THAT(another_list, Contains(string("fee"))); | 938 EXPECT_THAT(another_list, Contains(string("fee"))); |
| 760 } | 939 } |
| 761 | 940 |
| 762 TEST(ContainsTest, ListDoesNotMatchWhenElementIsNotInContainer) { | 941 TEST(ContainsTest, ListDoesNotMatchWhenElementIsNotInContainer) { |
| 763 list<int> some_list; | 942 list<int> some_list; |
| 764 some_list.push_back(3); | 943 some_list.push_back(3); |
| 765 some_list.push_back(1); | 944 some_list.push_back(1); |
| 766 EXPECT_THAT(some_list, Not(Contains(4))); | 945 EXPECT_THAT(some_list, Not(Contains(4))); |
| 767 } | 946 } |
| 768 | 947 |
| 769 TEST(ContainsTest, SetMatchesWhenElementIsInContainer) { | 948 TEST(ContainsTest, SetMatchesWhenElementIsInContainer) { |
| 770 set<int> some_set; | 949 set<int> some_set; |
| 771 some_set.insert(3); | 950 some_set.insert(3); |
| 772 some_set.insert(1); | 951 some_set.insert(1); |
| 773 some_set.insert(2); | 952 some_set.insert(2); |
| 774 EXPECT_THAT(some_set, Contains(1.0)); | 953 EXPECT_THAT(some_set, Contains(Eq(1.0))); |
| 775 EXPECT_THAT(some_set, Contains(3.0f)); | 954 EXPECT_THAT(some_set, Contains(Eq(3.0f))); |
| 776 EXPECT_THAT(some_set, Contains(2)); | 955 EXPECT_THAT(some_set, Contains(2)); |
| 777 | 956 |
| 778 set<const char*> another_set; | 957 set<const char*> another_set; |
| 779 another_set.insert("fee"); | 958 another_set.insert("fee"); |
| 780 another_set.insert("fie"); | 959 another_set.insert("fie"); |
| 781 another_set.insert("foe"); | 960 another_set.insert("foe"); |
| 782 another_set.insert("fum"); | 961 another_set.insert("fum"); |
| 783 EXPECT_THAT(another_set, Contains(string("fum"))); | 962 EXPECT_THAT(another_set, Contains(Eq(string("fum")))); |
| 784 } | 963 } |
| 785 | 964 |
| 786 TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) { | 965 TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) { |
| 787 set<int> some_set; | 966 set<int> some_set; |
| 788 some_set.insert(3); | 967 some_set.insert(3); |
| 789 some_set.insert(1); | 968 some_set.insert(1); |
| 790 EXPECT_THAT(some_set, Not(Contains(4))); | 969 EXPECT_THAT(some_set, Not(Contains(4))); |
| 791 | 970 |
| 792 set<const char*> c_string_set; | 971 set<const char*> c_string_set; |
| 793 c_string_set.insert("hello"); | 972 c_string_set.insert("hello"); |
| 794 EXPECT_THAT(c_string_set, Not(Contains(string("hello").c_str()))); | 973 EXPECT_THAT(c_string_set, Not(Contains(string("hello").c_str()))); |
| 795 } | 974 } |
| 796 | 975 |
| 797 TEST(ContainsTest, DescribesItselfCorrectly) { | 976 TEST(ContainsTest, DescribesItselfCorrectly) { |
| 977 const int a[2] = { 1, 2 }; |
| 978 Matcher<const int(&)[2]> m = Contains(2); |
| 979 EXPECT_EQ("element 1 matches", Explain(m, a)); |
| 980 |
| 981 m = Contains(3); |
| 982 EXPECT_EQ("", Explain(m, a)); |
| 983 } |
| 984 |
| 985 TEST(ContainsTest, ExplainsMatchResultCorrectly) { |
| 798 Matcher<vector<int> > m = Contains(1); | 986 Matcher<vector<int> > m = Contains(1); |
| 799 EXPECT_EQ("contains 1", Describe(m)); | 987 EXPECT_EQ("contains at least one element that is equal to 1", Describe(m)); |
| 988 |
| 989 Matcher<vector<int> > m2 = Not(m); |
| 990 EXPECT_EQ("doesn't contain any element that is equal to 1", Describe(m2)); |
| 800 } | 991 } |
| 801 | 992 |
| 802 TEST(ContainsTest, MapMatchesWhenElementIsInContainer) { | 993 TEST(ContainsTest, MapMatchesWhenElementIsInContainer) { |
| 803 map<const char*, int> my_map; | 994 map<const char*, int> my_map; |
| 804 const char* bar = "a string"; | 995 const char* bar = "a string"; |
| 805 my_map[bar] = 2; | 996 my_map[bar] = 2; |
| 806 EXPECT_THAT(my_map, Contains(pair<const char* const, int>(bar, 2))); | 997 EXPECT_THAT(my_map, Contains(pair<const char* const, int>(bar, 2))); |
| 807 | 998 |
| 808 map<string, int> another_map; | 999 map<string, int> another_map; |
| 809 another_map["fee"] = 1; | 1000 another_map["fee"] = 1; |
| 810 another_map["fie"] = 2; | 1001 another_map["fie"] = 2; |
| 811 another_map["foe"] = 3; | 1002 another_map["foe"] = 3; |
| 812 another_map["fum"] = 4; | 1003 another_map["fum"] = 4; |
| 813 EXPECT_THAT(another_map, Contains(pair<const string, int>(string("fee"), 1))); | 1004 EXPECT_THAT(another_map, Contains(pair<const string, int>(string("fee"), 1))); |
| 814 EXPECT_THAT(another_map, Contains(pair<const string, int>("fie", 2))); | 1005 EXPECT_THAT(another_map, Contains(pair<const string, int>("fie", 2))); |
| 815 } | 1006 } |
| 816 | 1007 |
| 817 TEST(ContainsTest, MapDoesNotMatchWhenElementIsNotInContainer) { | 1008 TEST(ContainsTest, MapDoesNotMatchWhenElementIsNotInContainer) { |
| 818 map<int, int> some_map; | 1009 map<int, int> some_map; |
| 819 some_map[1] = 11; | 1010 some_map[1] = 11; |
| 820 some_map[2] = 22; | 1011 some_map[2] = 22; |
| 821 EXPECT_THAT(some_map, Not(Contains(pair<const int, int>(2, 23)))); | 1012 EXPECT_THAT(some_map, Not(Contains(pair<const int, int>(2, 23)))); |
| 822 } | 1013 } |
| 823 | 1014 |
| 824 TEST(ContainsTest, ArrayMatchesWhenElementIsInContainer) { | 1015 TEST(ContainsTest, ArrayMatchesWhenElementIsInContainer) { |
| 825 const char* string_array[] = { "fee", "fie", "foe", "fum" }; | 1016 const char* string_array[] = { "fee", "fie", "foe", "fum" }; |
| 826 EXPECT_THAT(string_array, Contains(string("fum"))); | 1017 EXPECT_THAT(string_array, Contains(Eq(string("fum")))); |
| 827 } | 1018 } |
| 828 | 1019 |
| 829 TEST(ContainsTest, ArrayDoesNotMatchWhenElementIsNotInContainer) { | 1020 TEST(ContainsTest, ArrayDoesNotMatchWhenElementIsNotInContainer) { |
| 830 int int_array[] = { 1, 2, 3, 4 }; | 1021 int int_array[] = { 1, 2, 3, 4 }; |
| 831 EXPECT_THAT(int_array, Not(Contains(5))); | 1022 EXPECT_THAT(int_array, Not(Contains(5))); |
| 832 } | 1023 } |
| 833 | 1024 |
| 1025 TEST(ContainsTest, AcceptsMatcher) { |
| 1026 const int a[] = { 1, 2, 3 }; |
| 1027 EXPECT_THAT(a, Contains(Gt(2))); |
| 1028 EXPECT_THAT(a, Not(Contains(Gt(4)))); |
| 1029 } |
| 1030 |
| 1031 TEST(ContainsTest, WorksForNativeArrayAsTuple) { |
| 1032 const int a[] = { 1, 2 }; |
| 1033 const int* const pointer = a; |
| 1034 EXPECT_THAT(make_tuple(pointer, 2), Contains(1)); |
| 1035 EXPECT_THAT(make_tuple(pointer, 2), Not(Contains(Gt(3)))); |
| 1036 } |
| 1037 |
| 1038 TEST(ContainsTest, WorksForTwoDimensionalNativeArray) { |
| 1039 int a[][3] = { { 1, 2, 3 }, { 4, 5, 6 } }; |
| 1040 EXPECT_THAT(a, Contains(ElementsAre(4, 5, 6))); |
| 1041 EXPECT_THAT(a, Contains(Contains(5))); |
| 1042 EXPECT_THAT(a, Not(Contains(ElementsAre(3, 4, 5)))); |
| 1043 EXPECT_THAT(a, Contains(Not(Contains(5)))); |
| 1044 } |
| 1045 |
| 834 } // namespace | 1046 } // namespace |
| OLD | NEW |