File indexing completed on 2025-02-21 10:03:07
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
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255 #ifndef GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_
0256 #define GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_
0257
0258 #include <algorithm>
0259 #include <cmath>
0260 #include <initializer_list>
0261 #include <iterator>
0262 #include <limits>
0263 #include <memory>
0264 #include <ostream> // NOLINT
0265 #include <sstream>
0266 #include <string>
0267 #include <type_traits>
0268 #include <utility>
0269 #include <vector>
0270
0271 #include "gmock/internal/gmock-internal-utils.h"
0272 #include "gmock/internal/gmock-port.h"
0273 #include "gmock/internal/gmock-pp.h"
0274 #include "gtest/gtest.h"
0275
0276
0277 #if defined(_MSC_VER) && _MSC_VER >= 1915
0278 #define GMOCK_MAYBE_5046_ 5046
0279 #else
0280 #define GMOCK_MAYBE_5046_
0281 #endif
0282
0283 GTEST_DISABLE_MSC_WARNINGS_PUSH_(
0284 4251 GMOCK_MAYBE_5046_
0285
0286 )
0287
0288 namespace testing {
0289
0290
0291
0292
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303 class StringMatchResultListener : public MatchResultListener {
0304 public:
0305 StringMatchResultListener() : MatchResultListener(&ss_) {}
0306
0307
0308 std::string str() const { return ss_.str(); }
0309
0310
0311 void Clear() { ss_.str(""); }
0312
0313 private:
0314 ::std::stringstream ss_;
0315
0316 StringMatchResultListener(const StringMatchResultListener&) = delete;
0317 StringMatchResultListener& operator=(const StringMatchResultListener&) =
0318 delete;
0319 };
0320
0321
0322
0323 namespace internal {
0324
0325
0326
0327
0328
0329
0330
0331
0332
0333
0334
0335 template <typename T, typename M>
0336 class MatcherCastImpl {
0337 public:
0338 static Matcher<T> Cast(const M& polymorphic_matcher_or_value) {
0339
0340
0341
0342
0343
0344
0345
0346
0347
0348
0349
0350
0351
0352 return CastImpl(polymorphic_matcher_or_value,
0353 std::is_convertible<M, Matcher<T>>{},
0354 std::is_convertible<M, T>{});
0355 }
0356
0357 private:
0358 template <bool Ignore>
0359 static Matcher<T> CastImpl(const M& polymorphic_matcher_or_value,
0360 std::true_type ,
0361 std::integral_constant<bool, Ignore>) {
0362
0363
0364
0365
0366
0367
0368
0369
0370 return polymorphic_matcher_or_value;
0371 }
0372
0373
0374
0375
0376 static Matcher<T> CastImpl(const M& value,
0377 std::false_type ,
0378 std::true_type ) {
0379 return Matcher<T>(ImplicitCast_<T>(value));
0380 }
0381
0382
0383
0384
0385
0386
0387
0388
0389
0390
0391
0392 static Matcher<T> CastImpl(const M& value,
0393 std::false_type ,
0394 std::false_type );
0395 };
0396
0397
0398
0399
0400 template <typename T, typename U>
0401 class MatcherCastImpl<T, Matcher<U>> {
0402 public:
0403 static Matcher<T> Cast(const Matcher<U>& source_matcher) {
0404 return Matcher<T>(new Impl(source_matcher));
0405 }
0406
0407 private:
0408 class Impl : public MatcherInterface<T> {
0409 public:
0410 explicit Impl(const Matcher<U>& source_matcher)
0411 : source_matcher_(source_matcher) {}
0412
0413
0414 bool MatchAndExplain(T x, MatchResultListener* listener) const override {
0415 using FromType = typename std::remove_cv<typename std::remove_pointer<
0416 typename std::remove_reference<T>::type>::type>::type;
0417 using ToType = typename std::remove_cv<typename std::remove_pointer<
0418 typename std::remove_reference<U>::type>::type>::type;
0419
0420 static_assert(
0421
0422
0423 (std::is_pointer<typename std::remove_reference<T>::type>::value !=
0424 std::is_pointer<typename std::remove_reference<U>::type>::value) ||
0425 std::is_same<FromType, ToType>::value ||
0426 !std::is_base_of<FromType, ToType>::value,
0427 "Can't implicitly convert from <base> to <derived>");
0428
0429
0430
0431 using CastType =
0432 typename std::conditional<std::is_convertible<T&, const U&>::value,
0433 T&, U>::type;
0434
0435 return source_matcher_.MatchAndExplain(static_cast<CastType>(x),
0436 listener);
0437 }
0438
0439 void DescribeTo(::std::ostream* os) const override {
0440 source_matcher_.DescribeTo(os);
0441 }
0442
0443 void DescribeNegationTo(::std::ostream* os) const override {
0444 source_matcher_.DescribeNegationTo(os);
0445 }
0446
0447 private:
0448 const Matcher<U> source_matcher_;
0449 };
0450 };
0451
0452
0453
0454 template <typename T>
0455 class MatcherCastImpl<T, Matcher<T>> {
0456 public:
0457 static Matcher<T> Cast(const Matcher<T>& matcher) { return matcher; }
0458 };
0459
0460
0461 template <typename Derived>
0462 class MatcherBaseImpl {
0463 public:
0464 MatcherBaseImpl() = default;
0465
0466 template <typename T>
0467 operator ::testing::Matcher<T>() const {
0468 return ::testing::Matcher<T>(new
0469 typename Derived::template gmock_Impl<T>());
0470 }
0471 };
0472
0473
0474 template <template <typename...> class Derived, typename... Ts>
0475 class MatcherBaseImpl<Derived<Ts...>> {
0476 public:
0477
0478
0479 template <typename E = std::enable_if<sizeof...(Ts) == 1>,
0480 typename E::type* = nullptr>
0481 explicit MatcherBaseImpl(Ts... params)
0482 : params_(std::forward<Ts>(params)...) {}
0483 template <typename E = std::enable_if<sizeof...(Ts) != 1>,
0484 typename = typename E::type>
0485 MatcherBaseImpl(Ts... params)
0486 : params_(std::forward<Ts>(params)...) {}
0487
0488 template <typename F>
0489 operator ::testing::Matcher<F>() const {
0490 return Apply<F>(MakeIndexSequence<sizeof...(Ts)>{});
0491 }
0492
0493 private:
0494 template <typename F, std::size_t... tuple_ids>
0495 ::testing::Matcher<F> Apply(IndexSequence<tuple_ids...>) const {
0496 return ::testing::Matcher<F>(
0497 new typename Derived<Ts...>::template gmock_Impl<F>(
0498 std::get<tuple_ids>(params_)...));
0499 }
0500
0501 const std::tuple<Ts...> params_;
0502 };
0503
0504 }
0505
0506
0507
0508
0509
0510 template <typename T, typename M>
0511 inline Matcher<T> MatcherCast(const M& matcher) {
0512 return internal::MatcherCastImpl<T, M>::Cast(matcher);
0513 }
0514
0515
0516
0517 template <typename T, typename M>
0518 inline Matcher<T> SafeMatcherCast(const M& polymorphic_matcher_or_value) {
0519 return MatcherCast<T>(polymorphic_matcher_or_value);
0520 }
0521
0522
0523
0524
0525
0526
0527
0528
0529
0530
0531 template <typename T, typename U>
0532 inline Matcher<T> SafeMatcherCast(const Matcher<U>& matcher) {
0533
0534 static_assert(std::is_convertible<const T&, const U&>::value,
0535 "T must be implicitly convertible to U");
0536
0537
0538 static_assert(std::is_reference<T>::value || !std::is_reference<U>::value,
0539 "cannot convert non reference arg to reference");
0540
0541
0542 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(T) RawT;
0543 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(U) RawU;
0544 constexpr bool kTIsOther = GMOCK_KIND_OF_(RawT) == internal::kOther;
0545 constexpr bool kUIsOther = GMOCK_KIND_OF_(RawU) == internal::kOther;
0546 static_assert(
0547 kTIsOther || kUIsOther ||
0548 (internal::LosslessArithmeticConvertible<RawT, RawU>::value),
0549 "conversion of arithmetic types must be lossless");
0550 return MatcherCast<T>(matcher);
0551 }
0552
0553
0554 template <typename T>
0555 Matcher<T> A();
0556
0557
0558
0559 namespace internal {
0560
0561
0562 inline void PrintIfNotEmpty(const std::string& explanation,
0563 ::std::ostream* os) {
0564 if (explanation != "" && os != nullptr) {
0565 *os << ", " << explanation;
0566 }
0567 }
0568
0569
0570
0571
0572 inline bool IsReadableTypeName(const std::string& type_name) {
0573
0574
0575 return (type_name.length() <= 20 ||
0576 type_name.find_first_of("<(") == std::string::npos);
0577 }
0578
0579
0580
0581
0582
0583
0584 template <typename Value, typename T>
0585 bool MatchPrintAndExplain(Value& value, const Matcher<T>& matcher,
0586 MatchResultListener* listener) {
0587 if (!listener->IsInterested()) {
0588
0589
0590 return matcher.Matches(value);
0591 }
0592
0593 StringMatchResultListener inner_listener;
0594 const bool match = matcher.MatchAndExplain(value, &inner_listener);
0595
0596 UniversalPrint(value, listener->stream());
0597 #if GTEST_HAS_RTTI
0598 const std::string& type_name = GetTypeName<Value>();
0599 if (IsReadableTypeName(type_name))
0600 *listener->stream() << " (of type " << type_name << ")";
0601 #endif
0602 PrintIfNotEmpty(inner_listener.str(), listener->stream());
0603
0604 return match;
0605 }
0606
0607
0608
0609 template <size_t N>
0610 class TuplePrefix {
0611 public:
0612
0613
0614
0615 template <typename MatcherTuple, typename ValueTuple>
0616 static bool Matches(const MatcherTuple& matcher_tuple,
0617 const ValueTuple& value_tuple) {
0618 return TuplePrefix<N - 1>::Matches(matcher_tuple, value_tuple) &&
0619 std::get<N - 1>(matcher_tuple).Matches(std::get<N - 1>(value_tuple));
0620 }
0621
0622
0623
0624
0625
0626 template <typename MatcherTuple, typename ValueTuple>
0627 static void ExplainMatchFailuresTo(const MatcherTuple& matchers,
0628 const ValueTuple& values,
0629 ::std::ostream* os) {
0630
0631 TuplePrefix<N - 1>::ExplainMatchFailuresTo(matchers, values, os);
0632
0633
0634
0635 typename std::tuple_element<N - 1, MatcherTuple>::type matcher =
0636 std::get<N - 1>(matchers);
0637 typedef typename std::tuple_element<N - 1, ValueTuple>::type Value;
0638 const Value& value = std::get<N - 1>(values);
0639 StringMatchResultListener listener;
0640 if (!matcher.MatchAndExplain(value, &listener)) {
0641 *os << " Expected arg #" << N - 1 << ": ";
0642 std::get<N - 1>(matchers).DescribeTo(os);
0643 *os << "\n Actual: ";
0644
0645
0646
0647
0648
0649 internal::UniversalPrint(value, os);
0650 PrintIfNotEmpty(listener.str(), os);
0651 *os << "\n";
0652 }
0653 }
0654 };
0655
0656
0657 template <>
0658 class TuplePrefix<0> {
0659 public:
0660 template <typename MatcherTuple, typename ValueTuple>
0661 static bool Matches(const MatcherTuple& ,
0662 const ValueTuple& ) {
0663 return true;
0664 }
0665
0666 template <typename MatcherTuple, typename ValueTuple>
0667 static void ExplainMatchFailuresTo(const MatcherTuple& ,
0668 const ValueTuple& ,
0669 ::std::ostream* ) {}
0670 };
0671
0672
0673
0674
0675
0676
0677 template <typename MatcherTuple, typename ValueTuple>
0678 bool TupleMatches(const MatcherTuple& matcher_tuple,
0679 const ValueTuple& value_tuple) {
0680
0681
0682 static_assert(std::tuple_size<MatcherTuple>::value ==
0683 std::tuple_size<ValueTuple>::value,
0684 "matcher and value have different numbers of fields");
0685 return TuplePrefix<std::tuple_size<ValueTuple>::value>::Matches(matcher_tuple,
0686 value_tuple);
0687 }
0688
0689
0690
0691 template <typename MatcherTuple, typename ValueTuple>
0692 void ExplainMatchFailureTupleTo(const MatcherTuple& matchers,
0693 const ValueTuple& values, ::std::ostream* os) {
0694 TuplePrefix<std::tuple_size<MatcherTuple>::value>::ExplainMatchFailuresTo(
0695 matchers, values, os);
0696 }
0697
0698
0699
0700
0701
0702 template <typename Tuple, typename Func, typename OutIter>
0703 class TransformTupleValuesHelper {
0704 private:
0705 typedef ::std::tuple_size<Tuple> TupleSize;
0706
0707 public:
0708
0709
0710 static OutIter Run(Func f, const Tuple& t, OutIter out) {
0711 return IterateOverTuple<Tuple, TupleSize::value>()(f, t, out);
0712 }
0713
0714 private:
0715 template <typename Tup, size_t kRemainingSize>
0716 struct IterateOverTuple {
0717 OutIter operator()(Func f, const Tup& t, OutIter out) const {
0718 *out++ = f(::std::get<TupleSize::value - kRemainingSize>(t));
0719 return IterateOverTuple<Tup, kRemainingSize - 1>()(f, t, out);
0720 }
0721 };
0722 template <typename Tup>
0723 struct IterateOverTuple<Tup, 0> {
0724 OutIter operator()(Func , const Tup& , OutIter out) const {
0725 return out;
0726 }
0727 };
0728 };
0729
0730
0731
0732
0733 template <typename Tuple, typename Func, typename OutIter>
0734 OutIter TransformTupleValues(Func f, const Tuple& t, OutIter out) {
0735 return TransformTupleValuesHelper<Tuple, Func, OutIter>::Run(f, t, out);
0736 }
0737
0738
0739
0740
0741
0742 class AnythingMatcher {
0743 public:
0744 using is_gtest_matcher = void;
0745
0746 template <typename T>
0747 bool MatchAndExplain(const T& , std::ostream* ) const {
0748 return true;
0749 }
0750 void DescribeTo(std::ostream* os) const { *os << "is anything"; }
0751 void DescribeNegationTo(::std::ostream* os) const {
0752
0753
0754
0755 *os << "never matches";
0756 }
0757 };
0758
0759
0760
0761 class IsNullMatcher {
0762 public:
0763 template <typename Pointer>
0764 bool MatchAndExplain(const Pointer& p,
0765 MatchResultListener* ) const {
0766 return p == nullptr;
0767 }
0768
0769 void DescribeTo(::std::ostream* os) const { *os << "is NULL"; }
0770 void DescribeNegationTo(::std::ostream* os) const { *os << "isn't NULL"; }
0771 };
0772
0773
0774
0775 class NotNullMatcher {
0776 public:
0777 template <typename Pointer>
0778 bool MatchAndExplain(const Pointer& p,
0779 MatchResultListener* ) const {
0780 return p != nullptr;
0781 }
0782
0783 void DescribeTo(::std::ostream* os) const { *os << "isn't NULL"; }
0784 void DescribeNegationTo(::std::ostream* os) const { *os << "is NULL"; }
0785 };
0786
0787
0788
0789
0790
0791
0792
0793
0794
0795
0796
0797
0798
0799
0800 template <typename T>
0801 class RefMatcher;
0802
0803 template <typename T>
0804 class RefMatcher<T&> {
0805
0806
0807
0808
0809
0810 public:
0811
0812
0813
0814 explicit RefMatcher(T& x) : object_(x) {}
0815
0816 template <typename Super>
0817 operator Matcher<Super&>() const {
0818
0819
0820
0821
0822
0823 return MakeMatcher(new Impl<Super>(object_));
0824 }
0825
0826 private:
0827 template <typename Super>
0828 class Impl : public MatcherInterface<Super&> {
0829 public:
0830 explicit Impl(Super& x) : object_(x) {}
0831
0832
0833
0834 bool MatchAndExplain(Super& x,
0835 MatchResultListener* listener) const override {
0836 *listener << "which is located @" << static_cast<const void*>(&x);
0837 return &x == &object_;
0838 }
0839
0840 void DescribeTo(::std::ostream* os) const override {
0841 *os << "references the variable ";
0842 UniversalPrinter<Super&>::Print(object_, os);
0843 }
0844
0845 void DescribeNegationTo(::std::ostream* os) const override {
0846 *os << "does not reference the variable ";
0847 UniversalPrinter<Super&>::Print(object_, os);
0848 }
0849
0850 private:
0851 const Super& object_;
0852 };
0853
0854 T& object_;
0855 };
0856
0857
0858 inline bool CaseInsensitiveCStringEquals(const char* lhs, const char* rhs) {
0859 return String::CaseInsensitiveCStringEquals(lhs, rhs);
0860 }
0861
0862 inline bool CaseInsensitiveCStringEquals(const wchar_t* lhs,
0863 const wchar_t* rhs) {
0864 return String::CaseInsensitiveWideCStringEquals(lhs, rhs);
0865 }
0866
0867
0868
0869 template <typename StringType>
0870 bool CaseInsensitiveStringEquals(const StringType& s1, const StringType& s2) {
0871
0872 if (!CaseInsensitiveCStringEquals(s1.c_str(), s2.c_str())) {
0873 return false;
0874 }
0875
0876
0877 const typename StringType::value_type nul = 0;
0878 const size_t i1 = s1.find(nul), i2 = s2.find(nul);
0879
0880
0881 if (i1 == StringType::npos || i2 == StringType::npos) {
0882 return i1 == i2;
0883 }
0884
0885
0886 return CaseInsensitiveStringEquals(s1.substr(i1 + 1), s2.substr(i2 + 1));
0887 }
0888
0889
0890
0891
0892 template <typename StringType>
0893 class StrEqualityMatcher {
0894 public:
0895 StrEqualityMatcher(StringType str, bool expect_eq, bool case_sensitive)
0896 : string_(std::move(str)),
0897 expect_eq_(expect_eq),
0898 case_sensitive_(case_sensitive) {}
0899
0900 #if GTEST_INTERNAL_HAS_STRING_VIEW
0901 bool MatchAndExplain(const internal::StringView& s,
0902 MatchResultListener* listener) const {
0903
0904
0905 const StringType& str = std::string(s);
0906 return MatchAndExplain(str, listener);
0907 }
0908 #endif
0909
0910
0911
0912
0913
0914
0915 template <typename CharType>
0916 bool MatchAndExplain(CharType* s, MatchResultListener* listener) const {
0917 if (s == nullptr) {
0918 return !expect_eq_;
0919 }
0920 return MatchAndExplain(StringType(s), listener);
0921 }
0922
0923
0924
0925
0926
0927 template <typename MatcheeStringType>
0928 bool MatchAndExplain(const MatcheeStringType& s,
0929 MatchResultListener* ) const {
0930 const StringType s2(s);
0931 const bool eq = case_sensitive_ ? s2 == string_
0932 : CaseInsensitiveStringEquals(s2, string_);
0933 return expect_eq_ == eq;
0934 }
0935
0936 void DescribeTo(::std::ostream* os) const {
0937 DescribeToHelper(expect_eq_, os);
0938 }
0939
0940 void DescribeNegationTo(::std::ostream* os) const {
0941 DescribeToHelper(!expect_eq_, os);
0942 }
0943
0944 private:
0945 void DescribeToHelper(bool expect_eq, ::std::ostream* os) const {
0946 *os << (expect_eq ? "is " : "isn't ");
0947 *os << "equal to ";
0948 if (!case_sensitive_) {
0949 *os << "(ignoring case) ";
0950 }
0951 UniversalPrint(string_, os);
0952 }
0953
0954 const StringType string_;
0955 const bool expect_eq_;
0956 const bool case_sensitive_;
0957 };
0958
0959
0960
0961
0962 template <typename StringType>
0963 class HasSubstrMatcher {
0964 public:
0965 explicit HasSubstrMatcher(const StringType& substring)
0966 : substring_(substring) {}
0967
0968 #if GTEST_INTERNAL_HAS_STRING_VIEW
0969 bool MatchAndExplain(const internal::StringView& s,
0970 MatchResultListener* listener) const {
0971
0972
0973 const StringType& str = std::string(s);
0974 return MatchAndExplain(str, listener);
0975 }
0976 #endif
0977
0978
0979
0980
0981
0982
0983 template <typename CharType>
0984 bool MatchAndExplain(CharType* s, MatchResultListener* listener) const {
0985 return s != nullptr && MatchAndExplain(StringType(s), listener);
0986 }
0987
0988
0989
0990
0991
0992 template <typename MatcheeStringType>
0993 bool MatchAndExplain(const MatcheeStringType& s,
0994 MatchResultListener* ) const {
0995 return StringType(s).find(substring_) != StringType::npos;
0996 }
0997
0998
0999 void DescribeTo(::std::ostream* os) const {
1000 *os << "has substring ";
1001 UniversalPrint(substring_, os);
1002 }
1003
1004 void DescribeNegationTo(::std::ostream* os) const {
1005 *os << "has no substring ";
1006 UniversalPrint(substring_, os);
1007 }
1008
1009 private:
1010 const StringType substring_;
1011 };
1012
1013
1014
1015
1016 template <typename StringType>
1017 class StartsWithMatcher {
1018 public:
1019 explicit StartsWithMatcher(const StringType& prefix) : prefix_(prefix) {}
1020
1021 #if GTEST_INTERNAL_HAS_STRING_VIEW
1022 bool MatchAndExplain(const internal::StringView& s,
1023 MatchResultListener* listener) const {
1024
1025
1026 const StringType& str = std::string(s);
1027 return MatchAndExplain(str, listener);
1028 }
1029 #endif
1030
1031
1032
1033
1034
1035
1036 template <typename CharType>
1037 bool MatchAndExplain(CharType* s, MatchResultListener* listener) const {
1038 return s != nullptr && MatchAndExplain(StringType(s), listener);
1039 }
1040
1041
1042
1043
1044
1045 template <typename MatcheeStringType>
1046 bool MatchAndExplain(const MatcheeStringType& s,
1047 MatchResultListener* ) const {
1048 const StringType& s2(s);
1049 return s2.length() >= prefix_.length() &&
1050 s2.substr(0, prefix_.length()) == prefix_;
1051 }
1052
1053 void DescribeTo(::std::ostream* os) const {
1054 *os << "starts with ";
1055 UniversalPrint(prefix_, os);
1056 }
1057
1058 void DescribeNegationTo(::std::ostream* os) const {
1059 *os << "doesn't start with ";
1060 UniversalPrint(prefix_, os);
1061 }
1062
1063 private:
1064 const StringType prefix_;
1065 };
1066
1067
1068
1069
1070 template <typename StringType>
1071 class EndsWithMatcher {
1072 public:
1073 explicit EndsWithMatcher(const StringType& suffix) : suffix_(suffix) {}
1074
1075 #if GTEST_INTERNAL_HAS_STRING_VIEW
1076 bool MatchAndExplain(const internal::StringView& s,
1077 MatchResultListener* listener) const {
1078
1079
1080 const StringType& str = std::string(s);
1081 return MatchAndExplain(str, listener);
1082 }
1083 #endif
1084
1085
1086
1087
1088
1089
1090 template <typename CharType>
1091 bool MatchAndExplain(CharType* s, MatchResultListener* listener) const {
1092 return s != nullptr && MatchAndExplain(StringType(s), listener);
1093 }
1094
1095
1096
1097
1098
1099 template <typename MatcheeStringType>
1100 bool MatchAndExplain(const MatcheeStringType& s,
1101 MatchResultListener* ) const {
1102 const StringType& s2(s);
1103 return s2.length() >= suffix_.length() &&
1104 s2.substr(s2.length() - suffix_.length()) == suffix_;
1105 }
1106
1107 void DescribeTo(::std::ostream* os) const {
1108 *os << "ends with ";
1109 UniversalPrint(suffix_, os);
1110 }
1111
1112 void DescribeNegationTo(::std::ostream* os) const {
1113 *os << "doesn't end with ";
1114 UniversalPrint(suffix_, os);
1115 }
1116
1117 private:
1118 const StringType suffix_;
1119 };
1120
1121
1122
1123 class WhenBase64UnescapedMatcher {
1124 public:
1125 using is_gtest_matcher = void;
1126
1127 explicit WhenBase64UnescapedMatcher(
1128 const Matcher<const std::string&>& internal_matcher)
1129 : internal_matcher_(internal_matcher) {}
1130
1131
1132 template <typename MatcheeStringType>
1133 bool MatchAndExplain(const MatcheeStringType& s,
1134 MatchResultListener* listener) const {
1135 const std::string s2(s);
1136 std::string unescaped;
1137 if (!internal::Base64Unescape(s2, &unescaped)) {
1138 if (listener != nullptr) {
1139 *listener << "is not a valid base64 escaped string";
1140 }
1141 return false;
1142 }
1143 return MatchPrintAndExplain(unescaped, internal_matcher_, listener);
1144 }
1145
1146 void DescribeTo(::std::ostream* os) const {
1147 *os << "matches after Base64Unescape ";
1148 internal_matcher_.DescribeTo(os);
1149 }
1150
1151 void DescribeNegationTo(::std::ostream* os) const {
1152 *os << "does not match after Base64Unescape ";
1153 internal_matcher_.DescribeTo(os);
1154 }
1155
1156 private:
1157 const Matcher<const std::string&> internal_matcher_;
1158 };
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168 template <typename D, typename Op>
1169 class PairMatchBase {
1170 public:
1171 template <typename T1, typename T2>
1172 operator Matcher<::std::tuple<T1, T2>>() const {
1173 return Matcher<::std::tuple<T1, T2>>(new Impl<const ::std::tuple<T1, T2>&>);
1174 }
1175 template <typename T1, typename T2>
1176 operator Matcher<const ::std::tuple<T1, T2>&>() const {
1177 return MakeMatcher(new Impl<const ::std::tuple<T1, T2>&>);
1178 }
1179
1180 private:
1181 static ::std::ostream& GetDesc(::std::ostream& os) {
1182 return os << D::Desc();
1183 }
1184
1185 template <typename Tuple>
1186 class Impl : public MatcherInterface<Tuple> {
1187 public:
1188 bool MatchAndExplain(Tuple args,
1189 MatchResultListener* ) const override {
1190 return Op()(::std::get<0>(args), ::std::get<1>(args));
1191 }
1192 void DescribeTo(::std::ostream* os) const override {
1193 *os << "are " << GetDesc;
1194 }
1195 void DescribeNegationTo(::std::ostream* os) const override {
1196 *os << "aren't " << GetDesc;
1197 }
1198 };
1199 };
1200
1201 class Eq2Matcher : public PairMatchBase<Eq2Matcher, AnyEq> {
1202 public:
1203 static const char* Desc() { return "an equal pair"; }
1204 };
1205 class Ne2Matcher : public PairMatchBase<Ne2Matcher, AnyNe> {
1206 public:
1207 static const char* Desc() { return "an unequal pair"; }
1208 };
1209 class Lt2Matcher : public PairMatchBase<Lt2Matcher, AnyLt> {
1210 public:
1211 static const char* Desc() { return "a pair where the first < the second"; }
1212 };
1213 class Gt2Matcher : public PairMatchBase<Gt2Matcher, AnyGt> {
1214 public:
1215 static const char* Desc() { return "a pair where the first > the second"; }
1216 };
1217 class Le2Matcher : public PairMatchBase<Le2Matcher, AnyLe> {
1218 public:
1219 static const char* Desc() { return "a pair where the first <= the second"; }
1220 };
1221 class Ge2Matcher : public PairMatchBase<Ge2Matcher, AnyGe> {
1222 public:
1223 static const char* Desc() { return "a pair where the first >= the second"; }
1224 };
1225
1226
1227
1228
1229
1230 template <typename T>
1231 class NotMatcherImpl : public MatcherInterface<const T&> {
1232 public:
1233 explicit NotMatcherImpl(const Matcher<T>& matcher) : matcher_(matcher) {}
1234
1235 bool MatchAndExplain(const T& x,
1236 MatchResultListener* listener) const override {
1237 return !matcher_.MatchAndExplain(x, listener);
1238 }
1239
1240 void DescribeTo(::std::ostream* os) const override {
1241 matcher_.DescribeNegationTo(os);
1242 }
1243
1244 void DescribeNegationTo(::std::ostream* os) const override {
1245 matcher_.DescribeTo(os);
1246 }
1247
1248 private:
1249 const Matcher<T> matcher_;
1250 };
1251
1252
1253
1254 template <typename InnerMatcher>
1255 class NotMatcher {
1256 public:
1257 explicit NotMatcher(InnerMatcher matcher) : matcher_(matcher) {}
1258
1259
1260
1261 template <typename T>
1262 operator Matcher<T>() const {
1263 return Matcher<T>(new NotMatcherImpl<T>(SafeMatcherCast<T>(matcher_)));
1264 }
1265
1266 private:
1267 InnerMatcher matcher_;
1268 };
1269
1270
1271
1272
1273
1274 template <typename T>
1275 class AllOfMatcherImpl : public MatcherInterface<const T&> {
1276 public:
1277 explicit AllOfMatcherImpl(std::vector<Matcher<T>> matchers)
1278 : matchers_(std::move(matchers)) {}
1279
1280 void DescribeTo(::std::ostream* os) const override {
1281 *os << "(";
1282 for (size_t i = 0; i < matchers_.size(); ++i) {
1283 if (i != 0) *os << ") and (";
1284 matchers_[i].DescribeTo(os);
1285 }
1286 *os << ")";
1287 }
1288
1289 void DescribeNegationTo(::std::ostream* os) const override {
1290 *os << "(";
1291 for (size_t i = 0; i < matchers_.size(); ++i) {
1292 if (i != 0) *os << ") or (";
1293 matchers_[i].DescribeNegationTo(os);
1294 }
1295 *os << ")";
1296 }
1297
1298 bool MatchAndExplain(const T& x,
1299 MatchResultListener* listener) const override {
1300
1301
1302 std::string all_match_result;
1303
1304 for (size_t i = 0; i < matchers_.size(); ++i) {
1305 StringMatchResultListener slistener;
1306 if (matchers_[i].MatchAndExplain(x, &slistener)) {
1307 if (all_match_result.empty()) {
1308 all_match_result = slistener.str();
1309 } else {
1310 std::string result = slistener.str();
1311 if (!result.empty()) {
1312 all_match_result += ", and ";
1313 all_match_result += result;
1314 }
1315 }
1316 } else {
1317 *listener << slistener.str();
1318 return false;
1319 }
1320 }
1321
1322
1323 *listener << all_match_result;
1324 return true;
1325 }
1326
1327 private:
1328 const std::vector<Matcher<T>> matchers_;
1329 };
1330
1331
1332
1333
1334
1335 template <template <typename T> class CombiningMatcher, typename... Args>
1336 class VariadicMatcher {
1337 public:
1338 VariadicMatcher(const Args&... matchers)
1339 : matchers_(matchers...) {
1340 static_assert(sizeof...(Args) > 0, "Must have at least one matcher.");
1341 }
1342
1343 VariadicMatcher(const VariadicMatcher&) = default;
1344 VariadicMatcher& operator=(const VariadicMatcher&) = delete;
1345
1346
1347
1348
1349 template <typename T>
1350 operator Matcher<T>() const {
1351 std::vector<Matcher<T>> values;
1352 CreateVariadicMatcher<T>(&values, std::integral_constant<size_t, 0>());
1353 return Matcher<T>(new CombiningMatcher<T>(std::move(values)));
1354 }
1355
1356 private:
1357 template <typename T, size_t I>
1358 void CreateVariadicMatcher(std::vector<Matcher<T>>* values,
1359 std::integral_constant<size_t, I>) const {
1360 values->push_back(SafeMatcherCast<T>(std::get<I>(matchers_)));
1361 CreateVariadicMatcher<T>(values, std::integral_constant<size_t, I + 1>());
1362 }
1363
1364 template <typename T>
1365 void CreateVariadicMatcher(
1366 std::vector<Matcher<T>>*,
1367 std::integral_constant<size_t, sizeof...(Args)>) const {}
1368
1369 std::tuple<Args...> matchers_;
1370 };
1371
1372 template <typename... Args>
1373 using AllOfMatcher = VariadicMatcher<AllOfMatcherImpl, Args...>;
1374
1375
1376
1377
1378
1379 template <typename T>
1380 class AnyOfMatcherImpl : public MatcherInterface<const T&> {
1381 public:
1382 explicit AnyOfMatcherImpl(std::vector<Matcher<T>> matchers)
1383 : matchers_(std::move(matchers)) {}
1384
1385 void DescribeTo(::std::ostream* os) const override {
1386 *os << "(";
1387 for (size_t i = 0; i < matchers_.size(); ++i) {
1388 if (i != 0) *os << ") or (";
1389 matchers_[i].DescribeTo(os);
1390 }
1391 *os << ")";
1392 }
1393
1394 void DescribeNegationTo(::std::ostream* os) const override {
1395 *os << "(";
1396 for (size_t i = 0; i < matchers_.size(); ++i) {
1397 if (i != 0) *os << ") and (";
1398 matchers_[i].DescribeNegationTo(os);
1399 }
1400 *os << ")";
1401 }
1402
1403 bool MatchAndExplain(const T& x,
1404 MatchResultListener* listener) const override {
1405 std::string no_match_result;
1406
1407
1408
1409 for (size_t i = 0; i < matchers_.size(); ++i) {
1410 StringMatchResultListener slistener;
1411 if (matchers_[i].MatchAndExplain(x, &slistener)) {
1412 *listener << slistener.str();
1413 return true;
1414 } else {
1415 if (no_match_result.empty()) {
1416 no_match_result = slistener.str();
1417 } else {
1418 std::string result = slistener.str();
1419 if (!result.empty()) {
1420 no_match_result += ", and ";
1421 no_match_result += result;
1422 }
1423 }
1424 }
1425 }
1426
1427
1428 *listener << no_match_result;
1429 return false;
1430 }
1431
1432 private:
1433 const std::vector<Matcher<T>> matchers_;
1434 };
1435
1436
1437 template <typename... Args>
1438 using AnyOfMatcher = VariadicMatcher<AnyOfMatcherImpl, Args...>;
1439
1440
1441 template <typename MatcherTrue, typename MatcherFalse>
1442 class ConditionalMatcher {
1443 public:
1444 ConditionalMatcher(bool condition, MatcherTrue matcher_true,
1445 MatcherFalse matcher_false)
1446 : condition_(condition),
1447 matcher_true_(std::move(matcher_true)),
1448 matcher_false_(std::move(matcher_false)) {}
1449
1450 template <typename T>
1451 operator Matcher<T>() const {
1452 return condition_ ? SafeMatcherCast<T>(matcher_true_)
1453 : SafeMatcherCast<T>(matcher_false_);
1454 }
1455
1456 private:
1457 bool condition_;
1458 MatcherTrue matcher_true_;
1459 MatcherFalse matcher_false_;
1460 };
1461
1462
1463 template <template <class> class MatcherImpl, typename T>
1464 class SomeOfArrayMatcher {
1465 public:
1466
1467
1468 template <typename Iter>
1469 SomeOfArrayMatcher(Iter first, Iter last) : matchers_(first, last) {}
1470
1471 template <typename U>
1472 operator Matcher<U>() const {
1473 using RawU = typename std::decay<U>::type;
1474 std::vector<Matcher<RawU>> matchers;
1475 for (const auto& matcher : matchers_) {
1476 matchers.push_back(MatcherCast<RawU>(matcher));
1477 }
1478 return Matcher<U>(new MatcherImpl<RawU>(std::move(matchers)));
1479 }
1480
1481 private:
1482 const ::std::vector<T> matchers_;
1483 };
1484
1485 template <typename T>
1486 using AllOfArrayMatcher = SomeOfArrayMatcher<AllOfMatcherImpl, T>;
1487
1488 template <typename T>
1489 using AnyOfArrayMatcher = SomeOfArrayMatcher<AnyOfMatcherImpl, T>;
1490
1491
1492
1493 template <typename Predicate>
1494 class TrulyMatcher {
1495 public:
1496 explicit TrulyMatcher(Predicate pred) : predicate_(pred) {}
1497
1498
1499
1500
1501
1502 template <typename T>
1503 bool MatchAndExplain(T& x,
1504 MatchResultListener* listener) const {
1505
1506
1507
1508
1509
1510
1511 if (predicate_(x)) return true;
1512 *listener << "didn't satisfy the given predicate";
1513 return false;
1514 }
1515
1516 void DescribeTo(::std::ostream* os) const {
1517 *os << "satisfies the given predicate";
1518 }
1519
1520 void DescribeNegationTo(::std::ostream* os) const {
1521 *os << "doesn't satisfy the given predicate";
1522 }
1523
1524 private:
1525 Predicate predicate_;
1526 };
1527
1528
1529
1530 template <typename M>
1531 class MatcherAsPredicate {
1532 public:
1533 explicit MatcherAsPredicate(M matcher) : matcher_(matcher) {}
1534
1535
1536
1537
1538
1539
1540
1541 template <typename T>
1542 bool operator()(const T& x) const {
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557 return MatcherCast<const T&>(matcher_).Matches(x);
1558 }
1559
1560 private:
1561 M matcher_;
1562 };
1563
1564
1565
1566 template <typename M>
1567 class PredicateFormatterFromMatcher {
1568 public:
1569 explicit PredicateFormatterFromMatcher(M m) : matcher_(std::move(m)) {}
1570
1571
1572
1573
1574 template <typename T>
1575 AssertionResult operator()(const char* value_text, const T& x) const {
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587 const Matcher<const T&> matcher = SafeMatcherCast<const T&>(matcher_);
1588
1589
1590
1591 if (matcher.Matches(x)) {
1592 return AssertionSuccess();
1593 }
1594
1595 ::std::stringstream ss;
1596 ss << "Value of: " << value_text << "\n"
1597 << "Expected: ";
1598 matcher.DescribeTo(&ss);
1599
1600
1601 StringMatchResultListener listener;
1602 if (MatchPrintAndExplain(x, matcher, &listener)) {
1603 ss << "\n The matcher failed on the initial attempt; but passed when "
1604 "rerun to generate the explanation.";
1605 }
1606 ss << "\n Actual: " << listener.str();
1607 return AssertionFailure() << ss.str();
1608 }
1609
1610 private:
1611 const M matcher_;
1612 };
1613
1614
1615
1616
1617
1618 template <typename M>
1619 inline PredicateFormatterFromMatcher<M> MakePredicateFormatterFromMatcher(
1620 M matcher) {
1621 return PredicateFormatterFromMatcher<M>(std::move(matcher));
1622 }
1623
1624
1625
1626 class IsNanMatcher {
1627 public:
1628 template <typename FloatType>
1629 bool MatchAndExplain(const FloatType& f,
1630 MatchResultListener* ) const {
1631 return (::std::isnan)(f);
1632 }
1633
1634 void DescribeTo(::std::ostream* os) const { *os << "is NaN"; }
1635 void DescribeNegationTo(::std::ostream* os) const { *os << "isn't NaN"; }
1636 };
1637
1638
1639
1640
1641
1642 template <typename FloatType>
1643 class FloatingEqMatcher {
1644 public:
1645
1646
1647
1648
1649
1650
1651 FloatingEqMatcher(FloatType expected, bool nan_eq_nan)
1652 : expected_(expected), nan_eq_nan_(nan_eq_nan), max_abs_error_(-1) {}
1653
1654
1655
1656
1657 FloatingEqMatcher(FloatType expected, bool nan_eq_nan,
1658 FloatType max_abs_error)
1659 : expected_(expected),
1660 nan_eq_nan_(nan_eq_nan),
1661 max_abs_error_(max_abs_error) {
1662 GTEST_CHECK_(max_abs_error >= 0)
1663 << ", where max_abs_error is" << max_abs_error;
1664 }
1665
1666
1667 template <typename T>
1668 class Impl : public MatcherInterface<T> {
1669 public:
1670 Impl(FloatType expected, bool nan_eq_nan, FloatType max_abs_error)
1671 : expected_(expected),
1672 nan_eq_nan_(nan_eq_nan),
1673 max_abs_error_(max_abs_error) {}
1674
1675 bool MatchAndExplain(T value,
1676 MatchResultListener* listener) const override {
1677 const FloatingPoint<FloatType> actual(value), expected(expected_);
1678
1679
1680 if (actual.is_nan() || expected.is_nan()) {
1681 if (actual.is_nan() && expected.is_nan()) {
1682 return nan_eq_nan_;
1683 }
1684
1685 return false;
1686 }
1687 if (HasMaxAbsError()) {
1688
1689
1690
1691
1692 if (value == expected_) {
1693 return true;
1694 }
1695
1696 const FloatType diff = value - expected_;
1697 if (::std::fabs(diff) <= max_abs_error_) {
1698 return true;
1699 }
1700
1701 if (listener->IsInterested()) {
1702 *listener << "which is " << diff << " from " << expected_;
1703 }
1704 return false;
1705 } else {
1706 return actual.AlmostEquals(expected);
1707 }
1708 }
1709
1710 void DescribeTo(::std::ostream* os) const override {
1711
1712
1713
1714 const ::std::streamsize old_precision =
1715 os->precision(::std::numeric_limits<FloatType>::digits10 + 2);
1716 if (FloatingPoint<FloatType>(expected_).is_nan()) {
1717 if (nan_eq_nan_) {
1718 *os << "is NaN";
1719 } else {
1720 *os << "never matches";
1721 }
1722 } else {
1723 *os << "is approximately " << expected_;
1724 if (HasMaxAbsError()) {
1725 *os << " (absolute error <= " << max_abs_error_ << ")";
1726 }
1727 }
1728 os->precision(old_precision);
1729 }
1730
1731 void DescribeNegationTo(::std::ostream* os) const override {
1732
1733 const ::std::streamsize old_precision =
1734 os->precision(::std::numeric_limits<FloatType>::digits10 + 2);
1735 if (FloatingPoint<FloatType>(expected_).is_nan()) {
1736 if (nan_eq_nan_) {
1737 *os << "isn't NaN";
1738 } else {
1739 *os << "is anything";
1740 }
1741 } else {
1742 *os << "isn't approximately " << expected_;
1743 if (HasMaxAbsError()) {
1744 *os << " (absolute error > " << max_abs_error_ << ")";
1745 }
1746 }
1747
1748 os->precision(old_precision);
1749 }
1750
1751 private:
1752 bool HasMaxAbsError() const { return max_abs_error_ >= 0; }
1753
1754 const FloatType expected_;
1755 const bool nan_eq_nan_;
1756
1757 const FloatType max_abs_error_;
1758 };
1759
1760
1761
1762
1763 operator Matcher<FloatType>() const {
1764 return MakeMatcher(
1765 new Impl<FloatType>(expected_, nan_eq_nan_, max_abs_error_));
1766 }
1767
1768 operator Matcher<const FloatType&>() const {
1769 return MakeMatcher(
1770 new Impl<const FloatType&>(expected_, nan_eq_nan_, max_abs_error_));
1771 }
1772
1773 operator Matcher<FloatType&>() const {
1774 return MakeMatcher(
1775 new Impl<FloatType&>(expected_, nan_eq_nan_, max_abs_error_));
1776 }
1777
1778 private:
1779 const FloatType expected_;
1780 const bool nan_eq_nan_;
1781
1782 const FloatType max_abs_error_;
1783 };
1784
1785
1786
1787
1788
1789
1790 template <typename FloatType>
1791 class FloatingEq2Matcher {
1792 public:
1793 FloatingEq2Matcher() { Init(-1, false); }
1794
1795 explicit FloatingEq2Matcher(bool nan_eq_nan) { Init(-1, nan_eq_nan); }
1796
1797 explicit FloatingEq2Matcher(FloatType max_abs_error) {
1798 Init(max_abs_error, false);
1799 }
1800
1801 FloatingEq2Matcher(FloatType max_abs_error, bool nan_eq_nan) {
1802 Init(max_abs_error, nan_eq_nan);
1803 }
1804
1805 template <typename T1, typename T2>
1806 operator Matcher<::std::tuple<T1, T2>>() const {
1807 return MakeMatcher(
1808 new Impl<::std::tuple<T1, T2>>(max_abs_error_, nan_eq_nan_));
1809 }
1810 template <typename T1, typename T2>
1811 operator Matcher<const ::std::tuple<T1, T2>&>() const {
1812 return MakeMatcher(
1813 new Impl<const ::std::tuple<T1, T2>&>(max_abs_error_, nan_eq_nan_));
1814 }
1815
1816 private:
1817 static ::std::ostream& GetDesc(::std::ostream& os) {
1818 return os << "an almost-equal pair";
1819 }
1820
1821 template <typename Tuple>
1822 class Impl : public MatcherInterface<Tuple> {
1823 public:
1824 Impl(FloatType max_abs_error, bool nan_eq_nan)
1825 : max_abs_error_(max_abs_error), nan_eq_nan_(nan_eq_nan) {}
1826
1827 bool MatchAndExplain(Tuple args,
1828 MatchResultListener* listener) const override {
1829 if (max_abs_error_ == -1) {
1830 FloatingEqMatcher<FloatType> fm(::std::get<0>(args), nan_eq_nan_);
1831 return static_cast<Matcher<FloatType>>(fm).MatchAndExplain(
1832 ::std::get<1>(args), listener);
1833 } else {
1834 FloatingEqMatcher<FloatType> fm(::std::get<0>(args), nan_eq_nan_,
1835 max_abs_error_);
1836 return static_cast<Matcher<FloatType>>(fm).MatchAndExplain(
1837 ::std::get<1>(args), listener);
1838 }
1839 }
1840 void DescribeTo(::std::ostream* os) const override {
1841 *os << "are " << GetDesc;
1842 }
1843 void DescribeNegationTo(::std::ostream* os) const override {
1844 *os << "aren't " << GetDesc;
1845 }
1846
1847 private:
1848 FloatType max_abs_error_;
1849 const bool nan_eq_nan_;
1850 };
1851
1852 void Init(FloatType max_abs_error_val, bool nan_eq_nan_val) {
1853 max_abs_error_ = max_abs_error_val;
1854 nan_eq_nan_ = nan_eq_nan_val;
1855 }
1856 FloatType max_abs_error_;
1857 bool nan_eq_nan_;
1858 };
1859
1860
1861
1862 template <typename InnerMatcher>
1863 class PointeeMatcher {
1864 public:
1865 explicit PointeeMatcher(const InnerMatcher& matcher) : matcher_(matcher) {}
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875 template <typename Pointer>
1876 operator Matcher<Pointer>() const {
1877 return Matcher<Pointer>(new Impl<const Pointer&>(matcher_));
1878 }
1879
1880 private:
1881
1882 template <typename Pointer>
1883 class Impl : public MatcherInterface<Pointer> {
1884 public:
1885 using Pointee =
1886 typename std::pointer_traits<GTEST_REMOVE_REFERENCE_AND_CONST_(
1887 Pointer)>::element_type;
1888
1889 explicit Impl(const InnerMatcher& matcher)
1890 : matcher_(MatcherCast<const Pointee&>(matcher)) {}
1891
1892 void DescribeTo(::std::ostream* os) const override {
1893 *os << "points to a value that ";
1894 matcher_.DescribeTo(os);
1895 }
1896
1897 void DescribeNegationTo(::std::ostream* os) const override {
1898 *os << "does not point to a value that ";
1899 matcher_.DescribeTo(os);
1900 }
1901
1902 bool MatchAndExplain(Pointer pointer,
1903 MatchResultListener* listener) const override {
1904 if (GetRawPointer(pointer) == nullptr) return false;
1905
1906 *listener << "which points to ";
1907 return MatchPrintAndExplain(*pointer, matcher_, listener);
1908 }
1909
1910 private:
1911 const Matcher<const Pointee&> matcher_;
1912 };
1913
1914 const InnerMatcher matcher_;
1915 };
1916
1917
1918
1919
1920
1921 template <typename InnerMatcher>
1922 class PointerMatcher {
1923 public:
1924 explicit PointerMatcher(const InnerMatcher& matcher) : matcher_(matcher) {}
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934 template <typename PointerType>
1935 operator Matcher<PointerType>() const {
1936 return Matcher<PointerType>(new Impl<const PointerType&>(matcher_));
1937 }
1938
1939 private:
1940
1941 template <typename PointerType>
1942 class Impl : public MatcherInterface<PointerType> {
1943 public:
1944 using Pointer =
1945 const typename std::pointer_traits<GTEST_REMOVE_REFERENCE_AND_CONST_(
1946 PointerType)>::element_type*;
1947
1948 explicit Impl(const InnerMatcher& matcher)
1949 : matcher_(MatcherCast<Pointer>(matcher)) {}
1950
1951 void DescribeTo(::std::ostream* os) const override {
1952 *os << "is a pointer that ";
1953 matcher_.DescribeTo(os);
1954 }
1955
1956 void DescribeNegationTo(::std::ostream* os) const override {
1957 *os << "is not a pointer that ";
1958 matcher_.DescribeTo(os);
1959 }
1960
1961 bool MatchAndExplain(PointerType pointer,
1962 MatchResultListener* listener) const override {
1963 *listener << "which is a pointer that ";
1964 Pointer p = GetRawPointer(pointer);
1965 return MatchPrintAndExplain(p, matcher_, listener);
1966 }
1967
1968 private:
1969 Matcher<Pointer> matcher_;
1970 };
1971
1972 const InnerMatcher matcher_;
1973 };
1974
1975 #if GTEST_HAS_RTTI
1976
1977
1978
1979
1980
1981
1982 template <typename To>
1983 class WhenDynamicCastToMatcherBase {
1984 public:
1985 explicit WhenDynamicCastToMatcherBase(const Matcher<To>& matcher)
1986 : matcher_(matcher) {}
1987
1988 void DescribeTo(::std::ostream* os) const {
1989 GetCastTypeDescription(os);
1990 matcher_.DescribeTo(os);
1991 }
1992
1993 void DescribeNegationTo(::std::ostream* os) const {
1994 GetCastTypeDescription(os);
1995 matcher_.DescribeNegationTo(os);
1996 }
1997
1998 protected:
1999 const Matcher<To> matcher_;
2000
2001 static std::string GetToName() { return GetTypeName<To>(); }
2002
2003 private:
2004 static void GetCastTypeDescription(::std::ostream* os) {
2005 *os << "when dynamic_cast to " << GetToName() << ", ";
2006 }
2007 };
2008
2009
2010
2011 template <typename To>
2012 class WhenDynamicCastToMatcher : public WhenDynamicCastToMatcherBase<To> {
2013 public:
2014 explicit WhenDynamicCastToMatcher(const Matcher<To>& matcher)
2015 : WhenDynamicCastToMatcherBase<To>(matcher) {}
2016
2017 template <typename From>
2018 bool MatchAndExplain(From from, MatchResultListener* listener) const {
2019 To to = dynamic_cast<To>(from);
2020 return MatchPrintAndExplain(to, this->matcher_, listener);
2021 }
2022 };
2023
2024
2025
2026 template <typename To>
2027 class WhenDynamicCastToMatcher<To&> : public WhenDynamicCastToMatcherBase<To&> {
2028 public:
2029 explicit WhenDynamicCastToMatcher(const Matcher<To&>& matcher)
2030 : WhenDynamicCastToMatcherBase<To&>(matcher) {}
2031
2032 template <typename From>
2033 bool MatchAndExplain(From& from, MatchResultListener* listener) const {
2034
2035 To* to = dynamic_cast<To*>(&from);
2036 if (to == nullptr) {
2037 *listener << "which cannot be dynamic_cast to " << this->GetToName();
2038 return false;
2039 }
2040 return MatchPrintAndExplain(*to, this->matcher_, listener);
2041 }
2042 };
2043 #endif
2044
2045
2046
2047 template <typename Class, typename FieldType>
2048 class FieldMatcher {
2049 public:
2050 FieldMatcher(FieldType Class::*field,
2051 const Matcher<const FieldType&>& matcher)
2052 : field_(field), matcher_(matcher), whose_field_("whose given field ") {}
2053
2054 FieldMatcher(const std::string& field_name, FieldType Class::*field,
2055 const Matcher<const FieldType&>& matcher)
2056 : field_(field),
2057 matcher_(matcher),
2058 whose_field_("whose field `" + field_name + "` ") {}
2059
2060 void DescribeTo(::std::ostream* os) const {
2061 *os << "is an object " << whose_field_;
2062 matcher_.DescribeTo(os);
2063 }
2064
2065 void DescribeNegationTo(::std::ostream* os) const {
2066 *os << "is an object " << whose_field_;
2067 matcher_.DescribeNegationTo(os);
2068 }
2069
2070 template <typename T>
2071 bool MatchAndExplain(const T& value, MatchResultListener* listener) const {
2072
2073
2074 return MatchAndExplainImpl(
2075 typename std::is_pointer<typename std::remove_const<T>::type>::type(),
2076 value, listener);
2077 }
2078
2079 private:
2080 bool MatchAndExplainImpl(std::false_type ,
2081 const Class& obj,
2082 MatchResultListener* listener) const {
2083 *listener << whose_field_ << "is ";
2084 return MatchPrintAndExplain(obj.*field_, matcher_, listener);
2085 }
2086
2087 bool MatchAndExplainImpl(std::true_type , const Class* p,
2088 MatchResultListener* listener) const {
2089 if (p == nullptr) return false;
2090
2091 *listener << "which points to an object ";
2092
2093
2094
2095 return MatchAndExplainImpl(std::false_type(), *p, listener);
2096 }
2097
2098 const FieldType Class::*field_;
2099 const Matcher<const FieldType&> matcher_;
2100
2101
2102
2103 const std::string whose_field_;
2104 };
2105
2106
2107
2108
2109
2110
2111 template <typename Class, typename PropertyType, typename Property>
2112 class PropertyMatcher {
2113 public:
2114 typedef const PropertyType& RefToConstProperty;
2115
2116 PropertyMatcher(Property property, const Matcher<RefToConstProperty>& matcher)
2117 : property_(property),
2118 matcher_(matcher),
2119 whose_property_("whose given property ") {}
2120
2121 PropertyMatcher(const std::string& property_name, Property property,
2122 const Matcher<RefToConstProperty>& matcher)
2123 : property_(property),
2124 matcher_(matcher),
2125 whose_property_("whose property `" + property_name + "` ") {}
2126
2127 void DescribeTo(::std::ostream* os) const {
2128 *os << "is an object " << whose_property_;
2129 matcher_.DescribeTo(os);
2130 }
2131
2132 void DescribeNegationTo(::std::ostream* os) const {
2133 *os << "is an object " << whose_property_;
2134 matcher_.DescribeNegationTo(os);
2135 }
2136
2137 template <typename T>
2138 bool MatchAndExplain(const T& value, MatchResultListener* listener) const {
2139 return MatchAndExplainImpl(
2140 typename std::is_pointer<typename std::remove_const<T>::type>::type(),
2141 value, listener);
2142 }
2143
2144 private:
2145 bool MatchAndExplainImpl(std::false_type ,
2146 const Class& obj,
2147 MatchResultListener* listener) const {
2148 *listener << whose_property_ << "is ";
2149
2150
2151 RefToConstProperty result = (obj.*property_)();
2152 return MatchPrintAndExplain(result, matcher_, listener);
2153 }
2154
2155 bool MatchAndExplainImpl(std::true_type , const Class* p,
2156 MatchResultListener* listener) const {
2157 if (p == nullptr) return false;
2158
2159 *listener << "which points to an object ";
2160
2161
2162
2163 return MatchAndExplainImpl(std::false_type(), *p, listener);
2164 }
2165
2166 Property property_;
2167 const Matcher<RefToConstProperty> matcher_;
2168
2169
2170
2171 const std::string whose_property_;
2172 };
2173
2174
2175
2176 template <typename Functor>
2177 struct CallableTraits {
2178 typedef Functor StorageType;
2179
2180 static void CheckIsValid(Functor ) {}
2181
2182 template <typename T>
2183 static auto Invoke(Functor f, const T& arg) -> decltype(f(arg)) {
2184 return f(arg);
2185 }
2186 };
2187
2188
2189 template <typename ArgType, typename ResType>
2190 struct CallableTraits<ResType (*)(ArgType)> {
2191 typedef ResType ResultType;
2192 typedef ResType (*StorageType)(ArgType);
2193
2194 static void CheckIsValid(ResType (*f)(ArgType)) {
2195 GTEST_CHECK_(f != nullptr)
2196 << "NULL function pointer is passed into ResultOf().";
2197 }
2198 template <typename T>
2199 static ResType Invoke(ResType (*f)(ArgType), T arg) {
2200 return (*f)(arg);
2201 }
2202 };
2203
2204
2205
2206 template <typename Callable, typename InnerMatcher>
2207 class ResultOfMatcher {
2208 public:
2209 ResultOfMatcher(Callable callable, InnerMatcher matcher)
2210 : ResultOfMatcher("", std::move(callable),
2211 std::move(matcher)) {}
2212
2213 ResultOfMatcher(const std::string& result_description, Callable callable,
2214 InnerMatcher matcher)
2215 : result_description_(result_description),
2216 callable_(std::move(callable)),
2217 matcher_(std::move(matcher)) {
2218 CallableTraits<Callable>::CheckIsValid(callable_);
2219 }
2220
2221 template <typename T>
2222 operator Matcher<T>() const {
2223 return Matcher<T>(
2224 new Impl<const T&>(result_description_, callable_, matcher_));
2225 }
2226
2227 private:
2228 typedef typename CallableTraits<Callable>::StorageType CallableStorageType;
2229
2230 template <typename T>
2231 class Impl : public MatcherInterface<T> {
2232 using ResultType = decltype(CallableTraits<Callable>::template Invoke<T>(
2233 std::declval<CallableStorageType>(), std::declval<T>()));
2234
2235 public:
2236 template <typename M>
2237 Impl(const std::string& result_description,
2238 const CallableStorageType& callable, const M& matcher)
2239 : result_description_(result_description),
2240 callable_(callable),
2241 matcher_(MatcherCast<ResultType>(matcher)) {}
2242
2243 void DescribeTo(::std::ostream* os) const override {
2244 if (result_description_.empty()) {
2245 *os << "is mapped by the given callable to a value that ";
2246 } else {
2247 *os << "whose " << result_description_ << " ";
2248 }
2249 matcher_.DescribeTo(os);
2250 }
2251
2252 void DescribeNegationTo(::std::ostream* os) const override {
2253 if (result_description_.empty()) {
2254 *os << "is mapped by the given callable to a value that ";
2255 } else {
2256 *os << "whose " << result_description_ << " ";
2257 }
2258 matcher_.DescribeNegationTo(os);
2259 }
2260
2261 bool MatchAndExplain(T obj, MatchResultListener* listener) const override {
2262 if (result_description_.empty()) {
2263 *listener << "which is mapped by the given callable to ";
2264 } else {
2265 *listener << "whose " << result_description_ << " is ";
2266 }
2267
2268
2269
2270
2271 ResultType result =
2272 CallableTraits<Callable>::template Invoke<T>(callable_, obj);
2273 return MatchPrintAndExplain(result, matcher_, listener);
2274 }
2275
2276 private:
2277 const std::string result_description_;
2278
2279
2280
2281
2282
2283 mutable CallableStorageType callable_;
2284 const Matcher<ResultType> matcher_;
2285 };
2286
2287 const std::string result_description_;
2288 const CallableStorageType callable_;
2289 const InnerMatcher matcher_;
2290 };
2291
2292
2293 template <typename SizeMatcher>
2294 class SizeIsMatcher {
2295 public:
2296 explicit SizeIsMatcher(const SizeMatcher& size_matcher)
2297 : size_matcher_(size_matcher) {}
2298
2299 template <typename Container>
2300 operator Matcher<Container>() const {
2301 return Matcher<Container>(new Impl<const Container&>(size_matcher_));
2302 }
2303
2304 template <typename Container>
2305 class Impl : public MatcherInterface<Container> {
2306 public:
2307 using SizeType = decltype(std::declval<Container>().size());
2308 explicit Impl(const SizeMatcher& size_matcher)
2309 : size_matcher_(MatcherCast<SizeType>(size_matcher)) {}
2310
2311 void DescribeTo(::std::ostream* os) const override {
2312 *os << "size ";
2313 size_matcher_.DescribeTo(os);
2314 }
2315 void DescribeNegationTo(::std::ostream* os) const override {
2316 *os << "size ";
2317 size_matcher_.DescribeNegationTo(os);
2318 }
2319
2320 bool MatchAndExplain(Container container,
2321 MatchResultListener* listener) const override {
2322 SizeType size = container.size();
2323 StringMatchResultListener size_listener;
2324 const bool result = size_matcher_.MatchAndExplain(size, &size_listener);
2325 *listener << "whose size " << size
2326 << (result ? " matches" : " doesn't match");
2327 PrintIfNotEmpty(size_listener.str(), listener->stream());
2328 return result;
2329 }
2330
2331 private:
2332 const Matcher<SizeType> size_matcher_;
2333 };
2334
2335 private:
2336 const SizeMatcher size_matcher_;
2337 };
2338
2339
2340
2341 template <typename DistanceMatcher>
2342 class BeginEndDistanceIsMatcher {
2343 public:
2344 explicit BeginEndDistanceIsMatcher(const DistanceMatcher& distance_matcher)
2345 : distance_matcher_(distance_matcher) {}
2346
2347 template <typename Container>
2348 operator Matcher<Container>() const {
2349 return Matcher<Container>(new Impl<const Container&>(distance_matcher_));
2350 }
2351
2352 template <typename Container>
2353 class Impl : public MatcherInterface<Container> {
2354 public:
2355 typedef internal::StlContainerView<GTEST_REMOVE_REFERENCE_AND_CONST_(
2356 Container)>
2357 ContainerView;
2358 typedef typename std::iterator_traits<
2359 typename ContainerView::type::const_iterator>::difference_type
2360 DistanceType;
2361 explicit Impl(const DistanceMatcher& distance_matcher)
2362 : distance_matcher_(MatcherCast<DistanceType>(distance_matcher)) {}
2363
2364 void DescribeTo(::std::ostream* os) const override {
2365 *os << "distance between begin() and end() ";
2366 distance_matcher_.DescribeTo(os);
2367 }
2368 void DescribeNegationTo(::std::ostream* os) const override {
2369 *os << "distance between begin() and end() ";
2370 distance_matcher_.DescribeNegationTo(os);
2371 }
2372
2373 bool MatchAndExplain(Container container,
2374 MatchResultListener* listener) const override {
2375 using std::begin;
2376 using std::end;
2377 DistanceType distance = std::distance(begin(container), end(container));
2378 StringMatchResultListener distance_listener;
2379 const bool result =
2380 distance_matcher_.MatchAndExplain(distance, &distance_listener);
2381 *listener << "whose distance between begin() and end() " << distance
2382 << (result ? " matches" : " doesn't match");
2383 PrintIfNotEmpty(distance_listener.str(), listener->stream());
2384 return result;
2385 }
2386
2387 private:
2388 const Matcher<DistanceType> distance_matcher_;
2389 };
2390
2391 private:
2392 const DistanceMatcher distance_matcher_;
2393 };
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405 template <typename Container>
2406 class ContainerEqMatcher {
2407 public:
2408 typedef internal::StlContainerView<Container> View;
2409 typedef typename View::type StlContainer;
2410 typedef typename View::const_reference StlContainerReference;
2411
2412 static_assert(!std::is_const<Container>::value,
2413 "Container type must not be const");
2414 static_assert(!std::is_reference<Container>::value,
2415 "Container type must not be a reference");
2416
2417
2418
2419 explicit ContainerEqMatcher(const Container& expected)
2420 : expected_(View::Copy(expected)) {}
2421
2422 void DescribeTo(::std::ostream* os) const {
2423 *os << "equals ";
2424 UniversalPrint(expected_, os);
2425 }
2426 void DescribeNegationTo(::std::ostream* os) const {
2427 *os << "does not equal ";
2428 UniversalPrint(expected_, os);
2429 }
2430
2431 template <typename LhsContainer>
2432 bool MatchAndExplain(const LhsContainer& lhs,
2433 MatchResultListener* listener) const {
2434 typedef internal::StlContainerView<
2435 typename std::remove_const<LhsContainer>::type>
2436 LhsView;
2437 StlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2438 if (lhs_stl_container == expected_) return true;
2439
2440 ::std::ostream* const os = listener->stream();
2441 if (os != nullptr) {
2442
2443 bool printed_header = false;
2444 for (auto it = lhs_stl_container.begin(); it != lhs_stl_container.end();
2445 ++it) {
2446 if (internal::ArrayAwareFind(expected_.begin(), expected_.end(), *it) ==
2447 expected_.end()) {
2448 if (printed_header) {
2449 *os << ", ";
2450 } else {
2451 *os << "which has these unexpected elements: ";
2452 printed_header = true;
2453 }
2454 UniversalPrint(*it, os);
2455 }
2456 }
2457
2458
2459 bool printed_header2 = false;
2460 for (auto it = expected_.begin(); it != expected_.end(); ++it) {
2461 if (internal::ArrayAwareFind(lhs_stl_container.begin(),
2462 lhs_stl_container.end(),
2463 *it) == lhs_stl_container.end()) {
2464 if (printed_header2) {
2465 *os << ", ";
2466 } else {
2467 *os << (printed_header ? ",\nand" : "which")
2468 << " doesn't have these expected elements: ";
2469 printed_header2 = true;
2470 }
2471 UniversalPrint(*it, os);
2472 }
2473 }
2474 }
2475
2476 return false;
2477 }
2478
2479 private:
2480 const StlContainer expected_;
2481 };
2482
2483
2484 struct LessComparator {
2485 template <typename T, typename U>
2486 bool operator()(const T& lhs, const U& rhs) const {
2487 return lhs < rhs;
2488 }
2489 };
2490
2491
2492 template <typename Comparator, typename ContainerMatcher>
2493 class WhenSortedByMatcher {
2494 public:
2495 WhenSortedByMatcher(const Comparator& comparator,
2496 const ContainerMatcher& matcher)
2497 : comparator_(comparator), matcher_(matcher) {}
2498
2499 template <typename LhsContainer>
2500 operator Matcher<LhsContainer>() const {
2501 return MakeMatcher(new Impl<LhsContainer>(comparator_, matcher_));
2502 }
2503
2504 template <typename LhsContainer>
2505 class Impl : public MatcherInterface<LhsContainer> {
2506 public:
2507 typedef internal::StlContainerView<GTEST_REMOVE_REFERENCE_AND_CONST_(
2508 LhsContainer)>
2509 LhsView;
2510 typedef typename LhsView::type LhsStlContainer;
2511 typedef typename LhsView::const_reference LhsStlContainerReference;
2512
2513
2514 typedef
2515 typename RemoveConstFromKey<typename LhsStlContainer::value_type>::type
2516 LhsValue;
2517
2518 Impl(const Comparator& comparator, const ContainerMatcher& matcher)
2519 : comparator_(comparator), matcher_(matcher) {}
2520
2521 void DescribeTo(::std::ostream* os) const override {
2522 *os << "(when sorted) ";
2523 matcher_.DescribeTo(os);
2524 }
2525
2526 void DescribeNegationTo(::std::ostream* os) const override {
2527 *os << "(when sorted) ";
2528 matcher_.DescribeNegationTo(os);
2529 }
2530
2531 bool MatchAndExplain(LhsContainer lhs,
2532 MatchResultListener* listener) const override {
2533 LhsStlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2534 ::std::vector<LhsValue> sorted_container(lhs_stl_container.begin(),
2535 lhs_stl_container.end());
2536 ::std::sort(sorted_container.begin(), sorted_container.end(),
2537 comparator_);
2538
2539 if (!listener->IsInterested()) {
2540
2541
2542 return matcher_.Matches(sorted_container);
2543 }
2544
2545 *listener << "which is ";
2546 UniversalPrint(sorted_container, listener->stream());
2547 *listener << " when sorted";
2548
2549 StringMatchResultListener inner_listener;
2550 const bool match =
2551 matcher_.MatchAndExplain(sorted_container, &inner_listener);
2552 PrintIfNotEmpty(inner_listener.str(), listener->stream());
2553 return match;
2554 }
2555
2556 private:
2557 const Comparator comparator_;
2558 const Matcher<const ::std::vector<LhsValue>&> matcher_;
2559
2560 Impl(const Impl&) = delete;
2561 Impl& operator=(const Impl&) = delete;
2562 };
2563
2564 private:
2565 const Comparator comparator_;
2566 const ContainerMatcher matcher_;
2567 };
2568
2569
2570
2571
2572
2573 template <typename TupleMatcher, typename RhsContainer>
2574 class PointwiseMatcher {
2575 static_assert(
2576 !IsHashTable<GTEST_REMOVE_REFERENCE_AND_CONST_(RhsContainer)>::value,
2577 "use UnorderedPointwise with hash tables");
2578
2579 public:
2580 typedef internal::StlContainerView<RhsContainer> RhsView;
2581 typedef typename RhsView::type RhsStlContainer;
2582 typedef typename RhsStlContainer::value_type RhsValue;
2583
2584 static_assert(!std::is_const<RhsContainer>::value,
2585 "RhsContainer type must not be const");
2586 static_assert(!std::is_reference<RhsContainer>::value,
2587 "RhsContainer type must not be a reference");
2588
2589
2590
2591 PointwiseMatcher(const TupleMatcher& tuple_matcher, const RhsContainer& rhs)
2592 : tuple_matcher_(tuple_matcher), rhs_(RhsView::Copy(rhs)) {}
2593
2594 template <typename LhsContainer>
2595 operator Matcher<LhsContainer>() const {
2596 static_assert(
2597 !IsHashTable<GTEST_REMOVE_REFERENCE_AND_CONST_(LhsContainer)>::value,
2598 "use UnorderedPointwise with hash tables");
2599
2600 return Matcher<LhsContainer>(
2601 new Impl<const LhsContainer&>(tuple_matcher_, rhs_));
2602 }
2603
2604 template <typename LhsContainer>
2605 class Impl : public MatcherInterface<LhsContainer> {
2606 public:
2607 typedef internal::StlContainerView<GTEST_REMOVE_REFERENCE_AND_CONST_(
2608 LhsContainer)>
2609 LhsView;
2610 typedef typename LhsView::type LhsStlContainer;
2611 typedef typename LhsView::const_reference LhsStlContainerReference;
2612 typedef typename LhsStlContainer::value_type LhsValue;
2613
2614
2615
2616
2617 typedef ::std::tuple<const LhsValue&, const RhsValue&> InnerMatcherArg;
2618
2619 Impl(const TupleMatcher& tuple_matcher, const RhsStlContainer& rhs)
2620
2621 : mono_tuple_matcher_(SafeMatcherCast<InnerMatcherArg>(tuple_matcher)),
2622 rhs_(rhs) {}
2623
2624 void DescribeTo(::std::ostream* os) const override {
2625 *os << "contains " << rhs_.size()
2626 << " values, where each value and its corresponding value in ";
2627 UniversalPrinter<RhsStlContainer>::Print(rhs_, os);
2628 *os << " ";
2629 mono_tuple_matcher_.DescribeTo(os);
2630 }
2631 void DescribeNegationTo(::std::ostream* os) const override {
2632 *os << "doesn't contain exactly " << rhs_.size()
2633 << " values, or contains a value x at some index i"
2634 << " where x and the i-th value of ";
2635 UniversalPrint(rhs_, os);
2636 *os << " ";
2637 mono_tuple_matcher_.DescribeNegationTo(os);
2638 }
2639
2640 bool MatchAndExplain(LhsContainer lhs,
2641 MatchResultListener* listener) const override {
2642 LhsStlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2643 const size_t actual_size = lhs_stl_container.size();
2644 if (actual_size != rhs_.size()) {
2645 *listener << "which contains " << actual_size << " values";
2646 return false;
2647 }
2648
2649 auto left = lhs_stl_container.begin();
2650 auto right = rhs_.begin();
2651 for (size_t i = 0; i != actual_size; ++i, ++left, ++right) {
2652 if (listener->IsInterested()) {
2653 StringMatchResultListener inner_listener;
2654
2655
2656
2657 if (!mono_tuple_matcher_.MatchAndExplain(
2658 InnerMatcherArg(ImplicitCast_<const LhsValue&>(*left),
2659 ImplicitCast_<const RhsValue&>(*right)),
2660 &inner_listener)) {
2661 *listener << "where the value pair (";
2662 UniversalPrint(*left, listener->stream());
2663 *listener << ", ";
2664 UniversalPrint(*right, listener->stream());
2665 *listener << ") at index #" << i << " don't match";
2666 PrintIfNotEmpty(inner_listener.str(), listener->stream());
2667 return false;
2668 }
2669 } else {
2670 if (!mono_tuple_matcher_.Matches(
2671 InnerMatcherArg(ImplicitCast_<const LhsValue&>(*left),
2672 ImplicitCast_<const RhsValue&>(*right))))
2673 return false;
2674 }
2675 }
2676
2677 return true;
2678 }
2679
2680 private:
2681 const Matcher<InnerMatcherArg> mono_tuple_matcher_;
2682 const RhsStlContainer rhs_;
2683 };
2684
2685 private:
2686 const TupleMatcher tuple_matcher_;
2687 const RhsStlContainer rhs_;
2688 };
2689
2690
2691 template <typename Container>
2692 class QuantifierMatcherImpl : public MatcherInterface<Container> {
2693 public:
2694 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
2695 typedef StlContainerView<RawContainer> View;
2696 typedef typename View::type StlContainer;
2697 typedef typename View::const_reference StlContainerReference;
2698 typedef typename StlContainer::value_type Element;
2699
2700 template <typename InnerMatcher>
2701 explicit QuantifierMatcherImpl(InnerMatcher inner_matcher)
2702 : inner_matcher_(
2703 testing::SafeMatcherCast<const Element&>(inner_matcher)) {}
2704
2705
2706
2707
2708 bool MatchAndExplainImpl(bool all_elements_should_match, Container container,
2709 MatchResultListener* listener) const {
2710 StlContainerReference stl_container = View::ConstReference(container);
2711 size_t i = 0;
2712 for (auto it = stl_container.begin(); it != stl_container.end();
2713 ++it, ++i) {
2714 StringMatchResultListener inner_listener;
2715 const bool matches = inner_matcher_.MatchAndExplain(*it, &inner_listener);
2716
2717 if (matches != all_elements_should_match) {
2718 *listener << "whose element #" << i
2719 << (matches ? " matches" : " doesn't match");
2720 PrintIfNotEmpty(inner_listener.str(), listener->stream());
2721 return !all_elements_should_match;
2722 }
2723 }
2724 return all_elements_should_match;
2725 }
2726
2727 bool MatchAndExplainImpl(const Matcher<size_t>& count_matcher,
2728 Container container,
2729 MatchResultListener* listener) const {
2730 StlContainerReference stl_container = View::ConstReference(container);
2731 size_t i = 0;
2732 std::vector<size_t> match_elements;
2733 for (auto it = stl_container.begin(); it != stl_container.end();
2734 ++it, ++i) {
2735 StringMatchResultListener inner_listener;
2736 const bool matches = inner_matcher_.MatchAndExplain(*it, &inner_listener);
2737 if (matches) {
2738 match_elements.push_back(i);
2739 }
2740 }
2741 if (listener->IsInterested()) {
2742 if (match_elements.empty()) {
2743 *listener << "has no element that matches";
2744 } else if (match_elements.size() == 1) {
2745 *listener << "whose element #" << match_elements[0] << " matches";
2746 } else {
2747 *listener << "whose elements (";
2748 std::string sep = "";
2749 for (size_t e : match_elements) {
2750 *listener << sep << e;
2751 sep = ", ";
2752 }
2753 *listener << ") match";
2754 }
2755 }
2756 StringMatchResultListener count_listener;
2757 if (count_matcher.MatchAndExplain(match_elements.size(), &count_listener)) {
2758 *listener << " and whose match quantity of " << match_elements.size()
2759 << " matches";
2760 PrintIfNotEmpty(count_listener.str(), listener->stream());
2761 return true;
2762 } else {
2763 if (match_elements.empty()) {
2764 *listener << " and";
2765 } else {
2766 *listener << " but";
2767 }
2768 *listener << " whose match quantity of " << match_elements.size()
2769 << " does not match";
2770 PrintIfNotEmpty(count_listener.str(), listener->stream());
2771 return false;
2772 }
2773 }
2774
2775 protected:
2776 const Matcher<const Element&> inner_matcher_;
2777 };
2778
2779
2780
2781 template <typename Container>
2782 class ContainsMatcherImpl : public QuantifierMatcherImpl<Container> {
2783 public:
2784 template <typename InnerMatcher>
2785 explicit ContainsMatcherImpl(InnerMatcher inner_matcher)
2786 : QuantifierMatcherImpl<Container>(inner_matcher) {}
2787
2788
2789 void DescribeTo(::std::ostream* os) const override {
2790 *os << "contains at least one element that ";
2791 this->inner_matcher_.DescribeTo(os);
2792 }
2793
2794 void DescribeNegationTo(::std::ostream* os) const override {
2795 *os << "doesn't contain any element that ";
2796 this->inner_matcher_.DescribeTo(os);
2797 }
2798
2799 bool MatchAndExplain(Container container,
2800 MatchResultListener* listener) const override {
2801 return this->MatchAndExplainImpl(false, container, listener);
2802 }
2803 };
2804
2805
2806
2807 template <typename Container>
2808 class EachMatcherImpl : public QuantifierMatcherImpl<Container> {
2809 public:
2810 template <typename InnerMatcher>
2811 explicit EachMatcherImpl(InnerMatcher inner_matcher)
2812 : QuantifierMatcherImpl<Container>(inner_matcher) {}
2813
2814
2815 void DescribeTo(::std::ostream* os) const override {
2816 *os << "only contains elements that ";
2817 this->inner_matcher_.DescribeTo(os);
2818 }
2819
2820 void DescribeNegationTo(::std::ostream* os) const override {
2821 *os << "contains some element that ";
2822 this->inner_matcher_.DescribeNegationTo(os);
2823 }
2824
2825 bool MatchAndExplain(Container container,
2826 MatchResultListener* listener) const override {
2827 return this->MatchAndExplainImpl(true, container, listener);
2828 }
2829 };
2830
2831
2832
2833 template <typename Container>
2834 class ContainsTimesMatcherImpl : public QuantifierMatcherImpl<Container> {
2835 public:
2836 template <typename InnerMatcher>
2837 explicit ContainsTimesMatcherImpl(InnerMatcher inner_matcher,
2838 Matcher<size_t> count_matcher)
2839 : QuantifierMatcherImpl<Container>(inner_matcher),
2840 count_matcher_(std::move(count_matcher)) {}
2841
2842 void DescribeTo(::std::ostream* os) const override {
2843 *os << "quantity of elements that match ";
2844 this->inner_matcher_.DescribeTo(os);
2845 *os << " ";
2846 count_matcher_.DescribeTo(os);
2847 }
2848
2849 void DescribeNegationTo(::std::ostream* os) const override {
2850 *os << "quantity of elements that match ";
2851 this->inner_matcher_.DescribeTo(os);
2852 *os << " ";
2853 count_matcher_.DescribeNegationTo(os);
2854 }
2855
2856 bool MatchAndExplain(Container container,
2857 MatchResultListener* listener) const override {
2858 return this->MatchAndExplainImpl(count_matcher_, container, listener);
2859 }
2860
2861 private:
2862 const Matcher<size_t> count_matcher_;
2863 };
2864
2865
2866 template <typename M>
2867 class ContainsTimesMatcher {
2868 public:
2869 explicit ContainsTimesMatcher(M m, Matcher<size_t> count_matcher)
2870 : inner_matcher_(m), count_matcher_(std::move(count_matcher)) {}
2871
2872 template <typename Container>
2873 operator Matcher<Container>() const {
2874 return Matcher<Container>(new ContainsTimesMatcherImpl<const Container&>(
2875 inner_matcher_, count_matcher_));
2876 }
2877
2878 private:
2879 const M inner_matcher_;
2880 const Matcher<size_t> count_matcher_;
2881 };
2882
2883
2884 template <typename M>
2885 class ContainsMatcher {
2886 public:
2887 explicit ContainsMatcher(M m) : inner_matcher_(m) {}
2888
2889 template <typename Container>
2890 operator Matcher<Container>() const {
2891 return Matcher<Container>(
2892 new ContainsMatcherImpl<const Container&>(inner_matcher_));
2893 }
2894
2895 ContainsTimesMatcher<M> Times(Matcher<size_t> count_matcher) const {
2896 return ContainsTimesMatcher<M>(inner_matcher_, std::move(count_matcher));
2897 }
2898
2899 private:
2900 const M inner_matcher_;
2901 };
2902
2903
2904 template <typename M>
2905 class EachMatcher {
2906 public:
2907 explicit EachMatcher(M m) : inner_matcher_(m) {}
2908
2909 template <typename Container>
2910 operator Matcher<Container>() const {
2911 return Matcher<Container>(
2912 new EachMatcherImpl<const Container&>(inner_matcher_));
2913 }
2914
2915 private:
2916 const M inner_matcher_;
2917 };
2918
2919 struct Rank1 {};
2920 struct Rank0 : Rank1 {};
2921
2922 namespace pair_getters {
2923 using std::get;
2924 template <typename T>
2925 auto First(T& x, Rank1) -> decltype(get<0>(x)) {
2926 return get<0>(x);
2927 }
2928 template <typename T>
2929 auto First(T& x, Rank0) -> decltype((x.first)) {
2930 return x.first;
2931 }
2932
2933 template <typename T>
2934 auto Second(T& x, Rank1) -> decltype(get<1>(x)) {
2935 return get<1>(x);
2936 }
2937 template <typename T>
2938 auto Second(T& x, Rank0) -> decltype((x.second)) {
2939 return x.second;
2940 }
2941 }
2942
2943
2944
2945
2946
2947 template <typename PairType>
2948 class KeyMatcherImpl : public MatcherInterface<PairType> {
2949 public:
2950 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(PairType) RawPairType;
2951 typedef typename RawPairType::first_type KeyType;
2952
2953 template <typename InnerMatcher>
2954 explicit KeyMatcherImpl(InnerMatcher inner_matcher)
2955 : inner_matcher_(
2956 testing::SafeMatcherCast<const KeyType&>(inner_matcher)) {}
2957
2958
2959
2960 bool MatchAndExplain(PairType key_value,
2961 MatchResultListener* listener) const override {
2962 StringMatchResultListener inner_listener;
2963 const bool match = inner_matcher_.MatchAndExplain(
2964 pair_getters::First(key_value, Rank0()), &inner_listener);
2965 const std::string explanation = inner_listener.str();
2966 if (explanation != "") {
2967 *listener << "whose first field is a value " << explanation;
2968 }
2969 return match;
2970 }
2971
2972
2973 void DescribeTo(::std::ostream* os) const override {
2974 *os << "has a key that ";
2975 inner_matcher_.DescribeTo(os);
2976 }
2977
2978
2979 void DescribeNegationTo(::std::ostream* os) const override {
2980 *os << "doesn't have a key that ";
2981 inner_matcher_.DescribeTo(os);
2982 }
2983
2984 private:
2985 const Matcher<const KeyType&> inner_matcher_;
2986 };
2987
2988
2989 template <typename M>
2990 class KeyMatcher {
2991 public:
2992 explicit KeyMatcher(M m) : matcher_for_key_(m) {}
2993
2994 template <typename PairType>
2995 operator Matcher<PairType>() const {
2996 return Matcher<PairType>(
2997 new KeyMatcherImpl<const PairType&>(matcher_for_key_));
2998 }
2999
3000 private:
3001 const M matcher_for_key_;
3002 };
3003
3004
3005 template <typename InnerMatcher>
3006 class AddressMatcher {
3007 public:
3008 explicit AddressMatcher(InnerMatcher m) : matcher_(m) {}
3009
3010 template <typename Type>
3011 operator Matcher<Type>() const {
3012 return Matcher<Type>(new Impl<const Type&>(matcher_));
3013 }
3014
3015 private:
3016
3017 template <typename Type>
3018 class Impl : public MatcherInterface<Type> {
3019 public:
3020 using Address = const GTEST_REMOVE_REFERENCE_AND_CONST_(Type) *;
3021 explicit Impl(const InnerMatcher& matcher)
3022 : matcher_(MatcherCast<Address>(matcher)) {}
3023
3024 void DescribeTo(::std::ostream* os) const override {
3025 *os << "has address that ";
3026 matcher_.DescribeTo(os);
3027 }
3028
3029 void DescribeNegationTo(::std::ostream* os) const override {
3030 *os << "does not have address that ";
3031 matcher_.DescribeTo(os);
3032 }
3033
3034 bool MatchAndExplain(Type object,
3035 MatchResultListener* listener) const override {
3036 *listener << "which has address ";
3037 Address address = std::addressof(object);
3038 return MatchPrintAndExplain(address, matcher_, listener);
3039 }
3040
3041 private:
3042 const Matcher<Address> matcher_;
3043 };
3044 const InnerMatcher matcher_;
3045 };
3046
3047
3048
3049 template <typename PairType>
3050 class PairMatcherImpl : public MatcherInterface<PairType> {
3051 public:
3052 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(PairType) RawPairType;
3053 typedef typename RawPairType::first_type FirstType;
3054 typedef typename RawPairType::second_type SecondType;
3055
3056 template <typename FirstMatcher, typename SecondMatcher>
3057 PairMatcherImpl(FirstMatcher first_matcher, SecondMatcher second_matcher)
3058 : first_matcher_(
3059 testing::SafeMatcherCast<const FirstType&>(first_matcher)),
3060 second_matcher_(
3061 testing::SafeMatcherCast<const SecondType&>(second_matcher)) {}
3062
3063
3064 void DescribeTo(::std::ostream* os) const override {
3065 *os << "has a first field that ";
3066 first_matcher_.DescribeTo(os);
3067 *os << ", and has a second field that ";
3068 second_matcher_.DescribeTo(os);
3069 }
3070
3071
3072 void DescribeNegationTo(::std::ostream* os) const override {
3073 *os << "has a first field that ";
3074 first_matcher_.DescribeNegationTo(os);
3075 *os << ", or has a second field that ";
3076 second_matcher_.DescribeNegationTo(os);
3077 }
3078
3079
3080
3081 bool MatchAndExplain(PairType a_pair,
3082 MatchResultListener* listener) const override {
3083 if (!listener->IsInterested()) {
3084
3085
3086 return first_matcher_.Matches(pair_getters::First(a_pair, Rank0())) &&
3087 second_matcher_.Matches(pair_getters::Second(a_pair, Rank0()));
3088 }
3089 StringMatchResultListener first_inner_listener;
3090 if (!first_matcher_.MatchAndExplain(pair_getters::First(a_pair, Rank0()),
3091 &first_inner_listener)) {
3092 *listener << "whose first field does not match";
3093 PrintIfNotEmpty(first_inner_listener.str(), listener->stream());
3094 return false;
3095 }
3096 StringMatchResultListener second_inner_listener;
3097 if (!second_matcher_.MatchAndExplain(pair_getters::Second(a_pair, Rank0()),
3098 &second_inner_listener)) {
3099 *listener << "whose second field does not match";
3100 PrintIfNotEmpty(second_inner_listener.str(), listener->stream());
3101 return false;
3102 }
3103 ExplainSuccess(first_inner_listener.str(), second_inner_listener.str(),
3104 listener);
3105 return true;
3106 }
3107
3108 private:
3109 void ExplainSuccess(const std::string& first_explanation,
3110 const std::string& second_explanation,
3111 MatchResultListener* listener) const {
3112 *listener << "whose both fields match";
3113 if (first_explanation != "") {
3114 *listener << ", where the first field is a value " << first_explanation;
3115 }
3116 if (second_explanation != "") {
3117 *listener << ", ";
3118 if (first_explanation != "") {
3119 *listener << "and ";
3120 } else {
3121 *listener << "where ";
3122 }
3123 *listener << "the second field is a value " << second_explanation;
3124 }
3125 }
3126
3127 const Matcher<const FirstType&> first_matcher_;
3128 const Matcher<const SecondType&> second_matcher_;
3129 };
3130
3131
3132 template <typename FirstMatcher, typename SecondMatcher>
3133 class PairMatcher {
3134 public:
3135 PairMatcher(FirstMatcher first_matcher, SecondMatcher second_matcher)
3136 : first_matcher_(first_matcher), second_matcher_(second_matcher) {}
3137
3138 template <typename PairType>
3139 operator Matcher<PairType>() const {
3140 return Matcher<PairType>(
3141 new PairMatcherImpl<const PairType&>(first_matcher_, second_matcher_));
3142 }
3143
3144 private:
3145 const FirstMatcher first_matcher_;
3146 const SecondMatcher second_matcher_;
3147 };
3148
3149 template <typename T, size_t... I>
3150 auto UnpackStructImpl(const T& t, IndexSequence<I...>, int)
3151 -> decltype(std::tie(get<I>(t)...)) {
3152 static_assert(std::tuple_size<T>::value == sizeof...(I),
3153 "Number of arguments doesn't match the number of fields.");
3154 return std::tie(get<I>(t)...);
3155 }
3156
3157 #if defined(__cpp_structured_bindings) && __cpp_structured_bindings >= 201606
3158 template <typename T>
3159 auto UnpackStructImpl(const T& t, MakeIndexSequence<1>, char) {
3160 const auto& [a] = t;
3161 return std::tie(a);
3162 }
3163 template <typename T>
3164 auto UnpackStructImpl(const T& t, MakeIndexSequence<2>, char) {
3165 const auto& [a, b] = t;
3166 return std::tie(a, b);
3167 }
3168 template <typename T>
3169 auto UnpackStructImpl(const T& t, MakeIndexSequence<3>, char) {
3170 const auto& [a, b, c] = t;
3171 return std::tie(a, b, c);
3172 }
3173 template <typename T>
3174 auto UnpackStructImpl(const T& t, MakeIndexSequence<4>, char) {
3175 const auto& [a, b, c, d] = t;
3176 return std::tie(a, b, c, d);
3177 }
3178 template <typename T>
3179 auto UnpackStructImpl(const T& t, MakeIndexSequence<5>, char) {
3180 const auto& [a, b, c, d, e] = t;
3181 return std::tie(a, b, c, d, e);
3182 }
3183 template <typename T>
3184 auto UnpackStructImpl(const T& t, MakeIndexSequence<6>, char) {
3185 const auto& [a, b, c, d, e, f] = t;
3186 return std::tie(a, b, c, d, e, f);
3187 }
3188 template <typename T>
3189 auto UnpackStructImpl(const T& t, MakeIndexSequence<7>, char) {
3190 const auto& [a, b, c, d, e, f, g] = t;
3191 return std::tie(a, b, c, d, e, f, g);
3192 }
3193 template <typename T>
3194 auto UnpackStructImpl(const T& t, MakeIndexSequence<8>, char) {
3195 const auto& [a, b, c, d, e, f, g, h] = t;
3196 return std::tie(a, b, c, d, e, f, g, h);
3197 }
3198 template <typename T>
3199 auto UnpackStructImpl(const T& t, MakeIndexSequence<9>, char) {
3200 const auto& [a, b, c, d, e, f, g, h, i] = t;
3201 return std::tie(a, b, c, d, e, f, g, h, i);
3202 }
3203 template <typename T>
3204 auto UnpackStructImpl(const T& t, MakeIndexSequence<10>, char) {
3205 const auto& [a, b, c, d, e, f, g, h, i, j] = t;
3206 return std::tie(a, b, c, d, e, f, g, h, i, j);
3207 }
3208 template <typename T>
3209 auto UnpackStructImpl(const T& t, MakeIndexSequence<11>, char) {
3210 const auto& [a, b, c, d, e, f, g, h, i, j, k] = t;
3211 return std::tie(a, b, c, d, e, f, g, h, i, j, k);
3212 }
3213 template <typename T>
3214 auto UnpackStructImpl(const T& t, MakeIndexSequence<12>, char) {
3215 const auto& [a, b, c, d, e, f, g, h, i, j, k, l] = t;
3216 return std::tie(a, b, c, d, e, f, g, h, i, j, k, l);
3217 }
3218 template <typename T>
3219 auto UnpackStructImpl(const T& t, MakeIndexSequence<13>, char) {
3220 const auto& [a, b, c, d, e, f, g, h, i, j, k, l, m] = t;
3221 return std::tie(a, b, c, d, e, f, g, h, i, j, k, l, m);
3222 }
3223 template <typename T>
3224 auto UnpackStructImpl(const T& t, MakeIndexSequence<14>, char) {
3225 const auto& [a, b, c, d, e, f, g, h, i, j, k, l, m, n] = t;
3226 return std::tie(a, b, c, d, e, f, g, h, i, j, k, l, m, n);
3227 }
3228 template <typename T>
3229 auto UnpackStructImpl(const T& t, MakeIndexSequence<15>, char) {
3230 const auto& [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o] = t;
3231 return std::tie(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o);
3232 }
3233 template <typename T>
3234 auto UnpackStructImpl(const T& t, MakeIndexSequence<16>, char) {
3235 const auto& [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p] = t;
3236 return std::tie(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p);
3237 }
3238 #endif
3239
3240 template <size_t I, typename T>
3241 auto UnpackStruct(const T& t)
3242 -> decltype((UnpackStructImpl)(t, MakeIndexSequence<I>{}, 0)) {
3243 return (UnpackStructImpl)(t, MakeIndexSequence<I>{}, 0);
3244 }
3245
3246
3247
3248
3249 template <typename T, size_t N>
3250 void VariadicExpand(const T (&)[N]) {}
3251
3252 template <typename Struct, typename StructSize>
3253 class FieldsAreMatcherImpl;
3254
3255 template <typename Struct, size_t... I>
3256 class FieldsAreMatcherImpl<Struct, IndexSequence<I...>>
3257 : public MatcherInterface<Struct> {
3258 using UnpackedType =
3259 decltype(UnpackStruct<sizeof...(I)>(std::declval<const Struct&>()));
3260 using MatchersType = std::tuple<
3261 Matcher<const typename std::tuple_element<I, UnpackedType>::type&>...>;
3262
3263 public:
3264 template <typename Inner>
3265 explicit FieldsAreMatcherImpl(const Inner& matchers)
3266 : matchers_(testing::SafeMatcherCast<
3267 const typename std::tuple_element<I, UnpackedType>::type&>(
3268 std::get<I>(matchers))...) {}
3269
3270 void DescribeTo(::std::ostream* os) const override {
3271 const char* separator = "";
3272 VariadicExpand(
3273 {(*os << separator << "has field #" << I << " that ",
3274 std::get<I>(matchers_).DescribeTo(os), separator = ", and ")...});
3275 }
3276
3277 void DescribeNegationTo(::std::ostream* os) const override {
3278 const char* separator = "";
3279 VariadicExpand({(*os << separator << "has field #" << I << " that ",
3280 std::get<I>(matchers_).DescribeNegationTo(os),
3281 separator = ", or ")...});
3282 }
3283
3284 bool MatchAndExplain(Struct t, MatchResultListener* listener) const override {
3285 return MatchInternal((UnpackStruct<sizeof...(I)>)(t), listener);
3286 }
3287
3288 private:
3289 bool MatchInternal(UnpackedType tuple, MatchResultListener* listener) const {
3290 if (!listener->IsInterested()) {
3291
3292
3293 bool good = true;
3294 VariadicExpand({good = good && std::get<I>(matchers_).Matches(
3295 std::get<I>(tuple))...});
3296 return good;
3297 }
3298
3299 size_t failed_pos = ~size_t{};
3300
3301 std::vector<StringMatchResultListener> inner_listener(sizeof...(I));
3302
3303 VariadicExpand(
3304 {failed_pos == ~size_t{} && !std::get<I>(matchers_).MatchAndExplain(
3305 std::get<I>(tuple), &inner_listener[I])
3306 ? failed_pos = I
3307 : 0 ...});
3308 if (failed_pos != ~size_t{}) {
3309 *listener << "whose field #" << failed_pos << " does not match";
3310 PrintIfNotEmpty(inner_listener[failed_pos].str(), listener->stream());
3311 return false;
3312 }
3313
3314 *listener << "whose all elements match";
3315 const char* separator = ", where";
3316 for (size_t index = 0; index < sizeof...(I); ++index) {
3317 const std::string str = inner_listener[index].str();
3318 if (!str.empty()) {
3319 *listener << separator << " field #" << index << " is a value " << str;
3320 separator = ", and";
3321 }
3322 }
3323
3324 return true;
3325 }
3326
3327 MatchersType matchers_;
3328 };
3329
3330 template <typename... Inner>
3331 class FieldsAreMatcher {
3332 public:
3333 explicit FieldsAreMatcher(Inner... inner) : matchers_(std::move(inner)...) {}
3334
3335 template <typename Struct>
3336 operator Matcher<Struct>() const {
3337 return Matcher<Struct>(
3338 new FieldsAreMatcherImpl<const Struct&, IndexSequenceFor<Inner...>>(
3339 matchers_));
3340 }
3341
3342 private:
3343 std::tuple<Inner...> matchers_;
3344 };
3345
3346
3347 template <typename Container>
3348 class ElementsAreMatcherImpl : public MatcherInterface<Container> {
3349 public:
3350 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
3351 typedef internal::StlContainerView<RawContainer> View;
3352 typedef typename View::type StlContainer;
3353 typedef typename View::const_reference StlContainerReference;
3354 typedef typename StlContainer::value_type Element;
3355
3356
3357
3358 template <typename InputIter>
3359 ElementsAreMatcherImpl(InputIter first, InputIter last) {
3360 while (first != last) {
3361 matchers_.push_back(MatcherCast<const Element&>(*first++));
3362 }
3363 }
3364
3365
3366 void DescribeTo(::std::ostream* os) const override {
3367 if (count() == 0) {
3368 *os << "is empty";
3369 } else if (count() == 1) {
3370 *os << "has 1 element that ";
3371 matchers_[0].DescribeTo(os);
3372 } else {
3373 *os << "has " << Elements(count()) << " where\n";
3374 for (size_t i = 0; i != count(); ++i) {
3375 *os << "element #" << i << " ";
3376 matchers_[i].DescribeTo(os);
3377 if (i + 1 < count()) {
3378 *os << ",\n";
3379 }
3380 }
3381 }
3382 }
3383
3384
3385 void DescribeNegationTo(::std::ostream* os) const override {
3386 if (count() == 0) {
3387 *os << "isn't empty";
3388 return;
3389 }
3390
3391 *os << "doesn't have " << Elements(count()) << ", or\n";
3392 for (size_t i = 0; i != count(); ++i) {
3393 *os << "element #" << i << " ";
3394 matchers_[i].DescribeNegationTo(os);
3395 if (i + 1 < count()) {
3396 *os << ", or\n";
3397 }
3398 }
3399 }
3400
3401 bool MatchAndExplain(Container container,
3402 MatchResultListener* listener) const override {
3403
3404
3405
3406 const bool listener_interested = listener->IsInterested();
3407
3408
3409 ::std::vector<std::string> explanations(count());
3410 StlContainerReference stl_container = View::ConstReference(container);
3411 auto it = stl_container.begin();
3412 size_t exam_pos = 0;
3413 bool mismatch_found = false;
3414
3415
3416
3417
3418 for (; it != stl_container.end() && exam_pos != count(); ++it, ++exam_pos) {
3419 bool match;
3420 if (listener_interested) {
3421 StringMatchResultListener s;
3422 match = matchers_[exam_pos].MatchAndExplain(*it, &s);
3423 explanations[exam_pos] = s.str();
3424 } else {
3425 match = matchers_[exam_pos].Matches(*it);
3426 }
3427
3428 if (!match) {
3429 mismatch_found = true;
3430 break;
3431 }
3432 }
3433
3434
3435
3436
3437
3438 size_t actual_count = exam_pos;
3439 for (; it != stl_container.end(); ++it) {
3440 ++actual_count;
3441 }
3442
3443 if (actual_count != count()) {
3444
3445
3446
3447
3448 if (listener_interested && (actual_count != 0)) {
3449 *listener << "which has " << Elements(actual_count);
3450 }
3451 return false;
3452 }
3453
3454 if (mismatch_found) {
3455
3456 if (listener_interested) {
3457 *listener << "whose element #" << exam_pos << " doesn't match";
3458 PrintIfNotEmpty(explanations[exam_pos], listener->stream());
3459 }
3460 return false;
3461 }
3462
3463
3464
3465 if (listener_interested) {
3466 bool reason_printed = false;
3467 for (size_t i = 0; i != count(); ++i) {
3468 const std::string& s = explanations[i];
3469 if (!s.empty()) {
3470 if (reason_printed) {
3471 *listener << ",\nand ";
3472 }
3473 *listener << "whose element #" << i << " matches, " << s;
3474 reason_printed = true;
3475 }
3476 }
3477 }
3478 return true;
3479 }
3480
3481 private:
3482 static Message Elements(size_t count) {
3483 return Message() << count << (count == 1 ? " element" : " elements");
3484 }
3485
3486 size_t count() const { return matchers_.size(); }
3487
3488 ::std::vector<Matcher<const Element&>> matchers_;
3489 };
3490
3491
3492
3493
3494
3495 class GTEST_API_ MatchMatrix {
3496 public:
3497 MatchMatrix(size_t num_elements, size_t num_matchers)
3498 : num_elements_(num_elements),
3499 num_matchers_(num_matchers),
3500 matched_(num_elements_ * num_matchers_, 0) {}
3501
3502 size_t LhsSize() const { return num_elements_; }
3503 size_t RhsSize() const { return num_matchers_; }
3504 bool HasEdge(size_t ilhs, size_t irhs) const {
3505 return matched_[SpaceIndex(ilhs, irhs)] == 1;
3506 }
3507 void SetEdge(size_t ilhs, size_t irhs, bool b) {
3508 matched_[SpaceIndex(ilhs, irhs)] = b ? 1 : 0;
3509 }
3510
3511
3512
3513
3514 bool NextGraph();
3515
3516 void Randomize();
3517
3518 std::string DebugString() const;
3519
3520 private:
3521 size_t SpaceIndex(size_t ilhs, size_t irhs) const {
3522 return ilhs * num_matchers_ + irhs;
3523 }
3524
3525 size_t num_elements_;
3526 size_t num_matchers_;
3527
3528
3529
3530
3531 ::std::vector<char> matched_;
3532 };
3533
3534 typedef ::std::pair<size_t, size_t> ElementMatcherPair;
3535 typedef ::std::vector<ElementMatcherPair> ElementMatcherPairs;
3536
3537
3538
3539 GTEST_API_ ElementMatcherPairs FindMaxBipartiteMatching(const MatchMatrix& g);
3540
3541 struct UnorderedMatcherRequire {
3542 enum Flags {
3543 Superset = 1 << 0,
3544 Subset = 1 << 1,
3545 ExactMatch = Superset | Subset,
3546 };
3547 };
3548
3549
3550
3551
3552 class GTEST_API_ UnorderedElementsAreMatcherImplBase {
3553 protected:
3554 explicit UnorderedElementsAreMatcherImplBase(
3555 UnorderedMatcherRequire::Flags matcher_flags)
3556 : match_flags_(matcher_flags) {}
3557
3558
3559
3560
3561 typedef ::std::vector<const MatcherDescriberInterface*> MatcherDescriberVec;
3562
3563
3564 void DescribeToImpl(::std::ostream* os) const;
3565
3566
3567 void DescribeNegationToImpl(::std::ostream* os) const;
3568
3569 bool VerifyMatchMatrix(const ::std::vector<std::string>& element_printouts,
3570 const MatchMatrix& matrix,
3571 MatchResultListener* listener) const;
3572
3573 bool FindPairing(const MatchMatrix& matrix,
3574 MatchResultListener* listener) const;
3575
3576 MatcherDescriberVec& matcher_describers() { return matcher_describers_; }
3577
3578 static Message Elements(size_t n) {
3579 return Message() << n << " element" << (n == 1 ? "" : "s");
3580 }
3581
3582 UnorderedMatcherRequire::Flags match_flags() const { return match_flags_; }
3583
3584 private:
3585 UnorderedMatcherRequire::Flags match_flags_;
3586 MatcherDescriberVec matcher_describers_;
3587 };
3588
3589
3590
3591 template <typename Container>
3592 class UnorderedElementsAreMatcherImpl
3593 : public MatcherInterface<Container>,
3594 public UnorderedElementsAreMatcherImplBase {
3595 public:
3596 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
3597 typedef internal::StlContainerView<RawContainer> View;
3598 typedef typename View::type StlContainer;
3599 typedef typename View::const_reference StlContainerReference;
3600 typedef typename StlContainer::value_type Element;
3601
3602 template <typename InputIter>
3603 UnorderedElementsAreMatcherImpl(UnorderedMatcherRequire::Flags matcher_flags,
3604 InputIter first, InputIter last)
3605 : UnorderedElementsAreMatcherImplBase(matcher_flags) {
3606 for (; first != last; ++first) {
3607 matchers_.push_back(MatcherCast<const Element&>(*first));
3608 }
3609 for (const auto& m : matchers_) {
3610 matcher_describers().push_back(m.GetDescriber());
3611 }
3612 }
3613
3614
3615 void DescribeTo(::std::ostream* os) const override {
3616 return UnorderedElementsAreMatcherImplBase::DescribeToImpl(os);
3617 }
3618
3619
3620 void DescribeNegationTo(::std::ostream* os) const override {
3621 return UnorderedElementsAreMatcherImplBase::DescribeNegationToImpl(os);
3622 }
3623
3624 bool MatchAndExplain(Container container,
3625 MatchResultListener* listener) const override {
3626 StlContainerReference stl_container = View::ConstReference(container);
3627 ::std::vector<std::string> element_printouts;
3628 MatchMatrix matrix =
3629 AnalyzeElements(stl_container.begin(), stl_container.end(),
3630 &element_printouts, listener);
3631
3632 if (matrix.LhsSize() == 0 && matrix.RhsSize() == 0) {
3633 return true;
3634 }
3635
3636 if (match_flags() == UnorderedMatcherRequire::ExactMatch) {
3637 if (matrix.LhsSize() != matrix.RhsSize()) {
3638
3639
3640
3641
3642 if (matrix.LhsSize() != 0 && listener->IsInterested()) {
3643 *listener << "which has " << Elements(matrix.LhsSize());
3644 }
3645 return false;
3646 }
3647 }
3648
3649 return VerifyMatchMatrix(element_printouts, matrix, listener) &&
3650 FindPairing(matrix, listener);
3651 }
3652
3653 private:
3654 template <typename ElementIter>
3655 MatchMatrix AnalyzeElements(ElementIter elem_first, ElementIter elem_last,
3656 ::std::vector<std::string>* element_printouts,
3657 MatchResultListener* listener) const {
3658 element_printouts->clear();
3659 ::std::vector<char> did_match;
3660 size_t num_elements = 0;
3661 DummyMatchResultListener dummy;
3662 for (; elem_first != elem_last; ++num_elements, ++elem_first) {
3663 if (listener->IsInterested()) {
3664 element_printouts->push_back(PrintToString(*elem_first));
3665 }
3666 for (size_t irhs = 0; irhs != matchers_.size(); ++irhs) {
3667 did_match.push_back(
3668 matchers_[irhs].MatchAndExplain(*elem_first, &dummy));
3669 }
3670 }
3671
3672 MatchMatrix matrix(num_elements, matchers_.size());
3673 ::std::vector<char>::const_iterator did_match_iter = did_match.begin();
3674 for (size_t ilhs = 0; ilhs != num_elements; ++ilhs) {
3675 for (size_t irhs = 0; irhs != matchers_.size(); ++irhs) {
3676 matrix.SetEdge(ilhs, irhs, *did_match_iter++ != 0);
3677 }
3678 }
3679 return matrix;
3680 }
3681
3682 ::std::vector<Matcher<const Element&>> matchers_;
3683 };
3684
3685
3686
3687 template <typename Target>
3688 struct CastAndAppendTransform {
3689 template <typename Arg>
3690 Matcher<Target> operator()(const Arg& a) const {
3691 return MatcherCast<Target>(a);
3692 }
3693 };
3694
3695
3696 template <typename MatcherTuple>
3697 class UnorderedElementsAreMatcher {
3698 public:
3699 explicit UnorderedElementsAreMatcher(const MatcherTuple& args)
3700 : matchers_(args) {}
3701
3702 template <typename Container>
3703 operator Matcher<Container>() const {
3704 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
3705 typedef typename internal::StlContainerView<RawContainer>::type View;
3706 typedef typename View::value_type Element;
3707 typedef ::std::vector<Matcher<const Element&>> MatcherVec;
3708 MatcherVec matchers;
3709 matchers.reserve(::std::tuple_size<MatcherTuple>::value);
3710 TransformTupleValues(CastAndAppendTransform<const Element&>(), matchers_,
3711 ::std::back_inserter(matchers));
3712 return Matcher<Container>(
3713 new UnorderedElementsAreMatcherImpl<const Container&>(
3714 UnorderedMatcherRequire::ExactMatch, matchers.begin(),
3715 matchers.end()));
3716 }
3717
3718 private:
3719 const MatcherTuple matchers_;
3720 };
3721
3722
3723 template <typename MatcherTuple>
3724 class ElementsAreMatcher {
3725 public:
3726 explicit ElementsAreMatcher(const MatcherTuple& args) : matchers_(args) {}
3727
3728 template <typename Container>
3729 operator Matcher<Container>() const {
3730 static_assert(
3731 !IsHashTable<GTEST_REMOVE_REFERENCE_AND_CONST_(Container)>::value ||
3732 ::std::tuple_size<MatcherTuple>::value < 2,
3733 "use UnorderedElementsAre with hash tables");
3734
3735 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
3736 typedef typename internal::StlContainerView<RawContainer>::type View;
3737 typedef typename View::value_type Element;
3738 typedef ::std::vector<Matcher<const Element&>> MatcherVec;
3739 MatcherVec matchers;
3740 matchers.reserve(::std::tuple_size<MatcherTuple>::value);
3741 TransformTupleValues(CastAndAppendTransform<const Element&>(), matchers_,
3742 ::std::back_inserter(matchers));
3743 return Matcher<Container>(new ElementsAreMatcherImpl<const Container&>(
3744 matchers.begin(), matchers.end()));
3745 }
3746
3747 private:
3748 const MatcherTuple matchers_;
3749 };
3750
3751
3752 template <typename T>
3753 class UnorderedElementsAreArrayMatcher {
3754 public:
3755 template <typename Iter>
3756 UnorderedElementsAreArrayMatcher(UnorderedMatcherRequire::Flags match_flags,
3757 Iter first, Iter last)
3758 : match_flags_(match_flags), matchers_(first, last) {}
3759
3760 template <typename Container>
3761 operator Matcher<Container>() const {
3762 return Matcher<Container>(
3763 new UnorderedElementsAreMatcherImpl<const Container&>(
3764 match_flags_, matchers_.begin(), matchers_.end()));
3765 }
3766
3767 private:
3768 UnorderedMatcherRequire::Flags match_flags_;
3769 ::std::vector<T> matchers_;
3770 };
3771
3772
3773 template <typename T>
3774 class ElementsAreArrayMatcher {
3775 public:
3776 template <typename Iter>
3777 ElementsAreArrayMatcher(Iter first, Iter last) : matchers_(first, last) {}
3778
3779 template <typename Container>
3780 operator Matcher<Container>() const {
3781 static_assert(
3782 !IsHashTable<GTEST_REMOVE_REFERENCE_AND_CONST_(Container)>::value,
3783 "use UnorderedElementsAreArray with hash tables");
3784
3785 return Matcher<Container>(new ElementsAreMatcherImpl<const Container&>(
3786 matchers_.begin(), matchers_.end()));
3787 }
3788
3789 private:
3790 const ::std::vector<T> matchers_;
3791 };
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802 template <typename Tuple2Matcher, typename Second>
3803 class BoundSecondMatcher {
3804 public:
3805 BoundSecondMatcher(const Tuple2Matcher& tm, const Second& second)
3806 : tuple2_matcher_(tm), second_value_(second) {}
3807
3808 BoundSecondMatcher(const BoundSecondMatcher& other) = default;
3809
3810 template <typename T>
3811 operator Matcher<T>() const {
3812 return MakeMatcher(new Impl<T>(tuple2_matcher_, second_value_));
3813 }
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823 void operator=(const BoundSecondMatcher& ) {
3824 GTEST_LOG_(FATAL) << "BoundSecondMatcher should never be assigned.";
3825 }
3826
3827 private:
3828 template <typename T>
3829 class Impl : public MatcherInterface<T> {
3830 public:
3831 typedef ::std::tuple<T, Second> ArgTuple;
3832
3833 Impl(const Tuple2Matcher& tm, const Second& second)
3834 : mono_tuple2_matcher_(SafeMatcherCast<const ArgTuple&>(tm)),
3835 second_value_(second) {}
3836
3837 void DescribeTo(::std::ostream* os) const override {
3838 *os << "and ";
3839 UniversalPrint(second_value_, os);
3840 *os << " ";
3841 mono_tuple2_matcher_.DescribeTo(os);
3842 }
3843
3844 bool MatchAndExplain(T x, MatchResultListener* listener) const override {
3845 return mono_tuple2_matcher_.MatchAndExplain(ArgTuple(x, second_value_),
3846 listener);
3847 }
3848
3849 private:
3850 const Matcher<const ArgTuple&> mono_tuple2_matcher_;
3851 const Second second_value_;
3852 };
3853
3854 const Tuple2Matcher tuple2_matcher_;
3855 const Second second_value_;
3856 };
3857
3858
3859
3860
3861
3862 template <typename Tuple2Matcher, typename Second>
3863 BoundSecondMatcher<Tuple2Matcher, Second> MatcherBindSecond(
3864 const Tuple2Matcher& tm, const Second& second) {
3865 return BoundSecondMatcher<Tuple2Matcher, Second>(tm, second);
3866 }
3867
3868
3869
3870
3871
3872
3873 GTEST_API_ std::string FormatMatcherDescription(
3874 bool negation, const char* matcher_name,
3875 const std::vector<const char*>& param_names, const Strings& param_values);
3876
3877
3878 template <typename ValueMatcher>
3879 class OptionalMatcher {
3880 public:
3881 explicit OptionalMatcher(const ValueMatcher& value_matcher)
3882 : value_matcher_(value_matcher) {}
3883
3884 template <typename Optional>
3885 operator Matcher<Optional>() const {
3886 return Matcher<Optional>(new Impl<const Optional&>(value_matcher_));
3887 }
3888
3889 template <typename Optional>
3890 class Impl : public MatcherInterface<Optional> {
3891 public:
3892 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Optional) OptionalView;
3893 typedef typename OptionalView::value_type ValueType;
3894 explicit Impl(const ValueMatcher& value_matcher)
3895 : value_matcher_(MatcherCast<ValueType>(value_matcher)) {}
3896
3897 void DescribeTo(::std::ostream* os) const override {
3898 *os << "value ";
3899 value_matcher_.DescribeTo(os);
3900 }
3901
3902 void DescribeNegationTo(::std::ostream* os) const override {
3903 *os << "value ";
3904 value_matcher_.DescribeNegationTo(os);
3905 }
3906
3907 bool MatchAndExplain(Optional optional,
3908 MatchResultListener* listener) const override {
3909 if (!optional) {
3910 *listener << "which is not engaged";
3911 return false;
3912 }
3913 const ValueType& value = *optional;
3914 StringMatchResultListener value_listener;
3915 const bool match = value_matcher_.MatchAndExplain(value, &value_listener);
3916 *listener << "whose value " << PrintToString(value)
3917 << (match ? " matches" : " doesn't match");
3918 PrintIfNotEmpty(value_listener.str(), listener->stream());
3919 return match;
3920 }
3921
3922 private:
3923 const Matcher<ValueType> value_matcher_;
3924 };
3925
3926 private:
3927 const ValueMatcher value_matcher_;
3928 };
3929
3930 namespace variant_matcher {
3931
3932 template <typename T>
3933 void holds_alternative() {}
3934 template <typename T>
3935 void get() {}
3936
3937
3938 template <typename T>
3939 class VariantMatcher {
3940 public:
3941 explicit VariantMatcher(::testing::Matcher<const T&> matcher)
3942 : matcher_(std::move(matcher)) {}
3943
3944 template <typename Variant>
3945 bool MatchAndExplain(const Variant& value,
3946 ::testing::MatchResultListener* listener) const {
3947 using std::get;
3948 if (!listener->IsInterested()) {
3949 return holds_alternative<T>(value) && matcher_.Matches(get<T>(value));
3950 }
3951
3952 if (!holds_alternative<T>(value)) {
3953 *listener << "whose value is not of type '" << GetTypeName() << "'";
3954 return false;
3955 }
3956
3957 const T& elem = get<T>(value);
3958 StringMatchResultListener elem_listener;
3959 const bool match = matcher_.MatchAndExplain(elem, &elem_listener);
3960 *listener << "whose value " << PrintToString(elem)
3961 << (match ? " matches" : " doesn't match");
3962 PrintIfNotEmpty(elem_listener.str(), listener->stream());
3963 return match;
3964 }
3965
3966 void DescribeTo(std::ostream* os) const {
3967 *os << "is a variant<> with value of type '" << GetTypeName()
3968 << "' and the value ";
3969 matcher_.DescribeTo(os);
3970 }
3971
3972 void DescribeNegationTo(std::ostream* os) const {
3973 *os << "is a variant<> with value of type other than '" << GetTypeName()
3974 << "' or the value ";
3975 matcher_.DescribeNegationTo(os);
3976 }
3977
3978 private:
3979 static std::string GetTypeName() {
3980 #if GTEST_HAS_RTTI
3981 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(
3982 return internal::GetTypeName<T>());
3983 #endif
3984 return "the element type";
3985 }
3986
3987 const ::testing::Matcher<const T&> matcher_;
3988 };
3989
3990 }
3991
3992 namespace any_cast_matcher {
3993
3994
3995 template <typename T>
3996 void any_cast() {}
3997
3998
3999 template <typename T>
4000 class AnyCastMatcher {
4001 public:
4002 explicit AnyCastMatcher(const ::testing::Matcher<const T&>& matcher)
4003 : matcher_(matcher) {}
4004
4005 template <typename AnyType>
4006 bool MatchAndExplain(const AnyType& value,
4007 ::testing::MatchResultListener* listener) const {
4008 if (!listener->IsInterested()) {
4009 const T* ptr = any_cast<T>(&value);
4010 return ptr != nullptr && matcher_.Matches(*ptr);
4011 }
4012
4013 const T* elem = any_cast<T>(&value);
4014 if (elem == nullptr) {
4015 *listener << "whose value is not of type '" << GetTypeName() << "'";
4016 return false;
4017 }
4018
4019 StringMatchResultListener elem_listener;
4020 const bool match = matcher_.MatchAndExplain(*elem, &elem_listener);
4021 *listener << "whose value " << PrintToString(*elem)
4022 << (match ? " matches" : " doesn't match");
4023 PrintIfNotEmpty(elem_listener.str(), listener->stream());
4024 return match;
4025 }
4026
4027 void DescribeTo(std::ostream* os) const {
4028 *os << "is an 'any' type with value of type '" << GetTypeName()
4029 << "' and the value ";
4030 matcher_.DescribeTo(os);
4031 }
4032
4033 void DescribeNegationTo(std::ostream* os) const {
4034 *os << "is an 'any' type with value of type other than '" << GetTypeName()
4035 << "' or the value ";
4036 matcher_.DescribeNegationTo(os);
4037 }
4038
4039 private:
4040 static std::string GetTypeName() {
4041 #if GTEST_HAS_RTTI
4042 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(
4043 return internal::GetTypeName<T>());
4044 #endif
4045 return "the element type";
4046 }
4047
4048 const ::testing::Matcher<const T&> matcher_;
4049 };
4050
4051 }
4052
4053
4054 template <class ArgsTuple, size_t... k>
4055 class ArgsMatcherImpl : public MatcherInterface<ArgsTuple> {
4056 public:
4057 using RawArgsTuple = typename std::decay<ArgsTuple>::type;
4058 using SelectedArgs =
4059 std::tuple<typename std::tuple_element<k, RawArgsTuple>::type...>;
4060 using MonomorphicInnerMatcher = Matcher<const SelectedArgs&>;
4061
4062 template <typename InnerMatcher>
4063 explicit ArgsMatcherImpl(const InnerMatcher& inner_matcher)
4064 : inner_matcher_(SafeMatcherCast<const SelectedArgs&>(inner_matcher)) {}
4065
4066 bool MatchAndExplain(ArgsTuple args,
4067 MatchResultListener* listener) const override {
4068
4069 (void)args;
4070 const SelectedArgs& selected_args =
4071 std::forward_as_tuple(std::get<k>(args)...);
4072 if (!listener->IsInterested()) return inner_matcher_.Matches(selected_args);
4073
4074 PrintIndices(listener->stream());
4075 *listener << "are " << PrintToString(selected_args);
4076
4077 StringMatchResultListener inner_listener;
4078 const bool match =
4079 inner_matcher_.MatchAndExplain(selected_args, &inner_listener);
4080 PrintIfNotEmpty(inner_listener.str(), listener->stream());
4081 return match;
4082 }
4083
4084 void DescribeTo(::std::ostream* os) const override {
4085 *os << "are a tuple ";
4086 PrintIndices(os);
4087 inner_matcher_.DescribeTo(os);
4088 }
4089
4090 void DescribeNegationTo(::std::ostream* os) const override {
4091 *os << "are a tuple ";
4092 PrintIndices(os);
4093 inner_matcher_.DescribeNegationTo(os);
4094 }
4095
4096 private:
4097
4098 static void PrintIndices(::std::ostream* os) {
4099 *os << "whose fields (";
4100 const char* sep = "";
4101
4102 (void)sep;
4103 const char* dummy[] = {"", (*os << sep << "#" << k, sep = ", ")...};
4104 (void)dummy;
4105 *os << ") ";
4106 }
4107
4108 MonomorphicInnerMatcher inner_matcher_;
4109 };
4110
4111 template <class InnerMatcher, size_t... k>
4112 class ArgsMatcher {
4113 public:
4114 explicit ArgsMatcher(InnerMatcher inner_matcher)
4115 : inner_matcher_(std::move(inner_matcher)) {}
4116
4117 template <typename ArgsTuple>
4118 operator Matcher<ArgsTuple>() const {
4119 return MakeMatcher(new ArgsMatcherImpl<ArgsTuple, k...>(inner_matcher_));
4120 }
4121
4122 private:
4123 InnerMatcher inner_matcher_;
4124 };
4125
4126 }
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143 template <typename Iter>
4144 inline internal::ElementsAreArrayMatcher<
4145 typename ::std::iterator_traits<Iter>::value_type>
4146 ElementsAreArray(Iter first, Iter last) {
4147 typedef typename ::std::iterator_traits<Iter>::value_type T;
4148 return internal::ElementsAreArrayMatcher<T>(first, last);
4149 }
4150
4151 template <typename T>
4152 inline auto ElementsAreArray(const T* pointer, size_t count)
4153 -> decltype(ElementsAreArray(pointer, pointer + count)) {
4154 return ElementsAreArray(pointer, pointer + count);
4155 }
4156
4157 template <typename T, size_t N>
4158 inline auto ElementsAreArray(const T (&array)[N])
4159 -> decltype(ElementsAreArray(array, N)) {
4160 return ElementsAreArray(array, N);
4161 }
4162
4163 template <typename Container>
4164 inline auto ElementsAreArray(const Container& container)
4165 -> decltype(ElementsAreArray(container.begin(), container.end())) {
4166 return ElementsAreArray(container.begin(), container.end());
4167 }
4168
4169 template <typename T>
4170 inline auto ElementsAreArray(::std::initializer_list<T> xs)
4171 -> decltype(ElementsAreArray(xs.begin(), xs.end())) {
4172 return ElementsAreArray(xs.begin(), xs.end());
4173 }
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188 template <typename Iter>
4189 inline internal::UnorderedElementsAreArrayMatcher<
4190 typename ::std::iterator_traits<Iter>::value_type>
4191 UnorderedElementsAreArray(Iter first, Iter last) {
4192 typedef typename ::std::iterator_traits<Iter>::value_type T;
4193 return internal::UnorderedElementsAreArrayMatcher<T>(
4194 internal::UnorderedMatcherRequire::ExactMatch, first, last);
4195 }
4196
4197 template <typename T>
4198 inline internal::UnorderedElementsAreArrayMatcher<T> UnorderedElementsAreArray(
4199 const T* pointer, size_t count) {
4200 return UnorderedElementsAreArray(pointer, pointer + count);
4201 }
4202
4203 template <typename T, size_t N>
4204 inline internal::UnorderedElementsAreArrayMatcher<T> UnorderedElementsAreArray(
4205 const T (&array)[N]) {
4206 return UnorderedElementsAreArray(array, N);
4207 }
4208
4209 template <typename Container>
4210 inline internal::UnorderedElementsAreArrayMatcher<
4211 typename Container::value_type>
4212 UnorderedElementsAreArray(const Container& container) {
4213 return UnorderedElementsAreArray(container.begin(), container.end());
4214 }
4215
4216 template <typename T>
4217 inline internal::UnorderedElementsAreArrayMatcher<T> UnorderedElementsAreArray(
4218 ::std::initializer_list<T> xs) {
4219 return UnorderedElementsAreArray(xs.begin(), xs.end());
4220 }
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231 const internal::AnythingMatcher _ = {};
4232
4233 template <typename T>
4234 inline Matcher<T> A() {
4235 return _;
4236 }
4237
4238
4239 template <typename T>
4240 inline Matcher<T> An() {
4241 return _;
4242 }
4243
4244 template <typename T, typename M>
4245 Matcher<T> internal::MatcherCastImpl<T, M>::CastImpl(
4246 const M& value, std::false_type ,
4247 std::false_type ) {
4248 return Eq(value);
4249 }
4250
4251
4252 inline PolymorphicMatcher<internal::IsNullMatcher> IsNull() {
4253 return MakePolymorphicMatcher(internal::IsNullMatcher());
4254 }
4255
4256
4257
4258
4259 inline PolymorphicMatcher<internal::NotNullMatcher> NotNull() {
4260 return MakePolymorphicMatcher(internal::NotNullMatcher());
4261 }
4262
4263
4264
4265 template <typename T>
4266 inline internal::RefMatcher<T&> Ref(T& x) {
4267 return internal::RefMatcher<T&>(x);
4268 }
4269
4270
4271 inline PolymorphicMatcher<internal::IsNanMatcher> IsNan() {
4272 return MakePolymorphicMatcher(internal::IsNanMatcher());
4273 }
4274
4275
4276
4277 inline internal::FloatingEqMatcher<double> DoubleEq(double rhs) {
4278 return internal::FloatingEqMatcher<double>(rhs, false);
4279 }
4280
4281
4282
4283 inline internal::FloatingEqMatcher<double> NanSensitiveDoubleEq(double rhs) {
4284 return internal::FloatingEqMatcher<double>(rhs, true);
4285 }
4286
4287
4288
4289
4290 inline internal::FloatingEqMatcher<double> DoubleNear(double rhs,
4291 double max_abs_error) {
4292 return internal::FloatingEqMatcher<double>(rhs, false, max_abs_error);
4293 }
4294
4295
4296
4297
4298 inline internal::FloatingEqMatcher<double> NanSensitiveDoubleNear(
4299 double rhs, double max_abs_error) {
4300 return internal::FloatingEqMatcher<double>(rhs, true, max_abs_error);
4301 }
4302
4303
4304
4305 inline internal::FloatingEqMatcher<float> FloatEq(float rhs) {
4306 return internal::FloatingEqMatcher<float>(rhs, false);
4307 }
4308
4309
4310
4311 inline internal::FloatingEqMatcher<float> NanSensitiveFloatEq(float rhs) {
4312 return internal::FloatingEqMatcher<float>(rhs, true);
4313 }
4314
4315
4316
4317
4318 inline internal::FloatingEqMatcher<float> FloatNear(float rhs,
4319 float max_abs_error) {
4320 return internal::FloatingEqMatcher<float>(rhs, false, max_abs_error);
4321 }
4322
4323
4324
4325
4326 inline internal::FloatingEqMatcher<float> NanSensitiveFloatNear(
4327 float rhs, float max_abs_error) {
4328 return internal::FloatingEqMatcher<float>(rhs, true, max_abs_error);
4329 }
4330
4331
4332
4333 template <typename InnerMatcher>
4334 inline internal::PointeeMatcher<InnerMatcher> Pointee(
4335 const InnerMatcher& inner_matcher) {
4336 return internal::PointeeMatcher<InnerMatcher>(inner_matcher);
4337 }
4338
4339 #if GTEST_HAS_RTTI
4340
4341
4342
4343
4344
4345
4346 template <typename To>
4347 inline PolymorphicMatcher<internal::WhenDynamicCastToMatcher<To>>
4348 WhenDynamicCastTo(const Matcher<To>& inner_matcher) {
4349 return MakePolymorphicMatcher(
4350 internal::WhenDynamicCastToMatcher<To>(inner_matcher));
4351 }
4352 #endif
4353
4354
4355
4356
4357
4358 template <typename Class, typename FieldType, typename FieldMatcher>
4359 inline PolymorphicMatcher<internal::FieldMatcher<Class, FieldType>> Field(
4360 FieldType Class::*field, const FieldMatcher& matcher) {
4361 return MakePolymorphicMatcher(internal::FieldMatcher<Class, FieldType>(
4362 field, MatcherCast<const FieldType&>(matcher)));
4363
4364
4365
4366
4367 }
4368
4369
4370
4371 template <typename Class, typename FieldType, typename FieldMatcher>
4372 inline PolymorphicMatcher<internal::FieldMatcher<Class, FieldType>> Field(
4373 const std::string& field_name, FieldType Class::*field,
4374 const FieldMatcher& matcher) {
4375 return MakePolymorphicMatcher(internal::FieldMatcher<Class, FieldType>(
4376 field_name, field, MatcherCast<const FieldType&>(matcher)));
4377 }
4378
4379
4380
4381
4382
4383 template <typename Class, typename PropertyType, typename PropertyMatcher>
4384 inline PolymorphicMatcher<internal::PropertyMatcher<
4385 Class, PropertyType, PropertyType (Class::*)() const>>
4386 Property(PropertyType (Class::*property)() const,
4387 const PropertyMatcher& matcher) {
4388 return MakePolymorphicMatcher(
4389 internal::PropertyMatcher<Class, PropertyType,
4390 PropertyType (Class::*)() const>(
4391 property, MatcherCast<const PropertyType&>(matcher)));
4392
4393
4394
4395
4396 }
4397
4398
4399
4400 template <typename Class, typename PropertyType, typename PropertyMatcher>
4401 inline PolymorphicMatcher<internal::PropertyMatcher<
4402 Class, PropertyType, PropertyType (Class::*)() const>>
4403 Property(const std::string& property_name,
4404 PropertyType (Class::*property)() const,
4405 const PropertyMatcher& matcher) {
4406 return MakePolymorphicMatcher(
4407 internal::PropertyMatcher<Class, PropertyType,
4408 PropertyType (Class::*)() const>(
4409 property_name, property, MatcherCast<const PropertyType&>(matcher)));
4410 }
4411
4412
4413 template <typename Class, typename PropertyType, typename PropertyMatcher>
4414 inline PolymorphicMatcher<internal::PropertyMatcher<
4415 Class, PropertyType, PropertyType (Class::*)() const&>>
4416 Property(PropertyType (Class::*property)() const&,
4417 const PropertyMatcher& matcher) {
4418 return MakePolymorphicMatcher(
4419 internal::PropertyMatcher<Class, PropertyType,
4420 PropertyType (Class::*)() const&>(
4421 property, MatcherCast<const PropertyType&>(matcher)));
4422 }
4423
4424
4425 template <typename Class, typename PropertyType, typename PropertyMatcher>
4426 inline PolymorphicMatcher<internal::PropertyMatcher<
4427 Class, PropertyType, PropertyType (Class::*)() const&>>
4428 Property(const std::string& property_name,
4429 PropertyType (Class::*property)() const&,
4430 const PropertyMatcher& matcher) {
4431 return MakePolymorphicMatcher(
4432 internal::PropertyMatcher<Class, PropertyType,
4433 PropertyType (Class::*)() const&>(
4434 property_name, property, MatcherCast<const PropertyType&>(matcher)));
4435 }
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445 template <typename Callable, typename InnerMatcher>
4446 internal::ResultOfMatcher<Callable, InnerMatcher> ResultOf(
4447 Callable callable, InnerMatcher matcher) {
4448 return internal::ResultOfMatcher<Callable, InnerMatcher>(std::move(callable),
4449 std::move(matcher));
4450 }
4451
4452
4453
4454 template <typename Callable, typename InnerMatcher>
4455 internal::ResultOfMatcher<Callable, InnerMatcher> ResultOf(
4456 const std::string& result_description, Callable callable,
4457 InnerMatcher matcher) {
4458 return internal::ResultOfMatcher<Callable, InnerMatcher>(
4459 result_description, std::move(callable), std::move(matcher));
4460 }
4461
4462
4463
4464
4465 template <typename T = std::string>
4466 PolymorphicMatcher<internal::StrEqualityMatcher<std::string>> StrEq(
4467 const internal::StringLike<T>& str) {
4468 return MakePolymorphicMatcher(
4469 internal::StrEqualityMatcher<std::string>(std::string(str), true, true));
4470 }
4471
4472
4473 template <typename T = std::string>
4474 PolymorphicMatcher<internal::StrEqualityMatcher<std::string>> StrNe(
4475 const internal::StringLike<T>& str) {
4476 return MakePolymorphicMatcher(
4477 internal::StrEqualityMatcher<std::string>(std::string(str), false, true));
4478 }
4479
4480
4481 template <typename T = std::string>
4482 PolymorphicMatcher<internal::StrEqualityMatcher<std::string>> StrCaseEq(
4483 const internal::StringLike<T>& str) {
4484 return MakePolymorphicMatcher(
4485 internal::StrEqualityMatcher<std::string>(std::string(str), true, false));
4486 }
4487
4488
4489 template <typename T = std::string>
4490 PolymorphicMatcher<internal::StrEqualityMatcher<std::string>> StrCaseNe(
4491 const internal::StringLike<T>& str) {
4492 return MakePolymorphicMatcher(internal::StrEqualityMatcher<std::string>(
4493 std::string(str), false, false));
4494 }
4495
4496
4497
4498 template <typename T = std::string>
4499 PolymorphicMatcher<internal::HasSubstrMatcher<std::string>> HasSubstr(
4500 const internal::StringLike<T>& substring) {
4501 return MakePolymorphicMatcher(
4502 internal::HasSubstrMatcher<std::string>(std::string(substring)));
4503 }
4504
4505
4506 template <typename T = std::string>
4507 PolymorphicMatcher<internal::StartsWithMatcher<std::string>> StartsWith(
4508 const internal::StringLike<T>& prefix) {
4509 return MakePolymorphicMatcher(
4510 internal::StartsWithMatcher<std::string>(std::string(prefix)));
4511 }
4512
4513
4514 template <typename T = std::string>
4515 PolymorphicMatcher<internal::EndsWithMatcher<std::string>> EndsWith(
4516 const internal::StringLike<T>& suffix) {
4517 return MakePolymorphicMatcher(
4518 internal::EndsWithMatcher<std::string>(std::string(suffix)));
4519 }
4520
4521 #if GTEST_HAS_STD_WSTRING
4522
4523
4524
4525 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring>> StrEq(
4526 const std::wstring& str) {
4527 return MakePolymorphicMatcher(
4528 internal::StrEqualityMatcher<std::wstring>(str, true, true));
4529 }
4530
4531
4532 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring>> StrNe(
4533 const std::wstring& str) {
4534 return MakePolymorphicMatcher(
4535 internal::StrEqualityMatcher<std::wstring>(str, false, true));
4536 }
4537
4538
4539 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring>> StrCaseEq(
4540 const std::wstring& str) {
4541 return MakePolymorphicMatcher(
4542 internal::StrEqualityMatcher<std::wstring>(str, true, false));
4543 }
4544
4545
4546 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring>> StrCaseNe(
4547 const std::wstring& str) {
4548 return MakePolymorphicMatcher(
4549 internal::StrEqualityMatcher<std::wstring>(str, false, false));
4550 }
4551
4552
4553
4554 inline PolymorphicMatcher<internal::HasSubstrMatcher<std::wstring>> HasSubstr(
4555 const std::wstring& substring) {
4556 return MakePolymorphicMatcher(
4557 internal::HasSubstrMatcher<std::wstring>(substring));
4558 }
4559
4560
4561 inline PolymorphicMatcher<internal::StartsWithMatcher<std::wstring>> StartsWith(
4562 const std::wstring& prefix) {
4563 return MakePolymorphicMatcher(
4564 internal::StartsWithMatcher<std::wstring>(prefix));
4565 }
4566
4567
4568 inline PolymorphicMatcher<internal::EndsWithMatcher<std::wstring>> EndsWith(
4569 const std::wstring& suffix) {
4570 return MakePolymorphicMatcher(
4571 internal::EndsWithMatcher<std::wstring>(suffix));
4572 }
4573
4574 #endif
4575
4576
4577
4578 inline internal::Eq2Matcher Eq() { return internal::Eq2Matcher(); }
4579
4580
4581
4582 inline internal::Ge2Matcher Ge() { return internal::Ge2Matcher(); }
4583
4584
4585
4586 inline internal::Gt2Matcher Gt() { return internal::Gt2Matcher(); }
4587
4588
4589
4590 inline internal::Le2Matcher Le() { return internal::Le2Matcher(); }
4591
4592
4593
4594 inline internal::Lt2Matcher Lt() { return internal::Lt2Matcher(); }
4595
4596
4597
4598 inline internal::Ne2Matcher Ne() { return internal::Ne2Matcher(); }
4599
4600
4601
4602 inline internal::FloatingEq2Matcher<float> FloatEq() {
4603 return internal::FloatingEq2Matcher<float>();
4604 }
4605
4606
4607
4608 inline internal::FloatingEq2Matcher<double> DoubleEq() {
4609 return internal::FloatingEq2Matcher<double>();
4610 }
4611
4612
4613
4614 inline internal::FloatingEq2Matcher<float> NanSensitiveFloatEq() {
4615 return internal::FloatingEq2Matcher<float>(true);
4616 }
4617
4618
4619
4620 inline internal::FloatingEq2Matcher<double> NanSensitiveDoubleEq() {
4621 return internal::FloatingEq2Matcher<double>(true);
4622 }
4623
4624
4625
4626 inline internal::FloatingEq2Matcher<float> FloatNear(float max_abs_error) {
4627 return internal::FloatingEq2Matcher<float>(max_abs_error);
4628 }
4629
4630
4631
4632 inline internal::FloatingEq2Matcher<double> DoubleNear(double max_abs_error) {
4633 return internal::FloatingEq2Matcher<double>(max_abs_error);
4634 }
4635
4636
4637
4638
4639 inline internal::FloatingEq2Matcher<float> NanSensitiveFloatNear(
4640 float max_abs_error) {
4641 return internal::FloatingEq2Matcher<float>(max_abs_error, true);
4642 }
4643
4644
4645
4646
4647 inline internal::FloatingEq2Matcher<double> NanSensitiveDoubleNear(
4648 double max_abs_error) {
4649 return internal::FloatingEq2Matcher<double>(max_abs_error, true);
4650 }
4651
4652
4653
4654 template <typename InnerMatcher>
4655 inline internal::NotMatcher<InnerMatcher> Not(InnerMatcher m) {
4656 return internal::NotMatcher<InnerMatcher>(m);
4657 }
4658
4659
4660
4661
4662 template <typename Predicate>
4663 inline PolymorphicMatcher<internal::TrulyMatcher<Predicate>> Truly(
4664 Predicate pred) {
4665 return MakePolymorphicMatcher(internal::TrulyMatcher<Predicate>(pred));
4666 }
4667
4668
4669
4670
4671
4672
4673
4674 template <typename SizeMatcher>
4675 inline internal::SizeIsMatcher<SizeMatcher> SizeIs(
4676 const SizeMatcher& size_matcher) {
4677 return internal::SizeIsMatcher<SizeMatcher>(size_matcher);
4678 }
4679
4680
4681
4682
4683
4684
4685 template <typename DistanceMatcher>
4686 inline internal::BeginEndDistanceIsMatcher<DistanceMatcher> BeginEndDistanceIs(
4687 const DistanceMatcher& distance_matcher) {
4688 return internal::BeginEndDistanceIsMatcher<DistanceMatcher>(distance_matcher);
4689 }
4690
4691
4692
4693
4694
4695 template <typename Container>
4696 inline PolymorphicMatcher<
4697 internal::ContainerEqMatcher<typename std::remove_const<Container>::type>>
4698 ContainerEq(const Container& rhs) {
4699 return MakePolymorphicMatcher(internal::ContainerEqMatcher<Container>(rhs));
4700 }
4701
4702
4703
4704 template <typename Comparator, typename ContainerMatcher>
4705 inline internal::WhenSortedByMatcher<Comparator, ContainerMatcher> WhenSortedBy(
4706 const Comparator& comparator, const ContainerMatcher& container_matcher) {
4707 return internal::WhenSortedByMatcher<Comparator, ContainerMatcher>(
4708 comparator, container_matcher);
4709 }
4710
4711
4712
4713 template <typename ContainerMatcher>
4714 inline internal::WhenSortedByMatcher<internal::LessComparator, ContainerMatcher>
4715 WhenSorted(const ContainerMatcher& container_matcher) {
4716 return internal::WhenSortedByMatcher<internal::LessComparator,
4717 ContainerMatcher>(
4718 internal::LessComparator(), container_matcher);
4719 }
4720
4721
4722
4723
4724
4725
4726
4727 template <typename TupleMatcher, typename Container>
4728 inline internal::PointwiseMatcher<TupleMatcher,
4729 typename std::remove_const<Container>::type>
4730 Pointwise(const TupleMatcher& tuple_matcher, const Container& rhs) {
4731 return internal::PointwiseMatcher<TupleMatcher, Container>(tuple_matcher,
4732 rhs);
4733 }
4734
4735
4736 template <typename TupleMatcher, typename T>
4737 inline internal::PointwiseMatcher<TupleMatcher, std::vector<T>> Pointwise(
4738 const TupleMatcher& tuple_matcher, std::initializer_list<T> rhs) {
4739 return Pointwise(tuple_matcher, std::vector<T>(rhs));
4740 }
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753 template <typename Tuple2Matcher, typename RhsContainer>
4754 inline internal::UnorderedElementsAreArrayMatcher<
4755 typename internal::BoundSecondMatcher<
4756 Tuple2Matcher,
4757 typename internal::StlContainerView<
4758 typename std::remove_const<RhsContainer>::type>::type::value_type>>
4759 UnorderedPointwise(const Tuple2Matcher& tuple2_matcher,
4760 const RhsContainer& rhs_container) {
4761
4762
4763 typedef typename internal::StlContainerView<RhsContainer> RhsView;
4764 typedef typename RhsView::type RhsStlContainer;
4765 typedef typename RhsStlContainer::value_type Second;
4766 const RhsStlContainer& rhs_stl_container =
4767 RhsView::ConstReference(rhs_container);
4768
4769
4770 ::std::vector<internal::BoundSecondMatcher<Tuple2Matcher, Second>> matchers;
4771 for (auto it = rhs_stl_container.begin(); it != rhs_stl_container.end();
4772 ++it) {
4773 matchers.push_back(internal::MatcherBindSecond(tuple2_matcher, *it));
4774 }
4775
4776
4777 return UnorderedElementsAreArray(matchers);
4778 }
4779
4780
4781 template <typename Tuple2Matcher, typename T>
4782 inline internal::UnorderedElementsAreArrayMatcher<
4783 typename internal::BoundSecondMatcher<Tuple2Matcher, T>>
4784 UnorderedPointwise(const Tuple2Matcher& tuple2_matcher,
4785 std::initializer_list<T> rhs) {
4786 return UnorderedPointwise(tuple2_matcher, std::vector<T>(rhs));
4787 }
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820 template <typename M>
4821 inline internal::ContainsMatcher<M> Contains(M matcher) {
4822 return internal::ContainsMatcher<M>(matcher);
4823 }
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852 template <typename Iter>
4853 inline internal::UnorderedElementsAreArrayMatcher<
4854 typename ::std::iterator_traits<Iter>::value_type>
4855 IsSupersetOf(Iter first, Iter last) {
4856 typedef typename ::std::iterator_traits<Iter>::value_type T;
4857 return internal::UnorderedElementsAreArrayMatcher<T>(
4858 internal::UnorderedMatcherRequire::Superset, first, last);
4859 }
4860
4861 template <typename T>
4862 inline internal::UnorderedElementsAreArrayMatcher<T> IsSupersetOf(
4863 const T* pointer, size_t count) {
4864 return IsSupersetOf(pointer, pointer + count);
4865 }
4866
4867 template <typename T, size_t N>
4868 inline internal::UnorderedElementsAreArrayMatcher<T> IsSupersetOf(
4869 const T (&array)[N]) {
4870 return IsSupersetOf(array, N);
4871 }
4872
4873 template <typename Container>
4874 inline internal::UnorderedElementsAreArrayMatcher<
4875 typename Container::value_type>
4876 IsSupersetOf(const Container& container) {
4877 return IsSupersetOf(container.begin(), container.end());
4878 }
4879
4880 template <typename T>
4881 inline internal::UnorderedElementsAreArrayMatcher<T> IsSupersetOf(
4882 ::std::initializer_list<T> xs) {
4883 return IsSupersetOf(xs.begin(), xs.end());
4884 }
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909 template <typename Iter>
4910 inline internal::UnorderedElementsAreArrayMatcher<
4911 typename ::std::iterator_traits<Iter>::value_type>
4912 IsSubsetOf(Iter first, Iter last) {
4913 typedef typename ::std::iterator_traits<Iter>::value_type T;
4914 return internal::UnorderedElementsAreArrayMatcher<T>(
4915 internal::UnorderedMatcherRequire::Subset, first, last);
4916 }
4917
4918 template <typename T>
4919 inline internal::UnorderedElementsAreArrayMatcher<T> IsSubsetOf(
4920 const T* pointer, size_t count) {
4921 return IsSubsetOf(pointer, pointer + count);
4922 }
4923
4924 template <typename T, size_t N>
4925 inline internal::UnorderedElementsAreArrayMatcher<T> IsSubsetOf(
4926 const T (&array)[N]) {
4927 return IsSubsetOf(array, N);
4928 }
4929
4930 template <typename Container>
4931 inline internal::UnorderedElementsAreArrayMatcher<
4932 typename Container::value_type>
4933 IsSubsetOf(const Container& container) {
4934 return IsSubsetOf(container.begin(), container.end());
4935 }
4936
4937 template <typename T>
4938 inline internal::UnorderedElementsAreArrayMatcher<T> IsSubsetOf(
4939 ::std::initializer_list<T> xs) {
4940 return IsSubsetOf(xs.begin(), xs.end());
4941 }
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970 template <typename M>
4971 inline internal::EachMatcher<M> Each(M matcher) {
4972 return internal::EachMatcher<M>(matcher);
4973 }
4974
4975
4976
4977
4978 template <typename M>
4979 inline internal::KeyMatcher<M> Key(M inner_matcher) {
4980 return internal::KeyMatcher<M>(inner_matcher);
4981 }
4982
4983
4984
4985
4986
4987
4988 template <typename FirstMatcher, typename SecondMatcher>
4989 inline internal::PairMatcher<FirstMatcher, SecondMatcher> Pair(
4990 FirstMatcher first_matcher, SecondMatcher second_matcher) {
4991 return internal::PairMatcher<FirstMatcher, SecondMatcher>(first_matcher,
4992 second_matcher);
4993 }
4994
4995 namespace no_adl {
4996
4997
4998
4999
5000
5001 template <typename MatcherTrue, typename MatcherFalse>
5002 internal::ConditionalMatcher<MatcherTrue, MatcherFalse> Conditional(
5003 bool condition, MatcherTrue matcher_true, MatcherFalse matcher_false) {
5004 return internal::ConditionalMatcher<MatcherTrue, MatcherFalse>(
5005 condition, std::move(matcher_true), std::move(matcher_false));
5006 }
5007
5008
5009
5010
5011
5012 template <typename... M>
5013 internal::FieldsAreMatcher<typename std::decay<M>::type...> FieldsAre(
5014 M&&... matchers) {
5015 return internal::FieldsAreMatcher<typename std::decay<M>::type...>(
5016 std::forward<M>(matchers)...);
5017 }
5018
5019
5020
5021 template <typename InnerMatcher>
5022 inline internal::PointerMatcher<InnerMatcher> Pointer(
5023 const InnerMatcher& inner_matcher) {
5024 return internal::PointerMatcher<InnerMatcher>(inner_matcher);
5025 }
5026
5027
5028
5029 template <typename InnerMatcher>
5030 inline internal::AddressMatcher<InnerMatcher> Address(
5031 const InnerMatcher& inner_matcher) {
5032 return internal::AddressMatcher<InnerMatcher>(inner_matcher);
5033 }
5034
5035
5036
5037 template <typename MatcherType>
5038 internal::WhenBase64UnescapedMatcher WhenBase64Unescaped(
5039 const MatcherType& internal_matcher) {
5040 return internal::WhenBase64UnescapedMatcher(internal_matcher);
5041 }
5042 }
5043
5044
5045
5046 template <typename M>
5047 inline internal::MatcherAsPredicate<M> Matches(M matcher) {
5048 return internal::MatcherAsPredicate<M>(matcher);
5049 }
5050
5051
5052 template <typename T, typename M>
5053 inline bool Value(const T& value, M matcher) {
5054 return testing::Matches(matcher)(value);
5055 }
5056
5057
5058
5059 template <typename T, typename M>
5060 inline bool ExplainMatchResult(M matcher, const T& value,
5061 MatchResultListener* listener) {
5062 return SafeMatcherCast<const T&>(matcher).MatchAndExplain(value, listener);
5063 }
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076 template <typename T, typename M>
5077 std::string DescribeMatcher(const M& matcher, bool negation = false) {
5078 ::std::stringstream ss;
5079 Matcher<T> monomorphic_matcher = SafeMatcherCast<T>(matcher);
5080 if (negation) {
5081 monomorphic_matcher.DescribeNegationTo(&ss);
5082 } else {
5083 monomorphic_matcher.DescribeTo(&ss);
5084 }
5085 return ss.str();
5086 }
5087
5088 template <typename... Args>
5089 internal::ElementsAreMatcher<
5090 std::tuple<typename std::decay<const Args&>::type...>>
5091 ElementsAre(const Args&... matchers) {
5092 return internal::ElementsAreMatcher<
5093 std::tuple<typename std::decay<const Args&>::type...>>(
5094 std::make_tuple(matchers...));
5095 }
5096
5097 template <typename... Args>
5098 internal::UnorderedElementsAreMatcher<
5099 std::tuple<typename std::decay<const Args&>::type...>>
5100 UnorderedElementsAre(const Args&... matchers) {
5101 return internal::UnorderedElementsAreMatcher<
5102 std::tuple<typename std::decay<const Args&>::type...>>(
5103 std::make_tuple(matchers...));
5104 }
5105
5106
5107 template <typename... Args>
5108 internal::AllOfMatcher<typename std::decay<const Args&>::type...> AllOf(
5109 const Args&... matchers) {
5110 return internal::AllOfMatcher<typename std::decay<const Args&>::type...>(
5111 matchers...);
5112 }
5113
5114 template <typename... Args>
5115 internal::AnyOfMatcher<typename std::decay<const Args&>::type...> AnyOf(
5116 const Args&... matchers) {
5117 return internal::AnyOfMatcher<typename std::decay<const Args&>::type...>(
5118 matchers...);
5119 }
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143 template <typename Iter>
5144 inline internal::AnyOfArrayMatcher<
5145 typename ::std::iterator_traits<Iter>::value_type>
5146 AnyOfArray(Iter first, Iter last) {
5147 return internal::AnyOfArrayMatcher<
5148 typename ::std::iterator_traits<Iter>::value_type>(first, last);
5149 }
5150
5151 template <typename Iter>
5152 inline internal::AllOfArrayMatcher<
5153 typename ::std::iterator_traits<Iter>::value_type>
5154 AllOfArray(Iter first, Iter last) {
5155 return internal::AllOfArrayMatcher<
5156 typename ::std::iterator_traits<Iter>::value_type>(first, last);
5157 }
5158
5159 template <typename T>
5160 inline internal::AnyOfArrayMatcher<T> AnyOfArray(const T* ptr, size_t count) {
5161 return AnyOfArray(ptr, ptr + count);
5162 }
5163
5164 template <typename T>
5165 inline internal::AllOfArrayMatcher<T> AllOfArray(const T* ptr, size_t count) {
5166 return AllOfArray(ptr, ptr + count);
5167 }
5168
5169 template <typename T, size_t N>
5170 inline internal::AnyOfArrayMatcher<T> AnyOfArray(const T (&array)[N]) {
5171 return AnyOfArray(array, N);
5172 }
5173
5174 template <typename T, size_t N>
5175 inline internal::AllOfArrayMatcher<T> AllOfArray(const T (&array)[N]) {
5176 return AllOfArray(array, N);
5177 }
5178
5179 template <typename Container>
5180 inline internal::AnyOfArrayMatcher<typename Container::value_type> AnyOfArray(
5181 const Container& container) {
5182 return AnyOfArray(container.begin(), container.end());
5183 }
5184
5185 template <typename Container>
5186 inline internal::AllOfArrayMatcher<typename Container::value_type> AllOfArray(
5187 const Container& container) {
5188 return AllOfArray(container.begin(), container.end());
5189 }
5190
5191 template <typename T>
5192 inline internal::AnyOfArrayMatcher<T> AnyOfArray(
5193 ::std::initializer_list<T> xs) {
5194 return AnyOfArray(xs.begin(), xs.end());
5195 }
5196
5197 template <typename T>
5198 inline internal::AllOfArrayMatcher<T> AllOfArray(
5199 ::std::initializer_list<T> xs) {
5200 return AllOfArray(xs.begin(), xs.end());
5201 }
5202
5203
5204
5205
5206 template <size_t... k, typename InnerMatcher>
5207 internal::ArgsMatcher<typename std::decay<InnerMatcher>::type, k...> Args(
5208 InnerMatcher&& matcher) {
5209 return internal::ArgsMatcher<typename std::decay<InnerMatcher>::type, k...>(
5210 std::forward<InnerMatcher>(matcher));
5211 }
5212
5213
5214
5215
5216
5217
5218
5219
5220 template <typename InnerMatcher>
5221 inline InnerMatcher AllArgs(const InnerMatcher& matcher) {
5222 return matcher;
5223 }
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233 template <typename ValueMatcher>
5234 inline internal::OptionalMatcher<ValueMatcher> Optional(
5235 const ValueMatcher& value_matcher) {
5236 return internal::OptionalMatcher<ValueMatcher>(value_matcher);
5237 }
5238
5239
5240 template <typename T>
5241 PolymorphicMatcher<internal::any_cast_matcher::AnyCastMatcher<T>> AnyWith(
5242 const Matcher<const T&>& matcher) {
5243 return MakePolymorphicMatcher(
5244 internal::any_cast_matcher::AnyCastMatcher<T>(matcher));
5245 }
5246
5247
5248
5249
5250
5251 template <typename T>
5252 PolymorphicMatcher<internal::variant_matcher::VariantMatcher<T>> VariantWith(
5253 const Matcher<const T&>& matcher) {
5254 return MakePolymorphicMatcher(
5255 internal::variant_matcher::VariantMatcher<T>(matcher));
5256 }
5257
5258 #if GTEST_HAS_EXCEPTIONS
5259
5260
5261
5262 namespace internal {
5263
5264 class WithWhatMatcherImpl {
5265 public:
5266 WithWhatMatcherImpl(Matcher<std::string> matcher)
5267 : matcher_(std::move(matcher)) {}
5268
5269 void DescribeTo(std::ostream* os) const {
5270 *os << "contains .what() that ";
5271 matcher_.DescribeTo(os);
5272 }
5273
5274 void DescribeNegationTo(std::ostream* os) const {
5275 *os << "contains .what() that does not ";
5276 matcher_.DescribeTo(os);
5277 }
5278
5279 template <typename Err>
5280 bool MatchAndExplain(const Err& err, MatchResultListener* listener) const {
5281 *listener << "which contains .what() (of value = " << err.what()
5282 << ") that ";
5283 return matcher_.MatchAndExplain(err.what(), listener);
5284 }
5285
5286 private:
5287 const Matcher<std::string> matcher_;
5288 };
5289
5290 inline PolymorphicMatcher<WithWhatMatcherImpl> WithWhat(
5291 Matcher<std::string> m) {
5292 return MakePolymorphicMatcher(WithWhatMatcherImpl(std::move(m)));
5293 }
5294
5295 template <typename Err>
5296 class ExceptionMatcherImpl {
5297 class NeverThrown {
5298 public:
5299 const char* what() const noexcept {
5300 return "this exception should never be thrown";
5301 }
5302 };
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325 using DefaultExceptionType = typename std::conditional<
5326 std::is_same<typename std::remove_cv<
5327 typename std::remove_reference<Err>::type>::type,
5328 std::exception>::value,
5329 const NeverThrown&, const std::exception&>::type;
5330
5331 public:
5332 ExceptionMatcherImpl(Matcher<const Err&> matcher)
5333 : matcher_(std::move(matcher)) {}
5334
5335 void DescribeTo(std::ostream* os) const {
5336 *os << "throws an exception which is a " << GetTypeName<Err>();
5337 *os << " which ";
5338 matcher_.DescribeTo(os);
5339 }
5340
5341 void DescribeNegationTo(std::ostream* os) const {
5342 *os << "throws an exception which is not a " << GetTypeName<Err>();
5343 *os << " which ";
5344 matcher_.DescribeNegationTo(os);
5345 }
5346
5347 template <typename T>
5348 bool MatchAndExplain(T&& x, MatchResultListener* listener) const {
5349 try {
5350 (void)(std::forward<T>(x)());
5351 } catch (const Err& err) {
5352 *listener << "throws an exception which is a " << GetTypeName<Err>();
5353 *listener << " ";
5354 return matcher_.MatchAndExplain(err, listener);
5355 } catch (DefaultExceptionType err) {
5356 #if GTEST_HAS_RTTI
5357 *listener << "throws an exception of type " << GetTypeName(typeid(err));
5358 *listener << " ";
5359 #else
5360 *listener << "throws an std::exception-derived type ";
5361 #endif
5362 *listener << "with description \"" << err.what() << "\"";
5363 return false;
5364 } catch (...) {
5365 *listener << "throws an exception of an unknown type";
5366 return false;
5367 }
5368
5369 *listener << "does not throw any exception";
5370 return false;
5371 }
5372
5373 private:
5374 const Matcher<const Err&> matcher_;
5375 };
5376
5377 }
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401 template <typename Err>
5402 PolymorphicMatcher<internal::ExceptionMatcherImpl<Err>> Throws() {
5403 return MakePolymorphicMatcher(
5404 internal::ExceptionMatcherImpl<Err>(A<const Err&>()));
5405 }
5406
5407 template <typename Err, typename ExceptionMatcher>
5408 PolymorphicMatcher<internal::ExceptionMatcherImpl<Err>> Throws(
5409 const ExceptionMatcher& exception_matcher) {
5410
5411
5412
5413 return MakePolymorphicMatcher(internal::ExceptionMatcherImpl<Err>(
5414 SafeMatcherCast<const Err&>(exception_matcher)));
5415 }
5416
5417 template <typename Err, typename MessageMatcher>
5418 PolymorphicMatcher<internal::ExceptionMatcherImpl<Err>> ThrowsMessage(
5419 MessageMatcher&& message_matcher) {
5420 static_assert(std::is_base_of<std::exception, Err>::value,
5421 "expected an std::exception-derived type");
5422 return Throws<Err>(internal::WithWhat(
5423 MatcherCast<std::string>(std::forward<MessageMatcher>(message_matcher))));
5424 }
5425
5426 #endif
5427
5428
5429
5430
5431
5432 #define ASSERT_THAT(value, matcher) \
5433 ASSERT_PRED_FORMAT1( \
5434 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
5435 #define EXPECT_THAT(value, matcher) \
5436 EXPECT_PRED_FORMAT1( \
5437 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
5438
5439
5440 #define MATCHER(name, description) \
5441 class name##Matcher \
5442 : public ::testing::internal::MatcherBaseImpl<name##Matcher> { \
5443 public: \
5444 template <typename arg_type> \
5445 class gmock_Impl : public ::testing::MatcherInterface<const arg_type&> { \
5446 public: \
5447 gmock_Impl() {} \
5448 bool MatchAndExplain( \
5449 const arg_type& arg, \
5450 ::testing::MatchResultListener* result_listener) const override; \
5451 void DescribeTo(::std::ostream* gmock_os) const override { \
5452 *gmock_os << FormatDescription(false); \
5453 } \
5454 void DescribeNegationTo(::std::ostream* gmock_os) const override { \
5455 *gmock_os << FormatDescription(true); \
5456 } \
5457 \
5458 private: \
5459 ::std::string FormatDescription(bool negation) const { \
5460 \
5461 ::std::string gmock_description = (description); \
5462 if (!gmock_description.empty()) { \
5463 return gmock_description; \
5464 } \
5465 return ::testing::internal::FormatMatcherDescription(negation, #name, \
5466 {}, {}); \
5467 } \
5468 }; \
5469 }; \
5470 GTEST_ATTRIBUTE_UNUSED_ inline name##Matcher name() { return {}; } \
5471 template <typename arg_type> \
5472 bool name##Matcher::gmock_Impl<arg_type>::MatchAndExplain( \
5473 const arg_type& arg, \
5474 ::testing::MatchResultListener* result_listener GTEST_ATTRIBUTE_UNUSED_) \
5475 const
5476
5477 #define MATCHER_P(name, p0, description) \
5478 GMOCK_INTERNAL_MATCHER(name, name##MatcherP, description, (#p0), (p0))
5479 #define MATCHER_P2(name, p0, p1, description) \
5480 GMOCK_INTERNAL_MATCHER(name, name##MatcherP2, description, (#p0, #p1), \
5481 (p0, p1))
5482 #define MATCHER_P3(name, p0, p1, p2, description) \
5483 GMOCK_INTERNAL_MATCHER(name, name##MatcherP3, description, (#p0, #p1, #p2), \
5484 (p0, p1, p2))
5485 #define MATCHER_P4(name, p0, p1, p2, p3, description) \
5486 GMOCK_INTERNAL_MATCHER(name, name##MatcherP4, description, \
5487 (#p0, #p1, #p2, #p3), (p0, p1, p2, p3))
5488 #define MATCHER_P5(name, p0, p1, p2, p3, p4, description) \
5489 GMOCK_INTERNAL_MATCHER(name, name##MatcherP5, description, \
5490 (#p0, #p1, #p2, #p3, #p4), (p0, p1, p2, p3, p4))
5491 #define MATCHER_P6(name, p0, p1, p2, p3, p4, p5, description) \
5492 GMOCK_INTERNAL_MATCHER(name, name##MatcherP6, description, \
5493 (#p0, #p1, #p2, #p3, #p4, #p5), \
5494 (p0, p1, p2, p3, p4, p5))
5495 #define MATCHER_P7(name, p0, p1, p2, p3, p4, p5, p6, description) \
5496 GMOCK_INTERNAL_MATCHER(name, name##MatcherP7, description, \
5497 (#p0, #p1, #p2, #p3, #p4, #p5, #p6), \
5498 (p0, p1, p2, p3, p4, p5, p6))
5499 #define MATCHER_P8(name, p0, p1, p2, p3, p4, p5, p6, p7, description) \
5500 GMOCK_INTERNAL_MATCHER(name, name##MatcherP8, description, \
5501 (#p0, #p1, #p2, #p3, #p4, #p5, #p6, #p7), \
5502 (p0, p1, p2, p3, p4, p5, p6, p7))
5503 #define MATCHER_P9(name, p0, p1, p2, p3, p4, p5, p6, p7, p8, description) \
5504 GMOCK_INTERNAL_MATCHER(name, name##MatcherP9, description, \
5505 (#p0, #p1, #p2, #p3, #p4, #p5, #p6, #p7, #p8), \
5506 (p0, p1, p2, p3, p4, p5, p6, p7, p8))
5507 #define MATCHER_P10(name, p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, description) \
5508 GMOCK_INTERNAL_MATCHER(name, name##MatcherP10, description, \
5509 (#p0, #p1, #p2, #p3, #p4, #p5, #p6, #p7, #p8, #p9), \
5510 (p0, p1, p2, p3, p4, p5, p6, p7, p8, p9))
5511
5512 #define GMOCK_INTERNAL_MATCHER(name, full_name, description, arg_names, args) \
5513 template <GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args)> \
5514 class full_name : public ::testing::internal::MatcherBaseImpl< \
5515 full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>> { \
5516 public: \
5517 using full_name::MatcherBaseImpl::MatcherBaseImpl; \
5518 template <typename arg_type> \
5519 class gmock_Impl : public ::testing::MatcherInterface<const arg_type&> { \
5520 public: \
5521 explicit gmock_Impl(GMOCK_INTERNAL_MATCHER_FUNCTION_ARGS(args)) \
5522 : GMOCK_INTERNAL_MATCHER_FORWARD_ARGS(args) {} \
5523 bool MatchAndExplain( \
5524 const arg_type& arg, \
5525 ::testing::MatchResultListener* result_listener) const override; \
5526 void DescribeTo(::std::ostream* gmock_os) const override { \
5527 *gmock_os << FormatDescription(false); \
5528 } \
5529 void DescribeNegationTo(::std::ostream* gmock_os) const override { \
5530 *gmock_os << FormatDescription(true); \
5531 } \
5532 GMOCK_INTERNAL_MATCHER_MEMBERS(args) \
5533 \
5534 private: \
5535 ::std::string FormatDescription(bool negation) const { \
5536 ::std::string gmock_description = (description); \
5537 if (!gmock_description.empty()) { \
5538 return gmock_description; \
5539 } \
5540 return ::testing::internal::FormatMatcherDescription( \
5541 negation, #name, {GMOCK_PP_REMOVE_PARENS(arg_names)}, \
5542 ::testing::internal::UniversalTersePrintTupleFieldsToStrings( \
5543 ::std::tuple<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>( \
5544 GMOCK_INTERNAL_MATCHER_MEMBERS_USAGE(args)))); \
5545 } \
5546 }; \
5547 }; \
5548 template <GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args)> \
5549 inline full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)> name( \
5550 GMOCK_INTERNAL_MATCHER_FUNCTION_ARGS(args)) { \
5551 return full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>( \
5552 GMOCK_INTERNAL_MATCHER_ARGS_USAGE(args)); \
5553 } \
5554 template <GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args)> \
5555 template <typename arg_type> \
5556 bool full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>::gmock_Impl< \
5557 arg_type>::MatchAndExplain(const arg_type& arg, \
5558 ::testing::MatchResultListener* \
5559 result_listener GTEST_ATTRIBUTE_UNUSED_) \
5560 const
5561
5562 #define GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args) \
5563 GMOCK_PP_TAIL( \
5564 GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAM, , args))
5565 #define GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAM(i_unused, data_unused, arg) \
5566 , typename arg##_type
5567
5568 #define GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args) \
5569 GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_TYPE_PARAM, , args))
5570 #define GMOCK_INTERNAL_MATCHER_TYPE_PARAM(i_unused, data_unused, arg) \
5571 , arg##_type
5572
5573 #define GMOCK_INTERNAL_MATCHER_FUNCTION_ARGS(args) \
5574 GMOCK_PP_TAIL(dummy_first GMOCK_PP_FOR_EACH( \
5575 GMOCK_INTERNAL_MATCHER_FUNCTION_ARG, , args))
5576 #define GMOCK_INTERNAL_MATCHER_FUNCTION_ARG(i, data_unused, arg) \
5577 , arg##_type gmock_p##i
5578
5579 #define GMOCK_INTERNAL_MATCHER_FORWARD_ARGS(args) \
5580 GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_FORWARD_ARG, , args))
5581 #define GMOCK_INTERNAL_MATCHER_FORWARD_ARG(i, data_unused, arg) \
5582 , arg(::std::forward<arg##_type>(gmock_p##i))
5583
5584 #define GMOCK_INTERNAL_MATCHER_MEMBERS(args) \
5585 GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_MEMBER, , args)
5586 #define GMOCK_INTERNAL_MATCHER_MEMBER(i_unused, data_unused, arg) \
5587 const arg##_type arg;
5588
5589 #define GMOCK_INTERNAL_MATCHER_MEMBERS_USAGE(args) \
5590 GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_MEMBER_USAGE, , args))
5591 #define GMOCK_INTERNAL_MATCHER_MEMBER_USAGE(i_unused, data_unused, arg) , arg
5592
5593 #define GMOCK_INTERNAL_MATCHER_ARGS_USAGE(args) \
5594 GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_ARG_USAGE, , args))
5595 #define GMOCK_INTERNAL_MATCHER_ARG_USAGE(i, data_unused, arg_unused) \
5596 , gmock_p##i
5597
5598
5599 using namespace no_adl;
5600
5601 }
5602
5603 GTEST_DISABLE_MSC_WARNINGS_POP_()
5604
5605
5606
5607
5608 #include "gmock/internal/custom/gmock-matchers.h"
5609
5610 #endif