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