61 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_
62 #define GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_
70 #include <type_traits>
73 #include "gmock/gmock-actions.h"
74 #include "gmock/gmock-cardinalities.h"
75 #include "gmock/gmock-matchers.h"
76 #include "gmock/internal/gmock-internal-utils.h"
77 #include "gmock/internal/gmock-port.h"
78 #include "gtest/gtest.h"
80 #if GTEST_HAS_EXCEPTIONS
100 template <
typename F>
class FunctionMocker;
103 class ExpectationBase;
106 template <
typename F>
class TypedExpectation;
109 class ExpectationTester;
125 class UntypedActionResultHolderBase;
132 UntypedFunctionMockerBase();
133 virtual ~UntypedFunctionMockerBase();
138 bool VerifyAndClearExpectationsLocked()
142 virtual void ClearDefaultActionsLocked()
154 virtual UntypedActionResultHolderBase* UntypedPerformDefaultAction(
155 void* untyped_args,
const std::string& call_description)
const = 0;
160 virtual UntypedActionResultHolderBase* UntypedPerformAction(
161 const void* untyped_action,
void* untyped_args)
const = 0;
166 virtual void UntypedDescribeUninterestingCall(
167 const void* untyped_args,
168 ::std::ostream* os)
const
177 virtual const ExpectationBase* UntypedFindMatchingExpectation(
178 const void* untyped_args,
179 const void** untyped_action,
bool* is_excessive,
180 ::std::ostream* what, ::std::ostream* why)
184 virtual void UntypedPrintArgs(
const void* untyped_args,
185 ::std::ostream* os)
const = 0;
191 void RegisterOwner(
const void* mock_obj)
197 void SetOwnerAndName(
const void* mock_obj,
const char*
name)
203 const void* MockObject()
const
208 const char* Name()
const
215 UntypedActionResultHolderBase* UntypedInvokeWith(
void* untyped_args)
219 typedef std::vector<const void*> UntypedOnCallSpecs;
221 using UntypedExpectations = std::vector<std::shared_ptr<ExpectationBase>>;
230 const void* mock_obj_;
237 UntypedOnCallSpecs untyped_on_call_specs_;
248 UntypedExpectations untyped_expectations_;
252 class UntypedOnCallSpecBase {
255 UntypedOnCallSpecBase(
const char* a_file,
int a_line)
256 :
file_(a_file),
line_(a_line), last_clause_(kNone) {}
259 const char*
file()
const {
return file_; }
273 void AssertSpecProperty(
bool property,
279 void ExpectSpecProperty(
bool property,
293 template <
typename F>
294 class OnCallSpec :
public UntypedOnCallSpecBase {
296 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
297 typedef typename Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple;
301 OnCallSpec(
const char* a_file,
int a_line,
302 const ArgumentMatcherTuple&
matchers)
303 : UntypedOnCallSpecBase(a_file, a_line),
311 OnCallSpec& With(
const Matcher<const ArgumentTuple&>&
m) {
313 ExpectSpecProperty(last_clause_ < kWith,
314 ".With() cannot appear "
315 "more than once in an ON_CALL().");
316 last_clause_ = kWith;
323 OnCallSpec& WillByDefault(
const Action<F>&
action) {
324 ExpectSpecProperty(last_clause_ < kWillByDefault,
325 ".WillByDefault() must appear "
326 "exactly once in an ON_CALL().");
327 last_clause_ = kWillByDefault;
329 ExpectSpecProperty(!
action.IsDoDefault(),
330 "DoDefault() cannot be used in ON_CALL().");
341 const Action<F>& GetAction()
const {
342 AssertSpecProperty(last_clause_ == kWillByDefault,
343 ".WillByDefault() must appear exactly "
344 "once in an ON_CALL().");
363 Matcher<const ArgumentTuple&> extra_matcher_;
383 static void AllowLeak(
const void* mock_obj)
389 static bool VerifyAndClearExpectations(
void* mock_obj)
395 static bool VerifyAndClear(
void* mock_obj)
399 static bool IsNaggy(
void* mock_obj)
402 static bool IsNice(
void* mock_obj)
405 static bool IsStrict(
void* mock_obj)
409 friend class internal::UntypedFunctionMockerBase;
413 template <
typename F>
414 friend class internal::FunctionMocker;
416 template <
typename M>
417 friend class NiceMock;
419 template <
typename M>
420 friend class NaggyMock;
422 template <
typename M>
423 friend class StrictMock;
427 static void AllowUninterestingCalls(
const void* mock_obj)
432 static void WarnUninterestingCalls(
const void* mock_obj)
437 static void FailUninterestingCalls(
const void* mock_obj)
442 static void UnregisterCallReaction(
const void* mock_obj)
448 const void* mock_obj)
454 static bool VerifyAndClearExpectationsLocked(
void* mock_obj)
458 static void ClearDefaultActionsLocked(
void* mock_obj)
462 static void Register(
463 const void* mock_obj,
464 internal::UntypedFunctionMockerBase* mocker)
470 static void RegisterUseByOnCallOrExpectCall(
471 const void* mock_obj,
const char*
file,
int line)
478 static void UnregisterLocked(internal::UntypedFunctionMockerBase* mocker)
521 return expectation_base_ == rhs.expectation_base_;
527 friend class ExpectationSet;
528 friend class Sequence;
529 friend class ::testing::internal::ExpectationBase;
530 friend class ::testing::internal::UntypedFunctionMockerBase;
532 template <
typename F>
533 friend class ::testing::internal::FunctionMocker;
535 template <
typename F>
536 friend class ::testing::internal::TypedExpectation;
542 return lhs.expectation_base_.get() < rhs.expectation_base_.get();
546 typedef ::std::set<Expectation, Less> Set;
549 const std::shared_ptr<internal::ExpectationBase>& expectation_base);
552 const std::shared_ptr<internal::ExpectationBase>& expectation_base()
const {
553 return expectation_base_;
557 std::shared_ptr<internal::ExpectationBase> expectation_base_;
573 class ExpectationSet {
576 typedef Expectation::Set::const_iterator const_iterator;
587 ExpectationSet(internal::ExpectationBase& exp) {
603 bool operator==(
const ExpectationSet& rhs)
const {
607 bool operator!=(
const ExpectationSet& rhs)
const {
return !(*
this == rhs); }
640 std::shared_ptr<Expectation> last_expectation_;
672 bool sequence_created_;
702 virtual ~ExpectationBase();
705 const char*
file()
const {
return file_; }
707 const char* source_text()
const {
return source_text_.c_str(); }
709 const Cardinality& cardinality()
const {
return cardinality_; }
712 void DescribeLocationTo(::std::ostream* os)
const {
718 void DescribeCallCountTo(::std::ostream* os)
const
723 virtual void MaybeDescribeExtraMatcherTo(::std::ostream* os) = 0;
726 friend class ::testing::Expectation;
727 friend class UntypedFunctionMockerBase;
741 typedef std::vector<const void*> UntypedActions;
748 void AssertSpecProperty(
bool property,
754 void ExpectSpecProperty(
bool property,
761 void SpecifyCardinality(
const Cardinality& cardinality);
765 bool cardinality_specified()
const {
return cardinality_specified_; }
768 void set_cardinality(
const Cardinality& a_cardinality) {
769 cardinality_ = a_cardinality;
777 void RetireAllPreRequisites()
781 bool is_retired()
const
783 g_gmock_mutex.AssertHeld();
790 g_gmock_mutex.AssertHeld();
795 bool IsSatisfied()
const
797 g_gmock_mutex.AssertHeld();
798 return cardinality().IsSatisfiedByCallCount(call_count_);
802 bool IsSaturated()
const
804 g_gmock_mutex.AssertHeld();
805 return cardinality().IsSaturatedByCallCount(call_count_);
809 bool IsOverSaturated()
const
811 g_gmock_mutex.AssertHeld();
812 return cardinality().IsOverSaturatedByCallCount(call_count_);
816 bool AllPrerequisitesAreSatisfied()
const
820 void FindUnsatisfiedPrerequisites(ExpectationSet*
result)
const
824 int call_count()
const
826 g_gmock_mutex.AssertHeld();
831 void IncrementCallCount()
833 g_gmock_mutex.AssertHeld();
841 void CheckActionCountIfNotDone()
const
844 friend class ::testing::Sequence;
845 friend class ::testing::internal::ExpectationTester;
847 template <
typename Function>
848 friend class TypedExpectation;
851 void UntypedTimes(
const Cardinality& a_cardinality);
859 bool cardinality_specified_;
867 ExpectationSet immediate_prerequisites_;
873 UntypedActions untyped_actions_;
874 bool extra_matcher_specified_;
875 bool repeated_action_specified_;
876 bool retires_on_saturation_;
878 mutable bool action_count_checked_;
885 template <
typename F>
886 class TypedExpectation :
public ExpectationBase {
888 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
889 typedef typename Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple;
892 TypedExpectation(FunctionMocker<F>* owner,
const char* a_file,
int a_line,
894 const ArgumentMatcherTuple&
m)
895 : ExpectationBase(a_file, a_line, a_source_text),
904 ~TypedExpectation()
override {
907 CheckActionCountIfNotDone();
908 for (UntypedActions::const_iterator
it = untyped_actions_.begin();
909 it != untyped_actions_.end(); ++
it) {
910 delete static_cast<const Action<F>*
>(*it);
915 TypedExpectation& With(
const Matcher<const ArgumentTuple&>&
m) {
916 if (last_clause_ == kWith) {
917 ExpectSpecProperty(
false,
918 ".With() cannot appear "
919 "more than once in an EXPECT_CALL().");
921 ExpectSpecProperty(last_clause_ < kWith,
922 ".With() must be the first "
923 "clause in an EXPECT_CALL().");
925 last_clause_ = kWith;
928 extra_matcher_specified_ =
true;
933 TypedExpectation& Times(
const Cardinality& a_cardinality) {
934 ExpectationBase::UntypedTimes(a_cardinality);
939 TypedExpectation& Times(
int n) {
944 TypedExpectation& InSequence(
const Sequence& s) {
945 ExpectSpecProperty(last_clause_ <= kInSequence,
946 ".InSequence() cannot appear after .After(),"
947 " .WillOnce(), .WillRepeatedly(), or "
948 ".RetiresOnSaturation().");
949 last_clause_ = kInSequence;
951 s.AddExpectation(GetHandle());
954 TypedExpectation& InSequence(
const Sequence& s1,
const Sequence& s2) {
955 return InSequence(s1).InSequence(s2);
957 TypedExpectation& InSequence(
const Sequence& s1,
const Sequence& s2,
958 const Sequence& s3) {
959 return InSequence(s1, s2).InSequence(s3);
961 TypedExpectation& InSequence(
const Sequence& s1,
const Sequence& s2,
962 const Sequence& s3,
const Sequence& s4) {
963 return InSequence(s1, s2, s3).InSequence(s4);
965 TypedExpectation& InSequence(
const Sequence& s1,
const Sequence& s2,
966 const Sequence& s3,
const Sequence& s4,
967 const Sequence& s5) {
968 return InSequence(s1, s2, s3, s4).InSequence(s5);
972 TypedExpectation& After(
const ExpectationSet& s) {
973 ExpectSpecProperty(last_clause_ <= kAfter,
974 ".After() cannot appear after .WillOnce(),"
975 " .WillRepeatedly(), or "
976 ".RetiresOnSaturation().");
977 last_clause_ = kAfter;
979 for (ExpectationSet::const_iterator
it = s.begin();
it != s.end(); ++
it) {
980 immediate_prerequisites_ += *
it;
984 TypedExpectation& After(
const ExpectationSet& s1,
const ExpectationSet& s2) {
985 return After(s1).After(s2);
987 TypedExpectation& After(
const ExpectationSet& s1,
const ExpectationSet& s2,
988 const ExpectationSet& s3) {
989 return After(s1, s2).After(s3);
991 TypedExpectation& After(
const ExpectationSet& s1,
const ExpectationSet& s2,
992 const ExpectationSet& s3,
const ExpectationSet& s4) {
993 return After(s1, s2, s3).After(s4);
995 TypedExpectation& After(
const ExpectationSet& s1,
const ExpectationSet& s2,
996 const ExpectationSet& s3,
const ExpectationSet& s4,
997 const ExpectationSet& s5) {
998 return After(s1, s2, s3, s4).After(s5);
1002 TypedExpectation& WillOnce(
const Action<F>&
action) {
1003 ExpectSpecProperty(last_clause_ <= kWillOnce,
1004 ".WillOnce() cannot appear after "
1005 ".WillRepeatedly() or .RetiresOnSaturation().");
1006 last_clause_ = kWillOnce;
1008 untyped_actions_.push_back(
new Action<F>(
action));
1009 if (!cardinality_specified()) {
1010 set_cardinality(
Exactly(
static_cast<int>(untyped_actions_.size())));
1016 TypedExpectation& WillRepeatedly(
const Action<F>&
action) {
1017 if (last_clause_ == kWillRepeatedly) {
1018 ExpectSpecProperty(
false,
1019 ".WillRepeatedly() cannot appear "
1020 "more than once in an EXPECT_CALL().");
1022 ExpectSpecProperty(last_clause_ < kWillRepeatedly,
1023 ".WillRepeatedly() cannot appear "
1024 "after .RetiresOnSaturation().");
1026 last_clause_ = kWillRepeatedly;
1027 repeated_action_specified_ =
true;
1029 repeated_action_ =
action;
1030 if (!cardinality_specified()) {
1031 set_cardinality(
AtLeast(
static_cast<int>(untyped_actions_.size())));
1036 CheckActionCountIfNotDone();
1041 TypedExpectation& RetiresOnSaturation() {
1042 ExpectSpecProperty(last_clause_ < kRetiresOnSaturation,
1043 ".RetiresOnSaturation() cannot appear "
1045 last_clause_ = kRetiresOnSaturation;
1046 retires_on_saturation_ =
true;
1050 CheckActionCountIfNotDone();
1056 const ArgumentMatcherTuple&
matchers()
const {
1061 const Matcher<const ArgumentTuple&>& extra_matcher()
const {
1062 return extra_matcher_;
1066 const Action<F>& repeated_action()
const {
return repeated_action_; }
1070 void MaybeDescribeExtraMatcherTo(::std::ostream* os)
override {
1071 if (extra_matcher_specified_) {
1072 *os <<
" Expected args: ";
1073 extra_matcher_.DescribeTo(os);
1079 template <
typename Function>
1080 friend class FunctionMocker;
1084 Expectation GetHandle()
override {
return owner_->GetHandleOf(
this); }
1093 g_gmock_mutex.AssertHeld();
1098 bool ShouldHandleArguments(
const ArgumentTuple&
args)
const
1100 g_gmock_mutex.AssertHeld();
1106 CheckActionCountIfNotDone();
1107 return !is_retired() && AllPrerequisitesAreSatisfied() &&
Matches(
args);
1112 void ExplainMatchResultTo(
1113 const ArgumentTuple&
args,
1114 ::std::ostream* os)
const
1116 g_gmock_mutex.AssertHeld();
1119 *os <<
" Expected: the expectation is active\n"
1120 <<
" Actual: it is retired\n";
1125 StringMatchResultListener listener;
1126 if (!extra_matcher_.MatchAndExplain(
args, &listener)) {
1127 *os <<
" Expected args: ";
1128 extra_matcher_.DescribeTo(os);
1129 *os <<
"\n Actual: don't match";
1134 }
else if (!AllPrerequisitesAreSatisfied()) {
1135 *os <<
" Expected: all pre-requisites are satisfied\n"
1136 <<
" Actual: the following immediate pre-requisites "
1137 <<
"are not satisfied:\n";
1138 ExpectationSet unsatisfied_prereqs;
1139 FindUnsatisfiedPrerequisites(&unsatisfied_prereqs);
1141 for (ExpectationSet::const_iterator
it = unsatisfied_prereqs.begin();
1142 it != unsatisfied_prereqs.end(); ++
it) {
1143 it->expectation_base()->DescribeLocationTo(os);
1144 *os <<
"pre-requisite #" <<
i++ <<
"\n";
1146 *os <<
" (end of pre-requisites)\n";
1152 *os <<
"The call matches the expectation.\n";
1157 const Action<F>& GetCurrentAction(
const FunctionMocker<F>* mocker,
1158 const ArgumentTuple&
args)
const
1160 g_gmock_mutex.AssertHeld();
1161 const int count = call_count();
1163 "call_count() is <= 0 when GetCurrentAction() is "
1164 "called - this should never happen.");
1166 const int action_count =
static_cast<int>(untyped_actions_.size());
1167 if (action_count > 0 && !repeated_action_specified_ &&
1168 count > action_count) {
1171 ::std::stringstream ss;
1172 DescribeLocationTo(&ss);
1173 ss <<
"Actions ran out in " << source_text() <<
"...\n"
1174 <<
"Called " <<
count <<
" times, but only "
1175 << action_count <<
" WillOnce()"
1176 << (action_count == 1 ?
" is" :
"s are") <<
" specified - ";
1177 mocker->DescribeDefaultActionTo(
args, &ss);
1181 return count <= action_count
1182 ? *
static_cast<const Action<F>*
>(
1183 untyped_actions_[
static_cast<size_t>(
count - 1)])
1184 : repeated_action();
1194 const Action<F>* GetActionForArguments(
const FunctionMocker<F>* mocker,
1195 const ArgumentTuple&
args,
1196 ::std::ostream* what,
1197 ::std::ostream* why)
1199 g_gmock_mutex.AssertHeld();
1200 if (IsSaturated()) {
1202 IncrementCallCount();
1203 *what <<
"Mock function called more times than expected - ";
1204 mocker->DescribeDefaultActionTo(
args, what);
1205 DescribeCallCountTo(why);
1210 IncrementCallCount();
1211 RetireAllPreRequisites();
1213 if (retires_on_saturation_ && IsSaturated()) {
1218 *what <<
"Mock function call matches " << source_text() <<
"...\n";
1219 return &(GetCurrentAction(mocker,
args));
1224 FunctionMocker<F>*
const owner_;
1226 Matcher<const ArgumentTuple&> extra_matcher_;
1227 Action<F> repeated_action_;
1247 template <
typename F>
1250 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
1251 typedef typename internal::Function<F>::ArgumentMatcherTuple
1252 ArgumentMatcherTuple;
1256 MockSpec(internal::FunctionMocker<F>* function_mocker,
1257 const ArgumentMatcherTuple&
matchers)
1262 internal::OnCallSpec<F>& InternalDefaultActionSetAt(
1271 internal::TypedExpectation<F>& InternalExpectedAt(
1276 return function_mocker_->AddNewExpectation(
1283 MockSpec<F>& operator()(
const internal::WithoutMatchers&,
void*
const) {
1288 template <
typename Function>
1289 friend class internal::FunctionMocker;
1292 internal::FunctionMocker<F>*
const function_mocker_;
1308 template <
typename T>
1309 class ReferenceOrValueWrapper {
1312 explicit ReferenceOrValueWrapper(
T value)
1325 const T& Peek()
const {
1335 template <
typename T>
1336 class ReferenceOrValueWrapper<
T&> {
1340 typedef T& reference;
1341 explicit ReferenceOrValueWrapper(reference
ref)
1342 : value_ptr_(&
ref) {}
1343 T& Unwrap() {
return *value_ptr_; }
1344 const T& Peek()
const {
return *value_ptr_; }
1365 class UntypedActionResultHolderBase {
1367 virtual ~UntypedActionResultHolderBase() {}
1370 virtual void PrintAsActionResult(::std::ostream* os)
const = 0;
1374 template <
typename T>
1375 class ActionResultHolder :
public UntypedActionResultHolderBase {
1379 return result_.Unwrap();
1383 void PrintAsActionResult(::std::ostream* os)
const override {
1384 *os <<
"\n Returns: ";
1391 template <
typename F>
1392 static ActionResultHolder* PerformDefaultAction(
1393 const FunctionMocker<F>* func_mocker,
1394 typename Function<F>::ArgumentTuple&&
args,
1396 return new ActionResultHolder(Wrapper(func_mocker->PerformDefaultAction(
1402 template <
typename F>
1403 static ActionResultHolder* PerformAction(
1404 const Action<F>&
action,
typename Function<F>::ArgumentTuple&&
args) {
1405 return new ActionResultHolder(
1410 typedef ReferenceOrValueWrapper<T> Wrapper;
1412 explicit ActionResultHolder(Wrapper
result)
1423 class ActionResultHolder<void> :
public UntypedActionResultHolderBase {
1427 void PrintAsActionResult(::std::ostream* )
const override {}
1431 template <
typename F>
1432 static ActionResultHolder* PerformDefaultAction(
1433 const FunctionMocker<F>* func_mocker,
1434 typename Function<F>::ArgumentTuple&&
args,
1436 func_mocker->PerformDefaultAction(
std::move(
args), call_description);
1437 return new ActionResultHolder;
1442 template <
typename F>
1443 static ActionResultHolder* PerformAction(
1444 const Action<F>&
action,
typename Function<F>::ArgumentTuple&&
args) {
1446 return new ActionResultHolder;
1450 ActionResultHolder() {}
1454 template <
typename F>
1455 class FunctionMocker;
1457 template <
typename R,
typename...
Args>
1458 class FunctionMocker<R(
Args...)> final :
public UntypedFunctionMockerBase {
1459 using F = R(
Args...);
1463 using ArgumentTuple = std::tuple<
Args...>;
1464 using ArgumentMatcherTuple = std::tuple<Matcher<Args>...>;
1480 FunctionMocker(
const FunctionMocker&) =
delete;
1481 FunctionMocker& operator=(
const FunctionMocker&) =
delete;
1488 VerifyAndClearExpectationsLocked();
1489 Mock::UnregisterLocked(
this);
1490 ClearDefaultActionsLocked();
1496 const OnCallSpec<F>* FindOnCallSpec(
1497 const ArgumentTuple&
args)
const {
1498 for (UntypedOnCallSpecs::const_reverse_iterator
it
1499 = untyped_on_call_specs_.rbegin();
1500 it != untyped_on_call_specs_.rend(); ++
it) {
1501 const OnCallSpec<F>*
spec =
static_cast<const OnCallSpec<F>*
>(*it);
1516 Result PerformDefaultAction(ArgumentTuple&&
args,
1518 const OnCallSpec<F>*
const spec =
1519 this->FindOnCallSpec(
args);
1520 if (
spec !=
nullptr) {
1525 "\n The mock function has no default action "
1526 "set, and its return type has no default value set.";
1527 #if GTEST_HAS_EXCEPTIONS
1528 if (!DefaultValue<Result>::Exists()) {
1529 throw std::runtime_error(
message);
1542 UntypedActionResultHolderBase* UntypedPerformDefaultAction(
1544 const std::string& call_description)
const override {
1545 ArgumentTuple*
args =
static_cast<ArgumentTuple*
>(untyped_args);
1546 return ResultHolder::PerformDefaultAction(
this,
std::move(*
args),
1554 UntypedActionResultHolderBase* UntypedPerformAction(
1555 const void* untyped_action,
void* untyped_args)
const override {
1558 const Action<F>
action = *
static_cast<const Action<F>*
>(untyped_action);
1559 ArgumentTuple*
args =
static_cast<ArgumentTuple*
>(untyped_args);
1565 void ClearDefaultActionsLocked()
override
1567 g_gmock_mutex.AssertHeld();
1576 UntypedOnCallSpecs specs_to_delete;
1577 untyped_on_call_specs_.swap(specs_to_delete);
1579 g_gmock_mutex.Unlock();
1580 for (UntypedOnCallSpecs::const_iterator
it =
1581 specs_to_delete.begin();
1582 it != specs_to_delete.end(); ++
it) {
1583 delete static_cast<const OnCallSpec<F>*
>(*it);
1588 g_gmock_mutex.Lock();
1595 ArgumentTuple tuple(std::forward<Args>(
args)...);
1596 std::unique_ptr<ResultHolder> holder(DownCast_<ResultHolder*>(
1597 this->UntypedInvokeWith(
static_cast<void*
>(&tuple))));
1598 return holder->Unwrap();
1601 MockSpec<F> With(Matcher<Args>...
m) {
1606 template <
typename Function>
1607 friend class MockSpec;
1609 typedef ActionResultHolder<Result> ResultHolder;
1612 OnCallSpec<F>& AddNewOnCallSpec(
1614 const ArgumentMatcherTuple&
m)
1616 Mock::RegisterUseByOnCallOrExpectCall(MockObject(),
file,
line);
1617 OnCallSpec<F>*
const on_call_spec =
new OnCallSpec<F>(
file,
line,
m);
1618 untyped_on_call_specs_.push_back(on_call_spec);
1619 return *on_call_spec;
1623 TypedExpectation<F>& AddNewExpectation(
const char*
file,
int line,
1625 const ArgumentMatcherTuple&
m)
1627 Mock::RegisterUseByOnCallOrExpectCall(MockObject(),
file,
line);
1629 new TypedExpectation<F>(
this,
file,
line, source_text,
m);
1630 const std::shared_ptr<ExpectationBase> untyped_expectation(
expectation);
1633 untyped_expectations_.push_back(untyped_expectation);
1637 if (implicit_sequence !=
nullptr) {
1638 implicit_sequence->AddExpectation(
Expectation(untyped_expectation));
1645 template <
typename Func>
friend class TypedExpectation;
1652 void DescribeDefaultActionTo(
const ArgumentTuple&
args,
1653 ::std::ostream* os)
const {
1654 const OnCallSpec<F>*
const spec = FindOnCallSpec(
args);
1656 if (
spec ==
nullptr) {
1658 :
"returning default value.\n");
1660 *os <<
"taking default action specified at:\n"
1668 void UntypedDescribeUninterestingCall(
const void* untyped_args,
1669 ::std::ostream* os)
const override
1671 const ArgumentTuple&
args =
1672 *
static_cast<const ArgumentTuple*
>(untyped_args);
1673 *os <<
"Uninteresting mock function call - ";
1674 DescribeDefaultActionTo(
args, os);
1675 *os <<
" Function call: " << Name();
1695 const ExpectationBase* UntypedFindMatchingExpectation(
1696 const void* untyped_args,
const void** untyped_action,
bool* is_excessive,
1697 ::std::ostream* what, ::std::ostream* why)
override
1699 const ArgumentTuple&
args =
1700 *
static_cast<const ArgumentTuple*
>(untyped_args);
1702 TypedExpectation<F>* exp = this->FindMatchingExpectationLocked(
args);
1703 if (exp ==
nullptr) {
1704 this->FormatUnexpectedCallMessageLocked(
args, what, why);
1711 *is_excessive = exp->IsSaturated();
1712 const Action<F>*
action = exp->GetActionForArguments(
this,
args, what, why);
1715 *untyped_action =
action;
1720 void UntypedPrintArgs(
const void* untyped_args,
1721 ::std::ostream* os)
const override {
1722 const ArgumentTuple&
args =
1723 *
static_cast<const ArgumentTuple*
>(untyped_args);
1729 TypedExpectation<F>* FindMatchingExpectationLocked(
1730 const ArgumentTuple&
args)
const
1732 g_gmock_mutex.AssertHeld();
1735 for (
typename UntypedExpectations::const_reverse_iterator
it =
1736 untyped_expectations_.rbegin();
1737 it != untyped_expectations_.rend(); ++
it) {
1738 TypedExpectation<F>*
const exp =
1739 static_cast<TypedExpectation<F>*
>(
it->get());
1740 if (exp->ShouldHandleArguments(
args)) {
1748 void FormatUnexpectedCallMessageLocked(
1749 const ArgumentTuple&
args,
1751 ::std::ostream* why)
const
1753 g_gmock_mutex.AssertHeld();
1754 *os <<
"\nUnexpected mock function call - ";
1755 DescribeDefaultActionTo(
args, os);
1756 PrintTriedExpectationsLocked(
args, why);
1761 void PrintTriedExpectationsLocked(
1762 const ArgumentTuple&
args,
1763 ::std::ostream* why)
const
1765 g_gmock_mutex.AssertHeld();
1766 const size_t count = untyped_expectations_.size();
1767 *why <<
"Google Mock tried the following " <<
count <<
" "
1768 << (
count == 1 ?
"expectation, but it didn't match" :
1769 "expectations, but none matched")
1771 for (
size_t i = 0;
i <
count;
i++) {
1773 static_cast<TypedExpectation<F>*
>(untyped_expectations_[
i].get());
1777 *why <<
"tried expectation #" <<
i <<
": ";
1840 template <
typename F>
1843 template <
typename R,
typename...
Args>
1844 class MockFunction<R(
Args...)> {
1847 MockFunction(
const MockFunction&) =
delete;
1848 MockFunction& operator=(
const MockFunction&) =
delete;
1852 return this->
Call(std::forward<Args>(
args)...);
1858 mock_.SetOwnerAndName(
this,
"Call");
1859 return mock_.Invoke(std::forward<Args>(
args)...);
1862 internal::MockSpec<R(
Args...)> gmock_Call(Matcher<Args>...
m) {
1863 mock_.RegisterOwner(
this);
1867 internal::MockSpec<R(
Args...)> gmock_Call(
const internal::WithoutMatchers&,
1869 return this->gmock_Call(::testing::A<Args>()...);
1873 internal::FunctionMocker<R(
Args...)> mock_;
1881 using internal::MockSpec;
1898 template <
typename T>
1899 inline const T&
Const(
const T&
x) {
return x; }
1903 : expectation_base_(exp.GetHandle().expectation_base()) {}
1971 #define GMOCK_ON_CALL_IMPL_(mock_expr, Setter, call) \
1972 ((mock_expr).gmock_##call)(::testing::internal::GetWithoutMatchers(), \
1974 .Setter(__FILE__, __LINE__, #mock_expr, #call)
1976 #define ON_CALL(obj, call) \
1977 GMOCK_ON_CALL_IMPL_(obj, InternalDefaultActionSetAt, call)
1979 #define EXPECT_CALL(obj, call) \
1980 GMOCK_ON_CALL_IMPL_(obj, InternalExpectedAt, call)
1982 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_