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