42 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_
43 #define GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_
47 #include <initializer_list>
54 #include <type_traits>
57 #include "gmock/internal/gmock-internal-utils.h"
58 #include "gmock/internal/gmock-port.h"
59 #include "gtest/gtest.h"
62 #if defined(_MSC_VER) && _MSC_VER >= 1915
63 #define GMOCK_MAYBE_5046_ 5046
65 #define GMOCK_MAYBE_5046_
88 class StringMatchResultListener :
public MatchResultListener {
99 ::std::stringstream
ss_;
118 template <
typename T,
typename M>
119 class MatcherCastImpl {
121 static Matcher<T>
Cast(
const M& polymorphic_matcher_or_value) {
135 return CastImpl(polymorphic_matcher_or_value,
136 std::is_convertible<M, Matcher<T>>{},
137 std::is_convertible<M, T>{});
141 template <
bool Ignore>
142 static Matcher<T>
CastImpl(
const M& polymorphic_matcher_or_value,
144 bool_constant<Ignore>) {
153 return polymorphic_matcher_or_value;
162 return Matcher<T>(ImplicitCast_<T>(
value));
183 template <
typename T,
typename U>
184 class MatcherCastImpl<
T, Matcher<U> > {
186 static Matcher<T>
Cast(
const Matcher<U>& source_matcher) {
187 return Matcher<T>(
new Impl(source_matcher));
191 class Impl :
public MatcherInterface<T> {
193 explicit Impl(
const Matcher<U>& source_matcher)
194 : source_matcher_(source_matcher) {}
197 bool MatchAndExplain(
T x, MatchResultListener* listener)
const override {
198 using FromType =
typename std::remove_cv<
typename std::remove_pointer<
200 using ToType =
typename std::remove_cv<
typename std::remove_pointer<
210 "Can't implicitly convert from <base> to <derived>");
212 return source_matcher_.MatchAndExplain(
static_cast<U
>(
x), listener);
215 void DescribeTo(::std::ostream* os)
const override {
216 source_matcher_.DescribeTo(os);
219 void DescribeNegationTo(::std::ostream* os)
const override {
220 source_matcher_.DescribeNegationTo(os);
224 const Matcher<U> source_matcher_;
232 template <
typename T>
233 class MatcherCastImpl<
T, Matcher<
T> > {
235 static Matcher<T>
Cast(
const Matcher<T>& matcher) {
return matcher; }
244 template <
typename T,
typename M>
253 template <
typename T>
254 class SafeMatcherCastImpl {
258 template <
typename M>
259 static inline Matcher<T>
Cast(
const M& polymorphic_matcher_or_value) {
272 template <
typename U>
273 static inline Matcher<T>
Cast(
const Matcher<U>& matcher) {
276 "T must be implicitly convertible to U");
281 cannot_convert_non_reference_arg_to_reference);
289 kTIsOther || kUIsOther ||
291 conversion_of_arithmetic_types_must_be_lossless);
292 return MatcherCast<T>(matcher);
296 template <
typename T,
typename M>
302 template <
typename T>
311 ::std::ostream* os) {
312 if (explanation !=
"" && os !=
nullptr) {
313 *os <<
", " << explanation;
324 type_name.find_first_of(
"<(") == std::string::npos);
332 template <
typename Value,
typename T>
334 MatchResultListener* listener) {
335 if (!listener->IsInterested()) {
338 return matcher.Matches(
value);
341 StringMatchResultListener inner_listener;
342 const bool match = matcher.MatchAndExplain(
value, &inner_listener);
348 *listener->stream() <<
" (of type " <<
type_name <<
")";
363 template <
typename MatcherTuple,
typename ValueTuple>
364 static bool Matches(
const MatcherTuple& matcher_tuple,
365 const ValueTuple& value_tuple) {
374 template <
typename MatcherTuple,
typename ValueTuple>
377 ::std::ostream* os) {
383 typename std::tuple_element<
N - 1, MatcherTuple>
::type matcher =
385 typedef typename std::tuple_element<
N - 1, ValueTuple>
::type Value;
387 StringMatchResultListener listener;
388 if (!matcher.MatchAndExplain(
value, &listener)) {
389 *os <<
" Expected arg #" <<
N - 1 <<
": ";
391 *os <<
"\n Actual: ";
406 class TuplePrefix<0> {
408 template <
typename MatcherTuple,
typename ValueTuple>
409 static bool Matches(
const MatcherTuple& ,
410 const ValueTuple& ) {
414 template <
typename MatcherTuple,
typename ValueTuple>
425 template <
typename MatcherTuple,
typename ValueTuple>
427 const ValueTuple& value_tuple) {
432 matcher_and_value_have_different_numbers_of_fields);
439 template <
typename MatcherTuple,
typename ValueTuple>
442 ::std::ostream* os) {
451 template <
typename Tuple,
typename Func,
typename OutIter>
452 class TransformTupleValuesHelper {
454 typedef ::std::tuple_size<Tuple>
TupleSize;
459 static OutIter
Run(
Func f,
const Tuple& t, OutIter
out) {
460 return IterateOverTuple<Tuple, TupleSize::value>()(f, t,
out);
464 template <
typename Tup,
size_t kRemainingSize>
465 struct IterateOverTuple {
467 *
out++ = f(::std::get<TupleSize::value - kRemainingSize>(t));
468 return IterateOverTuple<Tup, kRemainingSize - 1>()(f, t,
out);
471 template <
typename Tup>
472 struct IterateOverTuple<Tup, 0> {
482 template <
typename Tuple,
typename Func,
typename OutIter>
488 template <
typename T>
489 class AnyMatcherImpl :
public MatcherInterface<const T&> {
492 MatchResultListener* )
const override {
495 void DescribeTo(::std::ostream* os)
const override { *os <<
"is anything"; }
500 *os <<
"never matches";
508 class AnythingMatcher {
510 template <
typename T>
511 operator Matcher<T>()
const {
return A<T>(); }
516 class IsNullMatcher {
518 template <
typename Po
inter>
520 MatchResultListener* )
const {
524 void DescribeTo(::std::ostream* os)
const { *os <<
"is NULL"; }
532 class NotNullMatcher {
534 template <
typename Po
inter>
536 MatchResultListener* )
const {
540 void DescribeTo(::std::ostream* os)
const { *os <<
"isn't NULL"; }
559 template <
typename T>
562 template <
typename T>
563 class RefMatcher<
T&> {
573 explicit RefMatcher(
T&
x) : object_(
x) {}
575 template <
typename Super>
576 operator Matcher<Super&>()
const {
586 template <
typename Super>
587 class Impl :
public MatcherInterface<Super&> {
589 explicit Impl(Super&
x) : object_(
x) {}
593 bool MatchAndExplain(Super&
x,
594 MatchResultListener* listener)
const override {
595 *listener <<
"which is located @" <<
static_cast<const void*
>(&
x);
596 return &
x == &object_;
599 void DescribeTo(::std::ostream* os)
const override {
600 *os <<
"references the variable ";
604 void DescribeNegationTo(::std::ostream* os)
const override {
605 *os <<
"does not reference the variable ";
610 const Super& object_;
626 const wchar_t* rhs) {
632 template <
typename StringType>
642 const size_t i1 = s1.find(nul),
i2 = s2.find(nul);
645 if (
i1 == StringType::npos ||
i2 == StringType::npos) {
656 template <
typename StringType>
657 class StrEqualityMatcher {
665 MatchResultListener* listener)
const {
671 #endif // GTEST_HAS_ABSL
678 template <
typename CharType>
679 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
690 template <
typename MatcheeStringType>
692 MatchResultListener* )
const {
709 *os << (expect_eq ?
"is " :
"isn't ");
712 *os <<
"(ignoring case) ";
727 template <
typename StringType>
728 class HasSubstrMatcher {
735 MatchResultListener* listener)
const {
741 #endif // GTEST_HAS_ABSL
748 template <
typename CharType>
749 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
757 template <
typename MatcheeStringType>
759 MatchResultListener* )
const {
761 return s2.find(
substring_) != StringType::npos;
766 *os <<
"has substring ";
771 *os <<
"has no substring ";
784 template <
typename StringType>
785 class StartsWithMatcher {
792 MatchResultListener* listener)
const {
798 #endif // GTEST_HAS_ABSL
805 template <
typename CharType>
806 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
814 template <
typename MatcheeStringType>
816 MatchResultListener* )
const {
818 return s2.length() >=
prefix_.length() &&
823 *os <<
"starts with ";
828 *os <<
"doesn't start with ";
841 template <
typename StringType>
842 class EndsWithMatcher {
848 MatchResultListener* listener)
const {
854 #endif // GTEST_HAS_ABSL
861 template <
typename CharType>
862 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
870 template <
typename MatcheeStringType>
872 MatchResultListener* )
const {
874 return s2.length() >=
suffix_.length() &&
884 *os <<
"doesn't end with ";
902 template <
typename D,
typename Op>
903 class PairMatchBase {
905 template <
typename T1,
typename T2>
906 operator Matcher<::std::tuple<T1, T2>>()
const {
907 return Matcher<::std::tuple<T1, T2>>(
new Impl<const ::std::tuple<T1, T2>&>);
909 template <
typename T1,
typename T2>
910 operator Matcher<const ::std::tuple<T1, T2>&>()
const {
911 return MakeMatcher(
new Impl<const ::std::tuple<T1, T2>&>);
915 static ::std::ostream&
GetDesc(::std::ostream& os) {
916 return os << D::Desc();
919 template <
typename Tuple>
920 class Impl :
public MatcherInterface<Tuple> {
923 MatchResultListener* )
const override {
924 return Op()(::std::get<0>(
args), ::std::get<1>(
args));
926 void DescribeTo(::std::ostream* os)
const override {
935 class Eq2Matcher :
public PairMatchBase<Eq2Matcher, AnyEq> {
937 static const char*
Desc() {
return "an equal pair"; }
939 class Ne2Matcher :
public PairMatchBase<Ne2Matcher, AnyNe> {
941 static const char*
Desc() {
return "an unequal pair"; }
943 class Lt2Matcher :
public PairMatchBase<Lt2Matcher, AnyLt> {
945 static const char*
Desc() {
return "a pair where the first < the second"; }
947 class Gt2Matcher :
public PairMatchBase<Gt2Matcher, AnyGt> {
949 static const char*
Desc() {
return "a pair where the first > the second"; }
951 class Le2Matcher :
public PairMatchBase<Le2Matcher, AnyLe> {
953 static const char*
Desc() {
return "a pair where the first <= the second"; }
955 class Ge2Matcher :
public PairMatchBase<Ge2Matcher, AnyGe> {
957 static const char*
Desc() {
return "a pair where the first >= the second"; }
964 template <
typename T>
965 class NotMatcherImpl :
public MatcherInterface<const T&> {
971 MatchResultListener* listener)
const override {
972 return !
matcher_.MatchAndExplain(
x, listener);
975 void DescribeTo(::std::ostream* os)
const override {
991 template <
typename InnerMatcher>
998 template <
typename T>
999 operator Matcher<T>()
const {
1000 return Matcher<T>(
new NotMatcherImpl<T>(SafeMatcherCast<T>(
matcher_)));
1013 template <
typename T>
1014 class AllOfMatcherImpl :
public MatcherInterface<const T&> {
1016 explicit AllOfMatcherImpl(std::vector<Matcher<T> >
matchers)
1019 void DescribeTo(::std::ostream* os)
const override {
1022 if (
i != 0) *os <<
") and (";
1028 void DescribeNegationTo(::std::ostream* os)
const override {
1031 if (
i != 0) *os <<
") or (";
1037 bool MatchAndExplain(
const T&
x,
1038 MatchResultListener* listener)
const override {
1044 StringMatchResultListener slistener;
1045 if (
matchers_[
i].MatchAndExplain(
x, &slistener)) {
1046 if (all_match_result.empty()) {
1047 all_match_result = slistener.str();
1051 all_match_result +=
", and ";
1052 all_match_result +=
result;
1056 *listener << slistener.str();
1062 *listener << all_match_result;
1067 const std::vector<Matcher<T> >
matchers_;
1076 template <
template <
typename T>
class CombiningMatcher,
typename...
Args>
1077 class VariadicMatcher {
1081 static_assert(
sizeof...(
Args) > 0,
"Must have at least one matcher.");
1087 template <
typename T>
1088 operator Matcher<T>()
const {
1089 std::vector<Matcher<T> >
values;
1090 CreateVariadicMatcher<T>(&
values, std::integral_constant<size_t, 0>());
1095 template <
typename T,
size_t I>
1096 void CreateVariadicMatcher(std::vector<Matcher<T> >*
values,
1097 std::integral_constant<size_t, I>)
const {
1099 CreateVariadicMatcher<T>(
values, std::integral_constant<size_t, I + 1>());
1102 template <
typename T>
1103 void CreateVariadicMatcher(
1104 std::vector<Matcher<T> >*,
1105 std::integral_constant<
size_t,
sizeof...(
Args)>)
const {}
1112 template <
typename...
Args>
1113 using AllOfMatcher = VariadicMatcher<AllOfMatcherImpl,
Args...>;
1119 template <
typename T>
1120 class AnyOfMatcherImpl :
public MatcherInterface<const T&> {
1122 explicit AnyOfMatcherImpl(std::vector<Matcher<T> >
matchers)
1125 void DescribeTo(::std::ostream* os)
const override {
1128 if (
i != 0) *os <<
") or (";
1134 void DescribeNegationTo(::std::ostream* os)
const override {
1137 if (
i != 0) *os <<
") and (";
1143 bool MatchAndExplain(
const T&
x,
1144 MatchResultListener* listener)
const override {
1150 StringMatchResultListener slistener;
1151 if (
matchers_[
i].MatchAndExplain(
x, &slistener)) {
1152 *listener << slistener.str();
1155 if (no_match_result.empty()) {
1156 no_match_result = slistener.str();
1160 no_match_result +=
", and ";
1161 no_match_result +=
result;
1168 *listener << no_match_result;
1173 const std::vector<Matcher<T> >
matchers_;
1179 template <
typename...
Args>
1180 using AnyOfMatcher = VariadicMatcher<AnyOfMatcherImpl,
Args...>;
1183 template <
template <
class>
class MatcherImpl,
typename T>
1184 class SomeOfArrayMatcher {
1188 template <
typename Iter>
1191 template <
typename U>
1192 operator Matcher<U>()
const {
1194 std::vector<Matcher<RawU>>
matchers;
1196 matchers.push_back(MatcherCast<RawU>(matcher));
1207 template <
typename T>
1208 using AllOfArrayMatcher = SomeOfArrayMatcher<AllOfMatcherImpl, T>;
1210 template <
typename T>
1211 using AnyOfArrayMatcher = SomeOfArrayMatcher<AnyOfMatcherImpl, T>;
1215 template <
typename Predicate>
1216 class TrulyMatcher {
1224 template <
typename T>
1226 MatchResultListener* )
const {
1239 *os <<
"satisfies the given predicate";
1243 *os <<
"doesn't satisfy the given predicate";
1254 template <
typename M>
1255 class MatcherAsPredicate {
1265 template <
typename T>
1281 return MatcherCast<const T&>(
matcher_).Matches(
x);
1292 template <
typename M>
1293 class PredicateFormatterFromMatcher {
1300 template <
typename T>
1301 AssertionResult
operator()(
const char* value_text,
const T&
x)
const {
1313 const Matcher<const T&> matcher = SafeMatcherCast<const T&>(
matcher_);
1317 if (matcher.Matches(
x)) {
1321 ::std::stringstream ss;
1322 ss <<
"Value of: " << value_text <<
"\n"
1324 matcher.DescribeTo(&ss);
1327 StringMatchResultListener listener;
1329 ss <<
"\n The matcher failed on the initial attempt; but passed when "
1330 "rerun to generate the explanation.";
1332 ss <<
"\n Actual: " << listener.str();
1346 template <
typename M>
1347 inline PredicateFormatterFromMatcher<M>
1349 return PredicateFormatterFromMatcher<M>(
std::move(matcher));
1356 template <
typename FloatType>
1357 class FloatingEqMatcher {
1378 <<
", where max_abs_error is" << max_abs_error;
1382 template <
typename T>
1383 class Impl :
public MatcherInterface<T> {
1391 MatchResultListener* listener)
const override {
1392 const FloatingPoint<FloatType> actual(
value), expected(
expected_);
1395 if (actual.is_nan() || expected.is_nan()) {
1396 if (actual.is_nan() && expected.is_nan()) {
1416 if (listener->IsInterested()) {
1421 return actual.AlmostEquals(expected);
1425 void DescribeTo(::std::ostream* os)
const override {
1429 const ::std::streamsize old_precision = os->precision(
1430 ::std::numeric_limits<FloatType>::digits10 + 2);
1431 if (FloatingPoint<FloatType>(
expected_).is_nan()) {
1435 *os <<
"never matches";
1438 *os <<
"is approximately " <<
expected_;
1443 os->precision(old_precision);
1448 const ::std::streamsize old_precision = os->precision(
1449 ::std::numeric_limits<FloatType>::digits10 + 2);
1450 if (FloatingPoint<FloatType>(
expected_).is_nan()) {
1454 *os <<
"is anything";
1457 *os <<
"isn't approximately " <<
expected_;
1463 os->precision(old_precision);
1485 operator Matcher<FloatType>()
const {
1490 operator Matcher<const FloatType&>()
const {
1495 operator Matcher<FloatType&>()
const {
1514 template <
typename FloatType>
1515 class FloatingEq2Matcher {
1517 FloatingEq2Matcher() {
Init(-1,
false); }
1519 explicit FloatingEq2Matcher(
bool nan_eq_nan) {
Init(-1, nan_eq_nan); }
1521 explicit FloatingEq2Matcher(
FloatType max_abs_error) {
1522 Init(max_abs_error,
false);
1525 FloatingEq2Matcher(
FloatType max_abs_error,
bool nan_eq_nan) {
1526 Init(max_abs_error, nan_eq_nan);
1529 template <
typename T1,
typename T2>
1530 operator Matcher<::std::tuple<T1, T2>>()
const {
1532 new Impl<::std::tuple<T1, T2>>(max_abs_error_, nan_eq_nan_));
1534 template <
typename T1,
typename T2>
1535 operator Matcher<const ::std::tuple<T1, T2>&>()
const {
1537 new Impl<const ::std::tuple<T1, T2>&>(max_abs_error_, nan_eq_nan_));
1541 static ::std::ostream& GetDesc(::std::ostream& os) {
1542 return os <<
"an almost-equal pair";
1545 template <
typename Tuple>
1546 class Impl :
public MatcherInterface<Tuple> {
1548 Impl(
FloatType max_abs_error,
bool nan_eq_nan) :
1549 max_abs_error_(max_abs_error),
1550 nan_eq_nan_(nan_eq_nan) {}
1552 bool MatchAndExplain(Tuple
args,
1553 MatchResultListener* listener)
const override {
1554 if (max_abs_error_ == -1) {
1555 FloatingEqMatcher<FloatType> fm(::std::get<0>(
args), nan_eq_nan_);
1556 return static_cast<Matcher<FloatType>
>(fm).MatchAndExplain(
1557 ::std::get<1>(
args), listener);
1559 FloatingEqMatcher<FloatType> fm(::std::get<0>(
args), nan_eq_nan_,
1561 return static_cast<Matcher<FloatType>
>(fm).MatchAndExplain(
1562 ::std::get<1>(
args), listener);
1565 void DescribeTo(::std::ostream* os)
const override {
1566 *os <<
"are " << GetDesc;
1568 void DescribeNegationTo(::std::ostream* os)
const override {
1569 *os <<
"aren't " << GetDesc;
1574 const bool nan_eq_nan_;
1577 void Init(
FloatType max_abs_error_val,
bool nan_eq_nan_val) {
1578 max_abs_error_ = max_abs_error_val;
1579 nan_eq_nan_ = nan_eq_nan_val;
1587 template <
typename InnerMatcher>
1588 class PointeeMatcher {
1600 template <
typename Po
inter>
1601 operator Matcher<Pointer>()
const {
1602 return Matcher<Pointer>(
new Impl<const Pointer&>(
matcher_));
1607 template <
typename Po
inter>
1608 class Impl :
public MatcherInterface<Pointer> {
1614 explicit Impl(
const InnerMatcher& matcher)
1615 :
matcher_(MatcherCast<const Pointee&>(matcher)) {}
1617 void DescribeTo(::std::ostream* os)
const override {
1618 *os <<
"points to a value that ";
1623 *os <<
"does not point to a value that ";
1628 MatchResultListener* listener)
const override {
1631 *listener <<
"which points to ";
1636 const Matcher<const Pointee&>
matcher_;
1653 template <
typename To>
1654 class WhenDynamicCastToMatcherBase {
1673 return GetTypeName<To>();
1678 *os <<
"when dynamic_cast to " <<
GetToName() <<
", ";
1686 template <
typename To>
1687 class WhenDynamicCastToMatcher :
public WhenDynamicCastToMatcherBase<To> {
1690 : WhenDynamicCastToMatcherBase<To>(matcher) {}
1692 template <
typename From>
1694 To
to =
dynamic_cast<To
>(
from);
1701 template <
typename To>
1702 class WhenDynamicCastToMatcher<To&> :
public WhenDynamicCastToMatcherBase<To&> {
1704 explicit WhenDynamicCastToMatcher(
const Matcher<To&>& matcher)
1705 : WhenDynamicCastToMatcherBase<To&>(matcher) {}
1707 template <
typename From>
1708 bool MatchAndExplain(From&
from, MatchResultListener* listener)
const {
1710 To*
to =
dynamic_cast<To*
>(&
from);
1711 if (
to ==
nullptr) {
1712 *listener <<
"which cannot be dynamic_cast to " << this->GetToName();
1718 #endif // GTEST_HAS_RTTI
1722 template <
typename Class,
typename FieldType>
1723 class FieldMatcher {
1726 const Matcher<const FieldType&>& matcher)
1730 const Matcher<const FieldType&>& matcher)
1733 whose_field_(
"whose field `" + field_name +
"` ") {}
1736 *os <<
"is an object " << whose_field_;
1741 *os <<
"is an object " << whose_field_;
1745 template <
typename T>
1757 MatchResultListener* listener)
const {
1758 *listener << whose_field_ <<
"is ";
1763 MatchResultListener* listener)
const {
1764 if (p ==
nullptr)
return false;
1766 *listener <<
"which points to an object ";
1774 const Matcher<const FieldType&>
matcher_;
1788 template <
typename Class,
typename PropertyType,
typename Property>
1789 class PropertyMatcher {
1791 typedef const PropertyType& RefToConstProperty;
1796 whose_property_(
"whose given property ") {}
1799 const Matcher<RefToConstProperty>& matcher)
1802 whose_property_(
"whose property `" + property_name +
"` ") {}
1805 *os <<
"is an object " << whose_property_;
1810 *os <<
"is an object " << whose_property_;
1814 template <
typename T>
1824 MatchResultListener* listener)
const {
1825 *listener << whose_property_ <<
"is ";
1833 MatchResultListener* listener)
const {
1834 if (p ==
nullptr)
return false;
1836 *listener <<
"which points to an object ";
1844 const Matcher<RefToConstProperty>
matcher_;
1855 template <
typename Functor>
1856 struct CallableTraits {
1861 template <
typename T>
1862 static auto Invoke(Functor f,
T arg) -> decltype(f(
arg)) {
return f(
arg); }
1866 template <
typename ArgType,
typename ResType>
1867 struct CallableTraits<ResType(*)(ArgType)> {
1868 typedef ResType ResultType;
1869 typedef ResType(*StorageType)(ArgType);
1871 static void CheckIsValid(ResType(*
f)(ArgType)) {
1873 <<
"NULL function pointer is passed into ResultOf().";
1875 template <
typename T>
1876 static ResType
Invoke(ResType(*
f)(ArgType),
T arg) {
1883 template <
typename Callable,
typename InnerMatcher>
1884 class ResultOfMatcher {
1891 template <
typename T>
1892 operator Matcher<T>()
const {
1899 template <
typename T>
1900 class Impl :
public MatcherInterface<T> {
1902 std::declval<CallableStorageType>(), std::declval<T>()));
1905 template <
typename M>
1909 void DescribeTo(::std::ostream* os)
const override {
1910 *os <<
"is mapped by the given callable to a value that ";
1915 *os <<
"is mapped by the given callable to a value that ";
1920 *listener <<
"which is mapped by the given callable to ";
1937 const Matcher<ResultType>
matcher_;
1949 template <
typename SizeMatcher>
1950 class SizeIsMatcher {
1956 template <
typename Container>
1957 operator Matcher<Container>()
const {
1958 return Matcher<Container>(
new Impl<const Container&>(
size_matcher_));
1961 template <
typename Container>
1962 class Impl :
public MatcherInterface<Container> {
1964 using SizeType = decltype(std::declval<Container>().
size());
1965 explicit Impl(
const SizeMatcher& size_matcher)
1968 void DescribeTo(::std::ostream* os)
const override {
1978 MatchResultListener* listener)
const override {
1980 StringMatchResultListener size_listener;
1983 <<
"whose size " <<
size << (
result ?
" matches" :
" doesn't match");
2000 template <
typename DistanceMatcher>
2001 class BeginEndDistanceIsMatcher {
2006 template <
typename Container>
2007 operator Matcher<Container>()
const {
2011 template <
typename Container>
2012 class Impl :
public MatcherInterface<Container> {
2014 typedef internal::StlContainerView<
2016 typedef typename std::iterator_traits<
2017 typename ContainerView::type::const_iterator>::difference_type
2019 explicit Impl(
const DistanceMatcher& distance_matcher)
2022 void DescribeTo(::std::ostream* os)
const override {
2023 *os <<
"distance between begin() and end() ";
2027 *os <<
"distance between begin() and end() ";
2032 MatchResultListener* listener)
const override {
2036 StringMatchResultListener distance_listener;
2039 *listener <<
"whose distance between begin() and end() " << distance
2040 << (
result ?
" matches" :
" doesn't match");
2065 template <
typename Container>
2066 class ContainerEqMatcher {
2068 typedef internal::StlContainerView<Container>
View;
2087 *os <<
"does not equal ";
2091 template <
typename LhsContainer>
2093 MatchResultListener* listener)
const {
2094 typedef internal::StlContainerView<
2102 ::std::ostream*
const os = listener->stream();
2103 if (os !=
nullptr) {
2105 bool printed_header =
false;
2106 for (
typename LhsStlContainer::const_iterator
it =
2107 lhs_stl_container.begin();
2108 it != lhs_stl_container.end(); ++
it) {
2111 if (printed_header) {
2114 *os <<
"which has these unexpected elements: ";
2115 printed_header =
true;
2122 bool printed_header2 =
false;
2123 for (
typename StlContainer::const_iterator
it =
expected_.begin();
2126 lhs_stl_container.begin(), lhs_stl_container.end(), *
it) ==
2127 lhs_stl_container.end()) {
2128 if (printed_header2) {
2131 *os << (printed_header ?
",\nand" :
"which")
2132 <<
" doesn't have these expected elements: ";
2133 printed_header2 =
true;
2150 struct LessComparator {
2151 template <
typename T,
typename U>
2152 bool operator()(
const T& lhs,
const U& rhs)
const {
return lhs < rhs; }
2156 template <
typename Comparator,
typename ContainerMatcher>
2157 class WhenSortedByMatcher {
2160 const ContainerMatcher& matcher)
2163 template <
typename LhsContainer>
2164 operator Matcher<LhsContainer>()
const {
2168 template <
typename LhsContainer>
2169 class Impl :
public MatcherInterface<LhsContainer> {
2171 typedef internal::StlContainerView<
2177 typedef typename RemoveConstFromKey<
2180 Impl(
const Comparator& comparator,
const ContainerMatcher& matcher)
2183 void DescribeTo(::std::ostream* os)
const override {
2184 *os <<
"(when sorted) ";
2189 *os <<
"(when sorted) ";
2194 MatchResultListener* listener)
const override {
2196 ::std::vector<LhsValue> sorted_container(lhs_stl_container.begin(),
2197 lhs_stl_container.end());
2199 sorted_container.begin(), sorted_container.end(),
comparator_);
2201 if (!listener->IsInterested()) {
2204 return matcher_.Matches(sorted_container);
2207 *listener <<
"which is ";
2209 *listener <<
" when sorted";
2211 StringMatchResultListener inner_listener;
2212 const bool match =
matcher_.MatchAndExplain(sorted_container,
2220 const Matcher<const ::std::vector<LhsValue>&>
matcher_;
2236 template <
typename TupleMatcher,
typename RhsContainer>
2237 class PointwiseMatcher {
2240 use_UnorderedPointwise_with_hash_tables);
2243 typedef internal::StlContainerView<RhsContainer>
RhsView;
2249 PointwiseMatcher(
const TupleMatcher& tuple_matcher,
const RhsContainer& rhs)
2257 template <
typename LhsContainer>
2258 operator Matcher<LhsContainer>()
const {
2261 use_UnorderedPointwise_with_hash_tables);
2263 return Matcher<LhsContainer>(
2267 template <
typename LhsContainer>
2268 class Impl :
public MatcherInterface<LhsContainer> {
2270 typedef internal::StlContainerView<
2279 typedef ::std::tuple<const LhsValue&, const RhsValue&>
InnerMatcherArg;
2286 void DescribeTo(::std::ostream* os)
const override {
2287 *os <<
"contains " <<
rhs_.size()
2288 <<
" values, where each value and its corresponding value in ";
2294 *os <<
"doesn't contain exactly " <<
rhs_.size()
2295 <<
" values, or contains a value x at some index i"
2296 <<
" where x and the i-th value of ";
2303 MatchResultListener* listener)
const override {
2305 const size_t actual_size = lhs_stl_container.size();
2306 if (actual_size !=
rhs_.size()) {
2307 *listener <<
"which contains " << actual_size <<
" values";
2311 typename LhsStlContainer::const_iterator left = lhs_stl_container.begin();
2312 typename RhsStlContainer::const_iterator right =
rhs_.begin();
2313 for (
size_t i = 0;
i != actual_size; ++
i, ++left, ++right) {
2314 if (listener->IsInterested()) {
2315 StringMatchResultListener inner_listener;
2321 ImplicitCast_<const RhsValue&>(*right)),
2323 *listener <<
"where the value pair (";
2327 *listener <<
") at index #" <<
i <<
" don't match";
2334 ImplicitCast_<const RhsValue&>(*right))))
2357 template <
typename Container>
2358 class QuantifierMatcherImpl :
public MatcherInterface<Container> {
2361 typedef StlContainerView<RawContainer>
View;
2366 template <
typename InnerMatcher>
2369 testing::SafeMatcherCast<const Element&>(inner_matcher)) {}
2376 MatchResultListener* listener)
const {
2379 for (
typename StlContainer::const_iterator
it = stl_container.begin();
2380 it != stl_container.end(); ++
it, ++
i) {
2381 StringMatchResultListener inner_listener;
2384 if (matches != all_elements_should_match) {
2385 *listener <<
"whose element #" <<
i
2386 << (matches ?
" matches" :
" doesn't match");
2388 return !all_elements_should_match;
2391 return all_elements_should_match;
2402 template <
typename Container>
2403 class ContainsMatcherImpl :
public QuantifierMatcherImpl<Container> {
2405 template <
typename InnerMatcher>
2407 : QuantifierMatcherImpl<Container>(inner_matcher) {}
2410 void DescribeTo(::std::ostream* os)
const override {
2411 *os <<
"contains at least one element that ";
2416 *os <<
"doesn't contain any element that ";
2421 MatchResultListener* listener)
const override {
2431 template <
typename Container>
2432 class EachMatcherImpl :
public QuantifierMatcherImpl<Container> {
2434 template <
typename InnerMatcher>
2436 : QuantifierMatcherImpl<Container>(inner_matcher) {}
2439 void DescribeTo(::std::ostream* os)
const override {
2440 *os <<
"only contains elements that ";
2445 *os <<
"contains some element that ";
2450 MatchResultListener* listener)
const override {
2459 template <
typename M>
2460 class ContainsMatcher {
2464 template <
typename Container>
2465 operator Matcher<Container>()
const {
2466 return Matcher<Container>(
2477 template <
typename M>
2482 template <
typename Container>
2483 operator Matcher<Container>()
const {
2484 return Matcher<Container>(
2495 struct Rank0 : Rank1 {};
2497 namespace pair_getters {
2499 template <
typename T>
2500 auto First(
T&
x, Rank1) -> decltype(get<0>(
x)) {
2503 template <
typename T>
2504 auto First(
T&
x, Rank0) -> decltype((
x.first)) {
2508 template <
typename T>
2509 auto Second(
T&
x, Rank1) -> decltype(get<1>(
x)) {
2512 template <
typename T>
2513 auto Second(
T&
x, Rank0) -> decltype((
x.second)) {
2522 template <
typename PairType>
2523 class KeyMatcherImpl :
public MatcherInterface<PairType> {
2526 typedef typename RawPairType::first_type
KeyType;
2528 template <
typename InnerMatcher>
2531 testing::SafeMatcherCast<const KeyType&>(inner_matcher)) {
2536 MatchResultListener* listener)
const override {
2537 StringMatchResultListener inner_listener;
2539 pair_getters::First(key_value, Rank0()), &inner_listener);
2540 const std::string explanation = inner_listener.str();
2541 if (explanation !=
"") {
2542 *listener <<
"whose first field is a value " << explanation;
2548 void DescribeTo(::std::ostream* os)
const override {
2549 *os <<
"has a key that ";
2555 *os <<
"doesn't have a key that ";
2566 template <
typename M>
2571 template <
typename PairType>
2572 operator Matcher<PairType>()
const {
2573 return Matcher<PairType>(
2585 template <
typename PairType>
2586 class PairMatcherImpl :
public MatcherInterface<PairType> {
2589 typedef typename RawPairType::first_type
FirstType;
2590 typedef typename RawPairType::second_type
SecondType;
2592 template <
typename FirstMatcher,
typename SecondMatcher>
2593 PairMatcherImpl(FirstMatcher first_matcher, SecondMatcher second_matcher)
2595 testing::SafeMatcherCast<const FirstType&>(first_matcher)),
2597 testing::SafeMatcherCast<const SecondType&>(second_matcher)) {
2601 void DescribeTo(::std::ostream* os)
const override {
2602 *os <<
"has a first field that ";
2604 *os <<
", and has a second field that ";
2610 *os <<
"has a first field that ";
2612 *os <<
", or has a second field that ";
2619 MatchResultListener* listener)
const override {
2620 if (!listener->IsInterested()) {
2623 return first_matcher_.Matches(pair_getters::First(a_pair, Rank0())) &&
2626 StringMatchResultListener first_inner_listener;
2627 if (!
first_matcher_.MatchAndExplain(pair_getters::First(a_pair, Rank0()),
2628 &first_inner_listener)) {
2629 *listener <<
"whose first field does not match";
2633 StringMatchResultListener second_inner_listener;
2634 if (!
second_matcher_.MatchAndExplain(pair_getters::Second(a_pair, Rank0()),
2635 &second_inner_listener)) {
2636 *listener <<
"whose second field does not match";
2640 ExplainSuccess(first_inner_listener.str(), second_inner_listener.str(),
2648 MatchResultListener* listener)
const {
2649 *listener <<
"whose both fields match";
2650 if (first_explanation !=
"") {
2651 *listener <<
", where the first field is a value " << first_explanation;
2653 if (second_explanation !=
"") {
2655 if (first_explanation !=
"") {
2656 *listener <<
"and ";
2658 *listener <<
"where ";
2660 *listener <<
"the second field is a value " << second_explanation;
2671 template <
typename FirstMatcher,
typename SecondMatcher>
2674 PairMatcher(FirstMatcher first_matcher, SecondMatcher second_matcher)
2677 template <
typename PairType>
2678 operator Matcher<PairType> ()
const {
2679 return Matcher<PairType>(
2691 template <
typename Container>
2692 class ElementsAreMatcherImpl :
public MatcherInterface<Container> {
2695 typedef internal::StlContainerView<RawContainer>
View;
2702 template <
typename InputIter>
2704 while (
first != last) {
2710 void DescribeTo(::std::ostream* os)
const override {
2713 }
else if (
count() == 1) {
2714 *os <<
"has 1 element that ";
2718 for (
size_t i = 0;
i !=
count(); ++
i) {
2719 *os <<
"element #" <<
i <<
" ";
2731 *os <<
"isn't empty";
2736 for (
size_t i = 0;
i !=
count(); ++
i) {
2737 *os <<
"element #" <<
i <<
" ";
2746 MatchResultListener* listener)
const override {
2750 const bool listener_interested = listener->IsInterested();
2753 ::std::vector<std::string> explanations(
count());
2755 typename StlContainer::const_iterator
it = stl_container.begin();
2756 size_t exam_pos = 0;
2757 bool mismatch_found =
false;
2762 for (;
it != stl_container.end() && exam_pos !=
count(); ++
it, ++exam_pos) {
2764 if (listener_interested) {
2765 StringMatchResultListener s;
2767 explanations[exam_pos] = s.str();
2773 mismatch_found =
true;
2782 size_t actual_count = exam_pos;
2783 for (;
it != stl_container.end(); ++
it) {
2787 if (actual_count !=
count()) {
2792 if (listener_interested && (actual_count != 0)) {
2793 *listener <<
"which has " <<
Elements(actual_count);
2798 if (mismatch_found) {
2800 if (listener_interested) {
2801 *listener <<
"whose element #" << exam_pos <<
" doesn't match";
2809 if (listener_interested) {
2810 bool reason_printed =
false;
2811 for (
size_t i = 0;
i !=
count(); ++
i) {
2814 if (reason_printed) {
2815 *listener <<
",\nand ";
2817 *listener <<
"whose element #" <<
i <<
" matches, " << s;
2818 reason_printed =
true;
2832 ::std::vector<Matcher<const Element&> >
matchers_;
2845 num_matchers_(num_matchers),
2846 matched_(num_elements_* num_matchers_, 0) {
2849 size_t LhsSize()
const {
return num_elements_; }
2850 size_t RhsSize()
const {
return num_matchers_; }
2851 bool HasEdge(
size_t ilhs,
size_t irhs)
const {
2852 return matched_[SpaceIndex(ilhs, irhs)] == 1;
2854 void SetEdge(
size_t ilhs,
size_t irhs,
bool b) {
2855 matched_[SpaceIndex(ilhs, irhs)] =
b ? 1 : 0;
2868 size_t SpaceIndex(
size_t ilhs,
size_t irhs)
const {
2869 return ilhs * num_matchers_ + irhs;
2872 size_t num_elements_;
2873 size_t num_matchers_;
2878 ::std::vector<char> matched_;
2889 struct UnorderedMatcherRequire {
2893 ExactMatch = Superset | Subset,
2900 class GTEST_API_ UnorderedElementsAreMatcherImplBase {
2902 explicit UnorderedElementsAreMatcherImplBase(
2904 : match_flags_(matcher_flags) {}
2909 typedef ::std::vector<const MatcherDescriberInterface*> MatcherDescriberVec;
2912 void DescribeToImpl(::std::ostream* os)
const;
2915 void DescribeNegationToImpl(::std::ostream* os)
const;
2917 bool VerifyMatchMatrix(const ::std::vector<std::string>& element_printouts,
2918 const MatchMatrix& matrix,
2919 MatchResultListener* listener)
const;
2922 MatchResultListener* listener)
const;
2924 MatcherDescriberVec& matcher_describers() {
2925 return matcher_describers_;
2928 static Message Elements(
size_t n) {
2929 return Message() <<
n <<
" element" << (
n == 1 ?
"" :
"s");
2936 MatcherDescriberVec matcher_describers_;
2943 template <
typename Container>
2944 class UnorderedElementsAreMatcherImpl
2945 :
public MatcherInterface<Container>,
2946 public UnorderedElementsAreMatcherImplBase {
2949 typedef internal::StlContainerView<RawContainer>
View;
2955 template <
typename InputIter>
2957 InputIter
first, InputIter last)
2958 : UnorderedElementsAreMatcherImplBase(matcher_flags) {
2966 void DescribeTo(::std::ostream* os)
const override {
2976 MatchResultListener* listener)
const override {
2978 ::std::vector<std::string> element_printouts;
2979 MatchMatrix matrix =
2981 &element_printouts, listener);
2983 if (matrix.LhsSize() == 0 && matrix.RhsSize() == 0) {
2987 if (match_flags() == UnorderedMatcherRequire::ExactMatch) {
2988 if (matrix.LhsSize() != matrix.RhsSize()) {
2993 if (matrix.LhsSize() != 0 && listener->IsInterested()) {
2994 *listener <<
"which has " <<
Elements(matrix.LhsSize());
3000 return VerifyMatchMatrix(element_printouts, matrix, listener) &&
3005 template <
typename ElementIter>
3006 MatchMatrix
AnalyzeElements(ElementIter elem_first, ElementIter elem_last,
3007 ::std::vector<std::string>* element_printouts,
3008 MatchResultListener* listener)
const {
3009 element_printouts->clear();
3010 ::std::vector<char> did_match;
3012 for (; elem_first != elem_last; ++
num_elements, ++elem_first) {
3013 if (listener->IsInterested()) {
3016 for (
size_t irhs = 0; irhs !=
matchers_.size(); ++irhs) {
3022 ::std::vector<char>::const_iterator did_match_iter = did_match.begin();
3024 for (
size_t irhs = 0; irhs !=
matchers_.size(); ++irhs) {
3025 matrix.SetEdge(ilhs, irhs, *did_match_iter++ != 0);
3031 ::std::vector<Matcher<const Element&> >
matchers_;
3038 template <
typename Target>
3039 struct CastAndAppendTransform {
3040 template <
typename Arg>
3042 return MatcherCast<Target>(
a);
3047 template <
typename MatcherTuple>
3048 class UnorderedElementsAreMatcher {
3053 template <
typename Container>
3054 operator Matcher<Container>()
const {
3058 typedef ::std::vector<Matcher<const Element&> > MatcherVec;
3063 return Matcher<Container>(
3064 new UnorderedElementsAreMatcherImpl<const Container&>(
3065 UnorderedMatcherRequire::ExactMatch,
matchers.begin(),
3075 template <
typename MatcherTuple>
3076 class ElementsAreMatcher {
3080 template <
typename Container>
3081 operator Matcher<Container>()
const {
3085 use_UnorderedElementsAre_with_hash_tables);
3090 typedef ::std::vector<Matcher<const Element&> > MatcherVec;
3095 return Matcher<Container>(
new ElementsAreMatcherImpl<const Container&>(
3105 template <
typename T>
3106 class UnorderedElementsAreArrayMatcher {
3108 template <
typename Iter>
3113 template <
typename Container>
3114 operator Matcher<Container>()
const {
3115 return Matcher<Container>(
3116 new UnorderedElementsAreMatcherImpl<const Container&>(
3128 template <
typename T>
3129 class ElementsAreArrayMatcher {
3131 template <
typename Iter>
3134 template <
typename Container>
3135 operator Matcher<Container>()
const {
3138 use_UnorderedElementsAreArray_with_hash_tables);
3140 return Matcher<Container>(
new ElementsAreMatcherImpl<const Container&>(
3159 template <
typename Tuple2Matcher,
typename Second>
3160 class BoundSecondMatcher {
3165 template <
typename T>
3166 operator Matcher<T>()
const {
3183 template <
typename T>
3184 class Impl :
public MatcherInterface<T> {
3186 typedef ::std::tuple<T, Second>
ArgTuple;
3192 void DescribeTo(::std::ostream* os)
const override {
3219 template <
typename Tuple2Matcher,
typename Second>
3221 const Tuple2Matcher&
tm,
const Second&
second) {
3222 return BoundSecondMatcher<Tuple2Matcher, Second>(
tm,
second);
3231 const char* matcher_name,
3235 template <
typename ValueMatcher>
3236 class OptionalMatcher {
3238 explicit OptionalMatcher(
const ValueMatcher& value_matcher)
3239 : value_matcher_(value_matcher) {}
3241 template <
typename Optional>
3242 operator Matcher<Optional>()
const {
3243 return Matcher<Optional>(
new Impl<const Optional&>(value_matcher_));
3246 template <
typename Optional>
3247 class Impl :
public MatcherInterface<Optional> {
3251 explicit Impl(
const ValueMatcher& value_matcher)
3252 : value_matcher_(MatcherCast<ValueType>(value_matcher)) {}
3254 void DescribeTo(::std::ostream* os)
const override {
3256 value_matcher_.DescribeTo(os);
3259 void DescribeNegationTo(::std::ostream* os)
const override {
3261 value_matcher_.DescribeNegationTo(os);
3264 bool MatchAndExplain(
Optional optional,
3265 MatchResultListener* listener)
const override {
3267 *listener <<
"which is not engaged";
3271 StringMatchResultListener value_listener;
3272 const bool match = value_matcher_.MatchAndExplain(
value, &value_listener);
3274 << (
match ?
" matches" :
" doesn't match");
3280 const Matcher<ValueType> value_matcher_;
3285 const ValueMatcher value_matcher_;
3289 namespace variant_matcher {
3291 template <
typename T>
3293 template <
typename T>
3297 template <
typename T>
3298 class VariantMatcher {
3303 template <
typename Variant>
3304 bool MatchAndExplain(
const Variant&
value,
3308 return holds_alternative<T>(
value) && matcher_.Matches(get<T>(
value));
3311 if (!holds_alternative<T>(
value)) {
3312 *listener <<
"whose value is not of type '" <<
GetTypeName() <<
"'";
3317 StringMatchResultListener elem_listener;
3318 const bool match = matcher_.MatchAndExplain(
elem, &elem_listener);
3320 << (
match ?
" matches" :
" doesn't match");
3325 void DescribeTo(std::ostream* os)
const {
3326 *os <<
"is a variant<> with value of type '" <<
GetTypeName()
3327 <<
"' and the value ";
3328 matcher_.DescribeTo(os);
3331 void DescribeNegationTo(std::ostream* os)
const {
3332 *os <<
"is a variant<> with value of type other than '" <<
GetTypeName()
3333 <<
"' or the value ";
3334 matcher_.DescribeNegationTo(os);
3341 return internal::GetTypeName<T>());
3343 return "the element type";
3346 const ::testing::Matcher<const T&> matcher_;
3351 namespace any_cast_matcher {
3354 template <
typename T>
3358 template <
typename T>
3359 class AnyCastMatcher {
3361 explicit AnyCastMatcher(const ::testing::Matcher<const T&>& matcher)
3362 : matcher_(matcher) {}
3364 template <
typename AnyType>
3365 bool MatchAndExplain(
const AnyType&
value,
3369 return ptr !=
nullptr && matcher_.Matches(*
ptr);
3373 if (
elem ==
nullptr) {
3374 *listener <<
"whose value is not of type '" <<
GetTypeName() <<
"'";
3378 StringMatchResultListener elem_listener;
3379 const bool match = matcher_.MatchAndExplain(*
elem, &elem_listener);
3381 << (
match ?
" matches" :
" doesn't match");
3386 void DescribeTo(std::ostream* os)
const {
3387 *os <<
"is an 'any' type with value of type '" <<
GetTypeName()
3388 <<
"' and the value ";
3389 matcher_.DescribeTo(os);
3392 void DescribeNegationTo(std::ostream* os)
const {
3393 *os <<
"is an 'any' type with value of type other than '" <<
GetTypeName()
3394 <<
"' or the value ";
3395 matcher_.DescribeNegationTo(os);
3402 return internal::GetTypeName<T>());
3404 return "the element type";
3407 const ::testing::Matcher<const T&> matcher_;
3413 template <
class ArgsTuple,
size_t...
k>
3414 class ArgsMatcherImpl :
public MatcherInterface<ArgsTuple> {
3421 template <
typename InnerMatcher>
3423 :
inner_matcher_(SafeMatcherCast<const SelectedArgs&>(inner_matcher)) {}
3426 MatchResultListener* listener)
const override {
3430 std::forward_as_tuple(std::get<k>(
args)...);
3431 if (!listener->IsInterested())
return inner_matcher_.Matches(selected_args);
3436 StringMatchResultListener inner_listener;
3443 void DescribeTo(::std::ostream* os)
const override {
3444 *os <<
"are a tuple ";
3450 *os <<
"are a tuple ";
3458 *os <<
"whose fields (";
3459 const char*
sep =
"";
3462 const char*
dummy[] = {
"", (*os <<
sep <<
"#" <<
k,
sep =
", ")...};
3470 template <
class InnerMatcher,
size_t...
k>
3476 template <
typename ArgsTuple>
3477 operator Matcher<ArgsTuple>()
const {
3502 template <
typename Iter>
3503 inline internal::ElementsAreArrayMatcher<
3507 return internal::ElementsAreArrayMatcher<T>(
first, last);
3510 template <
typename T>
3512 const T* pointer,
size_t count) {
3516 template <
typename T,
size_t N>
3522 template <
typename Container>
3523 inline internal::ElementsAreArrayMatcher<typename Container::value_type>
3528 template <
typename T>
3529 inline internal::ElementsAreArrayMatcher<T>
3547 template <
typename Iter>
3548 inline internal::UnorderedElementsAreArrayMatcher<
3552 return internal::UnorderedElementsAreArrayMatcher<T>(
3553 internal::UnorderedMatcherRequire::ExactMatch,
first, last);
3556 template <
typename T>
3557 inline internal::UnorderedElementsAreArrayMatcher<T>
3562 template <
typename T,
size_t N>
3563 inline internal::UnorderedElementsAreArrayMatcher<T>
3568 template <
typename Container>
3569 inline internal::UnorderedElementsAreArrayMatcher<
3575 template <
typename T>
3576 inline internal::UnorderedElementsAreArrayMatcher<T>
3590 const internal::AnythingMatcher
_ = {};
3592 template <
typename T>
3593 inline Matcher<T>
A() {
3594 return Matcher<T>(
new internal::AnyMatcherImpl<T>());
3598 template <
typename T>
3599 inline Matcher<T>
An() {
return A<T>(); }
3601 template <
typename T,
typename M>
3609 inline PolymorphicMatcher<internal::IsNullMatcher >
IsNull() {
3616 inline PolymorphicMatcher<internal::NotNullMatcher >
NotNull() {
3622 template <
typename T>
3623 inline internal::RefMatcher<T&>
Ref(
T&
x) {
3624 return internal::RefMatcher<T&>(
x);
3629 inline internal::FloatingEqMatcher<double>
DoubleEq(
double rhs) {
3630 return internal::FloatingEqMatcher<double>(rhs,
false);
3636 return internal::FloatingEqMatcher<double>(rhs,
true);
3642 inline internal::FloatingEqMatcher<double>
DoubleNear(
3643 double rhs,
double max_abs_error) {
3644 return internal::FloatingEqMatcher<double>(rhs,
false, max_abs_error);
3651 double rhs,
double max_abs_error) {
3652 return internal::FloatingEqMatcher<double>(rhs,
true, max_abs_error);
3657 inline internal::FloatingEqMatcher<float>
FloatEq(
float rhs) {
3658 return internal::FloatingEqMatcher<float>(rhs,
false);
3664 return internal::FloatingEqMatcher<float>(rhs,
true);
3670 inline internal::FloatingEqMatcher<float>
FloatNear(
3671 float rhs,
float max_abs_error) {
3672 return internal::FloatingEqMatcher<float>(rhs,
false, max_abs_error);
3679 float rhs,
float max_abs_error) {
3680 return internal::FloatingEqMatcher<float>(rhs,
true, max_abs_error);
3685 template <
typename InnerMatcher>
3686 inline internal::PointeeMatcher<InnerMatcher>
Pointee(
3687 const InnerMatcher& inner_matcher) {
3688 return internal::PointeeMatcher<InnerMatcher>(inner_matcher);
3698 template <
typename To>
3699 inline PolymorphicMatcher<internal::WhenDynamicCastToMatcher<To> >
3702 internal::WhenDynamicCastToMatcher<To>(inner_matcher));
3704 #endif // GTEST_HAS_RTTI
3710 template <
typename Class,
typename FieldType,
typename FieldMatcher>
3711 inline PolymorphicMatcher<
3712 internal::FieldMatcher<Class, FieldType> >
Field(
3715 internal::FieldMatcher<Class, FieldType>(
3716 field, MatcherCast<const FieldType&>(matcher)));
3725 template <
typename Class,
typename FieldType,
typename FieldMatcher>
3726 inline PolymorphicMatcher<internal::FieldMatcher<Class, FieldType> >
Field(
3728 const FieldMatcher& matcher) {
3730 field_name,
field, MatcherCast<const FieldType&>(matcher)));
3737 template <
typename Class,
typename PropertyType,
typename PropertyMatcher>
3738 inline PolymorphicMatcher<internal::PropertyMatcher<
3739 Class, PropertyType, PropertyType (Class::*)()
const> >
3740 Property(PropertyType (Class::*property)()
const,
3741 const PropertyMatcher& matcher) {
3743 internal::PropertyMatcher<Class, PropertyType,
3744 PropertyType (Class::*)()
const>(
3745 property, MatcherCast<const PropertyType&>(matcher)));
3754 template <
typename Class,
typename PropertyType,
typename PropertyMatcher>
3755 inline PolymorphicMatcher<internal::PropertyMatcher<
3756 Class, PropertyType, PropertyType (Class::*)()
const> >
3758 PropertyType (Class::*property)()
const,
3759 const PropertyMatcher& matcher) {
3761 internal::PropertyMatcher<Class, PropertyType,
3762 PropertyType (Class::*)()
const>(
3763 property_name, property, MatcherCast<const PropertyType&>(matcher)));
3767 template <
typename Class,
typename PropertyType,
typename PropertyMatcher>
3768 inline PolymorphicMatcher<internal::PropertyMatcher<
3769 Class, PropertyType, PropertyType (Class::*)()
const &> >
3770 Property(PropertyType (Class::*property)()
const &,
3771 const PropertyMatcher& matcher) {
3773 internal::PropertyMatcher<Class, PropertyType,
3774 PropertyType (Class::*)()
const&>(
3775 property, MatcherCast<const PropertyType&>(matcher)));
3779 template <
typename Class,
typename PropertyType,
typename PropertyMatcher>
3780 inline PolymorphicMatcher<internal::PropertyMatcher<
3781 Class, PropertyType, PropertyType (Class::*)()
const &> >
3783 PropertyType (Class::*property)()
const &,
3784 const PropertyMatcher& matcher) {
3786 internal::PropertyMatcher<Class, PropertyType,
3787 PropertyType (Class::*)()
const&>(
3788 property_name, property, MatcherCast<const PropertyType&>(matcher)));
3800 template <
typename Callable,
typename InnerMatcher>
3801 internal::ResultOfMatcher<Callable, InnerMatcher>
ResultOf(
3802 Callable callable, InnerMatcher matcher) {
3803 return internal::ResultOfMatcher<Callable, InnerMatcher>(
3810 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::string> >
StrEq(
3813 internal::StrEqualityMatcher<std::string>(
str,
true,
true));
3817 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::string> >
StrNe(
3820 internal::StrEqualityMatcher<std::string>(
str,
false,
true));
3824 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::string> >
StrCaseEq(
3827 internal::StrEqualityMatcher<std::string>(
str,
true,
false));
3831 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::string> >
StrCaseNe(
3834 internal::StrEqualityMatcher<std::string>(
str,
false,
false));
3839 inline PolymorphicMatcher<internal::HasSubstrMatcher<std::string> >
HasSubstr(
3842 internal::HasSubstrMatcher<std::string>(substring));
3846 inline PolymorphicMatcher<internal::StartsWithMatcher<std::string> >
StartsWith(
3849 internal::StartsWithMatcher<std::string>(
prefix));
3853 inline PolymorphicMatcher<internal::EndsWithMatcher<std::string> >
EndsWith(
3858 #if GTEST_HAS_STD_WSTRING
3862 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring> >
StrEq(
3865 internal::StrEqualityMatcher<std::wstring>(
str,
true,
true));
3869 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring> >
StrNe(
3872 internal::StrEqualityMatcher<std::wstring>(
str,
false,
true));
3876 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring> >
3879 internal::StrEqualityMatcher<std::wstring>(
str,
true,
false));
3883 inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring> >
3886 internal::StrEqualityMatcher<std::wstring>(
str,
false,
false));
3891 inline PolymorphicMatcher<internal::HasSubstrMatcher<std::wstring> >
HasSubstr(
3894 internal::HasSubstrMatcher<std::wstring>(substring));
3898 inline PolymorphicMatcher<internal::StartsWithMatcher<std::wstring> >
3901 internal::StartsWithMatcher<std::wstring>(
prefix));
3905 inline PolymorphicMatcher<internal::EndsWithMatcher<std::wstring> >
EndsWith(
3908 internal::EndsWithMatcher<std::wstring>(
suffix));
3911 #endif // GTEST_HAS_STD_WSTRING
3915 inline internal::Eq2Matcher
Eq() {
return internal::Eq2Matcher(); }
3919 inline internal::Ge2Matcher
Ge() {
return internal::Ge2Matcher(); }
3923 inline internal::Gt2Matcher
Gt() {
return internal::Gt2Matcher(); }
3927 inline internal::Le2Matcher
Le() {
return internal::Le2Matcher(); }
3931 inline internal::Lt2Matcher
Lt() {
return internal::Lt2Matcher(); }
3935 inline internal::Ne2Matcher
Ne() {
return internal::Ne2Matcher(); }
3939 inline internal::FloatingEq2Matcher<float>
FloatEq() {
3940 return internal::FloatingEq2Matcher<float>();
3945 inline internal::FloatingEq2Matcher<double>
DoubleEq() {
3946 return internal::FloatingEq2Matcher<double>();
3952 return internal::FloatingEq2Matcher<float>(
true);
3958 return internal::FloatingEq2Matcher<double>(
true);
3963 inline internal::FloatingEq2Matcher<float>
FloatNear(
float max_abs_error) {
3964 return internal::FloatingEq2Matcher<float>(max_abs_error);
3969 inline internal::FloatingEq2Matcher<double>
DoubleNear(
double max_abs_error) {
3970 return internal::FloatingEq2Matcher<double>(max_abs_error);
3977 float max_abs_error) {
3978 return internal::FloatingEq2Matcher<float>(max_abs_error,
true);
3985 double max_abs_error) {
3986 return internal::FloatingEq2Matcher<double>(max_abs_error,
true);
3991 template <
typename InnerMatcher>
3992 inline internal::NotMatcher<InnerMatcher>
Not(InnerMatcher
m) {
3993 return internal::NotMatcher<InnerMatcher>(
m);
3999 template <
typename Predicate>
4000 inline PolymorphicMatcher<internal::TrulyMatcher<Predicate> >
4001 Truly(Predicate pred) {
4011 template <
typename SizeMatcher>
4012 inline internal::SizeIsMatcher<SizeMatcher>
4013 SizeIs(
const SizeMatcher& size_matcher) {
4014 return internal::SizeIsMatcher<SizeMatcher>(size_matcher);
4022 template <
typename DistanceMatcher>
4023 inline internal::BeginEndDistanceIsMatcher<DistanceMatcher>
4025 return internal::BeginEndDistanceIsMatcher<DistanceMatcher>(distance_matcher);
4032 template <
typename Container>
4033 inline PolymorphicMatcher<internal::ContainerEqMatcher<
4040 internal::ContainerEqMatcher<RawContainer>(rhs));
4045 template <
typename Comparator,
typename ContainerMatcher>
4046 inline internal::WhenSortedByMatcher<Comparator, ContainerMatcher>
4048 const ContainerMatcher& container_matcher) {
4049 return internal::WhenSortedByMatcher<Comparator, ContainerMatcher>(
4050 comparator, container_matcher);
4055 template <
typename ContainerMatcher>
4056 inline internal::WhenSortedByMatcher<internal::LessComparator, ContainerMatcher>
4057 WhenSorted(
const ContainerMatcher& container_matcher) {
4059 internal::WhenSortedByMatcher<internal::LessComparator, ContainerMatcher>(
4060 internal::LessComparator(), container_matcher);
4069 template <
typename TupleMatcher,
typename Container>
4070 inline internal::PointwiseMatcher<TupleMatcher,
4072 Pointwise(
const TupleMatcher& tuple_matcher,
const Container& rhs) {
4077 return internal::PointwiseMatcher<TupleMatcher, RawContainer>(
4078 tuple_matcher, rhs);
4083 template <
typename TupleMatcher,
typename T>
4084 inline internal::PointwiseMatcher<TupleMatcher, std::vector<T> >
Pointwise(
4085 const TupleMatcher& tuple_matcher, std::initializer_list<T> rhs) {
4086 return Pointwise(tuple_matcher, std::vector<T>(rhs));
4101 template <
typename Tuple2Matcher,
typename RhsContainer>
4102 inline internal::UnorderedElementsAreArrayMatcher<
4103 typename internal::BoundSecondMatcher<
4105 typename internal::StlContainerView<
4108 const RhsContainer& rhs_container) {
4116 typedef typename internal::StlContainerView<RawRhsContainer> RhsView;
4119 const RhsStlContainer& rhs_stl_container =
4123 ::std::vector<internal::BoundSecondMatcher<Tuple2Matcher, Second> >
matchers;
4124 for (
typename RhsStlContainer::const_iterator
it = rhs_stl_container.begin();
4125 it != rhs_stl_container.end(); ++
it) {
4136 template <
typename Tuple2Matcher,
typename T>
4137 inline internal::UnorderedElementsAreArrayMatcher<
4138 typename internal::BoundSecondMatcher<Tuple2Matcher, T> >
4140 std::initializer_list<T> rhs) {
4163 template <
typename M>
4164 inline internal::ContainsMatcher<M>
Contains(M matcher) {
4165 return internal::ContainsMatcher<M>(matcher);
4195 template <
typename Iter>
4196 inline internal::UnorderedElementsAreArrayMatcher<
4200 return internal::UnorderedElementsAreArrayMatcher<T>(
4201 internal::UnorderedMatcherRequire::Superset,
first, last);
4204 template <
typename T>
4205 inline internal::UnorderedElementsAreArrayMatcher<T> IsSupersetOf(
4206 const T* pointer,
size_t count) {
4207 return IsSupersetOf(pointer, pointer +
count);
4210 template <
typename T,
size_t N>
4211 inline internal::UnorderedElementsAreArrayMatcher<T> IsSupersetOf(
4213 return IsSupersetOf(
array,
N);
4216 template <
typename Container>
4217 inline internal::UnorderedElementsAreArrayMatcher<
4219 IsSupersetOf(
const Container&
container) {
4223 template <
typename T>
4224 inline internal::UnorderedElementsAreArrayMatcher<T> IsSupersetOf(
4225 ::std::initializer_list<T> xs) {
4226 return IsSupersetOf(xs.begin(), xs.end());
4252 template <
typename Iter>
4253 inline internal::UnorderedElementsAreArrayMatcher<
4257 return internal::UnorderedElementsAreArrayMatcher<T>(
4258 internal::UnorderedMatcherRequire::Subset,
first, last);
4261 template <
typename T>
4262 inline internal::UnorderedElementsAreArrayMatcher<T> IsSubsetOf(
4263 const T* pointer,
size_t count) {
4264 return IsSubsetOf(pointer, pointer +
count);
4267 template <
typename T,
size_t N>
4268 inline internal::UnorderedElementsAreArrayMatcher<T> IsSubsetOf(
4270 return IsSubsetOf(
array,
N);
4273 template <
typename Container>
4274 inline internal::UnorderedElementsAreArrayMatcher<
4276 IsSubsetOf(
const Container&
container) {
4280 template <
typename T>
4281 inline internal::UnorderedElementsAreArrayMatcher<T> IsSubsetOf(
4282 ::std::initializer_list<T> xs) {
4283 return IsSubsetOf(xs.begin(), xs.end());
4313 template <
typename M>
4314 inline internal::EachMatcher<M>
Each(M matcher) {
4315 return internal::EachMatcher<M>(matcher);
4321 template <
typename M>
4322 inline internal::KeyMatcher<M>
Key(M inner_matcher) {
4323 return internal::KeyMatcher<M>(inner_matcher);
4331 template <
typename FirstMatcher,
typename SecondMatcher>
4332 inline internal::PairMatcher<FirstMatcher, SecondMatcher>
4333 Pair(FirstMatcher first_matcher, SecondMatcher second_matcher) {
4334 return internal::PairMatcher<FirstMatcher, SecondMatcher>(
4335 first_matcher, second_matcher);
4340 template <
typename M>
4341 inline internal::MatcherAsPredicate<M>
Matches(M matcher) {
4342 return internal::MatcherAsPredicate<M>(matcher);
4346 template <
typename T,
typename M>
4353 template <
typename T,
typename M>
4355 M matcher,
const T&
value, MatchResultListener* listener) {
4356 return SafeMatcherCast<const T&>(matcher).MatchAndExplain(
value, listener);
4369 template <
typename T,
typename M>
4370 std::string DescribeMatcher(
const M& matcher,
bool negation =
false) {
4371 ::std::stringstream ss;
4372 Matcher<T> monomorphic_matcher = SafeMatcherCast<T>(matcher);
4374 monomorphic_matcher.DescribeNegationTo(&ss);
4376 monomorphic_matcher.DescribeTo(&ss);
4381 template <
typename...
Args>
4382 internal::ElementsAreMatcher<
4385 return internal::ElementsAreMatcher<
4390 template <
typename...
Args>
4391 internal::UnorderedElementsAreMatcher<
4394 return internal::UnorderedElementsAreMatcher<
4400 template <
typename...
Args>
4407 template <
typename...
Args>
4436 template <
typename Iter>
4437 inline internal::AnyOfArrayMatcher<
4440 return internal::AnyOfArrayMatcher<
4444 template <
typename Iter>
4445 inline internal::AllOfArrayMatcher<
4448 return internal::AllOfArrayMatcher<
4452 template <
typename T>
4453 inline internal::AnyOfArrayMatcher<T> AnyOfArray(
const T*
ptr,
size_t count) {
4457 template <
typename T>
4458 inline internal::AllOfArrayMatcher<T> AllOfArray(
const T*
ptr,
size_t count) {
4462 template <
typename T,
size_t N>
4463 inline internal::AnyOfArrayMatcher<T> AnyOfArray(
const T (&
array)[
N]) {
4464 return AnyOfArray(
array,
N);
4467 template <
typename T,
size_t N>
4468 inline internal::AllOfArrayMatcher<T> AllOfArray(
const T (&
array)[
N]) {
4469 return AllOfArray(
array,
N);
4472 template <
typename Container>
4473 inline internal::AnyOfArrayMatcher<typename Container::value_type> AnyOfArray(
4478 template <
typename Container>
4479 inline internal::AllOfArrayMatcher<typename Container::value_type> AllOfArray(
4484 template <
typename T>
4485 inline internal::AnyOfArrayMatcher<T> AnyOfArray(
4486 ::std::initializer_list<T> xs) {
4487 return AnyOfArray(xs.begin(), xs.end());
4490 template <
typename T>
4491 inline internal::AllOfArrayMatcher<T> AllOfArray(
4492 ::std::initializer_list<T> xs) {
4493 return AllOfArray(xs.begin(), xs.end());
4499 template <
size_t...
k,
typename InnerMatcher>
4501 InnerMatcher&& matcher) {
4503 std::forward<InnerMatcher>(matcher));
4513 template <
typename InnerMatcher>
4514 inline InnerMatcher
AllArgs(
const InnerMatcher& matcher) {
return matcher; }
4524 template <
typename ValueMatcher>
4525 inline internal::OptionalMatcher<ValueMatcher>
Optional(
4526 const ValueMatcher& value_matcher) {
4527 return internal::OptionalMatcher<ValueMatcher>(value_matcher);
4531 template <
typename T>
4532 PolymorphicMatcher<internal::any_cast_matcher::AnyCastMatcher<T> > AnyWith(
4533 const Matcher<const T&>& matcher) {
4535 internal::any_cast_matcher::AnyCastMatcher<T>(matcher));
4542 template <
typename T>
4543 PolymorphicMatcher<internal::variant_matcher::VariantMatcher<T> > VariantWith(
4544 const Matcher<const T&>& matcher) {
4546 internal::variant_matcher::VariantMatcher<T>(matcher));
4553 #define ASSERT_THAT(value, matcher) ASSERT_PRED_FORMAT1(\
4554 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
4555 #define EXPECT_THAT(value, matcher) EXPECT_PRED_FORMAT1(\
4556 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
4565 #include "gmock/internal/custom/gmock-matchers.h"
4567 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_