39 #ifndef GTEST_INCLUDE_GTEST_GTEST_MATCHERS_H_ 40 #define GTEST_INCLUDE_GTEST_GTEST_MATCHERS_H_ 51 #if defined(_MSC_VER) && _MSC_VER >= 1915 52 #define GTEST_MAYBE_5046_ 5046 54 #define GTEST_MAYBE_5046_ 79 class MatchResultListener {
84 explicit MatchResultListener(::std::ostream* os) : stream_(os) {}
85 virtual ~MatchResultListener() = 0;
91 if (stream_ !=
nullptr) *stream_ <<
x;
96 ::std::ostream* stream() {
return stream_; }
102 bool IsInterested()
const {
return stream_ !=
nullptr; }
105 ::std::ostream*
const stream_;
110 inline MatchResultListener::~MatchResultListener() {
115 class MatcherDescriberInterface {
117 virtual ~MatcherDescriberInterface() {}
124 virtual void DescribeTo(::std::ostream* os)
const = 0;
132 virtual void DescribeNegationTo(::std::ostream* os)
const {
140 template <
typename T>
141 class MatcherInterface :
public MatcherDescriberInterface {
174 virtual bool MatchAndExplain(T
x, MatchResultListener* listener)
const = 0;
184 template <
typename T>
185 class MatcherInterfaceAdapter :
public MatcherInterface<const T&> {
187 explicit MatcherInterfaceAdapter(
const MatcherInterface<T>* impl)
189 ~MatcherInterfaceAdapter()
override {
delete impl_; }
191 void DescribeTo(::std::ostream* os)
const override { impl_->DescribeTo(os); }
193 void DescribeNegationTo(::std::ostream* os)
const override {
194 impl_->DescribeNegationTo(os);
197 bool MatchAndExplain(
const T&
x,
198 MatchResultListener* listener)
const override {
199 return impl_->MatchAndExplain(x, listener);
203 const MatcherInterface<T>*
const impl_;
209 template <
typename A,
typename B>
210 bool operator()(
const A& a,
const B& b)
const {
return a == b; }
213 template <
typename A,
typename B>
214 bool operator()(
const A& a,
const B& b)
const {
return a != b; }
217 template <
typename A,
typename B>
218 bool operator()(
const A& a,
const B& b)
const {
return a < b; }
221 template <
typename A,
typename B>
222 bool operator()(
const A& a,
const B& b)
const {
return a > b; }
225 template <
typename A,
typename B>
226 bool operator()(
const A& a,
const B& b)
const {
return a <= b; }
229 template <
typename A,
typename B>
230 bool operator()(
const A& a,
const B& b)
const {
return a >= b; }
234 class DummyMatchResultListener :
public MatchResultListener {
236 DummyMatchResultListener() : MatchResultListener(
nullptr) {}
245 class StreamMatchResultListener :
public MatchResultListener {
247 explicit StreamMatchResultListener(::std::ostream* os)
248 : MatchResultListener(os) {}
257 template <
typename T>
262 bool MatchAndExplain(
const T&
x, MatchResultListener* listener)
const {
263 return impl_->MatchAndExplain(x, listener);
267 bool Matches(
const T& x)
const {
268 DummyMatchResultListener dummy;
269 return MatchAndExplain(x, &dummy);
273 void DescribeTo(::std::ostream* os)
const { impl_->DescribeTo(os); }
276 void DescribeNegationTo(::std::ostream* os)
const {
277 impl_->DescribeNegationTo(os);
281 void ExplainMatchResultTo(
const T& x, ::std::ostream* os)
const {
282 StreamMatchResultListener listener(os);
283 MatchAndExplain(x, &listener);
289 const MatcherDescriberInterface* GetDescriber()
const {
297 explicit MatcherBase(
const MatcherInterface<const T&>* impl) : impl_(impl) {}
299 template <
typename U>
300 explicit MatcherBase(
301 const MatcherInterface<U>* impl,
302 typename internal::EnableIf<
304 : impl_(
new internal::MatcherInterfaceAdapter<U>(impl)) {}
306 MatcherBase(
const MatcherBase&) =
default;
307 MatcherBase& operator=(
const MatcherBase&) =
default;
308 MatcherBase(MatcherBase&&) =
default;
309 MatcherBase& operator=(MatcherBase&&) =
default;
311 virtual ~MatcherBase() {}
314 std::shared_ptr<const MatcherInterface<const T&>> impl_;
323 template <
typename T>
324 class Matcher :
public internal::MatcherBase<T> {
329 explicit Matcher() {}
332 explicit Matcher(
const MatcherInterface<const T&>* impl)
333 : internal::MatcherBase<T>(impl) {}
335 template <
typename U>
336 explicit Matcher(
const MatcherInterface<U>* impl,
337 typename internal::EnableIf<
339 : internal::MatcherBase<T>(impl) {}
351 :
public internal::MatcherBase<const std::string&> {
355 explicit Matcher(
const MatcherInterface<const std::string&>* impl)
356 : internal::MatcherBase<const std::string&>(impl) {}
360 Matcher(
const std::string& s);
363 Matcher(
const char* s);
368 :
public internal::MatcherBase<std::string> {
372 explicit Matcher(
const MatcherInterface<const std::string&>* impl)
373 : internal::MatcherBase<std::string>(impl) {}
374 explicit Matcher(
const MatcherInterface<std::string>* impl)
375 : internal::MatcherBase<std::string>(impl) {}
379 Matcher(
const std::string& s);
382 Matcher(
const char* s);
390 class GTEST_API_ Matcher<const absl::string_view&>
391 :
public internal::MatcherBase<const absl::string_view&> {
395 explicit Matcher(
const MatcherInterface<const absl::string_view&>* impl)
396 : internal::MatcherBase<const absl::string_view&>(impl) {}
400 Matcher(
const std::string& s);
403 Matcher(
const char* s);
406 Matcher(absl::string_view s);
411 :
public internal::MatcherBase<absl::string_view> {
415 explicit Matcher(
const MatcherInterface<const absl::string_view&>* impl)
416 : internal::MatcherBase<absl::string_view>(impl) {}
417 explicit Matcher(
const MatcherInterface<absl::string_view>* impl)
418 : internal::MatcherBase<absl::string_view>(impl) {}
422 Matcher(
const std::string& s);
425 Matcher(
const char* s);
428 Matcher(absl::string_view s);
430 #endif // GTEST_HAS_ABSL 433 template <
typename T>
434 std::ostream& operator<<(std::ostream& os, const Matcher<T>& matcher) {
435 matcher.DescribeTo(&os);
451 template <
class Impl>
452 class PolymorphicMatcher {
454 explicit PolymorphicMatcher(
const Impl& an_impl) : impl_(an_impl) {}
458 Impl& mutable_impl() {
return impl_; }
462 const Impl& impl()
const {
return impl_; }
464 template <
typename T>
465 operator Matcher<T>()
const {
466 return Matcher<T>(
new MonomorphicImpl<const T&>(impl_));
470 template <
typename T>
471 class MonomorphicImpl :
public MatcherInterface<T> {
473 explicit MonomorphicImpl(
const Impl& impl) : impl_(impl) {}
475 virtual void DescribeTo(::std::ostream* os)
const { impl_.DescribeTo(os); }
477 virtual void DescribeNegationTo(::std::ostream* os)
const {
478 impl_.DescribeNegationTo(os);
481 virtual bool MatchAndExplain(T
x, MatchResultListener* listener)
const {
482 return impl_.MatchAndExplain(x, listener);
498 template <
typename T>
499 inline Matcher<T> MakeMatcher(
const MatcherInterface<T>* impl) {
500 return Matcher<T>(impl);
510 template <
class Impl>
511 inline PolymorphicMatcher<Impl> MakePolymorphicMatcher(
const Impl& impl) {
512 return PolymorphicMatcher<Impl>(impl);
526 template <
typename D,
typename Rhs,
typename Op>
527 class ComparisonBase {
529 explicit ComparisonBase(
const Rhs& rhs) : rhs_(rhs) {}
530 template <
typename Lhs>
531 operator Matcher<Lhs>()
const {
532 return Matcher<Lhs>(
new Impl<const Lhs&>(rhs_));
536 template <
typename T>
537 static const T& Unwrap(
const T& v) {
return v; }
538 template <
typename T>
539 static const T& Unwrap(std::reference_wrapper<T> v) {
return v; }
541 template <
typename Lhs,
typename = Rhs>
542 class Impl :
public MatcherInterface<Lhs> {
544 explicit Impl(
const Rhs& rhs) : rhs_(rhs) {}
545 bool MatchAndExplain(Lhs lhs,
546 MatchResultListener* )
const override {
547 return Op()(lhs, Unwrap(rhs_));
549 void DescribeTo(::std::ostream* os)
const override {
550 *os << D::Desc() <<
" ";
553 void DescribeNegationTo(::std::ostream* os)
const override {
554 *os << D::NegatedDesc() <<
" ";
564 template <
typename Rhs>
565 class EqMatcher :
public ComparisonBase<EqMatcher<Rhs>, Rhs, AnyEq> {
567 explicit EqMatcher(
const Rhs& rhs)
568 : ComparisonBase<EqMatcher<Rhs>, Rhs, AnyEq>(rhs) { }
569 static const char* Desc() {
return "is equal to"; }
570 static const char* NegatedDesc() {
return "isn't equal to"; }
572 template <
typename Rhs>
573 class NeMatcher :
public ComparisonBase<NeMatcher<Rhs>, Rhs, AnyNe> {
575 explicit NeMatcher(
const Rhs& rhs)
576 : ComparisonBase<NeMatcher<Rhs>, Rhs, AnyNe>(rhs) { }
577 static const char* Desc() {
return "isn't equal to"; }
578 static const char* NegatedDesc() {
return "is equal to"; }
580 template <
typename Rhs>
581 class LtMatcher :
public ComparisonBase<LtMatcher<Rhs>, Rhs, AnyLt> {
583 explicit LtMatcher(
const Rhs& rhs)
584 : ComparisonBase<LtMatcher<Rhs>, Rhs, AnyLt>(rhs) { }
585 static const char* Desc() {
return "is <"; }
586 static const char* NegatedDesc() {
return "isn't <"; }
588 template <
typename Rhs>
589 class GtMatcher :
public ComparisonBase<GtMatcher<Rhs>, Rhs, AnyGt> {
591 explicit GtMatcher(
const Rhs& rhs)
592 : ComparisonBase<GtMatcher<Rhs>, Rhs, AnyGt>(rhs) { }
593 static const char* Desc() {
return "is >"; }
594 static const char* NegatedDesc() {
return "isn't >"; }
596 template <
typename Rhs>
597 class LeMatcher :
public ComparisonBase<LeMatcher<Rhs>, Rhs, AnyLe> {
599 explicit LeMatcher(
const Rhs& rhs)
600 : ComparisonBase<LeMatcher<Rhs>, Rhs, AnyLe>(rhs) { }
601 static const char* Desc() {
return "is <="; }
602 static const char* NegatedDesc() {
return "isn't <="; }
604 template <
typename Rhs>
605 class GeMatcher :
public ComparisonBase<GeMatcher<Rhs>, Rhs, AnyGe> {
607 explicit GeMatcher(
const Rhs& rhs)
608 : ComparisonBase<GeMatcher<Rhs>, Rhs, AnyGe>(rhs) { }
609 static const char* Desc() {
return "is >="; }
610 static const char* NegatedDesc() {
return "isn't >="; }
616 class MatchesRegexMatcher {
618 MatchesRegexMatcher(
const RE* regex,
bool full_match)
619 : regex_(regex), full_match_(full_match) {}
622 bool MatchAndExplain(
const absl::string_view& s,
623 MatchResultListener* listener)
const {
624 return MatchAndExplain(std::string(s), listener);
626 #endif // GTEST_HAS_ABSL 633 template <
typename CharType>
634 bool MatchAndExplain(CharType* s, MatchResultListener* listener)
const {
635 return s !=
nullptr && MatchAndExplain(std::string(s), listener);
642 template <
class MatcheeStringType>
643 bool MatchAndExplain(
const MatcheeStringType& s,
644 MatchResultListener* )
const {
645 const std::string& s2(s);
646 return full_match_ ? RE::FullMatch(s2, *regex_)
647 : RE::PartialMatch(s2, *regex_);
650 void DescribeTo(::std::ostream* os)
const {
651 *os << (full_match_ ?
"matches" :
"contains") <<
" regular expression ";
655 void DescribeNegationTo(::std::ostream* os)
const {
656 *os <<
"doesn't " << (full_match_ ?
"match" :
"contain")
657 <<
" regular expression ";
662 const std::shared_ptr<const RE> regex_;
663 const bool full_match_;
669 inline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex(
670 const internal::RE* regex) {
671 return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex,
true));
673 inline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex(
674 const std::string& regex) {
675 return MatchesRegex(
new internal::RE(regex));
680 inline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex(
681 const internal::RE* regex) {
682 return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex,
false));
684 inline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex(
685 const std::string& regex) {
686 return ContainsRegex(
new internal::RE(regex));
692 template <
typename T>
693 inline internal::EqMatcher<T> Eq(T
x) {
return internal::EqMatcher<T>(
x); }
697 template <
typename T>
698 Matcher<T>::Matcher(T
value) { *
this = Eq(value); }
712 template <
typename Lhs,
typename Rhs>
713 inline Matcher<Lhs> TypedEq(
const Rhs& rhs) {
return Eq(rhs); }
716 template <
typename Rhs>
717 inline internal::GeMatcher<Rhs> Ge(Rhs x) {
718 return internal::GeMatcher<Rhs>(
x);
722 template <
typename Rhs>
723 inline internal::GtMatcher<Rhs> Gt(Rhs x) {
724 return internal::GtMatcher<Rhs>(
x);
728 template <
typename Rhs>
729 inline internal::LeMatcher<Rhs> Le(Rhs x) {
730 return internal::LeMatcher<Rhs>(
x);
734 template <
typename Rhs>
735 inline internal::LtMatcher<Rhs> Lt(Rhs x) {
736 return internal::LtMatcher<Rhs>(
x);
740 template <
typename Rhs>
741 inline internal::NeMatcher<Rhs> Ne(Rhs x) {
742 return internal::NeMatcher<Rhs>(
x);
748 #endif // GTEST_INCLUDE_GTEST_GTEST_MATCHERS_H_ Definition: gmock-actions.h:59
#define GTEST_DISABLE_MSC_WARNINGS_POP_()
Definition: gtest-port.h:314
std::string Print(const T &value)
Definition: googletest-printers-test.cc:233
#define GTEST_MAYBE_5046_
Definition: gtest-matchers.h:54
#define GTEST_API_
Definition: gtest-port.h:759
std::ostream & operator<<(std::ostream &os, const Message &sb)
Definition: gtest-message.h:198
void UniversalPrint(const T &value,::std::ostream *os)
Definition: gtest-printers.h:864
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 GTEST_MAYBE_5046_) namespace testing
Definition: gtest-matchers.h:57
int value
Definition: gmock-matchers_test.cc:657
int x
Definition: gmock-matchers_test.cc:3610
type
Definition: upload.py:443
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
Definition: gtest-port.h:688