File indexing completed on 2025-08-07 08:19:53
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_
0039 #define GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_
0040
0041 #include <math.h>
0042 #include <algorithm>
0043 #include <iterator>
0044 #include <limits>
0045 #include <ostream> // NOLINT
0046 #include <sstream>
0047 #include <string>
0048 #include <utility>
0049 #include <vector>
0050
0051 #include "gmock/internal/gmock-internal-utils.h"
0052 #include "gmock/internal/gmock-port.h"
0053 #include "gtest/gtest.h"
0054
0055 #if GTEST_HAS_STD_INITIALIZER_LIST_
0056 # include <initializer_list> // NOLINT -- must be after gtest.h
0057 #endif
0058
0059 namespace testing {
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080 class MatchResultListener {
0081 public:
0082
0083
0084
0085 explicit MatchResultListener(::std::ostream* os) : stream_(os) {}
0086 virtual ~MatchResultListener() = 0;
0087
0088
0089
0090 template <typename T>
0091 MatchResultListener& operator<<(const T& x) {
0092 if (stream_ != NULL)
0093 *stream_ << x;
0094 return *this;
0095 }
0096
0097
0098 ::std::ostream* stream() { return stream_; }
0099
0100
0101
0102
0103
0104 bool IsInterested() const { return stream_ != NULL; }
0105
0106 private:
0107 ::std::ostream* const stream_;
0108
0109 GTEST_DISALLOW_COPY_AND_ASSIGN_(MatchResultListener);
0110 };
0111
0112 inline MatchResultListener::~MatchResultListener() {
0113 }
0114
0115
0116
0117 class MatcherDescriberInterface {
0118 public:
0119 virtual ~MatcherDescriberInterface() {}
0120
0121
0122
0123
0124
0125
0126 virtual void DescribeTo(::std::ostream* os) const = 0;
0127
0128
0129
0130
0131
0132
0133
0134 virtual void DescribeNegationTo(::std::ostream* os) const {
0135 *os << "not (";
0136 DescribeTo(os);
0137 *os << ")";
0138 }
0139 };
0140
0141
0142 template <typename T>
0143 class MatcherInterface : public MatcherDescriberInterface {
0144 public:
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176 virtual bool MatchAndExplain(T x, MatchResultListener* listener) const = 0;
0177
0178
0179
0180
0181 };
0182
0183
0184 class StringMatchResultListener : public MatchResultListener {
0185 public:
0186 StringMatchResultListener() : MatchResultListener(&ss_) {}
0187
0188
0189 internal::string str() const { return ss_.str(); }
0190
0191
0192 void Clear() { ss_.str(""); }
0193
0194 private:
0195 ::std::stringstream ss_;
0196
0197 GTEST_DISALLOW_COPY_AND_ASSIGN_(StringMatchResultListener);
0198 };
0199
0200 namespace internal {
0201
0202 struct AnyEq {
0203 template <typename A, typename B>
0204 bool operator()(const A& a, const B& b) const { return a == b; }
0205 };
0206 struct AnyNe {
0207 template <typename A, typename B>
0208 bool operator()(const A& a, const B& b) const { return a != b; }
0209 };
0210 struct AnyLt {
0211 template <typename A, typename B>
0212 bool operator()(const A& a, const B& b) const { return a < b; }
0213 };
0214 struct AnyGt {
0215 template <typename A, typename B>
0216 bool operator()(const A& a, const B& b) const { return a > b; }
0217 };
0218 struct AnyLe {
0219 template <typename A, typename B>
0220 bool operator()(const A& a, const B& b) const { return a <= b; }
0221 };
0222 struct AnyGe {
0223 template <typename A, typename B>
0224 bool operator()(const A& a, const B& b) const { return a >= b; }
0225 };
0226
0227
0228 class DummyMatchResultListener : public MatchResultListener {
0229 public:
0230 DummyMatchResultListener() : MatchResultListener(NULL) {}
0231
0232 private:
0233 GTEST_DISALLOW_COPY_AND_ASSIGN_(DummyMatchResultListener);
0234 };
0235
0236
0237
0238
0239 class StreamMatchResultListener : public MatchResultListener {
0240 public:
0241 explicit StreamMatchResultListener(::std::ostream* os)
0242 : MatchResultListener(os) {}
0243
0244 private:
0245 GTEST_DISALLOW_COPY_AND_ASSIGN_(StreamMatchResultListener);
0246 };
0247
0248
0249
0250
0251 template <typename T>
0252 class MatcherBase {
0253 public:
0254
0255
0256 bool MatchAndExplain(T x, MatchResultListener* listener) const {
0257 return impl_->MatchAndExplain(x, listener);
0258 }
0259
0260
0261 bool Matches(T x) const {
0262 DummyMatchResultListener dummy;
0263 return MatchAndExplain(x, &dummy);
0264 }
0265
0266
0267 void DescribeTo(::std::ostream* os) const { impl_->DescribeTo(os); }
0268
0269
0270 void DescribeNegationTo(::std::ostream* os) const {
0271 impl_->DescribeNegationTo(os);
0272 }
0273
0274
0275 void ExplainMatchResultTo(T x, ::std::ostream* os) const {
0276 StreamMatchResultListener listener(os);
0277 MatchAndExplain(x, &listener);
0278 }
0279
0280
0281
0282
0283 const MatcherDescriberInterface* GetDescriber() const {
0284 return impl_.get();
0285 }
0286
0287 protected:
0288 MatcherBase() {}
0289
0290
0291 explicit MatcherBase(const MatcherInterface<T>* impl)
0292 : impl_(impl) {}
0293
0294 virtual ~MatcherBase() {}
0295
0296 private:
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308 ::testing::internal::linked_ptr<const MatcherInterface<T> > impl_;
0309 };
0310
0311 }
0312
0313
0314
0315
0316
0317
0318 template <typename T>
0319 class Matcher : public internal::MatcherBase<T> {
0320 public:
0321
0322
0323
0324 explicit Matcher() {}
0325
0326
0327 explicit Matcher(const MatcherInterface<T>* impl)
0328 : internal::MatcherBase<T>(impl) {}
0329
0330
0331
0332 Matcher(T value);
0333 };
0334
0335
0336
0337
0338 template <>
0339 class GTEST_API_ Matcher<const internal::string&>
0340 : public internal::MatcherBase<const internal::string&> {
0341 public:
0342 Matcher() {}
0343
0344 explicit Matcher(const MatcherInterface<const internal::string&>* impl)
0345 : internal::MatcherBase<const internal::string&>(impl) {}
0346
0347
0348
0349 Matcher(const internal::string& s);
0350
0351
0352 Matcher(const char* s);
0353 };
0354
0355 template <>
0356 class GTEST_API_ Matcher<internal::string>
0357 : public internal::MatcherBase<internal::string> {
0358 public:
0359 Matcher() {}
0360
0361 explicit Matcher(const MatcherInterface<internal::string>* impl)
0362 : internal::MatcherBase<internal::string>(impl) {}
0363
0364
0365
0366 Matcher(const internal::string& s);
0367
0368
0369 Matcher(const char* s);
0370 };
0371
0372 #if GTEST_HAS_STRING_PIECE_
0373
0374
0375
0376 template <>
0377 class GTEST_API_ Matcher<const StringPiece&>
0378 : public internal::MatcherBase<const StringPiece&> {
0379 public:
0380 Matcher() {}
0381
0382 explicit Matcher(const MatcherInterface<const StringPiece&>* impl)
0383 : internal::MatcherBase<const StringPiece&>(impl) {}
0384
0385
0386
0387 Matcher(const internal::string& s);
0388
0389
0390 Matcher(const char* s);
0391
0392
0393 Matcher(StringPiece s);
0394 };
0395
0396 template <>
0397 class GTEST_API_ Matcher<StringPiece>
0398 : public internal::MatcherBase<StringPiece> {
0399 public:
0400 Matcher() {}
0401
0402 explicit Matcher(const MatcherInterface<StringPiece>* impl)
0403 : internal::MatcherBase<StringPiece>(impl) {}
0404
0405
0406
0407 Matcher(const internal::string& s);
0408
0409
0410 Matcher(const char* s);
0411
0412
0413 Matcher(StringPiece s);
0414 };
0415 #endif
0416
0417
0418
0419
0420
0421
0422
0423
0424
0425
0426
0427
0428
0429 template <class Impl>
0430 class PolymorphicMatcher {
0431 public:
0432 explicit PolymorphicMatcher(const Impl& an_impl) : impl_(an_impl) {}
0433
0434
0435
0436 Impl& mutable_impl() { return impl_; }
0437
0438
0439
0440 const Impl& impl() const { return impl_; }
0441
0442 template <typename T>
0443 operator Matcher<T>() const {
0444 return Matcher<T>(new MonomorphicImpl<T>(impl_));
0445 }
0446
0447 private:
0448 template <typename T>
0449 class MonomorphicImpl : public MatcherInterface<T> {
0450 public:
0451 explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {}
0452
0453 virtual void DescribeTo(::std::ostream* os) const {
0454 impl_.DescribeTo(os);
0455 }
0456
0457 virtual void DescribeNegationTo(::std::ostream* os) const {
0458 impl_.DescribeNegationTo(os);
0459 }
0460
0461 virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
0462 return impl_.MatchAndExplain(x, listener);
0463 }
0464
0465 private:
0466 const Impl impl_;
0467
0468 GTEST_DISALLOW_ASSIGN_(MonomorphicImpl);
0469 };
0470
0471 Impl impl_;
0472
0473 GTEST_DISALLOW_ASSIGN_(PolymorphicMatcher);
0474 };
0475
0476
0477
0478
0479
0480
0481
0482
0483 template <typename T>
0484 inline Matcher<T> MakeMatcher(const MatcherInterface<T>* impl) {
0485 return Matcher<T>(impl);
0486 }
0487
0488
0489
0490
0491
0492
0493
0494
0495 template <class Impl>
0496 inline PolymorphicMatcher<Impl> MakePolymorphicMatcher(const Impl& impl) {
0497 return PolymorphicMatcher<Impl>(impl);
0498 }
0499
0500
0501
0502 namespace internal {
0503
0504
0505
0506
0507
0508
0509
0510
0511
0512
0513
0514 template <typename T, typename M>
0515 class MatcherCastImpl {
0516 public:
0517 static Matcher<T> Cast(const M& polymorphic_matcher_or_value) {
0518
0519
0520
0521
0522
0523
0524
0525
0526
0527
0528
0529
0530
0531 return CastImpl(
0532 polymorphic_matcher_or_value,
0533 BooleanConstant<
0534 internal::ImplicitlyConvertible<M, Matcher<T> >::value>());
0535 }
0536
0537 private:
0538 static Matcher<T> CastImpl(const M& value, BooleanConstant<false>) {
0539
0540
0541
0542 return Matcher<T>(ImplicitCast_<T>(value));
0543 }
0544
0545 static Matcher<T> CastImpl(const M& polymorphic_matcher_or_value,
0546 BooleanConstant<true>) {
0547
0548
0549
0550
0551
0552
0553
0554
0555 return polymorphic_matcher_or_value;
0556 }
0557 };
0558
0559
0560
0561
0562 template <typename T, typename U>
0563 class MatcherCastImpl<T, Matcher<U> > {
0564 public:
0565 static Matcher<T> Cast(const Matcher<U>& source_matcher) {
0566 return Matcher<T>(new Impl(source_matcher));
0567 }
0568
0569 private:
0570 class Impl : public MatcherInterface<T> {
0571 public:
0572 explicit Impl(const Matcher<U>& source_matcher)
0573 : source_matcher_(source_matcher) {}
0574
0575
0576 virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
0577 return source_matcher_.MatchAndExplain(static_cast<U>(x), listener);
0578 }
0579
0580 virtual void DescribeTo(::std::ostream* os) const {
0581 source_matcher_.DescribeTo(os);
0582 }
0583
0584 virtual void DescribeNegationTo(::std::ostream* os) const {
0585 source_matcher_.DescribeNegationTo(os);
0586 }
0587
0588 private:
0589 const Matcher<U> source_matcher_;
0590
0591 GTEST_DISALLOW_ASSIGN_(Impl);
0592 };
0593 };
0594
0595
0596
0597 template <typename T>
0598 class MatcherCastImpl<T, Matcher<T> > {
0599 public:
0600 static Matcher<T> Cast(const Matcher<T>& matcher) { return matcher; }
0601 };
0602
0603 }
0604
0605
0606
0607
0608
0609 template <typename T, typename M>
0610 inline Matcher<T> MatcherCast(const M& matcher) {
0611 return internal::MatcherCastImpl<T, M>::Cast(matcher);
0612 }
0613
0614
0615
0616
0617
0618
0619
0620
0621 template <typename T>
0622 class SafeMatcherCastImpl {
0623 public:
0624
0625
0626 template <typename M>
0627 static inline Matcher<T> Cast(const M& polymorphic_matcher_or_value) {
0628 return internal::MatcherCastImpl<T, M>::Cast(polymorphic_matcher_or_value);
0629 }
0630
0631
0632
0633
0634
0635
0636
0637
0638
0639
0640 template <typename U>
0641 static inline Matcher<T> Cast(const Matcher<U>& matcher) {
0642
0643 GTEST_COMPILE_ASSERT_((internal::ImplicitlyConvertible<T, U>::value),
0644 T_must_be_implicitly_convertible_to_U);
0645
0646
0647 GTEST_COMPILE_ASSERT_(
0648 internal::is_reference<T>::value || !internal::is_reference<U>::value,
0649 cannot_convert_non_referentce_arg_to_reference);
0650
0651
0652 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(T) RawT;
0653 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(U) RawU;
0654 const bool kTIsOther = GMOCK_KIND_OF_(RawT) == internal::kOther;
0655 const bool kUIsOther = GMOCK_KIND_OF_(RawU) == internal::kOther;
0656 GTEST_COMPILE_ASSERT_(
0657 kTIsOther || kUIsOther ||
0658 (internal::LosslessArithmeticConvertible<RawT, RawU>::value),
0659 conversion_of_arithmetic_types_must_be_lossless);
0660 return MatcherCast<T>(matcher);
0661 }
0662 };
0663
0664 template <typename T, typename M>
0665 inline Matcher<T> SafeMatcherCast(const M& polymorphic_matcher) {
0666 return SafeMatcherCastImpl<T>::Cast(polymorphic_matcher);
0667 }
0668
0669
0670 template <typename T>
0671 Matcher<T> A();
0672
0673
0674
0675 namespace internal {
0676
0677
0678 inline void PrintIfNotEmpty(const internal::string& explanation,
0679 ::std::ostream* os) {
0680 if (explanation != "" && os != NULL) {
0681 *os << ", " << explanation;
0682 }
0683 }
0684
0685
0686
0687
0688 inline bool IsReadableTypeName(const string& type_name) {
0689
0690
0691 return (type_name.length() <= 20 ||
0692 type_name.find_first_of("<(") == string::npos);
0693 }
0694
0695
0696
0697
0698
0699
0700 template <typename Value, typename T>
0701 bool MatchPrintAndExplain(Value& value, const Matcher<T>& matcher,
0702 MatchResultListener* listener) {
0703 if (!listener->IsInterested()) {
0704
0705
0706 return matcher.Matches(value);
0707 }
0708
0709 StringMatchResultListener inner_listener;
0710 const bool match = matcher.MatchAndExplain(value, &inner_listener);
0711
0712 UniversalPrint(value, listener->stream());
0713 #if GTEST_HAS_RTTI
0714 const string& type_name = GetTypeName<Value>();
0715 if (IsReadableTypeName(type_name))
0716 *listener->stream() << " (of type " << type_name << ")";
0717 #endif
0718 PrintIfNotEmpty(inner_listener.str(), listener->stream());
0719
0720 return match;
0721 }
0722
0723
0724
0725 template <size_t N>
0726 class TuplePrefix {
0727 public:
0728
0729
0730
0731 template <typename MatcherTuple, typename ValueTuple>
0732 static bool Matches(const MatcherTuple& matcher_tuple,
0733 const ValueTuple& value_tuple) {
0734 return TuplePrefix<N - 1>::Matches(matcher_tuple, value_tuple)
0735 && get<N - 1>(matcher_tuple).Matches(get<N - 1>(value_tuple));
0736 }
0737
0738
0739
0740
0741
0742 template <typename MatcherTuple, typename ValueTuple>
0743 static void ExplainMatchFailuresTo(const MatcherTuple& matchers,
0744 const ValueTuple& values,
0745 ::std::ostream* os) {
0746
0747 TuplePrefix<N - 1>::ExplainMatchFailuresTo(matchers, values, os);
0748
0749
0750
0751 typename tuple_element<N - 1, MatcherTuple>::type matcher =
0752 get<N - 1>(matchers);
0753 typedef typename tuple_element<N - 1, ValueTuple>::type Value;
0754 Value value = get<N - 1>(values);
0755 StringMatchResultListener listener;
0756 if (!matcher.MatchAndExplain(value, &listener)) {
0757
0758
0759 *os << " Expected arg #" << N - 1 << ": ";
0760 get<N - 1>(matchers).DescribeTo(os);
0761 *os << "\n Actual: ";
0762
0763
0764
0765
0766
0767 internal::UniversalPrint(value, os);
0768 PrintIfNotEmpty(listener.str(), os);
0769 *os << "\n";
0770 }
0771 }
0772 };
0773
0774
0775 template <>
0776 class TuplePrefix<0> {
0777 public:
0778 template <typename MatcherTuple, typename ValueTuple>
0779 static bool Matches(const MatcherTuple& ,
0780 const ValueTuple& ) {
0781 return true;
0782 }
0783
0784 template <typename MatcherTuple, typename ValueTuple>
0785 static void ExplainMatchFailuresTo(const MatcherTuple& ,
0786 const ValueTuple& ,
0787 ::std::ostream* ) {}
0788 };
0789
0790
0791
0792
0793
0794
0795 template <typename MatcherTuple, typename ValueTuple>
0796 bool TupleMatches(const MatcherTuple& matcher_tuple,
0797 const ValueTuple& value_tuple) {
0798
0799
0800 GTEST_COMPILE_ASSERT_(tuple_size<MatcherTuple>::value ==
0801 tuple_size<ValueTuple>::value,
0802 matcher_and_value_have_different_numbers_of_fields);
0803 return TuplePrefix<tuple_size<ValueTuple>::value>::
0804 Matches(matcher_tuple, value_tuple);
0805 }
0806
0807
0808
0809 template <typename MatcherTuple, typename ValueTuple>
0810 void ExplainMatchFailureTupleTo(const MatcherTuple& matchers,
0811 const ValueTuple& values,
0812 ::std::ostream* os) {
0813 TuplePrefix<tuple_size<MatcherTuple>::value>::ExplainMatchFailuresTo(
0814 matchers, values, os);
0815 }
0816
0817
0818
0819
0820
0821 template <typename Tuple, typename Func, typename OutIter>
0822 class TransformTupleValuesHelper {
0823 private:
0824 typedef ::testing::tuple_size<Tuple> TupleSize;
0825
0826 public:
0827
0828
0829 static OutIter Run(Func f, const Tuple& t, OutIter out) {
0830 return IterateOverTuple<Tuple, TupleSize::value>()(f, t, out);
0831 }
0832
0833 private:
0834 template <typename Tup, size_t kRemainingSize>
0835 struct IterateOverTuple {
0836 OutIter operator() (Func f, const Tup& t, OutIter out) const {
0837 *out++ = f(::testing::get<TupleSize::value - kRemainingSize>(t));
0838 return IterateOverTuple<Tup, kRemainingSize - 1>()(f, t, out);
0839 }
0840 };
0841 template <typename Tup>
0842 struct IterateOverTuple<Tup, 0> {
0843 OutIter operator() (Func , const Tup& , OutIter out) const {
0844 return out;
0845 }
0846 };
0847 };
0848
0849
0850
0851
0852 template <typename Tuple, typename Func, typename OutIter>
0853 OutIter TransformTupleValues(Func f, const Tuple& t, OutIter out) {
0854 return TransformTupleValuesHelper<Tuple, Func, OutIter>::Run(f, t, out);
0855 }
0856
0857
0858 template <typename T>
0859 class AnyMatcherImpl : public MatcherInterface<T> {
0860 public:
0861 virtual bool MatchAndExplain(
0862 T , MatchResultListener* ) const { return true; }
0863 virtual void DescribeTo(::std::ostream* os) const { *os << "is anything"; }
0864 virtual void DescribeNegationTo(::std::ostream* os) const {
0865
0866
0867
0868 *os << "never matches";
0869 }
0870 };
0871
0872
0873
0874
0875
0876 class AnythingMatcher {
0877 public:
0878 template <typename T>
0879 operator Matcher<T>() const { return A<T>(); }
0880 };
0881
0882
0883
0884
0885
0886
0887
0888
0889
0890
0891
0892 template <typename D, typename Rhs, typename Op>
0893 class ComparisonBase {
0894 public:
0895 explicit ComparisonBase(const Rhs& rhs) : rhs_(rhs) {}
0896 template <typename Lhs>
0897 operator Matcher<Lhs>() const {
0898 return MakeMatcher(new Impl<Lhs>(rhs_));
0899 }
0900
0901 private:
0902 template <typename Lhs>
0903 class Impl : public MatcherInterface<Lhs> {
0904 public:
0905 explicit Impl(const Rhs& rhs) : rhs_(rhs) {}
0906 virtual bool MatchAndExplain(
0907 Lhs lhs, MatchResultListener* ) const {
0908 return Op()(lhs, rhs_);
0909 }
0910 virtual void DescribeTo(::std::ostream* os) const {
0911 *os << D::Desc() << " ";
0912 UniversalPrint(rhs_, os);
0913 }
0914 virtual void DescribeNegationTo(::std::ostream* os) const {
0915 *os << D::NegatedDesc() << " ";
0916 UniversalPrint(rhs_, os);
0917 }
0918 private:
0919 Rhs rhs_;
0920 GTEST_DISALLOW_ASSIGN_(Impl);
0921 };
0922 Rhs rhs_;
0923 GTEST_DISALLOW_ASSIGN_(ComparisonBase);
0924 };
0925
0926 template <typename Rhs>
0927 class EqMatcher : public ComparisonBase<EqMatcher<Rhs>, Rhs, AnyEq> {
0928 public:
0929 explicit EqMatcher(const Rhs& rhs)
0930 : ComparisonBase<EqMatcher<Rhs>, Rhs, AnyEq>(rhs) { }
0931 static const char* Desc() { return "is equal to"; }
0932 static const char* NegatedDesc() { return "isn't equal to"; }
0933 };
0934 template <typename Rhs>
0935 class NeMatcher : public ComparisonBase<NeMatcher<Rhs>, Rhs, AnyNe> {
0936 public:
0937 explicit NeMatcher(const Rhs& rhs)
0938 : ComparisonBase<NeMatcher<Rhs>, Rhs, AnyNe>(rhs) { }
0939 static const char* Desc() { return "isn't equal to"; }
0940 static const char* NegatedDesc() { return "is equal to"; }
0941 };
0942 template <typename Rhs>
0943 class LtMatcher : public ComparisonBase<LtMatcher<Rhs>, Rhs, AnyLt> {
0944 public:
0945 explicit LtMatcher(const Rhs& rhs)
0946 : ComparisonBase<LtMatcher<Rhs>, Rhs, AnyLt>(rhs) { }
0947 static const char* Desc() { return "is <"; }
0948 static const char* NegatedDesc() { return "isn't <"; }
0949 };
0950 template <typename Rhs>
0951 class GtMatcher : public ComparisonBase<GtMatcher<Rhs>, Rhs, AnyGt> {
0952 public:
0953 explicit GtMatcher(const Rhs& rhs)
0954 : ComparisonBase<GtMatcher<Rhs>, Rhs, AnyGt>(rhs) { }
0955 static const char* Desc() { return "is >"; }
0956 static const char* NegatedDesc() { return "isn't >"; }
0957 };
0958 template <typename Rhs>
0959 class LeMatcher : public ComparisonBase<LeMatcher<Rhs>, Rhs, AnyLe> {
0960 public:
0961 explicit LeMatcher(const Rhs& rhs)
0962 : ComparisonBase<LeMatcher<Rhs>, Rhs, AnyLe>(rhs) { }
0963 static const char* Desc() { return "is <="; }
0964 static const char* NegatedDesc() { return "isn't <="; }
0965 };
0966 template <typename Rhs>
0967 class GeMatcher : public ComparisonBase<GeMatcher<Rhs>, Rhs, AnyGe> {
0968 public:
0969 explicit GeMatcher(const Rhs& rhs)
0970 : ComparisonBase<GeMatcher<Rhs>, Rhs, AnyGe>(rhs) { }
0971 static const char* Desc() { return "is >="; }
0972 static const char* NegatedDesc() { return "isn't >="; }
0973 };
0974
0975
0976
0977 class IsNullMatcher {
0978 public:
0979 template <typename Pointer>
0980 bool MatchAndExplain(const Pointer& p,
0981 MatchResultListener* ) const {
0982 #if GTEST_LANG_CXX11
0983 return p == nullptr;
0984 #else
0985 return GetRawPointer(p) == NULL;
0986 #endif
0987 }
0988
0989 void DescribeTo(::std::ostream* os) const { *os << "is NULL"; }
0990 void DescribeNegationTo(::std::ostream* os) const {
0991 *os << "isn't NULL";
0992 }
0993 };
0994
0995
0996
0997 class NotNullMatcher {
0998 public:
0999 template <typename Pointer>
1000 bool MatchAndExplain(const Pointer& p,
1001 MatchResultListener* ) const {
1002 #if GTEST_LANG_CXX11
1003 return p != nullptr;
1004 #else
1005 return GetRawPointer(p) != NULL;
1006 #endif
1007 }
1008
1009 void DescribeTo(::std::ostream* os) const { *os << "isn't NULL"; }
1010 void DescribeNegationTo(::std::ostream* os) const {
1011 *os << "is NULL";
1012 }
1013 };
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028 template <typename T>
1029 class RefMatcher;
1030
1031 template <typename T>
1032 class RefMatcher<T&> {
1033
1034
1035
1036
1037
1038 public:
1039
1040
1041
1042 explicit RefMatcher(T& x) : object_(x) {}
1043
1044 template <typename Super>
1045 operator Matcher<Super&>() const {
1046
1047
1048
1049
1050
1051 return MakeMatcher(new Impl<Super>(object_));
1052 }
1053
1054 private:
1055 template <typename Super>
1056 class Impl : public MatcherInterface<Super&> {
1057 public:
1058 explicit Impl(Super& x) : object_(x) {}
1059
1060
1061
1062 virtual bool MatchAndExplain(
1063 Super& x, MatchResultListener* listener) const {
1064 *listener << "which is located @" << static_cast<const void*>(&x);
1065 return &x == &object_;
1066 }
1067
1068 virtual void DescribeTo(::std::ostream* os) const {
1069 *os << "references the variable ";
1070 UniversalPrinter<Super&>::Print(object_, os);
1071 }
1072
1073 virtual void DescribeNegationTo(::std::ostream* os) const {
1074 *os << "does not reference the variable ";
1075 UniversalPrinter<Super&>::Print(object_, os);
1076 }
1077
1078 private:
1079 const Super& object_;
1080
1081 GTEST_DISALLOW_ASSIGN_(Impl);
1082 };
1083
1084 T& object_;
1085
1086 GTEST_DISALLOW_ASSIGN_(RefMatcher);
1087 };
1088
1089
1090 inline bool CaseInsensitiveCStringEquals(const char* lhs, const char* rhs) {
1091 return String::CaseInsensitiveCStringEquals(lhs, rhs);
1092 }
1093
1094 inline bool CaseInsensitiveCStringEquals(const wchar_t* lhs,
1095 const wchar_t* rhs) {
1096 return String::CaseInsensitiveWideCStringEquals(lhs, rhs);
1097 }
1098
1099
1100
1101 template <typename StringType>
1102 bool CaseInsensitiveStringEquals(const StringType& s1,
1103 const StringType& s2) {
1104
1105 if (!CaseInsensitiveCStringEquals(s1.c_str(), s2.c_str())) {
1106 return false;
1107 }
1108
1109
1110 const typename StringType::value_type nul = 0;
1111 const size_t i1 = s1.find(nul), i2 = s2.find(nul);
1112
1113
1114 if (i1 == StringType::npos || i2 == StringType::npos) {
1115 return i1 == i2;
1116 }
1117
1118
1119 return CaseInsensitiveStringEquals(s1.substr(i1 + 1), s2.substr(i2 + 1));
1120 }
1121
1122
1123
1124
1125 template <typename StringType>
1126 class StrEqualityMatcher {
1127 public:
1128 StrEqualityMatcher(const StringType& str, bool expect_eq,
1129 bool case_sensitive)
1130 : string_(str), expect_eq_(expect_eq), case_sensitive_(case_sensitive) {}
1131
1132
1133
1134
1135
1136
1137 template <typename CharType>
1138 bool MatchAndExplain(CharType* s, MatchResultListener* listener) const {
1139 if (s == NULL) {
1140 return !expect_eq_;
1141 }
1142 return MatchAndExplain(StringType(s), listener);
1143 }
1144
1145
1146
1147
1148
1149 template <typename MatcheeStringType>
1150 bool MatchAndExplain(const MatcheeStringType& s,
1151 MatchResultListener* ) const {
1152 const StringType& s2(s);
1153 const bool eq = case_sensitive_ ? s2 == string_ :
1154 CaseInsensitiveStringEquals(s2, string_);
1155 return expect_eq_ == eq;
1156 }
1157
1158 void DescribeTo(::std::ostream* os) const {
1159 DescribeToHelper(expect_eq_, os);
1160 }
1161
1162 void DescribeNegationTo(::std::ostream* os) const {
1163 DescribeToHelper(!expect_eq_, os);
1164 }
1165
1166 private:
1167 void DescribeToHelper(bool expect_eq, ::std::ostream* os) const {
1168 *os << (expect_eq ? "is " : "isn't ");
1169 *os << "equal to ";
1170 if (!case_sensitive_) {
1171 *os << "(ignoring case) ";
1172 }
1173 UniversalPrint(string_, os);
1174 }
1175
1176 const StringType string_;
1177 const bool expect_eq_;
1178 const bool case_sensitive_;
1179
1180 GTEST_DISALLOW_ASSIGN_(StrEqualityMatcher);
1181 };
1182
1183
1184
1185
1186 template <typename StringType>
1187 class HasSubstrMatcher {
1188 public:
1189 explicit HasSubstrMatcher(const StringType& substring)
1190 : substring_(substring) {}
1191
1192
1193
1194
1195
1196
1197 template <typename CharType>
1198 bool MatchAndExplain(CharType* s, MatchResultListener* listener) const {
1199 return s != NULL && MatchAndExplain(StringType(s), listener);
1200 }
1201
1202
1203
1204
1205
1206 template <typename MatcheeStringType>
1207 bool MatchAndExplain(const MatcheeStringType& s,
1208 MatchResultListener* ) const {
1209 const StringType& s2(s);
1210 return s2.find(substring_) != StringType::npos;
1211 }
1212
1213
1214 void DescribeTo(::std::ostream* os) const {
1215 *os << "has substring ";
1216 UniversalPrint(substring_, os);
1217 }
1218
1219 void DescribeNegationTo(::std::ostream* os) const {
1220 *os << "has no substring ";
1221 UniversalPrint(substring_, os);
1222 }
1223
1224 private:
1225 const StringType substring_;
1226
1227 GTEST_DISALLOW_ASSIGN_(HasSubstrMatcher);
1228 };
1229
1230
1231
1232
1233 template <typename StringType>
1234 class StartsWithMatcher {
1235 public:
1236 explicit StartsWithMatcher(const StringType& prefix) : prefix_(prefix) {
1237 }
1238
1239
1240
1241
1242
1243
1244 template <typename CharType>
1245 bool MatchAndExplain(CharType* s, MatchResultListener* listener) const {
1246 return s != NULL && MatchAndExplain(StringType(s), listener);
1247 }
1248
1249
1250
1251
1252
1253 template <typename MatcheeStringType>
1254 bool MatchAndExplain(const MatcheeStringType& s,
1255 MatchResultListener* ) const {
1256 const StringType& s2(s);
1257 return s2.length() >= prefix_.length() &&
1258 s2.substr(0, prefix_.length()) == prefix_;
1259 }
1260
1261 void DescribeTo(::std::ostream* os) const {
1262 *os << "starts with ";
1263 UniversalPrint(prefix_, os);
1264 }
1265
1266 void DescribeNegationTo(::std::ostream* os) const {
1267 *os << "doesn't start with ";
1268 UniversalPrint(prefix_, os);
1269 }
1270
1271 private:
1272 const StringType prefix_;
1273
1274 GTEST_DISALLOW_ASSIGN_(StartsWithMatcher);
1275 };
1276
1277
1278
1279
1280 template <typename StringType>
1281 class EndsWithMatcher {
1282 public:
1283 explicit EndsWithMatcher(const StringType& suffix) : suffix_(suffix) {}
1284
1285
1286
1287
1288
1289
1290 template <typename CharType>
1291 bool MatchAndExplain(CharType* s, MatchResultListener* listener) const {
1292 return s != NULL && MatchAndExplain(StringType(s), listener);
1293 }
1294
1295
1296
1297
1298
1299 template <typename MatcheeStringType>
1300 bool MatchAndExplain(const MatcheeStringType& s,
1301 MatchResultListener* ) const {
1302 const StringType& s2(s);
1303 return s2.length() >= suffix_.length() &&
1304 s2.substr(s2.length() - suffix_.length()) == suffix_;
1305 }
1306
1307 void DescribeTo(::std::ostream* os) const {
1308 *os << "ends with ";
1309 UniversalPrint(suffix_, os);
1310 }
1311
1312 void DescribeNegationTo(::std::ostream* os) const {
1313 *os << "doesn't end with ";
1314 UniversalPrint(suffix_, os);
1315 }
1316
1317 private:
1318 const StringType suffix_;
1319
1320 GTEST_DISALLOW_ASSIGN_(EndsWithMatcher);
1321 };
1322
1323
1324
1325
1326 class MatchesRegexMatcher {
1327 public:
1328 MatchesRegexMatcher(const RE* regex, bool full_match)
1329 : regex_(regex), full_match_(full_match) {}
1330
1331
1332
1333
1334
1335
1336 template <typename CharType>
1337 bool MatchAndExplain(CharType* s, MatchResultListener* listener) const {
1338 return s != NULL && MatchAndExplain(internal::string(s), listener);
1339 }
1340
1341
1342
1343
1344
1345 template <class MatcheeStringType>
1346 bool MatchAndExplain(const MatcheeStringType& s,
1347 MatchResultListener* ) const {
1348 const internal::string& s2(s);
1349 return full_match_ ? RE::FullMatch(s2, *regex_) :
1350 RE::PartialMatch(s2, *regex_);
1351 }
1352
1353 void DescribeTo(::std::ostream* os) const {
1354 *os << (full_match_ ? "matches" : "contains")
1355 << " regular expression ";
1356 UniversalPrinter<internal::string>::Print(regex_->pattern(), os);
1357 }
1358
1359 void DescribeNegationTo(::std::ostream* os) const {
1360 *os << "doesn't " << (full_match_ ? "match" : "contain")
1361 << " regular expression ";
1362 UniversalPrinter<internal::string>::Print(regex_->pattern(), os);
1363 }
1364
1365 private:
1366 const internal::linked_ptr<const RE> regex_;
1367 const bool full_match_;
1368
1369 GTEST_DISALLOW_ASSIGN_(MatchesRegexMatcher);
1370 };
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380 template <typename D, typename Op>
1381 class PairMatchBase {
1382 public:
1383 template <typename T1, typename T2>
1384 operator Matcher< ::testing::tuple<T1, T2> >() const {
1385 return MakeMatcher(new Impl< ::testing::tuple<T1, T2> >);
1386 }
1387 template <typename T1, typename T2>
1388 operator Matcher<const ::testing::tuple<T1, T2>&>() const {
1389 return MakeMatcher(new Impl<const ::testing::tuple<T1, T2>&>);
1390 }
1391
1392 private:
1393 static ::std::ostream& GetDesc(::std::ostream& os) {
1394 return os << D::Desc();
1395 }
1396
1397 template <typename Tuple>
1398 class Impl : public MatcherInterface<Tuple> {
1399 public:
1400 virtual bool MatchAndExplain(
1401 Tuple args,
1402 MatchResultListener* ) const {
1403 return Op()(::testing::get<0>(args), ::testing::get<1>(args));
1404 }
1405 virtual void DescribeTo(::std::ostream* os) const {
1406 *os << "are " << GetDesc;
1407 }
1408 virtual void DescribeNegationTo(::std::ostream* os) const {
1409 *os << "aren't " << GetDesc;
1410 }
1411 };
1412 };
1413
1414 class Eq2Matcher : public PairMatchBase<Eq2Matcher, AnyEq> {
1415 public:
1416 static const char* Desc() { return "an equal pair"; }
1417 };
1418 class Ne2Matcher : public PairMatchBase<Ne2Matcher, AnyNe> {
1419 public:
1420 static const char* Desc() { return "an unequal pair"; }
1421 };
1422 class Lt2Matcher : public PairMatchBase<Lt2Matcher, AnyLt> {
1423 public:
1424 static const char* Desc() { return "a pair where the first < the second"; }
1425 };
1426 class Gt2Matcher : public PairMatchBase<Gt2Matcher, AnyGt> {
1427 public:
1428 static const char* Desc() { return "a pair where the first > the second"; }
1429 };
1430 class Le2Matcher : public PairMatchBase<Le2Matcher, AnyLe> {
1431 public:
1432 static const char* Desc() { return "a pair where the first <= the second"; }
1433 };
1434 class Ge2Matcher : public PairMatchBase<Ge2Matcher, AnyGe> {
1435 public:
1436 static const char* Desc() { return "a pair where the first >= the second"; }
1437 };
1438
1439
1440
1441
1442
1443 template <typename T>
1444 class NotMatcherImpl : public MatcherInterface<T> {
1445 public:
1446 explicit NotMatcherImpl(const Matcher<T>& matcher)
1447 : matcher_(matcher) {}
1448
1449 virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
1450 return !matcher_.MatchAndExplain(x, listener);
1451 }
1452
1453 virtual void DescribeTo(::std::ostream* os) const {
1454 matcher_.DescribeNegationTo(os);
1455 }
1456
1457 virtual void DescribeNegationTo(::std::ostream* os) const {
1458 matcher_.DescribeTo(os);
1459 }
1460
1461 private:
1462 const Matcher<T> matcher_;
1463
1464 GTEST_DISALLOW_ASSIGN_(NotMatcherImpl);
1465 };
1466
1467
1468
1469 template <typename InnerMatcher>
1470 class NotMatcher {
1471 public:
1472 explicit NotMatcher(InnerMatcher matcher) : matcher_(matcher) {}
1473
1474
1475
1476 template <typename T>
1477 operator Matcher<T>() const {
1478 return Matcher<T>(new NotMatcherImpl<T>(SafeMatcherCast<T>(matcher_)));
1479 }
1480
1481 private:
1482 InnerMatcher matcher_;
1483
1484 GTEST_DISALLOW_ASSIGN_(NotMatcher);
1485 };
1486
1487
1488
1489
1490
1491 template <typename T>
1492 class BothOfMatcherImpl : public MatcherInterface<T> {
1493 public:
1494 BothOfMatcherImpl(const Matcher<T>& matcher1, const Matcher<T>& matcher2)
1495 : matcher1_(matcher1), matcher2_(matcher2) {}
1496
1497 virtual void DescribeTo(::std::ostream* os) const {
1498 *os << "(";
1499 matcher1_.DescribeTo(os);
1500 *os << ") and (";
1501 matcher2_.DescribeTo(os);
1502 *os << ")";
1503 }
1504
1505 virtual void DescribeNegationTo(::std::ostream* os) const {
1506 *os << "(";
1507 matcher1_.DescribeNegationTo(os);
1508 *os << ") or (";
1509 matcher2_.DescribeNegationTo(os);
1510 *os << ")";
1511 }
1512
1513 virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
1514
1515
1516 StringMatchResultListener listener1;
1517 if (!matcher1_.MatchAndExplain(x, &listener1)) {
1518 *listener << listener1.str();
1519 return false;
1520 }
1521
1522 StringMatchResultListener listener2;
1523 if (!matcher2_.MatchAndExplain(x, &listener2)) {
1524 *listener << listener2.str();
1525 return false;
1526 }
1527
1528
1529 const internal::string s1 = listener1.str();
1530 const internal::string s2 = listener2.str();
1531
1532 if (s1 == "") {
1533 *listener << s2;
1534 } else {
1535 *listener << s1;
1536 if (s2 != "") {
1537 *listener << ", and " << s2;
1538 }
1539 }
1540 return true;
1541 }
1542
1543 private:
1544 const Matcher<T> matcher1_;
1545 const Matcher<T> matcher2_;
1546
1547 GTEST_DISALLOW_ASSIGN_(BothOfMatcherImpl);
1548 };
1549
1550 #if GTEST_LANG_CXX11
1551
1552
1553
1554
1555
1556
1557
1558 template <int kSize, typename Head, typename... Tail>
1559 struct MatcherList {
1560 typedef MatcherList<kSize - 1, Tail...> MatcherListTail;
1561 typedef ::std::pair<Head, typename MatcherListTail::ListType> ListType;
1562
1563
1564
1565
1566
1567 static ListType BuildList(const Head& matcher, const Tail&... tail) {
1568 return ListType(matcher, MatcherListTail::BuildList(tail...));
1569 }
1570
1571
1572
1573
1574
1575 template <typename T, template <typename > class CombiningMatcher>
1576 static Matcher<T> CreateMatcher(const ListType& matchers) {
1577 return Matcher<T>(new CombiningMatcher<T>(
1578 SafeMatcherCast<T>(matchers.first),
1579 MatcherListTail::template CreateMatcher<T, CombiningMatcher>(
1580 matchers.second)));
1581 }
1582 };
1583
1584
1585
1586 template <typename Matcher1, typename Matcher2>
1587 struct MatcherList<2, Matcher1, Matcher2> {
1588 typedef ::std::pair<Matcher1, Matcher2> ListType;
1589
1590 static ListType BuildList(const Matcher1& matcher1,
1591 const Matcher2& matcher2) {
1592 return ::std::pair<Matcher1, Matcher2>(matcher1, matcher2);
1593 }
1594
1595 template <typename T, template <typename > class CombiningMatcher>
1596 static Matcher<T> CreateMatcher(const ListType& matchers) {
1597 return Matcher<T>(new CombiningMatcher<T>(
1598 SafeMatcherCast<T>(matchers.first),
1599 SafeMatcherCast<T>(matchers.second)));
1600 }
1601 };
1602
1603
1604
1605
1606
1607 template <template <typename T> class CombiningMatcher, typename... Args>
1608 class VariadicMatcher {
1609 public:
1610 VariadicMatcher(const Args&... matchers)
1611 : matchers_(MatcherListType::BuildList(matchers...)) {}
1612
1613
1614
1615
1616 template <typename T>
1617 operator Matcher<T>() const {
1618 return MatcherListType::template CreateMatcher<T, CombiningMatcher>(
1619 matchers_);
1620 }
1621
1622 private:
1623 typedef MatcherList<sizeof...(Args), Args...> MatcherListType;
1624
1625 const typename MatcherListType::ListType matchers_;
1626
1627 GTEST_DISALLOW_ASSIGN_(VariadicMatcher);
1628 };
1629
1630 template <typename... Args>
1631 using AllOfMatcher = VariadicMatcher<BothOfMatcherImpl, Args...>;
1632
1633 #endif
1634
1635
1636
1637 template <typename Matcher1, typename Matcher2>
1638 class BothOfMatcher {
1639 public:
1640 BothOfMatcher(Matcher1 matcher1, Matcher2 matcher2)
1641 : matcher1_(matcher1), matcher2_(matcher2) {}
1642
1643
1644
1645
1646 template <typename T>
1647 operator Matcher<T>() const {
1648 return Matcher<T>(new BothOfMatcherImpl<T>(SafeMatcherCast<T>(matcher1_),
1649 SafeMatcherCast<T>(matcher2_)));
1650 }
1651
1652 private:
1653 Matcher1 matcher1_;
1654 Matcher2 matcher2_;
1655
1656 GTEST_DISALLOW_ASSIGN_(BothOfMatcher);
1657 };
1658
1659
1660
1661
1662
1663 template <typename T>
1664 class EitherOfMatcherImpl : public MatcherInterface<T> {
1665 public:
1666 EitherOfMatcherImpl(const Matcher<T>& matcher1, const Matcher<T>& matcher2)
1667 : matcher1_(matcher1), matcher2_(matcher2) {}
1668
1669 virtual void DescribeTo(::std::ostream* os) const {
1670 *os << "(";
1671 matcher1_.DescribeTo(os);
1672 *os << ") or (";
1673 matcher2_.DescribeTo(os);
1674 *os << ")";
1675 }
1676
1677 virtual void DescribeNegationTo(::std::ostream* os) const {
1678 *os << "(";
1679 matcher1_.DescribeNegationTo(os);
1680 *os << ") and (";
1681 matcher2_.DescribeNegationTo(os);
1682 *os << ")";
1683 }
1684
1685 virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
1686
1687
1688 StringMatchResultListener listener1;
1689 if (matcher1_.MatchAndExplain(x, &listener1)) {
1690 *listener << listener1.str();
1691 return true;
1692 }
1693
1694 StringMatchResultListener listener2;
1695 if (matcher2_.MatchAndExplain(x, &listener2)) {
1696 *listener << listener2.str();
1697 return true;
1698 }
1699
1700
1701 const internal::string s1 = listener1.str();
1702 const internal::string s2 = listener2.str();
1703
1704 if (s1 == "") {
1705 *listener << s2;
1706 } else {
1707 *listener << s1;
1708 if (s2 != "") {
1709 *listener << ", and " << s2;
1710 }
1711 }
1712 return false;
1713 }
1714
1715 private:
1716 const Matcher<T> matcher1_;
1717 const Matcher<T> matcher2_;
1718
1719 GTEST_DISALLOW_ASSIGN_(EitherOfMatcherImpl);
1720 };
1721
1722 #if GTEST_LANG_CXX11
1723
1724 template <typename... Args>
1725 using AnyOfMatcher = VariadicMatcher<EitherOfMatcherImpl, Args...>;
1726
1727 #endif
1728
1729
1730
1731
1732 template <typename Matcher1, typename Matcher2>
1733 class EitherOfMatcher {
1734 public:
1735 EitherOfMatcher(Matcher1 matcher1, Matcher2 matcher2)
1736 : matcher1_(matcher1), matcher2_(matcher2) {}
1737
1738
1739
1740
1741 template <typename T>
1742 operator Matcher<T>() const {
1743 return Matcher<T>(new EitherOfMatcherImpl<T>(
1744 SafeMatcherCast<T>(matcher1_), SafeMatcherCast<T>(matcher2_)));
1745 }
1746
1747 private:
1748 Matcher1 matcher1_;
1749 Matcher2 matcher2_;
1750
1751 GTEST_DISALLOW_ASSIGN_(EitherOfMatcher);
1752 };
1753
1754
1755
1756 template <typename Predicate>
1757 class TrulyMatcher {
1758 public:
1759 explicit TrulyMatcher(Predicate pred) : predicate_(pred) {}
1760
1761
1762
1763
1764
1765 template <typename T>
1766 bool MatchAndExplain(T& x,
1767 MatchResultListener* ) const {
1768
1769
1770
1771
1772
1773
1774 if (predicate_(x))
1775 return true;
1776 return false;
1777 }
1778
1779 void DescribeTo(::std::ostream* os) const {
1780 *os << "satisfies the given predicate";
1781 }
1782
1783 void DescribeNegationTo(::std::ostream* os) const {
1784 *os << "doesn't satisfy the given predicate";
1785 }
1786
1787 private:
1788 Predicate predicate_;
1789
1790 GTEST_DISALLOW_ASSIGN_(TrulyMatcher);
1791 };
1792
1793
1794
1795 template <typename M>
1796 class MatcherAsPredicate {
1797 public:
1798 explicit MatcherAsPredicate(M matcher) : matcher_(matcher) {}
1799
1800
1801
1802
1803
1804
1805
1806 template <typename T>
1807 bool operator()(const T& x) const {
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822 return MatcherCast<const T&>(matcher_).Matches(x);
1823 }
1824
1825 private:
1826 M matcher_;
1827
1828 GTEST_DISALLOW_ASSIGN_(MatcherAsPredicate);
1829 };
1830
1831
1832
1833 template <typename M>
1834 class PredicateFormatterFromMatcher {
1835 public:
1836 explicit PredicateFormatterFromMatcher(M m) : matcher_(internal::move(m)) {}
1837
1838
1839
1840
1841 template <typename T>
1842 AssertionResult operator()(const char* value_text, const T& x) const {
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854 const Matcher<const T&> matcher = SafeMatcherCast<const T&>(matcher_);
1855 StringMatchResultListener listener;
1856 if (MatchPrintAndExplain(x, matcher, &listener))
1857 return AssertionSuccess();
1858
1859 ::std::stringstream ss;
1860 ss << "Value of: " << value_text << "\n"
1861 << "Expected: ";
1862 matcher.DescribeTo(&ss);
1863 ss << "\n Actual: " << listener.str();
1864 return AssertionFailure() << ss.str();
1865 }
1866
1867 private:
1868 const M matcher_;
1869
1870 GTEST_DISALLOW_ASSIGN_(PredicateFormatterFromMatcher);
1871 };
1872
1873
1874
1875
1876
1877 template <typename M>
1878 inline PredicateFormatterFromMatcher<M>
1879 MakePredicateFormatterFromMatcher(M matcher) {
1880 return PredicateFormatterFromMatcher<M>(internal::move(matcher));
1881 }
1882
1883
1884
1885
1886
1887 template <typename FloatType>
1888 class FloatingEqMatcher {
1889 public:
1890
1891
1892
1893
1894
1895
1896 FloatingEqMatcher(FloatType expected, bool nan_eq_nan) :
1897 expected_(expected), nan_eq_nan_(nan_eq_nan), max_abs_error_(-1) {
1898 }
1899
1900
1901
1902
1903 FloatingEqMatcher(FloatType expected, bool nan_eq_nan,
1904 FloatType max_abs_error)
1905 : expected_(expected),
1906 nan_eq_nan_(nan_eq_nan),
1907 max_abs_error_(max_abs_error) {
1908 GTEST_CHECK_(max_abs_error >= 0)
1909 << ", where max_abs_error is" << max_abs_error;
1910 }
1911
1912
1913 template <typename T>
1914 class Impl : public MatcherInterface<T> {
1915 public:
1916 Impl(FloatType expected, bool nan_eq_nan, FloatType max_abs_error)
1917 : expected_(expected),
1918 nan_eq_nan_(nan_eq_nan),
1919 max_abs_error_(max_abs_error) {}
1920
1921 virtual bool MatchAndExplain(T value,
1922 MatchResultListener* listener) const {
1923 const FloatingPoint<FloatType> actual(value), expected(expected_);
1924
1925
1926 if (actual.is_nan() || expected.is_nan()) {
1927 if (actual.is_nan() && expected.is_nan()) {
1928 return nan_eq_nan_;
1929 }
1930
1931 return false;
1932 }
1933 if (HasMaxAbsError()) {
1934
1935
1936
1937
1938 if (value == expected_) {
1939 return true;
1940 }
1941
1942 const FloatType diff = value - expected_;
1943 if (fabs(diff) <= max_abs_error_) {
1944 return true;
1945 }
1946
1947 if (listener->IsInterested()) {
1948 *listener << "which is " << diff << " from " << expected_;
1949 }
1950 return false;
1951 } else {
1952 return actual.AlmostEquals(expected);
1953 }
1954 }
1955
1956 virtual void DescribeTo(::std::ostream* os) const {
1957
1958
1959
1960 const ::std::streamsize old_precision = os->precision(
1961 ::std::numeric_limits<FloatType>::digits10 + 2);
1962 if (FloatingPoint<FloatType>(expected_).is_nan()) {
1963 if (nan_eq_nan_) {
1964 *os << "is NaN";
1965 } else {
1966 *os << "never matches";
1967 }
1968 } else {
1969 *os << "is approximately " << expected_;
1970 if (HasMaxAbsError()) {
1971 *os << " (absolute error <= " << max_abs_error_ << ")";
1972 }
1973 }
1974 os->precision(old_precision);
1975 }
1976
1977 virtual void DescribeNegationTo(::std::ostream* os) const {
1978
1979 const ::std::streamsize old_precision = os->precision(
1980 ::std::numeric_limits<FloatType>::digits10 + 2);
1981 if (FloatingPoint<FloatType>(expected_).is_nan()) {
1982 if (nan_eq_nan_) {
1983 *os << "isn't NaN";
1984 } else {
1985 *os << "is anything";
1986 }
1987 } else {
1988 *os << "isn't approximately " << expected_;
1989 if (HasMaxAbsError()) {
1990 *os << " (absolute error > " << max_abs_error_ << ")";
1991 }
1992 }
1993
1994 os->precision(old_precision);
1995 }
1996
1997 private:
1998 bool HasMaxAbsError() const {
1999 return max_abs_error_ >= 0;
2000 }
2001
2002 const FloatType expected_;
2003 const bool nan_eq_nan_;
2004
2005 const FloatType max_abs_error_;
2006
2007 GTEST_DISALLOW_ASSIGN_(Impl);
2008 };
2009
2010
2011
2012
2013
2014
2015
2016 operator Matcher<FloatType>() const {
2017 return MakeMatcher(
2018 new Impl<FloatType>(expected_, nan_eq_nan_, max_abs_error_));
2019 }
2020
2021 operator Matcher<const FloatType&>() const {
2022 return MakeMatcher(
2023 new Impl<const FloatType&>(expected_, nan_eq_nan_, max_abs_error_));
2024 }
2025
2026 operator Matcher<FloatType&>() const {
2027 return MakeMatcher(
2028 new Impl<FloatType&>(expected_, nan_eq_nan_, max_abs_error_));
2029 }
2030
2031 private:
2032 const FloatType expected_;
2033 const bool nan_eq_nan_;
2034
2035 const FloatType max_abs_error_;
2036
2037 GTEST_DISALLOW_ASSIGN_(FloatingEqMatcher);
2038 };
2039
2040
2041
2042 template <typename InnerMatcher>
2043 class PointeeMatcher {
2044 public:
2045 explicit PointeeMatcher(const InnerMatcher& matcher) : matcher_(matcher) {}
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055 template <typename Pointer>
2056 operator Matcher<Pointer>() const {
2057 return MakeMatcher(new Impl<Pointer>(matcher_));
2058 }
2059
2060 private:
2061
2062 template <typename Pointer>
2063 class Impl : public MatcherInterface<Pointer> {
2064 public:
2065 typedef typename PointeeOf<GTEST_REMOVE_CONST_(
2066 GTEST_REMOVE_REFERENCE_(Pointer))>::type Pointee;
2067
2068 explicit Impl(const InnerMatcher& matcher)
2069 : matcher_(MatcherCast<const Pointee&>(matcher)) {}
2070
2071 virtual void DescribeTo(::std::ostream* os) const {
2072 *os << "points to a value that ";
2073 matcher_.DescribeTo(os);
2074 }
2075
2076 virtual void DescribeNegationTo(::std::ostream* os) const {
2077 *os << "does not point to a value that ";
2078 matcher_.DescribeTo(os);
2079 }
2080
2081 virtual bool MatchAndExplain(Pointer pointer,
2082 MatchResultListener* listener) const {
2083 if (GetRawPointer(pointer) == NULL)
2084 return false;
2085
2086 *listener << "which points to ";
2087 return MatchPrintAndExplain(*pointer, matcher_, listener);
2088 }
2089
2090 private:
2091 const Matcher<const Pointee&> matcher_;
2092
2093 GTEST_DISALLOW_ASSIGN_(Impl);
2094 };
2095
2096 const InnerMatcher matcher_;
2097
2098 GTEST_DISALLOW_ASSIGN_(PointeeMatcher);
2099 };
2100
2101
2102
2103
2104
2105
2106
2107 template <typename To>
2108 class WhenDynamicCastToMatcherBase {
2109 public:
2110 explicit WhenDynamicCastToMatcherBase(const Matcher<To>& matcher)
2111 : matcher_(matcher) {}
2112
2113 void DescribeTo(::std::ostream* os) const {
2114 GetCastTypeDescription(os);
2115 matcher_.DescribeTo(os);
2116 }
2117
2118 void DescribeNegationTo(::std::ostream* os) const {
2119 GetCastTypeDescription(os);
2120 matcher_.DescribeNegationTo(os);
2121 }
2122
2123 protected:
2124 const Matcher<To> matcher_;
2125
2126 static string GetToName() {
2127 #if GTEST_HAS_RTTI
2128 return GetTypeName<To>();
2129 #else
2130 return "the target type";
2131 #endif
2132 }
2133
2134 private:
2135 static void GetCastTypeDescription(::std::ostream* os) {
2136 *os << "when dynamic_cast to " << GetToName() << ", ";
2137 }
2138
2139 GTEST_DISALLOW_ASSIGN_(WhenDynamicCastToMatcherBase);
2140 };
2141
2142
2143
2144 template <typename To>
2145 class WhenDynamicCastToMatcher : public WhenDynamicCastToMatcherBase<To> {
2146 public:
2147 explicit WhenDynamicCastToMatcher(const Matcher<To>& matcher)
2148 : WhenDynamicCastToMatcherBase<To>(matcher) {}
2149
2150 template <typename From>
2151 bool MatchAndExplain(From from, MatchResultListener* listener) const {
2152
2153 To to = dynamic_cast<To>(from);
2154 return MatchPrintAndExplain(to, this->matcher_, listener);
2155 }
2156 };
2157
2158
2159
2160 template <typename To>
2161 class WhenDynamicCastToMatcher<To&> : public WhenDynamicCastToMatcherBase<To&> {
2162 public:
2163 explicit WhenDynamicCastToMatcher(const Matcher<To&>& matcher)
2164 : WhenDynamicCastToMatcherBase<To&>(matcher) {}
2165
2166 template <typename From>
2167 bool MatchAndExplain(From& from, MatchResultListener* listener) const {
2168
2169 To* to = dynamic_cast<To*>(&from);
2170 if (to == NULL) {
2171 *listener << "which cannot be dynamic_cast to " << this->GetToName();
2172 return false;
2173 }
2174 return MatchPrintAndExplain(*to, this->matcher_, listener);
2175 }
2176 };
2177
2178
2179
2180 template <typename Class, typename FieldType>
2181 class FieldMatcher {
2182 public:
2183 FieldMatcher(FieldType Class::*field,
2184 const Matcher<const FieldType&>& matcher)
2185 : field_(field), matcher_(matcher) {}
2186
2187 void DescribeTo(::std::ostream* os) const {
2188 *os << "is an object whose given field ";
2189 matcher_.DescribeTo(os);
2190 }
2191
2192 void DescribeNegationTo(::std::ostream* os) const {
2193 *os << "is an object whose given field ";
2194 matcher_.DescribeNegationTo(os);
2195 }
2196
2197 template <typename T>
2198 bool MatchAndExplain(const T& value, MatchResultListener* listener) const {
2199 return MatchAndExplainImpl(
2200 typename ::testing::internal::
2201 is_pointer<GTEST_REMOVE_CONST_(T)>::type(),
2202 value, listener);
2203 }
2204
2205 private:
2206
2207
2208
2209 bool MatchAndExplainImpl(false_type , const Class& obj,
2210 MatchResultListener* listener) const {
2211 *listener << "whose given field is ";
2212 return MatchPrintAndExplain(obj.*field_, matcher_, listener);
2213 }
2214
2215 bool MatchAndExplainImpl(true_type , const Class* p,
2216 MatchResultListener* listener) const {
2217 if (p == NULL)
2218 return false;
2219
2220 *listener << "which points to an object ";
2221
2222
2223
2224 return MatchAndExplainImpl(false_type(), *p, listener);
2225 }
2226
2227 const FieldType Class::*field_;
2228 const Matcher<const FieldType&> matcher_;
2229
2230 GTEST_DISALLOW_ASSIGN_(FieldMatcher);
2231 };
2232
2233
2234
2235 template <typename Class, typename PropertyType>
2236 class PropertyMatcher {
2237 public:
2238
2239
2240
2241
2242 typedef GTEST_REFERENCE_TO_CONST_(PropertyType) RefToConstProperty;
2243
2244 PropertyMatcher(PropertyType (Class::*property)() const,
2245 const Matcher<RefToConstProperty>& matcher)
2246 : property_(property), matcher_(matcher) {}
2247
2248 void DescribeTo(::std::ostream* os) const {
2249 *os << "is an object whose given property ";
2250 matcher_.DescribeTo(os);
2251 }
2252
2253 void DescribeNegationTo(::std::ostream* os) const {
2254 *os << "is an object whose given property ";
2255 matcher_.DescribeNegationTo(os);
2256 }
2257
2258 template <typename T>
2259 bool MatchAndExplain(const T&value, MatchResultListener* listener) const {
2260 return MatchAndExplainImpl(
2261 typename ::testing::internal::
2262 is_pointer<GTEST_REMOVE_CONST_(T)>::type(),
2263 value, listener);
2264 }
2265
2266 private:
2267
2268
2269
2270 bool MatchAndExplainImpl(false_type , const Class& obj,
2271 MatchResultListener* listener) const {
2272 *listener << "whose given property is ";
2273
2274
2275 #if defined(_PREFAST_ ) && _MSC_VER == 1800
2276
2277
2278 posix::Abort();
2279 return false;
2280 #else
2281 RefToConstProperty result = (obj.*property_)();
2282 return MatchPrintAndExplain(result, matcher_, listener);
2283 #endif
2284 }
2285
2286 bool MatchAndExplainImpl(true_type , const Class* p,
2287 MatchResultListener* listener) const {
2288 if (p == NULL)
2289 return false;
2290
2291 *listener << "which points to an object ";
2292
2293
2294
2295 return MatchAndExplainImpl(false_type(), *p, listener);
2296 }
2297
2298 PropertyType (Class::*property_)() const;
2299 const Matcher<RefToConstProperty> matcher_;
2300
2301 GTEST_DISALLOW_ASSIGN_(PropertyMatcher);
2302 };
2303
2304
2305
2306
2307
2308 template <typename Functor>
2309 struct CallableTraits {
2310 typedef typename Functor::result_type ResultType;
2311 typedef Functor StorageType;
2312
2313 static void CheckIsValid(Functor ) {}
2314 template <typename T>
2315 static ResultType Invoke(Functor f, T arg) { return f(arg); }
2316 };
2317
2318
2319 template <typename ArgType, typename ResType>
2320 struct CallableTraits<ResType(*)(ArgType)> {
2321 typedef ResType ResultType;
2322 typedef ResType(*StorageType)(ArgType);
2323
2324 static void CheckIsValid(ResType(*f)(ArgType)) {
2325 GTEST_CHECK_(f != NULL)
2326 << "NULL function pointer is passed into ResultOf().";
2327 }
2328 template <typename T>
2329 static ResType Invoke(ResType(*f)(ArgType), T arg) {
2330 return (*f)(arg);
2331 }
2332 };
2333
2334
2335
2336 template <typename Callable>
2337 class ResultOfMatcher {
2338 public:
2339 typedef typename CallableTraits<Callable>::ResultType ResultType;
2340
2341 ResultOfMatcher(Callable callable, const Matcher<ResultType>& matcher)
2342 : callable_(callable), matcher_(matcher) {
2343 CallableTraits<Callable>::CheckIsValid(callable_);
2344 }
2345
2346 template <typename T>
2347 operator Matcher<T>() const {
2348 return Matcher<T>(new Impl<T>(callable_, matcher_));
2349 }
2350
2351 private:
2352 typedef typename CallableTraits<Callable>::StorageType CallableStorageType;
2353
2354 template <typename T>
2355 class Impl : public MatcherInterface<T> {
2356 public:
2357 Impl(CallableStorageType callable, const Matcher<ResultType>& matcher)
2358 : callable_(callable), matcher_(matcher) {}
2359
2360 virtual void DescribeTo(::std::ostream* os) const {
2361 *os << "is mapped by the given callable to a value that ";
2362 matcher_.DescribeTo(os);
2363 }
2364
2365 virtual void DescribeNegationTo(::std::ostream* os) const {
2366 *os << "is mapped by the given callable to a value that ";
2367 matcher_.DescribeNegationTo(os);
2368 }
2369
2370 virtual bool MatchAndExplain(T obj, MatchResultListener* listener) const {
2371 *listener << "which is mapped by the given callable to ";
2372
2373
2374 ResultType result =
2375 CallableTraits<Callable>::template Invoke<T>(callable_, obj);
2376 return MatchPrintAndExplain(result, matcher_, listener);
2377 }
2378
2379 private:
2380
2381
2382
2383
2384
2385 mutable CallableStorageType callable_;
2386 const Matcher<ResultType> matcher_;
2387
2388 GTEST_DISALLOW_ASSIGN_(Impl);
2389 };
2390
2391 const CallableStorageType callable_;
2392 const Matcher<ResultType> matcher_;
2393
2394 GTEST_DISALLOW_ASSIGN_(ResultOfMatcher);
2395 };
2396
2397
2398 template <typename SizeMatcher>
2399 class SizeIsMatcher {
2400 public:
2401 explicit SizeIsMatcher(const SizeMatcher& size_matcher)
2402 : size_matcher_(size_matcher) {
2403 }
2404
2405 template <typename Container>
2406 operator Matcher<Container>() const {
2407 return MakeMatcher(new Impl<Container>(size_matcher_));
2408 }
2409
2410 template <typename Container>
2411 class Impl : public MatcherInterface<Container> {
2412 public:
2413 typedef internal::StlContainerView<
2414 GTEST_REMOVE_REFERENCE_AND_CONST_(Container)> ContainerView;
2415 typedef typename ContainerView::type::size_type SizeType;
2416 explicit Impl(const SizeMatcher& size_matcher)
2417 : size_matcher_(MatcherCast<SizeType>(size_matcher)) {}
2418
2419 virtual void DescribeTo(::std::ostream* os) const {
2420 *os << "size ";
2421 size_matcher_.DescribeTo(os);
2422 }
2423 virtual void DescribeNegationTo(::std::ostream* os) const {
2424 *os << "size ";
2425 size_matcher_.DescribeNegationTo(os);
2426 }
2427
2428 virtual bool MatchAndExplain(Container container,
2429 MatchResultListener* listener) const {
2430 SizeType size = container.size();
2431 StringMatchResultListener size_listener;
2432 const bool result = size_matcher_.MatchAndExplain(size, &size_listener);
2433 *listener
2434 << "whose size " << size << (result ? " matches" : " doesn't match");
2435 PrintIfNotEmpty(size_listener.str(), listener->stream());
2436 return result;
2437 }
2438
2439 private:
2440 const Matcher<SizeType> size_matcher_;
2441 GTEST_DISALLOW_ASSIGN_(Impl);
2442 };
2443
2444 private:
2445 const SizeMatcher size_matcher_;
2446 GTEST_DISALLOW_ASSIGN_(SizeIsMatcher);
2447 };
2448
2449
2450
2451 template <typename DistanceMatcher>
2452 class BeginEndDistanceIsMatcher {
2453 public:
2454 explicit BeginEndDistanceIsMatcher(const DistanceMatcher& distance_matcher)
2455 : distance_matcher_(distance_matcher) {}
2456
2457 template <typename Container>
2458 operator Matcher<Container>() const {
2459 return MakeMatcher(new Impl<Container>(distance_matcher_));
2460 }
2461
2462 template <typename Container>
2463 class Impl : public MatcherInterface<Container> {
2464 public:
2465 typedef internal::StlContainerView<
2466 GTEST_REMOVE_REFERENCE_AND_CONST_(Container)> ContainerView;
2467 typedef typename std::iterator_traits<
2468 typename ContainerView::type::const_iterator>::difference_type
2469 DistanceType;
2470 explicit Impl(const DistanceMatcher& distance_matcher)
2471 : distance_matcher_(MatcherCast<DistanceType>(distance_matcher)) {}
2472
2473 virtual void DescribeTo(::std::ostream* os) const {
2474 *os << "distance between begin() and end() ";
2475 distance_matcher_.DescribeTo(os);
2476 }
2477 virtual void DescribeNegationTo(::std::ostream* os) const {
2478 *os << "distance between begin() and end() ";
2479 distance_matcher_.DescribeNegationTo(os);
2480 }
2481
2482 virtual bool MatchAndExplain(Container container,
2483 MatchResultListener* listener) const {
2484 #if GTEST_HAS_STD_BEGIN_AND_END_
2485 using std::begin;
2486 using std::end;
2487 DistanceType distance = std::distance(begin(container), end(container));
2488 #else
2489 DistanceType distance = std::distance(container.begin(), container.end());
2490 #endif
2491 StringMatchResultListener distance_listener;
2492 const bool result =
2493 distance_matcher_.MatchAndExplain(distance, &distance_listener);
2494 *listener << "whose distance between begin() and end() " << distance
2495 << (result ? " matches" : " doesn't match");
2496 PrintIfNotEmpty(distance_listener.str(), listener->stream());
2497 return result;
2498 }
2499
2500 private:
2501 const Matcher<DistanceType> distance_matcher_;
2502 GTEST_DISALLOW_ASSIGN_(Impl);
2503 };
2504
2505 private:
2506 const DistanceMatcher distance_matcher_;
2507 GTEST_DISALLOW_ASSIGN_(BeginEndDistanceIsMatcher);
2508 };
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520 template <typename Container>
2521 class ContainerEqMatcher {
2522 public:
2523 typedef internal::StlContainerView<Container> View;
2524 typedef typename View::type StlContainer;
2525 typedef typename View::const_reference StlContainerReference;
2526
2527
2528
2529 explicit ContainerEqMatcher(const Container& expected)
2530 : expected_(View::Copy(expected)) {
2531
2532
2533 (void)testing::StaticAssertTypeEq<Container,
2534 GTEST_REMOVE_REFERENCE_AND_CONST_(Container)>();
2535 }
2536
2537 void DescribeTo(::std::ostream* os) const {
2538 *os << "equals ";
2539 UniversalPrint(expected_, os);
2540 }
2541 void DescribeNegationTo(::std::ostream* os) const {
2542 *os << "does not equal ";
2543 UniversalPrint(expected_, os);
2544 }
2545
2546 template <typename LhsContainer>
2547 bool MatchAndExplain(const LhsContainer& lhs,
2548 MatchResultListener* listener) const {
2549
2550
2551 typedef internal::StlContainerView<GTEST_REMOVE_CONST_(LhsContainer)>
2552 LhsView;
2553 typedef typename LhsView::type LhsStlContainer;
2554 StlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2555 if (lhs_stl_container == expected_)
2556 return true;
2557
2558 ::std::ostream* const os = listener->stream();
2559 if (os != NULL) {
2560
2561 bool printed_header = false;
2562 for (typename LhsStlContainer::const_iterator it =
2563 lhs_stl_container.begin();
2564 it != lhs_stl_container.end(); ++it) {
2565 if (internal::ArrayAwareFind(expected_.begin(), expected_.end(), *it) ==
2566 expected_.end()) {
2567 if (printed_header) {
2568 *os << ", ";
2569 } else {
2570 *os << "which has these unexpected elements: ";
2571 printed_header = true;
2572 }
2573 UniversalPrint(*it, os);
2574 }
2575 }
2576
2577
2578 bool printed_header2 = false;
2579 for (typename StlContainer::const_iterator it = expected_.begin();
2580 it != expected_.end(); ++it) {
2581 if (internal::ArrayAwareFind(
2582 lhs_stl_container.begin(), lhs_stl_container.end(), *it) ==
2583 lhs_stl_container.end()) {
2584 if (printed_header2) {
2585 *os << ", ";
2586 } else {
2587 *os << (printed_header ? ",\nand" : "which")
2588 << " doesn't have these expected elements: ";
2589 printed_header2 = true;
2590 }
2591 UniversalPrint(*it, os);
2592 }
2593 }
2594 }
2595
2596 return false;
2597 }
2598
2599 private:
2600 const StlContainer expected_;
2601
2602 GTEST_DISALLOW_ASSIGN_(ContainerEqMatcher);
2603 };
2604
2605
2606 struct LessComparator {
2607 template <typename T, typename U>
2608 bool operator()(const T& lhs, const U& rhs) const { return lhs < rhs; }
2609 };
2610
2611
2612 template <typename Comparator, typename ContainerMatcher>
2613 class WhenSortedByMatcher {
2614 public:
2615 WhenSortedByMatcher(const Comparator& comparator,
2616 const ContainerMatcher& matcher)
2617 : comparator_(comparator), matcher_(matcher) {}
2618
2619 template <typename LhsContainer>
2620 operator Matcher<LhsContainer>() const {
2621 return MakeMatcher(new Impl<LhsContainer>(comparator_, matcher_));
2622 }
2623
2624 template <typename LhsContainer>
2625 class Impl : public MatcherInterface<LhsContainer> {
2626 public:
2627 typedef internal::StlContainerView<
2628 GTEST_REMOVE_REFERENCE_AND_CONST_(LhsContainer)> LhsView;
2629 typedef typename LhsView::type LhsStlContainer;
2630 typedef typename LhsView::const_reference LhsStlContainerReference;
2631
2632
2633 typedef typename RemoveConstFromKey<
2634 typename LhsStlContainer::value_type>::type LhsValue;
2635
2636 Impl(const Comparator& comparator, const ContainerMatcher& matcher)
2637 : comparator_(comparator), matcher_(matcher) {}
2638
2639 virtual void DescribeTo(::std::ostream* os) const {
2640 *os << "(when sorted) ";
2641 matcher_.DescribeTo(os);
2642 }
2643
2644 virtual void DescribeNegationTo(::std::ostream* os) const {
2645 *os << "(when sorted) ";
2646 matcher_.DescribeNegationTo(os);
2647 }
2648
2649 virtual bool MatchAndExplain(LhsContainer lhs,
2650 MatchResultListener* listener) const {
2651 LhsStlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2652 ::std::vector<LhsValue> sorted_container(lhs_stl_container.begin(),
2653 lhs_stl_container.end());
2654 ::std::sort(
2655 sorted_container.begin(), sorted_container.end(), comparator_);
2656
2657 if (!listener->IsInterested()) {
2658
2659
2660 return matcher_.Matches(sorted_container);
2661 }
2662
2663 *listener << "which is ";
2664 UniversalPrint(sorted_container, listener->stream());
2665 *listener << " when sorted";
2666
2667 StringMatchResultListener inner_listener;
2668 const bool match = matcher_.MatchAndExplain(sorted_container,
2669 &inner_listener);
2670 PrintIfNotEmpty(inner_listener.str(), listener->stream());
2671 return match;
2672 }
2673
2674 private:
2675 const Comparator comparator_;
2676 const Matcher<const ::std::vector<LhsValue>&> matcher_;
2677
2678 GTEST_DISALLOW_COPY_AND_ASSIGN_(Impl);
2679 };
2680
2681 private:
2682 const Comparator comparator_;
2683 const ContainerMatcher matcher_;
2684
2685 GTEST_DISALLOW_ASSIGN_(WhenSortedByMatcher);
2686 };
2687
2688
2689
2690
2691
2692 template <typename TupleMatcher, typename RhsContainer>
2693 class PointwiseMatcher {
2694 public:
2695 typedef internal::StlContainerView<RhsContainer> RhsView;
2696 typedef typename RhsView::type RhsStlContainer;
2697 typedef typename RhsStlContainer::value_type RhsValue;
2698
2699
2700
2701 PointwiseMatcher(const TupleMatcher& tuple_matcher, const RhsContainer& rhs)
2702 : tuple_matcher_(tuple_matcher), rhs_(RhsView::Copy(rhs)) {
2703
2704
2705 (void)testing::StaticAssertTypeEq<RhsContainer,
2706 GTEST_REMOVE_REFERENCE_AND_CONST_(RhsContainer)>();
2707 }
2708
2709 template <typename LhsContainer>
2710 operator Matcher<LhsContainer>() const {
2711 return MakeMatcher(new Impl<LhsContainer>(tuple_matcher_, rhs_));
2712 }
2713
2714 template <typename LhsContainer>
2715 class Impl : public MatcherInterface<LhsContainer> {
2716 public:
2717 typedef internal::StlContainerView<
2718 GTEST_REMOVE_REFERENCE_AND_CONST_(LhsContainer)> LhsView;
2719 typedef typename LhsView::type LhsStlContainer;
2720 typedef typename LhsView::const_reference LhsStlContainerReference;
2721 typedef typename LhsStlContainer::value_type LhsValue;
2722
2723
2724
2725
2726 typedef ::testing::tuple<const LhsValue&, const RhsValue&> InnerMatcherArg;
2727
2728 Impl(const TupleMatcher& tuple_matcher, const RhsStlContainer& rhs)
2729
2730 : mono_tuple_matcher_(SafeMatcherCast<InnerMatcherArg>(tuple_matcher)),
2731 rhs_(rhs) {}
2732
2733 virtual void DescribeTo(::std::ostream* os) const {
2734 *os << "contains " << rhs_.size()
2735 << " values, where each value and its corresponding value in ";
2736 UniversalPrinter<RhsStlContainer>::Print(rhs_, os);
2737 *os << " ";
2738 mono_tuple_matcher_.DescribeTo(os);
2739 }
2740 virtual void DescribeNegationTo(::std::ostream* os) const {
2741 *os << "doesn't contain exactly " << rhs_.size()
2742 << " values, or contains a value x at some index i"
2743 << " where x and the i-th value of ";
2744 UniversalPrint(rhs_, os);
2745 *os << " ";
2746 mono_tuple_matcher_.DescribeNegationTo(os);
2747 }
2748
2749 virtual bool MatchAndExplain(LhsContainer lhs,
2750 MatchResultListener* listener) const {
2751 LhsStlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2752 const size_t actual_size = lhs_stl_container.size();
2753 if (actual_size != rhs_.size()) {
2754 *listener << "which contains " << actual_size << " values";
2755 return false;
2756 }
2757
2758 typename LhsStlContainer::const_iterator left = lhs_stl_container.begin();
2759 typename RhsStlContainer::const_iterator right = rhs_.begin();
2760 for (size_t i = 0; i != actual_size; ++i, ++left, ++right) {
2761 const InnerMatcherArg value_pair(*left, *right);
2762
2763 if (listener->IsInterested()) {
2764 StringMatchResultListener inner_listener;
2765 if (!mono_tuple_matcher_.MatchAndExplain(
2766 value_pair, &inner_listener)) {
2767 *listener << "where the value pair (";
2768 UniversalPrint(*left, listener->stream());
2769 *listener << ", ";
2770 UniversalPrint(*right, listener->stream());
2771 *listener << ") at index #" << i << " don't match";
2772 PrintIfNotEmpty(inner_listener.str(), listener->stream());
2773 return false;
2774 }
2775 } else {
2776 if (!mono_tuple_matcher_.Matches(value_pair))
2777 return false;
2778 }
2779 }
2780
2781 return true;
2782 }
2783
2784 private:
2785 const Matcher<InnerMatcherArg> mono_tuple_matcher_;
2786 const RhsStlContainer rhs_;
2787
2788 GTEST_DISALLOW_ASSIGN_(Impl);
2789 };
2790
2791 private:
2792 const TupleMatcher tuple_matcher_;
2793 const RhsStlContainer rhs_;
2794
2795 GTEST_DISALLOW_ASSIGN_(PointwiseMatcher);
2796 };
2797
2798
2799 template <typename Container>
2800 class QuantifierMatcherImpl : public MatcherInterface<Container> {
2801 public:
2802 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
2803 typedef StlContainerView<RawContainer> View;
2804 typedef typename View::type StlContainer;
2805 typedef typename View::const_reference StlContainerReference;
2806 typedef typename StlContainer::value_type Element;
2807
2808 template <typename InnerMatcher>
2809 explicit QuantifierMatcherImpl(InnerMatcher inner_matcher)
2810 : inner_matcher_(
2811 testing::SafeMatcherCast<const Element&>(inner_matcher)) {}
2812
2813
2814
2815
2816 bool MatchAndExplainImpl(bool all_elements_should_match,
2817 Container container,
2818 MatchResultListener* listener) const {
2819 StlContainerReference stl_container = View::ConstReference(container);
2820 size_t i = 0;
2821 for (typename StlContainer::const_iterator it = stl_container.begin();
2822 it != stl_container.end(); ++it, ++i) {
2823 StringMatchResultListener inner_listener;
2824 const bool matches = inner_matcher_.MatchAndExplain(*it, &inner_listener);
2825
2826 if (matches != all_elements_should_match) {
2827 *listener << "whose element #" << i
2828 << (matches ? " matches" : " doesn't match");
2829 PrintIfNotEmpty(inner_listener.str(), listener->stream());
2830 return !all_elements_should_match;
2831 }
2832 }
2833 return all_elements_should_match;
2834 }
2835
2836 protected:
2837 const Matcher<const Element&> inner_matcher_;
2838
2839 GTEST_DISALLOW_ASSIGN_(QuantifierMatcherImpl);
2840 };
2841
2842
2843
2844 template <typename Container>
2845 class ContainsMatcherImpl : public QuantifierMatcherImpl<Container> {
2846 public:
2847 template <typename InnerMatcher>
2848 explicit ContainsMatcherImpl(InnerMatcher inner_matcher)
2849 : QuantifierMatcherImpl<Container>(inner_matcher) {}
2850
2851
2852 virtual void DescribeTo(::std::ostream* os) const {
2853 *os << "contains at least one element that ";
2854 this->inner_matcher_.DescribeTo(os);
2855 }
2856
2857 virtual void DescribeNegationTo(::std::ostream* os) const {
2858 *os << "doesn't contain any element that ";
2859 this->inner_matcher_.DescribeTo(os);
2860 }
2861
2862 virtual bool MatchAndExplain(Container container,
2863 MatchResultListener* listener) const {
2864 return this->MatchAndExplainImpl(false, container, listener);
2865 }
2866
2867 private:
2868 GTEST_DISALLOW_ASSIGN_(ContainsMatcherImpl);
2869 };
2870
2871
2872
2873 template <typename Container>
2874 class EachMatcherImpl : public QuantifierMatcherImpl<Container> {
2875 public:
2876 template <typename InnerMatcher>
2877 explicit EachMatcherImpl(InnerMatcher inner_matcher)
2878 : QuantifierMatcherImpl<Container>(inner_matcher) {}
2879
2880
2881 virtual void DescribeTo(::std::ostream* os) const {
2882 *os << "only contains elements that ";
2883 this->inner_matcher_.DescribeTo(os);
2884 }
2885
2886 virtual void DescribeNegationTo(::std::ostream* os) const {
2887 *os << "contains some element that ";
2888 this->inner_matcher_.DescribeNegationTo(os);
2889 }
2890
2891 virtual bool MatchAndExplain(Container container,
2892 MatchResultListener* listener) const {
2893 return this->MatchAndExplainImpl(true, container, listener);
2894 }
2895
2896 private:
2897 GTEST_DISALLOW_ASSIGN_(EachMatcherImpl);
2898 };
2899
2900
2901 template <typename M>
2902 class ContainsMatcher {
2903 public:
2904 explicit ContainsMatcher(M m) : inner_matcher_(m) {}
2905
2906 template <typename Container>
2907 operator Matcher<Container>() const {
2908 return MakeMatcher(new ContainsMatcherImpl<Container>(inner_matcher_));
2909 }
2910
2911 private:
2912 const M inner_matcher_;
2913
2914 GTEST_DISALLOW_ASSIGN_(ContainsMatcher);
2915 };
2916
2917
2918 template <typename M>
2919 class EachMatcher {
2920 public:
2921 explicit EachMatcher(M m) : inner_matcher_(m) {}
2922
2923 template <typename Container>
2924 operator Matcher<Container>() const {
2925 return MakeMatcher(new EachMatcherImpl<Container>(inner_matcher_));
2926 }
2927
2928 private:
2929 const M inner_matcher_;
2930
2931 GTEST_DISALLOW_ASSIGN_(EachMatcher);
2932 };
2933
2934
2935
2936
2937
2938 template <typename PairType>
2939 class KeyMatcherImpl : public MatcherInterface<PairType> {
2940 public:
2941 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(PairType) RawPairType;
2942 typedef typename RawPairType::first_type KeyType;
2943
2944 template <typename InnerMatcher>
2945 explicit KeyMatcherImpl(InnerMatcher inner_matcher)
2946 : inner_matcher_(
2947 testing::SafeMatcherCast<const KeyType&>(inner_matcher)) {
2948 }
2949
2950
2951 virtual bool MatchAndExplain(PairType key_value,
2952 MatchResultListener* listener) const {
2953 StringMatchResultListener inner_listener;
2954 const bool match = inner_matcher_.MatchAndExplain(key_value.first,
2955 &inner_listener);
2956 const internal::string explanation = inner_listener.str();
2957 if (explanation != "") {
2958 *listener << "whose first field is a value " << explanation;
2959 }
2960 return match;
2961 }
2962
2963
2964 virtual void DescribeTo(::std::ostream* os) const {
2965 *os << "has a key that ";
2966 inner_matcher_.DescribeTo(os);
2967 }
2968
2969
2970 virtual void DescribeNegationTo(::std::ostream* os) const {
2971 *os << "doesn't have a key that ";
2972 inner_matcher_.DescribeTo(os);
2973 }
2974
2975 private:
2976 const Matcher<const KeyType&> inner_matcher_;
2977
2978 GTEST_DISALLOW_ASSIGN_(KeyMatcherImpl);
2979 };
2980
2981
2982 template <typename M>
2983 class KeyMatcher {
2984 public:
2985 explicit KeyMatcher(M m) : matcher_for_key_(m) {}
2986
2987 template <typename PairType>
2988 operator Matcher<PairType>() const {
2989 return MakeMatcher(new KeyMatcherImpl<PairType>(matcher_for_key_));
2990 }
2991
2992 private:
2993 const M matcher_for_key_;
2994
2995 GTEST_DISALLOW_ASSIGN_(KeyMatcher);
2996 };
2997
2998
2999
3000 template <typename PairType>
3001 class PairMatcherImpl : public MatcherInterface<PairType> {
3002 public:
3003 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(PairType) RawPairType;
3004 typedef typename RawPairType::first_type FirstType;
3005 typedef typename RawPairType::second_type SecondType;
3006
3007 template <typename FirstMatcher, typename SecondMatcher>
3008 PairMatcherImpl(FirstMatcher first_matcher, SecondMatcher second_matcher)
3009 : first_matcher_(
3010 testing::SafeMatcherCast<const FirstType&>(first_matcher)),
3011 second_matcher_(
3012 testing::SafeMatcherCast<const SecondType&>(second_matcher)) {
3013 }
3014
3015
3016 virtual void DescribeTo(::std::ostream* os) const {
3017 *os << "has a first field that ";
3018 first_matcher_.DescribeTo(os);
3019 *os << ", and has a second field that ";
3020 second_matcher_.DescribeTo(os);
3021 }
3022
3023
3024 virtual void DescribeNegationTo(::std::ostream* os) const {
3025 *os << "has a first field that ";
3026 first_matcher_.DescribeNegationTo(os);
3027 *os << ", or has a second field that ";
3028 second_matcher_.DescribeNegationTo(os);
3029 }
3030
3031
3032
3033 virtual bool MatchAndExplain(PairType a_pair,
3034 MatchResultListener* listener) const {
3035 if (!listener->IsInterested()) {
3036
3037
3038 return first_matcher_.Matches(a_pair.first) &&
3039 second_matcher_.Matches(a_pair.second);
3040 }
3041 StringMatchResultListener first_inner_listener;
3042 if (!first_matcher_.MatchAndExplain(a_pair.first,
3043 &first_inner_listener)) {
3044 *listener << "whose first field does not match";
3045 PrintIfNotEmpty(first_inner_listener.str(), listener->stream());
3046 return false;
3047 }
3048 StringMatchResultListener second_inner_listener;
3049 if (!second_matcher_.MatchAndExplain(a_pair.second,
3050 &second_inner_listener)) {
3051 *listener << "whose second field does not match";
3052 PrintIfNotEmpty(second_inner_listener.str(), listener->stream());
3053 return false;
3054 }
3055 ExplainSuccess(first_inner_listener.str(), second_inner_listener.str(),
3056 listener);
3057 return true;
3058 }
3059
3060 private:
3061 void ExplainSuccess(const internal::string& first_explanation,
3062 const internal::string& second_explanation,
3063 MatchResultListener* listener) const {
3064 *listener << "whose both fields match";
3065 if (first_explanation != "") {
3066 *listener << ", where the first field is a value " << first_explanation;
3067 }
3068 if (second_explanation != "") {
3069 *listener << ", ";
3070 if (first_explanation != "") {
3071 *listener << "and ";
3072 } else {
3073 *listener << "where ";
3074 }
3075 *listener << "the second field is a value " << second_explanation;
3076 }
3077 }
3078
3079 const Matcher<const FirstType&> first_matcher_;
3080 const Matcher<const SecondType&> second_matcher_;
3081
3082 GTEST_DISALLOW_ASSIGN_(PairMatcherImpl);
3083 };
3084
3085
3086 template <typename FirstMatcher, typename SecondMatcher>
3087 class PairMatcher {
3088 public:
3089 PairMatcher(FirstMatcher first_matcher, SecondMatcher second_matcher)
3090 : first_matcher_(first_matcher), second_matcher_(second_matcher) {}
3091
3092 template <typename PairType>
3093 operator Matcher<PairType> () const {
3094 return MakeMatcher(
3095 new PairMatcherImpl<PairType>(
3096 first_matcher_, second_matcher_));
3097 }
3098
3099 private:
3100 const FirstMatcher first_matcher_;
3101 const SecondMatcher second_matcher_;
3102
3103 GTEST_DISALLOW_ASSIGN_(PairMatcher);
3104 };
3105
3106
3107 template <typename Container>
3108 class ElementsAreMatcherImpl : public MatcherInterface<Container> {
3109 public:
3110 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
3111 typedef internal::StlContainerView<RawContainer> View;
3112 typedef typename View::type StlContainer;
3113 typedef typename View::const_reference StlContainerReference;
3114 typedef typename StlContainer::value_type Element;
3115
3116
3117
3118 template <typename InputIter>
3119 ElementsAreMatcherImpl(InputIter first, InputIter last) {
3120 while (first != last) {
3121 matchers_.push_back(MatcherCast<const Element&>(*first++));
3122 }
3123 }
3124
3125
3126 virtual void DescribeTo(::std::ostream* os) const {
3127 if (count() == 0) {
3128 *os << "is empty";
3129 } else if (count() == 1) {
3130 *os << "has 1 element that ";
3131 matchers_[0].DescribeTo(os);
3132 } else {
3133 *os << "has " << Elements(count()) << " where\n";
3134 for (size_t i = 0; i != count(); ++i) {
3135 *os << "element #" << i << " ";
3136 matchers_[i].DescribeTo(os);
3137 if (i + 1 < count()) {
3138 *os << ",\n";
3139 }
3140 }
3141 }
3142 }
3143
3144
3145 virtual void DescribeNegationTo(::std::ostream* os) const {
3146 if (count() == 0) {
3147 *os << "isn't empty";
3148 return;
3149 }
3150
3151 *os << "doesn't have " << Elements(count()) << ", or\n";
3152 for (size_t i = 0; i != count(); ++i) {
3153 *os << "element #" << i << " ";
3154 matchers_[i].DescribeNegationTo(os);
3155 if (i + 1 < count()) {
3156 *os << ", or\n";
3157 }
3158 }
3159 }
3160
3161 virtual bool MatchAndExplain(Container container,
3162 MatchResultListener* listener) const {
3163
3164
3165
3166 const bool listener_interested = listener->IsInterested();
3167
3168
3169 ::std::vector<internal::string> explanations(count());
3170 StlContainerReference stl_container = View::ConstReference(container);
3171 typename StlContainer::const_iterator it = stl_container.begin();
3172 size_t exam_pos = 0;
3173 bool mismatch_found = false;
3174
3175
3176
3177
3178 for (; it != stl_container.end() && exam_pos != count(); ++it, ++exam_pos) {
3179 bool match;
3180 if (listener_interested) {
3181 StringMatchResultListener s;
3182 match = matchers_[exam_pos].MatchAndExplain(*it, &s);
3183 explanations[exam_pos] = s.str();
3184 } else {
3185 match = matchers_[exam_pos].Matches(*it);
3186 }
3187
3188 if (!match) {
3189 mismatch_found = true;
3190 break;
3191 }
3192 }
3193
3194
3195
3196
3197
3198 size_t actual_count = exam_pos;
3199 for (; it != stl_container.end(); ++it) {
3200 ++actual_count;
3201 }
3202
3203 if (actual_count != count()) {
3204
3205
3206
3207
3208 if (listener_interested && (actual_count != 0)) {
3209 *listener << "which has " << Elements(actual_count);
3210 }
3211 return false;
3212 }
3213
3214 if (mismatch_found) {
3215
3216 if (listener_interested) {
3217 *listener << "whose element #" << exam_pos << " doesn't match";
3218 PrintIfNotEmpty(explanations[exam_pos], listener->stream());
3219 }
3220 return false;
3221 }
3222
3223
3224
3225 if (listener_interested) {
3226 bool reason_printed = false;
3227 for (size_t i = 0; i != count(); ++i) {
3228 const internal::string& s = explanations[i];
3229 if (!s.empty()) {
3230 if (reason_printed) {
3231 *listener << ",\nand ";
3232 }
3233 *listener << "whose element #" << i << " matches, " << s;
3234 reason_printed = true;
3235 }
3236 }
3237 }
3238 return true;
3239 }
3240
3241 private:
3242 static Message Elements(size_t count) {
3243 return Message() << count << (count == 1 ? " element" : " elements");
3244 }
3245
3246 size_t count() const { return matchers_.size(); }
3247
3248 ::std::vector<Matcher<const Element&> > matchers_;
3249
3250 GTEST_DISALLOW_ASSIGN_(ElementsAreMatcherImpl);
3251 };
3252
3253
3254
3255
3256
3257 class GTEST_API_ MatchMatrix {
3258 public:
3259 MatchMatrix(size_t num_elements, size_t num_matchers)
3260 : num_elements_(num_elements),
3261 num_matchers_(num_matchers),
3262 matched_(num_elements_* num_matchers_, 0) {
3263 }
3264
3265 size_t LhsSize() const { return num_elements_; }
3266 size_t RhsSize() const { return num_matchers_; }
3267 bool HasEdge(size_t ilhs, size_t irhs) const {
3268 return matched_[SpaceIndex(ilhs, irhs)] == 1;
3269 }
3270 void SetEdge(size_t ilhs, size_t irhs, bool b) {
3271 matched_[SpaceIndex(ilhs, irhs)] = b ? 1 : 0;
3272 }
3273
3274
3275
3276
3277 bool NextGraph();
3278
3279 void Randomize();
3280
3281 string DebugString() const;
3282
3283 private:
3284 size_t SpaceIndex(size_t ilhs, size_t irhs) const {
3285 return ilhs * num_matchers_ + irhs;
3286 }
3287
3288 size_t num_elements_;
3289 size_t num_matchers_;
3290
3291
3292
3293
3294 ::std::vector<char> matched_;
3295 };
3296
3297 typedef ::std::pair<size_t, size_t> ElementMatcherPair;
3298 typedef ::std::vector<ElementMatcherPair> ElementMatcherPairs;
3299
3300
3301
3302 GTEST_API_ ElementMatcherPairs
3303 FindMaxBipartiteMatching(const MatchMatrix& g);
3304
3305 GTEST_API_ bool FindPairing(const MatchMatrix& matrix,
3306 MatchResultListener* listener);
3307
3308
3309
3310
3311 class GTEST_API_ UnorderedElementsAreMatcherImplBase {
3312 protected:
3313
3314
3315
3316 typedef ::std::vector<const MatcherDescriberInterface*> MatcherDescriberVec;
3317
3318
3319 void DescribeToImpl(::std::ostream* os) const;
3320
3321
3322 void DescribeNegationToImpl(::std::ostream* os) const;
3323
3324 bool VerifyAllElementsAndMatchersAreMatched(
3325 const ::std::vector<string>& element_printouts,
3326 const MatchMatrix& matrix,
3327 MatchResultListener* listener) const;
3328
3329 MatcherDescriberVec& matcher_describers() {
3330 return matcher_describers_;
3331 }
3332
3333 static Message Elements(size_t n) {
3334 return Message() << n << " element" << (n == 1 ? "" : "s");
3335 }
3336
3337 private:
3338 MatcherDescriberVec matcher_describers_;
3339
3340 GTEST_DISALLOW_ASSIGN_(UnorderedElementsAreMatcherImplBase);
3341 };
3342
3343
3344 template <typename Container>
3345 class UnorderedElementsAreMatcherImpl
3346 : public MatcherInterface<Container>,
3347 public UnorderedElementsAreMatcherImplBase {
3348 public:
3349 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
3350 typedef internal::StlContainerView<RawContainer> View;
3351 typedef typename View::type StlContainer;
3352 typedef typename View::const_reference StlContainerReference;
3353 typedef typename StlContainer::const_iterator StlContainerConstIterator;
3354 typedef typename StlContainer::value_type Element;
3355
3356
3357
3358 template <typename InputIter>
3359 UnorderedElementsAreMatcherImpl(InputIter first, InputIter last) {
3360 for (; first != last; ++first) {
3361 matchers_.push_back(MatcherCast<const Element&>(*first));
3362 matcher_describers().push_back(matchers_.back().GetDescriber());
3363 }
3364 }
3365
3366
3367 virtual void DescribeTo(::std::ostream* os) const {
3368 return UnorderedElementsAreMatcherImplBase::DescribeToImpl(os);
3369 }
3370
3371
3372 virtual void DescribeNegationTo(::std::ostream* os) const {
3373 return UnorderedElementsAreMatcherImplBase::DescribeNegationToImpl(os);
3374 }
3375
3376 virtual bool MatchAndExplain(Container container,
3377 MatchResultListener* listener) const {
3378 StlContainerReference stl_container = View::ConstReference(container);
3379 ::std::vector<string> element_printouts;
3380 MatchMatrix matrix = AnalyzeElements(stl_container.begin(),
3381 stl_container.end(),
3382 &element_printouts,
3383 listener);
3384
3385 const size_t actual_count = matrix.LhsSize();
3386 if (actual_count == 0 && matchers_.empty()) {
3387 return true;
3388 }
3389 if (actual_count != matchers_.size()) {
3390
3391
3392
3393
3394 if (actual_count != 0 && listener->IsInterested()) {
3395 *listener << "which has " << Elements(actual_count);
3396 }
3397 return false;
3398 }
3399
3400 return VerifyAllElementsAndMatchersAreMatched(element_printouts,
3401 matrix, listener) &&
3402 FindPairing(matrix, listener);
3403 }
3404
3405 private:
3406 typedef ::std::vector<Matcher<const Element&> > MatcherVec;
3407
3408 template <typename ElementIter>
3409 MatchMatrix AnalyzeElements(ElementIter elem_first, ElementIter elem_last,
3410 ::std::vector<string>* element_printouts,
3411 MatchResultListener* listener) const {
3412 element_printouts->clear();
3413 ::std::vector<char> did_match;
3414 size_t num_elements = 0;
3415 for (; elem_first != elem_last; ++num_elements, ++elem_first) {
3416 if (listener->IsInterested()) {
3417 element_printouts->push_back(PrintToString(*elem_first));
3418 }
3419 for (size_t irhs = 0; irhs != matchers_.size(); ++irhs) {
3420 did_match.push_back(Matches(matchers_[irhs])(*elem_first));
3421 }
3422 }
3423
3424 MatchMatrix matrix(num_elements, matchers_.size());
3425 ::std::vector<char>::const_iterator did_match_iter = did_match.begin();
3426 for (size_t ilhs = 0; ilhs != num_elements; ++ilhs) {
3427 for (size_t irhs = 0; irhs != matchers_.size(); ++irhs) {
3428 matrix.SetEdge(ilhs, irhs, *did_match_iter++ != 0);
3429 }
3430 }
3431 return matrix;
3432 }
3433
3434 MatcherVec matchers_;
3435
3436 GTEST_DISALLOW_ASSIGN_(UnorderedElementsAreMatcherImpl);
3437 };
3438
3439
3440
3441 template <typename Target>
3442 struct CastAndAppendTransform {
3443 template <typename Arg>
3444 Matcher<Target> operator()(const Arg& a) const {
3445 return MatcherCast<Target>(a);
3446 }
3447 };
3448
3449
3450 template <typename MatcherTuple>
3451 class UnorderedElementsAreMatcher {
3452 public:
3453 explicit UnorderedElementsAreMatcher(const MatcherTuple& args)
3454 : matchers_(args) {}
3455
3456 template <typename Container>
3457 operator Matcher<Container>() const {
3458 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
3459 typedef typename internal::StlContainerView<RawContainer>::type View;
3460 typedef typename View::value_type Element;
3461 typedef ::std::vector<Matcher<const Element&> > MatcherVec;
3462 MatcherVec matchers;
3463 matchers.reserve(::testing::tuple_size<MatcherTuple>::value);
3464 TransformTupleValues(CastAndAppendTransform<const Element&>(), matchers_,
3465 ::std::back_inserter(matchers));
3466 return MakeMatcher(new UnorderedElementsAreMatcherImpl<Container>(
3467 matchers.begin(), matchers.end()));
3468 }
3469
3470 private:
3471 const MatcherTuple matchers_;
3472 GTEST_DISALLOW_ASSIGN_(UnorderedElementsAreMatcher);
3473 };
3474
3475
3476 template <typename MatcherTuple>
3477 class ElementsAreMatcher {
3478 public:
3479 explicit ElementsAreMatcher(const MatcherTuple& args) : matchers_(args) {}
3480
3481 template <typename Container>
3482 operator Matcher<Container>() const {
3483 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
3484 typedef typename internal::StlContainerView<RawContainer>::type View;
3485 typedef typename View::value_type Element;
3486 typedef ::std::vector<Matcher<const Element&> > MatcherVec;
3487 MatcherVec matchers;
3488 matchers.reserve(::testing::tuple_size<MatcherTuple>::value);
3489 TransformTupleValues(CastAndAppendTransform<const Element&>(), matchers_,
3490 ::std::back_inserter(matchers));
3491 return MakeMatcher(new ElementsAreMatcherImpl<Container>(
3492 matchers.begin(), matchers.end()));
3493 }
3494
3495 private:
3496 const MatcherTuple matchers_;
3497 GTEST_DISALLOW_ASSIGN_(ElementsAreMatcher);
3498 };
3499
3500
3501 template <typename T>
3502 class UnorderedElementsAreArrayMatcher {
3503 public:
3504 UnorderedElementsAreArrayMatcher() {}
3505
3506 template <typename Iter>
3507 UnorderedElementsAreArrayMatcher(Iter first, Iter last)
3508 : matchers_(first, last) {}
3509
3510 template <typename Container>
3511 operator Matcher<Container>() const {
3512 return MakeMatcher(
3513 new UnorderedElementsAreMatcherImpl<Container>(matchers_.begin(),
3514 matchers_.end()));
3515 }
3516
3517 private:
3518 ::std::vector<T> matchers_;
3519
3520 GTEST_DISALLOW_ASSIGN_(UnorderedElementsAreArrayMatcher);
3521 };
3522
3523
3524 template <typename T>
3525 class ElementsAreArrayMatcher {
3526 public:
3527 template <typename Iter>
3528 ElementsAreArrayMatcher(Iter first, Iter last) : matchers_(first, last) {}
3529
3530 template <typename Container>
3531 operator Matcher<Container>() const {
3532 return MakeMatcher(new ElementsAreMatcherImpl<Container>(
3533 matchers_.begin(), matchers_.end()));
3534 }
3535
3536 private:
3537 const ::std::vector<T> matchers_;
3538
3539 GTEST_DISALLOW_ASSIGN_(ElementsAreArrayMatcher);
3540 };
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551 template <typename Tuple2Matcher, typename Second>
3552 class BoundSecondMatcher {
3553 public:
3554 BoundSecondMatcher(const Tuple2Matcher& tm, const Second& second)
3555 : tuple2_matcher_(tm), second_value_(second) {}
3556
3557 template <typename T>
3558 operator Matcher<T>() const {
3559 return MakeMatcher(new Impl<T>(tuple2_matcher_, second_value_));
3560 }
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570 void operator=(const BoundSecondMatcher& ) {
3571 GTEST_LOG_(FATAL) << "BoundSecondMatcher should never be assigned.";
3572 }
3573
3574 private:
3575 template <typename T>
3576 class Impl : public MatcherInterface<T> {
3577 public:
3578 typedef ::testing::tuple<T, Second> ArgTuple;
3579
3580 Impl(const Tuple2Matcher& tm, const Second& second)
3581 : mono_tuple2_matcher_(SafeMatcherCast<const ArgTuple&>(tm)),
3582 second_value_(second) {}
3583
3584 virtual void DescribeTo(::std::ostream* os) const {
3585 *os << "and ";
3586 UniversalPrint(second_value_, os);
3587 *os << " ";
3588 mono_tuple2_matcher_.DescribeTo(os);
3589 }
3590
3591 virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
3592 return mono_tuple2_matcher_.MatchAndExplain(ArgTuple(x, second_value_),
3593 listener);
3594 }
3595
3596 private:
3597 const Matcher<const ArgTuple&> mono_tuple2_matcher_;
3598 const Second second_value_;
3599
3600 GTEST_DISALLOW_ASSIGN_(Impl);
3601 };
3602
3603 const Tuple2Matcher tuple2_matcher_;
3604 const Second second_value_;
3605 };
3606
3607
3608
3609
3610
3611 template <typename Tuple2Matcher, typename Second>
3612 BoundSecondMatcher<Tuple2Matcher, Second> MatcherBindSecond(
3613 const Tuple2Matcher& tm, const Second& second) {
3614 return BoundSecondMatcher<Tuple2Matcher, Second>(tm, second);
3615 }
3616
3617
3618
3619
3620
3621
3622 GTEST_API_ string FormatMatcherDescription(bool negation,
3623 const char* matcher_name,
3624 const Strings& param_values);
3625
3626 }
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643 template <typename Iter>
3644 inline internal::ElementsAreArrayMatcher<
3645 typename ::std::iterator_traits<Iter>::value_type>
3646 ElementsAreArray(Iter first, Iter last) {
3647 typedef typename ::std::iterator_traits<Iter>::value_type T;
3648 return internal::ElementsAreArrayMatcher<T>(first, last);
3649 }
3650
3651 template <typename T>
3652 inline internal::ElementsAreArrayMatcher<T> ElementsAreArray(
3653 const T* pointer, size_t count) {
3654 return ElementsAreArray(pointer, pointer + count);
3655 }
3656
3657 template <typename T, size_t N>
3658 inline internal::ElementsAreArrayMatcher<T> ElementsAreArray(
3659 const T (&array)[N]) {
3660 return ElementsAreArray(array, N);
3661 }
3662
3663 template <typename Container>
3664 inline internal::ElementsAreArrayMatcher<typename Container::value_type>
3665 ElementsAreArray(const Container& container) {
3666 return ElementsAreArray(container.begin(), container.end());
3667 }
3668
3669 #if GTEST_HAS_STD_INITIALIZER_LIST_
3670 template <typename T>
3671 inline internal::ElementsAreArrayMatcher<T>
3672 ElementsAreArray(::std::initializer_list<T> xs) {
3673 return ElementsAreArray(xs.begin(), xs.end());
3674 }
3675 #endif
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685 template <typename Iter>
3686 inline internal::UnorderedElementsAreArrayMatcher<
3687 typename ::std::iterator_traits<Iter>::value_type>
3688 UnorderedElementsAreArray(Iter first, Iter last) {
3689 typedef typename ::std::iterator_traits<Iter>::value_type T;
3690 return internal::UnorderedElementsAreArrayMatcher<T>(first, last);
3691 }
3692
3693 template <typename T>
3694 inline internal::UnorderedElementsAreArrayMatcher<T>
3695 UnorderedElementsAreArray(const T* pointer, size_t count) {
3696 return UnorderedElementsAreArray(pointer, pointer + count);
3697 }
3698
3699 template <typename T, size_t N>
3700 inline internal::UnorderedElementsAreArrayMatcher<T>
3701 UnorderedElementsAreArray(const T (&array)[N]) {
3702 return UnorderedElementsAreArray(array, N);
3703 }
3704
3705 template <typename Container>
3706 inline internal::UnorderedElementsAreArrayMatcher<
3707 typename Container::value_type>
3708 UnorderedElementsAreArray(const Container& container) {
3709 return UnorderedElementsAreArray(container.begin(), container.end());
3710 }
3711
3712 #if GTEST_HAS_STD_INITIALIZER_LIST_
3713 template <typename T>
3714 inline internal::UnorderedElementsAreArrayMatcher<T>
3715 UnorderedElementsAreArray(::std::initializer_list<T> xs) {
3716 return UnorderedElementsAreArray(xs.begin(), xs.end());
3717 }
3718 #endif
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729 const internal::AnythingMatcher _ = {};
3730
3731 template <typename T>
3732 inline Matcher<T> A() { return MakeMatcher(new internal::AnyMatcherImpl<T>()); }
3733
3734
3735 template <typename T>
3736 inline Matcher<T> An() { return A<T>(); }
3737
3738
3739
3740
3741 template <typename T>
3742 inline internal::EqMatcher<T> Eq(T x) { return internal::EqMatcher<T>(x); }
3743
3744
3745
3746 template <typename T>
3747 Matcher<T>::Matcher(T value) { *this = Eq(value); }
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761 template <typename Lhs, typename Rhs>
3762 inline Matcher<Lhs> TypedEq(const Rhs& rhs) { return Eq(rhs); }
3763
3764
3765 template <typename Rhs>
3766 inline internal::GeMatcher<Rhs> Ge(Rhs x) {
3767 return internal::GeMatcher<Rhs>(x);
3768 }
3769
3770
3771 template <typename Rhs>
3772 inline internal::GtMatcher<Rhs> Gt(Rhs x) {
3773 return internal::GtMatcher<Rhs>(x);
3774 }
3775
3776
3777 template <typename Rhs>
3778 inline internal::LeMatcher<Rhs> Le(Rhs x) {
3779 return internal::LeMatcher<Rhs>(x);
3780 }
3781
3782
3783 template <typename Rhs>
3784 inline internal::LtMatcher<Rhs> Lt(Rhs x) {
3785 return internal::LtMatcher<Rhs>(x);
3786 }
3787
3788
3789 template <typename Rhs>
3790 inline internal::NeMatcher<Rhs> Ne(Rhs x) {
3791 return internal::NeMatcher<Rhs>(x);
3792 }
3793
3794
3795 inline PolymorphicMatcher<internal::IsNullMatcher > IsNull() {
3796 return MakePolymorphicMatcher(internal::IsNullMatcher());
3797 }
3798
3799
3800
3801
3802 inline PolymorphicMatcher<internal::NotNullMatcher > NotNull() {
3803 return MakePolymorphicMatcher(internal::NotNullMatcher());
3804 }
3805
3806
3807
3808 template <typename T>
3809 inline internal::RefMatcher<T&> Ref(T& x) {
3810 return internal::RefMatcher<T&>(x);
3811 }
3812
3813
3814
3815 inline internal::FloatingEqMatcher<double> DoubleEq(double rhs) {
3816 return internal::FloatingEqMatcher<double>(rhs, false);
3817 }
3818
3819
3820
3821 inline internal::FloatingEqMatcher<double> NanSensitiveDoubleEq(double rhs) {
3822 return internal::FloatingEqMatcher<double>(rhs, true);
3823 }
3824
3825
3826
3827
3828 inline internal::FloatingEqMatcher<double> DoubleNear(
3829 double rhs, double max_abs_error) {
3830 return internal::FloatingEqMatcher<double>(rhs, false, max_abs_error);
3831 }
3832
3833
3834
3835
3836 inline internal::FloatingEqMatcher<double> NanSensitiveDoubleNear(
3837 double rhs, double max_abs_error) {
3838 return internal::FloatingEqMatcher<double>(rhs, true, max_abs_error);
3839 }
3840
3841
3842
3843 inline internal::FloatingEqMatcher<float> FloatEq(float rhs) {
3844 return internal::FloatingEqMatcher<float>(rhs, false);
3845 }
3846
3847
3848
3849 inline internal::FloatingEqMatcher<float> NanSensitiveFloatEq(float rhs) {
3850 return internal::FloatingEqMatcher<float>(rhs, true);
3851 }
3852
3853
3854
3855
3856 inline internal::FloatingEqMatcher<float> FloatNear(
3857 float rhs, float max_abs_error) {
3858 return internal::FloatingEqMatcher<float>(rhs, false, max_abs_error);
3859 }
3860
3861
3862
3863
3864 inline internal::FloatingEqMatcher<float> NanSensitiveFloatNear(
3865 float rhs, float max_abs_error) {
3866 return internal::FloatingEqMatcher<float>(rhs, true, max_abs_error);
3867 }
3868
3869
3870
3871 template <typename InnerMatcher>
3872 inline internal::PointeeMatcher<InnerMatcher> Pointee(
3873 const InnerMatcher& inner_matcher) {
3874 return internal::PointeeMatcher<InnerMatcher>(inner_matcher);
3875 }
3876
3877
3878
3879
3880
3881
3882
3883 template <typename To>
3884 inline PolymorphicMatcher<internal::WhenDynamicCastToMatcher<To> >
3885 WhenDynamicCastTo(const Matcher<To>& inner_matcher) {
3886 return MakePolymorphicMatcher(
3887 internal::WhenDynamicCastToMatcher<To>(inner_matcher));
3888 }
3889
3890
3891
3892
3893
3894 template <typename Class, typename FieldType, typename FieldMatcher>
3895 inline PolymorphicMatcher<
3896 internal::FieldMatcher<Class, FieldType> > Field(
3897 FieldType Class::*field, const FieldMatcher& matcher) {
3898 return MakePolymorphicMatcher(
3899 internal::FieldMatcher<Class, FieldType>(
3900 field, MatcherCast<const FieldType&>(matcher)));
3901
3902
3903
3904
3905 }
3906
3907
3908
3909
3910
3911 template <typename Class, typename PropertyType, typename PropertyMatcher>
3912 inline PolymorphicMatcher<
3913 internal::PropertyMatcher<Class, PropertyType> > Property(
3914 PropertyType (Class::*property)() const, const PropertyMatcher& matcher) {
3915 return MakePolymorphicMatcher(
3916 internal::PropertyMatcher<Class, PropertyType>(
3917 property,
3918 MatcherCast<GTEST_REFERENCE_TO_CONST_(PropertyType)>(matcher)));
3919
3920
3921
3922
3923 }
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938 template <typename Callable, typename ResultOfMatcher>
3939 internal::ResultOfMatcher<Callable> ResultOf(
3940 Callable callable, const ResultOfMatcher& matcher) {
3941 return internal::ResultOfMatcher<Callable>(
3942 callable,
3943 MatcherCast<typename internal::CallableTraits<Callable>::ResultType>(
3944 matcher));
3945
3946
3947
3948
3949 }
3950
3951
3952
3953
3954 inline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> >
3955 StrEq(const internal::string& str) {
3956 return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>(
3957 str, true, true));
3958 }
3959
3960
3961 inline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> >
3962 StrNe(const internal::string& str) {
3963 return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>(
3964 str, false, true));
3965 }
3966
3967
3968 inline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> >
3969 StrCaseEq(const internal::string& str) {
3970 return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>(
3971 str, true, false));
3972 }
3973
3974
3975 inline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> >
3976 StrCaseNe(const internal::string& str) {
3977 return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>(
3978 str, false, false));
3979 }
3980
3981
3982
3983 inline PolymorphicMatcher<internal::HasSubstrMatcher<internal::string> >
3984 HasSubstr(const internal::string& substring) {
3985 return MakePolymorphicMatcher(internal::HasSubstrMatcher<internal::string>(
3986 substring));
3987 }
3988
3989
3990 inline PolymorphicMatcher<internal::StartsWithMatcher<internal::string> >
3991 StartsWith(const internal::string& prefix) {
3992 return MakePolymorphicMatcher(internal::StartsWithMatcher<internal::string>(
3993 prefix));
3994 }
3995
3996
3997 inline PolymorphicMatcher<internal::EndsWithMatcher<internal::string> >
3998 EndsWith(const internal::string& suffix) {
3999 return MakePolymorphicMatcher(internal::EndsWithMatcher<internal::string>(
4000 suffix));
4001 }
4002
4003
4004
4005 inline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex(
4006 const internal::RE* regex) {
4007 return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, true));
4008 }
4009 inline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex(
4010 const internal::string& regex) {
4011 return MatchesRegex(new internal::RE(regex));
4012 }
4013
4014
4015
4016 inline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex(
4017 const internal::RE* regex) {
4018 return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, false));
4019 }
4020 inline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex(
4021 const internal::string& regex) {
4022 return ContainsRegex(new internal::RE(regex));
4023 }
4024
4025 #if GTEST_HAS_GLOBAL_WSTRING || GTEST_HAS_STD_WSTRING
4026
4027
4028
4029 inline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> >
4030 StrEq(const internal::wstring& str) {
4031 return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>(
4032 str, true, true));
4033 }
4034
4035
4036 inline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> >
4037 StrNe(const internal::wstring& str) {
4038 return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>(
4039 str, false, true));
4040 }
4041
4042
4043 inline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> >
4044 StrCaseEq(const internal::wstring& str) {
4045 return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>(
4046 str, true, false));
4047 }
4048
4049
4050 inline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> >
4051 StrCaseNe(const internal::wstring& str) {
4052 return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>(
4053 str, false, false));
4054 }
4055
4056
4057
4058 inline PolymorphicMatcher<internal::HasSubstrMatcher<internal::wstring> >
4059 HasSubstr(const internal::wstring& substring) {
4060 return MakePolymorphicMatcher(internal::HasSubstrMatcher<internal::wstring>(
4061 substring));
4062 }
4063
4064
4065 inline PolymorphicMatcher<internal::StartsWithMatcher<internal::wstring> >
4066 StartsWith(const internal::wstring& prefix) {
4067 return MakePolymorphicMatcher(internal::StartsWithMatcher<internal::wstring>(
4068 prefix));
4069 }
4070
4071
4072 inline PolymorphicMatcher<internal::EndsWithMatcher<internal::wstring> >
4073 EndsWith(const internal::wstring& suffix) {
4074 return MakePolymorphicMatcher(internal::EndsWithMatcher<internal::wstring>(
4075 suffix));
4076 }
4077
4078 #endif
4079
4080
4081
4082 inline internal::Eq2Matcher Eq() { return internal::Eq2Matcher(); }
4083
4084
4085
4086 inline internal::Ge2Matcher Ge() { return internal::Ge2Matcher(); }
4087
4088
4089
4090 inline internal::Gt2Matcher Gt() { return internal::Gt2Matcher(); }
4091
4092
4093
4094 inline internal::Le2Matcher Le() { return internal::Le2Matcher(); }
4095
4096
4097
4098 inline internal::Lt2Matcher Lt() { return internal::Lt2Matcher(); }
4099
4100
4101
4102 inline internal::Ne2Matcher Ne() { return internal::Ne2Matcher(); }
4103
4104
4105
4106 template <typename InnerMatcher>
4107 inline internal::NotMatcher<InnerMatcher> Not(InnerMatcher m) {
4108 return internal::NotMatcher<InnerMatcher>(m);
4109 }
4110
4111
4112
4113
4114 template <typename Predicate>
4115 inline PolymorphicMatcher<internal::TrulyMatcher<Predicate> >
4116 Truly(Predicate pred) {
4117 return MakePolymorphicMatcher(internal::TrulyMatcher<Predicate>(pred));
4118 }
4119
4120
4121
4122
4123
4124
4125
4126 template <typename SizeMatcher>
4127 inline internal::SizeIsMatcher<SizeMatcher>
4128 SizeIs(const SizeMatcher& size_matcher) {
4129 return internal::SizeIsMatcher<SizeMatcher>(size_matcher);
4130 }
4131
4132
4133
4134
4135
4136
4137 template <typename DistanceMatcher>
4138 inline internal::BeginEndDistanceIsMatcher<DistanceMatcher>
4139 BeginEndDistanceIs(const DistanceMatcher& distance_matcher) {
4140 return internal::BeginEndDistanceIsMatcher<DistanceMatcher>(distance_matcher);
4141 }
4142
4143
4144
4145
4146
4147 template <typename Container>
4148 inline PolymorphicMatcher<internal::ContainerEqMatcher<
4149 GTEST_REMOVE_CONST_(Container)> >
4150 ContainerEq(const Container& rhs) {
4151
4152
4153 typedef GTEST_REMOVE_CONST_(Container) RawContainer;
4154 return MakePolymorphicMatcher(
4155 internal::ContainerEqMatcher<RawContainer>(rhs));
4156 }
4157
4158
4159
4160 template <typename Comparator, typename ContainerMatcher>
4161 inline internal::WhenSortedByMatcher<Comparator, ContainerMatcher>
4162 WhenSortedBy(const Comparator& comparator,
4163 const ContainerMatcher& container_matcher) {
4164 return internal::WhenSortedByMatcher<Comparator, ContainerMatcher>(
4165 comparator, container_matcher);
4166 }
4167
4168
4169
4170 template <typename ContainerMatcher>
4171 inline internal::WhenSortedByMatcher<internal::LessComparator, ContainerMatcher>
4172 WhenSorted(const ContainerMatcher& container_matcher) {
4173 return
4174 internal::WhenSortedByMatcher<internal::LessComparator, ContainerMatcher>(
4175 internal::LessComparator(), container_matcher);
4176 }
4177
4178
4179
4180
4181
4182
4183
4184 template <typename TupleMatcher, typename Container>
4185 inline internal::PointwiseMatcher<TupleMatcher,
4186 GTEST_REMOVE_CONST_(Container)>
4187 Pointwise(const TupleMatcher& tuple_matcher, const Container& rhs) {
4188
4189
4190
4191 typedef GTEST_REMOVE_CONST_(Container) RawContainer;
4192 return internal::PointwiseMatcher<TupleMatcher, RawContainer>(
4193 tuple_matcher, rhs);
4194 }
4195
4196 #if GTEST_HAS_STD_INITIALIZER_LIST_
4197
4198
4199 template <typename TupleMatcher, typename T>
4200 inline internal::PointwiseMatcher<TupleMatcher, std::vector<T> > Pointwise(
4201 const TupleMatcher& tuple_matcher, std::initializer_list<T> rhs) {
4202 return Pointwise(tuple_matcher, std::vector<T>(rhs));
4203 }
4204
4205 #endif
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218 template <typename Tuple2Matcher, typename RhsContainer>
4219 inline internal::UnorderedElementsAreArrayMatcher<
4220 typename internal::BoundSecondMatcher<
4221 Tuple2Matcher, typename internal::StlContainerView<GTEST_REMOVE_CONST_(
4222 RhsContainer)>::type::value_type> >
4223 UnorderedPointwise(const Tuple2Matcher& tuple2_matcher,
4224 const RhsContainer& rhs_container) {
4225
4226
4227
4228 typedef GTEST_REMOVE_CONST_(RhsContainer) RawRhsContainer;
4229
4230
4231
4232 typedef typename internal::StlContainerView<RawRhsContainer> RhsView;
4233 typedef typename RhsView::type RhsStlContainer;
4234 typedef typename RhsStlContainer::value_type Second;
4235 const RhsStlContainer& rhs_stl_container =
4236 RhsView::ConstReference(rhs_container);
4237
4238
4239 ::std::vector<internal::BoundSecondMatcher<Tuple2Matcher, Second> > matchers;
4240 for (typename RhsStlContainer::const_iterator it = rhs_stl_container.begin();
4241 it != rhs_stl_container.end(); ++it) {
4242 matchers.push_back(
4243 internal::MatcherBindSecond(tuple2_matcher, *it));
4244 }
4245
4246
4247 return UnorderedElementsAreArray(matchers);
4248 }
4249
4250 #if GTEST_HAS_STD_INITIALIZER_LIST_
4251
4252
4253 template <typename Tuple2Matcher, typename T>
4254 inline internal::UnorderedElementsAreArrayMatcher<
4255 typename internal::BoundSecondMatcher<Tuple2Matcher, T> >
4256 UnorderedPointwise(const Tuple2Matcher& tuple2_matcher,
4257 std::initializer_list<T> rhs) {
4258 return UnorderedPointwise(tuple2_matcher, std::vector<T>(rhs));
4259 }
4260
4261 #endif
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281 template <typename M>
4282 inline internal::ContainsMatcher<M> Contains(M matcher) {
4283 return internal::ContainsMatcher<M>(matcher);
4284 }
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313 template <typename M>
4314 inline internal::EachMatcher<M> Each(M matcher) {
4315 return internal::EachMatcher<M>(matcher);
4316 }
4317
4318
4319
4320
4321 template <typename M>
4322 inline internal::KeyMatcher<M> Key(M inner_matcher) {
4323 return internal::KeyMatcher<M>(inner_matcher);
4324 }
4325
4326
4327
4328
4329
4330
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);
4336 }
4337
4338
4339
4340 template <typename M>
4341 inline internal::MatcherAsPredicate<M> Matches(M matcher) {
4342 return internal::MatcherAsPredicate<M>(matcher);
4343 }
4344
4345
4346 template <typename T, typename M>
4347 inline bool Value(const T& value, M matcher) {
4348 return testing::Matches(matcher)(value);
4349 }
4350
4351
4352
4353 template <typename T, typename M>
4354 inline bool ExplainMatchResult(
4355 M matcher, const T& value, MatchResultListener* listener) {
4356 return SafeMatcherCast<const T&>(matcher).MatchAndExplain(value, listener);
4357 }
4358
4359 #if GTEST_LANG_CXX11
4360
4361
4362 template <typename... Args>
4363 inline internal::AllOfMatcher<Args...> AllOf(const Args&... matchers) {
4364 return internal::AllOfMatcher<Args...>(matchers...);
4365 }
4366
4367 template <typename... Args>
4368 inline internal::AnyOfMatcher<Args...> AnyOf(const Args&... matchers) {
4369 return internal::AnyOfMatcher<Args...>(matchers...);
4370 }
4371
4372 #endif
4373
4374
4375
4376
4377
4378
4379
4380
4381 template <typename InnerMatcher>
4382 inline InnerMatcher AllArgs(const InnerMatcher& matcher) { return matcher; }
4383
4384
4385
4386
4387
4388 #define ASSERT_THAT(value, matcher) ASSERT_PRED_FORMAT1(\
4389 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
4390 #define EXPECT_THAT(value, matcher) EXPECT_PRED_FORMAT1(\
4391 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
4392
4393 }
4394
4395
4396
4397
4398 #include "gmock/internal/custom/gmock-matchers.h"
4399 #endif