255 #ifndef GOOGLETEST_GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_
256 #define GOOGLETEST_GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_
260 #include <initializer_list>
267 #include <type_traits>
271 #include "gmock/internal/gmock-internal-utils.h"
272 #include "gmock/internal/gmock-port.h"
273 #include "gmock/internal/gmock-pp.h"
274 #include "gtest/gtest.h"
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) ||
423 std::is_same<FromType, ToType>::value ||
424 !std::is_base_of<FromType, ToType>::value,
425 "Can't implicitly convert from <base> to <derived>");
430 typename std::conditional<std::is_convertible<T&, const U&>::value,
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 {
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) {
532 static_assert(std::is_convertible<const T&, const U&>::value,
533 "T must be implicitly convertible to U");
537 std::is_reference<T>::value || !std::is_reference<U>::value,
538 cannot_convert_non_reference_arg_to_reference);
546 kTIsOther || kUIsOther ||
547 (internal::LosslessArithmeticConvertible<RawT, RawU>::value),
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) {
682 std::tuple_size<ValueTuple>::value,
683 matcher_and_value_have_different_numbers_of_fields);
684 return TuplePrefix<std::tuple_size<ValueTuple>::value>::Matches(matcher_tuple,
690 template <
typename MatcherTuple,
typename ValueTuple>
691 void ExplainMatchFailureTupleTo(
const MatcherTuple& matchers,
692 const ValueTuple& values,
693 ::std::ostream* os) {
694 TuplePrefix<std::tuple_size<MatcherTuple>::value>::ExplainMatchFailuresTo(
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);
742 class AnythingMatcher {
744 using is_gtest_matcher = void;
746 template <
typename T>
747 bool MatchAndExplain(
const T& , std::ostream* )
const {
750 void DescribeTo(std::ostream* os)
const { *os <<
"is anything"; }
751 void DescribeNegationTo(::std::ostream* os)
const {
755 *os <<
"never matches";
761 class IsNullMatcher {
763 template <
typename Po
inter>
764 bool MatchAndExplain(
const Pointer& p,
765 MatchResultListener* )
const {
769 void DescribeTo(::std::ostream* os)
const { *os <<
"is NULL"; }
770 void DescribeNegationTo(::std::ostream* os)
const {
777 class NotNullMatcher {
779 template <
typename Po
inter>
780 bool MatchAndExplain(
const Pointer& p,
781 MatchResultListener* )
const {
785 void DescribeTo(::std::ostream* os)
const { *os <<
"isn't NULL"; }
786 void DescribeNegationTo(::std::ostream* os)
const {
804 template <
typename T>
807 template <
typename T>
808 class RefMatcher<T&> {
818 explicit RefMatcher(T& x) : object_(x) {}
820 template <
typename Super>
821 operator Matcher<Super&>()
const {
827 return MakeMatcher(
new Impl<Super>(object_));
831 template <
typename Super>
832 class Impl :
public MatcherInterface<Super&> {
834 explicit Impl(Super& x) : object_(x) {}
838 bool MatchAndExplain(Super& x,
839 MatchResultListener* listener)
const override {
840 *listener <<
"which is located @" <<
static_cast<const void*
>(&x);
841 return &x == &object_;
844 void DescribeTo(::std::ostream* os)
const override {
845 *os <<
"references the variable ";
849 void DescribeNegationTo(::std::ostream* os)
const override {
850 *os <<
"does not reference the variable ";
855 const Super& object_;
862 inline bool CaseInsensitiveCStringEquals(
const char* lhs,
const char* rhs) {
866 inline bool CaseInsensitiveCStringEquals(
const wchar_t* lhs,
867 const wchar_t* rhs) {
873 template <
typename StringType>
874 bool CaseInsensitiveStringEquals(
const StringType& s1,
875 const StringType& s2) {
877 if (!CaseInsensitiveCStringEquals(s1.c_str(), s2.c_str())) {
882 const typename StringType::value_type nul = 0;
883 const size_t i1 = s1.find(nul), i2 = s2.find(nul);
886 if (i1 == StringType::npos || i2 == StringType::npos) {
891 return CaseInsensitiveStringEquals(s1.substr(i1 + 1), s2.substr(i2 + 1));
897 template <
typename StringType>
898 class StrEqualityMatcher {
900 StrEqualityMatcher(StringType str,
bool expect_eq,
bool case_sensitive)
901 : string_(std::move(str)),
902 expect_eq_(expect_eq),
903 case_sensitive_(case_sensitive) {}
905 #if GTEST_INTERNAL_HAS_STRING_VIEW
906 bool MatchAndExplain(
const internal::StringView& s,
907 MatchResultListener* listener)
const {
910 const StringType& str = std::string(s);
911 return MatchAndExplain(str, listener);
920 template <
typename CharType>
921 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
925 return MatchAndExplain(StringType(s), listener);
932 template <
typename MatcheeStringType>
933 bool MatchAndExplain(
const MatcheeStringType& s,
934 MatchResultListener* )
const {
935 const StringType s2(s);
936 const bool eq = case_sensitive_ ? s2 == string_ :
937 CaseInsensitiveStringEquals(s2, string_);
938 return expect_eq_ == eq;
941 void DescribeTo(::std::ostream* os)
const {
942 DescribeToHelper(expect_eq_, os);
945 void DescribeNegationTo(::std::ostream* os)
const {
946 DescribeToHelper(!expect_eq_, os);
950 void DescribeToHelper(
bool expect_eq, ::std::ostream* os)
const {
951 *os << (expect_eq ?
"is " :
"isn't ");
953 if (!case_sensitive_) {
954 *os <<
"(ignoring case) ";
959 const StringType string_;
960 const bool expect_eq_;
961 const bool case_sensitive_;
967 template <
typename StringType>
968 class HasSubstrMatcher {
970 explicit HasSubstrMatcher(
const StringType& substring)
971 : substring_(substring) {}
973 #if GTEST_INTERNAL_HAS_STRING_VIEW
974 bool MatchAndExplain(
const internal::StringView& s,
975 MatchResultListener* listener)
const {
978 const StringType& str = std::string(s);
979 return MatchAndExplain(str, listener);
988 template <
typename CharType>
989 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
990 return s !=
nullptr && MatchAndExplain(StringType(s), listener);
997 template <
typename MatcheeStringType>
998 bool MatchAndExplain(
const MatcheeStringType& s,
999 MatchResultListener* )
const {
1000 return StringType(s).find(substring_) != StringType::npos;
1004 void DescribeTo(::std::ostream* os)
const {
1005 *os <<
"has substring ";
1009 void DescribeNegationTo(::std::ostream* os)
const {
1010 *os <<
"has no substring ";
1015 const StringType substring_;
1021 template <
typename StringType>
1022 class StartsWithMatcher {
1024 explicit StartsWithMatcher(
const StringType& prefix) : prefix_(prefix) {
1027 #if GTEST_INTERNAL_HAS_STRING_VIEW
1028 bool MatchAndExplain(
const internal::StringView& s,
1029 MatchResultListener* listener)
const {
1032 const StringType& str = std::string(s);
1033 return MatchAndExplain(str, listener);
1042 template <
typename CharType>
1043 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
1044 return s !=
nullptr && MatchAndExplain(StringType(s), listener);
1051 template <
typename MatcheeStringType>
1052 bool MatchAndExplain(
const MatcheeStringType& s,
1053 MatchResultListener* )
const {
1054 const StringType& s2(s);
1055 return s2.length() >= prefix_.length() &&
1056 s2.substr(0, prefix_.length()) == prefix_;
1059 void DescribeTo(::std::ostream* os)
const {
1060 *os <<
"starts with ";
1064 void DescribeNegationTo(::std::ostream* os)
const {
1065 *os <<
"doesn't start with ";
1070 const StringType prefix_;
1076 template <
typename StringType>
1077 class EndsWithMatcher {
1079 explicit EndsWithMatcher(
const StringType& suffix) : suffix_(suffix) {}
1081 #if GTEST_INTERNAL_HAS_STRING_VIEW
1082 bool MatchAndExplain(
const internal::StringView& s,
1083 MatchResultListener* listener)
const {
1086 const StringType& str = std::string(s);
1087 return MatchAndExplain(str, listener);
1096 template <
typename CharType>
1097 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
1098 return s !=
nullptr && MatchAndExplain(StringType(s), listener);
1105 template <
typename MatcheeStringType>
1106 bool MatchAndExplain(
const MatcheeStringType& s,
1107 MatchResultListener* )
const {
1108 const StringType& s2(s);
1109 return s2.length() >= suffix_.length() &&
1110 s2.substr(s2.length() - suffix_.length()) == suffix_;
1113 void DescribeTo(::std::ostream* os)
const {
1114 *os <<
"ends with ";
1118 void DescribeNegationTo(::std::ostream* os)
const {
1119 *os <<
"doesn't end with ";
1124 const StringType suffix_;
1135 template <
typename D,
typename Op>
1136 class PairMatchBase {
1138 template <
typename T1,
typename T2>
1139 operator Matcher<::std::tuple<T1, T2>>()
const {
1140 return Matcher<::std::tuple<T1, T2>>(
new Impl<const ::std::tuple<T1, T2>&>);
1142 template <
typename T1,
typename T2>
1143 operator Matcher<const ::std::tuple<T1, T2>&>()
const {
1144 return MakeMatcher(
new Impl<const ::std::tuple<T1, T2>&>);
1148 static ::std::ostream& GetDesc(::std::ostream& os) {
1149 return os << D::Desc();
1152 template <
typename Tuple>
1153 class Impl :
public MatcherInterface<Tuple> {
1155 bool MatchAndExplain(Tuple args,
1156 MatchResultListener* )
const override {
1157 return Op()(::std::get<0>(args), ::std::get<1>(args));
1159 void DescribeTo(::std::ostream* os)
const override {
1160 *os <<
"are " << GetDesc;
1162 void DescribeNegationTo(::std::ostream* os)
const override {
1163 *os <<
"aren't " << GetDesc;
1168 class Eq2Matcher :
public PairMatchBase<Eq2Matcher, AnyEq> {
1170 static const char* Desc() {
return "an equal pair"; }
1172 class Ne2Matcher :
public PairMatchBase<Ne2Matcher, AnyNe> {
1174 static const char* Desc() {
return "an unequal pair"; }
1176 class Lt2Matcher :
public PairMatchBase<Lt2Matcher, AnyLt> {
1178 static const char* Desc() {
return "a pair where the first < the second"; }
1180 class Gt2Matcher :
public PairMatchBase<Gt2Matcher, AnyGt> {
1182 static const char* Desc() {
return "a pair where the first > the second"; }
1184 class Le2Matcher :
public PairMatchBase<Le2Matcher, AnyLe> {
1186 static const char* Desc() {
return "a pair where the first <= the second"; }
1188 class Ge2Matcher :
public PairMatchBase<Ge2Matcher, AnyGe> {
1190 static const char* Desc() {
return "a pair where the first >= the second"; }
1197 template <
typename T>
1198 class NotMatcherImpl :
public MatcherInterface<const T&> {
1200 explicit NotMatcherImpl(
const Matcher<T>& matcher)
1201 : matcher_(matcher) {}
1203 bool MatchAndExplain(
const T& x,
1204 MatchResultListener* listener)
const override {
1205 return !matcher_.MatchAndExplain(x, listener);
1208 void DescribeTo(::std::ostream* os)
const override {
1209 matcher_.DescribeNegationTo(os);
1212 void DescribeNegationTo(::std::ostream* os)
const override {
1213 matcher_.DescribeTo(os);
1217 const Matcher<T> matcher_;
1222 template <
typename InnerMatcher>
1225 explicit NotMatcher(InnerMatcher matcher) : matcher_(matcher) {}
1229 template <
typename T>
1230 operator Matcher<T>()
const {
1231 return Matcher<T>(
new NotMatcherImpl<T>(SafeMatcherCast<T>(matcher_)));
1235 InnerMatcher matcher_;
1242 template <
typename T>
1243 class AllOfMatcherImpl :
public MatcherInterface<const T&> {
1245 explicit AllOfMatcherImpl(std::vector<Matcher<T> > matchers)
1246 : matchers_(std::move(matchers)) {}
1248 void DescribeTo(::std::ostream* os)
const override {
1250 for (
size_t i = 0; i < matchers_.size(); ++i) {
1251 if (i != 0) *os <<
") and (";
1252 matchers_[i].DescribeTo(os);
1257 void DescribeNegationTo(::std::ostream* os)
const override {
1259 for (
size_t i = 0; i < matchers_.size(); ++i) {
1260 if (i != 0) *os <<
") or (";
1261 matchers_[i].DescribeNegationTo(os);
1266 bool MatchAndExplain(
const T& x,
1267 MatchResultListener* listener)
const override {
1270 std::string all_match_result;
1272 for (
size_t i = 0; i < matchers_.size(); ++i) {
1273 StringMatchResultListener slistener;
1274 if (matchers_[i].MatchAndExplain(x, &slistener)) {
1275 if (all_match_result.empty()) {
1276 all_match_result = slistener.str();
1278 std::string result = slistener.str();
1279 if (!result.empty()) {
1280 all_match_result +=
", and ";
1281 all_match_result += result;
1285 *listener << slistener.str();
1291 *listener << all_match_result;
1296 const std::vector<Matcher<T> > matchers_;
1303 template <
template <
typename T>
class CombiningMatcher,
typename... Args>
1304 class VariadicMatcher {
1306 VariadicMatcher(
const Args&... matchers)
1307 : matchers_(matchers...) {
1308 static_assert(
sizeof...(Args) > 0,
"Must have at least one matcher.");
1311 VariadicMatcher(
const VariadicMatcher&) =
default;
1312 VariadicMatcher& operator=(
const VariadicMatcher&) =
delete;
1317 template <
typename T>
1318 operator Matcher<T>()
const {
1319 std::vector<Matcher<T> > values;
1320 CreateVariadicMatcher<T>(&values, std::integral_constant<size_t, 0>());
1321 return Matcher<T>(
new CombiningMatcher<T>(std::move(values)));
1325 template <
typename T,
size_t I>
1326 void CreateVariadicMatcher(std::vector<Matcher<T> >* values,
1327 std::integral_constant<size_t, I>)
const {
1328 values->push_back(SafeMatcherCast<T>(std::get<I>(matchers_)));
1329 CreateVariadicMatcher<T>(values, std::integral_constant<size_t, I + 1>());
1332 template <
typename T>
1333 void CreateVariadicMatcher(
1334 std::vector<Matcher<T> >*,
1335 std::integral_constant<
size_t,
sizeof...(Args)>)
const {}
1337 std::tuple<Args...> matchers_;
1340 template <
typename... Args>
1341 using AllOfMatcher = VariadicMatcher<AllOfMatcherImpl, Args...>;
1347 template <
typename T>
1348 class AnyOfMatcherImpl :
public MatcherInterface<const T&> {
1350 explicit AnyOfMatcherImpl(std::vector<Matcher<T> > matchers)
1351 : matchers_(std::move(matchers)) {}
1353 void DescribeTo(::std::ostream* os)
const override {
1355 for (
size_t i = 0; i < matchers_.size(); ++i) {
1356 if (i != 0) *os <<
") or (";
1357 matchers_[i].DescribeTo(os);
1362 void DescribeNegationTo(::std::ostream* os)
const override {
1364 for (
size_t i = 0; i < matchers_.size(); ++i) {
1365 if (i != 0) *os <<
") and (";
1366 matchers_[i].DescribeNegationTo(os);
1371 bool MatchAndExplain(
const T& x,
1372 MatchResultListener* listener)
const override {
1373 std::string no_match_result;
1377 for (
size_t i = 0; i < matchers_.size(); ++i) {
1378 StringMatchResultListener slistener;
1379 if (matchers_[i].MatchAndExplain(x, &slistener)) {
1380 *listener << slistener.str();
1383 if (no_match_result.empty()) {
1384 no_match_result = slistener.str();
1386 std::string result = slistener.str();
1387 if (!result.empty()) {
1388 no_match_result +=
", and ";
1389 no_match_result += result;
1396 *listener << no_match_result;
1401 const std::vector<Matcher<T> > matchers_;
1405 template <
typename... Args>
1406 using AnyOfMatcher = VariadicMatcher<AnyOfMatcherImpl, Args...>;
1409 template <
template <
class>
class MatcherImpl,
typename T>
1410 class SomeOfArrayMatcher {
1414 template <
typename Iter>
1415 SomeOfArrayMatcher(Iter first, Iter last) : matchers_(first, last) {}
1417 template <
typename U>
1418 operator Matcher<U>()
const {
1419 using RawU =
typename std::decay<U>::type;
1420 std::vector<Matcher<RawU>> matchers;
1421 for (
const auto& matcher : matchers_) {
1422 matchers.push_back(MatcherCast<RawU>(matcher));
1424 return Matcher<U>(
new MatcherImpl<RawU>(std::move(matchers)));
1428 const ::std::vector<T> matchers_;
1431 template <
typename T>
1432 using AllOfArrayMatcher = SomeOfArrayMatcher<AllOfMatcherImpl, T>;
1434 template <
typename T>
1435 using AnyOfArrayMatcher = SomeOfArrayMatcher<AnyOfMatcherImpl, T>;
1439 template <
typename Predicate>
1440 class TrulyMatcher {
1442 explicit TrulyMatcher(Predicate pred) : predicate_(pred) {}
1448 template <
typename T>
1449 bool MatchAndExplain(T& x,
1450 MatchResultListener* listener)
const {
1459 *listener <<
"didn't satisfy the given predicate";
1463 void DescribeTo(::std::ostream* os)
const {
1464 *os <<
"satisfies the given predicate";
1467 void DescribeNegationTo(::std::ostream* os)
const {
1468 *os <<
"doesn't satisfy the given predicate";
1472 Predicate predicate_;
1477 template <
typename M>
1478 class MatcherAsPredicate {
1480 explicit MatcherAsPredicate(M matcher) : matcher_(matcher) {}
1488 template <
typename T>
1489 bool operator()(
const T& x)
const {
1504 return MatcherCast<const T&>(matcher_).Matches(x);
1513 template <
typename M>
1514 class PredicateFormatterFromMatcher {
1516 explicit PredicateFormatterFromMatcher(M m) : matcher_(std::move(m)) {}
1521 template <
typename T>
1522 AssertionResult operator()(
const char* value_text,
const T& x)
const {
1534 const Matcher<const T&> matcher = SafeMatcherCast<const T&>(matcher_);
1538 if (matcher.Matches(x)) {
1539 return AssertionSuccess();
1542 ::std::stringstream ss;
1543 ss <<
"Value of: " << value_text <<
"\n"
1545 matcher.DescribeTo(&ss);
1548 StringMatchResultListener listener;
1549 if (MatchPrintAndExplain(x, matcher, &listener)) {
1550 ss <<
"\n The matcher failed on the initial attempt; but passed when "
1551 "rerun to generate the explanation.";
1553 ss <<
"\n Actual: " << listener.str();
1554 return AssertionFailure() << ss.str();
1565 template <
typename M>
1566 inline PredicateFormatterFromMatcher<M>
1567 MakePredicateFormatterFromMatcher(M matcher) {
1568 return PredicateFormatterFromMatcher<M>(std::move(matcher));
1573 class IsNanMatcher {
1575 template <
typename FloatType>
1576 bool MatchAndExplain(
const FloatType& f,
1577 MatchResultListener* )
const {
1578 return (::std::isnan)(f);
1581 void DescribeTo(::std::ostream* os)
const { *os <<
"is NaN"; }
1582 void DescribeNegationTo(::std::ostream* os)
const {
1591 template <
typename FloatType>
1592 class FloatingEqMatcher {
1600 FloatingEqMatcher(FloatType expected,
bool nan_eq_nan) :
1601 expected_(expected), nan_eq_nan_(nan_eq_nan), max_abs_error_(-1) {
1607 FloatingEqMatcher(FloatType expected,
bool nan_eq_nan,
1608 FloatType max_abs_error)
1609 : expected_(expected),
1610 nan_eq_nan_(nan_eq_nan),
1611 max_abs_error_(max_abs_error) {
1613 <<
", where max_abs_error is" << max_abs_error;
1617 template <
typename T>
1618 class Impl :
public MatcherInterface<T> {
1620 Impl(FloatType expected,
bool nan_eq_nan, FloatType max_abs_error)
1621 : expected_(expected),
1622 nan_eq_nan_(nan_eq_nan),
1623 max_abs_error_(max_abs_error) {}
1625 bool MatchAndExplain(T value,
1626 MatchResultListener* listener)
const override {
1627 const FloatingPoint<FloatType> actual(value), expected(expected_);
1630 if (actual.is_nan() || expected.is_nan()) {
1631 if (actual.is_nan() && expected.is_nan()) {
1637 if (HasMaxAbsError()) {
1642 if (value == expected_) {
1646 const FloatType diff = value - expected_;
1647 if (::std::fabs(diff) <= max_abs_error_) {
1651 if (listener->IsInterested()) {
1652 *listener <<
"which is " << diff <<
" from " << expected_;
1656 return actual.AlmostEquals(expected);
1660 void DescribeTo(::std::ostream* os)
const override {
1664 const ::std::streamsize old_precision = os->precision(
1665 ::std::numeric_limits<FloatType>::digits10 + 2);
1666 if (FloatingPoint<FloatType>(expected_).is_nan()) {
1670 *os <<
"never matches";
1673 *os <<
"is approximately " << expected_;
1674 if (HasMaxAbsError()) {
1675 *os <<
" (absolute error <= " << max_abs_error_ <<
")";
1678 os->precision(old_precision);
1681 void DescribeNegationTo(::std::ostream* os)
const override {
1683 const ::std::streamsize old_precision = os->precision(
1684 ::std::numeric_limits<FloatType>::digits10 + 2);
1685 if (FloatingPoint<FloatType>(expected_).is_nan()) {
1689 *os <<
"is anything";
1692 *os <<
"isn't approximately " << expected_;
1693 if (HasMaxAbsError()) {
1694 *os <<
" (absolute error > " << max_abs_error_ <<
")";
1698 os->precision(old_precision);
1702 bool HasMaxAbsError()
const {
1703 return max_abs_error_ >= 0;
1706 const FloatType expected_;
1707 const bool nan_eq_nan_;
1709 const FloatType max_abs_error_;
1715 operator Matcher<FloatType>()
const {
1717 new Impl<FloatType>(expected_, nan_eq_nan_, max_abs_error_));
1720 operator Matcher<const FloatType&>()
const {
1722 new Impl<const FloatType&>(expected_, nan_eq_nan_, max_abs_error_));
1725 operator Matcher<FloatType&>()
const {
1727 new Impl<FloatType&>(expected_, nan_eq_nan_, max_abs_error_));
1731 const FloatType expected_;
1732 const bool nan_eq_nan_;
1734 const FloatType max_abs_error_;
1742 template <
typename FloatType>
1743 class FloatingEq2Matcher {
1745 FloatingEq2Matcher() { Init(-1,
false); }
1747 explicit FloatingEq2Matcher(
bool nan_eq_nan) { Init(-1, nan_eq_nan); }
1749 explicit FloatingEq2Matcher(FloatType max_abs_error) {
1750 Init(max_abs_error,
false);
1753 FloatingEq2Matcher(FloatType max_abs_error,
bool nan_eq_nan) {
1754 Init(max_abs_error, nan_eq_nan);
1757 template <
typename T1,
typename T2>
1758 operator Matcher<::std::tuple<T1, T2>>()
const {
1760 new Impl<::std::tuple<T1, T2>>(max_abs_error_, nan_eq_nan_));
1762 template <
typename T1,
typename T2>
1763 operator Matcher<const ::std::tuple<T1, T2>&>()
const {
1765 new Impl<const ::std::tuple<T1, T2>&>(max_abs_error_, nan_eq_nan_));
1769 static ::std::ostream& GetDesc(::std::ostream& os) {
1770 return os <<
"an almost-equal pair";
1773 template <
typename Tuple>
1774 class Impl :
public MatcherInterface<Tuple> {
1776 Impl(FloatType max_abs_error,
bool nan_eq_nan) :
1777 max_abs_error_(max_abs_error),
1778 nan_eq_nan_(nan_eq_nan) {}
1780 bool MatchAndExplain(Tuple args,
1781 MatchResultListener* listener)
const override {
1782 if (max_abs_error_ == -1) {
1783 FloatingEqMatcher<FloatType> fm(::std::get<0>(args), nan_eq_nan_);
1784 return static_cast<Matcher<FloatType>
>(fm).MatchAndExplain(
1785 ::std::get<1>(args), listener);
1787 FloatingEqMatcher<FloatType> fm(::std::get<0>(args), nan_eq_nan_,
1789 return static_cast<Matcher<FloatType>
>(fm).MatchAndExplain(
1790 ::std::get<1>(args), listener);
1793 void DescribeTo(::std::ostream* os)
const override {
1794 *os <<
"are " << GetDesc;
1796 void DescribeNegationTo(::std::ostream* os)
const override {
1797 *os <<
"aren't " << GetDesc;
1801 FloatType max_abs_error_;
1802 const bool nan_eq_nan_;
1805 void Init(FloatType max_abs_error_val,
bool nan_eq_nan_val) {
1806 max_abs_error_ = max_abs_error_val;
1807 nan_eq_nan_ = nan_eq_nan_val;
1809 FloatType max_abs_error_;
1815 template <
typename InnerMatcher>
1816 class PointeeMatcher {
1818 explicit PointeeMatcher(
const InnerMatcher& matcher) : matcher_(matcher) {}
1828 template <
typename Po
inter>
1829 operator Matcher<Pointer>()
const {
1830 return Matcher<Pointer>(
new Impl<const Pointer&>(matcher_));
1835 template <
typename Po
inter>
1836 class Impl :
public MatcherInterface<Pointer> {
1840 Pointer)>::element_type;
1842 explicit Impl(
const InnerMatcher& matcher)
1843 : matcher_(MatcherCast<const Pointee&>(matcher)) {}
1845 void DescribeTo(::std::ostream* os)
const override {
1846 *os <<
"points to a value that ";
1847 matcher_.DescribeTo(os);
1850 void DescribeNegationTo(::std::ostream* os)
const override {
1851 *os <<
"does not point to a value that ";
1852 matcher_.DescribeTo(os);
1855 bool MatchAndExplain(Pointer pointer,
1856 MatchResultListener* listener)
const override {
1859 *listener <<
"which points to ";
1860 return MatchPrintAndExplain(*pointer, matcher_, listener);
1864 const Matcher<const Pointee&> matcher_;
1867 const InnerMatcher matcher_;
1874 template <
typename InnerMatcher>
1875 class PointerMatcher {
1877 explicit PointerMatcher(
const InnerMatcher& matcher) : matcher_(matcher) {}
1887 template <
typename Po
interType>
1888 operator Matcher<PointerType>()
const {
1889 return Matcher<PointerType>(
new Impl<const PointerType&>(matcher_));
1894 template <
typename Po
interType>
1895 class Impl :
public MatcherInterface<PointerType> {
1899 PointerType)>::element_type*;
1901 explicit Impl(
const InnerMatcher& matcher)
1902 : matcher_(MatcherCast<Pointer>(matcher)) {}
1904 void DescribeTo(::std::ostream* os)
const override {
1905 *os <<
"is a pointer that ";
1906 matcher_.DescribeTo(os);
1909 void DescribeNegationTo(::std::ostream* os)
const override {
1910 *os <<
"is not a pointer that ";
1911 matcher_.DescribeTo(os);
1914 bool MatchAndExplain(PointerType pointer,
1915 MatchResultListener* listener)
const override {
1916 *listener <<
"which is a pointer that ";
1918 return MatchPrintAndExplain(p, matcher_, listener);
1922 Matcher<Pointer> matcher_;
1925 const InnerMatcher matcher_;
1935 template <
typename To>
1936 class WhenDynamicCastToMatcherBase {
1938 explicit WhenDynamicCastToMatcherBase(
const Matcher<To>& matcher)
1939 : matcher_(matcher) {}
1941 void DescribeTo(::std::ostream* os)
const {
1942 GetCastTypeDescription(os);
1943 matcher_.DescribeTo(os);
1946 void DescribeNegationTo(::std::ostream* os)
const {
1947 GetCastTypeDescription(os);
1948 matcher_.DescribeNegationTo(os);
1952 const Matcher<To> matcher_;
1954 static std::string GetToName() {
1955 return GetTypeName<To>();
1959 static void GetCastTypeDescription(::std::ostream* os) {
1960 *os <<
"when dynamic_cast to " << GetToName() <<
", ";
1966 template <
typename To>
1967 class WhenDynamicCastToMatcher :
public WhenDynamicCastToMatcherBase<To> {
1969 explicit WhenDynamicCastToMatcher(
const Matcher<To>& matcher)
1970 : WhenDynamicCastToMatcherBase<To>(matcher) {}
1972 template <
typename From>
1973 bool MatchAndExplain(From from, MatchResultListener* listener)
const {
1974 To to =
dynamic_cast<To
>(from);
1975 return MatchPrintAndExplain(to, this->matcher_, listener);
1981 template <
typename To>
1982 class WhenDynamicCastToMatcher<To&> :
public WhenDynamicCastToMatcherBase<To&> {
1984 explicit WhenDynamicCastToMatcher(
const Matcher<To&>& matcher)
1985 : WhenDynamicCastToMatcherBase<To&>(matcher) {}
1987 template <
typename From>
1988 bool MatchAndExplain(From& from, MatchResultListener* listener)
const {
1990 To* to =
dynamic_cast<To*
>(&from);
1991 if (to ==
nullptr) {
1992 *listener <<
"which cannot be dynamic_cast to " << this->GetToName();
1995 return MatchPrintAndExplain(*to, this->matcher_, listener);
2002 template <
typename Class,
typename FieldType>
2003 class FieldMatcher {
2005 FieldMatcher(FieldType Class::*field,
2006 const Matcher<const FieldType&>& matcher)
2007 : field_(field), matcher_(matcher), whose_field_(
"whose given field ") {}
2009 FieldMatcher(
const std::string& field_name, FieldType Class::*field,
2010 const Matcher<const FieldType&>& matcher)
2013 whose_field_(
"whose field `" + field_name +
"` ") {}
2015 void DescribeTo(::std::ostream* os)
const {
2016 *os <<
"is an object " << whose_field_;
2017 matcher_.DescribeTo(os);
2020 void DescribeNegationTo(::std::ostream* os)
const {
2021 *os <<
"is an object " << whose_field_;
2022 matcher_.DescribeNegationTo(os);
2025 template <
typename T>
2026 bool MatchAndExplain(
const T& value, MatchResultListener* listener)
const {
2029 return MatchAndExplainImpl(
2030 typename std::is_pointer<
typename std::remove_const<T>::type>::type(),
2035 bool MatchAndExplainImpl(std::false_type ,
2037 MatchResultListener* listener)
const {
2038 *listener << whose_field_ <<
"is ";
2039 return MatchPrintAndExplain(obj.*field_, matcher_, listener);
2042 bool MatchAndExplainImpl(std::true_type ,
const Class* p,
2043 MatchResultListener* listener)
const {
2044 if (p ==
nullptr)
return false;
2046 *listener <<
"which points to an object ";
2050 return MatchAndExplainImpl(std::false_type(), *p, listener);
2053 const FieldType Class::*field_;
2054 const Matcher<const FieldType&> matcher_;
2058 const std::string whose_field_;
2066 template <
typename Class,
typename PropertyType,
typename Property>
2067 class PropertyMatcher {
2069 typedef const PropertyType& RefToConstProperty;
2071 PropertyMatcher(Property property,
const Matcher<RefToConstProperty>& matcher)
2072 : property_(property),
2074 whose_property_(
"whose given property ") {}
2076 PropertyMatcher(
const std::string& property_name, Property property,
2077 const Matcher<RefToConstProperty>& matcher)
2078 : property_(property),
2080 whose_property_(
"whose property `" + property_name +
"` ") {}
2082 void DescribeTo(::std::ostream* os)
const {
2083 *os <<
"is an object " << whose_property_;
2084 matcher_.DescribeTo(os);
2087 void DescribeNegationTo(::std::ostream* os)
const {
2088 *os <<
"is an object " << whose_property_;
2089 matcher_.DescribeNegationTo(os);
2092 template <
typename T>
2093 bool MatchAndExplain(
const T&value, MatchResultListener* listener)
const {
2094 return MatchAndExplainImpl(
2095 typename std::is_pointer<
typename std::remove_const<T>::type>::type(),
2100 bool MatchAndExplainImpl(std::false_type ,
2102 MatchResultListener* listener)
const {
2103 *listener << whose_property_ <<
"is ";
2106 RefToConstProperty result = (obj.*property_)();
2107 return MatchPrintAndExplain(result, matcher_, listener);
2110 bool MatchAndExplainImpl(std::true_type ,
const Class* p,
2111 MatchResultListener* listener)
const {
2112 if (p ==
nullptr)
return false;
2114 *listener <<
"which points to an object ";
2118 return MatchAndExplainImpl(std::false_type(), *p, listener);
2122 const Matcher<RefToConstProperty> matcher_;
2126 const std::string whose_property_;
2131 template <
typename Functor>
2132 struct CallableTraits {
2133 typedef Functor StorageType;
2135 static void CheckIsValid(Functor ) {}
2137 template <
typename T>
2138 static auto Invoke(Functor f,
const T& arg) -> decltype(f(arg)) {
2144 template <
typename ArgType,
typename ResType>
2145 struct CallableTraits<ResType(*)(ArgType)> {
2146 typedef ResType ResultType;
2147 typedef ResType(*StorageType)(ArgType);
2149 static void CheckIsValid(ResType(*f)(ArgType)) {
2151 <<
"NULL function pointer is passed into ResultOf().";
2153 template <
typename T>
2154 static ResType
Invoke(ResType(*f)(ArgType), T arg) {
2161 template <
typename Callable,
typename InnerMatcher>
2162 class ResultOfMatcher {
2164 ResultOfMatcher(Callable callable, InnerMatcher matcher)
2165 : callable_(std::move(callable)), matcher_(std::move(matcher)) {
2166 CallableTraits<Callable>::CheckIsValid(callable_);
2169 template <
typename T>
2170 operator Matcher<T>()
const {
2171 return Matcher<T>(
new Impl<const T&>(callable_, matcher_));
2175 typedef typename CallableTraits<Callable>::StorageType CallableStorageType;
2177 template <
typename T>
2178 class Impl :
public MatcherInterface<T> {
2179 using ResultType = decltype(CallableTraits<Callable>::template Invoke<T>(
2180 std::declval<CallableStorageType>(), std::declval<T>()));
2183 template <
typename M>
2184 Impl(
const CallableStorageType& callable,
const M& matcher)
2185 : callable_(callable), matcher_(MatcherCast<ResultType>(matcher)) {}
2187 void DescribeTo(::std::ostream* os)
const override {
2188 *os <<
"is mapped by the given callable to a value that ";
2189 matcher_.DescribeTo(os);
2192 void DescribeNegationTo(::std::ostream* os)
const override {
2193 *os <<
"is mapped by the given callable to a value that ";
2194 matcher_.DescribeNegationTo(os);
2197 bool MatchAndExplain(T obj, MatchResultListener* listener)
const override {
2198 *listener <<
"which is mapped by the given callable to ";
2204 CallableTraits<Callable>::template Invoke<T>(callable_, obj);
2205 return MatchPrintAndExplain(result, matcher_, listener);
2214 mutable CallableStorageType callable_;
2215 const Matcher<ResultType> matcher_;
2218 const CallableStorageType callable_;
2219 const InnerMatcher matcher_;
2223 template <
typename SizeMatcher>
2224 class SizeIsMatcher {
2226 explicit SizeIsMatcher(
const SizeMatcher& size_matcher)
2227 : size_matcher_(size_matcher) {
2230 template <
typename Container>
2231 operator Matcher<Container>()
const {
2232 return Matcher<Container>(
new Impl<const Container&>(size_matcher_));
2235 template <
typename Container>
2236 class Impl :
public MatcherInterface<Container> {
2238 using SizeType = decltype(std::declval<Container>().size());
2239 explicit Impl(
const SizeMatcher& size_matcher)
2240 : size_matcher_(MatcherCast<SizeType>(size_matcher)) {}
2242 void DescribeTo(::std::ostream* os)
const override {
2244 size_matcher_.DescribeTo(os);
2246 void DescribeNegationTo(::std::ostream* os)
const override {
2248 size_matcher_.DescribeNegationTo(os);
2251 bool MatchAndExplain(Container container,
2252 MatchResultListener* listener)
const override {
2253 SizeType size = container.size();
2254 StringMatchResultListener size_listener;
2255 const bool result = size_matcher_.MatchAndExplain(size, &size_listener);
2257 <<
"whose size " << size << (result ?
" matches" :
" doesn't match");
2258 PrintIfNotEmpty(size_listener.str(), listener->stream());
2263 const Matcher<SizeType> size_matcher_;
2267 const SizeMatcher size_matcher_;
2272 template <
typename DistanceMatcher>
2273 class BeginEndDistanceIsMatcher {
2275 explicit BeginEndDistanceIsMatcher(
const DistanceMatcher& distance_matcher)
2276 : distance_matcher_(distance_matcher) {}
2278 template <
typename Container>
2279 operator Matcher<Container>()
const {
2280 return Matcher<Container>(
new Impl<const Container&>(distance_matcher_));
2283 template <
typename Container>
2284 class Impl :
public MatcherInterface<Container> {
2286 typedef internal::StlContainerView<
2288 typedef typename std::iterator_traits<
2289 typename ContainerView::type::const_iterator>::difference_type
2291 explicit Impl(
const DistanceMatcher& distance_matcher)
2292 : distance_matcher_(MatcherCast<DistanceType>(distance_matcher)) {}
2294 void DescribeTo(::std::ostream* os)
const override {
2295 *os <<
"distance between begin() and end() ";
2296 distance_matcher_.DescribeTo(os);
2298 void DescribeNegationTo(::std::ostream* os)
const override {
2299 *os <<
"distance between begin() and end() ";
2300 distance_matcher_.DescribeNegationTo(os);
2303 bool MatchAndExplain(Container container,
2304 MatchResultListener* listener)
const override {
2307 DistanceType distance = std::distance(begin(container), end(container));
2308 StringMatchResultListener distance_listener;
2310 distance_matcher_.MatchAndExplain(distance, &distance_listener);
2311 *listener <<
"whose distance between begin() and end() " << distance
2312 << (result ?
" matches" :
" doesn't match");
2313 PrintIfNotEmpty(distance_listener.str(), listener->stream());
2318 const Matcher<DistanceType> distance_matcher_;
2322 const DistanceMatcher distance_matcher_;
2335 template <
typename Container>
2336 class ContainerEqMatcher {
2338 typedef internal::StlContainerView<Container> View;
2339 typedef typename View::type StlContainer;
2340 typedef typename View::const_reference StlContainerReference;
2342 static_assert(!std::is_const<Container>::value,
2343 "Container type must not be const");
2344 static_assert(!std::is_reference<Container>::value,
2345 "Container type must not be a reference");
2349 explicit ContainerEqMatcher(
const Container& expected)
2350 : expected_(View::Copy(expected)) {}
2352 void DescribeTo(::std::ostream* os)
const {
2356 void DescribeNegationTo(::std::ostream* os)
const {
2357 *os <<
"does not equal ";
2361 template <
typename LhsContainer>
2362 bool MatchAndExplain(
const LhsContainer& lhs,
2363 MatchResultListener* listener)
const {
2364 typedef internal::StlContainerView<
2365 typename std::remove_const<LhsContainer>::type>
2367 typedef typename LhsView::type LhsStlContainer;
2368 StlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2369 if (lhs_stl_container == expected_)
2372 ::std::ostream*
const os = listener->stream();
2373 if (os !=
nullptr) {
2375 bool printed_header =
false;
2376 for (
typename LhsStlContainer::const_iterator it =
2377 lhs_stl_container.begin();
2378 it != lhs_stl_container.end(); ++it) {
2381 if (printed_header) {
2384 *os <<
"which has these unexpected elements: ";
2385 printed_header =
true;
2392 bool printed_header2 =
false;
2393 for (
typename StlContainer::const_iterator it = expected_.begin();
2394 it != expected_.end(); ++it) {
2396 lhs_stl_container.begin(), lhs_stl_container.end(), *it) ==
2397 lhs_stl_container.end()) {
2398 if (printed_header2) {
2401 *os << (printed_header ?
",\nand" :
"which")
2402 <<
" doesn't have these expected elements: ";
2403 printed_header2 =
true;
2414 const StlContainer expected_;
2418 struct LessComparator {
2419 template <
typename T,
typename U>
2420 bool operator()(
const T& lhs,
const U& rhs)
const {
return lhs < rhs; }
2424 template <
typename Comparator,
typename ContainerMatcher>
2425 class WhenSortedByMatcher {
2427 WhenSortedByMatcher(
const Comparator& comparator,
2428 const ContainerMatcher& matcher)
2429 : comparator_(comparator), matcher_(matcher) {}
2431 template <
typename LhsContainer>
2432 operator Matcher<LhsContainer>()
const {
2433 return MakeMatcher(
new Impl<LhsContainer>(comparator_, matcher_));
2436 template <
typename LhsContainer>
2437 class Impl :
public MatcherInterface<LhsContainer> {
2439 typedef internal::StlContainerView<
2441 typedef typename LhsView::type LhsStlContainer;
2442 typedef typename LhsView::const_reference LhsStlContainerReference;
2445 typedef typename RemoveConstFromKey<
2446 typename LhsStlContainer::value_type>::type LhsValue;
2448 Impl(
const Comparator& comparator,
const ContainerMatcher& matcher)
2449 : comparator_(comparator), matcher_(matcher) {}
2451 void DescribeTo(::std::ostream* os)
const override {
2452 *os <<
"(when sorted) ";
2453 matcher_.DescribeTo(os);
2456 void DescribeNegationTo(::std::ostream* os)
const override {
2457 *os <<
"(when sorted) ";
2458 matcher_.DescribeNegationTo(os);
2461 bool MatchAndExplain(LhsContainer lhs,
2462 MatchResultListener* listener)
const override {
2463 LhsStlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2464 ::std::vector<LhsValue> sorted_container(lhs_stl_container.begin(),
2465 lhs_stl_container.end());
2467 sorted_container.begin(), sorted_container.end(), comparator_);
2469 if (!listener->IsInterested()) {
2472 return matcher_.Matches(sorted_container);
2475 *listener <<
"which is ";
2477 *listener <<
" when sorted";
2479 StringMatchResultListener inner_listener;
2480 const bool match = matcher_.MatchAndExplain(sorted_container,
2482 PrintIfNotEmpty(inner_listener.str(), listener->stream());
2487 const Comparator comparator_;
2488 const Matcher<const ::std::vector<LhsValue>&> matcher_;
2494 const Comparator comparator_;
2495 const ContainerMatcher matcher_;
2502 template <
typename TupleMatcher,
typename RhsContainer>
2503 class PointwiseMatcher {
2506 use_UnorderedPointwise_with_hash_tables);
2509 typedef internal::StlContainerView<RhsContainer> RhsView;
2510 typedef typename RhsView::type RhsStlContainer;
2511 typedef typename RhsStlContainer::value_type RhsValue;
2513 static_assert(!std::is_const<RhsContainer>::value,
2514 "RhsContainer type must not be const");
2515 static_assert(!std::is_reference<RhsContainer>::value,
2516 "RhsContainer type must not be a reference");
2520 PointwiseMatcher(
const TupleMatcher& tuple_matcher,
const RhsContainer& rhs)
2521 : tuple_matcher_(tuple_matcher), rhs_(RhsView::Copy(rhs)) {}
2523 template <
typename LhsContainer>
2524 operator Matcher<LhsContainer>()
const {
2527 use_UnorderedPointwise_with_hash_tables);
2529 return Matcher<LhsContainer>(
2530 new Impl<const LhsContainer&>(tuple_matcher_, rhs_));
2533 template <
typename LhsContainer>
2534 class Impl :
public MatcherInterface<LhsContainer> {
2536 typedef internal::StlContainerView<
2538 typedef typename LhsView::type LhsStlContainer;
2539 typedef typename LhsView::const_reference LhsStlContainerReference;
2540 typedef typename LhsStlContainer::value_type LhsValue;
2545 typedef ::std::tuple<const LhsValue&, const RhsValue&> InnerMatcherArg;
2547 Impl(
const TupleMatcher& tuple_matcher,
const RhsStlContainer& rhs)
2549 : mono_tuple_matcher_(SafeMatcherCast<InnerMatcherArg>(tuple_matcher)),
2552 void DescribeTo(::std::ostream* os)
const override {
2553 *os <<
"contains " << rhs_.size()
2554 <<
" values, where each value and its corresponding value in ";
2557 mono_tuple_matcher_.DescribeTo(os);
2559 void DescribeNegationTo(::std::ostream* os)
const override {
2560 *os <<
"doesn't contain exactly " << rhs_.size()
2561 <<
" values, or contains a value x at some index i"
2562 <<
" where x and the i-th value of ";
2565 mono_tuple_matcher_.DescribeNegationTo(os);
2568 bool MatchAndExplain(LhsContainer lhs,
2569 MatchResultListener* listener)
const override {
2570 LhsStlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2571 const size_t actual_size = lhs_stl_container.size();
2572 if (actual_size != rhs_.size()) {
2573 *listener <<
"which contains " << actual_size <<
" values";
2577 typename LhsStlContainer::const_iterator left = lhs_stl_container.begin();
2578 typename RhsStlContainer::const_iterator right = rhs_.begin();
2579 for (
size_t i = 0; i != actual_size; ++i, ++left, ++right) {
2580 if (listener->IsInterested()) {
2581 StringMatchResultListener inner_listener;
2585 if (!mono_tuple_matcher_.MatchAndExplain(
2586 InnerMatcherArg(ImplicitCast_<const LhsValue&>(*left),
2587 ImplicitCast_<const RhsValue&>(*right)),
2589 *listener <<
"where the value pair (";
2593 *listener <<
") at index #" << i <<
" don't match";
2594 PrintIfNotEmpty(inner_listener.str(), listener->stream());
2598 if (!mono_tuple_matcher_.Matches(
2599 InnerMatcherArg(ImplicitCast_<const LhsValue&>(*left),
2600 ImplicitCast_<const RhsValue&>(*right))))
2609 const Matcher<InnerMatcherArg> mono_tuple_matcher_;
2610 const RhsStlContainer rhs_;
2614 const TupleMatcher tuple_matcher_;
2615 const RhsStlContainer rhs_;
2619 template <
typename Container>
2620 class QuantifierMatcherImpl :
public MatcherInterface<Container> {
2623 typedef StlContainerView<RawContainer> View;
2624 typedef typename View::type StlContainer;
2625 typedef typename View::const_reference StlContainerReference;
2626 typedef typename StlContainer::value_type Element;
2628 template <
typename InnerMatcher>
2629 explicit QuantifierMatcherImpl(InnerMatcher inner_matcher)
2631 testing::SafeMatcherCast<const Element&>(inner_matcher)) {}
2636 bool MatchAndExplainImpl(
bool all_elements_should_match,
2637 Container container,
2638 MatchResultListener* listener)
const {
2639 StlContainerReference stl_container = View::ConstReference(container);
2641 for (
typename StlContainer::const_iterator it = stl_container.begin();
2642 it != stl_container.end(); ++it, ++i) {
2643 StringMatchResultListener inner_listener;
2644 const bool matches = inner_matcher_.MatchAndExplain(*it, &inner_listener);
2646 if (matches != all_elements_should_match) {
2647 *listener <<
"whose element #" << i
2648 << (matches ?
" matches" :
" doesn't match");
2649 PrintIfNotEmpty(inner_listener.str(), listener->stream());
2650 return !all_elements_should_match;
2653 return all_elements_should_match;
2657 const Matcher<const Element&> inner_matcher_;
2662 template <
typename Container>
2663 class ContainsMatcherImpl :
public QuantifierMatcherImpl<Container> {
2665 template <
typename InnerMatcher>
2666 explicit ContainsMatcherImpl(InnerMatcher inner_matcher)
2667 : QuantifierMatcherImpl<Container>(inner_matcher) {}
2670 void DescribeTo(::std::ostream* os)
const override {
2671 *os <<
"contains at least one element that ";
2672 this->inner_matcher_.DescribeTo(os);
2675 void DescribeNegationTo(::std::ostream* os)
const override {
2676 *os <<
"doesn't contain any element that ";
2677 this->inner_matcher_.DescribeTo(os);
2680 bool MatchAndExplain(Container container,
2681 MatchResultListener* listener)
const override {
2682 return this->MatchAndExplainImpl(
false, container, listener);
2688 template <
typename Container>
2689 class EachMatcherImpl :
public QuantifierMatcherImpl<Container> {
2691 template <
typename InnerMatcher>
2692 explicit EachMatcherImpl(InnerMatcher inner_matcher)
2693 : QuantifierMatcherImpl<Container>(inner_matcher) {}
2696 void DescribeTo(::std::ostream* os)
const override {
2697 *os <<
"only contains elements that ";
2698 this->inner_matcher_.DescribeTo(os);
2701 void DescribeNegationTo(::std::ostream* os)
const override {
2702 *os <<
"contains some element that ";
2703 this->inner_matcher_.DescribeNegationTo(os);
2706 bool MatchAndExplain(Container container,
2707 MatchResultListener* listener)
const override {
2708 return this->MatchAndExplainImpl(
true, container, listener);
2713 template <
typename M>
2714 class ContainsMatcher {
2716 explicit ContainsMatcher(M m) : inner_matcher_(m) {}
2718 template <
typename Container>
2719 operator Matcher<Container>()
const {
2720 return Matcher<Container>(
2721 new ContainsMatcherImpl<const Container&>(inner_matcher_));
2725 const M inner_matcher_;
2729 template <
typename M>
2732 explicit EachMatcher(M m) : inner_matcher_(m) {}
2734 template <
typename Container>
2735 operator Matcher<Container>()
const {
2736 return Matcher<Container>(
2737 new EachMatcherImpl<const Container&>(inner_matcher_));
2741 const M inner_matcher_;
2745 struct Rank0 : Rank1 {};
2747 namespace pair_getters {
2749 template <
typename T>
2750 auto First(T& x, Rank1) -> decltype(get<0>(x)) {
2753 template <
typename T>
2754 auto First(T& x, Rank0) -> decltype((x.first)) {
2758 template <
typename T>
2759 auto Second(T& x, Rank1) -> decltype(get<1>(x)) {
2762 template <
typename T>
2763 auto Second(T& x, Rank0) -> decltype((x.second)) {
2772 template <
typename PairType>
2773 class KeyMatcherImpl :
public MatcherInterface<PairType> {
2776 typedef typename RawPairType::first_type KeyType;
2778 template <
typename InnerMatcher>
2779 explicit KeyMatcherImpl(InnerMatcher inner_matcher)
2781 testing::SafeMatcherCast<const KeyType&>(inner_matcher)) {
2786 bool MatchAndExplain(PairType key_value,
2787 MatchResultListener* listener)
const override {
2788 StringMatchResultListener inner_listener;
2789 const bool match = inner_matcher_.MatchAndExplain(
2790 pair_getters::First(key_value, Rank0()), &inner_listener);
2791 const std::string explanation = inner_listener.str();
2792 if (explanation !=
"") {
2793 *listener <<
"whose first field is a value " << explanation;
2799 void DescribeTo(::std::ostream* os)
const override {
2800 *os <<
"has a key that ";
2801 inner_matcher_.DescribeTo(os);
2805 void DescribeNegationTo(::std::ostream* os)
const override {
2806 *os <<
"doesn't have a key that ";
2807 inner_matcher_.DescribeTo(os);
2811 const Matcher<const KeyType&> inner_matcher_;
2815 template <
typename M>
2818 explicit KeyMatcher(M m) : matcher_for_key_(m) {}
2820 template <
typename PairType>
2821 operator Matcher<PairType>()
const {
2822 return Matcher<PairType>(
2823 new KeyMatcherImpl<const PairType&>(matcher_for_key_));
2827 const M matcher_for_key_;
2831 template <
typename InnerMatcher>
2832 class AddressMatcher {
2834 explicit AddressMatcher(InnerMatcher m) : matcher_(m) {}
2836 template <
typename Type>
2837 operator Matcher<Type>()
const {
2838 return Matcher<Type>(
new Impl<const Type&>(matcher_));
2843 template <
typename Type>
2844 class Impl :
public MatcherInterface<Type> {
2847 explicit Impl(
const InnerMatcher& matcher)
2848 : matcher_(MatcherCast<Address>(matcher)) {}
2850 void DescribeTo(::std::ostream* os)
const override {
2851 *os <<
"has address that ";
2852 matcher_.DescribeTo(os);
2855 void DescribeNegationTo(::std::ostream* os)
const override {
2856 *os <<
"does not have address that ";
2857 matcher_.DescribeTo(os);
2860 bool MatchAndExplain(Type
object,
2861 MatchResultListener* listener)
const override {
2862 *listener <<
"which has address ";
2863 Address address = std::addressof(
object);
2864 return MatchPrintAndExplain(address, matcher_, listener);
2868 const Matcher<Address> matcher_;
2870 const InnerMatcher matcher_;
2875 template <
typename PairType>
2876 class PairMatcherImpl :
public MatcherInterface<PairType> {
2879 typedef typename RawPairType::first_type FirstType;
2880 typedef typename RawPairType::second_type SecondType;
2882 template <
typename FirstMatcher,
typename SecondMatcher>
2883 PairMatcherImpl(FirstMatcher first_matcher, SecondMatcher second_matcher)
2885 testing::SafeMatcherCast<const FirstType&>(first_matcher)),
2887 testing::SafeMatcherCast<const SecondType&>(second_matcher)) {
2891 void DescribeTo(::std::ostream* os)
const override {
2892 *os <<
"has a first field that ";
2893 first_matcher_.DescribeTo(os);
2894 *os <<
", and has a second field that ";
2895 second_matcher_.DescribeTo(os);
2899 void DescribeNegationTo(::std::ostream* os)
const override {
2900 *os <<
"has a first field that ";
2901 first_matcher_.DescribeNegationTo(os);
2902 *os <<
", or has a second field that ";
2903 second_matcher_.DescribeNegationTo(os);
2908 bool MatchAndExplain(PairType a_pair,
2909 MatchResultListener* listener)
const override {
2910 if (!listener->IsInterested()) {
2913 return first_matcher_.Matches(pair_getters::First(a_pair, Rank0())) &&
2914 second_matcher_.Matches(pair_getters::Second(a_pair, Rank0()));
2916 StringMatchResultListener first_inner_listener;
2917 if (!first_matcher_.MatchAndExplain(pair_getters::First(a_pair, Rank0()),
2918 &first_inner_listener)) {
2919 *listener <<
"whose first field does not match";
2920 PrintIfNotEmpty(first_inner_listener.str(), listener->stream());
2923 StringMatchResultListener second_inner_listener;
2924 if (!second_matcher_.MatchAndExplain(pair_getters::Second(a_pair, Rank0()),
2925 &second_inner_listener)) {
2926 *listener <<
"whose second field does not match";
2927 PrintIfNotEmpty(second_inner_listener.str(), listener->stream());
2930 ExplainSuccess(first_inner_listener.str(), second_inner_listener.str(),
2936 void ExplainSuccess(
const std::string& first_explanation,
2937 const std::string& second_explanation,
2938 MatchResultListener* listener)
const {
2939 *listener <<
"whose both fields match";
2940 if (first_explanation !=
"") {
2941 *listener <<
", where the first field is a value " << first_explanation;
2943 if (second_explanation !=
"") {
2945 if (first_explanation !=
"") {
2946 *listener <<
"and ";
2948 *listener <<
"where ";
2950 *listener <<
"the second field is a value " << second_explanation;
2954 const Matcher<const FirstType&> first_matcher_;
2955 const Matcher<const SecondType&> second_matcher_;
2959 template <
typename FirstMatcher,
typename SecondMatcher>
2962 PairMatcher(FirstMatcher first_matcher, SecondMatcher second_matcher)
2963 : first_matcher_(first_matcher), second_matcher_(second_matcher) {}
2965 template <
typename PairType>
2966 operator Matcher<PairType> ()
const {
2967 return Matcher<PairType>(
2968 new PairMatcherImpl<const PairType&>(first_matcher_, second_matcher_));
2972 const FirstMatcher first_matcher_;
2973 const SecondMatcher second_matcher_;
2976 template <
typename T,
size_t... I>
2977 auto UnpackStructImpl(
const T& t, IndexSequence<I...>,
int)
2978 -> decltype(std::tie(get<I>(t)...)) {
2979 static_assert(std::tuple_size<T>::value ==
sizeof...(I),
2980 "Number of arguments doesn't match the number of fields.");
2981 return std::tie(get<I>(t)...);
2984 #if defined(__cpp_structured_bindings) && __cpp_structured_bindings >= 201606
2985 template <
typename T>
2986 auto UnpackStructImpl(
const T& t, MakeIndexSequence<1>,
char) {
2987 const auto& [a] = t;
2990 template <
typename T>
2991 auto UnpackStructImpl(
const T& t, MakeIndexSequence<2>,
char) {
2992 const auto& [a, b] = t;
2993 return std::tie(a, b);
2995 template <
typename T>
2996 auto UnpackStructImpl(
const T& t, MakeIndexSequence<3>,
char) {
2997 const auto& [a, b, c] = t;
2998 return std::tie(a, b, c);
3000 template <
typename T>
3001 auto UnpackStructImpl(
const T& t, MakeIndexSequence<4>,
char) {
3002 const auto& [a, b, c, d] = t;
3003 return std::tie(a, b, c, d);
3005 template <
typename T>
3006 auto UnpackStructImpl(
const T& t, MakeIndexSequence<5>,
char) {
3007 const auto& [a, b, c, d, e] = t;
3008 return std::tie(a, b, c, d, e);
3010 template <
typename T>
3011 auto UnpackStructImpl(
const T& t, MakeIndexSequence<6>,
char) {
3012 const auto& [a, b, c, d, e, f] = t;
3013 return std::tie(a, b, c, d, e, f);
3015 template <
typename T>
3016 auto UnpackStructImpl(
const T& t, MakeIndexSequence<7>,
char) {
3017 const auto& [a, b, c, d, e, f, g] = t;
3018 return std::tie(a, b, c, d, e, f, g);
3020 template <
typename T>
3021 auto UnpackStructImpl(
const T& t, MakeIndexSequence<8>,
char) {
3022 const auto& [a, b, c, d, e, f, g, h] = t;
3023 return std::tie(a, b, c, d, e, f, g, h);
3025 template <
typename T>
3026 auto UnpackStructImpl(
const T& t, MakeIndexSequence<9>,
char) {
3027 const auto& [a, b, c, d, e, f, g, h, i] = t;
3028 return std::tie(a, b, c, d, e, f, g, h, i);
3030 template <
typename T>
3031 auto UnpackStructImpl(
const T& t, MakeIndexSequence<10>,
char) {
3032 const auto& [a, b, c, d, e, f, g, h, i, j] = t;
3033 return std::tie(a, b, c, d, e, f, g, h, i, j);
3035 template <
typename T>
3036 auto UnpackStructImpl(
const T& t, MakeIndexSequence<11>,
char) {
3037 const auto& [a, b, c, d, e, f, g, h, i, j, k] = t;
3038 return std::tie(a, b, c, d, e, f, g, h, i, j, k);
3040 template <
typename T>
3041 auto UnpackStructImpl(
const T& t, MakeIndexSequence<12>,
char) {
3042 const auto& [a, b, c, d, e, f, g, h, i, j, k, l] = t;
3043 return std::tie(a, b, c, d, e, f, g, h, i, j, k, l);
3045 template <
typename T>
3046 auto UnpackStructImpl(
const T& t, MakeIndexSequence<13>,
char) {
3047 const auto& [a, b, c, d, e, f, g, h, i, j, k, l, m] = t;
3048 return std::tie(a, b, c, d, e, f, g, h, i, j, k, l, m);
3050 template <
typename T>
3051 auto UnpackStructImpl(
const T& t, MakeIndexSequence<14>,
char) {
3052 const auto& [a, b, c, d, e, f, g, h, i, j, k, l, m, n] = t;
3053 return std::tie(a, b, c, d, e, f, g, h, i, j, k, l, m, n);
3055 template <
typename T>
3056 auto UnpackStructImpl(
const T& t, MakeIndexSequence<15>,
char) {
3057 const auto& [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o] = t;
3058 return std::tie(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o);
3060 template <
typename T>
3061 auto UnpackStructImpl(
const T& t, MakeIndexSequence<16>,
char) {
3062 const auto& [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p] = t;
3063 return std::tie(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p);
3067 template <
size_t I,
typename T>
3068 auto UnpackStruct(
const T& t)
3069 -> decltype((UnpackStructImpl)(t, MakeIndexSequence<I>{}, 0)) {
3070 return (UnpackStructImpl)(t, MakeIndexSequence<I>{}, 0);
3076 template <
typename T,
size_t N>
3077 void VariadicExpand(
const T (&)[N]) {}
3079 template <
typename Struct,
typename StructSize>
3080 class FieldsAreMatcherImpl;
3082 template <
typename Struct,
size_t... I>
3083 class FieldsAreMatcherImpl<Struct, IndexSequence<I...>>
3084 :
public MatcherInterface<Struct> {
3085 using UnpackedType =
3086 decltype(UnpackStruct<
sizeof...(I)>(std::declval<const Struct&>()));
3087 using MatchersType = std::tuple<
3088 Matcher<const typename std::tuple_element<I, UnpackedType>::type&>...>;
3091 template <
typename Inner>
3092 explicit FieldsAreMatcherImpl(
const Inner& matchers)
3093 : matchers_(testing::SafeMatcherCast<
3094 const typename std::tuple_element<I, UnpackedType>::type&>(
3095 std::get<I>(matchers))...) {}
3097 void DescribeTo(::std::ostream* os)
const override {
3098 const char* separator =
"";
3100 {(*os << separator <<
"has field #" << I <<
" that ",
3101 std::get<I>(matchers_).DescribeTo(os), separator =
", and ")...});
3104 void DescribeNegationTo(::std::ostream* os)
const override {
3105 const char* separator =
"";
3106 VariadicExpand({(*os << separator <<
"has field #" << I <<
" that ",
3107 std::get<I>(matchers_).DescribeNegationTo(os),
3108 separator =
", or ")...});
3111 bool MatchAndExplain(Struct t, MatchResultListener* listener)
const override {
3112 return MatchInternal((UnpackStruct<
sizeof...(I)>)(t), listener);
3116 bool MatchInternal(UnpackedType tuple, MatchResultListener* listener)
const {
3117 if (!listener->IsInterested()) {
3121 VariadicExpand({good = good && std::get<I>(matchers_).Matches(
3122 std::get<I>(tuple))...});
3126 size_t failed_pos = ~size_t{};
3128 std::vector<StringMatchResultListener> inner_listener(
sizeof...(I));
3131 {failed_pos == ~size_t{} && !std::get<I>(matchers_).MatchAndExplain(
3132 std::get<I>(tuple), &inner_listener[I])
3135 if (failed_pos != ~
size_t{}) {
3136 *listener <<
"whose field #" << failed_pos <<
" does not match";
3137 PrintIfNotEmpty(inner_listener[failed_pos].str(), listener->stream());
3141 *listener <<
"whose all elements match";
3142 const char* separator =
", where";
3143 for (
size_t index = 0; index <
sizeof...(I); ++index) {
3144 const std::string str = inner_listener[index].str();
3146 *listener << separator <<
" field #" << index <<
" is a value " << str;
3147 separator =
", and";
3154 MatchersType matchers_;
3157 template <
typename... Inner>
3158 class FieldsAreMatcher {
3160 explicit FieldsAreMatcher(Inner... inner) : matchers_(std::move(inner)...) {}
3162 template <
typename Struct>
3163 operator Matcher<Struct>()
const {
3164 return Matcher<Struct>(
3165 new FieldsAreMatcherImpl<
const Struct&, IndexSequenceFor<Inner...>>(
3170 std::tuple<Inner...> matchers_;
3174 template <
typename Container>
3175 class ElementsAreMatcherImpl :
public MatcherInterface<Container> {
3178 typedef internal::StlContainerView<RawContainer> View;
3179 typedef typename View::type StlContainer;
3180 typedef typename View::const_reference StlContainerReference;
3181 typedef typename StlContainer::value_type Element;
3185 template <
typename InputIter>
3186 ElementsAreMatcherImpl(InputIter first, InputIter last) {
3187 while (first != last) {
3188 matchers_.push_back(MatcherCast<const Element&>(*first++));
3193 void DescribeTo(::std::ostream* os)
const override {
3196 }
else if (count() == 1) {
3197 *os <<
"has 1 element that ";
3198 matchers_[0].DescribeTo(os);
3200 *os <<
"has " << Elements(count()) <<
" where\n";
3201 for (
size_t i = 0; i != count(); ++i) {
3202 *os <<
"element #" << i <<
" ";
3203 matchers_[i].DescribeTo(os);
3204 if (i + 1 < count()) {
3212 void DescribeNegationTo(::std::ostream* os)
const override {
3214 *os <<
"isn't empty";
3218 *os <<
"doesn't have " << Elements(count()) <<
", or\n";
3219 for (
size_t i = 0; i != count(); ++i) {
3220 *os <<
"element #" << i <<
" ";
3221 matchers_[i].DescribeNegationTo(os);
3222 if (i + 1 < count()) {
3228 bool MatchAndExplain(Container container,
3229 MatchResultListener* listener)
const override {
3233 const bool listener_interested = listener->IsInterested();
3236 ::std::vector<std::string> explanations(count());
3237 StlContainerReference stl_container = View::ConstReference(container);
3238 typename StlContainer::const_iterator it = stl_container.begin();
3239 size_t exam_pos = 0;
3240 bool mismatch_found =
false;
3245 for (; it != stl_container.end() && exam_pos != count(); ++it, ++exam_pos) {
3247 if (listener_interested) {
3248 StringMatchResultListener s;
3249 match = matchers_[exam_pos].MatchAndExplain(*it, &s);
3250 explanations[exam_pos] = s.str();
3252 match = matchers_[exam_pos].Matches(*it);
3256 mismatch_found =
true;
3265 size_t actual_count = exam_pos;
3266 for (; it != stl_container.end(); ++it) {
3270 if (actual_count != count()) {
3275 if (listener_interested && (actual_count != 0)) {
3276 *listener <<
"which has " << Elements(actual_count);
3281 if (mismatch_found) {
3283 if (listener_interested) {
3284 *listener <<
"whose element #" << exam_pos <<
" doesn't match";
3285 PrintIfNotEmpty(explanations[exam_pos], listener->stream());
3292 if (listener_interested) {
3293 bool reason_printed =
false;
3294 for (
size_t i = 0; i != count(); ++i) {
3295 const std::string& s = explanations[i];
3297 if (reason_printed) {
3298 *listener <<
",\nand ";
3300 *listener <<
"whose element #" << i <<
" matches, " << s;
3301 reason_printed =
true;
3309 static Message Elements(
size_t count) {
3310 return Message() << count << (count == 1 ?
" element" :
" elements");
3313 size_t count()
const {
return matchers_.size(); }
3315 ::std::vector<Matcher<const Element&> > matchers_;
3324 MatchMatrix(
size_t num_elements,
size_t num_matchers)
3325 : num_elements_(num_elements),
3326 num_matchers_(num_matchers),
3327 matched_(num_elements_* num_matchers_, 0) {
3330 size_t LhsSize()
const {
return num_elements_; }
3331 size_t RhsSize()
const {
return num_matchers_; }
3332 bool HasEdge(
size_t ilhs,
size_t irhs)
const {
3333 return matched_[SpaceIndex(ilhs, irhs)] == 1;
3335 void SetEdge(
size_t ilhs,
size_t irhs,
bool b) {
3336 matched_[SpaceIndex(ilhs, irhs)] = b ? 1 : 0;
3346 std::string DebugString()
const;
3349 size_t SpaceIndex(
size_t ilhs,
size_t irhs)
const {
3350 return ilhs * num_matchers_ + irhs;
3353 size_t num_elements_;
3354 size_t num_matchers_;
3359 ::std::vector<char> matched_;
3362 typedef ::std::pair<size_t, size_t> ElementMatcherPair;
3363 typedef ::std::vector<ElementMatcherPair> ElementMatcherPairs;
3368 FindMaxBipartiteMatching(
const MatchMatrix& g);
3370 struct UnorderedMatcherRequire {
3374 ExactMatch = Superset | Subset,
3381 class GTEST_API_ UnorderedElementsAreMatcherImplBase {
3383 explicit UnorderedElementsAreMatcherImplBase(
3384 UnorderedMatcherRequire::Flags matcher_flags)
3385 : match_flags_(matcher_flags) {}
3390 typedef ::std::vector<const MatcherDescriberInterface*> MatcherDescriberVec;
3393 void DescribeToImpl(::std::ostream* os)
const;
3396 void DescribeNegationToImpl(::std::ostream* os)
const;
3398 bool VerifyMatchMatrix(const ::std::vector<std::string>& element_printouts,
3399 const MatchMatrix& matrix,
3400 MatchResultListener* listener)
const;
3402 bool FindPairing(
const MatchMatrix& matrix,
3403 MatchResultListener* listener)
const;
3405 MatcherDescriberVec& matcher_describers() {
3406 return matcher_describers_;
3409 static Message Elements(
size_t n) {
3410 return Message() << n <<
" element" << (n == 1 ?
"" :
"s");
3413 UnorderedMatcherRequire::Flags match_flags()
const {
return match_flags_; }
3416 UnorderedMatcherRequire::Flags match_flags_;
3417 MatcherDescriberVec matcher_describers_;
3422 template <
typename Container>
3423 class UnorderedElementsAreMatcherImpl
3424 :
public MatcherInterface<Container>,
3425 public UnorderedElementsAreMatcherImplBase {
3428 typedef internal::StlContainerView<RawContainer> View;
3429 typedef typename View::type StlContainer;
3430 typedef typename View::const_reference StlContainerReference;
3431 typedef typename StlContainer::const_iterator StlContainerConstIterator;
3432 typedef typename StlContainer::value_type Element;
3434 template <
typename InputIter>
3435 UnorderedElementsAreMatcherImpl(UnorderedMatcherRequire::Flags matcher_flags,
3436 InputIter first, InputIter last)
3437 : UnorderedElementsAreMatcherImplBase(matcher_flags) {
3438 for (; first != last; ++first) {
3439 matchers_.push_back(MatcherCast<const Element&>(*first));
3441 for (
const auto& m : matchers_) {
3442 matcher_describers().push_back(m.GetDescriber());
3447 void DescribeTo(::std::ostream* os)
const override {
3448 return UnorderedElementsAreMatcherImplBase::DescribeToImpl(os);
3452 void DescribeNegationTo(::std::ostream* os)
const override {
3453 return UnorderedElementsAreMatcherImplBase::DescribeNegationToImpl(os);
3456 bool MatchAndExplain(Container container,
3457 MatchResultListener* listener)
const override {
3458 StlContainerReference stl_container = View::ConstReference(container);
3459 ::std::vector<std::string> element_printouts;
3460 MatchMatrix matrix =
3461 AnalyzeElements(stl_container.begin(), stl_container.end(),
3462 &element_printouts, listener);
3464 if (matrix.LhsSize() == 0 && matrix.RhsSize() == 0) {
3468 if (match_flags() == UnorderedMatcherRequire::ExactMatch) {
3469 if (matrix.LhsSize() != matrix.RhsSize()) {
3474 if (matrix.LhsSize() != 0 && listener->IsInterested()) {
3475 *listener <<
"which has " << Elements(matrix.LhsSize());
3481 return VerifyMatchMatrix(element_printouts, matrix, listener) &&
3482 FindPairing(matrix, listener);
3486 template <
typename ElementIter>
3487 MatchMatrix AnalyzeElements(ElementIter elem_first, ElementIter elem_last,
3488 ::std::vector<std::string>* element_printouts,
3489 MatchResultListener* listener)
const {
3490 element_printouts->clear();
3491 ::std::vector<char> did_match;
3492 size_t num_elements = 0;
3493 DummyMatchResultListener dummy;
3494 for (; elem_first != elem_last; ++num_elements, ++elem_first) {
3495 if (listener->IsInterested()) {
3498 for (
size_t irhs = 0; irhs != matchers_.size(); ++irhs) {
3499 did_match.push_back(
3500 matchers_[irhs].MatchAndExplain(*elem_first, &dummy));
3504 MatchMatrix matrix(num_elements, matchers_.size());
3505 ::std::vector<char>::const_iterator did_match_iter = did_match.begin();
3506 for (
size_t ilhs = 0; ilhs != num_elements; ++ilhs) {
3507 for (
size_t irhs = 0; irhs != matchers_.size(); ++irhs) {
3508 matrix.SetEdge(ilhs, irhs, *did_match_iter++ != 0);
3514 ::std::vector<Matcher<const Element&> > matchers_;
3519 template <
typename Target>
3520 struct CastAndAppendTransform {
3521 template <
typename Arg>
3522 Matcher<Target> operator()(
const Arg& a)
const {
3523 return MatcherCast<Target>(a);
3528 template <
typename MatcherTuple>
3529 class UnorderedElementsAreMatcher {
3531 explicit UnorderedElementsAreMatcher(
const MatcherTuple& args)
3532 : matchers_(args) {}
3534 template <
typename Container>
3535 operator Matcher<Container>()
const {
3538 typedef typename View::value_type Element;
3539 typedef ::std::vector<Matcher<const Element&> > MatcherVec;
3540 MatcherVec matchers;
3541 matchers.reserve(::std::tuple_size<MatcherTuple>::value);
3542 TransformTupleValues(CastAndAppendTransform<const Element&>(), matchers_,
3543 ::std::back_inserter(matchers));
3544 return Matcher<Container>(
3545 new UnorderedElementsAreMatcherImpl<const Container&>(
3546 UnorderedMatcherRequire::ExactMatch, matchers.begin(),
3551 const MatcherTuple matchers_;
3555 template <
typename MatcherTuple>
3556 class ElementsAreMatcher {
3558 explicit ElementsAreMatcher(
const MatcherTuple& args) : matchers_(args) {}
3560 template <
typename Container>
3561 operator Matcher<Container>()
const {
3564 ::std::tuple_size<MatcherTuple>::value < 2,
3565 use_UnorderedElementsAre_with_hash_tables);
3569 typedef typename View::value_type Element;
3570 typedef ::std::vector<Matcher<const Element&> > MatcherVec;
3571 MatcherVec matchers;
3572 matchers.reserve(::std::tuple_size<MatcherTuple>::value);
3573 TransformTupleValues(CastAndAppendTransform<const Element&>(), matchers_,
3574 ::std::back_inserter(matchers));
3575 return Matcher<Container>(
new ElementsAreMatcherImpl<const Container&>(
3576 matchers.begin(), matchers.end()));
3580 const MatcherTuple matchers_;
3584 template <
typename T>
3585 class UnorderedElementsAreArrayMatcher {
3587 template <
typename Iter>
3588 UnorderedElementsAreArrayMatcher(UnorderedMatcherRequire::Flags match_flags,
3589 Iter first, Iter last)
3590 : match_flags_(match_flags), matchers_(first, last) {}
3592 template <
typename Container>
3593 operator Matcher<Container>()
const {
3594 return Matcher<Container>(
3595 new UnorderedElementsAreMatcherImpl<const Container&>(
3596 match_flags_, matchers_.begin(), matchers_.end()));
3600 UnorderedMatcherRequire::Flags match_flags_;
3601 ::std::vector<T> matchers_;
3605 template <
typename T>
3606 class ElementsAreArrayMatcher {
3608 template <
typename Iter>
3609 ElementsAreArrayMatcher(Iter first, Iter last) : matchers_(first, last) {}
3611 template <
typename Container>
3612 operator Matcher<Container>()
const {
3615 use_UnorderedElementsAreArray_with_hash_tables);
3617 return Matcher<Container>(
new ElementsAreMatcherImpl<const Container&>(
3618 matchers_.begin(), matchers_.end()));
3622 const ::std::vector<T> matchers_;
3634 template <
typename Tuple2Matcher,
typename Second>
3635 class BoundSecondMatcher {
3637 BoundSecondMatcher(
const Tuple2Matcher& tm,
const Second& second)
3638 : tuple2_matcher_(tm), second_value_(second) {}
3640 BoundSecondMatcher(
const BoundSecondMatcher& other) =
default;
3642 template <
typename T>
3643 operator Matcher<T>()
const {
3644 return MakeMatcher(
new Impl<T>(tuple2_matcher_, second_value_));
3655 void operator=(
const BoundSecondMatcher& ) {
3656 GTEST_LOG_(FATAL) <<
"BoundSecondMatcher should never be assigned.";
3660 template <
typename T>
3661 class Impl :
public MatcherInterface<T> {
3663 typedef ::std::tuple<T, Second> ArgTuple;
3665 Impl(
const Tuple2Matcher& tm,
const Second& second)
3666 : mono_tuple2_matcher_(SafeMatcherCast<const ArgTuple&>(tm)),
3667 second_value_(second) {}
3669 void DescribeTo(::std::ostream* os)
const override {
3673 mono_tuple2_matcher_.DescribeTo(os);
3676 bool MatchAndExplain(T x, MatchResultListener* listener)
const override {
3677 return mono_tuple2_matcher_.MatchAndExplain(ArgTuple(x, second_value_),
3682 const Matcher<const ArgTuple&> mono_tuple2_matcher_;
3683 const Second second_value_;
3686 const Tuple2Matcher tuple2_matcher_;
3687 const Second second_value_;
3694 template <
typename Tuple2Matcher,
typename Second>
3695 BoundSecondMatcher<Tuple2Matcher, Second> MatcherBindSecond(
3696 const Tuple2Matcher& tm,
const Second& second) {
3697 return BoundSecondMatcher<Tuple2Matcher, Second>(tm, second);
3705 GTEST_API_ std::string FormatMatcherDescription(
bool negation,
3706 const char* matcher_name,
3710 template <
typename ValueMatcher>
3711 class OptionalMatcher {
3713 explicit OptionalMatcher(
const ValueMatcher& value_matcher)
3714 : value_matcher_(value_matcher) {}
3716 template <
typename Optional>
3717 operator Matcher<Optional>()
const {
3718 return Matcher<Optional>(
new Impl<const Optional&>(value_matcher_));
3721 template <
typename Optional>
3722 class Impl :
public MatcherInterface<Optional> {
3725 typedef typename OptionalView::value_type ValueType;
3726 explicit Impl(
const ValueMatcher& value_matcher)
3727 : value_matcher_(MatcherCast<ValueType>(value_matcher)) {}
3729 void DescribeTo(::std::ostream* os)
const override {
3731 value_matcher_.DescribeTo(os);
3734 void DescribeNegationTo(::std::ostream* os)
const override {
3736 value_matcher_.DescribeNegationTo(os);
3739 bool MatchAndExplain(Optional optional,
3740 MatchResultListener* listener)
const override {
3742 *listener <<
"which is not engaged";
3745 const ValueType& value = *optional;
3746 StringMatchResultListener value_listener;
3747 const bool match = value_matcher_.MatchAndExplain(value, &value_listener);
3749 << (match ?
" matches" :
" doesn't match");
3750 PrintIfNotEmpty(value_listener.str(), listener->stream());
3755 const Matcher<ValueType> value_matcher_;
3759 const ValueMatcher value_matcher_;
3762 namespace variant_matcher {
3764 template <
typename T>
3765 void holds_alternative() {}
3766 template <
typename T>
3770 template <
typename T>
3771 class VariantMatcher {
3774 : matcher_(std::move(matcher)) {}
3776 template <
typename Variant>
3777 bool MatchAndExplain(
const Variant& value,
3778 ::testing::MatchResultListener* listener)
const {
3780 if (!listener->IsInterested()) {
3781 return holds_alternative<T>(value) && matcher_.Matches(get<T>(value));
3784 if (!holds_alternative<T>(value)) {
3785 *listener <<
"whose value is not of type '" <<
GetTypeName() <<
"'";
3789 const T& elem = get<T>(value);
3790 StringMatchResultListener elem_listener;
3791 const bool match = matcher_.MatchAndExplain(elem, &elem_listener);
3793 << (match ?
" matches" :
" doesn't match");
3794 PrintIfNotEmpty(elem_listener.str(), listener->stream());
3798 void DescribeTo(std::ostream* os)
const {
3799 *os <<
"is a variant<> with value of type '" <<
GetTypeName()
3800 <<
"' and the value ";
3801 matcher_.DescribeTo(os);
3804 void DescribeNegationTo(std::ostream* os)
const {
3805 *os <<
"is a variant<> with value of type other than '" <<
GetTypeName()
3806 <<
"' or the value ";
3807 matcher_.DescribeNegationTo(os);
3814 return internal::GetTypeName<T>());
3816 return "the element type";
3819 const ::testing::Matcher<const T&> matcher_;
3824 namespace any_cast_matcher {
3827 template <
typename T>
3831 template <
typename T>
3832 class AnyCastMatcher {
3834 explicit AnyCastMatcher(const ::testing::Matcher<const T&>& matcher)
3835 : matcher_(matcher) {}
3837 template <
typename AnyType>
3838 bool MatchAndExplain(
const AnyType& value,
3839 ::testing::MatchResultListener* listener)
const {
3840 if (!listener->IsInterested()) {
3841 const T* ptr = any_cast<T>(&value);
3842 return ptr !=
nullptr && matcher_.Matches(*ptr);
3845 const T* elem = any_cast<T>(&value);
3846 if (elem ==
nullptr) {
3847 *listener <<
"whose value is not of type '" <<
GetTypeName() <<
"'";
3851 StringMatchResultListener elem_listener;
3852 const bool match = matcher_.MatchAndExplain(*elem, &elem_listener);
3854 << (match ?
" matches" :
" doesn't match");
3855 PrintIfNotEmpty(elem_listener.str(), listener->stream());
3859 void DescribeTo(std::ostream* os)
const {
3860 *os <<
"is an 'any' type with value of type '" <<
GetTypeName()
3861 <<
"' and the value ";
3862 matcher_.DescribeTo(os);
3865 void DescribeNegationTo(std::ostream* os)
const {
3866 *os <<
"is an 'any' type with value of type other than '" <<
GetTypeName()
3867 <<
"' or the value ";
3868 matcher_.DescribeNegationTo(os);
3875 return internal::GetTypeName<T>());
3877 return "the element type";
3880 const ::testing::Matcher<const T&> matcher_;
3886 template <
class ArgsTuple,
size_t... k>
3887 class ArgsMatcherImpl :
public MatcherInterface<ArgsTuple> {
3889 using RawArgsTuple =
typename std::decay<ArgsTuple>::type;
3890 using SelectedArgs =
3891 std::tuple<typename std::tuple_element<k, RawArgsTuple>::type...>;
3892 using MonomorphicInnerMatcher = Matcher<const SelectedArgs&>;
3894 template <
typename InnerMatcher>
3895 explicit ArgsMatcherImpl(
const InnerMatcher& inner_matcher)
3896 : inner_matcher_(SafeMatcherCast<const SelectedArgs&>(inner_matcher)) {}
3898 bool MatchAndExplain(ArgsTuple args,
3899 MatchResultListener* listener)
const override {
3902 const SelectedArgs& selected_args =
3903 std::forward_as_tuple(std::get<k>(args)...);
3904 if (!listener->IsInterested())
return inner_matcher_.Matches(selected_args);
3906 PrintIndices(listener->stream());
3909 StringMatchResultListener inner_listener;
3911 inner_matcher_.MatchAndExplain(selected_args, &inner_listener);
3912 PrintIfNotEmpty(inner_listener.str(), listener->stream());
3916 void DescribeTo(::std::ostream* os)
const override {
3917 *os <<
"are a tuple ";
3919 inner_matcher_.DescribeTo(os);
3922 void DescribeNegationTo(::std::ostream* os)
const override {
3923 *os <<
"are a tuple ";
3925 inner_matcher_.DescribeNegationTo(os);
3930 static void PrintIndices(::std::ostream* os) {
3931 *os <<
"whose fields (";
3932 const char* sep =
"";
3935 const char* dummy[] = {
"", (*os << sep <<
"#" << k, sep =
", ")...};
3940 MonomorphicInnerMatcher inner_matcher_;
3943 template <
class InnerMatcher,
size_t... k>
3946 explicit ArgsMatcher(InnerMatcher inner_matcher)
3947 : inner_matcher_(std::move(inner_matcher)) {}
3949 template <
typename ArgsTuple>
3950 operator Matcher<ArgsTuple>()
const {
3951 return MakeMatcher(
new ArgsMatcherImpl<ArgsTuple, k...>(inner_matcher_));
3955 InnerMatcher inner_matcher_;
3975 template <
typename Iter>
3976 inline internal::ElementsAreArrayMatcher<
3977 typename ::std::iterator_traits<Iter>::value_type>
3978 ElementsAreArray(Iter first, Iter last) {
3979 typedef typename ::std::iterator_traits<Iter>::value_type T;
3980 return internal::ElementsAreArrayMatcher<T>(first, last);
3983 template <
typename T>
3984 inline internal::ElementsAreArrayMatcher<T> ElementsAreArray(
3985 const T* pointer,
size_t count) {
3986 return ElementsAreArray(pointer, pointer + count);
3989 template <
typename T,
size_t N>
3990 inline internal::ElementsAreArrayMatcher<T> ElementsAreArray(
3991 const T (&array)[N]) {
3992 return ElementsAreArray(array, N);
3995 template <
typename Container>
3996 inline internal::ElementsAreArrayMatcher<typename Container::value_type>
3997 ElementsAreArray(
const Container& container) {
3998 return ElementsAreArray(container.begin(), container.end());
4001 template <
typename T>
4002 inline internal::ElementsAreArrayMatcher<T>
4003 ElementsAreArray(::std::initializer_list<T> xs) {
4004 return ElementsAreArray(xs.begin(), xs.end());
4020 template <
typename Iter>
4021 inline internal::UnorderedElementsAreArrayMatcher<
4022 typename ::std::iterator_traits<Iter>::value_type>
4023 UnorderedElementsAreArray(Iter first, Iter last) {
4024 typedef typename ::std::iterator_traits<Iter>::value_type T;
4025 return internal::UnorderedElementsAreArrayMatcher<T>(
4026 internal::UnorderedMatcherRequire::ExactMatch, first, last);
4029 template <
typename T>
4030 inline internal::UnorderedElementsAreArrayMatcher<T>
4031 UnorderedElementsAreArray(
const T* pointer,
size_t count) {
4032 return UnorderedElementsAreArray(pointer, pointer + count);
4035 template <
typename T,
size_t N>
4036 inline internal::UnorderedElementsAreArrayMatcher<T>
4037 UnorderedElementsAreArray(
const T (&array)[N]) {
4038 return UnorderedElementsAreArray(array, N);
4041 template <
typename Container>
4042 inline internal::UnorderedElementsAreArrayMatcher<
4043 typename Container::value_type>
4044 UnorderedElementsAreArray(
const Container& container) {
4045 return UnorderedElementsAreArray(container.begin(), container.end());
4048 template <
typename T>
4049 inline internal::UnorderedElementsAreArrayMatcher<T>
4050 UnorderedElementsAreArray(::std::initializer_list<T> xs) {
4051 return UnorderedElementsAreArray(xs.begin(), xs.end());
4063 const internal::AnythingMatcher _ = {};
4065 template <
typename T>
4066 inline Matcher<T> A() {
4071 template <
typename T>
4072 inline Matcher<T> An() {
4076 template <
typename T,
typename M>
4077 Matcher<T> internal::MatcherCastImpl<T, M>::CastImpl(
4078 const M& value, std::false_type ,
4084 inline PolymorphicMatcher<internal::IsNullMatcher > IsNull() {
4085 return MakePolymorphicMatcher(internal::IsNullMatcher());
4091 inline PolymorphicMatcher<internal::NotNullMatcher > NotNull() {
4092 return MakePolymorphicMatcher(internal::NotNullMatcher());
4097 template <
typename T>
4098 inline internal::RefMatcher<T&> Ref(T& x) {
4099 return internal::RefMatcher<T&>(x);
4103 inline PolymorphicMatcher<internal::IsNanMatcher> IsNan() {
4104 return MakePolymorphicMatcher(internal::IsNanMatcher());
4109 inline internal::FloatingEqMatcher<double> DoubleEq(
double rhs) {
4110 return internal::FloatingEqMatcher<double>(rhs,
false);
4115 inline internal::FloatingEqMatcher<double> NanSensitiveDoubleEq(
double rhs) {
4116 return internal::FloatingEqMatcher<double>(rhs,
true);
4122 inline internal::FloatingEqMatcher<double> DoubleNear(
4123 double rhs,
double max_abs_error) {
4124 return internal::FloatingEqMatcher<double>(rhs,
false, max_abs_error);
4130 inline internal::FloatingEqMatcher<double> NanSensitiveDoubleNear(
4131 double rhs,
double max_abs_error) {
4132 return internal::FloatingEqMatcher<double>(rhs,
true, max_abs_error);
4137 inline internal::FloatingEqMatcher<float> FloatEq(
float rhs) {
4138 return internal::FloatingEqMatcher<float>(rhs,
false);
4143 inline internal::FloatingEqMatcher<float> NanSensitiveFloatEq(
float rhs) {
4144 return internal::FloatingEqMatcher<float>(rhs,
true);
4150 inline internal::FloatingEqMatcher<float> FloatNear(
4151 float rhs,
float max_abs_error) {
4152 return internal::FloatingEqMatcher<float>(rhs,
false, max_abs_error);
4158 inline internal::FloatingEqMatcher<float> NanSensitiveFloatNear(
4159 float rhs,
float max_abs_error) {
4160 return internal::FloatingEqMatcher<float>(rhs,
true, max_abs_error);
4165 template <
typename InnerMatcher>
4166 inline internal::PointeeMatcher<InnerMatcher> Pointee(
4167 const InnerMatcher& inner_matcher) {
4168 return internal::PointeeMatcher<InnerMatcher>(inner_matcher);
4178 template <
typename To>
4179 inline PolymorphicMatcher<internal::WhenDynamicCastToMatcher<To> >
4180 WhenDynamicCastTo(
const Matcher<To>& inner_matcher) {
4181 return MakePolymorphicMatcher(
4182 internal::WhenDynamicCastToMatcher<To>(inner_matcher));
4190 template <
typename Class,
typename FieldType,
typename FieldMatcher>
4191 inline PolymorphicMatcher<
4192 internal::FieldMatcher<Class, FieldType> > Field(
4193 FieldType Class::*field,
const FieldMatcher& matcher) {
4194 return MakePolymorphicMatcher(
4195 internal::FieldMatcher<Class, FieldType>(
4196 field, MatcherCast<const FieldType&>(matcher)));
4205 template <
typename Class,
typename FieldType,
typename FieldMatcher>
4206 inline PolymorphicMatcher<internal::FieldMatcher<Class, FieldType> > Field(
4207 const std::string& field_name, FieldType Class::*field,
4208 const FieldMatcher& matcher) {
4209 return MakePolymorphicMatcher(internal::FieldMatcher<Class, FieldType>(
4210 field_name, field, MatcherCast<const FieldType&>(matcher)));
4217 template <
typename Class,
typename PropertyType,
typename PropertyMatcher>
4218 inline PolymorphicMatcher<internal::PropertyMatcher<
4219 Class, PropertyType, PropertyType (Class::*)()
const> >
4220 Property(PropertyType (Class::*property)()
const,
4221 const PropertyMatcher& matcher) {
4222 return MakePolymorphicMatcher(
4223 internal::PropertyMatcher<Class, PropertyType,
4224 PropertyType (Class::*)()
const>(
4225 property, MatcherCast<const PropertyType&>(matcher)));
4234 template <
typename Class,
typename PropertyType,
typename PropertyMatcher>
4235 inline PolymorphicMatcher<internal::PropertyMatcher<
4236 Class, PropertyType, PropertyType (Class::*)()
const> >
4237 Property(
const std::string& property_name,
4238 PropertyType (Class::*property)()
const,
4239 const PropertyMatcher& matcher) {
4240 return MakePolymorphicMatcher(
4241 internal::PropertyMatcher<Class, PropertyType,
4242 PropertyType (Class::*)()
const>(
4243 property_name, property, MatcherCast<const PropertyType&>(matcher)));
4247 template <
typename Class,
typename PropertyType,
typename PropertyMatcher>
4248 inline PolymorphicMatcher<internal::PropertyMatcher<
4249 Class, PropertyType, PropertyType (Class::*)()
const &> >
4250 Property(PropertyType (Class::*property)()
const &,
4251 const PropertyMatcher& matcher) {
4252 return MakePolymorphicMatcher(
4253 internal::PropertyMatcher<Class, PropertyType,
4254 PropertyType (Class::*)()
const&>(
4255 property, MatcherCast<const PropertyType&>(matcher)));
4259 template <
typename Class,
typename PropertyType,
typename PropertyMatcher>
4260 inline PolymorphicMatcher<internal::PropertyMatcher<
4261 Class, PropertyType, PropertyType (Class::*)()
const &> >
4262 Property(
const std::string& property_name,
4263 PropertyType (Class::*property)()
const &,
4264 const PropertyMatcher& matcher) {
4265 return MakePolymorphicMatcher(
4266 internal::PropertyMatcher<Class, PropertyType,
4267 PropertyType (Class::*)()
const&>(
4268 property_name, property, MatcherCast<const PropertyType&>(matcher)));
4279 template <
typename Callable,
typename InnerMatcher>
4280 internal::ResultOfMatcher<Callable, InnerMatcher> ResultOf(
4281 Callable callable, InnerMatcher matcher) {
4282 return internal::ResultOfMatcher<Callable, InnerMatcher>(
4283 std::move(callable), std::move(matcher));
4289 template <
typename T = std::
string>
4290 PolymorphicMatcher<internal::StrEqualityMatcher<std::string> > StrEq(
4291 const internal::StringLike<T>& str) {
4292 return MakePolymorphicMatcher(
4293 internal::StrEqualityMatcher<std::string>(std::string(str),
true,
true));
4297 template <
typename T = std::
string>
4298 PolymorphicMatcher<internal::StrEqualityMatcher<std::string> > StrNe(
4299 const internal::StringLike<T>& str) {
4300 return MakePolymorphicMatcher(
4301 internal::StrEqualityMatcher<std::string>(std::string(str),
false,
true));
4305 template <
typename T = std::
string>
4306 PolymorphicMatcher<internal::StrEqualityMatcher<std::string> > StrCaseEq(
4307 const internal::StringLike<T>& str) {
4308 return MakePolymorphicMatcher(
4309 internal::StrEqualityMatcher<std::string>(std::string(str),
true,
false));
4313 template <
typename T = std::
string>
4314 PolymorphicMatcher<internal::StrEqualityMatcher<std::string> > StrCaseNe(
4315 const internal::StringLike<T>& str) {
4316 return MakePolymorphicMatcher(internal::StrEqualityMatcher<std::string>(
4317 std::string(str),
false,
false));
4322 template <
typename T = std::
string>
4323 PolymorphicMatcher<internal::HasSubstrMatcher<std::string> > HasSubstr(
4324 const internal::StringLike<T>& substring) {
4325 return MakePolymorphicMatcher(
4326 internal::HasSubstrMatcher<std::string>(std::string(substring)));
4330 template <
typename T = std::
string>
4331 PolymorphicMatcher<internal::StartsWithMatcher<std::string> > StartsWith(
4332 const internal::StringLike<T>& prefix) {
4333 return MakePolymorphicMatcher(
4334 internal::StartsWithMatcher<std::string>(std::string(prefix)));
4338 template <
typename T = std::
string>
4339 PolymorphicMatcher<internal::EndsWithMatcher<std::string> > EndsWith(
4340 const internal::StringLike<T>& suffix) {
4341 return MakePolymorphicMatcher(
4342 internal::EndsWithMatcher<std::string>(std::string(suffix)));
4345 #if GTEST_HAS_STD_WSTRING
4349 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring> > StrEq(
4350 const std::wstring& str) {
4351 return MakePolymorphicMatcher(
4352 internal::StrEqualityMatcher<std::wstring>(str,
true,
true));
4356 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring> > StrNe(
4357 const std::wstring& str) {
4358 return MakePolymorphicMatcher(
4359 internal::StrEqualityMatcher<std::wstring>(str,
false,
true));
4363 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring> >
4364 StrCaseEq(
const std::wstring& str) {
4365 return MakePolymorphicMatcher(
4366 internal::StrEqualityMatcher<std::wstring>(str,
true,
false));
4370 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring> >
4371 StrCaseNe(
const std::wstring& str) {
4372 return MakePolymorphicMatcher(
4373 internal::StrEqualityMatcher<std::wstring>(str,
false,
false));
4378 inline PolymorphicMatcher<internal::HasSubstrMatcher<std::wstring> > HasSubstr(
4379 const std::wstring& substring) {
4380 return MakePolymorphicMatcher(
4381 internal::HasSubstrMatcher<std::wstring>(substring));
4385 inline PolymorphicMatcher<internal::StartsWithMatcher<std::wstring> >
4386 StartsWith(
const std::wstring& prefix) {
4387 return MakePolymorphicMatcher(
4388 internal::StartsWithMatcher<std::wstring>(prefix));
4392 inline PolymorphicMatcher<internal::EndsWithMatcher<std::wstring> > EndsWith(
4393 const std::wstring& suffix) {
4394 return MakePolymorphicMatcher(
4395 internal::EndsWithMatcher<std::wstring>(suffix));
4402 inline internal::Eq2Matcher Eq() {
return internal::Eq2Matcher(); }
4406 inline internal::Ge2Matcher Ge() {
return internal::Ge2Matcher(); }
4410 inline internal::Gt2Matcher Gt() {
return internal::Gt2Matcher(); }
4414 inline internal::Le2Matcher Le() {
return internal::Le2Matcher(); }
4418 inline internal::Lt2Matcher Lt() {
return internal::Lt2Matcher(); }
4422 inline internal::Ne2Matcher Ne() {
return internal::Ne2Matcher(); }
4426 inline internal::FloatingEq2Matcher<float> FloatEq() {
4427 return internal::FloatingEq2Matcher<float>();
4432 inline internal::FloatingEq2Matcher<double> DoubleEq() {
4433 return internal::FloatingEq2Matcher<double>();
4438 inline internal::FloatingEq2Matcher<float> NanSensitiveFloatEq() {
4439 return internal::FloatingEq2Matcher<float>(
true);
4444 inline internal::FloatingEq2Matcher<double> NanSensitiveDoubleEq() {
4445 return internal::FloatingEq2Matcher<double>(
true);
4450 inline internal::FloatingEq2Matcher<float> FloatNear(
float max_abs_error) {
4451 return internal::FloatingEq2Matcher<float>(max_abs_error);
4456 inline internal::FloatingEq2Matcher<double> DoubleNear(
double max_abs_error) {
4457 return internal::FloatingEq2Matcher<double>(max_abs_error);
4463 inline internal::FloatingEq2Matcher<float> NanSensitiveFloatNear(
4464 float max_abs_error) {
4465 return internal::FloatingEq2Matcher<float>(max_abs_error,
true);
4471 inline internal::FloatingEq2Matcher<double> NanSensitiveDoubleNear(
4472 double max_abs_error) {
4473 return internal::FloatingEq2Matcher<double>(max_abs_error,
true);
4478 template <
typename InnerMatcher>
4479 inline internal::NotMatcher<InnerMatcher> Not(InnerMatcher m) {
4480 return internal::NotMatcher<InnerMatcher>(m);
4486 template <
typename Predicate>
4487 inline PolymorphicMatcher<internal::TrulyMatcher<Predicate> >
4488 Truly(Predicate pred) {
4489 return MakePolymorphicMatcher(internal::TrulyMatcher<Predicate>(pred));
4498 template <
typename SizeMatcher>
4499 inline internal::SizeIsMatcher<SizeMatcher>
4500 SizeIs(
const SizeMatcher& size_matcher) {
4501 return internal::SizeIsMatcher<SizeMatcher>(size_matcher);
4509 template <
typename DistanceMatcher>
4510 inline internal::BeginEndDistanceIsMatcher<DistanceMatcher>
4511 BeginEndDistanceIs(
const DistanceMatcher& distance_matcher) {
4512 return internal::BeginEndDistanceIsMatcher<DistanceMatcher>(distance_matcher);
4519 template <
typename Container>
4520 inline PolymorphicMatcher<internal::ContainerEqMatcher<
4521 typename std::remove_const<Container>::type>>
4522 ContainerEq(
const Container& rhs) {
4523 return MakePolymorphicMatcher(internal::ContainerEqMatcher<Container>(rhs));
4528 template <
typename Comparator,
typename ContainerMatcher>
4529 inline internal::WhenSortedByMatcher<Comparator, ContainerMatcher>
4530 WhenSortedBy(
const Comparator& comparator,
4531 const ContainerMatcher& container_matcher) {
4532 return internal::WhenSortedByMatcher<Comparator, ContainerMatcher>(
4533 comparator, container_matcher);
4538 template <
typename ContainerMatcher>
4539 inline internal::WhenSortedByMatcher<internal::LessComparator, ContainerMatcher>
4540 WhenSorted(
const ContainerMatcher& container_matcher) {
4542 internal::WhenSortedByMatcher<internal::LessComparator, ContainerMatcher>(
4543 internal::LessComparator(), container_matcher);
4552 template <
typename TupleMatcher,
typename Container>
4553 inline internal::PointwiseMatcher<TupleMatcher,
4554 typename std::remove_const<Container>::type>
4555 Pointwise(
const TupleMatcher& tuple_matcher,
const Container& rhs) {
4556 return internal::PointwiseMatcher<TupleMatcher, Container>(tuple_matcher,
4562 template <
typename TupleMatcher,
typename T>
4563 inline internal::PointwiseMatcher<TupleMatcher, std::vector<T> > Pointwise(
4564 const TupleMatcher& tuple_matcher, std::initializer_list<T> rhs) {
4565 return Pointwise(tuple_matcher, std::vector<T>(rhs));
4580 template <
typename Tuple2Matcher,
typename RhsContainer>
4581 inline internal::UnorderedElementsAreArrayMatcher<
4582 typename internal::BoundSecondMatcher<
4584 typename internal::StlContainerView<
4585 typename std::remove_const<RhsContainer>::type>::type::value_type>>
4586 UnorderedPointwise(
const Tuple2Matcher& tuple2_matcher,
4587 const RhsContainer& rhs_container) {
4590 typedef typename internal::StlContainerView<RhsContainer> RhsView;
4591 typedef typename RhsView::type RhsStlContainer;
4592 typedef typename RhsStlContainer::value_type Second;
4593 const RhsStlContainer& rhs_stl_container =
4594 RhsView::ConstReference(rhs_container);
4597 ::std::vector<internal::BoundSecondMatcher<Tuple2Matcher, Second> > matchers;
4598 for (
typename RhsStlContainer::const_iterator it = rhs_stl_container.begin();
4599 it != rhs_stl_container.end(); ++it) {
4601 internal::MatcherBindSecond(tuple2_matcher, *it));
4605 return UnorderedElementsAreArray(matchers);
4610 template <
typename Tuple2Matcher,
typename T>
4611 inline internal::UnorderedElementsAreArrayMatcher<
4612 typename internal::BoundSecondMatcher<Tuple2Matcher, T> >
4613 UnorderedPointwise(
const Tuple2Matcher& tuple2_matcher,
4614 std::initializer_list<T> rhs) {
4615 return UnorderedPointwise(tuple2_matcher, std::vector<T>(rhs));
4637 template <
typename M>
4638 inline internal::ContainsMatcher<M> Contains(M matcher) {
4639 return internal::ContainsMatcher<M>(matcher);
4669 template <
typename Iter>
4670 inline internal::UnorderedElementsAreArrayMatcher<
4671 typename ::std::iterator_traits<Iter>::value_type>
4672 IsSupersetOf(Iter first, Iter last) {
4673 typedef typename ::std::iterator_traits<Iter>::value_type T;
4674 return internal::UnorderedElementsAreArrayMatcher<T>(
4675 internal::UnorderedMatcherRequire::Superset, first, last);
4678 template <
typename T>
4679 inline internal::UnorderedElementsAreArrayMatcher<T> IsSupersetOf(
4680 const T* pointer,
size_t count) {
4681 return IsSupersetOf(pointer, pointer + count);
4684 template <
typename T,
size_t N>
4685 inline internal::UnorderedElementsAreArrayMatcher<T> IsSupersetOf(
4686 const T (&array)[N]) {
4687 return IsSupersetOf(array, N);
4690 template <
typename Container>
4691 inline internal::UnorderedElementsAreArrayMatcher<
4692 typename Container::value_type>
4693 IsSupersetOf(
const Container& container) {
4694 return IsSupersetOf(container.begin(), container.end());
4697 template <
typename T>
4698 inline internal::UnorderedElementsAreArrayMatcher<T> IsSupersetOf(
4699 ::std::initializer_list<T> xs) {
4700 return IsSupersetOf(xs.begin(), xs.end());
4726 template <
typename Iter>
4727 inline internal::UnorderedElementsAreArrayMatcher<
4728 typename ::std::iterator_traits<Iter>::value_type>
4729 IsSubsetOf(Iter first, Iter last) {
4730 typedef typename ::std::iterator_traits<Iter>::value_type T;
4731 return internal::UnorderedElementsAreArrayMatcher<T>(
4732 internal::UnorderedMatcherRequire::Subset, first, last);
4735 template <
typename T>
4736 inline internal::UnorderedElementsAreArrayMatcher<T> IsSubsetOf(
4737 const T* pointer,
size_t count) {
4738 return IsSubsetOf(pointer, pointer + count);
4741 template <
typename T,
size_t N>
4742 inline internal::UnorderedElementsAreArrayMatcher<T> IsSubsetOf(
4743 const T (&array)[N]) {
4744 return IsSubsetOf(array, N);
4747 template <
typename Container>
4748 inline internal::UnorderedElementsAreArrayMatcher<
4749 typename Container::value_type>
4750 IsSubsetOf(
const Container& container) {
4751 return IsSubsetOf(container.begin(), container.end());
4754 template <
typename T>
4755 inline internal::UnorderedElementsAreArrayMatcher<T> IsSubsetOf(
4756 ::std::initializer_list<T> xs) {
4757 return IsSubsetOf(xs.begin(), xs.end());
4787 template <
typename M>
4788 inline internal::EachMatcher<M> Each(M matcher) {
4789 return internal::EachMatcher<M>(matcher);
4795 template <
typename M>
4796 inline internal::KeyMatcher<M> Key(M inner_matcher) {
4797 return internal::KeyMatcher<M>(inner_matcher);
4805 template <
typename FirstMatcher,
typename SecondMatcher>
4806 inline internal::PairMatcher<FirstMatcher, SecondMatcher>
4807 Pair(FirstMatcher first_matcher, SecondMatcher second_matcher) {
4808 return internal::PairMatcher<FirstMatcher, SecondMatcher>(
4809 first_matcher, second_matcher);
4817 template <
typename... M>
4818 internal::FieldsAreMatcher<typename std::decay<M>::type...> FieldsAre(
4820 return internal::FieldsAreMatcher<typename std::decay<M>::type...>(
4821 std::forward<M>(matchers)...);
4826 template <
typename InnerMatcher>
4827 inline internal::PointerMatcher<InnerMatcher> Pointer(
4828 const InnerMatcher& inner_matcher) {
4829 return internal::PointerMatcher<InnerMatcher>(inner_matcher);
4834 template <
typename InnerMatcher>
4835 inline internal::AddressMatcher<InnerMatcher> Address(
4836 const InnerMatcher& inner_matcher) {
4837 return internal::AddressMatcher<InnerMatcher>(inner_matcher);
4843 template <
typename M>
4844 inline internal::MatcherAsPredicate<M> Matches(M matcher) {
4845 return internal::MatcherAsPredicate<M>(matcher);
4849 template <
typename T,
typename M>
4850 inline bool Value(
const T& value, M matcher) {
4851 return testing::Matches(matcher)(value);
4856 template <
typename T,
typename M>
4857 inline bool ExplainMatchResult(
4858 M matcher,
const T& value, MatchResultListener* listener) {
4859 return SafeMatcherCast<const T&>(matcher).MatchAndExplain(value, listener);
4872 template <
typename T,
typename M>
4873 std::string DescribeMatcher(
const M& matcher,
bool negation =
false) {
4874 ::std::stringstream ss;
4875 Matcher<T> monomorphic_matcher = SafeMatcherCast<T>(matcher);
4877 monomorphic_matcher.DescribeNegationTo(&ss);
4879 monomorphic_matcher.DescribeTo(&ss);
4884 template <
typename... Args>
4885 internal::ElementsAreMatcher<
4886 std::tuple<typename std::decay<const Args&>::type...>>
4887 ElementsAre(
const Args&... matchers) {
4888 return internal::ElementsAreMatcher<
4889 std::tuple<typename std::decay<const Args&>::type...>>(
4890 std::make_tuple(matchers...));
4893 template <
typename... Args>
4894 internal::UnorderedElementsAreMatcher<
4895 std::tuple<typename std::decay<const Args&>::type...>>
4896 UnorderedElementsAre(
const Args&... matchers) {
4897 return internal::UnorderedElementsAreMatcher<
4898 std::tuple<typename std::decay<const Args&>::type...>>(
4899 std::make_tuple(matchers...));
4903 template <
typename... Args>
4904 internal::AllOfMatcher<typename std::decay<const Args&>::type...> AllOf(
4905 const Args&... matchers) {
4906 return internal::AllOfMatcher<typename std::decay<const Args&>::type...>(
4910 template <
typename... Args>
4911 internal::AnyOfMatcher<typename std::decay<const Args&>::type...> AnyOf(
4912 const Args&... matchers) {
4913 return internal::AnyOfMatcher<typename std::decay<const Args&>::type...>(
4939 template <
typename Iter>
4940 inline internal::AnyOfArrayMatcher<
4941 typename ::std::iterator_traits<Iter>::value_type>
4942 AnyOfArray(Iter first, Iter last) {
4943 return internal::AnyOfArrayMatcher<
4944 typename ::std::iterator_traits<Iter>::value_type>(first, last);
4947 template <
typename Iter>
4948 inline internal::AllOfArrayMatcher<
4949 typename ::std::iterator_traits<Iter>::value_type>
4950 AllOfArray(Iter first, Iter last) {
4951 return internal::AllOfArrayMatcher<
4952 typename ::std::iterator_traits<Iter>::value_type>(first, last);
4955 template <
typename T>
4956 inline internal::AnyOfArrayMatcher<T> AnyOfArray(
const T* ptr,
size_t count) {
4957 return AnyOfArray(ptr, ptr + count);
4960 template <
typename T>
4961 inline internal::AllOfArrayMatcher<T> AllOfArray(
const T* ptr,
size_t count) {
4962 return AllOfArray(ptr, ptr + count);
4965 template <
typename T,
size_t N>
4966 inline internal::AnyOfArrayMatcher<T> AnyOfArray(
const T (&array)[N]) {
4967 return AnyOfArray(array, N);
4970 template <
typename T,
size_t N>
4971 inline internal::AllOfArrayMatcher<T> AllOfArray(
const T (&array)[N]) {
4972 return AllOfArray(array, N);
4975 template <
typename Container>
4976 inline internal::AnyOfArrayMatcher<typename Container::value_type> AnyOfArray(
4977 const Container& container) {
4978 return AnyOfArray(container.begin(), container.end());
4981 template <
typename Container>
4982 inline internal::AllOfArrayMatcher<typename Container::value_type> AllOfArray(
4983 const Container& container) {
4984 return AllOfArray(container.begin(), container.end());
4987 template <
typename T>
4988 inline internal::AnyOfArrayMatcher<T> AnyOfArray(
4989 ::std::initializer_list<T> xs) {
4990 return AnyOfArray(xs.begin(), xs.end());
4993 template <
typename T>
4994 inline internal::AllOfArrayMatcher<T> AllOfArray(
4995 ::std::initializer_list<T> xs) {
4996 return AllOfArray(xs.begin(), xs.end());
5002 template <
size_t... k,
typename InnerMatcher>
5003 internal::ArgsMatcher<typename std::decay<InnerMatcher>::type, k...> Args(
5004 InnerMatcher&& matcher) {
5005 return internal::ArgsMatcher<typename std::decay<InnerMatcher>::type, k...>(
5006 std::forward<InnerMatcher>(matcher));
5016 template <
typename InnerMatcher>
5017 inline InnerMatcher AllArgs(
const InnerMatcher& matcher) {
return matcher; }
5027 template <
typename ValueMatcher>
5028 inline internal::OptionalMatcher<ValueMatcher> Optional(
5029 const ValueMatcher& value_matcher) {
5030 return internal::OptionalMatcher<ValueMatcher>(value_matcher);
5034 template <
typename T>
5035 PolymorphicMatcher<internal::any_cast_matcher::AnyCastMatcher<T> > AnyWith(
5036 const Matcher<const T&>& matcher) {
5037 return MakePolymorphicMatcher(
5038 internal::any_cast_matcher::AnyCastMatcher<T>(matcher));
5045 template <
typename T>
5046 PolymorphicMatcher<internal::variant_matcher::VariantMatcher<T> > VariantWith(
5047 const Matcher<const T&>& matcher) {
5048 return MakePolymorphicMatcher(
5049 internal::variant_matcher::VariantMatcher<T>(matcher));
5052 #if GTEST_HAS_EXCEPTIONS
5056 namespace internal {
5058 class WithWhatMatcherImpl {
5060 WithWhatMatcherImpl(Matcher<std::string> matcher)
5061 : matcher_(std::move(matcher)) {}
5063 void DescribeTo(std::ostream* os)
const {
5064 *os <<
"contains .what() that ";
5065 matcher_.DescribeTo(os);
5068 void DescribeNegationTo(std::ostream* os)
const {
5069 *os <<
"contains .what() that does not ";
5070 matcher_.DescribeTo(os);
5073 template <
typename Err>
5074 bool MatchAndExplain(
const Err& err, MatchResultListener* listener)
const {
5075 *listener <<
"which contains .what() that ";
5076 return matcher_.MatchAndExplain(err.what(), listener);
5080 const Matcher<std::string> matcher_;
5083 inline PolymorphicMatcher<WithWhatMatcherImpl> WithWhat(
5084 Matcher<std::string> m) {
5085 return MakePolymorphicMatcher(WithWhatMatcherImpl(std::move(m)));
5088 template <
typename Err>
5089 class ExceptionMatcherImpl {
5092 const char* what()
const noexcept {
5093 return "this exception should never be thrown";
5118 using DefaultExceptionType =
typename std::conditional<
5119 std::is_same<
typename std::remove_cv<
5120 typename std::remove_reference<Err>::type>::type,
5121 std::exception>::value,
5122 const NeverThrown&,
const std::exception&>::type;
5125 ExceptionMatcherImpl(Matcher<const Err&> matcher)
5126 : matcher_(std::move(matcher)) {}
5128 void DescribeTo(std::ostream* os)
const {
5129 *os <<
"throws an exception which is a " << GetTypeName<Err>();
5131 matcher_.DescribeTo(os);
5134 void DescribeNegationTo(std::ostream* os)
const {
5135 *os <<
"throws an exception which is not a " << GetTypeName<Err>();
5137 matcher_.DescribeNegationTo(os);
5140 template <
typename T>
5141 bool MatchAndExplain(T&& x, MatchResultListener* listener)
const {
5143 (void)(std::forward<T>(x)());
5144 }
catch (
const Err& err) {
5145 *listener <<
"throws an exception which is a " << GetTypeName<Err>();
5147 return matcher_.MatchAndExplain(err, listener);
5148 }
catch (DefaultExceptionType err) {
5150 *listener <<
"throws an exception of type " <<
GetTypeName(
typeid(err));
5153 *listener <<
"throws an std::exception-derived type ";
5155 *listener <<
"with description \"" << err.what() <<
"\"";
5158 *listener <<
"throws an exception of an unknown type";
5162 *listener <<
"does not throw any exception";
5167 const Matcher<const Err&> matcher_;
5194 template <
typename Err>
5195 PolymorphicMatcher<internal::ExceptionMatcherImpl<Err>> Throws() {
5196 return MakePolymorphicMatcher(
5197 internal::ExceptionMatcherImpl<Err>(A<const Err&>()));
5200 template <
typename Err,
typename ExceptionMatcher>
5201 PolymorphicMatcher<internal::ExceptionMatcherImpl<Err>> Throws(
5202 const ExceptionMatcher& exception_matcher) {
5206 return MakePolymorphicMatcher(internal::ExceptionMatcherImpl<Err>(
5207 SafeMatcherCast<const Err&>(exception_matcher)));
5210 template <
typename Err,
typename MessageMatcher>
5211 PolymorphicMatcher<internal::ExceptionMatcherImpl<Err>> ThrowsMessage(
5212 MessageMatcher&& message_matcher) {
5213 static_assert(std::is_base_of<std::exception, Err>::value,
5214 "expected an std::exception-derived type");
5215 return Throws<Err>(internal::WithWhat(
5216 MatcherCast<std::string>(std::forward<MessageMatcher>(message_matcher))));
5225 #define ASSERT_THAT(value, matcher) ASSERT_PRED_FORMAT1(\
5226 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
5227 #define EXPECT_THAT(value, matcher) EXPECT_PRED_FORMAT1(\
5228 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
5231 #define MATCHER(name, description) \
5232 class name##Matcher \
5233 : public ::testing::internal::MatcherBaseImpl<name##Matcher> { \
5235 template <typename arg_type> \
5236 class gmock_Impl : public ::testing::MatcherInterface<const arg_type&> { \
5239 bool MatchAndExplain( \
5240 const arg_type& arg, \
5241 ::testing::MatchResultListener* result_listener) const override; \
5242 void DescribeTo(::std::ostream* gmock_os) const override { \
5243 *gmock_os << FormatDescription(false); \
5245 void DescribeNegationTo(::std::ostream* gmock_os) const override { \
5246 *gmock_os << FormatDescription(true); \
5250 ::std::string FormatDescription(bool negation) const { \
5251 ::std::string gmock_description = (description); \
5252 if (!gmock_description.empty()) { \
5253 return gmock_description; \
5255 return ::testing::internal::FormatMatcherDescription(negation, #name, \
5260 GTEST_ATTRIBUTE_UNUSED_ inline name##Matcher name() { return {}; } \
5261 template <typename arg_type> \
5262 bool name##Matcher::gmock_Impl<arg_type>::MatchAndExplain( \
5263 const arg_type& arg, \
5264 ::testing::MatchResultListener* result_listener GTEST_ATTRIBUTE_UNUSED_) \
5267 #define MATCHER_P(name, p0, description) \
5268 GMOCK_INTERNAL_MATCHER(name, name##MatcherP, description, (p0))
5269 #define MATCHER_P2(name, p0, p1, description) \
5270 GMOCK_INTERNAL_MATCHER(name, name##MatcherP2, description, (p0, p1))
5271 #define MATCHER_P3(name, p0, p1, p2, description) \
5272 GMOCK_INTERNAL_MATCHER(name, name##MatcherP3, description, (p0, p1, p2))
5273 #define MATCHER_P4(name, p0, p1, p2, p3, description) \
5274 GMOCK_INTERNAL_MATCHER(name, name##MatcherP4, description, (p0, p1, p2, p3))
5275 #define MATCHER_P5(name, p0, p1, p2, p3, p4, description) \
5276 GMOCK_INTERNAL_MATCHER(name, name##MatcherP5, description, \
5277 (p0, p1, p2, p3, p4))
5278 #define MATCHER_P6(name, p0, p1, p2, p3, p4, p5, description) \
5279 GMOCK_INTERNAL_MATCHER(name, name##MatcherP6, description, \
5280 (p0, p1, p2, p3, p4, p5))
5281 #define MATCHER_P7(name, p0, p1, p2, p3, p4, p5, p6, description) \
5282 GMOCK_INTERNAL_MATCHER(name, name##MatcherP7, description, \
5283 (p0, p1, p2, p3, p4, p5, p6))
5284 #define MATCHER_P8(name, p0, p1, p2, p3, p4, p5, p6, p7, description) \
5285 GMOCK_INTERNAL_MATCHER(name, name##MatcherP8, description, \
5286 (p0, p1, p2, p3, p4, p5, p6, p7))
5287 #define MATCHER_P9(name, p0, p1, p2, p3, p4, p5, p6, p7, p8, description) \
5288 GMOCK_INTERNAL_MATCHER(name, name##MatcherP9, description, \
5289 (p0, p1, p2, p3, p4, p5, p6, p7, p8))
5290 #define MATCHER_P10(name, p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, description) \
5291 GMOCK_INTERNAL_MATCHER(name, name##MatcherP10, description, \
5292 (p0, p1, p2, p3, p4, p5, p6, p7, p8, p9))
5294 #define GMOCK_INTERNAL_MATCHER(name, full_name, description, args) \
5295 template <GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args)> \
5296 class full_name : public ::testing::internal::MatcherBaseImpl< \
5297 full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>> { \
5299 using full_name::MatcherBaseImpl::MatcherBaseImpl; \
5300 template <typename arg_type> \
5301 class gmock_Impl : public ::testing::MatcherInterface<const arg_type&> { \
5303 explicit gmock_Impl(GMOCK_INTERNAL_MATCHER_FUNCTION_ARGS(args)) \
5304 : GMOCK_INTERNAL_MATCHER_FORWARD_ARGS(args) {} \
5305 bool MatchAndExplain( \
5306 const arg_type& arg, \
5307 ::testing::MatchResultListener* result_listener) const override; \
5308 void DescribeTo(::std::ostream* gmock_os) const override { \
5309 *gmock_os << FormatDescription(false); \
5311 void DescribeNegationTo(::std::ostream* gmock_os) const override { \
5312 *gmock_os << FormatDescription(true); \
5314 GMOCK_INTERNAL_MATCHER_MEMBERS(args) \
5317 ::std::string FormatDescription(bool negation) const { \
5318 ::std::string gmock_description = (description); \
5319 if (!gmock_description.empty()) { \
5320 return gmock_description; \
5322 return ::testing::internal::FormatMatcherDescription( \
5324 ::testing::internal::UniversalTersePrintTupleFieldsToStrings( \
5325 ::std::tuple<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>( \
5326 GMOCK_INTERNAL_MATCHER_MEMBERS_USAGE(args)))); \
5330 template <GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args)> \
5331 inline full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)> name( \
5332 GMOCK_INTERNAL_MATCHER_FUNCTION_ARGS(args)) { \
5333 return full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>( \
5334 GMOCK_INTERNAL_MATCHER_ARGS_USAGE(args)); \
5336 template <GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args)> \
5337 template <typename arg_type> \
5338 bool full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>::gmock_Impl< \
5339 arg_type>::MatchAndExplain(const arg_type& arg, \
5340 ::testing::MatchResultListener* \
5341 result_listener GTEST_ATTRIBUTE_UNUSED_) \
5344 #define GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args) \
5346 GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAM, , args))
5347 #define GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAM(i_unused, data_unused, arg) \
5348 , typename arg##_type
5350 #define GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args) \
5351 GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_TYPE_PARAM, , args))
5352 #define GMOCK_INTERNAL_MATCHER_TYPE_PARAM(i_unused, data_unused, arg) \
5355 #define GMOCK_INTERNAL_MATCHER_FUNCTION_ARGS(args) \
5356 GMOCK_PP_TAIL(dummy_first GMOCK_PP_FOR_EACH( \
5357 GMOCK_INTERNAL_MATCHER_FUNCTION_ARG, , args))
5358 #define GMOCK_INTERNAL_MATCHER_FUNCTION_ARG(i, data_unused, arg) \
5359 , arg##_type gmock_p##i
5361 #define GMOCK_INTERNAL_MATCHER_FORWARD_ARGS(args) \
5362 GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_FORWARD_ARG, , args))
5363 #define GMOCK_INTERNAL_MATCHER_FORWARD_ARG(i, data_unused, arg) \
5364 , arg(::std::forward<arg##_type>(gmock_p##i))
5366 #define GMOCK_INTERNAL_MATCHER_MEMBERS(args) \
5367 GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_MEMBER, , args)
5368 #define GMOCK_INTERNAL_MATCHER_MEMBER(i_unused, data_unused, arg) \
5369 const arg##_type arg;
5371 #define GMOCK_INTERNAL_MATCHER_MEMBERS_USAGE(args) \
5372 GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_MEMBER_USAGE, , args))
5373 #define GMOCK_INTERNAL_MATCHER_MEMBER_USAGE(i_unused, data_unused, arg) , arg
5375 #define GMOCK_INTERNAL_MATCHER_ARGS_USAGE(args) \
5376 GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_ARG_USAGE, , args))
5377 #define GMOCK_INTERNAL_MATCHER_ARG_USAGE(i, data_unused, arg_unused) \
5381 using namespace no_adl;
5390 #include "gmock/internal/custom/gmock-matchers.h"
Definition: cmake-build-debug/googletest-src/googlemock/include/gmock/internal/gmock-internal-utils.h:52
RawContainer type
Definition: cmake-build-debug/googletest-src/googlemock/include/gmock/internal/gmock-internal-utils.h:327
static bool CaseInsensitiveWideCStringEquals(const wchar_t *lhs, const wchar_t *rhs)
static bool CaseInsensitiveCStringEquals(const char *lhs, const char *rhs)
static void Print(const T &value, ::std::ostream *os)
Definition: cmake-build-debug/googletest-src/googletest/include/gtest/gtest-printers.h:665
#define GMOCK_KIND_OF_(type)
Definition: cmake-build-debug/googletest-src/googlemock/include/gmock/internal/gmock-internal-utils.h:140
#define GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement)
Definition: cmake-build-debug/googletest-src/googletest/include/gtest/internal/gtest-internal.h:1362
#define GTEST_REMOVE_REFERENCE_AND_CONST_(T)
Definition: cmake-build-debug/googletest-src/googletest/include/gtest/internal/gtest-internal.h:888
#define GTEST_LOG_(severity)
Definition: cmake-build-debug/googletest-src/googletest/include/gtest/internal/gtest-port.h:980
#define GTEST_API_
Definition: cmake-build-debug/googletest-src/googletest/include/gtest/internal/gtest-port.h:775
#define GTEST_DISABLE_MSC_WARNINGS_POP_()
Definition: cmake-build-debug/googletest-src/googletest/include/gtest/internal/gtest-port.h:324
#define GTEST_CHECK_(condition)
Definition: cmake-build-debug/googletest-src/googletest/include/gtest/internal/gtest-port.h:1004
#define GTEST_COMPILE_ASSERT_(expr, msg)
Definition: cmake-build-debug/googletest-src/googletest/include/gtest/internal/gtest-port.h:875
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
Definition: cmake-build-debug/googletest-src/googletest/include/gtest/internal/gtest-port.h:693
#define GMOCK_MAYBE_5046_
<< DiffStrings(str, arg);
Definition: cmake-build-release/googletest-src/googlemock/include/gmock/gmock-matchers.h:280
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 GMOCK_MAYBE_5046_) namespace testing
Definition: cmake-build-release/googletest-src/googlemock/include/gmock/gmock-matchers.h:283
void UniversalPrint(const T &value, ::std::ostream *os)
Definition: cmake-build-debug/googletest-src/googletest/include/gtest/gtest-printers.h:902
::std::vector< ::std::string > Strings
Definition: cmake-build-debug/googletest-src/googletest/include/gtest/gtest-printers.h:909
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 >()))
Definition: cmake-build-debug/googletest-src/googlemock/include/gmock/internal/gmock-internal-utils.h:412
std::string GetTypeName()
Definition: cmake-build-debug/googletest-src/googletest/include/gtest/internal/gtest-type-util.h:93
Iter ArrayAwareFind(Iter begin, Iter end, const Element &elem)
Definition: cmake-build-debug/googletest-src/googletest/include/gtest/internal/gtest-internal.h:1043
@ kOther
Definition: cmake-build-debug/googletest-src/googlemock/include/gmock/internal/gmock-internal-utils.h:101
typename MakeIndexSequenceImpl< N >::type MakeIndexSequence
Definition: cmake-build-debug/googletest-src/googletest/include/gtest/internal/gtest-internal.h:1185
const Pointer::element_type * GetRawPointer(const Pointer &p)
Definition: cmake-build-debug/googletest-src/googlemock/include/gmock/internal/gmock-internal-utils.h:78
Definition: cmake-build-debug/googletest-src/googlemock/include/gmock/gmock-actions.h:154
::std::string PrintToString(const T &value)
Definition: cmake-build-debug/googletest-src/googletest/include/gtest/gtest-printers.h:942
std::decay< FunctionImpl >::type Invoke(FunctionImpl &&function_impl)
Definition: cmake-build-debug/googletest-src/googlemock/include/gmock/gmock-actions.h:1345