00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_
00039 #define GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_
00040
00041 #include <math.h>
00042 #include <algorithm>
00043 #include <iterator>
00044 #include <limits>
00045 #include <ostream>
00046 #include <sstream>
00047 #include <string>
00048 #include <utility>
00049 #include <vector>
00050
00051 #include "gmock/internal/gmock-internal-utils.h"
00052 #include "gmock/internal/gmock-port.h"
00053 #include "gtest/gtest.h"
00054
00055 #if GTEST_HAS_STD_INITIALIZER_LIST_
00056 # include <initializer_list>
00057 #endif
00058
00059 namespace testing {
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 class MatchResultListener {
00081 public:
00082
00083
00084
00085 explicit MatchResultListener(::std::ostream* os) : stream_(os) {}
00086 virtual ~MatchResultListener() = 0;
00087
00088
00089
00090 template <typename T>
00091 MatchResultListener& operator<<(const T& x) {
00092 if (stream_ != NULL)
00093 *stream_ << x;
00094 return *this;
00095 }
00096
00097
00098 ::std::ostream* stream() { return stream_; }
00099
00100
00101
00102
00103
00104 bool IsInterested() const { return stream_ != NULL; }
00105
00106 private:
00107 ::std::ostream* const stream_;
00108
00109 GTEST_DISALLOW_COPY_AND_ASSIGN_(MatchResultListener);
00110 };
00111
00112 inline MatchResultListener::~MatchResultListener() {
00113 }
00114
00115
00116
00117 class MatcherDescriberInterface {
00118 public:
00119 virtual ~MatcherDescriberInterface() {}
00120
00121
00122
00123
00124
00125
00126 virtual void DescribeTo(::std::ostream* os) const = 0;
00127
00128
00129
00130
00131
00132
00133
00134 virtual void DescribeNegationTo(::std::ostream* os) const {
00135 *os << "not (";
00136 DescribeTo(os);
00137 *os << ")";
00138 }
00139 };
00140
00141
00142 template <typename T>
00143 class MatcherInterface : public MatcherDescriberInterface {
00144 public:
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176 virtual bool MatchAndExplain(T x, MatchResultListener* listener) const = 0;
00177
00178
00179
00180
00181 };
00182
00183
00184 class StringMatchResultListener : public MatchResultListener {
00185 public:
00186 StringMatchResultListener() : MatchResultListener(&ss_) {}
00187
00188
00189 internal::string str() const { return ss_.str(); }
00190
00191
00192 void Clear() { ss_.str(""); }
00193
00194 private:
00195 ::std::stringstream ss_;
00196
00197 GTEST_DISALLOW_COPY_AND_ASSIGN_(StringMatchResultListener);
00198 };
00199
00200 namespace internal {
00201
00202 struct AnyEq {
00203 template <typename A, typename B>
00204 bool operator()(const A& a, const B& b) const { return a == b; }
00205 };
00206 struct AnyNe {
00207 template <typename A, typename B>
00208 bool operator()(const A& a, const B& b) const { return a != b; }
00209 };
00210 struct AnyLt {
00211 template <typename A, typename B>
00212 bool operator()(const A& a, const B& b) const { return a < b; }
00213 };
00214 struct AnyGt {
00215 template <typename A, typename B>
00216 bool operator()(const A& a, const B& b) const { return a > b; }
00217 };
00218 struct AnyLe {
00219 template <typename A, typename B>
00220 bool operator()(const A& a, const B& b) const { return a <= b; }
00221 };
00222 struct AnyGe {
00223 template <typename A, typename B>
00224 bool operator()(const A& a, const B& b) const { return a >= b; }
00225 };
00226
00227
00228 class DummyMatchResultListener : public MatchResultListener {
00229 public:
00230 DummyMatchResultListener() : MatchResultListener(NULL) {}
00231
00232 private:
00233 GTEST_DISALLOW_COPY_AND_ASSIGN_(DummyMatchResultListener);
00234 };
00235
00236
00237
00238
00239 class StreamMatchResultListener : public MatchResultListener {
00240 public:
00241 explicit StreamMatchResultListener(::std::ostream* os)
00242 : MatchResultListener(os) {}
00243
00244 private:
00245 GTEST_DISALLOW_COPY_AND_ASSIGN_(StreamMatchResultListener);
00246 };
00247
00248
00249
00250
00251 template <typename T>
00252 class MatcherBase {
00253 public:
00254
00255
00256 bool MatchAndExplain(T x, MatchResultListener* listener) const {
00257 return impl_->MatchAndExplain(x, listener);
00258 }
00259
00260
00261 bool Matches(T x) const {
00262 DummyMatchResultListener dummy;
00263 return MatchAndExplain(x, &dummy);
00264 }
00265
00266
00267 void DescribeTo(::std::ostream* os) const { impl_->DescribeTo(os); }
00268
00269
00270 void DescribeNegationTo(::std::ostream* os) const {
00271 impl_->DescribeNegationTo(os);
00272 }
00273
00274
00275 void ExplainMatchResultTo(T x, ::std::ostream* os) const {
00276 StreamMatchResultListener listener(os);
00277 MatchAndExplain(x, &listener);
00278 }
00279
00280
00281
00282
00283 const MatcherDescriberInterface* GetDescriber() const {
00284 return impl_.get();
00285 }
00286
00287 protected:
00288 MatcherBase() {}
00289
00290
00291 explicit MatcherBase(const MatcherInterface<T>* impl)
00292 : impl_(impl) {}
00293
00294 virtual ~MatcherBase() {}
00295
00296 private:
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308 ::testing::internal::linked_ptr<const MatcherInterface<T> > impl_;
00309 };
00310
00311 }
00312
00313
00314
00315
00316
00317
00318 template <typename T>
00319 class Matcher : public internal::MatcherBase<T> {
00320 public:
00321
00322
00323
00324 explicit Matcher() {}
00325
00326
00327 explicit Matcher(const MatcherInterface<T>* impl)
00328 : internal::MatcherBase<T>(impl) {}
00329
00330
00331
00332 Matcher(T value);
00333 };
00334
00335
00336
00337
00338 template <>
00339 class GTEST_API_ Matcher<const internal::string&>
00340 : public internal::MatcherBase<const internal::string&> {
00341 public:
00342 Matcher() {}
00343
00344 explicit Matcher(const MatcherInterface<const internal::string&>* impl)
00345 : internal::MatcherBase<const internal::string&>(impl) {}
00346
00347
00348
00349 Matcher(const internal::string& s);
00350
00351
00352 Matcher(const char* s);
00353 };
00354
00355 template <>
00356 class GTEST_API_ Matcher<internal::string>
00357 : public internal::MatcherBase<internal::string> {
00358 public:
00359 Matcher() {}
00360
00361 explicit Matcher(const MatcherInterface<internal::string>* impl)
00362 : internal::MatcherBase<internal::string>(impl) {}
00363
00364
00365
00366 Matcher(const internal::string& s);
00367
00368
00369 Matcher(const char* s);
00370 };
00371
00372 #if GTEST_HAS_STRING_PIECE_
00373
00374
00375
00376 template <>
00377 class GTEST_API_ Matcher<const StringPiece&>
00378 : public internal::MatcherBase<const StringPiece&> {
00379 public:
00380 Matcher() {}
00381
00382 explicit Matcher(const MatcherInterface<const StringPiece&>* impl)
00383 : internal::MatcherBase<const StringPiece&>(impl) {}
00384
00385
00386
00387 Matcher(const internal::string& s);
00388
00389
00390 Matcher(const char* s);
00391
00392
00393 Matcher(StringPiece s);
00394 };
00395
00396 template <>
00397 class GTEST_API_ Matcher<StringPiece>
00398 : public internal::MatcherBase<StringPiece> {
00399 public:
00400 Matcher() {}
00401
00402 explicit Matcher(const MatcherInterface<StringPiece>* impl)
00403 : internal::MatcherBase<StringPiece>(impl) {}
00404
00405
00406
00407 Matcher(const internal::string& s);
00408
00409
00410 Matcher(const char* s);
00411
00412
00413 Matcher(StringPiece s);
00414 };
00415 #endif // GTEST_HAS_STRING_PIECE_
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429 template <class Impl>
00430 class PolymorphicMatcher {
00431 public:
00432 explicit PolymorphicMatcher(const Impl& an_impl) : impl_(an_impl) {}
00433
00434
00435
00436 Impl& mutable_impl() { return impl_; }
00437
00438
00439
00440 const Impl& impl() const { return impl_; }
00441
00442 template <typename T>
00443 operator Matcher<T>() const {
00444 return Matcher<T>(new MonomorphicImpl<T>(impl_));
00445 }
00446
00447 private:
00448 template <typename T>
00449 class MonomorphicImpl : public MatcherInterface<T> {
00450 public:
00451 explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {}
00452
00453 virtual void DescribeTo(::std::ostream* os) const {
00454 impl_.DescribeTo(os);
00455 }
00456
00457 virtual void DescribeNegationTo(::std::ostream* os) const {
00458 impl_.DescribeNegationTo(os);
00459 }
00460
00461 virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
00462 return impl_.MatchAndExplain(x, listener);
00463 }
00464
00465 private:
00466 const Impl impl_;
00467
00468 GTEST_DISALLOW_ASSIGN_(MonomorphicImpl);
00469 };
00470
00471 Impl impl_;
00472
00473 GTEST_DISALLOW_ASSIGN_(PolymorphicMatcher);
00474 };
00475
00476
00477
00478
00479
00480
00481
00482
00483 template <typename T>
00484 inline Matcher<T> MakeMatcher(const MatcherInterface<T>* impl) {
00485 return Matcher<T>(impl);
00486 }
00487
00488
00489
00490
00491
00492
00493
00494
00495 template <class Impl>
00496 inline PolymorphicMatcher<Impl> MakePolymorphicMatcher(const Impl& impl) {
00497 return PolymorphicMatcher<Impl>(impl);
00498 }
00499
00500
00501
00502 namespace internal {
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514 template <typename T, typename M>
00515 class MatcherCastImpl {
00516 public:
00517 static Matcher<T> Cast(const M& polymorphic_matcher_or_value) {
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531 return CastImpl(
00532 polymorphic_matcher_or_value,
00533 BooleanConstant<
00534 internal::ImplicitlyConvertible<M, Matcher<T> >::value>());
00535 }
00536
00537 private:
00538 static Matcher<T> CastImpl(const M& value, BooleanConstant<false>) {
00539
00540
00541
00542 return Matcher<T>(ImplicitCast_<T>(value));
00543 }
00544
00545 static Matcher<T> CastImpl(const M& polymorphic_matcher_or_value,
00546 BooleanConstant<true>) {
00547
00548
00549
00550
00551
00552
00553
00554
00555 return polymorphic_matcher_or_value;
00556 }
00557 };
00558
00559
00560
00561
00562 template <typename T, typename U>
00563 class MatcherCastImpl<T, Matcher<U> > {
00564 public:
00565 static Matcher<T> Cast(const Matcher<U>& source_matcher) {
00566 return Matcher<T>(new Impl(source_matcher));
00567 }
00568
00569 private:
00570 class Impl : public MatcherInterface<T> {
00571 public:
00572 explicit Impl(const Matcher<U>& source_matcher)
00573 : source_matcher_(source_matcher) {}
00574
00575
00576 virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
00577 return source_matcher_.MatchAndExplain(static_cast<U>(x), listener);
00578 }
00579
00580 virtual void DescribeTo(::std::ostream* os) const {
00581 source_matcher_.DescribeTo(os);
00582 }
00583
00584 virtual void DescribeNegationTo(::std::ostream* os) const {
00585 source_matcher_.DescribeNegationTo(os);
00586 }
00587
00588 private:
00589 const Matcher<U> source_matcher_;
00590
00591 GTEST_DISALLOW_ASSIGN_(Impl);
00592 };
00593 };
00594
00595
00596
00597 template <typename T>
00598 class MatcherCastImpl<T, Matcher<T> > {
00599 public:
00600 static Matcher<T> Cast(const Matcher<T>& matcher) { return matcher; }
00601 };
00602
00603 }
00604
00605
00606
00607
00608
00609 template <typename T, typename M>
00610 inline Matcher<T> MatcherCast(const M& matcher) {
00611 return internal::MatcherCastImpl<T, M>::Cast(matcher);
00612 }
00613
00614
00615
00616
00617
00618
00619
00620
00621 template <typename T>
00622 class SafeMatcherCastImpl {
00623 public:
00624
00625
00626 template <typename M>
00627 static inline Matcher<T> Cast(const M& polymorphic_matcher_or_value) {
00628 return internal::MatcherCastImpl<T, M>::Cast(polymorphic_matcher_or_value);
00629 }
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640 template <typename U>
00641 static inline Matcher<T> Cast(const Matcher<U>& matcher) {
00642
00643 GTEST_COMPILE_ASSERT_((internal::ImplicitlyConvertible<T, U>::value),
00644 T_must_be_implicitly_convertible_to_U);
00645
00646
00647 GTEST_COMPILE_ASSERT_(
00648 internal::is_reference<T>::value || !internal::is_reference<U>::value,
00649 cannot_convert_non_referentce_arg_to_reference);
00650
00651
00652 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(T) RawT;
00653 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(U) RawU;
00654 const bool kTIsOther = GMOCK_KIND_OF_(RawT) == internal::kOther;
00655 const bool kUIsOther = GMOCK_KIND_OF_(RawU) == internal::kOther;
00656 GTEST_COMPILE_ASSERT_(
00657 kTIsOther || kUIsOther ||
00658 (internal::LosslessArithmeticConvertible<RawT, RawU>::value),
00659 conversion_of_arithmetic_types_must_be_lossless);
00660 return MatcherCast<T>(matcher);
00661 }
00662 };
00663
00664 template <typename T, typename M>
00665 inline Matcher<T> SafeMatcherCast(const M& polymorphic_matcher) {
00666 return SafeMatcherCastImpl<T>::Cast(polymorphic_matcher);
00667 }
00668
00669
00670 template <typename T>
00671 Matcher<T> A();
00672
00673
00674
00675 namespace internal {
00676
00677
00678 inline void PrintIfNotEmpty(const internal::string& explanation,
00679 ::std::ostream* os) {
00680 if (explanation != "" && os != NULL) {
00681 *os << ", " << explanation;
00682 }
00683 }
00684
00685
00686
00687
00688 inline bool IsReadableTypeName(const string& type_name) {
00689
00690
00691 return (type_name.length() <= 20 ||
00692 type_name.find_first_of("<(") == string::npos);
00693 }
00694
00695
00696
00697
00698
00699
00700 template <typename Value, typename T>
00701 bool MatchPrintAndExplain(Value& value, const Matcher<T>& matcher,
00702 MatchResultListener* listener) {
00703 if (!listener->IsInterested()) {
00704
00705
00706 return matcher.Matches(value);
00707 }
00708
00709 StringMatchResultListener inner_listener;
00710 const bool match = matcher.MatchAndExplain(value, &inner_listener);
00711
00712 UniversalPrint(value, listener->stream());
00713 #if GTEST_HAS_RTTI
00714 const string& type_name = GetTypeName<Value>();
00715 if (IsReadableTypeName(type_name))
00716 *listener->stream() << " (of type " << type_name << ")";
00717 #endif
00718 PrintIfNotEmpty(inner_listener.str(), listener->stream());
00719
00720 return match;
00721 }
00722
00723
00724
00725 template <size_t N>
00726 class TuplePrefix {
00727 public:
00728
00729
00730
00731 template <typename MatcherTuple, typename ValueTuple>
00732 static bool Matches(const MatcherTuple& matcher_tuple,
00733 const ValueTuple& value_tuple) {
00734 return TuplePrefix<N - 1>::Matches(matcher_tuple, value_tuple)
00735 && get<N - 1>(matcher_tuple).Matches(get<N - 1>(value_tuple));
00736 }
00737
00738
00739
00740
00741
00742 template <typename MatcherTuple, typename ValueTuple>
00743 static void ExplainMatchFailuresTo(const MatcherTuple& matchers,
00744 const ValueTuple& values,
00745 ::std::ostream* os) {
00746
00747 TuplePrefix<N - 1>::ExplainMatchFailuresTo(matchers, values, os);
00748
00749
00750
00751 typename tuple_element<N - 1, MatcherTuple>::type matcher =
00752 get<N - 1>(matchers);
00753 typedef typename tuple_element<N - 1, ValueTuple>::type Value;
00754 Value value = get<N - 1>(values);
00755 StringMatchResultListener listener;
00756 if (!matcher.MatchAndExplain(value, &listener)) {
00757
00758
00759 *os << " Expected arg #" << N - 1 << ": ";
00760 get<N - 1>(matchers).DescribeTo(os);
00761 *os << "\n Actual: ";
00762
00763
00764
00765
00766
00767 internal::UniversalPrint(value, os);
00768 PrintIfNotEmpty(listener.str(), os);
00769 *os << "\n";
00770 }
00771 }
00772 };
00773
00774
00775 template <>
00776 class TuplePrefix<0> {
00777 public:
00778 template <typename MatcherTuple, typename ValueTuple>
00779 static bool Matches(const MatcherTuple& ,
00780 const ValueTuple& ) {
00781 return true;
00782 }
00783
00784 template <typename MatcherTuple, typename ValueTuple>
00785 static void ExplainMatchFailuresTo(const MatcherTuple& ,
00786 const ValueTuple& ,
00787 ::std::ostream* ) {}
00788 };
00789
00790
00791
00792
00793
00794
00795 template <typename MatcherTuple, typename ValueTuple>
00796 bool TupleMatches(const MatcherTuple& matcher_tuple,
00797 const ValueTuple& value_tuple) {
00798
00799
00800 GTEST_COMPILE_ASSERT_(tuple_size<MatcherTuple>::value ==
00801 tuple_size<ValueTuple>::value,
00802 matcher_and_value_have_different_numbers_of_fields);
00803 return TuplePrefix<tuple_size<ValueTuple>::value>::
00804 Matches(matcher_tuple, value_tuple);
00805 }
00806
00807
00808
00809 template <typename MatcherTuple, typename ValueTuple>
00810 void ExplainMatchFailureTupleTo(const MatcherTuple& matchers,
00811 const ValueTuple& values,
00812 ::std::ostream* os) {
00813 TuplePrefix<tuple_size<MatcherTuple>::value>::ExplainMatchFailuresTo(
00814 matchers, values, os);
00815 }
00816
00817
00818
00819
00820
00821 template <typename Tuple, typename Func, typename OutIter>
00822 class TransformTupleValuesHelper {
00823 private:
00824 typedef ::testing::tuple_size<Tuple> TupleSize;
00825
00826 public:
00827
00828
00829 static OutIter Run(Func f, const Tuple& t, OutIter out) {
00830 return IterateOverTuple<Tuple, TupleSize::value>()(f, t, out);
00831 }
00832
00833 private:
00834 template <typename Tup, size_t kRemainingSize>
00835 struct IterateOverTuple {
00836 OutIter operator() (Func f, const Tup& t, OutIter out) const {
00837 *out++ = f(::testing::get<TupleSize::value - kRemainingSize>(t));
00838 return IterateOverTuple<Tup, kRemainingSize - 1>()(f, t, out);
00839 }
00840 };
00841 template <typename Tup>
00842 struct IterateOverTuple<Tup, 0> {
00843 OutIter operator() (Func , const Tup& , OutIter out) const {
00844 return out;
00845 }
00846 };
00847 };
00848
00849
00850
00851
00852 template <typename Tuple, typename Func, typename OutIter>
00853 OutIter TransformTupleValues(Func f, const Tuple& t, OutIter out) {
00854 return TransformTupleValuesHelper<Tuple, Func, OutIter>::Run(f, t, out);
00855 }
00856
00857
00858 template <typename T>
00859 class AnyMatcherImpl : public MatcherInterface<T> {
00860 public:
00861 virtual bool MatchAndExplain(
00862 T , MatchResultListener* ) const { return true; }
00863 virtual void DescribeTo(::std::ostream* os) const { *os << "is anything"; }
00864 virtual void DescribeNegationTo(::std::ostream* os) const {
00865
00866
00867
00868 *os << "never matches";
00869 }
00870 };
00871
00872
00873
00874
00875
00876 class AnythingMatcher {
00877 public:
00878 template <typename T>
00879 operator Matcher<T>() const { return A<T>(); }
00880 };
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892 template <typename D, typename Rhs, typename Op>
00893 class ComparisonBase {
00894 public:
00895 explicit ComparisonBase(const Rhs& rhs) : rhs_(rhs) {}
00896 template <typename Lhs>
00897 operator Matcher<Lhs>() const {
00898 return MakeMatcher(new Impl<Lhs>(rhs_));
00899 }
00900
00901 private:
00902 template <typename Lhs>
00903 class Impl : public MatcherInterface<Lhs> {
00904 public:
00905 explicit Impl(const Rhs& rhs) : rhs_(rhs) {}
00906 virtual bool MatchAndExplain(
00907 Lhs lhs, MatchResultListener* ) const {
00908 return Op()(lhs, rhs_);
00909 }
00910 virtual void DescribeTo(::std::ostream* os) const {
00911 *os << D::Desc() << " ";
00912 UniversalPrint(rhs_, os);
00913 }
00914 virtual void DescribeNegationTo(::std::ostream* os) const {
00915 *os << D::NegatedDesc() << " ";
00916 UniversalPrint(rhs_, os);
00917 }
00918 private:
00919 Rhs rhs_;
00920 GTEST_DISALLOW_ASSIGN_(Impl);
00921 };
00922 Rhs rhs_;
00923 GTEST_DISALLOW_ASSIGN_(ComparisonBase);
00924 };
00925
00926 template <typename Rhs>
00927 class EqMatcher : public ComparisonBase<EqMatcher<Rhs>, Rhs, AnyEq> {
00928 public:
00929 explicit EqMatcher(const Rhs& rhs)
00930 : ComparisonBase<EqMatcher<Rhs>, Rhs, AnyEq>(rhs) { }
00931 static const char* Desc() { return "is equal to"; }
00932 static const char* NegatedDesc() { return "isn't equal to"; }
00933 };
00934 template <typename Rhs>
00935 class NeMatcher : public ComparisonBase<NeMatcher<Rhs>, Rhs, AnyNe> {
00936 public:
00937 explicit NeMatcher(const Rhs& rhs)
00938 : ComparisonBase<NeMatcher<Rhs>, Rhs, AnyNe>(rhs) { }
00939 static const char* Desc() { return "isn't equal to"; }
00940 static const char* NegatedDesc() { return "is equal to"; }
00941 };
00942 template <typename Rhs>
00943 class LtMatcher : public ComparisonBase<LtMatcher<Rhs>, Rhs, AnyLt> {
00944 public:
00945 explicit LtMatcher(const Rhs& rhs)
00946 : ComparisonBase<LtMatcher<Rhs>, Rhs, AnyLt>(rhs) { }
00947 static const char* Desc() { return "is <"; }
00948 static const char* NegatedDesc() { return "isn't <"; }
00949 };
00950 template <typename Rhs>
00951 class GtMatcher : public ComparisonBase<GtMatcher<Rhs>, Rhs, AnyGt> {
00952 public:
00953 explicit GtMatcher(const Rhs& rhs)
00954 : ComparisonBase<GtMatcher<Rhs>, Rhs, AnyGt>(rhs) { }
00955 static const char* Desc() { return "is >"; }
00956 static const char* NegatedDesc() { return "isn't >"; }
00957 };
00958 template <typename Rhs>
00959 class LeMatcher : public ComparisonBase<LeMatcher<Rhs>, Rhs, AnyLe> {
00960 public:
00961 explicit LeMatcher(const Rhs& rhs)
00962 : ComparisonBase<LeMatcher<Rhs>, Rhs, AnyLe>(rhs) { }
00963 static const char* Desc() { return "is <="; }
00964 static const char* NegatedDesc() { return "isn't <="; }
00965 };
00966 template <typename Rhs>
00967 class GeMatcher : public ComparisonBase<GeMatcher<Rhs>, Rhs, AnyGe> {
00968 public:
00969 explicit GeMatcher(const Rhs& rhs)
00970 : ComparisonBase<GeMatcher<Rhs>, Rhs, AnyGe>(rhs) { }
00971 static const char* Desc() { return "is >="; }
00972 static const char* NegatedDesc() { return "isn't >="; }
00973 };
00974
00975
00976
00977 class IsNullMatcher {
00978 public:
00979 template <typename Pointer>
00980 bool MatchAndExplain(const Pointer& p,
00981 MatchResultListener* ) const {
00982 #if GTEST_LANG_CXX11
00983 return p == nullptr;
00984 #else // GTEST_LANG_CXX11
00985 return GetRawPointer(p) == NULL;
00986 #endif // GTEST_LANG_CXX11
00987 }
00988
00989 void DescribeTo(::std::ostream* os) const { *os << "is NULL"; }
00990 void DescribeNegationTo(::std::ostream* os) const {
00991 *os << "isn't NULL";
00992 }
00993 };
00994
00995
00996
00997 class NotNullMatcher {
00998 public:
00999 template <typename Pointer>
01000 bool MatchAndExplain(const Pointer& p,
01001 MatchResultListener* ) const {
01002 #if GTEST_LANG_CXX11
01003 return p != nullptr;
01004 #else // GTEST_LANG_CXX11
01005 return GetRawPointer(p) != NULL;
01006 #endif // GTEST_LANG_CXX11
01007 }
01008
01009 void DescribeTo(::std::ostream* os) const { *os << "isn't NULL"; }
01010 void DescribeNegationTo(::std::ostream* os) const {
01011 *os << "is NULL";
01012 }
01013 };
01014
01015
01016
01017
01018
01019
01020
01021
01022
01023
01024
01025
01026
01027
01028 template <typename T>
01029 class RefMatcher;
01030
01031 template <typename T>
01032 class RefMatcher<T&> {
01033
01034
01035
01036
01037
01038 public:
01039
01040
01041
01042 explicit RefMatcher(T& x) : object_(x) {}
01043
01044 template <typename Super>
01045 operator Matcher<Super&>() const {
01046
01047
01048
01049
01050
01051 return MakeMatcher(new Impl<Super>(object_));
01052 }
01053
01054 private:
01055 template <typename Super>
01056 class Impl : public MatcherInterface<Super&> {
01057 public:
01058 explicit Impl(Super& x) : object_(x) {}
01059
01060
01061
01062 virtual bool MatchAndExplain(
01063 Super& x, MatchResultListener* listener) const {
01064 *listener << "which is located @" << static_cast<const void*>(&x);
01065 return &x == &object_;
01066 }
01067
01068 virtual void DescribeTo(::std::ostream* os) const {
01069 *os << "references the variable ";
01070 UniversalPrinter<Super&>::Print(object_, os);
01071 }
01072
01073 virtual void DescribeNegationTo(::std::ostream* os) const {
01074 *os << "does not reference the variable ";
01075 UniversalPrinter<Super&>::Print(object_, os);
01076 }
01077
01078 private:
01079 const Super& object_;
01080
01081 GTEST_DISALLOW_ASSIGN_(Impl);
01082 };
01083
01084 T& object_;
01085
01086 GTEST_DISALLOW_ASSIGN_(RefMatcher);
01087 };
01088
01089
01090 inline bool CaseInsensitiveCStringEquals(const char* lhs, const char* rhs) {
01091 return String::CaseInsensitiveCStringEquals(lhs, rhs);
01092 }
01093
01094 inline bool CaseInsensitiveCStringEquals(const wchar_t* lhs,
01095 const wchar_t* rhs) {
01096 return String::CaseInsensitiveWideCStringEquals(lhs, rhs);
01097 }
01098
01099
01100
01101 template <typename StringType>
01102 bool CaseInsensitiveStringEquals(const StringType& s1,
01103 const StringType& s2) {
01104
01105 if (!CaseInsensitiveCStringEquals(s1.c_str(), s2.c_str())) {
01106 return false;
01107 }
01108
01109
01110 const typename StringType::value_type nul = 0;
01111 const size_t i1 = s1.find(nul), i2 = s2.find(nul);
01112
01113
01114 if (i1 == StringType::npos || i2 == StringType::npos) {
01115 return i1 == i2;
01116 }
01117
01118
01119 return CaseInsensitiveStringEquals(s1.substr(i1 + 1), s2.substr(i2 + 1));
01120 }
01121
01122
01123
01124
01125 template <typename StringType>
01126 class StrEqualityMatcher {
01127 public:
01128 StrEqualityMatcher(const StringType& str, bool expect_eq,
01129 bool case_sensitive)
01130 : string_(str), expect_eq_(expect_eq), case_sensitive_(case_sensitive) {}
01131
01132
01133
01134
01135
01136
01137 template <typename CharType>
01138 bool MatchAndExplain(CharType* s, MatchResultListener* listener) const {
01139 if (s == NULL) {
01140 return !expect_eq_;
01141 }
01142 return MatchAndExplain(StringType(s), listener);
01143 }
01144
01145
01146
01147
01148
01149 template <typename MatcheeStringType>
01150 bool MatchAndExplain(const MatcheeStringType& s,
01151 MatchResultListener* ) const {
01152 const StringType& s2(s);
01153 const bool eq = case_sensitive_ ? s2 == string_ :
01154 CaseInsensitiveStringEquals(s2, string_);
01155 return expect_eq_ == eq;
01156 }
01157
01158 void DescribeTo(::std::ostream* os) const {
01159 DescribeToHelper(expect_eq_, os);
01160 }
01161
01162 void DescribeNegationTo(::std::ostream* os) const {
01163 DescribeToHelper(!expect_eq_, os);
01164 }
01165
01166 private:
01167 void DescribeToHelper(bool expect_eq, ::std::ostream* os) const {
01168 *os << (expect_eq ? "is " : "isn't ");
01169 *os << "equal to ";
01170 if (!case_sensitive_) {
01171 *os << "(ignoring case) ";
01172 }
01173 UniversalPrint(string_, os);
01174 }
01175
01176 const StringType string_;
01177 const bool expect_eq_;
01178 const bool case_sensitive_;
01179
01180 GTEST_DISALLOW_ASSIGN_(StrEqualityMatcher);
01181 };
01182
01183
01184
01185
01186 template <typename StringType>
01187 class HasSubstrMatcher {
01188 public:
01189 explicit HasSubstrMatcher(const StringType& substring)
01190 : substring_(substring) {}
01191
01192
01193
01194
01195
01196
01197 template <typename CharType>
01198 bool MatchAndExplain(CharType* s, MatchResultListener* listener) const {
01199 return s != NULL && MatchAndExplain(StringType(s), listener);
01200 }
01201
01202
01203
01204
01205
01206 template <typename MatcheeStringType>
01207 bool MatchAndExplain(const MatcheeStringType& s,
01208 MatchResultListener* ) const {
01209 const StringType& s2(s);
01210 return s2.find(substring_) != StringType::npos;
01211 }
01212
01213
01214 void DescribeTo(::std::ostream* os) const {
01215 *os << "has substring ";
01216 UniversalPrint(substring_, os);
01217 }
01218
01219 void DescribeNegationTo(::std::ostream* os) const {
01220 *os << "has no substring ";
01221 UniversalPrint(substring_, os);
01222 }
01223
01224 private:
01225 const StringType substring_;
01226
01227 GTEST_DISALLOW_ASSIGN_(HasSubstrMatcher);
01228 };
01229
01230
01231
01232
01233 template <typename StringType>
01234 class StartsWithMatcher {
01235 public:
01236 explicit StartsWithMatcher(const StringType& prefix) : prefix_(prefix) {
01237 }
01238
01239
01240
01241
01242
01243
01244 template <typename CharType>
01245 bool MatchAndExplain(CharType* s, MatchResultListener* listener) const {
01246 return s != NULL && MatchAndExplain(StringType(s), listener);
01247 }
01248
01249
01250
01251
01252
01253 template <typename MatcheeStringType>
01254 bool MatchAndExplain(const MatcheeStringType& s,
01255 MatchResultListener* ) const {
01256 const StringType& s2(s);
01257 return s2.length() >= prefix_.length() &&
01258 s2.substr(0, prefix_.length()) == prefix_;
01259 }
01260
01261 void DescribeTo(::std::ostream* os) const {
01262 *os << "starts with ";
01263 UniversalPrint(prefix_, os);
01264 }
01265
01266 void DescribeNegationTo(::std::ostream* os) const {
01267 *os << "doesn't start with ";
01268 UniversalPrint(prefix_, os);
01269 }
01270
01271 private:
01272 const StringType prefix_;
01273
01274 GTEST_DISALLOW_ASSIGN_(StartsWithMatcher);
01275 };
01276
01277
01278
01279
01280 template <typename StringType>
01281 class EndsWithMatcher {
01282 public:
01283 explicit EndsWithMatcher(const StringType& suffix) : suffix_(suffix) {}
01284
01285
01286
01287
01288
01289
01290 template <typename CharType>
01291 bool MatchAndExplain(CharType* s, MatchResultListener* listener) const {
01292 return s != NULL && MatchAndExplain(StringType(s), listener);
01293 }
01294
01295
01296
01297
01298
01299 template <typename MatcheeStringType>
01300 bool MatchAndExplain(const MatcheeStringType& s,
01301 MatchResultListener* ) const {
01302 const StringType& s2(s);
01303 return s2.length() >= suffix_.length() &&
01304 s2.substr(s2.length() - suffix_.length()) == suffix_;
01305 }
01306
01307 void DescribeTo(::std::ostream* os) const {
01308 *os << "ends with ";
01309 UniversalPrint(suffix_, os);
01310 }
01311
01312 void DescribeNegationTo(::std::ostream* os) const {
01313 *os << "doesn't end with ";
01314 UniversalPrint(suffix_, os);
01315 }
01316
01317 private:
01318 const StringType suffix_;
01319
01320 GTEST_DISALLOW_ASSIGN_(EndsWithMatcher);
01321 };
01322
01323
01324
01325
01326 class MatchesRegexMatcher {
01327 public:
01328 MatchesRegexMatcher(const RE* regex, bool full_match)
01329 : regex_(regex), full_match_(full_match) {}
01330
01331
01332
01333
01334
01335
01336 template <typename CharType>
01337 bool MatchAndExplain(CharType* s, MatchResultListener* listener) const {
01338 return s != NULL && MatchAndExplain(internal::string(s), listener);
01339 }
01340
01341
01342
01343
01344
01345 template <class MatcheeStringType>
01346 bool MatchAndExplain(const MatcheeStringType& s,
01347 MatchResultListener* ) const {
01348 const internal::string& s2(s);
01349 return full_match_ ? RE::FullMatch(s2, *regex_) :
01350 RE::PartialMatch(s2, *regex_);
01351 }
01352
01353 void DescribeTo(::std::ostream* os) const {
01354 *os << (full_match_ ? "matches" : "contains")
01355 << " regular expression ";
01356 UniversalPrinter<internal::string>::Print(regex_->pattern(), os);
01357 }
01358
01359 void DescribeNegationTo(::std::ostream* os) const {
01360 *os << "doesn't " << (full_match_ ? "match" : "contain")
01361 << " regular expression ";
01362 UniversalPrinter<internal::string>::Print(regex_->pattern(), os);
01363 }
01364
01365 private:
01366 const internal::linked_ptr<const RE> regex_;
01367 const bool full_match_;
01368
01369 GTEST_DISALLOW_ASSIGN_(MatchesRegexMatcher);
01370 };
01371
01372
01373
01374
01375
01376
01377
01378
01379
01380 template <typename D, typename Op>
01381 class PairMatchBase {
01382 public:
01383 template <typename T1, typename T2>
01384 operator Matcher< ::testing::tuple<T1, T2> >() const {
01385 return MakeMatcher(new Impl< ::testing::tuple<T1, T2> >);
01386 }
01387 template <typename T1, typename T2>
01388 operator Matcher<const ::testing::tuple<T1, T2>&>() const {
01389 return MakeMatcher(new Impl<const ::testing::tuple<T1, T2>&>);
01390 }
01391
01392 private:
01393 static ::std::ostream& GetDesc(::std::ostream& os) {
01394 return os << D::Desc();
01395 }
01396
01397 template <typename Tuple>
01398 class Impl : public MatcherInterface<Tuple> {
01399 public:
01400 virtual bool MatchAndExplain(
01401 Tuple args,
01402 MatchResultListener* ) const {
01403 return Op()(::testing::get<0>(args), ::testing::get<1>(args));
01404 }
01405 virtual void DescribeTo(::std::ostream* os) const {
01406 *os << "are " << GetDesc;
01407 }
01408 virtual void DescribeNegationTo(::std::ostream* os) const {
01409 *os << "aren't " << GetDesc;
01410 }
01411 };
01412 };
01413
01414 class Eq2Matcher : public PairMatchBase<Eq2Matcher, AnyEq> {
01415 public:
01416 static const char* Desc() { return "an equal pair"; }
01417 };
01418 class Ne2Matcher : public PairMatchBase<Ne2Matcher, AnyNe> {
01419 public:
01420 static const char* Desc() { return "an unequal pair"; }
01421 };
01422 class Lt2Matcher : public PairMatchBase<Lt2Matcher, AnyLt> {
01423 public:
01424 static const char* Desc() { return "a pair where the first < the second"; }
01425 };
01426 class Gt2Matcher : public PairMatchBase<Gt2Matcher, AnyGt> {
01427 public:
01428 static const char* Desc() { return "a pair where the first > the second"; }
01429 };
01430 class Le2Matcher : public PairMatchBase<Le2Matcher, AnyLe> {
01431 public:
01432 static const char* Desc() { return "a pair where the first <= the second"; }
01433 };
01434 class Ge2Matcher : public PairMatchBase<Ge2Matcher, AnyGe> {
01435 public:
01436 static const char* Desc() { return "a pair where the first >= the second"; }
01437 };
01438
01439
01440
01441
01442
01443 template <typename T>
01444 class NotMatcherImpl : public MatcherInterface<T> {
01445 public:
01446 explicit NotMatcherImpl(const Matcher<T>& matcher)
01447 : matcher_(matcher) {}
01448
01449 virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
01450 return !matcher_.MatchAndExplain(x, listener);
01451 }
01452
01453 virtual void DescribeTo(::std::ostream* os) const {
01454 matcher_.DescribeNegationTo(os);
01455 }
01456
01457 virtual void DescribeNegationTo(::std::ostream* os) const {
01458 matcher_.DescribeTo(os);
01459 }
01460
01461 private:
01462 const Matcher<T> matcher_;
01463
01464 GTEST_DISALLOW_ASSIGN_(NotMatcherImpl);
01465 };
01466
01467
01468
01469 template <typename InnerMatcher>
01470 class NotMatcher {
01471 public:
01472 explicit NotMatcher(InnerMatcher matcher) : matcher_(matcher) {}
01473
01474
01475
01476 template <typename T>
01477 operator Matcher<T>() const {
01478 return Matcher<T>(new NotMatcherImpl<T>(SafeMatcherCast<T>(matcher_)));
01479 }
01480
01481 private:
01482 InnerMatcher matcher_;
01483
01484 GTEST_DISALLOW_ASSIGN_(NotMatcher);
01485 };
01486
01487
01488
01489
01490
01491 template <typename T>
01492 class BothOfMatcherImpl : public MatcherInterface<T> {
01493 public:
01494 BothOfMatcherImpl(const Matcher<T>& matcher1, const Matcher<T>& matcher2)
01495 : matcher1_(matcher1), matcher2_(matcher2) {}
01496
01497 virtual void DescribeTo(::std::ostream* os) const {
01498 *os << "(";
01499 matcher1_.DescribeTo(os);
01500 *os << ") and (";
01501 matcher2_.DescribeTo(os);
01502 *os << ")";
01503 }
01504
01505 virtual void DescribeNegationTo(::std::ostream* os) const {
01506 *os << "(";
01507 matcher1_.DescribeNegationTo(os);
01508 *os << ") or (";
01509 matcher2_.DescribeNegationTo(os);
01510 *os << ")";
01511 }
01512
01513 virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
01514
01515
01516 StringMatchResultListener listener1;
01517 if (!matcher1_.MatchAndExplain(x, &listener1)) {
01518 *listener << listener1.str();
01519 return false;
01520 }
01521
01522 StringMatchResultListener listener2;
01523 if (!matcher2_.MatchAndExplain(x, &listener2)) {
01524 *listener << listener2.str();
01525 return false;
01526 }
01527
01528
01529 const internal::string s1 = listener1.str();
01530 const internal::string s2 = listener2.str();
01531
01532 if (s1 == "") {
01533 *listener << s2;
01534 } else {
01535 *listener << s1;
01536 if (s2 != "") {
01537 *listener << ", and " << s2;
01538 }
01539 }
01540 return true;
01541 }
01542
01543 private:
01544 const Matcher<T> matcher1_;
01545 const Matcher<T> matcher2_;
01546
01547 GTEST_DISALLOW_ASSIGN_(BothOfMatcherImpl);
01548 };
01549
01550 #if GTEST_LANG_CXX11
01551
01552
01553
01554
01555
01556
01557
01558 template <int kSize, typename Head, typename... Tail>
01559 struct MatcherList {
01560 typedef MatcherList<kSize - 1, Tail...> MatcherListTail;
01561 typedef ::std::pair<Head, typename MatcherListTail::ListType> ListType;
01562
01563
01564
01565
01566
01567 static ListType BuildList(const Head& matcher, const Tail&... tail) {
01568 return ListType(matcher, MatcherListTail::BuildList(tail...));
01569 }
01570
01571
01572
01573
01574
01575 template <typename T, template <typename > class CombiningMatcher>
01576 static Matcher<T> CreateMatcher(const ListType& matchers) {
01577 return Matcher<T>(new CombiningMatcher<T>(
01578 SafeMatcherCast<T>(matchers.first),
01579 MatcherListTail::template CreateMatcher<T, CombiningMatcher>(
01580 matchers.second)));
01581 }
01582 };
01583
01584
01585
01586 template <typename Matcher1, typename Matcher2>
01587 struct MatcherList<2, Matcher1, Matcher2> {
01588 typedef ::std::pair<Matcher1, Matcher2> ListType;
01589
01590 static ListType BuildList(const Matcher1& matcher1,
01591 const Matcher2& matcher2) {
01592 return ::std::pair<Matcher1, Matcher2>(matcher1, matcher2);
01593 }
01594
01595 template <typename T, template <typename > class CombiningMatcher>
01596 static Matcher<T> CreateMatcher(const ListType& matchers) {
01597 return Matcher<T>(new CombiningMatcher<T>(
01598 SafeMatcherCast<T>(matchers.first),
01599 SafeMatcherCast<T>(matchers.second)));
01600 }
01601 };
01602
01603
01604
01605
01606
01607 template <template <typename T> class CombiningMatcher, typename... Args>
01608 class VariadicMatcher {
01609 public:
01610 VariadicMatcher(const Args&... matchers)
01611 : matchers_(MatcherListType::BuildList(matchers...)) {}
01612
01613
01614
01615
01616 template <typename T>
01617 operator Matcher<T>() const {
01618 return MatcherListType::template CreateMatcher<T, CombiningMatcher>(
01619 matchers_);
01620 }
01621
01622 private:
01623 typedef MatcherList<sizeof...(Args), Args...> MatcherListType;
01624
01625 const typename MatcherListType::ListType matchers_;
01626
01627 GTEST_DISALLOW_ASSIGN_(VariadicMatcher);
01628 };
01629
01630 template <typename... Args>
01631 using AllOfMatcher = VariadicMatcher<BothOfMatcherImpl, Args...>;
01632
01633 #endif // GTEST_LANG_CXX11
01634
01635
01636
01637 template <typename Matcher1, typename Matcher2>
01638 class BothOfMatcher {
01639 public:
01640 BothOfMatcher(Matcher1 matcher1, Matcher2 matcher2)
01641 : matcher1_(matcher1), matcher2_(matcher2) {}
01642
01643
01644
01645
01646 template <typename T>
01647 operator Matcher<T>() const {
01648 return Matcher<T>(new BothOfMatcherImpl<T>(SafeMatcherCast<T>(matcher1_),
01649 SafeMatcherCast<T>(matcher2_)));
01650 }
01651
01652 private:
01653 Matcher1 matcher1_;
01654 Matcher2 matcher2_;
01655
01656 GTEST_DISALLOW_ASSIGN_(BothOfMatcher);
01657 };
01658
01659
01660
01661
01662
01663 template <typename T>
01664 class EitherOfMatcherImpl : public MatcherInterface<T> {
01665 public:
01666 EitherOfMatcherImpl(const Matcher<T>& matcher1, const Matcher<T>& matcher2)
01667 : matcher1_(matcher1), matcher2_(matcher2) {}
01668
01669 virtual void DescribeTo(::std::ostream* os) const {
01670 *os << "(";
01671 matcher1_.DescribeTo(os);
01672 *os << ") or (";
01673 matcher2_.DescribeTo(os);
01674 *os << ")";
01675 }
01676
01677 virtual void DescribeNegationTo(::std::ostream* os) const {
01678 *os << "(";
01679 matcher1_.DescribeNegationTo(os);
01680 *os << ") and (";
01681 matcher2_.DescribeNegationTo(os);
01682 *os << ")";
01683 }
01684
01685 virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
01686
01687
01688 StringMatchResultListener listener1;
01689 if (matcher1_.MatchAndExplain(x, &listener1)) {
01690 *listener << listener1.str();
01691 return true;
01692 }
01693
01694 StringMatchResultListener listener2;
01695 if (matcher2_.MatchAndExplain(x, &listener2)) {
01696 *listener << listener2.str();
01697 return true;
01698 }
01699
01700
01701 const internal::string s1 = listener1.str();
01702 const internal::string s2 = listener2.str();
01703
01704 if (s1 == "") {
01705 *listener << s2;
01706 } else {
01707 *listener << s1;
01708 if (s2 != "") {
01709 *listener << ", and " << s2;
01710 }
01711 }
01712 return false;
01713 }
01714
01715 private:
01716 const Matcher<T> matcher1_;
01717 const Matcher<T> matcher2_;
01718
01719 GTEST_DISALLOW_ASSIGN_(EitherOfMatcherImpl);
01720 };
01721
01722 #if GTEST_LANG_CXX11
01723
01724 template <typename... Args>
01725 using AnyOfMatcher = VariadicMatcher<EitherOfMatcherImpl, Args...>;
01726
01727 #endif // GTEST_LANG_CXX11
01728
01729
01730
01731
01732 template <typename Matcher1, typename Matcher2>
01733 class EitherOfMatcher {
01734 public:
01735 EitherOfMatcher(Matcher1 matcher1, Matcher2 matcher2)
01736 : matcher1_(matcher1), matcher2_(matcher2) {}
01737
01738
01739
01740
01741 template <typename T>
01742 operator Matcher<T>() const {
01743 return Matcher<T>(new EitherOfMatcherImpl<T>(
01744 SafeMatcherCast<T>(matcher1_), SafeMatcherCast<T>(matcher2_)));
01745 }
01746
01747 private:
01748 Matcher1 matcher1_;
01749 Matcher2 matcher2_;
01750
01751 GTEST_DISALLOW_ASSIGN_(EitherOfMatcher);
01752 };
01753
01754
01755
01756 template <typename Predicate>
01757 class TrulyMatcher {
01758 public:
01759 explicit TrulyMatcher(Predicate pred) : predicate_(pred) {}
01760
01761
01762
01763
01764
01765 template <typename T>
01766 bool MatchAndExplain(T& x,
01767 MatchResultListener* ) const {
01768
01769
01770
01771
01772
01773
01774 if (predicate_(x))
01775 return true;
01776 return false;
01777 }
01778
01779 void DescribeTo(::std::ostream* os) const {
01780 *os << "satisfies the given predicate";
01781 }
01782
01783 void DescribeNegationTo(::std::ostream* os) const {
01784 *os << "doesn't satisfy the given predicate";
01785 }
01786
01787 private:
01788 Predicate predicate_;
01789
01790 GTEST_DISALLOW_ASSIGN_(TrulyMatcher);
01791 };
01792
01793
01794
01795 template <typename M>
01796 class MatcherAsPredicate {
01797 public:
01798 explicit MatcherAsPredicate(M matcher) : matcher_(matcher) {}
01799
01800
01801
01802
01803
01804
01805
01806 template <typename T>
01807 bool operator()(const T& x) const {
01808
01809
01810
01811
01812
01813
01814
01815
01816
01817
01818
01819
01820
01821
01822 return MatcherCast<const T&>(matcher_).Matches(x);
01823 }
01824
01825 private:
01826 M matcher_;
01827
01828 GTEST_DISALLOW_ASSIGN_(MatcherAsPredicate);
01829 };
01830
01831
01832
01833 template <typename M>
01834 class PredicateFormatterFromMatcher {
01835 public:
01836 explicit PredicateFormatterFromMatcher(M m) : matcher_(internal::move(m)) {}
01837
01838
01839
01840
01841 template <typename T>
01842 AssertionResult operator()(const char* value_text, const T& x) const {
01843
01844
01845
01846
01847
01848
01849
01850
01851
01852
01853
01854 const Matcher<const T&> matcher = SafeMatcherCast<const T&>(matcher_);
01855 StringMatchResultListener listener;
01856 if (MatchPrintAndExplain(x, matcher, &listener))
01857 return AssertionSuccess();
01858
01859 ::std::stringstream ss;
01860 ss << "Value of: " << value_text << "\n"
01861 << "Expected: ";
01862 matcher.DescribeTo(&ss);
01863 ss << "\n Actual: " << listener.str();
01864 return AssertionFailure() << ss.str();
01865 }
01866
01867 private:
01868 const M matcher_;
01869
01870 GTEST_DISALLOW_ASSIGN_(PredicateFormatterFromMatcher);
01871 };
01872
01873
01874
01875
01876
01877 template <typename M>
01878 inline PredicateFormatterFromMatcher<M>
01879 MakePredicateFormatterFromMatcher(M matcher) {
01880 return PredicateFormatterFromMatcher<M>(internal::move(matcher));
01881 }
01882
01883
01884
01885
01886
01887 template <typename FloatType>
01888 class FloatingEqMatcher {
01889 public:
01890
01891
01892
01893
01894
01895
01896 FloatingEqMatcher(FloatType expected, bool nan_eq_nan) :
01897 expected_(expected), nan_eq_nan_(nan_eq_nan), max_abs_error_(-1) {
01898 }
01899
01900
01901
01902
01903 FloatingEqMatcher(FloatType expected, bool nan_eq_nan,
01904 FloatType max_abs_error)
01905 : expected_(expected),
01906 nan_eq_nan_(nan_eq_nan),
01907 max_abs_error_(max_abs_error) {
01908 GTEST_CHECK_(max_abs_error >= 0)
01909 << ", where max_abs_error is" << max_abs_error;
01910 }
01911
01912
01913 template <typename T>
01914 class Impl : public MatcherInterface<T> {
01915 public:
01916 Impl(FloatType expected, bool nan_eq_nan, FloatType max_abs_error)
01917 : expected_(expected),
01918 nan_eq_nan_(nan_eq_nan),
01919 max_abs_error_(max_abs_error) {}
01920
01921 virtual bool MatchAndExplain(T value,
01922 MatchResultListener* listener) const {
01923 const FloatingPoint<FloatType> actual(value), expected(expected_);
01924
01925
01926 if (actual.is_nan() || expected.is_nan()) {
01927 if (actual.is_nan() && expected.is_nan()) {
01928 return nan_eq_nan_;
01929 }
01930
01931 return false;
01932 }
01933 if (HasMaxAbsError()) {
01934
01935
01936
01937
01938 if (value == expected_) {
01939 return true;
01940 }
01941
01942 const FloatType diff = value - expected_;
01943 if (fabs(diff) <= max_abs_error_) {
01944 return true;
01945 }
01946
01947 if (listener->IsInterested()) {
01948 *listener << "which is " << diff << " from " << expected_;
01949 }
01950 return false;
01951 } else {
01952 return actual.AlmostEquals(expected);
01953 }
01954 }
01955
01956 virtual void DescribeTo(::std::ostream* os) const {
01957
01958
01959
01960 const ::std::streamsize old_precision = os->precision(
01961 ::std::numeric_limits<FloatType>::digits10 + 2);
01962 if (FloatingPoint<FloatType>(expected_).is_nan()) {
01963 if (nan_eq_nan_) {
01964 *os << "is NaN";
01965 } else {
01966 *os << "never matches";
01967 }
01968 } else {
01969 *os << "is approximately " << expected_;
01970 if (HasMaxAbsError()) {
01971 *os << " (absolute error <= " << max_abs_error_ << ")";
01972 }
01973 }
01974 os->precision(old_precision);
01975 }
01976
01977 virtual void DescribeNegationTo(::std::ostream* os) const {
01978
01979 const ::std::streamsize old_precision = os->precision(
01980 ::std::numeric_limits<FloatType>::digits10 + 2);
01981 if (FloatingPoint<FloatType>(expected_).is_nan()) {
01982 if (nan_eq_nan_) {
01983 *os << "isn't NaN";
01984 } else {
01985 *os << "is anything";
01986 }
01987 } else {
01988 *os << "isn't approximately " << expected_;
01989 if (HasMaxAbsError()) {
01990 *os << " (absolute error > " << max_abs_error_ << ")";
01991 }
01992 }
01993
01994 os->precision(old_precision);
01995 }
01996
01997 private:
01998 bool HasMaxAbsError() const {
01999 return max_abs_error_ >= 0;
02000 }
02001
02002 const FloatType expected_;
02003 const bool nan_eq_nan_;
02004
02005 const FloatType max_abs_error_;
02006
02007 GTEST_DISALLOW_ASSIGN_(Impl);
02008 };
02009
02010
02011
02012
02013
02014
02015
02016 operator Matcher<FloatType>() const {
02017 return MakeMatcher(
02018 new Impl<FloatType>(expected_, nan_eq_nan_, max_abs_error_));
02019 }
02020
02021 operator Matcher<const FloatType&>() const {
02022 return MakeMatcher(
02023 new Impl<const FloatType&>(expected_, nan_eq_nan_, max_abs_error_));
02024 }
02025
02026 operator Matcher<FloatType&>() const {
02027 return MakeMatcher(
02028 new Impl<FloatType&>(expected_, nan_eq_nan_, max_abs_error_));
02029 }
02030
02031 private:
02032 const FloatType expected_;
02033 const bool nan_eq_nan_;
02034
02035 const FloatType max_abs_error_;
02036
02037 GTEST_DISALLOW_ASSIGN_(FloatingEqMatcher);
02038 };
02039
02040
02041
02042 template <typename InnerMatcher>
02043 class PointeeMatcher {
02044 public:
02045 explicit PointeeMatcher(const InnerMatcher& matcher) : matcher_(matcher) {}
02046
02047
02048
02049
02050
02051
02052
02053
02054
02055 template <typename Pointer>
02056 operator Matcher<Pointer>() const {
02057 return MakeMatcher(new Impl<Pointer>(matcher_));
02058 }
02059
02060 private:
02061
02062 template <typename Pointer>
02063 class Impl : public MatcherInterface<Pointer> {
02064 public:
02065 typedef typename PointeeOf<GTEST_REMOVE_CONST_(
02066 GTEST_REMOVE_REFERENCE_(Pointer))>::type Pointee;
02067
02068 explicit Impl(const InnerMatcher& matcher)
02069 : matcher_(MatcherCast<const Pointee&>(matcher)) {}
02070
02071 virtual void DescribeTo(::std::ostream* os) const {
02072 *os << "points to a value that ";
02073 matcher_.DescribeTo(os);
02074 }
02075
02076 virtual void DescribeNegationTo(::std::ostream* os) const {
02077 *os << "does not point to a value that ";
02078 matcher_.DescribeTo(os);
02079 }
02080
02081 virtual bool MatchAndExplain(Pointer pointer,
02082 MatchResultListener* listener) const {
02083 if (GetRawPointer(pointer) == NULL)
02084 return false;
02085
02086 *listener << "which points to ";
02087 return MatchPrintAndExplain(*pointer, matcher_, listener);
02088 }
02089
02090 private:
02091 const Matcher<const Pointee&> matcher_;
02092
02093 GTEST_DISALLOW_ASSIGN_(Impl);
02094 };
02095
02096 const InnerMatcher matcher_;
02097
02098 GTEST_DISALLOW_ASSIGN_(PointeeMatcher);
02099 };
02100
02101
02102
02103
02104
02105
02106
02107 template <typename To>
02108 class WhenDynamicCastToMatcherBase {
02109 public:
02110 explicit WhenDynamicCastToMatcherBase(const Matcher<To>& matcher)
02111 : matcher_(matcher) {}
02112
02113 void DescribeTo(::std::ostream* os) const {
02114 GetCastTypeDescription(os);
02115 matcher_.DescribeTo(os);
02116 }
02117
02118 void DescribeNegationTo(::std::ostream* os) const {
02119 GetCastTypeDescription(os);
02120 matcher_.DescribeNegationTo(os);
02121 }
02122
02123 protected:
02124 const Matcher<To> matcher_;
02125
02126 static string GetToName() {
02127 #if GTEST_HAS_RTTI
02128 return GetTypeName<To>();
02129 #else // GTEST_HAS_RTTI
02130 return "the target type";
02131 #endif // GTEST_HAS_RTTI
02132 }
02133
02134 private:
02135 static void GetCastTypeDescription(::std::ostream* os) {
02136 *os << "when dynamic_cast to " << GetToName() << ", ";
02137 }
02138
02139 GTEST_DISALLOW_ASSIGN_(WhenDynamicCastToMatcherBase);
02140 };
02141
02142
02143
02144 template <typename To>
02145 class WhenDynamicCastToMatcher : public WhenDynamicCastToMatcherBase<To> {
02146 public:
02147 explicit WhenDynamicCastToMatcher(const Matcher<To>& matcher)
02148 : WhenDynamicCastToMatcherBase<To>(matcher) {}
02149
02150 template <typename From>
02151 bool MatchAndExplain(From from, MatchResultListener* listener) const {
02152
02153 To to = dynamic_cast<To>(from);
02154 return MatchPrintAndExplain(to, this->matcher_, listener);
02155 }
02156 };
02157
02158
02159
02160 template <typename To>
02161 class WhenDynamicCastToMatcher<To&> : public WhenDynamicCastToMatcherBase<To&> {
02162 public:
02163 explicit WhenDynamicCastToMatcher(const Matcher<To&>& matcher)
02164 : WhenDynamicCastToMatcherBase<To&>(matcher) {}
02165
02166 template <typename From>
02167 bool MatchAndExplain(From& from, MatchResultListener* listener) const {
02168
02169 To* to = dynamic_cast<To*>(&from);
02170 if (to == NULL) {
02171 *listener << "which cannot be dynamic_cast to " << this->GetToName();
02172 return false;
02173 }
02174 return MatchPrintAndExplain(*to, this->matcher_, listener);
02175 }
02176 };
02177
02178
02179
02180 template <typename Class, typename FieldType>
02181 class FieldMatcher {
02182 public:
02183 FieldMatcher(FieldType Class::*field,
02184 const Matcher<const FieldType&>& matcher)
02185 : field_(field), matcher_(matcher) {}
02186
02187 void DescribeTo(::std::ostream* os) const {
02188 *os << "is an object whose given field ";
02189 matcher_.DescribeTo(os);
02190 }
02191
02192 void DescribeNegationTo(::std::ostream* os) const {
02193 *os << "is an object whose given field ";
02194 matcher_.DescribeNegationTo(os);
02195 }
02196
02197 template <typename T>
02198 bool MatchAndExplain(const T& value, MatchResultListener* listener) const {
02199 return MatchAndExplainImpl(
02200 typename ::testing::internal::
02201 is_pointer<GTEST_REMOVE_CONST_(T)>::type(),
02202 value, listener);
02203 }
02204
02205 private:
02206
02207
02208
02209 bool MatchAndExplainImpl(false_type , const Class& obj,
02210 MatchResultListener* listener) const {
02211 *listener << "whose given field is ";
02212 return MatchPrintAndExplain(obj.*field_, matcher_, listener);
02213 }
02214
02215 bool MatchAndExplainImpl(true_type , const Class* p,
02216 MatchResultListener* listener) const {
02217 if (p == NULL)
02218 return false;
02219
02220 *listener << "which points to an object ";
02221
02222
02223
02224 return MatchAndExplainImpl(false_type(), *p, listener);
02225 }
02226
02227 const FieldType Class::*field_;
02228 const Matcher<const FieldType&> matcher_;
02229
02230 GTEST_DISALLOW_ASSIGN_(FieldMatcher);
02231 };
02232
02233
02234
02235 template <typename Class, typename PropertyType>
02236 class PropertyMatcher {
02237 public:
02238
02239
02240
02241
02242 typedef GTEST_REFERENCE_TO_CONST_(PropertyType) RefToConstProperty;
02243
02244 PropertyMatcher(PropertyType (Class::*property)() const,
02245 const Matcher<RefToConstProperty>& matcher)
02246 : property_(property), matcher_(matcher) {}
02247
02248 void DescribeTo(::std::ostream* os) const {
02249 *os << "is an object whose given property ";
02250 matcher_.DescribeTo(os);
02251 }
02252
02253 void DescribeNegationTo(::std::ostream* os) const {
02254 *os << "is an object whose given property ";
02255 matcher_.DescribeNegationTo(os);
02256 }
02257
02258 template <typename T>
02259 bool MatchAndExplain(const T&value, MatchResultListener* listener) const {
02260 return MatchAndExplainImpl(
02261 typename ::testing::internal::
02262 is_pointer<GTEST_REMOVE_CONST_(T)>::type(),
02263 value, listener);
02264 }
02265
02266 private:
02267
02268
02269
02270 bool MatchAndExplainImpl(false_type , const Class& obj,
02271 MatchResultListener* listener) const {
02272 *listener << "whose given property is ";
02273
02274
02275 #if defined(_PREFAST_ ) && _MSC_VER == 1800
02276
02277
02278 posix::Abort();
02279 return false;
02280 #else
02281 RefToConstProperty result = (obj.*property_)();
02282 return MatchPrintAndExplain(result, matcher_, listener);
02283 #endif
02284 }
02285
02286 bool MatchAndExplainImpl(true_type , const Class* p,
02287 MatchResultListener* listener) const {
02288 if (p == NULL)
02289 return false;
02290
02291 *listener << "which points to an object ";
02292
02293
02294
02295 return MatchAndExplainImpl(false_type(), *p, listener);
02296 }
02297
02298 PropertyType (Class::*property_)() const;
02299 const Matcher<RefToConstProperty> matcher_;
02300
02301 GTEST_DISALLOW_ASSIGN_(PropertyMatcher);
02302 };
02303
02304
02305
02306
02307
02308 template <typename Functor>
02309 struct CallableTraits {
02310 typedef typename Functor::result_type ResultType;
02311 typedef Functor StorageType;
02312
02313 static void CheckIsValid(Functor ) {}
02314 template <typename T>
02315 static ResultType Invoke(Functor f, T arg) { return f(arg); }
02316 };
02317
02318
02319 template <typename ArgType, typename ResType>
02320 struct CallableTraits<ResType(*)(ArgType)> {
02321 typedef ResType ResultType;
02322 typedef ResType(*StorageType)(ArgType);
02323
02324 static void CheckIsValid(ResType(*f)(ArgType)) {
02325 GTEST_CHECK_(f != NULL)
02326 << "NULL function pointer is passed into ResultOf().";
02327 }
02328 template <typename T>
02329 static ResType Invoke(ResType(*f)(ArgType), T arg) {
02330 return (*f)(arg);
02331 }
02332 };
02333
02334
02335
02336 template <typename Callable>
02337 class ResultOfMatcher {
02338 public:
02339 typedef typename CallableTraits<Callable>::ResultType ResultType;
02340
02341 ResultOfMatcher(Callable callable, const Matcher<ResultType>& matcher)
02342 : callable_(callable), matcher_(matcher) {
02343 CallableTraits<Callable>::CheckIsValid(callable_);
02344 }
02345
02346 template <typename T>
02347 operator Matcher<T>() const {
02348 return Matcher<T>(new Impl<T>(callable_, matcher_));
02349 }
02350
02351 private:
02352 typedef typename CallableTraits<Callable>::StorageType CallableStorageType;
02353
02354 template <typename T>
02355 class Impl : public MatcherInterface<T> {
02356 public:
02357 Impl(CallableStorageType callable, const Matcher<ResultType>& matcher)
02358 : callable_(callable), matcher_(matcher) {}
02359
02360 virtual void DescribeTo(::std::ostream* os) const {
02361 *os << "is mapped by the given callable to a value that ";
02362 matcher_.DescribeTo(os);
02363 }
02364
02365 virtual void DescribeNegationTo(::std::ostream* os) const {
02366 *os << "is mapped by the given callable to a value that ";
02367 matcher_.DescribeNegationTo(os);
02368 }
02369
02370 virtual bool MatchAndExplain(T obj, MatchResultListener* listener) const {
02371 *listener << "which is mapped by the given callable to ";
02372
02373
02374 ResultType result =
02375 CallableTraits<Callable>::template Invoke<T>(callable_, obj);
02376 return MatchPrintAndExplain(result, matcher_, listener);
02377 }
02378
02379 private:
02380
02381
02382
02383
02384
02385 mutable CallableStorageType callable_;
02386 const Matcher<ResultType> matcher_;
02387
02388 GTEST_DISALLOW_ASSIGN_(Impl);
02389 };
02390
02391 const CallableStorageType callable_;
02392 const Matcher<ResultType> matcher_;
02393
02394 GTEST_DISALLOW_ASSIGN_(ResultOfMatcher);
02395 };
02396
02397
02398 template <typename SizeMatcher>
02399 class SizeIsMatcher {
02400 public:
02401 explicit SizeIsMatcher(const SizeMatcher& size_matcher)
02402 : size_matcher_(size_matcher) {
02403 }
02404
02405 template <typename Container>
02406 operator Matcher<Container>() const {
02407 return MakeMatcher(new Impl<Container>(size_matcher_));
02408 }
02409
02410 template <typename Container>
02411 class Impl : public MatcherInterface<Container> {
02412 public:
02413 typedef internal::StlContainerView<
02414 GTEST_REMOVE_REFERENCE_AND_CONST_(Container)> ContainerView;
02415 typedef typename ContainerView::type::size_type SizeType;
02416 explicit Impl(const SizeMatcher& size_matcher)
02417 : size_matcher_(MatcherCast<SizeType>(size_matcher)) {}
02418
02419 virtual void DescribeTo(::std::ostream* os) const {
02420 *os << "size ";
02421 size_matcher_.DescribeTo(os);
02422 }
02423 virtual void DescribeNegationTo(::std::ostream* os) const {
02424 *os << "size ";
02425 size_matcher_.DescribeNegationTo(os);
02426 }
02427
02428 virtual bool MatchAndExplain(Container container,
02429 MatchResultListener* listener) const {
02430 SizeType size = container.size();
02431 StringMatchResultListener size_listener;
02432 const bool result = size_matcher_.MatchAndExplain(size, &size_listener);
02433 *listener
02434 << "whose size " << size << (result ? " matches" : " doesn't match");
02435 PrintIfNotEmpty(size_listener.str(), listener->stream());
02436 return result;
02437 }
02438
02439 private:
02440 const Matcher<SizeType> size_matcher_;
02441 GTEST_DISALLOW_ASSIGN_(Impl);
02442 };
02443
02444 private:
02445 const SizeMatcher size_matcher_;
02446 GTEST_DISALLOW_ASSIGN_(SizeIsMatcher);
02447 };
02448
02449
02450
02451 template <typename DistanceMatcher>
02452 class BeginEndDistanceIsMatcher {
02453 public:
02454 explicit BeginEndDistanceIsMatcher(const DistanceMatcher& distance_matcher)
02455 : distance_matcher_(distance_matcher) {}
02456
02457 template <typename Container>
02458 operator Matcher<Container>() const {
02459 return MakeMatcher(new Impl<Container>(distance_matcher_));
02460 }
02461
02462 template <typename Container>
02463 class Impl : public MatcherInterface<Container> {
02464 public:
02465 typedef internal::StlContainerView<
02466 GTEST_REMOVE_REFERENCE_AND_CONST_(Container)> ContainerView;
02467 typedef typename std::iterator_traits<
02468 typename ContainerView::type::const_iterator>::difference_type
02469 DistanceType;
02470 explicit Impl(const DistanceMatcher& distance_matcher)
02471 : distance_matcher_(MatcherCast<DistanceType>(distance_matcher)) {}
02472
02473 virtual void DescribeTo(::std::ostream* os) const {
02474 *os << "distance between begin() and end() ";
02475 distance_matcher_.DescribeTo(os);
02476 }
02477 virtual void DescribeNegationTo(::std::ostream* os) const {
02478 *os << "distance between begin() and end() ";
02479 distance_matcher_.DescribeNegationTo(os);
02480 }
02481
02482 virtual bool MatchAndExplain(Container container,
02483 MatchResultListener* listener) const {
02484 #if GTEST_HAS_STD_BEGIN_AND_END_
02485 using std::begin;
02486 using std::end;
02487 DistanceType distance = std::distance(begin(container), end(container));
02488 #else
02489 DistanceType distance = std::distance(container.begin(), container.end());
02490 #endif
02491 StringMatchResultListener distance_listener;
02492 const bool result =
02493 distance_matcher_.MatchAndExplain(distance, &distance_listener);
02494 *listener << "whose distance between begin() and end() " << distance
02495 << (result ? " matches" : " doesn't match");
02496 PrintIfNotEmpty(distance_listener.str(), listener->stream());
02497 return result;
02498 }
02499
02500 private:
02501 const Matcher<DistanceType> distance_matcher_;
02502 GTEST_DISALLOW_ASSIGN_(Impl);
02503 };
02504
02505 private:
02506 const DistanceMatcher distance_matcher_;
02507 GTEST_DISALLOW_ASSIGN_(BeginEndDistanceIsMatcher);
02508 };
02509
02510
02511
02512
02513
02514
02515
02516
02517
02518
02519
02520 template <typename Container>
02521 class ContainerEqMatcher {
02522 public:
02523 typedef internal::StlContainerView<Container> View;
02524 typedef typename View::type StlContainer;
02525 typedef typename View::const_reference StlContainerReference;
02526
02527
02528
02529 explicit ContainerEqMatcher(const Container& expected)
02530 : expected_(View::Copy(expected)) {
02531
02532
02533 (void)testing::StaticAssertTypeEq<Container,
02534 GTEST_REMOVE_REFERENCE_AND_CONST_(Container)>();
02535 }
02536
02537 void DescribeTo(::std::ostream* os) const {
02538 *os << "equals ";
02539 UniversalPrint(expected_, os);
02540 }
02541 void DescribeNegationTo(::std::ostream* os) const {
02542 *os << "does not equal ";
02543 UniversalPrint(expected_, os);
02544 }
02545
02546 template <typename LhsContainer>
02547 bool MatchAndExplain(const LhsContainer& lhs,
02548 MatchResultListener* listener) const {
02549
02550
02551 typedef internal::StlContainerView<GTEST_REMOVE_CONST_(LhsContainer)>
02552 LhsView;
02553 typedef typename LhsView::type LhsStlContainer;
02554 StlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
02555 if (lhs_stl_container == expected_)
02556 return true;
02557
02558 ::std::ostream* const os = listener->stream();
02559 if (os != NULL) {
02560
02561 bool printed_header = false;
02562 for (typename LhsStlContainer::const_iterator it =
02563 lhs_stl_container.begin();
02564 it != lhs_stl_container.end(); ++it) {
02565 if (internal::ArrayAwareFind(expected_.begin(), expected_.end(), *it) ==
02566 expected_.end()) {
02567 if (printed_header) {
02568 *os << ", ";
02569 } else {
02570 *os << "which has these unexpected elements: ";
02571 printed_header = true;
02572 }
02573 UniversalPrint(*it, os);
02574 }
02575 }
02576
02577
02578 bool printed_header2 = false;
02579 for (typename StlContainer::const_iterator it = expected_.begin();
02580 it != expected_.end(); ++it) {
02581 if (internal::ArrayAwareFind(
02582 lhs_stl_container.begin(), lhs_stl_container.end(), *it) ==
02583 lhs_stl_container.end()) {
02584 if (printed_header2) {
02585 *os << ", ";
02586 } else {
02587 *os << (printed_header ? ",\nand" : "which")
02588 << " doesn't have these expected elements: ";
02589 printed_header2 = true;
02590 }
02591 UniversalPrint(*it, os);
02592 }
02593 }
02594 }
02595
02596 return false;
02597 }
02598
02599 private:
02600 const StlContainer expected_;
02601
02602 GTEST_DISALLOW_ASSIGN_(ContainerEqMatcher);
02603 };
02604
02605
02606 struct LessComparator {
02607 template <typename T, typename U>
02608 bool operator()(const T& lhs, const U& rhs) const { return lhs < rhs; }
02609 };
02610
02611
02612 template <typename Comparator, typename ContainerMatcher>
02613 class WhenSortedByMatcher {
02614 public:
02615 WhenSortedByMatcher(const Comparator& comparator,
02616 const ContainerMatcher& matcher)
02617 : comparator_(comparator), matcher_(matcher) {}
02618
02619 template <typename LhsContainer>
02620 operator Matcher<LhsContainer>() const {
02621 return MakeMatcher(new Impl<LhsContainer>(comparator_, matcher_));
02622 }
02623
02624 template <typename LhsContainer>
02625 class Impl : public MatcherInterface<LhsContainer> {
02626 public:
02627 typedef internal::StlContainerView<
02628 GTEST_REMOVE_REFERENCE_AND_CONST_(LhsContainer)> LhsView;
02629 typedef typename LhsView::type LhsStlContainer;
02630 typedef typename LhsView::const_reference LhsStlContainerReference;
02631
02632
02633 typedef typename RemoveConstFromKey<
02634 typename LhsStlContainer::value_type>::type LhsValue;
02635
02636 Impl(const Comparator& comparator, const ContainerMatcher& matcher)
02637 : comparator_(comparator), matcher_(matcher) {}
02638
02639 virtual void DescribeTo(::std::ostream* os) const {
02640 *os << "(when sorted) ";
02641 matcher_.DescribeTo(os);
02642 }
02643
02644 virtual void DescribeNegationTo(::std::ostream* os) const {
02645 *os << "(when sorted) ";
02646 matcher_.DescribeNegationTo(os);
02647 }
02648
02649 virtual bool MatchAndExplain(LhsContainer lhs,
02650 MatchResultListener* listener) const {
02651 LhsStlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
02652 ::std::vector<LhsValue> sorted_container(lhs_stl_container.begin(),
02653 lhs_stl_container.end());
02654 ::std::sort(
02655 sorted_container.begin(), sorted_container.end(), comparator_);
02656
02657 if (!listener->IsInterested()) {
02658
02659
02660 return matcher_.Matches(sorted_container);
02661 }
02662
02663 *listener << "which is ";
02664 UniversalPrint(sorted_container, listener->stream());
02665 *listener << " when sorted";
02666
02667 StringMatchResultListener inner_listener;
02668 const bool match = matcher_.MatchAndExplain(sorted_container,
02669 &inner_listener);
02670 PrintIfNotEmpty(inner_listener.str(), listener->stream());
02671 return match;
02672 }
02673
02674 private:
02675 const Comparator comparator_;
02676 const Matcher<const ::std::vector<LhsValue>&> matcher_;
02677
02678 GTEST_DISALLOW_COPY_AND_ASSIGN_(Impl);
02679 };
02680
02681 private:
02682 const Comparator comparator_;
02683 const ContainerMatcher matcher_;
02684
02685 GTEST_DISALLOW_ASSIGN_(WhenSortedByMatcher);
02686 };
02687
02688
02689
02690
02691
02692 template <typename TupleMatcher, typename RhsContainer>
02693 class PointwiseMatcher {
02694 public:
02695 typedef internal::StlContainerView<RhsContainer> RhsView;
02696 typedef typename RhsView::type RhsStlContainer;
02697 typedef typename RhsStlContainer::value_type RhsValue;
02698
02699
02700
02701 PointwiseMatcher(const TupleMatcher& tuple_matcher, const RhsContainer& rhs)
02702 : tuple_matcher_(tuple_matcher), rhs_(RhsView::Copy(rhs)) {
02703
02704
02705 (void)testing::StaticAssertTypeEq<RhsContainer,
02706 GTEST_REMOVE_REFERENCE_AND_CONST_(RhsContainer)>();
02707 }
02708
02709 template <typename LhsContainer>
02710 operator Matcher<LhsContainer>() const {
02711 return MakeMatcher(new Impl<LhsContainer>(tuple_matcher_, rhs_));
02712 }
02713
02714 template <typename LhsContainer>
02715 class Impl : public MatcherInterface<LhsContainer> {
02716 public:
02717 typedef internal::StlContainerView<
02718 GTEST_REMOVE_REFERENCE_AND_CONST_(LhsContainer)> LhsView;
02719 typedef typename LhsView::type LhsStlContainer;
02720 typedef typename LhsView::const_reference LhsStlContainerReference;
02721 typedef typename LhsStlContainer::value_type LhsValue;
02722
02723
02724
02725
02726 typedef ::testing::tuple<const LhsValue&, const RhsValue&> InnerMatcherArg;
02727
02728 Impl(const TupleMatcher& tuple_matcher, const RhsStlContainer& rhs)
02729
02730 : mono_tuple_matcher_(SafeMatcherCast<InnerMatcherArg>(tuple_matcher)),
02731 rhs_(rhs) {}
02732
02733 virtual void DescribeTo(::std::ostream* os) const {
02734 *os << "contains " << rhs_.size()
02735 << " values, where each value and its corresponding value in ";
02736 UniversalPrinter<RhsStlContainer>::Print(rhs_, os);
02737 *os << " ";
02738 mono_tuple_matcher_.DescribeTo(os);
02739 }
02740 virtual void DescribeNegationTo(::std::ostream* os) const {
02741 *os << "doesn't contain exactly " << rhs_.size()
02742 << " values, or contains a value x at some index i"
02743 << " where x and the i-th value of ";
02744 UniversalPrint(rhs_, os);
02745 *os << " ";
02746 mono_tuple_matcher_.DescribeNegationTo(os);
02747 }
02748
02749 virtual bool MatchAndExplain(LhsContainer lhs,
02750 MatchResultListener* listener) const {
02751 LhsStlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
02752 const size_t actual_size = lhs_stl_container.size();
02753 if (actual_size != rhs_.size()) {
02754 *listener << "which contains " << actual_size << " values";
02755 return false;
02756 }
02757
02758 typename LhsStlContainer::const_iterator left = lhs_stl_container.begin();
02759 typename RhsStlContainer::const_iterator right = rhs_.begin();
02760 for (size_t i = 0; i != actual_size; ++i, ++left, ++right) {
02761 const InnerMatcherArg value_pair(*left, *right);
02762
02763 if (listener->IsInterested()) {
02764 StringMatchResultListener inner_listener;
02765 if (!mono_tuple_matcher_.MatchAndExplain(
02766 value_pair, &inner_listener)) {
02767 *listener << "where the value pair (";
02768 UniversalPrint(*left, listener->stream());
02769 *listener << ", ";
02770 UniversalPrint(*right, listener->stream());
02771 *listener << ") at index #" << i << " don't match";
02772 PrintIfNotEmpty(inner_listener.str(), listener->stream());
02773 return false;
02774 }
02775 } else {
02776 if (!mono_tuple_matcher_.Matches(value_pair))
02777 return false;
02778 }
02779 }
02780
02781 return true;
02782 }
02783
02784 private:
02785 const Matcher<InnerMatcherArg> mono_tuple_matcher_;
02786 const RhsStlContainer rhs_;
02787
02788 GTEST_DISALLOW_ASSIGN_(Impl);
02789 };
02790
02791 private:
02792 const TupleMatcher tuple_matcher_;
02793 const RhsStlContainer rhs_;
02794
02795 GTEST_DISALLOW_ASSIGN_(PointwiseMatcher);
02796 };
02797
02798
02799 template <typename Container>
02800 class QuantifierMatcherImpl : public MatcherInterface<Container> {
02801 public:
02802 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
02803 typedef StlContainerView<RawContainer> View;
02804 typedef typename View::type StlContainer;
02805 typedef typename View::const_reference StlContainerReference;
02806 typedef typename StlContainer::value_type Element;
02807
02808 template <typename InnerMatcher>
02809 explicit QuantifierMatcherImpl(InnerMatcher inner_matcher)
02810 : inner_matcher_(
02811 testing::SafeMatcherCast<const Element&>(inner_matcher)) {}
02812
02813
02814
02815
02816 bool MatchAndExplainImpl(bool all_elements_should_match,
02817 Container container,
02818 MatchResultListener* listener) const {
02819 StlContainerReference stl_container = View::ConstReference(container);
02820 size_t i = 0;
02821 for (typename StlContainer::const_iterator it = stl_container.begin();
02822 it != stl_container.end(); ++it, ++i) {
02823 StringMatchResultListener inner_listener;
02824 const bool matches = inner_matcher_.MatchAndExplain(*it, &inner_listener);
02825
02826 if (matches != all_elements_should_match) {
02827 *listener << "whose element #" << i
02828 << (matches ? " matches" : " doesn't match");
02829 PrintIfNotEmpty(inner_listener.str(), listener->stream());
02830 return !all_elements_should_match;
02831 }
02832 }
02833 return all_elements_should_match;
02834 }
02835
02836 protected:
02837 const Matcher<const Element&> inner_matcher_;
02838
02839 GTEST_DISALLOW_ASSIGN_(QuantifierMatcherImpl);
02840 };
02841
02842
02843
02844 template <typename Container>
02845 class ContainsMatcherImpl : public QuantifierMatcherImpl<Container> {
02846 public:
02847 template <typename InnerMatcher>
02848 explicit ContainsMatcherImpl(InnerMatcher inner_matcher)
02849 : QuantifierMatcherImpl<Container>(inner_matcher) {}
02850
02851
02852 virtual void DescribeTo(::std::ostream* os) const {
02853 *os << "contains at least one element that ";
02854 this->inner_matcher_.DescribeTo(os);
02855 }
02856
02857 virtual void DescribeNegationTo(::std::ostream* os) const {
02858 *os << "doesn't contain any element that ";
02859 this->inner_matcher_.DescribeTo(os);
02860 }
02861
02862 virtual bool MatchAndExplain(Container container,
02863 MatchResultListener* listener) const {
02864 return this->MatchAndExplainImpl(false, container, listener);
02865 }
02866
02867 private:
02868 GTEST_DISALLOW_ASSIGN_(ContainsMatcherImpl);
02869 };
02870
02871
02872
02873 template <typename Container>
02874 class EachMatcherImpl : public QuantifierMatcherImpl<Container> {
02875 public:
02876 template <typename InnerMatcher>
02877 explicit EachMatcherImpl(InnerMatcher inner_matcher)
02878 : QuantifierMatcherImpl<Container>(inner_matcher) {}
02879
02880
02881 virtual void DescribeTo(::std::ostream* os) const {
02882 *os << "only contains elements that ";
02883 this->inner_matcher_.DescribeTo(os);
02884 }
02885
02886 virtual void DescribeNegationTo(::std::ostream* os) const {
02887 *os << "contains some element that ";
02888 this->inner_matcher_.DescribeNegationTo(os);
02889 }
02890
02891 virtual bool MatchAndExplain(Container container,
02892 MatchResultListener* listener) const {
02893 return this->MatchAndExplainImpl(true, container, listener);
02894 }
02895
02896 private:
02897 GTEST_DISALLOW_ASSIGN_(EachMatcherImpl);
02898 };
02899
02900
02901 template <typename M>
02902 class ContainsMatcher {
02903 public:
02904 explicit ContainsMatcher(M m) : inner_matcher_(m) {}
02905
02906 template <typename Container>
02907 operator Matcher<Container>() const {
02908 return MakeMatcher(new ContainsMatcherImpl<Container>(inner_matcher_));
02909 }
02910
02911 private:
02912 const M inner_matcher_;
02913
02914 GTEST_DISALLOW_ASSIGN_(ContainsMatcher);
02915 };
02916
02917
02918 template <typename M>
02919 class EachMatcher {
02920 public:
02921 explicit EachMatcher(M m) : inner_matcher_(m) {}
02922
02923 template <typename Container>
02924 operator Matcher<Container>() const {
02925 return MakeMatcher(new EachMatcherImpl<Container>(inner_matcher_));
02926 }
02927
02928 private:
02929 const M inner_matcher_;
02930
02931 GTEST_DISALLOW_ASSIGN_(EachMatcher);
02932 };
02933
02934
02935
02936
02937
02938 template <typename PairType>
02939 class KeyMatcherImpl : public MatcherInterface<PairType> {
02940 public:
02941 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(PairType) RawPairType;
02942 typedef typename RawPairType::first_type KeyType;
02943
02944 template <typename InnerMatcher>
02945 explicit KeyMatcherImpl(InnerMatcher inner_matcher)
02946 : inner_matcher_(
02947 testing::SafeMatcherCast<const KeyType&>(inner_matcher)) {
02948 }
02949
02950
02951 virtual bool MatchAndExplain(PairType key_value,
02952 MatchResultListener* listener) const {
02953 StringMatchResultListener inner_listener;
02954 const bool match = inner_matcher_.MatchAndExplain(key_value.first,
02955 &inner_listener);
02956 const internal::string explanation = inner_listener.str();
02957 if (explanation != "") {
02958 *listener << "whose first field is a value " << explanation;
02959 }
02960 return match;
02961 }
02962
02963
02964 virtual void DescribeTo(::std::ostream* os) const {
02965 *os << "has a key that ";
02966 inner_matcher_.DescribeTo(os);
02967 }
02968
02969
02970 virtual void DescribeNegationTo(::std::ostream* os) const {
02971 *os << "doesn't have a key that ";
02972 inner_matcher_.DescribeTo(os);
02973 }
02974
02975 private:
02976 const Matcher<const KeyType&> inner_matcher_;
02977
02978 GTEST_DISALLOW_ASSIGN_(KeyMatcherImpl);
02979 };
02980
02981
02982 template <typename M>
02983 class KeyMatcher {
02984 public:
02985 explicit KeyMatcher(M m) : matcher_for_key_(m) {}
02986
02987 template <typename PairType>
02988 operator Matcher<PairType>() const {
02989 return MakeMatcher(new KeyMatcherImpl<PairType>(matcher_for_key_));
02990 }
02991
02992 private:
02993 const M matcher_for_key_;
02994
02995 GTEST_DISALLOW_ASSIGN_(KeyMatcher);
02996 };
02997
02998
02999
03000 template <typename PairType>
03001 class PairMatcherImpl : public MatcherInterface<PairType> {
03002 public:
03003 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(PairType) RawPairType;
03004 typedef typename RawPairType::first_type FirstType;
03005 typedef typename RawPairType::second_type SecondType;
03006
03007 template <typename FirstMatcher, typename SecondMatcher>
03008 PairMatcherImpl(FirstMatcher first_matcher, SecondMatcher second_matcher)
03009 : first_matcher_(
03010 testing::SafeMatcherCast<const FirstType&>(first_matcher)),
03011 second_matcher_(
03012 testing::SafeMatcherCast<const SecondType&>(second_matcher)) {
03013 }
03014
03015
03016 virtual void DescribeTo(::std::ostream* os) const {
03017 *os << "has a first field that ";
03018 first_matcher_.DescribeTo(os);
03019 *os << ", and has a second field that ";
03020 second_matcher_.DescribeTo(os);
03021 }
03022
03023
03024 virtual void DescribeNegationTo(::std::ostream* os) const {
03025 *os << "has a first field that ";
03026 first_matcher_.DescribeNegationTo(os);
03027 *os << ", or has a second field that ";
03028 second_matcher_.DescribeNegationTo(os);
03029 }
03030
03031
03032
03033 virtual bool MatchAndExplain(PairType a_pair,
03034 MatchResultListener* listener) const {
03035 if (!listener->IsInterested()) {
03036
03037
03038 return first_matcher_.Matches(a_pair.first) &&
03039 second_matcher_.Matches(a_pair.second);
03040 }
03041 StringMatchResultListener first_inner_listener;
03042 if (!first_matcher_.MatchAndExplain(a_pair.first,
03043 &first_inner_listener)) {
03044 *listener << "whose first field does not match";
03045 PrintIfNotEmpty(first_inner_listener.str(), listener->stream());
03046 return false;
03047 }
03048 StringMatchResultListener second_inner_listener;
03049 if (!second_matcher_.MatchAndExplain(a_pair.second,
03050 &second_inner_listener)) {
03051 *listener << "whose second field does not match";
03052 PrintIfNotEmpty(second_inner_listener.str(), listener->stream());
03053 return false;
03054 }
03055 ExplainSuccess(first_inner_listener.str(), second_inner_listener.str(),
03056 listener);
03057 return true;
03058 }
03059
03060 private:
03061 void ExplainSuccess(const internal::string& first_explanation,
03062 const internal::string& second_explanation,
03063 MatchResultListener* listener) const {
03064 *listener << "whose both fields match";
03065 if (first_explanation != "") {
03066 *listener << ", where the first field is a value " << first_explanation;
03067 }
03068 if (second_explanation != "") {
03069 *listener << ", ";
03070 if (first_explanation != "") {
03071 *listener << "and ";
03072 } else {
03073 *listener << "where ";
03074 }
03075 *listener << "the second field is a value " << second_explanation;
03076 }
03077 }
03078
03079 const Matcher<const FirstType&> first_matcher_;
03080 const Matcher<const SecondType&> second_matcher_;
03081
03082 GTEST_DISALLOW_ASSIGN_(PairMatcherImpl);
03083 };
03084
03085
03086 template <typename FirstMatcher, typename SecondMatcher>
03087 class PairMatcher {
03088 public:
03089 PairMatcher(FirstMatcher first_matcher, SecondMatcher second_matcher)
03090 : first_matcher_(first_matcher), second_matcher_(second_matcher) {}
03091
03092 template <typename PairType>
03093 operator Matcher<PairType> () const {
03094 return MakeMatcher(
03095 new PairMatcherImpl<PairType>(
03096 first_matcher_, second_matcher_));
03097 }
03098
03099 private:
03100 const FirstMatcher first_matcher_;
03101 const SecondMatcher second_matcher_;
03102
03103 GTEST_DISALLOW_ASSIGN_(PairMatcher);
03104 };
03105
03106
03107 template <typename Container>
03108 class ElementsAreMatcherImpl : public MatcherInterface<Container> {
03109 public:
03110 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
03111 typedef internal::StlContainerView<RawContainer> View;
03112 typedef typename View::type StlContainer;
03113 typedef typename View::const_reference StlContainerReference;
03114 typedef typename StlContainer::value_type Element;
03115
03116
03117
03118 template <typename InputIter>
03119 ElementsAreMatcherImpl(InputIter first, InputIter last) {
03120 while (first != last) {
03121 matchers_.push_back(MatcherCast<const Element&>(*first++));
03122 }
03123 }
03124
03125
03126 virtual void DescribeTo(::std::ostream* os) const {
03127 if (count() == 0) {
03128 *os << "is empty";
03129 } else if (count() == 1) {
03130 *os << "has 1 element that ";
03131 matchers_[0].DescribeTo(os);
03132 } else {
03133 *os << "has " << Elements(count()) << " where\n";
03134 for (size_t i = 0; i != count(); ++i) {
03135 *os << "element #" << i << " ";
03136 matchers_[i].DescribeTo(os);
03137 if (i + 1 < count()) {
03138 *os << ",\n";
03139 }
03140 }
03141 }
03142 }
03143
03144
03145 virtual void DescribeNegationTo(::std::ostream* os) const {
03146 if (count() == 0) {
03147 *os << "isn't empty";
03148 return;
03149 }
03150
03151 *os << "doesn't have " << Elements(count()) << ", or\n";
03152 for (size_t i = 0; i != count(); ++i) {
03153 *os << "element #" << i << " ";
03154 matchers_[i].DescribeNegationTo(os);
03155 if (i + 1 < count()) {
03156 *os << ", or\n";
03157 }
03158 }
03159 }
03160
03161 virtual bool MatchAndExplain(Container container,
03162 MatchResultListener* listener) const {
03163
03164
03165
03166 const bool listener_interested = listener->IsInterested();
03167
03168
03169 ::std::vector<internal::string> explanations(count());
03170 StlContainerReference stl_container = View::ConstReference(container);
03171 typename StlContainer::const_iterator it = stl_container.begin();
03172 size_t exam_pos = 0;
03173 bool mismatch_found = false;
03174
03175
03176
03177
03178 for (; it != stl_container.end() && exam_pos != count(); ++it, ++exam_pos) {
03179 bool match;
03180 if (listener_interested) {
03181 StringMatchResultListener s;
03182 match = matchers_[exam_pos].MatchAndExplain(*it, &s);
03183 explanations[exam_pos] = s.str();
03184 } else {
03185 match = matchers_[exam_pos].Matches(*it);
03186 }
03187
03188 if (!match) {
03189 mismatch_found = true;
03190 break;
03191 }
03192 }
03193
03194
03195
03196
03197
03198 size_t actual_count = exam_pos;
03199 for (; it != stl_container.end(); ++it) {
03200 ++actual_count;
03201 }
03202
03203 if (actual_count != count()) {
03204
03205
03206
03207
03208 if (listener_interested && (actual_count != 0)) {
03209 *listener << "which has " << Elements(actual_count);
03210 }
03211 return false;
03212 }
03213
03214 if (mismatch_found) {
03215
03216 if (listener_interested) {
03217 *listener << "whose element #" << exam_pos << " doesn't match";
03218 PrintIfNotEmpty(explanations[exam_pos], listener->stream());
03219 }
03220 return false;
03221 }
03222
03223
03224
03225 if (listener_interested) {
03226 bool reason_printed = false;
03227 for (size_t i = 0; i != count(); ++i) {
03228 const internal::string& s = explanations[i];
03229 if (!s.empty()) {
03230 if (reason_printed) {
03231 *listener << ",\nand ";
03232 }
03233 *listener << "whose element #" << i << " matches, " << s;
03234 reason_printed = true;
03235 }
03236 }
03237 }
03238 return true;
03239 }
03240
03241 private:
03242 static Message Elements(size_t count) {
03243 return Message() << count << (count == 1 ? " element" : " elements");
03244 }
03245
03246 size_t count() const { return matchers_.size(); }
03247
03248 ::std::vector<Matcher<const Element&> > matchers_;
03249
03250 GTEST_DISALLOW_ASSIGN_(ElementsAreMatcherImpl);
03251 };
03252
03253
03254
03255
03256
03257 class GTEST_API_ MatchMatrix {
03258 public:
03259 MatchMatrix(size_t num_elements, size_t num_matchers)
03260 : num_elements_(num_elements),
03261 num_matchers_(num_matchers),
03262 matched_(num_elements_* num_matchers_, 0) {
03263 }
03264
03265 size_t LhsSize() const { return num_elements_; }
03266 size_t RhsSize() const { return num_matchers_; }
03267 bool HasEdge(size_t ilhs, size_t irhs) const {
03268 return matched_[SpaceIndex(ilhs, irhs)] == 1;
03269 }
03270 void SetEdge(size_t ilhs, size_t irhs, bool b) {
03271 matched_[SpaceIndex(ilhs, irhs)] = b ? 1 : 0;
03272 }
03273
03274
03275
03276
03277 bool NextGraph();
03278
03279 void Randomize();
03280
03281 string DebugString() const;
03282
03283 private:
03284 size_t SpaceIndex(size_t ilhs, size_t irhs) const {
03285 return ilhs * num_matchers_ + irhs;
03286 }
03287
03288 size_t num_elements_;
03289 size_t num_matchers_;
03290
03291
03292
03293
03294 ::std::vector<char> matched_;
03295 };
03296
03297 typedef ::std::pair<size_t, size_t> ElementMatcherPair;
03298 typedef ::std::vector<ElementMatcherPair> ElementMatcherPairs;
03299
03300
03301
03302 GTEST_API_ ElementMatcherPairs
03303 FindMaxBipartiteMatching(const MatchMatrix& g);
03304
03305 GTEST_API_ bool FindPairing(const MatchMatrix& matrix,
03306 MatchResultListener* listener);
03307
03308
03309
03310
03311 class GTEST_API_ UnorderedElementsAreMatcherImplBase {
03312 protected:
03313
03314
03315
03316 typedef ::std::vector<const MatcherDescriberInterface*> MatcherDescriberVec;
03317
03318
03319 void DescribeToImpl(::std::ostream* os) const;
03320
03321
03322 void DescribeNegationToImpl(::std::ostream* os) const;
03323
03324 bool VerifyAllElementsAndMatchersAreMatched(
03325 const ::std::vector<string>& element_printouts,
03326 const MatchMatrix& matrix,
03327 MatchResultListener* listener) const;
03328
03329 MatcherDescriberVec& matcher_describers() {
03330 return matcher_describers_;
03331 }
03332
03333 static Message Elements(size_t n) {
03334 return Message() << n << " element" << (n == 1 ? "" : "s");
03335 }
03336
03337 private:
03338 MatcherDescriberVec matcher_describers_;
03339
03340 GTEST_DISALLOW_ASSIGN_(UnorderedElementsAreMatcherImplBase);
03341 };
03342
03343
03344 template <typename Container>
03345 class UnorderedElementsAreMatcherImpl
03346 : public MatcherInterface<Container>,
03347 public UnorderedElementsAreMatcherImplBase {
03348 public:
03349 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
03350 typedef internal::StlContainerView<RawContainer> View;
03351 typedef typename View::type StlContainer;
03352 typedef typename View::const_reference StlContainerReference;
03353 typedef typename StlContainer::const_iterator StlContainerConstIterator;
03354 typedef typename StlContainer::value_type Element;
03355
03356
03357
03358 template <typename InputIter>
03359 UnorderedElementsAreMatcherImpl(InputIter first, InputIter last) {
03360 for (; first != last; ++first) {
03361 matchers_.push_back(MatcherCast<const Element&>(*first));
03362 matcher_describers().push_back(matchers_.back().GetDescriber());
03363 }
03364 }
03365
03366
03367 virtual void DescribeTo(::std::ostream* os) const {
03368 return UnorderedElementsAreMatcherImplBase::DescribeToImpl(os);
03369 }
03370
03371
03372 virtual void DescribeNegationTo(::std::ostream* os) const {
03373 return UnorderedElementsAreMatcherImplBase::DescribeNegationToImpl(os);
03374 }
03375
03376 virtual bool MatchAndExplain(Container container,
03377 MatchResultListener* listener) const {
03378 StlContainerReference stl_container = View::ConstReference(container);
03379 ::std::vector<string> element_printouts;
03380 MatchMatrix matrix = AnalyzeElements(stl_container.begin(),
03381 stl_container.end(),
03382 &element_printouts,
03383 listener);
03384
03385 const size_t actual_count = matrix.LhsSize();
03386 if (actual_count == 0 && matchers_.empty()) {
03387 return true;
03388 }
03389 if (actual_count != matchers_.size()) {
03390
03391
03392
03393
03394 if (actual_count != 0 && listener->IsInterested()) {
03395 *listener << "which has " << Elements(actual_count);
03396 }
03397 return false;
03398 }
03399
03400 return VerifyAllElementsAndMatchersAreMatched(element_printouts,
03401 matrix, listener) &&
03402 FindPairing(matrix, listener);
03403 }
03404
03405 private:
03406 typedef ::std::vector<Matcher<const Element&> > MatcherVec;
03407
03408 template <typename ElementIter>
03409 MatchMatrix AnalyzeElements(ElementIter elem_first, ElementIter elem_last,
03410 ::std::vector<string>* element_printouts,
03411 MatchResultListener* listener) const {
03412 element_printouts->clear();
03413 ::std::vector<char> did_match;
03414 size_t num_elements = 0;
03415 for (; elem_first != elem_last; ++num_elements, ++elem_first) {
03416 if (listener->IsInterested()) {
03417 element_printouts->push_back(PrintToString(*elem_first));
03418 }
03419 for (size_t irhs = 0; irhs != matchers_.size(); ++irhs) {
03420 did_match.push_back(Matches(matchers_[irhs])(*elem_first));
03421 }
03422 }
03423
03424 MatchMatrix matrix(num_elements, matchers_.size());
03425 ::std::vector<char>::const_iterator did_match_iter = did_match.begin();
03426 for (size_t ilhs = 0; ilhs != num_elements; ++ilhs) {
03427 for (size_t irhs = 0; irhs != matchers_.size(); ++irhs) {
03428 matrix.SetEdge(ilhs, irhs, *did_match_iter++ != 0);
03429 }
03430 }
03431 return matrix;
03432 }
03433
03434 MatcherVec matchers_;
03435
03436 GTEST_DISALLOW_ASSIGN_(UnorderedElementsAreMatcherImpl);
03437 };
03438
03439
03440
03441 template <typename Target>
03442 struct CastAndAppendTransform {
03443 template <typename Arg>
03444 Matcher<Target> operator()(const Arg& a) const {
03445 return MatcherCast<Target>(a);
03446 }
03447 };
03448
03449
03450 template <typename MatcherTuple>
03451 class UnorderedElementsAreMatcher {
03452 public:
03453 explicit UnorderedElementsAreMatcher(const MatcherTuple& args)
03454 : matchers_(args) {}
03455
03456 template <typename Container>
03457 operator Matcher<Container>() const {
03458 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
03459 typedef typename internal::StlContainerView<RawContainer>::type View;
03460 typedef typename View::value_type Element;
03461 typedef ::std::vector<Matcher<const Element&> > MatcherVec;
03462 MatcherVec matchers;
03463 matchers.reserve(::testing::tuple_size<MatcherTuple>::value);
03464 TransformTupleValues(CastAndAppendTransform<const Element&>(), matchers_,
03465 ::std::back_inserter(matchers));
03466 return MakeMatcher(new UnorderedElementsAreMatcherImpl<Container>(
03467 matchers.begin(), matchers.end()));
03468 }
03469
03470 private:
03471 const MatcherTuple matchers_;
03472 GTEST_DISALLOW_ASSIGN_(UnorderedElementsAreMatcher);
03473 };
03474
03475
03476 template <typename MatcherTuple>
03477 class ElementsAreMatcher {
03478 public:
03479 explicit ElementsAreMatcher(const MatcherTuple& args) : matchers_(args) {}
03480
03481 template <typename Container>
03482 operator Matcher<Container>() const {
03483 typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
03484 typedef typename internal::StlContainerView<RawContainer>::type View;
03485 typedef typename View::value_type Element;
03486 typedef ::std::vector<Matcher<const Element&> > MatcherVec;
03487 MatcherVec matchers;
03488 matchers.reserve(::testing::tuple_size<MatcherTuple>::value);
03489 TransformTupleValues(CastAndAppendTransform<const Element&>(), matchers_,
03490 ::std::back_inserter(matchers));
03491 return MakeMatcher(new ElementsAreMatcherImpl<Container>(
03492 matchers.begin(), matchers.end()));
03493 }
03494
03495 private:
03496 const MatcherTuple matchers_;
03497 GTEST_DISALLOW_ASSIGN_(ElementsAreMatcher);
03498 };
03499
03500
03501 template <typename T>
03502 class UnorderedElementsAreArrayMatcher {
03503 public:
03504 UnorderedElementsAreArrayMatcher() {}
03505
03506 template <typename Iter>
03507 UnorderedElementsAreArrayMatcher(Iter first, Iter last)
03508 : matchers_(first, last) {}
03509
03510 template <typename Container>
03511 operator Matcher<Container>() const {
03512 return MakeMatcher(
03513 new UnorderedElementsAreMatcherImpl<Container>(matchers_.begin(),
03514 matchers_.end()));
03515 }
03516
03517 private:
03518 ::std::vector<T> matchers_;
03519
03520 GTEST_DISALLOW_ASSIGN_(UnorderedElementsAreArrayMatcher);
03521 };
03522
03523
03524 template <typename T>
03525 class ElementsAreArrayMatcher {
03526 public:
03527 template <typename Iter>
03528 ElementsAreArrayMatcher(Iter first, Iter last) : matchers_(first, last) {}
03529
03530 template <typename Container>
03531 operator Matcher<Container>() const {
03532 return MakeMatcher(new ElementsAreMatcherImpl<Container>(
03533 matchers_.begin(), matchers_.end()));
03534 }
03535
03536 private:
03537 const ::std::vector<T> matchers_;
03538
03539 GTEST_DISALLOW_ASSIGN_(ElementsAreArrayMatcher);
03540 };
03541
03542
03543
03544
03545
03546
03547
03548
03549
03550
03551 template <typename Tuple2Matcher, typename Second>
03552 class BoundSecondMatcher {
03553 public:
03554 BoundSecondMatcher(const Tuple2Matcher& tm, const Second& second)
03555 : tuple2_matcher_(tm), second_value_(second) {}
03556
03557 template <typename T>
03558 operator Matcher<T>() const {
03559 return MakeMatcher(new Impl<T>(tuple2_matcher_, second_value_));
03560 }
03561
03562
03563
03564
03565
03566
03567
03568
03569
03570 void operator=(const BoundSecondMatcher& ) {
03571 GTEST_LOG_(FATAL) << "BoundSecondMatcher should never be assigned.";
03572 }
03573
03574 private:
03575 template <typename T>
03576 class Impl : public MatcherInterface<T> {
03577 public:
03578 typedef ::testing::tuple<T, Second> ArgTuple;
03579
03580 Impl(const Tuple2Matcher& tm, const Second& second)
03581 : mono_tuple2_matcher_(SafeMatcherCast<const ArgTuple&>(tm)),
03582 second_value_(second) {}
03583
03584 virtual void DescribeTo(::std::ostream* os) const {
03585 *os << "and ";
03586 UniversalPrint(second_value_, os);
03587 *os << " ";
03588 mono_tuple2_matcher_.DescribeTo(os);
03589 }
03590
03591 virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
03592 return mono_tuple2_matcher_.MatchAndExplain(ArgTuple(x, second_value_),
03593 listener);
03594 }
03595
03596 private:
03597 const Matcher<const ArgTuple&> mono_tuple2_matcher_;
03598 const Second second_value_;
03599
03600 GTEST_DISALLOW_ASSIGN_(Impl);
03601 };
03602
03603 const Tuple2Matcher tuple2_matcher_;
03604 const Second second_value_;
03605 };
03606
03607
03608
03609
03610
03611 template <typename Tuple2Matcher, typename Second>
03612 BoundSecondMatcher<Tuple2Matcher, Second> MatcherBindSecond(
03613 const Tuple2Matcher& tm, const Second& second) {
03614 return BoundSecondMatcher<Tuple2Matcher, Second>(tm, second);
03615 }
03616
03617
03618
03619
03620
03621
03622 GTEST_API_ string FormatMatcherDescription(bool negation,
03623 const char* matcher_name,
03624 const Strings& param_values);
03625
03626 }
03627
03628
03629
03630
03631
03632
03633
03634
03635
03636
03637
03638
03639
03640
03641
03642
03643 template <typename Iter>
03644 inline internal::ElementsAreArrayMatcher<
03645 typename ::std::iterator_traits<Iter>::value_type>
03646 ElementsAreArray(Iter first, Iter last) {
03647 typedef typename ::std::iterator_traits<Iter>::value_type T;
03648 return internal::ElementsAreArrayMatcher<T>(first, last);
03649 }
03650
03651 template <typename T>
03652 inline internal::ElementsAreArrayMatcher<T> ElementsAreArray(
03653 const T* pointer, size_t count) {
03654 return ElementsAreArray(pointer, pointer + count);
03655 }
03656
03657 template <typename T, size_t N>
03658 inline internal::ElementsAreArrayMatcher<T> ElementsAreArray(
03659 const T (&array)[N]) {
03660 return ElementsAreArray(array, N);
03661 }
03662
03663 template <typename Container>
03664 inline internal::ElementsAreArrayMatcher<typename Container::value_type>
03665 ElementsAreArray(const Container& container) {
03666 return ElementsAreArray(container.begin(), container.end());
03667 }
03668
03669 #if GTEST_HAS_STD_INITIALIZER_LIST_
03670 template <typename T>
03671 inline internal::ElementsAreArrayMatcher<T>
03672 ElementsAreArray(::std::initializer_list<T> xs) {
03673 return ElementsAreArray(xs.begin(), xs.end());
03674 }
03675 #endif
03676
03677
03678
03679
03680
03681
03682
03683
03684
03685 template <typename Iter>
03686 inline internal::UnorderedElementsAreArrayMatcher<
03687 typename ::std::iterator_traits<Iter>::value_type>
03688 UnorderedElementsAreArray(Iter first, Iter last) {
03689 typedef typename ::std::iterator_traits<Iter>::value_type T;
03690 return internal::UnorderedElementsAreArrayMatcher<T>(first, last);
03691 }
03692
03693 template <typename T>
03694 inline internal::UnorderedElementsAreArrayMatcher<T>
03695 UnorderedElementsAreArray(const T* pointer, size_t count) {
03696 return UnorderedElementsAreArray(pointer, pointer + count);
03697 }
03698
03699 template <typename T, size_t N>
03700 inline internal::UnorderedElementsAreArrayMatcher<T>
03701 UnorderedElementsAreArray(const T (&array)[N]) {
03702 return UnorderedElementsAreArray(array, N);
03703 }
03704
03705 template <typename Container>
03706 inline internal::UnorderedElementsAreArrayMatcher<
03707 typename Container::value_type>
03708 UnorderedElementsAreArray(const Container& container) {
03709 return UnorderedElementsAreArray(container.begin(), container.end());
03710 }
03711
03712 #if GTEST_HAS_STD_INITIALIZER_LIST_
03713 template <typename T>
03714 inline internal::UnorderedElementsAreArrayMatcher<T>
03715 UnorderedElementsAreArray(::std::initializer_list<T> xs) {
03716 return UnorderedElementsAreArray(xs.begin(), xs.end());
03717 }
03718 #endif
03719
03720
03721
03722
03723
03724
03725
03726
03727
03728
03729 const internal::AnythingMatcher _ = {};
03730
03731 template <typename T>
03732 inline Matcher<T> A() { return MakeMatcher(new internal::AnyMatcherImpl<T>()); }
03733
03734
03735 template <typename T>
03736 inline Matcher<T> An() { return A<T>(); }
03737
03738
03739
03740
03741 template <typename T>
03742 inline internal::EqMatcher<T> Eq(T x) { return internal::EqMatcher<T>(x); }
03743
03744
03745
03746 template <typename T>
03747 Matcher<T>::Matcher(T value) { *this = Eq(value); }
03748
03749
03750
03751
03752
03753
03754
03755
03756
03757
03758
03759
03760
03761 template <typename Lhs, typename Rhs>
03762 inline Matcher<Lhs> TypedEq(const Rhs& rhs) { return Eq(rhs); }
03763
03764
03765 template <typename Rhs>
03766 inline internal::GeMatcher<Rhs> Ge(Rhs x) {
03767 return internal::GeMatcher<Rhs>(x);
03768 }
03769
03770
03771 template <typename Rhs>
03772 inline internal::GtMatcher<Rhs> Gt(Rhs x) {
03773 return internal::GtMatcher<Rhs>(x);
03774 }
03775
03776
03777 template <typename Rhs>
03778 inline internal::LeMatcher<Rhs> Le(Rhs x) {
03779 return internal::LeMatcher<Rhs>(x);
03780 }
03781
03782
03783 template <typename Rhs>
03784 inline internal::LtMatcher<Rhs> Lt(Rhs x) {
03785 return internal::LtMatcher<Rhs>(x);
03786 }
03787
03788
03789 template <typename Rhs>
03790 inline internal::NeMatcher<Rhs> Ne(Rhs x) {
03791 return internal::NeMatcher<Rhs>(x);
03792 }
03793
03794
03795 inline PolymorphicMatcher<internal::IsNullMatcher > IsNull() {
03796 return MakePolymorphicMatcher(internal::IsNullMatcher());
03797 }
03798
03799
03800
03801
03802 inline PolymorphicMatcher<internal::NotNullMatcher > NotNull() {
03803 return MakePolymorphicMatcher(internal::NotNullMatcher());
03804 }
03805
03806
03807
03808 template <typename T>
03809 inline internal::RefMatcher<T&> Ref(T& x) {
03810 return internal::RefMatcher<T&>(x);
03811 }
03812
03813
03814
03815 inline internal::FloatingEqMatcher<double> DoubleEq(double rhs) {
03816 return internal::FloatingEqMatcher<double>(rhs, false);
03817 }
03818
03819
03820
03821 inline internal::FloatingEqMatcher<double> NanSensitiveDoubleEq(double rhs) {
03822 return internal::FloatingEqMatcher<double>(rhs, true);
03823 }
03824
03825
03826
03827
03828 inline internal::FloatingEqMatcher<double> DoubleNear(
03829 double rhs, double max_abs_error) {
03830 return internal::FloatingEqMatcher<double>(rhs, false, max_abs_error);
03831 }
03832
03833
03834
03835
03836 inline internal::FloatingEqMatcher<double> NanSensitiveDoubleNear(
03837 double rhs, double max_abs_error) {
03838 return internal::FloatingEqMatcher<double>(rhs, true, max_abs_error);
03839 }
03840
03841
03842
03843 inline internal::FloatingEqMatcher<float> FloatEq(float rhs) {
03844 return internal::FloatingEqMatcher<float>(rhs, false);
03845 }
03846
03847
03848
03849 inline internal::FloatingEqMatcher<float> NanSensitiveFloatEq(float rhs) {
03850 return internal::FloatingEqMatcher<float>(rhs, true);
03851 }
03852
03853
03854
03855
03856 inline internal::FloatingEqMatcher<float> FloatNear(
03857 float rhs, float max_abs_error) {
03858 return internal::FloatingEqMatcher<float>(rhs, false, max_abs_error);
03859 }
03860
03861
03862
03863
03864 inline internal::FloatingEqMatcher<float> NanSensitiveFloatNear(
03865 float rhs, float max_abs_error) {
03866 return internal::FloatingEqMatcher<float>(rhs, true, max_abs_error);
03867 }
03868
03869
03870
03871 template <typename InnerMatcher>
03872 inline internal::PointeeMatcher<InnerMatcher> Pointee(
03873 const InnerMatcher& inner_matcher) {
03874 return internal::PointeeMatcher<InnerMatcher>(inner_matcher);
03875 }
03876
03877
03878
03879
03880
03881
03882
03883 template <typename To>
03884 inline PolymorphicMatcher<internal::WhenDynamicCastToMatcher<To> >
03885 WhenDynamicCastTo(const Matcher<To>& inner_matcher) {
03886 return MakePolymorphicMatcher(
03887 internal::WhenDynamicCastToMatcher<To>(inner_matcher));
03888 }
03889
03890
03891
03892
03893
03894 template <typename Class, typename FieldType, typename FieldMatcher>
03895 inline PolymorphicMatcher<
03896 internal::FieldMatcher<Class, FieldType> > Field(
03897 FieldType Class::*field, const FieldMatcher& matcher) {
03898 return MakePolymorphicMatcher(
03899 internal::FieldMatcher<Class, FieldType>(
03900 field, MatcherCast<const FieldType&>(matcher)));
03901
03902
03903
03904
03905 }
03906
03907
03908
03909
03910
03911 template <typename Class, typename PropertyType, typename PropertyMatcher>
03912 inline PolymorphicMatcher<
03913 internal::PropertyMatcher<Class, PropertyType> > Property(
03914 PropertyType (Class::*property)() const, const PropertyMatcher& matcher) {
03915 return MakePolymorphicMatcher(
03916 internal::PropertyMatcher<Class, PropertyType>(
03917 property,
03918 MatcherCast<GTEST_REFERENCE_TO_CONST_(PropertyType)>(matcher)));
03919
03920
03921
03922
03923 }
03924
03925
03926
03927
03928
03929
03930
03931
03932
03933
03934
03935
03936
03937
03938 template <typename Callable, typename ResultOfMatcher>
03939 internal::ResultOfMatcher<Callable> ResultOf(
03940 Callable callable, const ResultOfMatcher& matcher) {
03941 return internal::ResultOfMatcher<Callable>(
03942 callable,
03943 MatcherCast<typename internal::CallableTraits<Callable>::ResultType>(
03944 matcher));
03945
03946
03947
03948
03949 }
03950
03951
03952
03953
03954 inline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> >
03955 StrEq(const internal::string& str) {
03956 return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>(
03957 str, true, true));
03958 }
03959
03960
03961 inline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> >
03962 StrNe(const internal::string& str) {
03963 return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>(
03964 str, false, true));
03965 }
03966
03967
03968 inline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> >
03969 StrCaseEq(const internal::string& str) {
03970 return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>(
03971 str, true, false));
03972 }
03973
03974
03975 inline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> >
03976 StrCaseNe(const internal::string& str) {
03977 return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>(
03978 str, false, false));
03979 }
03980
03981
03982
03983 inline PolymorphicMatcher<internal::HasSubstrMatcher<internal::string> >
03984 HasSubstr(const internal::string& substring) {
03985 return MakePolymorphicMatcher(internal::HasSubstrMatcher<internal::string>(
03986 substring));
03987 }
03988
03989
03990 inline PolymorphicMatcher<internal::StartsWithMatcher<internal::string> >
03991 StartsWith(const internal::string& prefix) {
03992 return MakePolymorphicMatcher(internal::StartsWithMatcher<internal::string>(
03993 prefix));
03994 }
03995
03996
03997 inline PolymorphicMatcher<internal::EndsWithMatcher<internal::string> >
03998 EndsWith(const internal::string& suffix) {
03999 return MakePolymorphicMatcher(internal::EndsWithMatcher<internal::string>(
04000 suffix));
04001 }
04002
04003
04004
04005 inline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex(
04006 const internal::RE* regex) {
04007 return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, true));
04008 }
04009 inline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex(
04010 const internal::string& regex) {
04011 return MatchesRegex(new internal::RE(regex));
04012 }
04013
04014
04015
04016 inline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex(
04017 const internal::RE* regex) {
04018 return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, false));
04019 }
04020 inline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex(
04021 const internal::string& regex) {
04022 return ContainsRegex(new internal::RE(regex));
04023 }
04024
04025 #if GTEST_HAS_GLOBAL_WSTRING || GTEST_HAS_STD_WSTRING
04026
04027
04028
04029 inline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> >
04030 StrEq(const internal::wstring& str) {
04031 return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>(
04032 str, true, true));
04033 }
04034
04035
04036 inline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> >
04037 StrNe(const internal::wstring& str) {
04038 return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>(
04039 str, false, true));
04040 }
04041
04042
04043 inline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> >
04044 StrCaseEq(const internal::wstring& str) {
04045 return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>(
04046 str, true, false));
04047 }
04048
04049
04050 inline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> >
04051 StrCaseNe(const internal::wstring& str) {
04052 return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>(
04053 str, false, false));
04054 }
04055
04056
04057
04058 inline PolymorphicMatcher<internal::HasSubstrMatcher<internal::wstring> >
04059 HasSubstr(const internal::wstring& substring) {
04060 return MakePolymorphicMatcher(internal::HasSubstrMatcher<internal::wstring>(
04061 substring));
04062 }
04063
04064
04065 inline PolymorphicMatcher<internal::StartsWithMatcher<internal::wstring> >
04066 StartsWith(const internal::wstring& prefix) {
04067 return MakePolymorphicMatcher(internal::StartsWithMatcher<internal::wstring>(
04068 prefix));
04069 }
04070
04071
04072 inline PolymorphicMatcher<internal::EndsWithMatcher<internal::wstring> >
04073 EndsWith(const internal::wstring& suffix) {
04074 return MakePolymorphicMatcher(internal::EndsWithMatcher<internal::wstring>(
04075 suffix));
04076 }
04077
04078 #endif // GTEST_HAS_GLOBAL_WSTRING || GTEST_HAS_STD_WSTRING
04079
04080
04081
04082 inline internal::Eq2Matcher Eq() { return internal::Eq2Matcher(); }
04083
04084
04085
04086 inline internal::Ge2Matcher Ge() { return internal::Ge2Matcher(); }
04087
04088
04089
04090 inline internal::Gt2Matcher Gt() { return internal::Gt2Matcher(); }
04091
04092
04093
04094 inline internal::Le2Matcher Le() { return internal::Le2Matcher(); }
04095
04096
04097
04098 inline internal::Lt2Matcher Lt() { return internal::Lt2Matcher(); }
04099
04100
04101
04102 inline internal::Ne2Matcher Ne() { return internal::Ne2Matcher(); }
04103
04104
04105
04106 template <typename InnerMatcher>
04107 inline internal::NotMatcher<InnerMatcher> Not(InnerMatcher m) {
04108 return internal::NotMatcher<InnerMatcher>(m);
04109 }
04110
04111
04112
04113
04114 template <typename Predicate>
04115 inline PolymorphicMatcher<internal::TrulyMatcher<Predicate> >
04116 Truly(Predicate pred) {
04117 return MakePolymorphicMatcher(internal::TrulyMatcher<Predicate>(pred));
04118 }
04119
04120
04121
04122
04123
04124
04125
04126 template <typename SizeMatcher>
04127 inline internal::SizeIsMatcher<SizeMatcher>
04128 SizeIs(const SizeMatcher& size_matcher) {
04129 return internal::SizeIsMatcher<SizeMatcher>(size_matcher);
04130 }
04131
04132
04133
04134
04135
04136
04137 template <typename DistanceMatcher>
04138 inline internal::BeginEndDistanceIsMatcher<DistanceMatcher>
04139 BeginEndDistanceIs(const DistanceMatcher& distance_matcher) {
04140 return internal::BeginEndDistanceIsMatcher<DistanceMatcher>(distance_matcher);
04141 }
04142
04143
04144
04145
04146
04147 template <typename Container>
04148 inline PolymorphicMatcher<internal::ContainerEqMatcher<
04149 GTEST_REMOVE_CONST_(Container)> >
04150 ContainerEq(const Container& rhs) {
04151
04152
04153 typedef GTEST_REMOVE_CONST_(Container) RawContainer;
04154 return MakePolymorphicMatcher(
04155 internal::ContainerEqMatcher<RawContainer>(rhs));
04156 }
04157
04158
04159
04160 template <typename Comparator, typename ContainerMatcher>
04161 inline internal::WhenSortedByMatcher<Comparator, ContainerMatcher>
04162 WhenSortedBy(const Comparator& comparator,
04163 const ContainerMatcher& container_matcher) {
04164 return internal::WhenSortedByMatcher<Comparator, ContainerMatcher>(
04165 comparator, container_matcher);
04166 }
04167
04168
04169
04170 template <typename ContainerMatcher>
04171 inline internal::WhenSortedByMatcher<internal::LessComparator, ContainerMatcher>
04172 WhenSorted(const ContainerMatcher& container_matcher) {
04173 return
04174 internal::WhenSortedByMatcher<internal::LessComparator, ContainerMatcher>(
04175 internal::LessComparator(), container_matcher);
04176 }
04177
04178
04179
04180
04181
04182
04183
04184 template <typename TupleMatcher, typename Container>
04185 inline internal::PointwiseMatcher<TupleMatcher,
04186 GTEST_REMOVE_CONST_(Container)>
04187 Pointwise(const TupleMatcher& tuple_matcher, const Container& rhs) {
04188
04189
04190
04191 typedef GTEST_REMOVE_CONST_(Container) RawContainer;
04192 return internal::PointwiseMatcher<TupleMatcher, RawContainer>(
04193 tuple_matcher, rhs);
04194 }
04195
04196 #if GTEST_HAS_STD_INITIALIZER_LIST_
04197
04198
04199 template <typename TupleMatcher, typename T>
04200 inline internal::PointwiseMatcher<TupleMatcher, std::vector<T> > Pointwise(
04201 const TupleMatcher& tuple_matcher, std::initializer_list<T> rhs) {
04202 return Pointwise(tuple_matcher, std::vector<T>(rhs));
04203 }
04204
04205 #endif // GTEST_HAS_STD_INITIALIZER_LIST_
04206
04207
04208
04209
04210
04211
04212
04213
04214
04215
04216
04217
04218 template <typename Tuple2Matcher, typename RhsContainer>
04219 inline internal::UnorderedElementsAreArrayMatcher<
04220 typename internal::BoundSecondMatcher<
04221 Tuple2Matcher, typename internal::StlContainerView<GTEST_REMOVE_CONST_(
04222 RhsContainer)>::type::value_type> >
04223 UnorderedPointwise(const Tuple2Matcher& tuple2_matcher,
04224 const RhsContainer& rhs_container) {
04225
04226
04227
04228 typedef GTEST_REMOVE_CONST_(RhsContainer) RawRhsContainer;
04229
04230
04231
04232 typedef typename internal::StlContainerView<RawRhsContainer> RhsView;
04233 typedef typename RhsView::type RhsStlContainer;
04234 typedef typename RhsStlContainer::value_type Second;
04235 const RhsStlContainer& rhs_stl_container =
04236 RhsView::ConstReference(rhs_container);
04237
04238
04239 ::std::vector<internal::BoundSecondMatcher<Tuple2Matcher, Second> > matchers;
04240 for (typename RhsStlContainer::const_iterator it = rhs_stl_container.begin();
04241 it != rhs_stl_container.end(); ++it) {
04242 matchers.push_back(
04243 internal::MatcherBindSecond(tuple2_matcher, *it));
04244 }
04245
04246
04247 return UnorderedElementsAreArray(matchers);
04248 }
04249
04250 #if GTEST_HAS_STD_INITIALIZER_LIST_
04251
04252
04253 template <typename Tuple2Matcher, typename T>
04254 inline internal::UnorderedElementsAreArrayMatcher<
04255 typename internal::BoundSecondMatcher<Tuple2Matcher, T> >
04256 UnorderedPointwise(const Tuple2Matcher& tuple2_matcher,
04257 std::initializer_list<T> rhs) {
04258 return UnorderedPointwise(tuple2_matcher, std::vector<T>(rhs));
04259 }
04260
04261 #endif // GTEST_HAS_STD_INITIALIZER_LIST_
04262
04263
04264
04265
04266
04267
04268
04269
04270
04271
04272
04273
04274
04275
04276
04277
04278
04279
04280
04281 template <typename M>
04282 inline internal::ContainsMatcher<M> Contains(M matcher) {
04283 return internal::ContainsMatcher<M>(matcher);
04284 }
04285
04286
04287
04288
04289
04290
04291
04292
04293
04294
04295
04296
04297
04298
04299
04300
04301
04302
04303
04304
04305
04306
04307
04308
04309
04310
04311
04312
04313 template <typename M>
04314 inline internal::EachMatcher<M> Each(M matcher) {
04315 return internal::EachMatcher<M>(matcher);
04316 }
04317
04318
04319
04320
04321 template <typename M>
04322 inline internal::KeyMatcher<M> Key(M inner_matcher) {
04323 return internal::KeyMatcher<M>(inner_matcher);
04324 }
04325
04326
04327
04328
04329
04330
04331 template <typename FirstMatcher, typename SecondMatcher>
04332 inline internal::PairMatcher<FirstMatcher, SecondMatcher>
04333 Pair(FirstMatcher first_matcher, SecondMatcher second_matcher) {
04334 return internal::PairMatcher<FirstMatcher, SecondMatcher>(
04335 first_matcher, second_matcher);
04336 }
04337
04338
04339
04340 template <typename M>
04341 inline internal::MatcherAsPredicate<M> Matches(M matcher) {
04342 return internal::MatcherAsPredicate<M>(matcher);
04343 }
04344
04345
04346 template <typename T, typename M>
04347 inline bool Value(const T& value, M matcher) {
04348 return testing::Matches(matcher)(value);
04349 }
04350
04351
04352
04353 template <typename T, typename M>
04354 inline bool ExplainMatchResult(
04355 M matcher, const T& value, MatchResultListener* listener) {
04356 return SafeMatcherCast<const T&>(matcher).MatchAndExplain(value, listener);
04357 }
04358
04359 #if GTEST_LANG_CXX11
04360
04361
04362 template <typename... Args>
04363 inline internal::AllOfMatcher<Args...> AllOf(const Args&... matchers) {
04364 return internal::AllOfMatcher<Args...>(matchers...);
04365 }
04366
04367 template <typename... Args>
04368 inline internal::AnyOfMatcher<Args...> AnyOf(const Args&... matchers) {
04369 return internal::AnyOfMatcher<Args...>(matchers...);
04370 }
04371
04372 #endif // GTEST_LANG_CXX11
04373
04374
04375
04376
04377
04378
04379
04380
04381 template <typename InnerMatcher>
04382 inline InnerMatcher AllArgs(const InnerMatcher& matcher) { return matcher; }
04383
04384
04385
04386
04387
04388 #define ASSERT_THAT(value, matcher) ASSERT_PRED_FORMAT1(\
04389 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
04390 #define EXPECT_THAT(value, matcher) EXPECT_PRED_FORMAT1(\
04391 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
04392
04393 }
04394
04395
04396
04397
04398 #include "gmock/internal/custom/gmock-matchers.h"
04399 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_