42 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_ 43 #define GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_ 47 #include <initializer_list> 54 #include <type_traits> 57 #include "gmock/internal/gmock-internal-utils.h" 58 #include "gmock/internal/gmock-port.h" 59 #include "gtest/gtest.h" 61 GTEST_DISABLE_MSC_WARNINGS_PUSH_(
81 class StringMatchResultListener :
public MatchResultListener {
83 StringMatchResultListener() : MatchResultListener(&ss_) {}
86 std::string str()
const {
return ss_.str(); }
89 void Clear() { ss_.str(
""); }
92 ::std::stringstream ss_;
94 GTEST_DISALLOW_COPY_AND_ASSIGN_(StringMatchResultListener);
111 template <
typename T,
typename M>
112 class MatcherCastImpl {
114 static Matcher<T> Cast(
const M& polymorphic_matcher_or_value) {
129 polymorphic_matcher_or_value,
131 std::is_convertible<M, Matcher<T> >::value>(),
133 std::is_convertible<M, T>::value>());
137 template <
bool Ignore>
138 static Matcher<T> CastImpl(
const M& polymorphic_matcher_or_value,
139 BooleanConstant<true> ,
140 BooleanConstant<Ignore>) {
149 return polymorphic_matcher_or_value;
155 static Matcher<T> CastImpl(
156 const M& value, BooleanConstant<false> ,
157 BooleanConstant<true> ) {
158 return Matcher<T>(ImplicitCast_<T>(value));
171 static Matcher<T> CastImpl(
172 const M& value, BooleanConstant<false> ,
173 BooleanConstant<false> );
179 template <
typename T,
typename U>
180 class MatcherCastImpl<T, Matcher<U> > {
182 static Matcher<T> Cast(
const Matcher<U>& source_matcher) {
183 return Matcher<T>(
new Impl(source_matcher));
187 class Impl :
public MatcherInterface<T> {
189 explicit Impl(
const Matcher<U>& source_matcher)
190 : source_matcher_(source_matcher) {}
193 bool MatchAndExplain(T x, MatchResultListener* listener)
const override {
194 using FromType =
typename std::remove_cv<
typename std::remove_pointer<
195 typename std::remove_reference<T>::type>::type>::type;
196 using ToType =
typename std::remove_cv<
typename std::remove_pointer<
197 typename std::remove_reference<U>::type>::type>::type;
202 (std::is_pointer<
typename std::remove_reference<T>::type>::value !=
203 std::is_pointer<
typename std::remove_reference<U>::type>::value) ||
204 std::is_same<FromType, ToType>::value ||
205 !std::is_base_of<FromType, ToType>::value,
206 "Can't implicitly convert from <base> to <derived>");
208 return source_matcher_.MatchAndExplain(static_cast<U>(x), listener);
211 void DescribeTo(::std::ostream* os)
const override {
212 source_matcher_.DescribeTo(os);
215 void DescribeNegationTo(::std::ostream* os)
const override {
216 source_matcher_.DescribeNegationTo(os);
220 const Matcher<U> source_matcher_;
222 GTEST_DISALLOW_ASSIGN_(Impl);
228 template <
typename T>
229 class MatcherCastImpl<T, Matcher<T> > {
231 static Matcher<T> Cast(
const Matcher<T>& matcher) {
return matcher; }
240 template <
typename T,
typename M>
241 inline Matcher<T> MatcherCast(
const M& matcher) {
242 return internal::MatcherCastImpl<T, M>::Cast(matcher);
249 template <
typename T>
250 class SafeMatcherCastImpl {
254 template <
typename M>
255 static inline Matcher<T> Cast(
const M& polymorphic_matcher_or_value) {
256 return internal::MatcherCastImpl<T, M>::Cast(polymorphic_matcher_or_value);
268 template <
typename U>
269 static inline Matcher<T> Cast(
const Matcher<U>& matcher) {
271 GTEST_COMPILE_ASSERT_((std::is_convertible<T, U>::value),
272 "T must be implicitly convertible to U");
275 GTEST_COMPILE_ASSERT_(
276 internal::is_reference<T>::value || !internal::is_reference<U>::value,
277 cannot_convert_non_reference_arg_to_reference);
280 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(T) RawT;
281 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(U) RawU;
282 const
bool kTIsOther = GMOCK_KIND_OF_(RawT) == internal::kOther;
283 const
bool kUIsOther = GMOCK_KIND_OF_(RawU) == internal::kOther;
284 GTEST_COMPILE_ASSERT_(
285 kTIsOther || kUIsOther ||
286 (internal::LosslessArithmeticConvertible<RawT, RawU>::value),
287 conversion_of_arithmetic_types_must_be_lossless);
288 return MatcherCast<T>(matcher);
292 template <typename T, typename M>
293 inline Matcher<T> SafeMatcherCast(const M& polymorphic_matcher) {
294 return SafeMatcherCastImpl<T>::Cast(polymorphic_matcher);
298 template <
typename T>
306 inline void PrintIfNotEmpty(
const std::string& explanation,
307 ::std::ostream* os) {
308 if (explanation !=
"" && os !=
nullptr) {
309 *os <<
", " << explanation;
316 inline bool IsReadableTypeName(
const std::string& type_name) {
319 return (type_name.length() <= 20 ||
320 type_name.find_first_of(
"<(") == std::string::npos);
328 template <
typename Value,
typename T>
329 bool MatchPrintAndExplain(Value& value,
const Matcher<T>& matcher,
330 MatchResultListener* listener) {
331 if (!listener->IsInterested()) {
334 return matcher.Matches(value);
337 StringMatchResultListener inner_listener;
338 const bool match = matcher.MatchAndExplain(value, &inner_listener);
340 UniversalPrint(value, listener->stream());
342 const std::string& type_name = GetTypeName<Value>();
343 if (IsReadableTypeName(type_name))
344 *listener->stream() <<
" (of type " << type_name <<
")";
346 PrintIfNotEmpty(inner_listener.str(), listener->stream());
359 template <
typename MatcherTuple,
typename ValueTuple>
360 static bool Matches(
const MatcherTuple& matcher_tuple,
361 const ValueTuple& value_tuple) {
362 return TuplePrefix<N - 1>::Matches(matcher_tuple, value_tuple) &&
363 std::get<N - 1>(matcher_tuple).Matches(std::get<N - 1>(value_tuple));
370 template <
typename MatcherTuple,
typename ValueTuple>
371 static void ExplainMatchFailuresTo(
const MatcherTuple& matchers,
372 const ValueTuple& values,
373 ::std::ostream* os) {
375 TuplePrefix<N - 1>::ExplainMatchFailuresTo(matchers, values, os);
379 typename std::tuple_element<N - 1, MatcherTuple>::type matcher =
380 std::get<N - 1>(matchers);
381 typedef typename std::tuple_element<N - 1, ValueTuple>::type Value;
382 const Value& value = std::get<N - 1>(values);
383 StringMatchResultListener listener;
384 if (!matcher.MatchAndExplain(value, &listener)) {
385 *os <<
" Expected arg #" << N - 1 <<
": ";
386 std::get<N - 1>(matchers).DescribeTo(os);
387 *os <<
"\n Actual: ";
393 internal::UniversalPrint(value, os);
394 PrintIfNotEmpty(listener.str(), os);
402 class TuplePrefix<0> {
404 template <
typename MatcherTuple,
typename ValueTuple>
405 static bool Matches(
const MatcherTuple& ,
406 const ValueTuple& ) {
410 template <
typename MatcherTuple,
typename ValueTuple>
411 static void ExplainMatchFailuresTo(
const MatcherTuple& ,
421 template <
typename MatcherTuple,
typename ValueTuple>
422 bool TupleMatches(
const MatcherTuple& matcher_tuple,
423 const ValueTuple& value_tuple) {
426 GTEST_COMPILE_ASSERT_(std::tuple_size<MatcherTuple>::value ==
427 std::tuple_size<ValueTuple>::value,
428 matcher_and_value_have_different_numbers_of_fields);
429 return TuplePrefix<std::tuple_size<ValueTuple>::value>::Matches(matcher_tuple,
435 template <
typename MatcherTuple,
typename ValueTuple>
436 void ExplainMatchFailureTupleTo(
const MatcherTuple& matchers,
437 const ValueTuple& values,
438 ::std::ostream* os) {
439 TuplePrefix<std::tuple_size<MatcherTuple>::value>::ExplainMatchFailuresTo(
440 matchers, values, os);
447 template <
typename Tuple,
typename Func,
typename OutIter>
448 class TransformTupleValuesHelper {
450 typedef ::std::tuple_size<Tuple> TupleSize;
455 static OutIter Run(Func f,
const Tuple& t, OutIter out) {
456 return IterateOverTuple<Tuple, TupleSize::value>()(f, t, out);
460 template <
typename Tup,
size_t kRemainingSize>
461 struct IterateOverTuple {
462 OutIter operator() (Func f,
const Tup& t, OutIter out)
const {
463 *out++ = f(::std::get<TupleSize::value - kRemainingSize>(t));
464 return IterateOverTuple<Tup, kRemainingSize - 1>()(f, t, out);
467 template <
typename Tup>
468 struct IterateOverTuple<Tup, 0> {
469 OutIter operator() (Func ,
const Tup& , OutIter out)
const {
478 template <
typename Tuple,
typename Func,
typename OutIter>
479 OutIter TransformTupleValues(Func f,
const Tuple& t, OutIter out) {
480 return TransformTupleValuesHelper<Tuple, Func, OutIter>::Run(f, t, out);
484 template <
typename T>
485 class AnyMatcherImpl :
public MatcherInterface<const T&> {
487 bool MatchAndExplain(
const T& ,
488 MatchResultListener* )
const override {
491 void DescribeTo(::std::ostream* os)
const override { *os <<
"is anything"; }
492 void DescribeNegationTo(::std::ostream* os)
const override {
496 *os <<
"never matches";
504 class AnythingMatcher {
506 template <
typename T>
507 operator Matcher<T>()
const {
return A<T>(); }
512 class IsNullMatcher {
514 template <
typename Po
inter>
515 bool MatchAndExplain(
const Pointer& p,
516 MatchResultListener* )
const {
520 void DescribeTo(::std::ostream* os)
const { *os <<
"is NULL"; }
521 void DescribeNegationTo(::std::ostream* os)
const {
528 class NotNullMatcher {
530 template <
typename Po
inter>
531 bool MatchAndExplain(
const Pointer& p,
532 MatchResultListener* )
const {
536 void DescribeTo(::std::ostream* os)
const { *os <<
"isn't NULL"; }
537 void DescribeNegationTo(::std::ostream* os)
const {
555 template <
typename T>
558 template <
typename T>
559 class RefMatcher<T&> {
569 explicit RefMatcher(T& x) : object_(x) {}
571 template <
typename Super>
572 operator Matcher<Super&>()
const {
578 return MakeMatcher(
new Impl<Super>(object_));
582 template <
typename Super>
583 class Impl :
public MatcherInterface<Super&> {
585 explicit Impl(Super& x) : object_(x) {}
589 bool MatchAndExplain(Super& x,
590 MatchResultListener* listener)
const override {
591 *listener <<
"which is located @" <<
static_cast<const void*
>(&x);
592 return &x == &object_;
595 void DescribeTo(::std::ostream* os)
const override {
596 *os <<
"references the variable ";
597 UniversalPrinter<Super&>::Print(object_, os);
600 void DescribeNegationTo(::std::ostream* os)
const override {
601 *os <<
"does not reference the variable ";
602 UniversalPrinter<Super&>::Print(object_, os);
606 const Super& object_;
608 GTEST_DISALLOW_ASSIGN_(Impl);
613 GTEST_DISALLOW_ASSIGN_(RefMatcher);
617 inline bool CaseInsensitiveCStringEquals(
const char* lhs,
const char* rhs) {
618 return String::CaseInsensitiveCStringEquals(lhs, rhs);
621 inline bool CaseInsensitiveCStringEquals(
const wchar_t* lhs,
622 const wchar_t* rhs) {
623 return String::CaseInsensitiveWideCStringEquals(lhs, rhs);
628 template <
typename StringType>
629 bool CaseInsensitiveStringEquals(
const StringType& s1,
630 const StringType& s2) {
632 if (!CaseInsensitiveCStringEquals(s1.c_str(), s2.c_str())) {
637 const typename StringType::value_type nul = 0;
638 const size_t i1 = s1.find(nul), i2 = s2.find(nul);
641 if (i1 == StringType::npos || i2 == StringType::npos) {
646 return CaseInsensitiveStringEquals(s1.substr(i1 + 1), s2.substr(i2 + 1));
652 template <
typename StringType>
653 class StrEqualityMatcher {
655 StrEqualityMatcher(
const StringType& str,
bool expect_eq,
657 : string_(str), expect_eq_(expect_eq), case_sensitive_(case_sensitive) {}
660 bool MatchAndExplain(
const absl::string_view& s,
661 MatchResultListener* listener)
const {
664 const StringType& str = string(s);
665 return MatchAndExplain(str, listener);
667 #endif // GTEST_HAS_ABSL 674 template <
typename CharType>
675 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
679 return MatchAndExplain(StringType(s), listener);
686 template <
typename MatcheeStringType>
687 bool MatchAndExplain(
const MatcheeStringType& s,
688 MatchResultListener* )
const {
689 const StringType& s2(s);
690 const bool eq = case_sensitive_ ? s2 == string_ :
691 CaseInsensitiveStringEquals(s2, string_);
692 return expect_eq_ == eq;
695 void DescribeTo(::std::ostream* os)
const {
696 DescribeToHelper(expect_eq_, os);
699 void DescribeNegationTo(::std::ostream* os)
const {
700 DescribeToHelper(!expect_eq_, os);
704 void DescribeToHelper(
bool expect_eq, ::std::ostream* os)
const {
705 *os << (expect_eq ?
"is " :
"isn't ");
707 if (!case_sensitive_) {
708 *os <<
"(ignoring case) ";
710 UniversalPrint(string_, os);
713 const StringType string_;
714 const bool expect_eq_;
715 const bool case_sensitive_;
717 GTEST_DISALLOW_ASSIGN_(StrEqualityMatcher);
723 template <
typename StringType>
724 class HasSubstrMatcher {
726 explicit HasSubstrMatcher(
const StringType& substring)
727 : substring_(substring) {}
730 bool MatchAndExplain(
const absl::string_view& s,
731 MatchResultListener* listener)
const {
734 const StringType& str = string(s);
735 return MatchAndExplain(str, listener);
737 #endif // GTEST_HAS_ABSL 744 template <
typename CharType>
745 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
746 return s !=
nullptr && MatchAndExplain(StringType(s), listener);
753 template <
typename MatcheeStringType>
754 bool MatchAndExplain(
const MatcheeStringType& s,
755 MatchResultListener* )
const {
756 const StringType& s2(s);
757 return s2.find(substring_) != StringType::npos;
761 void DescribeTo(::std::ostream* os)
const {
762 *os <<
"has substring ";
763 UniversalPrint(substring_, os);
766 void DescribeNegationTo(::std::ostream* os)
const {
767 *os <<
"has no substring ";
768 UniversalPrint(substring_, os);
772 const StringType substring_;
774 GTEST_DISALLOW_ASSIGN_(HasSubstrMatcher);
780 template <
typename StringType>
781 class StartsWithMatcher {
783 explicit StartsWithMatcher(
const StringType& prefix) : prefix_(prefix) {
787 bool MatchAndExplain(
const absl::string_view& s,
788 MatchResultListener* listener)
const {
791 const StringType& str = string(s);
792 return MatchAndExplain(str, listener);
794 #endif // GTEST_HAS_ABSL 801 template <
typename CharType>
802 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
803 return s !=
nullptr && MatchAndExplain(StringType(s), listener);
810 template <
typename MatcheeStringType>
811 bool MatchAndExplain(
const MatcheeStringType& s,
812 MatchResultListener* )
const {
813 const StringType& s2(s);
814 return s2.length() >= prefix_.length() &&
815 s2.substr(0, prefix_.length()) == prefix_;
818 void DescribeTo(::std::ostream* os)
const {
819 *os <<
"starts with ";
820 UniversalPrint(prefix_, os);
823 void DescribeNegationTo(::std::ostream* os)
const {
824 *os <<
"doesn't start with ";
825 UniversalPrint(prefix_, os);
829 const StringType prefix_;
831 GTEST_DISALLOW_ASSIGN_(StartsWithMatcher);
837 template <
typename StringType>
838 class EndsWithMatcher {
840 explicit EndsWithMatcher(
const StringType& suffix) : suffix_(suffix) {}
843 bool MatchAndExplain(
const absl::string_view& s,
844 MatchResultListener* listener)
const {
847 const StringType& str = string(s);
848 return MatchAndExplain(str, listener);
850 #endif // GTEST_HAS_ABSL 857 template <
typename CharType>
858 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
859 return s !=
nullptr && MatchAndExplain(StringType(s), listener);
866 template <
typename MatcheeStringType>
867 bool MatchAndExplain(
const MatcheeStringType& s,
868 MatchResultListener* )
const {
869 const StringType& s2(s);
870 return s2.length() >= suffix_.length() &&
871 s2.substr(s2.length() - suffix_.length()) == suffix_;
874 void DescribeTo(::std::ostream* os)
const {
876 UniversalPrint(suffix_, os);
879 void DescribeNegationTo(::std::ostream* os)
const {
880 *os <<
"doesn't end with ";
881 UniversalPrint(suffix_, os);
885 const StringType suffix_;
887 GTEST_DISALLOW_ASSIGN_(EndsWithMatcher);
898 template <
typename D,
typename Op>
899 class PairMatchBase {
901 template <
typename T1,
typename T2>
902 operator Matcher<::std::tuple<T1, T2>>()
const {
903 return Matcher<::std::tuple<T1, T2>>(
new Impl<const ::std::tuple<T1, T2>&>);
905 template <
typename T1,
typename T2>
906 operator Matcher<const ::std::tuple<T1, T2>&>()
const {
907 return MakeMatcher(
new Impl<const ::std::tuple<T1, T2>&>);
911 static ::std::ostream& GetDesc(::std::ostream& os) {
912 return os << D::Desc();
915 template <
typename Tuple>
916 class Impl :
public MatcherInterface<Tuple> {
918 bool MatchAndExplain(Tuple args,
919 MatchResultListener* )
const override {
920 return Op()(::std::get<0>(args), ::std::get<1>(args));
922 void DescribeTo(::std::ostream* os)
const override {
923 *os <<
"are " << GetDesc;
925 void DescribeNegationTo(::std::ostream* os)
const override {
926 *os <<
"aren't " << GetDesc;
931 class Eq2Matcher :
public PairMatchBase<Eq2Matcher, AnyEq> {
933 static const char* Desc() {
return "an equal pair"; }
935 class Ne2Matcher :
public PairMatchBase<Ne2Matcher, AnyNe> {
937 static const char* Desc() {
return "an unequal pair"; }
939 class Lt2Matcher :
public PairMatchBase<Lt2Matcher, AnyLt> {
941 static const char* Desc() {
return "a pair where the first < the second"; }
943 class Gt2Matcher :
public PairMatchBase<Gt2Matcher, AnyGt> {
945 static const char* Desc() {
return "a pair where the first > the second"; }
947 class Le2Matcher :
public PairMatchBase<Le2Matcher, AnyLe> {
949 static const char* Desc() {
return "a pair where the first <= the second"; }
951 class Ge2Matcher :
public PairMatchBase<Ge2Matcher, AnyGe> {
953 static const char* Desc() {
return "a pair where the first >= the second"; }
960 template <
typename T>
961 class NotMatcherImpl :
public MatcherInterface<const T&> {
963 explicit NotMatcherImpl(
const Matcher<T>& matcher)
964 : matcher_(matcher) {}
966 bool MatchAndExplain(
const T& x,
967 MatchResultListener* listener)
const override {
968 return !matcher_.MatchAndExplain(x, listener);
971 void DescribeTo(::std::ostream* os)
const override {
972 matcher_.DescribeNegationTo(os);
975 void DescribeNegationTo(::std::ostream* os)
const override {
976 matcher_.DescribeTo(os);
980 const Matcher<T> matcher_;
982 GTEST_DISALLOW_ASSIGN_(NotMatcherImpl);
987 template <
typename InnerMatcher>
990 explicit NotMatcher(InnerMatcher matcher) : matcher_(matcher) {}
994 template <
typename T>
995 operator Matcher<T>()
const {
996 return Matcher<T>(
new NotMatcherImpl<T>(SafeMatcherCast<T>(matcher_)));
1000 InnerMatcher matcher_;
1002 GTEST_DISALLOW_ASSIGN_(NotMatcher);
1009 template <
typename T>
1010 class AllOfMatcherImpl :
public MatcherInterface<const T&> {
1012 explicit AllOfMatcherImpl(std::vector<Matcher<T> > matchers)
1013 : matchers_(std::move(matchers)) {}
1015 void DescribeTo(::std::ostream* os)
const override {
1017 for (
size_t i = 0; i < matchers_.size(); ++i) {
1018 if (i != 0) *os <<
") and (";
1019 matchers_[i].DescribeTo(os);
1024 void DescribeNegationTo(::std::ostream* os)
const override {
1026 for (
size_t i = 0; i < matchers_.size(); ++i) {
1027 if (i != 0) *os <<
") or (";
1028 matchers_[i].DescribeNegationTo(os);
1033 bool MatchAndExplain(
const T& x,
1034 MatchResultListener* listener)
const override {
1037 std::string all_match_result;
1039 for (
size_t i = 0; i < matchers_.size(); ++i) {
1040 StringMatchResultListener slistener;
1041 if (matchers_[i].MatchAndExplain(x, &slistener)) {
1042 if (all_match_result.empty()) {
1043 all_match_result = slistener.str();
1045 std::string result = slistener.str();
1046 if (!result.empty()) {
1047 all_match_result +=
", and ";
1048 all_match_result += result;
1052 *listener << slistener.str();
1058 *listener << all_match_result;
1063 const std::vector<Matcher<T> > matchers_;
1065 GTEST_DISALLOW_ASSIGN_(AllOfMatcherImpl);
1072 template <
template <
typename T>
class CombiningMatcher,
typename... Args>
1073 class VariadicMatcher {
1075 VariadicMatcher(
const Args&... matchers)
1076 : matchers_(matchers...) {
1077 static_assert(
sizeof...(Args) > 0,
"Must have at least one matcher.");
1083 template <
typename T>
1084 operator Matcher<T>()
const {
1085 std::vector<Matcher<T> > values;
1086 CreateVariadicMatcher<T>(&values, std::integral_constant<size_t, 0>());
1087 return Matcher<T>(
new CombiningMatcher<T>(std::move(values)));
1091 template <
typename T,
size_t I>
1092 void CreateVariadicMatcher(std::vector<Matcher<T> >* values,
1093 std::integral_constant<size_t, I>)
const {
1094 values->push_back(SafeMatcherCast<T>(std::get<I>(matchers_)));
1095 CreateVariadicMatcher<T>(values, std::integral_constant<size_t, I + 1>());
1098 template <
typename T>
1099 void CreateVariadicMatcher(
1100 std::vector<Matcher<T> >*,
1101 std::integral_constant<
size_t,
sizeof...(Args)>)
const {}
1103 std::tuple<Args...> matchers_;
1105 GTEST_DISALLOW_ASSIGN_(VariadicMatcher);
1108 template <
typename... Args>
1109 using AllOfMatcher = VariadicMatcher<AllOfMatcherImpl, Args...>;
1115 template <
typename T>
1116 class AnyOfMatcherImpl :
public MatcherInterface<const T&> {
1118 explicit AnyOfMatcherImpl(std::vector<Matcher<T> > matchers)
1119 : matchers_(std::move(matchers)) {}
1121 void DescribeTo(::std::ostream* os)
const override {
1123 for (
size_t i = 0; i < matchers_.size(); ++i) {
1124 if (i != 0) *os <<
") or (";
1125 matchers_[i].DescribeTo(os);
1130 void DescribeNegationTo(::std::ostream* os)
const override {
1132 for (
size_t i = 0; i < matchers_.size(); ++i) {
1133 if (i != 0) *os <<
") and (";
1134 matchers_[i].DescribeNegationTo(os);
1139 bool MatchAndExplain(
const T& x,
1140 MatchResultListener* listener)
const override {
1141 std::string no_match_result;
1145 for (
size_t i = 0; i < matchers_.size(); ++i) {
1146 StringMatchResultListener slistener;
1147 if (matchers_[i].MatchAndExplain(x, &slistener)) {
1148 *listener << slistener.str();
1151 if (no_match_result.empty()) {
1152 no_match_result = slistener.str();
1154 std::string result = slistener.str();
1155 if (!result.empty()) {
1156 no_match_result +=
", and ";
1157 no_match_result += result;
1164 *listener << no_match_result;
1169 const std::vector<Matcher<T> > matchers_;
1171 GTEST_DISALLOW_ASSIGN_(AnyOfMatcherImpl);
1175 template <
typename... Args>
1176 using AnyOfMatcher = VariadicMatcher<AnyOfMatcherImpl, Args...>;
1179 template <
template <
class>
class MatcherImpl,
typename T>
1180 class SomeOfArrayMatcher {
1184 template <
typename Iter>
1185 SomeOfArrayMatcher(Iter first, Iter last) : matchers_(first, last) {}
1187 template <
typename U>
1188 operator Matcher<U>()
const {
1189 using RawU =
typename std::decay<U>::type;
1190 std::vector<Matcher<RawU>> matchers;
1191 for (
const auto& matcher : matchers_) {
1192 matchers.push_back(MatcherCast<RawU>(matcher));
1194 return Matcher<U>(
new MatcherImpl<RawU>(std::move(matchers)));
1198 const ::std::vector<T> matchers_;
1200 GTEST_DISALLOW_ASSIGN_(SomeOfArrayMatcher);
1203 template <
typename T>
1204 using AllOfArrayMatcher = SomeOfArrayMatcher<AllOfMatcherImpl, T>;
1206 template <
typename T>
1207 using AnyOfArrayMatcher = SomeOfArrayMatcher<AnyOfMatcherImpl, T>;
1211 template <
typename Predicate>
1212 class TrulyMatcher {
1214 explicit TrulyMatcher(Predicate pred) : predicate_(pred) {}
1220 template <
typename T>
1221 bool MatchAndExplain(T& x,
1222 MatchResultListener* )
const {
1234 void DescribeTo(::std::ostream* os)
const {
1235 *os <<
"satisfies the given predicate";
1238 void DescribeNegationTo(::std::ostream* os)
const {
1239 *os <<
"doesn't satisfy the given predicate";
1243 Predicate predicate_;
1245 GTEST_DISALLOW_ASSIGN_(TrulyMatcher);
1250 template <
typename M>
1251 class MatcherAsPredicate {
1253 explicit MatcherAsPredicate(M matcher) : matcher_(matcher) {}
1261 template <
typename T>
1262 bool operator()(
const T& x)
const {
1277 return MatcherCast<const T&>(matcher_).Matches(x);
1283 GTEST_DISALLOW_ASSIGN_(MatcherAsPredicate);
1288 template <
typename M>
1289 class PredicateFormatterFromMatcher {
1291 explicit PredicateFormatterFromMatcher(M m) : matcher_(std::move(m)) {}
1296 template <
typename T>
1297 AssertionResult operator()(
const char* value_text,
const T& x)
const {
1309 const Matcher<const T&> matcher = SafeMatcherCast<const T&>(matcher_);
1313 if (matcher.Matches(x)) {
1314 return AssertionSuccess();
1317 ::std::stringstream ss;
1318 ss <<
"Value of: " << value_text <<
"\n" 1320 matcher.DescribeTo(&ss);
1323 StringMatchResultListener listener;
1324 if (MatchPrintAndExplain(x, matcher, &listener)) {
1325 ss <<
"\n The matcher failed on the initial attempt; but passed when " 1326 "rerun to generate the explanation.";
1328 ss <<
"\n Actual: " << listener.str();
1329 return AssertionFailure() << ss.str();
1335 GTEST_DISALLOW_ASSIGN_(PredicateFormatterFromMatcher);
1342 template <
typename M>
1343 inline PredicateFormatterFromMatcher<M>
1344 MakePredicateFormatterFromMatcher(M matcher) {
1345 return PredicateFormatterFromMatcher<M>(std::move(matcher));
1352 template <
typename FloatType>
1353 class FloatingEqMatcher {
1361 FloatingEqMatcher(FloatType expected,
bool nan_eq_nan) :
1362 expected_(expected), nan_eq_nan_(nan_eq_nan), max_abs_error_(-1) {
1368 FloatingEqMatcher(FloatType expected,
bool nan_eq_nan,
1369 FloatType max_abs_error)
1370 : expected_(expected),
1371 nan_eq_nan_(nan_eq_nan),
1372 max_abs_error_(max_abs_error) {
1373 GTEST_CHECK_(max_abs_error >= 0)
1374 <<
", where max_abs_error is" << max_abs_error;
1378 template <
typename T>
1379 class Impl :
public MatcherInterface<T> {
1381 Impl(FloatType expected,
bool nan_eq_nan, FloatType max_abs_error)
1382 : expected_(expected),
1383 nan_eq_nan_(nan_eq_nan),
1384 max_abs_error_(max_abs_error) {}
1386 bool MatchAndExplain(T value,
1387 MatchResultListener* listener)
const override {
1388 const FloatingPoint<FloatType> actual(value), expected(expected_);
1391 if (actual.is_nan() || expected.is_nan()) {
1392 if (actual.is_nan() && expected.is_nan()) {
1398 if (HasMaxAbsError()) {
1403 if (value == expected_) {
1407 const FloatType diff = value - expected_;
1408 if (fabs(diff) <= max_abs_error_) {
1412 if (listener->IsInterested()) {
1413 *listener <<
"which is " << diff <<
" from " << expected_;
1417 return actual.AlmostEquals(expected);
1421 void DescribeTo(::std::ostream* os)
const override {
1425 const ::std::streamsize old_precision = os->precision(
1426 ::std::numeric_limits<FloatType>::digits10 + 2);
1427 if (FloatingPoint<FloatType>(expected_).is_nan()) {
1431 *os <<
"never matches";
1434 *os <<
"is approximately " << expected_;
1435 if (HasMaxAbsError()) {
1436 *os <<
" (absolute error <= " << max_abs_error_ <<
")";
1439 os->precision(old_precision);
1442 void DescribeNegationTo(::std::ostream* os)
const override {
1444 const ::std::streamsize old_precision = os->precision(
1445 ::std::numeric_limits<FloatType>::digits10 + 2);
1446 if (FloatingPoint<FloatType>(expected_).is_nan()) {
1450 *os <<
"is anything";
1453 *os <<
"isn't approximately " << expected_;
1454 if (HasMaxAbsError()) {
1455 *os <<
" (absolute error > " << max_abs_error_ <<
")";
1459 os->precision(old_precision);
1463 bool HasMaxAbsError()
const {
1464 return max_abs_error_ >= 0;
1467 const FloatType expected_;
1468 const bool nan_eq_nan_;
1470 const FloatType max_abs_error_;
1472 GTEST_DISALLOW_ASSIGN_(Impl);
1481 operator Matcher<FloatType>()
const {
1483 new Impl<FloatType>(expected_, nan_eq_nan_, max_abs_error_));
1486 operator Matcher<const FloatType&>()
const {
1488 new Impl<const FloatType&>(expected_, nan_eq_nan_, max_abs_error_));
1491 operator Matcher<FloatType&>()
const {
1493 new Impl<FloatType&>(expected_, nan_eq_nan_, max_abs_error_));
1497 const FloatType expected_;
1498 const bool nan_eq_nan_;
1500 const FloatType max_abs_error_;
1502 GTEST_DISALLOW_ASSIGN_(FloatingEqMatcher);
1510 template <
typename FloatType>
1511 class FloatingEq2Matcher {
1513 FloatingEq2Matcher() { Init(-1,
false); }
1515 explicit FloatingEq2Matcher(
bool nan_eq_nan) { Init(-1, nan_eq_nan); }
1517 explicit FloatingEq2Matcher(FloatType max_abs_error) {
1518 Init(max_abs_error,
false);
1521 FloatingEq2Matcher(FloatType max_abs_error,
bool nan_eq_nan) {
1522 Init(max_abs_error, nan_eq_nan);
1525 template <
typename T1,
typename T2>
1526 operator Matcher<::std::tuple<T1, T2>>()
const {
1528 new Impl<::std::tuple<T1, T2>>(max_abs_error_, nan_eq_nan_));
1530 template <
typename T1,
typename T2>
1531 operator Matcher<const ::std::tuple<T1, T2>&>()
const {
1533 new Impl<const ::std::tuple<T1, T2>&>(max_abs_error_, nan_eq_nan_));
1537 static ::std::ostream& GetDesc(::std::ostream& os) {
1538 return os <<
"an almost-equal pair";
1541 template <
typename Tuple>
1542 class Impl :
public MatcherInterface<Tuple> {
1544 Impl(FloatType max_abs_error,
bool nan_eq_nan) :
1545 max_abs_error_(max_abs_error),
1546 nan_eq_nan_(nan_eq_nan) {}
1548 bool MatchAndExplain(Tuple args,
1549 MatchResultListener* listener)
const override {
1550 if (max_abs_error_ == -1) {
1551 FloatingEqMatcher<FloatType> fm(::std::get<0>(args), nan_eq_nan_);
1552 return static_cast<Matcher<FloatType>
>(fm).MatchAndExplain(
1553 ::std::get<1>(args), listener);
1555 FloatingEqMatcher<FloatType> fm(::std::get<0>(args), nan_eq_nan_,
1557 return static_cast<Matcher<FloatType>
>(fm).MatchAndExplain(
1558 ::std::get<1>(args), listener);
1561 void DescribeTo(::std::ostream* os)
const override {
1562 *os <<
"are " << GetDesc;
1564 void DescribeNegationTo(::std::ostream* os)
const override {
1565 *os <<
"aren't " << GetDesc;
1569 FloatType max_abs_error_;
1570 const bool nan_eq_nan_;
1573 void Init(FloatType max_abs_error_val,
bool nan_eq_nan_val) {
1574 max_abs_error_ = max_abs_error_val;
1575 nan_eq_nan_ = nan_eq_nan_val;
1577 FloatType max_abs_error_;
1583 template <
typename InnerMatcher>
1584 class PointeeMatcher {
1586 explicit PointeeMatcher(
const InnerMatcher& matcher) : matcher_(matcher) {}
1596 template <
typename Po
inter>
1597 operator Matcher<Pointer>()
const {
1598 return Matcher<Pointer>(
new Impl<const Pointer&>(matcher_));
1603 template <
typename Po
inter>
1604 class Impl :
public MatcherInterface<Pointer> {
1606 typedef typename PointeeOf<GTEST_REMOVE_CONST_(
1607 GTEST_REMOVE_REFERENCE_(Pointer))>::type Pointee;
1609 explicit Impl(
const InnerMatcher& matcher)
1610 : matcher_(MatcherCast<const Pointee&>(matcher)) {}
1612 void DescribeTo(::std::ostream* os)
const override {
1613 *os <<
"points to a value that ";
1614 matcher_.DescribeTo(os);
1617 void DescribeNegationTo(::std::ostream* os)
const override {
1618 *os <<
"does not point to a value that ";
1619 matcher_.DescribeTo(os);
1622 bool MatchAndExplain(Pointer pointer,
1623 MatchResultListener* listener)
const override {
1624 if (GetRawPointer(pointer) ==
nullptr)
return false;
1626 *listener <<
"which points to ";
1627 return MatchPrintAndExplain(*pointer, matcher_, listener);
1631 const Matcher<const Pointee&> matcher_;
1633 GTEST_DISALLOW_ASSIGN_(Impl);
1636 const InnerMatcher matcher_;
1638 GTEST_DISALLOW_ASSIGN_(PointeeMatcher);
1648 template <
typename To>
1649 class WhenDynamicCastToMatcherBase {
1651 explicit WhenDynamicCastToMatcherBase(
const Matcher<To>& matcher)
1652 : matcher_(matcher) {}
1654 void DescribeTo(::std::ostream* os)
const {
1655 GetCastTypeDescription(os);
1656 matcher_.DescribeTo(os);
1659 void DescribeNegationTo(::std::ostream* os)
const {
1660 GetCastTypeDescription(os);
1661 matcher_.DescribeNegationTo(os);
1665 const Matcher<To> matcher_;
1667 static std::string GetToName() {
1668 return GetTypeName<To>();
1672 static void GetCastTypeDescription(::std::ostream* os) {
1673 *os <<
"when dynamic_cast to " << GetToName() <<
", ";
1676 GTEST_DISALLOW_ASSIGN_(WhenDynamicCastToMatcherBase);
1681 template <
typename To>
1682 class WhenDynamicCastToMatcher :
public WhenDynamicCastToMatcherBase<To> {
1684 explicit WhenDynamicCastToMatcher(
const Matcher<To>& matcher)
1685 : WhenDynamicCastToMatcherBase<To>(matcher) {}
1687 template <
typename From>
1688 bool MatchAndExplain(From from, MatchResultListener* listener)
const {
1689 To to =
dynamic_cast<To
>(from);
1690 return MatchPrintAndExplain(to, this->matcher_, listener);
1696 template <
typename To>
1697 class WhenDynamicCastToMatcher<To&> :
public WhenDynamicCastToMatcherBase<To&> {
1699 explicit WhenDynamicCastToMatcher(
const Matcher<To&>& matcher)
1700 : WhenDynamicCastToMatcherBase<To&>(matcher) {}
1702 template <
typename From>
1703 bool MatchAndExplain(From& from, MatchResultListener* listener)
const {
1705 To* to =
dynamic_cast<To*
>(&from);
1706 if (to ==
nullptr) {
1707 *listener <<
"which cannot be dynamic_cast to " << this->GetToName();
1710 return MatchPrintAndExplain(*to, this->matcher_, listener);
1713 #endif // GTEST_HAS_RTTI 1717 template <
typename Class,
typename FieldType>
1718 class FieldMatcher {
1720 FieldMatcher(FieldType Class::*field,
1721 const Matcher<const FieldType&>& matcher)
1722 : field_(field), matcher_(matcher), whose_field_(
"whose given field ") {}
1724 FieldMatcher(
const std::string& field_name, FieldType Class::*field,
1725 const Matcher<const FieldType&>& matcher)
1728 whose_field_(
"whose field `" + field_name +
"` ") {}
1730 void DescribeTo(::std::ostream* os)
const {
1731 *os <<
"is an object " << whose_field_;
1732 matcher_.DescribeTo(os);
1735 void DescribeNegationTo(::std::ostream* os)
const {
1736 *os <<
"is an object " << whose_field_;
1737 matcher_.DescribeNegationTo(os);
1740 template <
typename T>
1741 bool MatchAndExplain(
const T& value, MatchResultListener* listener)
const {
1744 return MatchAndExplainImpl(
1745 typename std::is_pointer<GTEST_REMOVE_CONST_(T)>::type(), value,
1750 bool MatchAndExplainImpl(std::false_type ,
1752 MatchResultListener* listener)
const {
1753 *listener << whose_field_ <<
"is ";
1754 return MatchPrintAndExplain(obj.*field_, matcher_, listener);
1757 bool MatchAndExplainImpl(std::true_type ,
const Class* p,
1758 MatchResultListener* listener)
const {
1759 if (p ==
nullptr)
return false;
1761 *listener <<
"which points to an object ";
1765 return MatchAndExplainImpl(std::false_type(), *p, listener);
1768 const FieldType Class::*field_;
1769 const Matcher<const FieldType&> matcher_;
1773 const std::string whose_field_;
1775 GTEST_DISALLOW_ASSIGN_(FieldMatcher);
1783 template <
typename Class,
typename PropertyType,
typename Property>
1784 class PropertyMatcher {
1786 typedef const PropertyType& RefToConstProperty;
1788 PropertyMatcher(Property property,
const Matcher<RefToConstProperty>& matcher)
1789 : property_(property),
1791 whose_property_(
"whose given property ") {}
1793 PropertyMatcher(
const std::string& property_name, Property property,
1794 const Matcher<RefToConstProperty>& matcher)
1795 : property_(property),
1797 whose_property_(
"whose property `" + property_name +
"` ") {}
1799 void DescribeTo(::std::ostream* os)
const {
1800 *os <<
"is an object " << whose_property_;
1801 matcher_.DescribeTo(os);
1804 void DescribeNegationTo(::std::ostream* os)
const {
1805 *os <<
"is an object " << whose_property_;
1806 matcher_.DescribeNegationTo(os);
1809 template <
typename T>
1810 bool MatchAndExplain(
const T&value, MatchResultListener* listener)
const {
1811 return MatchAndExplainImpl(
1812 typename std::is_pointer<GTEST_REMOVE_CONST_(T)>::type(), value,
1817 bool MatchAndExplainImpl(std::false_type ,
1819 MatchResultListener* listener)
const {
1820 *listener << whose_property_ <<
"is ";
1823 RefToConstProperty result = (obj.*property_)();
1824 return MatchPrintAndExplain(result, matcher_, listener);
1827 bool MatchAndExplainImpl(std::true_type ,
const Class* p,
1828 MatchResultListener* listener)
const {
1829 if (p ==
nullptr)
return false;
1831 *listener <<
"which points to an object ";
1835 return MatchAndExplainImpl(std::false_type(), *p, listener);
1839 const Matcher<RefToConstProperty> matcher_;
1843 const std::string whose_property_;
1845 GTEST_DISALLOW_ASSIGN_(PropertyMatcher);
1850 template <
typename Functor>
1851 struct CallableTraits {
1852 typedef Functor StorageType;
1854 static void CheckIsValid(Functor ) {}
1856 template <
typename T>
1857 static auto Invoke(Functor f, T arg) -> decltype(f(arg)) {
return f(arg); }
1861 template <
typename ArgType,
typename ResType>
1862 struct CallableTraits<ResType(*)(ArgType)> {
1863 typedef ResType ResultType;
1864 typedef ResType(*StorageType)(ArgType);
1866 static void CheckIsValid(ResType(*f)(ArgType)) {
1867 GTEST_CHECK_(f !=
nullptr)
1868 <<
"NULL function pointer is passed into ResultOf().";
1870 template <
typename T>
1871 static ResType Invoke(ResType(*f)(ArgType), T arg) {
1878 template <
typename Callable,
typename InnerMatcher>
1879 class ResultOfMatcher {
1881 ResultOfMatcher(Callable callable, InnerMatcher matcher)
1882 : callable_(std::move(callable)), matcher_(std::move(matcher)) {
1883 CallableTraits<Callable>::CheckIsValid(callable_);
1886 template <
typename T>
1887 operator Matcher<T>()
const {
1888 return Matcher<T>(
new Impl<T>(callable_, matcher_));
1892 typedef typename CallableTraits<Callable>::StorageType CallableStorageType;
1894 template <
typename T>
1895 class Impl :
public MatcherInterface<T> {
1896 using ResultType = decltype(CallableTraits<Callable>::template Invoke<T>(
1897 std::declval<CallableStorageType>(), std::declval<T>()));
1900 template <
typename M>
1901 Impl(
const CallableStorageType& callable,
const M& matcher)
1902 : callable_(callable), matcher_(MatcherCast<ResultType>(matcher)) {}
1904 void DescribeTo(::std::ostream* os)
const override {
1905 *os <<
"is mapped by the given callable to a value that ";
1906 matcher_.DescribeTo(os);
1909 void DescribeNegationTo(::std::ostream* os)
const override {
1910 *os <<
"is mapped by the given callable to a value that ";
1911 matcher_.DescribeNegationTo(os);
1914 bool MatchAndExplain(T obj, MatchResultListener* listener)
const override {
1915 *listener <<
"which is mapped by the given callable to ";
1921 CallableTraits<Callable>::template Invoke<T>(callable_, obj);
1922 return MatchPrintAndExplain(result, matcher_, listener);
1931 mutable CallableStorageType callable_;
1932 const Matcher<ResultType> matcher_;
1934 GTEST_DISALLOW_ASSIGN_(Impl);
1937 const CallableStorageType callable_;
1938 const InnerMatcher matcher_;
1940 GTEST_DISALLOW_ASSIGN_(ResultOfMatcher);
1944 template <
typename SizeMatcher>
1945 class SizeIsMatcher {
1947 explicit SizeIsMatcher(
const SizeMatcher& size_matcher)
1948 : size_matcher_(size_matcher) {
1951 template <
typename Container>
1952 operator Matcher<Container>()
const {
1953 return Matcher<Container>(
new Impl<const Container&>(size_matcher_));
1956 template <
typename Container>
1957 class Impl :
public MatcherInterface<Container> {
1959 using SizeType = decltype(std::declval<Container>().size());
1960 explicit Impl(
const SizeMatcher& size_matcher)
1961 : size_matcher_(MatcherCast<SizeType>(size_matcher)) {}
1963 void DescribeTo(::std::ostream* os)
const override {
1965 size_matcher_.DescribeTo(os);
1967 void DescribeNegationTo(::std::ostream* os)
const override {
1969 size_matcher_.DescribeNegationTo(os);
1972 bool MatchAndExplain(Container container,
1973 MatchResultListener* listener)
const override {
1974 SizeType size = container.size();
1975 StringMatchResultListener size_listener;
1976 const bool result = size_matcher_.MatchAndExplain(size, &size_listener);
1978 <<
"whose size " << size << (result ?
" matches" :
" doesn't match");
1979 PrintIfNotEmpty(size_listener.str(), listener->stream());
1984 const Matcher<SizeType> size_matcher_;
1985 GTEST_DISALLOW_ASSIGN_(Impl);
1989 const SizeMatcher size_matcher_;
1990 GTEST_DISALLOW_ASSIGN_(SizeIsMatcher);
1995 template <
typename DistanceMatcher>
1996 class BeginEndDistanceIsMatcher {
1998 explicit BeginEndDistanceIsMatcher(
const DistanceMatcher& distance_matcher)
1999 : distance_matcher_(distance_matcher) {}
2001 template <
typename Container>
2002 operator Matcher<Container>()
const {
2003 return Matcher<Container>(
new Impl<const Container&>(distance_matcher_));
2006 template <
typename Container>
2007 class Impl :
public MatcherInterface<Container> {
2009 typedef internal::StlContainerView<
2010 GTEST_REMOVE_REFERENCE_AND_CONST_(Container)> ContainerView;
2011 typedef typename std::iterator_traits<
2012 typename ContainerView::type::const_iterator>::difference_type
2014 explicit Impl(
const DistanceMatcher& distance_matcher)
2015 : distance_matcher_(MatcherCast<DistanceType>(distance_matcher)) {}
2017 void DescribeTo(::std::ostream* os)
const override {
2018 *os <<
"distance between begin() and end() ";
2019 distance_matcher_.DescribeTo(os);
2021 void DescribeNegationTo(::std::ostream* os)
const override {
2022 *os <<
"distance between begin() and end() ";
2023 distance_matcher_.DescribeNegationTo(os);
2026 bool MatchAndExplain(Container container,
2027 MatchResultListener* listener)
const override {
2030 DistanceType distance = std::distance(begin(container), end(container));
2031 StringMatchResultListener distance_listener;
2033 distance_matcher_.MatchAndExplain(distance, &distance_listener);
2034 *listener <<
"whose distance between begin() and end() " << distance
2035 << (result ?
" matches" :
" doesn't match");
2036 PrintIfNotEmpty(distance_listener.str(), listener->stream());
2041 const Matcher<DistanceType> distance_matcher_;
2042 GTEST_DISALLOW_ASSIGN_(Impl);
2046 const DistanceMatcher distance_matcher_;
2047 GTEST_DISALLOW_ASSIGN_(BeginEndDistanceIsMatcher);
2060 template <
typename Container>
2061 class ContainerEqMatcher {
2063 typedef internal::StlContainerView<Container> View;
2064 typedef typename View::type StlContainer;
2065 typedef typename View::const_reference StlContainerReference;
2069 explicit ContainerEqMatcher(
const Container& expected)
2070 : expected_(View::Copy(expected)) {
2073 (void)testing::StaticAssertTypeEq<Container,
2074 GTEST_REMOVE_REFERENCE_AND_CONST_(Container)>();
2077 void DescribeTo(::std::ostream* os)
const {
2079 UniversalPrint(expected_, os);
2081 void DescribeNegationTo(::std::ostream* os)
const {
2082 *os <<
"does not equal ";
2083 UniversalPrint(expected_, os);
2086 template <
typename LhsContainer>
2087 bool MatchAndExplain(
const LhsContainer& lhs,
2088 MatchResultListener* listener)
const {
2091 typedef internal::StlContainerView<GTEST_REMOVE_CONST_(LhsContainer)>
2093 typedef typename LhsView::type LhsStlContainer;
2094 StlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2095 if (lhs_stl_container == expected_)
2098 ::std::ostream*
const os = listener->stream();
2099 if (os !=
nullptr) {
2101 bool printed_header =
false;
2102 for (
typename LhsStlContainer::const_iterator it =
2103 lhs_stl_container.begin();
2104 it != lhs_stl_container.end(); ++it) {
2105 if (internal::ArrayAwareFind(expected_.begin(), expected_.end(), *it) ==
2107 if (printed_header) {
2110 *os <<
"which has these unexpected elements: ";
2111 printed_header =
true;
2113 UniversalPrint(*it, os);
2118 bool printed_header2 =
false;
2119 for (
typename StlContainer::const_iterator it = expected_.begin();
2120 it != expected_.end(); ++it) {
2121 if (internal::ArrayAwareFind(
2122 lhs_stl_container.begin(), lhs_stl_container.end(), *it) ==
2123 lhs_stl_container.end()) {
2124 if (printed_header2) {
2127 *os << (printed_header ?
",\nand" :
"which")
2128 <<
" doesn't have these expected elements: ";
2129 printed_header2 =
true;
2131 UniversalPrint(*it, os);
2140 const StlContainer expected_;
2142 GTEST_DISALLOW_ASSIGN_(ContainerEqMatcher);
2146 struct LessComparator {
2147 template <
typename T,
typename U>
2148 bool operator()(
const T& lhs,
const U& rhs)
const {
return lhs < rhs; }
2152 template <
typename Comparator,
typename ContainerMatcher>
2153 class WhenSortedByMatcher {
2155 WhenSortedByMatcher(
const Comparator& comparator,
2156 const ContainerMatcher& matcher)
2157 : comparator_(comparator), matcher_(matcher) {}
2159 template <
typename LhsContainer>
2160 operator Matcher<LhsContainer>()
const {
2161 return MakeMatcher(
new Impl<LhsContainer>(comparator_, matcher_));
2164 template <
typename LhsContainer>
2165 class Impl :
public MatcherInterface<LhsContainer> {
2167 typedef internal::StlContainerView<
2168 GTEST_REMOVE_REFERENCE_AND_CONST_(LhsContainer)> LhsView;
2169 typedef typename LhsView::type LhsStlContainer;
2170 typedef typename LhsView::const_reference LhsStlContainerReference;
2173 typedef typename RemoveConstFromKey<
2174 typename LhsStlContainer::value_type>::type LhsValue;
2176 Impl(
const Comparator& comparator,
const ContainerMatcher& matcher)
2177 : comparator_(comparator), matcher_(matcher) {}
2179 void DescribeTo(::std::ostream* os)
const override {
2180 *os <<
"(when sorted) ";
2181 matcher_.DescribeTo(os);
2184 void DescribeNegationTo(::std::ostream* os)
const override {
2185 *os <<
"(when sorted) ";
2186 matcher_.DescribeNegationTo(os);
2189 bool MatchAndExplain(LhsContainer lhs,
2190 MatchResultListener* listener)
const override {
2191 LhsStlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2192 ::std::vector<LhsValue> sorted_container(lhs_stl_container.begin(),
2193 lhs_stl_container.end());
2195 sorted_container.begin(), sorted_container.end(), comparator_);
2197 if (!listener->IsInterested()) {
2200 return matcher_.Matches(sorted_container);
2203 *listener <<
"which is ";
2204 UniversalPrint(sorted_container, listener->stream());
2205 *listener <<
" when sorted";
2207 StringMatchResultListener inner_listener;
2208 const bool match = matcher_.MatchAndExplain(sorted_container,
2210 PrintIfNotEmpty(inner_listener.str(), listener->stream());
2215 const Comparator comparator_;
2216 const Matcher<const ::std::vector<LhsValue>&> matcher_;
2218 GTEST_DISALLOW_COPY_AND_ASSIGN_(Impl);
2222 const Comparator comparator_;
2223 const ContainerMatcher matcher_;
2225 GTEST_DISALLOW_ASSIGN_(WhenSortedByMatcher);
2232 template <
typename TupleMatcher,
typename RhsContainer>
2233 class PointwiseMatcher {
2234 GTEST_COMPILE_ASSERT_(
2235 !IsHashTable<GTEST_REMOVE_REFERENCE_AND_CONST_(RhsContainer)>::value,
2236 use_UnorderedPointwise_with_hash_tables);
2239 typedef internal::StlContainerView<RhsContainer> RhsView;
2240 typedef typename RhsView::type RhsStlContainer;
2241 typedef typename RhsStlContainer::value_type RhsValue;
2245 PointwiseMatcher(
const TupleMatcher& tuple_matcher,
const RhsContainer& rhs)
2246 : tuple_matcher_(tuple_matcher), rhs_(RhsView::Copy(rhs)) {
2249 (void)testing::StaticAssertTypeEq<RhsContainer,
2250 GTEST_REMOVE_REFERENCE_AND_CONST_(RhsContainer)>();
2253 template <
typename LhsContainer>
2254 operator Matcher<LhsContainer>()
const {
2255 GTEST_COMPILE_ASSERT_(
2256 !IsHashTable<GTEST_REMOVE_REFERENCE_AND_CONST_(LhsContainer)>::value,
2257 use_UnorderedPointwise_with_hash_tables);
2259 return Matcher<LhsContainer>(
2260 new Impl<const LhsContainer&>(tuple_matcher_, rhs_));
2263 template <
typename LhsContainer>
2264 class Impl :
public MatcherInterface<LhsContainer> {
2266 typedef internal::StlContainerView<
2267 GTEST_REMOVE_REFERENCE_AND_CONST_(LhsContainer)> LhsView;
2268 typedef typename LhsView::type LhsStlContainer;
2269 typedef typename LhsView::const_reference LhsStlContainerReference;
2270 typedef typename LhsStlContainer::value_type LhsValue;
2275 typedef ::std::tuple<const LhsValue&, const RhsValue&> InnerMatcherArg;
2277 Impl(
const TupleMatcher& tuple_matcher,
const RhsStlContainer& rhs)
2279 : mono_tuple_matcher_(SafeMatcherCast<InnerMatcherArg>(tuple_matcher)),
2282 void DescribeTo(::std::ostream* os)
const override {
2283 *os <<
"contains " << rhs_.size()
2284 <<
" values, where each value and its corresponding value in ";
2285 UniversalPrinter<RhsStlContainer>::Print(rhs_, os);
2287 mono_tuple_matcher_.DescribeTo(os);
2289 void DescribeNegationTo(::std::ostream* os)
const override {
2290 *os <<
"doesn't contain exactly " << rhs_.size()
2291 <<
" values, or contains a value x at some index i" 2292 <<
" where x and the i-th value of ";
2293 UniversalPrint(rhs_, os);
2295 mono_tuple_matcher_.DescribeNegationTo(os);
2298 bool MatchAndExplain(LhsContainer lhs,
2299 MatchResultListener* listener)
const override {
2300 LhsStlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2301 const size_t actual_size = lhs_stl_container.size();
2302 if (actual_size != rhs_.size()) {
2303 *listener <<
"which contains " << actual_size <<
" values";
2307 typename LhsStlContainer::const_iterator left = lhs_stl_container.begin();
2308 typename RhsStlContainer::const_iterator right = rhs_.begin();
2309 for (
size_t i = 0; i != actual_size; ++i, ++left, ++right) {
2310 if (listener->IsInterested()) {
2311 StringMatchResultListener inner_listener;
2315 if (!mono_tuple_matcher_.MatchAndExplain(
2316 InnerMatcherArg(ImplicitCast_<const LhsValue&>(*left),
2317 ImplicitCast_<const RhsValue&>(*right)),
2319 *listener <<
"where the value pair (";
2320 UniversalPrint(*left, listener->stream());
2322 UniversalPrint(*right, listener->stream());
2323 *listener <<
") at index #" << i <<
" don't match";
2324 PrintIfNotEmpty(inner_listener.str(), listener->stream());
2328 if (!mono_tuple_matcher_.Matches(
2329 InnerMatcherArg(ImplicitCast_<const LhsValue&>(*left),
2330 ImplicitCast_<const RhsValue&>(*right))))
2339 const Matcher<InnerMatcherArg> mono_tuple_matcher_;
2340 const RhsStlContainer rhs_;
2342 GTEST_DISALLOW_ASSIGN_(Impl);
2346 const TupleMatcher tuple_matcher_;
2347 const RhsStlContainer rhs_;
2349 GTEST_DISALLOW_ASSIGN_(PointwiseMatcher);
2353 template <
typename Container>
2354 class QuantifierMatcherImpl :
public MatcherInterface<Container> {
2356 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
2357 typedef StlContainerView<RawContainer> View;
2358 typedef typename View::type StlContainer;
2359 typedef typename View::const_reference StlContainerReference;
2360 typedef typename StlContainer::value_type Element;
2362 template <typename InnerMatcher>
2363 explicit QuantifierMatcherImpl(InnerMatcher inner_matcher)
2365 testing::SafeMatcherCast<const Element&>(inner_matcher)) {}
2370 bool MatchAndExplainImpl(
bool all_elements_should_match,
2371 Container container,
2372 MatchResultListener* listener)
const {
2373 StlContainerReference stl_container = View::ConstReference(container);
2375 for (
typename StlContainer::const_iterator it = stl_container.begin();
2376 it != stl_container.end(); ++it, ++i) {
2377 StringMatchResultListener inner_listener;
2378 const bool matches = inner_matcher_.MatchAndExplain(*it, &inner_listener);
2380 if (matches != all_elements_should_match) {
2381 *listener <<
"whose element #" << i
2382 << (matches ?
" matches" :
" doesn't match");
2383 PrintIfNotEmpty(inner_listener.str(), listener->stream());
2384 return !all_elements_should_match;
2387 return all_elements_should_match;
2391 const Matcher<const Element&> inner_matcher_;
2393 GTEST_DISALLOW_ASSIGN_(QuantifierMatcherImpl);
2398 template <
typename Container>
2399 class ContainsMatcherImpl :
public QuantifierMatcherImpl<Container> {
2401 template <
typename InnerMatcher>
2402 explicit ContainsMatcherImpl(InnerMatcher inner_matcher)
2403 : QuantifierMatcherImpl<Container>(inner_matcher) {}
2406 void DescribeTo(::std::ostream* os)
const override {
2407 *os <<
"contains at least one element that ";
2408 this->inner_matcher_.DescribeTo(os);
2411 void DescribeNegationTo(::std::ostream* os)
const override {
2412 *os <<
"doesn't contain any element that ";
2413 this->inner_matcher_.DescribeTo(os);
2416 bool MatchAndExplain(Container container,
2417 MatchResultListener* listener)
const override {
2418 return this->MatchAndExplainImpl(
false, container, listener);
2422 GTEST_DISALLOW_ASSIGN_(ContainsMatcherImpl);
2427 template <
typename Container>
2428 class EachMatcherImpl :
public QuantifierMatcherImpl<Container> {
2430 template <
typename InnerMatcher>
2431 explicit EachMatcherImpl(InnerMatcher inner_matcher)
2432 : QuantifierMatcherImpl<Container>(inner_matcher) {}
2435 void DescribeTo(::std::ostream* os)
const override {
2436 *os <<
"only contains elements that ";
2437 this->inner_matcher_.DescribeTo(os);
2440 void DescribeNegationTo(::std::ostream* os)
const override {
2441 *os <<
"contains some element that ";
2442 this->inner_matcher_.DescribeNegationTo(os);
2445 bool MatchAndExplain(Container container,
2446 MatchResultListener* listener)
const override {
2447 return this->MatchAndExplainImpl(
true, container, listener);
2451 GTEST_DISALLOW_ASSIGN_(EachMatcherImpl);
2455 template <
typename M>
2456 class ContainsMatcher {
2458 explicit ContainsMatcher(M m) : inner_matcher_(m) {}
2460 template <
typename Container>
2461 operator Matcher<Container>()
const {
2462 return Matcher<Container>(
2463 new ContainsMatcherImpl<const Container&>(inner_matcher_));
2467 const M inner_matcher_;
2469 GTEST_DISALLOW_ASSIGN_(ContainsMatcher);
2473 template <
typename M>
2476 explicit EachMatcher(M m) : inner_matcher_(m) {}
2478 template <
typename Container>
2479 operator Matcher<Container>()
const {
2480 return Matcher<Container>(
2481 new EachMatcherImpl<const Container&>(inner_matcher_));
2485 const M inner_matcher_;
2487 GTEST_DISALLOW_ASSIGN_(EachMatcher);
2491 struct Rank0 : Rank1 {};
2493 namespace pair_getters {
2495 template <
typename T>
2496 auto First(T& x, Rank1) -> decltype(get<0>(x)) {
2499 template <
typename T>
2500 auto First(T& x, Rank0) -> decltype((x.first)) {
2504 template <
typename T>
2505 auto Second(T& x, Rank1) -> decltype(get<1>(x)) {
2508 template <
typename T>
2509 auto Second(T& x, Rank0) -> decltype((x.second)) {
2518 template <
typename PairType>
2519 class KeyMatcherImpl :
public MatcherInterface<PairType> {
2521 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(PairType) RawPairType;
2522 typedef typename RawPairType::first_type KeyType;
2524 template <typename InnerMatcher>
2525 explicit KeyMatcherImpl(InnerMatcher inner_matcher)
2527 testing::SafeMatcherCast<const KeyType&>(inner_matcher)) {
2531 bool MatchAndExplain(PairType key_value,
2532 MatchResultListener* listener)
const override {
2533 StringMatchResultListener inner_listener;
2534 const bool match = inner_matcher_.MatchAndExplain(
2535 pair_getters::First(key_value, Rank0()), &inner_listener);
2536 const std::string explanation = inner_listener.str();
2537 if (explanation !=
"") {
2538 *listener <<
"whose first field is a value " << explanation;
2544 void DescribeTo(::std::ostream* os)
const override {
2545 *os <<
"has a key that ";
2546 inner_matcher_.DescribeTo(os);
2550 void DescribeNegationTo(::std::ostream* os)
const override {
2551 *os <<
"doesn't have a key that ";
2552 inner_matcher_.DescribeTo(os);
2556 const Matcher<const KeyType&> inner_matcher_;
2558 GTEST_DISALLOW_ASSIGN_(KeyMatcherImpl);
2562 template <
typename M>
2565 explicit KeyMatcher(M m) : matcher_for_key_(m) {}
2567 template <
typename PairType>
2568 operator Matcher<PairType>()
const {
2569 return Matcher<PairType>(
2570 new KeyMatcherImpl<const PairType&>(matcher_for_key_));
2574 const M matcher_for_key_;
2576 GTEST_DISALLOW_ASSIGN_(KeyMatcher);
2581 template <
typename PairType>
2582 class PairMatcherImpl :
public MatcherInterface<PairType> {
2584 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(PairType) RawPairType;
2585 typedef typename RawPairType::first_type FirstType;
2586 typedef typename RawPairType::second_type SecondType;
2588 template <typename FirstMatcher, typename SecondMatcher>
2589 PairMatcherImpl(FirstMatcher first_matcher, SecondMatcher second_matcher)
2591 testing::SafeMatcherCast<const FirstType&>(first_matcher)),
2593 testing::SafeMatcherCast<const SecondType&>(second_matcher)) {
2597 void DescribeTo(::std::ostream* os)
const override {
2598 *os <<
"has a first field that ";
2599 first_matcher_.DescribeTo(os);
2600 *os <<
", and has a second field that ";
2601 second_matcher_.DescribeTo(os);
2605 void DescribeNegationTo(::std::ostream* os)
const override {
2606 *os <<
"has a first field that ";
2607 first_matcher_.DescribeNegationTo(os);
2608 *os <<
", or has a second field that ";
2609 second_matcher_.DescribeNegationTo(os);
2614 bool MatchAndExplain(PairType a_pair,
2615 MatchResultListener* listener)
const override {
2616 if (!listener->IsInterested()) {
2619 return first_matcher_.Matches(pair_getters::First(a_pair, Rank0())) &&
2620 second_matcher_.Matches(pair_getters::Second(a_pair, Rank0()));
2622 StringMatchResultListener first_inner_listener;
2623 if (!first_matcher_.MatchAndExplain(pair_getters::First(a_pair, Rank0()),
2624 &first_inner_listener)) {
2625 *listener <<
"whose first field does not match";
2626 PrintIfNotEmpty(first_inner_listener.str(), listener->stream());
2629 StringMatchResultListener second_inner_listener;
2630 if (!second_matcher_.MatchAndExplain(pair_getters::Second(a_pair, Rank0()),
2631 &second_inner_listener)) {
2632 *listener <<
"whose second field does not match";
2633 PrintIfNotEmpty(second_inner_listener.str(), listener->stream());
2636 ExplainSuccess(first_inner_listener.str(), second_inner_listener.str(),
2642 void ExplainSuccess(
const std::string& first_explanation,
2643 const std::string& second_explanation,
2644 MatchResultListener* listener)
const {
2645 *listener <<
"whose both fields match";
2646 if (first_explanation !=
"") {
2647 *listener <<
", where the first field is a value " << first_explanation;
2649 if (second_explanation !=
"") {
2651 if (first_explanation !=
"") {
2652 *listener <<
"and ";
2654 *listener <<
"where ";
2656 *listener <<
"the second field is a value " << second_explanation;
2660 const Matcher<const FirstType&> first_matcher_;
2661 const Matcher<const SecondType&> second_matcher_;
2663 GTEST_DISALLOW_ASSIGN_(PairMatcherImpl);
2667 template <
typename FirstMatcher,
typename SecondMatcher>
2670 PairMatcher(FirstMatcher first_matcher, SecondMatcher second_matcher)
2671 : first_matcher_(first_matcher), second_matcher_(second_matcher) {}
2673 template <
typename PairType>
2674 operator Matcher<PairType> ()
const {
2675 return Matcher<PairType>(
2676 new PairMatcherImpl<const PairType&>(first_matcher_, second_matcher_));
2680 const FirstMatcher first_matcher_;
2681 const SecondMatcher second_matcher_;
2683 GTEST_DISALLOW_ASSIGN_(PairMatcher);
2687 template <
typename Container>
2688 class ElementsAreMatcherImpl :
public MatcherInterface<Container> {
2690 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
2691 typedef internal::StlContainerView<RawContainer> View;
2692 typedef typename View::type StlContainer;
2693 typedef typename View::const_reference StlContainerReference;
2694 typedef typename StlContainer::value_type Element;
2698 template <typename InputIter>
2699 ElementsAreMatcherImpl(InputIter first, InputIter last) {
2700 while (first != last) {
2701 matchers_.push_back(MatcherCast<const Element&>(*first++));
2706 void DescribeTo(::std::ostream* os)
const override {
2709 }
else if (count() == 1) {
2710 *os <<
"has 1 element that ";
2711 matchers_[0].DescribeTo(os);
2713 *os <<
"has " << Elements(count()) <<
" where\n";
2714 for (
size_t i = 0; i != count(); ++i) {
2715 *os <<
"element #" << i <<
" ";
2716 matchers_[i].DescribeTo(os);
2717 if (i + 1 < count()) {
2725 void DescribeNegationTo(::std::ostream* os)
const override {
2727 *os <<
"isn't empty";
2731 *os <<
"doesn't have " << Elements(count()) <<
", or\n";
2732 for (
size_t i = 0; i != count(); ++i) {
2733 *os <<
"element #" << i <<
" ";
2734 matchers_[i].DescribeNegationTo(os);
2735 if (i + 1 < count()) {
2741 bool MatchAndExplain(Container container,
2742 MatchResultListener* listener)
const override {
2746 const bool listener_interested = listener->IsInterested();
2749 ::std::vector<std::string> explanations(count());
2750 StlContainerReference stl_container = View::ConstReference(container);
2751 typename StlContainer::const_iterator it = stl_container.begin();
2752 size_t exam_pos = 0;
2753 bool mismatch_found =
false;
2758 for (; it != stl_container.end() && exam_pos != count(); ++it, ++exam_pos) {
2760 if (listener_interested) {
2761 StringMatchResultListener s;
2762 match = matchers_[exam_pos].MatchAndExplain(*it, &s);
2763 explanations[exam_pos] = s.str();
2765 match = matchers_[exam_pos].Matches(*it);
2769 mismatch_found =
true;
2778 size_t actual_count = exam_pos;
2779 for (; it != stl_container.end(); ++it) {
2783 if (actual_count != count()) {
2788 if (listener_interested && (actual_count != 0)) {
2789 *listener <<
"which has " << Elements(actual_count);
2794 if (mismatch_found) {
2796 if (listener_interested) {
2797 *listener <<
"whose element #" << exam_pos <<
" doesn't match";
2798 PrintIfNotEmpty(explanations[exam_pos], listener->stream());
2805 if (listener_interested) {
2806 bool reason_printed =
false;
2807 for (
size_t i = 0; i != count(); ++i) {
2808 const std::string& s = explanations[i];
2810 if (reason_printed) {
2811 *listener <<
",\nand ";
2813 *listener <<
"whose element #" << i <<
" matches, " << s;
2814 reason_printed =
true;
2822 static Message Elements(
size_t count) {
2823 return Message() << count << (count == 1 ?
" element" :
" elements");
2826 size_t count()
const {
return matchers_.size(); }
2828 ::std::vector<Matcher<const Element&> > matchers_;
2830 GTEST_DISALLOW_ASSIGN_(ElementsAreMatcherImpl);
2837 class GTEST_API_ MatchMatrix {
2839 MatchMatrix(
size_t num_elements,
size_t num_matchers)
2840 : num_elements_(num_elements),
2841 num_matchers_(num_matchers),
2842 matched_(num_elements_* num_matchers_, 0) {
2845 size_t LhsSize()
const {
return num_elements_; }
2846 size_t RhsSize()
const {
return num_matchers_; }
2847 bool HasEdge(
size_t ilhs,
size_t irhs)
const {
2848 return matched_[SpaceIndex(ilhs, irhs)] == 1;
2850 void SetEdge(
size_t ilhs,
size_t irhs,
bool b) {
2851 matched_[SpaceIndex(ilhs, irhs)] = b ? 1 : 0;
2861 std::string DebugString()
const;
2864 size_t SpaceIndex(
size_t ilhs,
size_t irhs)
const {
2865 return ilhs * num_matchers_ + irhs;
2868 size_t num_elements_;
2869 size_t num_matchers_;
2874 ::std::vector<char> matched_;
2877 typedef ::std::pair<size_t, size_t> ElementMatcherPair;
2878 typedef ::std::vector<ElementMatcherPair> ElementMatcherPairs;
2882 GTEST_API_ ElementMatcherPairs
2883 FindMaxBipartiteMatching(
const MatchMatrix& g);
2885 struct UnorderedMatcherRequire {
2889 ExactMatch = Superset | Subset,
2896 class GTEST_API_ UnorderedElementsAreMatcherImplBase {
2898 explicit UnorderedElementsAreMatcherImplBase(
2899 UnorderedMatcherRequire::Flags matcher_flags)
2900 : match_flags_(matcher_flags) {}
2905 typedef ::std::vector<const MatcherDescriberInterface*> MatcherDescriberVec;
2908 void DescribeToImpl(::std::ostream* os)
const;
2911 void DescribeNegationToImpl(::std::ostream* os)
const;
2913 bool VerifyMatchMatrix(const ::std::vector<std::string>& element_printouts,
2914 const MatchMatrix& matrix,
2915 MatchResultListener* listener)
const;
2917 bool FindPairing(
const MatchMatrix& matrix,
2918 MatchResultListener* listener)
const;
2920 MatcherDescriberVec& matcher_describers() {
2921 return matcher_describers_;
2924 static Message Elements(
size_t n) {
2925 return Message() << n <<
" element" << (n == 1 ?
"" :
"s");
2928 UnorderedMatcherRequire::Flags match_flags()
const {
return match_flags_; }
2931 UnorderedMatcherRequire::Flags match_flags_;
2932 MatcherDescriberVec matcher_describers_;
2934 GTEST_DISALLOW_ASSIGN_(UnorderedElementsAreMatcherImplBase);
2939 template <
typename Container>
2940 class UnorderedElementsAreMatcherImpl
2941 :
public MatcherInterface<Container>,
2942 public UnorderedElementsAreMatcherImplBase {
2944 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
2945 typedef internal::StlContainerView<RawContainer> View;
2946 typedef typename View::type StlContainer;
2947 typedef typename View::const_reference StlContainerReference;
2948 typedef typename StlContainer::const_iterator StlContainerConstIterator;
2949 typedef typename StlContainer::value_type Element;
2951 template <typename InputIter>
2952 UnorderedElementsAreMatcherImpl(UnorderedMatcherRequire::Flags matcher_flags,
2953 InputIter first, InputIter last)
2954 : UnorderedElementsAreMatcherImplBase(matcher_flags) {
2955 for (; first != last; ++first) {
2956 matchers_.push_back(MatcherCast<const Element&>(*first));
2957 matcher_describers().push_back(matchers_.back().GetDescriber());
2962 void DescribeTo(::std::ostream* os)
const override {
2963 return UnorderedElementsAreMatcherImplBase::DescribeToImpl(os);
2967 void DescribeNegationTo(::std::ostream* os)
const override {
2968 return UnorderedElementsAreMatcherImplBase::DescribeNegationToImpl(os);
2971 bool MatchAndExplain(Container container,
2972 MatchResultListener* listener)
const override {
2973 StlContainerReference stl_container = View::ConstReference(container);
2974 ::std::vector<std::string> element_printouts;
2975 MatchMatrix matrix =
2976 AnalyzeElements(stl_container.begin(), stl_container.end(),
2977 &element_printouts, listener);
2979 if (matrix.LhsSize() == 0 && matrix.RhsSize() == 0) {
2983 if (match_flags() == UnorderedMatcherRequire::ExactMatch) {
2984 if (matrix.LhsSize() != matrix.RhsSize()) {
2989 if (matrix.LhsSize() != 0 && listener->IsInterested()) {
2990 *listener <<
"which has " << Elements(matrix.LhsSize());
2996 return VerifyMatchMatrix(element_printouts, matrix, listener) &&
2997 FindPairing(matrix, listener);
3001 template <
typename ElementIter>
3002 MatchMatrix AnalyzeElements(ElementIter elem_first, ElementIter elem_last,
3003 ::std::vector<std::string>* element_printouts,
3004 MatchResultListener* listener)
const {
3005 element_printouts->clear();
3006 ::std::vector<char> did_match;
3007 size_t num_elements = 0;
3008 for (; elem_first != elem_last; ++num_elements, ++elem_first) {
3009 if (listener->IsInterested()) {
3010 element_printouts->push_back(PrintToString(*elem_first));
3012 for (
size_t irhs = 0; irhs != matchers_.size(); ++irhs) {
3013 did_match.push_back(Matches(matchers_[irhs])(*elem_first));
3017 MatchMatrix matrix(num_elements, matchers_.size());
3018 ::std::vector<char>::const_iterator did_match_iter = did_match.begin();
3019 for (
size_t ilhs = 0; ilhs != num_elements; ++ilhs) {
3020 for (
size_t irhs = 0; irhs != matchers_.size(); ++irhs) {
3021 matrix.SetEdge(ilhs, irhs, *did_match_iter++ != 0);
3027 ::std::vector<Matcher<const Element&> > matchers_;
3029 GTEST_DISALLOW_ASSIGN_(UnorderedElementsAreMatcherImpl);
3034 template <
typename Target>
3035 struct CastAndAppendTransform {
3036 template <
typename Arg>
3037 Matcher<Target> operator()(
const Arg& a)
const {
3038 return MatcherCast<Target>(a);
3043 template <
typename MatcherTuple>
3044 class UnorderedElementsAreMatcher {
3046 explicit UnorderedElementsAreMatcher(
const MatcherTuple& args)
3047 : matchers_(args) {}
3049 template <
typename Container>
3050 operator Matcher<Container>()
const {
3051 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
3052 typedef typename internal::StlContainerView<RawContainer>::type View;
3053 typedef typename View::value_type Element;
3054 typedef ::std::vector<Matcher<const Element&> > MatcherVec;
3055 MatcherVec matchers;
3056 matchers.reserve(::std::tuple_size<MatcherTuple>::value);
3057 TransformTupleValues(CastAndAppendTransform<const Element&>(), matchers_,
3058 ::std::back_inserter(matchers));
3059 return Matcher<Container>(
3060 new UnorderedElementsAreMatcherImpl<const Container&>(
3061 UnorderedMatcherRequire::ExactMatch, matchers.begin(),
3066 const MatcherTuple matchers_;
3067 GTEST_DISALLOW_ASSIGN_(UnorderedElementsAreMatcher);
3071 template <typename MatcherTuple>
3072 class ElementsAreMatcher {
3074 explicit ElementsAreMatcher(
const MatcherTuple& args) : matchers_(args) {}
3076 template <
typename Container>
3077 operator Matcher<Container>()
const {
3078 GTEST_COMPILE_ASSERT_(
3079 !IsHashTable<GTEST_REMOVE_REFERENCE_AND_CONST_(Container)>::value ||
3080 ::std::tuple_size<MatcherTuple>::value < 2,
3081 use_UnorderedElementsAre_with_hash_tables);
3083 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
3084 typedef typename internal::StlContainerView<RawContainer>::type View;
3085 typedef typename View::value_type Element;
3086 typedef ::std::vector<Matcher<const Element&> > MatcherVec;
3087 MatcherVec matchers;
3088 matchers.reserve(::std::tuple_size<MatcherTuple>::value);
3089 TransformTupleValues(CastAndAppendTransform<const Element&>(), matchers_,
3090 ::std::back_inserter(matchers));
3091 return Matcher<Container>(new ElementsAreMatcherImpl<const Container&>(
3092 matchers.begin(), matchers.end()));
3096 const MatcherTuple matchers_;
3097 GTEST_DISALLOW_ASSIGN_(ElementsAreMatcher);
3101 template <typename T>
3102 class UnorderedElementsAreArrayMatcher {
3104 template <
typename Iter>
3105 UnorderedElementsAreArrayMatcher(UnorderedMatcherRequire::Flags match_flags,
3106 Iter first, Iter last)
3107 : match_flags_(match_flags), matchers_(first, last) {}
3109 template <
typename Container>
3110 operator Matcher<Container>()
const {
3111 return Matcher<Container>(
3112 new UnorderedElementsAreMatcherImpl<const Container&>(
3113 match_flags_, matchers_.begin(), matchers_.end()));
3117 UnorderedMatcherRequire::Flags match_flags_;
3118 ::std::vector<T> matchers_;
3120 GTEST_DISALLOW_ASSIGN_(UnorderedElementsAreArrayMatcher);
3124 template <
typename T>
3125 class ElementsAreArrayMatcher {
3127 template <
typename Iter>
3128 ElementsAreArrayMatcher(Iter first, Iter last) : matchers_(first, last) {}
3130 template <
typename Container>
3131 operator Matcher<Container>()
const {
3132 GTEST_COMPILE_ASSERT_(
3133 !IsHashTable<GTEST_REMOVE_REFERENCE_AND_CONST_(Container)>::value,
3134 use_UnorderedElementsAreArray_with_hash_tables);
3136 return Matcher<Container>(
new ElementsAreMatcherImpl<const Container&>(
3137 matchers_.begin(), matchers_.end()));
3141 const ::std::vector<T> matchers_;
3143 GTEST_DISALLOW_ASSIGN_(ElementsAreArrayMatcher);
3155 template <
typename Tuple2Matcher,
typename Second>
3156 class BoundSecondMatcher {
3158 BoundSecondMatcher(
const Tuple2Matcher& tm,
const Second& second)
3159 : tuple2_matcher_(tm), second_value_(second) {}
3161 template <
typename T>
3162 operator Matcher<T>()
const {
3163 return MakeMatcher(
new Impl<T>(tuple2_matcher_, second_value_));
3174 void operator=(
const BoundSecondMatcher& ) {
3175 GTEST_LOG_(FATAL) <<
"BoundSecondMatcher should never be assigned.";
3179 template <
typename T>
3180 class Impl :
public MatcherInterface<T> {
3182 typedef ::std::tuple<T, Second> ArgTuple;
3184 Impl(
const Tuple2Matcher& tm,
const Second& second)
3185 : mono_tuple2_matcher_(SafeMatcherCast<const ArgTuple&>(tm)),
3186 second_value_(second) {}
3188 void DescribeTo(::std::ostream* os)
const override {
3190 UniversalPrint(second_value_, os);
3192 mono_tuple2_matcher_.DescribeTo(os);
3195 bool MatchAndExplain(T x, MatchResultListener* listener)
const override {
3196 return mono_tuple2_matcher_.MatchAndExplain(ArgTuple(x, second_value_),
3201 const Matcher<const ArgTuple&> mono_tuple2_matcher_;
3202 const Second second_value_;
3204 GTEST_DISALLOW_ASSIGN_(Impl);
3207 const Tuple2Matcher tuple2_matcher_;
3208 const Second second_value_;
3215 template <
typename Tuple2Matcher,
typename Second>
3216 BoundSecondMatcher<Tuple2Matcher, Second> MatcherBindSecond(
3217 const Tuple2Matcher& tm,
const Second& second) {
3218 return BoundSecondMatcher<Tuple2Matcher, Second>(tm, second);
3226 GTEST_API_ std::string FormatMatcherDescription(
bool negation,
3227 const char* matcher_name,
3228 const Strings& param_values);
3231 template <
typename ValueMatcher>
3232 class OptionalMatcher {
3234 explicit OptionalMatcher(
const ValueMatcher& value_matcher)
3235 : value_matcher_(value_matcher) {}
3237 template <
typename Optional>
3238 operator Matcher<Optional>()
const {
3239 return Matcher<Optional>(
new Impl<const Optional&>(value_matcher_));
3242 template <
typename Optional>
3243 class Impl :
public MatcherInterface<Optional> {
3245 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Optional) OptionalView;
3246 typedef typename OptionalView::value_type ValueType;
3247 explicit Impl(const ValueMatcher& value_matcher)
3248 : value_matcher_(MatcherCast<ValueType>(value_matcher)) {}
3250 void DescribeTo(::std::ostream* os)
const override {
3252 value_matcher_.DescribeTo(os);
3255 void DescribeNegationTo(::std::ostream* os)
const override {
3257 value_matcher_.DescribeNegationTo(os);
3260 bool MatchAndExplain(Optional optional,
3261 MatchResultListener* listener)
const override {
3263 *listener <<
"which is not engaged";
3266 const ValueType& value = *optional;
3267 StringMatchResultListener value_listener;
3268 const bool match = value_matcher_.MatchAndExplain(value, &value_listener);
3269 *listener <<
"whose value " << PrintToString(value)
3270 << (match ?
" matches" :
" doesn't match");
3271 PrintIfNotEmpty(value_listener.str(), listener->stream());
3276 const Matcher<ValueType> value_matcher_;
3277 GTEST_DISALLOW_ASSIGN_(Impl);
3281 const ValueMatcher value_matcher_;
3282 GTEST_DISALLOW_ASSIGN_(OptionalMatcher);
3285 namespace variant_matcher {
3287 template <
typename T>
3288 void holds_alternative() {}
3289 template <
typename T>
3293 template <
typename T>
3294 class VariantMatcher {
3297 : matcher_(std::move(matcher)) {}
3299 template <
typename Variant>
3300 bool MatchAndExplain(
const Variant& value,
3301 ::testing::MatchResultListener* listener)
const {
3303 if (!listener->IsInterested()) {
3304 return holds_alternative<T>(value) && matcher_.Matches(get<T>(value));
3307 if (!holds_alternative<T>(value)) {
3308 *listener <<
"whose value is not of type '" << GetTypeName() <<
"'";
3312 const T& elem = get<T>(value);
3313 StringMatchResultListener elem_listener;
3314 const bool match = matcher_.MatchAndExplain(elem, &elem_listener);
3315 *listener <<
"whose value " << PrintToString(elem)
3316 << (match ?
" matches" :
" doesn't match");
3317 PrintIfNotEmpty(elem_listener.str(), listener->stream());
3321 void DescribeTo(std::ostream* os)
const {
3322 *os <<
"is a variant<> with value of type '" << GetTypeName()
3323 <<
"' and the value ";
3324 matcher_.DescribeTo(os);
3327 void DescribeNegationTo(std::ostream* os)
const {
3328 *os <<
"is a variant<> with value of type other than '" << GetTypeName()
3329 <<
"' or the value ";
3330 matcher_.DescribeNegationTo(os);
3334 static std::string GetTypeName() {
3336 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(
3337 return internal::GetTypeName<T>());
3339 return "the element type";
3342 const ::testing::Matcher<const T&> matcher_;
3347 namespace any_cast_matcher {
3350 template <
typename T>
3354 template <
typename T>
3355 class AnyCastMatcher {
3357 explicit AnyCastMatcher(const ::testing::Matcher<const T&>& matcher)
3358 : matcher_(matcher) {}
3360 template <
typename AnyType>
3361 bool MatchAndExplain(
const AnyType& value,
3362 ::testing::MatchResultListener* listener)
const {
3363 if (!listener->IsInterested()) {
3364 const T* ptr = any_cast<T>(&value);
3365 return ptr !=
nullptr && matcher_.Matches(*ptr);
3368 const T* elem = any_cast<T>(&value);
3369 if (elem ==
nullptr) {
3370 *listener <<
"whose value is not of type '" << GetTypeName() <<
"'";
3374 StringMatchResultListener elem_listener;
3375 const bool match = matcher_.MatchAndExplain(*elem, &elem_listener);
3376 *listener <<
"whose value " << PrintToString(*elem)
3377 << (match ?
" matches" :
" doesn't match");
3378 PrintIfNotEmpty(elem_listener.str(), listener->stream());
3382 void DescribeTo(std::ostream* os)
const {
3383 *os <<
"is an 'any' type with value of type '" << GetTypeName()
3384 <<
"' and the value ";
3385 matcher_.DescribeTo(os);
3388 void DescribeNegationTo(std::ostream* os)
const {
3389 *os <<
"is an 'any' type with value of type other than '" << GetTypeName()
3390 <<
"' or the value ";
3391 matcher_.DescribeNegationTo(os);
3395 static std::string GetTypeName() {
3397 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(
3398 return internal::GetTypeName<T>());
3400 return "the element type";
3403 const ::testing::Matcher<const T&> matcher_;
3409 template <
class ArgsTuple,
size_t... k>
3410 class ArgsMatcherImpl :
public MatcherInterface<ArgsTuple> {
3412 using RawArgsTuple =
typename std::decay<ArgsTuple>::type;
3413 using SelectedArgs =
3414 std::tuple<typename std::tuple_element<k, RawArgsTuple>::type...>;
3415 using MonomorphicInnerMatcher = Matcher<const SelectedArgs&>;
3417 template <
typename InnerMatcher>
3418 explicit ArgsMatcherImpl(
const InnerMatcher& inner_matcher)
3419 : inner_matcher_(SafeMatcherCast<const SelectedArgs&>(inner_matcher)) {}
3421 bool MatchAndExplain(ArgsTuple args,
3422 MatchResultListener* listener)
const override {
3425 const SelectedArgs& selected_args =
3426 std::forward_as_tuple(std::get<k>(args)...);
3427 if (!listener->IsInterested())
return inner_matcher_.Matches(selected_args);
3429 PrintIndices(listener->stream());
3430 *listener <<
"are " << PrintToString(selected_args);
3432 StringMatchResultListener inner_listener;
3434 inner_matcher_.MatchAndExplain(selected_args, &inner_listener);
3435 PrintIfNotEmpty(inner_listener.str(), listener->stream());
3439 void DescribeTo(::std::ostream* os)
const override {
3440 *os <<
"are a tuple ";
3442 inner_matcher_.DescribeTo(os);
3445 void DescribeNegationTo(::std::ostream* os)
const override {
3446 *os <<
"are a tuple ";
3448 inner_matcher_.DescribeNegationTo(os);
3453 static void PrintIndices(::std::ostream* os) {
3454 *os <<
"whose fields (";
3455 const char* sep =
"";
3458 const char* dummy[] = {
"", (*os << sep <<
"#" << k, sep =
", ")...};
3463 MonomorphicInnerMatcher inner_matcher_;
3466 template <
class InnerMatcher,
size_t... k>
3469 explicit ArgsMatcher(InnerMatcher inner_matcher)
3470 : inner_matcher_(std::move(inner_matcher)) {}
3472 template <
typename ArgsTuple>
3473 operator Matcher<ArgsTuple>()
const {
3474 return MakeMatcher(
new ArgsMatcherImpl<ArgsTuple, k...>(inner_matcher_));
3478 InnerMatcher inner_matcher_;
3498 template <
typename Iter>
3499 inline internal::ElementsAreArrayMatcher<
3500 typename ::std::iterator_traits<Iter>::value_type>
3501 ElementsAreArray(Iter first, Iter last) {
3502 typedef typename ::std::iterator_traits<Iter>::value_type T;
3503 return internal::ElementsAreArrayMatcher<T>(first, last);
3506 template <
typename T>
3507 inline internal::ElementsAreArrayMatcher<T> ElementsAreArray(
3508 const T* pointer,
size_t count) {
3509 return ElementsAreArray(pointer, pointer + count);
3512 template <
typename T,
size_t N>
3513 inline internal::ElementsAreArrayMatcher<T> ElementsAreArray(
3514 const T (&array)[N]) {
3515 return ElementsAreArray(array, N);
3518 template <
typename Container>
3519 inline internal::ElementsAreArrayMatcher<typename Container::value_type>
3520 ElementsAreArray(
const Container& container) {
3521 return ElementsAreArray(container.begin(), container.end());
3524 template <
typename T>
3525 inline internal::ElementsAreArrayMatcher<T>
3526 ElementsAreArray(::std::initializer_list<T> xs) {
3527 return ElementsAreArray(xs.begin(), xs.end());
3543 template <
typename Iter>
3544 inline internal::UnorderedElementsAreArrayMatcher<
3545 typename ::std::iterator_traits<Iter>::value_type>
3546 UnorderedElementsAreArray(Iter first, Iter last) {
3547 typedef typename ::std::iterator_traits<Iter>::value_type T;
3548 return internal::UnorderedElementsAreArrayMatcher<T>(
3549 internal::UnorderedMatcherRequire::ExactMatch, first, last);
3552 template <
typename T>
3553 inline internal::UnorderedElementsAreArrayMatcher<T>
3554 UnorderedElementsAreArray(
const T* pointer,
size_t count) {
3555 return UnorderedElementsAreArray(pointer, pointer + count);
3558 template <
typename T,
size_t N>
3559 inline internal::UnorderedElementsAreArrayMatcher<T>
3560 UnorderedElementsAreArray(
const T (&array)[N]) {
3561 return UnorderedElementsAreArray(array, N);
3564 template <
typename Container>
3565 inline internal::UnorderedElementsAreArrayMatcher<
3566 typename Container::value_type>
3567 UnorderedElementsAreArray(
const Container& container) {
3568 return UnorderedElementsAreArray(container.begin(), container.end());
3571 template <
typename T>
3572 inline internal::UnorderedElementsAreArrayMatcher<T>
3573 UnorderedElementsAreArray(::std::initializer_list<T> xs) {
3574 return UnorderedElementsAreArray(xs.begin(), xs.end());
3586 const internal::AnythingMatcher _ = {};
3588 template <
typename T>
3589 inline Matcher<T> A() {
3590 return Matcher<T>(
new internal::AnyMatcherImpl<T>());
3594 template <
typename T>
3595 inline Matcher<T> An() {
return A<T>(); }
3597 template <
typename T,
typename M>
3598 Matcher<T> internal::MatcherCastImpl<T, M>::CastImpl(
3600 internal::BooleanConstant<false> ,
3601 internal::BooleanConstant<false> ) {
3606 inline PolymorphicMatcher<internal::IsNullMatcher > IsNull() {
3607 return MakePolymorphicMatcher(internal::IsNullMatcher());
3613 inline PolymorphicMatcher<internal::NotNullMatcher > NotNull() {
3614 return MakePolymorphicMatcher(internal::NotNullMatcher());
3619 template <
typename T>
3620 inline internal::RefMatcher<T&> Ref(T& x) {
3621 return internal::RefMatcher<T&>(x);
3626 inline internal::FloatingEqMatcher<double> DoubleEq(
double rhs) {
3627 return internal::FloatingEqMatcher<double>(rhs,
false);
3632 inline internal::FloatingEqMatcher<double> NanSensitiveDoubleEq(
double rhs) {
3633 return internal::FloatingEqMatcher<double>(rhs,
true);
3639 inline internal::FloatingEqMatcher<double> DoubleNear(
3640 double rhs,
double max_abs_error) {
3641 return internal::FloatingEqMatcher<double>(rhs,
false, max_abs_error);
3647 inline internal::FloatingEqMatcher<double> NanSensitiveDoubleNear(
3648 double rhs,
double max_abs_error) {
3649 return internal::FloatingEqMatcher<double>(rhs,
true, max_abs_error);
3654 inline internal::FloatingEqMatcher<float> FloatEq(
float rhs) {
3655 return internal::FloatingEqMatcher<float>(rhs,
false);
3660 inline internal::FloatingEqMatcher<float> NanSensitiveFloatEq(
float rhs) {
3661 return internal::FloatingEqMatcher<float>(rhs,
true);
3667 inline internal::FloatingEqMatcher<float> FloatNear(
3668 float rhs,
float max_abs_error) {
3669 return internal::FloatingEqMatcher<float>(rhs,
false, max_abs_error);
3675 inline internal::FloatingEqMatcher<float> NanSensitiveFloatNear(
3676 float rhs,
float max_abs_error) {
3677 return internal::FloatingEqMatcher<float>(rhs,
true, max_abs_error);
3682 template <
typename InnerMatcher>
3683 inline internal::PointeeMatcher<InnerMatcher> Pointee(
3684 const InnerMatcher& inner_matcher) {
3685 return internal::PointeeMatcher<InnerMatcher>(inner_matcher);
3695 template <
typename To>
3696 inline PolymorphicMatcher<internal::WhenDynamicCastToMatcher<To> >
3697 WhenDynamicCastTo(
const Matcher<To>& inner_matcher) {
3698 return MakePolymorphicMatcher(
3699 internal::WhenDynamicCastToMatcher<To>(inner_matcher));
3701 #endif // GTEST_HAS_RTTI 3707 template <
typename Class,
typename FieldType,
typename FieldMatcher>
3708 inline PolymorphicMatcher<
3709 internal::FieldMatcher<Class, FieldType> > Field(
3710 FieldType Class::*field,
const FieldMatcher& matcher) {
3711 return MakePolymorphicMatcher(
3712 internal::FieldMatcher<Class, FieldType>(
3713 field, MatcherCast<const FieldType&>(matcher)));
3722 template <
typename Class,
typename FieldType,
typename FieldMatcher>
3723 inline PolymorphicMatcher<internal::FieldMatcher<Class, FieldType> > Field(
3724 const std::string& field_name, FieldType Class::*field,
3725 const FieldMatcher& matcher) {
3726 return MakePolymorphicMatcher(internal::FieldMatcher<Class, FieldType>(
3727 field_name, field, MatcherCast<const FieldType&>(matcher)));
3734 template <
typename Class,
typename PropertyType,
typename PropertyMatcher>
3735 inline PolymorphicMatcher<internal::PropertyMatcher<
3736 Class, PropertyType, PropertyType (Class::*)()
const> >
3737 Property(PropertyType (Class::*property)()
const,
3738 const PropertyMatcher& matcher) {
3739 return MakePolymorphicMatcher(
3740 internal::PropertyMatcher<Class, PropertyType,
3741 PropertyType (Class::*)()
const>(
3742 property, MatcherCast<const PropertyType&>(matcher)));
3751 template <
typename Class,
typename PropertyType,
typename PropertyMatcher>
3752 inline PolymorphicMatcher<internal::PropertyMatcher<
3753 Class, PropertyType, PropertyType (Class::*)()
const> >
3754 Property(
const std::string& property_name,
3755 PropertyType (Class::*property)()
const,
3756 const PropertyMatcher& matcher) {
3757 return MakePolymorphicMatcher(
3758 internal::PropertyMatcher<Class, PropertyType,
3759 PropertyType (Class::*)()
const>(
3760 property_name, property, MatcherCast<const PropertyType&>(matcher)));
3764 template <
typename Class,
typename PropertyType,
typename PropertyMatcher>
3765 inline PolymorphicMatcher<internal::PropertyMatcher<
3766 Class, PropertyType, PropertyType (Class::*)()
const &> >
3767 Property(PropertyType (Class::*property)()
const &,
3768 const PropertyMatcher& matcher) {
3769 return MakePolymorphicMatcher(
3770 internal::PropertyMatcher<Class, PropertyType,
3771 PropertyType (Class::*)()
const&>(
3772 property, MatcherCast<const PropertyType&>(matcher)));
3776 template <
typename Class,
typename PropertyType,
typename PropertyMatcher>
3777 inline PolymorphicMatcher<internal::PropertyMatcher<
3778 Class, PropertyType, PropertyType (Class::*)()
const &> >
3779 Property(
const std::string& property_name,
3780 PropertyType (Class::*property)()
const &,
3781 const PropertyMatcher& matcher) {
3782 return MakePolymorphicMatcher(
3783 internal::PropertyMatcher<Class, PropertyType,
3784 PropertyType (Class::*)()
const&>(
3785 property_name, property, MatcherCast<const PropertyType&>(matcher)));
3797 template <
typename Callable,
typename InnerMatcher>
3798 internal::ResultOfMatcher<Callable, InnerMatcher> ResultOf(
3799 Callable callable, InnerMatcher matcher) {
3800 return internal::ResultOfMatcher<Callable, InnerMatcher>(
3801 std::move(callable), std::move(matcher));
3807 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::string> > StrEq(
3808 const std::string& str) {
3809 return MakePolymorphicMatcher(
3810 internal::StrEqualityMatcher<std::string>(str,
true,
true));
3814 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::string> > StrNe(
3815 const std::string& str) {
3816 return MakePolymorphicMatcher(
3817 internal::StrEqualityMatcher<std::string>(str,
false,
true));
3821 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::string> > StrCaseEq(
3822 const std::string& str) {
3823 return MakePolymorphicMatcher(
3824 internal::StrEqualityMatcher<std::string>(str,
true,
false));
3828 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::string> > StrCaseNe(
3829 const std::string& str) {
3830 return MakePolymorphicMatcher(
3831 internal::StrEqualityMatcher<std::string>(str,
false,
false));
3836 inline PolymorphicMatcher<internal::HasSubstrMatcher<std::string> > HasSubstr(
3837 const std::string& substring) {
3838 return MakePolymorphicMatcher(
3839 internal::HasSubstrMatcher<std::string>(substring));
3843 inline PolymorphicMatcher<internal::StartsWithMatcher<std::string> > StartsWith(
3844 const std::string& prefix) {
3845 return MakePolymorphicMatcher(
3846 internal::StartsWithMatcher<std::string>(prefix));
3850 inline PolymorphicMatcher<internal::EndsWithMatcher<std::string> > EndsWith(
3851 const std::string& suffix) {
3852 return MakePolymorphicMatcher(internal::EndsWithMatcher<std::string>(suffix));
3855 #if GTEST_HAS_GLOBAL_WSTRING || GTEST_HAS_STD_WSTRING 3859 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring> > StrEq(
3860 const std::wstring& str) {
3861 return MakePolymorphicMatcher(
3862 internal::StrEqualityMatcher<std::wstring>(str,
true,
true));
3866 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring> > StrNe(
3867 const std::wstring& str) {
3868 return MakePolymorphicMatcher(
3869 internal::StrEqualityMatcher<std::wstring>(str,
false,
true));
3873 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring> >
3874 StrCaseEq(
const std::wstring& str) {
3875 return MakePolymorphicMatcher(
3876 internal::StrEqualityMatcher<std::wstring>(str,
true,
false));
3880 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring> >
3881 StrCaseNe(
const std::wstring& str) {
3882 return MakePolymorphicMatcher(
3883 internal::StrEqualityMatcher<std::wstring>(str,
false,
false));
3888 inline PolymorphicMatcher<internal::HasSubstrMatcher<std::wstring> > HasSubstr(
3889 const std::wstring& substring) {
3890 return MakePolymorphicMatcher(
3891 internal::HasSubstrMatcher<std::wstring>(substring));
3895 inline PolymorphicMatcher<internal::StartsWithMatcher<std::wstring> >
3896 StartsWith(
const std::wstring& prefix) {
3897 return MakePolymorphicMatcher(
3898 internal::StartsWithMatcher<std::wstring>(prefix));
3902 inline PolymorphicMatcher<internal::EndsWithMatcher<std::wstring> > EndsWith(
3903 const std::wstring& suffix) {
3904 return MakePolymorphicMatcher(
3905 internal::EndsWithMatcher<std::wstring>(suffix));
3908 #endif // GTEST_HAS_GLOBAL_WSTRING || GTEST_HAS_STD_WSTRING 3912 inline internal::Eq2Matcher Eq() {
return internal::Eq2Matcher(); }
3916 inline internal::Ge2Matcher Ge() {
return internal::Ge2Matcher(); }
3920 inline internal::Gt2Matcher Gt() {
return internal::Gt2Matcher(); }
3924 inline internal::Le2Matcher Le() {
return internal::Le2Matcher(); }
3928 inline internal::Lt2Matcher Lt() {
return internal::Lt2Matcher(); }
3932 inline internal::Ne2Matcher Ne() {
return internal::Ne2Matcher(); }
3936 inline internal::FloatingEq2Matcher<float> FloatEq() {
3937 return internal::FloatingEq2Matcher<float>();
3942 inline internal::FloatingEq2Matcher<double> DoubleEq() {
3943 return internal::FloatingEq2Matcher<double>();
3948 inline internal::FloatingEq2Matcher<float> NanSensitiveFloatEq() {
3949 return internal::FloatingEq2Matcher<float>(
true);
3954 inline internal::FloatingEq2Matcher<double> NanSensitiveDoubleEq() {
3955 return internal::FloatingEq2Matcher<double>(
true);
3960 inline internal::FloatingEq2Matcher<float> FloatNear(
float max_abs_error) {
3961 return internal::FloatingEq2Matcher<float>(max_abs_error);
3966 inline internal::FloatingEq2Matcher<double> DoubleNear(
double max_abs_error) {
3967 return internal::FloatingEq2Matcher<double>(max_abs_error);
3973 inline internal::FloatingEq2Matcher<float> NanSensitiveFloatNear(
3974 float max_abs_error) {
3975 return internal::FloatingEq2Matcher<float>(max_abs_error,
true);
3981 inline internal::FloatingEq2Matcher<double> NanSensitiveDoubleNear(
3982 double max_abs_error) {
3983 return internal::FloatingEq2Matcher<double>(max_abs_error,
true);
3988 template <
typename InnerMatcher>
3989 inline internal::NotMatcher<InnerMatcher> Not(InnerMatcher m) {
3990 return internal::NotMatcher<InnerMatcher>(m);
3996 template <
typename Predicate>
3997 inline PolymorphicMatcher<internal::TrulyMatcher<Predicate> >
3998 Truly(Predicate pred) {
3999 return MakePolymorphicMatcher(internal::TrulyMatcher<Predicate>(pred));
4008 template <
typename SizeMatcher>
4009 inline internal::SizeIsMatcher<SizeMatcher>
4010 SizeIs(
const SizeMatcher& size_matcher) {
4011 return internal::SizeIsMatcher<SizeMatcher>(size_matcher);
4019 template <
typename DistanceMatcher>
4020 inline internal::BeginEndDistanceIsMatcher<DistanceMatcher>
4021 BeginEndDistanceIs(
const DistanceMatcher& distance_matcher) {
4022 return internal::BeginEndDistanceIsMatcher<DistanceMatcher>(distance_matcher);
4029 template <
typename Container>
4030 inline PolymorphicMatcher<internal::ContainerEqMatcher<
4031 GTEST_REMOVE_CONST_(Container)> >
4032 ContainerEq(
const Container& rhs) {
4035 typedef GTEST_REMOVE_CONST_(Container) RawContainer;
4036 return MakePolymorphicMatcher(
4037 internal::ContainerEqMatcher<RawContainer>(rhs));
4042 template <typename Comparator, typename ContainerMatcher>
4043 inline internal::WhenSortedByMatcher<Comparator, ContainerMatcher>
4044 WhenSortedBy(const Comparator& comparator,
4045 const ContainerMatcher& container_matcher) {
4046 return internal::WhenSortedByMatcher<Comparator, ContainerMatcher>(
4047 comparator, container_matcher);
4052 template <
typename ContainerMatcher>
4053 inline internal::WhenSortedByMatcher<internal::LessComparator, ContainerMatcher>
4054 WhenSorted(
const ContainerMatcher& container_matcher) {
4056 internal::WhenSortedByMatcher<internal::LessComparator, ContainerMatcher>(
4057 internal::LessComparator(), container_matcher);
4066 template <
typename TupleMatcher,
typename Container>
4067 inline internal::PointwiseMatcher<TupleMatcher,
4068 GTEST_REMOVE_CONST_(Container)>
4069 Pointwise(
const TupleMatcher& tuple_matcher,
const Container& rhs) {
4073 typedef GTEST_REMOVE_CONST_(Container) RawContainer;
4074 return internal::PointwiseMatcher<TupleMatcher, RawContainer>(
4075 tuple_matcher, rhs);
4080 template <typename TupleMatcher, typename T>
4081 inline internal::PointwiseMatcher<TupleMatcher, std::vector<T> > Pointwise(
4082 const TupleMatcher& tuple_matcher, std::initializer_list<T> rhs) {
4083 return Pointwise(tuple_matcher, std::vector<T>(rhs));
4098 template <
typename Tuple2Matcher,
typename RhsContainer>
4099 inline internal::UnorderedElementsAreArrayMatcher<
4100 typename internal::BoundSecondMatcher<
4101 Tuple2Matcher,
typename internal::StlContainerView<GTEST_REMOVE_CONST_(
4102 RhsContainer)>::type::value_type> >
4103 UnorderedPointwise(
const Tuple2Matcher& tuple2_matcher,
4104 const RhsContainer& rhs_container) {
4108 typedef GTEST_REMOVE_CONST_(RhsContainer) RawRhsContainer;
4112 typedef typename internal::StlContainerView<RawRhsContainer> RhsView;
4113 typedef typename RhsView::type RhsStlContainer;
4114 typedef typename RhsStlContainer::value_type Second;
4115 const RhsStlContainer& rhs_stl_container =
4116 RhsView::ConstReference(rhs_container);
4119 ::std::vector<internal::BoundSecondMatcher<Tuple2Matcher, Second> > matchers;
4120 for (typename RhsStlContainer::const_iterator it = rhs_stl_container.begin();
4121 it != rhs_stl_container.end(); ++it) {
4123 internal::MatcherBindSecond(tuple2_matcher, *it));
4127 return UnorderedElementsAreArray(matchers);
4132 template <
typename Tuple2Matcher,
typename T>
4133 inline internal::UnorderedElementsAreArrayMatcher<
4134 typename internal::BoundSecondMatcher<Tuple2Matcher, T> >
4135 UnorderedPointwise(
const Tuple2Matcher& tuple2_matcher,
4136 std::initializer_list<T> rhs) {
4137 return UnorderedPointwise(tuple2_matcher, std::vector<T>(rhs));
4159 template <
typename M>
4160 inline internal::ContainsMatcher<M> Contains(M matcher) {
4161 return internal::ContainsMatcher<M>(matcher);
4191 template <
typename Iter>
4192 inline internal::UnorderedElementsAreArrayMatcher<
4193 typename ::std::iterator_traits<Iter>::value_type>
4194 IsSupersetOf(Iter first, Iter last) {
4195 typedef typename ::std::iterator_traits<Iter>::value_type T;
4196 return internal::UnorderedElementsAreArrayMatcher<T>(
4197 internal::UnorderedMatcherRequire::Superset, first, last);
4200 template <
typename T>
4201 inline internal::UnorderedElementsAreArrayMatcher<T> IsSupersetOf(
4202 const T* pointer,
size_t count) {
4203 return IsSupersetOf(pointer, pointer + count);
4206 template <
typename T,
size_t N>
4207 inline internal::UnorderedElementsAreArrayMatcher<T> IsSupersetOf(
4208 const T (&array)[N]) {
4209 return IsSupersetOf(array, N);
4212 template <
typename Container>
4213 inline internal::UnorderedElementsAreArrayMatcher<
4214 typename Container::value_type>
4215 IsSupersetOf(
const Container& container) {
4216 return IsSupersetOf(container.begin(), container.end());
4219 template <
typename T>
4220 inline internal::UnorderedElementsAreArrayMatcher<T> IsSupersetOf(
4221 ::std::initializer_list<T> xs) {
4222 return IsSupersetOf(xs.begin(), xs.end());
4248 template <
typename Iter>
4249 inline internal::UnorderedElementsAreArrayMatcher<
4250 typename ::std::iterator_traits<Iter>::value_type>
4251 IsSubsetOf(Iter first, Iter last) {
4252 typedef typename ::std::iterator_traits<Iter>::value_type T;
4253 return internal::UnorderedElementsAreArrayMatcher<T>(
4254 internal::UnorderedMatcherRequire::Subset, first, last);
4257 template <
typename T>
4258 inline internal::UnorderedElementsAreArrayMatcher<T> IsSubsetOf(
4259 const T* pointer,
size_t count) {
4260 return IsSubsetOf(pointer, pointer + count);
4263 template <
typename T,
size_t N>
4264 inline internal::UnorderedElementsAreArrayMatcher<T> IsSubsetOf(
4265 const T (&array)[N]) {
4266 return IsSubsetOf(array, N);
4269 template <
typename Container>
4270 inline internal::UnorderedElementsAreArrayMatcher<
4271 typename Container::value_type>
4272 IsSubsetOf(
const Container& container) {
4273 return IsSubsetOf(container.begin(), container.end());
4276 template <
typename T>
4277 inline internal::UnorderedElementsAreArrayMatcher<T> IsSubsetOf(
4278 ::std::initializer_list<T> xs) {
4279 return IsSubsetOf(xs.begin(), xs.end());
4309 template <
typename M>
4310 inline internal::EachMatcher<M> Each(M matcher) {
4311 return internal::EachMatcher<M>(matcher);
4317 template <
typename M>
4318 inline internal::KeyMatcher<M> Key(M inner_matcher) {
4319 return internal::KeyMatcher<M>(inner_matcher);
4327 template <
typename FirstMatcher,
typename SecondMatcher>
4328 inline internal::PairMatcher<FirstMatcher, SecondMatcher>
4329 Pair(FirstMatcher first_matcher, SecondMatcher second_matcher) {
4330 return internal::PairMatcher<FirstMatcher, SecondMatcher>(
4331 first_matcher, second_matcher);
4336 template <
typename M>
4337 inline internal::MatcherAsPredicate<M> Matches(M matcher) {
4338 return internal::MatcherAsPredicate<M>(matcher);
4342 template <
typename T,
typename M>
4343 inline bool Value(
const T& value, M matcher) {
4344 return testing::Matches(matcher)(value);
4349 template <
typename T,
typename M>
4350 inline bool ExplainMatchResult(
4351 M matcher,
const T& value, MatchResultListener* listener) {
4352 return SafeMatcherCast<const T&>(matcher).MatchAndExplain(value, listener);
4365 template <
typename T,
typename M>
4366 std::string DescribeMatcher(
const M& matcher,
bool negation =
false) {
4367 ::std::stringstream ss;
4368 Matcher<T> monomorphic_matcher = SafeMatcherCast<T>(matcher);
4370 monomorphic_matcher.DescribeNegationTo(&ss);
4372 monomorphic_matcher.DescribeTo(&ss);
4377 template <
typename... Args>
4378 internal::ElementsAreMatcher<
4379 std::tuple<typename std::decay<const Args&>::type...>>
4380 ElementsAre(
const Args&... matchers) {
4381 return internal::ElementsAreMatcher<
4382 std::tuple<typename std::decay<const Args&>::type...>>(
4383 std::make_tuple(matchers...));
4386 template <
typename... Args>
4387 internal::UnorderedElementsAreMatcher<
4388 std::tuple<typename std::decay<const Args&>::type...>>
4389 UnorderedElementsAre(
const Args&... matchers) {
4390 return internal::UnorderedElementsAreMatcher<
4391 std::tuple<typename std::decay<const Args&>::type...>>(
4392 std::make_tuple(matchers...));
4396 template <
typename... Args>
4397 internal::AllOfMatcher<typename std::decay<const Args&>::type...> AllOf(
4398 const Args&... matchers) {
4399 return internal::AllOfMatcher<typename std::decay<const Args&>::type...>(
4403 template <
typename... Args>
4404 internal::AnyOfMatcher<typename std::decay<const Args&>::type...> AnyOf(
4405 const Args&... matchers) {
4406 return internal::AnyOfMatcher<typename std::decay<const Args&>::type...>(
4432 template <
typename Iter>
4433 inline internal::AnyOfArrayMatcher<
4434 typename ::std::iterator_traits<Iter>::value_type>
4435 AnyOfArray(Iter first, Iter last) {
4436 return internal::AnyOfArrayMatcher<
4437 typename ::std::iterator_traits<Iter>::value_type>(first, last);
4440 template <
typename Iter>
4441 inline internal::AllOfArrayMatcher<
4442 typename ::std::iterator_traits<Iter>::value_type>
4443 AllOfArray(Iter first, Iter last) {
4444 return internal::AllOfArrayMatcher<
4445 typename ::std::iterator_traits<Iter>::value_type>(first, last);
4448 template <
typename T>
4449 inline internal::AnyOfArrayMatcher<T> AnyOfArray(
const T* ptr,
size_t count) {
4450 return AnyOfArray(ptr, ptr + count);
4453 template <
typename T>
4454 inline internal::AllOfArrayMatcher<T> AllOfArray(
const T* ptr,
size_t count) {
4455 return AllOfArray(ptr, ptr + count);
4458 template <
typename T,
size_t N>
4459 inline internal::AnyOfArrayMatcher<T> AnyOfArray(
const T (&array)[N]) {
4460 return AnyOfArray(array, N);
4463 template <
typename T,
size_t N>
4464 inline internal::AllOfArrayMatcher<T> AllOfArray(
const T (&array)[N]) {
4465 return AllOfArray(array, N);
4468 template <
typename Container>
4469 inline internal::AnyOfArrayMatcher<typename Container::value_type> AnyOfArray(
4470 const Container& container) {
4471 return AnyOfArray(container.begin(), container.end());
4474 template <
typename Container>
4475 inline internal::AllOfArrayMatcher<typename Container::value_type> AllOfArray(
4476 const Container& container) {
4477 return AllOfArray(container.begin(), container.end());
4480 template <
typename T>
4481 inline internal::AnyOfArrayMatcher<T> AnyOfArray(
4482 ::std::initializer_list<T> xs) {
4483 return AnyOfArray(xs.begin(), xs.end());
4486 template <
typename T>
4487 inline internal::AllOfArrayMatcher<T> AllOfArray(
4488 ::std::initializer_list<T> xs) {
4489 return AllOfArray(xs.begin(), xs.end());
4495 template <
size_t... k,
typename InnerMatcher>
4496 internal::ArgsMatcher<typename std::decay<InnerMatcher>::type, k...> Args(
4497 InnerMatcher&& matcher) {
4498 return internal::ArgsMatcher<typename std::decay<InnerMatcher>::type, k...>(
4499 std::forward<InnerMatcher>(matcher));
4509 template <
typename InnerMatcher>
4510 inline InnerMatcher AllArgs(
const InnerMatcher& matcher) {
return matcher; }
4520 template <
typename ValueMatcher>
4521 inline internal::OptionalMatcher<ValueMatcher> Optional(
4522 const ValueMatcher& value_matcher) {
4523 return internal::OptionalMatcher<ValueMatcher>(value_matcher);
4527 template <
typename T>
4528 PolymorphicMatcher<internal::any_cast_matcher::AnyCastMatcher<T> > AnyWith(
4529 const Matcher<const T&>& matcher) {
4530 return MakePolymorphicMatcher(
4531 internal::any_cast_matcher::AnyCastMatcher<T>(matcher));
4538 template <
typename T>
4539 PolymorphicMatcher<internal::variant_matcher::VariantMatcher<T> > VariantWith(
4540 const Matcher<const T&>& matcher) {
4541 return MakePolymorphicMatcher(
4542 internal::variant_matcher::VariantMatcher<T>(matcher));
4549 #define ASSERT_THAT(value, matcher) ASSERT_PRED_FORMAT1(\ 4550 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value) 4551 #define EXPECT_THAT(value, matcher) EXPECT_PRED_FORMAT1(\ 4552 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value) 4556 GTEST_DISABLE_MSC_WARNINGS_POP_()
4561 #include "gmock/internal/custom/gmock-matchers.h" 4563 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_ Definition: gmock-actions.h:59
Definition: gmock-internal-utils.h:52