| 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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 | 61 |
| 62 namespace { | 62 namespace { |
| 63 | 63 |
| 64 using testing::_; | 64 using testing::_; |
| 65 using testing::AnyNumber; | 65 using testing::AnyNumber; |
| 66 using testing::AtLeast; | 66 using testing::AtLeast; |
| 67 using testing::AtMost; | 67 using testing::AtMost; |
| 68 using testing::Between; | 68 using testing::Between; |
| 69 using testing::Cardinality; | 69 using testing::Cardinality; |
| 70 using testing::CardinalityInterface; | 70 using testing::CardinalityInterface; |
| 71 using testing::ContainsRegex; |
| 71 using testing::Const; | 72 using testing::Const; |
| 72 using testing::DoAll; | 73 using testing::DoAll; |
| 73 using testing::DoDefault; | 74 using testing::DoDefault; |
| 74 using testing::Eq; | 75 using testing::Eq; |
| 75 using testing::Expectation; | 76 using testing::Expectation; |
| 76 using testing::ExpectationSet; | 77 using testing::ExpectationSet; |
| 77 using testing::GMOCK_FLAG(verbose); | 78 using testing::GMOCK_FLAG(verbose); |
| 78 using testing::Gt; | 79 using testing::Gt; |
| 79 using testing::InSequence; | 80 using testing::InSequence; |
| 80 using testing::Invoke; | 81 using testing::Invoke; |
| 81 using testing::InvokeWithoutArgs; | 82 using testing::InvokeWithoutArgs; |
| 82 using testing::IsSubstring; | 83 using testing::IsSubstring; |
| 83 using testing::Lt; | 84 using testing::Lt; |
| 84 using testing::Message; | 85 using testing::Message; |
| 85 using testing::Mock; | 86 using testing::Mock; |
| 86 using testing::Ne; | 87 using testing::Ne; |
| 87 using testing::Return; | 88 using testing::Return; |
| 88 using testing::Sequence; | 89 using testing::Sequence; |
| 90 using testing::internal::ExpectationTester; |
| 89 using testing::internal::g_gmock_mutex; | 91 using testing::internal::g_gmock_mutex; |
| 90 using testing::internal::kErrorVerbosity; | 92 using testing::internal::kErrorVerbosity; |
| 91 using testing::internal::kInfoVerbosity; | 93 using testing::internal::kInfoVerbosity; |
| 92 using testing::internal::kWarningVerbosity; | 94 using testing::internal::kWarningVerbosity; |
| 93 using testing::internal::ExpectationTester; | 95 using testing::internal::String; |
| 94 using testing::internal::string; | 96 using testing::internal::string; |
| 95 | 97 |
| 98 #if GTEST_HAS_STREAM_REDIRECTION_ |
| 99 using testing::HasSubstr; |
| 100 using testing::internal::CaptureStdout; |
| 101 using testing::internal::GetCapturedStdout; |
| 102 #endif // GTEST_HAS_STREAM_REDIRECTION_ |
| 103 |
| 96 class Result {}; | 104 class Result {}; |
| 97 | 105 |
| 98 class MockA { | 106 class MockA { |
| 99 public: | 107 public: |
| 100 MockA() {} | 108 MockA() {} |
| 101 | 109 |
| 102 MOCK_METHOD1(DoA, void(int n)); // NOLINT | 110 MOCK_METHOD1(DoA, void(int n)); // NOLINT |
| 103 MOCK_METHOD1(ReturnResult, Result(int n)); // NOLINT | 111 MOCK_METHOD1(ReturnResult, Result(int n)); // NOLINT |
| 104 MOCK_METHOD2(Binary, bool(int x, int y)); // NOLINT | 112 MOCK_METHOD2(Binary, bool(int x, int y)); // NOLINT |
| 105 MOCK_METHOD2(ReturnInt, int(int x, int y)); // NOLINT | 113 MOCK_METHOD2(ReturnInt, int(int x, int y)); // NOLINT |
| (...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 503 EXPECT_CALL(a, DoA(1)); | 511 EXPECT_CALL(a, DoA(1)); |
| 504 }, "to be called once"); | 512 }, "to be called once"); |
| 505 EXPECT_NONFATAL_FAILURE({ // NOLINT | 513 EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 506 MockA a; | 514 MockA a; |
| 507 EXPECT_CALL(a, DoA(1)); | 515 EXPECT_CALL(a, DoA(1)); |
| 508 a.DoA(1); | 516 a.DoA(1); |
| 509 a.DoA(1); | 517 a.DoA(1); |
| 510 }, "to be called once"); | 518 }, "to be called once"); |
| 511 } | 519 } |
| 512 | 520 |
| 513 // TODO(wan@google.com): find a way to re-enable these tests. | 521 #if GTEST_HAS_STREAM_REDIRECTION_ |
| 514 #if 0 | |
| 515 | 522 |
| 516 // Tests that Google Mock doesn't print a warning when the number of | 523 // Tests that Google Mock doesn't print a warning when the number of |
| 517 // WillOnce() is adequate. | 524 // WillOnce() is adequate. |
| 518 TEST(ExpectCallSyntaxTest, DoesNotWarnOnAdequateActionCount) { | 525 TEST(ExpectCallSyntaxTest, DoesNotWarnOnAdequateActionCount) { |
| 519 CaptureTestStdout(); | 526 CaptureStdout(); |
| 520 { | 527 { |
| 521 MockB b; | 528 MockB b; |
| 522 | 529 |
| 523 // It's always fine to omit WillOnce() entirely. | 530 // It's always fine to omit WillOnce() entirely. |
| 524 EXPECT_CALL(b, DoB()) | 531 EXPECT_CALL(b, DoB()) |
| 525 .Times(0); | 532 .Times(0); |
| 526 EXPECT_CALL(b, DoB(1)) | 533 EXPECT_CALL(b, DoB(1)) |
| 527 .Times(AtMost(1)); | 534 .Times(AtMost(1)); |
| 528 EXPECT_CALL(b, DoB(2)) | 535 EXPECT_CALL(b, DoB(2)) |
| 529 .Times(1) | 536 .Times(1) |
| 530 .WillRepeatedly(Return(1)); | 537 .WillRepeatedly(Return(1)); |
| 531 | 538 |
| 532 // It's fine for the number of WillOnce()s to equal the upper bound. | 539 // It's fine for the number of WillOnce()s to equal the upper bound. |
| 533 EXPECT_CALL(b, DoB(3)) | 540 EXPECT_CALL(b, DoB(3)) |
| 534 .Times(Between(1, 2)) | 541 .Times(Between(1, 2)) |
| 535 .WillOnce(Return(1)) | 542 .WillOnce(Return(1)) |
| 536 .WillOnce(Return(2)); | 543 .WillOnce(Return(2)); |
| 537 | 544 |
| 538 // It's fine for the number of WillOnce()s to be smaller than the | 545 // It's fine for the number of WillOnce()s to be smaller than the |
| 539 // upper bound when there is a WillRepeatedly(). | 546 // upper bound when there is a WillRepeatedly(). |
| 540 EXPECT_CALL(b, DoB(4)) | 547 EXPECT_CALL(b, DoB(4)) |
| 541 .Times(AtMost(3)) | 548 .Times(AtMost(3)) |
| 542 .WillOnce(Return(1)) | 549 .WillOnce(Return(1)) |
| 543 .WillRepeatedly(Return(2)); | 550 .WillRepeatedly(Return(2)); |
| 544 | 551 |
| 545 // Satisfies the above expectations. | 552 // Satisfies the above expectations. |
| 546 b.DoB(2); | 553 b.DoB(2); |
| 547 b.DoB(3); | 554 b.DoB(3); |
| 548 } | 555 } |
| 549 const string& output = GetCapturedTestStdout(); | 556 EXPECT_STREQ("", GetCapturedStdout().c_str()); |
| 550 EXPECT_EQ("", output); | |
| 551 } | 557 } |
| 552 | 558 |
| 553 // Tests that Google Mock warns on having too many actions in an | 559 // Tests that Google Mock warns on having too many actions in an |
| 554 // expectation compared to its cardinality. | 560 // expectation compared to its cardinality. |
| 555 TEST(ExpectCallSyntaxTest, WarnsOnTooManyActions) { | 561 TEST(ExpectCallSyntaxTest, WarnsOnTooManyActions) { |
| 556 CaptureTestStdout(); | 562 CaptureStdout(); |
| 557 { | 563 { |
| 558 MockB b; | 564 MockB b; |
| 559 | 565 |
| 560 // Warns when the number of WillOnce()s is larger than the upper bound. | 566 // Warns when the number of WillOnce()s is larger than the upper bound. |
| 561 EXPECT_CALL(b, DoB()) | 567 EXPECT_CALL(b, DoB()) |
| 562 .Times(0) | 568 .Times(0) |
| 563 .WillOnce(Return(1)); // #1 | 569 .WillOnce(Return(1)); // #1 |
| 564 EXPECT_CALL(b, DoB()) | 570 EXPECT_CALL(b, DoB()) |
| 565 .Times(AtMost(1)) | 571 .Times(AtMost(1)) |
| 566 .WillOnce(Return(1)) | 572 .WillOnce(Return(1)) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 578 .WillRepeatedly(Return(1)); // #4 | 584 .WillRepeatedly(Return(1)); // #4 |
| 579 EXPECT_CALL(b, DoB(2)) | 585 EXPECT_CALL(b, DoB(2)) |
| 580 .Times(1) | 586 .Times(1) |
| 581 .WillOnce(Return(1)) | 587 .WillOnce(Return(1)) |
| 582 .WillRepeatedly(Return(2)); // #5 | 588 .WillRepeatedly(Return(2)); // #5 |
| 583 | 589 |
| 584 // Satisfies the above expectations. | 590 // Satisfies the above expectations. |
| 585 b.DoB(1); | 591 b.DoB(1); |
| 586 b.DoB(2); | 592 b.DoB(2); |
| 587 } | 593 } |
| 588 const string& output = GetCapturedTestStdout(); | 594 const String output = GetCapturedStdout(); |
| 589 EXPECT_PRED_FORMAT2( | 595 EXPECT_PRED_FORMAT2( |
| 590 IsSubstring, | 596 IsSubstring, |
| 591 "Too many actions specified in EXPECT_CALL(b, DoB())...\n" | 597 "Too many actions specified in EXPECT_CALL(b, DoB())...\n" |
| 592 "Expected to be never called, but has 1 WillOnce().", | 598 "Expected to be never called, but has 1 WillOnce().", |
| 593 output); // #1 | 599 output); // #1 |
| 594 EXPECT_PRED_FORMAT2( | 600 EXPECT_PRED_FORMAT2( |
| 595 IsSubstring, | 601 IsSubstring, |
| 596 "Too many actions specified in EXPECT_CALL(b, DoB())...\n" | 602 "Too many actions specified in EXPECT_CALL(b, DoB())...\n" |
| 597 "Expected to be called at most once, " | 603 "Expected to be called at most once, " |
| 598 "but has 2 WillOnce()s.", | 604 "but has 2 WillOnce()s.", |
| (...skipping 19 matching lines...) Expand all Loading... |
| 618 | 624 |
| 619 // Tests that Google Mock warns on having too few actions in an | 625 // Tests that Google Mock warns on having too few actions in an |
| 620 // expectation compared to its cardinality. | 626 // expectation compared to its cardinality. |
| 621 TEST(ExpectCallSyntaxTest, WarnsOnTooFewActions) { | 627 TEST(ExpectCallSyntaxTest, WarnsOnTooFewActions) { |
| 622 MockB b; | 628 MockB b; |
| 623 | 629 |
| 624 EXPECT_CALL(b, DoB()) | 630 EXPECT_CALL(b, DoB()) |
| 625 .Times(Between(2, 3)) | 631 .Times(Between(2, 3)) |
| 626 .WillOnce(Return(1)); | 632 .WillOnce(Return(1)); |
| 627 | 633 |
| 628 CaptureTestStdout(); | 634 CaptureStdout(); |
| 629 b.DoB(); | 635 b.DoB(); |
| 630 const string& output = GetCapturedTestStdout(); | 636 const String output = GetCapturedStdout(); |
| 631 EXPECT_PRED_FORMAT2( | 637 EXPECT_PRED_FORMAT2( |
| 632 IsSubstring, | 638 IsSubstring, |
| 633 "Too few actions specified in EXPECT_CALL(b, DoB())...\n" | 639 "Too few actions specified in EXPECT_CALL(b, DoB())...\n" |
| 634 "Expected to be called between 2 and 3 times, " | 640 "Expected to be called between 2 and 3 times, " |
| 635 "but has only 1 WillOnce().", | 641 "but has only 1 WillOnce().", |
| 636 output); | 642 output); |
| 637 b.DoB(); | 643 b.DoB(); |
| 638 } | 644 } |
| 639 | 645 |
| 640 #endif // 0 | 646 #endif // GTEST_HAS_STREAM_REDIRECTION_ |
| 641 | 647 |
| 642 // Tests the semantics of ON_CALL(). | 648 // Tests the semantics of ON_CALL(). |
| 643 | 649 |
| 644 // Tests that the built-in default action is taken when no ON_CALL() | 650 // Tests that the built-in default action is taken when no ON_CALL() |
| 645 // is specified. | 651 // is specified. |
| 646 TEST(OnCallTest, TakesBuiltInDefaultActionWhenNoOnCall) { | 652 TEST(OnCallTest, TakesBuiltInDefaultActionWhenNoOnCall) { |
| 647 MockB b; | 653 MockB b; |
| 648 EXPECT_CALL(b, DoB()); | 654 EXPECT_CALL(b, DoB()); |
| 649 | 655 |
| 650 EXPECT_EQ(0, b.DoB()); | 656 EXPECT_EQ(0, b.DoB()); |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 784 EXPECT_CALL(b, DoB()) | 790 EXPECT_CALL(b, DoB()) |
| 785 .WillOnce(Return(1)) | 791 .WillOnce(Return(1)) |
| 786 .WillOnce(Return(2)) | 792 .WillOnce(Return(2)) |
| 787 .WillOnce(Return(3)); | 793 .WillOnce(Return(3)); |
| 788 | 794 |
| 789 EXPECT_EQ(1, b.DoB()); | 795 EXPECT_EQ(1, b.DoB()); |
| 790 EXPECT_EQ(2, b.DoB()); | 796 EXPECT_EQ(2, b.DoB()); |
| 791 EXPECT_EQ(3, b.DoB()); | 797 EXPECT_EQ(3, b.DoB()); |
| 792 } | 798 } |
| 793 | 799 |
| 794 // TODO(wan@google.com): find a way to re-enable these tests. | 800 #if GTEST_HAS_STREAM_REDIRECTION_ |
| 795 #if 0 | |
| 796 | 801 |
| 797 // Tests that the default action is taken when the WillOnce(...) list is | 802 // Tests that the default action is taken when the WillOnce(...) list is |
| 798 // exhausted and there is no WillRepeatedly(). | 803 // exhausted and there is no WillRepeatedly(). |
| 799 TEST(ExpectCallTest, TakesDefaultActionWhenWillListIsExhausted) { | 804 TEST(ExpectCallTest, TakesDefaultActionWhenWillListIsExhausted) { |
| 800 MockB b; | 805 MockB b; |
| 801 EXPECT_CALL(b, DoB(_)) | 806 EXPECT_CALL(b, DoB(_)) |
| 802 .Times(1); | 807 .Times(1); |
| 803 EXPECT_CALL(b, DoB()) | 808 EXPECT_CALL(b, DoB()) |
| 804 .Times(AnyNumber()) | 809 .Times(AnyNumber()) |
| 805 .WillOnce(Return(1)) | 810 .WillOnce(Return(1)) |
| 806 .WillOnce(Return(2)); | 811 .WillOnce(Return(2)); |
| 807 | 812 |
| 808 CaptureTestStdout(); | 813 CaptureStdout(); |
| 809 EXPECT_EQ(0, b.DoB(1)); // Shouldn't generate a warning as the | 814 EXPECT_EQ(0, b.DoB(1)); // Shouldn't generate a warning as the |
| 810 // expectation has no action clause at all. | 815 // expectation has no action clause at all. |
| 811 EXPECT_EQ(1, b.DoB()); | 816 EXPECT_EQ(1, b.DoB()); |
| 812 EXPECT_EQ(2, b.DoB()); | 817 EXPECT_EQ(2, b.DoB()); |
| 813 const string& output1 = GetCapturedTestStdout(); | 818 const String output1 = GetCapturedStdout(); |
| 814 EXPECT_EQ("", output1); | 819 EXPECT_STREQ("", output1.c_str()); |
| 815 | 820 |
| 816 CaptureTestStdout(); | 821 CaptureStdout(); |
| 817 EXPECT_EQ(0, b.DoB()); | 822 EXPECT_EQ(0, b.DoB()); |
| 818 EXPECT_EQ(0, b.DoB()); | 823 EXPECT_EQ(0, b.DoB()); |
| 819 const string& output2 = GetCapturedTestStdout(); | 824 const String output2 = GetCapturedStdout(); |
| 820 EXPECT_PRED2(RE::PartialMatch, output2, | 825 EXPECT_THAT(output2.c_str(), |
| 821 "Actions ran out\\.\n" | 826 HasSubstr("Actions ran out in EXPECT_CALL(b, DoB())...\n" |
| 822 "Called 3 times, but only 2 WillOnce\\(\\)s are specified - " | 827 "Called 3 times, but only 2 WillOnce()s are specified" |
| 823 "returning default value\\."); | 828 " - returning default value.")); |
| 824 EXPECT_PRED2(RE::PartialMatch, output2, | 829 EXPECT_THAT(output2.c_str(), |
| 825 "Actions ran out\\.\n" | 830 HasSubstr("Actions ran out in EXPECT_CALL(b, DoB())...\n" |
| 826 "Called 4 times, but only 2 WillOnce\\(\\)s are specified - " | 831 "Called 4 times, but only 2 WillOnce()s are specified" |
| 827 "returning default value\\."); | 832 " - returning default value.")); |
| 828 } | 833 } |
| 829 | 834 |
| 830 #endif // 0 | 835 #endif // GTEST_HAS_STREAM_REDIRECTION_ |
| 831 | 836 |
| 832 // Tests that the WillRepeatedly() action is taken when the WillOnce(...) | 837 // Tests that the WillRepeatedly() action is taken when the WillOnce(...) |
| 833 // list is exhausted. | 838 // list is exhausted. |
| 834 TEST(ExpectCallTest, TakesRepeatedActionWhenWillListIsExhausted) { | 839 TEST(ExpectCallTest, TakesRepeatedActionWhenWillListIsExhausted) { |
| 835 MockB b; | 840 MockB b; |
| 836 EXPECT_CALL(b, DoB()) | 841 EXPECT_CALL(b, DoB()) |
| 837 .WillOnce(Return(1)) | 842 .WillOnce(Return(1)) |
| 838 .WillRepeatedly(Return(2)); | 843 .WillRepeatedly(Return(2)); |
| 839 | 844 |
| 840 EXPECT_EQ(1, b.DoB()); | 845 EXPECT_EQ(1, b.DoB()); |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 971 MockB b; | 976 MockB b; |
| 972 EXPECT_CALL(b, DoB(1)); | 977 EXPECT_CALL(b, DoB(1)); |
| 973 | 978 |
| 974 EXPECT_NONFATAL_FAILURE( | 979 EXPECT_NONFATAL_FAILURE( |
| 975 b.DoB(2), | 980 b.DoB(2), |
| 976 " Expected arg #0: is equal to 1\n" | 981 " Expected arg #0: is equal to 1\n" |
| 977 " Actual: 2\n"); | 982 " Actual: 2\n"); |
| 978 b.DoB(1); | 983 b.DoB(1); |
| 979 } | 984 } |
| 980 | 985 |
| 981 #ifdef GMOCK_HAS_REGEX | |
| 982 | |
| 983 // Tests that Google Mock explains that an expectation with | 986 // Tests that Google Mock explains that an expectation with |
| 984 // unsatisfied pre-requisites doesn't match the call. | 987 // unsatisfied pre-requisites doesn't match the call. |
| 985 TEST(UnexpectedCallTest, UnsatisifiedPrerequisites) { | 988 TEST(UnexpectedCallTest, UnsatisifiedPrerequisites) { |
| 986 Sequence s1, s2; | 989 Sequence s1, s2; |
| 987 MockB b; | 990 MockB b; |
| 988 EXPECT_CALL(b, DoB(1)) | 991 EXPECT_CALL(b, DoB(1)) |
| 989 .InSequence(s1); | 992 .InSequence(s1); |
| 990 EXPECT_CALL(b, DoB(2)) | 993 EXPECT_CALL(b, DoB(2)) |
| 991 .Times(AnyNumber()) | 994 .Times(AnyNumber()) |
| 992 .InSequence(s1); | 995 .InSequence(s1); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 1003 // the above statement. | 1006 // the above statement. |
| 1004 } | 1007 } |
| 1005 | 1008 |
| 1006 // There should be one non-fatal failure. | 1009 // There should be one non-fatal failure. |
| 1007 ASSERT_EQ(1, failures.size()); | 1010 ASSERT_EQ(1, failures.size()); |
| 1008 const ::testing::TestPartResult& r = failures.GetTestPartResult(0); | 1011 const ::testing::TestPartResult& r = failures.GetTestPartResult(0); |
| 1009 EXPECT_EQ(::testing::TestPartResult::kNonFatalFailure, r.type()); | 1012 EXPECT_EQ(::testing::TestPartResult::kNonFatalFailure, r.type()); |
| 1010 | 1013 |
| 1011 // Verifies that the failure message contains the two unsatisfied | 1014 // Verifies that the failure message contains the two unsatisfied |
| 1012 // pre-requisites but not the satisfied one. | 1015 // pre-requisites but not the satisfied one. |
| 1013 const char* const pattern = | 1016 #if GTEST_USES_PCRE |
| 1014 #if GMOCK_USES_PCRE | 1017 EXPECT_THAT(r.message(), ContainsRegex( |
| 1015 // PCRE has trouble using (.|\n) to match any character, but | 1018 // PCRE has trouble using (.|\n) to match any character, but |
| 1016 // supports the (?s) prefix for using . to match any character. | 1019 // supports the (?s) prefix for using . to match any character. |
| 1017 "(?s)the following immediate pre-requisites are not satisfied:\n" | 1020 "(?s)the following immediate pre-requisites are not satisfied:\n" |
| 1018 ".*: pre-requisite #0\n" | 1021 ".*: pre-requisite #0\n" |
| 1019 ".*: pre-requisite #1"; | 1022 ".*: pre-requisite #1")); |
| 1020 #else | 1023 #elif GTEST_USES_POSIX_RE |
| 1024 EXPECT_THAT(r.message(), ContainsRegex( |
| 1021 // POSIX RE doesn't understand the (?s) prefix, but has no trouble | 1025 // POSIX RE doesn't understand the (?s) prefix, but has no trouble |
| 1022 // with (.|\n). | 1026 // with (.|\n). |
| 1023 "the following immediate pre-requisites are not satisfied:\n" | 1027 "the following immediate pre-requisites are not satisfied:\n" |
| 1024 "(.|\n)*: pre-requisite #0\n" | 1028 "(.|\n)*: pre-requisite #0\n" |
| 1025 "(.|\n)*: pre-requisite #1"; | 1029 "(.|\n)*: pre-requisite #1")); |
| 1026 #endif // GMOCK_USES_PCRE | 1030 #else |
| 1031 // We can only use Google Test's own simple regex. |
| 1032 EXPECT_THAT(r.message(), ContainsRegex( |
| 1033 "the following immediate pre-requisites are not satisfied:")); |
| 1034 EXPECT_THAT(r.message(), ContainsRegex(": pre-requisite #0")); |
| 1035 EXPECT_THAT(r.message(), ContainsRegex(": pre-requisite #1")); |
| 1036 #endif // GTEST_USES_PCRE |
| 1027 | 1037 |
| 1028 EXPECT_TRUE( | |
| 1029 ::testing::internal::RE::PartialMatch(r.message(), pattern)) | |
| 1030 << " where the message is " << r.message(); | |
| 1031 b.DoB(1); | 1038 b.DoB(1); |
| 1032 b.DoB(3); | 1039 b.DoB(3); |
| 1033 b.DoB(4); | 1040 b.DoB(4); |
| 1034 } | 1041 } |
| 1035 | 1042 |
| 1036 #endif // GMOCK_HAS_REGEX | |
| 1037 | |
| 1038 TEST(UndefinedReturnValueTest, ReturnValueIsMandatory) { | 1043 TEST(UndefinedReturnValueTest, ReturnValueIsMandatory) { |
| 1039 MockA a; | 1044 MockA a; |
| 1040 // TODO(wan@google.com): We should really verify the output message, | 1045 // TODO(wan@google.com): We should really verify the output message, |
| 1041 // but we cannot yet due to that EXPECT_DEATH only captures stderr | 1046 // but we cannot yet due to that EXPECT_DEATH only captures stderr |
| 1042 // while Google Mock logs to stdout. | 1047 // while Google Mock logs to stdout. |
| 1043 EXPECT_DEATH_IF_SUPPORTED(a.ReturnResult(1), ""); | 1048 EXPECT_DEATH_IF_SUPPORTED(a.ReturnResult(1), ""); |
| 1044 } | 1049 } |
| 1045 | 1050 |
| 1046 // Tests that an excessive call (one whose arguments match the | 1051 // Tests that an excessive call (one whose arguments match the |
| 1047 // matchers but is called too many times) performs the default action. | 1052 // matchers but is called too many times) performs the default action. |
| (...skipping 723 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1771 MockC() {} | 1776 MockC() {} |
| 1772 | 1777 |
| 1773 MOCK_METHOD6(VoidMethod, void(bool cond, int n, string s, void* p, | 1778 MOCK_METHOD6(VoidMethod, void(bool cond, int n, string s, void* p, |
| 1774 const Printable& x, Unprintable y)); | 1779 const Printable& x, Unprintable y)); |
| 1775 MOCK_METHOD0(NonVoidMethod, int()); // NOLINT | 1780 MOCK_METHOD0(NonVoidMethod, int()); // NOLINT |
| 1776 | 1781 |
| 1777 private: | 1782 private: |
| 1778 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockC); | 1783 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockC); |
| 1779 }; | 1784 }; |
| 1780 | 1785 |
| 1781 // TODO(wan@google.com): find a way to re-enable these tests. | 1786 #if GTEST_HAS_STREAM_REDIRECTION_ |
| 1782 #if 0 | |
| 1783 | 1787 |
| 1784 // Tests that an uninteresting mock function call generates a warning | 1788 // Tests that an uninteresting mock function call generates a warning |
| 1785 // containing the stack trace. | 1789 // containing the stack trace. |
| 1786 TEST(FunctionCallMessageTest, UninterestingCallGeneratesFyiWithStackTrace) { | 1790 TEST(FunctionCallMessageTest, UninterestingCallGeneratesFyiWithStackTrace) { |
| 1787 MockC c; | 1791 MockC c; |
| 1788 CaptureTestStdout(); | 1792 CaptureStdout(); |
| 1789 c.VoidMethod(false, 5, "Hi", NULL, Printable(), Unprintable()); | 1793 c.VoidMethod(false, 5, "Hi", NULL, Printable(), Unprintable()); |
| 1790 const string& output = GetCapturedTestStdout(); | 1794 const String output = GetCapturedStdout(); |
| 1791 EXPECT_PRED_FORMAT2(IsSubstring, "GMOCK WARNING", output); | 1795 EXPECT_PRED_FORMAT2(IsSubstring, "GMOCK WARNING", output); |
| 1792 EXPECT_PRED_FORMAT2(IsSubstring, "Stack trace:", output); | 1796 EXPECT_PRED_FORMAT2(IsSubstring, "Stack trace:", output); |
| 1793 #ifndef NDEBUG | 1797 #ifndef NDEBUG |
| 1794 // We check the stack trace content in dbg-mode only, as opt-mode | 1798 // We check the stack trace content in dbg-mode only, as opt-mode |
| 1795 // may inline the call we are interested in seeing. | 1799 // may inline the call we are interested in seeing. |
| 1796 | 1800 |
| 1797 // Verifies that a void mock function's name appears in the stack | 1801 // Verifies that a void mock function's name appears in the stack |
| 1798 // trace. | 1802 // trace. |
| 1799 EXPECT_PRED_FORMAT2(IsSubstring, "::MockC::VoidMethod(", output); | 1803 EXPECT_PRED_FORMAT2(IsSubstring, "VoidMethod(", output); |
| 1800 | 1804 |
| 1801 // Verifies that a non-void mock function's name appears in the | 1805 // Verifies that a non-void mock function's name appears in the |
| 1802 // stack trace. | 1806 // stack trace. |
| 1803 CaptureTestStdout(); | 1807 CaptureStdout(); |
| 1804 c.NonVoidMethod(); | 1808 c.NonVoidMethod(); |
| 1805 const string& output2 = GetCapturedTestStdout(); | 1809 const String output2 = GetCapturedStdout(); |
| 1806 EXPECT_PRED_FORMAT2(IsSubstring, "::MockC::NonVoidMethod(", output2); | 1810 EXPECT_PRED_FORMAT2(IsSubstring, "NonVoidMethod(", output2); |
| 1807 #endif // NDEBUG | 1811 #endif // NDEBUG |
| 1808 } | 1812 } |
| 1809 | 1813 |
| 1810 // Tests that an uninteresting mock function call causes the function | 1814 // Tests that an uninteresting mock function call causes the function |
| 1811 // arguments and return value to be printed. | 1815 // arguments and return value to be printed. |
| 1812 TEST(FunctionCallMessageTest, UninterestingCallPrintsArgumentsAndReturnValue) { | 1816 TEST(FunctionCallMessageTest, UninterestingCallPrintsArgumentsAndReturnValue) { |
| 1813 // A non-void mock function. | 1817 // A non-void mock function. |
| 1814 MockB b; | 1818 MockB b; |
| 1815 CaptureTestStdout(); | 1819 CaptureStdout(); |
| 1816 b.DoB(); | 1820 b.DoB(); |
| 1817 const string& output1 = GetCapturedTestStdout(); | 1821 const String output1 = GetCapturedStdout(); |
| 1818 EXPECT_PRED_FORMAT2( | 1822 EXPECT_PRED_FORMAT2( |
| 1819 IsSubstring, | 1823 IsSubstring, |
| 1820 "Uninteresting mock function call - returning default value.\n" | 1824 "Uninteresting mock function call - returning default value.\n" |
| 1821 " Function call: DoB()\n" | 1825 " Function call: DoB()\n" |
| 1822 " Returns: 0\n", output1); | 1826 " Returns: 0\n", output1.c_str()); |
| 1823 // Makes sure the return value is printed. | 1827 // Makes sure the return value is printed. |
| 1824 | 1828 |
| 1825 // A void mock function. | 1829 // A void mock function. |
| 1826 MockC c; | 1830 MockC c; |
| 1827 CaptureTestStdout(); | 1831 CaptureStdout(); |
| 1828 c.VoidMethod(false, 5, "Hi", NULL, Printable(), Unprintable()); | 1832 c.VoidMethod(false, 5, "Hi", NULL, Printable(), Unprintable()); |
| 1829 const string& output2 = GetCapturedTestStdout(); | 1833 const String output2 = GetCapturedStdout(); |
| 1830 EXPECT_PRED2(RE::PartialMatch, output2, | 1834 EXPECT_THAT(output2.c_str(), |
| 1831 "Uninteresting mock function call - returning directly\\.\n" | 1835 ContainsRegex( |
| 1832 " Function call: VoidMethod" | 1836 "Uninteresting mock function call - returning directly\\.\n" |
| 1833 "\\(false, 5, \"Hi\", NULL, @0x\\w+ " | 1837 " Function call: VoidMethod" |
| 1834 "Printable, 4-byte object <0000 0000>\\)"); | 1838 "\\(false, 5, \"Hi\", NULL, @.+ " |
| 1839 "Printable, 4-byte object <0000 0000>\\)")); |
| 1835 // A void function has no return value to print. | 1840 // A void function has no return value to print. |
| 1836 } | 1841 } |
| 1837 | 1842 |
| 1838 // Tests how the --gmock_verbose flag affects Google Mock's output. | 1843 // Tests how the --gmock_verbose flag affects Google Mock's output. |
| 1839 | 1844 |
| 1840 class GMockVerboseFlagTest : public testing::Test { | 1845 class GMockVerboseFlagTest : public testing::Test { |
| 1841 public: | 1846 public: |
| 1842 // Verifies that the given Google Mock output is correct. (When | 1847 // Verifies that the given Google Mock output is correct. (When |
| 1843 // should_print is true, the output should match the given regex and | 1848 // should_print is true, the output should match the given regex and |
| 1844 // contain the given function name in the stack trace. When it's | 1849 // contain the given function name in the stack trace. When it's |
| 1845 // false, the output should be empty.) | 1850 // false, the output should be empty.) |
| 1846 void VerifyOutput(const string& output, bool should_print, | 1851 void VerifyOutput(const String& output, bool should_print, |
| 1847 const string& regex, | 1852 const string& expected_substring, |
| 1848 const string& function_name) { | 1853 const string& function_name) { |
| 1849 if (should_print) { | 1854 if (should_print) { |
| 1850 EXPECT_PRED2(RE::PartialMatch, output, regex); | 1855 EXPECT_THAT(output.c_str(), HasSubstr(expected_substring)); |
| 1851 #ifndef NDEBUG | 1856 #ifndef NDEBUG |
| 1852 // We check the stack trace content in dbg-mode only, as opt-mode | 1857 // We check the stack trace content in dbg-mode only, as opt-mode |
| 1853 // may inline the call we are interested in seeing. | 1858 // may inline the call we are interested in seeing. |
| 1854 EXPECT_PRED_FORMAT2(IsSubstring, function_name, output); | 1859 EXPECT_THAT(output.c_str(), HasSubstr(function_name)); |
| 1860 #else |
| 1861 // Suppresses 'unused function parameter' warnings. |
| 1862 static_cast<void>(function_name); |
| 1855 #endif // NDEBUG | 1863 #endif // NDEBUG |
| 1856 } else { | 1864 } else { |
| 1857 EXPECT_EQ("", output); | 1865 EXPECT_STREQ("", output.c_str()); |
| 1858 } | 1866 } |
| 1859 } | 1867 } |
| 1860 | 1868 |
| 1861 // Tests how the flag affects expected calls. | 1869 // Tests how the flag affects expected calls. |
| 1862 void TestExpectedCall(bool should_print) { | 1870 void TestExpectedCall(bool should_print) { |
| 1863 MockA a; | 1871 MockA a; |
| 1864 EXPECT_CALL(a, DoA(5)); | 1872 EXPECT_CALL(a, DoA(5)); |
| 1865 EXPECT_CALL(a, Binary(_, 1)) | 1873 EXPECT_CALL(a, Binary(_, 1)) |
| 1866 .WillOnce(Return(true)); | 1874 .WillOnce(Return(true)); |
| 1867 | 1875 |
| 1868 // A void-returning function. | 1876 // A void-returning function. |
| 1869 CaptureTestStdout(); | 1877 CaptureStdout(); |
| 1870 a.DoA(5); | 1878 a.DoA(5); |
| 1871 VerifyOutput( | 1879 VerifyOutput( |
| 1872 GetCapturedTestStdout(), | 1880 GetCapturedStdout(), |
| 1873 should_print, | 1881 should_print, |
| 1874 "Expected mock function call\\.\n" | 1882 "Mock function call matches EXPECT_CALL(a, DoA(5))...\n" |
| 1875 " Function call: DoA\\(5\\)\n" | 1883 " Function call: DoA(5)\n" |
| 1876 "Stack trace:", | 1884 "Stack trace:\n", |
| 1877 "MockA::DoA"); | 1885 "DoA"); |
| 1878 | 1886 |
| 1879 // A non-void-returning function. | 1887 // A non-void-returning function. |
| 1880 CaptureTestStdout(); | 1888 CaptureStdout(); |
| 1881 a.Binary(2, 1); | 1889 a.Binary(2, 1); |
| 1882 VerifyOutput( | 1890 VerifyOutput( |
| 1883 GetCapturedTestStdout(), | 1891 GetCapturedStdout(), |
| 1884 should_print, | 1892 should_print, |
| 1885 "Expected mock function call\\.\n" | 1893 "Mock function call matches EXPECT_CALL(a, Binary(_, 1))...\n" |
| 1886 " Function call: Binary\\(2, 1\\)\n" | 1894 " Function call: Binary(2, 1)\n" |
| 1887 " Returns: true\n" | 1895 " Returns: true\n" |
| 1888 "Stack trace:", | 1896 "Stack trace:\n", |
| 1889 "MockA::Binary"); | 1897 "Binary"); |
| 1890 } | 1898 } |
| 1891 | 1899 |
| 1892 // Tests how the flag affects uninteresting calls. | 1900 // Tests how the flag affects uninteresting calls. |
| 1893 void TestUninterestingCall(bool should_print) { | 1901 void TestUninterestingCall(bool should_print) { |
| 1894 MockA a; | 1902 MockA a; |
| 1895 | 1903 |
| 1896 // A void-returning function. | 1904 // A void-returning function. |
| 1897 CaptureTestStdout(); | 1905 CaptureStdout(); |
| 1898 a.DoA(5); | 1906 a.DoA(5); |
| 1899 VerifyOutput( | 1907 VerifyOutput( |
| 1900 GetCapturedTestStdout(), | 1908 GetCapturedStdout(), |
| 1901 should_print, | 1909 should_print, |
| 1902 "\nGMOCK WARNING:\n" | 1910 "\nGMOCK WARNING:\n" |
| 1903 "Uninteresting mock function call - returning directly\\.\n" | 1911 "Uninteresting mock function call - returning directly.\n" |
| 1904 " Function call: DoA\\(5\\)\n" | 1912 " Function call: DoA(5)\n" |
| 1905 "Stack trace:\n" | 1913 "Stack trace:\n", |
| 1906 "[\\s\\S]*", | 1914 "DoA"); |
| 1907 "MockA::DoA"); | |
| 1908 | 1915 |
| 1909 // A non-void-returning function. | 1916 // A non-void-returning function. |
| 1910 CaptureTestStdout(); | 1917 CaptureStdout(); |
| 1911 a.Binary(2, 1); | 1918 a.Binary(2, 1); |
| 1912 VerifyOutput( | 1919 VerifyOutput( |
| 1913 GetCapturedTestStdout(), | 1920 GetCapturedStdout(), |
| 1914 should_print, | 1921 should_print, |
| 1915 "\nGMOCK WARNING:\n" | 1922 "\nGMOCK WARNING:\n" |
| 1916 "Uninteresting mock function call - returning default value\\.\n" | 1923 "Uninteresting mock function call - returning default value.\n" |
| 1917 " Function call: Binary\\(2, 1\\)\n" | 1924 " Function call: Binary(2, 1)\n" |
| 1918 " Returns: false\n" | 1925 " Returns: false\n" |
| 1919 "Stack trace:\n" | 1926 "Stack trace:\n", |
| 1920 "[\\s\\S]*", | 1927 "Binary"); |
| 1921 "MockA::Binary"); | |
| 1922 } | 1928 } |
| 1923 }; | 1929 }; |
| 1924 | 1930 |
| 1925 // Tests that --gmock_verbose=info causes both expected and | 1931 // Tests that --gmock_verbose=info causes both expected and |
| 1926 // uninteresting calls to be reported. | 1932 // uninteresting calls to be reported. |
| 1927 TEST_F(GMockVerboseFlagTest, Info) { | 1933 TEST_F(GMockVerboseFlagTest, Info) { |
| 1928 GMOCK_FLAG(verbose) = kInfoVerbosity; | 1934 GMOCK_FLAG(verbose) = kInfoVerbosity; |
| 1929 TestExpectedCall(true); | 1935 TestExpectedCall(true); |
| 1930 TestUninterestingCall(true); | 1936 TestUninterestingCall(true); |
| 1931 } | 1937 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 1947 } | 1953 } |
| 1948 | 1954 |
| 1949 // Tests that --gmock_verbose=SOME_INVALID_VALUE has the same effect | 1955 // Tests that --gmock_verbose=SOME_INVALID_VALUE has the same effect |
| 1950 // as --gmock_verbose=warning. | 1956 // as --gmock_verbose=warning. |
| 1951 TEST_F(GMockVerboseFlagTest, InvalidFlagIsTreatedAsWarning) { | 1957 TEST_F(GMockVerboseFlagTest, InvalidFlagIsTreatedAsWarning) { |
| 1952 GMOCK_FLAG(verbose) = "invalid"; // Treated as "warning". | 1958 GMOCK_FLAG(verbose) = "invalid"; // Treated as "warning". |
| 1953 TestExpectedCall(false); | 1959 TestExpectedCall(false); |
| 1954 TestUninterestingCall(true); | 1960 TestUninterestingCall(true); |
| 1955 } | 1961 } |
| 1956 | 1962 |
| 1957 #endif // 0 | 1963 #endif // GTEST_HAS_STREAM_REDIRECTION_ |
| 1958 | 1964 |
| 1959 // A helper class that generates a failure when printed. We use it to | 1965 // A helper class that generates a failure when printed. We use it to |
| 1960 // ensure that Google Mock doesn't print a value (even to an internal | 1966 // ensure that Google Mock doesn't print a value (even to an internal |
| 1961 // buffer) when it is not supposed to do so. | 1967 // buffer) when it is not supposed to do so. |
| 1962 class PrintMeNot {}; | 1968 class PrintMeNot {}; |
| 1963 | 1969 |
| 1964 void PrintTo(PrintMeNot /* dummy */, ::std::ostream* /* os */) { | 1970 void PrintTo(PrintMeNot /* dummy */, ::std::ostream* /* os */) { |
| 1965 ADD_FAILURE() << "Google Mock is printing a value that shouldn't be " | 1971 ADD_FAILURE() << "Google Mock is printing a value that shouldn't be " |
| 1966 << "printed even to an internal buffer."; | 1972 << "printed even to an internal buffer."; |
| 1967 } | 1973 } |
| (...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2369 #endif // GMOCK_RENAME_MAIN | 2375 #endif // GMOCK_RENAME_MAIN |
| 2370 testing::InitGoogleMock(&argc, argv); | 2376 testing::InitGoogleMock(&argc, argv); |
| 2371 | 2377 |
| 2372 // Ensures that the tests pass no matter what value of | 2378 // Ensures that the tests pass no matter what value of |
| 2373 // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies. | 2379 // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies. |
| 2374 testing::GMOCK_FLAG(catch_leaked_mocks) = true; | 2380 testing::GMOCK_FLAG(catch_leaked_mocks) = true; |
| 2375 testing::GMOCK_FLAG(verbose) = testing::internal::kWarningVerbosity; | 2381 testing::GMOCK_FLAG(verbose) = testing::internal::kWarningVerbosity; |
| 2376 | 2382 |
| 2377 return RUN_ALL_TESTS(); | 2383 return RUN_ALL_TESTS(); |
| 2378 } | 2384 } |
| OLD | NEW |