255 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_
256 #define GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_
260 #include <initializer_list>
267 #include <type_traits>
277 #if defined(_MSC_VER) && _MSC_VER >= 1915
278 #define GMOCK_MAYBE_5046_ 5046
280 #define GMOCK_MAYBE_5046_
303 class StringMatchResultListener :
public MatchResultListener {
305 StringMatchResultListener() : MatchResultListener(&ss_) {}
308 std::string str()
const {
return ss_.str(); }
311 void Clear() { ss_.str(
""); }
314 ::std::stringstream ss_;
333 template <
typename T,
typename M>
334 class MatcherCastImpl {
336 static Matcher<T> Cast(
const M& polymorphic_matcher_or_value) {
350 return CastImpl(polymorphic_matcher_or_value,
351 std::is_convertible<M, Matcher<T>>{},
352 std::is_convertible<M, T>{});
356 template <
bool Ignore>
357 static Matcher<T> CastImpl(
const M& polymorphic_matcher_or_value,
359 std::integral_constant<bool, Ignore>) {
368 return polymorphic_matcher_or_value;
374 static Matcher<T> CastImpl(
const M&
value,
377 return Matcher<T>(ImplicitCast_<T>(
value));
390 static Matcher<T> CastImpl(
const M& value,
398 template <
typename T,
typename U>
399 class MatcherCastImpl<T, Matcher<U> > {
401 static Matcher<T> Cast(
const Matcher<U>& source_matcher) {
402 return Matcher<T>(
new Impl(source_matcher));
406 class Impl :
public MatcherInterface<T> {
408 explicit Impl(
const Matcher<U>& source_matcher)
409 : source_matcher_(source_matcher) {}
412 bool MatchAndExplain(T
x, MatchResultListener* listener)
const override {
413 using FromType =
typename std::remove_cv<
typename std::remove_pointer<
414 typename std::remove_reference<T>::type>::type>::type;
415 using ToType =
typename std::remove_cv<
typename std::remove_pointer<
416 typename std::remove_reference<U>::type>::type>::type;
421 (std::is_pointer<
typename std::remove_reference<T>::type>::
value !=
422 std::is_pointer<
typename std::remove_reference<U>::type>::
value) ||
425 "Can't implicitly convert from <base> to <derived>");
433 return source_matcher_.MatchAndExplain(static_cast<CastType>(x),
437 void DescribeTo(::std::ostream* os)
const override {
438 source_matcher_.DescribeTo(os);
441 void DescribeNegationTo(::std::ostream* os)
const override {
442 source_matcher_.DescribeNegationTo(os);
446 const Matcher<U> source_matcher_;
452 template <
typename T>
453 class MatcherCastImpl<T, Matcher<T> > {
455 static Matcher<T> Cast(
const Matcher<T>& matcher) {
return matcher; }
459 template <
typename Derived>
460 class MatcherBaseImpl {
462 MatcherBaseImpl() =
default;
464 template <
typename T>
465 operator ::testing::Matcher<T>()
const {
466 return ::testing::Matcher<T>(
new
467 typename Derived::template gmock_Impl<T>());
472 template <
template <
typename...>
class Derived,
typename... Ts>
473 class MatcherBaseImpl<Derived<Ts...>> {
477 template <
typename E = std::enable_if<
sizeof...(Ts) == 1>,
478 typename E::type* =
nullptr>
479 explicit MatcherBaseImpl(Ts... params)
480 : params_(std::forward<Ts>(params)...) {}
481 template <
typename E = std::enable_if<
sizeof...(Ts) != 1>,
482 typename =
typename E::type>
483 MatcherBaseImpl(Ts... params)
484 : params_(std::forward<Ts>(params)...) {}
486 template <
typename F>
487 operator ::testing::Matcher<F>()
const {
488 return Apply<F>(MakeIndexSequence<
sizeof...(Ts)>{});
492 template <
typename F, std::size_t... tuple_ids>
494 return ::testing::Matcher<F>(
495 new typename Derived<Ts...>::template gmock_Impl<F>(
496 std::get<tuple_ids>(params_)...));
499 const std::tuple<Ts...> params_;
508 template <
typename T,
typename M>
509 inline Matcher<T> MatcherCast(
const M& matcher) {
510 return internal::MatcherCastImpl<T, M>::Cast(matcher);
515 template <
typename T,
typename M>
516 inline Matcher<T> SafeMatcherCast(
const M& polymorphic_matcher_or_value) {
517 return MatcherCast<T>(polymorphic_matcher_or_value);
529 template <
typename T,
typename U>
530 inline Matcher<T> SafeMatcherCast(
const Matcher<U>& matcher) {
533 "T must be implicitly convertible to U");
538 cannot_convert_non_reference_arg_to_reference);
546 kTIsOther || kUIsOther ||
548 conversion_of_arithmetic_types_must_be_lossless);
549 return MatcherCast<T>(matcher);
553 template <
typename T>
561 inline void PrintIfNotEmpty(
const std::string& explanation,
562 ::std::ostream* os) {
563 if (explanation !=
"" && os !=
nullptr) {
564 *os <<
", " << explanation;
571 inline bool IsReadableTypeName(
const std::string& type_name) {
574 return (type_name.length() <= 20 ||
575 type_name.find_first_of(
"<(") == std::string::npos);
583 template <
typename Value,
typename T>
584 bool MatchPrintAndExplain(Value&
value,
const Matcher<T>& matcher,
585 MatchResultListener* listener) {
586 if (!listener->IsInterested()) {
589 return matcher.Matches(value);
592 StringMatchResultListener inner_listener;
593 const bool match = matcher.MatchAndExplain(value, &inner_listener);
597 const std::string& type_name = GetTypeName<Value>();
598 if (IsReadableTypeName(type_name))
599 *listener->stream() <<
" (of type " << type_name <<
")";
601 PrintIfNotEmpty(inner_listener.str(), listener->stream());
614 template <
typename MatcherTuple,
typename ValueTuple>
615 static bool Matches(
const MatcherTuple& matcher_tuple,
616 const ValueTuple& value_tuple) {
617 return TuplePrefix<N - 1>::Matches(matcher_tuple, value_tuple) &&
618 std::get<
N - 1>(matcher_tuple).Matches(std::get<N - 1>(value_tuple));
625 template <
typename MatcherTuple,
typename ValueTuple>
626 static void ExplainMatchFailuresTo(
const MatcherTuple& matchers,
627 const ValueTuple& values,
628 ::std::ostream* os) {
630 TuplePrefix<N - 1>::ExplainMatchFailuresTo(matchers, values, os);
634 typename std::tuple_element<
N - 1, MatcherTuple>::type matcher =
635 std::get<N - 1>(matchers);
636 typedef typename std::tuple_element<N - 1, ValueTuple>::type Value;
637 const Value& value = std::get<N - 1>(values);
638 StringMatchResultListener listener;
639 if (!matcher.MatchAndExplain(value, &listener)) {
640 *os <<
" Expected arg #" << N - 1 <<
": ";
641 std::get<N - 1>(matchers).DescribeTo(os);
642 *os <<
"\n Actual: ";
649 PrintIfNotEmpty(listener.str(), os);
657 class TuplePrefix<0> {
659 template <
typename MatcherTuple,
typename ValueTuple>
660 static bool Matches(
const MatcherTuple& ,
661 const ValueTuple& ) {
665 template <
typename MatcherTuple,
typename ValueTuple>
666 static void ExplainMatchFailuresTo(
const MatcherTuple& ,
676 template <
typename MatcherTuple,
typename ValueTuple>
677 bool TupleMatches(
const MatcherTuple& matcher_tuple,
678 const ValueTuple& value_tuple) {
683 matcher_and_value_have_different_numbers_of_fields);
690 template <
typename MatcherTuple,
typename ValueTuple>
691 void ExplainMatchFailureTupleTo(
const MatcherTuple& matchers,
692 const ValueTuple& values,
693 ::std::ostream* os) {
695 matchers, values, os);
702 template <
typename Tuple,
typename Func,
typename OutIter>
703 class TransformTupleValuesHelper {
705 typedef ::std::tuple_size<Tuple> TupleSize;
710 static OutIter Run(Func
f,
const Tuple& t, OutIter out) {
711 return IterateOverTuple<Tuple, TupleSize::value>()(f, t, out);
715 template <
typename Tup,
size_t kRemainingSize>
716 struct IterateOverTuple {
717 OutIter operator() (Func f,
const Tup& t, OutIter out)
const {
718 *out++ = f(::std::get<TupleSize::value - kRemainingSize>(t));
719 return IterateOverTuple<Tup, kRemainingSize - 1>()(f, t, out);
722 template <
typename Tup>
723 struct IterateOverTuple<Tup, 0> {
724 OutIter operator() (Func ,
const Tup& , OutIter out)
const {
733 template <
typename Tuple,
typename Func,
typename OutIter>
734 OutIter TransformTupleValues(Func
f,
const Tuple& t, OutIter out) {
735 return TransformTupleValuesHelper<Tuple, Func, OutIter>::Run(f, t, out);
739 template <
typename T>
740 class AnyMatcherImpl :
public MatcherInterface<const T&> {
742 bool MatchAndExplain(
const T& ,
743 MatchResultListener* )
const override {
746 void DescribeTo(::std::ostream* os)
const override { *os <<
"is anything"; }
747 void DescribeNegationTo(::std::ostream* os)
const override {
751 *os <<
"never matches";
759 class AnythingMatcher {
761 template <
typename T>
762 operator Matcher<T>()
const {
return A<T>(); }
767 class IsNullMatcher {
769 template <
typename Po
inter>
770 bool MatchAndExplain(
const Pointer&
p,
771 MatchResultListener* )
const {
775 void DescribeTo(::std::ostream* os)
const { *os <<
"is NULL"; }
776 void DescribeNegationTo(::std::ostream* os)
const {
783 class NotNullMatcher {
785 template <
typename Po
inter>
786 bool MatchAndExplain(
const Pointer&
p,
787 MatchResultListener* )
const {
791 void DescribeTo(::std::ostream* os)
const { *os <<
"isn't NULL"; }
792 void DescribeNegationTo(::std::ostream* os)
const {
810 template <
typename T>
813 template <
typename T>
814 class RefMatcher<T&> {
824 explicit RefMatcher(T&
x) : object_(x) {}
826 template <
typename Super>
827 operator Matcher<Super&>()
const {
833 return MakeMatcher(
new Impl<Super>(object_));
837 template <
typename Super>
838 class Impl :
public MatcherInterface<Super&> {
840 explicit Impl(Super& x) : object_(x) {}
844 bool MatchAndExplain(Super& x,
845 MatchResultListener* listener)
const override {
846 *listener <<
"which is located @" <<
static_cast<const void*
>(&
x);
847 return &x == &object_;
850 void DescribeTo(::std::ostream* os)
const override {
851 *os <<
"references the variable ";
855 void DescribeNegationTo(::std::ostream* os)
const override {
856 *os <<
"does not reference the variable ";
861 const Super& object_;
868 inline bool CaseInsensitiveCStringEquals(
const char* lhs,
const char* rhs) {
869 return String::CaseInsensitiveCStringEquals(lhs, rhs);
872 inline bool CaseInsensitiveCStringEquals(
const wchar_t* lhs,
873 const wchar_t* rhs) {
874 return String::CaseInsensitiveWideCStringEquals(lhs, rhs);
879 template <
typename StringType>
880 bool CaseInsensitiveStringEquals(
const StringType& s1,
881 const StringType& s2) {
883 if (!CaseInsensitiveCStringEquals(s1.c_str(), s2.c_str())) {
888 const typename StringType::value_type nul = 0;
889 const size_t i1 = s1.find(nul), i2 = s2.find(nul);
892 if (i1 == StringType::npos || i2 == StringType::npos) {
897 return CaseInsensitiveStringEquals(s1.substr(i1 + 1), s2.substr(i2 + 1));
903 template <
typename StringType>
904 class StrEqualityMatcher {
906 StrEqualityMatcher(StringType str,
bool expect_eq,
bool case_sensitive)
907 : string_(std::move(str)),
908 expect_eq_(expect_eq),
909 case_sensitive_(case_sensitive) {}
911 #if GTEST_INTERNAL_HAS_STRING_VIEW
912 bool MatchAndExplain(
const internal::StringView& s,
913 MatchResultListener* listener)
const {
916 const StringType& str = std::string(s);
917 return MatchAndExplain(str, listener);
919 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
926 template <
typename CharType>
927 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
931 return MatchAndExplain(StringType(s), listener);
938 template <
typename MatcheeStringType>
939 bool MatchAndExplain(
const MatcheeStringType& s,
940 MatchResultListener* )
const {
941 const StringType s2(s);
942 const bool eq = case_sensitive_ ? s2 == string_ :
943 CaseInsensitiveStringEquals(s2, string_);
944 return expect_eq_ == eq;
947 void DescribeTo(::std::ostream* os)
const {
948 DescribeToHelper(expect_eq_, os);
951 void DescribeNegationTo(::std::ostream* os)
const {
952 DescribeToHelper(!expect_eq_, os);
956 void DescribeToHelper(
bool expect_eq, ::std::ostream* os)
const {
957 *os << (expect_eq ?
"is " :
"isn't ");
959 if (!case_sensitive_) {
960 *os <<
"(ignoring case) ";
965 const StringType string_;
966 const bool expect_eq_;
967 const bool case_sensitive_;
973 template <
typename StringType>
974 class HasSubstrMatcher {
976 explicit HasSubstrMatcher(
const StringType& substring)
977 : substring_(substring) {}
979 #if GTEST_INTERNAL_HAS_STRING_VIEW
980 bool MatchAndExplain(
const internal::StringView& s,
981 MatchResultListener* listener)
const {
984 const StringType& str = std::string(s);
985 return MatchAndExplain(str, listener);
987 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
994 template <
typename CharType>
995 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
996 return s !=
nullptr && MatchAndExplain(StringType(s), listener);
1003 template <
typename MatcheeStringType>
1004 bool MatchAndExplain(
const MatcheeStringType& s,
1005 MatchResultListener* )
const {
1006 return StringType(s).find(substring_) != StringType::npos;
1010 void DescribeTo(::std::ostream* os)
const {
1011 *os <<
"has substring ";
1015 void DescribeNegationTo(::std::ostream* os)
const {
1016 *os <<
"has no substring ";
1021 const StringType substring_;
1027 template <
typename StringType>
1028 class StartsWithMatcher {
1030 explicit StartsWithMatcher(
const StringType& prefix) : prefix_(prefix) {
1033 #if GTEST_INTERNAL_HAS_STRING_VIEW
1034 bool MatchAndExplain(
const internal::StringView& s,
1035 MatchResultListener* listener)
const {
1038 const StringType& str = std::string(s);
1039 return MatchAndExplain(str, listener);
1041 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1048 template <
typename CharType>
1049 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
1050 return s !=
nullptr && MatchAndExplain(StringType(s), listener);
1057 template <
typename MatcheeStringType>
1058 bool MatchAndExplain(
const MatcheeStringType& s,
1059 MatchResultListener* )
const {
1060 const StringType& s2(s);
1061 return s2.length() >= prefix_.length() &&
1062 s2.substr(0, prefix_.length()) == prefix_;
1065 void DescribeTo(::std::ostream* os)
const {
1066 *os <<
"starts with ";
1070 void DescribeNegationTo(::std::ostream* os)
const {
1071 *os <<
"doesn't start with ";
1076 const StringType prefix_;
1082 template <
typename StringType>
1083 class EndsWithMatcher {
1085 explicit EndsWithMatcher(
const StringType& suffix) : suffix_(suffix) {}
1087 #if GTEST_INTERNAL_HAS_STRING_VIEW
1088 bool MatchAndExplain(
const internal::StringView& s,
1089 MatchResultListener* listener)
const {
1092 const StringType& str = std::string(s);
1093 return MatchAndExplain(str, listener);
1095 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1102 template <
typename CharType>
1103 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
1104 return s !=
nullptr && MatchAndExplain(StringType(s), listener);
1111 template <
typename MatcheeStringType>
1112 bool MatchAndExplain(
const MatcheeStringType& s,
1113 MatchResultListener* )
const {
1114 const StringType& s2(s);
1115 return s2.length() >= suffix_.length() &&
1116 s2.substr(s2.length() - suffix_.length()) == suffix_;
1119 void DescribeTo(::std::ostream* os)
const {
1120 *os <<
"ends with ";
1124 void DescribeNegationTo(::std::ostream* os)
const {
1125 *os <<
"doesn't end with ";
1130 const StringType suffix_;
1141 template <
typename D,
typename Op>
1142 class PairMatchBase {
1144 template <
typename T1,
typename T2>
1145 operator Matcher<::std::tuple<T1, T2>>()
const {
1146 return Matcher<::std::tuple<T1, T2>>(
new Impl<const ::std::tuple<T1, T2>&>);
1148 template <
typename T1,
typename T2>
1149 operator Matcher<const ::std::tuple<T1, T2>&>()
const {
1150 return MakeMatcher(
new Impl<const ::std::tuple<T1, T2>&>);
1154 static ::std::ostream& GetDesc(::std::ostream& os) {
1155 return os << D::Desc();
1158 template <
typename Tuple>
1159 class Impl :
public MatcherInterface<Tuple> {
1161 bool MatchAndExplain(Tuple args,
1162 MatchResultListener* )
const override {
1163 return Op()(::std::get<0>(args), ::std::get<1>(args));
1165 void DescribeTo(::std::ostream* os)
const override {
1166 *os <<
"are " << GetDesc;
1168 void DescribeNegationTo(::std::ostream* os)
const override {
1169 *os <<
"aren't " << GetDesc;
1174 class Eq2Matcher :
public PairMatchBase<Eq2Matcher, AnyEq> {
1176 static const char* Desc() {
return "an equal pair"; }
1178 class Ne2Matcher :
public PairMatchBase<Ne2Matcher, AnyNe> {
1180 static const char* Desc() {
return "an unequal pair"; }
1182 class Lt2Matcher :
public PairMatchBase<Lt2Matcher, AnyLt> {
1184 static const char* Desc() {
return "a pair where the first < the second"; }
1186 class Gt2Matcher :
public PairMatchBase<Gt2Matcher, AnyGt> {
1188 static const char* Desc() {
return "a pair where the first > the second"; }
1190 class Le2Matcher :
public PairMatchBase<Le2Matcher, AnyLe> {
1192 static const char* Desc() {
return "a pair where the first <= the second"; }
1194 class Ge2Matcher :
public PairMatchBase<Ge2Matcher, AnyGe> {
1196 static const char* Desc() {
return "a pair where the first >= the second"; }
1203 template <
typename T>
1204 class NotMatcherImpl :
public MatcherInterface<const T&> {
1206 explicit NotMatcherImpl(
const Matcher<T>& matcher)
1207 : matcher_(matcher) {}
1209 bool MatchAndExplain(
const T&
x,
1210 MatchResultListener* listener)
const override {
1211 return !matcher_.MatchAndExplain(x, listener);
1214 void DescribeTo(::std::ostream* os)
const override {
1215 matcher_.DescribeNegationTo(os);
1218 void DescribeNegationTo(::std::ostream* os)
const override {
1219 matcher_.DescribeTo(os);
1223 const Matcher<T> matcher_;
1228 template <
typename InnerMatcher>
1231 explicit NotMatcher(InnerMatcher matcher) : matcher_(matcher) {}
1235 template <
typename T>
1236 operator Matcher<T>()
const {
1237 return Matcher<T>(
new NotMatcherImpl<T>(SafeMatcherCast<T>(matcher_)));
1241 InnerMatcher matcher_;
1248 template <
typename T>
1249 class AllOfMatcherImpl :
public MatcherInterface<const T&> {
1251 explicit AllOfMatcherImpl(std::vector<Matcher<T> > matchers)
1252 : matchers_(std::move(matchers)) {}
1254 void DescribeTo(::std::ostream* os)
const override {
1256 for (
size_t i = 0;
i < matchers_.size(); ++
i) {
1257 if (
i != 0) *os <<
") and (";
1258 matchers_[
i].DescribeTo(os);
1263 void DescribeNegationTo(::std::ostream* os)
const override {
1265 for (
size_t i = 0;
i < matchers_.size(); ++
i) {
1266 if (
i != 0) *os <<
") or (";
1267 matchers_[
i].DescribeNegationTo(os);
1272 bool MatchAndExplain(
const T&
x,
1273 MatchResultListener* listener)
const override {
1276 std::string all_match_result;
1278 for (
size_t i = 0;
i < matchers_.size(); ++
i) {
1279 StringMatchResultListener slistener;
1280 if (matchers_[
i].MatchAndExplain(x, &slistener)) {
1281 if (all_match_result.empty()) {
1282 all_match_result = slistener.str();
1284 std::string result = slistener.str();
1285 if (!result.empty()) {
1286 all_match_result +=
", and ";
1287 all_match_result += result;
1291 *listener << slistener.str();
1297 *listener << all_match_result;
1302 const std::vector<Matcher<T> > matchers_;
1309 template <
template <
typename T>
class CombiningMatcher,
typename... Args>
1310 class VariadicMatcher {
1312 VariadicMatcher(
const Args&... matchers)
1313 : matchers_(matchers...) {
1314 static_assert(
sizeof...(Args) > 0,
"Must have at least one matcher.");
1317 VariadicMatcher(
const VariadicMatcher&) =
default;
1318 VariadicMatcher& operator=(
const VariadicMatcher&) =
delete;
1323 template <
typename T>
1324 operator Matcher<T>()
const {
1325 std::vector<Matcher<T> > values;
1326 CreateVariadicMatcher<T>(&values, std::integral_constant<size_t, 0>());
1327 return Matcher<T>(
new CombiningMatcher<T>(std::move(values)));
1331 template <
typename T,
size_t I>
1332 void CreateVariadicMatcher(std::vector<Matcher<T> >* values,
1333 std::integral_constant<size_t, I>)
const {
1334 values->push_back(SafeMatcherCast<T>(std::get<I>(matchers_)));
1335 CreateVariadicMatcher<T>(values, std::integral_constant<size_t, I + 1>());
1338 template <
typename T>
1339 void CreateVariadicMatcher(
1340 std::vector<Matcher<T> >*,
1341 std::integral_constant<
size_t,
sizeof...(Args)>)
const {}
1343 std::tuple<Args...> matchers_;
1346 template <
typename... Args>
1347 using AllOfMatcher = VariadicMatcher<AllOfMatcherImpl, Args...>;
1353 template <
typename T>
1354 class AnyOfMatcherImpl :
public MatcherInterface<const T&> {
1356 explicit AnyOfMatcherImpl(std::vector<Matcher<T> > matchers)
1357 : matchers_(std::move(matchers)) {}
1359 void DescribeTo(::std::ostream* os)
const override {
1361 for (
size_t i = 0;
i < matchers_.size(); ++
i) {
1362 if (
i != 0) *os <<
") or (";
1363 matchers_[
i].DescribeTo(os);
1368 void DescribeNegationTo(::std::ostream* os)
const override {
1370 for (
size_t i = 0;
i < matchers_.size(); ++
i) {
1371 if (
i != 0) *os <<
") and (";
1372 matchers_[
i].DescribeNegationTo(os);
1377 bool MatchAndExplain(
const T&
x,
1378 MatchResultListener* listener)
const override {
1379 std::string no_match_result;
1383 for (
size_t i = 0;
i < matchers_.size(); ++
i) {
1384 StringMatchResultListener slistener;
1385 if (matchers_[
i].MatchAndExplain(x, &slistener)) {
1386 *listener << slistener.str();
1389 if (no_match_result.empty()) {
1390 no_match_result = slistener.str();
1392 std::string result = slistener.str();
1393 if (!result.empty()) {
1394 no_match_result +=
", and ";
1395 no_match_result += result;
1402 *listener << no_match_result;
1407 const std::vector<Matcher<T> > matchers_;
1411 template <
typename... Args>
1412 using AnyOfMatcher = VariadicMatcher<AnyOfMatcherImpl, Args...>;
1415 template <
template <
class>
class MatcherImpl,
typename T>
1416 class SomeOfArrayMatcher {
1420 template <
typename Iter>
1421 SomeOfArrayMatcher(Iter first, Iter last) : matchers_(first, last) {}
1423 template <
typename U>
1424 operator Matcher<U>()
const {
1425 using RawU =
typename std::decay<U>::type;
1426 std::vector<Matcher<RawU>> matchers;
1427 for (
const auto& matcher : matchers_) {
1428 matchers.push_back(MatcherCast<RawU>(matcher));
1430 return Matcher<U>(
new MatcherImpl<RawU>(std::move(matchers)));
1434 const ::std::vector<T> matchers_;
1437 template <
typename T>
1438 using AllOfArrayMatcher = SomeOfArrayMatcher<AllOfMatcherImpl, T>;
1440 template <
typename T>
1441 using AnyOfArrayMatcher = SomeOfArrayMatcher<AnyOfMatcherImpl, T>;
1445 template <
typename Predicate>
1446 class TrulyMatcher {
1448 explicit TrulyMatcher(Predicate pred) : predicate_(pred) {}
1454 template <
typename T>
1455 bool MatchAndExplain(T&
x,
1456 MatchResultListener* )
const {
1468 void DescribeTo(::std::ostream* os)
const {
1469 *os <<
"satisfies the given predicate";
1472 void DescribeNegationTo(::std::ostream* os)
const {
1473 *os <<
"doesn't satisfy the given predicate";
1477 Predicate predicate_;
1482 template <
typename M>
1483 class MatcherAsPredicate {
1485 explicit MatcherAsPredicate(M matcher) : matcher_(matcher) {}
1493 template <
typename T>
1494 bool operator()(
const T&
x)
const {
1509 return MatcherCast<const T&>(matcher_).Matches(x);
1518 template <
typename M>
1519 class PredicateFormatterFromMatcher {
1521 explicit PredicateFormatterFromMatcher(M m) : matcher_(std::move(m)) {}
1526 template <
typename T>
1527 AssertionResult operator()(
const char* value_text,
const T&
x)
const {
1539 const Matcher<const T&> matcher = SafeMatcherCast<const T&>(matcher_);
1543 if (matcher.Matches(x)) {
1547 ::std::stringstream ss;
1548 ss <<
"Value of: " << value_text <<
"\n"
1550 matcher.DescribeTo(&ss);
1553 StringMatchResultListener listener;
1554 if (MatchPrintAndExplain(x, matcher, &listener)) {
1555 ss <<
"\n The matcher failed on the initial attempt; but passed when "
1556 "rerun to generate the explanation.";
1558 ss <<
"\n Actual: " << listener.str();
1570 template <
typename M>
1571 inline PredicateFormatterFromMatcher<M>
1572 MakePredicateFormatterFromMatcher(M matcher) {
1573 return PredicateFormatterFromMatcher<M>(std::move(matcher));
1578 class IsNanMatcher {
1580 template <
typename FloatType>
1581 bool MatchAndExplain(
const FloatType& f,
1582 MatchResultListener* )
const {
1583 return (::std::isnan)(f);
1586 void DescribeTo(::std::ostream* os)
const { *os <<
"is NaN"; }
1587 void DescribeNegationTo(::std::ostream* os)
const {
1596 template <
typename FloatType>
1597 class FloatingEqMatcher {
1605 FloatingEqMatcher(FloatType
expected,
bool nan_eq_nan) :
1606 expected_(expected), nan_eq_nan_(nan_eq_nan), max_abs_error_(-1) {
1612 FloatingEqMatcher(FloatType expected,
bool nan_eq_nan,
1613 FloatType max_abs_error)
1614 : expected_(expected),
1615 nan_eq_nan_(nan_eq_nan),
1616 max_abs_error_(max_abs_error) {
1618 <<
", where max_abs_error is" << max_abs_error;
1622 template <
typename T>
1623 class Impl :
public MatcherInterface<T> {
1625 Impl(FloatType expected,
bool nan_eq_nan, FloatType max_abs_error)
1626 : expected_(expected),
1627 nan_eq_nan_(nan_eq_nan),
1628 max_abs_error_(max_abs_error) {}
1630 bool MatchAndExplain(T value,
1631 MatchResultListener* listener)
const override {
1632 const FloatingPoint<FloatType> actual(value),
expected(expected_);
1635 if (actual.is_nan() || expected.is_nan()) {
1636 if (actual.is_nan() && expected.is_nan()) {
1642 if (HasMaxAbsError()) {
1647 if (value == expected_) {
1651 const FloatType
diff = value - expected_;
1652 if (::
std::fabs(diff) <= max_abs_error_) {
1656 if (listener->IsInterested()) {
1657 *listener <<
"which is " << diff <<
" from " << expected_;
1661 return actual.AlmostEquals(expected);
1665 void DescribeTo(::std::ostream* os)
const override {
1669 const ::std::streamsize old_precision = os->precision(
1670 ::std::numeric_limits<FloatType>::digits10 + 2);
1671 if (FloatingPoint<FloatType>(expected_).is_nan()) {
1675 *os <<
"never matches";
1678 *os <<
"is approximately " << expected_;
1679 if (HasMaxAbsError()) {
1680 *os <<
" (absolute error <= " << max_abs_error_ <<
")";
1683 os->precision(old_precision);
1686 void DescribeNegationTo(::std::ostream* os)
const override {
1688 const ::std::streamsize old_precision = os->precision(
1689 ::std::numeric_limits<FloatType>::digits10 + 2);
1690 if (FloatingPoint<FloatType>(expected_).is_nan()) {
1694 *os <<
"is anything";
1697 *os <<
"isn't approximately " << expected_;
1698 if (HasMaxAbsError()) {
1699 *os <<
" (absolute error > " << max_abs_error_ <<
")";
1703 os->precision(old_precision);
1707 bool HasMaxAbsError()
const {
1708 return max_abs_error_ >= 0;
1711 const FloatType expected_;
1712 const bool nan_eq_nan_;
1714 const FloatType max_abs_error_;
1723 operator Matcher<FloatType>()
const {
1725 new Impl<FloatType>(expected_, nan_eq_nan_, max_abs_error_));
1728 operator Matcher<const FloatType&>()
const {
1730 new Impl<const FloatType&>(expected_, nan_eq_nan_, max_abs_error_));
1733 operator Matcher<FloatType&>()
const {
1735 new Impl<FloatType&>(expected_, nan_eq_nan_, max_abs_error_));
1739 const FloatType expected_;
1740 const bool nan_eq_nan_;
1742 const FloatType max_abs_error_;
1750 template <
typename FloatType>
1751 class FloatingEq2Matcher {
1753 FloatingEq2Matcher() { Init(-1,
false); }
1755 explicit FloatingEq2Matcher(
bool nan_eq_nan) { Init(-1, nan_eq_nan); }
1757 explicit FloatingEq2Matcher(FloatType max_abs_error) {
1758 Init(max_abs_error,
false);
1761 FloatingEq2Matcher(FloatType max_abs_error,
bool nan_eq_nan) {
1762 Init(max_abs_error, nan_eq_nan);
1765 template <
typename T1,
typename T2>
1766 operator Matcher<::std::tuple<T1, T2>>()
const {
1768 new Impl<::std::tuple<T1, T2>>(max_abs_error_, nan_eq_nan_));
1770 template <
typename T1,
typename T2>
1771 operator Matcher<const ::std::tuple<T1, T2>&>()
const {
1773 new Impl<const ::std::tuple<T1, T2>&>(max_abs_error_, nan_eq_nan_));
1777 static ::std::ostream& GetDesc(::std::ostream& os) {
1778 return os <<
"an almost-equal pair";
1781 template <
typename Tuple>
1782 class Impl :
public MatcherInterface<Tuple> {
1784 Impl(FloatType max_abs_error,
bool nan_eq_nan) :
1785 max_abs_error_(max_abs_error),
1786 nan_eq_nan_(nan_eq_nan) {}
1788 bool MatchAndExplain(Tuple args,
1789 MatchResultListener* listener)
const override {
1790 if (max_abs_error_ == -1) {
1791 FloatingEqMatcher<FloatType> fm(::std::get<0>(args), nan_eq_nan_);
1792 return static_cast<Matcher<FloatType>
>(fm).MatchAndExplain(
1793 ::std::get<1>(args), listener);
1795 FloatingEqMatcher<FloatType> fm(::std::get<0>(args), nan_eq_nan_,
1797 return static_cast<Matcher<FloatType>
>(fm).MatchAndExplain(
1798 ::std::get<1>(args), listener);
1801 void DescribeTo(::std::ostream* os)
const override {
1802 *os <<
"are " << GetDesc;
1804 void DescribeNegationTo(::std::ostream* os)
const override {
1805 *os <<
"aren't " << GetDesc;
1809 FloatType max_abs_error_;
1810 const bool nan_eq_nan_;
1813 void Init(FloatType max_abs_error_val,
bool nan_eq_nan_val) {
1814 max_abs_error_ = max_abs_error_val;
1815 nan_eq_nan_ = nan_eq_nan_val;
1817 FloatType max_abs_error_;
1823 template <
typename InnerMatcher>
1824 class PointeeMatcher {
1826 explicit PointeeMatcher(
const InnerMatcher& matcher) : matcher_(matcher) {}
1836 template <
typename Po
inter>
1837 operator Matcher<Pointer>()
const {
1838 return Matcher<Pointer>(
new Impl<const Pointer&>(matcher_));
1843 template <
typename Po
inter>
1844 class Impl :
public MatcherInterface<Pointer> {
1846 typedef typename PointeeOf<GTEST_REMOVE_REFERENCE_AND_CONST_(Pointer)>::type
1849 explicit Impl(
const InnerMatcher& matcher)
1850 : matcher_(MatcherCast<const Pointee&>(matcher)) {}
1852 void DescribeTo(::std::ostream* os)
const override {
1853 *os <<
"points to a value that ";
1854 matcher_.DescribeTo(os);
1857 void DescribeNegationTo(::std::ostream* os)
const override {
1858 *os <<
"does not point to a value that ";
1859 matcher_.DescribeTo(os);
1862 bool MatchAndExplain(Pointer pointer,
1863 MatchResultListener* listener)
const override {
1866 *listener <<
"which points to ";
1867 return MatchPrintAndExplain(*pointer, matcher_, listener);
1871 const Matcher<const Pointee&> matcher_;
1874 const InnerMatcher matcher_;
1884 template <
typename To>
1885 class WhenDynamicCastToMatcherBase {
1887 explicit WhenDynamicCastToMatcherBase(
const Matcher<To>& matcher)
1888 : matcher_(matcher) {}
1890 void DescribeTo(::std::ostream* os)
const {
1891 GetCastTypeDescription(os);
1892 matcher_.DescribeTo(os);
1895 void DescribeNegationTo(::std::ostream* os)
const {
1896 GetCastTypeDescription(os);
1897 matcher_.DescribeNegationTo(os);
1901 const Matcher<To> matcher_;
1903 static std::string GetToName() {
1904 return GetTypeName<To>();
1908 static void GetCastTypeDescription(::std::ostream* os) {
1909 *os <<
"when dynamic_cast to " << GetToName() <<
", ";
1915 template <
typename To>
1916 class WhenDynamicCastToMatcher :
public WhenDynamicCastToMatcherBase<To> {
1918 explicit WhenDynamicCastToMatcher(
const Matcher<To>& matcher)
1919 : WhenDynamicCastToMatcherBase<To>(matcher) {}
1921 template <
typename From>
1922 bool MatchAndExplain(From from, MatchResultListener* listener)
const {
1923 To to =
dynamic_cast<To
>(from);
1924 return MatchPrintAndExplain(to, this->matcher_, listener);
1930 template <
typename To>
1931 class WhenDynamicCastToMatcher<To&> :
public WhenDynamicCastToMatcherBase<To&> {
1933 explicit WhenDynamicCastToMatcher(
const Matcher<To&>& matcher)
1934 : WhenDynamicCastToMatcherBase<To&>(matcher) {}
1936 template <
typename From>
1937 bool MatchAndExplain(From& from, MatchResultListener* listener)
const {
1939 To* to =
dynamic_cast<To*
>(&from);
1940 if (to ==
nullptr) {
1941 *listener <<
"which cannot be dynamic_cast to " << this->GetToName();
1944 return MatchPrintAndExplain(*to, this->matcher_, listener);
1947 #endif // GTEST_HAS_RTTI
1951 template <
typename Class,
typename FieldType>
1952 class FieldMatcher {
1954 FieldMatcher(FieldType Class::*field,
1955 const Matcher<const FieldType&>& matcher)
1956 : field_(field), matcher_(matcher), whose_field_(
"whose given field ") {}
1958 FieldMatcher(
const std::string& field_name, FieldType Class::*field,
1959 const Matcher<const FieldType&>& matcher)
1962 whose_field_(
"whose field `" + field_name +
"` ") {}
1964 void DescribeTo(::std::ostream* os)
const {
1965 *os <<
"is an object " << whose_field_;
1966 matcher_.DescribeTo(os);
1969 void DescribeNegationTo(::std::ostream* os)
const {
1970 *os <<
"is an object " << whose_field_;
1971 matcher_.DescribeNegationTo(os);
1974 template <
typename T>
1975 bool MatchAndExplain(
const T& value, MatchResultListener* listener)
const {
1978 return MatchAndExplainImpl(
1979 typename std::is_pointer<
typename std::remove_const<T>::type>::type(),
1984 bool MatchAndExplainImpl(std::false_type ,
1986 MatchResultListener* listener)
const {
1987 *listener << whose_field_ <<
"is ";
1988 return MatchPrintAndExplain(obj.*field_, matcher_, listener);
1991 bool MatchAndExplainImpl(std::true_type ,
const Class*
p,
1992 MatchResultListener* listener)
const {
1993 if (p ==
nullptr)
return false;
1995 *listener <<
"which points to an object ";
1999 return MatchAndExplainImpl(std::false_type(), *p, listener);
2002 const FieldType Class::*field_;
2003 const Matcher<const FieldType&> matcher_;
2007 const std::string whose_field_;
2015 template <
typename Class,
typename PropertyType,
typename Property>
2016 class PropertyMatcher {
2018 typedef const PropertyType& RefToConstProperty;
2020 PropertyMatcher(Property property,
const Matcher<RefToConstProperty>& matcher)
2021 : property_(property),
2023 whose_property_(
"whose given property ") {}
2025 PropertyMatcher(
const std::string& property_name, Property property,
2026 const Matcher<RefToConstProperty>& matcher)
2027 : property_(property),
2029 whose_property_(
"whose property `" + property_name +
"` ") {}
2031 void DescribeTo(::std::ostream* os)
const {
2032 *os <<
"is an object " << whose_property_;
2033 matcher_.DescribeTo(os);
2036 void DescribeNegationTo(::std::ostream* os)
const {
2037 *os <<
"is an object " << whose_property_;
2038 matcher_.DescribeNegationTo(os);
2041 template <
typename T>
2042 bool MatchAndExplain(
const T&value, MatchResultListener* listener)
const {
2043 return MatchAndExplainImpl(
2044 typename std::is_pointer<
typename std::remove_const<T>::type>::type(),
2049 bool MatchAndExplainImpl(std::false_type ,
2051 MatchResultListener* listener)
const {
2052 *listener << whose_property_ <<
"is ";
2055 RefToConstProperty result = (obj.*property_)();
2056 return MatchPrintAndExplain(result, matcher_, listener);
2059 bool MatchAndExplainImpl(std::true_type ,
const Class*
p,
2060 MatchResultListener* listener)
const {
2061 if (p ==
nullptr)
return false;
2063 *listener <<
"which points to an object ";
2067 return MatchAndExplainImpl(std::false_type(), *p, listener);
2071 const Matcher<RefToConstProperty> matcher_;
2075 const std::string whose_property_;
2080 template <
typename Functor>
2081 struct CallableTraits {
2082 typedef Functor StorageType;
2084 static void CheckIsValid(Functor ) {}
2086 template <
typename T>
2087 static auto Invoke(Functor f,
const T& arg) -> decltype(f(arg)) {
2093 template <
typename ArgType,
typename ResType>
2094 struct CallableTraits<ResType(*)(ArgType)> {
2095 typedef ResType ResultType;
2096 typedef ResType(*StorageType)(ArgType);
2098 static void CheckIsValid(ResType(*f)(ArgType)) {
2100 <<
"NULL function pointer is passed into ResultOf().";
2102 template <
typename T>
2103 static ResType
Invoke(ResType(*f)(ArgType), T arg) {
2110 template <
typename Callable,
typename InnerMatcher>
2111 class ResultOfMatcher {
2113 ResultOfMatcher(Callable callable, InnerMatcher matcher)
2114 : callable_(std::move(callable)), matcher_(std::move(matcher)) {
2115 CallableTraits<Callable>::CheckIsValid(callable_);
2118 template <
typename T>
2119 operator Matcher<T>()
const {
2120 return Matcher<T>(
new Impl<const T&>(callable_, matcher_));
2124 typedef typename CallableTraits<Callable>::StorageType CallableStorageType;
2126 template <
typename T>
2127 class Impl :
public MatcherInterface<T> {
2128 using ResultType = decltype(CallableTraits<Callable>::template Invoke<T>(
2129 std::declval<CallableStorageType>(), std::declval<T>()));
2132 template <
typename M>
2133 Impl(
const CallableStorageType& callable,
const M& matcher)
2134 : callable_(callable), matcher_(MatcherCast<ResultType>(matcher)) {}
2136 void DescribeTo(::std::ostream* os)
const override {
2137 *os <<
"is mapped by the given callable to a value that ";
2138 matcher_.DescribeTo(os);
2141 void DescribeNegationTo(::std::ostream* os)
const override {
2142 *os <<
"is mapped by the given callable to a value that ";
2143 matcher_.DescribeNegationTo(os);
2146 bool MatchAndExplain(T obj, MatchResultListener* listener)
const override {
2147 *listener <<
"which is mapped by the given callable to ";
2153 CallableTraits<Callable>::template Invoke<T>(callable_, obj);
2154 return MatchPrintAndExplain(result, matcher_, listener);
2163 mutable CallableStorageType callable_;
2164 const Matcher<ResultType> matcher_;
2167 const CallableStorageType callable_;
2168 const InnerMatcher matcher_;
2172 template <
typename SizeMatcher>
2173 class SizeIsMatcher {
2175 explicit SizeIsMatcher(
const SizeMatcher& size_matcher)
2176 : size_matcher_(size_matcher) {
2179 template <
typename Container>
2180 operator Matcher<Container>()
const {
2181 return Matcher<Container>(
new Impl<const Container&>(size_matcher_));
2184 template <
typename Container>
2185 class Impl :
public MatcherInterface<Container> {
2187 using SizeType = decltype(std::declval<Container>().size());
2188 explicit Impl(
const SizeMatcher& size_matcher)
2189 : size_matcher_(MatcherCast<SizeType>(size_matcher)) {}
2191 void DescribeTo(::std::ostream* os)
const override {
2193 size_matcher_.DescribeTo(os);
2195 void DescribeNegationTo(::std::ostream* os)
const override {
2197 size_matcher_.DescribeNegationTo(os);
2200 bool MatchAndExplain(Container
container,
2201 MatchResultListener* listener)
const override {
2202 SizeType size = container.size();
2203 StringMatchResultListener size_listener;
2204 const bool result = size_matcher_.MatchAndExplain(size, &size_listener);
2206 <<
"whose size " << size << (result ?
" matches" :
" doesn't match");
2207 PrintIfNotEmpty(size_listener.str(), listener->stream());
2212 const Matcher<SizeType> size_matcher_;
2216 const SizeMatcher size_matcher_;
2221 template <
typename DistanceMatcher>
2222 class BeginEndDistanceIsMatcher {
2224 explicit BeginEndDistanceIsMatcher(
const DistanceMatcher& distance_matcher)
2225 : distance_matcher_(distance_matcher) {}
2227 template <
typename Container>
2228 operator Matcher<Container>()
const {
2229 return Matcher<Container>(
new Impl<const Container&>(distance_matcher_));
2232 template <
typename Container>
2233 class Impl :
public MatcherInterface<Container> {
2235 typedef internal::StlContainerView<
2237 typedef typename std::iterator_traits<
2238 typename ContainerView::type::const_iterator>::difference_type
2240 explicit Impl(
const DistanceMatcher& distance_matcher)
2241 : distance_matcher_(MatcherCast<DistanceType>(distance_matcher)) {}
2243 void DescribeTo(::std::ostream* os)
const override {
2244 *os <<
"distance between begin() and end() ";
2245 distance_matcher_.DescribeTo(os);
2247 void DescribeNegationTo(::std::ostream* os)
const override {
2248 *os <<
"distance between begin() and end() ";
2249 distance_matcher_.DescribeNegationTo(os);
2252 bool MatchAndExplain(Container
container,
2253 MatchResultListener* listener)
const override {
2256 DistanceType distance = std::distance(begin(container), end(container));
2257 StringMatchResultListener distance_listener;
2259 distance_matcher_.MatchAndExplain(distance, &distance_listener);
2260 *listener <<
"whose distance between begin() and end() " << distance
2261 << (result ?
" matches" :
" doesn't match");
2262 PrintIfNotEmpty(distance_listener.str(), listener->stream());
2267 const Matcher<DistanceType> distance_matcher_;
2271 const DistanceMatcher distance_matcher_;
2284 template <
typename Container>
2285 class ContainerEqMatcher {
2287 typedef internal::StlContainerView<Container>
View;
2288 typedef typename View::type StlContainer;
2289 typedef typename View::const_reference StlContainerReference;
2292 "Container type must not be const");
2294 "Container type must not be a reference");
2298 explicit ContainerEqMatcher(
const Container&
expected)
2299 : expected_(View::Copy(expected)) {}
2301 void DescribeTo(::std::ostream* os)
const {
2305 void DescribeNegationTo(::std::ostream* os)
const {
2306 *os <<
"does not equal ";
2310 template <
typename LhsContainer>
2311 bool MatchAndExplain(
const LhsContainer& lhs,
2312 MatchResultListener* listener)
const {
2313 typedef internal::StlContainerView<
2314 typename std::remove_const<LhsContainer>::type>
2316 typedef typename LhsView::type LhsStlContainer;
2317 StlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2318 if (lhs_stl_container == expected_)
2321 ::std::ostream*
const os = listener->stream();
2322 if (os !=
nullptr) {
2324 bool printed_header =
false;
2325 for (
typename LhsStlContainer::const_iterator it =
2326 lhs_stl_container.begin();
2327 it != lhs_stl_container.end(); ++it) {
2330 if (printed_header) {
2333 *os <<
"which has these unexpected elements: ";
2334 printed_header =
true;
2341 bool printed_header2 =
false;
2342 for (
typename StlContainer::const_iterator it = expected_.begin();
2343 it != expected_.end(); ++it) {
2345 lhs_stl_container.begin(), lhs_stl_container.end(), *it) ==
2346 lhs_stl_container.end()) {
2347 if (printed_header2) {
2350 *os << (printed_header ?
",\nand" :
"which")
2351 <<
" doesn't have these expected elements: ";
2352 printed_header2 =
true;
2363 const StlContainer expected_;
2367 struct LessComparator {
2368 template <
typename T,
typename U>
2369 bool operator()(
const T& lhs,
const U& rhs)
const {
return lhs < rhs; }
2373 template <
typename Comparator,
typename ContainerMatcher>
2374 class WhenSortedByMatcher {
2376 WhenSortedByMatcher(
const Comparator& comparator,
2377 const ContainerMatcher& matcher)
2378 : comparator_(comparator), matcher_(matcher) {}
2380 template <
typename LhsContainer>
2381 operator Matcher<LhsContainer>()
const {
2382 return MakeMatcher(
new Impl<LhsContainer>(comparator_, matcher_));
2385 template <
typename LhsContainer>
2386 class Impl :
public MatcherInterface<LhsContainer> {
2388 typedef internal::StlContainerView<
2390 typedef typename LhsView::type LhsStlContainer;
2391 typedef typename LhsView::const_reference LhsStlContainerReference;
2394 typedef typename RemoveConstFromKey<
2395 typename LhsStlContainer::value_type>::type LhsValue;
2397 Impl(
const Comparator& comparator,
const ContainerMatcher& matcher)
2398 : comparator_(comparator), matcher_(matcher) {}
2400 void DescribeTo(::std::ostream* os)
const override {
2401 *os <<
"(when sorted) ";
2402 matcher_.DescribeTo(os);
2405 void DescribeNegationTo(::std::ostream* os)
const override {
2406 *os <<
"(when sorted) ";
2407 matcher_.DescribeNegationTo(os);
2410 bool MatchAndExplain(LhsContainer lhs,
2411 MatchResultListener* listener)
const override {
2412 LhsStlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2413 ::std::vector<LhsValue> sorted_container(lhs_stl_container.begin(),
2414 lhs_stl_container.end());
2416 sorted_container.begin(), sorted_container.end(), comparator_);
2418 if (!listener->IsInterested()) {
2421 return matcher_.Matches(sorted_container);
2424 *listener <<
"which is ";
2426 *listener <<
" when sorted";
2428 StringMatchResultListener inner_listener;
2429 const bool match = matcher_.MatchAndExplain(sorted_container,
2431 PrintIfNotEmpty(inner_listener.str(), listener->stream());
2436 const Comparator comparator_;
2437 const Matcher<const ::std::vector<LhsValue>&> matcher_;
2443 const Comparator comparator_;
2444 const ContainerMatcher matcher_;
2451 template <
typename TupleMatcher,
typename RhsContainer>
2452 class PointwiseMatcher {
2455 use_UnorderedPointwise_with_hash_tables);
2458 typedef internal::StlContainerView<RhsContainer> RhsView;
2459 typedef typename RhsView::type RhsStlContainer;
2460 typedef typename RhsStlContainer::value_type RhsValue;
2463 "RhsContainer type must not be const");
2465 "RhsContainer type must not be a reference");
2469 PointwiseMatcher(
const TupleMatcher& tuple_matcher,
const RhsContainer& rhs)
2470 : tuple_matcher_(tuple_matcher),
rhs_(RhsView::Copy(rhs)) {}
2472 template <
typename LhsContainer>
2473 operator Matcher<LhsContainer>()
const {
2476 use_UnorderedPointwise_with_hash_tables);
2478 return Matcher<LhsContainer>(
2479 new Impl<const LhsContainer&>(tuple_matcher_,
rhs_));
2482 template <
typename LhsContainer>
2483 class Impl :
public MatcherInterface<LhsContainer> {
2485 typedef internal::StlContainerView<
2487 typedef typename LhsView::type LhsStlContainer;
2488 typedef typename LhsView::const_reference LhsStlContainerReference;
2489 typedef typename LhsStlContainer::value_type LhsValue;
2494 typedef ::std::tuple<const LhsValue&, const RhsValue&> InnerMatcherArg;
2496 Impl(
const TupleMatcher& tuple_matcher,
const RhsStlContainer& rhs)
2498 : mono_tuple_matcher_(SafeMatcherCast<InnerMatcherArg>(tuple_matcher)),
2501 void DescribeTo(::std::ostream* os)
const override {
2502 *os <<
"contains " <<
rhs_.size()
2503 <<
" values, where each value and its corresponding value in ";
2506 mono_tuple_matcher_.DescribeTo(os);
2508 void DescribeNegationTo(::std::ostream* os)
const override {
2509 *os <<
"doesn't contain exactly " <<
rhs_.size()
2510 <<
" values, or contains a value x at some index i"
2511 <<
" where x and the i-th value of ";
2514 mono_tuple_matcher_.DescribeNegationTo(os);
2517 bool MatchAndExplain(LhsContainer lhs,
2518 MatchResultListener* listener)
const override {
2519 LhsStlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2520 const size_t actual_size = lhs_stl_container.size();
2521 if (actual_size !=
rhs_.size()) {
2522 *listener <<
"which contains " << actual_size <<
" values";
2526 typename LhsStlContainer::const_iterator left = lhs_stl_container.begin();
2527 typename RhsStlContainer::const_iterator right =
rhs_.begin();
2528 for (
size_t i = 0;
i != actual_size; ++
i, ++left, ++right) {
2529 if (listener->IsInterested()) {
2530 StringMatchResultListener inner_listener;
2534 if (!mono_tuple_matcher_.MatchAndExplain(
2535 InnerMatcherArg(ImplicitCast_<const LhsValue&>(*left),
2536 ImplicitCast_<const RhsValue&>(*right)),
2538 *listener <<
"where the value pair (";
2542 *listener <<
") at index #" <<
i <<
" don't match";
2543 PrintIfNotEmpty(inner_listener.str(), listener->stream());
2547 if (!mono_tuple_matcher_.Matches(
2548 InnerMatcherArg(ImplicitCast_<const LhsValue&>(*left),
2549 ImplicitCast_<const RhsValue&>(*right))))
2558 const Matcher<InnerMatcherArg> mono_tuple_matcher_;
2559 const RhsStlContainer
rhs_;
2563 const TupleMatcher tuple_matcher_;
2564 const RhsStlContainer
rhs_;
2568 template <
typename Container>
2569 class QuantifierMatcherImpl :
public MatcherInterface<Container> {
2572 typedef StlContainerView<RawContainer>
View;
2573 typedef typename View::type StlContainer;
2574 typedef typename View::const_reference StlContainerReference;
2575 typedef typename StlContainer::value_type Element;
2577 template <
typename InnerMatcher>
2578 explicit QuantifierMatcherImpl(InnerMatcher inner_matcher)
2580 testing::SafeMatcherCast<const Element&>(inner_matcher)) {}
2585 bool MatchAndExplainImpl(
bool all_elements_should_match,
2587 MatchResultListener* listener)
const {
2588 StlContainerReference stl_container = View::ConstReference(container);
2590 for (
typename StlContainer::const_iterator it = stl_container.begin();
2591 it != stl_container.end(); ++it, ++
i) {
2592 StringMatchResultListener inner_listener;
2593 const bool matches = inner_matcher_.MatchAndExplain(*it, &inner_listener);
2595 if (matches != all_elements_should_match) {
2596 *listener <<
"whose element #" << i
2597 << (matches ?
" matches" :
" doesn't match");
2598 PrintIfNotEmpty(inner_listener.str(), listener->stream());
2599 return !all_elements_should_match;
2602 return all_elements_should_match;
2606 const Matcher<const Element&> inner_matcher_;
2611 template <
typename Container>
2612 class ContainsMatcherImpl :
public QuantifierMatcherImpl<Container> {
2614 template <
typename InnerMatcher>
2615 explicit ContainsMatcherImpl(InnerMatcher inner_matcher)
2616 : QuantifierMatcherImpl<Container>(inner_matcher) {}
2619 void DescribeTo(::std::ostream* os)
const override {
2620 *os <<
"contains at least one element that ";
2621 this->inner_matcher_.DescribeTo(os);
2624 void DescribeNegationTo(::std::ostream* os)
const override {
2625 *os <<
"doesn't contain any element that ";
2626 this->inner_matcher_.DescribeTo(os);
2629 bool MatchAndExplain(Container
container,
2630 MatchResultListener* listener)
const override {
2631 return this->MatchAndExplainImpl(
false, container, listener);
2637 template <
typename Container>
2638 class EachMatcherImpl :
public QuantifierMatcherImpl<Container> {
2640 template <
typename InnerMatcher>
2641 explicit EachMatcherImpl(InnerMatcher inner_matcher)
2642 : QuantifierMatcherImpl<Container>(inner_matcher) {}
2645 void DescribeTo(::std::ostream* os)
const override {
2646 *os <<
"only contains elements that ";
2647 this->inner_matcher_.DescribeTo(os);
2650 void DescribeNegationTo(::std::ostream* os)
const override {
2651 *os <<
"contains some element that ";
2652 this->inner_matcher_.DescribeNegationTo(os);
2655 bool MatchAndExplain(Container
container,
2656 MatchResultListener* listener)
const override {
2657 return this->MatchAndExplainImpl(
true, container, listener);
2662 template <
typename M>
2663 class ContainsMatcher {
2665 explicit ContainsMatcher(M m) : inner_matcher_(m) {}
2667 template <
typename Container>
2668 operator Matcher<Container>()
const {
2669 return Matcher<Container>(
2670 new ContainsMatcherImpl<const Container&>(inner_matcher_));
2674 const M inner_matcher_;
2678 template <
typename M>
2681 explicit EachMatcher(M m) : inner_matcher_(m) {}
2683 template <
typename Container>
2684 operator Matcher<Container>()
const {
2685 return Matcher<Container>(
2686 new EachMatcherImpl<const Container&>(inner_matcher_));
2690 const M inner_matcher_;
2694 struct Rank0 : Rank1 {};
2696 namespace pair_getters {
2698 template <
typename T>
2699 auto First(T&
x, Rank1) -> decltype(get<0>(x)) {
2702 template <
typename T>
2703 auto First(T& x, Rank0) -> decltype((x.first)) {
2707 template <
typename T>
2708 auto Second(T& x, Rank1) -> decltype(get<1>(x)) {
2711 template <
typename T>
2712 auto Second(T& x, Rank0) -> decltype((x.second)) {
2721 template <
typename PairType>
2722 class KeyMatcherImpl :
public MatcherInterface<PairType> {
2725 typedef typename RawPairType::first_type KeyType;
2727 template <
typename InnerMatcher>
2728 explicit KeyMatcherImpl(InnerMatcher inner_matcher)
2730 testing::SafeMatcherCast<const KeyType&>(inner_matcher)) {
2735 bool MatchAndExplain(PairType key_value,
2736 MatchResultListener* listener)
const override {
2737 StringMatchResultListener inner_listener;
2738 const bool match = inner_matcher_.MatchAndExplain(
2739 pair_getters::First(key_value, Rank0()), &inner_listener);
2740 const std::string explanation = inner_listener.str();
2741 if (explanation !=
"") {
2742 *listener <<
"whose first field is a value " << explanation;
2748 void DescribeTo(::std::ostream* os)
const override {
2749 *os <<
"has a key that ";
2750 inner_matcher_.DescribeTo(os);
2754 void DescribeNegationTo(::std::ostream* os)
const override {
2755 *os <<
"doesn't have a key that ";
2756 inner_matcher_.DescribeTo(os);
2760 const Matcher<const KeyType&> inner_matcher_;
2764 template <
typename M>
2767 explicit KeyMatcher(M m) : matcher_for_key_(m) {}
2769 template <
typename PairType>
2770 operator Matcher<PairType>()
const {
2771 return Matcher<PairType>(
2772 new KeyMatcherImpl<const PairType&>(matcher_for_key_));
2776 const M matcher_for_key_;
2781 template <
typename PairType>
2782 class PairMatcherImpl :
public MatcherInterface<PairType> {
2785 typedef typename RawPairType::first_type FirstType;
2786 typedef typename RawPairType::second_type SecondType;
2788 template <
typename FirstMatcher,
typename SecondMatcher>
2789 PairMatcherImpl(FirstMatcher first_matcher, SecondMatcher second_matcher)
2791 testing::SafeMatcherCast<const FirstType&>(first_matcher)),
2793 testing::SafeMatcherCast<const SecondType&>(second_matcher)) {
2797 void DescribeTo(::std::ostream* os)
const override {
2798 *os <<
"has a first field that ";
2799 first_matcher_.DescribeTo(os);
2800 *os <<
", and has a second field that ";
2801 second_matcher_.DescribeTo(os);
2805 void DescribeNegationTo(::std::ostream* os)
const override {
2806 *os <<
"has a first field that ";
2807 first_matcher_.DescribeNegationTo(os);
2808 *os <<
", or has a second field that ";
2809 second_matcher_.DescribeNegationTo(os);
2814 bool MatchAndExplain(PairType a_pair,
2815 MatchResultListener* listener)
const override {
2816 if (!listener->IsInterested()) {
2819 return first_matcher_.Matches(pair_getters::First(a_pair, Rank0())) &&
2820 second_matcher_.Matches(pair_getters::Second(a_pair, Rank0()));
2822 StringMatchResultListener first_inner_listener;
2823 if (!first_matcher_.MatchAndExplain(pair_getters::First(a_pair, Rank0()),
2824 &first_inner_listener)) {
2825 *listener <<
"whose first field does not match";
2826 PrintIfNotEmpty(first_inner_listener.str(), listener->stream());
2829 StringMatchResultListener second_inner_listener;
2830 if (!second_matcher_.MatchAndExplain(pair_getters::Second(a_pair, Rank0()),
2831 &second_inner_listener)) {
2832 *listener <<
"whose second field does not match";
2833 PrintIfNotEmpty(second_inner_listener.str(), listener->stream());
2836 ExplainSuccess(first_inner_listener.str(), second_inner_listener.str(),
2842 void ExplainSuccess(
const std::string& first_explanation,
2843 const std::string& second_explanation,
2844 MatchResultListener* listener)
const {
2845 *listener <<
"whose both fields match";
2846 if (first_explanation !=
"") {
2847 *listener <<
", where the first field is a value " << first_explanation;
2849 if (second_explanation !=
"") {
2851 if (first_explanation !=
"") {
2852 *listener <<
"and ";
2854 *listener <<
"where ";
2856 *listener <<
"the second field is a value " << second_explanation;
2860 const Matcher<const FirstType&> first_matcher_;
2861 const Matcher<const SecondType&> second_matcher_;
2865 template <
typename FirstMatcher,
typename SecondMatcher>
2868 PairMatcher(FirstMatcher first_matcher, SecondMatcher second_matcher)
2869 : first_matcher_(first_matcher), second_matcher_(second_matcher) {}
2871 template <
typename PairType>
2872 operator Matcher<PairType> ()
const {
2873 return Matcher<PairType>(
2874 new PairMatcherImpl<const PairType&>(first_matcher_, second_matcher_));
2878 const FirstMatcher first_matcher_;
2879 const SecondMatcher second_matcher_;
2883 template <
typename Container>
2884 class ElementsAreMatcherImpl :
public MatcherInterface<Container> {
2887 typedef internal::StlContainerView<RawContainer>
View;
2888 typedef typename View::type StlContainer;
2889 typedef typename View::const_reference StlContainerReference;
2890 typedef typename StlContainer::value_type Element;
2894 template <
typename InputIter>
2895 ElementsAreMatcherImpl(InputIter first, InputIter last) {
2896 while (first != last) {
2897 matchers_.push_back(MatcherCast<const Element&>(*first++));
2902 void DescribeTo(::std::ostream* os)
const override {
2905 }
else if (
count() == 1) {
2906 *os <<
"has 1 element that ";
2907 matchers_[0].DescribeTo(os);
2909 *os <<
"has " << Elements(
count()) <<
" where\n";
2910 for (
size_t i = 0;
i !=
count(); ++
i) {
2911 *os <<
"element #" <<
i <<
" ";
2912 matchers_[
i].DescribeTo(os);
2913 if (i + 1 <
count()) {
2921 void DescribeNegationTo(::std::ostream* os)
const override {
2923 *os <<
"isn't empty";
2927 *os <<
"doesn't have " << Elements(
count()) <<
", or\n";
2928 for (
size_t i = 0;
i !=
count(); ++
i) {
2929 *os <<
"element #" <<
i <<
" ";
2930 matchers_[
i].DescribeNegationTo(os);
2931 if (i + 1 <
count()) {
2937 bool MatchAndExplain(Container
container,
2938 MatchResultListener* listener)
const override {
2942 const bool listener_interested = listener->IsInterested();
2945 ::std::vector<std::string> explanations(
count());
2946 StlContainerReference stl_container = View::ConstReference(container);
2947 typename StlContainer::const_iterator it = stl_container.begin();
2948 size_t exam_pos = 0;
2949 bool mismatch_found =
false;
2954 for (; it != stl_container.end() && exam_pos !=
count(); ++it, ++exam_pos) {
2956 if (listener_interested) {
2957 StringMatchResultListener s;
2958 match = matchers_[exam_pos].MatchAndExplain(*it, &s);
2959 explanations[exam_pos] = s.str();
2961 match = matchers_[exam_pos].Matches(*it);
2965 mismatch_found =
true;
2974 size_t actual_count = exam_pos;
2975 for (; it != stl_container.end(); ++it) {
2979 if (actual_count !=
count()) {
2984 if (listener_interested && (actual_count != 0)) {
2985 *listener <<
"which has " << Elements(actual_count);
2990 if (mismatch_found) {
2992 if (listener_interested) {
2993 *listener <<
"whose element #" << exam_pos <<
" doesn't match";
2994 PrintIfNotEmpty(explanations[exam_pos], listener->stream());
3001 if (listener_interested) {
3002 bool reason_printed =
false;
3003 for (
size_t i = 0;
i !=
count(); ++
i) {
3004 const std::string& s = explanations[
i];
3006 if (reason_printed) {
3007 *listener <<
",\nand ";
3009 *listener <<
"whose element #" <<
i <<
" matches, " << s;
3010 reason_printed =
true;
3018 static Message Elements(
size_t count) {
3019 return Message() << count << (count == 1 ?
" element" :
" elements");
3022 size_t count()
const {
return matchers_.size(); }
3024 ::std::vector<Matcher<const Element&> > matchers_;
3033 MatchMatrix(
size_t num_elements,
size_t num_matchers)
3034 : num_elements_(num_elements),
3035 num_matchers_(num_matchers),
3036 matched_(num_elements_* num_matchers_, 0) {
3039 size_t LhsSize()
const {
return num_elements_; }
3040 size_t RhsSize()
const {
return num_matchers_; }
3041 bool HasEdge(
size_t ilhs,
size_t irhs)
const {
3042 return matched_[SpaceIndex(ilhs, irhs)] == 1;
3044 void SetEdge(
size_t ilhs,
size_t irhs,
bool b) {
3045 matched_[SpaceIndex(ilhs, irhs)] = b ? 1 : 0;
3055 std::string DebugString()
const;
3058 size_t SpaceIndex(
size_t ilhs,
size_t irhs)
const {
3059 return ilhs * num_matchers_ + irhs;
3062 size_t num_elements_;
3063 size_t num_matchers_;
3068 ::std::vector<char> matched_;
3071 typedef ::std::pair<size_t, size_t> ElementMatcherPair;
3072 typedef ::std::vector<ElementMatcherPair> ElementMatcherPairs;
3079 struct UnorderedMatcherRequire {
3083 ExactMatch = Superset | Subset,
3090 class GTEST_API_ UnorderedElementsAreMatcherImplBase {
3092 explicit UnorderedElementsAreMatcherImplBase(
3093 UnorderedMatcherRequire::Flags matcher_flags)
3094 : match_flags_(matcher_flags) {}
3099 typedef ::std::vector<const MatcherDescriberInterface*> MatcherDescriberVec;
3102 void DescribeToImpl(::std::ostream* os)
const;
3105 void DescribeNegationToImpl(::std::ostream* os)
const;
3107 bool VerifyMatchMatrix(const ::std::vector<std::string>& element_printouts,
3108 const MatchMatrix& matrix,
3109 MatchResultListener* listener)
const;
3111 bool FindPairing(
const MatchMatrix& matrix,
3112 MatchResultListener* listener)
const;
3114 MatcherDescriberVec& matcher_describers() {
3115 return matcher_describers_;
3118 static Message Elements(
size_t n) {
3119 return Message() << n <<
" element" << (n == 1 ?
"" :
"s");
3122 UnorderedMatcherRequire::Flags match_flags()
const {
return match_flags_; }
3125 UnorderedMatcherRequire::Flags match_flags_;
3126 MatcherDescriberVec matcher_describers_;
3131 template <
typename Container>
3132 class UnorderedElementsAreMatcherImpl
3133 :
public MatcherInterface<Container>,
3134 public UnorderedElementsAreMatcherImplBase {
3137 typedef internal::StlContainerView<RawContainer>
View;
3138 typedef typename View::type StlContainer;
3139 typedef typename View::const_reference StlContainerReference;
3140 typedef typename StlContainer::const_iterator StlContainerConstIterator;
3141 typedef typename StlContainer::value_type Element;
3143 template <
typename InputIter>
3144 UnorderedElementsAreMatcherImpl(UnorderedMatcherRequire::Flags matcher_flags,
3145 InputIter first, InputIter last)
3146 : UnorderedElementsAreMatcherImplBase(matcher_flags) {
3147 for (; first != last; ++first) {
3148 matchers_.push_back(MatcherCast<const Element&>(*first));
3149 matcher_describers().push_back(matchers_.back().GetDescriber());
3154 void DescribeTo(::std::ostream* os)
const override {
3155 return UnorderedElementsAreMatcherImplBase::DescribeToImpl(os);
3159 void DescribeNegationTo(::std::ostream* os)
const override {
3160 return UnorderedElementsAreMatcherImplBase::DescribeNegationToImpl(os);
3163 bool MatchAndExplain(Container
container,
3164 MatchResultListener* listener)
const override {
3165 StlContainerReference stl_container = View::ConstReference(container);
3166 ::std::vector<std::string> element_printouts;
3167 MatchMatrix matrix =
3168 AnalyzeElements(stl_container.begin(), stl_container.end(),
3169 &element_printouts, listener);
3171 if (matrix.LhsSize() == 0 && matrix.RhsSize() == 0) {
3175 if (match_flags() == UnorderedMatcherRequire::ExactMatch) {
3176 if (matrix.LhsSize() != matrix.RhsSize()) {
3181 if (matrix.LhsSize() != 0 && listener->IsInterested()) {
3182 *listener <<
"which has " << Elements(matrix.LhsSize());
3188 return VerifyMatchMatrix(element_printouts, matrix, listener) &&
3189 FindPairing(matrix, listener);
3193 template <
typename ElementIter>
3194 MatchMatrix AnalyzeElements(ElementIter elem_first, ElementIter elem_last,
3195 ::std::vector<std::string>* element_printouts,
3196 MatchResultListener* listener)
const {
3197 element_printouts->clear();
3198 ::std::vector<char> did_match;
3199 size_t num_elements = 0;
3200 DummyMatchResultListener dummy;
3201 for (; elem_first != elem_last; ++num_elements, ++elem_first) {
3202 if (listener->IsInterested()) {
3205 for (
size_t irhs = 0; irhs != matchers_.size(); ++irhs) {
3206 did_match.push_back(
3207 matchers_[irhs].MatchAndExplain(*elem_first, &dummy));
3211 MatchMatrix matrix(num_elements, matchers_.size());
3212 ::std::vector<char>::const_iterator did_match_iter = did_match.begin();
3213 for (
size_t ilhs = 0; ilhs != num_elements; ++ilhs) {
3214 for (
size_t irhs = 0; irhs != matchers_.size(); ++irhs) {
3215 matrix.SetEdge(ilhs, irhs, *did_match_iter++ != 0);
3221 ::std::vector<Matcher<const Element&> > matchers_;
3226 template <
typename Target>
3227 struct CastAndAppendTransform {
3228 template <
typename Arg>
3229 Matcher<Target> operator()(
const Arg&
a)
const {
3230 return MatcherCast<Target>(
a);
3235 template <
typename MatcherTuple>
3236 class UnorderedElementsAreMatcher {
3238 explicit UnorderedElementsAreMatcher(
const MatcherTuple& args)
3239 : matchers_(args) {}
3241 template <
typename Container>
3242 operator Matcher<Container>()
const {
3244 typedef typename internal::StlContainerView<RawContainer>::type
View;
3245 typedef typename View::value_type Element;
3246 typedef ::std::vector<Matcher<const Element&> > MatcherVec;
3247 MatcherVec matchers;
3249 TransformTupleValues(CastAndAppendTransform<const Element&>(), matchers_,
3250 ::std::back_inserter(matchers));
3251 return Matcher<Container>(
3252 new UnorderedElementsAreMatcherImpl<const Container&>(
3253 UnorderedMatcherRequire::ExactMatch, matchers.begin(),
3258 const MatcherTuple matchers_;
3262 template <
typename MatcherTuple>
3263 class ElementsAreMatcher {
3265 explicit ElementsAreMatcher(
const MatcherTuple& args) : matchers_(args) {}
3267 template <
typename Container>
3268 operator Matcher<Container>()
const {
3272 use_UnorderedElementsAre_with_hash_tables);
3275 typedef typename internal::StlContainerView<RawContainer>::type
View;
3276 typedef typename View::value_type Element;
3277 typedef ::std::vector<Matcher<const Element&> > MatcherVec;
3278 MatcherVec matchers;
3280 TransformTupleValues(CastAndAppendTransform<const Element&>(), matchers_,
3281 ::std::back_inserter(matchers));
3282 return Matcher<Container>(
new ElementsAreMatcherImpl<const Container&>(
3283 matchers.begin(), matchers.end()));
3287 const MatcherTuple matchers_;
3291 template <
typename T>
3292 class UnorderedElementsAreArrayMatcher {
3294 template <
typename Iter>
3295 UnorderedElementsAreArrayMatcher(UnorderedMatcherRequire::Flags match_flags,
3296 Iter first, Iter last)
3297 : match_flags_(match_flags), matchers_(first, last) {}
3299 template <
typename Container>
3300 operator Matcher<Container>()
const {
3301 return Matcher<Container>(
3302 new UnorderedElementsAreMatcherImpl<const Container&>(
3303 match_flags_, matchers_.begin(), matchers_.end()));
3307 UnorderedMatcherRequire::Flags match_flags_;
3308 ::std::vector<T> matchers_;
3312 template <
typename T>
3313 class ElementsAreArrayMatcher {
3315 template <
typename Iter>
3316 ElementsAreArrayMatcher(Iter first, Iter last) : matchers_(first, last) {}
3318 template <
typename Container>
3319 operator Matcher<Container>()
const {
3322 use_UnorderedElementsAreArray_with_hash_tables);
3324 return Matcher<Container>(
new ElementsAreMatcherImpl<const Container&>(
3325 matchers_.begin(), matchers_.end()));
3329 const ::std::vector<T> matchers_;
3341 template <
typename Tuple2Matcher,
typename Second>
3342 class BoundSecondMatcher {
3344 BoundSecondMatcher(
const Tuple2Matcher& tm,
const Second& second)
3345 : tuple2_matcher_(tm), second_value_(second) {}
3347 BoundSecondMatcher(
const BoundSecondMatcher& other) =
default;
3349 template <
typename T>
3350 operator Matcher<T>()
const {
3351 return MakeMatcher(
new Impl<T>(tuple2_matcher_, second_value_));
3362 void operator=(
const BoundSecondMatcher& ) {
3363 GTEST_LOG_(FATAL) <<
"BoundSecondMatcher should never be assigned.";
3367 template <
typename T>
3368 class Impl :
public MatcherInterface<T> {
3370 typedef ::std::tuple<T, Second> ArgTuple;
3372 Impl(
const Tuple2Matcher& tm,
const Second& second)
3373 : mono_tuple2_matcher_(SafeMatcherCast<const ArgTuple&>(tm)),
3374 second_value_(second) {}
3376 void DescribeTo(::std::ostream* os)
const override {
3380 mono_tuple2_matcher_.DescribeTo(os);
3383 bool MatchAndExplain(T
x, MatchResultListener* listener)
const override {
3384 return mono_tuple2_matcher_.MatchAndExplain(ArgTuple(x, second_value_),
3389 const Matcher<const ArgTuple&> mono_tuple2_matcher_;
3390 const Second second_value_;
3393 const Tuple2Matcher tuple2_matcher_;
3394 const Second second_value_;
3401 template <
typename Tuple2Matcher,
typename Second>
3402 BoundSecondMatcher<Tuple2Matcher, Second> MatcherBindSecond(
3403 const Tuple2Matcher& tm,
const Second& second) {
3404 return BoundSecondMatcher<Tuple2Matcher, Second>(tm, second);
3413 const char* matcher_name,
3417 template <
typename ValueMatcher>
3418 class OptionalMatcher {
3420 explicit OptionalMatcher(
const ValueMatcher& value_matcher)
3421 : value_matcher_(value_matcher) {}
3423 template <
typename Optional>
3424 operator Matcher<Optional>()
const {
3425 return Matcher<Optional>(
new Impl<const Optional&>(value_matcher_));
3428 template <
typename Optional>
3429 class Impl :
public MatcherInterface<Optional> {
3432 typedef typename OptionalView::value_type ValueType;
3433 explicit Impl(
const ValueMatcher& value_matcher)
3434 : value_matcher_(MatcherCast<ValueType>(value_matcher)) {}
3436 void DescribeTo(::std::ostream* os)
const override {
3438 value_matcher_.DescribeTo(os);
3441 void DescribeNegationTo(::std::ostream* os)
const override {
3443 value_matcher_.DescribeNegationTo(os);
3446 bool MatchAndExplain(Optional optional,
3447 MatchResultListener* listener)
const override {
3449 *listener <<
"which is not engaged";
3452 const ValueType& value = *optional;
3453 StringMatchResultListener value_listener;
3454 const bool match = value_matcher_.MatchAndExplain(value, &value_listener);
3456 << (match ?
" matches" :
" doesn't match");
3457 PrintIfNotEmpty(value_listener.str(), listener->stream());
3462 const Matcher<ValueType> value_matcher_;
3466 const ValueMatcher value_matcher_;
3469 namespace variant_matcher {
3471 template <
typename T>
3472 void holds_alternative() {}
3473 template <
typename T>
3477 template <
typename T>
3478 class VariantMatcher {
3481 : matcher_(std::move(matcher)) {}
3483 template <
typename Variant>
3484 bool MatchAndExplain(
const Variant& value,
3485 ::testing::MatchResultListener* listener)
const {
3487 if (!listener->IsInterested()) {
3488 return holds_alternative<T>(
value) && matcher_.Matches(get<T>(value));
3491 if (!holds_alternative<T>(value)) {
3492 *listener <<
"whose value is not of type '" <<
GetTypeName() <<
"'";
3496 const T& elem = get<T>(
value);
3497 StringMatchResultListener elem_listener;
3498 const bool match = matcher_.MatchAndExplain(elem, &elem_listener);
3500 << (match ?
" matches" :
" doesn't match");
3501 PrintIfNotEmpty(elem_listener.str(), listener->stream());
3505 void DescribeTo(std::ostream* os)
const {
3506 *os <<
"is a variant<> with value of type '" <<
GetTypeName()
3507 <<
"' and the value ";
3508 matcher_.DescribeTo(os);
3511 void DescribeNegationTo(std::ostream* os)
const {
3512 *os <<
"is a variant<> with value of type other than '" <<
GetTypeName()
3513 <<
"' or the value ";
3514 matcher_.DescribeNegationTo(os);
3521 return internal::GetTypeName<T>());
3523 return "the element type";
3526 const ::testing::Matcher<const T&> matcher_;
3531 namespace any_cast_matcher {
3534 template <
typename T>
3538 template <
typename T>
3539 class AnyCastMatcher {
3541 explicit AnyCastMatcher(const ::testing::Matcher<const T&>& matcher)
3542 : matcher_(matcher) {}
3544 template <
typename AnyType>
3545 bool MatchAndExplain(
const AnyType& value,
3546 ::testing::MatchResultListener* listener)
const {
3547 if (!listener->IsInterested()) {
3548 const T* ptr = any_cast<T>(&
value);
3549 return ptr !=
nullptr && matcher_.Matches(*ptr);
3552 const T* elem = any_cast<T>(&
value);
3553 if (elem ==
nullptr) {
3554 *listener <<
"whose value is not of type '" <<
GetTypeName() <<
"'";
3558 StringMatchResultListener elem_listener;
3559 const bool match = matcher_.MatchAndExplain(*elem, &elem_listener);
3561 << (match ?
" matches" :
" doesn't match");
3562 PrintIfNotEmpty(elem_listener.str(), listener->stream());
3566 void DescribeTo(std::ostream* os)
const {
3567 *os <<
"is an 'any' type with value of type '" <<
GetTypeName()
3568 <<
"' and the value ";
3569 matcher_.DescribeTo(os);
3572 void DescribeNegationTo(std::ostream* os)
const {
3573 *os <<
"is an 'any' type with value of type other than '" <<
GetTypeName()
3574 <<
"' or the value ";
3575 matcher_.DescribeNegationTo(os);
3582 return internal::GetTypeName<T>());
3584 return "the element type";
3587 const ::testing::Matcher<const T&> matcher_;
3593 template <
class ArgsTuple,
size_t... k>
3594 class ArgsMatcherImpl :
public MatcherInterface<ArgsTuple> {
3596 using RawArgsTuple =
typename std::decay<ArgsTuple>::type;
3597 using SelectedArgs =
3598 std::tuple<typename std::tuple_element<k, RawArgsTuple>::type...>;
3599 using MonomorphicInnerMatcher = Matcher<const SelectedArgs&>;
3601 template <
typename InnerMatcher>
3602 explicit ArgsMatcherImpl(
const InnerMatcher& inner_matcher)
3603 : inner_matcher_(SafeMatcherCast<const SelectedArgs&>(inner_matcher)) {}
3605 bool MatchAndExplain(ArgsTuple args,
3606 MatchResultListener* listener)
const override {
3609 const SelectedArgs& selected_args =
3610 std::forward_as_tuple(std::get<k>(args)...);
3611 if (!listener->IsInterested())
return inner_matcher_.Matches(selected_args);
3613 PrintIndices(listener->stream());
3616 StringMatchResultListener inner_listener;
3618 inner_matcher_.MatchAndExplain(selected_args, &inner_listener);
3619 PrintIfNotEmpty(inner_listener.str(), listener->stream());
3623 void DescribeTo(::std::ostream* os)
const override {
3624 *os <<
"are a tuple ";
3626 inner_matcher_.DescribeTo(os);
3629 void DescribeNegationTo(::std::ostream* os)
const override {
3630 *os <<
"are a tuple ";
3632 inner_matcher_.DescribeNegationTo(os);
3637 static void PrintIndices(::std::ostream* os) {
3638 *os <<
"whose fields (";
3639 const char* sep =
"";
3642 const char* dummy[] = {
"", (*os << sep <<
"#" << k, sep =
", ")...};
3647 MonomorphicInnerMatcher inner_matcher_;
3650 template <
class InnerMatcher,
size_t... k>
3653 explicit ArgsMatcher(InnerMatcher inner_matcher)
3654 : inner_matcher_(std::move(inner_matcher)) {}
3656 template <
typename ArgsTuple>
3657 operator Matcher<ArgsTuple>()
const {
3658 return MakeMatcher(
new ArgsMatcherImpl<ArgsTuple, k...>(inner_matcher_));
3662 InnerMatcher inner_matcher_;
3682 template <
typename Iter>
3683 inline internal::ElementsAreArrayMatcher<
3684 typename ::std::iterator_traits<Iter>::value_type>
3685 ElementsAreArray(Iter first, Iter last) {
3686 typedef typename ::std::iterator_traits<Iter>::value_type
T;
3687 return internal::ElementsAreArrayMatcher<T>(first, last);
3690 template <
typename T>
3691 inline internal::ElementsAreArrayMatcher<T> ElementsAreArray(
3692 const T* pointer,
size_t count) {
3693 return ElementsAreArray(pointer, pointer + count);
3696 template <
typename T,
size_t N>
3697 inline internal::ElementsAreArrayMatcher<T> ElementsAreArray(
3698 const T (&array)[
N]) {
3699 return ElementsAreArray(array, N);
3702 template <
typename Container>
3703 inline internal::ElementsAreArrayMatcher<typename Container::value_type>
3704 ElementsAreArray(
const Container&
container) {
3705 return ElementsAreArray(container.begin(), container.end());
3708 template <
typename T>
3709 inline internal::ElementsAreArrayMatcher<T>
3710 ElementsAreArray(::std::initializer_list<T> xs) {
3711 return ElementsAreArray(xs.begin(), xs.end());
3727 template <
typename Iter>
3728 inline internal::UnorderedElementsAreArrayMatcher<
3729 typename ::std::iterator_traits<Iter>::value_type>
3730 UnorderedElementsAreArray(Iter first, Iter last) {
3731 typedef typename ::std::iterator_traits<Iter>::value_type
T;
3732 return internal::UnorderedElementsAreArrayMatcher<T>(
3733 internal::UnorderedMatcherRequire::ExactMatch, first, last);
3736 template <
typename T>
3737 inline internal::UnorderedElementsAreArrayMatcher<T>
3738 UnorderedElementsAreArray(
const T* pointer,
size_t count) {
3739 return UnorderedElementsAreArray(pointer, pointer + count);
3742 template <
typename T,
size_t N>
3743 inline internal::UnorderedElementsAreArrayMatcher<T>
3744 UnorderedElementsAreArray(
const T (&array)[N]) {
3745 return UnorderedElementsAreArray(array, N);
3748 template <
typename Container>
3749 inline internal::UnorderedElementsAreArrayMatcher<
3750 typename Container::value_type>
3751 UnorderedElementsAreArray(
const Container& container) {
3752 return UnorderedElementsAreArray(container.begin(), container.end());
3755 template <
typename T>
3756 inline internal::UnorderedElementsAreArrayMatcher<T>
3757 UnorderedElementsAreArray(::std::initializer_list<T> xs) {
3758 return UnorderedElementsAreArray(xs.begin(), xs.end());
3770 const internal::AnythingMatcher
_ = {};
3772 template <
typename T>
3773 inline Matcher<T>
A() {
3774 return Matcher<T>(
new internal::AnyMatcherImpl<T>());
3778 template <
typename T>
3779 inline Matcher<T> An() {
return A<T>(); }
3781 template <
typename T,
typename M>
3782 Matcher<T> internal::MatcherCastImpl<T, M>::CastImpl(
3783 const M& value, std::false_type ,
3789 inline PolymorphicMatcher<internal::IsNullMatcher >
IsNull() {
3790 return MakePolymorphicMatcher(internal::IsNullMatcher());
3796 inline PolymorphicMatcher<internal::NotNullMatcher > NotNull() {
3797 return MakePolymorphicMatcher(internal::NotNullMatcher());
3802 template <
typename T>
3803 inline internal::RefMatcher<T&> Ref(T&
x) {
3804 return internal::RefMatcher<T&>(
x);
3808 inline PolymorphicMatcher<internal::IsNanMatcher> IsNan() {
3809 return MakePolymorphicMatcher(internal::IsNanMatcher());
3814 inline internal::FloatingEqMatcher<double> DoubleEq(
double rhs) {
3815 return internal::FloatingEqMatcher<double>(rhs,
false);
3820 inline internal::FloatingEqMatcher<double> NanSensitiveDoubleEq(
double rhs) {
3821 return internal::FloatingEqMatcher<double>(rhs,
true);
3827 inline internal::FloatingEqMatcher<double> DoubleNear(
3828 double rhs,
double max_abs_error) {
3829 return internal::FloatingEqMatcher<double>(rhs,
false, max_abs_error);
3835 inline internal::FloatingEqMatcher<double> NanSensitiveDoubleNear(
3836 double rhs,
double max_abs_error) {
3837 return internal::FloatingEqMatcher<double>(rhs,
true, max_abs_error);
3842 inline internal::FloatingEqMatcher<float> FloatEq(
float rhs) {
3843 return internal::FloatingEqMatcher<float>(rhs,
false);
3848 inline internal::FloatingEqMatcher<float> NanSensitiveFloatEq(
float rhs) {
3849 return internal::FloatingEqMatcher<float>(rhs,
true);
3855 inline internal::FloatingEqMatcher<float> FloatNear(
3856 float rhs,
float max_abs_error) {
3857 return internal::FloatingEqMatcher<float>(rhs,
false, max_abs_error);
3863 inline internal::FloatingEqMatcher<float> NanSensitiveFloatNear(
3864 float rhs,
float max_abs_error) {
3865 return internal::FloatingEqMatcher<float>(rhs,
true, max_abs_error);
3870 template <
typename InnerMatcher>
3871 inline internal::PointeeMatcher<InnerMatcher> Pointee(
3872 const InnerMatcher& inner_matcher) {
3873 return internal::PointeeMatcher<InnerMatcher>(inner_matcher);
3883 template <
typename To>
3884 inline PolymorphicMatcher<internal::WhenDynamicCastToMatcher<To> >
3885 WhenDynamicCastTo(
const Matcher<To>& inner_matcher) {
3886 return MakePolymorphicMatcher(
3887 internal::WhenDynamicCastToMatcher<To>(inner_matcher));
3889 #endif // GTEST_HAS_RTTI
3895 template <
typename Class,
typename FieldType,
typename FieldMatcher>
3896 inline PolymorphicMatcher<
3897 internal::FieldMatcher<Class, FieldType> > Field(
3898 FieldType Class::*field,
const FieldMatcher& matcher) {
3899 return MakePolymorphicMatcher(
3900 internal::FieldMatcher<Class, FieldType>(
3901 field, MatcherCast<const FieldType&>(matcher)));
3910 template <
typename Class,
typename FieldType,
typename FieldMatcher>
3911 inline PolymorphicMatcher<internal::FieldMatcher<Class, FieldType> > Field(
3912 const std::string& field_name, FieldType Class::*field,
3913 const FieldMatcher& matcher) {
3914 return MakePolymorphicMatcher(internal::FieldMatcher<Class, FieldType>(
3915 field_name, field, MatcherCast<const FieldType&>(matcher)));
3922 template <
typename Class,
typename PropertyType,
typename PropertyMatcher>
3923 inline PolymorphicMatcher<internal::PropertyMatcher<
3924 Class, PropertyType, PropertyType (Class::*)()
const> >
3925 Property(PropertyType (Class::*property)()
const,
3926 const PropertyMatcher& matcher) {
3927 return MakePolymorphicMatcher(
3928 internal::PropertyMatcher<Class, PropertyType,
3929 PropertyType (Class::*)()
const>(
3930 property, MatcherCast<const PropertyType&>(matcher)));
3939 template <
typename Class,
typename PropertyType,
typename PropertyMatcher>
3940 inline PolymorphicMatcher<internal::PropertyMatcher<
3941 Class, PropertyType, PropertyType (Class::*)()
const> >
3942 Property(
const std::string& property_name,
3943 PropertyType (Class::*property)()
const,
3944 const PropertyMatcher& matcher) {
3945 return MakePolymorphicMatcher(
3946 internal::PropertyMatcher<Class, PropertyType,
3947 PropertyType (Class::*)()
const>(
3948 property_name, property, MatcherCast<const PropertyType&>(matcher)));
3952 template <
typename Class,
typename PropertyType,
typename PropertyMatcher>
3953 inline PolymorphicMatcher<internal::PropertyMatcher<
3954 Class, PropertyType, PropertyType (Class::*)()
const &> >
3955 Property(PropertyType (Class::*property)()
const &,
3956 const PropertyMatcher& matcher) {
3957 return MakePolymorphicMatcher(
3958 internal::PropertyMatcher<Class, PropertyType,
3959 PropertyType (Class::*)()
const&>(
3960 property, MatcherCast<const PropertyType&>(matcher)));
3964 template <
typename Class,
typename PropertyType,
typename PropertyMatcher>
3965 inline PolymorphicMatcher<internal::PropertyMatcher<
3966 Class, PropertyType, PropertyType (Class::*)()
const &> >
3967 Property(
const std::string& property_name,
3968 PropertyType (Class::*property)()
const &,
3969 const PropertyMatcher& matcher) {
3970 return MakePolymorphicMatcher(
3971 internal::PropertyMatcher<Class, PropertyType,
3972 PropertyType (Class::*)()
const&>(
3973 property_name, property, MatcherCast<const PropertyType&>(matcher)));
3984 template <
typename Callable,
typename InnerMatcher>
3985 internal::ResultOfMatcher<Callable, InnerMatcher> ResultOf(
3986 Callable callable, InnerMatcher matcher) {
3987 return internal::ResultOfMatcher<Callable, InnerMatcher>(
3988 std::move(callable), std::move(matcher));
3994 template <
typename T = std::
string>
3995 PolymorphicMatcher<internal::StrEqualityMatcher<std::string> > StrEq(
3996 const internal::StringLike<T>& str) {
3997 return MakePolymorphicMatcher(
3998 internal::StrEqualityMatcher<std::string>(std::string(str),
true,
true));
4002 template <
typename T = std::
string>
4003 PolymorphicMatcher<internal::StrEqualityMatcher<std::string> > StrNe(
4004 const internal::StringLike<T>& str) {
4005 return MakePolymorphicMatcher(
4006 internal::StrEqualityMatcher<std::string>(std::string(str),
false,
true));
4010 template <
typename T = std::
string>
4011 PolymorphicMatcher<internal::StrEqualityMatcher<std::string> > StrCaseEq(
4012 const internal::StringLike<T>& str) {
4013 return MakePolymorphicMatcher(
4014 internal::StrEqualityMatcher<std::string>(std::string(str),
true,
false));
4018 template <
typename T = std::
string>
4019 PolymorphicMatcher<internal::StrEqualityMatcher<std::string> > StrCaseNe(
4020 const internal::StringLike<T>& str) {
4021 return MakePolymorphicMatcher(internal::StrEqualityMatcher<std::string>(
4022 std::string(str),
false,
false));
4027 template <
typename T = std::
string>
4028 PolymorphicMatcher<internal::HasSubstrMatcher<std::string> > HasSubstr(
4029 const internal::StringLike<T>& substring) {
4030 return MakePolymorphicMatcher(
4031 internal::HasSubstrMatcher<std::string>(std::string(substring)));
4035 template <
typename T = std::
string>
4036 PolymorphicMatcher<internal::StartsWithMatcher<std::string> > StartsWith(
4037 const internal::StringLike<T>& prefix) {
4038 return MakePolymorphicMatcher(
4039 internal::StartsWithMatcher<std::string>(std::string(prefix)));
4043 template <
typename T = std::
string>
4044 PolymorphicMatcher<internal::EndsWithMatcher<std::string> > EndsWith(
4045 const internal::StringLike<T>& suffix) {
4046 return MakePolymorphicMatcher(
4047 internal::EndsWithMatcher<std::string>(std::string(suffix)));
4050 #if GTEST_HAS_STD_WSTRING
4054 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring> > StrEq(
4055 const std::wstring& str) {
4056 return MakePolymorphicMatcher(
4057 internal::StrEqualityMatcher<std::wstring>(str,
true,
true));
4061 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring> > StrNe(
4062 const std::wstring& str) {
4063 return MakePolymorphicMatcher(
4064 internal::StrEqualityMatcher<std::wstring>(str,
false,
true));
4068 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring> >
4069 StrCaseEq(
const std::wstring& str) {
4070 return MakePolymorphicMatcher(
4071 internal::StrEqualityMatcher<std::wstring>(str,
true,
false));
4075 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring> >
4076 StrCaseNe(
const std::wstring& str) {
4077 return MakePolymorphicMatcher(
4078 internal::StrEqualityMatcher<std::wstring>(str,
false,
false));
4083 inline PolymorphicMatcher<internal::HasSubstrMatcher<std::wstring> > HasSubstr(
4084 const std::wstring& substring) {
4085 return MakePolymorphicMatcher(
4086 internal::HasSubstrMatcher<std::wstring>(substring));
4090 inline PolymorphicMatcher<internal::StartsWithMatcher<std::wstring> >
4091 StartsWith(
const std::wstring& prefix) {
4092 return MakePolymorphicMatcher(
4093 internal::StartsWithMatcher<std::wstring>(prefix));
4097 inline PolymorphicMatcher<internal::EndsWithMatcher<std::wstring> > EndsWith(
4098 const std::wstring& suffix) {
4099 return MakePolymorphicMatcher(
4100 internal::EndsWithMatcher<std::wstring>(suffix));
4103 #endif // GTEST_HAS_STD_WSTRING
4107 inline internal::Eq2Matcher Eq() {
return internal::Eq2Matcher(); }
4111 inline internal::Ge2Matcher Ge() {
return internal::Ge2Matcher(); }
4115 inline internal::Gt2Matcher Gt() {
return internal::Gt2Matcher(); }
4119 inline internal::Le2Matcher Le() {
return internal::Le2Matcher(); }
4123 inline internal::Lt2Matcher Lt() {
return internal::Lt2Matcher(); }
4127 inline internal::Ne2Matcher Ne() {
return internal::Ne2Matcher(); }
4131 inline internal::FloatingEq2Matcher<float> FloatEq() {
4132 return internal::FloatingEq2Matcher<float>();
4137 inline internal::FloatingEq2Matcher<double> DoubleEq() {
4138 return internal::FloatingEq2Matcher<double>();
4143 inline internal::FloatingEq2Matcher<float> NanSensitiveFloatEq() {
4144 return internal::FloatingEq2Matcher<float>(
true);
4149 inline internal::FloatingEq2Matcher<double> NanSensitiveDoubleEq() {
4150 return internal::FloatingEq2Matcher<double>(
true);
4155 inline internal::FloatingEq2Matcher<float> FloatNear(
float max_abs_error) {
4156 return internal::FloatingEq2Matcher<float>(max_abs_error);
4161 inline internal::FloatingEq2Matcher<double> DoubleNear(
double max_abs_error) {
4162 return internal::FloatingEq2Matcher<double>(max_abs_error);
4168 inline internal::FloatingEq2Matcher<float> NanSensitiveFloatNear(
4169 float max_abs_error) {
4170 return internal::FloatingEq2Matcher<float>(max_abs_error,
true);
4176 inline internal::FloatingEq2Matcher<double> NanSensitiveDoubleNear(
4177 double max_abs_error) {
4178 return internal::FloatingEq2Matcher<double>(max_abs_error,
true);
4183 template <
typename InnerMatcher>
4184 inline internal::NotMatcher<InnerMatcher> Not(InnerMatcher m) {
4185 return internal::NotMatcher<InnerMatcher>(m);
4191 template <
typename Predicate>
4192 inline PolymorphicMatcher<internal::TrulyMatcher<Predicate> >
4193 Truly(Predicate pred) {
4194 return MakePolymorphicMatcher(internal::TrulyMatcher<Predicate>(pred));
4203 template <
typename SizeMatcher>
4204 inline internal::SizeIsMatcher<SizeMatcher>
4205 SizeIs(
const SizeMatcher& size_matcher) {
4206 return internal::SizeIsMatcher<SizeMatcher>(size_matcher);
4214 template <
typename DistanceMatcher>
4215 inline internal::BeginEndDistanceIsMatcher<DistanceMatcher>
4216 BeginEndDistanceIs(
const DistanceMatcher& distance_matcher) {
4217 return internal::BeginEndDistanceIsMatcher<DistanceMatcher>(distance_matcher);
4224 template <
typename Container>
4225 inline PolymorphicMatcher<internal::ContainerEqMatcher<
4226 typename std::remove_const<Container>::type>>
4227 ContainerEq(
const Container& rhs) {
4228 return MakePolymorphicMatcher(internal::ContainerEqMatcher<Container>(rhs));
4233 template <
typename Comparator,
typename ContainerMatcher>
4234 inline internal::WhenSortedByMatcher<Comparator, ContainerMatcher>
4235 WhenSortedBy(
const Comparator& comparator,
4236 const ContainerMatcher& container_matcher) {
4237 return internal::WhenSortedByMatcher<Comparator, ContainerMatcher>(
4238 comparator, container_matcher);
4243 template <
typename ContainerMatcher>
4244 inline internal::WhenSortedByMatcher<internal::LessComparator, ContainerMatcher>
4245 WhenSorted(
const ContainerMatcher& container_matcher) {
4247 internal::WhenSortedByMatcher<internal::LessComparator, ContainerMatcher>(
4248 internal::LessComparator(), container_matcher);
4257 template <
typename TupleMatcher,
typename Container>
4258 inline internal::PointwiseMatcher<TupleMatcher,
4259 typename std::remove_const<Container>::type>
4260 Pointwise(
const TupleMatcher& tuple_matcher,
const Container& rhs) {
4261 return internal::PointwiseMatcher<TupleMatcher, Container>(tuple_matcher,
4267 template <
typename TupleMatcher,
typename T>
4268 inline internal::PointwiseMatcher<TupleMatcher, std::vector<T> > Pointwise(
4269 const TupleMatcher& tuple_matcher, std::initializer_list<T> rhs) {
4270 return Pointwise(tuple_matcher, std::vector<T>(rhs));
4285 template <
typename Tuple2Matcher,
typename RhsContainer>
4286 inline internal::UnorderedElementsAreArrayMatcher<
4287 typename internal::BoundSecondMatcher<
4289 typename internal::StlContainerView<
4290 typename std::remove_const<RhsContainer>::type>::type::value_type>>
4291 UnorderedPointwise(
const Tuple2Matcher& tuple2_matcher,
4292 const RhsContainer& rhs_container) {
4295 typedef typename internal::StlContainerView<RhsContainer> RhsView;
4296 typedef typename RhsView::type RhsStlContainer;
4297 typedef typename RhsStlContainer::value_type Second;
4298 const RhsStlContainer& rhs_stl_container =
4299 RhsView::ConstReference(rhs_container);
4302 ::std::vector<internal::BoundSecondMatcher<Tuple2Matcher, Second> > matchers;
4303 for (
typename RhsStlContainer::const_iterator it = rhs_stl_container.begin();
4304 it != rhs_stl_container.end(); ++it) {
4306 internal::MatcherBindSecond(tuple2_matcher, *it));
4310 return UnorderedElementsAreArray(matchers);
4315 template <
typename Tuple2Matcher,
typename T>
4316 inline internal::UnorderedElementsAreArrayMatcher<
4317 typename internal::BoundSecondMatcher<Tuple2Matcher, T> >
4318 UnorderedPointwise(
const Tuple2Matcher& tuple2_matcher,
4319 std::initializer_list<T> rhs) {
4320 return UnorderedPointwise(tuple2_matcher, std::vector<T>(rhs));
4342 template <
typename M>
4343 inline internal::ContainsMatcher<M> Contains(M matcher) {
4344 return internal::ContainsMatcher<M>(matcher);
4374 template <
typename Iter>
4375 inline internal::UnorderedElementsAreArrayMatcher<
4376 typename ::std::iterator_traits<Iter>::value_type>
4377 IsSupersetOf(Iter first, Iter last) {
4378 typedef typename ::std::iterator_traits<Iter>::value_type
T;
4379 return internal::UnorderedElementsAreArrayMatcher<T>(
4380 internal::UnorderedMatcherRequire::Superset, first, last);
4383 template <
typename T>
4384 inline internal::UnorderedElementsAreArrayMatcher<T> IsSupersetOf(
4385 const T* pointer,
size_t count) {
4386 return IsSupersetOf(pointer, pointer + count);
4389 template <
typename T,
size_t N>
4390 inline internal::UnorderedElementsAreArrayMatcher<T> IsSupersetOf(
4391 const T (&array)[N]) {
4392 return IsSupersetOf(array, N);
4395 template <
typename Container>
4396 inline internal::UnorderedElementsAreArrayMatcher<
4397 typename Container::value_type>
4398 IsSupersetOf(
const Container& container) {
4399 return IsSupersetOf(container.begin(), container.end());
4402 template <
typename T>
4403 inline internal::UnorderedElementsAreArrayMatcher<T> IsSupersetOf(
4404 ::std::initializer_list<T> xs) {
4405 return IsSupersetOf(xs.begin(), xs.end());
4431 template <
typename Iter>
4432 inline internal::UnorderedElementsAreArrayMatcher<
4433 typename ::std::iterator_traits<Iter>::value_type>
4434 IsSubsetOf(Iter first, Iter last) {
4435 typedef typename ::std::iterator_traits<Iter>::value_type
T;
4436 return internal::UnorderedElementsAreArrayMatcher<T>(
4437 internal::UnorderedMatcherRequire::Subset, first, last);
4440 template <
typename T>
4441 inline internal::UnorderedElementsAreArrayMatcher<T> IsSubsetOf(
4442 const T* pointer,
size_t count) {
4443 return IsSubsetOf(pointer, pointer + count);
4446 template <
typename T,
size_t N>
4447 inline internal::UnorderedElementsAreArrayMatcher<T> IsSubsetOf(
4448 const T (&array)[N]) {
4449 return IsSubsetOf(array, N);
4452 template <
typename Container>
4453 inline internal::UnorderedElementsAreArrayMatcher<
4454 typename Container::value_type>
4455 IsSubsetOf(
const Container& container) {
4456 return IsSubsetOf(container.begin(), container.end());
4459 template <
typename T>
4460 inline internal::UnorderedElementsAreArrayMatcher<T> IsSubsetOf(
4461 ::std::initializer_list<T> xs) {
4462 return IsSubsetOf(xs.begin(), xs.end());
4492 template <
typename M>
4493 inline internal::EachMatcher<M> Each(M matcher) {
4494 return internal::EachMatcher<M>(matcher);
4500 template <
typename M>
4501 inline internal::KeyMatcher<M> Key(M inner_matcher) {
4502 return internal::KeyMatcher<M>(inner_matcher);
4510 template <
typename FirstMatcher,
typename SecondMatcher>
4511 inline internal::PairMatcher<FirstMatcher, SecondMatcher>
4512 Pair(FirstMatcher first_matcher, SecondMatcher second_matcher) {
4513 return internal::PairMatcher<FirstMatcher, SecondMatcher>(
4514 first_matcher, second_matcher);
4519 template <
typename M>
4520 inline internal::MatcherAsPredicate<M> Matches(M matcher) {
4521 return internal::MatcherAsPredicate<M>(matcher);
4525 template <
typename T,
typename M>
4526 inline bool Value(
const T& value, M matcher) {
4527 return testing::Matches(matcher)(
value);
4532 template <
typename T,
typename M>
4533 inline bool ExplainMatchResult(
4534 M matcher,
const T& value, MatchResultListener* listener) {
4535 return SafeMatcherCast<const T&>(matcher).MatchAndExplain(value, listener);
4548 template <
typename T,
typename M>
4549 std::string DescribeMatcher(
const M& matcher,
bool negation =
false) {
4550 ::std::stringstream ss;
4551 Matcher<T> monomorphic_matcher = SafeMatcherCast<T>(matcher);
4553 monomorphic_matcher.DescribeNegationTo(&ss);
4555 monomorphic_matcher.DescribeTo(&ss);
4560 template <
typename... Args>
4561 internal::ElementsAreMatcher<
4562 std::tuple<typename std::decay<const Args&>::type...>>
4563 ElementsAre(
const Args&... matchers) {
4564 return internal::ElementsAreMatcher<
4565 std::tuple<typename std::decay<const Args&>::type...>>(
4566 std::make_tuple(matchers...));
4569 template <
typename... Args>
4570 internal::UnorderedElementsAreMatcher<
4571 std::tuple<typename std::decay<const Args&>::type...>>
4572 UnorderedElementsAre(
const Args&... matchers) {
4573 return internal::UnorderedElementsAreMatcher<
4574 std::tuple<typename std::decay<const Args&>::type...>>(
4575 std::make_tuple(matchers...));
4579 template <
typename... Args>
4580 internal::AllOfMatcher<typename std::decay<const Args&>::type...> AllOf(
4581 const Args&... matchers) {
4582 return internal::AllOfMatcher<typename std::decay<const Args&>::type...>(
4586 template <
typename... Args>
4587 internal::AnyOfMatcher<typename std::decay<const Args&>::type...> AnyOf(
4588 const Args&... matchers) {
4589 return internal::AnyOfMatcher<typename std::decay<const Args&>::type...>(
4615 template <
typename Iter>
4616 inline internal::AnyOfArrayMatcher<
4617 typename ::std::iterator_traits<Iter>::value_type>
4618 AnyOfArray(Iter first, Iter last) {
4619 return internal::AnyOfArrayMatcher<
4620 typename ::std::iterator_traits<Iter>::value_type>(first, last);
4623 template <
typename Iter>
4624 inline internal::AllOfArrayMatcher<
4625 typename ::std::iterator_traits<Iter>::value_type>
4626 AllOfArray(Iter first, Iter last) {
4627 return internal::AllOfArrayMatcher<
4628 typename ::std::iterator_traits<Iter>::value_type>(first, last);
4631 template <
typename T>
4632 inline internal::AnyOfArrayMatcher<T> AnyOfArray(
const T* ptr,
size_t count) {
4633 return AnyOfArray(ptr, ptr + count);
4636 template <
typename T>
4637 inline internal::AllOfArrayMatcher<T> AllOfArray(
const T* ptr,
size_t count) {
4638 return AllOfArray(ptr, ptr + count);
4641 template <
typename T,
size_t N>
4642 inline internal::AnyOfArrayMatcher<T> AnyOfArray(
const T (&array)[N]) {
4643 return AnyOfArray(array, N);
4646 template <
typename T,
size_t N>
4647 inline internal::AllOfArrayMatcher<T> AllOfArray(
const T (&array)[N]) {
4648 return AllOfArray(array, N);
4651 template <
typename Container>
4652 inline internal::AnyOfArrayMatcher<typename Container::value_type> AnyOfArray(
4653 const Container& container) {
4654 return AnyOfArray(container.begin(), container.end());
4657 template <
typename Container>
4658 inline internal::AllOfArrayMatcher<typename Container::value_type> AllOfArray(
4659 const Container& container) {
4660 return AllOfArray(container.begin(), container.end());
4663 template <
typename T>
4664 inline internal::AnyOfArrayMatcher<T> AnyOfArray(
4665 ::std::initializer_list<T> xs) {
4666 return AnyOfArray(xs.begin(), xs.end());
4669 template <
typename T>
4670 inline internal::AllOfArrayMatcher<T> AllOfArray(
4671 ::std::initializer_list<T> xs) {
4672 return AllOfArray(xs.begin(), xs.end());
4678 template <
size_t... k,
typename InnerMatcher>
4679 internal::ArgsMatcher<typename std::decay<InnerMatcher>::type, k...> Args(
4680 InnerMatcher&& matcher) {
4681 return internal::ArgsMatcher<typename std::decay<InnerMatcher>::type, k...>(
4682 std::forward<InnerMatcher>(matcher));
4692 template <
typename InnerMatcher>
4693 inline InnerMatcher AllArgs(
const InnerMatcher& matcher) {
return matcher; }
4703 template <
typename ValueMatcher>
4704 inline internal::OptionalMatcher<ValueMatcher> Optional(
4705 const ValueMatcher& value_matcher) {
4706 return internal::OptionalMatcher<ValueMatcher>(value_matcher);
4710 template <
typename T>
4711 PolymorphicMatcher<internal::any_cast_matcher::AnyCastMatcher<T> > AnyWith(
4712 const Matcher<const T&>& matcher) {
4713 return MakePolymorphicMatcher(
4714 internal::any_cast_matcher::AnyCastMatcher<T>(matcher));
4721 template <
typename T>
4722 PolymorphicMatcher<internal::variant_matcher::VariantMatcher<T> > VariantWith(
4723 const Matcher<const T&>& matcher) {
4724 return MakePolymorphicMatcher(
4725 internal::variant_matcher::VariantMatcher<T>(matcher));
4732 #define ASSERT_THAT(value, matcher) ASSERT_PRED_FORMAT1(\
4733 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
4734 #define EXPECT_THAT(value, matcher) EXPECT_PRED_FORMAT1(\
4735 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
4738 #define MATCHER(name, description) \
4739 class name##Matcher \
4740 : public ::testing::internal::MatcherBaseImpl<name##Matcher> { \
4742 template <typename arg_type> \
4743 class gmock_Impl : public ::testing::MatcherInterface<const arg_type&> { \
4746 bool MatchAndExplain( \
4747 const arg_type& arg, \
4748 ::testing::MatchResultListener* result_listener) const override; \
4749 void DescribeTo(::std::ostream* gmock_os) const override { \
4750 *gmock_os << FormatDescription(false); \
4752 void DescribeNegationTo(::std::ostream* gmock_os) const override { \
4753 *gmock_os << FormatDescription(true); \
4757 ::std::string FormatDescription(bool negation) const { \
4758 ::std::string gmock_description = (description); \
4759 if (!gmock_description.empty()) { \
4760 return gmock_description; \
4762 return ::testing::internal::FormatMatcherDescription(negation, #name, \
4767 GTEST_ATTRIBUTE_UNUSED_ inline name##Matcher name() { return {}; } \
4768 template <typename arg_type> \
4769 bool name##Matcher::gmock_Impl<arg_type>::MatchAndExplain( \
4770 const arg_type& arg, \
4771 ::testing::MatchResultListener* result_listener GTEST_ATTRIBUTE_UNUSED_) \
4774 #define MATCHER_P(name, p0, description) \
4775 GMOCK_INTERNAL_MATCHER(name, name##MatcherP, description, (p0))
4776 #define MATCHER_P2(name, p0, p1, description) \
4777 GMOCK_INTERNAL_MATCHER(name, name##MatcherP2, description, (p0, p1))
4778 #define MATCHER_P3(name, p0, p1, p2, description) \
4779 GMOCK_INTERNAL_MATCHER(name, name##MatcherP3, description, (p0, p1, p2))
4780 #define MATCHER_P4(name, p0, p1, p2, p3, description) \
4781 GMOCK_INTERNAL_MATCHER(name, name##MatcherP4, description, (p0, p1, p2, p3))
4782 #define MATCHER_P5(name, p0, p1, p2, p3, p4, description) \
4783 GMOCK_INTERNAL_MATCHER(name, name##MatcherP5, description, \
4784 (p0, p1, p2, p3, p4))
4785 #define MATCHER_P6(name, p0, p1, p2, p3, p4, p5, description) \
4786 GMOCK_INTERNAL_MATCHER(name, name##MatcherP6, description, \
4787 (p0, p1, p2, p3, p4, p5))
4788 #define MATCHER_P7(name, p0, p1, p2, p3, p4, p5, p6, description) \
4789 GMOCK_INTERNAL_MATCHER(name, name##MatcherP7, description, \
4790 (p0, p1, p2, p3, p4, p5, p6))
4791 #define MATCHER_P8(name, p0, p1, p2, p3, p4, p5, p6, p7, description) \
4792 GMOCK_INTERNAL_MATCHER(name, name##MatcherP8, description, \
4793 (p0, p1, p2, p3, p4, p5, p6, p7))
4794 #define MATCHER_P9(name, p0, p1, p2, p3, p4, p5, p6, p7, p8, description) \
4795 GMOCK_INTERNAL_MATCHER(name, name##MatcherP9, description, \
4796 (p0, p1, p2, p3, p4, p5, p6, p7, p8))
4797 #define MATCHER_P10(name, p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, description) \
4798 GMOCK_INTERNAL_MATCHER(name, name##MatcherP10, description, \
4799 (p0, p1, p2, p3, p4, p5, p6, p7, p8, p9))
4801 #define GMOCK_INTERNAL_MATCHER(name, full_name, description, args) \
4802 template <GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args)> \
4803 class full_name : public ::testing::internal::MatcherBaseImpl< \
4804 full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>> { \
4806 using full_name::MatcherBaseImpl::MatcherBaseImpl; \
4807 template <typename arg_type> \
4808 class gmock_Impl : public ::testing::MatcherInterface<const arg_type&> { \
4810 explicit gmock_Impl(GMOCK_INTERNAL_MATCHER_FUNCTION_ARGS(args)) \
4811 : GMOCK_INTERNAL_MATCHER_FORWARD_ARGS(args) {} \
4812 bool MatchAndExplain( \
4813 const arg_type& arg, \
4814 ::testing::MatchResultListener* result_listener) const override; \
4815 void DescribeTo(::std::ostream* gmock_os) const override { \
4816 *gmock_os << FormatDescription(false); \
4818 void DescribeNegationTo(::std::ostream* gmock_os) const override { \
4819 *gmock_os << FormatDescription(true); \
4821 GMOCK_INTERNAL_MATCHER_MEMBERS(args) \
4824 ::std::string FormatDescription(bool negation) const { \
4825 ::std::string gmock_description = (description); \
4826 if (!gmock_description.empty()) { \
4827 return gmock_description; \
4829 return ::testing::internal::FormatMatcherDescription( \
4831 ::testing::internal::UniversalTersePrintTupleFieldsToStrings( \
4832 ::std::tuple<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>( \
4833 GMOCK_INTERNAL_MATCHER_MEMBERS_USAGE(args)))); \
4837 template <GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args)> \
4838 inline full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)> name( \
4839 GMOCK_INTERNAL_MATCHER_FUNCTION_ARGS(args)) { \
4840 return full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>( \
4841 GMOCK_INTERNAL_MATCHER_ARGS_USAGE(args)); \
4843 template <GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args)> \
4844 template <typename arg_type> \
4845 bool full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>::gmock_Impl< \
4846 arg_type>::MatchAndExplain(const arg_type& arg, \
4847 ::testing::MatchResultListener* \
4848 result_listener GTEST_ATTRIBUTE_UNUSED_) \
4851 #define GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args) \
4853 GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAM, , args))
4854 #define GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAM(i_unused, data_unused, arg) \
4855 , typename arg##_type
4857 #define GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args) \
4858 GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_TYPE_PARAM, , args))
4859 #define GMOCK_INTERNAL_MATCHER_TYPE_PARAM(i_unused, data_unused, arg) \
4862 #define GMOCK_INTERNAL_MATCHER_FUNCTION_ARGS(args) \
4863 GMOCK_PP_TAIL(dummy_first GMOCK_PP_FOR_EACH( \
4864 GMOCK_INTERNAL_MATCHER_FUNCTION_ARG, , args))
4865 #define GMOCK_INTERNAL_MATCHER_FUNCTION_ARG(i, data_unused, arg) \
4866 , arg##_type gmock_p##i
4868 #define GMOCK_INTERNAL_MATCHER_FORWARD_ARGS(args) \
4869 GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_FORWARD_ARG, , args))
4870 #define GMOCK_INTERNAL_MATCHER_FORWARD_ARG(i, data_unused, arg) \
4871 , arg(::std::forward<arg##_type>(gmock_p##i))
4873 #define GMOCK_INTERNAL_MATCHER_MEMBERS(args) \
4874 GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_MEMBER, , args)
4875 #define GMOCK_INTERNAL_MATCHER_MEMBER(i_unused, data_unused, arg) \
4876 const arg##_type arg;
4878 #define GMOCK_INTERNAL_MATCHER_MEMBERS_USAGE(args) \
4879 GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_MEMBER_USAGE, , args))
4880 #define GMOCK_INTERNAL_MATCHER_MEMBER_USAGE(i_unused, data_unused, arg) , arg
4882 #define GMOCK_INTERNAL_MATCHER_ARGS_USAGE(args) \
4883 GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_ARG_USAGE, , args))
4884 #define GMOCK_INTERNAL_MATCHER_ARG_USAGE(i, data_unused, arg_unused) \
4894 #include "gmock/internal/custom/gmock-matchers.h"
4896 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_
Sacado::Fad::DFad< double > F
#define GMOCK_KIND_OF_(type)
AssertionResult AssertionFailure()
#define GTEST_DISABLE_MSC_WARNINGS_POP_()
::std::string PrintToString(const T &value)
auto Apply(F &&f, Tuple &&args) -> decltype(ApplyImpl(std::forward< F >(f), std::forward< Tuple >(args), MakeIndexSequence< std::tuple_size< typename std::remove_reference< Tuple >::type >::value >()))
#define GTEST_LOG_(severity)
std::string Print(const T &value)
::std::vector< ::std::string > Strings
std::string GetTypeName()
std::decay< FunctionImpl >::type Invoke(FunctionImpl &&function_impl)
CacheTaylor< T > diff(const CacheTaylor< T > &x, int n=1)
Compute Taylor series of n-th derivative of x.
#define GTEST_COMPILE_ASSERT_(expr, msg)
AssertionResult AssertionSuccess()
#define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings)
#define GTEST_CHECK_(condition)
void UniversalPrint(const T &value,::std::ostream *os)
#define GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement)
GTEST_API_ std::string FormatMatcherDescription(bool negation, const char *matcher_name, const Strings ¶m_values)
GTEST_API_ ElementMatcherPairs FindMaxBipartiteMatching(const MatchMatrix &g)
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
#define GMOCK_MAYBE_5046_
<< DiffStrings(str, arg);
Iter ArrayAwareFind(Iter begin, Iter end, const Element &elem)
const Pointer::element_type * GetRawPointer(const Pointer &p)
AssertionResult IsNull(const char *str)
#define GTEST_REMOVE_REFERENCE_AND_CONST_(T)
static ExpectedAnswer expected[4]