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