github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/src/third_party/googlemock/test/gmock-spec-builders_test.cc (about) 1 // Copyright 2007, Google Inc. 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // * Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // * Redistributions in binary form must reproduce the above 11 // copyright notice, this list of conditions and the following disclaimer 12 // in the documentation and/or other materials provided with the 13 // distribution. 14 // * Neither the name of Google Inc. nor the names of its 15 // contributors may be used to endorse or promote products derived from 16 // this software without specific prior written permission. 17 // 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 // 30 // Author: wan@google.com (Zhanyong Wan) 31 32 // Google Mock - a framework for writing C++ mock classes. 33 // 34 // This file tests the spec builder syntax. 35 36 #include "gmock/gmock-spec-builders.h" 37 38 #include <ostream> // NOLINT 39 #include <sstream> 40 #include <string> 41 42 #include "gmock/gmock.h" 43 #include "gmock/internal/gmock-port.h" 44 #include "gtest/gtest.h" 45 #include "gtest/gtest-spi.h" 46 #include "gtest/internal/gtest-port.h" 47 48 namespace testing { 49 namespace internal { 50 51 // Helper class for testing the Expectation class template. 52 class ExpectationTester { 53 public: 54 // Sets the call count of the given expectation to the given number. 55 void SetCallCount(int n, ExpectationBase* exp) { 56 exp->call_count_ = n; 57 } 58 }; 59 60 } // namespace internal 61 } // namespace testing 62 63 namespace { 64 65 using testing::_; 66 using testing::AnyNumber; 67 using testing::AtLeast; 68 using testing::AtMost; 69 using testing::Between; 70 using testing::Cardinality; 71 using testing::CardinalityInterface; 72 using testing::ContainsRegex; 73 using testing::Const; 74 using testing::DoAll; 75 using testing::DoDefault; 76 using testing::Eq; 77 using testing::Expectation; 78 using testing::ExpectationSet; 79 using testing::GMOCK_FLAG(verbose); 80 using testing::Gt; 81 using testing::InSequence; 82 using testing::Invoke; 83 using testing::InvokeWithoutArgs; 84 using testing::IsSubstring; 85 using testing::Lt; 86 using testing::Message; 87 using testing::Mock; 88 using testing::NaggyMock; 89 using testing::Ne; 90 using testing::Return; 91 using testing::Sequence; 92 using testing::SetArgPointee; 93 using testing::internal::ExpectationTester; 94 using testing::internal::FormatFileLocation; 95 using testing::internal::kErrorVerbosity; 96 using testing::internal::kInfoVerbosity; 97 using testing::internal::kWarningVerbosity; 98 using testing::internal::linked_ptr; 99 using testing::internal::string; 100 101 #if GTEST_HAS_STREAM_REDIRECTION 102 using testing::HasSubstr; 103 using testing::internal::CaptureStdout; 104 using testing::internal::GetCapturedStdout; 105 #endif 106 107 class Incomplete; 108 109 class MockIncomplete { 110 public: 111 // This line verifies that a mock method can take a by-reference 112 // argument of an incomplete type. 113 MOCK_METHOD1(ByRefFunc, void(const Incomplete& x)); 114 }; 115 116 // Tells Google Mock how to print a value of type Incomplete. 117 void PrintTo(const Incomplete& x, ::std::ostream* os); 118 119 TEST(MockMethodTest, CanInstantiateWithIncompleteArgType) { 120 // Even though this mock class contains a mock method that takes 121 // by-reference an argument whose type is incomplete, we can still 122 // use the mock, as long as Google Mock knows how to print the 123 // argument. 124 MockIncomplete incomplete; 125 EXPECT_CALL(incomplete, ByRefFunc(_)) 126 .Times(AnyNumber()); 127 } 128 129 // The definition of the printer for the argument type doesn't have to 130 // be visible where the mock is used. 131 void PrintTo(const Incomplete& /* x */, ::std::ostream* os) { 132 *os << "incomplete"; 133 } 134 135 class Result {}; 136 137 class MockA { 138 public: 139 MockA() {} 140 141 MOCK_METHOD1(DoA, void(int n)); // NOLINT 142 MOCK_METHOD1(ReturnResult, Result(int n)); // NOLINT 143 MOCK_METHOD2(Binary, bool(int x, int y)); // NOLINT 144 MOCK_METHOD2(ReturnInt, int(int x, int y)); // NOLINT 145 146 private: 147 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockA); 148 }; 149 150 class MockB { 151 public: 152 MockB() {} 153 154 MOCK_CONST_METHOD0(DoB, int()); // NOLINT 155 MOCK_METHOD1(DoB, int(int n)); // NOLINT 156 157 private: 158 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockB); 159 }; 160 161 class ReferenceHoldingMock { 162 public: 163 ReferenceHoldingMock() {} 164 165 MOCK_METHOD1(AcceptReference, void(linked_ptr<MockA>*)); 166 167 private: 168 GTEST_DISALLOW_COPY_AND_ASSIGN_(ReferenceHoldingMock); 169 }; 170 171 // Tests that EXPECT_CALL and ON_CALL compile in a presence of macro 172 // redefining a mock method name. This could happen, for example, when 173 // the tested code #includes Win32 API headers which define many APIs 174 // as macros, e.g. #define TextOut TextOutW. 175 176 #define Method MethodW 177 178 class CC { 179 public: 180 virtual ~CC() {} 181 virtual int Method() = 0; 182 }; 183 class MockCC : public CC { 184 public: 185 MockCC() {} 186 187 MOCK_METHOD0(Method, int()); 188 189 private: 190 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockCC); 191 }; 192 193 // Tests that a method with expanded name compiles. 194 TEST(OnCallSyntaxTest, CompilesWithMethodNameExpandedFromMacro) { 195 MockCC cc; 196 ON_CALL(cc, Method()); 197 } 198 199 // Tests that the method with expanded name not only compiles but runs 200 // and returns a correct value, too. 201 TEST(OnCallSyntaxTest, WorksWithMethodNameExpandedFromMacro) { 202 MockCC cc; 203 ON_CALL(cc, Method()).WillByDefault(Return(42)); 204 EXPECT_EQ(42, cc.Method()); 205 } 206 207 // Tests that a method with expanded name compiles. 208 TEST(ExpectCallSyntaxTest, CompilesWithMethodNameExpandedFromMacro) { 209 MockCC cc; 210 EXPECT_CALL(cc, Method()); 211 cc.Method(); 212 } 213 214 // Tests that it works, too. 215 TEST(ExpectCallSyntaxTest, WorksWithMethodNameExpandedFromMacro) { 216 MockCC cc; 217 EXPECT_CALL(cc, Method()).WillOnce(Return(42)); 218 EXPECT_EQ(42, cc.Method()); 219 } 220 221 #undef Method // Done with macro redefinition tests. 222 223 // Tests that ON_CALL evaluates its arguments exactly once as promised 224 // by Google Mock. 225 TEST(OnCallSyntaxTest, EvaluatesFirstArgumentOnce) { 226 MockA a; 227 MockA* pa = &a; 228 229 ON_CALL(*pa++, DoA(_)); 230 EXPECT_EQ(&a + 1, pa); 231 } 232 233 TEST(OnCallSyntaxTest, EvaluatesSecondArgumentOnce) { 234 MockA a; 235 int n = 0; 236 237 ON_CALL(a, DoA(n++)); 238 EXPECT_EQ(1, n); 239 } 240 241 // Tests that the syntax of ON_CALL() is enforced at run time. 242 243 TEST(OnCallSyntaxTest, WithIsOptional) { 244 MockA a; 245 246 ON_CALL(a, DoA(5)) 247 .WillByDefault(Return()); 248 ON_CALL(a, DoA(_)) 249 .With(_) 250 .WillByDefault(Return()); 251 } 252 253 TEST(OnCallSyntaxTest, WithCanAppearAtMostOnce) { 254 MockA a; 255 256 EXPECT_NONFATAL_FAILURE({ // NOLINT 257 ON_CALL(a, ReturnResult(_)) 258 .With(_) 259 .With(_) 260 .WillByDefault(Return(Result())); 261 }, ".With() cannot appear more than once in an ON_CALL()"); 262 } 263 264 TEST(OnCallSyntaxTest, WillByDefaultIsMandatory) { 265 MockA a; 266 267 EXPECT_DEATH_IF_SUPPORTED({ 268 ON_CALL(a, DoA(5)); 269 a.DoA(5); 270 }, ""); 271 } 272 273 TEST(OnCallSyntaxTest, WillByDefaultCanAppearAtMostOnce) { 274 MockA a; 275 276 EXPECT_NONFATAL_FAILURE({ // NOLINT 277 ON_CALL(a, DoA(5)) 278 .WillByDefault(Return()) 279 .WillByDefault(Return()); 280 }, ".WillByDefault() must appear exactly once in an ON_CALL()"); 281 } 282 283 // Tests that EXPECT_CALL evaluates its arguments exactly once as 284 // promised by Google Mock. 285 TEST(ExpectCallSyntaxTest, EvaluatesFirstArgumentOnce) { 286 MockA a; 287 MockA* pa = &a; 288 289 EXPECT_CALL(*pa++, DoA(_)); 290 a.DoA(0); 291 EXPECT_EQ(&a + 1, pa); 292 } 293 294 TEST(ExpectCallSyntaxTest, EvaluatesSecondArgumentOnce) { 295 MockA a; 296 int n = 0; 297 298 EXPECT_CALL(a, DoA(n++)); 299 a.DoA(0); 300 EXPECT_EQ(1, n); 301 } 302 303 // Tests that the syntax of EXPECT_CALL() is enforced at run time. 304 305 TEST(ExpectCallSyntaxTest, WithIsOptional) { 306 MockA a; 307 308 EXPECT_CALL(a, DoA(5)) 309 .Times(0); 310 EXPECT_CALL(a, DoA(6)) 311 .With(_) 312 .Times(0); 313 } 314 315 TEST(ExpectCallSyntaxTest, WithCanAppearAtMostOnce) { 316 MockA a; 317 318 EXPECT_NONFATAL_FAILURE({ // NOLINT 319 EXPECT_CALL(a, DoA(6)) 320 .With(_) 321 .With(_); 322 }, ".With() cannot appear more than once in an EXPECT_CALL()"); 323 324 a.DoA(6); 325 } 326 327 TEST(ExpectCallSyntaxTest, WithMustBeFirstClause) { 328 MockA a; 329 330 EXPECT_NONFATAL_FAILURE({ // NOLINT 331 EXPECT_CALL(a, DoA(1)) 332 .Times(1) 333 .With(_); 334 }, ".With() must be the first clause in an EXPECT_CALL()"); 335 336 a.DoA(1); 337 338 EXPECT_NONFATAL_FAILURE({ // NOLINT 339 EXPECT_CALL(a, DoA(2)) 340 .WillOnce(Return()) 341 .With(_); 342 }, ".With() must be the first clause in an EXPECT_CALL()"); 343 344 a.DoA(2); 345 } 346 347 TEST(ExpectCallSyntaxTest, TimesCanBeInferred) { 348 MockA a; 349 350 EXPECT_CALL(a, DoA(1)) 351 .WillOnce(Return()); 352 353 EXPECT_CALL(a, DoA(2)) 354 .WillOnce(Return()) 355 .WillRepeatedly(Return()); 356 357 a.DoA(1); 358 a.DoA(2); 359 a.DoA(2); 360 } 361 362 TEST(ExpectCallSyntaxTest, TimesCanAppearAtMostOnce) { 363 MockA a; 364 365 EXPECT_NONFATAL_FAILURE({ // NOLINT 366 EXPECT_CALL(a, DoA(1)) 367 .Times(1) 368 .Times(2); 369 }, ".Times() cannot appear more than once in an EXPECT_CALL()"); 370 371 a.DoA(1); 372 a.DoA(1); 373 } 374 375 TEST(ExpectCallSyntaxTest, TimesMustBeBeforeInSequence) { 376 MockA a; 377 Sequence s; 378 379 EXPECT_NONFATAL_FAILURE({ // NOLINT 380 EXPECT_CALL(a, DoA(1)) 381 .InSequence(s) 382 .Times(1); 383 }, ".Times() cannot appear after "); 384 385 a.DoA(1); 386 } 387 388 TEST(ExpectCallSyntaxTest, InSequenceIsOptional) { 389 MockA a; 390 Sequence s; 391 392 EXPECT_CALL(a, DoA(1)); 393 EXPECT_CALL(a, DoA(2)) 394 .InSequence(s); 395 396 a.DoA(1); 397 a.DoA(2); 398 } 399 400 TEST(ExpectCallSyntaxTest, InSequenceCanAppearMultipleTimes) { 401 MockA a; 402 Sequence s1, s2; 403 404 EXPECT_CALL(a, DoA(1)) 405 .InSequence(s1, s2) 406 .InSequence(s1); 407 408 a.DoA(1); 409 } 410 411 TEST(ExpectCallSyntaxTest, InSequenceMustBeBeforeAfter) { 412 MockA a; 413 Sequence s; 414 415 Expectation e = EXPECT_CALL(a, DoA(1)) 416 .Times(AnyNumber()); 417 EXPECT_NONFATAL_FAILURE({ // NOLINT 418 EXPECT_CALL(a, DoA(2)) 419 .After(e) 420 .InSequence(s); 421 }, ".InSequence() cannot appear after "); 422 423 a.DoA(2); 424 } 425 426 TEST(ExpectCallSyntaxTest, InSequenceMustBeBeforeWillOnce) { 427 MockA a; 428 Sequence s; 429 430 EXPECT_NONFATAL_FAILURE({ // NOLINT 431 EXPECT_CALL(a, DoA(1)) 432 .WillOnce(Return()) 433 .InSequence(s); 434 }, ".InSequence() cannot appear after "); 435 436 a.DoA(1); 437 } 438 439 TEST(ExpectCallSyntaxTest, AfterMustBeBeforeWillOnce) { 440 MockA a; 441 442 Expectation e = EXPECT_CALL(a, DoA(1)); 443 EXPECT_NONFATAL_FAILURE({ 444 EXPECT_CALL(a, DoA(2)) 445 .WillOnce(Return()) 446 .After(e); 447 }, ".After() cannot appear after "); 448 449 a.DoA(1); 450 a.DoA(2); 451 } 452 453 TEST(ExpectCallSyntaxTest, WillIsOptional) { 454 MockA a; 455 456 EXPECT_CALL(a, DoA(1)); 457 EXPECT_CALL(a, DoA(2)) 458 .WillOnce(Return()); 459 460 a.DoA(1); 461 a.DoA(2); 462 } 463 464 TEST(ExpectCallSyntaxTest, WillCanAppearMultipleTimes) { 465 MockA a; 466 467 EXPECT_CALL(a, DoA(1)) 468 .Times(AnyNumber()) 469 .WillOnce(Return()) 470 .WillOnce(Return()) 471 .WillOnce(Return()); 472 } 473 474 TEST(ExpectCallSyntaxTest, WillMustBeBeforeWillRepeatedly) { 475 MockA a; 476 477 EXPECT_NONFATAL_FAILURE({ // NOLINT 478 EXPECT_CALL(a, DoA(1)) 479 .WillRepeatedly(Return()) 480 .WillOnce(Return()); 481 }, ".WillOnce() cannot appear after "); 482 483 a.DoA(1); 484 } 485 486 TEST(ExpectCallSyntaxTest, WillRepeatedlyIsOptional) { 487 MockA a; 488 489 EXPECT_CALL(a, DoA(1)) 490 .WillOnce(Return()); 491 EXPECT_CALL(a, DoA(2)) 492 .WillOnce(Return()) 493 .WillRepeatedly(Return()); 494 495 a.DoA(1); 496 a.DoA(2); 497 a.DoA(2); 498 } 499 500 TEST(ExpectCallSyntaxTest, WillRepeatedlyCannotAppearMultipleTimes) { 501 MockA a; 502 503 EXPECT_NONFATAL_FAILURE({ // NOLINT 504 EXPECT_CALL(a, DoA(1)) 505 .WillRepeatedly(Return()) 506 .WillRepeatedly(Return()); 507 }, ".WillRepeatedly() cannot appear more than once in an " 508 "EXPECT_CALL()"); 509 } 510 511 TEST(ExpectCallSyntaxTest, WillRepeatedlyMustBeBeforeRetiresOnSaturation) { 512 MockA a; 513 514 EXPECT_NONFATAL_FAILURE({ // NOLINT 515 EXPECT_CALL(a, DoA(1)) 516 .RetiresOnSaturation() 517 .WillRepeatedly(Return()); 518 }, ".WillRepeatedly() cannot appear after "); 519 } 520 521 TEST(ExpectCallSyntaxTest, RetiresOnSaturationIsOptional) { 522 MockA a; 523 524 EXPECT_CALL(a, DoA(1)); 525 EXPECT_CALL(a, DoA(1)) 526 .RetiresOnSaturation(); 527 528 a.DoA(1); 529 a.DoA(1); 530 } 531 532 TEST(ExpectCallSyntaxTest, RetiresOnSaturationCannotAppearMultipleTimes) { 533 MockA a; 534 535 EXPECT_NONFATAL_FAILURE({ // NOLINT 536 EXPECT_CALL(a, DoA(1)) 537 .RetiresOnSaturation() 538 .RetiresOnSaturation(); 539 }, ".RetiresOnSaturation() cannot appear more than once"); 540 541 a.DoA(1); 542 } 543 544 TEST(ExpectCallSyntaxTest, DefaultCardinalityIsOnce) { 545 { 546 MockA a; 547 EXPECT_CALL(a, DoA(1)); 548 a.DoA(1); 549 } 550 EXPECT_NONFATAL_FAILURE({ // NOLINT 551 MockA a; 552 EXPECT_CALL(a, DoA(1)); 553 }, "to be called once"); 554 EXPECT_NONFATAL_FAILURE({ // NOLINT 555 MockA a; 556 EXPECT_CALL(a, DoA(1)); 557 a.DoA(1); 558 a.DoA(1); 559 }, "to be called once"); 560 } 561 562 #if GTEST_HAS_STREAM_REDIRECTION 563 564 // Tests that Google Mock doesn't print a warning when the number of 565 // WillOnce() is adequate. 566 TEST(ExpectCallSyntaxTest, DoesNotWarnOnAdequateActionCount) { 567 CaptureStdout(); 568 { 569 MockB b; 570 571 // It's always fine to omit WillOnce() entirely. 572 EXPECT_CALL(b, DoB()) 573 .Times(0); 574 EXPECT_CALL(b, DoB(1)) 575 .Times(AtMost(1)); 576 EXPECT_CALL(b, DoB(2)) 577 .Times(1) 578 .WillRepeatedly(Return(1)); 579 580 // It's fine for the number of WillOnce()s to equal the upper bound. 581 EXPECT_CALL(b, DoB(3)) 582 .Times(Between(1, 2)) 583 .WillOnce(Return(1)) 584 .WillOnce(Return(2)); 585 586 // It's fine for the number of WillOnce()s to be smaller than the 587 // upper bound when there is a WillRepeatedly(). 588 EXPECT_CALL(b, DoB(4)) 589 .Times(AtMost(3)) 590 .WillOnce(Return(1)) 591 .WillRepeatedly(Return(2)); 592 593 // Satisfies the above expectations. 594 b.DoB(2); 595 b.DoB(3); 596 } 597 EXPECT_STREQ("", GetCapturedStdout().c_str()); 598 } 599 600 // Tests that Google Mock warns on having too many actions in an 601 // expectation compared to its cardinality. 602 TEST(ExpectCallSyntaxTest, WarnsOnTooManyActions) { 603 CaptureStdout(); 604 { 605 MockB b; 606 607 // Warns when the number of WillOnce()s is larger than the upper bound. 608 EXPECT_CALL(b, DoB()) 609 .Times(0) 610 .WillOnce(Return(1)); // #1 611 EXPECT_CALL(b, DoB()) 612 .Times(AtMost(1)) 613 .WillOnce(Return(1)) 614 .WillOnce(Return(2)); // #2 615 EXPECT_CALL(b, DoB(1)) 616 .Times(1) 617 .WillOnce(Return(1)) 618 .WillOnce(Return(2)) 619 .RetiresOnSaturation(); // #3 620 621 // Warns when the number of WillOnce()s equals the upper bound and 622 // there is a WillRepeatedly(). 623 EXPECT_CALL(b, DoB()) 624 .Times(0) 625 .WillRepeatedly(Return(1)); // #4 626 EXPECT_CALL(b, DoB(2)) 627 .Times(1) 628 .WillOnce(Return(1)) 629 .WillRepeatedly(Return(2)); // #5 630 631 // Satisfies the above expectations. 632 b.DoB(1); 633 b.DoB(2); 634 } 635 const std::string output = GetCapturedStdout(); 636 EXPECT_PRED_FORMAT2( 637 IsSubstring, 638 "Too many actions specified in EXPECT_CALL(b, DoB())...\n" 639 "Expected to be never called, but has 1 WillOnce().", 640 output); // #1 641 EXPECT_PRED_FORMAT2( 642 IsSubstring, 643 "Too many actions specified in EXPECT_CALL(b, DoB())...\n" 644 "Expected to be called at most once, " 645 "but has 2 WillOnce()s.", 646 output); // #2 647 EXPECT_PRED_FORMAT2( 648 IsSubstring, 649 "Too many actions specified in EXPECT_CALL(b, DoB(1))...\n" 650 "Expected to be called once, but has 2 WillOnce()s.", 651 output); // #3 652 EXPECT_PRED_FORMAT2( 653 IsSubstring, 654 "Too many actions specified in EXPECT_CALL(b, DoB())...\n" 655 "Expected to be never called, but has 0 WillOnce()s " 656 "and a WillRepeatedly().", 657 output); // #4 658 EXPECT_PRED_FORMAT2( 659 IsSubstring, 660 "Too many actions specified in EXPECT_CALL(b, DoB(2))...\n" 661 "Expected to be called once, but has 1 WillOnce() " 662 "and a WillRepeatedly().", 663 output); // #5 664 } 665 666 // Tests that Google Mock warns on having too few actions in an 667 // expectation compared to its cardinality. 668 TEST(ExpectCallSyntaxTest, WarnsOnTooFewActions) { 669 MockB b; 670 671 EXPECT_CALL(b, DoB()) 672 .Times(Between(2, 3)) 673 .WillOnce(Return(1)); 674 675 CaptureStdout(); 676 b.DoB(); 677 const std::string output = GetCapturedStdout(); 678 EXPECT_PRED_FORMAT2( 679 IsSubstring, 680 "Too few actions specified in EXPECT_CALL(b, DoB())...\n" 681 "Expected to be called between 2 and 3 times, " 682 "but has only 1 WillOnce().", 683 output); 684 b.DoB(); 685 } 686 687 #endif // GTEST_HAS_STREAM_REDIRECTION 688 689 // Tests the semantics of ON_CALL(). 690 691 // Tests that the built-in default action is taken when no ON_CALL() 692 // is specified. 693 TEST(OnCallTest, TakesBuiltInDefaultActionWhenNoOnCall) { 694 MockB b; 695 EXPECT_CALL(b, DoB()); 696 697 EXPECT_EQ(0, b.DoB()); 698 } 699 700 // Tests that the built-in default action is taken when no ON_CALL() 701 // matches the invocation. 702 TEST(OnCallTest, TakesBuiltInDefaultActionWhenNoOnCallMatches) { 703 MockB b; 704 ON_CALL(b, DoB(1)) 705 .WillByDefault(Return(1)); 706 EXPECT_CALL(b, DoB(_)); 707 708 EXPECT_EQ(0, b.DoB(2)); 709 } 710 711 // Tests that the last matching ON_CALL() action is taken. 712 TEST(OnCallTest, PicksLastMatchingOnCall) { 713 MockB b; 714 ON_CALL(b, DoB(_)) 715 .WillByDefault(Return(3)); 716 ON_CALL(b, DoB(2)) 717 .WillByDefault(Return(2)); 718 ON_CALL(b, DoB(1)) 719 .WillByDefault(Return(1)); 720 EXPECT_CALL(b, DoB(_)); 721 722 EXPECT_EQ(2, b.DoB(2)); 723 } 724 725 // Tests the semantics of EXPECT_CALL(). 726 727 // Tests that any call is allowed when no EXPECT_CALL() is specified. 728 TEST(ExpectCallTest, AllowsAnyCallWhenNoSpec) { 729 MockB b; 730 EXPECT_CALL(b, DoB()); 731 // There is no expectation on DoB(int). 732 733 b.DoB(); 734 735 // DoB(int) can be called any number of times. 736 b.DoB(1); 737 b.DoB(2); 738 } 739 740 // Tests that the last matching EXPECT_CALL() fires. 741 TEST(ExpectCallTest, PicksLastMatchingExpectCall) { 742 MockB b; 743 EXPECT_CALL(b, DoB(_)) 744 .WillRepeatedly(Return(2)); 745 EXPECT_CALL(b, DoB(1)) 746 .WillRepeatedly(Return(1)); 747 748 EXPECT_EQ(1, b.DoB(1)); 749 } 750 751 // Tests lower-bound violation. 752 TEST(ExpectCallTest, CatchesTooFewCalls) { 753 EXPECT_NONFATAL_FAILURE({ // NOLINT 754 MockB b; 755 EXPECT_CALL(b, DoB(5)) 756 .Times(AtLeast(2)); 757 758 b.DoB(5); 759 }, "Actual function call count doesn't match EXPECT_CALL(b, DoB(5))...\n" 760 " Expected: to be called at least twice\n" 761 " Actual: called once - unsatisfied and active"); 762 } 763 764 // Tests that the cardinality can be inferred when no Times(...) is 765 // specified. 766 TEST(ExpectCallTest, InfersCardinalityWhenThereIsNoWillRepeatedly) { 767 { 768 MockB b; 769 EXPECT_CALL(b, DoB()) 770 .WillOnce(Return(1)) 771 .WillOnce(Return(2)); 772 773 EXPECT_EQ(1, b.DoB()); 774 EXPECT_EQ(2, b.DoB()); 775 } 776 777 EXPECT_NONFATAL_FAILURE({ // NOLINT 778 MockB b; 779 EXPECT_CALL(b, DoB()) 780 .WillOnce(Return(1)) 781 .WillOnce(Return(2)); 782 783 EXPECT_EQ(1, b.DoB()); 784 }, "to be called twice"); 785 786 { // NOLINT 787 MockB b; 788 EXPECT_CALL(b, DoB()) 789 .WillOnce(Return(1)) 790 .WillOnce(Return(2)); 791 792 EXPECT_EQ(1, b.DoB()); 793 EXPECT_EQ(2, b.DoB()); 794 EXPECT_NONFATAL_FAILURE(b.DoB(), "to be called twice"); 795 } 796 } 797 798 TEST(ExpectCallTest, InfersCardinality1WhenThereIsWillRepeatedly) { 799 { 800 MockB b; 801 EXPECT_CALL(b, DoB()) 802 .WillOnce(Return(1)) 803 .WillRepeatedly(Return(2)); 804 805 EXPECT_EQ(1, b.DoB()); 806 } 807 808 { // NOLINT 809 MockB b; 810 EXPECT_CALL(b, DoB()) 811 .WillOnce(Return(1)) 812 .WillRepeatedly(Return(2)); 813 814 EXPECT_EQ(1, b.DoB()); 815 EXPECT_EQ(2, b.DoB()); 816 EXPECT_EQ(2, b.DoB()); 817 } 818 819 EXPECT_NONFATAL_FAILURE({ // NOLINT 820 MockB b; 821 EXPECT_CALL(b, DoB()) 822 .WillOnce(Return(1)) 823 .WillRepeatedly(Return(2)); 824 }, "to be called at least once"); 825 } 826 827 // Tests that the n-th action is taken for the n-th matching 828 // invocation. 829 TEST(ExpectCallTest, NthMatchTakesNthAction) { 830 MockB b; 831 EXPECT_CALL(b, DoB()) 832 .WillOnce(Return(1)) 833 .WillOnce(Return(2)) 834 .WillOnce(Return(3)); 835 836 EXPECT_EQ(1, b.DoB()); 837 EXPECT_EQ(2, b.DoB()); 838 EXPECT_EQ(3, b.DoB()); 839 } 840 841 // Tests that the WillRepeatedly() action is taken when the WillOnce(...) 842 // list is exhausted. 843 TEST(ExpectCallTest, TakesRepeatedActionWhenWillListIsExhausted) { 844 MockB b; 845 EXPECT_CALL(b, DoB()) 846 .WillOnce(Return(1)) 847 .WillRepeatedly(Return(2)); 848 849 EXPECT_EQ(1, b.DoB()); 850 EXPECT_EQ(2, b.DoB()); 851 EXPECT_EQ(2, b.DoB()); 852 } 853 854 #if GTEST_HAS_STREAM_REDIRECTION 855 856 // Tests that the default action is taken when the WillOnce(...) list is 857 // exhausted and there is no WillRepeatedly(). 858 TEST(ExpectCallTest, TakesDefaultActionWhenWillListIsExhausted) { 859 MockB b; 860 EXPECT_CALL(b, DoB(_)) 861 .Times(1); 862 EXPECT_CALL(b, DoB()) 863 .Times(AnyNumber()) 864 .WillOnce(Return(1)) 865 .WillOnce(Return(2)); 866 867 CaptureStdout(); 868 EXPECT_EQ(0, b.DoB(1)); // Shouldn't generate a warning as the 869 // expectation has no action clause at all. 870 EXPECT_EQ(1, b.DoB()); 871 EXPECT_EQ(2, b.DoB()); 872 const std::string output1 = GetCapturedStdout(); 873 EXPECT_STREQ("", output1.c_str()); 874 875 CaptureStdout(); 876 EXPECT_EQ(0, b.DoB()); 877 EXPECT_EQ(0, b.DoB()); 878 const std::string output2 = GetCapturedStdout(); 879 EXPECT_THAT(output2.c_str(), 880 HasSubstr("Actions ran out in EXPECT_CALL(b, DoB())...\n" 881 "Called 3 times, but only 2 WillOnce()s are specified" 882 " - returning default value.")); 883 EXPECT_THAT(output2.c_str(), 884 HasSubstr("Actions ran out in EXPECT_CALL(b, DoB())...\n" 885 "Called 4 times, but only 2 WillOnce()s are specified" 886 " - returning default value.")); 887 } 888 889 TEST(FunctionMockerMessageTest, ReportsExpectCallLocationForExhausedActions) { 890 MockB b; 891 std::string expect_call_location = FormatFileLocation(__FILE__, __LINE__ + 1); 892 EXPECT_CALL(b, DoB()).Times(AnyNumber()).WillOnce(Return(1)); 893 894 EXPECT_EQ(1, b.DoB()); 895 896 CaptureStdout(); 897 EXPECT_EQ(0, b.DoB()); 898 const std::string output = GetCapturedStdout(); 899 // The warning message should contain the call location. 900 EXPECT_PRED_FORMAT2(IsSubstring, expect_call_location, output); 901 } 902 903 TEST(FunctionMockerMessageTest, 904 ReportsDefaultActionLocationOfUninterestingCallsForNaggyMock) { 905 std::string on_call_location; 906 CaptureStdout(); 907 { 908 NaggyMock<MockB> b; 909 on_call_location = FormatFileLocation(__FILE__, __LINE__ + 1); 910 ON_CALL(b, DoB(_)).WillByDefault(Return(0)); 911 b.DoB(0); 912 } 913 EXPECT_PRED_FORMAT2(IsSubstring, on_call_location, GetCapturedStdout()); 914 } 915 916 #endif // GTEST_HAS_STREAM_REDIRECTION 917 918 // Tests that an uninteresting call performs the default action. 919 TEST(UninterestingCallTest, DoesDefaultAction) { 920 // When there is an ON_CALL() statement, the action specified by it 921 // should be taken. 922 MockA a; 923 ON_CALL(a, Binary(_, _)) 924 .WillByDefault(Return(true)); 925 EXPECT_TRUE(a.Binary(1, 2)); 926 927 // When there is no ON_CALL(), the default value for the return type 928 // should be returned. 929 MockB b; 930 EXPECT_EQ(0, b.DoB()); 931 } 932 933 // Tests that an unexpected call performs the default action. 934 TEST(UnexpectedCallTest, DoesDefaultAction) { 935 // When there is an ON_CALL() statement, the action specified by it 936 // should be taken. 937 MockA a; 938 ON_CALL(a, Binary(_, _)) 939 .WillByDefault(Return(true)); 940 EXPECT_CALL(a, Binary(0, 0)); 941 a.Binary(0, 0); 942 bool result = false; 943 EXPECT_NONFATAL_FAILURE(result = a.Binary(1, 2), 944 "Unexpected mock function call"); 945 EXPECT_TRUE(result); 946 947 // When there is no ON_CALL(), the default value for the return type 948 // should be returned. 949 MockB b; 950 EXPECT_CALL(b, DoB(0)) 951 .Times(0); 952 int n = -1; 953 EXPECT_NONFATAL_FAILURE(n = b.DoB(1), 954 "Unexpected mock function call"); 955 EXPECT_EQ(0, n); 956 } 957 958 // Tests that when an unexpected void function generates the right 959 // failure message. 960 TEST(UnexpectedCallTest, GeneratesFailureForVoidFunction) { 961 // First, tests the message when there is only one EXPECT_CALL(). 962 MockA a1; 963 EXPECT_CALL(a1, DoA(1)); 964 a1.DoA(1); 965 // Ideally we should match the failure message against a regex, but 966 // EXPECT_NONFATAL_FAILURE doesn't support that, so we test for 967 // multiple sub-strings instead. 968 EXPECT_NONFATAL_FAILURE( 969 a1.DoA(9), 970 "Unexpected mock function call - returning directly.\n" 971 " Function call: DoA(9)\n" 972 "Google Mock tried the following 1 expectation, but it didn't match:"); 973 EXPECT_NONFATAL_FAILURE( 974 a1.DoA(9), 975 " Expected arg #0: is equal to 1\n" 976 " Actual: 9\n" 977 " Expected: to be called once\n" 978 " Actual: called once - saturated and active"); 979 980 // Next, tests the message when there are more than one EXPECT_CALL(). 981 MockA a2; 982 EXPECT_CALL(a2, DoA(1)); 983 EXPECT_CALL(a2, DoA(3)); 984 a2.DoA(1); 985 EXPECT_NONFATAL_FAILURE( 986 a2.DoA(2), 987 "Unexpected mock function call - returning directly.\n" 988 " Function call: DoA(2)\n" 989 "Google Mock tried the following 2 expectations, but none matched:"); 990 EXPECT_NONFATAL_FAILURE( 991 a2.DoA(2), 992 "tried expectation #0: EXPECT_CALL(a2, DoA(1))...\n" 993 " Expected arg #0: is equal to 1\n" 994 " Actual: 2\n" 995 " Expected: to be called once\n" 996 " Actual: called once - saturated and active"); 997 EXPECT_NONFATAL_FAILURE( 998 a2.DoA(2), 999 "tried expectation #1: EXPECT_CALL(a2, DoA(3))...\n" 1000 " Expected arg #0: is equal to 3\n" 1001 " Actual: 2\n" 1002 " Expected: to be called once\n" 1003 " Actual: never called - unsatisfied and active"); 1004 a2.DoA(3); 1005 } 1006 1007 // Tests that an unexpected non-void function generates the right 1008 // failure message. 1009 TEST(UnexpectedCallTest, GeneartesFailureForNonVoidFunction) { 1010 MockB b1; 1011 EXPECT_CALL(b1, DoB(1)); 1012 b1.DoB(1); 1013 EXPECT_NONFATAL_FAILURE( 1014 b1.DoB(2), 1015 "Unexpected mock function call - returning default value.\n" 1016 " Function call: DoB(2)\n" 1017 " Returns: 0\n" 1018 "Google Mock tried the following 1 expectation, but it didn't match:"); 1019 EXPECT_NONFATAL_FAILURE( 1020 b1.DoB(2), 1021 " Expected arg #0: is equal to 1\n" 1022 " Actual: 2\n" 1023 " Expected: to be called once\n" 1024 " Actual: called once - saturated and active"); 1025 } 1026 1027 // Tests that Google Mock explains that an retired expectation doesn't 1028 // match the call. 1029 TEST(UnexpectedCallTest, RetiredExpectation) { 1030 MockB b; 1031 EXPECT_CALL(b, DoB(1)) 1032 .RetiresOnSaturation(); 1033 1034 b.DoB(1); 1035 EXPECT_NONFATAL_FAILURE( 1036 b.DoB(1), 1037 " Expected: the expectation is active\n" 1038 " Actual: it is retired"); 1039 } 1040 1041 // Tests that Google Mock explains that an expectation that doesn't 1042 // match the arguments doesn't match the call. 1043 TEST(UnexpectedCallTest, UnmatchedArguments) { 1044 MockB b; 1045 EXPECT_CALL(b, DoB(1)); 1046 1047 EXPECT_NONFATAL_FAILURE( 1048 b.DoB(2), 1049 " Expected arg #0: is equal to 1\n" 1050 " Actual: 2\n"); 1051 b.DoB(1); 1052 } 1053 1054 // Tests that Google Mock explains that an expectation with 1055 // unsatisfied pre-requisites doesn't match the call. 1056 TEST(UnexpectedCallTest, UnsatisifiedPrerequisites) { 1057 Sequence s1, s2; 1058 MockB b; 1059 EXPECT_CALL(b, DoB(1)) 1060 .InSequence(s1); 1061 EXPECT_CALL(b, DoB(2)) 1062 .Times(AnyNumber()) 1063 .InSequence(s1); 1064 EXPECT_CALL(b, DoB(3)) 1065 .InSequence(s2); 1066 EXPECT_CALL(b, DoB(4)) 1067 .InSequence(s1, s2); 1068 1069 ::testing::TestPartResultArray failures; 1070 { 1071 ::testing::ScopedFakeTestPartResultReporter reporter(&failures); 1072 b.DoB(4); 1073 // Now 'failures' contains the Google Test failures generated by 1074 // the above statement. 1075 } 1076 1077 // There should be one non-fatal failure. 1078 ASSERT_EQ(1, failures.size()); 1079 const ::testing::TestPartResult& r = failures.GetTestPartResult(0); 1080 EXPECT_EQ(::testing::TestPartResult::kNonFatalFailure, r.type()); 1081 1082 // Verifies that the failure message contains the two unsatisfied 1083 // pre-requisites but not the satisfied one. 1084 #if GTEST_USES_PCRE 1085 EXPECT_THAT(r.message(), ContainsRegex( 1086 // PCRE has trouble using (.|\n) to match any character, but 1087 // supports the (?s) prefix for using . to match any character. 1088 "(?s)the following immediate pre-requisites are not satisfied:\n" 1089 ".*: pre-requisite #0\n" 1090 ".*: pre-requisite #1")); 1091 #elif GTEST_USES_POSIX_RE 1092 EXPECT_THAT(r.message(), ContainsRegex( 1093 // POSIX RE doesn't understand the (?s) prefix, but has no trouble 1094 // with (.|\n). 1095 "the following immediate pre-requisites are not satisfied:\n" 1096 "(.|\n)*: pre-requisite #0\n" 1097 "(.|\n)*: pre-requisite #1")); 1098 #else 1099 // We can only use Google Test's own simple regex. 1100 EXPECT_THAT(r.message(), ContainsRegex( 1101 "the following immediate pre-requisites are not satisfied:")); 1102 EXPECT_THAT(r.message(), ContainsRegex(": pre-requisite #0")); 1103 EXPECT_THAT(r.message(), ContainsRegex(": pre-requisite #1")); 1104 #endif // GTEST_USES_PCRE 1105 1106 b.DoB(1); 1107 b.DoB(3); 1108 b.DoB(4); 1109 } 1110 1111 TEST(UndefinedReturnValueTest, ReturnValueIsMandatory) { 1112 MockA a; 1113 // TODO(wan@google.com): We should really verify the output message, 1114 // but we cannot yet due to that EXPECT_DEATH only captures stderr 1115 // while Google Mock logs to stdout. 1116 #if GTEST_HAS_EXCEPTIONS 1117 EXPECT_ANY_THROW(a.ReturnResult(1)); 1118 #else 1119 EXPECT_DEATH_IF_SUPPORTED(a.ReturnResult(1), ""); 1120 #endif 1121 } 1122 1123 // Tests that an excessive call (one whose arguments match the 1124 // matchers but is called too many times) performs the default action. 1125 TEST(ExcessiveCallTest, DoesDefaultAction) { 1126 // When there is an ON_CALL() statement, the action specified by it 1127 // should be taken. 1128 MockA a; 1129 ON_CALL(a, Binary(_, _)) 1130 .WillByDefault(Return(true)); 1131 EXPECT_CALL(a, Binary(0, 0)); 1132 a.Binary(0, 0); 1133 bool result = false; 1134 EXPECT_NONFATAL_FAILURE(result = a.Binary(0, 0), 1135 "Mock function called more times than expected"); 1136 EXPECT_TRUE(result); 1137 1138 // When there is no ON_CALL(), the default value for the return type 1139 // should be returned. 1140 MockB b; 1141 EXPECT_CALL(b, DoB(0)) 1142 .Times(0); 1143 int n = -1; 1144 EXPECT_NONFATAL_FAILURE(n = b.DoB(0), 1145 "Mock function called more times than expected"); 1146 EXPECT_EQ(0, n); 1147 } 1148 1149 // Tests that when a void function is called too many times, 1150 // the failure message contains the argument values. 1151 TEST(ExcessiveCallTest, GeneratesFailureForVoidFunction) { 1152 MockA a; 1153 EXPECT_CALL(a, DoA(_)) 1154 .Times(0); 1155 EXPECT_NONFATAL_FAILURE( 1156 a.DoA(9), 1157 "Mock function called more times than expected - returning directly.\n" 1158 " Function call: DoA(9)\n" 1159 " Expected: to be never called\n" 1160 " Actual: called once - over-saturated and active"); 1161 } 1162 1163 // Tests that when a non-void function is called too many times, the 1164 // failure message contains the argument values and the return value. 1165 TEST(ExcessiveCallTest, GeneratesFailureForNonVoidFunction) { 1166 MockB b; 1167 EXPECT_CALL(b, DoB(_)); 1168 b.DoB(1); 1169 EXPECT_NONFATAL_FAILURE( 1170 b.DoB(2), 1171 "Mock function called more times than expected - " 1172 "returning default value.\n" 1173 " Function call: DoB(2)\n" 1174 " Returns: 0\n" 1175 " Expected: to be called once\n" 1176 " Actual: called twice - over-saturated and active"); 1177 } 1178 1179 // Tests using sequences. 1180 1181 TEST(InSequenceTest, AllExpectationInScopeAreInSequence) { 1182 MockA a; 1183 { 1184 InSequence dummy; 1185 1186 EXPECT_CALL(a, DoA(1)); 1187 EXPECT_CALL(a, DoA(2)); 1188 } 1189 1190 EXPECT_NONFATAL_FAILURE({ // NOLINT 1191 a.DoA(2); 1192 }, "Unexpected mock function call"); 1193 1194 a.DoA(1); 1195 a.DoA(2); 1196 } 1197 1198 TEST(InSequenceTest, NestedInSequence) { 1199 MockA a; 1200 { 1201 InSequence dummy; 1202 1203 EXPECT_CALL(a, DoA(1)); 1204 { 1205 InSequence dummy2; 1206 1207 EXPECT_CALL(a, DoA(2)); 1208 EXPECT_CALL(a, DoA(3)); 1209 } 1210 } 1211 1212 EXPECT_NONFATAL_FAILURE({ // NOLINT 1213 a.DoA(1); 1214 a.DoA(3); 1215 }, "Unexpected mock function call"); 1216 1217 a.DoA(2); 1218 a.DoA(3); 1219 } 1220 1221 TEST(InSequenceTest, ExpectationsOutOfScopeAreNotAffected) { 1222 MockA a; 1223 { 1224 InSequence dummy; 1225 1226 EXPECT_CALL(a, DoA(1)); 1227 EXPECT_CALL(a, DoA(2)); 1228 } 1229 EXPECT_CALL(a, DoA(3)); 1230 1231 EXPECT_NONFATAL_FAILURE({ // NOLINT 1232 a.DoA(2); 1233 }, "Unexpected mock function call"); 1234 1235 a.DoA(3); 1236 a.DoA(1); 1237 a.DoA(2); 1238 } 1239 1240 // Tests that any order is allowed when no sequence is used. 1241 TEST(SequenceTest, AnyOrderIsOkByDefault) { 1242 { 1243 MockA a; 1244 MockB b; 1245 1246 EXPECT_CALL(a, DoA(1)); 1247 EXPECT_CALL(b, DoB()) 1248 .Times(AnyNumber()); 1249 1250 a.DoA(1); 1251 b.DoB(); 1252 } 1253 1254 { // NOLINT 1255 MockA a; 1256 MockB b; 1257 1258 EXPECT_CALL(a, DoA(1)); 1259 EXPECT_CALL(b, DoB()) 1260 .Times(AnyNumber()); 1261 1262 b.DoB(); 1263 a.DoA(1); 1264 } 1265 } 1266 1267 // Tests that the calls must be in strict order when a complete order 1268 // is specified. 1269 TEST(SequenceTest, CallsMustBeInStrictOrderWhenSaidSo1) { 1270 MockA a; 1271 ON_CALL(a, ReturnResult(_)) 1272 .WillByDefault(Return(Result())); 1273 1274 Sequence s; 1275 EXPECT_CALL(a, ReturnResult(1)) 1276 .InSequence(s); 1277 EXPECT_CALL(a, ReturnResult(2)) 1278 .InSequence(s); 1279 EXPECT_CALL(a, ReturnResult(3)) 1280 .InSequence(s); 1281 1282 a.ReturnResult(1); 1283 1284 // May only be called after a.ReturnResult(2). 1285 EXPECT_NONFATAL_FAILURE(a.ReturnResult(3), "Unexpected mock function call"); 1286 1287 a.ReturnResult(2); 1288 a.ReturnResult(3); 1289 } 1290 1291 // Tests that the calls must be in strict order when a complete order 1292 // is specified. 1293 TEST(SequenceTest, CallsMustBeInStrictOrderWhenSaidSo2) { 1294 MockA a; 1295 ON_CALL(a, ReturnResult(_)) 1296 .WillByDefault(Return(Result())); 1297 1298 Sequence s; 1299 EXPECT_CALL(a, ReturnResult(1)) 1300 .InSequence(s); 1301 EXPECT_CALL(a, ReturnResult(2)) 1302 .InSequence(s); 1303 1304 // May only be called after a.ReturnResult(1). 1305 EXPECT_NONFATAL_FAILURE(a.ReturnResult(2), "Unexpected mock function call"); 1306 1307 a.ReturnResult(1); 1308 a.ReturnResult(2); 1309 } 1310 1311 // Tests specifying a DAG using multiple sequences. 1312 class PartialOrderTest : public testing::Test { 1313 protected: 1314 PartialOrderTest() { 1315 ON_CALL(a_, ReturnResult(_)) 1316 .WillByDefault(Return(Result())); 1317 1318 // Specifies this partial ordering: 1319 // 1320 // a.ReturnResult(1) ==> 1321 // a.ReturnResult(2) * n ==> a.ReturnResult(3) 1322 // b.DoB() * 2 ==> 1323 Sequence x, y; 1324 EXPECT_CALL(a_, ReturnResult(1)) 1325 .InSequence(x); 1326 EXPECT_CALL(b_, DoB()) 1327 .Times(2) 1328 .InSequence(y); 1329 EXPECT_CALL(a_, ReturnResult(2)) 1330 .Times(AnyNumber()) 1331 .InSequence(x, y); 1332 EXPECT_CALL(a_, ReturnResult(3)) 1333 .InSequence(x); 1334 } 1335 1336 MockA a_; 1337 MockB b_; 1338 }; 1339 1340 TEST_F(PartialOrderTest, CallsMustConformToSpecifiedDag1) { 1341 a_.ReturnResult(1); 1342 b_.DoB(); 1343 1344 // May only be called after the second DoB(). 1345 EXPECT_NONFATAL_FAILURE(a_.ReturnResult(2), "Unexpected mock function call"); 1346 1347 b_.DoB(); 1348 a_.ReturnResult(3); 1349 } 1350 1351 TEST_F(PartialOrderTest, CallsMustConformToSpecifiedDag2) { 1352 // May only be called after ReturnResult(1). 1353 EXPECT_NONFATAL_FAILURE(a_.ReturnResult(2), "Unexpected mock function call"); 1354 1355 a_.ReturnResult(1); 1356 b_.DoB(); 1357 b_.DoB(); 1358 a_.ReturnResult(3); 1359 } 1360 1361 TEST_F(PartialOrderTest, CallsMustConformToSpecifiedDag3) { 1362 // May only be called last. 1363 EXPECT_NONFATAL_FAILURE(a_.ReturnResult(3), "Unexpected mock function call"); 1364 1365 a_.ReturnResult(1); 1366 b_.DoB(); 1367 b_.DoB(); 1368 a_.ReturnResult(3); 1369 } 1370 1371 TEST_F(PartialOrderTest, CallsMustConformToSpecifiedDag4) { 1372 a_.ReturnResult(1); 1373 b_.DoB(); 1374 b_.DoB(); 1375 a_.ReturnResult(3); 1376 1377 // May only be called before ReturnResult(3). 1378 EXPECT_NONFATAL_FAILURE(a_.ReturnResult(2), "Unexpected mock function call"); 1379 } 1380 1381 TEST(SequenceTest, Retirement) { 1382 MockA a; 1383 Sequence s; 1384 1385 EXPECT_CALL(a, DoA(1)) 1386 .InSequence(s); 1387 EXPECT_CALL(a, DoA(_)) 1388 .InSequence(s) 1389 .RetiresOnSaturation(); 1390 EXPECT_CALL(a, DoA(1)) 1391 .InSequence(s); 1392 1393 a.DoA(1); 1394 a.DoA(2); 1395 a.DoA(1); 1396 } 1397 1398 // Tests Expectation. 1399 1400 TEST(ExpectationTest, ConstrutorsWork) { 1401 MockA a; 1402 Expectation e1; // Default ctor. 1403 1404 // Ctor from various forms of EXPECT_CALL. 1405 Expectation e2 = EXPECT_CALL(a, DoA(2)); 1406 Expectation e3 = EXPECT_CALL(a, DoA(3)).With(_); 1407 { 1408 Sequence s; 1409 Expectation e4 = EXPECT_CALL(a, DoA(4)).Times(1); 1410 Expectation e5 = EXPECT_CALL(a, DoA(5)).InSequence(s); 1411 } 1412 Expectation e6 = EXPECT_CALL(a, DoA(6)).After(e2); 1413 Expectation e7 = EXPECT_CALL(a, DoA(7)).WillOnce(Return()); 1414 Expectation e8 = EXPECT_CALL(a, DoA(8)).WillRepeatedly(Return()); 1415 Expectation e9 = EXPECT_CALL(a, DoA(9)).RetiresOnSaturation(); 1416 1417 Expectation e10 = e2; // Copy ctor. 1418 1419 EXPECT_THAT(e1, Ne(e2)); 1420 EXPECT_THAT(e2, Eq(e10)); 1421 1422 a.DoA(2); 1423 a.DoA(3); 1424 a.DoA(4); 1425 a.DoA(5); 1426 a.DoA(6); 1427 a.DoA(7); 1428 a.DoA(8); 1429 a.DoA(9); 1430 } 1431 1432 TEST(ExpectationTest, AssignmentWorks) { 1433 MockA a; 1434 Expectation e1; 1435 Expectation e2 = EXPECT_CALL(a, DoA(1)); 1436 1437 EXPECT_THAT(e1, Ne(e2)); 1438 1439 e1 = e2; 1440 EXPECT_THAT(e1, Eq(e2)); 1441 1442 a.DoA(1); 1443 } 1444 1445 // Tests ExpectationSet. 1446 1447 TEST(ExpectationSetTest, MemberTypesAreCorrect) { 1448 ::testing::StaticAssertTypeEq<Expectation, ExpectationSet::value_type>(); 1449 } 1450 1451 TEST(ExpectationSetTest, ConstructorsWork) { 1452 MockA a; 1453 1454 Expectation e1; 1455 const Expectation e2; 1456 ExpectationSet es1; // Default ctor. 1457 ExpectationSet es2 = EXPECT_CALL(a, DoA(1)); // Ctor from EXPECT_CALL. 1458 ExpectationSet es3 = e1; // Ctor from Expectation. 1459 ExpectationSet es4(e1); // Ctor from Expectation; alternative syntax. 1460 ExpectationSet es5 = e2; // Ctor from const Expectation. 1461 ExpectationSet es6(e2); // Ctor from const Expectation; alternative syntax. 1462 ExpectationSet es7 = es2; // Copy ctor. 1463 1464 EXPECT_EQ(0, es1.size()); 1465 EXPECT_EQ(1, es2.size()); 1466 EXPECT_EQ(1, es3.size()); 1467 EXPECT_EQ(1, es4.size()); 1468 EXPECT_EQ(1, es5.size()); 1469 EXPECT_EQ(1, es6.size()); 1470 EXPECT_EQ(1, es7.size()); 1471 1472 EXPECT_THAT(es3, Ne(es2)); 1473 EXPECT_THAT(es4, Eq(es3)); 1474 EXPECT_THAT(es5, Eq(es4)); 1475 EXPECT_THAT(es6, Eq(es5)); 1476 EXPECT_THAT(es7, Eq(es2)); 1477 a.DoA(1); 1478 } 1479 1480 TEST(ExpectationSetTest, AssignmentWorks) { 1481 ExpectationSet es1; 1482 ExpectationSet es2 = Expectation(); 1483 1484 es1 = es2; 1485 EXPECT_EQ(1, es1.size()); 1486 EXPECT_THAT(*(es1.begin()), Eq(Expectation())); 1487 EXPECT_THAT(es1, Eq(es2)); 1488 } 1489 1490 TEST(ExpectationSetTest, InsertionWorks) { 1491 ExpectationSet es1; 1492 Expectation e1; 1493 es1 += e1; 1494 EXPECT_EQ(1, es1.size()); 1495 EXPECT_THAT(*(es1.begin()), Eq(e1)); 1496 1497 MockA a; 1498 Expectation e2 = EXPECT_CALL(a, DoA(1)); 1499 es1 += e2; 1500 EXPECT_EQ(2, es1.size()); 1501 1502 ExpectationSet::const_iterator it1 = es1.begin(); 1503 ExpectationSet::const_iterator it2 = it1; 1504 ++it2; 1505 EXPECT_TRUE(*it1 == e1 || *it2 == e1); // e1 must be in the set. 1506 EXPECT_TRUE(*it1 == e2 || *it2 == e2); // e2 must be in the set too. 1507 a.DoA(1); 1508 } 1509 1510 TEST(ExpectationSetTest, SizeWorks) { 1511 ExpectationSet es; 1512 EXPECT_EQ(0, es.size()); 1513 1514 es += Expectation(); 1515 EXPECT_EQ(1, es.size()); 1516 1517 MockA a; 1518 es += EXPECT_CALL(a, DoA(1)); 1519 EXPECT_EQ(2, es.size()); 1520 1521 a.DoA(1); 1522 } 1523 1524 TEST(ExpectationSetTest, IsEnumerable) { 1525 ExpectationSet es; 1526 EXPECT_TRUE(es.begin() == es.end()); 1527 1528 es += Expectation(); 1529 ExpectationSet::const_iterator it = es.begin(); 1530 EXPECT_TRUE(it != es.end()); 1531 EXPECT_THAT(*it, Eq(Expectation())); 1532 ++it; 1533 EXPECT_TRUE(it== es.end()); 1534 } 1535 1536 // Tests the .After() clause. 1537 1538 TEST(AfterTest, SucceedsWhenPartialOrderIsSatisfied) { 1539 MockA a; 1540 ExpectationSet es; 1541 es += EXPECT_CALL(a, DoA(1)); 1542 es += EXPECT_CALL(a, DoA(2)); 1543 EXPECT_CALL(a, DoA(3)) 1544 .After(es); 1545 1546 a.DoA(1); 1547 a.DoA(2); 1548 a.DoA(3); 1549 } 1550 1551 TEST(AfterTest, SucceedsWhenTotalOrderIsSatisfied) { 1552 MockA a; 1553 MockB b; 1554 // The following also verifies that const Expectation objects work 1555 // too. Do not remove the const modifiers. 1556 const Expectation e1 = EXPECT_CALL(a, DoA(1)); 1557 const Expectation e2 = EXPECT_CALL(b, DoB()) 1558 .Times(2) 1559 .After(e1); 1560 EXPECT_CALL(a, DoA(2)).After(e2); 1561 1562 a.DoA(1); 1563 b.DoB(); 1564 b.DoB(); 1565 a.DoA(2); 1566 } 1567 1568 // Calls must be in strict order when specified so using .After(). 1569 TEST(AfterTest, CallsMustBeInStrictOrderWhenSpecifiedSo1) { 1570 MockA a; 1571 MockB b; 1572 1573 // Define ordering: 1574 // a.DoA(1) ==> b.DoB() ==> a.DoA(2) 1575 Expectation e1 = EXPECT_CALL(a, DoA(1)); 1576 Expectation e2 = EXPECT_CALL(b, DoB()) 1577 .After(e1); 1578 EXPECT_CALL(a, DoA(2)) 1579 .After(e2); 1580 1581 a.DoA(1); 1582 1583 // May only be called after DoB(). 1584 EXPECT_NONFATAL_FAILURE(a.DoA(2), "Unexpected mock function call"); 1585 1586 b.DoB(); 1587 a.DoA(2); 1588 } 1589 1590 // Calls must be in strict order when specified so using .After(). 1591 TEST(AfterTest, CallsMustBeInStrictOrderWhenSpecifiedSo2) { 1592 MockA a; 1593 MockB b; 1594 1595 // Define ordering: 1596 // a.DoA(1) ==> b.DoB() * 2 ==> a.DoA(2) 1597 Expectation e1 = EXPECT_CALL(a, DoA(1)); 1598 Expectation e2 = EXPECT_CALL(b, DoB()) 1599 .Times(2) 1600 .After(e1); 1601 EXPECT_CALL(a, DoA(2)) 1602 .After(e2); 1603 1604 a.DoA(1); 1605 b.DoB(); 1606 1607 // May only be called after the second DoB(). 1608 EXPECT_NONFATAL_FAILURE(a.DoA(2), "Unexpected mock function call"); 1609 1610 b.DoB(); 1611 a.DoA(2); 1612 } 1613 1614 // Calls must satisfy the partial order when specified so. 1615 TEST(AfterTest, CallsMustSatisfyPartialOrderWhenSpecifiedSo) { 1616 MockA a; 1617 ON_CALL(a, ReturnResult(_)) 1618 .WillByDefault(Return(Result())); 1619 1620 // Define ordering: 1621 // a.DoA(1) ==> 1622 // a.DoA(2) ==> a.ReturnResult(3) 1623 Expectation e = EXPECT_CALL(a, DoA(1)); 1624 const ExpectationSet es = EXPECT_CALL(a, DoA(2)); 1625 EXPECT_CALL(a, ReturnResult(3)) 1626 .After(e, es); 1627 1628 // May only be called last. 1629 EXPECT_NONFATAL_FAILURE(a.ReturnResult(3), "Unexpected mock function call"); 1630 1631 a.DoA(2); 1632 a.DoA(1); 1633 a.ReturnResult(3); 1634 } 1635 1636 // Calls must satisfy the partial order when specified so. 1637 TEST(AfterTest, CallsMustSatisfyPartialOrderWhenSpecifiedSo2) { 1638 MockA a; 1639 1640 // Define ordering: 1641 // a.DoA(1) ==> 1642 // a.DoA(2) ==> a.DoA(3) 1643 Expectation e = EXPECT_CALL(a, DoA(1)); 1644 const ExpectationSet es = EXPECT_CALL(a, DoA(2)); 1645 EXPECT_CALL(a, DoA(3)) 1646 .After(e, es); 1647 1648 a.DoA(2); 1649 1650 // May only be called last. 1651 EXPECT_NONFATAL_FAILURE(a.DoA(3), "Unexpected mock function call"); 1652 1653 a.DoA(1); 1654 a.DoA(3); 1655 } 1656 1657 // .After() can be combined with .InSequence(). 1658 TEST(AfterTest, CanBeUsedWithInSequence) { 1659 MockA a; 1660 Sequence s; 1661 Expectation e = EXPECT_CALL(a, DoA(1)); 1662 EXPECT_CALL(a, DoA(2)).InSequence(s); 1663 EXPECT_CALL(a, DoA(3)) 1664 .InSequence(s) 1665 .After(e); 1666 1667 a.DoA(1); 1668 1669 // May only be after DoA(2). 1670 EXPECT_NONFATAL_FAILURE(a.DoA(3), "Unexpected mock function call"); 1671 1672 a.DoA(2); 1673 a.DoA(3); 1674 } 1675 1676 // .After() can be called multiple times. 1677 TEST(AfterTest, CanBeCalledManyTimes) { 1678 MockA a; 1679 Expectation e1 = EXPECT_CALL(a, DoA(1)); 1680 Expectation e2 = EXPECT_CALL(a, DoA(2)); 1681 Expectation e3 = EXPECT_CALL(a, DoA(3)); 1682 EXPECT_CALL(a, DoA(4)) 1683 .After(e1) 1684 .After(e2) 1685 .After(e3); 1686 1687 a.DoA(3); 1688 a.DoA(1); 1689 a.DoA(2); 1690 a.DoA(4); 1691 } 1692 1693 // .After() accepts up to 5 arguments. 1694 TEST(AfterTest, AcceptsUpToFiveArguments) { 1695 MockA a; 1696 Expectation e1 = EXPECT_CALL(a, DoA(1)); 1697 Expectation e2 = EXPECT_CALL(a, DoA(2)); 1698 Expectation e3 = EXPECT_CALL(a, DoA(3)); 1699 ExpectationSet es1 = EXPECT_CALL(a, DoA(4)); 1700 ExpectationSet es2 = EXPECT_CALL(a, DoA(5)); 1701 EXPECT_CALL(a, DoA(6)) 1702 .After(e1, e2, e3, es1, es2); 1703 1704 a.DoA(5); 1705 a.DoA(2); 1706 a.DoA(4); 1707 a.DoA(1); 1708 a.DoA(3); 1709 a.DoA(6); 1710 } 1711 1712 // .After() allows input to contain duplicated Expectations. 1713 TEST(AfterTest, AcceptsDuplicatedInput) { 1714 MockA a; 1715 ON_CALL(a, ReturnResult(_)) 1716 .WillByDefault(Return(Result())); 1717 1718 // Define ordering: 1719 // DoA(1) ==> 1720 // DoA(2) ==> ReturnResult(3) 1721 Expectation e1 = EXPECT_CALL(a, DoA(1)); 1722 Expectation e2 = EXPECT_CALL(a, DoA(2)); 1723 ExpectationSet es; 1724 es += e1; 1725 es += e2; 1726 EXPECT_CALL(a, ReturnResult(3)) 1727 .After(e1, e2, es, e1); 1728 1729 a.DoA(1); 1730 1731 // May only be after DoA(2). 1732 EXPECT_NONFATAL_FAILURE(a.ReturnResult(3), "Unexpected mock function call"); 1733 1734 a.DoA(2); 1735 a.ReturnResult(3); 1736 } 1737 1738 // An Expectation added to an ExpectationSet after it has been used in 1739 // an .After() has no effect. 1740 TEST(AfterTest, ChangesToExpectationSetHaveNoEffectAfterwards) { 1741 MockA a; 1742 ExpectationSet es1 = EXPECT_CALL(a, DoA(1)); 1743 Expectation e2 = EXPECT_CALL(a, DoA(2)); 1744 EXPECT_CALL(a, DoA(3)) 1745 .After(es1); 1746 es1 += e2; 1747 1748 a.DoA(1); 1749 a.DoA(3); 1750 a.DoA(2); 1751 } 1752 1753 // Tests that Google Mock correctly handles calls to mock functions 1754 // after a mock object owning one of their pre-requisites has died. 1755 1756 // Tests that calls that satisfy the original spec are successful. 1757 TEST(DeletingMockEarlyTest, Success1) { 1758 MockB* const b1 = new MockB; 1759 MockA* const a = new MockA; 1760 MockB* const b2 = new MockB; 1761 1762 { 1763 InSequence dummy; 1764 EXPECT_CALL(*b1, DoB(_)) 1765 .WillOnce(Return(1)); 1766 EXPECT_CALL(*a, Binary(_, _)) 1767 .Times(AnyNumber()) 1768 .WillRepeatedly(Return(true)); 1769 EXPECT_CALL(*b2, DoB(_)) 1770 .Times(AnyNumber()) 1771 .WillRepeatedly(Return(2)); 1772 } 1773 1774 EXPECT_EQ(1, b1->DoB(1)); 1775 delete b1; 1776 // a's pre-requisite has died. 1777 EXPECT_TRUE(a->Binary(0, 1)); 1778 delete b2; 1779 // a's successor has died. 1780 EXPECT_TRUE(a->Binary(1, 2)); 1781 delete a; 1782 } 1783 1784 // Tests that calls that satisfy the original spec are successful. 1785 TEST(DeletingMockEarlyTest, Success2) { 1786 MockB* const b1 = new MockB; 1787 MockA* const a = new MockA; 1788 MockB* const b2 = new MockB; 1789 1790 { 1791 InSequence dummy; 1792 EXPECT_CALL(*b1, DoB(_)) 1793 .WillOnce(Return(1)); 1794 EXPECT_CALL(*a, Binary(_, _)) 1795 .Times(AnyNumber()); 1796 EXPECT_CALL(*b2, DoB(_)) 1797 .Times(AnyNumber()) 1798 .WillRepeatedly(Return(2)); 1799 } 1800 1801 delete a; // a is trivially satisfied. 1802 EXPECT_EQ(1, b1->DoB(1)); 1803 EXPECT_EQ(2, b2->DoB(2)); 1804 delete b1; 1805 delete b2; 1806 } 1807 1808 // Tests that it's OK to delete a mock object itself in its action. 1809 1810 // Suppresses warning on unreferenced formal parameter in MSVC with 1811 // -W4. 1812 #ifdef _MSC_VER 1813 # pragma warning(push) 1814 # pragma warning(disable:4100) 1815 #endif 1816 1817 ACTION_P(Delete, ptr) { delete ptr; } 1818 1819 #ifdef _MSC_VER 1820 # pragma warning(pop) 1821 #endif 1822 1823 TEST(DeletingMockEarlyTest, CanDeleteSelfInActionReturningVoid) { 1824 MockA* const a = new MockA; 1825 EXPECT_CALL(*a, DoA(_)).WillOnce(Delete(a)); 1826 a->DoA(42); // This will cause a to be deleted. 1827 } 1828 1829 TEST(DeletingMockEarlyTest, CanDeleteSelfInActionReturningValue) { 1830 MockA* const a = new MockA; 1831 EXPECT_CALL(*a, ReturnResult(_)) 1832 .WillOnce(DoAll(Delete(a), Return(Result()))); 1833 a->ReturnResult(42); // This will cause a to be deleted. 1834 } 1835 1836 // Tests that calls that violate the original spec yield failures. 1837 TEST(DeletingMockEarlyTest, Failure1) { 1838 MockB* const b1 = new MockB; 1839 MockA* const a = new MockA; 1840 MockB* const b2 = new MockB; 1841 1842 { 1843 InSequence dummy; 1844 EXPECT_CALL(*b1, DoB(_)) 1845 .WillOnce(Return(1)); 1846 EXPECT_CALL(*a, Binary(_, _)) 1847 .Times(AnyNumber()); 1848 EXPECT_CALL(*b2, DoB(_)) 1849 .Times(AnyNumber()) 1850 .WillRepeatedly(Return(2)); 1851 } 1852 1853 delete a; // a is trivially satisfied. 1854 EXPECT_NONFATAL_FAILURE({ 1855 b2->DoB(2); 1856 }, "Unexpected mock function call"); 1857 EXPECT_EQ(1, b1->DoB(1)); 1858 delete b1; 1859 delete b2; 1860 } 1861 1862 // Tests that calls that violate the original spec yield failures. 1863 TEST(DeletingMockEarlyTest, Failure2) { 1864 MockB* const b1 = new MockB; 1865 MockA* const a = new MockA; 1866 MockB* const b2 = new MockB; 1867 1868 { 1869 InSequence dummy; 1870 EXPECT_CALL(*b1, DoB(_)); 1871 EXPECT_CALL(*a, Binary(_, _)) 1872 .Times(AnyNumber()); 1873 EXPECT_CALL(*b2, DoB(_)) 1874 .Times(AnyNumber()); 1875 } 1876 1877 EXPECT_NONFATAL_FAILURE(delete b1, 1878 "Actual: never called"); 1879 EXPECT_NONFATAL_FAILURE(a->Binary(0, 1), 1880 "Unexpected mock function call"); 1881 EXPECT_NONFATAL_FAILURE(b2->DoB(1), 1882 "Unexpected mock function call"); 1883 delete a; 1884 delete b2; 1885 } 1886 1887 class EvenNumberCardinality : public CardinalityInterface { 1888 public: 1889 // Returns true iff call_count calls will satisfy this cardinality. 1890 virtual bool IsSatisfiedByCallCount(int call_count) const { 1891 return call_count % 2 == 0; 1892 } 1893 1894 // Returns true iff call_count calls will saturate this cardinality. 1895 virtual bool IsSaturatedByCallCount(int /* call_count */) const { 1896 return false; 1897 } 1898 1899 // Describes self to an ostream. 1900 virtual void DescribeTo(::std::ostream* os) const { 1901 *os << "called even number of times"; 1902 } 1903 }; 1904 1905 Cardinality EvenNumber() { 1906 return Cardinality(new EvenNumberCardinality); 1907 } 1908 1909 TEST(ExpectationBaseTest, 1910 AllPrerequisitesAreSatisfiedWorksForNonMonotonicCardinality) { 1911 MockA* a = new MockA; 1912 Sequence s; 1913 1914 EXPECT_CALL(*a, DoA(1)) 1915 .Times(EvenNumber()) 1916 .InSequence(s); 1917 EXPECT_CALL(*a, DoA(2)) 1918 .Times(AnyNumber()) 1919 .InSequence(s); 1920 EXPECT_CALL(*a, DoA(3)) 1921 .Times(AnyNumber()); 1922 1923 a->DoA(3); 1924 a->DoA(1); 1925 EXPECT_NONFATAL_FAILURE(a->DoA(2), "Unexpected mock function call"); 1926 EXPECT_NONFATAL_FAILURE(delete a, "to be called even number of times"); 1927 } 1928 1929 // The following tests verify the message generated when a mock 1930 // function is called. 1931 1932 struct Printable { 1933 }; 1934 1935 inline void operator<<(::std::ostream& os, const Printable&) { 1936 os << "Printable"; 1937 } 1938 1939 struct Unprintable { 1940 Unprintable() : value(0) {} 1941 int value; 1942 }; 1943 1944 class MockC { 1945 public: 1946 MockC() {} 1947 1948 MOCK_METHOD6(VoidMethod, void(bool cond, int n, string s, void* p, 1949 const Printable& x, Unprintable y)); 1950 MOCK_METHOD0(NonVoidMethod, int()); // NOLINT 1951 1952 private: 1953 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockC); 1954 }; 1955 1956 class VerboseFlagPreservingFixture : public testing::Test { 1957 protected: 1958 VerboseFlagPreservingFixture() 1959 : saved_verbose_flag_(GMOCK_FLAG(verbose)) {} 1960 1961 ~VerboseFlagPreservingFixture() { GMOCK_FLAG(verbose) = saved_verbose_flag_; } 1962 1963 private: 1964 const string saved_verbose_flag_; 1965 1966 GTEST_DISALLOW_COPY_AND_ASSIGN_(VerboseFlagPreservingFixture); 1967 }; 1968 1969 #if GTEST_HAS_STREAM_REDIRECTION 1970 1971 // Tests that an uninteresting mock function call on a naggy mock 1972 // generates a warning containing the stack trace. 1973 TEST(FunctionCallMessageTest, 1974 UninterestingCallOnNaggyMockGeneratesFyiWithStackTrace) { 1975 NaggyMock<MockC> c; 1976 CaptureStdout(); 1977 c.VoidMethod(false, 5, "Hi", NULL, Printable(), Unprintable()); 1978 const std::string output = GetCapturedStdout(); 1979 EXPECT_PRED_FORMAT2(IsSubstring, "GMOCK WARNING", output); 1980 EXPECT_PRED_FORMAT2(IsSubstring, "Stack trace:", output); 1981 1982 # ifndef NDEBUG 1983 1984 // We check the stack trace content in dbg-mode only, as opt-mode 1985 // may inline the call we are interested in seeing. 1986 1987 // Verifies that a void mock function's name appears in the stack 1988 // trace. 1989 EXPECT_PRED_FORMAT2(IsSubstring, "VoidMethod(", output); 1990 1991 // Verifies that a non-void mock function's name appears in the 1992 // stack trace. 1993 CaptureStdout(); 1994 c.NonVoidMethod(); 1995 const std::string output2 = GetCapturedStdout(); 1996 EXPECT_PRED_FORMAT2(IsSubstring, "NonVoidMethod(", output2); 1997 1998 # endif // NDEBUG 1999 } 2000 2001 // Tests that an uninteresting mock function call on a naggy mock 2002 // causes the function arguments and return value to be printed. 2003 TEST(FunctionCallMessageTest, 2004 UninterestingCallOnNaggyMockPrintsArgumentsAndReturnValue) { 2005 // A non-void mock function. 2006 NaggyMock<MockB> b; 2007 CaptureStdout(); 2008 b.DoB(); 2009 const std::string output1 = GetCapturedStdout(); 2010 EXPECT_PRED_FORMAT2( 2011 IsSubstring, 2012 "Uninteresting mock function call - returning default value.\n" 2013 " Function call: DoB()\n" 2014 " Returns: 0\n", output1.c_str()); 2015 // Makes sure the return value is printed. 2016 2017 // A void mock function. 2018 NaggyMock<MockC> c; 2019 CaptureStdout(); 2020 c.VoidMethod(false, 5, "Hi", NULL, Printable(), Unprintable()); 2021 const std::string output2 = GetCapturedStdout(); 2022 EXPECT_THAT(output2.c_str(), 2023 ContainsRegex( 2024 "Uninteresting mock function call - returning directly\\.\n" 2025 " Function call: VoidMethod" 2026 "\\(false, 5, \"Hi\", NULL, @.+ " 2027 "Printable, 4-byte object <00-00 00-00>\\)")); 2028 // A void function has no return value to print. 2029 } 2030 2031 // Tests how the --gmock_verbose flag affects Google Mock's output. 2032 2033 class GMockVerboseFlagTest : public VerboseFlagPreservingFixture { 2034 public: 2035 // Verifies that the given Google Mock output is correct. (When 2036 // should_print is true, the output should match the given regex and 2037 // contain the given function name in the stack trace. When it's 2038 // false, the output should be empty.) 2039 void VerifyOutput(const std::string& output, bool should_print, 2040 const string& expected_substring, 2041 const string& function_name) { 2042 if (should_print) { 2043 EXPECT_THAT(output.c_str(), HasSubstr(expected_substring)); 2044 # ifndef NDEBUG 2045 // We check the stack trace content in dbg-mode only, as opt-mode 2046 // may inline the call we are interested in seeing. 2047 EXPECT_THAT(output.c_str(), HasSubstr(function_name)); 2048 # else 2049 // Suppresses 'unused function parameter' warnings. 2050 static_cast<void>(function_name); 2051 # endif // NDEBUG 2052 } else { 2053 EXPECT_STREQ("", output.c_str()); 2054 } 2055 } 2056 2057 // Tests how the flag affects expected calls. 2058 void TestExpectedCall(bool should_print) { 2059 MockA a; 2060 EXPECT_CALL(a, DoA(5)); 2061 EXPECT_CALL(a, Binary(_, 1)) 2062 .WillOnce(Return(true)); 2063 2064 // A void-returning function. 2065 CaptureStdout(); 2066 a.DoA(5); 2067 VerifyOutput( 2068 GetCapturedStdout(), 2069 should_print, 2070 "Mock function call matches EXPECT_CALL(a, DoA(5))...\n" 2071 " Function call: DoA(5)\n" 2072 "Stack trace:\n", 2073 "DoA"); 2074 2075 // A non-void-returning function. 2076 CaptureStdout(); 2077 a.Binary(2, 1); 2078 VerifyOutput( 2079 GetCapturedStdout(), 2080 should_print, 2081 "Mock function call matches EXPECT_CALL(a, Binary(_, 1))...\n" 2082 " Function call: Binary(2, 1)\n" 2083 " Returns: true\n" 2084 "Stack trace:\n", 2085 "Binary"); 2086 } 2087 2088 // Tests how the flag affects uninteresting calls on a naggy mock. 2089 void TestUninterestingCallOnNaggyMock(bool should_print) { 2090 NaggyMock<MockA> a; 2091 2092 // A void-returning function. 2093 CaptureStdout(); 2094 a.DoA(5); 2095 VerifyOutput( 2096 GetCapturedStdout(), 2097 should_print, 2098 "\nGMOCK WARNING:\n" 2099 "Uninteresting mock function call - returning directly.\n" 2100 " Function call: DoA(5)\n" 2101 "Stack trace:\n", 2102 "DoA"); 2103 2104 // A non-void-returning function. 2105 CaptureStdout(); 2106 a.Binary(2, 1); 2107 VerifyOutput( 2108 GetCapturedStdout(), 2109 should_print, 2110 "\nGMOCK WARNING:\n" 2111 "Uninteresting mock function call - returning default value.\n" 2112 " Function call: Binary(2, 1)\n" 2113 " Returns: false\n" 2114 "Stack trace:\n", 2115 "Binary"); 2116 } 2117 }; 2118 2119 // Tests that --gmock_verbose=info causes both expected and 2120 // uninteresting calls to be reported. 2121 TEST_F(GMockVerboseFlagTest, Info) { 2122 GMOCK_FLAG(verbose) = kInfoVerbosity; 2123 TestExpectedCall(true); 2124 TestUninterestingCallOnNaggyMock(true); 2125 } 2126 2127 // Tests that --gmock_verbose=warning causes uninteresting calls to be 2128 // reported. 2129 TEST_F(GMockVerboseFlagTest, Warning) { 2130 GMOCK_FLAG(verbose) = kWarningVerbosity; 2131 TestExpectedCall(false); 2132 TestUninterestingCallOnNaggyMock(true); 2133 } 2134 2135 // Tests that --gmock_verbose=warning causes neither expected nor 2136 // uninteresting calls to be reported. 2137 TEST_F(GMockVerboseFlagTest, Error) { 2138 GMOCK_FLAG(verbose) = kErrorVerbosity; 2139 TestExpectedCall(false); 2140 TestUninterestingCallOnNaggyMock(false); 2141 } 2142 2143 // Tests that --gmock_verbose=SOME_INVALID_VALUE has the same effect 2144 // as --gmock_verbose=warning. 2145 TEST_F(GMockVerboseFlagTest, InvalidFlagIsTreatedAsWarning) { 2146 GMOCK_FLAG(verbose) = "invalid"; // Treated as "warning". 2147 TestExpectedCall(false); 2148 TestUninterestingCallOnNaggyMock(true); 2149 } 2150 2151 #endif // GTEST_HAS_STREAM_REDIRECTION 2152 2153 // A helper class that generates a failure when printed. We use it to 2154 // ensure that Google Mock doesn't print a value (even to an internal 2155 // buffer) when it is not supposed to do so. 2156 class PrintMeNot {}; 2157 2158 void PrintTo(PrintMeNot /* dummy */, ::std::ostream* /* os */) { 2159 ADD_FAILURE() << "Google Mock is printing a value that shouldn't be " 2160 << "printed even to an internal buffer."; 2161 } 2162 2163 class LogTestHelper { 2164 public: 2165 LogTestHelper() {} 2166 2167 MOCK_METHOD1(Foo, PrintMeNot(PrintMeNot)); 2168 2169 private: 2170 GTEST_DISALLOW_COPY_AND_ASSIGN_(LogTestHelper); 2171 }; 2172 2173 class GMockLogTest : public VerboseFlagPreservingFixture { 2174 protected: 2175 LogTestHelper helper_; 2176 }; 2177 2178 TEST_F(GMockLogTest, DoesNotPrintGoodCallInternallyIfVerbosityIsWarning) { 2179 GMOCK_FLAG(verbose) = kWarningVerbosity; 2180 EXPECT_CALL(helper_, Foo(_)) 2181 .WillOnce(Return(PrintMeNot())); 2182 helper_.Foo(PrintMeNot()); // This is an expected call. 2183 } 2184 2185 TEST_F(GMockLogTest, DoesNotPrintGoodCallInternallyIfVerbosityIsError) { 2186 GMOCK_FLAG(verbose) = kErrorVerbosity; 2187 EXPECT_CALL(helper_, Foo(_)) 2188 .WillOnce(Return(PrintMeNot())); 2189 helper_.Foo(PrintMeNot()); // This is an expected call. 2190 } 2191 2192 TEST_F(GMockLogTest, DoesNotPrintWarningInternallyIfVerbosityIsError) { 2193 GMOCK_FLAG(verbose) = kErrorVerbosity; 2194 ON_CALL(helper_, Foo(_)) 2195 .WillByDefault(Return(PrintMeNot())); 2196 helper_.Foo(PrintMeNot()); // This should generate a warning. 2197 } 2198 2199 // Tests Mock::AllowLeak(). 2200 2201 TEST(AllowLeakTest, AllowsLeakingUnusedMockObject) { 2202 MockA* a = new MockA; 2203 Mock::AllowLeak(a); 2204 } 2205 2206 TEST(AllowLeakTest, CanBeCalledBeforeOnCall) { 2207 MockA* a = new MockA; 2208 Mock::AllowLeak(a); 2209 ON_CALL(*a, DoA(_)).WillByDefault(Return()); 2210 a->DoA(0); 2211 } 2212 2213 TEST(AllowLeakTest, CanBeCalledAfterOnCall) { 2214 MockA* a = new MockA; 2215 ON_CALL(*a, DoA(_)).WillByDefault(Return()); 2216 Mock::AllowLeak(a); 2217 } 2218 2219 TEST(AllowLeakTest, CanBeCalledBeforeExpectCall) { 2220 MockA* a = new MockA; 2221 Mock::AllowLeak(a); 2222 EXPECT_CALL(*a, DoA(_)); 2223 a->DoA(0); 2224 } 2225 2226 TEST(AllowLeakTest, CanBeCalledAfterExpectCall) { 2227 MockA* a = new MockA; 2228 EXPECT_CALL(*a, DoA(_)).Times(AnyNumber()); 2229 Mock::AllowLeak(a); 2230 } 2231 2232 TEST(AllowLeakTest, WorksWhenBothOnCallAndExpectCallArePresent) { 2233 MockA* a = new MockA; 2234 ON_CALL(*a, DoA(_)).WillByDefault(Return()); 2235 EXPECT_CALL(*a, DoA(_)).Times(AnyNumber()); 2236 Mock::AllowLeak(a); 2237 } 2238 2239 // Tests that we can verify and clear a mock object's expectations 2240 // when none of its methods has expectations. 2241 TEST(VerifyAndClearExpectationsTest, NoMethodHasExpectations) { 2242 MockB b; 2243 ASSERT_TRUE(Mock::VerifyAndClearExpectations(&b)); 2244 2245 // There should be no expectations on the methods now, so we can 2246 // freely call them. 2247 EXPECT_EQ(0, b.DoB()); 2248 EXPECT_EQ(0, b.DoB(1)); 2249 } 2250 2251 // Tests that we can verify and clear a mock object's expectations 2252 // when some, but not all, of its methods have expectations *and* the 2253 // verification succeeds. 2254 TEST(VerifyAndClearExpectationsTest, SomeMethodsHaveExpectationsAndSucceed) { 2255 MockB b; 2256 EXPECT_CALL(b, DoB()) 2257 .WillOnce(Return(1)); 2258 b.DoB(); 2259 ASSERT_TRUE(Mock::VerifyAndClearExpectations(&b)); 2260 2261 // There should be no expectations on the methods now, so we can 2262 // freely call them. 2263 EXPECT_EQ(0, b.DoB()); 2264 EXPECT_EQ(0, b.DoB(1)); 2265 } 2266 2267 // Tests that we can verify and clear a mock object's expectations 2268 // when some, but not all, of its methods have expectations *and* the 2269 // verification fails. 2270 TEST(VerifyAndClearExpectationsTest, SomeMethodsHaveExpectationsAndFail) { 2271 MockB b; 2272 EXPECT_CALL(b, DoB()) 2273 .WillOnce(Return(1)); 2274 bool result = true; 2275 EXPECT_NONFATAL_FAILURE(result = Mock::VerifyAndClearExpectations(&b), 2276 "Actual: never called"); 2277 ASSERT_FALSE(result); 2278 2279 // There should be no expectations on the methods now, so we can 2280 // freely call them. 2281 EXPECT_EQ(0, b.DoB()); 2282 EXPECT_EQ(0, b.DoB(1)); 2283 } 2284 2285 // Tests that we can verify and clear a mock object's expectations 2286 // when all of its methods have expectations. 2287 TEST(VerifyAndClearExpectationsTest, AllMethodsHaveExpectations) { 2288 MockB b; 2289 EXPECT_CALL(b, DoB()) 2290 .WillOnce(Return(1)); 2291 EXPECT_CALL(b, DoB(_)) 2292 .WillOnce(Return(2)); 2293 b.DoB(); 2294 b.DoB(1); 2295 ASSERT_TRUE(Mock::VerifyAndClearExpectations(&b)); 2296 2297 // There should be no expectations on the methods now, so we can 2298 // freely call them. 2299 EXPECT_EQ(0, b.DoB()); 2300 EXPECT_EQ(0, b.DoB(1)); 2301 } 2302 2303 // Tests that we can verify and clear a mock object's expectations 2304 // when a method has more than one expectation. 2305 TEST(VerifyAndClearExpectationsTest, AMethodHasManyExpectations) { 2306 MockB b; 2307 EXPECT_CALL(b, DoB(0)) 2308 .WillOnce(Return(1)); 2309 EXPECT_CALL(b, DoB(_)) 2310 .WillOnce(Return(2)); 2311 b.DoB(1); 2312 bool result = true; 2313 EXPECT_NONFATAL_FAILURE(result = Mock::VerifyAndClearExpectations(&b), 2314 "Actual: never called"); 2315 ASSERT_FALSE(result); 2316 2317 // There should be no expectations on the methods now, so we can 2318 // freely call them. 2319 EXPECT_EQ(0, b.DoB()); 2320 EXPECT_EQ(0, b.DoB(1)); 2321 } 2322 2323 // Tests that we can call VerifyAndClearExpectations() on the same 2324 // mock object multiple times. 2325 TEST(VerifyAndClearExpectationsTest, CanCallManyTimes) { 2326 MockB b; 2327 EXPECT_CALL(b, DoB()); 2328 b.DoB(); 2329 Mock::VerifyAndClearExpectations(&b); 2330 2331 EXPECT_CALL(b, DoB(_)) 2332 .WillOnce(Return(1)); 2333 b.DoB(1); 2334 Mock::VerifyAndClearExpectations(&b); 2335 Mock::VerifyAndClearExpectations(&b); 2336 2337 // There should be no expectations on the methods now, so we can 2338 // freely call them. 2339 EXPECT_EQ(0, b.DoB()); 2340 EXPECT_EQ(0, b.DoB(1)); 2341 } 2342 2343 // Tests that we can clear a mock object's default actions when none 2344 // of its methods has default actions. 2345 TEST(VerifyAndClearTest, NoMethodHasDefaultActions) { 2346 MockB b; 2347 // If this crashes or generates a failure, the test will catch it. 2348 Mock::VerifyAndClear(&b); 2349 EXPECT_EQ(0, b.DoB()); 2350 } 2351 2352 // Tests that we can clear a mock object's default actions when some, 2353 // but not all of its methods have default actions. 2354 TEST(VerifyAndClearTest, SomeMethodsHaveDefaultActions) { 2355 MockB b; 2356 ON_CALL(b, DoB()) 2357 .WillByDefault(Return(1)); 2358 2359 Mock::VerifyAndClear(&b); 2360 2361 // Verifies that the default action of int DoB() was removed. 2362 EXPECT_EQ(0, b.DoB()); 2363 } 2364 2365 // Tests that we can clear a mock object's default actions when all of 2366 // its methods have default actions. 2367 TEST(VerifyAndClearTest, AllMethodsHaveDefaultActions) { 2368 MockB b; 2369 ON_CALL(b, DoB()) 2370 .WillByDefault(Return(1)); 2371 ON_CALL(b, DoB(_)) 2372 .WillByDefault(Return(2)); 2373 2374 Mock::VerifyAndClear(&b); 2375 2376 // Verifies that the default action of int DoB() was removed. 2377 EXPECT_EQ(0, b.DoB()); 2378 2379 // Verifies that the default action of int DoB(int) was removed. 2380 EXPECT_EQ(0, b.DoB(0)); 2381 } 2382 2383 // Tests that we can clear a mock object's default actions when a 2384 // method has more than one ON_CALL() set on it. 2385 TEST(VerifyAndClearTest, AMethodHasManyDefaultActions) { 2386 MockB b; 2387 ON_CALL(b, DoB(0)) 2388 .WillByDefault(Return(1)); 2389 ON_CALL(b, DoB(_)) 2390 .WillByDefault(Return(2)); 2391 2392 Mock::VerifyAndClear(&b); 2393 2394 // Verifies that the default actions (there are two) of int DoB(int) 2395 // were removed. 2396 EXPECT_EQ(0, b.DoB(0)); 2397 EXPECT_EQ(0, b.DoB(1)); 2398 } 2399 2400 // Tests that we can call VerifyAndClear() on a mock object multiple 2401 // times. 2402 TEST(VerifyAndClearTest, CanCallManyTimes) { 2403 MockB b; 2404 ON_CALL(b, DoB()) 2405 .WillByDefault(Return(1)); 2406 Mock::VerifyAndClear(&b); 2407 Mock::VerifyAndClear(&b); 2408 2409 ON_CALL(b, DoB(_)) 2410 .WillByDefault(Return(1)); 2411 Mock::VerifyAndClear(&b); 2412 2413 EXPECT_EQ(0, b.DoB()); 2414 EXPECT_EQ(0, b.DoB(1)); 2415 } 2416 2417 // Tests that VerifyAndClear() works when the verification succeeds. 2418 TEST(VerifyAndClearTest, Success) { 2419 MockB b; 2420 ON_CALL(b, DoB()) 2421 .WillByDefault(Return(1)); 2422 EXPECT_CALL(b, DoB(1)) 2423 .WillOnce(Return(2)); 2424 2425 b.DoB(); 2426 b.DoB(1); 2427 ASSERT_TRUE(Mock::VerifyAndClear(&b)); 2428 2429 // There should be no expectations on the methods now, so we can 2430 // freely call them. 2431 EXPECT_EQ(0, b.DoB()); 2432 EXPECT_EQ(0, b.DoB(1)); 2433 } 2434 2435 // Tests that VerifyAndClear() works when the verification fails. 2436 TEST(VerifyAndClearTest, Failure) { 2437 MockB b; 2438 ON_CALL(b, DoB(_)) 2439 .WillByDefault(Return(1)); 2440 EXPECT_CALL(b, DoB()) 2441 .WillOnce(Return(2)); 2442 2443 b.DoB(1); 2444 bool result = true; 2445 EXPECT_NONFATAL_FAILURE(result = Mock::VerifyAndClear(&b), 2446 "Actual: never called"); 2447 ASSERT_FALSE(result); 2448 2449 // There should be no expectations on the methods now, so we can 2450 // freely call them. 2451 EXPECT_EQ(0, b.DoB()); 2452 EXPECT_EQ(0, b.DoB(1)); 2453 } 2454 2455 // Tests that VerifyAndClear() works when the default actions and 2456 // expectations are set on a const mock object. 2457 TEST(VerifyAndClearTest, Const) { 2458 MockB b; 2459 ON_CALL(Const(b), DoB()) 2460 .WillByDefault(Return(1)); 2461 2462 EXPECT_CALL(Const(b), DoB()) 2463 .WillOnce(DoDefault()) 2464 .WillOnce(Return(2)); 2465 2466 b.DoB(); 2467 b.DoB(); 2468 ASSERT_TRUE(Mock::VerifyAndClear(&b)); 2469 2470 // There should be no expectations on the methods now, so we can 2471 // freely call them. 2472 EXPECT_EQ(0, b.DoB()); 2473 EXPECT_EQ(0, b.DoB(1)); 2474 } 2475 2476 // Tests that we can set default actions and expectations on a mock 2477 // object after VerifyAndClear() has been called on it. 2478 TEST(VerifyAndClearTest, CanSetDefaultActionsAndExpectationsAfterwards) { 2479 MockB b; 2480 ON_CALL(b, DoB()) 2481 .WillByDefault(Return(1)); 2482 EXPECT_CALL(b, DoB(_)) 2483 .WillOnce(Return(2)); 2484 b.DoB(1); 2485 2486 Mock::VerifyAndClear(&b); 2487 2488 EXPECT_CALL(b, DoB()) 2489 .WillOnce(Return(3)); 2490 ON_CALL(b, DoB(_)) 2491 .WillByDefault(Return(4)); 2492 2493 EXPECT_EQ(3, b.DoB()); 2494 EXPECT_EQ(4, b.DoB(1)); 2495 } 2496 2497 // Tests that calling VerifyAndClear() on one mock object does not 2498 // affect other mock objects (either of the same type or not). 2499 TEST(VerifyAndClearTest, DoesNotAffectOtherMockObjects) { 2500 MockA a; 2501 MockB b1; 2502 MockB b2; 2503 2504 ON_CALL(a, Binary(_, _)) 2505 .WillByDefault(Return(true)); 2506 EXPECT_CALL(a, Binary(_, _)) 2507 .WillOnce(DoDefault()) 2508 .WillOnce(Return(false)); 2509 2510 ON_CALL(b1, DoB()) 2511 .WillByDefault(Return(1)); 2512 EXPECT_CALL(b1, DoB(_)) 2513 .WillOnce(Return(2)); 2514 2515 ON_CALL(b2, DoB()) 2516 .WillByDefault(Return(3)); 2517 EXPECT_CALL(b2, DoB(_)); 2518 2519 b2.DoB(0); 2520 Mock::VerifyAndClear(&b2); 2521 2522 // Verifies that the default actions and expectations of a and b1 2523 // are still in effect. 2524 EXPECT_TRUE(a.Binary(0, 0)); 2525 EXPECT_FALSE(a.Binary(0, 0)); 2526 2527 EXPECT_EQ(1, b1.DoB()); 2528 EXPECT_EQ(2, b1.DoB(0)); 2529 } 2530 2531 TEST(VerifyAndClearTest, 2532 DestroyingChainedMocksDoesNotDeadlockThroughExpectations) { 2533 linked_ptr<MockA> a(new MockA); 2534 ReferenceHoldingMock test_mock; 2535 2536 // EXPECT_CALL stores a reference to a inside test_mock. 2537 EXPECT_CALL(test_mock, AcceptReference(_)) 2538 .WillRepeatedly(SetArgPointee<0>(a)); 2539 2540 // Throw away the reference to the mock that we have in a. After this, the 2541 // only reference to it is stored by test_mock. 2542 a.reset(); 2543 2544 // When test_mock goes out of scope, it destroys the last remaining reference 2545 // to the mock object originally pointed to by a. This will cause the MockA 2546 // destructor to be called from inside the ReferenceHoldingMock destructor. 2547 // The state of all mocks is protected by a single global lock, but there 2548 // should be no deadlock. 2549 } 2550 2551 TEST(VerifyAndClearTest, 2552 DestroyingChainedMocksDoesNotDeadlockThroughDefaultAction) { 2553 linked_ptr<MockA> a(new MockA); 2554 ReferenceHoldingMock test_mock; 2555 2556 // ON_CALL stores a reference to a inside test_mock. 2557 ON_CALL(test_mock, AcceptReference(_)) 2558 .WillByDefault(SetArgPointee<0>(a)); 2559 2560 // Throw away the reference to the mock that we have in a. After this, the 2561 // only reference to it is stored by test_mock. 2562 a.reset(); 2563 2564 // When test_mock goes out of scope, it destroys the last remaining reference 2565 // to the mock object originally pointed to by a. This will cause the MockA 2566 // destructor to be called from inside the ReferenceHoldingMock destructor. 2567 // The state of all mocks is protected by a single global lock, but there 2568 // should be no deadlock. 2569 } 2570 2571 // Tests that a mock function's action can call a mock function 2572 // (either the same function or a different one) either as an explicit 2573 // action or as a default action without causing a dead lock. It 2574 // verifies that the action is not performed inside the critical 2575 // section. 2576 TEST(SynchronizationTest, CanCallMockMethodInAction) { 2577 MockA a; 2578 MockC c; 2579 ON_CALL(a, DoA(_)) 2580 .WillByDefault(IgnoreResult(InvokeWithoutArgs(&c, 2581 &MockC::NonVoidMethod))); 2582 EXPECT_CALL(a, DoA(1)); 2583 EXPECT_CALL(a, DoA(1)) 2584 .WillOnce(Invoke(&a, &MockA::DoA)) 2585 .RetiresOnSaturation(); 2586 EXPECT_CALL(c, NonVoidMethod()); 2587 2588 a.DoA(1); 2589 // This will match the second EXPECT_CALL() and trigger another a.DoA(1), 2590 // which will in turn match the first EXPECT_CALL() and trigger a call to 2591 // c.NonVoidMethod() that was specified by the ON_CALL() since the first 2592 // EXPECT_CALL() did not specify an action. 2593 } 2594 2595 } // namespace 2596 2597 // Allows the user to define his own main and then invoke gmock_main 2598 // from it. This might be necessary on some platforms which require 2599 // specific setup and teardown. 2600 #if GMOCK_RENAME_MAIN 2601 int gmock_main(int argc, char **argv) { 2602 #else 2603 int main(int argc, char **argv) { 2604 #endif // GMOCK_RENAME_MAIN 2605 testing::InitGoogleMock(&argc, argv); 2606 2607 // Ensures that the tests pass no matter what value of 2608 // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies. 2609 testing::GMOCK_FLAG(catch_leaked_mocks) = true; 2610 testing::GMOCK_FLAG(verbose) = testing::internal::kWarningVerbosity; 2611 2612 return RUN_ALL_TESTS(); 2613 }