39 #ifndef GTEST_INCLUDE_GTEST_GTEST_MATCHERS_H_
40 #define GTEST_INCLUDE_GTEST_GTEST_MATCHERS_H_
45 #include <type_traits>
47 #include "gtest/gtest-printers.h"
48 #include "gtest/internal/gtest-internal.h"
49 #include "gtest/internal/gtest-port.h"
52 #if defined(_MSC_VER) && _MSC_VER >= 1915
53 #define GTEST_MAYBE_5046_ 5046
55 #define GTEST_MAYBE_5046_
80 class MatchResultListener {
116 class MatcherDescriberInterface {
125 virtual void DescribeTo(::std::ostream* os)
const = 0;
141 template <
typename T>
142 class MatcherInterface :
public MatcherDescriberInterface {
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>
214 template <
typename A,
typename B>
218 template <
typename A,
typename B>
222 template <
typename A,
typename B>
226 template <
typename A,
typename B>
230 template <
typename A,
typename B>
235 class DummyMatchResultListener :
public MatchResultListener {
246 class StreamMatchResultListener :
public MatchResultListener {
258 template <
typename T>
264 return impl_->MatchAndExplain(
x, listener);
269 DummyMatchResultListener
dummy;
278 impl_->DescribeNegationTo(os);
283 StreamMatchResultListener listener(os);
298 explicit MatcherBase(
const MatcherInterface<const T&>* impl) :
impl_(impl) {}
300 template <
typename U>
302 const MatcherInterface<U>* impl,
305 :
impl_(
new internal::MatcherInterfaceAdapter<U>(impl)) {}
315 std::shared_ptr<const MatcherInterface<const T&>>
impl_;
324 template <
typename T>
325 class Matcher :
public internal::MatcherBase<T> {
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) {}
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) {}
384 Matcher(
const char* s);
393 :
public internal::MatcherBase<const absl::string_view&> {
397 explicit Matcher(
const MatcherInterface<const absl::string_view&>* impl)
398 : internal::MatcherBase<const absl::string_view&>(impl) {}
405 Matcher(
const char* s);
413 :
public internal::MatcherBase<absl::string_view> {
417 explicit Matcher(
const MatcherInterface<const absl::string_view&>* impl)
418 : internal::MatcherBase<absl::string_view>(impl) {}
419 explicit Matcher(
const MatcherInterface<absl::string_view>* impl)
420 : internal::MatcherBase<absl::string_view>(impl) {}
427 Matcher(
const char* s);
432 #endif // GTEST_HAS_ABSL
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 {
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> {
477 virtual void DescribeTo(::std::ostream* os)
const {
impl_.DescribeTo(os); }
480 impl_.DescribeNegationTo(os);
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>
514 return PolymorphicMatcher<Impl>(impl);
528 template <
typename D,
typename Rhs,
typename Op>
529 class ComparisonBase {
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) {}
548 MatchResultListener* )
const override {
549 return Op()(lhs, Unwrap(
rhs_));
551 void DescribeTo(::std::ostream* os)
const override {
552 *os << D::Desc() <<
" ";
556 *os << D::NegatedDesc() <<
" ";
566 template <
typename Rhs>
567 class EqMatcher :
public ComparisonBase<EqMatcher<Rhs>, Rhs, AnyEq> {
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> {
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> {
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> {
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> {
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> {
610 : ComparisonBase<GeMatcher<Rhs>, Rhs, AnyGe>(rhs) { }
611 static const char*
Desc() {
return "is >="; }
612 static const char*
NegatedDesc() {
return "isn't >="; }
618 class MatchesRegexMatcher {
625 MatchResultListener* listener)
const {
628 #endif // GTEST_HAS_ABSL
635 template <
typename CharType>
636 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
644 template <
class MatcheeStringType>
646 MatchResultListener* )
const {
653 *os << (
full_match_ ?
"matches" :
"contains") <<
" regular expression ";
658 *os <<
"doesn't " << (
full_match_ ?
"match" :
"contain")
659 <<
" regular expression ";
664 const std::shared_ptr<const RE>
regex_;
671 inline PolymorphicMatcher<internal::MatchesRegexMatcher>
MatchesRegex(
672 const internal::RE* regex) {
675 inline PolymorphicMatcher<internal::MatchesRegexMatcher>
MatchesRegex(
682 inline PolymorphicMatcher<internal::MatchesRegexMatcher>
ContainsRegex(
683 const internal::RE* regex) {
686 inline PolymorphicMatcher<internal::MatchesRegexMatcher>
ContainsRegex(
694 template <
typename T>
695 inline internal::EqMatcher<T>
Eq(
T x) {
return internal::EqMatcher<T>(
x); }
699 template <
typename T>
714 template <
typename Lhs,
typename Rhs>
715 inline Matcher<Lhs>
TypedEq(
const Rhs& rhs) {
return Eq(rhs); }
718 template <
typename Rhs>
719 inline internal::GeMatcher<Rhs>
Ge(Rhs
x) {
720 return internal::GeMatcher<Rhs>(
x);
724 template <
typename Rhs>
725 inline internal::GtMatcher<Rhs>
Gt(Rhs
x) {
726 return internal::GtMatcher<Rhs>(
x);
730 template <
typename Rhs>
731 inline internal::LeMatcher<Rhs>
Le(Rhs
x) {
732 return internal::LeMatcher<Rhs>(
x);
736 template <
typename Rhs>
737 inline internal::LtMatcher<Rhs>
Lt(Rhs
x) {
738 return internal::LtMatcher<Rhs>(
x);
742 template <
typename Rhs>
743 inline internal::NeMatcher<Rhs>
Ne(Rhs
x) {
744 return internal::NeMatcher<Rhs>(
x);
750 #endif // GTEST_INCLUDE_GTEST_GTEST_MATCHERS_H_