File indexing completed on 2025-02-21 10:03:08
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061 #ifndef GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_
0062 #define GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_
0063
0064 #include <cstdint>
0065 #include <functional>
0066 #include <map>
0067 #include <memory>
0068 #include <set>
0069 #include <sstream>
0070 #include <string>
0071 #include <type_traits>
0072 #include <utility>
0073 #include <vector>
0074
0075 #include "gmock/gmock-actions.h"
0076 #include "gmock/gmock-cardinalities.h"
0077 #include "gmock/gmock-matchers.h"
0078 #include "gmock/internal/gmock-internal-utils.h"
0079 #include "gmock/internal/gmock-port.h"
0080 #include "gtest/gtest.h"
0081
0082 #if GTEST_HAS_EXCEPTIONS
0083 #include <stdexcept> // NOLINT
0084 #endif
0085
0086 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
0087 )
0088
0089 namespace testing {
0090
0091
0092 class Expectation;
0093
0094
0095 class ExpectationSet;
0096
0097
0098
0099 namespace internal {
0100
0101
0102 template <typename F>
0103 class FunctionMocker;
0104
0105
0106 class ExpectationBase;
0107
0108
0109 template <typename F>
0110 class TypedExpectation;
0111
0112
0113 class ExpectationTester;
0114
0115
0116 template <typename MockClass>
0117 class NiceMockImpl;
0118 template <typename MockClass>
0119 class StrictMockImpl;
0120 template <typename MockClass>
0121 class NaggyMockImpl;
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134 GTEST_API_ GTEST_DECLARE_STATIC_MUTEX_(g_gmock_mutex);
0135
0136
0137
0138
0139 class GTEST_API_ UntypedFunctionMockerBase {
0140 public:
0141 UntypedFunctionMockerBase();
0142 virtual ~UntypedFunctionMockerBase();
0143
0144
0145
0146
0147 bool VerifyAndClearExpectationsLocked()
0148 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex);
0149
0150
0151 virtual void ClearDefaultActionsLocked()
0152 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) = 0;
0153
0154
0155
0156
0157
0158
0159
0160
0161 virtual void UntypedDescribeUninterestingCall(const void* untyped_args,
0162 ::std::ostream* os) const
0163 GTEST_LOCK_EXCLUDED_(g_gmock_mutex) = 0;
0164
0165
0166
0167
0168
0169
0170
0171 virtual const ExpectationBase* UntypedFindMatchingExpectation(
0172 const void* untyped_args, const void** untyped_action, bool* is_excessive,
0173 ::std::ostream* what, ::std::ostream* why)
0174 GTEST_LOCK_EXCLUDED_(g_gmock_mutex) = 0;
0175
0176
0177 virtual void UntypedPrintArgs(const void* untyped_args,
0178 ::std::ostream* os) const = 0;
0179
0180
0181
0182
0183
0184 void RegisterOwner(const void* mock_obj) GTEST_LOCK_EXCLUDED_(g_gmock_mutex);
0185
0186
0187
0188
0189 void SetOwnerAndName(const void* mock_obj, const char* name)
0190 GTEST_LOCK_EXCLUDED_(g_gmock_mutex);
0191
0192
0193
0194
0195 const void* MockObject() const GTEST_LOCK_EXCLUDED_(g_gmock_mutex);
0196
0197
0198
0199 const char* Name() const GTEST_LOCK_EXCLUDED_(g_gmock_mutex);
0200
0201 protected:
0202 typedef std::vector<const void*> UntypedOnCallSpecs;
0203
0204 using UntypedExpectations = std::vector<std::shared_ptr<ExpectationBase>>;
0205
0206
0207
0208 Expectation GetHandleOf(ExpectationBase* exp);
0209
0210
0211
0212
0213 const void* mock_obj_;
0214
0215
0216
0217 const char* name_;
0218
0219
0220 UntypedOnCallSpecs untyped_on_call_specs_;
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231 UntypedExpectations untyped_expectations_;
0232 };
0233
0234
0235 class UntypedOnCallSpecBase {
0236 public:
0237
0238 UntypedOnCallSpecBase(const char* a_file, int a_line)
0239 : file_(a_file), line_(a_line), last_clause_(kNone) {}
0240
0241
0242 const char* file() const { return file_; }
0243 int line() const { return line_; }
0244
0245 protected:
0246
0247 enum Clause {
0248
0249
0250 kNone,
0251 kWith,
0252 kWillByDefault
0253 };
0254
0255
0256 void AssertSpecProperty(bool property,
0257 const std::string& failure_message) const {
0258 Assert(property, file_, line_, failure_message);
0259 }
0260
0261
0262 void ExpectSpecProperty(bool property,
0263 const std::string& failure_message) const {
0264 Expect(property, file_, line_, failure_message);
0265 }
0266
0267 const char* file_;
0268 int line_;
0269
0270
0271
0272 Clause last_clause_;
0273 };
0274
0275
0276 template <typename F>
0277 class OnCallSpec : public UntypedOnCallSpecBase {
0278 public:
0279 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
0280 typedef typename Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple;
0281
0282
0283
0284 OnCallSpec(const char* a_file, int a_line,
0285 const ArgumentMatcherTuple& matchers)
0286 : UntypedOnCallSpecBase(a_file, a_line),
0287 matchers_(matchers),
0288
0289
0290
0291 extra_matcher_(A<const ArgumentTuple&>()) {}
0292
0293
0294 OnCallSpec& With(const Matcher<const ArgumentTuple&>& m) {
0295
0296 ExpectSpecProperty(last_clause_ < kWith,
0297 ".With() cannot appear "
0298 "more than once in an ON_CALL().");
0299 last_clause_ = kWith;
0300
0301 extra_matcher_ = m;
0302 return *this;
0303 }
0304
0305
0306 OnCallSpec& WillByDefault(const Action<F>& action) {
0307 ExpectSpecProperty(last_clause_ < kWillByDefault,
0308 ".WillByDefault() must appear "
0309 "exactly once in an ON_CALL().");
0310 last_clause_ = kWillByDefault;
0311
0312 ExpectSpecProperty(!action.IsDoDefault(),
0313 "DoDefault() cannot be used in ON_CALL().");
0314 action_ = action;
0315 return *this;
0316 }
0317
0318
0319 bool Matches(const ArgumentTuple& args) const {
0320 return TupleMatches(matchers_, args) && extra_matcher_.Matches(args);
0321 }
0322
0323
0324 const Action<F>& GetAction() const {
0325 AssertSpecProperty(last_clause_ == kWillByDefault,
0326 ".WillByDefault() must appear exactly "
0327 "once in an ON_CALL().");
0328 return action_;
0329 }
0330
0331 private:
0332
0333
0334
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344
0345 ArgumentMatcherTuple matchers_;
0346 Matcher<const ArgumentTuple&> extra_matcher_;
0347 Action<F> action_;
0348 };
0349
0350
0351 enum CallReaction {
0352 kAllow,
0353 kWarn,
0354 kFail,
0355 };
0356
0357 }
0358
0359
0360 class GTEST_API_ Mock {
0361 public:
0362
0363
0364
0365
0366 static void AllowLeak(const void* mock_obj)
0367 GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
0368
0369
0370
0371
0372 static bool VerifyAndClearExpectations(void* mock_obj)
0373 GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
0374
0375
0376
0377
0378 static bool VerifyAndClear(void* mock_obj)
0379 GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
0380
0381
0382 static bool IsNaggy(void* mock_obj)
0383 GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
0384
0385 static bool IsNice(void* mock_obj)
0386 GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
0387
0388 static bool IsStrict(void* mock_obj)
0389 GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
0390
0391 private:
0392 friend class internal::UntypedFunctionMockerBase;
0393
0394
0395
0396 template <typename F>
0397 friend class internal::FunctionMocker;
0398
0399 template <typename MockClass>
0400 friend class internal::NiceMockImpl;
0401 template <typename MockClass>
0402 friend class internal::NaggyMockImpl;
0403 template <typename MockClass>
0404 friend class internal::StrictMockImpl;
0405
0406
0407
0408 static void AllowUninterestingCalls(uintptr_t mock_obj)
0409 GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
0410
0411
0412
0413 static void WarnUninterestingCalls(uintptr_t mock_obj)
0414 GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
0415
0416
0417
0418 static void FailUninterestingCalls(uintptr_t mock_obj)
0419 GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
0420
0421
0422
0423 static void UnregisterCallReaction(uintptr_t mock_obj)
0424 GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
0425
0426
0427
0428 static internal::CallReaction GetReactionOnUninterestingCalls(
0429 const void* mock_obj) GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
0430
0431
0432
0433
0434 static bool VerifyAndClearExpectationsLocked(void* mock_obj)
0435 GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex);
0436
0437
0438 static void ClearDefaultActionsLocked(void* mock_obj)
0439 GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex);
0440
0441
0442 static void Register(const void* mock_obj,
0443 internal::UntypedFunctionMockerBase* mocker)
0444 GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
0445
0446
0447
0448
0449 static void RegisterUseByOnCallOrExpectCall(const void* mock_obj,
0450 const char* file, int line)
0451 GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
0452
0453
0454
0455
0456
0457 static void UnregisterLocked(internal::UntypedFunctionMockerBase* mocker)
0458 GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex);
0459 };
0460
0461
0462
0463
0464
0465
0466
0467
0468
0469
0470
0471
0472
0473
0474
0475
0476
0477 class GTEST_API_ Expectation {
0478 public:
0479
0480 Expectation();
0481 Expectation(Expectation&&) = default;
0482 Expectation(const Expectation&) = default;
0483 Expectation& operator=(Expectation&&) = default;
0484 Expectation& operator=(const Expectation&) = default;
0485 ~Expectation();
0486
0487
0488
0489
0490
0491
0492
0493
0494
0495
0496 Expectation(internal::ExpectationBase& exp);
0497
0498
0499
0500
0501
0502
0503 bool operator==(const Expectation& rhs) const {
0504 return expectation_base_ == rhs.expectation_base_;
0505 }
0506
0507 bool operator!=(const Expectation& rhs) const { return !(*this == rhs); }
0508
0509 private:
0510 friend class ExpectationSet;
0511 friend class Sequence;
0512 friend class ::testing::internal::ExpectationBase;
0513 friend class ::testing::internal::UntypedFunctionMockerBase;
0514
0515 template <typename F>
0516 friend class ::testing::internal::FunctionMocker;
0517
0518 template <typename F>
0519 friend class ::testing::internal::TypedExpectation;
0520
0521
0522 class Less {
0523 public:
0524 bool operator()(const Expectation& lhs, const Expectation& rhs) const {
0525 return lhs.expectation_base_.get() < rhs.expectation_base_.get();
0526 }
0527 };
0528
0529 typedef ::std::set<Expectation, Less> Set;
0530
0531 Expectation(
0532 const std::shared_ptr<internal::ExpectationBase>& expectation_base);
0533
0534
0535 const std::shared_ptr<internal::ExpectationBase>& expectation_base() const {
0536 return expectation_base_;
0537 }
0538
0539
0540 std::shared_ptr<internal::ExpectationBase> expectation_base_;
0541 };
0542
0543
0544
0545
0546
0547
0548
0549
0550
0551
0552
0553
0554
0555
0556 class ExpectationSet {
0557 public:
0558
0559 typedef Expectation::Set::const_iterator const_iterator;
0560
0561
0562 typedef Expectation::Set::value_type value_type;
0563
0564
0565 ExpectationSet() {}
0566
0567
0568
0569
0570 ExpectationSet(internal::ExpectationBase& exp) {
0571 *this += Expectation(exp);
0572 }
0573
0574
0575
0576
0577 ExpectationSet(const Expectation& e) {
0578 *this += e;
0579 }
0580
0581
0582
0583
0584
0585
0586 bool operator==(const ExpectationSet& rhs) const {
0587 return expectations_ == rhs.expectations_;
0588 }
0589
0590 bool operator!=(const ExpectationSet& rhs) const { return !(*this == rhs); }
0591
0592
0593
0594 ExpectationSet& operator+=(const Expectation& e) {
0595 expectations_.insert(e);
0596 return *this;
0597 }
0598
0599 int size() const { return static_cast<int>(expectations_.size()); }
0600
0601 const_iterator begin() const { return expectations_.begin(); }
0602 const_iterator end() const { return expectations_.end(); }
0603
0604 private:
0605 Expectation::Set expectations_;
0606 };
0607
0608
0609
0610
0611 class GTEST_API_ Sequence {
0612 public:
0613
0614 Sequence() : last_expectation_(new Expectation) {}
0615
0616
0617
0618 void AddExpectation(const Expectation& expectation) const;
0619
0620 private:
0621
0622 std::shared_ptr<Expectation> last_expectation_;
0623 };
0624
0625
0626
0627
0628
0629
0630
0631
0632
0633
0634
0635
0636
0637
0638
0639
0640
0641
0642
0643
0644
0645
0646
0647
0648
0649 class GTEST_API_ InSequence {
0650 public:
0651 InSequence();
0652 ~InSequence();
0653
0654 private:
0655 bool sequence_created_;
0656
0657 InSequence(const InSequence&) = delete;
0658 InSequence& operator=(const InSequence&) = delete;
0659 } GTEST_ATTRIBUTE_UNUSED_;
0660
0661 namespace internal {
0662
0663
0664
0665 GTEST_API_ extern ThreadLocal<Sequence*> g_gmock_implicit_sequence;
0666
0667
0668
0669
0670
0671
0672
0673
0674
0675
0676
0677
0678
0679
0680
0681 class GTEST_API_ ExpectationBase {
0682 public:
0683
0684 ExpectationBase(const char* file, int line, const std::string& source_text);
0685
0686 virtual ~ExpectationBase();
0687
0688
0689 const char* file() const { return file_; }
0690 int line() const { return line_; }
0691 const char* source_text() const { return source_text_.c_str(); }
0692
0693 const Cardinality& cardinality() const { return cardinality_; }
0694
0695
0696 void DescribeLocationTo(::std::ostream* os) const {
0697 *os << FormatFileLocation(file(), line()) << " ";
0698 }
0699
0700
0701
0702 void DescribeCallCountTo(::std::ostream* os) const
0703 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex);
0704
0705
0706
0707 virtual void MaybeDescribeExtraMatcherTo(::std::ostream* os) = 0;
0708
0709 protected:
0710 friend class ::testing::Expectation;
0711 friend class UntypedFunctionMockerBase;
0712
0713 enum Clause {
0714
0715 kNone,
0716 kWith,
0717 kTimes,
0718 kInSequence,
0719 kAfter,
0720 kWillOnce,
0721 kWillRepeatedly,
0722 kRetiresOnSaturation
0723 };
0724
0725 typedef std::vector<const void*> UntypedActions;
0726
0727
0728
0729 virtual Expectation GetHandle() = 0;
0730
0731
0732 void AssertSpecProperty(bool property,
0733 const std::string& failure_message) const {
0734 Assert(property, file_, line_, failure_message);
0735 }
0736
0737
0738 void ExpectSpecProperty(bool property,
0739 const std::string& failure_message) const {
0740 Expect(property, file_, line_, failure_message);
0741 }
0742
0743
0744
0745 void SpecifyCardinality(const Cardinality& cardinality);
0746
0747
0748
0749 bool cardinality_specified() const { return cardinality_specified_; }
0750
0751
0752 void set_cardinality(const Cardinality& a_cardinality) {
0753 cardinality_ = a_cardinality;
0754 }
0755
0756
0757
0758
0759
0760
0761 void RetireAllPreRequisites() GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex);
0762
0763
0764 bool is_retired() const GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
0765 g_gmock_mutex.AssertHeld();
0766 return retired_;
0767 }
0768
0769
0770 void Retire() GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
0771 g_gmock_mutex.AssertHeld();
0772 retired_ = true;
0773 }
0774
0775
0776 bool IsSatisfied() const GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
0777 g_gmock_mutex.AssertHeld();
0778 return cardinality().IsSatisfiedByCallCount(call_count_);
0779 }
0780
0781
0782 bool IsSaturated() const GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
0783 g_gmock_mutex.AssertHeld();
0784 return cardinality().IsSaturatedByCallCount(call_count_);
0785 }
0786
0787
0788 bool IsOverSaturated() const GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
0789 g_gmock_mutex.AssertHeld();
0790 return cardinality().IsOverSaturatedByCallCount(call_count_);
0791 }
0792
0793
0794
0795 bool AllPrerequisitesAreSatisfied() const
0796 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex);
0797
0798
0799 void FindUnsatisfiedPrerequisites(ExpectationSet* result) const
0800 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex);
0801
0802
0803 int call_count() const GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
0804 g_gmock_mutex.AssertHeld();
0805 return call_count_;
0806 }
0807
0808
0809 void IncrementCallCount() GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
0810 g_gmock_mutex.AssertHeld();
0811 call_count_++;
0812 }
0813
0814
0815
0816
0817
0818 void CheckActionCountIfNotDone() const GTEST_LOCK_EXCLUDED_(mutex_);
0819
0820 friend class ::testing::Sequence;
0821 friend class ::testing::internal::ExpectationTester;
0822
0823 template <typename Function>
0824 friend class TypedExpectation;
0825
0826
0827 void UntypedTimes(const Cardinality& a_cardinality);
0828
0829
0830
0831 const char* file_;
0832 int line_;
0833 const std::string source_text_;
0834
0835 bool cardinality_specified_;
0836 Cardinality cardinality_;
0837
0838
0839
0840
0841
0842
0843 ExpectationSet immediate_prerequisites_;
0844
0845
0846
0847 int call_count_;
0848 bool retired_;
0849 UntypedActions untyped_actions_;
0850 bool extra_matcher_specified_;
0851 bool repeated_action_specified_;
0852 bool retires_on_saturation_;
0853 Clause last_clause_;
0854 mutable bool action_count_checked_;
0855 mutable Mutex mutex_;
0856 };
0857
0858 template <typename F>
0859 class TypedExpectation;
0860
0861
0862 template <typename R, typename... Args>
0863 class TypedExpectation<R(Args...)> : public ExpectationBase {
0864 private:
0865 using F = R(Args...);
0866
0867 public:
0868 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
0869 typedef typename Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple;
0870 typedef typename Function<F>::Result Result;
0871
0872 TypedExpectation(FunctionMocker<F>* owner, const char* a_file, int a_line,
0873 const std::string& a_source_text,
0874 const ArgumentMatcherTuple& m)
0875 : ExpectationBase(a_file, a_line, a_source_text),
0876 owner_(owner),
0877 matchers_(m),
0878
0879
0880
0881 extra_matcher_(A<const ArgumentTuple&>()),
0882 repeated_action_(DoDefault()) {}
0883
0884 ~TypedExpectation() override {
0885
0886
0887 CheckActionCountIfNotDone();
0888 for (UntypedActions::const_iterator it = untyped_actions_.begin();
0889 it != untyped_actions_.end(); ++it) {
0890 delete static_cast<const Action<F>*>(*it);
0891 }
0892 }
0893
0894
0895 TypedExpectation& With(const Matcher<const ArgumentTuple&>& m) {
0896 if (last_clause_ == kWith) {
0897 ExpectSpecProperty(false,
0898 ".With() cannot appear "
0899 "more than once in an EXPECT_CALL().");
0900 } else {
0901 ExpectSpecProperty(last_clause_ < kWith,
0902 ".With() must be the first "
0903 "clause in an EXPECT_CALL().");
0904 }
0905 last_clause_ = kWith;
0906
0907 extra_matcher_ = m;
0908 extra_matcher_specified_ = true;
0909 return *this;
0910 }
0911
0912
0913 TypedExpectation& Times(const Cardinality& a_cardinality) {
0914 ExpectationBase::UntypedTimes(a_cardinality);
0915 return *this;
0916 }
0917
0918
0919 TypedExpectation& Times(int n) { return Times(Exactly(n)); }
0920
0921
0922 TypedExpectation& InSequence(const Sequence& s) {
0923 ExpectSpecProperty(last_clause_ <= kInSequence,
0924 ".InSequence() cannot appear after .After(),"
0925 " .WillOnce(), .WillRepeatedly(), or "
0926 ".RetiresOnSaturation().");
0927 last_clause_ = kInSequence;
0928
0929 s.AddExpectation(GetHandle());
0930 return *this;
0931 }
0932 TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2) {
0933 return InSequence(s1).InSequence(s2);
0934 }
0935 TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2,
0936 const Sequence& s3) {
0937 return InSequence(s1, s2).InSequence(s3);
0938 }
0939 TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2,
0940 const Sequence& s3, const Sequence& s4) {
0941 return InSequence(s1, s2, s3).InSequence(s4);
0942 }
0943 TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2,
0944 const Sequence& s3, const Sequence& s4,
0945 const Sequence& s5) {
0946 return InSequence(s1, s2, s3, s4).InSequence(s5);
0947 }
0948
0949
0950 TypedExpectation& After(const ExpectationSet& s) {
0951 ExpectSpecProperty(last_clause_ <= kAfter,
0952 ".After() cannot appear after .WillOnce(),"
0953 " .WillRepeatedly(), or "
0954 ".RetiresOnSaturation().");
0955 last_clause_ = kAfter;
0956
0957 for (ExpectationSet::const_iterator it = s.begin(); it != s.end(); ++it) {
0958 immediate_prerequisites_ += *it;
0959 }
0960 return *this;
0961 }
0962 TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2) {
0963 return After(s1).After(s2);
0964 }
0965 TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2,
0966 const ExpectationSet& s3) {
0967 return After(s1, s2).After(s3);
0968 }
0969 TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2,
0970 const ExpectationSet& s3, const ExpectationSet& s4) {
0971 return After(s1, s2, s3).After(s4);
0972 }
0973 TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2,
0974 const ExpectationSet& s3, const ExpectationSet& s4,
0975 const ExpectationSet& s5) {
0976 return After(s1, s2, s3, s4).After(s5);
0977 }
0978
0979
0980
0981 TypedExpectation& WillOnce(OnceAction<F> once_action) {
0982
0983
0984
0985 return WillOnce(Action<F>(ActionAdaptor{
0986 std::make_shared<OnceAction<F>>(std::move(once_action)),
0987 }));
0988 }
0989
0990
0991
0992
0993
0994
0995 template <int&... ExplicitArgumentBarrier, typename = void>
0996 TypedExpectation& WillOnce(Action<F> action) {
0997 ExpectSpecProperty(last_clause_ <= kWillOnce,
0998 ".WillOnce() cannot appear after "
0999 ".WillRepeatedly() or .RetiresOnSaturation().");
1000 last_clause_ = kWillOnce;
1001
1002 untyped_actions_.push_back(new Action<F>(std::move(action)));
1003
1004 if (!cardinality_specified()) {
1005 set_cardinality(Exactly(static_cast<int>(untyped_actions_.size())));
1006 }
1007 return *this;
1008 }
1009
1010
1011 TypedExpectation& WillRepeatedly(const Action<F>& action) {
1012 if (last_clause_ == kWillRepeatedly) {
1013 ExpectSpecProperty(false,
1014 ".WillRepeatedly() cannot appear "
1015 "more than once in an EXPECT_CALL().");
1016 } else {
1017 ExpectSpecProperty(last_clause_ < kWillRepeatedly,
1018 ".WillRepeatedly() cannot appear "
1019 "after .RetiresOnSaturation().");
1020 }
1021 last_clause_ = kWillRepeatedly;
1022 repeated_action_specified_ = true;
1023
1024 repeated_action_ = action;
1025 if (!cardinality_specified()) {
1026 set_cardinality(AtLeast(static_cast<int>(untyped_actions_.size())));
1027 }
1028
1029
1030
1031 CheckActionCountIfNotDone();
1032 return *this;
1033 }
1034
1035
1036 TypedExpectation& RetiresOnSaturation() {
1037 ExpectSpecProperty(last_clause_ < kRetiresOnSaturation,
1038 ".RetiresOnSaturation() cannot appear "
1039 "more than once.");
1040 last_clause_ = kRetiresOnSaturation;
1041 retires_on_saturation_ = true;
1042
1043
1044
1045 CheckActionCountIfNotDone();
1046 return *this;
1047 }
1048
1049
1050
1051 const ArgumentMatcherTuple& matchers() const { return matchers_; }
1052
1053
1054 const Matcher<const ArgumentTuple&>& extra_matcher() const {
1055 return extra_matcher_;
1056 }
1057
1058
1059 const Action<F>& repeated_action() const { return repeated_action_; }
1060
1061
1062
1063 void MaybeDescribeExtraMatcherTo(::std::ostream* os) override {
1064 if (extra_matcher_specified_) {
1065 *os << " Expected args: ";
1066 extra_matcher_.DescribeTo(os);
1067 *os << "\n";
1068 }
1069 }
1070
1071 private:
1072 template <typename Function>
1073 friend class FunctionMocker;
1074
1075
1076
1077 struct ActionAdaptor {
1078 std::shared_ptr<OnceAction<R(Args...)>> once_action;
1079
1080 R operator()(Args&&... args) const {
1081 return std::move(*once_action).Call(std::forward<Args>(args)...);
1082 }
1083 };
1084
1085
1086
1087 Expectation GetHandle() override { return owner_->GetHandleOf(this); }
1088
1089
1090
1091
1092
1093
1094 bool Matches(const ArgumentTuple& args) const
1095 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
1096 g_gmock_mutex.AssertHeld();
1097 return TupleMatches(matchers_, args) && extra_matcher_.Matches(args);
1098 }
1099
1100
1101
1102 bool ShouldHandleArguments(const ArgumentTuple& args) const
1103 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
1104 g_gmock_mutex.AssertHeld();
1105
1106
1107
1108
1109
1110 CheckActionCountIfNotDone();
1111 return !is_retired() && AllPrerequisitesAreSatisfied() && Matches(args);
1112 }
1113
1114
1115
1116 void ExplainMatchResultTo(const ArgumentTuple& args, ::std::ostream* os) const
1117 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
1118 g_gmock_mutex.AssertHeld();
1119
1120 if (is_retired()) {
1121 *os << " Expected: the expectation is active\n"
1122 << " Actual: it is retired\n";
1123 } else if (!Matches(args)) {
1124 if (!TupleMatches(matchers_, args)) {
1125 ExplainMatchFailureTupleTo(matchers_, args, os);
1126 }
1127 StringMatchResultListener listener;
1128 if (!extra_matcher_.MatchAndExplain(args, &listener)) {
1129 *os << " Expected args: ";
1130 extra_matcher_.DescribeTo(os);
1131 *os << "\n Actual: don't match";
1132
1133 internal::PrintIfNotEmpty(listener.str(), os);
1134 *os << "\n";
1135 }
1136 } else if (!AllPrerequisitesAreSatisfied()) {
1137 *os << " Expected: all pre-requisites are satisfied\n"
1138 << " Actual: the following immediate pre-requisites "
1139 << "are not satisfied:\n";
1140 ExpectationSet unsatisfied_prereqs;
1141 FindUnsatisfiedPrerequisites(&unsatisfied_prereqs);
1142 int i = 0;
1143 for (ExpectationSet::const_iterator it = unsatisfied_prereqs.begin();
1144 it != unsatisfied_prereqs.end(); ++it) {
1145 it->expectation_base()->DescribeLocationTo(os);
1146 *os << "pre-requisite #" << i++ << "\n";
1147 }
1148 *os << " (end of pre-requisites)\n";
1149 } else {
1150
1151
1152
1153
1154 *os << "The call matches the expectation.\n";
1155 }
1156 }
1157
1158
1159 const Action<F>& GetCurrentAction(const FunctionMocker<F>* mocker,
1160 const ArgumentTuple& args) const
1161 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
1162 g_gmock_mutex.AssertHeld();
1163 const int count = call_count();
1164 Assert(count >= 1, __FILE__, __LINE__,
1165 "call_count() is <= 0 when GetCurrentAction() is "
1166 "called - this should never happen.");
1167
1168 const int action_count = static_cast<int>(untyped_actions_.size());
1169 if (action_count > 0 && !repeated_action_specified_ &&
1170 count > action_count) {
1171
1172
1173 ::std::stringstream ss;
1174 DescribeLocationTo(&ss);
1175 ss << "Actions ran out in " << source_text() << "...\n"
1176 << "Called " << count << " times, but only " << action_count
1177 << " WillOnce()" << (action_count == 1 ? " is" : "s are")
1178 << " specified - ";
1179 mocker->DescribeDefaultActionTo(args, &ss);
1180 Log(kWarning, ss.str(), 1);
1181 }
1182
1183 return count <= action_count
1184 ? *static_cast<const Action<F>*>(
1185 untyped_actions_[static_cast<size_t>(count - 1)])
1186 : repeated_action();
1187 }
1188
1189
1190
1191
1192
1193
1194
1195
1196 const Action<F>* GetActionForArguments(const FunctionMocker<F>* mocker,
1197 const ArgumentTuple& args,
1198 ::std::ostream* what,
1199 ::std::ostream* why)
1200 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
1201 g_gmock_mutex.AssertHeld();
1202 if (IsSaturated()) {
1203
1204 IncrementCallCount();
1205 *what << "Mock function called more times than expected - ";
1206 mocker->DescribeDefaultActionTo(args, what);
1207 DescribeCallCountTo(why);
1208
1209 return nullptr;
1210 }
1211
1212 IncrementCallCount();
1213 RetireAllPreRequisites();
1214
1215 if (retires_on_saturation_ && IsSaturated()) {
1216 Retire();
1217 }
1218
1219
1220 *what << "Mock function call matches " << source_text() << "...\n";
1221 return &(GetCurrentAction(mocker, args));
1222 }
1223
1224
1225
1226 FunctionMocker<F>* const owner_;
1227 ArgumentMatcherTuple matchers_;
1228 Matcher<const ArgumentTuple&> extra_matcher_;
1229 Action<F> repeated_action_;
1230
1231 TypedExpectation(const TypedExpectation&) = delete;
1232 TypedExpectation& operator=(const TypedExpectation&) = delete;
1233 };
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246 GTEST_API_ void LogWithLocation(testing::internal::LogSeverity severity,
1247 const char* file, int line,
1248 const std::string& message);
1249
1250 template <typename F>
1251 class MockSpec {
1252 public:
1253 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
1254 typedef
1255 typename internal::Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple;
1256
1257
1258
1259 MockSpec(internal::FunctionMocker<F>* function_mocker,
1260 const ArgumentMatcherTuple& matchers)
1261 : function_mocker_(function_mocker), matchers_(matchers) {}
1262
1263
1264
1265 internal::OnCallSpec<F>& InternalDefaultActionSetAt(const char* file,
1266 int line, const char* obj,
1267 const char* call) {
1268 LogWithLocation(internal::kInfo, file, line,
1269 std::string("ON_CALL(") + obj + ", " + call + ") invoked");
1270 return function_mocker_->AddNewOnCallSpec(file, line, matchers_);
1271 }
1272
1273
1274
1275 internal::TypedExpectation<F>& InternalExpectedAt(const char* file, int line,
1276 const char* obj,
1277 const char* call) {
1278 const std::string source_text(std::string("EXPECT_CALL(") + obj + ", " +
1279 call + ")");
1280 LogWithLocation(internal::kInfo, file, line, source_text + " invoked");
1281 return function_mocker_->AddNewExpectation(file, line, source_text,
1282 matchers_);
1283 }
1284
1285
1286
1287
1288 MockSpec<F>& operator()(const internal::WithoutMatchers&, void* const) {
1289 return *this;
1290 }
1291
1292 private:
1293 template <typename Function>
1294 friend class internal::FunctionMocker;
1295
1296
1297 internal::FunctionMocker<F>* const function_mocker_;
1298
1299 ArgumentMatcherTuple matchers_;
1300 };
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311 template <typename T>
1312 class ReferenceOrValueWrapper {
1313 public:
1314
1315 explicit ReferenceOrValueWrapper(T value) : value_(std::move(value)) {}
1316
1317
1318
1319
1320 T Unwrap() { return std::move(value_); }
1321
1322
1323
1324
1325
1326 const T& Peek() const { return value_; }
1327
1328 private:
1329 T value_;
1330 };
1331
1332
1333
1334 template <typename T>
1335 class ReferenceOrValueWrapper<T&> {
1336 public:
1337
1338
1339 typedef T& reference;
1340 explicit ReferenceOrValueWrapper(reference ref) : value_ptr_(&ref) {}
1341 T& Unwrap() { return *value_ptr_; }
1342 const T& Peek() const { return *value_ptr_; }
1343
1344 private:
1345 T* value_ptr_;
1346 };
1347
1348
1349 template <typename T>
1350 void PrintAsActionResult(const T& result, std::ostream& os) {
1351 os << "\n Returns: ";
1352
1353 UniversalPrinter<T>::Print(result, &os);
1354 }
1355
1356
1357
1358 GTEST_API_ void ReportUninterestingCall(CallReaction reaction,
1359 const std::string& msg);
1360
1361
1362 class Cleanup final {
1363 public:
1364 explicit Cleanup(std::function<void()> f) : f_(std::move(f)) {}
1365 ~Cleanup() { f_(); }
1366
1367 private:
1368 std::function<void()> f_;
1369 };
1370
1371 template <typename F>
1372 class FunctionMocker;
1373
1374 template <typename R, typename... Args>
1375 class FunctionMocker<R(Args...)> final : public UntypedFunctionMockerBase {
1376 using F = R(Args...);
1377
1378 public:
1379 using Result = R;
1380 using ArgumentTuple = std::tuple<Args...>;
1381 using ArgumentMatcherTuple = std::tuple<Matcher<Args>...>;
1382
1383 FunctionMocker() {}
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397 FunctionMocker(const FunctionMocker&) = delete;
1398 FunctionMocker& operator=(const FunctionMocker&) = delete;
1399
1400
1401
1402
1403 ~FunctionMocker() override GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
1404 MutexLock l(&g_gmock_mutex);
1405 VerifyAndClearExpectationsLocked();
1406 Mock::UnregisterLocked(this);
1407 ClearDefaultActionsLocked();
1408 }
1409
1410
1411
1412
1413 const OnCallSpec<F>* FindOnCallSpec(const ArgumentTuple& args) const {
1414 for (UntypedOnCallSpecs::const_reverse_iterator it =
1415 untyped_on_call_specs_.rbegin();
1416 it != untyped_on_call_specs_.rend(); ++it) {
1417 const OnCallSpec<F>* spec = static_cast<const OnCallSpec<F>*>(*it);
1418 if (spec->Matches(args)) return spec;
1419 }
1420
1421 return nullptr;
1422 }
1423
1424
1425
1426
1427
1428
1429
1430
1431 Result PerformDefaultAction(ArgumentTuple&& args,
1432 const std::string& call_description) const {
1433 const OnCallSpec<F>* const spec = this->FindOnCallSpec(args);
1434 if (spec != nullptr) {
1435 return spec->GetAction().Perform(std::move(args));
1436 }
1437 const std::string message =
1438 call_description +
1439 "\n The mock function has no default action "
1440 "set, and its return type has no default value set.";
1441 #if GTEST_HAS_EXCEPTIONS
1442 if (!DefaultValue<Result>::Exists()) {
1443 throw std::runtime_error(message);
1444 }
1445 #else
1446 Assert(DefaultValue<Result>::Exists(), "", -1, message);
1447 #endif
1448 return DefaultValue<Result>::Get();
1449 }
1450
1451
1452
1453 void ClearDefaultActionsLocked() override
1454 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
1455 g_gmock_mutex.AssertHeld();
1456
1457
1458
1459
1460
1461
1462
1463
1464 UntypedOnCallSpecs specs_to_delete;
1465 untyped_on_call_specs_.swap(specs_to_delete);
1466
1467 g_gmock_mutex.Unlock();
1468 for (UntypedOnCallSpecs::const_iterator it = specs_to_delete.begin();
1469 it != specs_to_delete.end(); ++it) {
1470 delete static_cast<const OnCallSpec<F>*>(*it);
1471 }
1472
1473
1474
1475 g_gmock_mutex.Lock();
1476 }
1477
1478
1479
1480
1481 Result Invoke(Args... args) GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
1482 return InvokeWith(ArgumentTuple(std::forward<Args>(args)...));
1483 }
1484
1485 MockSpec<F> With(Matcher<Args>... m) {
1486 return MockSpec<F>(this, ::std::make_tuple(std::move(m)...));
1487 }
1488
1489 protected:
1490 template <typename Function>
1491 friend class MockSpec;
1492
1493
1494 OnCallSpec<F>& AddNewOnCallSpec(const char* file, int line,
1495 const ArgumentMatcherTuple& m)
1496 GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
1497 Mock::RegisterUseByOnCallOrExpectCall(MockObject(), file, line);
1498 OnCallSpec<F>* const on_call_spec = new OnCallSpec<F>(file, line, m);
1499 untyped_on_call_specs_.push_back(on_call_spec);
1500 return *on_call_spec;
1501 }
1502
1503
1504 TypedExpectation<F>& AddNewExpectation(const char* file, int line,
1505 const std::string& source_text,
1506 const ArgumentMatcherTuple& m)
1507 GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
1508 Mock::RegisterUseByOnCallOrExpectCall(MockObject(), file, line);
1509 TypedExpectation<F>* const expectation =
1510 new TypedExpectation<F>(this, file, line, source_text, m);
1511 const std::shared_ptr<ExpectationBase> untyped_expectation(expectation);
1512
1513
1514 untyped_expectations_.push_back(untyped_expectation);
1515
1516
1517 Sequence* const implicit_sequence = g_gmock_implicit_sequence.get();
1518 if (implicit_sequence != nullptr) {
1519 implicit_sequence->AddExpectation(Expectation(untyped_expectation));
1520 }
1521
1522 return *expectation;
1523 }
1524
1525 private:
1526 template <typename Func>
1527 friend class TypedExpectation;
1528
1529
1530
1531
1532
1533
1534 void DescribeDefaultActionTo(const ArgumentTuple& args,
1535 ::std::ostream* os) const {
1536 const OnCallSpec<F>* const spec = FindOnCallSpec(args);
1537
1538 if (spec == nullptr) {
1539 *os << (std::is_void<Result>::value ? "returning directly.\n"
1540 : "returning default value.\n");
1541 } else {
1542 *os << "taking default action specified at:\n"
1543 << FormatFileLocation(spec->file(), spec->line()) << "\n";
1544 }
1545 }
1546
1547
1548
1549
1550 void UntypedDescribeUninterestingCall(const void* untyped_args,
1551 ::std::ostream* os) const override
1552 GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
1553 const ArgumentTuple& args =
1554 *static_cast<const ArgumentTuple*>(untyped_args);
1555 *os << "Uninteresting mock function call - ";
1556 DescribeDefaultActionTo(args, os);
1557 *os << " Function call: " << Name();
1558 UniversalPrint(args, os);
1559 }
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577 const ExpectationBase* UntypedFindMatchingExpectation(
1578 const void* untyped_args, const void** untyped_action, bool* is_excessive,
1579 ::std::ostream* what, ::std::ostream* why) override
1580 GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
1581 const ArgumentTuple& args =
1582 *static_cast<const ArgumentTuple*>(untyped_args);
1583 MutexLock l(&g_gmock_mutex);
1584 TypedExpectation<F>* exp = this->FindMatchingExpectationLocked(args);
1585 if (exp == nullptr) {
1586 this->FormatUnexpectedCallMessageLocked(args, what, why);
1587 return nullptr;
1588 }
1589
1590
1591
1592
1593 *is_excessive = exp->IsSaturated();
1594 const Action<F>* action = exp->GetActionForArguments(this, args, what, why);
1595 if (action != nullptr && action->IsDoDefault())
1596 action = nullptr;
1597 *untyped_action = action;
1598 return exp;
1599 }
1600
1601
1602 void UntypedPrintArgs(const void* untyped_args,
1603 ::std::ostream* os) const override {
1604 const ArgumentTuple& args =
1605 *static_cast<const ArgumentTuple*>(untyped_args);
1606 UniversalPrint(args, os);
1607 }
1608
1609
1610
1611 TypedExpectation<F>* FindMatchingExpectationLocked(const ArgumentTuple& args)
1612 const GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
1613 g_gmock_mutex.AssertHeld();
1614
1615
1616 for (typename UntypedExpectations::const_reverse_iterator it =
1617 untyped_expectations_.rbegin();
1618 it != untyped_expectations_.rend(); ++it) {
1619 TypedExpectation<F>* const exp =
1620 static_cast<TypedExpectation<F>*>(it->get());
1621 if (exp->ShouldHandleArguments(args)) {
1622 return exp;
1623 }
1624 }
1625 return nullptr;
1626 }
1627
1628
1629 void FormatUnexpectedCallMessageLocked(const ArgumentTuple& args,
1630 ::std::ostream* os,
1631 ::std::ostream* why) const
1632 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
1633 g_gmock_mutex.AssertHeld();
1634 *os << "\nUnexpected mock function call - ";
1635 DescribeDefaultActionTo(args, os);
1636 PrintTriedExpectationsLocked(args, why);
1637 }
1638
1639
1640
1641 void PrintTriedExpectationsLocked(const ArgumentTuple& args,
1642 ::std::ostream* why) const
1643 GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
1644 g_gmock_mutex.AssertHeld();
1645 const size_t count = untyped_expectations_.size();
1646 *why << "Google Mock tried the following " << count << " "
1647 << (count == 1 ? "expectation, but it didn't match"
1648 : "expectations, but none matched")
1649 << ":\n";
1650 for (size_t i = 0; i < count; i++) {
1651 TypedExpectation<F>* const expectation =
1652 static_cast<TypedExpectation<F>*>(untyped_expectations_[i].get());
1653 *why << "\n";
1654 expectation->DescribeLocationTo(why);
1655 if (count > 1) {
1656 *why << "tried expectation #" << i << ": ";
1657 }
1658 *why << expectation->source_text() << "...\n";
1659 expectation->ExplainMatchResultTo(args, why);
1660 expectation->DescribeCallCountTo(why);
1661 }
1662 }
1663
1664
1665
1666
1667 R PerformAction(const void* untyped_action, ArgumentTuple&& args,
1668 const std::string& call_description) const {
1669 if (untyped_action == nullptr) {
1670 return PerformDefaultAction(std::move(args), call_description);
1671 }
1672
1673
1674
1675 const Action<F> action = *static_cast<const Action<F>*>(untyped_action);
1676 return action.Perform(std::move(args));
1677 }
1678
1679
1680
1681 template <typename T>
1682 using can_print_result = internal::conjunction<
1683
1684 internal::negation<std::is_void<T>>,
1685
1686
1687 std::is_move_constructible<T>>;
1688
1689
1690 template <typename T = R,
1691 typename std::enable_if<can_print_result<T>::value, int>::type = 0>
1692 R PerformActionAndPrintResult(const void* const untyped_action,
1693 ArgumentTuple&& args,
1694 const std::string& call_description,
1695 std::ostream& os) {
1696 R result = PerformAction(untyped_action, std::move(args), call_description);
1697
1698 PrintAsActionResult(result, os);
1699 return std::forward<R>(result);
1700 }
1701
1702
1703
1704 template <typename T = R,
1705 typename std::enable_if<
1706 internal::negation<can_print_result<T>>::value, int>::type = 0>
1707 R PerformActionAndPrintResult(const void* const untyped_action,
1708 ArgumentTuple&& args,
1709 const std::string& call_description,
1710 std::ostream&) {
1711 return PerformAction(untyped_action, std::move(args), call_description);
1712 }
1713
1714
1715
1716
1717 R InvokeWith(ArgumentTuple&& args) GTEST_LOCK_EXCLUDED_(g_gmock_mutex);
1718 };
1719
1720
1721
1722 template <typename R, typename... Args>
1723 R FunctionMocker<R(Args...)>::InvokeWith(ArgumentTuple&& args)
1724 GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
1725
1726
1727 if (untyped_expectations_.size() == 0) {
1728
1729
1730
1731
1732
1733
1734
1735 const CallReaction reaction =
1736 Mock::GetReactionOnUninterestingCalls(MockObject());
1737
1738
1739
1740
1741 const bool need_to_report_uninteresting_call =
1742
1743
1744 reaction == kAllow ? LogIsVisible(kInfo) :
1745
1746
1747 reaction == kWarn
1748 ? LogIsVisible(kWarning)
1749 :
1750
1751
1752 true;
1753
1754 if (!need_to_report_uninteresting_call) {
1755
1756 return this->PerformDefaultAction(
1757 std::move(args), "Function call: " + std::string(Name()));
1758 }
1759
1760
1761 ::std::stringstream ss;
1762 this->UntypedDescribeUninterestingCall(&args, &ss);
1763
1764
1765
1766
1767
1768
1769 const Cleanup report_uninteresting_call(
1770 [&] { ReportUninterestingCall(reaction, ss.str()); });
1771
1772 return PerformActionAndPrintResult(nullptr, std::move(args), ss.str(), ss);
1773 }
1774
1775 bool is_excessive = false;
1776 ::std::stringstream ss;
1777 ::std::stringstream why;
1778 ::std::stringstream loc;
1779 const void* untyped_action = nullptr;
1780
1781
1782
1783
1784 const ExpectationBase* const untyped_expectation =
1785 this->UntypedFindMatchingExpectation(&args, &untyped_action,
1786 &is_excessive, &ss, &why);
1787 const bool found = untyped_expectation != nullptr;
1788
1789
1790
1791
1792
1793 const bool need_to_report_call =
1794 !found || is_excessive || LogIsVisible(kInfo);
1795 if (!need_to_report_call) {
1796
1797 return PerformAction(untyped_action, std::move(args), "");
1798 }
1799
1800 ss << " Function call: " << Name();
1801 this->UntypedPrintArgs(&args, &ss);
1802
1803
1804
1805 if (found && !is_excessive) {
1806 untyped_expectation->DescribeLocationTo(&loc);
1807 }
1808
1809
1810
1811
1812
1813
1814 const Cleanup handle_failures([&] {
1815 ss << "\n" << why.str();
1816
1817 if (!found) {
1818
1819 Expect(false, nullptr, -1, ss.str());
1820 } else if (is_excessive) {
1821
1822 Expect(false, untyped_expectation->file(), untyped_expectation->line(),
1823 ss.str());
1824 } else {
1825
1826
1827 Log(kInfo, loc.str() + ss.str(), 2);
1828 }
1829 });
1830
1831 return PerformActionAndPrintResult(untyped_action, std::move(args), ss.str(),
1832 ss);
1833 }
1834
1835 }
1836
1837 namespace internal {
1838
1839 template <typename F>
1840 class MockFunction;
1841
1842 template <typename R, typename... Args>
1843 class MockFunction<R(Args...)> {
1844 public:
1845 MockFunction(const MockFunction&) = delete;
1846 MockFunction& operator=(const MockFunction&) = delete;
1847
1848 std::function<R(Args...)> AsStdFunction() {
1849 return [this](Args... args) -> R {
1850 return this->Call(std::forward<Args>(args)...);
1851 };
1852 }
1853
1854
1855 R Call(Args... args) {
1856 mock_.SetOwnerAndName(this, "Call");
1857 return mock_.Invoke(std::forward<Args>(args)...);
1858 }
1859
1860 MockSpec<R(Args...)> gmock_Call(Matcher<Args>... m) {
1861 mock_.RegisterOwner(this);
1862 return mock_.With(std::move(m)...);
1863 }
1864
1865 MockSpec<R(Args...)> gmock_Call(const WithoutMatchers&, R (*)(Args...)) {
1866 return this->gmock_Call(::testing::A<Args>()...);
1867 }
1868
1869 protected:
1870 MockFunction() = default;
1871 ~MockFunction() = default;
1872
1873 private:
1874 FunctionMocker<R(Args...)> mock_;
1875 };
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889 template <typename F, typename = void>
1890 struct SignatureOf;
1891
1892 template <typename R, typename... Args>
1893 struct SignatureOf<R(Args...)> {
1894 using type = R(Args...);
1895 };
1896
1897 template <template <typename> class C, typename F>
1898 struct SignatureOf<C<F>,
1899 typename std::enable_if<std::is_function<F>::value>::type>
1900 : SignatureOf<F> {};
1901
1902 template <typename F>
1903 using SignatureOfT = typename SignatureOf<F>::type;
1904
1905 }
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967 template <typename F>
1968 class MockFunction : public internal::MockFunction<internal::SignatureOfT<F>> {
1969 using Base = internal::MockFunction<internal::SignatureOfT<F>>;
1970
1971 public:
1972 using Base::Base;
1973 };
1974
1975
1976
1977
1978
1979
1980 using internal::MockSpec;
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997 template <typename T>
1998 inline const T& Const(const T& x) {
1999 return x;
2000 }
2001
2002
2003 inline Expectation::Expectation(internal::ExpectationBase& exp)
2004 : expectation_base_(exp.GetHandle().expectation_base()) {}
2005
2006 }
2007
2008 GTEST_DISABLE_MSC_WARNINGS_POP_()
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072 #define GMOCK_ON_CALL_IMPL_(mock_expr, Setter, call) \
2073 ((mock_expr).gmock_##call)(::testing::internal::GetWithoutMatchers(), \
2074 nullptr) \
2075 .Setter(__FILE__, __LINE__, #mock_expr, #call)
2076
2077 #define ON_CALL(obj, call) \
2078 GMOCK_ON_CALL_IMPL_(obj, InternalDefaultActionSetAt, call)
2079
2080 #define EXPECT_CALL(obj, call) \
2081 GMOCK_ON_CALL_IMPL_(obj, InternalExpectedAt, call)
2082
2083 #endif