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