2008-12-10 05:08:54 +00:00
|
|
|
// Copyright 2007, Google Inc.
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following disclaimer
|
|
|
|
// in the documentation and/or other materials provided with the
|
|
|
|
// distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived from
|
|
|
|
// this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2018-08-14 18:04:07 +00:00
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
// Google Mock - a framework for writing C++ mock classes.
|
|
|
|
//
|
|
|
|
// This file implements the ON_CALL() and EXPECT_CALL() macros.
|
|
|
|
//
|
|
|
|
// A user can use the ON_CALL() macro to specify the default action of
|
|
|
|
// a mock method. The syntax is:
|
|
|
|
//
|
|
|
|
// ON_CALL(mock_object, Method(argument-matchers))
|
2009-06-09 06:09:53 +00:00
|
|
|
// .With(multi-argument-matcher)
|
2008-12-10 05:08:54 +00:00
|
|
|
// .WillByDefault(action);
|
|
|
|
//
|
2009-06-09 06:09:53 +00:00
|
|
|
// where the .With() clause is optional.
|
2008-12-10 05:08:54 +00:00
|
|
|
//
|
|
|
|
// A user can use the EXPECT_CALL() macro to specify an expectation on
|
|
|
|
// a mock method. The syntax is:
|
|
|
|
//
|
|
|
|
// EXPECT_CALL(mock_object, Method(argument-matchers))
|
2009-06-09 06:09:53 +00:00
|
|
|
// .With(multi-argument-matchers)
|
2008-12-10 05:08:54 +00:00
|
|
|
// .Times(cardinality)
|
|
|
|
// .InSequence(sequences)
|
2009-07-01 19:04:51 +00:00
|
|
|
// .After(expectations)
|
2008-12-10 05:08:54 +00:00
|
|
|
// .WillOnce(action)
|
|
|
|
// .WillRepeatedly(action)
|
|
|
|
// .RetiresOnSaturation();
|
|
|
|
//
|
2009-07-01 19:04:51 +00:00
|
|
|
// where all clauses are optional, and .InSequence()/.After()/
|
|
|
|
// .WillOnce() can appear any number of times.
|
2008-12-10 05:08:54 +00:00
|
|
|
|
2018-07-27 15:15:08 +00:00
|
|
|
// GOOGLETEST_CM0002 DO NOT DELETE
|
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
#ifndef GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_
|
|
|
|
#define GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_
|
|
|
|
|
2019-07-11 07:49:59 +00:00
|
|
|
#include <functional>
|
2008-12-10 05:08:54 +00:00
|
|
|
#include <map>
|
2018-10-29 15:09:33 +00:00
|
|
|
#include <memory>
|
2008-12-10 05:08:54 +00:00
|
|
|
#include <set>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
2018-10-16 19:29:37 +00:00
|
|
|
#include <utility>
|
2008-12-10 05:08:54 +00:00
|
|
|
#include <vector>
|
2010-09-14 05:38:21 +00:00
|
|
|
#include "gmock/gmock-actions.h"
|
|
|
|
#include "gmock/gmock-cardinalities.h"
|
|
|
|
#include "gmock/gmock-matchers.h"
|
|
|
|
#include "gmock/internal/gmock-internal-utils.h"
|
|
|
|
#include "gmock/internal/gmock-port.h"
|
|
|
|
#include "gtest/gtest.h"
|
2008-12-10 05:08:54 +00:00
|
|
|
|
2018-01-26 16:57:58 +00:00
|
|
|
#if GTEST_HAS_EXCEPTIONS
|
|
|
|
# include <stdexcept> // NOLINT
|
|
|
|
#endif
|
|
|
|
|
2018-08-20 18:48:45 +00:00
|
|
|
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
|
|
|
|
/* class A needs to have dll-interface to be used by clients of class B */)
|
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
namespace testing {
|
|
|
|
|
2009-07-01 19:04:51 +00:00
|
|
|
// An abstract handle of an expectation.
|
|
|
|
class Expectation;
|
|
|
|
|
|
|
|
// A set of expectation handles.
|
|
|
|
class ExpectationSet;
|
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
// Anything inside the 'internal' namespace IS INTERNAL IMPLEMENTATION
|
|
|
|
// and MUST NOT BE USED IN USER CODE!!!
|
|
|
|
namespace internal {
|
|
|
|
|
2009-07-01 19:04:51 +00:00
|
|
|
// Implements a mock function.
|
|
|
|
template <typename F> class FunctionMocker;
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
// Base class for expectations.
|
|
|
|
class ExpectationBase;
|
|
|
|
|
2009-07-01 19:04:51 +00:00
|
|
|
// Implements an expectation.
|
|
|
|
template <typename F> class TypedExpectation;
|
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
// Helper class for testing the Expectation class template.
|
|
|
|
class ExpectationTester;
|
|
|
|
|
|
|
|
// Protects the mock object registry (in class Mock), all function
|
|
|
|
// mockers, and all expectations.
|
|
|
|
//
|
|
|
|
// The reason we don't use more fine-grained protection is: when a
|
|
|
|
// mock function Foo() is called, it needs to consult its expectations
|
|
|
|
// to see which one should be picked. If another thread is allowed to
|
|
|
|
// call a mock function (either Foo() or a different one) at the same
|
|
|
|
// time, it could affect the "retired" attributes of Foo()'s
|
|
|
|
// expectations when InSequence() is used, and thus affect which
|
|
|
|
// expectation gets picked. Therefore, we sequence all mock function
|
|
|
|
// calls to ensure the integrity of the mock objects' states.
|
2011-05-20 00:42:22 +00:00
|
|
|
GTEST_API_ GTEST_DECLARE_STATIC_MUTEX_(g_gmock_mutex);
|
2008-12-10 05:08:54 +00:00
|
|
|
|
2011-02-23 19:39:27 +00:00
|
|
|
// Untyped base class for ActionResultHolder<R>.
|
|
|
|
class UntypedActionResultHolderBase;
|
|
|
|
|
2018-11-08 16:14:50 +00:00
|
|
|
// Abstract base class of FunctionMocker. This is the
|
2008-12-10 05:08:54 +00:00
|
|
|
// type-agnostic part of the function mocker interface. Its pure
|
2018-11-08 16:14:50 +00:00
|
|
|
// virtual methods are implemented by FunctionMocker.
|
2011-05-20 00:42:22 +00:00
|
|
|
class GTEST_API_ UntypedFunctionMockerBase {
|
2008-12-10 05:08:54 +00:00
|
|
|
public:
|
2011-02-23 19:39:27 +00:00
|
|
|
UntypedFunctionMockerBase();
|
|
|
|
virtual ~UntypedFunctionMockerBase();
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
// Verifies that all expectations on this mock function have been
|
|
|
|
// satisfied. Reports one or more Google Test non-fatal failures
|
|
|
|
// and returns false if not.
|
2011-10-24 21:16:22 +00:00
|
|
|
bool VerifyAndClearExpectationsLocked()
|
|
|
|
GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex);
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
// Clears the ON_CALL()s set on this mock function.
|
2011-10-24 21:16:22 +00:00
|
|
|
virtual void ClearDefaultActionsLocked()
|
|
|
|
GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) = 0;
|
2011-02-23 19:39:27 +00:00
|
|
|
|
|
|
|
// In all of the following Untyped* functions, it's the caller's
|
|
|
|
// responsibility to guarantee the correctness of the arguments'
|
|
|
|
// types.
|
|
|
|
|
|
|
|
// Performs the default action with the given arguments and returns
|
|
|
|
// the action's result. The call description string will be used in
|
|
|
|
// the error message to describe the call in the case the default
|
|
|
|
// action fails.
|
|
|
|
// L = *
|
|
|
|
virtual UntypedActionResultHolderBase* UntypedPerformDefaultAction(
|
2018-04-05 20:09:17 +00:00
|
|
|
void* untyped_args, const std::string& call_description) const = 0;
|
2011-02-23 19:39:27 +00:00
|
|
|
|
|
|
|
// Performs the given action with the given arguments and returns
|
|
|
|
// the action's result.
|
|
|
|
// L = *
|
|
|
|
virtual UntypedActionResultHolderBase* UntypedPerformAction(
|
2018-04-05 20:09:17 +00:00
|
|
|
const void* untyped_action, void* untyped_args) const = 0;
|
2011-02-23 19:39:27 +00:00
|
|
|
|
|
|
|
// Writes a message that the call is uninteresting (i.e. neither
|
|
|
|
// explicitly expected nor explicitly unexpected) to the given
|
|
|
|
// ostream.
|
2011-10-24 21:16:22 +00:00
|
|
|
virtual void UntypedDescribeUninterestingCall(
|
|
|
|
const void* untyped_args,
|
|
|
|
::std::ostream* os) const
|
|
|
|
GTEST_LOCK_EXCLUDED_(g_gmock_mutex) = 0;
|
2011-02-23 19:39:27 +00:00
|
|
|
|
|
|
|
// Returns the expectation that matches the given function arguments
|
|
|
|
// (or NULL is there's no match); when a match is found,
|
|
|
|
// untyped_action is set to point to the action that should be
|
|
|
|
// performed (or NULL if the action is "do default"), and
|
|
|
|
// is_excessive is modified to indicate whether the call exceeds the
|
|
|
|
// expected number.
|
|
|
|
virtual const ExpectationBase* UntypedFindMatchingExpectation(
|
|
|
|
const void* untyped_args,
|
|
|
|
const void** untyped_action, bool* is_excessive,
|
2011-10-24 21:16:22 +00:00
|
|
|
::std::ostream* what, ::std::ostream* why)
|
|
|
|
GTEST_LOCK_EXCLUDED_(g_gmock_mutex) = 0;
|
2011-02-23 19:39:27 +00:00
|
|
|
|
|
|
|
// Prints the given function arguments to the ostream.
|
|
|
|
virtual void UntypedPrintArgs(const void* untyped_args,
|
|
|
|
::std::ostream* os) const = 0;
|
|
|
|
|
|
|
|
// Sets the mock object this mock method belongs to, and registers
|
|
|
|
// this information in the global mock registry. Will be called
|
|
|
|
// whenever an EXPECT_CALL() or ON_CALL() is executed on this mock
|
|
|
|
// method.
|
2011-10-24 21:16:22 +00:00
|
|
|
void RegisterOwner(const void* mock_obj)
|
|
|
|
GTEST_LOCK_EXCLUDED_(g_gmock_mutex);
|
2011-02-23 19:39:27 +00:00
|
|
|
|
|
|
|
// Sets the mock object this mock method belongs to, and sets the
|
|
|
|
// name of the mock function. Will be called upon each invocation
|
|
|
|
// of this mock function.
|
2011-10-24 21:16:22 +00:00
|
|
|
void SetOwnerAndName(const void* mock_obj, const char* name)
|
|
|
|
GTEST_LOCK_EXCLUDED_(g_gmock_mutex);
|
2011-02-23 19:39:27 +00:00
|
|
|
|
|
|
|
// Returns the mock object this mock method belongs to. Must be
|
|
|
|
// called after RegisterOwner() or SetOwnerAndName() has been
|
|
|
|
// called.
|
2011-10-24 21:16:22 +00:00
|
|
|
const void* MockObject() const
|
|
|
|
GTEST_LOCK_EXCLUDED_(g_gmock_mutex);
|
2011-02-23 19:39:27 +00:00
|
|
|
|
|
|
|
// Returns the name of this mock method. Must be called after
|
|
|
|
// SetOwnerAndName() has been called.
|
2011-10-24 21:16:22 +00:00
|
|
|
const char* Name() const
|
|
|
|
GTEST_LOCK_EXCLUDED_(g_gmock_mutex);
|
2011-02-23 19:39:27 +00:00
|
|
|
|
|
|
|
// Returns the result of invoking this mock function with the given
|
|
|
|
// arguments. This function can be safely called from multiple
|
|
|
|
// threads concurrently. The caller is responsible for deleting the
|
|
|
|
// result.
|
2018-04-05 20:09:17 +00:00
|
|
|
UntypedActionResultHolderBase* UntypedInvokeWith(void* untyped_args)
|
|
|
|
GTEST_LOCK_EXCLUDED_(g_gmock_mutex);
|
2011-02-23 19:39:27 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
typedef std::vector<const void*> UntypedOnCallSpecs;
|
|
|
|
|
2018-10-29 15:09:33 +00:00
|
|
|
using UntypedExpectations = std::vector<std::shared_ptr<ExpectationBase>>;
|
2011-02-23 19:39:27 +00:00
|
|
|
|
|
|
|
// Returns an Expectation object that references and co-owns exp,
|
|
|
|
// which must be an expectation on this mock function.
|
|
|
|
Expectation GetHandleOf(ExpectationBase* exp);
|
|
|
|
|
|
|
|
// Address of the mock object this mock method belongs to. Only
|
|
|
|
// valid after this mock method has been called or
|
|
|
|
// ON_CALL/EXPECT_CALL has been invoked on it.
|
|
|
|
const void* mock_obj_; // Protected by g_gmock_mutex.
|
|
|
|
|
|
|
|
// Name of the function being mocked. Only valid after this mock
|
|
|
|
// method has been called.
|
|
|
|
const char* name_; // Protected by g_gmock_mutex.
|
|
|
|
|
|
|
|
// All default action specs for this function mocker.
|
|
|
|
UntypedOnCallSpecs untyped_on_call_specs_;
|
|
|
|
|
|
|
|
// All expectations for this function mocker.
|
2018-04-05 20:09:17 +00:00
|
|
|
//
|
|
|
|
// It's undefined behavior to interleave expectations (EXPECT_CALLs
|
|
|
|
// or ON_CALLs) and mock function calls. Also, the order of
|
|
|
|
// expectations is important. Therefore it's a logic race condition
|
|
|
|
// to read/write untyped_expectations_ concurrently. In order for
|
|
|
|
// tools like tsan to catch concurrent read/write accesses to
|
|
|
|
// untyped_expectations, we deliberately leave accesses to it
|
|
|
|
// unprotected.
|
2011-02-23 19:39:27 +00:00
|
|
|
UntypedExpectations untyped_expectations_;
|
2008-12-10 05:08:54 +00:00
|
|
|
}; // class UntypedFunctionMockerBase
|
|
|
|
|
2011-02-23 19:39:27 +00:00
|
|
|
// Untyped base class for OnCallSpec<F>.
|
|
|
|
class UntypedOnCallSpecBase {
|
|
|
|
public:
|
|
|
|
// The arguments are the location of the ON_CALL() statement.
|
|
|
|
UntypedOnCallSpecBase(const char* a_file, int a_line)
|
|
|
|
: file_(a_file), line_(a_line), last_clause_(kNone) {}
|
|
|
|
|
|
|
|
// Where in the source file was the default action spec defined?
|
|
|
|
const char* file() const { return file_; }
|
|
|
|
int line() const { return line_; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// Gives each clause in the ON_CALL() statement a name.
|
|
|
|
enum Clause {
|
|
|
|
// Do not change the order of the enum members! The run-time
|
|
|
|
// syntax checking relies on it.
|
|
|
|
kNone,
|
|
|
|
kWith,
|
2011-04-08 01:32:32 +00:00
|
|
|
kWillByDefault
|
2011-02-23 19:39:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Asserts that the ON_CALL() statement has a certain property.
|
2017-05-15 21:07:03 +00:00
|
|
|
void AssertSpecProperty(bool property,
|
|
|
|
const std::string& failure_message) const {
|
2011-02-23 19:39:27 +00:00
|
|
|
Assert(property, file_, line_, failure_message);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expects that the ON_CALL() statement has a certain property.
|
2017-05-15 21:07:03 +00:00
|
|
|
void ExpectSpecProperty(bool property,
|
|
|
|
const std::string& failure_message) const {
|
2011-02-23 19:39:27 +00:00
|
|
|
Expect(property, file_, line_, failure_message);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* file_;
|
|
|
|
int line_;
|
|
|
|
|
|
|
|
// The last clause in the ON_CALL() statement as seen so far.
|
|
|
|
// Initially kNone and changes as the statement is parsed.
|
|
|
|
Clause last_clause_;
|
|
|
|
}; // class UntypedOnCallSpecBase
|
|
|
|
|
|
|
|
// This template class implements an ON_CALL spec.
|
2008-12-10 05:08:54 +00:00
|
|
|
template <typename F>
|
2011-02-23 19:39:27 +00:00
|
|
|
class OnCallSpec : public UntypedOnCallSpecBase {
|
2008-12-10 05:08:54 +00:00
|
|
|
public:
|
|
|
|
typedef typename Function<F>::ArgumentTuple ArgumentTuple;
|
|
|
|
typedef typename Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple;
|
|
|
|
|
2011-02-23 19:39:27 +00:00
|
|
|
// Constructs an OnCallSpec object from the information inside
|
2008-12-10 05:08:54 +00:00
|
|
|
// the parenthesis of an ON_CALL() statement.
|
2011-02-23 19:39:27 +00:00
|
|
|
OnCallSpec(const char* a_file, int a_line,
|
|
|
|
const ArgumentMatcherTuple& matchers)
|
|
|
|
: UntypedOnCallSpecBase(a_file, a_line),
|
2008-12-10 05:08:54 +00:00
|
|
|
matchers_(matchers),
|
2019-01-17 20:56:41 +00:00
|
|
|
// By default, extra_matcher_ should match anything. However,
|
|
|
|
// we cannot initialize it with _ as that causes ambiguity between
|
|
|
|
// Matcher's copy and move constructor for some argument types.
|
|
|
|
extra_matcher_(A<const ArgumentTuple&>()) {}
|
2008-12-10 05:08:54 +00:00
|
|
|
|
2009-06-09 06:09:53 +00:00
|
|
|
// Implements the .With() clause.
|
2011-02-23 19:39:27 +00:00
|
|
|
OnCallSpec& With(const Matcher<const ArgumentTuple&>& m) {
|
2008-12-10 05:08:54 +00:00
|
|
|
// Makes sure this is called at most once.
|
2009-06-09 06:09:53 +00:00
|
|
|
ExpectSpecProperty(last_clause_ < kWith,
|
|
|
|
".With() cannot appear "
|
2008-12-10 05:08:54 +00:00
|
|
|
"more than once in an ON_CALL().");
|
2009-06-09 06:09:53 +00:00
|
|
|
last_clause_ = kWith;
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
extra_matcher_ = m;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements the .WillByDefault() clause.
|
2011-02-23 19:39:27 +00:00
|
|
|
OnCallSpec& WillByDefault(const Action<F>& action) {
|
2009-06-09 06:09:53 +00:00
|
|
|
ExpectSpecProperty(last_clause_ < kWillByDefault,
|
2008-12-10 05:08:54 +00:00
|
|
|
".WillByDefault() must appear "
|
|
|
|
"exactly once in an ON_CALL().");
|
2009-06-09 06:09:53 +00:00
|
|
|
last_clause_ = kWillByDefault;
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
ExpectSpecProperty(!action.IsDoDefault(),
|
|
|
|
"DoDefault() cannot be used in ON_CALL().");
|
|
|
|
action_ = action;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true iff the given arguments match the matchers.
|
|
|
|
bool Matches(const ArgumentTuple& args) const {
|
|
|
|
return TupleMatches(matchers_, args) && extra_matcher_.Matches(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the action specified by the user.
|
|
|
|
const Action<F>& GetAction() const {
|
2009-06-09 06:09:53 +00:00
|
|
|
AssertSpecProperty(last_clause_ == kWillByDefault,
|
2008-12-10 05:08:54 +00:00
|
|
|
".WillByDefault() must appear exactly "
|
|
|
|
"once in an ON_CALL().");
|
|
|
|
return action_;
|
|
|
|
}
|
2009-12-23 00:13:23 +00:00
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
private:
|
|
|
|
// The information in statement
|
|
|
|
//
|
|
|
|
// ON_CALL(mock_object, Method(matchers))
|
2009-06-09 06:09:53 +00:00
|
|
|
// .With(multi-argument-matcher)
|
2008-12-10 05:08:54 +00:00
|
|
|
// .WillByDefault(action);
|
|
|
|
//
|
|
|
|
// is recorded in the data members like this:
|
|
|
|
//
|
|
|
|
// source file that contains the statement => file_
|
|
|
|
// line number of the statement => line_
|
|
|
|
// matchers => matchers_
|
|
|
|
// multi-argument-matcher => extra_matcher_
|
|
|
|
// action => action_
|
|
|
|
ArgumentMatcherTuple matchers_;
|
|
|
|
Matcher<const ArgumentTuple&> extra_matcher_;
|
|
|
|
Action<F> action_;
|
2011-02-23 19:39:27 +00:00
|
|
|
}; // class OnCallSpec
|
2008-12-10 05:08:54 +00:00
|
|
|
|
2012-05-31 20:40:56 +00:00
|
|
|
// Possible reactions on uninteresting calls.
|
2008-12-10 05:08:54 +00:00
|
|
|
enum CallReaction {
|
2012-05-31 20:40:56 +00:00
|
|
|
kAllow,
|
|
|
|
kWarn,
|
2013-03-01 07:10:07 +00:00
|
|
|
kFail,
|
2008-12-10 05:08:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
|
|
|
|
// Utilities for manipulating mock objects.
|
2011-05-20 00:42:22 +00:00
|
|
|
class GTEST_API_ Mock {
|
2008-12-10 05:08:54 +00:00
|
|
|
public:
|
|
|
|
// The following public methods can be called concurrently.
|
|
|
|
|
2009-04-22 22:25:31 +00:00
|
|
|
// Tells Google Mock to ignore mock_obj when checking for leaked
|
|
|
|
// mock objects.
|
2011-10-24 21:16:22 +00:00
|
|
|
static void AllowLeak(const void* mock_obj)
|
|
|
|
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
|
2009-04-22 22:25:31 +00:00
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
// Verifies and clears all expectations on the given mock object.
|
|
|
|
// If the expectations aren't satisfied, generates one or more
|
|
|
|
// Google Test non-fatal failures and returns false.
|
2011-10-24 21:16:22 +00:00
|
|
|
static bool VerifyAndClearExpectations(void* mock_obj)
|
|
|
|
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
// Verifies all expectations on the given mock object and clears its
|
|
|
|
// default actions and expectations. Returns true iff the
|
|
|
|
// verification was successful.
|
2011-10-24 21:16:22 +00:00
|
|
|
static bool VerifyAndClear(void* mock_obj)
|
|
|
|
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
|
2012-04-10 16:02:11 +00:00
|
|
|
|
2018-10-23 19:13:41 +00:00
|
|
|
// Returns whether the mock was created as a naggy mock (default)
|
2018-10-16 06:07:15 +00:00
|
|
|
static bool IsNaggy(void* mock_obj)
|
|
|
|
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
|
2018-10-23 19:13:41 +00:00
|
|
|
// Returns whether the mock was created as a nice mock
|
2018-10-16 06:07:15 +00:00
|
|
|
static bool IsNice(void* mock_obj)
|
|
|
|
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
|
2018-10-23 19:13:41 +00:00
|
|
|
// Returns whether the mock was created as a strict mock
|
2018-10-16 06:07:15 +00:00
|
|
|
static bool IsStrict(void* mock_obj)
|
|
|
|
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
|
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
private:
|
2011-02-23 19:39:27 +00:00
|
|
|
friend class internal::UntypedFunctionMockerBase;
|
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
// Needed for a function mocker to register itself (so that we know
|
|
|
|
// how to clear a mock object).
|
|
|
|
template <typename F>
|
2018-11-08 16:14:50 +00:00
|
|
|
friend class internal::FunctionMocker;
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
template <typename M>
|
Remove multiple inheritance from "unintesting call" mock classes.
Internal CL 156157936, which was published in commit
fe402c27790ff1cc9a7e17c5d0aea4ebe7fd8a71, introduced undefined behavior
by casting a base class (internal::{Naggy,Nice,Strict}Base<MockClass>,
using the curiously recurring template pattern) pointer to a derived
class ({Naggy,Nice,Strict}Mock<MockClass>), in the base class'
constructor. At that point, the object isn't guaranteed to have taken on
the shape of the derived class, and casting is undefined behavior.
The undefined behavior was caught by Chrome's CFI build bot [1], and
prevents rolling googletest past that commit / CL.
This commit simplifies the {Naggy,Nice,Strict}Mock class hierarchy in
a way that removes the undefined behavior.
[1] https://www.chromium.org/developers/testing/control-flow-integrity
2018-04-10 04:57:54 +00:00
|
|
|
friend class NiceMock;
|
2008-12-10 05:08:54 +00:00
|
|
|
|
2013-03-01 01:54:22 +00:00
|
|
|
template <typename M>
|
Remove multiple inheritance from "unintesting call" mock classes.
Internal CL 156157936, which was published in commit
fe402c27790ff1cc9a7e17c5d0aea4ebe7fd8a71, introduced undefined behavior
by casting a base class (internal::{Naggy,Nice,Strict}Base<MockClass>,
using the curiously recurring template pattern) pointer to a derived
class ({Naggy,Nice,Strict}Mock<MockClass>), in the base class'
constructor. At that point, the object isn't guaranteed to have taken on
the shape of the derived class, and casting is undefined behavior.
The undefined behavior was caught by Chrome's CFI build bot [1], and
prevents rolling googletest past that commit / CL.
This commit simplifies the {Naggy,Nice,Strict}Mock class hierarchy in
a way that removes the undefined behavior.
[1] https://www.chromium.org/developers/testing/control-flow-integrity
2018-04-10 04:57:54 +00:00
|
|
|
friend class NaggyMock;
|
2013-03-01 01:54:22 +00:00
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
template <typename M>
|
Remove multiple inheritance from "unintesting call" mock classes.
Internal CL 156157936, which was published in commit
fe402c27790ff1cc9a7e17c5d0aea4ebe7fd8a71, introduced undefined behavior
by casting a base class (internal::{Naggy,Nice,Strict}Base<MockClass>,
using the curiously recurring template pattern) pointer to a derived
class ({Naggy,Nice,Strict}Mock<MockClass>), in the base class'
constructor. At that point, the object isn't guaranteed to have taken on
the shape of the derived class, and casting is undefined behavior.
The undefined behavior was caught by Chrome's CFI build bot [1], and
prevents rolling googletest past that commit / CL.
This commit simplifies the {Naggy,Nice,Strict}Mock class hierarchy in
a way that removes the undefined behavior.
[1] https://www.chromium.org/developers/testing/control-flow-integrity
2018-04-10 04:57:54 +00:00
|
|
|
friend class StrictMock;
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
// Tells Google Mock to allow uninteresting calls on the given mock
|
|
|
|
// object.
|
2011-10-24 21:16:22 +00:00
|
|
|
static void AllowUninterestingCalls(const void* mock_obj)
|
|
|
|
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
// Tells Google Mock to warn the user about uninteresting calls on
|
|
|
|
// the given mock object.
|
2011-10-24 21:16:22 +00:00
|
|
|
static void WarnUninterestingCalls(const void* mock_obj)
|
|
|
|
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
// Tells Google Mock to fail uninteresting calls on the given mock
|
|
|
|
// object.
|
2011-10-24 21:16:22 +00:00
|
|
|
static void FailUninterestingCalls(const void* mock_obj)
|
|
|
|
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
// Tells Google Mock the given mock object is being destroyed and
|
|
|
|
// its entry in the call-reaction table should be removed.
|
2011-10-24 21:16:22 +00:00
|
|
|
static void UnregisterCallReaction(const void* mock_obj)
|
|
|
|
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
// Returns the reaction Google Mock will have on uninteresting calls
|
|
|
|
// made on the given mock object.
|
|
|
|
static internal::CallReaction GetReactionOnUninterestingCalls(
|
2012-05-31 20:40:56 +00:00
|
|
|
const void* mock_obj)
|
2011-10-24 21:16:22 +00:00
|
|
|
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
// Verifies that all expectations on the given mock object have been
|
|
|
|
// satisfied. Reports one or more Google Test non-fatal failures
|
|
|
|
// and returns false if not.
|
2011-10-24 21:16:22 +00:00
|
|
|
static bool VerifyAndClearExpectationsLocked(void* mock_obj)
|
|
|
|
GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex);
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
// Clears all ON_CALL()s set on the given mock object.
|
2011-10-24 21:16:22 +00:00
|
|
|
static void ClearDefaultActionsLocked(void* mock_obj)
|
|
|
|
GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex);
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
// Registers a mock object and a mock method it owns.
|
2011-10-24 21:16:22 +00:00
|
|
|
static void Register(
|
|
|
|
const void* mock_obj,
|
|
|
|
internal::UntypedFunctionMockerBase* mocker)
|
|
|
|
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
|
2008-12-10 05:08:54 +00:00
|
|
|
|
2009-04-22 22:25:31 +00:00
|
|
|
// Tells Google Mock where in the source code mock_obj is used in an
|
|
|
|
// ON_CALL or EXPECT_CALL. In case mock_obj is leaked, this
|
|
|
|
// information helps the user identify which object it is.
|
|
|
|
static void RegisterUseByOnCallOrExpectCall(
|
2011-10-24 21:16:22 +00:00
|
|
|
const void* mock_obj, const char* file, int line)
|
|
|
|
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
|
2009-04-22 22:25:31 +00:00
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
// Unregisters a mock method; removes the owning mock object from
|
|
|
|
// the registry when the last mock method associated with it has
|
|
|
|
// been unregistered. This is called only in the destructor of
|
2018-11-08 16:14:50 +00:00
|
|
|
// FunctionMocker.
|
2011-10-24 21:16:22 +00:00
|
|
|
static void UnregisterLocked(internal::UntypedFunctionMockerBase* mocker)
|
|
|
|
GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex);
|
2008-12-10 05:08:54 +00:00
|
|
|
}; // class Mock
|
|
|
|
|
2009-07-01 19:04:51 +00:00
|
|
|
// An abstract handle of an expectation. Useful in the .After()
|
|
|
|
// clause of EXPECT_CALL() for setting the (partial) order of
|
|
|
|
// expectations. The syntax:
|
|
|
|
//
|
|
|
|
// Expectation e1 = EXPECT_CALL(...)...;
|
|
|
|
// EXPECT_CALL(...).After(e1)...;
|
|
|
|
//
|
|
|
|
// sets two expectations where the latter can only be matched after
|
|
|
|
// the former has been satisfied.
|
|
|
|
//
|
|
|
|
// Notes:
|
|
|
|
// - This class is copyable and has value semantics.
|
|
|
|
// - Constness is shallow: a const Expectation object itself cannot
|
|
|
|
// be modified, but the mutable methods of the ExpectationBase
|
|
|
|
// object it references can be called via expectation_base().
|
2018-10-29 15:09:33 +00:00
|
|
|
|
2011-05-20 00:42:22 +00:00
|
|
|
class GTEST_API_ Expectation {
|
2009-07-01 19:04:51 +00:00
|
|
|
public:
|
|
|
|
// Constructs a null object that doesn't reference any expectation.
|
2009-10-01 21:56:16 +00:00
|
|
|
Expectation();
|
|
|
|
|
|
|
|
~Expectation();
|
2009-07-01 19:04:51 +00:00
|
|
|
|
|
|
|
// This single-argument ctor must not be explicit, in order to support the
|
|
|
|
// Expectation e = EXPECT_CALL(...);
|
|
|
|
// syntax.
|
|
|
|
//
|
|
|
|
// A TypedExpectation object stores its pre-requisites as
|
|
|
|
// Expectation objects, and needs to call the non-const Retire()
|
|
|
|
// method on the ExpectationBase objects they reference. Therefore
|
|
|
|
// Expectation must receive a *non-const* reference to the
|
|
|
|
// ExpectationBase object.
|
|
|
|
Expectation(internal::ExpectationBase& exp); // NOLINT
|
|
|
|
|
|
|
|
// The compiler-generated copy ctor and operator= work exactly as
|
|
|
|
// intended, so we don't need to define our own.
|
|
|
|
|
|
|
|
// Returns true iff rhs references the same expectation as this object does.
|
|
|
|
bool operator==(const Expectation& rhs) const {
|
|
|
|
return expectation_base_ == rhs.expectation_base_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const Expectation& rhs) const { return !(*this == rhs); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class ExpectationSet;
|
|
|
|
friend class Sequence;
|
|
|
|
friend class ::testing::internal::ExpectationBase;
|
2011-02-23 19:39:27 +00:00
|
|
|
friend class ::testing::internal::UntypedFunctionMockerBase;
|
2009-07-01 19:04:51 +00:00
|
|
|
|
|
|
|
template <typename F>
|
2018-11-08 16:14:50 +00:00
|
|
|
friend class ::testing::internal::FunctionMocker;
|
2009-07-01 19:04:51 +00:00
|
|
|
|
|
|
|
template <typename F>
|
|
|
|
friend class ::testing::internal::TypedExpectation;
|
|
|
|
|
|
|
|
// This comparator is needed for putting Expectation objects into a set.
|
|
|
|
class Less {
|
|
|
|
public:
|
|
|
|
bool operator()(const Expectation& lhs, const Expectation& rhs) const {
|
|
|
|
return lhs.expectation_base_.get() < rhs.expectation_base_.get();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef ::std::set<Expectation, Less> Set;
|
|
|
|
|
|
|
|
Expectation(
|
2018-10-29 15:09:33 +00:00
|
|
|
const std::shared_ptr<internal::ExpectationBase>& expectation_base);
|
2009-07-01 19:04:51 +00:00
|
|
|
|
|
|
|
// Returns the expectation this object references.
|
2018-10-29 15:09:33 +00:00
|
|
|
const std::shared_ptr<internal::ExpectationBase>& expectation_base() const {
|
2009-07-01 19:04:51 +00:00
|
|
|
return expectation_base_;
|
|
|
|
}
|
|
|
|
|
2018-10-29 15:09:33 +00:00
|
|
|
// A shared_ptr that co-owns the expectation this handle references.
|
|
|
|
std::shared_ptr<internal::ExpectationBase> expectation_base_;
|
2009-07-01 19:04:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// A set of expectation handles. Useful in the .After() clause of
|
|
|
|
// EXPECT_CALL() for setting the (partial) order of expectations. The
|
|
|
|
// syntax:
|
|
|
|
//
|
|
|
|
// ExpectationSet es;
|
|
|
|
// es += EXPECT_CALL(...)...;
|
|
|
|
// es += EXPECT_CALL(...)...;
|
|
|
|
// EXPECT_CALL(...).After(es)...;
|
|
|
|
//
|
|
|
|
// sets three expectations where the last one can only be matched
|
|
|
|
// after the first two have both been satisfied.
|
|
|
|
//
|
|
|
|
// This class is copyable and has value semantics.
|
|
|
|
class ExpectationSet {
|
|
|
|
public:
|
|
|
|
// A bidirectional iterator that can read a const element in the set.
|
|
|
|
typedef Expectation::Set::const_iterator const_iterator;
|
|
|
|
|
|
|
|
// An object stored in the set. This is an alias of Expectation.
|
|
|
|
typedef Expectation::Set::value_type value_type;
|
|
|
|
|
|
|
|
// Constructs an empty set.
|
|
|
|
ExpectationSet() {}
|
|
|
|
|
|
|
|
// This single-argument ctor must not be explicit, in order to support the
|
|
|
|
// ExpectationSet es = EXPECT_CALL(...);
|
|
|
|
// syntax.
|
|
|
|
ExpectationSet(internal::ExpectationBase& exp) { // NOLINT
|
|
|
|
*this += Expectation(exp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This single-argument ctor implements implicit conversion from
|
|
|
|
// Expectation and thus must not be explicit. This allows either an
|
|
|
|
// Expectation or an ExpectationSet to be used in .After().
|
|
|
|
ExpectationSet(const Expectation& e) { // NOLINT
|
|
|
|
*this += e;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The compiler-generator ctor and operator= works exactly as
|
|
|
|
// intended, so we don't need to define our own.
|
|
|
|
|
|
|
|
// Returns true iff rhs contains the same set of Expectation objects
|
|
|
|
// as this does.
|
|
|
|
bool operator==(const ExpectationSet& rhs) const {
|
|
|
|
return expectations_ == rhs.expectations_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const ExpectationSet& rhs) const { return !(*this == rhs); }
|
|
|
|
|
|
|
|
// Implements the syntax
|
|
|
|
// expectation_set += EXPECT_CALL(...);
|
|
|
|
ExpectationSet& operator+=(const Expectation& e) {
|
|
|
|
expectations_.insert(e);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
int size() const { return static_cast<int>(expectations_.size()); }
|
|
|
|
|
|
|
|
const_iterator begin() const { return expectations_.begin(); }
|
|
|
|
const_iterator end() const { return expectations_.end(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Expectation::Set expectations_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
// Sequence objects are used by a user to specify the relative order
|
|
|
|
// in which the expectations should match. They are copyable (we rely
|
|
|
|
// on the compiler-defined copy constructor and assignment operator).
|
2011-05-20 00:42:22 +00:00
|
|
|
class GTEST_API_ Sequence {
|
2008-12-10 05:08:54 +00:00
|
|
|
public:
|
|
|
|
// Constructs an empty sequence.
|
2009-07-01 19:04:51 +00:00
|
|
|
Sequence() : last_expectation_(new Expectation) {}
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
// Adds an expectation to this sequence. The caller must ensure
|
|
|
|
// that no other thread is accessing this Sequence object.
|
2009-07-01 19:04:51 +00:00
|
|
|
void AddExpectation(const Expectation& expectation) const;
|
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
private:
|
2018-10-29 15:09:33 +00:00
|
|
|
// The last expectation in this sequence.
|
|
|
|
std::shared_ptr<Expectation> last_expectation_;
|
2008-12-10 05:08:54 +00:00
|
|
|
}; // class Sequence
|
|
|
|
|
|
|
|
// An object of this type causes all EXPECT_CALL() statements
|
|
|
|
// encountered in its scope to be put in an anonymous sequence. The
|
|
|
|
// work is done in the constructor and destructor. You should only
|
|
|
|
// create an InSequence object on the stack.
|
|
|
|
//
|
|
|
|
// The sole purpose for this class is to support easy definition of
|
|
|
|
// sequential expectations, e.g.
|
|
|
|
//
|
|
|
|
// {
|
|
|
|
// InSequence dummy; // The name of the object doesn't matter.
|
|
|
|
//
|
|
|
|
// // The following expectations must match in the order they appear.
|
|
|
|
// EXPECT_CALL(a, Bar())...;
|
|
|
|
// EXPECT_CALL(a, Baz())...;
|
|
|
|
// ...
|
|
|
|
// EXPECT_CALL(b, Xyz())...;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// You can create InSequence objects in multiple threads, as long as
|
|
|
|
// they are used to affect different mock objects. The idea is that
|
|
|
|
// each thread can create and set up its own mocks as if it's the only
|
|
|
|
// thread. However, for clarity of your tests we recommend you to set
|
|
|
|
// up mocks in the main thread unless you have a good reason not to do
|
|
|
|
// so.
|
2011-05-20 00:42:22 +00:00
|
|
|
class GTEST_API_ InSequence {
|
2008-12-10 05:08:54 +00:00
|
|
|
public:
|
|
|
|
InSequence();
|
|
|
|
~InSequence();
|
|
|
|
private:
|
|
|
|
bool sequence_created_;
|
|
|
|
|
|
|
|
GTEST_DISALLOW_COPY_AND_ASSIGN_(InSequence); // NOLINT
|
2010-08-09 22:46:12 +00:00
|
|
|
} GTEST_ATTRIBUTE_UNUSED_;
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
// Points to the implicit sequence introduced by a living InSequence
|
|
|
|
// object (if any) in the current thread or NULL.
|
2011-05-20 00:42:22 +00:00
|
|
|
GTEST_API_ extern ThreadLocal<Sequence*> g_gmock_implicit_sequence;
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
// Base class for implementing expectations.
|
|
|
|
//
|
|
|
|
// There are two reasons for having a type-agnostic base class for
|
|
|
|
// Expectation:
|
|
|
|
//
|
|
|
|
// 1. We need to store collections of expectations of different
|
|
|
|
// types (e.g. all pre-requisites of a particular expectation, all
|
|
|
|
// expectations in a sequence). Therefore these expectation objects
|
|
|
|
// must share a common base class.
|
|
|
|
//
|
|
|
|
// 2. We can avoid binary code bloat by moving methods not depending
|
|
|
|
// on the template argument of Expectation to the base class.
|
|
|
|
//
|
|
|
|
// This class is internal and mustn't be used by user code directly.
|
2011-05-20 00:42:22 +00:00
|
|
|
class GTEST_API_ ExpectationBase {
|
2008-12-10 05:08:54 +00:00
|
|
|
public:
|
2009-10-21 06:15:34 +00:00
|
|
|
// source_text is the EXPECT_CALL(...) source that created this Expectation.
|
2017-05-15 21:07:03 +00:00
|
|
|
ExpectationBase(const char* file, int line, const std::string& source_text);
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
virtual ~ExpectationBase();
|
|
|
|
|
|
|
|
// Where in the source file was the expectation spec defined?
|
|
|
|
const char* file() const { return file_; }
|
|
|
|
int line() const { return line_; }
|
2009-10-21 06:15:34 +00:00
|
|
|
const char* source_text() const { return source_text_.c_str(); }
|
2008-12-10 05:08:54 +00:00
|
|
|
// Returns the cardinality specified in the expectation spec.
|
|
|
|
const Cardinality& cardinality() const { return cardinality_; }
|
|
|
|
|
|
|
|
// Describes the source file location of this expectation.
|
|
|
|
void DescribeLocationTo(::std::ostream* os) const {
|
2011-02-11 23:50:38 +00:00
|
|
|
*os << FormatFileLocation(file(), line()) << " ";
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Describes how many times a function call matching this
|
|
|
|
// expectation has occurred.
|
2011-10-24 21:16:22 +00:00
|
|
|
void DescribeCallCountTo(::std::ostream* os) const
|
|
|
|
GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex);
|
2011-02-23 19:39:27 +00:00
|
|
|
|
|
|
|
// If this mock method has an extra matcher (i.e. .With(matcher)),
|
|
|
|
// describes it to the ostream.
|
|
|
|
virtual void MaybeDescribeExtraMatcherTo(::std::ostream* os) = 0;
|
2009-12-23 00:13:23 +00:00
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
protected:
|
2009-07-01 19:04:51 +00:00
|
|
|
friend class ::testing::Expectation;
|
2011-02-23 19:39:27 +00:00
|
|
|
friend class UntypedFunctionMockerBase;
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
enum Clause {
|
|
|
|
// Don't change the order of the enum members!
|
2009-06-09 06:09:53 +00:00
|
|
|
kNone,
|
|
|
|
kWith,
|
|
|
|
kTimes,
|
|
|
|
kInSequence,
|
2009-07-01 19:04:51 +00:00
|
|
|
kAfter,
|
2009-06-09 06:09:53 +00:00
|
|
|
kWillOnce,
|
|
|
|
kWillRepeatedly,
|
2011-04-08 01:32:32 +00:00
|
|
|
kRetiresOnSaturation
|
2008-12-10 05:08:54 +00:00
|
|
|
};
|
|
|
|
|
2011-02-23 19:39:27 +00:00
|
|
|
typedef std::vector<const void*> UntypedActions;
|
|
|
|
|
2009-07-01 19:04:51 +00:00
|
|
|
// Returns an Expectation object that references and co-owns this
|
|
|
|
// expectation.
|
|
|
|
virtual Expectation GetHandle() = 0;
|
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
// Asserts that the EXPECT_CALL() statement has the given property.
|
2017-05-15 21:07:03 +00:00
|
|
|
void AssertSpecProperty(bool property,
|
|
|
|
const std::string& failure_message) const {
|
2008-12-10 05:08:54 +00:00
|
|
|
Assert(property, file_, line_, failure_message);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expects that the EXPECT_CALL() statement has the given property.
|
2017-05-15 21:07:03 +00:00
|
|
|
void ExpectSpecProperty(bool property,
|
|
|
|
const std::string& failure_message) const {
|
2008-12-10 05:08:54 +00:00
|
|
|
Expect(property, file_, line_, failure_message);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Explicitly specifies the cardinality of this expectation. Used
|
|
|
|
// by the subclasses to implement the .Times() clause.
|
|
|
|
void SpecifyCardinality(const Cardinality& cardinality);
|
|
|
|
|
|
|
|
// Returns true iff the user specified the cardinality explicitly
|
|
|
|
// using a .Times().
|
|
|
|
bool cardinality_specified() const { return cardinality_specified_; }
|
|
|
|
|
|
|
|
// Sets the cardinality of this expectation spec.
|
2009-12-23 00:13:23 +00:00
|
|
|
void set_cardinality(const Cardinality& a_cardinality) {
|
|
|
|
cardinality_ = a_cardinality;
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The following group of methods should only be called after the
|
|
|
|
// EXPECT_CALL() statement, and only when g_gmock_mutex is held by
|
|
|
|
// the current thread.
|
|
|
|
|
|
|
|
// Retires all pre-requisites of this expectation.
|
2011-10-24 21:16:22 +00:00
|
|
|
void RetireAllPreRequisites()
|
|
|
|
GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex);
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
// Returns true iff this expectation is retired.
|
2011-10-24 21:16:22 +00:00
|
|
|
bool is_retired() const
|
|
|
|
GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
|
2008-12-10 05:08:54 +00:00
|
|
|
g_gmock_mutex.AssertHeld();
|
|
|
|
return retired_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retires this expectation.
|
2011-10-24 21:16:22 +00:00
|
|
|
void Retire()
|
|
|
|
GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
|
2008-12-10 05:08:54 +00:00
|
|
|
g_gmock_mutex.AssertHeld();
|
|
|
|
retired_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true iff this expectation is satisfied.
|
2011-10-24 21:16:22 +00:00
|
|
|
bool IsSatisfied() const
|
|
|
|
GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
|
2008-12-10 05:08:54 +00:00
|
|
|
g_gmock_mutex.AssertHeld();
|
|
|
|
return cardinality().IsSatisfiedByCallCount(call_count_);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true iff this expectation is saturated.
|
2011-10-24 21:16:22 +00:00
|
|
|
bool IsSaturated() const
|
|
|
|
GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
|
2008-12-10 05:08:54 +00:00
|
|
|
g_gmock_mutex.AssertHeld();
|
|
|
|
return cardinality().IsSaturatedByCallCount(call_count_);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true iff this expectation is over-saturated.
|
2011-10-24 21:16:22 +00:00
|
|
|
bool IsOverSaturated() const
|
|
|
|
GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
|
2008-12-10 05:08:54 +00:00
|
|
|
g_gmock_mutex.AssertHeld();
|
|
|
|
return cardinality().IsOverSaturatedByCallCount(call_count_);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true iff all pre-requisites of this expectation are satisfied.
|
2011-10-24 21:16:22 +00:00
|
|
|
bool AllPrerequisitesAreSatisfied() const
|
|
|
|
GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex);
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
// Adds unsatisfied pre-requisites of this expectation to 'result'.
|
2011-10-24 21:16:22 +00:00
|
|
|
void FindUnsatisfiedPrerequisites(ExpectationSet* result) const
|
|
|
|
GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex);
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
// Returns the number this expectation has been invoked.
|
2011-10-24 21:16:22 +00:00
|
|
|
int call_count() const
|
|
|
|
GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
|
2008-12-10 05:08:54 +00:00
|
|
|
g_gmock_mutex.AssertHeld();
|
|
|
|
return call_count_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Increments the number this expectation has been invoked.
|
2011-10-24 21:16:22 +00:00
|
|
|
void IncrementCallCount()
|
|
|
|
GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
|
2008-12-10 05:08:54 +00:00
|
|
|
g_gmock_mutex.AssertHeld();
|
|
|
|
call_count_++;
|
|
|
|
}
|
|
|
|
|
2011-02-23 19:39:27 +00:00
|
|
|
// Checks the action count (i.e. the number of WillOnce() and
|
|
|
|
// WillRepeatedly() clauses) against the cardinality if this hasn't
|
|
|
|
// been done before. Prints a warning if there are too many or too
|
|
|
|
// few actions.
|
2011-10-24 21:16:22 +00:00
|
|
|
void CheckActionCountIfNotDone() const
|
|
|
|
GTEST_LOCK_EXCLUDED_(mutex_);
|
2011-02-23 19:39:27 +00:00
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
friend class ::testing::Sequence;
|
|
|
|
friend class ::testing::internal::ExpectationTester;
|
|
|
|
|
|
|
|
template <typename Function>
|
2009-07-01 19:04:51 +00:00
|
|
|
friend class TypedExpectation;
|
2008-12-10 05:08:54 +00:00
|
|
|
|
2011-02-23 19:39:27 +00:00
|
|
|
// Implements the .Times() clause.
|
|
|
|
void UntypedTimes(const Cardinality& a_cardinality);
|
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
// This group of fields are part of the spec and won't change after
|
|
|
|
// an EXPECT_CALL() statement finishes.
|
2009-10-21 06:15:34 +00:00
|
|
|
const char* file_; // The file that contains the expectation.
|
|
|
|
int line_; // The line number of the expectation.
|
2017-05-15 21:07:03 +00:00
|
|
|
const std::string source_text_; // The EXPECT_CALL(...) source text.
|
2008-12-10 05:08:54 +00:00
|
|
|
// True iff the cardinality is specified explicitly.
|
|
|
|
bool cardinality_specified_;
|
|
|
|
Cardinality cardinality_; // The cardinality of the expectation.
|
2009-07-01 19:04:51 +00:00
|
|
|
// The immediate pre-requisites (i.e. expectations that must be
|
|
|
|
// satisfied before this expectation can be matched) of this
|
2018-10-29 15:09:33 +00:00
|
|
|
// expectation. We use std::shared_ptr in the set because we want an
|
2009-07-01 19:04:51 +00:00
|
|
|
// Expectation object to be co-owned by its FunctionMocker and its
|
|
|
|
// successors. This allows multiple mock objects to be deleted at
|
|
|
|
// different times.
|
|
|
|
ExpectationSet immediate_prerequisites_;
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
// This group of fields are the current state of the expectation,
|
|
|
|
// and can change as the mock function is called.
|
|
|
|
int call_count_; // How many times this expectation has been invoked.
|
|
|
|
bool retired_; // True iff this expectation has retired.
|
2011-02-23 19:39:27 +00:00
|
|
|
UntypedActions untyped_actions_;
|
|
|
|
bool extra_matcher_specified_;
|
|
|
|
bool repeated_action_specified_; // True if a WillRepeatedly() was specified.
|
|
|
|
bool retires_on_saturation_;
|
|
|
|
Clause last_clause_;
|
|
|
|
mutable bool action_count_checked_; // Under mutex_.
|
|
|
|
mutable Mutex mutex_; // Protects action_count_checked_.
|
2009-12-23 00:13:23 +00:00
|
|
|
|
|
|
|
GTEST_DISALLOW_ASSIGN_(ExpectationBase);
|
2008-12-10 05:08:54 +00:00
|
|
|
}; // class ExpectationBase
|
|
|
|
|
|
|
|
// Impements an expectation for the given function type.
|
|
|
|
template <typename F>
|
2009-07-01 19:04:51 +00:00
|
|
|
class TypedExpectation : public ExpectationBase {
|
2008-12-10 05:08:54 +00:00
|
|
|
public:
|
|
|
|
typedef typename Function<F>::ArgumentTuple ArgumentTuple;
|
|
|
|
typedef typename Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple;
|
|
|
|
typedef typename Function<F>::Result Result;
|
|
|
|
|
2018-11-08 16:14:50 +00:00
|
|
|
TypedExpectation(FunctionMocker<F>* owner, const char* a_file, int a_line,
|
2017-05-15 21:07:03 +00:00
|
|
|
const std::string& a_source_text,
|
2009-07-01 19:04:51 +00:00
|
|
|
const ArgumentMatcherTuple& m)
|
2009-12-23 00:13:23 +00:00
|
|
|
: ExpectationBase(a_file, a_line, a_source_text),
|
2008-12-10 05:08:54 +00:00
|
|
|
owner_(owner),
|
|
|
|
matchers_(m),
|
2019-01-17 20:56:41 +00:00
|
|
|
// By default, extra_matcher_ should match anything. However,
|
|
|
|
// we cannot initialize it with _ as that causes ambiguity between
|
|
|
|
// Matcher's copy and move constructor for some argument types.
|
|
|
|
extra_matcher_(A<const ArgumentTuple&>()),
|
2011-02-23 19:39:27 +00:00
|
|
|
repeated_action_(DoDefault()) {}
|
2008-12-10 05:08:54 +00:00
|
|
|
|
2018-12-03 16:30:02 +00:00
|
|
|
~TypedExpectation() override {
|
2008-12-10 05:08:54 +00:00
|
|
|
// Check the validity of the action count if it hasn't been done
|
|
|
|
// yet (for example, if the expectation was never used).
|
|
|
|
CheckActionCountIfNotDone();
|
2011-02-23 19:39:27 +00:00
|
|
|
for (UntypedActions::const_iterator it = untyped_actions_.begin();
|
|
|
|
it != untyped_actions_.end(); ++it) {
|
|
|
|
delete static_cast<const Action<F>*>(*it);
|
|
|
|
}
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
|
|
|
|
2009-06-09 06:09:53 +00:00
|
|
|
// Implements the .With() clause.
|
2009-07-01 19:04:51 +00:00
|
|
|
TypedExpectation& With(const Matcher<const ArgumentTuple&>& m) {
|
2009-06-09 06:09:53 +00:00
|
|
|
if (last_clause_ == kWith) {
|
2008-12-10 05:08:54 +00:00
|
|
|
ExpectSpecProperty(false,
|
2009-06-09 06:09:53 +00:00
|
|
|
".With() cannot appear "
|
2008-12-10 05:08:54 +00:00
|
|
|
"more than once in an EXPECT_CALL().");
|
|
|
|
} else {
|
2009-06-09 06:09:53 +00:00
|
|
|
ExpectSpecProperty(last_clause_ < kWith,
|
|
|
|
".With() must be the first "
|
2008-12-10 05:08:54 +00:00
|
|
|
"clause in an EXPECT_CALL().");
|
|
|
|
}
|
2009-06-09 06:09:53 +00:00
|
|
|
last_clause_ = kWith;
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
extra_matcher_ = m;
|
2009-10-21 06:15:34 +00:00
|
|
|
extra_matcher_specified_ = true;
|
2008-12-10 05:08:54 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements the .Times() clause.
|
2009-12-23 00:13:23 +00:00
|
|
|
TypedExpectation& Times(const Cardinality& a_cardinality) {
|
2011-02-23 19:39:27 +00:00
|
|
|
ExpectationBase::UntypedTimes(a_cardinality);
|
2008-12-10 05:08:54 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements the .Times() clause.
|
2009-07-01 19:04:51 +00:00
|
|
|
TypedExpectation& Times(int n) {
|
2008-12-10 05:08:54 +00:00
|
|
|
return Times(Exactly(n));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements the .InSequence() clause.
|
2009-07-01 19:04:51 +00:00
|
|
|
TypedExpectation& InSequence(const Sequence& s) {
|
2009-06-09 06:09:53 +00:00
|
|
|
ExpectSpecProperty(last_clause_ <= kInSequence,
|
2009-07-01 19:04:51 +00:00
|
|
|
".InSequence() cannot appear after .After(),"
|
|
|
|
" .WillOnce(), .WillRepeatedly(), or "
|
2008-12-10 05:08:54 +00:00
|
|
|
".RetiresOnSaturation().");
|
2009-06-09 06:09:53 +00:00
|
|
|
last_clause_ = kInSequence;
|
2008-12-10 05:08:54 +00:00
|
|
|
|
2009-07-01 19:04:51 +00:00
|
|
|
s.AddExpectation(GetHandle());
|
2008-12-10 05:08:54 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2009-07-01 19:04:51 +00:00
|
|
|
TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2) {
|
2008-12-10 05:08:54 +00:00
|
|
|
return InSequence(s1).InSequence(s2);
|
|
|
|
}
|
2009-07-01 19:04:51 +00:00
|
|
|
TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2,
|
|
|
|
const Sequence& s3) {
|
2008-12-10 05:08:54 +00:00
|
|
|
return InSequence(s1, s2).InSequence(s3);
|
|
|
|
}
|
2009-07-01 19:04:51 +00:00
|
|
|
TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2,
|
|
|
|
const Sequence& s3, const Sequence& s4) {
|
2008-12-10 05:08:54 +00:00
|
|
|
return InSequence(s1, s2, s3).InSequence(s4);
|
|
|
|
}
|
2009-07-01 19:04:51 +00:00
|
|
|
TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2,
|
|
|
|
const Sequence& s3, const Sequence& s4,
|
|
|
|
const Sequence& s5) {
|
2008-12-10 05:08:54 +00:00
|
|
|
return InSequence(s1, s2, s3, s4).InSequence(s5);
|
|
|
|
}
|
|
|
|
|
2009-07-01 19:04:51 +00:00
|
|
|
// Implements that .After() clause.
|
|
|
|
TypedExpectation& After(const ExpectationSet& s) {
|
|
|
|
ExpectSpecProperty(last_clause_ <= kAfter,
|
|
|
|
".After() cannot appear after .WillOnce(),"
|
|
|
|
" .WillRepeatedly(), or "
|
|
|
|
".RetiresOnSaturation().");
|
|
|
|
last_clause_ = kAfter;
|
|
|
|
|
|
|
|
for (ExpectationSet::const_iterator it = s.begin(); it != s.end(); ++it) {
|
|
|
|
immediate_prerequisites_ += *it;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2) {
|
|
|
|
return After(s1).After(s2);
|
|
|
|
}
|
|
|
|
TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2,
|
|
|
|
const ExpectationSet& s3) {
|
|
|
|
return After(s1, s2).After(s3);
|
|
|
|
}
|
|
|
|
TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2,
|
|
|
|
const ExpectationSet& s3, const ExpectationSet& s4) {
|
|
|
|
return After(s1, s2, s3).After(s4);
|
|
|
|
}
|
|
|
|
TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2,
|
|
|
|
const ExpectationSet& s3, const ExpectationSet& s4,
|
|
|
|
const ExpectationSet& s5) {
|
|
|
|
return After(s1, s2, s3, s4).After(s5);
|
|
|
|
}
|
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
// Implements the .WillOnce() clause.
|
2009-07-01 19:04:51 +00:00
|
|
|
TypedExpectation& WillOnce(const Action<F>& action) {
|
2009-06-09 06:09:53 +00:00
|
|
|
ExpectSpecProperty(last_clause_ <= kWillOnce,
|
2008-12-10 05:08:54 +00:00
|
|
|
".WillOnce() cannot appear after "
|
|
|
|
".WillRepeatedly() or .RetiresOnSaturation().");
|
2009-06-09 06:09:53 +00:00
|
|
|
last_clause_ = kWillOnce;
|
2008-12-10 05:08:54 +00:00
|
|
|
|
2011-02-23 19:39:27 +00:00
|
|
|
untyped_actions_.push_back(new Action<F>(action));
|
2008-12-10 05:08:54 +00:00
|
|
|
if (!cardinality_specified()) {
|
2011-02-23 19:39:27 +00:00
|
|
|
set_cardinality(Exactly(static_cast<int>(untyped_actions_.size())));
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements the .WillRepeatedly() clause.
|
2009-07-01 19:04:51 +00:00
|
|
|
TypedExpectation& WillRepeatedly(const Action<F>& action) {
|
2009-06-09 06:09:53 +00:00
|
|
|
if (last_clause_ == kWillRepeatedly) {
|
2008-12-10 05:08:54 +00:00
|
|
|
ExpectSpecProperty(false,
|
|
|
|
".WillRepeatedly() cannot appear "
|
|
|
|
"more than once in an EXPECT_CALL().");
|
|
|
|
} else {
|
2009-06-09 06:09:53 +00:00
|
|
|
ExpectSpecProperty(last_clause_ < kWillRepeatedly,
|
2008-12-10 05:08:54 +00:00
|
|
|
".WillRepeatedly() cannot appear "
|
|
|
|
"after .RetiresOnSaturation().");
|
|
|
|
}
|
2009-06-09 06:09:53 +00:00
|
|
|
last_clause_ = kWillRepeatedly;
|
2008-12-10 05:08:54 +00:00
|
|
|
repeated_action_specified_ = true;
|
|
|
|
|
|
|
|
repeated_action_ = action;
|
|
|
|
if (!cardinality_specified()) {
|
2011-02-23 19:39:27 +00:00
|
|
|
set_cardinality(AtLeast(static_cast<int>(untyped_actions_.size())));
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now that no more action clauses can be specified, we check
|
|
|
|
// whether their count makes sense.
|
|
|
|
CheckActionCountIfNotDone();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements the .RetiresOnSaturation() clause.
|
2009-07-01 19:04:51 +00:00
|
|
|
TypedExpectation& RetiresOnSaturation() {
|
2009-06-09 06:09:53 +00:00
|
|
|
ExpectSpecProperty(last_clause_ < kRetiresOnSaturation,
|
2008-12-10 05:08:54 +00:00
|
|
|
".RetiresOnSaturation() cannot appear "
|
|
|
|
"more than once.");
|
2009-06-09 06:09:53 +00:00
|
|
|
last_clause_ = kRetiresOnSaturation;
|
2008-12-10 05:08:54 +00:00
|
|
|
retires_on_saturation_ = true;
|
|
|
|
|
|
|
|
// Now that no more action clauses can be specified, we check
|
|
|
|
// whether their count makes sense.
|
|
|
|
CheckActionCountIfNotDone();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the matchers for the arguments as specified inside the
|
|
|
|
// EXPECT_CALL() macro.
|
|
|
|
const ArgumentMatcherTuple& matchers() const {
|
|
|
|
return matchers_;
|
|
|
|
}
|
|
|
|
|
2009-06-09 06:09:53 +00:00
|
|
|
// Returns the matcher specified by the .With() clause.
|
2008-12-10 05:08:54 +00:00
|
|
|
const Matcher<const ArgumentTuple&>& extra_matcher() const {
|
|
|
|
return extra_matcher_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the action specified by the .WillRepeatedly() clause.
|
|
|
|
const Action<F>& repeated_action() const { return repeated_action_; }
|
|
|
|
|
2011-02-23 19:39:27 +00:00
|
|
|
// If this mock method has an extra matcher (i.e. .With(matcher)),
|
|
|
|
// describes it to the ostream.
|
2018-12-03 16:30:02 +00:00
|
|
|
void MaybeDescribeExtraMatcherTo(::std::ostream* os) override {
|
2009-10-21 06:15:34 +00:00
|
|
|
if (extra_matcher_specified_) {
|
|
|
|
*os << " Expected args: ";
|
|
|
|
extra_matcher_.DescribeTo(os);
|
|
|
|
*os << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
private:
|
|
|
|
template <typename Function>
|
2018-11-08 16:14:50 +00:00
|
|
|
friend class FunctionMocker;
|
2008-12-10 05:08:54 +00:00
|
|
|
|
2009-07-01 19:04:51 +00:00
|
|
|
// Returns an Expectation object that references and co-owns this
|
|
|
|
// expectation.
|
2018-12-03 16:30:02 +00:00
|
|
|
Expectation GetHandle() override { return owner_->GetHandleOf(this); }
|
2009-07-01 19:04:51 +00:00
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
// The following methods will be called only after the EXPECT_CALL()
|
|
|
|
// statement finishes and when the current thread holds
|
|
|
|
// g_gmock_mutex.
|
|
|
|
|
|
|
|
// Returns true iff this expectation matches the given arguments.
|
2011-10-24 21:16:22 +00:00
|
|
|
bool Matches(const ArgumentTuple& args) const
|
|
|
|
GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
|
2008-12-10 05:08:54 +00:00
|
|
|
g_gmock_mutex.AssertHeld();
|
|
|
|
return TupleMatches(matchers_, args) && extra_matcher_.Matches(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true iff this expectation should handle the given arguments.
|
2011-10-24 21:16:22 +00:00
|
|
|
bool ShouldHandleArguments(const ArgumentTuple& args) const
|
|
|
|
GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
|
2008-12-10 05:08:54 +00:00
|
|
|
g_gmock_mutex.AssertHeld();
|
|
|
|
|
|
|
|
// In case the action count wasn't checked when the expectation
|
|
|
|
// was defined (e.g. if this expectation has no WillRepeatedly()
|
|
|
|
// or RetiresOnSaturation() clause), we check it when the
|
|
|
|
// expectation is used for the first time.
|
|
|
|
CheckActionCountIfNotDone();
|
|
|
|
return !is_retired() && AllPrerequisitesAreSatisfied() && Matches(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Describes the result of matching the arguments against this
|
|
|
|
// expectation to the given ostream.
|
2011-10-24 21:16:22 +00:00
|
|
|
void ExplainMatchResultTo(
|
|
|
|
const ArgumentTuple& args,
|
|
|
|
::std::ostream* os) const
|
|
|
|
GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
|
2008-12-10 05:08:54 +00:00
|
|
|
g_gmock_mutex.AssertHeld();
|
|
|
|
|
|
|
|
if (is_retired()) {
|
|
|
|
*os << " Expected: the expectation is active\n"
|
|
|
|
<< " Actual: it is retired\n";
|
|
|
|
} else if (!Matches(args)) {
|
|
|
|
if (!TupleMatches(matchers_, args)) {
|
2010-03-24 17:35:11 +00:00
|
|
|
ExplainMatchFailureTupleTo(matchers_, args, os);
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
2010-01-08 21:55:40 +00:00
|
|
|
StringMatchResultListener listener;
|
|
|
|
if (!extra_matcher_.MatchAndExplain(args, &listener)) {
|
2009-06-09 05:42:12 +00:00
|
|
|
*os << " Expected args: ";
|
2008-12-10 05:08:54 +00:00
|
|
|
extra_matcher_.DescribeTo(os);
|
2009-06-09 05:42:12 +00:00
|
|
|
*os << "\n Actual: don't match";
|
2008-12-10 05:08:54 +00:00
|
|
|
|
2010-03-24 17:35:11 +00:00
|
|
|
internal::PrintIfNotEmpty(listener.str(), os);
|
2008-12-10 05:08:54 +00:00
|
|
|
*os << "\n";
|
|
|
|
}
|
|
|
|
} else if (!AllPrerequisitesAreSatisfied()) {
|
|
|
|
*os << " Expected: all pre-requisites are satisfied\n"
|
|
|
|
<< " Actual: the following immediate pre-requisites "
|
|
|
|
<< "are not satisfied:\n";
|
2009-07-01 19:04:51 +00:00
|
|
|
ExpectationSet unsatisfied_prereqs;
|
2008-12-10 05:08:54 +00:00
|
|
|
FindUnsatisfiedPrerequisites(&unsatisfied_prereqs);
|
|
|
|
int i = 0;
|
2009-07-01 19:04:51 +00:00
|
|
|
for (ExpectationSet::const_iterator it = unsatisfied_prereqs.begin();
|
2008-12-10 05:08:54 +00:00
|
|
|
it != unsatisfied_prereqs.end(); ++it) {
|
2009-07-01 19:04:51 +00:00
|
|
|
it->expectation_base()->DescribeLocationTo(os);
|
2008-12-10 05:08:54 +00:00
|
|
|
*os << "pre-requisite #" << i++ << "\n";
|
|
|
|
}
|
|
|
|
*os << " (end of pre-requisites)\n";
|
|
|
|
} else {
|
|
|
|
// This line is here just for completeness' sake. It will never
|
2010-03-24 17:35:11 +00:00
|
|
|
// be executed as currently the ExplainMatchResultTo() function
|
2008-12-10 05:08:54 +00:00
|
|
|
// is called only when the mock function call does NOT match the
|
|
|
|
// expectation.
|
|
|
|
*os << "The call matches the expectation.\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the action that should be taken for the current invocation.
|
2018-11-08 16:14:50 +00:00
|
|
|
const Action<F>& GetCurrentAction(const FunctionMocker<F>* mocker,
|
|
|
|
const ArgumentTuple& args) const
|
|
|
|
GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
|
2008-12-10 05:08:54 +00:00
|
|
|
g_gmock_mutex.AssertHeld();
|
|
|
|
const int count = call_count();
|
|
|
|
Assert(count >= 1, __FILE__, __LINE__,
|
|
|
|
"call_count() is <= 0 when GetCurrentAction() is "
|
|
|
|
"called - this should never happen.");
|
|
|
|
|
2011-02-23 19:39:27 +00:00
|
|
|
const int action_count = static_cast<int>(untyped_actions_.size());
|
2008-12-10 05:08:54 +00:00
|
|
|
if (action_count > 0 && !repeated_action_specified_ &&
|
|
|
|
count > action_count) {
|
|
|
|
// If there is at least one WillOnce() and no WillRepeatedly(),
|
|
|
|
// we warn the user when the WillOnce() clauses ran out.
|
|
|
|
::std::stringstream ss;
|
|
|
|
DescribeLocationTo(&ss);
|
2009-10-21 06:15:34 +00:00
|
|
|
ss << "Actions ran out in " << source_text() << "...\n"
|
2008-12-10 05:08:54 +00:00
|
|
|
<< "Called " << count << " times, but only "
|
|
|
|
<< action_count << " WillOnce()"
|
|
|
|
<< (action_count == 1 ? " is" : "s are") << " specified - ";
|
|
|
|
mocker->DescribeDefaultActionTo(args, &ss);
|
2012-05-31 20:40:56 +00:00
|
|
|
Log(kWarning, ss.str(), 1);
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 20:12:32 +00:00
|
|
|
return count <= action_count
|
|
|
|
? *static_cast<const Action<F>*>(
|
|
|
|
untyped_actions_[static_cast<size_t>(count - 1)])
|
|
|
|
: repeated_action();
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Given the arguments of a mock function call, if the call will
|
|
|
|
// over-saturate this expectation, returns the default action;
|
|
|
|
// otherwise, returns the next action in this expectation. Also
|
|
|
|
// describes *what* happened to 'what', and explains *why* Google
|
|
|
|
// Mock does it to 'why'. This method is not const as it calls
|
2011-02-23 19:39:27 +00:00
|
|
|
// IncrementCallCount(). A return value of NULL means the default
|
|
|
|
// action.
|
2018-11-08 16:14:50 +00:00
|
|
|
const Action<F>* GetActionForArguments(const FunctionMocker<F>* mocker,
|
|
|
|
const ArgumentTuple& args,
|
|
|
|
::std::ostream* what,
|
|
|
|
::std::ostream* why)
|
|
|
|
GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
|
2008-12-10 05:08:54 +00:00
|
|
|
g_gmock_mutex.AssertHeld();
|
|
|
|
if (IsSaturated()) {
|
|
|
|
// We have an excessive call.
|
|
|
|
IncrementCallCount();
|
|
|
|
*what << "Mock function called more times than expected - ";
|
|
|
|
mocker->DescribeDefaultActionTo(args, what);
|
|
|
|
DescribeCallCountTo(why);
|
|
|
|
|
2018-10-04 22:28:05 +00:00
|
|
|
return nullptr;
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IncrementCallCount();
|
|
|
|
RetireAllPreRequisites();
|
|
|
|
|
2011-02-23 19:39:27 +00:00
|
|
|
if (retires_on_saturation_ && IsSaturated()) {
|
2008-12-10 05:08:54 +00:00
|
|
|
Retire();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must be done after IncrementCount()!
|
2009-10-21 06:15:34 +00:00
|
|
|
*what << "Mock function call matches " << source_text() <<"...\n";
|
2011-02-23 19:39:27 +00:00
|
|
|
return &(GetCurrentAction(mocker, args));
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// All the fields below won't change once the EXPECT_CALL()
|
|
|
|
// statement finishes.
|
2018-11-08 16:14:50 +00:00
|
|
|
FunctionMocker<F>* const owner_;
|
2008-12-10 05:08:54 +00:00
|
|
|
ArgumentMatcherTuple matchers_;
|
|
|
|
Matcher<const ArgumentTuple&> extra_matcher_;
|
|
|
|
Action<F> repeated_action_;
|
2009-12-23 00:13:23 +00:00
|
|
|
|
|
|
|
GTEST_DISALLOW_COPY_AND_ASSIGN_(TypedExpectation);
|
2009-07-01 19:04:51 +00:00
|
|
|
}; // class TypedExpectation
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
// A MockSpec object is used by ON_CALL() or EXPECT_CALL() for
|
|
|
|
// specifying the default behavior of, or expectation on, a mock
|
|
|
|
// function.
|
|
|
|
|
|
|
|
// Note: class MockSpec really belongs to the ::testing namespace.
|
|
|
|
// However if we define it in ::testing, MSVC will complain when
|
|
|
|
// classes in ::testing::internal declare it as a friend class
|
|
|
|
// template. To workaround this compiler bug, we define MockSpec in
|
|
|
|
// ::testing::internal and import it into ::testing.
|
|
|
|
|
2011-02-23 19:39:27 +00:00
|
|
|
// Logs a message including file and line number information.
|
2011-05-20 00:42:22 +00:00
|
|
|
GTEST_API_ void LogWithLocation(testing::internal::LogSeverity severity,
|
|
|
|
const char* file, int line,
|
2017-05-15 21:07:03 +00:00
|
|
|
const std::string& message);
|
2011-02-23 19:39:27 +00:00
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
template <typename F>
|
|
|
|
class MockSpec {
|
|
|
|
public:
|
|
|
|
typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
|
|
|
|
typedef typename internal::Function<F>::ArgumentMatcherTuple
|
|
|
|
ArgumentMatcherTuple;
|
|
|
|
|
|
|
|
// Constructs a MockSpec object, given the function mocker object
|
|
|
|
// that the spec is associated with.
|
2018-11-08 16:14:50 +00:00
|
|
|
MockSpec(internal::FunctionMocker<F>* function_mocker,
|
2018-04-05 20:09:17 +00:00
|
|
|
const ArgumentMatcherTuple& matchers)
|
|
|
|
: function_mocker_(function_mocker), matchers_(matchers) {}
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
// Adds a new default action spec to the function mocker and returns
|
|
|
|
// the newly created spec.
|
2011-02-23 19:39:27 +00:00
|
|
|
internal::OnCallSpec<F>& InternalDefaultActionSetAt(
|
2008-12-10 05:08:54 +00:00
|
|
|
const char* file, int line, const char* obj, const char* call) {
|
2012-05-31 20:40:56 +00:00
|
|
|
LogWithLocation(internal::kInfo, file, line,
|
2017-05-15 21:07:03 +00:00
|
|
|
std::string("ON_CALL(") + obj + ", " + call + ") invoked");
|
2011-02-23 19:39:27 +00:00
|
|
|
return function_mocker_->AddNewOnCallSpec(file, line, matchers_);
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Adds a new expectation spec to the function mocker and returns
|
|
|
|
// the newly created spec.
|
2009-07-01 19:04:51 +00:00
|
|
|
internal::TypedExpectation<F>& InternalExpectedAt(
|
2008-12-10 05:08:54 +00:00
|
|
|
const char* file, int line, const char* obj, const char* call) {
|
2017-05-15 21:07:03 +00:00
|
|
|
const std::string source_text(std::string("EXPECT_CALL(") + obj + ", " +
|
|
|
|
call + ")");
|
2012-05-31 20:40:56 +00:00
|
|
|
LogWithLocation(internal::kInfo, file, line, source_text + " invoked");
|
2009-10-21 06:15:34 +00:00
|
|
|
return function_mocker_->AddNewExpectation(
|
|
|
|
file, line, source_text, matchers_);
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
|
|
|
|
2018-04-18 23:28:56 +00:00
|
|
|
// This operator overload is used to swallow the superfluous parameter list
|
|
|
|
// introduced by the ON/EXPECT_CALL macros. See the macro comments for more
|
|
|
|
// explanation.
|
|
|
|
MockSpec<F>& operator()(const internal::WithoutMatchers&, void* const) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
private:
|
|
|
|
template <typename Function>
|
|
|
|
friend class internal::FunctionMocker;
|
|
|
|
|
|
|
|
// The function mocker that owns this spec.
|
2018-11-08 16:14:50 +00:00
|
|
|
internal::FunctionMocker<F>* const function_mocker_;
|
2008-12-10 05:08:54 +00:00
|
|
|
// The argument matchers specified in the spec.
|
|
|
|
ArgumentMatcherTuple matchers_;
|
2009-12-23 00:13:23 +00:00
|
|
|
|
|
|
|
GTEST_DISALLOW_ASSIGN_(MockSpec);
|
2008-12-10 05:08:54 +00:00
|
|
|
}; // class MockSpec
|
|
|
|
|
2014-01-29 06:41:44 +00:00
|
|
|
// Wrapper type for generically holding an ordinary value or lvalue reference.
|
|
|
|
// If T is not a reference type, it must be copyable or movable.
|
|
|
|
// ReferenceOrValueWrapper<T> is movable, and will also be copyable unless
|
|
|
|
// T is a move-only value type (which means that it will always be copyable
|
|
|
|
// if the current platform does not support move semantics).
|
|
|
|
//
|
|
|
|
// The primary template defines handling for values, but function header
|
|
|
|
// comments describe the contract for the whole template (including
|
|
|
|
// specializations).
|
|
|
|
template <typename T>
|
|
|
|
class ReferenceOrValueWrapper {
|
|
|
|
public:
|
|
|
|
// Constructs a wrapper from the given value/reference.
|
2014-11-17 01:14:16 +00:00
|
|
|
explicit ReferenceOrValueWrapper(T value)
|
2018-10-16 19:29:37 +00:00
|
|
|
: value_(std::move(value)) {
|
2014-11-17 01:14:16 +00:00
|
|
|
}
|
2014-01-29 06:41:44 +00:00
|
|
|
|
|
|
|
// Unwraps and returns the underlying value/reference, exactly as
|
|
|
|
// originally passed. The behavior of calling this more than once on
|
|
|
|
// the same object is unspecified.
|
2018-10-16 19:29:37 +00:00
|
|
|
T Unwrap() { return std::move(value_); }
|
2014-01-29 06:41:44 +00:00
|
|
|
|
|
|
|
// Provides nondestructive access to the underlying value/reference.
|
|
|
|
// Always returns a const reference (more precisely,
|
|
|
|
// const RemoveReference<T>&). The behavior of calling this after
|
|
|
|
// calling Unwrap on the same object is unspecified.
|
|
|
|
const T& Peek() const {
|
|
|
|
return value_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
T value_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Specialization for lvalue reference types. See primary template
|
|
|
|
// for documentation.
|
|
|
|
template <typename T>
|
|
|
|
class ReferenceOrValueWrapper<T&> {
|
|
|
|
public:
|
|
|
|
// Workaround for debatable pass-by-reference lint warning (c-library-team
|
|
|
|
// policy precludes NOLINT in this context)
|
|
|
|
typedef T& reference;
|
|
|
|
explicit ReferenceOrValueWrapper(reference ref)
|
|
|
|
: value_ptr_(&ref) {}
|
|
|
|
T& Unwrap() { return *value_ptr_; }
|
|
|
|
const T& Peek() const { return *value_ptr_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
T* value_ptr_;
|
|
|
|
};
|
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
// MSVC warns about using 'this' in base member initializer list, so
|
|
|
|
// we need to temporarily disable the warning. We have to do it for
|
|
|
|
// the entire class to suppress the warning, even though it's about
|
|
|
|
// the constructor only.
|
2018-08-20 18:48:45 +00:00
|
|
|
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4355)
|
2008-12-10 05:08:54 +00:00
|
|
|
|
2009-05-29 19:50:06 +00:00
|
|
|
// C++ treats the void type specially. For example, you cannot define
|
|
|
|
// a void-typed variable or pass a void value to a function.
|
|
|
|
// ActionResultHolder<T> holds a value of type T, where T must be a
|
|
|
|
// copyable type or void (T doesn't need to be default-constructable).
|
|
|
|
// It hides the syntactic difference between void and other types, and
|
|
|
|
// is used to unify the code for invoking both void-returning and
|
2011-02-23 19:39:27 +00:00
|
|
|
// non-void-returning mock functions.
|
|
|
|
|
|
|
|
// Untyped base class for ActionResultHolder<T>.
|
|
|
|
class UntypedActionResultHolderBase {
|
|
|
|
public:
|
|
|
|
virtual ~UntypedActionResultHolderBase() {}
|
|
|
|
|
|
|
|
// Prints the held value as an action's result to os.
|
|
|
|
virtual void PrintAsActionResult(::std::ostream* os) const = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This generic definition is used when T is not void.
|
2009-05-29 19:50:06 +00:00
|
|
|
template <typename T>
|
2011-02-23 19:39:27 +00:00
|
|
|
class ActionResultHolder : public UntypedActionResultHolderBase {
|
2009-05-29 19:50:06 +00:00
|
|
|
public:
|
2014-01-29 06:41:44 +00:00
|
|
|
// Returns the held value. Must not be called more than once.
|
|
|
|
T Unwrap() {
|
|
|
|
return result_.Unwrap();
|
2011-02-23 19:39:27 +00:00
|
|
|
}
|
2009-05-29 19:50:06 +00:00
|
|
|
|
|
|
|
// Prints the held value as an action's result to os.
|
2018-12-03 16:30:02 +00:00
|
|
|
void PrintAsActionResult(::std::ostream* os) const override {
|
2009-05-29 19:50:06 +00:00
|
|
|
*os << "\n Returns: ";
|
2010-05-13 18:16:03 +00:00
|
|
|
// T may be a reference type, so we don't use UniversalPrint().
|
2014-01-29 06:41:44 +00:00
|
|
|
UniversalPrinter<T>::Print(result_.Peek(), os);
|
2009-05-29 19:50:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Performs the given mock function's default action and returns the
|
2011-02-23 19:39:27 +00:00
|
|
|
// result in a new-ed ActionResultHolder.
|
|
|
|
template <typename F>
|
|
|
|
static ActionResultHolder* PerformDefaultAction(
|
2018-11-08 16:14:50 +00:00
|
|
|
const FunctionMocker<F>* func_mocker,
|
2018-10-16 19:29:37 +00:00
|
|
|
typename Function<F>::ArgumentTuple&& args,
|
2017-05-15 21:07:03 +00:00
|
|
|
const std::string& call_description) {
|
2018-04-05 20:09:17 +00:00
|
|
|
return new ActionResultHolder(Wrapper(func_mocker->PerformDefaultAction(
|
2018-10-16 19:29:37 +00:00
|
|
|
std::move(args), call_description)));
|
2009-05-29 19:50:06 +00:00
|
|
|
}
|
|
|
|
|
2011-02-23 19:39:27 +00:00
|
|
|
// Performs the given action and returns the result in a new-ed
|
2009-05-29 19:50:06 +00:00
|
|
|
// ActionResultHolder.
|
2011-02-23 19:39:27 +00:00
|
|
|
template <typename F>
|
2018-04-05 20:09:17 +00:00
|
|
|
static ActionResultHolder* PerformAction(
|
2018-10-16 19:29:37 +00:00
|
|
|
const Action<F>& action, typename Function<F>::ArgumentTuple&& args) {
|
2018-04-05 20:09:17 +00:00
|
|
|
return new ActionResultHolder(
|
2018-10-16 19:29:37 +00:00
|
|
|
Wrapper(action.Perform(std::move(args))));
|
2009-05-29 19:50:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-01-29 06:41:44 +00:00
|
|
|
typedef ReferenceOrValueWrapper<T> Wrapper;
|
2009-12-23 00:13:23 +00:00
|
|
|
|
2014-11-17 01:14:16 +00:00
|
|
|
explicit ActionResultHolder(Wrapper result)
|
2018-10-16 19:29:37 +00:00
|
|
|
: result_(std::move(result)) {
|
2014-11-17 01:14:16 +00:00
|
|
|
}
|
2014-01-29 06:41:44 +00:00
|
|
|
|
|
|
|
Wrapper result_;
|
|
|
|
|
|
|
|
GTEST_DISALLOW_COPY_AND_ASSIGN_(ActionResultHolder);
|
2009-05-29 19:50:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Specialization for T = void.
|
|
|
|
template <>
|
2011-02-23 19:39:27 +00:00
|
|
|
class ActionResultHolder<void> : public UntypedActionResultHolderBase {
|
2009-05-29 19:50:06 +00:00
|
|
|
public:
|
2014-01-29 06:41:44 +00:00
|
|
|
void Unwrap() { }
|
2011-02-23 19:39:27 +00:00
|
|
|
|
2018-12-03 16:30:02 +00:00
|
|
|
void PrintAsActionResult(::std::ostream* /* os */) const override {}
|
2011-02-23 19:39:27 +00:00
|
|
|
|
2014-01-29 06:41:44 +00:00
|
|
|
// Performs the given mock function's default action and returns ownership
|
|
|
|
// of an empty ActionResultHolder*.
|
2011-02-23 19:39:27 +00:00
|
|
|
template <typename F>
|
|
|
|
static ActionResultHolder* PerformDefaultAction(
|
2018-11-08 16:14:50 +00:00
|
|
|
const FunctionMocker<F>* func_mocker,
|
2018-10-16 19:29:37 +00:00
|
|
|
typename Function<F>::ArgumentTuple&& args,
|
2017-05-15 21:07:03 +00:00
|
|
|
const std::string& call_description) {
|
2018-10-16 19:29:37 +00:00
|
|
|
func_mocker->PerformDefaultAction(std::move(args), call_description);
|
2014-01-29 06:41:44 +00:00
|
|
|
return new ActionResultHolder;
|
2009-05-29 19:50:06 +00:00
|
|
|
}
|
|
|
|
|
2014-01-29 06:41:44 +00:00
|
|
|
// Performs the given action and returns ownership of an empty
|
|
|
|
// ActionResultHolder*.
|
2011-02-23 19:39:27 +00:00
|
|
|
template <typename F>
|
|
|
|
static ActionResultHolder* PerformAction(
|
2018-10-16 19:29:37 +00:00
|
|
|
const Action<F>& action, typename Function<F>::ArgumentTuple&& args) {
|
|
|
|
action.Perform(std::move(args));
|
2014-01-29 06:41:44 +00:00
|
|
|
return new ActionResultHolder;
|
2009-05-29 19:50:06 +00:00
|
|
|
}
|
2014-01-29 06:41:44 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
ActionResultHolder() {}
|
|
|
|
GTEST_DISALLOW_COPY_AND_ASSIGN_(ActionResultHolder);
|
2009-05-29 19:50:06 +00:00
|
|
|
};
|
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
template <typename F>
|
2018-11-08 16:14:50 +00:00
|
|
|
class FunctionMocker;
|
|
|
|
|
|
|
|
template <typename R, typename... Args>
|
2019-01-22 14:58:06 +00:00
|
|
|
class FunctionMocker<R(Args...)> final : public UntypedFunctionMockerBase {
|
2018-11-08 16:14:50 +00:00
|
|
|
using F = R(Args...);
|
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
public:
|
2018-11-08 16:14:50 +00:00
|
|
|
using Result = R;
|
|
|
|
using ArgumentTuple = std::tuple<Args...>;
|
|
|
|
using ArgumentMatcherTuple = std::tuple<Matcher<Args>...>;
|
2008-12-10 05:08:54 +00:00
|
|
|
|
2018-11-08 16:14:50 +00:00
|
|
|
FunctionMocker() {}
|
|
|
|
|
|
|
|
// There is no generally useful and implementable semantics of
|
|
|
|
// copying a mock object, so copying a mock is usually a user error.
|
|
|
|
// Thus we disallow copying function mockers. If the user really
|
|
|
|
// wants to copy a mock object, they should implement their own copy
|
|
|
|
// operation, for example:
|
|
|
|
//
|
|
|
|
// class MockFoo : public Foo {
|
|
|
|
// public:
|
|
|
|
// // Defines a copy constructor explicitly.
|
|
|
|
// MockFoo(const MockFoo& src) {}
|
|
|
|
// ...
|
|
|
|
// };
|
|
|
|
FunctionMocker(const FunctionMocker&) = delete;
|
|
|
|
FunctionMocker& operator=(const FunctionMocker&) = delete;
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
// The destructor verifies that all expectations on this mock
|
|
|
|
// function have been satisfied. If not, it will report Google Test
|
|
|
|
// non-fatal failures for the violations.
|
2018-12-03 16:30:02 +00:00
|
|
|
~FunctionMocker() override GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
|
2008-12-10 05:08:54 +00:00
|
|
|
MutexLock l(&g_gmock_mutex);
|
|
|
|
VerifyAndClearExpectationsLocked();
|
|
|
|
Mock::UnregisterLocked(this);
|
2011-02-23 19:39:27 +00:00
|
|
|
ClearDefaultActionsLocked();
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the ON_CALL spec that matches this mock function with the
|
|
|
|
// given arguments; returns NULL if no matching ON_CALL is found.
|
|
|
|
// L = *
|
2011-02-23 19:39:27 +00:00
|
|
|
const OnCallSpec<F>* FindOnCallSpec(
|
2008-12-10 05:08:54 +00:00
|
|
|
const ArgumentTuple& args) const {
|
2011-02-23 19:39:27 +00:00
|
|
|
for (UntypedOnCallSpecs::const_reverse_iterator it
|
|
|
|
= untyped_on_call_specs_.rbegin();
|
|
|
|
it != untyped_on_call_specs_.rend(); ++it) {
|
|
|
|
const OnCallSpec<F>* spec = static_cast<const OnCallSpec<F>*>(*it);
|
|
|
|
if (spec->Matches(args))
|
|
|
|
return spec;
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
|
|
|
|
2018-10-04 22:28:05 +00:00
|
|
|
return nullptr;
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
|
|
|
|
2013-02-28 22:58:51 +00:00
|
|
|
// Performs the default action of this mock function on the given
|
|
|
|
// arguments and returns the result. Asserts (or throws if
|
|
|
|
// exceptions are enabled) with a helpful call descrption if there
|
|
|
|
// is no valid return value. This method doesn't depend on the
|
|
|
|
// mutable state of this object, and thus can be called concurrently
|
|
|
|
// without locking.
|
2008-12-10 05:08:54 +00:00
|
|
|
// L = *
|
2018-11-08 16:14:50 +00:00
|
|
|
Result PerformDefaultAction(ArgumentTuple&& args,
|
2018-10-16 19:29:37 +00:00
|
|
|
const std::string& call_description) const {
|
2011-02-23 19:39:27 +00:00
|
|
|
const OnCallSpec<F>* const spec =
|
|
|
|
this->FindOnCallSpec(args);
|
2018-10-04 22:28:05 +00:00
|
|
|
if (spec != nullptr) {
|
2018-10-16 19:29:37 +00:00
|
|
|
return spec->GetAction().Perform(std::move(args));
|
2009-01-27 22:28:45 +00:00
|
|
|
}
|
2017-05-15 21:07:03 +00:00
|
|
|
const std::string message =
|
|
|
|
call_description +
|
2013-02-28 22:58:51 +00:00
|
|
|
"\n The mock function has no default action "
|
|
|
|
"set, and its return type has no default value set.";
|
|
|
|
#if GTEST_HAS_EXCEPTIONS
|
|
|
|
if (!DefaultValue<Result>::Exists()) {
|
|
|
|
throw std::runtime_error(message);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
Assert(DefaultValue<Result>::Exists(), "", -1, message);
|
|
|
|
#endif
|
2009-01-27 22:28:45 +00:00
|
|
|
return DefaultValue<Result>::Get();
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
|
|
|
|
2011-02-23 19:39:27 +00:00
|
|
|
// Performs the default action with the given arguments and returns
|
|
|
|
// the action's result. The call description string will be used in
|
|
|
|
// the error message to describe the call in the case the default
|
|
|
|
// action fails. The caller is responsible for deleting the result.
|
|
|
|
// L = *
|
2018-12-03 16:30:02 +00:00
|
|
|
UntypedActionResultHolderBase* UntypedPerformDefaultAction(
|
2018-04-05 20:09:17 +00:00
|
|
|
void* untyped_args, // must point to an ArgumentTuple
|
2018-12-03 16:30:02 +00:00
|
|
|
const std::string& call_description) const override {
|
2018-04-05 20:09:17 +00:00
|
|
|
ArgumentTuple* args = static_cast<ArgumentTuple*>(untyped_args);
|
2018-10-16 19:29:37 +00:00
|
|
|
return ResultHolder::PerformDefaultAction(this, std::move(*args),
|
2018-04-05 20:09:17 +00:00
|
|
|
call_description);
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
|
|
|
|
2011-02-23 19:39:27 +00:00
|
|
|
// Performs the given action with the given arguments and returns
|
|
|
|
// the action's result. The caller is responsible for deleting the
|
|
|
|
// result.
|
|
|
|
// L = *
|
2018-12-03 16:30:02 +00:00
|
|
|
UntypedActionResultHolderBase* UntypedPerformAction(
|
|
|
|
const void* untyped_action, void* untyped_args) const override {
|
2011-02-23 19:39:27 +00:00
|
|
|
// Make a copy of the action before performing it, in case the
|
|
|
|
// action deletes the mock object (and thus deletes itself).
|
|
|
|
const Action<F> action = *static_cast<const Action<F>*>(untyped_action);
|
2018-04-05 20:09:17 +00:00
|
|
|
ArgumentTuple* args = static_cast<ArgumentTuple*>(untyped_args);
|
2018-10-16 19:29:37 +00:00
|
|
|
return ResultHolder::PerformAction(action, std::move(*args));
|
2011-02-23 19:39:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implements UntypedFunctionMockerBase::ClearDefaultActionsLocked():
|
|
|
|
// clears the ON_CALL()s set on this mock function.
|
2018-12-03 16:30:02 +00:00
|
|
|
void ClearDefaultActionsLocked() override
|
2011-10-24 21:16:22 +00:00
|
|
|
GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
|
2008-12-10 05:08:54 +00:00
|
|
|
g_gmock_mutex.AssertHeld();
|
2011-10-24 23:41:07 +00:00
|
|
|
|
|
|
|
// Deleting our default actions may trigger other mock objects to be
|
|
|
|
// deleted, for example if an action contains a reference counted smart
|
|
|
|
// pointer to that mock object, and that is the last reference. So if we
|
|
|
|
// delete our actions within the context of the global mutex we may deadlock
|
|
|
|
// when this method is called again. Instead, make a copy of the set of
|
|
|
|
// actions to delete, clear our set within the mutex, and then delete the
|
|
|
|
// actions outside of the mutex.
|
|
|
|
UntypedOnCallSpecs specs_to_delete;
|
|
|
|
untyped_on_call_specs_.swap(specs_to_delete);
|
|
|
|
|
|
|
|
g_gmock_mutex.Unlock();
|
2011-02-23 19:39:27 +00:00
|
|
|
for (UntypedOnCallSpecs::const_iterator it =
|
2011-10-24 23:41:07 +00:00
|
|
|
specs_to_delete.begin();
|
|
|
|
it != specs_to_delete.end(); ++it) {
|
2011-02-23 19:39:27 +00:00
|
|
|
delete static_cast<const OnCallSpec<F>*>(*it);
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
2011-10-24 23:41:07 +00:00
|
|
|
|
|
|
|
// Lock the mutex again, since the caller expects it to be locked when we
|
|
|
|
// return.
|
|
|
|
g_gmock_mutex.Lock();
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
2009-12-23 00:13:23 +00:00
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
// Returns the result of invoking this mock function with the given
|
|
|
|
// arguments. This function can be safely called from multiple
|
|
|
|
// threads concurrently.
|
2018-11-08 16:14:50 +00:00
|
|
|
Result Invoke(Args... args) GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
|
|
|
|
ArgumentTuple tuple(std::forward<Args>(args)...);
|
|
|
|
std::unique_ptr<ResultHolder> holder(DownCast_<ResultHolder*>(
|
|
|
|
this->UntypedInvokeWith(static_cast<void*>(&tuple))));
|
2014-01-29 06:41:44 +00:00
|
|
|
return holder->Unwrap();
|
2011-02-23 19:39:27 +00:00
|
|
|
}
|
2008-12-10 05:08:54 +00:00
|
|
|
|
2018-11-08 16:14:50 +00:00
|
|
|
MockSpec<F> With(Matcher<Args>... m) {
|
|
|
|
return MockSpec<F>(this, ::std::make_tuple(std::move(m)...));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
template <typename Function>
|
|
|
|
friend class MockSpec;
|
|
|
|
|
|
|
|
typedef ActionResultHolder<Result> ResultHolder;
|
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
// Adds and returns a default action spec for this mock function.
|
2011-02-23 19:39:27 +00:00
|
|
|
OnCallSpec<F>& AddNewOnCallSpec(
|
2008-12-10 05:08:54 +00:00
|
|
|
const char* file, int line,
|
2011-10-24 21:16:22 +00:00
|
|
|
const ArgumentMatcherTuple& m)
|
|
|
|
GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
|
2009-04-22 22:25:31 +00:00
|
|
|
Mock::RegisterUseByOnCallOrExpectCall(MockObject(), file, line);
|
2011-02-23 19:39:27 +00:00
|
|
|
OnCallSpec<F>* const on_call_spec = new OnCallSpec<F>(file, line, m);
|
|
|
|
untyped_on_call_specs_.push_back(on_call_spec);
|
|
|
|
return *on_call_spec;
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Adds and returns an expectation spec for this mock function.
|
2017-05-15 21:07:03 +00:00
|
|
|
TypedExpectation<F>& AddNewExpectation(const char* file, int line,
|
|
|
|
const std::string& source_text,
|
|
|
|
const ArgumentMatcherTuple& m)
|
|
|
|
GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
|
2009-04-22 22:25:31 +00:00
|
|
|
Mock::RegisterUseByOnCallOrExpectCall(MockObject(), file, line);
|
2011-02-23 19:39:27 +00:00
|
|
|
TypedExpectation<F>* const expectation =
|
|
|
|
new TypedExpectation<F>(this, file, line, source_text, m);
|
2018-10-29 15:09:33 +00:00
|
|
|
const std::shared_ptr<ExpectationBase> untyped_expectation(expectation);
|
2018-04-05 20:09:17 +00:00
|
|
|
// See the definition of untyped_expectations_ for why access to
|
|
|
|
// it is unprotected here.
|
2011-02-23 19:39:27 +00:00
|
|
|
untyped_expectations_.push_back(untyped_expectation);
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
// Adds this expectation into the implicit sequence if there is one.
|
|
|
|
Sequence* const implicit_sequence = g_gmock_implicit_sequence.get();
|
2018-10-04 22:28:05 +00:00
|
|
|
if (implicit_sequence != nullptr) {
|
2011-02-23 19:39:27 +00:00
|
|
|
implicit_sequence->AddExpectation(Expectation(untyped_expectation));
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return *expectation;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2009-07-01 19:04:51 +00:00
|
|
|
template <typename Func> friend class TypedExpectation;
|
2008-12-10 05:08:54 +00:00
|
|
|
|
2011-02-23 19:39:27 +00:00
|
|
|
// Some utilities needed for implementing UntypedInvokeWith().
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
// Describes what default action will be performed for the given
|
|
|
|
// arguments.
|
|
|
|
// L = *
|
|
|
|
void DescribeDefaultActionTo(const ArgumentTuple& args,
|
|
|
|
::std::ostream* os) const {
|
2011-02-23 19:39:27 +00:00
|
|
|
const OnCallSpec<F>* const spec = FindOnCallSpec(args);
|
2008-12-10 05:08:54 +00:00
|
|
|
|
2018-10-04 22:28:05 +00:00
|
|
|
if (spec == nullptr) {
|
2008-12-10 05:08:54 +00:00
|
|
|
*os << (internal::type_equals<Result, void>::value ?
|
|
|
|
"returning directly.\n" :
|
|
|
|
"returning default value.\n");
|
|
|
|
} else {
|
|
|
|
*os << "taking default action specified at:\n"
|
2011-02-11 23:50:38 +00:00
|
|
|
<< FormatFileLocation(spec->file(), spec->line()) << "\n";
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Writes a message that the call is uninteresting (i.e. neither
|
|
|
|
// explicitly expected nor explicitly unexpected) to the given
|
|
|
|
// ostream.
|
2018-12-03 16:30:02 +00:00
|
|
|
void UntypedDescribeUninterestingCall(const void* untyped_args,
|
|
|
|
::std::ostream* os) const override
|
|
|
|
GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
|
2011-02-23 19:39:27 +00:00
|
|
|
const ArgumentTuple& args =
|
|
|
|
*static_cast<const ArgumentTuple*>(untyped_args);
|
2008-12-10 05:08:54 +00:00
|
|
|
*os << "Uninteresting mock function call - ";
|
|
|
|
DescribeDefaultActionTo(args, os);
|
|
|
|
*os << " Function call: " << Name();
|
2010-05-13 18:16:03 +00:00
|
|
|
UniversalPrint(args, os);
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
|
|
|
|
2011-02-23 19:39:27 +00:00
|
|
|
// Returns the expectation that matches the given function arguments
|
|
|
|
// (or NULL is there's no match); when a match is found,
|
|
|
|
// untyped_action is set to point to the action that should be
|
|
|
|
// performed (or NULL if the action is "do default"), and
|
|
|
|
// is_excessive is modified to indicate whether the call exceeds the
|
|
|
|
// expected number.
|
|
|
|
//
|
2008-12-10 05:08:54 +00:00
|
|
|
// Critical section: We must find the matching expectation and the
|
|
|
|
// corresponding action that needs to be taken in an ATOMIC
|
|
|
|
// transaction. Otherwise another thread may call this mock
|
|
|
|
// method in the middle and mess up the state.
|
|
|
|
//
|
|
|
|
// However, performing the action has to be left out of the critical
|
|
|
|
// section. The reason is that we have no control on what the
|
|
|
|
// action does (it can invoke an arbitrary user function or even a
|
|
|
|
// mock function) and excessive locking could cause a dead lock.
|
2018-12-03 16:30:02 +00:00
|
|
|
const ExpectationBase* UntypedFindMatchingExpectation(
|
|
|
|
const void* untyped_args, const void** untyped_action, bool* is_excessive,
|
|
|
|
::std::ostream* what, ::std::ostream* why) override
|
|
|
|
GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
|
2011-02-23 19:39:27 +00:00
|
|
|
const ArgumentTuple& args =
|
|
|
|
*static_cast<const ArgumentTuple*>(untyped_args);
|
2008-12-10 05:08:54 +00:00
|
|
|
MutexLock l(&g_gmock_mutex);
|
2011-02-23 19:39:27 +00:00
|
|
|
TypedExpectation<F>* exp = this->FindMatchingExpectationLocked(args);
|
2018-10-04 22:28:05 +00:00
|
|
|
if (exp == nullptr) { // A match wasn't found.
|
2008-12-10 05:08:54 +00:00
|
|
|
this->FormatUnexpectedCallMessageLocked(args, what, why);
|
2018-10-04 22:28:05 +00:00
|
|
|
return nullptr;
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This line must be done before calling GetActionForArguments(),
|
|
|
|
// which will increment the call count for *exp and thus affect
|
|
|
|
// its saturation status.
|
2011-02-23 19:39:27 +00:00
|
|
|
*is_excessive = exp->IsSaturated();
|
|
|
|
const Action<F>* action = exp->GetActionForArguments(this, args, what, why);
|
2018-10-04 22:28:05 +00:00
|
|
|
if (action != nullptr && action->IsDoDefault())
|
|
|
|
action = nullptr; // Normalize "do default" to NULL.
|
2011-02-23 19:39:27 +00:00
|
|
|
*untyped_action = action;
|
|
|
|
return exp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prints the given function arguments to the ostream.
|
2018-12-03 16:30:02 +00:00
|
|
|
void UntypedPrintArgs(const void* untyped_args,
|
|
|
|
::std::ostream* os) const override {
|
2011-02-23 19:39:27 +00:00
|
|
|
const ArgumentTuple& args =
|
|
|
|
*static_cast<const ArgumentTuple*>(untyped_args);
|
|
|
|
UniversalPrint(args, os);
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the expectation that matches the arguments, or NULL if no
|
|
|
|
// expectation matches them.
|
2009-07-01 19:04:51 +00:00
|
|
|
TypedExpectation<F>* FindMatchingExpectationLocked(
|
2011-10-24 21:16:22 +00:00
|
|
|
const ArgumentTuple& args) const
|
|
|
|
GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
|
2008-12-10 05:08:54 +00:00
|
|
|
g_gmock_mutex.AssertHeld();
|
2018-04-05 20:09:17 +00:00
|
|
|
// See the definition of untyped_expectations_ for why access to
|
|
|
|
// it is unprotected here.
|
2011-02-23 19:39:27 +00:00
|
|
|
for (typename UntypedExpectations::const_reverse_iterator it =
|
|
|
|
untyped_expectations_.rbegin();
|
|
|
|
it != untyped_expectations_.rend(); ++it) {
|
|
|
|
TypedExpectation<F>* const exp =
|
|
|
|
static_cast<TypedExpectation<F>*>(it->get());
|
2008-12-10 05:08:54 +00:00
|
|
|
if (exp->ShouldHandleArguments(args)) {
|
|
|
|
return exp;
|
|
|
|
}
|
|
|
|
}
|
2018-10-04 22:28:05 +00:00
|
|
|
return nullptr;
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a message that the arguments don't match any expectation.
|
2011-10-24 21:16:22 +00:00
|
|
|
void FormatUnexpectedCallMessageLocked(
|
|
|
|
const ArgumentTuple& args,
|
|
|
|
::std::ostream* os,
|
|
|
|
::std::ostream* why) const
|
|
|
|
GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
|
2008-12-10 05:08:54 +00:00
|
|
|
g_gmock_mutex.AssertHeld();
|
|
|
|
*os << "\nUnexpected mock function call - ";
|
|
|
|
DescribeDefaultActionTo(args, os);
|
|
|
|
PrintTriedExpectationsLocked(args, why);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prints a list of expectations that have been tried against the
|
|
|
|
// current mock function call.
|
2011-10-24 21:16:22 +00:00
|
|
|
void PrintTriedExpectationsLocked(
|
|
|
|
const ArgumentTuple& args,
|
|
|
|
::std::ostream* why) const
|
|
|
|
GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
|
2008-12-10 05:08:54 +00:00
|
|
|
g_gmock_mutex.AssertHeld();
|
2018-10-12 17:12:55 +00:00
|
|
|
const size_t count = untyped_expectations_.size();
|
2008-12-10 05:08:54 +00:00
|
|
|
*why << "Google Mock tried the following " << count << " "
|
|
|
|
<< (count == 1 ? "expectation, but it didn't match" :
|
|
|
|
"expectations, but none matched")
|
|
|
|
<< ":\n";
|
2018-10-12 17:12:55 +00:00
|
|
|
for (size_t i = 0; i < count; i++) {
|
2011-02-23 19:39:27 +00:00
|
|
|
TypedExpectation<F>* const expectation =
|
|
|
|
static_cast<TypedExpectation<F>*>(untyped_expectations_[i].get());
|
2008-12-10 05:08:54 +00:00
|
|
|
*why << "\n";
|
2011-02-23 19:39:27 +00:00
|
|
|
expectation->DescribeLocationTo(why);
|
2008-12-10 05:08:54 +00:00
|
|
|
if (count > 1) {
|
2009-10-21 06:15:34 +00:00
|
|
|
*why << "tried expectation #" << i << ": ";
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
2011-02-23 19:39:27 +00:00
|
|
|
*why << expectation->source_text() << "...\n";
|
|
|
|
expectation->ExplainMatchResultTo(args, why);
|
|
|
|
expectation->DescribeCallCountTo(why);
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-08 16:14:50 +00:00
|
|
|
}; // class FunctionMocker
|
2008-12-10 05:08:54 +00:00
|
|
|
|
2018-08-20 18:48:45 +00:00
|
|
|
GTEST_DISABLE_MSC_WARNINGS_POP_() // 4355
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
// Reports an uninteresting call (whose description is in msg) in the
|
|
|
|
// manner specified by 'reaction'.
|
2017-05-15 21:07:03 +00:00
|
|
|
void ReportUninterestingCall(CallReaction reaction, const std::string& msg);
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
|
2018-11-08 16:14:50 +00:00
|
|
|
// A MockFunction<F> class has one mock method whose type is F. It is
|
|
|
|
// useful when you just want your test code to emit some messages and
|
|
|
|
// have Google Mock verify the right messages are sent (and perhaps at
|
|
|
|
// the right times). For example, if you are exercising code:
|
|
|
|
//
|
|
|
|
// Foo(1);
|
|
|
|
// Foo(2);
|
|
|
|
// Foo(3);
|
|
|
|
//
|
|
|
|
// and want to verify that Foo(1) and Foo(3) both invoke
|
|
|
|
// mock.Bar("a"), but Foo(2) doesn't invoke anything, you can write:
|
|
|
|
//
|
|
|
|
// TEST(FooTest, InvokesBarCorrectly) {
|
|
|
|
// MyMock mock;
|
|
|
|
// MockFunction<void(string check_point_name)> check;
|
|
|
|
// {
|
|
|
|
// InSequence s;
|
|
|
|
//
|
|
|
|
// EXPECT_CALL(mock, Bar("a"));
|
|
|
|
// EXPECT_CALL(check, Call("1"));
|
|
|
|
// EXPECT_CALL(check, Call("2"));
|
|
|
|
// EXPECT_CALL(mock, Bar("a"));
|
|
|
|
// }
|
|
|
|
// Foo(1);
|
|
|
|
// check.Call("1");
|
|
|
|
// Foo(2);
|
|
|
|
// check.Call("2");
|
|
|
|
// Foo(3);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// The expectation spec says that the first Bar("a") must happen
|
|
|
|
// before check point "1", the second Bar("a") must happen after check
|
|
|
|
// point "2", and nothing should happen between the two check
|
|
|
|
// points. The explicit check points make it easy to tell which
|
|
|
|
// Bar("a") is called by which call to Foo().
|
|
|
|
//
|
|
|
|
// MockFunction<F> can also be used to exercise code that accepts
|
|
|
|
// std::function<F> callbacks. To do so, use AsStdFunction() method
|
|
|
|
// to create std::function proxy forwarding to original object's Call.
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// TEST(FooTest, RunsCallbackWithBarArgument) {
|
|
|
|
// MockFunction<int(string)> callback;
|
|
|
|
// EXPECT_CALL(callback, Call("bar")).WillOnce(Return(1));
|
|
|
|
// Foo(callback.AsStdFunction());
|
|
|
|
// }
|
|
|
|
template <typename F>
|
|
|
|
class MockFunction;
|
|
|
|
|
|
|
|
template <typename R, typename... Args>
|
|
|
|
class MockFunction<R(Args...)> {
|
|
|
|
public:
|
|
|
|
MockFunction() {}
|
|
|
|
MockFunction(const MockFunction&) = delete;
|
|
|
|
MockFunction& operator=(const MockFunction&) = delete;
|
|
|
|
|
|
|
|
std::function<R(Args...)> AsStdFunction() {
|
|
|
|
return [this](Args... args) -> R {
|
|
|
|
return this->Call(std::forward<Args>(args)...);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implementation detail: the expansion of the MOCK_METHOD macro.
|
|
|
|
R Call(Args... args) {
|
|
|
|
mock_.SetOwnerAndName(this, "Call");
|
|
|
|
return mock_.Invoke(std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal::MockSpec<R(Args...)> gmock_Call(Matcher<Args>... m) {
|
|
|
|
mock_.RegisterOwner(this);
|
|
|
|
return mock_.With(std::move(m)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal::MockSpec<R(Args...)> gmock_Call(const internal::WithoutMatchers&,
|
|
|
|
R (*)(Args...)) {
|
|
|
|
return this->gmock_Call(::testing::A<Args>()...);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
mutable internal::FunctionMocker<R(Args...)> mock_;
|
|
|
|
};
|
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
// The style guide prohibits "using" statements in a namespace scope
|
|
|
|
// inside a header file. However, the MockSpec class template is
|
|
|
|
// meant to be defined in the ::testing namespace. The following line
|
|
|
|
// is just a trick for working around a bug in MSVC 8.0, which cannot
|
|
|
|
// handle it if we define MockSpec in ::testing.
|
|
|
|
using internal::MockSpec;
|
|
|
|
|
|
|
|
// Const(x) is a convenient function for obtaining a const reference
|
|
|
|
// to x. This is useful for setting expectations on an overloaded
|
|
|
|
// const mock method, e.g.
|
|
|
|
//
|
|
|
|
// class MockFoo : public FooInterface {
|
|
|
|
// public:
|
|
|
|
// MOCK_METHOD0(Bar, int());
|
|
|
|
// MOCK_CONST_METHOD0(Bar, int&());
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// MockFoo foo;
|
|
|
|
// // Expects a call to non-const MockFoo::Bar().
|
|
|
|
// EXPECT_CALL(foo, Bar());
|
|
|
|
// // Expects a call to const MockFoo::Bar().
|
|
|
|
// EXPECT_CALL(Const(foo), Bar());
|
|
|
|
template <typename T>
|
|
|
|
inline const T& Const(const T& x) { return x; }
|
|
|
|
|
2009-07-01 19:04:51 +00:00
|
|
|
// Constructs an Expectation object that references and co-owns exp.
|
|
|
|
inline Expectation::Expectation(internal::ExpectationBase& exp) // NOLINT
|
|
|
|
: expectation_base_(exp.GetHandle().expectation_base()) {}
|
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
} // namespace testing
|
|
|
|
|
2018-08-20 18:48:45 +00:00
|
|
|
GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251
|
|
|
|
|
2018-04-18 23:28:56 +00:00
|
|
|
// Implementation for ON_CALL and EXPECT_CALL macros. A separate macro is
|
|
|
|
// required to avoid compile errors when the name of the method used in call is
|
|
|
|
// a result of macro expansion. See CompilesWithMethodNameExpandedFromMacro
|
|
|
|
// tests in internal/gmock-spec-builders_test.cc for more details.
|
|
|
|
//
|
|
|
|
// This macro supports statements both with and without parameter matchers. If
|
|
|
|
// the parameter list is omitted, gMock will accept any parameters, which allows
|
|
|
|
// tests to be written that don't need to encode the number of method
|
|
|
|
// parameter. This technique may only be used for non-overloaded methods.
|
|
|
|
//
|
|
|
|
// // These are the same:
|
2018-07-17 07:46:47 +00:00
|
|
|
// ON_CALL(mock, NoArgsMethod()).WillByDefault(...);
|
|
|
|
// ON_CALL(mock, NoArgsMethod).WillByDefault(...);
|
2018-04-18 23:28:56 +00:00
|
|
|
//
|
|
|
|
// // As are these:
|
2018-07-17 07:46:47 +00:00
|
|
|
// ON_CALL(mock, TwoArgsMethod(_, _)).WillByDefault(...);
|
|
|
|
// ON_CALL(mock, TwoArgsMethod).WillByDefault(...);
|
2018-04-18 23:28:56 +00:00
|
|
|
//
|
|
|
|
// // Can also specify args if you want, of course:
|
2018-07-17 07:46:47 +00:00
|
|
|
// ON_CALL(mock, TwoArgsMethod(_, 45)).WillByDefault(...);
|
2018-04-18 23:28:56 +00:00
|
|
|
//
|
|
|
|
// // Overloads work as long as you specify parameters:
|
2018-07-17 07:46:47 +00:00
|
|
|
// ON_CALL(mock, OverloadedMethod(_)).WillByDefault(...);
|
|
|
|
// ON_CALL(mock, OverloadedMethod(_, _)).WillByDefault(...);
|
2018-04-18 23:28:56 +00:00
|
|
|
//
|
|
|
|
// // Oops! Which overload did you want?
|
2018-07-17 07:46:47 +00:00
|
|
|
// ON_CALL(mock, OverloadedMethod).WillByDefault(...);
|
2018-04-18 23:28:56 +00:00
|
|
|
// => ERROR: call to member function 'gmock_OverloadedMethod' is ambiguous
|
|
|
|
//
|
|
|
|
// How this works: The mock class uses two overloads of the gmock_Method
|
|
|
|
// expectation setter method plus an operator() overload on the MockSpec object.
|
|
|
|
// In the matcher list form, the macro expands to:
|
|
|
|
//
|
|
|
|
// // This statement:
|
2018-07-17 07:46:47 +00:00
|
|
|
// ON_CALL(mock, TwoArgsMethod(_, 45))...
|
2018-04-18 23:28:56 +00:00
|
|
|
//
|
2018-07-17 07:46:47 +00:00
|
|
|
// // ...expands to:
|
|
|
|
// mock.gmock_TwoArgsMethod(_, 45)(WithoutMatchers(), nullptr)...
|
2018-04-18 23:28:56 +00:00
|
|
|
// |-------------v---------------||------------v-------------|
|
|
|
|
// invokes first overload swallowed by operator()
|
|
|
|
//
|
2018-07-17 07:46:47 +00:00
|
|
|
// // ...which is essentially:
|
|
|
|
// mock.gmock_TwoArgsMethod(_, 45)...
|
2018-04-18 23:28:56 +00:00
|
|
|
//
|
|
|
|
// Whereas the form without a matcher list:
|
|
|
|
//
|
|
|
|
// // This statement:
|
2018-07-17 07:46:47 +00:00
|
|
|
// ON_CALL(mock, TwoArgsMethod)...
|
2018-04-18 23:28:56 +00:00
|
|
|
//
|
2018-07-17 07:46:47 +00:00
|
|
|
// // ...expands to:
|
|
|
|
// mock.gmock_TwoArgsMethod(WithoutMatchers(), nullptr)...
|
2018-04-18 23:28:56 +00:00
|
|
|
// |-----------------------v--------------------------|
|
|
|
|
// invokes second overload
|
|
|
|
//
|
2018-07-17 07:46:47 +00:00
|
|
|
// // ...which is essentially:
|
|
|
|
// mock.gmock_TwoArgsMethod(_, _)...
|
2018-04-18 23:28:56 +00:00
|
|
|
//
|
|
|
|
// The WithoutMatchers() argument is used to disambiguate overloads and to
|
|
|
|
// block the caller from accidentally invoking the second overload directly. The
|
|
|
|
// second argument is an internal type derived from the method signature. The
|
|
|
|
// failure to disambiguate two overloads of this method in the ON_CALL statement
|
|
|
|
// is how we block callers from setting expectations on overloaded methods.
|
2018-10-29 17:46:24 +00:00
|
|
|
#define GMOCK_ON_CALL_IMPL_(mock_expr, Setter, call) \
|
|
|
|
((mock_expr).gmock_##call)(::testing::internal::GetWithoutMatchers(), \
|
|
|
|
nullptr) \
|
2018-04-18 23:28:56 +00:00
|
|
|
.Setter(__FILE__, __LINE__, #mock_expr, #call)
|
|
|
|
|
|
|
|
#define ON_CALL(obj, call) \
|
|
|
|
GMOCK_ON_CALL_IMPL_(obj, InternalDefaultActionSetAt, call)
|
|
|
|
|
|
|
|
#define EXPECT_CALL(obj, call) \
|
|
|
|
GMOCK_ON_CALL_IMPL_(obj, InternalExpectedAt, call)
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
#endif // GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_
|