shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 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 | // |
| 30 | // Author: wan@google.com (Zhanyong Wan) |
| 31 | |
| 32 | // Google Mock - a framework for writing C++ mock classes. |
| 33 | // |
| 34 | // This file implements some commonly used argument matchers. More |
| 35 | // matchers can be defined by the user implementing the |
| 36 | // MatcherInterface<T> interface if necessary. |
| 37 | |
| 38 | #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_ |
| 39 | #define GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_ |
| 40 | |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 41 | #include <algorithm> |
zhanyong.wan | 16cf473 | 2009-05-14 20:55:30 +0000 | [diff] [blame] | 42 | #include <limits> |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 43 | #include <ostream> // NOLINT |
| 44 | #include <sstream> |
| 45 | #include <string> |
| 46 | #include <vector> |
| 47 | |
| 48 | #include <gmock/gmock-printers.h> |
| 49 | #include <gmock/internal/gmock-internal-utils.h> |
| 50 | #include <gmock/internal/gmock-port.h> |
| 51 | #include <gtest/gtest.h> |
| 52 | |
| 53 | namespace testing { |
| 54 | |
| 55 | // To implement a matcher Foo for type T, define: |
| 56 | // 1. a class FooMatcherImpl that implements the |
| 57 | // MatcherInterface<T> interface, and |
| 58 | // 2. a factory function that creates a Matcher<T> object from a |
| 59 | // FooMatcherImpl*. |
| 60 | // |
| 61 | // The two-level delegation design makes it possible to allow a user |
| 62 | // to write "v" instead of "Eq(v)" where a Matcher is expected, which |
| 63 | // is impossible if we pass matchers by pointers. It also eases |
| 64 | // ownership management as Matcher objects can now be copied like |
| 65 | // plain values. |
| 66 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 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 | // TODO(wan@google.com): add method |
| 71 | // bool InterestedInWhy(bool result) const; |
| 72 | // to indicate whether the listener is interested in why the match |
| 73 | // result is 'result'. |
| 74 | class MatchResultListener { |
| 75 | public: |
| 76 | // Creates a listener object with the given underlying ostream. The |
| 77 | // listener does not own the ostream. |
| 78 | explicit MatchResultListener(::std::ostream* os) : stream_(os) {} |
| 79 | virtual ~MatchResultListener() = 0; // Makes this class abstract. |
| 80 | |
| 81 | // Streams x to the underlying ostream; does nothing if the ostream |
| 82 | // is NULL. |
| 83 | template <typename T> |
| 84 | MatchResultListener& operator<<(const T& x) { |
| 85 | if (stream_ != NULL) |
| 86 | *stream_ << x; |
| 87 | return *this; |
| 88 | } |
| 89 | |
| 90 | // Returns the underlying ostream. |
| 91 | ::std::ostream* stream() { return stream_; } |
| 92 | |
| 93 | private: |
| 94 | ::std::ostream* const stream_; |
| 95 | |
| 96 | GTEST_DISALLOW_COPY_AND_ASSIGN_(MatchResultListener); |
| 97 | }; |
| 98 | |
| 99 | inline MatchResultListener::~MatchResultListener() { |
| 100 | } |
| 101 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 102 | // The implementation of a matcher. |
| 103 | template <typename T> |
| 104 | class MatcherInterface { |
| 105 | public: |
| 106 | virtual ~MatcherInterface() {} |
| 107 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 108 | // Returns true iff the matcher matches x; also explains the match |
| 109 | // result to 'listener'. |
| 110 | // |
| 111 | // You should override this method when defining a new matcher. For |
| 112 | // backward compatibility, we provide a default implementation that |
| 113 | // just forwards to the old, deprecated matcher API (Matches() and |
| 114 | // ExplainMatchResultTo()). |
| 115 | // |
| 116 | // It's the responsibility of the caller (Google Mock) to guarantee |
| 117 | // that 'listener' is not NULL. This helps to simplify a matcher's |
| 118 | // implementation when it doesn't care about the performance, as it |
| 119 | // can talk to 'listener' without checking its validity first. |
| 120 | // However, in order to implement dummy listeners efficiently, |
| 121 | // listener->stream() may be NULL. |
| 122 | virtual bool MatchAndExplain(T x, MatchResultListener* listener) const { |
| 123 | const bool match = Matches(x); |
| 124 | if (listener->stream() != NULL) { |
| 125 | ExplainMatchResultTo(x, listener->stream()); |
| 126 | } |
| 127 | return match; |
| 128 | } |
| 129 | |
| 130 | // DEPRECATED. This method will be removed. Override |
| 131 | // MatchAndExplain() instead. |
| 132 | // |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 133 | // Returns true iff the matcher matches x. |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 134 | virtual bool Matches(T /* x */) const { return false; } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 135 | |
| 136 | // Describes this matcher to an ostream. |
| 137 | virtual void DescribeTo(::std::ostream* os) const = 0; |
| 138 | |
| 139 | // Describes the negation of this matcher to an ostream. For |
| 140 | // example, if the description of this matcher is "is greater than |
| 141 | // 7", the negated description could be "is not greater than 7". |
| 142 | // You are not required to override this when implementing |
| 143 | // MatcherInterface, but it is highly advised so that your matcher |
| 144 | // can produce good error messages. |
| 145 | virtual void DescribeNegationTo(::std::ostream* os) const { |
| 146 | *os << "not ("; |
| 147 | DescribeTo(os); |
| 148 | *os << ")"; |
| 149 | } |
| 150 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 151 | // DEPRECATED. This method will be removed. Override |
| 152 | // MatchAndExplain() instead. |
| 153 | // |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 154 | // Explains why x matches, or doesn't match, the matcher. Override |
| 155 | // this to provide any additional information that helps a user |
| 156 | // understand the match result. |
zhanyong.wan | 3fbd2dd | 2009-03-26 19:06:45 +0000 | [diff] [blame] | 157 | virtual void ExplainMatchResultTo(T /* x */, ::std::ostream* /* os */) const { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 158 | // By default, nothing more needs to be explained, as Google Mock |
| 159 | // has already printed the value of x when this function is |
| 160 | // called. |
| 161 | } |
| 162 | }; |
| 163 | |
| 164 | namespace internal { |
| 165 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 166 | // A match result listener that ignores the explanation. |
| 167 | class DummyMatchResultListener : public MatchResultListener { |
| 168 | public: |
| 169 | DummyMatchResultListener() : MatchResultListener(NULL) {} |
| 170 | |
| 171 | private: |
| 172 | GTEST_DISALLOW_COPY_AND_ASSIGN_(DummyMatchResultListener); |
| 173 | }; |
| 174 | |
| 175 | // A match result listener that forwards the explanation to a given |
| 176 | // ostream. The difference between this and MatchResultListener is |
| 177 | // that the former is concrete. |
| 178 | class StreamMatchResultListener : public MatchResultListener { |
| 179 | public: |
| 180 | explicit StreamMatchResultListener(::std::ostream* os) |
| 181 | : MatchResultListener(os) {} |
| 182 | |
| 183 | private: |
| 184 | GTEST_DISALLOW_COPY_AND_ASSIGN_(StreamMatchResultListener); |
| 185 | }; |
| 186 | |
| 187 | // A match result listener that stores the explanation in a string. |
| 188 | class StringMatchResultListener : public MatchResultListener { |
| 189 | public: |
| 190 | StringMatchResultListener() : MatchResultListener(&ss_) {} |
| 191 | |
| 192 | // Returns the explanation heard so far. |
| 193 | internal::string str() const { return ss_.str(); } |
| 194 | |
| 195 | private: |
| 196 | ::std::stringstream ss_; |
| 197 | |
| 198 | GTEST_DISALLOW_COPY_AND_ASSIGN_(StringMatchResultListener); |
| 199 | }; |
| 200 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 201 | // An internal class for implementing Matcher<T>, which will derive |
| 202 | // from it. We put functionalities common to all Matcher<T> |
| 203 | // specializations here to avoid code duplication. |
| 204 | template <typename T> |
| 205 | class MatcherBase { |
| 206 | public: |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 207 | // Returns true iff the matcher matches x; also explains the match |
| 208 | // result to 'listener'. |
| 209 | bool MatchAndExplain(T x, MatchResultListener* listener) const { |
| 210 | return impl_->MatchAndExplain(x, listener); |
| 211 | } |
| 212 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 213 | // Returns true iff this matcher matches x. |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 214 | bool Matches(T x) const { |
| 215 | DummyMatchResultListener dummy; |
| 216 | return MatchAndExplain(x, &dummy); |
| 217 | } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 218 | |
| 219 | // Describes this matcher to an ostream. |
| 220 | void DescribeTo(::std::ostream* os) const { impl_->DescribeTo(os); } |
| 221 | |
| 222 | // Describes the negation of this matcher to an ostream. |
| 223 | void DescribeNegationTo(::std::ostream* os) const { |
| 224 | impl_->DescribeNegationTo(os); |
| 225 | } |
| 226 | |
| 227 | // Explains why x matches, or doesn't match, the matcher. |
| 228 | void ExplainMatchResultTo(T x, ::std::ostream* os) const { |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 229 | StreamMatchResultListener listener(os); |
| 230 | MatchAndExplain(x, &listener); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 231 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 232 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 233 | protected: |
| 234 | MatcherBase() {} |
| 235 | |
| 236 | // Constructs a matcher from its implementation. |
| 237 | explicit MatcherBase(const MatcherInterface<T>* impl) |
| 238 | : impl_(impl) {} |
| 239 | |
| 240 | virtual ~MatcherBase() {} |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 241 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 242 | private: |
| 243 | // shared_ptr (util/gtl/shared_ptr.h) and linked_ptr have similar |
| 244 | // interfaces. The former dynamically allocates a chunk of memory |
| 245 | // to hold the reference count, while the latter tracks all |
| 246 | // references using a circular linked list without allocating |
| 247 | // memory. It has been observed that linked_ptr performs better in |
| 248 | // typical scenarios. However, shared_ptr can out-perform |
| 249 | // linked_ptr when there are many more uses of the copy constructor |
| 250 | // than the default constructor. |
| 251 | // |
| 252 | // If performance becomes a problem, we should see if using |
| 253 | // shared_ptr helps. |
| 254 | ::testing::internal::linked_ptr<const MatcherInterface<T> > impl_; |
| 255 | }; |
| 256 | |
| 257 | // The default implementation of ExplainMatchResultTo() for |
| 258 | // polymorphic matchers. |
| 259 | template <typename PolymorphicMatcherImpl, typename T> |
zhanyong.wan | 3fbd2dd | 2009-03-26 19:06:45 +0000 | [diff] [blame] | 260 | inline void ExplainMatchResultTo(const PolymorphicMatcherImpl& /* impl */, |
| 261 | const T& /* x */, |
| 262 | ::std::ostream* /* os */) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 263 | // By default, nothing more needs to be said, as Google Mock already |
| 264 | // prints the value of x elsewhere. |
| 265 | } |
| 266 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 267 | // The default implementation of MatchAndExplain() for polymorphic |
| 268 | // matchers. |
| 269 | template <typename PolymorphicMatcherImpl, typename T> |
| 270 | inline bool MatchAndExplain(const PolymorphicMatcherImpl& impl, |
| 271 | const T& x, |
| 272 | MatchResultListener* listener) { |
| 273 | const bool match = impl.Matches(x); |
| 274 | |
| 275 | ::std::ostream* const os = listener->stream(); |
| 276 | if (os != NULL) { |
| 277 | using ::testing::internal::ExplainMatchResultTo; |
| 278 | // When resolving the following call, both |
| 279 | // ::testing::internal::ExplainMatchResultTo() and |
| 280 | // foo::ExplainMatchResultTo() are considered, where foo is the |
| 281 | // namespace where class PolymorphicMatcherImpl is defined. |
| 282 | ExplainMatchResultTo(impl, x, os); |
| 283 | } |
| 284 | |
| 285 | return match; |
| 286 | } |
| 287 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 288 | } // namespace internal |
| 289 | |
| 290 | // A Matcher<T> is a copyable and IMMUTABLE (except by assignment) |
| 291 | // object that can check whether a value of type T matches. The |
| 292 | // implementation of Matcher<T> is just a linked_ptr to const |
| 293 | // MatcherInterface<T>, so copying is fairly cheap. Don't inherit |
| 294 | // from Matcher! |
| 295 | template <typename T> |
| 296 | class Matcher : public internal::MatcherBase<T> { |
| 297 | public: |
| 298 | // Constructs a null matcher. Needed for storing Matcher objects in |
| 299 | // STL containers. |
| 300 | Matcher() {} |
| 301 | |
| 302 | // Constructs a matcher from its implementation. |
| 303 | explicit Matcher(const MatcherInterface<T>* impl) |
| 304 | : internal::MatcherBase<T>(impl) {} |
| 305 | |
zhanyong.wan | 1849065 | 2009-05-11 18:54:08 +0000 | [diff] [blame] | 306 | // Implicit constructor here allows people to write |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 307 | // EXPECT_CALL(foo, Bar(5)) instead of EXPECT_CALL(foo, Bar(Eq(5))) sometimes |
| 308 | Matcher(T value); // NOLINT |
| 309 | }; |
| 310 | |
| 311 | // The following two specializations allow the user to write str |
| 312 | // instead of Eq(str) and "foo" instead of Eq("foo") when a string |
| 313 | // matcher is expected. |
| 314 | template <> |
| 315 | class Matcher<const internal::string&> |
| 316 | : public internal::MatcherBase<const internal::string&> { |
| 317 | public: |
| 318 | Matcher() {} |
| 319 | |
| 320 | explicit Matcher(const MatcherInterface<const internal::string&>* impl) |
| 321 | : internal::MatcherBase<const internal::string&>(impl) {} |
| 322 | |
| 323 | // Allows the user to write str instead of Eq(str) sometimes, where |
| 324 | // str is a string object. |
| 325 | Matcher(const internal::string& s); // NOLINT |
| 326 | |
| 327 | // Allows the user to write "foo" instead of Eq("foo") sometimes. |
| 328 | Matcher(const char* s); // NOLINT |
| 329 | }; |
| 330 | |
| 331 | template <> |
| 332 | class Matcher<internal::string> |
| 333 | : public internal::MatcherBase<internal::string> { |
| 334 | public: |
| 335 | Matcher() {} |
| 336 | |
| 337 | explicit Matcher(const MatcherInterface<internal::string>* impl) |
| 338 | : internal::MatcherBase<internal::string>(impl) {} |
| 339 | |
| 340 | // Allows the user to write str instead of Eq(str) sometimes, where |
| 341 | // str is a string object. |
| 342 | Matcher(const internal::string& s); // NOLINT |
| 343 | |
| 344 | // Allows the user to write "foo" instead of Eq("foo") sometimes. |
| 345 | Matcher(const char* s); // NOLINT |
| 346 | }; |
| 347 | |
| 348 | // The PolymorphicMatcher class template makes it easy to implement a |
| 349 | // polymorphic matcher (i.e. a matcher that can match values of more |
| 350 | // than one type, e.g. Eq(n) and NotNull()). |
| 351 | // |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 352 | // To define a polymorphic matcher in the old, deprecated way, a user |
| 353 | // first provides an Impl class that has a Matches() method, a |
| 354 | // DescribeTo() method, and a DescribeNegationTo() method. The |
| 355 | // Matches() method is usually a method template (such that it works |
| 356 | // with multiple types). Then the user creates the polymorphic |
| 357 | // matcher using MakePolymorphicMatcher(). To provide additional |
| 358 | // explanation to the match result, define a FREE function (or |
| 359 | // function template) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 360 | // |
| 361 | // void ExplainMatchResultTo(const Impl& matcher, const Value& value, |
| 362 | // ::std::ostream* os); |
| 363 | // |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 364 | // in the SAME NAME SPACE where Impl is defined. |
| 365 | // |
| 366 | // The new, recommended way to define a polymorphic matcher is to |
| 367 | // provide an Impl class that has a DescribeTo() method and a |
| 368 | // DescribeNegationTo() method, and define a FREE function (or |
| 369 | // function template) |
| 370 | // |
| 371 | // bool MatchAndExplain(const Impl& matcher, const Value& value, |
| 372 | // MatchResultListener* listener); |
| 373 | // |
| 374 | // in the SAME NAME SPACE where Impl is defined. |
| 375 | // |
| 376 | // See the definition of NotNull() for a complete example. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 377 | template <class Impl> |
| 378 | class PolymorphicMatcher { |
| 379 | public: |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 380 | explicit PolymorphicMatcher(const Impl& an_impl) : impl_(an_impl) {} |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 381 | |
zhanyong.wan | 2b43a9e | 2009-08-31 23:51:23 +0000 | [diff] [blame] | 382 | // Returns a mutable reference to the underlying matcher |
| 383 | // implementation object. |
| 384 | Impl& mutable_impl() { return impl_; } |
| 385 | |
| 386 | // Returns an immutable reference to the underlying matcher |
| 387 | // implementation object. |
| 388 | const Impl& impl() const { return impl_; } |
| 389 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 390 | template <typename T> |
| 391 | operator Matcher<T>() const { |
| 392 | return Matcher<T>(new MonomorphicImpl<T>(impl_)); |
| 393 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 394 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 395 | private: |
| 396 | template <typename T> |
| 397 | class MonomorphicImpl : public MatcherInterface<T> { |
| 398 | public: |
| 399 | explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {} |
| 400 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 401 | virtual void DescribeTo(::std::ostream* os) const { |
| 402 | impl_.DescribeTo(os); |
| 403 | } |
| 404 | |
| 405 | virtual void DescribeNegationTo(::std::ostream* os) const { |
| 406 | impl_.DescribeNegationTo(os); |
| 407 | } |
| 408 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 409 | virtual bool MatchAndExplain(T x, MatchResultListener* listener) const { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 410 | // C++ uses Argument-Dependent Look-up (aka Koenig Look-up) to |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 411 | // resolve the call to MatchAndExplain() here. This means that |
| 412 | // if there's a MatchAndExplain() function defined in the name |
| 413 | // space where class Impl is defined, it will be picked by the |
| 414 | // compiler as the better match. Otherwise the default |
| 415 | // implementation of it in ::testing::internal will be picked. |
| 416 | using ::testing::internal::MatchAndExplain; |
| 417 | return MatchAndExplain(impl_, x, listener); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 418 | } |
zhanyong.wan | 2b43a9e | 2009-08-31 23:51:23 +0000 | [diff] [blame] | 419 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 420 | private: |
| 421 | const Impl impl_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 422 | |
| 423 | GTEST_DISALLOW_ASSIGN_(MonomorphicImpl); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 424 | }; |
| 425 | |
zhanyong.wan | 2b43a9e | 2009-08-31 23:51:23 +0000 | [diff] [blame] | 426 | Impl impl_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 427 | |
| 428 | GTEST_DISALLOW_ASSIGN_(PolymorphicMatcher); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 429 | }; |
| 430 | |
| 431 | // Creates a matcher from its implementation. This is easier to use |
| 432 | // than the Matcher<T> constructor as it doesn't require you to |
| 433 | // explicitly write the template argument, e.g. |
| 434 | // |
| 435 | // MakeMatcher(foo); |
| 436 | // vs |
| 437 | // Matcher<const string&>(foo); |
| 438 | template <typename T> |
| 439 | inline Matcher<T> MakeMatcher(const MatcherInterface<T>* impl) { |
| 440 | return Matcher<T>(impl); |
| 441 | }; |
| 442 | |
| 443 | // Creates a polymorphic matcher from its implementation. This is |
| 444 | // easier to use than the PolymorphicMatcher<Impl> constructor as it |
| 445 | // doesn't require you to explicitly write the template argument, e.g. |
| 446 | // |
| 447 | // MakePolymorphicMatcher(foo); |
| 448 | // vs |
| 449 | // PolymorphicMatcher<TypeOfFoo>(foo); |
| 450 | template <class Impl> |
| 451 | inline PolymorphicMatcher<Impl> MakePolymorphicMatcher(const Impl& impl) { |
| 452 | return PolymorphicMatcher<Impl>(impl); |
| 453 | } |
| 454 | |
| 455 | // In order to be safe and clear, casting between different matcher |
| 456 | // types is done explicitly via MatcherCast<T>(m), which takes a |
| 457 | // matcher m and returns a Matcher<T>. It compiles only when T can be |
| 458 | // statically converted to the argument type of m. |
| 459 | template <typename T, typename M> |
| 460 | Matcher<T> MatcherCast(M m); |
| 461 | |
zhanyong.wan | 1849065 | 2009-05-11 18:54:08 +0000 | [diff] [blame] | 462 | // Implements SafeMatcherCast(). |
| 463 | // |
zhanyong.wan | 95b1233 | 2009-09-25 18:55:50 +0000 | [diff] [blame] | 464 | // We use an intermediate class to do the actual safe casting as Nokia's |
| 465 | // Symbian compiler cannot decide between |
| 466 | // template <T, M> ... (M) and |
| 467 | // template <T, U> ... (const Matcher<U>&) |
| 468 | // for function templates but can for member function templates. |
| 469 | template <typename T> |
| 470 | class SafeMatcherCastImpl { |
| 471 | public: |
| 472 | // This overload handles polymorphic matchers only since monomorphic |
| 473 | // matchers are handled by the next one. |
| 474 | template <typename M> |
| 475 | static inline Matcher<T> Cast(M polymorphic_matcher) { |
| 476 | return Matcher<T>(polymorphic_matcher); |
| 477 | } |
zhanyong.wan | 1849065 | 2009-05-11 18:54:08 +0000 | [diff] [blame] | 478 | |
zhanyong.wan | 95b1233 | 2009-09-25 18:55:50 +0000 | [diff] [blame] | 479 | // This overload handles monomorphic matchers. |
| 480 | // |
| 481 | // In general, if type T can be implicitly converted to type U, we can |
| 482 | // safely convert a Matcher<U> to a Matcher<T> (i.e. Matcher is |
| 483 | // contravariant): just keep a copy of the original Matcher<U>, convert the |
| 484 | // argument from type T to U, and then pass it to the underlying Matcher<U>. |
| 485 | // The only exception is when U is a reference and T is not, as the |
| 486 | // underlying Matcher<U> may be interested in the argument's address, which |
| 487 | // is not preserved in the conversion from T to U. |
| 488 | template <typename U> |
| 489 | static inline Matcher<T> Cast(const Matcher<U>& matcher) { |
| 490 | // Enforce that T can be implicitly converted to U. |
| 491 | GMOCK_COMPILE_ASSERT_((internal::ImplicitlyConvertible<T, U>::value), |
| 492 | T_must_be_implicitly_convertible_to_U); |
| 493 | // Enforce that we are not converting a non-reference type T to a reference |
| 494 | // type U. |
| 495 | GMOCK_COMPILE_ASSERT_( |
| 496 | internal::is_reference<T>::value || !internal::is_reference<U>::value, |
| 497 | cannot_convert_non_referentce_arg_to_reference); |
| 498 | // In case both T and U are arithmetic types, enforce that the |
| 499 | // conversion is not lossy. |
| 500 | typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(T)) RawT; |
| 501 | typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(U)) RawU; |
| 502 | const bool kTIsOther = GMOCK_KIND_OF_(RawT) == internal::kOther; |
| 503 | const bool kUIsOther = GMOCK_KIND_OF_(RawU) == internal::kOther; |
| 504 | GMOCK_COMPILE_ASSERT_( |
| 505 | kTIsOther || kUIsOther || |
| 506 | (internal::LosslessArithmeticConvertible<RawT, RawU>::value), |
| 507 | conversion_of_arithmetic_types_must_be_lossless); |
| 508 | return MatcherCast<T>(matcher); |
| 509 | } |
| 510 | }; |
| 511 | |
| 512 | template <typename T, typename M> |
| 513 | inline Matcher<T> SafeMatcherCast(const M& polymorphic_matcher) { |
| 514 | return SafeMatcherCastImpl<T>::Cast(polymorphic_matcher); |
zhanyong.wan | 1849065 | 2009-05-11 18:54:08 +0000 | [diff] [blame] | 515 | } |
| 516 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 517 | // A<T>() returns a matcher that matches any value of type T. |
| 518 | template <typename T> |
| 519 | Matcher<T> A(); |
| 520 | |
| 521 | // Anything inside the 'internal' namespace IS INTERNAL IMPLEMENTATION |
| 522 | // and MUST NOT BE USED IN USER CODE!!! |
| 523 | namespace internal { |
| 524 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 525 | // If the given string is not empty and os is not NULL, wraps the |
| 526 | // string inside a pair of parentheses and streams the result to os. |
| 527 | inline void StreamInParensAsNeeded(const internal::string& str, |
| 528 | ::std::ostream* os) { |
| 529 | if (!str.empty() && os != NULL) { |
| 530 | *os << " (" << str << ")"; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | |
| 534 | // An internal helper class for doing compile-time loop on a tuple's |
| 535 | // fields. |
| 536 | template <size_t N> |
| 537 | class TuplePrefix { |
| 538 | public: |
| 539 | // TuplePrefix<N>::Matches(matcher_tuple, value_tuple) returns true |
| 540 | // iff the first N fields of matcher_tuple matches the first N |
| 541 | // fields of value_tuple, respectively. |
| 542 | template <typename MatcherTuple, typename ValueTuple> |
| 543 | static bool Matches(const MatcherTuple& matcher_tuple, |
| 544 | const ValueTuple& value_tuple) { |
| 545 | using ::std::tr1::get; |
| 546 | return TuplePrefix<N - 1>::Matches(matcher_tuple, value_tuple) |
| 547 | && get<N - 1>(matcher_tuple).Matches(get<N - 1>(value_tuple)); |
| 548 | } |
| 549 | |
| 550 | // TuplePrefix<N>::DescribeMatchFailuresTo(matchers, values, os) |
| 551 | // describes failures in matching the first N fields of matchers |
| 552 | // against the first N fields of values. If there is no failure, |
| 553 | // nothing will be streamed to os. |
| 554 | template <typename MatcherTuple, typename ValueTuple> |
| 555 | static void DescribeMatchFailuresTo(const MatcherTuple& matchers, |
| 556 | const ValueTuple& values, |
| 557 | ::std::ostream* os) { |
| 558 | using ::std::tr1::tuple_element; |
| 559 | using ::std::tr1::get; |
| 560 | |
| 561 | // First, describes failures in the first N - 1 fields. |
| 562 | TuplePrefix<N - 1>::DescribeMatchFailuresTo(matchers, values, os); |
| 563 | |
| 564 | // Then describes the failure (if any) in the (N - 1)-th (0-based) |
| 565 | // field. |
| 566 | typename tuple_element<N - 1, MatcherTuple>::type matcher = |
| 567 | get<N - 1>(matchers); |
| 568 | typedef typename tuple_element<N - 1, ValueTuple>::type Value; |
| 569 | Value value = get<N - 1>(values); |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 570 | StringMatchResultListener listener; |
| 571 | if (!matcher.MatchAndExplain(value, &listener)) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 572 | // TODO(wan): include in the message the name of the parameter |
| 573 | // as used in MOCK_METHOD*() when possible. |
| 574 | *os << " Expected arg #" << N - 1 << ": "; |
| 575 | get<N - 1>(matchers).DescribeTo(os); |
| 576 | *os << "\n Actual: "; |
| 577 | // We remove the reference in type Value to prevent the |
| 578 | // universal printer from printing the address of value, which |
| 579 | // isn't interesting to the user most of the time. The |
| 580 | // matcher's ExplainMatchResultTo() method handles the case when |
| 581 | // the address is interesting. |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 582 | internal::UniversalPrinter<GMOCK_REMOVE_REFERENCE_(Value)>:: |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 583 | Print(value, os); |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 584 | |
| 585 | StreamInParensAsNeeded(listener.str(), os); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 586 | *os << "\n"; |
| 587 | } |
| 588 | } |
| 589 | }; |
| 590 | |
| 591 | // The base case. |
| 592 | template <> |
| 593 | class TuplePrefix<0> { |
| 594 | public: |
| 595 | template <typename MatcherTuple, typename ValueTuple> |
zhanyong.wan | 3fbd2dd | 2009-03-26 19:06:45 +0000 | [diff] [blame] | 596 | static bool Matches(const MatcherTuple& /* matcher_tuple */, |
| 597 | const ValueTuple& /* value_tuple */) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 598 | return true; |
| 599 | } |
| 600 | |
| 601 | template <typename MatcherTuple, typename ValueTuple> |
zhanyong.wan | 3fbd2dd | 2009-03-26 19:06:45 +0000 | [diff] [blame] | 602 | static void DescribeMatchFailuresTo(const MatcherTuple& /* matchers */, |
| 603 | const ValueTuple& /* values */, |
| 604 | ::std::ostream* /* os */) {} |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 605 | }; |
| 606 | |
| 607 | // TupleMatches(matcher_tuple, value_tuple) returns true iff all |
| 608 | // matchers in matcher_tuple match the corresponding fields in |
| 609 | // value_tuple. It is a compiler error if matcher_tuple and |
| 610 | // value_tuple have different number of fields or incompatible field |
| 611 | // types. |
| 612 | template <typename MatcherTuple, typename ValueTuple> |
| 613 | bool TupleMatches(const MatcherTuple& matcher_tuple, |
| 614 | const ValueTuple& value_tuple) { |
| 615 | using ::std::tr1::tuple_size; |
| 616 | // Makes sure that matcher_tuple and value_tuple have the same |
| 617 | // number of fields. |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 618 | GMOCK_COMPILE_ASSERT_(tuple_size<MatcherTuple>::value == |
| 619 | tuple_size<ValueTuple>::value, |
| 620 | matcher_and_value_have_different_numbers_of_fields); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 621 | return TuplePrefix<tuple_size<ValueTuple>::value>:: |
| 622 | Matches(matcher_tuple, value_tuple); |
| 623 | } |
| 624 | |
| 625 | // Describes failures in matching matchers against values. If there |
| 626 | // is no failure, nothing will be streamed to os. |
| 627 | template <typename MatcherTuple, typename ValueTuple> |
| 628 | void DescribeMatchFailureTupleTo(const MatcherTuple& matchers, |
| 629 | const ValueTuple& values, |
| 630 | ::std::ostream* os) { |
| 631 | using ::std::tr1::tuple_size; |
| 632 | TuplePrefix<tuple_size<MatcherTuple>::value>::DescribeMatchFailuresTo( |
| 633 | matchers, values, os); |
| 634 | } |
| 635 | |
| 636 | // The MatcherCastImpl class template is a helper for implementing |
| 637 | // MatcherCast(). We need this helper in order to partially |
| 638 | // specialize the implementation of MatcherCast() (C++ allows |
| 639 | // class/struct templates to be partially specialized, but not |
| 640 | // function templates.). |
| 641 | |
| 642 | // This general version is used when MatcherCast()'s argument is a |
| 643 | // polymorphic matcher (i.e. something that can be converted to a |
| 644 | // Matcher but is not one yet; for example, Eq(value)). |
| 645 | template <typename T, typename M> |
| 646 | class MatcherCastImpl { |
| 647 | public: |
| 648 | static Matcher<T> Cast(M polymorphic_matcher) { |
| 649 | return Matcher<T>(polymorphic_matcher); |
| 650 | } |
| 651 | }; |
| 652 | |
| 653 | // This more specialized version is used when MatcherCast()'s argument |
| 654 | // is already a Matcher. This only compiles when type T can be |
| 655 | // statically converted to type U. |
| 656 | template <typename T, typename U> |
| 657 | class MatcherCastImpl<T, Matcher<U> > { |
| 658 | public: |
| 659 | static Matcher<T> Cast(const Matcher<U>& source_matcher) { |
| 660 | return Matcher<T>(new Impl(source_matcher)); |
| 661 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 662 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 663 | private: |
| 664 | class Impl : public MatcherInterface<T> { |
| 665 | public: |
| 666 | explicit Impl(const Matcher<U>& source_matcher) |
| 667 | : source_matcher_(source_matcher) {} |
| 668 | |
| 669 | // We delegate the matching logic to the source matcher. |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 670 | virtual bool MatchAndExplain(T x, MatchResultListener* listener) const { |
| 671 | return source_matcher_.MatchAndExplain(static_cast<U>(x), listener); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | virtual void DescribeTo(::std::ostream* os) const { |
| 675 | source_matcher_.DescribeTo(os); |
| 676 | } |
| 677 | |
| 678 | virtual void DescribeNegationTo(::std::ostream* os) const { |
| 679 | source_matcher_.DescribeNegationTo(os); |
| 680 | } |
| 681 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 682 | private: |
| 683 | const Matcher<U> source_matcher_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 684 | |
| 685 | GTEST_DISALLOW_ASSIGN_(Impl); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 686 | }; |
| 687 | }; |
| 688 | |
| 689 | // This even more specialized version is used for efficiently casting |
| 690 | // a matcher to its own type. |
| 691 | template <typename T> |
| 692 | class MatcherCastImpl<T, Matcher<T> > { |
| 693 | public: |
| 694 | static Matcher<T> Cast(const Matcher<T>& matcher) { return matcher; } |
| 695 | }; |
| 696 | |
| 697 | // Implements A<T>(). |
| 698 | template <typename T> |
| 699 | class AnyMatcherImpl : public MatcherInterface<T> { |
| 700 | public: |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 701 | virtual bool MatchAndExplain( |
| 702 | T /* x */, MatchResultListener* /* listener */) const { return true; } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 703 | virtual void DescribeTo(::std::ostream* os) const { *os << "is anything"; } |
| 704 | virtual void DescribeNegationTo(::std::ostream* os) const { |
| 705 | // This is mostly for completeness' safe, as it's not very useful |
| 706 | // to write Not(A<bool>()). However we cannot completely rule out |
| 707 | // such a possibility, and it doesn't hurt to be prepared. |
| 708 | *os << "never matches"; |
| 709 | } |
| 710 | }; |
| 711 | |
| 712 | // Implements _, a matcher that matches any value of any |
| 713 | // type. This is a polymorphic matcher, so we need a template type |
| 714 | // conversion operator to make it appearing as a Matcher<T> for any |
| 715 | // type T. |
| 716 | class AnythingMatcher { |
| 717 | public: |
| 718 | template <typename T> |
| 719 | operator Matcher<T>() const { return A<T>(); } |
| 720 | }; |
| 721 | |
| 722 | // Implements a matcher that compares a given value with a |
| 723 | // pre-supplied value using one of the ==, <=, <, etc, operators. The |
| 724 | // two values being compared don't have to have the same type. |
| 725 | // |
| 726 | // The matcher defined here is polymorphic (for example, Eq(5) can be |
| 727 | // used to match an int, a short, a double, etc). Therefore we use |
| 728 | // a template type conversion operator in the implementation. |
| 729 | // |
| 730 | // We define this as a macro in order to eliminate duplicated source |
| 731 | // code. |
| 732 | // |
| 733 | // The following template definition assumes that the Rhs parameter is |
| 734 | // a "bare" type (i.e. neither 'const T' nor 'T&'). |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 735 | #define GMOCK_IMPLEMENT_COMPARISON_MATCHER_(name, op, relation) \ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 736 | template <typename Rhs> class name##Matcher { \ |
| 737 | public: \ |
| 738 | explicit name##Matcher(const Rhs& rhs) : rhs_(rhs) {} \ |
| 739 | template <typename Lhs> \ |
| 740 | operator Matcher<Lhs>() const { \ |
| 741 | return MakeMatcher(new Impl<Lhs>(rhs_)); \ |
| 742 | } \ |
| 743 | private: \ |
| 744 | template <typename Lhs> \ |
| 745 | class Impl : public MatcherInterface<Lhs> { \ |
| 746 | public: \ |
| 747 | explicit Impl(const Rhs& rhs) : rhs_(rhs) {} \ |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 748 | virtual bool MatchAndExplain(\ |
| 749 | Lhs lhs, MatchResultListener* /* listener */) const { \ |
| 750 | return lhs op rhs_; \ |
| 751 | } \ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 752 | virtual void DescribeTo(::std::ostream* os) const { \ |
| 753 | *os << "is " relation " "; \ |
| 754 | UniversalPrinter<Rhs>::Print(rhs_, os); \ |
| 755 | } \ |
| 756 | virtual void DescribeNegationTo(::std::ostream* os) const { \ |
| 757 | *os << "is not " relation " "; \ |
| 758 | UniversalPrinter<Rhs>::Print(rhs_, os); \ |
| 759 | } \ |
| 760 | private: \ |
| 761 | Rhs rhs_; \ |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 762 | GTEST_DISALLOW_ASSIGN_(Impl); \ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 763 | }; \ |
| 764 | Rhs rhs_; \ |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 765 | GTEST_DISALLOW_ASSIGN_(name##Matcher); \ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | // Implements Eq(v), Ge(v), Gt(v), Le(v), Lt(v), and Ne(v) |
| 769 | // respectively. |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 770 | GMOCK_IMPLEMENT_COMPARISON_MATCHER_(Eq, ==, "equal to"); |
| 771 | GMOCK_IMPLEMENT_COMPARISON_MATCHER_(Ge, >=, "greater than or equal to"); |
| 772 | GMOCK_IMPLEMENT_COMPARISON_MATCHER_(Gt, >, "greater than"); |
| 773 | GMOCK_IMPLEMENT_COMPARISON_MATCHER_(Le, <=, "less than or equal to"); |
| 774 | GMOCK_IMPLEMENT_COMPARISON_MATCHER_(Lt, <, "less than"); |
| 775 | GMOCK_IMPLEMENT_COMPARISON_MATCHER_(Ne, !=, "not equal to"); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 776 | |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 777 | #undef GMOCK_IMPLEMENT_COMPARISON_MATCHER_ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 778 | |
vladlosev | 79b8350 | 2009-11-18 00:43:37 +0000 | [diff] [blame] | 779 | // Implements the polymorphic IsNull() matcher, which matches any raw or smart |
zhanyong.wan | 2d970ee | 2009-09-24 21:41:36 +0000 | [diff] [blame] | 780 | // pointer that is NULL. |
| 781 | class IsNullMatcher { |
| 782 | public: |
vladlosev | 79b8350 | 2009-11-18 00:43:37 +0000 | [diff] [blame] | 783 | template <typename Pointer> |
| 784 | bool Matches(const Pointer& p) const { return GetRawPointer(p) == NULL; } |
zhanyong.wan | 2d970ee | 2009-09-24 21:41:36 +0000 | [diff] [blame] | 785 | |
| 786 | void DescribeTo(::std::ostream* os) const { *os << "is NULL"; } |
| 787 | void DescribeNegationTo(::std::ostream* os) const { |
| 788 | *os << "is not NULL"; |
| 789 | } |
| 790 | }; |
| 791 | |
vladlosev | 79b8350 | 2009-11-18 00:43:37 +0000 | [diff] [blame] | 792 | // Implements the polymorphic NotNull() matcher, which matches any raw or smart |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 793 | // pointer that is not NULL. |
| 794 | class NotNullMatcher { |
| 795 | public: |
vladlosev | 79b8350 | 2009-11-18 00:43:37 +0000 | [diff] [blame] | 796 | template <typename Pointer> |
| 797 | bool Matches(const Pointer& p) const { return GetRawPointer(p) != NULL; } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 798 | |
| 799 | void DescribeTo(::std::ostream* os) const { *os << "is not NULL"; } |
| 800 | void DescribeNegationTo(::std::ostream* os) const { |
| 801 | *os << "is NULL"; |
| 802 | } |
| 803 | }; |
| 804 | |
| 805 | // Ref(variable) matches any argument that is a reference to |
| 806 | // 'variable'. This matcher is polymorphic as it can match any |
| 807 | // super type of the type of 'variable'. |
| 808 | // |
| 809 | // The RefMatcher template class implements Ref(variable). It can |
| 810 | // only be instantiated with a reference type. This prevents a user |
| 811 | // from mistakenly using Ref(x) to match a non-reference function |
| 812 | // argument. For example, the following will righteously cause a |
| 813 | // compiler error: |
| 814 | // |
| 815 | // int n; |
| 816 | // Matcher<int> m1 = Ref(n); // This won't compile. |
| 817 | // Matcher<int&> m2 = Ref(n); // This will compile. |
| 818 | template <typename T> |
| 819 | class RefMatcher; |
| 820 | |
| 821 | template <typename T> |
| 822 | class RefMatcher<T&> { |
| 823 | // Google Mock is a generic framework and thus needs to support |
| 824 | // mocking any function types, including those that take non-const |
| 825 | // reference arguments. Therefore the template parameter T (and |
| 826 | // Super below) can be instantiated to either a const type or a |
| 827 | // non-const type. |
| 828 | public: |
| 829 | // RefMatcher() takes a T& instead of const T&, as we want the |
| 830 | // compiler to catch using Ref(const_value) as a matcher for a |
| 831 | // non-const reference. |
| 832 | explicit RefMatcher(T& x) : object_(x) {} // NOLINT |
| 833 | |
| 834 | template <typename Super> |
| 835 | operator Matcher<Super&>() const { |
| 836 | // By passing object_ (type T&) to Impl(), which expects a Super&, |
| 837 | // we make sure that Super is a super type of T. In particular, |
| 838 | // this catches using Ref(const_value) as a matcher for a |
| 839 | // non-const reference, as you cannot implicitly convert a const |
| 840 | // reference to a non-const reference. |
| 841 | return MakeMatcher(new Impl<Super>(object_)); |
| 842 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 843 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 844 | private: |
| 845 | template <typename Super> |
| 846 | class Impl : public MatcherInterface<Super&> { |
| 847 | public: |
| 848 | explicit Impl(Super& x) : object_(x) {} // NOLINT |
| 849 | |
| 850 | // Matches() takes a Super& (as opposed to const Super&) in |
| 851 | // order to match the interface MatcherInterface<Super&>. |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 852 | virtual bool MatchAndExplain( |
| 853 | Super& x, MatchResultListener* listener) const { |
| 854 | *listener << "is located @" << static_cast<const void*>(&x); |
| 855 | return &x == &object_; |
| 856 | } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 857 | |
| 858 | virtual void DescribeTo(::std::ostream* os) const { |
| 859 | *os << "references the variable "; |
| 860 | UniversalPrinter<Super&>::Print(object_, os); |
| 861 | } |
| 862 | |
| 863 | virtual void DescribeNegationTo(::std::ostream* os) const { |
| 864 | *os << "does not reference the variable "; |
| 865 | UniversalPrinter<Super&>::Print(object_, os); |
| 866 | } |
| 867 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 868 | private: |
| 869 | const Super& object_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 870 | |
| 871 | GTEST_DISALLOW_ASSIGN_(Impl); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 872 | }; |
| 873 | |
| 874 | T& object_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 875 | |
| 876 | GTEST_DISALLOW_ASSIGN_(RefMatcher); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 877 | }; |
| 878 | |
| 879 | // Polymorphic helper functions for narrow and wide string matchers. |
| 880 | inline bool CaseInsensitiveCStringEquals(const char* lhs, const char* rhs) { |
| 881 | return String::CaseInsensitiveCStringEquals(lhs, rhs); |
| 882 | } |
| 883 | |
| 884 | inline bool CaseInsensitiveCStringEquals(const wchar_t* lhs, |
| 885 | const wchar_t* rhs) { |
| 886 | return String::CaseInsensitiveWideCStringEquals(lhs, rhs); |
| 887 | } |
| 888 | |
| 889 | // String comparison for narrow or wide strings that can have embedded NUL |
| 890 | // characters. |
| 891 | template <typename StringType> |
| 892 | bool CaseInsensitiveStringEquals(const StringType& s1, |
| 893 | const StringType& s2) { |
| 894 | // Are the heads equal? |
| 895 | if (!CaseInsensitiveCStringEquals(s1.c_str(), s2.c_str())) { |
| 896 | return false; |
| 897 | } |
| 898 | |
| 899 | // Skip the equal heads. |
| 900 | const typename StringType::value_type nul = 0; |
| 901 | const size_t i1 = s1.find(nul), i2 = s2.find(nul); |
| 902 | |
| 903 | // Are we at the end of either s1 or s2? |
| 904 | if (i1 == StringType::npos || i2 == StringType::npos) { |
| 905 | return i1 == i2; |
| 906 | } |
| 907 | |
| 908 | // Are the tails equal? |
| 909 | return CaseInsensitiveStringEquals(s1.substr(i1 + 1), s2.substr(i2 + 1)); |
| 910 | } |
| 911 | |
| 912 | // String matchers. |
| 913 | |
| 914 | // Implements equality-based string matchers like StrEq, StrCaseNe, and etc. |
| 915 | template <typename StringType> |
| 916 | class StrEqualityMatcher { |
| 917 | public: |
| 918 | typedef typename StringType::const_pointer ConstCharPointer; |
| 919 | |
| 920 | StrEqualityMatcher(const StringType& str, bool expect_eq, |
| 921 | bool case_sensitive) |
| 922 | : string_(str), expect_eq_(expect_eq), case_sensitive_(case_sensitive) {} |
| 923 | |
| 924 | // When expect_eq_ is true, returns true iff s is equal to string_; |
| 925 | // otherwise returns true iff s is not equal to string_. |
| 926 | bool Matches(ConstCharPointer s) const { |
| 927 | if (s == NULL) { |
| 928 | return !expect_eq_; |
| 929 | } |
| 930 | return Matches(StringType(s)); |
| 931 | } |
| 932 | |
| 933 | bool Matches(const StringType& s) const { |
| 934 | const bool eq = case_sensitive_ ? s == string_ : |
| 935 | CaseInsensitiveStringEquals(s, string_); |
| 936 | return expect_eq_ == eq; |
| 937 | } |
| 938 | |
| 939 | void DescribeTo(::std::ostream* os) const { |
| 940 | DescribeToHelper(expect_eq_, os); |
| 941 | } |
| 942 | |
| 943 | void DescribeNegationTo(::std::ostream* os) const { |
| 944 | DescribeToHelper(!expect_eq_, os); |
| 945 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 946 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 947 | private: |
| 948 | void DescribeToHelper(bool expect_eq, ::std::ostream* os) const { |
| 949 | *os << "is "; |
| 950 | if (!expect_eq) { |
| 951 | *os << "not "; |
| 952 | } |
| 953 | *os << "equal to "; |
| 954 | if (!case_sensitive_) { |
| 955 | *os << "(ignoring case) "; |
| 956 | } |
| 957 | UniversalPrinter<StringType>::Print(string_, os); |
| 958 | } |
| 959 | |
| 960 | const StringType string_; |
| 961 | const bool expect_eq_; |
| 962 | const bool case_sensitive_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 963 | |
| 964 | GTEST_DISALLOW_ASSIGN_(StrEqualityMatcher); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 965 | }; |
| 966 | |
| 967 | // Implements the polymorphic HasSubstr(substring) matcher, which |
| 968 | // can be used as a Matcher<T> as long as T can be converted to a |
| 969 | // string. |
| 970 | template <typename StringType> |
| 971 | class HasSubstrMatcher { |
| 972 | public: |
| 973 | typedef typename StringType::const_pointer ConstCharPointer; |
| 974 | |
| 975 | explicit HasSubstrMatcher(const StringType& substring) |
| 976 | : substring_(substring) {} |
| 977 | |
| 978 | // These overloaded methods allow HasSubstr(substring) to be used as a |
| 979 | // Matcher<T> as long as T can be converted to string. Returns true |
| 980 | // iff s contains substring_ as a substring. |
| 981 | bool Matches(ConstCharPointer s) const { |
| 982 | return s != NULL && Matches(StringType(s)); |
| 983 | } |
| 984 | |
| 985 | bool Matches(const StringType& s) const { |
| 986 | return s.find(substring_) != StringType::npos; |
| 987 | } |
| 988 | |
| 989 | // Describes what this matcher matches. |
| 990 | void DescribeTo(::std::ostream* os) const { |
| 991 | *os << "has substring "; |
| 992 | UniversalPrinter<StringType>::Print(substring_, os); |
| 993 | } |
| 994 | |
| 995 | void DescribeNegationTo(::std::ostream* os) const { |
| 996 | *os << "has no substring "; |
| 997 | UniversalPrinter<StringType>::Print(substring_, os); |
| 998 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 999 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1000 | private: |
| 1001 | const StringType substring_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1002 | |
| 1003 | GTEST_DISALLOW_ASSIGN_(HasSubstrMatcher); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1004 | }; |
| 1005 | |
| 1006 | // Implements the polymorphic StartsWith(substring) matcher, which |
| 1007 | // can be used as a Matcher<T> as long as T can be converted to a |
| 1008 | // string. |
| 1009 | template <typename StringType> |
| 1010 | class StartsWithMatcher { |
| 1011 | public: |
| 1012 | typedef typename StringType::const_pointer ConstCharPointer; |
| 1013 | |
| 1014 | explicit StartsWithMatcher(const StringType& prefix) : prefix_(prefix) { |
| 1015 | } |
| 1016 | |
| 1017 | // These overloaded methods allow StartsWith(prefix) to be used as a |
| 1018 | // Matcher<T> as long as T can be converted to string. Returns true |
| 1019 | // iff s starts with prefix_. |
| 1020 | bool Matches(ConstCharPointer s) const { |
| 1021 | return s != NULL && Matches(StringType(s)); |
| 1022 | } |
| 1023 | |
| 1024 | bool Matches(const StringType& s) const { |
| 1025 | return s.length() >= prefix_.length() && |
| 1026 | s.substr(0, prefix_.length()) == prefix_; |
| 1027 | } |
| 1028 | |
| 1029 | void DescribeTo(::std::ostream* os) const { |
| 1030 | *os << "starts with "; |
| 1031 | UniversalPrinter<StringType>::Print(prefix_, os); |
| 1032 | } |
| 1033 | |
| 1034 | void DescribeNegationTo(::std::ostream* os) const { |
| 1035 | *os << "doesn't start with "; |
| 1036 | UniversalPrinter<StringType>::Print(prefix_, os); |
| 1037 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1038 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1039 | private: |
| 1040 | const StringType prefix_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1041 | |
| 1042 | GTEST_DISALLOW_ASSIGN_(StartsWithMatcher); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1043 | }; |
| 1044 | |
| 1045 | // Implements the polymorphic EndsWith(substring) matcher, which |
| 1046 | // can be used as a Matcher<T> as long as T can be converted to a |
| 1047 | // string. |
| 1048 | template <typename StringType> |
| 1049 | class EndsWithMatcher { |
| 1050 | public: |
| 1051 | typedef typename StringType::const_pointer ConstCharPointer; |
| 1052 | |
| 1053 | explicit EndsWithMatcher(const StringType& suffix) : suffix_(suffix) {} |
| 1054 | |
| 1055 | // These overloaded methods allow EndsWith(suffix) to be used as a |
| 1056 | // Matcher<T> as long as T can be converted to string. Returns true |
| 1057 | // iff s ends with suffix_. |
| 1058 | bool Matches(ConstCharPointer s) const { |
| 1059 | return s != NULL && Matches(StringType(s)); |
| 1060 | } |
| 1061 | |
| 1062 | bool Matches(const StringType& s) const { |
| 1063 | return s.length() >= suffix_.length() && |
| 1064 | s.substr(s.length() - suffix_.length()) == suffix_; |
| 1065 | } |
| 1066 | |
| 1067 | void DescribeTo(::std::ostream* os) const { |
| 1068 | *os << "ends with "; |
| 1069 | UniversalPrinter<StringType>::Print(suffix_, os); |
| 1070 | } |
| 1071 | |
| 1072 | void DescribeNegationTo(::std::ostream* os) const { |
| 1073 | *os << "doesn't end with "; |
| 1074 | UniversalPrinter<StringType>::Print(suffix_, os); |
| 1075 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1076 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1077 | private: |
| 1078 | const StringType suffix_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1079 | |
| 1080 | GTEST_DISALLOW_ASSIGN_(EndsWithMatcher); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1081 | }; |
| 1082 | |
| 1083 | #if GMOCK_HAS_REGEX |
| 1084 | |
| 1085 | // Implements polymorphic matchers MatchesRegex(regex) and |
| 1086 | // ContainsRegex(regex), which can be used as a Matcher<T> as long as |
| 1087 | // T can be converted to a string. |
| 1088 | class MatchesRegexMatcher { |
| 1089 | public: |
| 1090 | MatchesRegexMatcher(const RE* regex, bool full_match) |
| 1091 | : regex_(regex), full_match_(full_match) {} |
| 1092 | |
| 1093 | // These overloaded methods allow MatchesRegex(regex) to be used as |
| 1094 | // a Matcher<T> as long as T can be converted to string. Returns |
| 1095 | // true iff s matches regular expression regex. When full_match_ is |
| 1096 | // true, a full match is done; otherwise a partial match is done. |
| 1097 | bool Matches(const char* s) const { |
| 1098 | return s != NULL && Matches(internal::string(s)); |
| 1099 | } |
| 1100 | |
| 1101 | bool Matches(const internal::string& s) const { |
| 1102 | return full_match_ ? RE::FullMatch(s, *regex_) : |
| 1103 | RE::PartialMatch(s, *regex_); |
| 1104 | } |
| 1105 | |
| 1106 | void DescribeTo(::std::ostream* os) const { |
| 1107 | *os << (full_match_ ? "matches" : "contains") |
| 1108 | << " regular expression "; |
| 1109 | UniversalPrinter<internal::string>::Print(regex_->pattern(), os); |
| 1110 | } |
| 1111 | |
| 1112 | void DescribeNegationTo(::std::ostream* os) const { |
| 1113 | *os << "doesn't " << (full_match_ ? "match" : "contain") |
| 1114 | << " regular expression "; |
| 1115 | UniversalPrinter<internal::string>::Print(regex_->pattern(), os); |
| 1116 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1117 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1118 | private: |
| 1119 | const internal::linked_ptr<const RE> regex_; |
| 1120 | const bool full_match_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1121 | |
| 1122 | GTEST_DISALLOW_ASSIGN_(MatchesRegexMatcher); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1123 | }; |
| 1124 | |
| 1125 | #endif // GMOCK_HAS_REGEX |
| 1126 | |
| 1127 | // Implements a matcher that compares the two fields of a 2-tuple |
| 1128 | // using one of the ==, <=, <, etc, operators. The two fields being |
| 1129 | // compared don't have to have the same type. |
| 1130 | // |
| 1131 | // The matcher defined here is polymorphic (for example, Eq() can be |
| 1132 | // used to match a tuple<int, short>, a tuple<const long&, double>, |
| 1133 | // etc). Therefore we use a template type conversion operator in the |
| 1134 | // implementation. |
| 1135 | // |
| 1136 | // We define this as a macro in order to eliminate duplicated source |
| 1137 | // code. |
zhanyong.wan | 2661c68 | 2009-06-09 05:42:12 +0000 | [diff] [blame] | 1138 | #define GMOCK_IMPLEMENT_COMPARISON2_MATCHER_(name, op) \ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1139 | class name##2Matcher { \ |
| 1140 | public: \ |
| 1141 | template <typename T1, typename T2> \ |
| 1142 | operator Matcher<const ::std::tr1::tuple<T1, T2>&>() const { \ |
| 1143 | return MakeMatcher(new Impl<T1, T2>); \ |
| 1144 | } \ |
| 1145 | private: \ |
| 1146 | template <typename T1, typename T2> \ |
| 1147 | class Impl : public MatcherInterface<const ::std::tr1::tuple<T1, T2>&> { \ |
| 1148 | public: \ |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 1149 | virtual bool MatchAndExplain( \ |
| 1150 | const ::std::tr1::tuple<T1, T2>& args, \ |
| 1151 | MatchResultListener* /* listener */) const { \ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1152 | return ::std::tr1::get<0>(args) op ::std::tr1::get<1>(args); \ |
| 1153 | } \ |
| 1154 | virtual void DescribeTo(::std::ostream* os) const { \ |
zhanyong.wan | 2661c68 | 2009-06-09 05:42:12 +0000 | [diff] [blame] | 1155 | *os << "are a pair (x, y) where x " #op " y"; \ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1156 | } \ |
| 1157 | virtual void DescribeNegationTo(::std::ostream* os) const { \ |
zhanyong.wan | 2661c68 | 2009-06-09 05:42:12 +0000 | [diff] [blame] | 1158 | *os << "are a pair (x, y) where x " #op " y is false"; \ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1159 | } \ |
| 1160 | }; \ |
| 1161 | } |
| 1162 | |
| 1163 | // Implements Eq(), Ge(), Gt(), Le(), Lt(), and Ne() respectively. |
zhanyong.wan | 2661c68 | 2009-06-09 05:42:12 +0000 | [diff] [blame] | 1164 | GMOCK_IMPLEMENT_COMPARISON2_MATCHER_(Eq, ==); |
| 1165 | GMOCK_IMPLEMENT_COMPARISON2_MATCHER_(Ge, >=); |
| 1166 | GMOCK_IMPLEMENT_COMPARISON2_MATCHER_(Gt, >); |
| 1167 | GMOCK_IMPLEMENT_COMPARISON2_MATCHER_(Le, <=); |
| 1168 | GMOCK_IMPLEMENT_COMPARISON2_MATCHER_(Lt, <); |
| 1169 | GMOCK_IMPLEMENT_COMPARISON2_MATCHER_(Ne, !=); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1170 | |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 1171 | #undef GMOCK_IMPLEMENT_COMPARISON2_MATCHER_ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1172 | |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 1173 | // Implements the Not(...) matcher for a particular argument type T. |
| 1174 | // We do not nest it inside the NotMatcher class template, as that |
| 1175 | // will prevent different instantiations of NotMatcher from sharing |
| 1176 | // the same NotMatcherImpl<T> class. |
| 1177 | template <typename T> |
| 1178 | class NotMatcherImpl : public MatcherInterface<T> { |
| 1179 | public: |
| 1180 | explicit NotMatcherImpl(const Matcher<T>& matcher) |
| 1181 | : matcher_(matcher) {} |
| 1182 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 1183 | virtual bool MatchAndExplain(T x, MatchResultListener* listener) const { |
| 1184 | return !matcher_.MatchAndExplain(x, listener); |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | virtual void DescribeTo(::std::ostream* os) const { |
| 1188 | matcher_.DescribeNegationTo(os); |
| 1189 | } |
| 1190 | |
| 1191 | virtual void DescribeNegationTo(::std::ostream* os) const { |
| 1192 | matcher_.DescribeTo(os); |
| 1193 | } |
| 1194 | |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 1195 | private: |
| 1196 | const Matcher<T> matcher_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1197 | |
| 1198 | GTEST_DISALLOW_ASSIGN_(NotMatcherImpl); |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 1199 | }; |
| 1200 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1201 | // Implements the Not(m) matcher, which matches a value that doesn't |
| 1202 | // match matcher m. |
| 1203 | template <typename InnerMatcher> |
| 1204 | class NotMatcher { |
| 1205 | public: |
| 1206 | explicit NotMatcher(InnerMatcher matcher) : matcher_(matcher) {} |
| 1207 | |
| 1208 | // This template type conversion operator allows Not(m) to be used |
| 1209 | // to match any type m can match. |
| 1210 | template <typename T> |
| 1211 | operator Matcher<T>() const { |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 1212 | return Matcher<T>(new NotMatcherImpl<T>(SafeMatcherCast<T>(matcher_))); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1213 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1214 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1215 | private: |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1216 | InnerMatcher matcher_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1217 | |
| 1218 | GTEST_DISALLOW_ASSIGN_(NotMatcher); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1219 | }; |
| 1220 | |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 1221 | // Implements the AllOf(m1, m2) matcher for a particular argument type |
| 1222 | // T. We do not nest it inside the BothOfMatcher class template, as |
| 1223 | // that will prevent different instantiations of BothOfMatcher from |
| 1224 | // sharing the same BothOfMatcherImpl<T> class. |
| 1225 | template <typename T> |
| 1226 | class BothOfMatcherImpl : public MatcherInterface<T> { |
| 1227 | public: |
| 1228 | BothOfMatcherImpl(const Matcher<T>& matcher1, const Matcher<T>& matcher2) |
| 1229 | : matcher1_(matcher1), matcher2_(matcher2) {} |
| 1230 | |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 1231 | virtual void DescribeTo(::std::ostream* os) const { |
| 1232 | *os << "("; |
| 1233 | matcher1_.DescribeTo(os); |
| 1234 | *os << ") and ("; |
| 1235 | matcher2_.DescribeTo(os); |
| 1236 | *os << ")"; |
| 1237 | } |
| 1238 | |
| 1239 | virtual void DescribeNegationTo(::std::ostream* os) const { |
| 1240 | *os << "not "; |
| 1241 | DescribeTo(os); |
| 1242 | } |
| 1243 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 1244 | virtual bool MatchAndExplain(T x, MatchResultListener* listener) const { |
| 1245 | // If either matcher1_ or matcher2_ doesn't match x, we only need |
| 1246 | // to explain why one of them fails. |
| 1247 | StringMatchResultListener listener1; |
| 1248 | if (!matcher1_.MatchAndExplain(x, &listener1)) { |
| 1249 | *listener << listener1.str(); |
| 1250 | return false; |
| 1251 | } |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 1252 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 1253 | StringMatchResultListener listener2; |
| 1254 | if (!matcher2_.MatchAndExplain(x, &listener2)) { |
| 1255 | *listener << listener2.str(); |
| 1256 | return false; |
| 1257 | } |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 1258 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 1259 | // Otherwise we need to explain why *both* of them match. |
| 1260 | const internal::string s1 = listener1.str(); |
| 1261 | const internal::string s2 = listener2.str(); |
| 1262 | |
| 1263 | if (s1 == "") { |
| 1264 | *listener << s2; |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 1265 | } else { |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 1266 | *listener << s1; |
| 1267 | if (s2 != "") { |
| 1268 | *listener << "; " << s2; |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 1269 | } |
| 1270 | } |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 1271 | return true; |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 1272 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1273 | |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 1274 | private: |
| 1275 | const Matcher<T> matcher1_; |
| 1276 | const Matcher<T> matcher2_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1277 | |
| 1278 | GTEST_DISALLOW_ASSIGN_(BothOfMatcherImpl); |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 1279 | }; |
| 1280 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1281 | // Used for implementing the AllOf(m_1, ..., m_n) matcher, which |
| 1282 | // matches a value that matches all of the matchers m_1, ..., and m_n. |
| 1283 | template <typename Matcher1, typename Matcher2> |
| 1284 | class BothOfMatcher { |
| 1285 | public: |
| 1286 | BothOfMatcher(Matcher1 matcher1, Matcher2 matcher2) |
| 1287 | : matcher1_(matcher1), matcher2_(matcher2) {} |
| 1288 | |
| 1289 | // This template type conversion operator allows a |
| 1290 | // BothOfMatcher<Matcher1, Matcher2> object to match any type that |
| 1291 | // both Matcher1 and Matcher2 can match. |
| 1292 | template <typename T> |
| 1293 | operator Matcher<T>() const { |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 1294 | return Matcher<T>(new BothOfMatcherImpl<T>(SafeMatcherCast<T>(matcher1_), |
| 1295 | SafeMatcherCast<T>(matcher2_))); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1296 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1297 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1298 | private: |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 1299 | Matcher1 matcher1_; |
| 1300 | Matcher2 matcher2_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1301 | |
| 1302 | GTEST_DISALLOW_ASSIGN_(BothOfMatcher); |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 1303 | }; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1304 | |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 1305 | // Implements the AnyOf(m1, m2) matcher for a particular argument type |
| 1306 | // T. We do not nest it inside the AnyOfMatcher class template, as |
| 1307 | // that will prevent different instantiations of AnyOfMatcher from |
| 1308 | // sharing the same EitherOfMatcherImpl<T> class. |
| 1309 | template <typename T> |
| 1310 | class EitherOfMatcherImpl : public MatcherInterface<T> { |
| 1311 | public: |
| 1312 | EitherOfMatcherImpl(const Matcher<T>& matcher1, const Matcher<T>& matcher2) |
| 1313 | : matcher1_(matcher1), matcher2_(matcher2) {} |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1314 | |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 1315 | virtual void DescribeTo(::std::ostream* os) const { |
| 1316 | *os << "("; |
| 1317 | matcher1_.DescribeTo(os); |
| 1318 | *os << ") or ("; |
| 1319 | matcher2_.DescribeTo(os); |
| 1320 | *os << ")"; |
| 1321 | } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1322 | |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 1323 | virtual void DescribeNegationTo(::std::ostream* os) const { |
| 1324 | *os << "not "; |
| 1325 | DescribeTo(os); |
| 1326 | } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1327 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 1328 | virtual bool MatchAndExplain(T x, MatchResultListener* listener) const { |
| 1329 | // If either matcher1_ or matcher2_ matches x, we just need to |
| 1330 | // explain why *one* of them matches. |
| 1331 | StringMatchResultListener listener1; |
| 1332 | if (matcher1_.MatchAndExplain(x, &listener1)) { |
| 1333 | *listener << listener1.str(); |
| 1334 | return true; |
| 1335 | } |
| 1336 | |
| 1337 | StringMatchResultListener listener2; |
| 1338 | if (matcher2_.MatchAndExplain(x, &listener2)) { |
| 1339 | *listener << listener2.str(); |
| 1340 | return true; |
| 1341 | } |
| 1342 | |
| 1343 | // Otherwise we need to explain why *both* of them fail. |
| 1344 | const internal::string s1 = listener1.str(); |
| 1345 | const internal::string s2 = listener2.str(); |
| 1346 | |
| 1347 | if (s1 == "") { |
| 1348 | *listener << s2; |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 1349 | } else { |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 1350 | *listener << s1; |
| 1351 | if (s2 != "") { |
| 1352 | *listener << "; " << s2; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1353 | } |
| 1354 | } |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 1355 | return false; |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 1356 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1357 | |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 1358 | private: |
| 1359 | const Matcher<T> matcher1_; |
| 1360 | const Matcher<T> matcher2_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1361 | |
| 1362 | GTEST_DISALLOW_ASSIGN_(EitherOfMatcherImpl); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1363 | }; |
| 1364 | |
| 1365 | // Used for implementing the AnyOf(m_1, ..., m_n) matcher, which |
| 1366 | // matches a value that matches at least one of the matchers m_1, ..., |
| 1367 | // and m_n. |
| 1368 | template <typename Matcher1, typename Matcher2> |
| 1369 | class EitherOfMatcher { |
| 1370 | public: |
| 1371 | EitherOfMatcher(Matcher1 matcher1, Matcher2 matcher2) |
| 1372 | : matcher1_(matcher1), matcher2_(matcher2) {} |
| 1373 | |
| 1374 | // This template type conversion operator allows a |
| 1375 | // EitherOfMatcher<Matcher1, Matcher2> object to match any type that |
| 1376 | // both Matcher1 and Matcher2 can match. |
| 1377 | template <typename T> |
| 1378 | operator Matcher<T>() const { |
zhanyong.wan | 16cf473 | 2009-05-14 20:55:30 +0000 | [diff] [blame] | 1379 | return Matcher<T>(new EitherOfMatcherImpl<T>( |
| 1380 | SafeMatcherCast<T>(matcher1_), SafeMatcherCast<T>(matcher2_))); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1381 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1382 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1383 | private: |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1384 | Matcher1 matcher1_; |
| 1385 | Matcher2 matcher2_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1386 | |
| 1387 | GTEST_DISALLOW_ASSIGN_(EitherOfMatcher); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1388 | }; |
| 1389 | |
| 1390 | // Used for implementing Truly(pred), which turns a predicate into a |
| 1391 | // matcher. |
| 1392 | template <typename Predicate> |
| 1393 | class TrulyMatcher { |
| 1394 | public: |
| 1395 | explicit TrulyMatcher(Predicate pred) : predicate_(pred) {} |
| 1396 | |
| 1397 | // This method template allows Truly(pred) to be used as a matcher |
| 1398 | // for type T where T is the argument type of predicate 'pred'. The |
| 1399 | // argument is passed by reference as the predicate may be |
| 1400 | // interested in the address of the argument. |
| 1401 | template <typename T> |
zhanyong.wan | 16cf473 | 2009-05-14 20:55:30 +0000 | [diff] [blame] | 1402 | bool Matches(T& x) const { // NOLINT |
zhanyong.wan | 652540a | 2009-02-23 23:37:29 +0000 | [diff] [blame] | 1403 | #if GTEST_OS_WINDOWS |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1404 | // MSVC warns about converting a value into bool (warning 4800). |
| 1405 | #pragma warning(push) // Saves the current warning state. |
| 1406 | #pragma warning(disable:4800) // Temporarily disables warning 4800. |
| 1407 | #endif // GTEST_OS_WINDOWS |
| 1408 | return predicate_(x); |
zhanyong.wan | 652540a | 2009-02-23 23:37:29 +0000 | [diff] [blame] | 1409 | #if GTEST_OS_WINDOWS |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1410 | #pragma warning(pop) // Restores the warning state. |
| 1411 | #endif // GTEST_OS_WINDOWS |
| 1412 | } |
| 1413 | |
| 1414 | void DescribeTo(::std::ostream* os) const { |
| 1415 | *os << "satisfies the given predicate"; |
| 1416 | } |
| 1417 | |
| 1418 | void DescribeNegationTo(::std::ostream* os) const { |
| 1419 | *os << "doesn't satisfy the given predicate"; |
| 1420 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1421 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1422 | private: |
| 1423 | Predicate predicate_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1424 | |
| 1425 | GTEST_DISALLOW_ASSIGN_(TrulyMatcher); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1426 | }; |
| 1427 | |
| 1428 | // Used for implementing Matches(matcher), which turns a matcher into |
| 1429 | // a predicate. |
| 1430 | template <typename M> |
| 1431 | class MatcherAsPredicate { |
| 1432 | public: |
| 1433 | explicit MatcherAsPredicate(M matcher) : matcher_(matcher) {} |
| 1434 | |
| 1435 | // This template operator() allows Matches(m) to be used as a |
| 1436 | // predicate on type T where m is a matcher on type T. |
| 1437 | // |
| 1438 | // The argument x is passed by reference instead of by value, as |
| 1439 | // some matcher may be interested in its address (e.g. as in |
| 1440 | // Matches(Ref(n))(x)). |
| 1441 | template <typename T> |
| 1442 | bool operator()(const T& x) const { |
| 1443 | // We let matcher_ commit to a particular type here instead of |
| 1444 | // when the MatcherAsPredicate object was constructed. This |
| 1445 | // allows us to write Matches(m) where m is a polymorphic matcher |
| 1446 | // (e.g. Eq(5)). |
| 1447 | // |
| 1448 | // If we write Matcher<T>(matcher_).Matches(x) here, it won't |
| 1449 | // compile when matcher_ has type Matcher<const T&>; if we write |
| 1450 | // Matcher<const T&>(matcher_).Matches(x) here, it won't compile |
| 1451 | // when matcher_ has type Matcher<T>; if we just write |
| 1452 | // matcher_.Matches(x), it won't compile when matcher_ is |
| 1453 | // polymorphic, e.g. Eq(5). |
| 1454 | // |
| 1455 | // MatcherCast<const T&>() is necessary for making the code work |
| 1456 | // in all of the above situations. |
| 1457 | return MatcherCast<const T&>(matcher_).Matches(x); |
| 1458 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1459 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1460 | private: |
| 1461 | M matcher_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1462 | |
| 1463 | GTEST_DISALLOW_ASSIGN_(MatcherAsPredicate); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1464 | }; |
| 1465 | |
| 1466 | // For implementing ASSERT_THAT() and EXPECT_THAT(). The template |
| 1467 | // argument M must be a type that can be converted to a matcher. |
| 1468 | template <typename M> |
| 1469 | class PredicateFormatterFromMatcher { |
| 1470 | public: |
| 1471 | explicit PredicateFormatterFromMatcher(const M& m) : matcher_(m) {} |
| 1472 | |
| 1473 | // This template () operator allows a PredicateFormatterFromMatcher |
| 1474 | // object to act as a predicate-formatter suitable for using with |
| 1475 | // Google Test's EXPECT_PRED_FORMAT1() macro. |
| 1476 | template <typename T> |
| 1477 | AssertionResult operator()(const char* value_text, const T& x) const { |
| 1478 | // We convert matcher_ to a Matcher<const T&> *now* instead of |
| 1479 | // when the PredicateFormatterFromMatcher object was constructed, |
| 1480 | // as matcher_ may be polymorphic (e.g. NotNull()) and we won't |
| 1481 | // know which type to instantiate it to until we actually see the |
| 1482 | // type of x here. |
| 1483 | // |
| 1484 | // We write MatcherCast<const T&>(matcher_) instead of |
| 1485 | // Matcher<const T&>(matcher_), as the latter won't compile when |
| 1486 | // matcher_ has type Matcher<T> (e.g. An<int>()). |
| 1487 | const Matcher<const T&> matcher = MatcherCast<const T&>(matcher_); |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 1488 | StringMatchResultListener listener; |
| 1489 | if (matcher.MatchAndExplain(x, &listener)) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1490 | return AssertionSuccess(); |
| 1491 | } else { |
| 1492 | ::std::stringstream ss; |
| 1493 | ss << "Value of: " << value_text << "\n" |
| 1494 | << "Expected: "; |
| 1495 | matcher.DescribeTo(&ss); |
| 1496 | ss << "\n Actual: "; |
| 1497 | UniversalPrinter<T>::Print(x, &ss); |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 1498 | StreamInParensAsNeeded(listener.str(), &ss); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1499 | return AssertionFailure(Message() << ss.str()); |
| 1500 | } |
| 1501 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1502 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1503 | private: |
| 1504 | const M matcher_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1505 | |
| 1506 | GTEST_DISALLOW_ASSIGN_(PredicateFormatterFromMatcher); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1507 | }; |
| 1508 | |
| 1509 | // A helper function for converting a matcher to a predicate-formatter |
| 1510 | // without the user needing to explicitly write the type. This is |
| 1511 | // used for implementing ASSERT_THAT() and EXPECT_THAT(). |
| 1512 | template <typename M> |
| 1513 | inline PredicateFormatterFromMatcher<M> |
| 1514 | MakePredicateFormatterFromMatcher(const M& matcher) { |
| 1515 | return PredicateFormatterFromMatcher<M>(matcher); |
| 1516 | } |
| 1517 | |
| 1518 | // Implements the polymorphic floating point equality matcher, which |
| 1519 | // matches two float values using ULP-based approximation. The |
| 1520 | // template is meant to be instantiated with FloatType being either |
| 1521 | // float or double. |
| 1522 | template <typename FloatType> |
| 1523 | class FloatingEqMatcher { |
| 1524 | public: |
| 1525 | // Constructor for FloatingEqMatcher. |
| 1526 | // The matcher's input will be compared with rhs. The matcher treats two |
| 1527 | // NANs as equal if nan_eq_nan is true. Otherwise, under IEEE standards, |
| 1528 | // equality comparisons between NANs will always return false. |
| 1529 | FloatingEqMatcher(FloatType rhs, bool nan_eq_nan) : |
| 1530 | rhs_(rhs), nan_eq_nan_(nan_eq_nan) {} |
| 1531 | |
| 1532 | // Implements floating point equality matcher as a Matcher<T>. |
| 1533 | template <typename T> |
| 1534 | class Impl : public MatcherInterface<T> { |
| 1535 | public: |
| 1536 | Impl(FloatType rhs, bool nan_eq_nan) : |
| 1537 | rhs_(rhs), nan_eq_nan_(nan_eq_nan) {} |
| 1538 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 1539 | virtual bool MatchAndExplain(T value, |
| 1540 | MatchResultListener* /* listener */) const { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1541 | const FloatingPoint<FloatType> lhs(value), rhs(rhs_); |
| 1542 | |
| 1543 | // Compares NaNs first, if nan_eq_nan_ is true. |
| 1544 | if (nan_eq_nan_ && lhs.is_nan()) { |
| 1545 | return rhs.is_nan(); |
| 1546 | } |
| 1547 | |
| 1548 | return lhs.AlmostEquals(rhs); |
| 1549 | } |
| 1550 | |
| 1551 | virtual void DescribeTo(::std::ostream* os) const { |
| 1552 | // os->precision() returns the previously set precision, which we |
| 1553 | // store to restore the ostream to its original configuration |
| 1554 | // after outputting. |
| 1555 | const ::std::streamsize old_precision = os->precision( |
| 1556 | ::std::numeric_limits<FloatType>::digits10 + 2); |
| 1557 | if (FloatingPoint<FloatType>(rhs_).is_nan()) { |
| 1558 | if (nan_eq_nan_) { |
| 1559 | *os << "is NaN"; |
| 1560 | } else { |
| 1561 | *os << "never matches"; |
| 1562 | } |
| 1563 | } else { |
| 1564 | *os << "is approximately " << rhs_; |
| 1565 | } |
| 1566 | os->precision(old_precision); |
| 1567 | } |
| 1568 | |
| 1569 | virtual void DescribeNegationTo(::std::ostream* os) const { |
| 1570 | // As before, get original precision. |
| 1571 | const ::std::streamsize old_precision = os->precision( |
| 1572 | ::std::numeric_limits<FloatType>::digits10 + 2); |
| 1573 | if (FloatingPoint<FloatType>(rhs_).is_nan()) { |
| 1574 | if (nan_eq_nan_) { |
| 1575 | *os << "is not NaN"; |
| 1576 | } else { |
| 1577 | *os << "is anything"; |
| 1578 | } |
| 1579 | } else { |
| 1580 | *os << "is not approximately " << rhs_; |
| 1581 | } |
| 1582 | // Restore original precision. |
| 1583 | os->precision(old_precision); |
| 1584 | } |
| 1585 | |
| 1586 | private: |
| 1587 | const FloatType rhs_; |
| 1588 | const bool nan_eq_nan_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1589 | |
| 1590 | GTEST_DISALLOW_ASSIGN_(Impl); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1591 | }; |
| 1592 | |
| 1593 | // The following 3 type conversion operators allow FloatEq(rhs) and |
| 1594 | // NanSensitiveFloatEq(rhs) to be used as a Matcher<float>, a |
| 1595 | // Matcher<const float&>, or a Matcher<float&>, but nothing else. |
| 1596 | // (While Google's C++ coding style doesn't allow arguments passed |
| 1597 | // by non-const reference, we may see them in code not conforming to |
| 1598 | // the style. Therefore Google Mock needs to support them.) |
| 1599 | operator Matcher<FloatType>() const { |
| 1600 | return MakeMatcher(new Impl<FloatType>(rhs_, nan_eq_nan_)); |
| 1601 | } |
| 1602 | |
| 1603 | operator Matcher<const FloatType&>() const { |
| 1604 | return MakeMatcher(new Impl<const FloatType&>(rhs_, nan_eq_nan_)); |
| 1605 | } |
| 1606 | |
| 1607 | operator Matcher<FloatType&>() const { |
| 1608 | return MakeMatcher(new Impl<FloatType&>(rhs_, nan_eq_nan_)); |
| 1609 | } |
| 1610 | private: |
| 1611 | const FloatType rhs_; |
| 1612 | const bool nan_eq_nan_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1613 | |
| 1614 | GTEST_DISALLOW_ASSIGN_(FloatingEqMatcher); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1615 | }; |
| 1616 | |
| 1617 | // Implements the Pointee(m) matcher for matching a pointer whose |
| 1618 | // pointee matches matcher m. The pointer can be either raw or smart. |
| 1619 | template <typename InnerMatcher> |
| 1620 | class PointeeMatcher { |
| 1621 | public: |
| 1622 | explicit PointeeMatcher(const InnerMatcher& matcher) : matcher_(matcher) {} |
| 1623 | |
| 1624 | // This type conversion operator template allows Pointee(m) to be |
| 1625 | // used as a matcher for any pointer type whose pointee type is |
| 1626 | // compatible with the inner matcher, where type Pointer can be |
| 1627 | // either a raw pointer or a smart pointer. |
| 1628 | // |
| 1629 | // The reason we do this instead of relying on |
| 1630 | // MakePolymorphicMatcher() is that the latter is not flexible |
| 1631 | // enough for implementing the DescribeTo() method of Pointee(). |
| 1632 | template <typename Pointer> |
| 1633 | operator Matcher<Pointer>() const { |
| 1634 | return MakeMatcher(new Impl<Pointer>(matcher_)); |
| 1635 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1636 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1637 | private: |
| 1638 | // The monomorphic implementation that works for a particular pointer type. |
| 1639 | template <typename Pointer> |
| 1640 | class Impl : public MatcherInterface<Pointer> { |
| 1641 | public: |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 1642 | typedef typename PointeeOf<GMOCK_REMOVE_CONST_( // NOLINT |
| 1643 | GMOCK_REMOVE_REFERENCE_(Pointer))>::type Pointee; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1644 | |
| 1645 | explicit Impl(const InnerMatcher& matcher) |
| 1646 | : matcher_(MatcherCast<const Pointee&>(matcher)) {} |
| 1647 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1648 | virtual void DescribeTo(::std::ostream* os) const { |
| 1649 | *os << "points to a value that "; |
| 1650 | matcher_.DescribeTo(os); |
| 1651 | } |
| 1652 | |
| 1653 | virtual void DescribeNegationTo(::std::ostream* os) const { |
| 1654 | *os << "does not point to a value that "; |
| 1655 | matcher_.DescribeTo(os); |
| 1656 | } |
| 1657 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 1658 | virtual bool MatchAndExplain(Pointer pointer, |
| 1659 | MatchResultListener* listener) const { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1660 | if (GetRawPointer(pointer) == NULL) |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 1661 | return false; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1662 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 1663 | StringMatchResultListener inner_listener; |
| 1664 | const bool match = matcher_.MatchAndExplain(*pointer, &inner_listener); |
| 1665 | const internal::string s = inner_listener.str(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1666 | if (s != "") { |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 1667 | *listener << "points to a value that " << s; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1668 | } |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 1669 | return match; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1670 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1671 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1672 | private: |
| 1673 | const Matcher<const Pointee&> matcher_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1674 | |
| 1675 | GTEST_DISALLOW_ASSIGN_(Impl); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1676 | }; |
| 1677 | |
| 1678 | const InnerMatcher matcher_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1679 | |
| 1680 | GTEST_DISALLOW_ASSIGN_(PointeeMatcher); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1681 | }; |
| 1682 | |
| 1683 | // Implements the Field() matcher for matching a field (i.e. member |
| 1684 | // variable) of an object. |
| 1685 | template <typename Class, typename FieldType> |
| 1686 | class FieldMatcher { |
| 1687 | public: |
| 1688 | FieldMatcher(FieldType Class::*field, |
| 1689 | const Matcher<const FieldType&>& matcher) |
| 1690 | : field_(field), matcher_(matcher) {} |
| 1691 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1692 | void DescribeTo(::std::ostream* os) const { |
| 1693 | *os << "the given field "; |
| 1694 | matcher_.DescribeTo(os); |
| 1695 | } |
| 1696 | |
| 1697 | void DescribeNegationTo(::std::ostream* os) const { |
| 1698 | *os << "the given field "; |
| 1699 | matcher_.DescribeNegationTo(os); |
| 1700 | } |
| 1701 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 1702 | // The first argument of MatchAndExplain() is needed to help |
zhanyong.wan | 1849065 | 2009-05-11 18:54:08 +0000 | [diff] [blame] | 1703 | // Symbian's C++ compiler choose which overload to use. Its type is |
| 1704 | // true_type iff the Field() matcher is used to match a pointer. |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 1705 | bool MatchAndExplain(false_type /* is_not_pointer */, const Class& obj, |
| 1706 | MatchResultListener* listener) const { |
| 1707 | StringMatchResultListener inner_listener; |
| 1708 | const bool match = matcher_.MatchAndExplain(obj.*field_, &inner_listener); |
| 1709 | const internal::string s = inner_listener.str(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1710 | if (s != "") { |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 1711 | *listener << "the given field " << s; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1712 | } |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 1713 | return match; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 1716 | bool MatchAndExplain(true_type /* is_pointer */, const Class* p, |
| 1717 | MatchResultListener* listener) const { |
| 1718 | if (p == NULL) |
| 1719 | return false; |
| 1720 | |
| 1721 | // Since *p has a field, it must be a class/struct/union type and |
| 1722 | // thus cannot be a pointer. Therefore we pass false_type() as |
| 1723 | // the first argument. |
| 1724 | return MatchAndExplain(false_type(), *p, listener); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1725 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1726 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1727 | private: |
| 1728 | const FieldType Class::*field_; |
| 1729 | const Matcher<const FieldType&> matcher_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1730 | |
| 1731 | GTEST_DISALLOW_ASSIGN_(FieldMatcher); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1732 | }; |
| 1733 | |
zhanyong.wan | 1849065 | 2009-05-11 18:54:08 +0000 | [diff] [blame] | 1734 | template <typename Class, typename FieldType, typename T> |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 1735 | bool MatchAndExplain(const FieldMatcher<Class, FieldType>& matcher, |
| 1736 | const T& value, MatchResultListener* listener) { |
| 1737 | return matcher.MatchAndExplain( |
| 1738 | typename ::testing::internal::is_pointer<T>::type(), value, listener); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1739 | } |
| 1740 | |
| 1741 | // Implements the Property() matcher for matching a property |
| 1742 | // (i.e. return value of a getter method) of an object. |
| 1743 | template <typename Class, typename PropertyType> |
| 1744 | class PropertyMatcher { |
| 1745 | public: |
| 1746 | // The property may have a reference type, so 'const PropertyType&' |
| 1747 | // may cause double references and fail to compile. That's why we |
| 1748 | // need GMOCK_REFERENCE_TO_CONST, which works regardless of |
| 1749 | // PropertyType being a reference or not. |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 1750 | typedef GMOCK_REFERENCE_TO_CONST_(PropertyType) RefToConstProperty; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1751 | |
| 1752 | PropertyMatcher(PropertyType (Class::*property)() const, |
| 1753 | const Matcher<RefToConstProperty>& matcher) |
| 1754 | : property_(property), matcher_(matcher) {} |
| 1755 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1756 | void DescribeTo(::std::ostream* os) const { |
| 1757 | *os << "the given property "; |
| 1758 | matcher_.DescribeTo(os); |
| 1759 | } |
| 1760 | |
| 1761 | void DescribeNegationTo(::std::ostream* os) const { |
| 1762 | *os << "the given property "; |
| 1763 | matcher_.DescribeNegationTo(os); |
| 1764 | } |
| 1765 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 1766 | // The first argument of MatchAndExplain() is needed to help |
zhanyong.wan | 1849065 | 2009-05-11 18:54:08 +0000 | [diff] [blame] | 1767 | // Symbian's C++ compiler choose which overload to use. Its type is |
| 1768 | // true_type iff the Property() matcher is used to match a pointer. |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 1769 | bool MatchAndExplain(false_type /* is_not_pointer */, const Class& obj, |
| 1770 | MatchResultListener* listener) const { |
| 1771 | StringMatchResultListener inner_listener; |
| 1772 | const bool match = matcher_.MatchAndExplain((obj.*property_)(), |
| 1773 | &inner_listener); |
| 1774 | const internal::string s = inner_listener.str(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1775 | if (s != "") { |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 1776 | *listener << "the given property " << s; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1777 | } |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 1778 | return match; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 1781 | bool MatchAndExplain(true_type /* is_pointer */, const Class* p, |
| 1782 | MatchResultListener* listener) const { |
| 1783 | if (p == NULL) |
| 1784 | return false; |
| 1785 | |
| 1786 | // Since *p has a property method, it must be a class/struct/union |
| 1787 | // type and thus cannot be a pointer. Therefore we pass |
| 1788 | // false_type() as the first argument. |
| 1789 | return MatchAndExplain(false_type(), *p, listener); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1790 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1791 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1792 | private: |
| 1793 | PropertyType (Class::*property_)() const; |
| 1794 | const Matcher<RefToConstProperty> matcher_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1795 | |
| 1796 | GTEST_DISALLOW_ASSIGN_(PropertyMatcher); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1797 | }; |
| 1798 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 1799 | template <typename Class, typename PropertyType, typename T> |
| 1800 | bool MatchAndExplain(const PropertyMatcher<Class, PropertyType>& matcher, |
| 1801 | const T& value, MatchResultListener* listener) { |
| 1802 | return matcher.MatchAndExplain( |
| 1803 | typename ::testing::internal::is_pointer<T>::type(), value, listener); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1804 | } |
| 1805 | |
| 1806 | // Type traits specifying various features of different functors for ResultOf. |
| 1807 | // The default template specifies features for functor objects. |
| 1808 | // Functor classes have to typedef argument_type and result_type |
| 1809 | // to be compatible with ResultOf. |
| 1810 | template <typename Functor> |
| 1811 | struct CallableTraits { |
| 1812 | typedef typename Functor::result_type ResultType; |
| 1813 | typedef Functor StorageType; |
| 1814 | |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1815 | static void CheckIsValid(Functor /* functor */) {} |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1816 | template <typename T> |
| 1817 | static ResultType Invoke(Functor f, T arg) { return f(arg); } |
| 1818 | }; |
| 1819 | |
| 1820 | // Specialization for function pointers. |
| 1821 | template <typename ArgType, typename ResType> |
| 1822 | struct CallableTraits<ResType(*)(ArgType)> { |
| 1823 | typedef ResType ResultType; |
| 1824 | typedef ResType(*StorageType)(ArgType); |
| 1825 | |
| 1826 | static void CheckIsValid(ResType(*f)(ArgType)) { |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 1827 | GTEST_CHECK_(f != NULL) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1828 | << "NULL function pointer is passed into ResultOf()."; |
| 1829 | } |
| 1830 | template <typename T> |
| 1831 | static ResType Invoke(ResType(*f)(ArgType), T arg) { |
| 1832 | return (*f)(arg); |
| 1833 | } |
| 1834 | }; |
| 1835 | |
| 1836 | // Implements the ResultOf() matcher for matching a return value of a |
| 1837 | // unary function of an object. |
| 1838 | template <typename Callable> |
| 1839 | class ResultOfMatcher { |
| 1840 | public: |
| 1841 | typedef typename CallableTraits<Callable>::ResultType ResultType; |
| 1842 | |
| 1843 | ResultOfMatcher(Callable callable, const Matcher<ResultType>& matcher) |
| 1844 | : callable_(callable), matcher_(matcher) { |
| 1845 | CallableTraits<Callable>::CheckIsValid(callable_); |
| 1846 | } |
| 1847 | |
| 1848 | template <typename T> |
| 1849 | operator Matcher<T>() const { |
| 1850 | return Matcher<T>(new Impl<T>(callable_, matcher_)); |
| 1851 | } |
| 1852 | |
| 1853 | private: |
| 1854 | typedef typename CallableTraits<Callable>::StorageType CallableStorageType; |
| 1855 | |
| 1856 | template <typename T> |
| 1857 | class Impl : public MatcherInterface<T> { |
| 1858 | public: |
| 1859 | Impl(CallableStorageType callable, const Matcher<ResultType>& matcher) |
| 1860 | : callable_(callable), matcher_(matcher) {} |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1861 | |
| 1862 | virtual void DescribeTo(::std::ostream* os) const { |
| 1863 | *os << "result of the given callable "; |
| 1864 | matcher_.DescribeTo(os); |
| 1865 | } |
| 1866 | |
| 1867 | virtual void DescribeNegationTo(::std::ostream* os) const { |
| 1868 | *os << "result of the given callable "; |
| 1869 | matcher_.DescribeNegationTo(os); |
| 1870 | } |
| 1871 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 1872 | virtual bool MatchAndExplain(T obj, MatchResultListener* listener) const { |
| 1873 | StringMatchResultListener inner_listener; |
| 1874 | const bool match = matcher_.MatchAndExplain( |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1875 | CallableTraits<Callable>::template Invoke<T>(callable_, obj), |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 1876 | &inner_listener); |
| 1877 | |
| 1878 | const internal::string s = inner_listener.str(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1879 | if (s != "") |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 1880 | *listener << "result of the given callable " << s; |
| 1881 | |
| 1882 | return match; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1883 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1884 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1885 | private: |
| 1886 | // Functors often define operator() as non-const method even though |
| 1887 | // they are actualy stateless. But we need to use them even when |
| 1888 | // 'this' is a const pointer. It's the user's responsibility not to |
| 1889 | // use stateful callables with ResultOf(), which does't guarantee |
| 1890 | // how many times the callable will be invoked. |
| 1891 | mutable CallableStorageType callable_; |
| 1892 | const Matcher<ResultType> matcher_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1893 | |
| 1894 | GTEST_DISALLOW_ASSIGN_(Impl); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1895 | }; // class Impl |
| 1896 | |
| 1897 | const CallableStorageType callable_; |
| 1898 | const Matcher<ResultType> matcher_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1899 | |
| 1900 | GTEST_DISALLOW_ASSIGN_(ResultOfMatcher); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1901 | }; |
| 1902 | |
| 1903 | // Explains the result of matching a value against a functor matcher. |
| 1904 | template <typename T, typename Callable> |
| 1905 | void ExplainMatchResultTo(const ResultOfMatcher<Callable>& matcher, |
| 1906 | T obj, ::std::ostream* os) { |
| 1907 | matcher.ExplainMatchResultTo(obj, os); |
| 1908 | } |
| 1909 | |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 1910 | // Implements an equality matcher for any STL-style container whose elements |
| 1911 | // support ==. This matcher is like Eq(), but its failure explanations provide |
| 1912 | // more detailed information that is useful when the container is used as a set. |
| 1913 | // The failure message reports elements that are in one of the operands but not |
| 1914 | // the other. The failure messages do not report duplicate or out-of-order |
| 1915 | // elements in the containers (which don't properly matter to sets, but can |
| 1916 | // occur if the containers are vectors or lists, for example). |
| 1917 | // |
| 1918 | // Uses the container's const_iterator, value_type, operator ==, |
| 1919 | // begin(), and end(). |
| 1920 | template <typename Container> |
| 1921 | class ContainerEqMatcher { |
| 1922 | public: |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 1923 | typedef internal::StlContainerView<Container> View; |
| 1924 | typedef typename View::type StlContainer; |
| 1925 | typedef typename View::const_reference StlContainerReference; |
| 1926 | |
| 1927 | // We make a copy of rhs in case the elements in it are modified |
| 1928 | // after this matcher is created. |
| 1929 | explicit ContainerEqMatcher(const Container& rhs) : rhs_(View::Copy(rhs)) { |
| 1930 | // Makes sure the user doesn't instantiate this class template |
| 1931 | // with a const or reference type. |
| 1932 | testing::StaticAssertTypeEq<Container, |
| 1933 | GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(Container))>(); |
| 1934 | } |
| 1935 | |
| 1936 | template <typename LhsContainer> |
| 1937 | bool Matches(const LhsContainer& lhs) const { |
| 1938 | // GMOCK_REMOVE_CONST_() is needed to work around an MSVC 8.0 bug |
| 1939 | // that causes LhsContainer to be a const type sometimes. |
| 1940 | typedef internal::StlContainerView<GMOCK_REMOVE_CONST_(LhsContainer)> |
| 1941 | LhsView; |
| 1942 | StlContainerReference lhs_stl_container = LhsView::ConstReference(lhs); |
| 1943 | return lhs_stl_container == rhs_; |
| 1944 | } |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 1945 | void DescribeTo(::std::ostream* os) const { |
| 1946 | *os << "equals "; |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 1947 | UniversalPrinter<StlContainer>::Print(rhs_, os); |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 1948 | } |
| 1949 | void DescribeNegationTo(::std::ostream* os) const { |
| 1950 | *os << "does not equal "; |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 1951 | UniversalPrinter<StlContainer>::Print(rhs_, os); |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 1954 | template <typename LhsContainer> |
| 1955 | void ExplainMatchResultTo(const LhsContainer& lhs, |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 1956 | ::std::ostream* os) const { |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 1957 | // GMOCK_REMOVE_CONST_() is needed to work around an MSVC 8.0 bug |
| 1958 | // that causes LhsContainer to be a const type sometimes. |
| 1959 | typedef internal::StlContainerView<GMOCK_REMOVE_CONST_(LhsContainer)> |
| 1960 | LhsView; |
| 1961 | typedef typename LhsView::type LhsStlContainer; |
| 1962 | StlContainerReference lhs_stl_container = LhsView::ConstReference(lhs); |
| 1963 | |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 1964 | // Something is different. Check for missing values first. |
| 1965 | bool printed_header = false; |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 1966 | for (typename LhsStlContainer::const_iterator it = |
| 1967 | lhs_stl_container.begin(); |
| 1968 | it != lhs_stl_container.end(); ++it) { |
| 1969 | if (internal::ArrayAwareFind(rhs_.begin(), rhs_.end(), *it) == |
| 1970 | rhs_.end()) { |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 1971 | if (printed_header) { |
| 1972 | *os << ", "; |
| 1973 | } else { |
| 1974 | *os << "Only in actual: "; |
| 1975 | printed_header = true; |
| 1976 | } |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 1977 | UniversalPrinter<typename LhsStlContainer::value_type>::Print(*it, os); |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 1978 | } |
| 1979 | } |
| 1980 | |
| 1981 | // Now check for extra values. |
| 1982 | bool printed_header2 = false; |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 1983 | for (typename StlContainer::const_iterator it = rhs_.begin(); |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 1984 | it != rhs_.end(); ++it) { |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 1985 | if (internal::ArrayAwareFind( |
| 1986 | lhs_stl_container.begin(), lhs_stl_container.end(), *it) == |
| 1987 | lhs_stl_container.end()) { |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 1988 | if (printed_header2) { |
| 1989 | *os << ", "; |
| 1990 | } else { |
| 1991 | *os << (printed_header ? "; not" : "Not") << " in actual: "; |
| 1992 | printed_header2 = true; |
| 1993 | } |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 1994 | UniversalPrinter<typename StlContainer::value_type>::Print(*it, os); |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 1995 | } |
| 1996 | } |
| 1997 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1998 | |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 1999 | private: |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 2000 | const StlContainer rhs_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 2001 | |
| 2002 | GTEST_DISALLOW_ASSIGN_(ContainerEqMatcher); |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 2003 | }; |
| 2004 | |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 2005 | template <typename LhsContainer, typename Container> |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 2006 | void ExplainMatchResultTo(const ContainerEqMatcher<Container>& matcher, |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 2007 | const LhsContainer& lhs, |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 2008 | ::std::ostream* os) { |
| 2009 | matcher.ExplainMatchResultTo(lhs, os); |
| 2010 | } |
| 2011 | |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 2012 | // Implements Contains(element_matcher) for the given argument type Container. |
| 2013 | template <typename Container> |
| 2014 | class ContainsMatcherImpl : public MatcherInterface<Container> { |
| 2015 | public: |
| 2016 | typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(Container)) RawContainer; |
| 2017 | typedef StlContainerView<RawContainer> View; |
| 2018 | typedef typename View::type StlContainer; |
| 2019 | typedef typename View::const_reference StlContainerReference; |
| 2020 | typedef typename StlContainer::value_type Element; |
| 2021 | |
| 2022 | template <typename InnerMatcher> |
| 2023 | explicit ContainsMatcherImpl(InnerMatcher inner_matcher) |
| 2024 | : inner_matcher_( |
| 2025 | testing::SafeMatcherCast<const Element&>(inner_matcher)) {} |
| 2026 | |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 2027 | // Describes what this matcher does. |
| 2028 | virtual void DescribeTo(::std::ostream* os) const { |
| 2029 | *os << "contains at least one element that "; |
| 2030 | inner_matcher_.DescribeTo(os); |
| 2031 | } |
| 2032 | |
| 2033 | // Describes what the negation of this matcher does. |
| 2034 | virtual void DescribeNegationTo(::std::ostream* os) const { |
| 2035 | *os << "doesn't contain any element that "; |
| 2036 | inner_matcher_.DescribeTo(os); |
| 2037 | } |
| 2038 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 2039 | virtual bool MatchAndExplain(Container container, |
| 2040 | MatchResultListener* listener) const { |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 2041 | StlContainerReference stl_container = View::ConstReference(container); |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 2042 | size_t i = 0; |
| 2043 | for (typename StlContainer::const_iterator it = stl_container.begin(); |
| 2044 | it != stl_container.end(); ++it, ++i) { |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 2045 | if (inner_matcher_.Matches(*it)) { |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 2046 | *listener << "element " << i << " matches"; |
| 2047 | return true; |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 2048 | } |
| 2049 | } |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 2050 | return false; |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 2051 | } |
| 2052 | |
| 2053 | private: |
| 2054 | const Matcher<const Element&> inner_matcher_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 2055 | |
| 2056 | GTEST_DISALLOW_ASSIGN_(ContainsMatcherImpl); |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 2057 | }; |
| 2058 | |
| 2059 | // Implements polymorphic Contains(element_matcher). |
| 2060 | template <typename M> |
| 2061 | class ContainsMatcher { |
| 2062 | public: |
| 2063 | explicit ContainsMatcher(M m) : inner_matcher_(m) {} |
| 2064 | |
| 2065 | template <typename Container> |
| 2066 | operator Matcher<Container>() const { |
| 2067 | return MakeMatcher(new ContainsMatcherImpl<Container>(inner_matcher_)); |
| 2068 | } |
| 2069 | |
| 2070 | private: |
| 2071 | const M inner_matcher_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 2072 | |
| 2073 | GTEST_DISALLOW_ASSIGN_(ContainsMatcher); |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 2074 | }; |
| 2075 | |
zhanyong.wan | b5937da | 2009-07-16 20:26:41 +0000 | [diff] [blame] | 2076 | // Implements Key(inner_matcher) for the given argument pair type. |
| 2077 | // Key(inner_matcher) matches an std::pair whose 'first' field matches |
| 2078 | // inner_matcher. For example, Contains(Key(Ge(5))) can be used to match an |
| 2079 | // std::map that contains at least one element whose key is >= 5. |
| 2080 | template <typename PairType> |
| 2081 | class KeyMatcherImpl : public MatcherInterface<PairType> { |
| 2082 | public: |
| 2083 | typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(PairType)) RawPairType; |
| 2084 | typedef typename RawPairType::first_type KeyType; |
| 2085 | |
| 2086 | template <typename InnerMatcher> |
| 2087 | explicit KeyMatcherImpl(InnerMatcher inner_matcher) |
| 2088 | : inner_matcher_( |
| 2089 | testing::SafeMatcherCast<const KeyType&>(inner_matcher)) { |
| 2090 | } |
| 2091 | |
| 2092 | // Returns true iff 'key_value.first' (the key) matches the inner matcher. |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 2093 | virtual bool MatchAndExplain(PairType key_value, |
| 2094 | MatchResultListener* listener) const { |
| 2095 | return inner_matcher_.MatchAndExplain(key_value.first, listener); |
zhanyong.wan | b5937da | 2009-07-16 20:26:41 +0000 | [diff] [blame] | 2096 | } |
| 2097 | |
| 2098 | // Describes what this matcher does. |
| 2099 | virtual void DescribeTo(::std::ostream* os) const { |
| 2100 | *os << "has a key that "; |
| 2101 | inner_matcher_.DescribeTo(os); |
| 2102 | } |
| 2103 | |
| 2104 | // Describes what the negation of this matcher does. |
| 2105 | virtual void DescribeNegationTo(::std::ostream* os) const { |
| 2106 | *os << "doesn't have a key that "; |
| 2107 | inner_matcher_.DescribeTo(os); |
| 2108 | } |
| 2109 | |
zhanyong.wan | b5937da | 2009-07-16 20:26:41 +0000 | [diff] [blame] | 2110 | private: |
| 2111 | const Matcher<const KeyType&> inner_matcher_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 2112 | |
| 2113 | GTEST_DISALLOW_ASSIGN_(KeyMatcherImpl); |
zhanyong.wan | b5937da | 2009-07-16 20:26:41 +0000 | [diff] [blame] | 2114 | }; |
| 2115 | |
| 2116 | // Implements polymorphic Key(matcher_for_key). |
| 2117 | template <typename M> |
| 2118 | class KeyMatcher { |
| 2119 | public: |
| 2120 | explicit KeyMatcher(M m) : matcher_for_key_(m) {} |
| 2121 | |
| 2122 | template <typename PairType> |
| 2123 | operator Matcher<PairType>() const { |
| 2124 | return MakeMatcher(new KeyMatcherImpl<PairType>(matcher_for_key_)); |
| 2125 | } |
| 2126 | |
| 2127 | private: |
| 2128 | const M matcher_for_key_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 2129 | |
| 2130 | GTEST_DISALLOW_ASSIGN_(KeyMatcher); |
zhanyong.wan | b5937da | 2009-07-16 20:26:41 +0000 | [diff] [blame] | 2131 | }; |
| 2132 | |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 2133 | // Implements Pair(first_matcher, second_matcher) for the given argument pair |
| 2134 | // type with its two matchers. See Pair() function below. |
| 2135 | template <typename PairType> |
| 2136 | class PairMatcherImpl : public MatcherInterface<PairType> { |
| 2137 | public: |
| 2138 | typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(PairType)) RawPairType; |
| 2139 | typedef typename RawPairType::first_type FirstType; |
| 2140 | typedef typename RawPairType::second_type SecondType; |
| 2141 | |
| 2142 | template <typename FirstMatcher, typename SecondMatcher> |
| 2143 | PairMatcherImpl(FirstMatcher first_matcher, SecondMatcher second_matcher) |
| 2144 | : first_matcher_( |
| 2145 | testing::SafeMatcherCast<const FirstType&>(first_matcher)), |
| 2146 | second_matcher_( |
| 2147 | testing::SafeMatcherCast<const SecondType&>(second_matcher)) { |
| 2148 | } |
| 2149 | |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 2150 | // Describes what this matcher does. |
| 2151 | virtual void DescribeTo(::std::ostream* os) const { |
| 2152 | *os << "has a first field that "; |
| 2153 | first_matcher_.DescribeTo(os); |
| 2154 | *os << ", and has a second field that "; |
| 2155 | second_matcher_.DescribeTo(os); |
| 2156 | } |
| 2157 | |
| 2158 | // Describes what the negation of this matcher does. |
| 2159 | virtual void DescribeNegationTo(::std::ostream* os) const { |
| 2160 | *os << "has a first field that "; |
| 2161 | first_matcher_.DescribeNegationTo(os); |
| 2162 | *os << ", or has a second field that "; |
| 2163 | second_matcher_.DescribeNegationTo(os); |
| 2164 | } |
| 2165 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 2166 | // Returns true iff 'a_pair.first' matches first_matcher and 'a_pair.second' |
| 2167 | // matches second_matcher. |
| 2168 | virtual bool MatchAndExplain(PairType a_pair, |
| 2169 | MatchResultListener* listener) const { |
| 2170 | StringMatchResultListener listener1; |
| 2171 | const bool match1 = first_matcher_.MatchAndExplain(a_pair.first, |
| 2172 | &listener1); |
| 2173 | internal::string s1 = listener1.str(); |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 2174 | if (s1 != "") { |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 2175 | s1 = "the first field " + s1; |
| 2176 | } |
| 2177 | if (!match1) { |
| 2178 | *listener << s1; |
| 2179 | return false; |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 2180 | } |
| 2181 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 2182 | StringMatchResultListener listener2; |
| 2183 | const bool match2 = second_matcher_.MatchAndExplain(a_pair.second, |
| 2184 | &listener2); |
| 2185 | internal::string s2 = listener2.str(); |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 2186 | if (s2 != "") { |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 2187 | s2 = "the second field " + s2; |
| 2188 | } |
| 2189 | if (!match2) { |
| 2190 | *listener << s2; |
| 2191 | return false; |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 2194 | *listener << s1; |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 2195 | if (s1 != "" && s2 != "") { |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 2196 | *listener << ", and "; |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 2197 | } |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 2198 | *listener << s2; |
| 2199 | return true; |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
| 2202 | private: |
| 2203 | const Matcher<const FirstType&> first_matcher_; |
| 2204 | const Matcher<const SecondType&> second_matcher_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 2205 | |
| 2206 | GTEST_DISALLOW_ASSIGN_(PairMatcherImpl); |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 2207 | }; |
| 2208 | |
| 2209 | // Implements polymorphic Pair(first_matcher, second_matcher). |
| 2210 | template <typename FirstMatcher, typename SecondMatcher> |
| 2211 | class PairMatcher { |
| 2212 | public: |
| 2213 | PairMatcher(FirstMatcher first_matcher, SecondMatcher second_matcher) |
| 2214 | : first_matcher_(first_matcher), second_matcher_(second_matcher) {} |
| 2215 | |
| 2216 | template <typename PairType> |
| 2217 | operator Matcher<PairType> () const { |
| 2218 | return MakeMatcher( |
| 2219 | new PairMatcherImpl<PairType>( |
| 2220 | first_matcher_, second_matcher_)); |
| 2221 | } |
| 2222 | |
| 2223 | private: |
| 2224 | const FirstMatcher first_matcher_; |
| 2225 | const SecondMatcher second_matcher_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 2226 | |
| 2227 | GTEST_DISALLOW_ASSIGN_(PairMatcher); |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 2228 | }; |
| 2229 | |
zhanyong.wan | 1afe1c7 | 2009-07-21 23:26:31 +0000 | [diff] [blame] | 2230 | // Implements ElementsAre() and ElementsAreArray(). |
| 2231 | template <typename Container> |
| 2232 | class ElementsAreMatcherImpl : public MatcherInterface<Container> { |
| 2233 | public: |
| 2234 | typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(Container)) RawContainer; |
| 2235 | typedef internal::StlContainerView<RawContainer> View; |
| 2236 | typedef typename View::type StlContainer; |
| 2237 | typedef typename View::const_reference StlContainerReference; |
| 2238 | typedef typename StlContainer::value_type Element; |
| 2239 | |
| 2240 | // Constructs the matcher from a sequence of element values or |
| 2241 | // element matchers. |
| 2242 | template <typename InputIter> |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 2243 | ElementsAreMatcherImpl(InputIter first, size_t a_count) { |
| 2244 | matchers_.reserve(a_count); |
zhanyong.wan | 1afe1c7 | 2009-07-21 23:26:31 +0000 | [diff] [blame] | 2245 | InputIter it = first; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 2246 | for (size_t i = 0; i != a_count; ++i, ++it) { |
zhanyong.wan | 1afe1c7 | 2009-07-21 23:26:31 +0000 | [diff] [blame] | 2247 | matchers_.push_back(MatcherCast<const Element&>(*it)); |
| 2248 | } |
| 2249 | } |
| 2250 | |
zhanyong.wan | 1afe1c7 | 2009-07-21 23:26:31 +0000 | [diff] [blame] | 2251 | // Describes what this matcher does. |
| 2252 | virtual void DescribeTo(::std::ostream* os) const { |
| 2253 | if (count() == 0) { |
| 2254 | *os << "is empty"; |
| 2255 | } else if (count() == 1) { |
| 2256 | *os << "has 1 element that "; |
| 2257 | matchers_[0].DescribeTo(os); |
| 2258 | } else { |
| 2259 | *os << "has " << Elements(count()) << " where\n"; |
| 2260 | for (size_t i = 0; i != count(); ++i) { |
| 2261 | *os << "element " << i << " "; |
| 2262 | matchers_[i].DescribeTo(os); |
| 2263 | if (i + 1 < count()) { |
| 2264 | *os << ",\n"; |
| 2265 | } |
| 2266 | } |
| 2267 | } |
| 2268 | } |
| 2269 | |
| 2270 | // Describes what the negation of this matcher does. |
| 2271 | virtual void DescribeNegationTo(::std::ostream* os) const { |
| 2272 | if (count() == 0) { |
| 2273 | *os << "is not empty"; |
| 2274 | return; |
| 2275 | } |
| 2276 | |
| 2277 | *os << "does not have " << Elements(count()) << ", or\n"; |
| 2278 | for (size_t i = 0; i != count(); ++i) { |
| 2279 | *os << "element " << i << " "; |
| 2280 | matchers_[i].DescribeNegationTo(os); |
| 2281 | if (i + 1 < count()) { |
| 2282 | *os << ", or\n"; |
| 2283 | } |
| 2284 | } |
| 2285 | } |
| 2286 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 2287 | virtual bool MatchAndExplain(Container container, |
| 2288 | MatchResultListener* listener) const { |
zhanyong.wan | 1afe1c7 | 2009-07-21 23:26:31 +0000 | [diff] [blame] | 2289 | StlContainerReference stl_container = View::ConstReference(container); |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 2290 | const size_t actual_count = stl_container.size(); |
| 2291 | if (actual_count != count()) { |
| 2292 | // The element count doesn't match. If the container is empty, |
| 2293 | // there's no need to explain anything as Google Mock already |
| 2294 | // prints the empty container. Otherwise we just need to show |
| 2295 | // how many elements there actually are. |
| 2296 | if (actual_count != 0) { |
| 2297 | *listener << "has " << Elements(actual_count); |
zhanyong.wan | 1afe1c7 | 2009-07-21 23:26:31 +0000 | [diff] [blame] | 2298 | } |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 2299 | return false; |
| 2300 | } |
zhanyong.wan | 1afe1c7 | 2009-07-21 23:26:31 +0000 | [diff] [blame] | 2301 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 2302 | typename StlContainer::const_iterator it = stl_container.begin(); |
| 2303 | // explanations[i] is the explanation of the element at index i. |
| 2304 | std::vector<internal::string> explanations(count()); |
| 2305 | for (size_t i = 0; i != count(); ++it, ++i) { |
| 2306 | StringMatchResultListener s; |
| 2307 | if (matchers_[i].MatchAndExplain(*it, &s)) { |
| 2308 | explanations[i] = s.str(); |
| 2309 | } else { |
| 2310 | // The container has the right size but the i-th element |
| 2311 | // doesn't match its expectation. |
| 2312 | *listener << "element " << i << " doesn't match"; |
zhanyong.wan | 1afe1c7 | 2009-07-21 23:26:31 +0000 | [diff] [blame] | 2313 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 2314 | StreamInParensAsNeeded(s.str(), listener->stream()); |
| 2315 | return false; |
zhanyong.wan | 1afe1c7 | 2009-07-21 23:26:31 +0000 | [diff] [blame] | 2316 | } |
| 2317 | } |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 2318 | |
| 2319 | // Every element matches its expectation. We need to explain why |
| 2320 | // (the obvious ones can be skipped). |
| 2321 | |
| 2322 | bool reason_printed = false; |
| 2323 | for (size_t i = 0; i != count(); ++i) { |
| 2324 | const internal::string& s = explanations[i]; |
| 2325 | if (!s.empty()) { |
| 2326 | if (reason_printed) { |
| 2327 | *listener << ",\n"; |
| 2328 | } |
| 2329 | *listener << "element " << i << " " << s; |
| 2330 | reason_printed = true; |
| 2331 | } |
| 2332 | } |
| 2333 | |
| 2334 | return true; |
zhanyong.wan | 1afe1c7 | 2009-07-21 23:26:31 +0000 | [diff] [blame] | 2335 | } |
| 2336 | |
| 2337 | private: |
| 2338 | static Message Elements(size_t count) { |
| 2339 | return Message() << count << (count == 1 ? " element" : " elements"); |
| 2340 | } |
| 2341 | |
| 2342 | size_t count() const { return matchers_.size(); } |
| 2343 | std::vector<Matcher<const Element&> > matchers_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 2344 | |
| 2345 | GTEST_DISALLOW_ASSIGN_(ElementsAreMatcherImpl); |
zhanyong.wan | 1afe1c7 | 2009-07-21 23:26:31 +0000 | [diff] [blame] | 2346 | }; |
| 2347 | |
| 2348 | // Implements ElementsAre() of 0 arguments. |
| 2349 | class ElementsAreMatcher0 { |
| 2350 | public: |
| 2351 | ElementsAreMatcher0() {} |
| 2352 | |
| 2353 | template <typename Container> |
| 2354 | operator Matcher<Container>() const { |
| 2355 | typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(Container)) |
| 2356 | RawContainer; |
| 2357 | typedef typename internal::StlContainerView<RawContainer>::type::value_type |
| 2358 | Element; |
| 2359 | |
| 2360 | const Matcher<const Element&>* const matchers = NULL; |
| 2361 | return MakeMatcher(new ElementsAreMatcherImpl<Container>(matchers, 0)); |
| 2362 | } |
| 2363 | }; |
| 2364 | |
| 2365 | // Implements ElementsAreArray(). |
| 2366 | template <typename T> |
| 2367 | class ElementsAreArrayMatcher { |
| 2368 | public: |
| 2369 | ElementsAreArrayMatcher(const T* first, size_t count) : |
| 2370 | first_(first), count_(count) {} |
| 2371 | |
| 2372 | template <typename Container> |
| 2373 | operator Matcher<Container>() const { |
| 2374 | typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(Container)) |
| 2375 | RawContainer; |
| 2376 | typedef typename internal::StlContainerView<RawContainer>::type::value_type |
| 2377 | Element; |
| 2378 | |
| 2379 | return MakeMatcher(new ElementsAreMatcherImpl<Container>(first_, count_)); |
| 2380 | } |
| 2381 | |
| 2382 | private: |
| 2383 | const T* const first_; |
| 2384 | const size_t count_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 2385 | |
| 2386 | GTEST_DISALLOW_ASSIGN_(ElementsAreArrayMatcher); |
zhanyong.wan | 1afe1c7 | 2009-07-21 23:26:31 +0000 | [diff] [blame] | 2387 | }; |
| 2388 | |
| 2389 | // Constants denoting interpolations in a matcher description string. |
| 2390 | const int kTupleInterpolation = -1; // "%(*)s" |
| 2391 | const int kPercentInterpolation = -2; // "%%" |
| 2392 | const int kInvalidInterpolation = -3; // "%" followed by invalid text |
| 2393 | |
| 2394 | // Records the location and content of an interpolation. |
| 2395 | struct Interpolation { |
| 2396 | Interpolation(const char* start, const char* end, int param) |
| 2397 | : start_pos(start), end_pos(end), param_index(param) {} |
| 2398 | |
| 2399 | // Points to the start of the interpolation (the '%' character). |
| 2400 | const char* start_pos; |
| 2401 | // Points to the first character after the interpolation. |
| 2402 | const char* end_pos; |
| 2403 | // 0-based index of the interpolated matcher parameter; |
| 2404 | // kTupleInterpolation for "%(*)s"; kPercentInterpolation for "%%". |
| 2405 | int param_index; |
| 2406 | }; |
| 2407 | |
| 2408 | typedef ::std::vector<Interpolation> Interpolations; |
| 2409 | |
| 2410 | // Parses a matcher description string and returns a vector of |
| 2411 | // interpolations that appear in the string; generates non-fatal |
| 2412 | // failures iff 'description' is an invalid matcher description. |
| 2413 | // 'param_names' is a NULL-terminated array of parameter names in the |
| 2414 | // order they appear in the MATCHER_P*() parameter list. |
| 2415 | Interpolations ValidateMatcherDescription( |
| 2416 | const char* param_names[], const char* description); |
| 2417 | |
| 2418 | // Returns the actual matcher description, given the matcher name, |
| 2419 | // user-supplied description template string, interpolations in the |
| 2420 | // string, and the printed values of the matcher parameters. |
| 2421 | string FormatMatcherDescription( |
| 2422 | const char* matcher_name, const char* description, |
| 2423 | const Interpolations& interp, const Strings& param_values); |
| 2424 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2425 | } // namespace internal |
| 2426 | |
| 2427 | // Implements MatcherCast(). |
| 2428 | template <typename T, typename M> |
| 2429 | inline Matcher<T> MatcherCast(M matcher) { |
| 2430 | return internal::MatcherCastImpl<T, M>::Cast(matcher); |
| 2431 | } |
| 2432 | |
| 2433 | // _ is a matcher that matches anything of any type. |
| 2434 | // |
| 2435 | // This definition is fine as: |
| 2436 | // |
| 2437 | // 1. The C++ standard permits using the name _ in a namespace that |
| 2438 | // is not the global namespace or ::std. |
| 2439 | // 2. The AnythingMatcher class has no data member or constructor, |
| 2440 | // so it's OK to create global variables of this type. |
| 2441 | // 3. c-style has approved of using _ in this case. |
| 2442 | const internal::AnythingMatcher _ = {}; |
| 2443 | // Creates a matcher that matches any value of the given type T. |
| 2444 | template <typename T> |
| 2445 | inline Matcher<T> A() { return MakeMatcher(new internal::AnyMatcherImpl<T>()); } |
| 2446 | |
| 2447 | // Creates a matcher that matches any value of the given type T. |
| 2448 | template <typename T> |
| 2449 | inline Matcher<T> An() { return A<T>(); } |
| 2450 | |
| 2451 | // Creates a polymorphic matcher that matches anything equal to x. |
| 2452 | // Note: if the parameter of Eq() were declared as const T&, Eq("foo") |
| 2453 | // wouldn't compile. |
| 2454 | template <typename T> |
| 2455 | inline internal::EqMatcher<T> Eq(T x) { return internal::EqMatcher<T>(x); } |
| 2456 | |
| 2457 | // Constructs a Matcher<T> from a 'value' of type T. The constructed |
| 2458 | // matcher matches any value that's equal to 'value'. |
| 2459 | template <typename T> |
| 2460 | Matcher<T>::Matcher(T value) { *this = Eq(value); } |
| 2461 | |
| 2462 | // Creates a monomorphic matcher that matches anything with type Lhs |
| 2463 | // and equal to rhs. A user may need to use this instead of Eq(...) |
| 2464 | // in order to resolve an overloading ambiguity. |
| 2465 | // |
| 2466 | // TypedEq<T>(x) is just a convenient short-hand for Matcher<T>(Eq(x)) |
| 2467 | // or Matcher<T>(x), but more readable than the latter. |
| 2468 | // |
| 2469 | // We could define similar monomorphic matchers for other comparison |
| 2470 | // operations (e.g. TypedLt, TypedGe, and etc), but decided not to do |
| 2471 | // it yet as those are used much less than Eq() in practice. A user |
| 2472 | // can always write Matcher<T>(Lt(5)) to be explicit about the type, |
| 2473 | // for example. |
| 2474 | template <typename Lhs, typename Rhs> |
| 2475 | inline Matcher<Lhs> TypedEq(const Rhs& rhs) { return Eq(rhs); } |
| 2476 | |
| 2477 | // Creates a polymorphic matcher that matches anything >= x. |
| 2478 | template <typename Rhs> |
| 2479 | inline internal::GeMatcher<Rhs> Ge(Rhs x) { |
| 2480 | return internal::GeMatcher<Rhs>(x); |
| 2481 | } |
| 2482 | |
| 2483 | // Creates a polymorphic matcher that matches anything > x. |
| 2484 | template <typename Rhs> |
| 2485 | inline internal::GtMatcher<Rhs> Gt(Rhs x) { |
| 2486 | return internal::GtMatcher<Rhs>(x); |
| 2487 | } |
| 2488 | |
| 2489 | // Creates a polymorphic matcher that matches anything <= x. |
| 2490 | template <typename Rhs> |
| 2491 | inline internal::LeMatcher<Rhs> Le(Rhs x) { |
| 2492 | return internal::LeMatcher<Rhs>(x); |
| 2493 | } |
| 2494 | |
| 2495 | // Creates a polymorphic matcher that matches anything < x. |
| 2496 | template <typename Rhs> |
| 2497 | inline internal::LtMatcher<Rhs> Lt(Rhs x) { |
| 2498 | return internal::LtMatcher<Rhs>(x); |
| 2499 | } |
| 2500 | |
| 2501 | // Creates a polymorphic matcher that matches anything != x. |
| 2502 | template <typename Rhs> |
| 2503 | inline internal::NeMatcher<Rhs> Ne(Rhs x) { |
| 2504 | return internal::NeMatcher<Rhs>(x); |
| 2505 | } |
| 2506 | |
zhanyong.wan | 2d970ee | 2009-09-24 21:41:36 +0000 | [diff] [blame] | 2507 | // Creates a polymorphic matcher that matches any NULL pointer. |
| 2508 | inline PolymorphicMatcher<internal::IsNullMatcher > IsNull() { |
| 2509 | return MakePolymorphicMatcher(internal::IsNullMatcher()); |
| 2510 | } |
| 2511 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2512 | // Creates a polymorphic matcher that matches any non-NULL pointer. |
| 2513 | // This is convenient as Not(NULL) doesn't compile (the compiler |
| 2514 | // thinks that that expression is comparing a pointer with an integer). |
| 2515 | inline PolymorphicMatcher<internal::NotNullMatcher > NotNull() { |
| 2516 | return MakePolymorphicMatcher(internal::NotNullMatcher()); |
| 2517 | } |
| 2518 | |
| 2519 | // Creates a polymorphic matcher that matches any argument that |
| 2520 | // references variable x. |
| 2521 | template <typename T> |
| 2522 | inline internal::RefMatcher<T&> Ref(T& x) { // NOLINT |
| 2523 | return internal::RefMatcher<T&>(x); |
| 2524 | } |
| 2525 | |
| 2526 | // Creates a matcher that matches any double argument approximately |
| 2527 | // equal to rhs, where two NANs are considered unequal. |
| 2528 | inline internal::FloatingEqMatcher<double> DoubleEq(double rhs) { |
| 2529 | return internal::FloatingEqMatcher<double>(rhs, false); |
| 2530 | } |
| 2531 | |
| 2532 | // Creates a matcher that matches any double argument approximately |
| 2533 | // equal to rhs, including NaN values when rhs is NaN. |
| 2534 | inline internal::FloatingEqMatcher<double> NanSensitiveDoubleEq(double rhs) { |
| 2535 | return internal::FloatingEqMatcher<double>(rhs, true); |
| 2536 | } |
| 2537 | |
| 2538 | // Creates a matcher that matches any float argument approximately |
| 2539 | // equal to rhs, where two NANs are considered unequal. |
| 2540 | inline internal::FloatingEqMatcher<float> FloatEq(float rhs) { |
| 2541 | return internal::FloatingEqMatcher<float>(rhs, false); |
| 2542 | } |
| 2543 | |
| 2544 | // Creates a matcher that matches any double argument approximately |
| 2545 | // equal to rhs, including NaN values when rhs is NaN. |
| 2546 | inline internal::FloatingEqMatcher<float> NanSensitiveFloatEq(float rhs) { |
| 2547 | return internal::FloatingEqMatcher<float>(rhs, true); |
| 2548 | } |
| 2549 | |
| 2550 | // Creates a matcher that matches a pointer (raw or smart) that points |
| 2551 | // to a value that matches inner_matcher. |
| 2552 | template <typename InnerMatcher> |
| 2553 | inline internal::PointeeMatcher<InnerMatcher> Pointee( |
| 2554 | const InnerMatcher& inner_matcher) { |
| 2555 | return internal::PointeeMatcher<InnerMatcher>(inner_matcher); |
| 2556 | } |
| 2557 | |
| 2558 | // Creates a matcher that matches an object whose given field matches |
| 2559 | // 'matcher'. For example, |
| 2560 | // Field(&Foo::number, Ge(5)) |
| 2561 | // matches a Foo object x iff x.number >= 5. |
| 2562 | template <typename Class, typename FieldType, typename FieldMatcher> |
| 2563 | inline PolymorphicMatcher< |
| 2564 | internal::FieldMatcher<Class, FieldType> > Field( |
| 2565 | FieldType Class::*field, const FieldMatcher& matcher) { |
| 2566 | return MakePolymorphicMatcher( |
| 2567 | internal::FieldMatcher<Class, FieldType>( |
| 2568 | field, MatcherCast<const FieldType&>(matcher))); |
| 2569 | // The call to MatcherCast() is required for supporting inner |
| 2570 | // matchers of compatible types. For example, it allows |
| 2571 | // Field(&Foo::bar, m) |
| 2572 | // to compile where bar is an int32 and m is a matcher for int64. |
| 2573 | } |
| 2574 | |
| 2575 | // Creates a matcher that matches an object whose given property |
| 2576 | // matches 'matcher'. For example, |
| 2577 | // Property(&Foo::str, StartsWith("hi")) |
| 2578 | // matches a Foo object x iff x.str() starts with "hi". |
| 2579 | template <typename Class, typename PropertyType, typename PropertyMatcher> |
| 2580 | inline PolymorphicMatcher< |
| 2581 | internal::PropertyMatcher<Class, PropertyType> > Property( |
| 2582 | PropertyType (Class::*property)() const, const PropertyMatcher& matcher) { |
| 2583 | return MakePolymorphicMatcher( |
| 2584 | internal::PropertyMatcher<Class, PropertyType>( |
| 2585 | property, |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 2586 | MatcherCast<GMOCK_REFERENCE_TO_CONST_(PropertyType)>(matcher))); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2587 | // The call to MatcherCast() is required for supporting inner |
| 2588 | // matchers of compatible types. For example, it allows |
| 2589 | // Property(&Foo::bar, m) |
| 2590 | // to compile where bar() returns an int32 and m is a matcher for int64. |
| 2591 | } |
| 2592 | |
| 2593 | // Creates a matcher that matches an object iff the result of applying |
| 2594 | // a callable to x matches 'matcher'. |
| 2595 | // For example, |
| 2596 | // ResultOf(f, StartsWith("hi")) |
| 2597 | // matches a Foo object x iff f(x) starts with "hi". |
| 2598 | // callable parameter can be a function, function pointer, or a functor. |
| 2599 | // Callable has to satisfy the following conditions: |
| 2600 | // * It is required to keep no state affecting the results of |
| 2601 | // the calls on it and make no assumptions about how many calls |
| 2602 | // will be made. Any state it keeps must be protected from the |
| 2603 | // concurrent access. |
| 2604 | // * If it is a function object, it has to define type result_type. |
| 2605 | // We recommend deriving your functor classes from std::unary_function. |
| 2606 | template <typename Callable, typename ResultOfMatcher> |
| 2607 | internal::ResultOfMatcher<Callable> ResultOf( |
| 2608 | Callable callable, const ResultOfMatcher& matcher) { |
| 2609 | return internal::ResultOfMatcher<Callable>( |
| 2610 | callable, |
| 2611 | MatcherCast<typename internal::CallableTraits<Callable>::ResultType>( |
| 2612 | matcher)); |
| 2613 | // The call to MatcherCast() is required for supporting inner |
| 2614 | // matchers of compatible types. For example, it allows |
| 2615 | // ResultOf(Function, m) |
| 2616 | // to compile where Function() returns an int32 and m is a matcher for int64. |
| 2617 | } |
| 2618 | |
| 2619 | // String matchers. |
| 2620 | |
| 2621 | // Matches a string equal to str. |
| 2622 | inline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> > |
| 2623 | StrEq(const internal::string& str) { |
| 2624 | return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>( |
| 2625 | str, true, true)); |
| 2626 | } |
| 2627 | |
| 2628 | // Matches a string not equal to str. |
| 2629 | inline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> > |
| 2630 | StrNe(const internal::string& str) { |
| 2631 | return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>( |
| 2632 | str, false, true)); |
| 2633 | } |
| 2634 | |
| 2635 | // Matches a string equal to str, ignoring case. |
| 2636 | inline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> > |
| 2637 | StrCaseEq(const internal::string& str) { |
| 2638 | return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>( |
| 2639 | str, true, false)); |
| 2640 | } |
| 2641 | |
| 2642 | // Matches a string not equal to str, ignoring case. |
| 2643 | inline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> > |
| 2644 | StrCaseNe(const internal::string& str) { |
| 2645 | return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>( |
| 2646 | str, false, false)); |
| 2647 | } |
| 2648 | |
| 2649 | // Creates a matcher that matches any string, std::string, or C string |
| 2650 | // that contains the given substring. |
| 2651 | inline PolymorphicMatcher<internal::HasSubstrMatcher<internal::string> > |
| 2652 | HasSubstr(const internal::string& substring) { |
| 2653 | return MakePolymorphicMatcher(internal::HasSubstrMatcher<internal::string>( |
| 2654 | substring)); |
| 2655 | } |
| 2656 | |
| 2657 | // Matches a string that starts with 'prefix' (case-sensitive). |
| 2658 | inline PolymorphicMatcher<internal::StartsWithMatcher<internal::string> > |
| 2659 | StartsWith(const internal::string& prefix) { |
| 2660 | return MakePolymorphicMatcher(internal::StartsWithMatcher<internal::string>( |
| 2661 | prefix)); |
| 2662 | } |
| 2663 | |
| 2664 | // Matches a string that ends with 'suffix' (case-sensitive). |
| 2665 | inline PolymorphicMatcher<internal::EndsWithMatcher<internal::string> > |
| 2666 | EndsWith(const internal::string& suffix) { |
| 2667 | return MakePolymorphicMatcher(internal::EndsWithMatcher<internal::string>( |
| 2668 | suffix)); |
| 2669 | } |
| 2670 | |
| 2671 | #ifdef GMOCK_HAS_REGEX |
| 2672 | |
| 2673 | // Matches a string that fully matches regular expression 'regex'. |
| 2674 | // The matcher takes ownership of 'regex'. |
| 2675 | inline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex( |
| 2676 | const internal::RE* regex) { |
| 2677 | return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, true)); |
| 2678 | } |
| 2679 | inline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex( |
| 2680 | const internal::string& regex) { |
| 2681 | return MatchesRegex(new internal::RE(regex)); |
| 2682 | } |
| 2683 | |
| 2684 | // Matches a string that contains regular expression 'regex'. |
| 2685 | // The matcher takes ownership of 'regex'. |
| 2686 | inline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex( |
| 2687 | const internal::RE* regex) { |
| 2688 | return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, false)); |
| 2689 | } |
| 2690 | inline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex( |
| 2691 | const internal::string& regex) { |
| 2692 | return ContainsRegex(new internal::RE(regex)); |
| 2693 | } |
| 2694 | |
| 2695 | #endif // GMOCK_HAS_REGEX |
| 2696 | |
| 2697 | #if GTEST_HAS_GLOBAL_WSTRING || GTEST_HAS_STD_WSTRING |
| 2698 | // Wide string matchers. |
| 2699 | |
| 2700 | // Matches a string equal to str. |
| 2701 | inline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> > |
| 2702 | StrEq(const internal::wstring& str) { |
| 2703 | return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>( |
| 2704 | str, true, true)); |
| 2705 | } |
| 2706 | |
| 2707 | // Matches a string not equal to str. |
| 2708 | inline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> > |
| 2709 | StrNe(const internal::wstring& str) { |
| 2710 | return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>( |
| 2711 | str, false, true)); |
| 2712 | } |
| 2713 | |
| 2714 | // Matches a string equal to str, ignoring case. |
| 2715 | inline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> > |
| 2716 | StrCaseEq(const internal::wstring& str) { |
| 2717 | return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>( |
| 2718 | str, true, false)); |
| 2719 | } |
| 2720 | |
| 2721 | // Matches a string not equal to str, ignoring case. |
| 2722 | inline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> > |
| 2723 | StrCaseNe(const internal::wstring& str) { |
| 2724 | return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>( |
| 2725 | str, false, false)); |
| 2726 | } |
| 2727 | |
| 2728 | // Creates a matcher that matches any wstring, std::wstring, or C wide string |
| 2729 | // that contains the given substring. |
| 2730 | inline PolymorphicMatcher<internal::HasSubstrMatcher<internal::wstring> > |
| 2731 | HasSubstr(const internal::wstring& substring) { |
| 2732 | return MakePolymorphicMatcher(internal::HasSubstrMatcher<internal::wstring>( |
| 2733 | substring)); |
| 2734 | } |
| 2735 | |
| 2736 | // Matches a string that starts with 'prefix' (case-sensitive). |
| 2737 | inline PolymorphicMatcher<internal::StartsWithMatcher<internal::wstring> > |
| 2738 | StartsWith(const internal::wstring& prefix) { |
| 2739 | return MakePolymorphicMatcher(internal::StartsWithMatcher<internal::wstring>( |
| 2740 | prefix)); |
| 2741 | } |
| 2742 | |
| 2743 | // Matches a string that ends with 'suffix' (case-sensitive). |
| 2744 | inline PolymorphicMatcher<internal::EndsWithMatcher<internal::wstring> > |
| 2745 | EndsWith(const internal::wstring& suffix) { |
| 2746 | return MakePolymorphicMatcher(internal::EndsWithMatcher<internal::wstring>( |
| 2747 | suffix)); |
| 2748 | } |
| 2749 | |
| 2750 | #endif // GTEST_HAS_GLOBAL_WSTRING || GTEST_HAS_STD_WSTRING |
| 2751 | |
| 2752 | // Creates a polymorphic matcher that matches a 2-tuple where the |
| 2753 | // first field == the second field. |
| 2754 | inline internal::Eq2Matcher Eq() { return internal::Eq2Matcher(); } |
| 2755 | |
| 2756 | // Creates a polymorphic matcher that matches a 2-tuple where the |
| 2757 | // first field >= the second field. |
| 2758 | inline internal::Ge2Matcher Ge() { return internal::Ge2Matcher(); } |
| 2759 | |
| 2760 | // Creates a polymorphic matcher that matches a 2-tuple where the |
| 2761 | // first field > the second field. |
| 2762 | inline internal::Gt2Matcher Gt() { return internal::Gt2Matcher(); } |
| 2763 | |
| 2764 | // Creates a polymorphic matcher that matches a 2-tuple where the |
| 2765 | // first field <= the second field. |
| 2766 | inline internal::Le2Matcher Le() { return internal::Le2Matcher(); } |
| 2767 | |
| 2768 | // Creates a polymorphic matcher that matches a 2-tuple where the |
| 2769 | // first field < the second field. |
| 2770 | inline internal::Lt2Matcher Lt() { return internal::Lt2Matcher(); } |
| 2771 | |
| 2772 | // Creates a polymorphic matcher that matches a 2-tuple where the |
| 2773 | // first field != the second field. |
| 2774 | inline internal::Ne2Matcher Ne() { return internal::Ne2Matcher(); } |
| 2775 | |
| 2776 | // Creates a matcher that matches any value of type T that m doesn't |
| 2777 | // match. |
| 2778 | template <typename InnerMatcher> |
| 2779 | inline internal::NotMatcher<InnerMatcher> Not(InnerMatcher m) { |
| 2780 | return internal::NotMatcher<InnerMatcher>(m); |
| 2781 | } |
| 2782 | |
| 2783 | // Creates a matcher that matches any value that matches all of the |
| 2784 | // given matchers. |
| 2785 | // |
| 2786 | // For now we only support up to 5 matchers. Support for more |
| 2787 | // matchers can be added as needed, or the user can use nested |
| 2788 | // AllOf()s. |
| 2789 | template <typename Matcher1, typename Matcher2> |
| 2790 | inline internal::BothOfMatcher<Matcher1, Matcher2> |
| 2791 | AllOf(Matcher1 m1, Matcher2 m2) { |
| 2792 | return internal::BothOfMatcher<Matcher1, Matcher2>(m1, m2); |
| 2793 | } |
| 2794 | |
| 2795 | template <typename Matcher1, typename Matcher2, typename Matcher3> |
| 2796 | inline internal::BothOfMatcher<Matcher1, |
| 2797 | internal::BothOfMatcher<Matcher2, Matcher3> > |
| 2798 | AllOf(Matcher1 m1, Matcher2 m2, Matcher3 m3) { |
| 2799 | return AllOf(m1, AllOf(m2, m3)); |
| 2800 | } |
| 2801 | |
| 2802 | template <typename Matcher1, typename Matcher2, typename Matcher3, |
| 2803 | typename Matcher4> |
| 2804 | inline internal::BothOfMatcher<Matcher1, |
| 2805 | internal::BothOfMatcher<Matcher2, |
| 2806 | internal::BothOfMatcher<Matcher3, Matcher4> > > |
| 2807 | AllOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4) { |
| 2808 | return AllOf(m1, AllOf(m2, m3, m4)); |
| 2809 | } |
| 2810 | |
| 2811 | template <typename Matcher1, typename Matcher2, typename Matcher3, |
| 2812 | typename Matcher4, typename Matcher5> |
| 2813 | inline internal::BothOfMatcher<Matcher1, |
| 2814 | internal::BothOfMatcher<Matcher2, |
| 2815 | internal::BothOfMatcher<Matcher3, |
| 2816 | internal::BothOfMatcher<Matcher4, Matcher5> > > > |
| 2817 | AllOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4, Matcher5 m5) { |
| 2818 | return AllOf(m1, AllOf(m2, m3, m4, m5)); |
| 2819 | } |
| 2820 | |
| 2821 | // Creates a matcher that matches any value that matches at least one |
| 2822 | // of the given matchers. |
| 2823 | // |
| 2824 | // For now we only support up to 5 matchers. Support for more |
| 2825 | // matchers can be added as needed, or the user can use nested |
| 2826 | // AnyOf()s. |
| 2827 | template <typename Matcher1, typename Matcher2> |
| 2828 | inline internal::EitherOfMatcher<Matcher1, Matcher2> |
| 2829 | AnyOf(Matcher1 m1, Matcher2 m2) { |
| 2830 | return internal::EitherOfMatcher<Matcher1, Matcher2>(m1, m2); |
| 2831 | } |
| 2832 | |
| 2833 | template <typename Matcher1, typename Matcher2, typename Matcher3> |
| 2834 | inline internal::EitherOfMatcher<Matcher1, |
| 2835 | internal::EitherOfMatcher<Matcher2, Matcher3> > |
| 2836 | AnyOf(Matcher1 m1, Matcher2 m2, Matcher3 m3) { |
| 2837 | return AnyOf(m1, AnyOf(m2, m3)); |
| 2838 | } |
| 2839 | |
| 2840 | template <typename Matcher1, typename Matcher2, typename Matcher3, |
| 2841 | typename Matcher4> |
| 2842 | inline internal::EitherOfMatcher<Matcher1, |
| 2843 | internal::EitherOfMatcher<Matcher2, |
| 2844 | internal::EitherOfMatcher<Matcher3, Matcher4> > > |
| 2845 | AnyOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4) { |
| 2846 | return AnyOf(m1, AnyOf(m2, m3, m4)); |
| 2847 | } |
| 2848 | |
| 2849 | template <typename Matcher1, typename Matcher2, typename Matcher3, |
| 2850 | typename Matcher4, typename Matcher5> |
| 2851 | inline internal::EitherOfMatcher<Matcher1, |
| 2852 | internal::EitherOfMatcher<Matcher2, |
| 2853 | internal::EitherOfMatcher<Matcher3, |
| 2854 | internal::EitherOfMatcher<Matcher4, Matcher5> > > > |
| 2855 | AnyOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4, Matcher5 m5) { |
| 2856 | return AnyOf(m1, AnyOf(m2, m3, m4, m5)); |
| 2857 | } |
| 2858 | |
| 2859 | // Returns a matcher that matches anything that satisfies the given |
| 2860 | // predicate. The predicate can be any unary function or functor |
| 2861 | // whose return type can be implicitly converted to bool. |
| 2862 | template <typename Predicate> |
| 2863 | inline PolymorphicMatcher<internal::TrulyMatcher<Predicate> > |
| 2864 | Truly(Predicate pred) { |
| 2865 | return MakePolymorphicMatcher(internal::TrulyMatcher<Predicate>(pred)); |
| 2866 | } |
| 2867 | |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 2868 | // Returns a matcher that matches an equal container. |
| 2869 | // This matcher behaves like Eq(), but in the event of mismatch lists the |
| 2870 | // values that are included in one container but not the other. (Duplicate |
| 2871 | // values and order differences are not explained.) |
| 2872 | template <typename Container> |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 2873 | inline PolymorphicMatcher<internal::ContainerEqMatcher< // NOLINT |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 2874 | GMOCK_REMOVE_CONST_(Container)> > |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 2875 | ContainerEq(const Container& rhs) { |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 2876 | // This following line is for working around a bug in MSVC 8.0, |
| 2877 | // which causes Container to be a const type sometimes. |
| 2878 | typedef GMOCK_REMOVE_CONST_(Container) RawContainer; |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame^] | 2879 | return MakePolymorphicMatcher( |
| 2880 | internal::ContainerEqMatcher<RawContainer>(rhs)); |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 2881 | } |
| 2882 | |
| 2883 | // Matches an STL-style container or a native array that contains at |
| 2884 | // least one element matching the given value or matcher. |
| 2885 | // |
| 2886 | // Examples: |
| 2887 | // ::std::set<int> page_ids; |
| 2888 | // page_ids.insert(3); |
| 2889 | // page_ids.insert(1); |
| 2890 | // EXPECT_THAT(page_ids, Contains(1)); |
| 2891 | // EXPECT_THAT(page_ids, Contains(Gt(2))); |
| 2892 | // EXPECT_THAT(page_ids, Not(Contains(4))); |
| 2893 | // |
| 2894 | // ::std::map<int, size_t> page_lengths; |
| 2895 | // page_lengths[1] = 100; |
zhanyong.wan | 4019819 | 2009-07-01 05:03:39 +0000 | [diff] [blame] | 2896 | // EXPECT_THAT(page_lengths, |
| 2897 | // Contains(::std::pair<const int, size_t>(1, 100))); |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 2898 | // |
| 2899 | // const char* user_ids[] = { "joe", "mike", "tom" }; |
| 2900 | // EXPECT_THAT(user_ids, Contains(Eq(::std::string("tom")))); |
| 2901 | template <typename M> |
| 2902 | inline internal::ContainsMatcher<M> Contains(M matcher) { |
| 2903 | return internal::ContainsMatcher<M>(matcher); |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 2904 | } |
| 2905 | |
zhanyong.wan | b5937da | 2009-07-16 20:26:41 +0000 | [diff] [blame] | 2906 | // Key(inner_matcher) matches an std::pair whose 'first' field matches |
| 2907 | // inner_matcher. For example, Contains(Key(Ge(5))) can be used to match an |
| 2908 | // std::map that contains at least one element whose key is >= 5. |
| 2909 | template <typename M> |
| 2910 | inline internal::KeyMatcher<M> Key(M inner_matcher) { |
| 2911 | return internal::KeyMatcher<M>(inner_matcher); |
| 2912 | } |
| 2913 | |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 2914 | // Pair(first_matcher, second_matcher) matches a std::pair whose 'first' field |
| 2915 | // matches first_matcher and whose 'second' field matches second_matcher. For |
| 2916 | // example, EXPECT_THAT(map_type, ElementsAre(Pair(Ge(5), "foo"))) can be used |
| 2917 | // to match a std::map<int, string> that contains exactly one element whose key |
| 2918 | // is >= 5 and whose value equals "foo". |
| 2919 | template <typename FirstMatcher, typename SecondMatcher> |
| 2920 | inline internal::PairMatcher<FirstMatcher, SecondMatcher> |
| 2921 | Pair(FirstMatcher first_matcher, SecondMatcher second_matcher) { |
| 2922 | return internal::PairMatcher<FirstMatcher, SecondMatcher>( |
| 2923 | first_matcher, second_matcher); |
| 2924 | } |
| 2925 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2926 | // Returns a predicate that is satisfied by anything that matches the |
| 2927 | // given matcher. |
| 2928 | template <typename M> |
| 2929 | inline internal::MatcherAsPredicate<M> Matches(M matcher) { |
| 2930 | return internal::MatcherAsPredicate<M>(matcher); |
| 2931 | } |
| 2932 | |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 2933 | // Returns true iff the value matches the matcher. |
| 2934 | template <typename T, typename M> |
| 2935 | inline bool Value(const T& value, M matcher) { |
| 2936 | return testing::Matches(matcher)(value); |
| 2937 | } |
| 2938 | |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 2939 | // AllArgs(m) is a synonym of m. This is useful in |
| 2940 | // |
| 2941 | // EXPECT_CALL(foo, Bar(_, _)).With(AllArgs(Eq())); |
| 2942 | // |
| 2943 | // which is easier to read than |
| 2944 | // |
| 2945 | // EXPECT_CALL(foo, Bar(_, _)).With(Eq()); |
| 2946 | template <typename InnerMatcher> |
| 2947 | inline InnerMatcher AllArgs(const InnerMatcher& matcher) { return matcher; } |
| 2948 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2949 | // These macros allow using matchers to check values in Google Test |
| 2950 | // tests. ASSERT_THAT(value, matcher) and EXPECT_THAT(value, matcher) |
| 2951 | // succeed iff the value matches the matcher. If the assertion fails, |
| 2952 | // the value and the description of the matcher will be printed. |
| 2953 | #define ASSERT_THAT(value, matcher) ASSERT_PRED_FORMAT1(\ |
| 2954 | ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value) |
| 2955 | #define EXPECT_THAT(value, matcher) EXPECT_PRED_FORMAT1(\ |
| 2956 | ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value) |
| 2957 | |
| 2958 | } // namespace testing |
| 2959 | |
| 2960 | #endif // GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_ |