| OLD | NEW |
| 1 // Copyright 2007, Google Inc. | 1 // Copyright 2007, 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 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 using testing::internal::kErrorVerbosity; | 90 using testing::internal::kErrorVerbosity; |
| 91 using testing::internal::kInfoVerbosity; | 91 using testing::internal::kInfoVerbosity; |
| 92 using testing::internal::kWarningVerbosity; | 92 using testing::internal::kWarningVerbosity; |
| 93 using testing::internal::ExpectationTester; | 93 using testing::internal::ExpectationTester; |
| 94 using testing::internal::string; | 94 using testing::internal::string; |
| 95 | 95 |
| 96 class Result {}; | 96 class Result {}; |
| 97 | 97 |
| 98 class MockA { | 98 class MockA { |
| 99 public: | 99 public: |
| 100 MockA() {} |
| 101 |
| 100 MOCK_METHOD1(DoA, void(int n)); // NOLINT | 102 MOCK_METHOD1(DoA, void(int n)); // NOLINT |
| 101 MOCK_METHOD1(ReturnResult, Result(int n)); // NOLINT | 103 MOCK_METHOD1(ReturnResult, Result(int n)); // NOLINT |
| 102 MOCK_METHOD2(Binary, bool(int x, int y)); // NOLINT | 104 MOCK_METHOD2(Binary, bool(int x, int y)); // NOLINT |
| 103 MOCK_METHOD2(ReturnInt, int(int x, int y)); // NOLINT | 105 MOCK_METHOD2(ReturnInt, int(int x, int y)); // NOLINT |
| 106 |
| 107 private: |
| 108 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockA); |
| 104 }; | 109 }; |
| 105 | 110 |
| 106 class MockB { | 111 class MockB { |
| 107 public: | 112 public: |
| 113 MockB() {} |
| 114 |
| 108 MOCK_CONST_METHOD0(DoB, int()); // NOLINT | 115 MOCK_CONST_METHOD0(DoB, int()); // NOLINT |
| 109 MOCK_METHOD1(DoB, int(int n)); // NOLINT | 116 MOCK_METHOD1(DoB, int(int n)); // NOLINT |
| 117 |
| 118 private: |
| 119 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockB); |
| 110 }; | 120 }; |
| 111 | 121 |
| 112 // Tests that EXPECT_CALL and ON_CALL compile in a presence of macro | 122 // Tests that EXPECT_CALL and ON_CALL compile in a presence of macro |
| 113 // redefining a mock method name. This could happen, for example, when | 123 // redefining a mock method name. This could happen, for example, when |
| 114 // the tested code #includes Win32 API headers which define many APIs | 124 // the tested code #includes Win32 API headers which define many APIs |
| 115 // as macros, e.g. #define TextOut TextOutW. | 125 // as macros, e.g. #define TextOut TextOutW. |
| 116 | 126 |
| 117 #define Method MethodW | 127 #define Method MethodW |
| 118 | 128 |
| 119 class CC { | 129 class CC { |
| 120 public: | 130 public: |
| 121 virtual ~CC() {} | 131 virtual ~CC() {} |
| 122 virtual int Method() = 0; | 132 virtual int Method() = 0; |
| 123 }; | 133 }; |
| 124 class MockCC : public CC { | 134 class MockCC : public CC { |
| 125 public: | 135 public: |
| 136 MockCC() {} |
| 137 |
| 126 MOCK_METHOD0(Method, int()); | 138 MOCK_METHOD0(Method, int()); |
| 139 |
| 140 private: |
| 141 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockCC); |
| 127 }; | 142 }; |
| 128 | 143 |
| 129 // Tests that a method with expanded name compiles. | 144 // Tests that a method with expanded name compiles. |
| 130 TEST(OnCallSyntaxTest, CompilesWithMethodNameExpandedFromMacro) { | 145 TEST(OnCallSyntaxTest, CompilesWithMethodNameExpandedFromMacro) { |
| 131 MockCC cc; | 146 MockCC cc; |
| 132 ON_CALL(cc, Method()); | 147 ON_CALL(cc, Method()); |
| 133 } | 148 } |
| 134 | 149 |
| 135 // Tests that the method with expanded name not only compiles but runs | 150 // Tests that the method with expanded name not only compiles but runs |
| 136 // and returns a correct value, too. | 151 // and returns a correct value, too. |
| (...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 564 EXPECT_CALL(b, DoB(2)) | 579 EXPECT_CALL(b, DoB(2)) |
| 565 .Times(1) | 580 .Times(1) |
| 566 .WillOnce(Return(1)) | 581 .WillOnce(Return(1)) |
| 567 .WillRepeatedly(Return(2)); // #5 | 582 .WillRepeatedly(Return(2)); // #5 |
| 568 | 583 |
| 569 // Satisfies the above expectations. | 584 // Satisfies the above expectations. |
| 570 b.DoB(1); | 585 b.DoB(1); |
| 571 b.DoB(2); | 586 b.DoB(2); |
| 572 } | 587 } |
| 573 const string& output = GetCapturedTestStdout(); | 588 const string& output = GetCapturedTestStdout(); |
| 574 EXPECT_PRED_FORMAT2(IsSubstring, | 589 EXPECT_PRED_FORMAT2( |
| 575 "Too many actions specified.\n" | 590 IsSubstring, |
| 576 "Expected to be never called, but has 1 WillOnce().", | 591 "Too many actions specified in EXPECT_CALL(b, DoB())...\n" |
| 577 output); // #1 | 592 "Expected to be never called, but has 1 WillOnce().", |
| 578 EXPECT_PRED_FORMAT2(IsSubstring, | 593 output); // #1 |
| 579 "Too many actions specified.\n" | 594 EXPECT_PRED_FORMAT2( |
| 580 "Expected to be called at most once, " | 595 IsSubstring, |
| 581 "but has 2 WillOnce()s.", | 596 "Too many actions specified in EXPECT_CALL(b, DoB())...\n" |
| 582 output); // #2 | 597 "Expected to be called at most once, " |
| 583 EXPECT_PRED_FORMAT2(IsSubstring, | 598 "but has 2 WillOnce()s.", |
| 584 "Too many actions specified.\n" | 599 output); // #2 |
| 585 "Expected to be called once, but has 2 WillOnce()s.", | 600 EXPECT_PRED_FORMAT2( |
| 586 output); // #3 | 601 IsSubstring, |
| 587 EXPECT_PRED_FORMAT2(IsSubstring, | 602 "Too many actions specified in EXPECT_CALL(b, DoB(1))...\n" |
| 588 "Too many actions specified.\n" | 603 "Expected to be called once, but has 2 WillOnce()s.", |
| 589 "Expected to be never called, but has 0 WillOnce()s " | 604 output); // #3 |
| 590 "and a WillRepeatedly().", | 605 EXPECT_PRED_FORMAT2( |
| 591 output); // #4 | 606 IsSubstring, |
| 592 EXPECT_PRED_FORMAT2(IsSubstring, | 607 "Too many actions specified in EXPECT_CALL(b, DoB())...\n" |
| 593 "Too many actions specified.\n" | 608 "Expected to be never called, but has 0 WillOnce()s " |
| 594 "Expected to be called once, but has 1 WillOnce() " | 609 "and a WillRepeatedly().", |
| 595 "and a WillRepeatedly().", | 610 output); // #4 |
| 596 output); // #5 | 611 EXPECT_PRED_FORMAT2( |
| 612 IsSubstring, |
| 613 "Too many actions specified in EXPECT_CALL(b, DoB(2))...\n" |
| 614 "Expected to be called once, but has 1 WillOnce() " |
| 615 "and a WillRepeatedly().", |
| 616 output); // #5 |
| 597 } | 617 } |
| 598 | 618 |
| 599 // Tests that Google Mock warns on having too few actions in an | 619 // Tests that Google Mock warns on having too few actions in an |
| 600 // expectation compared to its cardinality. | 620 // expectation compared to its cardinality. |
| 601 TEST(ExpectCallSyntaxTest, WarnsOnTooFewActions) { | 621 TEST(ExpectCallSyntaxTest, WarnsOnTooFewActions) { |
| 602 MockB b; | 622 MockB b; |
| 603 | 623 |
| 604 EXPECT_CALL(b, DoB()) | 624 EXPECT_CALL(b, DoB()) |
| 605 .Times(Between(2, 3)) | 625 .Times(Between(2, 3)) |
| 606 .WillOnce(Return(1)); | 626 .WillOnce(Return(1)); |
| 607 | 627 |
| 608 CaptureTestStdout(); | 628 CaptureTestStdout(); |
| 609 b.DoB(); | 629 b.DoB(); |
| 610 const string& output = GetCapturedTestStdout(); | 630 const string& output = GetCapturedTestStdout(); |
| 611 EXPECT_PRED_FORMAT2(IsSubstring, | 631 EXPECT_PRED_FORMAT2( |
| 612 "Too few actions specified.\n" | 632 IsSubstring, |
| 613 "Expected to be called between 2 and 3 times, " | 633 "Too few actions specified in EXPECT_CALL(b, DoB())...\n" |
| 614 "but has only 1 WillOnce().", | 634 "Expected to be called between 2 and 3 times, " |
| 615 output); | 635 "but has only 1 WillOnce().", |
| 636 output); |
| 616 b.DoB(); | 637 b.DoB(); |
| 617 } | 638 } |
| 618 | 639 |
| 619 #endif // 0 | 640 #endif // 0 |
| 620 | 641 |
| 621 // Tests the semantics of ON_CALL(). | 642 // Tests the semantics of ON_CALL(). |
| 622 | 643 |
| 623 // Tests that the built-in default action is taken when no ON_CALL() | 644 // Tests that the built-in default action is taken when no ON_CALL() |
| 624 // is specified. | 645 // is specified. |
| 625 TEST(OnCallTest, TakesBuiltInDefaultActionWhenNoOnCall) { | 646 TEST(OnCallTest, TakesBuiltInDefaultActionWhenNoOnCall) { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 681 } | 702 } |
| 682 | 703 |
| 683 // Tests lower-bound violation. | 704 // Tests lower-bound violation. |
| 684 TEST(ExpectCallTest, CatchesTooFewCalls) { | 705 TEST(ExpectCallTest, CatchesTooFewCalls) { |
| 685 EXPECT_NONFATAL_FAILURE({ // NOLINT | 706 EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 686 MockB b; | 707 MockB b; |
| 687 EXPECT_CALL(b, DoB(5)) | 708 EXPECT_CALL(b, DoB(5)) |
| 688 .Times(AtLeast(2)); | 709 .Times(AtLeast(2)); |
| 689 | 710 |
| 690 b.DoB(5); | 711 b.DoB(5); |
| 691 }, "Actual function call count doesn't match this expectation.\n" | 712 }, "Actual function call count doesn't match EXPECT_CALL(b, DoB(5))...\n" |
| 692 " Expected: to be called at least twice\n" | 713 " Expected: to be called at least twice\n" |
| 693 " Actual: called once - unsatisfied and active"); | 714 " Actual: called once - unsatisfied and active"); |
| 694 } | 715 } |
| 695 | 716 |
| 696 // Tests that the cardinality can be inferred when no Times(...) is | 717 // Tests that the cardinality can be inferred when no Times(...) is |
| 697 // specified. | 718 // specified. |
| 698 TEST(ExpectCallTest, InfersCardinalityWhenThereIsNoWillRepeatedly) { | 719 TEST(ExpectCallTest, InfersCardinalityWhenThereIsNoWillRepeatedly) { |
| 699 { | 720 { |
| 700 MockB b; | 721 MockB b; |
| 701 EXPECT_CALL(b, DoB()) | 722 EXPECT_CALL(b, DoB()) |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 888 EXPECT_CALL(a2, DoA(1)); | 909 EXPECT_CALL(a2, DoA(1)); |
| 889 EXPECT_CALL(a2, DoA(3)); | 910 EXPECT_CALL(a2, DoA(3)); |
| 890 a2.DoA(1); | 911 a2.DoA(1); |
| 891 EXPECT_NONFATAL_FAILURE( | 912 EXPECT_NONFATAL_FAILURE( |
| 892 a2.DoA(2), | 913 a2.DoA(2), |
| 893 "Unexpected mock function call - returning directly.\n" | 914 "Unexpected mock function call - returning directly.\n" |
| 894 " Function call: DoA(2)\n" | 915 " Function call: DoA(2)\n" |
| 895 "Google Mock tried the following 2 expectations, but none matched:"); | 916 "Google Mock tried the following 2 expectations, but none matched:"); |
| 896 EXPECT_NONFATAL_FAILURE( | 917 EXPECT_NONFATAL_FAILURE( |
| 897 a2.DoA(2), | 918 a2.DoA(2), |
| 898 "tried expectation #0\n" | 919 "tried expectation #0: EXPECT_CALL(a2, DoA(1))...\n" |
| 899 " Expected arg #0: is equal to 1\n" | 920 " Expected arg #0: is equal to 1\n" |
| 900 " Actual: 2\n" | 921 " Actual: 2\n" |
| 901 " Expected: to be called once\n" | 922 " Expected: to be called once\n" |
| 902 " Actual: called once - saturated and active"); | 923 " Actual: called once - saturated and active"); |
| 903 EXPECT_NONFATAL_FAILURE( | 924 EXPECT_NONFATAL_FAILURE( |
| 904 a2.DoA(2), | 925 a2.DoA(2), |
| 905 "tried expectation #1\n" | 926 "tried expectation #1: EXPECT_CALL(a2, DoA(3))...\n" |
| 906 " Expected arg #0: is equal to 3\n" | 927 " Expected arg #0: is equal to 3\n" |
| 907 " Actual: 2\n" | 928 " Actual: 2\n" |
| 908 " Expected: to be called once\n" | 929 " Expected: to be called once\n" |
| 909 " Actual: never called - unsatisfied and active"); | 930 " Actual: never called - unsatisfied and active"); |
| 910 a2.DoA(3); | 931 a2.DoA(3); |
| 911 } | 932 } |
| 912 | 933 |
| 913 // Tests that an unexpected non-void function generates the right | 934 // Tests that an unexpected non-void function generates the right |
| 914 // failure message. | 935 // failure message. |
| 915 TEST(UnexpectedCallTest, GeneartesFailureForNonVoidFunction) { | 936 TEST(UnexpectedCallTest, GeneartesFailureForNonVoidFunction) { |
| (...skipping 688 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1604 | 1625 |
| 1605 delete a; // a is trivially satisfied. | 1626 delete a; // a is trivially satisfied. |
| 1606 EXPECT_EQ(1, b1->DoB(1)); | 1627 EXPECT_EQ(1, b1->DoB(1)); |
| 1607 EXPECT_EQ(2, b2->DoB(2)); | 1628 EXPECT_EQ(2, b2->DoB(2)); |
| 1608 delete b1; | 1629 delete b1; |
| 1609 delete b2; | 1630 delete b2; |
| 1610 } | 1631 } |
| 1611 | 1632 |
| 1612 // Tests that it's OK to delete a mock object itself in its action. | 1633 // Tests that it's OK to delete a mock object itself in its action. |
| 1613 | 1634 |
| 1635 // Suppresses warning on unreferenced formal parameter in MSVC with |
| 1636 // -W4. |
| 1637 #ifdef _MSC_VER |
| 1638 #pragma warning(push) |
| 1639 #pragma warning(disable:4100) |
| 1640 #endif |
| 1641 |
| 1614 ACTION_P(Delete, ptr) { delete ptr; } | 1642 ACTION_P(Delete, ptr) { delete ptr; } |
| 1615 | 1643 |
| 1644 #ifdef _MSC_VER |
| 1645 #pragma warning(pop) |
| 1646 #endif |
| 1647 |
| 1616 TEST(DeletingMockEarlyTest, CanDeleteSelfInActionReturningVoid) { | 1648 TEST(DeletingMockEarlyTest, CanDeleteSelfInActionReturningVoid) { |
| 1617 MockA* const a = new MockA; | 1649 MockA* const a = new MockA; |
| 1618 EXPECT_CALL(*a, DoA(_)).WillOnce(Delete(a)); | 1650 EXPECT_CALL(*a, DoA(_)).WillOnce(Delete(a)); |
| 1619 a->DoA(42); // This will cause a to be deleted. | 1651 a->DoA(42); // This will cause a to be deleted. |
| 1620 } | 1652 } |
| 1621 | 1653 |
| 1622 TEST(DeletingMockEarlyTest, CanDeleteSelfInActionReturningValue) { | 1654 TEST(DeletingMockEarlyTest, CanDeleteSelfInActionReturningValue) { |
| 1623 MockA* const a = new MockA; | 1655 MockA* const a = new MockA; |
| 1624 EXPECT_CALL(*a, ReturnResult(_)) | 1656 EXPECT_CALL(*a, ReturnResult(_)) |
| 1625 .WillOnce(DoAll(Delete(a), Return(Result()))); | 1657 .WillOnce(DoAll(Delete(a), Return(Result()))); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1678 } | 1710 } |
| 1679 | 1711 |
| 1680 class EvenNumberCardinality : public CardinalityInterface { | 1712 class EvenNumberCardinality : public CardinalityInterface { |
| 1681 public: | 1713 public: |
| 1682 // Returns true iff call_count calls will satisfy this cardinality. | 1714 // Returns true iff call_count calls will satisfy this cardinality. |
| 1683 virtual bool IsSatisfiedByCallCount(int call_count) const { | 1715 virtual bool IsSatisfiedByCallCount(int call_count) const { |
| 1684 return call_count % 2 == 0; | 1716 return call_count % 2 == 0; |
| 1685 } | 1717 } |
| 1686 | 1718 |
| 1687 // Returns true iff call_count calls will saturate this cardinality. | 1719 // Returns true iff call_count calls will saturate this cardinality. |
| 1688 virtual bool IsSaturatedByCallCount(int call_count) const { return false; } | 1720 virtual bool IsSaturatedByCallCount(int /* call_count */) const { |
| 1721 return false; |
| 1722 } |
| 1689 | 1723 |
| 1690 // Describes self to an ostream. | 1724 // Describes self to an ostream. |
| 1691 virtual void DescribeTo(::std::ostream* os) const { | 1725 virtual void DescribeTo(::std::ostream* os) const { |
| 1692 *os << "called even number of times"; | 1726 *os << "called even number of times"; |
| 1693 } | 1727 } |
| 1694 }; | 1728 }; |
| 1695 | 1729 |
| 1696 Cardinality EvenNumber() { | 1730 Cardinality EvenNumber() { |
| 1697 return Cardinality(new EvenNumberCardinality); | 1731 return Cardinality(new EvenNumberCardinality); |
| 1698 } | 1732 } |
| (...skipping 28 matching lines...) Expand all Loading... |
| 1727 os << "Printable"; | 1761 os << "Printable"; |
| 1728 } | 1762 } |
| 1729 | 1763 |
| 1730 struct Unprintable { | 1764 struct Unprintable { |
| 1731 Unprintable() : value(0) {} | 1765 Unprintable() : value(0) {} |
| 1732 int value; | 1766 int value; |
| 1733 }; | 1767 }; |
| 1734 | 1768 |
| 1735 class MockC { | 1769 class MockC { |
| 1736 public: | 1770 public: |
| 1771 MockC() {} |
| 1772 |
| 1737 MOCK_METHOD6(VoidMethod, void(bool cond, int n, string s, void* p, | 1773 MOCK_METHOD6(VoidMethod, void(bool cond, int n, string s, void* p, |
| 1738 const Printable& x, Unprintable y)); | 1774 const Printable& x, Unprintable y)); |
| 1739 MOCK_METHOD0(NonVoidMethod, int()); // NOLINT | 1775 MOCK_METHOD0(NonVoidMethod, int()); // NOLINT |
| 1776 |
| 1777 private: |
| 1778 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockC); |
| 1740 }; | 1779 }; |
| 1741 | 1780 |
| 1742 // TODO(wan@google.com): find a way to re-enable these tests. | 1781 // TODO(wan@google.com): find a way to re-enable these tests. |
| 1743 #if 0 | 1782 #if 0 |
| 1744 | 1783 |
| 1745 // Tests that an uninteresting mock function call generates a warning | 1784 // Tests that an uninteresting mock function call generates a warning |
| 1746 // containing the stack trace. | 1785 // containing the stack trace. |
| 1747 TEST(FunctionCallMessageTest, UninterestingCallGeneratesFyiWithStackTrace) { | 1786 TEST(FunctionCallMessageTest, UninterestingCallGeneratesFyiWithStackTrace) { |
| 1748 MockC c; | 1787 MockC c; |
| 1749 CaptureTestStdout(); | 1788 CaptureTestStdout(); |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1922 // buffer) when it is not supposed to do so. | 1961 // buffer) when it is not supposed to do so. |
| 1923 class PrintMeNot {}; | 1962 class PrintMeNot {}; |
| 1924 | 1963 |
| 1925 void PrintTo(PrintMeNot /* dummy */, ::std::ostream* /* os */) { | 1964 void PrintTo(PrintMeNot /* dummy */, ::std::ostream* /* os */) { |
| 1926 ADD_FAILURE() << "Google Mock is printing a value that shouldn't be " | 1965 ADD_FAILURE() << "Google Mock is printing a value that shouldn't be " |
| 1927 << "printed even to an internal buffer."; | 1966 << "printed even to an internal buffer."; |
| 1928 } | 1967 } |
| 1929 | 1968 |
| 1930 class LogTestHelper { | 1969 class LogTestHelper { |
| 1931 public: | 1970 public: |
| 1971 LogTestHelper() {} |
| 1972 |
| 1932 MOCK_METHOD1(Foo, PrintMeNot(PrintMeNot)); | 1973 MOCK_METHOD1(Foo, PrintMeNot(PrintMeNot)); |
| 1974 |
| 1975 private: |
| 1976 GTEST_DISALLOW_COPY_AND_ASSIGN_(LogTestHelper); |
| 1933 }; | 1977 }; |
| 1934 | 1978 |
| 1935 class GMockLogTest : public ::testing::Test { | 1979 class GMockLogTest : public ::testing::Test { |
| 1936 protected: | 1980 protected: |
| 1937 virtual void SetUp() { | 1981 virtual void SetUp() { |
| 1938 // The code needs to work when both ::string and ::std::string are | 1982 // The code needs to work when both ::string and ::std::string are |
| 1939 // defined and the flag is implemented as a | 1983 // defined and the flag is implemented as a |
| 1940 // testing::internal::String. In this case, without the call to | 1984 // testing::internal::String. In this case, without the call to |
| 1941 // c_str(), the compiler will complain that it cannot figure out | 1985 // c_str(), the compiler will complain that it cannot figure out |
| 1942 // whether the String flag should be converted to a ::string or an | 1986 // whether the String flag should be converted to a ::string or an |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2039 EXPECT_EQ(0, b.DoB(1)); | 2083 EXPECT_EQ(0, b.DoB(1)); |
| 2040 } | 2084 } |
| 2041 | 2085 |
| 2042 // Tests that we can verify and clear a mock object's expectations | 2086 // Tests that we can verify and clear a mock object's expectations |
| 2043 // when some, but not all, of its methods have expectations *and* the | 2087 // when some, but not all, of its methods have expectations *and* the |
| 2044 // verification fails. | 2088 // verification fails. |
| 2045 TEST(VerifyAndClearExpectationsTest, SomeMethodsHaveExpectationsAndFail) { | 2089 TEST(VerifyAndClearExpectationsTest, SomeMethodsHaveExpectationsAndFail) { |
| 2046 MockB b; | 2090 MockB b; |
| 2047 EXPECT_CALL(b, DoB()) | 2091 EXPECT_CALL(b, DoB()) |
| 2048 .WillOnce(Return(1)); | 2092 .WillOnce(Return(1)); |
| 2049 bool result; | 2093 bool result = true; |
| 2050 EXPECT_NONFATAL_FAILURE(result = Mock::VerifyAndClearExpectations(&b), | 2094 EXPECT_NONFATAL_FAILURE(result = Mock::VerifyAndClearExpectations(&b), |
| 2051 "Actual: never called"); | 2095 "Actual: never called"); |
| 2052 ASSERT_FALSE(result); | 2096 ASSERT_FALSE(result); |
| 2053 | 2097 |
| 2054 // There should be no expectations on the methods now, so we can | 2098 // There should be no expectations on the methods now, so we can |
| 2055 // freely call them. | 2099 // freely call them. |
| 2056 EXPECT_EQ(0, b.DoB()); | 2100 EXPECT_EQ(0, b.DoB()); |
| 2057 EXPECT_EQ(0, b.DoB(1)); | 2101 EXPECT_EQ(0, b.DoB(1)); |
| 2058 } | 2102 } |
| 2059 | 2103 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 2077 | 2121 |
| 2078 // Tests that we can verify and clear a mock object's expectations | 2122 // Tests that we can verify and clear a mock object's expectations |
| 2079 // when a method has more than one expectation. | 2123 // when a method has more than one expectation. |
| 2080 TEST(VerifyAndClearExpectationsTest, AMethodHasManyExpectations) { | 2124 TEST(VerifyAndClearExpectationsTest, AMethodHasManyExpectations) { |
| 2081 MockB b; | 2125 MockB b; |
| 2082 EXPECT_CALL(b, DoB(0)) | 2126 EXPECT_CALL(b, DoB(0)) |
| 2083 .WillOnce(Return(1)); | 2127 .WillOnce(Return(1)); |
| 2084 EXPECT_CALL(b, DoB(_)) | 2128 EXPECT_CALL(b, DoB(_)) |
| 2085 .WillOnce(Return(2)); | 2129 .WillOnce(Return(2)); |
| 2086 b.DoB(1); | 2130 b.DoB(1); |
| 2087 bool result; | 2131 bool result = true; |
| 2088 EXPECT_NONFATAL_FAILURE(result = Mock::VerifyAndClearExpectations(&b), | 2132 EXPECT_NONFATAL_FAILURE(result = Mock::VerifyAndClearExpectations(&b), |
| 2089 "Actual: never called"); | 2133 "Actual: never called"); |
| 2090 ASSERT_FALSE(result); | 2134 ASSERT_FALSE(result); |
| 2091 | 2135 |
| 2092 // There should be no expectations on the methods now, so we can | 2136 // There should be no expectations on the methods now, so we can |
| 2093 // freely call them. | 2137 // freely call them. |
| 2094 EXPECT_EQ(0, b.DoB()); | 2138 EXPECT_EQ(0, b.DoB()); |
| 2095 EXPECT_EQ(0, b.DoB(1)); | 2139 EXPECT_EQ(0, b.DoB(1)); |
| 2096 } | 2140 } |
| 2097 | 2141 |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2209 | 2253 |
| 2210 // Tests that VerifyAndClear() works when the verification fails. | 2254 // Tests that VerifyAndClear() works when the verification fails. |
| 2211 TEST(VerifyAndClearTest, Failure) { | 2255 TEST(VerifyAndClearTest, Failure) { |
| 2212 MockB b; | 2256 MockB b; |
| 2213 ON_CALL(b, DoB(_)) | 2257 ON_CALL(b, DoB(_)) |
| 2214 .WillByDefault(Return(1)); | 2258 .WillByDefault(Return(1)); |
| 2215 EXPECT_CALL(b, DoB()) | 2259 EXPECT_CALL(b, DoB()) |
| 2216 .WillOnce(Return(2)); | 2260 .WillOnce(Return(2)); |
| 2217 | 2261 |
| 2218 b.DoB(1); | 2262 b.DoB(1); |
| 2219 bool result; | 2263 bool result = true; |
| 2220 EXPECT_NONFATAL_FAILURE(result = Mock::VerifyAndClear(&b), | 2264 EXPECT_NONFATAL_FAILURE(result = Mock::VerifyAndClear(&b), |
| 2221 "Actual: never called"); | 2265 "Actual: never called"); |
| 2222 ASSERT_FALSE(result); | 2266 ASSERT_FALSE(result); |
| 2223 | 2267 |
| 2224 // There should be no expectations on the methods now, so we can | 2268 // There should be no expectations on the methods now, so we can |
| 2225 // freely call them. | 2269 // freely call them. |
| 2226 EXPECT_EQ(0, b.DoB()); | 2270 EXPECT_EQ(0, b.DoB()); |
| 2227 EXPECT_EQ(0, b.DoB(1)); | 2271 EXPECT_EQ(0, b.DoB(1)); |
| 2228 } | 2272 } |
| 2229 | 2273 |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2325 #endif // GMOCK_RENAME_MAIN | 2369 #endif // GMOCK_RENAME_MAIN |
| 2326 testing::InitGoogleMock(&argc, argv); | 2370 testing::InitGoogleMock(&argc, argv); |
| 2327 | 2371 |
| 2328 // Ensures that the tests pass no matter what value of | 2372 // Ensures that the tests pass no matter what value of |
| 2329 // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies. | 2373 // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies. |
| 2330 testing::GMOCK_FLAG(catch_leaked_mocks) = true; | 2374 testing::GMOCK_FLAG(catch_leaked_mocks) = true; |
| 2331 testing::GMOCK_FLAG(verbose) = testing::internal::kWarningVerbosity; | 2375 testing::GMOCK_FLAG(verbose) = testing::internal::kWarningVerbosity; |
| 2332 | 2376 |
| 2333 return RUN_ALL_TESTS(); | 2377 return RUN_ALL_TESTS(); |
| 2334 } | 2378 } |
| OLD | NEW |