48#if GTEST_OS_CYGWIN || GTEST_OS_LINUX || GTEST_OS_MAC
57# pragma warning(disable:4800)
70 const char* file,
int line,
71 const std::string& message) {
72 ::std::ostringstream
s;
73 s << file <<
":" << line <<
": " << message << ::std::endl;
74 Log(severity,
s.str(), 0);
79 const std::string& a_source_text)
82 source_text_(a_source_text),
83 cardinality_specified_(false),
84 cardinality_(Exactly(1)),
87 extra_matcher_specified_(false),
88 repeated_action_specified_(false),
89 retires_on_saturation_(false),
91 action_count_checked_(false) {}
112 ::std::vector<ExpectationBase*> expectations(1,
this);
113 while (!expectations.empty()) {
115 expectations.pop_back();
121 if (!next->is_retired()) {
123 expectations.push_back(next);
133 g_gmock_mutex.AssertHeld();
134 ::std::vector<const ExpectationBase*> expectations(1,
this);
135 while (!expectations.empty()) {
137 expectations.pop_back();
143 if (!next->IsSatisfied())
return false;
144 expectations.push_back(next);
153 g_gmock_mutex.AssertHeld();
154 ::std::vector<const ExpectationBase*> expectations(1,
this);
155 while (!expectations.empty()) {
157 expectations.pop_back();
164 if (next->IsSatisfied()) {
167 if (next->call_count_ == 0) {
168 expectations.push_back(next);
184 g_gmock_mutex.AssertHeld();
187 *
os <<
" Expected: to be ";
188 cardinality().DescribeTo(
os);
189 *
os <<
"\n Actual: ";
194 *
os <<
" - " << (IsOverSaturated() ?
"over-saturated" :
195 IsSaturated() ?
"saturated" :
196 IsSatisfied() ?
"satisfied" :
"unsatisfied")
198 << (is_retired() ?
"retired" :
"active");
207 bool should_check =
false;
229 if (action_count > upper_bound ||
232 }
else if (0 < action_count && action_count < lower_bound &&
239 ::std::stringstream ss;
241 ss <<
"Too " << (too_many ?
"many" :
"few")
242 <<
" actions specified in " <<
source_text() <<
"...\n"
243 <<
"Expected to be ";
245 ss <<
", but has " << (too_many ?
"" :
"only ")
246 << action_count <<
" WillOnce()"
247 << (action_count == 1 ?
"" :
"s");
249 ss <<
" and a WillRepeatedly()";
260 ".Times() cannot appear "
261 "more than once in an EXPECT_CALL().");
264 ".Times() cannot appear after "
265 ".InSequence(), .WillOnce(), .WillRepeatedly(), "
266 "or .RetiresOnSaturation().");
281 const int stack_frames_to_skip =
282 GMOCK_FLAG(verbose) == kInfoVerbosity ? 3 : -1;
285 Log(
kInfo, msg, stack_frames_to_skip);
290 "\nNOTE: You can safely ignore the above warning unless this "
291 "call should not happen. Do not suppress it by blindly adding "
292 "an EXPECT_CALL() if you don't mean to enforce the call. "
294 "https://github.com/google/googletest/blob/master/googlemock/"
296 "knowing-when-to-expect for details.\n",
297 stack_frames_to_skip);
300 Expect(
false, NULL, -1, msg);
305 : mock_obj_(NULL), name_(
"") {}
317 mock_obj_ = mock_obj;
319 Mock::Register(mock_obj,
this);
331 mock_obj_ = mock_obj;
339 const void* mock_obj;
345 "MockObject() must not be called before RegisterOwner() or "
346 "SetOwnerAndName() has been called.");
362 "Name() must not be called before SetOwnerAndName() has "
376 if (untyped_expectations_.size() == 0) {
385 Mock::GetReactionOnUninterestingCalls(MockObject());
390 const bool need_to_report_uninteresting_call =
403 if (!need_to_report_uninteresting_call) {
405 return this->UntypedPerformDefaultAction(
406 untyped_args,
"Function call: " + std::string(Name()));
410 ::std::stringstream ss;
411 this->UntypedDescribeUninterestingCall(untyped_args, &ss);
415 this->UntypedPerformDefaultAction(untyped_args, ss.str());
419 result->PrintAsActionResult(&ss);
425 bool is_excessive =
false;
426 ::std::stringstream ss;
427 ::std::stringstream why;
428 ::std::stringstream loc;
429 const void* untyped_action = NULL;
434 this->UntypedFindMatchingExpectation(
435 untyped_args, &untyped_action, &is_excessive,
437 const bool found = untyped_expectation != NULL;
442 const bool need_to_report_call =
444 if (!need_to_report_call) {
447 untyped_action == NULL ?
448 this->UntypedPerformDefaultAction(untyped_args,
"") :
449 this->UntypedPerformAction(untyped_action, untyped_args);
452 ss <<
" Function call: " << Name();
453 this->UntypedPrintArgs(untyped_args, &ss);
457 if (found && !is_excessive) {
462 untyped_action == NULL ?
463 this->UntypedPerformDefaultAction(untyped_args, ss.str()) :
464 this->UntypedPerformAction(untyped_action, untyped_args);
466 result->PrintAsActionResult(&ss);
467 ss <<
"\n" << why.str();
471 Expect(
false, NULL, -1, ss.str());
472 }
else if (is_excessive) {
475 untyped_expectation->
line(), ss.str());
479 Log(
kInfo, loc.str() + ss.str(), 2);
490 for (UntypedExpectations::const_iterator it =
493 if (it->get() == exp) {
498 Assert(
false, __FILE__, __LINE__,
"Cannot find expectation.");
509 g_gmock_mutex.AssertHeld();
510 bool expectations_met =
true;
511 for (UntypedExpectations::const_iterator it =
514 ExpectationBase* const untyped_expectation = it->get();
515 if (untyped_expectation->IsOverSaturated()) {
519 expectations_met = false;
520 }
else if (!untyped_expectation->IsSatisfied()) {
521 expectations_met = false;
522 ::std::stringstream ss;
523 ss <<
"Actual function call count doesn't match "
524 << untyped_expectation->source_text() <<
"...\n";
528 untyped_expectation->MaybeDescribeExtraMatcherTo(&ss);
529 untyped_expectation->DescribeCallCountTo(&ss);
530 Expect(false, untyped_expectation->file(),
531 untyped_expectation->line(), ss.str());
542 UntypedExpectations expectations_to_delete;
543 untyped_expectations_.swap(expectations_to_delete);
545 g_gmock_mutex.Unlock();
546 expectations_to_delete.clear();
547 g_gmock_mutex.Lock();
549 return expectations_met;
553 if (mock_behavior >= kAllow && mock_behavior <= kFail) {
565typedef std::set<internal::UntypedFunctionMockerBase*> FunctionMockers;
570struct MockObjectState {
588class MockObjectRegistry {
591 typedef std::map<const void*, MockObjectState> StateMap;
597 ~MockObjectRegistry() {
604 int leaked_count = 0;
605 for (StateMap::const_iterator it = states_.begin(); it != states_.end();
607 if (it->second.leakable)
613 const MockObjectState&
state = it->second;
614 std::cout << internal::FormatFileLocation(
state.first_used_file,
615 state.first_used_line);
616 std::cout <<
" ERROR: this mock object";
617 if (
state.first_used_test !=
"") {
618 std::cout <<
" (used in test " <<
state.first_used_test_case <<
"."
619 <<
state.first_used_test <<
")";
621 std::cout <<
" should be deleted but never is. Its address is @"
625 if (leaked_count > 0) {
626 std::cout <<
"\nERROR: " << leaked_count <<
" leaked mock "
627 << (leaked_count == 1 ?
"object" :
"objects")
628 <<
" found at program exit. Expectations on a mock object is "
629 "verified when the object is destructed. Leaking a mock "
630 "means that its expectations aren't verified, which is "
631 "usually a test bug. If you really intend to leak a mock, "
632 "you can suppress this error using "
633 "testing::Mock::AllowLeak(mock_object), or you may use a "
634 "fake or stub instead of a mock.\n";
645 StateMap& states() {
return states_; }
652MockObjectRegistry g_mock_object_registry;
656std::map<const void*, internal::CallReaction> g_uninteresting_call_reaction;
660void SetReactionOnUninterestingCalls(
const void* mock_obj,
661 internal::CallReaction reaction)
663 internal::MutexLock
l(&internal::g_gmock_mutex);
664 g_uninteresting_call_reaction[mock_obj] = reaction;
671void Mock::AllowUninterestingCalls(
const void* mock_obj)
673 SetReactionOnUninterestingCalls(mock_obj, internal::kAllow);
678void Mock::WarnUninterestingCalls(
const void* mock_obj)
680 SetReactionOnUninterestingCalls(mock_obj, internal::kWarn);
685void Mock::FailUninterestingCalls(
const void* mock_obj)
687 SetReactionOnUninterestingCalls(mock_obj, internal::kFail);
692void Mock::UnregisterCallReaction(
const void* mock_obj)
694 internal::MutexLock
l(&internal::g_gmock_mutex);
695 g_uninteresting_call_reaction.erase(mock_obj);
700internal::CallReaction Mock::GetReactionOnUninterestingCalls(
701 const void* mock_obj)
703 internal::MutexLock
l(&internal::g_gmock_mutex);
704 return (g_uninteresting_call_reaction.count(mock_obj) == 0) ?
705 internal::intToCallReaction(
GMOCK_FLAG(default_mock_behavior)) :
706 g_uninteresting_call_reaction[mock_obj];
711void Mock::AllowLeak(
const void* mock_obj)
713 internal::MutexLock
l(&internal::g_gmock_mutex);
714 g_mock_object_registry.states()[mock_obj].leakable =
true;
720bool Mock::VerifyAndClearExpectations(
void* mock_obj)
722 internal::MutexLock
l(&internal::g_gmock_mutex);
723 return VerifyAndClearExpectationsLocked(mock_obj);
729bool Mock::VerifyAndClear(
void* mock_obj)
731 internal::MutexLock
l(&internal::g_gmock_mutex);
732 ClearDefaultActionsLocked(mock_obj);
733 return VerifyAndClearExpectationsLocked(mock_obj);
739bool Mock::VerifyAndClearExpectationsLocked(
void* mock_obj)
741 internal::g_gmock_mutex.AssertHeld();
742 if (g_mock_object_registry.states().count(mock_obj) == 0) {
749 bool expectations_met =
true;
750 FunctionMockers& mockers =
751 g_mock_object_registry.states()[mock_obj].function_mockers;
752 for (FunctionMockers::const_iterator it = mockers.begin();
753 it != mockers.end(); ++it) {
754 if (!(*it)->VerifyAndClearExpectationsLocked()) {
755 expectations_met =
false;
761 return expectations_met;
765void Mock::Register(
const void* mock_obj,
766 internal::UntypedFunctionMockerBase* mocker)
768 internal::MutexLock
l(&internal::g_gmock_mutex);
769 g_mock_object_registry.states()[mock_obj].function_mockers.insert(mocker);
775void Mock::RegisterUseByOnCallOrExpectCall(
const void* mock_obj,
776 const char* file,
int line)
778 internal::MutexLock
l(&internal::g_gmock_mutex);
779 MockObjectState&
state = g_mock_object_registry.states()[mock_obj];
780 if (
state.first_used_file == NULL) {
781 state.first_used_file = file;
782 state.first_used_line = line;
783 const TestInfo*
const test_info =
784 UnitTest::GetInstance()->current_test_info();
785 if (test_info != NULL) {
789 state.first_used_test_case = test_info->test_case_name();
790 state.first_used_test = test_info->name();
799void Mock::UnregisterLocked(internal::UntypedFunctionMockerBase* mocker)
801 internal::g_gmock_mutex.AssertHeld();
802 for (MockObjectRegistry::StateMap::iterator it =
803 g_mock_object_registry.states().begin();
804 it != g_mock_object_registry.states().end(); ++it) {
805 FunctionMockers& mockers = it->second.function_mockers;
806 if (mockers.erase(mocker) > 0) {
808 if (mockers.empty()) {
809 g_mock_object_registry.states().erase(it);
817void Mock::ClearDefaultActionsLocked(
void* mock_obj)
819 internal::g_gmock_mutex.AssertHeld();
821 if (g_mock_object_registry.states().count(mock_obj) == 0) {
828 FunctionMockers& mockers =
829 g_mock_object_registry.states()[mock_obj].function_mockers;
830 for (FunctionMockers::const_iterator it = mockers.begin();
831 it != mockers.end(); ++it) {
832 (*it)->ClearDefaultActionsLocked();
839Expectation::Expectation() {}
841Expectation::Expectation(
843 : expectation_base_(an_expectation_base) {}
849 if (*last_expectation_ != expectation) {
850 if (last_expectation_->expectation_base() != NULL) {
851 expectation.expectation_base()->immediate_prerequisites_
852 += *last_expectation_;
854 *last_expectation_ = expectation;
862 sequence_created_ =
true;
864 sequence_created_ =
false;
871 if (sequence_created_) {
void DescribeTo(::std::ostream *os) const
static void DescribeActualCallCountTo(int actual_call_count, ::std::ostream *os)
int ConservativeLowerBound() const
int ConservativeUpperBound() const
Expectation::Set::const_iterator const_iterator
const_iterator end() const
const_iterator begin() const
void AddExpectation(const Expectation &expectation) const
void UntypedTimes(const Cardinality &a_cardinality)
ExpectationSet immediate_prerequisites_
bool action_count_checked_
void DescribeCallCountTo(::std::ostream *os) const GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
const char * source_text() const
void SpecifyCardinality(const Cardinality &cardinality)
const char * file() const
void DescribeLocationTo(::std::ostream *os) const
ExpectationBase(const char *file, int line, const std::string &source_text)
bool AllPrerequisitesAreSatisfied() const GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
UntypedActions untyped_actions_
void FindUnsatisfiedPrerequisites(ExpectationSet *result) const GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
void CheckActionCountIfNotDone() const GTEST_LOCK_EXCLUDED_(mutex_)
bool repeated_action_specified_
bool cardinality_specified_
const Cardinality & cardinality() const
void ExpectSpecProperty(bool property, const std::string &failure_message) const
void RetireAllPreRequisites() GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
bool is_retired() const GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
virtual ~ExpectationBase()
void RegisterOwner(const void *mock_obj) GTEST_LOCK_EXCLUDED_(g_gmock_mutex)
void SetOwnerAndName(const void *mock_obj, const char *name) GTEST_LOCK_EXCLUDED_(g_gmock_mutex)
bool VerifyAndClearExpectationsLocked() GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex)
virtual ~UntypedFunctionMockerBase()
const void * MockObject() const GTEST_LOCK_EXCLUDED_(g_gmock_mutex)
Expectation GetHandleOf(ExpectationBase *exp)
UntypedActionResultHolderBase * UntypedInvokeWith(void *untyped_args) GTEST_LOCK_EXCLUDED_(g_gmock_mutex)
UntypedExpectations untyped_expectations_
const char * Name() const GTEST_LOCK_EXCLUDED_(g_gmock_mutex)
UntypedFunctionMockerBase()
const char * first_used_file
::std::string first_used_test
FunctionMockers function_mockers
::std::string first_used_test_case
#define GTEST_EXCLUSIVE_LOCK_REQUIRED_(locks)
#define GTEST_LOCK_EXCLUDED_(locks)
#define GTEST_DEFINE_STATIC_MUTEX_(mutex)
GTEST_API_ void LogWithLocation(testing::internal::LogSeverity severity, const char *file, int line, const std::string &message)
CallReaction intToCallReaction(int mock_behavior)
GTEST_API_ bool LogIsVisible(LogSeverity severity)
void Assert(bool condition, const char *file, int line)
void Expect(bool condition, const char *file, int line, const std::string &msg)
void ReportUninterestingCall(CallReaction reaction, const std::string &msg)
GTEST_API_ ThreadLocal< Sequence * > g_gmock_implicit_sequence