61 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_ 62 #define GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_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>>;
223 Expectation GetHandleOf(ExpectationBase* exp);
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_; }
258 int line()
const {
return line_; }
271 void AssertSpecProperty(
bool property,
272 const std::string& failure_message)
const {
273 Assert(property, file_, line_, failure_message);
277 void ExpectSpecProperty(
bool property,
278 const std::string& failure_message)
const {
279 Expect(property, file_, line_, failure_message);
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),
306 extra_matcher_(A<const ArgumentTuple&>()) {}
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().");
334 bool Matches(
const ArgumentTuple& args)
const {
335 return TupleMatches(matchers_, args) && extra_matcher_.Matches(args);
339 const Action<F>& GetAction()
const {
340 AssertSpecProperty(last_clause_ == kWillByDefault,
341 ".WillByDefault() must appear exactly " 342 "once in an ON_CALL().");
360 ArgumentMatcherTuple matchers_;
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)
445 static internal::CallReaction GetReactionOnUninterestingCalls(
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)
512 Expectation(internal::ExpectationBase& exp);
518 bool operator==(
const Expectation& rhs)
const {
519 return expectation_base_ == rhs.expectation_base_;
522 bool operator!=(
const Expectation& rhs)
const {
return !(*
this == rhs); }
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;
539 bool operator()(
const Expectation& lhs,
const Expectation& rhs)
const {
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;
577 typedef Expectation::Set::value_type value_type;
585 ExpectationSet(internal::ExpectationBase& exp) {
586 *
this += Expectation(exp);
592 ExpectationSet(
const Expectation& e) {
601 bool operator==(
const ExpectationSet& rhs)
const {
602 return expectations_ == rhs.expectations_;
605 bool operator!=(
const ExpectationSet& rhs)
const {
return !(*
this == rhs); }
609 ExpectationSet& operator+=(
const Expectation& e) {
610 expectations_.insert(e);
614 int size()
const {
return static_cast<int>(expectations_.size()); }
616 const_iterator begin()
const {
return expectations_.begin(); }
617 const_iterator end()
const {
return expectations_.end(); }
620 Expectation::Set expectations_;
630 Sequence() : last_expectation_(
new Expectation) {}
634 void AddExpectation(
const Expectation& expectation)
const;
638 std::shared_ptr<Expectation> last_expectation_;
670 bool sequence_created_;
698 ExpectationBase(
const char* file,
int line,
const std::string& source_text);
700 virtual ~ExpectationBase();
703 const char* file()
const {
return file_; }
704 int line()
const {
return line_; }
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;
743 virtual Expectation GetHandle() = 0;
746 void AssertSpecProperty(
bool property,
747 const std::string& failure_message)
const {
748 Assert(property, file_, line_, failure_message);
752 void ExpectSpecProperty(
bool property,
753 const std::string& failure_message)
const {
754 Expect(property, file_, line_, failure_message);
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);
855 const std::string source_text_;
857 bool cardinality_specified_;
858 Cardinality cardinality_;
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_;
877 mutable Mutex mutex_;
883 template <
typename F>
884 class TypedExpectation :
public ExpectationBase {
886 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
887 typedef typename Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple;
888 typedef typename Function<F>::Result Result;
890 TypedExpectation(FunctionMocker<F>* owner,
const char* a_file,
int a_line,
891 const std::string& a_source_text,
892 const ArgumentMatcherTuple& m)
893 : ExpectationBase(a_file, a_line, a_source_text),
899 extra_matcher_(A<const ArgumentTuple&>()),
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); }
1089 bool Matches(
const ArgumentTuple& args)
const 1091 g_gmock_mutex.AssertHeld();
1092 return TupleMatches(matchers_, args) && extra_matcher_.Matches(args);
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";
1119 }
else if (!Matches(args)) {
1120 if (!TupleMatches(matchers_, args)) {
1121 ExplainMatchFailureTupleTo(matchers_, args, os);
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";
1129 internal::PrintIfNotEmpty(listener.str(), os);
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();
1160 Assert(count >= 1, __FILE__, __LINE__,
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_;
1223 ArgumentMatcherTuple matchers_;
1224 Matcher<const ArgumentTuple&> extra_matcher_;
1225 Action<F> repeated_action_;
1242 const char* file,
int line,
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)
1256 : function_mocker_(function_mocker), matchers_(matchers) {}
1260 internal::OnCallSpec<F>& InternalDefaultActionSetAt(
1261 const char* file,
int line,
const char* obj,
const char* call) {
1263 std::string(
"ON_CALL(") + obj +
", " + call +
") invoked");
1264 return function_mocker_->AddNewOnCallSpec(file, line, matchers_);
1269 internal::TypedExpectation<F>& InternalExpectedAt(
1270 const char* file,
int line,
const char* obj,
const char* call) {
1271 const std::string source_text(std::string(
"EXPECT_CALL(") + obj +
", " +
1274 return function_mocker_->AddNewExpectation(
1275 file, line, source_text, matchers_);
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_;
1292 ArgumentMatcherTuple matchers_;
1306 template <
typename T>
1307 class ReferenceOrValueWrapper {
1310 explicit ReferenceOrValueWrapper(T
value)
1311 : value_(std::move(value)) {
1317 T Unwrap() {
return std::move(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,
1393 const std::string& call_description) {
1394 return new ActionResultHolder(Wrapper(func_mocker->PerformDefaultAction(
1395 std::move(args), call_description)));
1400 template <
typename F>
1401 static ActionResultHolder* PerformAction(
1402 const Action<F>&
action,
typename Function<F>::ArgumentTuple&& args) {
1403 return new ActionResultHolder(
1404 Wrapper(action.Perform(std::move(args))));
1408 typedef ReferenceOrValueWrapper<T> Wrapper;
1410 explicit ActionResultHolder(Wrapper result)
1411 : result_(std::move(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,
1433 const std::string& call_description) {
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) {
1443 action.Perform(std::move(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);
1500 if (spec->Matches(args))
1514 Result PerformDefaultAction(ArgumentTuple&& args,
1515 const std::string& call_description)
const {
1516 const OnCallSpec<F>*
const spec =
1517 this->FindOnCallSpec(args);
1518 if (spec !=
nullptr) {
1519 return spec->GetAction().Perform(std::move(args));
1521 const std::string message =
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);
1530 Assert(DefaultValue<Result>::Exists(),
"", -1, message);
1532 return DefaultValue<Result>::Get();
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);
1558 return ResultHolder::PerformAction(action, std::move(*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) {
1600 return MockSpec<F>(
this, ::std::make_tuple(std::move(m)...));
1604 template <
typename Function>
1605 friend class MockSpec;
1607 typedef ActionResultHolder<Result> ResultHolder;
1610 OnCallSpec<F>& AddNewOnCallSpec(
1611 const char* file,
int line,
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,
1622 const std::string& source_text,
1623 const ArgumentMatcherTuple& m)
1625 Mock::RegisterUseByOnCallOrExpectCall(MockObject(), file, line);
1626 TypedExpectation<F>*
const expectation =
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);
1634 Sequence*
const implicit_sequence = g_gmock_implicit_sequence.get();
1635 if (implicit_sequence !=
nullptr) {
1636 implicit_sequence->AddExpectation(Expectation(untyped_expectation));
1639 return *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);
1712 if (action !=
nullptr && action->IsDoDefault())
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++) {
1771 TypedExpectation<F>*
const expectation =
1772 static_cast<TypedExpectation<F>*
>(untyped_expectations_[
i].get());
1774 expectation->DescribeLocationTo(why);
1776 *why <<
"tried expectation #" <<
i <<
": ";
1778 *why << expectation->source_text() <<
"...\n";
1779 expectation->ExplainMatchResultTo(args, why);
1780 expectation->DescribeCallCountTo(why);
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;
1849 std::function<R(Args...)> AsStdFunction() {
1850 return [
this](Args... args) -> R {
1851 return this->Call(std::forward<Args>(args)...);
1856 R Call(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);
1863 return mock_.With(std::move(m)...);
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; }
1901 inline Expectation::Expectation(internal::ExpectationBase& exp)
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_ Definition: gmock-actions.h:59
GTEST_API_ Cardinality AtLeast(int n)
Definition: gmock-cardinalities.cc:139
int * count
Definition: gmock_stress_test.cc:96
void ReportUninterestingCall(CallReaction reaction, const std::string &msg)
Definition: gmock-spec-builders.cc:279
#define GTEST_ATTRIBUTE_UNUSED_
Definition: gtest-port.h:660
#define GTEST_DISABLE_MSC_WARNINGS_POP_()
Definition: gtest-port.h:314
bool operator==(faketype, faketype)
Definition: gtest.h:1498
tuple message
Definition: googletest-output-test.py:337
GTEST_API_::std::string FormatFileLocation(const char *file, int line)
Definition: gtest-port.cc:1018
bool operator!=(faketype, faketype)
Definition: gtest.h:1499
std::string Print(const T &value)
Definition: googletest-printers-test.cc:233
#define GTEST_API_
Definition: gtest-port.h:759
int i
Definition: gmock-matchers_test.cc:711
#define GTEST_LOCK_EXCLUDED_(locks)
Definition: gtest-port.h:2256
GTEST_API_ void LogWithLocation(testing::internal::LogSeverity severity, const char *file, int line, const std::string &message)
Definition: gmock-spec-builders.cc:69
#define GTEST_DISALLOW_ASSIGN_(type)
Definition: gtest-port.h:683
std::decay< FunctionImpl >::type Invoke(FunctionImpl &&function_impl)
Definition: gmock-actions.h:1084
#define GTEST_DECLARE_STATIC_MUTEX_(mutex)
Definition: gtest-port.h:1888
Definition: gmock-internal-utils.h:312
constexpr size_t n
Definition: MatrixInversionTest.cpp:14
GTestMutexLock MutexLock
Definition: gtest-port.h:1903
void UniversalPrint(const T &value,::std::ostream *os)
Definition: gtest-printers.h:864
LogSeverity
Definition: gmock-internal-utils.h:310
int value
Definition: gmock-matchers_test.cc:657
GTEST_API_ Cardinality Exactly(int n)
Definition: gmock-cardinalities.cc:153
internal::DoDefaultAction DoDefault()
Definition: gmock-actions.h:1042
int x
Definition: gmock-matchers_test.cc:3610
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251) namespace testing
Definition: gmock-spec-builders.h:82
void Assert(bool condition, const char *file, int line)
Definition: gmock-internal-utils.h:292
#define GTEST_EXCLUSIVE_LOCK_REQUIRED_(locks)
Definition: gtest-port.h:2255
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
Definition: gtest-port.h:688
Definition: gmock-internal-utils.h:311
GTEST_API_ ThreadLocal< Sequence * > g_gmock_implicit_sequence
Definition: gmock-spec-builders.cc:275
void Expect(bool condition, const char *file, int line, const std::string &msg)
Definition: gmock-internal-utils.h:298
GTEST_API_ void Log(LogSeverity severity, const std::string &message, int stack_frames_to_skip)
Definition: gmock-internal-utils.cc:149
action
Definition: upload.py:393