44 #include "gmock/gmock.h" 45 #include "gtest/gtest.h" 47 #if GTEST_OS_CYGWIN || GTEST_OS_LINUX || GTEST_OS_MAC 60 const char* file,
int line,
62 ::std::ostringstream
s;
63 s << file <<
":" << line <<
": " << message << ::std::endl;
64 Log(severity, s.str(), 0);
70 const string& a_source_text)
73 source_text_(a_source_text),
74 cardinality_specified_(false),
78 extra_matcher_specified_(false),
79 repeated_action_specified_(false),
80 retires_on_saturation_(false),
82 action_count_checked_(false) {}
85 ExpectationBase::~ExpectationBase() {}
89 void ExpectationBase::SpecifyCardinality(
const Cardinality& a_cardinality) {
90 cardinality_specified_ =
true;
91 cardinality_ = a_cardinality;
95 void ExpectationBase::RetireAllPreRequisites()
103 for (ExpectationSet::const_iterator it = immediate_prerequisites_.begin();
104 it != immediate_prerequisites_.end(); ++it) {
105 ExpectationBase*
const prerequisite = it->expectation_base().get();
106 if (!prerequisite->is_retired()) {
107 prerequisite->RetireAllPreRequisites();
108 prerequisite->Retire();
115 bool ExpectationBase::AllPrerequisitesAreSatisfied() const
117 g_gmock_mutex.AssertHeld();
118 for (ExpectationSet::const_iterator it = immediate_prerequisites_.begin();
119 it != immediate_prerequisites_.end(); ++it) {
120 if (!(it->expectation_base()->IsSatisfied()) ||
121 !(it->expectation_base()->AllPrerequisitesAreSatisfied()))
128 void ExpectationBase::FindUnsatisfiedPrerequisites(ExpectationSet* result)
const 130 g_gmock_mutex.AssertHeld();
131 for (ExpectationSet::const_iterator it = immediate_prerequisites_.begin();
132 it != immediate_prerequisites_.end(); ++it) {
133 if (it->expectation_base()->IsSatisfied()) {
136 if (it->expectation_base()->call_count_ == 0) {
137 it->expectation_base()->FindUnsatisfiedPrerequisites(result);
150 void ExpectationBase::DescribeCallCountTo(::std::ostream* os)
const 152 g_gmock_mutex.AssertHeld();
155 *os <<
" Expected: to be ";
156 cardinality().DescribeTo(os);
157 *os <<
"\n Actual: ";
158 Cardinality::DescribeActualCallCountTo(call_count(), os);
162 *os <<
" - " << (IsOverSaturated() ?
"over-saturated" :
163 IsSaturated() ?
"saturated" :
164 IsSatisfied() ?
"satisfied" :
"unsatisfied")
166 << (is_retired() ?
"retired" :
"active");
173 void ExpectationBase::CheckActionCountIfNotDone() const
175 bool should_check =
false;
178 if (!action_count_checked_) {
179 action_count_checked_ =
true;
185 if (!cardinality_specified_) {
192 const int action_count =
static_cast<int>(untyped_actions_.size());
193 const int upper_bound = cardinality().ConservativeUpperBound();
194 const int lower_bound = cardinality().ConservativeLowerBound();
197 if (action_count > upper_bound ||
198 (action_count == upper_bound && repeated_action_specified_)) {
200 }
else if (0 < action_count && action_count < lower_bound &&
201 !repeated_action_specified_) {
207 ::std::stringstream ss;
208 DescribeLocationTo(&ss);
209 ss <<
"Too " << (too_many ?
"many" :
"few")
210 <<
" actions specified in " << source_text() <<
"...\n" 211 <<
"Expected to be ";
212 cardinality().DescribeTo(&ss);
213 ss <<
", but has " << (too_many ?
"" :
"only ")
214 << action_count <<
" WillOnce()" 215 << (action_count == 1 ?
"" :
"s");
216 if (repeated_action_specified_) {
217 ss <<
" and a WillRepeatedly()";
225 void ExpectationBase::UntypedTimes(
const Cardinality& a_cardinality) {
226 if (last_clause_ == kTimes) {
227 ExpectSpecProperty(
false,
228 ".Times() cannot appear " 229 "more than once in an EXPECT_CALL().");
231 ExpectSpecProperty(last_clause_ < kTimes,
232 ".Times() cannot appear after " 233 ".InSequence(), .WillOnce(), .WillRepeatedly(), " 234 "or .RetiresOnSaturation().");
236 last_clause_ = kTimes;
238 SpecifyCardinality(a_cardinality);
256 Expect(
false, NULL, -1, msg);
260 UntypedFunctionMockerBase::UntypedFunctionMockerBase()
261 : mock_obj_(NULL),
name_(
"") {}
263 UntypedFunctionMockerBase::~UntypedFunctionMockerBase() {}
269 void UntypedFunctionMockerBase::RegisterOwner(
const void* mock_obj)
273 mock_obj_ = mock_obj;
275 Mock::Register(mock_obj,
this);
281 void UntypedFunctionMockerBase::SetOwnerAndName(
const void* mock_obj,
287 mock_obj_ = mock_obj;
293 const void* UntypedFunctionMockerBase::MockObject() const
295 const void* mock_obj;
300 Assert(mock_obj_ != NULL, __FILE__, __LINE__,
301 "MockObject() must not be called before RegisterOwner() or " 302 "SetOwnerAndName() has been called.");
303 mock_obj = mock_obj_;
310 const char* UntypedFunctionMockerBase::Name() const
318 "Name() must not be called before SetOwnerAndName() has " 328 const UntypedActionResultHolderBase*
329 UntypedFunctionMockerBase::UntypedInvokeWith(
const void*
const untyped_args)
331 if (untyped_expectations_.size() == 0) {
340 Mock::GetReactionOnUninterestingCalls(MockObject());
345 const bool need_to_report_uninteresting_call =
356 if (!need_to_report_uninteresting_call) {
358 return this->UntypedPerformDefaultAction(untyped_args,
"");
362 ::std::stringstream ss;
363 this->UntypedDescribeUninterestingCall(untyped_args, &ss);
366 const UntypedActionResultHolderBase*
const result =
367 this->UntypedPerformDefaultAction(untyped_args, ss.str());
371 result->PrintAsActionResult(&ss);
377 bool is_excessive =
false;
378 ::std::stringstream ss;
379 ::std::stringstream why;
380 ::std::stringstream loc;
381 const void* untyped_action = NULL;
385 const ExpectationBase*
const untyped_expectation =
386 this->UntypedFindMatchingExpectation(
387 untyped_args, &untyped_action, &is_excessive,
389 const bool found = untyped_expectation != NULL;
394 const bool need_to_report_call =
396 if (!need_to_report_call) {
399 untyped_action == NULL ?
400 this->UntypedPerformDefaultAction(untyped_args,
"") :
401 this->UntypedPerformAction(untyped_action, untyped_args);
404 ss <<
" Function call: " << Name();
405 this->UntypedPrintArgs(untyped_args, &ss);
409 if (found && !is_excessive) {
410 untyped_expectation->DescribeLocationTo(&loc);
413 const UntypedActionResultHolderBase*
const result =
414 untyped_action == NULL ?
415 this->UntypedPerformDefaultAction(untyped_args, ss.str()) :
416 this->UntypedPerformAction(untyped_action, untyped_args);
418 result->PrintAsActionResult(&ss);
419 ss <<
"\n" << why.str();
423 Expect(
false, NULL, -1, ss.str());
424 }
else if (is_excessive) {
426 Expect(
false, untyped_expectation->file(),
427 untyped_expectation->line(), ss.str());
431 Log(
kInfo, loc.str() + ss.str(), 2);
439 Expectation UntypedFunctionMockerBase::GetHandleOf(ExpectationBase* exp) {
440 for (UntypedExpectations::const_iterator it =
441 untyped_expectations_.begin();
442 it != untyped_expectations_.end(); ++it) {
443 if (it->get() == exp) {
444 return Expectation(*it);
448 Assert(
false, __FILE__, __LINE__,
"Cannot find expectation.");
449 return Expectation();
457 bool UntypedFunctionMockerBase::VerifyAndClearExpectationsLocked()
459 g_gmock_mutex.AssertHeld();
460 bool expectations_met =
true;
461 for (UntypedExpectations::const_iterator it =
462 untyped_expectations_.begin();
463 it != untyped_expectations_.end(); ++it) {
464 ExpectationBase*
const untyped_expectation = it->get();
465 if (untyped_expectation->IsOverSaturated()) {
469 expectations_met =
false;
470 }
else if (!untyped_expectation->IsSatisfied()) {
471 expectations_met =
false;
472 ::std::stringstream ss;
473 ss <<
"Actual function call count doesn't match " 474 << untyped_expectation->source_text() <<
"...\n";
478 untyped_expectation->MaybeDescribeExtraMatcherTo(&ss);
479 untyped_expectation->DescribeCallCountTo(&ss);
480 Expect(
false, untyped_expectation->file(),
481 untyped_expectation->line(), ss.str());
492 UntypedExpectations expectations_to_delete;
493 untyped_expectations_.swap(expectations_to_delete);
495 g_gmock_mutex.Unlock();
496 expectations_to_delete.clear();
497 g_gmock_mutex.Lock();
499 return expectations_met;
508 typedef std::set<internal::UntypedFunctionMockerBase*> FunctionMockers;
513 struct MockObjectState {
531 class MockObjectRegistry {
534 typedef std::map<const void*, MockObjectState> StateMap;
540 ~MockObjectRegistry() {
547 int leaked_count = 0;
548 for (StateMap::const_iterator it =
states_.begin(); it !=
states_.end();
550 if (it->second.leakable)
556 const MockObjectState& state = it->second;
558 state.first_used_line);
559 std::cout <<
" ERROR: this mock object";
560 if (state.first_used_test !=
"") {
561 std::cout <<
" (used in test " << state.first_used_test_case <<
"." 562 << state.first_used_test <<
")";
564 std::cout <<
" should be deleted but never is. Its address is @" 568 if (leaked_count > 0) {
569 std::cout <<
"\nERROR: " << leaked_count
570 <<
" leaked mock " << (leaked_count == 1 ?
"object" :
"objects")
571 <<
" found at program exit.\n";
582 StateMap& states() {
return states_; }
589 MockObjectRegistry g_mock_object_registry;
593 std::map<const void*, internal::CallReaction> g_uninteresting_call_reaction;
597 void SetReactionOnUninterestingCalls(
const void* mock_obj,
601 g_uninteresting_call_reaction[mock_obj] = reaction;
608 void Mock::AllowUninterestingCalls(
const void* mock_obj)
615 void Mock::WarnUninterestingCalls(
const void* mock_obj)
622 void Mock::FailUninterestingCalls(
const void* mock_obj)
629 void Mock::UnregisterCallReaction(
const void* mock_obj)
632 g_uninteresting_call_reaction.erase(mock_obj);
638 const void* mock_obj)
641 return (g_uninteresting_call_reaction.count(mock_obj) == 0) ?
647 void Mock::AllowLeak(
const void* mock_obj)
650 g_mock_object_registry.states()[mock_obj].leakable =
true;
656 bool Mock::VerifyAndClearExpectations(
void* mock_obj)
659 return VerifyAndClearExpectationsLocked(mock_obj);
665 bool Mock::VerifyAndClear(
void* mock_obj)
668 ClearDefaultActionsLocked(mock_obj);
669 return VerifyAndClearExpectationsLocked(mock_obj);
675 bool Mock::VerifyAndClearExpectationsLocked(
void* mock_obj)
677 internal::g_gmock_mutex.AssertHeld();
678 if (g_mock_object_registry.states().count(mock_obj) == 0) {
685 bool expectations_met =
true;
686 FunctionMockers& mockers =
687 g_mock_object_registry.states()[mock_obj].function_mockers;
688 for (FunctionMockers::const_iterator it = mockers.begin();
689 it != mockers.end(); ++it) {
690 if (!(*it)->VerifyAndClearExpectationsLocked()) {
691 expectations_met =
false;
697 return expectations_met;
701 void Mock::Register(
const void* mock_obj,
702 internal::UntypedFunctionMockerBase* mocker)
705 g_mock_object_registry.states()[mock_obj].function_mockers.insert(mocker);
711 void Mock::RegisterUseByOnCallOrExpectCall(
const void* mock_obj,
712 const char* file,
int line)
715 MockObjectState& state = g_mock_object_registry.states()[mock_obj];
716 if (state.first_used_file == NULL) {
717 state.first_used_file = file;
718 state.first_used_line = line;
719 const TestInfo*
const test_info =
720 UnitTest::GetInstance()->current_test_info();
721 if (test_info != NULL) {
725 state.first_used_test_case = test_info->test_case_name();
726 state.first_used_test = test_info->name();
735 void Mock::UnregisterLocked(internal::UntypedFunctionMockerBase* mocker)
737 internal::g_gmock_mutex.AssertHeld();
738 for (MockObjectRegistry::StateMap::iterator it =
739 g_mock_object_registry.states().begin();
740 it != g_mock_object_registry.states().end(); ++it) {
741 FunctionMockers& mockers = it->second.function_mockers;
742 if (mockers.erase(mocker) > 0) {
744 if (mockers.empty()) {
745 g_mock_object_registry.states().erase(it);
753 void Mock::ClearDefaultActionsLocked(
void* mock_obj)
755 internal::g_gmock_mutex.AssertHeld();
757 if (g_mock_object_registry.states().count(mock_obj) == 0) {
764 FunctionMockers& mockers =
765 g_mock_object_registry.states()[mock_obj].function_mockers;
766 for (FunctionMockers::const_iterator it = mockers.begin();
767 it != mockers.end(); ++it) {
768 (*it)->ClearDefaultActionsLocked();
775 Expectation::Expectation() {}
777 Expectation::Expectation(
778 const internal::linked_ptr<internal::ExpectationBase>& an_expectation_base)
779 : expectation_base_(an_expectation_base) {}
781 Expectation::~Expectation() {}
784 void Sequence::AddExpectation(
const Expectation& expectation)
const {
785 if (*last_expectation_ != expectation) {
786 if (last_expectation_->expectation_base() != NULL) {
787 expectation.expectation_base()->immediate_prerequisites_
788 += *last_expectation_;
790 *last_expectation_ = expectation;
795 InSequence::InSequence() {
798 sequence_created_ =
true;
800 sequence_created_ =
false;
806 InSequence::~InSequence() {
807 if (sequence_created_) {
GTEST_API_ ThreadLocal< Sequence * > g_gmock_implicit_sequence
GTEST_API_::std::string FormatFileLocation(const char *file, int line)
ExpectationBase(const char *file, int line, const string &source_text)
::std::string first_used_test
void ReportUninterestingCall(CallReaction reaction, const string &msg)
GTEST_API_ GTEST_DEFINE_STATIC_MUTEX_(g_linked_ptr_mutex)
void Expect(bool condition, const char *file, int line, const string &msg)
#define GTEST_EXCLUSIVE_LOCK_REQUIRED_(locks)
GTEST_API_ void LogWithLocation(testing::internal::LogSeverity severity, const char *file, int line, const string &message)
GTEST_API_ void Log(LogSeverity severity, const string &message, int stack_frames_to_skip)
GTEST_API_ bool LogIsVisible(LogSeverity severity)
GTEST_API_ Cardinality Exactly(int n)
::std::string first_used_test_case
void Assert(bool condition, const char *file, int line)
const char * first_used_file
FunctionMockers function_mockers
#define GTEST_LOCK_EXCLUDED_(locks)