00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "gmock/gmock-spec-builders.h"
00037
00038 #include <ostream>
00039 #include <sstream>
00040 #include <string>
00041
00042 #include "gmock/gmock.h"
00043 #include "gmock/internal/gmock-port.h"
00044 #include "gtest/gtest.h"
00045 #include "gtest/gtest-spi.h"
00046 #include "gtest/internal/gtest-port.h"
00047
00048 namespace testing {
00049 namespace internal {
00050
00051
00052 class ExpectationTester {
00053 public:
00054
00055 void SetCallCount(int n, ExpectationBase* exp) {
00056 exp->call_count_ = n;
00057 }
00058 };
00059
00060 }
00061 }
00062
00063 namespace {
00064
00065 using testing::_;
00066 using testing::AnyNumber;
00067 using testing::AtLeast;
00068 using testing::AtMost;
00069 using testing::Between;
00070 using testing::Cardinality;
00071 using testing::CardinalityInterface;
00072 using testing::ContainsRegex;
00073 using testing::Const;
00074 using testing::DoAll;
00075 using testing::DoDefault;
00076 using testing::Eq;
00077 using testing::Expectation;
00078 using testing::ExpectationSet;
00079 using testing::GMOCK_FLAG(verbose);
00080 using testing::Gt;
00081 using testing::InSequence;
00082 using testing::Invoke;
00083 using testing::InvokeWithoutArgs;
00084 using testing::IsSubstring;
00085 using testing::Lt;
00086 using testing::Message;
00087 using testing::Mock;
00088 using testing::NaggyMock;
00089 using testing::Ne;
00090 using testing::Return;
00091 using testing::Sequence;
00092 using testing::SetArgPointee;
00093 using testing::internal::ExpectationTester;
00094 using testing::internal::FormatFileLocation;
00095 using testing::internal::kErrorVerbosity;
00096 using testing::internal::kInfoVerbosity;
00097 using testing::internal::kWarningVerbosity;
00098 using testing::internal::linked_ptr;
00099 using testing::internal::string;
00100
00101 #if GTEST_HAS_STREAM_REDIRECTION
00102 using testing::HasSubstr;
00103 using testing::internal::CaptureStdout;
00104 using testing::internal::GetCapturedStdout;
00105 #endif
00106
00107 class Incomplete;
00108
00109 class MockIncomplete {
00110 public:
00111
00112
00113 MOCK_METHOD1(ByRefFunc, void(const Incomplete& x));
00114 };
00115
00116
00117 void PrintTo(const Incomplete& x, ::std::ostream* os);
00118
00119 TEST(MockMethodTest, CanInstantiateWithIncompleteArgType) {
00120
00121
00122
00123
00124 MockIncomplete incomplete;
00125 EXPECT_CALL(incomplete, ByRefFunc(_))
00126 .Times(AnyNumber());
00127 }
00128
00129
00130
00131 void PrintTo(const Incomplete& , ::std::ostream* os) {
00132 *os << "incomplete";
00133 }
00134
00135 class Result {};
00136
00137 class MockA {
00138 public:
00139 MockA() {}
00140
00141 MOCK_METHOD1(DoA, void(int n));
00142 MOCK_METHOD1(ReturnResult, Result(int n));
00143 MOCK_METHOD2(Binary, bool(int x, int y));
00144 MOCK_METHOD2(ReturnInt, int(int x, int y));
00145
00146 private:
00147 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockA);
00148 };
00149
00150 class MockB {
00151 public:
00152 MockB() {}
00153
00154 MOCK_CONST_METHOD0(DoB, int());
00155 MOCK_METHOD1(DoB, int(int n));
00156
00157 private:
00158 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockB);
00159 };
00160
00161 class ReferenceHoldingMock {
00162 public:
00163 ReferenceHoldingMock() {}
00164
00165 MOCK_METHOD1(AcceptReference, void(linked_ptr<MockA>*));
00166
00167 private:
00168 GTEST_DISALLOW_COPY_AND_ASSIGN_(ReferenceHoldingMock);
00169 };
00170
00171
00172
00173
00174
00175
00176 #define Method MethodW
00177
00178 class CC {
00179 public:
00180 virtual ~CC() {}
00181 virtual int Method() = 0;
00182 };
00183 class MockCC : public CC {
00184 public:
00185 MockCC() {}
00186
00187 MOCK_METHOD0(Method, int());
00188
00189 private:
00190 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockCC);
00191 };
00192
00193
00194 TEST(OnCallSyntaxTest, CompilesWithMethodNameExpandedFromMacro) {
00195 MockCC cc;
00196 ON_CALL(cc, Method());
00197 }
00198
00199
00200
00201 TEST(OnCallSyntaxTest, WorksWithMethodNameExpandedFromMacro) {
00202 MockCC cc;
00203 ON_CALL(cc, Method()).WillByDefault(Return(42));
00204 EXPECT_EQ(42, cc.Method());
00205 }
00206
00207
00208 TEST(ExpectCallSyntaxTest, CompilesWithMethodNameExpandedFromMacro) {
00209 MockCC cc;
00210 EXPECT_CALL(cc, Method());
00211 cc.Method();
00212 }
00213
00214
00215 TEST(ExpectCallSyntaxTest, WorksWithMethodNameExpandedFromMacro) {
00216 MockCC cc;
00217 EXPECT_CALL(cc, Method()).WillOnce(Return(42));
00218 EXPECT_EQ(42, cc.Method());
00219 }
00220
00221 #undef Method // Done with macro redefinition tests.
00222
00223
00224
00225 TEST(OnCallSyntaxTest, EvaluatesFirstArgumentOnce) {
00226 MockA a;
00227 MockA* pa = &a;
00228
00229 ON_CALL(*pa++, DoA(_));
00230 EXPECT_EQ(&a + 1, pa);
00231 }
00232
00233 TEST(OnCallSyntaxTest, EvaluatesSecondArgumentOnce) {
00234 MockA a;
00235 int n = 0;
00236
00237 ON_CALL(a, DoA(n++));
00238 EXPECT_EQ(1, n);
00239 }
00240
00241
00242
00243 TEST(OnCallSyntaxTest, WithIsOptional) {
00244 MockA a;
00245
00246 ON_CALL(a, DoA(5))
00247 .WillByDefault(Return());
00248 ON_CALL(a, DoA(_))
00249 .With(_)
00250 .WillByDefault(Return());
00251 }
00252
00253 TEST(OnCallSyntaxTest, WithCanAppearAtMostOnce) {
00254 MockA a;
00255
00256 EXPECT_NONFATAL_FAILURE({
00257 ON_CALL(a, ReturnResult(_))
00258 .With(_)
00259 .With(_)
00260 .WillByDefault(Return(Result()));
00261 }, ".With() cannot appear more than once in an ON_CALL()");
00262 }
00263
00264 TEST(OnCallSyntaxTest, WillByDefaultIsMandatory) {
00265 MockA a;
00266
00267 EXPECT_DEATH_IF_SUPPORTED({
00268 ON_CALL(a, DoA(5));
00269 a.DoA(5);
00270 }, "");
00271 }
00272
00273 TEST(OnCallSyntaxTest, WillByDefaultCanAppearAtMostOnce) {
00274 MockA a;
00275
00276 EXPECT_NONFATAL_FAILURE({
00277 ON_CALL(a, DoA(5))
00278 .WillByDefault(Return())
00279 .WillByDefault(Return());
00280 }, ".WillByDefault() must appear exactly once in an ON_CALL()");
00281 }
00282
00283
00284
00285 TEST(ExpectCallSyntaxTest, EvaluatesFirstArgumentOnce) {
00286 MockA a;
00287 MockA* pa = &a;
00288
00289 EXPECT_CALL(*pa++, DoA(_));
00290 a.DoA(0);
00291 EXPECT_EQ(&a + 1, pa);
00292 }
00293
00294 TEST(ExpectCallSyntaxTest, EvaluatesSecondArgumentOnce) {
00295 MockA a;
00296 int n = 0;
00297
00298 EXPECT_CALL(a, DoA(n++));
00299 a.DoA(0);
00300 EXPECT_EQ(1, n);
00301 }
00302
00303
00304
00305 TEST(ExpectCallSyntaxTest, WithIsOptional) {
00306 MockA a;
00307
00308 EXPECT_CALL(a, DoA(5))
00309 .Times(0);
00310 EXPECT_CALL(a, DoA(6))
00311 .With(_)
00312 .Times(0);
00313 }
00314
00315 TEST(ExpectCallSyntaxTest, WithCanAppearAtMostOnce) {
00316 MockA a;
00317
00318 EXPECT_NONFATAL_FAILURE({
00319 EXPECT_CALL(a, DoA(6))
00320 .With(_)
00321 .With(_);
00322 }, ".With() cannot appear more than once in an EXPECT_CALL()");
00323
00324 a.DoA(6);
00325 }
00326
00327 TEST(ExpectCallSyntaxTest, WithMustBeFirstClause) {
00328 MockA a;
00329
00330 EXPECT_NONFATAL_FAILURE({
00331 EXPECT_CALL(a, DoA(1))
00332 .Times(1)
00333 .With(_);
00334 }, ".With() must be the first clause in an EXPECT_CALL()");
00335
00336 a.DoA(1);
00337
00338 EXPECT_NONFATAL_FAILURE({
00339 EXPECT_CALL(a, DoA(2))
00340 .WillOnce(Return())
00341 .With(_);
00342 }, ".With() must be the first clause in an EXPECT_CALL()");
00343
00344 a.DoA(2);
00345 }
00346
00347 TEST(ExpectCallSyntaxTest, TimesCanBeInferred) {
00348 MockA a;
00349
00350 EXPECT_CALL(a, DoA(1))
00351 .WillOnce(Return());
00352
00353 EXPECT_CALL(a, DoA(2))
00354 .WillOnce(Return())
00355 .WillRepeatedly(Return());
00356
00357 a.DoA(1);
00358 a.DoA(2);
00359 a.DoA(2);
00360 }
00361
00362 TEST(ExpectCallSyntaxTest, TimesCanAppearAtMostOnce) {
00363 MockA a;
00364
00365 EXPECT_NONFATAL_FAILURE({
00366 EXPECT_CALL(a, DoA(1))
00367 .Times(1)
00368 .Times(2);
00369 }, ".Times() cannot appear more than once in an EXPECT_CALL()");
00370
00371 a.DoA(1);
00372 a.DoA(1);
00373 }
00374
00375 TEST(ExpectCallSyntaxTest, TimesMustBeBeforeInSequence) {
00376 MockA a;
00377 Sequence s;
00378
00379 EXPECT_NONFATAL_FAILURE({
00380 EXPECT_CALL(a, DoA(1))
00381 .InSequence(s)
00382 .Times(1);
00383 }, ".Times() cannot appear after ");
00384
00385 a.DoA(1);
00386 }
00387
00388 TEST(ExpectCallSyntaxTest, InSequenceIsOptional) {
00389 MockA a;
00390 Sequence s;
00391
00392 EXPECT_CALL(a, DoA(1));
00393 EXPECT_CALL(a, DoA(2))
00394 .InSequence(s);
00395
00396 a.DoA(1);
00397 a.DoA(2);
00398 }
00399
00400 TEST(ExpectCallSyntaxTest, InSequenceCanAppearMultipleTimes) {
00401 MockA a;
00402 Sequence s1, s2;
00403
00404 EXPECT_CALL(a, DoA(1))
00405 .InSequence(s1, s2)
00406 .InSequence(s1);
00407
00408 a.DoA(1);
00409 }
00410
00411 TEST(ExpectCallSyntaxTest, InSequenceMustBeBeforeAfter) {
00412 MockA a;
00413 Sequence s;
00414
00415 Expectation e = EXPECT_CALL(a, DoA(1))
00416 .Times(AnyNumber());
00417 EXPECT_NONFATAL_FAILURE({
00418 EXPECT_CALL(a, DoA(2))
00419 .After(e)
00420 .InSequence(s);
00421 }, ".InSequence() cannot appear after ");
00422
00423 a.DoA(2);
00424 }
00425
00426 TEST(ExpectCallSyntaxTest, InSequenceMustBeBeforeWillOnce) {
00427 MockA a;
00428 Sequence s;
00429
00430 EXPECT_NONFATAL_FAILURE({
00431 EXPECT_CALL(a, DoA(1))
00432 .WillOnce(Return())
00433 .InSequence(s);
00434 }, ".InSequence() cannot appear after ");
00435
00436 a.DoA(1);
00437 }
00438
00439 TEST(ExpectCallSyntaxTest, AfterMustBeBeforeWillOnce) {
00440 MockA a;
00441
00442 Expectation e = EXPECT_CALL(a, DoA(1));
00443 EXPECT_NONFATAL_FAILURE({
00444 EXPECT_CALL(a, DoA(2))
00445 .WillOnce(Return())
00446 .After(e);
00447 }, ".After() cannot appear after ");
00448
00449 a.DoA(1);
00450 a.DoA(2);
00451 }
00452
00453 TEST(ExpectCallSyntaxTest, WillIsOptional) {
00454 MockA a;
00455
00456 EXPECT_CALL(a, DoA(1));
00457 EXPECT_CALL(a, DoA(2))
00458 .WillOnce(Return());
00459
00460 a.DoA(1);
00461 a.DoA(2);
00462 }
00463
00464 TEST(ExpectCallSyntaxTest, WillCanAppearMultipleTimes) {
00465 MockA a;
00466
00467 EXPECT_CALL(a, DoA(1))
00468 .Times(AnyNumber())
00469 .WillOnce(Return())
00470 .WillOnce(Return())
00471 .WillOnce(Return());
00472 }
00473
00474 TEST(ExpectCallSyntaxTest, WillMustBeBeforeWillRepeatedly) {
00475 MockA a;
00476
00477 EXPECT_NONFATAL_FAILURE({
00478 EXPECT_CALL(a, DoA(1))
00479 .WillRepeatedly(Return())
00480 .WillOnce(Return());
00481 }, ".WillOnce() cannot appear after ");
00482
00483 a.DoA(1);
00484 }
00485
00486 TEST(ExpectCallSyntaxTest, WillRepeatedlyIsOptional) {
00487 MockA a;
00488
00489 EXPECT_CALL(a, DoA(1))
00490 .WillOnce(Return());
00491 EXPECT_CALL(a, DoA(2))
00492 .WillOnce(Return())
00493 .WillRepeatedly(Return());
00494
00495 a.DoA(1);
00496 a.DoA(2);
00497 a.DoA(2);
00498 }
00499
00500 TEST(ExpectCallSyntaxTest, WillRepeatedlyCannotAppearMultipleTimes) {
00501 MockA a;
00502
00503 EXPECT_NONFATAL_FAILURE({
00504 EXPECT_CALL(a, DoA(1))
00505 .WillRepeatedly(Return())
00506 .WillRepeatedly(Return());
00507 }, ".WillRepeatedly() cannot appear more than once in an "
00508 "EXPECT_CALL()");
00509 }
00510
00511 TEST(ExpectCallSyntaxTest, WillRepeatedlyMustBeBeforeRetiresOnSaturation) {
00512 MockA a;
00513
00514 EXPECT_NONFATAL_FAILURE({
00515 EXPECT_CALL(a, DoA(1))
00516 .RetiresOnSaturation()
00517 .WillRepeatedly(Return());
00518 }, ".WillRepeatedly() cannot appear after ");
00519 }
00520
00521 TEST(ExpectCallSyntaxTest, RetiresOnSaturationIsOptional) {
00522 MockA a;
00523
00524 EXPECT_CALL(a, DoA(1));
00525 EXPECT_CALL(a, DoA(1))
00526 .RetiresOnSaturation();
00527
00528 a.DoA(1);
00529 a.DoA(1);
00530 }
00531
00532 TEST(ExpectCallSyntaxTest, RetiresOnSaturationCannotAppearMultipleTimes) {
00533 MockA a;
00534
00535 EXPECT_NONFATAL_FAILURE({
00536 EXPECT_CALL(a, DoA(1))
00537 .RetiresOnSaturation()
00538 .RetiresOnSaturation();
00539 }, ".RetiresOnSaturation() cannot appear more than once");
00540
00541 a.DoA(1);
00542 }
00543
00544 TEST(ExpectCallSyntaxTest, DefaultCardinalityIsOnce) {
00545 {
00546 MockA a;
00547 EXPECT_CALL(a, DoA(1));
00548 a.DoA(1);
00549 }
00550 EXPECT_NONFATAL_FAILURE({
00551 MockA a;
00552 EXPECT_CALL(a, DoA(1));
00553 }, "to be called once");
00554 EXPECT_NONFATAL_FAILURE({
00555 MockA a;
00556 EXPECT_CALL(a, DoA(1));
00557 a.DoA(1);
00558 a.DoA(1);
00559 }, "to be called once");
00560 }
00561
00562 #if GTEST_HAS_STREAM_REDIRECTION
00563
00564
00565
00566 TEST(ExpectCallSyntaxTest, DoesNotWarnOnAdequateActionCount) {
00567 CaptureStdout();
00568 {
00569 MockB b;
00570
00571
00572 EXPECT_CALL(b, DoB())
00573 .Times(0);
00574 EXPECT_CALL(b, DoB(1))
00575 .Times(AtMost(1));
00576 EXPECT_CALL(b, DoB(2))
00577 .Times(1)
00578 .WillRepeatedly(Return(1));
00579
00580
00581 EXPECT_CALL(b, DoB(3))
00582 .Times(Between(1, 2))
00583 .WillOnce(Return(1))
00584 .WillOnce(Return(2));
00585
00586
00587
00588 EXPECT_CALL(b, DoB(4))
00589 .Times(AtMost(3))
00590 .WillOnce(Return(1))
00591 .WillRepeatedly(Return(2));
00592
00593
00594 b.DoB(2);
00595 b.DoB(3);
00596 }
00597 EXPECT_STREQ("", GetCapturedStdout().c_str());
00598 }
00599
00600
00601
00602 TEST(ExpectCallSyntaxTest, WarnsOnTooManyActions) {
00603 CaptureStdout();
00604 {
00605 MockB b;
00606
00607
00608 EXPECT_CALL(b, DoB())
00609 .Times(0)
00610 .WillOnce(Return(1));
00611 EXPECT_CALL(b, DoB())
00612 .Times(AtMost(1))
00613 .WillOnce(Return(1))
00614 .WillOnce(Return(2));
00615 EXPECT_CALL(b, DoB(1))
00616 .Times(1)
00617 .WillOnce(Return(1))
00618 .WillOnce(Return(2))
00619 .RetiresOnSaturation();
00620
00621
00622
00623 EXPECT_CALL(b, DoB())
00624 .Times(0)
00625 .WillRepeatedly(Return(1));
00626 EXPECT_CALL(b, DoB(2))
00627 .Times(1)
00628 .WillOnce(Return(1))
00629 .WillRepeatedly(Return(2));
00630
00631
00632 b.DoB(1);
00633 b.DoB(2);
00634 }
00635 const std::string output = GetCapturedStdout();
00636 EXPECT_PRED_FORMAT2(
00637 IsSubstring,
00638 "Too many actions specified in EXPECT_CALL(b, DoB())...\n"
00639 "Expected to be never called, but has 1 WillOnce().",
00640 output);
00641 EXPECT_PRED_FORMAT2(
00642 IsSubstring,
00643 "Too many actions specified in EXPECT_CALL(b, DoB())...\n"
00644 "Expected to be called at most once, "
00645 "but has 2 WillOnce()s.",
00646 output);
00647 EXPECT_PRED_FORMAT2(
00648 IsSubstring,
00649 "Too many actions specified in EXPECT_CALL(b, DoB(1))...\n"
00650 "Expected to be called once, but has 2 WillOnce()s.",
00651 output);
00652 EXPECT_PRED_FORMAT2(
00653 IsSubstring,
00654 "Too many actions specified in EXPECT_CALL(b, DoB())...\n"
00655 "Expected to be never called, but has 0 WillOnce()s "
00656 "and a WillRepeatedly().",
00657 output);
00658 EXPECT_PRED_FORMAT2(
00659 IsSubstring,
00660 "Too many actions specified in EXPECT_CALL(b, DoB(2))...\n"
00661 "Expected to be called once, but has 1 WillOnce() "
00662 "and a WillRepeatedly().",
00663 output);
00664 }
00665
00666
00667
00668 TEST(ExpectCallSyntaxTest, WarnsOnTooFewActions) {
00669 MockB b;
00670
00671 EXPECT_CALL(b, DoB())
00672 .Times(Between(2, 3))
00673 .WillOnce(Return(1));
00674
00675 CaptureStdout();
00676 b.DoB();
00677 const std::string output = GetCapturedStdout();
00678 EXPECT_PRED_FORMAT2(
00679 IsSubstring,
00680 "Too few actions specified in EXPECT_CALL(b, DoB())...\n"
00681 "Expected to be called between 2 and 3 times, "
00682 "but has only 1 WillOnce().",
00683 output);
00684 b.DoB();
00685 }
00686
00687 #endif // GTEST_HAS_STREAM_REDIRECTION
00688
00689
00690
00691
00692
00693 TEST(OnCallTest, TakesBuiltInDefaultActionWhenNoOnCall) {
00694 MockB b;
00695 EXPECT_CALL(b, DoB());
00696
00697 EXPECT_EQ(0, b.DoB());
00698 }
00699
00700
00701
00702 TEST(OnCallTest, TakesBuiltInDefaultActionWhenNoOnCallMatches) {
00703 MockB b;
00704 ON_CALL(b, DoB(1))
00705 .WillByDefault(Return(1));
00706 EXPECT_CALL(b, DoB(_));
00707
00708 EXPECT_EQ(0, b.DoB(2));
00709 }
00710
00711
00712 TEST(OnCallTest, PicksLastMatchingOnCall) {
00713 MockB b;
00714 ON_CALL(b, DoB(_))
00715 .WillByDefault(Return(3));
00716 ON_CALL(b, DoB(2))
00717 .WillByDefault(Return(2));
00718 ON_CALL(b, DoB(1))
00719 .WillByDefault(Return(1));
00720 EXPECT_CALL(b, DoB(_));
00721
00722 EXPECT_EQ(2, b.DoB(2));
00723 }
00724
00725
00726
00727
00728 TEST(ExpectCallTest, AllowsAnyCallWhenNoSpec) {
00729 MockB b;
00730 EXPECT_CALL(b, DoB());
00731
00732
00733 b.DoB();
00734
00735
00736 b.DoB(1);
00737 b.DoB(2);
00738 }
00739
00740
00741 TEST(ExpectCallTest, PicksLastMatchingExpectCall) {
00742 MockB b;
00743 EXPECT_CALL(b, DoB(_))
00744 .WillRepeatedly(Return(2));
00745 EXPECT_CALL(b, DoB(1))
00746 .WillRepeatedly(Return(1));
00747
00748 EXPECT_EQ(1, b.DoB(1));
00749 }
00750
00751
00752 TEST(ExpectCallTest, CatchesTooFewCalls) {
00753 EXPECT_NONFATAL_FAILURE({
00754 MockB b;
00755 EXPECT_CALL(b, DoB(5))
00756 .Times(AtLeast(2));
00757
00758 b.DoB(5);
00759 }, "Actual function call count doesn't match EXPECT_CALL(b, DoB(5))...\n"
00760 " Expected: to be called at least twice\n"
00761 " Actual: called once - unsatisfied and active");
00762 }
00763
00764
00765
00766 TEST(ExpectCallTest, InfersCardinalityWhenThereIsNoWillRepeatedly) {
00767 {
00768 MockB b;
00769 EXPECT_CALL(b, DoB())
00770 .WillOnce(Return(1))
00771 .WillOnce(Return(2));
00772
00773 EXPECT_EQ(1, b.DoB());
00774 EXPECT_EQ(2, b.DoB());
00775 }
00776
00777 EXPECT_NONFATAL_FAILURE({
00778 MockB b;
00779 EXPECT_CALL(b, DoB())
00780 .WillOnce(Return(1))
00781 .WillOnce(Return(2));
00782
00783 EXPECT_EQ(1, b.DoB());
00784 }, "to be called twice");
00785
00786 {
00787 MockB b;
00788 EXPECT_CALL(b, DoB())
00789 .WillOnce(Return(1))
00790 .WillOnce(Return(2));
00791
00792 EXPECT_EQ(1, b.DoB());
00793 EXPECT_EQ(2, b.DoB());
00794 EXPECT_NONFATAL_FAILURE(b.DoB(), "to be called twice");
00795 }
00796 }
00797
00798 TEST(ExpectCallTest, InfersCardinality1WhenThereIsWillRepeatedly) {
00799 {
00800 MockB b;
00801 EXPECT_CALL(b, DoB())
00802 .WillOnce(Return(1))
00803 .WillRepeatedly(Return(2));
00804
00805 EXPECT_EQ(1, b.DoB());
00806 }
00807
00808 {
00809 MockB b;
00810 EXPECT_CALL(b, DoB())
00811 .WillOnce(Return(1))
00812 .WillRepeatedly(Return(2));
00813
00814 EXPECT_EQ(1, b.DoB());
00815 EXPECT_EQ(2, b.DoB());
00816 EXPECT_EQ(2, b.DoB());
00817 }
00818
00819 EXPECT_NONFATAL_FAILURE({
00820 MockB b;
00821 EXPECT_CALL(b, DoB())
00822 .WillOnce(Return(1))
00823 .WillRepeatedly(Return(2));
00824 }, "to be called at least once");
00825 }
00826
00827
00828
00829 TEST(ExpectCallTest, NthMatchTakesNthAction) {
00830 MockB b;
00831 EXPECT_CALL(b, DoB())
00832 .WillOnce(Return(1))
00833 .WillOnce(Return(2))
00834 .WillOnce(Return(3));
00835
00836 EXPECT_EQ(1, b.DoB());
00837 EXPECT_EQ(2, b.DoB());
00838 EXPECT_EQ(3, b.DoB());
00839 }
00840
00841
00842
00843 TEST(ExpectCallTest, TakesRepeatedActionWhenWillListIsExhausted) {
00844 MockB b;
00845 EXPECT_CALL(b, DoB())
00846 .WillOnce(Return(1))
00847 .WillRepeatedly(Return(2));
00848
00849 EXPECT_EQ(1, b.DoB());
00850 EXPECT_EQ(2, b.DoB());
00851 EXPECT_EQ(2, b.DoB());
00852 }
00853
00854 #if GTEST_HAS_STREAM_REDIRECTION
00855
00856
00857
00858 TEST(ExpectCallTest, TakesDefaultActionWhenWillListIsExhausted) {
00859 MockB b;
00860 EXPECT_CALL(b, DoB(_))
00861 .Times(1);
00862 EXPECT_CALL(b, DoB())
00863 .Times(AnyNumber())
00864 .WillOnce(Return(1))
00865 .WillOnce(Return(2));
00866
00867 CaptureStdout();
00868 EXPECT_EQ(0, b.DoB(1));
00869
00870 EXPECT_EQ(1, b.DoB());
00871 EXPECT_EQ(2, b.DoB());
00872 const std::string output1 = GetCapturedStdout();
00873 EXPECT_STREQ("", output1.c_str());
00874
00875 CaptureStdout();
00876 EXPECT_EQ(0, b.DoB());
00877 EXPECT_EQ(0, b.DoB());
00878 const std::string output2 = GetCapturedStdout();
00879 EXPECT_THAT(output2.c_str(),
00880 HasSubstr("Actions ran out in EXPECT_CALL(b, DoB())...\n"
00881 "Called 3 times, but only 2 WillOnce()s are specified"
00882 " - returning default value."));
00883 EXPECT_THAT(output2.c_str(),
00884 HasSubstr("Actions ran out in EXPECT_CALL(b, DoB())...\n"
00885 "Called 4 times, but only 2 WillOnce()s are specified"
00886 " - returning default value."));
00887 }
00888
00889 TEST(FunctionMockerMessageTest, ReportsExpectCallLocationForExhausedActions) {
00890 MockB b;
00891 std::string expect_call_location = FormatFileLocation(__FILE__, __LINE__ + 1);
00892 EXPECT_CALL(b, DoB()).Times(AnyNumber()).WillOnce(Return(1));
00893
00894 EXPECT_EQ(1, b.DoB());
00895
00896 CaptureStdout();
00897 EXPECT_EQ(0, b.DoB());
00898 const std::string output = GetCapturedStdout();
00899
00900 EXPECT_PRED_FORMAT2(IsSubstring, expect_call_location, output);
00901 }
00902
00903 TEST(FunctionMockerMessageTest,
00904 ReportsDefaultActionLocationOfUninterestingCallsForNaggyMock) {
00905 std::string on_call_location;
00906 CaptureStdout();
00907 {
00908 NaggyMock<MockB> b;
00909 on_call_location = FormatFileLocation(__FILE__, __LINE__ + 1);
00910 ON_CALL(b, DoB(_)).WillByDefault(Return(0));
00911 b.DoB(0);
00912 }
00913 EXPECT_PRED_FORMAT2(IsSubstring, on_call_location, GetCapturedStdout());
00914 }
00915
00916 #endif // GTEST_HAS_STREAM_REDIRECTION
00917
00918
00919 TEST(UninterestingCallTest, DoesDefaultAction) {
00920
00921
00922 MockA a;
00923 ON_CALL(a, Binary(_, _))
00924 .WillByDefault(Return(true));
00925 EXPECT_TRUE(a.Binary(1, 2));
00926
00927
00928
00929 MockB b;
00930 EXPECT_EQ(0, b.DoB());
00931 }
00932
00933
00934 TEST(UnexpectedCallTest, DoesDefaultAction) {
00935
00936
00937 MockA a;
00938 ON_CALL(a, Binary(_, _))
00939 .WillByDefault(Return(true));
00940 EXPECT_CALL(a, Binary(0, 0));
00941 a.Binary(0, 0);
00942 bool result = false;
00943 EXPECT_NONFATAL_FAILURE(result = a.Binary(1, 2),
00944 "Unexpected mock function call");
00945 EXPECT_TRUE(result);
00946
00947
00948
00949 MockB b;
00950 EXPECT_CALL(b, DoB(0))
00951 .Times(0);
00952 int n = -1;
00953 EXPECT_NONFATAL_FAILURE(n = b.DoB(1),
00954 "Unexpected mock function call");
00955 EXPECT_EQ(0, n);
00956 }
00957
00958
00959
00960 TEST(UnexpectedCallTest, GeneratesFailureForVoidFunction) {
00961
00962 MockA a1;
00963 EXPECT_CALL(a1, DoA(1));
00964 a1.DoA(1);
00965
00966
00967
00968 EXPECT_NONFATAL_FAILURE(
00969 a1.DoA(9),
00970 "Unexpected mock function call - returning directly.\n"
00971 " Function call: DoA(9)\n"
00972 "Google Mock tried the following 1 expectation, but it didn't match:");
00973 EXPECT_NONFATAL_FAILURE(
00974 a1.DoA(9),
00975 " Expected arg #0: is equal to 1\n"
00976 " Actual: 9\n"
00977 " Expected: to be called once\n"
00978 " Actual: called once - saturated and active");
00979
00980
00981 MockA a2;
00982 EXPECT_CALL(a2, DoA(1));
00983 EXPECT_CALL(a2, DoA(3));
00984 a2.DoA(1);
00985 EXPECT_NONFATAL_FAILURE(
00986 a2.DoA(2),
00987 "Unexpected mock function call - returning directly.\n"
00988 " Function call: DoA(2)\n"
00989 "Google Mock tried the following 2 expectations, but none matched:");
00990 EXPECT_NONFATAL_FAILURE(
00991 a2.DoA(2),
00992 "tried expectation #0: EXPECT_CALL(a2, DoA(1))...\n"
00993 " Expected arg #0: is equal to 1\n"
00994 " Actual: 2\n"
00995 " Expected: to be called once\n"
00996 " Actual: called once - saturated and active");
00997 EXPECT_NONFATAL_FAILURE(
00998 a2.DoA(2),
00999 "tried expectation #1: EXPECT_CALL(a2, DoA(3))...\n"
01000 " Expected arg #0: is equal to 3\n"
01001 " Actual: 2\n"
01002 " Expected: to be called once\n"
01003 " Actual: never called - unsatisfied and active");
01004 a2.DoA(3);
01005 }
01006
01007
01008
01009 TEST(UnexpectedCallTest, GeneartesFailureForNonVoidFunction) {
01010 MockB b1;
01011 EXPECT_CALL(b1, DoB(1));
01012 b1.DoB(1);
01013 EXPECT_NONFATAL_FAILURE(
01014 b1.DoB(2),
01015 "Unexpected mock function call - returning default value.\n"
01016 " Function call: DoB(2)\n"
01017 " Returns: 0\n"
01018 "Google Mock tried the following 1 expectation, but it didn't match:");
01019 EXPECT_NONFATAL_FAILURE(
01020 b1.DoB(2),
01021 " Expected arg #0: is equal to 1\n"
01022 " Actual: 2\n"
01023 " Expected: to be called once\n"
01024 " Actual: called once - saturated and active");
01025 }
01026
01027
01028
01029 TEST(UnexpectedCallTest, RetiredExpectation) {
01030 MockB b;
01031 EXPECT_CALL(b, DoB(1))
01032 .RetiresOnSaturation();
01033
01034 b.DoB(1);
01035 EXPECT_NONFATAL_FAILURE(
01036 b.DoB(1),
01037 " Expected: the expectation is active\n"
01038 " Actual: it is retired");
01039 }
01040
01041
01042
01043 TEST(UnexpectedCallTest, UnmatchedArguments) {
01044 MockB b;
01045 EXPECT_CALL(b, DoB(1));
01046
01047 EXPECT_NONFATAL_FAILURE(
01048 b.DoB(2),
01049 " Expected arg #0: is equal to 1\n"
01050 " Actual: 2\n");
01051 b.DoB(1);
01052 }
01053
01054
01055
01056 TEST(UnexpectedCallTest, UnsatisifiedPrerequisites) {
01057 Sequence s1, s2;
01058 MockB b;
01059 EXPECT_CALL(b, DoB(1))
01060 .InSequence(s1);
01061 EXPECT_CALL(b, DoB(2))
01062 .Times(AnyNumber())
01063 .InSequence(s1);
01064 EXPECT_CALL(b, DoB(3))
01065 .InSequence(s2);
01066 EXPECT_CALL(b, DoB(4))
01067 .InSequence(s1, s2);
01068
01069 ::testing::TestPartResultArray failures;
01070 {
01071 ::testing::ScopedFakeTestPartResultReporter reporter(&failures);
01072 b.DoB(4);
01073
01074
01075 }
01076
01077
01078 ASSERT_EQ(1, failures.size());
01079 const ::testing::TestPartResult& r = failures.GetTestPartResult(0);
01080 EXPECT_EQ(::testing::TestPartResult::kNonFatalFailure, r.type());
01081
01082
01083
01084 #if GTEST_USES_PCRE
01085 EXPECT_THAT(r.message(), ContainsRegex(
01086
01087
01088 "(?s)the following immediate pre-requisites are not satisfied:\n"
01089 ".*: pre-requisite #0\n"
01090 ".*: pre-requisite #1"));
01091 #elif GTEST_USES_POSIX_RE
01092 EXPECT_THAT(r.message(), ContainsRegex(
01093
01094
01095 "the following immediate pre-requisites are not satisfied:\n"
01096 "(.|\n)*: pre-requisite #0\n"
01097 "(.|\n)*: pre-requisite #1"));
01098 #else
01099
01100 EXPECT_THAT(r.message(), ContainsRegex(
01101 "the following immediate pre-requisites are not satisfied:"));
01102 EXPECT_THAT(r.message(), ContainsRegex(": pre-requisite #0"));
01103 EXPECT_THAT(r.message(), ContainsRegex(": pre-requisite #1"));
01104 #endif // GTEST_USES_PCRE
01105
01106 b.DoB(1);
01107 b.DoB(3);
01108 b.DoB(4);
01109 }
01110
01111 TEST(UndefinedReturnValueTest, ReturnValueIsMandatory) {
01112 MockA a;
01113
01114
01115
01116 #if GTEST_HAS_EXCEPTIONS
01117 EXPECT_ANY_THROW(a.ReturnResult(1));
01118 #else
01119 EXPECT_DEATH_IF_SUPPORTED(a.ReturnResult(1), "");
01120 #endif
01121 }
01122
01123
01124
01125 TEST(ExcessiveCallTest, DoesDefaultAction) {
01126
01127
01128 MockA a;
01129 ON_CALL(a, Binary(_, _))
01130 .WillByDefault(Return(true));
01131 EXPECT_CALL(a, Binary(0, 0));
01132 a.Binary(0, 0);
01133 bool result = false;
01134 EXPECT_NONFATAL_FAILURE(result = a.Binary(0, 0),
01135 "Mock function called more times than expected");
01136 EXPECT_TRUE(result);
01137
01138
01139
01140 MockB b;
01141 EXPECT_CALL(b, DoB(0))
01142 .Times(0);
01143 int n = -1;
01144 EXPECT_NONFATAL_FAILURE(n = b.DoB(0),
01145 "Mock function called more times than expected");
01146 EXPECT_EQ(0, n);
01147 }
01148
01149
01150
01151 TEST(ExcessiveCallTest, GeneratesFailureForVoidFunction) {
01152 MockA a;
01153 EXPECT_CALL(a, DoA(_))
01154 .Times(0);
01155 EXPECT_NONFATAL_FAILURE(
01156 a.DoA(9),
01157 "Mock function called more times than expected - returning directly.\n"
01158 " Function call: DoA(9)\n"
01159 " Expected: to be never called\n"
01160 " Actual: called once - over-saturated and active");
01161 }
01162
01163
01164
01165 TEST(ExcessiveCallTest, GeneratesFailureForNonVoidFunction) {
01166 MockB b;
01167 EXPECT_CALL(b, DoB(_));
01168 b.DoB(1);
01169 EXPECT_NONFATAL_FAILURE(
01170 b.DoB(2),
01171 "Mock function called more times than expected - "
01172 "returning default value.\n"
01173 " Function call: DoB(2)\n"
01174 " Returns: 0\n"
01175 " Expected: to be called once\n"
01176 " Actual: called twice - over-saturated and active");
01177 }
01178
01179
01180
01181 TEST(InSequenceTest, AllExpectationInScopeAreInSequence) {
01182 MockA a;
01183 {
01184 InSequence dummy;
01185
01186 EXPECT_CALL(a, DoA(1));
01187 EXPECT_CALL(a, DoA(2));
01188 }
01189
01190 EXPECT_NONFATAL_FAILURE({
01191 a.DoA(2);
01192 }, "Unexpected mock function call");
01193
01194 a.DoA(1);
01195 a.DoA(2);
01196 }
01197
01198 TEST(InSequenceTest, NestedInSequence) {
01199 MockA a;
01200 {
01201 InSequence dummy;
01202
01203 EXPECT_CALL(a, DoA(1));
01204 {
01205 InSequence dummy2;
01206
01207 EXPECT_CALL(a, DoA(2));
01208 EXPECT_CALL(a, DoA(3));
01209 }
01210 }
01211
01212 EXPECT_NONFATAL_FAILURE({
01213 a.DoA(1);
01214 a.DoA(3);
01215 }, "Unexpected mock function call");
01216
01217 a.DoA(2);
01218 a.DoA(3);
01219 }
01220
01221 TEST(InSequenceTest, ExpectationsOutOfScopeAreNotAffected) {
01222 MockA a;
01223 {
01224 InSequence dummy;
01225
01226 EXPECT_CALL(a, DoA(1));
01227 EXPECT_CALL(a, DoA(2));
01228 }
01229 EXPECT_CALL(a, DoA(3));
01230
01231 EXPECT_NONFATAL_FAILURE({
01232 a.DoA(2);
01233 }, "Unexpected mock function call");
01234
01235 a.DoA(3);
01236 a.DoA(1);
01237 a.DoA(2);
01238 }
01239
01240
01241 TEST(SequenceTest, AnyOrderIsOkByDefault) {
01242 {
01243 MockA a;
01244 MockB b;
01245
01246 EXPECT_CALL(a, DoA(1));
01247 EXPECT_CALL(b, DoB())
01248 .Times(AnyNumber());
01249
01250 a.DoA(1);
01251 b.DoB();
01252 }
01253
01254 {
01255 MockA a;
01256 MockB b;
01257
01258 EXPECT_CALL(a, DoA(1));
01259 EXPECT_CALL(b, DoB())
01260 .Times(AnyNumber());
01261
01262 b.DoB();
01263 a.DoA(1);
01264 }
01265 }
01266
01267
01268
01269 TEST(SequenceTest, CallsMustBeInStrictOrderWhenSaidSo1) {
01270 MockA a;
01271 ON_CALL(a, ReturnResult(_))
01272 .WillByDefault(Return(Result()));
01273
01274 Sequence s;
01275 EXPECT_CALL(a, ReturnResult(1))
01276 .InSequence(s);
01277 EXPECT_CALL(a, ReturnResult(2))
01278 .InSequence(s);
01279 EXPECT_CALL(a, ReturnResult(3))
01280 .InSequence(s);
01281
01282 a.ReturnResult(1);
01283
01284
01285 EXPECT_NONFATAL_FAILURE(a.ReturnResult(3), "Unexpected mock function call");
01286
01287 a.ReturnResult(2);
01288 a.ReturnResult(3);
01289 }
01290
01291
01292
01293 TEST(SequenceTest, CallsMustBeInStrictOrderWhenSaidSo2) {
01294 MockA a;
01295 ON_CALL(a, ReturnResult(_))
01296 .WillByDefault(Return(Result()));
01297
01298 Sequence s;
01299 EXPECT_CALL(a, ReturnResult(1))
01300 .InSequence(s);
01301 EXPECT_CALL(a, ReturnResult(2))
01302 .InSequence(s);
01303
01304
01305 EXPECT_NONFATAL_FAILURE(a.ReturnResult(2), "Unexpected mock function call");
01306
01307 a.ReturnResult(1);
01308 a.ReturnResult(2);
01309 }
01310
01311
01312 class PartialOrderTest : public testing::Test {
01313 protected:
01314 PartialOrderTest() {
01315 ON_CALL(a_, ReturnResult(_))
01316 .WillByDefault(Return(Result()));
01317
01318
01319
01320
01321
01322
01323 Sequence x, y;
01324 EXPECT_CALL(a_, ReturnResult(1))
01325 .InSequence(x);
01326 EXPECT_CALL(b_, DoB())
01327 .Times(2)
01328 .InSequence(y);
01329 EXPECT_CALL(a_, ReturnResult(2))
01330 .Times(AnyNumber())
01331 .InSequence(x, y);
01332 EXPECT_CALL(a_, ReturnResult(3))
01333 .InSequence(x);
01334 }
01335
01336 MockA a_;
01337 MockB b_;
01338 };
01339
01340 TEST_F(PartialOrderTest, CallsMustConformToSpecifiedDag1) {
01341 a_.ReturnResult(1);
01342 b_.DoB();
01343
01344
01345 EXPECT_NONFATAL_FAILURE(a_.ReturnResult(2), "Unexpected mock function call");
01346
01347 b_.DoB();
01348 a_.ReturnResult(3);
01349 }
01350
01351 TEST_F(PartialOrderTest, CallsMustConformToSpecifiedDag2) {
01352
01353 EXPECT_NONFATAL_FAILURE(a_.ReturnResult(2), "Unexpected mock function call");
01354
01355 a_.ReturnResult(1);
01356 b_.DoB();
01357 b_.DoB();
01358 a_.ReturnResult(3);
01359 }
01360
01361 TEST_F(PartialOrderTest, CallsMustConformToSpecifiedDag3) {
01362
01363 EXPECT_NONFATAL_FAILURE(a_.ReturnResult(3), "Unexpected mock function call");
01364
01365 a_.ReturnResult(1);
01366 b_.DoB();
01367 b_.DoB();
01368 a_.ReturnResult(3);
01369 }
01370
01371 TEST_F(PartialOrderTest, CallsMustConformToSpecifiedDag4) {
01372 a_.ReturnResult(1);
01373 b_.DoB();
01374 b_.DoB();
01375 a_.ReturnResult(3);
01376
01377
01378 EXPECT_NONFATAL_FAILURE(a_.ReturnResult(2), "Unexpected mock function call");
01379 }
01380
01381 TEST(SequenceTest, Retirement) {
01382 MockA a;
01383 Sequence s;
01384
01385 EXPECT_CALL(a, DoA(1))
01386 .InSequence(s);
01387 EXPECT_CALL(a, DoA(_))
01388 .InSequence(s)
01389 .RetiresOnSaturation();
01390 EXPECT_CALL(a, DoA(1))
01391 .InSequence(s);
01392
01393 a.DoA(1);
01394 a.DoA(2);
01395 a.DoA(1);
01396 }
01397
01398
01399
01400 TEST(ExpectationTest, ConstrutorsWork) {
01401 MockA a;
01402 Expectation e1;
01403
01404
01405 Expectation e2 = EXPECT_CALL(a, DoA(2));
01406 Expectation e3 = EXPECT_CALL(a, DoA(3)).With(_);
01407 {
01408 Sequence s;
01409 Expectation e4 = EXPECT_CALL(a, DoA(4)).Times(1);
01410 Expectation e5 = EXPECT_CALL(a, DoA(5)).InSequence(s);
01411 }
01412 Expectation e6 = EXPECT_CALL(a, DoA(6)).After(e2);
01413 Expectation e7 = EXPECT_CALL(a, DoA(7)).WillOnce(Return());
01414 Expectation e8 = EXPECT_CALL(a, DoA(8)).WillRepeatedly(Return());
01415 Expectation e9 = EXPECT_CALL(a, DoA(9)).RetiresOnSaturation();
01416
01417 Expectation e10 = e2;
01418
01419 EXPECT_THAT(e1, Ne(e2));
01420 EXPECT_THAT(e2, Eq(e10));
01421
01422 a.DoA(2);
01423 a.DoA(3);
01424 a.DoA(4);
01425 a.DoA(5);
01426 a.DoA(6);
01427 a.DoA(7);
01428 a.DoA(8);
01429 a.DoA(9);
01430 }
01431
01432 TEST(ExpectationTest, AssignmentWorks) {
01433 MockA a;
01434 Expectation e1;
01435 Expectation e2 = EXPECT_CALL(a, DoA(1));
01436
01437 EXPECT_THAT(e1, Ne(e2));
01438
01439 e1 = e2;
01440 EXPECT_THAT(e1, Eq(e2));
01441
01442 a.DoA(1);
01443 }
01444
01445
01446
01447 TEST(ExpectationSetTest, MemberTypesAreCorrect) {
01448 ::testing::StaticAssertTypeEq<Expectation, ExpectationSet::value_type>();
01449 }
01450
01451 TEST(ExpectationSetTest, ConstructorsWork) {
01452 MockA a;
01453
01454 Expectation e1;
01455 const Expectation e2;
01456 ExpectationSet es1;
01457 ExpectationSet es2 = EXPECT_CALL(a, DoA(1));
01458 ExpectationSet es3 = e1;
01459 ExpectationSet es4(e1);
01460 ExpectationSet es5 = e2;
01461 ExpectationSet es6(e2);
01462 ExpectationSet es7 = es2;
01463
01464 EXPECT_EQ(0, es1.size());
01465 EXPECT_EQ(1, es2.size());
01466 EXPECT_EQ(1, es3.size());
01467 EXPECT_EQ(1, es4.size());
01468 EXPECT_EQ(1, es5.size());
01469 EXPECT_EQ(1, es6.size());
01470 EXPECT_EQ(1, es7.size());
01471
01472 EXPECT_THAT(es3, Ne(es2));
01473 EXPECT_THAT(es4, Eq(es3));
01474 EXPECT_THAT(es5, Eq(es4));
01475 EXPECT_THAT(es6, Eq(es5));
01476 EXPECT_THAT(es7, Eq(es2));
01477 a.DoA(1);
01478 }
01479
01480 TEST(ExpectationSetTest, AssignmentWorks) {
01481 ExpectationSet es1;
01482 ExpectationSet es2 = Expectation();
01483
01484 es1 = es2;
01485 EXPECT_EQ(1, es1.size());
01486 EXPECT_THAT(*(es1.begin()), Eq(Expectation()));
01487 EXPECT_THAT(es1, Eq(es2));
01488 }
01489
01490 TEST(ExpectationSetTest, InsertionWorks) {
01491 ExpectationSet es1;
01492 Expectation e1;
01493 es1 += e1;
01494 EXPECT_EQ(1, es1.size());
01495 EXPECT_THAT(*(es1.begin()), Eq(e1));
01496
01497 MockA a;
01498 Expectation e2 = EXPECT_CALL(a, DoA(1));
01499 es1 += e2;
01500 EXPECT_EQ(2, es1.size());
01501
01502 ExpectationSet::const_iterator it1 = es1.begin();
01503 ExpectationSet::const_iterator it2 = it1;
01504 ++it2;
01505 EXPECT_TRUE(*it1 == e1 || *it2 == e1);
01506 EXPECT_TRUE(*it1 == e2 || *it2 == e2);
01507 a.DoA(1);
01508 }
01509
01510 TEST(ExpectationSetTest, SizeWorks) {
01511 ExpectationSet es;
01512 EXPECT_EQ(0, es.size());
01513
01514 es += Expectation();
01515 EXPECT_EQ(1, es.size());
01516
01517 MockA a;
01518 es += EXPECT_CALL(a, DoA(1));
01519 EXPECT_EQ(2, es.size());
01520
01521 a.DoA(1);
01522 }
01523
01524 TEST(ExpectationSetTest, IsEnumerable) {
01525 ExpectationSet es;
01526 EXPECT_TRUE(es.begin() == es.end());
01527
01528 es += Expectation();
01529 ExpectationSet::const_iterator it = es.begin();
01530 EXPECT_TRUE(it != es.end());
01531 EXPECT_THAT(*it, Eq(Expectation()));
01532 ++it;
01533 EXPECT_TRUE(it== es.end());
01534 }
01535
01536
01537
01538 TEST(AfterTest, SucceedsWhenPartialOrderIsSatisfied) {
01539 MockA a;
01540 ExpectationSet es;
01541 es += EXPECT_CALL(a, DoA(1));
01542 es += EXPECT_CALL(a, DoA(2));
01543 EXPECT_CALL(a, DoA(3))
01544 .After(es);
01545
01546 a.DoA(1);
01547 a.DoA(2);
01548 a.DoA(3);
01549 }
01550
01551 TEST(AfterTest, SucceedsWhenTotalOrderIsSatisfied) {
01552 MockA a;
01553 MockB b;
01554
01555
01556 const Expectation e1 = EXPECT_CALL(a, DoA(1));
01557 const Expectation e2 = EXPECT_CALL(b, DoB())
01558 .Times(2)
01559 .After(e1);
01560 EXPECT_CALL(a, DoA(2)).After(e2);
01561
01562 a.DoA(1);
01563 b.DoB();
01564 b.DoB();
01565 a.DoA(2);
01566 }
01567
01568
01569 TEST(AfterTest, CallsMustBeInStrictOrderWhenSpecifiedSo1) {
01570 MockA a;
01571 MockB b;
01572
01573
01574
01575 Expectation e1 = EXPECT_CALL(a, DoA(1));
01576 Expectation e2 = EXPECT_CALL(b, DoB())
01577 .After(e1);
01578 EXPECT_CALL(a, DoA(2))
01579 .After(e2);
01580
01581 a.DoA(1);
01582
01583
01584 EXPECT_NONFATAL_FAILURE(a.DoA(2), "Unexpected mock function call");
01585
01586 b.DoB();
01587 a.DoA(2);
01588 }
01589
01590
01591 TEST(AfterTest, CallsMustBeInStrictOrderWhenSpecifiedSo2) {
01592 MockA a;
01593 MockB b;
01594
01595
01596
01597 Expectation e1 = EXPECT_CALL(a, DoA(1));
01598 Expectation e2 = EXPECT_CALL(b, DoB())
01599 .Times(2)
01600 .After(e1);
01601 EXPECT_CALL(a, DoA(2))
01602 .After(e2);
01603
01604 a.DoA(1);
01605 b.DoB();
01606
01607
01608 EXPECT_NONFATAL_FAILURE(a.DoA(2), "Unexpected mock function call");
01609
01610 b.DoB();
01611 a.DoA(2);
01612 }
01613
01614
01615 TEST(AfterTest, CallsMustSatisfyPartialOrderWhenSpecifiedSo) {
01616 MockA a;
01617 ON_CALL(a, ReturnResult(_))
01618 .WillByDefault(Return(Result()));
01619
01620
01621
01622
01623 Expectation e = EXPECT_CALL(a, DoA(1));
01624 const ExpectationSet es = EXPECT_CALL(a, DoA(2));
01625 EXPECT_CALL(a, ReturnResult(3))
01626 .After(e, es);
01627
01628
01629 EXPECT_NONFATAL_FAILURE(a.ReturnResult(3), "Unexpected mock function call");
01630
01631 a.DoA(2);
01632 a.DoA(1);
01633 a.ReturnResult(3);
01634 }
01635
01636
01637 TEST(AfterTest, CallsMustSatisfyPartialOrderWhenSpecifiedSo2) {
01638 MockA a;
01639
01640
01641
01642
01643 Expectation e = EXPECT_CALL(a, DoA(1));
01644 const ExpectationSet es = EXPECT_CALL(a, DoA(2));
01645 EXPECT_CALL(a, DoA(3))
01646 .After(e, es);
01647
01648 a.DoA(2);
01649
01650
01651 EXPECT_NONFATAL_FAILURE(a.DoA(3), "Unexpected mock function call");
01652
01653 a.DoA(1);
01654 a.DoA(3);
01655 }
01656
01657
01658 TEST(AfterTest, CanBeUsedWithInSequence) {
01659 MockA a;
01660 Sequence s;
01661 Expectation e = EXPECT_CALL(a, DoA(1));
01662 EXPECT_CALL(a, DoA(2)).InSequence(s);
01663 EXPECT_CALL(a, DoA(3))
01664 .InSequence(s)
01665 .After(e);
01666
01667 a.DoA(1);
01668
01669
01670 EXPECT_NONFATAL_FAILURE(a.DoA(3), "Unexpected mock function call");
01671
01672 a.DoA(2);
01673 a.DoA(3);
01674 }
01675
01676
01677 TEST(AfterTest, CanBeCalledManyTimes) {
01678 MockA a;
01679 Expectation e1 = EXPECT_CALL(a, DoA(1));
01680 Expectation e2 = EXPECT_CALL(a, DoA(2));
01681 Expectation e3 = EXPECT_CALL(a, DoA(3));
01682 EXPECT_CALL(a, DoA(4))
01683 .After(e1)
01684 .After(e2)
01685 .After(e3);
01686
01687 a.DoA(3);
01688 a.DoA(1);
01689 a.DoA(2);
01690 a.DoA(4);
01691 }
01692
01693
01694 TEST(AfterTest, AcceptsUpToFiveArguments) {
01695 MockA a;
01696 Expectation e1 = EXPECT_CALL(a, DoA(1));
01697 Expectation e2 = EXPECT_CALL(a, DoA(2));
01698 Expectation e3 = EXPECT_CALL(a, DoA(3));
01699 ExpectationSet es1 = EXPECT_CALL(a, DoA(4));
01700 ExpectationSet es2 = EXPECT_CALL(a, DoA(5));
01701 EXPECT_CALL(a, DoA(6))
01702 .After(e1, e2, e3, es1, es2);
01703
01704 a.DoA(5);
01705 a.DoA(2);
01706 a.DoA(4);
01707 a.DoA(1);
01708 a.DoA(3);
01709 a.DoA(6);
01710 }
01711
01712
01713 TEST(AfterTest, AcceptsDuplicatedInput) {
01714 MockA a;
01715 ON_CALL(a, ReturnResult(_))
01716 .WillByDefault(Return(Result()));
01717
01718
01719
01720
01721 Expectation e1 = EXPECT_CALL(a, DoA(1));
01722 Expectation e2 = EXPECT_CALL(a, DoA(2));
01723 ExpectationSet es;
01724 es += e1;
01725 es += e2;
01726 EXPECT_CALL(a, ReturnResult(3))
01727 .After(e1, e2, es, e1);
01728
01729 a.DoA(1);
01730
01731
01732 EXPECT_NONFATAL_FAILURE(a.ReturnResult(3), "Unexpected mock function call");
01733
01734 a.DoA(2);
01735 a.ReturnResult(3);
01736 }
01737
01738
01739
01740 TEST(AfterTest, ChangesToExpectationSetHaveNoEffectAfterwards) {
01741 MockA a;
01742 ExpectationSet es1 = EXPECT_CALL(a, DoA(1));
01743 Expectation e2 = EXPECT_CALL(a, DoA(2));
01744 EXPECT_CALL(a, DoA(3))
01745 .After(es1);
01746 es1 += e2;
01747
01748 a.DoA(1);
01749 a.DoA(3);
01750 a.DoA(2);
01751 }
01752
01753
01754
01755
01756
01757 TEST(DeletingMockEarlyTest, Success1) {
01758 MockB* const b1 = new MockB;
01759 MockA* const a = new MockA;
01760 MockB* const b2 = new MockB;
01761
01762 {
01763 InSequence dummy;
01764 EXPECT_CALL(*b1, DoB(_))
01765 .WillOnce(Return(1));
01766 EXPECT_CALL(*a, Binary(_, _))
01767 .Times(AnyNumber())
01768 .WillRepeatedly(Return(true));
01769 EXPECT_CALL(*b2, DoB(_))
01770 .Times(AnyNumber())
01771 .WillRepeatedly(Return(2));
01772 }
01773
01774 EXPECT_EQ(1, b1->DoB(1));
01775 delete b1;
01776
01777 EXPECT_TRUE(a->Binary(0, 1));
01778 delete b2;
01779
01780 EXPECT_TRUE(a->Binary(1, 2));
01781 delete a;
01782 }
01783
01784
01785 TEST(DeletingMockEarlyTest, Success2) {
01786 MockB* const b1 = new MockB;
01787 MockA* const a = new MockA;
01788 MockB* const b2 = new MockB;
01789
01790 {
01791 InSequence dummy;
01792 EXPECT_CALL(*b1, DoB(_))
01793 .WillOnce(Return(1));
01794 EXPECT_CALL(*a, Binary(_, _))
01795 .Times(AnyNumber());
01796 EXPECT_CALL(*b2, DoB(_))
01797 .Times(AnyNumber())
01798 .WillRepeatedly(Return(2));
01799 }
01800
01801 delete a;
01802 EXPECT_EQ(1, b1->DoB(1));
01803 EXPECT_EQ(2, b2->DoB(2));
01804 delete b1;
01805 delete b2;
01806 }
01807
01808
01809
01810
01811
01812 #ifdef _MSC_VER
01813 # pragma warning(push)
01814 # pragma warning(disable:4100)
01815 #endif
01816
01817 ACTION_P(Delete, ptr) { delete ptr; }
01818
01819 #ifdef _MSC_VER
01820 # pragma warning(pop)
01821 #endif
01822
01823 TEST(DeletingMockEarlyTest, CanDeleteSelfInActionReturningVoid) {
01824 MockA* const a = new MockA;
01825 EXPECT_CALL(*a, DoA(_)).WillOnce(Delete(a));
01826 a->DoA(42);
01827 }
01828
01829 TEST(DeletingMockEarlyTest, CanDeleteSelfInActionReturningValue) {
01830 MockA* const a = new MockA;
01831 EXPECT_CALL(*a, ReturnResult(_))
01832 .WillOnce(DoAll(Delete(a), Return(Result())));
01833 a->ReturnResult(42);
01834 }
01835
01836
01837 TEST(DeletingMockEarlyTest, Failure1) {
01838 MockB* const b1 = new MockB;
01839 MockA* const a = new MockA;
01840 MockB* const b2 = new MockB;
01841
01842 {
01843 InSequence dummy;
01844 EXPECT_CALL(*b1, DoB(_))
01845 .WillOnce(Return(1));
01846 EXPECT_CALL(*a, Binary(_, _))
01847 .Times(AnyNumber());
01848 EXPECT_CALL(*b2, DoB(_))
01849 .Times(AnyNumber())
01850 .WillRepeatedly(Return(2));
01851 }
01852
01853 delete a;
01854 EXPECT_NONFATAL_FAILURE({
01855 b2->DoB(2);
01856 }, "Unexpected mock function call");
01857 EXPECT_EQ(1, b1->DoB(1));
01858 delete b1;
01859 delete b2;
01860 }
01861
01862
01863 TEST(DeletingMockEarlyTest, Failure2) {
01864 MockB* const b1 = new MockB;
01865 MockA* const a = new MockA;
01866 MockB* const b2 = new MockB;
01867
01868 {
01869 InSequence dummy;
01870 EXPECT_CALL(*b1, DoB(_));
01871 EXPECT_CALL(*a, Binary(_, _))
01872 .Times(AnyNumber());
01873 EXPECT_CALL(*b2, DoB(_))
01874 .Times(AnyNumber());
01875 }
01876
01877 EXPECT_NONFATAL_FAILURE(delete b1,
01878 "Actual: never called");
01879 EXPECT_NONFATAL_FAILURE(a->Binary(0, 1),
01880 "Unexpected mock function call");
01881 EXPECT_NONFATAL_FAILURE(b2->DoB(1),
01882 "Unexpected mock function call");
01883 delete a;
01884 delete b2;
01885 }
01886
01887 class EvenNumberCardinality : public CardinalityInterface {
01888 public:
01889
01890 virtual bool IsSatisfiedByCallCount(int call_count) const {
01891 return call_count % 2 == 0;
01892 }
01893
01894
01895 virtual bool IsSaturatedByCallCount(int ) const {
01896 return false;
01897 }
01898
01899
01900 virtual void DescribeTo(::std::ostream* os) const {
01901 *os << "called even number of times";
01902 }
01903 };
01904
01905 Cardinality EvenNumber() {
01906 return Cardinality(new EvenNumberCardinality);
01907 }
01908
01909 TEST(ExpectationBaseTest,
01910 AllPrerequisitesAreSatisfiedWorksForNonMonotonicCardinality) {
01911 MockA* a = new MockA;
01912 Sequence s;
01913
01914 EXPECT_CALL(*a, DoA(1))
01915 .Times(EvenNumber())
01916 .InSequence(s);
01917 EXPECT_CALL(*a, DoA(2))
01918 .Times(AnyNumber())
01919 .InSequence(s);
01920 EXPECT_CALL(*a, DoA(3))
01921 .Times(AnyNumber());
01922
01923 a->DoA(3);
01924 a->DoA(1);
01925 EXPECT_NONFATAL_FAILURE(a->DoA(2), "Unexpected mock function call");
01926 EXPECT_NONFATAL_FAILURE(delete a, "to be called even number of times");
01927 }
01928
01929
01930
01931
01932 struct Printable {
01933 };
01934
01935 inline void operator<<(::std::ostream& os, const Printable&) {
01936 os << "Printable";
01937 }
01938
01939 struct Unprintable {
01940 Unprintable() : value(0) {}
01941 int value;
01942 };
01943
01944 class MockC {
01945 public:
01946 MockC() {}
01947
01948 MOCK_METHOD6(VoidMethod, void(bool cond, int n, string s, void* p,
01949 const Printable& x, Unprintable y));
01950 MOCK_METHOD0(NonVoidMethod, int());
01951
01952 private:
01953 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockC);
01954 };
01955
01956 class VerboseFlagPreservingFixture : public testing::Test {
01957 protected:
01958 VerboseFlagPreservingFixture()
01959 : saved_verbose_flag_(GMOCK_FLAG(verbose)) {}
01960
01961 ~VerboseFlagPreservingFixture() { GMOCK_FLAG(verbose) = saved_verbose_flag_; }
01962
01963 private:
01964 const string saved_verbose_flag_;
01965
01966 GTEST_DISALLOW_COPY_AND_ASSIGN_(VerboseFlagPreservingFixture);
01967 };
01968
01969 #if GTEST_HAS_STREAM_REDIRECTION
01970
01971
01972
01973 TEST(FunctionCallMessageTest,
01974 UninterestingCallOnNaggyMockGeneratesFyiWithStackTrace) {
01975 NaggyMock<MockC> c;
01976 CaptureStdout();
01977 c.VoidMethod(false, 5, "Hi", NULL, Printable(), Unprintable());
01978 const std::string output = GetCapturedStdout();
01979 EXPECT_PRED_FORMAT2(IsSubstring, "GMOCK WARNING", output);
01980 EXPECT_PRED_FORMAT2(IsSubstring, "Stack trace:", output);
01981
01982 # ifndef NDEBUG
01983
01984
01985
01986
01987
01988
01989 EXPECT_PRED_FORMAT2(IsSubstring, "VoidMethod(", output);
01990
01991
01992
01993 CaptureStdout();
01994 c.NonVoidMethod();
01995 const std::string output2 = GetCapturedStdout();
01996 EXPECT_PRED_FORMAT2(IsSubstring, "NonVoidMethod(", output2);
01997
01998 # endif // NDEBUG
01999 }
02000
02001
02002
02003 TEST(FunctionCallMessageTest,
02004 UninterestingCallOnNaggyMockPrintsArgumentsAndReturnValue) {
02005
02006 NaggyMock<MockB> b;
02007 CaptureStdout();
02008 b.DoB();
02009 const std::string output1 = GetCapturedStdout();
02010 EXPECT_PRED_FORMAT2(
02011 IsSubstring,
02012 "Uninteresting mock function call - returning default value.\n"
02013 " Function call: DoB()\n"
02014 " Returns: 0\n", output1.c_str());
02015
02016
02017
02018 NaggyMock<MockC> c;
02019 CaptureStdout();
02020 c.VoidMethod(false, 5, "Hi", NULL, Printable(), Unprintable());
02021 const std::string output2 = GetCapturedStdout();
02022 EXPECT_THAT(output2.c_str(),
02023 ContainsRegex(
02024 "Uninteresting mock function call - returning directly\\.\n"
02025 " Function call: VoidMethod"
02026 "\\(false, 5, \"Hi\", NULL, @.+ "
02027 "Printable, 4-byte object <00-00 00-00>\\)"));
02028
02029 }
02030
02031
02032
02033 class GMockVerboseFlagTest : public VerboseFlagPreservingFixture {
02034 public:
02035
02036
02037
02038
02039 void VerifyOutput(const std::string& output, bool should_print,
02040 const string& expected_substring,
02041 const string& function_name) {
02042 if (should_print) {
02043 EXPECT_THAT(output.c_str(), HasSubstr(expected_substring));
02044 # ifndef NDEBUG
02045
02046
02047 EXPECT_THAT(output.c_str(), HasSubstr(function_name));
02048 # else
02049
02050 static_cast<void>(function_name);
02051 # endif // NDEBUG
02052 } else {
02053 EXPECT_STREQ("", output.c_str());
02054 }
02055 }
02056
02057
02058 void TestExpectedCall(bool should_print) {
02059 MockA a;
02060 EXPECT_CALL(a, DoA(5));
02061 EXPECT_CALL(a, Binary(_, 1))
02062 .WillOnce(Return(true));
02063
02064
02065 CaptureStdout();
02066 a.DoA(5);
02067 VerifyOutput(
02068 GetCapturedStdout(),
02069 should_print,
02070 "Mock function call matches EXPECT_CALL(a, DoA(5))...\n"
02071 " Function call: DoA(5)\n"
02072 "Stack trace:\n",
02073 "DoA");
02074
02075
02076 CaptureStdout();
02077 a.Binary(2, 1);
02078 VerifyOutput(
02079 GetCapturedStdout(),
02080 should_print,
02081 "Mock function call matches EXPECT_CALL(a, Binary(_, 1))...\n"
02082 " Function call: Binary(2, 1)\n"
02083 " Returns: true\n"
02084 "Stack trace:\n",
02085 "Binary");
02086 }
02087
02088
02089 void TestUninterestingCallOnNaggyMock(bool should_print) {
02090 NaggyMock<MockA> a;
02091
02092
02093 CaptureStdout();
02094 a.DoA(5);
02095 VerifyOutput(
02096 GetCapturedStdout(),
02097 should_print,
02098 "\nGMOCK WARNING:\n"
02099 "Uninteresting mock function call - returning directly.\n"
02100 " Function call: DoA(5)\n"
02101 "Stack trace:\n",
02102 "DoA");
02103
02104
02105 CaptureStdout();
02106 a.Binary(2, 1);
02107 VerifyOutput(
02108 GetCapturedStdout(),
02109 should_print,
02110 "\nGMOCK WARNING:\n"
02111 "Uninteresting mock function call - returning default value.\n"
02112 " Function call: Binary(2, 1)\n"
02113 " Returns: false\n"
02114 "Stack trace:\n",
02115 "Binary");
02116 }
02117 };
02118
02119
02120
02121 TEST_F(GMockVerboseFlagTest, Info) {
02122 GMOCK_FLAG(verbose) = kInfoVerbosity;
02123 TestExpectedCall(true);
02124 TestUninterestingCallOnNaggyMock(true);
02125 }
02126
02127
02128
02129 TEST_F(GMockVerboseFlagTest, Warning) {
02130 GMOCK_FLAG(verbose) = kWarningVerbosity;
02131 TestExpectedCall(false);
02132 TestUninterestingCallOnNaggyMock(true);
02133 }
02134
02135
02136
02137 TEST_F(GMockVerboseFlagTest, Error) {
02138 GMOCK_FLAG(verbose) = kErrorVerbosity;
02139 TestExpectedCall(false);
02140 TestUninterestingCallOnNaggyMock(false);
02141 }
02142
02143
02144
02145 TEST_F(GMockVerboseFlagTest, InvalidFlagIsTreatedAsWarning) {
02146 GMOCK_FLAG(verbose) = "invalid";
02147 TestExpectedCall(false);
02148 TestUninterestingCallOnNaggyMock(true);
02149 }
02150
02151 #endif // GTEST_HAS_STREAM_REDIRECTION
02152
02153
02154
02155
02156 class PrintMeNot {};
02157
02158 void PrintTo(PrintMeNot , ::std::ostream* ) {
02159 ADD_FAILURE() << "Google Mock is printing a value that shouldn't be "
02160 << "printed even to an internal buffer.";
02161 }
02162
02163 class LogTestHelper {
02164 public:
02165 LogTestHelper() {}
02166
02167 MOCK_METHOD1(Foo, PrintMeNot(PrintMeNot));
02168
02169 private:
02170 GTEST_DISALLOW_COPY_AND_ASSIGN_(LogTestHelper);
02171 };
02172
02173 class GMockLogTest : public VerboseFlagPreservingFixture {
02174 protected:
02175 LogTestHelper helper_;
02176 };
02177
02178 TEST_F(GMockLogTest, DoesNotPrintGoodCallInternallyIfVerbosityIsWarning) {
02179 GMOCK_FLAG(verbose) = kWarningVerbosity;
02180 EXPECT_CALL(helper_, Foo(_))
02181 .WillOnce(Return(PrintMeNot()));
02182 helper_.Foo(PrintMeNot());
02183 }
02184
02185 TEST_F(GMockLogTest, DoesNotPrintGoodCallInternallyIfVerbosityIsError) {
02186 GMOCK_FLAG(verbose) = kErrorVerbosity;
02187 EXPECT_CALL(helper_, Foo(_))
02188 .WillOnce(Return(PrintMeNot()));
02189 helper_.Foo(PrintMeNot());
02190 }
02191
02192 TEST_F(GMockLogTest, DoesNotPrintWarningInternallyIfVerbosityIsError) {
02193 GMOCK_FLAG(verbose) = kErrorVerbosity;
02194 ON_CALL(helper_, Foo(_))
02195 .WillByDefault(Return(PrintMeNot()));
02196 helper_.Foo(PrintMeNot());
02197 }
02198
02199
02200
02201 TEST(AllowLeakTest, AllowsLeakingUnusedMockObject) {
02202 MockA* a = new MockA;
02203 Mock::AllowLeak(a);
02204 }
02205
02206 TEST(AllowLeakTest, CanBeCalledBeforeOnCall) {
02207 MockA* a = new MockA;
02208 Mock::AllowLeak(a);
02209 ON_CALL(*a, DoA(_)).WillByDefault(Return());
02210 a->DoA(0);
02211 }
02212
02213 TEST(AllowLeakTest, CanBeCalledAfterOnCall) {
02214 MockA* a = new MockA;
02215 ON_CALL(*a, DoA(_)).WillByDefault(Return());
02216 Mock::AllowLeak(a);
02217 }
02218
02219 TEST(AllowLeakTest, CanBeCalledBeforeExpectCall) {
02220 MockA* a = new MockA;
02221 Mock::AllowLeak(a);
02222 EXPECT_CALL(*a, DoA(_));
02223 a->DoA(0);
02224 }
02225
02226 TEST(AllowLeakTest, CanBeCalledAfterExpectCall) {
02227 MockA* a = new MockA;
02228 EXPECT_CALL(*a, DoA(_)).Times(AnyNumber());
02229 Mock::AllowLeak(a);
02230 }
02231
02232 TEST(AllowLeakTest, WorksWhenBothOnCallAndExpectCallArePresent) {
02233 MockA* a = new MockA;
02234 ON_CALL(*a, DoA(_)).WillByDefault(Return());
02235 EXPECT_CALL(*a, DoA(_)).Times(AnyNumber());
02236 Mock::AllowLeak(a);
02237 }
02238
02239
02240
02241 TEST(VerifyAndClearExpectationsTest, NoMethodHasExpectations) {
02242 MockB b;
02243 ASSERT_TRUE(Mock::VerifyAndClearExpectations(&b));
02244
02245
02246
02247 EXPECT_EQ(0, b.DoB());
02248 EXPECT_EQ(0, b.DoB(1));
02249 }
02250
02251
02252
02253
02254 TEST(VerifyAndClearExpectationsTest, SomeMethodsHaveExpectationsAndSucceed) {
02255 MockB b;
02256 EXPECT_CALL(b, DoB())
02257 .WillOnce(Return(1));
02258 b.DoB();
02259 ASSERT_TRUE(Mock::VerifyAndClearExpectations(&b));
02260
02261
02262
02263 EXPECT_EQ(0, b.DoB());
02264 EXPECT_EQ(0, b.DoB(1));
02265 }
02266
02267
02268
02269
02270 TEST(VerifyAndClearExpectationsTest, SomeMethodsHaveExpectationsAndFail) {
02271 MockB b;
02272 EXPECT_CALL(b, DoB())
02273 .WillOnce(Return(1));
02274 bool result = true;
02275 EXPECT_NONFATAL_FAILURE(result = Mock::VerifyAndClearExpectations(&b),
02276 "Actual: never called");
02277 ASSERT_FALSE(result);
02278
02279
02280
02281 EXPECT_EQ(0, b.DoB());
02282 EXPECT_EQ(0, b.DoB(1));
02283 }
02284
02285
02286
02287 TEST(VerifyAndClearExpectationsTest, AllMethodsHaveExpectations) {
02288 MockB b;
02289 EXPECT_CALL(b, DoB())
02290 .WillOnce(Return(1));
02291 EXPECT_CALL(b, DoB(_))
02292 .WillOnce(Return(2));
02293 b.DoB();
02294 b.DoB(1);
02295 ASSERT_TRUE(Mock::VerifyAndClearExpectations(&b));
02296
02297
02298
02299 EXPECT_EQ(0, b.DoB());
02300 EXPECT_EQ(0, b.DoB(1));
02301 }
02302
02303
02304
02305 TEST(VerifyAndClearExpectationsTest, AMethodHasManyExpectations) {
02306 MockB b;
02307 EXPECT_CALL(b, DoB(0))
02308 .WillOnce(Return(1));
02309 EXPECT_CALL(b, DoB(_))
02310 .WillOnce(Return(2));
02311 b.DoB(1);
02312 bool result = true;
02313 EXPECT_NONFATAL_FAILURE(result = Mock::VerifyAndClearExpectations(&b),
02314 "Actual: never called");
02315 ASSERT_FALSE(result);
02316
02317
02318
02319 EXPECT_EQ(0, b.DoB());
02320 EXPECT_EQ(0, b.DoB(1));
02321 }
02322
02323
02324
02325 TEST(VerifyAndClearExpectationsTest, CanCallManyTimes) {
02326 MockB b;
02327 EXPECT_CALL(b, DoB());
02328 b.DoB();
02329 Mock::VerifyAndClearExpectations(&b);
02330
02331 EXPECT_CALL(b, DoB(_))
02332 .WillOnce(Return(1));
02333 b.DoB(1);
02334 Mock::VerifyAndClearExpectations(&b);
02335 Mock::VerifyAndClearExpectations(&b);
02336
02337
02338
02339 EXPECT_EQ(0, b.DoB());
02340 EXPECT_EQ(0, b.DoB(1));
02341 }
02342
02343
02344
02345 TEST(VerifyAndClearTest, NoMethodHasDefaultActions) {
02346 MockB b;
02347
02348 Mock::VerifyAndClear(&b);
02349 EXPECT_EQ(0, b.DoB());
02350 }
02351
02352
02353
02354 TEST(VerifyAndClearTest, SomeMethodsHaveDefaultActions) {
02355 MockB b;
02356 ON_CALL(b, DoB())
02357 .WillByDefault(Return(1));
02358
02359 Mock::VerifyAndClear(&b);
02360
02361
02362 EXPECT_EQ(0, b.DoB());
02363 }
02364
02365
02366
02367 TEST(VerifyAndClearTest, AllMethodsHaveDefaultActions) {
02368 MockB b;
02369 ON_CALL(b, DoB())
02370 .WillByDefault(Return(1));
02371 ON_CALL(b, DoB(_))
02372 .WillByDefault(Return(2));
02373
02374 Mock::VerifyAndClear(&b);
02375
02376
02377 EXPECT_EQ(0, b.DoB());
02378
02379
02380 EXPECT_EQ(0, b.DoB(0));
02381 }
02382
02383
02384
02385 TEST(VerifyAndClearTest, AMethodHasManyDefaultActions) {
02386 MockB b;
02387 ON_CALL(b, DoB(0))
02388 .WillByDefault(Return(1));
02389 ON_CALL(b, DoB(_))
02390 .WillByDefault(Return(2));
02391
02392 Mock::VerifyAndClear(&b);
02393
02394
02395
02396 EXPECT_EQ(0, b.DoB(0));
02397 EXPECT_EQ(0, b.DoB(1));
02398 }
02399
02400
02401
02402 TEST(VerifyAndClearTest, CanCallManyTimes) {
02403 MockB b;
02404 ON_CALL(b, DoB())
02405 .WillByDefault(Return(1));
02406 Mock::VerifyAndClear(&b);
02407 Mock::VerifyAndClear(&b);
02408
02409 ON_CALL(b, DoB(_))
02410 .WillByDefault(Return(1));
02411 Mock::VerifyAndClear(&b);
02412
02413 EXPECT_EQ(0, b.DoB());
02414 EXPECT_EQ(0, b.DoB(1));
02415 }
02416
02417
02418 TEST(VerifyAndClearTest, Success) {
02419 MockB b;
02420 ON_CALL(b, DoB())
02421 .WillByDefault(Return(1));
02422 EXPECT_CALL(b, DoB(1))
02423 .WillOnce(Return(2));
02424
02425 b.DoB();
02426 b.DoB(1);
02427 ASSERT_TRUE(Mock::VerifyAndClear(&b));
02428
02429
02430
02431 EXPECT_EQ(0, b.DoB());
02432 EXPECT_EQ(0, b.DoB(1));
02433 }
02434
02435
02436 TEST(VerifyAndClearTest, Failure) {
02437 MockB b;
02438 ON_CALL(b, DoB(_))
02439 .WillByDefault(Return(1));
02440 EXPECT_CALL(b, DoB())
02441 .WillOnce(Return(2));
02442
02443 b.DoB(1);
02444 bool result = true;
02445 EXPECT_NONFATAL_FAILURE(result = Mock::VerifyAndClear(&b),
02446 "Actual: never called");
02447 ASSERT_FALSE(result);
02448
02449
02450
02451 EXPECT_EQ(0, b.DoB());
02452 EXPECT_EQ(0, b.DoB(1));
02453 }
02454
02455
02456
02457 TEST(VerifyAndClearTest, Const) {
02458 MockB b;
02459 ON_CALL(Const(b), DoB())
02460 .WillByDefault(Return(1));
02461
02462 EXPECT_CALL(Const(b), DoB())
02463 .WillOnce(DoDefault())
02464 .WillOnce(Return(2));
02465
02466 b.DoB();
02467 b.DoB();
02468 ASSERT_TRUE(Mock::VerifyAndClear(&b));
02469
02470
02471
02472 EXPECT_EQ(0, b.DoB());
02473 EXPECT_EQ(0, b.DoB(1));
02474 }
02475
02476
02477
02478 TEST(VerifyAndClearTest, CanSetDefaultActionsAndExpectationsAfterwards) {
02479 MockB b;
02480 ON_CALL(b, DoB())
02481 .WillByDefault(Return(1));
02482 EXPECT_CALL(b, DoB(_))
02483 .WillOnce(Return(2));
02484 b.DoB(1);
02485
02486 Mock::VerifyAndClear(&b);
02487
02488 EXPECT_CALL(b, DoB())
02489 .WillOnce(Return(3));
02490 ON_CALL(b, DoB(_))
02491 .WillByDefault(Return(4));
02492
02493 EXPECT_EQ(3, b.DoB());
02494 EXPECT_EQ(4, b.DoB(1));
02495 }
02496
02497
02498
02499 TEST(VerifyAndClearTest, DoesNotAffectOtherMockObjects) {
02500 MockA a;
02501 MockB b1;
02502 MockB b2;
02503
02504 ON_CALL(a, Binary(_, _))
02505 .WillByDefault(Return(true));
02506 EXPECT_CALL(a, Binary(_, _))
02507 .WillOnce(DoDefault())
02508 .WillOnce(Return(false));
02509
02510 ON_CALL(b1, DoB())
02511 .WillByDefault(Return(1));
02512 EXPECT_CALL(b1, DoB(_))
02513 .WillOnce(Return(2));
02514
02515 ON_CALL(b2, DoB())
02516 .WillByDefault(Return(3));
02517 EXPECT_CALL(b2, DoB(_));
02518
02519 b2.DoB(0);
02520 Mock::VerifyAndClear(&b2);
02521
02522
02523
02524 EXPECT_TRUE(a.Binary(0, 0));
02525 EXPECT_FALSE(a.Binary(0, 0));
02526
02527 EXPECT_EQ(1, b1.DoB());
02528 EXPECT_EQ(2, b1.DoB(0));
02529 }
02530
02531 TEST(VerifyAndClearTest,
02532 DestroyingChainedMocksDoesNotDeadlockThroughExpectations) {
02533 linked_ptr<MockA> a(new MockA);
02534 ReferenceHoldingMock test_mock;
02535
02536
02537 EXPECT_CALL(test_mock, AcceptReference(_))
02538 .WillRepeatedly(SetArgPointee<0>(a));
02539
02540
02541
02542 a.reset();
02543
02544
02545
02546
02547
02548
02549 }
02550
02551 TEST(VerifyAndClearTest,
02552 DestroyingChainedMocksDoesNotDeadlockThroughDefaultAction) {
02553 linked_ptr<MockA> a(new MockA);
02554 ReferenceHoldingMock test_mock;
02555
02556
02557 ON_CALL(test_mock, AcceptReference(_))
02558 .WillByDefault(SetArgPointee<0>(a));
02559
02560
02561
02562 a.reset();
02563
02564
02565
02566
02567
02568
02569 }
02570
02571
02572
02573
02574
02575
02576 TEST(SynchronizationTest, CanCallMockMethodInAction) {
02577 MockA a;
02578 MockC c;
02579 ON_CALL(a, DoA(_))
02580 .WillByDefault(IgnoreResult(InvokeWithoutArgs(&c,
02581 &MockC::NonVoidMethod)));
02582 EXPECT_CALL(a, DoA(1));
02583 EXPECT_CALL(a, DoA(1))
02584 .WillOnce(Invoke(&a, &MockA::DoA))
02585 .RetiresOnSaturation();
02586 EXPECT_CALL(c, NonVoidMethod());
02587
02588 a.DoA(1);
02589
02590
02591
02592
02593 }
02594
02595 }
02596
02597
02598
02599
02600 #if GMOCK_RENAME_MAIN
02601 int gmock_main(int argc, char **argv) {
02602 #else
02603 int main(int argc, char **argv) {
02604 #endif // GMOCK_RENAME_MAIN
02605 testing::InitGoogleMock(&argc, argv);
02606
02607
02608
02609 testing::GMOCK_FLAG(catch_leaked_mocks) = true;
02610 testing::GMOCK_FLAG(verbose) = testing::internal::kWarningVerbosity;
02611
02612 return RUN_ALL_TESTS();
02613 }