Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(346)

Side by Side Diff: testing/gmock/include/gmock/gmock-matchers.h

Issue 1151006: Update to current gtest/gmock. (Closed)
Patch Set: rebase Created 10 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2007, Google Inc. 1 // Copyright 2007, Google Inc.
2 // All rights reserved. 2 // All rights reserved.
3 // 3 //
4 // Redistribution and use in source and binary forms, with or without 4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are 5 // modification, are permitted provided that the following conditions are
6 // met: 6 // met:
7 // 7 //
8 // * Redistributions of source code must retain the above copyright 8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer. 9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above 10 // * Redistributions in binary form must reproduce the above
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 // MatcherInterface<T> interface, and 57 // MatcherInterface<T> interface, and
58 // 2. a factory function that creates a Matcher<T> object from a 58 // 2. a factory function that creates a Matcher<T> object from a
59 // FooMatcherImpl*. 59 // FooMatcherImpl*.
60 // 60 //
61 // The two-level delegation design makes it possible to allow a user 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 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 63 // is impossible if we pass matchers by pointers. It also eases
64 // ownership management as Matcher objects can now be copied like 64 // ownership management as Matcher objects can now be copied like
65 // plain values. 65 // plain values.
66 66
67 // MatchResultListener is an abstract class. Its << operator can be
68 // used by a matcher to explain why a value matches or doesn't match.
69 //
70 // TODO(wan@google.com): add method
71 // bool InterestedInWhy(bool result) const;
72 // to indicate whether the listener is interested in why the match
73 // result is 'result'.
74 class MatchResultListener {
75 public:
76 // Creates a listener object with the given underlying ostream. The
77 // listener does not own the ostream.
78 explicit MatchResultListener(::std::ostream* os) : stream_(os) {}
79 virtual ~MatchResultListener() = 0; // Makes this class abstract.
80
81 // Streams x to the underlying ostream; does nothing if the ostream
82 // is NULL.
83 template <typename T>
84 MatchResultListener& operator<<(const T& x) {
85 if (stream_ != NULL)
86 *stream_ << x;
87 return *this;
88 }
89
90 // Returns the underlying ostream.
91 ::std::ostream* stream() { return stream_; }
92
93 // Returns true iff the listener is interested in an explanation of
94 // the match result. A matcher's MatchAndExplain() method can use
95 // this information to avoid generating the explanation when no one
96 // intends to hear it.
97 bool IsInterested() const { return stream_ != NULL; }
98
99 private:
100 ::std::ostream* const stream_;
101
102 GTEST_DISALLOW_COPY_AND_ASSIGN_(MatchResultListener);
103 };
104
105 inline MatchResultListener::~MatchResultListener() {
106 }
107
67 // The implementation of a matcher. 108 // The implementation of a matcher.
68 template <typename T> 109 template <typename T>
69 class MatcherInterface { 110 class MatcherInterface {
70 public: 111 public:
71 virtual ~MatcherInterface() {} 112 virtual ~MatcherInterface() {}
72 113
73 // Returns true iff the matcher matches x. 114 // Returns true iff the matcher matches x; also explains the match
74 virtual bool Matches(T x) const = 0; 115 // result to 'listener', in the form of a non-restrictive relative
116 // clause ("which ...", "whose ...", etc) that describes x. For
117 // example, the MatchAndExplain() method of the Pointee(...) matcher
118 // should generate an explanation like "which points to ...".
119 //
120 // You should override this method when defining a new matcher.
121 //
122 // It's the responsibility of the caller (Google Mock) to guarantee
123 // that 'listener' is not NULL. This helps to simplify a matcher's
124 // implementation when it doesn't care about the performance, as it
125 // can talk to 'listener' without checking its validity first.
126 // However, in order to implement dummy listeners efficiently,
127 // listener->stream() may be NULL.
128 virtual bool MatchAndExplain(T x, MatchResultListener* listener) const = 0;
75 129
76 // Describes this matcher to an ostream. 130 // Describes this matcher to an ostream. The function should print
131 // a verb phrase that describes the property a value matching this
132 // matcher should have. The subject of the verb phrase is the value
133 // being matched. For example, the DescribeTo() method of the Gt(7)
134 // matcher prints "is greater than 7".
77 virtual void DescribeTo(::std::ostream* os) const = 0; 135 virtual void DescribeTo(::std::ostream* os) const = 0;
78 136
79 // Describes the negation of this matcher to an ostream. For 137 // Describes the negation of this matcher to an ostream. For
80 // example, if the description of this matcher is "is greater than 138 // example, if the description of this matcher is "is greater than
81 // 7", the negated description could be "is not greater than 7". 139 // 7", the negated description could be "is not greater than 7".
82 // You are not required to override this when implementing 140 // You are not required to override this when implementing
83 // MatcherInterface, but it is highly advised so that your matcher 141 // MatcherInterface, but it is highly advised so that your matcher
84 // can produce good error messages. 142 // can produce good error messages.
85 virtual void DescribeNegationTo(::std::ostream* os) const { 143 virtual void DescribeNegationTo(::std::ostream* os) const {
86 *os << "not ("; 144 *os << "not (";
87 DescribeTo(os); 145 DescribeTo(os);
88 *os << ")"; 146 *os << ")";
89 } 147 }
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.
94 virtual void ExplainMatchResultTo(T /* x */, ::std::ostream* /* os */) const {
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 }; 148 };
100 149
101 namespace internal { 150 namespace internal {
102 151
152 // A match result listener that ignores the explanation.
153 class DummyMatchResultListener : public MatchResultListener {
154 public:
155 DummyMatchResultListener() : MatchResultListener(NULL) {}
156
157 private:
158 GTEST_DISALLOW_COPY_AND_ASSIGN_(DummyMatchResultListener);
159 };
160
161 // A match result listener that forwards the explanation to a given
162 // ostream. The difference between this and MatchResultListener is
163 // that the former is concrete.
164 class StreamMatchResultListener : public MatchResultListener {
165 public:
166 explicit StreamMatchResultListener(::std::ostream* os)
167 : MatchResultListener(os) {}
168
169 private:
170 GTEST_DISALLOW_COPY_AND_ASSIGN_(StreamMatchResultListener);
171 };
172
173 // A match result listener that stores the explanation in a string.
174 class StringMatchResultListener : public MatchResultListener {
175 public:
176 StringMatchResultListener() : MatchResultListener(&ss_) {}
177
178 // Returns the explanation heard so far.
179 internal::string str() const { return ss_.str(); }
180
181 private:
182 ::std::stringstream ss_;
183
184 GTEST_DISALLOW_COPY_AND_ASSIGN_(StringMatchResultListener);
185 };
186
103 // An internal class for implementing Matcher<T>, which will derive 187 // An internal class for implementing Matcher<T>, which will derive
104 // from it. We put functionalities common to all Matcher<T> 188 // from it. We put functionalities common to all Matcher<T>
105 // specializations here to avoid code duplication. 189 // specializations here to avoid code duplication.
106 template <typename T> 190 template <typename T>
107 class MatcherBase { 191 class MatcherBase {
108 public: 192 public:
193 // Returns true iff the matcher matches x; also explains the match
194 // result to 'listener'.
195 bool MatchAndExplain(T x, MatchResultListener* listener) const {
196 return impl_->MatchAndExplain(x, listener);
197 }
198
109 // Returns true iff this matcher matches x. 199 // Returns true iff this matcher matches x.
110 bool Matches(T x) const { return impl_->Matches(x); } 200 bool Matches(T x) const {
201 DummyMatchResultListener dummy;
202 return MatchAndExplain(x, &dummy);
203 }
111 204
112 // Describes this matcher to an ostream. 205 // Describes this matcher to an ostream.
113 void DescribeTo(::std::ostream* os) const { impl_->DescribeTo(os); } 206 void DescribeTo(::std::ostream* os) const { impl_->DescribeTo(os); }
114 207
115 // Describes the negation of this matcher to an ostream. 208 // Describes the negation of this matcher to an ostream.
116 void DescribeNegationTo(::std::ostream* os) const { 209 void DescribeNegationTo(::std::ostream* os) const {
117 impl_->DescribeNegationTo(os); 210 impl_->DescribeNegationTo(os);
118 } 211 }
119 212
120 // Explains why x matches, or doesn't match, the matcher. 213 // Explains why x matches, or doesn't match, the matcher.
121 void ExplainMatchResultTo(T x, ::std::ostream* os) const { 214 void ExplainMatchResultTo(T x, ::std::ostream* os) const {
122 impl_->ExplainMatchResultTo(x, os); 215 StreamMatchResultListener listener(os);
216 MatchAndExplain(x, &listener);
123 } 217 }
124 218
125 protected: 219 protected:
126 MatcherBase() {} 220 MatcherBase() {}
127 221
128 // Constructs a matcher from its implementation. 222 // Constructs a matcher from its implementation.
129 explicit MatcherBase(const MatcherInterface<T>* impl) 223 explicit MatcherBase(const MatcherInterface<T>* impl)
130 : impl_(impl) {} 224 : impl_(impl) {}
131 225
132 virtual ~MatcherBase() {} 226 virtual ~MatcherBase() {}
133 227
134 private: 228 private:
135 // shared_ptr (util/gtl/shared_ptr.h) and linked_ptr have similar 229 // shared_ptr (util/gtl/shared_ptr.h) and linked_ptr have similar
136 // interfaces. The former dynamically allocates a chunk of memory 230 // interfaces. The former dynamically allocates a chunk of memory
137 // to hold the reference count, while the latter tracks all 231 // to hold the reference count, while the latter tracks all
138 // references using a circular linked list without allocating 232 // references using a circular linked list without allocating
139 // memory. It has been observed that linked_ptr performs better in 233 // memory. It has been observed that linked_ptr performs better in
140 // typical scenarios. However, shared_ptr can out-perform 234 // typical scenarios. However, shared_ptr can out-perform
141 // linked_ptr when there are many more uses of the copy constructor 235 // linked_ptr when there are many more uses of the copy constructor
142 // than the default constructor. 236 // than the default constructor.
143 // 237 //
144 // If performance becomes a problem, we should see if using 238 // If performance becomes a problem, we should see if using
145 // shared_ptr helps. 239 // shared_ptr helps.
146 ::testing::internal::linked_ptr<const MatcherInterface<T> > impl_; 240 ::testing::internal::linked_ptr<const MatcherInterface<T> > impl_;
147 }; 241 };
148 242
149 // The default implementation of ExplainMatchResultTo() for
150 // polymorphic matchers.
151 template <typename PolymorphicMatcherImpl, typename T>
152 inline void ExplainMatchResultTo(const PolymorphicMatcherImpl& /* impl */,
153 const T& /* x */,
154 ::std::ostream* /* os */) {
155 // By default, nothing more needs to be said, as Google Mock already
156 // prints the value of x elsewhere.
157 }
158
159 } // namespace internal 243 } // namespace internal
160 244
161 // A Matcher<T> is a copyable and IMMUTABLE (except by assignment) 245 // A Matcher<T> is a copyable and IMMUTABLE (except by assignment)
162 // object that can check whether a value of type T matches. The 246 // object that can check whether a value of type T matches. The
163 // implementation of Matcher<T> is just a linked_ptr to const 247 // implementation of Matcher<T> is just a linked_ptr to const
164 // MatcherInterface<T>, so copying is fairly cheap. Don't inherit 248 // MatcherInterface<T>, so copying is fairly cheap. Don't inherit
165 // from Matcher! 249 // from Matcher!
166 template <typename T> 250 template <typename T>
167 class Matcher : public internal::MatcherBase<T> { 251 class Matcher : public internal::MatcherBase<T> {
168 public: 252 public:
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 Matcher(const internal::string& s); // NOLINT 297 Matcher(const internal::string& s); // NOLINT
214 298
215 // Allows the user to write "foo" instead of Eq("foo") sometimes. 299 // Allows the user to write "foo" instead of Eq("foo") sometimes.
216 Matcher(const char* s); // NOLINT 300 Matcher(const char* s); // NOLINT
217 }; 301 };
218 302
219 // The PolymorphicMatcher class template makes it easy to implement a 303 // The PolymorphicMatcher class template makes it easy to implement a
220 // polymorphic matcher (i.e. a matcher that can match values of more 304 // polymorphic matcher (i.e. a matcher that can match values of more
221 // than one type, e.g. Eq(n) and NotNull()). 305 // than one type, e.g. Eq(n) and NotNull()).
222 // 306 //
223 // To define a polymorphic matcher, a user first provides a Impl class 307 // To define a polymorphic matcher, a user should provide an Impl
224 // that has a Matches() method, a DescribeTo() method, and a 308 // class that has a DescribeTo() method and a DescribeNegationTo()
225 // DescribeNegationTo() method. The Matches() method is usually a 309 // method, and define a member function (or member function template)
226 // method template (such that it works with multiple types). Then the
227 // user creates the polymorphic matcher using
228 // MakePolymorphicMatcher(). To provide additional explanation to the
229 // match result, define a FREE function (or function template)
230 // 310 //
231 // void ExplainMatchResultTo(const Impl& matcher, const Value& value, 311 // bool MatchAndExplain(const Value& value,
232 // ::std::ostream* os); 312 // MatchResultListener* listener) const;
233 // 313 //
234 // in the SAME NAME SPACE where Impl is defined. See the definition 314 // See the definition of NotNull() for a complete example.
235 // of NotNull() for a complete example.
236 template <class Impl> 315 template <class Impl>
237 class PolymorphicMatcher { 316 class PolymorphicMatcher {
238 public: 317 public:
239 explicit PolymorphicMatcher(const Impl& an_impl) : impl_(an_impl) {} 318 explicit PolymorphicMatcher(const Impl& an_impl) : impl_(an_impl) {}
240 319
241 // Returns a mutable reference to the underlying matcher 320 // Returns a mutable reference to the underlying matcher
242 // implementation object. 321 // implementation object.
243 Impl& mutable_impl() { return impl_; } 322 Impl& mutable_impl() { return impl_; }
244 323
245 // Returns an immutable reference to the underlying matcher 324 // Returns an immutable reference to the underlying matcher
246 // implementation object. 325 // implementation object.
247 const Impl& impl() const { return impl_; } 326 const Impl& impl() const { return impl_; }
248 327
249 template <typename T> 328 template <typename T>
250 operator Matcher<T>() const { 329 operator Matcher<T>() const {
251 return Matcher<T>(new MonomorphicImpl<T>(impl_)); 330 return Matcher<T>(new MonomorphicImpl<T>(impl_));
252 } 331 }
253 332
254 private: 333 private:
255 template <typename T> 334 template <typename T>
256 class MonomorphicImpl : public MatcherInterface<T> { 335 class MonomorphicImpl : public MatcherInterface<T> {
257 public: 336 public:
258 explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {} 337 explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {}
259 338
260 virtual bool Matches(T x) const { return impl_.Matches(x); }
261
262 virtual void DescribeTo(::std::ostream* os) const { 339 virtual void DescribeTo(::std::ostream* os) const {
263 impl_.DescribeTo(os); 340 impl_.DescribeTo(os);
264 } 341 }
265 342
266 virtual void DescribeNegationTo(::std::ostream* os) const { 343 virtual void DescribeNegationTo(::std::ostream* os) const {
267 impl_.DescribeNegationTo(os); 344 impl_.DescribeNegationTo(os);
268 } 345 }
269 346
270 virtual void ExplainMatchResultTo(T x, ::std::ostream* os) const { 347 virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
271 using ::testing::internal::ExplainMatchResultTo; 348 return impl_.MatchAndExplain(x, listener);
272
273 // C++ uses Argument-Dependent Look-up (aka Koenig Look-up) to
274 // resolve the call to ExplainMatchResultTo() here. This
275 // means that if there's a ExplainMatchResultTo() function
276 // defined in the name space where class Impl is defined, it
277 // will be picked by the compiler as the better match.
278 // Otherwise the default implementation of it in
279 // ::testing::internal will be picked.
280 //
281 // This look-up rule lets a writer of a polymorphic matcher
282 // customize the behavior of ExplainMatchResultTo() when he
283 // cares to. Nothing needs to be done by the writer if he
284 // doesn't need to customize it.
285 ExplainMatchResultTo(impl_, x, os);
286 } 349 }
287 350
288 private: 351 private:
289 const Impl impl_; 352 const Impl impl_;
290 353
291 GTEST_DISALLOW_ASSIGN_(MonomorphicImpl); 354 GTEST_DISALLOW_ASSIGN_(MonomorphicImpl);
292 }; 355 };
293 356
294 Impl impl_; 357 Impl impl_;
295 358
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 } 446 }
384 447
385 // A<T>() returns a matcher that matches any value of type T. 448 // A<T>() returns a matcher that matches any value of type T.
386 template <typename T> 449 template <typename T>
387 Matcher<T> A(); 450 Matcher<T> A();
388 451
389 // Anything inside the 'internal' namespace IS INTERNAL IMPLEMENTATION 452 // Anything inside the 'internal' namespace IS INTERNAL IMPLEMENTATION
390 // and MUST NOT BE USED IN USER CODE!!! 453 // and MUST NOT BE USED IN USER CODE!!!
391 namespace internal { 454 namespace internal {
392 455
393 // Appends the explanation on the result of matcher.Matches(value) to 456 // If the explanation is not empty, prints it to the listener.
394 // os iff the explanation is not empty. 457 // 'listener' must not be NULL.
395 template <typename T> 458 inline void PrintIfNotEmpty(
396 void ExplainMatchResultAsNeededTo(const Matcher<T>& matcher, T value, 459 const internal::string& explanation, MatchResultListener* listener) {
397 ::std::ostream* os) { 460 if (explanation != "") {
398 ::std::stringstream reason; 461 *listener << ", " << explanation;
399 matcher.ExplainMatchResultTo(value, &reason);
400 const internal::string s = reason.str();
401 if (s != "") {
402 *os << " (" << s << ")";
403 } 462 }
404 } 463 }
405 464
465 // Matches the value against the given matcher, prints the value and explains
466 // the match result to the listener. Returns the match result.
467 // 'listener' must not be NULL.
468 // Value cannot be passed by const reference, because some matchers take a
469 // non-const argument.
470 template <typename Value, typename T>
471 bool MatchPrintAndExplain(Value& value, const Matcher<T>& matcher,
472 MatchResultListener* listener) {
473 if (!listener->IsInterested()) {
474 // If the listener is not interested, we do not need to construct the
475 // inner explanation.
476 return matcher.Matches(value);
477 }
478
479 StringMatchResultListener inner_listener;
480 const bool match = matcher.MatchAndExplain(value, &inner_listener);
481
482 UniversalPrint(value, listener->stream());
483 PrintIfNotEmpty(inner_listener.str(), listener);
484
485 return match;
486 }
487
488 // If the given string is not empty and os is not NULL, wraps the
489 // string inside a pair of parentheses and streams the result to os.
490 inline void StreamInParensAsNeeded(const internal::string& str,
491 ::std::ostream* os) {
492 if (!str.empty() && os != NULL) {
493 *os << " (" << str << ")";
494 }
495 }
496
406 // An internal helper class for doing compile-time loop on a tuple's 497 // An internal helper class for doing compile-time loop on a tuple's
407 // fields. 498 // fields.
408 template <size_t N> 499 template <size_t N>
409 class TuplePrefix { 500 class TuplePrefix {
410 public: 501 public:
411 // TuplePrefix<N>::Matches(matcher_tuple, value_tuple) returns true 502 // TuplePrefix<N>::Matches(matcher_tuple, value_tuple) returns true
412 // iff the first N fields of matcher_tuple matches the first N 503 // iff the first N fields of matcher_tuple matches the first N
413 // fields of value_tuple, respectively. 504 // fields of value_tuple, respectively.
414 template <typename MatcherTuple, typename ValueTuple> 505 template <typename MatcherTuple, typename ValueTuple>
415 static bool Matches(const MatcherTuple& matcher_tuple, 506 static bool Matches(const MatcherTuple& matcher_tuple,
(...skipping 16 matching lines...) Expand all
432 523
433 // First, describes failures in the first N - 1 fields. 524 // First, describes failures in the first N - 1 fields.
434 TuplePrefix<N - 1>::DescribeMatchFailuresTo(matchers, values, os); 525 TuplePrefix<N - 1>::DescribeMatchFailuresTo(matchers, values, os);
435 526
436 // Then describes the failure (if any) in the (N - 1)-th (0-based) 527 // Then describes the failure (if any) in the (N - 1)-th (0-based)
437 // field. 528 // field.
438 typename tuple_element<N - 1, MatcherTuple>::type matcher = 529 typename tuple_element<N - 1, MatcherTuple>::type matcher =
439 get<N - 1>(matchers); 530 get<N - 1>(matchers);
440 typedef typename tuple_element<N - 1, ValueTuple>::type Value; 531 typedef typename tuple_element<N - 1, ValueTuple>::type Value;
441 Value value = get<N - 1>(values); 532 Value value = get<N - 1>(values);
442 if (!matcher.Matches(value)) { 533 StringMatchResultListener listener;
534 if (!matcher.MatchAndExplain(value, &listener)) {
443 // TODO(wan): include in the message the name of the parameter 535 // TODO(wan): include in the message the name of the parameter
444 // as used in MOCK_METHOD*() when possible. 536 // as used in MOCK_METHOD*() when possible.
445 *os << " Expected arg #" << N - 1 << ": "; 537 *os << " Expected arg #" << N - 1 << ": ";
446 get<N - 1>(matchers).DescribeTo(os); 538 get<N - 1>(matchers).DescribeTo(os);
447 *os << "\n Actual: "; 539 *os << "\n Actual: ";
448 // We remove the reference in type Value to prevent the 540 // We remove the reference in type Value to prevent the
449 // universal printer from printing the address of value, which 541 // universal printer from printing the address of value, which
450 // isn't interesting to the user most of the time. The 542 // isn't interesting to the user most of the time. The
451 // matcher's ExplainMatchResultTo() method handles the case when 543 // matcher's MatchAndExplain() method handles the case when
452 // the address is interesting. 544 // the address is interesting.
453 internal::UniversalPrinter<GMOCK_REMOVE_REFERENCE_(Value)>:: 545 internal::UniversalPrinter<GMOCK_REMOVE_REFERENCE_(Value)>::
454 Print(value, os); 546 Print(value, os);
455 ExplainMatchResultAsNeededTo<Value>(matcher, value, os); 547
548 StreamInParensAsNeeded(listener.str(), os);
456 *os << "\n"; 549 *os << "\n";
457 } 550 }
458 } 551 }
459 }; 552 };
460 553
461 // The base case. 554 // The base case.
462 template <> 555 template <>
463 class TuplePrefix<0> { 556 class TuplePrefix<0> {
464 public: 557 public:
465 template <typename MatcherTuple, typename ValueTuple> 558 template <typename MatcherTuple, typename ValueTuple>
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
530 return Matcher<T>(new Impl(source_matcher)); 623 return Matcher<T>(new Impl(source_matcher));
531 } 624 }
532 625
533 private: 626 private:
534 class Impl : public MatcherInterface<T> { 627 class Impl : public MatcherInterface<T> {
535 public: 628 public:
536 explicit Impl(const Matcher<U>& source_matcher) 629 explicit Impl(const Matcher<U>& source_matcher)
537 : source_matcher_(source_matcher) {} 630 : source_matcher_(source_matcher) {}
538 631
539 // We delegate the matching logic to the source matcher. 632 // We delegate the matching logic to the source matcher.
540 virtual bool Matches(T x) const { 633 virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
541 return source_matcher_.Matches(static_cast<U>(x)); 634 return source_matcher_.MatchAndExplain(static_cast<U>(x), listener);
542 } 635 }
543 636
544 virtual void DescribeTo(::std::ostream* os) const { 637 virtual void DescribeTo(::std::ostream* os) const {
545 source_matcher_.DescribeTo(os); 638 source_matcher_.DescribeTo(os);
546 } 639 }
547 640
548 virtual void DescribeNegationTo(::std::ostream* os) const { 641 virtual void DescribeNegationTo(::std::ostream* os) const {
549 source_matcher_.DescribeNegationTo(os); 642 source_matcher_.DescribeNegationTo(os);
550 } 643 }
551 644
552 virtual void ExplainMatchResultTo(T x, ::std::ostream* os) const {
553 source_matcher_.ExplainMatchResultTo(static_cast<U>(x), os);
554 }
555
556 private: 645 private:
557 const Matcher<U> source_matcher_; 646 const Matcher<U> source_matcher_;
558 647
559 GTEST_DISALLOW_ASSIGN_(Impl); 648 GTEST_DISALLOW_ASSIGN_(Impl);
560 }; 649 };
561 }; 650 };
562 651
563 // This even more specialized version is used for efficiently casting 652 // This even more specialized version is used for efficiently casting
564 // a matcher to its own type. 653 // a matcher to its own type.
565 template <typename T> 654 template <typename T>
566 class MatcherCastImpl<T, Matcher<T> > { 655 class MatcherCastImpl<T, Matcher<T> > {
567 public: 656 public:
568 static Matcher<T> Cast(const Matcher<T>& matcher) { return matcher; } 657 static Matcher<T> Cast(const Matcher<T>& matcher) { return matcher; }
569 }; 658 };
570 659
571 // Implements A<T>(). 660 // Implements A<T>().
572 template <typename T> 661 template <typename T>
573 class AnyMatcherImpl : public MatcherInterface<T> { 662 class AnyMatcherImpl : public MatcherInterface<T> {
574 public: 663 public:
575 virtual bool Matches(T /* x */) const { return true; } 664 virtual bool MatchAndExplain(
665 T /* x */, MatchResultListener* /* listener */) const { return true; }
576 virtual void DescribeTo(::std::ostream* os) const { *os << "is anything"; } 666 virtual void DescribeTo(::std::ostream* os) const { *os << "is anything"; }
577 virtual void DescribeNegationTo(::std::ostream* os) const { 667 virtual void DescribeNegationTo(::std::ostream* os) const {
578 // This is mostly for completeness' safe, as it's not very useful 668 // This is mostly for completeness' safe, as it's not very useful
579 // to write Not(A<bool>()). However we cannot completely rule out 669 // to write Not(A<bool>()). However we cannot completely rule out
580 // such a possibility, and it doesn't hurt to be prepared. 670 // such a possibility, and it doesn't hurt to be prepared.
581 *os << "never matches"; 671 *os << "never matches";
582 } 672 }
583 }; 673 };
584 674
585 // Implements _, a matcher that matches any value of any 675 // Implements _, a matcher that matches any value of any
(...skipping 25 matching lines...) Expand all
611 explicit name##Matcher(const Rhs& rhs) : rhs_(rhs) {} \ 701 explicit name##Matcher(const Rhs& rhs) : rhs_(rhs) {} \
612 template <typename Lhs> \ 702 template <typename Lhs> \
613 operator Matcher<Lhs>() const { \ 703 operator Matcher<Lhs>() const { \
614 return MakeMatcher(new Impl<Lhs>(rhs_)); \ 704 return MakeMatcher(new Impl<Lhs>(rhs_)); \
615 } \ 705 } \
616 private: \ 706 private: \
617 template <typename Lhs> \ 707 template <typename Lhs> \
618 class Impl : public MatcherInterface<Lhs> { \ 708 class Impl : public MatcherInterface<Lhs> { \
619 public: \ 709 public: \
620 explicit Impl(const Rhs& rhs) : rhs_(rhs) {} \ 710 explicit Impl(const Rhs& rhs) : rhs_(rhs) {} \
621 virtual bool Matches(Lhs lhs) const { return lhs op rhs_; } \ 711 virtual bool MatchAndExplain(\
712 Lhs lhs, MatchResultListener* /* listener */) const { \
713 return lhs op rhs_; \
714 } \
622 virtual void DescribeTo(::std::ostream* os) const { \ 715 virtual void DescribeTo(::std::ostream* os) const { \
623 *os << "is " relation " "; \ 716 *os << "is " relation " "; \
624 UniversalPrinter<Rhs>::Print(rhs_, os); \ 717 UniversalPrinter<Rhs>::Print(rhs_, os); \
625 } \ 718 } \
626 virtual void DescribeNegationTo(::std::ostream* os) const { \ 719 virtual void DescribeNegationTo(::std::ostream* os) const { \
627 *os << "is not " relation " "; \ 720 *os << "is not " relation " "; \
628 UniversalPrinter<Rhs>::Print(rhs_, os); \ 721 UniversalPrinter<Rhs>::Print(rhs_, os); \
629 } \ 722 } \
630 private: \ 723 private: \
631 Rhs rhs_; \ 724 Rhs rhs_; \
(...skipping 12 matching lines...) Expand all
644 GMOCK_IMPLEMENT_COMPARISON_MATCHER_(Lt, <, "less than"); 737 GMOCK_IMPLEMENT_COMPARISON_MATCHER_(Lt, <, "less than");
645 GMOCK_IMPLEMENT_COMPARISON_MATCHER_(Ne, !=, "not equal to"); 738 GMOCK_IMPLEMENT_COMPARISON_MATCHER_(Ne, !=, "not equal to");
646 739
647 #undef GMOCK_IMPLEMENT_COMPARISON_MATCHER_ 740 #undef GMOCK_IMPLEMENT_COMPARISON_MATCHER_
648 741
649 // Implements the polymorphic IsNull() matcher, which matches any raw or smart 742 // Implements the polymorphic IsNull() matcher, which matches any raw or smart
650 // pointer that is NULL. 743 // pointer that is NULL.
651 class IsNullMatcher { 744 class IsNullMatcher {
652 public: 745 public:
653 template <typename Pointer> 746 template <typename Pointer>
654 bool Matches(const Pointer& p) const { return GetRawPointer(p) == NULL; } 747 bool MatchAndExplain(const Pointer& p,
748 MatchResultListener* /* listener */) const {
749 return GetRawPointer(p) == NULL;
750 }
655 751
656 void DescribeTo(::std::ostream* os) const { *os << "is NULL"; } 752 void DescribeTo(::std::ostream* os) const { *os << "is NULL"; }
657 void DescribeNegationTo(::std::ostream* os) const { 753 void DescribeNegationTo(::std::ostream* os) const {
658 *os << "is not NULL"; 754 *os << "is not NULL";
659 } 755 }
660 }; 756 };
661 757
662 // Implements the polymorphic NotNull() matcher, which matches any raw or smart 758 // Implements the polymorphic NotNull() matcher, which matches any raw or smart
663 // pointer that is not NULL. 759 // pointer that is not NULL.
664 class NotNullMatcher { 760 class NotNullMatcher {
665 public: 761 public:
666 template <typename Pointer> 762 template <typename Pointer>
667 bool Matches(const Pointer& p) const { return GetRawPointer(p) != NULL; } 763 bool MatchAndExplain(const Pointer& p,
764 MatchResultListener* /* listener */) const {
765 return GetRawPointer(p) != NULL;
766 }
668 767
669 void DescribeTo(::std::ostream* os) const { *os << "is not NULL"; } 768 void DescribeTo(::std::ostream* os) const { *os << "is not NULL"; }
670 void DescribeNegationTo(::std::ostream* os) const { 769 void DescribeNegationTo(::std::ostream* os) const {
671 *os << "is NULL"; 770 *os << "is NULL";
672 } 771 }
673 }; 772 };
674 773
675 // Ref(variable) matches any argument that is a reference to 774 // Ref(variable) matches any argument that is a reference to
676 // 'variable'. This matcher is polymorphic as it can match any 775 // 'variable'. This matcher is polymorphic as it can match any
677 // super type of the type of 'variable'. 776 // super type of the type of 'variable'.
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
710 // reference to a non-const reference. 809 // reference to a non-const reference.
711 return MakeMatcher(new Impl<Super>(object_)); 810 return MakeMatcher(new Impl<Super>(object_));
712 } 811 }
713 812
714 private: 813 private:
715 template <typename Super> 814 template <typename Super>
716 class Impl : public MatcherInterface<Super&> { 815 class Impl : public MatcherInterface<Super&> {
717 public: 816 public:
718 explicit Impl(Super& x) : object_(x) {} // NOLINT 817 explicit Impl(Super& x) : object_(x) {} // NOLINT
719 818
720 // Matches() takes a Super& (as opposed to const Super&) in 819 // MatchAndExplain() takes a Super& (as opposed to const Super&)
721 // order to match the interface MatcherInterface<Super&>. 820 // in order to match the interface MatcherInterface<Super&>.
722 virtual bool Matches(Super& x) const { return &x == &object_; } // NOLINT 821 virtual bool MatchAndExplain(
822 Super& x, MatchResultListener* listener) const {
823 *listener << "is located @" << static_cast<const void*>(&x);
824 return &x == &object_;
825 }
723 826
724 virtual void DescribeTo(::std::ostream* os) const { 827 virtual void DescribeTo(::std::ostream* os) const {
725 *os << "references the variable "; 828 *os << "references the variable ";
726 UniversalPrinter<Super&>::Print(object_, os); 829 UniversalPrinter<Super&>::Print(object_, os);
727 } 830 }
728 831
729 virtual void DescribeNegationTo(::std::ostream* os) const { 832 virtual void DescribeNegationTo(::std::ostream* os) const {
730 *os << "does not reference the variable "; 833 *os << "does not reference the variable ";
731 UniversalPrinter<Super&>::Print(object_, os); 834 UniversalPrinter<Super&>::Print(object_, os);
732 } 835 }
733 836
734 virtual void ExplainMatchResultTo(Super& x, // NOLINT
735 ::std::ostream* os) const {
736 *os << "is located @" << static_cast<const void*>(&x);
737 }
738
739 private: 837 private:
740 const Super& object_; 838 const Super& object_;
741 839
742 GTEST_DISALLOW_ASSIGN_(Impl); 840 GTEST_DISALLOW_ASSIGN_(Impl);
743 }; 841 };
744 842
745 T& object_; 843 T& object_;
746 844
747 GTEST_DISALLOW_ASSIGN_(RefMatcher); 845 GTEST_DISALLOW_ASSIGN_(RefMatcher);
748 }; 846 };
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
787 class StrEqualityMatcher { 885 class StrEqualityMatcher {
788 public: 886 public:
789 typedef typename StringType::const_pointer ConstCharPointer; 887 typedef typename StringType::const_pointer ConstCharPointer;
790 888
791 StrEqualityMatcher(const StringType& str, bool expect_eq, 889 StrEqualityMatcher(const StringType& str, bool expect_eq,
792 bool case_sensitive) 890 bool case_sensitive)
793 : string_(str), expect_eq_(expect_eq), case_sensitive_(case_sensitive) {} 891 : string_(str), expect_eq_(expect_eq), case_sensitive_(case_sensitive) {}
794 892
795 // When expect_eq_ is true, returns true iff s is equal to string_; 893 // When expect_eq_ is true, returns true iff s is equal to string_;
796 // otherwise returns true iff s is not equal to string_. 894 // otherwise returns true iff s is not equal to string_.
797 bool Matches(ConstCharPointer s) const { 895 bool MatchAndExplain(ConstCharPointer s,
896 MatchResultListener* listener) const {
798 if (s == NULL) { 897 if (s == NULL) {
799 return !expect_eq_; 898 return !expect_eq_;
800 } 899 }
801 return Matches(StringType(s)); 900 return MatchAndExplain(StringType(s), listener);
802 } 901 }
803 902
804 bool Matches(const StringType& s) const { 903 bool MatchAndExplain(const StringType& s,
904 MatchResultListener* /* listener */) const {
805 const bool eq = case_sensitive_ ? s == string_ : 905 const bool eq = case_sensitive_ ? s == string_ :
806 CaseInsensitiveStringEquals(s, string_); 906 CaseInsensitiveStringEquals(s, string_);
807 return expect_eq_ == eq; 907 return expect_eq_ == eq;
808 } 908 }
809 909
810 void DescribeTo(::std::ostream* os) const { 910 void DescribeTo(::std::ostream* os) const {
811 DescribeToHelper(expect_eq_, os); 911 DescribeToHelper(expect_eq_, os);
812 } 912 }
813 913
814 void DescribeNegationTo(::std::ostream* os) const { 914 void DescribeNegationTo(::std::ostream* os) const {
(...skipping 27 matching lines...) Expand all
842 class HasSubstrMatcher { 942 class HasSubstrMatcher {
843 public: 943 public:
844 typedef typename StringType::const_pointer ConstCharPointer; 944 typedef typename StringType::const_pointer ConstCharPointer;
845 945
846 explicit HasSubstrMatcher(const StringType& substring) 946 explicit HasSubstrMatcher(const StringType& substring)
847 : substring_(substring) {} 947 : substring_(substring) {}
848 948
849 // These overloaded methods allow HasSubstr(substring) to be used as a 949 // These overloaded methods allow HasSubstr(substring) to be used as a
850 // Matcher<T> as long as T can be converted to string. Returns true 950 // Matcher<T> as long as T can be converted to string. Returns true
851 // iff s contains substring_ as a substring. 951 // iff s contains substring_ as a substring.
852 bool Matches(ConstCharPointer s) const { 952 bool MatchAndExplain(ConstCharPointer s,
853 return s != NULL && Matches(StringType(s)); 953 MatchResultListener* listener) const {
954 return s != NULL && MatchAndExplain(StringType(s), listener);
854 } 955 }
855 956
856 bool Matches(const StringType& s) const { 957 bool MatchAndExplain(const StringType& s,
958 MatchResultListener* /* listener */) const {
857 return s.find(substring_) != StringType::npos; 959 return s.find(substring_) != StringType::npos;
858 } 960 }
859 961
860 // Describes what this matcher matches. 962 // Describes what this matcher matches.
861 void DescribeTo(::std::ostream* os) const { 963 void DescribeTo(::std::ostream* os) const {
862 *os << "has substring "; 964 *os << "has substring ";
863 UniversalPrinter<StringType>::Print(substring_, os); 965 UniversalPrinter<StringType>::Print(substring_, os);
864 } 966 }
865 967
866 void DescribeNegationTo(::std::ostream* os) const { 968 void DescribeNegationTo(::std::ostream* os) const {
(...skipping 14 matching lines...) Expand all
881 class StartsWithMatcher { 983 class StartsWithMatcher {
882 public: 984 public:
883 typedef typename StringType::const_pointer ConstCharPointer; 985 typedef typename StringType::const_pointer ConstCharPointer;
884 986
885 explicit StartsWithMatcher(const StringType& prefix) : prefix_(prefix) { 987 explicit StartsWithMatcher(const StringType& prefix) : prefix_(prefix) {
886 } 988 }
887 989
888 // These overloaded methods allow StartsWith(prefix) to be used as a 990 // These overloaded methods allow StartsWith(prefix) to be used as a
889 // Matcher<T> as long as T can be converted to string. Returns true 991 // Matcher<T> as long as T can be converted to string. Returns true
890 // iff s starts with prefix_. 992 // iff s starts with prefix_.
891 bool Matches(ConstCharPointer s) const { 993 bool MatchAndExplain(ConstCharPointer s,
892 return s != NULL && Matches(StringType(s)); 994 MatchResultListener* listener) const {
995 return s != NULL && MatchAndExplain(StringType(s), listener);
893 } 996 }
894 997
895 bool Matches(const StringType& s) const { 998 bool MatchAndExplain(const StringType& s,
999 MatchResultListener* /* listener */) const {
896 return s.length() >= prefix_.length() && 1000 return s.length() >= prefix_.length() &&
897 s.substr(0, prefix_.length()) == prefix_; 1001 s.substr(0, prefix_.length()) == prefix_;
898 } 1002 }
899 1003
900 void DescribeTo(::std::ostream* os) const { 1004 void DescribeTo(::std::ostream* os) const {
901 *os << "starts with "; 1005 *os << "starts with ";
902 UniversalPrinter<StringType>::Print(prefix_, os); 1006 UniversalPrinter<StringType>::Print(prefix_, os);
903 } 1007 }
904 1008
905 void DescribeNegationTo(::std::ostream* os) const { 1009 void DescribeNegationTo(::std::ostream* os) const {
(...skipping 13 matching lines...) Expand all
919 template <typename StringType> 1023 template <typename StringType>
920 class EndsWithMatcher { 1024 class EndsWithMatcher {
921 public: 1025 public:
922 typedef typename StringType::const_pointer ConstCharPointer; 1026 typedef typename StringType::const_pointer ConstCharPointer;
923 1027
924 explicit EndsWithMatcher(const StringType& suffix) : suffix_(suffix) {} 1028 explicit EndsWithMatcher(const StringType& suffix) : suffix_(suffix) {}
925 1029
926 // These overloaded methods allow EndsWith(suffix) to be used as a 1030 // These overloaded methods allow EndsWith(suffix) to be used as a
927 // Matcher<T> as long as T can be converted to string. Returns true 1031 // Matcher<T> as long as T can be converted to string. Returns true
928 // iff s ends with suffix_. 1032 // iff s ends with suffix_.
929 bool Matches(ConstCharPointer s) const { 1033 bool MatchAndExplain(ConstCharPointer s,
930 return s != NULL && Matches(StringType(s)); 1034 MatchResultListener* listener) const {
1035 return s != NULL && MatchAndExplain(StringType(s), listener);
931 } 1036 }
932 1037
933 bool Matches(const StringType& s) const { 1038 bool MatchAndExplain(const StringType& s,
1039 MatchResultListener* /* listener */) const {
934 return s.length() >= suffix_.length() && 1040 return s.length() >= suffix_.length() &&
935 s.substr(s.length() - suffix_.length()) == suffix_; 1041 s.substr(s.length() - suffix_.length()) == suffix_;
936 } 1042 }
937 1043
938 void DescribeTo(::std::ostream* os) const { 1044 void DescribeTo(::std::ostream* os) const {
939 *os << "ends with "; 1045 *os << "ends with ";
940 UniversalPrinter<StringType>::Print(suffix_, os); 1046 UniversalPrinter<StringType>::Print(suffix_, os);
941 } 1047 }
942 1048
943 void DescribeNegationTo(::std::ostream* os) const { 1049 void DescribeNegationTo(::std::ostream* os) const {
944 *os << "doesn't end with "; 1050 *os << "doesn't end with ";
945 UniversalPrinter<StringType>::Print(suffix_, os); 1051 UniversalPrinter<StringType>::Print(suffix_, os);
946 } 1052 }
947 1053
948 private: 1054 private:
949 const StringType suffix_; 1055 const StringType suffix_;
950 1056
951 GTEST_DISALLOW_ASSIGN_(EndsWithMatcher); 1057 GTEST_DISALLOW_ASSIGN_(EndsWithMatcher);
952 }; 1058 };
953 1059
954 #if GMOCK_HAS_REGEX
955
956 // Implements polymorphic matchers MatchesRegex(regex) and 1060 // Implements polymorphic matchers MatchesRegex(regex) and
957 // ContainsRegex(regex), which can be used as a Matcher<T> as long as 1061 // ContainsRegex(regex), which can be used as a Matcher<T> as long as
958 // T can be converted to a string. 1062 // T can be converted to a string.
959 class MatchesRegexMatcher { 1063 class MatchesRegexMatcher {
960 public: 1064 public:
961 MatchesRegexMatcher(const RE* regex, bool full_match) 1065 MatchesRegexMatcher(const RE* regex, bool full_match)
962 : regex_(regex), full_match_(full_match) {} 1066 : regex_(regex), full_match_(full_match) {}
963 1067
964 // These overloaded methods allow MatchesRegex(regex) to be used as 1068 // These overloaded methods allow MatchesRegex(regex) to be used as
965 // a Matcher<T> as long as T can be converted to string. Returns 1069 // a Matcher<T> as long as T can be converted to string. Returns
966 // true iff s matches regular expression regex. When full_match_ is 1070 // true iff s matches regular expression regex. When full_match_ is
967 // true, a full match is done; otherwise a partial match is done. 1071 // true, a full match is done; otherwise a partial match is done.
968 bool Matches(const char* s) const { 1072 bool MatchAndExplain(const char* s,
969 return s != NULL && Matches(internal::string(s)); 1073 MatchResultListener* listener) const {
1074 return s != NULL && MatchAndExplain(internal::string(s), listener);
970 } 1075 }
971 1076
972 bool Matches(const internal::string& s) const { 1077 bool MatchAndExplain(const internal::string& s,
1078 MatchResultListener* /* listener */) const {
973 return full_match_ ? RE::FullMatch(s, *regex_) : 1079 return full_match_ ? RE::FullMatch(s, *regex_) :
974 RE::PartialMatch(s, *regex_); 1080 RE::PartialMatch(s, *regex_);
975 } 1081 }
976 1082
977 void DescribeTo(::std::ostream* os) const { 1083 void DescribeTo(::std::ostream* os) const {
978 *os << (full_match_ ? "matches" : "contains") 1084 *os << (full_match_ ? "matches" : "contains")
979 << " regular expression "; 1085 << " regular expression ";
980 UniversalPrinter<internal::string>::Print(regex_->pattern(), os); 1086 UniversalPrinter<internal::string>::Print(regex_->pattern(), os);
981 } 1087 }
982 1088
983 void DescribeNegationTo(::std::ostream* os) const { 1089 void DescribeNegationTo(::std::ostream* os) const {
984 *os << "doesn't " << (full_match_ ? "match" : "contain") 1090 *os << "doesn't " << (full_match_ ? "match" : "contain")
985 << " regular expression "; 1091 << " regular expression ";
986 UniversalPrinter<internal::string>::Print(regex_->pattern(), os); 1092 UniversalPrinter<internal::string>::Print(regex_->pattern(), os);
987 } 1093 }
988 1094
989 private: 1095 private:
990 const internal::linked_ptr<const RE> regex_; 1096 const internal::linked_ptr<const RE> regex_;
991 const bool full_match_; 1097 const bool full_match_;
992 1098
993 GTEST_DISALLOW_ASSIGN_(MatchesRegexMatcher); 1099 GTEST_DISALLOW_ASSIGN_(MatchesRegexMatcher);
994 }; 1100 };
995 1101
996 #endif // GMOCK_HAS_REGEX
997
998 // Implements a matcher that compares the two fields of a 2-tuple 1102 // Implements a matcher that compares the two fields of a 2-tuple
999 // using one of the ==, <=, <, etc, operators. The two fields being 1103 // using one of the ==, <=, <, etc, operators. The two fields being
1000 // compared don't have to have the same type. 1104 // compared don't have to have the same type.
1001 // 1105 //
1002 // The matcher defined here is polymorphic (for example, Eq() can be 1106 // The matcher defined here is polymorphic (for example, Eq() can be
1003 // used to match a tuple<int, short>, a tuple<const long&, double>, 1107 // used to match a tuple<int, short>, a tuple<const long&, double>,
1004 // etc). Therefore we use a template type conversion operator in the 1108 // etc). Therefore we use a template type conversion operator in the
1005 // implementation. 1109 // implementation.
1006 // 1110 //
1007 // We define this as a macro in order to eliminate duplicated source 1111 // We define this as a macro in order to eliminate duplicated source
1008 // code. 1112 // code.
1009 #define GMOCK_IMPLEMENT_COMPARISON2_MATCHER_(name, op) \ 1113 #define GMOCK_IMPLEMENT_COMPARISON2_MATCHER_(name, op) \
1010 class name##2Matcher { \ 1114 class name##2Matcher { \
1011 public: \ 1115 public: \
1012 template <typename T1, typename T2> \ 1116 template <typename T1, typename T2> \
1013 operator Matcher<const ::std::tr1::tuple<T1, T2>&>() const { \ 1117 operator Matcher<const ::std::tr1::tuple<T1, T2>&>() const { \
1014 return MakeMatcher(new Impl<T1, T2>); \ 1118 return MakeMatcher(new Impl<T1, T2>); \
1015 } \ 1119 } \
1016 private: \ 1120 private: \
1017 template <typename T1, typename T2> \ 1121 template <typename T1, typename T2> \
1018 class Impl : public MatcherInterface<const ::std::tr1::tuple<T1, T2>&> { \ 1122 class Impl : public MatcherInterface<const ::std::tr1::tuple<T1, T2>&> { \
1019 public: \ 1123 public: \
1020 virtual bool Matches(const ::std::tr1::tuple<T1, T2>& args) const { \ 1124 virtual bool MatchAndExplain( \
1125 const ::std::tr1::tuple<T1, T2>& args, \
1126 MatchResultListener* /* listener */) const { \
1021 return ::std::tr1::get<0>(args) op ::std::tr1::get<1>(args); \ 1127 return ::std::tr1::get<0>(args) op ::std::tr1::get<1>(args); \
1022 } \ 1128 } \
1023 virtual void DescribeTo(::std::ostream* os) const { \ 1129 virtual void DescribeTo(::std::ostream* os) const { \
1024 *os << "are a pair (x, y) where x " #op " y"; \ 1130 *os << "are a pair (x, y) where x " #op " y"; \
1025 } \ 1131 } \
1026 virtual void DescribeNegationTo(::std::ostream* os) const { \ 1132 virtual void DescribeNegationTo(::std::ostream* os) const { \
1027 *os << "are a pair (x, y) where x " #op " y is false"; \ 1133 *os << "are a pair (x, y) where x " #op " y is false"; \
1028 } \ 1134 } \
1029 }; \ 1135 }; \
1030 } 1136 }
(...skipping 11 matching lines...) Expand all
1042 // Implements the Not(...) matcher for a particular argument type T. 1148 // Implements the Not(...) matcher for a particular argument type T.
1043 // We do not nest it inside the NotMatcher class template, as that 1149 // We do not nest it inside the NotMatcher class template, as that
1044 // will prevent different instantiations of NotMatcher from sharing 1150 // will prevent different instantiations of NotMatcher from sharing
1045 // the same NotMatcherImpl<T> class. 1151 // the same NotMatcherImpl<T> class.
1046 template <typename T> 1152 template <typename T>
1047 class NotMatcherImpl : public MatcherInterface<T> { 1153 class NotMatcherImpl : public MatcherInterface<T> {
1048 public: 1154 public:
1049 explicit NotMatcherImpl(const Matcher<T>& matcher) 1155 explicit NotMatcherImpl(const Matcher<T>& matcher)
1050 : matcher_(matcher) {} 1156 : matcher_(matcher) {}
1051 1157
1052 virtual bool Matches(T x) const { 1158 virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
1053 return !matcher_.Matches(x); 1159 return !matcher_.MatchAndExplain(x, listener);
1054 } 1160 }
1055 1161
1056 virtual void DescribeTo(::std::ostream* os) const { 1162 virtual void DescribeTo(::std::ostream* os) const {
1057 matcher_.DescribeNegationTo(os); 1163 matcher_.DescribeNegationTo(os);
1058 } 1164 }
1059 1165
1060 virtual void DescribeNegationTo(::std::ostream* os) const { 1166 virtual void DescribeNegationTo(::std::ostream* os) const {
1061 matcher_.DescribeTo(os); 1167 matcher_.DescribeTo(os);
1062 } 1168 }
1063 1169
1064 virtual void ExplainMatchResultTo(T x, ::std::ostream* os) const {
1065 matcher_.ExplainMatchResultTo(x, os);
1066 }
1067
1068 private: 1170 private:
1069 const Matcher<T> matcher_; 1171 const Matcher<T> matcher_;
1070 1172
1071 GTEST_DISALLOW_ASSIGN_(NotMatcherImpl); 1173 GTEST_DISALLOW_ASSIGN_(NotMatcherImpl);
1072 }; 1174 };
1073 1175
1074 // Implements the Not(m) matcher, which matches a value that doesn't 1176 // Implements the Not(m) matcher, which matches a value that doesn't
1075 // match matcher m. 1177 // match matcher m.
1076 template <typename InnerMatcher> 1178 template <typename InnerMatcher>
1077 class NotMatcher { 1179 class NotMatcher {
(...skipping 16 matching lines...) Expand all
1094 // Implements the AllOf(m1, m2) matcher for a particular argument type 1196 // Implements the AllOf(m1, m2) matcher for a particular argument type
1095 // T. We do not nest it inside the BothOfMatcher class template, as 1197 // T. We do not nest it inside the BothOfMatcher class template, as
1096 // that will prevent different instantiations of BothOfMatcher from 1198 // that will prevent different instantiations of BothOfMatcher from
1097 // sharing the same BothOfMatcherImpl<T> class. 1199 // sharing the same BothOfMatcherImpl<T> class.
1098 template <typename T> 1200 template <typename T>
1099 class BothOfMatcherImpl : public MatcherInterface<T> { 1201 class BothOfMatcherImpl : public MatcherInterface<T> {
1100 public: 1202 public:
1101 BothOfMatcherImpl(const Matcher<T>& matcher1, const Matcher<T>& matcher2) 1203 BothOfMatcherImpl(const Matcher<T>& matcher1, const Matcher<T>& matcher2)
1102 : matcher1_(matcher1), matcher2_(matcher2) {} 1204 : matcher1_(matcher1), matcher2_(matcher2) {}
1103 1205
1104 virtual bool Matches(T x) const {
1105 return matcher1_.Matches(x) && matcher2_.Matches(x);
1106 }
1107
1108 virtual void DescribeTo(::std::ostream* os) const { 1206 virtual void DescribeTo(::std::ostream* os) const {
1109 *os << "("; 1207 *os << "(";
1110 matcher1_.DescribeTo(os); 1208 matcher1_.DescribeTo(os);
1111 *os << ") and ("; 1209 *os << ") and (";
1112 matcher2_.DescribeTo(os); 1210 matcher2_.DescribeTo(os);
1113 *os << ")"; 1211 *os << ")";
1114 } 1212 }
1115 1213
1116 virtual void DescribeNegationTo(::std::ostream* os) const { 1214 virtual void DescribeNegationTo(::std::ostream* os) const {
1117 *os << "not "; 1215 *os << "not ";
1118 DescribeTo(os); 1216 DescribeTo(os);
1119 } 1217 }
1120 1218
1121 virtual void ExplainMatchResultTo(T x, ::std::ostream* os) const { 1219 virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
1122 if (Matches(x)) { 1220 // If either matcher1_ or matcher2_ doesn't match x, we only need
1123 // When both matcher1_ and matcher2_ match x, we need to 1221 // to explain why one of them fails.
1124 // explain why *both* of them match. 1222 StringMatchResultListener listener1;
1125 ::std::stringstream ss1; 1223 if (!matcher1_.MatchAndExplain(x, &listener1)) {
1126 matcher1_.ExplainMatchResultTo(x, &ss1); 1224 *listener << listener1.str();
1127 const internal::string s1 = ss1.str(); 1225 return false;
1226 }
1128 1227
1129 ::std::stringstream ss2; 1228 StringMatchResultListener listener2;
1130 matcher2_.ExplainMatchResultTo(x, &ss2); 1229 if (!matcher2_.MatchAndExplain(x, &listener2)) {
1131 const internal::string s2 = ss2.str(); 1230 *listener << listener2.str();
1231 return false;
1232 }
1132 1233
1133 if (s1 == "") { 1234 // Otherwise we need to explain why *both* of them match.
1134 *os << s2; 1235 const internal::string s1 = listener1.str();
1135 } else { 1236 const internal::string s2 = listener2.str();
1136 *os << s1; 1237
1137 if (s2 != "") { 1238 if (s1 == "") {
1138 *os << "; " << s2; 1239 *listener << s2;
1139 }
1140 }
1141 } else { 1240 } else {
1142 // Otherwise we only need to explain why *one* of them fails 1241 *listener << s1;
1143 // to match. 1242 if (s2 != "") {
1144 if (!matcher1_.Matches(x)) { 1243 *listener << "; " << s2;
1145 matcher1_.ExplainMatchResultTo(x, os);
1146 } else {
1147 matcher2_.ExplainMatchResultTo(x, os);
1148 } 1244 }
1149 } 1245 }
1246 return true;
1150 } 1247 }
1151 1248
1152 private: 1249 private:
1153 const Matcher<T> matcher1_; 1250 const Matcher<T> matcher1_;
1154 const Matcher<T> matcher2_; 1251 const Matcher<T> matcher2_;
1155 1252
1156 GTEST_DISALLOW_ASSIGN_(BothOfMatcherImpl); 1253 GTEST_DISALLOW_ASSIGN_(BothOfMatcherImpl);
1157 }; 1254 };
1158 1255
1159 // Used for implementing the AllOf(m_1, ..., m_n) matcher, which 1256 // Used for implementing the AllOf(m_1, ..., m_n) matcher, which
(...skipping 23 matching lines...) Expand all
1183 // Implements the AnyOf(m1, m2) matcher for a particular argument type 1280 // Implements the AnyOf(m1, m2) matcher for a particular argument type
1184 // T. We do not nest it inside the AnyOfMatcher class template, as 1281 // T. We do not nest it inside the AnyOfMatcher class template, as
1185 // that will prevent different instantiations of AnyOfMatcher from 1282 // that will prevent different instantiations of AnyOfMatcher from
1186 // sharing the same EitherOfMatcherImpl<T> class. 1283 // sharing the same EitherOfMatcherImpl<T> class.
1187 template <typename T> 1284 template <typename T>
1188 class EitherOfMatcherImpl : public MatcherInterface<T> { 1285 class EitherOfMatcherImpl : public MatcherInterface<T> {
1189 public: 1286 public:
1190 EitherOfMatcherImpl(const Matcher<T>& matcher1, const Matcher<T>& matcher2) 1287 EitherOfMatcherImpl(const Matcher<T>& matcher1, const Matcher<T>& matcher2)
1191 : matcher1_(matcher1), matcher2_(matcher2) {} 1288 : matcher1_(matcher1), matcher2_(matcher2) {}
1192 1289
1193 virtual bool Matches(T x) const {
1194 return matcher1_.Matches(x) || matcher2_.Matches(x);
1195 }
1196
1197 virtual void DescribeTo(::std::ostream* os) const { 1290 virtual void DescribeTo(::std::ostream* os) const {
1198 *os << "("; 1291 *os << "(";
1199 matcher1_.DescribeTo(os); 1292 matcher1_.DescribeTo(os);
1200 *os << ") or ("; 1293 *os << ") or (";
1201 matcher2_.DescribeTo(os); 1294 matcher2_.DescribeTo(os);
1202 *os << ")"; 1295 *os << ")";
1203 } 1296 }
1204 1297
1205 virtual void DescribeNegationTo(::std::ostream* os) const { 1298 virtual void DescribeNegationTo(::std::ostream* os) const {
1206 *os << "not "; 1299 *os << "not ";
1207 DescribeTo(os); 1300 DescribeTo(os);
1208 } 1301 }
1209 1302
1210 virtual void ExplainMatchResultTo(T x, ::std::ostream* os) const { 1303 virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
1211 if (Matches(x)) { 1304 // If either matcher1_ or matcher2_ matches x, we just need to
1212 // If either matcher1_ or matcher2_ matches x, we just need 1305 // explain why *one* of them matches.
1213 // to explain why *one* of them matches. 1306 StringMatchResultListener listener1;
1214 if (matcher1_.Matches(x)) { 1307 if (matcher1_.MatchAndExplain(x, &listener1)) {
1215 matcher1_.ExplainMatchResultTo(x, os); 1308 *listener << listener1.str();
1216 } else { 1309 return true;
1217 matcher2_.ExplainMatchResultTo(x, os); 1310 }
1218 } 1311
1312 StringMatchResultListener listener2;
1313 if (matcher2_.MatchAndExplain(x, &listener2)) {
1314 *listener << listener2.str();
1315 return true;
1316 }
1317
1318 // Otherwise we need to explain why *both* of them fail.
1319 const internal::string s1 = listener1.str();
1320 const internal::string s2 = listener2.str();
1321
1322 if (s1 == "") {
1323 *listener << s2;
1219 } else { 1324 } else {
1220 // Otherwise we need to explain why *neither* matches. 1325 *listener << s1;
1221 ::std::stringstream ss1; 1326 if (s2 != "") {
1222 matcher1_.ExplainMatchResultTo(x, &ss1); 1327 *listener << "; " << s2;
1223 const internal::string s1 = ss1.str();
1224
1225 ::std::stringstream ss2;
1226 matcher2_.ExplainMatchResultTo(x, &ss2);
1227 const internal::string s2 = ss2.str();
1228
1229 if (s1 == "") {
1230 *os << s2;
1231 } else {
1232 *os << s1;
1233 if (s2 != "") {
1234 *os << "; " << s2;
1235 }
1236 } 1328 }
1237 } 1329 }
1330 return false;
1238 } 1331 }
1239 1332
1240 private: 1333 private:
1241 const Matcher<T> matcher1_; 1334 const Matcher<T> matcher1_;
1242 const Matcher<T> matcher2_; 1335 const Matcher<T> matcher2_;
1243 1336
1244 GTEST_DISALLOW_ASSIGN_(EitherOfMatcherImpl); 1337 GTEST_DISALLOW_ASSIGN_(EitherOfMatcherImpl);
1245 }; 1338 };
1246 1339
1247 // Used for implementing the AnyOf(m_1, ..., m_n) matcher, which 1340 // Used for implementing the AnyOf(m_1, ..., m_n) matcher, which
(...skipping 26 matching lines...) Expand all
1274 template <typename Predicate> 1367 template <typename Predicate>
1275 class TrulyMatcher { 1368 class TrulyMatcher {
1276 public: 1369 public:
1277 explicit TrulyMatcher(Predicate pred) : predicate_(pred) {} 1370 explicit TrulyMatcher(Predicate pred) : predicate_(pred) {}
1278 1371
1279 // This method template allows Truly(pred) to be used as a matcher 1372 // This method template allows Truly(pred) to be used as a matcher
1280 // for type T where T is the argument type of predicate 'pred'. The 1373 // for type T where T is the argument type of predicate 'pred'. The
1281 // argument is passed by reference as the predicate may be 1374 // argument is passed by reference as the predicate may be
1282 // interested in the address of the argument. 1375 // interested in the address of the argument.
1283 template <typename T> 1376 template <typename T>
1284 bool Matches(T& x) const { // NOLINT 1377 bool MatchAndExplain(T& x, // NOLINT
1378 MatchResultListener* /* listener */) const {
1285 #if GTEST_OS_WINDOWS 1379 #if GTEST_OS_WINDOWS
1286 // MSVC warns about converting a value into bool (warning 4800). 1380 // MSVC warns about converting a value into bool (warning 4800).
1287 #pragma warning(push) // Saves the current warning state. 1381 #pragma warning(push) // Saves the current warning state.
1288 #pragma warning(disable:4800) // Temporarily disables warning 4800. 1382 #pragma warning(disable:4800) // Temporarily disables warning 4800.
1289 #endif // GTEST_OS_WINDOWS 1383 #endif // GTEST_OS_WINDOWS
1290 return predicate_(x); 1384 return predicate_(x);
1291 #if GTEST_OS_WINDOWS 1385 #if GTEST_OS_WINDOWS
1292 #pragma warning(pop) // Restores the warning state. 1386 #pragma warning(pop) // Restores the warning state.
1293 #endif // GTEST_OS_WINDOWS 1387 #endif // GTEST_OS_WINDOWS
1294 } 1388 }
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
1360 // We convert matcher_ to a Matcher<const T&> *now* instead of 1454 // We convert matcher_ to a Matcher<const T&> *now* instead of
1361 // when the PredicateFormatterFromMatcher object was constructed, 1455 // when the PredicateFormatterFromMatcher object was constructed,
1362 // as matcher_ may be polymorphic (e.g. NotNull()) and we won't 1456 // as matcher_ may be polymorphic (e.g. NotNull()) and we won't
1363 // know which type to instantiate it to until we actually see the 1457 // know which type to instantiate it to until we actually see the
1364 // type of x here. 1458 // type of x here.
1365 // 1459 //
1366 // We write MatcherCast<const T&>(matcher_) instead of 1460 // We write MatcherCast<const T&>(matcher_) instead of
1367 // Matcher<const T&>(matcher_), as the latter won't compile when 1461 // Matcher<const T&>(matcher_), as the latter won't compile when
1368 // matcher_ has type Matcher<T> (e.g. An<int>()). 1462 // matcher_ has type Matcher<T> (e.g. An<int>()).
1369 const Matcher<const T&> matcher = MatcherCast<const T&>(matcher_); 1463 const Matcher<const T&> matcher = MatcherCast<const T&>(matcher_);
1370 if (matcher.Matches(x)) { 1464 StringMatchResultListener listener;
1465 if (matcher.MatchAndExplain(x, &listener)) {
1371 return AssertionSuccess(); 1466 return AssertionSuccess();
1372 } else { 1467 } else {
1373 ::std::stringstream ss; 1468 ::std::stringstream ss;
1374 ss << "Value of: " << value_text << "\n" 1469 ss << "Value of: " << value_text << "\n"
1375 << "Expected: "; 1470 << "Expected: ";
1376 matcher.DescribeTo(&ss); 1471 matcher.DescribeTo(&ss);
1377 ss << "\n Actual: "; 1472 ss << "\n Actual: ";
1378 UniversalPrinter<T>::Print(x, &ss); 1473 UniversalPrinter<T>::Print(x, &ss);
1379 ExplainMatchResultAsNeededTo<const T&>(matcher, x, &ss); 1474 StreamInParensAsNeeded(listener.str(), &ss);
1380 return AssertionFailure(Message() << ss.str()); 1475 return AssertionFailure(Message() << ss.str());
1381 } 1476 }
1382 } 1477 }
1383 1478
1384 private: 1479 private:
1385 const M matcher_; 1480 const M matcher_;
1386 1481
1387 GTEST_DISALLOW_ASSIGN_(PredicateFormatterFromMatcher); 1482 GTEST_DISALLOW_ASSIGN_(PredicateFormatterFromMatcher);
1388 }; 1483 };
1389 1484
(...skipping 20 matching lines...) Expand all
1410 FloatingEqMatcher(FloatType rhs, bool nan_eq_nan) : 1505 FloatingEqMatcher(FloatType rhs, bool nan_eq_nan) :
1411 rhs_(rhs), nan_eq_nan_(nan_eq_nan) {} 1506 rhs_(rhs), nan_eq_nan_(nan_eq_nan) {}
1412 1507
1413 // Implements floating point equality matcher as a Matcher<T>. 1508 // Implements floating point equality matcher as a Matcher<T>.
1414 template <typename T> 1509 template <typename T>
1415 class Impl : public MatcherInterface<T> { 1510 class Impl : public MatcherInterface<T> {
1416 public: 1511 public:
1417 Impl(FloatType rhs, bool nan_eq_nan) : 1512 Impl(FloatType rhs, bool nan_eq_nan) :
1418 rhs_(rhs), nan_eq_nan_(nan_eq_nan) {} 1513 rhs_(rhs), nan_eq_nan_(nan_eq_nan) {}
1419 1514
1420 virtual bool Matches(T value) const { 1515 virtual bool MatchAndExplain(T value,
1516 MatchResultListener* /* listener */) const {
1421 const FloatingPoint<FloatType> lhs(value), rhs(rhs_); 1517 const FloatingPoint<FloatType> lhs(value), rhs(rhs_);
1422 1518
1423 // Compares NaNs first, if nan_eq_nan_ is true. 1519 // Compares NaNs first, if nan_eq_nan_ is true.
1424 if (nan_eq_nan_ && lhs.is_nan()) { 1520 if (nan_eq_nan_ && lhs.is_nan()) {
1425 return rhs.is_nan(); 1521 return rhs.is_nan();
1426 } 1522 }
1427 1523
1428 return lhs.AlmostEquals(rhs); 1524 return lhs.AlmostEquals(rhs);
1429 } 1525 }
1430 1526
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
1518 // The monomorphic implementation that works for a particular pointer type. 1614 // The monomorphic implementation that works for a particular pointer type.
1519 template <typename Pointer> 1615 template <typename Pointer>
1520 class Impl : public MatcherInterface<Pointer> { 1616 class Impl : public MatcherInterface<Pointer> {
1521 public: 1617 public:
1522 typedef typename PointeeOf<GMOCK_REMOVE_CONST_( // NOLINT 1618 typedef typename PointeeOf<GMOCK_REMOVE_CONST_( // NOLINT
1523 GMOCK_REMOVE_REFERENCE_(Pointer))>::type Pointee; 1619 GMOCK_REMOVE_REFERENCE_(Pointer))>::type Pointee;
1524 1620
1525 explicit Impl(const InnerMatcher& matcher) 1621 explicit Impl(const InnerMatcher& matcher)
1526 : matcher_(MatcherCast<const Pointee&>(matcher)) {} 1622 : matcher_(MatcherCast<const Pointee&>(matcher)) {}
1527 1623
1528 virtual bool Matches(Pointer p) const {
1529 return GetRawPointer(p) != NULL && matcher_.Matches(*p);
1530 }
1531
1532 virtual void DescribeTo(::std::ostream* os) const { 1624 virtual void DescribeTo(::std::ostream* os) const {
1533 *os << "points to a value that "; 1625 *os << "points to a value that ";
1534 matcher_.DescribeTo(os); 1626 matcher_.DescribeTo(os);
1535 } 1627 }
1536 1628
1537 virtual void DescribeNegationTo(::std::ostream* os) const { 1629 virtual void DescribeNegationTo(::std::ostream* os) const {
1538 *os << "does not point to a value that "; 1630 *os << "does not point to a value that ";
1539 matcher_.DescribeTo(os); 1631 matcher_.DescribeTo(os);
1540 } 1632 }
1541 1633
1542 virtual void ExplainMatchResultTo(Pointer pointer, 1634 virtual bool MatchAndExplain(Pointer pointer,
1543 ::std::ostream* os) const { 1635 MatchResultListener* listener) const {
1544 if (GetRawPointer(pointer) == NULL) 1636 if (GetRawPointer(pointer) == NULL)
1545 return; 1637 return false;
1546 1638
1547 ::std::stringstream ss; 1639 *listener << "which points to ";
1548 matcher_.ExplainMatchResultTo(*pointer, &ss); 1640 return MatchPrintAndExplain(*pointer, matcher_, listener);
1549 const internal::string s = ss.str();
1550 if (s != "") {
1551 *os << "points to a value that " << s;
1552 }
1553 } 1641 }
1554 1642
1555 private: 1643 private:
1556 const Matcher<const Pointee&> matcher_; 1644 const Matcher<const Pointee&> matcher_;
1557 1645
1558 GTEST_DISALLOW_ASSIGN_(Impl); 1646 GTEST_DISALLOW_ASSIGN_(Impl);
1559 }; 1647 };
1560 1648
1561 const InnerMatcher matcher_; 1649 const InnerMatcher matcher_;
1562 1650
1563 GTEST_DISALLOW_ASSIGN_(PointeeMatcher); 1651 GTEST_DISALLOW_ASSIGN_(PointeeMatcher);
1564 }; 1652 };
1565 1653
1566 // Implements the Field() matcher for matching a field (i.e. member 1654 // Implements the Field() matcher for matching a field (i.e. member
1567 // variable) of an object. 1655 // variable) of an object.
1568 template <typename Class, typename FieldType> 1656 template <typename Class, typename FieldType>
1569 class FieldMatcher { 1657 class FieldMatcher {
1570 public: 1658 public:
1571 FieldMatcher(FieldType Class::*field, 1659 FieldMatcher(FieldType Class::*field,
1572 const Matcher<const FieldType&>& matcher) 1660 const Matcher<const FieldType&>& matcher)
1573 : field_(field), matcher_(matcher) {} 1661 : field_(field), matcher_(matcher) {}
1574 1662
1575 // Returns true iff the inner matcher matches obj.field.
1576 bool Matches(const Class& obj) const {
1577 return matcher_.Matches(obj.*field_);
1578 }
1579
1580 // Returns true iff the inner matcher matches obj->field.
1581 bool Matches(const Class* p) const {
1582 return (p != NULL) && matcher_.Matches(p->*field_);
1583 }
1584
1585 void DescribeTo(::std::ostream* os) const { 1663 void DescribeTo(::std::ostream* os) const {
1586 *os << "the given field "; 1664 *os << "is an object whose given field ";
1587 matcher_.DescribeTo(os); 1665 matcher_.DescribeTo(os);
1588 } 1666 }
1589 1667
1590 void DescribeNegationTo(::std::ostream* os) const { 1668 void DescribeNegationTo(::std::ostream* os) const {
1591 *os << "the given field "; 1669 *os << "is an object whose given field ";
1592 matcher_.DescribeNegationTo(os); 1670 matcher_.DescribeNegationTo(os);
1593 } 1671 }
1594 1672
1595 // The first argument of ExplainMatchResultTo() is needed to help 1673 template <typename T>
1596 // Symbian's C++ compiler choose which overload to use. Its type is 1674 bool MatchAndExplain(const T& value, MatchResultListener* listener) const {
1597 // true_type iff the Field() matcher is used to match a pointer. 1675 return MatchAndExplainImpl(
1598 void ExplainMatchResultTo(false_type /* is_not_pointer */, const Class& obj, 1676 typename ::testing::internal::
1599 ::std::ostream* os) const { 1677 is_pointer<GMOCK_REMOVE_CONST_(T)>::type(),
1600 ::std::stringstream ss; 1678 value, listener);
1601 matcher_.ExplainMatchResultTo(obj.*field_, &ss);
1602 const internal::string s = ss.str();
1603 if (s != "") {
1604 *os << "the given field " << s;
1605 }
1606 }
1607
1608 void ExplainMatchResultTo(true_type /* is_pointer */, const Class* p,
1609 ::std::ostream* os) const {
1610 if (p != NULL) {
1611 // Since *p has a field, it must be a class/struct/union type
1612 // and thus cannot be a pointer. Therefore we pass false_type()
1613 // as the first argument.
1614 ExplainMatchResultTo(false_type(), *p, os);
1615 }
1616 } 1679 }
1617 1680
1618 private: 1681 private:
1682 // The first argument of MatchAndExplainImpl() is needed to help
1683 // Symbian's C++ compiler choose which overload to use. Its type is
1684 // true_type iff the Field() matcher is used to match a pointer.
1685 bool MatchAndExplainImpl(false_type /* is_not_pointer */, const Class& obj,
1686 MatchResultListener* listener) const {
1687 *listener << "whose given field is ";
1688 return MatchPrintAndExplain(obj.*field_, matcher_, listener);
1689 }
1690
1691 bool MatchAndExplainImpl(true_type /* is_pointer */, const Class* p,
1692 MatchResultListener* listener) const {
1693 if (p == NULL)
1694 return false;
1695
1696 *listener << "which points to an object ";
1697 // Since *p has a field, it must be a class/struct/union type and
1698 // thus cannot be a pointer. Therefore we pass false_type() as
1699 // the first argument.
1700 return MatchAndExplainImpl(false_type(), *p, listener);
1701 }
1702
1619 const FieldType Class::*field_; 1703 const FieldType Class::*field_;
1620 const Matcher<const FieldType&> matcher_; 1704 const Matcher<const FieldType&> matcher_;
1621 1705
1622 GTEST_DISALLOW_ASSIGN_(FieldMatcher); 1706 GTEST_DISALLOW_ASSIGN_(FieldMatcher);
1623 }; 1707 };
1624 1708
1625 // Explains the result of matching an object or pointer against a field matcher.
1626 template <typename Class, typename FieldType, typename T>
1627 void ExplainMatchResultTo(const FieldMatcher<Class, FieldType>& matcher,
1628 const T& value, ::std::ostream* os) {
1629 matcher.ExplainMatchResultTo(
1630 typename ::testing::internal::is_pointer<T>::type(), value, os);
1631 }
1632
1633 // Implements the Property() matcher for matching a property 1709 // Implements the Property() matcher for matching a property
1634 // (i.e. return value of a getter method) of an object. 1710 // (i.e. return value of a getter method) of an object.
1635 template <typename Class, typename PropertyType> 1711 template <typename Class, typename PropertyType>
1636 class PropertyMatcher { 1712 class PropertyMatcher {
1637 public: 1713 public:
1638 // The property may have a reference type, so 'const PropertyType&' 1714 // The property may have a reference type, so 'const PropertyType&'
1639 // may cause double references and fail to compile. That's why we 1715 // may cause double references and fail to compile. That's why we
1640 // need GMOCK_REFERENCE_TO_CONST, which works regardless of 1716 // need GMOCK_REFERENCE_TO_CONST, which works regardless of
1641 // PropertyType being a reference or not. 1717 // PropertyType being a reference or not.
1642 typedef GMOCK_REFERENCE_TO_CONST_(PropertyType) RefToConstProperty; 1718 typedef GMOCK_REFERENCE_TO_CONST_(PropertyType) RefToConstProperty;
1643 1719
1644 PropertyMatcher(PropertyType (Class::*property)() const, 1720 PropertyMatcher(PropertyType (Class::*property)() const,
1645 const Matcher<RefToConstProperty>& matcher) 1721 const Matcher<RefToConstProperty>& matcher)
1646 : property_(property), matcher_(matcher) {} 1722 : property_(property), matcher_(matcher) {}
1647 1723
1648 // Returns true iff obj.property() matches the inner matcher.
1649 bool Matches(const Class& obj) const {
1650 return matcher_.Matches((obj.*property_)());
1651 }
1652
1653 // Returns true iff p->property() matches the inner matcher.
1654 bool Matches(const Class* p) const {
1655 return (p != NULL) && matcher_.Matches((p->*property_)());
1656 }
1657
1658 void DescribeTo(::std::ostream* os) const { 1724 void DescribeTo(::std::ostream* os) const {
1659 *os << "the given property "; 1725 *os << "is an object whose given property ";
1660 matcher_.DescribeTo(os); 1726 matcher_.DescribeTo(os);
1661 } 1727 }
1662 1728
1663 void DescribeNegationTo(::std::ostream* os) const { 1729 void DescribeNegationTo(::std::ostream* os) const {
1664 *os << "the given property "; 1730 *os << "is an object whose given property ";
1665 matcher_.DescribeNegationTo(os); 1731 matcher_.DescribeNegationTo(os);
1666 } 1732 }
1667 1733
1668 // The first argument of ExplainMatchResultTo() is needed to help 1734 template <typename T>
1669 // Symbian's C++ compiler choose which overload to use. Its type is 1735 bool MatchAndExplain(const T&value, MatchResultListener* listener) const {
1670 // true_type iff the Property() matcher is used to match a pointer. 1736 return MatchAndExplainImpl(
1671 void ExplainMatchResultTo(false_type /* is_not_pointer */, const Class& obj, 1737 typename ::testing::internal::
1672 ::std::ostream* os) const { 1738 is_pointer<GMOCK_REMOVE_CONST_(T)>::type(),
1673 ::std::stringstream ss; 1739 value, listener);
1674 matcher_.ExplainMatchResultTo((obj.*property_)(), &ss);
1675 const internal::string s = ss.str();
1676 if (s != "") {
1677 *os << "the given property " << s;
1678 }
1679 }
1680
1681 void ExplainMatchResultTo(true_type /* is_pointer */, const Class* p,
1682 ::std::ostream* os) const {
1683 if (p != NULL) {
1684 // Since *p has a property method, it must be a
1685 // class/struct/union type and thus cannot be a pointer.
1686 // Therefore we pass false_type() as the first argument.
1687 ExplainMatchResultTo(false_type(), *p, os);
1688 }
1689 } 1740 }
1690 1741
1691 private: 1742 private:
1743 // The first argument of MatchAndExplainImpl() is needed to help
1744 // Symbian's C++ compiler choose which overload to use. Its type is
1745 // true_type iff the Property() matcher is used to match a pointer.
1746 bool MatchAndExplainImpl(false_type /* is_not_pointer */, const Class& obj,
1747 MatchResultListener* listener) const {
1748 *listener << "whose given property is ";
1749 // Cannot pass the return value (for example, int) to MatchPrintAndExplain,
1750 // which takes a non-const reference as argument.
1751 RefToConstProperty result = (obj.*property_)();
1752 return MatchPrintAndExplain(result, matcher_, listener);
1753 }
1754
1755 bool MatchAndExplainImpl(true_type /* is_pointer */, const Class* p,
1756 MatchResultListener* listener) const {
1757 if (p == NULL)
1758 return false;
1759
1760 *listener << "which points to an object ";
1761 // Since *p has a property method, it must be a class/struct/union
1762 // type and thus cannot be a pointer. Therefore we pass
1763 // false_type() as the first argument.
1764 return MatchAndExplainImpl(false_type(), *p, listener);
1765 }
1766
1692 PropertyType (Class::*property_)() const; 1767 PropertyType (Class::*property_)() const;
1693 const Matcher<RefToConstProperty> matcher_; 1768 const Matcher<RefToConstProperty> matcher_;
1694 1769
1695 GTEST_DISALLOW_ASSIGN_(PropertyMatcher); 1770 GTEST_DISALLOW_ASSIGN_(PropertyMatcher);
1696 }; 1771 };
1697 1772
1698 // Explains the result of matching an object or pointer against a
1699 // property matcher.
1700 template <typename Class, typename PropertyType, typename T>
1701 void ExplainMatchResultTo(const PropertyMatcher<Class, PropertyType>& matcher,
1702 const T& value, ::std::ostream* os) {
1703 matcher.ExplainMatchResultTo(
1704 typename ::testing::internal::is_pointer<T>::type(), value, os);
1705 }
1706
1707 // Type traits specifying various features of different functors for ResultOf. 1773 // Type traits specifying various features of different functors for ResultOf.
1708 // The default template specifies features for functor objects. 1774 // The default template specifies features for functor objects.
1709 // Functor classes have to typedef argument_type and result_type 1775 // Functor classes have to typedef argument_type and result_type
1710 // to be compatible with ResultOf. 1776 // to be compatible with ResultOf.
1711 template <typename Functor> 1777 template <typename Functor>
1712 struct CallableTraits { 1778 struct CallableTraits {
1713 typedef typename Functor::result_type ResultType; 1779 typedef typename Functor::result_type ResultType;
1714 typedef Functor StorageType; 1780 typedef Functor StorageType;
1715 1781
1716 static void CheckIsValid(Functor /* functor */) {} 1782 static void CheckIsValid(Functor /* functor */) {}
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
1752 } 1818 }
1753 1819
1754 private: 1820 private:
1755 typedef typename CallableTraits<Callable>::StorageType CallableStorageType; 1821 typedef typename CallableTraits<Callable>::StorageType CallableStorageType;
1756 1822
1757 template <typename T> 1823 template <typename T>
1758 class Impl : public MatcherInterface<T> { 1824 class Impl : public MatcherInterface<T> {
1759 public: 1825 public:
1760 Impl(CallableStorageType callable, const Matcher<ResultType>& matcher) 1826 Impl(CallableStorageType callable, const Matcher<ResultType>& matcher)
1761 : callable_(callable), matcher_(matcher) {} 1827 : callable_(callable), matcher_(matcher) {}
1762 // Returns true iff callable_(obj) matches the inner matcher.
1763 // The calling syntax is different for different types of callables
1764 // so we abstract it in CallableTraits<Callable>::Invoke().
1765 virtual bool Matches(T obj) const {
1766 return matcher_.Matches(
1767 CallableTraits<Callable>::template Invoke<T>(callable_, obj));
1768 }
1769 1828
1770 virtual void DescribeTo(::std::ostream* os) const { 1829 virtual void DescribeTo(::std::ostream* os) const {
1771 *os << "result of the given callable "; 1830 *os << "is mapped by the given callable to a value that ";
1772 matcher_.DescribeTo(os); 1831 matcher_.DescribeTo(os);
1773 } 1832 }
1774 1833
1775 virtual void DescribeNegationTo(::std::ostream* os) const { 1834 virtual void DescribeNegationTo(::std::ostream* os) const {
1776 *os << "result of the given callable "; 1835 *os << "is mapped by the given callable to a value that ";
1777 matcher_.DescribeNegationTo(os); 1836 matcher_.DescribeNegationTo(os);
1778 } 1837 }
1779 1838
1780 virtual void ExplainMatchResultTo(T obj, ::std::ostream* os) const { 1839 virtual bool MatchAndExplain(T obj, MatchResultListener* listener) const {
1781 ::std::stringstream ss; 1840 *listener << "which is mapped by the given callable to ";
1782 matcher_.ExplainMatchResultTo( 1841 // Cannot pass the return value (for example, int) to
1783 CallableTraits<Callable>::template Invoke<T>(callable_, obj), 1842 // MatchPrintAndExplain, which takes a non-const reference as argument.
1784 &ss); 1843 ResultType result =
1785 const internal::string s = ss.str(); 1844 CallableTraits<Callable>::template Invoke<T>(callable_, obj);
1786 if (s != "") 1845 return MatchPrintAndExplain(result, matcher_, listener);
1787 *os << "result of the given callable " << s;
1788 } 1846 }
1789 1847
1790 private: 1848 private:
1791 // Functors often define operator() as non-const method even though 1849 // Functors often define operator() as non-const method even though
1792 // they are actualy stateless. But we need to use them even when 1850 // they are actualy stateless. But we need to use them even when
1793 // 'this' is a const pointer. It's the user's responsibility not to 1851 // 'this' is a const pointer. It's the user's responsibility not to
1794 // use stateful callables with ResultOf(), which does't guarantee 1852 // use stateful callables with ResultOf(), which does't guarantee
1795 // how many times the callable will be invoked. 1853 // how many times the callable will be invoked.
1796 mutable CallableStorageType callable_; 1854 mutable CallableStorageType callable_;
1797 const Matcher<ResultType> matcher_; 1855 const Matcher<ResultType> matcher_;
1798 1856
1799 GTEST_DISALLOW_ASSIGN_(Impl); 1857 GTEST_DISALLOW_ASSIGN_(Impl);
1800 }; // class Impl 1858 }; // class Impl
1801 1859
1802 const CallableStorageType callable_; 1860 const CallableStorageType callable_;
1803 const Matcher<ResultType> matcher_; 1861 const Matcher<ResultType> matcher_;
1804 1862
1805 GTEST_DISALLOW_ASSIGN_(ResultOfMatcher); 1863 GTEST_DISALLOW_ASSIGN_(ResultOfMatcher);
1806 }; 1864 };
1807 1865
1808 // Explains the result of matching a value against a functor matcher.
1809 template <typename T, typename Callable>
1810 void ExplainMatchResultTo(const ResultOfMatcher<Callable>& matcher,
1811 T obj, ::std::ostream* os) {
1812 matcher.ExplainMatchResultTo(obj, os);
1813 }
1814
1815 // Implements an equality matcher for any STL-style container whose elements 1866 // Implements an equality matcher for any STL-style container whose elements
1816 // support ==. This matcher is like Eq(), but its failure explanations provide 1867 // support ==. This matcher is like Eq(), but its failure explanations provide
1817 // more detailed information that is useful when the container is used as a set. 1868 // more detailed information that is useful when the container is used as a set.
1818 // The failure message reports elements that are in one of the operands but not 1869 // The failure message reports elements that are in one of the operands but not
1819 // the other. The failure messages do not report duplicate or out-of-order 1870 // the other. The failure messages do not report duplicate or out-of-order
1820 // elements in the containers (which don't properly matter to sets, but can 1871 // elements in the containers (which don't properly matter to sets, but can
1821 // occur if the containers are vectors or lists, for example). 1872 // occur if the containers are vectors or lists, for example).
1822 // 1873 //
1823 // Uses the container's const_iterator, value_type, operator ==, 1874 // Uses the container's const_iterator, value_type, operator ==,
1824 // begin(), and end(). 1875 // begin(), and end().
1825 template <typename Container> 1876 template <typename Container>
1826 class ContainerEqMatcher { 1877 class ContainerEqMatcher {
1827 public: 1878 public:
1828 typedef internal::StlContainerView<Container> View; 1879 typedef internal::StlContainerView<Container> View;
1829 typedef typename View::type StlContainer; 1880 typedef typename View::type StlContainer;
1830 typedef typename View::const_reference StlContainerReference; 1881 typedef typename View::const_reference StlContainerReference;
1831 1882
1832 // We make a copy of rhs in case the elements in it are modified 1883 // We make a copy of rhs in case the elements in it are modified
1833 // after this matcher is created. 1884 // after this matcher is created.
1834 explicit ContainerEqMatcher(const Container& rhs) : rhs_(View::Copy(rhs)) { 1885 explicit ContainerEqMatcher(const Container& rhs) : rhs_(View::Copy(rhs)) {
1835 // Makes sure the user doesn't instantiate this class template 1886 // Makes sure the user doesn't instantiate this class template
1836 // with a const or reference type. 1887 // with a const or reference type.
1837 testing::StaticAssertTypeEq<Container, 1888 testing::StaticAssertTypeEq<Container,
1838 GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(Container))>(); 1889 GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(Container))>();
1839 } 1890 }
1840 1891
1841 template <typename LhsContainer>
1842 bool Matches(const LhsContainer& lhs) const {
1843 // GMOCK_REMOVE_CONST_() is needed to work around an MSVC 8.0 bug
1844 // that causes LhsContainer to be a const type sometimes.
1845 typedef internal::StlContainerView<GMOCK_REMOVE_CONST_(LhsContainer)>
1846 LhsView;
1847 StlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
1848 return lhs_stl_container == rhs_;
1849 }
1850 void DescribeTo(::std::ostream* os) const { 1892 void DescribeTo(::std::ostream* os) const {
1851 *os << "equals "; 1893 *os << "equals ";
1852 UniversalPrinter<StlContainer>::Print(rhs_, os); 1894 UniversalPrinter<StlContainer>::Print(rhs_, os);
1853 } 1895 }
1854 void DescribeNegationTo(::std::ostream* os) const { 1896 void DescribeNegationTo(::std::ostream* os) const {
1855 *os << "does not equal "; 1897 *os << "does not equal ";
1856 UniversalPrinter<StlContainer>::Print(rhs_, os); 1898 UniversalPrinter<StlContainer>::Print(rhs_, os);
1857 } 1899 }
1858 1900
1859 template <typename LhsContainer> 1901 template <typename LhsContainer>
1860 void ExplainMatchResultTo(const LhsContainer& lhs, 1902 bool MatchAndExplain(const LhsContainer& lhs,
1861 ::std::ostream* os) const { 1903 MatchResultListener* listener) const {
1862 // GMOCK_REMOVE_CONST_() is needed to work around an MSVC 8.0 bug 1904 // GMOCK_REMOVE_CONST_() is needed to work around an MSVC 8.0 bug
1863 // that causes LhsContainer to be a const type sometimes. 1905 // that causes LhsContainer to be a const type sometimes.
1864 typedef internal::StlContainerView<GMOCK_REMOVE_CONST_(LhsContainer)> 1906 typedef internal::StlContainerView<GMOCK_REMOVE_CONST_(LhsContainer)>
1865 LhsView; 1907 LhsView;
1866 typedef typename LhsView::type LhsStlContainer; 1908 typedef typename LhsView::type LhsStlContainer;
1867 StlContainerReference lhs_stl_container = LhsView::ConstReference(lhs); 1909 StlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
1910 if (lhs_stl_container == rhs_)
1911 return true;
1868 1912
1869 // Something is different. Check for missing values first. 1913 ::std::ostream* const os = listener->stream();
1870 bool printed_header = false; 1914 if (os != NULL) {
1871 for (typename LhsStlContainer::const_iterator it = 1915 // Something is different. Check for missing values first.
1872 lhs_stl_container.begin(); 1916 bool printed_header = false;
1873 it != lhs_stl_container.end(); ++it) { 1917 for (typename LhsStlContainer::const_iterator it =
1874 if (internal::ArrayAwareFind(rhs_.begin(), rhs_.end(), *it) == 1918 lhs_stl_container.begin();
1875 rhs_.end()) { 1919 it != lhs_stl_container.end(); ++it) {
1876 if (printed_header) { 1920 if (internal::ArrayAwareFind(rhs_.begin(), rhs_.end(), *it) ==
1877 *os << ", "; 1921 rhs_.end()) {
1878 } else { 1922 if (printed_header) {
1879 *os << "Only in actual: "; 1923 *os << ", ";
1880 printed_header = true; 1924 } else {
1925 *os << "Only in actual: ";
1926 printed_header = true;
1927 }
1928 UniversalPrinter<typename LhsStlContainer::value_type>::
1929 Print(*it, os);
1881 } 1930 }
1882 UniversalPrinter<typename LhsStlContainer::value_type>::Print(*it, os); 1931 }
1932
1933 // Now check for extra values.
1934 bool printed_header2 = false;
1935 for (typename StlContainer::const_iterator it = rhs_.begin();
1936 it != rhs_.end(); ++it) {
1937 if (internal::ArrayAwareFind(
1938 lhs_stl_container.begin(), lhs_stl_container.end(), *it) ==
1939 lhs_stl_container.end()) {
1940 if (printed_header2) {
1941 *os << ", ";
1942 } else {
1943 *os << (printed_header ? "; not" : "Not") << " in actual: ";
1944 printed_header2 = true;
1945 }
1946 UniversalPrinter<typename StlContainer::value_type>::Print(*it, os);
1947 }
1883 } 1948 }
1884 } 1949 }
1885 1950
1886 // Now check for extra values. 1951 return false;
1887 bool printed_header2 = false;
1888 for (typename StlContainer::const_iterator it = rhs_.begin();
1889 it != rhs_.end(); ++it) {
1890 if (internal::ArrayAwareFind(
1891 lhs_stl_container.begin(), lhs_stl_container.end(), *it) ==
1892 lhs_stl_container.end()) {
1893 if (printed_header2) {
1894 *os << ", ";
1895 } else {
1896 *os << (printed_header ? "; not" : "Not") << " in actual: ";
1897 printed_header2 = true;
1898 }
1899 UniversalPrinter<typename StlContainer::value_type>::Print(*it, os);
1900 }
1901 }
1902 } 1952 }
1903 1953
1904 private: 1954 private:
1905 const StlContainer rhs_; 1955 const StlContainer rhs_;
1906 1956
1907 GTEST_DISALLOW_ASSIGN_(ContainerEqMatcher); 1957 GTEST_DISALLOW_ASSIGN_(ContainerEqMatcher);
1908 }; 1958 };
1909 1959
1910 template <typename LhsContainer, typename Container>
1911 void ExplainMatchResultTo(const ContainerEqMatcher<Container>& matcher,
1912 const LhsContainer& lhs,
1913 ::std::ostream* os) {
1914 matcher.ExplainMatchResultTo(lhs, os);
1915 }
1916
1917 // Implements Contains(element_matcher) for the given argument type Container. 1960 // Implements Contains(element_matcher) for the given argument type Container.
1918 template <typename Container> 1961 template <typename Container>
1919 class ContainsMatcherImpl : public MatcherInterface<Container> { 1962 class ContainsMatcherImpl : public MatcherInterface<Container> {
1920 public: 1963 public:
1921 typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(Container)) RawContainer; 1964 typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(Container)) RawContainer;
1922 typedef StlContainerView<RawContainer> View; 1965 typedef StlContainerView<RawContainer> View;
1923 typedef typename View::type StlContainer; 1966 typedef typename View::type StlContainer;
1924 typedef typename View::const_reference StlContainerReference; 1967 typedef typename View::const_reference StlContainerReference;
1925 typedef typename StlContainer::value_type Element; 1968 typedef typename StlContainer::value_type Element;
1926 1969
1927 template <typename InnerMatcher> 1970 template <typename InnerMatcher>
1928 explicit ContainsMatcherImpl(InnerMatcher inner_matcher) 1971 explicit ContainsMatcherImpl(InnerMatcher inner_matcher)
1929 : inner_matcher_( 1972 : inner_matcher_(
1930 testing::SafeMatcherCast<const Element&>(inner_matcher)) {} 1973 testing::SafeMatcherCast<const Element&>(inner_matcher)) {}
1931 1974
1932 // Returns true iff 'container' matches.
1933 virtual bool Matches(Container container) const {
1934 StlContainerReference stl_container = View::ConstReference(container);
1935 for (typename StlContainer::const_iterator it = stl_container.begin();
1936 it != stl_container.end(); ++it) {
1937 if (inner_matcher_.Matches(*it))
1938 return true;
1939 }
1940 return false;
1941 }
1942
1943 // Describes what this matcher does. 1975 // Describes what this matcher does.
1944 virtual void DescribeTo(::std::ostream* os) const { 1976 virtual void DescribeTo(::std::ostream* os) const {
1945 *os << "contains at least one element that "; 1977 *os << "contains at least one element that ";
1946 inner_matcher_.DescribeTo(os); 1978 inner_matcher_.DescribeTo(os);
1947 } 1979 }
1948 1980
1949 // Describes what the negation of this matcher does. 1981 // Describes what the negation of this matcher does.
1950 virtual void DescribeNegationTo(::std::ostream* os) const { 1982 virtual void DescribeNegationTo(::std::ostream* os) const {
1951 *os << "doesn't contain any element that "; 1983 *os << "doesn't contain any element that ";
1952 inner_matcher_.DescribeTo(os); 1984 inner_matcher_.DescribeTo(os);
1953 } 1985 }
1954 1986
1955 // Explains why 'container' matches, or doesn't match, this matcher. 1987 virtual bool MatchAndExplain(Container container,
1956 virtual void ExplainMatchResultTo(Container container, 1988 MatchResultListener* listener) const {
1957 ::std::ostream* os) const {
1958 StlContainerReference stl_container = View::ConstReference(container); 1989 StlContainerReference stl_container = View::ConstReference(container);
1959 1990 size_t i = 0;
1960 // We need to explain which (if any) element matches inner_matcher_. 1991 for (typename StlContainer::const_iterator it = stl_container.begin();
1961 typename StlContainer::const_iterator it = stl_container.begin(); 1992 it != stl_container.end(); ++it, ++i) {
1962 for (size_t i = 0; it != stl_container.end(); ++it, ++i) {
1963 if (inner_matcher_.Matches(*it)) { 1993 if (inner_matcher_.Matches(*it)) {
1964 *os << "element " << i << " matches"; 1994 *listener << "element " << i << " matches";
1965 return; 1995 return true;
1966 } 1996 }
1967 } 1997 }
1998 return false;
1968 } 1999 }
1969 2000
1970 private: 2001 private:
1971 const Matcher<const Element&> inner_matcher_; 2002 const Matcher<const Element&> inner_matcher_;
1972 2003
1973 GTEST_DISALLOW_ASSIGN_(ContainsMatcherImpl); 2004 GTEST_DISALLOW_ASSIGN_(ContainsMatcherImpl);
1974 }; 2005 };
1975 2006
1976 // Implements polymorphic Contains(element_matcher). 2007 // Implements polymorphic Contains(element_matcher).
1977 template <typename M> 2008 template <typename M>
(...skipping 22 matching lines...) Expand all
2000 typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(PairType)) RawPairType; 2031 typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(PairType)) RawPairType;
2001 typedef typename RawPairType::first_type KeyType; 2032 typedef typename RawPairType::first_type KeyType;
2002 2033
2003 template <typename InnerMatcher> 2034 template <typename InnerMatcher>
2004 explicit KeyMatcherImpl(InnerMatcher inner_matcher) 2035 explicit KeyMatcherImpl(InnerMatcher inner_matcher)
2005 : inner_matcher_( 2036 : inner_matcher_(
2006 testing::SafeMatcherCast<const KeyType&>(inner_matcher)) { 2037 testing::SafeMatcherCast<const KeyType&>(inner_matcher)) {
2007 } 2038 }
2008 2039
2009 // Returns true iff 'key_value.first' (the key) matches the inner matcher. 2040 // Returns true iff 'key_value.first' (the key) matches the inner matcher.
2010 virtual bool Matches(PairType key_value) const { 2041 virtual bool MatchAndExplain(PairType key_value,
2011 return inner_matcher_.Matches(key_value.first); 2042 MatchResultListener* listener) const {
2043 return inner_matcher_.MatchAndExplain(key_value.first, listener);
2012 } 2044 }
2013 2045
2014 // Describes what this matcher does. 2046 // Describes what this matcher does.
2015 virtual void DescribeTo(::std::ostream* os) const { 2047 virtual void DescribeTo(::std::ostream* os) const {
2016 *os << "has a key that "; 2048 *os << "has a key that ";
2017 inner_matcher_.DescribeTo(os); 2049 inner_matcher_.DescribeTo(os);
2018 } 2050 }
2019 2051
2020 // Describes what the negation of this matcher does. 2052 // Describes what the negation of this matcher does.
2021 virtual void DescribeNegationTo(::std::ostream* os) const { 2053 virtual void DescribeNegationTo(::std::ostream* os) const {
2022 *os << "doesn't have a key that "; 2054 *os << "doesn't have a key that ";
2023 inner_matcher_.DescribeTo(os); 2055 inner_matcher_.DescribeTo(os);
2024 } 2056 }
2025 2057
2026 // Explains why 'key_value' matches, or doesn't match, this matcher.
2027 virtual void ExplainMatchResultTo(PairType key_value,
2028 ::std::ostream* os) const {
2029 inner_matcher_.ExplainMatchResultTo(key_value.first, os);
2030 }
2031
2032 private: 2058 private:
2033 const Matcher<const KeyType&> inner_matcher_; 2059 const Matcher<const KeyType&> inner_matcher_;
2034 2060
2035 GTEST_DISALLOW_ASSIGN_(KeyMatcherImpl); 2061 GTEST_DISALLOW_ASSIGN_(KeyMatcherImpl);
2036 }; 2062 };
2037 2063
2038 // Implements polymorphic Key(matcher_for_key). 2064 // Implements polymorphic Key(matcher_for_key).
2039 template <typename M> 2065 template <typename M>
2040 class KeyMatcher { 2066 class KeyMatcher {
2041 public: 2067 public:
(...skipping 20 matching lines...) Expand all
2062 typedef typename RawPairType::second_type SecondType; 2088 typedef typename RawPairType::second_type SecondType;
2063 2089
2064 template <typename FirstMatcher, typename SecondMatcher> 2090 template <typename FirstMatcher, typename SecondMatcher>
2065 PairMatcherImpl(FirstMatcher first_matcher, SecondMatcher second_matcher) 2091 PairMatcherImpl(FirstMatcher first_matcher, SecondMatcher second_matcher)
2066 : first_matcher_( 2092 : first_matcher_(
2067 testing::SafeMatcherCast<const FirstType&>(first_matcher)), 2093 testing::SafeMatcherCast<const FirstType&>(first_matcher)),
2068 second_matcher_( 2094 second_matcher_(
2069 testing::SafeMatcherCast<const SecondType&>(second_matcher)) { 2095 testing::SafeMatcherCast<const SecondType&>(second_matcher)) {
2070 } 2096 }
2071 2097
2072 // Returns true iff 'a_pair.first' matches first_matcher and 'a_pair.second'
2073 // matches second_matcher.
2074 virtual bool Matches(PairType a_pair) const {
2075 return first_matcher_.Matches(a_pair.first) &&
2076 second_matcher_.Matches(a_pair.second);
2077 }
2078
2079 // Describes what this matcher does. 2098 // Describes what this matcher does.
2080 virtual void DescribeTo(::std::ostream* os) const { 2099 virtual void DescribeTo(::std::ostream* os) const {
2081 *os << "has a first field that "; 2100 *os << "has a first field that ";
2082 first_matcher_.DescribeTo(os); 2101 first_matcher_.DescribeTo(os);
2083 *os << ", and has a second field that "; 2102 *os << ", and has a second field that ";
2084 second_matcher_.DescribeTo(os); 2103 second_matcher_.DescribeTo(os);
2085 } 2104 }
2086 2105
2087 // Describes what the negation of this matcher does. 2106 // Describes what the negation of this matcher does.
2088 virtual void DescribeNegationTo(::std::ostream* os) const { 2107 virtual void DescribeNegationTo(::std::ostream* os) const {
2089 *os << "has a first field that "; 2108 *os << "has a first field that ";
2090 first_matcher_.DescribeNegationTo(os); 2109 first_matcher_.DescribeNegationTo(os);
2091 *os << ", or has a second field that "; 2110 *os << ", or has a second field that ";
2092 second_matcher_.DescribeNegationTo(os); 2111 second_matcher_.DescribeNegationTo(os);
2093 } 2112 }
2094 2113
2095 // Explains why 'a_pair' matches, or doesn't match, this matcher. 2114 // Returns true iff 'a_pair.first' matches first_matcher and 'a_pair.second'
2096 virtual void ExplainMatchResultTo(PairType a_pair, 2115 // matches second_matcher.
2097 ::std::ostream* os) const { 2116 virtual bool MatchAndExplain(PairType a_pair,
2098 ::std::stringstream ss1; 2117 MatchResultListener* listener) const {
2099 first_matcher_.ExplainMatchResultTo(a_pair.first, &ss1); 2118 if (!listener->IsInterested()) {
2100 internal::string s1 = ss1.str(); 2119 // If the listener is not interested, we don't need to construct the
2101 if (s1 != "") { 2120 // explanation.
2102 s1 = "the first field " + s1; 2121 return first_matcher_.Matches(a_pair.first) &&
2122 second_matcher_.Matches(a_pair.second);
2103 } 2123 }
2104 2124 StringMatchResultListener first_inner_listener;
2105 ::std::stringstream ss2; 2125 if (!first_matcher_.MatchAndExplain(a_pair.first,
2106 second_matcher_.ExplainMatchResultTo(a_pair.second, &ss2); 2126 &first_inner_listener)) {
2107 internal::string s2 = ss2.str(); 2127 *listener << "whose first field does not match";
2108 if (s2 != "") { 2128 PrintIfNotEmpty(first_inner_listener.str(), listener);
2109 s2 = "the second field " + s2; 2129 return false;
2110 } 2130 }
2111 2131 StringMatchResultListener second_inner_listener;
2112 *os << s1; 2132 if (!second_matcher_.MatchAndExplain(a_pair.second,
2113 if (s1 != "" && s2 != "") { 2133 &second_inner_listener)) {
2114 *os << ", and "; 2134 *listener << "whose second field does not match";
2135 PrintIfNotEmpty(second_inner_listener.str(), listener);
2136 return false;
2115 } 2137 }
2116 *os << s2; 2138 ExplainSuccess(first_inner_listener.str(), second_inner_listener.str(),
2139 listener);
2140 return true;
2117 } 2141 }
2118 2142
2119 private: 2143 private:
2144 void ExplainSuccess(const internal::string& first_explanation,
2145 const internal::string& second_explanation,
2146 MatchResultListener* listener) const {
2147 *listener << "whose both fields match";
2148 if (first_explanation != "") {
2149 *listener << ", where the first field is a value " << first_explanation;
2150 }
2151 if (second_explanation != "") {
2152 *listener << ", ";
2153 if (first_explanation != "") {
2154 *listener << "and ";
2155 } else {
2156 *listener << "where ";
2157 }
2158 *listener << "the second field is a value " << second_explanation;
2159 }
2160 }
2161
2120 const Matcher<const FirstType&> first_matcher_; 2162 const Matcher<const FirstType&> first_matcher_;
2121 const Matcher<const SecondType&> second_matcher_; 2163 const Matcher<const SecondType&> second_matcher_;
2122 2164
2123 GTEST_DISALLOW_ASSIGN_(PairMatcherImpl); 2165 GTEST_DISALLOW_ASSIGN_(PairMatcherImpl);
2124 }; 2166 };
2125 2167
2126 // Implements polymorphic Pair(first_matcher, second_matcher). 2168 // Implements polymorphic Pair(first_matcher, second_matcher).
2127 template <typename FirstMatcher, typename SecondMatcher> 2169 template <typename FirstMatcher, typename SecondMatcher>
2128 class PairMatcher { 2170 class PairMatcher {
2129 public: 2171 public:
(...skipping 28 matching lines...) Expand all
2158 // element matchers. 2200 // element matchers.
2159 template <typename InputIter> 2201 template <typename InputIter>
2160 ElementsAreMatcherImpl(InputIter first, size_t a_count) { 2202 ElementsAreMatcherImpl(InputIter first, size_t a_count) {
2161 matchers_.reserve(a_count); 2203 matchers_.reserve(a_count);
2162 InputIter it = first; 2204 InputIter it = first;
2163 for (size_t i = 0; i != a_count; ++i, ++it) { 2205 for (size_t i = 0; i != a_count; ++i, ++it) {
2164 matchers_.push_back(MatcherCast<const Element&>(*it)); 2206 matchers_.push_back(MatcherCast<const Element&>(*it));
2165 } 2207 }
2166 } 2208 }
2167 2209
2168 // Returns true iff 'container' matches.
2169 virtual bool Matches(Container container) const {
2170 StlContainerReference stl_container = View::ConstReference(container);
2171 if (stl_container.size() != count())
2172 return false;
2173
2174 typename StlContainer::const_iterator it = stl_container.begin();
2175 for (size_t i = 0; i != count(); ++it, ++i) {
2176 if (!matchers_[i].Matches(*it))
2177 return false;
2178 }
2179
2180 return true;
2181 }
2182
2183 // Describes what this matcher does. 2210 // Describes what this matcher does.
2184 virtual void DescribeTo(::std::ostream* os) const { 2211 virtual void DescribeTo(::std::ostream* os) const {
2185 if (count() == 0) { 2212 if (count() == 0) {
2186 *os << "is empty"; 2213 *os << "is empty";
2187 } else if (count() == 1) { 2214 } else if (count() == 1) {
2188 *os << "has 1 element that "; 2215 *os << "has 1 element that ";
2189 matchers_[0].DescribeTo(os); 2216 matchers_[0].DescribeTo(os);
2190 } else { 2217 } else {
2191 *os << "has " << Elements(count()) << " where\n"; 2218 *os << "has " << Elements(count()) << " where\n";
2192 for (size_t i = 0; i != count(); ++i) { 2219 for (size_t i = 0; i != count(); ++i) {
(...skipping 16 matching lines...) Expand all
2209 *os << "does not have " << Elements(count()) << ", or\n"; 2236 *os << "does not have " << Elements(count()) << ", or\n";
2210 for (size_t i = 0; i != count(); ++i) { 2237 for (size_t i = 0; i != count(); ++i) {
2211 *os << "element " << i << " "; 2238 *os << "element " << i << " ";
2212 matchers_[i].DescribeNegationTo(os); 2239 matchers_[i].DescribeNegationTo(os);
2213 if (i + 1 < count()) { 2240 if (i + 1 < count()) {
2214 *os << ", or\n"; 2241 *os << ", or\n";
2215 } 2242 }
2216 } 2243 }
2217 } 2244 }
2218 2245
2219 // Explains why 'container' matches, or doesn't match, this matcher. 2246 virtual bool MatchAndExplain(Container container,
2220 virtual void ExplainMatchResultTo(Container container, 2247 MatchResultListener* listener) const {
2221 ::std::ostream* os) const {
2222 StlContainerReference stl_container = View::ConstReference(container); 2248 StlContainerReference stl_container = View::ConstReference(container);
2223 if (Matches(container)) { 2249 const size_t actual_count = stl_container.size();
2224 // We need to explain why *each* element matches (the obvious 2250 if (actual_count != count()) {
2225 // ones can be skipped). 2251 // The element count doesn't match. If the container is empty,
2252 // there's no need to explain anything as Google Mock already
2253 // prints the empty container. Otherwise we just need to show
2254 // how many elements there actually are.
2255 if (actual_count != 0) {
2256 *listener << "has " << Elements(actual_count);
2257 }
2258 return false;
2259 }
2226 2260
2227 bool reason_printed = false; 2261 typename StlContainer::const_iterator it = stl_container.begin();
2228 typename StlContainer::const_iterator it = stl_container.begin(); 2262 // explanations[i] is the explanation of the element at index i.
2229 for (size_t i = 0; i != count(); ++it, ++i) { 2263 std::vector<internal::string> explanations(count());
2230 ::std::stringstream ss; 2264 for (size_t i = 0; i != count(); ++it, ++i) {
2231 matchers_[i].ExplainMatchResultTo(*it, &ss); 2265 StringMatchResultListener s;
2266 if (matchers_[i].MatchAndExplain(*it, &s)) {
2267 explanations[i] = s.str();
2268 } else {
2269 // The container has the right size but the i-th element
2270 // doesn't match its expectation.
2271 *listener << "element " << i << " doesn't match";
2232 2272
2233 const string s = ss.str(); 2273 StreamInParensAsNeeded(s.str(), listener->stream());
2234 if (!s.empty()) { 2274 return false;
2235 if (reason_printed) {
2236 *os << ",\n";
2237 }
2238 *os << "element " << i << " " << s;
2239 reason_printed = true;
2240 }
2241 }
2242 } else {
2243 // We need to explain why the container doesn't match.
2244 const size_t actual_count = stl_container.size();
2245 if (actual_count != count()) {
2246 // The element count doesn't match. If the container is
2247 // empty, there's no need to explain anything as Google Mock
2248 // already prints the empty container. Otherwise we just need
2249 // to show how many elements there actually are.
2250 if (actual_count != 0) {
2251 *os << "has " << Elements(actual_count);
2252 }
2253 return;
2254 }
2255
2256 // The container has the right size but at least one element
2257 // doesn't match expectation. We need to find this element and
2258 // explain why it doesn't match.
2259 typename StlContainer::const_iterator it = stl_container.begin();
2260 for (size_t i = 0; i != count(); ++it, ++i) {
2261 if (matchers_[i].Matches(*it)) {
2262 continue;
2263 }
2264
2265 *os << "element " << i << " doesn't match";
2266
2267 ::std::stringstream ss;
2268 matchers_[i].ExplainMatchResultTo(*it, &ss);
2269 const string s = ss.str();
2270 if (!s.empty()) {
2271 *os << " (" << s << ")";
2272 }
2273 return;
2274 } 2275 }
2275 } 2276 }
2277
2278 // Every element matches its expectation. We need to explain why
2279 // (the obvious ones can be skipped).
2280
2281 bool reason_printed = false;
2282 for (size_t i = 0; i != count(); ++i) {
2283 const internal::string& s = explanations[i];
2284 if (!s.empty()) {
2285 if (reason_printed) {
2286 *listener << ",\n";
2287 }
2288 *listener << "element " << i << " " << s;
2289 reason_printed = true;
2290 }
2291 }
2292
2293 return true;
2276 } 2294 }
2277 2295
2278 private: 2296 private:
2279 static Message Elements(size_t count) { 2297 static Message Elements(size_t count) {
2280 return Message() << count << (count == 1 ? " element" : " elements"); 2298 return Message() << count << (count == 1 ? " element" : " elements");
2281 } 2299 }
2282 2300
2283 size_t count() const { return matchers_.size(); } 2301 size_t count() const { return matchers_.size(); }
2284 std::vector<Matcher<const Element&> > matchers_; 2302 std::vector<Matcher<const Element&> > matchers_;
2285 2303
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after
2602 prefix)); 2620 prefix));
2603 } 2621 }
2604 2622
2605 // Matches a string that ends with 'suffix' (case-sensitive). 2623 // Matches a string that ends with 'suffix' (case-sensitive).
2606 inline PolymorphicMatcher<internal::EndsWithMatcher<internal::string> > 2624 inline PolymorphicMatcher<internal::EndsWithMatcher<internal::string> >
2607 EndsWith(const internal::string& suffix) { 2625 EndsWith(const internal::string& suffix) {
2608 return MakePolymorphicMatcher(internal::EndsWithMatcher<internal::string>( 2626 return MakePolymorphicMatcher(internal::EndsWithMatcher<internal::string>(
2609 suffix)); 2627 suffix));
2610 } 2628 }
2611 2629
2612 #ifdef GMOCK_HAS_REGEX
2613
2614 // Matches a string that fully matches regular expression 'regex'. 2630 // Matches a string that fully matches regular expression 'regex'.
2615 // The matcher takes ownership of 'regex'. 2631 // The matcher takes ownership of 'regex'.
2616 inline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex( 2632 inline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex(
2617 const internal::RE* regex) { 2633 const internal::RE* regex) {
2618 return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, true)); 2634 return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, true));
2619 } 2635 }
2620 inline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex( 2636 inline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex(
2621 const internal::string& regex) { 2637 const internal::string& regex) {
2622 return MatchesRegex(new internal::RE(regex)); 2638 return MatchesRegex(new internal::RE(regex));
2623 } 2639 }
2624 2640
2625 // Matches a string that contains regular expression 'regex'. 2641 // Matches a string that contains regular expression 'regex'.
2626 // The matcher takes ownership of 'regex'. 2642 // The matcher takes ownership of 'regex'.
2627 inline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex( 2643 inline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex(
2628 const internal::RE* regex) { 2644 const internal::RE* regex) {
2629 return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, false)); 2645 return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, false));
2630 } 2646 }
2631 inline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex( 2647 inline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex(
2632 const internal::string& regex) { 2648 const internal::string& regex) {
2633 return ContainsRegex(new internal::RE(regex)); 2649 return ContainsRegex(new internal::RE(regex));
2634 } 2650 }
2635 2651
2636 #endif // GMOCK_HAS_REGEX
2637
2638 #if GTEST_HAS_GLOBAL_WSTRING || GTEST_HAS_STD_WSTRING 2652 #if GTEST_HAS_GLOBAL_WSTRING || GTEST_HAS_STD_WSTRING
2639 // Wide string matchers. 2653 // Wide string matchers.
2640 2654
2641 // Matches a string equal to str. 2655 // Matches a string equal to str.
2642 inline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> > 2656 inline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> >
2643 StrEq(const internal::wstring& str) { 2657 StrEq(const internal::wstring& str) {
2644 return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>( 2658 return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>(
2645 str, true, true)); 2659 str, true, true));
2646 } 2660 }
2647 2661
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
2804 inline PolymorphicMatcher<internal::TrulyMatcher<Predicate> > 2818 inline PolymorphicMatcher<internal::TrulyMatcher<Predicate> >
2805 Truly(Predicate pred) { 2819 Truly(Predicate pred) {
2806 return MakePolymorphicMatcher(internal::TrulyMatcher<Predicate>(pred)); 2820 return MakePolymorphicMatcher(internal::TrulyMatcher<Predicate>(pred));
2807 } 2821 }
2808 2822
2809 // Returns a matcher that matches an equal container. 2823 // Returns a matcher that matches an equal container.
2810 // This matcher behaves like Eq(), but in the event of mismatch lists the 2824 // This matcher behaves like Eq(), but in the event of mismatch lists the
2811 // values that are included in one container but not the other. (Duplicate 2825 // values that are included in one container but not the other. (Duplicate
2812 // values and order differences are not explained.) 2826 // values and order differences are not explained.)
2813 template <typename Container> 2827 template <typename Container>
2814 inline PolymorphicMatcher<internal::ContainerEqMatcher< 2828 inline PolymorphicMatcher<internal::ContainerEqMatcher< // NOLINT
2815 GMOCK_REMOVE_CONST_(Container)> > 2829 GMOCK_REMOVE_CONST_(Container)> >
2816 ContainerEq(const Container& rhs) { 2830 ContainerEq(const Container& rhs) {
2817 // This following line is for working around a bug in MSVC 8.0, 2831 // This following line is for working around a bug in MSVC 8.0,
2818 // which causes Container to be a const type sometimes. 2832 // which causes Container to be a const type sometimes.
2819 typedef GMOCK_REMOVE_CONST_(Container) RawContainer; 2833 typedef GMOCK_REMOVE_CONST_(Container) RawContainer;
2820 return MakePolymorphicMatcher(internal::ContainerEqMatcher<RawContainer>(rhs)) ; 2834 return MakePolymorphicMatcher(
2835 internal::ContainerEqMatcher<RawContainer>(rhs));
2821 } 2836 }
2822 2837
2823 // Matches an STL-style container or a native array that contains at 2838 // Matches an STL-style container or a native array that contains at
2824 // least one element matching the given value or matcher. 2839 // least one element matching the given value or matcher.
2825 // 2840 //
2826 // Examples: 2841 // Examples:
2827 // ::std::set<int> page_ids; 2842 // ::std::set<int> page_ids;
2828 // page_ids.insert(3); 2843 // page_ids.insert(3);
2829 // page_ids.insert(1); 2844 // page_ids.insert(1);
2830 // EXPECT_THAT(page_ids, Contains(1)); 2845 // EXPECT_THAT(page_ids, Contains(1));
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
2869 inline internal::MatcherAsPredicate<M> Matches(M matcher) { 2884 inline internal::MatcherAsPredicate<M> Matches(M matcher) {
2870 return internal::MatcherAsPredicate<M>(matcher); 2885 return internal::MatcherAsPredicate<M>(matcher);
2871 } 2886 }
2872 2887
2873 // Returns true iff the value matches the matcher. 2888 // Returns true iff the value matches the matcher.
2874 template <typename T, typename M> 2889 template <typename T, typename M>
2875 inline bool Value(const T& value, M matcher) { 2890 inline bool Value(const T& value, M matcher) {
2876 return testing::Matches(matcher)(value); 2891 return testing::Matches(matcher)(value);
2877 } 2892 }
2878 2893
2894 // Matches the value against the given matcher and explains the match
2895 // result to listener.
2896 template <typename T, typename M>
2897 inline bool ExplainMatchResult(
2898 M matcher, const T& value, MatchResultListener* listener) {
2899 return SafeMatcherCast<const T&>(matcher).MatchAndExplain(value, listener);
2900 }
2901
2879 // AllArgs(m) is a synonym of m. This is useful in 2902 // AllArgs(m) is a synonym of m. This is useful in
2880 // 2903 //
2881 // EXPECT_CALL(foo, Bar(_, _)).With(AllArgs(Eq())); 2904 // EXPECT_CALL(foo, Bar(_, _)).With(AllArgs(Eq()));
2882 // 2905 //
2883 // which is easier to read than 2906 // which is easier to read than
2884 // 2907 //
2885 // EXPECT_CALL(foo, Bar(_, _)).With(Eq()); 2908 // EXPECT_CALL(foo, Bar(_, _)).With(Eq());
2886 template <typename InnerMatcher> 2909 template <typename InnerMatcher>
2887 inline InnerMatcher AllArgs(const InnerMatcher& matcher) { return matcher; } 2910 inline InnerMatcher AllArgs(const InnerMatcher& matcher) { return matcher; }
2888 2911
2889 // These macros allow using matchers to check values in Google Test 2912 // These macros allow using matchers to check values in Google Test
2890 // tests. ASSERT_THAT(value, matcher) and EXPECT_THAT(value, matcher) 2913 // tests. ASSERT_THAT(value, matcher) and EXPECT_THAT(value, matcher)
2891 // succeed iff the value matches the matcher. If the assertion fails, 2914 // succeed iff the value matches the matcher. If the assertion fails,
2892 // the value and the description of the matcher will be printed. 2915 // the value and the description of the matcher will be printed.
2893 #define ASSERT_THAT(value, matcher) ASSERT_PRED_FORMAT1(\ 2916 #define ASSERT_THAT(value, matcher) ASSERT_PRED_FORMAT1(\
2894 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value) 2917 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
2895 #define EXPECT_THAT(value, matcher) EXPECT_PRED_FORMAT1(\ 2918 #define EXPECT_THAT(value, matcher) EXPECT_PRED_FORMAT1(\
2896 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value) 2919 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
2897 2920
2898 } // namespace testing 2921 } // namespace testing
2899 2922
2900 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_ 2923 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_
OLDNEW
« no previous file with comments | « testing/gmock/include/gmock/gmock-generated-matchers.h.pump ('k') | testing/gmock/include/gmock/gmock-printers.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698