60 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_
61 #define GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_
69 #if GTEST_HAS_EXCEPTIONS
96 class ExpectationBase;
102 class ExpectationTester;
121 class UntypedActionResultHolderBase;
134 bool VerifyAndClearExpectationsLocked()
138 virtual void ClearDefaultActionsLocked()
151 const void* untyped_args,
152 const string& call_description)
const = 0;
158 const void* untyped_action,
159 const void* untyped_args)
const = 0;
164 virtual void UntypedDescribeUninterestingCall(
165 const void* untyped_args,
166 ::std::ostream*
os)
const
176 const void* untyped_args,
177 const void** untyped_action,
bool* is_excessive,
178 ::std::ostream* what, ::std::ostream* why)
182 virtual void UntypedPrintArgs(
const void* untyped_args,
183 ::std::ostream* os)
const = 0;
190 void RegisterOwner(
const void* mock_obj)
196 void SetOwnerAndName(
const void* mock_obj,
const char*
name)
202 const void* MockObject()
const
207 const char* Name()
const
215 const void* untyped_args)
221 typedef std::vector<internal::linked_ptr<ExpectationBase> >
284 template <
typename F>
307 ".With() cannot appear "
308 "more than once in an ON_CALL().");
318 ".WillByDefault() must appear "
319 "exactly once in an ON_CALL().");
323 "DoDefault() cannot be used in ON_CALL().");
336 ".WillByDefault() must appear exactly "
337 "once in an ON_CALL().");
377 static void AllowLeak(
const void* mock_obj)
383 static bool VerifyAndClearExpectations(
void* mock_obj)
389 static bool VerifyAndClear(
void* mock_obj)
397 template <
typename F>
400 template <
typename M>
403 template <
typename M>
406 template <
typename M>
411 static void AllowUninterestingCalls(
const void* mock_obj)
416 static void WarnUninterestingCalls(
const void* mock_obj)
421 static void FailUninterestingCalls(
const void* mock_obj)
426 static void UnregisterCallReaction(
const void* mock_obj)
432 const void* mock_obj)
438 static bool VerifyAndClearExpectationsLocked(
void* mock_obj)
442 static void ClearDefaultActionsLocked(
void* mock_obj)
446 static void Register(
447 const void* mock_obj,
454 static void RegisterUseByOnCallOrExpectCall(
455 const void* mock_obj,
const char*
file,
int line)
518 friend class ::testing::internal::ExpectationBase;
519 friend class ::testing::internal::UntypedFunctionMockerBase;
521 template <
typename F>
522 friend class ::testing::internal::FunctionMockerBase;
524 template <
typename F>
525 friend class ::testing::internal::TypedExpectation;
535 typedef ::std::set<Expectation, Less>
Set;
543 return expectation_base_;
626 void AddExpectation(
const Expectation& expectation)
const;
698 const char*
file()
const {
return file_; }
699 int line()
const {
return line_; }
711 void DescribeCallCountTo(::std::ostream*
os)
const
716 virtual void MaybeDescribeExtraMatcherTo(::std::ostream*
os) = 0;
719 friend class ::testing::Expectation;
742 Assert(property, file_, line_, failure_message);
747 Expect(property, file_, line_, failure_message);
752 void SpecifyCardinality(
const Cardinality& cardinality);
760 cardinality_ = a_cardinality;
768 void RetireAllPreRequisites()
772 bool is_retired() const
774 g_gmock_mutex.AssertHeld();
781 g_gmock_mutex.AssertHeld();
786 bool IsSatisfied() const
788 g_gmock_mutex.AssertHeld();
789 return cardinality().IsSatisfiedByCallCount(call_count_);
793 bool IsSaturated() const
795 g_gmock_mutex.AssertHeld();
796 return cardinality().IsSaturatedByCallCount(call_count_);
800 bool IsOverSaturated() const
802 g_gmock_mutex.AssertHeld();
803 return cardinality().IsOverSaturatedByCallCount(call_count_);
807 bool AllPrerequisitesAreSatisfied() const
815 int call_count() const
817 g_gmock_mutex.AssertHeld();
822 void IncrementCallCount()
824 g_gmock_mutex.AssertHeld();
832 void CheckActionCountIfNotDone() const
842 void UntypedTimes(const
Cardinality& a_cardinality);
848 const
string source_text_;
850 bool cardinality_specified_;
865 bool extra_matcher_specified_;
866 bool repeated_action_specified_;
867 bool retires_on_saturation_;
869 mutable
bool action_count_checked_;
876 template <typename F>
877 class TypedExpectation : public ExpectationBase {
884 const char* a_file,
int a_line,
const string& a_source_text,
885 const ArgumentMatcherTuple&
m)
886 : ExpectationBase(a_file, a_line, a_source_text),
893 extra_matcher_(
A<const ArgumentTuple&>()),
899 CheckActionCountIfNotDone();
900 for (UntypedActions::const_iterator
it = untyped_actions_.begin();
901 it != untyped_actions_.end(); ++
it) {
902 delete static_cast<const Action<F>*
>(*it);
908 if (last_clause_ == kWith) {
909 ExpectSpecProperty(
false,
910 ".With() cannot appear "
911 "more than once in an EXPECT_CALL().");
913 ExpectSpecProperty(last_clause_ < kWith,
914 ".With() must be the first "
915 "clause in an EXPECT_CALL().");
917 last_clause_ = kWith;
920 extra_matcher_specified_ =
true;
937 ExpectSpecProperty(last_clause_ <= kInSequence,
938 ".InSequence() cannot appear after .After(),"
939 " .WillOnce(), .WillRepeatedly(), or "
940 ".RetiresOnSaturation().");
941 last_clause_ = kInSequence;
946 TypedExpectation&
InSequence(
const Sequence& s1,
const Sequence& s2) {
949 TypedExpectation&
InSequence(
const Sequence& s1,
const Sequence& s2,
950 const Sequence& s3) {
953 TypedExpectation&
InSequence(
const Sequence& s1,
const Sequence& s2,
954 const Sequence& s3,
const Sequence& s4) {
957 TypedExpectation&
InSequence(
const Sequence& s1,
const Sequence& s2,
958 const Sequence& s3,
const Sequence& s4,
959 const Sequence& s5) {
965 ExpectSpecProperty(last_clause_ <= kAfter,
966 ".After() cannot appear after .WillOnce(),"
967 " .WillRepeatedly(), or "
968 ".RetiresOnSaturation().");
969 last_clause_ = kAfter;
972 immediate_prerequisites_ += *
it;
977 return After(s1).
After(s2);
981 return After(s1, s2).
After(s3);
985 return After(s1, s2, s3).
After(s4);
990 return After(s1, s2, s3, s4).
After(s5);
995 ExpectSpecProperty(last_clause_ <= kWillOnce,
996 ".WillOnce() cannot appear after "
997 ".WillRepeatedly() or .RetiresOnSaturation().");
998 last_clause_ = kWillOnce;
1000 untyped_actions_.push_back(
new Action<F>(action));
1001 if (!cardinality_specified()) {
1002 set_cardinality(
Exactly(static_cast<int>(untyped_actions_.size())));
1009 if (last_clause_ == kWillRepeatedly) {
1010 ExpectSpecProperty(
false,
1011 ".WillRepeatedly() cannot appear "
1012 "more than once in an EXPECT_CALL().");
1014 ExpectSpecProperty(last_clause_ < kWillRepeatedly,
1015 ".WillRepeatedly() cannot appear "
1016 "after .RetiresOnSaturation().");
1018 last_clause_ = kWillRepeatedly;
1019 repeated_action_specified_ =
true;
1021 repeated_action_ =
action;
1022 if (!cardinality_specified()) {
1023 set_cardinality(
AtLeast(static_cast<int>(untyped_actions_.size())));
1028 CheckActionCountIfNotDone();
1034 ExpectSpecProperty(last_clause_ < kRetiresOnSaturation,
1035 ".RetiresOnSaturation() cannot appear "
1037 last_clause_ = kRetiresOnSaturation;
1038 retires_on_saturation_ =
true;
1042 CheckActionCountIfNotDone();
1054 return extra_matcher_;
1063 if (extra_matcher_specified_) {
1064 *os <<
" Expected args: ";
1065 extra_matcher_.DescribeTo(os);
1071 template <
typename Function>
1077 return owner_->GetHandleOf(
this);
1087 g_gmock_mutex.AssertHeld();
1092 bool ShouldHandleArguments(
const ArgumentTuple&
args)
const
1094 g_gmock_mutex.AssertHeld();
1100 CheckActionCountIfNotDone();
1101 return !is_retired() && AllPrerequisitesAreSatisfied() &&
Matches(
args);
1106 void ExplainMatchResultTo(
1107 const ArgumentTuple&
args,
1108 ::std::ostream*
os)
const
1110 g_gmock_mutex.AssertHeld();
1113 *
os <<
" Expected: the expectation is active\n"
1114 <<
" Actual: it is retired\n";
1120 if (!extra_matcher_.MatchAndExplain(
args, &listener)) {
1121 *
os <<
" Expected args: ";
1122 extra_matcher_.DescribeTo(
os);
1123 *
os <<
"\n Actual: don't match";
1128 }
else if (!AllPrerequisitesAreSatisfied()) {
1129 *
os <<
" Expected: all pre-requisites are satisfied\n"
1130 <<
" Actual: the following immediate pre-requisites "
1131 <<
"are not satisfied:\n";
1133 FindUnsatisfiedPrerequisites(&unsatisfied_prereqs);
1136 it != unsatisfied_prereqs.
end(); ++
it) {
1137 it->expectation_base()->DescribeLocationTo(
os);
1138 *
os <<
"pre-requisite #" << i++ <<
"\n";
1140 *
os <<
" (end of pre-requisites)\n";
1146 *
os <<
"The call matches the expectation.\n";
1153 const ArgumentTuple&
args)
const
1155 g_gmock_mutex.AssertHeld();
1156 const int count = call_count();
1157 Assert(count >= 1, __FILE__, __LINE__,
1158 "call_count() is <= 0 when GetCurrentAction() is "
1159 "called - this should never happen.");
1161 const int action_count =
static_cast<int>(untyped_actions_.size());
1162 if (action_count > 0 && !repeated_action_specified_ &&
1163 count > action_count) {
1166 ::std::stringstream ss;
1167 DescribeLocationTo(&ss);
1168 ss <<
"Actions ran out in " << source_text() <<
"...\n"
1169 <<
"Called " << count <<
" times, but only "
1170 << action_count <<
" WillOnce()"
1171 << (action_count == 1 ?
" is" :
"s are") <<
" specified - ";
1172 mocker->DescribeDefaultActionTo(
args, &ss);
1176 return count <= action_count ?
1177 *
static_cast<const Action<F>*
>(untyped_actions_[count - 1]) :
1190 const ArgumentTuple&
args,
1191 ::std::ostream* what,
1192 ::std::ostream* why)
1194 g_gmock_mutex.AssertHeld();
1195 if (IsSaturated()) {
1197 IncrementCallCount();
1198 *what <<
"Mock function called more times than expected - ";
1199 mocker->DescribeDefaultActionTo(
args, what);
1200 DescribeCallCountTo(why);
1208 IncrementCallCount();
1209 RetireAllPreRequisites();
1211 if (retires_on_saturation_ && IsSaturated()) {
1216 *what <<
"Mock function call matches " << source_text() <<
"...\n";
1217 return &(GetCurrentAction(mocker,
args));
1242 const char* file,
int line,
1245 template <
typename F>
1249 typedef typename internal::Function<F>::ArgumentMatcherTuple
1255 : function_mocker_(function_mocker) {}
1260 const char* file,
int line,
const char* obj,
const char* call) {
1262 string(
"ON_CALL(") + obj +
", " + call +
") invoked");
1263 return function_mocker_->AddNewOnCallSpec(file, line, matchers_);
1269 const char* file,
int line,
const char* obj,
const char* call) {
1270 const string source_text(
string(
"EXPECT_CALL(") + obj +
", " + call +
")");
1272 return function_mocker_->AddNewExpectation(
1273 file, line, source_text, matchers_);
1277 template <
typename Function>
1278 friend class internal::FunctionMocker;
1281 matchers_ = matchers;
1301 template <
typename T>
1306 : value_(::testing::internal::
move(value)) {
1328 template <
typename T>
1335 : value_ptr_(&ref) {}
1337 const T&
Peek()
const {
return *value_ptr_; }
1349 # pragma warning(push) // Saves the current warning state.
1350 # pragma warning(disable:4355) // Temporarily disables warning 4355.
1367 virtual void PrintAsActionResult(::std::ostream*
os)
const = 0;
1371 template <
typename T>
1376 return result_.Unwrap();
1381 *os <<
"\n Returns: ";
1388 template <
typename F>
1392 const string& call_description) {
1399 template <
typename F>
1410 : result_(::testing::internal::
move(result)) {
1428 template <
typename F>
1432 const string& call_description) {
1439 template <
typename F>
1455 template <
typename F>
1470 VerifyAndClearExpectationsLocked();
1471 Mock::UnregisterLocked(
this);
1472 ClearDefaultActionsLocked();
1480 for (UntypedOnCallSpecs::const_reverse_iterator
it
1481 = untyped_on_call_specs_.rbegin();
1482 it != untyped_on_call_specs_.rend(); ++
it) {
1499 const string& call_description)
const {
1501 this->FindOnCallSpec(args);
1505 const string message = call_description +
1506 "\n The mock function has no default action "
1507 "set, and its return type has no default value set.";
1508 #if GTEST_HAS_EXCEPTIONS
1510 throw std::runtime_error(message);
1524 const void* untyped_args,
1525 const string& call_description)
const {
1528 return ResultHolder::PerformDefaultAction(
this, args, call_description);
1536 const void* untyped_action,
const void* untyped_args)
const {
1542 return ResultHolder::PerformAction(action, args);
1547 virtual void ClearDefaultActionsLocked()
1549 g_gmock_mutex.AssertHeld();
1559 untyped_on_call_specs_.swap(specs_to_delete);
1561 g_gmock_mutex.Unlock();
1562 for (UntypedOnCallSpecs::const_iterator
it =
1563 specs_to_delete.begin();
1564 it != specs_to_delete.end(); ++
it) {
1570 g_gmock_mutex.Lock();
1574 template <
typename Function>
1585 DownCast_<ResultHolder*>(this->UntypedInvokeWith(&
args)));
1586 return holder->Unwrap();
1591 const char* file,
int line,
1594 Mock::RegisterUseByOnCallOrExpectCall(MockObject(), file, line);
1596 untyped_on_call_specs_.push_back(on_call_spec);
1597 return *on_call_spec;
1604 const string& source_text,
1607 Mock::RegisterUseByOnCallOrExpectCall(MockObject(), file, line);
1611 untyped_expectations_.push_back(untyped_expectation);
1615 if (implicit_sequence != NULL) {
1619 return *expectation;
1635 ::std::ostream*
os)
const {
1640 "returning directly.\n" :
1641 "returning default value.\n");
1643 *os <<
"taking default action specified at:\n"
1651 virtual void UntypedDescribeUninterestingCall(
1652 const void* untyped_args,
1653 ::std::ostream*
os)
const
1657 *
os <<
"Uninteresting mock function call - ";
1658 DescribeDefaultActionTo(args,
os);
1659 *
os <<
" Function call: " << Name();
1680 const void* untyped_args,
1681 const void** untyped_action,
bool* is_excessive,
1682 ::std::ostream* what, ::std::ostream* why)
1689 this->FormatUnexpectedCallMessageLocked(args, what, why);
1700 *untyped_action =
action;
1705 virtual void UntypedPrintArgs(
const void* untyped_args,
1706 ::std::ostream*
os)
const {
1717 g_gmock_mutex.AssertHeld();
1718 for (
typename UntypedExpectations::const_reverse_iterator
it =
1719 untyped_expectations_.rbegin();
1720 it != untyped_expectations_.rend(); ++
it) {
1731 void FormatUnexpectedCallMessageLocked(
1734 ::std::ostream* why)
const
1736 g_gmock_mutex.AssertHeld();
1737 *
os <<
"\nUnexpected mock function call - ";
1738 DescribeDefaultActionTo(
args,
os);
1739 PrintTriedExpectationsLocked(
args, why);
1744 void PrintTriedExpectationsLocked(
1746 ::std::ostream* why)
const
1748 g_gmock_mutex.AssertHeld();
1749 const int count =
static_cast<int>(untyped_expectations_.size());
1750 *why <<
"Google Mock tried the following " << count <<
" "
1751 << (count == 1 ?
"expectation, but it didn't match" :
1752 "expectations, but none matched")
1760 *why <<
"tried expectation #" <<
i <<
": ";
1788 # pragma warning(pop) // Restores the warning state.
1825 template <
typename T>
1830 : expectation_base_(exp.GetHandle().expectation_base()) {}
1838 #define GMOCK_ON_CALL_IMPL_(obj, call) \
1839 ((obj).gmock_##call).InternalDefaultActionSetAt(__FILE__, __LINE__, \
1841 #define ON_CALL(obj, call) GMOCK_ON_CALL_IMPL_(obj, call)
1843 #define GMOCK_EXPECT_CALL_IMPL_(obj, call) \
1844 ((obj).gmock_##call).InternalExpectedAt(__FILE__, __LINE__, #obj, #call)
1845 #define EXPECT_CALL(obj, call) GMOCK_EXPECT_CALL_IMPL_(obj, call)
1847 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_