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