gmock-spec-builders.h
Go to the documentation of this file.
1 // 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))
40 // .With(multi-argument-matcher)
41 // .WillByDefault(action);
42 //
43 // where the .With() clause is optional.
44 //
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))
49 // .With(multi-argument-matchers)
50 // .Times(cardinality)
51 // .InSequence(sequences)
52 // .After(expectations)
53 // .WillOnce(action)
54 // .WillRepeatedly(action)
55 // .RetiresOnSaturation();
56 //
57 // where all clauses are optional, and .InSequence()/.After()/
58 // .WillOnce() can appear any number of times.
59 
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 
69 #if GTEST_HAS_EXCEPTIONS
70 # include <stdexcept> // NOLINT
71 #endif
72 
73 #include "gmock/gmock-actions.h"
75 #include "gmock/gmock-matchers.h"
78 #include "gtest/gtest.h"
79 
80 namespace testing
81 {
82 
83 // An abstract handle of an expectation.
84 class Expectation;
85 
86 // A set of expectation handles.
87 class ExpectationSet;
88 
89 // Anything inside the 'internal' namespace IS INTERNAL IMPLEMENTATION
90 // and MUST NOT BE USED IN USER CODE!!!
91 namespace internal
92 {
93 
94 // Implements a mock function.
95 template <typename F> class FunctionMocker;
96 
97 // Base class for expectations.
98 class ExpectationBase;
99 
100 // Implements an expectation.
101 template <typename F> class TypedExpectation;
102 
103 // Helper class for testing the Expectation class template.
104 class ExpectationTester;
105 
106 // Base class for function mockers.
107 template <typename F> class FunctionMockerBase;
108 
109 // Protects the mock object registry (in class Mock), all function
110 // mockers, and all expectations.
111 //
112 // The reason we don't use more fine-grained protection is: when a
113 // mock function Foo() is called, it needs to consult its expectations
114 // to see which one should be picked. If another thread is allowed to
115 // call a mock function (either Foo() or a different one) at the same
116 // time, it could affect the "retired" attributes of Foo()'s
117 // expectations when InSequence() is used, and thus affect which
118 // expectation gets picked. Therefore, we sequence all mock function
119 // calls to ensure the integrity of the mock objects' states.
121 
122 // Untyped base class for ActionResultHolder<R>.
123 class UntypedActionResultHolderBase;
124 
125 // Abstract base class of FunctionMockerBase. This is the
126 // type-agnostic part of the function mocker interface. Its pure
127 // virtual methods are implemented by FunctionMockerBase.
128 class GTEST_API_ UntypedFunctionMockerBase
129 {
130 public:
131  UntypedFunctionMockerBase();
132  virtual ~UntypedFunctionMockerBase();
133 
134  // Verifies that all expectations on this mock function have been
135  // satisfied. Reports one or more Google Test non-fatal failures
136  // and returns false if not.
137  bool VerifyAndClearExpectationsLocked()
138  GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex);
139 
140  // Clears the ON_CALL()s set on this mock function.
141  virtual void ClearDefaultActionsLocked()
142  GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) = 0;
143 
144  // In all of the following Untyped* functions, it's the caller's
145  // responsibility to guarantee the correctness of the arguments'
146  // types.
147 
148  // Performs the default action with the given arguments and returns
149  // the action's result. The call description string will be used in
150  // the error message to describe the call in the case the default
151  // action fails.
152  // L = *
153  virtual UntypedActionResultHolderBase * UntypedPerformDefaultAction(
154  const void * untyped_args,
155  const string & call_description) const = 0;
156 
157  // Performs the given action with the given arguments and returns
158  // the action's result.
159  // L = *
160  virtual UntypedActionResultHolderBase * UntypedPerformAction(
161  const void * untyped_action,
162  const void * untyped_args) const = 0;
163 
164  // Writes a message that the call is uninteresting (i.e. neither
165  // explicitly expected nor explicitly unexpected) to the given
166  // ostream.
167  virtual void UntypedDescribeUninterestingCall(
168  const void * untyped_args,
169  ::std::ostream * os) const
170  GTEST_LOCK_EXCLUDED_(g_gmock_mutex) = 0;
171 
172  // Returns the expectation that matches the given function arguments
173  // (or NULL is there's no match); when a match is found,
174  // untyped_action is set to point to the action that should be
175  // performed (or NULL if the action is "do default"), and
176  // is_excessive is modified to indicate whether the call exceeds the
177  // expected number.
178  virtual const ExpectationBase * UntypedFindMatchingExpectation(
179  const void * untyped_args,
180  const void ** untyped_action, bool * is_excessive,
181  ::std::ostream * what, ::std::ostream * why)
182  GTEST_LOCK_EXCLUDED_(g_gmock_mutex) = 0;
183 
184  // Prints the given function arguments to the ostream.
185  virtual void UntypedPrintArgs(const void * untyped_args,
186  ::std::ostream * os) const = 0;
187 
188  // Sets the mock object this mock method belongs to, and registers
189  // this information in the global mock registry. Will be called
190  // whenever an EXPECT_CALL() or ON_CALL() is executed on this mock
191  // method.
192  // TODO(wan@google.com): rename to SetAndRegisterOwner().
193  void RegisterOwner(const void * mock_obj)
194  GTEST_LOCK_EXCLUDED_(g_gmock_mutex);
195 
196  // Sets the mock object this mock method belongs to, and sets the
197  // name of the mock function. Will be called upon each invocation
198  // of this mock function.
199  void SetOwnerAndName(const void * mock_obj, const char * name)
200  GTEST_LOCK_EXCLUDED_(g_gmock_mutex);
201 
202  // Returns the mock object this mock method belongs to. Must be
203  // called after RegisterOwner() or SetOwnerAndName() has been
204  // called.
205  const void * MockObject() const
206  GTEST_LOCK_EXCLUDED_(g_gmock_mutex);
207 
208  // Returns the name of this mock method. Must be called after
209  // SetOwnerAndName() has been called.
210  const char * Name() const
211  GTEST_LOCK_EXCLUDED_(g_gmock_mutex);
212 
213  // Returns the result of invoking this mock function with the given
214  // arguments. This function can be safely called from multiple
215  // threads concurrently. The caller is responsible for deleting the
216  // result.
217  const UntypedActionResultHolderBase * UntypedInvokeWith(
218  const void * untyped_args)
219  GTEST_LOCK_EXCLUDED_(g_gmock_mutex);
220 
221 protected:
222  typedef std::vector<const void *> UntypedOnCallSpecs;
223 
224  typedef std::vector<internal::linked_ptr<ExpectationBase> >
226 
227  // Returns an Expectation object that references and co-owns exp,
228  // which must be an expectation on this mock function.
229  Expectation GetHandleOf(ExpectationBase * exp);
230 
231  // Address of the mock object this mock method belongs to. Only
232  // valid after this mock method has been called or
233  // ON_CALL/EXPECT_CALL has been invoked on it.
234  const void * mock_obj_; // Protected by g_gmock_mutex.
235 
236  // Name of the function being mocked. Only valid after this mock
237  // method has been called.
238  const char * name_; // Protected by g_gmock_mutex.
239 
240  // All default action specs for this function mocker.
241  UntypedOnCallSpecs untyped_on_call_specs_;
242 
243  // All expectations for this function mocker.
244  UntypedExpectations untyped_expectations_;
245 }; // class UntypedFunctionMockerBase
246 
247 // Untyped base class for OnCallSpec<F>.
249 {
250 public:
251  // The arguments are the location of the ON_CALL() statement.
252  UntypedOnCallSpecBase(const char * a_file, int a_line)
253  : file_(a_file), line_(a_line), last_clause_(kNone) {}
254 
255  // Where in the source file was the default action spec defined?
256  const char * file() const { return file_; }
257  int line() const { return line_; }
258 
259 protected:
260  // Gives each clause in the ON_CALL() statement a name.
261  enum Clause
262  {
263  // Do not change the order of the enum members! The run-time
264  // syntax checking relies on it.
265  kNone,
266  kWith,
267  kWillByDefault
268  };
269 
270  // Asserts that the ON_CALL() statement has a certain property.
271  void AssertSpecProperty(bool property, const string & failure_message) const
272  {
273  Assert(property, file_, line_, failure_message);
274  }
275 
276  // Expects that the ON_CALL() statement has a certain property.
277  void ExpectSpecProperty(bool property, const string & failure_message) const
278  {
279  Expect(property, file_, line_, failure_message);
280  }
281 
282  const char * file_;
283  int line_;
284 
285  // The last clause in the ON_CALL() statement as seen so far.
286  // Initially kNone and changes as the statement is parsed.
287  Clause last_clause_;
288 }; // class UntypedOnCallSpecBase
289 
290 // This template class implements an ON_CALL spec.
291 template <typename F>
292 class OnCallSpec : public UntypedOnCallSpecBase
293 {
294 public:
297 
298  // Constructs an OnCallSpec object from the information inside
299  // the parenthesis of an ON_CALL() statement.
300  OnCallSpec(const char * a_file, int a_line,
301  const ArgumentMatcherTuple & matchers)
302  : UntypedOnCallSpecBase(a_file, a_line),
303  matchers_(matchers),
304  // By default, extra_matcher_ should match anything. However,
305  // we cannot initialize it with _ as that triggers a compiler
306  // bug in Symbian's C++ compiler (cannot decide between two
307  // overloaded constructors of Matcher<const ArgumentTuple&>).
308  extra_matcher_(A<const ArgumentTuple & >())
309  {
310  }
311 
312  // Implements the .With() clause.
314  {
315  // Makes sure this is called at most once.
316  ExpectSpecProperty(last_clause_ < kWith,
317  ".With() cannot appear "
318  "more than once in an ON_CALL().");
319  last_clause_ = kWith;
320 
321  extra_matcher_ = m;
322  return *this;
323  }
324 
325  // Implements the .WillByDefault() clause.
327  {
328  ExpectSpecProperty(last_clause_ < kWillByDefault,
329  ".WillByDefault() must appear "
330  "exactly once in an ON_CALL().");
331  last_clause_ = kWillByDefault;
332 
333  ExpectSpecProperty(!action.IsDoDefault(),
334  "DoDefault() cannot be used in ON_CALL().");
335  action_ = action;
336  return *this;
337  }
338 
339  // Returns true iff the given arguments match the matchers.
340  bool Matches(const ArgumentTuple & args) const
341  {
342  return TupleMatches(matchers_, args) && extra_matcher_.Matches(args);
343  }
344 
345  // Returns the action specified by the user.
346  const Action<F> & GetAction() const
347  {
348  AssertSpecProperty(last_clause_ == kWillByDefault,
349  ".WillByDefault() must appear exactly "
350  "once in an ON_CALL().");
351  return action_;
352  }
353 
354 private:
355  // The information in statement
356  //
357  // ON_CALL(mock_object, Method(matchers))
358  // .With(multi-argument-matcher)
359  // .WillByDefault(action);
360  //
361  // is recorded in the data members like this:
362  //
363  // source file that contains the statement => file_
364  // line number of the statement => line_
365  // matchers => matchers_
366  // multi-argument-matcher => extra_matcher_
367  // action => action_
368  ArgumentMatcherTuple matchers_;
369  Matcher<const ArgumentTuple &> extra_matcher_;
370  Action<F> action_;
371 }; // class OnCallSpec
372 
373 // Possible reactions on uninteresting calls.
375 {
376  kAllow,
377  kWarn,
378  kFail,
379  kDefault = kWarn // By default, warn about uninteresting calls.
380 };
381 
382 } // namespace internal
383 
384 // Utilities for manipulating mock objects.
385 class GTEST_API_ Mock
386 {
387 public:
388  // The following public methods can be called concurrently.
389 
390  // Tells Google Mock to ignore mock_obj when checking for leaked
391  // mock objects.
392  static void AllowLeak(const void * mock_obj)
393  GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
394 
395  // Verifies and clears all expectations on the given mock object.
396  // If the expectations aren't satisfied, generates one or more
397  // Google Test non-fatal failures and returns false.
398  static bool VerifyAndClearExpectations(void * mock_obj)
399  GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
400 
401  // Verifies all expectations on the given mock object and clears its
402  // default actions and expectations. Returns true iff the
403  // verification was successful.
404  static bool VerifyAndClear(void * mock_obj)
405  GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
406 
407 private:
409 
410  // Needed for a function mocker to register itself (so that we know
411  // how to clear a mock object).
412  template <typename F>
414 
415  template <typename M>
416  friend class NiceMock;
417 
418  template <typename M>
419  friend class NaggyMock;
420 
421  template <typename M>
422  friend class StrictMock;
423 
424  // Tells Google Mock to allow uninteresting calls on the given mock
425  // object.
426  static void AllowUninterestingCalls(const void * mock_obj)
427  GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
428 
429  // Tells Google Mock to warn the user about uninteresting calls on
430  // the given mock object.
431  static void WarnUninterestingCalls(const void * mock_obj)
432  GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
433 
434  // Tells Google Mock to fail uninteresting calls on the given mock
435  // object.
436  static void FailUninterestingCalls(const void * mock_obj)
437  GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
438 
439  // Tells Google Mock the given mock object is being destroyed and
440  // its entry in the call-reaction table should be removed.
441  static void UnregisterCallReaction(const void * mock_obj)
442  GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
443 
444  // Returns the reaction Google Mock will have on uninteresting calls
445  // made on the given mock object.
446  static internal::CallReaction GetReactionOnUninterestingCalls(
447  const void * mock_obj)
448  GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
449 
450  // Verifies that all expectations on the given mock object have been
451  // satisfied. Reports one or more Google Test non-fatal failures
452  // and returns false if not.
453  static bool VerifyAndClearExpectationsLocked(void * mock_obj)
454  GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex);
455 
456  // Clears all ON_CALL()s set on the given mock object.
457  static void ClearDefaultActionsLocked(void * mock_obj)
458  GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex);
459 
460  // Registers a mock object and a mock method it owns.
461  static void Register(
462  const void * mock_obj,
464  GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
465 
466  // Tells Google Mock where in the source code mock_obj is used in an
467  // ON_CALL or EXPECT_CALL. In case mock_obj is leaked, this
468  // information helps the user identify which object it is.
469  static void RegisterUseByOnCallOrExpectCall(
470  const void * mock_obj, const char * file, int line)
471  GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
472 
473  // Unregisters a mock method; removes the owning mock object from
474  // the registry when the last mock method associated with it has
475  // been unregistered. This is called only in the destructor of
476  // FunctionMockerBase.
477  static void UnregisterLocked(internal::UntypedFunctionMockerBase * mocker)
478  GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex);
479 }; // class Mock
480 
481 // An abstract handle of an expectation. Useful in the .After()
482 // clause of EXPECT_CALL() for setting the (partial) order of
483 // expectations. The syntax:
484 //
485 // Expectation e1 = EXPECT_CALL(...)...;
486 // EXPECT_CALL(...).After(e1)...;
487 //
488 // sets two expectations where the latter can only be matched after
489 // the former has been satisfied.
490 //
491 // Notes:
492 // - This class is copyable and has value semantics.
493 // - Constness is shallow: a const Expectation object itself cannot
494 // be modified, but the mutable methods of the ExpectationBase
495 // object it references can be called via expectation_base().
496 // - The constructors and destructor are defined out-of-line because
497 // the Symbian WINSCW compiler wants to otherwise instantiate them
498 // when it sees this class definition, at which point it doesn't have
499 // ExpectationBase available yet, leading to incorrect destruction
500 // in the linked_ptr (or compilation errors if using a checking
501 // linked_ptr).
503 {
504 public:
505  // Constructs a null object that doesn't reference any expectation.
506  Expectation();
507 
508  ~Expectation();
509 
510  // This single-argument ctor must not be explicit, in order to support the
511  // Expectation e = EXPECT_CALL(...);
512  // syntax.
513  //
514  // A TypedExpectation object stores its pre-requisites as
515  // Expectation objects, and needs to call the non-const Retire()
516  // method on the ExpectationBase objects they reference. Therefore
517  // Expectation must receive a *non-const* reference to the
518  // ExpectationBase object.
519  Expectation(internal::ExpectationBase & exp); // NOLINT
520 
521  // The compiler-generated copy ctor and operator= work exactly as
522  // intended, so we don't need to define our own.
523 
524  // Returns true iff rhs references the same expectation as this object does.
525  bool operator==(const Expectation & rhs) const
526  {
527  return expectation_base_ == rhs.expectation_base_;
528  }
529 
530  bool operator!=(const Expectation & rhs) const { return !(*this == rhs); }
531 
532 private:
533  friend class ExpectationSet;
534  friend class Sequence;
535  friend class ::testing::internal::ExpectationBase;
536  friend class ::testing::internal::UntypedFunctionMockerBase;
537 
538  template <typename F>
539  friend class ::testing::internal::FunctionMockerBase;
540 
541  template <typename F>
542  friend class ::testing::internal::TypedExpectation;
543 
544  // This comparator is needed for putting Expectation objects into a set.
545  class Less
546  {
547  public:
548  bool operator()(const Expectation & lhs, const Expectation & rhs) const
549  {
550  return lhs.expectation_base_.get() < rhs.expectation_base_.get();
551  }
552  };
553 
554  typedef ::std::set<Expectation, Less> Set;
555 
556  Expectation(
557  const internal::linked_ptr<internal::ExpectationBase> & expectation_base);
558 
559  // Returns the expectation this object references.
562  {
563  return expectation_base_;
564  }
565 
566  // A linked_ptr that co-owns the expectation this handle references.
568 };
569 
570 // A set of expectation handles. Useful in the .After() clause of
571 // EXPECT_CALL() for setting the (partial) order of expectations. The
572 // syntax:
573 //
574 // ExpectationSet es;
575 // es += EXPECT_CALL(...)...;
576 // es += EXPECT_CALL(...)...;
577 // EXPECT_CALL(...).After(es)...;
578 //
579 // sets three expectations where the last one can only be matched
580 // after the first two have both been satisfied.
581 //
582 // This class is copyable and has value semantics.
583 class ExpectationSet
584 {
585 public:
586  // A bidirectional iterator that can read a const element in the set.
587  typedef Expectation::Set::const_iterator const_iterator;
588 
589  // An object stored in the set. This is an alias of Expectation.
590  typedef Expectation::Set::value_type value_type;
591 
592  // Constructs an empty set.
594 
595  // This single-argument ctor must not be explicit, in order to support the
596  // ExpectationSet es = EXPECT_CALL(...);
597  // syntax.
599  {
600  *this += Expectation(exp);
601  }
602 
603  // This single-argument ctor implements implicit conversion from
604  // Expectation and thus must not be explicit. This allows either an
605  // Expectation or an ExpectationSet to be used in .After().
606  ExpectationSet(const Expectation & e) // NOLINT
607  {
608  *this += e;
609  }
610 
611  // The compiler-generator ctor and operator= works exactly as
612  // intended, so we don't need to define our own.
613 
614  // Returns true iff rhs contains the same set of Expectation objects
615  // as this does.
616  bool operator==(const ExpectationSet & rhs) const
617  {
618  return expectations_ == rhs.expectations_;
619  }
620 
621  bool operator!=(const ExpectationSet & rhs) const { return !(*this == rhs); }
622 
623  // Implements the syntax
624  // expectation_set += EXPECT_CALL(...);
626  {
627  expectations_.insert(e);
628  return *this;
629  }
630 
631  int size() const { return static_cast<int>(expectations_.size()); }
632 
633  const_iterator begin() const { return expectations_.begin(); }
634  const_iterator end() const { return expectations_.end(); }
635 
636 private:
637  Expectation::Set expectations_;
638 };
639 
640 
641 // Sequence objects are used by a user to specify the relative order
642 // in which the expectations should match. They are copyable (we rely
643 // on the compiler-defined copy constructor and assignment operator).
644 class GTEST_API_ Sequence
645 {
646 public:
647  // Constructs an empty sequence.
648  Sequence() : last_expectation_(new Expectation) {}
649 
650  // Adds an expectation to this sequence. The caller must ensure
651  // that no other thread is accessing this Sequence object.
652  void AddExpectation(const Expectation & expectation) const;
653 
654 private:
655  // The last expectation in this sequence. We use a linked_ptr here
656  // because Sequence objects are copyable and we want the copies to
657  // be aliases. The linked_ptr allows the copies to co-own and share
658  // the same Expectation object.
659  internal::linked_ptr<Expectation> last_expectation_;
660 }; // class Sequence
661 
662 // An object of this type causes all EXPECT_CALL() statements
663 // encountered in its scope to be put in an anonymous sequence. The
664 // work is done in the constructor and destructor. You should only
665 // create an InSequence object on the stack.
666 //
667 // The sole purpose for this class is to support easy definition of
668 // sequential expectations, e.g.
669 //
670 // {
671 // InSequence dummy; // The name of the object doesn't matter.
672 //
673 // // The following expectations must match in the order they appear.
674 // EXPECT_CALL(a, Bar())...;
675 // EXPECT_CALL(a, Baz())...;
676 // ...
677 // EXPECT_CALL(b, Xyz())...;
678 // }
679 //
680 // You can create InSequence objects in multiple threads, as long as
681 // they are used to affect different mock objects. The idea is that
682 // each thread can create and set up its own mocks as if it's the only
683 // thread. However, for clarity of your tests we recommend you to set
684 // up mocks in the main thread unless you have a good reason not to do
685 // so.
686 class GTEST_API_ InSequence
687 {
688 public:
689  InSequence();
690  ~InSequence();
691 private:
692  bool sequence_created_;
693 
694  GTEST_DISALLOW_COPY_AND_ASSIGN_(InSequence); // NOLINT
696 
697 namespace internal
698 {
699 
700 // Points to the implicit sequence introduced by a living InSequence
701 // object (if any) in the current thread or NULL.
702 GTEST_API_ extern ThreadLocal<Sequence *> g_gmock_implicit_sequence;
703 
704 // Base class for implementing expectations.
705 //
706 // There are two reasons for having a type-agnostic base class for
707 // Expectation:
708 //
709 // 1. We need to store collections of expectations of different
710 // types (e.g. all pre-requisites of a particular expectation, all
711 // expectations in a sequence). Therefore these expectation objects
712 // must share a common base class.
713 //
714 // 2. We can avoid binary code bloat by moving methods not depending
715 // on the template argument of Expectation to the base class.
716 //
717 // This class is internal and mustn't be used by user code directly.
718 class GTEST_API_ ExpectationBase
719 {
720 public:
721  // source_text is the EXPECT_CALL(...) source that created this Expectation.
722  ExpectationBase(const char * file, int line, const string & source_text);
723 
724  virtual ~ExpectationBase();
725 
726  // Where in the source file was the expectation spec defined?
727  const char * file() const { return file_; }
728  int line() const { return line_; }
729  const char * source_text() const { return source_text_.c_str(); }
730  // Returns the cardinality specified in the expectation spec.
731  const Cardinality & cardinality() const { return cardinality_; }
732 
733  // Describes the source file location of this expectation.
734  void DescribeLocationTo(::std::ostream * os) const
735  {
736  *os << FormatFileLocation(file(), line()) << " ";
737  }
738 
739  // Describes how many times a function call matching this
740  // expectation has occurred.
741  void DescribeCallCountTo(::std::ostream * os) const
742  GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex);
743 
744  // If this mock method has an extra matcher (i.e. .With(matcher)),
745  // describes it to the ostream.
746  virtual void MaybeDescribeExtraMatcherTo(::std::ostream * os) = 0;
747 
748 protected:
749  friend class ::testing::Expectation;
750  friend class UntypedFunctionMockerBase;
751 
752  enum Clause
753  {
754  // Don't change the order of the enum members!
755  kNone,
756  kWith,
757  kTimes,
758  kInSequence,
759  kAfter,
760  kWillOnce,
761  kWillRepeatedly,
762  kRetiresOnSaturation
763  };
764 
765  typedef std::vector<const void *> UntypedActions;
766 
767  // Returns an Expectation object that references and co-owns this
768  // expectation.
769  virtual Expectation GetHandle() = 0;
770 
771  // Asserts that the EXPECT_CALL() statement has the given property.
772  void AssertSpecProperty(bool property, const string & failure_message) const
773  {
774  Assert(property, file_, line_, failure_message);
775  }
776 
777  // Expects that the EXPECT_CALL() statement has the given property.
778  void ExpectSpecProperty(bool property, const string & failure_message) const
779  {
780  Expect(property, file_, line_, failure_message);
781  }
782 
783  // Explicitly specifies the cardinality of this expectation. Used
784  // by the subclasses to implement the .Times() clause.
785  void SpecifyCardinality(const Cardinality & cardinality);
786 
787  // Returns true iff the user specified the cardinality explicitly
788  // using a .Times().
789  bool cardinality_specified() const { return cardinality_specified_; }
790 
791  // Sets the cardinality of this expectation spec.
792  void set_cardinality(const Cardinality & a_cardinality)
793  {
794  cardinality_ = a_cardinality;
795  }
796 
797  // The following group of methods should only be called after the
798  // EXPECT_CALL() statement, and only when g_gmock_mutex is held by
799  // the current thread.
800 
801  // Retires all pre-requisites of this expectation.
802  void RetireAllPreRequisites()
803  GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex);
804 
805  // Returns true iff this expectation is retired.
806  bool is_retired() const
807  GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
808  {
809  g_gmock_mutex.AssertHeld();
810  return retired_;
811  }
812 
813  // Retires this expectation.
814  void Retire()
815  GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
816  {
817  g_gmock_mutex.AssertHeld();
818  retired_ = true;
819  }
820 
821  // Returns true iff this expectation is satisfied.
822  bool IsSatisfied() const
823  GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
824  {
825  g_gmock_mutex.AssertHeld();
826  return cardinality().IsSatisfiedByCallCount(call_count_);
827  }
828 
829  // Returns true iff this expectation is saturated.
830  bool IsSaturated() const
831  GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
832  {
833  g_gmock_mutex.AssertHeld();
834  return cardinality().IsSaturatedByCallCount(call_count_);
835  }
836 
837  // Returns true iff this expectation is over-saturated.
838  bool IsOverSaturated() const
839  GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
840  {
841  g_gmock_mutex.AssertHeld();
842  return cardinality().IsOverSaturatedByCallCount(call_count_);
843  }
844 
845  // Returns true iff all pre-requisites of this expectation are satisfied.
846  bool AllPrerequisitesAreSatisfied() const
847  GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex);
848 
849  // Adds unsatisfied pre-requisites of this expectation to 'result'.
850  void FindUnsatisfiedPrerequisites(ExpectationSet * result) const
851  GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex);
852 
853  // Returns the number this expectation has been invoked.
854  int call_count() const
855  GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
856  {
857  g_gmock_mutex.AssertHeld();
858  return call_count_;
859  }
860 
861  // Increments the number this expectation has been invoked.
863  GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
864  {
865  g_gmock_mutex.AssertHeld();
866  call_count_++;
867  }
868 
869  // Checks the action count (i.e. the number of WillOnce() and
870  // WillRepeatedly() clauses) against the cardinality if this hasn't
871  // been done before. Prints a warning if there are too many or too
872  // few actions.
873  void CheckActionCountIfNotDone() const
874  GTEST_LOCK_EXCLUDED_(mutex_);
875 
876  friend class ::testing::Sequence;
877  friend class ::testing::internal::ExpectationTester;
878 
879  template <typename Function>
880  friend class TypedExpectation;
881 
882  // Implements the .Times() clause.
883  void UntypedTimes(const Cardinality & a_cardinality);
884 
885  // This group of fields are part of the spec and won't change after
886  // an EXPECT_CALL() statement finishes.
887  const char * file_; // The file that contains the expectation.
888  int line_; // The line number of the expectation.
889  const string source_text_; // The EXPECT_CALL(...) source text.
890  // True iff the cardinality is specified explicitly.
891  bool cardinality_specified_;
892  Cardinality cardinality_; // The cardinality of the expectation.
893  // The immediate pre-requisites (i.e. expectations that must be
894  // satisfied before this expectation can be matched) of this
895  // expectation. We use linked_ptr in the set because we want an
896  // Expectation object to be co-owned by its FunctionMocker and its
897  // successors. This allows multiple mock objects to be deleted at
898  // different times.
899  ExpectationSet immediate_prerequisites_;
900 
901  // This group of fields are the current state of the expectation,
902  // and can change as the mock function is called.
903  int call_count_; // How many times this expectation has been invoked.
904  bool retired_; // True iff this expectation has retired.
905  UntypedActions untyped_actions_;
906  bool extra_matcher_specified_;
907  bool repeated_action_specified_; // True if a WillRepeatedly() was specified.
908  bool retires_on_saturation_;
909  Clause last_clause_;
910  mutable bool action_count_checked_; // Under mutex_.
911  mutable Mutex mutex_; // Protects action_count_checked_.
912 
913  GTEST_DISALLOW_ASSIGN_(ExpectationBase);
914 }; // class ExpectationBase
915 
916 // Impements an expectation for the given function type.
917 template <typename F>
918 class TypedExpectation : public ExpectationBase
919 {
920 public:
923  typedef typename Function<F>::Result Result;
924 
926  const char * a_file, int a_line, const string & a_source_text,
927  const ArgumentMatcherTuple & m)
928  : ExpectationBase(a_file, a_line, a_source_text),
929  owner_(owner),
930  matchers_(m),
931  // By default, extra_matcher_ should match anything. However,
932  // we cannot initialize it with _ as that triggers a compiler
933  // bug in Symbian's C++ compiler (cannot decide between two
934  // overloaded constructors of Matcher<const ArgumentTuple&>).
935  extra_matcher_(A<const ArgumentTuple & >()),
936  repeated_action_(DoDefault()) {}
937 
939  {
940  // Check the validity of the action count if it hasn't been done
941  // yet (for example, if the expectation was never used).
942  CheckActionCountIfNotDone();
943 
944  for (UntypedActions::const_iterator it = untyped_actions_.begin();
945  it != untyped_actions_.end(); ++it)
946  {
947  delete static_cast<const Action<F>*>(*it);
948  }
949  }
950 
951  // Implements the .With() clause.
953  {
954  if (last_clause_ == kWith)
955  {
956  ExpectSpecProperty(false,
957  ".With() cannot appear "
958  "more than once in an EXPECT_CALL().");
959  }
960 
961  else
962  {
963  ExpectSpecProperty(last_clause_ < kWith,
964  ".With() must be the first "
965  "clause in an EXPECT_CALL().");
966  }
967 
968  last_clause_ = kWith;
969 
970  extra_matcher_ = m;
971  extra_matcher_specified_ = true;
972  return *this;
973  }
974 
975  // Implements the .Times() clause.
976  TypedExpectation & Times(const Cardinality & a_cardinality)
977  {
978  ExpectationBase::UntypedTimes(a_cardinality);
979  return *this;
980  }
981 
982  // Implements the .Times() clause.
984  {
985  return Times(Exactly(n));
986  }
987 
988  // Implements the .InSequence() clause.
989  TypedExpectation & InSequence(const Sequence & s)
990  {
991  ExpectSpecProperty(last_clause_ <= kInSequence,
992  ".InSequence() cannot appear after .After(),"
993  " .WillOnce(), .WillRepeatedly(), or "
994  ".RetiresOnSaturation().");
995  last_clause_ = kInSequence;
996 
997  s.AddExpectation(GetHandle());
998  return *this;
999  }
1000  TypedExpectation & InSequence(const Sequence & s1, const Sequence & s2)
1001  {
1002  return InSequence(s1).InSequence(s2);
1003  }
1004  TypedExpectation & InSequence(const Sequence & s1, const Sequence & s2,
1005  const Sequence & s3)
1006  {
1007  return InSequence(s1, s2).InSequence(s3);
1008  }
1009  TypedExpectation & InSequence(const Sequence & s1, const Sequence & s2,
1010  const Sequence & s3, const Sequence & s4)
1011  {
1012  return InSequence(s1, s2, s3).InSequence(s4);
1013  }
1014  TypedExpectation & InSequence(const Sequence & s1, const Sequence & s2,
1015  const Sequence & s3, const Sequence & s4,
1016  const Sequence & s5)
1017  {
1018  return InSequence(s1, s2, s3, s4).InSequence(s5);
1019  }
1020 
1021  // Implements that .After() clause.
1023  {
1024  ExpectSpecProperty(last_clause_ <= kAfter,
1025  ".After() cannot appear after .WillOnce(),"
1026  " .WillRepeatedly(), or "
1027  ".RetiresOnSaturation().");
1028  last_clause_ = kAfter;
1029 
1030  for (ExpectationSet::const_iterator it = s.begin(); it != s.end(); ++it)
1031  {
1032  immediate_prerequisites_ += *it;
1033  }
1034 
1035  return *this;
1036  }
1038  {
1039  return After(s1).After(s2);
1040  }
1042  const ExpectationSet & s3)
1043  {
1044  return After(s1, s2).After(s3);
1045  }
1047  const ExpectationSet & s3, const ExpectationSet & s4)
1048  {
1049  return After(s1, s2, s3).After(s4);
1050  }
1052  const ExpectationSet & s3, const ExpectationSet & s4,
1053  const ExpectationSet & s5)
1054  {
1055  return After(s1, s2, s3, s4).After(s5);
1056  }
1057 
1058  // Implements the .WillOnce() clause.
1060  {
1061  ExpectSpecProperty(last_clause_ <= kWillOnce,
1062  ".WillOnce() cannot appear after "
1063  ".WillRepeatedly() or .RetiresOnSaturation().");
1064  last_clause_ = kWillOnce;
1065 
1066  untyped_actions_.push_back(new Action<F>(action));
1067 
1068  if (!cardinality_specified())
1069  {
1070  set_cardinality(Exactly(static_cast<int>(untyped_actions_.size())));
1071  }
1072 
1073  return *this;
1074  }
1075 
1076  // Implements the .WillRepeatedly() clause.
1078  {
1079  if (last_clause_ == kWillRepeatedly)
1080  {
1081  ExpectSpecProperty(false,
1082  ".WillRepeatedly() cannot appear "
1083  "more than once in an EXPECT_CALL().");
1084  }
1085 
1086  else
1087  {
1088  ExpectSpecProperty(last_clause_ < kWillRepeatedly,
1089  ".WillRepeatedly() cannot appear "
1090  "after .RetiresOnSaturation().");
1091  }
1092 
1093  last_clause_ = kWillRepeatedly;
1094  repeated_action_specified_ = true;
1095 
1096  repeated_action_ = action;
1097 
1098  if (!cardinality_specified())
1099  {
1100  set_cardinality(AtLeast(static_cast<int>(untyped_actions_.size())));
1101  }
1102 
1103  // Now that no more action clauses can be specified, we check
1104  // whether their count makes sense.
1105  CheckActionCountIfNotDone();
1106  return *this;
1107  }
1108 
1109  // Implements the .RetiresOnSaturation() clause.
1111  {
1112  ExpectSpecProperty(last_clause_ < kRetiresOnSaturation,
1113  ".RetiresOnSaturation() cannot appear "
1114  "more than once.");
1115  last_clause_ = kRetiresOnSaturation;
1116  retires_on_saturation_ = true;
1117 
1118  // Now that no more action clauses can be specified, we check
1119  // whether their count makes sense.
1120  CheckActionCountIfNotDone();
1121  return *this;
1122  }
1123 
1124  // Returns the matchers for the arguments as specified inside the
1125  // EXPECT_CALL() macro.
1126  const ArgumentMatcherTuple & matchers() const
1127  {
1128  return matchers_;
1129  }
1130 
1131  // Returns the matcher specified by the .With() clause.
1133  {
1134  return extra_matcher_;
1135  }
1136 
1137  // Returns the action specified by the .WillRepeatedly() clause.
1138  const Action<F> & repeated_action() const { return repeated_action_; }
1139 
1140  // If this mock method has an extra matcher (i.e. .With(matcher)),
1141  // describes it to the ostream.
1142  virtual void MaybeDescribeExtraMatcherTo(::std::ostream * os)
1143  {
1144  if (extra_matcher_specified_)
1145  {
1146  *os << " Expected args: ";
1147  extra_matcher_.DescribeTo(os);
1148  *os << "\n";
1149  }
1150  }
1151 
1152 private:
1153  template <typename Function>
1154  friend class FunctionMockerBase;
1155 
1156  // Returns an Expectation object that references and co-owns this
1157  // expectation.
1159  {
1160  return owner_->GetHandleOf(this);
1161  }
1162 
1163  // The following methods will be called only after the EXPECT_CALL()
1164  // statement finishes and when the current thread holds
1165  // g_gmock_mutex.
1166 
1167  // Returns true iff this expectation matches the given arguments.
1168  bool Matches(const ArgumentTuple & args) const
1169  GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
1170  {
1171  g_gmock_mutex.AssertHeld();
1172  return TupleMatches(matchers_, args) && extra_matcher_.Matches(args);
1173  }
1174 
1175  // Returns true iff this expectation should handle the given arguments.
1176  bool ShouldHandleArguments(const ArgumentTuple & args) const
1177  GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
1178  {
1179  g_gmock_mutex.AssertHeld();
1180 
1181  // In case the action count wasn't checked when the expectation
1182  // was defined (e.g. if this expectation has no WillRepeatedly()
1183  // or RetiresOnSaturation() clause), we check it when the
1184  // expectation is used for the first time.
1185  CheckActionCountIfNotDone();
1186  return !is_retired() && AllPrerequisitesAreSatisfied() && Matches(args);
1187  }
1188 
1189  // Describes the result of matching the arguments against this
1190  // expectation to the given ostream.
1192  const ArgumentTuple & args,
1193  ::std::ostream * os) const
1194  GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
1195  {
1196  g_gmock_mutex.AssertHeld();
1197 
1198  if (is_retired())
1199  {
1200  *os << " Expected: the expectation is active\n"
1201  << " Actual: it is retired\n";
1202  }
1203 
1204  else if (!Matches(args))
1205  {
1206  if (!TupleMatches(matchers_, args))
1207  {
1208  ExplainMatchFailureTupleTo(matchers_, args, os);
1209  }
1210 
1211  StringMatchResultListener listener;
1212 
1213  if (!extra_matcher_.MatchAndExplain(args, &listener))
1214  {
1215  *os << " Expected args: ";
1216  extra_matcher_.DescribeTo(os);
1217  *os << "\n Actual: don't match";
1218 
1219  internal::PrintIfNotEmpty(listener.str(), os);
1220  *os << "\n";
1221  }
1222  }
1223 
1224  else if (!AllPrerequisitesAreSatisfied())
1225  {
1226  *os << " Expected: all pre-requisites are satisfied\n"
1227  << " Actual: the following immediate pre-requisites "
1228  << "are not satisfied:\n";
1229  ExpectationSet unsatisfied_prereqs;
1230  FindUnsatisfiedPrerequisites(&unsatisfied_prereqs);
1231  int i = 0;
1232 
1233  for (ExpectationSet::const_iterator it = unsatisfied_prereqs.begin();
1234  it != unsatisfied_prereqs.end(); ++it)
1235  {
1236  it->expectation_base()->DescribeLocationTo(os);
1237  *os << "pre-requisite #" << i++ << "\n";
1238  }
1239 
1240  *os << " (end of pre-requisites)\n";
1241  }
1242 
1243  else
1244  {
1245  // This line is here just for completeness' sake. It will never
1246  // be executed as currently the ExplainMatchResultTo() function
1247  // is called only when the mock function call does NOT match the
1248  // expectation.
1249  *os << "The call matches the expectation.\n";
1250  }
1251  }
1252 
1253  // Returns the action that should be taken for the current invocation.
1255  const FunctionMockerBase<F> * mocker,
1256  const ArgumentTuple & args) const
1257  GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
1258  {
1259  g_gmock_mutex.AssertHeld();
1260  const int count = call_count();
1261  Assert(count >= 1, __FILE__, __LINE__,
1262  "call_count() is <= 0 when GetCurrentAction() is "
1263  "called - this should never happen.");
1264 
1265  const int action_count = static_cast<int>(untyped_actions_.size());
1266 
1267  if (action_count > 0 && !repeated_action_specified_ &&
1268  count > action_count)
1269  {
1270  // If there is at least one WillOnce() and no WillRepeatedly(),
1271  // we warn the user when the WillOnce() clauses ran out.
1272  ::std::stringstream ss;
1273  DescribeLocationTo(&ss);
1274  ss << "Actions ran out in " << source_text() << "...\n"
1275  << "Called " << count << " times, but only "
1276  << action_count << " WillOnce()"
1277  << (action_count == 1 ? " is" : "s are") << " specified - ";
1278  mocker->DescribeDefaultActionTo(args, &ss);
1279  Log(kWarning, ss.str(), 1);
1280  }
1281 
1282  return count <= action_count ?
1283  *static_cast<const Action<F>*>(untyped_actions_[count - 1]) :
1284  repeated_action();
1285  }
1286 
1287  // Given the arguments of a mock function call, if the call will
1288  // over-saturate this expectation, returns the default action;
1289  // otherwise, returns the next action in this expectation. Also
1290  // describes *what* happened to 'what', and explains *why* Google
1291  // Mock does it to 'why'. This method is not const as it calls
1292  // IncrementCallCount(). A return value of NULL means the default
1293  // action.
1295  const FunctionMockerBase<F> * mocker,
1296  const ArgumentTuple & args,
1297  ::std::ostream * what,
1298  ::std::ostream * why)
1299  GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
1300  {
1301  g_gmock_mutex.AssertHeld();
1302 
1303  if (IsSaturated())
1304  {
1305  // We have an excessive call.
1306  IncrementCallCount();
1307  *what << "Mock function called more times than expected - ";
1308  mocker->DescribeDefaultActionTo(args, what);
1309  DescribeCallCountTo(why);
1310 
1311  // TODO(wan@google.com): allow the user to control whether
1312  // unexpected calls should fail immediately or continue using a
1313  // flag --gmock_unexpected_calls_are_fatal.
1314  return NULL;
1315  }
1316 
1317  IncrementCallCount();
1318  RetireAllPreRequisites();
1319 
1320  if (retires_on_saturation_ && IsSaturated())
1321  {
1322  Retire();
1323  }
1324 
1325  // Must be done after IncrementCount()!
1326  *what << "Mock function call matches " << source_text() << "...\n";
1327  return &(GetCurrentAction(mocker, args));
1328  }
1329 
1330  // All the fields below won't change once the EXPECT_CALL()
1331  // statement finishes.
1332  FunctionMockerBase<F> * const owner_;
1333  ArgumentMatcherTuple matchers_;
1334  Matcher<const ArgumentTuple &> extra_matcher_;
1335  Action<F> repeated_action_;
1336 
1338 }; // class TypedExpectation
1339 
1340 // A MockSpec object is used by ON_CALL() or EXPECT_CALL() for
1341 // specifying the default behavior of, or expectation on, a mock
1342 // function.
1343 
1344 // Note: class MockSpec really belongs to the ::testing namespace.
1345 // However if we define it in ::testing, MSVC will complain when
1346 // classes in ::testing::internal declare it as a friend class
1347 // template. To workaround this compiler bug, we define MockSpec in
1348 // ::testing::internal and import it into ::testing.
1349 
1350 // Logs a message including file and line number information.
1352  const char * file, int line,
1353  const string & message);
1354 
1355 template <typename F>
1356 class MockSpec
1357 {
1358 public:
1362 
1363  // Constructs a MockSpec object, given the function mocker object
1364  // that the spec is associated with.
1365  explicit MockSpec(internal::FunctionMockerBase<F> * function_mocker)
1366  : function_mocker_(function_mocker) {}
1367 
1368  // Adds a new default action spec to the function mocker and returns
1369  // the newly created spec.
1371  const char * file, int line, const char * obj, const char * call)
1372  {
1373  LogWithLocation(internal::kInfo, file, line,
1374  string("ON_CALL(") + obj + ", " + call + ") invoked");
1375  return function_mocker_->AddNewOnCallSpec(file, line, matchers_);
1376  }
1377 
1378  // Adds a new expectation spec to the function mocker and returns
1379  // the newly created spec.
1381  const char * file, int line, const char * obj, const char * call)
1382  {
1383  const string source_text(string("EXPECT_CALL(") + obj + ", " + call + ")");
1384  LogWithLocation(internal::kInfo, file, line, source_text + " invoked");
1385  return function_mocker_->AddNewExpectation(
1386  file, line, source_text, matchers_);
1387  }
1388 
1389 private:
1390  template <typename Function>
1392 
1393  void SetMatchers(const ArgumentMatcherTuple & matchers)
1394  {
1395  matchers_ = matchers;
1396  }
1397 
1398  // The function mocker that owns this spec.
1399  internal::FunctionMockerBase<F> * const function_mocker_;
1400  // The argument matchers specified in the spec.
1401  ArgumentMatcherTuple matchers_;
1402 
1404 }; // class MockSpec
1405 
1406 // MSVC warns about using 'this' in base member initializer list, so
1407 // we need to temporarily disable the warning. We have to do it for
1408 // the entire class to suppress the warning, even though it's about
1409 // the constructor only.
1410 
1411 #ifdef _MSC_VER
1412 # pragma warning(push) // Saves the current warning state.
1413 # pragma warning(disable:4355) // Temporarily disables warning 4355.
1414 #endif // _MSV_VER
1415 
1416 // C++ treats the void type specially. For example, you cannot define
1417 // a void-typed variable or pass a void value to a function.
1418 // ActionResultHolder<T> holds a value of type T, where T must be a
1419 // copyable type or void (T doesn't need to be default-constructable).
1420 // It hides the syntactic difference between void and other types, and
1421 // is used to unify the code for invoking both void-returning and
1422 // non-void-returning mock functions.
1423 
1424 // Untyped base class for ActionResultHolder<T>.
1426 {
1427 public:
1429 
1430  // Prints the held value as an action's result to os.
1431  virtual void PrintAsActionResult(::std::ostream * os) const = 0;
1432 };
1433 
1434 // This generic definition is used when T is not void.
1435 template <typename T>
1437 {
1438 public:
1439  explicit ActionResultHolder(T a_value) : value_(a_value) {}
1440 
1441  // The compiler-generated copy constructor and assignment operator
1442  // are exactly what we need, so we don't need to define them.
1443 
1444  // Returns the held value and deletes this object.
1446  {
1447  T retval(value_);
1448  delete this;
1449  return retval;
1450  }
1451 
1452  // Prints the held value as an action's result to os.
1453  virtual void PrintAsActionResult(::std::ostream * os) const
1454  {
1455  *os << "\n Returns: ";
1456  // T may be a reference type, so we don't use UniversalPrint().
1457  UniversalPrinter<T>::Print(value_, os);
1458  }
1459 
1460  // Performs the given mock function's default action and returns the
1461  // result in a new-ed ActionResultHolder.
1462  template <typename F>
1464  const FunctionMockerBase<F> * func_mocker,
1465  const typename Function<F>::ArgumentTuple & args,
1466  const string & call_description)
1467  {
1468  return new ActionResultHolder(
1469  func_mocker->PerformDefaultAction(args, call_description));
1470  }
1471 
1472  // Performs the given action and returns the result in a new-ed
1473  // ActionResultHolder.
1474  template <typename F>
1475  static ActionResultHolder *
1476  PerformAction(const Action<F> & action,
1477  const typename Function<F>::ArgumentTuple & args)
1478  {
1479  return new ActionResultHolder(action.Perform(args));
1480  }
1481 
1482 private:
1483  T value_;
1484 
1485  // T could be a reference type, so = isn't supported.
1487 };
1488 
1489 // Specialization for T = void.
1490 template <>
1492 {
1493 public:
1494  void GetValueAndDelete() const { delete this; }
1495 
1496  virtual void PrintAsActionResult(::std::ostream * /* os */) const {}
1497 
1498  // Performs the given mock function's default action and returns NULL;
1499  template <typename F>
1501  const FunctionMockerBase<F> * func_mocker,
1502  const typename Function<F>::ArgumentTuple & args,
1503  const string & call_description)
1504  {
1505  func_mocker->PerformDefaultAction(args, call_description);
1506  return NULL;
1507  }
1508 
1509  // Performs the given action and returns NULL.
1510  template <typename F>
1512  const Action<F> & action,
1513  const typename Function<F>::ArgumentTuple & args)
1514  {
1515  action.Perform(args);
1516  return NULL;
1517  }
1518 };
1519 
1520 // The base of the function mocker class for the given function type.
1521 // We put the methods in this class instead of its child to avoid code
1522 // bloat.
1523 template <typename F>
1525 {
1526 public:
1527  typedef typename Function<F>::Result Result;
1530 
1531  FunctionMockerBase() : current_spec_(this) {}
1532 
1533  // The destructor verifies that all expectations on this mock
1534  // function have been satisfied. If not, it will report Google Test
1535  // non-fatal failures for the violations.
1537  GTEST_LOCK_EXCLUDED_(g_gmock_mutex)
1538  {
1539  MutexLock l(&g_gmock_mutex);
1540  VerifyAndClearExpectationsLocked();
1541  Mock::UnregisterLocked(this);
1542  ClearDefaultActionsLocked();
1543  }
1544 
1545  // Returns the ON_CALL spec that matches this mock function with the
1546  // given arguments; returns NULL if no matching ON_CALL is found.
1547  // L = *
1549  const ArgumentTuple & args) const
1550  {
1551  for (UntypedOnCallSpecs::const_reverse_iterator it
1552  = untyped_on_call_specs_.rbegin();
1553  it != untyped_on_call_specs_.rend(); ++it)
1554  {
1555  const OnCallSpec<F> * spec = static_cast<const OnCallSpec<F>*>(*it);
1556 
1557  if (spec->Matches(args))
1558  { return spec; }
1559  }
1560 
1561  return NULL;
1562  }
1563 
1564  // Performs the default action of this mock function on the given
1565  // arguments and returns the result. Asserts (or throws if
1566  // exceptions are enabled) with a helpful call descrption if there
1567  // is no valid return value. This method doesn't depend on the
1568  // mutable state of this object, and thus can be called concurrently
1569  // without locking.
1570  // L = *
1571  Result PerformDefaultAction(const ArgumentTuple & args,
1572  const string & call_description) const
1573  {
1574  const OnCallSpec<F> * const spec =
1575  this->FindOnCallSpec(args);
1576 
1577  if (spec != NULL)
1578  {
1579  return spec->GetAction().Perform(args);
1580  }
1581 
1582  const string message = call_description +
1583  "\n The mock function has no default action "
1584  "set, and its return type has no default value set.";
1585 #if GTEST_HAS_EXCEPTIONS
1586 
1588  {
1589  throw std::runtime_error(message);
1590  }
1591 
1592 #else
1593  Assert(DefaultValue<Result>::Exists(), "", -1, message);
1594 #endif
1595  return DefaultValue<Result>::Get();
1596  }
1597 
1598  // Performs the default action with the given arguments and returns
1599  // the action's result. The call description string will be used in
1600  // the error message to describe the call in the case the default
1601  // action fails. The caller is responsible for deleting the result.
1602  // L = *
1604  const void * untyped_args, // must point to an ArgumentTuple
1605  const string & call_description) const
1606  {
1607  const ArgumentTuple & args =
1608  *static_cast<const ArgumentTuple *>(untyped_args);
1609  return ResultHolder::PerformDefaultAction(this, args, call_description);
1610  }
1611 
1612  // Performs the given action with the given arguments and returns
1613  // the action's result. The caller is responsible for deleting the
1614  // result.
1615  // L = *
1617  const void * untyped_action, const void * untyped_args) const
1618  {
1619  // Make a copy of the action before performing it, in case the
1620  // action deletes the mock object (and thus deletes itself).
1621  const Action<F> action = *static_cast<const Action<F>*>(untyped_action);
1622  const ArgumentTuple & args =
1623  *static_cast<const ArgumentTuple *>(untyped_args);
1624  return ResultHolder::PerformAction(action, args);
1625  }
1626 
1627  // Implements UntypedFunctionMockerBase::ClearDefaultActionsLocked():
1628  // clears the ON_CALL()s set on this mock function.
1630  GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
1631  {
1632  g_gmock_mutex.AssertHeld();
1633 
1634  // Deleting our default actions may trigger other mock objects to be
1635  // deleted, for example if an action contains a reference counted smart
1636  // pointer to that mock object, and that is the last reference. So if we
1637  // delete our actions within the context of the global mutex we may deadlock
1638  // when this method is called again. Instead, make a copy of the set of
1639  // actions to delete, clear our set within the mutex, and then delete the
1640  // actions outside of the mutex.
1641  UntypedOnCallSpecs specs_to_delete;
1642  untyped_on_call_specs_.swap(specs_to_delete);
1643 
1644  g_gmock_mutex.Unlock();
1645 
1646  for (UntypedOnCallSpecs::const_iterator it =
1647  specs_to_delete.begin();
1648  it != specs_to_delete.end(); ++it)
1649  {
1650  delete static_cast<const OnCallSpec<F>*>(*it);
1651  }
1652 
1653  // Lock the mutex again, since the caller expects it to be locked when we
1654  // return.
1655  g_gmock_mutex.Lock();
1656  }
1657 
1658 protected:
1659  template <typename Function>
1660  friend class MockSpec;
1661 
1663 
1664  // Returns the result of invoking this mock function with the given
1665  // arguments. This function can be safely called from multiple
1666  // threads concurrently.
1667  Result InvokeWith(const ArgumentTuple & args)
1668  GTEST_LOCK_EXCLUDED_(g_gmock_mutex)
1669  {
1670  return static_cast<const ResultHolder *>(
1671  this->UntypedInvokeWith(&args))->GetValueAndDelete();
1672  }
1673 
1674  // Adds and returns a default action spec for this mock function.
1676  const char * file, int line,
1677  const ArgumentMatcherTuple & m)
1678  GTEST_LOCK_EXCLUDED_(g_gmock_mutex)
1679  {
1680  Mock::RegisterUseByOnCallOrExpectCall(MockObject(), file, line);
1681  OnCallSpec<F> * const on_call_spec = new OnCallSpec<F>(file, line, m);
1682  untyped_on_call_specs_.push_back(on_call_spec);
1683  return *on_call_spec;
1684  }
1685 
1686  // Adds and returns an expectation spec for this mock function.
1688  const char * file,
1689  int line,
1690  const string & source_text,
1691  const ArgumentMatcherTuple & m)
1692  GTEST_LOCK_EXCLUDED_(g_gmock_mutex)
1693  {
1694  Mock::RegisterUseByOnCallOrExpectCall(MockObject(), file, line);
1695  TypedExpectation<F> * const expectation =
1696  new TypedExpectation<F>(this, file, line, source_text, m);
1697  const linked_ptr<ExpectationBase> untyped_expectation(expectation);
1698  untyped_expectations_.push_back(untyped_expectation);
1699 
1700  // Adds this expectation into the implicit sequence if there is one.
1701  Sequence * const implicit_sequence = g_gmock_implicit_sequence.get();
1702 
1703  if (implicit_sequence != NULL)
1704  {
1705  implicit_sequence->AddExpectation(Expectation(untyped_expectation));
1706  }
1707 
1708  return *expectation;
1709  }
1710 
1711  // The current spec (either default action spec or expectation spec)
1712  // being described on this function mocker.
1713  MockSpec<F> & current_spec() { return current_spec_; }
1714 
1715 private:
1716  template <typename Func> friend class TypedExpectation;
1717 
1718  // Some utilities needed for implementing UntypedInvokeWith().
1719 
1720  // Describes what default action will be performed for the given
1721  // arguments.
1722  // L = *
1723  void DescribeDefaultActionTo(const ArgumentTuple & args,
1724  ::std::ostream * os) const
1725  {
1726  const OnCallSpec<F> * const spec = FindOnCallSpec(args);
1727 
1728  if (spec == NULL)
1729  {
1730  *os << (internal::type_equals<Result, void>::value ?
1731  "returning directly.\n" :
1732  "returning default value.\n");
1733  }
1734 
1735  else
1736  {
1737  *os << "taking default action specified at:\n"
1738  << FormatFileLocation(spec->file(), spec->line()) << "\n";
1739  }
1740  }
1741 
1742  // Writes a message that the call is uninteresting (i.e. neither
1743  // explicitly expected nor explicitly unexpected) to the given
1744  // ostream.
1746  const void * untyped_args,
1747  ::std::ostream * os) const
1748  GTEST_LOCK_EXCLUDED_(g_gmock_mutex)
1749  {
1750  const ArgumentTuple & args =
1751  *static_cast<const ArgumentTuple *>(untyped_args);
1752  *os << "Uninteresting mock function call - ";
1753  DescribeDefaultActionTo(args, os);
1754  *os << " Function call: " << Name();
1755  UniversalPrint(args, os);
1756  }
1757 
1758  // Returns the expectation that matches the given function arguments
1759  // (or NULL is there's no match); when a match is found,
1760  // untyped_action is set to point to the action that should be
1761  // performed (or NULL if the action is "do default"), and
1762  // is_excessive is modified to indicate whether the call exceeds the
1763  // expected number.
1764  //
1765  // Critical section: We must find the matching expectation and the
1766  // corresponding action that needs to be taken in an ATOMIC
1767  // transaction. Otherwise another thread may call this mock
1768  // method in the middle and mess up the state.
1769  //
1770  // However, performing the action has to be left out of the critical
1771  // section. The reason is that we have no control on what the
1772  // action does (it can invoke an arbitrary user function or even a
1773  // mock function) and excessive locking could cause a dead lock.
1774  virtual const ExpectationBase * UntypedFindMatchingExpectation(
1775  const void * untyped_args,
1776  const void ** untyped_action, bool * is_excessive,
1777  ::std::ostream * what, ::std::ostream * why)
1778  GTEST_LOCK_EXCLUDED_(g_gmock_mutex)
1779  {
1780  const ArgumentTuple & args =
1781  *static_cast<const ArgumentTuple *>(untyped_args);
1782  MutexLock l(&g_gmock_mutex);
1783  TypedExpectation<F> * exp = this->FindMatchingExpectationLocked(args);
1784 
1785  if (exp == NULL) // A match wasn't found.
1786  {
1787  this->FormatUnexpectedCallMessageLocked(args, what, why);
1788  return NULL;
1789  }
1790 
1791  // This line must be done before calling GetActionForArguments(),
1792  // which will increment the call count for *exp and thus affect
1793  // its saturation status.
1794  *is_excessive = exp->IsSaturated();
1795  const Action<F> * action = exp->GetActionForArguments(this, args, what, why);
1796 
1797  if (action != NULL && action->IsDoDefault())
1798  { action = NULL; } // Normalize "do default" to NULL.
1799 
1800  *untyped_action = action;
1801  return exp;
1802  }
1803 
1804  // Prints the given function arguments to the ostream.
1805  virtual void UntypedPrintArgs(const void * untyped_args,
1806  ::std::ostream * os) const
1807  {
1808  const ArgumentTuple & args =
1809  *static_cast<const ArgumentTuple *>(untyped_args);
1810  UniversalPrint(args, os);
1811  }
1812 
1813  // Returns the expectation that matches the arguments, or NULL if no
1814  // expectation matches them.
1816  const ArgumentTuple & args) const
1817  GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
1818  {
1819  g_gmock_mutex.AssertHeld();
1820 
1821  for (typename UntypedExpectations::const_reverse_iterator it =
1822  untyped_expectations_.rbegin();
1823  it != untyped_expectations_.rend(); ++it)
1824  {
1825  TypedExpectation<F> * const exp =
1826  static_cast<TypedExpectation<F>*>(it->get());
1827 
1828  if (exp->ShouldHandleArguments(args))
1829  {
1830  return exp;
1831  }
1832  }
1833 
1834  return NULL;
1835  }
1836 
1837  // Returns a message that the arguments don't match any expectation.
1839  const ArgumentTuple & args,
1840  ::std::ostream * os,
1841  ::std::ostream * why) const
1842  GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
1843  {
1844  g_gmock_mutex.AssertHeld();
1845  *os << "\nUnexpected mock function call - ";
1846  DescribeDefaultActionTo(args, os);
1847  PrintTriedExpectationsLocked(args, why);
1848  }
1849 
1850  // Prints a list of expectations that have been tried against the
1851  // current mock function call.
1853  const ArgumentTuple & args,
1854  ::std::ostream * why) const
1855  GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
1856  {
1857  g_gmock_mutex.AssertHeld();
1858  const int count = static_cast<int>(untyped_expectations_.size());
1859  *why << "Google Mock tried the following " << count << " "
1860  << (count == 1 ? "expectation, but it didn't match" :
1861  "expectations, but none matched")
1862  << ":\n";
1863 
1864  for (int i = 0; i < count; i++)
1865  {
1866  TypedExpectation<F> * const expectation =
1867  static_cast<TypedExpectation<F>*>(untyped_expectations_[i].get());
1868  *why << "\n";
1869  expectation->DescribeLocationTo(why);
1870 
1871  if (count > 1)
1872  {
1873  *why << "tried expectation #" << i << ": ";
1874  }
1875 
1876  *why << expectation->source_text() << "...\n";
1877  expectation->ExplainMatchResultTo(args, why);
1878  expectation->DescribeCallCountTo(why);
1879  }
1880  }
1881 
1882  // The current spec (either default action spec or expectation spec)
1883  // being described on this function mocker.
1884  MockSpec<F> current_spec_;
1885 
1886  // There is no generally useful and implementable semantics of
1887  // copying a mock object, so copying a mock is usually a user error.
1888  // Thus we disallow copying function mockers. If the user really
1889  // wants to copy a mock object, he should implement his own copy
1890  // operation, for example:
1891  //
1892  // class MockFoo : public Foo {
1893  // public:
1894  // // Defines a copy constructor explicitly.
1895  // MockFoo(const MockFoo& src) {}
1896  // ...
1897  // };
1899 }; // class FunctionMockerBase
1900 
1901 #ifdef _MSC_VER
1902 # pragma warning(pop) // Restores the warning state.
1903 #endif // _MSV_VER
1904 
1905 // Implements methods of FunctionMockerBase.
1906 
1907 // Verifies that all expectations on this mock function have been
1908 // satisfied. Reports one or more Google Test non-fatal failures and
1909 // returns false if not.
1910 
1911 // Reports an uninteresting call (whose description is in msg) in the
1912 // manner specified by 'reaction'.
1913 void ReportUninterestingCall(CallReaction reaction, const string & msg);
1914 
1915 } // namespace internal
1916 
1917 // The style guide prohibits "using" statements in a namespace scope
1918 // inside a header file. However, the MockSpec class template is
1919 // meant to be defined in the ::testing namespace. The following line
1920 // is just a trick for working around a bug in MSVC 8.0, which cannot
1921 // handle it if we define MockSpec in ::testing.
1922 using internal::MockSpec;
1923 
1924 // Const(x) is a convenient function for obtaining a const reference
1925 // to x. This is useful for setting expectations on an overloaded
1926 // const mock method, e.g.
1927 //
1928 // class MockFoo : public FooInterface {
1929 // public:
1930 // MOCK_METHOD0(Bar, int());
1931 // MOCK_CONST_METHOD0(Bar, int&());
1932 // };
1933 //
1934 // MockFoo foo;
1935 // // Expects a call to non-const MockFoo::Bar().
1936 // EXPECT_CALL(foo, Bar());
1937 // // Expects a call to const MockFoo::Bar().
1938 // EXPECT_CALL(Const(foo), Bar());
1939 template <typename T>
1940 inline const T & Const(const T & x) { return x; }
1941 
1942 // Constructs an Expectation object that references and co-owns exp.
1944  : expectation_base_(exp.GetHandle().expectation_base()) {}
1945 
1946 } // namespace testing
1947 
1948 // A separate macro is required to avoid compile errors when the name
1949 // of the method used in call is a result of macro expansion.
1950 // See CompilesWithMethodNameExpandedFromMacro tests in
1951 // internal/gmock-spec-builders_test.cc for more details.
1952 #define GMOCK_ON_CALL_IMPL_(obj, call) \
1953  ((obj).gmock_##call).InternalDefaultActionSetAt(__FILE__, __LINE__, \
1954  #obj, #call)
1955 #define ON_CALL(obj, call) GMOCK_ON_CALL_IMPL_(obj, call)
1956 
1957 #define GMOCK_EXPECT_CALL_IMPL_(obj, call) \
1958  ((obj).gmock_##call).InternalExpectedAt(__FILE__, __LINE__, #obj, #call)
1959 #define EXPECT_CALL(obj, call) GMOCK_EXPECT_CALL_IMPL_(obj, call)
1960 
1961 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_
void ExpectSpecProperty(bool property, const string &failure_message) const
Function< F >::ArgumentMatcherTuple ArgumentMatcherTuple
void ExpectSpecProperty(bool property, const string &failure_message) const
GTEST_API_ ThreadLocal< Sequence * > g_gmock_implicit_sequence
virtual void UntypedDescribeUninterestingCall(const void *untyped_args,::std::ostream *os) const GTEST_LOCK_EXCLUDED_(g_gmock_mutex)
void AssertSpecProperty(bool property, const string &failure_message) const
bool Matches(const ArgumentTuple &args) const GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
OnCallSpec(const char *a_file, int a_line, const ArgumentMatcherTuple &matchers)
virtual ~FunctionMockerBase() GTEST_LOCK_EXCLUDED_(g_gmock_mutex)
ExpectationSet(const Expectation &e)
const internal::linked_ptr< internal::ExpectationBase > & expectation_base() const
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
virtual Expectation GetHandle()=0
GTEST_API_ Cardinality AtLeast(int n)
int * count
TypedExpectation(FunctionMockerBase< F > *owner, const char *a_file, int a_line, const string &a_source_text, const ArgumentMatcherTuple &m)
#define GTEST_API_
virtual void UntypedPrintArgs(const void *untyped_args,::std::ostream *os) const
class testing::internal::GTestFlagSaver GTEST_ATTRIBUTE_UNUSED_
ExpectationSet(internal::ExpectationBase &exp)
TypedExpectation & WillOnce(const Action< F > &action)
static ActionResultHolder * PerformAction(const Action< F > &action, const typename Function< F >::ArgumentTuple &args)
bool IsSatisfied() const GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
bool ShouldHandleArguments(const ArgumentTuple &args) const GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
const Action< F > & GetAction() const
void DescribeLocationTo(::std::ostream *os) const
std::vector< internal::linked_ptr< ExpectationBase > > UntypedExpectations
GTEST_API_::std::string FormatFileLocation(const char *file, int line)
TypedExpectation< F > * FindMatchingExpectationLocked(const ArgumentTuple &args) const GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
MockSpec(internal::FunctionMockerBase< F > *function_mocker)
TypedExpectation & Times(const Cardinality &a_cardinality)
TypedExpectation< F > & AddNewExpectation(const char *file, int line, const string &source_text, const ArgumentMatcherTuple &m) GTEST_LOCK_EXCLUDED_(g_gmock_mutex)
internal::OnCallSpec< F > & InternalDefaultActionSetAt(const char *file, int line, const char *obj, const char *call)
std::vector< const void * > UntypedActions
OnCallSpec & With(const Matcher< const ArgumentTuple & > &m)
const Matcher< const ArgumentTuple & > & extra_matcher() const
static ActionResultHolder * PerformDefaultAction(const FunctionMockerBase< F > *func_mocker, const typename Function< F >::ArgumentTuple &args, const string &call_description)
const Action< F > & repeated_action() const
TypedExpectation & InSequence(const Sequence &s1, const Sequence &s2, const Sequence &s3, const Sequence &s4)
GTEST_API_ GTEST_DECLARE_STATIC_MUTEX_(g_gmock_mutex)
bool is_retired() const GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
TypedExpectation & After(const ExpectationSet &s1, const ExpectationSet &s2)
Function< F >::ArgumentTuple ArgumentTuple
void IncrementCallCount() GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
bool IsOverSaturatedByCallCount(int call_count) const
bool operator==(const ExpectationSet &rhs) const
bool TupleMatches(const MatcherTuple &matcher_tuple, const ValueTuple &value_tuple)
void UntypedTimes(const Cardinality &a_cardinality)
const OnCallSpec< F > * FindOnCallSpec(const ArgumentTuple &args) const
static ActionResultHolder * PerformAction(const Action< F > &action, const typename Function< F >::ArgumentTuple &args)
Result InvokeWith(const ArgumentTuple &args) GTEST_LOCK_EXCLUDED_(g_gmock_mutex)
name
Definition: setup.py:38
Function< F >::ArgumentTuple ArgumentTuple
virtual void PrintAsActionResult(::std::ostream *os) const
TypedExpectation & After(const ExpectationSet &s1, const ExpectationSet &s2, const ExpectationSet &s3, const ExpectationSet &s4, const ExpectationSet &s5)
TypedExpectation & After(const ExpectationSet &s1, const ExpectationSet &s2, const ExpectationSet &s3, const ExpectationSet &s4)
bool operator==(const Expectation &rhs) const
TypedExpectation & InSequence(const Sequence &s1, const Sequence &s2)
Expectation::Set::const_iterator const_iterator
OnCallSpec< F > & AddNewOnCallSpec(const char *file, int line, const ArgumentMatcherTuple &m) GTEST_LOCK_EXCLUDED_(g_gmock_mutex)
int call_count() const GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
void ReportUninterestingCall(CallReaction reaction, const string &msg)
internal::TypedExpectation< F > & InternalExpectedAt(const char *file, int line, const char *obj, const char *call)
Expectation::Set::value_type value_type
internal::linked_ptr< internal::ExpectationBase > expectation_base_
internal::Function< F >::ArgumentTuple ArgumentTuple
TypedExpectation & InSequence(const Sequence &s)
void Expect(bool condition, const char *file, int line, const string &msg)
message
Definition: server.py:50
const Action< F > * GetActionForArguments(const FunctionMockerBase< F > *mocker, const ArgumentTuple &args,::std::ostream *what,::std::ostream *why) GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
#define GTEST_EXCLUSIVE_LOCK_REQUIRED_(locks)
bool operator()(const Expectation &lhs, const Expectation &rhs) const
virtual void PrintAsActionResult(::std::ostream *) const
string call
Definition: codegen.py:342
void AddExpectation(const Expectation &expectation) const
void PrintIfNotEmpty(const internal::string &explanation,::std::ostream *os)
bool operator!=(const ExpectationSet &rhs) const
bool Matches(const ArgumentTuple &args) const
GTEST_API_ void LogWithLocation(testing::internal::LogSeverity severity, const char *file, int line, const string &message)
internal::MatcherAsPredicate< M > Matches(M matcher)
TypedExpectation & WillRepeatedly(const Action< F > &action)
GTEST_API_ void Log(LogSeverity severity, const string &message, int stack_frames_to_skip)
UntypedOnCallSpecBase(const char *a_file, int a_line)
void UniversalPrint(const T &value,::std::ostream *os)
#define GTEST_DISALLOW_ASSIGN_(type)
virtual const ExpectationBase * UntypedFindMatchingExpectation(const void *untyped_args, const void **untyped_action, bool *is_excessive,::std::ostream *what,::std::ostream *why) GTEST_LOCK_EXCLUDED_(g_gmock_mutex)
Result PerformDefaultAction(const ArgumentTuple &args, const string &call_description) const
Function< F >::ArgumentTuple ArgumentTuple
TypedExpectation & After(const ExpectationSet &s1, const ExpectationSet &s2, const ExpectationSet &s3)
internal::Function< F >::ArgumentMatcherTuple ArgumentMatcherTuple
void ExplainMatchFailureTupleTo(const MatcherTuple &matchers, const ValueTuple &values,::std::ostream *os)
void Retire() GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
void AssertSpecProperty(bool property, const string &failure_message) const
ExpectationSet & operator+=(const Expectation &e)
Function< F >::ArgumentMatcherTuple ArgumentMatcherTuple
virtual void ClearDefaultActionsLocked() GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
bool IsSaturated() const GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
GTEST_API_ Cardinality Exactly(int n)
bool IsSaturatedByCallCount(int call_count) const
void set_cardinality(const Cardinality &a_cardinality)
const_iterator end() const
ActionResultHolder< Result > ResultHolder
const_iterator begin() const
internal::DoDefaultAction DoDefault()
TypedExpectation & InSequence(const Sequence &s1, const Sequence &s2, const Sequence &s3, const Sequence &s4, const Sequence &s5)
OnCallSpec & WillByDefault(const Action< F > &action)
void Assert(bool condition, const char *file, int line)
Result Perform(const ArgumentTuple &args) const
TypedExpectation & After(const ExpectationSet &s)
void FormatUnexpectedCallMessageLocked(const ArgumentTuple &args,::std::ostream *os,::std::ostream *why) const GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
Matcher< T > A()
void SetMatchers(const ArgumentMatcherTuple &matchers)
void ExplainMatchResultTo(const ArgumentTuple &args,::std::ostream *os) const GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
static ActionResultHolder * PerformDefaultAction(const FunctionMockerBase< F > *func_mocker, const typename Function< F >::ArgumentTuple &args, const string &call_description)
bool IsSatisfiedByCallCount(int call_count) const
const ArgumentMatcherTuple & matchers() const
static void Print(const T &value,::std::ostream *os)
TypedExpectation & With(const Matcher< const ArgumentTuple & > &m)
::std::set< Expectation, Less > Set
const Action< F > & GetCurrentAction(const FunctionMockerBase< F > *mocker, const ArgumentTuple &args) const GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
const Cardinality & cardinality() const
bool operator!=(const Expectation &rhs) const
void DescribeCallCountTo(::std::ostream *os) const GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
bool IsOverSaturated() const GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
void DescribeDefaultActionTo(const ArgumentTuple &args,::std::ostream *os) const
TypedExpectation & InSequence(const Sequence &s1, const Sequence &s2, const Sequence &s3)
void PrintTriedExpectationsLocked(const ArgumentTuple &args,::std::ostream *why) const GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
const T & Const(const T &x)
std::string name_
Function< F >::ArgumentMatcherTuple ArgumentMatcherTuple
virtual UntypedActionResultHolderBase * UntypedPerformDefaultAction(const void *untyped_args, const string &call_description) const
virtual void MaybeDescribeExtraMatcherTo(::std::ostream *os)
virtual UntypedActionResultHolderBase * UntypedPerformAction(const void *untyped_action, const void *untyped_args) const
#define GTEST_LOCK_EXCLUDED_(locks)


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:06:08