36 #include "gmock/gmock-spec-builders.h"
45 #include "gmock/gmock.h"
46 #include "gtest/gtest.h"
48 #if GTEST_OS_CYGWIN || GTEST_OS_LINUX || GTEST_OS_MAC
56 # pragma warning(push)
57 # pragma warning(disable:4800)
72 ::std::ostringstream s;
82 source_text_(a_source_text),
83 cardinality_specified_(
false),
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 =
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);
305 : mock_obj_(nullptr),
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());
418 if (
result !=
nullptr)
result->PrintAsActionResult(&ss);
424 bool is_excessive =
false;
425 ::std::stringstream ss;
426 ::std::stringstream why;
427 ::std::stringstream
loc;
428 const void* untyped_action =
nullptr;
433 this->UntypedFindMatchingExpectation(
434 untyped_args, &untyped_action, &is_excessive,
436 const bool found = untyped_expectation !=
nullptr;
441 const bool need_to_report_call =
443 if (!need_to_report_call) {
445 return untyped_action ==
nullptr
446 ? this->UntypedPerformDefaultAction(untyped_args,
"")
447 : this->UntypedPerformAction(untyped_action, untyped_args);
450 ss <<
" Function call: " << Name();
451 this->UntypedPrintArgs(untyped_args, &ss);
455 if (
found && !is_excessive) {
460 untyped_action ==
nullptr
461 ? this->UntypedPerformDefaultAction(untyped_args, ss.str())
462 : this->UntypedPerformAction(untyped_action, untyped_args);
463 if (
result !=
nullptr)
result->PrintAsActionResult(&ss);
464 ss <<
"\n" << why.str();
468 Expect(
false,
nullptr, -1, ss.str());
469 }
else if (is_excessive) {
472 untyped_expectation->
line(), ss.str());
487 for (UntypedExpectations::const_iterator
it =
490 if (
it->get() == exp) {
495 Assert(
false, __FILE__, __LINE__,
"Cannot find expectation.");
506 g_gmock_mutex.AssertHeld();
507 bool expectations_met =
true;
508 for (UntypedExpectations::const_iterator
it =
516 expectations_met =
false;
518 expectations_met =
false;
519 ::std::stringstream ss;
520 ss <<
"Actual function call count doesn't match "
528 untyped_expectation->
line(), ss.str());
542 g_gmock_mutex.Unlock();
543 expectations_to_delete.clear();
544 g_gmock_mutex.Lock();
546 return expectations_met;
550 if (mock_behavior >=
kAllow && mock_behavior <=
kFail) {
562 typedef std::set<internal::UntypedFunctionMockerBase*> FunctionMockers;
567 struct MockObjectState {
585 class MockObjectRegistry {
588 typedef std::map<const void*, MockObjectState> StateMap;
594 ~MockObjectRegistry() {
598 int leaked_count = 0;
601 if (
it->second.leakable)
607 const MockObjectState&
state =
it->second;
609 state.first_used_line);
610 std::cout <<
" ERROR: this mock object";
611 if (
state.first_used_test !=
"") {
612 std::cout <<
" (used in test " <<
state.first_used_test_suite <<
"."
613 <<
state.first_used_test <<
")";
615 std::cout <<
" should be deleted but never is. Its address is @"
619 if (leaked_count > 0) {
620 std::cout <<
"\nERROR: " << leaked_count <<
" leaked mock "
621 << (leaked_count == 1 ?
"object" :
"objects")
622 <<
" found at program exit. Expectations on a mock object is "
623 "verified when the object is destructed. Leaking a mock "
624 "means that its expectations aren't verified, which is "
625 "usually a test bug. If you really intend to leak a mock, "
626 "you can suppress this error using "
627 "testing::Mock::AllowLeak(mock_object), or you may use a "
628 "fake or stub instead of a mock.\n";
639 StateMap& states() {
return states_; }
646 MockObjectRegistry g_mock_object_registry;
650 std::map<const void*, internal::CallReaction> g_uninteresting_call_reaction;
654 void SetReactionOnUninterestingCalls(
const void* mock_obj,
658 g_uninteresting_call_reaction[mock_obj] = reaction;
665 void Mock::AllowUninterestingCalls(
const void* mock_obj)
672 void Mock::WarnUninterestingCalls(
const void* mock_obj)
679 void Mock::FailUninterestingCalls(
const void* mock_obj)
686 void Mock::UnregisterCallReaction(
const void* mock_obj)
689 g_uninteresting_call_reaction.erase(mock_obj);
695 const void* mock_obj)
698 return (g_uninteresting_call_reaction.count(mock_obj) == 0) ?
700 g_uninteresting_call_reaction[mock_obj];
705 void Mock::AllowLeak(
const void* mock_obj)
708 g_mock_object_registry.states()[mock_obj].leakable =
true;
714 bool Mock::VerifyAndClearExpectations(
void* mock_obj)
717 return VerifyAndClearExpectationsLocked(mock_obj);
723 bool Mock::VerifyAndClear(
void* mock_obj)
726 ClearDefaultActionsLocked(mock_obj);
727 return VerifyAndClearExpectationsLocked(mock_obj);
733 bool Mock::VerifyAndClearExpectationsLocked(
void* mock_obj)
735 internal::g_gmock_mutex.AssertHeld();
736 if (g_mock_object_registry.states().count(mock_obj) == 0) {
743 bool expectations_met =
true;
744 FunctionMockers& mockers =
745 g_mock_object_registry.states()[mock_obj].function_mockers;
746 for (FunctionMockers::const_iterator
it = mockers.begin();
747 it != mockers.end(); ++
it) {
748 if (!(*it)->VerifyAndClearExpectationsLocked()) {
749 expectations_met =
false;
755 return expectations_met;
758 bool Mock::IsNaggy(
void* mock_obj)
760 return Mock::GetReactionOnUninterestingCalls(mock_obj) ==
internal::kWarn;
762 bool Mock::IsNice(
void* mock_obj)
766 bool Mock::IsStrict(
void* mock_obj)
768 return Mock::GetReactionOnUninterestingCalls(mock_obj) ==
internal::kFail;
772 void Mock::Register(
const void* mock_obj,
773 internal::UntypedFunctionMockerBase* mocker)
776 g_mock_object_registry.states()[mock_obj].function_mockers.insert(mocker);
782 void Mock::RegisterUseByOnCallOrExpectCall(
const void* mock_obj,
786 MockObjectState&
state = g_mock_object_registry.states()[mock_obj];
787 if (
state.first_used_file ==
nullptr) {
790 const TestInfo*
const test_info =
792 if (test_info !=
nullptr) {
793 state.first_used_test_suite = test_info->test_suite_name();
794 state.first_used_test = test_info->name();
803 void Mock::UnregisterLocked(internal::UntypedFunctionMockerBase* mocker)
805 internal::g_gmock_mutex.AssertHeld();
807 g_mock_object_registry.states().begin();
808 it != g_mock_object_registry.states().end(); ++
it) {
809 FunctionMockers& mockers =
it->second.function_mockers;
810 if (mockers.erase(mocker) > 0) {
812 if (mockers.empty()) {
813 g_mock_object_registry.states().erase(
it);
821 void Mock::ClearDefaultActionsLocked(
void* mock_obj)
823 internal::g_gmock_mutex.AssertHeld();
825 if (g_mock_object_registry.states().count(mock_obj) == 0) {
832 FunctionMockers& mockers =
833 g_mock_object_registry.states()[mock_obj].function_mockers;
834 for (FunctionMockers::const_iterator
it = mockers.begin();
835 it != mockers.end(); ++
it) {
836 (*it)->ClearDefaultActionsLocked();
846 const std::shared_ptr<internal::ExpectationBase>& an_expectation_base)
847 : expectation_base_(an_expectation_base) {}
855 expectation.expectation_base()->immediate_prerequisites_
885 # pragma warning(pop)