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);
83ExpectationBase::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),
89 cardinality_(Exactly(1)),
92 extra_matcher_specified_(false),
93 repeated_action_specified_(false),
94 retires_on_saturation_(false),
96 action_count_checked_(false) {}
99ExpectationBase::~ExpectationBase() =
default;
103void ExpectationBase::SpecifyCardinality(
const Cardinality& a_cardinality) {
104 cardinality_specified_ =
true;
105 cardinality_ = a_cardinality;
109void 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);
136bool 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);
156void 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);
187void 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");
211void 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()";
257 Log(kWarning, ss.str(), -1);
262void 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);
309UntypedFunctionMockerBase::UntypedFunctionMockerBase()
310 : mock_obj_(nullptr), name_(
"") {}
312UntypedFunctionMockerBase::~UntypedFunctionMockerBase() =
default;
318void UntypedFunctionMockerBase::RegisterOwner(
const void* mock_obj)
321 MutexLock l(&g_gmock_mutex);
322 mock_obj_ = mock_obj;
324 Mock::Register(mock_obj,
this);
330void UntypedFunctionMockerBase::SetOwnerAndName(
const void* mock_obj,
336 mock_obj_ = mock_obj;
342const 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_;
359const char* UntypedFunctionMockerBase::Name() const
366 Assert(name_ !=
nullptr, __FILE__, __LINE__,
367 "Name() must not be called before SetOwnerAndName() has "
376Expectation 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();
395bool 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);
459typedef std::set<internal::UntypedFunctionMockerBase*> FunctionMockers;
464struct MockObjectState {
482class MockObjectRegistry {
485 typedef std::map<const void*, MockObjectState> StateMap;
491 ~MockObjectRegistry() {
494 int leaked_count = 0;
495 for (StateMap::const_iterator it = states_.begin(); it != states_.end();
497 if (it->second.leakable)
503 const MockObjectState& state = it->second;
504 std::cout << internal::FormatFileLocation(state.first_used_file,
505 state.first_used_line);
506 std::cout <<
" ERROR: this mock object";
507 if (!state.first_used_test.empty()) {
508 std::cout <<
" (used in test " << state.first_used_test_suite <<
"."
509 << state.first_used_test <<
")";
511 std::cout <<
" should be deleted but never is. Its address is @"
515 if (leaked_count > 0) {
516 std::cout <<
"\nERROR: " << leaked_count <<
" leaked mock "
517 << (leaked_count == 1 ?
"object" :
"objects")
518 <<
" found at program exit. Expectations on a mock object are "
519 "verified when the object is destructed. Leaking a mock "
520 "means that its expectations aren't verified, which is "
521 "usually a test bug. If you really intend to leak a mock, "
522 "you can suppress this error using "
523 "testing::Mock::AllowLeak(mock_object), or you may use a "
524 "fake or stub instead of a mock.\n";
531 qurt_exception_raise_fatal();
539 StateMap& states() {
return states_; }
546MockObjectRegistry g_mock_object_registry;
550std::unordered_map<uintptr_t, internal::CallReaction>&
551UninterestingCallReactionMap() {
552 static auto* map =
new std::unordered_map<uintptr_t, internal::CallReaction>;
558void SetReactionOnUninterestingCalls(uintptr_t mock_obj,
559 internal::CallReaction reaction)
561 internal::MutexLock l(&internal::g_gmock_mutex);
562 UninterestingCallReactionMap()[mock_obj] = reaction;
569void Mock::AllowUninterestingCalls(uintptr_t mock_obj)
571 SetReactionOnUninterestingCalls(mock_obj, internal::kAllow);
576void Mock::WarnUninterestingCalls(uintptr_t mock_obj)
578 SetReactionOnUninterestingCalls(mock_obj, internal::kWarn);
583void Mock::FailUninterestingCalls(uintptr_t mock_obj)
585 SetReactionOnUninterestingCalls(mock_obj, internal::kFail);
590void Mock::UnregisterCallReaction(uintptr_t mock_obj)
592 internal::MutexLock l(&internal::g_gmock_mutex);
593 UninterestingCallReactionMap().erase(
static_cast<uintptr_t
>(mock_obj));
598internal::CallReaction Mock::GetReactionOnUninterestingCalls(
600 internal::MutexLock l(&internal::g_gmock_mutex);
601 return (UninterestingCallReactionMap().
count(
602 reinterpret_cast<uintptr_t
>(mock_obj)) == 0)
603 ? internal::intToCallReaction(
605 : UninterestingCallReactionMap()[reinterpret_cast<uintptr_t>(
611void Mock::AllowLeak(
const void* mock_obj)
613 internal::MutexLock l(&internal::g_gmock_mutex);
614 g_mock_object_registry.states()[mock_obj].leakable =
true;
620bool Mock::VerifyAndClearExpectations(
void* mock_obj)
622 internal::MutexLock l(&internal::g_gmock_mutex);
623 return VerifyAndClearExpectationsLocked(mock_obj);
629bool Mock::VerifyAndClear(
void* mock_obj)
631 internal::MutexLock l(&internal::g_gmock_mutex);
632 ClearDefaultActionsLocked(mock_obj);
633 return VerifyAndClearExpectationsLocked(mock_obj);
639bool Mock::VerifyAndClearExpectationsLocked(
void* mock_obj)
641 internal::g_gmock_mutex.AssertHeld();
642 if (g_mock_object_registry.states().count(mock_obj) == 0) {
649 bool expectations_met =
true;
650 FunctionMockers& mockers =
651 g_mock_object_registry.states()[mock_obj].function_mockers;
652 for (FunctionMockers::const_iterator it = mockers.begin();
653 it != mockers.end(); ++it) {
654 if (!(*it)->VerifyAndClearExpectationsLocked()) {
655 expectations_met =
false;
661 return expectations_met;
664bool Mock::IsNaggy(
void* mock_obj)
666 return Mock::GetReactionOnUninterestingCalls(mock_obj) == internal::kWarn;
668bool Mock::IsNice(
void* mock_obj)
670 return Mock::GetReactionOnUninterestingCalls(mock_obj) == internal::kAllow;
672bool Mock::IsStrict(
void* mock_obj)
674 return Mock::GetReactionOnUninterestingCalls(mock_obj) == internal::kFail;
678void Mock::Register(
const void* mock_obj,
679 internal::UntypedFunctionMockerBase* mocker)
681 internal::MutexLock l(&internal::g_gmock_mutex);
682 g_mock_object_registry.states()[mock_obj].function_mockers.insert(mocker);
688void Mock::RegisterUseByOnCallOrExpectCall(
const void* mock_obj,
689 const char* file,
int line)
691 internal::MutexLock l(&internal::g_gmock_mutex);
692 MockObjectState& state = g_mock_object_registry.states()[mock_obj];
693 if (state.first_used_file ==
nullptr) {
694 state.first_used_file = file;
695 state.first_used_line = line;
696 const TestInfo*
const test_info =
697 UnitTest::GetInstance()->current_test_info();
698 if (test_info !=
nullptr) {
699 state.first_used_test_suite = test_info->test_suite_name();
700 state.first_used_test = test_info->name();
709void Mock::UnregisterLocked(internal::UntypedFunctionMockerBase* mocker)
711 internal::g_gmock_mutex.AssertHeld();
712 for (MockObjectRegistry::StateMap::iterator it =
713 g_mock_object_registry.states().begin();
714 it != g_mock_object_registry.states().end(); ++it) {
715 FunctionMockers& mockers = it->second.function_mockers;
716 if (mockers.erase(mocker) > 0) {
718 if (mockers.empty()) {
719 g_mock_object_registry.states().erase(it);
727void Mock::ClearDefaultActionsLocked(
void* mock_obj)
729 internal::g_gmock_mutex.AssertHeld();
731 if (g_mock_object_registry.states().count(mock_obj) == 0) {
738 FunctionMockers& mockers =
739 g_mock_object_registry.states()[mock_obj].function_mockers;
740 for (FunctionMockers::const_iterator it = mockers.begin();
741 it != mockers.end(); ++it) {
742 (*it)->ClearDefaultActionsLocked();
749Expectation::Expectation() =
default;
751Expectation::Expectation(
752 const std::shared_ptr<internal::ExpectationBase>& an_expectation_base)
753 : expectation_base_(an_expectation_base) {}
755Expectation::~Expectation() =
default;
758void Sequence::AddExpectation(
const Expectation& expectation)
const {
759 if (*last_expectation_ != expectation) {
760 if (last_expectation_->expectation_base() !=
nullptr) {
761 expectation.expectation_base()->immediate_prerequisites_ +=
764 *last_expectation_ = expectation;
769InSequence::InSequence() {
772 sequence_created_ =
true;
774 sequence_created_ =
false;
780InSequence::~InSequence() {
781 if (sequence_created_) {
789#if defined(_MSC_VER) && (_MSC_VER == 1900)
Definition gtest-port.h:1895
#define GMOCK_FLAG_GET(name)
Definition gmock-port.h:134
::std::string first_used_test_suite
Definition gmock-spec-builders.cc:472
int first_used_line
Definition gmock-spec-builders.cc:471
bool leakable
Definition gmock-spec-builders.cc:474
const char * first_used_file
Definition gmock-spec-builders.cc:470
::std::string first_used_test
Definition gmock-spec-builders.cc:473
FunctionMockers function_mockers
Definition gmock-spec-builders.cc:475
int * count
Definition gmock_stress_test.cc:90
#define GTEST_EXCLUSIVE_LOCK_REQUIRED_(locks)
Definition gtest-port.h:2301
#define GTEST_LOCK_EXCLUDED_(locks)
Definition gtest-port.h:2302
#define GTEST_DEFINE_STATIC_MUTEX_(mutex)
Definition gtest-port.h:1880
#define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings)
Definition gtest-port.h:360
#define GTEST_API_
Definition gtest-port.h:842
#define GTEST_DISABLE_MSC_WARNINGS_POP_()
Definition gtest-port.h:361
GTEST_API_ void LogWithLocation(testing::internal::LogSeverity severity, const char *file, int line, const std::string &message)
Definition gmock-spec-builders.cc:73
GTestMutexLock MutexLock
Definition gtest-port.h:1892
GTEST_API_ ThreadLocal< Sequence * > g_gmock_implicit_sequence
Definition gmock-spec-builders.cc:280
LogSeverity
Definition gmock-internal-utils.h:267
@ kInfo
Definition gmock-internal-utils.h:267
@ kWarning
Definition gmock-internal-utils.h:267
GTEST_API_::std::string FormatFileLocation(const char *file, int line)
Definition gtest-port.cc:977
static CallReaction intToCallReaction(int mock_behavior)
Definition gmock-spec-builders.cc:446
GTEST_API_ void Log(LogSeverity severity, const std::string &message, int stack_frames_to_skip)
Definition gmock-internal-utils.cc:153
const char kInfoVerbosity[]
Definition gmock-internal-utils.h:272
void Assert(bool condition, const char *file, int line, const std::string &msg)
Definition gmock-internal-utils.h:242
void Expect(bool condition, const char *file, int line, const std::string &msg)
Definition gmock-internal-utils.h:255
void ReportUninterestingCall(CallReaction reaction, const std::string &msg)
Definition gmock-spec-builders.cc:284
Definition gmock-actions.h:151