1*da58b97aSjoerg // Copyright 2007, Google Inc.
2*da58b97aSjoerg // All rights reserved.
3*da58b97aSjoerg //
4*da58b97aSjoerg // Redistribution and use in source and binary forms, with or without
5*da58b97aSjoerg // modification, are permitted provided that the following conditions are
6*da58b97aSjoerg // met:
7*da58b97aSjoerg //
8*da58b97aSjoerg //     * Redistributions of source code must retain the above copyright
9*da58b97aSjoerg // notice, this list of conditions and the following disclaimer.
10*da58b97aSjoerg //     * Redistributions in binary form must reproduce the above
11*da58b97aSjoerg // copyright notice, this list of conditions and the following disclaimer
12*da58b97aSjoerg // in the documentation and/or other materials provided with the
13*da58b97aSjoerg // distribution.
14*da58b97aSjoerg //     * Neither the name of Google Inc. nor the names of its
15*da58b97aSjoerg // contributors may be used to endorse or promote products derived from
16*da58b97aSjoerg // this software without specific prior written permission.
17*da58b97aSjoerg //
18*da58b97aSjoerg // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*da58b97aSjoerg // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*da58b97aSjoerg // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21*da58b97aSjoerg // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22*da58b97aSjoerg // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23*da58b97aSjoerg // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24*da58b97aSjoerg // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25*da58b97aSjoerg // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26*da58b97aSjoerg // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*da58b97aSjoerg // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28*da58b97aSjoerg // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*da58b97aSjoerg 
30*da58b97aSjoerg // The Google C++ Testing and Mocking Framework (Google Test)
31*da58b97aSjoerg //
32*da58b97aSjoerg // This file implements just enough of the matcher interface to allow
33*da58b97aSjoerg // EXPECT_DEATH and friends to accept a matcher argument.
34*da58b97aSjoerg 
35*da58b97aSjoerg // IWYU pragma: private, include "testing/base/public/gunit.h"
36*da58b97aSjoerg // IWYU pragma: friend third_party/googletest/googlemock/.*
37*da58b97aSjoerg // IWYU pragma: friend third_party/googletest/googletest/.*
38*da58b97aSjoerg 
39*da58b97aSjoerg #ifndef GTEST_INCLUDE_GTEST_GTEST_MATCHERS_H_
40*da58b97aSjoerg #define GTEST_INCLUDE_GTEST_GTEST_MATCHERS_H_
41*da58b97aSjoerg 
42*da58b97aSjoerg #include <memory>
43*da58b97aSjoerg #include <ostream>
44*da58b97aSjoerg #include <string>
45*da58b97aSjoerg #include <type_traits>
46*da58b97aSjoerg 
47*da58b97aSjoerg #include "gtest/gtest-printers.h"
48*da58b97aSjoerg #include "gtest/internal/gtest-internal.h"
49*da58b97aSjoerg #include "gtest/internal/gtest-port.h"
50*da58b97aSjoerg 
51*da58b97aSjoerg // MSVC warning C5046 is new as of VS2017 version 15.8.
52*da58b97aSjoerg #if defined(_MSC_VER) && _MSC_VER >= 1915
53*da58b97aSjoerg #define GTEST_MAYBE_5046_ 5046
54*da58b97aSjoerg #else
55*da58b97aSjoerg #define GTEST_MAYBE_5046_
56*da58b97aSjoerg #endif
57*da58b97aSjoerg 
58*da58b97aSjoerg GTEST_DISABLE_MSC_WARNINGS_PUSH_(
59*da58b97aSjoerg     4251 GTEST_MAYBE_5046_ /* class A needs to have dll-interface to be used by
60*da58b97aSjoerg                               clients of class B */
61*da58b97aSjoerg     /* Symbol involving type with internal linkage not defined */)
62*da58b97aSjoerg 
63*da58b97aSjoerg namespace testing {
64*da58b97aSjoerg 
65*da58b97aSjoerg // To implement a matcher Foo for type T, define:
66*da58b97aSjoerg //   1. a class FooMatcherImpl that implements the
67*da58b97aSjoerg //      MatcherInterface<T> interface, and
68*da58b97aSjoerg //   2. a factory function that creates a Matcher<T> object from a
69*da58b97aSjoerg //      FooMatcherImpl*.
70*da58b97aSjoerg //
71*da58b97aSjoerg // The two-level delegation design makes it possible to allow a user
72*da58b97aSjoerg // to write "v" instead of "Eq(v)" where a Matcher is expected, which
73*da58b97aSjoerg // is impossible if we pass matchers by pointers.  It also eases
74*da58b97aSjoerg // ownership management as Matcher objects can now be copied like
75*da58b97aSjoerg // plain values.
76*da58b97aSjoerg 
77*da58b97aSjoerg // MatchResultListener is an abstract class.  Its << operator can be
78*da58b97aSjoerg // used by a matcher to explain why a value matches or doesn't match.
79*da58b97aSjoerg //
80*da58b97aSjoerg class MatchResultListener {
81*da58b97aSjoerg  public:
82*da58b97aSjoerg   // Creates a listener object with the given underlying ostream.  The
83*da58b97aSjoerg   // listener does not own the ostream, and does not dereference it
84*da58b97aSjoerg   // in the constructor or destructor.
MatchResultListener(::std::ostream * os)85*da58b97aSjoerg   explicit MatchResultListener(::std::ostream* os) : stream_(os) {}
86*da58b97aSjoerg   virtual ~MatchResultListener() = 0;  // Makes this class abstract.
87*da58b97aSjoerg 
88*da58b97aSjoerg   // Streams x to the underlying ostream; does nothing if the ostream
89*da58b97aSjoerg   // is NULL.
90*da58b97aSjoerg   template <typename T>
91*da58b97aSjoerg   MatchResultListener& operator<<(const T& x) {
92*da58b97aSjoerg     if (stream_ != nullptr) *stream_ << x;
93*da58b97aSjoerg     return *this;
94*da58b97aSjoerg   }
95*da58b97aSjoerg 
96*da58b97aSjoerg   // Returns the underlying ostream.
stream()97*da58b97aSjoerg   ::std::ostream* stream() { return stream_; }
98*da58b97aSjoerg 
99*da58b97aSjoerg   // Returns true if and only if the listener is interested in an explanation
100*da58b97aSjoerg   // of the match result.  A matcher's MatchAndExplain() method can use
101*da58b97aSjoerg   // this information to avoid generating the explanation when no one
102*da58b97aSjoerg   // intends to hear it.
IsInterested()103*da58b97aSjoerg   bool IsInterested() const { return stream_ != nullptr; }
104*da58b97aSjoerg 
105*da58b97aSjoerg  private:
106*da58b97aSjoerg   ::std::ostream* const stream_;
107*da58b97aSjoerg 
108*da58b97aSjoerg   GTEST_DISALLOW_COPY_AND_ASSIGN_(MatchResultListener);
109*da58b97aSjoerg };
110*da58b97aSjoerg 
~MatchResultListener()111*da58b97aSjoerg inline MatchResultListener::~MatchResultListener() {
112*da58b97aSjoerg }
113*da58b97aSjoerg 
114*da58b97aSjoerg // An instance of a subclass of this knows how to describe itself as a
115*da58b97aSjoerg // matcher.
116*da58b97aSjoerg class MatcherDescriberInterface {
117*da58b97aSjoerg  public:
~MatcherDescriberInterface()118*da58b97aSjoerg   virtual ~MatcherDescriberInterface() {}
119*da58b97aSjoerg 
120*da58b97aSjoerg   // Describes this matcher to an ostream.  The function should print
121*da58b97aSjoerg   // a verb phrase that describes the property a value matching this
122*da58b97aSjoerg   // matcher should have.  The subject of the verb phrase is the value
123*da58b97aSjoerg   // being matched.  For example, the DescribeTo() method of the Gt(7)
124*da58b97aSjoerg   // matcher prints "is greater than 7".
125*da58b97aSjoerg   virtual void DescribeTo(::std::ostream* os) const = 0;
126*da58b97aSjoerg 
127*da58b97aSjoerg   // Describes the negation of this matcher to an ostream.  For
128*da58b97aSjoerg   // example, if the description of this matcher is "is greater than
129*da58b97aSjoerg   // 7", the negated description could be "is not greater than 7".
130*da58b97aSjoerg   // You are not required to override this when implementing
131*da58b97aSjoerg   // MatcherInterface, but it is highly advised so that your matcher
132*da58b97aSjoerg   // can produce good error messages.
DescribeNegationTo(::std::ostream * os)133*da58b97aSjoerg   virtual void DescribeNegationTo(::std::ostream* os) const {
134*da58b97aSjoerg     *os << "not (";
135*da58b97aSjoerg     DescribeTo(os);
136*da58b97aSjoerg     *os << ")";
137*da58b97aSjoerg   }
138*da58b97aSjoerg };
139*da58b97aSjoerg 
140*da58b97aSjoerg // The implementation of a matcher.
141*da58b97aSjoerg template <typename T>
142*da58b97aSjoerg class MatcherInterface : public MatcherDescriberInterface {
143*da58b97aSjoerg  public:
144*da58b97aSjoerg   // Returns true if and only if the matcher matches x; also explains the
145*da58b97aSjoerg   // match result to 'listener' if necessary (see the next paragraph), in
146*da58b97aSjoerg   // the form of a non-restrictive relative clause ("which ...",
147*da58b97aSjoerg   // "whose ...", etc) that describes x.  For example, the
148*da58b97aSjoerg   // MatchAndExplain() method of the Pointee(...) matcher should
149*da58b97aSjoerg   // generate an explanation like "which points to ...".
150*da58b97aSjoerg   //
151*da58b97aSjoerg   // Implementations of MatchAndExplain() should add an explanation of
152*da58b97aSjoerg   // the match result *if and only if* they can provide additional
153*da58b97aSjoerg   // information that's not already present (or not obvious) in the
154*da58b97aSjoerg   // print-out of x and the matcher's description.  Whether the match
155*da58b97aSjoerg   // succeeds is not a factor in deciding whether an explanation is
156*da58b97aSjoerg   // needed, as sometimes the caller needs to print a failure message
157*da58b97aSjoerg   // when the match succeeds (e.g. when the matcher is used inside
158*da58b97aSjoerg   // Not()).
159*da58b97aSjoerg   //
160*da58b97aSjoerg   // For example, a "has at least 10 elements" matcher should explain
161*da58b97aSjoerg   // what the actual element count is, regardless of the match result,
162*da58b97aSjoerg   // as it is useful information to the reader; on the other hand, an
163*da58b97aSjoerg   // "is empty" matcher probably only needs to explain what the actual
164*da58b97aSjoerg   // size is when the match fails, as it's redundant to say that the
165*da58b97aSjoerg   // size is 0 when the value is already known to be empty.
166*da58b97aSjoerg   //
167*da58b97aSjoerg   // You should override this method when defining a new matcher.
168*da58b97aSjoerg   //
169*da58b97aSjoerg   // It's the responsibility of the caller (Google Test) to guarantee
170*da58b97aSjoerg   // that 'listener' is not NULL.  This helps to simplify a matcher's
171*da58b97aSjoerg   // implementation when it doesn't care about the performance, as it
172*da58b97aSjoerg   // can talk to 'listener' without checking its validity first.
173*da58b97aSjoerg   // However, in order to implement dummy listeners efficiently,
174*da58b97aSjoerg   // listener->stream() may be NULL.
175*da58b97aSjoerg   virtual bool MatchAndExplain(T x, MatchResultListener* listener) const = 0;
176*da58b97aSjoerg 
177*da58b97aSjoerg   // Inherits these methods from MatcherDescriberInterface:
178*da58b97aSjoerg   //   virtual void DescribeTo(::std::ostream* os) const = 0;
179*da58b97aSjoerg   //   virtual void DescribeNegationTo(::std::ostream* os) const;
180*da58b97aSjoerg };
181*da58b97aSjoerg 
182*da58b97aSjoerg namespace internal {
183*da58b97aSjoerg 
184*da58b97aSjoerg // Converts a MatcherInterface<T> to a MatcherInterface<const T&>.
185*da58b97aSjoerg template <typename T>
186*da58b97aSjoerg class MatcherInterfaceAdapter : public MatcherInterface<const T&> {
187*da58b97aSjoerg  public:
MatcherInterfaceAdapter(const MatcherInterface<T> * impl)188*da58b97aSjoerg   explicit MatcherInterfaceAdapter(const MatcherInterface<T>* impl)
189*da58b97aSjoerg       : impl_(impl) {}
~MatcherInterfaceAdapter()190*da58b97aSjoerg   ~MatcherInterfaceAdapter() override { delete impl_; }
191*da58b97aSjoerg 
DescribeTo(::std::ostream * os)192*da58b97aSjoerg   void DescribeTo(::std::ostream* os) const override { impl_->DescribeTo(os); }
193*da58b97aSjoerg 
DescribeNegationTo(::std::ostream * os)194*da58b97aSjoerg   void DescribeNegationTo(::std::ostream* os) const override {
195*da58b97aSjoerg     impl_->DescribeNegationTo(os);
196*da58b97aSjoerg   }
197*da58b97aSjoerg 
MatchAndExplain(const T & x,MatchResultListener * listener)198*da58b97aSjoerg   bool MatchAndExplain(const T& x,
199*da58b97aSjoerg                        MatchResultListener* listener) const override {
200*da58b97aSjoerg     return impl_->MatchAndExplain(x, listener);
201*da58b97aSjoerg   }
202*da58b97aSjoerg 
203*da58b97aSjoerg  private:
204*da58b97aSjoerg   const MatcherInterface<T>* const impl_;
205*da58b97aSjoerg 
206*da58b97aSjoerg   GTEST_DISALLOW_COPY_AND_ASSIGN_(MatcherInterfaceAdapter);
207*da58b97aSjoerg };
208*da58b97aSjoerg 
209*da58b97aSjoerg struct AnyEq {
210*da58b97aSjoerg   template <typename A, typename B>
operatorAnyEq211*da58b97aSjoerg   bool operator()(const A& a, const B& b) const { return a == b; }
212*da58b97aSjoerg };
213*da58b97aSjoerg struct AnyNe {
214*da58b97aSjoerg   template <typename A, typename B>
operatorAnyNe215*da58b97aSjoerg   bool operator()(const A& a, const B& b) const { return a != b; }
216*da58b97aSjoerg };
217*da58b97aSjoerg struct AnyLt {
218*da58b97aSjoerg   template <typename A, typename B>
operatorAnyLt219*da58b97aSjoerg   bool operator()(const A& a, const B& b) const { return a < b; }
220*da58b97aSjoerg };
221*da58b97aSjoerg struct AnyGt {
222*da58b97aSjoerg   template <typename A, typename B>
operatorAnyGt223*da58b97aSjoerg   bool operator()(const A& a, const B& b) const { return a > b; }
224*da58b97aSjoerg };
225*da58b97aSjoerg struct AnyLe {
226*da58b97aSjoerg   template <typename A, typename B>
operatorAnyLe227*da58b97aSjoerg   bool operator()(const A& a, const B& b) const { return a <= b; }
228*da58b97aSjoerg };
229*da58b97aSjoerg struct AnyGe {
230*da58b97aSjoerg   template <typename A, typename B>
operatorAnyGe231*da58b97aSjoerg   bool operator()(const A& a, const B& b) const { return a >= b; }
232*da58b97aSjoerg };
233*da58b97aSjoerg 
234*da58b97aSjoerg // A match result listener that ignores the explanation.
235*da58b97aSjoerg class DummyMatchResultListener : public MatchResultListener {
236*da58b97aSjoerg  public:
DummyMatchResultListener()237*da58b97aSjoerg   DummyMatchResultListener() : MatchResultListener(nullptr) {}
238*da58b97aSjoerg 
239*da58b97aSjoerg  private:
240*da58b97aSjoerg   GTEST_DISALLOW_COPY_AND_ASSIGN_(DummyMatchResultListener);
241*da58b97aSjoerg };
242*da58b97aSjoerg 
243*da58b97aSjoerg // A match result listener that forwards the explanation to a given
244*da58b97aSjoerg // ostream.  The difference between this and MatchResultListener is
245*da58b97aSjoerg // that the former is concrete.
246*da58b97aSjoerg class StreamMatchResultListener : public MatchResultListener {
247*da58b97aSjoerg  public:
StreamMatchResultListener(::std::ostream * os)248*da58b97aSjoerg   explicit StreamMatchResultListener(::std::ostream* os)
249*da58b97aSjoerg       : MatchResultListener(os) {}
250*da58b97aSjoerg 
251*da58b97aSjoerg  private:
252*da58b97aSjoerg   GTEST_DISALLOW_COPY_AND_ASSIGN_(StreamMatchResultListener);
253*da58b97aSjoerg };
254*da58b97aSjoerg 
255*da58b97aSjoerg // An internal class for implementing Matcher<T>, which will derive
256*da58b97aSjoerg // from it.  We put functionalities common to all Matcher<T>
257*da58b97aSjoerg // specializations here to avoid code duplication.
258*da58b97aSjoerg template <typename T>
259*da58b97aSjoerg class MatcherBase {
260*da58b97aSjoerg  public:
261*da58b97aSjoerg   // Returns true if and only if the matcher matches x; also explains the
262*da58b97aSjoerg   // match result to 'listener'.
MatchAndExplain(const T & x,MatchResultListener * listener)263*da58b97aSjoerg   bool MatchAndExplain(const T& x, MatchResultListener* listener) const {
264*da58b97aSjoerg     return impl_->MatchAndExplain(x, listener);
265*da58b97aSjoerg   }
266*da58b97aSjoerg 
267*da58b97aSjoerg   // Returns true if and only if this matcher matches x.
Matches(const T & x)268*da58b97aSjoerg   bool Matches(const T& x) const {
269*da58b97aSjoerg     DummyMatchResultListener dummy;
270*da58b97aSjoerg     return MatchAndExplain(x, &dummy);
271*da58b97aSjoerg   }
272*da58b97aSjoerg 
273*da58b97aSjoerg   // Describes this matcher to an ostream.
DescribeTo(::std::ostream * os)274*da58b97aSjoerg   void DescribeTo(::std::ostream* os) const { impl_->DescribeTo(os); }
275*da58b97aSjoerg 
276*da58b97aSjoerg   // Describes the negation of this matcher to an ostream.
DescribeNegationTo(::std::ostream * os)277*da58b97aSjoerg   void DescribeNegationTo(::std::ostream* os) const {
278*da58b97aSjoerg     impl_->DescribeNegationTo(os);
279*da58b97aSjoerg   }
280*da58b97aSjoerg 
281*da58b97aSjoerg   // Explains why x matches, or doesn't match, the matcher.
ExplainMatchResultTo(const T & x,::std::ostream * os)282*da58b97aSjoerg   void ExplainMatchResultTo(const T& x, ::std::ostream* os) const {
283*da58b97aSjoerg     StreamMatchResultListener listener(os);
284*da58b97aSjoerg     MatchAndExplain(x, &listener);
285*da58b97aSjoerg   }
286*da58b97aSjoerg 
287*da58b97aSjoerg   // Returns the describer for this matcher object; retains ownership
288*da58b97aSjoerg   // of the describer, which is only guaranteed to be alive when
289*da58b97aSjoerg   // this matcher object is alive.
GetDescriber()290*da58b97aSjoerg   const MatcherDescriberInterface* GetDescriber() const {
291*da58b97aSjoerg     return impl_.get();
292*da58b97aSjoerg   }
293*da58b97aSjoerg 
294*da58b97aSjoerg  protected:
MatcherBase()295*da58b97aSjoerg   MatcherBase() {}
296*da58b97aSjoerg 
297*da58b97aSjoerg   // Constructs a matcher from its implementation.
MatcherBase(const MatcherInterface<const T &> * impl)298*da58b97aSjoerg   explicit MatcherBase(const MatcherInterface<const T&>* impl) : impl_(impl) {}
299*da58b97aSjoerg 
300*da58b97aSjoerg   template <typename U>
301*da58b97aSjoerg   explicit MatcherBase(
302*da58b97aSjoerg       const MatcherInterface<U>* impl,
303*da58b97aSjoerg       typename std::enable_if<!std::is_same<U, const U&>::value>::type* =
304*da58b97aSjoerg           nullptr)
impl_(new internal::MatcherInterfaceAdapter<U> (impl))305*da58b97aSjoerg       : impl_(new internal::MatcherInterfaceAdapter<U>(impl)) {}
306*da58b97aSjoerg 
307*da58b97aSjoerg   MatcherBase(const MatcherBase&) = default;
308*da58b97aSjoerg   MatcherBase& operator=(const MatcherBase&) = default;
309*da58b97aSjoerg   MatcherBase(MatcherBase&&) = default;
310*da58b97aSjoerg   MatcherBase& operator=(MatcherBase&&) = default;
311*da58b97aSjoerg 
~MatcherBase()312*da58b97aSjoerg   virtual ~MatcherBase() {}
313*da58b97aSjoerg 
314*da58b97aSjoerg  private:
315*da58b97aSjoerg   std::shared_ptr<const MatcherInterface<const T&>> impl_;
316*da58b97aSjoerg };
317*da58b97aSjoerg 
318*da58b97aSjoerg }  // namespace internal
319*da58b97aSjoerg 
320*da58b97aSjoerg // A Matcher<T> is a copyable and IMMUTABLE (except by assignment)
321*da58b97aSjoerg // object that can check whether a value of type T matches.  The
322*da58b97aSjoerg // implementation of Matcher<T> is just a std::shared_ptr to const
323*da58b97aSjoerg // MatcherInterface<T>.  Don't inherit from Matcher!
324*da58b97aSjoerg template <typename T>
325*da58b97aSjoerg class Matcher : public internal::MatcherBase<T> {
326*da58b97aSjoerg  public:
327*da58b97aSjoerg   // Constructs a null matcher.  Needed for storing Matcher objects in STL
328*da58b97aSjoerg   // containers.  A default-constructed matcher is not yet initialized.  You
329*da58b97aSjoerg   // cannot use it until a valid value has been assigned to it.
Matcher()330*da58b97aSjoerg   explicit Matcher() {}  // NOLINT
331*da58b97aSjoerg 
332*da58b97aSjoerg   // Constructs a matcher from its implementation.
Matcher(const MatcherInterface<const T &> * impl)333*da58b97aSjoerg   explicit Matcher(const MatcherInterface<const T&>* impl)
334*da58b97aSjoerg       : internal::MatcherBase<T>(impl) {}
335*da58b97aSjoerg 
336*da58b97aSjoerg   template <typename U>
337*da58b97aSjoerg   explicit Matcher(
338*da58b97aSjoerg       const MatcherInterface<U>* impl,
339*da58b97aSjoerg       typename std::enable_if<!std::is_same<U, const U&>::value>::type* =
340*da58b97aSjoerg           nullptr)
341*da58b97aSjoerg       : internal::MatcherBase<T>(impl) {}
342*da58b97aSjoerg 
343*da58b97aSjoerg   // Implicit constructor here allows people to write
344*da58b97aSjoerg   // EXPECT_CALL(foo, Bar(5)) instead of EXPECT_CALL(foo, Bar(Eq(5))) sometimes
345*da58b97aSjoerg   Matcher(T value);  // NOLINT
346*da58b97aSjoerg };
347*da58b97aSjoerg 
348*da58b97aSjoerg // The following two specializations allow the user to write str
349*da58b97aSjoerg // instead of Eq(str) and "foo" instead of Eq("foo") when a std::string
350*da58b97aSjoerg // matcher is expected.
351*da58b97aSjoerg template <>
352*da58b97aSjoerg class GTEST_API_ Matcher<const std::string&>
353*da58b97aSjoerg     : public internal::MatcherBase<const std::string&> {
354*da58b97aSjoerg  public:
Matcher()355*da58b97aSjoerg   Matcher() {}
356*da58b97aSjoerg 
Matcher(const MatcherInterface<const std::string &> * impl)357*da58b97aSjoerg   explicit Matcher(const MatcherInterface<const std::string&>* impl)
358*da58b97aSjoerg       : internal::MatcherBase<const std::string&>(impl) {}
359*da58b97aSjoerg 
360*da58b97aSjoerg   // Allows the user to write str instead of Eq(str) sometimes, where
361*da58b97aSjoerg   // str is a std::string object.
362*da58b97aSjoerg   Matcher(const std::string& s);  // NOLINT
363*da58b97aSjoerg 
364*da58b97aSjoerg   // Allows the user to write "foo" instead of Eq("foo") sometimes.
365*da58b97aSjoerg   Matcher(const char* s);  // NOLINT
366*da58b97aSjoerg };
367*da58b97aSjoerg 
368*da58b97aSjoerg template <>
369*da58b97aSjoerg class GTEST_API_ Matcher<std::string>
370*da58b97aSjoerg     : public internal::MatcherBase<std::string> {
371*da58b97aSjoerg  public:
Matcher()372*da58b97aSjoerg   Matcher() {}
373*da58b97aSjoerg 
Matcher(const MatcherInterface<const std::string &> * impl)374*da58b97aSjoerg   explicit Matcher(const MatcherInterface<const std::string&>* impl)
375*da58b97aSjoerg       : internal::MatcherBase<std::string>(impl) {}
Matcher(const MatcherInterface<std::string> * impl)376*da58b97aSjoerg   explicit Matcher(const MatcherInterface<std::string>* impl)
377*da58b97aSjoerg       : internal::MatcherBase<std::string>(impl) {}
378*da58b97aSjoerg 
379*da58b97aSjoerg   // Allows the user to write str instead of Eq(str) sometimes, where
380*da58b97aSjoerg   // str is a string object.
381*da58b97aSjoerg   Matcher(const std::string& s);  // NOLINT
382*da58b97aSjoerg 
383*da58b97aSjoerg   // Allows the user to write "foo" instead of Eq("foo") sometimes.
384*da58b97aSjoerg   Matcher(const char* s);  // NOLINT
385*da58b97aSjoerg };
386*da58b97aSjoerg 
387*da58b97aSjoerg #if GTEST_HAS_ABSL
388*da58b97aSjoerg // The following two specializations allow the user to write str
389*da58b97aSjoerg // instead of Eq(str) and "foo" instead of Eq("foo") when a absl::string_view
390*da58b97aSjoerg // matcher is expected.
391*da58b97aSjoerg template <>
392*da58b97aSjoerg class GTEST_API_ Matcher<const absl::string_view&>
393*da58b97aSjoerg     : public internal::MatcherBase<const absl::string_view&> {
394*da58b97aSjoerg  public:
Matcher()395*da58b97aSjoerg   Matcher() {}
396*da58b97aSjoerg 
Matcher(const MatcherInterface<const absl::string_view &> * impl)397*da58b97aSjoerg   explicit Matcher(const MatcherInterface<const absl::string_view&>* impl)
398*da58b97aSjoerg       : internal::MatcherBase<const absl::string_view&>(impl) {}
399*da58b97aSjoerg 
400*da58b97aSjoerg   // Allows the user to write str instead of Eq(str) sometimes, where
401*da58b97aSjoerg   // str is a std::string object.
402*da58b97aSjoerg   Matcher(const std::string& s);  // NOLINT
403*da58b97aSjoerg 
404*da58b97aSjoerg   // Allows the user to write "foo" instead of Eq("foo") sometimes.
405*da58b97aSjoerg   Matcher(const char* s);  // NOLINT
406*da58b97aSjoerg 
407*da58b97aSjoerg   // Allows the user to pass absl::string_views directly.
408*da58b97aSjoerg   Matcher(absl::string_view s);  // NOLINT
409*da58b97aSjoerg };
410*da58b97aSjoerg 
411*da58b97aSjoerg template <>
412*da58b97aSjoerg class GTEST_API_ Matcher<absl::string_view>
413*da58b97aSjoerg     : public internal::MatcherBase<absl::string_view> {
414*da58b97aSjoerg  public:
Matcher()415*da58b97aSjoerg   Matcher() {}
416*da58b97aSjoerg 
Matcher(const MatcherInterface<const absl::string_view &> * impl)417*da58b97aSjoerg   explicit Matcher(const MatcherInterface<const absl::string_view&>* impl)
418*da58b97aSjoerg       : internal::MatcherBase<absl::string_view>(impl) {}
Matcher(const MatcherInterface<absl::string_view> * impl)419*da58b97aSjoerg   explicit Matcher(const MatcherInterface<absl::string_view>* impl)
420*da58b97aSjoerg       : internal::MatcherBase<absl::string_view>(impl) {}
421*da58b97aSjoerg 
422*da58b97aSjoerg   // Allows the user to write str instead of Eq(str) sometimes, where
423*da58b97aSjoerg   // str is a std::string object.
424*da58b97aSjoerg   Matcher(const std::string& s);  // NOLINT
425*da58b97aSjoerg 
426*da58b97aSjoerg   // Allows the user to write "foo" instead of Eq("foo") sometimes.
427*da58b97aSjoerg   Matcher(const char* s);  // NOLINT
428*da58b97aSjoerg 
429*da58b97aSjoerg   // Allows the user to pass absl::string_views directly.
430*da58b97aSjoerg   Matcher(absl::string_view s);  // NOLINT
431*da58b97aSjoerg };
432*da58b97aSjoerg #endif  // GTEST_HAS_ABSL
433*da58b97aSjoerg 
434*da58b97aSjoerg // Prints a matcher in a human-readable format.
435*da58b97aSjoerg template <typename T>
436*da58b97aSjoerg std::ostream& operator<<(std::ostream& os, const Matcher<T>& matcher) {
437*da58b97aSjoerg   matcher.DescribeTo(&os);
438*da58b97aSjoerg   return os;
439*da58b97aSjoerg }
440*da58b97aSjoerg 
441*da58b97aSjoerg // The PolymorphicMatcher class template makes it easy to implement a
442*da58b97aSjoerg // polymorphic matcher (i.e. a matcher that can match values of more
443*da58b97aSjoerg // than one type, e.g. Eq(n) and NotNull()).
444*da58b97aSjoerg //
445*da58b97aSjoerg // To define a polymorphic matcher, a user should provide an Impl
446*da58b97aSjoerg // class that has a DescribeTo() method and a DescribeNegationTo()
447*da58b97aSjoerg // method, and define a member function (or member function template)
448*da58b97aSjoerg //
449*da58b97aSjoerg //   bool MatchAndExplain(const Value& value,
450*da58b97aSjoerg //                        MatchResultListener* listener) const;
451*da58b97aSjoerg //
452*da58b97aSjoerg // See the definition of NotNull() for a complete example.
453*da58b97aSjoerg template <class Impl>
454*da58b97aSjoerg class PolymorphicMatcher {
455*da58b97aSjoerg  public:
PolymorphicMatcher(const Impl & an_impl)456*da58b97aSjoerg   explicit PolymorphicMatcher(const Impl& an_impl) : impl_(an_impl) {}
457*da58b97aSjoerg 
458*da58b97aSjoerg   // Returns a mutable reference to the underlying matcher
459*da58b97aSjoerg   // implementation object.
mutable_impl()460*da58b97aSjoerg   Impl& mutable_impl() { return impl_; }
461*da58b97aSjoerg 
462*da58b97aSjoerg   // Returns an immutable reference to the underlying matcher
463*da58b97aSjoerg   // implementation object.
impl()464*da58b97aSjoerg   const Impl& impl() const { return impl_; }
465*da58b97aSjoerg 
466*da58b97aSjoerg   template <typename T>
467*da58b97aSjoerg   operator Matcher<T>() const {
468*da58b97aSjoerg     return Matcher<T>(new MonomorphicImpl<const T&>(impl_));
469*da58b97aSjoerg   }
470*da58b97aSjoerg 
471*da58b97aSjoerg  private:
472*da58b97aSjoerg   template <typename T>
473*da58b97aSjoerg   class MonomorphicImpl : public MatcherInterface<T> {
474*da58b97aSjoerg    public:
MonomorphicImpl(const Impl & impl)475*da58b97aSjoerg     explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {}
476*da58b97aSjoerg 
DescribeTo(::std::ostream * os)477*da58b97aSjoerg     virtual void DescribeTo(::std::ostream* os) const { impl_.DescribeTo(os); }
478*da58b97aSjoerg 
DescribeNegationTo(::std::ostream * os)479*da58b97aSjoerg     virtual void DescribeNegationTo(::std::ostream* os) const {
480*da58b97aSjoerg       impl_.DescribeNegationTo(os);
481*da58b97aSjoerg     }
482*da58b97aSjoerg 
MatchAndExplain(T x,MatchResultListener * listener)483*da58b97aSjoerg     virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
484*da58b97aSjoerg       return impl_.MatchAndExplain(x, listener);
485*da58b97aSjoerg     }
486*da58b97aSjoerg 
487*da58b97aSjoerg    private:
488*da58b97aSjoerg     const Impl impl_;
489*da58b97aSjoerg   };
490*da58b97aSjoerg 
491*da58b97aSjoerg   Impl impl_;
492*da58b97aSjoerg };
493*da58b97aSjoerg 
494*da58b97aSjoerg // Creates a matcher from its implementation.
495*da58b97aSjoerg // DEPRECATED: Especially in the generic code, prefer:
496*da58b97aSjoerg //   Matcher<T>(new MyMatcherImpl<const T&>(...));
497*da58b97aSjoerg //
498*da58b97aSjoerg // MakeMatcher may create a Matcher that accepts its argument by value, which
499*da58b97aSjoerg // leads to unnecessary copies & lack of support for non-copyable types.
500*da58b97aSjoerg template <typename T>
MakeMatcher(const MatcherInterface<T> * impl)501*da58b97aSjoerg inline Matcher<T> MakeMatcher(const MatcherInterface<T>* impl) {
502*da58b97aSjoerg   return Matcher<T>(impl);
503*da58b97aSjoerg }
504*da58b97aSjoerg 
505*da58b97aSjoerg // Creates a polymorphic matcher from its implementation.  This is
506*da58b97aSjoerg // easier to use than the PolymorphicMatcher<Impl> constructor as it
507*da58b97aSjoerg // doesn't require you to explicitly write the template argument, e.g.
508*da58b97aSjoerg //
509*da58b97aSjoerg //   MakePolymorphicMatcher(foo);
510*da58b97aSjoerg // vs
511*da58b97aSjoerg //   PolymorphicMatcher<TypeOfFoo>(foo);
512*da58b97aSjoerg template <class Impl>
MakePolymorphicMatcher(const Impl & impl)513*da58b97aSjoerg inline PolymorphicMatcher<Impl> MakePolymorphicMatcher(const Impl& impl) {
514*da58b97aSjoerg   return PolymorphicMatcher<Impl>(impl);
515*da58b97aSjoerg }
516*da58b97aSjoerg 
517*da58b97aSjoerg namespace internal {
518*da58b97aSjoerg // Implements a matcher that compares a given value with a
519*da58b97aSjoerg // pre-supplied value using one of the ==, <=, <, etc, operators.  The
520*da58b97aSjoerg // two values being compared don't have to have the same type.
521*da58b97aSjoerg //
522*da58b97aSjoerg // The matcher defined here is polymorphic (for example, Eq(5) can be
523*da58b97aSjoerg // used to match an int, a short, a double, etc).  Therefore we use
524*da58b97aSjoerg // a template type conversion operator in the implementation.
525*da58b97aSjoerg //
526*da58b97aSjoerg // The following template definition assumes that the Rhs parameter is
527*da58b97aSjoerg // a "bare" type (i.e. neither 'const T' nor 'T&').
528*da58b97aSjoerg template <typename D, typename Rhs, typename Op>
529*da58b97aSjoerg class ComparisonBase {
530*da58b97aSjoerg  public:
ComparisonBase(const Rhs & rhs)531*da58b97aSjoerg   explicit ComparisonBase(const Rhs& rhs) : rhs_(rhs) {}
532*da58b97aSjoerg   template <typename Lhs>
533*da58b97aSjoerg   operator Matcher<Lhs>() const {
534*da58b97aSjoerg     return Matcher<Lhs>(new Impl<const Lhs&>(rhs_));
535*da58b97aSjoerg   }
536*da58b97aSjoerg 
537*da58b97aSjoerg  private:
538*da58b97aSjoerg   template <typename T>
Unwrap(const T & v)539*da58b97aSjoerg   static const T& Unwrap(const T& v) { return v; }
540*da58b97aSjoerg   template <typename T>
Unwrap(std::reference_wrapper<T> v)541*da58b97aSjoerg   static const T& Unwrap(std::reference_wrapper<T> v) { return v; }
542*da58b97aSjoerg 
543*da58b97aSjoerg   template <typename Lhs, typename = Rhs>
544*da58b97aSjoerg   class Impl : public MatcherInterface<Lhs> {
545*da58b97aSjoerg    public:
Impl(const Rhs & rhs)546*da58b97aSjoerg     explicit Impl(const Rhs& rhs) : rhs_(rhs) {}
MatchAndExplain(Lhs lhs,MatchResultListener *)547*da58b97aSjoerg     bool MatchAndExplain(Lhs lhs,
548*da58b97aSjoerg                          MatchResultListener* /* listener */) const override {
549*da58b97aSjoerg       return Op()(lhs, Unwrap(rhs_));
550*da58b97aSjoerg     }
DescribeTo(::std::ostream * os)551*da58b97aSjoerg     void DescribeTo(::std::ostream* os) const override {
552*da58b97aSjoerg       *os << D::Desc() << " ";
553*da58b97aSjoerg       UniversalPrint(Unwrap(rhs_), os);
554*da58b97aSjoerg     }
DescribeNegationTo(::std::ostream * os)555*da58b97aSjoerg     void DescribeNegationTo(::std::ostream* os) const override {
556*da58b97aSjoerg       *os << D::NegatedDesc() <<  " ";
557*da58b97aSjoerg       UniversalPrint(Unwrap(rhs_), os);
558*da58b97aSjoerg     }
559*da58b97aSjoerg 
560*da58b97aSjoerg    private:
561*da58b97aSjoerg     Rhs rhs_;
562*da58b97aSjoerg   };
563*da58b97aSjoerg   Rhs rhs_;
564*da58b97aSjoerg };
565*da58b97aSjoerg 
566*da58b97aSjoerg template <typename Rhs>
567*da58b97aSjoerg class EqMatcher : public ComparisonBase<EqMatcher<Rhs>, Rhs, AnyEq> {
568*da58b97aSjoerg  public:
EqMatcher(const Rhs & rhs)569*da58b97aSjoerg   explicit EqMatcher(const Rhs& rhs)
570*da58b97aSjoerg       : ComparisonBase<EqMatcher<Rhs>, Rhs, AnyEq>(rhs) { }
Desc()571*da58b97aSjoerg   static const char* Desc() { return "is equal to"; }
NegatedDesc()572*da58b97aSjoerg   static const char* NegatedDesc() { return "isn't equal to"; }
573*da58b97aSjoerg };
574*da58b97aSjoerg template <typename Rhs>
575*da58b97aSjoerg class NeMatcher : public ComparisonBase<NeMatcher<Rhs>, Rhs, AnyNe> {
576*da58b97aSjoerg  public:
NeMatcher(const Rhs & rhs)577*da58b97aSjoerg   explicit NeMatcher(const Rhs& rhs)
578*da58b97aSjoerg       : ComparisonBase<NeMatcher<Rhs>, Rhs, AnyNe>(rhs) { }
Desc()579*da58b97aSjoerg   static const char* Desc() { return "isn't equal to"; }
NegatedDesc()580*da58b97aSjoerg   static const char* NegatedDesc() { return "is equal to"; }
581*da58b97aSjoerg };
582*da58b97aSjoerg template <typename Rhs>
583*da58b97aSjoerg class LtMatcher : public ComparisonBase<LtMatcher<Rhs>, Rhs, AnyLt> {
584*da58b97aSjoerg  public:
LtMatcher(const Rhs & rhs)585*da58b97aSjoerg   explicit LtMatcher(const Rhs& rhs)
586*da58b97aSjoerg       : ComparisonBase<LtMatcher<Rhs>, Rhs, AnyLt>(rhs) { }
Desc()587*da58b97aSjoerg   static const char* Desc() { return "is <"; }
NegatedDesc()588*da58b97aSjoerg   static const char* NegatedDesc() { return "isn't <"; }
589*da58b97aSjoerg };
590*da58b97aSjoerg template <typename Rhs>
591*da58b97aSjoerg class GtMatcher : public ComparisonBase<GtMatcher<Rhs>, Rhs, AnyGt> {
592*da58b97aSjoerg  public:
GtMatcher(const Rhs & rhs)593*da58b97aSjoerg   explicit GtMatcher(const Rhs& rhs)
594*da58b97aSjoerg       : ComparisonBase<GtMatcher<Rhs>, Rhs, AnyGt>(rhs) { }
Desc()595*da58b97aSjoerg   static const char* Desc() { return "is >"; }
NegatedDesc()596*da58b97aSjoerg   static const char* NegatedDesc() { return "isn't >"; }
597*da58b97aSjoerg };
598*da58b97aSjoerg template <typename Rhs>
599*da58b97aSjoerg class LeMatcher : public ComparisonBase<LeMatcher<Rhs>, Rhs, AnyLe> {
600*da58b97aSjoerg  public:
LeMatcher(const Rhs & rhs)601*da58b97aSjoerg   explicit LeMatcher(const Rhs& rhs)
602*da58b97aSjoerg       : ComparisonBase<LeMatcher<Rhs>, Rhs, AnyLe>(rhs) { }
Desc()603*da58b97aSjoerg   static const char* Desc() { return "is <="; }
NegatedDesc()604*da58b97aSjoerg   static const char* NegatedDesc() { return "isn't <="; }
605*da58b97aSjoerg };
606*da58b97aSjoerg template <typename Rhs>
607*da58b97aSjoerg class GeMatcher : public ComparisonBase<GeMatcher<Rhs>, Rhs, AnyGe> {
608*da58b97aSjoerg  public:
GeMatcher(const Rhs & rhs)609*da58b97aSjoerg   explicit GeMatcher(const Rhs& rhs)
610*da58b97aSjoerg       : ComparisonBase<GeMatcher<Rhs>, Rhs, AnyGe>(rhs) { }
Desc()611*da58b97aSjoerg   static const char* Desc() { return "is >="; }
NegatedDesc()612*da58b97aSjoerg   static const char* NegatedDesc() { return "isn't >="; }
613*da58b97aSjoerg };
614*da58b97aSjoerg 
615*da58b97aSjoerg // Implements polymorphic matchers MatchesRegex(regex) and
616*da58b97aSjoerg // ContainsRegex(regex), which can be used as a Matcher<T> as long as
617*da58b97aSjoerg // T can be converted to a string.
618*da58b97aSjoerg class MatchesRegexMatcher {
619*da58b97aSjoerg  public:
MatchesRegexMatcher(const RE * regex,bool full_match)620*da58b97aSjoerg   MatchesRegexMatcher(const RE* regex, bool full_match)
621*da58b97aSjoerg       : regex_(regex), full_match_(full_match) {}
622*da58b97aSjoerg 
623*da58b97aSjoerg #if GTEST_HAS_ABSL
MatchAndExplain(const absl::string_view & s,MatchResultListener * listener)624*da58b97aSjoerg   bool MatchAndExplain(const absl::string_view& s,
625*da58b97aSjoerg                        MatchResultListener* listener) const {
626*da58b97aSjoerg     return MatchAndExplain(std::string(s), listener);
627*da58b97aSjoerg   }
628*da58b97aSjoerg #endif  // GTEST_HAS_ABSL
629*da58b97aSjoerg 
630*da58b97aSjoerg   // Accepts pointer types, particularly:
631*da58b97aSjoerg   //   const char*
632*da58b97aSjoerg   //   char*
633*da58b97aSjoerg   //   const wchar_t*
634*da58b97aSjoerg   //   wchar_t*
635*da58b97aSjoerg   template <typename CharType>
MatchAndExplain(CharType * s,MatchResultListener * listener)636*da58b97aSjoerg   bool MatchAndExplain(CharType* s, MatchResultListener* listener) const {
637*da58b97aSjoerg     return s != nullptr && MatchAndExplain(std::string(s), listener);
638*da58b97aSjoerg   }
639*da58b97aSjoerg 
640*da58b97aSjoerg   // Matches anything that can convert to std::string.
641*da58b97aSjoerg   //
642*da58b97aSjoerg   // This is a template, not just a plain function with const std::string&,
643*da58b97aSjoerg   // because absl::string_view has some interfering non-explicit constructors.
644*da58b97aSjoerg   template <class MatcheeStringType>
MatchAndExplain(const MatcheeStringType & s,MatchResultListener *)645*da58b97aSjoerg   bool MatchAndExplain(const MatcheeStringType& s,
646*da58b97aSjoerg                        MatchResultListener* /* listener */) const {
647*da58b97aSjoerg     const std::string& s2(s);
648*da58b97aSjoerg     return full_match_ ? RE::FullMatch(s2, *regex_)
649*da58b97aSjoerg                        : RE::PartialMatch(s2, *regex_);
650*da58b97aSjoerg   }
651*da58b97aSjoerg 
DescribeTo(::std::ostream * os)652*da58b97aSjoerg   void DescribeTo(::std::ostream* os) const {
653*da58b97aSjoerg     *os << (full_match_ ? "matches" : "contains") << " regular expression ";
654*da58b97aSjoerg     UniversalPrinter<std::string>::Print(regex_->pattern(), os);
655*da58b97aSjoerg   }
656*da58b97aSjoerg 
DescribeNegationTo(::std::ostream * os)657*da58b97aSjoerg   void DescribeNegationTo(::std::ostream* os) const {
658*da58b97aSjoerg     *os << "doesn't " << (full_match_ ? "match" : "contain")
659*da58b97aSjoerg         << " regular expression ";
660*da58b97aSjoerg     UniversalPrinter<std::string>::Print(regex_->pattern(), os);
661*da58b97aSjoerg   }
662*da58b97aSjoerg 
663*da58b97aSjoerg  private:
664*da58b97aSjoerg   const std::shared_ptr<const RE> regex_;
665*da58b97aSjoerg   const bool full_match_;
666*da58b97aSjoerg };
667*da58b97aSjoerg }  // namespace internal
668*da58b97aSjoerg 
669*da58b97aSjoerg // Matches a string that fully matches regular expression 'regex'.
670*da58b97aSjoerg // The matcher takes ownership of 'regex'.
MatchesRegex(const internal::RE * regex)671*da58b97aSjoerg inline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex(
672*da58b97aSjoerg     const internal::RE* regex) {
673*da58b97aSjoerg   return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, true));
674*da58b97aSjoerg }
MatchesRegex(const std::string & regex)675*da58b97aSjoerg inline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex(
676*da58b97aSjoerg     const std::string& regex) {
677*da58b97aSjoerg   return MatchesRegex(new internal::RE(regex));
678*da58b97aSjoerg }
679*da58b97aSjoerg 
680*da58b97aSjoerg // Matches a string that contains regular expression 'regex'.
681*da58b97aSjoerg // The matcher takes ownership of 'regex'.
ContainsRegex(const internal::RE * regex)682*da58b97aSjoerg inline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex(
683*da58b97aSjoerg     const internal::RE* regex) {
684*da58b97aSjoerg   return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, false));
685*da58b97aSjoerg }
ContainsRegex(const std::string & regex)686*da58b97aSjoerg inline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex(
687*da58b97aSjoerg     const std::string& regex) {
688*da58b97aSjoerg   return ContainsRegex(new internal::RE(regex));
689*da58b97aSjoerg }
690*da58b97aSjoerg 
691*da58b97aSjoerg // Creates a polymorphic matcher that matches anything equal to x.
692*da58b97aSjoerg // Note: if the parameter of Eq() were declared as const T&, Eq("foo")
693*da58b97aSjoerg // wouldn't compile.
694*da58b97aSjoerg template <typename T>
Eq(T x)695*da58b97aSjoerg inline internal::EqMatcher<T> Eq(T x) { return internal::EqMatcher<T>(x); }
696*da58b97aSjoerg 
697*da58b97aSjoerg // Constructs a Matcher<T> from a 'value' of type T.  The constructed
698*da58b97aSjoerg // matcher matches any value that's equal to 'value'.
699*da58b97aSjoerg template <typename T>
Matcher(T value)700*da58b97aSjoerg Matcher<T>::Matcher(T value) { *this = Eq(value); }
701*da58b97aSjoerg 
702*da58b97aSjoerg // Creates a monomorphic matcher that matches anything with type Lhs
703*da58b97aSjoerg // and equal to rhs.  A user may need to use this instead of Eq(...)
704*da58b97aSjoerg // in order to resolve an overloading ambiguity.
705*da58b97aSjoerg //
706*da58b97aSjoerg // TypedEq<T>(x) is just a convenient short-hand for Matcher<T>(Eq(x))
707*da58b97aSjoerg // or Matcher<T>(x), but more readable than the latter.
708*da58b97aSjoerg //
709*da58b97aSjoerg // We could define similar monomorphic matchers for other comparison
710*da58b97aSjoerg // operations (e.g. TypedLt, TypedGe, and etc), but decided not to do
711*da58b97aSjoerg // it yet as those are used much less than Eq() in practice.  A user
712*da58b97aSjoerg // can always write Matcher<T>(Lt(5)) to be explicit about the type,
713*da58b97aSjoerg // for example.
714*da58b97aSjoerg template <typename Lhs, typename Rhs>
TypedEq(const Rhs & rhs)715*da58b97aSjoerg inline Matcher<Lhs> TypedEq(const Rhs& rhs) { return Eq(rhs); }
716*da58b97aSjoerg 
717*da58b97aSjoerg // Creates a polymorphic matcher that matches anything >= x.
718*da58b97aSjoerg template <typename Rhs>
Ge(Rhs x)719*da58b97aSjoerg inline internal::GeMatcher<Rhs> Ge(Rhs x) {
720*da58b97aSjoerg   return internal::GeMatcher<Rhs>(x);
721*da58b97aSjoerg }
722*da58b97aSjoerg 
723*da58b97aSjoerg // Creates a polymorphic matcher that matches anything > x.
724*da58b97aSjoerg template <typename Rhs>
Gt(Rhs x)725*da58b97aSjoerg inline internal::GtMatcher<Rhs> Gt(Rhs x) {
726*da58b97aSjoerg   return internal::GtMatcher<Rhs>(x);
727*da58b97aSjoerg }
728*da58b97aSjoerg 
729*da58b97aSjoerg // Creates a polymorphic matcher that matches anything <= x.
730*da58b97aSjoerg template <typename Rhs>
Le(Rhs x)731*da58b97aSjoerg inline internal::LeMatcher<Rhs> Le(Rhs x) {
732*da58b97aSjoerg   return internal::LeMatcher<Rhs>(x);
733*da58b97aSjoerg }
734*da58b97aSjoerg 
735*da58b97aSjoerg // Creates a polymorphic matcher that matches anything < x.
736*da58b97aSjoerg template <typename Rhs>
Lt(Rhs x)737*da58b97aSjoerg inline internal::LtMatcher<Rhs> Lt(Rhs x) {
738*da58b97aSjoerg   return internal::LtMatcher<Rhs>(x);
739*da58b97aSjoerg }
740*da58b97aSjoerg 
741*da58b97aSjoerg // Creates a polymorphic matcher that matches anything != x.
742*da58b97aSjoerg template <typename Rhs>
Ne(Rhs x)743*da58b97aSjoerg inline internal::NeMatcher<Rhs> Ne(Rhs x) {
744*da58b97aSjoerg   return internal::NeMatcher<Rhs>(x);
745*da58b97aSjoerg }
746*da58b97aSjoerg }  // namespace testing
747*da58b97aSjoerg 
748*da58b97aSjoerg GTEST_DISABLE_MSC_WARNINGS_POP_()  //  4251 5046
749*da58b97aSjoerg 
750*da58b97aSjoerg #endif  // GTEST_INCLUDE_GTEST_GTEST_MATCHERS_H_
751