gmock-spec-builders.h
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 implements the ON_CALL() and EXPECT_CALL() macros.
00035 //
00036 // A user can use the ON_CALL() macro to specify the default action of
00037 // a mock method.  The syntax is:
00038 //
00039 //   ON_CALL(mock_object, Method(argument-matchers))
00040 //       .With(multi-argument-matcher)
00041 //       .WillByDefault(action);
00042 //
00043 //  where the .With() clause is optional.
00044 //
00045 // A user can use the EXPECT_CALL() macro to specify an expectation on
00046 // a mock method.  The syntax is:
00047 //
00048 //   EXPECT_CALL(mock_object, Method(argument-matchers))
00049 //       .With(multi-argument-matchers)
00050 //       .Times(cardinality)
00051 //       .InSequence(sequences)
00052 //       .After(expectations)
00053 //       .WillOnce(action)
00054 //       .WillRepeatedly(action)
00055 //       .RetiresOnSaturation();
00056 //
00057 // where all clauses are optional, and .InSequence()/.After()/
00058 // .WillOnce() can appear any number of times.
00059 
00060 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_
00061 #define GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_
00062 
00063 #include <map>
00064 #include <set>
00065 #include <sstream>
00066 #include <string>
00067 #include <vector>
00068 
00069 #if GTEST_HAS_EXCEPTIONS
00070 # include <stdexcept>  // NOLINT
00071 #endif
00072 
00073 #include "gmock/gmock-actions.h"
00074 #include "gmock/gmock-cardinalities.h"
00075 #include "gmock/gmock-matchers.h"
00076 #include "gmock/internal/gmock-internal-utils.h"
00077 #include "gmock/internal/gmock-port.h"
00078 #include "gtest/gtest.h"
00079 
00080 namespace testing {
00081 
00082 // An abstract handle of an expectation.
00083 class Expectation;
00084 
00085 // A set of expectation handles.
00086 class ExpectationSet;
00087 
00088 // Anything inside the 'internal' namespace IS INTERNAL IMPLEMENTATION
00089 // and MUST NOT BE USED IN USER CODE!!!
00090 namespace internal {
00091 
00092 // Implements a mock function.
00093 template <typename F> class FunctionMocker;
00094 
00095 // Base class for expectations.
00096 class ExpectationBase;
00097 
00098 // Implements an expectation.
00099 template <typename F> class TypedExpectation;
00100 
00101 // Helper class for testing the Expectation class template.
00102 class ExpectationTester;
00103 
00104 // Base class for function mockers.
00105 template <typename F> class FunctionMockerBase;
00106 
00107 // Protects the mock object registry (in class Mock), all function
00108 // mockers, and all expectations.
00109 //
00110 // The reason we don't use more fine-grained protection is: when a
00111 // mock function Foo() is called, it needs to consult its expectations
00112 // to see which one should be picked.  If another thread is allowed to
00113 // call a mock function (either Foo() or a different one) at the same
00114 // time, it could affect the "retired" attributes of Foo()'s
00115 // expectations when InSequence() is used, and thus affect which
00116 // expectation gets picked.  Therefore, we sequence all mock function
00117 // calls to ensure the integrity of the mock objects' states.
00118 GTEST_API_ GTEST_DECLARE_STATIC_MUTEX_(g_gmock_mutex);
00119 
00120 // Untyped base class for ActionResultHolder<R>.
00121 class UntypedActionResultHolderBase;
00122 
00123 // Abstract base class of FunctionMockerBase.  This is the
00124 // type-agnostic part of the function mocker interface.  Its pure
00125 // virtual methods are implemented by FunctionMockerBase.
00126 class GTEST_API_ UntypedFunctionMockerBase {
00127  public:
00128   UntypedFunctionMockerBase();
00129   virtual ~UntypedFunctionMockerBase();
00130 
00131   // Verifies that all expectations on this mock function have been
00132   // satisfied.  Reports one or more Google Test non-fatal failures
00133   // and returns false if not.
00134   bool VerifyAndClearExpectationsLocked()
00135       GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex);
00136 
00137   // Clears the ON_CALL()s set on this mock function.
00138   virtual void ClearDefaultActionsLocked()
00139       GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) = 0;
00140 
00141   // In all of the following Untyped* functions, it's the caller's
00142   // responsibility to guarantee the correctness of the arguments'
00143   // types.
00144 
00145   // Performs the default action with the given arguments and returns
00146   // the action's result.  The call description string will be used in
00147   // the error message to describe the call in the case the default
00148   // action fails.
00149   // L = *
00150   virtual UntypedActionResultHolderBase* UntypedPerformDefaultAction(
00151       const void* untyped_args,
00152       const string& call_description) const = 0;
00153 
00154   // Performs the given action with the given arguments and returns
00155   // the action's result.
00156   // L = *
00157   virtual UntypedActionResultHolderBase* UntypedPerformAction(
00158       const void* untyped_action,
00159       const void* untyped_args) const = 0;
00160 
00161   // Writes a message that the call is uninteresting (i.e. neither
00162   // explicitly expected nor explicitly unexpected) to the given
00163   // ostream.
00164   virtual void UntypedDescribeUninterestingCall(
00165       const void* untyped_args,
00166       ::std::ostream* os) const
00167           GTEST_LOCK_EXCLUDED_(g_gmock_mutex) = 0;
00168 
00169   // Returns the expectation that matches the given function arguments
00170   // (or NULL is there's no match); when a match is found,
00171   // untyped_action is set to point to the action that should be
00172   // performed (or NULL if the action is "do default"), and
00173   // is_excessive is modified to indicate whether the call exceeds the
00174   // expected number.
00175   virtual const ExpectationBase* UntypedFindMatchingExpectation(
00176       const void* untyped_args,
00177       const void** untyped_action, bool* is_excessive,
00178       ::std::ostream* what, ::std::ostream* why)
00179           GTEST_LOCK_EXCLUDED_(g_gmock_mutex) = 0;
00180 
00181   // Prints the given function arguments to the ostream.
00182   virtual void UntypedPrintArgs(const void* untyped_args,
00183                                 ::std::ostream* os) const = 0;
00184 
00185   // Sets the mock object this mock method belongs to, and registers
00186   // this information in the global mock registry.  Will be called
00187   // whenever an EXPECT_CALL() or ON_CALL() is executed on this mock
00188   // method.
00189   // TODO(wan@google.com): rename to SetAndRegisterOwner().
00190   void RegisterOwner(const void* mock_obj)
00191       GTEST_LOCK_EXCLUDED_(g_gmock_mutex);
00192 
00193   // Sets the mock object this mock method belongs to, and sets the
00194   // name of the mock function.  Will be called upon each invocation
00195   // of this mock function.
00196   void SetOwnerAndName(const void* mock_obj, const char* name)
00197       GTEST_LOCK_EXCLUDED_(g_gmock_mutex);
00198 
00199   // Returns the mock object this mock method belongs to.  Must be
00200   // called after RegisterOwner() or SetOwnerAndName() has been
00201   // called.
00202   const void* MockObject() const
00203       GTEST_LOCK_EXCLUDED_(g_gmock_mutex);
00204 
00205   // Returns the name of this mock method.  Must be called after
00206   // SetOwnerAndName() has been called.
00207   const char* Name() const
00208       GTEST_LOCK_EXCLUDED_(g_gmock_mutex);
00209 
00210   // Returns the result of invoking this mock function with the given
00211   // arguments.  This function can be safely called from multiple
00212   // threads concurrently.  The caller is responsible for deleting the
00213   // result.
00214   const UntypedActionResultHolderBase* UntypedInvokeWith(
00215       const void* untyped_args)
00216           GTEST_LOCK_EXCLUDED_(g_gmock_mutex);
00217 
00218  protected:
00219   typedef std::vector<const void*> UntypedOnCallSpecs;
00220 
00221   typedef std::vector<internal::linked_ptr<ExpectationBase> >
00222   UntypedExpectations;
00223 
00224   // Returns an Expectation object that references and co-owns exp,
00225   // which must be an expectation on this mock function.
00226   Expectation GetHandleOf(ExpectationBase* exp);
00227 
00228   // Address of the mock object this mock method belongs to.  Only
00229   // valid after this mock method has been called or
00230   // ON_CALL/EXPECT_CALL has been invoked on it.
00231   const void* mock_obj_;  // Protected by g_gmock_mutex.
00232 
00233   // Name of the function being mocked.  Only valid after this mock
00234   // method has been called.
00235   const char* name_;  // Protected by g_gmock_mutex.
00236 
00237   // All default action specs for this function mocker.
00238   UntypedOnCallSpecs untyped_on_call_specs_;
00239 
00240   // All expectations for this function mocker.
00241   UntypedExpectations untyped_expectations_;
00242 };  // class UntypedFunctionMockerBase
00243 
00244 // Untyped base class for OnCallSpec<F>.
00245 class UntypedOnCallSpecBase {
00246  public:
00247   // The arguments are the location of the ON_CALL() statement.
00248   UntypedOnCallSpecBase(const char* a_file, int a_line)
00249       : file_(a_file), line_(a_line), last_clause_(kNone) {}
00250 
00251   // Where in the source file was the default action spec defined?
00252   const char* file() const { return file_; }
00253   int line() const { return line_; }
00254 
00255  protected:
00256   // Gives each clause in the ON_CALL() statement a name.
00257   enum Clause {
00258     // Do not change the order of the enum members!  The run-time
00259     // syntax checking relies on it.
00260     kNone,
00261     kWith,
00262     kWillByDefault
00263   };
00264 
00265   // Asserts that the ON_CALL() statement has a certain property.
00266   void AssertSpecProperty(bool property, const string& failure_message) const {
00267     Assert(property, file_, line_, failure_message);
00268   }
00269 
00270   // Expects that the ON_CALL() statement has a certain property.
00271   void ExpectSpecProperty(bool property, const string& failure_message) const {
00272     Expect(property, file_, line_, failure_message);
00273   }
00274 
00275   const char* file_;
00276   int line_;
00277 
00278   // The last clause in the ON_CALL() statement as seen so far.
00279   // Initially kNone and changes as the statement is parsed.
00280   Clause last_clause_;
00281 };  // class UntypedOnCallSpecBase
00282 
00283 // This template class implements an ON_CALL spec.
00284 template <typename F>
00285 class OnCallSpec : public UntypedOnCallSpecBase {
00286  public:
00287   typedef typename Function<F>::ArgumentTuple ArgumentTuple;
00288   typedef typename Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple;
00289 
00290   // Constructs an OnCallSpec object from the information inside
00291   // the parenthesis of an ON_CALL() statement.
00292   OnCallSpec(const char* a_file, int a_line,
00293              const ArgumentMatcherTuple& matchers)
00294       : UntypedOnCallSpecBase(a_file, a_line),
00295         matchers_(matchers),
00296         // By default, extra_matcher_ should match anything.  However,
00297         // we cannot initialize it with _ as that triggers a compiler
00298         // bug in Symbian's C++ compiler (cannot decide between two
00299         // overloaded constructors of Matcher<const ArgumentTuple&>).
00300         extra_matcher_(A<const ArgumentTuple&>()) {
00301   }
00302 
00303   // Implements the .With() clause.
00304   OnCallSpec& With(const Matcher<const ArgumentTuple&>& m) {
00305     // Makes sure this is called at most once.
00306     ExpectSpecProperty(last_clause_ < kWith,
00307                        ".With() cannot appear "
00308                        "more than once in an ON_CALL().");
00309     last_clause_ = kWith;
00310 
00311     extra_matcher_ = m;
00312     return *this;
00313   }
00314 
00315   // Implements the .WillByDefault() clause.
00316   OnCallSpec& WillByDefault(const Action<F>& action) {
00317     ExpectSpecProperty(last_clause_ < kWillByDefault,
00318                        ".WillByDefault() must appear "
00319                        "exactly once in an ON_CALL().");
00320     last_clause_ = kWillByDefault;
00321 
00322     ExpectSpecProperty(!action.IsDoDefault(),
00323                        "DoDefault() cannot be used in ON_CALL().");
00324     action_ = action;
00325     return *this;
00326   }
00327 
00328   // Returns true iff the given arguments match the matchers.
00329   bool Matches(const ArgumentTuple& args) const {
00330     return TupleMatches(matchers_, args) && extra_matcher_.Matches(args);
00331   }
00332 
00333   // Returns the action specified by the user.
00334   const Action<F>& GetAction() const {
00335     AssertSpecProperty(last_clause_ == kWillByDefault,
00336                        ".WillByDefault() must appear exactly "
00337                        "once in an ON_CALL().");
00338     return action_;
00339   }
00340 
00341  private:
00342   // The information in statement
00343   //
00344   //   ON_CALL(mock_object, Method(matchers))
00345   //       .With(multi-argument-matcher)
00346   //       .WillByDefault(action);
00347   //
00348   // is recorded in the data members like this:
00349   //
00350   //   source file that contains the statement => file_
00351   //   line number of the statement            => line_
00352   //   matchers                                => matchers_
00353   //   multi-argument-matcher                  => extra_matcher_
00354   //   action                                  => action_
00355   ArgumentMatcherTuple matchers_;
00356   Matcher<const ArgumentTuple&> extra_matcher_;
00357   Action<F> action_;
00358 };  // class OnCallSpec
00359 
00360 // Possible reactions on uninteresting calls.
00361 enum CallReaction {
00362   kAllow,
00363   kWarn,
00364   kFail,
00365   kDefault = kWarn  // By default, warn about uninteresting calls.
00366 };
00367 
00368 }  // namespace internal
00369 
00370 // Utilities for manipulating mock objects.
00371 class GTEST_API_ Mock {
00372  public:
00373   // The following public methods can be called concurrently.
00374 
00375   // Tells Google Mock to ignore mock_obj when checking for leaked
00376   // mock objects.
00377   static void AllowLeak(const void* mock_obj)
00378       GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
00379 
00380   // Verifies and clears all expectations on the given mock object.
00381   // If the expectations aren't satisfied, generates one or more
00382   // Google Test non-fatal failures and returns false.
00383   static bool VerifyAndClearExpectations(void* mock_obj)
00384       GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
00385 
00386   // Verifies all expectations on the given mock object and clears its
00387   // default actions and expectations.  Returns true iff the
00388   // verification was successful.
00389   static bool VerifyAndClear(void* mock_obj)
00390       GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
00391 
00392  private:
00393   friend class internal::UntypedFunctionMockerBase;
00394 
00395   // Needed for a function mocker to register itself (so that we know
00396   // how to clear a mock object).
00397   template <typename F>
00398   friend class internal::FunctionMockerBase;
00399 
00400   template <typename M>
00401   friend class NiceMock;
00402 
00403   template <typename M>
00404   friend class NaggyMock;
00405 
00406   template <typename M>
00407   friend class StrictMock;
00408 
00409   // Tells Google Mock to allow uninteresting calls on the given mock
00410   // object.
00411   static void AllowUninterestingCalls(const void* mock_obj)
00412       GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
00413 
00414   // Tells Google Mock to warn the user about uninteresting calls on
00415   // the given mock object.
00416   static void WarnUninterestingCalls(const void* mock_obj)
00417       GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
00418 
00419   // Tells Google Mock to fail uninteresting calls on the given mock
00420   // object.
00421   static void FailUninterestingCalls(const void* mock_obj)
00422       GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
00423 
00424   // Tells Google Mock the given mock object is being destroyed and
00425   // its entry in the call-reaction table should be removed.
00426   static void UnregisterCallReaction(const void* mock_obj)
00427       GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
00428 
00429   // Returns the reaction Google Mock will have on uninteresting calls
00430   // made on the given mock object.
00431   static internal::CallReaction GetReactionOnUninterestingCalls(
00432       const void* mock_obj)
00433           GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
00434 
00435   // Verifies that all expectations on the given mock object have been
00436   // satisfied.  Reports one or more Google Test non-fatal failures
00437   // and returns false if not.
00438   static bool VerifyAndClearExpectationsLocked(void* mock_obj)
00439       GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex);
00440 
00441   // Clears all ON_CALL()s set on the given mock object.
00442   static void ClearDefaultActionsLocked(void* mock_obj)
00443       GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex);
00444 
00445   // Registers a mock object and a mock method it owns.
00446   static void Register(
00447       const void* mock_obj,
00448       internal::UntypedFunctionMockerBase* mocker)
00449           GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
00450 
00451   // Tells Google Mock where in the source code mock_obj is used in an
00452   // ON_CALL or EXPECT_CALL.  In case mock_obj is leaked, this
00453   // information helps the user identify which object it is.
00454   static void RegisterUseByOnCallOrExpectCall(
00455       const void* mock_obj, const char* file, int line)
00456           GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
00457 
00458   // Unregisters a mock method; removes the owning mock object from
00459   // the registry when the last mock method associated with it has
00460   // been unregistered.  This is called only in the destructor of
00461   // FunctionMockerBase.
00462   static void UnregisterLocked(internal::UntypedFunctionMockerBase* mocker)
00463       GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex);
00464 };  // class Mock
00465 
00466 // An abstract handle of an expectation.  Useful in the .After()
00467 // clause of EXPECT_CALL() for setting the (partial) order of
00468 // expectations.  The syntax:
00469 //
00470 //   Expectation e1 = EXPECT_CALL(...)...;
00471 //   EXPECT_CALL(...).After(e1)...;
00472 //
00473 // sets two expectations where the latter can only be matched after
00474 // the former has been satisfied.
00475 //
00476 // Notes:
00477 //   - This class is copyable and has value semantics.
00478 //   - Constness is shallow: a const Expectation object itself cannot
00479 //     be modified, but the mutable methods of the ExpectationBase
00480 //     object it references can be called via expectation_base().
00481 //   - The constructors and destructor are defined out-of-line because
00482 //     the Symbian WINSCW compiler wants to otherwise instantiate them
00483 //     when it sees this class definition, at which point it doesn't have
00484 //     ExpectationBase available yet, leading to incorrect destruction
00485 //     in the linked_ptr (or compilation errors if using a checking
00486 //     linked_ptr).
00487 class GTEST_API_ Expectation {
00488  public:
00489   // Constructs a null object that doesn't reference any expectation.
00490   Expectation();
00491 
00492   ~Expectation();
00493 
00494   // This single-argument ctor must not be explicit, in order to support the
00495   //   Expectation e = EXPECT_CALL(...);
00496   // syntax.
00497   //
00498   // A TypedExpectation object stores its pre-requisites as
00499   // Expectation objects, and needs to call the non-const Retire()
00500   // method on the ExpectationBase objects they reference.  Therefore
00501   // Expectation must receive a *non-const* reference to the
00502   // ExpectationBase object.
00503   Expectation(internal::ExpectationBase& exp);  // NOLINT
00504 
00505   // The compiler-generated copy ctor and operator= work exactly as
00506   // intended, so we don't need to define our own.
00507 
00508   // Returns true iff rhs references the same expectation as this object does.
00509   bool operator==(const Expectation& rhs) const {
00510     return expectation_base_ == rhs.expectation_base_;
00511   }
00512 
00513   bool operator!=(const Expectation& rhs) const { return !(*this == rhs); }
00514 
00515  private:
00516   friend class ExpectationSet;
00517   friend class Sequence;
00518   friend class ::testing::internal::ExpectationBase;
00519   friend class ::testing::internal::UntypedFunctionMockerBase;
00520 
00521   template <typename F>
00522   friend class ::testing::internal::FunctionMockerBase;
00523 
00524   template <typename F>
00525   friend class ::testing::internal::TypedExpectation;
00526 
00527   // This comparator is needed for putting Expectation objects into a set.
00528   class Less {
00529    public:
00530     bool operator()(const Expectation& lhs, const Expectation& rhs) const {
00531       return lhs.expectation_base_.get() < rhs.expectation_base_.get();
00532     }
00533   };
00534 
00535   typedef ::std::set<Expectation, Less> Set;
00536 
00537   Expectation(
00538       const internal::linked_ptr<internal::ExpectationBase>& expectation_base);
00539 
00540   // Returns the expectation this object references.
00541   const internal::linked_ptr<internal::ExpectationBase>&
00542   expectation_base() const {
00543     return expectation_base_;
00544   }
00545 
00546   // A linked_ptr that co-owns the expectation this handle references.
00547   internal::linked_ptr<internal::ExpectationBase> expectation_base_;
00548 };
00549 
00550 // A set of expectation handles.  Useful in the .After() clause of
00551 // EXPECT_CALL() for setting the (partial) order of expectations.  The
00552 // syntax:
00553 //
00554 //   ExpectationSet es;
00555 //   es += EXPECT_CALL(...)...;
00556 //   es += EXPECT_CALL(...)...;
00557 //   EXPECT_CALL(...).After(es)...;
00558 //
00559 // sets three expectations where the last one can only be matched
00560 // after the first two have both been satisfied.
00561 //
00562 // This class is copyable and has value semantics.
00563 class ExpectationSet {
00564  public:
00565   // A bidirectional iterator that can read a const element in the set.
00566   typedef Expectation::Set::const_iterator const_iterator;
00567 
00568   // An object stored in the set.  This is an alias of Expectation.
00569   typedef Expectation::Set::value_type value_type;
00570 
00571   // Constructs an empty set.
00572   ExpectationSet() {}
00573 
00574   // This single-argument ctor must not be explicit, in order to support the
00575   //   ExpectationSet es = EXPECT_CALL(...);
00576   // syntax.
00577   ExpectationSet(internal::ExpectationBase& exp) {  // NOLINT
00578     *this += Expectation(exp);
00579   }
00580 
00581   // This single-argument ctor implements implicit conversion from
00582   // Expectation and thus must not be explicit.  This allows either an
00583   // Expectation or an ExpectationSet to be used in .After().
00584   ExpectationSet(const Expectation& e) {  // NOLINT
00585     *this += e;
00586   }
00587 
00588   // The compiler-generator ctor and operator= works exactly as
00589   // intended, so we don't need to define our own.
00590 
00591   // Returns true iff rhs contains the same set of Expectation objects
00592   // as this does.
00593   bool operator==(const ExpectationSet& rhs) const {
00594     return expectations_ == rhs.expectations_;
00595   }
00596 
00597   bool operator!=(const ExpectationSet& rhs) const { return !(*this == rhs); }
00598 
00599   // Implements the syntax
00600   //   expectation_set += EXPECT_CALL(...);
00601   ExpectationSet& operator+=(const Expectation& e) {
00602     expectations_.insert(e);
00603     return *this;
00604   }
00605 
00606   int size() const { return static_cast<int>(expectations_.size()); }
00607 
00608   const_iterator begin() const { return expectations_.begin(); }
00609   const_iterator end() const { return expectations_.end(); }
00610 
00611  private:
00612   Expectation::Set expectations_;
00613 };
00614 
00615 
00616 // Sequence objects are used by a user to specify the relative order
00617 // in which the expectations should match.  They are copyable (we rely
00618 // on the compiler-defined copy constructor and assignment operator).
00619 class GTEST_API_ Sequence {
00620  public:
00621   // Constructs an empty sequence.
00622   Sequence() : last_expectation_(new Expectation) {}
00623 
00624   // Adds an expectation to this sequence.  The caller must ensure
00625   // that no other thread is accessing this Sequence object.
00626   void AddExpectation(const Expectation& expectation) const;
00627 
00628  private:
00629   // The last expectation in this sequence.  We use a linked_ptr here
00630   // because Sequence objects are copyable and we want the copies to
00631   // be aliases.  The linked_ptr allows the copies to co-own and share
00632   // the same Expectation object.
00633   internal::linked_ptr<Expectation> last_expectation_;
00634 };  // class Sequence
00635 
00636 // An object of this type causes all EXPECT_CALL() statements
00637 // encountered in its scope to be put in an anonymous sequence.  The
00638 // work is done in the constructor and destructor.  You should only
00639 // create an InSequence object on the stack.
00640 //
00641 // The sole purpose for this class is to support easy definition of
00642 // sequential expectations, e.g.
00643 //
00644 //   {
00645 //     InSequence dummy;  // The name of the object doesn't matter.
00646 //
00647 //     // The following expectations must match in the order they appear.
00648 //     EXPECT_CALL(a, Bar())...;
00649 //     EXPECT_CALL(a, Baz())...;
00650 //     ...
00651 //     EXPECT_CALL(b, Xyz())...;
00652 //   }
00653 //
00654 // You can create InSequence objects in multiple threads, as long as
00655 // they are used to affect different mock objects.  The idea is that
00656 // each thread can create and set up its own mocks as if it's the only
00657 // thread.  However, for clarity of your tests we recommend you to set
00658 // up mocks in the main thread unless you have a good reason not to do
00659 // so.
00660 class GTEST_API_ InSequence {
00661  public:
00662   InSequence();
00663   ~InSequence();
00664  private:
00665   bool sequence_created_;
00666 
00667   GTEST_DISALLOW_COPY_AND_ASSIGN_(InSequence);  // NOLINT
00668 } GTEST_ATTRIBUTE_UNUSED_;
00669 
00670 namespace internal {
00671 
00672 // Points to the implicit sequence introduced by a living InSequence
00673 // object (if any) in the current thread or NULL.
00674 GTEST_API_ extern ThreadLocal<Sequence*> g_gmock_implicit_sequence;
00675 
00676 // Base class for implementing expectations.
00677 //
00678 // There are two reasons for having a type-agnostic base class for
00679 // Expectation:
00680 //
00681 //   1. We need to store collections of expectations of different
00682 //   types (e.g. all pre-requisites of a particular expectation, all
00683 //   expectations in a sequence).  Therefore these expectation objects
00684 //   must share a common base class.
00685 //
00686 //   2. We can avoid binary code bloat by moving methods not depending
00687 //   on the template argument of Expectation to the base class.
00688 //
00689 // This class is internal and mustn't be used by user code directly.
00690 class GTEST_API_ ExpectationBase {
00691  public:
00692   // source_text is the EXPECT_CALL(...) source that created this Expectation.
00693   ExpectationBase(const char* file, int line, const string& source_text);
00694 
00695   virtual ~ExpectationBase();
00696 
00697   // Where in the source file was the expectation spec defined?
00698   const char* file() const { return file_; }
00699   int line() const { return line_; }
00700   const char* source_text() const { return source_text_.c_str(); }
00701   // Returns the cardinality specified in the expectation spec.
00702   const Cardinality& cardinality() const { return cardinality_; }
00703 
00704   // Describes the source file location of this expectation.
00705   void DescribeLocationTo(::std::ostream* os) const {
00706     *os << FormatFileLocation(file(), line()) << " ";
00707   }
00708 
00709   // Describes how many times a function call matching this
00710   // expectation has occurred.
00711   void DescribeCallCountTo(::std::ostream* os) const
00712       GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex);
00713 
00714   // If this mock method has an extra matcher (i.e. .With(matcher)),
00715   // describes it to the ostream.
00716   virtual void MaybeDescribeExtraMatcherTo(::std::ostream* os) = 0;
00717 
00718  protected:
00719   friend class ::testing::Expectation;
00720   friend class UntypedFunctionMockerBase;
00721 
00722   enum Clause {
00723     // Don't change the order of the enum members!
00724     kNone,
00725     kWith,
00726     kTimes,
00727     kInSequence,
00728     kAfter,
00729     kWillOnce,
00730     kWillRepeatedly,
00731     kRetiresOnSaturation
00732   };
00733 
00734   typedef std::vector<const void*> UntypedActions;
00735 
00736   // Returns an Expectation object that references and co-owns this
00737   // expectation.
00738   virtual Expectation GetHandle() = 0;
00739 
00740   // Asserts that the EXPECT_CALL() statement has the given property.
00741   void AssertSpecProperty(bool property, const string& failure_message) const {
00742     Assert(property, file_, line_, failure_message);
00743   }
00744 
00745   // Expects that the EXPECT_CALL() statement has the given property.
00746   void ExpectSpecProperty(bool property, const string& failure_message) const {
00747     Expect(property, file_, line_, failure_message);
00748   }
00749 
00750   // Explicitly specifies the cardinality of this expectation.  Used
00751   // by the subclasses to implement the .Times() clause.
00752   void SpecifyCardinality(const Cardinality& cardinality);
00753 
00754   // Returns true iff the user specified the cardinality explicitly
00755   // using a .Times().
00756   bool cardinality_specified() const { return cardinality_specified_; }
00757 
00758   // Sets the cardinality of this expectation spec.
00759   void set_cardinality(const Cardinality& a_cardinality) {
00760     cardinality_ = a_cardinality;
00761   }
00762 
00763   // The following group of methods should only be called after the
00764   // EXPECT_CALL() statement, and only when g_gmock_mutex is held by
00765   // the current thread.
00766 
00767   // Retires all pre-requisites of this expectation.
00768   void RetireAllPreRequisites()
00769       GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex);
00770 
00771   // Returns true iff this expectation is retired.
00772   bool is_retired() const
00773       GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
00774     g_gmock_mutex.AssertHeld();
00775     return retired_;
00776   }
00777 
00778   // Retires this expectation.
00779   void Retire()
00780       GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
00781     g_gmock_mutex.AssertHeld();
00782     retired_ = true;
00783   }
00784 
00785   // Returns true iff this expectation is satisfied.
00786   bool IsSatisfied() const
00787       GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
00788     g_gmock_mutex.AssertHeld();
00789     return cardinality().IsSatisfiedByCallCount(call_count_);
00790   }
00791 
00792   // Returns true iff this expectation is saturated.
00793   bool IsSaturated() const
00794       GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
00795     g_gmock_mutex.AssertHeld();
00796     return cardinality().IsSaturatedByCallCount(call_count_);
00797   }
00798 
00799   // Returns true iff this expectation is over-saturated.
00800   bool IsOverSaturated() const
00801       GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
00802     g_gmock_mutex.AssertHeld();
00803     return cardinality().IsOverSaturatedByCallCount(call_count_);
00804   }
00805 
00806   // Returns true iff all pre-requisites of this expectation are satisfied.
00807   bool AllPrerequisitesAreSatisfied() const
00808       GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex);
00809 
00810   // Adds unsatisfied pre-requisites of this expectation to 'result'.
00811   void FindUnsatisfiedPrerequisites(ExpectationSet* result) const
00812       GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex);
00813 
00814   // Returns the number this expectation has been invoked.
00815   int call_count() const
00816       GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
00817     g_gmock_mutex.AssertHeld();
00818     return call_count_;
00819   }
00820 
00821   // Increments the number this expectation has been invoked.
00822   void IncrementCallCount()
00823       GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
00824     g_gmock_mutex.AssertHeld();
00825     call_count_++;
00826   }
00827 
00828   // Checks the action count (i.e. the number of WillOnce() and
00829   // WillRepeatedly() clauses) against the cardinality if this hasn't
00830   // been done before.  Prints a warning if there are too many or too
00831   // few actions.
00832   void CheckActionCountIfNotDone() const
00833       GTEST_LOCK_EXCLUDED_(mutex_);
00834 
00835   friend class ::testing::Sequence;
00836   friend class ::testing::internal::ExpectationTester;
00837 
00838   template <typename Function>
00839   friend class TypedExpectation;
00840 
00841   // Implements the .Times() clause.
00842   void UntypedTimes(const Cardinality& a_cardinality);
00843 
00844   // This group of fields are part of the spec and won't change after
00845   // an EXPECT_CALL() statement finishes.
00846   const char* file_;          // The file that contains the expectation.
00847   int line_;                  // The line number of the expectation.
00848   const string source_text_;  // The EXPECT_CALL(...) source text.
00849   // True iff the cardinality is specified explicitly.
00850   bool cardinality_specified_;
00851   Cardinality cardinality_;            // The cardinality of the expectation.
00852   // The immediate pre-requisites (i.e. expectations that must be
00853   // satisfied before this expectation can be matched) of this
00854   // expectation.  We use linked_ptr in the set because we want an
00855   // Expectation object to be co-owned by its FunctionMocker and its
00856   // successors.  This allows multiple mock objects to be deleted at
00857   // different times.
00858   ExpectationSet immediate_prerequisites_;
00859 
00860   // This group of fields are the current state of the expectation,
00861   // and can change as the mock function is called.
00862   int call_count_;  // How many times this expectation has been invoked.
00863   bool retired_;    // True iff this expectation has retired.
00864   UntypedActions untyped_actions_;
00865   bool extra_matcher_specified_;
00866   bool repeated_action_specified_;  // True if a WillRepeatedly() was specified.
00867   bool retires_on_saturation_;
00868   Clause last_clause_;
00869   mutable bool action_count_checked_;  // Under mutex_.
00870   mutable Mutex mutex_;  // Protects action_count_checked_.
00871 
00872   GTEST_DISALLOW_ASSIGN_(ExpectationBase);
00873 };  // class ExpectationBase
00874 
00875 // Impements an expectation for the given function type.
00876 template <typename F>
00877 class TypedExpectation : public ExpectationBase {
00878  public:
00879   typedef typename Function<F>::ArgumentTuple ArgumentTuple;
00880   typedef typename Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple;
00881   typedef typename Function<F>::Result Result;
00882 
00883   TypedExpectation(FunctionMockerBase<F>* owner,
00884                    const char* a_file, int a_line, const string& a_source_text,
00885                    const ArgumentMatcherTuple& m)
00886       : ExpectationBase(a_file, a_line, a_source_text),
00887         owner_(owner),
00888         matchers_(m),
00889         // By default, extra_matcher_ should match anything.  However,
00890         // we cannot initialize it with _ as that triggers a compiler
00891         // bug in Symbian's C++ compiler (cannot decide between two
00892         // overloaded constructors of Matcher<const ArgumentTuple&>).
00893         extra_matcher_(A<const ArgumentTuple&>()),
00894         repeated_action_(DoDefault()) {}
00895 
00896   virtual ~TypedExpectation() {
00897     // Check the validity of the action count if it hasn't been done
00898     // yet (for example, if the expectation was never used).
00899     CheckActionCountIfNotDone();
00900     for (UntypedActions::const_iterator it = untyped_actions_.begin();
00901          it != untyped_actions_.end(); ++it) {
00902       delete static_cast<const Action<F>*>(*it);
00903     }
00904   }
00905 
00906   // Implements the .With() clause.
00907   TypedExpectation& With(const Matcher<const ArgumentTuple&>& m) {
00908     if (last_clause_ == kWith) {
00909       ExpectSpecProperty(false,
00910                          ".With() cannot appear "
00911                          "more than once in an EXPECT_CALL().");
00912     } else {
00913       ExpectSpecProperty(last_clause_ < kWith,
00914                          ".With() must be the first "
00915                          "clause in an EXPECT_CALL().");
00916     }
00917     last_clause_ = kWith;
00918 
00919     extra_matcher_ = m;
00920     extra_matcher_specified_ = true;
00921     return *this;
00922   }
00923 
00924   // Implements the .Times() clause.
00925   TypedExpectation& Times(const Cardinality& a_cardinality) {
00926     ExpectationBase::UntypedTimes(a_cardinality);
00927     return *this;
00928   }
00929 
00930   // Implements the .Times() clause.
00931   TypedExpectation& Times(int n) {
00932     return Times(Exactly(n));
00933   }
00934 
00935   // Implements the .InSequence() clause.
00936   TypedExpectation& InSequence(const Sequence& s) {
00937     ExpectSpecProperty(last_clause_ <= kInSequence,
00938                        ".InSequence() cannot appear after .After(),"
00939                        " .WillOnce(), .WillRepeatedly(), or "
00940                        ".RetiresOnSaturation().");
00941     last_clause_ = kInSequence;
00942 
00943     s.AddExpectation(GetHandle());
00944     return *this;
00945   }
00946   TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2) {
00947     return InSequence(s1).InSequence(s2);
00948   }
00949   TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2,
00950                                const Sequence& s3) {
00951     return InSequence(s1, s2).InSequence(s3);
00952   }
00953   TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2,
00954                                const Sequence& s3, const Sequence& s4) {
00955     return InSequence(s1, s2, s3).InSequence(s4);
00956   }
00957   TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2,
00958                                const Sequence& s3, const Sequence& s4,
00959                                const Sequence& s5) {
00960     return InSequence(s1, s2, s3, s4).InSequence(s5);
00961   }
00962 
00963   // Implements that .After() clause.
00964   TypedExpectation& After(const ExpectationSet& s) {
00965     ExpectSpecProperty(last_clause_ <= kAfter,
00966                        ".After() cannot appear after .WillOnce(),"
00967                        " .WillRepeatedly(), or "
00968                        ".RetiresOnSaturation().");
00969     last_clause_ = kAfter;
00970 
00971     for (ExpectationSet::const_iterator it = s.begin(); it != s.end(); ++it) {
00972       immediate_prerequisites_ += *it;
00973     }
00974     return *this;
00975   }
00976   TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2) {
00977     return After(s1).After(s2);
00978   }
00979   TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2,
00980                           const ExpectationSet& s3) {
00981     return After(s1, s2).After(s3);
00982   }
00983   TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2,
00984                           const ExpectationSet& s3, const ExpectationSet& s4) {
00985     return After(s1, s2, s3).After(s4);
00986   }
00987   TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2,
00988                           const ExpectationSet& s3, const ExpectationSet& s4,
00989                           const ExpectationSet& s5) {
00990     return After(s1, s2, s3, s4).After(s5);
00991   }
00992 
00993   // Implements the .WillOnce() clause.
00994   TypedExpectation& WillOnce(const Action<F>& action) {
00995     ExpectSpecProperty(last_clause_ <= kWillOnce,
00996                        ".WillOnce() cannot appear after "
00997                        ".WillRepeatedly() or .RetiresOnSaturation().");
00998     last_clause_ = kWillOnce;
00999 
01000     untyped_actions_.push_back(new Action<F>(action));
01001     if (!cardinality_specified()) {
01002       set_cardinality(Exactly(static_cast<int>(untyped_actions_.size())));
01003     }
01004     return *this;
01005   }
01006 
01007   // Implements the .WillRepeatedly() clause.
01008   TypedExpectation& WillRepeatedly(const Action<F>& action) {
01009     if (last_clause_ == kWillRepeatedly) {
01010       ExpectSpecProperty(false,
01011                          ".WillRepeatedly() cannot appear "
01012                          "more than once in an EXPECT_CALL().");
01013     } else {
01014       ExpectSpecProperty(last_clause_ < kWillRepeatedly,
01015                          ".WillRepeatedly() cannot appear "
01016                          "after .RetiresOnSaturation().");
01017     }
01018     last_clause_ = kWillRepeatedly;
01019     repeated_action_specified_ = true;
01020 
01021     repeated_action_ = action;
01022     if (!cardinality_specified()) {
01023       set_cardinality(AtLeast(static_cast<int>(untyped_actions_.size())));
01024     }
01025 
01026     // Now that no more action clauses can be specified, we check
01027     // whether their count makes sense.
01028     CheckActionCountIfNotDone();
01029     return *this;
01030   }
01031 
01032   // Implements the .RetiresOnSaturation() clause.
01033   TypedExpectation& RetiresOnSaturation() {
01034     ExpectSpecProperty(last_clause_ < kRetiresOnSaturation,
01035                        ".RetiresOnSaturation() cannot appear "
01036                        "more than once.");
01037     last_clause_ = kRetiresOnSaturation;
01038     retires_on_saturation_ = true;
01039 
01040     // Now that no more action clauses can be specified, we check
01041     // whether their count makes sense.
01042     CheckActionCountIfNotDone();
01043     return *this;
01044   }
01045 
01046   // Returns the matchers for the arguments as specified inside the
01047   // EXPECT_CALL() macro.
01048   const ArgumentMatcherTuple& matchers() const {
01049     return matchers_;
01050   }
01051 
01052   // Returns the matcher specified by the .With() clause.
01053   const Matcher<const ArgumentTuple&>& extra_matcher() const {
01054     return extra_matcher_;
01055   }
01056 
01057   // Returns the action specified by the .WillRepeatedly() clause.
01058   const Action<F>& repeated_action() const { return repeated_action_; }
01059 
01060   // If this mock method has an extra matcher (i.e. .With(matcher)),
01061   // describes it to the ostream.
01062   virtual void MaybeDescribeExtraMatcherTo(::std::ostream* os) {
01063     if (extra_matcher_specified_) {
01064       *os << "    Expected args: ";
01065       extra_matcher_.DescribeTo(os);
01066       *os << "\n";
01067     }
01068   }
01069 
01070  private:
01071   template <typename Function>
01072   friend class FunctionMockerBase;
01073 
01074   // Returns an Expectation object that references and co-owns this
01075   // expectation.
01076   virtual Expectation GetHandle() {
01077     return owner_->GetHandleOf(this);
01078   }
01079 
01080   // The following methods will be called only after the EXPECT_CALL()
01081   // statement finishes and when the current thread holds
01082   // g_gmock_mutex.
01083 
01084   // Returns true iff this expectation matches the given arguments.
01085   bool Matches(const ArgumentTuple& args) const
01086       GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
01087     g_gmock_mutex.AssertHeld();
01088     return TupleMatches(matchers_, args) && extra_matcher_.Matches(args);
01089   }
01090 
01091   // Returns true iff this expectation should handle the given arguments.
01092   bool ShouldHandleArguments(const ArgumentTuple& args) const
01093       GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
01094     g_gmock_mutex.AssertHeld();
01095 
01096     // In case the action count wasn't checked when the expectation
01097     // was defined (e.g. if this expectation has no WillRepeatedly()
01098     // or RetiresOnSaturation() clause), we check it when the
01099     // expectation is used for the first time.
01100     CheckActionCountIfNotDone();
01101     return !is_retired() && AllPrerequisitesAreSatisfied() && Matches(args);
01102   }
01103 
01104   // Describes the result of matching the arguments against this
01105   // expectation to the given ostream.
01106   void ExplainMatchResultTo(
01107       const ArgumentTuple& args,
01108       ::std::ostream* os) const
01109           GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
01110     g_gmock_mutex.AssertHeld();
01111 
01112     if (is_retired()) {
01113       *os << "         Expected: the expectation is active\n"
01114           << "           Actual: it is retired\n";
01115     } else if (!Matches(args)) {
01116       if (!TupleMatches(matchers_, args)) {
01117         ExplainMatchFailureTupleTo(matchers_, args, os);
01118       }
01119       StringMatchResultListener listener;
01120       if (!extra_matcher_.MatchAndExplain(args, &listener)) {
01121         *os << "    Expected args: ";
01122         extra_matcher_.DescribeTo(os);
01123         *os << "\n           Actual: don't match";
01124 
01125         internal::PrintIfNotEmpty(listener.str(), os);
01126         *os << "\n";
01127       }
01128     } else if (!AllPrerequisitesAreSatisfied()) {
01129       *os << "         Expected: all pre-requisites are satisfied\n"
01130           << "           Actual: the following immediate pre-requisites "
01131           << "are not satisfied:\n";
01132       ExpectationSet unsatisfied_prereqs;
01133       FindUnsatisfiedPrerequisites(&unsatisfied_prereqs);
01134       int i = 0;
01135       for (ExpectationSet::const_iterator it = unsatisfied_prereqs.begin();
01136            it != unsatisfied_prereqs.end(); ++it) {
01137         it->expectation_base()->DescribeLocationTo(os);
01138         *os << "pre-requisite #" << i++ << "\n";
01139       }
01140       *os << "                   (end of pre-requisites)\n";
01141     } else {
01142       // This line is here just for completeness' sake.  It will never
01143       // be executed as currently the ExplainMatchResultTo() function
01144       // is called only when the mock function call does NOT match the
01145       // expectation.
01146       *os << "The call matches the expectation.\n";
01147     }
01148   }
01149 
01150   // Returns the action that should be taken for the current invocation.
01151   const Action<F>& GetCurrentAction(
01152       const FunctionMockerBase<F>* mocker,
01153       const ArgumentTuple& args) const
01154           GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
01155     g_gmock_mutex.AssertHeld();
01156     const int count = call_count();
01157     Assert(count >= 1, __FILE__, __LINE__,
01158            "call_count() is <= 0 when GetCurrentAction() is "
01159            "called - this should never happen.");
01160 
01161     const int action_count = static_cast<int>(untyped_actions_.size());
01162     if (action_count > 0 && !repeated_action_specified_ &&
01163         count > action_count) {
01164       // If there is at least one WillOnce() and no WillRepeatedly(),
01165       // we warn the user when the WillOnce() clauses ran out.
01166       ::std::stringstream ss;
01167       DescribeLocationTo(&ss);
01168       ss << "Actions ran out in " << source_text() << "...\n"
01169          << "Called " << count << " times, but only "
01170          << action_count << " WillOnce()"
01171          << (action_count == 1 ? " is" : "s are") << " specified - ";
01172       mocker->DescribeDefaultActionTo(args, &ss);
01173       Log(kWarning, ss.str(), 1);
01174     }
01175 
01176     return count <= action_count ?
01177         *static_cast<const Action<F>*>(untyped_actions_[count - 1]) :
01178         repeated_action();
01179   }
01180 
01181   // Given the arguments of a mock function call, if the call will
01182   // over-saturate this expectation, returns the default action;
01183   // otherwise, returns the next action in this expectation.  Also
01184   // describes *what* happened to 'what', and explains *why* Google
01185   // Mock does it to 'why'.  This method is not const as it calls
01186   // IncrementCallCount().  A return value of NULL means the default
01187   // action.
01188   const Action<F>* GetActionForArguments(
01189       const FunctionMockerBase<F>* mocker,
01190       const ArgumentTuple& args,
01191       ::std::ostream* what,
01192       ::std::ostream* why)
01193           GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
01194     g_gmock_mutex.AssertHeld();
01195     if (IsSaturated()) {
01196       // We have an excessive call.
01197       IncrementCallCount();
01198       *what << "Mock function called more times than expected - ";
01199       mocker->DescribeDefaultActionTo(args, what);
01200       DescribeCallCountTo(why);
01201 
01202       // TODO(wan@google.com): allow the user to control whether
01203       // unexpected calls should fail immediately or continue using a
01204       // flag --gmock_unexpected_calls_are_fatal.
01205       return NULL;
01206     }
01207 
01208     IncrementCallCount();
01209     RetireAllPreRequisites();
01210 
01211     if (retires_on_saturation_ && IsSaturated()) {
01212       Retire();
01213     }
01214 
01215     // Must be done after IncrementCount()!
01216     *what << "Mock function call matches " << source_text() <<"...\n";
01217     return &(GetCurrentAction(mocker, args));
01218   }
01219 
01220   // All the fields below won't change once the EXPECT_CALL()
01221   // statement finishes.
01222   FunctionMockerBase<F>* const owner_;
01223   ArgumentMatcherTuple matchers_;
01224   Matcher<const ArgumentTuple&> extra_matcher_;
01225   Action<F> repeated_action_;
01226 
01227   GTEST_DISALLOW_COPY_AND_ASSIGN_(TypedExpectation);
01228 };  // class TypedExpectation
01229 
01230 // A MockSpec object is used by ON_CALL() or EXPECT_CALL() for
01231 // specifying the default behavior of, or expectation on, a mock
01232 // function.
01233 
01234 // Note: class MockSpec really belongs to the ::testing namespace.
01235 // However if we define it in ::testing, MSVC will complain when
01236 // classes in ::testing::internal declare it as a friend class
01237 // template.  To workaround this compiler bug, we define MockSpec in
01238 // ::testing::internal and import it into ::testing.
01239 
01240 // Logs a message including file and line number information.
01241 GTEST_API_ void LogWithLocation(testing::internal::LogSeverity severity,
01242                                 const char* file, int line,
01243                                 const string& message);
01244 
01245 template <typename F>
01246 class MockSpec {
01247  public:
01248   typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
01249   typedef typename internal::Function<F>::ArgumentMatcherTuple
01250       ArgumentMatcherTuple;
01251 
01252   // Constructs a MockSpec object, given the function mocker object
01253   // that the spec is associated with.
01254   explicit MockSpec(internal::FunctionMockerBase<F>* function_mocker)
01255       : function_mocker_(function_mocker) {}
01256 
01257   // Adds a new default action spec to the function mocker and returns
01258   // the newly created spec.
01259   internal::OnCallSpec<F>& InternalDefaultActionSetAt(
01260       const char* file, int line, const char* obj, const char* call) {
01261     LogWithLocation(internal::kInfo, file, line,
01262         string("ON_CALL(") + obj + ", " + call + ") invoked");
01263     return function_mocker_->AddNewOnCallSpec(file, line, matchers_);
01264   }
01265 
01266   // Adds a new expectation spec to the function mocker and returns
01267   // the newly created spec.
01268   internal::TypedExpectation<F>& InternalExpectedAt(
01269       const char* file, int line, const char* obj, const char* call) {
01270     const string source_text(string("EXPECT_CALL(") + obj + ", " + call + ")");
01271     LogWithLocation(internal::kInfo, file, line, source_text + " invoked");
01272     return function_mocker_->AddNewExpectation(
01273         file, line, source_text, matchers_);
01274   }
01275 
01276  private:
01277   template <typename Function>
01278   friend class internal::FunctionMocker;
01279 
01280   void SetMatchers(const ArgumentMatcherTuple& matchers) {
01281     matchers_ = matchers;
01282   }
01283 
01284   // The function mocker that owns this spec.
01285   internal::FunctionMockerBase<F>* const function_mocker_;
01286   // The argument matchers specified in the spec.
01287   ArgumentMatcherTuple matchers_;
01288 
01289   GTEST_DISALLOW_ASSIGN_(MockSpec);
01290 };  // class MockSpec
01291 
01292 // MSVC warns about using 'this' in base member initializer list, so
01293 // we need to temporarily disable the warning.  We have to do it for
01294 // the entire class to suppress the warning, even though it's about
01295 // the constructor only.
01296 
01297 #ifdef _MSC_VER
01298 # pragma warning(push)          // Saves the current warning state.
01299 # pragma warning(disable:4355)  // Temporarily disables warning 4355.
01300 #endif  // _MSV_VER
01301 
01302 // C++ treats the void type specially.  For example, you cannot define
01303 // a void-typed variable or pass a void value to a function.
01304 // ActionResultHolder<T> holds a value of type T, where T must be a
01305 // copyable type or void (T doesn't need to be default-constructable).
01306 // It hides the syntactic difference between void and other types, and
01307 // is used to unify the code for invoking both void-returning and
01308 // non-void-returning mock functions.
01309 
01310 // Untyped base class for ActionResultHolder<T>.
01311 class UntypedActionResultHolderBase {
01312  public:
01313   virtual ~UntypedActionResultHolderBase() {}
01314 
01315   // Prints the held value as an action's result to os.
01316   virtual void PrintAsActionResult(::std::ostream* os) const = 0;
01317 };
01318 
01319 // This generic definition is used when T is not void.
01320 template <typename T>
01321 class ActionResultHolder : public UntypedActionResultHolderBase {
01322  public:
01323   explicit ActionResultHolder(T a_value) : value_(a_value) {}
01324 
01325   // The compiler-generated copy constructor and assignment operator
01326   // are exactly what we need, so we don't need to define them.
01327 
01328   // Returns the held value and deletes this object.
01329   T GetValueAndDelete() const {
01330     T retval(value_);
01331     delete this;
01332     return retval;
01333   }
01334 
01335   // Prints the held value as an action's result to os.
01336   virtual void PrintAsActionResult(::std::ostream* os) const {
01337     *os << "\n          Returns: ";
01338     // T may be a reference type, so we don't use UniversalPrint().
01339     UniversalPrinter<T>::Print(value_, os);
01340   }
01341 
01342   // Performs the given mock function's default action and returns the
01343   // result in a new-ed ActionResultHolder.
01344   template <typename F>
01345   static ActionResultHolder* PerformDefaultAction(
01346       const FunctionMockerBase<F>* func_mocker,
01347       const typename Function<F>::ArgumentTuple& args,
01348       const string& call_description) {
01349     return new ActionResultHolder(
01350         func_mocker->PerformDefaultAction(args, call_description));
01351   }
01352 
01353   // Performs the given action and returns the result in a new-ed
01354   // ActionResultHolder.
01355   template <typename F>
01356   static ActionResultHolder*
01357   PerformAction(const Action<F>& action,
01358                 const typename Function<F>::ArgumentTuple& args) {
01359     return new ActionResultHolder(action.Perform(args));
01360   }
01361 
01362  private:
01363   T value_;
01364 
01365   // T could be a reference type, so = isn't supported.
01366   GTEST_DISALLOW_ASSIGN_(ActionResultHolder);
01367 };
01368 
01369 // Specialization for T = void.
01370 template <>
01371 class ActionResultHolder<void> : public UntypedActionResultHolderBase {
01372  public:
01373   void GetValueAndDelete() const { delete this; }
01374 
01375   virtual void PrintAsActionResult(::std::ostream* /* os */) const {}
01376 
01377   // Performs the given mock function's default action and returns NULL;
01378   template <typename F>
01379   static ActionResultHolder* PerformDefaultAction(
01380       const FunctionMockerBase<F>* func_mocker,
01381       const typename Function<F>::ArgumentTuple& args,
01382       const string& call_description) {
01383     func_mocker->PerformDefaultAction(args, call_description);
01384     return NULL;
01385   }
01386 
01387   // Performs the given action and returns NULL.
01388   template <typename F>
01389   static ActionResultHolder* PerformAction(
01390       const Action<F>& action,
01391       const typename Function<F>::ArgumentTuple& args) {
01392     action.Perform(args);
01393     return NULL;
01394   }
01395 };
01396 
01397 // The base of the function mocker class for the given function type.
01398 // We put the methods in this class instead of its child to avoid code
01399 // bloat.
01400 template <typename F>
01401 class FunctionMockerBase : public UntypedFunctionMockerBase {
01402  public:
01403   typedef typename Function<F>::Result Result;
01404   typedef typename Function<F>::ArgumentTuple ArgumentTuple;
01405   typedef typename Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple;
01406 
01407   FunctionMockerBase() : current_spec_(this) {}
01408 
01409   // The destructor verifies that all expectations on this mock
01410   // function have been satisfied.  If not, it will report Google Test
01411   // non-fatal failures for the violations.
01412   virtual ~FunctionMockerBase()
01413         GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
01414     MutexLock l(&g_gmock_mutex);
01415     VerifyAndClearExpectationsLocked();
01416     Mock::UnregisterLocked(this);
01417     ClearDefaultActionsLocked();
01418   }
01419 
01420   // Returns the ON_CALL spec that matches this mock function with the
01421   // given arguments; returns NULL if no matching ON_CALL is found.
01422   // L = *
01423   const OnCallSpec<F>* FindOnCallSpec(
01424       const ArgumentTuple& args) const {
01425     for (UntypedOnCallSpecs::const_reverse_iterator it
01426              = untyped_on_call_specs_.rbegin();
01427          it != untyped_on_call_specs_.rend(); ++it) {
01428       const OnCallSpec<F>* spec = static_cast<const OnCallSpec<F>*>(*it);
01429       if (spec->Matches(args))
01430         return spec;
01431     }
01432 
01433     return NULL;
01434   }
01435 
01436   // Performs the default action of this mock function on the given
01437   // arguments and returns the result. Asserts (or throws if
01438   // exceptions are enabled) with a helpful call descrption if there
01439   // is no valid return value. This method doesn't depend on the
01440   // mutable state of this object, and thus can be called concurrently
01441   // without locking.
01442   // L = *
01443   Result PerformDefaultAction(const ArgumentTuple& args,
01444                               const string& call_description) const {
01445     const OnCallSpec<F>* const spec =
01446         this->FindOnCallSpec(args);
01447     if (spec != NULL) {
01448       return spec->GetAction().Perform(args);
01449     }
01450     const string message = call_description +
01451         "\n    The mock function has no default action "
01452         "set, and its return type has no default value set.";
01453 #if GTEST_HAS_EXCEPTIONS
01454     if (!DefaultValue<Result>::Exists()) {
01455       throw std::runtime_error(message);
01456     }
01457 #else
01458     Assert(DefaultValue<Result>::Exists(), "", -1, message);
01459 #endif
01460     return DefaultValue<Result>::Get();
01461   }
01462 
01463   // Performs the default action with the given arguments and returns
01464   // the action's result.  The call description string will be used in
01465   // the error message to describe the call in the case the default
01466   // action fails.  The caller is responsible for deleting the result.
01467   // L = *
01468   virtual UntypedActionResultHolderBase* UntypedPerformDefaultAction(
01469       const void* untyped_args,  // must point to an ArgumentTuple
01470       const string& call_description) const {
01471     const ArgumentTuple& args =
01472         *static_cast<const ArgumentTuple*>(untyped_args);
01473     return ResultHolder::PerformDefaultAction(this, args, call_description);
01474   }
01475 
01476   // Performs the given action with the given arguments and returns
01477   // the action's result.  The caller is responsible for deleting the
01478   // result.
01479   // L = *
01480   virtual UntypedActionResultHolderBase* UntypedPerformAction(
01481       const void* untyped_action, const void* untyped_args) const {
01482     // Make a copy of the action before performing it, in case the
01483     // action deletes the mock object (and thus deletes itself).
01484     const Action<F> action = *static_cast<const Action<F>*>(untyped_action);
01485     const ArgumentTuple& args =
01486         *static_cast<const ArgumentTuple*>(untyped_args);
01487     return ResultHolder::PerformAction(action, args);
01488   }
01489 
01490   // Implements UntypedFunctionMockerBase::ClearDefaultActionsLocked():
01491   // clears the ON_CALL()s set on this mock function.
01492   virtual void ClearDefaultActionsLocked()
01493       GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
01494     g_gmock_mutex.AssertHeld();
01495 
01496     // Deleting our default actions may trigger other mock objects to be
01497     // deleted, for example if an action contains a reference counted smart
01498     // pointer to that mock object, and that is the last reference. So if we
01499     // delete our actions within the context of the global mutex we may deadlock
01500     // when this method is called again. Instead, make a copy of the set of
01501     // actions to delete, clear our set within the mutex, and then delete the
01502     // actions outside of the mutex.
01503     UntypedOnCallSpecs specs_to_delete;
01504     untyped_on_call_specs_.swap(specs_to_delete);
01505 
01506     g_gmock_mutex.Unlock();
01507     for (UntypedOnCallSpecs::const_iterator it =
01508              specs_to_delete.begin();
01509          it != specs_to_delete.end(); ++it) {
01510       delete static_cast<const OnCallSpec<F>*>(*it);
01511     }
01512 
01513     // Lock the mutex again, since the caller expects it to be locked when we
01514     // return.
01515     g_gmock_mutex.Lock();
01516   }
01517 
01518  protected:
01519   template <typename Function>
01520   friend class MockSpec;
01521 
01522   typedef ActionResultHolder<Result> ResultHolder;
01523 
01524   // Returns the result of invoking this mock function with the given
01525   // arguments.  This function can be safely called from multiple
01526   // threads concurrently.
01527   Result InvokeWith(const ArgumentTuple& args)
01528         GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
01529     return static_cast<const ResultHolder*>(
01530         this->UntypedInvokeWith(&args))->GetValueAndDelete();
01531   }
01532 
01533   // Adds and returns a default action spec for this mock function.
01534   OnCallSpec<F>& AddNewOnCallSpec(
01535       const char* file, int line,
01536       const ArgumentMatcherTuple& m)
01537           GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
01538     Mock::RegisterUseByOnCallOrExpectCall(MockObject(), file, line);
01539     OnCallSpec<F>* const on_call_spec = new OnCallSpec<F>(file, line, m);
01540     untyped_on_call_specs_.push_back(on_call_spec);
01541     return *on_call_spec;
01542   }
01543 
01544   // Adds and returns an expectation spec for this mock function.
01545   TypedExpectation<F>& AddNewExpectation(
01546       const char* file,
01547       int line,
01548       const string& source_text,
01549       const ArgumentMatcherTuple& m)
01550           GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
01551     Mock::RegisterUseByOnCallOrExpectCall(MockObject(), file, line);
01552     TypedExpectation<F>* const expectation =
01553         new TypedExpectation<F>(this, file, line, source_text, m);
01554     const linked_ptr<ExpectationBase> untyped_expectation(expectation);
01555     untyped_expectations_.push_back(untyped_expectation);
01556 
01557     // Adds this expectation into the implicit sequence if there is one.
01558     Sequence* const implicit_sequence = g_gmock_implicit_sequence.get();
01559     if (implicit_sequence != NULL) {
01560       implicit_sequence->AddExpectation(Expectation(untyped_expectation));
01561     }
01562 
01563     return *expectation;
01564   }
01565 
01566   // The current spec (either default action spec or expectation spec)
01567   // being described on this function mocker.
01568   MockSpec<F>& current_spec() { return current_spec_; }
01569 
01570  private:
01571   template <typename Func> friend class TypedExpectation;
01572 
01573   // Some utilities needed for implementing UntypedInvokeWith().
01574 
01575   // Describes what default action will be performed for the given
01576   // arguments.
01577   // L = *
01578   void DescribeDefaultActionTo(const ArgumentTuple& args,
01579                                ::std::ostream* os) const {
01580     const OnCallSpec<F>* const spec = FindOnCallSpec(args);
01581 
01582     if (spec == NULL) {
01583       *os << (internal::type_equals<Result, void>::value ?
01584               "returning directly.\n" :
01585               "returning default value.\n");
01586     } else {
01587       *os << "taking default action specified at:\n"
01588           << FormatFileLocation(spec->file(), spec->line()) << "\n";
01589     }
01590   }
01591 
01592   // Writes a message that the call is uninteresting (i.e. neither
01593   // explicitly expected nor explicitly unexpected) to the given
01594   // ostream.
01595   virtual void UntypedDescribeUninterestingCall(
01596       const void* untyped_args,
01597       ::std::ostream* os) const
01598           GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
01599     const ArgumentTuple& args =
01600         *static_cast<const ArgumentTuple*>(untyped_args);
01601     *os << "Uninteresting mock function call - ";
01602     DescribeDefaultActionTo(args, os);
01603     *os << "    Function call: " << Name();
01604     UniversalPrint(args, os);
01605   }
01606 
01607   // Returns the expectation that matches the given function arguments
01608   // (or NULL is there's no match); when a match is found,
01609   // untyped_action is set to point to the action that should be
01610   // performed (or NULL if the action is "do default"), and
01611   // is_excessive is modified to indicate whether the call exceeds the
01612   // expected number.
01613   //
01614   // Critical section: We must find the matching expectation and the
01615   // corresponding action that needs to be taken in an ATOMIC
01616   // transaction.  Otherwise another thread may call this mock
01617   // method in the middle and mess up the state.
01618   //
01619   // However, performing the action has to be left out of the critical
01620   // section.  The reason is that we have no control on what the
01621   // action does (it can invoke an arbitrary user function or even a
01622   // mock function) and excessive locking could cause a dead lock.
01623   virtual const ExpectationBase* UntypedFindMatchingExpectation(
01624       const void* untyped_args,
01625       const void** untyped_action, bool* is_excessive,
01626       ::std::ostream* what, ::std::ostream* why)
01627           GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
01628     const ArgumentTuple& args =
01629         *static_cast<const ArgumentTuple*>(untyped_args);
01630     MutexLock l(&g_gmock_mutex);
01631     TypedExpectation<F>* exp = this->FindMatchingExpectationLocked(args);
01632     if (exp == NULL) {  // A match wasn't found.
01633       this->FormatUnexpectedCallMessageLocked(args, what, why);
01634       return NULL;
01635     }
01636 
01637     // This line must be done before calling GetActionForArguments(),
01638     // which will increment the call count for *exp and thus affect
01639     // its saturation status.
01640     *is_excessive = exp->IsSaturated();
01641     const Action<F>* action = exp->GetActionForArguments(this, args, what, why);
01642     if (action != NULL && action->IsDoDefault())
01643       action = NULL;  // Normalize "do default" to NULL.
01644     *untyped_action = action;
01645     return exp;
01646   }
01647 
01648   // Prints the given function arguments to the ostream.
01649   virtual void UntypedPrintArgs(const void* untyped_args,
01650                                 ::std::ostream* os) const {
01651     const ArgumentTuple& args =
01652         *static_cast<const ArgumentTuple*>(untyped_args);
01653     UniversalPrint(args, os);
01654   }
01655 
01656   // Returns the expectation that matches the arguments, or NULL if no
01657   // expectation matches them.
01658   TypedExpectation<F>* FindMatchingExpectationLocked(
01659       const ArgumentTuple& args) const
01660           GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
01661     g_gmock_mutex.AssertHeld();
01662     for (typename UntypedExpectations::const_reverse_iterator it =
01663              untyped_expectations_.rbegin();
01664          it != untyped_expectations_.rend(); ++it) {
01665       TypedExpectation<F>* const exp =
01666           static_cast<TypedExpectation<F>*>(it->get());
01667       if (exp->ShouldHandleArguments(args)) {
01668         return exp;
01669       }
01670     }
01671     return NULL;
01672   }
01673 
01674   // Returns a message that the arguments don't match any expectation.
01675   void FormatUnexpectedCallMessageLocked(
01676       const ArgumentTuple& args,
01677       ::std::ostream* os,
01678       ::std::ostream* why) const
01679           GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
01680     g_gmock_mutex.AssertHeld();
01681     *os << "\nUnexpected mock function call - ";
01682     DescribeDefaultActionTo(args, os);
01683     PrintTriedExpectationsLocked(args, why);
01684   }
01685 
01686   // Prints a list of expectations that have been tried against the
01687   // current mock function call.
01688   void PrintTriedExpectationsLocked(
01689       const ArgumentTuple& args,
01690       ::std::ostream* why) const
01691           GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
01692     g_gmock_mutex.AssertHeld();
01693     const int count = static_cast<int>(untyped_expectations_.size());
01694     *why << "Google Mock tried the following " << count << " "
01695          << (count == 1 ? "expectation, but it didn't match" :
01696              "expectations, but none matched")
01697          << ":\n";
01698     for (int i = 0; i < count; i++) {
01699       TypedExpectation<F>* const expectation =
01700           static_cast<TypedExpectation<F>*>(untyped_expectations_[i].get());
01701       *why << "\n";
01702       expectation->DescribeLocationTo(why);
01703       if (count > 1) {
01704         *why << "tried expectation #" << i << ": ";
01705       }
01706       *why << expectation->source_text() << "...\n";
01707       expectation->ExplainMatchResultTo(args, why);
01708       expectation->DescribeCallCountTo(why);
01709     }
01710   }
01711 
01712   // The current spec (either default action spec or expectation spec)
01713   // being described on this function mocker.
01714   MockSpec<F> current_spec_;
01715 
01716   // There is no generally useful and implementable semantics of
01717   // copying a mock object, so copying a mock is usually a user error.
01718   // Thus we disallow copying function mockers.  If the user really
01719   // wants to copy a mock object, he should implement his own copy
01720   // operation, for example:
01721   //
01722   //   class MockFoo : public Foo {
01723   //    public:
01724   //     // Defines a copy constructor explicitly.
01725   //     MockFoo(const MockFoo& src) {}
01726   //     ...
01727   //   };
01728   GTEST_DISALLOW_COPY_AND_ASSIGN_(FunctionMockerBase);
01729 };  // class FunctionMockerBase
01730 
01731 #ifdef _MSC_VER
01732 # pragma warning(pop)  // Restores the warning state.
01733 #endif  // _MSV_VER
01734 
01735 // Implements methods of FunctionMockerBase.
01736 
01737 // Verifies that all expectations on this mock function have been
01738 // satisfied.  Reports one or more Google Test non-fatal failures and
01739 // returns false if not.
01740 
01741 // Reports an uninteresting call (whose description is in msg) in the
01742 // manner specified by 'reaction'.
01743 void ReportUninterestingCall(CallReaction reaction, const string& msg);
01744 
01745 }  // namespace internal
01746 
01747 // The style guide prohibits "using" statements in a namespace scope
01748 // inside a header file.  However, the MockSpec class template is
01749 // meant to be defined in the ::testing namespace.  The following line
01750 // is just a trick for working around a bug in MSVC 8.0, which cannot
01751 // handle it if we define MockSpec in ::testing.
01752 using internal::MockSpec;
01753 
01754 // Const(x) is a convenient function for obtaining a const reference
01755 // to x.  This is useful for setting expectations on an overloaded
01756 // const mock method, e.g.
01757 //
01758 //   class MockFoo : public FooInterface {
01759 //    public:
01760 //     MOCK_METHOD0(Bar, int());
01761 //     MOCK_CONST_METHOD0(Bar, int&());
01762 //   };
01763 //
01764 //   MockFoo foo;
01765 //   // Expects a call to non-const MockFoo::Bar().
01766 //   EXPECT_CALL(foo, Bar());
01767 //   // Expects a call to const MockFoo::Bar().
01768 //   EXPECT_CALL(Const(foo), Bar());
01769 template <typename T>
01770 inline const T& Const(const T& x) { return x; }
01771 
01772 // Constructs an Expectation object that references and co-owns exp.
01773 inline Expectation::Expectation(internal::ExpectationBase& exp)  // NOLINT
01774     : expectation_base_(exp.GetHandle().expectation_base()) {}
01775 
01776 }  // namespace testing
01777 
01778 // A separate macro is required to avoid compile errors when the name
01779 // of the method used in call is a result of macro expansion.
01780 // See CompilesWithMethodNameExpandedFromMacro tests in
01781 // internal/gmock-spec-builders_test.cc for more details.
01782 #define GMOCK_ON_CALL_IMPL_(obj, call) \
01783     ((obj).gmock_##call).InternalDefaultActionSetAt(__FILE__, __LINE__, \
01784                                                     #obj, #call)
01785 #define ON_CALL(obj, call) GMOCK_ON_CALL_IMPL_(obj, call)
01786 
01787 #define GMOCK_EXPECT_CALL_IMPL_(obj, call) \
01788     ((obj).gmock_##call).InternalExpectedAt(__FILE__, __LINE__, #obj, #call)
01789 #define EXPECT_CALL(obj, call) GMOCK_EXPECT_CALL_IMPL_(obj, call)
01790 
01791 #endif  // GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_


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