39 #ifndef GTEST_INCLUDE_GTEST_GTEST_MATCHERS_H_
40 #define GTEST_INCLUDE_GTEST_GTEST_MATCHERS_H_
46 #include "gtest/gtest-printers.h"
47 #include "gtest/internal/gtest-internal.h"
48 #include "gtest/internal/gtest-port.h"
72 class MatchResultListener {
77 explicit MatchResultListener(::std::ostream* os) :
stream_(os) {}
78 virtual ~MatchResultListener() = 0;
95 bool IsInterested()
const {
return stream_ !=
nullptr; }
103 inline MatchResultListener::~MatchResultListener() {
108 class MatcherDescriberInterface {
110 virtual ~MatcherDescriberInterface() {}
117 virtual void DescribeTo(::std::ostream* os)
const = 0;
125 virtual void DescribeNegationTo(::std::ostream* os)
const {
133 template <
typename T>
134 class MatcherInterface :
public MatcherDescriberInterface {
167 virtual bool MatchAndExplain(
T x, MatchResultListener* listener)
const = 0;
177 template <
typename T>
178 class MatcherInterfaceAdapter :
public MatcherInterface<const T&> {
180 explicit MatcherInterfaceAdapter(
const MatcherInterface<T>* impl)
182 ~MatcherInterfaceAdapter()
override {
delete impl_; }
184 void DescribeTo(::std::ostream* os)
const override {
impl_->DescribeTo(os); }
186 void DescribeNegationTo(::std::ostream* os)
const override {
187 impl_->DescribeNegationTo(os);
190 bool MatchAndExplain(
const T&
x,
191 MatchResultListener* listener)
const override {
192 return impl_->MatchAndExplain(
x, listener);
196 const MatcherInterface<T>*
const impl_;
202 template <
typename A,
typename B>
203 bool operator()(
const A&
a,
const B&
b)
const {
return a ==
b; }
206 template <
typename A,
typename B>
207 bool operator()(
const A&
a,
const B&
b)
const {
return a !=
b; }
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; }
227 class DummyMatchResultListener :
public MatchResultListener {
229 DummyMatchResultListener() : MatchResultListener(
nullptr) {}
238 class StreamMatchResultListener :
public MatchResultListener {
240 explicit StreamMatchResultListener(::std::ostream* os)
241 : MatchResultListener(os) {}
250 template <
typename T>
255 bool MatchAndExplain(
const T&
x, MatchResultListener* listener)
const {
256 return impl_->MatchAndExplain(
x, listener);
261 DummyMatchResultListener
dummy;
262 return MatchAndExplain(
x, &
dummy);
266 void DescribeTo(::std::ostream* os)
const {
impl_->DescribeTo(os); }
269 void DescribeNegationTo(::std::ostream* os)
const {
270 impl_->DescribeNegationTo(os);
274 void ExplainMatchResultTo(
const T&
x, ::std::ostream* os)
const {
275 StreamMatchResultListener listener(os);
276 MatchAndExplain(
x, &listener);
282 const MatcherDescriberInterface* GetDescriber()
const {
290 explicit MatcherBase(
const MatcherInterface<const T&>* impl) :
impl_(impl) {}
292 template <
typename U>
293 explicit MatcherBase(
294 const MatcherInterface<U>* impl,
297 :
impl_(
new internal::MatcherInterfaceAdapter<U>(impl)) {}
299 MatcherBase(
const MatcherBase&) =
default;
300 MatcherBase& operator=(
const MatcherBase&) =
default;
301 MatcherBase(MatcherBase&&) =
default;
302 MatcherBase& operator=(MatcherBase&&) =
default;
304 virtual ~MatcherBase() {}
307 std::shared_ptr<const MatcherInterface<const T&>>
impl_;
316 template <
typename T>
317 class Matcher :
public internal::MatcherBase<T> {
322 explicit Matcher() {}
325 explicit Matcher(
const MatcherInterface<const T&>* impl)
326 : internal::MatcherBase<T>(impl) {}
328 template <
typename U>
329 explicit Matcher(
const MatcherInterface<U>* impl,
332 : internal::MatcherBase<T>(impl) {}
344 :
public internal::MatcherBase<const std::string&> {
348 explicit Matcher(
const MatcherInterface<const std::string&>* impl)
349 : internal::MatcherBase<const std::string&>(impl) {}
355 #if GTEST_HAS_GLOBAL_STRING
359 #endif // GTEST_HAS_GLOBAL_STRING
362 Matcher(
const char* s);
367 :
public internal::MatcherBase<std::string> {
371 explicit Matcher(
const MatcherInterface<const std::string&>* impl)
372 : internal::MatcherBase<std::string>(impl) {}
373 explicit Matcher(
const MatcherInterface<std::string>* impl)
374 : internal::MatcherBase<std::string>(impl) {}
380 #if GTEST_HAS_GLOBAL_STRING
384 #endif // GTEST_HAS_GLOBAL_STRING
387 Matcher(
const char* s);
390 #if GTEST_HAS_GLOBAL_STRING
396 :
public internal::MatcherBase<const ::string&> {
400 explicit Matcher(
const MatcherInterface<const ::string&>* impl)
401 : internal::MatcherBase<const ::string&>(impl) {}
412 Matcher(
const char* s);
417 :
public internal::MatcherBase< ::string> {
421 explicit Matcher(
const MatcherInterface<const ::string&>* impl)
422 : internal::MatcherBase< ::string>(impl) {}
423 explicit Matcher(
const MatcherInterface< ::string>* impl)
424 : internal::MatcherBase< ::string>(impl) {}
435 Matcher(
const char* s);
437 #endif // GTEST_HAS_GLOBAL_STRING
445 :
public internal::MatcherBase<const absl::string_view&> {
449 explicit Matcher(
const MatcherInterface<const absl::string_view&>* impl)
450 : internal::MatcherBase<const absl::string_view&>(impl) {}
456 #if GTEST_HAS_GLOBAL_STRING
460 #endif // GTEST_HAS_GLOBAL_STRING
463 Matcher(
const char* s);
471 :
public internal::MatcherBase<absl::string_view> {
475 explicit Matcher(
const MatcherInterface<const absl::string_view&>* impl)
476 : internal::MatcherBase<absl::string_view>(impl) {}
477 explicit Matcher(
const MatcherInterface<absl::string_view>* impl)
478 : internal::MatcherBase<absl::string_view>(impl) {}
484 #if GTEST_HAS_GLOBAL_STRING
488 #endif // GTEST_HAS_GLOBAL_STRING
491 Matcher(
const char* s);
496 #endif // GTEST_HAS_ABSL
499 template <
typename T>
500 std::ostream&
operator<<(std::ostream& os,
const Matcher<T>& matcher) {
501 matcher.DescribeTo(&os);
517 template <
class Impl>
518 class PolymorphicMatcher {
520 explicit PolymorphicMatcher(
const Impl& an_impl) :
impl_(an_impl) {}
524 Impl& mutable_impl() {
return impl_; }
528 const Impl& impl()
const {
return impl_; }
530 template <
typename T>
531 operator Matcher<T>()
const {
532 return Matcher<T>(
new MonomorphicImpl<const T&>(
impl_));
536 template <
typename T>
537 class MonomorphicImpl :
public MatcherInterface<T> {
539 explicit MonomorphicImpl(
const Impl& impl) :
impl_(impl) {}
541 virtual void DescribeTo(::std::ostream* os)
const {
impl_.DescribeTo(os); }
543 virtual void DescribeNegationTo(::std::ostream* os)
const {
544 impl_.DescribeNegationTo(os);
547 virtual bool MatchAndExplain(
T x, MatchResultListener* listener)
const {
548 return impl_.MatchAndExplain(
x, listener);
564 template <
typename T>
565 inline Matcher<T>
MakeMatcher(
const MatcherInterface<T>* impl) {
566 return Matcher<T>(impl);
576 template <
class Impl>
578 return PolymorphicMatcher<Impl>(impl);
592 template <
typename D,
typename Rhs,
typename Op>
593 class ComparisonBase {
595 explicit ComparisonBase(
const Rhs& rhs) :
rhs_(rhs) {}
596 template <
typename Lhs>
597 operator Matcher<Lhs>()
const {
598 return Matcher<Lhs>(
new Impl<const Lhs&>(
rhs_));
602 template <
typename Lhs>
603 class Impl :
public MatcherInterface<Lhs> {
605 explicit Impl(
const Rhs& rhs) :
rhs_(rhs) {}
606 bool MatchAndExplain(Lhs lhs,
607 MatchResultListener* )
const override {
608 return Op()(lhs,
rhs_);
610 void DescribeTo(::std::ostream* os)
const override {
611 *os << D::Desc() <<
" ";
614 void DescribeNegationTo(::std::ostream* os)
const override {
615 *os << D::NegatedDesc() <<
" ";
625 template <
typename Rhs>
626 class EqMatcher :
public ComparisonBase<EqMatcher<Rhs>, Rhs, AnyEq> {
628 explicit EqMatcher(
const Rhs& rhs)
629 : ComparisonBase<EqMatcher<Rhs>, Rhs, AnyEq>(rhs) { }
630 static const char* Desc() {
return "is equal to"; }
631 static const char* NegatedDesc() {
return "isn't equal to"; }
633 template <
typename Rhs>
634 class NeMatcher :
public ComparisonBase<NeMatcher<Rhs>, Rhs, AnyNe> {
636 explicit NeMatcher(
const Rhs& rhs)
637 : ComparisonBase<NeMatcher<Rhs>, Rhs, AnyNe>(rhs) { }
638 static const char* Desc() {
return "isn't equal to"; }
639 static const char* NegatedDesc() {
return "is equal to"; }
641 template <
typename Rhs>
642 class LtMatcher :
public ComparisonBase<LtMatcher<Rhs>, Rhs, AnyLt> {
644 explicit LtMatcher(
const Rhs& rhs)
645 : ComparisonBase<LtMatcher<Rhs>, Rhs, AnyLt>(rhs) { }
646 static const char* Desc() {
return "is <"; }
647 static const char* NegatedDesc() {
return "isn't <"; }
649 template <
typename Rhs>
650 class GtMatcher :
public ComparisonBase<GtMatcher<Rhs>, Rhs, AnyGt> {
652 explicit GtMatcher(
const Rhs& rhs)
653 : ComparisonBase<GtMatcher<Rhs>, Rhs, AnyGt>(rhs) { }
654 static const char* Desc() {
return "is >"; }
655 static const char* NegatedDesc() {
return "isn't >"; }
657 template <
typename Rhs>
658 class LeMatcher :
public ComparisonBase<LeMatcher<Rhs>, Rhs, AnyLe> {
660 explicit LeMatcher(
const Rhs& rhs)
661 : ComparisonBase<LeMatcher<Rhs>, Rhs, AnyLe>(rhs) { }
662 static const char* Desc() {
return "is <="; }
663 static const char* NegatedDesc() {
return "isn't <="; }
665 template <
typename Rhs>
666 class GeMatcher :
public ComparisonBase<GeMatcher<Rhs>, Rhs, AnyGe> {
668 explicit GeMatcher(
const Rhs& rhs)
669 : ComparisonBase<GeMatcher<Rhs>, Rhs, AnyGe>(rhs) { }
670 static const char* Desc() {
return "is >="; }
671 static const char* NegatedDesc() {
return "isn't >="; }
677 class MatchesRegexMatcher {
679 MatchesRegexMatcher(
const RE* regex,
bool full_match)
680 : regex_(regex), full_match_(full_match) {}
684 MatchResultListener* listener)
const {
685 return MatchAndExplain(
string(s), listener);
687 #endif // GTEST_HAS_ABSL
694 template <
typename CharType>
695 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
696 return s !=
nullptr && MatchAndExplain(
std::string(s), listener);
703 template <
class MatcheeStringType>
704 bool MatchAndExplain(
const MatcheeStringType& s,
705 MatchResultListener* )
const {
707 return full_match_ ? RE::FullMatch(s2, *regex_)
708 : RE::PartialMatch(s2, *regex_);
711 void DescribeTo(::std::ostream* os)
const {
712 *os << (full_match_ ?
"matches" :
"contains") <<
" regular expression ";
716 void DescribeNegationTo(::std::ostream* os)
const {
717 *os <<
"doesn't " << (full_match_ ?
"match" :
"contain")
718 <<
" regular expression ";
723 const std::shared_ptr<const RE> regex_;
724 const bool full_match_;
730 inline PolymorphicMatcher<internal::MatchesRegexMatcher>
MatchesRegex(
731 const internal::RE* regex) {
734 inline PolymorphicMatcher<internal::MatchesRegexMatcher>
MatchesRegex(
741 inline PolymorphicMatcher<internal::MatchesRegexMatcher>
ContainsRegex(
742 const internal::RE* regex) {
745 inline PolymorphicMatcher<internal::MatchesRegexMatcher>
ContainsRegex(
753 template <
typename T>
754 inline internal::EqMatcher<T>
Eq(
T x) {
return internal::EqMatcher<T>(
x); }
758 template <
typename T>
773 template <
typename Lhs,
typename Rhs>
774 inline Matcher<Lhs>
TypedEq(
const Rhs& rhs) {
return Eq(rhs); }
777 template <
typename Rhs>
778 inline internal::GeMatcher<Rhs>
Ge(Rhs
x) {
779 return internal::GeMatcher<Rhs>(
x);
783 template <
typename Rhs>
784 inline internal::GtMatcher<Rhs>
Gt(Rhs
x) {
785 return internal::GtMatcher<Rhs>(
x);
789 template <
typename Rhs>
790 inline internal::LeMatcher<Rhs>
Le(Rhs
x) {
791 return internal::LeMatcher<Rhs>(
x);
795 template <
typename Rhs>
796 inline internal::LtMatcher<Rhs>
Lt(Rhs
x) {
797 return internal::LtMatcher<Rhs>(
x);
801 template <
typename Rhs>
802 inline internal::NeMatcher<Rhs>
Ne(Rhs
x) {
803 return internal::NeMatcher<Rhs>(
x);
809 #endif // GTEST_INCLUDE_GTEST_GTEST_MATCHERS_H_