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 spec builder syntax (ON_CALL and 34 // EXPECT_CALL). 35 36 #include "gmock/gmock-spec-builders.h" 37 38 #include <stdlib.h> 39 #include <iostream> // NOLINT 40 #include <map> 41 #include <set> 42 #include <string> 43 #include <vector> 44 #include "gmock/gmock.h" 45 #include "gtest/gtest.h" 46 47 #if GTEST_OS_CYGWIN || GTEST_OS_LINUX || GTEST_OS_MAC 48 # include <unistd.h> // NOLINT 49 #endif 50 51 // Silence C4800 (C4800: 'int *const ': forcing value 52 // to bool 'true' or 'false') for MSVC 14,15 53 #ifdef _MSC_VER 54 #if _MSC_VER <= 1900 55 # pragma warning(push) 56 # pragma warning(disable:4800) 57 #endif 58 #endif 59 60 namespace testing { 61 namespace internal { 62 63 // Protects the mock object registry (in class Mock), all function 64 // mockers, and all expectations. 65 GTEST_API_ GTEST_DEFINE_STATIC_MUTEX_(g_gmock_mutex); 66 67 // Logs a message including file and line number information. 68 GTEST_API_ void LogWithLocation(testing::internal::LogSeverity severity, 69 const char* file, int line, 70 const std::string& message) { 71 ::std::ostringstream s; 72 s << file << ":" << line << ": " << message << ::std::endl; 73 Log(severity, s.str(), 0); 74 } 75 76 // Constructs an ExpectationBase object. 77 ExpectationBase::ExpectationBase(const char* a_file, int a_line, 78 const std::string& a_source_text) 79 : file_(a_file), 80 line_(a_line), 81 source_text_(a_source_text), 82 cardinality_specified_(false), 83 cardinality_(Exactly(1)), 84 call_count_(0), 85 retired_(false), 86 extra_matcher_specified_(false), 87 repeated_action_specified_(false), 88 retires_on_saturation_(false), 89 last_clause_(kNone), 90 action_count_checked_(false) {} 91 92 // Destructs an ExpectationBase object. 93 ExpectationBase::~ExpectationBase() {} 94 95 // Explicitly specifies the cardinality of this expectation. Used by 96 // the subclasses to implement the .Times() clause. 97 void ExpectationBase::SpecifyCardinality(const Cardinality& a_cardinality) { 98 cardinality_specified_ = true; 99 cardinality_ = a_cardinality; 100 } 101 102 // Retires all pre-requisites of this expectation. 103 void ExpectationBase::RetireAllPreRequisites() 104 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { 105 if (is_retired()) { 106 // We can take this short-cut as we never retire an expectation 107 // until we have retired all its pre-requisites. 108 return; 109 } 110 111 ::std::vector<ExpectationBase*> expectations(1, this); 112 while (!expectations.empty()) { 113 ExpectationBase* exp = expectations.back(); 114 expectations.pop_back(); 115 116 for (ExpectationSet::const_iterator it = 117 exp->immediate_prerequisites_.begin(); 118 it != exp->immediate_prerequisites_.end(); ++it) { 119 ExpectationBase* next = it->expectation_base().get(); 120 if (!next->is_retired()) { 121 next->Retire(); 122 expectations.push_back(next); 123 } 124 } 125 } 126 } 127 128 // Returns true iff all pre-requisites of this expectation have been 129 // satisfied. 130 bool ExpectationBase::AllPrerequisitesAreSatisfied() const 131 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { 132 g_gmock_mutex.AssertHeld(); 133 ::std::vector<const ExpectationBase*> expectations(1, this); 134 while (!expectations.empty()) { 135 const ExpectationBase* exp = expectations.back(); 136 expectations.pop_back(); 137 138 for (ExpectationSet::const_iterator it = 139 exp->immediate_prerequisites_.begin(); 140 it != exp->immediate_prerequisites_.end(); ++it) { 141 const ExpectationBase* next = it->expectation_base().get(); 142 if (!next->IsSatisfied()) return false; 143 expectations.push_back(next); 144 } 145 } 146 return true; 147 } 148 149 // Adds unsatisfied pre-requisites of this expectation to 'result'. 150 void ExpectationBase::FindUnsatisfiedPrerequisites(ExpectationSet* result) const 151 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { 152 g_gmock_mutex.AssertHeld(); 153 ::std::vector<const ExpectationBase*> expectations(1, this); 154 while (!expectations.empty()) { 155 const ExpectationBase* exp = expectations.back(); 156 expectations.pop_back(); 157 158 for (ExpectationSet::const_iterator it = 159 exp->immediate_prerequisites_.begin(); 160 it != exp->immediate_prerequisites_.end(); ++it) { 161 const ExpectationBase* next = it->expectation_base().get(); 162 163 if (next->IsSatisfied()) { 164 // If *it is satisfied and has a call count of 0, some of its 165 // pre-requisites may not be satisfied yet. 166 if (next->call_count_ == 0) { 167 expectations.push_back(next); 168 } 169 } else { 170 // Now that we know next is unsatisfied, we are not so interested 171 // in whether its pre-requisites are satisfied. Therefore we 172 // don't iterate into it here. 173 *result += *it; 174 } 175 } 176 } 177 } 178 179 // Describes how many times a function call matching this 180 // expectation has occurred. 181 void ExpectationBase::DescribeCallCountTo(::std::ostream* os) const 182 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { 183 g_gmock_mutex.AssertHeld(); 184 185 // Describes how many times the function is expected to be called. 186 *os << " Expected: to be "; 187 cardinality().DescribeTo(os); 188 *os << "\n Actual: "; 189 Cardinality::DescribeActualCallCountTo(call_count(), os); 190 191 // Describes the state of the expectation (e.g. is it satisfied? 192 // is it active?). 193 *os << " - " << (IsOverSaturated() ? "over-saturated" : 194 IsSaturated() ? "saturated" : 195 IsSatisfied() ? "satisfied" : "unsatisfied") 196 << " and " 197 << (is_retired() ? "retired" : "active"); 198 } 199 200 // Checks the action count (i.e. the number of WillOnce() and 201 // WillRepeatedly() clauses) against the cardinality if this hasn't 202 // been done before. Prints a warning if there are too many or too 203 // few actions. 204 void ExpectationBase::CheckActionCountIfNotDone() const 205 GTEST_LOCK_EXCLUDED_(mutex_) { 206 bool should_check = false; 207 { 208 MutexLock l(&mutex_); 209 if (!action_count_checked_) { 210 action_count_checked_ = true; 211 should_check = true; 212 } 213 } 214 215 if (should_check) { 216 if (!cardinality_specified_) { 217 // The cardinality was inferred - no need to check the action 218 // count against it. 219 return; 220 } 221 222 // The cardinality was explicitly specified. 223 const int action_count = static_cast<int>(untyped_actions_.size()); 224 const int upper_bound = cardinality().ConservativeUpperBound(); 225 const int lower_bound = cardinality().ConservativeLowerBound(); 226 bool too_many; // True if there are too many actions, or false 227 // if there are too few. 228 if (action_count > upper_bound || 229 (action_count == upper_bound && repeated_action_specified_)) { 230 too_many = true; 231 } else if (0 < action_count && action_count < lower_bound && 232 !repeated_action_specified_) { 233 too_many = false; 234 } else { 235 return; 236 } 237 238 ::std::stringstream ss; 239 DescribeLocationTo(&ss); 240 ss << "Too " << (too_many ? "many" : "few") 241 << " actions specified in " << source_text() << "...\n" 242 << "Expected to be "; 243 cardinality().DescribeTo(&ss); 244 ss << ", but has " << (too_many ? "" : "only ") 245 << action_count << " WillOnce()" 246 << (action_count == 1 ? "" : "s"); 247 if (repeated_action_specified_) { 248 ss << " and a WillRepeatedly()"; 249 } 250 ss << "."; 251 Log(kWarning, ss.str(), -1); // -1 means "don't print stack trace". 252 } 253 } 254 255 // Implements the .Times() clause. 256 void ExpectationBase::UntypedTimes(const Cardinality& a_cardinality) { 257 if (last_clause_ == kTimes) { 258 ExpectSpecProperty(false, 259 ".Times() cannot appear " 260 "more than once in an EXPECT_CALL()."); 261 } else { 262 ExpectSpecProperty(last_clause_ < kTimes, 263 ".Times() cannot appear after " 264 ".InSequence(), .WillOnce(), .WillRepeatedly(), " 265 "or .RetiresOnSaturation()."); 266 } 267 last_clause_ = kTimes; 268 269 SpecifyCardinality(a_cardinality); 270 } 271 272 // Points to the implicit sequence introduced by a living InSequence 273 // object (if any) in the current thread or NULL. 274 GTEST_API_ ThreadLocal<Sequence*> g_gmock_implicit_sequence; 275 276 // Reports an uninteresting call (whose description is in msg) in the 277 // manner specified by 'reaction'. 278 void ReportUninterestingCall(CallReaction reaction, const std::string& msg) { 279 // Include a stack trace only if --gmock_verbose=info is specified. 280 const int stack_frames_to_skip = 281 GMOCK_FLAG(verbose) == kInfoVerbosity ? 3 : -1; 282 switch (reaction) { 283 case kAllow: 284 Log(kInfo, msg, stack_frames_to_skip); 285 break; 286 case kWarn: 287 Log(kWarning, 288 msg + 289 "\nNOTE: You can safely ignore the above warning unless this " 290 "call should not happen. Do not suppress it by blindly adding " 291 "an EXPECT_CALL() if you don't mean to enforce the call. " 292 "See " 293 "https://github.com/google/googletest/blob/master/googlemock/" 294 "docs/CookBook.md#" 295 "knowing-when-to-expect for details.\n", 296 stack_frames_to_skip); 297 break; 298 default: // FAIL 299 Expect(false, NULL, -1, msg); 300 } 301 } 302 303 UntypedFunctionMockerBase::UntypedFunctionMockerBase() 304 : mock_obj_(NULL), name_("") {} 305 306 UntypedFunctionMockerBase::~UntypedFunctionMockerBase() {} 307 308 // Sets the mock object this mock method belongs to, and registers 309 // this information in the global mock registry. Will be called 310 // whenever an EXPECT_CALL() or ON_CALL() is executed on this mock 311 // method. 312 void UntypedFunctionMockerBase::RegisterOwner(const void* mock_obj) 313 GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { 314 { 315 MutexLock l(&g_gmock_mutex); 316 mock_obj_ = mock_obj; 317 } 318 Mock::Register(mock_obj, this); 319 } 320 321 // Sets the mock object this mock method belongs to, and sets the name 322 // of the mock function. Will be called upon each invocation of this 323 // mock function. 324 void UntypedFunctionMockerBase::SetOwnerAndName(const void* mock_obj, 325 const char* name) 326 GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { 327 // We protect name_ under g_gmock_mutex in case this mock function 328 // is called from two threads concurrently. 329 MutexLock l(&g_gmock_mutex); 330 mock_obj_ = mock_obj; 331 name_ = name; 332 } 333 334 // Returns the name of the function being mocked. Must be called 335 // after RegisterOwner() or SetOwnerAndName() has been called. 336 const void* UntypedFunctionMockerBase::MockObject() const 337 GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { 338 const void* mock_obj; 339 { 340 // We protect mock_obj_ under g_gmock_mutex in case this mock 341 // function is called from two threads concurrently. 342 MutexLock l(&g_gmock_mutex); 343 Assert(mock_obj_ != NULL, __FILE__, __LINE__, 344 "MockObject() must not be called before RegisterOwner() or " 345 "SetOwnerAndName() has been called."); 346 mock_obj = mock_obj_; 347 } 348 return mock_obj; 349 } 350 351 // Returns the name of this mock method. Must be called after 352 // SetOwnerAndName() has been called. 353 const char* UntypedFunctionMockerBase::Name() const 354 GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { 355 const char* name; 356 { 357 // We protect name_ under g_gmock_mutex in case this mock 358 // function is called from two threads concurrently. 359 MutexLock l(&g_gmock_mutex); 360 Assert(name_ != NULL, __FILE__, __LINE__, 361 "Name() must not be called before SetOwnerAndName() has " 362 "been called."); 363 name = name_; 364 } 365 return name; 366 } 367 368 // Calculates the result of invoking this mock function with the given 369 // arguments, prints it, and returns it. The caller is responsible 370 // for deleting the result. 371 UntypedActionResultHolderBase* UntypedFunctionMockerBase::UntypedInvokeWith( 372 void* const untyped_args) GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { 373 // See the definition of untyped_expectations_ for why access to it 374 // is unprotected here. 375 if (untyped_expectations_.size() == 0) { 376 // No expectation is set on this mock method - we have an 377 // uninteresting call. 378 379 // We must get Google Mock's reaction on uninteresting calls 380 // made on this mock object BEFORE performing the action, 381 // because the action may DELETE the mock object and make the 382 // following expression meaningless. 383 const CallReaction reaction = 384 Mock::GetReactionOnUninterestingCalls(MockObject()); 385 386 // True iff we need to print this call's arguments and return 387 // value. This definition must be kept in sync with 388 // the behavior of ReportUninterestingCall(). 389 const bool need_to_report_uninteresting_call = 390 // If the user allows this uninteresting call, we print it 391 // only when they want informational messages. 392 reaction == kAllow ? LogIsVisible(kInfo) : 393 // If the user wants this to be a warning, we print 394 // it only when they want to see warnings. 395 reaction == kWarn 396 ? LogIsVisible(kWarning) 397 : 398 // Otherwise, the user wants this to be an error, and we 399 // should always print detailed information in the error. 400 true; 401 402 if (!need_to_report_uninteresting_call) { 403 // Perform the action without printing the call information. 404 return this->UntypedPerformDefaultAction( 405 untyped_args, "Function call: " + std::string(Name())); 406 } 407 408 // Warns about the uninteresting call. 409 ::std::stringstream ss; 410 this->UntypedDescribeUninterestingCall(untyped_args, &ss); 411 412 // Calculates the function result. 413 UntypedActionResultHolderBase* const result = 414 this->UntypedPerformDefaultAction(untyped_args, ss.str()); 415 416 // Prints the function result. 417 if (result != NULL) 418 result->PrintAsActionResult(&ss); 419 420 ReportUninterestingCall(reaction, ss.str()); 421 return result; 422 } 423 424 bool is_excessive = false; 425 ::std::stringstream ss; 426 ::std::stringstream why; 427 ::std::stringstream loc; 428 const void* untyped_action = NULL; 429 430 // The UntypedFindMatchingExpectation() function acquires and 431 // releases g_gmock_mutex. 432 const ExpectationBase* const untyped_expectation = 433 this->UntypedFindMatchingExpectation( 434 untyped_args, &untyped_action, &is_excessive, 435 &ss, &why); 436 const bool found = untyped_expectation != NULL; 437 438 // True iff we need to print the call's arguments and return value. 439 // This definition must be kept in sync with the uses of Expect() 440 // and Log() in this function. 441 const bool need_to_report_call = 442 !found || is_excessive || LogIsVisible(kInfo); 443 if (!need_to_report_call) { 444 // Perform the action without printing the call information. 445 return 446 untyped_action == NULL ? 447 this->UntypedPerformDefaultAction(untyped_args, "") : 448 this->UntypedPerformAction(untyped_action, untyped_args); 449 } 450 451 ss << " Function call: " << Name(); 452 this->UntypedPrintArgs(untyped_args, &ss); 453 454 // In case the action deletes a piece of the expectation, we 455 // generate the message beforehand. 456 if (found && !is_excessive) { 457 untyped_expectation->DescribeLocationTo(&loc); 458 } 459 460 UntypedActionResultHolderBase* const result = 461 untyped_action == NULL ? 462 this->UntypedPerformDefaultAction(untyped_args, ss.str()) : 463 this->UntypedPerformAction(untyped_action, untyped_args); 464 if (result != NULL) 465 result->PrintAsActionResult(&ss); 466 ss << "\n" << why.str(); 467 468 if (!found) { 469 // No expectation matches this call - reports a failure. 470 Expect(false, NULL, -1, ss.str()); 471 } else if (is_excessive) { 472 // We had an upper-bound violation and the failure message is in ss. 473 Expect(false, untyped_expectation->file(), 474 untyped_expectation->line(), ss.str()); 475 } else { 476 // We had an expected call and the matching expectation is 477 // described in ss. 478 Log(kInfo, loc.str() + ss.str(), 2); 479 } 480 481 return result; 482 } 483 484 // Returns an Expectation object that references and co-owns exp, 485 // which must be an expectation on this mock function. 486 Expectation UntypedFunctionMockerBase::GetHandleOf(ExpectationBase* exp) { 487 // See the definition of untyped_expectations_ for why access to it 488 // is unprotected here. 489 for (UntypedExpectations::const_iterator it = 490 untyped_expectations_.begin(); 491 it != untyped_expectations_.end(); ++it) { 492 if (it->get() == exp) { 493 return Expectation(*it); 494 } 495 } 496 497 Assert(false, __FILE__, __LINE__, "Cannot find expectation."); 498 return Expectation(); 499 // The above statement is just to make the code compile, and will 500 // never be executed. 501 } 502 503 // Verifies that all expectations on this mock function have been 504 // satisfied. Reports one or more Google Test non-fatal failures 505 // and returns false if not. 506 bool UntypedFunctionMockerBase::VerifyAndClearExpectationsLocked() 507 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { 508 g_gmock_mutex.AssertHeld(); 509 bool expectations_met = true; 510 for (UntypedExpectations::const_iterator it = 511 untyped_expectations_.begin(); 512 it != untyped_expectations_.end(); ++it) { 513 ExpectationBase* const untyped_expectation = it->get(); 514 if (untyped_expectation->IsOverSaturated()) { 515 // There was an upper-bound violation. Since the error was 516 // already reported when it occurred, there is no need to do 517 // anything here. 518 expectations_met = false; 519 } else if (!untyped_expectation->IsSatisfied()) { 520 expectations_met = false; 521 ::std::stringstream ss; 522 ss << "Actual function call count doesn't match " 523 << untyped_expectation->source_text() << "...\n"; 524 // No need to show the source file location of the expectation 525 // in the description, as the Expect() call that follows already 526 // takes care of it. 527 untyped_expectation->MaybeDescribeExtraMatcherTo(&ss); 528 untyped_expectation->DescribeCallCountTo(&ss); 529 Expect(false, untyped_expectation->file(), 530 untyped_expectation->line(), ss.str()); 531 } 532 } 533 534 // Deleting our expectations may trigger other mock objects to be deleted, for 535 // example if an action contains a reference counted smart pointer to that 536 // mock object, and that is the last reference. So if we delete our 537 // expectations within the context of the global mutex we may deadlock when 538 // this method is called again. Instead, make a copy of the set of 539 // expectations to delete, clear our set within the mutex, and then clear the 540 // copied set outside of it. 541 UntypedExpectations expectations_to_delete; 542 untyped_expectations_.swap(expectations_to_delete); 543 544 g_gmock_mutex.Unlock(); 545 expectations_to_delete.clear(); 546 g_gmock_mutex.Lock(); 547 548 return expectations_met; 549 } 550 551 CallReaction intToCallReaction(int mock_behavior) { 552 if (mock_behavior >= kAllow && mock_behavior <= kFail) { 553 return static_cast<internal::CallReaction>(mock_behavior); 554 } 555 return kWarn; 556 } 557 558 } // namespace internal 559 560 // Class Mock. 561 562 namespace { 563 564 typedef std::set<internal::UntypedFunctionMockerBase*> FunctionMockers; 565 566 // The current state of a mock object. Such information is needed for 567 // detecting leaked mock objects and explicitly verifying a mock's 568 // expectations. 569 struct MockObjectState { 570 MockObjectState() 571 : first_used_file(NULL), first_used_line(-1), leakable(false) {} 572 573 // Where in the source file an ON_CALL or EXPECT_CALL is first 574 // invoked on this mock object. 575 const char* first_used_file; 576 int first_used_line; 577 ::std::string first_used_test_case; 578 ::std::string first_used_test; 579 bool leakable; // true iff it's OK to leak the object. 580 FunctionMockers function_mockers; // All registered methods of the object. 581 }; 582 583 // A global registry holding the state of all mock objects that are 584 // alive. A mock object is added to this registry the first time 585 // Mock::AllowLeak(), ON_CALL(), or EXPECT_CALL() is called on it. It 586 // is removed from the registry in the mock object's destructor. 587 class MockObjectRegistry { 588 public: 589 // Maps a mock object (identified by its address) to its state. 590 typedef std::map<const void*, MockObjectState> StateMap; 591 592 // This destructor will be called when a program exits, after all 593 // tests in it have been run. By then, there should be no mock 594 // object alive. Therefore we report any living object as test 595 // failure, unless the user explicitly asked us to ignore it. 596 ~MockObjectRegistry() { 597 // "using ::std::cout;" doesn't work with Symbian's STLport, where cout is 598 // a macro. 599 600 if (!GMOCK_FLAG(catch_leaked_mocks)) 601 return; 602 603 int leaked_count = 0; 604 for (StateMap::const_iterator it = states_.begin(); it != states_.end(); 605 ++it) { 606 if (it->second.leakable) // The user said it's fine to leak this object. 607 continue; 608 609 // FIXME: Print the type of the leaked object. 610 // This can help the user identify the leaked object. 611 std::cout << "\n"; 612 const MockObjectState& state = it->second; 613 std::cout << internal::FormatFileLocation(state.first_used_file, 614 state.first_used_line); 615 std::cout << " ERROR: this mock object"; 616 if (state.first_used_test != "") { 617 std::cout << " (used in test " << state.first_used_test_case << "." 618 << state.first_used_test << ")"; 619 } 620 std::cout << " should be deleted but never is. Its address is @" 621 << it->first << "."; 622 leaked_count++; 623 } 624 if (leaked_count > 0) { 625 std::cout << "\nERROR: " << leaked_count << " leaked mock " 626 << (leaked_count == 1 ? "object" : "objects") 627 << " found at program exit. Expectations on a mock object is " 628 "verified when the object is destructed. Leaking a mock " 629 "means that its expectations aren't verified, which is " 630 "usually a test bug. If you really intend to leak a mock, " 631 "you can suppress this error using " 632 "testing::Mock::AllowLeak(mock_object), or you may use a " 633 "fake or stub instead of a mock.\n"; 634 std::cout.flush(); 635 ::std::cerr.flush(); 636 // RUN_ALL_TESTS() has already returned when this destructor is 637 // called. Therefore we cannot use the normal Google Test 638 // failure reporting mechanism. 639 _exit(1); // We cannot call exit() as it is not reentrant and 640 // may already have been called. 641 } 642 } 643 644 StateMap& states() { return states_; } 645 646 private: 647 StateMap states_; 648 }; 649 650 // Protected by g_gmock_mutex. 651 MockObjectRegistry g_mock_object_registry; 652 653 // Maps a mock object to the reaction Google Mock should have when an 654 // uninteresting method is called. Protected by g_gmock_mutex. 655 std::map<const void*, internal::CallReaction> g_uninteresting_call_reaction; 656 657 // Sets the reaction Google Mock should have when an uninteresting 658 // method of the given mock object is called. 659 void SetReactionOnUninterestingCalls(const void* mock_obj, 660 internal::CallReaction reaction) 661 GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { 662 internal::MutexLock l(&internal::g_gmock_mutex); 663 g_uninteresting_call_reaction[mock_obj] = reaction; 664 } 665 666 } // namespace 667 668 // Tells Google Mock to allow uninteresting calls on the given mock 669 // object. 670 void Mock::AllowUninterestingCalls(const void* mock_obj) 671 GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { 672 SetReactionOnUninterestingCalls(mock_obj, internal::kAllow); 673 } 674 675 // Tells Google Mock to warn the user about uninteresting calls on the 676 // given mock object. 677 void Mock::WarnUninterestingCalls(const void* mock_obj) 678 GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { 679 SetReactionOnUninterestingCalls(mock_obj, internal::kWarn); 680 } 681 682 // Tells Google Mock to fail uninteresting calls on the given mock 683 // object. 684 void Mock::FailUninterestingCalls(const void* mock_obj) 685 GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { 686 SetReactionOnUninterestingCalls(mock_obj, internal::kFail); 687 } 688 689 // Tells Google Mock the given mock object is being destroyed and its 690 // entry in the call-reaction table should be removed. 691 void Mock::UnregisterCallReaction(const void* mock_obj) 692 GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { 693 internal::MutexLock l(&internal::g_gmock_mutex); 694 g_uninteresting_call_reaction.erase(mock_obj); 695 } 696 697 // Returns the reaction Google Mock will have on uninteresting calls 698 // made on the given mock object. 699 internal::CallReaction Mock::GetReactionOnUninterestingCalls( 700 const void* mock_obj) 701 GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { 702 internal::MutexLock l(&internal::g_gmock_mutex); 703 return (g_uninteresting_call_reaction.count(mock_obj) == 0) ? 704 internal::intToCallReaction(GMOCK_FLAG(default_mock_behavior)) : 705 g_uninteresting_call_reaction[mock_obj]; 706 } 707 708 // Tells Google Mock to ignore mock_obj when checking for leaked mock 709 // objects. 710 void Mock::AllowLeak(const void* mock_obj) 711 GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { 712 internal::MutexLock l(&internal::g_gmock_mutex); 713 g_mock_object_registry.states()[mock_obj].leakable = true; 714 } 715 716 // Verifies and clears all expectations on the given mock object. If 717 // the expectations aren't satisfied, generates one or more Google 718 // Test non-fatal failures and returns false. 719 bool Mock::VerifyAndClearExpectations(void* mock_obj) 720 GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { 721 internal::MutexLock l(&internal::g_gmock_mutex); 722 return VerifyAndClearExpectationsLocked(mock_obj); 723 } 724 725 // Verifies all expectations on the given mock object and clears its 726 // default actions and expectations. Returns true iff the 727 // verification was successful. 728 bool Mock::VerifyAndClear(void* mock_obj) 729 GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { 730 internal::MutexLock l(&internal::g_gmock_mutex); 731 ClearDefaultActionsLocked(mock_obj); 732 return VerifyAndClearExpectationsLocked(mock_obj); 733 } 734 735 // Verifies and clears all expectations on the given mock object. If 736 // the expectations aren't satisfied, generates one or more Google 737 // Test non-fatal failures and returns false. 738 bool Mock::VerifyAndClearExpectationsLocked(void* mock_obj) 739 GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex) { 740 internal::g_gmock_mutex.AssertHeld(); 741 if (g_mock_object_registry.states().count(mock_obj) == 0) { 742 // No EXPECT_CALL() was set on the given mock object. 743 return true; 744 } 745 746 // Verifies and clears the expectations on each mock method in the 747 // given mock object. 748 bool expectations_met = true; 749 FunctionMockers& mockers = 750 g_mock_object_registry.states()[mock_obj].function_mockers; 751 for (FunctionMockers::const_iterator it = mockers.begin(); 752 it != mockers.end(); ++it) { 753 if (!(*it)->VerifyAndClearExpectationsLocked()) { 754 expectations_met = false; 755 } 756 } 757 758 // We don't clear the content of mockers, as they may still be 759 // needed by ClearDefaultActionsLocked(). 760 return expectations_met; 761 } 762 763 // Registers a mock object and a mock method it owns. 764 void Mock::Register(const void* mock_obj, 765 internal::UntypedFunctionMockerBase* mocker) 766 GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { 767 internal::MutexLock l(&internal::g_gmock_mutex); 768 g_mock_object_registry.states()[mock_obj].function_mockers.insert(mocker); 769 } 770 771 // Tells Google Mock where in the source code mock_obj is used in an 772 // ON_CALL or EXPECT_CALL. In case mock_obj is leaked, this 773 // information helps the user identify which object it is. 774 void Mock::RegisterUseByOnCallOrExpectCall(const void* mock_obj, 775 const char* file, int line) 776 GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { 777 internal::MutexLock l(&internal::g_gmock_mutex); 778 MockObjectState& state = g_mock_object_registry.states()[mock_obj]; 779 if (state.first_used_file == NULL) { 780 state.first_used_file = file; 781 state.first_used_line = line; 782 const TestInfo* const test_info = 783 UnitTest::GetInstance()->current_test_info(); 784 if (test_info != NULL) { 785 // FIXME: record the test case name when the 786 // ON_CALL or EXPECT_CALL is invoked from SetUpTestCase() or 787 // TearDownTestCase(). 788 state.first_used_test_case = test_info->test_case_name(); 789 state.first_used_test = test_info->name(); 790 } 791 } 792 } 793 794 // Unregisters a mock method; removes the owning mock object from the 795 // registry when the last mock method associated with it has been 796 // unregistered. This is called only in the destructor of 797 // FunctionMockerBase. 798 void Mock::UnregisterLocked(internal::UntypedFunctionMockerBase* mocker) 799 GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex) { 800 internal::g_gmock_mutex.AssertHeld(); 801 for (MockObjectRegistry::StateMap::iterator it = 802 g_mock_object_registry.states().begin(); 803 it != g_mock_object_registry.states().end(); ++it) { 804 FunctionMockers& mockers = it->second.function_mockers; 805 if (mockers.erase(mocker) > 0) { 806 // mocker was in mockers and has been just removed. 807 if (mockers.empty()) { 808 g_mock_object_registry.states().erase(it); 809 } 810 return; 811 } 812 } 813 } 814 815 // Clears all ON_CALL()s set on the given mock object. 816 void Mock::ClearDefaultActionsLocked(void* mock_obj) 817 GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex) { 818 internal::g_gmock_mutex.AssertHeld(); 819 820 if (g_mock_object_registry.states().count(mock_obj) == 0) { 821 // No ON_CALL() was set on the given mock object. 822 return; 823 } 824 825 // Clears the default actions for each mock method in the given mock 826 // object. 827 FunctionMockers& mockers = 828 g_mock_object_registry.states()[mock_obj].function_mockers; 829 for (FunctionMockers::const_iterator it = mockers.begin(); 830 it != mockers.end(); ++it) { 831 (*it)->ClearDefaultActionsLocked(); 832 } 833 834 // We don't clear the content of mockers, as they may still be 835 // needed by VerifyAndClearExpectationsLocked(). 836 } 837 838 Expectation::Expectation() {} 839 840 Expectation::Expectation( 841 const internal::linked_ptr<internal::ExpectationBase>& an_expectation_base) 842 : expectation_base_(an_expectation_base) {} 843 844 Expectation::~Expectation() {} 845 846 // Adds an expectation to a sequence. 847 void Sequence::AddExpectation(const Expectation& expectation) const { 848 if (*last_expectation_ != expectation) { 849 if (last_expectation_->expectation_base() != NULL) { 850 expectation.expectation_base()->immediate_prerequisites_ 851 += *last_expectation_; 852 } 853 *last_expectation_ = expectation; 854 } 855 } 856 857 // Creates the implicit sequence if there isn't one. 858 InSequence::InSequence() { 859 if (internal::g_gmock_implicit_sequence.get() == NULL) { 860 internal::g_gmock_implicit_sequence.set(new Sequence); 861 sequence_created_ = true; 862 } else { 863 sequence_created_ = false; 864 } 865 } 866 867 // Deletes the implicit sequence if it was created by the constructor 868 // of this object. 869 InSequence::~InSequence() { 870 if (sequence_created_) { 871 delete internal::g_gmock_implicit_sequence.get(); 872 internal::g_gmock_implicit_sequence.set(NULL); 873 } 874 } 875 876 } // namespace testing 877 878 #ifdef _MSC_VER 879 #if _MSC_VER <= 1900 880 # pragma warning(pop) 881 #endif 882 #endif 883