87 {
88
89
90class Expectation;
91
92
93class ExpectationSet;
94
95
96
97namespace internal {
98
99
100template <typename F> class FunctionMocker;
101
102
103class ExpectationBase;
104
105
106template <typename F> class TypedExpectation;
107
108
109class ExpectationTester;
110
111
112template <typename MockClass>
113class NiceMockImpl;
114template <typename MockClass>
115class StrictMockImpl;
116template <typename MockClass>
117class NaggyMockImpl;
118
119
120
121
122
123
124
125
126
127
128
129
131
132
133class UntypedActionResultHolderBase;
134
135
136
137
139 public:
140 UntypedFunctionMockerBase();
141 virtual ~UntypedFunctionMockerBase();
142
143
144
145
146 bool VerifyAndClearExpectationsLocked()
148
149
150 virtual void ClearDefaultActionsLocked()
152
153
154
155
156
157
158
159
160
161
162 virtual UntypedActionResultHolderBase* UntypedPerformDefaultAction(
163 void* untyped_args,
const std::
string& call_description)
const = 0;
164
165
166
167
168 virtual UntypedActionResultHolderBase* UntypedPerformAction(
169 const void* untyped_action,
void* untyped_args)
const = 0;
170
171
172
173
174 virtual void UntypedDescribeUninterestingCall(
175 const void* untyped_args,
178
179
180
181
182
183
184
185 virtual
const ExpectationBase* UntypedFindMatchingExpectation(
186 const void* untyped_args,
187 const void** untyped_action,
bool* is_excessive,
188 ::
std::ostream* what, ::
std::ostream* why)
190
191
192 virtual
void UntypedPrintArgs(
const void* untyped_args,
194
195
196
197
198
199 void RegisterOwner(
const void* mock_obj)
201
202
203
204
205 void SetOwnerAndName(
const void* mock_obj,
const char* name)
207
208
209
210
213
214
215
218
219
220
221
222
223 UntypedActionResultHolderBase* UntypedInvokeWith(void* untyped_args)
225
226 protected:
227 typedef
std::vector<
const void*> UntypedOnCallSpecs;
228
229 using UntypedExpectations =
std::vector<
std::shared_ptr<ExpectationBase>>;
230
231
232
233 Expectation GetHandleOf(ExpectationBase* exp);
234
235
236
237
238 const void* mock_obj_;
239
240
241
243
244
245 UntypedOnCallSpecs untyped_on_call_specs_;
246
247
248
249
250
251
252
253
254
255
256 UntypedExpectations untyped_expectations_;
257};
258
259
260class UntypedOnCallSpecBase {
261 public:
262
263 UntypedOnCallSpecBase(const char* a_file, int a_line)
264 : file_(a_file), line_(a_line), last_clause_(
kNone) {}
265
266
267 const char*
file()
const {
return file_; }
268 int line() const { return line_; }
269
270 protected:
271
272 enum Clause {
273
274
276 kWith,
277 kWillByDefault
278 };
279
280
281 void AssertSpecProperty(bool property,
282 const std::string& failure_message) const {
283 Assert(property, file_, line_, failure_message);
284 }
285
286
287 void ExpectSpecProperty(bool property,
288 const std::string& failure_message) const {
289 Expect(property, file_, line_, failure_message);
290 }
291
292 const char* file_;
293 int line_;
294
295
296
297 Clause last_clause_;
298};
299
300
301template <typename F>
302class OnCallSpec : public UntypedOnCallSpecBase {
303 public:
304 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
305 typedef typename Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple;
306
307
308
309 OnCallSpec(const char* a_file, int a_line,
310 const ArgumentMatcherTuple& matchers)
311 : UntypedOnCallSpecBase(a_file, a_line),
312 matchers_(matchers),
313
314
315
316 extra_matcher_(A<
const ArgumentTuple&>()) {}
317
318
319 OnCallSpec& With(const Matcher<const ArgumentTuple&>& m) {
320
321 ExpectSpecProperty(last_clause_ < kWith,
322 ".With() cannot appear "
323 "more than once in an ON_CALL().");
324 last_clause_ = kWith;
325
326 extra_matcher_ = m;
327 return *this;
328 }
329
330
331 OnCallSpec& WillByDefault(
const Action<F>&
action) {
332 ExpectSpecProperty(last_clause_ < kWillByDefault,
333 ".WillByDefault() must appear "
334 "exactly once in an ON_CALL().");
335 last_clause_ = kWillByDefault;
336
337 ExpectSpecProperty(!
action.IsDoDefault(),
338 "DoDefault() cannot be used in ON_CALL().");
340 return *this;
341 }
342
343
344 bool Matches(const ArgumentTuple& args) const {
345 return TupleMatches(matchers_, args) && extra_matcher_.Matches(args);
346 }
347
348
349 const Action<F>& GetAction() const {
350 AssertSpecProperty(last_clause_ == kWillByDefault,
351 ".WillByDefault() must appear exactly "
352 "once in an ON_CALL().");
353 return action_;
354 }
355
356 private:
357
358
359
360
361
362
363
364
365
366
367
368
369
370 ArgumentMatcherTuple matchers_;
371 Matcher<const ArgumentTuple&> extra_matcher_;
372 Action<F> action_;
373};
374
375
376enum CallReaction {
377 kAllow,
378 kWarn,
379 kFail,
380};
381
382}
383
384
386 public:
387
388
389
390
391 static void AllowLeak(const void* mock_obj)
393
394
395
396
397 static bool VerifyAndClearExpectations(void* mock_obj)
399
400
401
402
403 static bool VerifyAndClear(void* mock_obj)
405
406
407 static bool IsNaggy(void* mock_obj)
409
410 static bool IsNice(void* mock_obj)
412
413 static bool IsStrict(void* mock_obj)
415
416 private:
417 friend class internal::UntypedFunctionMockerBase;
418
419
420
421 template <typename F>
422 friend class internal::FunctionMocker;
423
424 template <typename MockClass>
425 friend class internal::NiceMockImpl;
426 template <typename MockClass>
427 friend class internal::NaggyMockImpl;
428 template <typename MockClass>
429 friend class internal::StrictMockImpl;
430
431
432
433 static void AllowUninterestingCalls(const void* mock_obj)
435
436
437
438 static void WarnUninterestingCalls(const void* mock_obj)
440
441
442
443 static void FailUninterestingCalls(const void* mock_obj)
445
446
447
448 static void UnregisterCallReaction(const void* mock_obj)
450
451
452
453 static internal::CallReaction GetReactionOnUninterestingCalls(
454 const void* mock_obj)
456
457
458
459
460 static bool VerifyAndClearExpectationsLocked(void* mock_obj)
462
463
464 static void ClearDefaultActionsLocked(void* mock_obj)
466
467
468 static void Register(
469 const void* mock_obj,
470 internal::UntypedFunctionMockerBase* mocker)
472
473
474
475
476 static void RegisterUseByOnCallOrExpectCall(
477 const void* mock_obj,
const char*
file,
int line)
479
480
481
482
483
484 static void UnregisterLocked(internal::UntypedFunctionMockerBase* mocker)
486};
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
505 public:
506
507 Expectation();
508 Expectation(Expectation&&) = default;
509 Expectation(const Expectation&) = default;
510 Expectation& operator=(Expectation&&) = default;
511 Expectation& operator=(const Expectation&) = default;
512 ~Expectation();
513
514
515
516
517
518
519
520
521
522
523 Expectation(internal::ExpectationBase& exp);
524
525
526
527
528
529
530 bool operator==(
const Expectation& rhs)
const {
531 return expectation_base_ == rhs.expectation_base_;
532 }
533
534 bool operator!=(
const Expectation& rhs)
const {
return !(*
this == rhs); }
535
536 private:
537 friend class ExpectationSet;
538 friend class Sequence;
539 friend class ::testing::internal::ExpectationBase;
540 friend class ::testing::internal::UntypedFunctionMockerBase;
541
542 template <typename F>
543 friend class ::testing::internal::FunctionMocker;
544
545 template <typename F>
546 friend class ::testing::internal::TypedExpectation;
547
548
549 class Less {
550 public:
551 bool operator()(const Expectation& lhs, const Expectation& rhs) const {
552 return lhs.expectation_base_.get() < rhs.expectation_base_.get();
553 }
554 };
555
556 typedef ::std::set<Expectation, Less> Set;
557
558 Expectation(
559 const std::shared_ptr<internal::ExpectationBase>& expectation_base);
560
561
562 const std::shared_ptr<internal::ExpectationBase>& expectation_base() const {
563 return expectation_base_;
564 }
565
566
567 std::shared_ptr<internal::ExpectationBase> expectation_base_;
568};
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583class ExpectationSet {
584 public:
585
586 typedef Expectation::Set::const_iterator const_iterator;
587
588
589 typedef Expectation::Set::value_type value_type;
590
591
592 ExpectationSet() {}
593
594
595
596
597 ExpectationSet(internal::ExpectationBase& exp) {
598 *this += Expectation(exp);
599 }
600
601
602
603
604 ExpectationSet(const Expectation& e) {
605 *this += e;
606 }
607
608
609
610
611
612
613 bool operator==(
const ExpectationSet& rhs)
const {
614 return expectations_ == rhs.expectations_;
615 }
616
617 bool operator!=(
const ExpectationSet& rhs)
const {
return !(*
this == rhs); }
618
619
620
621 ExpectationSet&
operator+=(
const Expectation& e) {
622 expectations_.insert(e);
623 return *this;
624 }
625
626 int size() const { return static_cast<int>(expectations_.size()); }
627
628 const_iterator begin() const { return expectations_.begin(); }
629 const_iterator end() const { return expectations_.end(); }
630
631 private:
632 Expectation::Set expectations_;
633};
634
635
636
637
638
640 public:
641
642 Sequence() : last_expectation_(new Expectation) {}
643
644
645
646 void AddExpectation(const Expectation& expectation) const;
647
648 private:
649
650 std::shared_ptr<Expectation> last_expectation_;
651};
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
678 public:
679 InSequence();
680 ~InSequence();
681 private:
682 bool sequence_created_;
683
686
687namespace internal {
688
689
690
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
708 public:
709
710 ExpectationBase(
const char*
file,
int line,
const std::string& source_text);
711
712 virtual ~ExpectationBase();
713
714
715 const char*
file()
const {
return file_; }
716 int line() const { return line_; }
717 const char* source_text() const { return source_text_.c_str(); }
718
719 const Cardinality& cardinality() const { return cardinality_; }
720
721
722 void DescribeLocationTo(::std::ostream* os) const {
724 }
725
726
727
728 void DescribeCallCountTo(::std::ostream* os) const
730
731
732
733 virtual void MaybeDescribeExtraMatcherTo(::std::ostream* os) = 0;
734
735 protected:
736 friend class ::testing::Expectation;
737 friend class UntypedFunctionMockerBase;
738
739 enum Clause {
740
742 kWith,
743 kTimes,
744 kInSequence,
745 kAfter,
746 kWillOnce,
747 kWillRepeatedly,
748 kRetiresOnSaturation
749 };
750
751 typedef std::vector<const void*> UntypedActions;
752
753
754
755 virtual Expectation GetHandle() = 0;
756
757
758 void AssertSpecProperty(bool property,
759 const std::string& failure_message) const {
760 Assert(property, file_, line_, failure_message);
761 }
762
763
764 void ExpectSpecProperty(bool property,
765 const std::string& failure_message) const {
766 Expect(property, file_, line_, failure_message);
767 }
768
769
770
771 void SpecifyCardinality(const Cardinality& cardinality);
772
773
774
775 bool cardinality_specified() const { return cardinality_specified_; }
776
777
778 void set_cardinality(const Cardinality& a_cardinality) {
779 cardinality_ = a_cardinality;
780 }
781
782
783
784
785
786
787 void RetireAllPreRequisites()
789
790
791 bool is_retired()
const
793 g_gmock_mutex.AssertHeld();
794 return retired_;
795 }
796
797
798 void Retire()
800 g_gmock_mutex.AssertHeld();
801 retired_ = true;
802 }
803
804
805 bool IsSatisfied()
const
807 g_gmock_mutex.AssertHeld();
808 return cardinality().IsSatisfiedByCallCount(call_count_);
809 }
810
811
812 bool IsSaturated()
const
814 g_gmock_mutex.AssertHeld();
815 return cardinality().IsSaturatedByCallCount(call_count_);
816 }
817
818
819 bool IsOverSaturated()
const
821 g_gmock_mutex.AssertHeld();
822 return cardinality().IsOverSaturatedByCallCount(call_count_);
823 }
824
825
826
827 bool AllPrerequisitesAreSatisfied()
const
829
830
831 void FindUnsatisfiedPrerequisites(ExpectationSet* result)
const
833
834
835 int call_count()
const
837 g_gmock_mutex.AssertHeld();
838 return call_count_;
839 }
840
841
842 void IncrementCallCount()
844 g_gmock_mutex.AssertHeld();
845 call_count_++;
846 }
847
848
849
850
851
852 void CheckActionCountIfNotDone()
const
854
855 friend class ::
testing::Sequence;
856 friend class ::
testing::internal::ExpectationTester;
857
858 template <typename Function>
859 friend class TypedExpectation;
860
861
862 void UntypedTimes(
const Cardinality& a_cardinality);
863
864
865
867 int line_;
869
870 bool cardinality_specified_;
871 Cardinality cardinality_;
872
873
874
875
876
877
878 ExpectationSet immediate_prerequisites_;
879
880
881
882 int call_count_;
883 bool retired_;
884 UntypedActions untyped_actions_;
885 bool extra_matcher_specified_;
886 bool repeated_action_specified_;
887 bool retires_on_saturation_;
888 Clause last_clause_;
889 mutable bool action_count_checked_;
890 mutable Mutex mutex_;
891};
892
893
894template <typename F>
895class TypedExpectation : public ExpectationBase {
896 public:
897 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
898 typedef typename Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple;
899 typedef typename Function<F>::Result Result;
900
901 TypedExpectation(FunctionMocker<F>* owner, const char* a_file, int a_line,
902 const std::string& a_source_text,
903 const ArgumentMatcherTuple& m)
904 : ExpectationBase(a_file, a_line, a_source_text),
905 owner_(owner),
906 matchers_(m),
907
908
909
910 extra_matcher_(A<
const ArgumentTuple&>()),
912
913 ~TypedExpectation() override {
914
915
916 CheckActionCountIfNotDone();
917 for (UntypedActions::const_iterator it = untyped_actions_.begin();
918 it != untyped_actions_.end(); ++it) {
919 delete static_cast<const Action<F>*>(*it);
920 }
921 }
922
923
924 TypedExpectation& With(const Matcher<const ArgumentTuple&>& m) {
925 if (last_clause_ == kWith) {
926 ExpectSpecProperty(false,
927 ".With() cannot appear "
928 "more than once in an EXPECT_CALL().");
929 } else {
930 ExpectSpecProperty(last_clause_ < kWith,
931 ".With() must be the first "
932 "clause in an EXPECT_CALL().");
933 }
934 last_clause_ = kWith;
935
936 extra_matcher_ = m;
937 extra_matcher_specified_ = true;
938 return *this;
939 }
940
941
942 TypedExpectation& Times(const Cardinality& a_cardinality) {
943 ExpectationBase::UntypedTimes(a_cardinality);
944 return *this;
945 }
946
947
948 TypedExpectation& Times(int n) {
950 }
951
952
953 TypedExpectation& InSequence(const Sequence& s) {
954 ExpectSpecProperty(last_clause_ <= kInSequence,
955 ".InSequence() cannot appear after .After(),"
956 " .WillOnce(), .WillRepeatedly(), or "
957 ".RetiresOnSaturation().");
958 last_clause_ = kInSequence;
959
960 s.AddExpectation(GetHandle());
961 return *this;
962 }
963 TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2) {
964 return InSequence(s1).InSequence(s2);
965 }
966 TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2,
967 const Sequence& s3) {
968 return InSequence(s1, s2).InSequence(s3);
969 }
970 TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2,
971 const Sequence& s3, const Sequence& s4) {
972 return InSequence(s1, s2, s3).InSequence(s4);
973 }
974 TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2,
975 const Sequence& s3, const Sequence& s4,
976 const Sequence& s5) {
977 return InSequence(s1, s2, s3, s4).InSequence(s5);
978 }
979
980
981 TypedExpectation& After(const ExpectationSet& s) {
982 ExpectSpecProperty(last_clause_ <= kAfter,
983 ".After() cannot appear after .WillOnce(),"
984 " .WillRepeatedly(), or "
985 ".RetiresOnSaturation().");
986 last_clause_ = kAfter;
987
988 for (ExpectationSet::const_iterator it = s.begin(); it != s.end(); ++it) {
989 immediate_prerequisites_ += *it;
990 }
991 return *this;
992 }
993 TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2) {
994 return After(s1).After(s2);
995 }
996 TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2,
997 const ExpectationSet& s3) {
998 return After(s1, s2).After(s3);
999 }
1000 TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2,
1001 const ExpectationSet& s3, const ExpectationSet& s4) {
1002 return After(s1, s2, s3).After(s4);
1003 }
1004 TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2,
1005 const ExpectationSet& s3, const ExpectationSet& s4,
1006 const ExpectationSet& s5) {
1007 return After(s1, s2, s3, s4).After(s5);
1008 }
1009
1010
1011 TypedExpectation& WillOnce(
const Action<F>&
action) {
1012 ExpectSpecProperty(last_clause_ <= kWillOnce,
1013 ".WillOnce() cannot appear after "
1014 ".WillRepeatedly() or .RetiresOnSaturation().");
1015 last_clause_ = kWillOnce;
1016
1017 untyped_actions_.push_back(
new Action<F>(
action));
1018 if (!cardinality_specified()) {
1019 set_cardinality(
Exactly(
static_cast<int>(untyped_actions_.size())));
1020 }
1021 return *this;
1022 }
1023
1024
1025 TypedExpectation& WillRepeatedly(
const Action<F>&
action) {
1026 if (last_clause_ == kWillRepeatedly) {
1027 ExpectSpecProperty(false,
1028 ".WillRepeatedly() cannot appear "
1029 "more than once in an EXPECT_CALL().");
1030 } else {
1031 ExpectSpecProperty(last_clause_ < kWillRepeatedly,
1032 ".WillRepeatedly() cannot appear "
1033 "after .RetiresOnSaturation().");
1034 }
1035 last_clause_ = kWillRepeatedly;
1036 repeated_action_specified_ = true;
1037
1038 repeated_action_ =
action;
1039 if (!cardinality_specified()) {
1040 set_cardinality(
AtLeast(
static_cast<int>(untyped_actions_.size())));
1041 }
1042
1043
1044
1045 CheckActionCountIfNotDone();
1046 return *this;
1047 }
1048
1049
1050 TypedExpectation& RetiresOnSaturation() {
1051 ExpectSpecProperty(last_clause_ < kRetiresOnSaturation,
1052 ".RetiresOnSaturation() cannot appear "
1053 "more than once.");
1054 last_clause_ = kRetiresOnSaturation;
1055 retires_on_saturation_ = true;
1056
1057
1058
1059 CheckActionCountIfNotDone();
1060 return *this;
1061 }
1062
1063
1064
1065 const ArgumentMatcherTuple& matchers() const {
1066 return matchers_;
1067 }
1068
1069
1070 const Matcher<const ArgumentTuple&>& extra_matcher() const {
1071 return extra_matcher_;
1072 }
1073
1074
1075 const Action<F>& repeated_action() const { return repeated_action_; }
1076
1077
1078
1079 void MaybeDescribeExtraMatcherTo(::std::ostream* os) override {
1080 if (extra_matcher_specified_) {
1081 *os << " Expected args: ";
1082 extra_matcher_.DescribeTo(os);
1083 *os << "\n";
1084 }
1085 }
1086
1087 private:
1088 template <typename Function>
1089 friend class FunctionMocker;
1090
1091
1092
1093 Expectation GetHandle() override { return owner_->GetHandleOf(this); }
1094
1095
1096
1097
1098
1099
1100 bool Matches(const ArgumentTuple& args) const
1102 g_gmock_mutex.AssertHeld();
1103 return TupleMatches(matchers_, args) && extra_matcher_.Matches(args);
1104 }
1105
1106
1107
1108 bool ShouldHandleArguments(const ArgumentTuple& args) const
1110 g_gmock_mutex.AssertHeld();
1111
1112
1113
1114
1115
1116 CheckActionCountIfNotDone();
1117 return !is_retired() && AllPrerequisitesAreSatisfied() && Matches(args);
1118 }
1119
1120
1121
1122 void ExplainMatchResultTo(
1123 const ArgumentTuple& args,
1124 ::std::ostream* os) const
1126 g_gmock_mutex.AssertHeld();
1127
1128 if (is_retired()) {
1129 *os << " Expected: the expectation is active\n"
1130 << " Actual: it is retired\n";
1131 } else if (!Matches(args)) {
1132 if (!TupleMatches(matchers_, args)) {
1133 ExplainMatchFailureTupleTo(matchers_, args, os);
1134 }
1135 StringMatchResultListener listener;
1136 if (!extra_matcher_.MatchAndExplain(args, &listener)) {
1137 *os << " Expected args: ";
1138 extra_matcher_.DescribeTo(os);
1139 *os << "\n Actual: don't match";
1140
1141 internal::PrintIfNotEmpty(listener.str(), os);
1142 *os << "\n";
1143 }
1144 } else if (!AllPrerequisitesAreSatisfied()) {
1145 *os << " Expected: all pre-requisites are satisfied\n"
1146 << " Actual: the following immediate pre-requisites "
1147 << "are not satisfied:\n";
1148 ExpectationSet unsatisfied_prereqs;
1149 FindUnsatisfiedPrerequisites(&unsatisfied_prereqs);
1151 for (ExpectationSet::const_iterator it = unsatisfied_prereqs.begin();
1152 it != unsatisfied_prereqs.end(); ++it) {
1153 it->expectation_base()->DescribeLocationTo(os);
1154 *os <<
"pre-requisite #" <<
i++ <<
"\n";
1155 }
1156 *os << " (end of pre-requisites)\n";
1157 } else {
1158
1159
1160
1161
1162 *os << "The call matches the expectation.\n";
1163 }
1164 }
1165
1166
1167 const Action<F>& GetCurrentAction(const FunctionMocker<F>* mocker,
1168 const ArgumentTuple& args) const
1170 g_gmock_mutex.AssertHeld();
1171 const int count = call_count();
1173 "call_count() is <= 0 when GetCurrentAction() is "
1174 "called - this should never happen.");
1175
1176 const int action_count = static_cast<int>(untyped_actions_.size());
1177 if (action_count > 0 && !repeated_action_specified_ &&
1178 count > action_count) {
1179
1180
1181 ::std::stringstream ss;
1182 DescribeLocationTo(&ss);
1183 ss << "Actions ran out in " << source_text() << "...\n"
1184 <<
"Called " <<
count <<
" times, but only "
1185 << action_count << " WillOnce()"
1186 << (action_count == 1 ? " is" : "s are") << " specified - ";
1187 mocker->DescribeDefaultActionTo(args, &ss);
1189 }
1190
1191 return count <= action_count
1192 ? *static_cast<const Action<F>*>(
1193 untyped_actions_[
static_cast<size_t>(
count - 1)])
1194 : repeated_action();
1195 }
1196
1197
1198
1199
1200
1201
1202
1203
1204 const Action<F>* GetActionForArguments(const FunctionMocker<F>* mocker,
1205 const ArgumentTuple& args,
1206 ::std::ostream* what,
1207 ::std::ostream* why)
1209 g_gmock_mutex.AssertHeld();
1210 if (IsSaturated()) {
1211
1212 IncrementCallCount();
1213 *what << "Mock function called more times than expected - ";
1214 mocker->DescribeDefaultActionTo(args, what);
1215 DescribeCallCountTo(why);
1216
1217 return nullptr;
1218 }
1219
1220 IncrementCallCount();
1221 RetireAllPreRequisites();
1222
1223 if (retires_on_saturation_ && IsSaturated()) {
1224 Retire();
1225 }
1226
1227
1228 *what << "Mock function call matches " << source_text() <<"...\n";
1229 return &(GetCurrentAction(mocker, args));
1230 }
1231
1232
1233
1234 FunctionMocker<F>* const owner_;
1235 ArgumentMatcherTuple matchers_;
1236 Matcher<const ArgumentTuple&> extra_matcher_;
1237 Action<F> repeated_action_;
1238
1240};
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1254 const char*
file,
int line,
1256
1257template <typename F>
1258class MockSpec {
1259 public:
1260 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
1261 typedef typename internal::Function<F>::ArgumentMatcherTuple
1262 ArgumentMatcherTuple;
1263
1264
1265
1266 MockSpec(internal::FunctionMocker<F>* function_mocker,
1267 const ArgumentMatcherTuple& matchers)
1268 : function_mocker_(function_mocker), matchers_(matchers) {}
1269
1270
1271
1272 internal::OnCallSpec<F>& InternalDefaultActionSetAt(
1273 const char*
file,
int line,
const char* obj,
const char* call) {
1275 std::string("ON_CALL(") + obj + ", " + call + ") invoked");
1276 return function_mocker_->AddNewOnCallSpec(
file, line, matchers_);
1277 }
1278
1279
1280
1281 internal::TypedExpectation<F>& InternalExpectedAt(
1282 const char*
file,
int line,
const char* obj,
const char* call) {
1283 const std::string source_text(std::string("EXPECT_CALL(") + obj + ", " +
1284 call + ")");
1286 return function_mocker_->AddNewExpectation(
1287 file, line, source_text, matchers_);
1288 }
1289
1290
1291
1292
1293 MockSpec<F>& operator()(const internal::WithoutMatchers&, void* const) {
1294 return *this;
1295 }
1296
1297 private:
1298 template <typename Function>
1299 friend class internal::FunctionMocker;
1300
1301
1302 internal::FunctionMocker<F>* const function_mocker_;
1303
1304 ArgumentMatcherTuple matchers_;
1305};
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316template <typename T>
1317class ReferenceOrValueWrapper {
1318 public:
1319
1320 explicit ReferenceOrValueWrapper(T
value)
1322 }
1323
1324
1325
1326
1327 T Unwrap() { return std::move(value_); }
1328
1329
1330
1331
1332
1333 const T& Peek() const {
1334 return value_;
1335 }
1336
1337 private:
1338 T value_;
1339};
1340
1341
1342
1343template <typename T>
1344class ReferenceOrValueWrapper<T&> {
1345 public:
1346
1347
1348 typedef T& reference;
1349 explicit ReferenceOrValueWrapper(reference ref)
1350 : value_ptr_(&ref) {}
1351 T& Unwrap() { return *value_ptr_; }
1352 const T& Peek() const { return *value_ptr_; }
1353
1354 private:
1355 T* value_ptr_;
1356};
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367class UntypedActionResultHolderBase {
1368 public:
1369 virtual ~UntypedActionResultHolderBase() {}
1370
1371
1372 virtual void PrintAsActionResult(::std::ostream* os) const = 0;
1373};
1374
1375
1376template <typename T>
1377class ActionResultHolder : public UntypedActionResultHolderBase {
1378 public:
1379
1380 T Unwrap() {
1381 return result_.Unwrap();
1382 }
1383
1384
1385 void PrintAsActionResult(::std::ostream* os) const override {
1386 *os << "\n Returns: ";
1387
1389 }
1390
1391
1392
1393 template <typename F>
1394 static ActionResultHolder* PerformDefaultAction(
1395 const FunctionMocker<F>* func_mocker,
1396 typename Function<F>::ArgumentTuple&& args,
1397 const std::string& call_description) {
1398 return new ActionResultHolder(Wrapper(func_mocker->PerformDefaultAction(
1399 std::move(args), call_description)));
1400 }
1401
1402
1403
1404 template <typename F>
1405 static ActionResultHolder* PerformAction(
1406 const Action<F>&
action,
typename Function<F>::ArgumentTuple&& args) {
1407 return new ActionResultHolder(
1408 Wrapper(
action.Perform(std::move(args))));
1409 }
1410
1411 private:
1412 typedef ReferenceOrValueWrapper<T> Wrapper;
1413
1414 explicit ActionResultHolder(Wrapper result)
1415 : result_(
std::move(result)) {
1416 }
1417
1418 Wrapper result_;
1419
1421};
1422
1423
1424template <>
1425class ActionResultHolder<void> : public UntypedActionResultHolderBase {
1426 public:
1427 void Unwrap() { }
1428
1429 void PrintAsActionResult(::std::ostream* ) const override {}
1430
1431
1432
1433 template <typename F>
1434 static ActionResultHolder* PerformDefaultAction(
1435 const FunctionMocker<F>* func_mocker,
1436 typename Function<F>::ArgumentTuple&& args,
1437 const std::string& call_description) {
1438 func_mocker->PerformDefaultAction(std::move(args), call_description);
1439 return new ActionResultHolder;
1440 }
1441
1442
1443
1444 template <typename F>
1445 static ActionResultHolder* PerformAction(
1446 const Action<F>&
action,
typename Function<F>::ArgumentTuple&& args) {
1447 action.Perform(std::move(args));
1448 return new ActionResultHolder;
1449 }
1450
1451 private:
1452 ActionResultHolder() {}
1454};
1455
1456template <typename F>
1457class FunctionMocker;
1458
1459template <typename R, typename... Args>
1460class FunctionMocker<R(Args...)> final : public UntypedFunctionMockerBase {
1461 using F = R(Args...);
1462
1463 public:
1464 using Result = R;
1465 using ArgumentTuple = std::tuple<Args...>;
1466 using ArgumentMatcherTuple = std::tuple<Matcher<Args>...>;
1467
1468 FunctionMocker() {}
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482 FunctionMocker(const FunctionMocker&) = delete;
1483 FunctionMocker& operator=(const FunctionMocker&) = delete;
1484
1485
1486
1487
1490 VerifyAndClearExpectationsLocked();
1491 Mock::UnregisterLocked(this);
1492 ClearDefaultActionsLocked();
1493 }
1494
1495
1496
1497
1498 const OnCallSpec<F>* FindOnCallSpec(
1499 const ArgumentTuple& args) const {
1500 for (UntypedOnCallSpecs::const_reverse_iterator it
1501 = untyped_on_call_specs_.rbegin();
1502 it != untyped_on_call_specs_.rend(); ++it) {
1503 const OnCallSpec<F>* spec = static_cast<const OnCallSpec<F>*>(*it);
1504 if (spec->Matches(args))
1505 return spec;
1506 }
1507
1508 return nullptr;
1509 }
1510
1511
1512
1513
1514
1515
1516
1517
1518 Result PerformDefaultAction(ArgumentTuple&& args,
1519 const std::string& call_description) const {
1520 const OnCallSpec<F>* const spec =
1521 this->FindOnCallSpec(args);
1522 if (spec != nullptr) {
1523 return spec->GetAction().Perform(std::move(args));
1524 }
1526 call_description +
1527 "\n The mock function has no default action "
1528 "set, and its return type has no default value set.";
1529#if GTEST_HAS_EXCEPTIONS
1530 if (!DefaultValue<Result>::Exists()) {
1531 throw std::runtime_error(
message);
1532 }
1533#else
1535#endif
1536 return DefaultValue<Result>::Get();
1537 }
1538
1539
1540
1541
1542
1543
1544 UntypedActionResultHolderBase* UntypedPerformDefaultAction(
1545 void* untyped_args,
1546 const std::string& call_description) const override {
1547 ArgumentTuple* args = static_cast<ArgumentTuple*>(untyped_args);
1548 return ResultHolder::PerformDefaultAction(this, std::move(*args),
1549 call_description);
1550 }
1551
1552
1553
1554
1555
1556 UntypedActionResultHolderBase* UntypedPerformAction(
1557 const void* untyped_action, void* untyped_args) const override {
1558
1559
1560 const Action<F>
action = *
static_cast<const Action<F>*
>(untyped_action);
1561 ArgumentTuple* args = static_cast<ArgumentTuple*>(untyped_args);
1562 return ResultHolder::PerformAction(
action, std::move(*args));
1563 }
1564
1565
1566
1567 void ClearDefaultActionsLocked() override
1569 g_gmock_mutex.AssertHeld();
1570
1571
1572
1573
1574
1575
1576
1577
1578 UntypedOnCallSpecs specs_to_delete;
1579 untyped_on_call_specs_.swap(specs_to_delete);
1580
1581 g_gmock_mutex.Unlock();
1582 for (UntypedOnCallSpecs::const_iterator it =
1583 specs_to_delete.begin();
1584 it != specs_to_delete.end(); ++it) {
1585 delete static_cast<const OnCallSpec<F>*>(*it);
1586 }
1587
1588
1589
1590 g_gmock_mutex.Lock();
1591 }
1592
1593
1594
1595
1597 ArgumentTuple tuple(std::forward<Args>(args)...);
1598 std::unique_ptr<ResultHolder> holder(DownCast_<ResultHolder*>(
1599 this->UntypedInvokeWith(static_cast<void*>(&tuple))));
1600 return holder->Unwrap();
1601 }
1602
1603 MockSpec<F> With(Matcher<Args>... m) {
1604 return MockSpec<F>(this, ::std::make_tuple(std::move(m)...));
1605 }
1606
1607 protected:
1608 template <typename Function>
1609 friend class MockSpec;
1610
1611 typedef ActionResultHolder<Result> ResultHolder;
1612
1613
1614 OnCallSpec<F>& AddNewOnCallSpec(
1615 const char*
file,
int line,
1616 const ArgumentMatcherTuple& m)
1618 Mock::RegisterUseByOnCallOrExpectCall(MockObject(),
file, line);
1619 OnCallSpec<F>*
const on_call_spec =
new OnCallSpec<F>(
file, line, m);
1620 untyped_on_call_specs_.push_back(on_call_spec);
1621 return *on_call_spec;
1622 }
1623
1624
1625 TypedExpectation<F>& AddNewExpectation(
const char*
file,
int line,
1626 const std::string& source_text,
1627 const ArgumentMatcherTuple& m)
1629 Mock::RegisterUseByOnCallOrExpectCall(MockObject(),
file, line);
1630 TypedExpectation<F>* const expectation =
1631 new TypedExpectation<F>(
this,
file, line, source_text, m);
1632 const std::shared_ptr<ExpectationBase> untyped_expectation(expectation);
1633
1634
1635 untyped_expectations_.push_back(untyped_expectation);
1636
1637
1639 if (implicit_sequence != nullptr) {
1640 implicit_sequence->AddExpectation(Expectation(untyped_expectation));
1641 }
1642
1643 return *expectation;
1644 }
1645
1646 private:
1647 template <typename Func> friend class TypedExpectation;
1648
1649
1650
1651
1652
1653
1654 void DescribeDefaultActionTo(const ArgumentTuple& args,
1655 ::std::ostream* os) const {
1656 const OnCallSpec<F>* const spec = FindOnCallSpec(args);
1657
1658 if (spec == nullptr) {
1660 : "returning default value.\n");
1661 } else {
1662 *os << "taking default action specified at:\n"
1664 }
1665 }
1666
1667
1668
1669
1670 void UntypedDescribeUninterestingCall(const void* untyped_args,
1671 ::std::ostream* os) const override
1673 const ArgumentTuple& args =
1674 *static_cast<const ArgumentTuple*>(untyped_args);
1675 *os << "Uninteresting mock function call - ";
1676 DescribeDefaultActionTo(args, os);
1677 *os << " Function call: " << Name();
1679 }
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697 const ExpectationBase* UntypedFindMatchingExpectation(
1698 const void* untyped_args, const void** untyped_action, bool* is_excessive,
1699 ::std::ostream* what, ::std::ostream* why) override
1701 const ArgumentTuple& args =
1702 *static_cast<const ArgumentTuple*>(untyped_args);
1704 TypedExpectation<F>* exp = this->FindMatchingExpectationLocked(args);
1705 if (exp == nullptr) {
1706 this->FormatUnexpectedCallMessageLocked(args, what, why);
1707 return nullptr;
1708 }
1709
1710
1711
1712
1713 *is_excessive = exp->IsSaturated();
1714 const Action<F>*
action = exp->GetActionForArguments(
this, args, what, why);
1717 *untyped_action =
action;
1718 return exp;
1719 }
1720
1721
1722 void UntypedPrintArgs(const void* untyped_args,
1723 ::std::ostream* os) const override {
1724 const ArgumentTuple& args =
1725 *static_cast<const ArgumentTuple*>(untyped_args);
1727 }
1728
1729
1730
1731 TypedExpectation<F>* FindMatchingExpectationLocked(
1732 const ArgumentTuple& args) const
1734 g_gmock_mutex.AssertHeld();
1735
1736
1737 for (typename UntypedExpectations::const_reverse_iterator it =
1738 untyped_expectations_.rbegin();
1739 it != untyped_expectations_.rend(); ++it) {
1740 TypedExpectation<F>* const exp =
1741 static_cast<TypedExpectation<F>*>(it->get());
1742 if (exp->ShouldHandleArguments(args)) {
1743 return exp;
1744 }
1745 }
1746 return nullptr;
1747 }
1748
1749
1750 void FormatUnexpectedCallMessageLocked(
1751 const ArgumentTuple& args,
1752 ::std::ostream* os,
1753 ::std::ostream* why) const
1755 g_gmock_mutex.AssertHeld();
1756 *os << "\nUnexpected mock function call - ";
1757 DescribeDefaultActionTo(args, os);
1758 PrintTriedExpectationsLocked(args, why);
1759 }
1760
1761
1762
1763 void PrintTriedExpectationsLocked(
1764 const ArgumentTuple& args,
1765 ::std::ostream* why) const
1767 g_gmock_mutex.AssertHeld();
1768 const size_t count = untyped_expectations_.size();
1769 *why <<
"Google Mock tried the following " <<
count <<
" "
1770 << (
count == 1 ?
"expectation, but it didn't match" :
1771 "expectations, but none matched")
1772 << ":\n";
1773 for (
size_t i = 0;
i <
count;
i++) {
1774 TypedExpectation<F>* const expectation =
1775 static_cast<TypedExpectation<F>*
>(untyped_expectations_[
i].get());
1776 *why << "\n";
1777 expectation->DescribeLocationTo(why);
1779 *why <<
"tried expectation #" <<
i <<
": ";
1780 }
1781 *why << expectation->source_text() << "...\n";
1782 expectation->ExplainMatchResultTo(args, why);
1783 expectation->DescribeCallCountTo(why);
1784 }
1785 }
1786};
1787
1788
1789
1791
1792}
1793
1794namespace internal {
1795
1796template <typename F>
1797class MockFunction;
1798
1799template <typename R, typename... Args>
1800class MockFunction<R(Args...)> {
1801 public:
1802 MockFunction(const MockFunction&) = delete;
1803 MockFunction& operator=(const MockFunction&) = delete;
1804
1805 std::function<R(Args...)> AsStdFunction() {
1806 return [this](Args... args) -> R {
1807 return this->Call(std::forward<Args>(args)...);
1808 };
1809 }
1810
1811
1812 R Call(Args... args) {
1813 mock_.SetOwnerAndName(this, "Call");
1814 return mock_.Invoke(std::forward<Args>(args)...);
1815 }
1816
1817 MockSpec<R(Args...)> gmock_Call(Matcher<Args>... m) {
1818 mock_.RegisterOwner(this);
1819 return mock_.With(std::move(m)...);
1820 }
1821
1822 MockSpec<R(Args...)> gmock_Call(const WithoutMatchers&, R (*)(Args...)) {
1823 return this->gmock_Call(::testing::A<Args>()...);
1824 }
1825
1826 protected:
1827 MockFunction() = default;
1828 ~MockFunction() = default;
1829
1830 private:
1831 FunctionMocker<R(Args...)> mock_;
1832};
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846template <typename F, typename = void>
1847struct SignatureOf;
1848
1849template <typename R, typename... Args>
1850struct SignatureOf<R(Args...)> {
1851 using type = R(Args...);
1852};
1853
1854template <template <typename> class C, typename F>
1855struct SignatureOf<C<F>,
1856 typename
std::enable_if<std::is_function<F>::value>
::type>
1857 : SignatureOf<F> {};
1858
1859template <typename F>
1861
1862}
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924template <typename F>
1925class MockFunction : public internal::MockFunction<internal::SignatureOfT<F>> {
1926 using Base = internal::MockFunction<internal::SignatureOfT<F>>;
1927
1928 public:
1930};
1931
1932
1933
1934
1935
1936
1937using internal::MockSpec;
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954template <typename T>
1955inline const T& Const(
const T&
x) {
return x; }
1956
1957
1958inline Expectation::Expectation(internal::ExpectationBase& exp)
1959 : expectation_base_(exp.GetHandle().expectation_base()) {}
1960
1961}
bool operator!=(const UnicodeText &lhs, const UnicodeText &rhs)
#define GTEST_ATTRIBUTE_UNUSED_
#define GTEST_EXCLUSIVE_LOCK_REQUIRED_(locks)
#define GTEST_LOCK_EXCLUDED_(locks)
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
#define GTEST_DECLARE_STATIC_MUTEX_(mutex)
ICOORD & operator+=(ICOORD &op1, const ICOORD &op2)
GTEST_API_ Cardinality AtLeast(int n)
GTEST_API_ Cardinality Exactly(int n)
internal::DoDefaultAction DoDefault()
std::decay< FunctionImpl >::type Invoke(FunctionImpl &&function_impl)
GTEST_API_ void LogWithLocation(testing::internal::LogSeverity severity, const char *file, int line, const std::string &message)
GTEST_API_ ThreadLocal< Sequence * > g_gmock_implicit_sequence
void UniversalPrint(const T &value, ::std::ostream *os)
GTEST_API_::std::string FormatFileLocation(const char *file, int line)
bool operator==(faketype, faketype)
GTEST_API_ void Log(LogSeverity severity, const std::string &message, int stack_frames_to_skip)
void Assert(bool condition, const char *file, int line, const std::string &msg)
void Expect(bool condition, const char *file, int line, const std::string &msg)
void ReportUninterestingCall(CallReaction reaction, const std::string &msg)
std::string Print(const T &value)