Path: blob/master/dep/googletest/include/gtest/gtest-matchers.h
4806 views
// Copyright 2007, Google Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above10// copyright notice, this list of conditions and the following disclaimer11// in the documentation and/or other materials provided with the12// distribution.13// * Neither the name of Google Inc. nor the names of its14// contributors may be used to endorse or promote products derived from15// this software without specific prior written permission.16//17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2829// The Google C++ Testing and Mocking Framework (Google Test)30//31// This file implements just enough of the matcher interface to allow32// EXPECT_DEATH and friends to accept a matcher argument.3334// IWYU pragma: private, include "gtest/gtest.h"35// IWYU pragma: friend gtest/.*36// IWYU pragma: friend gmock/.*3738#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_MATCHERS_H_39#define GOOGLETEST_INCLUDE_GTEST_GTEST_MATCHERS_H_4041#include <atomic>42#include <functional>43#include <memory>44#include <ostream>45#include <string>46#include <type_traits>4748#include "gtest/gtest-printers.h"49#include "gtest/internal/gtest-internal.h"50#include "gtest/internal/gtest-port.h"5152// MSVC warning C5046 is new as of VS2017 version 15.8.53#if defined(_MSC_VER) && _MSC_VER >= 191554#define GTEST_MAYBE_5046_ 504655#else56#define GTEST_MAYBE_5046_57#endif5859GTEST_DISABLE_MSC_WARNINGS_PUSH_(604251 GTEST_MAYBE_5046_ /* class A needs to have dll-interface to be used by61clients of class B */62/* Symbol involving type with internal linkage not defined */)6364namespace testing {6566// To implement a matcher Foo for type T, define:67// 1. a class FooMatcherMatcher that implements the matcher interface:68// using is_gtest_matcher = void;69// bool MatchAndExplain(const T&, std::ostream*) const;70// (MatchResultListener* can also be used instead of std::ostream*)71// void DescribeTo(std::ostream*) const;72// void DescribeNegationTo(std::ostream*) const;73//74// 2. a factory function that creates a Matcher<T> object from a75// FooMatcherMatcher.7677class MatchResultListener {78public:79// Creates a listener object with the given underlying ostream. The80// listener does not own the ostream, and does not dereference it81// in the constructor or destructor.82explicit MatchResultListener(::std::ostream* os) : stream_(os) {}83virtual ~MatchResultListener() = 0; // Makes this class abstract.8485// Streams x to the underlying ostream; does nothing if the ostream86// is NULL.87template <typename T>88MatchResultListener& operator<<(const T& x) {89if (stream_ != nullptr) *stream_ << x;90return *this;91}9293// Returns the underlying ostream.94::std::ostream* stream() { return stream_; }9596// Returns true if and only if the listener is interested in an explanation97// of the match result. A matcher's MatchAndExplain() method can use98// this information to avoid generating the explanation when no one99// intends to hear it.100bool IsInterested() const { return stream_ != nullptr; }101102private:103::std::ostream* const stream_;104105MatchResultListener(const MatchResultListener&) = delete;106MatchResultListener& operator=(const MatchResultListener&) = delete;107};108109inline MatchResultListener::~MatchResultListener() = default;110111// An instance of a subclass of this knows how to describe itself as a112// matcher.113class GTEST_API_ MatcherDescriberInterface {114public:115virtual ~MatcherDescriberInterface() = default;116117// Describes this matcher to an ostream. The function should print118// a verb phrase that describes the property a value matching this119// matcher should have. The subject of the verb phrase is the value120// being matched. For example, the DescribeTo() method of the Gt(7)121// matcher prints "is greater than 7".122virtual void DescribeTo(::std::ostream* os) const = 0;123124// Describes the negation of this matcher to an ostream. For125// example, if the description of this matcher is "is greater than126// 7", the negated description could be "is not greater than 7".127// You are not required to override this when implementing128// MatcherInterface, but it is highly advised so that your matcher129// can produce good error messages.130virtual void DescribeNegationTo(::std::ostream* os) const {131*os << "not (";132DescribeTo(os);133*os << ")";134}135};136137// The implementation of a matcher.138template <typename T>139class MatcherInterface : public MatcherDescriberInterface {140public:141// Returns true if and only if the matcher matches x; also explains the142// match result to 'listener' if necessary (see the next paragraph), in143// the form of a non-restrictive relative clause ("which ...",144// "whose ...", etc) that describes x. For example, the145// MatchAndExplain() method of the Pointee(...) matcher should146// generate an explanation like "which points to ...".147//148// Implementations of MatchAndExplain() should add an explanation of149// the match result *if and only if* they can provide additional150// information that's not already present (or not obvious) in the151// print-out of x and the matcher's description. Whether the match152// succeeds is not a factor in deciding whether an explanation is153// needed, as sometimes the caller needs to print a failure message154// when the match succeeds (e.g. when the matcher is used inside155// Not()).156//157// For example, a "has at least 10 elements" matcher should explain158// what the actual element count is, regardless of the match result,159// as it is useful information to the reader; on the other hand, an160// "is empty" matcher probably only needs to explain what the actual161// size is when the match fails, as it's redundant to say that the162// size is 0 when the value is already known to be empty.163//164// You should override this method when defining a new matcher.165//166// It's the responsibility of the caller (Google Test) to guarantee167// that 'listener' is not NULL. This helps to simplify a matcher's168// implementation when it doesn't care about the performance, as it169// can talk to 'listener' without checking its validity first.170// However, in order to implement dummy listeners efficiently,171// listener->stream() may be NULL.172virtual bool MatchAndExplain(T x, MatchResultListener* listener) const = 0;173174// Inherits these methods from MatcherDescriberInterface:175// virtual void DescribeTo(::std::ostream* os) const = 0;176// virtual void DescribeNegationTo(::std::ostream* os) const;177};178179namespace internal {180181// A match result listener that ignores the explanation.182class DummyMatchResultListener : public MatchResultListener {183public:184DummyMatchResultListener() : MatchResultListener(nullptr) {}185186private:187DummyMatchResultListener(const DummyMatchResultListener&) = delete;188DummyMatchResultListener& operator=(const DummyMatchResultListener&) = delete;189};190191// A match result listener that forwards the explanation to a given192// ostream. The difference between this and MatchResultListener is193// that the former is concrete.194class StreamMatchResultListener : public MatchResultListener {195public:196explicit StreamMatchResultListener(::std::ostream* os)197: MatchResultListener(os) {}198199private:200StreamMatchResultListener(const StreamMatchResultListener&) = delete;201StreamMatchResultListener& operator=(const StreamMatchResultListener&) =202delete;203};204205struct SharedPayloadBase {206std::atomic<int> ref{1};207void Ref() { ref.fetch_add(1, std::memory_order_relaxed); }208bool Unref() { return ref.fetch_sub(1, std::memory_order_acq_rel) == 1; }209};210211template <typename T>212struct SharedPayload : SharedPayloadBase {213explicit SharedPayload(const T& v) : value(v) {}214explicit SharedPayload(T&& v) : value(std::move(v)) {}215216static void Destroy(SharedPayloadBase* shared) {217delete static_cast<SharedPayload*>(shared);218}219220T value;221};222223// An internal class for implementing Matcher<T>, which will derive224// from it. We put functionalities common to all Matcher<T>225// specializations here to avoid code duplication.226template <typename T>227class MatcherBase : private MatcherDescriberInterface {228public:229// Returns true if and only if the matcher matches x; also explains the230// match result to 'listener'.231bool MatchAndExplain(const T& x, MatchResultListener* listener) const {232GTEST_CHECK_(vtable_ != nullptr);233return vtable_->match_and_explain(*this, x, listener);234}235236// Returns true if and only if this matcher matches x.237bool Matches(const T& x) const {238DummyMatchResultListener dummy;239return MatchAndExplain(x, &dummy);240}241242// Describes this matcher to an ostream.243void DescribeTo(::std::ostream* os) const final {244GTEST_CHECK_(vtable_ != nullptr);245vtable_->describe(*this, os, false);246}247248// Describes the negation of this matcher to an ostream.249void DescribeNegationTo(::std::ostream* os) const final {250GTEST_CHECK_(vtable_ != nullptr);251vtable_->describe(*this, os, true);252}253254// Explains why x matches, or doesn't match, the matcher.255void ExplainMatchResultTo(const T& x, ::std::ostream* os) const {256StreamMatchResultListener listener(os);257MatchAndExplain(x, &listener);258}259260// Returns the describer for this matcher object; retains ownership261// of the describer, which is only guaranteed to be alive when262// this matcher object is alive.263const MatcherDescriberInterface* GetDescriber() const {264if (vtable_ == nullptr) return nullptr;265return vtable_->get_describer(*this);266}267268protected:269MatcherBase() : vtable_(nullptr), buffer_() {}270271// Constructs a matcher from its implementation.272template <typename U>273explicit MatcherBase(const MatcherInterface<U>* impl)274: vtable_(nullptr), buffer_() {275Init(impl);276}277278template <typename M, typename = typename std::remove_reference<279M>::type::is_gtest_matcher>280MatcherBase(M&& m) : vtable_(nullptr), buffer_() { // NOLINT281Init(std::forward<M>(m));282}283284MatcherBase(const MatcherBase& other)285: vtable_(other.vtable_), buffer_(other.buffer_) {286if (IsShared()) buffer_.shared->Ref();287}288289MatcherBase& operator=(const MatcherBase& other) {290if (this == &other) return *this;291Destroy();292vtable_ = other.vtable_;293buffer_ = other.buffer_;294if (IsShared()) buffer_.shared->Ref();295return *this;296}297298MatcherBase(MatcherBase&& other)299: vtable_(other.vtable_), buffer_(other.buffer_) {300other.vtable_ = nullptr;301}302303MatcherBase& operator=(MatcherBase&& other) {304if (this == &other) return *this;305Destroy();306vtable_ = other.vtable_;307buffer_ = other.buffer_;308other.vtable_ = nullptr;309return *this;310}311312~MatcherBase() override { Destroy(); }313314private:315struct VTable {316bool (*match_and_explain)(const MatcherBase&, const T&,317MatchResultListener*);318void (*describe)(const MatcherBase&, std::ostream*, bool negation);319// Returns the captured object if it implements the interface, otherwise320// returns the MatcherBase itself.321const MatcherDescriberInterface* (*get_describer)(const MatcherBase&);322// Called on shared instances when the reference count reaches 0.323void (*shared_destroy)(SharedPayloadBase*);324};325326bool IsShared() const {327return vtable_ != nullptr && vtable_->shared_destroy != nullptr;328}329330// If the implementation uses a listener, call that.331template <typename P>332static auto MatchAndExplainImpl(const MatcherBase& m, const T& value,333MatchResultListener* listener)334-> decltype(P::Get(m).MatchAndExplain(value, listener->stream())) {335return P::Get(m).MatchAndExplain(value, listener->stream());336}337338template <typename P>339static auto MatchAndExplainImpl(const MatcherBase& m, const T& value,340MatchResultListener* listener)341-> decltype(P::Get(m).MatchAndExplain(value, listener)) {342return P::Get(m).MatchAndExplain(value, listener);343}344345template <typename P>346static void DescribeImpl(const MatcherBase& m, std::ostream* os,347bool negation) {348if (negation) {349P::Get(m).DescribeNegationTo(os);350} else {351P::Get(m).DescribeTo(os);352}353}354355template <typename P>356static const MatcherDescriberInterface* GetDescriberImpl(357const MatcherBase& m) {358// If the impl is a MatcherDescriberInterface, then return it.359// Otherwise use MatcherBase itself.360// This allows us to implement the GetDescriber() function without support361// from the impl, but some users really want to get their impl back when362// they call GetDescriber().363// We use std::get on a tuple as a workaround of not having `if constexpr`.364return std::get<(365std::is_convertible<decltype(&P::Get(m)),366const MatcherDescriberInterface*>::value367? 1368: 0)>(std::make_tuple(&m, &P::Get(m)));369}370371template <typename P>372const VTable* GetVTable() {373static constexpr VTable kVTable = {&MatchAndExplainImpl<P>,374&DescribeImpl<P>, &GetDescriberImpl<P>,375P::shared_destroy};376return &kVTable;377}378379union Buffer {380// Add some types to give Buffer some common alignment/size use cases.381void* ptr;382double d;383int64_t i;384// And add one for the out-of-line cases.385SharedPayloadBase* shared;386};387388void Destroy() {389if (IsShared() && buffer_.shared->Unref()) {390vtable_->shared_destroy(buffer_.shared);391}392}393394template <typename M>395static constexpr bool IsInlined() {396return sizeof(M) <= sizeof(Buffer) && alignof(M) <= alignof(Buffer) &&397std::is_trivially_copy_constructible<M>::value &&398std::is_trivially_destructible<M>::value;399}400401template <typename M, bool = MatcherBase::IsInlined<M>()>402struct ValuePolicy {403static const M& Get(const MatcherBase& m) {404// When inlined along with Init, need to be explicit to avoid violating405// strict aliasing rules.406const M* ptr =407static_cast<const M*>(static_cast<const void*>(&m.buffer_));408return *ptr;409}410static void Init(MatcherBase& m, M impl) {411::new (static_cast<void*>(&m.buffer_)) M(impl);412}413static constexpr auto shared_destroy = nullptr;414};415416template <typename M>417struct ValuePolicy<M, false> {418using Shared = SharedPayload<M>;419static const M& Get(const MatcherBase& m) {420return static_cast<Shared*>(m.buffer_.shared)->value;421}422template <typename Arg>423static void Init(MatcherBase& m, Arg&& arg) {424m.buffer_.shared = new Shared(std::forward<Arg>(arg));425}426static constexpr auto shared_destroy = &Shared::Destroy;427};428429template <typename U, bool B>430struct ValuePolicy<const MatcherInterface<U>*, B> {431using M = const MatcherInterface<U>;432using Shared = SharedPayload<std::unique_ptr<M>>;433static const M& Get(const MatcherBase& m) {434return *static_cast<Shared*>(m.buffer_.shared)->value;435}436static void Init(MatcherBase& m, M* impl) {437m.buffer_.shared = new Shared(std::unique_ptr<M>(impl));438}439440static constexpr auto shared_destroy = &Shared::Destroy;441};442443template <typename M>444void Init(M&& m) {445using MM = typename std::decay<M>::type;446using Policy = ValuePolicy<MM>;447vtable_ = GetVTable<Policy>();448Policy::Init(*this, std::forward<M>(m));449}450451const VTable* vtable_;452Buffer buffer_;453};454455} // namespace internal456457// A Matcher<T> is a copyable and IMMUTABLE (except by assignment)458// object that can check whether a value of type T matches. The459// implementation of Matcher<T> is just a std::shared_ptr to const460// MatcherInterface<T>. Don't inherit from Matcher!461template <typename T>462class Matcher : public internal::MatcherBase<T> {463public:464// Constructs a null matcher. Needed for storing Matcher objects in STL465// containers. A default-constructed matcher is not yet initialized. You466// cannot use it until a valid value has been assigned to it.467explicit Matcher() {} // NOLINT468469// Constructs a matcher from its implementation.470explicit Matcher(const MatcherInterface<const T&>* impl)471: internal::MatcherBase<T>(impl) {}472473template <typename U>474explicit Matcher(475const MatcherInterface<U>* impl,476typename std::enable_if<!std::is_same<U, const U&>::value>::type* =477nullptr)478: internal::MatcherBase<T>(impl) {}479480template <typename M, typename = typename std::remove_reference<481M>::type::is_gtest_matcher>482Matcher(M&& m) : internal::MatcherBase<T>(std::forward<M>(m)) {} // NOLINT483484// Implicit constructor here allows people to write485// EXPECT_CALL(foo, Bar(5)) instead of EXPECT_CALL(foo, Bar(Eq(5))) sometimes486Matcher(T value); // NOLINT487};488489// The following two specializations allow the user to write str490// instead of Eq(str) and "foo" instead of Eq("foo") when a std::string491// matcher is expected.492template <>493class GTEST_API_ Matcher<const std::string&>494: public internal::MatcherBase<const std::string&> {495public:496Matcher() = default;497498explicit Matcher(const MatcherInterface<const std::string&>* impl)499: internal::MatcherBase<const std::string&>(impl) {}500501template <typename M, typename = typename std::remove_reference<502M>::type::is_gtest_matcher>503Matcher(M&& m) // NOLINT504: internal::MatcherBase<const std::string&>(std::forward<M>(m)) {}505506// Allows the user to write str instead of Eq(str) sometimes, where507// str is a std::string object.508Matcher(const std::string& s); // NOLINT509510// Allows the user to write "foo" instead of Eq("foo") sometimes.511Matcher(const char* s); // NOLINT512};513514template <>515class GTEST_API_ Matcher<std::string>516: public internal::MatcherBase<std::string> {517public:518Matcher() = default;519520explicit Matcher(const MatcherInterface<const std::string&>* impl)521: internal::MatcherBase<std::string>(impl) {}522explicit Matcher(const MatcherInterface<std::string>* impl)523: internal::MatcherBase<std::string>(impl) {}524525template <typename M, typename = typename std::remove_reference<526M>::type::is_gtest_matcher>527Matcher(M&& m) // NOLINT528: internal::MatcherBase<std::string>(std::forward<M>(m)) {}529530// Allows the user to write str instead of Eq(str) sometimes, where531// str is a string object.532Matcher(const std::string& s); // NOLINT533534// Allows the user to write "foo" instead of Eq("foo") sometimes.535Matcher(const char* s); // NOLINT536};537538#if GTEST_INTERNAL_HAS_STRING_VIEW539// The following two specializations allow the user to write str540// instead of Eq(str) and "foo" instead of Eq("foo") when a absl::string_view541// matcher is expected.542template <>543class GTEST_API_ Matcher<const internal::StringView&>544: public internal::MatcherBase<const internal::StringView&> {545public:546Matcher() = default;547548explicit Matcher(const MatcherInterface<const internal::StringView&>* impl)549: internal::MatcherBase<const internal::StringView&>(impl) {}550551template <typename M, typename = typename std::remove_reference<552M>::type::is_gtest_matcher>553Matcher(M&& m) // NOLINT554: internal::MatcherBase<const internal::StringView&>(std::forward<M>(m)) {555}556557// Allows the user to write str instead of Eq(str) sometimes, where558// str is a std::string object.559Matcher(const std::string& s); // NOLINT560561// Allows the user to write "foo" instead of Eq("foo") sometimes.562Matcher(const char* s); // NOLINT563564// Allows the user to pass absl::string_views or std::string_views directly.565Matcher(internal::StringView s); // NOLINT566};567568template <>569class GTEST_API_ Matcher<internal::StringView>570: public internal::MatcherBase<internal::StringView> {571public:572Matcher() = default;573574explicit Matcher(const MatcherInterface<const internal::StringView&>* impl)575: internal::MatcherBase<internal::StringView>(impl) {}576explicit Matcher(const MatcherInterface<internal::StringView>* impl)577: internal::MatcherBase<internal::StringView>(impl) {}578579template <typename M, typename = typename std::remove_reference<580M>::type::is_gtest_matcher>581Matcher(M&& m) // NOLINT582: internal::MatcherBase<internal::StringView>(std::forward<M>(m)) {}583584// Allows the user to write str instead of Eq(str) sometimes, where585// str is a std::string object.586Matcher(const std::string& s); // NOLINT587588// Allows the user to write "foo" instead of Eq("foo") sometimes.589Matcher(const char* s); // NOLINT590591// Allows the user to pass absl::string_views or std::string_views directly.592Matcher(internal::StringView s); // NOLINT593};594#endif // GTEST_INTERNAL_HAS_STRING_VIEW595596// Prints a matcher in a human-readable format.597template <typename T>598std::ostream& operator<<(std::ostream& os, const Matcher<T>& matcher) {599matcher.DescribeTo(&os);600return os;601}602603// The PolymorphicMatcher class template makes it easy to implement a604// polymorphic matcher (i.e. a matcher that can match values of more605// than one type, e.g. Eq(n) and NotNull()).606//607// To define a polymorphic matcher, a user should provide an Impl608// class that has a DescribeTo() method and a DescribeNegationTo()609// method, and define a member function (or member function template)610//611// bool MatchAndExplain(const Value& value,612// MatchResultListener* listener) const;613//614// See the definition of NotNull() for a complete example.615template <class Impl>616class PolymorphicMatcher {617public:618explicit PolymorphicMatcher(const Impl& an_impl) : impl_(an_impl) {}619620// Returns a mutable reference to the underlying matcher621// implementation object.622Impl& mutable_impl() { return impl_; }623624// Returns an immutable reference to the underlying matcher625// implementation object.626const Impl& impl() const { return impl_; }627628template <typename T>629operator Matcher<T>() const {630return Matcher<T>(new MonomorphicImpl<const T&>(impl_));631}632633private:634template <typename T>635class MonomorphicImpl : public MatcherInterface<T> {636public:637explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {}638639void DescribeTo(::std::ostream* os) const override { impl_.DescribeTo(os); }640641void DescribeNegationTo(::std::ostream* os) const override {642impl_.DescribeNegationTo(os);643}644645bool MatchAndExplain(T x, MatchResultListener* listener) const override {646return impl_.MatchAndExplain(x, listener);647}648649private:650const Impl impl_;651};652653Impl impl_;654};655656// Creates a matcher from its implementation.657// DEPRECATED: Especially in the generic code, prefer:658// Matcher<T>(new MyMatcherImpl<const T&>(...));659//660// MakeMatcher may create a Matcher that accepts its argument by value, which661// leads to unnecessary copies & lack of support for non-copyable types.662template <typename T>663inline Matcher<T> MakeMatcher(const MatcherInterface<T>* impl) {664return Matcher<T>(impl);665}666667// Creates a polymorphic matcher from its implementation. This is668// easier to use than the PolymorphicMatcher<Impl> constructor as it669// doesn't require you to explicitly write the template argument, e.g.670//671// MakePolymorphicMatcher(foo);672// vs673// PolymorphicMatcher<TypeOfFoo>(foo);674template <class Impl>675inline PolymorphicMatcher<Impl> MakePolymorphicMatcher(const Impl& impl) {676return PolymorphicMatcher<Impl>(impl);677}678679namespace internal {680// Implements a matcher that compares a given value with a681// pre-supplied value using one of the ==, <=, <, etc, operators. The682// two values being compared don't have to have the same type.683//684// The matcher defined here is polymorphic (for example, Eq(5) can be685// used to match an int, a short, a double, etc). Therefore we use686// a template type conversion operator in the implementation.687//688// The following template definition assumes that the Rhs parameter is689// a "bare" type (i.e. neither 'const T' nor 'T&').690template <typename D, typename Rhs, typename Op>691class ComparisonBase {692public:693explicit ComparisonBase(const Rhs& rhs) : rhs_(rhs) {}694695using is_gtest_matcher = void;696697template <typename Lhs>698bool MatchAndExplain(const Lhs& lhs, std::ostream*) const {699return Op()(lhs, Unwrap(rhs_));700}701void DescribeTo(std::ostream* os) const {702*os << D::Desc() << " ";703UniversalPrint(Unwrap(rhs_), os);704}705void DescribeNegationTo(std::ostream* os) const {706*os << D::NegatedDesc() << " ";707UniversalPrint(Unwrap(rhs_), os);708}709710private:711template <typename T>712static const T& Unwrap(const T& v) {713return v;714}715template <typename T>716static const T& Unwrap(std::reference_wrapper<T> v) {717return v;718}719720Rhs rhs_;721};722723template <typename Rhs>724class EqMatcher : public ComparisonBase<EqMatcher<Rhs>, Rhs, std::equal_to<>> {725public:726explicit EqMatcher(const Rhs& rhs)727: ComparisonBase<EqMatcher<Rhs>, Rhs, std::equal_to<>>(rhs) {}728static const char* Desc() { return "is equal to"; }729static const char* NegatedDesc() { return "isn't equal to"; }730};731template <typename Rhs>732class NeMatcher733: public ComparisonBase<NeMatcher<Rhs>, Rhs, std::not_equal_to<>> {734public:735explicit NeMatcher(const Rhs& rhs)736: ComparisonBase<NeMatcher<Rhs>, Rhs, std::not_equal_to<>>(rhs) {}737static const char* Desc() { return "isn't equal to"; }738static const char* NegatedDesc() { return "is equal to"; }739};740template <typename Rhs>741class LtMatcher : public ComparisonBase<LtMatcher<Rhs>, Rhs, std::less<>> {742public:743explicit LtMatcher(const Rhs& rhs)744: ComparisonBase<LtMatcher<Rhs>, Rhs, std::less<>>(rhs) {}745static const char* Desc() { return "is <"; }746static const char* NegatedDesc() { return "isn't <"; }747};748template <typename Rhs>749class GtMatcher : public ComparisonBase<GtMatcher<Rhs>, Rhs, std::greater<>> {750public:751explicit GtMatcher(const Rhs& rhs)752: ComparisonBase<GtMatcher<Rhs>, Rhs, std::greater<>>(rhs) {}753static const char* Desc() { return "is >"; }754static const char* NegatedDesc() { return "isn't >"; }755};756template <typename Rhs>757class LeMatcher758: public ComparisonBase<LeMatcher<Rhs>, Rhs, std::less_equal<>> {759public:760explicit LeMatcher(const Rhs& rhs)761: ComparisonBase<LeMatcher<Rhs>, Rhs, std::less_equal<>>(rhs) {}762static const char* Desc() { return "is <="; }763static const char* NegatedDesc() { return "isn't <="; }764};765template <typename Rhs>766class GeMatcher767: public ComparisonBase<GeMatcher<Rhs>, Rhs, std::greater_equal<>> {768public:769explicit GeMatcher(const Rhs& rhs)770: ComparisonBase<GeMatcher<Rhs>, Rhs, std::greater_equal<>>(rhs) {}771static const char* Desc() { return "is >="; }772static const char* NegatedDesc() { return "isn't >="; }773};774775template <typename T, typename = typename std::enable_if<776std::is_constructible<std::string, T>::value>::type>777using StringLike = T;778779// Implements polymorphic matchers MatchesRegex(regex) and780// ContainsRegex(regex), which can be used as a Matcher<T> as long as781// T can be converted to a string.782class MatchesRegexMatcher {783public:784MatchesRegexMatcher(const RE* regex, bool full_match)785: regex_(regex), full_match_(full_match) {}786787#if GTEST_INTERNAL_HAS_STRING_VIEW788bool MatchAndExplain(const internal::StringView& s,789MatchResultListener* listener) const {790return MatchAndExplain(std::string(s), listener);791}792#endif // GTEST_INTERNAL_HAS_STRING_VIEW793794// Accepts pointer types, particularly:795// const char*796// char*797// const wchar_t*798// wchar_t*799template <typename CharType>800bool MatchAndExplain(CharType* s, MatchResultListener* listener) const {801return s != nullptr && MatchAndExplain(std::string(s), listener);802}803804// Matches anything that can convert to std::string.805//806// This is a template, not just a plain function with const std::string&,807// because absl::string_view has some interfering non-explicit constructors.808template <class MatcheeStringType>809bool MatchAndExplain(const MatcheeStringType& s,810MatchResultListener* /* listener */) const {811const std::string s2(s);812return full_match_ ? RE::FullMatch(s2, *regex_)813: RE::PartialMatch(s2, *regex_);814}815816void DescribeTo(::std::ostream* os) const {817*os << (full_match_ ? "matches" : "contains") << " regular expression ";818UniversalPrinter<std::string>::Print(regex_->pattern(), os);819}820821void DescribeNegationTo(::std::ostream* os) const {822*os << "doesn't " << (full_match_ ? "match" : "contain")823<< " regular expression ";824UniversalPrinter<std::string>::Print(regex_->pattern(), os);825}826827private:828const std::shared_ptr<const RE> regex_;829const bool full_match_;830};831} // namespace internal832833// Matches a string that fully matches regular expression 'regex'.834// The matcher takes ownership of 'regex'.835inline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex(836const internal::RE* regex) {837return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, true));838}839template <typename T = std::string>840PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex(841const internal::StringLike<T>& regex) {842return MatchesRegex(new internal::RE(std::string(regex)));843}844845// Matches a string that contains regular expression 'regex'.846// The matcher takes ownership of 'regex'.847inline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex(848const internal::RE* regex) {849return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, false));850}851template <typename T = std::string>852PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex(853const internal::StringLike<T>& regex) {854return ContainsRegex(new internal::RE(std::string(regex)));855}856857// Creates a polymorphic matcher that matches anything equal to x.858// Note: if the parameter of Eq() were declared as const T&, Eq("foo")859// wouldn't compile.860template <typename T>861inline internal::EqMatcher<T> Eq(T x) {862return internal::EqMatcher<T>(x);863}864865// Constructs a Matcher<T> from a 'value' of type T. The constructed866// matcher matches any value that's equal to 'value'.867template <typename T>868Matcher<T>::Matcher(T value) {869*this = Eq(value);870}871872// Creates a monomorphic matcher that matches anything with type Lhs873// and equal to rhs. A user may need to use this instead of Eq(...)874// in order to resolve an overloading ambiguity.875//876// TypedEq<T>(x) is just a convenient short-hand for Matcher<T>(Eq(x))877// or Matcher<T>(x), but more readable than the latter.878//879// We could define similar monomorphic matchers for other comparison880// operations (e.g. TypedLt, TypedGe, and etc), but decided not to do881// it yet as those are used much less than Eq() in practice. A user882// can always write Matcher<T>(Lt(5)) to be explicit about the type,883// for example.884template <typename Lhs, typename Rhs>885inline Matcher<Lhs> TypedEq(const Rhs& rhs) {886return Eq(rhs);887}888889// Creates a polymorphic matcher that matches anything >= x.890template <typename Rhs>891inline internal::GeMatcher<Rhs> Ge(Rhs x) {892return internal::GeMatcher<Rhs>(x);893}894895// Creates a polymorphic matcher that matches anything > x.896template <typename Rhs>897inline internal::GtMatcher<Rhs> Gt(Rhs x) {898return internal::GtMatcher<Rhs>(x);899}900901// Creates a polymorphic matcher that matches anything <= x.902template <typename Rhs>903inline internal::LeMatcher<Rhs> Le(Rhs x) {904return internal::LeMatcher<Rhs>(x);905}906907// Creates a polymorphic matcher that matches anything < x.908template <typename Rhs>909inline internal::LtMatcher<Rhs> Lt(Rhs x) {910return internal::LtMatcher<Rhs>(x);911}912913// Creates a polymorphic matcher that matches anything != x.914template <typename Rhs>915inline internal::NeMatcher<Rhs> Ne(Rhs x) {916return internal::NeMatcher<Rhs>(x);917}918} // namespace testing919920GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 5046921922#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_MATCHERS_H_923924925