288 {
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303class StringMatchResultListener : public MatchResultListener {
304 public:
305 StringMatchResultListener() : MatchResultListener(&ss_) {}
306
307
308 std::string str() const { return ss_.str(); }
309
310
311 void Clear() { ss_.str(""); }
312
313 private:
314 ::std::stringstream ss_;
315
317};
318
319
320
321namespace internal {
322
323
324
325
326
327
328
329
330
331
332
333template <typename T, typename M>
334class MatcherCastImpl {
335 public:
336 static Matcher<T> Cast(const M& polymorphic_matcher_or_value) {
337
338
339
340
341
342
343
344
345
346
347
348
349
350 return CastImpl(polymorphic_matcher_or_value,
351 std::is_convertible<M, Matcher<T>>{},
352 std::is_convertible<M, T>{});
353 }
354
355 private:
356 template <bool Ignore>
357 static Matcher<T> CastImpl(const M& polymorphic_matcher_or_value,
358 std::true_type ,
359 std::integral_constant<bool, Ignore>) {
360
361
362
363
364
365
366
367
368 return polymorphic_matcher_or_value;
369 }
370
371
372
373
374 static Matcher<T> CastImpl(
const M&
value,
375 std::false_type ,
376 std::true_type ) {
377 return Matcher<T>(ImplicitCast_<T>(
value));
378 }
379
380
381
382
383
384
385
386
387
388
389
390 static Matcher<T> CastImpl(
const M&
value,
391 std::false_type ,
392 std::false_type );
393};
394
395
396
397
398template <typename T, typename U>
399class MatcherCastImpl<T, Matcher<U> > {
400 public:
401 static Matcher<T> Cast(const Matcher<U>& source_matcher) {
402 return Matcher<T>(new Impl(source_matcher));
403 }
404
405 private:
406 class Impl : public MatcherInterface<T> {
407 public:
408 explicit Impl(const Matcher<U>& source_matcher)
409 : source_matcher_(source_matcher) {}
410
411
412 bool MatchAndExplain(T
x, MatchResultListener* listener)
const override {
413 using FromType = typename std::remove_cv<typename std::remove_pointer<
415 using ToType = typename std::remove_cv<typename std::remove_pointer<
417
418 static_assert(
419
420
425 "Can't implicitly convert from <base> to <derived>");
426
427
428
429 using CastType =
432
433 return source_matcher_.MatchAndExplain(
static_cast<CastType
>(
x),
434 listener);
435 }
436
437 void DescribeTo(::std::ostream* os) const override {
438 source_matcher_.DescribeTo(os);
439 }
440
441 void DescribeNegationTo(::std::ostream* os) const override {
442 source_matcher_.DescribeNegationTo(os);
443 }
444
445 private:
446 const Matcher<U> source_matcher_;
447 };
448};
449
450
451
452template <typename T>
453class MatcherCastImpl<T, Matcher<T> > {
454 public:
455 static Matcher<T> Cast(const Matcher<T>& matcher) { return matcher; }
456};
457
458
459template <typename Derived>
460class MatcherBaseImpl {
461 public:
462 MatcherBaseImpl() = default;
463
464 template <typename T>
465 operator ::testing::Matcher<T>() const {
466 return ::testing::Matcher<T>(new
467 typename Derived::template gmock_Impl<T>());
468 }
469};
470
471
472template <template <typename...> class Derived, typename... Ts>
473class MatcherBaseImpl<Derived<Ts...>> {
474 public:
475
476
477 template <typename E = std::enable_if<sizeof...(Ts) == 1>,
479 explicit MatcherBaseImpl(Ts... params)
480 : params_(std::forward<Ts>(params)...) {}
481 template <typename E = std::enable_if<sizeof...(Ts) != 1>,
483 MatcherBaseImpl(Ts... params)
484 : params_(std::forward<Ts>(params)...) {}
485
486 template <typename F>
487 operator ::testing::Matcher<F>() const {
489 }
490
491 private:
492 template <typename F, std::size_t... tuple_ids>
494 return ::testing::Matcher<F>(
495 new typename Derived<Ts...>::template gmock_Impl<F>(
496 std::get<tuple_ids>(params_)...));
497 }
498
499 const std::tuple<Ts...> params_;
500};
501
502}
503
504
505
506
507
508template <typename T, typename M>
509inline Matcher<T> MatcherCast(const M& matcher) {
510 return internal::MatcherCastImpl<T, M>::Cast(matcher);
511}
512
513
514
515template <typename T, typename M>
516inline Matcher<T> SafeMatcherCast(const M& polymorphic_matcher_or_value) {
517 return MatcherCast<T>(polymorphic_matcher_or_value);
518}
519
520
521
522
523
524
525
526
527
528
529template <typename T, typename U>
530inline Matcher<T> SafeMatcherCast(const Matcher<U>& matcher) {
531
533 "T must be implicitly convertible to U");
534
535
538 cannot_convert_non_reference_arg_to_reference);
539
540
546 kTIsOther || kUIsOther ||
548 conversion_of_arithmetic_types_must_be_lossless);
549 return MatcherCast<T>(matcher);
550}
551
552
553template <typename T>
554Matcher<T> A();
555
556
557
558namespace internal {
559
560
561inline void PrintIfNotEmpty(const std::string& explanation,
562 ::std::ostream* os) {
563 if (explanation != "" && os != nullptr) {
564 *os << ", " << explanation;
565 }
566}
567
568
569
570
571inline bool IsReadableTypeName(const std::string& type_name) {
572
573
574 return (type_name.length() <= 20 ||
575 type_name.find_first_of("<(") == std::string::npos);
576}
577
578
579
580
581
582
583template <typename Value, typename T>
584bool MatchPrintAndExplain(Value&
value,
const Matcher<T>& matcher,
585 MatchResultListener* listener) {
586 if (!listener->IsInterested()) {
587
588
589 return matcher.Matches(
value);
590 }
591
592 StringMatchResultListener inner_listener;
593 const bool match = matcher.MatchAndExplain(
value, &inner_listener);
594
596#if GTEST_HAS_RTTI
597 const std::string& type_name = GetTypeName<Value>();
598 if (IsReadableTypeName(type_name))
599 *listener->stream() << " (of type " << type_name << ")";
600#endif
601 PrintIfNotEmpty(inner_listener.str(), listener->stream());
602
603 return match;
604}
605
606
607
608template <size_t N>
609class TuplePrefix {
610 public:
611
612
613
614 template <typename MatcherTuple, typename ValueTuple>
615 static bool Matches(const MatcherTuple& matcher_tuple,
616 const ValueTuple& value_tuple) {
617 return TuplePrefix<N - 1>::Matches(matcher_tuple, value_tuple) &&
618 std::get<N - 1>(matcher_tuple).Matches(std::get<N - 1>(value_tuple));
619 }
620
621
622
623
624
625 template <typename MatcherTuple, typename ValueTuple>
626 static void ExplainMatchFailuresTo(const MatcherTuple& matchers,
627 const ValueTuple& values,
628 ::std::ostream* os) {
629
630 TuplePrefix<N - 1>::ExplainMatchFailuresTo(matchers, values, os);
631
632
633
634 typename std::tuple_element<N - 1, MatcherTuple>
::type matcher =
635 std::get<N - 1>(matchers);
636 typedef typename std::tuple_element<N - 1, ValueTuple>
::type Value;
637 const Value&
value = std::get<N - 1>(values);
638 StringMatchResultListener listener;
639 if (!matcher.MatchAndExplain(
value, &listener)) {
640 *os << " Expected arg #" << N - 1 << ": ";
641 std::get<N - 1>(matchers).DescribeTo(os);
642 *os << "\n Actual: ";
643
644
645
646
647
649 PrintIfNotEmpty(listener.str(), os);
650 *os << "\n";
651 }
652 }
653};
654
655
656template <>
657class TuplePrefix<0> {
658 public:
659 template <typename MatcherTuple, typename ValueTuple>
660 static bool Matches(const MatcherTuple& ,
661 const ValueTuple& ) {
662 return true;
663 }
664
665 template <typename MatcherTuple, typename ValueTuple>
666 static void ExplainMatchFailuresTo(const MatcherTuple& ,
667 const ValueTuple& ,
668 ::std::ostream* ) {}
669};
670
671
672
673
674
675
676template <typename MatcherTuple, typename ValueTuple>
677bool TupleMatches(const MatcherTuple& matcher_tuple,
678 const ValueTuple& value_tuple) {
679
680
683 matcher_and_value_have_different_numbers_of_fields);
685 value_tuple);
686}
687
688
689
690template <typename MatcherTuple, typename ValueTuple>
691void ExplainMatchFailureTupleTo(const MatcherTuple& matchers,
692 const ValueTuple& values,
693 ::std::ostream* os) {
695 matchers, values, os);
696}
697
698
699
700
701
702template <typename Tuple, typename Func, typename OutIter>
703class TransformTupleValuesHelper {
704 private:
705 typedef ::std::tuple_size<Tuple> TupleSize;
706
707 public:
708
709
710 static OutIter
Run(Func f,
const Tuple& t, OutIter out) {
711 return IterateOverTuple<Tuple, TupleSize::value>()(f, t, out);
712 }
713
714 private:
715 template <typename Tup, size_t kRemainingSize>
716 struct IterateOverTuple {
717 OutIter operator() (Func f, const Tup& t, OutIter out) const {
718 *out++ = f(::std::get<TupleSize::value - kRemainingSize>(t));
719 return IterateOverTuple<Tup, kRemainingSize - 1>()(f, t, out);
720 }
721 };
722 template <typename Tup>
723 struct IterateOverTuple<Tup, 0> {
724 OutIter operator() (Func , const Tup& , OutIter out) const {
725 return out;
726 }
727 };
728};
729
730
731
732
733template <typename Tuple, typename Func, typename OutIter>
734OutIter TransformTupleValues(Func f, const Tuple& t, OutIter out) {
736}
737
738
739
740
741
742class AnythingMatcher {
743 public:
744 using is_gtest_matcher = void;
745
746 template <typename T>
747 bool MatchAndExplain(const T& , std::ostream* ) const {
748 return true;
749 }
750 void DescribeTo(std::ostream* os) const { *os << "is anything"; }
751 void DescribeNegationTo(::std::ostream* os) const {
752
753
754
755 *os << "never matches";
756 }
757};
758
759
760
761class IsNullMatcher {
762 public:
763 template <typename Pointer>
764 bool MatchAndExplain(
const Pointer&
p,
765 MatchResultListener* ) const {
767 }
768
769 void DescribeTo(::std::ostream* os) const { *os << "is NULL"; }
770 void DescribeNegationTo(::std::ostream* os) const {
771 *os << "isn't NULL";
772 }
773};
774
775
776
777class NotNullMatcher {
778 public:
779 template <typename Pointer>
780 bool MatchAndExplain(
const Pointer&
p,
781 MatchResultListener* ) const {
783 }
784
785 void DescribeTo(::std::ostream* os) const { *os << "isn't NULL"; }
786 void DescribeNegationTo(::std::ostream* os) const {
787 *os << "is NULL";
788 }
789};
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804template <typename T>
805class RefMatcher;
806
807template <typename T>
808class RefMatcher<T&> {
809
810
811
812
813
814 public:
815
816
817
818 explicit RefMatcher(T&
x) : object_(
x) {}
819
820 template <typename Super>
821 operator Matcher<Super&>() const {
822
823
824
825
826
827 return MakeMatcher(new Impl<Super>(object_));
828 }
829
830 private:
831 template <typename Super>
832 class Impl : public MatcherInterface<Super&> {
833 public:
834 explicit Impl(Super&
x) : object_(
x) {}
835
836
837
838 bool MatchAndExplain(Super&
x,
839 MatchResultListener* listener) const override {
840 *listener <<
"which is located @" <<
static_cast<const void*
>(&
x);
841 return &
x == &object_;
842 }
843
844 void DescribeTo(::std::ostream* os) const override {
845 *os << "references the variable ";
847 }
848
849 void DescribeNegationTo(::std::ostream* os) const override {
850 *os << "does not reference the variable ";
852 }
853
854 private:
855 const Super& object_;
856 };
857
858 T& object_;
859};
860
861
862inline bool CaseInsensitiveCStringEquals(const char* lhs, const char* rhs) {
863 return String::CaseInsensitiveCStringEquals(lhs, rhs);
864}
865
866inline bool CaseInsensitiveCStringEquals(const wchar_t* lhs,
867 const wchar_t* rhs) {
868 return String::CaseInsensitiveWideCStringEquals(lhs, rhs);
869}
870
871
872
873template <typename StringType>
874bool CaseInsensitiveStringEquals(const StringType& s1,
875 const StringType& s2) {
876
877 if (!CaseInsensitiveCStringEquals(s1.c_str(), s2.c_str())) {
878 return false;
879 }
880
881
882 const typename StringType::value_type nul = 0;
883 const size_t i1 = s1.find(nul), i2 = s2.find(nul);
884
885
886 if (i1 == StringType::npos || i2 == StringType::npos) {
887 return i1 == i2;
888 }
889
890
891 return CaseInsensitiveStringEquals(s1.substr(i1 + 1), s2.substr(i2 + 1));
892}
893
894
895
896
897template <typename StringType>
898class StrEqualityMatcher {
899 public:
900 StrEqualityMatcher(StringType str, bool expect_eq, bool case_sensitive)
901 : string_(
std::move(str)),
902 expect_eq_(expect_eq),
903 case_sensitive_(case_sensitive) {}
904
905#if GTEST_INTERNAL_HAS_STRING_VIEW
906 bool MatchAndExplain(const internal::StringView& s,
907 MatchResultListener* listener) const {
908
909
910 const StringType& str = std::string(s);
911 return MatchAndExplain(str, listener);
912 }
913#endif
914
915
916
917
918
919
920 template <typename CharType>
921 bool MatchAndExplain(CharType* s, MatchResultListener* listener) const {
922 if (s == nullptr) {
923 return !expect_eq_;
924 }
925 return MatchAndExplain(StringType(s), listener);
926 }
927
928
929
930
931
932 template <typename MatcheeStringType>
933 bool MatchAndExplain(const MatcheeStringType& s,
934 MatchResultListener* ) const {
935 const StringType s2(s);
936 const bool eq = case_sensitive_ ? s2 == string_ :
937 CaseInsensitiveStringEquals(s2, string_);
938 return expect_eq_ == eq;
939 }
940
941 void DescribeTo(::std::ostream* os) const {
942 DescribeToHelper(expect_eq_, os);
943 }
944
945 void DescribeNegationTo(::std::ostream* os) const {
946 DescribeToHelper(!expect_eq_, os);
947 }
948
949 private:
950 void DescribeToHelper(bool expect_eq, ::std::ostream* os) const {
951 *os << (expect_eq ? "is " : "isn't ");
952 *os << "equal to ";
953 if (!case_sensitive_) {
954 *os << "(ignoring case) ";
955 }
957 }
958
959 const StringType string_;
960 const bool expect_eq_;
961 const bool case_sensitive_;
962};
963
964
965
966
967template <typename StringType>
968class HasSubstrMatcher {
969 public:
970 explicit HasSubstrMatcher(const StringType& substring)
971 : substring_(substring) {}
972
973#if GTEST_INTERNAL_HAS_STRING_VIEW
974 bool MatchAndExplain(const internal::StringView& s,
975 MatchResultListener* listener) const {
976
977
978 const StringType& str = std::string(s);
979 return MatchAndExplain(str, listener);
980 }
981#endif
982
983
984
985
986
987
988 template <typename CharType>
989 bool MatchAndExplain(CharType* s, MatchResultListener* listener) const {
990 return s != nullptr && MatchAndExplain(StringType(s), listener);
991 }
992
993
994
995
996
997 template <typename MatcheeStringType>
998 bool MatchAndExplain(const MatcheeStringType& s,
999 MatchResultListener* ) const {
1000 return StringType(s).find(substring_) != StringType::npos;
1001 }
1002
1003
1004 void DescribeTo(::std::ostream* os) const {
1005 *os << "has substring ";
1007 }
1008
1009 void DescribeNegationTo(::std::ostream* os) const {
1010 *os << "has no substring ";
1012 }
1013
1014 private:
1015 const StringType substring_;
1016};
1017
1018
1019
1020
1021template <typename StringType>
1022class StartsWithMatcher {
1023 public:
1024 explicit StartsWithMatcher(const StringType& prefix) : prefix_(prefix) {
1025 }
1026
1027#if GTEST_INTERNAL_HAS_STRING_VIEW
1028 bool MatchAndExplain(const internal::StringView& s,
1029 MatchResultListener* listener) const {
1030
1031
1032 const StringType& str = std::string(s);
1033 return MatchAndExplain(str, listener);
1034 }
1035#endif
1036
1037
1038
1039
1040
1041
1042 template <typename CharType>
1043 bool MatchAndExplain(CharType* s, MatchResultListener* listener) const {
1044 return s != nullptr && MatchAndExplain(StringType(s), listener);
1045 }
1046
1047
1048
1049
1050
1051 template <typename MatcheeStringType>
1052 bool MatchAndExplain(const MatcheeStringType& s,
1053 MatchResultListener* ) const {
1054 const StringType& s2(s);
1055 return s2.length() >= prefix_.length() &&
1056 s2.substr(0, prefix_.length()) == prefix_;
1057 }
1058
1059 void DescribeTo(::std::ostream* os) const {
1060 *os << "starts with ";
1062 }
1063
1064 void DescribeNegationTo(::std::ostream* os) const {
1065 *os << "doesn't start with ";
1067 }
1068
1069 private:
1070 const StringType prefix_;
1071};
1072
1073
1074
1075
1076template <typename StringType>
1077class EndsWithMatcher {
1078 public:
1079 explicit EndsWithMatcher(const StringType& suffix) : suffix_(suffix) {}
1080
1081#if GTEST_INTERNAL_HAS_STRING_VIEW
1082 bool MatchAndExplain(const internal::StringView& s,
1083 MatchResultListener* listener) const {
1084
1085
1086 const StringType& str = std::string(s);
1087 return MatchAndExplain(str, listener);
1088 }
1089#endif
1090
1091
1092
1093
1094
1095
1096 template <typename CharType>
1097 bool MatchAndExplain(CharType* s, MatchResultListener* listener) const {
1098 return s != nullptr && MatchAndExplain(StringType(s), listener);
1099 }
1100
1101
1102
1103
1104
1105 template <typename MatcheeStringType>
1106 bool MatchAndExplain(const MatcheeStringType& s,
1107 MatchResultListener* ) const {
1108 const StringType& s2(s);
1109 return s2.length() >= suffix_.length() &&
1110 s2.substr(s2.length() - suffix_.length()) == suffix_;
1111 }
1112
1113 void DescribeTo(::std::ostream* os) const {
1114 *os << "ends with ";
1116 }
1117
1118 void DescribeNegationTo(::std::ostream* os) const {
1119 *os << "doesn't end with ";
1121 }
1122
1123 private:
1124 const StringType suffix_;
1125};
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135template <typename D, typename Op>
1136class PairMatchBase {
1137 public:
1138 template <typename T1, typename T2>
1139 operator Matcher<::std::tuple<T1, T2>>() const {
1140 return Matcher<::std::tuple<T1, T2>>(new Impl<const ::std::tuple<T1, T2>&>);
1141 }
1142 template <typename T1, typename T2>
1143 operator Matcher<const ::std::tuple<T1, T2>&>() const {
1144 return MakeMatcher(new Impl<const ::std::tuple<T1, T2>&>);
1145 }
1146
1147 private:
1148 static ::std::ostream& GetDesc(::std::ostream& os) {
1149 return os << D::Desc();
1150 }
1151
1152 template <typename Tuple>
1153 class Impl : public MatcherInterface<Tuple> {
1154 public:
1155 bool MatchAndExplain(Tuple args,
1156 MatchResultListener* ) const override {
1157 return Op()(::std::get<0>(args), ::std::get<1>(args));
1158 }
1159 void DescribeTo(::std::ostream* os) const override {
1160 *os << "are " << GetDesc;
1161 }
1162 void DescribeNegationTo(::std::ostream* os) const override {
1163 *os << "aren't " << GetDesc;
1164 }
1165 };
1166};
1167
1168class Eq2Matcher : public PairMatchBase<Eq2Matcher, AnyEq> {
1169 public:
1170 static const char* Desc() { return "an equal pair"; }
1171};
1172class Ne2Matcher : public PairMatchBase<Ne2Matcher, AnyNe> {
1173 public:
1174 static const char* Desc() { return "an unequal pair"; }
1175};
1176class Lt2Matcher : public PairMatchBase<Lt2Matcher, AnyLt> {
1177 public:
1178 static const char* Desc() { return "a pair where the first < the second"; }
1179};
1180class Gt2Matcher : public PairMatchBase<Gt2Matcher, AnyGt> {
1181 public:
1182 static const char* Desc() { return "a pair where the first > the second"; }
1183};
1184class Le2Matcher : public PairMatchBase<Le2Matcher, AnyLe> {
1185 public:
1186 static const char* Desc() { return "a pair where the first <= the second"; }
1187};
1188class Ge2Matcher : public PairMatchBase<Ge2Matcher, AnyGe> {
1189 public:
1190 static const char* Desc() { return "a pair where the first >= the second"; }
1191};
1192
1193
1194
1195
1196
1197template <typename T>
1198class NotMatcherImpl : public MatcherInterface<const T&> {
1199 public:
1200 explicit NotMatcherImpl(const Matcher<T>& matcher)
1201 : matcher_(matcher) {}
1202
1203 bool MatchAndExplain(
const T&
x,
1204 MatchResultListener* listener) const override {
1205 return !matcher_.MatchAndExplain(
x, listener);
1206 }
1207
1208 void DescribeTo(::std::ostream* os) const override {
1209 matcher_.DescribeNegationTo(os);
1210 }
1211
1212 void DescribeNegationTo(::std::ostream* os) const override {
1213 matcher_.DescribeTo(os);
1214 }
1215
1216 private:
1217 const Matcher<T> matcher_;
1218};
1219
1220
1221
1222template <typename InnerMatcher>
1223class NotMatcher {
1224 public:
1225 explicit NotMatcher(InnerMatcher matcher) : matcher_(matcher) {}
1226
1227
1228
1229 template <typename T>
1230 operator Matcher<T>() const {
1231 return Matcher<T>(new NotMatcherImpl<T>(SafeMatcherCast<T>(matcher_)));
1232 }
1233
1234 private:
1235 InnerMatcher matcher_;
1236};
1237
1238
1239
1240
1241
1242template <typename T>
1243class AllOfMatcherImpl : public MatcherInterface<const T&> {
1244 public:
1245 explicit AllOfMatcherImpl(std::vector<Matcher<T> > matchers)
1246 : matchers_(
std::move(matchers)) {}
1247
1248 void DescribeTo(::std::ostream* os) const override {
1249 *os << "(";
1250 for (
size_t i = 0;
i < matchers_.size(); ++
i) {
1251 if (
i != 0) *os <<
") and (";
1252 matchers_[
i].DescribeTo(os);
1253 }
1254 *os << ")";
1255 }
1256
1257 void DescribeNegationTo(::std::ostream* os) const override {
1258 *os << "(";
1259 for (
size_t i = 0;
i < matchers_.size(); ++
i) {
1260 if (
i != 0) *os <<
") or (";
1261 matchers_[
i].DescribeNegationTo(os);
1262 }
1263 *os << ")";
1264 }
1265
1266 bool MatchAndExplain(
const T&
x,
1267 MatchResultListener* listener) const override {
1268
1269
1270 std::string all_match_result;
1271
1272 for (
size_t i = 0;
i < matchers_.size(); ++
i) {
1273 StringMatchResultListener slistener;
1274 if (matchers_[
i].MatchAndExplain(
x, &slistener)) {
1275 if (all_match_result.empty()) {
1276 all_match_result = slistener.str();
1277 } else {
1278 std::string result = slistener.str();
1279 if (!result.empty()) {
1280 all_match_result += ", and ";
1281 all_match_result += result;
1282 }
1283 }
1284 } else {
1285 *listener << slistener.str();
1286 return false;
1287 }
1288 }
1289
1290
1291 *listener << all_match_result;
1292 return true;
1293 }
1294
1295 private:
1296 const std::vector<Matcher<T> > matchers_;
1297};
1298
1299
1300
1301
1302
1303template <template <typename T> class CombiningMatcher, typename... Args>
1304class VariadicMatcher {
1305 public:
1306 VariadicMatcher(const Args&... matchers)
1307 : matchers_(matchers...) {
1308 static_assert(sizeof...(Args) > 0, "Must have at least one matcher.");
1309 }
1310
1311 VariadicMatcher(const VariadicMatcher&) = default;
1312 VariadicMatcher& operator=(const VariadicMatcher&) = delete;
1313
1314
1315
1316
1317 template <typename T>
1318 operator Matcher<T>() const {
1319 std::vector<Matcher<T> > values;
1320 CreateVariadicMatcher<T>(&values, std::integral_constant<size_t, 0>());
1321 return Matcher<T>(new CombiningMatcher<T>(std::move(values)));
1322 }
1323
1324 private:
1325 template <typename T, size_t I>
1326 void CreateVariadicMatcher(std::vector<Matcher<T> >* values,
1327 std::integral_constant<size_t, I>) const {
1328 values->push_back(SafeMatcherCast<T>(std::get<I>(matchers_)));
1329 CreateVariadicMatcher<T>(values, std::integral_constant<size_t, I + 1>());
1330 }
1331
1332 template <typename T>
1333 void CreateVariadicMatcher(
1334 std::vector<Matcher<T> >*,
1335 std::integral_constant<size_t, sizeof...(Args)>) const {}
1336
1337 std::tuple<Args...> matchers_;
1338};
1339
1340template <typename... Args>
1341using AllOfMatcher = VariadicMatcher<AllOfMatcherImpl, Args...>;
1342
1343
1344
1345
1346
1347template <typename T>
1348class AnyOfMatcherImpl : public MatcherInterface<const T&> {
1349 public:
1350 explicit AnyOfMatcherImpl(std::vector<Matcher<T> > matchers)
1351 : matchers_(
std::move(matchers)) {}
1352
1353 void DescribeTo(::std::ostream* os) const override {
1354 *os << "(";
1355 for (
size_t i = 0;
i < matchers_.size(); ++
i) {
1356 if (
i != 0) *os <<
") or (";
1357 matchers_[
i].DescribeTo(os);
1358 }
1359 *os << ")";
1360 }
1361
1362 void DescribeNegationTo(::std::ostream* os) const override {
1363 *os << "(";
1364 for (
size_t i = 0;
i < matchers_.size(); ++
i) {
1365 if (
i != 0) *os <<
") and (";
1366 matchers_[
i].DescribeNegationTo(os);
1367 }
1368 *os << ")";
1369 }
1370
1371 bool MatchAndExplain(
const T&
x,
1372 MatchResultListener* listener) const override {
1373 std::string no_match_result;
1374
1375
1376
1377 for (
size_t i = 0;
i < matchers_.size(); ++
i) {
1378 StringMatchResultListener slistener;
1379 if (matchers_[
i].MatchAndExplain(
x, &slistener)) {
1380 *listener << slistener.str();
1381 return true;
1382 } else {
1383 if (no_match_result.empty()) {
1384 no_match_result = slistener.str();
1385 } else {
1386 std::string result = slistener.str();
1387 if (!result.empty()) {
1388 no_match_result += ", and ";
1389 no_match_result += result;
1390 }
1391 }
1392 }
1393 }
1394
1395
1396 *listener << no_match_result;
1397 return false;
1398 }
1399
1400 private:
1401 const std::vector<Matcher<T> > matchers_;
1402};
1403
1404
1405template <typename... Args>
1406using AnyOfMatcher = VariadicMatcher<AnyOfMatcherImpl, Args...>;
1407
1408
1409template <template <class> class MatcherImpl, typename T>
1410class SomeOfArrayMatcher {
1411 public:
1412
1413
1414 template <typename Iter>
1416
1417 template <typename U>
1418 operator Matcher<U>() const {
1420 std::vector<Matcher<RawU>> matchers;
1421 for (const auto& matcher : matchers_) {
1422 matchers.push_back(MatcherCast<RawU>(matcher));
1423 }
1424 return Matcher<U>(new MatcherImpl<RawU>(std::move(matchers)));
1425 }
1426
1427 private:
1428 const ::std::vector<T> matchers_;
1429};
1430
1431template <typename T>
1432using AllOfArrayMatcher = SomeOfArrayMatcher<AllOfMatcherImpl, T>;
1433
1434template <typename T>
1435using AnyOfArrayMatcher = SomeOfArrayMatcher<AnyOfMatcherImpl, T>;
1436
1437
1438
1439template <typename Predicate>
1440class TrulyMatcher {
1441 public:
1442 explicit TrulyMatcher(Predicate pred) : predicate_(pred) {}
1443
1444
1445
1446
1447
1448 template <typename T>
1449 bool MatchAndExplain(T&
x,
1450 MatchResultListener* listener) const {
1451
1452
1453
1454
1455
1456
1458 return true;
1459 *listener << "didn't satisfy the given predicate";
1460 return false;
1461 }
1462
1463 void DescribeTo(::std::ostream* os) const {
1464 *os << "satisfies the given predicate";
1465 }
1466
1467 void DescribeNegationTo(::std::ostream* os) const {
1468 *os << "doesn't satisfy the given predicate";
1469 }
1470
1471 private:
1472 Predicate predicate_;
1473};
1474
1475
1476
1477template <typename M>
1478class MatcherAsPredicate {
1479 public:
1480 explicit MatcherAsPredicate(M matcher) : matcher_(matcher) {}
1481
1482
1483
1484
1485
1486
1487
1488 template <typename T>
1489 bool operator()(
const T&
x)
const {
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504 return MatcherCast<const T&>(matcher_).Matches(
x);
1505 }
1506
1507 private:
1508 M matcher_;
1509};
1510
1511
1512
1513template <typename M>
1514class PredicateFormatterFromMatcher {
1515 public:
1516 explicit PredicateFormatterFromMatcher(M m) : matcher_(
std::move(m)) {}
1517
1518
1519
1520
1521 template <typename T>
1522 AssertionResult operator()(
const char* value_text,
const T&
x)
const {
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534 const Matcher<const T&> matcher = SafeMatcherCast<const T&>(matcher_);
1535
1536
1537
1538 if (matcher.Matches(
x)) {
1540 }
1541
1542 ::std::stringstream ss;
1543 ss << "Value of: " << value_text << "\n"
1544 << "Expected: ";
1545 matcher.DescribeTo(&ss);
1546
1547
1548 StringMatchResultListener listener;
1549 if (MatchPrintAndExplain(
x, matcher, &listener)) {
1550 ss << "\n The matcher failed on the initial attempt; but passed when "
1551 "rerun to generate the explanation.";
1552 }
1553 ss << "\n Actual: " << listener.str();
1555 }
1556
1557 private:
1558 const M matcher_;
1559};
1560
1561
1562
1563
1564
1565template <typename M>
1566inline PredicateFormatterFromMatcher<M>
1567MakePredicateFormatterFromMatcher(M matcher) {
1568 return PredicateFormatterFromMatcher<M>(std::move(matcher));
1569}
1570
1571
1572
1573class IsNanMatcher {
1574 public:
1575 template <typename FloatType>
1576 bool MatchAndExplain(const FloatType& f,
1577 MatchResultListener* ) const {
1578 return (::std::isnan)(f);
1579 }
1580
1581 void DescribeTo(::std::ostream* os) const { *os << "is NaN"; }
1582 void DescribeNegationTo(::std::ostream* os) const {
1583 *os << "isn't NaN";
1584 }
1585};
1586
1587
1588
1589
1590
1591template <typename FloatType>
1592class FloatingEqMatcher {
1593 public:
1594
1595
1596
1597
1598
1599
1600 FloatingEqMatcher(FloatType expected, bool nan_eq_nan) :
1601 expected_(expected), nan_eq_nan_(nan_eq_nan), max_abs_error_(-1) {
1602 }
1603
1604
1605
1606
1607 FloatingEqMatcher(FloatType expected, bool nan_eq_nan,
1608 FloatType max_abs_error)
1609 : expected_(expected),
1610 nan_eq_nan_(nan_eq_nan),
1611 max_abs_error_(max_abs_error) {
1613 << ", where max_abs_error is" << max_abs_error;
1614 }
1615
1616
1617 template <typename T>
1618 class Impl : public MatcherInterface<T> {
1619 public:
1620 Impl(FloatType expected, bool nan_eq_nan, FloatType max_abs_error)
1621 : expected_(expected),
1622 nan_eq_nan_(nan_eq_nan),
1623 max_abs_error_(max_abs_error) {}
1624
1625 bool MatchAndExplain(T
value,
1626 MatchResultListener* listener) const override {
1627 const FloatingPoint<FloatType> actual(
value), expected(expected_);
1628
1629
1630 if (actual.is_nan() || expected.is_nan()) {
1631 if (actual.is_nan() && expected.is_nan()) {
1632 return nan_eq_nan_;
1633 }
1634
1635 return false;
1636 }
1637 if (HasMaxAbsError()) {
1638
1639
1640
1641
1642 if (
value == expected_) {
1643 return true;
1644 }
1645
1646 const FloatType diff =
value - expected_;
1647 if (::std::fabs(diff) <= max_abs_error_) {
1648 return true;
1649 }
1650
1651 if (listener->IsInterested()) {
1652 *listener << "which is " << diff << " from " << expected_;
1653 }
1654 return false;
1655 } else {
1656 return actual.AlmostEquals(expected);
1657 }
1658 }
1659
1660 void DescribeTo(::std::ostream* os) const override {
1661
1662
1663
1664 const ::std::streamsize old_precision = os->precision(
1665 ::std::numeric_limits<FloatType>::digits10 + 2);
1666 if (FloatingPoint<FloatType>(expected_).is_nan()) {
1667 if (nan_eq_nan_) {
1668 *os << "is NaN";
1669 } else {
1670 *os << "never matches";
1671 }
1672 } else {
1673 *os << "is approximately " << expected_;
1674 if (HasMaxAbsError()) {
1675 *os << " (absolute error <= " << max_abs_error_ << ")";
1676 }
1677 }
1678 os->precision(old_precision);
1679 }
1680
1681 void DescribeNegationTo(::std::ostream* os) const override {
1682
1683 const ::std::streamsize old_precision = os->precision(
1684 ::std::numeric_limits<FloatType>::digits10 + 2);
1685 if (FloatingPoint<FloatType>(expected_).is_nan()) {
1686 if (nan_eq_nan_) {
1687 *os << "isn't NaN";
1688 } else {
1689 *os << "is anything";
1690 }
1691 } else {
1692 *os << "isn't approximately " << expected_;
1693 if (HasMaxAbsError()) {
1694 *os << " (absolute error > " << max_abs_error_ << ")";
1695 }
1696 }
1697
1698 os->precision(old_precision);
1699 }
1700
1701 private:
1702 bool HasMaxAbsError() const {
1703 return max_abs_error_ >= 0;
1704 }
1705
1706 const FloatType expected_;
1707 const bool nan_eq_nan_;
1708
1709 const FloatType max_abs_error_;
1710 };
1711
1712
1713
1714
1715 operator Matcher<FloatType>() const {
1716 return MakeMatcher(
1717 new Impl<FloatType>(expected_, nan_eq_nan_, max_abs_error_));
1718 }
1719
1720 operator Matcher<const FloatType&>() const {
1721 return MakeMatcher(
1722 new Impl<const FloatType&>(expected_, nan_eq_nan_, max_abs_error_));
1723 }
1724
1725 operator Matcher<FloatType&>() const {
1726 return MakeMatcher(
1727 new Impl<FloatType&>(expected_, nan_eq_nan_, max_abs_error_));
1728 }
1729
1730 private:
1731 const FloatType expected_;
1732 const bool nan_eq_nan_;
1733
1734 const FloatType max_abs_error_;
1735};
1736
1737
1738
1739
1740
1741
1742template <typename FloatType>
1743class FloatingEq2Matcher {
1744 public:
1745 FloatingEq2Matcher() { Init(-1, false); }
1746
1747 explicit FloatingEq2Matcher(bool nan_eq_nan) { Init(-1, nan_eq_nan); }
1748
1749 explicit FloatingEq2Matcher(FloatType max_abs_error) {
1750 Init(max_abs_error, false);
1751 }
1752
1753 FloatingEq2Matcher(FloatType max_abs_error, bool nan_eq_nan) {
1754 Init(max_abs_error, nan_eq_nan);
1755 }
1756
1757 template <typename T1, typename T2>
1758 operator Matcher<::std::tuple<T1, T2>>() const {
1759 return MakeMatcher(
1760 new Impl<::std::tuple<T1, T2>>(max_abs_error_, nan_eq_nan_));
1761 }
1762 template <typename T1, typename T2>
1763 operator Matcher<const ::std::tuple<T1, T2>&>() const {
1764 return MakeMatcher(
1765 new Impl<const ::std::tuple<T1, T2>&>(max_abs_error_, nan_eq_nan_));
1766 }
1767
1768 private:
1769 static ::std::ostream& GetDesc(::std::ostream& os) {
1770 return os << "an almost-equal pair";
1771 }
1772
1773 template <typename Tuple>
1774 class Impl : public MatcherInterface<Tuple> {
1775 public:
1776 Impl(FloatType max_abs_error, bool nan_eq_nan) :
1777 max_abs_error_(max_abs_error),
1778 nan_eq_nan_(nan_eq_nan) {}
1779
1780 bool MatchAndExplain(Tuple args,
1781 MatchResultListener* listener) const override {
1782 if (max_abs_error_ == -1) {
1783 FloatingEqMatcher<FloatType> fm(::std::get<0>(args), nan_eq_nan_);
1784 return static_cast<Matcher<FloatType>>(fm).MatchAndExplain(
1785 ::std::get<1>(args), listener);
1786 } else {
1787 FloatingEqMatcher<FloatType> fm(::std::get<0>(args), nan_eq_nan_,
1788 max_abs_error_);
1789 return static_cast<Matcher<FloatType>>(fm).MatchAndExplain(
1790 ::std::get<1>(args), listener);
1791 }
1792 }
1793 void DescribeTo(::std::ostream* os) const override {
1794 *os << "are " << GetDesc;
1795 }
1796 void DescribeNegationTo(::std::ostream* os) const override {
1797 *os << "aren't " << GetDesc;
1798 }
1799
1800 private:
1801 FloatType max_abs_error_;
1802 const bool nan_eq_nan_;
1803 };
1804
1805 void Init(FloatType max_abs_error_val, bool nan_eq_nan_val) {
1806 max_abs_error_ = max_abs_error_val;
1807 nan_eq_nan_ = nan_eq_nan_val;
1808 }
1809 FloatType max_abs_error_;
1810 bool nan_eq_nan_;
1811};
1812
1813
1814
1815template <typename InnerMatcher>
1816class PointeeMatcher {
1817 public:
1818 explicit PointeeMatcher(const InnerMatcher& matcher) : matcher_(matcher) {}
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828 template <typename Pointer>
1829 operator Matcher<Pointer>() const {
1830 return Matcher<Pointer>(new Impl<const Pointer&>(matcher_));
1831 }
1832
1833 private:
1834
1835 template <typename Pointer>
1836 class Impl : public MatcherInterface<Pointer> {
1837 public:
1838 using Pointee =
1840 Pointer)>::element_type;
1841
1842 explicit Impl(const InnerMatcher& matcher)
1843 : matcher_(MatcherCast<
const Pointee&>(matcher)) {}
1844
1845 void DescribeTo(::std::ostream* os) const override {
1846 *os << "points to a value that ";
1847 matcher_.DescribeTo(os);
1848 }
1849
1850 void DescribeNegationTo(::std::ostream* os) const override {
1851 *os << "does not point to a value that ";
1852 matcher_.DescribeTo(os);
1853 }
1854
1855 bool MatchAndExplain(Pointer pointer,
1856 MatchResultListener* listener) const override {
1858
1859 *listener << "which points to ";
1860 return MatchPrintAndExplain(*pointer, matcher_, listener);
1861 }
1862
1863 private:
1864 const Matcher<const Pointee&> matcher_;
1865 };
1866
1867 const InnerMatcher matcher_;
1868};
1869
1870
1871
1872
1873
1874template <typename InnerMatcher>
1875class PointerMatcher {
1876 public:
1877 explicit PointerMatcher(const InnerMatcher& matcher) : matcher_(matcher) {}
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887 template <typename PointerType>
1888 operator Matcher<PointerType>() const {
1889 return Matcher<PointerType>(new Impl<const PointerType&>(matcher_));
1890 }
1891
1892 private:
1893
1894 template <typename PointerType>
1895 class Impl : public MatcherInterface<PointerType> {
1896 public:
1897 using Pointer =
1899 PointerType)>::element_type*;
1900
1901 explicit Impl(const InnerMatcher& matcher)
1902 : matcher_(MatcherCast<Pointer>(matcher)) {}
1903
1904 void DescribeTo(::std::ostream* os) const override {
1905 *os << "is a pointer that ";
1906 matcher_.DescribeTo(os);
1907 }
1908
1909 void DescribeNegationTo(::std::ostream* os) const override {
1910 *os << "is not a pointer that ";
1911 matcher_.DescribeTo(os);
1912 }
1913
1914 bool MatchAndExplain(PointerType pointer,
1915 MatchResultListener* listener) const override {
1916 *listener << "which is a pointer that ";
1918 return MatchPrintAndExplain(
p, matcher_, listener);
1919 }
1920
1921 private:
1922 Matcher<Pointer> matcher_;
1923 };
1924
1925 const InnerMatcher matcher_;
1926};
1927
1928#if GTEST_HAS_RTTI
1929
1930
1931
1932
1933
1934
1935template <typename To>
1936class WhenDynamicCastToMatcherBase {
1937 public:
1938 explicit WhenDynamicCastToMatcherBase(const Matcher<To>& matcher)
1939 : matcher_(matcher) {}
1940
1941 void DescribeTo(::std::ostream* os) const {
1942 GetCastTypeDescription(os);
1943 matcher_.DescribeTo(os);
1944 }
1945
1946 void DescribeNegationTo(::std::ostream* os) const {
1947 GetCastTypeDescription(os);
1948 matcher_.DescribeNegationTo(os);
1949 }
1950
1951 protected:
1952 const Matcher<To> matcher_;
1953
1954 static std::string GetToName() {
1955 return GetTypeName<To>();
1956 }
1957
1958 private:
1959 static void GetCastTypeDescription(::std::ostream* os) {
1960 *os << "when dynamic_cast to " << GetToName() << ", ";
1961 }
1962};
1963
1964
1965
1966template <typename To>
1967class WhenDynamicCastToMatcher : public WhenDynamicCastToMatcherBase<To> {
1968 public:
1969 explicit WhenDynamicCastToMatcher(const Matcher<To>& matcher)
1970 : WhenDynamicCastToMatcherBase<To>(matcher) {}
1971
1972 template <typename From>
1973 bool MatchAndExplain(From from, MatchResultListener* listener) const {
1974 To to = dynamic_cast<To>(from);
1975 return MatchPrintAndExplain(to, this->matcher_, listener);
1976 }
1977};
1978
1979
1980
1981template <typename To>
1982class WhenDynamicCastToMatcher<To&> : public WhenDynamicCastToMatcherBase<To&> {
1983 public:
1984 explicit WhenDynamicCastToMatcher(const Matcher<To&>& matcher)
1985 : WhenDynamicCastToMatcherBase<To&>(matcher) {}
1986
1987 template <typename From>
1988 bool MatchAndExplain(From& from, MatchResultListener* listener) const {
1989
1990 To* to = dynamic_cast<To*>(&from);
1991 if (to == nullptr) {
1992 *listener << "which cannot be dynamic_cast to " << this->GetToName();
1993 return false;
1994 }
1995 return MatchPrintAndExplain(*to, this->matcher_, listener);
1996 }
1997};
1998#endif
1999
2000
2001
2002template <typename Class, typename FieldType>
2003class FieldMatcher {
2004 public:
2005 FieldMatcher(FieldType Class::*field,
2006 const Matcher<const FieldType&>& matcher)
2007 : field_(field), matcher_(matcher), whose_field_("whose given field ") {}
2008
2009 FieldMatcher(const std::string& field_name, FieldType Class::*field,
2010 const Matcher<const FieldType&>& matcher)
2011 : field_(field),
2012 matcher_(matcher),
2013 whose_field_("whose field `" + field_name + "` ") {}
2014
2015 void DescribeTo(::std::ostream* os) const {
2016 *os << "is an object " << whose_field_;
2017 matcher_.DescribeTo(os);
2018 }
2019
2020 void DescribeNegationTo(::std::ostream* os) const {
2021 *os << "is an object " << whose_field_;
2022 matcher_.DescribeNegationTo(os);
2023 }
2024
2025 template <typename T>
2026 bool MatchAndExplain(
const T&
value, MatchResultListener* listener)
const {
2027
2028
2029 return MatchAndExplainImpl(
2032 }
2033
2034 private:
2035 bool MatchAndExplainImpl(std::false_type ,
2036 const Class& obj,
2037 MatchResultListener* listener) const {
2038 *listener << whose_field_ << "is ";
2039 return MatchPrintAndExplain(obj.*field_, matcher_, listener);
2040 }
2041
2042 bool MatchAndExplainImpl(std::true_type ,
const Class*
p,
2043 MatchResultListener* listener) const {
2044 if (
p ==
nullptr)
return false;
2045
2046 *listener << "which points to an object ";
2047
2048
2049
2050 return MatchAndExplainImpl(std::false_type(), *
p, listener);
2051 }
2052
2053 const FieldType Class::*field_;
2054 const Matcher<const FieldType&> matcher_;
2055
2056
2057
2058 const std::string whose_field_;
2059};
2060
2061
2062
2063
2064
2065
2066template <typename Class, typename PropertyType, typename Property>
2067class PropertyMatcher {
2068 public:
2069 typedef const PropertyType& RefToConstProperty;
2070
2071 PropertyMatcher(Property property, const Matcher<RefToConstProperty>& matcher)
2072 : property_(property),
2073 matcher_(matcher),
2074 whose_property_("whose given property ") {}
2075
2076 PropertyMatcher(const std::string& property_name, Property property,
2077 const Matcher<RefToConstProperty>& matcher)
2078 : property_(property),
2079 matcher_(matcher),
2080 whose_property_("whose property `" + property_name + "` ") {}
2081
2082 void DescribeTo(::std::ostream* os) const {
2083 *os << "is an object " << whose_property_;
2084 matcher_.DescribeTo(os);
2085 }
2086
2087 void DescribeNegationTo(::std::ostream* os) const {
2088 *os << "is an object " << whose_property_;
2089 matcher_.DescribeNegationTo(os);
2090 }
2091
2092 template <typename T>
2093 bool MatchAndExplain(
const T&
value, MatchResultListener* listener)
const {
2094 return MatchAndExplainImpl(
2097 }
2098
2099 private:
2100 bool MatchAndExplainImpl(std::false_type ,
2101 const Class& obj,
2102 MatchResultListener* listener) const {
2103 *listener << whose_property_ << "is ";
2104
2105
2106 RefToConstProperty result = (obj.*property_)();
2107 return MatchPrintAndExplain(result, matcher_, listener);
2108 }
2109
2110 bool MatchAndExplainImpl(std::true_type ,
const Class*
p,
2111 MatchResultListener* listener) const {
2112 if (
p ==
nullptr)
return false;
2113
2114 *listener << "which points to an object ";
2115
2116
2117
2118 return MatchAndExplainImpl(std::false_type(), *
p, listener);
2119 }
2120
2121 Property property_;
2122 const Matcher<RefToConstProperty> matcher_;
2123
2124
2125
2126 const std::string whose_property_;
2127};
2128
2129
2130
2131template <typename Functor>
2132struct CallableTraits {
2133 typedef Functor StorageType;
2134
2135 static void CheckIsValid(Functor ) {}
2136
2137 template <typename T>
2138 static auto Invoke(Functor f,
const T& arg) ->
decltype(f(arg)) {
2139 return f(arg);
2140 }
2141};
2142
2143
2144template <typename ArgType, typename ResType>
2145struct CallableTraits<ResType(*)(ArgType)> {
2146 typedef ResType ResultType;
2147 typedef ResType(*StorageType)(ArgType);
2148
2149 static void CheckIsValid(ResType(*f)(ArgType)) {
2151 << "NULL function pointer is passed into ResultOf().";
2152 }
2153 template <typename T>
2154 static ResType
Invoke(ResType(*f)(ArgType), T arg) {
2155 return (*f)(arg);
2156 }
2157};
2158
2159
2160
2161template <typename Callable, typename InnerMatcher>
2162class ResultOfMatcher {
2163 public:
2164 ResultOfMatcher(Callable callable, InnerMatcher matcher)
2165 : callable_(
std::move(callable)), matcher_(
std::move(matcher)) {
2166 CallableTraits<Callable>::CheckIsValid(callable_);
2167 }
2168
2169 template <typename T>
2170 operator Matcher<T>() const {
2171 return Matcher<T>(new Impl<const T&>(callable_, matcher_));
2172 }
2173
2174 private:
2175 typedef typename CallableTraits<Callable>::StorageType CallableStorageType;
2176
2177 template <typename T>
2178 class Impl : public MatcherInterface<T> {
2179 using ResultType = decltype(CallableTraits<Callable>::template Invoke<T>(
2180 std::declval<CallableStorageType>(), std::declval<T>()));
2181
2182 public:
2183 template <typename M>
2184 Impl(const CallableStorageType& callable, const M& matcher)
2185 : callable_(callable), matcher_(MatcherCast<ResultType>(matcher)) {}
2186
2187 void DescribeTo(::std::ostream* os) const override {
2188 *os << "is mapped by the given callable to a value that ";
2189 matcher_.DescribeTo(os);
2190 }
2191
2192 void DescribeNegationTo(::std::ostream* os) const override {
2193 *os << "is mapped by the given callable to a value that ";
2194 matcher_.DescribeNegationTo(os);
2195 }
2196
2197 bool MatchAndExplain(T obj, MatchResultListener* listener) const override {
2198 *listener << "which is mapped by the given callable to ";
2199
2200
2201
2202
2203 ResultType result =
2204 CallableTraits<Callable>::template Invoke<T>(callable_, obj);
2205 return MatchPrintAndExplain(result, matcher_, listener);
2206 }
2207
2208 private:
2209
2210
2211
2212
2213
2214 mutable CallableStorageType callable_;
2215 const Matcher<ResultType> matcher_;
2216 };
2217
2218 const CallableStorageType callable_;
2219 const InnerMatcher matcher_;
2220};
2221
2222
2223template <typename SizeMatcher>
2224class SizeIsMatcher {
2225 public:
2226 explicit SizeIsMatcher(const SizeMatcher& size_matcher)
2227 : size_matcher_(size_matcher) {
2228 }
2229
2230 template <typename Container>
2231 operator Matcher<Container>() const {
2232 return Matcher<Container>(new Impl<const Container&>(size_matcher_));
2233 }
2234
2235 template <typename Container>
2236 class Impl : public MatcherInterface<Container> {
2237 public:
2238 using SizeType = decltype(std::declval<Container>().size());
2239 explicit Impl(const SizeMatcher& size_matcher)
2240 : size_matcher_(MatcherCast<SizeType>(size_matcher)) {}
2241
2242 void DescribeTo(::std::ostream* os) const override {
2243 *os << "size ";
2244 size_matcher_.DescribeTo(os);
2245 }
2246 void DescribeNegationTo(::std::ostream* os) const override {
2247 *os << "size ";
2248 size_matcher_.DescribeNegationTo(os);
2249 }
2250
2251 bool MatchAndExplain(Container container,
2252 MatchResultListener* listener) const override {
2253 SizeType size = container.size();
2254 StringMatchResultListener size_listener;
2255 const bool result = size_matcher_.MatchAndExplain(size, &size_listener);
2256 *listener
2257 << "whose size " << size << (result ? " matches" : " doesn't match");
2258 PrintIfNotEmpty(size_listener.str(), listener->stream());
2259 return result;
2260 }
2261
2262 private:
2263 const Matcher<SizeType> size_matcher_;
2264 };
2265
2266 private:
2267 const SizeMatcher size_matcher_;
2268};
2269
2270
2271
2272template <typename DistanceMatcher>
2273class BeginEndDistanceIsMatcher {
2274 public:
2275 explicit BeginEndDistanceIsMatcher(const DistanceMatcher& distance_matcher)
2276 : distance_matcher_(distance_matcher) {}
2277
2278 template <typename Container>
2279 operator Matcher<Container>() const {
2280 return Matcher<Container>(new Impl<const Container&>(distance_matcher_));
2281 }
2282
2283 template <typename Container>
2284 class Impl : public MatcherInterface<Container> {
2285 public:
2286 typedef internal::StlContainerView<
2288 typedef typename std::iterator_traits<
2289 typename ContainerView::type::const_iterator>::difference_type
2290 DistanceType;
2291 explicit Impl(const DistanceMatcher& distance_matcher)
2292 : distance_matcher_(MatcherCast<DistanceType>(distance_matcher)) {}
2293
2294 void DescribeTo(::std::ostream* os) const override {
2295 *os << "distance between begin() and end() ";
2296 distance_matcher_.DescribeTo(os);
2297 }
2298 void DescribeNegationTo(::std::ostream* os) const override {
2299 *os << "distance between begin() and end() ";
2300 distance_matcher_.DescribeNegationTo(os);
2301 }
2302
2303 bool MatchAndExplain(Container container,
2304 MatchResultListener* listener) const override {
2305 using std::begin;
2306 using std::end;
2308 StringMatchResultListener distance_listener;
2309 const bool result =
2310 distance_matcher_.MatchAndExplain(
distance, &distance_listener);
2311 *listener <<
"whose distance between begin() and end() " <<
distance
2312 << (result ? " matches" : " doesn't match");
2313 PrintIfNotEmpty(distance_listener.str(), listener->stream());
2314 return result;
2315 }
2316
2317 private:
2318 const Matcher<DistanceType> distance_matcher_;
2319 };
2320
2321 private:
2322 const DistanceMatcher distance_matcher_;
2323};
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335template <typename Container>
2336class ContainerEqMatcher {
2337 public:
2338 typedef internal::StlContainerView<Container> View;
2340 typedef typename View::const_reference StlContainerReference;
2341
2343 "Container type must not be const");
2345 "Container type must not be a reference");
2346
2347
2348
2349 explicit ContainerEqMatcher(const Container& expected)
2350 : expected_(View::Copy(expected)) {}
2351
2352 void DescribeTo(::std::ostream* os) const {
2353 *os << "equals ";
2355 }
2356 void DescribeNegationTo(::std::ostream* os) const {
2357 *os << "does not equal ";
2359 }
2360
2361 template <typename LhsContainer>
2362 bool MatchAndExplain(const LhsContainer& lhs,
2363 MatchResultListener* listener) const {
2364 typedef internal::StlContainerView<
2366 LhsView;
2368 StlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2369 if (lhs_stl_container == expected_)
2370 return true;
2371
2372 ::std::ostream* const os = listener->stream();
2373 if (os != nullptr) {
2374
2375 bool printed_header = false;
2376 for (typename LhsStlContainer::const_iterator it =
2377 lhs_stl_container.begin();
2378 it != lhs_stl_container.end(); ++it) {
2380 expected_.end()) {
2381 if (printed_header) {
2382 *os << ", ";
2383 } else {
2384 *os << "which has these unexpected elements: ";
2385 printed_header = true;
2386 }
2388 }
2389 }
2390
2391
2392 bool printed_header2 = false;
2393 for (typename StlContainer::const_iterator it = expected_.begin();
2394 it != expected_.end(); ++it) {
2396 lhs_stl_container.begin(), lhs_stl_container.end(), *it) ==
2397 lhs_stl_container.end()) {
2398 if (printed_header2) {
2399 *os << ", ";
2400 } else {
2401 *os << (printed_header ? ",\nand" : "which")
2402 << " doesn't have these expected elements: ";
2403 printed_header2 = true;
2404 }
2406 }
2407 }
2408 }
2409
2410 return false;
2411 }
2412
2413 private:
2414 const StlContainer expected_;
2415};
2416
2417
2418struct LessComparator {
2419 template <typename T, typename U>
2420 bool operator()(const T& lhs, const U& rhs) const { return lhs < rhs; }
2421};
2422
2423
2424template <typename Comparator, typename ContainerMatcher>
2425class WhenSortedByMatcher {
2426 public:
2427 WhenSortedByMatcher(const Comparator& comparator,
2428 const ContainerMatcher& matcher)
2429 : comparator_(comparator), matcher_(matcher) {}
2430
2431 template <typename LhsContainer>
2432 operator Matcher<LhsContainer>() const {
2433 return MakeMatcher(new Impl<LhsContainer>(comparator_, matcher_));
2434 }
2435
2436 template <typename LhsContainer>
2437 class Impl : public MatcherInterface<LhsContainer> {
2438 public:
2439 typedef internal::StlContainerView<
2442 typedef typename LhsView::const_reference LhsStlContainerReference;
2443
2444
2445 typedef typename RemoveConstFromKey<
2446 typename LhsStlContainer::value_type>
::type LhsValue;
2447
2448 Impl(const Comparator& comparator, const ContainerMatcher& matcher)
2449 : comparator_(comparator), matcher_(matcher) {}
2450
2451 void DescribeTo(::std::ostream* os) const override {
2452 *os << "(when sorted) ";
2453 matcher_.DescribeTo(os);
2454 }
2455
2456 void DescribeNegationTo(::std::ostream* os) const override {
2457 *os << "(when sorted) ";
2458 matcher_.DescribeNegationTo(os);
2459 }
2460
2461 bool MatchAndExplain(LhsContainer lhs,
2462 MatchResultListener* listener) const override {
2463 LhsStlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2464 ::std::vector<LhsValue> sorted_container(lhs_stl_container.begin(),
2465 lhs_stl_container.end());
2466 ::std::sort(
2467 sorted_container.begin(), sorted_container.end(), comparator_);
2468
2469 if (!listener->IsInterested()) {
2470
2471
2472 return matcher_.Matches(sorted_container);
2473 }
2474
2475 *listener << "which is ";
2477 *listener << " when sorted";
2478
2479 StringMatchResultListener inner_listener;
2480 const bool match = matcher_.MatchAndExplain(sorted_container,
2481 &inner_listener);
2482 PrintIfNotEmpty(inner_listener.str(), listener->stream());
2483 return match;
2484 }
2485
2486 private:
2487 const Comparator comparator_;
2488 const Matcher<const ::std::vector<LhsValue>&> matcher_;
2489
2491 };
2492
2493 private:
2494 const Comparator comparator_;
2495 const ContainerMatcher matcher_;
2496};
2497
2498
2499
2500
2501
2502template <typename TupleMatcher, typename RhsContainer>
2503class PointwiseMatcher {
2506 use_UnorderedPointwise_with_hash_tables);
2507
2508 public:
2509 typedef internal::StlContainerView<RhsContainer> RhsView;
2511 typedef typename RhsStlContainer::value_type RhsValue;
2512
2514 "RhsContainer type must not be const");
2516 "RhsContainer type must not be a reference");
2517
2518
2519
2520 PointwiseMatcher(const TupleMatcher& tuple_matcher, const RhsContainer& rhs)
2521 : tuple_matcher_(tuple_matcher), rhs_(RhsView::Copy(rhs)) {}
2522
2523 template <typename LhsContainer>
2524 operator Matcher<LhsContainer>() const {
2527 use_UnorderedPointwise_with_hash_tables);
2528
2529 return Matcher<LhsContainer>(
2530 new Impl<const LhsContainer&>(tuple_matcher_, rhs_));
2531 }
2532
2533 template <typename LhsContainer>
2534 class Impl : public MatcherInterface<LhsContainer> {
2535 public:
2536 typedef internal::StlContainerView<
2539 typedef typename LhsView::const_reference LhsStlContainerReference;
2540 typedef typename LhsStlContainer::value_type LhsValue;
2541
2542
2543
2544
2545 typedef ::std::tuple<const LhsValue&, const RhsValue&> InnerMatcherArg;
2546
2547 Impl(const TupleMatcher& tuple_matcher, const RhsStlContainer& rhs)
2548
2549 : mono_tuple_matcher_(SafeMatcherCast<InnerMatcherArg>(tuple_matcher)),
2550 rhs_(rhs) {}
2551
2552 void DescribeTo(::std::ostream* os) const override {
2553 *os << "contains " << rhs_.size()
2554 << " values, where each value and its corresponding value in ";
2556 *os << " ";
2557 mono_tuple_matcher_.DescribeTo(os);
2558 }
2559 void DescribeNegationTo(::std::ostream* os) const override {
2560 *os << "doesn't contain exactly " << rhs_.size()
2561 << " values, or contains a value x at some index i"
2562 << " where x and the i-th value of ";
2564 *os << " ";
2565 mono_tuple_matcher_.DescribeNegationTo(os);
2566 }
2567
2568 bool MatchAndExplain(LhsContainer lhs,
2569 MatchResultListener* listener) const override {
2570 LhsStlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2571 const size_t actual_size = lhs_stl_container.size();
2572 if (actual_size != rhs_.size()) {
2573 *listener << "which contains " << actual_size << " values";
2574 return false;
2575 }
2576
2577 typename LhsStlContainer::const_iterator left = lhs_stl_container.begin();
2578 typename RhsStlContainer::const_iterator right = rhs_.begin();
2579 for (
size_t i = 0;
i != actual_size; ++
i, ++left, ++right) {
2580 if (listener->IsInterested()) {
2581 StringMatchResultListener inner_listener;
2582
2583
2584
2585 if (!mono_tuple_matcher_.MatchAndExplain(
2586 InnerMatcherArg(ImplicitCast_<const LhsValue&>(*left),
2587 ImplicitCast_<const RhsValue&>(*right)),
2588 &inner_listener)) {
2589 *listener << "where the value pair (";
2591 *listener << ", ";
2593 *listener <<
") at index #" <<
i <<
" don't match";
2594 PrintIfNotEmpty(inner_listener.str(), listener->stream());
2595 return false;
2596 }
2597 } else {
2598 if (!mono_tuple_matcher_.Matches(
2599 InnerMatcherArg(ImplicitCast_<const LhsValue&>(*left),
2600 ImplicitCast_<const RhsValue&>(*right))))
2601 return false;
2602 }
2603 }
2604
2605 return true;
2606 }
2607
2608 private:
2609 const Matcher<InnerMatcherArg> mono_tuple_matcher_;
2610 const RhsStlContainer rhs_;
2611 };
2612
2613 private:
2614 const TupleMatcher tuple_matcher_;
2615 const RhsStlContainer rhs_;
2616};
2617
2618
2619template <typename Container>
2620class QuantifierMatcherImpl : public MatcherInterface<Container> {
2621 public:
2623 typedef StlContainerView<RawContainer> View;
2625 typedef typename View::const_reference StlContainerReference;
2626 typedef typename StlContainer::value_type Element;
2627
2628 template <typename InnerMatcher>
2629 explicit QuantifierMatcherImpl(InnerMatcher inner_matcher)
2630 : inner_matcher_(
2631 testing::SafeMatcherCast<
const Element&>(inner_matcher)) {}
2632
2633
2634
2635
2636 bool MatchAndExplainImpl(bool all_elements_should_match,
2637 Container container,
2638 MatchResultListener* listener) const {
2639 StlContainerReference stl_container = View::ConstReference(container);
2641 for (typename StlContainer::const_iterator it = stl_container.begin();
2642 it != stl_container.end(); ++it, ++
i) {
2643 StringMatchResultListener inner_listener;
2644 const bool matches = inner_matcher_.MatchAndExplain(*it, &inner_listener);
2645
2646 if (matches != all_elements_should_match) {
2647 *listener <<
"whose element #" <<
i
2648 << (matches ? " matches" : " doesn't match");
2649 PrintIfNotEmpty(inner_listener.str(), listener->stream());
2650 return !all_elements_should_match;
2651 }
2652 }
2653 return all_elements_should_match;
2654 }
2655
2656 protected:
2657 const Matcher<const Element&> inner_matcher_;
2658};
2659
2660
2661
2662template <typename Container>
2663class ContainsMatcherImpl : public QuantifierMatcherImpl<Container> {
2664 public:
2665 template <typename InnerMatcher>
2666 explicit ContainsMatcherImpl(InnerMatcher inner_matcher)
2667 : QuantifierMatcherImpl<Container>(inner_matcher) {}
2668
2669
2670 void DescribeTo(::std::ostream* os) const override {
2671 *os << "contains at least one element that ";
2672 this->inner_matcher_.DescribeTo(os);
2673 }
2674
2675 void DescribeNegationTo(::std::ostream* os) const override {
2676 *os << "doesn't contain any element that ";
2677 this->inner_matcher_.DescribeTo(os);
2678 }
2679
2680 bool MatchAndExplain(Container container,
2681 MatchResultListener* listener) const override {
2682 return this->MatchAndExplainImpl(false, container, listener);
2683 }
2684};
2685
2686
2687
2688template <typename Container>
2689class EachMatcherImpl : public QuantifierMatcherImpl<Container> {
2690 public:
2691 template <typename InnerMatcher>
2692 explicit EachMatcherImpl(InnerMatcher inner_matcher)
2693 : QuantifierMatcherImpl<Container>(inner_matcher) {}
2694
2695
2696 void DescribeTo(::std::ostream* os) const override {
2697 *os << "only contains elements that ";
2698 this->inner_matcher_.DescribeTo(os);
2699 }
2700
2701 void DescribeNegationTo(::std::ostream* os) const override {
2702 *os << "contains some element that ";
2703 this->inner_matcher_.DescribeNegationTo(os);
2704 }
2705
2706 bool MatchAndExplain(Container container,
2707 MatchResultListener* listener) const override {
2708 return this->MatchAndExplainImpl(true, container, listener);
2709 }
2710};
2711
2712
2713template <typename M>
2714class ContainsMatcher {
2715 public:
2716 explicit ContainsMatcher(M m) : inner_matcher_(m) {}
2717
2718 template <typename Container>
2719 operator Matcher<Container>() const {
2720 return Matcher<Container>(
2721 new ContainsMatcherImpl<const Container&>(inner_matcher_));
2722 }
2723
2724 private:
2725 const M inner_matcher_;
2726};
2727
2728
2729template <typename M>
2730class EachMatcher {
2731 public:
2732 explicit EachMatcher(M m) : inner_matcher_(m) {}
2733
2734 template <typename Container>
2735 operator Matcher<Container>() const {
2736 return Matcher<Container>(
2737 new EachMatcherImpl<const Container&>(inner_matcher_));
2738 }
2739
2740 private:
2741 const M inner_matcher_;
2742};
2743
2744struct Rank1 {};
2745struct Rank0 : Rank1 {};
2746
2747namespace pair_getters {
2748using std::get;
2749template <typename T>
2750auto First(T&
x, Rank1) ->
decltype(get<0>(
x)) {
2752}
2753template <typename T>
2754auto First(T&
x, Rank0) ->
decltype((
x.first)) {
2756}
2757
2758template <typename T>
2759auto Second(T&
x, Rank1) ->
decltype(get<1>(
x)) {
2761}
2762template <typename T>
2763auto Second(T&
x, Rank0) ->
decltype((
x.second)) {
2765}
2766}
2767
2768
2769
2770
2771
2772template <typename PairType>
2773class KeyMatcherImpl : public MatcherInterface<PairType> {
2774 public:
2776 typedef typename RawPairType::first_type KeyType;
2777
2778 template <typename InnerMatcher>
2779 explicit KeyMatcherImpl(InnerMatcher inner_matcher)
2780 : inner_matcher_(
2781 testing::SafeMatcherCast<
const KeyType&>(inner_matcher)) {
2782 }
2783
2784
2785
2786 bool MatchAndExplain(PairType key_value,
2787 MatchResultListener* listener) const override {
2788 StringMatchResultListener inner_listener;
2789 const bool match = inner_matcher_.MatchAndExplain(
2790 pair_getters::First(key_value, Rank0()), &inner_listener);
2791 const std::string explanation = inner_listener.str();
2792 if (explanation != "") {
2793 *listener << "whose first field is a value " << explanation;
2794 }
2795 return match;
2796 }
2797
2798
2799 void DescribeTo(::std::ostream* os) const override {
2800 *os << "has a key that ";
2801 inner_matcher_.DescribeTo(os);
2802 }
2803
2804
2805 void DescribeNegationTo(::std::ostream* os) const override {
2806 *os << "doesn't have a key that ";
2807 inner_matcher_.DescribeTo(os);
2808 }
2809
2810 private:
2811 const Matcher<const KeyType&> inner_matcher_;
2812};
2813
2814
2815template <typename M>
2816class KeyMatcher {
2817 public:
2818 explicit KeyMatcher(M m) : matcher_for_key_(m) {}
2819
2820 template <typename PairType>
2821 operator Matcher<PairType>() const {
2822 return Matcher<PairType>(
2823 new KeyMatcherImpl<const PairType&>(matcher_for_key_));
2824 }
2825
2826 private:
2827 const M matcher_for_key_;
2828};
2829
2830
2831template <typename InnerMatcher>
2832class AddressMatcher {
2833 public:
2834 explicit AddressMatcher(InnerMatcher m) : matcher_(m) {}
2835
2836 template <typename Type>
2837 operator Matcher<Type>() const {
2838 return Matcher<Type>(new Impl<const Type&>(matcher_));
2839 }
2840
2841 private:
2842
2843 template <typename Type>
2844 class Impl : public MatcherInterface<Type> {
2845 public:
2847 explicit Impl(const InnerMatcher& matcher)
2848 : matcher_(MatcherCast<Address>(matcher)) {}
2849
2850 void DescribeTo(::std::ostream* os) const override {
2851 *os << "has address that ";
2852 matcher_.DescribeTo(os);
2853 }
2854
2855 void DescribeNegationTo(::std::ostream* os) const override {
2856 *os << "does not have address that ";
2857 matcher_.DescribeTo(os);
2858 }
2859
2860 bool MatchAndExplain(Type object,
2861 MatchResultListener* listener) const override {
2862 *listener << "which has address ";
2863 Address address = std::addressof(object);
2864 return MatchPrintAndExplain(address, matcher_, listener);
2865 }
2866
2867 private:
2868 const Matcher<Address> matcher_;
2869 };
2870 const InnerMatcher matcher_;
2871};
2872
2873
2874
2875template <typename PairType>
2876class PairMatcherImpl : public MatcherInterface<PairType> {
2877 public:
2879 typedef typename RawPairType::first_type FirstType;
2880 typedef typename RawPairType::second_type SecondType;
2881
2882 template <typename FirstMatcher, typename SecondMatcher>
2883 PairMatcherImpl(FirstMatcher first_matcher, SecondMatcher second_matcher)
2884 : first_matcher_(
2885 testing::SafeMatcherCast<
const FirstType&>(first_matcher)),
2886 second_matcher_(
2887 testing::SafeMatcherCast<
const SecondType&>(second_matcher)) {
2888 }
2889
2890
2891 void DescribeTo(::std::ostream* os) const override {
2892 *os << "has a first field that ";
2893 first_matcher_.DescribeTo(os);
2894 *os << ", and has a second field that ";
2895 second_matcher_.DescribeTo(os);
2896 }
2897
2898
2899 void DescribeNegationTo(::std::ostream* os) const override {
2900 *os << "has a first field that ";
2901 first_matcher_.DescribeNegationTo(os);
2902 *os << ", or has a second field that ";
2903 second_matcher_.DescribeNegationTo(os);
2904 }
2905
2906
2907
2908 bool MatchAndExplain(PairType a_pair,
2909 MatchResultListener* listener) const override {
2910 if (!listener->IsInterested()) {
2911
2912
2913 return first_matcher_.Matches(pair_getters::First(a_pair, Rank0())) &&
2914 second_matcher_.Matches(pair_getters::Second(a_pair, Rank0()));
2915 }
2916 StringMatchResultListener first_inner_listener;
2917 if (!first_matcher_.MatchAndExplain(pair_getters::First(a_pair, Rank0()),
2918 &first_inner_listener)) {
2919 *listener << "whose first field does not match";
2920 PrintIfNotEmpty(first_inner_listener.str(), listener->stream());
2921 return false;
2922 }
2923 StringMatchResultListener second_inner_listener;
2924 if (!second_matcher_.MatchAndExplain(pair_getters::Second(a_pair, Rank0()),
2925 &second_inner_listener)) {
2926 *listener << "whose second field does not match";
2927 PrintIfNotEmpty(second_inner_listener.str(), listener->stream());
2928 return false;
2929 }
2930 ExplainSuccess(first_inner_listener.str(), second_inner_listener.str(),
2931 listener);
2932 return true;
2933 }
2934
2935 private:
2936 void ExplainSuccess(const std::string& first_explanation,
2937 const std::string& second_explanation,
2938 MatchResultListener* listener) const {
2939 *listener << "whose both fields match";
2940 if (first_explanation != "") {
2941 *listener << ", where the first field is a value " << first_explanation;
2942 }
2943 if (second_explanation != "") {
2944 *listener << ", ";
2945 if (first_explanation != "") {
2946 *listener << "and ";
2947 } else {
2948 *listener << "where ";
2949 }
2950 *listener << "the second field is a value " << second_explanation;
2951 }
2952 }
2953
2954 const Matcher<const FirstType&> first_matcher_;
2955 const Matcher<const SecondType&> second_matcher_;
2956};
2957
2958
2959template <typename FirstMatcher, typename SecondMatcher>
2960class PairMatcher {
2961 public:
2962 PairMatcher(FirstMatcher first_matcher, SecondMatcher second_matcher)
2963 : first_matcher_(first_matcher), second_matcher_(second_matcher) {}
2964
2965 template <typename PairType>
2966 operator Matcher<PairType> () const {
2967 return Matcher<PairType>(
2968 new PairMatcherImpl<const PairType&>(first_matcher_, second_matcher_));
2969 }
2970
2971 private:
2972 const FirstMatcher first_matcher_;
2973 const SecondMatcher second_matcher_;
2974};
2975
2976template <typename T, size_t... I>
2977auto UnpackStructImpl(const T& t, IndexSequence<I...>, int)
2978 -> decltype(std::tie(get<I>(t)...)) {
2980 "Number of arguments doesn't match the number of fields.");
2981 return std::tie(get<I>(t)...);
2982}
2983
2984#if defined(__cpp_structured_bindings) && __cpp_structured_bindings >= 201606
2985template <typename T>
2986auto UnpackStructImpl(const T& t, MakeIndexSequence<1>, char) {
2987 const auto& [a] = t;
2988 return std::tie(a);
2989}
2990template <typename T>
2991auto UnpackStructImpl(const T& t, MakeIndexSequence<2>, char) {
2992 const auto& [a, b] = t;
2993 return std::tie(a, b);
2994}
2995template <typename T>
2996auto UnpackStructImpl(const T& t, MakeIndexSequence<3>, char) {
2997 const auto& [a, b, c] = t;
2998 return std::tie(a, b, c);
2999}
3000template <typename T>
3001auto UnpackStructImpl(const T& t, MakeIndexSequence<4>, char) {
3002 const auto& [a, b, c, d] = t;
3003 return std::tie(a, b, c, d);
3004}
3005template <typename T>
3006auto UnpackStructImpl(const T& t, MakeIndexSequence<5>, char) {
3007 const auto& [a, b, c, d, e] = t;
3008 return std::tie(a, b, c, d, e);
3009}
3010template <typename T>
3011auto UnpackStructImpl(const T& t, MakeIndexSequence<6>, char) {
3012 const auto& [a, b, c, d, e, f] = t;
3013 return std::tie(a, b, c, d, e, f);
3014}
3015template <typename T>
3016auto UnpackStructImpl(const T& t, MakeIndexSequence<7>, char) {
3017 const auto& [a, b, c, d, e, f, g] = t;
3018 return std::tie(a, b, c, d, e, f, g);
3019}
3020template <typename T>
3021auto UnpackStructImpl(const T& t, MakeIndexSequence<8>, char) {
3022 const auto& [a, b, c, d, e, f, g, h] = t;
3023 return std::tie(a, b, c, d, e, f, g, h);
3024}
3025template <typename T>
3026auto UnpackStructImpl(const T& t, MakeIndexSequence<9>, char) {
3027 const auto& [a, b, c, d, e, f, g, h,
i] = t;
3028 return std::tie(a, b, c, d, e, f, g, h,
i);
3029}
3030template <typename T>
3031auto UnpackStructImpl(const T& t, MakeIndexSequence<10>, char) {
3032 const auto& [a, b, c, d, e, f, g, h,
i, j] = t;
3033 return std::tie(a, b, c, d, e, f, g, h,
i, j);
3034}
3035template <typename T>
3036auto UnpackStructImpl(const T& t, MakeIndexSequence<11>, char) {
3037 const auto& [a, b, c, d, e, f, g, h,
i, j, k] = t;
3038 return std::tie(a, b, c, d, e, f, g, h,
i, j, k);
3039}
3040template <typename T>
3041auto UnpackStructImpl(const T& t, MakeIndexSequence<12>, char) {
3042 const auto& [a, b, c, d, e, f, g, h,
i, j, k, l] = t;
3043 return std::tie(a, b, c, d, e, f, g, h,
i, j, k, l);
3044}
3045template <typename T>
3046auto UnpackStructImpl(const T& t, MakeIndexSequence<13>, char) {
3047 const auto& [a, b, c, d, e, f, g, h,
i, j, k, l, m] = t;
3048 return std::tie(a, b, c, d, e, f, g, h,
i, j, k, l, m);
3049}
3050template <typename T>
3051auto UnpackStructImpl(const T& t, MakeIndexSequence<14>, char) {
3052 const auto& [a, b, c, d, e, f, g, h,
i, j, k, l, m, n] = t;
3053 return std::tie(a, b, c, d, e, f, g, h,
i, j, k, l, m, n);
3054}
3055template <typename T>
3056auto UnpackStructImpl(const T& t, MakeIndexSequence<15>, char) {
3057 const auto& [a, b, c, d, e, f, g, h,
i, j, k, l, m, n, o] = t;
3058 return std::tie(a, b, c, d, e, f, g, h,
i, j, k, l, m, n, o);
3059}
3060template <typename T>
3061auto UnpackStructImpl(const T& t, MakeIndexSequence<16>, char) {
3062 const auto& [a, b, c, d, e, f, g, h,
i, j, k, l, m, n, o,
p] = t;
3063 return std::tie(a, b, c, d, e, f, g, h,
i, j, k, l, m, n, o,
p);
3064}
3065#endif
3066
3067template <size_t I, typename T>
3068auto UnpackStruct(const T& t)
3069 -> decltype((UnpackStructImpl)(t, MakeIndexSequence<I>{}, 0)) {
3070 return (UnpackStructImpl)(t, MakeIndexSequence<I>{}, 0);
3071}
3072
3073
3074
3075
3076template <typename T, size_t N>
3077void VariadicExpand(const T (&)[N]) {}
3078
3079template <typename Struct, typename StructSize>
3080class FieldsAreMatcherImpl;
3081
3082template <typename Struct, size_t... I>
3083class FieldsAreMatcherImpl<Struct, IndexSequence<I...>>
3084 : public MatcherInterface<Struct> {
3085 using UnpackedType =
3086 decltype(UnpackStruct<sizeof...(I)>(std::declval<const Struct&>()));
3087 using MatchersType = std::tuple<
3089
3090 public:
3091 template <typename Inner>
3092 explicit FieldsAreMatcherImpl(const Inner& matchers)
3093 : matchers_(
testing::SafeMatcherCast<
3094 const typename
std::tuple_element<I, UnpackedType>::
type&>(
3095 std::get<I>(matchers))...) {}
3096
3097 void DescribeTo(::std::ostream* os) const override {
3098 const char* separator = "";
3099 VariadicExpand(
3100 {(*os << separator << "has field #" << I << " that ",
3101 std::get<I>(matchers_).DescribeTo(os), separator = ", and ")...});
3102 }
3103
3104 void DescribeNegationTo(::std::ostream* os) const override {
3105 const char* separator = "";
3106 VariadicExpand({(*os << separator << "has field #" << I << " that ",
3107 std::get<I>(matchers_).DescribeNegationTo(os),
3108 separator = ", or ")...});
3109 }
3110
3111 bool MatchAndExplain(Struct t, MatchResultListener* listener) const override {
3112 return MatchInternal((UnpackStruct<sizeof...(I)>)(t), listener);
3113 }
3114
3115 private:
3116 bool MatchInternal(UnpackedType tuple, MatchResultListener* listener) const {
3117 if (!listener->IsInterested()) {
3118
3119
3120 bool good = true;
3121 VariadicExpand({good = good && std::get<I>(matchers_).Matches(
3122 std::get<I>(tuple))...});
3123 return good;
3124 }
3125
3126 size_t failed_pos = ~size_t{};
3127
3128 std::vector<StringMatchResultListener> inner_listener(sizeof...(I));
3129
3130 VariadicExpand(
3131 {failed_pos == ~size_t{} && !std::get<I>(matchers_).MatchAndExplain(
3132 std::get<I>(tuple), &inner_listener[I])
3133 ? failed_pos = I
3134 : 0 ...});
3135 if (failed_pos != ~size_t{}) {
3136 *listener << "whose field #" << failed_pos << " does not match";
3137 PrintIfNotEmpty(inner_listener[failed_pos].str(), listener->stream());
3138 return false;
3139 }
3140
3141 *listener << "whose all elements match";
3142 const char* separator = ", where";
3143 for (size_t index = 0; index < sizeof...(I); ++index) {
3144 const std::string str = inner_listener[index].str();
3145 if (!str.empty()) {
3146 *listener << separator << " field #" << index << " is a value " << str;
3147 separator = ", and";
3148 }
3149 }
3150
3151 return true;
3152 }
3153
3154 MatchersType matchers_;
3155};
3156
3157template <typename... Inner>
3158class FieldsAreMatcher {
3159 public:
3160 explicit FieldsAreMatcher(Inner... inner) : matchers_(
std::move(inner)...) {}
3161
3162 template <typename Struct>
3163 operator Matcher<Struct>() const {
3164 return Matcher<Struct>(
3165 new FieldsAreMatcherImpl<const Struct&, IndexSequenceFor<Inner...>>(
3166 matchers_));
3167 }
3168
3169 private:
3170 std::tuple<Inner...> matchers_;
3171};
3172
3173
3174template <typename Container>
3175class ElementsAreMatcherImpl : public MatcherInterface<Container> {
3176 public:
3178 typedef internal::StlContainerView<RawContainer> View;
3180 typedef typename View::const_reference StlContainerReference;
3181 typedef typename StlContainer::value_type Element;
3182
3183
3184
3185 template <typename InputIter>
3186 ElementsAreMatcherImpl(InputIter first, InputIter
last) {
3187 while (first !=
last) {
3188 matchers_.push_back(MatcherCast<const Element&>(*first++));
3189 }
3190 }
3191
3192
3193 void DescribeTo(::std::ostream* os) const override {
3195 *os << "is empty";
3196 }
else if (
count() == 1) {
3197 *os << "has 1 element that ";
3198 matchers_[0].DescribeTo(os);
3199 } else {
3200 *os <<
"has " << Elements(
count()) <<
" where\n";
3201 for (
size_t i = 0;
i !=
count(); ++
i) {
3202 *os <<
"element #" <<
i <<
" ";
3203 matchers_[
i].DescribeTo(os);
3205 *os << ",\n";
3206 }
3207 }
3208 }
3209 }
3210
3211
3212 void DescribeNegationTo(::std::ostream* os) const override {
3214 *os << "isn't empty";
3215 return;
3216 }
3217
3218 *os <<
"doesn't have " << Elements(
count()) <<
", or\n";
3219 for (
size_t i = 0;
i !=
count(); ++
i) {
3220 *os <<
"element #" <<
i <<
" ";
3221 matchers_[
i].DescribeNegationTo(os);
3223 *os << ", or\n";
3224 }
3225 }
3226 }
3227
3228 bool MatchAndExplain(Container container,
3229 MatchResultListener* listener) const override {
3230
3231
3232
3233 const bool listener_interested = listener->IsInterested();
3234
3235
3236 ::std::vector<std::string> explanations(
count());
3237 StlContainerReference stl_container = View::ConstReference(container);
3238 typename StlContainer::const_iterator it = stl_container.begin();
3239 size_t exam_pos = 0;
3240 bool mismatch_found = false;
3241
3242
3243
3244
3245 for (; it != stl_container.end() && exam_pos !=
count(); ++it, ++exam_pos) {
3246 bool match;
3247 if (listener_interested) {
3248 StringMatchResultListener s;
3249 match = matchers_[exam_pos].MatchAndExplain(*it, &s);
3250 explanations[exam_pos] = s.str();
3251 } else {
3252 match = matchers_[exam_pos].Matches(*it);
3253 }
3254
3255 if (!match) {
3256 mismatch_found = true;
3257 break;
3258 }
3259 }
3260
3261
3262
3263
3264
3265 size_t actual_count = exam_pos;
3266 for (; it != stl_container.end(); ++it) {
3267 ++actual_count;
3268 }
3269
3270 if (actual_count !=
count()) {
3271
3272
3273
3274
3275 if (listener_interested && (actual_count != 0)) {
3276 *listener << "which has " << Elements(actual_count);
3277 }
3278 return false;
3279 }
3280
3281 if (mismatch_found) {
3282
3283 if (listener_interested) {
3284 *listener << "whose element #" << exam_pos << " doesn't match";
3285 PrintIfNotEmpty(explanations[exam_pos], listener->stream());
3286 }
3287 return false;
3288 }
3289
3290
3291
3292 if (listener_interested) {
3293 bool reason_printed = false;
3294 for (
size_t i = 0;
i !=
count(); ++
i) {
3295 const std::string& s = explanations[
i];
3296 if (!s.empty()) {
3297 if (reason_printed) {
3298 *listener << ",\nand ";
3299 }
3300 *listener <<
"whose element #" <<
i <<
" matches, " << s;
3301 reason_printed = true;
3302 }
3303 }
3304 }
3305 return true;
3306 }
3307
3308 private:
3309 static Message Elements(
size_t count) {
3310 return Message() <<
count << (
count == 1 ?
" element" :
" elements");
3311 }
3312
3313 size_t count()
const {
return matchers_.size(); }
3314
3315 ::std::vector<Matcher<const Element&> > matchers_;
3316};
3317
3318
3319
3320
3321
3323 public:
3324 MatchMatrix(size_t num_elements, size_t num_matchers)
3325 : num_elements_(num_elements),
3326 num_matchers_(num_matchers),
3327 matched_(num_elements_* num_matchers_, 0) {
3328 }
3329
3330 size_t LhsSize() const { return num_elements_; }
3331 size_t RhsSize() const { return num_matchers_; }
3332 bool HasEdge(size_t ilhs, size_t irhs) const {
3333 return matched_[SpaceIndex(ilhs, irhs)] == 1;
3334 }
3335 void SetEdge(size_t ilhs, size_t irhs, bool b) {
3336 matched_[SpaceIndex(ilhs, irhs)] = b ? 1 : 0;
3337 }
3338
3339
3340
3341
3342 bool NextGraph();
3343
3344 void Randomize();
3345
3346 std::string DebugString() const;
3347
3348 private:
3349 size_t SpaceIndex(size_t ilhs, size_t irhs) const {
3350 return ilhs * num_matchers_ + irhs;
3351 }
3352
3353 size_t num_elements_;
3354 size_t num_matchers_;
3355
3356
3357
3358
3359 ::std::vector<char> matched_;
3360};
3361
3362typedef ::std::pair<size_t, size_t> ElementMatcherPair;
3363typedef ::std::vector<ElementMatcherPair> ElementMatcherPairs;
3364
3365
3366
3369
3370struct UnorderedMatcherRequire {
3372 Superset = 1 << 0,
3373 Subset = 1 << 1,
3374 ExactMatch = Superset | Subset,
3375 };
3376};
3377
3378
3379
3380
3381class GTEST_API_ UnorderedElementsAreMatcherImplBase {
3382 protected:
3383 explicit UnorderedElementsAreMatcherImplBase(
3385 : match_flags_(matcher_flags) {}
3386
3387
3388
3389
3390 typedef ::std::vector<const MatcherDescriberInterface*> MatcherDescriberVec;
3391
3392
3393 void DescribeToImpl(::std::ostream* os) const;
3394
3395
3396 void DescribeNegationToImpl(::std::ostream* os) const;
3397
3398 bool VerifyMatchMatrix(const ::std::vector<std::string>& element_printouts,
3399 const MatchMatrix& matrix,
3400 MatchResultListener* listener) const;
3401
3402 bool FindPairing(const MatchMatrix& matrix,
3403 MatchResultListener* listener) const;
3404
3405 MatcherDescriberVec& matcher_describers() {
3406 return matcher_describers_;
3407 }
3408
3409 static Message Elements(size_t n) {
3410 return Message() << n << " element" << (n == 1 ? "" : "s");
3411 }
3412
3414
3415 private:
3417 MatcherDescriberVec matcher_describers_;
3418};
3419
3420
3421
3422template <typename Container>
3423class UnorderedElementsAreMatcherImpl
3424 : public MatcherInterface<Container>,
3425 public UnorderedElementsAreMatcherImplBase {
3426 public:
3428 typedef internal::StlContainerView<RawContainer> View;
3430 typedef typename View::const_reference StlContainerReference;
3431 typedef typename StlContainer::const_iterator StlContainerConstIterator;
3432 typedef typename StlContainer::value_type Element;
3433
3434 template <typename InputIter>
3436 InputIter first, InputIter
last)
3437 : UnorderedElementsAreMatcherImplBase(matcher_flags) {
3438 for (; first !=
last; ++first) {
3439 matchers_.push_back(MatcherCast<const Element&>(*first));
3440 }
3441 for (const auto& m : matchers_) {
3442 matcher_describers().push_back(m.GetDescriber());
3443 }
3444 }
3445
3446
3447 void DescribeTo(::std::ostream* os) const override {
3448 return UnorderedElementsAreMatcherImplBase::DescribeToImpl(os);
3449 }
3450
3451
3452 void DescribeNegationTo(::std::ostream* os) const override {
3453 return UnorderedElementsAreMatcherImplBase::DescribeNegationToImpl(os);
3454 }
3455
3456 bool MatchAndExplain(Container container,
3457 MatchResultListener* listener) const override {
3458 StlContainerReference stl_container = View::ConstReference(container);
3459 ::std::vector<std::string> element_printouts;
3460 MatchMatrix matrix =
3461 AnalyzeElements(stl_container.begin(), stl_container.end(),
3462 &element_printouts, listener);
3463
3464 if (matrix.LhsSize() == 0 && matrix.RhsSize() == 0) {
3465 return true;
3466 }
3467
3468 if (match_flags() == UnorderedMatcherRequire::ExactMatch) {
3469 if (matrix.LhsSize() != matrix.RhsSize()) {
3470
3471
3472
3473
3474 if (matrix.LhsSize() != 0 && listener->IsInterested()) {
3475 *listener << "which has " << Elements(matrix.LhsSize());
3476 }
3477 return false;
3478 }
3479 }
3480
3481 return VerifyMatchMatrix(element_printouts, matrix, listener) &&
3482 FindPairing(matrix, listener);
3483 }
3484
3485 private:
3486 template <typename ElementIter>
3487 MatchMatrix AnalyzeElements(ElementIter elem_first, ElementIter elem_last,
3488 ::std::vector<std::string>* element_printouts,
3489 MatchResultListener* listener) const {
3490 element_printouts->clear();
3491 ::std::vector<char> did_match;
3492 size_t num_elements = 0;
3493 DummyMatchResultListener dummy;
3494 for (; elem_first != elem_last; ++num_elements, ++elem_first) {
3495 if (listener->IsInterested()) {
3497 }
3498 for (size_t irhs = 0; irhs != matchers_.size(); ++irhs) {
3499 did_match.push_back(
3500 matchers_[irhs].MatchAndExplain(*elem_first, &dummy));
3501 }
3502 }
3503
3504 MatchMatrix matrix(num_elements, matchers_.size());
3505 ::std::vector<char>::const_iterator did_match_iter = did_match.begin();
3506 for (size_t ilhs = 0; ilhs != num_elements; ++ilhs) {
3507 for (size_t irhs = 0; irhs != matchers_.size(); ++irhs) {
3508 matrix.SetEdge(ilhs, irhs, *did_match_iter++ != 0);
3509 }
3510 }
3511 return matrix;
3512 }
3513
3514 ::std::vector<Matcher<const Element&> > matchers_;
3515};
3516
3517
3518
3519template <typename Target>
3520struct CastAndAppendTransform {
3521 template <typename Arg>
3522 Matcher<Target> operator()(const Arg& a) const {
3523 return MatcherCast<Target>(a);
3524 }
3525};
3526
3527
3528template <typename MatcherTuple>
3529class UnorderedElementsAreMatcher {
3530 public:
3531 explicit UnorderedElementsAreMatcher(const MatcherTuple& args)
3532 : matchers_(args) {}
3533
3534 template <typename Container>
3535 operator Matcher<Container>() const {
3538 typedef typename View::value_type Element;
3539 typedef ::std::vector<Matcher<const Element&> > MatcherVec;
3540 MatcherVec matchers;
3542 TransformTupleValues(CastAndAppendTransform<const Element&>(), matchers_,
3543 ::std::back_inserter(matchers));
3544 return Matcher<Container>(
3545 new UnorderedElementsAreMatcherImpl<const Container&>(
3546 UnorderedMatcherRequire::ExactMatch, matchers.begin(),
3547 matchers.end()));
3548 }
3549
3550 private:
3551 const MatcherTuple matchers_;
3552};
3553
3554
3555template <typename MatcherTuple>
3556class ElementsAreMatcher {
3557 public:
3558 explicit ElementsAreMatcher(const MatcherTuple& args) : matchers_(args) {}
3559
3560 template <typename Container>
3561 operator Matcher<Container>() const {
3565 use_UnorderedElementsAre_with_hash_tables);
3566
3569 typedef typename View::value_type Element;
3570 typedef ::std::vector<Matcher<const Element&> > MatcherVec;
3571 MatcherVec matchers;
3573 TransformTupleValues(CastAndAppendTransform<const Element&>(), matchers_,
3574 ::std::back_inserter(matchers));
3575 return Matcher<Container>(new ElementsAreMatcherImpl<const Container&>(
3576 matchers.begin(), matchers.end()));
3577 }
3578
3579 private:
3580 const MatcherTuple matchers_;
3581};
3582
3583
3584template <typename T>
3585class UnorderedElementsAreArrayMatcher {
3586 public:
3587 template <typename Iter>
3590 : match_flags_(match_flags), matchers_(first,
last) {}
3591
3592 template <typename Container>
3593 operator Matcher<Container>() const {
3594 return Matcher<Container>(
3595 new UnorderedElementsAreMatcherImpl<const Container&>(
3596 match_flags_, matchers_.begin(), matchers_.end()));
3597 }
3598
3599 private:
3601 ::std::vector<T> matchers_;
3602};
3603
3604
3605template <typename T>
3606class ElementsAreArrayMatcher {
3607 public:
3608 template <typename Iter>
3610
3611 template <typename Container>
3612 operator Matcher<Container>() const {
3615 use_UnorderedElementsAreArray_with_hash_tables);
3616
3617 return Matcher<Container>(new ElementsAreMatcherImpl<const Container&>(
3618 matchers_.begin(), matchers_.end()));
3619 }
3620
3621 private:
3622 const ::std::vector<T> matchers_;
3623};
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634template <typename Tuple2Matcher, typename Second>
3635class BoundSecondMatcher {
3636 public:
3637 BoundSecondMatcher(const Tuple2Matcher& tm, const Second& second)
3638 : tuple2_matcher_(tm), second_value_(second) {}
3639
3640 BoundSecondMatcher(const BoundSecondMatcher& other) = default;
3641
3642 template <typename T>
3643 operator Matcher<T>() const {
3644 return MakeMatcher(new Impl<T>(tuple2_matcher_, second_value_));
3645 }
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655 void operator=(const BoundSecondMatcher& ) {
3657 }
3658
3659 private:
3660 template <typename T>
3661 class Impl : public MatcherInterface<T> {
3662 public:
3663 typedef ::std::tuple<T, Second> ArgTuple;
3664
3665 Impl(const Tuple2Matcher& tm, const Second& second)
3666 : mono_tuple2_matcher_(SafeMatcherCast<
const ArgTuple&>(tm)),
3667 second_value_(second) {}
3668
3669 void DescribeTo(::std::ostream* os) const override {
3670 *os << "and ";
3672 *os << " ";
3673 mono_tuple2_matcher_.DescribeTo(os);
3674 }
3675
3676 bool MatchAndExplain(T
x, MatchResultListener* listener)
const override {
3677 return mono_tuple2_matcher_.MatchAndExplain(ArgTuple(
x, second_value_),
3678 listener);
3679 }
3680
3681 private:
3682 const Matcher<const ArgTuple&> mono_tuple2_matcher_;
3683 const Second second_value_;
3684 };
3685
3686 const Tuple2Matcher tuple2_matcher_;
3687 const Second second_value_;
3688};
3689
3690
3691
3692
3693
3694template <typename Tuple2Matcher, typename Second>
3695BoundSecondMatcher<Tuple2Matcher, Second> MatcherBindSecond(
3696 const Tuple2Matcher& tm, const Second& second) {
3697 return BoundSecondMatcher<Tuple2Matcher, Second>(tm, second);
3698}
3699
3700
3701
3702
3703
3704
3706 const char* matcher_name,
3708
3709
3710template <typename ValueMatcher>
3711class OptionalMatcher {
3712 public:
3713 explicit OptionalMatcher(const ValueMatcher& value_matcher)
3714 : value_matcher_(value_matcher) {}
3715
3716 template <typename Optional>
3717 operator Matcher<Optional>() const {
3718 return Matcher<Optional>(new Impl<const Optional&>(value_matcher_));
3719 }
3720
3721 template <typename Optional>
3722 class Impl : public MatcherInterface<Optional> {
3723 public:
3725 typedef typename OptionalView::value_type ValueType;
3726 explicit Impl(const ValueMatcher& value_matcher)
3727 : value_matcher_(MatcherCast<ValueType>(value_matcher)) {}
3728
3729 void DescribeTo(::std::ostream* os) const override {
3730 *os << "value ";
3731 value_matcher_.DescribeTo(os);
3732 }
3733
3734 void DescribeNegationTo(::std::ostream* os) const override {
3735 *os << "value ";
3736 value_matcher_.DescribeNegationTo(os);
3737 }
3738
3739 bool MatchAndExplain(Optional optional,
3740 MatchResultListener* listener) const override {
3741 if (!optional) {
3742 *listener << "which is not engaged";
3743 return false;
3744 }
3745 const ValueType&
value = *optional;
3746 StringMatchResultListener value_listener;
3747 const bool match = value_matcher_.MatchAndExplain(
value, &value_listener);
3749 << (match ? " matches" : " doesn't match");
3750 PrintIfNotEmpty(value_listener.str(), listener->stream());
3751 return match;
3752 }
3753
3754 private:
3755 const Matcher<ValueType> value_matcher_;
3756 };
3757
3758 private:
3759 const ValueMatcher value_matcher_;
3760};
3761
3762namespace variant_matcher {
3763
3764template <typename T>
3765void holds_alternative() {}
3766template <typename T>
3767void get() {}
3768
3769
3770template <typename T>
3771class VariantMatcher {
3772 public:
3774 : matcher_(
std::move(matcher)) {}
3775
3776 template <typename Variant>
3777 bool MatchAndExplain(
const Variant&
value,
3778 ::testing::MatchResultListener* listener) const {
3779 using std::get;
3780 if (!listener->IsInterested()) {
3781 return holds_alternative<T>(
value) && matcher_.Matches(get<T>(
value));
3782 }
3783
3784 if (!holds_alternative<T>(
value)) {
3785 *listener <<
"whose value is not of type '" <<
GetTypeName() <<
"'";
3786 return false;
3787 }
3788
3789 const T& elem = get<T>(
value);
3790 StringMatchResultListener elem_listener;
3791 const bool match = matcher_.MatchAndExplain(elem, &elem_listener);
3793 << (match ? " matches" : " doesn't match");
3794 PrintIfNotEmpty(elem_listener.str(), listener->stream());
3795 return match;
3796 }
3797
3798 void DescribeTo(std::ostream* os) const {
3799 *os <<
"is a variant<> with value of type '" <<
GetTypeName()
3800 << "' and the value ";
3801 matcher_.DescribeTo(os);
3802 }
3803
3804 void DescribeNegationTo(std::ostream* os) const {
3805 *os <<
"is a variant<> with value of type other than '" <<
GetTypeName()
3806 << "' or the value ";
3807 matcher_.DescribeNegationTo(os);
3808 }
3809
3810 private:
3812#if GTEST_HAS_RTTI
3814 return internal::GetTypeName<T>());
3815#endif
3816 return "the element type";
3817 }
3818
3819 const ::testing::Matcher<const T&> matcher_;
3820};
3821
3822}
3823
3824namespace any_cast_matcher {
3825
3826
3827template <typename T>
3828void any_cast() {}
3829
3830
3831template <typename T>
3832class AnyCastMatcher {
3833 public:
3834 explicit AnyCastMatcher(const ::testing::Matcher<const T&>& matcher)
3835 : matcher_(matcher) {}
3836
3837 template <typename AnyType>
3838 bool MatchAndExplain(
const AnyType&
value,
3839 ::testing::MatchResultListener* listener) const {
3840 if (!listener->IsInterested()) {
3841 const T* ptr = any_cast<T>(&
value);
3842 return ptr != nullptr && matcher_.Matches(*ptr);
3843 }
3844
3845 const T* elem = any_cast<T>(&
value);
3846 if (elem == nullptr) {
3847 *listener <<
"whose value is not of type '" <<
GetTypeName() <<
"'";
3848 return false;
3849 }
3850
3851 StringMatchResultListener elem_listener;
3852 const bool match = matcher_.MatchAndExplain(*elem, &elem_listener);
3854 << (match ? " matches" : " doesn't match");
3855 PrintIfNotEmpty(elem_listener.str(), listener->stream());
3856 return match;
3857 }
3858
3859 void DescribeTo(std::ostream* os) const {
3860 *os <<
"is an 'any' type with value of type '" <<
GetTypeName()
3861 << "' and the value ";
3862 matcher_.DescribeTo(os);
3863 }
3864
3865 void DescribeNegationTo(std::ostream* os) const {
3866 *os <<
"is an 'any' type with value of type other than '" <<
GetTypeName()
3867 << "' or the value ";
3868 matcher_.DescribeNegationTo(os);
3869 }
3870
3871 private:
3873#if GTEST_HAS_RTTI
3875 return internal::GetTypeName<T>());
3876#endif
3877 return "the element type";
3878 }
3879
3880 const ::testing::Matcher<const T&> matcher_;
3881};
3882
3883}
3884
3885
3886template <class ArgsTuple, size_t... k>
3887class ArgsMatcherImpl : public MatcherInterface<ArgsTuple> {
3888 public:
3890 using SelectedArgs =
3892 using MonomorphicInnerMatcher = Matcher<const SelectedArgs&>;
3893
3894 template <typename InnerMatcher>
3895 explicit ArgsMatcherImpl(const InnerMatcher& inner_matcher)
3896 : inner_matcher_(SafeMatcherCast<
const SelectedArgs&>(inner_matcher)) {}
3897
3898 bool MatchAndExplain(ArgsTuple args,
3899 MatchResultListener* listener) const override {
3900
3901 (void)args;
3902 const SelectedArgs& selected_args =
3903 std::forward_as_tuple(std::get<k>(args)...);
3904 if (!listener->IsInterested()) return inner_matcher_.Matches(selected_args);
3905
3906 PrintIndices(listener->stream());
3908
3909 StringMatchResultListener inner_listener;
3910 const bool match =
3911 inner_matcher_.MatchAndExplain(selected_args, &inner_listener);
3912 PrintIfNotEmpty(inner_listener.str(), listener->stream());
3913 return match;
3914 }
3915
3916 void DescribeTo(::std::ostream* os) const override {
3917 *os << "are a tuple ";
3918 PrintIndices(os);
3919 inner_matcher_.DescribeTo(os);
3920 }
3921
3922 void DescribeNegationTo(::std::ostream* os) const override {
3923 *os << "are a tuple ";
3924 PrintIndices(os);
3925 inner_matcher_.DescribeNegationTo(os);
3926 }
3927
3928 private:
3929
3930 static void PrintIndices(::std::ostream* os) {
3931 *os << "whose fields (";
3932 const char* sep = "";
3933
3934 (void)sep;
3935 const char* dummy[] = {"", (*os << sep << "#" << k, sep = ", ")...};
3936 (void)dummy;
3937 *os << ") ";
3938 }
3939
3940 MonomorphicInnerMatcher inner_matcher_;
3941};
3942
3943template <class InnerMatcher, size_t... k>
3944class ArgsMatcher {
3945 public:
3946 explicit ArgsMatcher(InnerMatcher inner_matcher)
3947 : inner_matcher_(
std::move(inner_matcher)) {}
3948
3949 template <typename ArgsTuple>
3950 operator Matcher<ArgsTuple>() const {
3951 return MakeMatcher(new ArgsMatcherImpl<ArgsTuple, k...>(inner_matcher_));
3952 }
3953
3954 private:
3955 InnerMatcher inner_matcher_;
3956};
3957
3958}
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975template <typename Iter>
3976inline internal::ElementsAreArrayMatcher<
3977 typename ::std::iterator_traits<Iter>::value_type>
3979 typedef typename ::std::iterator_traits<Iter>::value_type T;
3980 return internal::ElementsAreArrayMatcher<T>(first,
last);
3981}
3982
3983template <typename T>
3984inline internal::ElementsAreArrayMatcher<T> ElementsAreArray(
3985 const T* pointer,
size_t count) {
3986 return ElementsAreArray(pointer, pointer +
count);
3987}
3988
3989template <typename T, size_t N>
3990inline internal::ElementsAreArrayMatcher<T> ElementsAreArray(
3991 const T (&array)[N]) {
3992 return ElementsAreArray(array, N);
3993}
3994
3995template <typename Container>
3996inline internal::ElementsAreArrayMatcher<typename Container::value_type>
3997ElementsAreArray(const Container& container) {
3998 return ElementsAreArray(container.begin(), container.end());
3999}
4000
4001template <typename T>
4002inline internal::ElementsAreArrayMatcher<T>
4003ElementsAreArray(::std::initializer_list<T> xs) {
4004 return ElementsAreArray(xs.begin(), xs.end());
4005}
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020template <typename Iter>
4021inline internal::UnorderedElementsAreArrayMatcher<
4022 typename ::std::iterator_traits<Iter>::value_type>
4024 typedef typename ::std::iterator_traits<Iter>::value_type T;
4025 return internal::UnorderedElementsAreArrayMatcher<T>(
4026 internal::UnorderedMatcherRequire::ExactMatch, first,
last);
4027}
4028
4029template <typename T>
4030inline internal::UnorderedElementsAreArrayMatcher<T>
4031UnorderedElementsAreArray(
const T* pointer,
size_t count) {
4032 return UnorderedElementsAreArray(pointer, pointer +
count);
4033}
4034
4035template <typename T, size_t N>
4036inline internal::UnorderedElementsAreArrayMatcher<T>
4037UnorderedElementsAreArray(const T (&array)[N]) {
4038 return UnorderedElementsAreArray(array, N);
4039}
4040
4041template <typename Container>
4042inline internal::UnorderedElementsAreArrayMatcher<
4043 typename Container::value_type>
4044UnorderedElementsAreArray(const Container& container) {
4045 return UnorderedElementsAreArray(container.begin(), container.end());
4046}
4047
4048template <typename T>
4049inline internal::UnorderedElementsAreArrayMatcher<T>
4050UnorderedElementsAreArray(::std::initializer_list<T> xs) {
4051 return UnorderedElementsAreArray(xs.begin(), xs.end());
4052}
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063const internal::AnythingMatcher _ = {};
4064
4065template <typename T>
4066inline Matcher<T> A() {
4067 return _;
4068}
4069
4070
4071template <typename T>
4072inline Matcher<T> An() {
4073 return _;
4074}
4075
4076template <typename T, typename M>
4077Matcher<T> internal::MatcherCastImpl<T, M>::CastImpl(
4078 const M&
value, std::false_type ,
4079 std::false_type ) {
4081}
4082
4083
4084inline PolymorphicMatcher<internal::IsNullMatcher >
IsNull() {
4085 return MakePolymorphicMatcher(internal::IsNullMatcher());
4086}
4087
4088
4089
4090
4091inline PolymorphicMatcher<internal::NotNullMatcher > NotNull() {
4092 return MakePolymorphicMatcher(internal::NotNullMatcher());
4093}
4094
4095
4096
4097template <typename T>
4098inline internal::RefMatcher<T&> Ref(T&
x) {
4099 return internal::RefMatcher<T&>(
x);
4100}
4101
4102
4103inline PolymorphicMatcher<internal::IsNanMatcher> IsNan() {
4104 return MakePolymorphicMatcher(internal::IsNanMatcher());
4105}
4106
4107
4108
4109inline internal::FloatingEqMatcher<double> DoubleEq(double rhs) {
4110 return internal::FloatingEqMatcher<double>(rhs, false);
4111}
4112
4113
4114
4115inline internal::FloatingEqMatcher<double> NanSensitiveDoubleEq(double rhs) {
4116 return internal::FloatingEqMatcher<double>(rhs, true);
4117}
4118
4119
4120
4121
4122inline internal::FloatingEqMatcher<double> DoubleNear(
4123 double rhs, double max_abs_error) {
4124 return internal::FloatingEqMatcher<double>(rhs, false, max_abs_error);
4125}
4126
4127
4128
4129
4130inline internal::FloatingEqMatcher<double> NanSensitiveDoubleNear(
4131 double rhs, double max_abs_error) {
4132 return internal::FloatingEqMatcher<double>(rhs, true, max_abs_error);
4133}
4134
4135
4136
4137inline internal::FloatingEqMatcher<float> FloatEq(float rhs) {
4138 return internal::FloatingEqMatcher<float>(rhs, false);
4139}
4140
4141
4142
4143inline internal::FloatingEqMatcher<float> NanSensitiveFloatEq(float rhs) {
4144 return internal::FloatingEqMatcher<float>(rhs, true);
4145}
4146
4147
4148
4149
4150inline internal::FloatingEqMatcher<float> FloatNear(
4151 float rhs, float max_abs_error) {
4152 return internal::FloatingEqMatcher<float>(rhs, false, max_abs_error);
4153}
4154
4155
4156
4157
4158inline internal::FloatingEqMatcher<float> NanSensitiveFloatNear(
4159 float rhs, float max_abs_error) {
4160 return internal::FloatingEqMatcher<float>(rhs, true, max_abs_error);
4161}
4162
4163
4164
4165template <typename InnerMatcher>
4166inline internal::PointeeMatcher<InnerMatcher> Pointee(
4167 const InnerMatcher& inner_matcher) {
4168 return internal::PointeeMatcher<InnerMatcher>(inner_matcher);
4169}
4170
4171#if GTEST_HAS_RTTI
4172
4173
4174
4175
4176
4177
4178template <typename To>
4179inline PolymorphicMatcher<internal::WhenDynamicCastToMatcher<To> >
4180WhenDynamicCastTo(const Matcher<To>& inner_matcher) {
4181 return MakePolymorphicMatcher(
4182 internal::WhenDynamicCastToMatcher<To>(inner_matcher));
4183}
4184#endif
4185
4186
4187
4188
4189
4190template <typename Class, typename FieldType, typename FieldMatcher>
4191inline PolymorphicMatcher<
4192 internal::FieldMatcher<Class, FieldType> > Field(
4193 FieldType Class::*field, const FieldMatcher& matcher) {
4194 return MakePolymorphicMatcher(
4195 internal::FieldMatcher<Class, FieldType>(
4196 field, MatcherCast<const FieldType&>(matcher)));
4197
4198
4199
4200
4201}
4202
4203
4204
4205template <typename Class, typename FieldType, typename FieldMatcher>
4206inline PolymorphicMatcher<internal::FieldMatcher<Class, FieldType> > Field(
4207 const std::string& field_name, FieldType Class::*field,
4208 const FieldMatcher& matcher) {
4209 return MakePolymorphicMatcher(internal::FieldMatcher<Class, FieldType>(
4210 field_name, field, MatcherCast<const FieldType&>(matcher)));
4211}
4212
4213
4214
4215
4216
4217template <typename Class, typename PropertyType, typename PropertyMatcher>
4218inline PolymorphicMatcher<internal::PropertyMatcher<
4219 Class, PropertyType, PropertyType (Class::*)() const> >
4220Property(PropertyType (Class::*property)() const,
4221 const PropertyMatcher& matcher) {
4222 return MakePolymorphicMatcher(
4223 internal::PropertyMatcher<Class, PropertyType,
4224 PropertyType (Class::*)() const>(
4225 property, MatcherCast<const PropertyType&>(matcher)));
4226
4227
4228
4229
4230}
4231
4232
4233
4234template <typename Class, typename PropertyType, typename PropertyMatcher>
4235inline PolymorphicMatcher<internal::PropertyMatcher<
4236 Class, PropertyType, PropertyType (Class::*)() const> >
4237Property(const std::string& property_name,
4238 PropertyType (Class::*property)() const,
4239 const PropertyMatcher& matcher) {
4240 return MakePolymorphicMatcher(
4241 internal::PropertyMatcher<Class, PropertyType,
4242 PropertyType (Class::*)() const>(
4243 property_name, property, MatcherCast<const PropertyType&>(matcher)));
4244}
4245
4246
4247template <typename Class, typename PropertyType, typename PropertyMatcher>
4248inline PolymorphicMatcher<internal::PropertyMatcher<
4249 Class, PropertyType, PropertyType (Class::*)() const &> >
4250Property(PropertyType (Class::*property)() const &,
4251 const PropertyMatcher& matcher) {
4252 return MakePolymorphicMatcher(
4253 internal::PropertyMatcher<Class, PropertyType,
4254 PropertyType (Class::*)() const&>(
4255 property, MatcherCast<const PropertyType&>(matcher)));
4256}
4257
4258
4259template <typename Class, typename PropertyType, typename PropertyMatcher>
4260inline PolymorphicMatcher<internal::PropertyMatcher<
4261 Class, PropertyType, PropertyType (Class::*)() const &> >
4262Property(const std::string& property_name,
4263 PropertyType (Class::*property)() const &,
4264 const PropertyMatcher& matcher) {
4265 return MakePolymorphicMatcher(
4266 internal::PropertyMatcher<Class, PropertyType,
4267 PropertyType (Class::*)() const&>(
4268 property_name, property, MatcherCast<const PropertyType&>(matcher)));
4269}
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279template <typename Callable, typename InnerMatcher>
4280internal::ResultOfMatcher<Callable, InnerMatcher> ResultOf(
4281 Callable callable, InnerMatcher matcher) {
4282 return internal::ResultOfMatcher<Callable, InnerMatcher>(
4283 std::move(callable), std::move(matcher));
4284}
4285
4286
4287
4288
4289template <typename T = std::string>
4290PolymorphicMatcher<internal::StrEqualityMatcher<std::string> > StrEq(
4291 const internal::StringLike<T>& str) {
4292 return MakePolymorphicMatcher(
4293 internal::StrEqualityMatcher<std::string>(std::string(str), true, true));
4294}
4295
4296
4297template <typename T = std::string>
4298PolymorphicMatcher<internal::StrEqualityMatcher<std::string> > StrNe(
4299 const internal::StringLike<T>& str) {
4300 return MakePolymorphicMatcher(
4301 internal::StrEqualityMatcher<std::string>(std::string(str), false, true));
4302}
4303
4304
4305template <typename T = std::string>
4306PolymorphicMatcher<internal::StrEqualityMatcher<std::string> > StrCaseEq(
4307 const internal::StringLike<T>& str) {
4308 return MakePolymorphicMatcher(
4309 internal::StrEqualityMatcher<std::string>(std::string(str), true, false));
4310}
4311
4312
4313template <typename T = std::string>
4314PolymorphicMatcher<internal::StrEqualityMatcher<std::string> > StrCaseNe(
4315 const internal::StringLike<T>& str) {
4316 return MakePolymorphicMatcher(internal::StrEqualityMatcher<std::string>(
4317 std::string(str), false, false));
4318}
4319
4320
4321
4322template <typename T = std::string>
4323PolymorphicMatcher<internal::HasSubstrMatcher<std::string> > HasSubstr(
4324 const internal::StringLike<T>& substring) {
4325 return MakePolymorphicMatcher(
4326 internal::HasSubstrMatcher<std::string>(std::string(substring)));
4327}
4328
4329
4330template <typename T = std::string>
4331PolymorphicMatcher<internal::StartsWithMatcher<std::string> > StartsWith(
4332 const internal::StringLike<T>& prefix) {
4333 return MakePolymorphicMatcher(
4334 internal::StartsWithMatcher<std::string>(std::string(prefix)));
4335}
4336
4337
4338template <typename T = std::string>
4339PolymorphicMatcher<internal::EndsWithMatcher<std::string> > EndsWith(
4340 const internal::StringLike<T>& suffix) {
4341 return MakePolymorphicMatcher(
4342 internal::EndsWithMatcher<std::string>(std::string(suffix)));
4343}
4344
4345#if GTEST_HAS_STD_WSTRING
4346
4347
4348
4349inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring> > StrEq(
4350 const std::wstring& str) {
4351 return MakePolymorphicMatcher(
4352 internal::StrEqualityMatcher<std::wstring>(str, true, true));
4353}
4354
4355
4356inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring> > StrNe(
4357 const std::wstring& str) {
4358 return MakePolymorphicMatcher(
4359 internal::StrEqualityMatcher<std::wstring>(str, false, true));
4360}
4361
4362
4363inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring> >
4364StrCaseEq(const std::wstring& str) {
4365 return MakePolymorphicMatcher(
4366 internal::StrEqualityMatcher<std::wstring>(str, true, false));
4367}
4368
4369
4370inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring> >
4371StrCaseNe(const std::wstring& str) {
4372 return MakePolymorphicMatcher(
4373 internal::StrEqualityMatcher<std::wstring>(str, false, false));
4374}
4375
4376
4377
4378inline PolymorphicMatcher<internal::HasSubstrMatcher<std::wstring> > HasSubstr(
4379 const std::wstring& substring) {
4380 return MakePolymorphicMatcher(
4381 internal::HasSubstrMatcher<std::wstring>(substring));
4382}
4383
4384
4385inline PolymorphicMatcher<internal::StartsWithMatcher<std::wstring> >
4386StartsWith(const std::wstring& prefix) {
4387 return MakePolymorphicMatcher(
4388 internal::StartsWithMatcher<std::wstring>(prefix));
4389}
4390
4391
4392inline PolymorphicMatcher<internal::EndsWithMatcher<std::wstring> > EndsWith(
4393 const std::wstring& suffix) {
4394 return MakePolymorphicMatcher(
4395 internal::EndsWithMatcher<std::wstring>(suffix));
4396}
4397
4398#endif
4399
4400
4401
4402inline internal::Eq2Matcher Eq() { return internal::Eq2Matcher(); }
4403
4404
4405
4406inline internal::Ge2Matcher Ge() { return internal::Ge2Matcher(); }
4407
4408
4409
4410inline internal::Gt2Matcher Gt() { return internal::Gt2Matcher(); }
4411
4412
4413
4414inline internal::Le2Matcher Le() { return internal::Le2Matcher(); }
4415
4416
4417
4418inline internal::Lt2Matcher Lt() { return internal::Lt2Matcher(); }
4419
4420
4421
4422inline internal::Ne2Matcher Ne() { return internal::Ne2Matcher(); }
4423
4424
4425
4426inline internal::FloatingEq2Matcher<float> FloatEq() {
4427 return internal::FloatingEq2Matcher<float>();
4428}
4429
4430
4431
4432inline internal::FloatingEq2Matcher<double> DoubleEq() {
4433 return internal::FloatingEq2Matcher<double>();
4434}
4435
4436
4437
4438inline internal::FloatingEq2Matcher<float> NanSensitiveFloatEq() {
4439 return internal::FloatingEq2Matcher<float>(true);
4440}
4441
4442
4443
4444inline internal::FloatingEq2Matcher<double> NanSensitiveDoubleEq() {
4445 return internal::FloatingEq2Matcher<double>(true);
4446}
4447
4448
4449
4450inline internal::FloatingEq2Matcher<float> FloatNear(float max_abs_error) {
4451 return internal::FloatingEq2Matcher<float>(max_abs_error);
4452}
4453
4454
4455
4456inline internal::FloatingEq2Matcher<double> DoubleNear(double max_abs_error) {
4457 return internal::FloatingEq2Matcher<double>(max_abs_error);
4458}
4459
4460
4461
4462
4463inline internal::FloatingEq2Matcher<float> NanSensitiveFloatNear(
4464 float max_abs_error) {
4465 return internal::FloatingEq2Matcher<float>(max_abs_error, true);
4466}
4467
4468
4469
4470
4471inline internal::FloatingEq2Matcher<double> NanSensitiveDoubleNear(
4472 double max_abs_error) {
4473 return internal::FloatingEq2Matcher<double>(max_abs_error, true);
4474}
4475
4476
4477
4478template <typename InnerMatcher>
4479inline internal::NotMatcher<InnerMatcher> Not(InnerMatcher m) {
4480 return internal::NotMatcher<InnerMatcher>(m);
4481}
4482
4483
4484
4485
4486template <typename Predicate>
4487inline PolymorphicMatcher<internal::TrulyMatcher<Predicate> >
4488Truly(Predicate pred) {
4489 return MakePolymorphicMatcher(internal::TrulyMatcher<Predicate>(pred));
4490}
4491
4492
4493
4494
4495
4496
4497
4498template <typename SizeMatcher>
4499inline internal::SizeIsMatcher<SizeMatcher>
4500SizeIs(const SizeMatcher& size_matcher) {
4501 return internal::SizeIsMatcher<SizeMatcher>(size_matcher);
4502}
4503
4504
4505
4506
4507
4508
4509template <typename DistanceMatcher>
4510inline internal::BeginEndDistanceIsMatcher<DistanceMatcher>
4511BeginEndDistanceIs(const DistanceMatcher& distance_matcher) {
4512 return internal::BeginEndDistanceIsMatcher<DistanceMatcher>(distance_matcher);
4513}
4514
4515
4516
4517
4518
4519template <typename Container>
4520inline PolymorphicMatcher<internal::ContainerEqMatcher<
4522ContainerEq(const Container& rhs) {
4523 return MakePolymorphicMatcher(internal::ContainerEqMatcher<Container>(rhs));
4524}
4525
4526
4527
4528template <typename Comparator, typename ContainerMatcher>
4529inline internal::WhenSortedByMatcher<Comparator, ContainerMatcher>
4530WhenSortedBy(const Comparator& comparator,
4531 const ContainerMatcher& container_matcher) {
4532 return internal::WhenSortedByMatcher<Comparator, ContainerMatcher>(
4533 comparator, container_matcher);
4534}
4535
4536
4537
4538template <typename ContainerMatcher>
4539inline internal::WhenSortedByMatcher<internal::LessComparator, ContainerMatcher>
4540WhenSorted(const ContainerMatcher& container_matcher) {
4541 return
4542 internal::WhenSortedByMatcher<internal::LessComparator, ContainerMatcher>(
4543 internal::LessComparator(), container_matcher);
4544}
4545
4546
4547
4548
4549
4550
4551
4552template <typename TupleMatcher, typename Container>
4553inline internal::PointwiseMatcher<TupleMatcher,
4555Pointwise(const TupleMatcher& tuple_matcher, const Container& rhs) {
4556 return internal::PointwiseMatcher<TupleMatcher, Container>(tuple_matcher,
4557 rhs);
4558}
4559
4560
4561
4562template <typename TupleMatcher, typename T>
4563inline internal::PointwiseMatcher<TupleMatcher, std::vector<T> > Pointwise(
4564 const TupleMatcher& tuple_matcher, std::initializer_list<T> rhs) {
4565 return Pointwise(tuple_matcher, std::vector<T>(rhs));
4566}
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580template <typename Tuple2Matcher, typename RhsContainer>
4581inline internal::UnorderedElementsAreArrayMatcher<
4582 typename internal::BoundSecondMatcher<
4583 Tuple2Matcher,
4584 typename internal::StlContainerView<
4586UnorderedPointwise(const Tuple2Matcher& tuple2_matcher,
4587 const RhsContainer& rhs_container) {
4588
4589
4590 typedef typename internal::StlContainerView<RhsContainer> RhsView;
4592 typedef typename RhsStlContainer::value_type Second;
4593 const RhsStlContainer& rhs_stl_container =
4594 RhsView::ConstReference(rhs_container);
4595
4596
4597 ::std::vector<internal::BoundSecondMatcher<Tuple2Matcher, Second> > matchers;
4598 for (typename RhsStlContainer::const_iterator it = rhs_stl_container.begin();
4599 it != rhs_stl_container.end(); ++it) {
4600 matchers.push_back(
4601 internal::MatcherBindSecond(tuple2_matcher, *it));
4602 }
4603
4604
4605 return UnorderedElementsAreArray(matchers);
4606}
4607
4608
4609
4610template <typename Tuple2Matcher, typename T>
4611inline internal::UnorderedElementsAreArrayMatcher<
4612 typename internal::BoundSecondMatcher<Tuple2Matcher, T> >
4613UnorderedPointwise(const Tuple2Matcher& tuple2_matcher,
4614 std::initializer_list<T> rhs) {
4615 return UnorderedPointwise(tuple2_matcher, std::vector<T>(rhs));
4616}
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637template <typename M>
4638inline internal::ContainsMatcher<M> Contains(M matcher) {
4639 return internal::ContainsMatcher<M>(matcher);
4640}
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669template <typename Iter>
4670inline internal::UnorderedElementsAreArrayMatcher<
4671 typename ::std::iterator_traits<Iter>::value_type>
4673 typedef typename ::std::iterator_traits<Iter>::value_type T;
4674 return internal::UnorderedElementsAreArrayMatcher<T>(
4675 internal::UnorderedMatcherRequire::Superset, first,
last);
4676}
4677
4678template <typename T>
4679inline internal::UnorderedElementsAreArrayMatcher<T> IsSupersetOf(
4680 const T* pointer,
size_t count) {
4681 return IsSupersetOf(pointer, pointer +
count);
4682}
4683
4684template <typename T, size_t N>
4685inline internal::UnorderedElementsAreArrayMatcher<T> IsSupersetOf(
4686 const T (&array)[N]) {
4687 return IsSupersetOf(array, N);
4688}
4689
4690template <typename Container>
4691inline internal::UnorderedElementsAreArrayMatcher<
4692 typename Container::value_type>
4693IsSupersetOf(const Container& container) {
4694 return IsSupersetOf(container.begin(), container.end());
4695}
4696
4697template <typename T>
4698inline internal::UnorderedElementsAreArrayMatcher<T> IsSupersetOf(
4699 ::std::initializer_list<T> xs) {
4700 return IsSupersetOf(xs.begin(), xs.end());
4701}
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726template <typename Iter>
4727inline internal::UnorderedElementsAreArrayMatcher<
4728 typename ::std::iterator_traits<Iter>::value_type>
4730 typedef typename ::std::iterator_traits<Iter>::value_type T;
4731 return internal::UnorderedElementsAreArrayMatcher<T>(
4732 internal::UnorderedMatcherRequire::Subset, first,
last);
4733}
4734
4735template <typename T>
4736inline internal::UnorderedElementsAreArrayMatcher<T> IsSubsetOf(
4737 const T* pointer,
size_t count) {
4738 return IsSubsetOf(pointer, pointer +
count);
4739}
4740
4741template <typename T, size_t N>
4742inline internal::UnorderedElementsAreArrayMatcher<T> IsSubsetOf(
4743 const T (&array)[N]) {
4744 return IsSubsetOf(array, N);
4745}
4746
4747template <typename Container>
4748inline internal::UnorderedElementsAreArrayMatcher<
4749 typename Container::value_type>
4750IsSubsetOf(const Container& container) {
4751 return IsSubsetOf(container.begin(), container.end());
4752}
4753
4754template <typename T>
4755inline internal::UnorderedElementsAreArrayMatcher<T> IsSubsetOf(
4756 ::std::initializer_list<T> xs) {
4757 return IsSubsetOf(xs.begin(), xs.end());
4758}
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787template <typename M>
4788inline internal::EachMatcher<M> Each(M matcher) {
4789 return internal::EachMatcher<M>(matcher);
4790}
4791
4792
4793
4794
4795template <typename M>
4796inline internal::KeyMatcher<M> Key(M inner_matcher) {
4797 return internal::KeyMatcher<M>(inner_matcher);
4798}
4799
4800
4801
4802
4803
4804
4805template <typename FirstMatcher, typename SecondMatcher>
4806inline internal::PairMatcher<FirstMatcher, SecondMatcher>
4807Pair(FirstMatcher first_matcher, SecondMatcher second_matcher) {
4808 return internal::PairMatcher<FirstMatcher, SecondMatcher>(
4809 first_matcher, second_matcher);
4810}
4811
4812namespace no_adl {
4813
4814
4815
4816
4817template <typename... M>
4819 M&&... matchers) {
4821 std::forward<M>(matchers)...);
4822}
4823
4824
4825
4826template <typename InnerMatcher>
4827inline internal::PointerMatcher<InnerMatcher> Pointer(
4828 const InnerMatcher& inner_matcher) {
4829 return internal::PointerMatcher<InnerMatcher>(inner_matcher);
4830}
4831
4832
4833
4834template <typename InnerMatcher>
4835inline internal::AddressMatcher<InnerMatcher> Address(
4836 const InnerMatcher& inner_matcher) {
4837 return internal::AddressMatcher<InnerMatcher>(inner_matcher);
4838}
4839}
4840
4841
4842
4843template <typename M>
4844inline internal::MatcherAsPredicate<M> Matches(M matcher) {
4845 return internal::MatcherAsPredicate<M>(matcher);
4846}
4847
4848
4849template <typename T, typename M>
4850inline bool Value(
const T&
value, M matcher) {
4851 return testing::Matches(matcher)(
value);
4852}
4853
4854
4855
4856template <typename T, typename M>
4857inline bool ExplainMatchResult(
4858 M matcher,
const T&
value, MatchResultListener* listener) {
4859 return SafeMatcherCast<const T&>(matcher).MatchAndExplain(
value, listener);
4860}
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872template <typename T, typename M>
4873std::string DescribeMatcher(const M& matcher, bool negation = false) {
4874 ::std::stringstream ss;
4875 Matcher<T> monomorphic_matcher = SafeMatcherCast<T>(matcher);
4876 if (negation) {
4877 monomorphic_matcher.DescribeNegationTo(&ss);
4878 } else {
4879 monomorphic_matcher.DescribeTo(&ss);
4880 }
4881 return ss.str();
4882}
4883
4884template <typename... Args>
4885internal::ElementsAreMatcher<
4887ElementsAre(const Args&... matchers) {
4888 return internal::ElementsAreMatcher<
4890 std::make_tuple(matchers...));
4891}
4892
4893template <typename... Args>
4894internal::UnorderedElementsAreMatcher<
4896UnorderedElementsAre(const Args&... matchers) {
4897 return internal::UnorderedElementsAreMatcher<
4899 std::make_tuple(matchers...));
4900}
4901
4902
4903template <typename... Args>
4905 const Args&... matchers) {
4907 matchers...);
4908}
4909
4910template <typename... Args>
4912 const Args&... matchers) {
4914 matchers...);
4915}
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939template <typename Iter>
4940inline internal::AnyOfArrayMatcher<
4941 typename ::std::iterator_traits<Iter>::value_type>
4943 return internal::AnyOfArrayMatcher<
4944 typename ::std::iterator_traits<Iter>::value_type>(first,
last);
4945}
4946
4947template <typename Iter>
4948inline internal::AllOfArrayMatcher<
4949 typename ::std::iterator_traits<Iter>::value_type>
4951 return internal::AllOfArrayMatcher<
4952 typename ::std::iterator_traits<Iter>::value_type>(first,
last);
4953}
4954
4955template <typename T>
4956inline internal::AnyOfArrayMatcher<T> AnyOfArray(
const T* ptr,
size_t count) {
4957 return AnyOfArray(ptr, ptr +
count);
4958}
4959
4960template <typename T>
4961inline internal::AllOfArrayMatcher<T> AllOfArray(
const T* ptr,
size_t count) {
4962 return AllOfArray(ptr, ptr +
count);
4963}
4964
4965template <typename T, size_t N>
4966inline internal::AnyOfArrayMatcher<T> AnyOfArray(const T (&array)[N]) {
4967 return AnyOfArray(array, N);
4968}
4969
4970template <typename T, size_t N>
4971inline internal::AllOfArrayMatcher<T> AllOfArray(const T (&array)[N]) {
4972 return AllOfArray(array, N);
4973}
4974
4975template <typename Container>
4976inline internal::AnyOfArrayMatcher<typename Container::value_type> AnyOfArray(
4977 const Container& container) {
4978 return AnyOfArray(container.begin(), container.end());
4979}
4980
4981template <typename Container>
4982inline internal::AllOfArrayMatcher<typename Container::value_type> AllOfArray(
4983 const Container& container) {
4984 return AllOfArray(container.begin(), container.end());
4985}
4986
4987template <typename T>
4988inline internal::AnyOfArrayMatcher<T> AnyOfArray(
4989 ::std::initializer_list<T> xs) {
4990 return AnyOfArray(xs.begin(), xs.end());
4991}
4992
4993template <typename T>
4994inline internal::AllOfArrayMatcher<T> AllOfArray(
4995 ::std::initializer_list<T> xs) {
4996 return AllOfArray(xs.begin(), xs.end());
4997}
4998
4999
5000
5001
5002template <size_t... k, typename InnerMatcher>
5004 InnerMatcher&& matcher) {
5006 std::forward<InnerMatcher>(matcher));
5007}
5008
5009
5010
5011
5012
5013
5014
5015
5016template <typename InnerMatcher>
5017inline InnerMatcher AllArgs(const InnerMatcher& matcher) { return matcher; }
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027template <typename ValueMatcher>
5028inline internal::OptionalMatcher<ValueMatcher> Optional(
5029 const ValueMatcher& value_matcher) {
5030 return internal::OptionalMatcher<ValueMatcher>(value_matcher);
5031}
5032
5033
5034template <typename T>
5035PolymorphicMatcher<internal::any_cast_matcher::AnyCastMatcher<T> > AnyWith(
5036 const Matcher<const T&>& matcher) {
5037 return MakePolymorphicMatcher(
5038 internal::any_cast_matcher::AnyCastMatcher<T>(matcher));
5039}
5040
5041
5042
5043
5044
5045template <typename T>
5046PolymorphicMatcher<internal::variant_matcher::VariantMatcher<T> > VariantWith(
5047 const Matcher<const T&>& matcher) {
5048 return MakePolymorphicMatcher(
5049 internal::variant_matcher::VariantMatcher<T>(matcher));
5050}
5051
5052#if GTEST_HAS_EXCEPTIONS
5053
5054
5055
5056namespace internal {
5057
5058class WithWhatMatcherImpl {
5059 public:
5060 WithWhatMatcherImpl(Matcher<std::string> matcher)
5061 : matcher_(
std::move(matcher)) {}
5062
5063 void DescribeTo(std::ostream* os) const {
5064 *os << "contains .what() that ";
5065 matcher_.DescribeTo(os);
5066 }
5067
5068 void DescribeNegationTo(std::ostream* os) const {
5069 *os << "contains .what() that does not ";
5070 matcher_.DescribeTo(os);
5071 }
5072
5073 template <typename Err>
5074 bool MatchAndExplain(const Err& err, MatchResultListener* listener) const {
5075 *listener << "which contains .what() that ";
5076 return matcher_.MatchAndExplain(err.what(), listener);
5077 }
5078
5079 private:
5080 const Matcher<std::string> matcher_;
5081};
5082
5083inline PolymorphicMatcher<WithWhatMatcherImpl> WithWhat(
5084 Matcher<std::string> m) {
5085 return MakePolymorphicMatcher(WithWhatMatcherImpl(std::move(m)));
5086}
5087
5088template <typename Err>
5089class ExceptionMatcherImpl {
5090 class NeverThrown {
5091 public:
5092 const char* what()
const noexcept {
5093 return "this exception should never be thrown";
5094 }
5095 };
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118 using DefaultExceptionType = typename std::conditional<
5119 std::is_same<typename std::remove_cv<
5122 const NeverThrown&,
const std::exception&>
::type;
5123
5124 public:
5125 ExceptionMatcherImpl(Matcher<const Err&> matcher)
5126 : matcher_(
std::move(matcher)) {}
5127
5128 void DescribeTo(std::ostream* os) const {
5129 *os << "throws an exception which is a " << GetTypeName<Err>();
5130 *os << " which ";
5131 matcher_.DescribeTo(os);
5132 }
5133
5134 void DescribeNegationTo(std::ostream* os) const {
5135 *os << "throws an exception which is not a " << GetTypeName<Err>();
5136 *os << " which ";
5137 matcher_.DescribeNegationTo(os);
5138 }
5139
5140 template <typename T>
5141 bool MatchAndExplain(T&&
x, MatchResultListener* listener)
const {
5142 try {
5143 (void)(std::forward<T>(
x)());
5144 } catch (const Err& err) {
5145 *listener << "throws an exception which is a " << GetTypeName<Err>();
5146 *listener << " ";
5147 return matcher_.MatchAndExplain(err, listener);
5148 } catch (DefaultExceptionType err) {
5149#if GTEST_HAS_RTTI
5150 *listener <<
"throws an exception of type " <<
GetTypeName(
typeid(err));
5151 *listener << " ";
5152#else
5153 *listener << "throws an std::exception-derived type ";
5154#endif
5155 *listener << "with description \"" << err.what() << "\"";
5156 return false;
5157 } catch (...) {
5158 *listener << "throws an exception of an unknown type";
5159 return false;
5160 }
5161
5162 *listener << "does not throw any exception";
5163 return false;
5164 }
5165
5166 private:
5167 const Matcher<const Err&> matcher_;
5168};
5169
5170}
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194template <typename Err>
5195PolymorphicMatcher<internal::ExceptionMatcherImpl<Err>> Throws() {
5196 return MakePolymorphicMatcher(
5197 internal::ExceptionMatcherImpl<Err>(A<const Err&>()));
5198}
5199
5200template <typename Err, typename ExceptionMatcher>
5201PolymorphicMatcher<internal::ExceptionMatcherImpl<Err>> Throws(
5202 const ExceptionMatcher& exception_matcher) {
5203
5204
5205
5206 return MakePolymorphicMatcher(internal::ExceptionMatcherImpl<Err>(
5207 SafeMatcherCast<const Err&>(exception_matcher)));
5208}
5209
5210template <typename Err, typename MessageMatcher>
5211PolymorphicMatcher<internal::ExceptionMatcherImpl<Err>> ThrowsMessage(
5212 MessageMatcher&& message_matcher) {
5214 "expected an std::exception-derived type");
5215 return Throws<Err>(internal::WithWhat(
5216 MatcherCast<std::string>(std::forward<MessageMatcher>(message_matcher))));
5217}
5218
5219#endif
5220
5221
5222
5223
5224
5225#define ASSERT_THAT(value, matcher) ASSERT_PRED_FORMAT1(\
5226 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
5227#define EXPECT_THAT(value, matcher) EXPECT_PRED_FORMAT1(\
5228 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
5229
5230
5231#define MATCHER(name, description) \
5232 class name##Matcher \
5233 : public ::testing::internal::MatcherBaseImpl<name##Matcher> { \
5234 public: \
5235 template <typename arg_type> \
5236 class gmock_Impl : public ::testing::MatcherInterface<const arg_type&> { \
5237 public: \
5238 gmock_Impl() {} \
5239 bool MatchAndExplain( \
5240 const arg_type& arg, \
5241 ::testing::MatchResultListener* result_listener) const override; \
5242 void DescribeTo(::std::ostream* gmock_os) const override { \
5243 *gmock_os << FormatDescription(false); \
5244 } \
5245 void DescribeNegationTo(::std::ostream* gmock_os) const override { \
5246 *gmock_os << FormatDescription(true); \
5247 } \
5248 \
5249 private: \
5250 ::std::string FormatDescription(bool negation) const { \
5251 ::std::string gmock_description = (description); \
5252 if (!gmock_description.empty()) { \
5253 return gmock_description; \
5254 } \
5255 return ::testing::internal::FormatMatcherDescription(negation, #name, \
5256 {}); \
5257 } \
5258 }; \
5259 }; \
5260 GTEST_ATTRIBUTE_UNUSED_ inline name##Matcher name() { return {}; } \
5261 template <typename arg_type> \
5262 bool name##Matcher::gmock_Impl<arg_type>::MatchAndExplain( \
5263 const arg_type& arg, \
5264 ::testing::MatchResultListener* result_listener GTEST_ATTRIBUTE_UNUSED_) \
5265 const
5266
5267#define MATCHER_P(name, p0, description) \
5268 GMOCK_INTERNAL_MATCHER(name, name##MatcherP, description, (p0))
5269#define MATCHER_P2(name, p0, p1, description) \
5270 GMOCK_INTERNAL_MATCHER(name, name##MatcherP2, description, (p0, p1))
5271#define MATCHER_P3(name, p0, p1, p2, description) \
5272 GMOCK_INTERNAL_MATCHER(name, name##MatcherP3, description, (p0, p1, p2))
5273#define MATCHER_P4(name, p0, p1, p2, p3, description) \
5274 GMOCK_INTERNAL_MATCHER(name, name##MatcherP4, description, (p0, p1, p2, p3))
5275#define MATCHER_P5(name, p0, p1, p2, p3, p4, description) \
5276 GMOCK_INTERNAL_MATCHER(name, name##MatcherP5, description, \
5277 (p0, p1, p2, p3, p4))
5278#define MATCHER_P6(name, p0, p1, p2, p3, p4, p5, description) \
5279 GMOCK_INTERNAL_MATCHER(name, name##MatcherP6, description, \
5280 (p0, p1, p2, p3, p4, p5))
5281#define MATCHER_P7(name, p0, p1, p2, p3, p4, p5, p6, description) \
5282 GMOCK_INTERNAL_MATCHER(name, name##MatcherP7, description, \
5283 (p0, p1, p2, p3, p4, p5, p6))
5284#define MATCHER_P8(name, p0, p1, p2, p3, p4, p5, p6, p7, description) \
5285 GMOCK_INTERNAL_MATCHER(name, name##MatcherP8, description, \
5286 (p0, p1, p2, p3, p4, p5, p6, p7))
5287#define MATCHER_P9(name, p0, p1, p2, p3, p4, p5, p6, p7, p8, description) \
5288 GMOCK_INTERNAL_MATCHER(name, name##MatcherP9, description, \
5289 (p0, p1, p2, p3, p4, p5, p6, p7, p8))
5290#define MATCHER_P10(name, p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, description) \
5291 GMOCK_INTERNAL_MATCHER(name, name##MatcherP10, description, \
5292 (p0, p1, p2, p3, p4, p5, p6, p7, p8, p9))
5293
5294#define GMOCK_INTERNAL_MATCHER(name, full_name, description, args) \
5295 template <GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args)> \
5296 class full_name : public ::testing::internal::MatcherBaseImpl< \
5297 full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>> { \
5298 public: \
5299 using full_name::MatcherBaseImpl::MatcherBaseImpl; \
5300 template <typename arg_type> \
5301 class gmock_Impl : public ::testing::MatcherInterface<const arg_type&> { \
5302 public: \
5303 explicit gmock_Impl(GMOCK_INTERNAL_MATCHER_FUNCTION_ARGS(args)) \
5304 : GMOCK_INTERNAL_MATCHER_FORWARD_ARGS(args) {} \
5305 bool MatchAndExplain( \
5306 const arg_type& arg, \
5307 ::testing::MatchResultListener* result_listener) const override; \
5308 void DescribeTo(::std::ostream* gmock_os) const override { \
5309 *gmock_os << FormatDescription(false); \
5310 } \
5311 void DescribeNegationTo(::std::ostream* gmock_os) const override { \
5312 *gmock_os << FormatDescription(true); \
5313 } \
5314 GMOCK_INTERNAL_MATCHER_MEMBERS(args) \
5315 \
5316 private: \
5317 ::std::string FormatDescription(bool negation) const { \
5318 ::std::string gmock_description = (description); \
5319 if (!gmock_description.empty()) { \
5320 return gmock_description; \
5321 } \
5322 return ::testing::internal::FormatMatcherDescription( \
5323 negation, #name, \
5324 ::testing::internal::UniversalTersePrintTupleFieldsToStrings( \
5325 ::std::tuple<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>( \
5326 GMOCK_INTERNAL_MATCHER_MEMBERS_USAGE(args)))); \
5327 } \
5328 }; \
5329 }; \
5330 template <GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args)> \
5331 inline full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)> name( \
5332 GMOCK_INTERNAL_MATCHER_FUNCTION_ARGS(args)) { \
5333 return full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>( \
5334 GMOCK_INTERNAL_MATCHER_ARGS_USAGE(args)); \
5335 } \
5336 template <GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args)> \
5337 template <typename arg_type> \
5338 bool full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>::gmock_Impl< \
5339 arg_type>::MatchAndExplain(const arg_type& arg, \
5340 ::testing::MatchResultListener* \
5341 result_listener GTEST_ATTRIBUTE_UNUSED_) \
5342 const
5343
5344#define GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args) \
5345 GMOCK_PP_TAIL( \
5346 GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAM, , args))
5347#define GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAM(i_unused, data_unused, arg) \
5348 , typename arg##_type
5349
5350#define GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args) \
5351 GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_TYPE_PARAM, , args))
5352#define GMOCK_INTERNAL_MATCHER_TYPE_PARAM(i_unused, data_unused, arg) \
5353 , arg##_type
5354
5355#define GMOCK_INTERNAL_MATCHER_FUNCTION_ARGS(args) \
5356 GMOCK_PP_TAIL(dummy_first GMOCK_PP_FOR_EACH( \
5357 GMOCK_INTERNAL_MATCHER_FUNCTION_ARG, , args))
5358#define GMOCK_INTERNAL_MATCHER_FUNCTION_ARG(i, data_unused, arg) \
5359 , arg##_type gmock_p##i
5360
5361#define GMOCK_INTERNAL_MATCHER_FORWARD_ARGS(args) \
5362 GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_FORWARD_ARG, , args))
5363#define GMOCK_INTERNAL_MATCHER_FORWARD_ARG(i, data_unused, arg) \
5364 , arg(::std::forward<arg##_type>(gmock_p##i))
5365
5366#define GMOCK_INTERNAL_MATCHER_MEMBERS(args) \
5367 GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_MEMBER, , args)
5368#define GMOCK_INTERNAL_MATCHER_MEMBER(i_unused, data_unused, arg) \
5369 const arg##_type arg;
5370
5371#define GMOCK_INTERNAL_MATCHER_MEMBERS_USAGE(args) \
5372 GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_MEMBER_USAGE, , args))
5373#define GMOCK_INTERNAL_MATCHER_MEMBER_USAGE(i_unused, data_unused, arg) , arg
5374
5375#define GMOCK_INTERNAL_MATCHER_ARGS_USAGE(args) \
5376 GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_ARG_USAGE, , args))
5377#define GMOCK_INTERNAL_MATCHER_ARG_USAGE(i, data_unused, arg_unused) \
5378 , gmock_p##i
5379
5380
5381using namespace no_adl;
5382
5383}
UnicodeText::const_iterator::difference_type distance(const UnicodeText::const_iterator &first, const UnicodeText::const_iterator &last)
#define GMOCK_KIND_OF_(type)
#define GTEST_LOG_(severity)
#define GTEST_CHECK_(condition)
#define GTEST_COMPILE_ASSERT_(expr, msg)
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
#define GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement)
#define GTEST_REMOVE_REFERENCE_AND_CONST_(T)
AssertionResult AssertionFailure()
::std::string PrintToString(const T &value)
AssertionResult AssertionSuccess()
std::decay< FunctionImpl >::type Invoke(FunctionImpl &&function_impl)
void UniversalPrint(const T &value, ::std::ostream *os)
auto Apply(F &&f, Tuple &&args) -> decltype(ApplyImpl(std::forward< F >(f), std::forward< Tuple >(args), MakeIndexSequence< std::tuple_size< typename std::remove_reference< Tuple >::type >::value >()))
::std::vector< ::std::string > Strings
std::string GetTypeName()
Iter ArrayAwareFind(Iter begin, Iter end, const Element &elem)
typename MakeIndexSequenceImpl< N >::type MakeIndexSequence
GTEST_API_ std::string FormatMatcherDescription(bool negation, const char *matcher_name, const Strings ¶m_values)
const Pointer::element_type * GetRawPointer(const Pointer &p)
AssertionResult IsNull(const char *str)
GTEST_API_ ElementMatcherPairs FindMaxBipartiteMatching(const MatchMatrix &g)
def Iter(n, format, sep='')
std::string Print(const T &value)