File indexing completed on 2026-11-19 10:45:45
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039 #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_MATCHERS_H_
0040 #define GOOGLETEST_INCLUDE_GTEST_GTEST_MATCHERS_H_
0041
0042 #include <atomic>
0043 #include <cstddef>
0044 #include <functional>
0045 #include <memory>
0046 #include <ostream>
0047 #include <string>
0048 #include <string_view>
0049 #include <type_traits>
0050
0051 #include "gtest/gtest-printers.h"
0052 #include "gtest/internal/gtest-internal.h"
0053 #include "gtest/internal/gtest-port.h"
0054
0055
0056 #if defined(_MSC_VER) && _MSC_VER >= 1915
0057 #define GTEST_MAYBE_5046_ 5046
0058 #else
0059 #define GTEST_MAYBE_5046_
0060 #endif
0061
0062 GTEST_DISABLE_MSC_WARNINGS_PUSH_(
0063 4251 GTEST_MAYBE_5046_
0064
0065 )
0066
0067 namespace testing {
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080 class [[nodiscard]] MatchResultListener {
0081 public:
0082
0083
0084
0085 explicit MatchResultListener(::std::ostream* os) : stream_(os) {}
0086 virtual ~MatchResultListener() = 0;
0087
0088
0089
0090 template <typename T>
0091 MatchResultListener& operator<<(const T& x) {
0092 if (stream_ != nullptr) *stream_ << x;
0093 return *this;
0094 }
0095
0096
0097 ::std::ostream* stream() { return stream_; }
0098
0099
0100
0101
0102
0103 bool IsInterested() const { return stream_ != nullptr; }
0104
0105 private:
0106 ::std::ostream* const stream_;
0107
0108 MatchResultListener(const MatchResultListener&) = delete;
0109 MatchResultListener& operator=(const MatchResultListener&) = delete;
0110 };
0111
0112 inline MatchResultListener::~MatchResultListener() = default;
0113
0114
0115
0116 class GTEST_API_ [[nodiscard]] MatcherDescriberInterface {
0117 public:
0118 virtual ~MatcherDescriberInterface() = default;
0119
0120
0121
0122
0123
0124
0125 virtual void DescribeTo(::std::ostream* os) const = 0;
0126
0127
0128
0129
0130
0131
0132
0133 virtual void DescribeNegationTo(::std::ostream* os) const {
0134 *os << "not (";
0135 DescribeTo(os);
0136 *os << ")";
0137 }
0138 };
0139
0140
0141 template <typename T>
0142 class [[nodiscard]] MatcherInterface : public MatcherDescriberInterface {
0143 public:
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175 virtual bool MatchAndExplain(T x, MatchResultListener* listener) const = 0;
0176
0177
0178
0179
0180 };
0181
0182 namespace internal {
0183
0184
0185 class [[nodiscard]] DummyMatchResultListener : public MatchResultListener {
0186 public:
0187 DummyMatchResultListener() : MatchResultListener(nullptr) {}
0188
0189 private:
0190 DummyMatchResultListener(const DummyMatchResultListener&) = delete;
0191 DummyMatchResultListener& operator=(const DummyMatchResultListener&) = delete;
0192 };
0193
0194
0195
0196
0197 class [[nodiscard]] StreamMatchResultListener : public MatchResultListener {
0198 public:
0199 explicit StreamMatchResultListener(::std::ostream* os)
0200 : MatchResultListener(os) {}
0201
0202 private:
0203 StreamMatchResultListener(const StreamMatchResultListener&) = delete;
0204 StreamMatchResultListener& operator=(const StreamMatchResultListener&) =
0205 delete;
0206 };
0207
0208 struct SharedPayloadBase {
0209 std::atomic<int> ref{1};
0210 void Ref() { ref.fetch_add(1, std::memory_order_relaxed); }
0211 bool Unref() { return ref.fetch_sub(1, std::memory_order_acq_rel) == 1; }
0212 };
0213
0214 template <typename T>
0215 struct SharedPayload : SharedPayloadBase {
0216 explicit SharedPayload(const T& v) : value(v) {}
0217 explicit SharedPayload(T&& v) : value(std::move(v)) {}
0218
0219 static void Destroy(SharedPayloadBase* shared) {
0220 delete static_cast<SharedPayload*>(shared);
0221 }
0222
0223 T value;
0224 };
0225
0226
0227
0228
0229 template <typename T>
0230 class [[nodiscard]] MatcherBase : private MatcherDescriberInterface {
0231 public:
0232
0233
0234 bool MatchAndExplain(const T& x, MatchResultListener* listener) const {
0235 GTEST_CHECK_(vtable_ != nullptr);
0236 return vtable_->match_and_explain(*this, x, listener);
0237 }
0238
0239
0240 bool Matches(const T& x) const {
0241 DummyMatchResultListener dummy;
0242 return MatchAndExplain(x, &dummy);
0243 }
0244
0245
0246 void DescribeTo(::std::ostream* os) const final {
0247 GTEST_CHECK_(vtable_ != nullptr);
0248 vtable_->describe(*this, os, false);
0249 }
0250
0251
0252 void DescribeNegationTo(::std::ostream* os) const final {
0253 GTEST_CHECK_(vtable_ != nullptr);
0254 vtable_->describe(*this, os, true);
0255 }
0256
0257
0258 void ExplainMatchResultTo(const T& x, ::std::ostream* os) const {
0259 StreamMatchResultListener listener(os);
0260 MatchAndExplain(x, &listener);
0261 }
0262
0263
0264
0265
0266 const MatcherDescriberInterface* GetDescriber() const {
0267 if (vtable_ == nullptr) return nullptr;
0268 return vtable_->get_describer(*this);
0269 }
0270
0271 protected:
0272 MatcherBase() : vtable_(nullptr), buffer_() {}
0273
0274
0275 template <typename U>
0276 explicit MatcherBase(const MatcherInterface<U>* impl)
0277 : vtable_(nullptr), buffer_() {
0278 Init(impl);
0279 }
0280
0281 template <typename M,
0282 typename = typename std::remove_reference_t<M>::is_gtest_matcher>
0283 MatcherBase(M&& m) : vtable_(nullptr), buffer_() {
0284 Init(std::forward<M>(m));
0285 }
0286
0287 MatcherBase(const MatcherBase& other)
0288 : vtable_(other.vtable_), buffer_(other.buffer_) {
0289 if (IsShared()) buffer_.shared->Ref();
0290 }
0291
0292 MatcherBase& operator=(const MatcherBase& other) {
0293 if (this == &other) return *this;
0294 Destroy();
0295 vtable_ = other.vtable_;
0296 buffer_ = other.buffer_;
0297 if (IsShared()) buffer_.shared->Ref();
0298 return *this;
0299 }
0300
0301 MatcherBase(MatcherBase&& other) noexcept
0302 : vtable_(other.vtable_), buffer_(other.buffer_) {
0303 other.vtable_ = nullptr;
0304 }
0305
0306 MatcherBase& operator=(MatcherBase&& other) noexcept {
0307 if (this == &other) return *this;
0308 Destroy();
0309 vtable_ = other.vtable_;
0310 buffer_ = other.buffer_;
0311 other.vtable_ = nullptr;
0312 return *this;
0313 }
0314
0315 ~MatcherBase() override { Destroy(); }
0316
0317 private:
0318 struct VTable {
0319 bool (*match_and_explain)(const MatcherBase&, const T&,
0320 MatchResultListener*);
0321 void (*describe)(const MatcherBase&, std::ostream*, bool negation);
0322
0323
0324 const MatcherDescriberInterface* (*get_describer)(const MatcherBase&);
0325
0326 void (*shared_destroy)(SharedPayloadBase*);
0327 };
0328
0329 bool IsShared() const {
0330 return vtable_ != nullptr && vtable_->shared_destroy != nullptr;
0331 }
0332
0333
0334 template <typename P>
0335 static auto MatchAndExplainImpl(const MatcherBase& m, const T& value,
0336 MatchResultListener* listener)
0337 -> decltype(P::Get(m).MatchAndExplain(value, listener->stream())) {
0338 return P::Get(m).MatchAndExplain(value, listener->stream());
0339 }
0340
0341 template <typename P>
0342 static auto MatchAndExplainImpl(const MatcherBase& m, const T& value,
0343 MatchResultListener* listener)
0344 -> decltype(P::Get(m).MatchAndExplain(value, listener)) {
0345 return P::Get(m).MatchAndExplain(value, listener);
0346 }
0347
0348 template <typename P>
0349 static void DescribeImpl(const MatcherBase& m, std::ostream* os,
0350 bool negation) {
0351 if (negation) {
0352 P::Get(m).DescribeNegationTo(os);
0353 } else {
0354 P::Get(m).DescribeTo(os);
0355 }
0356 }
0357
0358 template <typename P>
0359 static const MatcherDescriberInterface* GetDescriberImpl(
0360 const MatcherBase& m) {
0361
0362
0363
0364
0365
0366
0367 return std::get<(std::is_convertible_v<decltype(&P::Get(m)),
0368 const MatcherDescriberInterface*>
0369 ? 1
0370 : 0)>(std::make_tuple(&m, &P::Get(m)));
0371 }
0372
0373 template <typename P>
0374 const VTable* GetVTable() {
0375 static constexpr VTable kVTable = {&MatchAndExplainImpl<P>,
0376 &DescribeImpl<P>, &GetDescriberImpl<P>,
0377 P::shared_destroy};
0378 return &kVTable;
0379 }
0380
0381 union Buffer {
0382
0383 void* ptr;
0384 double d;
0385 int64_t i;
0386
0387 SharedPayloadBase* shared;
0388 };
0389
0390 void Destroy() {
0391 if (IsShared() && buffer_.shared->Unref()) {
0392 vtable_->shared_destroy(buffer_.shared);
0393 }
0394 }
0395
0396 template <typename M>
0397 static constexpr bool IsInlined() {
0398 return sizeof(M) <= sizeof(Buffer) && alignof(M) <= alignof(Buffer) &&
0399 std::is_trivially_copy_constructible_v<M> &&
0400 std::is_trivially_destructible_v<M>;
0401 }
0402
0403 template <typename M, bool = MatcherBase::IsInlined<M>()>
0404 struct ValuePolicy {
0405 static const M& Get(const MatcherBase& m) {
0406
0407
0408 const M* ptr =
0409 static_cast<const M*>(static_cast<const void*>(&m.buffer_));
0410 return *ptr;
0411 }
0412 static void Init(MatcherBase& m, M impl) {
0413 ::new (static_cast<void*>(&m.buffer_)) M(impl);
0414 }
0415 static constexpr auto shared_destroy = nullptr;
0416 };
0417
0418 template <typename M>
0419 struct ValuePolicy<M, false> {
0420 using Shared = SharedPayload<M>;
0421 static const M& Get(const MatcherBase& m) {
0422 return static_cast<Shared*>(m.buffer_.shared)->value;
0423 }
0424 template <typename Arg>
0425 static void Init(MatcherBase& m, Arg&& arg) {
0426 m.buffer_.shared = new Shared(std::forward<Arg>(arg));
0427 }
0428 static constexpr auto shared_destroy = &Shared::Destroy;
0429 };
0430
0431 template <typename U, bool B>
0432 struct ValuePolicy<const MatcherInterface<U>*, B> {
0433 using M = const MatcherInterface<U>;
0434 using Shared = SharedPayload<std::unique_ptr<M>>;
0435 static const M& Get(const MatcherBase& m) {
0436 return *static_cast<Shared*>(m.buffer_.shared)->value;
0437 }
0438 static void Init(MatcherBase& m, M* impl) {
0439 m.buffer_.shared = new Shared(std::unique_ptr<M>(impl));
0440 }
0441
0442 static constexpr auto shared_destroy = &Shared::Destroy;
0443 };
0444
0445 template <typename M>
0446 void Init(M&& m) {
0447 using MM = std::decay_t<M>;
0448 using Policy = ValuePolicy<MM>;
0449 vtable_ = GetVTable<Policy>();
0450 Policy::Init(*this, std::forward<M>(m));
0451 }
0452
0453 const VTable* vtable_;
0454 Buffer buffer_;
0455 };
0456
0457 }
0458
0459
0460
0461
0462
0463 template <typename T>
0464 class [[nodiscard]] Matcher : public internal::MatcherBase<T> {
0465 public:
0466
0467
0468
0469 explicit Matcher() {}
0470
0471
0472 explicit Matcher(const MatcherInterface<const T&>* impl)
0473 : internal::MatcherBase<T>(impl) {}
0474
0475 template <typename U>
0476 explicit Matcher(const MatcherInterface<U>* impl,
0477 std::enable_if_t<!std::is_same_v<U, const U&>>* = nullptr)
0478 : internal::MatcherBase<T>(impl) {}
0479
0480 template <typename M,
0481 typename = typename std::remove_reference_t<M>::is_gtest_matcher>
0482 Matcher(M&& m) : internal::MatcherBase<T>(std::forward<M>(m)) {}
0483
0484
0485
0486 Matcher(T value);
0487
0488
0489
0490
0491
0492
0493 template <typename U>
0494 Matcher(U,
0495 std::enable_if_t<std::is_same_v<U, std::nullptr_t>>* = nullptr);
0496 };
0497
0498
0499
0500
0501 template <>
0502 class GTEST_API_ [[nodiscard]]
0503 Matcher<const std::string&> : public internal::MatcherBase<const std::string&> {
0504 public:
0505 Matcher() = default;
0506
0507 explicit Matcher(const MatcherInterface<const std::string&>* impl)
0508 : internal::MatcherBase<const std::string&>(impl) {}
0509
0510 template <typename M,
0511 typename = typename std::remove_reference_t<M>::is_gtest_matcher>
0512 Matcher(M&& m)
0513 : internal::MatcherBase<const std::string&>(std::forward<M>(m)) {}
0514
0515
0516
0517 Matcher(const std::string& s);
0518
0519
0520 Matcher(const char* s);
0521 };
0522
0523 template <>
0524 class GTEST_API_ [[nodiscard]]
0525 Matcher<std::string> : public internal::MatcherBase<std::string> {
0526 public:
0527 Matcher() = default;
0528
0529 explicit Matcher(const MatcherInterface<const std::string&>* impl)
0530 : internal::MatcherBase<std::string>(impl) {}
0531 explicit Matcher(const MatcherInterface<std::string>* impl)
0532 : internal::MatcherBase<std::string>(impl) {}
0533
0534 template <typename M,
0535 typename = typename std::remove_reference_t<M>::is_gtest_matcher>
0536 Matcher(M&& m)
0537 : internal::MatcherBase<std::string>(std::forward<M>(m)) {}
0538
0539
0540
0541 Matcher(const std::string& s);
0542
0543
0544 Matcher(const char* s);
0545 };
0546
0547
0548
0549
0550 template <>
0551 class GTEST_API_ [[nodiscard]] Matcher<const internal::StringView&>
0552 : public internal::MatcherBase<const internal::StringView&> {
0553 public:
0554 Matcher() = default;
0555
0556 explicit Matcher(const MatcherInterface<const internal::StringView&>* impl)
0557 : internal::MatcherBase<const internal::StringView&>(impl) {}
0558
0559 template <typename M,
0560 typename = typename std::remove_reference_t<M>::is_gtest_matcher>
0561 Matcher(M&& m)
0562 : internal::MatcherBase<const internal::StringView&>(std::forward<M>(m)) {
0563 }
0564
0565
0566
0567 Matcher(const std::string& s);
0568
0569
0570 Matcher(const char* s);
0571
0572
0573 Matcher(internal::StringView s);
0574 };
0575
0576 template <>
0577 class GTEST_API_ [[nodiscard]] Matcher<internal::StringView>
0578 : public internal::MatcherBase<internal::StringView> {
0579 public:
0580 Matcher() = default;
0581
0582 explicit Matcher(const MatcherInterface<const internal::StringView&>* impl)
0583 : internal::MatcherBase<internal::StringView>(impl) {}
0584 explicit Matcher(const MatcherInterface<internal::StringView>* impl)
0585 : internal::MatcherBase<internal::StringView>(impl) {}
0586
0587 template <typename M,
0588 typename = typename std::remove_reference_t<M>::is_gtest_matcher>
0589 Matcher(M&& m)
0590 : internal::MatcherBase<internal::StringView>(std::forward<M>(m)) {}
0591
0592
0593
0594 Matcher(const std::string& s);
0595
0596
0597 Matcher(const char* s);
0598
0599
0600 Matcher(internal::StringView s);
0601 };
0602
0603
0604 template <typename T>
0605 std::ostream& operator<<(std::ostream& os, const Matcher<T>& matcher) {
0606 matcher.DescribeTo(&os);
0607 return os;
0608 }
0609
0610
0611
0612
0613
0614
0615
0616
0617
0618
0619
0620
0621
0622 template <class Impl>
0623 class [[nodiscard]] PolymorphicMatcher {
0624 public:
0625 explicit PolymorphicMatcher(const Impl& an_impl) : impl_(an_impl) {}
0626
0627
0628
0629 Impl& mutable_impl() { return impl_; }
0630
0631
0632
0633 const Impl& impl() const { return impl_; }
0634
0635 template <typename T>
0636 operator Matcher<T>() const {
0637 return Matcher<T>(new MonomorphicImpl<const T&>(impl_));
0638 }
0639
0640 private:
0641 template <typename T>
0642 class MonomorphicImpl : public MatcherInterface<T> {
0643 public:
0644 explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {}
0645
0646 void DescribeTo(::std::ostream* os) const override { impl_.DescribeTo(os); }
0647
0648 void DescribeNegationTo(::std::ostream* os) const override {
0649 impl_.DescribeNegationTo(os);
0650 }
0651
0652 bool MatchAndExplain(T x, MatchResultListener* listener) const override {
0653 return impl_.MatchAndExplain(x, listener);
0654 }
0655
0656 private:
0657 const Impl impl_;
0658 };
0659
0660 Impl impl_;
0661 };
0662
0663
0664
0665
0666
0667
0668
0669 template <typename T>
0670 inline Matcher<T> MakeMatcher(const MatcherInterface<T>* impl) {
0671 return Matcher<T>(impl);
0672 }
0673
0674
0675
0676
0677
0678
0679
0680
0681 template <class Impl>
0682 inline PolymorphicMatcher<Impl> MakePolymorphicMatcher(const Impl& impl) {
0683 return PolymorphicMatcher<Impl>(impl);
0684 }
0685
0686 namespace internal {
0687
0688
0689
0690
0691
0692
0693
0694
0695
0696
0697 template <typename D, typename Rhs, typename Op>
0698 class [[nodiscard]] ComparisonBase {
0699 public:
0700 explicit ComparisonBase(const Rhs& rhs) : rhs_(rhs) {}
0701
0702 using is_gtest_matcher = void;
0703
0704 template <typename Lhs>
0705 bool MatchAndExplain(const Lhs& lhs, std::ostream*) const {
0706 return Op()(lhs, Unwrap(rhs_));
0707 }
0708 void DescribeTo(std::ostream* os) const {
0709 *os << D::Desc() << " ";
0710 UniversalPrint(Unwrap(rhs_), os);
0711 }
0712 void DescribeNegationTo(std::ostream* os) const {
0713 *os << D::NegatedDesc() << " ";
0714 UniversalPrint(Unwrap(rhs_), os);
0715 }
0716
0717 private:
0718 template <typename T>
0719 static const T& Unwrap(const T& v) {
0720 return v;
0721 }
0722 template <typename T>
0723 static const T& Unwrap(std::reference_wrapper<T> v) {
0724 return v;
0725 }
0726
0727 Rhs rhs_;
0728 };
0729
0730 template <typename Rhs>
0731 class [[nodiscard]] EqMatcher
0732 : public ComparisonBase<EqMatcher<Rhs>, Rhs, std::equal_to<>> {
0733 public:
0734 explicit EqMatcher(const Rhs& rhs)
0735 : ComparisonBase<EqMatcher<Rhs>, Rhs, std::equal_to<>>(rhs) {}
0736 static const char* Desc() { return "is equal to"; }
0737 static const char* NegatedDesc() { return "isn't equal to"; }
0738 };
0739 template <typename Rhs>
0740 class [[nodiscard]] NeMatcher
0741 : public ComparisonBase<NeMatcher<Rhs>, Rhs, std::not_equal_to<>> {
0742 public:
0743 explicit NeMatcher(const Rhs& rhs)
0744 : ComparisonBase<NeMatcher<Rhs>, Rhs, std::not_equal_to<>>(rhs) {}
0745 static const char* Desc() { return "isn't equal to"; }
0746 static const char* NegatedDesc() { return "is equal to"; }
0747 };
0748 template <typename Rhs>
0749 class [[nodiscard]] LtMatcher
0750 : public ComparisonBase<LtMatcher<Rhs>, Rhs, std::less<>> {
0751 public:
0752 explicit LtMatcher(const Rhs& rhs)
0753 : ComparisonBase<LtMatcher<Rhs>, Rhs, std::less<>>(rhs) {}
0754 static const char* Desc() { return "is <"; }
0755 static const char* NegatedDesc() { return "isn't <"; }
0756 };
0757 template <typename Rhs>
0758 class [[nodiscard]] GtMatcher
0759 : public ComparisonBase<GtMatcher<Rhs>, Rhs, std::greater<>> {
0760 public:
0761 explicit GtMatcher(const Rhs& rhs)
0762 : ComparisonBase<GtMatcher<Rhs>, Rhs, std::greater<>>(rhs) {}
0763 static const char* Desc() { return "is >"; }
0764 static const char* NegatedDesc() { return "isn't >"; }
0765 };
0766 template <typename Rhs>
0767 class [[nodiscard]] LeMatcher
0768 : public ComparisonBase<LeMatcher<Rhs>, Rhs, std::less_equal<>> {
0769 public:
0770 explicit LeMatcher(const Rhs& rhs)
0771 : ComparisonBase<LeMatcher<Rhs>, Rhs, std::less_equal<>>(rhs) {}
0772 static const char* Desc() { return "is <="; }
0773 static const char* NegatedDesc() { return "isn't <="; }
0774 };
0775 template <typename Rhs>
0776 class [[nodiscard]] GeMatcher
0777 : public ComparisonBase<GeMatcher<Rhs>, Rhs, std::greater_equal<>> {
0778 public:
0779 explicit GeMatcher(const Rhs& rhs)
0780 : ComparisonBase<GeMatcher<Rhs>, Rhs, std::greater_equal<>>(rhs) {}
0781 static const char* Desc() { return "is >="; }
0782 static const char* NegatedDesc() { return "isn't >="; }
0783 };
0784
0785
0786
0787 template <typename Rhs, typename StoredRhs>
0788 class [[nodiscard]] ImplicitCastEqMatcher {
0789 public:
0790 explicit ImplicitCastEqMatcher(const StoredRhs& rhs) : stored_rhs_(rhs) {}
0791
0792 using is_gtest_matcher = void;
0793
0794 template <typename Lhs>
0795 bool MatchAndExplain(const Lhs& lhs, std::ostream*) const {
0796 return lhs == rhs();
0797 }
0798
0799 void DescribeTo(std::ostream* os) const {
0800 *os << "is equal to ";
0801 UniversalPrint(rhs(), os);
0802 }
0803 void DescribeNegationTo(std::ostream* os) const {
0804 *os << "isn't equal to ";
0805 UniversalPrint(rhs(), os);
0806 }
0807
0808 private:
0809 Rhs rhs() const { return ImplicitCast_<Rhs>(stored_rhs_); }
0810
0811 StoredRhs stored_rhs_;
0812 };
0813
0814
0815
0816
0817 template <class T>
0818 extern std::enable_if_t<std::is_constructible_v<std::string, T>, std::string>
0819 ResolveAsString(const void* );
0820
0821 #if GTEST_HAS_STD_WSTRING
0822
0823
0824 template <class T>
0825 extern std::enable_if_t<std::is_constructible_v<std::wstring, T>, std::wstring>
0826 ResolveAsString(... );
0827 #endif
0828
0829
0830
0831
0832 template <typename T>
0833 using StringType = decltype(ResolveAsString<T>(nullptr));
0834
0835
0836
0837
0838 class [[nodiscard]] MatchesRegexMatcher {
0839 public:
0840 MatchesRegexMatcher(const RE* regex, bool full_match)
0841 : regex_(regex), full_match_(full_match) {}
0842
0843 bool MatchAndExplain(const internal::StringView& s,
0844 MatchResultListener* listener) const {
0845 return MatchAndExplain(std::string(s), listener);
0846 }
0847
0848
0849
0850
0851
0852
0853 template <typename CharType>
0854 bool MatchAndExplain(CharType* s, MatchResultListener* listener) const {
0855 return s != nullptr && MatchAndExplain(std::string(s), listener);
0856 }
0857
0858
0859
0860
0861
0862 template <class MatcheeStringType>
0863 bool MatchAndExplain(const MatcheeStringType& s,
0864 MatchResultListener* ) const {
0865 const std::string s2(s);
0866 return full_match_ ? RE::FullMatch(s2, *regex_)
0867 : RE::PartialMatch(s2, *regex_);
0868 }
0869
0870 void DescribeTo(::std::ostream* os) const {
0871 *os << (full_match_ ? "matches" : "contains") << " regular expression ";
0872 UniversalPrinter<std::string>::Print(regex_->pattern(), os);
0873 }
0874
0875 void DescribeNegationTo(::std::ostream* os) const {
0876 *os << "doesn't " << (full_match_ ? "match" : "contain")
0877 << " regular expression ";
0878 UniversalPrinter<std::string>::Print(regex_->pattern(), os);
0879 }
0880
0881 private:
0882 const std::shared_ptr<const RE> regex_;
0883 const bool full_match_;
0884 };
0885 }
0886
0887
0888
0889 inline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex(
0890 const internal::RE* regex) {
0891 return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, true));
0892 }
0893 template <typename T = std::string>
0894 std::enable_if_t<std::is_constructible_v<internal::RE, internal::StringType<T>>,
0895 PolymorphicMatcher<internal::MatchesRegexMatcher>>
0896 MatchesRegex(const T& regex) {
0897 return MatchesRegex(new internal::RE(internal::StringType<T>(regex)));
0898 }
0899
0900
0901
0902 inline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex(
0903 const internal::RE* regex) {
0904 return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, false));
0905 }
0906 template <typename T = std::string>
0907 std::enable_if_t<std::is_constructible_v<internal::RE, internal::StringType<T>>,
0908 PolymorphicMatcher<internal::MatchesRegexMatcher>>
0909 ContainsRegex(const T& regex) {
0910 return ContainsRegex(new internal::RE(internal::StringType<T>(regex)));
0911 }
0912
0913
0914
0915
0916 template <typename T>
0917 inline internal::EqMatcher<T> Eq(T x) {
0918 return internal::EqMatcher<T>(x);
0919 }
0920
0921
0922
0923 template <typename T>
0924 Matcher<T>::Matcher(T value) {
0925 *this = Eq(value);
0926 }
0927
0928
0929
0930 template <typename T>
0931 template <typename U>
0932 Matcher<T>::Matcher(U, std::enable_if_t<std::is_same_v<U, std::nullptr_t>>*) {
0933 *this = Eq(nullptr);
0934 }
0935
0936
0937
0938
0939
0940
0941
0942
0943
0944
0945
0946
0947
0948 template <typename Lhs, typename Rhs>
0949 inline Matcher<Lhs> TypedEq(const Rhs& rhs) {
0950 return Eq(rhs);
0951 }
0952
0953
0954 template <typename Rhs>
0955 inline internal::GeMatcher<Rhs> Ge(Rhs x) {
0956 return internal::GeMatcher<Rhs>(x);
0957 }
0958
0959
0960 template <typename Rhs>
0961 inline internal::GtMatcher<Rhs> Gt(Rhs x) {
0962 return internal::GtMatcher<Rhs>(x);
0963 }
0964
0965
0966 template <typename Rhs>
0967 inline internal::LeMatcher<Rhs> Le(Rhs x) {
0968 return internal::LeMatcher<Rhs>(x);
0969 }
0970
0971
0972 template <typename Rhs>
0973 inline internal::LtMatcher<Rhs> Lt(Rhs x) {
0974 return internal::LtMatcher<Rhs>(x);
0975 }
0976
0977
0978 template <typename Rhs>
0979 inline internal::NeMatcher<Rhs> Ne(Rhs x) {
0980 return internal::NeMatcher<Rhs>(x);
0981 }
0982 }
0983
0984 GTEST_DISABLE_MSC_WARNINGS_POP_()
0985
0986 #endif