45 #include <unordered_map>
52 #if defined(GTEST_OS_CYGWIN) || defined(GTEST_OS_LINUX) || defined(GTEST_OS_MAC)
56 #include <qurt_event.h>
61 #if defined(_MSC_VER) && (_MSC_VER == 1900)
74 const char* file,
int line,
75 const std::string& message) {
76 ::std::ostringstream s;
79 Log(severity, s.str(), 0);
83 ExpectationBase::ExpectationBase(
const char* a_file,
int a_line,
84 const std::string& a_source_text)
87 source_text_(a_source_text),
88 cardinality_specified_(
false),
92 extra_matcher_specified_(
false),
93 repeated_action_specified_(
false),
94 retires_on_saturation_(
false),
96 action_count_checked_(
false) {}
99 ExpectationBase::~ExpectationBase() =
default;
103 void ExpectationBase::SpecifyCardinality(
const Cardinality& a_cardinality) {
104 cardinality_specified_ =
true;
105 cardinality_ = a_cardinality;
109 void ExpectationBase::RetireAllPreRequisites()
117 ::std::vector<ExpectationBase*> expectations(1,
this);
118 while (!expectations.empty()) {
119 ExpectationBase*
exp = expectations.back();
120 expectations.pop_back();
122 for (ExpectationSet::const_iterator it =
123 exp->immediate_prerequisites_.begin();
124 it != exp->immediate_prerequisites_.end(); ++it) {
125 ExpectationBase* next = it->expectation_base().get();
126 if (!next->is_retired()) {
128 expectations.push_back(next);
136 bool ExpectationBase::AllPrerequisitesAreSatisfied() const
138 g_gmock_mutex.AssertHeld();
139 ::std::vector<const ExpectationBase*> expectations(1,
this);
140 while (!expectations.empty()) {
141 const ExpectationBase* exp = expectations.back();
142 expectations.pop_back();
144 for (ExpectationSet::const_iterator it =
145 exp->immediate_prerequisites_.begin();
146 it != exp->immediate_prerequisites_.end(); ++it) {
147 const ExpectationBase* next = it->expectation_base().get();
148 if (!next->IsSatisfied())
return false;
149 expectations.push_back(next);
156 void ExpectationBase::FindUnsatisfiedPrerequisites(ExpectationSet* result)
const
158 g_gmock_mutex.AssertHeld();
159 ::std::vector<const ExpectationBase*> expectations(1,
this);
160 while (!expectations.empty()) {
161 const ExpectationBase* exp = expectations.back();
162 expectations.pop_back();
164 for (ExpectationSet::const_iterator it =
165 exp->immediate_prerequisites_.begin();
166 it != exp->immediate_prerequisites_.end(); ++it) {
167 const ExpectationBase* next = it->expectation_base().get();
169 if (next->IsSatisfied()) {
172 if (next->call_count_ == 0) {
173 expectations.push_back(next);
187 void ExpectationBase::DescribeCallCountTo(::std::ostream* os)
const
189 g_gmock_mutex.AssertHeld();
192 *os <<
" Expected: to be ";
193 cardinality().DescribeTo(os);
194 *os <<
"\n Actual: ";
195 Cardinality::DescribeActualCallCountTo(call_count(), os);
200 << (IsOverSaturated() ?
"over-saturated"
201 : IsSaturated() ?
"saturated"
202 : IsSatisfied() ?
"satisfied"
204 <<
" and " << (is_retired() ?
"retired" :
"active");
211 void ExpectationBase::CheckActionCountIfNotDone() const
213 bool should_check =
false;
216 if (!action_count_checked_) {
217 action_count_checked_ =
true;
223 if (!cardinality_specified_) {
230 const int action_count =
static_cast<int>(untyped_actions_.size());
231 const int upper_bound = cardinality().ConservativeUpperBound();
232 const int lower_bound = cardinality().ConservativeLowerBound();
235 if (action_count > upper_bound ||
236 (action_count == upper_bound && repeated_action_specified_)) {
238 }
else if (0 < action_count && action_count < lower_bound &&
239 !repeated_action_specified_) {
245 ::std::stringstream ss;
246 DescribeLocationTo(&ss);
247 ss <<
"Too " << (too_many ?
"many" :
"few") <<
" actions specified in "
248 << source_text() <<
"...\n"
249 <<
"Expected to be ";
250 cardinality().DescribeTo(&ss);
251 ss <<
", but has " << (too_many ?
"" :
"only ") << action_count
252 <<
" WillOnce()" << (action_count == 1 ?
"" :
"s");
253 if (repeated_action_specified_) {
254 ss <<
" and a WillRepeatedly()";
262 void ExpectationBase::UntypedTimes(
const Cardinality& a_cardinality) {
263 if (last_clause_ == kTimes) {
264 ExpectSpecProperty(
false,
265 ".Times() cannot appear "
266 "more than once in an EXPECT_CALL().");
269 last_clause_ < kTimes,
270 ".Times() may only appear *before* .InSequence(), .WillOnce(), "
271 ".WillRepeatedly(), or .RetiresOnSaturation(), not after.");
273 last_clause_ = kTimes;
275 SpecifyCardinality(a_cardinality);
286 const int stack_frames_to_skip =
290 Log(
kInfo, msg, stack_frames_to_skip);
295 "\nNOTE: You can safely ignore the above warning unless this "
296 "call should not happen. Do not suppress it by blindly adding "
297 "an EXPECT_CALL() if you don't mean to enforce the call. "
299 "https://github.com/google/googletest/blob/main/docs/"
300 "gmock_cook_book.md#"
301 "knowing-when-to-expect-useoncall for details.\n",
302 stack_frames_to_skip);
305 Expect(
false,
nullptr, -1, msg);
309 UntypedFunctionMockerBase::UntypedFunctionMockerBase()
310 : mock_obj_(nullptr), name_(
"") {}
312 UntypedFunctionMockerBase::~UntypedFunctionMockerBase() =
default;
318 void UntypedFunctionMockerBase::RegisterOwner(
const void* mock_obj)
322 mock_obj_ = mock_obj;
324 Mock::Register(mock_obj,
this);
330 void UntypedFunctionMockerBase::SetOwnerAndName(
const void* mock_obj,
336 mock_obj_ = mock_obj;
342 const void* UntypedFunctionMockerBase::MockObject() const
344 const void* mock_obj;
349 Assert(mock_obj_ !=
nullptr, __FILE__, __LINE__,
350 "MockObject() must not be called before RegisterOwner() or "
351 "SetOwnerAndName() has been called.");
352 mock_obj = mock_obj_;
359 const char* UntypedFunctionMockerBase::Name() const
366 Assert(name_ !=
nullptr, __FILE__, __LINE__,
367 "Name() must not be called before SetOwnerAndName() has "
376 Expectation UntypedFunctionMockerBase::GetHandleOf(ExpectationBase* exp) {
379 for (UntypedExpectations::const_iterator it = untyped_expectations_.begin();
380 it != untyped_expectations_.end(); ++it) {
381 if (it->get() ==
exp) {
382 return Expectation(*it);
386 Assert(
false, __FILE__, __LINE__,
"Cannot find expectation.");
387 return Expectation();
395 bool UntypedFunctionMockerBase::VerifyAndClearExpectationsLocked()
397 g_gmock_mutex.AssertHeld();
398 bool expectations_met =
true;
399 for (UntypedExpectations::const_iterator it = untyped_expectations_.begin();
400 it != untyped_expectations_.end(); ++it) {
401 ExpectationBase*
const untyped_expectation = it->get();
402 if (untyped_expectation->IsOverSaturated()) {
406 expectations_met =
false;
407 }
else if (!untyped_expectation->IsSatisfied()) {
408 expectations_met =
false;
409 ::std::stringstream ss;
411 const ::std::string& expectation_name =
412 untyped_expectation->GetDescription();
413 ss <<
"Actual function ";
414 if (!expectation_name.empty()) {
415 ss <<
"\"" << expectation_name <<
"\" ";
417 ss <<
"call count doesn't match " << untyped_expectation->source_text()
422 untyped_expectation->MaybeDescribeExtraMatcherTo(&ss);
423 untyped_expectation->DescribeCallCountTo(&ss);
424 Expect(
false, untyped_expectation->file(), untyped_expectation->line(),
436 UntypedExpectations expectations_to_delete;
437 untyped_expectations_.swap(expectations_to_delete);
439 g_gmock_mutex.Unlock();
440 expectations_to_delete.clear();
441 g_gmock_mutex.Lock();
443 return expectations_met;
447 if (mock_behavior >= kAllow && mock_behavior <= kFail) {
448 return static_cast<internal::CallReaction
>(mock_behavior);
459 typedef std::set<internal::UntypedFunctionMockerBase*> FunctionMockers;
464 struct MockObjectState {
482 class MockObjectRegistry {
485 typedef std::map<const void*, MockObjectState> StateMap;
491 ~MockObjectRegistry() {
495 int leaked_count = 0;
496 for (StateMap::const_iterator it =
states_.begin(); it !=
states_.end();
498 if (it->second.leakable)
504 const MockObjectState& state = it->second;
506 state.first_used_line);
507 std::cout <<
" ERROR: this mock object";
508 if (!state.first_used_test.empty()) {
509 std::cout <<
" (used in test " << state.first_used_test_suite <<
"."
510 << state.first_used_test <<
")";
512 std::cout <<
" should be deleted but never is. Its address is @"
516 if (leaked_count > 0) {
517 std::cout <<
"\nERROR: " << leaked_count <<
" leaked mock "
518 << (leaked_count == 1 ?
"object" :
"objects")
519 <<
" found at program exit. Expectations on a mock object are "
520 "verified when the object is destructed. Leaking a mock "
521 "means that its expectations aren't verified, which is "
522 "usually a test bug. If you really intend to leak a mock, "
523 "you can suppress this error using "
524 "testing::Mock::AllowLeak(mock_object), or you may use a "
525 "fake or stub instead of a mock.\n";
532 qurt_exception_raise_fatal();
540 StateMap& states() {
return states_; }
547 MockObjectRegistry g_mock_object_registry;
551 std::unordered_map<uintptr_t, internal::CallReaction>&
552 UninterestingCallReactionMap() {
553 static auto* map =
new std::unordered_map<uintptr_t, internal::CallReaction>;
559 void SetReactionOnUninterestingCalls(uintptr_t mock_obj,
560 internal::CallReaction reaction)
563 UninterestingCallReactionMap()[mock_obj] = reaction;
570 void Mock::AllowUninterestingCalls(uintptr_t mock_obj)
572 SetReactionOnUninterestingCalls(mock_obj, internal::kAllow);
577 void Mock::WarnUninterestingCalls(uintptr_t mock_obj)
579 SetReactionOnUninterestingCalls(mock_obj, internal::kWarn);
584 void Mock::FailUninterestingCalls(uintptr_t mock_obj)
586 SetReactionOnUninterestingCalls(mock_obj, internal::kFail);
591 void Mock::UnregisterCallReaction(uintptr_t mock_obj)
594 UninterestingCallReactionMap().erase(static_cast<uintptr_t>(mock_obj));
599 internal::CallReaction Mock::GetReactionOnUninterestingCalls(
602 return (UninterestingCallReactionMap().
count(
603 reinterpret_cast<uintptr_t>(mock_obj)) == 0)
606 : UninterestingCallReactionMap()[reinterpret_cast<uintptr_t>(
612 void Mock::AllowLeak(
const void* mock_obj)
615 g_mock_object_registry.states()[mock_obj].leakable =
true;
621 bool Mock::VerifyAndClearExpectations(
void* mock_obj)
624 return VerifyAndClearExpectationsLocked(mock_obj);
630 bool Mock::VerifyAndClear(
void* mock_obj)
633 ClearDefaultActionsLocked(mock_obj);
634 return VerifyAndClearExpectationsLocked(mock_obj);
640 bool Mock::VerifyAndClearExpectationsLocked(
void* mock_obj)
642 internal::g_gmock_mutex.AssertHeld();
643 if (g_mock_object_registry.states().count(mock_obj) == 0) {
650 bool expectations_met =
true;
651 FunctionMockers& mockers =
652 g_mock_object_registry.states()[mock_obj].function_mockers;
653 for (FunctionMockers::const_iterator it = mockers.begin();
654 it != mockers.end(); ++it) {
655 if (!(*it)->VerifyAndClearExpectationsLocked()) {
656 expectations_met =
false;
662 return expectations_met;
665 bool Mock::IsNaggy(
void* mock_obj)
667 return Mock::GetReactionOnUninterestingCalls(mock_obj) == internal::kWarn;
669 bool Mock::IsNice(
void* mock_obj)
671 return Mock::GetReactionOnUninterestingCalls(mock_obj) == internal::kAllow;
673 bool Mock::IsStrict(
void* mock_obj)
675 return Mock::GetReactionOnUninterestingCalls(mock_obj) == internal::kFail;
679 void Mock::Register(
const void* mock_obj,
680 internal::UntypedFunctionMockerBase* mocker)
683 g_mock_object_registry.states()[mock_obj].function_mockers.insert(mocker);
689 void Mock::RegisterUseByOnCallOrExpectCall(
const void* mock_obj,
690 const char* file,
int line)
693 MockObjectState& state = g_mock_object_registry.states()[mock_obj];
694 if (state.first_used_file ==
nullptr) {
695 state.first_used_file = file;
696 state.first_used_line = line;
697 const TestInfo*
const test_info =
698 UnitTest::GetInstance()->current_test_info();
699 if (test_info !=
nullptr) {
700 state.first_used_test_suite = test_info->test_suite_name();
701 state.first_used_test = test_info->name();
710 void Mock::UnregisterLocked(internal::UntypedFunctionMockerBase* mocker)
712 internal::g_gmock_mutex.AssertHeld();
713 for (MockObjectRegistry::StateMap::iterator it =
714 g_mock_object_registry.states().begin();
715 it != g_mock_object_registry.states().end(); ++it) {
716 FunctionMockers& mockers = it->second.function_mockers;
717 if (mockers.erase(mocker) > 0) {
719 if (mockers.empty()) {
720 g_mock_object_registry.states().erase(it);
728 void Mock::ClearDefaultActionsLocked(
void* mock_obj)
730 internal::g_gmock_mutex.AssertHeld();
732 if (g_mock_object_registry.states().count(mock_obj) == 0) {
739 FunctionMockers& mockers =
740 g_mock_object_registry.states()[mock_obj].function_mockers;
741 for (FunctionMockers::const_iterator it = mockers.begin();
742 it != mockers.end(); ++it) {
743 (*it)->ClearDefaultActionsLocked();
750 Expectation::Expectation() =
default;
752 Expectation::Expectation(
753 const std::shared_ptr<internal::ExpectationBase>& an_expectation_base)
754 : expectation_base_(an_expectation_base) {}
756 Expectation::~Expectation() =
default;
759 void Sequence::AddExpectation(
const Expectation& expectation)
const {
760 if (*last_expectation_ != expectation) {
761 if (last_expectation_->expectation_base() !=
nullptr) {
762 expectation.expectation_base()->immediate_prerequisites_ +=
765 *last_expectation_ = expectation;
770 InSequence::InSequence() {
773 sequence_created_ =
true;
775 sequence_created_ =
false;
781 InSequence::~InSequence() {
782 if (sequence_created_) {
790 #if defined(_MSC_VER) && (_MSC_VER == 1900)
GTEST_DISABLE_MSC_WARNINGS_POP_() TEST(LinkTest
void ReportUninterestingCall(CallReaction reaction, const std::string &msg)
const char kInfoVerbosity[]
#define GTEST_LOCK_EXCLUDED_(locks)
::std::string first_used_test
GTEST_API_ void LogWithLocation(testing::internal::LogSeverity severity, const char *file, int line, const std::string &message)
GTEST_API_::std::string FormatFileLocation(const char *file, int line)
#define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings)
#define GMOCK_FLAG_GET(name)
static GTEST_DEFINE_STATIC_MUTEX_(g_log_mutex)
static CallReaction intToCallReaction(int mock_behavior)
GTEST_API_ Cardinality Exactly(int n)
void Assert(bool condition, const char *file, int line, const std::string &msg)
#define GTEST_EXCLUSIVE_LOCK_REQUIRED_(locks)
const char * first_used_file
FunctionMockers function_mockers
GTEST_API_ ThreadLocal< Sequence * > g_gmock_implicit_sequence
::std::string first_used_test_suite
void Expect(bool condition, const char *file, int line, const std::string &msg)
GTEST_API_ void Log(LogSeverity severity, const std::string &message, int stack_frames_to_skip)