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