1dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// Copyright 2007, Google Inc. 2dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// All rights reserved. 3dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// 4dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// Redistribution and use in source and binary forms, with or without 5dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// modification, are permitted provided that the following conditions are 6dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// met: 7dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// 8dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// * Redistributions of source code must retain the above copyright 9dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// notice, this list of conditions and the following disclaimer. 10dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// * Redistributions in binary form must reproduce the above 11dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// copyright notice, this list of conditions and the following disclaimer 12dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// in the documentation and/or other materials provided with the 13dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// distribution. 14dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// * Neither the name of Google Inc. nor the names of its 15dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// contributors may be used to endorse or promote products derived from 16dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// this software without specific prior written permission. 17dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// 18dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// 30dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// Author: wan@google.com (Zhanyong Wan) 31dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 32dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// Google Mock - a framework for writing C++ mock classes. 33dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// 34dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// This file implements the ON_CALL() and EXPECT_CALL() macros. 35dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// 36dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// A user can use the ON_CALL() macro to specify the default action of 37dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// a mock method. The syntax is: 38dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// 39dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// ON_CALL(mock_object, Method(argument-matchers)) 40dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// .With(multi-argument-matcher) 41dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// .WillByDefault(action); 42dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// 43dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// where the .With() clause is optional. 44dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// 45dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// A user can use the EXPECT_CALL() macro to specify an expectation on 46dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// a mock method. The syntax is: 47dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// 48dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// EXPECT_CALL(mock_object, Method(argument-matchers)) 49dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// .With(multi-argument-matchers) 50dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// .Times(cardinality) 51dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// .InSequence(sequences) 5246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// .After(expectations) 53dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// .WillOnce(action) 54dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// .WillRepeatedly(action) 55dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// .RetiresOnSaturation(); 56dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// 5746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// where all clauses are optional, and .InSequence()/.After()/ 5846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// .WillOnce() can appear any number of times. 59dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 60dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter#ifndef GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_ 61dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter#define GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_ 62dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 63dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter#include <map> 64dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter#include <set> 65dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter#include <sstream> 66dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter#include <string> 67dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter#include <vector> 68dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 6946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan#include "gmock/gmock-actions.h" 7046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan#include "gmock/gmock-cardinalities.h" 7146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan#include "gmock/gmock-matchers.h" 7246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan#include "gmock/internal/gmock-internal-utils.h" 7346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan#include "gmock/internal/gmock-port.h" 7446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan#include "gtest/gtest.h" 75dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 76dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixternamespace testing { 77dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 7846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// An abstract handle of an expectation. 7946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chanclass Expectation; 8046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 8146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// A set of expectation handles. 8246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chanclass ExpectationSet; 8346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 84dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// Anything inside the 'internal' namespace IS INTERNAL IMPLEMENTATION 85dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// and MUST NOT BE USED IN USER CODE!!! 86dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixternamespace internal { 87dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 8846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// Implements a mock function. 8946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chantemplate <typename F> class FunctionMocker; 90dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 91dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// Base class for expectations. 92dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixterclass ExpectationBase; 93dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 9446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// Implements an expectation. 9546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chantemplate <typename F> class TypedExpectation; 9646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 97dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// Helper class for testing the Expectation class template. 98dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixterclass ExpectationTester; 99dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 100dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// Base class for function mockers. 10146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chantemplate <typename F> class FunctionMockerBase; 102dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 103dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// Protects the mock object registry (in class Mock), all function 104dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// mockers, and all expectations. 105dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// 106dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// The reason we don't use more fine-grained protection is: when a 107dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// mock function Foo() is called, it needs to consult its expectations 108dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// to see which one should be picked. If another thread is allowed to 109dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// call a mock function (either Foo() or a different one) at the same 110dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// time, it could affect the "retired" attributes of Foo()'s 111dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// expectations when InSequence() is used, and thus affect which 112dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// expectation gets picked. Therefore, we sequence all mock function 113dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// calls to ensure the integrity of the mock objects' states. 11446108a219a4b812dd8f36fee479a0340ea5963f5Ben ChanGTEST_API_ GTEST_DECLARE_STATIC_MUTEX_(g_gmock_mutex); 11546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 11646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// Untyped base class for ActionResultHolder<R>. 11746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chanclass UntypedActionResultHolderBase; 118dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 119dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// Abstract base class of FunctionMockerBase. This is the 120dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// type-agnostic part of the function mocker interface. Its pure 121dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// virtual methods are implemented by FunctionMockerBase. 12246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chanclass GTEST_API_ UntypedFunctionMockerBase { 123dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter public: 12446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan UntypedFunctionMockerBase(); 12546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan virtual ~UntypedFunctionMockerBase(); 126dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 127dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Verifies that all expectations on this mock function have been 128dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // satisfied. Reports one or more Google Test non-fatal failures 129dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // and returns false if not. 13046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan bool VerifyAndClearExpectationsLocked() 13146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex); 132dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 133dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Clears the ON_CALL()s set on this mock function. 13446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan virtual void ClearDefaultActionsLocked() 13546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) = 0; 13646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 13746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // In all of the following Untyped* functions, it's the caller's 13846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // responsibility to guarantee the correctness of the arguments' 13946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // types. 14046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 14146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Performs the default action with the given arguments and returns 14246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // the action's result. The call description string will be used in 14346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // the error message to describe the call in the case the default 14446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // action fails. 14546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // L = * 14646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan virtual UntypedActionResultHolderBase* UntypedPerformDefaultAction( 14746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const void* untyped_args, 14846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const string& call_description) const = 0; 14946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 15046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Performs the given action with the given arguments and returns 15146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // the action's result. 15246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // L = * 15346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan virtual UntypedActionResultHolderBase* UntypedPerformAction( 15446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const void* untyped_action, 15546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const void* untyped_args) const = 0; 15646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 15746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Writes a message that the call is uninteresting (i.e. neither 15846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // explicitly expected nor explicitly unexpected) to the given 15946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // ostream. 16046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan virtual void UntypedDescribeUninterestingCall( 16146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const void* untyped_args, 16246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan ::std::ostream* os) const 16346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_LOCK_EXCLUDED_(g_gmock_mutex) = 0; 16446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 16546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Returns the expectation that matches the given function arguments 16646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // (or NULL is there's no match); when a match is found, 16746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // untyped_action is set to point to the action that should be 16846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // performed (or NULL if the action is "do default"), and 16946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // is_excessive is modified to indicate whether the call exceeds the 17046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // expected number. 17146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan virtual const ExpectationBase* UntypedFindMatchingExpectation( 17246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const void* untyped_args, 17346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const void** untyped_action, bool* is_excessive, 17446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan ::std::ostream* what, ::std::ostream* why) 17546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_LOCK_EXCLUDED_(g_gmock_mutex) = 0; 17646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 17746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Prints the given function arguments to the ostream. 17846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan virtual void UntypedPrintArgs(const void* untyped_args, 17946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan ::std::ostream* os) const = 0; 18046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 18146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Sets the mock object this mock method belongs to, and registers 18246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // this information in the global mock registry. Will be called 18346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // whenever an EXPECT_CALL() or ON_CALL() is executed on this mock 18446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // method. 18546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // TODO(wan@google.com): rename to SetAndRegisterOwner(). 18646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan void RegisterOwner(const void* mock_obj) 18746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_LOCK_EXCLUDED_(g_gmock_mutex); 18846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 18946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Sets the mock object this mock method belongs to, and sets the 19046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // name of the mock function. Will be called upon each invocation 19146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // of this mock function. 19246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan void SetOwnerAndName(const void* mock_obj, const char* name) 19346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_LOCK_EXCLUDED_(g_gmock_mutex); 19446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 19546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Returns the mock object this mock method belongs to. Must be 19646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // called after RegisterOwner() or SetOwnerAndName() has been 19746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // called. 19846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const void* MockObject() const 19946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_LOCK_EXCLUDED_(g_gmock_mutex); 20046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 20146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Returns the name of this mock method. Must be called after 20246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // SetOwnerAndName() has been called. 20346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const char* Name() const 20446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_LOCK_EXCLUDED_(g_gmock_mutex); 20546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 20646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Returns the result of invoking this mock function with the given 20746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // arguments. This function can be safely called from multiple 20846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // threads concurrently. The caller is responsible for deleting the 20946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // result. 21046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const UntypedActionResultHolderBase* UntypedInvokeWith( 21146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const void* untyped_args) 21246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_LOCK_EXCLUDED_(g_gmock_mutex); 21346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 21446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan protected: 21546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan typedef std::vector<const void*> UntypedOnCallSpecs; 21646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 21746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan typedef std::vector<internal::linked_ptr<ExpectationBase> > 21846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan UntypedExpectations; 21946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 22046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Returns an Expectation object that references and co-owns exp, 22146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // which must be an expectation on this mock function. 22246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan Expectation GetHandleOf(ExpectationBase* exp); 22346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 22446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Address of the mock object this mock method belongs to. Only 22546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // valid after this mock method has been called or 22646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // ON_CALL/EXPECT_CALL has been invoked on it. 22746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const void* mock_obj_; // Protected by g_gmock_mutex. 22846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 22946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Name of the function being mocked. Only valid after this mock 23046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // method has been called. 23146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const char* name_; // Protected by g_gmock_mutex. 23246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 23346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // All default action specs for this function mocker. 23446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan UntypedOnCallSpecs untyped_on_call_specs_; 23546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 23646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // All expectations for this function mocker. 23746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan UntypedExpectations untyped_expectations_; 238dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter}; // class UntypedFunctionMockerBase 239dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 24046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// Untyped base class for OnCallSpec<F>. 24146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chanclass UntypedOnCallSpecBase { 24246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan public: 24346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // The arguments are the location of the ON_CALL() statement. 24446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan UntypedOnCallSpecBase(const char* a_file, int a_line) 24546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan : file_(a_file), line_(a_line), last_clause_(kNone) {} 24646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 24746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Where in the source file was the default action spec defined? 24846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const char* file() const { return file_; } 24946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan int line() const { return line_; } 25046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 25146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan protected: 25246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Gives each clause in the ON_CALL() statement a name. 25346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan enum Clause { 25446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Do not change the order of the enum members! The run-time 25546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // syntax checking relies on it. 25646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan kNone, 25746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan kWith, 25846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan kWillByDefault 25946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan }; 26046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 26146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Asserts that the ON_CALL() statement has a certain property. 26246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan void AssertSpecProperty(bool property, const string& failure_message) const { 26346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan Assert(property, file_, line_, failure_message); 26446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan } 26546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 26646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Expects that the ON_CALL() statement has a certain property. 26746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan void ExpectSpecProperty(bool property, const string& failure_message) const { 26846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan Expect(property, file_, line_, failure_message); 26946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan } 27046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 27146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const char* file_; 27246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan int line_; 27346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 27446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // The last clause in the ON_CALL() statement as seen so far. 27546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Initially kNone and changes as the statement is parsed. 27646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan Clause last_clause_; 27746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan}; // class UntypedOnCallSpecBase 27846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 27946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// This template class implements an ON_CALL spec. 280dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixtertemplate <typename F> 28146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chanclass OnCallSpec : public UntypedOnCallSpecBase { 282dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter public: 283dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter typedef typename Function<F>::ArgumentTuple ArgumentTuple; 284dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter typedef typename Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple; 285dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 28646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Constructs an OnCallSpec object from the information inside 287dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // the parenthesis of an ON_CALL() statement. 28846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan OnCallSpec(const char* a_file, int a_line, 28946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const ArgumentMatcherTuple& matchers) 29046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan : UntypedOnCallSpecBase(a_file, a_line), 291dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter matchers_(matchers), 292dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // By default, extra_matcher_ should match anything. However, 293dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // we cannot initialize it with _ as that triggers a compiler 294dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // bug in Symbian's C++ compiler (cannot decide between two 295dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // overloaded constructors of Matcher<const ArgumentTuple&>). 29646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan extra_matcher_(A<const ArgumentTuple&>()) { 297dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 298dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 299dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Implements the .With() clause. 30046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan OnCallSpec& With(const Matcher<const ArgumentTuple&>& m) { 301dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Makes sure this is called at most once. 302dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter ExpectSpecProperty(last_clause_ < kWith, 303dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter ".With() cannot appear " 304dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter "more than once in an ON_CALL()."); 305dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter last_clause_ = kWith; 306dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 307dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter extra_matcher_ = m; 308dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter return *this; 309dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 310dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 311dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Implements the .WillByDefault() clause. 31246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan OnCallSpec& WillByDefault(const Action<F>& action) { 313dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter ExpectSpecProperty(last_clause_ < kWillByDefault, 314dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter ".WillByDefault() must appear " 315dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter "exactly once in an ON_CALL()."); 316dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter last_clause_ = kWillByDefault; 317dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 318dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter ExpectSpecProperty(!action.IsDoDefault(), 319dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter "DoDefault() cannot be used in ON_CALL()."); 320dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter action_ = action; 321dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter return *this; 322dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 323dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 324dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Returns true iff the given arguments match the matchers. 325dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter bool Matches(const ArgumentTuple& args) const { 326dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter return TupleMatches(matchers_, args) && extra_matcher_.Matches(args); 327dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 328dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 329dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Returns the action specified by the user. 330dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter const Action<F>& GetAction() const { 331dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter AssertSpecProperty(last_clause_ == kWillByDefault, 332dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter ".WillByDefault() must appear exactly " 333dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter "once in an ON_CALL()."); 334dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter return action_; 335dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 336dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 33746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan private: 338dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // The information in statement 339dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // 340dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // ON_CALL(mock_object, Method(matchers)) 341dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // .With(multi-argument-matcher) 342dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // .WillByDefault(action); 343dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // 344dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // is recorded in the data members like this: 345dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // 346dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // source file that contains the statement => file_ 347dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // line number of the statement => line_ 348dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // matchers => matchers_ 349dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // multi-argument-matcher => extra_matcher_ 350dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // action => action_ 351dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter ArgumentMatcherTuple matchers_; 352dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter Matcher<const ArgumentTuple&> extra_matcher_; 353dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter Action<F> action_; 35446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan}; // class OnCallSpec 355dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 356dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// Possible reactions on uninteresting calls. 357dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixterenum CallReaction { 35846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan kAllow, 35946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan kWarn, 36046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan kFail 361dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter}; 362dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 363dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter} // namespace internal 364dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 365dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// Utilities for manipulating mock objects. 36646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chanclass GTEST_API_ Mock { 367dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter public: 368dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // The following public methods can be called concurrently. 369dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 370dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Tells Google Mock to ignore mock_obj when checking for leaked 371dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // mock objects. 37246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan static void AllowLeak(const void* mock_obj) 37346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex); 374dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 375dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Verifies and clears all expectations on the given mock object. 376dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // If the expectations aren't satisfied, generates one or more 377dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Google Test non-fatal failures and returns false. 37846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan static bool VerifyAndClearExpectations(void* mock_obj) 37946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex); 380dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 381dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Verifies all expectations on the given mock object and clears its 382dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // default actions and expectations. Returns true iff the 383dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // verification was successful. 38446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan static bool VerifyAndClear(void* mock_obj) 38546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex); 38646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 387dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter private: 38846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan friend class internal::UntypedFunctionMockerBase; 38946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 390dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Needed for a function mocker to register itself (so that we know 391dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // how to clear a mock object). 392dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter template <typename F> 393dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter friend class internal::FunctionMockerBase; 394dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 395dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter template <typename M> 396dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter friend class NiceMock; 397dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 398dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter template <typename M> 399dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter friend class StrictMock; 400dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 401dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Tells Google Mock to allow uninteresting calls on the given mock 402dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // object. 40346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan static void AllowUninterestingCalls(const void* mock_obj) 40446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex); 405dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 406dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Tells Google Mock to warn the user about uninteresting calls on 407dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // the given mock object. 40846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan static void WarnUninterestingCalls(const void* mock_obj) 40946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex); 410dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 411dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Tells Google Mock to fail uninteresting calls on the given mock 412dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // object. 41346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan static void FailUninterestingCalls(const void* mock_obj) 41446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex); 415dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 416dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Tells Google Mock the given mock object is being destroyed and 417dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // its entry in the call-reaction table should be removed. 41846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan static void UnregisterCallReaction(const void* mock_obj) 41946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex); 420dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 421dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Returns the reaction Google Mock will have on uninteresting calls 422dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // made on the given mock object. 423dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter static internal::CallReaction GetReactionOnUninterestingCalls( 42446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const void* mock_obj) 42546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex); 426dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 427dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Verifies that all expectations on the given mock object have been 428dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // satisfied. Reports one or more Google Test non-fatal failures 429dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // and returns false if not. 43046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan static bool VerifyAndClearExpectationsLocked(void* mock_obj) 43146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex); 432dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 433dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Clears all ON_CALL()s set on the given mock object. 43446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan static void ClearDefaultActionsLocked(void* mock_obj) 43546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex); 436dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 437dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Registers a mock object and a mock method it owns. 43846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan static void Register( 43946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const void* mock_obj, 44046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan internal::UntypedFunctionMockerBase* mocker) 44146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex); 442dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 443dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Tells Google Mock where in the source code mock_obj is used in an 444dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // ON_CALL or EXPECT_CALL. In case mock_obj is leaked, this 445dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // information helps the user identify which object it is. 446dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter static void RegisterUseByOnCallOrExpectCall( 44746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const void* mock_obj, const char* file, int line) 44846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex); 449dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 450dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Unregisters a mock method; removes the owning mock object from 451dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // the registry when the last mock method associated with it has 452dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // been unregistered. This is called only in the destructor of 453dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // FunctionMockerBase. 45446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan static void UnregisterLocked(internal::UntypedFunctionMockerBase* mocker) 45546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex); 456dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter}; // class Mock 457dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 45846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// An abstract handle of an expectation. Useful in the .After() 45946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// clause of EXPECT_CALL() for setting the (partial) order of 46046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// expectations. The syntax: 46146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// 46246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// Expectation e1 = EXPECT_CALL(...)...; 46346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// EXPECT_CALL(...).After(e1)...; 46446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// 46546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// sets two expectations where the latter can only be matched after 46646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// the former has been satisfied. 46746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// 46846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// Notes: 46946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// - This class is copyable and has value semantics. 47046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// - Constness is shallow: a const Expectation object itself cannot 47146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// be modified, but the mutable methods of the ExpectationBase 47246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// object it references can be called via expectation_base(). 47346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// - The constructors and destructor are defined out-of-line because 47446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// the Symbian WINSCW compiler wants to otherwise instantiate them 47546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// when it sees this class definition, at which point it doesn't have 47646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// ExpectationBase available yet, leading to incorrect destruction 47746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// in the linked_ptr (or compilation errors if using a checking 47846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// linked_ptr). 47946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chanclass GTEST_API_ Expectation { 48046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan public: 48146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Constructs a null object that doesn't reference any expectation. 48246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan Expectation(); 48346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 48446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan ~Expectation(); 48546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 48646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // This single-argument ctor must not be explicit, in order to support the 48746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Expectation e = EXPECT_CALL(...); 48846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // syntax. 48946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // 49046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // A TypedExpectation object stores its pre-requisites as 49146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Expectation objects, and needs to call the non-const Retire() 49246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // method on the ExpectationBase objects they reference. Therefore 49346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Expectation must receive a *non-const* reference to the 49446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // ExpectationBase object. 49546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan Expectation(internal::ExpectationBase& exp); // NOLINT 49646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 49746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // The compiler-generated copy ctor and operator= work exactly as 49846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // intended, so we don't need to define our own. 49946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 50046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Returns true iff rhs references the same expectation as this object does. 50146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan bool operator==(const Expectation& rhs) const { 50246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan return expectation_base_ == rhs.expectation_base_; 50346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan } 50446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 50546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan bool operator!=(const Expectation& rhs) const { return !(*this == rhs); } 50646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 50746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan private: 50846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan friend class ExpectationSet; 50946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan friend class Sequence; 51046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan friend class ::testing::internal::ExpectationBase; 51146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan friend class ::testing::internal::UntypedFunctionMockerBase; 51246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 51346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan template <typename F> 51446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan friend class ::testing::internal::FunctionMockerBase; 51546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 51646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan template <typename F> 51746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan friend class ::testing::internal::TypedExpectation; 51846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 51946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // This comparator is needed for putting Expectation objects into a set. 52046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan class Less { 52146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan public: 52246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan bool operator()(const Expectation& lhs, const Expectation& rhs) const { 52346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan return lhs.expectation_base_.get() < rhs.expectation_base_.get(); 52446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan } 52546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan }; 52646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 52746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan typedef ::std::set<Expectation, Less> Set; 52846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 52946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan Expectation( 53046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const internal::linked_ptr<internal::ExpectationBase>& expectation_base); 53146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 53246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Returns the expectation this object references. 53346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const internal::linked_ptr<internal::ExpectationBase>& 53446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan expectation_base() const { 53546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan return expectation_base_; 53646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan } 53746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 53846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // A linked_ptr that co-owns the expectation this handle references. 53946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan internal::linked_ptr<internal::ExpectationBase> expectation_base_; 54046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan}; 54146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 54246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// A set of expectation handles. Useful in the .After() clause of 54346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// EXPECT_CALL() for setting the (partial) order of expectations. The 54446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// syntax: 54546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// 54646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// ExpectationSet es; 54746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// es += EXPECT_CALL(...)...; 54846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// es += EXPECT_CALL(...)...; 54946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// EXPECT_CALL(...).After(es)...; 55046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// 55146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// sets three expectations where the last one can only be matched 55246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// after the first two have both been satisfied. 55346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// 55446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// This class is copyable and has value semantics. 55546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chanclass ExpectationSet { 55646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan public: 55746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // A bidirectional iterator that can read a const element in the set. 55846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan typedef Expectation::Set::const_iterator const_iterator; 55946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 56046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // An object stored in the set. This is an alias of Expectation. 56146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan typedef Expectation::Set::value_type value_type; 56246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 56346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Constructs an empty set. 56446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan ExpectationSet() {} 56546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 56646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // This single-argument ctor must not be explicit, in order to support the 56746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // ExpectationSet es = EXPECT_CALL(...); 56846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // syntax. 56946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan ExpectationSet(internal::ExpectationBase& exp) { // NOLINT 57046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan *this += Expectation(exp); 57146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan } 57246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 57346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // This single-argument ctor implements implicit conversion from 57446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Expectation and thus must not be explicit. This allows either an 57546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Expectation or an ExpectationSet to be used in .After(). 57646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan ExpectationSet(const Expectation& e) { // NOLINT 57746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan *this += e; 57846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan } 57946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 58046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // The compiler-generator ctor and operator= works exactly as 58146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // intended, so we don't need to define our own. 58246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 58346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Returns true iff rhs contains the same set of Expectation objects 58446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // as this does. 58546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan bool operator==(const ExpectationSet& rhs) const { 58646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan return expectations_ == rhs.expectations_; 58746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan } 58846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 58946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan bool operator!=(const ExpectationSet& rhs) const { return !(*this == rhs); } 59046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 59146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Implements the syntax 59246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // expectation_set += EXPECT_CALL(...); 59346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan ExpectationSet& operator+=(const Expectation& e) { 59446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan expectations_.insert(e); 59546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan return *this; 59646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan } 59746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 59846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan int size() const { return static_cast<int>(expectations_.size()); } 59946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 60046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const_iterator begin() const { return expectations_.begin(); } 60146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const_iterator end() const { return expectations_.end(); } 60246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 60346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan private: 60446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan Expectation::Set expectations_; 60546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan}; 60646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 60746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 608dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// Sequence objects are used by a user to specify the relative order 609dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// in which the expectations should match. They are copyable (we rely 610dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// on the compiler-defined copy constructor and assignment operator). 61146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chanclass GTEST_API_ Sequence { 612dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter public: 613dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Constructs an empty sequence. 61446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan Sequence() : last_expectation_(new Expectation) {} 615dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 616dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Adds an expectation to this sequence. The caller must ensure 617dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // that no other thread is accessing this Sequence object. 61846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan void AddExpectation(const Expectation& expectation) const; 61946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 620dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter private: 62146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // The last expectation in this sequence. We use a linked_ptr here 62246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // because Sequence objects are copyable and we want the copies to 62346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // be aliases. The linked_ptr allows the copies to co-own and share 62446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // the same Expectation object. 62546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan internal::linked_ptr<Expectation> last_expectation_; 626dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter}; // class Sequence 627dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 628dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// An object of this type causes all EXPECT_CALL() statements 629dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// encountered in its scope to be put in an anonymous sequence. The 630dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// work is done in the constructor and destructor. You should only 631dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// create an InSequence object on the stack. 632dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// 633dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// The sole purpose for this class is to support easy definition of 634dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// sequential expectations, e.g. 635dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// 636dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// { 637dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// InSequence dummy; // The name of the object doesn't matter. 638dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// 639dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// // The following expectations must match in the order they appear. 640dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// EXPECT_CALL(a, Bar())...; 641dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// EXPECT_CALL(a, Baz())...; 642dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// ... 643dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// EXPECT_CALL(b, Xyz())...; 644dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// } 645dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// 646dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// You can create InSequence objects in multiple threads, as long as 647dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// they are used to affect different mock objects. The idea is that 648dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// each thread can create and set up its own mocks as if it's the only 649dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// thread. However, for clarity of your tests we recommend you to set 650dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// up mocks in the main thread unless you have a good reason not to do 651dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// so. 65246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chanclass GTEST_API_ InSequence { 653dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter public: 654dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter InSequence(); 655dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter ~InSequence(); 656dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter private: 657dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter bool sequence_created_; 658dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 659dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter GTEST_DISALLOW_COPY_AND_ASSIGN_(InSequence); // NOLINT 66046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan} GTEST_ATTRIBUTE_UNUSED_; 661dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 662dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixternamespace internal { 663dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 664dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// Points to the implicit sequence introduced by a living InSequence 665dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// object (if any) in the current thread or NULL. 66646108a219a4b812dd8f36fee479a0340ea5963f5Ben ChanGTEST_API_ extern ThreadLocal<Sequence*> g_gmock_implicit_sequence; 667dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 668dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// Base class for implementing expectations. 669dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// 670dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// There are two reasons for having a type-agnostic base class for 671dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// Expectation: 672dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// 673dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// 1. We need to store collections of expectations of different 674dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// types (e.g. all pre-requisites of a particular expectation, all 675dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// expectations in a sequence). Therefore these expectation objects 676dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// must share a common base class. 677dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// 678dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// 2. We can avoid binary code bloat by moving methods not depending 679dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// on the template argument of Expectation to the base class. 680dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// 681dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// This class is internal and mustn't be used by user code directly. 68246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chanclass GTEST_API_ ExpectationBase { 683dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter public: 68446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // source_text is the EXPECT_CALL(...) source that created this Expectation. 68546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan ExpectationBase(const char* file, int line, const string& source_text); 686dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 687dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter virtual ~ExpectationBase(); 688dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 689dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Where in the source file was the expectation spec defined? 690dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter const char* file() const { return file_; } 691dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter int line() const { return line_; } 69246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const char* source_text() const { return source_text_.c_str(); } 693dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Returns the cardinality specified in the expectation spec. 694dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter const Cardinality& cardinality() const { return cardinality_; } 695dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 696dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Describes the source file location of this expectation. 697dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter void DescribeLocationTo(::std::ostream* os) const { 69846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan *os << FormatFileLocation(file(), line()) << " "; 699dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 700dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 701dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Describes how many times a function call matching this 702dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // expectation has occurred. 70346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan void DescribeCallCountTo(::std::ostream* os) const 70446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex); 70546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 70646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // If this mock method has an extra matcher (i.e. .With(matcher)), 70746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // describes it to the ostream. 70846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan virtual void MaybeDescribeExtraMatcherTo(::std::ostream* os) = 0; 70946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 710dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter protected: 71146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan friend class ::testing::Expectation; 71246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan friend class UntypedFunctionMockerBase; 713dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 714dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter enum Clause { 715dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Don't change the order of the enum members! 716dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter kNone, 717dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter kWith, 718dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter kTimes, 719dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter kInSequence, 72046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan kAfter, 721dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter kWillOnce, 722dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter kWillRepeatedly, 72346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan kRetiresOnSaturation 724dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter }; 725dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 72646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan typedef std::vector<const void*> UntypedActions; 72746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 72846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Returns an Expectation object that references and co-owns this 72946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // expectation. 73046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan virtual Expectation GetHandle() = 0; 73146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 732dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Asserts that the EXPECT_CALL() statement has the given property. 733dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter void AssertSpecProperty(bool property, const string& failure_message) const { 734dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter Assert(property, file_, line_, failure_message); 735dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 736dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 737dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Expects that the EXPECT_CALL() statement has the given property. 738dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter void ExpectSpecProperty(bool property, const string& failure_message) const { 739dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter Expect(property, file_, line_, failure_message); 740dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 741dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 742dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Explicitly specifies the cardinality of this expectation. Used 743dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // by the subclasses to implement the .Times() clause. 744dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter void SpecifyCardinality(const Cardinality& cardinality); 745dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 746dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Returns true iff the user specified the cardinality explicitly 747dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // using a .Times(). 748dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter bool cardinality_specified() const { return cardinality_specified_; } 749dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 750dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Sets the cardinality of this expectation spec. 75146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan void set_cardinality(const Cardinality& a_cardinality) { 75246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan cardinality_ = a_cardinality; 753dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 754dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 755dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // The following group of methods should only be called after the 756dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // EXPECT_CALL() statement, and only when g_gmock_mutex is held by 757dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // the current thread. 758dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 759dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Retires all pre-requisites of this expectation. 76046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan void RetireAllPreRequisites() 76146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex); 762dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 763dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Returns true iff this expectation is retired. 76446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan bool is_retired() const 76546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { 766dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter g_gmock_mutex.AssertHeld(); 767dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter return retired_; 768dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 769dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 770dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Retires this expectation. 77146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan void Retire() 77246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { 773dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter g_gmock_mutex.AssertHeld(); 774dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter retired_ = true; 775dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 776dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 777dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Returns true iff this expectation is satisfied. 77846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan bool IsSatisfied() const 77946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { 780dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter g_gmock_mutex.AssertHeld(); 781dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter return cardinality().IsSatisfiedByCallCount(call_count_); 782dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 783dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 784dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Returns true iff this expectation is saturated. 78546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan bool IsSaturated() const 78646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { 787dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter g_gmock_mutex.AssertHeld(); 788dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter return cardinality().IsSaturatedByCallCount(call_count_); 789dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 790dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 791dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Returns true iff this expectation is over-saturated. 79246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan bool IsOverSaturated() const 79346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { 794dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter g_gmock_mutex.AssertHeld(); 795dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter return cardinality().IsOverSaturatedByCallCount(call_count_); 796dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 797dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 798dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Returns true iff all pre-requisites of this expectation are satisfied. 79946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan bool AllPrerequisitesAreSatisfied() const 80046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex); 801dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 802dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Adds unsatisfied pre-requisites of this expectation to 'result'. 80346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan void FindUnsatisfiedPrerequisites(ExpectationSet* result) const 80446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex); 805dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 806dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Returns the number this expectation has been invoked. 80746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan int call_count() const 80846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { 809dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter g_gmock_mutex.AssertHeld(); 810dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter return call_count_; 811dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 812dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 813dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Increments the number this expectation has been invoked. 81446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan void IncrementCallCount() 81546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { 816dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter g_gmock_mutex.AssertHeld(); 817dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter call_count_++; 818dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 819dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 82046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Checks the action count (i.e. the number of WillOnce() and 82146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // WillRepeatedly() clauses) against the cardinality if this hasn't 82246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // been done before. Prints a warning if there are too many or too 82346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // few actions. 82446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan void CheckActionCountIfNotDone() const 82546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_LOCK_EXCLUDED_(mutex_); 82646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 827dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter friend class ::testing::Sequence; 828dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter friend class ::testing::internal::ExpectationTester; 829dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 830dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter template <typename Function> 83146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan friend class TypedExpectation; 83246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 83346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Implements the .Times() clause. 83446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan void UntypedTimes(const Cardinality& a_cardinality); 835dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 836dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // This group of fields are part of the spec and won't change after 837dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // an EXPECT_CALL() statement finishes. 83846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const char* file_; // The file that contains the expectation. 83946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan int line_; // The line number of the expectation. 84046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const string source_text_; // The EXPECT_CALL(...) source text. 841dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // True iff the cardinality is specified explicitly. 842dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter bool cardinality_specified_; 843dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter Cardinality cardinality_; // The cardinality of the expectation. 84446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // The immediate pre-requisites (i.e. expectations that must be 84546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // satisfied before this expectation can be matched) of this 84646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // expectation. We use linked_ptr in the set because we want an 84746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Expectation object to be co-owned by its FunctionMocker and its 84846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // successors. This allows multiple mock objects to be deleted at 84946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // different times. 85046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan ExpectationSet immediate_prerequisites_; 851dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 852dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // This group of fields are the current state of the expectation, 853dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // and can change as the mock function is called. 854dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter int call_count_; // How many times this expectation has been invoked. 855dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter bool retired_; // True iff this expectation has retired. 85646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan UntypedActions untyped_actions_; 85746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan bool extra_matcher_specified_; 85846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan bool repeated_action_specified_; // True if a WillRepeatedly() was specified. 85946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan bool retires_on_saturation_; 86046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan Clause last_clause_; 86146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan mutable bool action_count_checked_; // Under mutex_. 86246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan mutable Mutex mutex_; // Protects action_count_checked_. 86346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 86446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_DISALLOW_ASSIGN_(ExpectationBase); 865dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter}; // class ExpectationBase 866dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 867dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// Impements an expectation for the given function type. 868dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixtertemplate <typename F> 86946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chanclass TypedExpectation : public ExpectationBase { 870dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter public: 871dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter typedef typename Function<F>::ArgumentTuple ArgumentTuple; 872dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter typedef typename Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple; 873dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter typedef typename Function<F>::Result Result; 874dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 87546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan TypedExpectation(FunctionMockerBase<F>* owner, 87646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const char* a_file, int a_line, const string& a_source_text, 87746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const ArgumentMatcherTuple& m) 87846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan : ExpectationBase(a_file, a_line, a_source_text), 879dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter owner_(owner), 880dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter matchers_(m), 881dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // By default, extra_matcher_ should match anything. However, 882dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // we cannot initialize it with _ as that triggers a compiler 883dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // bug in Symbian's C++ compiler (cannot decide between two 884dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // overloaded constructors of Matcher<const ArgumentTuple&>). 885dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter extra_matcher_(A<const ArgumentTuple&>()), 88646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan repeated_action_(DoDefault()) {} 887dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 88846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan virtual ~TypedExpectation() { 889dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Check the validity of the action count if it hasn't been done 890dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // yet (for example, if the expectation was never used). 891dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter CheckActionCountIfNotDone(); 89246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan for (UntypedActions::const_iterator it = untyped_actions_.begin(); 89346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan it != untyped_actions_.end(); ++it) { 89446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan delete static_cast<const Action<F>*>(*it); 89546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan } 896dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 897dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 898dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Implements the .With() clause. 89946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan TypedExpectation& With(const Matcher<const ArgumentTuple&>& m) { 900dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter if (last_clause_ == kWith) { 901dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter ExpectSpecProperty(false, 902dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter ".With() cannot appear " 903dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter "more than once in an EXPECT_CALL()."); 904dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } else { 905dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter ExpectSpecProperty(last_clause_ < kWith, 906dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter ".With() must be the first " 907dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter "clause in an EXPECT_CALL()."); 908dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 909dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter last_clause_ = kWith; 910dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 911dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter extra_matcher_ = m; 91246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan extra_matcher_specified_ = true; 913dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter return *this; 914dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 915dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 916dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Implements the .Times() clause. 91746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan TypedExpectation& Times(const Cardinality& a_cardinality) { 91846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan ExpectationBase::UntypedTimes(a_cardinality); 919dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter return *this; 920dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 921dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 922dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Implements the .Times() clause. 92346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan TypedExpectation& Times(int n) { 924dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter return Times(Exactly(n)); 925dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 926dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 927dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Implements the .InSequence() clause. 92846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan TypedExpectation& InSequence(const Sequence& s) { 929dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter ExpectSpecProperty(last_clause_ <= kInSequence, 93046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan ".InSequence() cannot appear after .After()," 93146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan " .WillOnce(), .WillRepeatedly(), or " 932dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter ".RetiresOnSaturation()."); 933dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter last_clause_ = kInSequence; 934dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 93546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan s.AddExpectation(GetHandle()); 936dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter return *this; 937dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 93846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2) { 939dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter return InSequence(s1).InSequence(s2); 940dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 94146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2, 94246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const Sequence& s3) { 943dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter return InSequence(s1, s2).InSequence(s3); 944dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 94546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2, 94646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const Sequence& s3, const Sequence& s4) { 947dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter return InSequence(s1, s2, s3).InSequence(s4); 948dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 94946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2, 95046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const Sequence& s3, const Sequence& s4, 95146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const Sequence& s5) { 952dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter return InSequence(s1, s2, s3, s4).InSequence(s5); 953dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 954dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 95546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Implements that .After() clause. 95646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan TypedExpectation& After(const ExpectationSet& s) { 95746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan ExpectSpecProperty(last_clause_ <= kAfter, 95846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan ".After() cannot appear after .WillOnce()," 95946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan " .WillRepeatedly(), or " 96046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan ".RetiresOnSaturation()."); 96146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan last_clause_ = kAfter; 96246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 96346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan for (ExpectationSet::const_iterator it = s.begin(); it != s.end(); ++it) { 96446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan immediate_prerequisites_ += *it; 96546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan } 96646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan return *this; 96746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan } 96846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2) { 96946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan return After(s1).After(s2); 97046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan } 97146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2, 97246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const ExpectationSet& s3) { 97346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan return After(s1, s2).After(s3); 97446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan } 97546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2, 97646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const ExpectationSet& s3, const ExpectationSet& s4) { 97746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan return After(s1, s2, s3).After(s4); 97846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan } 97946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2, 98046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const ExpectationSet& s3, const ExpectationSet& s4, 98146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const ExpectationSet& s5) { 98246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan return After(s1, s2, s3, s4).After(s5); 98346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan } 98446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 985dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Implements the .WillOnce() clause. 98646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan TypedExpectation& WillOnce(const Action<F>& action) { 987dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter ExpectSpecProperty(last_clause_ <= kWillOnce, 988dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter ".WillOnce() cannot appear after " 989dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter ".WillRepeatedly() or .RetiresOnSaturation()."); 990dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter last_clause_ = kWillOnce; 991dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 99246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan untyped_actions_.push_back(new Action<F>(action)); 993dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter if (!cardinality_specified()) { 99446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan set_cardinality(Exactly(static_cast<int>(untyped_actions_.size()))); 995dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 996dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter return *this; 997dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 998dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 999dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Implements the .WillRepeatedly() clause. 100046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan TypedExpectation& WillRepeatedly(const Action<F>& action) { 1001dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter if (last_clause_ == kWillRepeatedly) { 1002dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter ExpectSpecProperty(false, 1003dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter ".WillRepeatedly() cannot appear " 1004dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter "more than once in an EXPECT_CALL()."); 1005dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } else { 1006dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter ExpectSpecProperty(last_clause_ < kWillRepeatedly, 1007dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter ".WillRepeatedly() cannot appear " 1008dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter "after .RetiresOnSaturation()."); 1009dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1010dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter last_clause_ = kWillRepeatedly; 1011dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter repeated_action_specified_ = true; 1012dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1013dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter repeated_action_ = action; 1014dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter if (!cardinality_specified()) { 101546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan set_cardinality(AtLeast(static_cast<int>(untyped_actions_.size()))); 1016dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1017dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1018dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Now that no more action clauses can be specified, we check 1019dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // whether their count makes sense. 1020dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter CheckActionCountIfNotDone(); 1021dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter return *this; 1022dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1023dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1024dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Implements the .RetiresOnSaturation() clause. 102546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan TypedExpectation& RetiresOnSaturation() { 1026dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter ExpectSpecProperty(last_clause_ < kRetiresOnSaturation, 1027dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter ".RetiresOnSaturation() cannot appear " 1028dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter "more than once."); 1029dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter last_clause_ = kRetiresOnSaturation; 1030dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter retires_on_saturation_ = true; 1031dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1032dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Now that no more action clauses can be specified, we check 1033dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // whether their count makes sense. 1034dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter CheckActionCountIfNotDone(); 1035dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter return *this; 1036dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1037dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1038dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Returns the matchers for the arguments as specified inside the 1039dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // EXPECT_CALL() macro. 1040dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter const ArgumentMatcherTuple& matchers() const { 1041dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter return matchers_; 1042dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1043dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1044dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Returns the matcher specified by the .With() clause. 1045dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter const Matcher<const ArgumentTuple&>& extra_matcher() const { 1046dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter return extra_matcher_; 1047dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1048dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1049dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Returns the action specified by the .WillRepeatedly() clause. 1050dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter const Action<F>& repeated_action() const { return repeated_action_; } 1051dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 105246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // If this mock method has an extra matcher (i.e. .With(matcher)), 105346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // describes it to the ostream. 105446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan virtual void MaybeDescribeExtraMatcherTo(::std::ostream* os) { 105546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan if (extra_matcher_specified_) { 105646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan *os << " Expected args: "; 105746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan extra_matcher_.DescribeTo(os); 105846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan *os << "\n"; 105946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan } 1060dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 106146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 1062dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter private: 1063dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter template <typename Function> 1064dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter friend class FunctionMockerBase; 1065dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 106646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Returns an Expectation object that references and co-owns this 106746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // expectation. 106846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan virtual Expectation GetHandle() { 106946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan return owner_->GetHandleOf(this); 107046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan } 107146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 1072dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // The following methods will be called only after the EXPECT_CALL() 1073dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // statement finishes and when the current thread holds 1074dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // g_gmock_mutex. 1075dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1076dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Returns true iff this expectation matches the given arguments. 107746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan bool Matches(const ArgumentTuple& args) const 107846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { 1079dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter g_gmock_mutex.AssertHeld(); 1080dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter return TupleMatches(matchers_, args) && extra_matcher_.Matches(args); 1081dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1082dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1083dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Returns true iff this expectation should handle the given arguments. 108446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan bool ShouldHandleArguments(const ArgumentTuple& args) const 108546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { 1086dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter g_gmock_mutex.AssertHeld(); 1087dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1088dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // In case the action count wasn't checked when the expectation 1089dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // was defined (e.g. if this expectation has no WillRepeatedly() 1090dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // or RetiresOnSaturation() clause), we check it when the 1091dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // expectation is used for the first time. 1092dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter CheckActionCountIfNotDone(); 1093dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter return !is_retired() && AllPrerequisitesAreSatisfied() && Matches(args); 1094dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1095dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1096dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Describes the result of matching the arguments against this 1097dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // expectation to the given ostream. 109846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan void ExplainMatchResultTo( 109946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const ArgumentTuple& args, 110046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan ::std::ostream* os) const 110146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { 1102dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter g_gmock_mutex.AssertHeld(); 1103dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1104dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter if (is_retired()) { 1105dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter *os << " Expected: the expectation is active\n" 1106dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter << " Actual: it is retired\n"; 1107dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } else if (!Matches(args)) { 1108dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter if (!TupleMatches(matchers_, args)) { 110946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan ExplainMatchFailureTupleTo(matchers_, args, os); 1110dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 111146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan StringMatchResultListener listener; 111246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan if (!extra_matcher_.MatchAndExplain(args, &listener)) { 1113dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter *os << " Expected args: "; 1114dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter extra_matcher_.DescribeTo(os); 1115dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter *os << "\n Actual: don't match"; 1116dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 111746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan internal::PrintIfNotEmpty(listener.str(), os); 1118dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter *os << "\n"; 1119dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1120dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } else if (!AllPrerequisitesAreSatisfied()) { 1121dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter *os << " Expected: all pre-requisites are satisfied\n" 1122dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter << " Actual: the following immediate pre-requisites " 1123dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter << "are not satisfied:\n"; 112446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan ExpectationSet unsatisfied_prereqs; 1125dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter FindUnsatisfiedPrerequisites(&unsatisfied_prereqs); 1126dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter int i = 0; 112746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan for (ExpectationSet::const_iterator it = unsatisfied_prereqs.begin(); 1128dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter it != unsatisfied_prereqs.end(); ++it) { 112946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan it->expectation_base()->DescribeLocationTo(os); 1130dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter *os << "pre-requisite #" << i++ << "\n"; 1131dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1132dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter *os << " (end of pre-requisites)\n"; 1133dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } else { 1134dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // This line is here just for completeness' sake. It will never 113546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // be executed as currently the ExplainMatchResultTo() function 1136dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // is called only when the mock function call does NOT match the 1137dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // expectation. 1138dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter *os << "The call matches the expectation.\n"; 1139dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1140dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1141dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1142dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Returns the action that should be taken for the current invocation. 114346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const Action<F>& GetCurrentAction( 114446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const FunctionMockerBase<F>* mocker, 114546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const ArgumentTuple& args) const 114646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { 1147dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter g_gmock_mutex.AssertHeld(); 1148dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter const int count = call_count(); 1149dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter Assert(count >= 1, __FILE__, __LINE__, 1150dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter "call_count() is <= 0 when GetCurrentAction() is " 1151dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter "called - this should never happen."); 1152dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 115346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const int action_count = static_cast<int>(untyped_actions_.size()); 1154dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter if (action_count > 0 && !repeated_action_specified_ && 1155dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter count > action_count) { 1156dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // If there is at least one WillOnce() and no WillRepeatedly(), 1157dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // we warn the user when the WillOnce() clauses ran out. 1158dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter ::std::stringstream ss; 1159dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter DescribeLocationTo(&ss); 116046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan ss << "Actions ran out in " << source_text() << "...\n" 1161dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter << "Called " << count << " times, but only " 1162dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter << action_count << " WillOnce()" 1163dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter << (action_count == 1 ? " is" : "s are") << " specified - "; 1164dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter mocker->DescribeDefaultActionTo(args, &ss); 116546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan Log(kWarning, ss.str(), 1); 1166dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1167dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 116846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan return count <= action_count ? 116946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan *static_cast<const Action<F>*>(untyped_actions_[count - 1]) : 117046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan repeated_action(); 1171dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1172dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1173dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Given the arguments of a mock function call, if the call will 1174dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // over-saturate this expectation, returns the default action; 1175dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // otherwise, returns the next action in this expectation. Also 1176dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // describes *what* happened to 'what', and explains *why* Google 1177dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Mock does it to 'why'. This method is not const as it calls 117846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // IncrementCallCount(). A return value of NULL means the default 117946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // action. 118046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const Action<F>* GetActionForArguments( 118146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const FunctionMockerBase<F>* mocker, 118246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const ArgumentTuple& args, 118346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan ::std::ostream* what, 118446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan ::std::ostream* why) 118546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { 1186dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter g_gmock_mutex.AssertHeld(); 1187dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter if (IsSaturated()) { 1188dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // We have an excessive call. 1189dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter IncrementCallCount(); 1190dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter *what << "Mock function called more times than expected - "; 1191dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter mocker->DescribeDefaultActionTo(args, what); 1192dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter DescribeCallCountTo(why); 1193dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 119446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // TODO(wan@google.com): allow the user to control whether 119546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // unexpected calls should fail immediately or continue using a 119646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // flag --gmock_unexpected_calls_are_fatal. 119746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan return NULL; 1198dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1199dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1200dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter IncrementCallCount(); 1201dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter RetireAllPreRequisites(); 1202dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 120346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan if (retires_on_saturation_ && IsSaturated()) { 1204dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter Retire(); 1205dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1206dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1207dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Must be done after IncrementCount()! 120846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan *what << "Mock function call matches " << source_text() <<"...\n"; 120946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan return &(GetCurrentAction(mocker, args)); 1210dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1211dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1212dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // All the fields below won't change once the EXPECT_CALL() 1213dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // statement finishes. 1214dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter FunctionMockerBase<F>* const owner_; 1215dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter ArgumentMatcherTuple matchers_; 1216dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter Matcher<const ArgumentTuple&> extra_matcher_; 1217dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter Action<F> repeated_action_; 121846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 121946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_DISALLOW_COPY_AND_ASSIGN_(TypedExpectation); 122046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan}; // class TypedExpectation 1221dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1222dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// A MockSpec object is used by ON_CALL() or EXPECT_CALL() for 1223dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// specifying the default behavior of, or expectation on, a mock 1224dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// function. 1225dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1226dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// Note: class MockSpec really belongs to the ::testing namespace. 1227dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// However if we define it in ::testing, MSVC will complain when 1228dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// classes in ::testing::internal declare it as a friend class 1229dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// template. To workaround this compiler bug, we define MockSpec in 1230dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// ::testing::internal and import it into ::testing. 1231dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 123246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// Logs a message including file and line number information. 123346108a219a4b812dd8f36fee479a0340ea5963f5Ben ChanGTEST_API_ void LogWithLocation(testing::internal::LogSeverity severity, 123446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const char* file, int line, 123546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const string& message); 123646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 1237dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixtertemplate <typename F> 1238dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixterclass MockSpec { 1239dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter public: 1240dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple; 1241dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter typedef typename internal::Function<F>::ArgumentMatcherTuple 1242dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter ArgumentMatcherTuple; 1243dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1244dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Constructs a MockSpec object, given the function mocker object 1245dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // that the spec is associated with. 1246dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter explicit MockSpec(internal::FunctionMockerBase<F>* function_mocker) 1247dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter : function_mocker_(function_mocker) {} 1248dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1249dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Adds a new default action spec to the function mocker and returns 1250dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // the newly created spec. 125146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan internal::OnCallSpec<F>& InternalDefaultActionSetAt( 1252dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter const char* file, int line, const char* obj, const char* call) { 125346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan LogWithLocation(internal::kInfo, file, line, 1254dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter string("ON_CALL(") + obj + ", " + call + ") invoked"); 125546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan return function_mocker_->AddNewOnCallSpec(file, line, matchers_); 1256dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1257dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1258dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Adds a new expectation spec to the function mocker and returns 1259dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // the newly created spec. 126046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan internal::TypedExpectation<F>& InternalExpectedAt( 1261dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter const char* file, int line, const char* obj, const char* call) { 126246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const string source_text(string("EXPECT_CALL(") + obj + ", " + call + ")"); 126346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan LogWithLocation(internal::kInfo, file, line, source_text + " invoked"); 126446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan return function_mocker_->AddNewExpectation( 126546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan file, line, source_text, matchers_); 1266dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1267dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1268dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter private: 1269dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter template <typename Function> 1270dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter friend class internal::FunctionMocker; 1271dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1272dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter void SetMatchers(const ArgumentMatcherTuple& matchers) { 1273dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter matchers_ = matchers; 1274dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1275dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1276dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // The function mocker that owns this spec. 1277dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter internal::FunctionMockerBase<F>* const function_mocker_; 1278dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // The argument matchers specified in the spec. 1279dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter ArgumentMatcherTuple matchers_; 128046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 128146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_DISALLOW_ASSIGN_(MockSpec); 1282dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter}; // class MockSpec 1283dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1284dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// MSVC warns about using 'this' in base member initializer list, so 1285dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// we need to temporarily disable the warning. We have to do it for 1286dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// the entire class to suppress the warning, even though it's about 1287dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// the constructor only. 1288dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1289dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter#ifdef _MSC_VER 129046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan# pragma warning(push) // Saves the current warning state. 129146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan# pragma warning(disable:4355) // Temporarily disables warning 4355. 1292dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter#endif // _MSV_VER 1293dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1294dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// C++ treats the void type specially. For example, you cannot define 1295dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// a void-typed variable or pass a void value to a function. 1296dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// ActionResultHolder<T> holds a value of type T, where T must be a 1297dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// copyable type or void (T doesn't need to be default-constructable). 1298dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// It hides the syntactic difference between void and other types, and 1299dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// is used to unify the code for invoking both void-returning and 130046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// non-void-returning mock functions. 130146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 130246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// Untyped base class for ActionResultHolder<T>. 130346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chanclass UntypedActionResultHolderBase { 130446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan public: 130546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan virtual ~UntypedActionResultHolderBase() {} 130646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 130746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Prints the held value as an action's result to os. 130846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan virtual void PrintAsActionResult(::std::ostream* os) const = 0; 130946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan}; 131046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 131146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// This generic definition is used when T is not void. 1312dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixtertemplate <typename T> 131346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chanclass ActionResultHolder : public UntypedActionResultHolderBase { 1314dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter public: 131546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan explicit ActionResultHolder(T a_value) : value_(a_value) {} 1316dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1317dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // The compiler-generated copy constructor and assignment operator 1318dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // are exactly what we need, so we don't need to define them. 1319dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 132046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Returns the held value and deletes this object. 132146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan T GetValueAndDelete() const { 132246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan T retval(value_); 132346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan delete this; 132446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan return retval; 132546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan } 1326dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1327dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Prints the held value as an action's result to os. 132846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan virtual void PrintAsActionResult(::std::ostream* os) const { 1329dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter *os << "\n Returns: "; 133046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // T may be a reference type, so we don't use UniversalPrint(). 1331dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter UniversalPrinter<T>::Print(value_, os); 1332dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1333dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1334dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Performs the given mock function's default action and returns the 133546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // result in a new-ed ActionResultHolder. 133646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan template <typename F> 133746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan static ActionResultHolder* PerformDefaultAction( 133846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const FunctionMockerBase<F>* func_mocker, 133946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const typename Function<F>::ArgumentTuple& args, 1340dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter const string& call_description) { 134146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan return new ActionResultHolder( 1342dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter func_mocker->PerformDefaultAction(args, call_description)); 1343dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1344dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 134546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Performs the given action and returns the result in a new-ed 1346dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // ActionResultHolder. 134746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan template <typename F> 134846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan static ActionResultHolder* 134946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan PerformAction(const Action<F>& action, 135046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const typename Function<F>::ArgumentTuple& args) { 135146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan return new ActionResultHolder(action.Perform(args)); 1352dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1353dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1354dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter private: 1355dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter T value_; 135646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 135746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // T could be a reference type, so = isn't supported. 135846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_DISALLOW_ASSIGN_(ActionResultHolder); 1359dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter}; 1360dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1361dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// Specialization for T = void. 1362dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixtertemplate <> 136346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chanclass ActionResultHolder<void> : public UntypedActionResultHolderBase { 1364dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter public: 136546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan void GetValueAndDelete() const { delete this; } 136646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 136746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan virtual void PrintAsActionResult(::std::ostream* /* os */) const {} 136846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 136946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Performs the given mock function's default action and returns NULL; 137046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan template <typename F> 137146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan static ActionResultHolder* PerformDefaultAction( 137246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const FunctionMockerBase<F>* func_mocker, 137346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const typename Function<F>::ArgumentTuple& args, 1374dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter const string& call_description) { 1375dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter func_mocker->PerformDefaultAction(args, call_description); 137646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan return NULL; 1377dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1378dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 137946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Performs the given action and returns NULL. 138046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan template <typename F> 138146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan static ActionResultHolder* PerformAction( 138246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const Action<F>& action, 138346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const typename Function<F>::ArgumentTuple& args) { 1384dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter action.Perform(args); 138546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan return NULL; 1386dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1387dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter}; 1388dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1389dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// The base of the function mocker class for the given function type. 1390dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// We put the methods in this class instead of its child to avoid code 1391dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// bloat. 1392dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixtertemplate <typename F> 1393dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixterclass FunctionMockerBase : public UntypedFunctionMockerBase { 1394dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter public: 1395dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter typedef typename Function<F>::Result Result; 1396dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter typedef typename Function<F>::ArgumentTuple ArgumentTuple; 1397dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter typedef typename Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple; 1398dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 139946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan FunctionMockerBase() : current_spec_(this) {} 1400dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1401dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // The destructor verifies that all expectations on this mock 1402dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // function have been satisfied. If not, it will report Google Test 1403dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // non-fatal failures for the violations. 140446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan virtual ~FunctionMockerBase() 140546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { 1406dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter MutexLock l(&g_gmock_mutex); 1407dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter VerifyAndClearExpectationsLocked(); 1408dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter Mock::UnregisterLocked(this); 140946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan ClearDefaultActionsLocked(); 1410dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1411dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1412dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Returns the ON_CALL spec that matches this mock function with the 1413dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // given arguments; returns NULL if no matching ON_CALL is found. 1414dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // L = * 141546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const OnCallSpec<F>* FindOnCallSpec( 1416dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter const ArgumentTuple& args) const { 141746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan for (UntypedOnCallSpecs::const_reverse_iterator it 141846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan = untyped_on_call_specs_.rbegin(); 141946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan it != untyped_on_call_specs_.rend(); ++it) { 142046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const OnCallSpec<F>* spec = static_cast<const OnCallSpec<F>*>(*it); 142146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan if (spec->Matches(args)) 142246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan return spec; 1423dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1424dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1425dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter return NULL; 1426dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1427dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1428dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Performs the default action of this mock function on the given arguments 1429dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // and returns the result. Asserts with a helpful call descrption if there is 1430dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // no valid return value. This method doesn't depend on the mutable state of 1431dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // this object, and thus can be called concurrently without locking. 1432dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // L = * 1433dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter Result PerformDefaultAction(const ArgumentTuple& args, 1434dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter const string& call_description) const { 143546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const OnCallSpec<F>* const spec = 143646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan this->FindOnCallSpec(args); 1437dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter if (spec != NULL) { 1438dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter return spec->GetAction().Perform(args); 1439dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1440dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter Assert(DefaultValue<Result>::Exists(), "", -1, 1441dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter call_description + "\n The mock function has no default action " 1442dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter "set, and its return type has no default value set."); 1443dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter return DefaultValue<Result>::Get(); 1444dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1445dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 144646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Performs the default action with the given arguments and returns 144746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // the action's result. The call description string will be used in 144846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // the error message to describe the call in the case the default 144946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // action fails. The caller is responsible for deleting the result. 145046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // L = * 145146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan virtual UntypedActionResultHolderBase* UntypedPerformDefaultAction( 145246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const void* untyped_args, // must point to an ArgumentTuple 145346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const string& call_description) const { 145446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const ArgumentTuple& args = 145546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan *static_cast<const ArgumentTuple*>(untyped_args); 145646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan return ResultHolder::PerformDefaultAction(this, args, call_description); 1457dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1458dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 145946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Performs the given action with the given arguments and returns 146046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // the action's result. The caller is responsible for deleting the 146146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // result. 146246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // L = * 146346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan virtual UntypedActionResultHolderBase* UntypedPerformAction( 146446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const void* untyped_action, const void* untyped_args) const { 146546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Make a copy of the action before performing it, in case the 146646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // action deletes the mock object (and thus deletes itself). 146746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const Action<F> action = *static_cast<const Action<F>*>(untyped_action); 146846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const ArgumentTuple& args = 146946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan *static_cast<const ArgumentTuple*>(untyped_args); 147046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan return ResultHolder::PerformAction(action, args); 147146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan } 147246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 147346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Implements UntypedFunctionMockerBase::ClearDefaultActionsLocked(): 147446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // clears the ON_CALL()s set on this mock function. 147546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan virtual void ClearDefaultActionsLocked() 147646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { 1477dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter g_gmock_mutex.AssertHeld(); 1478dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 147946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Deleting our default actions may trigger other mock objects to be 148046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // deleted, for example if an action contains a reference counted smart 148146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // pointer to that mock object, and that is the last reference. So if we 148246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // delete our actions within the context of the global mutex we may deadlock 148346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // when this method is called again. Instead, make a copy of the set of 148446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // actions to delete, clear our set within the mutex, and then delete the 148546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // actions outside of the mutex. 148646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan UntypedOnCallSpecs specs_to_delete; 148746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan untyped_on_call_specs_.swap(specs_to_delete); 148846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 148946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan g_gmock_mutex.Unlock(); 149046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan for (UntypedOnCallSpecs::const_iterator it = 149146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan specs_to_delete.begin(); 149246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan it != specs_to_delete.end(); ++it) { 149346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan delete static_cast<const OnCallSpec<F>*>(*it); 1494dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 149546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 149646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Lock the mutex again, since the caller expects it to be locked when we 149746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // return. 149846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan g_gmock_mutex.Lock(); 1499dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 150046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 1501dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter protected: 1502dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter template <typename Function> 1503dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter friend class MockSpec; 1504dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 150546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan typedef ActionResultHolder<Result> ResultHolder; 150646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 1507dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Returns the result of invoking this mock function with the given 1508dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // arguments. This function can be safely called from multiple 1509dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // threads concurrently. 151046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan Result InvokeWith(const ArgumentTuple& args) 151146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { 151246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan return static_cast<const ResultHolder*>( 151346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan this->UntypedInvokeWith(&args))->GetValueAndDelete(); 151446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan } 1515dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1516dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Adds and returns a default action spec for this mock function. 151746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan OnCallSpec<F>& AddNewOnCallSpec( 1518dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter const char* file, int line, 151946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const ArgumentMatcherTuple& m) 152046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { 1521dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter Mock::RegisterUseByOnCallOrExpectCall(MockObject(), file, line); 152246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan OnCallSpec<F>* const on_call_spec = new OnCallSpec<F>(file, line, m); 152346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan untyped_on_call_specs_.push_back(on_call_spec); 152446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan return *on_call_spec; 1525dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1526dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1527dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Adds and returns an expectation spec for this mock function. 152846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan TypedExpectation<F>& AddNewExpectation( 152946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const char* file, 153046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan int line, 153146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const string& source_text, 153246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const ArgumentMatcherTuple& m) 153346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { 1534dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter Mock::RegisterUseByOnCallOrExpectCall(MockObject(), file, line); 153546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan TypedExpectation<F>* const expectation = 153646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan new TypedExpectation<F>(this, file, line, source_text, m); 153746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const linked_ptr<ExpectationBase> untyped_expectation(expectation); 153846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan untyped_expectations_.push_back(untyped_expectation); 1539dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1540dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Adds this expectation into the implicit sequence if there is one. 1541dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter Sequence* const implicit_sequence = g_gmock_implicit_sequence.get(); 1542dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter if (implicit_sequence != NULL) { 154346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan implicit_sequence->AddExpectation(Expectation(untyped_expectation)); 1544dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1545dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1546dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter return *expectation; 1547dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1548dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1549dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // The current spec (either default action spec or expectation spec) 1550dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // being described on this function mocker. 1551dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter MockSpec<F>& current_spec() { return current_spec_; } 1552dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 155346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan private: 155446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan template <typename Func> friend class TypedExpectation; 1555dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 155646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Some utilities needed for implementing UntypedInvokeWith(). 1557dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1558dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Describes what default action will be performed for the given 1559dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // arguments. 1560dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // L = * 1561dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter void DescribeDefaultActionTo(const ArgumentTuple& args, 1562dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter ::std::ostream* os) const { 156346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const OnCallSpec<F>* const spec = FindOnCallSpec(args); 1564dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1565dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter if (spec == NULL) { 1566dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter *os << (internal::type_equals<Result, void>::value ? 1567dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter "returning directly.\n" : 1568dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter "returning default value.\n"); 1569dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } else { 1570dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter *os << "taking default action specified at:\n" 157146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan << FormatFileLocation(spec->file(), spec->line()) << "\n"; 1572dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1573dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1574dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1575dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Writes a message that the call is uninteresting (i.e. neither 1576dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // explicitly expected nor explicitly unexpected) to the given 1577dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // ostream. 157846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan virtual void UntypedDescribeUninterestingCall( 157946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const void* untyped_args, 158046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan ::std::ostream* os) const 158146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { 158246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const ArgumentTuple& args = 158346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan *static_cast<const ArgumentTuple*>(untyped_args); 1584dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter *os << "Uninteresting mock function call - "; 1585dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter DescribeDefaultActionTo(args, os); 1586dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter *os << " Function call: " << Name(); 158746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan UniversalPrint(args, os); 1588dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1589dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 159046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Returns the expectation that matches the given function arguments 159146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // (or NULL is there's no match); when a match is found, 159246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // untyped_action is set to point to the action that should be 159346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // performed (or NULL if the action is "do default"), and 159446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // is_excessive is modified to indicate whether the call exceeds the 159546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // expected number. 159646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // 1597dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Critical section: We must find the matching expectation and the 1598dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // corresponding action that needs to be taken in an ATOMIC 1599dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // transaction. Otherwise another thread may call this mock 1600dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // method in the middle and mess up the state. 1601dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // 1602dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // However, performing the action has to be left out of the critical 1603dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // section. The reason is that we have no control on what the 1604dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // action does (it can invoke an arbitrary user function or even a 1605dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // mock function) and excessive locking could cause a dead lock. 160646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan virtual const ExpectationBase* UntypedFindMatchingExpectation( 160746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const void* untyped_args, 160846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const void** untyped_action, bool* is_excessive, 160946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan ::std::ostream* what, ::std::ostream* why) 161046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { 161146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const ArgumentTuple& args = 161246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan *static_cast<const ArgumentTuple*>(untyped_args); 1613dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter MutexLock l(&g_gmock_mutex); 161446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan TypedExpectation<F>* exp = this->FindMatchingExpectationLocked(args); 161546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan if (exp == NULL) { // A match wasn't found. 1616dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter this->FormatUnexpectedCallMessageLocked(args, what, why); 161746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan return NULL; 1618dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1619dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1620dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // This line must be done before calling GetActionForArguments(), 1621dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // which will increment the call count for *exp and thus affect 1622dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // its saturation status. 162346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan *is_excessive = exp->IsSaturated(); 162446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const Action<F>* action = exp->GetActionForArguments(this, args, what, why); 162546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan if (action != NULL && action->IsDoDefault()) 162646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan action = NULL; // Normalize "do default" to NULL. 162746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan *untyped_action = action; 162846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan return exp; 162946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan } 163046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 163146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan // Prints the given function arguments to the ostream. 163246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan virtual void UntypedPrintArgs(const void* untyped_args, 163346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan ::std::ostream* os) const { 163446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const ArgumentTuple& args = 163546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan *static_cast<const ArgumentTuple*>(untyped_args); 163646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan UniversalPrint(args, os); 1637dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1638dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1639dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Returns the expectation that matches the arguments, or NULL if no 1640dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // expectation matches them. 164146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan TypedExpectation<F>* FindMatchingExpectationLocked( 164246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const ArgumentTuple& args) const 164346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { 1644dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter g_gmock_mutex.AssertHeld(); 164546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan for (typename UntypedExpectations::const_reverse_iterator it = 164646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan untyped_expectations_.rbegin(); 164746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan it != untyped_expectations_.rend(); ++it) { 164846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan TypedExpectation<F>* const exp = 164946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan static_cast<TypedExpectation<F>*>(it->get()); 1650dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter if (exp->ShouldHandleArguments(args)) { 1651dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter return exp; 1652dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1653dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1654dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter return NULL; 1655dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1656dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1657dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Returns a message that the arguments don't match any expectation. 165846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan void FormatUnexpectedCallMessageLocked( 165946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const ArgumentTuple& args, 166046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan ::std::ostream* os, 166146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan ::std::ostream* why) const 166246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { 1663dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter g_gmock_mutex.AssertHeld(); 1664dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter *os << "\nUnexpected mock function call - "; 1665dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter DescribeDefaultActionTo(args, os); 1666dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter PrintTriedExpectationsLocked(args, why); 1667dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1668dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1669dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Prints a list of expectations that have been tried against the 1670dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // current mock function call. 167146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan void PrintTriedExpectationsLocked( 167246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const ArgumentTuple& args, 167346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan ::std::ostream* why) const 167446108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { 1675dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter g_gmock_mutex.AssertHeld(); 167646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan const int count = static_cast<int>(untyped_expectations_.size()); 1677dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter *why << "Google Mock tried the following " << count << " " 1678dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter << (count == 1 ? "expectation, but it didn't match" : 1679dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter "expectations, but none matched") 1680dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter << ":\n"; 1681dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter for (int i = 0; i < count; i++) { 168246108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan TypedExpectation<F>* const expectation = 168346108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan static_cast<TypedExpectation<F>*>(untyped_expectations_[i].get()); 1684dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter *why << "\n"; 168546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan expectation->DescribeLocationTo(why); 1686dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter if (count > 1) { 168746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan *why << "tried expectation #" << i << ": "; 1688dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 168946108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan *why << expectation->source_text() << "...\n"; 169046108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan expectation->ExplainMatchResultTo(args, why); 169146108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan expectation->DescribeCallCountTo(why); 1692dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1693dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter } 1694dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1695dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // The current spec (either default action spec or expectation spec) 1696dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // being described on this function mocker. 1697dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter MockSpec<F> current_spec_; 1698dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1699dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // There is no generally useful and implementable semantics of 1700dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // copying a mock object, so copying a mock is usually a user error. 1701dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // Thus we disallow copying function mockers. If the user really 1702dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // wants to copy a mock object, he should implement his own copy 1703dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // operation, for example: 1704dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // 1705dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // class MockFoo : public Foo { 1706dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // public: 1707dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // // Defines a copy constructor explicitly. 1708dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // MockFoo(const MockFoo& src) {} 1709dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // ... 1710dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter // }; 1711dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter GTEST_DISALLOW_COPY_AND_ASSIGN_(FunctionMockerBase); 1712dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter}; // class FunctionMockerBase 1713dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1714dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter#ifdef _MSC_VER 171546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan# pragma warning(pop) // Restores the warning state. 1716dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter#endif // _MSV_VER 1717dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1718dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// Implements methods of FunctionMockerBase. 1719dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1720dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// Verifies that all expectations on this mock function have been 1721dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// satisfied. Reports one or more Google Test non-fatal failures and 1722dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// returns false if not. 1723dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1724dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// Reports an uninteresting call (whose description is in msg) in the 1725dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// manner specified by 'reaction'. 1726dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixtervoid ReportUninterestingCall(CallReaction reaction, const string& msg); 1727dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1728dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter} // namespace internal 1729dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1730dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// The style guide prohibits "using" statements in a namespace scope 1731dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// inside a header file. However, the MockSpec class template is 1732dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// meant to be defined in the ::testing namespace. The following line 1733dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// is just a trick for working around a bug in MSVC 8.0, which cannot 1734dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// handle it if we define MockSpec in ::testing. 1735dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixterusing internal::MockSpec; 1736dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1737dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// Const(x) is a convenient function for obtaining a const reference 1738dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// to x. This is useful for setting expectations on an overloaded 1739dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// const mock method, e.g. 1740dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// 1741dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// class MockFoo : public FooInterface { 1742dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// public: 1743dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// MOCK_METHOD0(Bar, int()); 1744dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// MOCK_CONST_METHOD0(Bar, int&()); 1745dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// }; 1746dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// 1747dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// MockFoo foo; 1748dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// // Expects a call to non-const MockFoo::Bar(). 1749dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// EXPECT_CALL(foo, Bar()); 1750dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// // Expects a call to const MockFoo::Bar(). 1751dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// EXPECT_CALL(Const(foo), Bar()); 1752dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixtertemplate <typename T> 1753dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixterinline const T& Const(const T& x) { return x; } 1754dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 175546108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan// Constructs an Expectation object that references and co-owns exp. 175646108a219a4b812dd8f36fee479a0340ea5963f5Ben Chaninline Expectation::Expectation(internal::ExpectationBase& exp) // NOLINT 175746108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan : expectation_base_(exp.GetHandle().expectation_base()) {} 175846108a219a4b812dd8f36fee479a0340ea5963f5Ben Chan 1759dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter} // namespace testing 1760dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1761dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// A separate macro is required to avoid compile errors when the name 1762dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// of the method used in call is a result of macro expansion. 1763dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// See CompilesWithMethodNameExpandedFromMacro tests in 1764dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter// internal/gmock-spec-builders_test.cc for more details. 1765dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter#define GMOCK_ON_CALL_IMPL_(obj, call) \ 1766dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter ((obj).gmock_##call).InternalDefaultActionSetAt(__FILE__, __LINE__, \ 1767dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter #obj, #call) 1768dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter#define ON_CALL(obj, call) GMOCK_ON_CALL_IMPL_(obj, call) 1769dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1770dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter#define GMOCK_EXPECT_CALL_IMPL_(obj, call) \ 1771dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter ((obj).gmock_##call).InternalExpectedAt(__FILE__, __LINE__, #obj, #call) 1772dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter#define EXPECT_CALL(obj, call) GMOCK_EXPECT_CALL_IMPL_(obj, call) 1773dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter 1774dd1c93d5709e32713961cfd95fe30489a4ad2d26Ken Mixter#endif // GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_ 1775