Blame googlemock/include/gmock/gmock-spec-builders.h

Packit bd1cd8
// Copyright 2007, Google Inc.
Packit bd1cd8
// All rights reserved.
Packit bd1cd8
//
Packit bd1cd8
// Redistribution and use in source and binary forms, with or without
Packit bd1cd8
// modification, are permitted provided that the following conditions are
Packit bd1cd8
// met:
Packit bd1cd8
//
Packit bd1cd8
//     * Redistributions of source code must retain the above copyright
Packit bd1cd8
// notice, this list of conditions and the following disclaimer.
Packit bd1cd8
//     * Redistributions in binary form must reproduce the above
Packit bd1cd8
// copyright notice, this list of conditions and the following disclaimer
Packit bd1cd8
// in the documentation and/or other materials provided with the
Packit bd1cd8
// distribution.
Packit bd1cd8
//     * Neither the name of Google Inc. nor the names of its
Packit bd1cd8
// contributors may be used to endorse or promote products derived from
Packit bd1cd8
// this software without specific prior written permission.
Packit bd1cd8
//
Packit bd1cd8
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit bd1cd8
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit bd1cd8
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit bd1cd8
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit bd1cd8
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit bd1cd8
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit bd1cd8
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit bd1cd8
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit bd1cd8
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit bd1cd8
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit bd1cd8
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit bd1cd8
//
Packit bd1cd8
// Author: wan@google.com (Zhanyong Wan)
Packit bd1cd8
Packit bd1cd8
// Google Mock - a framework for writing C++ mock classes.
Packit bd1cd8
//
Packit bd1cd8
// This file implements the ON_CALL() and EXPECT_CALL() macros.
Packit bd1cd8
//
Packit bd1cd8
// A user can use the ON_CALL() macro to specify the default action of
Packit bd1cd8
// a mock method.  The syntax is:
Packit bd1cd8
//
Packit bd1cd8
//   ON_CALL(mock_object, Method(argument-matchers))
Packit bd1cd8
//       .With(multi-argument-matcher)
Packit bd1cd8
//       .WillByDefault(action);
Packit bd1cd8
//
Packit bd1cd8
//  where the .With() clause is optional.
Packit bd1cd8
//
Packit bd1cd8
// A user can use the EXPECT_CALL() macro to specify an expectation on
Packit bd1cd8
// a mock method.  The syntax is:
Packit bd1cd8
//
Packit bd1cd8
//   EXPECT_CALL(mock_object, Method(argument-matchers))
Packit bd1cd8
//       .With(multi-argument-matchers)
Packit bd1cd8
//       .Times(cardinality)
Packit bd1cd8
//       .InSequence(sequences)
Packit bd1cd8
//       .After(expectations)
Packit bd1cd8
//       .WillOnce(action)
Packit bd1cd8
//       .WillRepeatedly(action)
Packit bd1cd8
//       .RetiresOnSaturation();
Packit bd1cd8
//
Packit bd1cd8
// where all clauses are optional, and .InSequence()/.After()/
Packit bd1cd8
// .WillOnce() can appear any number of times.
Packit bd1cd8
Packit bd1cd8
#ifndef GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_
Packit bd1cd8
#define GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_
Packit bd1cd8
Packit bd1cd8
#include <map>
Packit bd1cd8
#include <set>
Packit bd1cd8
#include <sstream>
Packit bd1cd8
#include <string>
Packit bd1cd8
#include <vector>
Packit bd1cd8
Packit bd1cd8
#if GTEST_HAS_EXCEPTIONS
Packit bd1cd8
# include <stdexcept>  // NOLINT
Packit bd1cd8
#endif
Packit bd1cd8
Packit bd1cd8
#include "gmock/gmock-actions.h"
Packit bd1cd8
#include "gmock/gmock-cardinalities.h"
Packit bd1cd8
#include "gmock/gmock-matchers.h"
Packit bd1cd8
#include "gmock/internal/gmock-internal-utils.h"
Packit bd1cd8
#include "gmock/internal/gmock-port.h"
Packit bd1cd8
#include "gtest/gtest.h"
Packit bd1cd8
Packit bd1cd8
namespace testing {
Packit bd1cd8
Packit bd1cd8
// An abstract handle of an expectation.
Packit bd1cd8
class Expectation;
Packit bd1cd8
Packit bd1cd8
// A set of expectation handles.
Packit bd1cd8
class ExpectationSet;
Packit bd1cd8
Packit bd1cd8
// Anything inside the 'internal' namespace IS INTERNAL IMPLEMENTATION
Packit bd1cd8
// and MUST NOT BE USED IN USER CODE!!!
Packit bd1cd8
namespace internal {
Packit bd1cd8
Packit bd1cd8
// Implements a mock function.
Packit bd1cd8
template <typename F> class FunctionMocker;
Packit bd1cd8
Packit bd1cd8
// Base class for expectations.
Packit bd1cd8
class ExpectationBase;
Packit bd1cd8
Packit bd1cd8
// Implements an expectation.
Packit bd1cd8
template <typename F> class TypedExpectation;
Packit bd1cd8
Packit bd1cd8
// Helper class for testing the Expectation class template.
Packit bd1cd8
class ExpectationTester;
Packit bd1cd8
Packit bd1cd8
// Base class for function mockers.
Packit bd1cd8
template <typename F> class FunctionMockerBase;
Packit bd1cd8
Packit bd1cd8
// Protects the mock object registry (in class Mock), all function
Packit bd1cd8
// mockers, and all expectations.
Packit bd1cd8
//
Packit bd1cd8
// The reason we don't use more fine-grained protection is: when a
Packit bd1cd8
// mock function Foo() is called, it needs to consult its expectations
Packit bd1cd8
// to see which one should be picked.  If another thread is allowed to
Packit bd1cd8
// call a mock function (either Foo() or a different one) at the same
Packit bd1cd8
// time, it could affect the "retired" attributes of Foo()'s
Packit bd1cd8
// expectations when InSequence() is used, and thus affect which
Packit bd1cd8
// expectation gets picked.  Therefore, we sequence all mock function
Packit bd1cd8
// calls to ensure the integrity of the mock objects' states.
Packit bd1cd8
GTEST_API_ GTEST_DECLARE_STATIC_MUTEX_(g_gmock_mutex);
Packit bd1cd8
Packit bd1cd8
// Untyped base class for ActionResultHolder<R>.
Packit bd1cd8
class UntypedActionResultHolderBase;
Packit bd1cd8
Packit bd1cd8
// Abstract base class of FunctionMockerBase.  This is the
Packit bd1cd8
// type-agnostic part of the function mocker interface.  Its pure
Packit bd1cd8
// virtual methods are implemented by FunctionMockerBase.
Packit bd1cd8
class GTEST_API_ UntypedFunctionMockerBase {
Packit bd1cd8
 public:
Packit bd1cd8
  UntypedFunctionMockerBase();
Packit bd1cd8
  virtual ~UntypedFunctionMockerBase();
Packit bd1cd8
Packit bd1cd8
  // Verifies that all expectations on this mock function have been
Packit bd1cd8
  // satisfied.  Reports one or more Google Test non-fatal failures
Packit bd1cd8
  // and returns false if not.
Packit bd1cd8
  bool VerifyAndClearExpectationsLocked()
Packit bd1cd8
      GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex);
Packit bd1cd8
Packit bd1cd8
  // Clears the ON_CALL()s set on this mock function.
Packit bd1cd8
  virtual void ClearDefaultActionsLocked()
Packit bd1cd8
      GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) = 0;
Packit bd1cd8
Packit bd1cd8
  // In all of the following Untyped* functions, it's the caller's
Packit bd1cd8
  // responsibility to guarantee the correctness of the arguments'
Packit bd1cd8
  // types.
Packit bd1cd8
Packit bd1cd8
  // Performs the default action with the given arguments and returns
Packit bd1cd8
  // the action's result.  The call description string will be used in
Packit bd1cd8
  // the error message to describe the call in the case the default
Packit bd1cd8
  // action fails.
Packit bd1cd8
  // L = *
Packit bd1cd8
  virtual UntypedActionResultHolderBase* UntypedPerformDefaultAction(
Packit bd1cd8
      const void* untyped_args,
Packit bd1cd8
      const string& call_description) const = 0;
Packit bd1cd8
Packit bd1cd8
  // Performs the given action with the given arguments and returns
Packit bd1cd8
  // the action's result.
Packit bd1cd8
  // L = *
Packit bd1cd8
  virtual UntypedActionResultHolderBase* UntypedPerformAction(
Packit bd1cd8
      const void* untyped_action,
Packit bd1cd8
      const void* untyped_args) const = 0;
Packit bd1cd8
Packit bd1cd8
  // Writes a message that the call is uninteresting (i.e. neither
Packit bd1cd8
  // explicitly expected nor explicitly unexpected) to the given
Packit bd1cd8
  // ostream.
Packit bd1cd8
  virtual void UntypedDescribeUninterestingCall(
Packit bd1cd8
      const void* untyped_args,
Packit bd1cd8
      ::std::ostream* os) const
Packit bd1cd8
          GTEST_LOCK_EXCLUDED_(g_gmock_mutex) = 0;
Packit bd1cd8
Packit bd1cd8
  // Returns the expectation that matches the given function arguments
Packit bd1cd8
  // (or NULL is there's no match); when a match is found,
Packit bd1cd8
  // untyped_action is set to point to the action that should be
Packit bd1cd8
  // performed (or NULL if the action is "do default"), and
Packit bd1cd8
  // is_excessive is modified to indicate whether the call exceeds the
Packit bd1cd8
  // expected number.
Packit bd1cd8
  virtual const ExpectationBase* UntypedFindMatchingExpectation(
Packit bd1cd8
      const void* untyped_args,
Packit bd1cd8
      const void** untyped_action, bool* is_excessive,
Packit bd1cd8
      ::std::ostream* what, ::std::ostream* why)
Packit bd1cd8
          GTEST_LOCK_EXCLUDED_(g_gmock_mutex) = 0;
Packit bd1cd8
Packit bd1cd8
  // Prints the given function arguments to the ostream.
Packit bd1cd8
  virtual void UntypedPrintArgs(const void* untyped_args,
Packit bd1cd8
                                ::std::ostream* os) const = 0;
Packit bd1cd8
Packit bd1cd8
  // Sets the mock object this mock method belongs to, and registers
Packit bd1cd8
  // this information in the global mock registry.  Will be called
Packit bd1cd8
  // whenever an EXPECT_CALL() or ON_CALL() is executed on this mock
Packit bd1cd8
  // method.
Packit bd1cd8
  // TODO(wan@google.com): rename to SetAndRegisterOwner().
Packit bd1cd8
  void RegisterOwner(const void* mock_obj)
Packit bd1cd8
      GTEST_LOCK_EXCLUDED_(g_gmock_mutex);
Packit bd1cd8
Packit bd1cd8
  // Sets the mock object this mock method belongs to, and sets the
Packit bd1cd8
  // name of the mock function.  Will be called upon each invocation
Packit bd1cd8
  // of this mock function.
Packit bd1cd8
  void SetOwnerAndName(const void* mock_obj, const char* name)
Packit bd1cd8
      GTEST_LOCK_EXCLUDED_(g_gmock_mutex);
Packit bd1cd8
Packit bd1cd8
  // Returns the mock object this mock method belongs to.  Must be
Packit bd1cd8
  // called after RegisterOwner() or SetOwnerAndName() has been
Packit bd1cd8
  // called.
Packit bd1cd8
  const void* MockObject() const
Packit bd1cd8
      GTEST_LOCK_EXCLUDED_(g_gmock_mutex);
Packit bd1cd8
Packit bd1cd8
  // Returns the name of this mock method.  Must be called after
Packit bd1cd8
  // SetOwnerAndName() has been called.
Packit bd1cd8
  const char* Name() const
Packit bd1cd8
      GTEST_LOCK_EXCLUDED_(g_gmock_mutex);
Packit bd1cd8
Packit bd1cd8
  // Returns the result of invoking this mock function with the given
Packit bd1cd8
  // arguments.  This function can be safely called from multiple
Packit bd1cd8
  // threads concurrently.  The caller is responsible for deleting the
Packit bd1cd8
  // result.
Packit bd1cd8
  UntypedActionResultHolderBase* UntypedInvokeWith(
Packit bd1cd8
      const void* untyped_args)
Packit bd1cd8
          GTEST_LOCK_EXCLUDED_(g_gmock_mutex);
Packit bd1cd8
Packit bd1cd8
 protected:
Packit bd1cd8
  typedef std::vector<const void*> UntypedOnCallSpecs;
Packit bd1cd8
Packit bd1cd8
  typedef std::vector<internal::linked_ptr<ExpectationBase> >
Packit bd1cd8
  UntypedExpectations;
Packit bd1cd8
Packit bd1cd8
  // Returns an Expectation object that references and co-owns exp,
Packit bd1cd8
  // which must be an expectation on this mock function.
Packit bd1cd8
  Expectation GetHandleOf(ExpectationBase* exp);
Packit bd1cd8
Packit bd1cd8
  // Address of the mock object this mock method belongs to.  Only
Packit bd1cd8
  // valid after this mock method has been called or
Packit bd1cd8
  // ON_CALL/EXPECT_CALL has been invoked on it.
Packit bd1cd8
  const void* mock_obj_;  // Protected by g_gmock_mutex.
Packit bd1cd8
Packit bd1cd8
  // Name of the function being mocked.  Only valid after this mock
Packit bd1cd8
  // method has been called.
Packit bd1cd8
  const char* name_;  // Protected by g_gmock_mutex.
Packit bd1cd8
Packit bd1cd8
  // All default action specs for this function mocker.
Packit bd1cd8
  UntypedOnCallSpecs untyped_on_call_specs_;
Packit bd1cd8
Packit bd1cd8
  // All expectations for this function mocker.
Packit bd1cd8
  UntypedExpectations untyped_expectations_;
Packit bd1cd8
};  // class UntypedFunctionMockerBase
Packit bd1cd8
Packit bd1cd8
// Untyped base class for OnCallSpec<F>.
Packit bd1cd8
class UntypedOnCallSpecBase {
Packit bd1cd8
 public:
Packit bd1cd8
  // The arguments are the location of the ON_CALL() statement.
Packit bd1cd8
  UntypedOnCallSpecBase(const char* a_file, int a_line)
Packit bd1cd8
      : file_(a_file), line_(a_line), last_clause_(kNone) {}
Packit bd1cd8
Packit bd1cd8
  // Where in the source file was the default action spec defined?
Packit bd1cd8
  const char* file() const { return file_; }
Packit bd1cd8
  int line() const { return line_; }
Packit bd1cd8
Packit bd1cd8
 protected:
Packit bd1cd8
  // Gives each clause in the ON_CALL() statement a name.
Packit bd1cd8
  enum Clause {
Packit bd1cd8
    // Do not change the order of the enum members!  The run-time
Packit bd1cd8
    // syntax checking relies on it.
Packit bd1cd8
    kNone,
Packit bd1cd8
    kWith,
Packit bd1cd8
    kWillByDefault
Packit bd1cd8
  };
Packit bd1cd8
Packit bd1cd8
  // Asserts that the ON_CALL() statement has a certain property.
Packit bd1cd8
  void AssertSpecProperty(bool property, const string& failure_message) const {
Packit bd1cd8
    Assert(property, file_, line_, failure_message);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Expects that the ON_CALL() statement has a certain property.
Packit bd1cd8
  void ExpectSpecProperty(bool property, const string& failure_message) const {
Packit bd1cd8
    Expect(property, file_, line_, failure_message);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  const char* file_;
Packit bd1cd8
  int line_;
Packit bd1cd8
Packit bd1cd8
  // The last clause in the ON_CALL() statement as seen so far.
Packit bd1cd8
  // Initially kNone and changes as the statement is parsed.
Packit bd1cd8
  Clause last_clause_;
Packit bd1cd8
};  // class UntypedOnCallSpecBase
Packit bd1cd8
Packit bd1cd8
// This template class implements an ON_CALL spec.
Packit bd1cd8
template <typename F>
Packit bd1cd8
class OnCallSpec : public UntypedOnCallSpecBase {
Packit bd1cd8
 public:
Packit bd1cd8
  typedef typename Function<F>::ArgumentTuple ArgumentTuple;
Packit bd1cd8
  typedef typename Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple;
Packit bd1cd8
Packit bd1cd8
  // Constructs an OnCallSpec object from the information inside
Packit bd1cd8
  // the parenthesis of an ON_CALL() statement.
Packit bd1cd8
  OnCallSpec(const char* a_file, int a_line,
Packit bd1cd8
             const ArgumentMatcherTuple& matchers)
Packit bd1cd8
      : UntypedOnCallSpecBase(a_file, a_line),
Packit bd1cd8
        matchers_(matchers),
Packit bd1cd8
        // By default, extra_matcher_ should match anything.  However,
Packit bd1cd8
        // we cannot initialize it with _ as that triggers a compiler
Packit bd1cd8
        // bug in Symbian's C++ compiler (cannot decide between two
Packit bd1cd8
        // overloaded constructors of Matcher<const ArgumentTuple&>).
Packit bd1cd8
        extra_matcher_(A<const ArgumentTuple&>()) {
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Implements the .With() clause.
Packit bd1cd8
  OnCallSpec& With(const Matcher<const ArgumentTuple&>& m) {
Packit bd1cd8
    // Makes sure this is called at most once.
Packit bd1cd8
    ExpectSpecProperty(last_clause_ < kWith,
Packit bd1cd8
                       ".With() cannot appear "
Packit bd1cd8
                       "more than once in an ON_CALL().");
Packit bd1cd8
    last_clause_ = kWith;
Packit bd1cd8
Packit bd1cd8
    extra_matcher_ = m;
Packit bd1cd8
    return *this;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Implements the .WillByDefault() clause.
Packit bd1cd8
  OnCallSpec& WillByDefault(const Action<F>& action) {
Packit bd1cd8
    ExpectSpecProperty(last_clause_ < kWillByDefault,
Packit bd1cd8
                       ".WillByDefault() must appear "
Packit bd1cd8
                       "exactly once in an ON_CALL().");
Packit bd1cd8
    last_clause_ = kWillByDefault;
Packit bd1cd8
Packit bd1cd8
    ExpectSpecProperty(!action.IsDoDefault(),
Packit bd1cd8
                       "DoDefault() cannot be used in ON_CALL().");
Packit bd1cd8
    action_ = action;
Packit bd1cd8
    return *this;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Returns true iff the given arguments match the matchers.
Packit bd1cd8
  bool Matches(const ArgumentTuple& args) const {
Packit bd1cd8
    return TupleMatches(matchers_, args) && extra_matcher_.Matches(args);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Returns the action specified by the user.
Packit bd1cd8
  const Action<F>& GetAction() const {
Packit bd1cd8
    AssertSpecProperty(last_clause_ == kWillByDefault,
Packit bd1cd8
                       ".WillByDefault() must appear exactly "
Packit bd1cd8
                       "once in an ON_CALL().");
Packit bd1cd8
    return action_;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  // The information in statement
Packit bd1cd8
  //
Packit bd1cd8
  //   ON_CALL(mock_object, Method(matchers))
Packit bd1cd8
  //       .With(multi-argument-matcher)
Packit bd1cd8
  //       .WillByDefault(action);
Packit bd1cd8
  //
Packit bd1cd8
  // is recorded in the data members like this:
Packit bd1cd8
  //
Packit bd1cd8
  //   source file that contains the statement => file_
Packit bd1cd8
  //   line number of the statement            => line_
Packit bd1cd8
  //   matchers                                => matchers_
Packit bd1cd8
  //   multi-argument-matcher                  => extra_matcher_
Packit bd1cd8
  //   action                                  => action_
Packit bd1cd8
  ArgumentMatcherTuple matchers_;
Packit bd1cd8
  Matcher<const ArgumentTuple&> extra_matcher_;
Packit bd1cd8
  Action<F> action_;
Packit bd1cd8
};  // class OnCallSpec
Packit bd1cd8
Packit bd1cd8
// Possible reactions on uninteresting calls.
Packit bd1cd8
enum CallReaction {
Packit bd1cd8
  kAllow,
Packit bd1cd8
  kWarn,
Packit bd1cd8
  kFail,
Packit bd1cd8
  kDefault = kWarn  // By default, warn about uninteresting calls.
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
}  // namespace internal
Packit bd1cd8
Packit bd1cd8
// Utilities for manipulating mock objects.
Packit bd1cd8
class GTEST_API_ Mock {
Packit bd1cd8
 public:
Packit bd1cd8
  // The following public methods can be called concurrently.
Packit bd1cd8
Packit bd1cd8
  // Tells Google Mock to ignore mock_obj when checking for leaked
Packit bd1cd8
  // mock objects.
Packit bd1cd8
  static void AllowLeak(const void* mock_obj)
Packit bd1cd8
      GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
Packit bd1cd8
Packit bd1cd8
  // Verifies and clears all expectations on the given mock object.
Packit bd1cd8
  // If the expectations aren't satisfied, generates one or more
Packit bd1cd8
  // Google Test non-fatal failures and returns false.
Packit bd1cd8
  static bool VerifyAndClearExpectations(void* mock_obj)
Packit bd1cd8
      GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
Packit bd1cd8
Packit bd1cd8
  // Verifies all expectations on the given mock object and clears its
Packit bd1cd8
  // default actions and expectations.  Returns true iff the
Packit bd1cd8
  // verification was successful.
Packit bd1cd8
  static bool VerifyAndClear(void* mock_obj)
Packit bd1cd8
      GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  friend class internal::UntypedFunctionMockerBase;
Packit bd1cd8
Packit bd1cd8
  // Needed for a function mocker to register itself (so that we know
Packit bd1cd8
  // how to clear a mock object).
Packit bd1cd8
  template <typename F>
Packit bd1cd8
  friend class internal::FunctionMockerBase;
Packit bd1cd8
Packit bd1cd8
  template <typename M>
Packit bd1cd8
  friend class NiceMock;
Packit bd1cd8
Packit bd1cd8
  template <typename M>
Packit bd1cd8
  friend class NaggyMock;
Packit bd1cd8
Packit bd1cd8
  template <typename M>
Packit bd1cd8
  friend class StrictMock;
Packit bd1cd8
Packit bd1cd8
  // Tells Google Mock to allow uninteresting calls on the given mock
Packit bd1cd8
  // object.
Packit bd1cd8
  static void AllowUninterestingCalls(const void* mock_obj)
Packit bd1cd8
      GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
Packit bd1cd8
Packit bd1cd8
  // Tells Google Mock to warn the user about uninteresting calls on
Packit bd1cd8
  // the given mock object.
Packit bd1cd8
  static void WarnUninterestingCalls(const void* mock_obj)
Packit bd1cd8
      GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
Packit bd1cd8
Packit bd1cd8
  // Tells Google Mock to fail uninteresting calls on the given mock
Packit bd1cd8
  // object.
Packit bd1cd8
  static void FailUninterestingCalls(const void* mock_obj)
Packit bd1cd8
      GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
Packit bd1cd8
Packit bd1cd8
  // Tells Google Mock the given mock object is being destroyed and
Packit bd1cd8
  // its entry in the call-reaction table should be removed.
Packit bd1cd8
  static void UnregisterCallReaction(const void* mock_obj)
Packit bd1cd8
      GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
Packit bd1cd8
Packit bd1cd8
  // Returns the reaction Google Mock will have on uninteresting calls
Packit bd1cd8
  // made on the given mock object.
Packit bd1cd8
  static internal::CallReaction GetReactionOnUninterestingCalls(
Packit bd1cd8
      const void* mock_obj)
Packit bd1cd8
          GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
Packit bd1cd8
Packit bd1cd8
  // Verifies that all expectations on the given mock object have been
Packit bd1cd8
  // satisfied.  Reports one or more Google Test non-fatal failures
Packit bd1cd8
  // and returns false if not.
Packit bd1cd8
  static bool VerifyAndClearExpectationsLocked(void* mock_obj)
Packit bd1cd8
      GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex);
Packit bd1cd8
Packit bd1cd8
  // Clears all ON_CALL()s set on the given mock object.
Packit bd1cd8
  static void ClearDefaultActionsLocked(void* mock_obj)
Packit bd1cd8
      GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex);
Packit bd1cd8
Packit bd1cd8
  // Registers a mock object and a mock method it owns.
Packit bd1cd8
  static void Register(
Packit bd1cd8
      const void* mock_obj,
Packit bd1cd8
      internal::UntypedFunctionMockerBase* mocker)
Packit bd1cd8
          GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
Packit bd1cd8
Packit bd1cd8
  // Tells Google Mock where in the source code mock_obj is used in an
Packit bd1cd8
  // ON_CALL or EXPECT_CALL.  In case mock_obj is leaked, this
Packit bd1cd8
  // information helps the user identify which object it is.
Packit bd1cd8
  static void RegisterUseByOnCallOrExpectCall(
Packit bd1cd8
      const void* mock_obj, const char* file, int line)
Packit bd1cd8
          GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
Packit bd1cd8
Packit bd1cd8
  // Unregisters a mock method; removes the owning mock object from
Packit bd1cd8
  // the registry when the last mock method associated with it has
Packit bd1cd8
  // been unregistered.  This is called only in the destructor of
Packit bd1cd8
  // FunctionMockerBase.
Packit bd1cd8
  static void UnregisterLocked(internal::UntypedFunctionMockerBase* mocker)
Packit bd1cd8
      GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex);
Packit bd1cd8
};  // class Mock
Packit bd1cd8
Packit bd1cd8
// An abstract handle of an expectation.  Useful in the .After()
Packit bd1cd8
// clause of EXPECT_CALL() for setting the (partial) order of
Packit bd1cd8
// expectations.  The syntax:
Packit bd1cd8
//
Packit bd1cd8
//   Expectation e1 = EXPECT_CALL(...)...;
Packit bd1cd8
//   EXPECT_CALL(...).After(e1)...;
Packit bd1cd8
//
Packit bd1cd8
// sets two expectations where the latter can only be matched after
Packit bd1cd8
// the former has been satisfied.
Packit bd1cd8
//
Packit bd1cd8
// Notes:
Packit bd1cd8
//   - This class is copyable and has value semantics.
Packit bd1cd8
//   - Constness is shallow: a const Expectation object itself cannot
Packit bd1cd8
//     be modified, but the mutable methods of the ExpectationBase
Packit bd1cd8
//     object it references can be called via expectation_base().
Packit bd1cd8
//   - The constructors and destructor are defined out-of-line because
Packit bd1cd8
//     the Symbian WINSCW compiler wants to otherwise instantiate them
Packit bd1cd8
//     when it sees this class definition, at which point it doesn't have
Packit bd1cd8
//     ExpectationBase available yet, leading to incorrect destruction
Packit bd1cd8
//     in the linked_ptr (or compilation errors if using a checking
Packit bd1cd8
//     linked_ptr).
Packit bd1cd8
class GTEST_API_ Expectation {
Packit bd1cd8
 public:
Packit bd1cd8
  // Constructs a null object that doesn't reference any expectation.
Packit bd1cd8
  Expectation();
Packit bd1cd8
Packit bd1cd8
  ~Expectation();
Packit bd1cd8
Packit bd1cd8
  // This single-argument ctor must not be explicit, in order to support the
Packit bd1cd8
  //   Expectation e = EXPECT_CALL(...);
Packit bd1cd8
  // syntax.
Packit bd1cd8
  //
Packit bd1cd8
  // A TypedExpectation object stores its pre-requisites as
Packit bd1cd8
  // Expectation objects, and needs to call the non-const Retire()
Packit bd1cd8
  // method on the ExpectationBase objects they reference.  Therefore
Packit bd1cd8
  // Expectation must receive a *non-const* reference to the
Packit bd1cd8
  // ExpectationBase object.
Packit bd1cd8
  Expectation(internal::ExpectationBase& exp);  // NOLINT
Packit bd1cd8
Packit bd1cd8
  // The compiler-generated copy ctor and operator= work exactly as
Packit bd1cd8
  // intended, so we don't need to define our own.
Packit bd1cd8
Packit bd1cd8
  // Returns true iff rhs references the same expectation as this object does.
Packit bd1cd8
  bool operator==(const Expectation& rhs) const {
Packit bd1cd8
    return expectation_base_ == rhs.expectation_base_;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  bool operator!=(const Expectation& rhs) const { return !(*this == rhs); }
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  friend class ExpectationSet;
Packit bd1cd8
  friend class Sequence;
Packit bd1cd8
  friend class ::testing::internal::ExpectationBase;
Packit bd1cd8
  friend class ::testing::internal::UntypedFunctionMockerBase;
Packit bd1cd8
Packit bd1cd8
  template <typename F>
Packit bd1cd8
  friend class ::testing::internal::FunctionMockerBase;
Packit bd1cd8
Packit bd1cd8
  template <typename F>
Packit bd1cd8
  friend class ::testing::internal::TypedExpectation;
Packit bd1cd8
Packit bd1cd8
  // This comparator is needed for putting Expectation objects into a set.
Packit bd1cd8
  class Less {
Packit bd1cd8
   public:
Packit bd1cd8
    bool operator()(const Expectation& lhs, const Expectation& rhs) const {
Packit bd1cd8
      return lhs.expectation_base_.get() < rhs.expectation_base_.get();
Packit bd1cd8
    }
Packit bd1cd8
  };
Packit bd1cd8
Packit bd1cd8
  typedef ::std::set<Expectation, Less> Set;
Packit bd1cd8
Packit bd1cd8
  Expectation(
Packit bd1cd8
      const internal::linked_ptr<internal::ExpectationBase>& expectation_base);
Packit bd1cd8
Packit bd1cd8
  // Returns the expectation this object references.
Packit bd1cd8
  const internal::linked_ptr<internal::ExpectationBase>&
Packit bd1cd8
  expectation_base() const {
Packit bd1cd8
    return expectation_base_;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // A linked_ptr that co-owns the expectation this handle references.
Packit bd1cd8
  internal::linked_ptr<internal::ExpectationBase> expectation_base_;
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// A set of expectation handles.  Useful in the .After() clause of
Packit bd1cd8
// EXPECT_CALL() for setting the (partial) order of expectations.  The
Packit bd1cd8
// syntax:
Packit bd1cd8
//
Packit bd1cd8
//   ExpectationSet es;
Packit bd1cd8
//   es += EXPECT_CALL(...)...;
Packit bd1cd8
//   es += EXPECT_CALL(...)...;
Packit bd1cd8
//   EXPECT_CALL(...).After(es)...;
Packit bd1cd8
//
Packit bd1cd8
// sets three expectations where the last one can only be matched
Packit bd1cd8
// after the first two have both been satisfied.
Packit bd1cd8
//
Packit bd1cd8
// This class is copyable and has value semantics.
Packit bd1cd8
class ExpectationSet {
Packit bd1cd8
 public:
Packit bd1cd8
  // A bidirectional iterator that can read a const element in the set.
Packit bd1cd8
  typedef Expectation::Set::const_iterator const_iterator;
Packit bd1cd8
Packit bd1cd8
  // An object stored in the set.  This is an alias of Expectation.
Packit bd1cd8
  typedef Expectation::Set::value_type value_type;
Packit bd1cd8
Packit bd1cd8
  // Constructs an empty set.
Packit bd1cd8
  ExpectationSet() {}
Packit bd1cd8
Packit bd1cd8
  // This single-argument ctor must not be explicit, in order to support the
Packit bd1cd8
  //   ExpectationSet es = EXPECT_CALL(...);
Packit bd1cd8
  // syntax.
Packit bd1cd8
  ExpectationSet(internal::ExpectationBase& exp) {  // NOLINT
Packit bd1cd8
    *this += Expectation(exp);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // This single-argument ctor implements implicit conversion from
Packit bd1cd8
  // Expectation and thus must not be explicit.  This allows either an
Packit bd1cd8
  // Expectation or an ExpectationSet to be used in .After().
Packit bd1cd8
  ExpectationSet(const Expectation& e) {  // NOLINT
Packit bd1cd8
    *this += e;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // The compiler-generator ctor and operator= works exactly as
Packit bd1cd8
  // intended, so we don't need to define our own.
Packit bd1cd8
Packit bd1cd8
  // Returns true iff rhs contains the same set of Expectation objects
Packit bd1cd8
  // as this does.
Packit bd1cd8
  bool operator==(const ExpectationSet& rhs) const {
Packit bd1cd8
    return expectations_ == rhs.expectations_;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  bool operator!=(const ExpectationSet& rhs) const { return !(*this == rhs); }
Packit bd1cd8
Packit bd1cd8
  // Implements the syntax
Packit bd1cd8
  //   expectation_set += EXPECT_CALL(...);
Packit bd1cd8
  ExpectationSet& operator+=(const Expectation& e) {
Packit bd1cd8
    expectations_.insert(e);
Packit bd1cd8
    return *this;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  int size() const { return static_cast<int>(expectations_.size()); }
Packit bd1cd8
Packit bd1cd8
  const_iterator begin() const { return expectations_.begin(); }
Packit bd1cd8
  const_iterator end() const { return expectations_.end(); }
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  Expectation::Set expectations_;
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
// Sequence objects are used by a user to specify the relative order
Packit bd1cd8
// in which the expectations should match.  They are copyable (we rely
Packit bd1cd8
// on the compiler-defined copy constructor and assignment operator).
Packit bd1cd8
class GTEST_API_ Sequence {
Packit bd1cd8
 public:
Packit bd1cd8
  // Constructs an empty sequence.
Packit bd1cd8
  Sequence() : last_expectation_(new Expectation) {}
Packit bd1cd8
Packit bd1cd8
  // Adds an expectation to this sequence.  The caller must ensure
Packit bd1cd8
  // that no other thread is accessing this Sequence object.
Packit bd1cd8
  void AddExpectation(const Expectation& expectation) const;
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  // The last expectation in this sequence.  We use a linked_ptr here
Packit bd1cd8
  // because Sequence objects are copyable and we want the copies to
Packit bd1cd8
  // be aliases.  The linked_ptr allows the copies to co-own and share
Packit bd1cd8
  // the same Expectation object.
Packit bd1cd8
  internal::linked_ptr<Expectation> last_expectation_;
Packit bd1cd8
};  // class Sequence
Packit bd1cd8
Packit bd1cd8
// An object of this type causes all EXPECT_CALL() statements
Packit bd1cd8
// encountered in its scope to be put in an anonymous sequence.  The
Packit bd1cd8
// work is done in the constructor and destructor.  You should only
Packit bd1cd8
// create an InSequence object on the stack.
Packit bd1cd8
//
Packit bd1cd8
// The sole purpose for this class is to support easy definition of
Packit bd1cd8
// sequential expectations, e.g.
Packit bd1cd8
//
Packit bd1cd8
//   {
Packit bd1cd8
//     InSequence dummy;  // The name of the object doesn't matter.
Packit bd1cd8
//
Packit bd1cd8
//     // The following expectations must match in the order they appear.
Packit bd1cd8
//     EXPECT_CALL(a, Bar())...;
Packit bd1cd8
//     EXPECT_CALL(a, Baz())...;
Packit bd1cd8
//     ...
Packit bd1cd8
//     EXPECT_CALL(b, Xyz())...;
Packit bd1cd8
//   }
Packit bd1cd8
//
Packit bd1cd8
// You can create InSequence objects in multiple threads, as long as
Packit bd1cd8
// they are used to affect different mock objects.  The idea is that
Packit bd1cd8
// each thread can create and set up its own mocks as if it's the only
Packit bd1cd8
// thread.  However, for clarity of your tests we recommend you to set
Packit bd1cd8
// up mocks in the main thread unless you have a good reason not to do
Packit bd1cd8
// so.
Packit bd1cd8
class GTEST_API_ InSequence {
Packit bd1cd8
 public:
Packit bd1cd8
  InSequence();
Packit bd1cd8
  ~InSequence();
Packit bd1cd8
 private:
Packit bd1cd8
  bool sequence_created_;
Packit bd1cd8
Packit bd1cd8
  GTEST_DISALLOW_COPY_AND_ASSIGN_(InSequence);  // NOLINT
Packit bd1cd8
} GTEST_ATTRIBUTE_UNUSED_;
Packit bd1cd8
Packit bd1cd8
namespace internal {
Packit bd1cd8
Packit bd1cd8
// Points to the implicit sequence introduced by a living InSequence
Packit bd1cd8
// object (if any) in the current thread or NULL.
Packit bd1cd8
GTEST_API_ extern ThreadLocal<Sequence*> g_gmock_implicit_sequence;
Packit bd1cd8
Packit bd1cd8
// Base class for implementing expectations.
Packit bd1cd8
//
Packit bd1cd8
// There are two reasons for having a type-agnostic base class for
Packit bd1cd8
// Expectation:
Packit bd1cd8
//
Packit bd1cd8
//   1. We need to store collections of expectations of different
Packit bd1cd8
//   types (e.g. all pre-requisites of a particular expectation, all
Packit bd1cd8
//   expectations in a sequence).  Therefore these expectation objects
Packit bd1cd8
//   must share a common base class.
Packit bd1cd8
//
Packit bd1cd8
//   2. We can avoid binary code bloat by moving methods not depending
Packit bd1cd8
//   on the template argument of Expectation to the base class.
Packit bd1cd8
//
Packit bd1cd8
// This class is internal and mustn't be used by user code directly.
Packit bd1cd8
class GTEST_API_ ExpectationBase {
Packit bd1cd8
 public:
Packit bd1cd8
  // source_text is the EXPECT_CALL(...) source that created this Expectation.
Packit bd1cd8
  ExpectationBase(const char* file, int line, const string& source_text);
Packit bd1cd8
Packit bd1cd8
  virtual ~ExpectationBase();
Packit bd1cd8
Packit bd1cd8
  // Where in the source file was the expectation spec defined?
Packit bd1cd8
  const char* file() const { return file_; }
Packit bd1cd8
  int line() const { return line_; }
Packit bd1cd8
  const char* source_text() const { return source_text_.c_str(); }
Packit bd1cd8
  // Returns the cardinality specified in the expectation spec.
Packit bd1cd8
  const Cardinality& cardinality() const { return cardinality_; }
Packit bd1cd8
Packit bd1cd8
  // Describes the source file location of this expectation.
Packit bd1cd8
  void DescribeLocationTo(::std::ostream* os) const {
Packit bd1cd8
    *os << FormatFileLocation(file(), line()) << " ";
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Describes how many times a function call matching this
Packit bd1cd8
  // expectation has occurred.
Packit bd1cd8
  void DescribeCallCountTo(::std::ostream* os) const
Packit bd1cd8
      GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex);
Packit bd1cd8
Packit bd1cd8
  // If this mock method has an extra matcher (i.e. .With(matcher)),
Packit bd1cd8
  // describes it to the ostream.
Packit bd1cd8
  virtual void MaybeDescribeExtraMatcherTo(::std::ostream* os) = 0;
Packit bd1cd8
Packit bd1cd8
 protected:
Packit bd1cd8
  friend class ::testing::Expectation;
Packit bd1cd8
  friend class UntypedFunctionMockerBase;
Packit bd1cd8
Packit bd1cd8
  enum Clause {
Packit bd1cd8
    // Don't change the order of the enum members!
Packit bd1cd8
    kNone,
Packit bd1cd8
    kWith,
Packit bd1cd8
    kTimes,
Packit bd1cd8
    kInSequence,
Packit bd1cd8
    kAfter,
Packit bd1cd8
    kWillOnce,
Packit bd1cd8
    kWillRepeatedly,
Packit bd1cd8
    kRetiresOnSaturation
Packit bd1cd8
  };
Packit bd1cd8
Packit bd1cd8
  typedef std::vector<const void*> UntypedActions;
Packit bd1cd8
Packit bd1cd8
  // Returns an Expectation object that references and co-owns this
Packit bd1cd8
  // expectation.
Packit bd1cd8
  virtual Expectation GetHandle() = 0;
Packit bd1cd8
Packit bd1cd8
  // Asserts that the EXPECT_CALL() statement has the given property.
Packit bd1cd8
  void AssertSpecProperty(bool property, const string& failure_message) const {
Packit bd1cd8
    Assert(property, file_, line_, failure_message);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Expects that the EXPECT_CALL() statement has the given property.
Packit bd1cd8
  void ExpectSpecProperty(bool property, const string& failure_message) const {
Packit bd1cd8
    Expect(property, file_, line_, failure_message);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Explicitly specifies the cardinality of this expectation.  Used
Packit bd1cd8
  // by the subclasses to implement the .Times() clause.
Packit bd1cd8
  void SpecifyCardinality(const Cardinality& cardinality);
Packit bd1cd8
Packit bd1cd8
  // Returns true iff the user specified the cardinality explicitly
Packit bd1cd8
  // using a .Times().
Packit bd1cd8
  bool cardinality_specified() const { return cardinality_specified_; }
Packit bd1cd8
Packit bd1cd8
  // Sets the cardinality of this expectation spec.
Packit bd1cd8
  void set_cardinality(const Cardinality& a_cardinality) {
Packit bd1cd8
    cardinality_ = a_cardinality;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // The following group of methods should only be called after the
Packit bd1cd8
  // EXPECT_CALL() statement, and only when g_gmock_mutex is held by
Packit bd1cd8
  // the current thread.
Packit bd1cd8
Packit bd1cd8
  // Retires all pre-requisites of this expectation.
Packit bd1cd8
  void RetireAllPreRequisites()
Packit bd1cd8
      GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex);
Packit bd1cd8
Packit bd1cd8
  // Returns true iff this expectation is retired.
Packit bd1cd8
  bool is_retired() const
Packit bd1cd8
      GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
Packit bd1cd8
    g_gmock_mutex.AssertHeld();
Packit bd1cd8
    return retired_;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Retires this expectation.
Packit bd1cd8
  void Retire()
Packit bd1cd8
      GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
Packit bd1cd8
    g_gmock_mutex.AssertHeld();
Packit bd1cd8
    retired_ = true;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Returns true iff this expectation is satisfied.
Packit bd1cd8
  bool IsSatisfied() const
Packit bd1cd8
      GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
Packit bd1cd8
    g_gmock_mutex.AssertHeld();
Packit bd1cd8
    return cardinality().IsSatisfiedByCallCount(call_count_);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Returns true iff this expectation is saturated.
Packit bd1cd8
  bool IsSaturated() const
Packit bd1cd8
      GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
Packit bd1cd8
    g_gmock_mutex.AssertHeld();
Packit bd1cd8
    return cardinality().IsSaturatedByCallCount(call_count_);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Returns true iff this expectation is over-saturated.
Packit bd1cd8
  bool IsOverSaturated() const
Packit bd1cd8
      GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
Packit bd1cd8
    g_gmock_mutex.AssertHeld();
Packit bd1cd8
    return cardinality().IsOverSaturatedByCallCount(call_count_);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Returns true iff all pre-requisites of this expectation are satisfied.
Packit bd1cd8
  bool AllPrerequisitesAreSatisfied() const
Packit bd1cd8
      GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex);
Packit bd1cd8
Packit bd1cd8
  // Adds unsatisfied pre-requisites of this expectation to 'result'.
Packit bd1cd8
  void FindUnsatisfiedPrerequisites(ExpectationSet* result) const
Packit bd1cd8
      GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex);
Packit bd1cd8
Packit bd1cd8
  // Returns the number this expectation has been invoked.
Packit bd1cd8
  int call_count() const
Packit bd1cd8
      GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
Packit bd1cd8
    g_gmock_mutex.AssertHeld();
Packit bd1cd8
    return call_count_;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Increments the number this expectation has been invoked.
Packit bd1cd8
  void IncrementCallCount()
Packit bd1cd8
      GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
Packit bd1cd8
    g_gmock_mutex.AssertHeld();
Packit bd1cd8
    call_count_++;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Checks the action count (i.e. the number of WillOnce() and
Packit bd1cd8
  // WillRepeatedly() clauses) against the cardinality if this hasn't
Packit bd1cd8
  // been done before.  Prints a warning if there are too many or too
Packit bd1cd8
  // few actions.
Packit bd1cd8
  void CheckActionCountIfNotDone() const
Packit bd1cd8
      GTEST_LOCK_EXCLUDED_(mutex_);
Packit bd1cd8
Packit bd1cd8
  friend class ::testing::Sequence;
Packit bd1cd8
  friend class ::testing::internal::ExpectationTester;
Packit bd1cd8
Packit bd1cd8
  template <typename Function>
Packit bd1cd8
  friend class TypedExpectation;
Packit bd1cd8
Packit bd1cd8
  // Implements the .Times() clause.
Packit bd1cd8
  void UntypedTimes(const Cardinality& a_cardinality);
Packit bd1cd8
Packit bd1cd8
  // This group of fields are part of the spec and won't change after
Packit bd1cd8
  // an EXPECT_CALL() statement finishes.
Packit bd1cd8
  const char* file_;          // The file that contains the expectation.
Packit bd1cd8
  int line_;                  // The line number of the expectation.
Packit bd1cd8
  const string source_text_;  // The EXPECT_CALL(...) source text.
Packit bd1cd8
  // True iff the cardinality is specified explicitly.
Packit bd1cd8
  bool cardinality_specified_;
Packit bd1cd8
  Cardinality cardinality_;            // The cardinality of the expectation.
Packit bd1cd8
  // The immediate pre-requisites (i.e. expectations that must be
Packit bd1cd8
  // satisfied before this expectation can be matched) of this
Packit bd1cd8
  // expectation.  We use linked_ptr in the set because we want an
Packit bd1cd8
  // Expectation object to be co-owned by its FunctionMocker and its
Packit bd1cd8
  // successors.  This allows multiple mock objects to be deleted at
Packit bd1cd8
  // different times.
Packit bd1cd8
  ExpectationSet immediate_prerequisites_;
Packit bd1cd8
Packit bd1cd8
  // This group of fields are the current state of the expectation,
Packit bd1cd8
  // and can change as the mock function is called.
Packit bd1cd8
  int call_count_;  // How many times this expectation has been invoked.
Packit bd1cd8
  bool retired_;    // True iff this expectation has retired.
Packit bd1cd8
  UntypedActions untyped_actions_;
Packit bd1cd8
  bool extra_matcher_specified_;
Packit bd1cd8
  bool repeated_action_specified_;  // True if a WillRepeatedly() was specified.
Packit bd1cd8
  bool retires_on_saturation_;
Packit bd1cd8
  Clause last_clause_;
Packit bd1cd8
  mutable bool action_count_checked_;  // Under mutex_.
Packit bd1cd8
  mutable Mutex mutex_;  // Protects action_count_checked_.
Packit bd1cd8
Packit bd1cd8
  GTEST_DISALLOW_ASSIGN_(ExpectationBase);
Packit bd1cd8
};  // class ExpectationBase
Packit bd1cd8
Packit bd1cd8
// Impements an expectation for the given function type.
Packit bd1cd8
template <typename F>
Packit bd1cd8
class TypedExpectation : public ExpectationBase {
Packit bd1cd8
 public:
Packit bd1cd8
  typedef typename Function<F>::ArgumentTuple ArgumentTuple;
Packit bd1cd8
  typedef typename Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple;
Packit bd1cd8
  typedef typename Function<F>::Result Result;
Packit bd1cd8
Packit bd1cd8
  TypedExpectation(FunctionMockerBase<F>* owner,
Packit bd1cd8
                   const char* a_file, int a_line, const string& a_source_text,
Packit bd1cd8
                   const ArgumentMatcherTuple& m)
Packit bd1cd8
      : ExpectationBase(a_file, a_line, a_source_text),
Packit bd1cd8
        owner_(owner),
Packit bd1cd8
        matchers_(m),
Packit bd1cd8
        // By default, extra_matcher_ should match anything.  However,
Packit bd1cd8
        // we cannot initialize it with _ as that triggers a compiler
Packit bd1cd8
        // bug in Symbian's C++ compiler (cannot decide between two
Packit bd1cd8
        // overloaded constructors of Matcher<const ArgumentTuple&>).
Packit bd1cd8
        extra_matcher_(A<const ArgumentTuple&>()),
Packit bd1cd8
        repeated_action_(DoDefault()) {}
Packit bd1cd8
Packit bd1cd8
  virtual ~TypedExpectation() {
Packit bd1cd8
    // Check the validity of the action count if it hasn't been done
Packit bd1cd8
    // yet (for example, if the expectation was never used).
Packit bd1cd8
    CheckActionCountIfNotDone();
Packit bd1cd8
    for (UntypedActions::const_iterator it = untyped_actions_.begin();
Packit bd1cd8
         it != untyped_actions_.end(); ++it) {
Packit bd1cd8
      delete static_cast<const Action<F>*>(*it);
Packit bd1cd8
    }
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Implements the .With() clause.
Packit bd1cd8
  TypedExpectation& With(const Matcher<const ArgumentTuple&>& m) {
Packit bd1cd8
    if (last_clause_ == kWith) {
Packit bd1cd8
      ExpectSpecProperty(false,
Packit bd1cd8
                         ".With() cannot appear "
Packit bd1cd8
                         "more than once in an EXPECT_CALL().");
Packit bd1cd8
    } else {
Packit bd1cd8
      ExpectSpecProperty(last_clause_ < kWith,
Packit bd1cd8
                         ".With() must be the first "
Packit bd1cd8
                         "clause in an EXPECT_CALL().");
Packit bd1cd8
    }
Packit bd1cd8
    last_clause_ = kWith;
Packit bd1cd8
Packit bd1cd8
    extra_matcher_ = m;
Packit bd1cd8
    extra_matcher_specified_ = true;
Packit bd1cd8
    return *this;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Implements the .Times() clause.
Packit bd1cd8
  TypedExpectation& Times(const Cardinality& a_cardinality) {
Packit bd1cd8
    ExpectationBase::UntypedTimes(a_cardinality);
Packit bd1cd8
    return *this;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Implements the .Times() clause.
Packit bd1cd8
  TypedExpectation& Times(int n) {
Packit bd1cd8
    return Times(Exactly(n));
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Implements the .InSequence() clause.
Packit bd1cd8
  TypedExpectation& InSequence(const Sequence& s) {
Packit bd1cd8
    ExpectSpecProperty(last_clause_ <= kInSequence,
Packit bd1cd8
                       ".InSequence() cannot appear after .After(),"
Packit bd1cd8
                       " .WillOnce(), .WillRepeatedly(), or "
Packit bd1cd8
                       ".RetiresOnSaturation().");
Packit bd1cd8
    last_clause_ = kInSequence;
Packit bd1cd8
Packit bd1cd8
    s.AddExpectation(GetHandle());
Packit bd1cd8
    return *this;
Packit bd1cd8
  }
Packit bd1cd8
  TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2) {
Packit bd1cd8
    return InSequence(s1).InSequence(s2);
Packit bd1cd8
  }
Packit bd1cd8
  TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2,
Packit bd1cd8
                               const Sequence& s3) {
Packit bd1cd8
    return InSequence(s1, s2).InSequence(s3);
Packit bd1cd8
  }
Packit bd1cd8
  TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2,
Packit bd1cd8
                               const Sequence& s3, const Sequence& s4) {
Packit bd1cd8
    return InSequence(s1, s2, s3).InSequence(s4);
Packit bd1cd8
  }
Packit bd1cd8
  TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2,
Packit bd1cd8
                               const Sequence& s3, const Sequence& s4,
Packit bd1cd8
                               const Sequence& s5) {
Packit bd1cd8
    return InSequence(s1, s2, s3, s4).InSequence(s5);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Implements that .After() clause.
Packit bd1cd8
  TypedExpectation& After(const ExpectationSet& s) {
Packit bd1cd8
    ExpectSpecProperty(last_clause_ <= kAfter,
Packit bd1cd8
                       ".After() cannot appear after .WillOnce(),"
Packit bd1cd8
                       " .WillRepeatedly(), or "
Packit bd1cd8
                       ".RetiresOnSaturation().");
Packit bd1cd8
    last_clause_ = kAfter;
Packit bd1cd8
Packit bd1cd8
    for (ExpectationSet::const_iterator it = s.begin(); it != s.end(); ++it) {
Packit bd1cd8
      immediate_prerequisites_ += *it;
Packit bd1cd8
    }
Packit bd1cd8
    return *this;
Packit bd1cd8
  }
Packit bd1cd8
  TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2) {
Packit bd1cd8
    return After(s1).After(s2);
Packit bd1cd8
  }
Packit bd1cd8
  TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2,
Packit bd1cd8
                          const ExpectationSet& s3) {
Packit bd1cd8
    return After(s1, s2).After(s3);
Packit bd1cd8
  }
Packit bd1cd8
  TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2,
Packit bd1cd8
                          const ExpectationSet& s3, const ExpectationSet& s4) {
Packit bd1cd8
    return After(s1, s2, s3).After(s4);
Packit bd1cd8
  }
Packit bd1cd8
  TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2,
Packit bd1cd8
                          const ExpectationSet& s3, const ExpectationSet& s4,
Packit bd1cd8
                          const ExpectationSet& s5) {
Packit bd1cd8
    return After(s1, s2, s3, s4).After(s5);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Implements the .WillOnce() clause.
Packit bd1cd8
  TypedExpectation& WillOnce(const Action<F>& action) {
Packit bd1cd8
    ExpectSpecProperty(last_clause_ <= kWillOnce,
Packit bd1cd8
                       ".WillOnce() cannot appear after "
Packit bd1cd8
                       ".WillRepeatedly() or .RetiresOnSaturation().");
Packit bd1cd8
    last_clause_ = kWillOnce;
Packit bd1cd8
Packit bd1cd8
    untyped_actions_.push_back(new Action<F>(action));
Packit bd1cd8
    if (!cardinality_specified()) {
Packit bd1cd8
      set_cardinality(Exactly(static_cast<int>(untyped_actions_.size())));
Packit bd1cd8
    }
Packit bd1cd8
    return *this;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Implements the .WillRepeatedly() clause.
Packit bd1cd8
  TypedExpectation& WillRepeatedly(const Action<F>& action) {
Packit bd1cd8
    if (last_clause_ == kWillRepeatedly) {
Packit bd1cd8
      ExpectSpecProperty(false,
Packit bd1cd8
                         ".WillRepeatedly() cannot appear "
Packit bd1cd8
                         "more than once in an EXPECT_CALL().");
Packit bd1cd8
    } else {
Packit bd1cd8
      ExpectSpecProperty(last_clause_ < kWillRepeatedly,
Packit bd1cd8
                         ".WillRepeatedly() cannot appear "
Packit bd1cd8
                         "after .RetiresOnSaturation().");
Packit bd1cd8
    }
Packit bd1cd8
    last_clause_ = kWillRepeatedly;
Packit bd1cd8
    repeated_action_specified_ = true;
Packit bd1cd8
Packit bd1cd8
    repeated_action_ = action;
Packit bd1cd8
    if (!cardinality_specified()) {
Packit bd1cd8
      set_cardinality(AtLeast(static_cast<int>(untyped_actions_.size())));
Packit bd1cd8
    }
Packit bd1cd8
Packit bd1cd8
    // Now that no more action clauses can be specified, we check
Packit bd1cd8
    // whether their count makes sense.
Packit bd1cd8
    CheckActionCountIfNotDone();
Packit bd1cd8
    return *this;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Implements the .RetiresOnSaturation() clause.
Packit bd1cd8
  TypedExpectation& RetiresOnSaturation() {
Packit bd1cd8
    ExpectSpecProperty(last_clause_ < kRetiresOnSaturation,
Packit bd1cd8
                       ".RetiresOnSaturation() cannot appear "
Packit bd1cd8
                       "more than once.");
Packit bd1cd8
    last_clause_ = kRetiresOnSaturation;
Packit bd1cd8
    retires_on_saturation_ = true;
Packit bd1cd8
Packit bd1cd8
    // Now that no more action clauses can be specified, we check
Packit bd1cd8
    // whether their count makes sense.
Packit bd1cd8
    CheckActionCountIfNotDone();
Packit bd1cd8
    return *this;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Returns the matchers for the arguments as specified inside the
Packit bd1cd8
  // EXPECT_CALL() macro.
Packit bd1cd8
  const ArgumentMatcherTuple& matchers() const {
Packit bd1cd8
    return matchers_;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Returns the matcher specified by the .With() clause.
Packit bd1cd8
  const Matcher<const ArgumentTuple&>& extra_matcher() const {
Packit bd1cd8
    return extra_matcher_;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Returns the action specified by the .WillRepeatedly() clause.
Packit bd1cd8
  const Action<F>& repeated_action() const { return repeated_action_; }
Packit bd1cd8
Packit bd1cd8
  // If this mock method has an extra matcher (i.e. .With(matcher)),
Packit bd1cd8
  // describes it to the ostream.
Packit bd1cd8
  virtual void MaybeDescribeExtraMatcherTo(::std::ostream* os) {
Packit bd1cd8
    if (extra_matcher_specified_) {
Packit bd1cd8
      *os << "    Expected args: ";
Packit bd1cd8
      extra_matcher_.DescribeTo(os);
Packit bd1cd8
      *os << "\n";
Packit bd1cd8
    }
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  template <typename Function>
Packit bd1cd8
  friend class FunctionMockerBase;
Packit bd1cd8
Packit bd1cd8
  // Returns an Expectation object that references and co-owns this
Packit bd1cd8
  // expectation.
Packit bd1cd8
  virtual Expectation GetHandle() {
Packit bd1cd8
    return owner_->GetHandleOf(this);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // The following methods will be called only after the EXPECT_CALL()
Packit bd1cd8
  // statement finishes and when the current thread holds
Packit bd1cd8
  // g_gmock_mutex.
Packit bd1cd8
Packit bd1cd8
  // Returns true iff this expectation matches the given arguments.
Packit bd1cd8
  bool Matches(const ArgumentTuple& args) const
Packit bd1cd8
      GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
Packit bd1cd8
    g_gmock_mutex.AssertHeld();
Packit bd1cd8
    return TupleMatches(matchers_, args) && extra_matcher_.Matches(args);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Returns true iff this expectation should handle the given arguments.
Packit bd1cd8
  bool ShouldHandleArguments(const ArgumentTuple& args) const
Packit bd1cd8
      GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
Packit bd1cd8
    g_gmock_mutex.AssertHeld();
Packit bd1cd8
Packit bd1cd8
    // In case the action count wasn't checked when the expectation
Packit bd1cd8
    // was defined (e.g. if this expectation has no WillRepeatedly()
Packit bd1cd8
    // or RetiresOnSaturation() clause), we check it when the
Packit bd1cd8
    // expectation is used for the first time.
Packit bd1cd8
    CheckActionCountIfNotDone();
Packit bd1cd8
    return !is_retired() && AllPrerequisitesAreSatisfied() && Matches(args);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Describes the result of matching the arguments against this
Packit bd1cd8
  // expectation to the given ostream.
Packit bd1cd8
  void ExplainMatchResultTo(
Packit bd1cd8
      const ArgumentTuple& args,
Packit bd1cd8
      ::std::ostream* os) const
Packit bd1cd8
          GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
Packit bd1cd8
    g_gmock_mutex.AssertHeld();
Packit bd1cd8
Packit bd1cd8
    if (is_retired()) {
Packit bd1cd8
      *os << "         Expected: the expectation is active\n"
Packit bd1cd8
          << "           Actual: it is retired\n";
Packit bd1cd8
    } else if (!Matches(args)) {
Packit bd1cd8
      if (!TupleMatches(matchers_, args)) {
Packit bd1cd8
        ExplainMatchFailureTupleTo(matchers_, args, os);
Packit bd1cd8
      }
Packit bd1cd8
      StringMatchResultListener listener;
Packit bd1cd8
      if (!extra_matcher_.MatchAndExplain(args, &listener)) {
Packit bd1cd8
        *os << "    Expected args: ";
Packit bd1cd8
        extra_matcher_.DescribeTo(os);
Packit bd1cd8
        *os << "\n           Actual: don't match";
Packit bd1cd8
Packit bd1cd8
        internal::PrintIfNotEmpty(listener.str(), os);
Packit bd1cd8
        *os << "\n";
Packit bd1cd8
      }
Packit bd1cd8
    } else if (!AllPrerequisitesAreSatisfied()) {
Packit bd1cd8
      *os << "         Expected: all pre-requisites are satisfied\n"
Packit bd1cd8
          << "           Actual: the following immediate pre-requisites "
Packit bd1cd8
          << "are not satisfied:\n";
Packit bd1cd8
      ExpectationSet unsatisfied_prereqs;
Packit bd1cd8
      FindUnsatisfiedPrerequisites(&unsatisfied_prereqs);
Packit bd1cd8
      int i = 0;
Packit bd1cd8
      for (ExpectationSet::const_iterator it = unsatisfied_prereqs.begin();
Packit bd1cd8
           it != unsatisfied_prereqs.end(); ++it) {
Packit bd1cd8
        it->expectation_base()->DescribeLocationTo(os);
Packit bd1cd8
        *os << "pre-requisite #" << i++ << "\n";
Packit bd1cd8
      }
Packit bd1cd8
      *os << "                   (end of pre-requisites)\n";
Packit bd1cd8
    } else {
Packit bd1cd8
      // This line is here just for completeness' sake.  It will never
Packit bd1cd8
      // be executed as currently the ExplainMatchResultTo() function
Packit bd1cd8
      // is called only when the mock function call does NOT match the
Packit bd1cd8
      // expectation.
Packit bd1cd8
      *os << "The call matches the expectation.\n";
Packit bd1cd8
    }
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Returns the action that should be taken for the current invocation.
Packit bd1cd8
  const Action<F>& GetCurrentAction(
Packit bd1cd8
      const FunctionMockerBase<F>* mocker,
Packit bd1cd8
      const ArgumentTuple& args) const
Packit bd1cd8
          GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
Packit bd1cd8
    g_gmock_mutex.AssertHeld();
Packit bd1cd8
    const int count = call_count();
Packit bd1cd8
    Assert(count >= 1, __FILE__, __LINE__,
Packit bd1cd8
           "call_count() is <= 0 when GetCurrentAction() is "
Packit bd1cd8
           "called - this should never happen.");
Packit bd1cd8
Packit bd1cd8
    const int action_count = static_cast<int>(untyped_actions_.size());
Packit bd1cd8
    if (action_count > 0 && !repeated_action_specified_ &&
Packit bd1cd8
        count > action_count) {
Packit bd1cd8
      // If there is at least one WillOnce() and no WillRepeatedly(),
Packit bd1cd8
      // we warn the user when the WillOnce() clauses ran out.
Packit bd1cd8
      ::std::stringstream ss;
Packit bd1cd8
      DescribeLocationTo(&ss);
Packit bd1cd8
      ss << "Actions ran out in " << source_text() << "...\n"
Packit bd1cd8
         << "Called " << count << " times, but only "
Packit bd1cd8
         << action_count << " WillOnce()"
Packit bd1cd8
         << (action_count == 1 ? " is" : "s are") << " specified - ";
Packit bd1cd8
      mocker->DescribeDefaultActionTo(args, &ss);
Packit bd1cd8
      Log(kWarning, ss.str(), 1);
Packit bd1cd8
    }
Packit bd1cd8
Packit bd1cd8
    return count <= action_count ?
Packit bd1cd8
        *static_cast<const Action<F>*>(untyped_actions_[count - 1]) :
Packit bd1cd8
        repeated_action();
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Given the arguments of a mock function call, if the call will
Packit bd1cd8
  // over-saturate this expectation, returns the default action;
Packit bd1cd8
  // otherwise, returns the next action in this expectation.  Also
Packit bd1cd8
  // describes *what* happened to 'what', and explains *why* Google
Packit bd1cd8
  // Mock does it to 'why'.  This method is not const as it calls
Packit bd1cd8
  // IncrementCallCount().  A return value of NULL means the default
Packit bd1cd8
  // action.
Packit bd1cd8
  const Action<F>* GetActionForArguments(
Packit bd1cd8
      const FunctionMockerBase<F>* mocker,
Packit bd1cd8
      const ArgumentTuple& args,
Packit bd1cd8
      ::std::ostream* what,
Packit bd1cd8
      ::std::ostream* why)
Packit bd1cd8
          GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
Packit bd1cd8
    g_gmock_mutex.AssertHeld();
Packit bd1cd8
    if (IsSaturated()) {
Packit bd1cd8
      // We have an excessive call.
Packit bd1cd8
      IncrementCallCount();
Packit bd1cd8
      *what << "Mock function called more times than expected - ";
Packit bd1cd8
      mocker->DescribeDefaultActionTo(args, what);
Packit bd1cd8
      DescribeCallCountTo(why);
Packit bd1cd8
Packit bd1cd8
      // TODO(wan@google.com): allow the user to control whether
Packit bd1cd8
      // unexpected calls should fail immediately or continue using a
Packit bd1cd8
      // flag --gmock_unexpected_calls_are_fatal.
Packit bd1cd8
      return NULL;
Packit bd1cd8
    }
Packit bd1cd8
Packit bd1cd8
    IncrementCallCount();
Packit bd1cd8
    RetireAllPreRequisites();
Packit bd1cd8
Packit bd1cd8
    if (retires_on_saturation_ && IsSaturated()) {
Packit bd1cd8
      Retire();
Packit bd1cd8
    }
Packit bd1cd8
Packit bd1cd8
    // Must be done after IncrementCount()!
Packit bd1cd8
    *what << "Mock function call matches " << source_text() <<"...\n";
Packit bd1cd8
    return &(GetCurrentAction(mocker, args));
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // All the fields below won't change once the EXPECT_CALL()
Packit bd1cd8
  // statement finishes.
Packit bd1cd8
  FunctionMockerBase<F>* const owner_;
Packit bd1cd8
  ArgumentMatcherTuple matchers_;
Packit bd1cd8
  Matcher<const ArgumentTuple&> extra_matcher_;
Packit bd1cd8
  Action<F> repeated_action_;
Packit bd1cd8
Packit bd1cd8
  GTEST_DISALLOW_COPY_AND_ASSIGN_(TypedExpectation);
Packit bd1cd8
};  // class TypedExpectation
Packit bd1cd8
Packit bd1cd8
// A MockSpec object is used by ON_CALL() or EXPECT_CALL() for
Packit bd1cd8
// specifying the default behavior of, or expectation on, a mock
Packit bd1cd8
// function.
Packit bd1cd8
Packit bd1cd8
// Note: class MockSpec really belongs to the ::testing namespace.
Packit bd1cd8
// However if we define it in ::testing, MSVC will complain when
Packit bd1cd8
// classes in ::testing::internal declare it as a friend class
Packit bd1cd8
// template.  To workaround this compiler bug, we define MockSpec in
Packit bd1cd8
// ::testing::internal and import it into ::testing.
Packit bd1cd8
Packit bd1cd8
// Logs a message including file and line number information.
Packit bd1cd8
GTEST_API_ void LogWithLocation(testing::internal::LogSeverity severity,
Packit bd1cd8
                                const char* file, int line,
Packit bd1cd8
                                const string& message);
Packit bd1cd8
Packit bd1cd8
template <typename F>
Packit bd1cd8
class MockSpec {
Packit bd1cd8
 public:
Packit bd1cd8
  typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
Packit bd1cd8
  typedef typename internal::Function<F>::ArgumentMatcherTuple
Packit bd1cd8
      ArgumentMatcherTuple;
Packit bd1cd8
Packit bd1cd8
  // Constructs a MockSpec object, given the function mocker object
Packit bd1cd8
  // that the spec is associated with.
Packit bd1cd8
  explicit MockSpec(internal::FunctionMockerBase<F>* function_mocker)
Packit bd1cd8
      : function_mocker_(function_mocker) {}
Packit bd1cd8
Packit bd1cd8
  // Adds a new default action spec to the function mocker and returns
Packit bd1cd8
  // the newly created spec.
Packit bd1cd8
  internal::OnCallSpec<F>& InternalDefaultActionSetAt(
Packit bd1cd8
      const char* file, int line, const char* obj, const char* call) {
Packit bd1cd8
    LogWithLocation(internal::kInfo, file, line,
Packit bd1cd8
        string("ON_CALL(") + obj + ", " + call + ") invoked");
Packit bd1cd8
    return function_mocker_->AddNewOnCallSpec(file, line, matchers_);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Adds a new expectation spec to the function mocker and returns
Packit bd1cd8
  // the newly created spec.
Packit bd1cd8
  internal::TypedExpectation<F>& InternalExpectedAt(
Packit bd1cd8
      const char* file, int line, const char* obj, const char* call) {
Packit bd1cd8
    const string source_text(string("EXPECT_CALL(") + obj + ", " + call + ")");
Packit bd1cd8
    LogWithLocation(internal::kInfo, file, line, source_text + " invoked");
Packit bd1cd8
    return function_mocker_->AddNewExpectation(
Packit bd1cd8
        file, line, source_text, matchers_);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  template <typename Function>
Packit bd1cd8
  friend class internal::FunctionMocker;
Packit bd1cd8
Packit bd1cd8
  void SetMatchers(const ArgumentMatcherTuple& matchers) {
Packit bd1cd8
    matchers_ = matchers;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // The function mocker that owns this spec.
Packit bd1cd8
  internal::FunctionMockerBase<F>* const function_mocker_;
Packit bd1cd8
  // The argument matchers specified in the spec.
Packit bd1cd8
  ArgumentMatcherTuple matchers_;
Packit bd1cd8
Packit bd1cd8
  GTEST_DISALLOW_ASSIGN_(MockSpec);
Packit bd1cd8
};  // class MockSpec
Packit bd1cd8
Packit bd1cd8
// Wrapper type for generically holding an ordinary value or lvalue reference.
Packit bd1cd8
// If T is not a reference type, it must be copyable or movable.
Packit bd1cd8
// ReferenceOrValueWrapper<T> is movable, and will also be copyable unless
Packit bd1cd8
// T is a move-only value type (which means that it will always be copyable
Packit bd1cd8
// if the current platform does not support move semantics).
Packit bd1cd8
//
Packit bd1cd8
// The primary template defines handling for values, but function header
Packit bd1cd8
// comments describe the contract for the whole template (including
Packit bd1cd8
// specializations).
Packit bd1cd8
template <typename T>
Packit bd1cd8
class ReferenceOrValueWrapper {
Packit bd1cd8
 public:
Packit bd1cd8
  // Constructs a wrapper from the given value/reference.
Packit bd1cd8
  explicit ReferenceOrValueWrapper(T value)
Packit bd1cd8
      : value_(::testing::internal::move(value)) {
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Unwraps and returns the underlying value/reference, exactly as
Packit bd1cd8
  // originally passed. The behavior of calling this more than once on
Packit bd1cd8
  // the same object is unspecified.
Packit bd1cd8
  T Unwrap() { return ::testing::internal::move(value_); }
Packit bd1cd8
Packit bd1cd8
  // Provides nondestructive access to the underlying value/reference.
Packit bd1cd8
  // Always returns a const reference (more precisely,
Packit bd1cd8
  // const RemoveReference<T>&). The behavior of calling this after
Packit bd1cd8
  // calling Unwrap on the same object is unspecified.
Packit bd1cd8
  const T& Peek() const {
Packit bd1cd8
    return value_;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  T value_;
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// Specialization for lvalue reference types. See primary template
Packit bd1cd8
// for documentation.
Packit bd1cd8
template <typename T>
Packit bd1cd8
class ReferenceOrValueWrapper<T&> {
Packit bd1cd8
 public:
Packit bd1cd8
  // Workaround for debatable pass-by-reference lint warning (c-library-team
Packit bd1cd8
  // policy precludes NOLINT in this context)
Packit bd1cd8
  typedef T& reference;
Packit bd1cd8
  explicit ReferenceOrValueWrapper(reference ref)
Packit bd1cd8
      : value_ptr_(&ref) {}
Packit bd1cd8
  T& Unwrap() { return *value_ptr_; }
Packit bd1cd8
  const T& Peek() const { return *value_ptr_; }
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  T* value_ptr_;
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// MSVC warns about using 'this' in base member initializer list, so
Packit bd1cd8
// we need to temporarily disable the warning.  We have to do it for
Packit bd1cd8
// the entire class to suppress the warning, even though it's about
Packit bd1cd8
// the constructor only.
Packit bd1cd8
Packit bd1cd8
#ifdef _MSC_VER
Packit bd1cd8
# pragma warning(push)          // Saves the current warning state.
Packit bd1cd8
# pragma warning(disable:4355)  // Temporarily disables warning 4355.
Packit bd1cd8
#endif  // _MSV_VER
Packit bd1cd8
Packit bd1cd8
// C++ treats the void type specially.  For example, you cannot define
Packit bd1cd8
// a void-typed variable or pass a void value to a function.
Packit bd1cd8
// ActionResultHolder<T> holds a value of type T, where T must be a
Packit bd1cd8
// copyable type or void (T doesn't need to be default-constructable).
Packit bd1cd8
// It hides the syntactic difference between void and other types, and
Packit bd1cd8
// is used to unify the code for invoking both void-returning and
Packit bd1cd8
// non-void-returning mock functions.
Packit bd1cd8
Packit bd1cd8
// Untyped base class for ActionResultHolder<T>.
Packit bd1cd8
class UntypedActionResultHolderBase {
Packit bd1cd8
 public:
Packit bd1cd8
  virtual ~UntypedActionResultHolderBase() {}
Packit bd1cd8
Packit bd1cd8
  // Prints the held value as an action's result to os.
Packit bd1cd8
  virtual void PrintAsActionResult(::std::ostream* os) const = 0;
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// This generic definition is used when T is not void.
Packit bd1cd8
template <typename T>
Packit bd1cd8
class ActionResultHolder : public UntypedActionResultHolderBase {
Packit bd1cd8
 public:
Packit bd1cd8
  // Returns the held value. Must not be called more than once.
Packit bd1cd8
  T Unwrap() {
Packit bd1cd8
    return result_.Unwrap();
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Prints the held value as an action's result to os.
Packit bd1cd8
  virtual void PrintAsActionResult(::std::ostream* os) const {
Packit bd1cd8
    *os << "\n          Returns: ";
Packit bd1cd8
    // T may be a reference type, so we don't use UniversalPrint().
Packit bd1cd8
    UniversalPrinter<T>::Print(result_.Peek(), os);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Performs the given mock function's default action and returns the
Packit bd1cd8
  // result in a new-ed ActionResultHolder.
Packit bd1cd8
  template <typename F>
Packit bd1cd8
  static ActionResultHolder* PerformDefaultAction(
Packit bd1cd8
      const FunctionMockerBase<F>* func_mocker,
Packit bd1cd8
      const typename Function<F>::ArgumentTuple& args,
Packit bd1cd8
      const string& call_description) {
Packit bd1cd8
    return new ActionResultHolder(Wrapper(
Packit bd1cd8
        func_mocker->PerformDefaultAction(args, call_description)));
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Performs the given action and returns the result in a new-ed
Packit bd1cd8
  // ActionResultHolder.
Packit bd1cd8
  template <typename F>
Packit bd1cd8
  static ActionResultHolder*
Packit bd1cd8
  PerformAction(const Action<F>& action,
Packit bd1cd8
                const typename Function<F>::ArgumentTuple& args) {
Packit bd1cd8
    return new ActionResultHolder(Wrapper(action.Perform(args)));
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  typedef ReferenceOrValueWrapper<T> Wrapper;
Packit bd1cd8
Packit bd1cd8
  explicit ActionResultHolder(Wrapper result)
Packit bd1cd8
      : result_(::testing::internal::move(result)) {
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  Wrapper result_;
Packit bd1cd8
Packit bd1cd8
  GTEST_DISALLOW_COPY_AND_ASSIGN_(ActionResultHolder);
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// Specialization for T = void.
Packit bd1cd8
template <>
Packit bd1cd8
class ActionResultHolder<void> : public UntypedActionResultHolderBase {
Packit bd1cd8
 public:
Packit bd1cd8
  void Unwrap() { }
Packit bd1cd8
Packit bd1cd8
  virtual void PrintAsActionResult(::std::ostream* /* os */) const {}
Packit bd1cd8
Packit bd1cd8
  // Performs the given mock function's default action and returns ownership
Packit bd1cd8
  // of an empty ActionResultHolder*.
Packit bd1cd8
  template <typename F>
Packit bd1cd8
  static ActionResultHolder* PerformDefaultAction(
Packit bd1cd8
      const FunctionMockerBase<F>* func_mocker,
Packit bd1cd8
      const typename Function<F>::ArgumentTuple& args,
Packit bd1cd8
      const string& call_description) {
Packit bd1cd8
    func_mocker->PerformDefaultAction(args, call_description);
Packit bd1cd8
    return new ActionResultHolder;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Performs the given action and returns ownership of an empty
Packit bd1cd8
  // ActionResultHolder*.
Packit bd1cd8
  template <typename F>
Packit bd1cd8
  static ActionResultHolder* PerformAction(
Packit bd1cd8
      const Action<F>& action,
Packit bd1cd8
      const typename Function<F>::ArgumentTuple& args) {
Packit bd1cd8
    action.Perform(args);
Packit bd1cd8
    return new ActionResultHolder;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  ActionResultHolder() {}
Packit bd1cd8
  GTEST_DISALLOW_COPY_AND_ASSIGN_(ActionResultHolder);
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// The base of the function mocker class for the given function type.
Packit bd1cd8
// We put the methods in this class instead of its child to avoid code
Packit bd1cd8
// bloat.
Packit bd1cd8
template <typename F>
Packit bd1cd8
class FunctionMockerBase : public UntypedFunctionMockerBase {
Packit bd1cd8
 public:
Packit bd1cd8
  typedef typename Function<F>::Result Result;
Packit bd1cd8
  typedef typename Function<F>::ArgumentTuple ArgumentTuple;
Packit bd1cd8
  typedef typename Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple;
Packit bd1cd8
Packit bd1cd8
  FunctionMockerBase() : current_spec_(this) {}
Packit bd1cd8
Packit bd1cd8
  // The destructor verifies that all expectations on this mock
Packit bd1cd8
  // function have been satisfied.  If not, it will report Google Test
Packit bd1cd8
  // non-fatal failures for the violations.
Packit bd1cd8
  virtual ~FunctionMockerBase()
Packit bd1cd8
        GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
Packit bd1cd8
    MutexLock l(&g_gmock_mutex);
Packit bd1cd8
    VerifyAndClearExpectationsLocked();
Packit bd1cd8
    Mock::UnregisterLocked(this);
Packit bd1cd8
    ClearDefaultActionsLocked();
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Returns the ON_CALL spec that matches this mock function with the
Packit bd1cd8
  // given arguments; returns NULL if no matching ON_CALL is found.
Packit bd1cd8
  // L = *
Packit bd1cd8
  const OnCallSpec<F>* FindOnCallSpec(
Packit bd1cd8
      const ArgumentTuple& args) const {
Packit bd1cd8
    for (UntypedOnCallSpecs::const_reverse_iterator it
Packit bd1cd8
             = untyped_on_call_specs_.rbegin();
Packit bd1cd8
         it != untyped_on_call_specs_.rend(); ++it) {
Packit bd1cd8
      const OnCallSpec<F>* spec = static_cast<const OnCallSpec<F>*>(*it);
Packit bd1cd8
      if (spec->Matches(args))
Packit bd1cd8
        return spec;
Packit bd1cd8
    }
Packit bd1cd8
Packit bd1cd8
    return NULL;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Performs the default action of this mock function on the given
Packit bd1cd8
  // arguments and returns the result. Asserts (or throws if
Packit bd1cd8
  // exceptions are enabled) with a helpful call descrption if there
Packit bd1cd8
  // is no valid return value. This method doesn't depend on the
Packit bd1cd8
  // mutable state of this object, and thus can be called concurrently
Packit bd1cd8
  // without locking.
Packit bd1cd8
  // L = *
Packit bd1cd8
  Result PerformDefaultAction(const ArgumentTuple& args,
Packit bd1cd8
                              const string& call_description) const {
Packit bd1cd8
    const OnCallSpec<F>* const spec =
Packit bd1cd8
        this->FindOnCallSpec(args);
Packit bd1cd8
    if (spec != NULL) {
Packit bd1cd8
      return spec->GetAction().Perform(args);
Packit bd1cd8
    }
Packit bd1cd8
    const string message = call_description +
Packit bd1cd8
        "\n    The mock function has no default action "
Packit bd1cd8
        "set, and its return type has no default value set.";
Packit bd1cd8
#if GTEST_HAS_EXCEPTIONS
Packit bd1cd8
    if (!DefaultValue<Result>::Exists()) {
Packit bd1cd8
      throw std::runtime_error(message);
Packit bd1cd8
    }
Packit bd1cd8
#else
Packit bd1cd8
    Assert(DefaultValue<Result>::Exists(), "", -1, message);
Packit bd1cd8
#endif
Packit bd1cd8
    return DefaultValue<Result>::Get();
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Performs the default action with the given arguments and returns
Packit bd1cd8
  // the action's result.  The call description string will be used in
Packit bd1cd8
  // the error message to describe the call in the case the default
Packit bd1cd8
  // action fails.  The caller is responsible for deleting the result.
Packit bd1cd8
  // L = *
Packit bd1cd8
  virtual UntypedActionResultHolderBase* UntypedPerformDefaultAction(
Packit bd1cd8
      const void* untyped_args,  // must point to an ArgumentTuple
Packit bd1cd8
      const string& call_description) const {
Packit bd1cd8
    const ArgumentTuple& args =
Packit bd1cd8
        *static_cast<const ArgumentTuple*>(untyped_args);
Packit bd1cd8
    return ResultHolder::PerformDefaultAction(this, args, call_description);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Performs the given action with the given arguments and returns
Packit bd1cd8
  // the action's result.  The caller is responsible for deleting the
Packit bd1cd8
  // result.
Packit bd1cd8
  // L = *
Packit bd1cd8
  virtual UntypedActionResultHolderBase* UntypedPerformAction(
Packit bd1cd8
      const void* untyped_action, const void* untyped_args) const {
Packit bd1cd8
    // Make a copy of the action before performing it, in case the
Packit bd1cd8
    // action deletes the mock object (and thus deletes itself).
Packit bd1cd8
    const Action<F> action = *static_cast<const Action<F>*>(untyped_action);
Packit bd1cd8
    const ArgumentTuple& args =
Packit bd1cd8
        *static_cast<const ArgumentTuple*>(untyped_args);
Packit bd1cd8
    return ResultHolder::PerformAction(action, args);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Implements UntypedFunctionMockerBase::ClearDefaultActionsLocked():
Packit bd1cd8
  // clears the ON_CALL()s set on this mock function.
Packit bd1cd8
  virtual void ClearDefaultActionsLocked()
Packit bd1cd8
      GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
Packit bd1cd8
    g_gmock_mutex.AssertHeld();
Packit bd1cd8
Packit bd1cd8
    // Deleting our default actions may trigger other mock objects to be
Packit bd1cd8
    // deleted, for example if an action contains a reference counted smart
Packit bd1cd8
    // pointer to that mock object, and that is the last reference. So if we
Packit bd1cd8
    // delete our actions within the context of the global mutex we may deadlock
Packit bd1cd8
    // when this method is called again. Instead, make a copy of the set of
Packit bd1cd8
    // actions to delete, clear our set within the mutex, and then delete the
Packit bd1cd8
    // actions outside of the mutex.
Packit bd1cd8
    UntypedOnCallSpecs specs_to_delete;
Packit bd1cd8
    untyped_on_call_specs_.swap(specs_to_delete);
Packit bd1cd8
Packit bd1cd8
    g_gmock_mutex.Unlock();
Packit bd1cd8
    for (UntypedOnCallSpecs::const_iterator it =
Packit bd1cd8
             specs_to_delete.begin();
Packit bd1cd8
         it != specs_to_delete.end(); ++it) {
Packit bd1cd8
      delete static_cast<const OnCallSpec<F>*>(*it);
Packit bd1cd8
    }
Packit bd1cd8
Packit bd1cd8
    // Lock the mutex again, since the caller expects it to be locked when we
Packit bd1cd8
    // return.
Packit bd1cd8
    g_gmock_mutex.Lock();
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
 protected:
Packit bd1cd8
  template <typename Function>
Packit bd1cd8
  friend class MockSpec;
Packit bd1cd8
Packit bd1cd8
  typedef ActionResultHolder<Result> ResultHolder;
Packit bd1cd8
Packit bd1cd8
  // Returns the result of invoking this mock function with the given
Packit bd1cd8
  // arguments.  This function can be safely called from multiple
Packit bd1cd8
  // threads concurrently.
Packit bd1cd8
  Result InvokeWith(const ArgumentTuple& args)
Packit bd1cd8
        GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
Packit bd1cd8
    scoped_ptr<ResultHolder> holder(
Packit bd1cd8
        DownCast_<ResultHolder*>(this->UntypedInvokeWith(&args)));
Packit bd1cd8
    return holder->Unwrap();
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Adds and returns a default action spec for this mock function.
Packit bd1cd8
  OnCallSpec<F>& AddNewOnCallSpec(
Packit bd1cd8
      const char* file, int line,
Packit bd1cd8
      const ArgumentMatcherTuple& m)
Packit bd1cd8
          GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
Packit bd1cd8
    Mock::RegisterUseByOnCallOrExpectCall(MockObject(), file, line);
Packit bd1cd8
    OnCallSpec<F>* const on_call_spec = new OnCallSpec<F>(file, line, m);
Packit bd1cd8
    untyped_on_call_specs_.push_back(on_call_spec);
Packit bd1cd8
    return *on_call_spec;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Adds and returns an expectation spec for this mock function.
Packit bd1cd8
  TypedExpectation<F>& AddNewExpectation(
Packit bd1cd8
      const char* file,
Packit bd1cd8
      int line,
Packit bd1cd8
      const string& source_text,
Packit bd1cd8
      const ArgumentMatcherTuple& m)
Packit bd1cd8
          GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
Packit bd1cd8
    Mock::RegisterUseByOnCallOrExpectCall(MockObject(), file, line);
Packit bd1cd8
    TypedExpectation<F>* const expectation =
Packit bd1cd8
        new TypedExpectation<F>(this, file, line, source_text, m);
Packit bd1cd8
    const linked_ptr<ExpectationBase> untyped_expectation(expectation);
Packit bd1cd8
    untyped_expectations_.push_back(untyped_expectation);
Packit bd1cd8
Packit bd1cd8
    // Adds this expectation into the implicit sequence if there is one.
Packit bd1cd8
    Sequence* const implicit_sequence = g_gmock_implicit_sequence.get();
Packit bd1cd8
    if (implicit_sequence != NULL) {
Packit bd1cd8
      implicit_sequence->AddExpectation(Expectation(untyped_expectation));
Packit bd1cd8
    }
Packit bd1cd8
Packit bd1cd8
    return *expectation;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // The current spec (either default action spec or expectation spec)
Packit bd1cd8
  // being described on this function mocker.
Packit bd1cd8
  MockSpec<F>& current_spec() { return current_spec_; }
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  template <typename Func> friend class TypedExpectation;
Packit bd1cd8
Packit bd1cd8
  // Some utilities needed for implementing UntypedInvokeWith().
Packit bd1cd8
Packit bd1cd8
  // Describes what default action will be performed for the given
Packit bd1cd8
  // arguments.
Packit bd1cd8
  // L = *
Packit bd1cd8
  void DescribeDefaultActionTo(const ArgumentTuple& args,
Packit bd1cd8
                               ::std::ostream* os) const {
Packit bd1cd8
    const OnCallSpec<F>* const spec = FindOnCallSpec(args);
Packit bd1cd8
Packit bd1cd8
    if (spec == NULL) {
Packit bd1cd8
      *os << (internal::type_equals<Result, void>::value ?
Packit bd1cd8
              "returning directly.\n" :
Packit bd1cd8
              "returning default value.\n");
Packit bd1cd8
    } else {
Packit bd1cd8
      *os << "taking default action specified at:\n"
Packit bd1cd8
          << FormatFileLocation(spec->file(), spec->line()) << "\n";
Packit bd1cd8
    }
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Writes a message that the call is uninteresting (i.e. neither
Packit bd1cd8
  // explicitly expected nor explicitly unexpected) to the given
Packit bd1cd8
  // ostream.
Packit bd1cd8
  virtual void UntypedDescribeUninterestingCall(
Packit bd1cd8
      const void* untyped_args,
Packit bd1cd8
      ::std::ostream* os) const
Packit bd1cd8
          GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
Packit bd1cd8
    const ArgumentTuple& args =
Packit bd1cd8
        *static_cast<const ArgumentTuple*>(untyped_args);
Packit bd1cd8
    *os << "Uninteresting mock function call - ";
Packit bd1cd8
    DescribeDefaultActionTo(args, os);
Packit bd1cd8
    *os << "    Function call: " << Name();
Packit bd1cd8
    UniversalPrint(args, os);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Returns the expectation that matches the given function arguments
Packit bd1cd8
  // (or NULL is there's no match); when a match is found,
Packit bd1cd8
  // untyped_action is set to point to the action that should be
Packit bd1cd8
  // performed (or NULL if the action is "do default"), and
Packit bd1cd8
  // is_excessive is modified to indicate whether the call exceeds the
Packit bd1cd8
  // expected number.
Packit bd1cd8
  //
Packit bd1cd8
  // Critical section: We must find the matching expectation and the
Packit bd1cd8
  // corresponding action that needs to be taken in an ATOMIC
Packit bd1cd8
  // transaction.  Otherwise another thread may call this mock
Packit bd1cd8
  // method in the middle and mess up the state.
Packit bd1cd8
  //
Packit bd1cd8
  // However, performing the action has to be left out of the critical
Packit bd1cd8
  // section.  The reason is that we have no control on what the
Packit bd1cd8
  // action does (it can invoke an arbitrary user function or even a
Packit bd1cd8
  // mock function) and excessive locking could cause a dead lock.
Packit bd1cd8
  virtual const ExpectationBase* UntypedFindMatchingExpectation(
Packit bd1cd8
      const void* untyped_args,
Packit bd1cd8
      const void** untyped_action, bool* is_excessive,
Packit bd1cd8
      ::std::ostream* what, ::std::ostream* why)
Packit bd1cd8
          GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
Packit bd1cd8
    const ArgumentTuple& args =
Packit bd1cd8
        *static_cast<const ArgumentTuple*>(untyped_args);
Packit bd1cd8
    MutexLock l(&g_gmock_mutex);
Packit bd1cd8
    TypedExpectation<F>* exp = this->FindMatchingExpectationLocked(args);
Packit bd1cd8
    if (exp == NULL) {  // A match wasn't found.
Packit bd1cd8
      this->FormatUnexpectedCallMessageLocked(args, what, why);
Packit bd1cd8
      return NULL;
Packit bd1cd8
    }
Packit bd1cd8
Packit bd1cd8
    // This line must be done before calling GetActionForArguments(),
Packit bd1cd8
    // which will increment the call count for *exp and thus affect
Packit bd1cd8
    // its saturation status.
Packit bd1cd8
    *is_excessive = exp->IsSaturated();
Packit bd1cd8
    const Action<F>* action = exp->GetActionForArguments(this, args, what, why);
Packit bd1cd8
    if (action != NULL && action->IsDoDefault())
Packit bd1cd8
      action = NULL;  // Normalize "do default" to NULL.
Packit bd1cd8
    *untyped_action = action;
Packit bd1cd8
    return exp;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Prints the given function arguments to the ostream.
Packit bd1cd8
  virtual void UntypedPrintArgs(const void* untyped_args,
Packit bd1cd8
                                ::std::ostream* os) const {
Packit bd1cd8
    const ArgumentTuple& args =
Packit bd1cd8
        *static_cast<const ArgumentTuple*>(untyped_args);
Packit bd1cd8
    UniversalPrint(args, os);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Returns the expectation that matches the arguments, or NULL if no
Packit bd1cd8
  // expectation matches them.
Packit bd1cd8
  TypedExpectation<F>* FindMatchingExpectationLocked(
Packit bd1cd8
      const ArgumentTuple& args) const
Packit bd1cd8
          GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
Packit bd1cd8
    g_gmock_mutex.AssertHeld();
Packit bd1cd8
    for (typename UntypedExpectations::const_reverse_iterator it =
Packit bd1cd8
             untyped_expectations_.rbegin();
Packit bd1cd8
         it != untyped_expectations_.rend(); ++it) {
Packit bd1cd8
      TypedExpectation<F>* const exp =
Packit bd1cd8
          static_cast<TypedExpectation<F>*>(it->get());
Packit bd1cd8
      if (exp->ShouldHandleArguments(args)) {
Packit bd1cd8
        return exp;
Packit bd1cd8
      }
Packit bd1cd8
    }
Packit bd1cd8
    return NULL;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Returns a message that the arguments don't match any expectation.
Packit bd1cd8
  void FormatUnexpectedCallMessageLocked(
Packit bd1cd8
      const ArgumentTuple& args,
Packit bd1cd8
      ::std::ostream* os,
Packit bd1cd8
      ::std::ostream* why) const
Packit bd1cd8
          GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
Packit bd1cd8
    g_gmock_mutex.AssertHeld();
Packit bd1cd8
    *os << "\nUnexpected mock function call - ";
Packit bd1cd8
    DescribeDefaultActionTo(args, os);
Packit bd1cd8
    PrintTriedExpectationsLocked(args, why);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Prints a list of expectations that have been tried against the
Packit bd1cd8
  // current mock function call.
Packit bd1cd8
  void PrintTriedExpectationsLocked(
Packit bd1cd8
      const ArgumentTuple& args,
Packit bd1cd8
      ::std::ostream* why) const
Packit bd1cd8
          GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
Packit bd1cd8
    g_gmock_mutex.AssertHeld();
Packit bd1cd8
    const int count = static_cast<int>(untyped_expectations_.size());
Packit bd1cd8
    *why << "Google Mock tried the following " << count << " "
Packit bd1cd8
         << (count == 1 ? "expectation, but it didn't match" :
Packit bd1cd8
             "expectations, but none matched")
Packit bd1cd8
         << ":\n";
Packit bd1cd8
    for (int i = 0; i < count; i++) {
Packit bd1cd8
      TypedExpectation<F>* const expectation =
Packit bd1cd8
          static_cast<TypedExpectation<F>*>(untyped_expectations_[i].get());
Packit bd1cd8
      *why << "\n";
Packit bd1cd8
      expectation->DescribeLocationTo(why);
Packit bd1cd8
      if (count > 1) {
Packit bd1cd8
        *why << "tried expectation #" << i << ": ";
Packit bd1cd8
      }
Packit bd1cd8
      *why << expectation->source_text() << "...\n";
Packit bd1cd8
      expectation->ExplainMatchResultTo(args, why);
Packit bd1cd8
      expectation->DescribeCallCountTo(why);
Packit bd1cd8
    }
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // The current spec (either default action spec or expectation spec)
Packit bd1cd8
  // being described on this function mocker.
Packit bd1cd8
  MockSpec<F> current_spec_;
Packit bd1cd8
Packit bd1cd8
  // There is no generally useful and implementable semantics of
Packit bd1cd8
  // copying a mock object, so copying a mock is usually a user error.
Packit bd1cd8
  // Thus we disallow copying function mockers.  If the user really
Packit bd1cd8
  // wants to copy a mock object, he should implement his own copy
Packit bd1cd8
  // operation, for example:
Packit bd1cd8
  //
Packit bd1cd8
  //   class MockFoo : public Foo {
Packit bd1cd8
  //    public:
Packit bd1cd8
  //     // Defines a copy constructor explicitly.
Packit bd1cd8
  //     MockFoo(const MockFoo& src) {}
Packit bd1cd8
  //     ...
Packit bd1cd8
  //   };
Packit bd1cd8
  GTEST_DISALLOW_COPY_AND_ASSIGN_(FunctionMockerBase);
Packit bd1cd8
};  // class FunctionMockerBase
Packit bd1cd8
Packit bd1cd8
#ifdef _MSC_VER
Packit bd1cd8
# pragma warning(pop)  // Restores the warning state.
Packit bd1cd8
#endif  // _MSV_VER
Packit bd1cd8
Packit bd1cd8
// Implements methods of FunctionMockerBase.
Packit bd1cd8
Packit bd1cd8
// Verifies that all expectations on this mock function have been
Packit bd1cd8
// satisfied.  Reports one or more Google Test non-fatal failures and
Packit bd1cd8
// returns false if not.
Packit bd1cd8
Packit bd1cd8
// Reports an uninteresting call (whose description is in msg) in the
Packit bd1cd8
// manner specified by 'reaction'.
Packit bd1cd8
void ReportUninterestingCall(CallReaction reaction, const string& msg);
Packit bd1cd8
Packit bd1cd8
}  // namespace internal
Packit bd1cd8
Packit bd1cd8
// The style guide prohibits "using" statements in a namespace scope
Packit bd1cd8
// inside a header file.  However, the MockSpec class template is
Packit bd1cd8
// meant to be defined in the ::testing namespace.  The following line
Packit bd1cd8
// is just a trick for working around a bug in MSVC 8.0, which cannot
Packit bd1cd8
// handle it if we define MockSpec in ::testing.
Packit bd1cd8
using internal::MockSpec;
Packit bd1cd8
Packit bd1cd8
// Const(x) is a convenient function for obtaining a const reference
Packit bd1cd8
// to x.  This is useful for setting expectations on an overloaded
Packit bd1cd8
// const mock method, e.g.
Packit bd1cd8
//
Packit bd1cd8
//   class MockFoo : public FooInterface {
Packit bd1cd8
//    public:
Packit bd1cd8
//     MOCK_METHOD0(Bar, int());
Packit bd1cd8
//     MOCK_CONST_METHOD0(Bar, int&());
Packit bd1cd8
//   };
Packit bd1cd8
//
Packit bd1cd8
//   MockFoo foo;
Packit bd1cd8
//   // Expects a call to non-const MockFoo::Bar().
Packit bd1cd8
//   EXPECT_CALL(foo, Bar());
Packit bd1cd8
//   // Expects a call to const MockFoo::Bar().
Packit bd1cd8
//   EXPECT_CALL(Const(foo), Bar());
Packit bd1cd8
template <typename T>
Packit bd1cd8
inline const T& Const(const T& x) { return x; }
Packit bd1cd8
Packit bd1cd8
// Constructs an Expectation object that references and co-owns exp.
Packit bd1cd8
inline Expectation::Expectation(internal::ExpectationBase& exp)  // NOLINT
Packit bd1cd8
    : expectation_base_(exp.GetHandle().expectation_base()) {}
Packit bd1cd8
Packit bd1cd8
}  // namespace testing
Packit bd1cd8
Packit bd1cd8
// A separate macro is required to avoid compile errors when the name
Packit bd1cd8
// of the method used in call is a result of macro expansion.
Packit bd1cd8
// See CompilesWithMethodNameExpandedFromMacro tests in
Packit bd1cd8
// internal/gmock-spec-builders_test.cc for more details.
Packit bd1cd8
#define GMOCK_ON_CALL_IMPL_(obj, call) \
Packit bd1cd8
    ((obj).gmock_##call).InternalDefaultActionSetAt(__FILE__, __LINE__, \
Packit bd1cd8
                                                    #obj, #call)
Packit bd1cd8
#define ON_CALL(obj, call) GMOCK_ON_CALL_IMPL_(obj, call)
Packit bd1cd8
Packit bd1cd8
#define GMOCK_EXPECT_CALL_IMPL_(obj, call) \
Packit bd1cd8
    ((obj).gmock_##call).InternalExpectedAt(__FILE__, __LINE__, #obj, #call)
Packit bd1cd8
#define EXPECT_CALL(obj, call) GMOCK_EXPECT_CALL_IMPL_(obj, call)
Packit bd1cd8
Packit bd1cd8
#endif  // GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_