42 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_ 43 #define GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_ 47 #include <initializer_list> 54 #include <type_traits> 62 #if defined(_MSC_VER) && _MSC_VER >= 1915 63 #define GMOCK_MAYBE_5046_ 5046 65 #define GMOCK_MAYBE_5046_ 88 class StringMatchResultListener :
public MatchResultListener {
90 StringMatchResultListener() : MatchResultListener(&ss_) {}
93 std::string str()
const {
return ss_.str(); }
96 void Clear() { ss_.str(
""); }
99 ::std::stringstream ss_;
118 template <
typename T,
typename M>
119 class MatcherCastImpl {
121 static Matcher<T> Cast(
const M& polymorphic_matcher_or_value) {
136 polymorphic_matcher_or_value,
138 std::is_convertible<M, Matcher<T> >::
value>(),
144 template <
bool Ignore>
145 static Matcher<T> CastImpl(
const M& polymorphic_matcher_or_value,
146 BooleanConstant<true> ,
147 BooleanConstant<Ignore>) {
156 return polymorphic_matcher_or_value;
162 static Matcher<T> CastImpl(
163 const M&
value, BooleanConstant<false> ,
164 BooleanConstant<true> ) {
165 return Matcher<T>(ImplicitCast_<T>(
value));
178 static Matcher<T> CastImpl(
179 const M& value, BooleanConstant<false> ,
180 BooleanConstant<false> );
186 template <
typename T,
typename U>
187 class MatcherCastImpl<T, Matcher<U> > {
189 static Matcher<T> Cast(
const Matcher<U>& source_matcher) {
190 return Matcher<T>(
new Impl(source_matcher));
194 class Impl :
public MatcherInterface<T> {
196 explicit Impl(
const Matcher<U>& source_matcher)
197 : source_matcher_(source_matcher) {}
200 bool MatchAndExplain(T
x, MatchResultListener* listener)
const override {
201 using FromType =
typename std::remove_cv<
typename std::remove_pointer<
203 using ToType =
typename std::remove_cv<
typename std::remove_pointer<
213 "Can't implicitly convert from <base> to <derived>");
215 return source_matcher_.MatchAndExplain(static_cast<U>(x), listener);
218 void DescribeTo(::std::ostream* os)
const override {
219 source_matcher_.DescribeTo(os);
222 void DescribeNegationTo(::std::ostream* os)
const override {
223 source_matcher_.DescribeNegationTo(os);
227 const Matcher<U> source_matcher_;
235 template <
typename T>
236 class MatcherCastImpl<T, Matcher<T> > {
238 static Matcher<T> Cast(
const Matcher<T>& matcher) {
return matcher; }
247 template <
typename T,
typename M>
248 inline Matcher<T> MatcherCast(
const M& matcher) {
249 return internal::MatcherCastImpl<T, M>::Cast(matcher);
256 template <
typename T>
257 class SafeMatcherCastImpl {
261 template <
typename M>
262 static inline Matcher<T> Cast(
const M& polymorphic_matcher_or_value) {
263 return internal::MatcherCastImpl<T, M>::Cast(polymorphic_matcher_or_value);
275 template <
typename U>
276 static inline Matcher<T> Cast(
const Matcher<U>& matcher) {
279 "T must be implicitly convertible to U");
284 cannot_convert_non_reference_arg_to_reference);
292 kTIsOther || kUIsOther ||
294 conversion_of_arithmetic_types_must_be_lossless);
295 return MatcherCast<T>(matcher);
299 template <
typename T,
typename M>
300 inline Matcher<T> SafeMatcherCast(
const M& polymorphic_matcher) {
301 return SafeMatcherCastImpl<T>::Cast(polymorphic_matcher);
305 template <
typename T>
313 inline void PrintIfNotEmpty(
const std::string& explanation,
314 ::std::ostream* os) {
315 if (explanation !=
"" && os !=
nullptr) {
316 *os <<
", " << explanation;
323 inline bool IsReadableTypeName(
const std::string& type_name) {
326 return (type_name.length() <= 20 ||
327 type_name.find_first_of(
"<(") == std::string::npos);
335 template <
typename Value,
typename T>
336 bool MatchPrintAndExplain(Value&
value,
const Matcher<T>& matcher,
337 MatchResultListener* listener) {
338 if (!listener->IsInterested()) {
341 return matcher.Matches(value);
344 StringMatchResultListener inner_listener;
345 const bool match = matcher.MatchAndExplain(value, &inner_listener);
349 const std::string& type_name = GetTypeName<Value>();
350 if (IsReadableTypeName(type_name))
351 *listener->stream() <<
" (of type " << type_name <<
")";
353 PrintIfNotEmpty(inner_listener.str(), listener->stream());
366 template <
typename MatcherTuple,
typename ValueTuple>
367 static bool Matches(
const MatcherTuple& matcher_tuple,
368 const ValueTuple& value_tuple) {
369 return TuplePrefix<N - 1>::Matches(matcher_tuple, value_tuple) &&
370 std::get<N - 1>(matcher_tuple).Matches(std::get<N - 1>(value_tuple));
377 template <
typename MatcherTuple,
typename ValueTuple>
378 static void ExplainMatchFailuresTo(
const MatcherTuple& matchers,
379 const ValueTuple& values,
380 ::std::ostream* os) {
382 TuplePrefix<N - 1>::ExplainMatchFailuresTo(matchers, values, os);
386 typename std::tuple_element<N - 1, MatcherTuple>
::type matcher =
387 std::get<N - 1>(matchers);
388 typedef typename std::tuple_element<N - 1, ValueTuple>
::type Value;
389 const Value& value = std::get<N - 1>(values);
390 StringMatchResultListener listener;
391 if (!matcher.MatchAndExplain(value, &listener)) {
392 *os <<
" Expected arg #" << N - 1 <<
": ";
393 std::get<N - 1>(matchers).DescribeTo(os);
394 *os <<
"\n Actual: ";
401 PrintIfNotEmpty(listener.str(), os);
409 class TuplePrefix<0> {
411 template <
typename MatcherTuple,
typename ValueTuple>
412 static bool Matches(
const MatcherTuple& ,
413 const ValueTuple& ) {
417 template <
typename MatcherTuple,
typename ValueTuple>
418 static void ExplainMatchFailuresTo(
const MatcherTuple& ,
428 template <
typename MatcherTuple,
typename ValueTuple>
429 bool TupleMatches(
const MatcherTuple& matcher_tuple,
430 const ValueTuple& value_tuple) {
435 matcher_and_value_have_different_numbers_of_fields);
442 template <
typename MatcherTuple,
typename ValueTuple>
443 void ExplainMatchFailureTupleTo(
const MatcherTuple& matchers,
444 const ValueTuple& values,
445 ::std::ostream* os) {
447 matchers, values, os);
454 template <
typename Tuple,
typename Func,
typename OutIter>
455 class TransformTupleValuesHelper {
457 typedef ::std::tuple_size<Tuple> TupleSize;
462 static OutIter
Run(Func f,
const Tuple&
t, OutIter out) {
463 return IterateOverTuple<Tuple, TupleSize::value>()(f, t, out);
467 template <
typename Tup,
size_t kRemainingSize>
468 struct IterateOverTuple {
469 OutIter operator() (Func f,
const Tup& t, OutIter out)
const {
470 *out++ = f(::std::get<TupleSize::value - kRemainingSize>(t));
471 return IterateOverTuple<Tup, kRemainingSize - 1>()(f, t, out);
474 template <
typename Tup>
475 struct IterateOverTuple<Tup, 0> {
476 OutIter operator() (Func ,
const Tup& , OutIter out)
const {
485 template <
typename Tuple,
typename Func,
typename OutIter>
486 OutIter TransformTupleValues(Func f,
const Tuple&
t, OutIter out) {
491 template <
typename T>
492 class AnyMatcherImpl :
public MatcherInterface<const T&> {
494 bool MatchAndExplain(
const T& ,
495 MatchResultListener* )
const override {
498 void DescribeTo(::std::ostream* os)
const override { *os <<
"is anything"; }
499 void DescribeNegationTo(::std::ostream* os)
const override {
503 *os <<
"never matches";
511 class AnythingMatcher {
513 template <
typename T>
514 operator Matcher<T>()
const {
return A<T>(); }
519 class IsNullMatcher {
521 template <
typename Po
inter>
522 bool MatchAndExplain(
const Pointer&
p,
523 MatchResultListener* )
const {
527 void DescribeTo(::std::ostream* os)
const { *os <<
"is NULL"; }
528 void DescribeNegationTo(::std::ostream* os)
const {
535 class NotNullMatcher {
537 template <
typename Po
inter>
538 bool MatchAndExplain(
const Pointer&
p,
539 MatchResultListener* )
const {
543 void DescribeTo(::std::ostream* os)
const { *os <<
"isn't NULL"; }
544 void DescribeNegationTo(::std::ostream* os)
const {
562 template <
typename T>
565 template <
typename T>
566 class RefMatcher<T&> {
576 explicit RefMatcher(T&
x) : object_(x) {}
578 template <
typename Super>
579 operator Matcher<Super&>()
const {
585 return MakeMatcher(
new Impl<Super>(object_));
589 template <
typename Super>
590 class Impl :
public MatcherInterface<Super&> {
592 explicit Impl(Super& x) : object_(x) {}
596 bool MatchAndExplain(Super& x,
597 MatchResultListener* listener)
const override {
598 *listener <<
"which is located @" <<
static_cast<const void*
>(&
x);
599 return &x == &object_;
602 void DescribeTo(::std::ostream* os)
const override {
603 *os <<
"references the variable ";
607 void DescribeNegationTo(::std::ostream* os)
const override {
608 *os <<
"does not reference the variable ";
613 const Super& object_;
624 inline bool CaseInsensitiveCStringEquals(
const char* lhs,
const char* rhs) {
625 return String::CaseInsensitiveCStringEquals(lhs, rhs);
628 inline bool CaseInsensitiveCStringEquals(
const wchar_t* lhs,
629 const wchar_t* rhs) {
630 return String::CaseInsensitiveWideCStringEquals(lhs, rhs);
635 template <
typename StringType>
636 bool CaseInsensitiveStringEquals(
const StringType& s1,
637 const StringType& s2) {
639 if (!CaseInsensitiveCStringEquals(s1.c_str(), s2.c_str())) {
644 const typename StringType::value_type nul = 0;
645 const size_t i1 = s1.find(nul), i2 = s2.find(nul);
648 if (i1 == StringType::npos || i2 == StringType::npos) {
653 return CaseInsensitiveStringEquals(s1.substr(i1 + 1), s2.substr(i2 + 1));
659 template <
typename StringType>
660 class StrEqualityMatcher {
662 StrEqualityMatcher(
const StringType& str,
bool expect_eq,
664 : string_(str), expect_eq_(expect_eq), case_sensitive_(case_sensitive) {}
667 bool MatchAndExplain(
const absl::string_view& s,
668 MatchResultListener* listener)
const {
671 const StringType& str = std::string(s);
672 return MatchAndExplain(str, listener);
674 #endif // GTEST_HAS_ABSL 681 template <
typename CharType>
682 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
686 return MatchAndExplain(StringType(s), listener);
693 template <
typename MatcheeStringType>
694 bool MatchAndExplain(
const MatcheeStringType& s,
695 MatchResultListener* )
const {
696 const StringType& s2(s);
697 const bool eq = case_sensitive_ ? s2 == string_ :
698 CaseInsensitiveStringEquals(s2, string_);
699 return expect_eq_ == eq;
702 void DescribeTo(::std::ostream* os)
const {
703 DescribeToHelper(expect_eq_, os);
706 void DescribeNegationTo(::std::ostream* os)
const {
707 DescribeToHelper(!expect_eq_, os);
711 void DescribeToHelper(
bool expect_eq, ::std::ostream* os)
const {
712 *os << (expect_eq ?
"is " :
"isn't ");
714 if (!case_sensitive_) {
715 *os <<
"(ignoring case) ";
720 const StringType string_;
721 const bool expect_eq_;
722 const bool case_sensitive_;
730 template <
typename StringType>
731 class HasSubstrMatcher {
733 explicit HasSubstrMatcher(
const StringType& substring)
734 : substring_(substring) {}
737 bool MatchAndExplain(
const absl::string_view& s,
738 MatchResultListener* listener)
const {
741 const StringType& str = std::string(s);
742 return MatchAndExplain(str, listener);
744 #endif // GTEST_HAS_ABSL 751 template <
typename CharType>
752 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
753 return s !=
nullptr && MatchAndExplain(StringType(s), listener);
760 template <
typename MatcheeStringType>
761 bool MatchAndExplain(
const MatcheeStringType& s,
762 MatchResultListener* )
const {
763 const StringType& s2(s);
764 return s2.find(substring_) != StringType::npos;
768 void DescribeTo(::std::ostream* os)
const {
769 *os <<
"has substring ";
773 void DescribeNegationTo(::std::ostream* os)
const {
774 *os <<
"has no substring ";
779 const StringType substring_;
787 template <
typename StringType>
788 class StartsWithMatcher {
790 explicit StartsWithMatcher(
const StringType& prefix) : prefix_(prefix) {
794 bool MatchAndExplain(
const absl::string_view& s,
795 MatchResultListener* listener)
const {
798 const StringType& str = std::string(s);
799 return MatchAndExplain(str, listener);
801 #endif // GTEST_HAS_ABSL 808 template <
typename CharType>
809 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
810 return s !=
nullptr && MatchAndExplain(StringType(s), listener);
817 template <
typename MatcheeStringType>
818 bool MatchAndExplain(
const MatcheeStringType& s,
819 MatchResultListener* )
const {
820 const StringType& s2(s);
821 return s2.length() >= prefix_.length() &&
822 s2.substr(0, prefix_.length()) == prefix_;
825 void DescribeTo(::std::ostream* os)
const {
826 *os <<
"starts with ";
830 void DescribeNegationTo(::std::ostream* os)
const {
831 *os <<
"doesn't start with ";
836 const StringType prefix_;
844 template <
typename StringType>
845 class EndsWithMatcher {
847 explicit EndsWithMatcher(
const StringType& suffix) : suffix_(suffix) {}
850 bool MatchAndExplain(
const absl::string_view& s,
851 MatchResultListener* listener)
const {
854 const StringType& str = std::string(s);
855 return MatchAndExplain(str, listener);
857 #endif // GTEST_HAS_ABSL 864 template <
typename CharType>
865 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
866 return s !=
nullptr && MatchAndExplain(StringType(s), listener);
873 template <
typename MatcheeStringType>
874 bool MatchAndExplain(
const MatcheeStringType& s,
875 MatchResultListener* )
const {
876 const StringType& s2(s);
877 return s2.length() >= suffix_.length() &&
878 s2.substr(s2.length() - suffix_.length()) == suffix_;
881 void DescribeTo(::std::ostream* os)
const {
886 void DescribeNegationTo(::std::ostream* os)
const {
887 *os <<
"doesn't end with ";
892 const StringType suffix_;
905 template <
typename D,
typename Op>
906 class PairMatchBase {
908 template <
typename T1,
typename T2>
909 operator Matcher<::std::tuple<T1, T2>>()
const {
910 return Matcher<::std::tuple<T1, T2>>(
new Impl<const ::std::tuple<T1, T2>&>);
912 template <
typename T1,
typename T2>
913 operator Matcher<const ::std::tuple<T1, T2>&>()
const {
914 return MakeMatcher(
new Impl<const ::std::tuple<T1, T2>&>);
918 static ::std::ostream& GetDesc(::std::ostream& os) {
919 return os << D::Desc();
922 template <
typename Tuple>
923 class Impl :
public MatcherInterface<Tuple> {
925 bool MatchAndExplain(Tuple args,
926 MatchResultListener* )
const override {
927 return Op()(::std::get<0>(args), ::std::get<1>(args));
929 void DescribeTo(::std::ostream* os)
const override {
930 *os <<
"are " << GetDesc;
932 void DescribeNegationTo(::std::ostream* os)
const override {
933 *os <<
"aren't " << GetDesc;
938 class Eq2Matcher :
public PairMatchBase<Eq2Matcher, AnyEq> {
940 static const char* Desc() {
return "an equal pair"; }
942 class Ne2Matcher :
public PairMatchBase<Ne2Matcher, AnyNe> {
944 static const char* Desc() {
return "an unequal pair"; }
946 class Lt2Matcher :
public PairMatchBase<Lt2Matcher, AnyLt> {
948 static const char* Desc() {
return "a pair where the first < the second"; }
950 class Gt2Matcher :
public PairMatchBase<Gt2Matcher, AnyGt> {
952 static const char* Desc() {
return "a pair where the first > the second"; }
954 class Le2Matcher :
public PairMatchBase<Le2Matcher, AnyLe> {
956 static const char* Desc() {
return "a pair where the first <= the second"; }
958 class Ge2Matcher :
public PairMatchBase<Ge2Matcher, AnyGe> {
960 static const char* Desc() {
return "a pair where the first >= the second"; }
967 template <
typename T>
968 class NotMatcherImpl :
public MatcherInterface<const T&> {
970 explicit NotMatcherImpl(
const Matcher<T>& matcher)
971 : matcher_(matcher) {}
973 bool MatchAndExplain(
const T&
x,
974 MatchResultListener* listener)
const override {
975 return !matcher_.MatchAndExplain(x, listener);
978 void DescribeTo(::std::ostream* os)
const override {
979 matcher_.DescribeNegationTo(os);
982 void DescribeNegationTo(::std::ostream* os)
const override {
983 matcher_.DescribeTo(os);
987 const Matcher<T> matcher_;
994 template <
typename InnerMatcher>
997 explicit NotMatcher(InnerMatcher matcher) : matcher_(matcher) {}
1001 template <
typename T>
1002 operator Matcher<T>()
const {
1003 return Matcher<T>(
new NotMatcherImpl<T>(SafeMatcherCast<T>(matcher_)));
1007 InnerMatcher matcher_;
1016 template <
typename T>
1017 class AllOfMatcherImpl :
public MatcherInterface<const T&> {
1019 explicit AllOfMatcherImpl(std::vector<Matcher<T> > matchers)
1020 : matchers_(std::move(matchers)) {}
1022 void DescribeTo(::std::ostream* os)
const override {
1024 for (
size_t i = 0;
i < matchers_.size(); ++
i) {
1025 if (
i != 0) *os <<
") and (";
1026 matchers_[
i].DescribeTo(os);
1031 void DescribeNegationTo(::std::ostream* os)
const override {
1033 for (
size_t i = 0;
i < matchers_.size(); ++
i) {
1034 if (
i != 0) *os <<
") or (";
1035 matchers_[
i].DescribeNegationTo(os);
1040 bool MatchAndExplain(
const T&
x,
1041 MatchResultListener* listener)
const override {
1044 std::string all_match_result;
1046 for (
size_t i = 0;
i < matchers_.size(); ++
i) {
1047 StringMatchResultListener slistener;
1048 if (matchers_[
i].MatchAndExplain(x, &slistener)) {
1049 if (all_match_result.empty()) {
1050 all_match_result = slistener.str();
1052 std::string result = slistener.str();
1053 if (!result.empty()) {
1054 all_match_result +=
", and ";
1055 all_match_result += result;
1059 *listener << slistener.str();
1065 *listener << all_match_result;
1070 const std::vector<Matcher<T> > matchers_;
1079 template <
template <
typename T>
class CombiningMatcher,
typename... Args>
1080 class VariadicMatcher {
1082 VariadicMatcher(
const Args&... matchers)
1083 : matchers_(matchers...) {
1084 static_assert(
sizeof...(Args) > 0,
"Must have at least one matcher.");
1090 template <
typename T>
1091 operator Matcher<T>()
const {
1092 std::vector<Matcher<T> > values;
1093 CreateVariadicMatcher<T>(&values, std::integral_constant<size_t, 0>());
1094 return Matcher<T>(
new CombiningMatcher<T>(std::move(values)));
1098 template <
typename T,
size_t I>
1099 void CreateVariadicMatcher(std::vector<Matcher<T> >* values,
1100 std::integral_constant<size_t, I>)
const {
1101 values->push_back(SafeMatcherCast<T>(std::get<I>(matchers_)));
1102 CreateVariadicMatcher<T>(values, std::integral_constant<size_t, I + 1>());
1105 template <
typename T>
1106 void CreateVariadicMatcher(
1107 std::vector<Matcher<T> >*,
1108 std::integral_constant<
size_t,
sizeof...(Args)>)
const {}
1110 std::tuple<Args...> matchers_;
1115 template <
typename... Args>
1116 using AllOfMatcher = VariadicMatcher<AllOfMatcherImpl, Args...>;
1122 template <
typename T>
1123 class AnyOfMatcherImpl :
public MatcherInterface<const T&> {
1125 explicit AnyOfMatcherImpl(std::vector<Matcher<T> > matchers)
1126 : matchers_(std::move(matchers)) {}
1128 void DescribeTo(::std::ostream* os)
const override {
1130 for (
size_t i = 0;
i < matchers_.size(); ++
i) {
1131 if (
i != 0) *os <<
") or (";
1132 matchers_[
i].DescribeTo(os);
1137 void DescribeNegationTo(::std::ostream* os)
const override {
1139 for (
size_t i = 0;
i < matchers_.size(); ++
i) {
1140 if (
i != 0) *os <<
") and (";
1141 matchers_[
i].DescribeNegationTo(os);
1146 bool MatchAndExplain(
const T&
x,
1147 MatchResultListener* listener)
const override {
1148 std::string no_match_result;
1152 for (
size_t i = 0;
i < matchers_.size(); ++
i) {
1153 StringMatchResultListener slistener;
1154 if (matchers_[
i].MatchAndExplain(x, &slistener)) {
1155 *listener << slistener.str();
1158 if (no_match_result.empty()) {
1159 no_match_result = slistener.str();
1161 std::string result = slistener.str();
1162 if (!result.empty()) {
1163 no_match_result +=
", and ";
1164 no_match_result += result;
1171 *listener << no_match_result;
1176 const std::vector<Matcher<T> > matchers_;
1182 template <
typename... Args>
1183 using AnyOfMatcher = VariadicMatcher<AnyOfMatcherImpl, Args...>;
1186 template <
template <
class>
class MatcherImpl,
typename T>
1187 class SomeOfArrayMatcher {
1191 template <
typename Iter>
1192 SomeOfArrayMatcher(
Iter first,
Iter last) : matchers_(first, last) {}
1194 template <
typename U>
1195 operator Matcher<U>()
const {
1197 std::vector<Matcher<RawU>> matchers;
1198 for (
const auto& matcher : matchers_) {
1199 matchers.push_back(MatcherCast<RawU>(matcher));
1201 return Matcher<U>(
new MatcherImpl<RawU>(std::move(matchers)));
1205 const ::std::vector<T> matchers_;
1210 template <
typename T>
1211 using AllOfArrayMatcher = SomeOfArrayMatcher<AllOfMatcherImpl, T>;
1213 template <
typename T>
1214 using AnyOfArrayMatcher = SomeOfArrayMatcher<AnyOfMatcherImpl, T>;
1218 template <
typename Predicate>
1219 class TrulyMatcher {
1221 explicit TrulyMatcher(Predicate pred) : predicate_(pred) {}
1227 template <
typename T>
1228 bool MatchAndExplain(T&
x,
1229 MatchResultListener* )
const {
1241 void DescribeTo(::std::ostream* os)
const {
1242 *os <<
"satisfies the given predicate";
1245 void DescribeNegationTo(::std::ostream* os)
const {
1246 *os <<
"doesn't satisfy the given predicate";
1250 Predicate predicate_;
1257 template <
typename M>
1258 class MatcherAsPredicate {
1260 explicit MatcherAsPredicate(M matcher) : matcher_(matcher) {}
1268 template <
typename T>
1269 bool operator()(
const T&
x)
const {
1284 return MatcherCast<const T&>(matcher_).Matches(x);
1295 template <
typename M>
1296 class PredicateFormatterFromMatcher {
1298 explicit PredicateFormatterFromMatcher(M m) : matcher_(std::move(m)) {}
1303 template <
typename T>
1304 AssertionResult operator()(
const char* value_text,
const T&
x)
const {
1316 const Matcher<const T&> matcher = SafeMatcherCast<const T&>(matcher_);
1320 if (matcher.Matches(x)) {
1324 ::std::stringstream ss;
1325 ss <<
"Value of: " << value_text <<
"\n" 1327 matcher.DescribeTo(&ss);
1330 StringMatchResultListener listener;
1331 if (MatchPrintAndExplain(x, matcher, &listener)) {
1332 ss <<
"\n The matcher failed on the initial attempt; but passed when " 1333 "rerun to generate the explanation.";
1335 ss <<
"\n Actual: " << listener.str();
1349 template <
typename M>
1350 inline PredicateFormatterFromMatcher<M>
1351 MakePredicateFormatterFromMatcher(M matcher) {
1352 return PredicateFormatterFromMatcher<M>(std::move(matcher));
1359 template <
typename FloatType>
1360 class FloatingEqMatcher {
1368 FloatingEqMatcher(FloatType expected,
bool nan_eq_nan) :
1369 expected_(expected), nan_eq_nan_(nan_eq_nan), max_abs_error_(-1) {
1375 FloatingEqMatcher(FloatType expected,
bool nan_eq_nan,
1376 FloatType max_abs_error)
1377 : expected_(expected),
1378 nan_eq_nan_(nan_eq_nan),
1379 max_abs_error_(max_abs_error) {
1381 <<
", where max_abs_error is" << max_abs_error;
1385 template <
typename T>
1386 class Impl :
public MatcherInterface<T> {
1388 Impl(FloatType expected,
bool nan_eq_nan, FloatType max_abs_error)
1389 : expected_(expected),
1390 nan_eq_nan_(nan_eq_nan),
1391 max_abs_error_(max_abs_error) {}
1393 bool MatchAndExplain(T value,
1394 MatchResultListener* listener)
const override {
1395 const FloatingPoint<FloatType> actual(value), expected(expected_);
1398 if (actual.is_nan() || expected.is_nan()) {
1399 if (actual.is_nan() && expected.is_nan()) {
1405 if (HasMaxAbsError()) {
1410 if (value == expected_) {
1414 const FloatType diff = value - expected_;
1415 if (fabs(diff) <= max_abs_error_) {
1419 if (listener->IsInterested()) {
1420 *listener <<
"which is " << diff <<
" from " << expected_;
1424 return actual.AlmostEquals(expected);
1428 void DescribeTo(::std::ostream* os)
const override {
1432 const ::std::streamsize old_precision = os->precision(
1433 ::std::numeric_limits<FloatType>::digits10 + 2);
1434 if (FloatingPoint<FloatType>(expected_).is_nan()) {
1438 *os <<
"never matches";
1441 *os <<
"is approximately " << expected_;
1442 if (HasMaxAbsError()) {
1443 *os <<
" (absolute error <= " << max_abs_error_ <<
")";
1446 os->precision(old_precision);
1449 void DescribeNegationTo(::std::ostream* os)
const override {
1451 const ::std::streamsize old_precision = os->precision(
1452 ::std::numeric_limits<FloatType>::digits10 + 2);
1453 if (FloatingPoint<FloatType>(expected_).is_nan()) {
1457 *os <<
"is anything";
1460 *os <<
"isn't approximately " << expected_;
1461 if (HasMaxAbsError()) {
1462 *os <<
" (absolute error > " << max_abs_error_ <<
")";
1466 os->precision(old_precision);
1470 bool HasMaxAbsError()
const {
1471 return max_abs_error_ >= 0;
1474 const FloatType expected_;
1475 const bool nan_eq_nan_;
1477 const FloatType max_abs_error_;
1488 operator Matcher<FloatType>()
const {
1490 new Impl<FloatType>(expected_, nan_eq_nan_, max_abs_error_));
1493 operator Matcher<const FloatType&>()
const {
1495 new Impl<const FloatType&>(expected_, nan_eq_nan_, max_abs_error_));
1498 operator Matcher<FloatType&>()
const {
1500 new Impl<FloatType&>(expected_, nan_eq_nan_, max_abs_error_));
1504 const FloatType expected_;
1505 const bool nan_eq_nan_;
1507 const FloatType max_abs_error_;
1517 template <
typename FloatType>
1518 class FloatingEq2Matcher {
1520 FloatingEq2Matcher() { Init(-1,
false); }
1522 explicit FloatingEq2Matcher(
bool nan_eq_nan) { Init(-1, nan_eq_nan); }
1524 explicit FloatingEq2Matcher(FloatType max_abs_error) {
1525 Init(max_abs_error,
false);
1528 FloatingEq2Matcher(FloatType max_abs_error,
bool nan_eq_nan) {
1529 Init(max_abs_error, nan_eq_nan);
1532 template <
typename T1,
typename T2>
1533 operator Matcher<::std::tuple<T1, T2>>()
const {
1535 new Impl<::std::tuple<T1, T2>>(max_abs_error_, nan_eq_nan_));
1537 template <
typename T1,
typename T2>
1538 operator Matcher<const ::std::tuple<T1, T2>&>()
const {
1540 new Impl<const ::std::tuple<T1, T2>&>(max_abs_error_, nan_eq_nan_));
1544 static ::std::ostream& GetDesc(::std::ostream& os) {
1545 return os <<
"an almost-equal pair";
1548 template <
typename Tuple>
1549 class Impl :
public MatcherInterface<Tuple> {
1551 Impl(FloatType max_abs_error,
bool nan_eq_nan) :
1552 max_abs_error_(max_abs_error),
1553 nan_eq_nan_(nan_eq_nan) {}
1555 bool MatchAndExplain(Tuple args,
1556 MatchResultListener* listener)
const override {
1557 if (max_abs_error_ == -1) {
1558 FloatingEqMatcher<FloatType> fm(::std::get<0>(args), nan_eq_nan_);
1559 return static_cast<Matcher<FloatType>
>(fm).MatchAndExplain(
1560 ::std::get<1>(args), listener);
1562 FloatingEqMatcher<FloatType> fm(::std::get<0>(args), nan_eq_nan_,
1564 return static_cast<Matcher<FloatType>
>(fm).MatchAndExplain(
1565 ::std::get<1>(args), listener);
1568 void DescribeTo(::std::ostream* os)
const override {
1569 *os <<
"are " << GetDesc;
1571 void DescribeNegationTo(::std::ostream* os)
const override {
1572 *os <<
"aren't " << GetDesc;
1576 FloatType max_abs_error_;
1577 const bool nan_eq_nan_;
1580 void Init(FloatType max_abs_error_val,
bool nan_eq_nan_val) {
1581 max_abs_error_ = max_abs_error_val;
1582 nan_eq_nan_ = nan_eq_nan_val;
1584 FloatType max_abs_error_;
1590 template <
typename InnerMatcher>
1591 class PointeeMatcher {
1593 explicit PointeeMatcher(
const InnerMatcher& matcher) : matcher_(matcher) {}
1603 template <
typename Po
inter>
1604 operator Matcher<Pointer>()
const {
1605 return Matcher<Pointer>(
new Impl<const Pointer&>(matcher_));
1610 template <
typename Po
inter>
1611 class Impl :
public MatcherInterface<Pointer> {
1616 explicit Impl(
const InnerMatcher& matcher)
1617 : matcher_(MatcherCast<const Pointee&>(matcher)) {}
1619 void DescribeTo(::std::ostream* os)
const override {
1620 *os <<
"points to a value that ";
1621 matcher_.DescribeTo(os);
1624 void DescribeNegationTo(::std::ostream* os)
const override {
1625 *os <<
"does not point to a value that ";
1626 matcher_.DescribeTo(os);
1629 bool MatchAndExplain(Pointer pointer,
1630 MatchResultListener* listener)
const override {
1633 *listener <<
"which points to ";
1634 return MatchPrintAndExplain(*pointer, matcher_, listener);
1638 const Matcher<const Pointee&> matcher_;
1643 const InnerMatcher matcher_;
1655 template <
typename To>
1656 class WhenDynamicCastToMatcherBase {
1658 explicit WhenDynamicCastToMatcherBase(
const Matcher<To>& matcher)
1659 : matcher_(matcher) {}
1661 void DescribeTo(::std::ostream* os)
const {
1662 GetCastTypeDescription(os);
1663 matcher_.DescribeTo(os);
1666 void DescribeNegationTo(::std::ostream* os)
const {
1667 GetCastTypeDescription(os);
1668 matcher_.DescribeNegationTo(os);
1672 const Matcher<To> matcher_;
1674 static std::string GetToName() {
1675 return GetTypeName<To>();
1679 static void GetCastTypeDescription(::std::ostream* os) {
1680 *os <<
"when dynamic_cast to " << GetToName() <<
", ";
1688 template <
typename To>
1689 class WhenDynamicCastToMatcher :
public WhenDynamicCastToMatcherBase<To> {
1691 explicit WhenDynamicCastToMatcher(
const Matcher<To>& matcher)
1692 : WhenDynamicCastToMatcherBase<To>(matcher) {}
1694 template <
typename From>
1695 bool MatchAndExplain(From from, MatchResultListener* listener)
const {
1696 To to =
dynamic_cast<To
>(from);
1697 return MatchPrintAndExplain(to, this->matcher_, listener);
1703 template <
typename To>
1704 class WhenDynamicCastToMatcher<To&> :
public WhenDynamicCastToMatcherBase<To&> {
1706 explicit WhenDynamicCastToMatcher(
const Matcher<To&>& matcher)
1707 : WhenDynamicCastToMatcherBase<To&>(matcher) {}
1709 template <
typename From>
1710 bool MatchAndExplain(From& from, MatchResultListener* listener)
const {
1712 To* to =
dynamic_cast<To*
>(&from);
1713 if (to ==
nullptr) {
1714 *listener <<
"which cannot be dynamic_cast to " << this->GetToName();
1717 return MatchPrintAndExplain(*to, this->matcher_, listener);
1720 #endif // GTEST_HAS_RTTI 1724 template <
typename Class,
typename FieldType>
1725 class FieldMatcher {
1727 FieldMatcher(FieldType Class::*field,
1728 const Matcher<const FieldType&>& matcher)
1729 : field_(field), matcher_(matcher), whose_field_(
"whose given field ") {}
1731 FieldMatcher(
const std::string& field_name, FieldType Class::*field,
1732 const Matcher<const FieldType&>& matcher)
1735 whose_field_(
"whose field `" + field_name +
"` ") {}
1737 void DescribeTo(::std::ostream* os)
const {
1738 *os <<
"is an object " << whose_field_;
1739 matcher_.DescribeTo(os);
1742 void DescribeNegationTo(::std::ostream* os)
const {
1743 *os <<
"is an object " << whose_field_;
1744 matcher_.DescribeNegationTo(os);
1747 template <
typename T>
1748 bool MatchAndExplain(
const T& value, MatchResultListener* listener)
const {
1751 return MatchAndExplainImpl(
1759 MatchResultListener* listener)
const {
1760 *listener << whose_field_ <<
"is ";
1761 return MatchPrintAndExplain(obj.*field_, matcher_, listener);
1765 MatchResultListener* listener)
const {
1766 if (p ==
nullptr)
return false;
1768 *listener <<
"which points to an object ";
1775 const FieldType Class::*field_;
1776 const Matcher<const FieldType&> matcher_;
1780 const std::string whose_field_;
1790 template <
typename Class,
typename PropertyType,
typename Property>
1791 class PropertyMatcher {
1793 typedef const PropertyType& RefToConstProperty;
1795 PropertyMatcher(Property property,
const Matcher<RefToConstProperty>& matcher)
1796 : property_(property),
1798 whose_property_(
"whose given property ") {}
1800 PropertyMatcher(
const std::string& property_name, Property property,
1801 const Matcher<RefToConstProperty>& matcher)
1802 : property_(property),
1804 whose_property_(
"whose property `" + property_name +
"` ") {}
1806 void DescribeTo(::std::ostream* os)
const {
1807 *os <<
"is an object " << whose_property_;
1808 matcher_.DescribeTo(os);
1811 void DescribeNegationTo(::std::ostream* os)
const {
1812 *os <<
"is an object " << whose_property_;
1813 matcher_.DescribeNegationTo(os);
1816 template <
typename T>
1817 bool MatchAndExplain(
const T&value, MatchResultListener* listener)
const {
1818 return MatchAndExplainImpl(
1826 MatchResultListener* listener)
const {
1827 *listener << whose_property_ <<
"is ";
1830 RefToConstProperty result = (obj.*property_)();
1831 return MatchPrintAndExplain(result, matcher_, listener);
1835 MatchResultListener* listener)
const {
1836 if (p ==
nullptr)
return false;
1838 *listener <<
"which points to an object ";
1846 const Matcher<RefToConstProperty> matcher_;
1850 const std::string whose_property_;
1857 template <
typename Functor>
1858 struct CallableTraits {
1859 typedef Functor StorageType;
1861 static void CheckIsValid(Functor ) {}
1863 template <
typename T>
1864 static auto Invoke(Functor f, T arg) -> decltype(f(arg)) {
return f(arg); }
1868 template <
typename ArgType,
typename ResType>
1869 struct CallableTraits<ResType(*)(ArgType)> {
1870 typedef ResType ResultType;
1871 typedef ResType(*StorageType)(ArgType);
1873 static void CheckIsValid(ResType(*f)(ArgType)) {
1875 <<
"NULL function pointer is passed into ResultOf().";
1877 template <
typename T>
1878 static ResType
Invoke(ResType(*f)(ArgType), T arg) {
1885 template <
typename Callable,
typename InnerMatcher>
1886 class ResultOfMatcher {
1888 ResultOfMatcher(Callable callable, InnerMatcher matcher)
1889 : callable_(std::move(callable)), matcher_(std::move(matcher)) {
1890 CallableTraits<Callable>::CheckIsValid(callable_);
1893 template <
typename T>
1894 operator Matcher<T>()
const {
1895 return Matcher<T>(
new Impl<T>(callable_, matcher_));
1899 typedef typename CallableTraits<Callable>::StorageType CallableStorageType;
1901 template <
typename T>
1902 class Impl :
public MatcherInterface<T> {
1903 using ResultType = decltype(CallableTraits<Callable>::template Invoke<T>(
1904 std::declval<CallableStorageType>(), std::declval<T>()));
1907 template <
typename M>
1908 Impl(
const CallableStorageType& callable,
const M& matcher)
1909 : callable_(callable), matcher_(MatcherCast<ResultType>(matcher)) {}
1911 void DescribeTo(::std::ostream* os)
const override {
1912 *os <<
"is mapped by the given callable to a value that ";
1913 matcher_.DescribeTo(os);
1916 void DescribeNegationTo(::std::ostream* os)
const override {
1917 *os <<
"is mapped by the given callable to a value that ";
1918 matcher_.DescribeNegationTo(os);
1921 bool MatchAndExplain(T obj, MatchResultListener* listener)
const override {
1922 *listener <<
"which is mapped by the given callable to ";
1928 CallableTraits<Callable>::template Invoke<T>(callable_, obj);
1929 return MatchPrintAndExplain(result, matcher_, listener);
1938 mutable CallableStorageType callable_;
1939 const Matcher<ResultType> matcher_;
1944 const CallableStorageType callable_;
1945 const InnerMatcher matcher_;
1951 template <
typename SizeMatcher>
1952 class SizeIsMatcher {
1954 explicit SizeIsMatcher(
const SizeMatcher& size_matcher)
1955 : size_matcher_(size_matcher) {
1958 template <
typename Container>
1959 operator Matcher<Container>()
const {
1960 return Matcher<Container>(
new Impl<const Container&>(size_matcher_));
1963 template <
typename Container>
1964 class Impl :
public MatcherInterface<Container> {
1966 using SizeType = decltype(std::declval<Container>().size());
1967 explicit Impl(
const SizeMatcher& size_matcher)
1968 : size_matcher_(MatcherCast<SizeType>(size_matcher)) {}
1970 void DescribeTo(::std::ostream* os)
const override {
1972 size_matcher_.DescribeTo(os);
1974 void DescribeNegationTo(::std::ostream* os)
const override {
1976 size_matcher_.DescribeNegationTo(os);
1979 bool MatchAndExplain(Container container,
1980 MatchResultListener* listener)
const override {
1981 SizeType size = container.size();
1982 StringMatchResultListener size_listener;
1983 const bool result = size_matcher_.MatchAndExplain(size, &size_listener);
1985 <<
"whose size " << size << (result ?
" matches" :
" doesn't match");
1986 PrintIfNotEmpty(size_listener.str(), listener->stream());
1991 const Matcher<SizeType> size_matcher_;
1996 const SizeMatcher size_matcher_;
2002 template <
typename DistanceMatcher>
2003 class BeginEndDistanceIsMatcher {
2005 explicit BeginEndDistanceIsMatcher(
const DistanceMatcher& distance_matcher)
2006 : distance_matcher_(distance_matcher) {}
2008 template <
typename Container>
2009 operator Matcher<Container>()
const {
2010 return Matcher<Container>(
new Impl<const Container&>(distance_matcher_));
2013 template <
typename Container>
2014 class Impl :
public MatcherInterface<Container> {
2016 typedef internal::StlContainerView<
2018 typedef typename std::iterator_traits<
2019 typename ContainerView::type::const_iterator>::difference_type
2021 explicit Impl(
const DistanceMatcher& distance_matcher)
2022 : distance_matcher_(MatcherCast<DistanceType>(distance_matcher)) {}
2024 void DescribeTo(::std::ostream* os)
const override {
2025 *os <<
"distance between begin() and end() ";
2026 distance_matcher_.DescribeTo(os);
2028 void DescribeNegationTo(::std::ostream* os)
const override {
2029 *os <<
"distance between begin() and end() ";
2030 distance_matcher_.DescribeNegationTo(os);
2033 bool MatchAndExplain(Container container,
2034 MatchResultListener* listener)
const override {
2037 DistanceType distance = std::distance(begin(container), end(container));
2038 StringMatchResultListener distance_listener;
2040 distance_matcher_.MatchAndExplain(distance, &distance_listener);
2041 *listener <<
"whose distance between begin() and end() " << distance
2042 << (result ?
" matches" :
" doesn't match");
2043 PrintIfNotEmpty(distance_listener.str(), listener->stream());
2048 const Matcher<DistanceType> distance_matcher_;
2053 const DistanceMatcher distance_matcher_;
2067 template <
typename Container>
2068 class ContainerEqMatcher {
2070 typedef internal::StlContainerView<Container> View;
2072 typedef typename View::const_reference StlContainerReference;
2076 explicit ContainerEqMatcher(
const Container& expected)
2077 : expected_(View::Copy(expected)) {
2084 void DescribeTo(::std::ostream* os)
const {
2088 void DescribeNegationTo(::std::ostream* os)
const {
2089 *os <<
"does not equal ";
2093 template <
typename LhsContainer>
2094 bool MatchAndExplain(
const LhsContainer& lhs,
2095 MatchResultListener* listener)
const {
2098 typedef internal::StlContainerView<GTEST_REMOVE_CONST_(LhsContainer)>
2101 StlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2102 if (lhs_stl_container == expected_)
2105 ::std::ostream*
const os = listener->stream();
2106 if (os !=
nullptr) {
2108 bool printed_header =
false;
2109 for (
typename LhsStlContainer::const_iterator it =
2110 lhs_stl_container.begin();
2111 it != lhs_stl_container.end(); ++it) {
2114 if (printed_header) {
2117 *os <<
"which has these unexpected elements: ";
2118 printed_header =
true;
2125 bool printed_header2 =
false;
2126 for (
typename StlContainer::const_iterator it = expected_.begin();
2127 it != expected_.end(); ++it) {
2129 lhs_stl_container.begin(), lhs_stl_container.end(), *it) ==
2130 lhs_stl_container.end()) {
2131 if (printed_header2) {
2134 *os << (printed_header ?
",\nand" :
"which")
2135 <<
" doesn't have these expected elements: ";
2136 printed_header2 =
true;
2147 const StlContainer expected_;
2153 struct LessComparator {
2154 template <
typename T,
typename U>
2155 bool operator()(
const T& lhs,
const U& rhs)
const {
return lhs < rhs; }
2159 template <
typename Comparator,
typename ContainerMatcher>
2160 class WhenSortedByMatcher {
2162 WhenSortedByMatcher(
const Comparator& comparator,
2163 const ContainerMatcher& matcher)
2164 : comparator_(comparator), matcher_(matcher) {}
2166 template <
typename LhsContainer>
2167 operator Matcher<LhsContainer>()
const {
2168 return MakeMatcher(
new Impl<LhsContainer>(comparator_, matcher_));
2171 template <
typename LhsContainer>
2172 class Impl :
public MatcherInterface<LhsContainer> {
2174 typedef internal::StlContainerView<
2177 typedef typename LhsView::const_reference LhsStlContainerReference;
2180 typedef typename RemoveConstFromKey<
2181 typename LhsStlContainer::value_type>
::type LhsValue;
2183 Impl(
const Comparator& comparator,
const ContainerMatcher& matcher)
2184 : comparator_(comparator), matcher_(matcher) {}
2186 void DescribeTo(::std::ostream* os)
const override {
2187 *os <<
"(when sorted) ";
2188 matcher_.DescribeTo(os);
2191 void DescribeNegationTo(::std::ostream* os)
const override {
2192 *os <<
"(when sorted) ";
2193 matcher_.DescribeNegationTo(os);
2196 bool MatchAndExplain(LhsContainer lhs,
2197 MatchResultListener* listener)
const override {
2198 LhsStlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2199 ::std::vector<LhsValue> sorted_container(lhs_stl_container.begin(),
2200 lhs_stl_container.end());
2202 sorted_container.begin(), sorted_container.end(), comparator_);
2204 if (!listener->IsInterested()) {
2207 return matcher_.Matches(sorted_container);
2210 *listener <<
"which is ";
2212 *listener <<
" when sorted";
2214 StringMatchResultListener inner_listener;
2215 const bool match = matcher_.MatchAndExplain(sorted_container,
2217 PrintIfNotEmpty(inner_listener.str(), listener->stream());
2222 const Comparator comparator_;
2223 const Matcher<const ::std::vector<LhsValue>&> matcher_;
2229 const Comparator comparator_;
2230 const ContainerMatcher matcher_;
2239 template <
typename TupleMatcher,
typename RhsContainer>
2240 class PointwiseMatcher {
2243 use_UnorderedPointwise_with_hash_tables);
2246 typedef internal::StlContainerView<RhsContainer> RhsView;
2248 typedef typename RhsStlContainer::value_type RhsValue;
2252 PointwiseMatcher(
const TupleMatcher& tuple_matcher,
const RhsContainer& rhs)
2253 : tuple_matcher_(tuple_matcher), rhs_(RhsView::Copy(rhs)) {
2260 template <
typename LhsContainer>
2261 operator Matcher<LhsContainer>()
const {
2264 use_UnorderedPointwise_with_hash_tables);
2266 return Matcher<LhsContainer>(
2267 new Impl<const LhsContainer&>(tuple_matcher_, rhs_));
2270 template <
typename LhsContainer>
2271 class Impl :
public MatcherInterface<LhsContainer> {
2273 typedef internal::StlContainerView<
2276 typedef typename LhsView::const_reference LhsStlContainerReference;
2277 typedef typename LhsStlContainer::value_type LhsValue;
2282 typedef ::std::tuple<const LhsValue&, const RhsValue&> InnerMatcherArg;
2284 Impl(
const TupleMatcher& tuple_matcher,
const RhsStlContainer& rhs)
2286 : mono_tuple_matcher_(SafeMatcherCast<InnerMatcherArg>(tuple_matcher)),
2289 void DescribeTo(::std::ostream* os)
const override {
2290 *os <<
"contains " << rhs_.size()
2291 <<
" values, where each value and its corresponding value in ";
2294 mono_tuple_matcher_.DescribeTo(os);
2296 void DescribeNegationTo(::std::ostream* os)
const override {
2297 *os <<
"doesn't contain exactly " << rhs_.size()
2298 <<
" values, or contains a value x at some index i" 2299 <<
" where x and the i-th value of ";
2302 mono_tuple_matcher_.DescribeNegationTo(os);
2305 bool MatchAndExplain(LhsContainer lhs,
2306 MatchResultListener* listener)
const override {
2307 LhsStlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2308 const size_t actual_size = lhs_stl_container.size();
2309 if (actual_size != rhs_.size()) {
2310 *listener <<
"which contains " << actual_size <<
" values";
2314 typename LhsStlContainer::const_iterator left = lhs_stl_container.begin();
2315 typename RhsStlContainer::const_iterator right = rhs_.begin();
2316 for (
size_t i = 0;
i != actual_size; ++
i, ++left, ++right) {
2317 if (listener->IsInterested()) {
2318 StringMatchResultListener inner_listener;
2322 if (!mono_tuple_matcher_.MatchAndExplain(
2323 InnerMatcherArg(ImplicitCast_<const LhsValue&>(*left),
2324 ImplicitCast_<const RhsValue&>(*right)),
2326 *listener <<
"where the value pair (";
2330 *listener <<
") at index #" <<
i <<
" don't match";
2331 PrintIfNotEmpty(inner_listener.str(), listener->stream());
2335 if (!mono_tuple_matcher_.Matches(
2336 InnerMatcherArg(ImplicitCast_<const LhsValue&>(*left),
2337 ImplicitCast_<const RhsValue&>(*right))))
2346 const Matcher<InnerMatcherArg> mono_tuple_matcher_;
2347 const RhsStlContainer rhs_;
2353 const TupleMatcher tuple_matcher_;
2354 const RhsStlContainer rhs_;
2360 template <
typename Container>
2361 class QuantifierMatcherImpl :
public MatcherInterface<Container> {
2364 typedef StlContainerView<RawContainer> View;
2366 typedef typename View::const_reference StlContainerReference;
2367 typedef typename StlContainer::value_type Element;
2369 template <
typename InnerMatcher>
2370 explicit QuantifierMatcherImpl(InnerMatcher inner_matcher)
2372 testing::SafeMatcherCast<const Element&>(inner_matcher)) {}
2377 bool MatchAndExplainImpl(
bool all_elements_should_match,
2378 Container container,
2379 MatchResultListener* listener)
const {
2380 StlContainerReference stl_container = View::ConstReference(container);
2382 for (
typename StlContainer::const_iterator it = stl_container.begin();
2383 it != stl_container.end(); ++it, ++
i) {
2384 StringMatchResultListener inner_listener;
2385 const bool matches = inner_matcher_.MatchAndExplain(*it, &inner_listener);
2387 if (matches != all_elements_should_match) {
2388 *listener <<
"whose element #" << i
2389 << (matches ?
" matches" :
" doesn't match");
2390 PrintIfNotEmpty(inner_listener.str(), listener->stream());
2391 return !all_elements_should_match;
2394 return all_elements_should_match;
2398 const Matcher<const Element&> inner_matcher_;
2405 template <
typename Container>
2406 class ContainsMatcherImpl :
public QuantifierMatcherImpl<Container> {
2408 template <
typename InnerMatcher>
2409 explicit ContainsMatcherImpl(InnerMatcher inner_matcher)
2410 : QuantifierMatcherImpl<Container>(inner_matcher) {}
2413 void DescribeTo(::std::ostream* os)
const override {
2414 *os <<
"contains at least one element that ";
2415 this->inner_matcher_.DescribeTo(os);
2418 void DescribeNegationTo(::std::ostream* os)
const override {
2419 *os <<
"doesn't contain any element that ";
2420 this->inner_matcher_.DescribeTo(os);
2423 bool MatchAndExplain(Container container,
2424 MatchResultListener* listener)
const override {
2425 return this->MatchAndExplainImpl(
false, container, listener);
2434 template <
typename Container>
2435 class EachMatcherImpl :
public QuantifierMatcherImpl<Container> {
2437 template <
typename InnerMatcher>
2438 explicit EachMatcherImpl(InnerMatcher inner_matcher)
2439 : QuantifierMatcherImpl<Container>(inner_matcher) {}
2442 void DescribeTo(::std::ostream* os)
const override {
2443 *os <<
"only contains elements that ";
2444 this->inner_matcher_.DescribeTo(os);
2447 void DescribeNegationTo(::std::ostream* os)
const override {
2448 *os <<
"contains some element that ";
2449 this->inner_matcher_.DescribeNegationTo(os);
2452 bool MatchAndExplain(Container container,
2453 MatchResultListener* listener)
const override {
2454 return this->MatchAndExplainImpl(
true, container, listener);
2462 template <
typename M>
2463 class ContainsMatcher {
2465 explicit ContainsMatcher(M m) : inner_matcher_(m) {}
2467 template <
typename Container>
2468 operator Matcher<Container>()
const {
2469 return Matcher<Container>(
2470 new ContainsMatcherImpl<const Container&>(inner_matcher_));
2474 const M inner_matcher_;
2480 template <
typename M>
2483 explicit EachMatcher(M m) : inner_matcher_(m) {}
2485 template <
typename Container>
2486 operator Matcher<Container>()
const {
2487 return Matcher<Container>(
2488 new EachMatcherImpl<const Container&>(inner_matcher_));
2492 const M inner_matcher_;
2498 struct Rank0 : Rank1 {};
2500 namespace pair_getters {
2502 template <
typename T>
2503 auto First(T&
x, Rank1) -> decltype(get<0>(x)) {
2506 template <
typename T>
2507 auto First(T& x, Rank0) -> decltype((x.first)) {
2511 template <
typename T>
2512 auto Second(T& x, Rank1) -> decltype(get<1>(x)) {
2515 template <
typename T>
2516 auto Second(T& x, Rank0) -> decltype((x.second)) {
2525 template <
typename PairType>
2526 class KeyMatcherImpl :
public MatcherInterface<PairType> {
2529 typedef typename RawPairType::first_type KeyType;
2531 template <
typename InnerMatcher>
2532 explicit KeyMatcherImpl(InnerMatcher inner_matcher)
2534 testing::SafeMatcherCast<const KeyType&>(inner_matcher)) {
2538 bool MatchAndExplain(PairType key_value,
2539 MatchResultListener* listener)
const override {
2540 StringMatchResultListener inner_listener;
2541 const bool match = inner_matcher_.MatchAndExplain(
2542 pair_getters::First(key_value, Rank0()), &inner_listener);
2543 const std::string explanation = inner_listener.str();
2544 if (explanation !=
"") {
2545 *listener <<
"whose first field is a value " << explanation;
2551 void DescribeTo(::std::ostream* os)
const override {
2552 *os <<
"has a key that ";
2553 inner_matcher_.DescribeTo(os);
2557 void DescribeNegationTo(::std::ostream* os)
const override {
2558 *os <<
"doesn't have a key that ";
2559 inner_matcher_.DescribeTo(os);
2563 const Matcher<const KeyType&> inner_matcher_;
2569 template <
typename M>
2572 explicit KeyMatcher(M m) : matcher_for_key_(m) {}
2574 template <
typename PairType>
2575 operator Matcher<PairType>()
const {
2576 return Matcher<PairType>(
2577 new KeyMatcherImpl<const PairType&>(matcher_for_key_));
2581 const M matcher_for_key_;
2588 template <
typename PairType>
2589 class PairMatcherImpl :
public MatcherInterface<PairType> {
2592 typedef typename RawPairType::first_type FirstType;
2593 typedef typename RawPairType::second_type SecondType;
2595 template <
typename FirstMatcher,
typename SecondMatcher>
2596 PairMatcherImpl(FirstMatcher first_matcher, SecondMatcher second_matcher)
2598 testing::SafeMatcherCast<const FirstType&>(first_matcher)),
2600 testing::SafeMatcherCast<const SecondType&>(second_matcher)) {
2604 void DescribeTo(::std::ostream* os)
const override {
2605 *os <<
"has a first field that ";
2606 first_matcher_.DescribeTo(os);
2607 *os <<
", and has a second field that ";
2608 second_matcher_.DescribeTo(os);
2612 void DescribeNegationTo(::std::ostream* os)
const override {
2613 *os <<
"has a first field that ";
2614 first_matcher_.DescribeNegationTo(os);
2615 *os <<
", or has a second field that ";
2616 second_matcher_.DescribeNegationTo(os);
2621 bool MatchAndExplain(PairType a_pair,
2622 MatchResultListener* listener)
const override {
2623 if (!listener->IsInterested()) {
2626 return first_matcher_.Matches(pair_getters::First(a_pair, Rank0())) &&
2627 second_matcher_.Matches(pair_getters::Second(a_pair, Rank0()));
2629 StringMatchResultListener first_inner_listener;
2630 if (!first_matcher_.MatchAndExplain(pair_getters::First(a_pair, Rank0()),
2631 &first_inner_listener)) {
2632 *listener <<
"whose first field does not match";
2633 PrintIfNotEmpty(first_inner_listener.str(), listener->stream());
2636 StringMatchResultListener second_inner_listener;
2637 if (!second_matcher_.MatchAndExplain(pair_getters::Second(a_pair, Rank0()),
2638 &second_inner_listener)) {
2639 *listener <<
"whose second field does not match";
2640 PrintIfNotEmpty(second_inner_listener.str(), listener->stream());
2643 ExplainSuccess(first_inner_listener.str(), second_inner_listener.str(),
2649 void ExplainSuccess(
const std::string& first_explanation,
2650 const std::string& second_explanation,
2651 MatchResultListener* listener)
const {
2652 *listener <<
"whose both fields match";
2653 if (first_explanation !=
"") {
2654 *listener <<
", where the first field is a value " << first_explanation;
2656 if (second_explanation !=
"") {
2658 if (first_explanation !=
"") {
2659 *listener <<
"and ";
2661 *listener <<
"where ";
2663 *listener <<
"the second field is a value " << second_explanation;
2667 const Matcher<const FirstType&> first_matcher_;
2668 const Matcher<const SecondType&> second_matcher_;
2674 template <
typename FirstMatcher,
typename SecondMatcher>
2677 PairMatcher(FirstMatcher first_matcher, SecondMatcher second_matcher)
2678 : first_matcher_(first_matcher), second_matcher_(second_matcher) {}
2680 template <
typename PairType>
2681 operator Matcher<PairType> ()
const {
2682 return Matcher<PairType>(
2683 new PairMatcherImpl<const PairType&>(first_matcher_, second_matcher_));
2687 const FirstMatcher first_matcher_;
2688 const SecondMatcher second_matcher_;
2694 template <
typename Container>
2695 class ElementsAreMatcherImpl :
public MatcherInterface<Container> {
2698 typedef internal::StlContainerView<RawContainer> View;
2700 typedef typename View::const_reference StlContainerReference;
2701 typedef typename StlContainer::value_type Element;
2705 template <
typename InputIter>
2706 ElementsAreMatcherImpl(InputIter first, InputIter last) {
2707 while (first != last) {
2708 matchers_.push_back(MatcherCast<const Element&>(*first++));
2713 void DescribeTo(::std::ostream* os)
const override {
2716 }
else if (
count() == 1) {
2717 *os <<
"has 1 element that ";
2718 matchers_[0].DescribeTo(os);
2720 *os <<
"has " << Elements(
count()) <<
" where\n";
2721 for (
size_t i = 0;
i !=
count(); ++
i) {
2722 *os <<
"element #" <<
i <<
" ";
2723 matchers_[
i].DescribeTo(os);
2724 if (i + 1 <
count()) {
2732 void DescribeNegationTo(::std::ostream* os)
const override {
2734 *os <<
"isn't empty";
2738 *os <<
"doesn't have " << Elements(
count()) <<
", or\n";
2739 for (
size_t i = 0;
i !=
count(); ++
i) {
2740 *os <<
"element #" <<
i <<
" ";
2741 matchers_[
i].DescribeNegationTo(os);
2742 if (i + 1 <
count()) {
2748 bool MatchAndExplain(Container container,
2749 MatchResultListener* listener)
const override {
2753 const bool listener_interested = listener->IsInterested();
2756 ::std::vector<std::string> explanations(
count());
2757 StlContainerReference stl_container = View::ConstReference(container);
2758 typename StlContainer::const_iterator it = stl_container.begin();
2759 size_t exam_pos = 0;
2760 bool mismatch_found =
false;
2765 for (; it != stl_container.end() && exam_pos !=
count(); ++it, ++exam_pos) {
2767 if (listener_interested) {
2768 StringMatchResultListener s;
2769 match = matchers_[exam_pos].MatchAndExplain(*it, &s);
2770 explanations[exam_pos] = s.str();
2772 match = matchers_[exam_pos].Matches(*it);
2776 mismatch_found =
true;
2785 size_t actual_count = exam_pos;
2786 for (; it != stl_container.end(); ++it) {
2790 if (actual_count !=
count()) {
2795 if (listener_interested && (actual_count != 0)) {
2796 *listener <<
"which has " << Elements(actual_count);
2801 if (mismatch_found) {
2803 if (listener_interested) {
2804 *listener <<
"whose element #" << exam_pos <<
" doesn't match";
2805 PrintIfNotEmpty(explanations[exam_pos], listener->stream());
2812 if (listener_interested) {
2813 bool reason_printed =
false;
2814 for (
size_t i = 0;
i !=
count(); ++
i) {
2815 const std::string& s = explanations[
i];
2817 if (reason_printed) {
2818 *listener <<
",\nand ";
2820 *listener <<
"whose element #" <<
i <<
" matches, " << s;
2821 reason_printed =
true;
2829 static Message Elements(
size_t count) {
2830 return Message() << count << (count == 1 ?
" element" :
" elements");
2833 size_t count()
const {
return matchers_.size(); }
2835 ::std::vector<Matcher<const Element&> > matchers_;
2846 MatchMatrix(
size_t num_elements,
size_t num_matchers)
2847 : num_elements_(num_elements),
2848 num_matchers_(num_matchers),
2849 matched_(num_elements_* num_matchers_, 0) {
2852 size_t LhsSize()
const {
return num_elements_; }
2853 size_t RhsSize()
const {
return num_matchers_; }
2854 bool HasEdge(
size_t ilhs,
size_t irhs)
const {
2855 return matched_[SpaceIndex(ilhs, irhs)] == 1;
2857 void SetEdge(
size_t ilhs,
size_t irhs,
bool b) {
2858 matched_[SpaceIndex(ilhs, irhs)] = b ? 1 : 0;
2868 std::string DebugString()
const;
2871 size_t SpaceIndex(
size_t ilhs,
size_t irhs)
const {
2872 return ilhs * num_matchers_ + irhs;
2875 size_t num_elements_;
2876 size_t num_matchers_;
2881 ::std::vector<char> matched_;
2884 typedef ::std::pair<size_t, size_t> ElementMatcherPair;
2885 typedef ::std::vector<ElementMatcherPair> ElementMatcherPairs;
2892 struct UnorderedMatcherRequire {
2896 ExactMatch = Superset | Subset,
2903 class GTEST_API_ UnorderedElementsAreMatcherImplBase {
2905 explicit UnorderedElementsAreMatcherImplBase(
2906 UnorderedMatcherRequire::Flags matcher_flags)
2907 : match_flags_(matcher_flags) {}
2912 typedef ::std::vector<const MatcherDescriberInterface*> MatcherDescriberVec;
2915 void DescribeToImpl(::std::ostream* os)
const;
2918 void DescribeNegationToImpl(::std::ostream* os)
const;
2920 bool VerifyMatchMatrix(const ::std::vector<std::string>& element_printouts,
2921 const MatchMatrix& matrix,
2922 MatchResultListener* listener)
const;
2924 bool FindPairing(
const MatchMatrix& matrix,
2925 MatchResultListener* listener)
const;
2927 MatcherDescriberVec& matcher_describers() {
2928 return matcher_describers_;
2931 static Message Elements(
size_t n) {
2932 return Message() << n <<
" element" << (n == 1 ?
"" :
"s");
2935 UnorderedMatcherRequire::Flags match_flags()
const {
return match_flags_; }
2938 UnorderedMatcherRequire::Flags match_flags_;
2939 MatcherDescriberVec matcher_describers_;
2946 template <
typename Container>
2947 class UnorderedElementsAreMatcherImpl
2948 :
public MatcherInterface<Container>,
2949 public UnorderedElementsAreMatcherImplBase {
2952 typedef internal::StlContainerView<RawContainer> View;
2954 typedef typename View::const_reference StlContainerReference;
2955 typedef typename StlContainer::const_iterator StlContainerConstIterator;
2956 typedef typename StlContainer::value_type Element;
2958 template <
typename InputIter>
2959 UnorderedElementsAreMatcherImpl(UnorderedMatcherRequire::Flags matcher_flags,
2960 InputIter first, InputIter last)
2961 : UnorderedElementsAreMatcherImplBase(matcher_flags) {
2962 for (; first != last; ++first) {
2963 matchers_.push_back(MatcherCast<const Element&>(*first));
2964 matcher_describers().push_back(matchers_.back().GetDescriber());
2969 void DescribeTo(::std::ostream* os)
const override {
2970 return UnorderedElementsAreMatcherImplBase::DescribeToImpl(os);
2974 void DescribeNegationTo(::std::ostream* os)
const override {
2975 return UnorderedElementsAreMatcherImplBase::DescribeNegationToImpl(os);
2978 bool MatchAndExplain(Container container,
2979 MatchResultListener* listener)
const override {
2980 StlContainerReference stl_container = View::ConstReference(container);
2981 ::std::vector<std::string> element_printouts;
2982 MatchMatrix matrix =
2983 AnalyzeElements(stl_container.begin(), stl_container.end(),
2984 &element_printouts, listener);
2986 if (matrix.LhsSize() == 0 && matrix.RhsSize() == 0) {
2990 if (match_flags() == UnorderedMatcherRequire::ExactMatch) {
2991 if (matrix.LhsSize() != matrix.RhsSize()) {
2996 if (matrix.LhsSize() != 0 && listener->IsInterested()) {
2997 *listener <<
"which has " << Elements(matrix.LhsSize());
3003 return VerifyMatchMatrix(element_printouts, matrix, listener) &&
3004 FindPairing(matrix, listener);
3008 template <
typename ElementIter>
3009 MatchMatrix AnalyzeElements(ElementIter elem_first, ElementIter elem_last,
3010 ::std::vector<std::string>* element_printouts,
3011 MatchResultListener* listener)
const {
3012 element_printouts->clear();
3013 ::std::vector<char> did_match;
3014 size_t num_elements = 0;
3015 for (; elem_first != elem_last; ++num_elements, ++elem_first) {
3016 if (listener->IsInterested()) {
3019 for (
size_t irhs = 0; irhs != matchers_.size(); ++irhs) {
3020 did_match.push_back(Matches(matchers_[irhs])(*elem_first));
3024 MatchMatrix matrix(num_elements, matchers_.size());
3025 ::std::vector<char>::const_iterator did_match_iter = did_match.begin();
3026 for (
size_t ilhs = 0; ilhs != num_elements; ++ilhs) {
3027 for (
size_t irhs = 0; irhs != matchers_.size(); ++irhs) {
3028 matrix.SetEdge(ilhs, irhs, *did_match_iter++ != 0);
3034 ::std::vector<Matcher<const Element&> > matchers_;
3041 template <
typename Target>
3042 struct CastAndAppendTransform {
3043 template <
typename Arg>
3044 Matcher<Target> operator()(
const Arg& a)
const {
3045 return MatcherCast<Target>(a);
3050 template <
typename MatcherTuple>
3051 class UnorderedElementsAreMatcher {
3053 explicit UnorderedElementsAreMatcher(
const MatcherTuple& args)
3054 : matchers_(args) {}
3056 template <
typename Container>
3057 operator Matcher<Container>()
const {
3060 typedef typename View::value_type Element;
3061 typedef ::std::vector<Matcher<const Element&> > MatcherVec;
3062 MatcherVec matchers;
3064 TransformTupleValues(CastAndAppendTransform<const Element&>(), matchers_,
3065 ::std::back_inserter(matchers));
3066 return Matcher<Container>(
3067 new UnorderedElementsAreMatcherImpl<const Container&>(
3068 UnorderedMatcherRequire::ExactMatch, matchers.begin(),
3073 const MatcherTuple matchers_;
3078 template <
typename MatcherTuple>
3079 class ElementsAreMatcher {
3081 explicit ElementsAreMatcher(
const MatcherTuple& args) : matchers_(args) {}
3083 template <
typename Container>
3084 operator Matcher<Container>()
const {
3088 use_UnorderedElementsAre_with_hash_tables);
3092 typedef typename View::value_type Element;
3093 typedef ::std::vector<Matcher<const Element&> > MatcherVec;
3094 MatcherVec matchers;
3096 TransformTupleValues(CastAndAppendTransform<const Element&>(), matchers_,
3097 ::std::back_inserter(matchers));
3098 return Matcher<Container>(
new ElementsAreMatcherImpl<const Container&>(
3099 matchers.begin(), matchers.end()));
3103 const MatcherTuple matchers_;
3108 template <
typename T>
3109 class UnorderedElementsAreArrayMatcher {
3111 template <
typename Iter>
3112 UnorderedElementsAreArrayMatcher(UnorderedMatcherRequire::Flags match_flags,
3114 : match_flags_(match_flags), matchers_(first, last) {}
3116 template <
typename Container>
3117 operator Matcher<Container>()
const {
3118 return Matcher<Container>(
3119 new UnorderedElementsAreMatcherImpl<const Container&>(
3120 match_flags_, matchers_.begin(), matchers_.end()));
3124 UnorderedMatcherRequire::Flags match_flags_;
3125 ::std::vector<T> matchers_;
3131 template <
typename T>
3132 class ElementsAreArrayMatcher {
3134 template <
typename Iter>
3135 ElementsAreArrayMatcher(
Iter first,
Iter last) : matchers_(first, last) {}
3137 template <
typename Container>
3138 operator Matcher<Container>()
const {
3141 use_UnorderedElementsAreArray_with_hash_tables);
3143 return Matcher<Container>(
new ElementsAreMatcherImpl<const Container&>(
3144 matchers_.begin(), matchers_.end()));
3148 const ::std::vector<T> matchers_;
3162 template <
typename Tuple2Matcher,
typename Second>
3163 class BoundSecondMatcher {
3165 BoundSecondMatcher(
const Tuple2Matcher& tm,
const Second& second)
3166 : tuple2_matcher_(tm), second_value_(second) {}
3168 template <
typename T>
3169 operator Matcher<T>()
const {
3170 return MakeMatcher(
new Impl<T>(tuple2_matcher_, second_value_));
3181 void operator=(
const BoundSecondMatcher& ) {
3182 GTEST_LOG_(FATAL) <<
"BoundSecondMatcher should never be assigned.";
3186 template <
typename T>
3187 class Impl :
public MatcherInterface<T> {
3189 typedef ::std::tuple<T, Second> ArgTuple;
3191 Impl(
const Tuple2Matcher& tm,
const Second& second)
3192 : mono_tuple2_matcher_(SafeMatcherCast<const ArgTuple&>(tm)),
3193 second_value_(second) {}
3195 void DescribeTo(::std::ostream* os)
const override {
3199 mono_tuple2_matcher_.DescribeTo(os);
3202 bool MatchAndExplain(T
x, MatchResultListener* listener)
const override {
3203 return mono_tuple2_matcher_.MatchAndExplain(ArgTuple(x, second_value_),
3208 const Matcher<const ArgTuple&> mono_tuple2_matcher_;
3209 const Second second_value_;
3214 const Tuple2Matcher tuple2_matcher_;
3215 const Second second_value_;
3222 template <
typename Tuple2Matcher,
typename Second>
3223 BoundSecondMatcher<Tuple2Matcher, Second> MatcherBindSecond(
3224 const Tuple2Matcher& tm,
const Second& second) {
3225 return BoundSecondMatcher<Tuple2Matcher, Second>(tm, second);
3234 const char* matcher_name,
3238 template <
typename ValueMatcher>
3239 class OptionalMatcher {
3241 explicit OptionalMatcher(
const ValueMatcher& value_matcher)
3242 : value_matcher_(value_matcher) {}
3244 template <
typename Optional>
3245 operator Matcher<Optional>()
const {
3246 return Matcher<Optional>(
new Impl<const Optional&>(value_matcher_));
3249 template <
typename Optional>
3250 class Impl :
public MatcherInterface<Optional> {
3253 typedef typename OptionalView::value_type ValueType;
3254 explicit Impl(
const ValueMatcher& value_matcher)
3255 : value_matcher_(MatcherCast<ValueType>(value_matcher)) {}
3257 void DescribeTo(::std::ostream* os)
const override {
3259 value_matcher_.DescribeTo(os);
3262 void DescribeNegationTo(::std::ostream* os)
const override {
3264 value_matcher_.DescribeNegationTo(os);
3267 bool MatchAndExplain(Optional optional,
3268 MatchResultListener* listener)
const override {
3270 *listener <<
"which is not engaged";
3273 const ValueType& value = *optional;
3274 StringMatchResultListener value_listener;
3275 const bool match = value_matcher_.MatchAndExplain(value, &value_listener);
3277 << (match ?
" matches" :
" doesn't match");
3278 PrintIfNotEmpty(value_listener.str(), listener->stream());
3283 const Matcher<ValueType> value_matcher_;
3288 const ValueMatcher value_matcher_;
3292 namespace variant_matcher {
3294 template <
typename T>
3295 void holds_alternative() {}
3296 template <
typename T>
3300 template <
typename T>
3301 class VariantMatcher {
3304 : matcher_(std::move(matcher)) {}
3306 template <
typename Variant>
3307 bool MatchAndExplain(
const Variant& value,
3308 ::testing::MatchResultListener* listener)
const {
3310 if (!listener->IsInterested()) {
3311 return holds_alternative<T>(
value) && matcher_.Matches(get<T>(value));
3314 if (!holds_alternative<T>(value)) {
3315 *listener <<
"whose value is not of type '" <<
GetTypeName() <<
"'";
3319 const T& elem = get<T>(
value);
3320 StringMatchResultListener elem_listener;
3321 const bool match = matcher_.MatchAndExplain(elem, &elem_listener);
3323 << (match ?
" matches" :
" doesn't match");
3324 PrintIfNotEmpty(elem_listener.str(), listener->stream());
3328 void DescribeTo(std::ostream* os)
const {
3329 *os <<
"is a variant<> with value of type '" <<
GetTypeName()
3330 <<
"' and the value ";
3331 matcher_.DescribeTo(os);
3334 void DescribeNegationTo(std::ostream* os)
const {
3335 *os <<
"is a variant<> with value of type other than '" <<
GetTypeName()
3336 <<
"' or the value ";
3337 matcher_.DescribeNegationTo(os);
3344 return internal::GetTypeName<T>());
3346 return "the element type";
3349 const ::testing::Matcher<const T&> matcher_;
3354 namespace any_cast_matcher {
3357 template <
typename T>
3361 template <
typename T>
3362 class AnyCastMatcher {
3364 explicit AnyCastMatcher(const ::testing::Matcher<const T&>& matcher)
3365 : matcher_(matcher) {}
3367 template <
typename AnyType>
3368 bool MatchAndExplain(
const AnyType& value,
3369 ::testing::MatchResultListener* listener)
const {
3370 if (!listener->IsInterested()) {
3371 const T* ptr = any_cast<T>(&
value);
3372 return ptr !=
nullptr && matcher_.Matches(*ptr);
3375 const T* elem = any_cast<T>(&
value);
3376 if (elem ==
nullptr) {
3377 *listener <<
"whose value is not of type '" <<
GetTypeName() <<
"'";
3381 StringMatchResultListener elem_listener;
3382 const bool match = matcher_.MatchAndExplain(*elem, &elem_listener);
3384 << (match ?
" matches" :
" doesn't match");
3385 PrintIfNotEmpty(elem_listener.str(), listener->stream());
3389 void DescribeTo(std::ostream* os)
const {
3390 *os <<
"is an 'any' type with value of type '" <<
GetTypeName()
3391 <<
"' and the value ";
3392 matcher_.DescribeTo(os);
3395 void DescribeNegationTo(std::ostream* os)
const {
3396 *os <<
"is an 'any' type with value of type other than '" <<
GetTypeName()
3397 <<
"' or the value ";
3398 matcher_.DescribeNegationTo(os);
3405 return internal::GetTypeName<T>());
3407 return "the element type";
3410 const ::testing::Matcher<const T&> matcher_;
3416 template <
class ArgsTuple,
size_t... k>
3417 class ArgsMatcherImpl :
public MatcherInterface<ArgsTuple> {
3420 using SelectedArgs =
3422 using MonomorphicInnerMatcher = Matcher<const SelectedArgs&>;
3424 template <
typename InnerMatcher>
3425 explicit ArgsMatcherImpl(
const InnerMatcher& inner_matcher)
3426 : inner_matcher_(SafeMatcherCast<const SelectedArgs&>(inner_matcher)) {}
3428 bool MatchAndExplain(ArgsTuple args,
3429 MatchResultListener* listener)
const override {
3432 const SelectedArgs& selected_args =
3433 std::forward_as_tuple(std::get<k>(args)...);
3434 if (!listener->IsInterested())
return inner_matcher_.Matches(selected_args);
3436 PrintIndices(listener->stream());
3439 StringMatchResultListener inner_listener;
3441 inner_matcher_.MatchAndExplain(selected_args, &inner_listener);
3442 PrintIfNotEmpty(inner_listener.str(), listener->stream());
3446 void DescribeTo(::std::ostream* os)
const override {
3447 *os <<
"are a tuple ";
3449 inner_matcher_.DescribeTo(os);
3452 void DescribeNegationTo(::std::ostream* os)
const override {
3453 *os <<
"are a tuple ";
3455 inner_matcher_.DescribeNegationTo(os);
3460 static void PrintIndices(::std::ostream* os) {
3461 *os <<
"whose fields (";
3462 const char* sep =
"";
3465 const char* dummy[] = {
"", (*os << sep <<
"#" << k, sep =
", ")...};
3470 MonomorphicInnerMatcher inner_matcher_;
3473 template <
class InnerMatcher,
size_t... k>
3476 explicit ArgsMatcher(InnerMatcher inner_matcher)
3477 : inner_matcher_(std::move(inner_matcher)) {}
3479 template <
typename ArgsTuple>
3480 operator Matcher<ArgsTuple>()
const {
3481 return MakeMatcher(
new ArgsMatcherImpl<ArgsTuple, k...>(inner_matcher_));
3485 InnerMatcher inner_matcher_;
3505 template <
typename Iter>
3506 inline internal::ElementsAreArrayMatcher<
3507 typename ::std::iterator_traits<Iter>::value_type>
3508 ElementsAreArray(
Iter first,
Iter last) {
3509 typedef typename ::std::iterator_traits<Iter>::value_type T;
3510 return internal::ElementsAreArrayMatcher<T>(first, last);
3513 template <
typename T>
3514 inline internal::ElementsAreArrayMatcher<T> ElementsAreArray(
3515 const T* pointer,
size_t count) {
3516 return ElementsAreArray(pointer, pointer + count);
3519 template <
typename T,
size_t N>
3520 inline internal::ElementsAreArrayMatcher<T> ElementsAreArray(
3521 const T (&array)[N]) {
3522 return ElementsAreArray(array, N);
3525 template <
typename Container>
3526 inline internal::ElementsAreArrayMatcher<typename Container::value_type>
3527 ElementsAreArray(
const Container& container) {
3528 return ElementsAreArray(container.begin(), container.end());
3531 template <
typename T>
3532 inline internal::ElementsAreArrayMatcher<T>
3533 ElementsAreArray(::std::initializer_list<T> xs) {
3534 return ElementsAreArray(xs.begin(), xs.end());
3550 template <
typename Iter>
3551 inline internal::UnorderedElementsAreArrayMatcher<
3552 typename ::std::iterator_traits<Iter>::value_type>
3553 UnorderedElementsAreArray(
Iter first,
Iter last) {
3554 typedef typename ::std::iterator_traits<Iter>::value_type T;
3555 return internal::UnorderedElementsAreArrayMatcher<T>(
3556 internal::UnorderedMatcherRequire::ExactMatch, first, last);
3559 template <
typename T>
3560 inline internal::UnorderedElementsAreArrayMatcher<T>
3561 UnorderedElementsAreArray(
const T* pointer,
size_t count) {
3562 return UnorderedElementsAreArray(pointer, pointer + count);
3565 template <
typename T,
size_t N>
3566 inline internal::UnorderedElementsAreArrayMatcher<T>
3567 UnorderedElementsAreArray(
const T (&array)[N]) {
3568 return UnorderedElementsAreArray(array, N);
3571 template <
typename Container>
3572 inline internal::UnorderedElementsAreArrayMatcher<
3573 typename Container::value_type>
3574 UnorderedElementsAreArray(
const Container& container) {
3575 return UnorderedElementsAreArray(container.begin(), container.end());
3578 template <
typename T>
3579 inline internal::UnorderedElementsAreArrayMatcher<T>
3580 UnorderedElementsAreArray(::std::initializer_list<T> xs) {
3581 return UnorderedElementsAreArray(xs.begin(), xs.end());
3593 const internal::AnythingMatcher _ = {};
3595 template <
typename T>
3596 inline Matcher<T> A() {
3597 return Matcher<T>(
new internal::AnyMatcherImpl<T>());
3601 template <
typename T>
3602 inline Matcher<T> An() {
return A<T>(); }
3604 template <
typename T,
typename M>
3605 Matcher<T> internal::MatcherCastImpl<T, M>::CastImpl(
3607 internal::BooleanConstant<false> ,
3608 internal::BooleanConstant<false> ) {
3613 inline PolymorphicMatcher<internal::IsNullMatcher >
IsNull() {
3614 return MakePolymorphicMatcher(internal::IsNullMatcher());
3620 inline PolymorphicMatcher<internal::NotNullMatcher > NotNull() {
3621 return MakePolymorphicMatcher(internal::NotNullMatcher());
3626 template <
typename T>
3627 inline internal::RefMatcher<T&> Ref(T&
x) {
3628 return internal::RefMatcher<T&>(
x);
3633 inline internal::FloatingEqMatcher<double> DoubleEq(
double rhs) {
3634 return internal::FloatingEqMatcher<double>(rhs,
false);
3639 inline internal::FloatingEqMatcher<double> NanSensitiveDoubleEq(
double rhs) {
3640 return internal::FloatingEqMatcher<double>(rhs,
true);
3646 inline internal::FloatingEqMatcher<double> DoubleNear(
3647 double rhs,
double max_abs_error) {
3648 return internal::FloatingEqMatcher<double>(rhs,
false, max_abs_error);
3654 inline internal::FloatingEqMatcher<double> NanSensitiveDoubleNear(
3655 double rhs,
double max_abs_error) {
3656 return internal::FloatingEqMatcher<double>(rhs,
true, max_abs_error);
3661 inline internal::FloatingEqMatcher<float> FloatEq(
float rhs) {
3662 return internal::FloatingEqMatcher<float>(rhs,
false);
3667 inline internal::FloatingEqMatcher<float> NanSensitiveFloatEq(
float rhs) {
3668 return internal::FloatingEqMatcher<float>(rhs,
true);
3674 inline internal::FloatingEqMatcher<float> FloatNear(
3675 float rhs,
float max_abs_error) {
3676 return internal::FloatingEqMatcher<float>(rhs,
false, max_abs_error);
3682 inline internal::FloatingEqMatcher<float> NanSensitiveFloatNear(
3683 float rhs,
float max_abs_error) {
3684 return internal::FloatingEqMatcher<float>(rhs,
true, max_abs_error);
3689 template <
typename InnerMatcher>
3690 inline internal::PointeeMatcher<InnerMatcher> Pointee(
3691 const InnerMatcher& inner_matcher) {
3692 return internal::PointeeMatcher<InnerMatcher>(inner_matcher);
3702 template <
typename To>
3703 inline PolymorphicMatcher<internal::WhenDynamicCastToMatcher<To> >
3704 WhenDynamicCastTo(
const Matcher<To>& inner_matcher) {
3705 return MakePolymorphicMatcher(
3706 internal::WhenDynamicCastToMatcher<To>(inner_matcher));
3708 #endif // GTEST_HAS_RTTI 3714 template <
typename Class,
typename FieldType,
typename FieldMatcher>
3715 inline PolymorphicMatcher<
3716 internal::FieldMatcher<Class, FieldType> > Field(
3717 FieldType Class::*field,
const FieldMatcher& matcher) {
3718 return MakePolymorphicMatcher(
3719 internal::FieldMatcher<Class, FieldType>(
3720 field, MatcherCast<const FieldType&>(matcher)));
3729 template <
typename Class,
typename FieldType,
typename FieldMatcher>
3730 inline PolymorphicMatcher<internal::FieldMatcher<Class, FieldType> > Field(
3731 const std::string& field_name, FieldType Class::*field,
3732 const FieldMatcher& matcher) {
3733 return MakePolymorphicMatcher(internal::FieldMatcher<Class, FieldType>(
3734 field_name, field, MatcherCast<const FieldType&>(matcher)));
3741 template <
typename Class,
typename PropertyType,
typename PropertyMatcher>
3742 inline PolymorphicMatcher<internal::PropertyMatcher<
3743 Class, PropertyType, PropertyType (Class::*)()
const> >
3744 Property(PropertyType (Class::*property)()
const,
3745 const PropertyMatcher& matcher) {
3746 return MakePolymorphicMatcher(
3747 internal::PropertyMatcher<Class, PropertyType,
3748 PropertyType (Class::*)()
const>(
3749 property, MatcherCast<const PropertyType&>(matcher)));
3758 template <
typename Class,
typename PropertyType,
typename PropertyMatcher>
3759 inline PolymorphicMatcher<internal::PropertyMatcher<
3760 Class, PropertyType, PropertyType (Class::*)()
const> >
3761 Property(
const std::string& property_name,
3762 PropertyType (Class::*property)()
const,
3763 const PropertyMatcher& matcher) {
3764 return MakePolymorphicMatcher(
3765 internal::PropertyMatcher<Class, PropertyType,
3766 PropertyType (Class::*)()
const>(
3767 property_name, property, MatcherCast<const PropertyType&>(matcher)));
3771 template <
typename Class,
typename PropertyType,
typename PropertyMatcher>
3772 inline PolymorphicMatcher<internal::PropertyMatcher<
3773 Class, PropertyType, PropertyType (Class::*)()
const &> >
3774 Property(PropertyType (Class::*property)()
const &,
3775 const PropertyMatcher& matcher) {
3776 return MakePolymorphicMatcher(
3777 internal::PropertyMatcher<Class, PropertyType,
3778 PropertyType (Class::*)()
const&>(
3779 property, MatcherCast<const PropertyType&>(matcher)));
3783 template <
typename Class,
typename PropertyType,
typename PropertyMatcher>
3784 inline PolymorphicMatcher<internal::PropertyMatcher<
3785 Class, PropertyType, PropertyType (Class::*)()
const &> >
3786 Property(
const std::string& property_name,
3787 PropertyType (Class::*property)()
const &,
3788 const PropertyMatcher& matcher) {
3789 return MakePolymorphicMatcher(
3790 internal::PropertyMatcher<Class, PropertyType,
3791 PropertyType (Class::*)()
const&>(
3792 property_name, property, MatcherCast<const PropertyType&>(matcher)));
3804 template <
typename Callable,
typename InnerMatcher>
3805 internal::ResultOfMatcher<Callable, InnerMatcher> ResultOf(
3806 Callable callable, InnerMatcher matcher) {
3807 return internal::ResultOfMatcher<Callable, InnerMatcher>(
3808 std::move(callable), std::move(matcher));
3814 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::string> > StrEq(
3815 const std::string& str) {
3816 return MakePolymorphicMatcher(
3817 internal::StrEqualityMatcher<std::string>(str,
true,
true));
3821 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::string> > StrNe(
3822 const std::string& str) {
3823 return MakePolymorphicMatcher(
3824 internal::StrEqualityMatcher<std::string>(str,
false,
true));
3828 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::string> > StrCaseEq(
3829 const std::string& str) {
3830 return MakePolymorphicMatcher(
3831 internal::StrEqualityMatcher<std::string>(str,
true,
false));
3835 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::string> > StrCaseNe(
3836 const std::string& str) {
3837 return MakePolymorphicMatcher(
3838 internal::StrEqualityMatcher<std::string>(str,
false,
false));
3843 inline PolymorphicMatcher<internal::HasSubstrMatcher<std::string> > HasSubstr(
3844 const std::string& substring) {
3845 return MakePolymorphicMatcher(
3846 internal::HasSubstrMatcher<std::string>(substring));
3850 inline PolymorphicMatcher<internal::StartsWithMatcher<std::string> >
StartsWith(
3851 const std::string& prefix) {
3852 return MakePolymorphicMatcher(
3853 internal::StartsWithMatcher<std::string>(prefix));
3857 inline PolymorphicMatcher<internal::EndsWithMatcher<std::string> > EndsWith(
3858 const std::string& suffix) {
3859 return MakePolymorphicMatcher(internal::EndsWithMatcher<std::string>(suffix));
3862 #if GTEST_HAS_STD_WSTRING 3866 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring> > StrEq(
3867 const std::wstring& str) {
3868 return MakePolymorphicMatcher(
3869 internal::StrEqualityMatcher<std::wstring>(str,
true,
true));
3873 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring> > StrNe(
3874 const std::wstring& str) {
3875 return MakePolymorphicMatcher(
3876 internal::StrEqualityMatcher<std::wstring>(str,
false,
true));
3880 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring> >
3881 StrCaseEq(
const std::wstring& str) {
3882 return MakePolymorphicMatcher(
3883 internal::StrEqualityMatcher<std::wstring>(str,
true,
false));
3887 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring> >
3888 StrCaseNe(
const std::wstring& str) {
3889 return MakePolymorphicMatcher(
3890 internal::StrEqualityMatcher<std::wstring>(str,
false,
false));
3895 inline PolymorphicMatcher<internal::HasSubstrMatcher<std::wstring> > HasSubstr(
3896 const std::wstring& substring) {
3897 return MakePolymorphicMatcher(
3898 internal::HasSubstrMatcher<std::wstring>(substring));
3902 inline PolymorphicMatcher<internal::StartsWithMatcher<std::wstring> >
3904 return MakePolymorphicMatcher(
3905 internal::StartsWithMatcher<std::wstring>(prefix));
3909 inline PolymorphicMatcher<internal::EndsWithMatcher<std::wstring> > EndsWith(
3910 const std::wstring& suffix) {
3911 return MakePolymorphicMatcher(
3912 internal::EndsWithMatcher<std::wstring>(suffix));
3915 #endif // GTEST_HAS_STD_WSTRING 3919 inline internal::Eq2Matcher Eq() {
return internal::Eq2Matcher(); }
3923 inline internal::Ge2Matcher Ge() {
return internal::Ge2Matcher(); }
3927 inline internal::Gt2Matcher Gt() {
return internal::Gt2Matcher(); }
3931 inline internal::Le2Matcher Le() {
return internal::Le2Matcher(); }
3935 inline internal::Lt2Matcher Lt() {
return internal::Lt2Matcher(); }
3939 inline internal::Ne2Matcher Ne() {
return internal::Ne2Matcher(); }
3943 inline internal::FloatingEq2Matcher<float> FloatEq() {
3944 return internal::FloatingEq2Matcher<float>();
3949 inline internal::FloatingEq2Matcher<double> DoubleEq() {
3950 return internal::FloatingEq2Matcher<double>();
3955 inline internal::FloatingEq2Matcher<float> NanSensitiveFloatEq() {
3956 return internal::FloatingEq2Matcher<float>(
true);
3961 inline internal::FloatingEq2Matcher<double> NanSensitiveDoubleEq() {
3962 return internal::FloatingEq2Matcher<double>(
true);
3967 inline internal::FloatingEq2Matcher<float> FloatNear(
float max_abs_error) {
3968 return internal::FloatingEq2Matcher<float>(max_abs_error);
3973 inline internal::FloatingEq2Matcher<double> DoubleNear(
double max_abs_error) {
3974 return internal::FloatingEq2Matcher<double>(max_abs_error);
3980 inline internal::FloatingEq2Matcher<float> NanSensitiveFloatNear(
3981 float max_abs_error) {
3982 return internal::FloatingEq2Matcher<float>(max_abs_error,
true);
3988 inline internal::FloatingEq2Matcher<double> NanSensitiveDoubleNear(
3989 double max_abs_error) {
3990 return internal::FloatingEq2Matcher<double>(max_abs_error,
true);
3995 template <
typename InnerMatcher>
3996 inline internal::NotMatcher<InnerMatcher> Not(InnerMatcher m) {
3997 return internal::NotMatcher<InnerMatcher>(m);
4003 template <
typename Predicate>
4004 inline PolymorphicMatcher<internal::TrulyMatcher<Predicate> >
4005 Truly(Predicate pred) {
4006 return MakePolymorphicMatcher(internal::TrulyMatcher<Predicate>(pred));
4015 template <
typename SizeMatcher>
4016 inline internal::SizeIsMatcher<SizeMatcher>
4017 SizeIs(
const SizeMatcher& size_matcher) {
4018 return internal::SizeIsMatcher<SizeMatcher>(size_matcher);
4026 template <
typename DistanceMatcher>
4027 inline internal::BeginEndDistanceIsMatcher<DistanceMatcher>
4028 BeginEndDistanceIs(
const DistanceMatcher& distance_matcher) {
4029 return internal::BeginEndDistanceIsMatcher<DistanceMatcher>(distance_matcher);
4036 template <
typename Container>
4037 inline PolymorphicMatcher<internal::ContainerEqMatcher<
4039 ContainerEq(
const Container& rhs) {
4043 return MakePolymorphicMatcher(
4044 internal::ContainerEqMatcher<RawContainer>(rhs));
4049 template <
typename Comparator,
typename ContainerMatcher>
4050 inline internal::WhenSortedByMatcher<Comparator, ContainerMatcher>
4051 WhenSortedBy(
const Comparator& comparator,
4052 const ContainerMatcher& container_matcher) {
4053 return internal::WhenSortedByMatcher<Comparator, ContainerMatcher>(
4054 comparator, container_matcher);
4059 template <
typename ContainerMatcher>
4060 inline internal::WhenSortedByMatcher<internal::LessComparator, ContainerMatcher>
4061 WhenSorted(
const ContainerMatcher& container_matcher) {
4063 internal::WhenSortedByMatcher<internal::LessComparator, ContainerMatcher>(
4064 internal::LessComparator(), container_matcher);
4073 template <
typename TupleMatcher,
typename Container>
4074 inline internal::PointwiseMatcher<TupleMatcher,
4076 Pointwise(
const TupleMatcher& tuple_matcher,
const Container& rhs) {
4081 return internal::PointwiseMatcher<TupleMatcher, RawContainer>(
4082 tuple_matcher, rhs);
4087 template <
typename TupleMatcher,
typename T>
4088 inline internal::PointwiseMatcher<TupleMatcher, std::vector<T> > Pointwise(
4089 const TupleMatcher& tuple_matcher, std::initializer_list<T> rhs) {
4090 return Pointwise(tuple_matcher, std::vector<T>(rhs));
4105 template <
typename Tuple2Matcher,
typename RhsContainer>
4106 inline internal::UnorderedElementsAreArrayMatcher<
4107 typename internal::BoundSecondMatcher<
4109 RhsContainer)>::type::value_type> >
4110 UnorderedPointwise(
const Tuple2Matcher& tuple2_matcher,
4111 const RhsContainer& rhs_container) {
4119 typedef typename internal::StlContainerView<RawRhsContainer> RhsView;
4121 typedef typename RhsStlContainer::value_type Second;
4122 const RhsStlContainer& rhs_stl_container =
4123 RhsView::ConstReference(rhs_container);
4126 ::std::vector<internal::BoundSecondMatcher<Tuple2Matcher, Second> > matchers;
4127 for (
typename RhsStlContainer::const_iterator it = rhs_stl_container.begin();
4128 it != rhs_stl_container.end(); ++it) {
4130 internal::MatcherBindSecond(tuple2_matcher, *it));
4134 return UnorderedElementsAreArray(matchers);
4139 template <
typename Tuple2Matcher,
typename T>
4140 inline internal::UnorderedElementsAreArrayMatcher<
4141 typename internal::BoundSecondMatcher<Tuple2Matcher, T> >
4142 UnorderedPointwise(
const Tuple2Matcher& tuple2_matcher,
4143 std::initializer_list<T> rhs) {
4144 return UnorderedPointwise(tuple2_matcher, std::vector<T>(rhs));
4166 template <
typename M>
4167 inline internal::ContainsMatcher<M> Contains(M matcher) {
4168 return internal::ContainsMatcher<M>(matcher);
4198 template <
typename Iter>
4199 inline internal::UnorderedElementsAreArrayMatcher<
4200 typename ::std::iterator_traits<Iter>::value_type>
4201 IsSupersetOf(
Iter first,
Iter last) {
4202 typedef typename ::std::iterator_traits<Iter>::value_type T;
4203 return internal::UnorderedElementsAreArrayMatcher<T>(
4204 internal::UnorderedMatcherRequire::Superset, first, last);
4207 template <
typename T>
4208 inline internal::UnorderedElementsAreArrayMatcher<T> IsSupersetOf(
4209 const T* pointer,
size_t count) {
4210 return IsSupersetOf(pointer, pointer + count);
4213 template <
typename T,
size_t N>
4214 inline internal::UnorderedElementsAreArrayMatcher<T> IsSupersetOf(
4215 const T (&array)[N]) {
4216 return IsSupersetOf(array, N);
4219 template <
typename Container>
4220 inline internal::UnorderedElementsAreArrayMatcher<
4221 typename Container::value_type>
4222 IsSupersetOf(
const Container& container) {
4223 return IsSupersetOf(container.begin(), container.end());
4226 template <
typename T>
4227 inline internal::UnorderedElementsAreArrayMatcher<T> IsSupersetOf(
4228 ::std::initializer_list<T> xs) {
4229 return IsSupersetOf(xs.begin(), xs.end());
4255 template <
typename Iter>
4256 inline internal::UnorderedElementsAreArrayMatcher<
4257 typename ::std::iterator_traits<Iter>::value_type>
4258 IsSubsetOf(
Iter first,
Iter last) {
4259 typedef typename ::std::iterator_traits<Iter>::value_type T;
4260 return internal::UnorderedElementsAreArrayMatcher<T>(
4261 internal::UnorderedMatcherRequire::Subset, first, last);
4264 template <
typename T>
4265 inline internal::UnorderedElementsAreArrayMatcher<T> IsSubsetOf(
4266 const T* pointer,
size_t count) {
4267 return IsSubsetOf(pointer, pointer + count);
4270 template <
typename T,
size_t N>
4271 inline internal::UnorderedElementsAreArrayMatcher<T> IsSubsetOf(
4272 const T (&array)[N]) {
4273 return IsSubsetOf(array, N);
4276 template <
typename Container>
4277 inline internal::UnorderedElementsAreArrayMatcher<
4278 typename Container::value_type>
4279 IsSubsetOf(
const Container& container) {
4280 return IsSubsetOf(container.begin(), container.end());
4283 template <
typename T>
4284 inline internal::UnorderedElementsAreArrayMatcher<T> IsSubsetOf(
4285 ::std::initializer_list<T> xs) {
4286 return IsSubsetOf(xs.begin(), xs.end());
4316 template <
typename M>
4317 inline internal::EachMatcher<M> Each(M matcher) {
4318 return internal::EachMatcher<M>(matcher);
4324 template <
typename M>
4325 inline internal::KeyMatcher<M> Key(M inner_matcher) {
4326 return internal::KeyMatcher<M>(inner_matcher);
4334 template <
typename FirstMatcher,
typename SecondMatcher>
4335 inline internal::PairMatcher<FirstMatcher, SecondMatcher>
4336 Pair(FirstMatcher first_matcher, SecondMatcher second_matcher) {
4337 return internal::PairMatcher<FirstMatcher, SecondMatcher>(
4338 first_matcher, second_matcher);
4343 template <
typename M>
4344 inline internal::MatcherAsPredicate<M> Matches(M matcher) {
4345 return internal::MatcherAsPredicate<M>(matcher);
4349 template <
typename T,
typename M>
4350 inline bool Value(
const T& value, M matcher) {
4351 return testing::Matches(matcher)(
value);
4356 template <
typename T,
typename M>
4357 inline bool ExplainMatchResult(
4358 M matcher,
const T& value, MatchResultListener* listener) {
4359 return SafeMatcherCast<const T&>(matcher).MatchAndExplain(value, listener);
4372 template <
typename T,
typename M>
4373 std::string DescribeMatcher(
const M& matcher,
bool negation =
false) {
4374 ::std::stringstream ss;
4375 Matcher<T> monomorphic_matcher = SafeMatcherCast<T>(matcher);
4377 monomorphic_matcher.DescribeNegationTo(&ss);
4379 monomorphic_matcher.DescribeTo(&ss);
4384 template <
typename... Args>
4385 internal::ElementsAreMatcher<
4387 ElementsAre(
const Args&... matchers) {
4388 return internal::ElementsAreMatcher<
4390 std::make_tuple(matchers...));
4393 template <
typename... Args>
4394 internal::UnorderedElementsAreMatcher<
4396 UnorderedElementsAre(
const Args&... matchers) {
4397 return internal::UnorderedElementsAreMatcher<
4399 std::make_tuple(matchers...));
4403 template <
typename... Args>
4405 const Args&... matchers) {
4410 template <
typename... Args>
4412 const Args&... matchers) {
4439 template <
typename Iter>
4440 inline internal::AnyOfArrayMatcher<
4441 typename ::std::iterator_traits<Iter>::value_type>
4442 AnyOfArray(
Iter first,
Iter last) {
4443 return internal::AnyOfArrayMatcher<
4444 typename ::std::iterator_traits<Iter>::value_type>(first, last);
4447 template <
typename Iter>
4448 inline internal::AllOfArrayMatcher<
4449 typename ::std::iterator_traits<Iter>::value_type>
4450 AllOfArray(
Iter first,
Iter last) {
4451 return internal::AllOfArrayMatcher<
4452 typename ::std::iterator_traits<Iter>::value_type>(first, last);
4455 template <
typename T>
4456 inline internal::AnyOfArrayMatcher<T> AnyOfArray(
const T* ptr,
size_t count) {
4457 return AnyOfArray(ptr, ptr + count);
4460 template <
typename T>
4461 inline internal::AllOfArrayMatcher<T> AllOfArray(
const T* ptr,
size_t count) {
4462 return AllOfArray(ptr, ptr + count);
4465 template <
typename T,
size_t N>
4466 inline internal::AnyOfArrayMatcher<T> AnyOfArray(
const T (&array)[N]) {
4467 return AnyOfArray(array, N);
4470 template <
typename T,
size_t N>
4471 inline internal::AllOfArrayMatcher<T> AllOfArray(
const T (&array)[N]) {
4472 return AllOfArray(array, N);
4475 template <
typename Container>
4476 inline internal::AnyOfArrayMatcher<typename Container::value_type> AnyOfArray(
4477 const Container& container) {
4478 return AnyOfArray(container.begin(), container.end());
4481 template <
typename Container>
4482 inline internal::AllOfArrayMatcher<typename Container::value_type> AllOfArray(
4483 const Container& container) {
4484 return AllOfArray(container.begin(), container.end());
4487 template <
typename T>
4488 inline internal::AnyOfArrayMatcher<T> AnyOfArray(
4489 ::std::initializer_list<T> xs) {
4490 return AnyOfArray(xs.begin(), xs.end());
4493 template <
typename T>
4494 inline internal::AllOfArrayMatcher<T> AllOfArray(
4495 ::std::initializer_list<T> xs) {
4496 return AllOfArray(xs.begin(), xs.end());
4502 template <
size_t... k,
typename InnerMatcher>
4504 InnerMatcher&& matcher) {
4505 return internal::ArgsMatcher<typename std::decay<InnerMatcher>::type, k...>(
4506 std::forward<InnerMatcher>(matcher));
4516 template <
typename InnerMatcher>
4517 inline InnerMatcher AllArgs(
const InnerMatcher& matcher) {
return matcher; }
4527 template <
typename ValueMatcher>
4528 inline internal::OptionalMatcher<ValueMatcher> Optional(
4529 const ValueMatcher& value_matcher) {
4530 return internal::OptionalMatcher<ValueMatcher>(value_matcher);
4534 template <
typename T>
4535 PolymorphicMatcher<internal::any_cast_matcher::AnyCastMatcher<T> > AnyWith(
4536 const Matcher<const T&>& matcher) {
4537 return MakePolymorphicMatcher(
4538 internal::any_cast_matcher::AnyCastMatcher<T>(matcher));
4545 template <
typename T>
4546 PolymorphicMatcher<internal::variant_matcher::VariantMatcher<T> > VariantWith(
4547 const Matcher<const T&>& matcher) {
4548 return MakePolymorphicMatcher(
4549 internal::variant_matcher::VariantMatcher<T>(matcher));
4556 #define ASSERT_THAT(value, matcher) ASSERT_PRED_FORMAT1(\ 4557 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value) 4558 #define EXPECT_THAT(value, matcher) EXPECT_PRED_FORMAT1(\ 4559 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value) 4568 #include "gmock/internal/custom/gmock-matchers.h" 4570 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_ const char * p
Definition: gmock-matchers_test.cc:3613
#define GMOCK_KIND_OF_(type)
Definition: gmock-internal-utils.h:172
Definition: gmock-actions.h:59
AssertionResult AssertionFailure()
Definition: gtest.cc:1028
int * count
Definition: gmock_stress_test.cc:96
#define GTEST_DISABLE_MSC_WARNINGS_POP_()
Definition: gtest-port.h:314
::std::string PrintToString(const T &value)
Definition: gtest-printers.h:914
#define GTEST_LOG_(severity)
Definition: gtest-port.h:990
Definition: gmock-internal-utils.h:131
std::string Print(const T &value)
Definition: googletest-printers-test.cc:233
::std::vector< ::std::string > Strings
Definition: gtest-printers.h:871
#define GTEST_API_
Definition: gtest-port.h:759
#define GTEST_REMOVE_REFERENCE_(T)
Definition: gtest-internal.h:869
int i
Definition: gmock-matchers_test.cc:711
bool_constant< true > true_type
Definition: gtest-port.h:1932
clear all close all load ct GNMSLog0 mat reformat t
Definition: gmock-internal-utils.h:52
#define GTEST_DISALLOW_ASSIGN_(type)
Definition: gtest-port.h:683
std::string GetTypeName()
Definition: gtest-type-util.h:80
bool_constant< false > false_type
Definition: gtest-port.h:1931
std::decay< FunctionImpl >::type Invoke(FunctionImpl &&function_impl)
Definition: gmock-actions.h:1084
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 GMOCK_MAYBE_5046_) namespace testing
Definition: gmock-matchers.h:68
#define GTEST_COMPILE_ASSERT_(expr, msg)
Definition: gtest-port.h:859
AssertionResult AssertionSuccess()
Definition: gtest.cc:1023
#define GTEST_CHECK_(condition)
Definition: gtest-port.h:1014
constexpr size_t n
Definition: MatrixInversionTest.cpp:14
def Iter(n, format, sep='')
Definition: gen_gtest_pred_impl.py:188
void UniversalPrint(const T &value,::std::ostream *os)
Definition: gtest-printers.h:864
#define GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement)
Definition: gtest-internal.h:1309
GTEST_API_ std::string FormatMatcherDescription(bool negation, const char *matcher_name, const Strings ¶m_values)
Definition: gmock-matchers.cc:52
int value
Definition: gmock-matchers_test.cc:657
int x
Definition: gmock-matchers_test.cc:3610
type
Definition: upload.py:443
GTEST_API_ ElementMatcherPairs FindMaxBipartiteMatching(const MatchMatrix &g)
Definition: gmock-matchers.cc:228
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
Definition: gtest-port.h:688
bool StaticAssertTypeEq()
Definition: gtest.h:2281
#define GMOCK_MAYBE_5046_
Definition: gmock-matchers.h:65
#define GTEST_REMOVE_CONST_(T)
Definition: gtest-internal.h:890
def StartsWith(lines, pos, string)
Definition: pump.py:163
Iter ArrayAwareFind(Iter begin, Iter end, const Element &elem)
Definition: gtest-internal.h:1035
def Run(command)
Definition: googletest-break-on-failure-unittest.py:76
const Pointer::element_type * GetRawPointer(const Pointer &p)
Definition: gmock-internal-utils.h:92
AssertionResult IsNull(const char *str)
Definition: gtest-unittest-api_test.cc:139
#define GTEST_REMOVE_REFERENCE_AND_CONST_(T)
Definition: gtest-internal.h:894