You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

750 lines
26 KiB

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