blob: 30721ce6666fa7cda779c4eaf1b97f8923f61158 [file] [log] [blame]
shiqiane35fdd92008-12-10 05:08:54 +00001// Copyright 2007, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29//
30// Author: wan@google.com (Zhanyong Wan)
31
32// Google Mock - a framework for writing C++ mock classes.
33//
34// This file implements the ON_CALL() and EXPECT_CALL() macros.
35//
36// A user can use the ON_CALL() macro to specify the default action of
37// a mock method. The syntax is:
38//
39// ON_CALL(mock_object, Method(argument-matchers))
zhanyong.wanbf550852009-06-09 06:09:53 +000040// .With(multi-argument-matcher)
shiqiane35fdd92008-12-10 05:08:54 +000041// .WillByDefault(action);
42//
zhanyong.wanbf550852009-06-09 06:09:53 +000043// where the .With() clause is optional.
shiqiane35fdd92008-12-10 05:08:54 +000044//
45// A user can use the EXPECT_CALL() macro to specify an expectation on
46// a mock method. The syntax is:
47//
48// EXPECT_CALL(mock_object, Method(argument-matchers))
zhanyong.wanbf550852009-06-09 06:09:53 +000049// .With(multi-argument-matchers)
shiqiane35fdd92008-12-10 05:08:54 +000050// .Times(cardinality)
51// .InSequence(sequences)
zhanyong.wan41b9b0b2009-07-01 19:04:51 +000052// .After(expectations)
shiqiane35fdd92008-12-10 05:08:54 +000053// .WillOnce(action)
54// .WillRepeatedly(action)
55// .RetiresOnSaturation();
56//
zhanyong.wan41b9b0b2009-07-01 19:04:51 +000057// where all clauses are optional, and .InSequence()/.After()/
58// .WillOnce() can appear any number of times.
shiqiane35fdd92008-12-10 05:08:54 +000059
60#ifndef GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_
61#define GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_
62
63#include <map>
64#include <set>
65#include <sstream>
66#include <string>
67#include <vector>
68
zhanyong.wan53e08c42010-09-14 05:38:21 +000069#include "gmock/gmock-actions.h"
70#include "gmock/gmock-cardinalities.h"
71#include "gmock/gmock-matchers.h"
72#include "gmock/internal/gmock-internal-utils.h"
73#include "gmock/internal/gmock-port.h"
74#include "gtest/gtest.h"
shiqiane35fdd92008-12-10 05:08:54 +000075
76namespace testing {
77
zhanyong.wan41b9b0b2009-07-01 19:04:51 +000078// An abstract handle of an expectation.
79class Expectation;
80
81// A set of expectation handles.
82class ExpectationSet;
83
shiqiane35fdd92008-12-10 05:08:54 +000084// Anything inside the 'internal' namespace IS INTERNAL IMPLEMENTATION
85// and MUST NOT BE USED IN USER CODE!!!
86namespace internal {
87
zhanyong.wan41b9b0b2009-07-01 19:04:51 +000088// Implements a mock function.
89template <typename F> class FunctionMocker;
shiqiane35fdd92008-12-10 05:08:54 +000090
91// Base class for expectations.
92class ExpectationBase;
93
zhanyong.wan41b9b0b2009-07-01 19:04:51 +000094// Implements an expectation.
95template <typename F> class TypedExpectation;
96
shiqiane35fdd92008-12-10 05:08:54 +000097// Helper class for testing the Expectation class template.
98class ExpectationTester;
99
100// Base class for function mockers.
zhanyong.wan41b9b0b2009-07-01 19:04:51 +0000101template <typename F> class FunctionMockerBase;
shiqiane35fdd92008-12-10 05:08:54 +0000102
shiqiane35fdd92008-12-10 05:08:54 +0000103// Protects the mock object registry (in class Mock), all function
104// mockers, and all expectations.
105//
106// The reason we don't use more fine-grained protection is: when a
107// mock function Foo() is called, it needs to consult its expectations
108// to see which one should be picked. If another thread is allowed to
109// call a mock function (either Foo() or a different one) at the same
110// time, it could affect the "retired" attributes of Foo()'s
111// expectations when InSequence() is used, and thus affect which
112// expectation gets picked. Therefore, we sequence all mock function
113// calls to ensure the integrity of the mock objects' states.
vladlosev587c1b32011-05-20 00:42:22 +0000114GTEST_API_ GTEST_DECLARE_STATIC_MUTEX_(g_gmock_mutex);
shiqiane35fdd92008-12-10 05:08:54 +0000115
zhanyong.waned6c9272011-02-23 19:39:27 +0000116// Untyped base class for ActionResultHolder<R>.
117class UntypedActionResultHolderBase;
118
shiqiane35fdd92008-12-10 05:08:54 +0000119// Abstract base class of FunctionMockerBase. This is the
120// type-agnostic part of the function mocker interface. Its pure
121// virtual methods are implemented by FunctionMockerBase.
vladlosev587c1b32011-05-20 00:42:22 +0000122class GTEST_API_ UntypedFunctionMockerBase {
shiqiane35fdd92008-12-10 05:08:54 +0000123 public:
zhanyong.waned6c9272011-02-23 19:39:27 +0000124 UntypedFunctionMockerBase();
125 virtual ~UntypedFunctionMockerBase();
shiqiane35fdd92008-12-10 05:08:54 +0000126
127 // Verifies that all expectations on this mock function have been
128 // satisfied. Reports one or more Google Test non-fatal failures
129 // and returns false if not.
vladlosev4d60a592011-10-24 21:16:22 +0000130 bool VerifyAndClearExpectationsLocked()
131 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex);
shiqiane35fdd92008-12-10 05:08:54 +0000132
133 // Clears the ON_CALL()s set on this mock function.
vladlosev4d60a592011-10-24 21:16:22 +0000134 virtual void ClearDefaultActionsLocked()
135 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) = 0;
zhanyong.waned6c9272011-02-23 19:39:27 +0000136
137 // In all of the following Untyped* functions, it's the caller's
138 // responsibility to guarantee the correctness of the arguments'
139 // types.
140
141 // Performs the default action with the given arguments and returns
142 // the action's result. The call description string will be used in
143 // the error message to describe the call in the case the default
144 // action fails.
145 // L = *
146 virtual UntypedActionResultHolderBase* UntypedPerformDefaultAction(
147 const void* untyped_args,
148 const string& call_description) const = 0;
149
150 // Performs the given action with the given arguments and returns
151 // the action's result.
152 // L = *
153 virtual UntypedActionResultHolderBase* UntypedPerformAction(
154 const void* untyped_action,
155 const void* untyped_args) const = 0;
156
157 // Writes a message that the call is uninteresting (i.e. neither
158 // explicitly expected nor explicitly unexpected) to the given
159 // ostream.
vladlosev4d60a592011-10-24 21:16:22 +0000160 virtual void UntypedDescribeUninterestingCall(
161 const void* untyped_args,
162 ::std::ostream* os) const
163 GTEST_LOCK_EXCLUDED_(g_gmock_mutex) = 0;
zhanyong.waned6c9272011-02-23 19:39:27 +0000164
165 // Returns the expectation that matches the given function arguments
166 // (or NULL is there's no match); when a match is found,
167 // untyped_action is set to point to the action that should be
168 // performed (or NULL if the action is "do default"), and
169 // is_excessive is modified to indicate whether the call exceeds the
170 // expected number.
zhanyong.waned6c9272011-02-23 19:39:27 +0000171 virtual const ExpectationBase* UntypedFindMatchingExpectation(
172 const void* untyped_args,
173 const void** untyped_action, bool* is_excessive,
vladlosev4d60a592011-10-24 21:16:22 +0000174 ::std::ostream* what, ::std::ostream* why)
175 GTEST_LOCK_EXCLUDED_(g_gmock_mutex) = 0;
zhanyong.waned6c9272011-02-23 19:39:27 +0000176
177 // Prints the given function arguments to the ostream.
178 virtual void UntypedPrintArgs(const void* untyped_args,
179 ::std::ostream* os) const = 0;
180
181 // Sets the mock object this mock method belongs to, and registers
182 // this information in the global mock registry. Will be called
183 // whenever an EXPECT_CALL() or ON_CALL() is executed on this mock
184 // method.
185 // TODO(wan@google.com): rename to SetAndRegisterOwner().
vladlosev4d60a592011-10-24 21:16:22 +0000186 void RegisterOwner(const void* mock_obj)
187 GTEST_LOCK_EXCLUDED_(g_gmock_mutex);
zhanyong.waned6c9272011-02-23 19:39:27 +0000188
189 // Sets the mock object this mock method belongs to, and sets the
190 // name of the mock function. Will be called upon each invocation
191 // of this mock function.
vladlosev4d60a592011-10-24 21:16:22 +0000192 void SetOwnerAndName(const void* mock_obj, const char* name)
193 GTEST_LOCK_EXCLUDED_(g_gmock_mutex);
zhanyong.waned6c9272011-02-23 19:39:27 +0000194
195 // Returns the mock object this mock method belongs to. Must be
196 // called after RegisterOwner() or SetOwnerAndName() has been
197 // called.
vladlosev4d60a592011-10-24 21:16:22 +0000198 const void* MockObject() const
199 GTEST_LOCK_EXCLUDED_(g_gmock_mutex);
zhanyong.waned6c9272011-02-23 19:39:27 +0000200
201 // Returns the name of this mock method. Must be called after
202 // SetOwnerAndName() has been called.
vladlosev4d60a592011-10-24 21:16:22 +0000203 const char* Name() const
204 GTEST_LOCK_EXCLUDED_(g_gmock_mutex);
zhanyong.waned6c9272011-02-23 19:39:27 +0000205
206 // Returns the result of invoking this mock function with the given
207 // arguments. This function can be safely called from multiple
208 // threads concurrently. The caller is responsible for deleting the
209 // result.
zhanyong.waned6c9272011-02-23 19:39:27 +0000210 const UntypedActionResultHolderBase* UntypedInvokeWith(
vladlosev4d60a592011-10-24 21:16:22 +0000211 const void* untyped_args)
212 GTEST_LOCK_EXCLUDED_(g_gmock_mutex);
zhanyong.waned6c9272011-02-23 19:39:27 +0000213
214 protected:
215 typedef std::vector<const void*> UntypedOnCallSpecs;
216
217 typedef std::vector<internal::linked_ptr<ExpectationBase> >
218 UntypedExpectations;
219
220 // Returns an Expectation object that references and co-owns exp,
221 // which must be an expectation on this mock function.
222 Expectation GetHandleOf(ExpectationBase* exp);
223
224 // Address of the mock object this mock method belongs to. Only
225 // valid after this mock method has been called or
226 // ON_CALL/EXPECT_CALL has been invoked on it.
227 const void* mock_obj_; // Protected by g_gmock_mutex.
228
229 // Name of the function being mocked. Only valid after this mock
230 // method has been called.
231 const char* name_; // Protected by g_gmock_mutex.
232
233 // All default action specs for this function mocker.
234 UntypedOnCallSpecs untyped_on_call_specs_;
235
236 // All expectations for this function mocker.
237 UntypedExpectations untyped_expectations_;
shiqiane35fdd92008-12-10 05:08:54 +0000238}; // class UntypedFunctionMockerBase
239
zhanyong.waned6c9272011-02-23 19:39:27 +0000240// Untyped base class for OnCallSpec<F>.
241class UntypedOnCallSpecBase {
shiqiane35fdd92008-12-10 05:08:54 +0000242 public:
zhanyong.waned6c9272011-02-23 19:39:27 +0000243 // The arguments are the location of the ON_CALL() statement.
244 UntypedOnCallSpecBase(const char* a_file, int a_line)
245 : file_(a_file), line_(a_line), last_clause_(kNone) {}
shiqiane35fdd92008-12-10 05:08:54 +0000246
247 // Where in the source file was the default action spec defined?
248 const char* file() const { return file_; }
249 int line() const { return line_; }
250
zhanyong.waned6c9272011-02-23 19:39:27 +0000251 protected:
252 // Gives each clause in the ON_CALL() statement a name.
253 enum Clause {
254 // Do not change the order of the enum members! The run-time
255 // syntax checking relies on it.
256 kNone,
257 kWith,
vladlosevab29bb62011-04-08 01:32:32 +0000258 kWillByDefault
zhanyong.waned6c9272011-02-23 19:39:27 +0000259 };
260
261 // Asserts that the ON_CALL() statement has a certain property.
262 void AssertSpecProperty(bool property, const string& failure_message) const {
263 Assert(property, file_, line_, failure_message);
264 }
265
266 // Expects that the ON_CALL() statement has a certain property.
267 void ExpectSpecProperty(bool property, const string& failure_message) const {
268 Expect(property, file_, line_, failure_message);
269 }
270
271 const char* file_;
272 int line_;
273
274 // The last clause in the ON_CALL() statement as seen so far.
275 // Initially kNone and changes as the statement is parsed.
276 Clause last_clause_;
277}; // class UntypedOnCallSpecBase
278
279// This template class implements an ON_CALL spec.
280template <typename F>
281class OnCallSpec : public UntypedOnCallSpecBase {
282 public:
283 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
284 typedef typename Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple;
285
286 // Constructs an OnCallSpec object from the information inside
287 // the parenthesis of an ON_CALL() statement.
288 OnCallSpec(const char* a_file, int a_line,
289 const ArgumentMatcherTuple& matchers)
290 : UntypedOnCallSpecBase(a_file, a_line),
291 matchers_(matchers),
292 // By default, extra_matcher_ should match anything. However,
293 // we cannot initialize it with _ as that triggers a compiler
294 // bug in Symbian's C++ compiler (cannot decide between two
295 // overloaded constructors of Matcher<const ArgumentTuple&>).
296 extra_matcher_(A<const ArgumentTuple&>()) {
297 }
298
zhanyong.wanbf550852009-06-09 06:09:53 +0000299 // Implements the .With() clause.
zhanyong.waned6c9272011-02-23 19:39:27 +0000300 OnCallSpec& With(const Matcher<const ArgumentTuple&>& m) {
shiqiane35fdd92008-12-10 05:08:54 +0000301 // Makes sure this is called at most once.
zhanyong.wanbf550852009-06-09 06:09:53 +0000302 ExpectSpecProperty(last_clause_ < kWith,
303 ".With() cannot appear "
shiqiane35fdd92008-12-10 05:08:54 +0000304 "more than once in an ON_CALL().");
zhanyong.wanbf550852009-06-09 06:09:53 +0000305 last_clause_ = kWith;
shiqiane35fdd92008-12-10 05:08:54 +0000306
307 extra_matcher_ = m;
308 return *this;
309 }
310
311 // Implements the .WillByDefault() clause.
zhanyong.waned6c9272011-02-23 19:39:27 +0000312 OnCallSpec& WillByDefault(const Action<F>& action) {
zhanyong.wanbf550852009-06-09 06:09:53 +0000313 ExpectSpecProperty(last_clause_ < kWillByDefault,
shiqiane35fdd92008-12-10 05:08:54 +0000314 ".WillByDefault() must appear "
315 "exactly once in an ON_CALL().");
zhanyong.wanbf550852009-06-09 06:09:53 +0000316 last_clause_ = kWillByDefault;
shiqiane35fdd92008-12-10 05:08:54 +0000317
318 ExpectSpecProperty(!action.IsDoDefault(),
319 "DoDefault() cannot be used in ON_CALL().");
320 action_ = action;
321 return *this;
322 }
323
324 // Returns true iff the given arguments match the matchers.
325 bool Matches(const ArgumentTuple& args) const {
326 return TupleMatches(matchers_, args) && extra_matcher_.Matches(args);
327 }
328
329 // Returns the action specified by the user.
330 const Action<F>& GetAction() const {
zhanyong.wanbf550852009-06-09 06:09:53 +0000331 AssertSpecProperty(last_clause_ == kWillByDefault,
shiqiane35fdd92008-12-10 05:08:54 +0000332 ".WillByDefault() must appear exactly "
333 "once in an ON_CALL().");
334 return action_;
335 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000336
shiqiane35fdd92008-12-10 05:08:54 +0000337 private:
shiqiane35fdd92008-12-10 05:08:54 +0000338 // The information in statement
339 //
340 // ON_CALL(mock_object, Method(matchers))
zhanyong.wanbf550852009-06-09 06:09:53 +0000341 // .With(multi-argument-matcher)
shiqiane35fdd92008-12-10 05:08:54 +0000342 // .WillByDefault(action);
343 //
344 // is recorded in the data members like this:
345 //
346 // source file that contains the statement => file_
347 // line number of the statement => line_
348 // matchers => matchers_
349 // multi-argument-matcher => extra_matcher_
350 // action => action_
shiqiane35fdd92008-12-10 05:08:54 +0000351 ArgumentMatcherTuple matchers_;
352 Matcher<const ArgumentTuple&> extra_matcher_;
353 Action<F> action_;
zhanyong.waned6c9272011-02-23 19:39:27 +0000354}; // class OnCallSpec
shiqiane35fdd92008-12-10 05:08:54 +0000355
zhanyong.wan41b9b0b2009-07-01 19:04:51 +0000356// Possible reactions on uninteresting calls. TODO(wan@google.com):
357// rename the enum values to the kFoo style.
shiqiane35fdd92008-12-10 05:08:54 +0000358enum CallReaction {
359 ALLOW,
360 WARN,
vladlosevab29bb62011-04-08 01:32:32 +0000361 FAIL
shiqiane35fdd92008-12-10 05:08:54 +0000362};
363
364} // namespace internal
365
366// Utilities for manipulating mock objects.
vladlosev587c1b32011-05-20 00:42:22 +0000367class GTEST_API_ Mock {
shiqiane35fdd92008-12-10 05:08:54 +0000368 public:
369 // The following public methods can be called concurrently.
370
zhanyong.wandf35a762009-04-22 22:25:31 +0000371 // Tells Google Mock to ignore mock_obj when checking for leaked
372 // mock objects.
vladlosev4d60a592011-10-24 21:16:22 +0000373 static void AllowLeak(const void* mock_obj)
374 GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
zhanyong.wandf35a762009-04-22 22:25:31 +0000375
shiqiane35fdd92008-12-10 05:08:54 +0000376 // Verifies and clears all expectations on the given mock object.
377 // If the expectations aren't satisfied, generates one or more
378 // Google Test non-fatal failures and returns false.
vladlosev4d60a592011-10-24 21:16:22 +0000379 static bool VerifyAndClearExpectations(void* mock_obj)
380 GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
shiqiane35fdd92008-12-10 05:08:54 +0000381
382 // Verifies all expectations on the given mock object and clears its
383 // default actions and expectations. Returns true iff the
384 // verification was successful.
vladlosev4d60a592011-10-24 21:16:22 +0000385 static bool VerifyAndClear(void* mock_obj)
386 GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
jgm79a367e2012-04-10 16:02:11 +0000387
shiqiane35fdd92008-12-10 05:08:54 +0000388 private:
zhanyong.waned6c9272011-02-23 19:39:27 +0000389 friend class internal::UntypedFunctionMockerBase;
390
shiqiane35fdd92008-12-10 05:08:54 +0000391 // Needed for a function mocker to register itself (so that we know
392 // how to clear a mock object).
393 template <typename F>
394 friend class internal::FunctionMockerBase;
395
shiqiane35fdd92008-12-10 05:08:54 +0000396 template <typename M>
397 friend class NiceMock;
398
399 template <typename M>
400 friend class StrictMock;
401
402 // Tells Google Mock to allow uninteresting calls on the given mock
403 // object.
vladlosev4d60a592011-10-24 21:16:22 +0000404 static void AllowUninterestingCalls(const void* mock_obj)
405 GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
shiqiane35fdd92008-12-10 05:08:54 +0000406
407 // Tells Google Mock to warn the user about uninteresting calls on
408 // the given mock object.
vladlosev4d60a592011-10-24 21:16:22 +0000409 static void WarnUninterestingCalls(const void* mock_obj)
410 GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
shiqiane35fdd92008-12-10 05:08:54 +0000411
412 // Tells Google Mock to fail uninteresting calls on the given mock
413 // object.
vladlosev4d60a592011-10-24 21:16:22 +0000414 static void FailUninterestingCalls(const void* mock_obj)
415 GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
shiqiane35fdd92008-12-10 05:08:54 +0000416
417 // Tells Google Mock the given mock object is being destroyed and
418 // its entry in the call-reaction table should be removed.
vladlosev4d60a592011-10-24 21:16:22 +0000419 static void UnregisterCallReaction(const void* mock_obj)
420 GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
shiqiane35fdd92008-12-10 05:08:54 +0000421
422 // Returns the reaction Google Mock will have on uninteresting calls
423 // made on the given mock object.
shiqiane35fdd92008-12-10 05:08:54 +0000424 static internal::CallReaction GetReactionOnUninterestingCalls(
425 const void* mock_obj);
vladlosev4d60a592011-10-24 21:16:22 +0000426 GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
shiqiane35fdd92008-12-10 05:08:54 +0000427
428 // Verifies that all expectations on the given mock object have been
429 // satisfied. Reports one or more Google Test non-fatal failures
430 // and returns false if not.
vladlosev4d60a592011-10-24 21:16:22 +0000431 static bool VerifyAndClearExpectationsLocked(void* mock_obj)
432 GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex);
shiqiane35fdd92008-12-10 05:08:54 +0000433
434 // Clears all ON_CALL()s set on the given mock object.
vladlosev4d60a592011-10-24 21:16:22 +0000435 static void ClearDefaultActionsLocked(void* mock_obj)
436 GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex);
shiqiane35fdd92008-12-10 05:08:54 +0000437
438 // Registers a mock object and a mock method it owns.
vladlosev4d60a592011-10-24 21:16:22 +0000439 static void Register(
440 const void* mock_obj,
441 internal::UntypedFunctionMockerBase* mocker)
442 GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
shiqiane35fdd92008-12-10 05:08:54 +0000443
zhanyong.wandf35a762009-04-22 22:25:31 +0000444 // Tells Google Mock where in the source code mock_obj is used in an
445 // ON_CALL or EXPECT_CALL. In case mock_obj is leaked, this
446 // information helps the user identify which object it is.
zhanyong.wandf35a762009-04-22 22:25:31 +0000447 static void RegisterUseByOnCallOrExpectCall(
vladlosev4d60a592011-10-24 21:16:22 +0000448 const void* mock_obj, const char* file, int line)
449 GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
zhanyong.wandf35a762009-04-22 22:25:31 +0000450
shiqiane35fdd92008-12-10 05:08:54 +0000451 // Unregisters a mock method; removes the owning mock object from
452 // the registry when the last mock method associated with it has
453 // been unregistered. This is called only in the destructor of
454 // FunctionMockerBase.
vladlosev4d60a592011-10-24 21:16:22 +0000455 static void UnregisterLocked(internal::UntypedFunctionMockerBase* mocker)
456 GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex);
shiqiane35fdd92008-12-10 05:08:54 +0000457}; // class Mock
458
zhanyong.wan41b9b0b2009-07-01 19:04:51 +0000459// An abstract handle of an expectation. Useful in the .After()
460// clause of EXPECT_CALL() for setting the (partial) order of
461// expectations. The syntax:
462//
463// Expectation e1 = EXPECT_CALL(...)...;
464// EXPECT_CALL(...).After(e1)...;
465//
466// sets two expectations where the latter can only be matched after
467// the former has been satisfied.
468//
469// Notes:
470// - This class is copyable and has value semantics.
471// - Constness is shallow: a const Expectation object itself cannot
472// be modified, but the mutable methods of the ExpectationBase
473// object it references can be called via expectation_base().
zhanyong.wan7c95d832009-10-01 21:56:16 +0000474// - The constructors and destructor are defined out-of-line because
475// the Symbian WINSCW compiler wants to otherwise instantiate them
476// when it sees this class definition, at which point it doesn't have
477// ExpectationBase available yet, leading to incorrect destruction
478// in the linked_ptr (or compilation errors if using a checking
479// linked_ptr).
vladlosev587c1b32011-05-20 00:42:22 +0000480class GTEST_API_ Expectation {
zhanyong.wan41b9b0b2009-07-01 19:04:51 +0000481 public:
482 // Constructs a null object that doesn't reference any expectation.
zhanyong.wan7c95d832009-10-01 21:56:16 +0000483 Expectation();
484
485 ~Expectation();
zhanyong.wan41b9b0b2009-07-01 19:04:51 +0000486
487 // This single-argument ctor must not be explicit, in order to support the
488 // Expectation e = EXPECT_CALL(...);
489 // syntax.
490 //
491 // A TypedExpectation object stores its pre-requisites as
492 // Expectation objects, and needs to call the non-const Retire()
493 // method on the ExpectationBase objects they reference. Therefore
494 // Expectation must receive a *non-const* reference to the
495 // ExpectationBase object.
496 Expectation(internal::ExpectationBase& exp); // NOLINT
497
498 // The compiler-generated copy ctor and operator= work exactly as
499 // intended, so we don't need to define our own.
500
501 // Returns true iff rhs references the same expectation as this object does.
502 bool operator==(const Expectation& rhs) const {
503 return expectation_base_ == rhs.expectation_base_;
504 }
505
506 bool operator!=(const Expectation& rhs) const { return !(*this == rhs); }
507
508 private:
509 friend class ExpectationSet;
510 friend class Sequence;
511 friend class ::testing::internal::ExpectationBase;
zhanyong.waned6c9272011-02-23 19:39:27 +0000512 friend class ::testing::internal::UntypedFunctionMockerBase;
zhanyong.wan41b9b0b2009-07-01 19:04:51 +0000513
514 template <typename F>
515 friend class ::testing::internal::FunctionMockerBase;
516
517 template <typename F>
518 friend class ::testing::internal::TypedExpectation;
519
520 // This comparator is needed for putting Expectation objects into a set.
521 class Less {
522 public:
523 bool operator()(const Expectation& lhs, const Expectation& rhs) const {
524 return lhs.expectation_base_.get() < rhs.expectation_base_.get();
525 }
526 };
527
528 typedef ::std::set<Expectation, Less> Set;
529
530 Expectation(
zhanyong.wan7c95d832009-10-01 21:56:16 +0000531 const internal::linked_ptr<internal::ExpectationBase>& expectation_base);
zhanyong.wan41b9b0b2009-07-01 19:04:51 +0000532
533 // Returns the expectation this object references.
534 const internal::linked_ptr<internal::ExpectationBase>&
535 expectation_base() const {
536 return expectation_base_;
537 }
538
539 // A linked_ptr that co-owns the expectation this handle references.
540 internal::linked_ptr<internal::ExpectationBase> expectation_base_;
541};
542
543// A set of expectation handles. Useful in the .After() clause of
544// EXPECT_CALL() for setting the (partial) order of expectations. The
545// syntax:
546//
547// ExpectationSet es;
548// es += EXPECT_CALL(...)...;
549// es += EXPECT_CALL(...)...;
550// EXPECT_CALL(...).After(es)...;
551//
552// sets three expectations where the last one can only be matched
553// after the first two have both been satisfied.
554//
555// This class is copyable and has value semantics.
556class ExpectationSet {
557 public:
558 // A bidirectional iterator that can read a const element in the set.
559 typedef Expectation::Set::const_iterator const_iterator;
560
561 // An object stored in the set. This is an alias of Expectation.
562 typedef Expectation::Set::value_type value_type;
563
564 // Constructs an empty set.
565 ExpectationSet() {}
566
567 // This single-argument ctor must not be explicit, in order to support the
568 // ExpectationSet es = EXPECT_CALL(...);
569 // syntax.
570 ExpectationSet(internal::ExpectationBase& exp) { // NOLINT
571 *this += Expectation(exp);
572 }
573
574 // This single-argument ctor implements implicit conversion from
575 // Expectation and thus must not be explicit. This allows either an
576 // Expectation or an ExpectationSet to be used in .After().
577 ExpectationSet(const Expectation& e) { // NOLINT
578 *this += e;
579 }
580
581 // The compiler-generator ctor and operator= works exactly as
582 // intended, so we don't need to define our own.
583
584 // Returns true iff rhs contains the same set of Expectation objects
585 // as this does.
586 bool operator==(const ExpectationSet& rhs) const {
587 return expectations_ == rhs.expectations_;
588 }
589
590 bool operator!=(const ExpectationSet& rhs) const { return !(*this == rhs); }
591
592 // Implements the syntax
593 // expectation_set += EXPECT_CALL(...);
594 ExpectationSet& operator+=(const Expectation& e) {
595 expectations_.insert(e);
596 return *this;
597 }
598
599 int size() const { return static_cast<int>(expectations_.size()); }
600
601 const_iterator begin() const { return expectations_.begin(); }
602 const_iterator end() const { return expectations_.end(); }
603
604 private:
605 Expectation::Set expectations_;
606};
607
608
shiqiane35fdd92008-12-10 05:08:54 +0000609// Sequence objects are used by a user to specify the relative order
610// in which the expectations should match. They are copyable (we rely
611// on the compiler-defined copy constructor and assignment operator).
vladlosev587c1b32011-05-20 00:42:22 +0000612class GTEST_API_ Sequence {
shiqiane35fdd92008-12-10 05:08:54 +0000613 public:
614 // Constructs an empty sequence.
zhanyong.wan41b9b0b2009-07-01 19:04:51 +0000615 Sequence() : last_expectation_(new Expectation) {}
shiqiane35fdd92008-12-10 05:08:54 +0000616
617 // Adds an expectation to this sequence. The caller must ensure
618 // that no other thread is accessing this Sequence object.
zhanyong.wan41b9b0b2009-07-01 19:04:51 +0000619 void AddExpectation(const Expectation& expectation) const;
620
shiqiane35fdd92008-12-10 05:08:54 +0000621 private:
zhanyong.wan41b9b0b2009-07-01 19:04:51 +0000622 // The last expectation in this sequence. We use a linked_ptr here
623 // because Sequence objects are copyable and we want the copies to
624 // be aliases. The linked_ptr allows the copies to co-own and share
625 // the same Expectation object.
626 internal::linked_ptr<Expectation> last_expectation_;
shiqiane35fdd92008-12-10 05:08:54 +0000627}; // class Sequence
628
629// An object of this type causes all EXPECT_CALL() statements
630// encountered in its scope to be put in an anonymous sequence. The
631// work is done in the constructor and destructor. You should only
632// create an InSequence object on the stack.
633//
634// The sole purpose for this class is to support easy definition of
635// sequential expectations, e.g.
636//
637// {
638// InSequence dummy; // The name of the object doesn't matter.
639//
640// // The following expectations must match in the order they appear.
641// EXPECT_CALL(a, Bar())...;
642// EXPECT_CALL(a, Baz())...;
643// ...
644// EXPECT_CALL(b, Xyz())...;
645// }
646//
647// You can create InSequence objects in multiple threads, as long as
648// they are used to affect different mock objects. The idea is that
649// each thread can create and set up its own mocks as if it's the only
650// thread. However, for clarity of your tests we recommend you to set
651// up mocks in the main thread unless you have a good reason not to do
652// so.
vladlosev587c1b32011-05-20 00:42:22 +0000653class GTEST_API_ InSequence {
shiqiane35fdd92008-12-10 05:08:54 +0000654 public:
655 InSequence();
656 ~InSequence();
657 private:
658 bool sequence_created_;
659
660 GTEST_DISALLOW_COPY_AND_ASSIGN_(InSequence); // NOLINT
zhanyong.wanccedc1c2010-08-09 22:46:12 +0000661} GTEST_ATTRIBUTE_UNUSED_;
shiqiane35fdd92008-12-10 05:08:54 +0000662
663namespace internal {
664
665// Points to the implicit sequence introduced by a living InSequence
666// object (if any) in the current thread or NULL.
vladlosev587c1b32011-05-20 00:42:22 +0000667GTEST_API_ extern ThreadLocal<Sequence*> g_gmock_implicit_sequence;
shiqiane35fdd92008-12-10 05:08:54 +0000668
669// Base class for implementing expectations.
670//
671// There are two reasons for having a type-agnostic base class for
672// Expectation:
673//
674// 1. We need to store collections of expectations of different
675// types (e.g. all pre-requisites of a particular expectation, all
676// expectations in a sequence). Therefore these expectation objects
677// must share a common base class.
678//
679// 2. We can avoid binary code bloat by moving methods not depending
680// on the template argument of Expectation to the base class.
681//
682// This class is internal and mustn't be used by user code directly.
vladlosev587c1b32011-05-20 00:42:22 +0000683class GTEST_API_ ExpectationBase {
shiqiane35fdd92008-12-10 05:08:54 +0000684 public:
vladlosev6c54a5e2009-10-21 06:15:34 +0000685 // source_text is the EXPECT_CALL(...) source that created this Expectation.
686 ExpectationBase(const char* file, int line, const string& source_text);
shiqiane35fdd92008-12-10 05:08:54 +0000687
688 virtual ~ExpectationBase();
689
690 // Where in the source file was the expectation spec defined?
691 const char* file() const { return file_; }
692 int line() const { return line_; }
vladlosev6c54a5e2009-10-21 06:15:34 +0000693 const char* source_text() const { return source_text_.c_str(); }
shiqiane35fdd92008-12-10 05:08:54 +0000694 // Returns the cardinality specified in the expectation spec.
695 const Cardinality& cardinality() const { return cardinality_; }
696
697 // Describes the source file location of this expectation.
698 void DescribeLocationTo(::std::ostream* os) const {
vladloseve5121b52011-02-11 23:50:38 +0000699 *os << FormatFileLocation(file(), line()) << " ";
shiqiane35fdd92008-12-10 05:08:54 +0000700 }
701
702 // Describes how many times a function call matching this
703 // expectation has occurred.
vladlosev4d60a592011-10-24 21:16:22 +0000704 void DescribeCallCountTo(::std::ostream* os) const
705 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex);
zhanyong.waned6c9272011-02-23 19:39:27 +0000706
707 // If this mock method has an extra matcher (i.e. .With(matcher)),
708 // describes it to the ostream.
709 virtual void MaybeDescribeExtraMatcherTo(::std::ostream* os) = 0;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000710
shiqiane35fdd92008-12-10 05:08:54 +0000711 protected:
zhanyong.wan41b9b0b2009-07-01 19:04:51 +0000712 friend class ::testing::Expectation;
zhanyong.waned6c9272011-02-23 19:39:27 +0000713 friend class UntypedFunctionMockerBase;
shiqiane35fdd92008-12-10 05:08:54 +0000714
715 enum Clause {
716 // Don't change the order of the enum members!
zhanyong.wanbf550852009-06-09 06:09:53 +0000717 kNone,
718 kWith,
719 kTimes,
720 kInSequence,
zhanyong.wan41b9b0b2009-07-01 19:04:51 +0000721 kAfter,
zhanyong.wanbf550852009-06-09 06:09:53 +0000722 kWillOnce,
723 kWillRepeatedly,
vladlosevab29bb62011-04-08 01:32:32 +0000724 kRetiresOnSaturation
shiqiane35fdd92008-12-10 05:08:54 +0000725 };
726
zhanyong.waned6c9272011-02-23 19:39:27 +0000727 typedef std::vector<const void*> UntypedActions;
728
zhanyong.wan41b9b0b2009-07-01 19:04:51 +0000729 // Returns an Expectation object that references and co-owns this
730 // expectation.
731 virtual Expectation GetHandle() = 0;
732
shiqiane35fdd92008-12-10 05:08:54 +0000733 // Asserts that the EXPECT_CALL() statement has the given property.
734 void AssertSpecProperty(bool property, const string& failure_message) const {
735 Assert(property, file_, line_, failure_message);
736 }
737
738 // Expects that the EXPECT_CALL() statement has the given property.
739 void ExpectSpecProperty(bool property, const string& failure_message) const {
740 Expect(property, file_, line_, failure_message);
741 }
742
743 // Explicitly specifies the cardinality of this expectation. Used
744 // by the subclasses to implement the .Times() clause.
745 void SpecifyCardinality(const Cardinality& cardinality);
746
747 // Returns true iff the user specified the cardinality explicitly
748 // using a .Times().
749 bool cardinality_specified() const { return cardinality_specified_; }
750
751 // Sets the cardinality of this expectation spec.
zhanyong.wan32de5f52009-12-23 00:13:23 +0000752 void set_cardinality(const Cardinality& a_cardinality) {
753 cardinality_ = a_cardinality;
shiqiane35fdd92008-12-10 05:08:54 +0000754 }
755
756 // The following group of methods should only be called after the
757 // EXPECT_CALL() statement, and only when g_gmock_mutex is held by
758 // the current thread.
759
760 // Retires all pre-requisites of this expectation.
vladlosev4d60a592011-10-24 21:16:22 +0000761 void RetireAllPreRequisites()
762 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex);
shiqiane35fdd92008-12-10 05:08:54 +0000763
764 // Returns true iff this expectation is retired.
vladlosev4d60a592011-10-24 21:16:22 +0000765 bool is_retired() const
766 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
shiqiane35fdd92008-12-10 05:08:54 +0000767 g_gmock_mutex.AssertHeld();
768 return retired_;
769 }
770
771 // Retires this expectation.
vladlosev4d60a592011-10-24 21:16:22 +0000772 void Retire()
773 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
shiqiane35fdd92008-12-10 05:08:54 +0000774 g_gmock_mutex.AssertHeld();
775 retired_ = true;
776 }
777
778 // Returns true iff this expectation is satisfied.
vladlosev4d60a592011-10-24 21:16:22 +0000779 bool IsSatisfied() const
780 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
shiqiane35fdd92008-12-10 05:08:54 +0000781 g_gmock_mutex.AssertHeld();
782 return cardinality().IsSatisfiedByCallCount(call_count_);
783 }
784
785 // Returns true iff this expectation is saturated.
vladlosev4d60a592011-10-24 21:16:22 +0000786 bool IsSaturated() const
787 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
shiqiane35fdd92008-12-10 05:08:54 +0000788 g_gmock_mutex.AssertHeld();
789 return cardinality().IsSaturatedByCallCount(call_count_);
790 }
791
792 // Returns true iff this expectation is over-saturated.
vladlosev4d60a592011-10-24 21:16:22 +0000793 bool IsOverSaturated() const
794 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
shiqiane35fdd92008-12-10 05:08:54 +0000795 g_gmock_mutex.AssertHeld();
796 return cardinality().IsOverSaturatedByCallCount(call_count_);
797 }
798
799 // Returns true iff all pre-requisites of this expectation are satisfied.
vladlosev4d60a592011-10-24 21:16:22 +0000800 bool AllPrerequisitesAreSatisfied() const
801 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex);
shiqiane35fdd92008-12-10 05:08:54 +0000802
803 // Adds unsatisfied pre-requisites of this expectation to 'result'.
vladlosev4d60a592011-10-24 21:16:22 +0000804 void FindUnsatisfiedPrerequisites(ExpectationSet* result) const
805 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex);
shiqiane35fdd92008-12-10 05:08:54 +0000806
807 // Returns the number this expectation has been invoked.
vladlosev4d60a592011-10-24 21:16:22 +0000808 int call_count() const
809 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
shiqiane35fdd92008-12-10 05:08:54 +0000810 g_gmock_mutex.AssertHeld();
811 return call_count_;
812 }
813
814 // Increments the number this expectation has been invoked.
vladlosev4d60a592011-10-24 21:16:22 +0000815 void IncrementCallCount()
816 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
shiqiane35fdd92008-12-10 05:08:54 +0000817 g_gmock_mutex.AssertHeld();
818 call_count_++;
819 }
820
zhanyong.waned6c9272011-02-23 19:39:27 +0000821 // Checks the action count (i.e. the number of WillOnce() and
822 // WillRepeatedly() clauses) against the cardinality if this hasn't
823 // been done before. Prints a warning if there are too many or too
824 // few actions.
vladlosev4d60a592011-10-24 21:16:22 +0000825 void CheckActionCountIfNotDone() const
826 GTEST_LOCK_EXCLUDED_(mutex_);
zhanyong.waned6c9272011-02-23 19:39:27 +0000827
shiqiane35fdd92008-12-10 05:08:54 +0000828 friend class ::testing::Sequence;
829 friend class ::testing::internal::ExpectationTester;
830
831 template <typename Function>
zhanyong.wan41b9b0b2009-07-01 19:04:51 +0000832 friend class TypedExpectation;
shiqiane35fdd92008-12-10 05:08:54 +0000833
zhanyong.waned6c9272011-02-23 19:39:27 +0000834 // Implements the .Times() clause.
835 void UntypedTimes(const Cardinality& a_cardinality);
836
shiqiane35fdd92008-12-10 05:08:54 +0000837 // This group of fields are part of the spec and won't change after
838 // an EXPECT_CALL() statement finishes.
vladlosev6c54a5e2009-10-21 06:15:34 +0000839 const char* file_; // The file that contains the expectation.
840 int line_; // The line number of the expectation.
841 const string source_text_; // The EXPECT_CALL(...) source text.
shiqiane35fdd92008-12-10 05:08:54 +0000842 // True iff the cardinality is specified explicitly.
843 bool cardinality_specified_;
844 Cardinality cardinality_; // The cardinality of the expectation.
zhanyong.wan41b9b0b2009-07-01 19:04:51 +0000845 // The immediate pre-requisites (i.e. expectations that must be
846 // satisfied before this expectation can be matched) of this
847 // expectation. We use linked_ptr in the set because we want an
848 // Expectation object to be co-owned by its FunctionMocker and its
849 // successors. This allows multiple mock objects to be deleted at
850 // different times.
851 ExpectationSet immediate_prerequisites_;
shiqiane35fdd92008-12-10 05:08:54 +0000852
853 // This group of fields are the current state of the expectation,
854 // and can change as the mock function is called.
855 int call_count_; // How many times this expectation has been invoked.
856 bool retired_; // True iff this expectation has retired.
zhanyong.waned6c9272011-02-23 19:39:27 +0000857 UntypedActions untyped_actions_;
858 bool extra_matcher_specified_;
859 bool repeated_action_specified_; // True if a WillRepeatedly() was specified.
860 bool retires_on_saturation_;
861 Clause last_clause_;
862 mutable bool action_count_checked_; // Under mutex_.
863 mutable Mutex mutex_; // Protects action_count_checked_.
zhanyong.wan32de5f52009-12-23 00:13:23 +0000864
865 GTEST_DISALLOW_ASSIGN_(ExpectationBase);
shiqiane35fdd92008-12-10 05:08:54 +0000866}; // class ExpectationBase
867
868// Impements an expectation for the given function type.
869template <typename F>
zhanyong.wan41b9b0b2009-07-01 19:04:51 +0000870class TypedExpectation : public ExpectationBase {
shiqiane35fdd92008-12-10 05:08:54 +0000871 public:
872 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
873 typedef typename Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple;
874 typedef typename Function<F>::Result Result;
875
vladlosev6c54a5e2009-10-21 06:15:34 +0000876 TypedExpectation(FunctionMockerBase<F>* owner,
zhanyong.wan32de5f52009-12-23 00:13:23 +0000877 const char* a_file, int a_line, const string& a_source_text,
zhanyong.wan41b9b0b2009-07-01 19:04:51 +0000878 const ArgumentMatcherTuple& m)
zhanyong.wan32de5f52009-12-23 00:13:23 +0000879 : ExpectationBase(a_file, a_line, a_source_text),
shiqiane35fdd92008-12-10 05:08:54 +0000880 owner_(owner),
881 matchers_(m),
zhanyong.wan18490652009-05-11 18:54:08 +0000882 // By default, extra_matcher_ should match anything. However,
883 // we cannot initialize it with _ as that triggers a compiler
884 // bug in Symbian's C++ compiler (cannot decide between two
885 // overloaded constructors of Matcher<const ArgumentTuple&>).
886 extra_matcher_(A<const ArgumentTuple&>()),
zhanyong.waned6c9272011-02-23 19:39:27 +0000887 repeated_action_(DoDefault()) {}
shiqiane35fdd92008-12-10 05:08:54 +0000888
zhanyong.wan41b9b0b2009-07-01 19:04:51 +0000889 virtual ~TypedExpectation() {
shiqiane35fdd92008-12-10 05:08:54 +0000890 // Check the validity of the action count if it hasn't been done
891 // yet (for example, if the expectation was never used).
892 CheckActionCountIfNotDone();
zhanyong.waned6c9272011-02-23 19:39:27 +0000893 for (UntypedActions::const_iterator it = untyped_actions_.begin();
894 it != untyped_actions_.end(); ++it) {
895 delete static_cast<const Action<F>*>(*it);
896 }
shiqiane35fdd92008-12-10 05:08:54 +0000897 }
898
zhanyong.wanbf550852009-06-09 06:09:53 +0000899 // Implements the .With() clause.
zhanyong.wan41b9b0b2009-07-01 19:04:51 +0000900 TypedExpectation& With(const Matcher<const ArgumentTuple&>& m) {
zhanyong.wanbf550852009-06-09 06:09:53 +0000901 if (last_clause_ == kWith) {
shiqiane35fdd92008-12-10 05:08:54 +0000902 ExpectSpecProperty(false,
zhanyong.wanbf550852009-06-09 06:09:53 +0000903 ".With() cannot appear "
shiqiane35fdd92008-12-10 05:08:54 +0000904 "more than once in an EXPECT_CALL().");
905 } else {
zhanyong.wanbf550852009-06-09 06:09:53 +0000906 ExpectSpecProperty(last_clause_ < kWith,
907 ".With() must be the first "
shiqiane35fdd92008-12-10 05:08:54 +0000908 "clause in an EXPECT_CALL().");
909 }
zhanyong.wanbf550852009-06-09 06:09:53 +0000910 last_clause_ = kWith;
shiqiane35fdd92008-12-10 05:08:54 +0000911
912 extra_matcher_ = m;
vladlosev6c54a5e2009-10-21 06:15:34 +0000913 extra_matcher_specified_ = true;
shiqiane35fdd92008-12-10 05:08:54 +0000914 return *this;
915 }
916
917 // Implements the .Times() clause.
zhanyong.wan32de5f52009-12-23 00:13:23 +0000918 TypedExpectation& Times(const Cardinality& a_cardinality) {
zhanyong.waned6c9272011-02-23 19:39:27 +0000919 ExpectationBase::UntypedTimes(a_cardinality);
shiqiane35fdd92008-12-10 05:08:54 +0000920 return *this;
921 }
922
923 // Implements the .Times() clause.
zhanyong.wan41b9b0b2009-07-01 19:04:51 +0000924 TypedExpectation& Times(int n) {
shiqiane35fdd92008-12-10 05:08:54 +0000925 return Times(Exactly(n));
926 }
927
928 // Implements the .InSequence() clause.
zhanyong.wan41b9b0b2009-07-01 19:04:51 +0000929 TypedExpectation& InSequence(const Sequence& s) {
zhanyong.wanbf550852009-06-09 06:09:53 +0000930 ExpectSpecProperty(last_clause_ <= kInSequence,
zhanyong.wan41b9b0b2009-07-01 19:04:51 +0000931 ".InSequence() cannot appear after .After(),"
932 " .WillOnce(), .WillRepeatedly(), or "
shiqiane35fdd92008-12-10 05:08:54 +0000933 ".RetiresOnSaturation().");
zhanyong.wanbf550852009-06-09 06:09:53 +0000934 last_clause_ = kInSequence;
shiqiane35fdd92008-12-10 05:08:54 +0000935
zhanyong.wan41b9b0b2009-07-01 19:04:51 +0000936 s.AddExpectation(GetHandle());
shiqiane35fdd92008-12-10 05:08:54 +0000937 return *this;
938 }
zhanyong.wan41b9b0b2009-07-01 19:04:51 +0000939 TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2) {
shiqiane35fdd92008-12-10 05:08:54 +0000940 return InSequence(s1).InSequence(s2);
941 }
zhanyong.wan41b9b0b2009-07-01 19:04:51 +0000942 TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2,
943 const Sequence& s3) {
shiqiane35fdd92008-12-10 05:08:54 +0000944 return InSequence(s1, s2).InSequence(s3);
945 }
zhanyong.wan41b9b0b2009-07-01 19:04:51 +0000946 TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2,
947 const Sequence& s3, const Sequence& s4) {
shiqiane35fdd92008-12-10 05:08:54 +0000948 return InSequence(s1, s2, s3).InSequence(s4);
949 }
zhanyong.wan41b9b0b2009-07-01 19:04:51 +0000950 TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2,
951 const Sequence& s3, const Sequence& s4,
952 const Sequence& s5) {
shiqiane35fdd92008-12-10 05:08:54 +0000953 return InSequence(s1, s2, s3, s4).InSequence(s5);
954 }
955
zhanyong.wan41b9b0b2009-07-01 19:04:51 +0000956 // Implements that .After() clause.
957 TypedExpectation& After(const ExpectationSet& s) {
958 ExpectSpecProperty(last_clause_ <= kAfter,
959 ".After() cannot appear after .WillOnce(),"
960 " .WillRepeatedly(), or "
961 ".RetiresOnSaturation().");
962 last_clause_ = kAfter;
963
964 for (ExpectationSet::const_iterator it = s.begin(); it != s.end(); ++it) {
965 immediate_prerequisites_ += *it;
966 }
967 return *this;
968 }
969 TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2) {
970 return After(s1).After(s2);
971 }
972 TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2,
973 const ExpectationSet& s3) {
974 return After(s1, s2).After(s3);
975 }
976 TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2,
977 const ExpectationSet& s3, const ExpectationSet& s4) {
978 return After(s1, s2, s3).After(s4);
979 }
980 TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2,
981 const ExpectationSet& s3, const ExpectationSet& s4,
982 const ExpectationSet& s5) {
983 return After(s1, s2, s3, s4).After(s5);
984 }
985
shiqiane35fdd92008-12-10 05:08:54 +0000986 // Implements the .WillOnce() clause.
zhanyong.wan41b9b0b2009-07-01 19:04:51 +0000987 TypedExpectation& WillOnce(const Action<F>& action) {
zhanyong.wanbf550852009-06-09 06:09:53 +0000988 ExpectSpecProperty(last_clause_ <= kWillOnce,
shiqiane35fdd92008-12-10 05:08:54 +0000989 ".WillOnce() cannot appear after "
990 ".WillRepeatedly() or .RetiresOnSaturation().");
zhanyong.wanbf550852009-06-09 06:09:53 +0000991 last_clause_ = kWillOnce;
shiqiane35fdd92008-12-10 05:08:54 +0000992
zhanyong.waned6c9272011-02-23 19:39:27 +0000993 untyped_actions_.push_back(new Action<F>(action));
shiqiane35fdd92008-12-10 05:08:54 +0000994 if (!cardinality_specified()) {
zhanyong.waned6c9272011-02-23 19:39:27 +0000995 set_cardinality(Exactly(static_cast<int>(untyped_actions_.size())));
shiqiane35fdd92008-12-10 05:08:54 +0000996 }
997 return *this;
998 }
999
1000 // Implements the .WillRepeatedly() clause.
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001001 TypedExpectation& WillRepeatedly(const Action<F>& action) {
zhanyong.wanbf550852009-06-09 06:09:53 +00001002 if (last_clause_ == kWillRepeatedly) {
shiqiane35fdd92008-12-10 05:08:54 +00001003 ExpectSpecProperty(false,
1004 ".WillRepeatedly() cannot appear "
1005 "more than once in an EXPECT_CALL().");
1006 } else {
zhanyong.wanbf550852009-06-09 06:09:53 +00001007 ExpectSpecProperty(last_clause_ < kWillRepeatedly,
shiqiane35fdd92008-12-10 05:08:54 +00001008 ".WillRepeatedly() cannot appear "
1009 "after .RetiresOnSaturation().");
1010 }
zhanyong.wanbf550852009-06-09 06:09:53 +00001011 last_clause_ = kWillRepeatedly;
shiqiane35fdd92008-12-10 05:08:54 +00001012 repeated_action_specified_ = true;
1013
1014 repeated_action_ = action;
1015 if (!cardinality_specified()) {
zhanyong.waned6c9272011-02-23 19:39:27 +00001016 set_cardinality(AtLeast(static_cast<int>(untyped_actions_.size())));
shiqiane35fdd92008-12-10 05:08:54 +00001017 }
1018
1019 // Now that no more action clauses can be specified, we check
1020 // whether their count makes sense.
1021 CheckActionCountIfNotDone();
1022 return *this;
1023 }
1024
1025 // Implements the .RetiresOnSaturation() clause.
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001026 TypedExpectation& RetiresOnSaturation() {
zhanyong.wanbf550852009-06-09 06:09:53 +00001027 ExpectSpecProperty(last_clause_ < kRetiresOnSaturation,
shiqiane35fdd92008-12-10 05:08:54 +00001028 ".RetiresOnSaturation() cannot appear "
1029 "more than once.");
zhanyong.wanbf550852009-06-09 06:09:53 +00001030 last_clause_ = kRetiresOnSaturation;
shiqiane35fdd92008-12-10 05:08:54 +00001031 retires_on_saturation_ = true;
1032
1033 // Now that no more action clauses can be specified, we check
1034 // whether their count makes sense.
1035 CheckActionCountIfNotDone();
1036 return *this;
1037 }
1038
1039 // Returns the matchers for the arguments as specified inside the
1040 // EXPECT_CALL() macro.
1041 const ArgumentMatcherTuple& matchers() const {
1042 return matchers_;
1043 }
1044
zhanyong.wanbf550852009-06-09 06:09:53 +00001045 // Returns the matcher specified by the .With() clause.
shiqiane35fdd92008-12-10 05:08:54 +00001046 const Matcher<const ArgumentTuple&>& extra_matcher() const {
1047 return extra_matcher_;
1048 }
1049
shiqiane35fdd92008-12-10 05:08:54 +00001050 // Returns the action specified by the .WillRepeatedly() clause.
1051 const Action<F>& repeated_action() const { return repeated_action_; }
1052
zhanyong.waned6c9272011-02-23 19:39:27 +00001053 // If this mock method has an extra matcher (i.e. .With(matcher)),
1054 // describes it to the ostream.
1055 virtual void MaybeDescribeExtraMatcherTo(::std::ostream* os) {
vladlosev6c54a5e2009-10-21 06:15:34 +00001056 if (extra_matcher_specified_) {
1057 *os << " Expected args: ";
1058 extra_matcher_.DescribeTo(os);
1059 *os << "\n";
1060 }
1061 }
1062
shiqiane35fdd92008-12-10 05:08:54 +00001063 private:
1064 template <typename Function>
1065 friend class FunctionMockerBase;
1066
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001067 // Returns an Expectation object that references and co-owns this
1068 // expectation.
1069 virtual Expectation GetHandle() {
1070 return owner_->GetHandleOf(this);
1071 }
1072
shiqiane35fdd92008-12-10 05:08:54 +00001073 // The following methods will be called only after the EXPECT_CALL()
1074 // statement finishes and when the current thread holds
1075 // g_gmock_mutex.
1076
1077 // Returns true iff this expectation matches the given arguments.
vladlosev4d60a592011-10-24 21:16:22 +00001078 bool Matches(const ArgumentTuple& args) const
1079 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
shiqiane35fdd92008-12-10 05:08:54 +00001080 g_gmock_mutex.AssertHeld();
1081 return TupleMatches(matchers_, args) && extra_matcher_.Matches(args);
1082 }
1083
1084 // Returns true iff this expectation should handle the given arguments.
vladlosev4d60a592011-10-24 21:16:22 +00001085 bool ShouldHandleArguments(const ArgumentTuple& args) const
1086 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
shiqiane35fdd92008-12-10 05:08:54 +00001087 g_gmock_mutex.AssertHeld();
1088
1089 // In case the action count wasn't checked when the expectation
1090 // was defined (e.g. if this expectation has no WillRepeatedly()
1091 // or RetiresOnSaturation() clause), we check it when the
1092 // expectation is used for the first time.
1093 CheckActionCountIfNotDone();
1094 return !is_retired() && AllPrerequisitesAreSatisfied() && Matches(args);
1095 }
1096
1097 // Describes the result of matching the arguments against this
1098 // expectation to the given ostream.
vladlosev4d60a592011-10-24 21:16:22 +00001099 void ExplainMatchResultTo(
1100 const ArgumentTuple& args,
1101 ::std::ostream* os) const
1102 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
shiqiane35fdd92008-12-10 05:08:54 +00001103 g_gmock_mutex.AssertHeld();
1104
1105 if (is_retired()) {
1106 *os << " Expected: the expectation is active\n"
1107 << " Actual: it is retired\n";
1108 } else if (!Matches(args)) {
1109 if (!TupleMatches(matchers_, args)) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001110 ExplainMatchFailureTupleTo(matchers_, args, os);
shiqiane35fdd92008-12-10 05:08:54 +00001111 }
zhanyong.wan82113312010-01-08 21:55:40 +00001112 StringMatchResultListener listener;
1113 if (!extra_matcher_.MatchAndExplain(args, &listener)) {
zhanyong.wan2661c682009-06-09 05:42:12 +00001114 *os << " Expected args: ";
shiqiane35fdd92008-12-10 05:08:54 +00001115 extra_matcher_.DescribeTo(os);
zhanyong.wan2661c682009-06-09 05:42:12 +00001116 *os << "\n Actual: don't match";
shiqiane35fdd92008-12-10 05:08:54 +00001117
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001118 internal::PrintIfNotEmpty(listener.str(), os);
shiqiane35fdd92008-12-10 05:08:54 +00001119 *os << "\n";
1120 }
1121 } else if (!AllPrerequisitesAreSatisfied()) {
1122 *os << " Expected: all pre-requisites are satisfied\n"
1123 << " Actual: the following immediate pre-requisites "
1124 << "are not satisfied:\n";
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001125 ExpectationSet unsatisfied_prereqs;
shiqiane35fdd92008-12-10 05:08:54 +00001126 FindUnsatisfiedPrerequisites(&unsatisfied_prereqs);
1127 int i = 0;
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001128 for (ExpectationSet::const_iterator it = unsatisfied_prereqs.begin();
shiqiane35fdd92008-12-10 05:08:54 +00001129 it != unsatisfied_prereqs.end(); ++it) {
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001130 it->expectation_base()->DescribeLocationTo(os);
shiqiane35fdd92008-12-10 05:08:54 +00001131 *os << "pre-requisite #" << i++ << "\n";
1132 }
1133 *os << " (end of pre-requisites)\n";
1134 } else {
1135 // This line is here just for completeness' sake. It will never
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001136 // be executed as currently the ExplainMatchResultTo() function
shiqiane35fdd92008-12-10 05:08:54 +00001137 // is called only when the mock function call does NOT match the
1138 // expectation.
1139 *os << "The call matches the expectation.\n";
1140 }
1141 }
1142
1143 // Returns the action that should be taken for the current invocation.
vladlosev4d60a592011-10-24 21:16:22 +00001144 const Action<F>& GetCurrentAction(
1145 const FunctionMockerBase<F>* mocker,
1146 const ArgumentTuple& args) const
1147 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
shiqiane35fdd92008-12-10 05:08:54 +00001148 g_gmock_mutex.AssertHeld();
1149 const int count = call_count();
1150 Assert(count >= 1, __FILE__, __LINE__,
1151 "call_count() is <= 0 when GetCurrentAction() is "
1152 "called - this should never happen.");
1153
zhanyong.waned6c9272011-02-23 19:39:27 +00001154 const int action_count = static_cast<int>(untyped_actions_.size());
shiqiane35fdd92008-12-10 05:08:54 +00001155 if (action_count > 0 && !repeated_action_specified_ &&
1156 count > action_count) {
1157 // If there is at least one WillOnce() and no WillRepeatedly(),
1158 // we warn the user when the WillOnce() clauses ran out.
1159 ::std::stringstream ss;
1160 DescribeLocationTo(&ss);
vladlosev6c54a5e2009-10-21 06:15:34 +00001161 ss << "Actions ran out in " << source_text() << "...\n"
shiqiane35fdd92008-12-10 05:08:54 +00001162 << "Called " << count << " times, but only "
1163 << action_count << " WillOnce()"
1164 << (action_count == 1 ? " is" : "s are") << " specified - ";
1165 mocker->DescribeDefaultActionTo(args, &ss);
1166 Log(WARNING, ss.str(), 1);
1167 }
1168
zhanyong.waned6c9272011-02-23 19:39:27 +00001169 return count <= action_count ?
1170 *static_cast<const Action<F>*>(untyped_actions_[count - 1]) :
1171 repeated_action();
shiqiane35fdd92008-12-10 05:08:54 +00001172 }
1173
1174 // Given the arguments of a mock function call, if the call will
1175 // over-saturate this expectation, returns the default action;
1176 // otherwise, returns the next action in this expectation. Also
1177 // describes *what* happened to 'what', and explains *why* Google
1178 // Mock does it to 'why'. This method is not const as it calls
zhanyong.waned6c9272011-02-23 19:39:27 +00001179 // IncrementCallCount(). A return value of NULL means the default
1180 // action.
vladlosev4d60a592011-10-24 21:16:22 +00001181 const Action<F>* GetActionForArguments(
1182 const FunctionMockerBase<F>* mocker,
1183 const ArgumentTuple& args,
1184 ::std::ostream* what,
1185 ::std::ostream* why)
1186 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
shiqiane35fdd92008-12-10 05:08:54 +00001187 g_gmock_mutex.AssertHeld();
1188 if (IsSaturated()) {
1189 // We have an excessive call.
1190 IncrementCallCount();
1191 *what << "Mock function called more times than expected - ";
1192 mocker->DescribeDefaultActionTo(args, what);
1193 DescribeCallCountTo(why);
1194
zhanyong.waned6c9272011-02-23 19:39:27 +00001195 // TODO(wan@google.com): allow the user to control whether
1196 // unexpected calls should fail immediately or continue using a
1197 // flag --gmock_unexpected_calls_are_fatal.
1198 return NULL;
shiqiane35fdd92008-12-10 05:08:54 +00001199 }
1200
1201 IncrementCallCount();
1202 RetireAllPreRequisites();
1203
zhanyong.waned6c9272011-02-23 19:39:27 +00001204 if (retires_on_saturation_ && IsSaturated()) {
shiqiane35fdd92008-12-10 05:08:54 +00001205 Retire();
1206 }
1207
1208 // Must be done after IncrementCount()!
vladlosev6c54a5e2009-10-21 06:15:34 +00001209 *what << "Mock function call matches " << source_text() <<"...\n";
zhanyong.waned6c9272011-02-23 19:39:27 +00001210 return &(GetCurrentAction(mocker, args));
shiqiane35fdd92008-12-10 05:08:54 +00001211 }
1212
1213 // All the fields below won't change once the EXPECT_CALL()
1214 // statement finishes.
1215 FunctionMockerBase<F>* const owner_;
1216 ArgumentMatcherTuple matchers_;
1217 Matcher<const ArgumentTuple&> extra_matcher_;
shiqiane35fdd92008-12-10 05:08:54 +00001218 Action<F> repeated_action_;
zhanyong.wan32de5f52009-12-23 00:13:23 +00001219
1220 GTEST_DISALLOW_COPY_AND_ASSIGN_(TypedExpectation);
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001221}; // class TypedExpectation
shiqiane35fdd92008-12-10 05:08:54 +00001222
1223// A MockSpec object is used by ON_CALL() or EXPECT_CALL() for
1224// specifying the default behavior of, or expectation on, a mock
1225// function.
1226
1227// Note: class MockSpec really belongs to the ::testing namespace.
1228// However if we define it in ::testing, MSVC will complain when
1229// classes in ::testing::internal declare it as a friend class
1230// template. To workaround this compiler bug, we define MockSpec in
1231// ::testing::internal and import it into ::testing.
1232
zhanyong.waned6c9272011-02-23 19:39:27 +00001233// Logs a message including file and line number information.
vladlosev587c1b32011-05-20 00:42:22 +00001234GTEST_API_ void LogWithLocation(testing::internal::LogSeverity severity,
1235 const char* file, int line,
1236 const string& message);
zhanyong.waned6c9272011-02-23 19:39:27 +00001237
shiqiane35fdd92008-12-10 05:08:54 +00001238template <typename F>
1239class MockSpec {
1240 public:
1241 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
1242 typedef typename internal::Function<F>::ArgumentMatcherTuple
1243 ArgumentMatcherTuple;
1244
1245 // Constructs a MockSpec object, given the function mocker object
1246 // that the spec is associated with.
1247 explicit MockSpec(internal::FunctionMockerBase<F>* function_mocker)
1248 : function_mocker_(function_mocker) {}
1249
1250 // Adds a new default action spec to the function mocker and returns
1251 // the newly created spec.
zhanyong.waned6c9272011-02-23 19:39:27 +00001252 internal::OnCallSpec<F>& InternalDefaultActionSetAt(
shiqiane35fdd92008-12-10 05:08:54 +00001253 const char* file, int line, const char* obj, const char* call) {
1254 LogWithLocation(internal::INFO, file, line,
1255 string("ON_CALL(") + obj + ", " + call + ") invoked");
zhanyong.waned6c9272011-02-23 19:39:27 +00001256 return function_mocker_->AddNewOnCallSpec(file, line, matchers_);
shiqiane35fdd92008-12-10 05:08:54 +00001257 }
1258
1259 // Adds a new expectation spec to the function mocker and returns
1260 // the newly created spec.
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001261 internal::TypedExpectation<F>& InternalExpectedAt(
shiqiane35fdd92008-12-10 05:08:54 +00001262 const char* file, int line, const char* obj, const char* call) {
vladlosev6c54a5e2009-10-21 06:15:34 +00001263 const string source_text(string("EXPECT_CALL(") + obj + ", " + call + ")");
1264 LogWithLocation(internal::INFO, file, line, source_text + " invoked");
1265 return function_mocker_->AddNewExpectation(
1266 file, line, source_text, matchers_);
shiqiane35fdd92008-12-10 05:08:54 +00001267 }
1268
1269 private:
1270 template <typename Function>
1271 friend class internal::FunctionMocker;
1272
1273 void SetMatchers(const ArgumentMatcherTuple& matchers) {
1274 matchers_ = matchers;
1275 }
1276
shiqiane35fdd92008-12-10 05:08:54 +00001277 // The function mocker that owns this spec.
1278 internal::FunctionMockerBase<F>* const function_mocker_;
1279 // The argument matchers specified in the spec.
1280 ArgumentMatcherTuple matchers_;
zhanyong.wan32de5f52009-12-23 00:13:23 +00001281
1282 GTEST_DISALLOW_ASSIGN_(MockSpec);
shiqiane35fdd92008-12-10 05:08:54 +00001283}; // class MockSpec
1284
1285// MSVC warns about using 'this' in base member initializer list, so
1286// we need to temporarily disable the warning. We have to do it for
1287// the entire class to suppress the warning, even though it's about
1288// the constructor only.
1289
1290#ifdef _MSC_VER
zhanyong.wan658ac0b2011-02-24 07:29:13 +00001291# pragma warning(push) // Saves the current warning state.
1292# pragma warning(disable:4355) // Temporarily disables warning 4355.
shiqiane35fdd92008-12-10 05:08:54 +00001293#endif // _MSV_VER
1294
zhanyong.wan9413f2f2009-05-29 19:50:06 +00001295// C++ treats the void type specially. For example, you cannot define
1296// a void-typed variable or pass a void value to a function.
1297// ActionResultHolder<T> holds a value of type T, where T must be a
1298// copyable type or void (T doesn't need to be default-constructable).
1299// It hides the syntactic difference between void and other types, and
1300// is used to unify the code for invoking both void-returning and
zhanyong.waned6c9272011-02-23 19:39:27 +00001301// non-void-returning mock functions.
1302
1303// Untyped base class for ActionResultHolder<T>.
1304class UntypedActionResultHolderBase {
1305 public:
1306 virtual ~UntypedActionResultHolderBase() {}
1307
1308 // Prints the held value as an action's result to os.
1309 virtual void PrintAsActionResult(::std::ostream* os) const = 0;
1310};
1311
1312// This generic definition is used when T is not void.
zhanyong.wan9413f2f2009-05-29 19:50:06 +00001313template <typename T>
zhanyong.waned6c9272011-02-23 19:39:27 +00001314class ActionResultHolder : public UntypedActionResultHolderBase {
zhanyong.wan9413f2f2009-05-29 19:50:06 +00001315 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00001316 explicit ActionResultHolder(T a_value) : value_(a_value) {}
zhanyong.wan9413f2f2009-05-29 19:50:06 +00001317
1318 // The compiler-generated copy constructor and assignment operator
1319 // are exactly what we need, so we don't need to define them.
1320
zhanyong.waned6c9272011-02-23 19:39:27 +00001321 // Returns the held value and deletes this object.
1322 T GetValueAndDelete() const {
1323 T retval(value_);
1324 delete this;
1325 return retval;
1326 }
zhanyong.wan9413f2f2009-05-29 19:50:06 +00001327
1328 // Prints the held value as an action's result to os.
zhanyong.waned6c9272011-02-23 19:39:27 +00001329 virtual void PrintAsActionResult(::std::ostream* os) const {
zhanyong.wan9413f2f2009-05-29 19:50:06 +00001330 *os << "\n Returns: ";
vladloseve2e8ba42010-05-13 18:16:03 +00001331 // T may be a reference type, so we don't use UniversalPrint().
zhanyong.wan9413f2f2009-05-29 19:50:06 +00001332 UniversalPrinter<T>::Print(value_, os);
1333 }
1334
1335 // Performs the given mock function's default action and returns the
zhanyong.waned6c9272011-02-23 19:39:27 +00001336 // result in a new-ed ActionResultHolder.
1337 template <typename F>
1338 static ActionResultHolder* PerformDefaultAction(
1339 const FunctionMockerBase<F>* func_mocker,
1340 const typename Function<F>::ArgumentTuple& args,
zhanyong.wan9413f2f2009-05-29 19:50:06 +00001341 const string& call_description) {
zhanyong.waned6c9272011-02-23 19:39:27 +00001342 return new ActionResultHolder(
zhanyong.wan9413f2f2009-05-29 19:50:06 +00001343 func_mocker->PerformDefaultAction(args, call_description));
1344 }
1345
zhanyong.waned6c9272011-02-23 19:39:27 +00001346 // Performs the given action and returns the result in a new-ed
zhanyong.wan9413f2f2009-05-29 19:50:06 +00001347 // ActionResultHolder.
zhanyong.waned6c9272011-02-23 19:39:27 +00001348 template <typename F>
1349 static ActionResultHolder*
1350 PerformAction(const Action<F>& action,
1351 const typename Function<F>::ArgumentTuple& args) {
1352 return new ActionResultHolder(action.Perform(args));
zhanyong.wan9413f2f2009-05-29 19:50:06 +00001353 }
1354
1355 private:
1356 T value_;
zhanyong.wan32de5f52009-12-23 00:13:23 +00001357
1358 // T could be a reference type, so = isn't supported.
1359 GTEST_DISALLOW_ASSIGN_(ActionResultHolder);
zhanyong.wan9413f2f2009-05-29 19:50:06 +00001360};
1361
1362// Specialization for T = void.
1363template <>
zhanyong.waned6c9272011-02-23 19:39:27 +00001364class ActionResultHolder<void> : public UntypedActionResultHolderBase {
zhanyong.wan9413f2f2009-05-29 19:50:06 +00001365 public:
zhanyong.waned6c9272011-02-23 19:39:27 +00001366 void GetValueAndDelete() const { delete this; }
zhanyong.wan9413f2f2009-05-29 19:50:06 +00001367
zhanyong.waned6c9272011-02-23 19:39:27 +00001368 virtual void PrintAsActionResult(::std::ostream* /* os */) const {}
1369
1370 // Performs the given mock function's default action and returns NULL;
1371 template <typename F>
1372 static ActionResultHolder* PerformDefaultAction(
1373 const FunctionMockerBase<F>* func_mocker,
1374 const typename Function<F>::ArgumentTuple& args,
zhanyong.wan9413f2f2009-05-29 19:50:06 +00001375 const string& call_description) {
1376 func_mocker->PerformDefaultAction(args, call_description);
zhanyong.waned6c9272011-02-23 19:39:27 +00001377 return NULL;
zhanyong.wan9413f2f2009-05-29 19:50:06 +00001378 }
1379
zhanyong.waned6c9272011-02-23 19:39:27 +00001380 // Performs the given action and returns NULL.
1381 template <typename F>
1382 static ActionResultHolder* PerformAction(
1383 const Action<F>& action,
1384 const typename Function<F>::ArgumentTuple& args) {
zhanyong.wan9413f2f2009-05-29 19:50:06 +00001385 action.Perform(args);
zhanyong.waned6c9272011-02-23 19:39:27 +00001386 return NULL;
zhanyong.wan9413f2f2009-05-29 19:50:06 +00001387 }
1388};
1389
shiqiane35fdd92008-12-10 05:08:54 +00001390// The base of the function mocker class for the given function type.
1391// We put the methods in this class instead of its child to avoid code
1392// bloat.
1393template <typename F>
1394class FunctionMockerBase : public UntypedFunctionMockerBase {
1395 public:
1396 typedef typename Function<F>::Result Result;
1397 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
1398 typedef typename Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple;
1399
zhanyong.waned6c9272011-02-23 19:39:27 +00001400 FunctionMockerBase() : current_spec_(this) {}
shiqiane35fdd92008-12-10 05:08:54 +00001401
1402 // The destructor verifies that all expectations on this mock
1403 // function have been satisfied. If not, it will report Google Test
1404 // non-fatal failures for the violations.
vladlosev4d60a592011-10-24 21:16:22 +00001405 virtual ~FunctionMockerBase()
1406 GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
shiqiane35fdd92008-12-10 05:08:54 +00001407 MutexLock l(&g_gmock_mutex);
1408 VerifyAndClearExpectationsLocked();
1409 Mock::UnregisterLocked(this);
zhanyong.waned6c9272011-02-23 19:39:27 +00001410 ClearDefaultActionsLocked();
shiqiane35fdd92008-12-10 05:08:54 +00001411 }
1412
1413 // Returns the ON_CALL spec that matches this mock function with the
1414 // given arguments; returns NULL if no matching ON_CALL is found.
1415 // L = *
zhanyong.waned6c9272011-02-23 19:39:27 +00001416 const OnCallSpec<F>* FindOnCallSpec(
shiqiane35fdd92008-12-10 05:08:54 +00001417 const ArgumentTuple& args) const {
zhanyong.waned6c9272011-02-23 19:39:27 +00001418 for (UntypedOnCallSpecs::const_reverse_iterator it
1419 = untyped_on_call_specs_.rbegin();
1420 it != untyped_on_call_specs_.rend(); ++it) {
1421 const OnCallSpec<F>* spec = static_cast<const OnCallSpec<F>*>(*it);
1422 if (spec->Matches(args))
1423 return spec;
shiqiane35fdd92008-12-10 05:08:54 +00001424 }
1425
1426 return NULL;
1427 }
1428
zhanyong.wan5b95fa72009-01-27 22:28:45 +00001429 // Performs the default action of this mock function on the given arguments
1430 // and returns the result. Asserts with a helpful call descrption if there is
1431 // no valid return value. This method doesn't depend on the mutable state of
1432 // this object, and thus can be called concurrently without locking.
shiqiane35fdd92008-12-10 05:08:54 +00001433 // L = *
zhanyong.wan5b95fa72009-01-27 22:28:45 +00001434 Result PerformDefaultAction(const ArgumentTuple& args,
1435 const string& call_description) const {
zhanyong.waned6c9272011-02-23 19:39:27 +00001436 const OnCallSpec<F>* const spec =
1437 this->FindOnCallSpec(args);
zhanyong.wan5b95fa72009-01-27 22:28:45 +00001438 if (spec != NULL) {
1439 return spec->GetAction().Perform(args);
1440 }
1441 Assert(DefaultValue<Result>::Exists(), "", -1,
1442 call_description + "\n The mock function has no default action "
1443 "set, and its return type has no default value set.");
1444 return DefaultValue<Result>::Get();
shiqiane35fdd92008-12-10 05:08:54 +00001445 }
1446
zhanyong.waned6c9272011-02-23 19:39:27 +00001447 // Performs the default action with the given arguments and returns
1448 // the action's result. The call description string will be used in
1449 // the error message to describe the call in the case the default
1450 // action fails. The caller is responsible for deleting the result.
1451 // L = *
1452 virtual UntypedActionResultHolderBase* UntypedPerformDefaultAction(
1453 const void* untyped_args, // must point to an ArgumentTuple
1454 const string& call_description) const {
1455 const ArgumentTuple& args =
1456 *static_cast<const ArgumentTuple*>(untyped_args);
1457 return ResultHolder::PerformDefaultAction(this, args, call_description);
shiqiane35fdd92008-12-10 05:08:54 +00001458 }
1459
zhanyong.waned6c9272011-02-23 19:39:27 +00001460 // Performs the given action with the given arguments and returns
1461 // the action's result. The caller is responsible for deleting the
1462 // result.
1463 // L = *
1464 virtual UntypedActionResultHolderBase* UntypedPerformAction(
1465 const void* untyped_action, const void* untyped_args) const {
1466 // Make a copy of the action before performing it, in case the
1467 // action deletes the mock object (and thus deletes itself).
1468 const Action<F> action = *static_cast<const Action<F>*>(untyped_action);
1469 const ArgumentTuple& args =
1470 *static_cast<const ArgumentTuple*>(untyped_args);
1471 return ResultHolder::PerformAction(action, args);
1472 }
shiqiane35fdd92008-12-10 05:08:54 +00001473
zhanyong.waned6c9272011-02-23 19:39:27 +00001474 // Implements UntypedFunctionMockerBase::ClearDefaultActionsLocked():
1475 // clears the ON_CALL()s set on this mock function.
vladlosev4d60a592011-10-24 21:16:22 +00001476 virtual void ClearDefaultActionsLocked()
1477 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
shiqiane35fdd92008-12-10 05:08:54 +00001478 g_gmock_mutex.AssertHeld();
vladlosev9bcb5f92011-10-24 23:41:07 +00001479
1480 // Deleting our default actions may trigger other mock objects to be
1481 // deleted, for example if an action contains a reference counted smart
1482 // pointer to that mock object, and that is the last reference. So if we
1483 // delete our actions within the context of the global mutex we may deadlock
1484 // when this method is called again. Instead, make a copy of the set of
1485 // actions to delete, clear our set within the mutex, and then delete the
1486 // actions outside of the mutex.
1487 UntypedOnCallSpecs specs_to_delete;
1488 untyped_on_call_specs_.swap(specs_to_delete);
1489
1490 g_gmock_mutex.Unlock();
zhanyong.waned6c9272011-02-23 19:39:27 +00001491 for (UntypedOnCallSpecs::const_iterator it =
vladlosev9bcb5f92011-10-24 23:41:07 +00001492 specs_to_delete.begin();
1493 it != specs_to_delete.end(); ++it) {
zhanyong.waned6c9272011-02-23 19:39:27 +00001494 delete static_cast<const OnCallSpec<F>*>(*it);
shiqiane35fdd92008-12-10 05:08:54 +00001495 }
vladlosev9bcb5f92011-10-24 23:41:07 +00001496
1497 // Lock the mutex again, since the caller expects it to be locked when we
1498 // return.
1499 g_gmock_mutex.Lock();
shiqiane35fdd92008-12-10 05:08:54 +00001500 }
zhanyong.wan32de5f52009-12-23 00:13:23 +00001501
shiqiane35fdd92008-12-10 05:08:54 +00001502 protected:
1503 template <typename Function>
1504 friend class MockSpec;
1505
zhanyong.waned6c9272011-02-23 19:39:27 +00001506 typedef ActionResultHolder<Result> ResultHolder;
1507
shiqiane35fdd92008-12-10 05:08:54 +00001508 // Returns the result of invoking this mock function with the given
1509 // arguments. This function can be safely called from multiple
1510 // threads concurrently.
vladlosev4d60a592011-10-24 21:16:22 +00001511 Result InvokeWith(const ArgumentTuple& args)
1512 GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
zhanyong.waned6c9272011-02-23 19:39:27 +00001513 return static_cast<const ResultHolder*>(
1514 this->UntypedInvokeWith(&args))->GetValueAndDelete();
1515 }
shiqiane35fdd92008-12-10 05:08:54 +00001516
1517 // Adds and returns a default action spec for this mock function.
zhanyong.waned6c9272011-02-23 19:39:27 +00001518 OnCallSpec<F>& AddNewOnCallSpec(
shiqiane35fdd92008-12-10 05:08:54 +00001519 const char* file, int line,
vladlosev4d60a592011-10-24 21:16:22 +00001520 const ArgumentMatcherTuple& m)
1521 GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
zhanyong.wandf35a762009-04-22 22:25:31 +00001522 Mock::RegisterUseByOnCallOrExpectCall(MockObject(), file, line);
zhanyong.waned6c9272011-02-23 19:39:27 +00001523 OnCallSpec<F>* const on_call_spec = new OnCallSpec<F>(file, line, m);
1524 untyped_on_call_specs_.push_back(on_call_spec);
1525 return *on_call_spec;
shiqiane35fdd92008-12-10 05:08:54 +00001526 }
1527
1528 // Adds and returns an expectation spec for this mock function.
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001529 TypedExpectation<F>& AddNewExpectation(
vladlosev6c54a5e2009-10-21 06:15:34 +00001530 const char* file,
1531 int line,
1532 const string& source_text,
vladlosev4d60a592011-10-24 21:16:22 +00001533 const ArgumentMatcherTuple& m)
1534 GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
zhanyong.wandf35a762009-04-22 22:25:31 +00001535 Mock::RegisterUseByOnCallOrExpectCall(MockObject(), file, line);
zhanyong.waned6c9272011-02-23 19:39:27 +00001536 TypedExpectation<F>* const expectation =
1537 new TypedExpectation<F>(this, file, line, source_text, m);
1538 const linked_ptr<ExpectationBase> untyped_expectation(expectation);
1539 untyped_expectations_.push_back(untyped_expectation);
shiqiane35fdd92008-12-10 05:08:54 +00001540
1541 // Adds this expectation into the implicit sequence if there is one.
1542 Sequence* const implicit_sequence = g_gmock_implicit_sequence.get();
1543 if (implicit_sequence != NULL) {
zhanyong.waned6c9272011-02-23 19:39:27 +00001544 implicit_sequence->AddExpectation(Expectation(untyped_expectation));
shiqiane35fdd92008-12-10 05:08:54 +00001545 }
1546
1547 return *expectation;
1548 }
1549
1550 // The current spec (either default action spec or expectation spec)
1551 // being described on this function mocker.
1552 MockSpec<F>& current_spec() { return current_spec_; }
zhanyong.wan32de5f52009-12-23 00:13:23 +00001553
shiqiane35fdd92008-12-10 05:08:54 +00001554 private:
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001555 template <typename Func> friend class TypedExpectation;
shiqiane35fdd92008-12-10 05:08:54 +00001556
zhanyong.waned6c9272011-02-23 19:39:27 +00001557 // Some utilities needed for implementing UntypedInvokeWith().
shiqiane35fdd92008-12-10 05:08:54 +00001558
1559 // Describes what default action will be performed for the given
1560 // arguments.
1561 // L = *
1562 void DescribeDefaultActionTo(const ArgumentTuple& args,
1563 ::std::ostream* os) const {
zhanyong.waned6c9272011-02-23 19:39:27 +00001564 const OnCallSpec<F>* const spec = FindOnCallSpec(args);
shiqiane35fdd92008-12-10 05:08:54 +00001565
1566 if (spec == NULL) {
1567 *os << (internal::type_equals<Result, void>::value ?
1568 "returning directly.\n" :
1569 "returning default value.\n");
1570 } else {
1571 *os << "taking default action specified at:\n"
vladloseve5121b52011-02-11 23:50:38 +00001572 << FormatFileLocation(spec->file(), spec->line()) << "\n";
shiqiane35fdd92008-12-10 05:08:54 +00001573 }
1574 }
1575
1576 // Writes a message that the call is uninteresting (i.e. neither
1577 // explicitly expected nor explicitly unexpected) to the given
1578 // ostream.
vladlosev4d60a592011-10-24 21:16:22 +00001579 virtual void UntypedDescribeUninterestingCall(
1580 const void* untyped_args,
1581 ::std::ostream* os) const
1582 GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
zhanyong.waned6c9272011-02-23 19:39:27 +00001583 const ArgumentTuple& args =
1584 *static_cast<const ArgumentTuple*>(untyped_args);
shiqiane35fdd92008-12-10 05:08:54 +00001585 *os << "Uninteresting mock function call - ";
1586 DescribeDefaultActionTo(args, os);
1587 *os << " Function call: " << Name();
vladloseve2e8ba42010-05-13 18:16:03 +00001588 UniversalPrint(args, os);
shiqiane35fdd92008-12-10 05:08:54 +00001589 }
1590
zhanyong.waned6c9272011-02-23 19:39:27 +00001591 // Returns the expectation that matches the given function arguments
1592 // (or NULL is there's no match); when a match is found,
1593 // untyped_action is set to point to the action that should be
1594 // performed (or NULL if the action is "do default"), and
1595 // is_excessive is modified to indicate whether the call exceeds the
1596 // expected number.
1597 //
shiqiane35fdd92008-12-10 05:08:54 +00001598 // Critical section: We must find the matching expectation and the
1599 // corresponding action that needs to be taken in an ATOMIC
1600 // transaction. Otherwise another thread may call this mock
1601 // method in the middle and mess up the state.
1602 //
1603 // However, performing the action has to be left out of the critical
1604 // section. The reason is that we have no control on what the
1605 // action does (it can invoke an arbitrary user function or even a
1606 // mock function) and excessive locking could cause a dead lock.
zhanyong.waned6c9272011-02-23 19:39:27 +00001607 virtual const ExpectationBase* UntypedFindMatchingExpectation(
1608 const void* untyped_args,
1609 const void** untyped_action, bool* is_excessive,
vladlosev4d60a592011-10-24 21:16:22 +00001610 ::std::ostream* what, ::std::ostream* why)
1611 GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
zhanyong.waned6c9272011-02-23 19:39:27 +00001612 const ArgumentTuple& args =
1613 *static_cast<const ArgumentTuple*>(untyped_args);
shiqiane35fdd92008-12-10 05:08:54 +00001614 MutexLock l(&g_gmock_mutex);
zhanyong.waned6c9272011-02-23 19:39:27 +00001615 TypedExpectation<F>* exp = this->FindMatchingExpectationLocked(args);
1616 if (exp == NULL) { // A match wasn't found.
shiqiane35fdd92008-12-10 05:08:54 +00001617 this->FormatUnexpectedCallMessageLocked(args, what, why);
zhanyong.waned6c9272011-02-23 19:39:27 +00001618 return NULL;
shiqiane35fdd92008-12-10 05:08:54 +00001619 }
1620
1621 // This line must be done before calling GetActionForArguments(),
1622 // which will increment the call count for *exp and thus affect
1623 // its saturation status.
zhanyong.waned6c9272011-02-23 19:39:27 +00001624 *is_excessive = exp->IsSaturated();
1625 const Action<F>* action = exp->GetActionForArguments(this, args, what, why);
1626 if (action != NULL && action->IsDoDefault())
1627 action = NULL; // Normalize "do default" to NULL.
1628 *untyped_action = action;
1629 return exp;
1630 }
1631
1632 // Prints the given function arguments to the ostream.
1633 virtual void UntypedPrintArgs(const void* untyped_args,
1634 ::std::ostream* os) const {
1635 const ArgumentTuple& args =
1636 *static_cast<const ArgumentTuple*>(untyped_args);
1637 UniversalPrint(args, os);
shiqiane35fdd92008-12-10 05:08:54 +00001638 }
1639
1640 // Returns the expectation that matches the arguments, or NULL if no
1641 // expectation matches them.
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001642 TypedExpectation<F>* FindMatchingExpectationLocked(
vladlosev4d60a592011-10-24 21:16:22 +00001643 const ArgumentTuple& args) const
1644 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
shiqiane35fdd92008-12-10 05:08:54 +00001645 g_gmock_mutex.AssertHeld();
zhanyong.waned6c9272011-02-23 19:39:27 +00001646 for (typename UntypedExpectations::const_reverse_iterator it =
1647 untyped_expectations_.rbegin();
1648 it != untyped_expectations_.rend(); ++it) {
1649 TypedExpectation<F>* const exp =
1650 static_cast<TypedExpectation<F>*>(it->get());
shiqiane35fdd92008-12-10 05:08:54 +00001651 if (exp->ShouldHandleArguments(args)) {
1652 return exp;
1653 }
1654 }
1655 return NULL;
1656 }
1657
1658 // Returns a message that the arguments don't match any expectation.
vladlosev4d60a592011-10-24 21:16:22 +00001659 void FormatUnexpectedCallMessageLocked(
1660 const ArgumentTuple& args,
1661 ::std::ostream* os,
1662 ::std::ostream* why) const
1663 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
shiqiane35fdd92008-12-10 05:08:54 +00001664 g_gmock_mutex.AssertHeld();
1665 *os << "\nUnexpected mock function call - ";
1666 DescribeDefaultActionTo(args, os);
1667 PrintTriedExpectationsLocked(args, why);
1668 }
1669
1670 // Prints a list of expectations that have been tried against the
1671 // current mock function call.
vladlosev4d60a592011-10-24 21:16:22 +00001672 void PrintTriedExpectationsLocked(
1673 const ArgumentTuple& args,
1674 ::std::ostream* why) const
1675 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
shiqiane35fdd92008-12-10 05:08:54 +00001676 g_gmock_mutex.AssertHeld();
zhanyong.waned6c9272011-02-23 19:39:27 +00001677 const int count = static_cast<int>(untyped_expectations_.size());
shiqiane35fdd92008-12-10 05:08:54 +00001678 *why << "Google Mock tried the following " << count << " "
1679 << (count == 1 ? "expectation, but it didn't match" :
1680 "expectations, but none matched")
1681 << ":\n";
1682 for (int i = 0; i < count; i++) {
zhanyong.waned6c9272011-02-23 19:39:27 +00001683 TypedExpectation<F>* const expectation =
1684 static_cast<TypedExpectation<F>*>(untyped_expectations_[i].get());
shiqiane35fdd92008-12-10 05:08:54 +00001685 *why << "\n";
zhanyong.waned6c9272011-02-23 19:39:27 +00001686 expectation->DescribeLocationTo(why);
shiqiane35fdd92008-12-10 05:08:54 +00001687 if (count > 1) {
vladlosev6c54a5e2009-10-21 06:15:34 +00001688 *why << "tried expectation #" << i << ": ";
shiqiane35fdd92008-12-10 05:08:54 +00001689 }
zhanyong.waned6c9272011-02-23 19:39:27 +00001690 *why << expectation->source_text() << "...\n";
1691 expectation->ExplainMatchResultTo(args, why);
1692 expectation->DescribeCallCountTo(why);
shiqiane35fdd92008-12-10 05:08:54 +00001693 }
1694 }
1695
shiqiane35fdd92008-12-10 05:08:54 +00001696 // The current spec (either default action spec or expectation spec)
1697 // being described on this function mocker.
1698 MockSpec<F> current_spec_;
1699
zhanyong.wan16cf4732009-05-14 20:55:30 +00001700 // There is no generally useful and implementable semantics of
1701 // copying a mock object, so copying a mock is usually a user error.
1702 // Thus we disallow copying function mockers. If the user really
1703 // wants to copy a mock object, he should implement his own copy
1704 // operation, for example:
1705 //
1706 // class MockFoo : public Foo {
1707 // public:
1708 // // Defines a copy constructor explicitly.
1709 // MockFoo(const MockFoo& src) {}
1710 // ...
1711 // };
1712 GTEST_DISALLOW_COPY_AND_ASSIGN_(FunctionMockerBase);
shiqiane35fdd92008-12-10 05:08:54 +00001713}; // class FunctionMockerBase
1714
1715#ifdef _MSC_VER
zhanyong.wan658ac0b2011-02-24 07:29:13 +00001716# pragma warning(pop) // Restores the warning state.
shiqiane35fdd92008-12-10 05:08:54 +00001717#endif // _MSV_VER
1718
1719// Implements methods of FunctionMockerBase.
1720
1721// Verifies that all expectations on this mock function have been
1722// satisfied. Reports one or more Google Test non-fatal failures and
1723// returns false if not.
shiqiane35fdd92008-12-10 05:08:54 +00001724
1725// Reports an uninteresting call (whose description is in msg) in the
1726// manner specified by 'reaction'.
1727void ReportUninterestingCall(CallReaction reaction, const string& msg);
1728
shiqiane35fdd92008-12-10 05:08:54 +00001729} // namespace internal
1730
1731// The style guide prohibits "using" statements in a namespace scope
1732// inside a header file. However, the MockSpec class template is
1733// meant to be defined in the ::testing namespace. The following line
1734// is just a trick for working around a bug in MSVC 8.0, which cannot
1735// handle it if we define MockSpec in ::testing.
1736using internal::MockSpec;
1737
1738// Const(x) is a convenient function for obtaining a const reference
1739// to x. This is useful for setting expectations on an overloaded
1740// const mock method, e.g.
1741//
1742// class MockFoo : public FooInterface {
1743// public:
1744// MOCK_METHOD0(Bar, int());
1745// MOCK_CONST_METHOD0(Bar, int&());
1746// };
1747//
1748// MockFoo foo;
1749// // Expects a call to non-const MockFoo::Bar().
1750// EXPECT_CALL(foo, Bar());
1751// // Expects a call to const MockFoo::Bar().
1752// EXPECT_CALL(Const(foo), Bar());
1753template <typename T>
1754inline const T& Const(const T& x) { return x; }
1755
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001756// Constructs an Expectation object that references and co-owns exp.
1757inline Expectation::Expectation(internal::ExpectationBase& exp) // NOLINT
1758 : expectation_base_(exp.GetHandle().expectation_base()) {}
1759
shiqiane35fdd92008-12-10 05:08:54 +00001760} // namespace testing
1761
1762// A separate macro is required to avoid compile errors when the name
1763// of the method used in call is a result of macro expansion.
1764// See CompilesWithMethodNameExpandedFromMacro tests in
1765// internal/gmock-spec-builders_test.cc for more details.
zhanyong.wane0d051e2009-02-19 00:33:37 +00001766#define GMOCK_ON_CALL_IMPL_(obj, call) \
shiqiane35fdd92008-12-10 05:08:54 +00001767 ((obj).gmock_##call).InternalDefaultActionSetAt(__FILE__, __LINE__, \
1768 #obj, #call)
zhanyong.wane0d051e2009-02-19 00:33:37 +00001769#define ON_CALL(obj, call) GMOCK_ON_CALL_IMPL_(obj, call)
shiqiane35fdd92008-12-10 05:08:54 +00001770
zhanyong.wane0d051e2009-02-19 00:33:37 +00001771#define GMOCK_EXPECT_CALL_IMPL_(obj, call) \
shiqiane35fdd92008-12-10 05:08:54 +00001772 ((obj).gmock_##call).InternalExpectedAt(__FILE__, __LINE__, #obj, #call)
zhanyong.wane0d051e2009-02-19 00:33:37 +00001773#define EXPECT_CALL(obj, call) GMOCK_EXPECT_CALL_IMPL_(obj, call)
shiqiane35fdd92008-12-10 05:08:54 +00001774
1775#endif // GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_