39 #ifndef GTEST_INCLUDE_GTEST_GTEST_MATCHERS_H_
40 #define GTEST_INCLUDE_GTEST_GTEST_MATCHERS_H_
72 class MatchResultListener {
77 explicit MatchResultListener(::std::ostream* os) : stream_(os) {}
78 virtual ~MatchResultListener() = 0;
84 if (stream_ !=
nullptr) *stream_ << x;
89 ::std::ostream*
stream() {
return stream_; }
95 bool IsInterested()
const {
return stream_ !=
nullptr; }
98 ::std::ostream*
const stream_;
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);
260 bool Matches(
const T& x)
const {
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,
295 typename internal::EnableIf<
296 !internal::IsSame<U, const U&>::value>::type* =
nullptr)
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,
330 typename internal::EnableIf<
331 !internal::IsSame<U, const U&>::value>::type* =
nullptr)
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
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
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);
444 class GTEST_API_ Matcher<const absl::string_view&>
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
463 Matcher(
const char* s);
466 Matcher(absl::string_view 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
491 Matcher(
const char* s);
494 Matcher(absl::string_view s);
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>
577 inline PolymorphicMatcher<Impl> MakePolymorphicMatcher(
const Impl& 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 T>
603 static const T& Unwrap(
const T& v) {
return v; }
604 template <
typename T>
605 static const T& Unwrap(std::reference_wrapper<T> v) {
return v; }
607 template <
typename Lhs,
typename = Rhs>
608 class Impl :
public MatcherInterface<Lhs> {
610 explicit Impl(
const Rhs& rhs) : rhs_(rhs) {}
611 bool MatchAndExplain(Lhs lhs,
612 MatchResultListener* )
const override {
613 return Op()(lhs, Unwrap(rhs_));
615 void DescribeTo(::std::ostream* os)
const override {
616 *os << D::Desc() <<
" ";
619 void DescribeNegationTo(::std::ostream* os)
const override {
620 *os << D::NegatedDesc() <<
" ";
630 template <
typename Rhs>
631 class EqMatcher :
public ComparisonBase<EqMatcher<Rhs>, Rhs, AnyEq> {
633 explicit EqMatcher(
const Rhs& rhs)
634 : ComparisonBase<EqMatcher<Rhs>, Rhs, AnyEq>(rhs) { }
635 static const char* Desc() {
return "is equal to"; }
636 static const char* NegatedDesc() {
return "isn't equal to"; }
638 template <
typename Rhs>
639 class NeMatcher :
public ComparisonBase<NeMatcher<Rhs>, Rhs, AnyNe> {
641 explicit NeMatcher(
const Rhs& rhs)
642 : ComparisonBase<NeMatcher<Rhs>, Rhs, AnyNe>(rhs) { }
643 static const char* Desc() {
return "isn't equal to"; }
644 static const char* NegatedDesc() {
return "is equal to"; }
646 template <
typename Rhs>
647 class LtMatcher :
public ComparisonBase<LtMatcher<Rhs>, Rhs, AnyLt> {
649 explicit LtMatcher(
const Rhs& rhs)
650 : ComparisonBase<LtMatcher<Rhs>, Rhs, AnyLt>(rhs) { }
651 static const char* Desc() {
return "is <"; }
652 static const char* NegatedDesc() {
return "isn't <"; }
654 template <
typename Rhs>
655 class GtMatcher :
public ComparisonBase<GtMatcher<Rhs>, Rhs, AnyGt> {
657 explicit GtMatcher(
const Rhs& rhs)
658 : ComparisonBase<GtMatcher<Rhs>, Rhs, AnyGt>(rhs) { }
659 static const char* Desc() {
return "is >"; }
660 static const char* NegatedDesc() {
return "isn't >"; }
662 template <
typename Rhs>
663 class LeMatcher :
public ComparisonBase<LeMatcher<Rhs>, Rhs, AnyLe> {
665 explicit LeMatcher(
const Rhs& rhs)
666 : ComparisonBase<LeMatcher<Rhs>, Rhs, AnyLe>(rhs) { }
667 static const char* Desc() {
return "is <="; }
668 static const char* NegatedDesc() {
return "isn't <="; }
670 template <
typename Rhs>
671 class GeMatcher :
public ComparisonBase<GeMatcher<Rhs>, Rhs, AnyGe> {
673 explicit GeMatcher(
const Rhs& rhs)
674 : ComparisonBase<GeMatcher<Rhs>, Rhs, AnyGe>(rhs) { }
675 static const char* Desc() {
return "is >="; }
676 static const char* NegatedDesc() {
return "isn't >="; }
682 class MatchesRegexMatcher {
684 MatchesRegexMatcher(
const RE* regex,
bool full_match)
685 : regex_(regex), full_match_(full_match) {}
688 bool MatchAndExplain(
const absl::string_view& s,
689 MatchResultListener* listener)
const {
690 return MatchAndExplain(
string(s), listener);
699 template <
typename CharType>
700 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
701 return s !=
nullptr && MatchAndExplain(
std::string(s), listener);
708 template <
class MatcheeStringType>
709 bool MatchAndExplain(
const MatcheeStringType& s,
710 MatchResultListener* )
const {
712 return full_match_ ? RE::FullMatch(s2, *regex_)
713 : RE::PartialMatch(s2, *regex_);
716 void DescribeTo(::std::ostream* os)
const {
717 *os << (full_match_ ?
"matches" :
"contains") <<
" regular expression ";
718 UniversalPrinter<std::string>::Print(regex_->pattern(), os);
721 void DescribeNegationTo(::std::ostream* os)
const {
722 *os <<
"doesn't " << (full_match_ ?
"match" :
"contain")
723 <<
" regular expression ";
724 UniversalPrinter<std::string>::Print(regex_->pattern(), os);
728 const std::shared_ptr<const RE> regex_;
729 const bool full_match_;
735 inline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex(
736 const internal::RE* regex) {
737 return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex,
true));
739 inline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex(
741 return MatchesRegex(
new internal::RE(regex));
746 inline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex(
747 const internal::RE* regex) {
748 return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex,
false));
750 inline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex(
752 return ContainsRegex(
new internal::RE(regex));
758 template <
typename T>
759 inline internal::EqMatcher<T> Eq(T x) {
return internal::EqMatcher<T>(x); }
763 template <
typename T>
764 Matcher<T>::Matcher(T value) { *
this = Eq(value); }
778 template <
typename Lhs,
typename Rhs>
779 inline Matcher<Lhs> TypedEq(
const Rhs& rhs) {
return Eq(rhs); }
782 template <
typename Rhs>
783 inline internal::GeMatcher<Rhs> Ge(Rhs x) {
784 return internal::GeMatcher<Rhs>(x);
788 template <
typename Rhs>
789 inline internal::GtMatcher<Rhs> Gt(Rhs x) {
790 return internal::GtMatcher<Rhs>(x);
794 template <
typename Rhs>
795 inline internal::LeMatcher<Rhs> Le(Rhs x) {
796 return internal::LeMatcher<Rhs>(x);
800 template <
typename Rhs>
801 inline internal::LtMatcher<Rhs> Lt(Rhs x) {
802 return internal::LtMatcher<Rhs>(x);
806 template <
typename Rhs>
807 inline internal::NeMatcher<Rhs> Ne(Rhs x) {
808 return internal::NeMatcher<Rhs>(x);
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 5046) namespace testing
#define GTEST_DISABLE_MSC_WARNINGS_POP_()
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
std::ostream & operator<<(std::ostream &output, const CloverFieldParam ¶m)
void UniversalPrint(const T &value, ::std::ostream *os)