blob: e69844fa6c46cdddcaa4abe153297a7506b952dc [file] [log] [blame]
shiqiane35fdd92008-12-10 05:08:54 +00001// Copyright 2007, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29//
30// Author: wan@google.com (Zhanyong Wan)
31
32// Google Mock - a framework for writing C++ mock classes.
33//
34// This file tests some commonly used argument matchers.
35
36#include <gmock/gmock-matchers.h>
37
38#include <string.h>
39#include <functional>
zhanyong.wan6a896b52009-01-16 01:13:50 +000040#include <list>
41#include <map>
42#include <set>
shiqiane35fdd92008-12-10 05:08:54 +000043#include <sstream>
zhanyong.wan6a896b52009-01-16 01:13:50 +000044#include <string>
zhanyong.wanf5e1ce52009-09-16 07:02:02 +000045#include <utility>
zhanyong.wan6a896b52009-01-16 01:13:50 +000046#include <vector>
shiqiane35fdd92008-12-10 05:08:54 +000047#include <gmock/gmock.h>
48#include <gtest/gtest.h>
49#include <gtest/gtest-spi.h>
50
51namespace testing {
zhanyong.wan4a5330d2009-02-19 00:36:44 +000052
53namespace internal {
54string FormatMatcherDescriptionSyntaxError(const char* description,
55 const char* error_pos);
56int GetParamIndex(const char* param_names[], const string& param_name);
57string JoinAsTuple(const Strings& fields);
58bool SkipPrefix(const char* prefix, const char** pstr);
59} // namespace internal
60
shiqiane35fdd92008-12-10 05:08:54 +000061namespace gmock_matchers_test {
62
zhanyong.wanb5937da2009-07-16 20:26:41 +000063using std::map;
64using std::multimap;
shiqiane35fdd92008-12-10 05:08:54 +000065using std::stringstream;
zhanyong.wanb8243162009-06-04 05:48:20 +000066using std::tr1::make_tuple;
shiqiane35fdd92008-12-10 05:08:54 +000067using testing::A;
zhanyong.wanbf550852009-06-09 06:09:53 +000068using testing::AllArgs;
shiqiane35fdd92008-12-10 05:08:54 +000069using testing::AllOf;
70using testing::An;
71using testing::AnyOf;
72using testing::ByRef;
73using testing::DoubleEq;
74using testing::EndsWith;
75using testing::Eq;
76using testing::Field;
77using testing::FloatEq;
78using testing::Ge;
79using testing::Gt;
80using testing::HasSubstr;
zhanyong.wan2d970ee2009-09-24 21:41:36 +000081using testing::IsNull;
zhanyong.wanb5937da2009-07-16 20:26:41 +000082using testing::Key;
shiqiane35fdd92008-12-10 05:08:54 +000083using testing::Le;
84using testing::Lt;
85using testing::MakeMatcher;
86using testing::MakePolymorphicMatcher;
87using testing::Matcher;
88using testing::MatcherCast;
89using testing::MatcherInterface;
90using testing::Matches;
zhanyong.wan82113312010-01-08 21:55:40 +000091using testing::MatchResultListener;
shiqiane35fdd92008-12-10 05:08:54 +000092using testing::NanSensitiveDoubleEq;
93using testing::NanSensitiveFloatEq;
94using testing::Ne;
95using testing::Not;
96using testing::NotNull;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +000097using testing::Pair;
shiqiane35fdd92008-12-10 05:08:54 +000098using testing::Pointee;
99using testing::PolymorphicMatcher;
100using testing::Property;
101using testing::Ref;
102using testing::ResultOf;
103using testing::StartsWith;
104using testing::StrCaseEq;
105using testing::StrCaseNe;
106using testing::StrEq;
107using testing::StrNe;
108using testing::Truly;
109using testing::TypedEq;
zhanyong.wanb8243162009-06-04 05:48:20 +0000110using testing::Value;
shiqiane35fdd92008-12-10 05:08:54 +0000111using testing::_;
112using testing::internal::FloatingEqMatcher;
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000113using testing::internal::FormatMatcherDescriptionSyntaxError;
114using testing::internal::GetParamIndex;
115using testing::internal::Interpolation;
116using testing::internal::Interpolations;
117using testing::internal::JoinAsTuple;
118using testing::internal::SkipPrefix;
shiqiane35fdd92008-12-10 05:08:54 +0000119using testing::internal::String;
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000120using testing::internal::Strings;
121using testing::internal::ValidateMatcherDescription;
122using testing::internal::kInvalidInterpolation;
123using testing::internal::kPercentInterpolation;
124using testing::internal::kTupleInterpolation;
vladlosev79b83502009-11-18 00:43:37 +0000125using testing::internal::linked_ptr;
vladloseve56daa72009-11-18 01:08:08 +0000126using testing::internal::scoped_ptr;
shiqiane35fdd92008-12-10 05:08:54 +0000127using testing::internal::string;
128
129#ifdef GMOCK_HAS_REGEX
130using testing::ContainsRegex;
131using testing::MatchesRegex;
132using testing::internal::RE;
133#endif // GMOCK_HAS_REGEX
134
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000135// For testing ExplainMatchResultTo().
136class GreaterThanMatcher : public MatcherInterface<int> {
137 public:
138 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
139
140 virtual bool Matches(int lhs) const { return lhs > rhs_; }
141
142 virtual void DescribeTo(::std::ostream* os) const {
143 *os << "is greater than " << rhs_;
144 }
145
146 virtual void ExplainMatchResultTo(int lhs, ::std::ostream* os) const {
147 const int diff = lhs - rhs_;
148 if (diff > 0) {
149 *os << "is " << diff << " more than " << rhs_;
150 } else if (diff == 0) {
151 *os << "is the same as " << rhs_;
152 } else {
153 *os << "is " << -diff << " less than " << rhs_;
154 }
155 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000156
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000157 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000158 int rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000159};
160
161Matcher<int> GreaterThan(int n) {
162 return MakeMatcher(new GreaterThanMatcher(n));
163}
164
shiqiane35fdd92008-12-10 05:08:54 +0000165// Returns the description of the given matcher.
166template <typename T>
167string Describe(const Matcher<T>& m) {
168 stringstream ss;
169 m.DescribeTo(&ss);
170 return ss.str();
171}
172
173// Returns the description of the negation of the given matcher.
174template <typename T>
175string DescribeNegation(const Matcher<T>& m) {
176 stringstream ss;
177 m.DescribeNegationTo(&ss);
178 return ss.str();
179}
180
181// Returns the reason why x matches, or doesn't match, m.
182template <typename MatcherType, typename Value>
183string Explain(const MatcherType& m, const Value& x) {
184 stringstream ss;
185 m.ExplainMatchResultTo(x, &ss);
186 return ss.str();
187}
188
189// Makes sure that the MatcherInterface<T> interface doesn't
190// change.
191class EvenMatcherImpl : public MatcherInterface<int> {
192 public:
193 virtual bool Matches(int x) const { return x % 2 == 0; }
194
195 virtual void DescribeTo(::std::ostream* os) const {
196 *os << "is an even number";
197 }
198
199 // We deliberately don't define DescribeNegationTo() and
200 // ExplainMatchResultTo() here, to make sure the definition of these
201 // two methods is optional.
202};
203
zhanyong.wan82113312010-01-08 21:55:40 +0000204TEST(MatcherInterfaceTest, CanBeImplementedUsingDeprecatedAPI) {
shiqiane35fdd92008-12-10 05:08:54 +0000205 EvenMatcherImpl m;
206}
207
zhanyong.wan82113312010-01-08 21:55:40 +0000208// Tests implementing a monomorphic matcher using MatchAndExplain().
209
210class NewEvenMatcherImpl : public MatcherInterface<int> {
211 public:
212 virtual bool MatchAndExplain(int x, MatchResultListener* listener) const {
213 const bool match = x % 2 == 0;
214 // Verifies that we can stream to a listener directly.
215 *listener << "value % " << 2;
216 if (listener->stream() != NULL) {
217 // Verifies that we can stream to a listener's underlying stream
218 // too.
219 *listener->stream() << " == " << (x % 2);
220 }
221 return match;
222 }
223
224 virtual void DescribeTo(::std::ostream* os) const {
225 *os << "is an even number";
226 }
227};
228
229TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {
230 Matcher<int> m = MakeMatcher(new NewEvenMatcherImpl);
231 EXPECT_TRUE(m.Matches(2));
232 EXPECT_FALSE(m.Matches(3));
233 EXPECT_EQ("value % 2 == 0", Explain(m, 2));
234 EXPECT_EQ("value % 2 == 1", Explain(m, 3));
235}
236
shiqiane35fdd92008-12-10 05:08:54 +0000237// Tests default-constructing a matcher.
238TEST(MatcherTest, CanBeDefaultConstructed) {
239 Matcher<double> m;
240}
241
242// Tests that Matcher<T> can be constructed from a MatcherInterface<T>*.
243TEST(MatcherTest, CanBeConstructedFromMatcherInterface) {
244 const MatcherInterface<int>* impl = new EvenMatcherImpl;
245 Matcher<int> m(impl);
246 EXPECT_TRUE(m.Matches(4));
247 EXPECT_FALSE(m.Matches(5));
248}
249
250// Tests that value can be used in place of Eq(value).
251TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) {
252 Matcher<int> m1 = 5;
253 EXPECT_TRUE(m1.Matches(5));
254 EXPECT_FALSE(m1.Matches(6));
255}
256
257// Tests that NULL can be used in place of Eq(NULL).
258TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) {
259 Matcher<int*> m1 = NULL;
260 EXPECT_TRUE(m1.Matches(NULL));
261 int n = 0;
262 EXPECT_FALSE(m1.Matches(&n));
263}
264
265// Tests that matchers are copyable.
266TEST(MatcherTest, IsCopyable) {
267 // Tests the copy constructor.
268 Matcher<bool> m1 = Eq(false);
269 EXPECT_TRUE(m1.Matches(false));
270 EXPECT_FALSE(m1.Matches(true));
271
272 // Tests the assignment operator.
273 m1 = Eq(true);
274 EXPECT_TRUE(m1.Matches(true));
275 EXPECT_FALSE(m1.Matches(false));
276}
277
278// Tests that Matcher<T>::DescribeTo() calls
279// MatcherInterface<T>::DescribeTo().
280TEST(MatcherTest, CanDescribeItself) {
281 EXPECT_EQ("is an even number",
282 Describe(Matcher<int>(new EvenMatcherImpl)));
283}
284
zhanyong.wan82113312010-01-08 21:55:40 +0000285// Tests Matcher<T>::MatchAndExplain().
286TEST(MatcherTest, MatchAndExplain) {
287 Matcher<int> m = GreaterThan(0);
288 ::testing::internal::StringMatchResultListener listener1;
289 EXPECT_TRUE(m.MatchAndExplain(42, &listener1));
290 EXPECT_EQ("is 42 more than 0", listener1.str());
291
292 ::testing::internal::StringMatchResultListener listener2;
293 EXPECT_FALSE(m.MatchAndExplain(-9, &listener2));
294 EXPECT_EQ("is 9 less than 0", listener2.str());
295}
296
shiqiane35fdd92008-12-10 05:08:54 +0000297// Tests that a C-string literal can be implicitly converted to a
298// Matcher<string> or Matcher<const string&>.
299TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
300 Matcher<string> m1 = "hi";
301 EXPECT_TRUE(m1.Matches("hi"));
302 EXPECT_FALSE(m1.Matches("hello"));
303
304 Matcher<const string&> m2 = "hi";
305 EXPECT_TRUE(m2.Matches("hi"));
306 EXPECT_FALSE(m2.Matches("hello"));
307}
308
309// Tests that a string object can be implicitly converted to a
310// Matcher<string> or Matcher<const string&>.
311TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {
312 Matcher<string> m1 = string("hi");
313 EXPECT_TRUE(m1.Matches("hi"));
314 EXPECT_FALSE(m1.Matches("hello"));
315
316 Matcher<const string&> m2 = string("hi");
317 EXPECT_TRUE(m2.Matches("hi"));
318 EXPECT_FALSE(m2.Matches("hello"));
319}
320
321// Tests that MakeMatcher() constructs a Matcher<T> from a
322// MatcherInterface* without requiring the user to explicitly
323// write the type.
324TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) {
325 const MatcherInterface<int>* dummy_impl = NULL;
326 Matcher<int> m = MakeMatcher(dummy_impl);
327}
328
zhanyong.wan82113312010-01-08 21:55:40 +0000329// Tests that MakePolymorphicMatcher() can construct a polymorphic
330// matcher from its implementation using the old API.
shiqiane35fdd92008-12-10 05:08:54 +0000331const int bar = 1;
332class ReferencesBarOrIsZeroImpl {
333 public:
334 template <typename T>
335 bool Matches(const T& x) const {
336 const void* p = &x;
337 return p == &bar || x == 0;
338 }
339
340 void DescribeTo(::std::ostream* os) const { *os << "bar or zero"; }
341
342 void DescribeNegationTo(::std::ostream* os) const {
343 *os << "doesn't reference bar and is not zero";
344 }
345};
346
347// This function verifies that MakePolymorphicMatcher() returns a
348// PolymorphicMatcher<T> where T is the argument's type.
349PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() {
350 return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl());
351}
352
zhanyong.wan82113312010-01-08 21:55:40 +0000353TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) {
shiqiane35fdd92008-12-10 05:08:54 +0000354 // Using a polymorphic matcher to match a reference type.
355 Matcher<const int&> m1 = ReferencesBarOrIsZero();
356 EXPECT_TRUE(m1.Matches(0));
357 // Verifies that the identity of a by-reference argument is preserved.
358 EXPECT_TRUE(m1.Matches(bar));
359 EXPECT_FALSE(m1.Matches(1));
360 EXPECT_EQ("bar or zero", Describe(m1));
361
362 // Using a polymorphic matcher to match a value type.
363 Matcher<double> m2 = ReferencesBarOrIsZero();
364 EXPECT_TRUE(m2.Matches(0.0));
365 EXPECT_FALSE(m2.Matches(0.1));
366 EXPECT_EQ("bar or zero", Describe(m2));
367}
368
zhanyong.wan82113312010-01-08 21:55:40 +0000369// Tests implementing a polymorphic matcher using MatchAndExplain().
370
371class PolymorphicIsEvenImpl {
372 public:
373 void DescribeTo(::std::ostream* os) const { *os << "is even"; }
374
375 void DescribeNegationTo(::std::ostream* os) const {
376 *os << "is odd";
377 }
378};
379
380template <typename T>
381bool MatchAndExplain(const PolymorphicIsEvenImpl& /* impl */,
382 T x, MatchResultListener* listener) {
383 // Verifies that we can stream to the listener directly.
384 *listener << "% " << 2;
385 if (listener->stream() != NULL) {
386 // Verifies that we can stream to the listener's underlying stream
387 // too.
388 *listener->stream() << " == " << (x % 2);
389 }
390 return (x % 2) == 0;
391}
392
393PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() {
394 return MakePolymorphicMatcher(PolymorphicIsEvenImpl());
395}
396
397TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {
398 // Using PolymorphicIsEven() as a Matcher<int>.
399 const Matcher<int> m1 = PolymorphicIsEven();
400 EXPECT_TRUE(m1.Matches(42));
401 EXPECT_FALSE(m1.Matches(43));
402 EXPECT_EQ("is even", Describe(m1));
403
404 const Matcher<int> not_m1 = Not(m1);
405 EXPECT_EQ("is odd", Describe(not_m1));
406
407 EXPECT_EQ("% 2 == 0", Explain(m1, 42));
408
409 // Using PolymorphicIsEven() as a Matcher<char>.
410 const Matcher<char> m2 = PolymorphicIsEven();
411 EXPECT_TRUE(m2.Matches('\x42'));
412 EXPECT_FALSE(m2.Matches('\x43'));
413 EXPECT_EQ("is even", Describe(m2));
414
415 const Matcher<char> not_m2 = Not(m2);
416 EXPECT_EQ("is odd", Describe(not_m2));
417
418 EXPECT_EQ("% 2 == 0", Explain(m2, '\x42'));
419}
420
shiqiane35fdd92008-12-10 05:08:54 +0000421// Tests that MatcherCast<T>(m) works when m is a polymorphic matcher.
422TEST(MatcherCastTest, FromPolymorphicMatcher) {
423 Matcher<int> m = MatcherCast<int>(Eq(5));
424 EXPECT_TRUE(m.Matches(5));
425 EXPECT_FALSE(m.Matches(6));
426}
427
428// For testing casting matchers between compatible types.
429class IntValue {
430 public:
431 // An int can be statically (although not implicitly) cast to a
432 // IntValue.
zhanyong.wan32de5f52009-12-23 00:13:23 +0000433 explicit IntValue(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +0000434
435 int value() const { return value_; }
436 private:
437 int value_;
438};
439
440// For testing casting matchers between compatible types.
441bool IsPositiveIntValue(const IntValue& foo) {
442 return foo.value() > 0;
443}
444
445// Tests that MatcherCast<T>(m) works when m is a Matcher<U> where T
446// can be statically converted to U.
447TEST(MatcherCastTest, FromCompatibleType) {
448 Matcher<double> m1 = Eq(2.0);
449 Matcher<int> m2 = MatcherCast<int>(m1);
450 EXPECT_TRUE(m2.Matches(2));
451 EXPECT_FALSE(m2.Matches(3));
452
453 Matcher<IntValue> m3 = Truly(IsPositiveIntValue);
454 Matcher<int> m4 = MatcherCast<int>(m3);
455 // In the following, the arguments 1 and 0 are statically converted
456 // to IntValue objects, and then tested by the IsPositiveIntValue()
457 // predicate.
458 EXPECT_TRUE(m4.Matches(1));
459 EXPECT_FALSE(m4.Matches(0));
460}
461
462// Tests that MatcherCast<T>(m) works when m is a Matcher<const T&>.
463TEST(MatcherCastTest, FromConstReferenceToNonReference) {
464 Matcher<const int&> m1 = Eq(0);
465 Matcher<int> m2 = MatcherCast<int>(m1);
466 EXPECT_TRUE(m2.Matches(0));
467 EXPECT_FALSE(m2.Matches(1));
468}
469
470// Tests that MatcherCast<T>(m) works when m is a Matcher<T&>.
471TEST(MatcherCastTest, FromReferenceToNonReference) {
472 Matcher<int&> m1 = Eq(0);
473 Matcher<int> m2 = MatcherCast<int>(m1);
474 EXPECT_TRUE(m2.Matches(0));
475 EXPECT_FALSE(m2.Matches(1));
476}
477
478// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
479TEST(MatcherCastTest, FromNonReferenceToConstReference) {
480 Matcher<int> m1 = Eq(0);
481 Matcher<const int&> m2 = MatcherCast<const int&>(m1);
482 EXPECT_TRUE(m2.Matches(0));
483 EXPECT_FALSE(m2.Matches(1));
484}
485
486// Tests that MatcherCast<T&>(m) works when m is a Matcher<T>.
487TEST(MatcherCastTest, FromNonReferenceToReference) {
488 Matcher<int> m1 = Eq(0);
489 Matcher<int&> m2 = MatcherCast<int&>(m1);
490 int n = 0;
491 EXPECT_TRUE(m2.Matches(n));
492 n = 1;
493 EXPECT_FALSE(m2.Matches(n));
494}
495
496// Tests that MatcherCast<T>(m) works when m is a Matcher<T>.
497TEST(MatcherCastTest, FromSameType) {
498 Matcher<int> m1 = Eq(0);
499 Matcher<int> m2 = MatcherCast<int>(m1);
500 EXPECT_TRUE(m2.Matches(0));
501 EXPECT_FALSE(m2.Matches(1));
502}
503
zhanyong.wan18490652009-05-11 18:54:08 +0000504class Base {};
505class Derived : public Base {};
506
507// Tests that SafeMatcherCast<T>(m) works when m is a polymorphic matcher.
508TEST(SafeMatcherCastTest, FromPolymorphicMatcher) {
509 Matcher<char> m2 = SafeMatcherCast<char>(Eq(32));
510 EXPECT_TRUE(m2.Matches(' '));
511 EXPECT_FALSE(m2.Matches('\n'));
512}
513
zhanyong.wan16cf4732009-05-14 20:55:30 +0000514// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where
515// T and U are arithmetic types and T can be losslessly converted to
516// U.
517TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) {
zhanyong.wan18490652009-05-11 18:54:08 +0000518 Matcher<double> m1 = DoubleEq(1.0);
zhanyong.wan16cf4732009-05-14 20:55:30 +0000519 Matcher<float> m2 = SafeMatcherCast<float>(m1);
520 EXPECT_TRUE(m2.Matches(1.0f));
521 EXPECT_FALSE(m2.Matches(2.0f));
522
523 Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>('a'));
524 EXPECT_TRUE(m3.Matches('a'));
525 EXPECT_FALSE(m3.Matches('b'));
zhanyong.wan18490652009-05-11 18:54:08 +0000526}
527
528// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where T and U
529// are pointers or references to a derived and a base class, correspondingly.
530TEST(SafeMatcherCastTest, FromBaseClass) {
531 Derived d, d2;
532 Matcher<Base*> m1 = Eq(&d);
533 Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1);
534 EXPECT_TRUE(m2.Matches(&d));
535 EXPECT_FALSE(m2.Matches(&d2));
536
537 Matcher<Base&> m3 = Ref(d);
538 Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3);
539 EXPECT_TRUE(m4.Matches(d));
540 EXPECT_FALSE(m4.Matches(d2));
541}
542
543// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<const T&>.
544TEST(SafeMatcherCastTest, FromConstReferenceToReference) {
545 int n = 0;
546 Matcher<const int&> m1 = Ref(n);
547 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
548 int n1 = 0;
549 EXPECT_TRUE(m2.Matches(n));
550 EXPECT_FALSE(m2.Matches(n1));
551}
552
553// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
554TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) {
555 Matcher<int> m1 = Eq(0);
556 Matcher<const int&> m2 = SafeMatcherCast<const int&>(m1);
557 EXPECT_TRUE(m2.Matches(0));
558 EXPECT_FALSE(m2.Matches(1));
559}
560
561// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<T>.
562TEST(SafeMatcherCastTest, FromNonReferenceToReference) {
563 Matcher<int> m1 = Eq(0);
564 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
565 int n = 0;
566 EXPECT_TRUE(m2.Matches(n));
567 n = 1;
568 EXPECT_FALSE(m2.Matches(n));
569}
570
571// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<T>.
572TEST(SafeMatcherCastTest, FromSameType) {
573 Matcher<int> m1 = Eq(0);
574 Matcher<int> m2 = SafeMatcherCast<int>(m1);
575 EXPECT_TRUE(m2.Matches(0));
576 EXPECT_FALSE(m2.Matches(1));
577}
578
shiqiane35fdd92008-12-10 05:08:54 +0000579// Tests that A<T>() matches any value of type T.
580TEST(ATest, MatchesAnyValue) {
581 // Tests a matcher for a value type.
582 Matcher<double> m1 = A<double>();
583 EXPECT_TRUE(m1.Matches(91.43));
584 EXPECT_TRUE(m1.Matches(-15.32));
585
586 // Tests a matcher for a reference type.
587 int a = 2;
588 int b = -6;
589 Matcher<int&> m2 = A<int&>();
590 EXPECT_TRUE(m2.Matches(a));
591 EXPECT_TRUE(m2.Matches(b));
592}
593
594// Tests that A<T>() describes itself properly.
595TEST(ATest, CanDescribeSelf) {
596 EXPECT_EQ("is anything", Describe(A<bool>()));
597}
598
599// Tests that An<T>() matches any value of type T.
600TEST(AnTest, MatchesAnyValue) {
601 // Tests a matcher for a value type.
602 Matcher<int> m1 = An<int>();
603 EXPECT_TRUE(m1.Matches(9143));
604 EXPECT_TRUE(m1.Matches(-1532));
605
606 // Tests a matcher for a reference type.
607 int a = 2;
608 int b = -6;
609 Matcher<int&> m2 = An<int&>();
610 EXPECT_TRUE(m2.Matches(a));
611 EXPECT_TRUE(m2.Matches(b));
612}
613
614// Tests that An<T>() describes itself properly.
615TEST(AnTest, CanDescribeSelf) {
616 EXPECT_EQ("is anything", Describe(An<int>()));
617}
618
619// Tests that _ can be used as a matcher for any type and matches any
620// value of that type.
621TEST(UnderscoreTest, MatchesAnyValue) {
622 // Uses _ as a matcher for a value type.
623 Matcher<int> m1 = _;
624 EXPECT_TRUE(m1.Matches(123));
625 EXPECT_TRUE(m1.Matches(-242));
626
627 // Uses _ as a matcher for a reference type.
628 bool a = false;
629 const bool b = true;
630 Matcher<const bool&> m2 = _;
631 EXPECT_TRUE(m2.Matches(a));
632 EXPECT_TRUE(m2.Matches(b));
633}
634
635// Tests that _ describes itself properly.
636TEST(UnderscoreTest, CanDescribeSelf) {
637 Matcher<int> m = _;
638 EXPECT_EQ("is anything", Describe(m));
639}
640
641// Tests that Eq(x) matches any value equal to x.
642TEST(EqTest, MatchesEqualValue) {
643 // 2 C-strings with same content but different addresses.
644 const char a1[] = "hi";
645 const char a2[] = "hi";
646
647 Matcher<const char*> m1 = Eq(a1);
648 EXPECT_TRUE(m1.Matches(a1));
649 EXPECT_FALSE(m1.Matches(a2));
650}
651
652// Tests that Eq(v) describes itself properly.
653
654class Unprintable {
655 public:
656 Unprintable() : c_('a') {}
657
zhanyong.wan32de5f52009-12-23 00:13:23 +0000658 bool operator==(const Unprintable& /* rhs */) { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000659 private:
660 char c_;
661};
662
663TEST(EqTest, CanDescribeSelf) {
664 Matcher<Unprintable> m = Eq(Unprintable());
665 EXPECT_EQ("is equal to 1-byte object <61>", Describe(m));
666}
667
668// Tests that Eq(v) can be used to match any type that supports
669// comparing with type T, where T is v's type.
670TEST(EqTest, IsPolymorphic) {
671 Matcher<int> m1 = Eq(1);
672 EXPECT_TRUE(m1.Matches(1));
673 EXPECT_FALSE(m1.Matches(2));
674
675 Matcher<char> m2 = Eq(1);
676 EXPECT_TRUE(m2.Matches('\1'));
677 EXPECT_FALSE(m2.Matches('a'));
678}
679
680// Tests that TypedEq<T>(v) matches values of type T that's equal to v.
681TEST(TypedEqTest, ChecksEqualityForGivenType) {
682 Matcher<char> m1 = TypedEq<char>('a');
683 EXPECT_TRUE(m1.Matches('a'));
684 EXPECT_FALSE(m1.Matches('b'));
685
686 Matcher<int> m2 = TypedEq<int>(6);
687 EXPECT_TRUE(m2.Matches(6));
688 EXPECT_FALSE(m2.Matches(7));
689}
690
691// Tests that TypedEq(v) describes itself properly.
692TEST(TypedEqTest, CanDescribeSelf) {
693 EXPECT_EQ("is equal to 2", Describe(TypedEq<int>(2)));
694}
695
696// Tests that TypedEq<T>(v) has type Matcher<T>.
697
698// Type<T>::IsTypeOf(v) compiles iff the type of value v is T, where T
699// is a "bare" type (i.e. not in the form of const U or U&). If v's
700// type is not T, the compiler will generate a message about
701// "undefined referece".
702template <typename T>
703struct Type {
zhanyong.wan32de5f52009-12-23 00:13:23 +0000704 static bool IsTypeOf(const T& /* v */) { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000705
706 template <typename T2>
707 static void IsTypeOf(T2 v);
708};
709
710TEST(TypedEqTest, HasSpecifiedType) {
711 // Verfies that the type of TypedEq<T>(v) is Matcher<T>.
712 Type<Matcher<int> >::IsTypeOf(TypedEq<int>(5));
713 Type<Matcher<double> >::IsTypeOf(TypedEq<double>(5));
714}
715
716// Tests that Ge(v) matches anything >= v.
717TEST(GeTest, ImplementsGreaterThanOrEqual) {
718 Matcher<int> m1 = Ge(0);
719 EXPECT_TRUE(m1.Matches(1));
720 EXPECT_TRUE(m1.Matches(0));
721 EXPECT_FALSE(m1.Matches(-1));
722}
723
724// Tests that Ge(v) describes itself properly.
725TEST(GeTest, CanDescribeSelf) {
726 Matcher<int> m = Ge(5);
727 EXPECT_EQ("is greater than or equal to 5", Describe(m));
728}
729
730// Tests that Gt(v) matches anything > v.
731TEST(GtTest, ImplementsGreaterThan) {
732 Matcher<double> m1 = Gt(0);
733 EXPECT_TRUE(m1.Matches(1.0));
734 EXPECT_FALSE(m1.Matches(0.0));
735 EXPECT_FALSE(m1.Matches(-1.0));
736}
737
738// Tests that Gt(v) describes itself properly.
739TEST(GtTest, CanDescribeSelf) {
740 Matcher<int> m = Gt(5);
741 EXPECT_EQ("is greater than 5", Describe(m));
742}
743
744// Tests that Le(v) matches anything <= v.
745TEST(LeTest, ImplementsLessThanOrEqual) {
746 Matcher<char> m1 = Le('b');
747 EXPECT_TRUE(m1.Matches('a'));
748 EXPECT_TRUE(m1.Matches('b'));
749 EXPECT_FALSE(m1.Matches('c'));
750}
751
752// Tests that Le(v) describes itself properly.
753TEST(LeTest, CanDescribeSelf) {
754 Matcher<int> m = Le(5);
755 EXPECT_EQ("is less than or equal to 5", Describe(m));
756}
757
758// Tests that Lt(v) matches anything < v.
759TEST(LtTest, ImplementsLessThan) {
760 Matcher<const string&> m1 = Lt("Hello");
761 EXPECT_TRUE(m1.Matches("Abc"));
762 EXPECT_FALSE(m1.Matches("Hello"));
763 EXPECT_FALSE(m1.Matches("Hello, world!"));
764}
765
766// Tests that Lt(v) describes itself properly.
767TEST(LtTest, CanDescribeSelf) {
768 Matcher<int> m = Lt(5);
769 EXPECT_EQ("is less than 5", Describe(m));
770}
771
772// Tests that Ne(v) matches anything != v.
773TEST(NeTest, ImplementsNotEqual) {
774 Matcher<int> m1 = Ne(0);
775 EXPECT_TRUE(m1.Matches(1));
776 EXPECT_TRUE(m1.Matches(-1));
777 EXPECT_FALSE(m1.Matches(0));
778}
779
780// Tests that Ne(v) describes itself properly.
781TEST(NeTest, CanDescribeSelf) {
782 Matcher<int> m = Ne(5);
783 EXPECT_EQ("is not equal to 5", Describe(m));
784}
785
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000786// Tests that IsNull() matches any NULL pointer of any type.
787TEST(IsNullTest, MatchesNullPointer) {
788 Matcher<int*> m1 = IsNull();
789 int* p1 = NULL;
790 int n = 0;
791 EXPECT_TRUE(m1.Matches(p1));
792 EXPECT_FALSE(m1.Matches(&n));
793
794 Matcher<const char*> m2 = IsNull();
795 const char* p2 = NULL;
796 EXPECT_TRUE(m2.Matches(p2));
797 EXPECT_FALSE(m2.Matches("hi"));
798
zhanyong.wan95b12332009-09-25 18:55:50 +0000799#if !GTEST_OS_SYMBIAN
800 // Nokia's Symbian compiler generates:
801 // gmock-matchers.h: ambiguous access to overloaded function
802 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(void *)'
803 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(const testing::
804 // MatcherInterface<void *> *)'
805 // gmock-matchers.h: (point of instantiation: 'testing::
806 // gmock_matchers_test::IsNullTest_MatchesNullPointer_Test::TestBody()')
807 // gmock-matchers.h: (instantiating: 'testing::PolymorphicMatc
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000808 Matcher<void*> m3 = IsNull();
809 void* p3 = NULL;
810 EXPECT_TRUE(m3.Matches(p3));
811 EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef)));
zhanyong.wan95b12332009-09-25 18:55:50 +0000812#endif
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000813}
814
vladlosev79b83502009-11-18 00:43:37 +0000815TEST(IsNullTest, LinkedPtr) {
816 const Matcher<linked_ptr<int> > m = IsNull();
817 const linked_ptr<int> null_p;
818 const linked_ptr<int> non_null_p(new int);
819
820 EXPECT_TRUE(m.Matches(null_p));
821 EXPECT_FALSE(m.Matches(non_null_p));
822}
823
824TEST(IsNullTest, ReferenceToConstLinkedPtr) {
825 const Matcher<const linked_ptr<double>&> m = IsNull();
826 const linked_ptr<double> null_p;
827 const linked_ptr<double> non_null_p(new double);
828
829 EXPECT_TRUE(m.Matches(null_p));
830 EXPECT_FALSE(m.Matches(non_null_p));
831}
832
vladloseve56daa72009-11-18 01:08:08 +0000833TEST(IsNullTest, ReferenceToConstScopedPtr) {
834 const Matcher<const scoped_ptr<double>&> m = IsNull();
835 const scoped_ptr<double> null_p;
836 const scoped_ptr<double> non_null_p(new double);
837
838 EXPECT_TRUE(m.Matches(null_p));
839 EXPECT_FALSE(m.Matches(non_null_p));
840}
841
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000842// Tests that IsNull() describes itself properly.
843TEST(IsNullTest, CanDescribeSelf) {
844 Matcher<int*> m = IsNull();
845 EXPECT_EQ("is NULL", Describe(m));
846 EXPECT_EQ("is not NULL", DescribeNegation(m));
847}
848
shiqiane35fdd92008-12-10 05:08:54 +0000849// Tests that NotNull() matches any non-NULL pointer of any type.
850TEST(NotNullTest, MatchesNonNullPointer) {
851 Matcher<int*> m1 = NotNull();
852 int* p1 = NULL;
853 int n = 0;
854 EXPECT_FALSE(m1.Matches(p1));
855 EXPECT_TRUE(m1.Matches(&n));
856
857 Matcher<const char*> m2 = NotNull();
858 const char* p2 = NULL;
859 EXPECT_FALSE(m2.Matches(p2));
860 EXPECT_TRUE(m2.Matches("hi"));
861}
862
vladlosev79b83502009-11-18 00:43:37 +0000863TEST(NotNullTest, LinkedPtr) {
864 const Matcher<linked_ptr<int> > m = NotNull();
865 const linked_ptr<int> null_p;
866 const linked_ptr<int> non_null_p(new int);
867
868 EXPECT_FALSE(m.Matches(null_p));
869 EXPECT_TRUE(m.Matches(non_null_p));
870}
871
872TEST(NotNullTest, ReferenceToConstLinkedPtr) {
873 const Matcher<const linked_ptr<double>&> m = NotNull();
874 const linked_ptr<double> null_p;
875 const linked_ptr<double> non_null_p(new double);
876
877 EXPECT_FALSE(m.Matches(null_p));
878 EXPECT_TRUE(m.Matches(non_null_p));
879}
880
vladloseve56daa72009-11-18 01:08:08 +0000881TEST(NotNullTest, ReferenceToConstScopedPtr) {
882 const Matcher<const scoped_ptr<double>&> m = NotNull();
883 const scoped_ptr<double> null_p;
884 const scoped_ptr<double> non_null_p(new double);
885
886 EXPECT_FALSE(m.Matches(null_p));
887 EXPECT_TRUE(m.Matches(non_null_p));
888}
889
shiqiane35fdd92008-12-10 05:08:54 +0000890// Tests that NotNull() describes itself properly.
891TEST(NotNullTest, CanDescribeSelf) {
892 Matcher<int*> m = NotNull();
893 EXPECT_EQ("is not NULL", Describe(m));
894}
895
896// Tests that Ref(variable) matches an argument that references
897// 'variable'.
898TEST(RefTest, MatchesSameVariable) {
899 int a = 0;
900 int b = 0;
901 Matcher<int&> m = Ref(a);
902 EXPECT_TRUE(m.Matches(a));
903 EXPECT_FALSE(m.Matches(b));
904}
905
906// Tests that Ref(variable) describes itself properly.
907TEST(RefTest, CanDescribeSelf) {
908 int n = 5;
909 Matcher<int&> m = Ref(n);
910 stringstream ss;
911 ss << "references the variable @" << &n << " 5";
912 EXPECT_EQ(string(ss.str()), Describe(m));
913}
914
915// Test that Ref(non_const_varialbe) can be used as a matcher for a
916// const reference.
917TEST(RefTest, CanBeUsedAsMatcherForConstReference) {
918 int a = 0;
919 int b = 0;
920 Matcher<const int&> m = Ref(a);
921 EXPECT_TRUE(m.Matches(a));
922 EXPECT_FALSE(m.Matches(b));
923}
924
925// Tests that Ref(variable) is covariant, i.e. Ref(derived) can be
926// used wherever Ref(base) can be used (Ref(derived) is a sub-type
927// of Ref(base), but not vice versa.
928
shiqiane35fdd92008-12-10 05:08:54 +0000929TEST(RefTest, IsCovariant) {
930 Base base, base2;
931 Derived derived;
932 Matcher<const Base&> m1 = Ref(base);
933 EXPECT_TRUE(m1.Matches(base));
934 EXPECT_FALSE(m1.Matches(base2));
935 EXPECT_FALSE(m1.Matches(derived));
936
937 m1 = Ref(derived);
938 EXPECT_TRUE(m1.Matches(derived));
939 EXPECT_FALSE(m1.Matches(base));
940 EXPECT_FALSE(m1.Matches(base2));
941}
942
943// Tests string comparison matchers.
944
945TEST(StrEqTest, MatchesEqualString) {
946 Matcher<const char*> m = StrEq(string("Hello"));
947 EXPECT_TRUE(m.Matches("Hello"));
948 EXPECT_FALSE(m.Matches("hello"));
949 EXPECT_FALSE(m.Matches(NULL));
950
951 Matcher<const string&> m2 = StrEq("Hello");
952 EXPECT_TRUE(m2.Matches("Hello"));
953 EXPECT_FALSE(m2.Matches("Hi"));
954}
955
956TEST(StrEqTest, CanDescribeSelf) {
957 Matcher<string> m = StrEq("Hi-\'\"\?\\\a\b\f\n\r\t\v\xD3");
958 EXPECT_EQ("is equal to \"Hi-\'\\\"\\?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",
959 Describe(m));
960
961 string str("01204500800");
962 str[3] = '\0';
963 Matcher<string> m2 = StrEq(str);
964 EXPECT_EQ("is equal to \"012\\04500800\"", Describe(m2));
965 str[0] = str[6] = str[7] = str[9] = str[10] = '\0';
966 Matcher<string> m3 = StrEq(str);
967 EXPECT_EQ("is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3));
968}
969
970TEST(StrNeTest, MatchesUnequalString) {
971 Matcher<const char*> m = StrNe("Hello");
972 EXPECT_TRUE(m.Matches(""));
973 EXPECT_TRUE(m.Matches(NULL));
974 EXPECT_FALSE(m.Matches("Hello"));
975
976 Matcher<string> m2 = StrNe(string("Hello"));
977 EXPECT_TRUE(m2.Matches("hello"));
978 EXPECT_FALSE(m2.Matches("Hello"));
979}
980
981TEST(StrNeTest, CanDescribeSelf) {
982 Matcher<const char*> m = StrNe("Hi");
983 EXPECT_EQ("is not equal to \"Hi\"", Describe(m));
984}
985
986TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
987 Matcher<const char*> m = StrCaseEq(string("Hello"));
988 EXPECT_TRUE(m.Matches("Hello"));
989 EXPECT_TRUE(m.Matches("hello"));
990 EXPECT_FALSE(m.Matches("Hi"));
991 EXPECT_FALSE(m.Matches(NULL));
992
993 Matcher<const string&> m2 = StrCaseEq("Hello");
994 EXPECT_TRUE(m2.Matches("hello"));
995 EXPECT_FALSE(m2.Matches("Hi"));
996}
997
998TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
999 string str1("oabocdooeoo");
1000 string str2("OABOCDOOEOO");
1001 Matcher<const string&> m0 = StrCaseEq(str1);
1002 EXPECT_FALSE(m0.Matches(str2 + string(1, '\0')));
1003
1004 str1[3] = str2[3] = '\0';
1005 Matcher<const string&> m1 = StrCaseEq(str1);
1006 EXPECT_TRUE(m1.Matches(str2));
1007
1008 str1[0] = str1[6] = str1[7] = str1[10] = '\0';
1009 str2[0] = str2[6] = str2[7] = str2[10] = '\0';
1010 Matcher<const string&> m2 = StrCaseEq(str1);
1011 str1[9] = str2[9] = '\0';
1012 EXPECT_FALSE(m2.Matches(str2));
1013
1014 Matcher<const string&> m3 = StrCaseEq(str1);
1015 EXPECT_TRUE(m3.Matches(str2));
1016
1017 EXPECT_FALSE(m3.Matches(str2 + "x"));
1018 str2.append(1, '\0');
1019 EXPECT_FALSE(m3.Matches(str2));
1020 EXPECT_FALSE(m3.Matches(string(str2, 0, 9)));
1021}
1022
1023TEST(StrCaseEqTest, CanDescribeSelf) {
1024 Matcher<string> m = StrCaseEq("Hi");
1025 EXPECT_EQ("is equal to (ignoring case) \"Hi\"", Describe(m));
1026}
1027
1028TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1029 Matcher<const char*> m = StrCaseNe("Hello");
1030 EXPECT_TRUE(m.Matches("Hi"));
1031 EXPECT_TRUE(m.Matches(NULL));
1032 EXPECT_FALSE(m.Matches("Hello"));
1033 EXPECT_FALSE(m.Matches("hello"));
1034
1035 Matcher<string> m2 = StrCaseNe(string("Hello"));
1036 EXPECT_TRUE(m2.Matches(""));
1037 EXPECT_FALSE(m2.Matches("Hello"));
1038}
1039
1040TEST(StrCaseNeTest, CanDescribeSelf) {
1041 Matcher<const char*> m = StrCaseNe("Hi");
1042 EXPECT_EQ("is not equal to (ignoring case) \"Hi\"", Describe(m));
1043}
1044
1045// Tests that HasSubstr() works for matching string-typed values.
1046TEST(HasSubstrTest, WorksForStringClasses) {
1047 const Matcher<string> m1 = HasSubstr("foo");
1048 EXPECT_TRUE(m1.Matches(string("I love food.")));
1049 EXPECT_FALSE(m1.Matches(string("tofo")));
1050
1051 const Matcher<const std::string&> m2 = HasSubstr("foo");
1052 EXPECT_TRUE(m2.Matches(std::string("I love food.")));
1053 EXPECT_FALSE(m2.Matches(std::string("tofo")));
1054}
1055
1056// Tests that HasSubstr() works for matching C-string-typed values.
1057TEST(HasSubstrTest, WorksForCStrings) {
1058 const Matcher<char*> m1 = HasSubstr("foo");
1059 EXPECT_TRUE(m1.Matches(const_cast<char*>("I love food.")));
1060 EXPECT_FALSE(m1.Matches(const_cast<char*>("tofo")));
1061 EXPECT_FALSE(m1.Matches(NULL));
1062
1063 const Matcher<const char*> m2 = HasSubstr("foo");
1064 EXPECT_TRUE(m2.Matches("I love food."));
1065 EXPECT_FALSE(m2.Matches("tofo"));
1066 EXPECT_FALSE(m2.Matches(NULL));
1067}
1068
1069// Tests that HasSubstr(s) describes itself properly.
1070TEST(HasSubstrTest, CanDescribeSelf) {
1071 Matcher<string> m = HasSubstr("foo\n\"");
1072 EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m));
1073}
1074
zhanyong.wanb5937da2009-07-16 20:26:41 +00001075TEST(KeyTest, CanDescribeSelf) {
1076 Matcher<const std::pair<std::string, int>&> m = Key("foo");
1077 EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m));
1078}
1079
1080TEST(KeyTest, MatchesCorrectly) {
1081 std::pair<int, std::string> p(25, "foo");
1082 EXPECT_THAT(p, Key(25));
1083 EXPECT_THAT(p, Not(Key(42)));
1084 EXPECT_THAT(p, Key(Ge(20)));
1085 EXPECT_THAT(p, Not(Key(Lt(25))));
1086}
1087
1088TEST(KeyTest, SafelyCastsInnerMatcher) {
1089 Matcher<int> is_positive = Gt(0);
1090 Matcher<int> is_negative = Lt(0);
1091 std::pair<char, bool> p('a', true);
1092 EXPECT_THAT(p, Key(is_positive));
1093 EXPECT_THAT(p, Not(Key(is_negative)));
1094}
1095
1096TEST(KeyTest, InsideContainsUsingMap) {
zhanyong.wan95b12332009-09-25 18:55:50 +00001097 std::map<int, char> container;
1098 container.insert(std::make_pair(1, 'a'));
1099 container.insert(std::make_pair(2, 'b'));
1100 container.insert(std::make_pair(4, 'c'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001101 EXPECT_THAT(container, Contains(Key(1)));
1102 EXPECT_THAT(container, Not(Contains(Key(3))));
1103}
1104
1105TEST(KeyTest, InsideContainsUsingMultimap) {
zhanyong.wan95b12332009-09-25 18:55:50 +00001106 std::multimap<int, char> container;
1107 container.insert(std::make_pair(1, 'a'));
1108 container.insert(std::make_pair(2, 'b'));
1109 container.insert(std::make_pair(4, 'c'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001110
1111 EXPECT_THAT(container, Not(Contains(Key(25))));
zhanyong.wan95b12332009-09-25 18:55:50 +00001112 container.insert(std::make_pair(25, 'd'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001113 EXPECT_THAT(container, Contains(Key(25)));
zhanyong.wan95b12332009-09-25 18:55:50 +00001114 container.insert(std::make_pair(25, 'e'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001115 EXPECT_THAT(container, Contains(Key(25)));
1116
1117 EXPECT_THAT(container, Contains(Key(1)));
1118 EXPECT_THAT(container, Not(Contains(Key(3))));
1119}
1120
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001121TEST(PairTest, Typing) {
1122 // Test verifies the following type conversions can be compiled.
1123 Matcher<const std::pair<const char*, int>&> m1 = Pair("foo", 42);
1124 Matcher<const std::pair<const char*, int> > m2 = Pair("foo", 42);
1125 Matcher<std::pair<const char*, int> > m3 = Pair("foo", 42);
1126
1127 Matcher<std::pair<int, const std::string> > m4 = Pair(25, "42");
1128 Matcher<std::pair<const std::string, int> > m5 = Pair("25", 42);
1129}
1130
1131TEST(PairTest, CanDescribeSelf) {
1132 Matcher<const std::pair<std::string, int>&> m1 = Pair("foo", 42);
1133 EXPECT_EQ("has a first field that is equal to \"foo\""
1134 ", and has a second field that is equal to 42",
1135 Describe(m1));
1136 EXPECT_EQ("has a first field that is not equal to \"foo\""
1137 ", or has a second field that is not equal to 42",
1138 DescribeNegation(m1));
1139 // Double and triple negation (1 or 2 times not and description of negation).
1140 Matcher<const std::pair<int, int>&> m2 = Not(Pair(Not(13), 42));
1141 EXPECT_EQ("has a first field that is not equal to 13"
1142 ", and has a second field that is equal to 42",
1143 DescribeNegation(m2));
1144}
1145
1146TEST(PairTest, CanExplainMatchResultTo) {
zhanyong.wan82113312010-01-08 21:55:40 +00001147 // If neither field matches, Pair() should explain about the first
1148 // field.
1149 const Matcher<std::pair<int, int> > m = Pair(GreaterThan(0), GreaterThan(0));
1150 EXPECT_EQ("the first field is 1 less than 0",
1151 Explain(m, std::make_pair(-1, -2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001152
zhanyong.wan82113312010-01-08 21:55:40 +00001153 // If the first field matches but the second doesn't, Pair() should
1154 // explain about the second field.
1155 EXPECT_EQ("the second field is 2 less than 0",
1156 Explain(m, std::make_pair(1, -2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001157
zhanyong.wan82113312010-01-08 21:55:40 +00001158 // If the first field doesn't match but the second does, Pair()
1159 // should explain about the first field.
1160 EXPECT_EQ("the first field is 1 less than 0",
1161 Explain(m, std::make_pair(-1, 2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001162
zhanyong.wan82113312010-01-08 21:55:40 +00001163 // If both fields match, Pair() should explain about them both.
1164 EXPECT_EQ("the first field is 1 more than 0"
1165 ", and the second field is 2 more than 0",
1166 Explain(m, std::make_pair(1, 2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001167}
1168
1169TEST(PairTest, MatchesCorrectly) {
1170 std::pair<int, std::string> p(25, "foo");
1171
1172 // Both fields match.
1173 EXPECT_THAT(p, Pair(25, "foo"));
1174 EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o")));
1175
1176 // 'first' doesnt' match, but 'second' matches.
1177 EXPECT_THAT(p, Not(Pair(42, "foo")));
1178 EXPECT_THAT(p, Not(Pair(Lt(25), "foo")));
1179
1180 // 'first' matches, but 'second' doesn't match.
1181 EXPECT_THAT(p, Not(Pair(25, "bar")));
1182 EXPECT_THAT(p, Not(Pair(25, Not("foo"))));
1183
1184 // Neither field matches.
1185 EXPECT_THAT(p, Not(Pair(13, "bar")));
1186 EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a"))));
1187}
1188
1189TEST(PairTest, SafelyCastsInnerMatchers) {
1190 Matcher<int> is_positive = Gt(0);
1191 Matcher<int> is_negative = Lt(0);
1192 std::pair<char, bool> p('a', true);
1193 EXPECT_THAT(p, Pair(is_positive, _));
1194 EXPECT_THAT(p, Not(Pair(is_negative, _)));
1195 EXPECT_THAT(p, Pair(_, is_positive));
1196 EXPECT_THAT(p, Not(Pair(_, is_negative)));
1197}
1198
1199TEST(PairTest, InsideContainsUsingMap) {
zhanyong.wan95b12332009-09-25 18:55:50 +00001200 std::map<int, char> container;
1201 container.insert(std::make_pair(1, 'a'));
1202 container.insert(std::make_pair(2, 'b'));
1203 container.insert(std::make_pair(4, 'c'));
1204 EXPECT_THAT(container, Contains(Pair(1, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001205 EXPECT_THAT(container, Contains(Pair(1, _)));
zhanyong.wan95b12332009-09-25 18:55:50 +00001206 EXPECT_THAT(container, Contains(Pair(_, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001207 EXPECT_THAT(container, Not(Contains(Pair(3, _))));
1208}
1209
shiqiane35fdd92008-12-10 05:08:54 +00001210// Tests StartsWith(s).
1211
1212TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
1213 const Matcher<const char*> m1 = StartsWith(string(""));
1214 EXPECT_TRUE(m1.Matches("Hi"));
1215 EXPECT_TRUE(m1.Matches(""));
1216 EXPECT_FALSE(m1.Matches(NULL));
1217
1218 const Matcher<const string&> m2 = StartsWith("Hi");
1219 EXPECT_TRUE(m2.Matches("Hi"));
1220 EXPECT_TRUE(m2.Matches("Hi Hi!"));
1221 EXPECT_TRUE(m2.Matches("High"));
1222 EXPECT_FALSE(m2.Matches("H"));
1223 EXPECT_FALSE(m2.Matches(" Hi"));
1224}
1225
1226TEST(StartsWithTest, CanDescribeSelf) {
1227 Matcher<const std::string> m = StartsWith("Hi");
1228 EXPECT_EQ("starts with \"Hi\"", Describe(m));
1229}
1230
1231// Tests EndsWith(s).
1232
1233TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
1234 const Matcher<const char*> m1 = EndsWith("");
1235 EXPECT_TRUE(m1.Matches("Hi"));
1236 EXPECT_TRUE(m1.Matches(""));
1237 EXPECT_FALSE(m1.Matches(NULL));
1238
1239 const Matcher<const string&> m2 = EndsWith(string("Hi"));
1240 EXPECT_TRUE(m2.Matches("Hi"));
1241 EXPECT_TRUE(m2.Matches("Wow Hi Hi"));
1242 EXPECT_TRUE(m2.Matches("Super Hi"));
1243 EXPECT_FALSE(m2.Matches("i"));
1244 EXPECT_FALSE(m2.Matches("Hi "));
1245}
1246
1247TEST(EndsWithTest, CanDescribeSelf) {
1248 Matcher<const std::string> m = EndsWith("Hi");
1249 EXPECT_EQ("ends with \"Hi\"", Describe(m));
1250}
1251
1252#ifdef GMOCK_HAS_REGEX
1253
1254// Tests MatchesRegex().
1255
1256TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
1257 const Matcher<const char*> m1 = MatchesRegex("a.*z");
1258 EXPECT_TRUE(m1.Matches("az"));
1259 EXPECT_TRUE(m1.Matches("abcz"));
1260 EXPECT_FALSE(m1.Matches(NULL));
1261
1262 const Matcher<const string&> m2 = MatchesRegex(new RE("a.*z"));
1263 EXPECT_TRUE(m2.Matches("azbz"));
1264 EXPECT_FALSE(m2.Matches("az1"));
1265 EXPECT_FALSE(m2.Matches("1az"));
1266}
1267
1268TEST(MatchesRegexTest, CanDescribeSelf) {
1269 Matcher<const std::string> m1 = MatchesRegex(string("Hi.*"));
1270 EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1));
1271
1272 Matcher<const char*> m2 = MatchesRegex(new RE("[a-z].*"));
1273 EXPECT_EQ("matches regular expression \"[a-z].*\"", Describe(m2));
1274}
1275
1276// Tests ContainsRegex().
1277
1278TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
1279 const Matcher<const char*> m1 = ContainsRegex(string("a.*z"));
1280 EXPECT_TRUE(m1.Matches("az"));
1281 EXPECT_TRUE(m1.Matches("0abcz1"));
1282 EXPECT_FALSE(m1.Matches(NULL));
1283
1284 const Matcher<const string&> m2 = ContainsRegex(new RE("a.*z"));
1285 EXPECT_TRUE(m2.Matches("azbz"));
1286 EXPECT_TRUE(m2.Matches("az1"));
1287 EXPECT_FALSE(m2.Matches("1a"));
1288}
1289
1290TEST(ContainsRegexTest, CanDescribeSelf) {
1291 Matcher<const std::string> m1 = ContainsRegex("Hi.*");
1292 EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1));
1293
1294 Matcher<const char*> m2 = ContainsRegex(new RE("[a-z].*"));
1295 EXPECT_EQ("contains regular expression \"[a-z].*\"", Describe(m2));
1296}
1297#endif // GMOCK_HAS_REGEX
1298
1299// Tests for wide strings.
1300#if GTEST_HAS_STD_WSTRING
1301TEST(StdWideStrEqTest, MatchesEqual) {
1302 Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello"));
1303 EXPECT_TRUE(m.Matches(L"Hello"));
1304 EXPECT_FALSE(m.Matches(L"hello"));
1305 EXPECT_FALSE(m.Matches(NULL));
1306
1307 Matcher<const ::std::wstring&> m2 = StrEq(L"Hello");
1308 EXPECT_TRUE(m2.Matches(L"Hello"));
1309 EXPECT_FALSE(m2.Matches(L"Hi"));
1310
1311 Matcher<const ::std::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1312 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1313 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1314
1315 ::std::wstring str(L"01204500800");
1316 str[3] = L'\0';
1317 Matcher<const ::std::wstring&> m4 = StrEq(str);
1318 EXPECT_TRUE(m4.Matches(str));
1319 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1320 Matcher<const ::std::wstring&> m5 = StrEq(str);
1321 EXPECT_TRUE(m5.Matches(str));
1322}
1323
1324TEST(StdWideStrEqTest, CanDescribeSelf) {
1325 Matcher< ::std::wstring> m = StrEq(L"Hi-\'\"\?\\\a\b\f\n\r\t\v");
1326 EXPECT_EQ("is equal to L\"Hi-\'\\\"\\?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
1327 Describe(m));
1328
1329 Matcher< ::std::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1330 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1331 Describe(m2));
1332
1333 ::std::wstring str(L"01204500800");
1334 str[3] = L'\0';
1335 Matcher<const ::std::wstring&> m4 = StrEq(str);
1336 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1337 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1338 Matcher<const ::std::wstring&> m5 = StrEq(str);
1339 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1340}
1341
1342TEST(StdWideStrNeTest, MatchesUnequalString) {
1343 Matcher<const wchar_t*> m = StrNe(L"Hello");
1344 EXPECT_TRUE(m.Matches(L""));
1345 EXPECT_TRUE(m.Matches(NULL));
1346 EXPECT_FALSE(m.Matches(L"Hello"));
1347
1348 Matcher< ::std::wstring> m2 = StrNe(::std::wstring(L"Hello"));
1349 EXPECT_TRUE(m2.Matches(L"hello"));
1350 EXPECT_FALSE(m2.Matches(L"Hello"));
1351}
1352
1353TEST(StdWideStrNeTest, CanDescribeSelf) {
1354 Matcher<const wchar_t*> m = StrNe(L"Hi");
1355 EXPECT_EQ("is not equal to L\"Hi\"", Describe(m));
1356}
1357
1358TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1359 Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello"));
1360 EXPECT_TRUE(m.Matches(L"Hello"));
1361 EXPECT_TRUE(m.Matches(L"hello"));
1362 EXPECT_FALSE(m.Matches(L"Hi"));
1363 EXPECT_FALSE(m.Matches(NULL));
1364
1365 Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello");
1366 EXPECT_TRUE(m2.Matches(L"hello"));
1367 EXPECT_FALSE(m2.Matches(L"Hi"));
1368}
1369
1370TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1371 ::std::wstring str1(L"oabocdooeoo");
1372 ::std::wstring str2(L"OABOCDOOEOO");
1373 Matcher<const ::std::wstring&> m0 = StrCaseEq(str1);
1374 EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L'\0')));
1375
1376 str1[3] = str2[3] = L'\0';
1377 Matcher<const ::std::wstring&> m1 = StrCaseEq(str1);
1378 EXPECT_TRUE(m1.Matches(str2));
1379
1380 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1381 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1382 Matcher<const ::std::wstring&> m2 = StrCaseEq(str1);
1383 str1[9] = str2[9] = L'\0';
1384 EXPECT_FALSE(m2.Matches(str2));
1385
1386 Matcher<const ::std::wstring&> m3 = StrCaseEq(str1);
1387 EXPECT_TRUE(m3.Matches(str2));
1388
1389 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1390 str2.append(1, L'\0');
1391 EXPECT_FALSE(m3.Matches(str2));
1392 EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9)));
1393}
1394
1395TEST(StdWideStrCaseEqTest, CanDescribeSelf) {
1396 Matcher< ::std::wstring> m = StrCaseEq(L"Hi");
1397 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1398}
1399
1400TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1401 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1402 EXPECT_TRUE(m.Matches(L"Hi"));
1403 EXPECT_TRUE(m.Matches(NULL));
1404 EXPECT_FALSE(m.Matches(L"Hello"));
1405 EXPECT_FALSE(m.Matches(L"hello"));
1406
1407 Matcher< ::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello"));
1408 EXPECT_TRUE(m2.Matches(L""));
1409 EXPECT_FALSE(m2.Matches(L"Hello"));
1410}
1411
1412TEST(StdWideStrCaseNeTest, CanDescribeSelf) {
1413 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
1414 EXPECT_EQ("is not equal to (ignoring case) L\"Hi\"", Describe(m));
1415}
1416
1417// Tests that HasSubstr() works for matching wstring-typed values.
1418TEST(StdWideHasSubstrTest, WorksForStringClasses) {
1419 const Matcher< ::std::wstring> m1 = HasSubstr(L"foo");
1420 EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food.")));
1421 EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo")));
1422
1423 const Matcher<const ::std::wstring&> m2 = HasSubstr(L"foo");
1424 EXPECT_TRUE(m2.Matches(::std::wstring(L"I love food.")));
1425 EXPECT_FALSE(m2.Matches(::std::wstring(L"tofo")));
1426}
1427
1428// Tests that HasSubstr() works for matching C-wide-string-typed values.
1429TEST(StdWideHasSubstrTest, WorksForCStrings) {
1430 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1431 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1432 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1433 EXPECT_FALSE(m1.Matches(NULL));
1434
1435 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1436 EXPECT_TRUE(m2.Matches(L"I love food."));
1437 EXPECT_FALSE(m2.Matches(L"tofo"));
1438 EXPECT_FALSE(m2.Matches(NULL));
1439}
1440
1441// Tests that HasSubstr(s) describes itself properly.
1442TEST(StdWideHasSubstrTest, CanDescribeSelf) {
1443 Matcher< ::std::wstring> m = HasSubstr(L"foo\n\"");
1444 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1445}
1446
1447// Tests StartsWith(s).
1448
1449TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) {
1450 const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L""));
1451 EXPECT_TRUE(m1.Matches(L"Hi"));
1452 EXPECT_TRUE(m1.Matches(L""));
1453 EXPECT_FALSE(m1.Matches(NULL));
1454
1455 const Matcher<const ::std::wstring&> m2 = StartsWith(L"Hi");
1456 EXPECT_TRUE(m2.Matches(L"Hi"));
1457 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1458 EXPECT_TRUE(m2.Matches(L"High"));
1459 EXPECT_FALSE(m2.Matches(L"H"));
1460 EXPECT_FALSE(m2.Matches(L" Hi"));
1461}
1462
1463TEST(StdWideStartsWithTest, CanDescribeSelf) {
1464 Matcher<const ::std::wstring> m = StartsWith(L"Hi");
1465 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1466}
1467
1468// Tests EndsWith(s).
1469
1470TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) {
1471 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1472 EXPECT_TRUE(m1.Matches(L"Hi"));
1473 EXPECT_TRUE(m1.Matches(L""));
1474 EXPECT_FALSE(m1.Matches(NULL));
1475
1476 const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L"Hi"));
1477 EXPECT_TRUE(m2.Matches(L"Hi"));
1478 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1479 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1480 EXPECT_FALSE(m2.Matches(L"i"));
1481 EXPECT_FALSE(m2.Matches(L"Hi "));
1482}
1483
1484TEST(StdWideEndsWithTest, CanDescribeSelf) {
1485 Matcher<const ::std::wstring> m = EndsWith(L"Hi");
1486 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1487}
1488
1489#endif // GTEST_HAS_STD_WSTRING
1490
1491#if GTEST_HAS_GLOBAL_WSTRING
1492TEST(GlobalWideStrEqTest, MatchesEqual) {
1493 Matcher<const wchar_t*> m = StrEq(::wstring(L"Hello"));
1494 EXPECT_TRUE(m.Matches(L"Hello"));
1495 EXPECT_FALSE(m.Matches(L"hello"));
1496 EXPECT_FALSE(m.Matches(NULL));
1497
1498 Matcher<const ::wstring&> m2 = StrEq(L"Hello");
1499 EXPECT_TRUE(m2.Matches(L"Hello"));
1500 EXPECT_FALSE(m2.Matches(L"Hi"));
1501
1502 Matcher<const ::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1503 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1504 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1505
1506 ::wstring str(L"01204500800");
1507 str[3] = L'\0';
1508 Matcher<const ::wstring&> m4 = StrEq(str);
1509 EXPECT_TRUE(m4.Matches(str));
1510 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1511 Matcher<const ::wstring&> m5 = StrEq(str);
1512 EXPECT_TRUE(m5.Matches(str));
1513}
1514
1515TEST(GlobalWideStrEqTest, CanDescribeSelf) {
1516 Matcher< ::wstring> m = StrEq(L"Hi-\'\"\?\\\a\b\f\n\r\t\v");
1517 EXPECT_EQ("is equal to L\"Hi-\'\\\"\\?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
1518 Describe(m));
1519
1520 Matcher< ::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1521 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1522 Describe(m2));
1523
1524 ::wstring str(L"01204500800");
1525 str[3] = L'\0';
1526 Matcher<const ::wstring&> m4 = StrEq(str);
1527 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1528 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1529 Matcher<const ::wstring&> m5 = StrEq(str);
1530 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1531}
1532
1533TEST(GlobalWideStrNeTest, MatchesUnequalString) {
1534 Matcher<const wchar_t*> m = StrNe(L"Hello");
1535 EXPECT_TRUE(m.Matches(L""));
1536 EXPECT_TRUE(m.Matches(NULL));
1537 EXPECT_FALSE(m.Matches(L"Hello"));
1538
1539 Matcher< ::wstring> m2 = StrNe(::wstring(L"Hello"));
1540 EXPECT_TRUE(m2.Matches(L"hello"));
1541 EXPECT_FALSE(m2.Matches(L"Hello"));
1542}
1543
1544TEST(GlobalWideStrNeTest, CanDescribeSelf) {
1545 Matcher<const wchar_t*> m = StrNe(L"Hi");
1546 EXPECT_EQ("is not equal to L\"Hi\"", Describe(m));
1547}
1548
1549TEST(GlobalWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1550 Matcher<const wchar_t*> m = StrCaseEq(::wstring(L"Hello"));
1551 EXPECT_TRUE(m.Matches(L"Hello"));
1552 EXPECT_TRUE(m.Matches(L"hello"));
1553 EXPECT_FALSE(m.Matches(L"Hi"));
1554 EXPECT_FALSE(m.Matches(NULL));
1555
1556 Matcher<const ::wstring&> m2 = StrCaseEq(L"Hello");
1557 EXPECT_TRUE(m2.Matches(L"hello"));
1558 EXPECT_FALSE(m2.Matches(L"Hi"));
1559}
1560
1561TEST(GlobalWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1562 ::wstring str1(L"oabocdooeoo");
1563 ::wstring str2(L"OABOCDOOEOO");
1564 Matcher<const ::wstring&> m0 = StrCaseEq(str1);
1565 EXPECT_FALSE(m0.Matches(str2 + ::wstring(1, L'\0')));
1566
1567 str1[3] = str2[3] = L'\0';
1568 Matcher<const ::wstring&> m1 = StrCaseEq(str1);
1569 EXPECT_TRUE(m1.Matches(str2));
1570
1571 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1572 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1573 Matcher<const ::wstring&> m2 = StrCaseEq(str1);
1574 str1[9] = str2[9] = L'\0';
1575 EXPECT_FALSE(m2.Matches(str2));
1576
1577 Matcher<const ::wstring&> m3 = StrCaseEq(str1);
1578 EXPECT_TRUE(m3.Matches(str2));
1579
1580 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1581 str2.append(1, L'\0');
1582 EXPECT_FALSE(m3.Matches(str2));
1583 EXPECT_FALSE(m3.Matches(::wstring(str2, 0, 9)));
1584}
1585
1586TEST(GlobalWideStrCaseEqTest, CanDescribeSelf) {
1587 Matcher< ::wstring> m = StrCaseEq(L"Hi");
1588 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1589}
1590
1591TEST(GlobalWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1592 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1593 EXPECT_TRUE(m.Matches(L"Hi"));
1594 EXPECT_TRUE(m.Matches(NULL));
1595 EXPECT_FALSE(m.Matches(L"Hello"));
1596 EXPECT_FALSE(m.Matches(L"hello"));
1597
1598 Matcher< ::wstring> m2 = StrCaseNe(::wstring(L"Hello"));
1599 EXPECT_TRUE(m2.Matches(L""));
1600 EXPECT_FALSE(m2.Matches(L"Hello"));
1601}
1602
1603TEST(GlobalWideStrCaseNeTest, CanDescribeSelf) {
1604 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
1605 EXPECT_EQ("is not equal to (ignoring case) L\"Hi\"", Describe(m));
1606}
1607
1608// Tests that HasSubstr() works for matching wstring-typed values.
1609TEST(GlobalWideHasSubstrTest, WorksForStringClasses) {
1610 const Matcher< ::wstring> m1 = HasSubstr(L"foo");
1611 EXPECT_TRUE(m1.Matches(::wstring(L"I love food.")));
1612 EXPECT_FALSE(m1.Matches(::wstring(L"tofo")));
1613
1614 const Matcher<const ::wstring&> m2 = HasSubstr(L"foo");
1615 EXPECT_TRUE(m2.Matches(::wstring(L"I love food.")));
1616 EXPECT_FALSE(m2.Matches(::wstring(L"tofo")));
1617}
1618
1619// Tests that HasSubstr() works for matching C-wide-string-typed values.
1620TEST(GlobalWideHasSubstrTest, WorksForCStrings) {
1621 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1622 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1623 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1624 EXPECT_FALSE(m1.Matches(NULL));
1625
1626 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1627 EXPECT_TRUE(m2.Matches(L"I love food."));
1628 EXPECT_FALSE(m2.Matches(L"tofo"));
1629 EXPECT_FALSE(m2.Matches(NULL));
1630}
1631
1632// Tests that HasSubstr(s) describes itself properly.
1633TEST(GlobalWideHasSubstrTest, CanDescribeSelf) {
1634 Matcher< ::wstring> m = HasSubstr(L"foo\n\"");
1635 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1636}
1637
1638// Tests StartsWith(s).
1639
1640TEST(GlobalWideStartsWithTest, MatchesStringWithGivenPrefix) {
1641 const Matcher<const wchar_t*> m1 = StartsWith(::wstring(L""));
1642 EXPECT_TRUE(m1.Matches(L"Hi"));
1643 EXPECT_TRUE(m1.Matches(L""));
1644 EXPECT_FALSE(m1.Matches(NULL));
1645
1646 const Matcher<const ::wstring&> m2 = StartsWith(L"Hi");
1647 EXPECT_TRUE(m2.Matches(L"Hi"));
1648 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1649 EXPECT_TRUE(m2.Matches(L"High"));
1650 EXPECT_FALSE(m2.Matches(L"H"));
1651 EXPECT_FALSE(m2.Matches(L" Hi"));
1652}
1653
1654TEST(GlobalWideStartsWithTest, CanDescribeSelf) {
1655 Matcher<const ::wstring> m = StartsWith(L"Hi");
1656 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1657}
1658
1659// Tests EndsWith(s).
1660
1661TEST(GlobalWideEndsWithTest, MatchesStringWithGivenSuffix) {
1662 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1663 EXPECT_TRUE(m1.Matches(L"Hi"));
1664 EXPECT_TRUE(m1.Matches(L""));
1665 EXPECT_FALSE(m1.Matches(NULL));
1666
1667 const Matcher<const ::wstring&> m2 = EndsWith(::wstring(L"Hi"));
1668 EXPECT_TRUE(m2.Matches(L"Hi"));
1669 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1670 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1671 EXPECT_FALSE(m2.Matches(L"i"));
1672 EXPECT_FALSE(m2.Matches(L"Hi "));
1673}
1674
1675TEST(GlobalWideEndsWithTest, CanDescribeSelf) {
1676 Matcher<const ::wstring> m = EndsWith(L"Hi");
1677 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1678}
1679
1680#endif // GTEST_HAS_GLOBAL_WSTRING
1681
1682
1683typedef ::std::tr1::tuple<long, int> Tuple2; // NOLINT
1684
1685// Tests that Eq() matches a 2-tuple where the first field == the
1686// second field.
1687TEST(Eq2Test, MatchesEqualArguments) {
1688 Matcher<const Tuple2&> m = Eq();
1689 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1690 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1691}
1692
1693// Tests that Eq() describes itself properly.
1694TEST(Eq2Test, CanDescribeSelf) {
1695 Matcher<const Tuple2&> m = Eq();
zhanyong.wan2661c682009-06-09 05:42:12 +00001696 EXPECT_EQ("are a pair (x, y) where x == y", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001697}
1698
1699// Tests that Ge() matches a 2-tuple where the first field >= the
1700// second field.
1701TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) {
1702 Matcher<const Tuple2&> m = Ge();
1703 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1704 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1705 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1706}
1707
1708// Tests that Ge() describes itself properly.
1709TEST(Ge2Test, CanDescribeSelf) {
1710 Matcher<const Tuple2&> m = Ge();
zhanyong.wan2661c682009-06-09 05:42:12 +00001711 EXPECT_EQ("are a pair (x, y) where x >= y", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001712}
1713
1714// Tests that Gt() matches a 2-tuple where the first field > the
1715// second field.
1716TEST(Gt2Test, MatchesGreaterThanArguments) {
1717 Matcher<const Tuple2&> m = Gt();
1718 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1719 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1720 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1721}
1722
1723// Tests that Gt() describes itself properly.
1724TEST(Gt2Test, CanDescribeSelf) {
1725 Matcher<const Tuple2&> m = Gt();
zhanyong.wan2661c682009-06-09 05:42:12 +00001726 EXPECT_EQ("are a pair (x, y) where x > y", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001727}
1728
1729// Tests that Le() matches a 2-tuple where the first field <= the
1730// second field.
1731TEST(Le2Test, MatchesLessThanOrEqualArguments) {
1732 Matcher<const Tuple2&> m = Le();
1733 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1734 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1735 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1736}
1737
1738// Tests that Le() describes itself properly.
1739TEST(Le2Test, CanDescribeSelf) {
1740 Matcher<const Tuple2&> m = Le();
zhanyong.wan2661c682009-06-09 05:42:12 +00001741 EXPECT_EQ("are a pair (x, y) where x <= y", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001742}
1743
1744// Tests that Lt() matches a 2-tuple where the first field < the
1745// second field.
1746TEST(Lt2Test, MatchesLessThanArguments) {
1747 Matcher<const Tuple2&> m = Lt();
1748 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1749 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1750 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1751}
1752
1753// Tests that Lt() describes itself properly.
1754TEST(Lt2Test, CanDescribeSelf) {
1755 Matcher<const Tuple2&> m = Lt();
zhanyong.wan2661c682009-06-09 05:42:12 +00001756 EXPECT_EQ("are a pair (x, y) where x < y", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001757}
1758
1759// Tests that Ne() matches a 2-tuple where the first field != the
1760// second field.
1761TEST(Ne2Test, MatchesUnequalArguments) {
1762 Matcher<const Tuple2&> m = Ne();
1763 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1764 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1765 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1766}
1767
1768// Tests that Ne() describes itself properly.
1769TEST(Ne2Test, CanDescribeSelf) {
1770 Matcher<const Tuple2&> m = Ne();
zhanyong.wan2661c682009-06-09 05:42:12 +00001771 EXPECT_EQ("are a pair (x, y) where x != y", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001772}
1773
1774// Tests that Not(m) matches any value that doesn't match m.
1775TEST(NotTest, NegatesMatcher) {
1776 Matcher<int> m;
1777 m = Not(Eq(2));
1778 EXPECT_TRUE(m.Matches(3));
1779 EXPECT_FALSE(m.Matches(2));
1780}
1781
1782// Tests that Not(m) describes itself properly.
1783TEST(NotTest, CanDescribeSelf) {
1784 Matcher<int> m = Not(Eq(5));
1785 EXPECT_EQ("is not equal to 5", Describe(m));
1786}
1787
zhanyong.wan18490652009-05-11 18:54:08 +00001788// Tests that monomorphic matchers are safely cast by the Not matcher.
1789TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {
1790 // greater_than_5 is a monomorphic matcher.
1791 Matcher<int> greater_than_5 = Gt(5);
1792
1793 Matcher<const int&> m = Not(greater_than_5);
1794 Matcher<int&> m2 = Not(greater_than_5);
1795 Matcher<int&> m3 = Not(m);
1796}
1797
shiqiane35fdd92008-12-10 05:08:54 +00001798// Tests that AllOf(m1, ..., mn) matches any value that matches all of
1799// the given matchers.
1800TEST(AllOfTest, MatchesWhenAllMatch) {
1801 Matcher<int> m;
1802 m = AllOf(Le(2), Ge(1));
1803 EXPECT_TRUE(m.Matches(1));
1804 EXPECT_TRUE(m.Matches(2));
1805 EXPECT_FALSE(m.Matches(0));
1806 EXPECT_FALSE(m.Matches(3));
1807
1808 m = AllOf(Gt(0), Ne(1), Ne(2));
1809 EXPECT_TRUE(m.Matches(3));
1810 EXPECT_FALSE(m.Matches(2));
1811 EXPECT_FALSE(m.Matches(1));
1812 EXPECT_FALSE(m.Matches(0));
1813
1814 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
1815 EXPECT_TRUE(m.Matches(4));
1816 EXPECT_FALSE(m.Matches(3));
1817 EXPECT_FALSE(m.Matches(2));
1818 EXPECT_FALSE(m.Matches(1));
1819 EXPECT_FALSE(m.Matches(0));
1820
1821 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
1822 EXPECT_TRUE(m.Matches(0));
1823 EXPECT_TRUE(m.Matches(1));
1824 EXPECT_FALSE(m.Matches(3));
1825}
1826
1827// Tests that AllOf(m1, ..., mn) describes itself properly.
1828TEST(AllOfTest, CanDescribeSelf) {
1829 Matcher<int> m;
1830 m = AllOf(Le(2), Ge(1));
1831 EXPECT_EQ("(is less than or equal to 2) and "
1832 "(is greater than or equal to 1)",
1833 Describe(m));
1834
1835 m = AllOf(Gt(0), Ne(1), Ne(2));
1836 EXPECT_EQ("(is greater than 0) and "
1837 "((is not equal to 1) and "
1838 "(is not equal to 2))",
1839 Describe(m));
1840
1841
1842 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
1843 EXPECT_EQ("(is greater than 0) and "
1844 "((is not equal to 1) and "
1845 "((is not equal to 2) and "
1846 "(is not equal to 3)))",
1847 Describe(m));
1848
1849
1850 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
1851 EXPECT_EQ("(is greater than or equal to 0) and "
1852 "((is less than 10) and "
1853 "((is not equal to 3) and "
1854 "((is not equal to 5) and "
1855 "(is not equal to 7))))", Describe(m));
1856}
1857
zhanyong.wan18490652009-05-11 18:54:08 +00001858// Tests that monomorphic matchers are safely cast by the AllOf matcher.
1859TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
1860 // greater_than_5 and less_than_10 are monomorphic matchers.
1861 Matcher<int> greater_than_5 = Gt(5);
1862 Matcher<int> less_than_10 = Lt(10);
1863
1864 Matcher<const int&> m = AllOf(greater_than_5, less_than_10);
1865 Matcher<int&> m2 = AllOf(greater_than_5, less_than_10);
1866 Matcher<int&> m3 = AllOf(greater_than_5, m2);
1867
1868 // Tests that BothOf works when composing itself.
1869 Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10);
1870 Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);
1871}
1872
shiqiane35fdd92008-12-10 05:08:54 +00001873// Tests that AnyOf(m1, ..., mn) matches any value that matches at
1874// least one of the given matchers.
1875TEST(AnyOfTest, MatchesWhenAnyMatches) {
1876 Matcher<int> m;
1877 m = AnyOf(Le(1), Ge(3));
1878 EXPECT_TRUE(m.Matches(1));
1879 EXPECT_TRUE(m.Matches(4));
1880 EXPECT_FALSE(m.Matches(2));
1881
1882 m = AnyOf(Lt(0), Eq(1), Eq(2));
1883 EXPECT_TRUE(m.Matches(-1));
1884 EXPECT_TRUE(m.Matches(1));
1885 EXPECT_TRUE(m.Matches(2));
1886 EXPECT_FALSE(m.Matches(0));
1887
1888 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
1889 EXPECT_TRUE(m.Matches(-1));
1890 EXPECT_TRUE(m.Matches(1));
1891 EXPECT_TRUE(m.Matches(2));
1892 EXPECT_TRUE(m.Matches(3));
1893 EXPECT_FALSE(m.Matches(0));
1894
1895 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
1896 EXPECT_TRUE(m.Matches(0));
1897 EXPECT_TRUE(m.Matches(11));
1898 EXPECT_TRUE(m.Matches(3));
1899 EXPECT_FALSE(m.Matches(2));
1900}
1901
1902// Tests that AnyOf(m1, ..., mn) describes itself properly.
1903TEST(AnyOfTest, CanDescribeSelf) {
1904 Matcher<int> m;
1905 m = AnyOf(Le(1), Ge(3));
1906 EXPECT_EQ("(is less than or equal to 1) or "
1907 "(is greater than or equal to 3)",
1908 Describe(m));
1909
1910 m = AnyOf(Lt(0), Eq(1), Eq(2));
1911 EXPECT_EQ("(is less than 0) or "
1912 "((is equal to 1) or (is equal to 2))",
1913 Describe(m));
1914
1915 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
1916 EXPECT_EQ("(is less than 0) or "
1917 "((is equal to 1) or "
1918 "((is equal to 2) or "
1919 "(is equal to 3)))",
1920 Describe(m));
1921
1922 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
1923 EXPECT_EQ("(is less than or equal to 0) or "
1924 "((is greater than 10) or "
1925 "((is equal to 3) or "
1926 "((is equal to 5) or "
1927 "(is equal to 7))))",
1928 Describe(m));
1929}
1930
zhanyong.wan18490652009-05-11 18:54:08 +00001931// Tests that monomorphic matchers are safely cast by the AnyOf matcher.
1932TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
1933 // greater_than_5 and less_than_10 are monomorphic matchers.
1934 Matcher<int> greater_than_5 = Gt(5);
1935 Matcher<int> less_than_10 = Lt(10);
1936
1937 Matcher<const int&> m = AnyOf(greater_than_5, less_than_10);
1938 Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10);
1939 Matcher<int&> m3 = AnyOf(greater_than_5, m2);
1940
1941 // Tests that EitherOf works when composing itself.
1942 Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10);
1943 Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);
1944}
1945
shiqiane35fdd92008-12-10 05:08:54 +00001946// The following predicate function and predicate functor are for
1947// testing the Truly(predicate) matcher.
1948
1949// Returns non-zero if the input is positive. Note that the return
1950// type of this function is not bool. It's OK as Truly() accepts any
1951// unary function or functor whose return type can be implicitly
1952// converted to bool.
1953int IsPositive(double x) {
1954 return x > 0 ? 1 : 0;
1955}
1956
1957// This functor returns true if the input is greater than the given
1958// number.
1959class IsGreaterThan {
1960 public:
1961 explicit IsGreaterThan(int threshold) : threshold_(threshold) {}
1962
1963 bool operator()(int n) const { return n > threshold_; }
zhanyong.wan32de5f52009-12-23 00:13:23 +00001964
shiqiane35fdd92008-12-10 05:08:54 +00001965 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +00001966 int threshold_;
shiqiane35fdd92008-12-10 05:08:54 +00001967};
1968
1969// For testing Truly().
1970const int foo = 0;
1971
1972// This predicate returns true iff the argument references foo and has
1973// a zero value.
1974bool ReferencesFooAndIsZero(const int& n) {
1975 return (&n == &foo) && (n == 0);
1976}
1977
1978// Tests that Truly(predicate) matches what satisfies the given
1979// predicate.
1980TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {
1981 Matcher<double> m = Truly(IsPositive);
1982 EXPECT_TRUE(m.Matches(2.0));
1983 EXPECT_FALSE(m.Matches(-1.5));
1984}
1985
1986// Tests that Truly(predicate_functor) works too.
1987TEST(TrulyTest, CanBeUsedWithFunctor) {
1988 Matcher<int> m = Truly(IsGreaterThan(5));
1989 EXPECT_TRUE(m.Matches(6));
1990 EXPECT_FALSE(m.Matches(4));
1991}
1992
1993// Tests that Truly(predicate) can describe itself properly.
1994TEST(TrulyTest, CanDescribeSelf) {
1995 Matcher<double> m = Truly(IsPositive);
1996 EXPECT_EQ("satisfies the given predicate",
1997 Describe(m));
1998}
1999
2000// Tests that Truly(predicate) works when the matcher takes its
2001// argument by reference.
2002TEST(TrulyTest, WorksForByRefArguments) {
2003 Matcher<const int&> m = Truly(ReferencesFooAndIsZero);
2004 EXPECT_TRUE(m.Matches(foo));
2005 int n = 0;
2006 EXPECT_FALSE(m.Matches(n));
2007}
2008
2009// Tests that Matches(m) is a predicate satisfied by whatever that
2010// matches matcher m.
2011TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {
2012 EXPECT_TRUE(Matches(Ge(0))(1));
2013 EXPECT_FALSE(Matches(Eq('a'))('b'));
2014}
2015
2016// Tests that Matches(m) works when the matcher takes its argument by
2017// reference.
2018TEST(MatchesTest, WorksOnByRefArguments) {
2019 int m = 0, n = 0;
2020 EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n));
2021 EXPECT_FALSE(Matches(Ref(m))(n));
2022}
2023
2024// Tests that a Matcher on non-reference type can be used in
2025// Matches().
2026TEST(MatchesTest, WorksWithMatcherOnNonRefType) {
2027 Matcher<int> eq5 = Eq(5);
2028 EXPECT_TRUE(Matches(eq5)(5));
2029 EXPECT_FALSE(Matches(eq5)(2));
2030}
2031
zhanyong.wanb8243162009-06-04 05:48:20 +00002032// Tests Value(value, matcher). Since Value() is a simple wrapper for
2033// Matches(), which has been tested already, we don't spend a lot of
2034// effort on testing Value().
2035TEST(ValueTest, WorksWithPolymorphicMatcher) {
2036 EXPECT_TRUE(Value("hi", StartsWith("h")));
2037 EXPECT_FALSE(Value(5, Gt(10)));
2038}
2039
2040TEST(ValueTest, WorksWithMonomorphicMatcher) {
2041 const Matcher<int> is_zero = Eq(0);
2042 EXPECT_TRUE(Value(0, is_zero));
2043 EXPECT_FALSE(Value('a', is_zero));
2044
2045 int n = 0;
2046 const Matcher<const int&> ref_n = Ref(n);
2047 EXPECT_TRUE(Value(n, ref_n));
2048 EXPECT_FALSE(Value(1, ref_n));
2049}
2050
zhanyong.wanbf550852009-06-09 06:09:53 +00002051TEST(AllArgsTest, WorksForTuple) {
2052 EXPECT_THAT(make_tuple(1, 2L), AllArgs(Lt()));
2053 EXPECT_THAT(make_tuple(2L, 1), Not(AllArgs(Lt())));
2054}
2055
2056TEST(AllArgsTest, WorksForNonTuple) {
2057 EXPECT_THAT(42, AllArgs(Gt(0)));
2058 EXPECT_THAT('a', Not(AllArgs(Eq('b'))));
2059}
2060
2061class AllArgsHelper {
2062 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002063 AllArgsHelper() {}
2064
zhanyong.wanbf550852009-06-09 06:09:53 +00002065 MOCK_METHOD2(Helper, int(char x, int y));
zhanyong.wan32de5f52009-12-23 00:13:23 +00002066
2067 private:
2068 GTEST_DISALLOW_COPY_AND_ASSIGN_(AllArgsHelper);
zhanyong.wanbf550852009-06-09 06:09:53 +00002069};
2070
2071TEST(AllArgsTest, WorksInWithClause) {
2072 AllArgsHelper helper;
2073 ON_CALL(helper, Helper(_, _))
2074 .With(AllArgs(Lt()))
2075 .WillByDefault(Return(1));
2076 EXPECT_CALL(helper, Helper(_, _));
2077 EXPECT_CALL(helper, Helper(_, _))
2078 .With(AllArgs(Gt()))
2079 .WillOnce(Return(2));
2080
2081 EXPECT_EQ(1, helper.Helper('\1', 2));
2082 EXPECT_EQ(2, helper.Helper('a', 1));
2083}
2084
shiqiane35fdd92008-12-10 05:08:54 +00002085// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2086// matches the matcher.
2087TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {
2088 ASSERT_THAT(5, Ge(2)) << "This should succeed.";
2089 ASSERT_THAT("Foo", EndsWith("oo"));
2090 EXPECT_THAT(2, AllOf(Le(7), Ge(0))) << "This should succeed too.";
2091 EXPECT_THAT("Hello", StartsWith("Hell"));
2092}
2093
2094// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2095// doesn't match the matcher.
2096TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {
2097 // 'n' must be static as it is used in an EXPECT_FATAL_FAILURE(),
2098 // which cannot reference auto variables.
2099 static int n;
2100 n = 5;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002101
2102 // VC++ prior to version 8.0 SP1 has a bug where it will not see any
2103 // functions declared in the namespace scope from within nested classes.
2104 // EXPECT/ASSERT_(NON)FATAL_FAILURE macros use nested classes so that all
2105 // namespace-level functions invoked inside them need to be explicitly
2106 // resolved.
2107 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Gt(10)),
shiqiane35fdd92008-12-10 05:08:54 +00002108 "Value of: n\n"
2109 "Expected: is greater than 10\n"
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002110 " Actual: 5");
shiqiane35fdd92008-12-10 05:08:54 +00002111 n = 0;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002112 EXPECT_NONFATAL_FAILURE(
2113 EXPECT_THAT(n, ::testing::AllOf(::testing::Le(7), ::testing::Ge(5))),
2114 "Value of: n\n"
2115 "Expected: (is less than or equal to 7) and "
2116 "(is greater than or equal to 5)\n"
2117 " Actual: 0");
shiqiane35fdd92008-12-10 05:08:54 +00002118}
2119
2120// Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument
2121// has a reference type.
2122TEST(MatcherAssertionTest, WorksForByRefArguments) {
2123 // We use a static variable here as EXPECT_FATAL_FAILURE() cannot
2124 // reference auto variables.
2125 static int n;
2126 n = 0;
2127 EXPECT_THAT(n, AllOf(Le(7), Ref(n)));
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002128 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
shiqiane35fdd92008-12-10 05:08:54 +00002129 "Value of: n\n"
2130 "Expected: does not reference the variable @");
2131 // Tests the "Actual" part.
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002132 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
shiqiane35fdd92008-12-10 05:08:54 +00002133 "Actual: 0 (is located @");
2134}
2135
zhanyong.wan95b12332009-09-25 18:55:50 +00002136#if !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002137// Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is
2138// monomorphic.
zhanyong.wan95b12332009-09-25 18:55:50 +00002139
2140// ASSERT_THAT("hello", starts_with_he) fails to compile with Nokia's
2141// Symbian compiler: it tries to compile
2142// template<T, U> class MatcherCastImpl { ...
2143// virtual bool Matches(T x) const {
2144// return source_matcher_.Matches(static_cast<U>(x));
2145// with U == string and T == const char*
2146// With ASSERT_THAT("hello"...) changed to ASSERT_THAT(string("hello") ... )
2147// the compiler silently crashes with no output.
2148// If MatcherCastImpl is changed to use U(x) instead of static_cast<U>(x)
2149// the code compiles but the converted string is bogus.
shiqiane35fdd92008-12-10 05:08:54 +00002150TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
2151 Matcher<const char*> starts_with_he = StartsWith("he");
2152 ASSERT_THAT("hello", starts_with_he);
2153
2154 Matcher<const string&> ends_with_ok = EndsWith("ok");
2155 ASSERT_THAT("book", ends_with_ok);
2156
2157 Matcher<int> is_greater_than_5 = Gt(5);
2158 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5),
2159 "Value of: 5\n"
2160 "Expected: is greater than 5\n"
2161 " Actual: 5");
2162}
zhanyong.wan95b12332009-09-25 18:55:50 +00002163#endif // !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002164
2165// Tests floating-point matchers.
2166template <typename RawType>
2167class FloatingPointTest : public testing::Test {
2168 protected:
2169 typedef typename testing::internal::FloatingPoint<RawType> Floating;
2170 typedef typename Floating::Bits Bits;
2171
2172 virtual void SetUp() {
2173 const size_t max_ulps = Floating::kMaxUlps;
2174
2175 // The bits that represent 0.0.
2176 const Bits zero_bits = Floating(0).bits();
2177
2178 // Makes some numbers close to 0.0.
2179 close_to_positive_zero_ = Floating::ReinterpretBits(zero_bits + max_ulps/2);
2180 close_to_negative_zero_ = -Floating::ReinterpretBits(
2181 zero_bits + max_ulps - max_ulps/2);
2182 further_from_negative_zero_ = -Floating::ReinterpretBits(
2183 zero_bits + max_ulps + 1 - max_ulps/2);
2184
2185 // The bits that represent 1.0.
2186 const Bits one_bits = Floating(1).bits();
2187
2188 // Makes some numbers close to 1.0.
2189 close_to_one_ = Floating::ReinterpretBits(one_bits + max_ulps);
2190 further_from_one_ = Floating::ReinterpretBits(one_bits + max_ulps + 1);
2191
2192 // +infinity.
2193 infinity_ = Floating::Infinity();
2194
2195 // The bits that represent +infinity.
2196 const Bits infinity_bits = Floating(infinity_).bits();
2197
2198 // Makes some numbers close to infinity.
2199 close_to_infinity_ = Floating::ReinterpretBits(infinity_bits - max_ulps);
2200 further_from_infinity_ = Floating::ReinterpretBits(
2201 infinity_bits - max_ulps - 1);
2202
2203 // Makes some NAN's.
2204 nan1_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 1);
2205 nan2_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 200);
2206 }
2207
2208 void TestSize() {
2209 EXPECT_EQ(sizeof(RawType), sizeof(Bits));
2210 }
2211
2212 // A battery of tests for FloatingEqMatcher::Matches.
2213 // matcher_maker is a pointer to a function which creates a FloatingEqMatcher.
2214 void TestMatches(
2215 testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) {
2216 Matcher<RawType> m1 = matcher_maker(0.0);
2217 EXPECT_TRUE(m1.Matches(-0.0));
2218 EXPECT_TRUE(m1.Matches(close_to_positive_zero_));
2219 EXPECT_TRUE(m1.Matches(close_to_negative_zero_));
2220 EXPECT_FALSE(m1.Matches(1.0));
2221
2222 Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_);
2223 EXPECT_FALSE(m2.Matches(further_from_negative_zero_));
2224
2225 Matcher<RawType> m3 = matcher_maker(1.0);
2226 EXPECT_TRUE(m3.Matches(close_to_one_));
2227 EXPECT_FALSE(m3.Matches(further_from_one_));
2228
2229 // Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above.
2230 EXPECT_FALSE(m3.Matches(0.0));
2231
2232 Matcher<RawType> m4 = matcher_maker(-infinity_);
2233 EXPECT_TRUE(m4.Matches(-close_to_infinity_));
2234
2235 Matcher<RawType> m5 = matcher_maker(infinity_);
2236 EXPECT_TRUE(m5.Matches(close_to_infinity_));
2237
2238 // This is interesting as the representations of infinity_ and nan1_
2239 // are only 1 DLP apart.
2240 EXPECT_FALSE(m5.Matches(nan1_));
2241
2242 // matcher_maker can produce a Matcher<const RawType&>, which is needed in
2243 // some cases.
2244 Matcher<const RawType&> m6 = matcher_maker(0.0);
2245 EXPECT_TRUE(m6.Matches(-0.0));
2246 EXPECT_TRUE(m6.Matches(close_to_positive_zero_));
2247 EXPECT_FALSE(m6.Matches(1.0));
2248
2249 // matcher_maker can produce a Matcher<RawType&>, which is needed in some
2250 // cases.
2251 Matcher<RawType&> m7 = matcher_maker(0.0);
2252 RawType x = 0.0;
2253 EXPECT_TRUE(m7.Matches(x));
2254 x = 0.01f;
2255 EXPECT_FALSE(m7.Matches(x));
2256 }
2257
2258 // Pre-calculated numbers to be used by the tests.
2259
2260 static RawType close_to_positive_zero_;
2261 static RawType close_to_negative_zero_;
2262 static RawType further_from_negative_zero_;
2263
2264 static RawType close_to_one_;
2265 static RawType further_from_one_;
2266
2267 static RawType infinity_;
2268 static RawType close_to_infinity_;
2269 static RawType further_from_infinity_;
2270
2271 static RawType nan1_;
2272 static RawType nan2_;
2273};
2274
2275template <typename RawType>
2276RawType FloatingPointTest<RawType>::close_to_positive_zero_;
2277
2278template <typename RawType>
2279RawType FloatingPointTest<RawType>::close_to_negative_zero_;
2280
2281template <typename RawType>
2282RawType FloatingPointTest<RawType>::further_from_negative_zero_;
2283
2284template <typename RawType>
2285RawType FloatingPointTest<RawType>::close_to_one_;
2286
2287template <typename RawType>
2288RawType FloatingPointTest<RawType>::further_from_one_;
2289
2290template <typename RawType>
2291RawType FloatingPointTest<RawType>::infinity_;
2292
2293template <typename RawType>
2294RawType FloatingPointTest<RawType>::close_to_infinity_;
2295
2296template <typename RawType>
2297RawType FloatingPointTest<RawType>::further_from_infinity_;
2298
2299template <typename RawType>
2300RawType FloatingPointTest<RawType>::nan1_;
2301
2302template <typename RawType>
2303RawType FloatingPointTest<RawType>::nan2_;
2304
2305// Instantiate FloatingPointTest for testing floats.
2306typedef FloatingPointTest<float> FloatTest;
2307
2308TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) {
2309 TestMatches(&FloatEq);
2310}
2311
2312TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
2313 TestMatches(&NanSensitiveFloatEq);
2314}
2315
2316TEST_F(FloatTest, FloatEqCannotMatchNaN) {
2317 // FloatEq never matches NaN.
2318 Matcher<float> m = FloatEq(nan1_);
2319 EXPECT_FALSE(m.Matches(nan1_));
2320 EXPECT_FALSE(m.Matches(nan2_));
2321 EXPECT_FALSE(m.Matches(1.0));
2322}
2323
2324TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {
2325 // NanSensitiveFloatEq will match NaN.
2326 Matcher<float> m = NanSensitiveFloatEq(nan1_);
2327 EXPECT_TRUE(m.Matches(nan1_));
2328 EXPECT_TRUE(m.Matches(nan2_));
2329 EXPECT_FALSE(m.Matches(1.0));
2330}
2331
2332TEST_F(FloatTest, FloatEqCanDescribeSelf) {
2333 Matcher<float> m1 = FloatEq(2.0f);
2334 EXPECT_EQ("is approximately 2", Describe(m1));
2335 EXPECT_EQ("is not approximately 2", DescribeNegation(m1));
2336
2337 Matcher<float> m2 = FloatEq(0.5f);
2338 EXPECT_EQ("is approximately 0.5", Describe(m2));
2339 EXPECT_EQ("is not approximately 0.5", DescribeNegation(m2));
2340
2341 Matcher<float> m3 = FloatEq(nan1_);
2342 EXPECT_EQ("never matches", Describe(m3));
2343 EXPECT_EQ("is anything", DescribeNegation(m3));
2344}
2345
2346TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {
2347 Matcher<float> m1 = NanSensitiveFloatEq(2.0f);
2348 EXPECT_EQ("is approximately 2", Describe(m1));
2349 EXPECT_EQ("is not approximately 2", DescribeNegation(m1));
2350
2351 Matcher<float> m2 = NanSensitiveFloatEq(0.5f);
2352 EXPECT_EQ("is approximately 0.5", Describe(m2));
2353 EXPECT_EQ("is not approximately 0.5", DescribeNegation(m2));
2354
2355 Matcher<float> m3 = NanSensitiveFloatEq(nan1_);
2356 EXPECT_EQ("is NaN", Describe(m3));
2357 EXPECT_EQ("is not NaN", DescribeNegation(m3));
2358}
2359
2360// Instantiate FloatingPointTest for testing doubles.
2361typedef FloatingPointTest<double> DoubleTest;
2362
2363TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
2364 TestMatches(&DoubleEq);
2365}
2366
2367TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
2368 TestMatches(&NanSensitiveDoubleEq);
2369}
2370
2371TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {
2372 // DoubleEq never matches NaN.
2373 Matcher<double> m = DoubleEq(nan1_);
2374 EXPECT_FALSE(m.Matches(nan1_));
2375 EXPECT_FALSE(m.Matches(nan2_));
2376 EXPECT_FALSE(m.Matches(1.0));
2377}
2378
2379TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {
2380 // NanSensitiveDoubleEq will match NaN.
2381 Matcher<double> m = NanSensitiveDoubleEq(nan1_);
2382 EXPECT_TRUE(m.Matches(nan1_));
2383 EXPECT_TRUE(m.Matches(nan2_));
2384 EXPECT_FALSE(m.Matches(1.0));
2385}
2386
2387TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {
2388 Matcher<double> m1 = DoubleEq(2.0);
2389 EXPECT_EQ("is approximately 2", Describe(m1));
2390 EXPECT_EQ("is not approximately 2", DescribeNegation(m1));
2391
2392 Matcher<double> m2 = DoubleEq(0.5);
2393 EXPECT_EQ("is approximately 0.5", Describe(m2));
2394 EXPECT_EQ("is not approximately 0.5", DescribeNegation(m2));
2395
2396 Matcher<double> m3 = DoubleEq(nan1_);
2397 EXPECT_EQ("never matches", Describe(m3));
2398 EXPECT_EQ("is anything", DescribeNegation(m3));
2399}
2400
2401TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {
2402 Matcher<double> m1 = NanSensitiveDoubleEq(2.0);
2403 EXPECT_EQ("is approximately 2", Describe(m1));
2404 EXPECT_EQ("is not approximately 2", DescribeNegation(m1));
2405
2406 Matcher<double> m2 = NanSensitiveDoubleEq(0.5);
2407 EXPECT_EQ("is approximately 0.5", Describe(m2));
2408 EXPECT_EQ("is not approximately 0.5", DescribeNegation(m2));
2409
2410 Matcher<double> m3 = NanSensitiveDoubleEq(nan1_);
2411 EXPECT_EQ("is NaN", Describe(m3));
2412 EXPECT_EQ("is not NaN", DescribeNegation(m3));
2413}
2414
2415TEST(PointeeTest, RawPointer) {
2416 const Matcher<int*> m = Pointee(Ge(0));
2417
2418 int n = 1;
2419 EXPECT_TRUE(m.Matches(&n));
2420 n = -1;
2421 EXPECT_FALSE(m.Matches(&n));
2422 EXPECT_FALSE(m.Matches(NULL));
2423}
2424
2425TEST(PointeeTest, RawPointerToConst) {
2426 const Matcher<const double*> m = Pointee(Ge(0));
2427
2428 double x = 1;
2429 EXPECT_TRUE(m.Matches(&x));
2430 x = -1;
2431 EXPECT_FALSE(m.Matches(&x));
2432 EXPECT_FALSE(m.Matches(NULL));
2433}
2434
2435TEST(PointeeTest, ReferenceToConstRawPointer) {
2436 const Matcher<int* const &> m = Pointee(Ge(0));
2437
2438 int n = 1;
2439 EXPECT_TRUE(m.Matches(&n));
2440 n = -1;
2441 EXPECT_FALSE(m.Matches(&n));
2442 EXPECT_FALSE(m.Matches(NULL));
2443}
2444
2445TEST(PointeeTest, ReferenceToNonConstRawPointer) {
2446 const Matcher<double* &> m = Pointee(Ge(0));
2447
2448 double x = 1.0;
2449 double* p = &x;
2450 EXPECT_TRUE(m.Matches(p));
2451 x = -1;
2452 EXPECT_FALSE(m.Matches(p));
2453 p = NULL;
2454 EXPECT_FALSE(m.Matches(p));
2455}
2456
2457TEST(PointeeTest, NeverMatchesNull) {
2458 const Matcher<const char*> m = Pointee(_);
2459 EXPECT_FALSE(m.Matches(NULL));
2460}
2461
2462// Tests that we can write Pointee(value) instead of Pointee(Eq(value)).
2463TEST(PointeeTest, MatchesAgainstAValue) {
2464 const Matcher<int*> m = Pointee(5);
2465
2466 int n = 5;
2467 EXPECT_TRUE(m.Matches(&n));
2468 n = -1;
2469 EXPECT_FALSE(m.Matches(&n));
2470 EXPECT_FALSE(m.Matches(NULL));
2471}
2472
2473TEST(PointeeTest, CanDescribeSelf) {
2474 const Matcher<int*> m = Pointee(Gt(3));
2475 EXPECT_EQ("points to a value that is greater than 3", Describe(m));
2476 EXPECT_EQ("does not point to a value that is greater than 3",
2477 DescribeNegation(m));
2478}
2479
shiqiane35fdd92008-12-10 05:08:54 +00002480TEST(PointeeTest, CanExplainMatchResult) {
2481 const Matcher<const string*> m = Pointee(StartsWith("Hi"));
2482
2483 EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL)));
2484
2485 const Matcher<int*> m2 = Pointee(GreaterThan(1));
2486 int n = 3;
2487 EXPECT_EQ("points to a value that is 2 more than 1", Explain(m2, &n));
2488}
2489
2490// An uncopyable class.
2491class Uncopyable {
2492 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002493 explicit Uncopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00002494
2495 int value() const { return value_; }
2496 private:
2497 const int value_;
2498 GTEST_DISALLOW_COPY_AND_ASSIGN_(Uncopyable);
2499};
2500
2501// Returns true iff x.value() is positive.
2502bool ValueIsPositive(const Uncopyable& x) { return x.value() > 0; }
2503
2504// A user-defined struct for testing Field().
2505struct AStruct {
2506 AStruct() : x(0), y(1.0), z(5), p(NULL) {}
2507 AStruct(const AStruct& rhs)
2508 : x(rhs.x), y(rhs.y), z(rhs.z.value()), p(rhs.p) {}
2509
2510 int x; // A non-const field.
2511 const double y; // A const field.
2512 Uncopyable z; // An uncopyable field.
2513 const char* p; // A pointer field.
zhanyong.wan32de5f52009-12-23 00:13:23 +00002514
2515 private:
2516 GTEST_DISALLOW_ASSIGN_(AStruct);
shiqiane35fdd92008-12-10 05:08:54 +00002517};
2518
2519// A derived struct for testing Field().
2520struct DerivedStruct : public AStruct {
2521 char ch;
zhanyong.wan32de5f52009-12-23 00:13:23 +00002522
2523 private:
2524 GTEST_DISALLOW_ASSIGN_(DerivedStruct);
shiqiane35fdd92008-12-10 05:08:54 +00002525};
2526
2527// Tests that Field(&Foo::field, ...) works when field is non-const.
2528TEST(FieldTest, WorksForNonConstField) {
2529 Matcher<AStruct> m = Field(&AStruct::x, Ge(0));
2530
2531 AStruct a;
2532 EXPECT_TRUE(m.Matches(a));
2533 a.x = -1;
2534 EXPECT_FALSE(m.Matches(a));
2535}
2536
2537// Tests that Field(&Foo::field, ...) works when field is const.
2538TEST(FieldTest, WorksForConstField) {
2539 AStruct a;
2540
2541 Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0));
2542 EXPECT_TRUE(m.Matches(a));
2543 m = Field(&AStruct::y, Le(0.0));
2544 EXPECT_FALSE(m.Matches(a));
2545}
2546
2547// Tests that Field(&Foo::field, ...) works when field is not copyable.
2548TEST(FieldTest, WorksForUncopyableField) {
2549 AStruct a;
2550
2551 Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive));
2552 EXPECT_TRUE(m.Matches(a));
2553 m = Field(&AStruct::z, Not(Truly(ValueIsPositive)));
2554 EXPECT_FALSE(m.Matches(a));
2555}
2556
2557// Tests that Field(&Foo::field, ...) works when field is a pointer.
2558TEST(FieldTest, WorksForPointerField) {
2559 // Matching against NULL.
2560 Matcher<AStruct> m = Field(&AStruct::p, static_cast<const char*>(NULL));
2561 AStruct a;
2562 EXPECT_TRUE(m.Matches(a));
2563 a.p = "hi";
2564 EXPECT_FALSE(m.Matches(a));
2565
2566 // Matching a pointer that is not NULL.
2567 m = Field(&AStruct::p, StartsWith("hi"));
2568 a.p = "hill";
2569 EXPECT_TRUE(m.Matches(a));
2570 a.p = "hole";
2571 EXPECT_FALSE(m.Matches(a));
2572}
2573
2574// Tests that Field() works when the object is passed by reference.
2575TEST(FieldTest, WorksForByRefArgument) {
2576 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2577
2578 AStruct a;
2579 EXPECT_TRUE(m.Matches(a));
2580 a.x = -1;
2581 EXPECT_FALSE(m.Matches(a));
2582}
2583
2584// Tests that Field(&Foo::field, ...) works when the argument's type
2585// is a sub-type of Foo.
2586TEST(FieldTest, WorksForArgumentOfSubType) {
2587 // Note that the matcher expects DerivedStruct but we say AStruct
2588 // inside Field().
2589 Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0));
2590
2591 DerivedStruct d;
2592 EXPECT_TRUE(m.Matches(d));
2593 d.x = -1;
2594 EXPECT_FALSE(m.Matches(d));
2595}
2596
2597// Tests that Field(&Foo::field, m) works when field's type and m's
2598// argument type are compatible but not the same.
2599TEST(FieldTest, WorksForCompatibleMatcherType) {
2600 // The field is an int, but the inner matcher expects a signed char.
2601 Matcher<const AStruct&> m = Field(&AStruct::x,
2602 Matcher<signed char>(Ge(0)));
2603
2604 AStruct a;
2605 EXPECT_TRUE(m.Matches(a));
2606 a.x = -1;
2607 EXPECT_FALSE(m.Matches(a));
2608}
2609
2610// Tests that Field() can describe itself.
2611TEST(FieldTest, CanDescribeSelf) {
2612 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2613
2614 EXPECT_EQ("the given field is greater than or equal to 0", Describe(m));
2615 EXPECT_EQ("the given field is not greater than or equal to 0",
2616 DescribeNegation(m));
2617}
2618
2619// Tests that Field() can explain the match result.
2620TEST(FieldTest, CanExplainMatchResult) {
2621 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2622
2623 AStruct a;
2624 a.x = 1;
2625 EXPECT_EQ("", Explain(m, a));
2626
2627 m = Field(&AStruct::x, GreaterThan(0));
2628 EXPECT_EQ("the given field is 1 more than 0", Explain(m, a));
2629}
2630
2631// Tests that Field() works when the argument is a pointer to const.
2632TEST(FieldForPointerTest, WorksForPointerToConst) {
2633 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2634
2635 AStruct a;
2636 EXPECT_TRUE(m.Matches(&a));
2637 a.x = -1;
2638 EXPECT_FALSE(m.Matches(&a));
2639}
2640
2641// Tests that Field() works when the argument is a pointer to non-const.
2642TEST(FieldForPointerTest, WorksForPointerToNonConst) {
2643 Matcher<AStruct*> m = Field(&AStruct::x, Ge(0));
2644
2645 AStruct a;
2646 EXPECT_TRUE(m.Matches(&a));
2647 a.x = -1;
2648 EXPECT_FALSE(m.Matches(&a));
2649}
2650
2651// Tests that Field() does not match the NULL pointer.
2652TEST(FieldForPointerTest, DoesNotMatchNull) {
2653 Matcher<const AStruct*> m = Field(&AStruct::x, _);
2654 EXPECT_FALSE(m.Matches(NULL));
2655}
2656
2657// Tests that Field(&Foo::field, ...) works when the argument's type
2658// is a sub-type of const Foo*.
2659TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
2660 // Note that the matcher expects DerivedStruct but we say AStruct
2661 // inside Field().
2662 Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0));
2663
2664 DerivedStruct d;
2665 EXPECT_TRUE(m.Matches(&d));
2666 d.x = -1;
2667 EXPECT_FALSE(m.Matches(&d));
2668}
2669
2670// Tests that Field() can describe itself when used to match a pointer.
2671TEST(FieldForPointerTest, CanDescribeSelf) {
2672 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2673
2674 EXPECT_EQ("the given field is greater than or equal to 0", Describe(m));
2675 EXPECT_EQ("the given field is not greater than or equal to 0",
2676 DescribeNegation(m));
2677}
2678
2679// Tests that Field() can explain the result of matching a pointer.
2680TEST(FieldForPointerTest, CanExplainMatchResult) {
2681 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2682
2683 AStruct a;
2684 a.x = 1;
2685 EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(NULL)));
2686 EXPECT_EQ("", Explain(m, &a));
2687
2688 m = Field(&AStruct::x, GreaterThan(0));
2689 EXPECT_EQ("the given field is 1 more than 0", Explain(m, &a));
2690}
2691
2692// A user-defined class for testing Property().
2693class AClass {
2694 public:
2695 AClass() : n_(0) {}
2696
2697 // A getter that returns a non-reference.
2698 int n() const { return n_; }
2699
2700 void set_n(int new_n) { n_ = new_n; }
2701
2702 // A getter that returns a reference to const.
2703 const string& s() const { return s_; }
2704
2705 void set_s(const string& new_s) { s_ = new_s; }
2706
2707 // A getter that returns a reference to non-const.
2708 double& x() const { return x_; }
2709 private:
2710 int n_;
2711 string s_;
2712
2713 static double x_;
2714};
2715
2716double AClass::x_ = 0.0;
2717
2718// A derived class for testing Property().
2719class DerivedClass : public AClass {
2720 private:
2721 int k_;
2722};
2723
2724// Tests that Property(&Foo::property, ...) works when property()
2725// returns a non-reference.
2726TEST(PropertyTest, WorksForNonReferenceProperty) {
2727 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
2728
2729 AClass a;
2730 a.set_n(1);
2731 EXPECT_TRUE(m.Matches(a));
2732
2733 a.set_n(-1);
2734 EXPECT_FALSE(m.Matches(a));
2735}
2736
2737// Tests that Property(&Foo::property, ...) works when property()
2738// returns a reference to const.
2739TEST(PropertyTest, WorksForReferenceToConstProperty) {
2740 Matcher<const AClass&> m = Property(&AClass::s, StartsWith("hi"));
2741
2742 AClass a;
2743 a.set_s("hill");
2744 EXPECT_TRUE(m.Matches(a));
2745
2746 a.set_s("hole");
2747 EXPECT_FALSE(m.Matches(a));
2748}
2749
2750// Tests that Property(&Foo::property, ...) works when property()
2751// returns a reference to non-const.
2752TEST(PropertyTest, WorksForReferenceToNonConstProperty) {
2753 double x = 0.0;
2754 AClass a;
2755
2756 Matcher<const AClass&> m = Property(&AClass::x, Ref(x));
2757 EXPECT_FALSE(m.Matches(a));
2758
2759 m = Property(&AClass::x, Not(Ref(x)));
2760 EXPECT_TRUE(m.Matches(a));
2761}
2762
2763// Tests that Property(&Foo::property, ...) works when the argument is
2764// passed by value.
2765TEST(PropertyTest, WorksForByValueArgument) {
2766 Matcher<AClass> m = Property(&AClass::s, StartsWith("hi"));
2767
2768 AClass a;
2769 a.set_s("hill");
2770 EXPECT_TRUE(m.Matches(a));
2771
2772 a.set_s("hole");
2773 EXPECT_FALSE(m.Matches(a));
2774}
2775
2776// Tests that Property(&Foo::property, ...) works when the argument's
2777// type is a sub-type of Foo.
2778TEST(PropertyTest, WorksForArgumentOfSubType) {
2779 // The matcher expects a DerivedClass, but inside the Property() we
2780 // say AClass.
2781 Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0));
2782
2783 DerivedClass d;
2784 d.set_n(1);
2785 EXPECT_TRUE(m.Matches(d));
2786
2787 d.set_n(-1);
2788 EXPECT_FALSE(m.Matches(d));
2789}
2790
2791// Tests that Property(&Foo::property, m) works when property()'s type
2792// and m's argument type are compatible but different.
2793TEST(PropertyTest, WorksForCompatibleMatcherType) {
2794 // n() returns an int but the inner matcher expects a signed char.
2795 Matcher<const AClass&> m = Property(&AClass::n,
2796 Matcher<signed char>(Ge(0)));
2797
2798 AClass a;
2799 EXPECT_TRUE(m.Matches(a));
2800 a.set_n(-1);
2801 EXPECT_FALSE(m.Matches(a));
2802}
2803
2804// Tests that Property() can describe itself.
2805TEST(PropertyTest, CanDescribeSelf) {
2806 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
2807
2808 EXPECT_EQ("the given property is greater than or equal to 0", Describe(m));
2809 EXPECT_EQ("the given property is not greater than or equal to 0",
2810 DescribeNegation(m));
2811}
2812
2813// Tests that Property() can explain the match result.
2814TEST(PropertyTest, CanExplainMatchResult) {
2815 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
2816
2817 AClass a;
2818 a.set_n(1);
2819 EXPECT_EQ("", Explain(m, a));
2820
2821 m = Property(&AClass::n, GreaterThan(0));
2822 EXPECT_EQ("the given property is 1 more than 0", Explain(m, a));
2823}
2824
2825// Tests that Property() works when the argument is a pointer to const.
2826TEST(PropertyForPointerTest, WorksForPointerToConst) {
2827 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
2828
2829 AClass a;
2830 a.set_n(1);
2831 EXPECT_TRUE(m.Matches(&a));
2832
2833 a.set_n(-1);
2834 EXPECT_FALSE(m.Matches(&a));
2835}
2836
2837// Tests that Property() works when the argument is a pointer to non-const.
2838TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
2839 Matcher<AClass*> m = Property(&AClass::s, StartsWith("hi"));
2840
2841 AClass a;
2842 a.set_s("hill");
2843 EXPECT_TRUE(m.Matches(&a));
2844
2845 a.set_s("hole");
2846 EXPECT_FALSE(m.Matches(&a));
2847}
2848
2849// Tests that Property() does not match the NULL pointer.
2850TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
2851 Matcher<const AClass*> m = Property(&AClass::x, _);
2852 EXPECT_FALSE(m.Matches(NULL));
2853}
2854
2855// Tests that Property(&Foo::property, ...) works when the argument's
2856// type is a sub-type of const Foo*.
2857TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
2858 // The matcher expects a DerivedClass, but inside the Property() we
2859 // say AClass.
2860 Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0));
2861
2862 DerivedClass d;
2863 d.set_n(1);
2864 EXPECT_TRUE(m.Matches(&d));
2865
2866 d.set_n(-1);
2867 EXPECT_FALSE(m.Matches(&d));
2868}
2869
2870// Tests that Property() can describe itself when used to match a pointer.
2871TEST(PropertyForPointerTest, CanDescribeSelf) {
2872 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
2873
2874 EXPECT_EQ("the given property is greater than or equal to 0", Describe(m));
2875 EXPECT_EQ("the given property is not greater than or equal to 0",
2876 DescribeNegation(m));
2877}
2878
2879// Tests that Property() can explain the result of matching a pointer.
2880TEST(PropertyForPointerTest, CanExplainMatchResult) {
2881 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
2882
2883 AClass a;
2884 a.set_n(1);
2885 EXPECT_EQ("", Explain(m, static_cast<const AClass*>(NULL)));
2886 EXPECT_EQ("", Explain(m, &a));
2887
2888 m = Property(&AClass::n, GreaterThan(0));
2889 EXPECT_EQ("the given property is 1 more than 0", Explain(m, &a));
2890}
2891
2892// Tests ResultOf.
2893
2894// Tests that ResultOf(f, ...) compiles and works as expected when f is a
2895// function pointer.
2896string IntToStringFunction(int input) { return input == 1 ? "foo" : "bar"; }
2897
2898TEST(ResultOfTest, WorksForFunctionPointers) {
2899 Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(string("foo")));
2900
2901 EXPECT_TRUE(matcher.Matches(1));
2902 EXPECT_FALSE(matcher.Matches(2));
2903}
2904
2905// Tests that ResultOf() can describe itself.
2906TEST(ResultOfTest, CanDescribeItself) {
2907 Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo"));
2908
2909 EXPECT_EQ("result of the given callable is equal to \"foo\"",
2910 Describe(matcher));
2911 EXPECT_EQ("result of the given callable is not equal to \"foo\"",
2912 DescribeNegation(matcher));
2913}
2914
2915// Tests that ResultOf() can explain the match result.
2916int IntFunction(int input) { return input == 42 ? 80 : 90; }
2917
2918TEST(ResultOfTest, CanExplainMatchResult) {
2919 Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
2920 EXPECT_EQ("", Explain(matcher, 36));
2921
2922 matcher = ResultOf(&IntFunction, GreaterThan(85));
2923 EXPECT_EQ("result of the given callable is 5 more than 85",
2924 Explain(matcher, 36));
2925}
2926
2927// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
2928// returns a non-reference.
2929TEST(ResultOfTest, WorksForNonReferenceResults) {
2930 Matcher<int> matcher = ResultOf(&IntFunction, Eq(80));
2931
2932 EXPECT_TRUE(matcher.Matches(42));
2933 EXPECT_FALSE(matcher.Matches(36));
2934}
2935
2936// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
2937// returns a reference to non-const.
2938double& DoubleFunction(double& input) { return input; }
2939
2940Uncopyable& RefUncopyableFunction(Uncopyable& obj) {
2941 return obj;
2942}
2943
2944TEST(ResultOfTest, WorksForReferenceToNonConstResults) {
2945 double x = 3.14;
2946 double x2 = x;
2947 Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x));
2948
2949 EXPECT_TRUE(matcher.Matches(x));
2950 EXPECT_FALSE(matcher.Matches(x2));
2951
2952 // Test that ResultOf works with uncopyable objects
2953 Uncopyable obj(0);
2954 Uncopyable obj2(0);
2955 Matcher<Uncopyable&> matcher2 =
2956 ResultOf(&RefUncopyableFunction, Ref(obj));
2957
2958 EXPECT_TRUE(matcher2.Matches(obj));
2959 EXPECT_FALSE(matcher2.Matches(obj2));
2960}
2961
2962// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
2963// returns a reference to const.
2964const string& StringFunction(const string& input) { return input; }
2965
2966TEST(ResultOfTest, WorksForReferenceToConstResults) {
2967 string s = "foo";
2968 string s2 = s;
2969 Matcher<const string&> matcher = ResultOf(&StringFunction, Ref(s));
2970
2971 EXPECT_TRUE(matcher.Matches(s));
2972 EXPECT_FALSE(matcher.Matches(s2));
2973}
2974
2975// Tests that ResultOf(f, m) works when f(x) and m's
2976// argument types are compatible but different.
2977TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {
2978 // IntFunction() returns int but the inner matcher expects a signed char.
2979 Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85)));
2980
2981 EXPECT_TRUE(matcher.Matches(36));
2982 EXPECT_FALSE(matcher.Matches(42));
2983}
2984
shiqiane35fdd92008-12-10 05:08:54 +00002985// Tests that the program aborts when ResultOf is passed
2986// a NULL function pointer.
2987TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +00002988 EXPECT_DEATH_IF_SUPPORTED(
shiqiane35fdd92008-12-10 05:08:54 +00002989 ResultOf(static_cast<string(*)(int)>(NULL), Eq(string("foo"))),
2990 "NULL function pointer is passed into ResultOf\\(\\)\\.");
2991}
shiqiane35fdd92008-12-10 05:08:54 +00002992
2993// Tests that ResultOf(f, ...) compiles and works as expected when f is a
2994// function reference.
2995TEST(ResultOfTest, WorksForFunctionReferences) {
2996 Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq("foo"));
2997 EXPECT_TRUE(matcher.Matches(1));
2998 EXPECT_FALSE(matcher.Matches(2));
2999}
3000
3001// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3002// function object.
3003struct Functor : public ::std::unary_function<int, string> {
3004 result_type operator()(argument_type input) const {
3005 return IntToStringFunction(input);
3006 }
3007};
3008
3009TEST(ResultOfTest, WorksForFunctors) {
3010 Matcher<int> matcher = ResultOf(Functor(), Eq(string("foo")));
3011
3012 EXPECT_TRUE(matcher.Matches(1));
3013 EXPECT_FALSE(matcher.Matches(2));
3014}
3015
3016// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3017// functor with more then one operator() defined. ResultOf() must work
3018// for each defined operator().
3019struct PolymorphicFunctor {
3020 typedef int result_type;
3021 int operator()(int n) { return n; }
3022 int operator()(const char* s) { return static_cast<int>(strlen(s)); }
3023};
3024
3025TEST(ResultOfTest, WorksForPolymorphicFunctors) {
3026 Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5));
3027
3028 EXPECT_TRUE(matcher_int.Matches(10));
3029 EXPECT_FALSE(matcher_int.Matches(2));
3030
3031 Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5));
3032
3033 EXPECT_TRUE(matcher_string.Matches("long string"));
3034 EXPECT_FALSE(matcher_string.Matches("shrt"));
3035}
3036
3037const int* ReferencingFunction(const int& n) { return &n; }
3038
3039struct ReferencingFunctor {
3040 typedef const int* result_type;
3041 result_type operator()(const int& n) { return &n; }
3042};
3043
3044TEST(ResultOfTest, WorksForReferencingCallables) {
3045 const int n = 1;
3046 const int n2 = 1;
3047 Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n));
3048 EXPECT_TRUE(matcher2.Matches(n));
3049 EXPECT_FALSE(matcher2.Matches(n2));
3050
3051 Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n));
3052 EXPECT_TRUE(matcher3.Matches(n));
3053 EXPECT_FALSE(matcher3.Matches(n2));
3054}
3055
shiqiane35fdd92008-12-10 05:08:54 +00003056class DivisibleByImpl {
3057 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003058 explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {}
shiqiane35fdd92008-12-10 05:08:54 +00003059
3060 template <typename T>
3061 bool Matches(const T& n) const {
3062 return (n % divider_) == 0;
3063 }
3064
3065 void DescribeTo(::std::ostream* os) const {
3066 *os << "is divisible by " << divider_;
3067 }
3068
3069 void DescribeNegationTo(::std::ostream* os) const {
3070 *os << "is not divisible by " << divider_;
3071 }
3072
zhanyong.wan32de5f52009-12-23 00:13:23 +00003073 void set_divider(int a_divider) { divider_ = a_divider; }
shiqiane35fdd92008-12-10 05:08:54 +00003074 int divider() const { return divider_; }
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003075
shiqiane35fdd92008-12-10 05:08:54 +00003076 private:
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003077 int divider_;
shiqiane35fdd92008-12-10 05:08:54 +00003078};
3079
3080// For testing using ExplainMatchResultTo() with polymorphic matchers.
3081template <typename T>
3082void ExplainMatchResultTo(const DivisibleByImpl& impl, const T& n,
3083 ::std::ostream* os) {
3084 *os << "is " << (n % impl.divider()) << " modulo "
3085 << impl.divider();
3086}
3087
3088PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) {
3089 return MakePolymorphicMatcher(DivisibleByImpl(n));
3090}
3091
3092// Tests that when AllOf() fails, only the first failing matcher is
3093// asked to explain why.
3094TEST(ExplainMatchResultTest, AllOf_False_False) {
3095 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
3096 EXPECT_EQ("is 1 modulo 4", Explain(m, 5));
3097}
3098
3099// Tests that when AllOf() fails, only the first failing matcher is
3100// asked to explain why.
3101TEST(ExplainMatchResultTest, AllOf_False_True) {
3102 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
3103 EXPECT_EQ("is 2 modulo 4", Explain(m, 6));
3104}
3105
3106// Tests that when AllOf() fails, only the first failing matcher is
3107// asked to explain why.
3108TEST(ExplainMatchResultTest, AllOf_True_False) {
3109 const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));
3110 EXPECT_EQ("is 2 modulo 3", Explain(m, 5));
3111}
3112
3113// Tests that when AllOf() succeeds, all matchers are asked to explain
3114// why.
3115TEST(ExplainMatchResultTest, AllOf_True_True) {
3116 const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3));
3117 EXPECT_EQ("is 0 modulo 2; is 0 modulo 3", Explain(m, 6));
3118}
3119
3120TEST(ExplainMatchResultTest, AllOf_True_True_2) {
3121 const Matcher<int> m = AllOf(Ge(2), Le(3));
3122 EXPECT_EQ("", Explain(m, 2));
3123}
3124
3125TEST(ExplainmatcherResultTest, MonomorphicMatcher) {
3126 const Matcher<int> m = GreaterThan(5);
3127 EXPECT_EQ("is 1 more than 5", Explain(m, 6));
3128}
3129
3130// The following two tests verify that values without a public copy
3131// ctor can be used as arguments to matchers like Eq(), Ge(), and etc
3132// with the help of ByRef().
3133
3134class NotCopyable {
3135 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003136 explicit NotCopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00003137
3138 int value() const { return value_; }
3139
3140 bool operator==(const NotCopyable& rhs) const {
3141 return value() == rhs.value();
3142 }
3143
3144 bool operator>=(const NotCopyable& rhs) const {
3145 return value() >= rhs.value();
3146 }
3147 private:
3148 int value_;
3149
3150 GTEST_DISALLOW_COPY_AND_ASSIGN_(NotCopyable);
3151};
3152
3153TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) {
3154 const NotCopyable const_value1(1);
3155 const Matcher<const NotCopyable&> m = Eq(ByRef(const_value1));
3156
3157 const NotCopyable n1(1), n2(2);
3158 EXPECT_TRUE(m.Matches(n1));
3159 EXPECT_FALSE(m.Matches(n2));
3160}
3161
3162TEST(ByRefTest, AllowsNotCopyableValueInMatchers) {
3163 NotCopyable value2(2);
3164 const Matcher<NotCopyable&> m = Ge(ByRef(value2));
3165
3166 NotCopyable n1(1), n2(2);
3167 EXPECT_FALSE(m.Matches(n1));
3168 EXPECT_TRUE(m.Matches(n2));
3169}
3170
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003171#if GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003172// Tests ContainerEq with different container types, and
3173// different element types.
3174
3175template <typename T>
zhanyong.wanb8243162009-06-04 05:48:20 +00003176class ContainerEqTest : public testing::Test {};
zhanyong.wan6a896b52009-01-16 01:13:50 +00003177
3178typedef testing::Types<
3179 std::set<int>,
3180 std::vector<size_t>,
3181 std::multiset<size_t>,
3182 std::list<int> >
3183 ContainerEqTestTypes;
3184
3185TYPED_TEST_CASE(ContainerEqTest, ContainerEqTestTypes);
3186
3187// Tests that the filled container is equal to itself.
3188TYPED_TEST(ContainerEqTest, EqualsSelf) {
3189 static const int vals[] = {1, 1, 2, 3, 5, 8};
3190 TypeParam my_set(vals, vals + 6);
3191 const Matcher<TypeParam> m = ContainerEq(my_set);
3192 EXPECT_TRUE(m.Matches(my_set));
3193 EXPECT_EQ("", Explain(m, my_set));
3194}
3195
3196// Tests that missing values are reported.
3197TYPED_TEST(ContainerEqTest, ValueMissing) {
3198 static const int vals[] = {1, 1, 2, 3, 5, 8};
3199 static const int test_vals[] = {2, 1, 8, 5};
3200 TypeParam my_set(vals, vals + 6);
3201 TypeParam test_set(test_vals, test_vals + 4);
3202 const Matcher<TypeParam> m = ContainerEq(my_set);
3203 EXPECT_FALSE(m.Matches(test_set));
3204 EXPECT_EQ("Not in actual: 3", Explain(m, test_set));
3205}
3206
3207// Tests that added values are reported.
3208TYPED_TEST(ContainerEqTest, ValueAdded) {
3209 static const int vals[] = {1, 1, 2, 3, 5, 8};
3210 static const int test_vals[] = {1, 2, 3, 5, 8, 46};
3211 TypeParam my_set(vals, vals + 6);
3212 TypeParam test_set(test_vals, test_vals + 6);
3213 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3214 EXPECT_FALSE(m.Matches(test_set));
3215 EXPECT_EQ("Only in actual: 46", Explain(m, test_set));
3216}
3217
3218// Tests that added and missing values are reported together.
3219TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) {
3220 static const int vals[] = {1, 1, 2, 3, 5, 8};
3221 static const int test_vals[] = {1, 2, 3, 8, 46};
3222 TypeParam my_set(vals, vals + 6);
3223 TypeParam test_set(test_vals, test_vals + 5);
3224 const Matcher<TypeParam> m = ContainerEq(my_set);
3225 EXPECT_FALSE(m.Matches(test_set));
3226 EXPECT_EQ("Only in actual: 46; not in actual: 5", Explain(m, test_set));
3227}
3228
3229// Tests duplicated value -- expect no explanation.
3230TYPED_TEST(ContainerEqTest, DuplicateDifference) {
3231 static const int vals[] = {1, 1, 2, 3, 5, 8};
3232 static const int test_vals[] = {1, 2, 3, 5, 8};
3233 TypeParam my_set(vals, vals + 6);
3234 TypeParam test_set(test_vals, test_vals + 5);
3235 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3236 // Depending on the container, match may be true or false
3237 // But in any case there should be no explanation.
3238 EXPECT_EQ("", Explain(m, test_set));
3239}
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003240#endif // GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003241
3242// Tests that mutliple missing values are reported.
3243// Using just vector here, so order is predicatble.
3244TEST(ContainerEqExtraTest, MultipleValuesMissing) {
3245 static const int vals[] = {1, 1, 2, 3, 5, 8};
3246 static const int test_vals[] = {2, 1, 5};
3247 std::vector<int> my_set(vals, vals + 6);
3248 std::vector<int> test_set(test_vals, test_vals + 3);
3249 const Matcher<std::vector<int> > m = ContainerEq(my_set);
3250 EXPECT_FALSE(m.Matches(test_set));
3251 EXPECT_EQ("Not in actual: 3, 8", Explain(m, test_set));
3252}
3253
3254// Tests that added values are reported.
3255// Using just vector here, so order is predicatble.
3256TEST(ContainerEqExtraTest, MultipleValuesAdded) {
3257 static const int vals[] = {1, 1, 2, 3, 5, 8};
3258 static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46};
3259 std::list<size_t> my_set(vals, vals + 6);
3260 std::list<size_t> test_set(test_vals, test_vals + 7);
3261 const Matcher<const std::list<size_t>&> m = ContainerEq(my_set);
3262 EXPECT_FALSE(m.Matches(test_set));
3263 EXPECT_EQ("Only in actual: 92, 46", Explain(m, test_set));
3264}
3265
3266// Tests that added and missing values are reported together.
3267TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) {
3268 static const int vals[] = {1, 1, 2, 3, 5, 8};
3269 static const int test_vals[] = {1, 2, 3, 92, 46};
3270 std::list<size_t> my_set(vals, vals + 6);
3271 std::list<size_t> test_set(test_vals, test_vals + 5);
3272 const Matcher<const std::list<size_t> > m = ContainerEq(my_set);
3273 EXPECT_FALSE(m.Matches(test_set));
3274 EXPECT_EQ("Only in actual: 92, 46; not in actual: 5, 8",
3275 Explain(m, test_set));
3276}
3277
3278// Tests to see that duplicate elements are detected,
3279// but (as above) not reported in the explanation.
3280TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) {
3281 static const int vals[] = {1, 1, 2, 3, 5, 8};
3282 static const int test_vals[] = {1, 2, 3, 5, 8};
3283 std::vector<int> my_set(vals, vals + 6);
3284 std::vector<int> test_set(test_vals, test_vals + 5);
3285 const Matcher<std::vector<int> > m = ContainerEq(my_set);
3286 EXPECT_TRUE(m.Matches(my_set));
3287 EXPECT_FALSE(m.Matches(test_set));
3288 // There is nothing to report when both sets contain all the same values.
3289 EXPECT_EQ("", Explain(m, test_set));
3290}
3291
3292// Tests that ContainerEq works for non-trivial associative containers,
3293// like maps.
3294TEST(ContainerEqExtraTest, WorksForMaps) {
3295 std::map<int, std::string> my_map;
3296 my_map[0] = "a";
3297 my_map[1] = "b";
3298
3299 std::map<int, std::string> test_map;
3300 test_map[0] = "aa";
3301 test_map[1] = "b";
3302
3303 const Matcher<const std::map<int, std::string>&> m = ContainerEq(my_map);
3304 EXPECT_TRUE(m.Matches(my_map));
3305 EXPECT_FALSE(m.Matches(test_map));
3306
3307 EXPECT_EQ("Only in actual: (0, \"aa\"); not in actual: (0, \"a\")",
3308 Explain(m, test_map));
3309}
3310
zhanyong.wanb8243162009-06-04 05:48:20 +00003311TEST(ContainerEqExtraTest, WorksForNativeArray) {
3312 int a1[] = { 1, 2, 3 };
3313 int a2[] = { 1, 2, 3 };
3314 int b[] = { 1, 2, 4 };
3315
3316 EXPECT_THAT(a1, ContainerEq(a2));
3317 EXPECT_THAT(a1, Not(ContainerEq(b)));
3318}
3319
3320TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) {
3321 const char a1[][3] = { "hi", "lo" };
3322 const char a2[][3] = { "hi", "lo" };
3323 const char b[][3] = { "lo", "hi" };
3324
3325 // Tests using ContainerEq() in the first dimension.
3326 EXPECT_THAT(a1, ContainerEq(a2));
3327 EXPECT_THAT(a1, Not(ContainerEq(b)));
3328
3329 // Tests using ContainerEq() in the second dimension.
3330 EXPECT_THAT(a1, ElementsAre(ContainerEq(a2[0]), ContainerEq(a2[1])));
3331 EXPECT_THAT(a1, ElementsAre(Not(ContainerEq(b[0])), ContainerEq(a2[1])));
3332}
3333
3334TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) {
3335 const int a1[] = { 1, 2, 3 };
3336 const int a2[] = { 1, 2, 3 };
3337 const int b[] = { 1, 2, 3, 4 };
3338
zhanyong.wan2661c682009-06-09 05:42:12 +00003339 const int* const p1 = a1;
3340 EXPECT_THAT(make_tuple(p1, 3), ContainerEq(a2));
3341 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(b)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003342
3343 const int c[] = { 1, 3, 2 };
zhanyong.wan2661c682009-06-09 05:42:12 +00003344 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(c)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003345}
3346
3347TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) {
3348 std::string a1[][3] = {
3349 { "hi", "hello", "ciao" },
3350 { "bye", "see you", "ciao" }
3351 };
3352
3353 std::string a2[][3] = {
3354 { "hi", "hello", "ciao" },
3355 { "bye", "see you", "ciao" }
3356 };
3357
3358 const Matcher<const std::string(&)[2][3]> m = ContainerEq(a2);
3359 EXPECT_THAT(a1, m);
3360
3361 a2[0][0] = "ha";
3362 EXPECT_THAT(a1, m);
3363}
3364
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003365// Tests GetParamIndex().
3366
3367TEST(GetParamIndexTest, WorksForEmptyParamList) {
3368 const char* params[] = { NULL };
3369 EXPECT_EQ(kTupleInterpolation, GetParamIndex(params, "*"));
3370 EXPECT_EQ(kInvalidInterpolation, GetParamIndex(params, "a"));
3371}
3372
3373TEST(GetParamIndexTest, RecognizesStar) {
3374 const char* params[] = { "a", "b", NULL };
3375 EXPECT_EQ(kTupleInterpolation, GetParamIndex(params, "*"));
3376}
3377
3378TEST(GetParamIndexTest, RecognizesKnownParam) {
3379 const char* params[] = { "foo", "bar", NULL };
3380 EXPECT_EQ(0, GetParamIndex(params, "foo"));
3381 EXPECT_EQ(1, GetParamIndex(params, "bar"));
3382}
3383
3384TEST(GetParamIndexTest, RejectsUnknownParam) {
3385 const char* params[] = { "foo", "bar", NULL };
3386 EXPECT_EQ(kInvalidInterpolation, GetParamIndex(params, "foobar"));
3387}
3388
3389// Tests SkipPrefix().
3390
3391TEST(SkipPrefixTest, SkipsWhenPrefixMatches) {
3392 const char* const str = "hello";
3393
3394 const char* p = str;
3395 EXPECT_TRUE(SkipPrefix("", &p));
3396 EXPECT_EQ(str, p);
3397
3398 p = str;
3399 EXPECT_TRUE(SkipPrefix("hell", &p));
3400 EXPECT_EQ(str + 4, p);
3401}
3402
3403TEST(SkipPrefixTest, DoesNotSkipWhenPrefixDoesNotMatch) {
3404 const char* const str = "world";
3405
3406 const char* p = str;
3407 EXPECT_FALSE(SkipPrefix("W", &p));
3408 EXPECT_EQ(str, p);
3409
3410 p = str;
3411 EXPECT_FALSE(SkipPrefix("world!", &p));
3412 EXPECT_EQ(str, p);
3413}
3414
3415// Tests FormatMatcherDescriptionSyntaxError().
3416TEST(FormatMatcherDescriptionSyntaxErrorTest, FormatsCorrectly) {
3417 const char* const description = "hello%world";
3418 EXPECT_EQ("Syntax error at index 5 in matcher description \"hello%world\": ",
3419 FormatMatcherDescriptionSyntaxError(description, description + 5));
3420}
3421
3422// Tests ValidateMatcherDescription().
3423
3424TEST(ValidateMatcherDescriptionTest, AcceptsEmptyDescription) {
3425 const char* params[] = { "foo", "bar", NULL };
3426 EXPECT_THAT(ValidateMatcherDescription(params, ""),
3427 ElementsAre());
3428}
3429
3430TEST(ValidateMatcherDescriptionTest,
3431 AcceptsNonEmptyDescriptionWithNoInterpolation) {
3432 const char* params[] = { "foo", "bar", NULL };
3433 EXPECT_THAT(ValidateMatcherDescription(params, "a simple description"),
3434 ElementsAre());
3435}
3436
zhanyong.wan82113312010-01-08 21:55:40 +00003437// The MATCHER*() macros trigger warning C4100 (unreferenced formal
3438// parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
3439// the macro definition, as the warnings are generated when the macro
3440// is expanded and macro expansion cannot contain #pragma. Therefore
3441// we suppress them here.
3442#ifdef _MSC_VER
3443#pragma warning(push)
3444#pragma warning(disable:4100)
3445#endif
3446
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003447// We use MATCHER_P3() to define a matcher for testing
3448// ValidateMatcherDescription(); otherwise we'll end up with much
3449// plumbing code. This is not circular as
3450// ValidateMatcherDescription() doesn't affect whether the matcher
3451// matches a value or not.
3452MATCHER_P3(EqInterpolation, start, end, index, "equals Interpolation%(*)s") {
3453 return arg.start_pos == start && arg.end_pos == end &&
3454 arg.param_index == index;
3455}
3456
zhanyong.wan82113312010-01-08 21:55:40 +00003457#ifdef _MSC_VER
3458#pragma warning(pop)
3459#endif
3460
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003461TEST(ValidateMatcherDescriptionTest, AcceptsPercentInterpolation) {
3462 const char* params[] = { "foo", NULL };
3463 const char* const desc = "one %%";
3464 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3465 ElementsAre(EqInterpolation(desc + 4, desc + 6,
3466 kPercentInterpolation)));
3467}
3468
3469TEST(ValidateMatcherDescriptionTest, AcceptsTupleInterpolation) {
3470 const char* params[] = { "foo", "bar", "baz", NULL };
3471 const char* const desc = "%(*)s after";
3472 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3473 ElementsAre(EqInterpolation(desc, desc + 5,
3474 kTupleInterpolation)));
3475}
3476
3477TEST(ValidateMatcherDescriptionTest, AcceptsParamInterpolation) {
3478 const char* params[] = { "foo", "bar", "baz", NULL };
3479 const char* const desc = "a %(bar)s.";
3480 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3481 ElementsAre(EqInterpolation(desc + 2, desc + 9, 1)));
3482}
3483
3484TEST(ValidateMatcherDescriptionTest, AcceptsMultiplenterpolations) {
3485 const char* params[] = { "foo", "bar", "baz", NULL };
3486 const char* const desc = "%(baz)s %(foo)s %(bar)s";
3487 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3488 ElementsAre(EqInterpolation(desc, desc + 7, 2),
3489 EqInterpolation(desc + 8, desc + 15, 0),
3490 EqInterpolation(desc + 16, desc + 23, 1)));
3491}
3492
3493TEST(ValidateMatcherDescriptionTest, AcceptsRepeatedParams) {
3494 const char* params[] = { "foo", "bar", NULL };
3495 const char* const desc = "%(foo)s and %(foo)s";
3496 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3497 ElementsAre(EqInterpolation(desc, desc + 7, 0),
3498 EqInterpolation(desc + 12, desc + 19, 0)));
3499}
3500
3501TEST(ValidateMatcherDescriptionTest, RejectsUnknownParam) {
3502 const char* params[] = { "a", "bar", NULL };
3503 EXPECT_NONFATAL_FAILURE({
3504 EXPECT_THAT(ValidateMatcherDescription(params, "%(foo)s"),
3505 ElementsAre());
3506 }, "Syntax error at index 2 in matcher description \"%(foo)s\": "
3507 "\"foo\" is an invalid parameter name.");
3508}
3509
3510TEST(ValidateMatcherDescriptionTest, RejectsUnfinishedParam) {
3511 const char* params[] = { "a", "bar", NULL };
3512 EXPECT_NONFATAL_FAILURE({
3513 EXPECT_THAT(ValidateMatcherDescription(params, "%(foo)"),
3514 ElementsAre());
3515 }, "Syntax error at index 0 in matcher description \"%(foo)\": "
3516 "an interpolation must end with \")s\", but \"%(foo)\" does not.");
3517
3518 EXPECT_NONFATAL_FAILURE({
3519 EXPECT_THAT(ValidateMatcherDescription(params, "x%(a"),
3520 ElementsAre());
3521 }, "Syntax error at index 1 in matcher description \"x%(a\": "
3522 "an interpolation must end with \")s\", but \"%(a\" does not.");
3523}
3524
3525TEST(ValidateMatcherDescriptionTest, RejectsSinglePercent) {
3526 const char* params[] = { "a", NULL };
3527 EXPECT_NONFATAL_FAILURE({
3528 EXPECT_THAT(ValidateMatcherDescription(params, "a %."),
3529 ElementsAre());
3530 }, "Syntax error at index 2 in matcher description \"a %.\": "
3531 "use \"%%\" instead of \"%\" to print \"%\".");
3532
3533}
3534
3535// Tests JoinAsTuple().
3536
3537TEST(JoinAsTupleTest, JoinsEmptyTuple) {
3538 EXPECT_EQ("", JoinAsTuple(Strings()));
3539}
3540
3541TEST(JoinAsTupleTest, JoinsOneTuple) {
3542 const char* fields[] = { "1" };
3543 EXPECT_EQ("1", JoinAsTuple(Strings(fields, fields + 1)));
3544}
3545
3546TEST(JoinAsTupleTest, JoinsTwoTuple) {
3547 const char* fields[] = { "1", "a" };
3548 EXPECT_EQ("(1, a)", JoinAsTuple(Strings(fields, fields + 2)));
3549}
3550
3551TEST(JoinAsTupleTest, JoinsTenTuple) {
3552 const char* fields[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
3553 EXPECT_EQ("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)",
3554 JoinAsTuple(Strings(fields, fields + 10)));
3555}
3556
3557// Tests FormatMatcherDescription().
3558
3559TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) {
3560 EXPECT_EQ("is even",
3561 FormatMatcherDescription("IsEven", "", Interpolations(),
3562 Strings()));
3563
3564 const char* params[] = { "5" };
3565 EXPECT_EQ("equals 5",
3566 FormatMatcherDescription("Equals", "", Interpolations(),
3567 Strings(params, params + 1)));
3568
3569 const char* params2[] = { "5", "8" };
3570 EXPECT_EQ("is in range (5, 8)",
3571 FormatMatcherDescription("IsInRange", "", Interpolations(),
3572 Strings(params2, params2 + 2)));
3573}
3574
3575TEST(FormatMatcherDescriptionTest, WorksForDescriptionWithNoInterpolation) {
3576 EXPECT_EQ("is positive",
3577 FormatMatcherDescription("Gt0", "is positive", Interpolations(),
3578 Strings()));
3579
3580 const char* params[] = { "5", "6" };
3581 EXPECT_EQ("is negative",
3582 FormatMatcherDescription("Lt0", "is negative", Interpolations(),
3583 Strings(params, params + 2)));
3584}
3585
3586TEST(FormatMatcherDescriptionTest,
3587 WorksWhenDescriptionStartsWithInterpolation) {
3588 const char* params[] = { "5" };
3589 const char* const desc = "%(num)s times bigger";
3590 const Interpolation interp[] = { Interpolation(desc, desc + 7, 0) };
3591 EXPECT_EQ("5 times bigger",
3592 FormatMatcherDescription("Foo", desc,
3593 Interpolations(interp, interp + 1),
3594 Strings(params, params + 1)));
3595}
3596
3597TEST(FormatMatcherDescriptionTest,
3598 WorksWhenDescriptionEndsWithInterpolation) {
3599 const char* params[] = { "5", "6" };
3600 const char* const desc = "is bigger than %(y)s";
3601 const Interpolation interp[] = { Interpolation(desc + 15, desc + 20, 1) };
3602 EXPECT_EQ("is bigger than 6",
3603 FormatMatcherDescription("Foo", desc,
3604 Interpolations(interp, interp + 1),
3605 Strings(params, params + 2)));
3606}
3607
3608TEST(FormatMatcherDescriptionTest,
3609 WorksWhenDescriptionStartsAndEndsWithInterpolation) {
3610 const char* params[] = { "5", "6" };
3611 const char* const desc = "%(x)s <= arg <= %(y)s";
3612 const Interpolation interp[] = {
3613 Interpolation(desc, desc + 5, 0),
3614 Interpolation(desc + 16, desc + 21, 1)
3615 };
3616 EXPECT_EQ("5 <= arg <= 6",
3617 FormatMatcherDescription("Foo", desc,
3618 Interpolations(interp, interp + 2),
3619 Strings(params, params + 2)));
3620}
3621
3622TEST(FormatMatcherDescriptionTest,
3623 WorksWhenDescriptionDoesNotStartOrEndWithInterpolation) {
3624 const char* params[] = { "5.2" };
3625 const char* const desc = "has %(x)s cents";
3626 const Interpolation interp[] = { Interpolation(desc + 4, desc + 9, 0) };
3627 EXPECT_EQ("has 5.2 cents",
3628 FormatMatcherDescription("Foo", desc,
3629 Interpolations(interp, interp + 1),
3630 Strings(params, params + 1)));
3631}
3632
3633TEST(FormatMatcherDescriptionTest,
3634 WorksWhenDescriptionContainsMultipleInterpolations) {
3635 const char* params[] = { "5", "6" };
3636 const char* const desc = "in %(*)s or [%(x)s, %(y)s]";
3637 const Interpolation interp[] = {
3638 Interpolation(desc + 3, desc + 8, kTupleInterpolation),
3639 Interpolation(desc + 13, desc + 18, 0),
3640 Interpolation(desc + 20, desc + 25, 1)
3641 };
3642 EXPECT_EQ("in (5, 6) or [5, 6]",
3643 FormatMatcherDescription("Foo", desc,
3644 Interpolations(interp, interp + 3),
3645 Strings(params, params + 2)));
3646}
3647
3648TEST(FormatMatcherDescriptionTest,
3649 WorksWhenDescriptionContainsRepeatedParams) {
3650 const char* params[] = { "9" };
3651 const char* const desc = "in [-%(x)s, %(x)s]";
3652 const Interpolation interp[] = {
3653 Interpolation(desc + 5, desc + 10, 0),
3654 Interpolation(desc + 12, desc + 17, 0)
3655 };
3656 EXPECT_EQ("in [-9, 9]",
3657 FormatMatcherDescription("Foo", desc,
3658 Interpolations(interp, interp + 2),
3659 Strings(params, params + 1)));
3660}
3661
3662TEST(FormatMatcherDescriptionTest,
3663 WorksForDescriptionWithInvalidInterpolation) {
3664 const char* params[] = { "9" };
3665 const char* const desc = "> %(x)s %(x)";
3666 const Interpolation interp[] = { Interpolation(desc + 2, desc + 7, 0) };
3667 EXPECT_EQ("> 9 %(x)",
3668 FormatMatcherDescription("Foo", desc,
3669 Interpolations(interp, interp + 1),
3670 Strings(params, params + 1)));
3671}
3672
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003673// Tests PolymorphicMatcher::mutable_impl().
3674TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {
3675 PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
3676 DivisibleByImpl& impl = m.mutable_impl();
3677 EXPECT_EQ(42, impl.divider());
3678
3679 impl.set_divider(0);
3680 EXPECT_EQ(0, m.mutable_impl().divider());
3681}
3682
3683// Tests PolymorphicMatcher::impl().
3684TEST(PolymorphicMatcherTest, CanAccessImpl) {
3685 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
3686 const DivisibleByImpl& impl = m.impl();
3687 EXPECT_EQ(42, impl.divider());
3688}
3689
shiqiane35fdd92008-12-10 05:08:54 +00003690} // namespace gmock_matchers_test
3691} // namespace testing