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


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:42