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 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 virtual void ExplainMatchResultTo(int lhs, ::std::ostream* os) const { | 216 virtual void ExplainMatchResultTo(int lhs, ::std::ostream* os) const { |
217 const int diff = lhs - rhs_; | 217 const int diff = lhs - rhs_; |
218 if (diff > 0) { | 218 if (diff > 0) { |
219 *os << "is " << diff << " more than " << rhs_; | 219 *os << "is " << diff << " more than " << rhs_; |
220 } else if (diff == 0) { | 220 } else if (diff == 0) { |
221 *os << "is the same as " << rhs_; | 221 *os << "is the same as " << rhs_; |
222 } else { | 222 } else { |
223 *os << "is " << -diff << " less than " << rhs_; | 223 *os << "is " << -diff << " less than " << rhs_; |
224 } | 224 } |
225 } | 225 } |
| 226 |
226 private: | 227 private: |
227 const int rhs_; | 228 int rhs_; |
228 }; | 229 }; |
229 | 230 |
230 Matcher<int> GreaterThan(int n) { | 231 Matcher<int> GreaterThan(int n) { |
231 return MakeMatcher(new GreaterThanMatcher(n)); | 232 return MakeMatcher(new GreaterThanMatcher(n)); |
232 } | 233 } |
233 | 234 |
234 // Tests for ElementsAre(). | 235 // Tests for ElementsAre(). |
235 | 236 |
236 // Evaluates to the number of elements in 'array'. | 237 // Evaluates to the number of elements in 'array'. |
237 #define GMOCK_ARRAY_SIZE_(array) (sizeof(array)/sizeof(array[0])) | 238 #define GMOCK_ARRAY_SIZE_(array) (sizeof(array)/sizeof(array[0])) |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 EXPECT_FALSE(m.Matches(test_vector)); | 405 EXPECT_FALSE(m.Matches(test_vector)); |
405 } | 406 } |
406 | 407 |
407 TEST(ElementsAreTest, WorksForNestedContainer) { | 408 TEST(ElementsAreTest, WorksForNestedContainer) { |
408 const char* strings[] = { | 409 const char* strings[] = { |
409 "Hi", | 410 "Hi", |
410 "world" | 411 "world" |
411 }; | 412 }; |
412 | 413 |
413 vector<list<char> > nested; | 414 vector<list<char> > nested; |
414 for (int i = 0; i < GMOCK_ARRAY_SIZE_(strings); i++) { | 415 for (size_t i = 0; i < GMOCK_ARRAY_SIZE_(strings); i++) { |
415 nested.push_back(list<char>(strings[i], strings[i] + strlen(strings[i]))); | 416 nested.push_back(list<char>(strings[i], strings[i] + strlen(strings[i]))); |
416 } | 417 } |
417 | 418 |
418 EXPECT_THAT(nested, ElementsAre(ElementsAre('H', Ne('e')), | 419 EXPECT_THAT(nested, ElementsAre(ElementsAre('H', Ne('e')), |
419 ElementsAre('w', 'o', _, _, 'd'))); | 420 ElementsAre('w', 'o', _, _, 'd'))); |
420 EXPECT_THAT(nested, Not(ElementsAre(ElementsAre('H', 'e'), | 421 EXPECT_THAT(nested, Not(ElementsAre(ElementsAre('H', 'e'), |
421 ElementsAre('w', 'o', _, _, 'd')))); | 422 ElementsAre('w', 'o', _, _, 'd')))); |
422 } | 423 } |
423 | 424 |
424 TEST(ElementsAreTest, WorksWithByRefElementMatchers) { | 425 TEST(ElementsAreTest, WorksWithByRefElementMatchers) { |
(...skipping 14 matching lines...) Expand all Loading... |
439 | 440 |
440 TEST(ElementsAreTest, WorksWithNativeArrayPassedByReference) { | 441 TEST(ElementsAreTest, WorksWithNativeArrayPassedByReference) { |
441 int array[] = { 0, 1, 2 }; | 442 int array[] = { 0, 1, 2 }; |
442 EXPECT_THAT(array, ElementsAre(0, 1, _)); | 443 EXPECT_THAT(array, ElementsAre(0, 1, _)); |
443 EXPECT_THAT(array, Not(ElementsAre(1, _, _))); | 444 EXPECT_THAT(array, Not(ElementsAre(1, _, _))); |
444 EXPECT_THAT(array, Not(ElementsAre(0, _))); | 445 EXPECT_THAT(array, Not(ElementsAre(0, _))); |
445 } | 446 } |
446 | 447 |
447 class NativeArrayPassedAsPointerAndSize { | 448 class NativeArrayPassedAsPointerAndSize { |
448 public: | 449 public: |
| 450 NativeArrayPassedAsPointerAndSize() {} |
| 451 |
449 MOCK_METHOD2(Helper, void(int* array, int size)); | 452 MOCK_METHOD2(Helper, void(int* array, int size)); |
| 453 |
| 454 private: |
| 455 GTEST_DISALLOW_COPY_AND_ASSIGN_(NativeArrayPassedAsPointerAndSize); |
450 }; | 456 }; |
451 | 457 |
452 TEST(ElementsAreTest, WorksWithNativeArrayPassedAsPointerAndSize) { | 458 TEST(ElementsAreTest, WorksWithNativeArrayPassedAsPointerAndSize) { |
453 int array[] = { 0, 1 }; | 459 int array[] = { 0, 1 }; |
454 ::std::tr1::tuple<int*, size_t> array_as_tuple(array, 2); | 460 ::std::tr1::tuple<int*, size_t> array_as_tuple(array, 2); |
455 EXPECT_THAT(array_as_tuple, ElementsAre(0, 1)); | 461 EXPECT_THAT(array_as_tuple, ElementsAre(0, 1)); |
456 EXPECT_THAT(array_as_tuple, Not(ElementsAre(0))); | 462 EXPECT_THAT(array_as_tuple, Not(ElementsAre(0))); |
457 | 463 |
458 NativeArrayPassedAsPointerAndSize helper; | 464 NativeArrayPassedAsPointerAndSize helper; |
459 EXPECT_CALL(helper, Helper(_, _)) | 465 EXPECT_CALL(helper, Helper(_, _)) |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
543 | 549 |
544 EXPECT_EQ("is even", Describe(m)); | 550 EXPECT_EQ("is even", Describe(m)); |
545 EXPECT_EQ("not (is even)", DescribeNegation(m)); | 551 EXPECT_EQ("not (is even)", DescribeNegation(m)); |
546 EXPECT_EQ("", Explain(m, 6)); | 552 EXPECT_EQ("", Explain(m, 6)); |
547 EXPECT_EQ("", Explain(m, 7)); | 553 EXPECT_EQ("", Explain(m, 7)); |
548 } | 554 } |
549 | 555 |
550 // Tests that the description string supplied to MATCHER() must be | 556 // Tests that the description string supplied to MATCHER() must be |
551 // valid. | 557 // valid. |
552 | 558 |
553 MATCHER(HasBadDescription, "Invalid%") { return true; } | 559 MATCHER(HasBadDescription, "Invalid%") { |
| 560 // Uses arg to suppress "unused parameter" warning. |
| 561 return arg==arg; |
| 562 } |
554 | 563 |
555 TEST(MatcherMacroTest, | 564 TEST(MatcherMacroTest, |
556 CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) { | 565 CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) { |
557 EXPECT_NONFATAL_FAILURE( | 566 EXPECT_NONFATAL_FAILURE( |
558 HasBadDescription(), | 567 HasBadDescription(), |
559 "Syntax error at index 7 in matcher description \"Invalid%\": " | 568 "Syntax error at index 7 in matcher description \"Invalid%\": " |
560 "use \"%%\" instead of \"%\" to print \"%\"."); | 569 "use \"%%\" instead of \"%\" to print \"%\"."); |
561 } | 570 } |
562 | 571 |
563 MATCHER(HasGoodDescription, "good") { return true; } | 572 MATCHER(HasGoodDescription, "good") { return arg==arg; } |
564 | 573 |
565 TEST(MatcherMacroTest, AcceptsValidDescription) { | 574 TEST(MatcherMacroTest, AcceptsValidDescription) { |
566 const Matcher<int> m = HasGoodDescription(); | 575 const Matcher<int> m = HasGoodDescription(); |
567 EXPECT_EQ("good", Describe(m)); | 576 EXPECT_EQ("good", Describe(m)); |
568 } | 577 } |
569 | 578 |
570 // Tests that the body of MATCHER() can reference the type of the | 579 // Tests that the body of MATCHER() can reference the type of the |
571 // value being matched. | 580 // value being matched. |
572 | 581 |
573 MATCHER(IsEmptyString, "") { | 582 MATCHER(IsEmptyString, "") { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
635 | 644 |
636 TEST(MatcherPMacroTest, | 645 TEST(MatcherPMacroTest, |
637 CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) { | 646 CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) { |
638 EXPECT_NONFATAL_FAILURE( | 647 EXPECT_NONFATAL_FAILURE( |
639 HasBadDescription1(2), | 648 HasBadDescription1(2), |
640 "Syntax error at index 6 in matcher description \"not %(m)s good\": " | 649 "Syntax error at index 6 in matcher description \"not %(m)s good\": " |
641 "\"m\" is an invalid parameter name."); | 650 "\"m\" is an invalid parameter name."); |
642 } | 651 } |
643 | 652 |
644 | 653 |
645 MATCHER_P(HasGoodDescription1, n, "good %(n)s") { return true; } | 654 MATCHER_P(HasGoodDescription1, n, "good %(n)s") { return arg==arg; } |
646 | 655 |
647 TEST(MatcherPMacroTest, AcceptsValidDescription) { | 656 TEST(MatcherPMacroTest, AcceptsValidDescription) { |
648 const Matcher<int> m = HasGoodDescription1(5); | 657 const Matcher<int> m = HasGoodDescription1(5); |
649 EXPECT_EQ("good 5", Describe(m)); | 658 EXPECT_EQ("good 5", Describe(m)); |
650 } | 659 } |
651 | 660 |
652 // Tests that the description is calculated correctly from the matcher name. | 661 // Tests that the description is calculated correctly from the matcher name. |
653 MATCHER_P(_is_Greater_Than32and_, n, "") { return arg > 32 && arg > n; } | 662 MATCHER_P(_is_Greater_Than32and_, n, "") { return arg > 32 && arg > n; } |
654 | 663 |
655 TEST(MatcherPMacroTest, GeneratesCorrectDescription) { | 664 TEST(MatcherPMacroTest, GeneratesCorrectDescription) { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
702 TEST(MatcherPnMacroTest, | 711 TEST(MatcherPnMacroTest, |
703 CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) { | 712 CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) { |
704 EXPECT_NONFATAL_FAILURE( | 713 EXPECT_NONFATAL_FAILURE( |
705 HasBadDescription2(3, 4), | 714 HasBadDescription2(3, 4), |
706 "Syntax error at index 4 in matcher description \"not %(good\": " | 715 "Syntax error at index 4 in matcher description \"not %(good\": " |
707 "an interpolation must end with \")s\", but \"%(good\" does not."); | 716 "an interpolation must end with \")s\", but \"%(good\" does not."); |
708 } | 717 } |
709 | 718 |
710 MATCHER_P2(HasComplexDescription, foo, bar, | 719 MATCHER_P2(HasComplexDescription, foo, bar, |
711 "is as complex as %(foo)s %(bar)s (i.e. %(*)s or %%%(foo)s!)") { | 720 "is as complex as %(foo)s %(bar)s (i.e. %(*)s or %%%(foo)s!)") { |
712 return true; | 721 return arg==arg; |
713 } | 722 } |
714 | 723 |
715 TEST(MatcherPnMacroTest, AcceptsValidDescription) { | 724 TEST(MatcherPnMacroTest, AcceptsValidDescription) { |
716 Matcher<int> m = HasComplexDescription(100, "ducks"); | 725 Matcher<int> m = HasComplexDescription(100, "ducks"); |
717 EXPECT_EQ("is as complex as 100 \"ducks\" (i.e. (100, \"ducks\") or %100!)", | 726 EXPECT_EQ("is as complex as 100 \"ducks\" (i.e. (100, \"ducks\") or %100!)", |
718 Describe(m)); | 727 Describe(m)); |
719 } | 728 } |
720 | 729 |
721 // Tests that the body of MATCHER_Pn() can reference the parameter | 730 // Tests that the body of MATCHER_Pn() can reference the parameter |
722 // types. | 731 // types. |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
854 | 863 |
855 EXPECT_THAT(124, Not(EqualsSumOf(100L, 20, static_cast<char>(3)))); | 864 EXPECT_THAT(124, Not(EqualsSumOf(100L, 20, static_cast<char>(3)))); |
856 EXPECT_THAT("abcde", Not(EqualsSumOf(::std::string("a"), "b", 'c', "d"))); | 865 EXPECT_THAT("abcde", Not(EqualsSumOf(::std::string("a"), "b", 'c', "d"))); |
857 } | 866 } |
858 | 867 |
859 // Tests that the matcher body can promote the parameter types. | 868 // Tests that the matcher body can promote the parameter types. |
860 | 869 |
861 MATCHER_P2(EqConcat, prefix, suffix, "") { | 870 MATCHER_P2(EqConcat, prefix, suffix, "") { |
862 // The following lines promote the two parameters to desired types. | 871 // The following lines promote the two parameters to desired types. |
863 std::string prefix_str(prefix); | 872 std::string prefix_str(prefix); |
864 char suffix_char(suffix); | 873 char suffix_char = static_cast<char>(suffix); |
865 return arg == prefix_str + suffix_char; | 874 return arg == prefix_str + suffix_char; |
866 } | 875 } |
867 | 876 |
868 TEST(MatcherPnMacroTest, SimpleTypePromotion) { | 877 TEST(MatcherPnMacroTest, SimpleTypePromotion) { |
869 Matcher<std::string> no_promo = | 878 Matcher<std::string> no_promo = |
870 EqConcat(std::string("foo"), 't'); | 879 EqConcat(std::string("foo"), 't'); |
871 Matcher<const std::string&> promo = | 880 Matcher<const std::string&> promo = |
872 EqConcat("foo", static_cast<int>('t')); | 881 EqConcat("foo", static_cast<int>('t')); |
873 EXPECT_FALSE(no_promo.Matches("fool")); | 882 EXPECT_FALSE(no_promo.Matches("fool")); |
874 EXPECT_FALSE(promo.Matches("fool")); | 883 EXPECT_FALSE(promo.Matches("fool")); |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1037 | 1046 |
1038 TEST(ContainsTest, WorksForTwoDimensionalNativeArray) { | 1047 TEST(ContainsTest, WorksForTwoDimensionalNativeArray) { |
1039 int a[][3] = { { 1, 2, 3 }, { 4, 5, 6 } }; | 1048 int a[][3] = { { 1, 2, 3 }, { 4, 5, 6 } }; |
1040 EXPECT_THAT(a, Contains(ElementsAre(4, 5, 6))); | 1049 EXPECT_THAT(a, Contains(ElementsAre(4, 5, 6))); |
1041 EXPECT_THAT(a, Contains(Contains(5))); | 1050 EXPECT_THAT(a, Contains(Contains(5))); |
1042 EXPECT_THAT(a, Not(Contains(ElementsAre(3, 4, 5)))); | 1051 EXPECT_THAT(a, Not(Contains(ElementsAre(3, 4, 5)))); |
1043 EXPECT_THAT(a, Contains(Not(Contains(5)))); | 1052 EXPECT_THAT(a, Contains(Not(Contains(5)))); |
1044 } | 1053 } |
1045 | 1054 |
1046 } // namespace | 1055 } // namespace |
OLD | NEW |