255#ifndef GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_
256#define GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_
260#include <initializer_list>
267#include <type_traits>
271#include "gmock/internal/gmock-internal-utils.h"
272#include "gmock/internal/gmock-port.h"
273#include "gmock/internal/gmock-pp.h"
274#include "gtest/gtest.h"
277#if defined(_MSC_VER) && _MSC_VER >= 1915
278#define GMOCK_MAYBE_5046_ 5046
280#define GMOCK_MAYBE_5046_
283GTEST_DISABLE_MSC_WARNINGS_PUSH_(
284 4251 GMOCK_MAYBE_5046_
303class StringMatchResultListener :
public MatchResultListener {
305 StringMatchResultListener() : MatchResultListener(&ss_) {}
308 std::string str()
const {
return ss_.str(); }
311 void Clear() { ss_.str(
""); }
314 ::std::stringstream ss_;
316 StringMatchResultListener(
const StringMatchResultListener&) =
delete;
317 StringMatchResultListener& operator=(
const StringMatchResultListener&) =
335template <
typename T,
typename M>
336class MatcherCastImpl {
338 static Matcher<T> Cast(
const M& polymorphic_matcher_or_value) {
352 return CastImpl(polymorphic_matcher_or_value,
353 std::is_convertible<M, Matcher<T>>{},
354 std::is_convertible<M, T>{});
358 template <
bool Ignore>
359 static Matcher<T> CastImpl(
const M& polymorphic_matcher_or_value,
361 std::integral_constant<bool, Ignore>) {
370 return polymorphic_matcher_or_value;
376 static Matcher<T> CastImpl(
const M& value,
379 return Matcher<T>(ImplicitCast_<T>(value));
392 static Matcher<T> CastImpl(
const M& value,
400template <
typename T,
typename U>
401class MatcherCastImpl<T, Matcher<U>> {
403 static Matcher<T> Cast(
const Matcher<U>& source_matcher) {
404 return Matcher<T>(
new Impl(source_matcher));
408 class Impl :
public MatcherInterface<T> {
410 explicit Impl(
const Matcher<U>& source_matcher)
411 : source_matcher_(source_matcher) {}
414 bool MatchAndExplain(T x, MatchResultListener* listener)
const override {
415 using FromType =
typename std::remove_cv<
typename std::remove_pointer<
416 typename std::remove_reference<T>::type>::type>::type;
417 using ToType =
typename std::remove_cv<
typename std::remove_pointer<
418 typename std::remove_reference<U>::type>::type>::type;
423 (std::is_pointer<typename std::remove_reference<T>::type>::value !=
424 std::is_pointer<typename std::remove_reference<U>::type>::value) ||
425 std::is_same<FromType, ToType>::value ||
426 !std::is_base_of<FromType, ToType>::value,
427 "Can't implicitly convert from <base> to <derived>");
432 typename std::conditional<std::is_convertible<T&, const U&>::value,
435 return source_matcher_.MatchAndExplain(
static_cast<CastType
>(x),
439 void DescribeTo(::std::ostream* os)
const override {
440 source_matcher_.DescribeTo(os);
443 void DescribeNegationTo(::std::ostream* os)
const override {
444 source_matcher_.DescribeNegationTo(os);
448 const Matcher<U> source_matcher_;
455class MatcherCastImpl<T, Matcher<T>> {
457 static Matcher<T> Cast(
const Matcher<T>& matcher) {
return matcher; }
461template <
typename Derived>
462class MatcherBaseImpl {
464 MatcherBaseImpl() =
default;
466 template <
typename T>
467 operator ::testing::Matcher<T>()
const {
468 return ::testing::Matcher<T>(
new
469 typename Derived::template gmock_Impl<T>());
474template <
template <
typename...>
class Derived,
typename... Ts>
475class MatcherBaseImpl<Derived<Ts...>> {
479 template <
typename E = std::enable_if<
sizeof...(Ts) == 1>,
480 typename E::type* =
nullptr>
481 explicit MatcherBaseImpl(Ts... params)
482 : params_(std::forward<Ts>(params)...) {}
483 template <
typename E = std::enable_if<
sizeof...(Ts) != 1>,
484 typename =
typename E::type>
485 MatcherBaseImpl(Ts... params)
486 : params_(std::forward<Ts>(params)...) {}
488 template <
typename F>
489 operator ::testing::Matcher<F>()
const {
490 return Apply<F>(MakeIndexSequence<
sizeof...(Ts)>{});
494 template <
typename F, std::size_t... tuple_ids>
496 return ::testing::Matcher<F>(
497 new typename Derived<Ts...>::template gmock_Impl<F>(
498 std::get<tuple_ids>(params_)...));
501 const std::tuple<Ts...> params_;
510template <
typename T,
typename M>
511inline Matcher<T> MatcherCast(
const M& matcher) {
512 return internal::MatcherCastImpl<T, M>::Cast(matcher);
517template <
typename T,
typename M>
518inline Matcher<T> SafeMatcherCast(
const M& polymorphic_matcher_or_value) {
519 return MatcherCast<T>(polymorphic_matcher_or_value);
531template <
typename T,
typename U>
532inline Matcher<T> SafeMatcherCast(
const Matcher<U>& matcher) {
534 static_assert(std::is_convertible<const T&, const U&>::value,
535 "T must be implicitly convertible to U");
538 static_assert(std::is_reference<T>::value || !std::is_reference<U>::value,
539 "cannot convert non reference arg to reference");
542 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(T) RawT;
543 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(U) RawU;
544 constexpr bool kTIsOther = GMOCK_KIND_OF_(RawT) == internal::kOther;
545 constexpr bool kUIsOther = GMOCK_KIND_OF_(RawU) == internal::kOther;
547 kTIsOther || kUIsOther ||
548 (internal::LosslessArithmeticConvertible<RawT, RawU>::value),
549 "conversion of arithmetic types must be lossless");
550 return MatcherCast<T>(matcher);
562inline void PrintIfNotEmpty(
const std::string& explanation,
563 ::std::ostream* os) {
564 if (explanation !=
"" && os !=
nullptr) {
565 *os <<
", " << explanation;
572inline bool IsReadableTypeName(
const std::string& type_name) {
575 return (type_name.length() <= 20 ||
576 type_name.find_first_of(
"<(") == std::string::npos);
584template <
typename Value,
typename T>
585bool MatchPrintAndExplain(Value& value,
const Matcher<T>& matcher,
586 MatchResultListener* listener) {
587 if (!listener->IsInterested()) {
590 return matcher.Matches(value);
593 StringMatchResultListener inner_listener;
594 const bool match = matcher.MatchAndExplain(value, &inner_listener);
596 UniversalPrint(value, listener->stream());
598 const std::string& type_name = GetTypeName<Value>();
599 if (IsReadableTypeName(type_name))
600 *listener->stream() <<
" (of type " << type_name <<
")";
602 PrintIfNotEmpty(inner_listener.str(), listener->stream());
615 template <
typename MatcherTuple,
typename ValueTuple>
616 static bool Matches(
const MatcherTuple& matcher_tuple,
617 const ValueTuple& value_tuple) {
618 return TuplePrefix<N - 1>::Matches(matcher_tuple, value_tuple) &&
619 std::get<N - 1>(matcher_tuple).Matches(std::get<N - 1>(value_tuple));
626 template <
typename MatcherTuple,
typename ValueTuple>
627 static void ExplainMatchFailuresTo(
const MatcherTuple& matchers,
628 const ValueTuple& values,
629 ::std::ostream* os) {
631 TuplePrefix<N - 1>::ExplainMatchFailuresTo(matchers, values, os);
635 typename std::tuple_element<N - 1, MatcherTuple>::type matcher =
636 std::get<N - 1>(matchers);
637 typedef typename std::tuple_element<N - 1, ValueTuple>::type Value;
638 const Value& value = std::get<N - 1>(values);
639 StringMatchResultListener listener;
640 if (!matcher.MatchAndExplain(value, &listener)) {
641 *os <<
" Expected arg #" << N - 1 <<
": ";
642 std::get<N - 1>(matchers).DescribeTo(os);
643 *os <<
"\n Actual: ";
649 internal::UniversalPrint(value, os);
650 PrintIfNotEmpty(listener.str(), os);
658class TuplePrefix<0> {
660 template <
typename MatcherTuple,
typename ValueTuple>
661 static bool Matches(
const MatcherTuple& ,
662 const ValueTuple& ) {
666 template <
typename MatcherTuple,
typename ValueTuple>
667 static void ExplainMatchFailuresTo(
const MatcherTuple& ,
677template <
typename MatcherTuple,
typename ValueTuple>
678bool TupleMatches(
const MatcherTuple& matcher_tuple,
679 const ValueTuple& value_tuple) {
682 static_assert(std::tuple_size<MatcherTuple>::value ==
683 std::tuple_size<ValueTuple>::value,
684 "matcher and value have different numbers of fields");
685 return TuplePrefix<std::tuple_size<ValueTuple>::value>::Matches(matcher_tuple,
691template <
typename MatcherTuple,
typename ValueTuple>
692void ExplainMatchFailureTupleTo(
const MatcherTuple& matchers,
693 const ValueTuple& values, ::std::ostream* os) {
694 TuplePrefix<std::tuple_size<MatcherTuple>::value>::ExplainMatchFailuresTo(
695 matchers, values, os);
702template <
typename Tuple,
typename Func,
typename OutIter>
703class TransformTupleValuesHelper {
705 typedef ::std::tuple_size<Tuple> TupleSize;
710 static OutIter Run(Func f,
const Tuple& t, OutIter out) {
711 return IterateOverTuple<Tuple, TupleSize::value>()(f, t, out);
715 template <
typename Tup,
size_t kRemainingSize>
716 struct IterateOverTuple {
717 OutIter operator()(Func f,
const Tup& t, OutIter out)
const {
718 *out++ = f(::std::get<TupleSize::value - kRemainingSize>(t));
719 return IterateOverTuple<Tup, kRemainingSize - 1>()(f, t, out);
722 template <
typename Tup>
723 struct IterateOverTuple<Tup, 0> {
724 OutIter operator()(Func ,
const Tup& , OutIter out)
const {
733template <
typename Tuple,
typename Func,
typename OutIter>
734OutIter TransformTupleValues(Func f,
const Tuple& t, OutIter out) {
735 return TransformTupleValuesHelper<Tuple, Func, OutIter>::Run(f, t, out);
742class AnythingMatcher {
744 using is_gtest_matcher = void;
746 template <
typename T>
747 bool MatchAndExplain(
const T& , std::ostream* )
const {
750 void DescribeTo(std::ostream* os)
const { *os <<
"is anything"; }
751 void DescribeNegationTo(::std::ostream* os)
const {
755 *os <<
"never matches";
763 template <
typename Po
inter>
764 bool MatchAndExplain(
const Pointer& p,
765 MatchResultListener* )
const {
769 void DescribeTo(::std::ostream* os)
const { *os <<
"is NULL"; }
770 void DescribeNegationTo(::std::ostream* os)
const { *os <<
"isn't NULL"; }
775class NotNullMatcher {
777 template <
typename Po
inter>
778 bool MatchAndExplain(
const Pointer& p,
779 MatchResultListener* )
const {
783 void DescribeTo(::std::ostream* os)
const { *os <<
"isn't NULL"; }
784 void DescribeNegationTo(::std::ostream* os)
const { *os <<
"is NULL"; }
804class RefMatcher<T&> {
814 explicit RefMatcher(T& x) : object_(x) {}
816 template <
typename Super>
817 operator Matcher<Super&>()
const {
823 return MakeMatcher(
new Impl<Super>(object_));
827 template <
typename Super>
828 class Impl :
public MatcherInterface<Super&> {
830 explicit Impl(Super& x) : object_(x) {}
834 bool MatchAndExplain(Super& x,
835 MatchResultListener* listener)
const override {
836 *listener <<
"which is located @" <<
static_cast<const void*
>(&x);
837 return &x == &object_;
840 void DescribeTo(::std::ostream* os)
const override {
841 *os <<
"references the variable ";
842 UniversalPrinter<Super&>::Print(object_, os);
845 void DescribeNegationTo(::std::ostream* os)
const override {
846 *os <<
"does not reference the variable ";
847 UniversalPrinter<Super&>::Print(object_, os);
851 const Super& object_;
858inline bool CaseInsensitiveCStringEquals(
const char* lhs,
const char* rhs) {
859 return String::CaseInsensitiveCStringEquals(lhs, rhs);
862inline bool CaseInsensitiveCStringEquals(
const wchar_t* lhs,
863 const wchar_t* rhs) {
864 return String::CaseInsensitiveWideCStringEquals(lhs, rhs);
869template <
typename StringType>
870bool CaseInsensitiveStringEquals(
const StringType& s1,
const StringType& s2) {
872 if (!CaseInsensitiveCStringEquals(s1.c_str(), s2.c_str())) {
877 const typename StringType::value_type nul = 0;
878 const size_t i1 = s1.find(nul), i2 = s2.find(nul);
881 if (i1 == StringType::npos || i2 == StringType::npos) {
886 return CaseInsensitiveStringEquals(s1.substr(i1 + 1), s2.substr(i2 + 1));
892template <
typename StringType>
893class StrEqualityMatcher {
895 StrEqualityMatcher(StringType str,
bool expect_eq,
bool case_sensitive)
896 : string_(std::move(str)),
897 expect_eq_(expect_eq),
898 case_sensitive_(case_sensitive) {}
900#if GTEST_INTERNAL_HAS_STRING_VIEW
901 bool MatchAndExplain(
const internal::StringView& s,
902 MatchResultListener* listener)
const {
905 const StringType& str = std::string(s);
906 return MatchAndExplain(str, listener);
915 template <
typename CharType>
916 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
920 return MatchAndExplain(StringType(s), listener);
927 template <
typename MatcheeStringType>
928 bool MatchAndExplain(
const MatcheeStringType& s,
929 MatchResultListener* )
const {
930 const StringType s2(s);
931 const bool eq = case_sensitive_ ? s2 == string_
932 : CaseInsensitiveStringEquals(s2, string_);
933 return expect_eq_ == eq;
936 void DescribeTo(::std::ostream* os)
const {
937 DescribeToHelper(expect_eq_, os);
940 void DescribeNegationTo(::std::ostream* os)
const {
941 DescribeToHelper(!expect_eq_, os);
945 void DescribeToHelper(
bool expect_eq, ::std::ostream* os)
const {
946 *os << (expect_eq ?
"is " :
"isn't ");
948 if (!case_sensitive_) {
949 *os <<
"(ignoring case) ";
951 UniversalPrint(string_, os);
954 const StringType string_;
955 const bool expect_eq_;
956 const bool case_sensitive_;
962template <
typename StringType>
963class HasSubstrMatcher {
965 explicit HasSubstrMatcher(
const StringType& substring)
966 : substring_(substring) {}
968#if GTEST_INTERNAL_HAS_STRING_VIEW
969 bool MatchAndExplain(
const internal::StringView& s,
970 MatchResultListener* listener)
const {
973 const StringType& str = std::string(s);
974 return MatchAndExplain(str, listener);
983 template <
typename CharType>
984 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
985 return s !=
nullptr && MatchAndExplain(StringType(s), listener);
992 template <
typename MatcheeStringType>
993 bool MatchAndExplain(
const MatcheeStringType& s,
994 MatchResultListener* )
const {
995 return StringType(s).find(substring_) != StringType::npos;
999 void DescribeTo(::std::ostream* os)
const {
1000 *os <<
"has substring ";
1001 UniversalPrint(substring_, os);
1004 void DescribeNegationTo(::std::ostream* os)
const {
1005 *os <<
"has no substring ";
1006 UniversalPrint(substring_, os);
1010 const StringType substring_;
1016template <
typename StringType>
1017class StartsWithMatcher {
1019 explicit StartsWithMatcher(
const StringType& prefix) : prefix_(prefix) {}
1021#if GTEST_INTERNAL_HAS_STRING_VIEW
1022 bool MatchAndExplain(
const internal::StringView& s,
1023 MatchResultListener* listener)
const {
1026 const StringType& str = std::string(s);
1027 return MatchAndExplain(str, listener);
1036 template <
typename CharType>
1037 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
1038 return s !=
nullptr && MatchAndExplain(StringType(s), listener);
1045 template <
typename MatcheeStringType>
1046 bool MatchAndExplain(
const MatcheeStringType& s,
1047 MatchResultListener* )
const {
1048 const StringType& s2(s);
1049 return s2.length() >= prefix_.length() &&
1050 s2.substr(0, prefix_.length()) == prefix_;
1053 void DescribeTo(::std::ostream* os)
const {
1054 *os <<
"starts with ";
1055 UniversalPrint(prefix_, os);
1058 void DescribeNegationTo(::std::ostream* os)
const {
1059 *os <<
"doesn't start with ";
1060 UniversalPrint(prefix_, os);
1064 const StringType prefix_;
1070template <
typename StringType>
1071class EndsWithMatcher {
1073 explicit EndsWithMatcher(
const StringType& suffix) : suffix_(suffix) {}
1075#if GTEST_INTERNAL_HAS_STRING_VIEW
1076 bool MatchAndExplain(
const internal::StringView& s,
1077 MatchResultListener* listener)
const {
1080 const StringType& str = std::string(s);
1081 return MatchAndExplain(str, listener);
1090 template <
typename CharType>
1091 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
1092 return s !=
nullptr && MatchAndExplain(StringType(s), listener);
1099 template <
typename MatcheeStringType>
1100 bool MatchAndExplain(
const MatcheeStringType& s,
1101 MatchResultListener* )
const {
1102 const StringType& s2(s);
1103 return s2.length() >= suffix_.length() &&
1104 s2.substr(s2.length() - suffix_.length()) == suffix_;
1107 void DescribeTo(::std::ostream* os)
const {
1108 *os <<
"ends with ";
1109 UniversalPrint(suffix_, os);
1112 void DescribeNegationTo(::std::ostream* os)
const {
1113 *os <<
"doesn't end with ";
1114 UniversalPrint(suffix_, os);
1118 const StringType suffix_;
1123class WhenBase64UnescapedMatcher {
1125 using is_gtest_matcher = void;
1127 explicit WhenBase64UnescapedMatcher(
1128 const Matcher<const std::string&>& internal_matcher)
1129 : internal_matcher_(internal_matcher) {}
1132 template <
typename MatcheeStringType>
1133 bool MatchAndExplain(
const MatcheeStringType& s,
1134 MatchResultListener* listener)
const {
1135 const std::string s2(s);
1136 std::string unescaped;
1137 if (!internal::Base64Unescape(s2, &unescaped)) {
1138 if (listener !=
nullptr) {
1139 *listener <<
"is not a valid base64 escaped string";
1143 return MatchPrintAndExplain(unescaped, internal_matcher_, listener);
1146 void DescribeTo(::std::ostream* os)
const {
1147 *os <<
"matches after Base64Unescape ";
1148 internal_matcher_.DescribeTo(os);
1151 void DescribeNegationTo(::std::ostream* os)
const {
1152 *os <<
"does not match after Base64Unescape ";
1153 internal_matcher_.DescribeTo(os);
1157 const Matcher<const std::string&> internal_matcher_;
1168template <
typename D,
typename Op>
1169class PairMatchBase {
1171 template <
typename T1,
typename T2>
1172 operator Matcher<::std::tuple<T1, T2>>()
const {
1173 return Matcher<::std::tuple<T1, T2>>(
new Impl<const ::std::tuple<T1, T2>&>);
1175 template <
typename T1,
typename T2>
1176 operator Matcher<const ::std::tuple<T1, T2>&>()
const {
1177 return MakeMatcher(
new Impl<const ::std::tuple<T1, T2>&>);
1181 static ::std::ostream& GetDesc(::std::ostream& os) {
1182 return os << D::Desc();
1185 template <
typename Tuple>
1186 class Impl :
public MatcherInterface<Tuple> {
1188 bool MatchAndExplain(Tuple args,
1189 MatchResultListener* )
const override {
1190 return Op()(::std::get<0>(args), ::std::get<1>(args));
1192 void DescribeTo(::std::ostream* os)
const override {
1193 *os <<
"are " << GetDesc;
1195 void DescribeNegationTo(::std::ostream* os)
const override {
1196 *os <<
"aren't " << GetDesc;
1201class Eq2Matcher :
public PairMatchBase<Eq2Matcher, AnyEq> {
1203 static const char* Desc() {
return "an equal pair"; }
1205class Ne2Matcher :
public PairMatchBase<Ne2Matcher, AnyNe> {
1207 static const char* Desc() {
return "an unequal pair"; }
1209class Lt2Matcher :
public PairMatchBase<Lt2Matcher, AnyLt> {
1211 static const char* Desc() {
return "a pair where the first < the second"; }
1213class Gt2Matcher :
public PairMatchBase<Gt2Matcher, AnyGt> {
1215 static const char* Desc() {
return "a pair where the first > the second"; }
1217class Le2Matcher :
public PairMatchBase<Le2Matcher, AnyLe> {
1219 static const char* Desc() {
return "a pair where the first <= the second"; }
1221class Ge2Matcher :
public PairMatchBase<Ge2Matcher, AnyGe> {
1223 static const char* Desc() {
return "a pair where the first >= the second"; }
1230template <
typename T>
1231class NotMatcherImpl :
public MatcherInterface<const T&> {
1233 explicit NotMatcherImpl(
const Matcher<T>& matcher) : matcher_(matcher) {}
1235 bool MatchAndExplain(
const T& x,
1236 MatchResultListener* listener)
const override {
1237 return !matcher_.MatchAndExplain(x, listener);
1240 void DescribeTo(::std::ostream* os)
const override {
1241 matcher_.DescribeNegationTo(os);
1244 void DescribeNegationTo(::std::ostream* os)
const override {
1245 matcher_.DescribeTo(os);
1249 const Matcher<T> matcher_;
1254template <
typename InnerMatcher>
1257 explicit NotMatcher(InnerMatcher matcher) : matcher_(matcher) {}
1261 template <
typename T>
1262 operator Matcher<T>()
const {
1263 return Matcher<T>(
new NotMatcherImpl<T>(SafeMatcherCast<T>(matcher_)));
1267 InnerMatcher matcher_;
1274template <
typename T>
1275class AllOfMatcherImpl :
public MatcherInterface<const T&> {
1277 explicit AllOfMatcherImpl(std::vector<Matcher<T>> matchers)
1278 : matchers_(std::move(matchers)) {}
1280 void DescribeTo(::std::ostream* os)
const override {
1282 for (
size_t i = 0; i < matchers_.size(); ++i) {
1283 if (i != 0) *os <<
") and (";
1284 matchers_[i].DescribeTo(os);
1289 void DescribeNegationTo(::std::ostream* os)
const override {
1291 for (
size_t i = 0; i < matchers_.size(); ++i) {
1292 if (i != 0) *os <<
") or (";
1293 matchers_[i].DescribeNegationTo(os);
1298 bool MatchAndExplain(
const T& x,
1299 MatchResultListener* listener)
const override {
1302 std::string all_match_result;
1304 for (
size_t i = 0; i < matchers_.size(); ++i) {
1305 StringMatchResultListener slistener;
1306 if (matchers_[i].MatchAndExplain(x, &slistener)) {
1307 if (all_match_result.empty()) {
1308 all_match_result = slistener.str();
1310 std::string result = slistener.str();
1311 if (!result.empty()) {
1312 all_match_result +=
", and ";
1313 all_match_result += result;
1317 *listener << slistener.str();
1323 *listener << all_match_result;
1328 const std::vector<Matcher<T>> matchers_;
1335template <
template <
typename T>
class CombiningMatcher,
typename... Args>
1336class VariadicMatcher {
1338 VariadicMatcher(
const Args&... matchers)
1339 : matchers_(matchers...) {
1340 static_assert(
sizeof...(Args) > 0,
"Must have at least one matcher.");
1343 VariadicMatcher(
const VariadicMatcher&) =
default;
1344 VariadicMatcher& operator=(
const VariadicMatcher&) =
delete;
1349 template <
typename T>
1350 operator Matcher<T>()
const {
1351 std::vector<Matcher<T>> values;
1352 CreateVariadicMatcher<T>(&values, std::integral_constant<size_t, 0>());
1353 return Matcher<T>(
new CombiningMatcher<T>(std::move(values)));
1357 template <
typename T,
size_t I>
1358 void CreateVariadicMatcher(std::vector<Matcher<T>>* values,
1359 std::integral_constant<size_t, I>)
const {
1360 values->push_back(SafeMatcherCast<T>(std::get<I>(matchers_)));
1361 CreateVariadicMatcher<T>(values, std::integral_constant<size_t, I + 1>());
1364 template <
typename T>
1365 void CreateVariadicMatcher(
1366 std::vector<Matcher<T>>*,
1367 std::integral_constant<
size_t,
sizeof...(Args)>)
const {}
1369 std::tuple<Args...> matchers_;
1372template <
typename... Args>
1373using AllOfMatcher = VariadicMatcher<AllOfMatcherImpl, Args...>;
1379template <
typename T>
1380class AnyOfMatcherImpl :
public MatcherInterface<const T&> {
1382 explicit AnyOfMatcherImpl(std::vector<Matcher<T>> matchers)
1383 : matchers_(std::move(matchers)) {}
1385 void DescribeTo(::std::ostream* os)
const override {
1387 for (
size_t i = 0; i < matchers_.size(); ++i) {
1388 if (i != 0) *os <<
") or (";
1389 matchers_[i].DescribeTo(os);
1394 void DescribeNegationTo(::std::ostream* os)
const override {
1396 for (
size_t i = 0; i < matchers_.size(); ++i) {
1397 if (i != 0) *os <<
") and (";
1398 matchers_[i].DescribeNegationTo(os);
1403 bool MatchAndExplain(
const T& x,
1404 MatchResultListener* listener)
const override {
1405 std::string no_match_result;
1409 for (
size_t i = 0; i < matchers_.size(); ++i) {
1410 StringMatchResultListener slistener;
1411 if (matchers_[i].MatchAndExplain(x, &slistener)) {
1412 *listener << slistener.str();
1415 if (no_match_result.empty()) {
1416 no_match_result = slistener.str();
1418 std::string result = slistener.str();
1419 if (!result.empty()) {
1420 no_match_result +=
", and ";
1421 no_match_result += result;
1428 *listener << no_match_result;
1433 const std::vector<Matcher<T>> matchers_;
1437template <
typename... Args>
1438using AnyOfMatcher = VariadicMatcher<AnyOfMatcherImpl, Args...>;
1441template <
typename MatcherTrue,
typename MatcherFalse>
1442class ConditionalMatcher {
1444 ConditionalMatcher(
bool condition, MatcherTrue matcher_true,
1445 MatcherFalse matcher_false)
1446 : condition_(condition),
1447 matcher_true_(std::move(matcher_true)),
1448 matcher_false_(std::move(matcher_false)) {}
1450 template <
typename T>
1451 operator Matcher<T>()
const {
1452 return condition_ ? SafeMatcherCast<T>(matcher_true_)
1453 : SafeMatcherCast<T>(matcher_false_);
1458 MatcherTrue matcher_true_;
1459 MatcherFalse matcher_false_;
1463template <
template <
class>
class MatcherImpl,
typename T>
1464class SomeOfArrayMatcher {
1468 template <
typename Iter>
1469 SomeOfArrayMatcher(Iter first, Iter last) : matchers_(first, last) {}
1471 template <
typename U>
1472 operator Matcher<U>()
const {
1473 using RawU =
typename std::decay<U>::type;
1474 std::vector<Matcher<RawU>> matchers;
1475 for (
const auto& matcher : matchers_) {
1476 matchers.push_back(MatcherCast<RawU>(matcher));
1478 return Matcher<U>(
new MatcherImpl<RawU>(std::move(matchers)));
1482 const ::std::vector<T> matchers_;
1485template <
typename T>
1486using AllOfArrayMatcher = SomeOfArrayMatcher<AllOfMatcherImpl, T>;
1488template <
typename T>
1489using AnyOfArrayMatcher = SomeOfArrayMatcher<AnyOfMatcherImpl, T>;
1493template <
typename Predicate>
1496 explicit TrulyMatcher(Predicate pred) : predicate_(pred) {}
1502 template <
typename T>
1503 bool MatchAndExplain(T& x,
1504 MatchResultListener* listener)
const {
1511 if (predicate_(x))
return true;
1512 *listener <<
"didn't satisfy the given predicate";
1516 void DescribeTo(::std::ostream* os)
const {
1517 *os <<
"satisfies the given predicate";
1520 void DescribeNegationTo(::std::ostream* os)
const {
1521 *os <<
"doesn't satisfy the given predicate";
1525 Predicate predicate_;
1530template <
typename M>
1531class MatcherAsPredicate {
1533 explicit MatcherAsPredicate(M matcher) : matcher_(matcher) {}
1541 template <
typename T>
1542 bool operator()(
const T& x)
const {
1557 return MatcherCast<const T&>(matcher_).Matches(x);
1566template <
typename M>
1567class PredicateFormatterFromMatcher {
1569 explicit PredicateFormatterFromMatcher(M m) : matcher_(std::move(m)) {}
1574 template <
typename T>
1575 AssertionResult operator()(
const char* value_text,
const T& x)
const {
1587 const Matcher<const T&> matcher = SafeMatcherCast<const T&>(matcher_);
1591 if (matcher.Matches(x)) {
1592 return AssertionSuccess();
1595 ::std::stringstream ss;
1596 ss <<
"Value of: " << value_text <<
"\n"
1598 matcher.DescribeTo(&ss);
1601 StringMatchResultListener listener;
1602 if (MatchPrintAndExplain(x, matcher, &listener)) {
1603 ss <<
"\n The matcher failed on the initial attempt; but passed when "
1604 "rerun to generate the explanation.";
1606 ss <<
"\n Actual: " << listener.str();
1607 return AssertionFailure() << ss.str();
1618template <
typename M>
1619inline PredicateFormatterFromMatcher<M> MakePredicateFormatterFromMatcher(
1621 return PredicateFormatterFromMatcher<M>(std::move(matcher));
1628 template <
typename FloatType>
1629 bool MatchAndExplain(
const FloatType& f,
1630 MatchResultListener* )
const {
1631 return (::std::isnan)(f);
1634 void DescribeTo(::std::ostream* os)
const { *os <<
"is NaN"; }
1635 void DescribeNegationTo(::std::ostream* os)
const { *os <<
"isn't NaN"; }
1642template <
typename FloatType>
1643class FloatingEqMatcher {
1651 FloatingEqMatcher(FloatType expected,
bool nan_eq_nan)
1652 : expected_(expected), nan_eq_nan_(nan_eq_nan), max_abs_error_(-1) {}
1657 FloatingEqMatcher(FloatType expected,
bool nan_eq_nan,
1658 FloatType max_abs_error)
1659 : expected_(expected),
1660 nan_eq_nan_(nan_eq_nan),
1661 max_abs_error_(max_abs_error) {
1662 GTEST_CHECK_(max_abs_error >= 0)
1663 <<
", where max_abs_error is" << max_abs_error;
1667 template <
typename T>
1668 class Impl :
public MatcherInterface<T> {
1670 Impl(FloatType expected,
bool nan_eq_nan, FloatType max_abs_error)
1671 : expected_(expected),
1672 nan_eq_nan_(nan_eq_nan),
1673 max_abs_error_(max_abs_error) {}
1675 bool MatchAndExplain(T value,
1676 MatchResultListener* listener)
const override {
1677 const FloatingPoint<FloatType> actual(value), expected(expected_);
1680 if (actual.is_nan() || expected.is_nan()) {
1681 if (actual.is_nan() && expected.is_nan()) {
1687 if (HasMaxAbsError()) {
1692 if (value == expected_) {
1696 const FloatType diff = value - expected_;
1697 if (::std::fabs(diff) <= max_abs_error_) {
1701 if (listener->IsInterested()) {
1702 *listener <<
"which is " << diff <<
" from " << expected_;
1706 return actual.AlmostEquals(expected);
1710 void DescribeTo(::std::ostream* os)
const override {
1714 const ::std::streamsize old_precision =
1715 os->precision(::std::numeric_limits<FloatType>::digits10 + 2);
1716 if (FloatingPoint<FloatType>(expected_).is_nan()) {
1720 *os <<
"never matches";
1723 *os <<
"is approximately " << expected_;
1724 if (HasMaxAbsError()) {
1725 *os <<
" (absolute error <= " << max_abs_error_ <<
")";
1728 os->precision(old_precision);
1731 void DescribeNegationTo(::std::ostream* os)
const override {
1733 const ::std::streamsize old_precision =
1734 os->precision(::std::numeric_limits<FloatType>::digits10 + 2);
1735 if (FloatingPoint<FloatType>(expected_).is_nan()) {
1739 *os <<
"is anything";
1742 *os <<
"isn't approximately " << expected_;
1743 if (HasMaxAbsError()) {
1744 *os <<
" (absolute error > " << max_abs_error_ <<
")";
1748 os->precision(old_precision);
1752 bool HasMaxAbsError()
const {
return max_abs_error_ >= 0; }
1754 const FloatType expected_;
1755 const bool nan_eq_nan_;
1757 const FloatType max_abs_error_;
1763 operator Matcher<FloatType>()
const {
1765 new Impl<FloatType>(expected_, nan_eq_nan_, max_abs_error_));
1768 operator Matcher<const FloatType&>()
const {
1770 new Impl<const FloatType&>(expected_, nan_eq_nan_, max_abs_error_));
1773 operator Matcher<FloatType&>()
const {
1775 new Impl<FloatType&>(expected_, nan_eq_nan_, max_abs_error_));
1779 const FloatType expected_;
1780 const bool nan_eq_nan_;
1782 const FloatType max_abs_error_;
1790template <
typename FloatType>
1791class FloatingEq2Matcher {
1793 FloatingEq2Matcher() { Init(-1,
false); }
1795 explicit FloatingEq2Matcher(
bool nan_eq_nan) { Init(-1, nan_eq_nan); }
1797 explicit FloatingEq2Matcher(FloatType max_abs_error) {
1798 Init(max_abs_error,
false);
1801 FloatingEq2Matcher(FloatType max_abs_error,
bool nan_eq_nan) {
1802 Init(max_abs_error, nan_eq_nan);
1805 template <
typename T1,
typename T2>
1806 operator Matcher<::std::tuple<T1, T2>>()
const {
1808 new Impl<::std::tuple<T1, T2>>(max_abs_error_, nan_eq_nan_));
1810 template <
typename T1,
typename T2>
1811 operator Matcher<const ::std::tuple<T1, T2>&>()
const {
1813 new Impl<const ::std::tuple<T1, T2>&>(max_abs_error_, nan_eq_nan_));
1817 static ::std::ostream& GetDesc(::std::ostream& os) {
1818 return os <<
"an almost-equal pair";
1821 template <
typename Tuple>
1822 class Impl :
public MatcherInterface<Tuple> {
1824 Impl(FloatType max_abs_error,
bool nan_eq_nan)
1825 : max_abs_error_(max_abs_error), nan_eq_nan_(nan_eq_nan) {}
1827 bool MatchAndExplain(Tuple args,
1828 MatchResultListener* listener)
const override {
1829 if (max_abs_error_ == -1) {
1830 FloatingEqMatcher<FloatType> fm(::std::get<0>(args), nan_eq_nan_);
1831 return static_cast<Matcher<FloatType>
>(fm).MatchAndExplain(
1832 ::std::get<1>(args), listener);
1834 FloatingEqMatcher<FloatType> fm(::std::get<0>(args), nan_eq_nan_,
1836 return static_cast<Matcher<FloatType>
>(fm).MatchAndExplain(
1837 ::std::get<1>(args), listener);
1840 void DescribeTo(::std::ostream* os)
const override {
1841 *os <<
"are " << GetDesc;
1843 void DescribeNegationTo(::std::ostream* os)
const override {
1844 *os <<
"aren't " << GetDesc;
1848 FloatType max_abs_error_;
1849 const bool nan_eq_nan_;
1852 void Init(FloatType max_abs_error_val,
bool nan_eq_nan_val) {
1853 max_abs_error_ = max_abs_error_val;
1854 nan_eq_nan_ = nan_eq_nan_val;
1856 FloatType max_abs_error_;
1862template <
typename InnerMatcher>
1863class PointeeMatcher {
1865 explicit PointeeMatcher(
const InnerMatcher& matcher) : matcher_(matcher) {}
1875 template <
typename Po
inter>
1876 operator Matcher<Pointer>()
const {
1877 return Matcher<Pointer>(
new Impl<const Pointer&>(matcher_));
1882 template <
typename Po
inter>
1883 class Impl :
public MatcherInterface<Pointer> {
1886 typename std::pointer_traits<GTEST_REMOVE_REFERENCE_AND_CONST_(
1887 Pointer)>::element_type;
1889 explicit Impl(
const InnerMatcher& matcher)
1890 : matcher_(MatcherCast<const Pointee&>(matcher)) {}
1892 void DescribeTo(::std::ostream* os)
const override {
1893 *os <<
"points to a value that ";
1894 matcher_.DescribeTo(os);
1897 void DescribeNegationTo(::std::ostream* os)
const override {
1898 *os <<
"does not point to a value that ";
1899 matcher_.DescribeTo(os);
1902 bool MatchAndExplain(Pointer pointer,
1903 MatchResultListener* listener)
const override {
1904 if (GetRawPointer(pointer) ==
nullptr)
return false;
1906 *listener <<
"which points to ";
1907 return MatchPrintAndExplain(*pointer, matcher_, listener);
1911 const Matcher<const Pointee&> matcher_;
1914 const InnerMatcher matcher_;
1921template <
typename InnerMatcher>
1922class PointerMatcher {
1924 explicit PointerMatcher(
const InnerMatcher& matcher) : matcher_(matcher) {}
1934 template <
typename Po
interType>
1935 operator Matcher<PointerType>()
const {
1936 return Matcher<PointerType>(
new Impl<const PointerType&>(matcher_));
1941 template <
typename Po
interType>
1942 class Impl :
public MatcherInterface<PointerType> {
1945 const typename std::pointer_traits<GTEST_REMOVE_REFERENCE_AND_CONST_(
1946 PointerType)>::element_type*;
1948 explicit Impl(
const InnerMatcher& matcher)
1949 : matcher_(MatcherCast<Pointer>(matcher)) {}
1951 void DescribeTo(::std::ostream* os)
const override {
1952 *os <<
"is a pointer that ";
1953 matcher_.DescribeTo(os);
1956 void DescribeNegationTo(::std::ostream* os)
const override {
1957 *os <<
"is not a pointer that ";
1958 matcher_.DescribeTo(os);
1961 bool MatchAndExplain(PointerType pointer,
1962 MatchResultListener* listener)
const override {
1963 *listener <<
"which is a pointer that ";
1964 Pointer p = GetRawPointer(pointer);
1965 return MatchPrintAndExplain(p, matcher_, listener);
1969 Matcher<Pointer> matcher_;
1972 const InnerMatcher matcher_;
1982template <
typename To>
1983class WhenDynamicCastToMatcherBase {
1985 explicit WhenDynamicCastToMatcherBase(
const Matcher<To>& matcher)
1986 : matcher_(matcher) {}
1988 void DescribeTo(::std::ostream* os)
const {
1989 GetCastTypeDescription(os);
1990 matcher_.DescribeTo(os);
1993 void DescribeNegationTo(::std::ostream* os)
const {
1994 GetCastTypeDescription(os);
1995 matcher_.DescribeNegationTo(os);
1999 const Matcher<To> matcher_;
2001 static std::string GetToName() {
return GetTypeName<To>(); }
2004 static void GetCastTypeDescription(::std::ostream* os) {
2005 *os <<
"when dynamic_cast to " << GetToName() <<
", ";
2011template <
typename To>
2012class WhenDynamicCastToMatcher :
public WhenDynamicCastToMatcherBase<To> {
2014 explicit WhenDynamicCastToMatcher(
const Matcher<To>& matcher)
2015 : WhenDynamicCastToMatcherBase<To>(matcher) {}
2017 template <
typename From>
2018 bool MatchAndExplain(From from, MatchResultListener* listener)
const {
2019 To to =
dynamic_cast<To
>(from);
2020 return MatchPrintAndExplain(to, this->matcher_, listener);
2026template <
typename To>
2027class WhenDynamicCastToMatcher<To&> :
public WhenDynamicCastToMatcherBase<To&> {
2029 explicit WhenDynamicCastToMatcher(
const Matcher<To&>& matcher)
2030 : WhenDynamicCastToMatcherBase<To&>(matcher) {}
2032 template <
typename From>
2033 bool MatchAndExplain(From& from, MatchResultListener* listener)
const {
2035 To* to =
dynamic_cast<To*
>(&from);
2036 if (to ==
nullptr) {
2037 *listener <<
"which cannot be dynamic_cast to " << this->GetToName();
2040 return MatchPrintAndExplain(*to, this->matcher_, listener);
2047template <
typename Class,
typename FieldType>
2050 FieldMatcher(FieldType Class::*field,
2051 const Matcher<const FieldType&>& matcher)
2052 : field_(field), matcher_(matcher), whose_field_(
"whose given field ") {}
2054 FieldMatcher(
const std::string& field_name, FieldType Class::*field,
2055 const Matcher<const FieldType&>& matcher)
2058 whose_field_(
"whose field `" + field_name +
"` ") {}
2060 void DescribeTo(::std::ostream* os)
const {
2061 *os <<
"is an object " << whose_field_;
2062 matcher_.DescribeTo(os);
2065 void DescribeNegationTo(::std::ostream* os)
const {
2066 *os <<
"is an object " << whose_field_;
2067 matcher_.DescribeNegationTo(os);
2070 template <
typename T>
2071 bool MatchAndExplain(
const T& value, MatchResultListener* listener)
const {
2074 return MatchAndExplainImpl(
2075 typename std::is_pointer<
typename std::remove_const<T>::type>::type(),
2080 bool MatchAndExplainImpl(std::false_type ,
2082 MatchResultListener* listener)
const {
2083 *listener << whose_field_ <<
"is ";
2084 return MatchPrintAndExplain(obj.*field_, matcher_, listener);
2087 bool MatchAndExplainImpl(std::true_type ,
const Class* p,
2088 MatchResultListener* listener)
const {
2089 if (p ==
nullptr)
return false;
2091 *listener <<
"which points to an object ";
2095 return MatchAndExplainImpl(std::false_type(), *p, listener);
2098 const FieldType Class::*field_;
2099 const Matcher<const FieldType&> matcher_;
2103 const std::string whose_field_;
2111template <
typename Class,
typename PropertyType,
typename Property>
2112class PropertyMatcher {
2114 typedef const PropertyType& RefToConstProperty;
2116 PropertyMatcher(Property property,
const Matcher<RefToConstProperty>& matcher)
2117 : property_(property),
2119 whose_property_(
"whose given property ") {}
2121 PropertyMatcher(
const std::string& property_name, Property property,
2122 const Matcher<RefToConstProperty>& matcher)
2123 : property_(property),
2125 whose_property_(
"whose property `" + property_name +
"` ") {}
2127 void DescribeTo(::std::ostream* os)
const {
2128 *os <<
"is an object " << whose_property_;
2129 matcher_.DescribeTo(os);
2132 void DescribeNegationTo(::std::ostream* os)
const {
2133 *os <<
"is an object " << whose_property_;
2134 matcher_.DescribeNegationTo(os);
2137 template <
typename T>
2138 bool MatchAndExplain(
const T& value, MatchResultListener* listener)
const {
2139 return MatchAndExplainImpl(
2140 typename std::is_pointer<
typename std::remove_const<T>::type>::type(),
2145 bool MatchAndExplainImpl(std::false_type ,
2147 MatchResultListener* listener)
const {
2148 *listener << whose_property_ <<
"is ";
2151 RefToConstProperty result = (obj.*property_)();
2152 return MatchPrintAndExplain(result, matcher_, listener);
2155 bool MatchAndExplainImpl(std::true_type ,
const Class* p,
2156 MatchResultListener* listener)
const {
2157 if (p ==
nullptr)
return false;
2159 *listener <<
"which points to an object ";
2163 return MatchAndExplainImpl(std::false_type(), *p, listener);
2167 const Matcher<RefToConstProperty> matcher_;
2171 const std::string whose_property_;
2176template <
typename Functor>
2177struct CallableTraits {
2178 typedef Functor StorageType;
2180 static void CheckIsValid(Functor ) {}
2182 template <
typename T>
2183 static auto Invoke(Functor f,
const T& arg) ->
decltype(f(arg)) {
2189template <
typename ArgType,
typename ResType>
2190struct CallableTraits<ResType (*)(ArgType)> {
2191 typedef ResType ResultType;
2192 typedef ResType (*StorageType)(ArgType);
2194 static void CheckIsValid(ResType (*f)(ArgType)) {
2195 GTEST_CHECK_(f !=
nullptr)
2196 <<
"NULL function pointer is passed into ResultOf().";
2198 template <
typename T>
2199 static ResType Invoke(ResType (*f)(ArgType), T arg) {
2206template <
typename Callable,
typename InnerMatcher>
2207class ResultOfMatcher {
2209 ResultOfMatcher(Callable callable, InnerMatcher matcher)
2210 : ResultOfMatcher(
"", std::move(callable),
2211 std::move(matcher)) {}
2213 ResultOfMatcher(
const std::string& result_description, Callable callable,
2214 InnerMatcher matcher)
2215 : result_description_(result_description),
2216 callable_(std::move(callable)),
2217 matcher_(std::move(matcher)) {
2218 CallableTraits<Callable>::CheckIsValid(callable_);
2221 template <
typename T>
2222 operator Matcher<T>()
const {
2224 new Impl<const T&>(result_description_, callable_, matcher_));
2228 typedef typename CallableTraits<Callable>::StorageType CallableStorageType;
2230 template <
typename T>
2231 class Impl :
public MatcherInterface<T> {
2232 using ResultType =
decltype(CallableTraits<Callable>::template Invoke<T>(
2233 std::declval<CallableStorageType>(), std::declval<T>()));
2236 template <
typename M>
2237 Impl(
const std::string& result_description,
2238 const CallableStorageType& callable,
const M& matcher)
2239 : result_description_(result_description),
2240 callable_(callable),
2241 matcher_(MatcherCast<ResultType>(matcher)) {}
2243 void DescribeTo(::std::ostream* os)
const override {
2244 if (result_description_.empty()) {
2245 *os <<
"is mapped by the given callable to a value that ";
2247 *os <<
"whose " << result_description_ <<
" ";
2249 matcher_.DescribeTo(os);
2252 void DescribeNegationTo(::std::ostream* os)
const override {
2253 if (result_description_.empty()) {
2254 *os <<
"is mapped by the given callable to a value that ";
2256 *os <<
"whose " << result_description_ <<
" ";
2258 matcher_.DescribeNegationTo(os);
2261 bool MatchAndExplain(T obj, MatchResultListener* listener)
const override {
2262 if (result_description_.empty()) {
2263 *listener <<
"which is mapped by the given callable to ";
2265 *listener <<
"whose " << result_description_ <<
" is ";
2272 CallableTraits<Callable>::template Invoke<T>(callable_, obj);
2273 return MatchPrintAndExplain(result, matcher_, listener);
2277 const std::string result_description_;
2283 mutable CallableStorageType callable_;
2284 const Matcher<ResultType> matcher_;
2287 const std::string result_description_;
2288 const CallableStorageType callable_;
2289 const InnerMatcher matcher_;
2293template <
typename SizeMatcher>
2294class SizeIsMatcher {
2296 explicit SizeIsMatcher(
const SizeMatcher& size_matcher)
2297 : size_matcher_(size_matcher) {}
2299 template <
typename Container>
2300 operator Matcher<Container>()
const {
2301 return Matcher<Container>(
new Impl<const Container&>(size_matcher_));
2304 template <
typename Container>
2305 class Impl :
public MatcherInterface<Container> {
2307 using SizeType =
decltype(std::declval<Container>().size());
2308 explicit Impl(
const SizeMatcher& size_matcher)
2309 : size_matcher_(MatcherCast<SizeType>(size_matcher)) {}
2311 void DescribeTo(::std::ostream* os)
const override {
2313 size_matcher_.DescribeTo(os);
2315 void DescribeNegationTo(::std::ostream* os)
const override {
2317 size_matcher_.DescribeNegationTo(os);
2320 bool MatchAndExplain(Container container,
2321 MatchResultListener* listener)
const override {
2322 SizeType size = container.size();
2323 StringMatchResultListener size_listener;
2324 const bool result = size_matcher_.MatchAndExplain(size, &size_listener);
2325 *listener <<
"whose size " << size
2326 << (result ?
" matches" :
" doesn't match");
2327 PrintIfNotEmpty(size_listener.str(), listener->stream());
2332 const Matcher<SizeType> size_matcher_;
2336 const SizeMatcher size_matcher_;
2341template <
typename DistanceMatcher>
2342class BeginEndDistanceIsMatcher {
2344 explicit BeginEndDistanceIsMatcher(
const DistanceMatcher& distance_matcher)
2345 : distance_matcher_(distance_matcher) {}
2347 template <
typename Container>
2348 operator Matcher<Container>()
const {
2349 return Matcher<Container>(
new Impl<const Container&>(distance_matcher_));
2352 template <
typename Container>
2353 class Impl :
public MatcherInterface<Container> {
2355 typedef internal::StlContainerView<GTEST_REMOVE_REFERENCE_AND_CONST_(
2358 typedef typename std::iterator_traits<
2359 typename ContainerView::type::const_iterator>::difference_type
2361 explicit Impl(
const DistanceMatcher& distance_matcher)
2362 : distance_matcher_(MatcherCast<DistanceType>(distance_matcher)) {}
2364 void DescribeTo(::std::ostream* os)
const override {
2365 *os <<
"distance between begin() and end() ";
2366 distance_matcher_.DescribeTo(os);
2368 void DescribeNegationTo(::std::ostream* os)
const override {
2369 *os <<
"distance between begin() and end() ";
2370 distance_matcher_.DescribeNegationTo(os);
2373 bool MatchAndExplain(Container container,
2374 MatchResultListener* listener)
const override {
2377 DistanceType distance = std::distance(begin(container), end(container));
2378 StringMatchResultListener distance_listener;
2380 distance_matcher_.MatchAndExplain(distance, &distance_listener);
2381 *listener <<
"whose distance between begin() and end() " << distance
2382 << (result ?
" matches" :
" doesn't match");
2383 PrintIfNotEmpty(distance_listener.str(), listener->stream());
2388 const Matcher<DistanceType> distance_matcher_;
2392 const DistanceMatcher distance_matcher_;
2405template <
typename Container>
2406class ContainerEqMatcher {
2408 typedef internal::StlContainerView<Container> View;
2409 typedef typename View::type StlContainer;
2410 typedef typename View::const_reference StlContainerReference;
2412 static_assert(!std::is_const<Container>::value,
2413 "Container type must not be const");
2414 static_assert(!std::is_reference<Container>::value,
2415 "Container type must not be a reference");
2419 explicit ContainerEqMatcher(
const Container& expected)
2420 : expected_(View::Copy(expected)) {}
2422 void DescribeTo(::std::ostream* os)
const {
2424 UniversalPrint(expected_, os);
2426 void DescribeNegationTo(::std::ostream* os)
const {
2427 *os <<
"does not equal ";
2428 UniversalPrint(expected_, os);
2431 template <
typename LhsContainer>
2432 bool MatchAndExplain(
const LhsContainer& lhs,
2433 MatchResultListener* listener)
const {
2434 typedef internal::StlContainerView<
2435 typename std::remove_const<LhsContainer>::type>
2437 StlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2438 if (lhs_stl_container == expected_)
return true;
2440 ::std::ostream*
const os = listener->stream();
2441 if (os !=
nullptr) {
2443 bool printed_header =
false;
2444 for (
auto it = lhs_stl_container.begin(); it != lhs_stl_container.end();
2446 if (internal::ArrayAwareFind(expected_.begin(), expected_.end(), *it) ==
2448 if (printed_header) {
2451 *os <<
"which has these unexpected elements: ";
2452 printed_header =
true;
2454 UniversalPrint(*it, os);
2459 bool printed_header2 =
false;
2460 for (
auto it = expected_.begin(); it != expected_.end(); ++it) {
2461 if (internal::ArrayAwareFind(lhs_stl_container.begin(),
2462 lhs_stl_container.end(),
2463 *it) == lhs_stl_container.end()) {
2464 if (printed_header2) {
2467 *os << (printed_header ?
",\nand" :
"which")
2468 <<
" doesn't have these expected elements: ";
2469 printed_header2 =
true;
2471 UniversalPrint(*it, os);
2480 const StlContainer expected_;
2484struct LessComparator {
2485 template <
typename T,
typename U>
2486 bool operator()(
const T& lhs,
const U& rhs)
const {
2492template <
typename Comparator,
typename ContainerMatcher>
2493class WhenSortedByMatcher {
2495 WhenSortedByMatcher(
const Comparator& comparator,
2496 const ContainerMatcher& matcher)
2497 : comparator_(comparator), matcher_(matcher) {}
2499 template <
typename LhsContainer>
2500 operator Matcher<LhsContainer>()
const {
2501 return MakeMatcher(
new Impl<LhsContainer>(comparator_, matcher_));
2504 template <
typename LhsContainer>
2505 class Impl :
public MatcherInterface<LhsContainer> {
2507 typedef internal::StlContainerView<GTEST_REMOVE_REFERENCE_AND_CONST_(
2510 typedef typename LhsView::type LhsStlContainer;
2511 typedef typename LhsView::const_reference LhsStlContainerReference;
2515 typename RemoveConstFromKey<typename LhsStlContainer::value_type>::type
2518 Impl(
const Comparator& comparator,
const ContainerMatcher& matcher)
2519 : comparator_(comparator), matcher_(matcher) {}
2521 void DescribeTo(::std::ostream* os)
const override {
2522 *os <<
"(when sorted) ";
2523 matcher_.DescribeTo(os);
2526 void DescribeNegationTo(::std::ostream* os)
const override {
2527 *os <<
"(when sorted) ";
2528 matcher_.DescribeNegationTo(os);
2531 bool MatchAndExplain(LhsContainer lhs,
2532 MatchResultListener* listener)
const override {
2533 LhsStlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2534 ::std::vector<LhsValue> sorted_container(lhs_stl_container.begin(),
2535 lhs_stl_container.end());
2536 ::std::sort(sorted_container.begin(), sorted_container.end(),
2539 if (!listener->IsInterested()) {
2542 return matcher_.Matches(sorted_container);
2545 *listener <<
"which is ";
2546 UniversalPrint(sorted_container, listener->stream());
2547 *listener <<
" when sorted";
2549 StringMatchResultListener inner_listener;
2551 matcher_.MatchAndExplain(sorted_container, &inner_listener);
2552 PrintIfNotEmpty(inner_listener.str(), listener->stream());
2557 const Comparator comparator_;
2558 const Matcher<const ::std::vector<LhsValue>&> matcher_;
2560 Impl(
const Impl&) =
delete;
2561 Impl& operator=(
const Impl&) =
delete;
2565 const Comparator comparator_;
2566 const ContainerMatcher matcher_;
2573template <
typename TupleMatcher,
typename RhsContainer>
2574class PointwiseMatcher {
2576 !IsHashTable<GTEST_REMOVE_REFERENCE_AND_CONST_(RhsContainer)>::value,
2577 "use UnorderedPointwise with hash tables");
2580 typedef internal::StlContainerView<RhsContainer> RhsView;
2581 typedef typename RhsView::type RhsStlContainer;
2582 typedef typename RhsStlContainer::value_type RhsValue;
2584 static_assert(!std::is_const<RhsContainer>::value,
2585 "RhsContainer type must not be const");
2586 static_assert(!std::is_reference<RhsContainer>::value,
2587 "RhsContainer type must not be a reference");
2591 PointwiseMatcher(
const TupleMatcher& tuple_matcher,
const RhsContainer& rhs)
2592 : tuple_matcher_(tuple_matcher), rhs_(RhsView::Copy(rhs)) {}
2594 template <
typename LhsContainer>
2595 operator Matcher<LhsContainer>()
const {
2597 !IsHashTable<GTEST_REMOVE_REFERENCE_AND_CONST_(LhsContainer)>::value,
2598 "use UnorderedPointwise with hash tables");
2600 return Matcher<LhsContainer>(
2601 new Impl<const LhsContainer&>(tuple_matcher_, rhs_));
2604 template <
typename LhsContainer>
2605 class Impl :
public MatcherInterface<LhsContainer> {
2607 typedef internal::StlContainerView<GTEST_REMOVE_REFERENCE_AND_CONST_(
2610 typedef typename LhsView::type LhsStlContainer;
2611 typedef typename LhsView::const_reference LhsStlContainerReference;
2612 typedef typename LhsStlContainer::value_type LhsValue;
2617 typedef ::std::tuple<const LhsValue&, const RhsValue&> InnerMatcherArg;
2619 Impl(
const TupleMatcher& tuple_matcher,
const RhsStlContainer& rhs)
2621 : mono_tuple_matcher_(SafeMatcherCast<InnerMatcherArg>(tuple_matcher)),
2624 void DescribeTo(::std::ostream* os)
const override {
2625 *os <<
"contains " << rhs_.size()
2626 <<
" values, where each value and its corresponding value in ";
2627 UniversalPrinter<RhsStlContainer>::Print(rhs_, os);
2629 mono_tuple_matcher_.DescribeTo(os);
2631 void DescribeNegationTo(::std::ostream* os)
const override {
2632 *os <<
"doesn't contain exactly " << rhs_.size()
2633 <<
" values, or contains a value x at some index i"
2634 <<
" where x and the i-th value of ";
2635 UniversalPrint(rhs_, os);
2637 mono_tuple_matcher_.DescribeNegationTo(os);
2640 bool MatchAndExplain(LhsContainer lhs,
2641 MatchResultListener* listener)
const override {
2642 LhsStlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2643 const size_t actual_size = lhs_stl_container.size();
2644 if (actual_size != rhs_.size()) {
2645 *listener <<
"which contains " << actual_size <<
" values";
2649 auto left = lhs_stl_container.begin();
2650 auto right = rhs_.begin();
2651 for (
size_t i = 0; i != actual_size; ++i, ++left, ++right) {
2652 if (listener->IsInterested()) {
2653 StringMatchResultListener inner_listener;
2657 if (!mono_tuple_matcher_.MatchAndExplain(
2658 InnerMatcherArg(ImplicitCast_<const LhsValue&>(*left),
2659 ImplicitCast_<const RhsValue&>(*right)),
2661 *listener <<
"where the value pair (";
2662 UniversalPrint(*left, listener->stream());
2664 UniversalPrint(*right, listener->stream());
2665 *listener <<
") at index #" << i <<
" don't match";
2666 PrintIfNotEmpty(inner_listener.str(), listener->stream());
2670 if (!mono_tuple_matcher_.Matches(
2671 InnerMatcherArg(ImplicitCast_<const LhsValue&>(*left),
2672 ImplicitCast_<const RhsValue&>(*right))))
2681 const Matcher<InnerMatcherArg> mono_tuple_matcher_;
2682 const RhsStlContainer rhs_;
2686 const TupleMatcher tuple_matcher_;
2687 const RhsStlContainer rhs_;
2691template <
typename Container>
2692class QuantifierMatcherImpl :
public MatcherInterface<Container> {
2694 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
2695 typedef StlContainerView<RawContainer> View;
2696 typedef typename View::type StlContainer;
2697 typedef typename View::const_reference StlContainerReference;
2698 typedef typename StlContainer::value_type Element;
2700 template <
typename InnerMatcher>
2701 explicit QuantifierMatcherImpl(InnerMatcher inner_matcher)
2703 testing::SafeMatcherCast<const Element&>(inner_matcher)) {}
2708 bool MatchAndExplainImpl(
bool all_elements_should_match, Container container,
2709 MatchResultListener* listener)
const {
2710 StlContainerReference stl_container = View::ConstReference(container);
2712 for (
auto it = stl_container.begin(); it != stl_container.end();
2714 StringMatchResultListener inner_listener;
2715 const bool matches = inner_matcher_.MatchAndExplain(*it, &inner_listener);
2717 if (matches != all_elements_should_match) {
2718 *listener <<
"whose element #" << i
2719 << (matches ?
" matches" :
" doesn't match");
2720 PrintIfNotEmpty(inner_listener.str(), listener->stream());
2721 return !all_elements_should_match;
2724 return all_elements_should_match;
2727 bool MatchAndExplainImpl(
const Matcher<size_t>& count_matcher,
2728 Container container,
2729 MatchResultListener* listener)
const {
2730 StlContainerReference stl_container = View::ConstReference(container);
2732 std::vector<size_t> match_elements;
2733 for (
auto it = stl_container.begin(); it != stl_container.end();
2735 StringMatchResultListener inner_listener;
2736 const bool matches = inner_matcher_.MatchAndExplain(*it, &inner_listener);
2738 match_elements.push_back(i);
2741 if (listener->IsInterested()) {
2742 if (match_elements.empty()) {
2743 *listener <<
"has no element that matches";
2744 }
else if (match_elements.size() == 1) {
2745 *listener <<
"whose element #" << match_elements[0] <<
" matches";
2747 *listener <<
"whose elements (";
2748 std::string sep =
"";
2749 for (
size_t e : match_elements) {
2750 *listener << sep << e;
2753 *listener <<
") match";
2756 StringMatchResultListener count_listener;
2757 if (count_matcher.MatchAndExplain(match_elements.size(), &count_listener)) {
2758 *listener <<
" and whose match quantity of " << match_elements.size()
2760 PrintIfNotEmpty(count_listener.str(), listener->stream());
2763 if (match_elements.empty()) {
2764 *listener <<
" and";
2766 *listener <<
" but";
2768 *listener <<
" whose match quantity of " << match_elements.size()
2769 <<
" does not match";
2770 PrintIfNotEmpty(count_listener.str(), listener->stream());
2776 const Matcher<const Element&> inner_matcher_;
2781template <
typename Container>
2782class ContainsMatcherImpl :
public QuantifierMatcherImpl<Container> {
2784 template <
typename InnerMatcher>
2785 explicit ContainsMatcherImpl(InnerMatcher inner_matcher)
2786 : QuantifierMatcherImpl<Container>(inner_matcher) {}
2789 void DescribeTo(::std::ostream* os)
const override {
2790 *os <<
"contains at least one element that ";
2791 this->inner_matcher_.DescribeTo(os);
2794 void DescribeNegationTo(::std::ostream* os)
const override {
2795 *os <<
"doesn't contain any element that ";
2796 this->inner_matcher_.DescribeTo(os);
2799 bool MatchAndExplain(Container container,
2800 MatchResultListener* listener)
const override {
2801 return this->MatchAndExplainImpl(
false, container, listener);
2807template <
typename Container>
2808class EachMatcherImpl :
public QuantifierMatcherImpl<Container> {
2810 template <
typename InnerMatcher>
2811 explicit EachMatcherImpl(InnerMatcher inner_matcher)
2812 : QuantifierMatcherImpl<Container>(inner_matcher) {}
2815 void DescribeTo(::std::ostream* os)
const override {
2816 *os <<
"only contains elements that ";
2817 this->inner_matcher_.DescribeTo(os);
2820 void DescribeNegationTo(::std::ostream* os)
const override {
2821 *os <<
"contains some element that ";
2822 this->inner_matcher_.DescribeNegationTo(os);
2825 bool MatchAndExplain(Container container,
2826 MatchResultListener* listener)
const override {
2827 return this->MatchAndExplainImpl(
true, container, listener);
2833template <
typename Container>
2834class ContainsTimesMatcherImpl :
public QuantifierMatcherImpl<Container> {
2836 template <
typename InnerMatcher>
2837 explicit ContainsTimesMatcherImpl(InnerMatcher inner_matcher,
2838 Matcher<size_t> count_matcher)
2839 : QuantifierMatcherImpl<Container>(inner_matcher),
2840 count_matcher_(std::move(count_matcher)) {}
2842 void DescribeTo(::std::ostream* os)
const override {
2843 *os <<
"quantity of elements that match ";
2844 this->inner_matcher_.DescribeTo(os);
2846 count_matcher_.DescribeTo(os);
2849 void DescribeNegationTo(::std::ostream* os)
const override {
2850 *os <<
"quantity of elements that match ";
2851 this->inner_matcher_.DescribeTo(os);
2853 count_matcher_.DescribeNegationTo(os);
2856 bool MatchAndExplain(Container container,
2857 MatchResultListener* listener)
const override {
2858 return this->MatchAndExplainImpl(count_matcher_, container, listener);
2862 const Matcher<size_t> count_matcher_;
2866template <
typename M>
2867class ContainsTimesMatcher {
2869 explicit ContainsTimesMatcher(M m, Matcher<size_t> count_matcher)
2870 : inner_matcher_(m), count_matcher_(std::move(count_matcher)) {}
2872 template <
typename Container>
2873 operator Matcher<Container>()
const {
2874 return Matcher<Container>(
new ContainsTimesMatcherImpl<const Container&>(
2875 inner_matcher_, count_matcher_));
2879 const M inner_matcher_;
2880 const Matcher<size_t> count_matcher_;
2884template <
typename M>
2885class ContainsMatcher {
2887 explicit ContainsMatcher(M m) : inner_matcher_(m) {}
2889 template <
typename Container>
2890 operator Matcher<Container>()
const {
2891 return Matcher<Container>(
2892 new ContainsMatcherImpl<const Container&>(inner_matcher_));
2895 ContainsTimesMatcher<M> Times(Matcher<size_t> count_matcher)
const {
2896 return ContainsTimesMatcher<M>(inner_matcher_, std::move(count_matcher));
2900 const M inner_matcher_;
2904template <
typename M>
2907 explicit EachMatcher(M m) : inner_matcher_(m) {}
2909 template <
typename Container>
2910 operator Matcher<Container>()
const {
2911 return Matcher<Container>(
2912 new EachMatcherImpl<const Container&>(inner_matcher_));
2916 const M inner_matcher_;
2920struct Rank0 : Rank1 {};
2922namespace pair_getters {
2924template <
typename T>
2925auto First(T& x, Rank1) ->
decltype(get<0>(x)) {
2928template <
typename T>
2929auto First(T& x, Rank0) ->
decltype((x.first)) {
2933template <
typename T>
2934auto Second(T& x, Rank1) ->
decltype(get<1>(x)) {
2937template <
typename T>
2938auto Second(T& x, Rank0) ->
decltype((x.second)) {
2947template <
typename PairType>
2948class KeyMatcherImpl :
public MatcherInterface<PairType> {
2950 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(PairType) RawPairType;
2951 typedef typename RawPairType::first_type KeyType;
2953 template <
typename InnerMatcher>
2954 explicit KeyMatcherImpl(InnerMatcher inner_matcher)
2956 testing::SafeMatcherCast<const KeyType&>(inner_matcher)) {}
2960 bool MatchAndExplain(PairType key_value,
2961 MatchResultListener* listener)
const override {
2962 StringMatchResultListener inner_listener;
2963 const bool match = inner_matcher_.MatchAndExplain(
2964 pair_getters::First(key_value, Rank0()), &inner_listener);
2965 const std::string explanation = inner_listener.str();
2966 if (explanation !=
"") {
2967 *listener <<
"whose first field is a value " << explanation;
2973 void DescribeTo(::std::ostream* os)
const override {
2974 *os <<
"has a key that ";
2975 inner_matcher_.DescribeTo(os);
2979 void DescribeNegationTo(::std::ostream* os)
const override {
2980 *os <<
"doesn't have a key that ";
2981 inner_matcher_.DescribeTo(os);
2985 const Matcher<const KeyType&> inner_matcher_;
2989template <
typename M>
2992 explicit KeyMatcher(M m) : matcher_for_key_(m) {}
2994 template <
typename PairType>
2995 operator Matcher<PairType>()
const {
2996 return Matcher<PairType>(
2997 new KeyMatcherImpl<const PairType&>(matcher_for_key_));
3001 const M matcher_for_key_;
3005template <
typename InnerMatcher>
3006class AddressMatcher {
3008 explicit AddressMatcher(InnerMatcher m) : matcher_(m) {}
3010 template <
typename Type>
3011 operator Matcher<Type>()
const {
3012 return Matcher<Type>(
new Impl<const Type&>(matcher_));
3017 template <
typename Type>
3018 class Impl :
public MatcherInterface<Type> {
3020 using Address =
const GTEST_REMOVE_REFERENCE_AND_CONST_(Type) *;
3021 explicit Impl(
const InnerMatcher& matcher)
3022 : matcher_(MatcherCast<Address>(matcher)) {}
3024 void DescribeTo(::std::ostream* os)
const override {
3025 *os <<
"has address that ";
3026 matcher_.DescribeTo(os);
3029 void DescribeNegationTo(::std::ostream* os)
const override {
3030 *os <<
"does not have address that ";
3031 matcher_.DescribeTo(os);
3034 bool MatchAndExplain(Type
object,
3035 MatchResultListener* listener)
const override {
3036 *listener <<
"which has address ";
3037 Address address = std::addressof(
object);
3038 return MatchPrintAndExplain(address, matcher_, listener);
3042 const Matcher<Address> matcher_;
3044 const InnerMatcher matcher_;
3049template <
typename PairType>
3050class PairMatcherImpl :
public MatcherInterface<PairType> {
3052 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(PairType) RawPairType;
3053 typedef typename RawPairType::first_type FirstType;
3054 typedef typename RawPairType::second_type SecondType;
3056 template <
typename FirstMatcher,
typename SecondMatcher>
3057 PairMatcherImpl(FirstMatcher first_matcher, SecondMatcher second_matcher)
3059 testing::SafeMatcherCast<const FirstType&>(first_matcher)),
3061 testing::SafeMatcherCast<const SecondType&>(second_matcher)) {}
3064 void DescribeTo(::std::ostream* os)
const override {
3065 *os <<
"has a first field that ";
3066 first_matcher_.DescribeTo(os);
3067 *os <<
", and has a second field that ";
3068 second_matcher_.DescribeTo(os);
3072 void DescribeNegationTo(::std::ostream* os)
const override {
3073 *os <<
"has a first field that ";
3074 first_matcher_.DescribeNegationTo(os);
3075 *os <<
", or has a second field that ";
3076 second_matcher_.DescribeNegationTo(os);
3081 bool MatchAndExplain(PairType a_pair,
3082 MatchResultListener* listener)
const override {
3083 if (!listener->IsInterested()) {
3086 return first_matcher_.Matches(pair_getters::First(a_pair, Rank0())) &&
3087 second_matcher_.Matches(pair_getters::Second(a_pair, Rank0()));
3089 StringMatchResultListener first_inner_listener;
3090 if (!first_matcher_.MatchAndExplain(pair_getters::First(a_pair, Rank0()),
3091 &first_inner_listener)) {
3092 *listener <<
"whose first field does not match";
3093 PrintIfNotEmpty(first_inner_listener.str(), listener->stream());
3096 StringMatchResultListener second_inner_listener;
3097 if (!second_matcher_.MatchAndExplain(pair_getters::Second(a_pair, Rank0()),
3098 &second_inner_listener)) {
3099 *listener <<
"whose second field does not match";
3100 PrintIfNotEmpty(second_inner_listener.str(), listener->stream());
3103 ExplainSuccess(first_inner_listener.str(), second_inner_listener.str(),
3109 void ExplainSuccess(
const std::string& first_explanation,
3110 const std::string& second_explanation,
3111 MatchResultListener* listener)
const {
3112 *listener <<
"whose both fields match";
3113 if (first_explanation !=
"") {
3114 *listener <<
", where the first field is a value " << first_explanation;
3116 if (second_explanation !=
"") {
3118 if (first_explanation !=
"") {
3119 *listener <<
"and ";
3121 *listener <<
"where ";
3123 *listener <<
"the second field is a value " << second_explanation;
3127 const Matcher<const FirstType&> first_matcher_;
3128 const Matcher<const SecondType&> second_matcher_;
3132template <
typename FirstMatcher,
typename SecondMatcher>
3135 PairMatcher(FirstMatcher first_matcher, SecondMatcher second_matcher)
3136 : first_matcher_(first_matcher), second_matcher_(second_matcher) {}
3138 template <
typename PairType>
3139 operator Matcher<PairType>()
const {
3140 return Matcher<PairType>(
3141 new PairMatcherImpl<const PairType&>(first_matcher_, second_matcher_));
3145 const FirstMatcher first_matcher_;
3146 const SecondMatcher second_matcher_;
3149template <
typename T,
size_t... I>
3150auto UnpackStructImpl(
const T& t, IndexSequence<I...>,
int)
3151 ->
decltype(std::tie(get<I>(t)...)) {
3152 static_assert(std::tuple_size<T>::value ==
sizeof...(I),
3153 "Number of arguments doesn't match the number of fields.");
3154 return std::tie(get<I>(t)...);
3157#if defined(__cpp_structured_bindings) && __cpp_structured_bindings >= 201606
3158template <
typename T>
3159auto UnpackStructImpl(
const T& t, MakeIndexSequence<1>,
char) {
3160 const auto& [a] = t;
3163template <
typename T>
3164auto UnpackStructImpl(
const T& t, MakeIndexSequence<2>,
char) {
3165 const auto& [a, b] = t;
3166 return std::tie(a, b);
3168template <
typename T>
3169auto UnpackStructImpl(
const T& t, MakeIndexSequence<3>,
char) {
3170 const auto& [a, b, c] = t;
3171 return std::tie(a, b, c);
3173template <
typename T>
3174auto UnpackStructImpl(
const T& t, MakeIndexSequence<4>,
char) {
3175 const auto& [a, b, c, d] = t;
3176 return std::tie(a, b, c, d);
3178template <
typename T>
3179auto UnpackStructImpl(
const T& t, MakeIndexSequence<5>,
char) {
3180 const auto& [a, b, c, d, e] = t;
3181 return std::tie(a, b, c, d, e);
3183template <
typename T>
3184auto UnpackStructImpl(
const T& t, MakeIndexSequence<6>,
char) {
3185 const auto& [a, b, c, d, e, f] = t;
3186 return std::tie(a, b, c, d, e, f);
3188template <
typename T>
3189auto UnpackStructImpl(
const T& t, MakeIndexSequence<7>,
char) {
3190 const auto& [a, b, c, d, e, f, g] = t;
3191 return std::tie(a, b, c, d, e, f, g);
3193template <
typename T>
3194auto UnpackStructImpl(
const T& t, MakeIndexSequence<8>,
char) {
3195 const auto& [a, b, c, d, e, f, g, h] = t;
3196 return std::tie(a, b, c, d, e, f, g, h);
3198template <
typename T>
3199auto UnpackStructImpl(
const T& t, MakeIndexSequence<9>,
char) {
3200 const auto& [a, b, c, d, e, f, g, h, i] = t;
3201 return std::tie(a, b, c, d, e, f, g, h, i);
3203template <
typename T>
3204auto UnpackStructImpl(
const T& t, MakeIndexSequence<10>,
char) {
3205 const auto& [a, b, c, d, e, f, g, h, i, j] = t;
3206 return std::tie(a, b, c, d, e, f, g, h, i, j);
3208template <
typename T>
3209auto UnpackStructImpl(
const T& t, MakeIndexSequence<11>,
char) {
3210 const auto& [a, b, c, d, e, f, g, h, i, j, k] = t;
3211 return std::tie(a, b, c, d, e, f, g, h, i, j, k);
3213template <
typename T>
3214auto UnpackStructImpl(
const T& t, MakeIndexSequence<12>,
char) {
3215 const auto& [a, b, c, d, e, f, g, h, i, j, k, l] = t;
3216 return std::tie(a, b, c, d, e, f, g, h, i, j, k, l);
3218template <
typename T>
3219auto UnpackStructImpl(
const T& t, MakeIndexSequence<13>,
char) {
3220 const auto& [a, b, c, d, e, f, g, h, i, j, k, l, m] = t;
3221 return std::tie(a, b, c, d, e, f, g, h, i, j, k, l, m);
3223template <
typename T>
3224auto UnpackStructImpl(
const T& t, MakeIndexSequence<14>,
char) {
3225 const auto& [a, b, c, d, e, f, g, h, i, j, k, l, m, n] = t;
3226 return std::tie(a, b, c, d, e, f, g, h, i, j, k, l, m, n);
3228template <
typename T>
3229auto UnpackStructImpl(
const T& t, MakeIndexSequence<15>,
char) {
3230 const auto& [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o] = t;
3231 return std::tie(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o);
3233template <
typename T>
3234auto UnpackStructImpl(
const T& t, MakeIndexSequence<16>,
char) {
3235 const auto& [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p] = t;
3236 return std::tie(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p);
3238template <
typename T>
3239auto UnpackStructImpl(
const T& t, MakeIndexSequence<17>,
char) {
3240 const auto& [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q] = t;
3241 return std::tie(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q);
3243template <
typename T>
3244auto UnpackStructImpl(
const T& t, MakeIndexSequence<18>,
char) {
3245 const auto& [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r] = t;
3246 return std::tie(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r);
3248template <
typename T>
3249auto UnpackStructImpl(
const T& t, MakeIndexSequence<19>,
char) {
3250 const auto& [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s] = t;
3251 return std::tie(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s);
3255template <
size_t I,
typename T>
3256auto UnpackStruct(
const T& t)
3257 ->
decltype((UnpackStructImpl)(t, MakeIndexSequence<I>{}, 0)) {
3258 return (UnpackStructImpl)(t, MakeIndexSequence<I>{}, 0);
3264template <
typename T,
size_t N>
3265void VariadicExpand(
const T (&)[N]) {}
3267template <
typename Struct,
typename StructSize>
3268class FieldsAreMatcherImpl;
3270template <
typename Struct,
size_t... I>
3271class FieldsAreMatcherImpl<Struct, IndexSequence<I...>>
3272 :
public MatcherInterface<Struct> {
3273 using UnpackedType =
3274 decltype(UnpackStruct<
sizeof...(I)>(std::declval<const Struct&>()));
3275 using MatchersType = std::tuple<
3276 Matcher<const typename std::tuple_element<I, UnpackedType>::type&>...>;
3279 template <
typename Inner>
3280 explicit FieldsAreMatcherImpl(
const Inner& matchers)
3281 : matchers_(testing::SafeMatcherCast<
3282 const typename std::tuple_element<I, UnpackedType>::type&>(
3283 std::get<I>(matchers))...) {}
3285 void DescribeTo(::std::ostream* os)
const override {
3286 const char* separator =
"";
3288 {(*os << separator <<
"has field #" << I <<
" that ",
3289 std::get<I>(matchers_).DescribeTo(os), separator =
", and ")...});
3292 void DescribeNegationTo(::std::ostream* os)
const override {
3293 const char* separator =
"";
3294 VariadicExpand({(*os << separator <<
"has field #" << I <<
" that ",
3295 std::get<I>(matchers_).DescribeNegationTo(os),
3296 separator =
", or ")...});
3299 bool MatchAndExplain(Struct t, MatchResultListener* listener)
const override {
3300 return MatchInternal((UnpackStruct<
sizeof...(I)>)(t), listener);
3304 bool MatchInternal(UnpackedType tuple, MatchResultListener* listener)
const {
3305 if (!listener->IsInterested()) {
3309 VariadicExpand({good = good && std::get<I>(matchers_).Matches(
3310 std::get<I>(tuple))...});
3314 size_t failed_pos = ~size_t{};
3316 std::vector<StringMatchResultListener> inner_listener(
sizeof...(I));
3319 {failed_pos == ~size_t{}&& !std::get<I>(matchers_).MatchAndExplain(
3320 std::get<I>(tuple), &inner_listener[I])
3323 if (failed_pos != ~
size_t{}) {
3324 *listener <<
"whose field #" << failed_pos <<
" does not match";
3325 PrintIfNotEmpty(inner_listener[failed_pos].str(), listener->stream());
3329 *listener <<
"whose all elements match";
3330 const char* separator =
", where";
3331 for (
size_t index = 0; index <
sizeof...(I); ++index) {
3332 const std::string str = inner_listener[index].str();
3334 *listener << separator <<
" field #" << index <<
" is a value " << str;
3335 separator =
", and";
3342 MatchersType matchers_;
3345template <
typename... Inner>
3346class FieldsAreMatcher {
3348 explicit FieldsAreMatcher(Inner... inner) : matchers_(std::move(inner)...) {}
3350 template <
typename Struct>
3351 operator Matcher<Struct>()
const {
3352 return Matcher<Struct>(
3353 new FieldsAreMatcherImpl<
const Struct&, IndexSequenceFor<Inner...>>(
3358 std::tuple<Inner...> matchers_;
3362template <
typename Container>
3363class ElementsAreMatcherImpl :
public MatcherInterface<Container> {
3365 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
3366 typedef internal::StlContainerView<RawContainer> View;
3367 typedef typename View::type StlContainer;
3368 typedef typename View::const_reference StlContainerReference;
3369 typedef typename StlContainer::value_type Element;
3373 template <
typename InputIter>
3374 ElementsAreMatcherImpl(InputIter first, InputIter last) {
3375 while (first != last) {
3376 matchers_.push_back(MatcherCast<const Element&>(*first++));
3381 void DescribeTo(::std::ostream* os)
const override {
3384 }
else if (count() == 1) {
3385 *os <<
"has 1 element that ";
3386 matchers_[0].DescribeTo(os);
3388 *os <<
"has " << Elements(count()) <<
" where\n";
3389 for (
size_t i = 0; i != count(); ++i) {
3390 *os <<
"element #" << i <<
" ";
3391 matchers_[i].DescribeTo(os);
3392 if (i + 1 < count()) {
3400 void DescribeNegationTo(::std::ostream* os)
const override {
3402 *os <<
"isn't empty";
3406 *os <<
"doesn't have " << Elements(count()) <<
", or\n";
3407 for (
size_t i = 0; i != count(); ++i) {
3408 *os <<
"element #" << i <<
" ";
3409 matchers_[i].DescribeNegationTo(os);
3410 if (i + 1 < count()) {
3416 bool MatchAndExplain(Container container,
3417 MatchResultListener* listener)
const override {
3421 const bool listener_interested = listener->IsInterested();
3424 ::std::vector<std::string> explanations(count());
3425 StlContainerReference stl_container = View::ConstReference(container);
3426 auto it = stl_container.begin();
3427 size_t exam_pos = 0;
3428 bool mismatch_found =
false;
3433 for (; it != stl_container.end() && exam_pos != count(); ++it, ++exam_pos) {
3435 if (listener_interested) {
3436 StringMatchResultListener s;
3437 match = matchers_[exam_pos].MatchAndExplain(*it, &s);
3438 explanations[exam_pos] = s.str();
3440 match = matchers_[exam_pos].Matches(*it);
3444 mismatch_found =
true;
3453 size_t actual_count = exam_pos;
3454 for (; it != stl_container.end(); ++it) {
3458 if (actual_count != count()) {
3463 if (listener_interested && (actual_count != 0)) {
3464 *listener <<
"which has " << Elements(actual_count);
3469 if (mismatch_found) {
3471 if (listener_interested) {
3472 *listener <<
"whose element #" << exam_pos <<
" doesn't match";
3473 PrintIfNotEmpty(explanations[exam_pos], listener->stream());
3480 if (listener_interested) {
3481 bool reason_printed =
false;
3482 for (
size_t i = 0; i != count(); ++i) {
3483 const std::string& s = explanations[i];
3485 if (reason_printed) {
3486 *listener <<
",\nand ";
3488 *listener <<
"whose element #" << i <<
" matches, " << s;
3489 reason_printed =
true;
3497 static Message Elements(
size_t count) {
3498 return Message() << count << (count == 1 ?
" element" :
" elements");
3501 size_t count()
const {
return matchers_.size(); }
3503 ::std::vector<Matcher<const Element&>> matchers_;
3510class GTEST_API_ MatchMatrix {
3512 MatchMatrix(
size_t num_elements,
size_t num_matchers)
3513 : num_elements_(num_elements),
3514 num_matchers_(num_matchers),
3515 matched_(num_elements_ * num_matchers_, 0) {}
3517 size_t LhsSize()
const {
return num_elements_; }
3518 size_t RhsSize()
const {
return num_matchers_; }
3519 bool HasEdge(
size_t ilhs,
size_t irhs)
const {
3520 return matched_[SpaceIndex(ilhs, irhs)] == 1;
3522 void SetEdge(
size_t ilhs,
size_t irhs,
bool b) {
3523 matched_[SpaceIndex(ilhs, irhs)] = b ? 1 : 0;
3533 std::string DebugString()
const;
3536 size_t SpaceIndex(
size_t ilhs,
size_t irhs)
const {
3537 return ilhs * num_matchers_ + irhs;
3540 size_t num_elements_;
3541 size_t num_matchers_;
3546 ::std::vector<char> matched_;
3549typedef ::std::pair<size_t, size_t> ElementMatcherPair;
3550typedef ::std::vector<ElementMatcherPair> ElementMatcherPairs;
3554GTEST_API_ ElementMatcherPairs FindMaxBipartiteMatching(
const MatchMatrix& g);
3556struct UnorderedMatcherRequire {
3560 ExactMatch = Superset | Subset,
3567class GTEST_API_ UnorderedElementsAreMatcherImplBase {
3569 explicit UnorderedElementsAreMatcherImplBase(
3570 UnorderedMatcherRequire::Flags matcher_flags)
3571 : match_flags_(matcher_flags) {}
3576 typedef ::std::vector<const MatcherDescriberInterface*> MatcherDescriberVec;
3579 void DescribeToImpl(::std::ostream* os)
const;
3582 void DescribeNegationToImpl(::std::ostream* os)
const;
3584 bool VerifyMatchMatrix(const ::std::vector<std::string>& element_printouts,
3585 const MatchMatrix& matrix,
3586 MatchResultListener* listener)
const;
3588 bool FindPairing(
const MatchMatrix& matrix,
3589 MatchResultListener* listener)
const;
3591 MatcherDescriberVec& matcher_describers() {
return matcher_describers_; }
3593 static Message Elements(
size_t n) {
3594 return Message() << n <<
" element" << (n == 1 ?
"" :
"s");
3597 UnorderedMatcherRequire::Flags match_flags()
const {
return match_flags_; }
3600 UnorderedMatcherRequire::Flags match_flags_;
3601 MatcherDescriberVec matcher_describers_;
3606template <
typename Container>
3607class UnorderedElementsAreMatcherImpl
3608 :
public MatcherInterface<Container>,
3609 public UnorderedElementsAreMatcherImplBase {
3611 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
3612 typedef internal::StlContainerView<RawContainer> View;
3613 typedef typename View::type StlContainer;
3614 typedef typename View::const_reference StlContainerReference;
3615 typedef typename StlContainer::value_type Element;
3617 template <
typename InputIter>
3618 UnorderedElementsAreMatcherImpl(UnorderedMatcherRequire::Flags matcher_flags,
3619 InputIter first, InputIter last)
3620 : UnorderedElementsAreMatcherImplBase(matcher_flags) {
3621 for (; first != last; ++first) {
3622 matchers_.push_back(MatcherCast<const Element&>(*first));
3624 for (
const auto& m : matchers_) {
3625 matcher_describers().push_back(m.GetDescriber());
3630 void DescribeTo(::std::ostream* os)
const override {
3631 return UnorderedElementsAreMatcherImplBase::DescribeToImpl(os);
3635 void DescribeNegationTo(::std::ostream* os)
const override {
3636 return UnorderedElementsAreMatcherImplBase::DescribeNegationToImpl(os);
3639 bool MatchAndExplain(Container container,
3640 MatchResultListener* listener)
const override {
3641 StlContainerReference stl_container = View::ConstReference(container);
3642 ::std::vector<std::string> element_printouts;
3643 MatchMatrix matrix =
3644 AnalyzeElements(stl_container.begin(), stl_container.end(),
3645 &element_printouts, listener);
3647 if (matrix.LhsSize() == 0 && matrix.RhsSize() == 0) {
3651 if (match_flags() == UnorderedMatcherRequire::ExactMatch) {
3652 if (matrix.LhsSize() != matrix.RhsSize()) {
3657 if (matrix.LhsSize() != 0 && listener->IsInterested()) {
3658 *listener <<
"which has " << Elements(matrix.LhsSize());
3664 return VerifyMatchMatrix(element_printouts, matrix, listener) &&
3665 FindPairing(matrix, listener);
3669 template <
typename ElementIter>
3670 MatchMatrix AnalyzeElements(ElementIter elem_first, ElementIter elem_last,
3671 ::std::vector<std::string>* element_printouts,
3672 MatchResultListener* listener)
const {
3673 element_printouts->clear();
3674 ::std::vector<char> did_match;
3675 size_t num_elements = 0;
3676 DummyMatchResultListener dummy;
3677 for (; elem_first != elem_last; ++num_elements, ++elem_first) {
3678 if (listener->IsInterested()) {
3679 element_printouts->push_back(PrintToString(*elem_first));
3681 for (
size_t irhs = 0; irhs != matchers_.size(); ++irhs) {
3682 did_match.push_back(
3683 matchers_[irhs].MatchAndExplain(*elem_first, &dummy));
3687 MatchMatrix matrix(num_elements, matchers_.size());
3688 ::std::vector<char>::const_iterator did_match_iter = did_match.begin();
3689 for (
size_t ilhs = 0; ilhs != num_elements; ++ilhs) {
3690 for (
size_t irhs = 0; irhs != matchers_.size(); ++irhs) {
3691 matrix.SetEdge(ilhs, irhs, *did_match_iter++ != 0);
3697 ::std::vector<Matcher<const Element&>> matchers_;
3702template <
typename Target>
3703struct CastAndAppendTransform {
3704 template <
typename Arg>
3705 Matcher<Target> operator()(
const Arg& a)
const {
3706 return MatcherCast<Target>(a);
3711template <
typename MatcherTuple>
3712class UnorderedElementsAreMatcher {
3714 explicit UnorderedElementsAreMatcher(
const MatcherTuple& args)
3715 : matchers_(args) {}
3717 template <
typename Container>
3718 operator Matcher<Container>()
const {
3719 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
3720 typedef typename internal::StlContainerView<RawContainer>::type View;
3721 typedef typename View::value_type Element;
3722 typedef ::std::vector<Matcher<const Element&>> MatcherVec;
3723 MatcherVec matchers;
3724 matchers.reserve(::std::tuple_size<MatcherTuple>::value);
3725 TransformTupleValues(CastAndAppendTransform<const Element&>(), matchers_,
3726 ::std::back_inserter(matchers));
3727 return Matcher<Container>(
3728 new UnorderedElementsAreMatcherImpl<const Container&>(
3729 UnorderedMatcherRequire::ExactMatch, matchers.begin(),
3734 const MatcherTuple matchers_;
3738template <
typename MatcherTuple>
3739class ElementsAreMatcher {
3741 explicit ElementsAreMatcher(
const MatcherTuple& args) : matchers_(args) {}
3743 template <
typename Container>
3744 operator Matcher<Container>()
const {
3746 !IsHashTable<GTEST_REMOVE_REFERENCE_AND_CONST_(Container)>::value ||
3747 ::std::tuple_size<MatcherTuple>::value < 2,
3748 "use UnorderedElementsAre with hash tables");
3750 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
3751 typedef typename internal::StlContainerView<RawContainer>::type View;
3752 typedef typename View::value_type Element;
3753 typedef ::std::vector<Matcher<const Element&>> MatcherVec;
3754 MatcherVec matchers;
3755 matchers.reserve(::std::tuple_size<MatcherTuple>::value);
3756 TransformTupleValues(CastAndAppendTransform<const Element&>(), matchers_,
3757 ::std::back_inserter(matchers));
3758 return Matcher<Container>(
new ElementsAreMatcherImpl<const Container&>(
3759 matchers.begin(), matchers.end()));
3763 const MatcherTuple matchers_;
3767template <
typename T>
3768class UnorderedElementsAreArrayMatcher {
3770 template <
typename Iter>
3771 UnorderedElementsAreArrayMatcher(UnorderedMatcherRequire::Flags match_flags,
3772 Iter first, Iter last)
3773 : match_flags_(match_flags), matchers_(first, last) {}
3775 template <
typename Container>
3776 operator Matcher<Container>()
const {
3777 return Matcher<Container>(
3778 new UnorderedElementsAreMatcherImpl<const Container&>(
3779 match_flags_, matchers_.begin(), matchers_.end()));
3783 UnorderedMatcherRequire::Flags match_flags_;
3784 ::std::vector<T> matchers_;
3788template <
typename T>
3789class ElementsAreArrayMatcher {
3791 template <
typename Iter>
3792 ElementsAreArrayMatcher(Iter first, Iter last) : matchers_(first, last) {}
3794 template <
typename Container>
3795 operator Matcher<Container>()
const {
3797 !IsHashTable<GTEST_REMOVE_REFERENCE_AND_CONST_(Container)>::value,
3798 "use UnorderedElementsAreArray with hash tables");
3800 return Matcher<Container>(
new ElementsAreMatcherImpl<const Container&>(
3801 matchers_.begin(), matchers_.end()));
3805 const ::std::vector<T> matchers_;
3817template <
typename Tuple2Matcher,
typename Second>
3818class BoundSecondMatcher {
3820 BoundSecondMatcher(
const Tuple2Matcher& tm,
const Second& second)
3821 : tuple2_matcher_(tm), second_value_(second) {}
3823 BoundSecondMatcher(
const BoundSecondMatcher& other) =
default;
3825 template <
typename T>
3826 operator Matcher<T>()
const {
3827 return MakeMatcher(
new Impl<T>(tuple2_matcher_, second_value_));
3838 void operator=(
const BoundSecondMatcher& ) {
3839 GTEST_LOG_(FATAL) <<
"BoundSecondMatcher should never be assigned.";
3843 template <
typename T>
3844 class Impl :
public MatcherInterface<T> {
3846 typedef ::std::tuple<T, Second> ArgTuple;
3848 Impl(
const Tuple2Matcher& tm,
const Second& second)
3849 : mono_tuple2_matcher_(SafeMatcherCast<const ArgTuple&>(tm)),
3850 second_value_(second) {}
3852 void DescribeTo(::std::ostream* os)
const override {
3854 UniversalPrint(second_value_, os);
3856 mono_tuple2_matcher_.DescribeTo(os);
3859 bool MatchAndExplain(T x, MatchResultListener* listener)
const override {
3860 return mono_tuple2_matcher_.MatchAndExplain(ArgTuple(x, second_value_),
3865 const Matcher<const ArgTuple&> mono_tuple2_matcher_;
3866 const Second second_value_;
3869 const Tuple2Matcher tuple2_matcher_;
3870 const Second second_value_;
3877template <
typename Tuple2Matcher,
typename Second>
3878BoundSecondMatcher<Tuple2Matcher, Second> MatcherBindSecond(
3879 const Tuple2Matcher& tm,
const Second& second) {
3880 return BoundSecondMatcher<Tuple2Matcher, Second>(tm, second);
3888GTEST_API_ std::string FormatMatcherDescription(
3889 bool negation,
const char* matcher_name,
3890 const std::vector<const char*>& param_names,
const Strings& param_values);
3893template <
typename ValueMatcher>
3894class OptionalMatcher {
3896 explicit OptionalMatcher(
const ValueMatcher& value_matcher)
3897 : value_matcher_(value_matcher) {}
3899 template <
typename Optional>
3900 operator Matcher<Optional>()
const {
3901 return Matcher<Optional>(
new Impl<const Optional&>(value_matcher_));
3904 template <
typename Optional>
3905 class Impl :
public MatcherInterface<Optional> {
3907 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Optional) OptionalView;
3908 typedef typename OptionalView::value_type ValueType;
3909 explicit Impl(
const ValueMatcher& value_matcher)
3910 : value_matcher_(MatcherCast<ValueType>(value_matcher)) {}
3912 void DescribeTo(::std::ostream* os)
const override {
3914 value_matcher_.DescribeTo(os);
3917 void DescribeNegationTo(::std::ostream* os)
const override {
3919 value_matcher_.DescribeNegationTo(os);
3922 bool MatchAndExplain(Optional optional,
3923 MatchResultListener* listener)
const override {
3925 *listener <<
"which is not engaged";
3928 const ValueType& value = *optional;
3929 StringMatchResultListener value_listener;
3930 const bool match = value_matcher_.MatchAndExplain(value, &value_listener);
3931 *listener <<
"whose value " << PrintToString(value)
3932 << (match ?
" matches" :
" doesn't match");
3933 PrintIfNotEmpty(value_listener.str(), listener->stream());
3938 const Matcher<ValueType> value_matcher_;
3942 const ValueMatcher value_matcher_;
3945namespace variant_matcher {
3947template <
typename T>
3948void holds_alternative() {}
3949template <
typename T>
3953template <
typename T>
3954class VariantMatcher {
3957 : matcher_(std::move(matcher)) {}
3959 template <
typename Variant>
3960 bool MatchAndExplain(
const Variant& value,
3961 ::testing::MatchResultListener* listener)
const {
3963 if (!listener->IsInterested()) {
3964 return holds_alternative<T>(value) && matcher_.Matches(get<T>(value));
3967 if (!holds_alternative<T>(value)) {
3968 *listener <<
"whose value is not of type '" << GetTypeName() <<
"'";
3972 const T& elem = get<T>(value);
3973 StringMatchResultListener elem_listener;
3974 const bool match = matcher_.MatchAndExplain(elem, &elem_listener);
3975 *listener <<
"whose value " << PrintToString(elem)
3976 << (match ?
" matches" :
" doesn't match");
3977 PrintIfNotEmpty(elem_listener.str(), listener->stream());
3981 void DescribeTo(std::ostream* os)
const {
3982 *os <<
"is a variant<> with value of type '" << GetTypeName()
3983 <<
"' and the value ";
3984 matcher_.DescribeTo(os);
3987 void DescribeNegationTo(std::ostream* os)
const {
3988 *os <<
"is a variant<> with value of type other than '" << GetTypeName()
3989 <<
"' or the value ";
3990 matcher_.DescribeNegationTo(os);
3994 static std::string GetTypeName() {
3996 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(
3997 return internal::GetTypeName<T>());
3999 return "the element type";
4002 const ::testing::Matcher<const T&> matcher_;
4007namespace any_cast_matcher {
4010template <
typename T>
4014template <
typename T>
4015class AnyCastMatcher {
4017 explicit AnyCastMatcher(const ::testing::Matcher<const T&>& matcher)
4018 : matcher_(matcher) {}
4020 template <
typename AnyType>
4021 bool MatchAndExplain(
const AnyType& value,
4022 ::testing::MatchResultListener* listener)
const {
4023 if (!listener->IsInterested()) {
4024 const T* ptr = any_cast<T>(&value);
4025 return ptr !=
nullptr && matcher_.Matches(*ptr);
4028 const T* elem = any_cast<T>(&value);
4029 if (elem ==
nullptr) {
4030 *listener <<
"whose value is not of type '" << GetTypeName() <<
"'";
4034 StringMatchResultListener elem_listener;
4035 const bool match = matcher_.MatchAndExplain(*elem, &elem_listener);
4036 *listener <<
"whose value " << PrintToString(*elem)
4037 << (match ?
" matches" :
" doesn't match");
4038 PrintIfNotEmpty(elem_listener.str(), listener->stream());
4042 void DescribeTo(std::ostream* os)
const {
4043 *os <<
"is an 'any' type with value of type '" << GetTypeName()
4044 <<
"' and the value ";
4045 matcher_.DescribeTo(os);
4048 void DescribeNegationTo(std::ostream* os)
const {
4049 *os <<
"is an 'any' type with value of type other than '" << GetTypeName()
4050 <<
"' or the value ";
4051 matcher_.DescribeNegationTo(os);
4055 static std::string GetTypeName() {
4057 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(
4058 return internal::GetTypeName<T>());
4060 return "the element type";
4063 const ::testing::Matcher<const T&> matcher_;
4069template <
class ArgsTuple,
size_t... k>
4070class ArgsMatcherImpl :
public MatcherInterface<ArgsTuple> {
4072 using RawArgsTuple =
typename std::decay<ArgsTuple>::type;
4073 using SelectedArgs =
4074 std::tuple<typename std::tuple_element<k, RawArgsTuple>::type...>;
4075 using MonomorphicInnerMatcher = Matcher<const SelectedArgs&>;
4077 template <
typename InnerMatcher>
4078 explicit ArgsMatcherImpl(
const InnerMatcher& inner_matcher)
4079 : inner_matcher_(SafeMatcherCast<const SelectedArgs&>(inner_matcher)) {}
4081 bool MatchAndExplain(ArgsTuple args,
4082 MatchResultListener* listener)
const override {
4085 const SelectedArgs& selected_args =
4086 std::forward_as_tuple(std::get<k>(args)...);
4087 if (!listener->IsInterested())
return inner_matcher_.Matches(selected_args);
4089 PrintIndices(listener->stream());
4090 *listener <<
"are " << PrintToString(selected_args);
4092 StringMatchResultListener inner_listener;
4094 inner_matcher_.MatchAndExplain(selected_args, &inner_listener);
4095 PrintIfNotEmpty(inner_listener.str(), listener->stream());
4099 void DescribeTo(::std::ostream* os)
const override {
4100 *os <<
"are a tuple ";
4102 inner_matcher_.DescribeTo(os);
4105 void DescribeNegationTo(::std::ostream* os)
const override {
4106 *os <<
"are a tuple ";
4108 inner_matcher_.DescribeNegationTo(os);
4113 static void PrintIndices(::std::ostream* os) {
4114 *os <<
"whose fields (";
4115 const char* sep =
"";
4118 const char* dummy[] = {
"", (*os << sep <<
"#" << k, sep =
", ")...};
4123 MonomorphicInnerMatcher inner_matcher_;
4126template <
class InnerMatcher,
size_t... k>
4129 explicit ArgsMatcher(InnerMatcher inner_matcher)
4130 : inner_matcher_(std::move(inner_matcher)) {}
4132 template <
typename ArgsTuple>
4133 operator Matcher<ArgsTuple>()
const {
4134 return MakeMatcher(
new ArgsMatcherImpl<ArgsTuple, k...>(inner_matcher_));
4138 InnerMatcher inner_matcher_;
4158template <
typename Iter>
4159inline internal::ElementsAreArrayMatcher<
4160 typename ::std::iterator_traits<Iter>::value_type>
4161ElementsAreArray(Iter first, Iter last) {
4162 typedef typename ::std::iterator_traits<Iter>::value_type T;
4163 return internal::ElementsAreArrayMatcher<T>(first, last);
4166template <
typename T>
4167inline auto ElementsAreArray(
const T* pointer,
size_t count)
4168 ->
decltype(ElementsAreArray(pointer, pointer + count)) {
4169 return ElementsAreArray(pointer, pointer + count);
4172template <
typename T,
size_t N>
4173inline auto ElementsAreArray(
const T (&array)[N])
4174 ->
decltype(ElementsAreArray(array, N)) {
4175 return ElementsAreArray(array, N);
4178template <
typename Container>
4179inline auto ElementsAreArray(
const Container& container)
4180 ->
decltype(ElementsAreArray(container.begin(), container.end())) {
4181 return ElementsAreArray(container.begin(), container.end());
4184template <
typename T>
4185inline auto ElementsAreArray(::std::initializer_list<T> xs)
4186 ->
decltype(ElementsAreArray(xs.begin(), xs.end())) {
4187 return ElementsAreArray(xs.begin(), xs.end());
4203template <
typename Iter>
4204inline internal::UnorderedElementsAreArrayMatcher<
4205 typename ::std::iterator_traits<Iter>::value_type>
4206UnorderedElementsAreArray(Iter first, Iter last) {
4207 typedef typename ::std::iterator_traits<Iter>::value_type T;
4208 return internal::UnorderedElementsAreArrayMatcher<T>(
4209 internal::UnorderedMatcherRequire::ExactMatch, first, last);
4212template <
typename T>
4213inline internal::UnorderedElementsAreArrayMatcher<T> UnorderedElementsAreArray(
4214 const T* pointer,
size_t count) {
4215 return UnorderedElementsAreArray(pointer, pointer + count);
4218template <
typename T,
size_t N>
4219inline internal::UnorderedElementsAreArrayMatcher<T> UnorderedElementsAreArray(
4220 const T (&array)[N]) {
4221 return UnorderedElementsAreArray(array, N);
4224template <
typename Container>
4225inline internal::UnorderedElementsAreArrayMatcher<
4226 typename Container::value_type>
4227UnorderedElementsAreArray(
const Container& container) {
4228 return UnorderedElementsAreArray(container.begin(), container.end());
4231template <
typename T>
4232inline internal::UnorderedElementsAreArrayMatcher<T> UnorderedElementsAreArray(
4233 ::std::initializer_list<T> xs) {
4234 return UnorderedElementsAreArray(xs.begin(), xs.end());
4246const internal::AnythingMatcher _ = {};
4248template <
typename T>
4249inline Matcher<T> A() {
4254template <
typename T>
4255inline Matcher<T> An() {
4259template <
typename T,
typename M>
4260Matcher<T> internal::MatcherCastImpl<T, M>::CastImpl(
4261 const M& value, std::false_type ,
4267inline PolymorphicMatcher<internal::IsNullMatcher> IsNull() {
4268 return MakePolymorphicMatcher(internal::IsNullMatcher());
4274inline PolymorphicMatcher<internal::NotNullMatcher> NotNull() {
4275 return MakePolymorphicMatcher(internal::NotNullMatcher());
4280template <
typename T>
4281inline internal::RefMatcher<T&> Ref(T& x) {
4282 return internal::RefMatcher<T&>(x);
4286inline PolymorphicMatcher<internal::IsNanMatcher> IsNan() {
4287 return MakePolymorphicMatcher(internal::IsNanMatcher());
4292inline internal::FloatingEqMatcher<double> DoubleEq(
double rhs) {
4293 return internal::FloatingEqMatcher<double>(rhs,
false);
4298inline internal::FloatingEqMatcher<double> NanSensitiveDoubleEq(
double rhs) {
4299 return internal::FloatingEqMatcher<double>(rhs,
true);
4305inline internal::FloatingEqMatcher<double> DoubleNear(
double rhs,
4306 double max_abs_error) {
4307 return internal::FloatingEqMatcher<double>(rhs,
false, max_abs_error);
4313inline internal::FloatingEqMatcher<double> NanSensitiveDoubleNear(
4314 double rhs,
double max_abs_error) {
4315 return internal::FloatingEqMatcher<double>(rhs,
true, max_abs_error);
4320inline internal::FloatingEqMatcher<float> FloatEq(
float rhs) {
4321 return internal::FloatingEqMatcher<float>(rhs,
false);
4326inline internal::FloatingEqMatcher<float> NanSensitiveFloatEq(
float rhs) {
4327 return internal::FloatingEqMatcher<float>(rhs,
true);
4333inline internal::FloatingEqMatcher<float> FloatNear(
float rhs,
4334 float max_abs_error) {
4335 return internal::FloatingEqMatcher<float>(rhs,
false, max_abs_error);
4341inline internal::FloatingEqMatcher<float> NanSensitiveFloatNear(
4342 float rhs,
float max_abs_error) {
4343 return internal::FloatingEqMatcher<float>(rhs,
true, max_abs_error);
4348template <
typename InnerMatcher>
4349inline internal::PointeeMatcher<InnerMatcher> Pointee(
4350 const InnerMatcher& inner_matcher) {
4351 return internal::PointeeMatcher<InnerMatcher>(inner_matcher);
4361template <
typename To>
4362inline PolymorphicMatcher<internal::WhenDynamicCastToMatcher<To>>
4363WhenDynamicCastTo(
const Matcher<To>& inner_matcher) {
4364 return MakePolymorphicMatcher(
4365 internal::WhenDynamicCastToMatcher<To>(inner_matcher));
4373template <
typename Class,
typename FieldType,
typename FieldMatcher>
4374inline PolymorphicMatcher<internal::FieldMatcher<Class, FieldType>> Field(
4375 FieldType Class::*field,
const FieldMatcher& matcher) {
4376 return MakePolymorphicMatcher(internal::FieldMatcher<Class, FieldType>(
4377 field, MatcherCast<const FieldType&>(matcher)));
4386template <
typename Class,
typename FieldType,
typename FieldMatcher>
4387inline PolymorphicMatcher<internal::FieldMatcher<Class, FieldType>> Field(
4388 const std::string& field_name, FieldType Class::*field,
4389 const FieldMatcher& matcher) {
4390 return MakePolymorphicMatcher(internal::FieldMatcher<Class, FieldType>(
4391 field_name, field, MatcherCast<const FieldType&>(matcher)));
4398template <
typename Class,
typename PropertyType,
typename PropertyMatcher>
4399inline PolymorphicMatcher<internal::PropertyMatcher<
4400 Class, PropertyType, PropertyType (Class::*)()
const>>
4401Property(PropertyType (Class::*property)()
const,
4402 const PropertyMatcher& matcher) {
4403 return MakePolymorphicMatcher(
4404 internal::PropertyMatcher<Class, PropertyType,
4405 PropertyType (Class::*)()
const>(
4406 property, MatcherCast<const PropertyType&>(matcher)));
4415template <
typename Class,
typename PropertyType,
typename PropertyMatcher>
4416inline PolymorphicMatcher<internal::PropertyMatcher<
4417 Class, PropertyType, PropertyType (Class::*)()
const>>
4418Property(
const std::string& property_name,
4419 PropertyType (Class::*property)()
const,
4420 const PropertyMatcher& matcher) {
4421 return MakePolymorphicMatcher(
4422 internal::PropertyMatcher<Class, PropertyType,
4423 PropertyType (Class::*)()
const>(
4424 property_name, property, MatcherCast<const PropertyType&>(matcher)));
4428template <
typename Class,
typename PropertyType,
typename PropertyMatcher>
4429inline PolymorphicMatcher<internal::PropertyMatcher<
4430 Class, PropertyType, PropertyType (Class::*)()
const&>>
4431Property(PropertyType (Class::*property)()
const&,
4432 const PropertyMatcher& matcher) {
4433 return MakePolymorphicMatcher(
4434 internal::PropertyMatcher<Class, PropertyType,
4435 PropertyType (Class::*)()
const&>(
4436 property, MatcherCast<const PropertyType&>(matcher)));
4440template <
typename Class,
typename PropertyType,
typename PropertyMatcher>
4441inline PolymorphicMatcher<internal::PropertyMatcher<
4442 Class, PropertyType, PropertyType (Class::*)()
const&>>
4443Property(
const std::string& property_name,
4444 PropertyType (Class::*property)()
const&,
4445 const PropertyMatcher& matcher) {
4446 return MakePolymorphicMatcher(
4447 internal::PropertyMatcher<Class, PropertyType,
4448 PropertyType (Class::*)()
const&>(
4449 property_name, property, MatcherCast<const PropertyType&>(matcher)));
4460template <
typename Callable,
typename InnerMatcher>
4461internal::ResultOfMatcher<Callable, InnerMatcher> ResultOf(
4462 Callable callable, InnerMatcher matcher) {
4463 return internal::ResultOfMatcher<Callable, InnerMatcher>(std::move(callable),
4464 std::move(matcher));
4469template <
typename Callable,
typename InnerMatcher>
4470internal::ResultOfMatcher<Callable, InnerMatcher> ResultOf(
4471 const std::string& result_description, Callable callable,
4472 InnerMatcher matcher) {
4473 return internal::ResultOfMatcher<Callable, InnerMatcher>(
4474 result_description, std::move(callable), std::move(matcher));
4480template <
typename T = std::
string>
4481PolymorphicMatcher<internal::StrEqualityMatcher<std::string>> StrEq(
4482 const internal::StringLike<T>& str) {
4483 return MakePolymorphicMatcher(
4484 internal::StrEqualityMatcher<std::string>(std::string(str),
true,
true));
4488template <
typename T = std::
string>
4489PolymorphicMatcher<internal::StrEqualityMatcher<std::string>> StrNe(
4490 const internal::StringLike<T>& str) {
4491 return MakePolymorphicMatcher(
4492 internal::StrEqualityMatcher<std::string>(std::string(str),
false,
true));
4496template <
typename T = std::
string>
4497PolymorphicMatcher<internal::StrEqualityMatcher<std::string>> StrCaseEq(
4498 const internal::StringLike<T>& str) {
4499 return MakePolymorphicMatcher(
4500 internal::StrEqualityMatcher<std::string>(std::string(str),
true,
false));
4504template <
typename T = std::
string>
4505PolymorphicMatcher<internal::StrEqualityMatcher<std::string>> StrCaseNe(
4506 const internal::StringLike<T>& str) {
4507 return MakePolymorphicMatcher(internal::StrEqualityMatcher<std::string>(
4508 std::string(str),
false,
false));
4513template <
typename T = std::
string>
4514PolymorphicMatcher<internal::HasSubstrMatcher<std::string>> HasSubstr(
4515 const internal::StringLike<T>& substring) {
4516 return MakePolymorphicMatcher(
4517 internal::HasSubstrMatcher<std::string>(std::string(substring)));
4521template <
typename T = std::
string>
4522PolymorphicMatcher<internal::StartsWithMatcher<std::string>> StartsWith(
4523 const internal::StringLike<T>& prefix) {
4524 return MakePolymorphicMatcher(
4525 internal::StartsWithMatcher<std::string>(std::string(prefix)));
4529template <
typename T = std::
string>
4530PolymorphicMatcher<internal::EndsWithMatcher<std::string>> EndsWith(
4531 const internal::StringLike<T>& suffix) {
4532 return MakePolymorphicMatcher(
4533 internal::EndsWithMatcher<std::string>(std::string(suffix)));
4536#if GTEST_HAS_STD_WSTRING
4540inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring>> StrEq(
4541 const std::wstring& str) {
4542 return MakePolymorphicMatcher(
4543 internal::StrEqualityMatcher<std::wstring>(str,
true,
true));
4547inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring>> StrNe(
4548 const std::wstring& str) {
4549 return MakePolymorphicMatcher(
4550 internal::StrEqualityMatcher<std::wstring>(str,
false,
true));
4554inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring>> StrCaseEq(
4555 const std::wstring& str) {
4556 return MakePolymorphicMatcher(
4557 internal::StrEqualityMatcher<std::wstring>(str,
true,
false));
4561inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring>> StrCaseNe(
4562 const std::wstring& str) {
4563 return MakePolymorphicMatcher(
4564 internal::StrEqualityMatcher<std::wstring>(str,
false,
false));
4569inline PolymorphicMatcher<internal::HasSubstrMatcher<std::wstring>> HasSubstr(
4570 const std::wstring& substring) {
4571 return MakePolymorphicMatcher(
4572 internal::HasSubstrMatcher<std::wstring>(substring));
4576inline PolymorphicMatcher<internal::StartsWithMatcher<std::wstring>> StartsWith(
4577 const std::wstring& prefix) {
4578 return MakePolymorphicMatcher(
4579 internal::StartsWithMatcher<std::wstring>(prefix));
4583inline PolymorphicMatcher<internal::EndsWithMatcher<std::wstring>> EndsWith(
4584 const std::wstring& suffix) {
4585 return MakePolymorphicMatcher(
4586 internal::EndsWithMatcher<std::wstring>(suffix));
4593inline internal::Eq2Matcher Eq() {
return internal::Eq2Matcher(); }
4597inline internal::Ge2Matcher Ge() {
return internal::Ge2Matcher(); }
4601inline internal::Gt2Matcher Gt() {
return internal::Gt2Matcher(); }
4605inline internal::Le2Matcher Le() {
return internal::Le2Matcher(); }
4609inline internal::Lt2Matcher Lt() {
return internal::Lt2Matcher(); }
4613inline internal::Ne2Matcher Ne() {
return internal::Ne2Matcher(); }
4617inline internal::FloatingEq2Matcher<float> FloatEq() {
4618 return internal::FloatingEq2Matcher<float>();
4623inline internal::FloatingEq2Matcher<double> DoubleEq() {
4624 return internal::FloatingEq2Matcher<double>();
4629inline internal::FloatingEq2Matcher<float> NanSensitiveFloatEq() {
4630 return internal::FloatingEq2Matcher<float>(
true);
4635inline internal::FloatingEq2Matcher<double> NanSensitiveDoubleEq() {
4636 return internal::FloatingEq2Matcher<double>(
true);
4641inline internal::FloatingEq2Matcher<float> FloatNear(
float max_abs_error) {
4642 return internal::FloatingEq2Matcher<float>(max_abs_error);
4647inline internal::FloatingEq2Matcher<double> DoubleNear(
double max_abs_error) {
4648 return internal::FloatingEq2Matcher<double>(max_abs_error);
4654inline internal::FloatingEq2Matcher<float> NanSensitiveFloatNear(
4655 float max_abs_error) {
4656 return internal::FloatingEq2Matcher<float>(max_abs_error,
true);
4662inline internal::FloatingEq2Matcher<double> NanSensitiveDoubleNear(
4663 double max_abs_error) {
4664 return internal::FloatingEq2Matcher<double>(max_abs_error,
true);
4669template <
typename InnerMatcher>
4670inline internal::NotMatcher<InnerMatcher> Not(InnerMatcher m) {
4671 return internal::NotMatcher<InnerMatcher>(m);
4677template <
typename Predicate>
4678inline PolymorphicMatcher<internal::TrulyMatcher<Predicate>> Truly(
4680 return MakePolymorphicMatcher(internal::TrulyMatcher<Predicate>(pred));
4689template <
typename SizeMatcher>
4690inline internal::SizeIsMatcher<SizeMatcher> SizeIs(
4691 const SizeMatcher& size_matcher) {
4692 return internal::SizeIsMatcher<SizeMatcher>(size_matcher);
4700template <
typename DistanceMatcher>
4701inline internal::BeginEndDistanceIsMatcher<DistanceMatcher> BeginEndDistanceIs(
4702 const DistanceMatcher& distance_matcher) {
4703 return internal::BeginEndDistanceIsMatcher<DistanceMatcher>(distance_matcher);
4710template <
typename Container>
4711inline PolymorphicMatcher<
4712 internal::ContainerEqMatcher<typename std::remove_const<Container>::type>>
4713ContainerEq(
const Container& rhs) {
4714 return MakePolymorphicMatcher(internal::ContainerEqMatcher<Container>(rhs));
4719template <
typename Comparator,
typename ContainerMatcher>
4720inline internal::WhenSortedByMatcher<Comparator, ContainerMatcher> WhenSortedBy(
4721 const Comparator& comparator,
const ContainerMatcher& container_matcher) {
4722 return internal::WhenSortedByMatcher<Comparator, ContainerMatcher>(
4723 comparator, container_matcher);
4728template <
typename ContainerMatcher>
4729inline internal::WhenSortedByMatcher<internal::LessComparator, ContainerMatcher>
4730WhenSorted(
const ContainerMatcher& container_matcher) {
4731 return internal::WhenSortedByMatcher<internal::LessComparator,
4733 internal::LessComparator(), container_matcher);
4742template <
typename TupleMatcher,
typename Container>
4743inline internal::PointwiseMatcher<TupleMatcher,
4744 typename std::remove_const<Container>::type>
4745Pointwise(
const TupleMatcher& tuple_matcher,
const Container& rhs) {
4746 return internal::PointwiseMatcher<TupleMatcher, Container>(tuple_matcher,
4751template <
typename TupleMatcher,
typename T>
4752inline internal::PointwiseMatcher<TupleMatcher, std::vector<T>> Pointwise(
4753 const TupleMatcher& tuple_matcher, std::initializer_list<T> rhs) {
4754 return Pointwise(tuple_matcher, std::vector<T>(rhs));
4768template <
typename Tuple2Matcher,
typename RhsContainer>
4769inline internal::UnorderedElementsAreArrayMatcher<
4770 typename internal::BoundSecondMatcher<
4772 typename internal::StlContainerView<
4773 typename std::remove_const<RhsContainer>::type>::type::value_type>>
4774UnorderedPointwise(
const Tuple2Matcher& tuple2_matcher,
4775 const RhsContainer& rhs_container) {
4778 typedef typename internal::StlContainerView<RhsContainer> RhsView;
4779 typedef typename RhsView::type RhsStlContainer;
4780 typedef typename RhsStlContainer::value_type Second;
4781 const RhsStlContainer& rhs_stl_container =
4782 RhsView::ConstReference(rhs_container);
4785 ::std::vector<internal::BoundSecondMatcher<Tuple2Matcher, Second>> matchers;
4786 for (
auto it = rhs_stl_container.begin(); it != rhs_stl_container.end();
4788 matchers.push_back(internal::MatcherBindSecond(tuple2_matcher, *it));
4792 return UnorderedElementsAreArray(matchers);
4796template <
typename Tuple2Matcher,
typename T>
4797inline internal::UnorderedElementsAreArrayMatcher<
4798 typename internal::BoundSecondMatcher<Tuple2Matcher, T>>
4799UnorderedPointwise(
const Tuple2Matcher& tuple2_matcher,
4800 std::initializer_list<T> rhs) {
4801 return UnorderedPointwise(tuple2_matcher, std::vector<T>(rhs));
4835template <
typename M>
4836inline internal::ContainsMatcher<M> Contains(M matcher) {
4837 return internal::ContainsMatcher<M>(matcher);
4867template <
typename Iter>
4868inline internal::UnorderedElementsAreArrayMatcher<
4869 typename ::std::iterator_traits<Iter>::value_type>
4870IsSupersetOf(Iter first, Iter last) {
4871 typedef typename ::std::iterator_traits<Iter>::value_type T;
4872 return internal::UnorderedElementsAreArrayMatcher<T>(
4873 internal::UnorderedMatcherRequire::Superset, first, last);
4876template <
typename T>
4877inline internal::UnorderedElementsAreArrayMatcher<T> IsSupersetOf(
4878 const T* pointer,
size_t count) {
4879 return IsSupersetOf(pointer, pointer + count);
4882template <
typename T,
size_t N>
4883inline internal::UnorderedElementsAreArrayMatcher<T> IsSupersetOf(
4884 const T (&array)[N]) {
4885 return IsSupersetOf(array, N);
4888template <
typename Container>
4889inline internal::UnorderedElementsAreArrayMatcher<
4890 typename Container::value_type>
4891IsSupersetOf(
const Container& container) {
4892 return IsSupersetOf(container.begin(), container.end());
4895template <
typename T>
4896inline internal::UnorderedElementsAreArrayMatcher<T> IsSupersetOf(
4897 ::std::initializer_list<T> xs) {
4898 return IsSupersetOf(xs.begin(), xs.end());
4924template <
typename Iter>
4925inline internal::UnorderedElementsAreArrayMatcher<
4926 typename ::std::iterator_traits<Iter>::value_type>
4927IsSubsetOf(Iter first, Iter last) {
4928 typedef typename ::std::iterator_traits<Iter>::value_type T;
4929 return internal::UnorderedElementsAreArrayMatcher<T>(
4930 internal::UnorderedMatcherRequire::Subset, first, last);
4933template <
typename T>
4934inline internal::UnorderedElementsAreArrayMatcher<T> IsSubsetOf(
4935 const T* pointer,
size_t count) {
4936 return IsSubsetOf(pointer, pointer + count);
4939template <
typename T,
size_t N>
4940inline internal::UnorderedElementsAreArrayMatcher<T> IsSubsetOf(
4941 const T (&array)[N]) {
4942 return IsSubsetOf(array, N);
4945template <
typename Container>
4946inline internal::UnorderedElementsAreArrayMatcher<
4947 typename Container::value_type>
4948IsSubsetOf(
const Container& container) {
4949 return IsSubsetOf(container.begin(), container.end());
4952template <
typename T>
4953inline internal::UnorderedElementsAreArrayMatcher<T> IsSubsetOf(
4954 ::std::initializer_list<T> xs) {
4955 return IsSubsetOf(xs.begin(), xs.end());
4985template <
typename M>
4986inline internal::EachMatcher<M> Each(M matcher) {
4987 return internal::EachMatcher<M>(matcher);
4993template <
typename M>
4994inline internal::KeyMatcher<M> Key(M inner_matcher) {
4995 return internal::KeyMatcher<M>(inner_matcher);
5003template <
typename FirstMatcher,
typename SecondMatcher>
5004inline internal::PairMatcher<FirstMatcher, SecondMatcher> Pair(
5005 FirstMatcher first_matcher, SecondMatcher second_matcher) {
5006 return internal::PairMatcher<FirstMatcher, SecondMatcher>(first_matcher,
5016template <
typename MatcherTrue,
typename MatcherFalse>
5017internal::ConditionalMatcher<MatcherTrue, MatcherFalse> Conditional(
5018 bool condition, MatcherTrue matcher_true, MatcherFalse matcher_false) {
5019 return internal::ConditionalMatcher<MatcherTrue, MatcherFalse>(
5020 condition, std::move(matcher_true), std::move(matcher_false));
5027template <
typename... M>
5028internal::FieldsAreMatcher<typename std::decay<M>::type...> FieldsAre(
5030 return internal::FieldsAreMatcher<typename std::decay<M>::type...>(
5031 std::forward<M>(matchers)...);
5036template <
typename InnerMatcher>
5037inline internal::PointerMatcher<InnerMatcher> Pointer(
5038 const InnerMatcher& inner_matcher) {
5039 return internal::PointerMatcher<InnerMatcher>(inner_matcher);
5044template <
typename InnerMatcher>
5045inline internal::AddressMatcher<InnerMatcher> Address(
5046 const InnerMatcher& inner_matcher) {
5047 return internal::AddressMatcher<InnerMatcher>(inner_matcher);
5052template <
typename MatcherType>
5053internal::WhenBase64UnescapedMatcher WhenBase64Unescaped(
5054 const MatcherType& internal_matcher) {
5055 return internal::WhenBase64UnescapedMatcher(internal_matcher);
5061template <
typename M>
5062inline internal::MatcherAsPredicate<M> Matches(M matcher) {
5063 return internal::MatcherAsPredicate<M>(matcher);
5067template <
typename T,
typename M>
5068inline bool Value(
const T& value, M matcher) {
5069 return testing::Matches(matcher)(value);
5074template <
typename T,
typename M>
5075inline bool ExplainMatchResult(M matcher,
const T& value,
5076 MatchResultListener* listener) {
5077 return SafeMatcherCast<const T&>(matcher).MatchAndExplain(value, listener);
5091template <
typename T,
typename M>
5092std::string DescribeMatcher(
const M& matcher,
bool negation =
false) {
5093 ::std::stringstream ss;
5094 Matcher<T> monomorphic_matcher = SafeMatcherCast<T>(matcher);
5096 monomorphic_matcher.DescribeNegationTo(&ss);
5098 monomorphic_matcher.DescribeTo(&ss);
5103template <
typename... Args>
5104internal::ElementsAreMatcher<
5105 std::tuple<typename std::decay<const Args&>::type...>>
5106ElementsAre(
const Args&... matchers) {
5107 return internal::ElementsAreMatcher<
5108 std::tuple<typename std::decay<const Args&>::type...>>(
5109 std::make_tuple(matchers...));
5112template <
typename... Args>
5113internal::UnorderedElementsAreMatcher<
5114 std::tuple<typename std::decay<const Args&>::type...>>
5115UnorderedElementsAre(
const Args&... matchers) {
5116 return internal::UnorderedElementsAreMatcher<
5117 std::tuple<typename std::decay<const Args&>::type...>>(
5118 std::make_tuple(matchers...));
5122template <
typename... Args>
5123internal::AllOfMatcher<typename std::decay<const Args&>::type...> AllOf(
5124 const Args&... matchers) {
5125 return internal::AllOfMatcher<typename std::decay<const Args&>::type...>(
5129template <
typename... Args>
5130internal::AnyOfMatcher<typename std::decay<const Args&>::type...> AnyOf(
5131 const Args&... matchers) {
5132 return internal::AnyOfMatcher<typename std::decay<const Args&>::type...>(
5158template <
typename Iter>
5159inline internal::AnyOfArrayMatcher<
5160 typename ::std::iterator_traits<Iter>::value_type>
5161AnyOfArray(Iter first, Iter last) {
5162 return internal::AnyOfArrayMatcher<
5163 typename ::std::iterator_traits<Iter>::value_type>(first, last);
5166template <
typename Iter>
5167inline internal::AllOfArrayMatcher<
5168 typename ::std::iterator_traits<Iter>::value_type>
5169AllOfArray(Iter first, Iter last) {
5170 return internal::AllOfArrayMatcher<
5171 typename ::std::iterator_traits<Iter>::value_type>(first, last);
5174template <
typename T>
5175inline internal::AnyOfArrayMatcher<T> AnyOfArray(
const T* ptr,
size_t count) {
5176 return AnyOfArray(ptr, ptr + count);
5179template <
typename T>
5180inline internal::AllOfArrayMatcher<T> AllOfArray(
const T* ptr,
size_t count) {
5181 return AllOfArray(ptr, ptr + count);
5184template <
typename T,
size_t N>
5185inline internal::AnyOfArrayMatcher<T> AnyOfArray(
const T (&array)[N]) {
5186 return AnyOfArray(array, N);
5189template <
typename T,
size_t N>
5190inline internal::AllOfArrayMatcher<T> AllOfArray(
const T (&array)[N]) {
5191 return AllOfArray(array, N);
5194template <
typename Container>
5195inline internal::AnyOfArrayMatcher<typename Container::value_type> AnyOfArray(
5196 const Container& container) {
5197 return AnyOfArray(container.begin(), container.end());
5200template <
typename Container>
5201inline internal::AllOfArrayMatcher<typename Container::value_type> AllOfArray(
5202 const Container& container) {
5203 return AllOfArray(container.begin(), container.end());
5206template <
typename T>
5207inline internal::AnyOfArrayMatcher<T> AnyOfArray(
5208 ::std::initializer_list<T> xs) {
5209 return AnyOfArray(xs.begin(), xs.end());
5212template <
typename T>
5213inline internal::AllOfArrayMatcher<T> AllOfArray(
5214 ::std::initializer_list<T> xs) {
5215 return AllOfArray(xs.begin(), xs.end());
5221template <
size_t... k,
typename InnerMatcher>
5222internal::ArgsMatcher<typename std::decay<InnerMatcher>::type, k...> Args(
5223 InnerMatcher&& matcher) {
5224 return internal::ArgsMatcher<typename std::decay<InnerMatcher>::type, k...>(
5225 std::forward<InnerMatcher>(matcher));
5235template <
typename InnerMatcher>
5236inline InnerMatcher AllArgs(
const InnerMatcher& matcher) {
5248template <
typename ValueMatcher>
5249inline internal::OptionalMatcher<ValueMatcher> Optional(
5250 const ValueMatcher& value_matcher) {
5251 return internal::OptionalMatcher<ValueMatcher>(value_matcher);
5255template <
typename T>
5256PolymorphicMatcher<internal::any_cast_matcher::AnyCastMatcher<T>> AnyWith(
5257 const Matcher<const T&>& matcher) {
5258 return MakePolymorphicMatcher(
5259 internal::any_cast_matcher::AnyCastMatcher<T>(matcher));
5266template <
typename T>
5267PolymorphicMatcher<internal::variant_matcher::VariantMatcher<T>> VariantWith(
5268 const Matcher<const T&>& matcher) {
5269 return MakePolymorphicMatcher(
5270 internal::variant_matcher::VariantMatcher<T>(matcher));
5273#if GTEST_HAS_EXCEPTIONS
5279class WithWhatMatcherImpl {
5281 WithWhatMatcherImpl(Matcher<std::string> matcher)
5282 : matcher_(std::move(matcher)) {}
5284 void DescribeTo(std::ostream* os)
const {
5285 *os <<
"contains .what() that ";
5286 matcher_.DescribeTo(os);
5289 void DescribeNegationTo(std::ostream* os)
const {
5290 *os <<
"contains .what() that does not ";
5291 matcher_.DescribeTo(os);
5294 template <
typename Err>
5295 bool MatchAndExplain(
const Err& err, MatchResultListener* listener)
const {
5296 *listener <<
"which contains .what() (of value = " << err.what()
5298 return matcher_.MatchAndExplain(err.what(), listener);
5302 const Matcher<std::string> matcher_;
5305inline PolymorphicMatcher<WithWhatMatcherImpl> WithWhat(
5306 Matcher<std::string> m) {
5307 return MakePolymorphicMatcher(WithWhatMatcherImpl(std::move(m)));
5310template <
typename Err>
5311class ExceptionMatcherImpl {
5314 const char* what() const noexcept {
5315 return "this exception should never be thrown";
5340 using DefaultExceptionType =
typename std::conditional<
5341 std::is_same<
typename std::remove_cv<
5342 typename std::remove_reference<Err>::type>::type,
5343 std::exception>::value,
5344 const NeverThrown&,
const std::exception&>::type;
5347 ExceptionMatcherImpl(Matcher<const Err&> matcher)
5348 : matcher_(std::move(matcher)) {}
5350 void DescribeTo(std::ostream* os)
const {
5351 *os <<
"throws an exception which is a " << GetTypeName<Err>();
5353 matcher_.DescribeTo(os);
5356 void DescribeNegationTo(std::ostream* os)
const {
5357 *os <<
"throws an exception which is not a " << GetTypeName<Err>();
5359 matcher_.DescribeNegationTo(os);
5362 template <
typename T>
5363 bool MatchAndExplain(T&& x, MatchResultListener* listener)
const {
5365 (void)(std::forward<T>(x)());
5366 }
catch (
const Err& err) {
5367 *listener <<
"throws an exception which is a " << GetTypeName<Err>();
5369 return matcher_.MatchAndExplain(err, listener);
5370 }
catch (DefaultExceptionType err) {
5372 *listener <<
"throws an exception of type " << GetTypeName(
typeid(err));
5375 *listener <<
"throws an std::exception-derived type ";
5377 *listener <<
"with description \"" << err.what() <<
"\"";
5380 *listener <<
"throws an exception of an unknown type";
5384 *listener <<
"does not throw any exception";
5389 const Matcher<const Err&> matcher_;
5416template <
typename Err>
5417PolymorphicMatcher<internal::ExceptionMatcherImpl<Err>> Throws() {
5418 return MakePolymorphicMatcher(
5419 internal::ExceptionMatcherImpl<Err>(A<const Err&>()));
5422template <
typename Err,
typename ExceptionMatcher>
5423PolymorphicMatcher<internal::ExceptionMatcherImpl<Err>> Throws(
5424 const ExceptionMatcher& exception_matcher) {
5428 return MakePolymorphicMatcher(internal::ExceptionMatcherImpl<Err>(
5429 SafeMatcherCast<const Err&>(exception_matcher)));
5432template <
typename Err,
typename MessageMatcher>
5433PolymorphicMatcher<internal::ExceptionMatcherImpl<Err>> ThrowsMessage(
5434 MessageMatcher&& message_matcher) {
5435 static_assert(std::is_base_of<std::exception, Err>::value,
5436 "expected an std::exception-derived type");
5437 return Throws<Err>(internal::WithWhat(
5438 MatcherCast<std::string>(std::forward<MessageMatcher>(message_matcher))));
5447#define ASSERT_THAT(value, matcher) \
5448 ASSERT_PRED_FORMAT1( \
5449 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
5450#define EXPECT_THAT(value, matcher) \
5451 EXPECT_PRED_FORMAT1( \
5452 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
5455#define MATCHER(name, description) \
5456 class name##Matcher \
5457 : public ::testing::internal::MatcherBaseImpl<name##Matcher> { \
5459 template <typename arg_type> \
5460 class gmock_Impl : public ::testing::MatcherInterface<const arg_type&> { \
5463 bool MatchAndExplain( \
5464 const arg_type& arg, \
5465 ::testing::MatchResultListener* result_listener) const override; \
5466 void DescribeTo(::std::ostream* gmock_os) const override { \
5467 *gmock_os << FormatDescription(false); \
5469 void DescribeNegationTo(::std::ostream* gmock_os) const override { \
5470 *gmock_os << FormatDescription(true); \
5474 ::std::string FormatDescription(bool negation) const { \
5476 ::std::string gmock_description = (description); \
5477 if (!gmock_description.empty()) { \
5478 return gmock_description; \
5480 return ::testing::internal::FormatMatcherDescription(negation, #name, \
5485 GTEST_ATTRIBUTE_UNUSED_ inline name##Matcher name() { return {}; } \
5486 template <typename arg_type> \
5487 bool name##Matcher::gmock_Impl<arg_type>::MatchAndExplain( \
5488 const arg_type& arg, \
5489 ::testing::MatchResultListener* result_listener GTEST_ATTRIBUTE_UNUSED_) \
5492#define MATCHER_P(name, p0, description) \
5493 GMOCK_INTERNAL_MATCHER(name, name##MatcherP, description, (#p0), (p0))
5494#define MATCHER_P2(name, p0, p1, description) \
5495 GMOCK_INTERNAL_MATCHER(name, name##MatcherP2, description, (#p0, #p1), \
5497#define MATCHER_P3(name, p0, p1, p2, description) \
5498 GMOCK_INTERNAL_MATCHER(name, name##MatcherP3, description, (#p0, #p1, #p2), \
5500#define MATCHER_P4(name, p0, p1, p2, p3, description) \
5501 GMOCK_INTERNAL_MATCHER(name, name##MatcherP4, description, \
5502 (#p0, #p1, #p2, #p3), (p0, p1, p2, p3))
5503#define MATCHER_P5(name, p0, p1, p2, p3, p4, description) \
5504 GMOCK_INTERNAL_MATCHER(name, name##MatcherP5, description, \
5505 (#p0, #p1, #p2, #p3, #p4), (p0, p1, p2, p3, p4))
5506#define MATCHER_P6(name, p0, p1, p2, p3, p4, p5, description) \
5507 GMOCK_INTERNAL_MATCHER(name, name##MatcherP6, description, \
5508 (#p0, #p1, #p2, #p3, #p4, #p5), \
5509 (p0, p1, p2, p3, p4, p5))
5510#define MATCHER_P7(name, p0, p1, p2, p3, p4, p5, p6, description) \
5511 GMOCK_INTERNAL_MATCHER(name, name##MatcherP7, description, \
5512 (#p0, #p1, #p2, #p3, #p4, #p5, #p6), \
5513 (p0, p1, p2, p3, p4, p5, p6))
5514#define MATCHER_P8(name, p0, p1, p2, p3, p4, p5, p6, p7, description) \
5515 GMOCK_INTERNAL_MATCHER(name, name##MatcherP8, description, \
5516 (#p0, #p1, #p2, #p3, #p4, #p5, #p6, #p7), \
5517 (p0, p1, p2, p3, p4, p5, p6, p7))
5518#define MATCHER_P9(name, p0, p1, p2, p3, p4, p5, p6, p7, p8, description) \
5519 GMOCK_INTERNAL_MATCHER(name, name##MatcherP9, description, \
5520 (#p0, #p1, #p2, #p3, #p4, #p5, #p6, #p7, #p8), \
5521 (p0, p1, p2, p3, p4, p5, p6, p7, p8))
5522#define MATCHER_P10(name, p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, description) \
5523 GMOCK_INTERNAL_MATCHER(name, name##MatcherP10, description, \
5524 (#p0, #p1, #p2, #p3, #p4, #p5, #p6, #p7, #p8, #p9), \
5525 (p0, p1, p2, p3, p4, p5, p6, p7, p8, p9))
5527#define GMOCK_INTERNAL_MATCHER(name, full_name, description, arg_names, args) \
5528 template <GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args)> \
5529 class full_name : public ::testing::internal::MatcherBaseImpl< \
5530 full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>> { \
5532 using full_name::MatcherBaseImpl::MatcherBaseImpl; \
5533 template <typename arg_type> \
5534 class gmock_Impl : public ::testing::MatcherInterface<const arg_type&> { \
5536 explicit gmock_Impl(GMOCK_INTERNAL_MATCHER_FUNCTION_ARGS(args)) \
5537 : GMOCK_INTERNAL_MATCHER_FORWARD_ARGS(args) {} \
5538 bool MatchAndExplain( \
5539 const arg_type& arg, \
5540 ::testing::MatchResultListener* result_listener) const override; \
5541 void DescribeTo(::std::ostream* gmock_os) const override { \
5542 *gmock_os << FormatDescription(false); \
5544 void DescribeNegationTo(::std::ostream* gmock_os) const override { \
5545 *gmock_os << FormatDescription(true); \
5547 GMOCK_INTERNAL_MATCHER_MEMBERS(args) \
5550 ::std::string FormatDescription(bool negation) const { \
5551 ::std::string gmock_description = (description); \
5552 if (!gmock_description.empty()) { \
5553 return gmock_description; \
5555 return ::testing::internal::FormatMatcherDescription( \
5556 negation, #name, {GMOCK_PP_REMOVE_PARENS(arg_names)}, \
5557 ::testing::internal::UniversalTersePrintTupleFieldsToStrings( \
5558 ::std::tuple<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>( \
5559 GMOCK_INTERNAL_MATCHER_MEMBERS_USAGE(args)))); \
5563 template <GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args)> \
5564 inline full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)> name( \
5565 GMOCK_INTERNAL_MATCHER_FUNCTION_ARGS(args)) { \
5566 return full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>( \
5567 GMOCK_INTERNAL_MATCHER_ARGS_USAGE(args)); \
5569 template <GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args)> \
5570 template <typename arg_type> \
5571 bool full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>::gmock_Impl< \
5572 arg_type>::MatchAndExplain(const arg_type& arg, \
5573 ::testing::MatchResultListener* \
5574 result_listener GTEST_ATTRIBUTE_UNUSED_) \
5577#define GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args) \
5579 GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAM, , args))
5580#define GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAM(i_unused, data_unused, arg) \
5581 , typename arg##_type
5583#define GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args) \
5584 GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_TYPE_PARAM, , args))
5585#define GMOCK_INTERNAL_MATCHER_TYPE_PARAM(i_unused, data_unused, arg) \
5588#define GMOCK_INTERNAL_MATCHER_FUNCTION_ARGS(args) \
5589 GMOCK_PP_TAIL(dummy_first GMOCK_PP_FOR_EACH( \
5590 GMOCK_INTERNAL_MATCHER_FUNCTION_ARG, , args))
5591#define GMOCK_INTERNAL_MATCHER_FUNCTION_ARG(i, data_unused, arg) \
5592 , arg##_type gmock_p##i
5594#define GMOCK_INTERNAL_MATCHER_FORWARD_ARGS(args) \
5595 GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_FORWARD_ARG, , args))
5596#define GMOCK_INTERNAL_MATCHER_FORWARD_ARG(i, data_unused, arg) \
5597 , arg(::std::forward<arg##_type>(gmock_p##i))
5599#define GMOCK_INTERNAL_MATCHER_MEMBERS(args) \
5600 GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_MEMBER, , args)
5601#define GMOCK_INTERNAL_MATCHER_MEMBER(i_unused, data_unused, arg) \
5602 const arg##_type arg;
5604#define GMOCK_INTERNAL_MATCHER_MEMBERS_USAGE(args) \
5605 GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_MEMBER_USAGE, , args))
5606#define GMOCK_INTERNAL_MATCHER_MEMBER_USAGE(i_unused, data_unused, arg) , arg
5608#define GMOCK_INTERNAL_MATCHER_ARGS_USAGE(args) \
5609 GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_ARG_USAGE, , args))
5610#define GMOCK_INTERNAL_MATCHER_ARG_USAGE(i, data_unused, arg_unused) \
5614using namespace no_adl;
5618GTEST_DISABLE_MSC_WARNINGS_POP_()
5623#include "gmock/internal/custom/gmock-matchers.h"
Definition gmock-internal-utils.h:55