303class StringMatchResultListener :
public MatchResultListener {
305 StringMatchResultListener() : MatchResultListener(&ss_) {}
308 std::string str()
const {
return ss_.str(); }
311 void Clear() { ss_.str(
""); }
314 ::std::stringstream ss_;
316 StringMatchResultListener(
const StringMatchResultListener&) =
delete;
317 StringMatchResultListener& operator=(
const StringMatchResultListener&) =
335template <
typename T,
typename M>
336class MatcherCastImpl {
338 static Matcher<T> Cast(
const M& polymorphic_matcher_or_value) {
352 return CastImpl(polymorphic_matcher_or_value,
353 std::is_convertible<M, Matcher<T>>{},
354 std::is_convertible<M, T>{});
358 template <
bool Ignore>
359 static Matcher<T> CastImpl(
const M& polymorphic_matcher_or_value,
361 std::integral_constant<bool, Ignore>) {
370 return polymorphic_matcher_or_value;
376 static Matcher<T> CastImpl(
const M& value,
379 return Matcher<T>(ImplicitCast_<T>(value));
392 static Matcher<T> CastImpl(
const M& value,
400template <
typename T,
typename U>
401class MatcherCastImpl<T, Matcher<U>> {
403 static Matcher<T> Cast(
const Matcher<U>& source_matcher) {
404 return Matcher<T>(
new Impl(source_matcher));
408 class Impl :
public MatcherInterface<T> {
410 explicit Impl(
const Matcher<U>& source_matcher)
411 : source_matcher_(source_matcher) {}
414 bool MatchAndExplain(T x, MatchResultListener* listener)
const override {
415 using FromType =
typename std::remove_cv<
typename std::remove_pointer<
416 typename std::remove_reference<T>::type>::type>::type;
417 using ToType =
typename std::remove_cv<
typename std::remove_pointer<
418 typename std::remove_reference<U>::type>::type>::type;
423 (std::is_pointer<typename std::remove_reference<T>::type>::value !=
424 std::is_pointer<typename std::remove_reference<U>::type>::value) ||
425 std::is_same<FromType, ToType>::value ||
426 !std::is_base_of<FromType, ToType>::value,
427 "Can't implicitly convert from <base> to <derived>");
432 typename std::conditional<std::is_convertible<T&, const U&>::value,
435 return source_matcher_.MatchAndExplain(
static_cast<CastType
>(x),
439 void DescribeTo(::std::ostream* os)
const override {
440 source_matcher_.DescribeTo(os);
443 void DescribeNegationTo(::std::ostream* os)
const override {
444 source_matcher_.DescribeNegationTo(os);
448 const Matcher<U> source_matcher_;
455class MatcherCastImpl<T, Matcher<T>> {
457 static Matcher<T> Cast(
const Matcher<T>& matcher) {
return matcher; }
461template <
typename Derived>
462class MatcherBaseImpl {
464 MatcherBaseImpl() =
default;
466 template <
typename T>
467 operator ::testing::Matcher<T>()
const {
468 return ::testing::Matcher<T>(
new
469 typename Derived::template gmock_Impl<T>());
474template <
template <
typename...>
class Derived,
typename... Ts>
475class MatcherBaseImpl<Derived<Ts...>> {
479 template <
typename E = std::enable_if<
sizeof...(Ts) == 1>,
480 typename E::type* =
nullptr>
481 explicit MatcherBaseImpl(Ts... params)
482 : params_(std::forward<Ts>(params)...) {}
483 template <
typename E = std::enable_if<
sizeof...(Ts) != 1>,
484 typename =
typename E::type>
485 MatcherBaseImpl(Ts... params)
486 : params_(std::forward<Ts>(params)...) {}
488 template <
typename F>
489 operator ::testing::Matcher<F>()
const {
490 return Apply<F>(MakeIndexSequence<
sizeof...(Ts)>{});
494 template <
typename F, std::size_t... tuple_ids>
496 return ::testing::Matcher<F>(
497 new typename Derived<Ts...>::template gmock_Impl<F>(
498 std::get<tuple_ids>(params_)...));
501 const std::tuple<Ts...> params_;
510template <
typename T,
typename M>
511inline Matcher<T> MatcherCast(
const M& matcher) {
512 return internal::MatcherCastImpl<T, M>::Cast(matcher);
517template <
typename T,
typename M>
518inline Matcher<T> SafeMatcherCast(
const M& polymorphic_matcher_or_value) {
519 return MatcherCast<T>(polymorphic_matcher_or_value);
531template <
typename T,
typename U>
532inline Matcher<T> SafeMatcherCast(
const Matcher<U>& matcher) {
534 static_assert(std::is_convertible<const T&, const U&>::value,
535 "T must be implicitly convertible to U");
538 static_assert(std::is_reference<T>::value || !std::is_reference<U>::value,
539 "cannot convert non reference arg to reference");
542 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(T) RawT;
543 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(U) RawU;
547 kTIsOther || kUIsOther ||
548 (internal::LosslessArithmeticConvertible<RawT, RawU>::value),
549 "conversion of arithmetic types must be lossless");
550 return MatcherCast<T>(matcher);
562inline void PrintIfNotEmpty(
const std::string& explanation,
563 ::std::ostream* os) {
564 if (explanation !=
"" && os !=
nullptr) {
565 *os <<
", " << explanation;
572inline bool IsReadableTypeName(
const std::string& type_name) {
575 return (type_name.length() <= 20 ||
576 type_name.find_first_of(
"<(") == std::string::npos);
584template <
typename Value,
typename T>
585bool MatchPrintAndExplain(Value& value,
const Matcher<T>& matcher,
586 MatchResultListener* listener) {
587 if (!listener->IsInterested()) {
590 return matcher.Matches(value);
593 StringMatchResultListener inner_listener;
594 const bool match = matcher.MatchAndExplain(value, &inner_listener);
596 UniversalPrint(value, listener->stream());
598 const std::string& type_name = GetTypeName<Value>();
599 if (IsReadableTypeName(type_name))
600 *listener->stream() <<
" (of type " << type_name <<
")";
602 PrintIfNotEmpty(inner_listener.str(), listener->stream());
615 template <
typename MatcherTuple,
typename ValueTuple>
616 static bool Matches(
const MatcherTuple& matcher_tuple,
617 const ValueTuple& value_tuple) {
618 return TuplePrefix<N - 1>::Matches(matcher_tuple, value_tuple) &&
619 std::get<N - 1>(matcher_tuple).Matches(std::get<N - 1>(value_tuple));
626 template <
typename MatcherTuple,
typename ValueTuple>
627 static void ExplainMatchFailuresTo(
const MatcherTuple& matchers,
628 const ValueTuple& values,
629 ::std::ostream* os) {
631 TuplePrefix<N - 1>::ExplainMatchFailuresTo(matchers, values, os);
635 typename std::tuple_element<N - 1, MatcherTuple>::type matcher =
636 std::get<N - 1>(matchers);
637 typedef typename std::tuple_element<N - 1, ValueTuple>::type Value;
638 const Value& value = std::get<N - 1>(values);
639 StringMatchResultListener listener;
640 if (!matcher.MatchAndExplain(value, &listener)) {
641 *os <<
" Expected arg #" << N - 1 <<
": ";
642 std::get<N - 1>(matchers).DescribeTo(os);
643 *os <<
"\n Actual: ";
649 internal::UniversalPrint(value, os);
650 PrintIfNotEmpty(listener.str(), os);
658class TuplePrefix<0> {
660 template <
typename MatcherTuple,
typename ValueTuple>
661 static bool Matches(
const MatcherTuple& ,
662 const ValueTuple& ) {
666 template <
typename MatcherTuple,
typename ValueTuple>
667 static void ExplainMatchFailuresTo(
const MatcherTuple& ,
677template <
typename MatcherTuple,
typename ValueTuple>
678bool TupleMatches(
const MatcherTuple& matcher_tuple,
679 const ValueTuple& value_tuple) {
682 static_assert(std::tuple_size<MatcherTuple>::value ==
683 std::tuple_size<ValueTuple>::value,
684 "matcher and value have different numbers of fields");
685 return TuplePrefix<std::tuple_size<ValueTuple>::value>::Matches(matcher_tuple,
691template <
typename MatcherTuple,
typename ValueTuple>
692void ExplainMatchFailureTupleTo(
const MatcherTuple& matchers,
693 const ValueTuple& values, ::std::ostream* os) {
694 TuplePrefix<std::tuple_size<MatcherTuple>::value>::ExplainMatchFailuresTo(
695 matchers, values, os);
702template <
typename Tuple,
typename Func,
typename OutIter>
703class TransformTupleValuesHelper {
705 typedef ::std::tuple_size<Tuple> TupleSize;
710 static OutIter Run(Func f,
const Tuple& t, OutIter out) {
711 return IterateOverTuple<Tuple, TupleSize::value>()(f, t, out);
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);
722 template <
typename Tup>
723 struct IterateOverTuple<Tup, 0> {
724 OutIter operator()(Func ,
const Tup& , OutIter out)
const {
733template <
typename Tuple,
typename Func,
typename OutIter>
734OutIter TransformTupleValues(Func f,
const Tuple& t, OutIter out) {
735 return TransformTupleValuesHelper<Tuple, Func, OutIter>::Run(f, t, out);
742class AnythingMatcher {
744 using is_gtest_matcher = void;
746 template <
typename T>
747 bool MatchAndExplain(
const T& , std::ostream* )
const {
750 void DescribeTo(std::ostream* os)
const { *os <<
"is anything"; }
751 void DescribeNegationTo(::std::ostream* os)
const {
755 *os <<
"never matches";
763 template <
typename Po
inter>
764 bool MatchAndExplain(
const Pointer& p,
765 MatchResultListener* )
const {
769 void DescribeTo(::std::ostream* os)
const { *os <<
"is NULL"; }
770 void DescribeNegationTo(::std::ostream* os)
const { *os <<
"isn't NULL"; }
775class NotNullMatcher {
777 template <
typename Po
inter>
778 bool MatchAndExplain(
const Pointer& p,
779 MatchResultListener* )
const {
783 void DescribeTo(::std::ostream* os)
const { *os <<
"isn't NULL"; }
784 void DescribeNegationTo(::std::ostream* os)
const { *os <<
"is NULL"; }
804class RefMatcher<T&> {
814 explicit RefMatcher(T& x) : object_(x) {}
816 template <
typename Super>
817 operator Matcher<Super&>()
const {
823 return MakeMatcher(
new Impl<Super>(object_));
827 template <
typename Super>
828 class Impl :
public MatcherInterface<Super&> {
830 explicit Impl(Super& x) : object_(x) {}
834 bool MatchAndExplain(Super& x,
835 MatchResultListener* listener)
const override {
836 *listener <<
"which is located @" <<
static_cast<const void*
>(&x);
837 return &x == &object_;
840 void DescribeTo(::std::ostream* os)
const override {
841 *os <<
"references the variable ";
842 UniversalPrinter<Super&>::Print(object_, os);
845 void DescribeNegationTo(::std::ostream* os)
const override {
846 *os <<
"does not reference the variable ";
847 UniversalPrinter<Super&>::Print(object_, os);
851 const Super& object_;
858inline bool CaseInsensitiveCStringEquals(
const char* lhs,
const char* rhs) {
859 return String::CaseInsensitiveCStringEquals(lhs, rhs);
862inline bool CaseInsensitiveCStringEquals(
const wchar_t* lhs,
863 const wchar_t* rhs) {
864 return String::CaseInsensitiveWideCStringEquals(lhs, rhs);
869template <
typename StringType>
870bool CaseInsensitiveStringEquals(
const StringType& s1,
const StringType& s2) {
872 if (!CaseInsensitiveCStringEquals(s1.c_str(), s2.c_str())) {
877 const typename StringType::value_type nul = 0;
878 const size_t i1 = s1.find(nul), i2 = s2.find(nul);
881 if (i1 == StringType::npos || i2 == StringType::npos) {
886 return CaseInsensitiveStringEquals(s1.substr(i1 + 1), s2.substr(i2 + 1));
892template <
typename StringType>
893class StrEqualityMatcher {
895 StrEqualityMatcher(StringType str,
bool expect_eq,
bool case_sensitive)
896 : string_(std::move(str)),
897 expect_eq_(expect_eq),
898 case_sensitive_(case_sensitive) {}
900#if GTEST_INTERNAL_HAS_STRING_VIEW
901 bool MatchAndExplain(
const internal::StringView& s,
902 MatchResultListener* listener)
const {
905 const StringType& str = std::string(s);
906 return MatchAndExplain(str, listener);
915 template <
typename CharType>
916 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
920 return MatchAndExplain(StringType(s), listener);
927 template <
typename MatcheeStringType>
928 bool MatchAndExplain(
const MatcheeStringType& s,
929 MatchResultListener* )
const {
930 const StringType s2(s);
931 const bool eq = case_sensitive_ ? s2 == string_
932 : CaseInsensitiveStringEquals(s2, string_);
933 return expect_eq_ == eq;
936 void DescribeTo(::std::ostream* os)
const {
937 DescribeToHelper(expect_eq_, os);
940 void DescribeNegationTo(::std::ostream* os)
const {
941 DescribeToHelper(!expect_eq_, os);
945 void DescribeToHelper(
bool expect_eq, ::std::ostream* os)
const {
946 *os << (expect_eq ?
"is " :
"isn't ");
948 if (!case_sensitive_) {
949 *os <<
"(ignoring case) ";
951 UniversalPrint(string_, os);
954 const StringType string_;
955 const bool expect_eq_;
956 const bool case_sensitive_;
962template <
typename StringType>
963class HasSubstrMatcher {
965 explicit HasSubstrMatcher(
const StringType& substring)
966 : substring_(substring) {}
968#if GTEST_INTERNAL_HAS_STRING_VIEW
969 bool MatchAndExplain(
const internal::StringView& s,
970 MatchResultListener* listener)
const {
973 const StringType& str = std::string(s);
974 return MatchAndExplain(str, listener);
983 template <
typename CharType>
984 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
985 return s !=
nullptr && MatchAndExplain(StringType(s), listener);
992 template <
typename MatcheeStringType>
993 bool MatchAndExplain(
const MatcheeStringType& s,
994 MatchResultListener* )
const {
995 return StringType(s).find(substring_) != StringType::npos;
999 void DescribeTo(::std::ostream* os)
const {
1000 *os <<
"has substring ";
1001 UniversalPrint(substring_, os);
1004 void DescribeNegationTo(::std::ostream* os)
const {
1005 *os <<
"has no substring ";
1006 UniversalPrint(substring_, os);
1010 const StringType substring_;
1016template <
typename StringType>
1017class StartsWithMatcher {
1019 explicit StartsWithMatcher(
const StringType& prefix) : prefix_(prefix) {}
1021#if GTEST_INTERNAL_HAS_STRING_VIEW
1022 bool MatchAndExplain(
const internal::StringView& s,
1023 MatchResultListener* listener)
const {
1026 const StringType& str = std::string(s);
1027 return MatchAndExplain(str, listener);
1036 template <
typename CharType>
1037 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
1038 return s !=
nullptr && MatchAndExplain(StringType(s), listener);
1045 template <
typename MatcheeStringType>
1046 bool MatchAndExplain(
const MatcheeStringType& s,
1047 MatchResultListener* )
const {
1048 const StringType& s2(s);
1049 return s2.length() >= prefix_.length() &&
1050 s2.substr(0, prefix_.length()) == prefix_;
1053 void DescribeTo(::std::ostream* os)
const {
1054 *os <<
"starts with ";
1055 UniversalPrint(prefix_, os);
1058 void DescribeNegationTo(::std::ostream* os)
const {
1059 *os <<
"doesn't start with ";
1060 UniversalPrint(prefix_, os);
1064 const StringType prefix_;
1070template <
typename StringType>
1071class EndsWithMatcher {
1073 explicit EndsWithMatcher(
const StringType& suffix) : suffix_(suffix) {}
1075#if GTEST_INTERNAL_HAS_STRING_VIEW
1076 bool MatchAndExplain(
const internal::StringView& s,
1077 MatchResultListener* listener)
const {
1080 const StringType& str = std::string(s);
1081 return MatchAndExplain(str, listener);
1090 template <
typename CharType>
1091 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
1092 return s !=
nullptr && MatchAndExplain(StringType(s), listener);
1099 template <
typename MatcheeStringType>
1100 bool MatchAndExplain(
const MatcheeStringType& s,
1101 MatchResultListener* )
const {
1102 const StringType& s2(s);
1103 return s2.length() >= suffix_.length() &&
1104 s2.substr(s2.length() - suffix_.length()) == suffix_;
1107 void DescribeTo(::std::ostream* os)
const {
1108 *os <<
"ends with ";
1109 UniversalPrint(suffix_, os);
1112 void DescribeNegationTo(::std::ostream* os)
const {
1113 *os <<
"doesn't end with ";
1114 UniversalPrint(suffix_, os);
1118 const StringType suffix_;
1123class WhenBase64UnescapedMatcher {
1125 using is_gtest_matcher = void;
1127 explicit WhenBase64UnescapedMatcher(
1128 const Matcher<const std::string&>& internal_matcher)
1129 : internal_matcher_(internal_matcher) {}
1132 template <
typename MatcheeStringType>
1133 bool MatchAndExplain(
const MatcheeStringType& s,
1134 MatchResultListener* listener)
const {
1135 const std::string s2(s);
1136 std::string unescaped;
1137 if (!internal::Base64Unescape(s2, &unescaped)) {
1138 if (listener !=
nullptr) {
1139 *listener <<
"is not a valid base64 escaped string";
1143 return MatchPrintAndExplain(unescaped, internal_matcher_, listener);
1146 void DescribeTo(::std::ostream* os)
const {
1147 *os <<
"matches after Base64Unescape ";
1148 internal_matcher_.DescribeTo(os);
1151 void DescribeNegationTo(::std::ostream* os)
const {
1152 *os <<
"does not match after Base64Unescape ";
1153 internal_matcher_.DescribeTo(os);
1157 const Matcher<const std::string&> internal_matcher_;
1168template <
typename D,
typename Op>
1169class PairMatchBase {
1171 template <
typename T1,
typename T2>
1172 operator Matcher<::std::tuple<T1, T2>>()
const {
1173 return Matcher<::std::tuple<T1, T2>>(
new Impl<const ::std::tuple<T1, T2>&>);
1175 template <
typename T1,
typename T2>
1176 operator Matcher<const ::std::tuple<T1, T2>&>()
const {
1177 return MakeMatcher(
new Impl<const ::std::tuple<T1, T2>&>);
1181 static ::std::ostream& GetDesc(::std::ostream& os) {
1182 return os << D::Desc();
1185 template <
typename Tuple>
1186 class Impl :
public MatcherInterface<Tuple> {
1188 bool MatchAndExplain(Tuple args,
1189 MatchResultListener* )
const override {
1190 return Op()(::std::get<0>(args), ::std::get<1>(args));
1192 void DescribeTo(::std::ostream* os)
const override {
1193 *os <<
"are " << GetDesc;
1195 void DescribeNegationTo(::std::ostream* os)
const override {
1196 *os <<
"aren't " << GetDesc;
1201class Eq2Matcher :
public PairMatchBase<Eq2Matcher, AnyEq> {
1203 static const char* Desc() {
return "an equal pair"; }
1205class Ne2Matcher :
public PairMatchBase<Ne2Matcher, AnyNe> {
1207 static const char* Desc() {
return "an unequal pair"; }
1209class Lt2Matcher :
public PairMatchBase<Lt2Matcher, AnyLt> {
1211 static const char* Desc() {
return "a pair where the first < the second"; }
1213class Gt2Matcher :
public PairMatchBase<Gt2Matcher, AnyGt> {
1215 static const char* Desc() {
return "a pair where the first > the second"; }
1217class Le2Matcher :
public PairMatchBase<Le2Matcher, AnyLe> {
1219 static const char* Desc() {
return "a pair where the first <= the second"; }
1221class Ge2Matcher :
public PairMatchBase<Ge2Matcher, AnyGe> {
1223 static const char* Desc() {
return "a pair where the first >= the second"; }
1230template <
typename T>
1231class NotMatcherImpl :
public MatcherInterface<const T&> {
1233 explicit NotMatcherImpl(
const Matcher<T>& matcher) : matcher_(matcher) {}
1235 bool MatchAndExplain(
const T& x,
1236 MatchResultListener* listener)
const override {
1237 return !matcher_.MatchAndExplain(x, listener);
1240 void DescribeTo(::std::ostream* os)
const override {
1241 matcher_.DescribeNegationTo(os);
1244 void DescribeNegationTo(::std::ostream* os)
const override {
1245 matcher_.DescribeTo(os);
1249 const Matcher<T> matcher_;
1254template <
typename InnerMatcher>
1257 explicit NotMatcher(InnerMatcher matcher) : matcher_(matcher) {}
1261 template <
typename T>
1262 operator Matcher<T>()
const {
1263 return Matcher<T>(
new NotMatcherImpl<T>(SafeMatcherCast<T>(matcher_)));
1267 InnerMatcher matcher_;
1274template <
typename T>
1275class AllOfMatcherImpl :
public MatcherInterface<const T&> {
1277 explicit AllOfMatcherImpl(std::vector<Matcher<T>> matchers)
1278 : matchers_(std::move(matchers)) {}
1280 void DescribeTo(::std::ostream* os)
const override {
1282 for (
size_t i = 0; i < matchers_.size(); ++i) {
1283 if (i != 0) *os <<
") and (";
1284 matchers_[i].DescribeTo(os);
1289 void DescribeNegationTo(::std::ostream* os)
const override {
1291 for (
size_t i = 0; i < matchers_.size(); ++i) {
1292 if (i != 0) *os <<
") or (";
1293 matchers_[i].DescribeNegationTo(os);
1298 bool MatchAndExplain(
const T& x,
1299 MatchResultListener* listener)
const override {
1302 std::string all_match_result;
1304 for (
size_t i = 0; i < matchers_.size(); ++i) {
1305 StringMatchResultListener slistener;
1306 if (matchers_[i].MatchAndExplain(x, &slistener)) {
1307 if (all_match_result.empty()) {
1308 all_match_result = slistener.str();
1310 std::string result = slistener.str();
1311 if (!result.empty()) {
1312 all_match_result +=
", and ";
1313 all_match_result += result;
1317 *listener << slistener.str();
1323 *listener << all_match_result;
1328 const std::vector<Matcher<T>> matchers_;
1335template <
template <
typename T>
class CombiningMatcher,
typename... Args>
1336class VariadicMatcher {
1338 VariadicMatcher(
const Args&... matchers)
1339 : matchers_(matchers...) {
1340 static_assert(
sizeof...(Args) > 0,
"Must have at least one matcher.");
1343 VariadicMatcher(
const VariadicMatcher&) =
default;
1344 VariadicMatcher& operator=(
const VariadicMatcher&) =
delete;
1349 template <
typename T>
1350 operator Matcher<T>()
const {
1351 std::vector<Matcher<T>> values;
1352 CreateVariadicMatcher<T>(&values, std::integral_constant<size_t, 0>());
1353 return Matcher<T>(
new CombiningMatcher<T>(std::move(values)));
1357 template <
typename T,
size_t I>
1358 void CreateVariadicMatcher(std::vector<Matcher<T>>* values,
1359 std::integral_constant<size_t, I>)
const {
1360 values->push_back(SafeMatcherCast<T>(std::get<I>(matchers_)));
1361 CreateVariadicMatcher<T>(values, std::integral_constant<size_t, I + 1>());
1364 template <
typename T>
1365 void CreateVariadicMatcher(
1366 std::vector<Matcher<T>>*,
1367 std::integral_constant<
size_t,
sizeof...(Args)>)
const {}
1369 std::tuple<Args...> matchers_;
1372template <
typename... Args>
1373using AllOfMatcher = VariadicMatcher<AllOfMatcherImpl, Args...>;
1379template <
typename T>
1380class AnyOfMatcherImpl :
public MatcherInterface<const T&> {
1382 explicit AnyOfMatcherImpl(std::vector<Matcher<T>> matchers)
1383 : matchers_(std::move(matchers)) {}
1385 void DescribeTo(::std::ostream* os)
const override {
1387 for (
size_t i = 0; i < matchers_.size(); ++i) {
1388 if (i != 0) *os <<
") or (";
1389 matchers_[i].DescribeTo(os);
1394 void DescribeNegationTo(::std::ostream* os)
const override {
1396 for (
size_t i = 0; i < matchers_.size(); ++i) {
1397 if (i != 0) *os <<
") and (";
1398 matchers_[i].DescribeNegationTo(os);
1403 bool MatchAndExplain(
const T& x,
1404 MatchResultListener* listener)
const override {
1405 std::string no_match_result;
1409 for (
size_t i = 0; i < matchers_.size(); ++i) {
1410 StringMatchResultListener slistener;
1411 if (matchers_[i].MatchAndExplain(x, &slistener)) {
1412 *listener << slistener.str();
1415 if (no_match_result.empty()) {
1416 no_match_result = slistener.str();
1418 std::string result = slistener.str();
1419 if (!result.empty()) {
1420 no_match_result +=
", and ";
1421 no_match_result += result;
1428 *listener << no_match_result;
1433 const std::vector<Matcher<T>> matchers_;
1437template <
typename... Args>
1438using AnyOfMatcher = VariadicMatcher<AnyOfMatcherImpl, Args...>;
1441template <
typename MatcherTrue,
typename MatcherFalse>
1442class ConditionalMatcher {
1444 ConditionalMatcher(
bool condition, MatcherTrue matcher_true,
1445 MatcherFalse matcher_false)
1446 : condition_(condition),
1447 matcher_true_(std::move(matcher_true)),
1448 matcher_false_(std::move(matcher_false)) {}
1450 template <
typename T>
1451 operator Matcher<T>()
const {
1452 return condition_ ? SafeMatcherCast<T>(matcher_true_)
1453 : SafeMatcherCast<T>(matcher_false_);
1458 MatcherTrue matcher_true_;
1459 MatcherFalse matcher_false_;
1463template <
template <
class>
class MatcherImpl,
typename T>
1464class SomeOfArrayMatcher {
1468 template <
typename Iter>
1469 SomeOfArrayMatcher(Iter first, Iter last) : matchers_(first, last) {}
1471 template <
typename U>
1472 operator Matcher<U>()
const {
1473 using RawU =
typename std::decay<U>::type;
1474 std::vector<Matcher<RawU>> matchers;
1475 for (
const auto& matcher : matchers_) {
1476 matchers.push_back(MatcherCast<RawU>(matcher));
1478 return Matcher<U>(
new MatcherImpl<RawU>(std::move(matchers)));
1482 const ::std::vector<T> matchers_;
1485template <
typename T>
1486using AllOfArrayMatcher = SomeOfArrayMatcher<AllOfMatcherImpl, T>;
1488template <
typename T>
1489using AnyOfArrayMatcher = SomeOfArrayMatcher<AnyOfMatcherImpl, T>;
1493template <
typename Predicate>
1496 explicit TrulyMatcher(Predicate pred) : predicate_(pred) {}
1502 template <
typename T>
1503 bool MatchAndExplain(T& x,
1504 MatchResultListener* listener)
const {
1511 if (predicate_(x))
return true;
1512 *listener <<
"didn't satisfy the given predicate";
1516 void DescribeTo(::std::ostream* os)
const {
1517 *os <<
"satisfies the given predicate";
1520 void DescribeNegationTo(::std::ostream* os)
const {
1521 *os <<
"doesn't satisfy the given predicate";
1525 Predicate predicate_;
1530template <
typename M>
1531class MatcherAsPredicate {
1533 explicit MatcherAsPredicate(M matcher) : matcher_(matcher) {}
1541 template <
typename T>
1542 bool operator()(
const T& x)
const {
1557 return MatcherCast<const T&>(matcher_).Matches(x);
1566template <
typename M>
1567class PredicateFormatterFromMatcher {
1569 explicit PredicateFormatterFromMatcher(M m) : matcher_(std::move(m)) {}
1574 template <
typename T>
1575 AssertionResult operator()(
const char* value_text,
const T& x)
const {
1587 const Matcher<const T&> matcher = SafeMatcherCast<const T&>(matcher_);
1591 if (matcher.Matches(x)) {
1592 return AssertionSuccess();
1595 ::std::stringstream ss;
1596 ss <<
"Value of: " << value_text <<
"\n"
1598 matcher.DescribeTo(&ss);
1601 StringMatchResultListener listener;
1602 if (MatchPrintAndExplain(x, matcher, &listener)) {
1603 ss <<
"\n The matcher failed on the initial attempt; but passed when "
1604 "rerun to generate the explanation.";
1606 ss <<
"\n Actual: " << listener.str();
1607 return AssertionFailure() << ss.str();
1618template <
typename M>
1619inline PredicateFormatterFromMatcher<M> MakePredicateFormatterFromMatcher(
1621 return PredicateFormatterFromMatcher<M>(std::move(matcher));
1628 template <
typename FloatType>
1629 bool MatchAndExplain(
const FloatType& f,
1630 MatchResultListener* )
const {
1631 return (::std::isnan)(f);
1634 void DescribeTo(::std::ostream* os)
const { *os <<
"is NaN"; }
1635 void DescribeNegationTo(::std::ostream* os)
const { *os <<
"isn't NaN"; }
1642template <
typename FloatType>
1643class FloatingEqMatcher {
1651 FloatingEqMatcher(FloatType expected,
bool nan_eq_nan)
1652 : expected_(expected), nan_eq_nan_(nan_eq_nan), max_abs_error_(-1) {}
1657 FloatingEqMatcher(FloatType expected,
bool nan_eq_nan,
1658 FloatType max_abs_error)
1659 : expected_(expected),
1660 nan_eq_nan_(nan_eq_nan),
1661 max_abs_error_(max_abs_error) {
1662 GTEST_CHECK_(max_abs_error >= 0)
1663 <<
", where max_abs_error is" << max_abs_error;
1667 template <
typename T>
1668 class Impl :
public MatcherInterface<T> {
1670 Impl(FloatType expected,
bool nan_eq_nan, FloatType max_abs_error)
1671 : expected_(expected),
1672 nan_eq_nan_(nan_eq_nan),
1673 max_abs_error_(max_abs_error) {}
1675 bool MatchAndExplain(T value,
1676 MatchResultListener* listener)
const override {
1677 const FloatingPoint<FloatType> actual(value), expected(expected_);
1680 if (actual.is_nan() || expected.is_nan()) {
1681 if (actual.is_nan() && expected.is_nan()) {
1687 if (HasMaxAbsError()) {
1692 if (value == expected_) {
1696 const FloatType diff = value - expected_;
1697 if (::std::fabs(diff) <= max_abs_error_) {
1701 if (listener->IsInterested()) {
1702 *listener <<
"which is " << diff <<
" from " << expected_;
1706 return actual.AlmostEquals(expected);
1710 void DescribeTo(::std::ostream* os)
const override {
1714 const ::std::streamsize old_precision =
1715 os->precision(::std::numeric_limits<FloatType>::digits10 + 2);
1716 if (FloatingPoint<FloatType>(expected_).is_nan()) {
1720 *os <<
"never matches";
1723 *os <<
"is approximately " << expected_;
1724 if (HasMaxAbsError()) {
1725 *os <<
" (absolute error <= " << max_abs_error_ <<
")";
1728 os->precision(old_precision);
1731 void DescribeNegationTo(::std::ostream* os)
const override {
1733 const ::std::streamsize old_precision =
1734 os->precision(::std::numeric_limits<FloatType>::digits10 + 2);
1735 if (FloatingPoint<FloatType>(expected_).is_nan()) {
1739 *os <<
"is anything";
1742 *os <<
"isn't approximately " << expected_;
1743 if (HasMaxAbsError()) {
1744 *os <<
" (absolute error > " << max_abs_error_ <<
")";
1748 os->precision(old_precision);
1752 bool HasMaxAbsError()
const {
return max_abs_error_ >= 0; }
1754 const FloatType expected_;
1755 const bool nan_eq_nan_;
1757 const FloatType max_abs_error_;
1763 operator Matcher<FloatType>()
const {
1765 new Impl<FloatType>(expected_, nan_eq_nan_, max_abs_error_));
1768 operator Matcher<const FloatType&>()
const {
1770 new Impl<const FloatType&>(expected_, nan_eq_nan_, max_abs_error_));
1773 operator Matcher<FloatType&>()
const {
1775 new Impl<FloatType&>(expected_, nan_eq_nan_, max_abs_error_));
1779 const FloatType expected_;
1780 const bool nan_eq_nan_;
1782 const FloatType max_abs_error_;
1790template <
typename FloatType>
1791class FloatingEq2Matcher {
1793 FloatingEq2Matcher() { Init(-1,
false); }
1795 explicit FloatingEq2Matcher(
bool nan_eq_nan) { Init(-1, nan_eq_nan); }
1797 explicit FloatingEq2Matcher(FloatType max_abs_error) {
1798 Init(max_abs_error,
false);
1801 FloatingEq2Matcher(FloatType max_abs_error,
bool nan_eq_nan) {
1802 Init(max_abs_error, nan_eq_nan);
1805 template <
typename T1,
typename T2>
1806 operator Matcher<::std::tuple<T1, T2>>()
const {
1808 new Impl<::std::tuple<T1, T2>>(max_abs_error_, nan_eq_nan_));
1810 template <
typename T1,
typename T2>
1811 operator Matcher<const ::std::tuple<T1, T2>&>()
const {
1813 new Impl<const ::std::tuple<T1, T2>&>(max_abs_error_, nan_eq_nan_));
1817 static ::std::ostream& GetDesc(::std::ostream& os) {
1818 return os <<
"an almost-equal pair";
1821 template <
typename Tuple>
1822 class Impl :
public MatcherInterface<Tuple> {
1824 Impl(FloatType max_abs_error,
bool nan_eq_nan)
1825 : max_abs_error_(max_abs_error), nan_eq_nan_(nan_eq_nan) {}
1827 bool MatchAndExplain(Tuple args,
1828 MatchResultListener* listener)
const override {
1829 if (max_abs_error_ == -1) {
1830 FloatingEqMatcher<FloatType> fm(::std::get<0>(args), nan_eq_nan_);
1831 return static_cast<Matcher<FloatType>
>(fm).MatchAndExplain(
1832 ::std::get<1>(args), listener);
1834 FloatingEqMatcher<FloatType> fm(::std::get<0>(args), nan_eq_nan_,
1836 return static_cast<Matcher<FloatType>
>(fm).MatchAndExplain(
1837 ::std::get<1>(args), listener);
1840 void DescribeTo(::std::ostream* os)
const override {
1841 *os <<
"are " << GetDesc;
1843 void DescribeNegationTo(::std::ostream* os)
const override {
1844 *os <<
"aren't " << GetDesc;
1848 FloatType max_abs_error_;
1849 const bool nan_eq_nan_;
1852 void Init(FloatType max_abs_error_val,
bool nan_eq_nan_val) {
1853 max_abs_error_ = max_abs_error_val;
1854 nan_eq_nan_ = nan_eq_nan_val;
1856 FloatType max_abs_error_;
1862template <
typename InnerMatcher>
1863class PointeeMatcher {
1865 explicit PointeeMatcher(
const InnerMatcher& matcher) : matcher_(matcher) {}
1875 template <
typename Po
inter>
1876 operator Matcher<Pointer>()
const {
1877 return Matcher<Pointer>(
new Impl<const Pointer&>(matcher_));
1882 template <
typename Po
inter>
1883 class Impl :
public MatcherInterface<Pointer> {
1886 typename std::pointer_traits<GTEST_REMOVE_REFERENCE_AND_CONST_(
1887 Pointer)>::element_type;
1889 explicit Impl(
const InnerMatcher& matcher)
1890 : matcher_(MatcherCast<const Pointee&>(matcher)) {}
1892 void DescribeTo(::std::ostream* os)
const override {
1893 *os <<
"points to a value that ";
1894 matcher_.DescribeTo(os);
1897 void DescribeNegationTo(::std::ostream* os)
const override {
1898 *os <<
"does not point to a value that ";
1899 matcher_.DescribeTo(os);
1902 bool MatchAndExplain(Pointer pointer,
1903 MatchResultListener* listener)
const override {
1904 if (GetRawPointer(pointer) ==
nullptr)
return false;
1906 *listener <<
"which points to ";
1907 return MatchPrintAndExplain(*pointer, matcher_, listener);
1911 const Matcher<const Pointee&> matcher_;
1914 const InnerMatcher matcher_;
1921template <
typename InnerMatcher>
1922class PointerMatcher {
1924 explicit PointerMatcher(
const InnerMatcher& matcher) : matcher_(matcher) {}
1934 template <
typename Po
interType>
1935 operator Matcher<PointerType>()
const {
1936 return Matcher<PointerType>(
new Impl<const PointerType&>(matcher_));
1941 template <
typename Po
interType>
1942 class Impl :
public MatcherInterface<PointerType> {
1945 const typename std::pointer_traits<GTEST_REMOVE_REFERENCE_AND_CONST_(
1946 PointerType)>::element_type*;
1948 explicit Impl(
const InnerMatcher& matcher)
1949 : matcher_(MatcherCast<Pointer>(matcher)) {}
1951 void DescribeTo(::std::ostream* os)
const override {
1952 *os <<
"is a pointer that ";
1953 matcher_.DescribeTo(os);
1956 void DescribeNegationTo(::std::ostream* os)
const override {
1957 *os <<
"is not a pointer that ";
1958 matcher_.DescribeTo(os);
1961 bool MatchAndExplain(PointerType pointer,
1962 MatchResultListener* listener)
const override {
1963 *listener <<
"which is a pointer that ";
1964 Pointer p = GetRawPointer(pointer);
1965 return MatchPrintAndExplain(p, matcher_, listener);
1969 Matcher<Pointer> matcher_;
1972 const InnerMatcher matcher_;
1982template <
typename To>
1983class WhenDynamicCastToMatcherBase {
1985 explicit WhenDynamicCastToMatcherBase(
const Matcher<To>& matcher)
1986 : matcher_(matcher) {}
1988 void DescribeTo(::std::ostream* os)
const {
1989 GetCastTypeDescription(os);
1990 matcher_.DescribeTo(os);
1993 void DescribeNegationTo(::std::ostream* os)
const {
1994 GetCastTypeDescription(os);
1995 matcher_.DescribeNegationTo(os);
1999 const Matcher<To> matcher_;
2001 static std::string GetToName() {
return GetTypeName<To>(); }
2004 static void GetCastTypeDescription(::std::ostream* os) {
2005 *os <<
"when dynamic_cast to " << GetToName() <<
", ";
2011template <
typename To>
2012class WhenDynamicCastToMatcher :
public WhenDynamicCastToMatcherBase<To> {
2014 explicit WhenDynamicCastToMatcher(
const Matcher<To>& matcher)
2015 : WhenDynamicCastToMatcherBase<To>(matcher) {}
2017 template <
typename From>
2018 bool MatchAndExplain(From from, MatchResultListener* listener)
const {
2019 To to =
dynamic_cast<To
>(from);
2020 return MatchPrintAndExplain(to, this->matcher_, listener);
2026template <
typename To>
2027class WhenDynamicCastToMatcher<To&> :
public WhenDynamicCastToMatcherBase<To&> {
2029 explicit WhenDynamicCastToMatcher(
const Matcher<To&>& matcher)
2030 : WhenDynamicCastToMatcherBase<To&>(matcher) {}
2032 template <
typename From>
2033 bool MatchAndExplain(From& from, MatchResultListener* listener)
const {
2035 To* to =
dynamic_cast<To*
>(&from);
2036 if (to ==
nullptr) {
2037 *listener <<
"which cannot be dynamic_cast to " << this->GetToName();
2040 return MatchPrintAndExplain(*to, this->matcher_, listener);
2047template <
typename Class,
typename FieldType>
2050 FieldMatcher(FieldType Class::*field,
2051 const Matcher<const FieldType&>& matcher)
2052 : field_(field), matcher_(matcher), whose_field_(
"whose given field ") {}
2054 FieldMatcher(
const std::string& field_name, FieldType Class::*field,
2055 const Matcher<const FieldType&>& matcher)
2058 whose_field_(
"whose field `" + field_name +
"` ") {}
2060 void DescribeTo(::std::ostream* os)
const {
2061 *os <<
"is an object " << whose_field_;
2062 matcher_.DescribeTo(os);
2065 void DescribeNegationTo(::std::ostream* os)
const {
2066 *os <<
"is an object " << whose_field_;
2067 matcher_.DescribeNegationTo(os);
2070 template <
typename T>
2071 bool MatchAndExplain(
const T& value, MatchResultListener* listener)
const {
2074 return MatchAndExplainImpl(
2075 typename std::is_pointer<
typename std::remove_const<T>::type>::type(),
2080 bool MatchAndExplainImpl(std::false_type ,
2082 MatchResultListener* listener)
const {
2083 *listener << whose_field_ <<
"is ";
2084 return MatchPrintAndExplain(obj.*field_, matcher_, listener);
2087 bool MatchAndExplainImpl(std::true_type ,
const Class* p,
2088 MatchResultListener* listener)
const {
2089 if (p ==
nullptr)
return false;
2091 *listener <<
"which points to an object ";
2095 return MatchAndExplainImpl(std::false_type(), *p, listener);
2098 const FieldType Class::*field_;
2099 const Matcher<const FieldType&> matcher_;
2103 const std::string whose_field_;
2111template <
typename Class,
typename PropertyType,
typename Property>
2112class PropertyMatcher {
2114 typedef const PropertyType& RefToConstProperty;
2116 PropertyMatcher(Property property,
const Matcher<RefToConstProperty>& matcher)
2117 : property_(property),
2119 whose_property_(
"whose given property ") {}
2121 PropertyMatcher(
const std::string& property_name, Property property,
2122 const Matcher<RefToConstProperty>& matcher)
2123 : property_(property),
2125 whose_property_(
"whose property `" + property_name +
"` ") {}
2127 void DescribeTo(::std::ostream* os)
const {
2128 *os <<
"is an object " << whose_property_;
2129 matcher_.DescribeTo(os);
2132 void DescribeNegationTo(::std::ostream* os)
const {
2133 *os <<
"is an object " << whose_property_;
2134 matcher_.DescribeNegationTo(os);
2137 template <
typename T>
2138 bool MatchAndExplain(
const T& value, MatchResultListener* listener)
const {
2139 return MatchAndExplainImpl(
2140 typename std::is_pointer<
typename std::remove_const<T>::type>::type(),
2145 bool MatchAndExplainImpl(std::false_type ,
2147 MatchResultListener* listener)
const {
2148 *listener << whose_property_ <<
"is ";
2151 RefToConstProperty result = (obj.*property_)();
2152 return MatchPrintAndExplain(result, matcher_, listener);
2155 bool MatchAndExplainImpl(std::true_type ,
const Class* p,
2156 MatchResultListener* listener)
const {
2157 if (p ==
nullptr)
return false;
2159 *listener <<
"which points to an object ";
2163 return MatchAndExplainImpl(std::false_type(), *p, listener);
2167 const Matcher<RefToConstProperty> matcher_;
2171 const std::string whose_property_;
2176template <
typename Functor>
2177struct CallableTraits {
2178 typedef Functor StorageType;
2180 static void CheckIsValid(Functor ) {}
2182 template <
typename T>
2183 static auto Invoke(Functor f,
const T& arg) ->
decltype(f(arg)) {
2189template <
typename ArgType,
typename ResType>
2190struct CallableTraits<ResType (*)(ArgType)> {
2191 typedef ResType ResultType;
2192 typedef ResType (*StorageType)(ArgType);
2194 static void CheckIsValid(ResType (*f)(ArgType)) {
2195 GTEST_CHECK_(f !=
nullptr)
2196 <<
"NULL function pointer is passed into ResultOf().";
2198 template <
typename T>
2199 static ResType
Invoke(ResType (*f)(ArgType), T arg) {
2206template <
typename Callable,
typename InnerMatcher>
2207class ResultOfMatcher {
2209 ResultOfMatcher(Callable callable, InnerMatcher matcher)
2210 : ResultOfMatcher(
"", std::move(callable),
2211 std::move(matcher)) {}
2213 ResultOfMatcher(
const std::string& result_description, Callable callable,
2214 InnerMatcher matcher)
2215 : result_description_(result_description),
2216 callable_(std::move(callable)),
2217 matcher_(std::move(matcher)) {
2218 CallableTraits<Callable>::CheckIsValid(callable_);
2221 template <
typename T>
2222 operator Matcher<T>()
const {
2224 new Impl<const T&>(result_description_, callable_, matcher_));
2228 typedef typename CallableTraits<Callable>::StorageType CallableStorageType;
2230 template <
typename T>
2231 class Impl :
public MatcherInterface<T> {
2232 using ResultType =
decltype(CallableTraits<Callable>::template Invoke<T>(
2233 std::declval<CallableStorageType>(), std::declval<T>()));
2236 template <
typename M>
2237 Impl(
const std::string& result_description,
2238 const CallableStorageType& callable,
const M& matcher)
2239 : result_description_(result_description),
2240 callable_(callable),
2241 matcher_(MatcherCast<ResultType>(matcher)) {}
2243 void DescribeTo(::std::ostream* os)
const override {
2244 if (result_description_.empty()) {
2245 *os <<
"is mapped by the given callable to a value that ";
2247 *os <<
"whose " << result_description_ <<
" ";
2249 matcher_.DescribeTo(os);
2252 void DescribeNegationTo(::std::ostream* os)
const override {
2253 if (result_description_.empty()) {
2254 *os <<
"is mapped by the given callable to a value that ";
2256 *os <<
"whose " << result_description_ <<
" ";
2258 matcher_.DescribeNegationTo(os);
2261 bool MatchAndExplain(T obj, MatchResultListener* listener)
const override {
2262 if (result_description_.empty()) {
2263 *listener <<
"which is mapped by the given callable to ";
2265 *listener <<
"whose " << result_description_ <<
" is ";
2272 CallableTraits<Callable>::template Invoke<T>(callable_, obj);
2273 return MatchPrintAndExplain(result, matcher_, listener);
2277 const std::string result_description_;
2283 mutable CallableStorageType callable_;
2284 const Matcher<ResultType> matcher_;
2287 const std::string result_description_;
2288 const CallableStorageType callable_;
2289 const InnerMatcher matcher_;
2293template <
typename SizeMatcher>
2294class SizeIsMatcher {
2296 explicit SizeIsMatcher(
const SizeMatcher& size_matcher)
2297 : size_matcher_(size_matcher) {}
2299 template <
typename Container>
2300 operator Matcher<Container>()
const {
2301 return Matcher<Container>(
new Impl<const Container&>(size_matcher_));
2304 template <
typename Container>
2305 class Impl :
public MatcherInterface<Container> {
2307 using SizeType =
decltype(std::declval<Container>().size());
2308 explicit Impl(
const SizeMatcher& size_matcher)
2309 : size_matcher_(MatcherCast<SizeType>(size_matcher)) {}
2311 void DescribeTo(::std::ostream* os)
const override {
2313 size_matcher_.DescribeTo(os);
2315 void DescribeNegationTo(::std::ostream* os)
const override {
2317 size_matcher_.DescribeNegationTo(os);
2320 bool MatchAndExplain(Container container,
2321 MatchResultListener* listener)
const override {
2322 SizeType size = container.size();
2323 StringMatchResultListener size_listener;
2324 const bool result = size_matcher_.MatchAndExplain(size, &size_listener);
2325 *listener <<
"whose size " << size
2326 << (result ?
" matches" :
" doesn't match");
2327 PrintIfNotEmpty(size_listener.str(), listener->stream());
2332 const Matcher<SizeType> size_matcher_;
2336 const SizeMatcher size_matcher_;
2341template <
typename DistanceMatcher>
2342class BeginEndDistanceIsMatcher {
2344 explicit BeginEndDistanceIsMatcher(
const DistanceMatcher& distance_matcher)
2345 : distance_matcher_(distance_matcher) {}
2347 template <
typename Container>
2348 operator Matcher<Container>()
const {
2349 return Matcher<Container>(
new Impl<const Container&>(distance_matcher_));
2352 template <
typename Container>
2353 class Impl :
public MatcherInterface<Container> {
2355 typedef internal::StlContainerView<GTEST_REMOVE_REFERENCE_AND_CONST_(
2358 typedef typename std::iterator_traits<
2359 typename ContainerView::type::const_iterator>::difference_type
2361 explicit Impl(
const DistanceMatcher& distance_matcher)
2362 : distance_matcher_(MatcherCast<DistanceType>(distance_matcher)) {}
2364 void DescribeTo(::std::ostream* os)
const override {
2365 *os <<
"distance between begin() and end() ";
2366 distance_matcher_.DescribeTo(os);
2368 void DescribeNegationTo(::std::ostream* os)
const override {
2369 *os <<
"distance between begin() and end() ";
2370 distance_matcher_.DescribeNegationTo(os);
2373 bool MatchAndExplain(Container container,
2374 MatchResultListener* listener)
const override {
2377 DistanceType distance = std::distance(begin(container), end(container));
2378 StringMatchResultListener distance_listener;
2380 distance_matcher_.MatchAndExplain(distance, &distance_listener);
2381 *listener <<
"whose distance between begin() and end() " << distance
2382 << (result ?
" matches" :
" doesn't match");
2383 PrintIfNotEmpty(distance_listener.str(), listener->stream());
2388 const Matcher<DistanceType> distance_matcher_;
2392 const DistanceMatcher distance_matcher_;
2405template <
typename Container>
2406class ContainerEqMatcher {
2408 typedef internal::StlContainerView<Container> View;
2409 typedef typename View::type StlContainer;
2410 typedef typename View::const_reference StlContainerReference;
2412 static_assert(!std::is_const<Container>::value,
2413 "Container type must not be const");
2414 static_assert(!std::is_reference<Container>::value,
2415 "Container type must not be a reference");
2419 explicit ContainerEqMatcher(
const Container& expected)
2420 : expected_(View::Copy(expected)) {}
2422 void DescribeTo(::std::ostream* os)
const {
2424 UniversalPrint(expected_, os);
2426 void DescribeNegationTo(::std::ostream* os)
const {
2427 *os <<
"does not equal ";
2428 UniversalPrint(expected_, os);
2431 template <
typename LhsContainer>
2432 bool MatchAndExplain(
const LhsContainer& lhs,
2433 MatchResultListener* listener)
const {
2434 typedef internal::StlContainerView<
2435 typename std::remove_const<LhsContainer>::type>
2437 StlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2438 if (lhs_stl_container == expected_)
return true;
2440 ::std::ostream*
const os = listener->stream();
2441 if (os !=
nullptr) {
2443 bool printed_header =
false;
2444 for (
auto it = lhs_stl_container.begin(); it != lhs_stl_container.end();
2446 if (internal::ArrayAwareFind(expected_.begin(), expected_.end(), *it) ==
2448 if (printed_header) {
2451 *os <<
"which has these unexpected elements: ";
2452 printed_header =
true;
2454 UniversalPrint(*it, os);
2459 bool printed_header2 =
false;
2460 for (
auto it = expected_.begin(); it != expected_.end(); ++it) {
2461 if (internal::ArrayAwareFind(lhs_stl_container.begin(),
2462 lhs_stl_container.end(),
2463 *it) == lhs_stl_container.end()) {
2464 if (printed_header2) {
2467 *os << (printed_header ?
",\nand" :
"which")
2468 <<
" doesn't have these expected elements: ";
2469 printed_header2 =
true;
2471 UniversalPrint(*it, os);
2480 const StlContainer expected_;
2484struct LessComparator {
2485 template <
typename T,
typename U>
2486 bool operator()(
const T& lhs,
const U& rhs)
const {
2492template <
typename Comparator,
typename ContainerMatcher>
2493class WhenSortedByMatcher {
2495 WhenSortedByMatcher(
const Comparator& comparator,
2496 const ContainerMatcher& matcher)
2497 : comparator_(comparator), matcher_(matcher) {}
2499 template <
typename LhsContainer>
2500 operator Matcher<LhsContainer>()
const {
2501 return MakeMatcher(
new Impl<LhsContainer>(comparator_, matcher_));
2504 template <
typename LhsContainer>
2505 class Impl :
public MatcherInterface<LhsContainer> {
2507 typedef internal::StlContainerView<GTEST_REMOVE_REFERENCE_AND_CONST_(
2510 typedef typename LhsView::type LhsStlContainer;
2511 typedef typename LhsView::const_reference LhsStlContainerReference;
2515 typename RemoveConstFromKey<typename LhsStlContainer::value_type>::type
2518 Impl(
const Comparator& comparator,
const ContainerMatcher& matcher)
2519 : comparator_(comparator), matcher_(matcher) {}
2521 void DescribeTo(::std::ostream* os)
const override {
2522 *os <<
"(when sorted) ";
2523 matcher_.DescribeTo(os);
2526 void DescribeNegationTo(::std::ostream* os)
const override {
2527 *os <<
"(when sorted) ";
2528 matcher_.DescribeNegationTo(os);
2531 bool MatchAndExplain(LhsContainer lhs,
2532 MatchResultListener* listener)
const override {
2533 LhsStlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2534 ::std::vector<LhsValue> sorted_container(lhs_stl_container.begin(),
2535 lhs_stl_container.end());
2536 ::std::sort(sorted_container.begin(), sorted_container.end(),
2539 if (!listener->IsInterested()) {
2542 return matcher_.Matches(sorted_container);
2545 *listener <<
"which is ";
2546 UniversalPrint(sorted_container, listener->stream());
2547 *listener <<
" when sorted";
2549 StringMatchResultListener inner_listener;
2551 matcher_.MatchAndExplain(sorted_container, &inner_listener);
2552 PrintIfNotEmpty(inner_listener.str(), listener->stream());
2557 const Comparator comparator_;
2558 const Matcher<const ::std::vector<LhsValue>&> matcher_;
2560 Impl(
const Impl&) =
delete;
2561 Impl& operator=(
const Impl&) =
delete;
2565 const Comparator comparator_;
2566 const ContainerMatcher matcher_;
2573template <
typename TupleMatcher,
typename RhsContainer>
2574class PointwiseMatcher {
2576 !IsHashTable<GTEST_REMOVE_REFERENCE_AND_CONST_(RhsContainer)>::value,
2577 "use UnorderedPointwise with hash tables");
2580 typedef internal::StlContainerView<RhsContainer> RhsView;
2581 typedef typename RhsView::type RhsStlContainer;
2582 typedef typename RhsStlContainer::value_type RhsValue;
2584 static_assert(!std::is_const<RhsContainer>::value,
2585 "RhsContainer type must not be const");
2586 static_assert(!std::is_reference<RhsContainer>::value,
2587 "RhsContainer type must not be a reference");
2591 PointwiseMatcher(
const TupleMatcher& tuple_matcher,
const RhsContainer& rhs)
2592 : tuple_matcher_(tuple_matcher), rhs_(RhsView::Copy(rhs)) {}
2594 template <
typename LhsContainer>
2595 operator Matcher<LhsContainer>()
const {
2597 !IsHashTable<GTEST_REMOVE_REFERENCE_AND_CONST_(LhsContainer)>::value,
2598 "use UnorderedPointwise with hash tables");
2600 return Matcher<LhsContainer>(
2601 new Impl<const LhsContainer&>(tuple_matcher_, rhs_));
2604 template <
typename LhsContainer>
2605 class Impl :
public MatcherInterface<LhsContainer> {
2607 typedef internal::StlContainerView<GTEST_REMOVE_REFERENCE_AND_CONST_(
2610 typedef typename LhsView::type LhsStlContainer;
2611 typedef typename LhsView::const_reference LhsStlContainerReference;
2612 typedef typename LhsStlContainer::value_type LhsValue;
2617 typedef ::std::tuple<const LhsValue&, const RhsValue&> InnerMatcherArg;
2619 Impl(
const TupleMatcher& tuple_matcher,
const RhsStlContainer& rhs)
2621 : mono_tuple_matcher_(SafeMatcherCast<InnerMatcherArg>(tuple_matcher)),
2624 void DescribeTo(::std::ostream* os)
const override {
2625 *os <<
"contains " << rhs_.size()
2626 <<
" values, where each value and its corresponding value in ";
2627 UniversalPrinter<RhsStlContainer>::Print(rhs_, os);
2629 mono_tuple_matcher_.DescribeTo(os);
2631 void DescribeNegationTo(::std::ostream* os)
const override {
2632 *os <<
"doesn't contain exactly " << rhs_.size()
2633 <<
" values, or contains a value x at some index i"
2634 <<
" where x and the i-th value of ";
2635 UniversalPrint(rhs_, os);
2637 mono_tuple_matcher_.DescribeNegationTo(os);
2640 bool MatchAndExplain(LhsContainer lhs,
2641 MatchResultListener* listener)
const override {
2642 LhsStlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2643 const size_t actual_size = lhs_stl_container.size();
2644 if (actual_size != rhs_.size()) {
2645 *listener <<
"which contains " << actual_size <<
" values";
2649 auto left = lhs_stl_container.begin();
2650 auto right = rhs_.begin();
2651 for (
size_t i = 0; i != actual_size; ++i, ++left, ++right) {
2652 if (listener->IsInterested()) {
2653 StringMatchResultListener inner_listener;
2657 if (!mono_tuple_matcher_.MatchAndExplain(
2658 InnerMatcherArg(ImplicitCast_<const LhsValue&>(*left),
2659 ImplicitCast_<const RhsValue&>(*right)),
2661 *listener <<
"where the value pair (";
2662 UniversalPrint(*left, listener->stream());
2664 UniversalPrint(*right, listener->stream());
2665 *listener <<
") at index #" << i <<
" don't match";
2666 PrintIfNotEmpty(inner_listener.str(), listener->stream());
2670 if (!mono_tuple_matcher_.Matches(
2671 InnerMatcherArg(ImplicitCast_<const LhsValue&>(*left),
2672 ImplicitCast_<const RhsValue&>(*right))))
2681 const Matcher<InnerMatcherArg> mono_tuple_matcher_;
2682 const RhsStlContainer rhs_;
2686 const TupleMatcher tuple_matcher_;
2687 const RhsStlContainer rhs_;
2691template <
typename Container>
2692class QuantifierMatcherImpl :
public MatcherInterface<Container> {
2694 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
2695 typedef StlContainerView<RawContainer> View;
2696 typedef typename View::type StlContainer;
2697 typedef typename View::const_reference StlContainerReference;
2698 typedef typename StlContainer::value_type Element;
2700 template <
typename InnerMatcher>
2701 explicit QuantifierMatcherImpl(InnerMatcher inner_matcher)
2703 testing::SafeMatcherCast<const Element&>(inner_matcher)) {}
2708 bool MatchAndExplainImpl(
bool all_elements_should_match, Container container,
2709 MatchResultListener* listener)
const {
2710 StlContainerReference stl_container = View::ConstReference(container);
2712 for (
auto it = stl_container.begin(); it != stl_container.end();
2714 StringMatchResultListener inner_listener;
2715 const bool matches = inner_matcher_.MatchAndExplain(*it, &inner_listener);
2717 if (matches != all_elements_should_match) {
2718 *listener <<
"whose element #" << i
2719 << (matches ?
" matches" :
" doesn't match");
2720 PrintIfNotEmpty(inner_listener.str(), listener->stream());
2721 return !all_elements_should_match;
2724 return all_elements_should_match;
2727 bool MatchAndExplainImpl(
const Matcher<size_t>& count_matcher,
2728 Container container,
2729 MatchResultListener* listener)
const {
2730 StlContainerReference stl_container = View::ConstReference(container);
2732 std::vector<size_t> match_elements;
2733 for (
auto it = stl_container.begin(); it != stl_container.end();
2735 StringMatchResultListener inner_listener;
2736 const bool matches = inner_matcher_.MatchAndExplain(*it, &inner_listener);
2738 match_elements.push_back(i);
2741 if (listener->IsInterested()) {
2742 if (match_elements.empty()) {
2743 *listener <<
"has no element that matches";
2744 }
else if (match_elements.size() == 1) {
2745 *listener <<
"whose element #" << match_elements[0] <<
" matches";
2747 *listener <<
"whose elements (";
2748 std::string sep =
"";
2749 for (
size_t e : match_elements) {
2750 *listener << sep << e;
2753 *listener <<
") match";
2756 StringMatchResultListener count_listener;
2757 if (count_matcher.MatchAndExplain(match_elements.size(), &count_listener)) {
2758 *listener <<
" and whose match quantity of " << match_elements.size()
2760 PrintIfNotEmpty(count_listener.str(), listener->stream());
2763 if (match_elements.empty()) {
2764 *listener <<
" and";
2766 *listener <<
" but";
2768 *listener <<
" whose match quantity of " << match_elements.size()
2769 <<
" does not match";
2770 PrintIfNotEmpty(count_listener.str(), listener->stream());
2776 const Matcher<const Element&> inner_matcher_;
2781template <
typename Container>
2782class ContainsMatcherImpl :
public QuantifierMatcherImpl<Container> {
2784 template <
typename InnerMatcher>
2785 explicit ContainsMatcherImpl(InnerMatcher inner_matcher)
2786 : QuantifierMatcherImpl<Container>(inner_matcher) {}
2789 void DescribeTo(::std::ostream* os)
const override {
2790 *os <<
"contains at least one element that ";
2791 this->inner_matcher_.DescribeTo(os);
2794 void DescribeNegationTo(::std::ostream* os)
const override {
2795 *os <<
"doesn't contain any element that ";
2796 this->inner_matcher_.DescribeTo(os);
2799 bool MatchAndExplain(Container container,
2800 MatchResultListener* listener)
const override {
2801 return this->MatchAndExplainImpl(
false, container, listener);
2807template <
typename Container>
2808class EachMatcherImpl :
public QuantifierMatcherImpl<Container> {
2810 template <
typename InnerMatcher>
2811 explicit EachMatcherImpl(InnerMatcher inner_matcher)
2812 : QuantifierMatcherImpl<Container>(inner_matcher) {}
2815 void DescribeTo(::std::ostream* os)
const override {
2816 *os <<
"only contains elements that ";
2817 this->inner_matcher_.DescribeTo(os);
2820 void DescribeNegationTo(::std::ostream* os)
const override {
2821 *os <<
"contains some element that ";
2822 this->inner_matcher_.DescribeNegationTo(os);
2825 bool MatchAndExplain(Container container,
2826 MatchResultListener* listener)
const override {
2827 return this->MatchAndExplainImpl(
true, container, listener);
2833template <
typename Container>
2834class ContainsTimesMatcherImpl :
public QuantifierMatcherImpl<Container> {
2836 template <
typename InnerMatcher>
2837 explicit ContainsTimesMatcherImpl(InnerMatcher inner_matcher,
2838 Matcher<size_t> count_matcher)
2839 : QuantifierMatcherImpl<Container>(inner_matcher),
2840 count_matcher_(std::move(count_matcher)) {}
2842 void DescribeTo(::std::ostream* os)
const override {
2843 *os <<
"quantity of elements that match ";
2844 this->inner_matcher_.DescribeTo(os);
2846 count_matcher_.DescribeTo(os);
2849 void DescribeNegationTo(::std::ostream* os)
const override {
2850 *os <<
"quantity of elements that match ";
2851 this->inner_matcher_.DescribeTo(os);
2853 count_matcher_.DescribeNegationTo(os);
2856 bool MatchAndExplain(Container container,
2857 MatchResultListener* listener)
const override {
2858 return this->MatchAndExplainImpl(count_matcher_, container, listener);
2862 const Matcher<size_t> count_matcher_;
2866template <
typename M>
2867class ContainsTimesMatcher {
2869 explicit ContainsTimesMatcher(M m, Matcher<size_t> count_matcher)
2870 : inner_matcher_(m), count_matcher_(std::move(count_matcher)) {}
2872 template <
typename Container>
2873 operator Matcher<Container>()
const {
2874 return Matcher<Container>(
new ContainsTimesMatcherImpl<const Container&>(
2875 inner_matcher_, count_matcher_));
2879 const M inner_matcher_;
2880 const Matcher<size_t> count_matcher_;
2884template <
typename M>
2885class ContainsMatcher {
2887 explicit ContainsMatcher(M m) : inner_matcher_(m) {}
2889 template <
typename Container>
2890 operator Matcher<Container>()
const {
2891 return Matcher<Container>(
2892 new ContainsMatcherImpl<const Container&>(inner_matcher_));
2895 ContainsTimesMatcher<M> Times(Matcher<size_t> count_matcher)
const {
2896 return ContainsTimesMatcher<M>(inner_matcher_, std::move(count_matcher));
2900 const M inner_matcher_;
2904template <
typename M>
2907 explicit EachMatcher(M m) : inner_matcher_(m) {}
2909 template <
typename Container>
2910 operator Matcher<Container>()
const {
2911 return Matcher<Container>(
2912 new EachMatcherImpl<const Container&>(inner_matcher_));
2916 const M inner_matcher_;
2920struct Rank0 : Rank1 {};
2922namespace pair_getters {
2924template <
typename T>
2925auto First(T& x, Rank1) ->
decltype(get<0>(x)) {
2928template <
typename T>
2929auto First(T& x, Rank0) ->
decltype((x.first)) {
2933template <
typename T>
2934auto Second(T& x, Rank1) ->
decltype(get<1>(x)) {
2937template <
typename T>
2938auto Second(T& x, Rank0) ->
decltype((x.second)) {
2947template <
typename PairType>
2948class KeyMatcherImpl :
public MatcherInterface<PairType> {
2950 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(PairType) RawPairType;
2951 typedef typename RawPairType::first_type KeyType;
2953 template <
typename InnerMatcher>
2954 explicit KeyMatcherImpl(InnerMatcher inner_matcher)
2956 testing::SafeMatcherCast<const KeyType&>(inner_matcher)) {}
2960 bool MatchAndExplain(PairType key_value,
2961 MatchResultListener* listener)
const override {
2962 StringMatchResultListener inner_listener;
2963 const bool match = inner_matcher_.MatchAndExplain(
2964 pair_getters::First(key_value, Rank0()), &inner_listener);
2965 const std::string explanation = inner_listener.str();
2966 if (explanation !=
"") {
2967 *listener <<
"whose first field is a value " << explanation;
2973 void DescribeTo(::std::ostream* os)
const override {
2974 *os <<
"has a key that ";
2975 inner_matcher_.DescribeTo(os);
2979 void DescribeNegationTo(::std::ostream* os)
const override {
2980 *os <<
"doesn't have a key that ";
2981 inner_matcher_.DescribeTo(os);
2985 const Matcher<const KeyType&> inner_matcher_;
2989template <
typename M>
2992 explicit KeyMatcher(M m) : matcher_for_key_(m) {}
2994 template <
typename PairType>
2995 operator Matcher<PairType>()
const {
2996 return Matcher<PairType>(
2997 new KeyMatcherImpl<const PairType&>(matcher_for_key_));
3001 const M matcher_for_key_;
3005template <
typename InnerMatcher>
3006class AddressMatcher {
3008 explicit AddressMatcher(InnerMatcher m) : matcher_(m) {}
3010 template <
typename Type>
3011 operator Matcher<Type>()
const {
3012 return Matcher<Type>(
new Impl<const Type&>(matcher_));
3017 template <
typename Type>
3018 class Impl :
public MatcherInterface<Type> {
3020 using Address =
const GTEST_REMOVE_REFERENCE_AND_CONST_(Type) *;
3021 explicit Impl(
const InnerMatcher& matcher)
3022 : matcher_(MatcherCast<Address>(matcher)) {}
3024 void DescribeTo(::std::ostream* os)
const override {
3025 *os <<
"has address that ";
3026 matcher_.DescribeTo(os);
3029 void DescribeNegationTo(::std::ostream* os)
const override {
3030 *os <<
"does not have address that ";
3031 matcher_.DescribeTo(os);
3034 bool MatchAndExplain(Type
object,
3035 MatchResultListener* listener)
const override {
3036 *listener <<
"which has address ";
3037 Address address = std::addressof(
object);
3038 return MatchPrintAndExplain(address, matcher_, listener);
3042 const Matcher<Address> matcher_;
3044 const InnerMatcher matcher_;
3049template <
typename PairType>
3050class PairMatcherImpl :
public MatcherInterface<PairType> {
3052 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(PairType) RawPairType;
3053 typedef typename RawPairType::first_type FirstType;
3054 typedef typename RawPairType::second_type SecondType;
3056 template <
typename FirstMatcher,
typename SecondMatcher>
3057 PairMatcherImpl(FirstMatcher first_matcher, SecondMatcher second_matcher)
3059 testing::SafeMatcherCast<const FirstType&>(first_matcher)),
3061 testing::SafeMatcherCast<const SecondType&>(second_matcher)) {}
3064 void DescribeTo(::std::ostream* os)
const override {
3065 *os <<
"has a first field that ";
3066 first_matcher_.DescribeTo(os);
3067 *os <<
", and has a second field that ";
3068 second_matcher_.DescribeTo(os);
3072 void DescribeNegationTo(::std::ostream* os)
const override {
3073 *os <<
"has a first field that ";
3074 first_matcher_.DescribeNegationTo(os);
3075 *os <<
", or has a second field that ";
3076 second_matcher_.DescribeNegationTo(os);
3081 bool MatchAndExplain(PairType a_pair,
3082 MatchResultListener* listener)
const override {
3083 if (!listener->IsInterested()) {
3086 return first_matcher_.Matches(pair_getters::First(a_pair, Rank0())) &&
3087 second_matcher_.Matches(pair_getters::Second(a_pair, Rank0()));
3089 StringMatchResultListener first_inner_listener;
3090 if (!first_matcher_.MatchAndExplain(pair_getters::First(a_pair, Rank0()),
3091 &first_inner_listener)) {
3092 *listener <<
"whose first field does not match";
3093 PrintIfNotEmpty(first_inner_listener.str(), listener->stream());
3096 StringMatchResultListener second_inner_listener;
3097 if (!second_matcher_.MatchAndExplain(pair_getters::Second(a_pair, Rank0()),
3098 &second_inner_listener)) {
3099 *listener <<
"whose second field does not match";
3100 PrintIfNotEmpty(second_inner_listener.str(), listener->stream());
3103 ExplainSuccess(first_inner_listener.str(), second_inner_listener.str(),
3109 void ExplainSuccess(
const std::string& first_explanation,
3110 const std::string& second_explanation,
3111 MatchResultListener* listener)
const {
3112 *listener <<
"whose both fields match";
3113 if (first_explanation !=
"") {
3114 *listener <<
", where the first field is a value " << first_explanation;
3116 if (second_explanation !=
"") {
3118 if (first_explanation !=
"") {
3119 *listener <<
"and ";
3121 *listener <<
"where ";
3123 *listener <<
"the second field is a value " << second_explanation;
3127 const Matcher<const FirstType&> first_matcher_;
3128 const Matcher<const SecondType&> second_matcher_;
3132template <
typename FirstMatcher,
typename SecondMatcher>
3135 PairMatcher(FirstMatcher first_matcher, SecondMatcher second_matcher)
3136 : first_matcher_(first_matcher), second_matcher_(second_matcher) {}
3138 template <
typename PairType>
3139 operator Matcher<PairType>()
const {
3140 return Matcher<PairType>(
3141 new PairMatcherImpl<const PairType&>(first_matcher_, second_matcher_));
3145 const FirstMatcher first_matcher_;
3146 const SecondMatcher second_matcher_;
3149template <
typename T,
size_t... I>
3150auto UnpackStructImpl(
const T& t, IndexSequence<I...>,
int)
3151 ->
decltype(std::tie(get<I>(t)...)) {
3152 static_assert(std::tuple_size<T>::value ==
sizeof...(I),
3153 "Number of arguments doesn't match the number of fields.");
3154 return std::tie(get<I>(t)...);
3157#if defined(__cpp_structured_bindings) && __cpp_structured_bindings >= 201606
3158template <
typename T>
3159auto UnpackStructImpl(
const T& t, MakeIndexSequence<1>,
char) {
3160 const auto& [a] = t;
3163template <
typename T>
3164auto UnpackStructImpl(
const T& t, MakeIndexSequence<2>,
char) {
3165 const auto& [a, b] = t;
3166 return std::tie(a, b);
3168template <
typename T>
3169auto UnpackStructImpl(
const T& t, MakeIndexSequence<3>,
char) {
3170 const auto& [a, b, c] = t;
3171 return std::tie(a, b, c);
3173template <
typename T>
3174auto UnpackStructImpl(
const T& t, MakeIndexSequence<4>,
char) {
3175 const auto& [a, b, c, d] = t;
3176 return std::tie(a, b, c, d);
3178template <
typename T>
3179auto UnpackStructImpl(
const T& t, MakeIndexSequence<5>,
char) {
3180 const auto& [a, b, c, d, e] = t;
3181 return std::tie(a, b, c, d, e);
3183template <
typename T>
3184auto UnpackStructImpl(
const T& t, MakeIndexSequence<6>,
char) {
3185 const auto& [a, b, c, d, e, f] = t;
3186 return std::tie(a, b, c, d, e, f);
3188template <
typename T>
3189auto UnpackStructImpl(
const T& t, MakeIndexSequence<7>,
char) {
3190 const auto& [a, b, c, d, e, f, g] = t;
3191 return std::tie(a, b, c, d, e, f, g);
3193template <
typename T>
3194auto UnpackStructImpl(
const T& t, MakeIndexSequence<8>,
char) {
3195 const auto& [a, b, c, d, e, f, g, h] = t;
3196 return std::tie(a, b, c, d, e, f, g, h);
3198template <
typename T>
3199auto UnpackStructImpl(
const T& t, MakeIndexSequence<9>,
char) {
3200 const auto& [a, b, c, d, e, f, g, h, i] = t;
3201 return std::tie(a, b, c, d, e, f, g, h, i);
3203template <
typename T>
3204auto UnpackStructImpl(
const T& t, MakeIndexSequence<10>,
char) {
3205 const auto& [a, b, c, d, e, f, g, h, i, j] = t;
3206 return std::tie(a, b, c, d, e, f, g, h, i, j);
3208template <
typename T>
3209auto UnpackStructImpl(
const T& t, MakeIndexSequence<11>,
char) {
3210 const auto& [a, b, c, d, e, f, g, h, i, j, k] = t;
3211 return std::tie(a, b, c, d, e, f, g, h, i, j, k);
3213template <
typename T>
3214auto UnpackStructImpl(
const T& t, MakeIndexSequence<12>,
char) {
3215 const auto& [a, b, c, d, e, f, g, h, i, j, k, l] = t;
3216 return std::tie(a, b, c, d, e, f, g, h, i, j, k, l);
3218template <
typename T>
3219auto UnpackStructImpl(
const T& t, MakeIndexSequence<13>,
char) {
3220 const auto& [a, b, c, d, e, f, g, h, i, j, k, l, m] = t;
3221 return std::tie(a, b, c, d, e, f, g, h, i, j, k, l, m);
3223template <
typename T>
3224auto UnpackStructImpl(
const T& t, MakeIndexSequence<14>,
char) {
3225 const auto& [a, b, c, d, e, f, g, h, i, j, k, l, m, n] = t;
3226 return std::tie(a, b, c, d, e, f, g, h, i, j, k, l, m, n);
3228template <
typename T>
3229auto UnpackStructImpl(
const T& t, MakeIndexSequence<15>,
char) {
3230 const auto& [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o] = t;
3231 return std::tie(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o);
3233template <
typename T>
3234auto UnpackStructImpl(
const T& t, MakeIndexSequence<16>,
char) {
3235 const auto& [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p] = t;
3236 return std::tie(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p);
3240template <
size_t I,
typename T>
3241auto UnpackStruct(
const T& t)
3242 ->
decltype((UnpackStructImpl)(t, MakeIndexSequence<I>{}, 0)) {
3243 return (UnpackStructImpl)(t, MakeIndexSequence<I>{}, 0);
3249template <
typename T,
size_t N>
3250void VariadicExpand(
const T (&)[N]) {}
3252template <
typename Struct,
typename StructSize>
3253class FieldsAreMatcherImpl;
3255template <
typename Struct,
size_t... I>
3256class FieldsAreMatcherImpl<Struct, IndexSequence<I...>>
3257 :
public MatcherInterface<Struct> {
3258 using UnpackedType =
3259 decltype(UnpackStruct<
sizeof...(I)>(std::declval<const Struct&>()));
3260 using MatchersType = std::tuple<
3261 Matcher<const typename std::tuple_element<I, UnpackedType>::type&>...>;
3264 template <
typename Inner>
3265 explicit FieldsAreMatcherImpl(
const Inner& matchers)
3266 : matchers_(testing::SafeMatcherCast<
3267 const typename std::tuple_element<I, UnpackedType>::type&>(
3268 std::get<I>(matchers))...) {}
3270 void DescribeTo(::std::ostream* os)
const override {
3271 const char* separator =
"";
3273 {(*os << separator <<
"has field #" << I <<
" that ",
3274 std::get<I>(matchers_).DescribeTo(os), separator =
", and ")...});
3277 void DescribeNegationTo(::std::ostream* os)
const override {
3278 const char* separator =
"";
3279 VariadicExpand({(*os << separator <<
"has field #" << I <<
" that ",
3280 std::get<I>(matchers_).DescribeNegationTo(os),
3281 separator =
", or ")...});
3284 bool MatchAndExplain(Struct t, MatchResultListener* listener)
const override {
3285 return MatchInternal((UnpackStruct<
sizeof...(I)>)(t), listener);
3289 bool MatchInternal(UnpackedType tuple, MatchResultListener* listener)
const {
3290 if (!listener->IsInterested()) {
3294 VariadicExpand({good = good && std::get<I>(matchers_).Matches(
3295 std::get<I>(tuple))...});
3299 size_t failed_pos = ~size_t{};
3301 std::vector<StringMatchResultListener> inner_listener(
sizeof...(I));
3304 {failed_pos == ~size_t{} && !std::get<I>(matchers_).MatchAndExplain(
3305 std::get<I>(tuple), &inner_listener[I])
3308 if (failed_pos != ~
size_t{}) {
3309 *listener <<
"whose field #" << failed_pos <<
" does not match";
3310 PrintIfNotEmpty(inner_listener[failed_pos].str(), listener->stream());
3314 *listener <<
"whose all elements match";
3315 const char* separator =
", where";
3316 for (
size_t index = 0; index <
sizeof...(I); ++index) {
3317 const std::string str = inner_listener[index].str();
3319 *listener << separator <<
" field #" << index <<
" is a value " << str;
3320 separator =
", and";
3327 MatchersType matchers_;
3330template <
typename... Inner>
3331class FieldsAreMatcher {
3333 explicit FieldsAreMatcher(Inner... inner) : matchers_(std::move(inner)...) {}
3335 template <
typename Struct>
3336 operator Matcher<Struct>()
const {
3337 return Matcher<Struct>(
3338 new FieldsAreMatcherImpl<
const Struct&, IndexSequenceFor<Inner...>>(
3343 std::tuple<Inner...> matchers_;
3347template <
typename Container>
3348class ElementsAreMatcherImpl :
public MatcherInterface<Container> {
3350 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
3351 typedef internal::StlContainerView<RawContainer> View;
3352 typedef typename View::type StlContainer;
3353 typedef typename View::const_reference StlContainerReference;
3354 typedef typename StlContainer::value_type Element;
3358 template <
typename InputIter>
3359 ElementsAreMatcherImpl(InputIter first, InputIter last) {
3360 while (first != last) {
3361 matchers_.push_back(MatcherCast<const Element&>(*first++));
3366 void DescribeTo(::std::ostream* os)
const override {
3369 }
else if (count() == 1) {
3370 *os <<
"has 1 element that ";
3371 matchers_[0].DescribeTo(os);
3373 *os <<
"has " << Elements(count()) <<
" where\n";
3374 for (
size_t i = 0; i != count(); ++i) {
3375 *os <<
"element #" << i <<
" ";
3376 matchers_[i].DescribeTo(os);
3377 if (i + 1 < count()) {
3385 void DescribeNegationTo(::std::ostream* os)
const override {
3387 *os <<
"isn't empty";
3391 *os <<
"doesn't have " << Elements(count()) <<
", or\n";
3392 for (
size_t i = 0; i != count(); ++i) {
3393 *os <<
"element #" << i <<
" ";
3394 matchers_[i].DescribeNegationTo(os);
3395 if (i + 1 < count()) {
3401 bool MatchAndExplain(Container container,
3402 MatchResultListener* listener)
const override {
3406 const bool listener_interested = listener->IsInterested();
3409 ::std::vector<std::string> explanations(count());
3410 StlContainerReference stl_container = View::ConstReference(container);
3411 auto it = stl_container.begin();
3412 size_t exam_pos = 0;
3413 bool mismatch_found =
false;
3418 for (; it != stl_container.end() && exam_pos != count(); ++it, ++exam_pos) {
3420 if (listener_interested) {
3421 StringMatchResultListener s;
3422 match = matchers_[exam_pos].MatchAndExplain(*it, &s);
3423 explanations[exam_pos] = s.str();
3425 match = matchers_[exam_pos].Matches(*it);
3429 mismatch_found =
true;
3438 size_t actual_count = exam_pos;
3439 for (; it != stl_container.end(); ++it) {
3443 if (actual_count != count()) {
3448 if (listener_interested && (actual_count != 0)) {
3449 *listener <<
"which has " << Elements(actual_count);
3454 if (mismatch_found) {
3456 if (listener_interested) {
3457 *listener <<
"whose element #" << exam_pos <<
" doesn't match";
3458 PrintIfNotEmpty(explanations[exam_pos], listener->stream());
3465 if (listener_interested) {
3466 bool reason_printed =
false;
3467 for (
size_t i = 0; i != count(); ++i) {
3468 const std::string& s = explanations[i];
3470 if (reason_printed) {
3471 *listener <<
",\nand ";
3473 *listener <<
"whose element #" << i <<
" matches, " << s;
3474 reason_printed =
true;
3482 static Message Elements(
size_t count) {
3483 return Message() << count << (count == 1 ?
" element" :
" elements");
3486 size_t count()
const {
return matchers_.size(); }
3488 ::std::vector<Matcher<const Element&>> matchers_;
3495class GTEST_API_ MatchMatrix {
3497 MatchMatrix(
size_t num_elements,
size_t num_matchers)
3498 : num_elements_(num_elements),
3499 num_matchers_(num_matchers),
3500 matched_(num_elements_ * num_matchers_, 0) {}
3502 size_t LhsSize()
const {
return num_elements_; }
3503 size_t RhsSize()
const {
return num_matchers_; }
3504 bool HasEdge(
size_t ilhs,
size_t irhs)
const {
3505 return matched_[SpaceIndex(ilhs, irhs)] == 1;
3507 void SetEdge(
size_t ilhs,
size_t irhs,
bool b) {
3508 matched_[SpaceIndex(ilhs, irhs)] = b ? 1 : 0;
3518 std::string DebugString()
const;
3521 size_t SpaceIndex(
size_t ilhs,
size_t irhs)
const {
3522 return ilhs * num_matchers_ + irhs;
3525 size_t num_elements_;
3526 size_t num_matchers_;
3531 ::std::vector<char> matched_;
3534typedef ::std::pair<size_t, size_t> ElementMatcherPair;
3535typedef ::std::vector<ElementMatcherPair> ElementMatcherPairs;
3541struct UnorderedMatcherRequire {
3545 ExactMatch = Superset | Subset,
3552class GTEST_API_ UnorderedElementsAreMatcherImplBase {
3554 explicit UnorderedElementsAreMatcherImplBase(
3555 UnorderedMatcherRequire::Flags matcher_flags)
3556 : match_flags_(matcher_flags) {}
3561 typedef ::std::vector<const MatcherDescriberInterface*> MatcherDescriberVec;
3564 void DescribeToImpl(::std::ostream* os)
const;
3567 void DescribeNegationToImpl(::std::ostream* os)
const;
3569 bool VerifyMatchMatrix(const ::std::vector<std::string>& element_printouts,
3570 const MatchMatrix& matrix,
3571 MatchResultListener* listener)
const;
3573 bool FindPairing(
const MatchMatrix& matrix,
3574 MatchResultListener* listener)
const;
3576 MatcherDescriberVec& matcher_describers() {
return matcher_describers_; }
3578 static Message Elements(
size_t n) {
3579 return Message() << n <<
" element" << (n == 1 ?
"" :
"s");
3582 UnorderedMatcherRequire::Flags match_flags()
const {
return match_flags_; }
3585 UnorderedMatcherRequire::Flags match_flags_;
3586 MatcherDescriberVec matcher_describers_;
3591template <
typename Container>
3592class UnorderedElementsAreMatcherImpl
3593 :
public MatcherInterface<Container>,
3594 public UnorderedElementsAreMatcherImplBase {
3596 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
3597 typedef internal::StlContainerView<RawContainer> View;
3598 typedef typename View::type StlContainer;
3599 typedef typename View::const_reference StlContainerReference;
3600 typedef typename StlContainer::value_type Element;
3602 template <
typename InputIter>
3603 UnorderedElementsAreMatcherImpl(UnorderedMatcherRequire::Flags matcher_flags,
3604 InputIter first, InputIter last)
3605 : UnorderedElementsAreMatcherImplBase(matcher_flags) {
3606 for (; first != last; ++first) {
3607 matchers_.push_back(MatcherCast<const Element&>(*first));
3609 for (
const auto& m : matchers_) {
3610 matcher_describers().push_back(m.GetDescriber());
3615 void DescribeTo(::std::ostream* os)
const override {
3616 return UnorderedElementsAreMatcherImplBase::DescribeToImpl(os);
3620 void DescribeNegationTo(::std::ostream* os)
const override {
3621 return UnorderedElementsAreMatcherImplBase::DescribeNegationToImpl(os);
3624 bool MatchAndExplain(Container container,
3625 MatchResultListener* listener)
const override {
3626 StlContainerReference stl_container = View::ConstReference(container);
3627 ::std::vector<std::string> element_printouts;
3628 MatchMatrix matrix =
3629 AnalyzeElements(stl_container.begin(), stl_container.end(),
3630 &element_printouts, listener);
3632 if (matrix.LhsSize() == 0 && matrix.RhsSize() == 0) {
3636 if (match_flags() == UnorderedMatcherRequire::ExactMatch) {
3637 if (matrix.LhsSize() != matrix.RhsSize()) {
3642 if (matrix.LhsSize() != 0 && listener->IsInterested()) {
3643 *listener <<
"which has " << Elements(matrix.LhsSize());
3649 return VerifyMatchMatrix(element_printouts, matrix, listener) &&
3650 FindPairing(matrix, listener);
3654 template <
typename ElementIter>
3655 MatchMatrix AnalyzeElements(ElementIter elem_first, ElementIter elem_last,
3656 ::std::vector<std::string>* element_printouts,
3657 MatchResultListener* listener)
const {
3658 element_printouts->clear();
3659 ::std::vector<char> did_match;
3660 size_t num_elements = 0;
3661 DummyMatchResultListener dummy;
3662 for (; elem_first != elem_last; ++num_elements, ++elem_first) {
3663 if (listener->IsInterested()) {
3664 element_printouts->push_back(PrintToString(*elem_first));
3666 for (
size_t irhs = 0; irhs != matchers_.size(); ++irhs) {
3667 did_match.push_back(
3668 matchers_[irhs].MatchAndExplain(*elem_first, &dummy));
3672 MatchMatrix matrix(num_elements, matchers_.size());
3673 ::std::vector<char>::const_iterator did_match_iter = did_match.begin();
3674 for (
size_t ilhs = 0; ilhs != num_elements; ++ilhs) {
3675 for (
size_t irhs = 0; irhs != matchers_.size(); ++irhs) {
3676 matrix.SetEdge(ilhs, irhs, *did_match_iter++ != 0);
3682 ::std::vector<Matcher<const Element&>> matchers_;
3687template <
typename Target>
3688struct CastAndAppendTransform {
3689 template <
typename Arg>
3690 Matcher<Target> operator()(
const Arg& a)
const {
3691 return MatcherCast<Target>(a);
3696template <
typename MatcherTuple>
3697class UnorderedElementsAreMatcher {
3699 explicit UnorderedElementsAreMatcher(
const MatcherTuple& args)
3700 : matchers_(args) {}
3702 template <
typename Container>
3703 operator Matcher<Container>()
const {
3704 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
3705 typedef typename internal::StlContainerView<RawContainer>::type View;
3706 typedef typename View::value_type Element;
3707 typedef ::std::vector<Matcher<const Element&>> MatcherVec;
3708 MatcherVec matchers;
3709 matchers.reserve(::std::tuple_size<MatcherTuple>::value);
3710 TransformTupleValues(CastAndAppendTransform<const Element&>(), matchers_,
3711 ::std::back_inserter(matchers));
3712 return Matcher<Container>(
3713 new UnorderedElementsAreMatcherImpl<const Container&>(
3714 UnorderedMatcherRequire::ExactMatch, matchers.begin(),
3719 const MatcherTuple matchers_;
3723template <
typename MatcherTuple>
3724class ElementsAreMatcher {
3726 explicit ElementsAreMatcher(
const MatcherTuple& args) : matchers_(args) {}
3728 template <
typename Container>
3729 operator Matcher<Container>()
const {
3731 !IsHashTable<GTEST_REMOVE_REFERENCE_AND_CONST_(Container)>::value ||
3732 ::std::tuple_size<MatcherTuple>::value < 2,
3733 "use UnorderedElementsAre with hash tables");
3735 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
3736 typedef typename internal::StlContainerView<RawContainer>::type View;
3737 typedef typename View::value_type Element;
3738 typedef ::std::vector<Matcher<const Element&>> MatcherVec;
3739 MatcherVec matchers;
3740 matchers.reserve(::std::tuple_size<MatcherTuple>::value);
3741 TransformTupleValues(CastAndAppendTransform<const Element&>(), matchers_,
3742 ::std::back_inserter(matchers));
3743 return Matcher<Container>(
new ElementsAreMatcherImpl<const Container&>(
3744 matchers.begin(), matchers.end()));
3748 const MatcherTuple matchers_;
3752template <
typename T>
3753class UnorderedElementsAreArrayMatcher {
3755 template <
typename Iter>
3756 UnorderedElementsAreArrayMatcher(UnorderedMatcherRequire::Flags match_flags,
3757 Iter first, Iter last)
3758 : match_flags_(match_flags), matchers_(first, last) {}
3760 template <
typename Container>
3761 operator Matcher<Container>()
const {
3762 return Matcher<Container>(
3763 new UnorderedElementsAreMatcherImpl<const Container&>(
3764 match_flags_, matchers_.begin(), matchers_.end()));
3768 UnorderedMatcherRequire::Flags match_flags_;
3769 ::std::vector<T> matchers_;
3773template <
typename T>
3774class ElementsAreArrayMatcher {
3776 template <
typename Iter>
3777 ElementsAreArrayMatcher(Iter first, Iter last) : matchers_(first, last) {}
3779 template <
typename Container>
3780 operator Matcher<Container>()
const {
3782 !IsHashTable<GTEST_REMOVE_REFERENCE_AND_CONST_(Container)>::value,
3783 "use UnorderedElementsAreArray with hash tables");
3785 return Matcher<Container>(
new ElementsAreMatcherImpl<const Container&>(
3786 matchers_.begin(), matchers_.end()));
3790 const ::std::vector<T> matchers_;
3802template <
typename Tuple2Matcher,
typename Second>
3803class BoundSecondMatcher {
3805 BoundSecondMatcher(
const Tuple2Matcher& tm,
const Second& second)
3806 : tuple2_matcher_(tm), second_value_(second) {}
3808 BoundSecondMatcher(
const BoundSecondMatcher& other) =
default;
3810 template <
typename T>
3811 operator Matcher<T>()
const {
3812 return MakeMatcher(
new Impl<T>(tuple2_matcher_, second_value_));
3823 void operator=(
const BoundSecondMatcher& ) {
3824 GTEST_LOG_(FATAL) <<
"BoundSecondMatcher should never be assigned.";
3828 template <
typename T>
3829 class Impl :
public MatcherInterface<T> {
3831 typedef ::std::tuple<T, Second> ArgTuple;
3833 Impl(
const Tuple2Matcher& tm,
const Second& second)
3834 : mono_tuple2_matcher_(SafeMatcherCast<const ArgTuple&>(tm)),
3835 second_value_(second) {}
3837 void DescribeTo(::std::ostream* os)
const override {
3839 UniversalPrint(second_value_, os);
3841 mono_tuple2_matcher_.DescribeTo(os);
3844 bool MatchAndExplain(T x, MatchResultListener* listener)
const override {
3845 return mono_tuple2_matcher_.MatchAndExplain(ArgTuple(x, second_value_),
3850 const Matcher<const ArgTuple&> mono_tuple2_matcher_;
3851 const Second second_value_;
3854 const Tuple2Matcher tuple2_matcher_;
3855 const Second second_value_;
3862template <
typename Tuple2Matcher,
typename Second>
3863BoundSecondMatcher<Tuple2Matcher, Second> MatcherBindSecond(
3864 const Tuple2Matcher& tm,
const Second& second) {
3865 return BoundSecondMatcher<Tuple2Matcher, Second>(tm, second);
3874 bool negation,
const char* matcher_name,
3875 const std::vector<const char*>& param_names,
const Strings& param_values);
3878template <
typename ValueMatcher>
3879class OptionalMatcher {
3881 explicit OptionalMatcher(
const ValueMatcher& value_matcher)
3882 : value_matcher_(value_matcher) {}
3884 template <
typename Optional>
3885 operator Matcher<Optional>()
const {
3886 return Matcher<Optional>(
new Impl<const Optional&>(value_matcher_));
3889 template <
typename Optional>
3890 class Impl :
public MatcherInterface<Optional> {
3892 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Optional) OptionalView;
3893 typedef typename OptionalView::value_type ValueType;
3894 explicit Impl(
const ValueMatcher& value_matcher)
3895 : value_matcher_(MatcherCast<ValueType>(value_matcher)) {}
3897 void DescribeTo(::std::ostream* os)
const override {
3899 value_matcher_.DescribeTo(os);
3902 void DescribeNegationTo(::std::ostream* os)
const override {
3904 value_matcher_.DescribeNegationTo(os);
3907 bool MatchAndExplain(Optional optional,
3908 MatchResultListener* listener)
const override {
3910 *listener <<
"which is not engaged";
3913 const ValueType& value = *optional;
3914 StringMatchResultListener value_listener;
3915 const bool match = value_matcher_.MatchAndExplain(value, &value_listener);
3916 *listener <<
"whose value " << PrintToString(value)
3917 << (match ?
" matches" :
" doesn't match");
3918 PrintIfNotEmpty(value_listener.str(), listener->stream());
3923 const Matcher<ValueType> value_matcher_;
3927 const ValueMatcher value_matcher_;
3930namespace variant_matcher {
3932template <
typename T>
3933void holds_alternative() {}
3934template <
typename T>
3938template <
typename T>
3939class VariantMatcher {
3942 : matcher_(std::move(matcher)) {}
3944 template <
typename Variant>
3945 bool MatchAndExplain(
const Variant& value,
3946 ::testing::MatchResultListener* listener)
const {
3948 if (!listener->IsInterested()) {
3949 return holds_alternative<T>(value) && matcher_.Matches(get<T>(value));
3952 if (!holds_alternative<T>(value)) {
3953 *listener <<
"whose value is not of type '" << GetTypeName() <<
"'";
3957 const T& elem = get<T>(value);
3958 StringMatchResultListener elem_listener;
3959 const bool match = matcher_.MatchAndExplain(elem, &elem_listener);
3960 *listener <<
"whose value " << PrintToString(elem)
3961 << (match ?
" matches" :
" doesn't match");
3962 PrintIfNotEmpty(elem_listener.str(), listener->stream());
3966 void DescribeTo(std::ostream* os)
const {
3967 *os <<
"is a variant<> with value of type '" << GetTypeName()
3968 <<
"' and the value ";
3969 matcher_.DescribeTo(os);
3972 void DescribeNegationTo(std::ostream* os)
const {
3973 *os <<
"is a variant<> with value of type other than '" << GetTypeName()
3974 <<
"' or the value ";
3975 matcher_.DescribeNegationTo(os);
3979 static std::string GetTypeName() {
3981 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(
3982 return internal::GetTypeName<T>());
3984 return "the element type";
3987 const ::testing::Matcher<const T&> matcher_;
3992namespace any_cast_matcher {
3995template <
typename T>
3999template <
typename T>
4000class AnyCastMatcher {
4002 explicit AnyCastMatcher(const ::testing::Matcher<const T&>& matcher)
4003 : matcher_(matcher) {}
4005 template <
typename AnyType>
4006 bool MatchAndExplain(
const AnyType& value,
4007 ::testing::MatchResultListener* listener)
const {
4008 if (!listener->IsInterested()) {
4009 const T* ptr = any_cast<T>(&value);
4010 return ptr !=
nullptr && matcher_.Matches(*ptr);
4013 const T* elem = any_cast<T>(&value);
4014 if (elem ==
nullptr) {
4015 *listener <<
"whose value is not of type '" << GetTypeName() <<
"'";
4019 StringMatchResultListener elem_listener;
4020 const bool match = matcher_.MatchAndExplain(*elem, &elem_listener);
4021 *listener <<
"whose value " << PrintToString(*elem)
4022 << (match ?
" matches" :
" doesn't match");
4023 PrintIfNotEmpty(elem_listener.str(), listener->stream());
4027 void DescribeTo(std::ostream* os)
const {
4028 *os <<
"is an 'any' type with value of type '" << GetTypeName()
4029 <<
"' and the value ";
4030 matcher_.DescribeTo(os);
4033 void DescribeNegationTo(std::ostream* os)
const {
4034 *os <<
"is an 'any' type with value of type other than '" << GetTypeName()
4035 <<
"' or the value ";
4036 matcher_.DescribeNegationTo(os);
4040 static std::string GetTypeName() {
4042 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(
4043 return internal::GetTypeName<T>());
4045 return "the element type";
4048 const ::testing::Matcher<const T&> matcher_;
4054template <
class ArgsTuple,
size_t... k>
4055class ArgsMatcherImpl :
public MatcherInterface<ArgsTuple> {
4057 using RawArgsTuple =
typename std::decay<ArgsTuple>::type;
4058 using SelectedArgs =
4059 std::tuple<typename std::tuple_element<k, RawArgsTuple>::type...>;
4060 using MonomorphicInnerMatcher = Matcher<const SelectedArgs&>;
4062 template <
typename InnerMatcher>
4063 explicit ArgsMatcherImpl(
const InnerMatcher& inner_matcher)
4064 : inner_matcher_(SafeMatcherCast<const SelectedArgs&>(inner_matcher)) {}
4066 bool MatchAndExplain(ArgsTuple args,
4067 MatchResultListener* listener)
const override {
4070 const SelectedArgs& selected_args =
4071 std::forward_as_tuple(std::get<k>(args)...);
4072 if (!listener->IsInterested())
return inner_matcher_.Matches(selected_args);
4074 PrintIndices(listener->stream());
4075 *listener <<
"are " << PrintToString(selected_args);
4077 StringMatchResultListener inner_listener;
4079 inner_matcher_.MatchAndExplain(selected_args, &inner_listener);
4080 PrintIfNotEmpty(inner_listener.str(), listener->stream());
4084 void DescribeTo(::std::ostream* os)
const override {
4085 *os <<
"are a tuple ";
4087 inner_matcher_.DescribeTo(os);
4090 void DescribeNegationTo(::std::ostream* os)
const override {
4091 *os <<
"are a tuple ";
4093 inner_matcher_.DescribeNegationTo(os);
4098 static void PrintIndices(::std::ostream* os) {
4099 *os <<
"whose fields (";
4100 const char* sep =
"";
4103 const char* dummy[] = {
"", (*os << sep <<
"#" << k, sep =
", ")...};
4108 MonomorphicInnerMatcher inner_matcher_;
4111template <
class InnerMatcher,
size_t... k>
4114 explicit ArgsMatcher(InnerMatcher inner_matcher)
4115 : inner_matcher_(std::move(inner_matcher)) {}
4117 template <
typename ArgsTuple>
4118 operator Matcher<ArgsTuple>()
const {
4119 return MakeMatcher(
new ArgsMatcherImpl<ArgsTuple, k...>(inner_matcher_));
4123 InnerMatcher inner_matcher_;
4143template <
typename Iter>
4144inline internal::ElementsAreArrayMatcher<
4145 typename ::std::iterator_traits<Iter>::value_type>
4146ElementsAreArray(Iter first, Iter last) {
4147 typedef typename ::std::iterator_traits<Iter>::value_type T;
4148 return internal::ElementsAreArrayMatcher<T>(first, last);
4151template <
typename T>
4152inline auto ElementsAreArray(
const T* pointer,
size_t count)
4153 ->
decltype(ElementsAreArray(pointer, pointer + count)) {
4154 return ElementsAreArray(pointer, pointer + count);
4157template <
typename T,
size_t N>
4158inline auto ElementsAreArray(
const T (&array)[N])
4159 ->
decltype(ElementsAreArray(array, N)) {
4160 return ElementsAreArray(array, N);
4163template <
typename Container>
4164inline auto ElementsAreArray(
const Container& container)
4165 ->
decltype(ElementsAreArray(container.begin(), container.end())) {
4166 return ElementsAreArray(container.begin(), container.end());
4169template <
typename T>
4170inline auto ElementsAreArray(::std::initializer_list<T> xs)
4171 ->
decltype(ElementsAreArray(xs.begin(), xs.end())) {
4172 return ElementsAreArray(xs.begin(), xs.end());
4188template <
typename Iter>
4189inline internal::UnorderedElementsAreArrayMatcher<
4190 typename ::std::iterator_traits<Iter>::value_type>
4191UnorderedElementsAreArray(Iter first, Iter last) {
4192 typedef typename ::std::iterator_traits<Iter>::value_type T;
4193 return internal::UnorderedElementsAreArrayMatcher<T>(
4194 internal::UnorderedMatcherRequire::ExactMatch, first, last);
4197template <
typename T>
4198inline internal::UnorderedElementsAreArrayMatcher<T> UnorderedElementsAreArray(
4199 const T* pointer,
size_t count) {
4200 return UnorderedElementsAreArray(pointer, pointer + count);
4203template <
typename T,
size_t N>
4204inline internal::UnorderedElementsAreArrayMatcher<T> UnorderedElementsAreArray(
4205 const T (&array)[N]) {
4206 return UnorderedElementsAreArray(array, N);
4209template <
typename Container>
4210inline internal::UnorderedElementsAreArrayMatcher<
4211 typename Container::value_type>
4212UnorderedElementsAreArray(
const Container& container) {
4213 return UnorderedElementsAreArray(container.begin(), container.end());
4216template <
typename T>
4217inline internal::UnorderedElementsAreArrayMatcher<T> UnorderedElementsAreArray(
4218 ::std::initializer_list<T> xs) {
4219 return UnorderedElementsAreArray(xs.begin(), xs.end());
4231const internal::AnythingMatcher _ = {};
4233template <
typename T>
4234inline Matcher<T> A() {
4239template <
typename T>
4240inline Matcher<T> An() {
4244template <
typename T,
typename M>
4245Matcher<T> internal::MatcherCastImpl<T, M>::CastImpl(
4246 const M& value, std::false_type ,
4252inline PolymorphicMatcher<internal::IsNullMatcher> IsNull() {
4253 return MakePolymorphicMatcher(internal::IsNullMatcher());
4259inline PolymorphicMatcher<internal::NotNullMatcher> NotNull() {
4260 return MakePolymorphicMatcher(internal::NotNullMatcher());
4265template <
typename T>
4266inline internal::RefMatcher<T&> Ref(T& x) {
4267 return internal::RefMatcher<T&>(x);
4271inline PolymorphicMatcher<internal::IsNanMatcher> IsNan() {
4272 return MakePolymorphicMatcher(internal::IsNanMatcher());
4277inline internal::FloatingEqMatcher<double> DoubleEq(
double rhs) {
4278 return internal::FloatingEqMatcher<double>(rhs,
false);
4283inline internal::FloatingEqMatcher<double> NanSensitiveDoubleEq(
double rhs) {
4284 return internal::FloatingEqMatcher<double>(rhs,
true);
4290inline internal::FloatingEqMatcher<double> DoubleNear(
double rhs,
4291 double max_abs_error) {
4292 return internal::FloatingEqMatcher<double>(rhs,
false, max_abs_error);
4298inline internal::FloatingEqMatcher<double> NanSensitiveDoubleNear(
4299 double rhs,
double max_abs_error) {
4300 return internal::FloatingEqMatcher<double>(rhs,
true, max_abs_error);
4305inline internal::FloatingEqMatcher<float> FloatEq(
float rhs) {
4306 return internal::FloatingEqMatcher<float>(rhs,
false);
4311inline internal::FloatingEqMatcher<float> NanSensitiveFloatEq(
float rhs) {
4312 return internal::FloatingEqMatcher<float>(rhs,
true);
4318inline internal::FloatingEqMatcher<float> FloatNear(
float rhs,
4319 float max_abs_error) {
4320 return internal::FloatingEqMatcher<float>(rhs,
false, max_abs_error);
4326inline internal::FloatingEqMatcher<float> NanSensitiveFloatNear(
4327 float rhs,
float max_abs_error) {
4328 return internal::FloatingEqMatcher<float>(rhs,
true, max_abs_error);
4333template <
typename InnerMatcher>
4334inline internal::PointeeMatcher<InnerMatcher> Pointee(
4335 const InnerMatcher& inner_matcher) {
4336 return internal::PointeeMatcher<InnerMatcher>(inner_matcher);
4346template <
typename To>
4347inline PolymorphicMatcher<internal::WhenDynamicCastToMatcher<To>>
4348WhenDynamicCastTo(
const Matcher<To>& inner_matcher) {
4349 return MakePolymorphicMatcher(
4350 internal::WhenDynamicCastToMatcher<To>(inner_matcher));
4358template <
typename Class,
typename FieldType,
typename FieldMatcher>
4359inline PolymorphicMatcher<internal::FieldMatcher<Class, FieldType>> Field(
4360 FieldType Class::*field,
const FieldMatcher& matcher) {
4361 return MakePolymorphicMatcher(internal::FieldMatcher<Class, FieldType>(
4362 field, MatcherCast<const FieldType&>(matcher)));
4371template <
typename Class,
typename FieldType,
typename FieldMatcher>
4372inline PolymorphicMatcher<internal::FieldMatcher<Class, FieldType>> Field(
4373 const std::string& field_name, FieldType Class::*field,
4374 const FieldMatcher& matcher) {
4375 return MakePolymorphicMatcher(internal::FieldMatcher<Class, FieldType>(
4376 field_name, field, MatcherCast<const FieldType&>(matcher)));
4383template <
typename Class,
typename PropertyType,
typename PropertyMatcher>
4384inline PolymorphicMatcher<internal::PropertyMatcher<
4385 Class, PropertyType, PropertyType (Class::*)()
const>>
4386Property(PropertyType (Class::*property)()
const,
4387 const PropertyMatcher& matcher) {
4388 return MakePolymorphicMatcher(
4389 internal::PropertyMatcher<Class, PropertyType,
4390 PropertyType (Class::*)()
const>(
4391 property, MatcherCast<const PropertyType&>(matcher)));
4400template <
typename Class,
typename PropertyType,
typename PropertyMatcher>
4401inline PolymorphicMatcher<internal::PropertyMatcher<
4402 Class, PropertyType, PropertyType (Class::*)()
const>>
4403Property(
const std::string& property_name,
4404 PropertyType (Class::*property)()
const,
4405 const PropertyMatcher& matcher) {
4406 return MakePolymorphicMatcher(
4407 internal::PropertyMatcher<Class, PropertyType,
4408 PropertyType (Class::*)()
const>(
4409 property_name, property, MatcherCast<const PropertyType&>(matcher)));
4413template <
typename Class,
typename PropertyType,
typename PropertyMatcher>
4414inline PolymorphicMatcher<internal::PropertyMatcher<
4415 Class, PropertyType, PropertyType (Class::*)()
const&>>
4416Property(PropertyType (Class::*property)()
const&,
4417 const PropertyMatcher& matcher) {
4418 return MakePolymorphicMatcher(
4419 internal::PropertyMatcher<Class, PropertyType,
4420 PropertyType (Class::*)()
const&>(
4421 property, MatcherCast<const PropertyType&>(matcher)));
4425template <
typename Class,
typename PropertyType,
typename PropertyMatcher>
4426inline PolymorphicMatcher<internal::PropertyMatcher<
4427 Class, PropertyType, PropertyType (Class::*)()
const&>>
4428Property(
const std::string& property_name,
4429 PropertyType (Class::*property)()
const&,
4430 const PropertyMatcher& matcher) {
4431 return MakePolymorphicMatcher(
4432 internal::PropertyMatcher<Class, PropertyType,
4433 PropertyType (Class::*)()
const&>(
4434 property_name, property, MatcherCast<const PropertyType&>(matcher)));
4445template <
typename Callable,
typename InnerMatcher>
4446internal::ResultOfMatcher<Callable, InnerMatcher> ResultOf(
4447 Callable callable, InnerMatcher matcher) {
4448 return internal::ResultOfMatcher<Callable, InnerMatcher>(std::move(callable),
4449 std::move(matcher));
4454template <
typename Callable,
typename InnerMatcher>
4455internal::ResultOfMatcher<Callable, InnerMatcher> ResultOf(
4456 const std::string& result_description, Callable callable,
4457 InnerMatcher matcher) {
4458 return internal::ResultOfMatcher<Callable, InnerMatcher>(
4459 result_description, std::move(callable), std::move(matcher));
4465template <
typename T = std::
string>
4466PolymorphicMatcher<internal::StrEqualityMatcher<std::string>> StrEq(
4467 const internal::StringLike<T>& str) {
4468 return MakePolymorphicMatcher(
4469 internal::StrEqualityMatcher<std::string>(std::string(str),
true,
true));
4473template <
typename T = std::
string>
4474PolymorphicMatcher<internal::StrEqualityMatcher<std::string>> StrNe(
4475 const internal::StringLike<T>& str) {
4476 return MakePolymorphicMatcher(
4477 internal::StrEqualityMatcher<std::string>(std::string(str),
false,
true));
4481template <
typename T = std::
string>
4482PolymorphicMatcher<internal::StrEqualityMatcher<std::string>> StrCaseEq(
4483 const internal::StringLike<T>& str) {
4484 return MakePolymorphicMatcher(
4485 internal::StrEqualityMatcher<std::string>(std::string(str),
true,
false));
4489template <
typename T = std::
string>
4490PolymorphicMatcher<internal::StrEqualityMatcher<std::string>> StrCaseNe(
4491 const internal::StringLike<T>& str) {
4492 return MakePolymorphicMatcher(internal::StrEqualityMatcher<std::string>(
4493 std::string(str),
false,
false));
4498template <
typename T = std::
string>
4499PolymorphicMatcher<internal::HasSubstrMatcher<std::string>> HasSubstr(
4500 const internal::StringLike<T>& substring) {
4501 return MakePolymorphicMatcher(
4502 internal::HasSubstrMatcher<std::string>(std::string(substring)));
4506template <
typename T = std::
string>
4507PolymorphicMatcher<internal::StartsWithMatcher<std::string>> StartsWith(
4508 const internal::StringLike<T>& prefix) {
4509 return MakePolymorphicMatcher(
4510 internal::StartsWithMatcher<std::string>(std::string(prefix)));
4514template <
typename T = std::
string>
4515PolymorphicMatcher<internal::EndsWithMatcher<std::string>> EndsWith(
4516 const internal::StringLike<T>& suffix) {
4517 return MakePolymorphicMatcher(
4518 internal::EndsWithMatcher<std::string>(std::string(suffix)));
4521#if GTEST_HAS_STD_WSTRING
4525inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring>> StrEq(
4526 const std::wstring& str) {
4527 return MakePolymorphicMatcher(
4528 internal::StrEqualityMatcher<std::wstring>(str,
true,
true));
4532inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring>> StrNe(
4533 const std::wstring& str) {
4534 return MakePolymorphicMatcher(
4535 internal::StrEqualityMatcher<std::wstring>(str,
false,
true));
4539inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring>> StrCaseEq(
4540 const std::wstring& str) {
4541 return MakePolymorphicMatcher(
4542 internal::StrEqualityMatcher<std::wstring>(str,
true,
false));
4546inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring>> StrCaseNe(
4547 const std::wstring& str) {
4548 return MakePolymorphicMatcher(
4549 internal::StrEqualityMatcher<std::wstring>(str,
false,
false));
4554inline PolymorphicMatcher<internal::HasSubstrMatcher<std::wstring>> HasSubstr(
4555 const std::wstring& substring) {
4556 return MakePolymorphicMatcher(
4557 internal::HasSubstrMatcher<std::wstring>(substring));
4561inline PolymorphicMatcher<internal::StartsWithMatcher<std::wstring>> StartsWith(
4562 const std::wstring& prefix) {
4563 return MakePolymorphicMatcher(
4564 internal::StartsWithMatcher<std::wstring>(prefix));
4568inline PolymorphicMatcher<internal::EndsWithMatcher<std::wstring>> EndsWith(
4569 const std::wstring& suffix) {
4570 return MakePolymorphicMatcher(
4571 internal::EndsWithMatcher<std::wstring>(suffix));
4578inline internal::Eq2Matcher Eq() {
return internal::Eq2Matcher(); }
4582inline internal::Ge2Matcher Ge() {
return internal::Ge2Matcher(); }
4586inline internal::Gt2Matcher Gt() {
return internal::Gt2Matcher(); }
4590inline internal::Le2Matcher Le() {
return internal::Le2Matcher(); }
4594inline internal::Lt2Matcher Lt() {
return internal::Lt2Matcher(); }
4598inline internal::Ne2Matcher Ne() {
return internal::Ne2Matcher(); }
4602inline internal::FloatingEq2Matcher<float> FloatEq() {
4603 return internal::FloatingEq2Matcher<float>();
4608inline internal::FloatingEq2Matcher<double> DoubleEq() {
4609 return internal::FloatingEq2Matcher<double>();
4614inline internal::FloatingEq2Matcher<float> NanSensitiveFloatEq() {
4615 return internal::FloatingEq2Matcher<float>(
true);
4620inline internal::FloatingEq2Matcher<double> NanSensitiveDoubleEq() {
4621 return internal::FloatingEq2Matcher<double>(
true);
4626inline internal::FloatingEq2Matcher<float> FloatNear(
float max_abs_error) {
4627 return internal::FloatingEq2Matcher<float>(max_abs_error);
4632inline internal::FloatingEq2Matcher<double> DoubleNear(
double max_abs_error) {
4633 return internal::FloatingEq2Matcher<double>(max_abs_error);
4639inline internal::FloatingEq2Matcher<float> NanSensitiveFloatNear(
4640 float max_abs_error) {
4641 return internal::FloatingEq2Matcher<float>(max_abs_error,
true);
4647inline internal::FloatingEq2Matcher<double> NanSensitiveDoubleNear(
4648 double max_abs_error) {
4649 return internal::FloatingEq2Matcher<double>(max_abs_error,
true);
4654template <
typename InnerMatcher>
4655inline internal::NotMatcher<InnerMatcher> Not(InnerMatcher m) {
4656 return internal::NotMatcher<InnerMatcher>(m);
4662template <
typename Predicate>
4663inline PolymorphicMatcher<internal::TrulyMatcher<Predicate>> Truly(
4665 return MakePolymorphicMatcher(internal::TrulyMatcher<Predicate>(pred));
4674template <
typename SizeMatcher>
4675inline internal::SizeIsMatcher<SizeMatcher> SizeIs(
4676 const SizeMatcher& size_matcher) {
4677 return internal::SizeIsMatcher<SizeMatcher>(size_matcher);
4685template <
typename DistanceMatcher>
4686inline internal::BeginEndDistanceIsMatcher<DistanceMatcher> BeginEndDistanceIs(
4687 const DistanceMatcher& distance_matcher) {
4688 return internal::BeginEndDistanceIsMatcher<DistanceMatcher>(distance_matcher);
4695template <
typename Container>
4696inline PolymorphicMatcher<
4697 internal::ContainerEqMatcher<typename std::remove_const<Container>::type>>
4698ContainerEq(
const Container& rhs) {
4699 return MakePolymorphicMatcher(internal::ContainerEqMatcher<Container>(rhs));
4704template <
typename Comparator,
typename ContainerMatcher>
4705inline internal::WhenSortedByMatcher<Comparator, ContainerMatcher> WhenSortedBy(
4706 const Comparator& comparator,
const ContainerMatcher& container_matcher) {
4707 return internal::WhenSortedByMatcher<Comparator, ContainerMatcher>(
4708 comparator, container_matcher);
4713template <
typename ContainerMatcher>
4714inline internal::WhenSortedByMatcher<internal::LessComparator, ContainerMatcher>
4715WhenSorted(
const ContainerMatcher& container_matcher) {
4716 return internal::WhenSortedByMatcher<internal::LessComparator,
4718 internal::LessComparator(), container_matcher);
4727template <
typename TupleMatcher,
typename Container>
4728inline internal::PointwiseMatcher<TupleMatcher,
4729 typename std::remove_const<Container>::type>
4730Pointwise(
const TupleMatcher& tuple_matcher,
const Container& rhs) {
4731 return internal::PointwiseMatcher<TupleMatcher, Container>(tuple_matcher,
4736template <
typename TupleMatcher,
typename T>
4737inline internal::PointwiseMatcher<TupleMatcher, std::vector<T>> Pointwise(
4738 const TupleMatcher& tuple_matcher, std::initializer_list<T> rhs) {
4739 return Pointwise(tuple_matcher, std::vector<T>(rhs));
4753template <
typename Tuple2Matcher,
typename RhsContainer>
4754inline internal::UnorderedElementsAreArrayMatcher<
4755 typename internal::BoundSecondMatcher<
4757 typename internal::StlContainerView<
4758 typename std::remove_const<RhsContainer>::type>::type::value_type>>
4759UnorderedPointwise(
const Tuple2Matcher& tuple2_matcher,
4760 const RhsContainer& rhs_container) {
4763 typedef typename internal::StlContainerView<RhsContainer> RhsView;
4764 typedef typename RhsView::type RhsStlContainer;
4765 typedef typename RhsStlContainer::value_type Second;
4766 const RhsStlContainer& rhs_stl_container =
4767 RhsView::ConstReference(rhs_container);
4770 ::std::vector<internal::BoundSecondMatcher<Tuple2Matcher, Second>> matchers;
4771 for (
auto it = rhs_stl_container.begin(); it != rhs_stl_container.end();
4773 matchers.push_back(internal::MatcherBindSecond(tuple2_matcher, *it));
4777 return UnorderedElementsAreArray(matchers);
4781template <
typename Tuple2Matcher,
typename T>
4782inline internal::UnorderedElementsAreArrayMatcher<
4783 typename internal::BoundSecondMatcher<Tuple2Matcher, T>>
4784UnorderedPointwise(
const Tuple2Matcher& tuple2_matcher,
4785 std::initializer_list<T> rhs) {
4786 return UnorderedPointwise(tuple2_matcher, std::vector<T>(rhs));
4820template <
typename M>
4821inline internal::ContainsMatcher<M> Contains(M matcher) {
4822 return internal::ContainsMatcher<M>(matcher);
4852template <
typename Iter>
4853inline internal::UnorderedElementsAreArrayMatcher<
4854 typename ::std::iterator_traits<Iter>::value_type>
4855IsSupersetOf(Iter first, Iter last) {
4856 typedef typename ::std::iterator_traits<Iter>::value_type T;
4857 return internal::UnorderedElementsAreArrayMatcher<T>(
4858 internal::UnorderedMatcherRequire::Superset, first, last);
4861template <
typename T>
4862inline internal::UnorderedElementsAreArrayMatcher<T> IsSupersetOf(
4863 const T* pointer,
size_t count) {
4864 return IsSupersetOf(pointer, pointer + count);
4867template <
typename T,
size_t N>
4868inline internal::UnorderedElementsAreArrayMatcher<T> IsSupersetOf(
4869 const T (&array)[N]) {
4870 return IsSupersetOf(array, N);
4873template <
typename Container>
4874inline internal::UnorderedElementsAreArrayMatcher<
4875 typename Container::value_type>
4876IsSupersetOf(
const Container& container) {
4877 return IsSupersetOf(container.begin(), container.end());
4880template <
typename T>
4881inline internal::UnorderedElementsAreArrayMatcher<T> IsSupersetOf(
4882 ::std::initializer_list<T> xs) {
4883 return IsSupersetOf(xs.begin(), xs.end());
4909template <
typename Iter>
4910inline internal::UnorderedElementsAreArrayMatcher<
4911 typename ::std::iterator_traits<Iter>::value_type>
4912IsSubsetOf(Iter first, Iter last) {
4913 typedef typename ::std::iterator_traits<Iter>::value_type T;
4914 return internal::UnorderedElementsAreArrayMatcher<T>(
4915 internal::UnorderedMatcherRequire::Subset, first, last);
4918template <
typename T>
4919inline internal::UnorderedElementsAreArrayMatcher<T> IsSubsetOf(
4920 const T* pointer,
size_t count) {
4921 return IsSubsetOf(pointer, pointer + count);
4924template <
typename T,
size_t N>
4925inline internal::UnorderedElementsAreArrayMatcher<T> IsSubsetOf(
4926 const T (&array)[N]) {
4927 return IsSubsetOf(array, N);
4930template <
typename Container>
4931inline internal::UnorderedElementsAreArrayMatcher<
4932 typename Container::value_type>
4933IsSubsetOf(
const Container& container) {
4934 return IsSubsetOf(container.begin(), container.end());
4937template <
typename T>
4938inline internal::UnorderedElementsAreArrayMatcher<T> IsSubsetOf(
4939 ::std::initializer_list<T> xs) {
4940 return IsSubsetOf(xs.begin(), xs.end());
4970template <
typename M>
4971inline internal::EachMatcher<M> Each(M matcher) {
4972 return internal::EachMatcher<M>(matcher);
4978template <
typename M>
4979inline internal::KeyMatcher<M> Key(M inner_matcher) {
4980 return internal::KeyMatcher<M>(inner_matcher);
4988template <
typename FirstMatcher,
typename SecondMatcher>
4989inline internal::PairMatcher<FirstMatcher, SecondMatcher> Pair(
4990 FirstMatcher first_matcher, SecondMatcher second_matcher) {
4991 return internal::PairMatcher<FirstMatcher, SecondMatcher>(first_matcher,
5001template <
typename MatcherTrue,
typename MatcherFalse>
5002internal::ConditionalMatcher<MatcherTrue, MatcherFalse> Conditional(
5003 bool condition, MatcherTrue matcher_true, MatcherFalse matcher_false) {
5004 return internal::ConditionalMatcher<MatcherTrue, MatcherFalse>(
5005 condition, std::move(matcher_true), std::move(matcher_false));
5012template <
typename... M>
5013internal::FieldsAreMatcher<typename std::decay<M>::type...> FieldsAre(
5015 return internal::FieldsAreMatcher<typename std::decay<M>::type...>(
5016 std::forward<M>(matchers)...);
5021template <
typename InnerMatcher>
5022inline internal::PointerMatcher<InnerMatcher> Pointer(
5023 const InnerMatcher& inner_matcher) {
5024 return internal::PointerMatcher<InnerMatcher>(inner_matcher);
5029template <
typename InnerMatcher>
5030inline internal::AddressMatcher<InnerMatcher> Address(
5031 const InnerMatcher& inner_matcher) {
5032 return internal::AddressMatcher<InnerMatcher>(inner_matcher);
5037template <
typename MatcherType>
5038internal::WhenBase64UnescapedMatcher WhenBase64Unescaped(
5039 const MatcherType& internal_matcher) {
5040 return internal::WhenBase64UnescapedMatcher(internal_matcher);
5046template <
typename M>
5047inline internal::MatcherAsPredicate<M> Matches(M matcher) {
5048 return internal::MatcherAsPredicate<M>(matcher);
5052template <
typename T,
typename M>
5053inline bool Value(
const T& value, M matcher) {
5054 return testing::Matches(matcher)(value);
5059template <
typename T,
typename M>
5060inline bool ExplainMatchResult(M matcher,
const T& value,
5061 MatchResultListener* listener) {
5062 return SafeMatcherCast<const T&>(matcher).MatchAndExplain(value, listener);
5076template <
typename T,
typename M>
5077std::string DescribeMatcher(
const M& matcher,
bool negation =
false) {
5078 ::std::stringstream ss;
5079 Matcher<T> monomorphic_matcher = SafeMatcherCast<T>(matcher);
5081 monomorphic_matcher.DescribeNegationTo(&ss);
5083 monomorphic_matcher.DescribeTo(&ss);
5088template <
typename... Args>
5089internal::ElementsAreMatcher<
5090 std::tuple<typename std::decay<const Args&>::type...>>
5091ElementsAre(
const Args&... matchers) {
5092 return internal::ElementsAreMatcher<
5093 std::tuple<typename std::decay<const Args&>::type...>>(
5094 std::make_tuple(matchers...));
5097template <
typename... Args>
5098internal::UnorderedElementsAreMatcher<
5099 std::tuple<typename std::decay<const Args&>::type...>>
5100UnorderedElementsAre(
const Args&... matchers) {
5101 return internal::UnorderedElementsAreMatcher<
5102 std::tuple<typename std::decay<const Args&>::type...>>(
5103 std::make_tuple(matchers...));
5107template <
typename... Args>
5108internal::AllOfMatcher<typename std::decay<const Args&>::type...> AllOf(
5109 const Args&... matchers) {
5110 return internal::AllOfMatcher<typename std::decay<const Args&>::type...>(
5114template <
typename... Args>
5115internal::AnyOfMatcher<typename std::decay<const Args&>::type...> AnyOf(
5116 const Args&... matchers) {
5117 return internal::AnyOfMatcher<typename std::decay<const Args&>::type...>(
5143template <
typename Iter>
5144inline internal::AnyOfArrayMatcher<
5145 typename ::std::iterator_traits<Iter>::value_type>
5146AnyOfArray(Iter first, Iter last) {
5147 return internal::AnyOfArrayMatcher<
5148 typename ::std::iterator_traits<Iter>::value_type>(first, last);
5151template <
typename Iter>
5152inline internal::AllOfArrayMatcher<
5153 typename ::std::iterator_traits<Iter>::value_type>
5154AllOfArray(Iter first, Iter last) {
5155 return internal::AllOfArrayMatcher<
5156 typename ::std::iterator_traits<Iter>::value_type>(first, last);
5159template <
typename T>
5160inline internal::AnyOfArrayMatcher<T> AnyOfArray(
const T* ptr,
size_t count) {
5161 return AnyOfArray(ptr, ptr + count);
5164template <
typename T>
5165inline internal::AllOfArrayMatcher<T> AllOfArray(
const T* ptr,
size_t count) {
5166 return AllOfArray(ptr, ptr + count);
5169template <
typename T,
size_t N>
5170inline internal::AnyOfArrayMatcher<T> AnyOfArray(
const T (&array)[N]) {
5171 return AnyOfArray(array, N);
5174template <
typename T,
size_t N>
5175inline internal::AllOfArrayMatcher<T> AllOfArray(
const T (&array)[N]) {
5176 return AllOfArray(array, N);
5179template <
typename Container>
5180inline internal::AnyOfArrayMatcher<typename Container::value_type> AnyOfArray(
5181 const Container& container) {
5182 return AnyOfArray(container.begin(), container.end());
5185template <
typename Container>
5186inline internal::AllOfArrayMatcher<typename Container::value_type> AllOfArray(
5187 const Container& container) {
5188 return AllOfArray(container.begin(), container.end());
5191template <
typename T>
5192inline internal::AnyOfArrayMatcher<T> AnyOfArray(
5193 ::std::initializer_list<T> xs) {
5194 return AnyOfArray(xs.begin(), xs.end());
5197template <
typename T>
5198inline internal::AllOfArrayMatcher<T> AllOfArray(
5199 ::std::initializer_list<T> xs) {
5200 return AllOfArray(xs.begin(), xs.end());
5206template <
size_t... k,
typename InnerMatcher>
5207internal::ArgsMatcher<typename std::decay<InnerMatcher>::type, k...> Args(
5208 InnerMatcher&& matcher) {
5209 return internal::ArgsMatcher<typename std::decay<InnerMatcher>::type, k...>(
5210 std::forward<InnerMatcher>(matcher));
5220template <
typename InnerMatcher>
5221inline InnerMatcher AllArgs(
const InnerMatcher& matcher) {
5233template <
typename ValueMatcher>
5234inline internal::OptionalMatcher<ValueMatcher> Optional(
5235 const ValueMatcher& value_matcher) {
5236 return internal::OptionalMatcher<ValueMatcher>(value_matcher);
5240template <
typename T>
5241PolymorphicMatcher<internal::any_cast_matcher::AnyCastMatcher<T>> AnyWith(
5242 const Matcher<const T&>& matcher) {
5243 return MakePolymorphicMatcher(
5244 internal::any_cast_matcher::AnyCastMatcher<T>(matcher));
5251template <
typename T>
5252PolymorphicMatcher<internal::variant_matcher::VariantMatcher<T>> VariantWith(
5253 const Matcher<const T&>& matcher) {
5254 return MakePolymorphicMatcher(
5255 internal::variant_matcher::VariantMatcher<T>(matcher));
5258#if GTEST_HAS_EXCEPTIONS
5264class WithWhatMatcherImpl {
5266 WithWhatMatcherImpl(Matcher<std::string> matcher)
5267 : matcher_(std::move(matcher)) {}
5269 void DescribeTo(std::ostream* os)
const {
5270 *os <<
"contains .what() that ";
5271 matcher_.DescribeTo(os);
5274 void DescribeNegationTo(std::ostream* os)
const {
5275 *os <<
"contains .what() that does not ";
5276 matcher_.DescribeTo(os);
5279 template <
typename Err>
5280 bool MatchAndExplain(
const Err& err, MatchResultListener* listener)
const {
5281 *listener <<
"which contains .what() (of value = " << err.what()
5283 return matcher_.MatchAndExplain(err.what(), listener);
5287 const Matcher<std::string> matcher_;
5290inline PolymorphicMatcher<WithWhatMatcherImpl> WithWhat(
5291 Matcher<std::string> m) {
5292 return MakePolymorphicMatcher(WithWhatMatcherImpl(std::move(m)));
5295template <
typename Err>
5296class ExceptionMatcherImpl {
5299 const char* what()
const noexcept {
5300 return "this exception should never be thrown";
5325 using DefaultExceptionType =
typename std::conditional<
5326 std::is_same<
typename std::remove_cv<
5327 typename std::remove_reference<Err>::type>::type,
5328 std::exception>::value,
5329 const NeverThrown&,
const std::exception&>::type;
5332 ExceptionMatcherImpl(Matcher<const Err&> matcher)
5333 : matcher_(std::move(matcher)) {}
5335 void DescribeTo(std::ostream* os)
const {
5336 *os <<
"throws an exception which is a " << GetTypeName<Err>();
5338 matcher_.DescribeTo(os);
5341 void DescribeNegationTo(std::ostream* os)
const {
5342 *os <<
"throws an exception which is not a " << GetTypeName<Err>();
5344 matcher_.DescribeNegationTo(os);
5347 template <
typename T>
5348 bool MatchAndExplain(T&& x, MatchResultListener* listener)
const {
5350 (void)(std::forward<T>(x)());
5351 }
catch (
const Err& err) {
5352 *listener <<
"throws an exception which is a " << GetTypeName<Err>();
5354 return matcher_.MatchAndExplain(err, listener);
5355 }
catch (DefaultExceptionType err) {
5357 *listener <<
"throws an exception of type " << GetTypeName(
typeid(err));
5360 *listener <<
"throws an std::exception-derived type ";
5362 *listener <<
"with description \"" << err.what() <<
"\"";
5365 *listener <<
"throws an exception of an unknown type";
5369 *listener <<
"does not throw any exception";
5374 const Matcher<const Err&> matcher_;
5401template <
typename Err>
5402PolymorphicMatcher<internal::ExceptionMatcherImpl<Err>> Throws() {
5403 return MakePolymorphicMatcher(
5404 internal::ExceptionMatcherImpl<Err>(A<const Err&>()));
5407template <
typename Err,
typename ExceptionMatcher>
5408PolymorphicMatcher<internal::ExceptionMatcherImpl<Err>> Throws(
5409 const ExceptionMatcher& exception_matcher) {
5413 return MakePolymorphicMatcher(internal::ExceptionMatcherImpl<Err>(
5414 SafeMatcherCast<const Err&>(exception_matcher)));
5417template <
typename Err,
typename MessageMatcher>
5418PolymorphicMatcher<internal::ExceptionMatcherImpl<Err>> ThrowsMessage(
5419 MessageMatcher&& message_matcher) {
5420 static_assert(std::is_base_of<std::exception, Err>::value,
5421 "expected an std::exception-derived type");
5422 return Throws<Err>(internal::WithWhat(
5423 MatcherCast<std::string>(std::forward<MessageMatcher>(message_matcher))));
5432#define ASSERT_THAT(value, matcher) \
5433 ASSERT_PRED_FORMAT1( \
5434 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
5435#define EXPECT_THAT(value, matcher) \
5436 EXPECT_PRED_FORMAT1( \
5437 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
5440#define MATCHER(name, description) \
5441 class name##Matcher \
5442 : public ::testing::internal::MatcherBaseImpl<name##Matcher> { \
5444 template <typename arg_type> \
5445 class gmock_Impl : public ::testing::MatcherInterface<const arg_type&> { \
5448 bool MatchAndExplain( \
5449 const arg_type& arg, \
5450 ::testing::MatchResultListener* result_listener) const override; \
5451 void DescribeTo(::std::ostream* gmock_os) const override { \
5452 *gmock_os << FormatDescription(false); \
5454 void DescribeNegationTo(::std::ostream* gmock_os) const override { \
5455 *gmock_os << FormatDescription(true); \
5459 ::std::string FormatDescription(bool negation) const { \
5461 ::std::string gmock_description = (description); \
5462 if (!gmock_description.empty()) { \
5463 return gmock_description; \
5465 return ::testing::internal::FormatMatcherDescription(negation, #name, \
5470 GTEST_ATTRIBUTE_UNUSED_ inline name##Matcher name() { return {}; } \
5471 template <typename arg_type> \
5472 bool name##Matcher::gmock_Impl<arg_type>::MatchAndExplain( \
5473 const arg_type& arg, \
5474 ::testing::MatchResultListener* result_listener GTEST_ATTRIBUTE_UNUSED_) \
5477#define MATCHER_P(name, p0, description) \
5478 GMOCK_INTERNAL_MATCHER(name, name##MatcherP, description, (#p0), (p0))
5479#define MATCHER_P2(name, p0, p1, description) \
5480 GMOCK_INTERNAL_MATCHER(name, name##MatcherP2, description, (#p0, #p1), \
5482#define MATCHER_P3(name, p0, p1, p2, description) \
5483 GMOCK_INTERNAL_MATCHER(name, name##MatcherP3, description, (#p0, #p1, #p2), \
5485#define MATCHER_P4(name, p0, p1, p2, p3, description) \
5486 GMOCK_INTERNAL_MATCHER(name, name##MatcherP4, description, \
5487 (#p0, #p1, #p2, #p3), (p0, p1, p2, p3))
5488#define MATCHER_P5(name, p0, p1, p2, p3, p4, description) \
5489 GMOCK_INTERNAL_MATCHER(name, name##MatcherP5, description, \
5490 (#p0, #p1, #p2, #p3, #p4), (p0, p1, p2, p3, p4))
5491#define MATCHER_P6(name, p0, p1, p2, p3, p4, p5, description) \
5492 GMOCK_INTERNAL_MATCHER(name, name##MatcherP6, description, \
5493 (#p0, #p1, #p2, #p3, #p4, #p5), \
5494 (p0, p1, p2, p3, p4, p5))
5495#define MATCHER_P7(name, p0, p1, p2, p3, p4, p5, p6, description) \
5496 GMOCK_INTERNAL_MATCHER(name, name##MatcherP7, description, \
5497 (#p0, #p1, #p2, #p3, #p4, #p5, #p6), \
5498 (p0, p1, p2, p3, p4, p5, p6))
5499#define MATCHER_P8(name, p0, p1, p2, p3, p4, p5, p6, p7, description) \
5500 GMOCK_INTERNAL_MATCHER(name, name##MatcherP8, description, \
5501 (#p0, #p1, #p2, #p3, #p4, #p5, #p6, #p7), \
5502 (p0, p1, p2, p3, p4, p5, p6, p7))
5503#define MATCHER_P9(name, p0, p1, p2, p3, p4, p5, p6, p7, p8, description) \
5504 GMOCK_INTERNAL_MATCHER(name, name##MatcherP9, description, \
5505 (#p0, #p1, #p2, #p3, #p4, #p5, #p6, #p7, #p8), \
5506 (p0, p1, p2, p3, p4, p5, p6, p7, p8))
5507#define MATCHER_P10(name, p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, description) \
5508 GMOCK_INTERNAL_MATCHER(name, name##MatcherP10, description, \
5509 (#p0, #p1, #p2, #p3, #p4, #p5, #p6, #p7, #p8, #p9), \
5510 (p0, p1, p2, p3, p4, p5, p6, p7, p8, p9))
5512#define GMOCK_INTERNAL_MATCHER(name, full_name, description, arg_names, args) \
5513 template <GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args)> \
5514 class full_name : public ::testing::internal::MatcherBaseImpl< \
5515 full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>> { \
5517 using full_name::MatcherBaseImpl::MatcherBaseImpl; \
5518 template <typename arg_type> \
5519 class gmock_Impl : public ::testing::MatcherInterface<const arg_type&> { \
5521 explicit gmock_Impl(GMOCK_INTERNAL_MATCHER_FUNCTION_ARGS(args)) \
5522 : GMOCK_INTERNAL_MATCHER_FORWARD_ARGS(args) {} \
5523 bool MatchAndExplain( \
5524 const arg_type& arg, \
5525 ::testing::MatchResultListener* result_listener) const override; \
5526 void DescribeTo(::std::ostream* gmock_os) const override { \
5527 *gmock_os << FormatDescription(false); \
5529 void DescribeNegationTo(::std::ostream* gmock_os) const override { \
5530 *gmock_os << FormatDescription(true); \
5532 GMOCK_INTERNAL_MATCHER_MEMBERS(args) \
5535 ::std::string FormatDescription(bool negation) const { \
5536 ::std::string gmock_description = (description); \
5537 if (!gmock_description.empty()) { \
5538 return gmock_description; \
5540 return ::testing::internal::FormatMatcherDescription( \
5541 negation, #name, {GMOCK_PP_REMOVE_PARENS(arg_names)}, \
5542 ::testing::internal::UniversalTersePrintTupleFieldsToStrings( \
5543 ::std::tuple<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>( \
5544 GMOCK_INTERNAL_MATCHER_MEMBERS_USAGE(args)))); \
5548 template <GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args)> \
5549 inline full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)> name( \
5550 GMOCK_INTERNAL_MATCHER_FUNCTION_ARGS(args)) { \
5551 return full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>( \
5552 GMOCK_INTERNAL_MATCHER_ARGS_USAGE(args)); \
5554 template <GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args)> \
5555 template <typename arg_type> \
5556 bool full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>::gmock_Impl< \
5557 arg_type>::MatchAndExplain(const arg_type& arg, \
5558 ::testing::MatchResultListener* \
5559 result_listener GTEST_ATTRIBUTE_UNUSED_) \
5562#define GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args) \
5564 GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAM, , args))
5565#define GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAM(i_unused, data_unused, arg) \
5566 , typename arg##_type
5568#define GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args) \
5569 GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_TYPE_PARAM, , args))
5570#define GMOCK_INTERNAL_MATCHER_TYPE_PARAM(i_unused, data_unused, arg) \
5573#define GMOCK_INTERNAL_MATCHER_FUNCTION_ARGS(args) \
5574 GMOCK_PP_TAIL(dummy_first GMOCK_PP_FOR_EACH( \
5575 GMOCK_INTERNAL_MATCHER_FUNCTION_ARG, , args))
5576#define GMOCK_INTERNAL_MATCHER_FUNCTION_ARG(i, data_unused, arg) \
5577 , arg##_type gmock_p##i
5579#define GMOCK_INTERNAL_MATCHER_FORWARD_ARGS(args) \
5580 GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_FORWARD_ARG, , args))
5581#define GMOCK_INTERNAL_MATCHER_FORWARD_ARG(i, data_unused, arg) \
5582 , arg(::std::forward<arg##_type>(gmock_p##i))
5584#define GMOCK_INTERNAL_MATCHER_MEMBERS(args) \
5585 GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_MEMBER, , args)
5586#define GMOCK_INTERNAL_MATCHER_MEMBER(i_unused, data_unused, arg) \
5587 const arg##_type arg;
5589#define GMOCK_INTERNAL_MATCHER_MEMBERS_USAGE(args) \
5590 GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_MEMBER_USAGE, , args))
5591#define GMOCK_INTERNAL_MATCHER_MEMBER_USAGE(i_unused, data_unused, arg) , arg
5593#define GMOCK_INTERNAL_MATCHER_ARGS_USAGE(args) \
5594 GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_ARG_USAGE, , args))
5595#define GMOCK_INTERNAL_MATCHER_ARG_USAGE(i, data_unused, arg_unused) \
5599using namespace no_adl;