306class StringMatchResultListener :
public MatchResultListener {
308 StringMatchResultListener() : MatchResultListener(&ss_) {}
311 std::string str()
const {
return ss_.str(); }
314 void Clear() { ss_.str(
""); }
317 ::std::stringstream ss_;
319 StringMatchResultListener(
const StringMatchResultListener&) =
delete;
320 StringMatchResultListener& operator=(
const StringMatchResultListener&) =
338template <
typename T,
typename M>
339class MatcherCastImpl {
341 static Matcher<T> Cast(
const M& polymorphic_matcher_or_value) {
355 return CastImpl(polymorphic_matcher_or_value,
356 std::is_convertible<M, Matcher<T>>{},
357 std::is_convertible<M, T>{});
361 template <
bool Ignore>
362 static Matcher<T> CastImpl(
const M& polymorphic_matcher_or_value,
364 std::integral_constant<bool, Ignore>) {
373 return polymorphic_matcher_or_value;
379 static Matcher<T> CastImpl(
const M&
value,
382 return Matcher<T>(ImplicitCast_<T>(
value));
395 static Matcher<T> CastImpl(
const M&
value,
403template <
typename T,
typename U>
404class MatcherCastImpl<
T, Matcher<U>> {
406 static Matcher<T> Cast(
const Matcher<U>& source_matcher) {
407 return Matcher<T>(
new Impl(source_matcher));
411 class Impl :
public MatcherInterface<T> {
413 explicit Impl(
const Matcher<U>& source_matcher)
414 : source_matcher_(source_matcher) {}
417 bool MatchAndExplain(
T x, MatchResultListener* listener)
const override {
418 using FromType =
typename std::remove_cv<
typename std::remove_pointer<
419 typename std::remove_reference<T>::type>::type>::type;
420 using ToType =
typename std::remove_cv<
typename std::remove_pointer<
421 typename std::remove_reference<U>::type>::type>::type;
426 (std::is_pointer<typename std::remove_reference<T>::type>
::value !=
427 std::is_pointer<typename std::remove_reference<U>::type>
::value) ||
428 std::is_same<FromType, ToType>::value ||
429 !std::is_base_of<FromType, ToType>::value,
430 "Can't implicitly convert from <base> to <derived>");
435 typename std::conditional<std::is_convertible<T&, const U&>::value,
438 return source_matcher_.MatchAndExplain(
static_cast<CastType
>(
x),
442 void DescribeTo(::std::ostream* os)
const override {
443 source_matcher_.DescribeTo(os);
446 void DescribeNegationTo(::std::ostream* os)
const override {
447 source_matcher_.DescribeNegationTo(os);
451 const Matcher<U> source_matcher_;
458class MatcherCastImpl<
T, Matcher<
T>> {
460 static Matcher<T> Cast(
const Matcher<T>& matcher) {
return matcher; }
464template <
typename Derived>
465class MatcherBaseImpl {
467 MatcherBaseImpl() =
default;
469 template <
typename T>
470 operator ::testing::Matcher<T>()
const {
471 return ::testing::Matcher<T>(
new
472 typename Derived::template gmock_Impl<T>());
477template <
template <
typename...>
class Derived,
typename... Ts>
478class MatcherBaseImpl<Derived<Ts...>> {
482 template <
typename E = std::enable_if<
sizeof...(Ts) == 1>,
483 typename E::type* =
nullptr>
484 explicit MatcherBaseImpl(Ts... params)
485 : params_(std::forward<Ts>(params)...) {}
486 template <
typename E = std::enable_if<
sizeof...(Ts) != 1>,
487 typename =
typename E::type>
488 MatcherBaseImpl(Ts... params)
489 : params_(std::forward<Ts>(params)...) {}
491 template <
typename F>
492 operator ::testing::Matcher<F>()
const {
493 return Apply<F>(MakeIndexSequence<
sizeof...(Ts)>{});
497 template <
typename F, std::size_t... tuple_ids>
499 return ::testing::Matcher<F>(
500 new typename Derived<Ts...>::template gmock_Impl<F>(
501 std::get<tuple_ids>(params_)...));
504 const std::tuple<Ts...> params_;
513template <
typename T,
typename M>
514inline Matcher<T> MatcherCast(
const M& matcher) {
515 return internal::MatcherCastImpl<T, M>::Cast(matcher);
520template <
typename T,
typename M>
521inline Matcher<T> SafeMatcherCast(
const M& polymorphic_matcher_or_value) {
522 return MatcherCast<T>(polymorphic_matcher_or_value);
534template <
typename T,
typename U>
535inline Matcher<T> SafeMatcherCast(
const Matcher<U>& matcher) {
537 static_assert(std::is_convertible<const T&, const U&>::value,
538 "T must be implicitly convertible to U");
541 static_assert(std::is_reference<T>::value || !std::is_reference<U>::value,
542 "cannot convert non reference arg to reference");
550 kTIsOther || kUIsOther ||
551 (internal::LosslessArithmeticConvertible<RawT, RawU>::value),
552 "conversion of arithmetic types must be lossless");
553 return MatcherCast<T>(matcher);
565inline void PrintIfNotEmpty(
const std::string& explanation,
566 ::std::ostream* os) {
567 if (!explanation.empty() && os !=
nullptr) {
568 *os <<
", " << explanation;
575inline bool IsReadableTypeName(
const std::string& type_name) {
578 return (type_name.length() <= 20 ||
579 type_name.find_first_of(
"<(") == std::string::npos);
587template <
typename Value,
typename T>
588bool MatchPrintAndExplain(Value&
value,
const Matcher<T>& matcher,
589 MatchResultListener* listener) {
590 if (!listener->IsInterested()) {
593 return matcher.Matches(
value);
596 StringMatchResultListener inner_listener;
597 const bool match = matcher.MatchAndExplain(
value, &inner_listener);
602 if (IsReadableTypeName(type_name))
603 *listener->stream() <<
" (of type " << type_name <<
")";
605 PrintIfNotEmpty(inner_listener.str(), listener->stream());
618 template <
typename MatcherTuple,
typename ValueTuple>
619 static bool Matches(
const MatcherTuple& matcher_tuple,
620 const ValueTuple& value_tuple) {
621 return TuplePrefix<N - 1>::Matches(matcher_tuple, value_tuple) &&
622 std::get<N - 1>(matcher_tuple).Matches(std::get<N - 1>(value_tuple));
629 template <
typename MatcherTuple,
typename ValueTuple>
630 static void ExplainMatchFailuresTo(
const MatcherTuple& matchers,
631 const ValueTuple& values,
632 ::std::ostream* os) {
634 TuplePrefix<N - 1>::ExplainMatchFailuresTo(matchers, values, os);
638 typename std::tuple_element<N - 1, MatcherTuple>::type matcher =
639 std::get<N - 1>(matchers);
640 typedef typename std::tuple_element<N - 1, ValueTuple>::type Value;
641 const Value&
value = std::get<N - 1>(values);
642 StringMatchResultListener listener;
643 if (!matcher.MatchAndExplain(
value, &listener)) {
644 *os <<
" Expected arg #" << N - 1 <<
": ";
645 std::get<N - 1>(matchers).DescribeTo(os);
646 *os <<
"\n Actual: ";
652 internal::UniversalPrint(
value, os);
653 PrintIfNotEmpty(listener.str(), os);
661class TuplePrefix<0> {
663 template <
typename MatcherTuple,
typename ValueTuple>
664 static bool Matches(
const MatcherTuple& ,
665 const ValueTuple& ) {
669 template <
typename MatcherTuple,
typename ValueTuple>
670 static void ExplainMatchFailuresTo(
const MatcherTuple& ,
680template <
typename MatcherTuple,
typename ValueTuple>
681bool TupleMatches(
const MatcherTuple& matcher_tuple,
682 const ValueTuple& value_tuple) {
685 static_assert(std::tuple_size<MatcherTuple>::value ==
686 std::tuple_size<ValueTuple>::value,
687 "matcher and value have different numbers of fields");
688 return TuplePrefix<std::tuple_size<ValueTuple>::value>::Matches(matcher_tuple,
694template <
typename MatcherTuple,
typename ValueTuple>
695void ExplainMatchFailureTupleTo(
const MatcherTuple& matchers,
696 const ValueTuple& values, ::std::ostream* os) {
697 TuplePrefix<std::tuple_size<MatcherTuple>::value>::ExplainMatchFailuresTo(
698 matchers, values, os);
705template <
typename Tuple,
typename Func,
typename OutIter>
706class TransformTupleValuesHelper {
708 typedef ::std::tuple_size<Tuple> TupleSize;
713 static OutIter Run(Func f,
const Tuple& t, OutIter out) {
714 return IterateOverTuple<Tuple, TupleSize::value>()(f, t, out);
718 template <
typename Tup,
size_t kRemainingSize>
719 struct IterateOverTuple {
720 OutIter operator()(Func f,
const Tup& t, OutIter out)
const {
721 *out++ = f(::std::get<TupleSize::value - kRemainingSize>(t));
722 return IterateOverTuple<Tup, kRemainingSize - 1>()(f, t, out);
725 template <
typename Tup>
726 struct IterateOverTuple<Tup, 0> {
727 OutIter operator()(Func ,
const Tup& , OutIter out)
const {
736template <
typename Tuple,
typename Func,
typename OutIter>
737OutIter TransformTupleValues(Func f,
const Tuple& t, OutIter out) {
738 return TransformTupleValuesHelper<Tuple, Func, OutIter>::Run(f, t, out);
745class AnythingMatcher {
747 using is_gtest_matcher = void;
749 template <
typename T>
750 bool MatchAndExplain(
const T& , std::ostream* )
const {
753 void DescribeTo(std::ostream* os)
const { *os <<
"is anything"; }
754 void DescribeNegationTo(::std::ostream* os)
const {
758 *os <<
"never matches";
766 template <
typename Po
inter>
767 bool MatchAndExplain(
const Pointer&
p,
768 MatchResultListener* )
const {
772 void DescribeTo(::std::ostream* os)
const { *os <<
"is NULL"; }
773 void DescribeNegationTo(::std::ostream* os)
const { *os <<
"isn't NULL"; }
778class NotNullMatcher {
780 template <
typename Po
inter>
781 bool MatchAndExplain(
const Pointer&
p,
782 MatchResultListener* )
const {
786 void DescribeTo(::std::ostream* os)
const { *os <<
"isn't NULL"; }
787 void DescribeNegationTo(::std::ostream* os)
const { *os <<
"is NULL"; }
807class RefMatcher<
T&> {
817 explicit RefMatcher(
T&
x) : object_(
x) {}
819 template <
typename Super>
820 operator Matcher<Super&>()
const {
826 return MakeMatcher(
new Impl<Super>(object_));
830 template <
typename Super>
831 class Impl :
public MatcherInterface<Super&> {
833 explicit Impl(Super&
x) : object_(
x) {}
837 bool MatchAndExplain(Super&
x,
838 MatchResultListener* listener)
const override {
839 *listener <<
"which is located @" <<
static_cast<const void*
>(&
x);
840 return &
x == &object_;
843 void DescribeTo(::std::ostream* os)
const override {
844 *os <<
"references the variable ";
845 UniversalPrinter<Super&>::Print(object_, os);
848 void DescribeNegationTo(::std::ostream* os)
const override {
849 *os <<
"does not reference the variable ";
850 UniversalPrinter<Super&>::Print(object_, os);
854 const Super& object_;
861inline bool CaseInsensitiveCStringEquals(
const char* lhs,
const char* rhs) {
865inline bool CaseInsensitiveCStringEquals(
const wchar_t* lhs,
866 const wchar_t* rhs) {
872template <
typename StringType>
873bool CaseInsensitiveStringEquals(
const StringType& s1,
const StringType& s2) {
875 if (!CaseInsensitiveCStringEquals(s1.c_str(), s2.c_str())) {
880 const typename StringType::value_type nul = 0;
881 const size_t i1 = s1.find(nul), i2 = s2.find(nul);
884 if (i1 == StringType::npos || i2 == StringType::npos) {
889 return CaseInsensitiveStringEquals(s1.substr(i1 + 1), s2.substr(i2 + 1));
895template <
typename StringType>
896class StrEqualityMatcher {
898 StrEqualityMatcher(StringType str,
bool expect_eq,
bool case_sensitive)
899 : string_(std::move(str)),
900 expect_eq_(expect_eq),
901 case_sensitive_(case_sensitive) {}
903#if GTEST_INTERNAL_HAS_STRING_VIEW
904 bool MatchAndExplain(
const internal::StringView& s,
905 MatchResultListener* listener)
const {
908 const StringType& str = std::string(s);
909 return MatchAndExplain(str, listener);
918 template <
typename CharType>
919 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
923 return MatchAndExplain(StringType(s), listener);
930 template <
typename MatcheeStringType>
931 bool MatchAndExplain(
const MatcheeStringType& s,
932 MatchResultListener* )
const {
933 const StringType s2(s);
934 const bool eq = case_sensitive_ ? s2 == string_
935 : CaseInsensitiveStringEquals(s2, string_);
936 return expect_eq_ == eq;
939 void DescribeTo(::std::ostream* os)
const {
940 DescribeToHelper(expect_eq_, os);
943 void DescribeNegationTo(::std::ostream* os)
const {
944 DescribeToHelper(!expect_eq_, os);
948 void DescribeToHelper(
bool expect_eq, ::std::ostream* os)
const {
949 *os << (expect_eq ?
"is " :
"isn't ");
951 if (!case_sensitive_) {
952 *os <<
"(ignoring case) ";
954 UniversalPrint(string_, os);
957 const StringType string_;
958 const bool expect_eq_;
959 const bool case_sensitive_;
965template <
typename StringType>
966class HasSubstrMatcher {
968 explicit HasSubstrMatcher(
const StringType& substring)
969 : substring_(substring) {}
971#if GTEST_INTERNAL_HAS_STRING_VIEW
972 bool MatchAndExplain(
const internal::StringView& s,
973 MatchResultListener* listener)
const {
976 const StringType& str = std::string(s);
977 return MatchAndExplain(str, listener);
986 template <
typename CharType>
987 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
988 return s !=
nullptr && MatchAndExplain(StringType(s), listener);
995 template <
typename MatcheeStringType>
996 bool MatchAndExplain(
const MatcheeStringType& s,
997 MatchResultListener* )
const {
998 return StringType(s).find(substring_) != StringType::npos;
1002 void DescribeTo(::std::ostream* os)
const {
1003 *os <<
"has substring ";
1004 UniversalPrint(substring_, os);
1007 void DescribeNegationTo(::std::ostream* os)
const {
1008 *os <<
"has no substring ";
1009 UniversalPrint(substring_, os);
1013 const StringType substring_;
1019template <
typename StringType>
1020class StartsWithMatcher {
1022 explicit StartsWithMatcher(
const StringType& prefix) : prefix_(prefix) {}
1024#if GTEST_INTERNAL_HAS_STRING_VIEW
1025 bool MatchAndExplain(
const internal::StringView& s,
1026 MatchResultListener* listener)
const {
1029 const StringType& str = std::string(s);
1030 return MatchAndExplain(str, listener);
1039 template <
typename CharType>
1040 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
1041 return s !=
nullptr && MatchAndExplain(StringType(s), listener);
1048 template <
typename MatcheeStringType>
1049 bool MatchAndExplain(
const MatcheeStringType& s,
1050 MatchResultListener* )
const {
1051 const StringType& s2(s);
1052 return s2.length() >= prefix_.length() &&
1053 s2.substr(0, prefix_.length()) == prefix_;
1056 void DescribeTo(::std::ostream* os)
const {
1057 *os <<
"starts with ";
1058 UniversalPrint(prefix_, os);
1061 void DescribeNegationTo(::std::ostream* os)
const {
1062 *os <<
"doesn't start with ";
1063 UniversalPrint(prefix_, os);
1067 const StringType prefix_;
1073template <
typename StringType>
1074class EndsWithMatcher {
1076 explicit EndsWithMatcher(
const StringType& suffix) : suffix_(suffix) {}
1078#if GTEST_INTERNAL_HAS_STRING_VIEW
1079 bool MatchAndExplain(
const internal::StringView& s,
1080 MatchResultListener* listener)
const {
1083 const StringType& str = std::string(s);
1084 return MatchAndExplain(str, listener);
1093 template <
typename CharType>
1094 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
1095 return s !=
nullptr && MatchAndExplain(StringType(s), listener);
1102 template <
typename MatcheeStringType>
1103 bool MatchAndExplain(
const MatcheeStringType& s,
1104 MatchResultListener* )
const {
1105 const StringType& s2(s);
1106 return s2.length() >= suffix_.length() &&
1107 s2.substr(s2.length() - suffix_.length()) == suffix_;
1110 void DescribeTo(::std::ostream* os)
const {
1111 *os <<
"ends with ";
1112 UniversalPrint(suffix_, os);
1115 void DescribeNegationTo(::std::ostream* os)
const {
1116 *os <<
"doesn't end with ";
1117 UniversalPrint(suffix_, os);
1121 const StringType suffix_;
1126class WhenBase64UnescapedMatcher {
1128 using is_gtest_matcher = void;
1130 explicit WhenBase64UnescapedMatcher(
1131 const Matcher<const std::string&>& internal_matcher)
1132 : internal_matcher_(internal_matcher) {}
1135 template <
typename MatcheeStringType>
1136 bool MatchAndExplain(
const MatcheeStringType& s,
1137 MatchResultListener* listener)
const {
1138 const std::string s2(s);
1139 std::string unescaped;
1140 if (!internal::Base64Unescape(s2, &unescaped)) {
1141 if (listener !=
nullptr) {
1142 *listener <<
"is not a valid base64 escaped string";
1146 return MatchPrintAndExplain(unescaped, internal_matcher_, listener);
1149 void DescribeTo(::std::ostream* os)
const {
1150 *os <<
"matches after Base64Unescape ";
1151 internal_matcher_.DescribeTo(os);
1154 void DescribeNegationTo(::std::ostream* os)
const {
1155 *os <<
"does not match after Base64Unescape ";
1156 internal_matcher_.DescribeTo(os);
1160 const Matcher<const std::string&> internal_matcher_;
1171template <
typename D,
typename Op>
1172class PairMatchBase {
1174 template <
typename T1,
typename T2>
1175 operator Matcher<::std::tuple<T1, T2>>()
const {
1176 return Matcher<::std::tuple<T1, T2>>(
new Impl<const ::std::tuple<T1, T2>&>);
1178 template <
typename T1,
typename T2>
1179 operator Matcher<const ::std::tuple<T1, T2>&>()
const {
1180 return MakeMatcher(
new Impl<const ::std::tuple<T1, T2>&>);
1184 static ::std::ostream& GetDesc(::std::ostream& os) {
1185 return os << D::Desc();
1188 template <
typename Tuple>
1189 class Impl :
public MatcherInterface<Tuple> {
1191 bool MatchAndExplain(Tuple args,
1192 MatchResultListener* )
const override {
1193 return Op()(::std::get<0>(args), ::std::get<1>(args));
1195 void DescribeTo(::std::ostream* os)
const override {
1196 *os <<
"are " << GetDesc;
1198 void DescribeNegationTo(::std::ostream* os)
const override {
1199 *os <<
"aren't " << GetDesc;
1204class Eq2Matcher :
public PairMatchBase<Eq2Matcher, std::equal_to<>> {
1206 static const char* Desc() {
return "an equal pair"; }
1208class Ne2Matcher :
public PairMatchBase<Ne2Matcher, std::not_equal_to<>> {
1210 static const char* Desc() {
return "an unequal pair"; }
1212class Lt2Matcher :
public PairMatchBase<Lt2Matcher, std::less<>> {
1214 static const char* Desc() {
return "a pair where the first < the second"; }
1216class Gt2Matcher :
public PairMatchBase<Gt2Matcher, std::greater<>> {
1218 static const char* Desc() {
return "a pair where the first > the second"; }
1220class Le2Matcher :
public PairMatchBase<Le2Matcher, std::less_equal<>> {
1222 static const char* Desc() {
return "a pair where the first <= the second"; }
1224class Ge2Matcher :
public PairMatchBase<Ge2Matcher, std::greater_equal<>> {
1226 static const char* Desc() {
return "a pair where the first >= the second"; }
1233template <
typename T>
1234class NotMatcherImpl :
public MatcherInterface<const T&> {
1236 explicit NotMatcherImpl(
const Matcher<T>& matcher) : matcher_(matcher) {}
1238 bool MatchAndExplain(
const T&
x,
1239 MatchResultListener* listener)
const override {
1240 return !matcher_.MatchAndExplain(
x, listener);
1243 void DescribeTo(::std::ostream* os)
const override {
1244 matcher_.DescribeNegationTo(os);
1247 void DescribeNegationTo(::std::ostream* os)
const override {
1248 matcher_.DescribeTo(os);
1252 const Matcher<T> matcher_;
1257template <
typename InnerMatcher>
1260 explicit NotMatcher(InnerMatcher matcher) : matcher_(matcher) {}
1264 template <
typename T>
1265 operator Matcher<T>()
const {
1266 return Matcher<T>(
new NotMatcherImpl<T>(SafeMatcherCast<T>(matcher_)));
1270 InnerMatcher matcher_;
1277template <
typename T>
1278class AllOfMatcherImpl :
public MatcherInterface<const T&> {
1280 explicit AllOfMatcherImpl(std::vector<Matcher<T>> matchers)
1281 : matchers_(std::move(matchers)) {}
1283 void DescribeTo(::std::ostream* os)
const override {
1285 for (
size_t i = 0;
i < matchers_.size(); ++
i) {
1286 if (
i != 0) *os <<
") and (";
1287 matchers_[
i].DescribeTo(os);
1292 void DescribeNegationTo(::std::ostream* os)
const override {
1294 for (
size_t i = 0;
i < matchers_.size(); ++
i) {
1295 if (
i != 0) *os <<
") or (";
1296 matchers_[
i].DescribeNegationTo(os);
1301 bool MatchAndExplain(
const T&
x,
1302 MatchResultListener* listener)
const override {
1305 std::string all_match_result;
1307 for (
size_t i = 0;
i < matchers_.size(); ++
i) {
1308 StringMatchResultListener slistener;
1309 if (matchers_[
i].MatchAndExplain(
x, &slistener)) {
1310 if (all_match_result.empty()) {
1311 all_match_result = slistener.str();
1313 std::string result = slistener.str();
1314 if (!result.empty()) {
1315 all_match_result +=
", and ";
1316 all_match_result += result;
1320 *listener << slistener.str();
1326 *listener << all_match_result;
1331 const std::vector<Matcher<T>> matchers_;
1338template <
template <
typename T>
class CombiningMatcher,
typename... Args>
1339class VariadicMatcher {
1341 VariadicMatcher(
const Args&... matchers)
1342 : matchers_(matchers...) {
1343 static_assert(
sizeof...(Args) > 0,
"Must have at least one matcher.");
1346 VariadicMatcher(
const VariadicMatcher&) =
default;
1347 VariadicMatcher& operator=(
const VariadicMatcher&) =
delete;
1352 template <
typename T>
1353 operator Matcher<T>()
const {
1354 std::vector<Matcher<T>> values;
1355 CreateVariadicMatcher<T>(&values, std::integral_constant<size_t, 0>());
1356 return Matcher<T>(
new CombiningMatcher<T>(std::move(values)));
1360 template <
typename T,
size_t I>
1361 void CreateVariadicMatcher(std::vector<Matcher<T>>* values,
1362 std::integral_constant<size_t, I>)
const {
1363 values->push_back(SafeMatcherCast<T>(std::get<I>(matchers_)));
1364 CreateVariadicMatcher<T>(values, std::integral_constant<size_t, I + 1>());
1367 template <
typename T>
1368 void CreateVariadicMatcher(
1369 std::vector<Matcher<T>>*,
1370 std::integral_constant<
size_t,
sizeof...(Args)>)
const {}
1372 std::tuple<Args...> matchers_;
1375template <
typename... Args>
1376using AllOfMatcher = VariadicMatcher<AllOfMatcherImpl, Args...>;
1382template <
typename T>
1383class AnyOfMatcherImpl :
public MatcherInterface<const T&> {
1385 explicit AnyOfMatcherImpl(std::vector<Matcher<T>> matchers)
1386 : matchers_(std::move(matchers)) {}
1388 void DescribeTo(::std::ostream* os)
const override {
1390 for (
size_t i = 0;
i < matchers_.size(); ++
i) {
1391 if (
i != 0) *os <<
") or (";
1392 matchers_[
i].DescribeTo(os);
1397 void DescribeNegationTo(::std::ostream* os)
const override {
1399 for (
size_t i = 0;
i < matchers_.size(); ++
i) {
1400 if (
i != 0) *os <<
") and (";
1401 matchers_[
i].DescribeNegationTo(os);
1406 bool MatchAndExplain(
const T&
x,
1407 MatchResultListener* listener)
const override {
1408 std::string no_match_result;
1412 for (
size_t i = 0;
i < matchers_.size(); ++
i) {
1413 StringMatchResultListener slistener;
1414 if (matchers_[
i].MatchAndExplain(
x, &slistener)) {
1415 *listener << slistener.str();
1418 if (no_match_result.empty()) {
1419 no_match_result = slistener.str();
1421 std::string result = slistener.str();
1422 if (!result.empty()) {
1423 no_match_result +=
", and ";
1424 no_match_result += result;
1431 *listener << no_match_result;
1436 const std::vector<Matcher<T>> matchers_;
1440template <
typename... Args>
1441using AnyOfMatcher = VariadicMatcher<AnyOfMatcherImpl, Args...>;
1444template <
typename MatcherTrue,
typename MatcherFalse>
1445class ConditionalMatcher {
1447 ConditionalMatcher(
bool condition, MatcherTrue matcher_true,
1448 MatcherFalse matcher_false)
1449 : condition_(condition),
1450 matcher_true_(std::move(matcher_true)),
1451 matcher_false_(std::move(matcher_false)) {}
1453 template <
typename T>
1454 operator Matcher<T>()
const {
1455 return condition_ ? SafeMatcherCast<T>(matcher_true_)
1456 : SafeMatcherCast<T>(matcher_false_);
1461 MatcherTrue matcher_true_;
1462 MatcherFalse matcher_false_;
1466template <
template <
class>
class MatcherImpl,
typename T>
1467class SomeOfArrayMatcher {
1471 template <
typename Iter>
1472 SomeOfArrayMatcher(Iter first, Iter last) : matchers_(first, last) {}
1474 template <
typename U>
1475 operator Matcher<U>()
const {
1476 using RawU =
typename std::decay<U>::type;
1477 std::vector<Matcher<RawU>> matchers;
1478 matchers.reserve(matchers_.size());
1479 for (
const auto& matcher : matchers_) {
1480 matchers.push_back(MatcherCast<RawU>(matcher));
1482 return Matcher<U>(
new MatcherImpl<RawU>(std::move(matchers)));
1486 const ::std::vector<T> matchers_;
1489template <
typename T>
1490using AllOfArrayMatcher = SomeOfArrayMatcher<AllOfMatcherImpl, T>;
1492template <
typename T>
1493using AnyOfArrayMatcher = SomeOfArrayMatcher<AnyOfMatcherImpl, T>;
1497template <
typename Predicate>
1500 explicit TrulyMatcher(Predicate pred) : predicate_(pred) {}
1506 template <
typename T>
1507 bool MatchAndExplain(
T&
x,
1508 MatchResultListener* listener)
const {
1515 if (predicate_(
x))
return true;
1516 *listener <<
"didn't satisfy the given predicate";
1520 void DescribeTo(::std::ostream* os)
const {
1521 *os <<
"satisfies the given predicate";
1524 void DescribeNegationTo(::std::ostream* os)
const {
1525 *os <<
"doesn't satisfy the given predicate";
1529 Predicate predicate_;
1534template <
typename M>
1535class MatcherAsPredicate {
1537 explicit MatcherAsPredicate(M matcher) : matcher_(matcher) {}
1545 template <
typename T>
1546 bool operator()(
const T&
x)
const {
1561 return MatcherCast<const T&>(matcher_).Matches(
x);
1570template <
typename M>
1571class PredicateFormatterFromMatcher {
1573 explicit PredicateFormatterFromMatcher(M m) : matcher_(std::move(m)) {}
1578 template <
typename T>
1579 AssertionResult operator()(
const char* value_text,
const T&
x)
const {
1591 const Matcher<const T&> matcher = SafeMatcherCast<const T&>(matcher_);
1595 if (matcher.Matches(
x)) {
1596 return AssertionSuccess();
1599 ::std::stringstream ss;
1600 ss <<
"Value of: " << value_text <<
"\n"
1602 matcher.DescribeTo(&ss);
1605 StringMatchResultListener listener;
1606 if (MatchPrintAndExplain(
x, matcher, &listener)) {
1607 ss <<
"\n The matcher failed on the initial attempt; but passed when "
1608 "rerun to generate the explanation.";
1610 ss <<
"\n Actual: " << listener.str();
1611 return AssertionFailure() << ss.str();
1622template <
typename M>
1623inline PredicateFormatterFromMatcher<M> MakePredicateFormatterFromMatcher(
1625 return PredicateFormatterFromMatcher<M>(std::move(matcher));
1632 template <
typename FloatType>
1633 bool MatchAndExplain(
const FloatType& f,
1634 MatchResultListener* )
const {
1635 return (::std::isnan)(f);
1638 void DescribeTo(::std::ostream* os)
const { *os <<
"is NaN"; }
1639 void DescribeNegationTo(::std::ostream* os)
const { *os <<
"isn't NaN"; }
1646template <
typename FloatType>
1647class FloatingEqMatcher {
1655 FloatingEqMatcher(FloatType expected,
bool nan_eq_nan)
1656 : expected_(expected), nan_eq_nan_(nan_eq_nan), max_abs_error_(-1) {}
1661 FloatingEqMatcher(FloatType expected,
bool nan_eq_nan,
1662 FloatType max_abs_error)
1663 : expected_(expected),
1664 nan_eq_nan_(nan_eq_nan),
1665 max_abs_error_(max_abs_error) {
1667 <<
", where max_abs_error is" << max_abs_error;
1671 template <
typename T>
1672 class Impl :
public MatcherInterface<T> {
1674 Impl(FloatType expected,
bool nan_eq_nan, FloatType max_abs_error)
1675 : expected_(expected),
1676 nan_eq_nan_(nan_eq_nan),
1677 max_abs_error_(max_abs_error) {}
1679 bool MatchAndExplain(
T value,
1680 MatchResultListener* listener)
const override {
1681 const FloatingPoint<FloatType> actual(
value), expected(expected_);
1684 if (actual.is_nan() || expected.is_nan()) {
1685 if (actual.is_nan() && expected.is_nan()) {
1691 if (HasMaxAbsError()) {
1696 if (
value == expected_) {
1700 const FloatType diff =
value - expected_;
1701 if (::std::fabs(diff) <= max_abs_error_) {
1705 if (listener->IsInterested()) {
1706 *listener <<
"which is " << diff <<
" from " << expected_;
1710 return actual.AlmostEquals(expected);
1714 void DescribeTo(::std::ostream* os)
const override {
1718 const ::std::streamsize old_precision =
1719 os->precision(::std::numeric_limits<FloatType>::digits10 + 2);
1720 if (FloatingPoint<FloatType>(expected_).is_nan()) {
1724 *os <<
"never matches";
1727 *os <<
"is approximately " << expected_;
1728 if (HasMaxAbsError()) {
1729 *os <<
" (absolute error <= " << max_abs_error_ <<
")";
1732 os->precision(old_precision);
1735 void DescribeNegationTo(::std::ostream* os)
const override {
1737 const ::std::streamsize old_precision =
1738 os->precision(::std::numeric_limits<FloatType>::digits10 + 2);
1739 if (FloatingPoint<FloatType>(expected_).is_nan()) {
1743 *os <<
"is anything";
1746 *os <<
"isn't approximately " << expected_;
1747 if (HasMaxAbsError()) {
1748 *os <<
" (absolute error > " << max_abs_error_ <<
")";
1752 os->precision(old_precision);
1756 bool HasMaxAbsError()
const {
return max_abs_error_ >= 0; }
1758 const FloatType expected_;
1759 const bool nan_eq_nan_;
1761 const FloatType max_abs_error_;
1767 operator Matcher<FloatType>()
const {
1769 new Impl<FloatType>(expected_, nan_eq_nan_, max_abs_error_));
1772 operator Matcher<const FloatType&>()
const {
1774 new Impl<const FloatType&>(expected_, nan_eq_nan_, max_abs_error_));
1777 operator Matcher<FloatType&>()
const {
1779 new Impl<FloatType&>(expected_, nan_eq_nan_, max_abs_error_));
1783 const FloatType expected_;
1784 const bool nan_eq_nan_;
1786 const FloatType max_abs_error_;
1794template <
typename FloatType>
1795class FloatingEq2Matcher {
1797 FloatingEq2Matcher() { Init(-1,
false); }
1799 explicit FloatingEq2Matcher(
bool nan_eq_nan) { Init(-1, nan_eq_nan); }
1801 explicit FloatingEq2Matcher(FloatType max_abs_error) {
1802 Init(max_abs_error,
false);
1805 FloatingEq2Matcher(FloatType max_abs_error,
bool nan_eq_nan) {
1806 Init(max_abs_error, nan_eq_nan);
1809 template <
typename T1,
typename T2>
1810 operator Matcher<::std::tuple<T1, T2>>()
const {
1812 new Impl<::std::tuple<T1, T2>>(max_abs_error_, nan_eq_nan_));
1814 template <
typename T1,
typename T2>
1815 operator Matcher<const ::std::tuple<T1, T2>&>()
const {
1817 new Impl<const ::std::tuple<T1, T2>&>(max_abs_error_, nan_eq_nan_));
1821 static ::std::ostream& GetDesc(::std::ostream& os) {
1822 return os <<
"an almost-equal pair";
1825 template <
typename Tuple>
1826 class Impl :
public MatcherInterface<Tuple> {
1828 Impl(FloatType max_abs_error,
bool nan_eq_nan)
1829 : max_abs_error_(max_abs_error), nan_eq_nan_(nan_eq_nan) {}
1831 bool MatchAndExplain(Tuple args,
1832 MatchResultListener* listener)
const override {
1833 if (max_abs_error_ == -1) {
1834 FloatingEqMatcher<FloatType> fm(::std::get<0>(args), nan_eq_nan_);
1835 return static_cast<Matcher<FloatType>
>(fm).MatchAndExplain(
1836 ::std::get<1>(args), listener);
1838 FloatingEqMatcher<FloatType> fm(::std::get<0>(args), nan_eq_nan_,
1840 return static_cast<Matcher<FloatType>
>(fm).MatchAndExplain(
1841 ::std::get<1>(args), listener);
1844 void DescribeTo(::std::ostream* os)
const override {
1845 *os <<
"are " << GetDesc;
1847 void DescribeNegationTo(::std::ostream* os)
const override {
1848 *os <<
"aren't " << GetDesc;
1852 FloatType max_abs_error_;
1853 const bool nan_eq_nan_;
1856 void Init(FloatType max_abs_error_val,
bool nan_eq_nan_val) {
1857 max_abs_error_ = max_abs_error_val;
1858 nan_eq_nan_ = nan_eq_nan_val;
1860 FloatType max_abs_error_;
1866template <
typename InnerMatcher>
1867class PointeeMatcher {
1869 explicit PointeeMatcher(
const InnerMatcher& matcher) : matcher_(matcher) {}
1879 template <
typename Po
inter>
1880 operator Matcher<Pointer>()
const {
1881 return Matcher<Pointer>(
new Impl<const Pointer&>(matcher_));
1886 template <
typename Po
inter>
1887 class Impl :
public MatcherInterface<Pointer> {
1891 Pointer)>::element_type;
1893 explicit Impl(
const InnerMatcher& matcher)
1894 : matcher_(MatcherCast<const Pointee&>(matcher)) {}
1896 void DescribeTo(::std::ostream* os)
const override {
1897 *os <<
"points to a value that ";
1898 matcher_.DescribeTo(os);
1901 void DescribeNegationTo(::std::ostream* os)
const override {
1902 *os <<
"does not point to a value that ";
1903 matcher_.DescribeTo(os);
1906 bool MatchAndExplain(Pointer pointer,
1907 MatchResultListener* listener)
const override {
1908 if (GetRawPointer(pointer) ==
nullptr)
return false;
1910 *listener <<
"which points to ";
1911 return MatchPrintAndExplain(*pointer, matcher_, listener);
1915 const Matcher<const Pointee&> matcher_;
1918 const InnerMatcher matcher_;
1925template <
typename InnerMatcher>
1926class PointerMatcher {
1928 explicit PointerMatcher(
const InnerMatcher& matcher) : matcher_(matcher) {}
1938 template <
typename Po
interType>
1939 operator Matcher<PointerType>()
const {
1940 return Matcher<PointerType>(
new Impl<const PointerType&>(matcher_));
1945 template <
typename Po
interType>
1946 class Impl :
public MatcherInterface<PointerType> {
1950 PointerType)>::element_type*;
1952 explicit Impl(
const InnerMatcher& matcher)
1953 : matcher_(MatcherCast<Pointer>(matcher)) {}
1955 void DescribeTo(::std::ostream* os)
const override {
1956 *os <<
"is a pointer that ";
1957 matcher_.DescribeTo(os);
1960 void DescribeNegationTo(::std::ostream* os)
const override {
1961 *os <<
"is not a pointer that ";
1962 matcher_.DescribeTo(os);
1965 bool MatchAndExplain(PointerType pointer,
1966 MatchResultListener* listener)
const override {
1967 *listener <<
"which is a pointer that ";
1968 Pointer
p = GetRawPointer(pointer);
1969 return MatchPrintAndExplain(
p, matcher_, listener);
1973 Matcher<Pointer> matcher_;
1976 const InnerMatcher matcher_;
1986template <
typename To>
1987class WhenDynamicCastToMatcherBase {
1989 explicit WhenDynamicCastToMatcherBase(
const Matcher<To>& matcher)
1990 : matcher_(matcher) {}
1992 void DescribeTo(::std::ostream* os)
const {
1993 GetCastTypeDescription(os);
1994 matcher_.DescribeTo(os);
1997 void DescribeNegationTo(::std::ostream* os)
const {
1998 GetCastTypeDescription(os);
1999 matcher_.DescribeNegationTo(os);
2003 const Matcher<To> matcher_;
2005 static std::string GetToName() {
return GetTypeName<To>(); }
2008 static void GetCastTypeDescription(::std::ostream* os) {
2009 *os <<
"when dynamic_cast to " << GetToName() <<
", ";
2015template <
typename To>
2016class WhenDynamicCastToMatcher :
public WhenDynamicCastToMatcherBase<To> {
2018 explicit WhenDynamicCastToMatcher(
const Matcher<To>& matcher)
2019 : WhenDynamicCastToMatcherBase<To>(matcher) {}
2021 template <
typename From>
2022 bool MatchAndExplain(From from, MatchResultListener* listener)
const {
2023 To to =
dynamic_cast<To
>(from);
2024 return MatchPrintAndExplain(to, this->matcher_, listener);
2030template <
typename To>
2031class WhenDynamicCastToMatcher<To&> :
public WhenDynamicCastToMatcherBase<To&> {
2033 explicit WhenDynamicCastToMatcher(
const Matcher<To&>& matcher)
2034 : WhenDynamicCastToMatcherBase<To&>(matcher) {}
2036 template <
typename From>
2037 bool MatchAndExplain(From& from, MatchResultListener* listener)
const {
2039 To* to =
dynamic_cast<To*
>(&from);
2040 if (to ==
nullptr) {
2041 *listener <<
"which cannot be dynamic_cast to " << this->GetToName();
2044 return MatchPrintAndExplain(*to, this->matcher_, listener);
2051template <
typename Class,
typename FieldType>
2054 FieldMatcher(FieldType Class::*field,
2055 const Matcher<const FieldType&>& matcher)
2056 : field_(field), matcher_(matcher), whose_field_(
"whose given field ") {}
2058 FieldMatcher(
const std::string& field_name, FieldType Class::*field,
2059 const Matcher<const FieldType&>& matcher)
2062 whose_field_(
"whose field `" + field_name +
"` ") {}
2064 void DescribeTo(::std::ostream* os)
const {
2065 *os <<
"is an object " << whose_field_;
2066 matcher_.DescribeTo(os);
2069 void DescribeNegationTo(::std::ostream* os)
const {
2070 *os <<
"is an object " << whose_field_;
2071 matcher_.DescribeNegationTo(os);
2074 template <
typename T>
2075 bool MatchAndExplain(
const T&
value, MatchResultListener* listener)
const {
2078 return MatchAndExplainImpl(
2079 typename std::is_pointer<
typename std::remove_const<T>::type>::type(),
2084 bool MatchAndExplainImpl(std::false_type ,
2086 MatchResultListener* listener)
const {
2087 *listener << whose_field_ <<
"is ";
2088 return MatchPrintAndExplain(obj.*field_, matcher_, listener);
2091 bool MatchAndExplainImpl(std::true_type ,
const Class*
p,
2092 MatchResultListener* listener)
const {
2093 if (
p ==
nullptr)
return false;
2095 *listener <<
"which points to an object ";
2099 return MatchAndExplainImpl(std::false_type(), *
p, listener);
2102 const FieldType Class::*field_;
2103 const Matcher<const FieldType&> matcher_;
2107 const std::string whose_field_;
2115template <
typename Class,
typename PropertyType,
typename Property>
2116class PropertyMatcher {
2118 typedef const PropertyType& RefToConstProperty;
2120 PropertyMatcher(Property property,
const Matcher<RefToConstProperty>& matcher)
2121 : property_(property),
2123 whose_property_(
"whose given property ") {}
2125 PropertyMatcher(
const std::string& property_name, Property property,
2126 const Matcher<RefToConstProperty>& matcher)
2127 : property_(property),
2129 whose_property_(
"whose property `" + property_name +
"` ") {}
2131 void DescribeTo(::std::ostream* os)
const {
2132 *os <<
"is an object " << whose_property_;
2133 matcher_.DescribeTo(os);
2136 void DescribeNegationTo(::std::ostream* os)
const {
2137 *os <<
"is an object " << whose_property_;
2138 matcher_.DescribeNegationTo(os);
2141 template <
typename T>
2142 bool MatchAndExplain(
const T&
value, MatchResultListener* listener)
const {
2143 return MatchAndExplainImpl(
2144 typename std::is_pointer<
typename std::remove_const<T>::type>::type(),
2149 bool MatchAndExplainImpl(std::false_type ,
2151 MatchResultListener* listener)
const {
2152 *listener << whose_property_ <<
"is ";
2155 RefToConstProperty result = (obj.*property_)();
2156 return MatchPrintAndExplain(result, matcher_, listener);
2159 bool MatchAndExplainImpl(std::true_type ,
const Class*
p,
2160 MatchResultListener* listener)
const {
2161 if (
p ==
nullptr)
return false;
2163 *listener <<
"which points to an object ";
2167 return MatchAndExplainImpl(std::false_type(), *
p, listener);
2171 const Matcher<RefToConstProperty> matcher_;
2175 const std::string whose_property_;
2180template <
typename Functor>
2181struct CallableTraits {
2182 typedef Functor StorageType;
2184 static void CheckIsValid(Functor ) {}
2186 template <
typename T>
2187 static auto Invoke(Functor f,
const T& arg) ->
decltype(f(arg)) {
2193template <
typename ArgType,
typename ResType>
2194struct CallableTraits<ResType (*)(ArgType)> {
2195 typedef ResType ResultType;
2196 typedef ResType (*StorageType)(ArgType);
2198 static void CheckIsValid(ResType (*f)(ArgType)) {
2200 <<
"NULL function pointer is passed into ResultOf().";
2202 template <
typename T>
2203 static ResType
Invoke(ResType (*f)(ArgType),
T arg) {
2210template <
typename Callable,
typename InnerMatcher>
2211class ResultOfMatcher {
2213 ResultOfMatcher(Callable callable, InnerMatcher matcher)
2214 : ResultOfMatcher(
"", std::move(callable),
2215 std::move(matcher)) {}
2217 ResultOfMatcher(
const std::string& result_description, Callable callable,
2218 InnerMatcher matcher)
2219 : result_description_(result_description),
2220 callable_(std::move(callable)),
2221 matcher_(std::move(matcher)) {
2222 CallableTraits<Callable>::CheckIsValid(callable_);
2225 template <
typename T>
2226 operator Matcher<T>()
const {
2228 new Impl<const T&>(result_description_, callable_, matcher_));
2232 typedef typename CallableTraits<Callable>::StorageType CallableStorageType;
2234 template <
typename T>
2235 class Impl :
public MatcherInterface<T> {
2236 using ResultType =
decltype(CallableTraits<Callable>::template Invoke<T>(
2237 std::declval<CallableStorageType>(), std::declval<T>()));
2240 template <
typename M>
2241 Impl(
const std::string& result_description,
2242 const CallableStorageType& callable,
const M& matcher)
2243 : result_description_(result_description),
2244 callable_(callable),
2245 matcher_(MatcherCast<ResultType>(matcher)) {}
2247 void DescribeTo(::std::ostream* os)
const override {
2248 if (result_description_.empty()) {
2249 *os <<
"is mapped by the given callable to a value that ";
2251 *os <<
"whose " << result_description_ <<
" ";
2253 matcher_.DescribeTo(os);
2256 void DescribeNegationTo(::std::ostream* os)
const override {
2257 if (result_description_.empty()) {
2258 *os <<
"is mapped by the given callable to a value that ";
2260 *os <<
"whose " << result_description_ <<
" ";
2262 matcher_.DescribeNegationTo(os);
2265 bool MatchAndExplain(
T obj, MatchResultListener* listener)
const override {
2266 if (result_description_.empty()) {
2267 *listener <<
"which is mapped by the given callable to ";
2269 *listener <<
"whose " << result_description_ <<
" is ";
2276 CallableTraits<Callable>::template Invoke<T>(callable_, obj);
2277 return MatchPrintAndExplain(result, matcher_, listener);
2281 const std::string result_description_;
2287 mutable CallableStorageType callable_;
2288 const Matcher<ResultType> matcher_;
2291 const std::string result_description_;
2292 const CallableStorageType callable_;
2293 const InnerMatcher matcher_;
2297template <
typename SizeMatcher>
2298class SizeIsMatcher {
2300 explicit SizeIsMatcher(
const SizeMatcher& size_matcher)
2301 : size_matcher_(size_matcher) {}
2303 template <
typename Container>
2304 operator Matcher<Container>()
const {
2305 return Matcher<Container>(
new Impl<const Container&>(size_matcher_));
2308 template <
typename Container>
2309 class Impl :
public MatcherInterface<Container> {
2311 using SizeType =
decltype(std::declval<Container>().size());
2312 explicit Impl(
const SizeMatcher& size_matcher)
2313 : size_matcher_(MatcherCast<SizeType>(size_matcher)) {}
2315 void DescribeTo(::std::ostream* os)
const override {
2316 *os <<
"has a size that ";
2317 size_matcher_.DescribeTo(os);
2319 void DescribeNegationTo(::std::ostream* os)
const override {
2320 *os <<
"has a size that ";
2321 size_matcher_.DescribeNegationTo(os);
2324 bool MatchAndExplain(Container container,
2325 MatchResultListener* listener)
const override {
2326 SizeType size = container.size();
2327 StringMatchResultListener size_listener;
2328 const bool result = size_matcher_.MatchAndExplain(size, &size_listener);
2329 *listener <<
"whose size " << size
2330 << (result ?
" matches" :
" doesn't match");
2331 PrintIfNotEmpty(size_listener.str(), listener->stream());
2336 const Matcher<SizeType> size_matcher_;
2340 const SizeMatcher size_matcher_;
2345template <
typename DistanceMatcher>
2346class BeginEndDistanceIsMatcher {
2348 explicit BeginEndDistanceIsMatcher(
const DistanceMatcher& distance_matcher)
2349 : distance_matcher_(distance_matcher) {}
2351 template <
typename Container>
2352 operator Matcher<Container>()
const {
2353 return Matcher<Container>(
new Impl<const Container&>(distance_matcher_));
2356 template <
typename Container>
2357 class Impl :
public MatcherInterface<Container> {
2362 typedef typename std::iterator_traits<
2363 typename ContainerView::type::const_iterator>::difference_type
2365 explicit Impl(
const DistanceMatcher& distance_matcher)
2366 : distance_matcher_(MatcherCast<DistanceType>(distance_matcher)) {}
2368 void DescribeTo(::std::ostream* os)
const override {
2369 *os <<
"distance between begin() and end() ";
2370 distance_matcher_.DescribeTo(os);
2372 void DescribeNegationTo(::std::ostream* os)
const override {
2373 *os <<
"distance between begin() and end() ";
2374 distance_matcher_.DescribeNegationTo(os);
2377 bool MatchAndExplain(Container container,
2378 MatchResultListener* listener)
const override {
2381 DistanceType distance = std::distance(begin(container), end(container));
2382 StringMatchResultListener distance_listener;
2384 distance_matcher_.MatchAndExplain(distance, &distance_listener);
2385 *listener <<
"whose distance between begin() and end() " << distance
2386 << (result ?
" matches" :
" doesn't match");
2387 PrintIfNotEmpty(distance_listener.str(), listener->stream());
2392 const Matcher<DistanceType> distance_matcher_;
2396 const DistanceMatcher distance_matcher_;
2409template <
typename Container>
2410class ContainerEqMatcher {
2412 typedef internal::StlContainerView<Container> View;
2413 typedef typename View::type StlContainer;
2414 typedef typename View::const_reference StlContainerReference;
2416 static_assert(!std::is_const<Container>::value,
2417 "Container type must not be const");
2418 static_assert(!std::is_reference<Container>::value,
2419 "Container type must not be a reference");
2423 explicit ContainerEqMatcher(
const Container& expected)
2424 : expected_(View::Copy(expected)) {}
2426 void DescribeTo(::std::ostream* os)
const {
2428 UniversalPrint(expected_, os);
2430 void DescribeNegationTo(::std::ostream* os)
const {
2431 *os <<
"does not equal ";
2432 UniversalPrint(expected_, os);
2435 template <
typename LhsContainer>
2436 bool MatchAndExplain(
const LhsContainer& lhs,
2437 MatchResultListener* listener)
const {
2438 typedef internal::StlContainerView<
2439 typename std::remove_const<LhsContainer>::type>
2441 StlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2442 if (lhs_stl_container == expected_)
return true;
2444 ::std::ostream*
const os = listener->stream();
2445 if (os !=
nullptr) {
2447 bool printed_header =
false;
2448 for (
auto it = lhs_stl_container.begin(); it != lhs_stl_container.end();
2450 if (internal::ArrayAwareFind(expected_.begin(), expected_.end(), *it) ==
2452 if (printed_header) {
2455 *os <<
"which has these unexpected elements: ";
2456 printed_header =
true;
2458 UniversalPrint(*it, os);
2463 bool printed_header2 =
false;
2464 for (
auto it = expected_.begin(); it != expected_.end(); ++it) {
2465 if (internal::ArrayAwareFind(lhs_stl_container.begin(),
2466 lhs_stl_container.end(),
2467 *it) == lhs_stl_container.end()) {
2468 if (printed_header2) {
2471 *os << (printed_header ?
",\nand" :
"which")
2472 <<
" doesn't have these expected elements: ";
2473 printed_header2 =
true;
2475 UniversalPrint(*it, os);
2484 const StlContainer expected_;
2488struct LessComparator {
2489 template <
typename T,
typename U>
2490 bool operator()(
const T& lhs,
const U& rhs)
const {
2496template <
typename Comparator,
typename ContainerMatcher>
2497class WhenSortedByMatcher {
2499 WhenSortedByMatcher(
const Comparator& comparator,
2500 const ContainerMatcher& matcher)
2501 : comparator_(comparator), matcher_(matcher) {}
2503 template <
typename LhsContainer>
2504 operator Matcher<LhsContainer>()
const {
2505 return MakeMatcher(
new Impl<LhsContainer>(comparator_, matcher_));
2508 template <
typename LhsContainer>
2509 class Impl :
public MatcherInterface<LhsContainer> {
2514 typedef typename LhsView::type LhsStlContainer;
2515 typedef typename LhsView::const_reference LhsStlContainerReference;
2519 typename RemoveConstFromKey<typename LhsStlContainer::value_type>::type
2522 Impl(
const Comparator& comparator,
const ContainerMatcher& matcher)
2523 : comparator_(comparator), matcher_(matcher) {}
2525 void DescribeTo(::std::ostream* os)
const override {
2526 *os <<
"(when sorted) ";
2527 matcher_.DescribeTo(os);
2530 void DescribeNegationTo(::std::ostream* os)
const override {
2531 *os <<
"(when sorted) ";
2532 matcher_.DescribeNegationTo(os);
2535 bool MatchAndExplain(LhsContainer lhs,
2536 MatchResultListener* listener)
const override {
2537 LhsStlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2538 ::std::vector<LhsValue> sorted_container(lhs_stl_container.begin(),
2539 lhs_stl_container.end());
2540 ::std::sort(sorted_container.begin(), sorted_container.end(),
2543 if (!listener->IsInterested()) {
2546 return matcher_.Matches(sorted_container);
2549 *listener <<
"which is ";
2550 UniversalPrint(sorted_container, listener->stream());
2551 *listener <<
" when sorted";
2553 StringMatchResultListener inner_listener;
2555 matcher_.MatchAndExplain(sorted_container, &inner_listener);
2556 PrintIfNotEmpty(inner_listener.str(), listener->stream());
2561 const Comparator comparator_;
2562 const Matcher<const ::std::vector<LhsValue>&> matcher_;
2564 Impl(
const Impl&) =
delete;
2565 Impl& operator=(
const Impl&) =
delete;
2569 const Comparator comparator_;
2570 const ContainerMatcher matcher_;
2577template <
typename TupleMatcher,
typename RhsContainer>
2578class PointwiseMatcher {
2581 "use UnorderedPointwise with hash tables");
2584 typedef internal::StlContainerView<RhsContainer> RhsView;
2585 typedef typename RhsView::type RhsStlContainer;
2586 typedef typename RhsStlContainer::value_type RhsValue;
2588 static_assert(!std::is_const<RhsContainer>::value,
2589 "RhsContainer type must not be const");
2590 static_assert(!std::is_reference<RhsContainer>::value,
2591 "RhsContainer type must not be a reference");
2595 PointwiseMatcher(
const TupleMatcher& tuple_matcher,
const RhsContainer& rhs)
2596 : tuple_matcher_(tuple_matcher), rhs_(RhsView::Copy(rhs)) {}
2598 template <
typename LhsContainer>
2599 operator Matcher<LhsContainer>()
const {
2602 "use UnorderedPointwise with hash tables");
2604 return Matcher<LhsContainer>(
2605 new Impl<const LhsContainer&>(tuple_matcher_, rhs_));
2608 template <
typename LhsContainer>
2609 class Impl :
public MatcherInterface<LhsContainer> {
2614 typedef typename LhsView::type LhsStlContainer;
2615 typedef typename LhsView::const_reference LhsStlContainerReference;
2616 typedef typename LhsStlContainer::value_type LhsValue;
2621 typedef ::std::tuple<const LhsValue&, const RhsValue&> InnerMatcherArg;
2623 Impl(
const TupleMatcher& tuple_matcher,
const RhsStlContainer& rhs)
2625 : mono_tuple_matcher_(SafeMatcherCast<InnerMatcherArg>(tuple_matcher)),
2628 void DescribeTo(::std::ostream* os)
const override {
2629 *os <<
"contains " << rhs_.size()
2630 <<
" values, where each value and its corresponding value in ";
2631 UniversalPrinter<RhsStlContainer>::Print(rhs_, os);
2633 mono_tuple_matcher_.DescribeTo(os);
2635 void DescribeNegationTo(::std::ostream* os)
const override {
2636 *os <<
"doesn't contain exactly " << rhs_.size()
2637 <<
" values, or contains a value x at some index i"
2638 <<
" where x and the i-th value of ";
2639 UniversalPrint(rhs_, os);
2641 mono_tuple_matcher_.DescribeNegationTo(os);
2644 bool MatchAndExplain(LhsContainer lhs,
2645 MatchResultListener* listener)
const override {
2646 LhsStlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2647 const size_t actual_size = lhs_stl_container.size();
2648 if (actual_size != rhs_.size()) {
2649 *listener <<
"which contains " << actual_size <<
" values";
2653 auto left = lhs_stl_container.begin();
2654 auto right = rhs_.begin();
2655 for (
size_t i = 0;
i != actual_size; ++
i, ++left, ++right) {
2656 if (listener->IsInterested()) {
2657 StringMatchResultListener inner_listener;
2661 if (!mono_tuple_matcher_.MatchAndExplain(
2662 InnerMatcherArg(ImplicitCast_<const LhsValue&>(*left),
2663 ImplicitCast_<const RhsValue&>(*right)),
2665 *listener <<
"where the value pair (";
2666 UniversalPrint(*left, listener->stream());
2668 UniversalPrint(*right, listener->stream());
2669 *listener <<
") at index #" <<
i <<
" don't match";
2670 PrintIfNotEmpty(inner_listener.str(), listener->stream());
2674 if (!mono_tuple_matcher_.Matches(
2675 InnerMatcherArg(ImplicitCast_<const LhsValue&>(*left),
2676 ImplicitCast_<const RhsValue&>(*right))))
2685 const Matcher<InnerMatcherArg> mono_tuple_matcher_;
2686 const RhsStlContainer rhs_;
2690 const TupleMatcher tuple_matcher_;
2691 const RhsStlContainer rhs_;
2695template <
typename Container>
2696class QuantifierMatcherImpl :
public MatcherInterface<Container> {
2699 typedef StlContainerView<RawContainer> View;
2700 typedef typename View::type StlContainer;
2701 typedef typename View::const_reference StlContainerReference;
2702 typedef typename StlContainer::value_type Element;
2704 template <
typename InnerMatcher>
2705 explicit QuantifierMatcherImpl(InnerMatcher inner_matcher)
2707 testing::SafeMatcherCast<const Element&>(inner_matcher)) {}
2712 bool MatchAndExplainImpl(
bool all_elements_should_match, Container container,
2713 MatchResultListener* listener)
const {
2714 StlContainerReference stl_container = View::ConstReference(container);
2716 for (
auto it = stl_container.begin(); it != stl_container.end();
2718 StringMatchResultListener inner_listener;
2719 const bool matches = inner_matcher_.MatchAndExplain(*it, &inner_listener);
2721 if (matches != all_elements_should_match) {
2722 *listener <<
"whose element #" <<
i
2723 << (matches ?
" matches" :
" doesn't match");
2724 PrintIfNotEmpty(inner_listener.str(), listener->stream());
2725 return !all_elements_should_match;
2728 return all_elements_should_match;
2731 bool MatchAndExplainImpl(
const Matcher<size_t>& count_matcher,
2732 Container container,
2733 MatchResultListener* listener)
const {
2734 StlContainerReference stl_container = View::ConstReference(container);
2736 std::vector<size_t> match_elements;
2737 for (
auto it = stl_container.begin(); it != stl_container.end();
2739 StringMatchResultListener inner_listener;
2740 const bool matches = inner_matcher_.MatchAndExplain(*it, &inner_listener);
2742 match_elements.push_back(
i);
2745 if (listener->IsInterested()) {
2746 if (match_elements.empty()) {
2747 *listener <<
"has no element that matches";
2748 }
else if (match_elements.size() == 1) {
2749 *listener <<
"whose element #" << match_elements[0] <<
" matches";
2751 *listener <<
"whose elements (";
2752 std::string sep =
"";
2753 for (
size_t e : match_elements) {
2754 *listener << sep << e;
2757 *listener <<
") match";
2760 StringMatchResultListener count_listener;
2761 if (count_matcher.MatchAndExplain(match_elements.size(), &count_listener)) {
2762 *listener <<
" and whose match quantity of " << match_elements.size()
2764 PrintIfNotEmpty(count_listener.str(), listener->stream());
2767 if (match_elements.empty()) {
2768 *listener <<
" and";
2770 *listener <<
" but";
2772 *listener <<
" whose match quantity of " << match_elements.size()
2773 <<
" does not match";
2774 PrintIfNotEmpty(count_listener.str(), listener->stream());
2780 const Matcher<const Element&> inner_matcher_;
2785template <
typename Container>
2786class ContainsMatcherImpl :
public QuantifierMatcherImpl<Container> {
2788 template <
typename InnerMatcher>
2789 explicit ContainsMatcherImpl(InnerMatcher inner_matcher)
2790 : QuantifierMatcherImpl<Container>(inner_matcher) {}
2793 void DescribeTo(::std::ostream* os)
const override {
2794 *os <<
"contains at least one element that ";
2795 this->inner_matcher_.DescribeTo(os);
2798 void DescribeNegationTo(::std::ostream* os)
const override {
2799 *os <<
"doesn't contain any element that ";
2800 this->inner_matcher_.DescribeTo(os);
2803 bool MatchAndExplain(Container container,
2804 MatchResultListener* listener)
const override {
2805 return this->MatchAndExplainImpl(
false, container, listener);
2811template <
typename Container>
2812class EachMatcherImpl :
public QuantifierMatcherImpl<Container> {
2814 template <
typename InnerMatcher>
2815 explicit EachMatcherImpl(InnerMatcher inner_matcher)
2816 : QuantifierMatcherImpl<Container>(inner_matcher) {}
2819 void DescribeTo(::std::ostream* os)
const override {
2820 *os <<
"only contains elements that ";
2821 this->inner_matcher_.DescribeTo(os);
2824 void DescribeNegationTo(::std::ostream* os)
const override {
2825 *os <<
"contains some element that ";
2826 this->inner_matcher_.DescribeNegationTo(os);
2829 bool MatchAndExplain(Container container,
2830 MatchResultListener* listener)
const override {
2831 return this->MatchAndExplainImpl(
true, container, listener);
2837template <
typename Container>
2838class ContainsTimesMatcherImpl :
public QuantifierMatcherImpl<Container> {
2840 template <
typename InnerMatcher>
2841 explicit ContainsTimesMatcherImpl(InnerMatcher inner_matcher,
2842 Matcher<size_t> count_matcher)
2843 : QuantifierMatcherImpl<Container>(inner_matcher),
2844 count_matcher_(std::move(count_matcher)) {}
2846 void DescribeTo(::std::ostream* os)
const override {
2847 *os <<
"quantity of elements that match ";
2848 this->inner_matcher_.DescribeTo(os);
2850 count_matcher_.DescribeTo(os);
2853 void DescribeNegationTo(::std::ostream* os)
const override {
2854 *os <<
"quantity of elements that match ";
2855 this->inner_matcher_.DescribeTo(os);
2857 count_matcher_.DescribeNegationTo(os);
2860 bool MatchAndExplain(Container container,
2861 MatchResultListener* listener)
const override {
2862 return this->MatchAndExplainImpl(count_matcher_, container, listener);
2866 const Matcher<size_t> count_matcher_;
2870template <
typename M>
2871class ContainsTimesMatcher {
2873 explicit ContainsTimesMatcher(M m, Matcher<size_t> count_matcher)
2874 : inner_matcher_(m), count_matcher_(std::move(count_matcher)) {}
2876 template <
typename Container>
2877 operator Matcher<Container>()
const {
2878 return Matcher<Container>(
new ContainsTimesMatcherImpl<const Container&>(
2879 inner_matcher_, count_matcher_));
2883 const M inner_matcher_;
2884 const Matcher<size_t> count_matcher_;
2888template <
typename M>
2889class ContainsMatcher {
2891 explicit ContainsMatcher(M m) : inner_matcher_(m) {}
2893 template <
typename Container>
2894 operator Matcher<Container>()
const {
2895 return Matcher<Container>(
2896 new ContainsMatcherImpl<const Container&>(inner_matcher_));
2899 ContainsTimesMatcher<M> Times(Matcher<size_t> count_matcher)
const {
2900 return ContainsTimesMatcher<M>(inner_matcher_, std::move(count_matcher));
2904 const M inner_matcher_;
2908template <
typename M>
2911 explicit EachMatcher(M m) : inner_matcher_(m) {}
2913 template <
typename Container>
2914 operator Matcher<Container>()
const {
2915 return Matcher<Container>(
2916 new EachMatcherImpl<const Container&>(inner_matcher_));
2920 const M inner_matcher_;
2924struct Rank0 : Rank1 {};
2926namespace pair_getters {
2928template <
typename T>
2929auto First(
T&
x, Rank1) ->
decltype(get<0>(
x)) {
2932template <
typename T>
2933auto First(
T&
x, Rank0) ->
decltype((
x.first)) {
2937template <
typename T>
2938auto Second(
T&
x, Rank1) ->
decltype(get<1>(
x)) {
2941template <
typename T>
2942auto Second(
T&
x, Rank0) ->
decltype((
x.second)) {
2951template <
typename PairType>
2952class KeyMatcherImpl :
public MatcherInterface<PairType> {
2955 typedef typename RawPairType::first_type KeyType;
2957 template <
typename InnerMatcher>
2958 explicit KeyMatcherImpl(InnerMatcher inner_matcher)
2960 testing::SafeMatcherCast<const KeyType&>(inner_matcher)) {}
2964 bool MatchAndExplain(PairType key_value,
2965 MatchResultListener* listener)
const override {
2966 StringMatchResultListener inner_listener;
2967 const bool match = inner_matcher_.MatchAndExplain(
2968 pair_getters::First(key_value, Rank0()), &inner_listener);
2969 const std::string explanation = inner_listener.str();
2970 if (!explanation.empty()) {
2971 *listener <<
"whose first field is a value " << explanation;
2977 void DescribeTo(::std::ostream* os)
const override {
2978 *os <<
"has a key that ";
2979 inner_matcher_.DescribeTo(os);
2983 void DescribeNegationTo(::std::ostream* os)
const override {
2984 *os <<
"doesn't have a key that ";
2985 inner_matcher_.DescribeTo(os);
2989 const Matcher<const KeyType&> inner_matcher_;
2993template <
typename M>
2996 explicit KeyMatcher(M m) : matcher_for_key_(m) {}
2998 template <
typename PairType>
2999 operator Matcher<PairType>()
const {
3000 return Matcher<PairType>(
3001 new KeyMatcherImpl<const PairType&>(matcher_for_key_));
3005 const M matcher_for_key_;
3009template <
typename InnerMatcher>
3010class AddressMatcher {
3012 explicit AddressMatcher(InnerMatcher m) : matcher_(m) {}
3014 template <
typename Type>
3015 operator Matcher<Type>()
const {
3016 return Matcher<Type>(
new Impl<const Type&>(matcher_));
3021 template <
typename Type>
3022 class Impl :
public MatcherInterface<Type> {
3025 explicit Impl(
const InnerMatcher& matcher)
3026 : matcher_(MatcherCast<Address>(matcher)) {}
3028 void DescribeTo(::std::ostream* os)
const override {
3029 *os <<
"has address that ";
3030 matcher_.DescribeTo(os);
3033 void DescribeNegationTo(::std::ostream* os)
const override {
3034 *os <<
"does not have address that ";
3035 matcher_.DescribeTo(os);
3038 bool MatchAndExplain(Type
object,
3039 MatchResultListener* listener)
const override {
3040 *listener <<
"which has address ";
3041 Address address = std::addressof(
object);
3042 return MatchPrintAndExplain(address, matcher_, listener);
3046 const Matcher<Address> matcher_;
3048 const InnerMatcher matcher_;
3053template <
typename PairType>
3054class PairMatcherImpl :
public MatcherInterface<PairType> {
3057 typedef typename RawPairType::first_type FirstType;
3058 typedef typename RawPairType::second_type SecondType;
3060 template <
typename FirstMatcher,
typename SecondMatcher>
3061 PairMatcherImpl(FirstMatcher first_matcher, SecondMatcher second_matcher)
3063 testing::SafeMatcherCast<const FirstType&>(first_matcher)),
3065 testing::SafeMatcherCast<const SecondType&>(second_matcher)) {}
3068 void DescribeTo(::std::ostream* os)
const override {
3069 *os <<
"has a first field that ";
3070 first_matcher_.DescribeTo(os);
3071 *os <<
", and has a second field that ";
3072 second_matcher_.DescribeTo(os);
3076 void DescribeNegationTo(::std::ostream* os)
const override {
3077 *os <<
"has a first field that ";
3078 first_matcher_.DescribeNegationTo(os);
3079 *os <<
", or has a second field that ";
3080 second_matcher_.DescribeNegationTo(os);
3085 bool MatchAndExplain(PairType a_pair,
3086 MatchResultListener* listener)
const override {
3087 if (!listener->IsInterested()) {
3090 return first_matcher_.Matches(pair_getters::First(a_pair, Rank0())) &&
3091 second_matcher_.Matches(pair_getters::Second(a_pair, Rank0()));
3093 StringMatchResultListener first_inner_listener;
3094 if (!first_matcher_.MatchAndExplain(pair_getters::First(a_pair, Rank0()),
3095 &first_inner_listener)) {
3096 *listener <<
"whose first field does not match";
3097 PrintIfNotEmpty(first_inner_listener.str(), listener->stream());
3100 StringMatchResultListener second_inner_listener;
3101 if (!second_matcher_.MatchAndExplain(pair_getters::Second(a_pair, Rank0()),
3102 &second_inner_listener)) {
3103 *listener <<
"whose second field does not match";
3104 PrintIfNotEmpty(second_inner_listener.str(), listener->stream());
3107 ExplainSuccess(first_inner_listener.str(), second_inner_listener.str(),
3113 void ExplainSuccess(
const std::string& first_explanation,
3114 const std::string& second_explanation,
3115 MatchResultListener* listener)
const {
3116 *listener <<
"whose both fields match";
3117 if (!first_explanation.empty()) {
3118 *listener <<
", where the first field is a value " << first_explanation;
3120 if (!second_explanation.empty()) {
3122 if (!first_explanation.empty()) {
3123 *listener <<
"and ";
3125 *listener <<
"where ";
3127 *listener <<
"the second field is a value " << second_explanation;
3131 const Matcher<const FirstType&> first_matcher_;
3132 const Matcher<const SecondType&> second_matcher_;
3136template <
typename FirstMatcher,
typename SecondMatcher>
3139 PairMatcher(FirstMatcher first_matcher, SecondMatcher second_matcher)
3140 : first_matcher_(first_matcher), second_matcher_(second_matcher) {}
3142 template <
typename PairType>
3143 operator Matcher<PairType>()
const {
3144 return Matcher<PairType>(
3145 new PairMatcherImpl<const PairType&>(first_matcher_, second_matcher_));
3149 const FirstMatcher first_matcher_;
3150 const SecondMatcher second_matcher_;
3153template <
typename T,
size_t... I>
3154auto UnpackStructImpl(
const T& t, IndexSequence<I...>,
int)
3155 ->
decltype(std::tie(get<I>(t)...)) {
3156 static_assert(std::tuple_size<T>::value ==
sizeof...(I),
3157 "Number of arguments doesn't match the number of fields.");
3158 return std::tie(get<I>(t)...);
3161#if defined(__cpp_structured_bindings) && __cpp_structured_bindings >= 201606
3162template <
typename T>
3164 const auto& [a] = t;
3167template <
typename T>
3169 const auto& [a, b] = t;
3170 return std::tie(a, b);
3172template <
typename T>
3174 const auto& [a, b, c] = t;
3175 return std::tie(a, b, c);
3177template <
typename T>
3179 const auto& [a, b, c, d] = t;
3180 return std::tie(a, b, c, d);
3182template <
typename T>
3184 const auto& [a, b, c, d, e] = t;
3185 return std::tie(a, b, c, d, e);
3187template <
typename T>
3189 const auto& [a, b, c, d, e, f] = t;
3190 return std::tie(a, b, c, d, e, f);
3192template <
typename T>
3194 const auto& [a, b, c, d, e, f, g] = t;
3195 return std::tie(a, b, c, d, e, f, g);
3197template <
typename T>
3199 const auto& [a, b, c, d, e, f, g, h] = t;
3200 return std::tie(a, b, c, d, e, f, g, h);
3202template <
typename T>
3204 const auto& [a, b, c, d, e, f, g, h,
i] = t;
3205 return std::tie(a, b, c, d, e, f, g, h,
i);
3207template <
typename T>
3209 const auto& [a, b, c, d, e, f, g, h,
i, j] = t;
3210 return std::tie(a, b, c, d, e, f, g, h,
i, j);
3212template <
typename T>
3214 const auto& [a, b, c, d, e, f, g, h,
i, j, k] = t;
3215 return std::tie(a, b, c, d, e, f, g, h,
i, j, k);
3217template <
typename T>
3219 const auto& [a, b, c, d, e, f, g, h,
i, j, k, l] = t;
3220 return std::tie(a, b, c, d, e, f, g, h,
i, j, k, l);
3222template <
typename T>
3224 const auto& [a, b, c, d, e, f, g, h,
i, j, k, l, m] = t;
3225 return std::tie(a, b, c, d, e, f, g, h,
i, j, k, l, m);
3227template <
typename T>
3229 const auto& [a, b, c, d, e, f, g, h,
i, j, k, l, m, n] = t;
3230 return std::tie(a, b, c, d, e, f, g, h,
i, j, k, l, m, n);
3232template <
typename T>
3234 const auto& [a, b, c, d, e, f, g, h,
i, j, k, l, m, n, o] = t;
3235 return std::tie(a, b, c, d, e, f, g, h,
i, j, k, l, m, n, o);
3237template <
typename T>
3239 const auto& [a, b, c, d, e, f, g, h,
i, j, k, l, m, n, o,
p] = t;
3240 return std::tie(a, b, c, d, e, f, g, h,
i, j, k, l, m, n, o,
p);
3242template <
typename T>
3244 const auto& [a, b, c, d, e, f, g, h,
i, j, k, l, m, n, o,
p, q] = t;
3245 return std::tie(a, b, c, d, e, f, g, h,
i, j, k, l, m, n, o,
p, q);
3247template <
typename T>
3249 const auto& [a, b, c, d, e, f, g, h,
i, j, k, l, m, n, o,
p, q, r] = t;
3250 return std::tie(a, b, c, d, e, f, g, h,
i, j, k, l, m, n, o,
p, q, r);
3252template <
typename T>
3254 const auto& [a, b, c, d, e, f, g, h,
i, j, k, l, m, n, o,
p, q, r, s] = t;
3255 return std::tie(a, b, c, d, e, f, g, h,
i, j, k, l, m, n, o,
p, q, r, s);
3259template <
size_t I,
typename T>
3260auto UnpackStruct(
const T& t)
3268template <
typename T,
size_t N>
3269void VariadicExpand(
const T (&)[N]) {}
3271template <
typename Struct,
typename StructSize>
3272class FieldsAreMatcherImpl;
3274template <
typename Struct,
size_t... I>
3275class FieldsAreMatcherImpl<Struct, IndexSequence<I...>>
3276 :
public MatcherInterface<Struct> {
3277 using UnpackedType =
3278 decltype(UnpackStruct<
sizeof...(I)>(std::declval<const Struct&>()));
3279 using MatchersType = std::tuple<
3280 Matcher<const typename std::tuple_element<I, UnpackedType>::type&>...>;
3283 template <
typename Inner>
3284 explicit FieldsAreMatcherImpl(
const Inner& matchers)
3285 : matchers_(testing::SafeMatcherCast<
3286 const typename std::tuple_element<I, UnpackedType>::type&>(
3287 std::get<I>(matchers))...) {}
3289 void DescribeTo(::std::ostream* os)
const override {
3290 const char* separator =
"";
3292 {(*os << separator <<
"has field #" << I <<
" that ",
3293 std::get<I>(matchers_).DescribeTo(os), separator =
", and ")...});
3296 void DescribeNegationTo(::std::ostream* os)
const override {
3297 const char* separator =
"";
3298 VariadicExpand({(*os << separator <<
"has field #" << I <<
" that ",
3299 std::get<I>(matchers_).DescribeNegationTo(os),
3300 separator =
", or ")...});
3303 bool MatchAndExplain(Struct t, MatchResultListener* listener)
const override {
3304 return MatchInternal((UnpackStruct<
sizeof...(I)>)(t), listener);
3308 bool MatchInternal(UnpackedType tuple, MatchResultListener* listener)
const {
3309 if (!listener->IsInterested()) {
3313 VariadicExpand({good = good && std::get<I>(matchers_).Matches(
3314 std::get<I>(tuple))...});
3318 size_t failed_pos = ~size_t{};
3320 std::vector<StringMatchResultListener> inner_listener(
sizeof...(I));
3323 {failed_pos == ~size_t{} && !std::get<I>(matchers_).MatchAndExplain(
3324 std::get<I>(tuple), &inner_listener[I])
3327 if (failed_pos != ~
size_t{}) {
3328 *listener <<
"whose field #" << failed_pos <<
" does not match";
3329 PrintIfNotEmpty(inner_listener[failed_pos].str(), listener->stream());
3333 *listener <<
"whose all elements match";
3334 const char* separator =
", where";
3335 for (
size_t index = 0; index <
sizeof...(I); ++index) {
3336 const std::string str = inner_listener[index].str();
3338 *listener << separator <<
" field #" << index <<
" is a value " << str;
3339 separator =
", and";
3346 MatchersType matchers_;
3349template <
typename... Inner>
3350class FieldsAreMatcher {
3352 explicit FieldsAreMatcher(Inner... inner) : matchers_(std::move(inner)...) {}
3354 template <
typename Struct>
3355 operator Matcher<Struct>()
const {
3356 return Matcher<Struct>(
3357 new FieldsAreMatcherImpl<
const Struct&, IndexSequenceFor<Inner...>>(
3362 std::tuple<Inner...> matchers_;
3366template <
typename Container>
3367class ElementsAreMatcherImpl :
public MatcherInterface<Container> {
3370 typedef internal::StlContainerView<RawContainer> View;
3371 typedef typename View::type StlContainer;
3372 typedef typename View::const_reference StlContainerReference;
3373 typedef typename StlContainer::value_type Element;
3377 template <
typename InputIter>
3378 ElementsAreMatcherImpl(InputIter first, InputIter last) {
3379 while (first != last) {
3380 matchers_.push_back(MatcherCast<const Element&>(*first++));
3385 void DescribeTo(::std::ostream* os)
const override {
3388 }
else if (
count() == 1) {
3389 *os <<
"has 1 element that ";
3390 matchers_[0].DescribeTo(os);
3392 *os <<
"has " << Elements(
count()) <<
" where\n";
3393 for (
size_t i = 0;
i !=
count(); ++
i) {
3394 *os <<
"element #" <<
i <<
" ";
3395 matchers_[
i].DescribeTo(os);
3404 void DescribeNegationTo(::std::ostream* os)
const override {
3406 *os <<
"isn't empty";
3410 *os <<
"doesn't have " << Elements(
count()) <<
", or\n";
3411 for (
size_t i = 0;
i !=
count(); ++
i) {
3412 *os <<
"element #" <<
i <<
" ";
3413 matchers_[
i].DescribeNegationTo(os);
3420 bool MatchAndExplain(Container container,
3421 MatchResultListener* listener)
const override {
3425 const bool listener_interested = listener->IsInterested();
3428 ::std::vector<std::string> explanations(
count());
3429 StlContainerReference stl_container = View::ConstReference(container);
3430 auto it = stl_container.begin();
3431 size_t exam_pos = 0;
3432 bool mismatch_found =
false;
3437 for (; it != stl_container.end() && exam_pos !=
count(); ++it, ++exam_pos) {
3439 if (listener_interested) {
3440 StringMatchResultListener s;
3441 match = matchers_[exam_pos].MatchAndExplain(*it, &s);
3442 explanations[exam_pos] = s.str();
3444 match = matchers_[exam_pos].Matches(*it);
3448 mismatch_found =
true;
3457 size_t actual_count = exam_pos;
3458 for (; it != stl_container.end(); ++it) {
3462 if (actual_count !=
count()) {
3467 if (listener_interested && (actual_count != 0)) {
3468 *listener <<
"which has " << Elements(actual_count);
3473 if (mismatch_found) {
3475 if (listener_interested) {
3476 *listener <<
"whose element #" << exam_pos <<
" doesn't match";
3477 PrintIfNotEmpty(explanations[exam_pos], listener->stream());
3484 if (listener_interested) {
3485 bool reason_printed =
false;
3486 for (
size_t i = 0;
i !=
count(); ++
i) {
3487 const std::string& s = explanations[
i];
3489 if (reason_printed) {
3490 *listener <<
",\nand ";
3492 *listener <<
"whose element #" <<
i <<
" matches, " << s;
3493 reason_printed =
true;
3501 static Message Elements(
size_t count) {
3502 return Message() <<
count << (
count == 1 ?
" element" :
" elements");
3505 size_t count()
const {
return matchers_.size(); }
3507 ::std::vector<Matcher<const Element&>> matchers_;
3516 MatchMatrix(
size_t num_elements,
size_t num_matchers)
3517 : num_elements_(num_elements),
3518 num_matchers_(num_matchers),
3519 matched_(num_elements_ * num_matchers_, 0) {}
3521 size_t LhsSize()
const {
return num_elements_; }
3522 size_t RhsSize()
const {
return num_matchers_; }
3523 bool HasEdge(
size_t ilhs,
size_t irhs)
const {
3524 return matched_[SpaceIndex(ilhs, irhs)] == 1;
3526 void SetEdge(
size_t ilhs,
size_t irhs,
bool b) {
3527 matched_[SpaceIndex(ilhs, irhs)] = b ? 1 : 0;
3537 std::string DebugString()
const;
3540 size_t SpaceIndex(
size_t ilhs,
size_t irhs)
const {
3541 return ilhs * num_matchers_ + irhs;
3544 size_t num_elements_;
3545 size_t num_matchers_;
3550 ::std::vector<char> matched_;
3553typedef ::std::pair<size_t, size_t> ElementMatcherPair;
3554typedef ::std::vector<ElementMatcherPair> ElementMatcherPairs;
3560struct UnorderedMatcherRequire {
3564 ExactMatch = Superset | Subset,
3571class GTEST_API_ UnorderedElementsAreMatcherImplBase {
3573 explicit UnorderedElementsAreMatcherImplBase(
3574 UnorderedMatcherRequire::Flags matcher_flags)
3575 : match_flags_(matcher_flags) {}
3580 typedef ::std::vector<const MatcherDescriberInterface*> MatcherDescriberVec;
3583 void DescribeToImpl(::std::ostream* os)
const;
3586 void DescribeNegationToImpl(::std::ostream* os)
const;
3588 bool VerifyMatchMatrix(const ::std::vector<std::string>& element_printouts,
3589 const MatchMatrix& matrix,
3590 MatchResultListener* listener)
const;
3592 bool FindPairing(
const MatchMatrix& matrix,
3593 MatchResultListener* listener)
const;
3595 MatcherDescriberVec& matcher_describers() {
return matcher_describers_; }
3597 static Message Elements(
size_t n) {
3598 return Message() << n <<
" element" << (n == 1 ?
"" :
"s");
3601 UnorderedMatcherRequire::Flags match_flags()
const {
return match_flags_; }
3604 UnorderedMatcherRequire::Flags match_flags_;
3605 MatcherDescriberVec matcher_describers_;
3610template <
typename Container>
3611class UnorderedElementsAreMatcherImpl
3612 :
public MatcherInterface<Container>,
3613 public UnorderedElementsAreMatcherImplBase {
3616 typedef internal::StlContainerView<RawContainer> View;
3617 typedef typename View::type StlContainer;
3618 typedef typename View::const_reference StlContainerReference;
3619 typedef typename StlContainer::value_type Element;
3621 template <
typename InputIter>
3622 UnorderedElementsAreMatcherImpl(UnorderedMatcherRequire::Flags matcher_flags,
3623 InputIter first, InputIter last)
3624 : UnorderedElementsAreMatcherImplBase(matcher_flags) {
3625 for (; first != last; ++first) {
3626 matchers_.push_back(MatcherCast<const Element&>(*first));
3628 for (
const auto& m : matchers_) {
3629 matcher_describers().push_back(m.GetDescriber());
3634 void DescribeTo(::std::ostream* os)
const override {
3635 return UnorderedElementsAreMatcherImplBase::DescribeToImpl(os);
3639 void DescribeNegationTo(::std::ostream* os)
const override {
3640 return UnorderedElementsAreMatcherImplBase::DescribeNegationToImpl(os);
3643 bool MatchAndExplain(Container container,
3644 MatchResultListener* listener)
const override {
3645 StlContainerReference stl_container = View::ConstReference(container);
3646 ::std::vector<std::string> element_printouts;
3647 MatchMatrix matrix =
3648 AnalyzeElements(stl_container.begin(), stl_container.end(),
3649 &element_printouts, listener);
3651 return VerifyMatchMatrix(element_printouts, matrix, listener) &&
3652 FindPairing(matrix, listener);
3656 template <
typename ElementIter>
3657 MatchMatrix AnalyzeElements(ElementIter elem_first, ElementIter elem_last,
3658 ::std::vector<std::string>* element_printouts,
3659 MatchResultListener* listener)
const {
3660 element_printouts->clear();
3661 ::std::vector<char> did_match;
3662 size_t num_elements = 0;
3663 DummyMatchResultListener dummy;
3664 for (; elem_first != elem_last; ++num_elements, ++elem_first) {
3665 if (listener->IsInterested()) {
3666 element_printouts->push_back(PrintToString(*elem_first));
3668 for (
size_t irhs = 0; irhs != matchers_.size(); ++irhs) {
3669 did_match.push_back(
3670 matchers_[irhs].MatchAndExplain(*elem_first, &dummy));
3674 MatchMatrix matrix(num_elements, matchers_.size());
3675 ::std::vector<char>::const_iterator did_match_iter = did_match.begin();
3676 for (
size_t ilhs = 0; ilhs != num_elements; ++ilhs) {
3677 for (
size_t irhs = 0; irhs != matchers_.size(); ++irhs) {
3678 matrix.SetEdge(ilhs, irhs, *did_match_iter++ != 0);
3684 ::std::vector<Matcher<const Element&>> matchers_;
3689template <
typename Target>
3690struct CastAndAppendTransform {
3691 template <
typename Arg>
3692 Matcher<Target> operator()(
const Arg& a)
const {
3693 return MatcherCast<Target>(a);
3698template <
typename MatcherTuple>
3699class UnorderedElementsAreMatcher {
3701 explicit UnorderedElementsAreMatcher(
const MatcherTuple& args)
3702 : matchers_(args) {}
3704 template <
typename Container>
3705 operator Matcher<Container>()
const {
3707 typedef typename internal::StlContainerView<RawContainer>::type View;
3708 typedef typename View::value_type Element;
3709 typedef ::std::vector<Matcher<const Element&>> MatcherVec;
3710 MatcherVec matchers;
3711 matchers.reserve(::std::tuple_size<MatcherTuple>::value);
3712 TransformTupleValues(CastAndAppendTransform<const Element&>(), matchers_,
3713 ::std::back_inserter(matchers));
3714 return Matcher<Container>(
3715 new UnorderedElementsAreMatcherImpl<const Container&>(
3716 UnorderedMatcherRequire::ExactMatch, matchers.begin(),
3721 const MatcherTuple matchers_;
3725template <
typename MatcherTuple>
3726class ElementsAreMatcher {
3728 explicit ElementsAreMatcher(
const MatcherTuple& args) : matchers_(args) {}
3730 template <
typename Container>
3731 operator Matcher<Container>()
const {
3734 ::std::tuple_size<MatcherTuple>::value < 2,
3735 "use UnorderedElementsAre with hash tables");
3738 typedef typename internal::StlContainerView<RawContainer>::type View;
3739 typedef typename View::value_type Element;
3740 typedef ::std::vector<Matcher<const Element&>> MatcherVec;
3741 MatcherVec matchers;
3742 matchers.reserve(::std::tuple_size<MatcherTuple>::value);
3743 TransformTupleValues(CastAndAppendTransform<const Element&>(), matchers_,
3744 ::std::back_inserter(matchers));
3745 return Matcher<Container>(
new ElementsAreMatcherImpl<const Container&>(
3746 matchers.begin(), matchers.end()));
3750 const MatcherTuple matchers_;
3754template <
typename T>
3755class UnorderedElementsAreArrayMatcher {
3757 template <
typename Iter>
3758 UnorderedElementsAreArrayMatcher(UnorderedMatcherRequire::Flags match_flags,
3759 Iter first, Iter last)
3760 : match_flags_(match_flags), matchers_(first, last) {}
3762 template <
typename Container>
3763 operator Matcher<Container>()
const {
3764 return Matcher<Container>(
3765 new UnorderedElementsAreMatcherImpl<const Container&>(
3766 match_flags_, matchers_.begin(), matchers_.end()));
3770 UnorderedMatcherRequire::Flags match_flags_;
3771 ::std::vector<T> matchers_;
3775template <
typename T>
3776class ElementsAreArrayMatcher {
3778 template <
typename Iter>
3779 ElementsAreArrayMatcher(Iter first, Iter last) : matchers_(first, last) {}
3781 template <
typename Container>
3782 operator Matcher<Container>()
const {
3785 "use UnorderedElementsAreArray with hash tables");
3787 return Matcher<Container>(
new ElementsAreMatcherImpl<const Container&>(
3788 matchers_.begin(), matchers_.end()));
3792 const ::std::vector<T> matchers_;
3804template <
typename Tuple2Matcher,
typename Second>
3805class BoundSecondMatcher {
3807 BoundSecondMatcher(
const Tuple2Matcher& tm,
const Second& second)
3808 : tuple2_matcher_(tm), second_value_(second) {}
3810 BoundSecondMatcher(
const BoundSecondMatcher& other) =
default;
3812 template <
typename T>
3813 operator Matcher<T>()
const {
3814 return MakeMatcher(
new Impl<T>(tuple2_matcher_, second_value_));
3825 void operator=(
const BoundSecondMatcher& ) {
3826 GTEST_LOG_(FATAL) <<
"BoundSecondMatcher should never be assigned.";
3830 template <
typename T>
3831 class Impl :
public MatcherInterface<T> {
3833 typedef ::std::tuple<T, Second> ArgTuple;
3835 Impl(
const Tuple2Matcher& tm,
const Second& second)
3836 : mono_tuple2_matcher_(SafeMatcherCast<const ArgTuple&>(tm)),
3837 second_value_(second) {}
3839 void DescribeTo(::std::ostream* os)
const override {
3841 UniversalPrint(second_value_, os);
3843 mono_tuple2_matcher_.DescribeTo(os);
3846 bool MatchAndExplain(
T x, MatchResultListener* listener)
const override {
3847 return mono_tuple2_matcher_.MatchAndExplain(ArgTuple(
x, second_value_),
3852 const Matcher<const ArgTuple&> mono_tuple2_matcher_;
3853 const Second second_value_;
3856 const Tuple2Matcher tuple2_matcher_;
3857 const Second second_value_;
3864template <
typename Tuple2Matcher,
typename Second>
3865BoundSecondMatcher<Tuple2Matcher, Second> MatcherBindSecond(
3866 const Tuple2Matcher& tm,
const Second& second) {
3867 return BoundSecondMatcher<Tuple2Matcher, Second>(tm, second);
3876 bool negation,
const char* matcher_name,
3877 const std::vector<const char*>& param_names,
const Strings& param_values);
3880template <
typename ValueMatcher>
3881class OptionalMatcher {
3883 explicit OptionalMatcher(
const ValueMatcher& value_matcher)
3884 : value_matcher_(value_matcher) {}
3886 template <
typename Optional>
3887 operator Matcher<Optional>()
const {
3888 return Matcher<Optional>(
new Impl<const Optional&>(value_matcher_));
3891 template <
typename Optional>
3892 class Impl :
public MatcherInterface<Optional> {
3895 typedef typename OptionalView::value_type ValueType;
3896 explicit Impl(
const ValueMatcher& value_matcher)
3897 : value_matcher_(MatcherCast<ValueType>(value_matcher)) {}
3899 void DescribeTo(::std::ostream* os)
const override {
3901 value_matcher_.DescribeTo(os);
3904 void DescribeNegationTo(::std::ostream* os)
const override {
3906 value_matcher_.DescribeNegationTo(os);
3909 bool MatchAndExplain(Optional optional,
3910 MatchResultListener* listener)
const override {
3912 *listener <<
"which is not engaged";
3915 const ValueType&
value = *optional;
3916 StringMatchResultListener value_listener;
3917 const bool match = value_matcher_.MatchAndExplain(
value, &value_listener);
3918 *listener <<
"whose value " << PrintToString(
value)
3919 << (match ?
" matches" :
" doesn't match");
3920 PrintIfNotEmpty(value_listener.str(), listener->stream());
3925 const Matcher<ValueType> value_matcher_;
3929 const ValueMatcher value_matcher_;
3932namespace variant_matcher {
3934template <
typename T>
3935void holds_alternative() {}
3936template <
typename T>
3940template <
typename T>
3941class VariantMatcher {
3944 : matcher_(std::move(matcher)) {}
3946 template <
typename Variant>
3947 bool MatchAndExplain(
const Variant&
value,
3948 ::testing::MatchResultListener* listener)
const {
3950 if (!listener->IsInterested()) {
3951 return holds_alternative<T>(
value) && matcher_.Matches(get<T>(
value));
3954 if (!holds_alternative<T>(
value)) {
3955 *listener <<
"whose value is not of type '" << GetTypeName() <<
"'";
3959 const T& elem = get<T>(
value);
3960 StringMatchResultListener elem_listener;
3961 const bool match = matcher_.MatchAndExplain(elem, &elem_listener);
3962 *listener <<
"whose value " << PrintToString(elem)
3963 << (match ?
" matches" :
" doesn't match");
3964 PrintIfNotEmpty(elem_listener.str(), listener->stream());
3968 void DescribeTo(std::ostream* os)
const {
3969 *os <<
"is a variant<> with value of type '" << GetTypeName()
3970 <<
"' and the value ";
3971 matcher_.DescribeTo(os);
3974 void DescribeNegationTo(std::ostream* os)
const {
3975 *os <<
"is a variant<> with value of type other than '" << GetTypeName()
3976 <<
"' or the value ";
3977 matcher_.DescribeNegationTo(os);
3981 static std::string GetTypeName() {
3984 return internal::GetTypeName<T>());
3986 return "the element type";
3989 const ::testing::Matcher<const T&> matcher_;
3994namespace any_cast_matcher {
3997template <
typename T>
4001template <
typename T>
4002class AnyCastMatcher {
4004 explicit AnyCastMatcher(const ::testing::Matcher<const T&>& matcher)
4005 : matcher_(matcher) {}
4007 template <
typename AnyType>
4008 bool MatchAndExplain(
const AnyType&
value,
4009 ::testing::MatchResultListener* listener)
const {
4010 if (!listener->IsInterested()) {
4011 const T* ptr = any_cast<T>(&
value);
4012 return ptr !=
nullptr && matcher_.Matches(*ptr);
4015 const T* elem = any_cast<T>(&
value);
4016 if (elem ==
nullptr) {
4017 *listener <<
"whose value is not of type '" << GetTypeName() <<
"'";
4021 StringMatchResultListener elem_listener;
4022 const bool match = matcher_.MatchAndExplain(*elem, &elem_listener);
4023 *listener <<
"whose value " << PrintToString(*elem)
4024 << (match ?
" matches" :
" doesn't match");
4025 PrintIfNotEmpty(elem_listener.str(), listener->stream());
4029 void DescribeTo(std::ostream* os)
const {
4030 *os <<
"is an 'any' type with value of type '" << GetTypeName()
4031 <<
"' and the value ";
4032 matcher_.DescribeTo(os);
4035 void DescribeNegationTo(std::ostream* os)
const {
4036 *os <<
"is an 'any' type with value of type other than '" << GetTypeName()
4037 <<
"' or the value ";
4038 matcher_.DescribeNegationTo(os);
4042 static std::string GetTypeName() {
4045 return internal::GetTypeName<T>());
4047 return "the element type";
4050 const ::testing::Matcher<const T&> matcher_;
4056template <
class ArgsTuple,
size_t... k>
4057class ArgsMatcherImpl :
public MatcherInterface<ArgsTuple> {
4059 using RawArgsTuple =
typename std::decay<ArgsTuple>::type;
4060 using SelectedArgs =
4061 std::tuple<typename std::tuple_element<k, RawArgsTuple>::type...>;
4062 using MonomorphicInnerMatcher = Matcher<const SelectedArgs&>;
4064 template <
typename InnerMatcher>
4065 explicit ArgsMatcherImpl(
const InnerMatcher& inner_matcher)
4066 : inner_matcher_(SafeMatcherCast<const SelectedArgs&>(inner_matcher)) {}
4068 bool MatchAndExplain(ArgsTuple args,
4069 MatchResultListener* listener)
const override {
4072 const SelectedArgs& selected_args =
4073 std::forward_as_tuple(std::get<k>(args)...);
4074 if (!listener->IsInterested())
return inner_matcher_.Matches(selected_args);
4076 PrintIndices(listener->stream());
4077 *listener <<
"are " << PrintToString(selected_args);
4079 StringMatchResultListener inner_listener;
4081 inner_matcher_.MatchAndExplain(selected_args, &inner_listener);
4082 PrintIfNotEmpty(inner_listener.str(), listener->stream());
4086 void DescribeTo(::std::ostream* os)
const override {
4087 *os <<
"are a tuple ";
4089 inner_matcher_.DescribeTo(os);
4092 void DescribeNegationTo(::std::ostream* os)
const override {
4093 *os <<
"are a tuple ";
4095 inner_matcher_.DescribeNegationTo(os);
4100 static void PrintIndices(::std::ostream* os) {
4101 *os <<
"whose fields (";
4102 const char* sep =
"";
4109 const char* dummy[] = {
4110 "", (
static_cast<void>(*os << sep <<
"#" << k), sep =
", ")...};
4115 MonomorphicInnerMatcher inner_matcher_;
4118template <
class InnerMatcher,
size_t... k>
4121 explicit ArgsMatcher(InnerMatcher inner_matcher)
4122 : inner_matcher_(std::move(inner_matcher)) {}
4124 template <
typename ArgsTuple>
4125 operator Matcher<ArgsTuple>()
const {
4126 return MakeMatcher(
new ArgsMatcherImpl<ArgsTuple, k...>(inner_matcher_));
4130 InnerMatcher inner_matcher_;
4150template <
typename Iter>
4151inline internal::ElementsAreArrayMatcher<
4152 typename ::std::iterator_traits<Iter>::value_type>
4153ElementsAreArray(Iter first, Iter last) {
4154 typedef typename ::std::iterator_traits<Iter>::value_type
T;
4155 return internal::ElementsAreArrayMatcher<T>(first, last);
4158template <
typename T>
4159inline auto ElementsAreArray(
const T* pointer,
size_t count)
4160 ->
decltype(ElementsAreArray(pointer, pointer +
count)) {
4161 return ElementsAreArray(pointer, pointer +
count);
4164template <
typename T,
size_t N>
4165inline auto ElementsAreArray(
const T (&array)[N])
4166 ->
decltype(ElementsAreArray(array, N)) {
4167 return ElementsAreArray(array, N);
4170template <
typename Container>
4171inline auto ElementsAreArray(
const Container& container)
4172 ->
decltype(ElementsAreArray(container.begin(), container.end())) {
4173 return ElementsAreArray(container.begin(), container.end());
4176template <
typename T>
4177inline auto ElementsAreArray(::std::initializer_list<T> xs)
4178 ->
decltype(ElementsAreArray(xs.begin(), xs.end())) {
4179 return ElementsAreArray(xs.begin(), xs.end());
4195template <
typename Iter>
4196inline internal::UnorderedElementsAreArrayMatcher<
4197 typename ::std::iterator_traits<Iter>::value_type>
4198UnorderedElementsAreArray(Iter first, Iter last) {
4199 typedef typename ::std::iterator_traits<Iter>::value_type
T;
4200 return internal::UnorderedElementsAreArrayMatcher<T>(
4201 internal::UnorderedMatcherRequire::ExactMatch, first, last);
4204template <
typename T>
4205inline internal::UnorderedElementsAreArrayMatcher<T> UnorderedElementsAreArray(
4206 const T* pointer,
size_t count) {
4207 return UnorderedElementsAreArray(pointer, pointer +
count);
4210template <
typename T,
size_t N>
4211inline internal::UnorderedElementsAreArrayMatcher<T> UnorderedElementsAreArray(
4212 const T (&array)[N]) {
4213 return UnorderedElementsAreArray(array, N);
4216template <
typename Container>
4217inline internal::UnorderedElementsAreArrayMatcher<
4218 typename Container::value_type>
4219UnorderedElementsAreArray(
const Container& container) {
4220 return UnorderedElementsAreArray(container.begin(), container.end());
4223template <
typename T>
4224inline internal::UnorderedElementsAreArrayMatcher<T> UnorderedElementsAreArray(
4225 ::std::initializer_list<T> xs) {
4226 return UnorderedElementsAreArray(xs.begin(), xs.end());
4238const internal::AnythingMatcher _ = {};
4240template <
typename T>
4241inline Matcher<T> A() {
4246template <
typename T>
4247inline Matcher<T> An() {
4251template <
typename T,
typename M>
4252Matcher<T> internal::MatcherCastImpl<T, M>::CastImpl(
4253 const M&
value, std::false_type ,
4259inline PolymorphicMatcher<internal::IsNullMatcher> IsNull() {
4260 return MakePolymorphicMatcher(internal::IsNullMatcher());
4266inline PolymorphicMatcher<internal::NotNullMatcher> NotNull() {
4267 return MakePolymorphicMatcher(internal::NotNullMatcher());
4272template <
typename T>
4273inline internal::RefMatcher<T&> Ref(
T&
x) {
4274 return internal::RefMatcher<T&>(
x);
4278inline PolymorphicMatcher<internal::IsNanMatcher> IsNan() {
4279 return MakePolymorphicMatcher(internal::IsNanMatcher());
4284inline internal::FloatingEqMatcher<double> DoubleEq(
double rhs) {
4285 return internal::FloatingEqMatcher<double>(rhs,
false);
4290inline internal::FloatingEqMatcher<double> NanSensitiveDoubleEq(
double rhs) {
4291 return internal::FloatingEqMatcher<double>(rhs,
true);
4297inline internal::FloatingEqMatcher<double> DoubleNear(
double rhs,
4298 double max_abs_error) {
4299 return internal::FloatingEqMatcher<double>(rhs,
false, max_abs_error);
4305inline internal::FloatingEqMatcher<double> NanSensitiveDoubleNear(
4306 double rhs,
double max_abs_error) {
4307 return internal::FloatingEqMatcher<double>(rhs,
true, max_abs_error);
4312inline internal::FloatingEqMatcher<float> FloatEq(
float rhs) {
4313 return internal::FloatingEqMatcher<float>(rhs,
false);
4318inline internal::FloatingEqMatcher<float> NanSensitiveFloatEq(
float rhs) {
4319 return internal::FloatingEqMatcher<float>(rhs,
true);
4325inline internal::FloatingEqMatcher<float> FloatNear(
float rhs,
4326 float max_abs_error) {
4327 return internal::FloatingEqMatcher<float>(rhs,
false, max_abs_error);
4333inline internal::FloatingEqMatcher<float> NanSensitiveFloatNear(
4334 float rhs,
float max_abs_error) {
4335 return internal::FloatingEqMatcher<float>(rhs,
true, max_abs_error);
4340template <
typename InnerMatcher>
4341inline internal::PointeeMatcher<InnerMatcher> Pointee(
4342 const InnerMatcher& inner_matcher) {
4343 return internal::PointeeMatcher<InnerMatcher>(inner_matcher);
4353template <
typename To>
4354inline PolymorphicMatcher<internal::WhenDynamicCastToMatcher<To>>
4355WhenDynamicCastTo(
const Matcher<To>& inner_matcher) {
4356 return MakePolymorphicMatcher(
4357 internal::WhenDynamicCastToMatcher<To>(inner_matcher));
4365template <
typename Class,
typename FieldType,
typename FieldMatcher>
4366inline PolymorphicMatcher<internal::FieldMatcher<Class, FieldType>> Field(
4367 FieldType Class::*field,
const FieldMatcher& matcher) {
4368 return MakePolymorphicMatcher(internal::FieldMatcher<Class, FieldType>(
4369 field, MatcherCast<const FieldType&>(matcher)));
4378template <
typename Class,
typename FieldType,
typename FieldMatcher>
4379inline PolymorphicMatcher<internal::FieldMatcher<Class, FieldType>> Field(
4380 const std::string& field_name, FieldType Class::*field,
4381 const FieldMatcher& matcher) {
4382 return MakePolymorphicMatcher(internal::FieldMatcher<Class, FieldType>(
4383 field_name, field, MatcherCast<const FieldType&>(matcher)));
4390template <
typename Class,
typename PropertyType,
typename PropertyMatcher>
4391inline PolymorphicMatcher<internal::PropertyMatcher<
4392 Class, PropertyType, PropertyType (Class::*)()
const>>
4393Property(PropertyType (Class::*property)()
const,
4394 const PropertyMatcher& matcher) {
4395 return MakePolymorphicMatcher(
4396 internal::PropertyMatcher<Class, PropertyType,
4397 PropertyType (Class::*)()
const>(
4398 property, MatcherCast<const PropertyType&>(matcher)));
4407template <
typename Class,
typename PropertyType,
typename PropertyMatcher>
4408inline PolymorphicMatcher<internal::PropertyMatcher<
4409 Class, PropertyType, PropertyType (Class::*)()
const>>
4410Property(
const std::string& property_name,
4411 PropertyType (Class::*property)()
const,
4412 const PropertyMatcher& matcher) {
4413 return MakePolymorphicMatcher(
4414 internal::PropertyMatcher<Class, PropertyType,
4415 PropertyType (Class::*)()
const>(
4416 property_name, property, MatcherCast<const PropertyType&>(matcher)));
4420template <
typename Class,
typename PropertyType,
typename PropertyMatcher>
4421inline PolymorphicMatcher<internal::PropertyMatcher<
4422 Class, PropertyType, PropertyType (Class::*)()
const&>>
4423Property(PropertyType (Class::*property)()
const&,
4424 const PropertyMatcher& matcher) {
4425 return MakePolymorphicMatcher(
4426 internal::PropertyMatcher<Class, PropertyType,
4427 PropertyType (Class::*)()
const&>(
4428 property, MatcherCast<const PropertyType&>(matcher)));
4432template <
typename Class,
typename PropertyType,
typename PropertyMatcher>
4433inline PolymorphicMatcher<internal::PropertyMatcher<
4434 Class, PropertyType, PropertyType (Class::*)()
const&>>
4435Property(
const std::string& property_name,
4436 PropertyType (Class::*property)()
const&,
4437 const PropertyMatcher& matcher) {
4438 return MakePolymorphicMatcher(
4439 internal::PropertyMatcher<Class, PropertyType,
4440 PropertyType (Class::*)()
const&>(
4441 property_name, property, MatcherCast<const PropertyType&>(matcher)));
4452template <
typename Callable,
typename InnerMatcher>
4453internal::ResultOfMatcher<Callable, InnerMatcher> ResultOf(
4454 Callable callable, InnerMatcher matcher) {
4455 return internal::ResultOfMatcher<Callable, InnerMatcher>(std::move(callable),
4456 std::move(matcher));
4461template <
typename Callable,
typename InnerMatcher>
4462internal::ResultOfMatcher<Callable, InnerMatcher> ResultOf(
4463 const std::string& result_description, Callable callable,
4464 InnerMatcher matcher) {
4465 return internal::ResultOfMatcher<Callable, InnerMatcher>(
4466 result_description, std::move(callable), std::move(matcher));
4472template <
typename T = std::
string>
4473PolymorphicMatcher<internal::StrEqualityMatcher<std::string>> StrEq(
4474 const internal::StringLike<T>& str) {
4475 return MakePolymorphicMatcher(
4476 internal::StrEqualityMatcher<std::string>(std::string(str),
true,
true));
4480template <
typename T = std::
string>
4481PolymorphicMatcher<internal::StrEqualityMatcher<std::string>> StrNe(
4482 const internal::StringLike<T>& str) {
4483 return MakePolymorphicMatcher(
4484 internal::StrEqualityMatcher<std::string>(std::string(str),
false,
true));
4488template <
typename T = std::
string>
4489PolymorphicMatcher<internal::StrEqualityMatcher<std::string>> StrCaseEq(
4490 const internal::StringLike<T>& str) {
4491 return MakePolymorphicMatcher(
4492 internal::StrEqualityMatcher<std::string>(std::string(str),
true,
false));
4496template <
typename T = std::
string>
4497PolymorphicMatcher<internal::StrEqualityMatcher<std::string>> StrCaseNe(
4498 const internal::StringLike<T>& str) {
4499 return MakePolymorphicMatcher(internal::StrEqualityMatcher<std::string>(
4500 std::string(str),
false,
false));
4505template <
typename T = std::
string>
4506PolymorphicMatcher<internal::HasSubstrMatcher<std::string>> HasSubstr(
4507 const internal::StringLike<T>& substring) {
4508 return MakePolymorphicMatcher(
4509 internal::HasSubstrMatcher<std::string>(std::string(substring)));
4513template <
typename T = std::
string>
4514PolymorphicMatcher<internal::StartsWithMatcher<std::string>> StartsWith(
4515 const internal::StringLike<T>& prefix) {
4516 return MakePolymorphicMatcher(
4517 internal::StartsWithMatcher<std::string>(std::string(prefix)));
4521template <
typename T = std::
string>
4522PolymorphicMatcher<internal::EndsWithMatcher<std::string>> EndsWith(
4523 const internal::StringLike<T>& suffix) {
4524 return MakePolymorphicMatcher(
4525 internal::EndsWithMatcher<std::string>(std::string(suffix)));
4528#if GTEST_HAS_STD_WSTRING
4532inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring>> StrEq(
4533 const std::wstring& str) {
4534 return MakePolymorphicMatcher(
4535 internal::StrEqualityMatcher<std::wstring>(str,
true,
true));
4539inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring>> StrNe(
4540 const std::wstring& str) {
4541 return MakePolymorphicMatcher(
4542 internal::StrEqualityMatcher<std::wstring>(str,
false,
true));
4546inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring>> StrCaseEq(
4547 const std::wstring& str) {
4548 return MakePolymorphicMatcher(
4549 internal::StrEqualityMatcher<std::wstring>(str,
true,
false));
4553inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring>> StrCaseNe(
4554 const std::wstring& str) {
4555 return MakePolymorphicMatcher(
4556 internal::StrEqualityMatcher<std::wstring>(str,
false,
false));
4561inline PolymorphicMatcher<internal::HasSubstrMatcher<std::wstring>> HasSubstr(
4562 const std::wstring& substring) {
4563 return MakePolymorphicMatcher(
4564 internal::HasSubstrMatcher<std::wstring>(substring));
4568inline PolymorphicMatcher<internal::StartsWithMatcher<std::wstring>> StartsWith(
4569 const std::wstring& prefix) {
4570 return MakePolymorphicMatcher(
4571 internal::StartsWithMatcher<std::wstring>(prefix));
4575inline PolymorphicMatcher<internal::EndsWithMatcher<std::wstring>> EndsWith(
4576 const std::wstring& suffix) {
4577 return MakePolymorphicMatcher(
4578 internal::EndsWithMatcher<std::wstring>(suffix));
4585inline internal::Eq2Matcher Eq() {
return internal::Eq2Matcher(); }
4589inline internal::Ge2Matcher Ge() {
return internal::Ge2Matcher(); }
4593inline internal::Gt2Matcher Gt() {
return internal::Gt2Matcher(); }
4597inline internal::Le2Matcher Le() {
return internal::Le2Matcher(); }
4601inline internal::Lt2Matcher Lt() {
return internal::Lt2Matcher(); }
4605inline internal::Ne2Matcher Ne() {
return internal::Ne2Matcher(); }
4609inline internal::FloatingEq2Matcher<float> FloatEq() {
4610 return internal::FloatingEq2Matcher<float>();
4615inline internal::FloatingEq2Matcher<double> DoubleEq() {
4616 return internal::FloatingEq2Matcher<double>();
4621inline internal::FloatingEq2Matcher<float> NanSensitiveFloatEq() {
4622 return internal::FloatingEq2Matcher<float>(
true);
4627inline internal::FloatingEq2Matcher<double> NanSensitiveDoubleEq() {
4628 return internal::FloatingEq2Matcher<double>(
true);
4633inline internal::FloatingEq2Matcher<float> FloatNear(
float max_abs_error) {
4634 return internal::FloatingEq2Matcher<float>(max_abs_error);
4639inline internal::FloatingEq2Matcher<double> DoubleNear(
double max_abs_error) {
4640 return internal::FloatingEq2Matcher<double>(max_abs_error);
4646inline internal::FloatingEq2Matcher<float> NanSensitiveFloatNear(
4647 float max_abs_error) {
4648 return internal::FloatingEq2Matcher<float>(max_abs_error,
true);
4654inline internal::FloatingEq2Matcher<double> NanSensitiveDoubleNear(
4655 double max_abs_error) {
4656 return internal::FloatingEq2Matcher<double>(max_abs_error,
true);
4661template <
typename InnerMatcher>
4662inline internal::NotMatcher<InnerMatcher> Not(InnerMatcher m) {
4663 return internal::NotMatcher<InnerMatcher>(m);
4669template <
typename Predicate>
4670inline PolymorphicMatcher<internal::TrulyMatcher<Predicate>> Truly(
4672 return MakePolymorphicMatcher(internal::TrulyMatcher<Predicate>(pred));
4681template <
typename SizeMatcher>
4682inline internal::SizeIsMatcher<SizeMatcher> SizeIs(
4683 const SizeMatcher& size_matcher) {
4684 return internal::SizeIsMatcher<SizeMatcher>(size_matcher);
4692template <
typename DistanceMatcher>
4693inline internal::BeginEndDistanceIsMatcher<DistanceMatcher> BeginEndDistanceIs(
4694 const DistanceMatcher& distance_matcher) {
4695 return internal::BeginEndDistanceIsMatcher<DistanceMatcher>(distance_matcher);
4702template <
typename Container>
4703inline PolymorphicMatcher<
4704 internal::ContainerEqMatcher<typename std::remove_const<Container>::type>>
4705ContainerEq(
const Container& rhs) {
4706 return MakePolymorphicMatcher(internal::ContainerEqMatcher<Container>(rhs));
4711template <
typename Comparator,
typename ContainerMatcher>
4712inline internal::WhenSortedByMatcher<Comparator, ContainerMatcher> WhenSortedBy(
4713 const Comparator& comparator,
const ContainerMatcher& container_matcher) {
4714 return internal::WhenSortedByMatcher<Comparator, ContainerMatcher>(
4715 comparator, container_matcher);
4720template <
typename ContainerMatcher>
4721inline internal::WhenSortedByMatcher<internal::LessComparator, ContainerMatcher>
4722WhenSorted(
const ContainerMatcher& container_matcher) {
4723 return internal::WhenSortedByMatcher<internal::LessComparator,
4725 internal::LessComparator(), container_matcher);
4734template <
typename TupleMatcher,
typename Container>
4735inline internal::PointwiseMatcher<TupleMatcher,
4736 typename std::remove_const<Container>::type>
4737Pointwise(
const TupleMatcher& tuple_matcher,
const Container& rhs) {
4738 return internal::PointwiseMatcher<TupleMatcher, Container>(tuple_matcher,
4743template <
typename TupleMatcher,
typename T>
4744inline internal::PointwiseMatcher<TupleMatcher, std::vector<T>> Pointwise(
4745 const TupleMatcher& tuple_matcher, std::initializer_list<T> rhs) {
4746 return Pointwise(tuple_matcher, std::vector<T>(rhs));
4760template <
typename Tuple2Matcher,
typename RhsContainer>
4761inline internal::UnorderedElementsAreArrayMatcher<
4762 typename internal::BoundSecondMatcher<
4764 typename internal::StlContainerView<
4765 typename std::remove_const<RhsContainer>::type>::type::value_type>>
4766UnorderedPointwise(
const Tuple2Matcher& tuple2_matcher,
4767 const RhsContainer& rhs_container) {
4770 typedef typename internal::StlContainerView<RhsContainer> RhsView;
4771 typedef typename RhsView::type RhsStlContainer;
4772 typedef typename RhsStlContainer::value_type Second;
4773 const RhsStlContainer& rhs_stl_container =
4774 RhsView::ConstReference(rhs_container);
4777 ::std::vector<internal::BoundSecondMatcher<Tuple2Matcher, Second>> matchers;
4778 for (
auto it = rhs_stl_container.begin(); it != rhs_stl_container.end();
4780 matchers.push_back(internal::MatcherBindSecond(tuple2_matcher, *it));
4784 return UnorderedElementsAreArray(matchers);
4788template <
typename Tuple2Matcher,
typename T>
4789inline internal::UnorderedElementsAreArrayMatcher<
4790 typename internal::BoundSecondMatcher<Tuple2Matcher, T>>
4791UnorderedPointwise(
const Tuple2Matcher& tuple2_matcher,
4792 std::initializer_list<T> rhs) {
4793 return UnorderedPointwise(tuple2_matcher, std::vector<T>(rhs));
4827template <
typename M>
4828inline internal::ContainsMatcher<M> Contains(M matcher) {
4829 return internal::ContainsMatcher<M>(matcher);
4859template <
typename Iter>
4860inline internal::UnorderedElementsAreArrayMatcher<
4861 typename ::std::iterator_traits<Iter>::value_type>
4862IsSupersetOf(Iter first, Iter last) {
4863 typedef typename ::std::iterator_traits<Iter>::value_type
T;
4864 return internal::UnorderedElementsAreArrayMatcher<T>(
4865 internal::UnorderedMatcherRequire::Superset, first, last);
4868template <
typename T>
4869inline internal::UnorderedElementsAreArrayMatcher<T> IsSupersetOf(
4870 const T* pointer,
size_t count) {
4871 return IsSupersetOf(pointer, pointer +
count);
4874template <
typename T,
size_t N>
4875inline internal::UnorderedElementsAreArrayMatcher<T> IsSupersetOf(
4876 const T (&array)[N]) {
4877 return IsSupersetOf(array, N);
4880template <
typename Container>
4881inline internal::UnorderedElementsAreArrayMatcher<
4882 typename Container::value_type>
4883IsSupersetOf(
const Container& container) {
4884 return IsSupersetOf(container.begin(), container.end());
4887template <
typename T>
4888inline internal::UnorderedElementsAreArrayMatcher<T> IsSupersetOf(
4889 ::std::initializer_list<T> xs) {
4890 return IsSupersetOf(xs.begin(), xs.end());
4916template <
typename Iter>
4917inline internal::UnorderedElementsAreArrayMatcher<
4918 typename ::std::iterator_traits<Iter>::value_type>
4919IsSubsetOf(Iter first, Iter last) {
4920 typedef typename ::std::iterator_traits<Iter>::value_type
T;
4921 return internal::UnorderedElementsAreArrayMatcher<T>(
4922 internal::UnorderedMatcherRequire::Subset, first, last);
4925template <
typename T>
4926inline internal::UnorderedElementsAreArrayMatcher<T> IsSubsetOf(
4927 const T* pointer,
size_t count) {
4928 return IsSubsetOf(pointer, pointer +
count);
4931template <
typename T,
size_t N>
4932inline internal::UnorderedElementsAreArrayMatcher<T> IsSubsetOf(
4933 const T (&array)[N]) {
4934 return IsSubsetOf(array, N);
4937template <
typename Container>
4938inline internal::UnorderedElementsAreArrayMatcher<
4939 typename Container::value_type>
4940IsSubsetOf(
const Container& container) {
4941 return IsSubsetOf(container.begin(), container.end());
4944template <
typename T>
4945inline internal::UnorderedElementsAreArrayMatcher<T> IsSubsetOf(
4946 ::std::initializer_list<T> xs) {
4947 return IsSubsetOf(xs.begin(), xs.end());
4977template <
typename M>
4978inline internal::EachMatcher<M> Each(M matcher) {
4979 return internal::EachMatcher<M>(matcher);
4985template <
typename M>
4986inline internal::KeyMatcher<M> Key(M inner_matcher) {
4987 return internal::KeyMatcher<M>(inner_matcher);
4995template <
typename FirstMatcher,
typename SecondMatcher>
4996inline internal::PairMatcher<FirstMatcher, SecondMatcher> Pair(
4997 FirstMatcher first_matcher, SecondMatcher second_matcher) {
4998 return internal::PairMatcher<FirstMatcher, SecondMatcher>(first_matcher,
5008template <
typename MatcherTrue,
typename MatcherFalse>
5009internal::ConditionalMatcher<MatcherTrue, MatcherFalse> Conditional(
5010 bool condition, MatcherTrue matcher_true, MatcherFalse matcher_false) {
5011 return internal::ConditionalMatcher<MatcherTrue, MatcherFalse>(
5012 condition, std::move(matcher_true), std::move(matcher_false));
5019template <
typename... M>
5020internal::FieldsAreMatcher<typename std::decay<M>::type...> FieldsAre(
5022 return internal::FieldsAreMatcher<typename std::decay<M>::type...>(
5023 std::forward<M>(matchers)...);
5028template <
typename InnerMatcher>
5029inline internal::PointerMatcher<InnerMatcher> Pointer(
5030 const InnerMatcher& inner_matcher) {
5031 return internal::PointerMatcher<InnerMatcher>(inner_matcher);
5036template <
typename InnerMatcher>
5037inline internal::AddressMatcher<InnerMatcher> Address(
5038 const InnerMatcher& inner_matcher) {
5039 return internal::AddressMatcher<InnerMatcher>(inner_matcher);
5044template <
typename MatcherType>
5045internal::WhenBase64UnescapedMatcher WhenBase64Unescaped(
5046 const MatcherType& internal_matcher) {
5047 return internal::WhenBase64UnescapedMatcher(internal_matcher);
5053template <
typename M>
5054inline internal::MatcherAsPredicate<M> Matches(M matcher) {
5055 return internal::MatcherAsPredicate<M>(matcher);
5059template <
typename T,
typename M>
5060inline bool Value(
const T&
value, M matcher) {
5061 return testing::Matches(matcher)(
value);
5066template <
typename T,
typename M>
5067inline bool ExplainMatchResult(M matcher,
const T&
value,
5068 MatchResultListener* listener) {
5069 return SafeMatcherCast<const T&>(matcher).MatchAndExplain(
value, listener);
5083template <
typename T,
typename M>
5084std::string DescribeMatcher(
const M& matcher,
bool negation =
false) {
5085 ::std::stringstream ss;
5086 Matcher<T> monomorphic_matcher = SafeMatcherCast<T>(matcher);
5088 monomorphic_matcher.DescribeNegationTo(&ss);
5090 monomorphic_matcher.DescribeTo(&ss);
5095template <
typename... Args>
5096internal::ElementsAreMatcher<
5097 std::tuple<typename std::decay<const Args&>::type...>>
5098ElementsAre(
const Args&... matchers) {
5099 return internal::ElementsAreMatcher<
5100 std::tuple<typename std::decay<const Args&>::type...>>(
5101 std::make_tuple(matchers...));
5104template <
typename... Args>
5105internal::UnorderedElementsAreMatcher<
5106 std::tuple<typename std::decay<const Args&>::type...>>
5107UnorderedElementsAre(
const Args&... matchers) {
5108 return internal::UnorderedElementsAreMatcher<
5109 std::tuple<typename std::decay<const Args&>::type...>>(
5110 std::make_tuple(matchers...));
5114template <
typename... Args>
5115internal::AllOfMatcher<typename std::decay<const Args&>::type...> AllOf(
5116 const Args&... matchers) {
5117 return internal::AllOfMatcher<typename std::decay<const Args&>::type...>(
5121template <
typename... Args>
5122internal::AnyOfMatcher<typename std::decay<const Args&>::type...> AnyOf(
5123 const Args&... matchers) {
5124 return internal::AnyOfMatcher<typename std::decay<const Args&>::type...>(
5150template <
typename Iter>
5151inline internal::AnyOfArrayMatcher<
5152 typename ::std::iterator_traits<Iter>::value_type>
5153AnyOfArray(Iter first, Iter last) {
5154 return internal::AnyOfArrayMatcher<
5155 typename ::std::iterator_traits<Iter>::value_type>(first, last);
5158template <
typename Iter>
5159inline internal::AllOfArrayMatcher<
5160 typename ::std::iterator_traits<Iter>::value_type>
5161AllOfArray(Iter first, Iter last) {
5162 return internal::AllOfArrayMatcher<
5163 typename ::std::iterator_traits<Iter>::value_type>(first, last);
5166template <
typename T>
5167inline internal::AnyOfArrayMatcher<T> AnyOfArray(
const T* ptr,
size_t count) {
5168 return AnyOfArray(ptr, ptr +
count);
5171template <
typename T>
5172inline internal::AllOfArrayMatcher<T> AllOfArray(
const T* ptr,
size_t count) {
5173 return AllOfArray(ptr, ptr +
count);
5176template <
typename T,
size_t N>
5177inline internal::AnyOfArrayMatcher<T> AnyOfArray(
const T (&array)[N]) {
5178 return AnyOfArray(array, N);
5181template <
typename T,
size_t N>
5182inline internal::AllOfArrayMatcher<T> AllOfArray(
const T (&array)[N]) {
5183 return AllOfArray(array, N);
5186template <
typename Container>
5187inline internal::AnyOfArrayMatcher<typename Container::value_type> AnyOfArray(
5188 const Container& container) {
5189 return AnyOfArray(container.begin(), container.end());
5192template <
typename Container>
5193inline internal::AllOfArrayMatcher<typename Container::value_type> AllOfArray(
5194 const Container& container) {
5195 return AllOfArray(container.begin(), container.end());
5198template <
typename T>
5199inline internal::AnyOfArrayMatcher<T> AnyOfArray(
5200 ::std::initializer_list<T> xs) {
5201 return AnyOfArray(xs.begin(), xs.end());
5204template <
typename T>
5205inline internal::AllOfArrayMatcher<T> AllOfArray(
5206 ::std::initializer_list<T> xs) {
5207 return AllOfArray(xs.begin(), xs.end());
5213template <
size_t... k,
typename InnerMatcher>
5214internal::ArgsMatcher<typename std::decay<InnerMatcher>::type, k...> Args(
5215 InnerMatcher&& matcher) {
5216 return internal::ArgsMatcher<typename std::decay<InnerMatcher>::type, k...>(
5217 std::forward<InnerMatcher>(matcher));
5227template <
typename InnerMatcher>
5228inline InnerMatcher AllArgs(
const InnerMatcher& matcher) {
5240template <
typename ValueMatcher>
5241inline internal::OptionalMatcher<ValueMatcher> Optional(
5242 const ValueMatcher& value_matcher) {
5243 return internal::OptionalMatcher<ValueMatcher>(value_matcher);
5247template <
typename T>
5248PolymorphicMatcher<internal::any_cast_matcher::AnyCastMatcher<T>> AnyWith(
5249 const Matcher<const T&>& matcher) {
5250 return MakePolymorphicMatcher(
5251 internal::any_cast_matcher::AnyCastMatcher<T>(matcher));
5258template <
typename T>
5259PolymorphicMatcher<internal::variant_matcher::VariantMatcher<T>> VariantWith(
5260 const Matcher<const T&>& matcher) {
5261 return MakePolymorphicMatcher(
5262 internal::variant_matcher::VariantMatcher<T>(matcher));
5265#if GTEST_HAS_EXCEPTIONS
5271class WithWhatMatcherImpl {
5273 WithWhatMatcherImpl(Matcher<std::string> matcher)
5274 : matcher_(std::move(matcher)) {}
5276 void DescribeTo(std::ostream* os)
const {
5277 *os <<
"contains .what() that ";
5278 matcher_.DescribeTo(os);
5281 void DescribeNegationTo(std::ostream* os)
const {
5282 *os <<
"contains .what() that does not ";
5283 matcher_.DescribeTo(os);
5286 template <
typename Err>
5287 bool MatchAndExplain(
const Err& err, MatchResultListener* listener)
const {
5288 *listener <<
"which contains .what() (of value = " << err.what()
5290 return matcher_.MatchAndExplain(err.what(), listener);
5294 const Matcher<std::string> matcher_;
5297inline PolymorphicMatcher<WithWhatMatcherImpl> WithWhat(
5298 Matcher<std::string> m) {
5299 return MakePolymorphicMatcher(WithWhatMatcherImpl(std::move(m)));
5302template <
typename Err>
5303class ExceptionMatcherImpl {
5306 const char* what()
const noexcept {
5307 return "this exception should never be thrown";
5332 using DefaultExceptionType =
typename std::conditional<
5333 std::is_same<
typename std::remove_cv<
5334 typename std::remove_reference<Err>::type>::type,
5336 const NeverThrown&,
const std::exception&>::type;
5339 ExceptionMatcherImpl(Matcher<const Err&> matcher)
5340 : matcher_(std::move(matcher)) {}
5342 void DescribeTo(std::ostream* os)
const {
5343 *os <<
"throws an exception which is a " << GetTypeName<Err>();
5345 matcher_.DescribeTo(os);
5348 void DescribeNegationTo(std::ostream* os)
const {
5349 *os <<
"throws an exception which is not a " << GetTypeName<Err>();
5351 matcher_.DescribeNegationTo(os);
5354 template <
typename T>
5355 bool MatchAndExplain(
T&&
x, MatchResultListener* listener)
const {
5357 (void)(std::forward<T>(
x)());
5358 }
catch (
const Err& err) {
5359 *listener <<
"throws an exception which is a " << GetTypeName<Err>();
5361 return matcher_.MatchAndExplain(err, listener);
5362 }
catch (DefaultExceptionType err) {
5364 *listener <<
"throws an exception of type " << GetTypeName(
typeid(err));
5367 *listener <<
"throws an std::exception-derived type ";
5369 *listener <<
"with description \"" << err.what() <<
"\"";
5372 *listener <<
"throws an exception of an unknown type";
5376 *listener <<
"does not throw any exception";
5381 const Matcher<const Err&> matcher_;
5408template <
typename Err>
5409PolymorphicMatcher<internal::ExceptionMatcherImpl<Err>> Throws() {
5410 return MakePolymorphicMatcher(
5411 internal::ExceptionMatcherImpl<Err>(A<const Err&>()));
5414template <
typename Err,
typename ExceptionMatcher>
5415PolymorphicMatcher<internal::ExceptionMatcherImpl<Err>> Throws(
5416 const ExceptionMatcher& exception_matcher) {
5420 return MakePolymorphicMatcher(internal::ExceptionMatcherImpl<Err>(
5421 SafeMatcherCast<const Err&>(exception_matcher)));
5424template <
typename Err,
typename MessageMatcher>
5425PolymorphicMatcher<internal::ExceptionMatcherImpl<Err>> ThrowsMessage(
5426 MessageMatcher&& message_matcher) {
5427 static_assert(std::is_base_of<std::exception, Err>::value,
5428 "expected an std::exception-derived type");
5429 return Throws<Err>(internal::WithWhat(
5430 MatcherCast<std::string>(std::forward<MessageMatcher>(message_matcher))));
5439#define ASSERT_THAT(value, matcher) \
5440 ASSERT_PRED_FORMAT1( \
5441 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
5442#define EXPECT_THAT(value, matcher) \
5443 EXPECT_PRED_FORMAT1( \
5444 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
5447#define MATCHER(name, description) \
5448 class name##Matcher \
5449 : public ::testing::internal::MatcherBaseImpl<name##Matcher> { \
5451 template <typename arg_type> \
5452 class gmock_Impl : public ::testing::MatcherInterface<const arg_type&> { \
5455 bool MatchAndExplain( \
5456 const arg_type& arg, \
5457 ::testing::MatchResultListener* result_listener) const override; \
5458 void DescribeTo(::std::ostream* gmock_os) const override { \
5459 *gmock_os << FormatDescription(false); \
5461 void DescribeNegationTo(::std::ostream* gmock_os) const override { \
5462 *gmock_os << FormatDescription(true); \
5466 ::std::string FormatDescription(bool negation) const { \
5468 ::std::string gmock_description = (description); \
5469 if (!gmock_description.empty()) { \
5470 return gmock_description; \
5472 return ::testing::internal::FormatMatcherDescription(negation, #name, \
5477 inline name##Matcher GMOCK_INTERNAL_WARNING_PUSH() \
5478 GMOCK_INTERNAL_WARNING_CLANG(ignored, "-Wunused-function") \
5479 GMOCK_INTERNAL_WARNING_CLANG(ignored, "-Wunused-member-function") \
5480 name GMOCK_INTERNAL_WARNING_POP()() { \
5483 template <typename arg_type> \
5484 bool name##Matcher::gmock_Impl<arg_type>::MatchAndExplain( \
5485 const arg_type& arg, \
5486 ::testing::MatchResultListener* result_listener GTEST_ATTRIBUTE_UNUSED_) \
5489#define MATCHER_P(name, p0, description) \
5490 GMOCK_INTERNAL_MATCHER(name, name##MatcherP, description, (#p0), (p0))
5491#define MATCHER_P2(name, p0, p1, description) \
5492 GMOCK_INTERNAL_MATCHER(name, name##MatcherP2, description, (#p0, #p1), \
5494#define MATCHER_P3(name, p0, p1, p2, description) \
5495 GMOCK_INTERNAL_MATCHER(name, name##MatcherP3, description, (#p0, #p1, #p2), \
5497#define MATCHER_P4(name, p0, p1, p2, p3, description) \
5498 GMOCK_INTERNAL_MATCHER(name, name##MatcherP4, description, \
5499 (#p0, #p1, #p2, #p3), (p0, p1, p2, p3))
5500#define MATCHER_P5(name, p0, p1, p2, p3, p4, description) \
5501 GMOCK_INTERNAL_MATCHER(name, name##MatcherP5, description, \
5502 (#p0, #p1, #p2, #p3, #p4), (p0, p1, p2, p3, p4))
5503#define MATCHER_P6(name, p0, p1, p2, p3, p4, p5, description) \
5504 GMOCK_INTERNAL_MATCHER(name, name##MatcherP6, description, \
5505 (#p0, #p1, #p2, #p3, #p4, #p5), \
5506 (p0, p1, p2, p3, p4, p5))
5507#define MATCHER_P7(name, p0, p1, p2, p3, p4, p5, p6, description) \
5508 GMOCK_INTERNAL_MATCHER(name, name##MatcherP7, description, \
5509 (#p0, #p1, #p2, #p3, #p4, #p5, #p6), \
5510 (p0, p1, p2, p3, p4, p5, p6))
5511#define MATCHER_P8(name, p0, p1, p2, p3, p4, p5, p6, p7, description) \
5512 GMOCK_INTERNAL_MATCHER(name, name##MatcherP8, description, \
5513 (#p0, #p1, #p2, #p3, #p4, #p5, #p6, #p7), \
5514 (p0, p1, p2, p3, p4, p5, p6, p7))
5515#define MATCHER_P9(name, p0, p1, p2, p3, p4, p5, p6, p7, p8, description) \
5516 GMOCK_INTERNAL_MATCHER(name, name##MatcherP9, description, \
5517 (#p0, #p1, #p2, #p3, #p4, #p5, #p6, #p7, #p8), \
5518 (p0, p1, p2, p3, p4, p5, p6, p7, p8))
5519#define MATCHER_P10(name, p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, description) \
5520 GMOCK_INTERNAL_MATCHER(name, name##MatcherP10, description, \
5521 (#p0, #p1, #p2, #p3, #p4, #p5, #p6, #p7, #p8, #p9), \
5522 (p0, p1, p2, p3, p4, p5, p6, p7, p8, p9))
5524#define GMOCK_INTERNAL_MATCHER(name, full_name, description, arg_names, args) \
5525 template <GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args)> \
5526 class full_name : public ::testing::internal::MatcherBaseImpl< \
5527 full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>> { \
5529 using full_name::MatcherBaseImpl::MatcherBaseImpl; \
5530 template <typename arg_type> \
5531 class gmock_Impl : public ::testing::MatcherInterface<const arg_type&> { \
5533 explicit gmock_Impl(GMOCK_INTERNAL_MATCHER_FUNCTION_ARGS(args)) \
5534 : GMOCK_INTERNAL_MATCHER_FORWARD_ARGS(args) {} \
5535 bool MatchAndExplain( \
5536 const arg_type& arg, \
5537 ::testing::MatchResultListener* result_listener) const override; \
5538 void DescribeTo(::std::ostream* gmock_os) const override { \
5539 *gmock_os << FormatDescription(false); \
5541 void DescribeNegationTo(::std::ostream* gmock_os) const override { \
5542 *gmock_os << FormatDescription(true); \
5544 GMOCK_INTERNAL_MATCHER_MEMBERS(args) \
5547 ::std::string FormatDescription(bool negation) const { \
5548 ::std::string gmock_description; \
5549 gmock_description = (description); \
5550 if (!gmock_description.empty()) { \
5551 return gmock_description; \
5553 return ::testing::internal::FormatMatcherDescription( \
5554 negation, #name, {GMOCK_PP_REMOVE_PARENS(arg_names)}, \
5555 ::testing::internal::UniversalTersePrintTupleFieldsToStrings( \
5556 ::std::tuple<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>( \
5557 GMOCK_INTERNAL_MATCHER_MEMBERS_USAGE(args)))); \
5561 template <GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args)> \
5562 inline full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)> name( \
5563 GMOCK_INTERNAL_MATCHER_FUNCTION_ARGS(args)) { \
5564 return full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>( \
5565 GMOCK_INTERNAL_MATCHER_ARGS_USAGE(args)); \
5567 template <GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args)> \
5568 template <typename arg_type> \
5569 bool full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>::gmock_Impl< \
5570 arg_type>::MatchAndExplain(const arg_type& arg, \
5571 ::testing::MatchResultListener* \
5572 result_listener GTEST_ATTRIBUTE_UNUSED_) \
5575#define GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args) \
5577 GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAM, , args))
5578#define GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAM(i_unused, data_unused, arg) \
5579 , typename arg##_type
5581#define GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args) \
5582 GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_TYPE_PARAM, , args))
5583#define GMOCK_INTERNAL_MATCHER_TYPE_PARAM(i_unused, data_unused, arg) \
5586#define GMOCK_INTERNAL_MATCHER_FUNCTION_ARGS(args) \
5587 GMOCK_PP_TAIL(dummy_first GMOCK_PP_FOR_EACH( \
5588 GMOCK_INTERNAL_MATCHER_FUNCTION_ARG, , args))
5589#define GMOCK_INTERNAL_MATCHER_FUNCTION_ARG(i, data_unused, arg) \
5590 , arg##_type gmock_p##i
5592#define GMOCK_INTERNAL_MATCHER_FORWARD_ARGS(args) \
5593 GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_FORWARD_ARG, , args))
5594#define GMOCK_INTERNAL_MATCHER_FORWARD_ARG(i, data_unused, arg) \
5595 , arg(::std::forward<arg##_type>(gmock_p##i))
5597#define GMOCK_INTERNAL_MATCHER_MEMBERS(args) \
5598 GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_MEMBER, , args)
5599#define GMOCK_INTERNAL_MATCHER_MEMBER(i_unused, data_unused, arg) \
5600 const arg##_type arg;
5602#define GMOCK_INTERNAL_MATCHER_MEMBERS_USAGE(args) \
5603 GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_MEMBER_USAGE, , args))
5604#define GMOCK_INTERNAL_MATCHER_MEMBER_USAGE(i_unused, data_unused, arg) , arg
5606#define GMOCK_INTERNAL_MATCHER_ARGS_USAGE(args) \
5607 GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_ARG_USAGE, , args))
5608#define GMOCK_INTERNAL_MATCHER_ARG_USAGE(i, data_unused, arg_unused) \
5612using namespace no_adl;