61 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_
62 #define GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_
71 #include "gmock/gmock-actions.h"
72 #include "gmock/gmock-cardinalities.h"
73 #include "gmock/gmock-matchers.h"
74 #include "gmock/internal/gmock-internal-utils.h"
75 #include "gmock/internal/gmock-port.h"
76 #include "gtest/gtest.h"
78 #if GTEST_HAS_EXCEPTIONS
98 template <
typename F>
class FunctionMocker;
101 class ExpectationBase;
104 template <
typename F>
class TypedExpectation;
107 class ExpectationTester;
123 class UntypedActionResultHolderBase;
130 UntypedFunctionMockerBase();
131 virtual ~UntypedFunctionMockerBase();
136 bool VerifyAndClearExpectationsLocked()
140 virtual void ClearDefaultActionsLocked()
152 virtual UntypedActionResultHolderBase* UntypedPerformDefaultAction(
153 void* untyped_args,
const std::string& call_description)
const = 0;
158 virtual UntypedActionResultHolderBase* UntypedPerformAction(
159 const void* untyped_action,
void* untyped_args)
const = 0;
164 virtual void UntypedDescribeUninterestingCall(
165 const void* untyped_args,
166 ::std::ostream* os)
const
175 virtual const ExpectationBase* UntypedFindMatchingExpectation(
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;
189 void RegisterOwner(
const void* mock_obj)
195 void SetOwnerAndName(
const void* mock_obj,
const char*
name)
201 const void* MockObject()
const
206 const char* Name()
const
213 UntypedActionResultHolderBase* UntypedInvokeWith(
void* untyped_args)
217 typedef std::vector<const void*> UntypedOnCallSpecs;
219 using UntypedExpectations = std::vector<std::shared_ptr<ExpectationBase>>;
228 const void* mock_obj_;
235 UntypedOnCallSpecs untyped_on_call_specs_;
246 UntypedExpectations untyped_expectations_;
250 class UntypedOnCallSpecBase {
253 UntypedOnCallSpecBase(
const char* a_file,
int a_line)
254 :
file_(a_file),
line_(a_line), last_clause_(kNone) {}
257 const char*
file()
const {
return file_; }
271 void AssertSpecProperty(
bool property,
277 void ExpectSpecProperty(
bool property,
291 template <
typename F>
292 class OnCallSpec :
public UntypedOnCallSpecBase {
294 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
295 typedef typename Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple;
299 OnCallSpec(
const char* a_file,
int a_line,
300 const ArgumentMatcherTuple&
matchers)
301 : UntypedOnCallSpecBase(a_file, a_line),
309 OnCallSpec& With(
const Matcher<const ArgumentTuple&>&
m) {
311 ExpectSpecProperty(last_clause_ < kWith,
312 ".With() cannot appear "
313 "more than once in an ON_CALL().");
314 last_clause_ = kWith;
321 OnCallSpec& WillByDefault(
const Action<F>&
action) {
322 ExpectSpecProperty(last_clause_ < kWillByDefault,
323 ".WillByDefault() must appear "
324 "exactly once in an ON_CALL().");
325 last_clause_ = kWillByDefault;
327 ExpectSpecProperty(!
action.IsDoDefault(),
328 "DoDefault() cannot be used in ON_CALL().");
339 const Action<F>& GetAction()
const {
340 AssertSpecProperty(last_clause_ == kWillByDefault,
341 ".WillByDefault() must appear exactly "
342 "once in an ON_CALL().");
361 Matcher<const ArgumentTuple&> extra_matcher_;
381 static void AllowLeak(
const void* mock_obj)
387 static bool VerifyAndClearExpectations(
void* mock_obj)
393 static bool VerifyAndClear(
void* mock_obj)
397 static bool IsNaggy(
void* mock_obj)
400 static bool IsNice(
void* mock_obj)
403 static bool IsStrict(
void* mock_obj)
407 friend class internal::UntypedFunctionMockerBase;
411 template <
typename F>
412 friend class internal::FunctionMocker;
414 template <
typename M>
415 friend class NiceMock;
417 template <
typename M>
418 friend class NaggyMock;
420 template <
typename M>
421 friend class StrictMock;
425 static void AllowUninterestingCalls(
const void* mock_obj)
430 static void WarnUninterestingCalls(
const void* mock_obj)
435 static void FailUninterestingCalls(
const void* mock_obj)
440 static void UnregisterCallReaction(
const void* mock_obj)
446 const void* mock_obj)
452 static bool VerifyAndClearExpectationsLocked(
void* mock_obj)
456 static void ClearDefaultActionsLocked(
void* mock_obj)
460 static void Register(
461 const void* mock_obj,
462 internal::UntypedFunctionMockerBase* mocker)
468 static void RegisterUseByOnCallOrExpectCall(
469 const void* mock_obj,
const char*
file,
int line)
476 static void UnregisterLocked(internal::UntypedFunctionMockerBase* mocker)
519 return expectation_base_ == rhs.expectation_base_;
525 friend class ExpectationSet;
526 friend class Sequence;
527 friend class ::testing::internal::ExpectationBase;
528 friend class ::testing::internal::UntypedFunctionMockerBase;
530 template <
typename F>
531 friend class ::testing::internal::FunctionMocker;
533 template <
typename F>
534 friend class ::testing::internal::TypedExpectation;
540 return lhs.expectation_base_.get() < rhs.expectation_base_.get();
544 typedef ::std::set<Expectation, Less> Set;
547 const std::shared_ptr<internal::ExpectationBase>& expectation_base);
550 const std::shared_ptr<internal::ExpectationBase>& expectation_base()
const {
551 return expectation_base_;
555 std::shared_ptr<internal::ExpectationBase> expectation_base_;
571 class ExpectationSet {
574 typedef Expectation::Set::const_iterator const_iterator;
585 ExpectationSet(internal::ExpectationBase& exp) {
601 bool operator==(
const ExpectationSet& rhs)
const {
605 bool operator!=(
const ExpectationSet& rhs)
const {
return !(*
this == rhs); }
638 std::shared_ptr<Expectation> last_expectation_;
670 bool sequence_created_;
700 virtual ~ExpectationBase();
703 const char*
file()
const {
return file_; }
705 const char* source_text()
const {
return source_text_.c_str(); }
707 const Cardinality& cardinality()
const {
return cardinality_; }
710 void DescribeLocationTo(::std::ostream* os)
const {
716 void DescribeCallCountTo(::std::ostream* os)
const
721 virtual void MaybeDescribeExtraMatcherTo(::std::ostream* os) = 0;
724 friend class ::testing::Expectation;
725 friend class UntypedFunctionMockerBase;
739 typedef std::vector<const void*> UntypedActions;
746 void AssertSpecProperty(
bool property,
752 void ExpectSpecProperty(
bool property,
759 void SpecifyCardinality(
const Cardinality& cardinality);
763 bool cardinality_specified()
const {
return cardinality_specified_; }
766 void set_cardinality(
const Cardinality& a_cardinality) {
767 cardinality_ = a_cardinality;
775 void RetireAllPreRequisites()
779 bool is_retired()
const
781 g_gmock_mutex.AssertHeld();
788 g_gmock_mutex.AssertHeld();
793 bool IsSatisfied()
const
795 g_gmock_mutex.AssertHeld();
796 return cardinality().IsSatisfiedByCallCount(call_count_);
800 bool IsSaturated()
const
802 g_gmock_mutex.AssertHeld();
803 return cardinality().IsSaturatedByCallCount(call_count_);
807 bool IsOverSaturated()
const
809 g_gmock_mutex.AssertHeld();
810 return cardinality().IsOverSaturatedByCallCount(call_count_);
814 bool AllPrerequisitesAreSatisfied()
const
818 void FindUnsatisfiedPrerequisites(ExpectationSet*
result)
const
822 int call_count()
const
824 g_gmock_mutex.AssertHeld();
829 void IncrementCallCount()
831 g_gmock_mutex.AssertHeld();
839 void CheckActionCountIfNotDone()
const
842 friend class ::testing::Sequence;
843 friend class ::testing::internal::ExpectationTester;
845 template <
typename Function>
846 friend class TypedExpectation;
849 void UntypedTimes(
const Cardinality& a_cardinality);
857 bool cardinality_specified_;
865 ExpectationSet immediate_prerequisites_;
871 UntypedActions untyped_actions_;
872 bool extra_matcher_specified_;
873 bool repeated_action_specified_;
874 bool retires_on_saturation_;
876 mutable bool action_count_checked_;
883 template <
typename F>
884 class TypedExpectation :
public ExpectationBase {
886 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
887 typedef typename Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple;
890 TypedExpectation(FunctionMocker<F>* owner,
const char* a_file,
int a_line,
892 const ArgumentMatcherTuple&
m)
893 : ExpectationBase(a_file, a_line, a_source_text),
902 ~TypedExpectation()
override {
905 CheckActionCountIfNotDone();
906 for (UntypedActions::const_iterator
it = untyped_actions_.begin();
907 it != untyped_actions_.end(); ++
it) {
908 delete static_cast<const Action<F>*
>(*it);
913 TypedExpectation& With(
const Matcher<const ArgumentTuple&>&
m) {
914 if (last_clause_ == kWith) {
915 ExpectSpecProperty(
false,
916 ".With() cannot appear "
917 "more than once in an EXPECT_CALL().");
919 ExpectSpecProperty(last_clause_ < kWith,
920 ".With() must be the first "
921 "clause in an EXPECT_CALL().");
923 last_clause_ = kWith;
926 extra_matcher_specified_ =
true;
931 TypedExpectation& Times(
const Cardinality& a_cardinality) {
932 ExpectationBase::UntypedTimes(a_cardinality);
937 TypedExpectation& Times(
int n) {
942 TypedExpectation& InSequence(
const Sequence& s) {
943 ExpectSpecProperty(last_clause_ <= kInSequence,
944 ".InSequence() cannot appear after .After(),"
945 " .WillOnce(), .WillRepeatedly(), or "
946 ".RetiresOnSaturation().");
947 last_clause_ = kInSequence;
949 s.AddExpectation(GetHandle());
952 TypedExpectation& InSequence(
const Sequence& s1,
const Sequence& s2) {
953 return InSequence(s1).InSequence(s2);
955 TypedExpectation& InSequence(
const Sequence& s1,
const Sequence& s2,
956 const Sequence& s3) {
957 return InSequence(s1, s2).InSequence(s3);
959 TypedExpectation& InSequence(
const Sequence& s1,
const Sequence& s2,
960 const Sequence& s3,
const Sequence& s4) {
961 return InSequence(s1, s2, s3).InSequence(s4);
963 TypedExpectation& InSequence(
const Sequence& s1,
const Sequence& s2,
964 const Sequence& s3,
const Sequence& s4,
965 const Sequence& s5) {
966 return InSequence(s1, s2, s3, s4).InSequence(s5);
970 TypedExpectation& After(
const ExpectationSet& s) {
971 ExpectSpecProperty(last_clause_ <= kAfter,
972 ".After() cannot appear after .WillOnce(),"
973 " .WillRepeatedly(), or "
974 ".RetiresOnSaturation().");
975 last_clause_ = kAfter;
977 for (ExpectationSet::const_iterator
it = s.begin();
it != s.end(); ++
it) {
978 immediate_prerequisites_ += *
it;
982 TypedExpectation& After(
const ExpectationSet& s1,
const ExpectationSet& s2) {
983 return After(s1).After(s2);
985 TypedExpectation& After(
const ExpectationSet& s1,
const ExpectationSet& s2,
986 const ExpectationSet& s3) {
987 return After(s1, s2).After(s3);
989 TypedExpectation& After(
const ExpectationSet& s1,
const ExpectationSet& s2,
990 const ExpectationSet& s3,
const ExpectationSet& s4) {
991 return After(s1, s2, s3).After(s4);
993 TypedExpectation& After(
const ExpectationSet& s1,
const ExpectationSet& s2,
994 const ExpectationSet& s3,
const ExpectationSet& s4,
995 const ExpectationSet& s5) {
996 return After(s1, s2, s3, s4).After(s5);
1000 TypedExpectation& WillOnce(
const Action<F>&
action) {
1001 ExpectSpecProperty(last_clause_ <= kWillOnce,
1002 ".WillOnce() cannot appear after "
1003 ".WillRepeatedly() or .RetiresOnSaturation().");
1004 last_clause_ = kWillOnce;
1006 untyped_actions_.push_back(
new Action<F>(
action));
1007 if (!cardinality_specified()) {
1008 set_cardinality(
Exactly(
static_cast<int>(untyped_actions_.size())));
1014 TypedExpectation& WillRepeatedly(
const Action<F>&
action) {
1015 if (last_clause_ == kWillRepeatedly) {
1016 ExpectSpecProperty(
false,
1017 ".WillRepeatedly() cannot appear "
1018 "more than once in an EXPECT_CALL().");
1020 ExpectSpecProperty(last_clause_ < kWillRepeatedly,
1021 ".WillRepeatedly() cannot appear "
1022 "after .RetiresOnSaturation().");
1024 last_clause_ = kWillRepeatedly;
1025 repeated_action_specified_ =
true;
1027 repeated_action_ =
action;
1028 if (!cardinality_specified()) {
1029 set_cardinality(
AtLeast(
static_cast<int>(untyped_actions_.size())));
1034 CheckActionCountIfNotDone();
1039 TypedExpectation& RetiresOnSaturation() {
1040 ExpectSpecProperty(last_clause_ < kRetiresOnSaturation,
1041 ".RetiresOnSaturation() cannot appear "
1043 last_clause_ = kRetiresOnSaturation;
1044 retires_on_saturation_ =
true;
1048 CheckActionCountIfNotDone();
1054 const ArgumentMatcherTuple&
matchers()
const {
1059 const Matcher<const ArgumentTuple&>& extra_matcher()
const {
1060 return extra_matcher_;
1064 const Action<F>& repeated_action()
const {
return repeated_action_; }
1068 void MaybeDescribeExtraMatcherTo(::std::ostream* os)
override {
1069 if (extra_matcher_specified_) {
1070 *os <<
" Expected args: ";
1071 extra_matcher_.DescribeTo(os);
1077 template <
typename Function>
1078 friend class FunctionMocker;
1082 Expectation GetHandle()
override {
return owner_->GetHandleOf(
this); }
1091 g_gmock_mutex.AssertHeld();
1096 bool ShouldHandleArguments(
const ArgumentTuple&
args)
const
1098 g_gmock_mutex.AssertHeld();
1104 CheckActionCountIfNotDone();
1105 return !is_retired() && AllPrerequisitesAreSatisfied() &&
Matches(
args);
1110 void ExplainMatchResultTo(
1111 const ArgumentTuple&
args,
1112 ::std::ostream* os)
const
1114 g_gmock_mutex.AssertHeld();
1117 *os <<
" Expected: the expectation is active\n"
1118 <<
" Actual: it is retired\n";
1123 StringMatchResultListener listener;
1124 if (!extra_matcher_.MatchAndExplain(
args, &listener)) {
1125 *os <<
" Expected args: ";
1126 extra_matcher_.DescribeTo(os);
1127 *os <<
"\n Actual: don't match";
1132 }
else if (!AllPrerequisitesAreSatisfied()) {
1133 *os <<
" Expected: all pre-requisites are satisfied\n"
1134 <<
" Actual: the following immediate pre-requisites "
1135 <<
"are not satisfied:\n";
1136 ExpectationSet unsatisfied_prereqs;
1137 FindUnsatisfiedPrerequisites(&unsatisfied_prereqs);
1139 for (ExpectationSet::const_iterator
it = unsatisfied_prereqs.begin();
1140 it != unsatisfied_prereqs.end(); ++
it) {
1141 it->expectation_base()->DescribeLocationTo(os);
1142 *os <<
"pre-requisite #" <<
i++ <<
"\n";
1144 *os <<
" (end of pre-requisites)\n";
1150 *os <<
"The call matches the expectation.\n";
1155 const Action<F>& GetCurrentAction(
const FunctionMocker<F>* mocker,
1156 const ArgumentTuple&
args)
const
1158 g_gmock_mutex.AssertHeld();
1159 const int count = call_count();
1161 "call_count() is <= 0 when GetCurrentAction() is "
1162 "called - this should never happen.");
1164 const int action_count =
static_cast<int>(untyped_actions_.size());
1165 if (action_count > 0 && !repeated_action_specified_ &&
1166 count > action_count) {
1169 ::std::stringstream ss;
1170 DescribeLocationTo(&ss);
1171 ss <<
"Actions ran out in " << source_text() <<
"...\n"
1172 <<
"Called " <<
count <<
" times, but only "
1173 << action_count <<
" WillOnce()"
1174 << (action_count == 1 ?
" is" :
"s are") <<
" specified - ";
1175 mocker->DescribeDefaultActionTo(
args, &ss);
1179 return count <= action_count
1180 ? *
static_cast<const Action<F>*
>(
1181 untyped_actions_[
static_cast<size_t>(
count - 1)])
1182 : repeated_action();
1192 const Action<F>* GetActionForArguments(
const FunctionMocker<F>* mocker,
1193 const ArgumentTuple&
args,
1194 ::std::ostream* what,
1195 ::std::ostream* why)
1197 g_gmock_mutex.AssertHeld();
1198 if (IsSaturated()) {
1200 IncrementCallCount();
1201 *what <<
"Mock function called more times than expected - ";
1202 mocker->DescribeDefaultActionTo(
args, what);
1203 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));
1222 FunctionMocker<F>*
const owner_;
1224 Matcher<const ArgumentTuple&> extra_matcher_;
1225 Action<F> repeated_action_;
1245 template <
typename F>
1248 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
1249 typedef typename internal::Function<F>::ArgumentMatcherTuple
1250 ArgumentMatcherTuple;
1254 MockSpec(internal::FunctionMocker<F>* function_mocker,
1255 const ArgumentMatcherTuple&
matchers)
1260 internal::OnCallSpec<F>& InternalDefaultActionSetAt(
1269 internal::TypedExpectation<F>& InternalExpectedAt(
1274 return function_mocker_->AddNewExpectation(
1281 MockSpec<F>& operator()(
const internal::WithoutMatchers&,
void*
const) {
1286 template <
typename Function>
1287 friend class internal::FunctionMocker;
1290 internal::FunctionMocker<F>*
const function_mocker_;
1306 template <
typename T>
1307 class ReferenceOrValueWrapper {
1310 explicit ReferenceOrValueWrapper(
T value)
1323 const T& Peek()
const {
1333 template <
typename T>
1334 class ReferenceOrValueWrapper<
T&> {
1338 typedef T& reference;
1339 explicit ReferenceOrValueWrapper(reference
ref)
1340 : value_ptr_(&
ref) {}
1341 T& Unwrap() {
return *value_ptr_; }
1342 const T& Peek()
const {
return *value_ptr_; }
1363 class UntypedActionResultHolderBase {
1365 virtual ~UntypedActionResultHolderBase() {}
1368 virtual void PrintAsActionResult(::std::ostream* os)
const = 0;
1372 template <
typename T>
1373 class ActionResultHolder :
public UntypedActionResultHolderBase {
1377 return result_.Unwrap();
1381 void PrintAsActionResult(::std::ostream* os)
const override {
1382 *os <<
"\n Returns: ";
1389 template <
typename F>
1390 static ActionResultHolder* PerformDefaultAction(
1391 const FunctionMocker<F>* func_mocker,
1392 typename Function<F>::ArgumentTuple&&
args,
1394 return new ActionResultHolder(Wrapper(func_mocker->PerformDefaultAction(
1400 template <
typename F>
1401 static ActionResultHolder* PerformAction(
1402 const Action<F>&
action,
typename Function<F>::ArgumentTuple&&
args) {
1403 return new ActionResultHolder(
1408 typedef ReferenceOrValueWrapper<T> Wrapper;
1410 explicit ActionResultHolder(Wrapper
result)
1421 class ActionResultHolder<void> :
public UntypedActionResultHolderBase {
1425 void PrintAsActionResult(::std::ostream* )
const override {}
1429 template <
typename F>
1430 static ActionResultHolder* PerformDefaultAction(
1431 const FunctionMocker<F>* func_mocker,
1432 typename Function<F>::ArgumentTuple&&
args,
1434 func_mocker->PerformDefaultAction(
std::move(
args), call_description);
1435 return new ActionResultHolder;
1440 template <
typename F>
1441 static ActionResultHolder* PerformAction(
1442 const Action<F>&
action,
typename Function<F>::ArgumentTuple&&
args) {
1444 return new ActionResultHolder;
1448 ActionResultHolder() {}
1452 template <
typename F>
1453 class FunctionMocker;
1455 template <
typename R,
typename...
Args>
1456 class FunctionMocker<R(
Args...)> final :
public UntypedFunctionMockerBase {
1457 using F = R(
Args...);
1461 using ArgumentTuple = std::tuple<
Args...>;
1462 using ArgumentMatcherTuple = std::tuple<Matcher<Args>...>;
1478 FunctionMocker(
const FunctionMocker&) =
delete;
1479 FunctionMocker& operator=(
const FunctionMocker&) =
delete;
1486 VerifyAndClearExpectationsLocked();
1487 Mock::UnregisterLocked(
this);
1488 ClearDefaultActionsLocked();
1494 const OnCallSpec<F>* FindOnCallSpec(
1495 const ArgumentTuple&
args)
const {
1496 for (UntypedOnCallSpecs::const_reverse_iterator
it
1497 = untyped_on_call_specs_.rbegin();
1498 it != untyped_on_call_specs_.rend(); ++
it) {
1499 const OnCallSpec<F>*
spec =
static_cast<const OnCallSpec<F>*
>(*it);
1514 Result PerformDefaultAction(ArgumentTuple&&
args,
1516 const OnCallSpec<F>*
const spec =
1517 this->FindOnCallSpec(
args);
1518 if (
spec !=
nullptr) {
1523 "\n The mock function has no default action "
1524 "set, and its return type has no default value set.";
1525 #if GTEST_HAS_EXCEPTIONS
1526 if (!DefaultValue<Result>::Exists()) {
1527 throw std::runtime_error(
message);
1540 UntypedActionResultHolderBase* UntypedPerformDefaultAction(
1542 const std::string& call_description)
const override {
1543 ArgumentTuple*
args =
static_cast<ArgumentTuple*
>(untyped_args);
1544 return ResultHolder::PerformDefaultAction(
this,
std::move(*
args),
1552 UntypedActionResultHolderBase* UntypedPerformAction(
1553 const void* untyped_action,
void* untyped_args)
const override {
1556 const Action<F>
action = *
static_cast<const Action<F>*
>(untyped_action);
1557 ArgumentTuple*
args =
static_cast<ArgumentTuple*
>(untyped_args);
1563 void ClearDefaultActionsLocked()
override
1565 g_gmock_mutex.AssertHeld();
1574 UntypedOnCallSpecs specs_to_delete;
1575 untyped_on_call_specs_.swap(specs_to_delete);
1577 g_gmock_mutex.Unlock();
1578 for (UntypedOnCallSpecs::const_iterator
it =
1579 specs_to_delete.begin();
1580 it != specs_to_delete.end(); ++
it) {
1581 delete static_cast<const OnCallSpec<F>*
>(*it);
1586 g_gmock_mutex.Lock();
1593 ArgumentTuple tuple(std::forward<Args>(
args)...);
1594 std::unique_ptr<ResultHolder> holder(DownCast_<ResultHolder*>(
1595 this->UntypedInvokeWith(
static_cast<void*
>(&tuple))));
1596 return holder->Unwrap();
1599 MockSpec<F> With(Matcher<Args>...
m) {
1604 template <
typename Function>
1605 friend class MockSpec;
1607 typedef ActionResultHolder<Result> ResultHolder;
1610 OnCallSpec<F>& AddNewOnCallSpec(
1612 const ArgumentMatcherTuple&
m)
1614 Mock::RegisterUseByOnCallOrExpectCall(MockObject(),
file,
line);
1615 OnCallSpec<F>*
const on_call_spec =
new OnCallSpec<F>(
file,
line,
m);
1616 untyped_on_call_specs_.push_back(on_call_spec);
1617 return *on_call_spec;
1621 TypedExpectation<F>& AddNewExpectation(
const char*
file,
int line,
1623 const ArgumentMatcherTuple&
m)
1625 Mock::RegisterUseByOnCallOrExpectCall(MockObject(),
file,
line);
1627 new TypedExpectation<F>(
this,
file,
line, source_text,
m);
1628 const std::shared_ptr<ExpectationBase> untyped_expectation(
expectation);
1631 untyped_expectations_.push_back(untyped_expectation);
1635 if (implicit_sequence !=
nullptr) {
1636 implicit_sequence->AddExpectation(
Expectation(untyped_expectation));
1643 template <
typename Func>
friend class TypedExpectation;
1650 void DescribeDefaultActionTo(
const ArgumentTuple&
args,
1651 ::std::ostream* os)
const {
1652 const OnCallSpec<F>*
const spec = FindOnCallSpec(
args);
1654 if (
spec ==
nullptr) {
1656 "returning directly.\n" :
1657 "returning default value.\n");
1659 *os <<
"taking default action specified at:\n"
1667 void UntypedDescribeUninterestingCall(
const void* untyped_args,
1668 ::std::ostream* os)
const override
1670 const ArgumentTuple&
args =
1671 *
static_cast<const ArgumentTuple*
>(untyped_args);
1672 *os <<
"Uninteresting mock function call - ";
1673 DescribeDefaultActionTo(
args, os);
1674 *os <<
" Function call: " << Name();
1694 const ExpectationBase* UntypedFindMatchingExpectation(
1695 const void* untyped_args,
const void** untyped_action,
bool* is_excessive,
1696 ::std::ostream* what, ::std::ostream* why)
override
1698 const ArgumentTuple&
args =
1699 *
static_cast<const ArgumentTuple*
>(untyped_args);
1701 TypedExpectation<F>* exp = this->FindMatchingExpectationLocked(
args);
1702 if (exp ==
nullptr) {
1703 this->FormatUnexpectedCallMessageLocked(
args, what, why);
1710 *is_excessive = exp->IsSaturated();
1711 const Action<F>*
action = exp->GetActionForArguments(
this,
args, what, why);
1714 *untyped_action =
action;
1719 void UntypedPrintArgs(
const void* untyped_args,
1720 ::std::ostream* os)
const override {
1721 const ArgumentTuple&
args =
1722 *
static_cast<const ArgumentTuple*
>(untyped_args);
1728 TypedExpectation<F>* FindMatchingExpectationLocked(
1729 const ArgumentTuple&
args)
const
1731 g_gmock_mutex.AssertHeld();
1734 for (
typename UntypedExpectations::const_reverse_iterator
it =
1735 untyped_expectations_.rbegin();
1736 it != untyped_expectations_.rend(); ++
it) {
1737 TypedExpectation<F>*
const exp =
1738 static_cast<TypedExpectation<F>*
>(
it->get());
1739 if (exp->ShouldHandleArguments(
args)) {
1747 void FormatUnexpectedCallMessageLocked(
1748 const ArgumentTuple&
args,
1750 ::std::ostream* why)
const
1752 g_gmock_mutex.AssertHeld();
1753 *os <<
"\nUnexpected mock function call - ";
1754 DescribeDefaultActionTo(
args, os);
1755 PrintTriedExpectationsLocked(
args, why);
1760 void PrintTriedExpectationsLocked(
1761 const ArgumentTuple&
args,
1762 ::std::ostream* why)
const
1764 g_gmock_mutex.AssertHeld();
1765 const size_t count = untyped_expectations_.size();
1766 *why <<
"Google Mock tried the following " <<
count <<
" "
1767 << (
count == 1 ?
"expectation, but it didn't match" :
1768 "expectations, but none matched")
1770 for (
size_t i = 0;
i <
count;
i++) {
1772 static_cast<TypedExpectation<F>*
>(untyped_expectations_[
i].get());
1776 *why <<
"tried expectation #" <<
i <<
": ";
1839 template <
typename F>
1842 template <
typename R,
typename...
Args>
1843 class MockFunction<R(
Args...)> {
1846 MockFunction(
const MockFunction&) =
delete;
1847 MockFunction& operator=(
const MockFunction&) =
delete;
1851 return this->
Call(std::forward<Args>(
args)...);
1857 mock_.SetOwnerAndName(
this,
"Call");
1858 return mock_.Invoke(std::forward<Args>(
args)...);
1861 internal::MockSpec<R(
Args...)> gmock_Call(Matcher<Args>...
m) {
1862 mock_.RegisterOwner(
this);
1866 internal::MockSpec<R(
Args...)> gmock_Call(
const internal::WithoutMatchers&,
1868 return this->gmock_Call(::testing::A<Args>()...);
1872 mutable internal::FunctionMocker<R(
Args...)> mock_;
1880 using internal::MockSpec;
1897 template <
typename T>
1898 inline const T&
Const(
const T&
x) {
return x; }
1902 : expectation_base_(exp.GetHandle().expectation_base()) {}
1970 #define GMOCK_ON_CALL_IMPL_(mock_expr, Setter, call) \
1971 ((mock_expr).gmock_##call)(::testing::internal::GetWithoutMatchers(), \
1973 .Setter(__FILE__, __LINE__, #mock_expr, #call)
1975 #define ON_CALL(obj, call) \
1976 GMOCK_ON_CALL_IMPL_(obj, InternalDefaultActionSetAt, call)
1978 #define EXPECT_CALL(obj, call) \
1979 GMOCK_ON_CALL_IMPL_(obj, InternalExpectedAt, call)
1981 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_