39 #ifndef GTEST_INCLUDE_GTEST_GTEST_MATCHERS_H_
40 #define GTEST_INCLUDE_GTEST_GTEST_MATCHERS_H_
45 #include <type_traits>
52 #if defined(_MSC_VER) && _MSC_VER >= 1915
53 #define GTEST_MAYBE_5046_ 5046
55 #define GTEST_MAYBE_5046_
80 class MatchResultListener {
85 explicit MatchResultListener(::std::ostream* os) : stream_(os) {}
86 virtual ~MatchResultListener() = 0;
92 if (stream_ !=
nullptr) *stream_ <<
x;
97 ::std::ostream* stream() {
return stream_; }
103 bool IsInterested()
const {
return stream_ !=
nullptr; }
106 ::std::ostream*
const stream_;
111 inline MatchResultListener::~MatchResultListener() {
116 class MatcherDescriberInterface {
118 virtual ~MatcherDescriberInterface() {}
125 virtual void DescribeTo(::std::ostream* os)
const = 0;
133 virtual void DescribeNegationTo(::std::ostream* os)
const {
141 template <
typename T>
142 class MatcherInterface :
public MatcherDescriberInterface {
175 virtual bool MatchAndExplain(
T x, MatchResultListener* listener)
const = 0;
185 template <
typename T>
186 class MatcherInterfaceAdapter :
public MatcherInterface<const T&> {
188 explicit MatcherInterfaceAdapter(
const MatcherInterface<T>* impl)
190 ~MatcherInterfaceAdapter()
override {
delete impl_; }
192 void DescribeTo(::std::ostream* os)
const override { impl_->DescribeTo(os); }
194 void DescribeNegationTo(::std::ostream* os)
const override {
195 impl_->DescribeNegationTo(os);
198 bool MatchAndExplain(
const T&
x,
199 MatchResultListener* listener)
const override {
200 return impl_->MatchAndExplain(x, listener);
204 const MatcherInterface<T>*
const impl_;
210 template <
typename A,
typename B>
211 bool operator()(
const A&
a,
const B& b)
const {
return a == b; }
214 template <
typename A,
typename B>
215 bool operator()(
const A&
a,
const B& b)
const {
return a != b; }
218 template <
typename A,
typename B>
219 bool operator()(
const A&
a,
const B& b)
const {
return a < b; }
222 template <
typename A,
typename B>
223 bool operator()(
const A&
a,
const B& b)
const {
return a > b; }
226 template <
typename A,
typename B>
227 bool operator()(
const A&
a,
const B& b)
const {
return a <= b; }
230 template <
typename A,
typename B>
231 bool operator()(
const A&
a,
const B& b)
const {
return a >= b; }
235 class DummyMatchResultListener :
public MatchResultListener {
237 DummyMatchResultListener() : MatchResultListener(
nullptr) {}
246 class StreamMatchResultListener :
public MatchResultListener {
248 explicit StreamMatchResultListener(::std::ostream* os)
249 : MatchResultListener(os) {}
258 template <
typename T>
263 bool MatchAndExplain(
const T&
x, MatchResultListener* listener)
const {
264 return impl_->MatchAndExplain(x, listener);
268 bool Matches(
const T& x)
const {
269 DummyMatchResultListener dummy;
270 return MatchAndExplain(x, &dummy);
274 void DescribeTo(::std::ostream* os)
const { impl_->DescribeTo(os); }
277 void DescribeNegationTo(::std::ostream* os)
const {
278 impl_->DescribeNegationTo(os);
282 void ExplainMatchResultTo(
const T& x, ::std::ostream* os)
const {
283 StreamMatchResultListener listener(os);
284 MatchAndExplain(x, &listener);
290 const MatcherDescriberInterface* GetDescriber()
const {
298 explicit MatcherBase(
const MatcherInterface<const T&>* impl) : impl_(impl) {}
300 template <
typename U>
301 explicit MatcherBase(
302 const MatcherInterface<U>* impl,
305 : impl_(
new internal::MatcherInterfaceAdapter<U>(impl)) {}
307 MatcherBase(
const MatcherBase&) =
default;
308 MatcherBase& operator=(
const MatcherBase&) =
default;
309 MatcherBase(MatcherBase&&) =
default;
310 MatcherBase& operator=(MatcherBase&&) =
default;
312 virtual ~MatcherBase() {}
315 std::shared_ptr<const MatcherInterface<const T&>> impl_;
324 template <
typename T>
325 class Matcher :
public internal::MatcherBase<T> {
330 explicit Matcher() {}
333 explicit Matcher(
const MatcherInterface<const T&>* impl)
334 : internal::MatcherBase<T>(impl) {}
336 template <
typename U>
338 const MatcherInterface<U>* impl,
341 : internal::MatcherBase<T>(impl) {}
353 :
public internal::MatcherBase<const std::string&> {
357 explicit Matcher(
const MatcherInterface<const std::string&>* impl)
358 : internal::MatcherBase<const std::string&>(impl) {}
362 Matcher(
const std::string& s);
365 Matcher(
const char* s);
370 :
public internal::MatcherBase<std::string> {
374 explicit Matcher(
const MatcherInterface<const std::string&>* impl)
375 : internal::MatcherBase<std::string>(impl) {}
376 explicit Matcher(
const MatcherInterface<std::string>* impl)
377 : internal::MatcherBase<std::string>(impl) {}
381 Matcher(
const std::string& s);
384 Matcher(
const char* s);
387 #if GTEST_INTERNAL_HAS_STRING_VIEW
392 class GTEST_API_ Matcher<const internal::StringView&>
393 :
public internal::MatcherBase<const internal::StringView&> {
397 explicit Matcher(
const MatcherInterface<const internal::StringView&>* impl)
398 : internal::MatcherBase<const internal::StringView&>(impl) {}
402 Matcher(
const std::string& s);
405 Matcher(
const char* s);
408 Matcher(internal::StringView s);
412 class GTEST_API_ Matcher<internal::StringView>
413 :
public internal::MatcherBase<internal::StringView> {
417 explicit Matcher(
const MatcherInterface<const internal::StringView&>* impl)
418 : internal::MatcherBase<internal::StringView>(impl) {}
419 explicit Matcher(
const MatcherInterface<internal::StringView>* impl)
420 : internal::MatcherBase<internal::StringView>(impl) {}
424 Matcher(
const std::string& s);
427 Matcher(
const char* s);
430 Matcher(internal::StringView s);
432 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
435 template <
typename T>
436 std::ostream& operator<<(std::ostream& os, const Matcher<T>& matcher) {
437 matcher.DescribeTo(&os);
453 template <
class Impl>
454 class PolymorphicMatcher {
456 explicit PolymorphicMatcher(
const Impl& an_impl) : impl_(an_impl) {}
460 Impl& mutable_impl() {
return impl_; }
464 const Impl& impl()
const {
return impl_; }
466 template <
typename T>
467 operator Matcher<T>()
const {
468 return Matcher<T>(
new MonomorphicImpl<const T&>(impl_));
472 template <
typename T>
473 class MonomorphicImpl :
public MatcherInterface<T> {
475 explicit MonomorphicImpl(
const Impl& impl) : impl_(impl) {}
477 void DescribeTo(::std::ostream* os)
const override { impl_.DescribeTo(os); }
479 void DescribeNegationTo(::std::ostream* os)
const override {
480 impl_.DescribeNegationTo(os);
483 bool MatchAndExplain(
T x, MatchResultListener* listener)
const override {
484 return impl_.MatchAndExplain(x, listener);
500 template <
typename T>
501 inline Matcher<T> MakeMatcher(
const MatcherInterface<T>* impl) {
502 return Matcher<T>(impl);
512 template <
class Impl>
513 inline PolymorphicMatcher<Impl> MakePolymorphicMatcher(
const Impl& impl) {
514 return PolymorphicMatcher<Impl>(impl);
528 template <
typename D,
typename Rhs,
typename Op>
529 class ComparisonBase {
531 explicit ComparisonBase(
const Rhs& rhs) :
rhs_(rhs) {}
532 template <
typename Lhs>
533 operator Matcher<Lhs>()
const {
534 return Matcher<Lhs>(
new Impl<const Lhs&>(
rhs_));
538 template <
typename T>
539 static const T& Unwrap(
const T& v) {
return v; }
540 template <
typename T>
541 static const T& Unwrap(std::reference_wrapper<T> v) {
return v; }
543 template <
typename Lhs,
typename = Rhs>
544 class Impl :
public MatcherInterface<Lhs> {
546 explicit Impl(
const Rhs& rhs) :
rhs_(rhs) {}
547 bool MatchAndExplain(Lhs lhs,
548 MatchResultListener* )
const override {
549 return Op()(lhs, Unwrap(
rhs_));
551 void DescribeTo(::std::ostream* os)
const override {
552 *os << D::Desc() <<
" ";
555 void DescribeNegationTo(::std::ostream* os)
const override {
556 *os << D::NegatedDesc() <<
" ";
566 template <
typename Rhs>
567 class EqMatcher :
public ComparisonBase<EqMatcher<Rhs>, Rhs, AnyEq> {
569 explicit EqMatcher(
const Rhs& rhs)
570 : ComparisonBase<EqMatcher<Rhs>, Rhs, AnyEq>(rhs) { }
571 static const char* Desc() {
return "is equal to"; }
572 static const char* NegatedDesc() {
return "isn't equal to"; }
574 template <
typename Rhs>
575 class NeMatcher :
public ComparisonBase<NeMatcher<Rhs>, Rhs, AnyNe> {
577 explicit NeMatcher(
const Rhs& rhs)
578 : ComparisonBase<NeMatcher<Rhs>, Rhs, AnyNe>(rhs) { }
579 static const char* Desc() {
return "isn't equal to"; }
580 static const char* NegatedDesc() {
return "is equal to"; }
582 template <
typename Rhs>
583 class LtMatcher :
public ComparisonBase<LtMatcher<Rhs>, Rhs, AnyLt> {
585 explicit LtMatcher(
const Rhs& rhs)
586 : ComparisonBase<LtMatcher<Rhs>, Rhs, AnyLt>(rhs) { }
587 static const char* Desc() {
return "is <"; }
588 static const char* NegatedDesc() {
return "isn't <"; }
590 template <
typename Rhs>
591 class GtMatcher :
public ComparisonBase<GtMatcher<Rhs>, Rhs, AnyGt> {
593 explicit GtMatcher(
const Rhs& rhs)
594 : ComparisonBase<GtMatcher<Rhs>, Rhs, AnyGt>(rhs) { }
595 static const char* Desc() {
return "is >"; }
596 static const char* NegatedDesc() {
return "isn't >"; }
598 template <
typename Rhs>
599 class LeMatcher :
public ComparisonBase<LeMatcher<Rhs>, Rhs, AnyLe> {
601 explicit LeMatcher(
const Rhs& rhs)
602 : ComparisonBase<LeMatcher<Rhs>, Rhs, AnyLe>(rhs) { }
603 static const char* Desc() {
return "is <="; }
604 static const char* NegatedDesc() {
return "isn't <="; }
606 template <
typename Rhs>
607 class GeMatcher :
public ComparisonBase<GeMatcher<Rhs>, Rhs, AnyGe> {
609 explicit GeMatcher(
const Rhs& rhs)
610 : ComparisonBase<GeMatcher<Rhs>, Rhs, AnyGe>(rhs) { }
611 static const char* Desc() {
return "is >="; }
612 static const char* NegatedDesc() {
return "isn't >="; }
615 template <
typename T,
typename =
typename std::enable_if<
617 using StringLike =
T;
622 class MatchesRegexMatcher {
624 MatchesRegexMatcher(
const RE* regex,
bool full_match)
625 : regex_(regex), full_match_(full_match) {}
627 #if GTEST_INTERNAL_HAS_STRING_VIEW
628 bool MatchAndExplain(
const internal::StringView& s,
629 MatchResultListener* listener)
const {
630 return MatchAndExplain(std::string(s), listener);
632 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
639 template <
typename CharType>
640 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
641 return s !=
nullptr && MatchAndExplain(std::string(s), listener);
648 template <
class MatcheeStringType>
649 bool MatchAndExplain(
const MatcheeStringType& s,
650 MatchResultListener* )
const {
651 const std::string& s2(s);
652 return full_match_ ? RE::FullMatch(s2, *regex_)
653 : RE::PartialMatch(s2, *regex_);
656 void DescribeTo(::std::ostream* os)
const {
657 *os << (full_match_ ?
"matches" :
"contains") <<
" regular expression ";
661 void DescribeNegationTo(::std::ostream* os)
const {
662 *os <<
"doesn't " << (full_match_ ?
"match" :
"contain")
663 <<
" regular expression ";
668 const std::shared_ptr<const RE> regex_;
669 const bool full_match_;
675 inline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex(
676 const internal::RE* regex) {
677 return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex,
true));
679 template <
typename T = std::
string>
680 PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex(
681 const internal::StringLike<T>& regex) {
682 return MatchesRegex(
new internal::RE(std::string(regex)));
687 inline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex(
688 const internal::RE* regex) {
689 return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex,
false));
691 template <
typename T = std::
string>
692 PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex(
693 const internal::StringLike<T>& regex) {
694 return ContainsRegex(
new internal::RE(std::string(regex)));
700 template <
typename T>
701 inline internal::EqMatcher<T> Eq(
T x) {
return internal::EqMatcher<T>(
x); }
705 template <
typename T>
706 Matcher<T>::Matcher(
T value) { *
this = Eq(value); }
720 template <
typename Lhs,
typename Rhs>
721 inline Matcher<Lhs> TypedEq(
const Rhs& rhs) {
return Eq(rhs); }
724 template <
typename Rhs>
725 inline internal::GeMatcher<Rhs> Ge(Rhs x) {
726 return internal::GeMatcher<Rhs>(
x);
730 template <
typename Rhs>
731 inline internal::GtMatcher<Rhs> Gt(Rhs x) {
732 return internal::GtMatcher<Rhs>(
x);
736 template <
typename Rhs>
737 inline internal::LeMatcher<Rhs> Le(Rhs x) {
738 return internal::LeMatcher<Rhs>(
x);
742 template <
typename Rhs>
743 inline internal::LtMatcher<Rhs> Lt(Rhs x) {
744 return internal::LtMatcher<Rhs>(
x);
748 template <
typename Rhs>
749 inline internal::NeMatcher<Rhs> Ne(Rhs x) {
750 return internal::NeMatcher<Rhs>(
x);
756 #endif // GTEST_INCLUDE_GTEST_GTEST_MATCHERS_H_
#define GTEST_DISABLE_MSC_WARNINGS_POP_()
std::string Print(const T &value)
#define GTEST_MAYBE_5046_
std::ostream & operator<<(std::ostream &os, const Expr< T > &xx)
#define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings)
void UniversalPrint(const T &value,::std::ostream *os)
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)