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