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

Side by Side Diff: testing/gmock/test/gmock-matchers_test.cc

Issue 113807: Checkin a version of gmock, modified to use our boost_tuple in VS2005. (Closed)
Patch Set: Fix grammar issue. Created 11 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2007, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: wan@google.com (Zhanyong Wan)
31
32 // Google Mock - a framework for writing C++ mock classes.
33 //
34 // This file tests some commonly used argument matchers.
35
36 #include <gmock/gmock-matchers.h>
37
38 #include <string.h>
39 #include <functional>
40 #include <list>
41 #include <map>
42 #include <set>
43 #include <sstream>
44 #include <string>
45 #include <vector>
46 #include <gmock/gmock.h>
47 #include <gtest/gtest.h>
48 #include <gtest/gtest-spi.h>
49
50 namespace testing {
51
52 namespace internal {
53 string FormatMatcherDescriptionSyntaxError(const char* description,
54 const char* error_pos);
55 int GetParamIndex(const char* param_names[], const string& param_name);
56 string JoinAsTuple(const Strings& fields);
57 bool SkipPrefix(const char* prefix, const char** pstr);
58 } // namespace internal
59
60 namespace gmock_matchers_test {
61
62 using std::stringstream;
63 using testing::A;
64 using testing::AllOf;
65 using testing::An;
66 using testing::AnyOf;
67 using testing::ByRef;
68 using testing::DoubleEq;
69 using testing::EndsWith;
70 using testing::Eq;
71 using testing::Field;
72 using testing::FloatEq;
73 using testing::Ge;
74 using testing::Gt;
75 using testing::HasSubstr;
76 using testing::Le;
77 using testing::Lt;
78 using testing::MakeMatcher;
79 using testing::MakePolymorphicMatcher;
80 using testing::Matcher;
81 using testing::MatcherCast;
82 using testing::MatcherInterface;
83 using testing::Matches;
84 using testing::NanSensitiveDoubleEq;
85 using testing::NanSensitiveFloatEq;
86 using testing::Ne;
87 using testing::Not;
88 using testing::NotNull;
89 using testing::Pointee;
90 using testing::PolymorphicMatcher;
91 using testing::Property;
92 using testing::Ref;
93 using testing::ResultOf;
94 using testing::StartsWith;
95 using testing::StrCaseEq;
96 using testing::StrCaseNe;
97 using testing::StrEq;
98 using testing::StrNe;
99 using testing::Truly;
100 using testing::TypedEq;
101 using testing::_;
102 using testing::internal::FloatingEqMatcher;
103 using testing::internal::FormatMatcherDescriptionSyntaxError;
104 using testing::internal::GetParamIndex;
105 using testing::internal::Interpolation;
106 using testing::internal::Interpolations;
107 using testing::internal::JoinAsTuple;
108 using testing::internal::SkipPrefix;
109 using testing::internal::String;
110 using testing::internal::Strings;
111 using testing::internal::ValidateMatcherDescription;
112 using testing::internal::kInvalidInterpolation;
113 using testing::internal::kPercentInterpolation;
114 using testing::internal::kTupleInterpolation;
115 using testing::internal::string;
116
117 #ifdef GMOCK_HAS_REGEX
118 using testing::ContainsRegex;
119 using testing::MatchesRegex;
120 using testing::internal::RE;
121 #endif // GMOCK_HAS_REGEX
122
123 // Returns the description of the given matcher.
124 template <typename T>
125 string Describe(const Matcher<T>& m) {
126 stringstream ss;
127 m.DescribeTo(&ss);
128 return ss.str();
129 }
130
131 // Returns the description of the negation of the given matcher.
132 template <typename T>
133 string DescribeNegation(const Matcher<T>& m) {
134 stringstream ss;
135 m.DescribeNegationTo(&ss);
136 return ss.str();
137 }
138
139 // Returns the reason why x matches, or doesn't match, m.
140 template <typename MatcherType, typename Value>
141 string Explain(const MatcherType& m, const Value& x) {
142 stringstream ss;
143 m.ExplainMatchResultTo(x, &ss);
144 return ss.str();
145 }
146
147 // Makes sure that the MatcherInterface<T> interface doesn't
148 // change.
149 class EvenMatcherImpl : public MatcherInterface<int> {
150 public:
151 virtual bool Matches(int x) const { return x % 2 == 0; }
152
153 virtual void DescribeTo(::std::ostream* os) const {
154 *os << "is an even number";
155 }
156
157 // We deliberately don't define DescribeNegationTo() and
158 // ExplainMatchResultTo() here, to make sure the definition of these
159 // two methods is optional.
160 };
161
162 TEST(MatcherInterfaceTest, CanBeImplemented) {
163 EvenMatcherImpl m;
164 }
165
166 // Tests default-constructing a matcher.
167 TEST(MatcherTest, CanBeDefaultConstructed) {
168 Matcher<double> m;
169 }
170
171 // Tests that Matcher<T> can be constructed from a MatcherInterface<T>*.
172 TEST(MatcherTest, CanBeConstructedFromMatcherInterface) {
173 const MatcherInterface<int>* impl = new EvenMatcherImpl;
174 Matcher<int> m(impl);
175 EXPECT_TRUE(m.Matches(4));
176 EXPECT_FALSE(m.Matches(5));
177 }
178
179 // Tests that value can be used in place of Eq(value).
180 TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) {
181 Matcher<int> m1 = 5;
182 EXPECT_TRUE(m1.Matches(5));
183 EXPECT_FALSE(m1.Matches(6));
184 }
185
186 // Tests that NULL can be used in place of Eq(NULL).
187 TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) {
188 Matcher<int*> m1 = NULL;
189 EXPECT_TRUE(m1.Matches(NULL));
190 int n = 0;
191 EXPECT_FALSE(m1.Matches(&n));
192 }
193
194 // Tests that matchers are copyable.
195 TEST(MatcherTest, IsCopyable) {
196 // Tests the copy constructor.
197 Matcher<bool> m1 = Eq(false);
198 EXPECT_TRUE(m1.Matches(false));
199 EXPECT_FALSE(m1.Matches(true));
200
201 // Tests the assignment operator.
202 m1 = Eq(true);
203 EXPECT_TRUE(m1.Matches(true));
204 EXPECT_FALSE(m1.Matches(false));
205 }
206
207 // Tests that Matcher<T>::DescribeTo() calls
208 // MatcherInterface<T>::DescribeTo().
209 TEST(MatcherTest, CanDescribeItself) {
210 EXPECT_EQ("is an even number",
211 Describe(Matcher<int>(new EvenMatcherImpl)));
212 }
213
214 // Tests that a C-string literal can be implicitly converted to a
215 // Matcher<string> or Matcher<const string&>.
216 TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
217 Matcher<string> m1 = "hi";
218 EXPECT_TRUE(m1.Matches("hi"));
219 EXPECT_FALSE(m1.Matches("hello"));
220
221 Matcher<const string&> m2 = "hi";
222 EXPECT_TRUE(m2.Matches("hi"));
223 EXPECT_FALSE(m2.Matches("hello"));
224 }
225
226 // Tests that a string object can be implicitly converted to a
227 // Matcher<string> or Matcher<const string&>.
228 TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {
229 Matcher<string> m1 = string("hi");
230 EXPECT_TRUE(m1.Matches("hi"));
231 EXPECT_FALSE(m1.Matches("hello"));
232
233 Matcher<const string&> m2 = string("hi");
234 EXPECT_TRUE(m2.Matches("hi"));
235 EXPECT_FALSE(m2.Matches("hello"));
236 }
237
238 // Tests that MakeMatcher() constructs a Matcher<T> from a
239 // MatcherInterface* without requiring the user to explicitly
240 // write the type.
241 TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) {
242 const MatcherInterface<int>* dummy_impl = NULL;
243 Matcher<int> m = MakeMatcher(dummy_impl);
244 }
245
246 // Tests that MakePolymorphicMatcher() constructs a polymorphic
247 // matcher from its implementation.
248 const int bar = 1;
249 class ReferencesBarOrIsZeroImpl {
250 public:
251 template <typename T>
252 bool Matches(const T& x) const {
253 const void* p = &x;
254 return p == &bar || x == 0;
255 }
256
257 void DescribeTo(::std::ostream* os) const { *os << "bar or zero"; }
258
259 void DescribeNegationTo(::std::ostream* os) const {
260 *os << "doesn't reference bar and is not zero";
261 }
262 };
263
264 // This function verifies that MakePolymorphicMatcher() returns a
265 // PolymorphicMatcher<T> where T is the argument's type.
266 PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() {
267 return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl());
268 }
269
270 TEST(MakePolymorphicMatcherTest, ConstructsMatcherFromImpl) {
271 // Using a polymorphic matcher to match a reference type.
272 Matcher<const int&> m1 = ReferencesBarOrIsZero();
273 EXPECT_TRUE(m1.Matches(0));
274 // Verifies that the identity of a by-reference argument is preserved.
275 EXPECT_TRUE(m1.Matches(bar));
276 EXPECT_FALSE(m1.Matches(1));
277 EXPECT_EQ("bar or zero", Describe(m1));
278
279 // Using a polymorphic matcher to match a value type.
280 Matcher<double> m2 = ReferencesBarOrIsZero();
281 EXPECT_TRUE(m2.Matches(0.0));
282 EXPECT_FALSE(m2.Matches(0.1));
283 EXPECT_EQ("bar or zero", Describe(m2));
284 }
285
286 // Tests that MatcherCast<T>(m) works when m is a polymorphic matcher.
287 TEST(MatcherCastTest, FromPolymorphicMatcher) {
288 Matcher<int> m = MatcherCast<int>(Eq(5));
289 EXPECT_TRUE(m.Matches(5));
290 EXPECT_FALSE(m.Matches(6));
291 }
292
293 // For testing casting matchers between compatible types.
294 class IntValue {
295 public:
296 // An int can be statically (although not implicitly) cast to a
297 // IntValue.
298 explicit IntValue(int value) : value_(value) {}
299
300 int value() const { return value_; }
301 private:
302 int value_;
303 };
304
305 // For testing casting matchers between compatible types.
306 bool IsPositiveIntValue(const IntValue& foo) {
307 return foo.value() > 0;
308 }
309
310 // Tests that MatcherCast<T>(m) works when m is a Matcher<U> where T
311 // can be statically converted to U.
312 TEST(MatcherCastTest, FromCompatibleType) {
313 Matcher<double> m1 = Eq(2.0);
314 Matcher<int> m2 = MatcherCast<int>(m1);
315 EXPECT_TRUE(m2.Matches(2));
316 EXPECT_FALSE(m2.Matches(3));
317
318 Matcher<IntValue> m3 = Truly(IsPositiveIntValue);
319 Matcher<int> m4 = MatcherCast<int>(m3);
320 // In the following, the arguments 1 and 0 are statically converted
321 // to IntValue objects, and then tested by the IsPositiveIntValue()
322 // predicate.
323 EXPECT_TRUE(m4.Matches(1));
324 EXPECT_FALSE(m4.Matches(0));
325 }
326
327 // Tests that MatcherCast<T>(m) works when m is a Matcher<const T&>.
328 TEST(MatcherCastTest, FromConstReferenceToNonReference) {
329 Matcher<const int&> m1 = Eq(0);
330 Matcher<int> m2 = MatcherCast<int>(m1);
331 EXPECT_TRUE(m2.Matches(0));
332 EXPECT_FALSE(m2.Matches(1));
333 }
334
335 // Tests that MatcherCast<T>(m) works when m is a Matcher<T&>.
336 TEST(MatcherCastTest, FromReferenceToNonReference) {
337 Matcher<int&> m1 = Eq(0);
338 Matcher<int> m2 = MatcherCast<int>(m1);
339 EXPECT_TRUE(m2.Matches(0));
340 EXPECT_FALSE(m2.Matches(1));
341 }
342
343 // Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
344 TEST(MatcherCastTest, FromNonReferenceToConstReference) {
345 Matcher<int> m1 = Eq(0);
346 Matcher<const int&> m2 = MatcherCast<const int&>(m1);
347 EXPECT_TRUE(m2.Matches(0));
348 EXPECT_FALSE(m2.Matches(1));
349 }
350
351 // Tests that MatcherCast<T&>(m) works when m is a Matcher<T>.
352 TEST(MatcherCastTest, FromNonReferenceToReference) {
353 Matcher<int> m1 = Eq(0);
354 Matcher<int&> m2 = MatcherCast<int&>(m1);
355 int n = 0;
356 EXPECT_TRUE(m2.Matches(n));
357 n = 1;
358 EXPECT_FALSE(m2.Matches(n));
359 }
360
361 // Tests that MatcherCast<T>(m) works when m is a Matcher<T>.
362 TEST(MatcherCastTest, FromSameType) {
363 Matcher<int> m1 = Eq(0);
364 Matcher<int> m2 = MatcherCast<int>(m1);
365 EXPECT_TRUE(m2.Matches(0));
366 EXPECT_FALSE(m2.Matches(1));
367 }
368
369 class Base {};
370 class Derived : public Base {};
371
372 // Tests that SafeMatcherCast<T>(m) works when m is a polymorphic matcher.
373 TEST(SafeMatcherCastTest, FromPolymorphicMatcher) {
374 Matcher<char> m2 = SafeMatcherCast<char>(Eq(32));
375 EXPECT_TRUE(m2.Matches(' '));
376 EXPECT_FALSE(m2.Matches('\n'));
377 }
378
379 // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where
380 // T and U are arithmetic types and T can be losslessly converted to
381 // U.
382 TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) {
383 Matcher<double> m1 = DoubleEq(1.0);
384 Matcher<float> m2 = SafeMatcherCast<float>(m1);
385 EXPECT_TRUE(m2.Matches(1.0f));
386 EXPECT_FALSE(m2.Matches(2.0f));
387
388 Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>('a'));
389 EXPECT_TRUE(m3.Matches('a'));
390 EXPECT_FALSE(m3.Matches('b'));
391 }
392
393 // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where T and U
394 // are pointers or references to a derived and a base class, correspondingly.
395 TEST(SafeMatcherCastTest, FromBaseClass) {
396 Derived d, d2;
397 Matcher<Base*> m1 = Eq(&d);
398 Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1);
399 EXPECT_TRUE(m2.Matches(&d));
400 EXPECT_FALSE(m2.Matches(&d2));
401
402 Matcher<Base&> m3 = Ref(d);
403 Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3);
404 EXPECT_TRUE(m4.Matches(d));
405 EXPECT_FALSE(m4.Matches(d2));
406 }
407
408 // Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<const T&>.
409 TEST(SafeMatcherCastTest, FromConstReferenceToReference) {
410 int n = 0;
411 Matcher<const int&> m1 = Ref(n);
412 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
413 int n1 = 0;
414 EXPECT_TRUE(m2.Matches(n));
415 EXPECT_FALSE(m2.Matches(n1));
416 }
417
418 // Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
419 TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) {
420 Matcher<int> m1 = Eq(0);
421 Matcher<const int&> m2 = SafeMatcherCast<const int&>(m1);
422 EXPECT_TRUE(m2.Matches(0));
423 EXPECT_FALSE(m2.Matches(1));
424 }
425
426 // Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<T>.
427 TEST(SafeMatcherCastTest, FromNonReferenceToReference) {
428 Matcher<int> m1 = Eq(0);
429 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
430 int n = 0;
431 EXPECT_TRUE(m2.Matches(n));
432 n = 1;
433 EXPECT_FALSE(m2.Matches(n));
434 }
435
436 // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<T>.
437 TEST(SafeMatcherCastTest, FromSameType) {
438 Matcher<int> m1 = Eq(0);
439 Matcher<int> m2 = SafeMatcherCast<int>(m1);
440 EXPECT_TRUE(m2.Matches(0));
441 EXPECT_FALSE(m2.Matches(1));
442 }
443
444 // Tests that A<T>() matches any value of type T.
445 TEST(ATest, MatchesAnyValue) {
446 // Tests a matcher for a value type.
447 Matcher<double> m1 = A<double>();
448 EXPECT_TRUE(m1.Matches(91.43));
449 EXPECT_TRUE(m1.Matches(-15.32));
450
451 // Tests a matcher for a reference type.
452 int a = 2;
453 int b = -6;
454 Matcher<int&> m2 = A<int&>();
455 EXPECT_TRUE(m2.Matches(a));
456 EXPECT_TRUE(m2.Matches(b));
457 }
458
459 // Tests that A<T>() describes itself properly.
460 TEST(ATest, CanDescribeSelf) {
461 EXPECT_EQ("is anything", Describe(A<bool>()));
462 }
463
464 // Tests that An<T>() matches any value of type T.
465 TEST(AnTest, MatchesAnyValue) {
466 // Tests a matcher for a value type.
467 Matcher<int> m1 = An<int>();
468 EXPECT_TRUE(m1.Matches(9143));
469 EXPECT_TRUE(m1.Matches(-1532));
470
471 // Tests a matcher for a reference type.
472 int a = 2;
473 int b = -6;
474 Matcher<int&> m2 = An<int&>();
475 EXPECT_TRUE(m2.Matches(a));
476 EXPECT_TRUE(m2.Matches(b));
477 }
478
479 // Tests that An<T>() describes itself properly.
480 TEST(AnTest, CanDescribeSelf) {
481 EXPECT_EQ("is anything", Describe(An<int>()));
482 }
483
484 // Tests that _ can be used as a matcher for any type and matches any
485 // value of that type.
486 TEST(UnderscoreTest, MatchesAnyValue) {
487 // Uses _ as a matcher for a value type.
488 Matcher<int> m1 = _;
489 EXPECT_TRUE(m1.Matches(123));
490 EXPECT_TRUE(m1.Matches(-242));
491
492 // Uses _ as a matcher for a reference type.
493 bool a = false;
494 const bool b = true;
495 Matcher<const bool&> m2 = _;
496 EXPECT_TRUE(m2.Matches(a));
497 EXPECT_TRUE(m2.Matches(b));
498 }
499
500 // Tests that _ describes itself properly.
501 TEST(UnderscoreTest, CanDescribeSelf) {
502 Matcher<int> m = _;
503 EXPECT_EQ("is anything", Describe(m));
504 }
505
506 // Tests that Eq(x) matches any value equal to x.
507 TEST(EqTest, MatchesEqualValue) {
508 // 2 C-strings with same content but different addresses.
509 const char a1[] = "hi";
510 const char a2[] = "hi";
511
512 Matcher<const char*> m1 = Eq(a1);
513 EXPECT_TRUE(m1.Matches(a1));
514 EXPECT_FALSE(m1.Matches(a2));
515 }
516
517 // Tests that Eq(v) describes itself properly.
518
519 class Unprintable {
520 public:
521 Unprintable() : c_('a') {}
522
523 bool operator==(const Unprintable& rhs) { return true; }
524 private:
525 char c_;
526 };
527
528 TEST(EqTest, CanDescribeSelf) {
529 Matcher<Unprintable> m = Eq(Unprintable());
530 EXPECT_EQ("is equal to 1-byte object <61>", Describe(m));
531 }
532
533 // Tests that Eq(v) can be used to match any type that supports
534 // comparing with type T, where T is v's type.
535 TEST(EqTest, IsPolymorphic) {
536 Matcher<int> m1 = Eq(1);
537 EXPECT_TRUE(m1.Matches(1));
538 EXPECT_FALSE(m1.Matches(2));
539
540 Matcher<char> m2 = Eq(1);
541 EXPECT_TRUE(m2.Matches('\1'));
542 EXPECT_FALSE(m2.Matches('a'));
543 }
544
545 // Tests that TypedEq<T>(v) matches values of type T that's equal to v.
546 TEST(TypedEqTest, ChecksEqualityForGivenType) {
547 Matcher<char> m1 = TypedEq<char>('a');
548 EXPECT_TRUE(m1.Matches('a'));
549 EXPECT_FALSE(m1.Matches('b'));
550
551 Matcher<int> m2 = TypedEq<int>(6);
552 EXPECT_TRUE(m2.Matches(6));
553 EXPECT_FALSE(m2.Matches(7));
554 }
555
556 // Tests that TypedEq(v) describes itself properly.
557 TEST(TypedEqTest, CanDescribeSelf) {
558 EXPECT_EQ("is equal to 2", Describe(TypedEq<int>(2)));
559 }
560
561 // Tests that TypedEq<T>(v) has type Matcher<T>.
562
563 // Type<T>::IsTypeOf(v) compiles iff the type of value v is T, where T
564 // is a "bare" type (i.e. not in the form of const U or U&). If v's
565 // type is not T, the compiler will generate a message about
566 // "undefined referece".
567 template <typename T>
568 struct Type {
569 static bool IsTypeOf(const T& v) { return true; }
570
571 template <typename T2>
572 static void IsTypeOf(T2 v);
573 };
574
575 TEST(TypedEqTest, HasSpecifiedType) {
576 // Verfies that the type of TypedEq<T>(v) is Matcher<T>.
577 Type<Matcher<int> >::IsTypeOf(TypedEq<int>(5));
578 Type<Matcher<double> >::IsTypeOf(TypedEq<double>(5));
579 }
580
581 // Tests that Ge(v) matches anything >= v.
582 TEST(GeTest, ImplementsGreaterThanOrEqual) {
583 Matcher<int> m1 = Ge(0);
584 EXPECT_TRUE(m1.Matches(1));
585 EXPECT_TRUE(m1.Matches(0));
586 EXPECT_FALSE(m1.Matches(-1));
587 }
588
589 // Tests that Ge(v) describes itself properly.
590 TEST(GeTest, CanDescribeSelf) {
591 Matcher<int> m = Ge(5);
592 EXPECT_EQ("is greater than or equal to 5", Describe(m));
593 }
594
595 // Tests that Gt(v) matches anything > v.
596 TEST(GtTest, ImplementsGreaterThan) {
597 Matcher<double> m1 = Gt(0);
598 EXPECT_TRUE(m1.Matches(1.0));
599 EXPECT_FALSE(m1.Matches(0.0));
600 EXPECT_FALSE(m1.Matches(-1.0));
601 }
602
603 // Tests that Gt(v) describes itself properly.
604 TEST(GtTest, CanDescribeSelf) {
605 Matcher<int> m = Gt(5);
606 EXPECT_EQ("is greater than 5", Describe(m));
607 }
608
609 // Tests that Le(v) matches anything <= v.
610 TEST(LeTest, ImplementsLessThanOrEqual) {
611 Matcher<char> m1 = Le('b');
612 EXPECT_TRUE(m1.Matches('a'));
613 EXPECT_TRUE(m1.Matches('b'));
614 EXPECT_FALSE(m1.Matches('c'));
615 }
616
617 // Tests that Le(v) describes itself properly.
618 TEST(LeTest, CanDescribeSelf) {
619 Matcher<int> m = Le(5);
620 EXPECT_EQ("is less than or equal to 5", Describe(m));
621 }
622
623 // Tests that Lt(v) matches anything < v.
624 TEST(LtTest, ImplementsLessThan) {
625 Matcher<const string&> m1 = Lt("Hello");
626 EXPECT_TRUE(m1.Matches("Abc"));
627 EXPECT_FALSE(m1.Matches("Hello"));
628 EXPECT_FALSE(m1.Matches("Hello, world!"));
629 }
630
631 // Tests that Lt(v) describes itself properly.
632 TEST(LtTest, CanDescribeSelf) {
633 Matcher<int> m = Lt(5);
634 EXPECT_EQ("is less than 5", Describe(m));
635 }
636
637 // Tests that Ne(v) matches anything != v.
638 TEST(NeTest, ImplementsNotEqual) {
639 Matcher<int> m1 = Ne(0);
640 EXPECT_TRUE(m1.Matches(1));
641 EXPECT_TRUE(m1.Matches(-1));
642 EXPECT_FALSE(m1.Matches(0));
643 }
644
645 // Tests that Ne(v) describes itself properly.
646 TEST(NeTest, CanDescribeSelf) {
647 Matcher<int> m = Ne(5);
648 EXPECT_EQ("is not equal to 5", Describe(m));
649 }
650
651 // Tests that NotNull() matches any non-NULL pointer of any type.
652 TEST(NotNullTest, MatchesNonNullPointer) {
653 Matcher<int*> m1 = NotNull();
654 int* p1 = NULL;
655 int n = 0;
656 EXPECT_FALSE(m1.Matches(p1));
657 EXPECT_TRUE(m1.Matches(&n));
658
659 Matcher<const char*> m2 = NotNull();
660 const char* p2 = NULL;
661 EXPECT_FALSE(m2.Matches(p2));
662 EXPECT_TRUE(m2.Matches("hi"));
663 }
664
665 // Tests that NotNull() describes itself properly.
666 TEST(NotNullTest, CanDescribeSelf) {
667 Matcher<int*> m = NotNull();
668 EXPECT_EQ("is not NULL", Describe(m));
669 }
670
671 // Tests that Ref(variable) matches an argument that references
672 // 'variable'.
673 TEST(RefTest, MatchesSameVariable) {
674 int a = 0;
675 int b = 0;
676 Matcher<int&> m = Ref(a);
677 EXPECT_TRUE(m.Matches(a));
678 EXPECT_FALSE(m.Matches(b));
679 }
680
681 // Tests that Ref(variable) describes itself properly.
682 TEST(RefTest, CanDescribeSelf) {
683 int n = 5;
684 Matcher<int&> m = Ref(n);
685 stringstream ss;
686 ss << "references the variable @" << &n << " 5";
687 EXPECT_EQ(string(ss.str()), Describe(m));
688 }
689
690 // Test that Ref(non_const_varialbe) can be used as a matcher for a
691 // const reference.
692 TEST(RefTest, CanBeUsedAsMatcherForConstReference) {
693 int a = 0;
694 int b = 0;
695 Matcher<const int&> m = Ref(a);
696 EXPECT_TRUE(m.Matches(a));
697 EXPECT_FALSE(m.Matches(b));
698 }
699
700 // Tests that Ref(variable) is covariant, i.e. Ref(derived) can be
701 // used wherever Ref(base) can be used (Ref(derived) is a sub-type
702 // of Ref(base), but not vice versa.
703
704 TEST(RefTest, IsCovariant) {
705 Base base, base2;
706 Derived derived;
707 Matcher<const Base&> m1 = Ref(base);
708 EXPECT_TRUE(m1.Matches(base));
709 EXPECT_FALSE(m1.Matches(base2));
710 EXPECT_FALSE(m1.Matches(derived));
711
712 m1 = Ref(derived);
713 EXPECT_TRUE(m1.Matches(derived));
714 EXPECT_FALSE(m1.Matches(base));
715 EXPECT_FALSE(m1.Matches(base2));
716 }
717
718 // Tests string comparison matchers.
719
720 TEST(StrEqTest, MatchesEqualString) {
721 Matcher<const char*> m = StrEq(string("Hello"));
722 EXPECT_TRUE(m.Matches("Hello"));
723 EXPECT_FALSE(m.Matches("hello"));
724 EXPECT_FALSE(m.Matches(NULL));
725
726 Matcher<const string&> m2 = StrEq("Hello");
727 EXPECT_TRUE(m2.Matches("Hello"));
728 EXPECT_FALSE(m2.Matches("Hi"));
729 }
730
731 TEST(StrEqTest, CanDescribeSelf) {
732 Matcher<string> m = StrEq("Hi-\'\"\?\\\a\b\f\n\r\t\v\xD3");
733 EXPECT_EQ("is equal to \"Hi-\'\\\"\\?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",
734 Describe(m));
735
736 string str("01204500800");
737 str[3] = '\0';
738 Matcher<string> m2 = StrEq(str);
739 EXPECT_EQ("is equal to \"012\\04500800\"", Describe(m2));
740 str[0] = str[6] = str[7] = str[9] = str[10] = '\0';
741 Matcher<string> m3 = StrEq(str);
742 EXPECT_EQ("is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3));
743 }
744
745 TEST(StrNeTest, MatchesUnequalString) {
746 Matcher<const char*> m = StrNe("Hello");
747 EXPECT_TRUE(m.Matches(""));
748 EXPECT_TRUE(m.Matches(NULL));
749 EXPECT_FALSE(m.Matches("Hello"));
750
751 Matcher<string> m2 = StrNe(string("Hello"));
752 EXPECT_TRUE(m2.Matches("hello"));
753 EXPECT_FALSE(m2.Matches("Hello"));
754 }
755
756 TEST(StrNeTest, CanDescribeSelf) {
757 Matcher<const char*> m = StrNe("Hi");
758 EXPECT_EQ("is not equal to \"Hi\"", Describe(m));
759 }
760
761 TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
762 Matcher<const char*> m = StrCaseEq(string("Hello"));
763 EXPECT_TRUE(m.Matches("Hello"));
764 EXPECT_TRUE(m.Matches("hello"));
765 EXPECT_FALSE(m.Matches("Hi"));
766 EXPECT_FALSE(m.Matches(NULL));
767
768 Matcher<const string&> m2 = StrCaseEq("Hello");
769 EXPECT_TRUE(m2.Matches("hello"));
770 EXPECT_FALSE(m2.Matches("Hi"));
771 }
772
773 TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
774 string str1("oabocdooeoo");
775 string str2("OABOCDOOEOO");
776 Matcher<const string&> m0 = StrCaseEq(str1);
777 EXPECT_FALSE(m0.Matches(str2 + string(1, '\0')));
778
779 str1[3] = str2[3] = '\0';
780 Matcher<const string&> m1 = StrCaseEq(str1);
781 EXPECT_TRUE(m1.Matches(str2));
782
783 str1[0] = str1[6] = str1[7] = str1[10] = '\0';
784 str2[0] = str2[6] = str2[7] = str2[10] = '\0';
785 Matcher<const string&> m2 = StrCaseEq(str1);
786 str1[9] = str2[9] = '\0';
787 EXPECT_FALSE(m2.Matches(str2));
788
789 Matcher<const string&> m3 = StrCaseEq(str1);
790 EXPECT_TRUE(m3.Matches(str2));
791
792 EXPECT_FALSE(m3.Matches(str2 + "x"));
793 str2.append(1, '\0');
794 EXPECT_FALSE(m3.Matches(str2));
795 EXPECT_FALSE(m3.Matches(string(str2, 0, 9)));
796 }
797
798 TEST(StrCaseEqTest, CanDescribeSelf) {
799 Matcher<string> m = StrCaseEq("Hi");
800 EXPECT_EQ("is equal to (ignoring case) \"Hi\"", Describe(m));
801 }
802
803 TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
804 Matcher<const char*> m = StrCaseNe("Hello");
805 EXPECT_TRUE(m.Matches("Hi"));
806 EXPECT_TRUE(m.Matches(NULL));
807 EXPECT_FALSE(m.Matches("Hello"));
808 EXPECT_FALSE(m.Matches("hello"));
809
810 Matcher<string> m2 = StrCaseNe(string("Hello"));
811 EXPECT_TRUE(m2.Matches(""));
812 EXPECT_FALSE(m2.Matches("Hello"));
813 }
814
815 TEST(StrCaseNeTest, CanDescribeSelf) {
816 Matcher<const char*> m = StrCaseNe("Hi");
817 EXPECT_EQ("is not equal to (ignoring case) \"Hi\"", Describe(m));
818 }
819
820 // Tests that HasSubstr() works for matching string-typed values.
821 TEST(HasSubstrTest, WorksForStringClasses) {
822 const Matcher<string> m1 = HasSubstr("foo");
823 EXPECT_TRUE(m1.Matches(string("I love food.")));
824 EXPECT_FALSE(m1.Matches(string("tofo")));
825
826 const Matcher<const std::string&> m2 = HasSubstr("foo");
827 EXPECT_TRUE(m2.Matches(std::string("I love food.")));
828 EXPECT_FALSE(m2.Matches(std::string("tofo")));
829 }
830
831 // Tests that HasSubstr() works for matching C-string-typed values.
832 TEST(HasSubstrTest, WorksForCStrings) {
833 const Matcher<char*> m1 = HasSubstr("foo");
834 EXPECT_TRUE(m1.Matches(const_cast<char*>("I love food.")));
835 EXPECT_FALSE(m1.Matches(const_cast<char*>("tofo")));
836 EXPECT_FALSE(m1.Matches(NULL));
837
838 const Matcher<const char*> m2 = HasSubstr("foo");
839 EXPECT_TRUE(m2.Matches("I love food."));
840 EXPECT_FALSE(m2.Matches("tofo"));
841 EXPECT_FALSE(m2.Matches(NULL));
842 }
843
844 // Tests that HasSubstr(s) describes itself properly.
845 TEST(HasSubstrTest, CanDescribeSelf) {
846 Matcher<string> m = HasSubstr("foo\n\"");
847 EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m));
848 }
849
850 // Tests StartsWith(s).
851
852 TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
853 const Matcher<const char*> m1 = StartsWith(string(""));
854 EXPECT_TRUE(m1.Matches("Hi"));
855 EXPECT_TRUE(m1.Matches(""));
856 EXPECT_FALSE(m1.Matches(NULL));
857
858 const Matcher<const string&> m2 = StartsWith("Hi");
859 EXPECT_TRUE(m2.Matches("Hi"));
860 EXPECT_TRUE(m2.Matches("Hi Hi!"));
861 EXPECT_TRUE(m2.Matches("High"));
862 EXPECT_FALSE(m2.Matches("H"));
863 EXPECT_FALSE(m2.Matches(" Hi"));
864 }
865
866 TEST(StartsWithTest, CanDescribeSelf) {
867 Matcher<const std::string> m = StartsWith("Hi");
868 EXPECT_EQ("starts with \"Hi\"", Describe(m));
869 }
870
871 // Tests EndsWith(s).
872
873 TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
874 const Matcher<const char*> m1 = EndsWith("");
875 EXPECT_TRUE(m1.Matches("Hi"));
876 EXPECT_TRUE(m1.Matches(""));
877 EXPECT_FALSE(m1.Matches(NULL));
878
879 const Matcher<const string&> m2 = EndsWith(string("Hi"));
880 EXPECT_TRUE(m2.Matches("Hi"));
881 EXPECT_TRUE(m2.Matches("Wow Hi Hi"));
882 EXPECT_TRUE(m2.Matches("Super Hi"));
883 EXPECT_FALSE(m2.Matches("i"));
884 EXPECT_FALSE(m2.Matches("Hi "));
885 }
886
887 TEST(EndsWithTest, CanDescribeSelf) {
888 Matcher<const std::string> m = EndsWith("Hi");
889 EXPECT_EQ("ends with \"Hi\"", Describe(m));
890 }
891
892 #ifdef GMOCK_HAS_REGEX
893
894 // Tests MatchesRegex().
895
896 TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
897 const Matcher<const char*> m1 = MatchesRegex("a.*z");
898 EXPECT_TRUE(m1.Matches("az"));
899 EXPECT_TRUE(m1.Matches("abcz"));
900 EXPECT_FALSE(m1.Matches(NULL));
901
902 const Matcher<const string&> m2 = MatchesRegex(new RE("a.*z"));
903 EXPECT_TRUE(m2.Matches("azbz"));
904 EXPECT_FALSE(m2.Matches("az1"));
905 EXPECT_FALSE(m2.Matches("1az"));
906 }
907
908 TEST(MatchesRegexTest, CanDescribeSelf) {
909 Matcher<const std::string> m1 = MatchesRegex(string("Hi.*"));
910 EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1));
911
912 Matcher<const char*> m2 = MatchesRegex(new RE("[a-z].*"));
913 EXPECT_EQ("matches regular expression \"[a-z].*\"", Describe(m2));
914 }
915
916 // Tests ContainsRegex().
917
918 TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
919 const Matcher<const char*> m1 = ContainsRegex(string("a.*z"));
920 EXPECT_TRUE(m1.Matches("az"));
921 EXPECT_TRUE(m1.Matches("0abcz1"));
922 EXPECT_FALSE(m1.Matches(NULL));
923
924 const Matcher<const string&> m2 = ContainsRegex(new RE("a.*z"));
925 EXPECT_TRUE(m2.Matches("azbz"));
926 EXPECT_TRUE(m2.Matches("az1"));
927 EXPECT_FALSE(m2.Matches("1a"));
928 }
929
930 TEST(ContainsRegexTest, CanDescribeSelf) {
931 Matcher<const std::string> m1 = ContainsRegex("Hi.*");
932 EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1));
933
934 Matcher<const char*> m2 = ContainsRegex(new RE("[a-z].*"));
935 EXPECT_EQ("contains regular expression \"[a-z].*\"", Describe(m2));
936 }
937 #endif // GMOCK_HAS_REGEX
938
939 // Tests for wide strings.
940 #if GTEST_HAS_STD_WSTRING
941 TEST(StdWideStrEqTest, MatchesEqual) {
942 Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello"));
943 EXPECT_TRUE(m.Matches(L"Hello"));
944 EXPECT_FALSE(m.Matches(L"hello"));
945 EXPECT_FALSE(m.Matches(NULL));
946
947 Matcher<const ::std::wstring&> m2 = StrEq(L"Hello");
948 EXPECT_TRUE(m2.Matches(L"Hello"));
949 EXPECT_FALSE(m2.Matches(L"Hi"));
950
951 Matcher<const ::std::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
952 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
953 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
954
955 ::std::wstring str(L"01204500800");
956 str[3] = L'\0';
957 Matcher<const ::std::wstring&> m4 = StrEq(str);
958 EXPECT_TRUE(m4.Matches(str));
959 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
960 Matcher<const ::std::wstring&> m5 = StrEq(str);
961 EXPECT_TRUE(m5.Matches(str));
962 }
963
964 TEST(StdWideStrEqTest, CanDescribeSelf) {
965 Matcher< ::std::wstring> m = StrEq(L"Hi-\'\"\?\\\a\b\f\n\r\t\v");
966 EXPECT_EQ("is equal to L\"Hi-\'\\\"\\?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
967 Describe(m));
968
969 Matcher< ::std::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
970 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
971 Describe(m2));
972
973 ::std::wstring str(L"01204500800");
974 str[3] = L'\0';
975 Matcher<const ::std::wstring&> m4 = StrEq(str);
976 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
977 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
978 Matcher<const ::std::wstring&> m5 = StrEq(str);
979 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
980 }
981
982 TEST(StdWideStrNeTest, MatchesUnequalString) {
983 Matcher<const wchar_t*> m = StrNe(L"Hello");
984 EXPECT_TRUE(m.Matches(L""));
985 EXPECT_TRUE(m.Matches(NULL));
986 EXPECT_FALSE(m.Matches(L"Hello"));
987
988 Matcher< ::std::wstring> m2 = StrNe(::std::wstring(L"Hello"));
989 EXPECT_TRUE(m2.Matches(L"hello"));
990 EXPECT_FALSE(m2.Matches(L"Hello"));
991 }
992
993 TEST(StdWideStrNeTest, CanDescribeSelf) {
994 Matcher<const wchar_t*> m = StrNe(L"Hi");
995 EXPECT_EQ("is not equal to L\"Hi\"", Describe(m));
996 }
997
998 TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
999 Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello"));
1000 EXPECT_TRUE(m.Matches(L"Hello"));
1001 EXPECT_TRUE(m.Matches(L"hello"));
1002 EXPECT_FALSE(m.Matches(L"Hi"));
1003 EXPECT_FALSE(m.Matches(NULL));
1004
1005 Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello");
1006 EXPECT_TRUE(m2.Matches(L"hello"));
1007 EXPECT_FALSE(m2.Matches(L"Hi"));
1008 }
1009
1010 TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1011 ::std::wstring str1(L"oabocdooeoo");
1012 ::std::wstring str2(L"OABOCDOOEOO");
1013 Matcher<const ::std::wstring&> m0 = StrCaseEq(str1);
1014 EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L'\0')));
1015
1016 str1[3] = str2[3] = L'\0';
1017 Matcher<const ::std::wstring&> m1 = StrCaseEq(str1);
1018 EXPECT_TRUE(m1.Matches(str2));
1019
1020 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1021 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1022 Matcher<const ::std::wstring&> m2 = StrCaseEq(str1);
1023 str1[9] = str2[9] = L'\0';
1024 EXPECT_FALSE(m2.Matches(str2));
1025
1026 Matcher<const ::std::wstring&> m3 = StrCaseEq(str1);
1027 EXPECT_TRUE(m3.Matches(str2));
1028
1029 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1030 str2.append(1, L'\0');
1031 EXPECT_FALSE(m3.Matches(str2));
1032 EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9)));
1033 }
1034
1035 TEST(StdWideStrCaseEqTest, CanDescribeSelf) {
1036 Matcher< ::std::wstring> m = StrCaseEq(L"Hi");
1037 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1038 }
1039
1040 TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1041 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1042 EXPECT_TRUE(m.Matches(L"Hi"));
1043 EXPECT_TRUE(m.Matches(NULL));
1044 EXPECT_FALSE(m.Matches(L"Hello"));
1045 EXPECT_FALSE(m.Matches(L"hello"));
1046
1047 Matcher< ::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello"));
1048 EXPECT_TRUE(m2.Matches(L""));
1049 EXPECT_FALSE(m2.Matches(L"Hello"));
1050 }
1051
1052 TEST(StdWideStrCaseNeTest, CanDescribeSelf) {
1053 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
1054 EXPECT_EQ("is not equal to (ignoring case) L\"Hi\"", Describe(m));
1055 }
1056
1057 // Tests that HasSubstr() works for matching wstring-typed values.
1058 TEST(StdWideHasSubstrTest, WorksForStringClasses) {
1059 const Matcher< ::std::wstring> m1 = HasSubstr(L"foo");
1060 EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food.")));
1061 EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo")));
1062
1063 const Matcher<const ::std::wstring&> m2 = HasSubstr(L"foo");
1064 EXPECT_TRUE(m2.Matches(::std::wstring(L"I love food.")));
1065 EXPECT_FALSE(m2.Matches(::std::wstring(L"tofo")));
1066 }
1067
1068 // Tests that HasSubstr() works for matching C-wide-string-typed values.
1069 TEST(StdWideHasSubstrTest, WorksForCStrings) {
1070 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1071 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1072 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1073 EXPECT_FALSE(m1.Matches(NULL));
1074
1075 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1076 EXPECT_TRUE(m2.Matches(L"I love food."));
1077 EXPECT_FALSE(m2.Matches(L"tofo"));
1078 EXPECT_FALSE(m2.Matches(NULL));
1079 }
1080
1081 // Tests that HasSubstr(s) describes itself properly.
1082 TEST(StdWideHasSubstrTest, CanDescribeSelf) {
1083 Matcher< ::std::wstring> m = HasSubstr(L"foo\n\"");
1084 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1085 }
1086
1087 // Tests StartsWith(s).
1088
1089 TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) {
1090 const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L""));
1091 EXPECT_TRUE(m1.Matches(L"Hi"));
1092 EXPECT_TRUE(m1.Matches(L""));
1093 EXPECT_FALSE(m1.Matches(NULL));
1094
1095 const Matcher<const ::std::wstring&> m2 = StartsWith(L"Hi");
1096 EXPECT_TRUE(m2.Matches(L"Hi"));
1097 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1098 EXPECT_TRUE(m2.Matches(L"High"));
1099 EXPECT_FALSE(m2.Matches(L"H"));
1100 EXPECT_FALSE(m2.Matches(L" Hi"));
1101 }
1102
1103 TEST(StdWideStartsWithTest, CanDescribeSelf) {
1104 Matcher<const ::std::wstring> m = StartsWith(L"Hi");
1105 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1106 }
1107
1108 // Tests EndsWith(s).
1109
1110 TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) {
1111 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1112 EXPECT_TRUE(m1.Matches(L"Hi"));
1113 EXPECT_TRUE(m1.Matches(L""));
1114 EXPECT_FALSE(m1.Matches(NULL));
1115
1116 const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L"Hi"));
1117 EXPECT_TRUE(m2.Matches(L"Hi"));
1118 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1119 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1120 EXPECT_FALSE(m2.Matches(L"i"));
1121 EXPECT_FALSE(m2.Matches(L"Hi "));
1122 }
1123
1124 TEST(StdWideEndsWithTest, CanDescribeSelf) {
1125 Matcher<const ::std::wstring> m = EndsWith(L"Hi");
1126 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1127 }
1128
1129 #endif // GTEST_HAS_STD_WSTRING
1130
1131 #if GTEST_HAS_GLOBAL_WSTRING
1132 TEST(GlobalWideStrEqTest, MatchesEqual) {
1133 Matcher<const wchar_t*> m = StrEq(::wstring(L"Hello"));
1134 EXPECT_TRUE(m.Matches(L"Hello"));
1135 EXPECT_FALSE(m.Matches(L"hello"));
1136 EXPECT_FALSE(m.Matches(NULL));
1137
1138 Matcher<const ::wstring&> m2 = StrEq(L"Hello");
1139 EXPECT_TRUE(m2.Matches(L"Hello"));
1140 EXPECT_FALSE(m2.Matches(L"Hi"));
1141
1142 Matcher<const ::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1143 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1144 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1145
1146 ::wstring str(L"01204500800");
1147 str[3] = L'\0';
1148 Matcher<const ::wstring&> m4 = StrEq(str);
1149 EXPECT_TRUE(m4.Matches(str));
1150 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1151 Matcher<const ::wstring&> m5 = StrEq(str);
1152 EXPECT_TRUE(m5.Matches(str));
1153 }
1154
1155 TEST(GlobalWideStrEqTest, CanDescribeSelf) {
1156 Matcher< ::wstring> m = StrEq(L"Hi-\'\"\?\\\a\b\f\n\r\t\v");
1157 EXPECT_EQ("is equal to L\"Hi-\'\\\"\\?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
1158 Describe(m));
1159
1160 Matcher< ::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1161 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1162 Describe(m2));
1163
1164 ::wstring str(L"01204500800");
1165 str[3] = L'\0';
1166 Matcher<const ::wstring&> m4 = StrEq(str);
1167 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1168 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1169 Matcher<const ::wstring&> m5 = StrEq(str);
1170 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1171 }
1172
1173 TEST(GlobalWideStrNeTest, MatchesUnequalString) {
1174 Matcher<const wchar_t*> m = StrNe(L"Hello");
1175 EXPECT_TRUE(m.Matches(L""));
1176 EXPECT_TRUE(m.Matches(NULL));
1177 EXPECT_FALSE(m.Matches(L"Hello"));
1178
1179 Matcher< ::wstring> m2 = StrNe(::wstring(L"Hello"));
1180 EXPECT_TRUE(m2.Matches(L"hello"));
1181 EXPECT_FALSE(m2.Matches(L"Hello"));
1182 }
1183
1184 TEST(GlobalWideStrNeTest, CanDescribeSelf) {
1185 Matcher<const wchar_t*> m = StrNe(L"Hi");
1186 EXPECT_EQ("is not equal to L\"Hi\"", Describe(m));
1187 }
1188
1189 TEST(GlobalWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1190 Matcher<const wchar_t*> m = StrCaseEq(::wstring(L"Hello"));
1191 EXPECT_TRUE(m.Matches(L"Hello"));
1192 EXPECT_TRUE(m.Matches(L"hello"));
1193 EXPECT_FALSE(m.Matches(L"Hi"));
1194 EXPECT_FALSE(m.Matches(NULL));
1195
1196 Matcher<const ::wstring&> m2 = StrCaseEq(L"Hello");
1197 EXPECT_TRUE(m2.Matches(L"hello"));
1198 EXPECT_FALSE(m2.Matches(L"Hi"));
1199 }
1200
1201 TEST(GlobalWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1202 ::wstring str1(L"oabocdooeoo");
1203 ::wstring str2(L"OABOCDOOEOO");
1204 Matcher<const ::wstring&> m0 = StrCaseEq(str1);
1205 EXPECT_FALSE(m0.Matches(str2 + ::wstring(1, L'\0')));
1206
1207 str1[3] = str2[3] = L'\0';
1208 Matcher<const ::wstring&> m1 = StrCaseEq(str1);
1209 EXPECT_TRUE(m1.Matches(str2));
1210
1211 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1212 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1213 Matcher<const ::wstring&> m2 = StrCaseEq(str1);
1214 str1[9] = str2[9] = L'\0';
1215 EXPECT_FALSE(m2.Matches(str2));
1216
1217 Matcher<const ::wstring&> m3 = StrCaseEq(str1);
1218 EXPECT_TRUE(m3.Matches(str2));
1219
1220 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1221 str2.append(1, L'\0');
1222 EXPECT_FALSE(m3.Matches(str2));
1223 EXPECT_FALSE(m3.Matches(::wstring(str2, 0, 9)));
1224 }
1225
1226 TEST(GlobalWideStrCaseEqTest, CanDescribeSelf) {
1227 Matcher< ::wstring> m = StrCaseEq(L"Hi");
1228 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1229 }
1230
1231 TEST(GlobalWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1232 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1233 EXPECT_TRUE(m.Matches(L"Hi"));
1234 EXPECT_TRUE(m.Matches(NULL));
1235 EXPECT_FALSE(m.Matches(L"Hello"));
1236 EXPECT_FALSE(m.Matches(L"hello"));
1237
1238 Matcher< ::wstring> m2 = StrCaseNe(::wstring(L"Hello"));
1239 EXPECT_TRUE(m2.Matches(L""));
1240 EXPECT_FALSE(m2.Matches(L"Hello"));
1241 }
1242
1243 TEST(GlobalWideStrCaseNeTest, CanDescribeSelf) {
1244 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
1245 EXPECT_EQ("is not equal to (ignoring case) L\"Hi\"", Describe(m));
1246 }
1247
1248 // Tests that HasSubstr() works for matching wstring-typed values.
1249 TEST(GlobalWideHasSubstrTest, WorksForStringClasses) {
1250 const Matcher< ::wstring> m1 = HasSubstr(L"foo");
1251 EXPECT_TRUE(m1.Matches(::wstring(L"I love food.")));
1252 EXPECT_FALSE(m1.Matches(::wstring(L"tofo")));
1253
1254 const Matcher<const ::wstring&> m2 = HasSubstr(L"foo");
1255 EXPECT_TRUE(m2.Matches(::wstring(L"I love food.")));
1256 EXPECT_FALSE(m2.Matches(::wstring(L"tofo")));
1257 }
1258
1259 // Tests that HasSubstr() works for matching C-wide-string-typed values.
1260 TEST(GlobalWideHasSubstrTest, WorksForCStrings) {
1261 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1262 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1263 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1264 EXPECT_FALSE(m1.Matches(NULL));
1265
1266 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1267 EXPECT_TRUE(m2.Matches(L"I love food."));
1268 EXPECT_FALSE(m2.Matches(L"tofo"));
1269 EXPECT_FALSE(m2.Matches(NULL));
1270 }
1271
1272 // Tests that HasSubstr(s) describes itself properly.
1273 TEST(GlobalWideHasSubstrTest, CanDescribeSelf) {
1274 Matcher< ::wstring> m = HasSubstr(L"foo\n\"");
1275 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1276 }
1277
1278 // Tests StartsWith(s).
1279
1280 TEST(GlobalWideStartsWithTest, MatchesStringWithGivenPrefix) {
1281 const Matcher<const wchar_t*> m1 = StartsWith(::wstring(L""));
1282 EXPECT_TRUE(m1.Matches(L"Hi"));
1283 EXPECT_TRUE(m1.Matches(L""));
1284 EXPECT_FALSE(m1.Matches(NULL));
1285
1286 const Matcher<const ::wstring&> m2 = StartsWith(L"Hi");
1287 EXPECT_TRUE(m2.Matches(L"Hi"));
1288 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1289 EXPECT_TRUE(m2.Matches(L"High"));
1290 EXPECT_FALSE(m2.Matches(L"H"));
1291 EXPECT_FALSE(m2.Matches(L" Hi"));
1292 }
1293
1294 TEST(GlobalWideStartsWithTest, CanDescribeSelf) {
1295 Matcher<const ::wstring> m = StartsWith(L"Hi");
1296 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1297 }
1298
1299 // Tests EndsWith(s).
1300
1301 TEST(GlobalWideEndsWithTest, MatchesStringWithGivenSuffix) {
1302 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1303 EXPECT_TRUE(m1.Matches(L"Hi"));
1304 EXPECT_TRUE(m1.Matches(L""));
1305 EXPECT_FALSE(m1.Matches(NULL));
1306
1307 const Matcher<const ::wstring&> m2 = EndsWith(::wstring(L"Hi"));
1308 EXPECT_TRUE(m2.Matches(L"Hi"));
1309 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1310 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1311 EXPECT_FALSE(m2.Matches(L"i"));
1312 EXPECT_FALSE(m2.Matches(L"Hi "));
1313 }
1314
1315 TEST(GlobalWideEndsWithTest, CanDescribeSelf) {
1316 Matcher<const ::wstring> m = EndsWith(L"Hi");
1317 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1318 }
1319
1320 #endif // GTEST_HAS_GLOBAL_WSTRING
1321
1322
1323 typedef ::std::tr1::tuple<long, int> Tuple2; // NOLINT
1324
1325 // Tests that Eq() matches a 2-tuple where the first field == the
1326 // second field.
1327 TEST(Eq2Test, MatchesEqualArguments) {
1328 Matcher<const Tuple2&> m = Eq();
1329 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1330 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1331 }
1332
1333 // Tests that Eq() describes itself properly.
1334 TEST(Eq2Test, CanDescribeSelf) {
1335 Matcher<const Tuple2&> m = Eq();
1336 EXPECT_EQ("argument #0 is equal to argument #1", Describe(m));
1337 }
1338
1339 // Tests that Ge() matches a 2-tuple where the first field >= the
1340 // second field.
1341 TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) {
1342 Matcher<const Tuple2&> m = Ge();
1343 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1344 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1345 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1346 }
1347
1348 // Tests that Ge() describes itself properly.
1349 TEST(Ge2Test, CanDescribeSelf) {
1350 Matcher<const Tuple2&> m = Ge();
1351 EXPECT_EQ("argument #0 is greater than or equal to argument #1",
1352 Describe(m));
1353 }
1354
1355 // Tests that Gt() matches a 2-tuple where the first field > the
1356 // second field.
1357 TEST(Gt2Test, MatchesGreaterThanArguments) {
1358 Matcher<const Tuple2&> m = Gt();
1359 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1360 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1361 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1362 }
1363
1364 // Tests that Gt() describes itself properly.
1365 TEST(Gt2Test, CanDescribeSelf) {
1366 Matcher<const Tuple2&> m = Gt();
1367 EXPECT_EQ("argument #0 is greater than argument #1", Describe(m));
1368 }
1369
1370 // Tests that Le() matches a 2-tuple where the first field <= the
1371 // second field.
1372 TEST(Le2Test, MatchesLessThanOrEqualArguments) {
1373 Matcher<const Tuple2&> m = Le();
1374 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1375 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1376 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1377 }
1378
1379 // Tests that Le() describes itself properly.
1380 TEST(Le2Test, CanDescribeSelf) {
1381 Matcher<const Tuple2&> m = Le();
1382 EXPECT_EQ("argument #0 is less than or equal to argument #1",
1383 Describe(m));
1384 }
1385
1386 // Tests that Lt() matches a 2-tuple where the first field < the
1387 // second field.
1388 TEST(Lt2Test, MatchesLessThanArguments) {
1389 Matcher<const Tuple2&> m = Lt();
1390 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1391 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1392 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1393 }
1394
1395 // Tests that Lt() describes itself properly.
1396 TEST(Lt2Test, CanDescribeSelf) {
1397 Matcher<const Tuple2&> m = Lt();
1398 EXPECT_EQ("argument #0 is less than argument #1", Describe(m));
1399 }
1400
1401 // Tests that Ne() matches a 2-tuple where the first field != the
1402 // second field.
1403 TEST(Ne2Test, MatchesUnequalArguments) {
1404 Matcher<const Tuple2&> m = Ne();
1405 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1406 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1407 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1408 }
1409
1410 // Tests that Ne() describes itself properly.
1411 TEST(Ne2Test, CanDescribeSelf) {
1412 Matcher<const Tuple2&> m = Ne();
1413 EXPECT_EQ("argument #0 is not equal to argument #1", Describe(m));
1414 }
1415
1416 // Tests that Not(m) matches any value that doesn't match m.
1417 TEST(NotTest, NegatesMatcher) {
1418 Matcher<int> m;
1419 m = Not(Eq(2));
1420 EXPECT_TRUE(m.Matches(3));
1421 EXPECT_FALSE(m.Matches(2));
1422 }
1423
1424 // Tests that Not(m) describes itself properly.
1425 TEST(NotTest, CanDescribeSelf) {
1426 Matcher<int> m = Not(Eq(5));
1427 EXPECT_EQ("is not equal to 5", Describe(m));
1428 }
1429
1430 // Tests that monomorphic matchers are safely cast by the Not matcher.
1431 TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {
1432 // greater_than_5 is a monomorphic matcher.
1433 Matcher<int> greater_than_5 = Gt(5);
1434
1435 Matcher<const int&> m = Not(greater_than_5);
1436 Matcher<int&> m2 = Not(greater_than_5);
1437 Matcher<int&> m3 = Not(m);
1438 }
1439
1440 // Tests that AllOf(m1, ..., mn) matches any value that matches all of
1441 // the given matchers.
1442 TEST(AllOfTest, MatchesWhenAllMatch) {
1443 Matcher<int> m;
1444 m = AllOf(Le(2), Ge(1));
1445 EXPECT_TRUE(m.Matches(1));
1446 EXPECT_TRUE(m.Matches(2));
1447 EXPECT_FALSE(m.Matches(0));
1448 EXPECT_FALSE(m.Matches(3));
1449
1450 m = AllOf(Gt(0), Ne(1), Ne(2));
1451 EXPECT_TRUE(m.Matches(3));
1452 EXPECT_FALSE(m.Matches(2));
1453 EXPECT_FALSE(m.Matches(1));
1454 EXPECT_FALSE(m.Matches(0));
1455
1456 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
1457 EXPECT_TRUE(m.Matches(4));
1458 EXPECT_FALSE(m.Matches(3));
1459 EXPECT_FALSE(m.Matches(2));
1460 EXPECT_FALSE(m.Matches(1));
1461 EXPECT_FALSE(m.Matches(0));
1462
1463 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
1464 EXPECT_TRUE(m.Matches(0));
1465 EXPECT_TRUE(m.Matches(1));
1466 EXPECT_FALSE(m.Matches(3));
1467 }
1468
1469 // Tests that AllOf(m1, ..., mn) describes itself properly.
1470 TEST(AllOfTest, CanDescribeSelf) {
1471 Matcher<int> m;
1472 m = AllOf(Le(2), Ge(1));
1473 EXPECT_EQ("(is less than or equal to 2) and "
1474 "(is greater than or equal to 1)",
1475 Describe(m));
1476
1477 m = AllOf(Gt(0), Ne(1), Ne(2));
1478 EXPECT_EQ("(is greater than 0) and "
1479 "((is not equal to 1) and "
1480 "(is not equal to 2))",
1481 Describe(m));
1482
1483
1484 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
1485 EXPECT_EQ("(is greater than 0) and "
1486 "((is not equal to 1) and "
1487 "((is not equal to 2) and "
1488 "(is not equal to 3)))",
1489 Describe(m));
1490
1491
1492 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
1493 EXPECT_EQ("(is greater than or equal to 0) and "
1494 "((is less than 10) and "
1495 "((is not equal to 3) and "
1496 "((is not equal to 5) and "
1497 "(is not equal to 7))))", Describe(m));
1498 }
1499
1500 // Tests that monomorphic matchers are safely cast by the AllOf matcher.
1501 TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
1502 // greater_than_5 and less_than_10 are monomorphic matchers.
1503 Matcher<int> greater_than_5 = Gt(5);
1504 Matcher<int> less_than_10 = Lt(10);
1505
1506 Matcher<const int&> m = AllOf(greater_than_5, less_than_10);
1507 Matcher<int&> m2 = AllOf(greater_than_5, less_than_10);
1508 Matcher<int&> m3 = AllOf(greater_than_5, m2);
1509
1510 // Tests that BothOf works when composing itself.
1511 Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10);
1512 Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);
1513 }
1514
1515 // Tests that AnyOf(m1, ..., mn) matches any value that matches at
1516 // least one of the given matchers.
1517 TEST(AnyOfTest, MatchesWhenAnyMatches) {
1518 Matcher<int> m;
1519 m = AnyOf(Le(1), Ge(3));
1520 EXPECT_TRUE(m.Matches(1));
1521 EXPECT_TRUE(m.Matches(4));
1522 EXPECT_FALSE(m.Matches(2));
1523
1524 m = AnyOf(Lt(0), Eq(1), Eq(2));
1525 EXPECT_TRUE(m.Matches(-1));
1526 EXPECT_TRUE(m.Matches(1));
1527 EXPECT_TRUE(m.Matches(2));
1528 EXPECT_FALSE(m.Matches(0));
1529
1530 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
1531 EXPECT_TRUE(m.Matches(-1));
1532 EXPECT_TRUE(m.Matches(1));
1533 EXPECT_TRUE(m.Matches(2));
1534 EXPECT_TRUE(m.Matches(3));
1535 EXPECT_FALSE(m.Matches(0));
1536
1537 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
1538 EXPECT_TRUE(m.Matches(0));
1539 EXPECT_TRUE(m.Matches(11));
1540 EXPECT_TRUE(m.Matches(3));
1541 EXPECT_FALSE(m.Matches(2));
1542 }
1543
1544 // Tests that AnyOf(m1, ..., mn) describes itself properly.
1545 TEST(AnyOfTest, CanDescribeSelf) {
1546 Matcher<int> m;
1547 m = AnyOf(Le(1), Ge(3));
1548 EXPECT_EQ("(is less than or equal to 1) or "
1549 "(is greater than or equal to 3)",
1550 Describe(m));
1551
1552 m = AnyOf(Lt(0), Eq(1), Eq(2));
1553 EXPECT_EQ("(is less than 0) or "
1554 "((is equal to 1) or (is equal to 2))",
1555 Describe(m));
1556
1557 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
1558 EXPECT_EQ("(is less than 0) or "
1559 "((is equal to 1) or "
1560 "((is equal to 2) or "
1561 "(is equal to 3)))",
1562 Describe(m));
1563
1564 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
1565 EXPECT_EQ("(is less than or equal to 0) or "
1566 "((is greater than 10) or "
1567 "((is equal to 3) or "
1568 "((is equal to 5) or "
1569 "(is equal to 7))))",
1570 Describe(m));
1571 }
1572
1573 // Tests that monomorphic matchers are safely cast by the AnyOf matcher.
1574 TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
1575 // greater_than_5 and less_than_10 are monomorphic matchers.
1576 Matcher<int> greater_than_5 = Gt(5);
1577 Matcher<int> less_than_10 = Lt(10);
1578
1579 Matcher<const int&> m = AnyOf(greater_than_5, less_than_10);
1580 Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10);
1581 Matcher<int&> m3 = AnyOf(greater_than_5, m2);
1582
1583 // Tests that EitherOf works when composing itself.
1584 Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10);
1585 Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);
1586 }
1587
1588 // The following predicate function and predicate functor are for
1589 // testing the Truly(predicate) matcher.
1590
1591 // Returns non-zero if the input is positive. Note that the return
1592 // type of this function is not bool. It's OK as Truly() accepts any
1593 // unary function or functor whose return type can be implicitly
1594 // converted to bool.
1595 int IsPositive(double x) {
1596 return x > 0 ? 1 : 0;
1597 }
1598
1599 // This functor returns true if the input is greater than the given
1600 // number.
1601 class IsGreaterThan {
1602 public:
1603 explicit IsGreaterThan(int threshold) : threshold_(threshold) {}
1604
1605 bool operator()(int n) const { return n > threshold_; }
1606 private:
1607 const int threshold_;
1608 };
1609
1610 // For testing Truly().
1611 const int foo = 0;
1612
1613 // This predicate returns true iff the argument references foo and has
1614 // a zero value.
1615 bool ReferencesFooAndIsZero(const int& n) {
1616 return (&n == &foo) && (n == 0);
1617 }
1618
1619 // Tests that Truly(predicate) matches what satisfies the given
1620 // predicate.
1621 TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {
1622 Matcher<double> m = Truly(IsPositive);
1623 EXPECT_TRUE(m.Matches(2.0));
1624 EXPECT_FALSE(m.Matches(-1.5));
1625 }
1626
1627 // Tests that Truly(predicate_functor) works too.
1628 TEST(TrulyTest, CanBeUsedWithFunctor) {
1629 Matcher<int> m = Truly(IsGreaterThan(5));
1630 EXPECT_TRUE(m.Matches(6));
1631 EXPECT_FALSE(m.Matches(4));
1632 }
1633
1634 // Tests that Truly(predicate) can describe itself properly.
1635 TEST(TrulyTest, CanDescribeSelf) {
1636 Matcher<double> m = Truly(IsPositive);
1637 EXPECT_EQ("satisfies the given predicate",
1638 Describe(m));
1639 }
1640
1641 // Tests that Truly(predicate) works when the matcher takes its
1642 // argument by reference.
1643 TEST(TrulyTest, WorksForByRefArguments) {
1644 Matcher<const int&> m = Truly(ReferencesFooAndIsZero);
1645 EXPECT_TRUE(m.Matches(foo));
1646 int n = 0;
1647 EXPECT_FALSE(m.Matches(n));
1648 }
1649
1650 // Tests that Matches(m) is a predicate satisfied by whatever that
1651 // matches matcher m.
1652 TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {
1653 EXPECT_TRUE(Matches(Ge(0))(1));
1654 EXPECT_FALSE(Matches(Eq('a'))('b'));
1655 }
1656
1657 // Tests that Matches(m) works when the matcher takes its argument by
1658 // reference.
1659 TEST(MatchesTest, WorksOnByRefArguments) {
1660 int m = 0, n = 0;
1661 EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n));
1662 EXPECT_FALSE(Matches(Ref(m))(n));
1663 }
1664
1665 // Tests that a Matcher on non-reference type can be used in
1666 // Matches().
1667 TEST(MatchesTest, WorksWithMatcherOnNonRefType) {
1668 Matcher<int> eq5 = Eq(5);
1669 EXPECT_TRUE(Matches(eq5)(5));
1670 EXPECT_FALSE(Matches(eq5)(2));
1671 }
1672
1673 // Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
1674 // matches the matcher.
1675 TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {
1676 ASSERT_THAT(5, Ge(2)) << "This should succeed.";
1677 ASSERT_THAT("Foo", EndsWith("oo"));
1678 EXPECT_THAT(2, AllOf(Le(7), Ge(0))) << "This should succeed too.";
1679 EXPECT_THAT("Hello", StartsWith("Hell"));
1680 }
1681
1682 // Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
1683 // doesn't match the matcher.
1684 TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {
1685 // 'n' must be static as it is used in an EXPECT_FATAL_FAILURE(),
1686 // which cannot reference auto variables.
1687 static int n;
1688 n = 5;
1689 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Gt(10)) << "This should fail.",
1690 "Value of: n\n"
1691 "Expected: is greater than 10\n"
1692 " Actual: 5\n"
1693 "This should fail.");
1694 n = 0;
1695 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(n, AllOf(Le(7), Ge(5))),
1696 "Value of: n\n"
1697 "Expected: (is less than or equal to 7) and "
1698 "(is greater than or equal to 5)\n"
1699 " Actual: 0");
1700 }
1701
1702 // Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument
1703 // has a reference type.
1704 TEST(MatcherAssertionTest, WorksForByRefArguments) {
1705 // We use a static variable here as EXPECT_FATAL_FAILURE() cannot
1706 // reference auto variables.
1707 static int n;
1708 n = 0;
1709 EXPECT_THAT(n, AllOf(Le(7), Ref(n)));
1710 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Not(Ref(n))),
1711 "Value of: n\n"
1712 "Expected: does not reference the variable @");
1713 // Tests the "Actual" part.
1714 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Not(Ref(n))),
1715 "Actual: 0 (is located @");
1716 }
1717
1718 // Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is
1719 // monomorphic.
1720 TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
1721 Matcher<const char*> starts_with_he = StartsWith("he");
1722 ASSERT_THAT("hello", starts_with_he);
1723
1724 Matcher<const string&> ends_with_ok = EndsWith("ok");
1725 ASSERT_THAT("book", ends_with_ok);
1726
1727 Matcher<int> is_greater_than_5 = Gt(5);
1728 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5),
1729 "Value of: 5\n"
1730 "Expected: is greater than 5\n"
1731 " Actual: 5");
1732 }
1733
1734 // Tests floating-point matchers.
1735 template <typename RawType>
1736 class FloatingPointTest : public testing::Test {
1737 protected:
1738 typedef typename testing::internal::FloatingPoint<RawType> Floating;
1739 typedef typename Floating::Bits Bits;
1740
1741 virtual void SetUp() {
1742 const size_t max_ulps = Floating::kMaxUlps;
1743
1744 // The bits that represent 0.0.
1745 const Bits zero_bits = Floating(0).bits();
1746
1747 // Makes some numbers close to 0.0.
1748 close_to_positive_zero_ = Floating::ReinterpretBits(zero_bits + max_ulps/2);
1749 close_to_negative_zero_ = -Floating::ReinterpretBits(
1750 zero_bits + max_ulps - max_ulps/2);
1751 further_from_negative_zero_ = -Floating::ReinterpretBits(
1752 zero_bits + max_ulps + 1 - max_ulps/2);
1753
1754 // The bits that represent 1.0.
1755 const Bits one_bits = Floating(1).bits();
1756
1757 // Makes some numbers close to 1.0.
1758 close_to_one_ = Floating::ReinterpretBits(one_bits + max_ulps);
1759 further_from_one_ = Floating::ReinterpretBits(one_bits + max_ulps + 1);
1760
1761 // +infinity.
1762 infinity_ = Floating::Infinity();
1763
1764 // The bits that represent +infinity.
1765 const Bits infinity_bits = Floating(infinity_).bits();
1766
1767 // Makes some numbers close to infinity.
1768 close_to_infinity_ = Floating::ReinterpretBits(infinity_bits - max_ulps);
1769 further_from_infinity_ = Floating::ReinterpretBits(
1770 infinity_bits - max_ulps - 1);
1771
1772 // Makes some NAN's.
1773 nan1_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 1);
1774 nan2_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 200);
1775 }
1776
1777 void TestSize() {
1778 EXPECT_EQ(sizeof(RawType), sizeof(Bits));
1779 }
1780
1781 // A battery of tests for FloatingEqMatcher::Matches.
1782 // matcher_maker is a pointer to a function which creates a FloatingEqMatcher.
1783 void TestMatches(
1784 testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) {
1785 Matcher<RawType> m1 = matcher_maker(0.0);
1786 EXPECT_TRUE(m1.Matches(-0.0));
1787 EXPECT_TRUE(m1.Matches(close_to_positive_zero_));
1788 EXPECT_TRUE(m1.Matches(close_to_negative_zero_));
1789 EXPECT_FALSE(m1.Matches(1.0));
1790
1791 Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_);
1792 EXPECT_FALSE(m2.Matches(further_from_negative_zero_));
1793
1794 Matcher<RawType> m3 = matcher_maker(1.0);
1795 EXPECT_TRUE(m3.Matches(close_to_one_));
1796 EXPECT_FALSE(m3.Matches(further_from_one_));
1797
1798 // Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above.
1799 EXPECT_FALSE(m3.Matches(0.0));
1800
1801 Matcher<RawType> m4 = matcher_maker(-infinity_);
1802 EXPECT_TRUE(m4.Matches(-close_to_infinity_));
1803
1804 Matcher<RawType> m5 = matcher_maker(infinity_);
1805 EXPECT_TRUE(m5.Matches(close_to_infinity_));
1806
1807 // This is interesting as the representations of infinity_ and nan1_
1808 // are only 1 DLP apart.
1809 EXPECT_FALSE(m5.Matches(nan1_));
1810
1811 // matcher_maker can produce a Matcher<const RawType&>, which is needed in
1812 // some cases.
1813 Matcher<const RawType&> m6 = matcher_maker(0.0);
1814 EXPECT_TRUE(m6.Matches(-0.0));
1815 EXPECT_TRUE(m6.Matches(close_to_positive_zero_));
1816 EXPECT_FALSE(m6.Matches(1.0));
1817
1818 // matcher_maker can produce a Matcher<RawType&>, which is needed in some
1819 // cases.
1820 Matcher<RawType&> m7 = matcher_maker(0.0);
1821 RawType x = 0.0;
1822 EXPECT_TRUE(m7.Matches(x));
1823 x = 0.01f;
1824 EXPECT_FALSE(m7.Matches(x));
1825 }
1826
1827 // Pre-calculated numbers to be used by the tests.
1828
1829 static RawType close_to_positive_zero_;
1830 static RawType close_to_negative_zero_;
1831 static RawType further_from_negative_zero_;
1832
1833 static RawType close_to_one_;
1834 static RawType further_from_one_;
1835
1836 static RawType infinity_;
1837 static RawType close_to_infinity_;
1838 static RawType further_from_infinity_;
1839
1840 static RawType nan1_;
1841 static RawType nan2_;
1842 };
1843
1844 template <typename RawType>
1845 RawType FloatingPointTest<RawType>::close_to_positive_zero_;
1846
1847 template <typename RawType>
1848 RawType FloatingPointTest<RawType>::close_to_negative_zero_;
1849
1850 template <typename RawType>
1851 RawType FloatingPointTest<RawType>::further_from_negative_zero_;
1852
1853 template <typename RawType>
1854 RawType FloatingPointTest<RawType>::close_to_one_;
1855
1856 template <typename RawType>
1857 RawType FloatingPointTest<RawType>::further_from_one_;
1858
1859 template <typename RawType>
1860 RawType FloatingPointTest<RawType>::infinity_;
1861
1862 template <typename RawType>
1863 RawType FloatingPointTest<RawType>::close_to_infinity_;
1864
1865 template <typename RawType>
1866 RawType FloatingPointTest<RawType>::further_from_infinity_;
1867
1868 template <typename RawType>
1869 RawType FloatingPointTest<RawType>::nan1_;
1870
1871 template <typename RawType>
1872 RawType FloatingPointTest<RawType>::nan2_;
1873
1874 // Instantiate FloatingPointTest for testing floats.
1875 typedef FloatingPointTest<float> FloatTest;
1876
1877 TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) {
1878 TestMatches(&FloatEq);
1879 }
1880
1881 TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
1882 TestMatches(&NanSensitiveFloatEq);
1883 }
1884
1885 TEST_F(FloatTest, FloatEqCannotMatchNaN) {
1886 // FloatEq never matches NaN.
1887 Matcher<float> m = FloatEq(nan1_);
1888 EXPECT_FALSE(m.Matches(nan1_));
1889 EXPECT_FALSE(m.Matches(nan2_));
1890 EXPECT_FALSE(m.Matches(1.0));
1891 }
1892
1893 TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {
1894 // NanSensitiveFloatEq will match NaN.
1895 Matcher<float> m = NanSensitiveFloatEq(nan1_);
1896 EXPECT_TRUE(m.Matches(nan1_));
1897 EXPECT_TRUE(m.Matches(nan2_));
1898 EXPECT_FALSE(m.Matches(1.0));
1899 }
1900
1901 TEST_F(FloatTest, FloatEqCanDescribeSelf) {
1902 Matcher<float> m1 = FloatEq(2.0f);
1903 EXPECT_EQ("is approximately 2", Describe(m1));
1904 EXPECT_EQ("is not approximately 2", DescribeNegation(m1));
1905
1906 Matcher<float> m2 = FloatEq(0.5f);
1907 EXPECT_EQ("is approximately 0.5", Describe(m2));
1908 EXPECT_EQ("is not approximately 0.5", DescribeNegation(m2));
1909
1910 Matcher<float> m3 = FloatEq(nan1_);
1911 EXPECT_EQ("never matches", Describe(m3));
1912 EXPECT_EQ("is anything", DescribeNegation(m3));
1913 }
1914
1915 TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {
1916 Matcher<float> m1 = NanSensitiveFloatEq(2.0f);
1917 EXPECT_EQ("is approximately 2", Describe(m1));
1918 EXPECT_EQ("is not approximately 2", DescribeNegation(m1));
1919
1920 Matcher<float> m2 = NanSensitiveFloatEq(0.5f);
1921 EXPECT_EQ("is approximately 0.5", Describe(m2));
1922 EXPECT_EQ("is not approximately 0.5", DescribeNegation(m2));
1923
1924 Matcher<float> m3 = NanSensitiveFloatEq(nan1_);
1925 EXPECT_EQ("is NaN", Describe(m3));
1926 EXPECT_EQ("is not NaN", DescribeNegation(m3));
1927 }
1928
1929 // Instantiate FloatingPointTest for testing doubles.
1930 typedef FloatingPointTest<double> DoubleTest;
1931
1932 TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
1933 TestMatches(&DoubleEq);
1934 }
1935
1936 TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
1937 TestMatches(&NanSensitiveDoubleEq);
1938 }
1939
1940 TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {
1941 // DoubleEq never matches NaN.
1942 Matcher<double> m = DoubleEq(nan1_);
1943 EXPECT_FALSE(m.Matches(nan1_));
1944 EXPECT_FALSE(m.Matches(nan2_));
1945 EXPECT_FALSE(m.Matches(1.0));
1946 }
1947
1948 TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {
1949 // NanSensitiveDoubleEq will match NaN.
1950 Matcher<double> m = NanSensitiveDoubleEq(nan1_);
1951 EXPECT_TRUE(m.Matches(nan1_));
1952 EXPECT_TRUE(m.Matches(nan2_));
1953 EXPECT_FALSE(m.Matches(1.0));
1954 }
1955
1956 TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {
1957 Matcher<double> m1 = DoubleEq(2.0);
1958 EXPECT_EQ("is approximately 2", Describe(m1));
1959 EXPECT_EQ("is not approximately 2", DescribeNegation(m1));
1960
1961 Matcher<double> m2 = DoubleEq(0.5);
1962 EXPECT_EQ("is approximately 0.5", Describe(m2));
1963 EXPECT_EQ("is not approximately 0.5", DescribeNegation(m2));
1964
1965 Matcher<double> m3 = DoubleEq(nan1_);
1966 EXPECT_EQ("never matches", Describe(m3));
1967 EXPECT_EQ("is anything", DescribeNegation(m3));
1968 }
1969
1970 TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {
1971 Matcher<double> m1 = NanSensitiveDoubleEq(2.0);
1972 EXPECT_EQ("is approximately 2", Describe(m1));
1973 EXPECT_EQ("is not approximately 2", DescribeNegation(m1));
1974
1975 Matcher<double> m2 = NanSensitiveDoubleEq(0.5);
1976 EXPECT_EQ("is approximately 0.5", Describe(m2));
1977 EXPECT_EQ("is not approximately 0.5", DescribeNegation(m2));
1978
1979 Matcher<double> m3 = NanSensitiveDoubleEq(nan1_);
1980 EXPECT_EQ("is NaN", Describe(m3));
1981 EXPECT_EQ("is not NaN", DescribeNegation(m3));
1982 }
1983
1984 TEST(PointeeTest, RawPointer) {
1985 const Matcher<int*> m = Pointee(Ge(0));
1986
1987 int n = 1;
1988 EXPECT_TRUE(m.Matches(&n));
1989 n = -1;
1990 EXPECT_FALSE(m.Matches(&n));
1991 EXPECT_FALSE(m.Matches(NULL));
1992 }
1993
1994 TEST(PointeeTest, RawPointerToConst) {
1995 const Matcher<const double*> m = Pointee(Ge(0));
1996
1997 double x = 1;
1998 EXPECT_TRUE(m.Matches(&x));
1999 x = -1;
2000 EXPECT_FALSE(m.Matches(&x));
2001 EXPECT_FALSE(m.Matches(NULL));
2002 }
2003
2004 TEST(PointeeTest, ReferenceToConstRawPointer) {
2005 const Matcher<int* const &> m = Pointee(Ge(0));
2006
2007 int n = 1;
2008 EXPECT_TRUE(m.Matches(&n));
2009 n = -1;
2010 EXPECT_FALSE(m.Matches(&n));
2011 EXPECT_FALSE(m.Matches(NULL));
2012 }
2013
2014 TEST(PointeeTest, ReferenceToNonConstRawPointer) {
2015 const Matcher<double* &> m = Pointee(Ge(0));
2016
2017 double x = 1.0;
2018 double* p = &x;
2019 EXPECT_TRUE(m.Matches(p));
2020 x = -1;
2021 EXPECT_FALSE(m.Matches(p));
2022 p = NULL;
2023 EXPECT_FALSE(m.Matches(p));
2024 }
2025
2026 TEST(PointeeTest, NeverMatchesNull) {
2027 const Matcher<const char*> m = Pointee(_);
2028 EXPECT_FALSE(m.Matches(NULL));
2029 }
2030
2031 // Tests that we can write Pointee(value) instead of Pointee(Eq(value)).
2032 TEST(PointeeTest, MatchesAgainstAValue) {
2033 const Matcher<int*> m = Pointee(5);
2034
2035 int n = 5;
2036 EXPECT_TRUE(m.Matches(&n));
2037 n = -1;
2038 EXPECT_FALSE(m.Matches(&n));
2039 EXPECT_FALSE(m.Matches(NULL));
2040 }
2041
2042 TEST(PointeeTest, CanDescribeSelf) {
2043 const Matcher<int*> m = Pointee(Gt(3));
2044 EXPECT_EQ("points to a value that is greater than 3", Describe(m));
2045 EXPECT_EQ("does not point to a value that is greater than 3",
2046 DescribeNegation(m));
2047 }
2048
2049 // For testing ExplainMatchResultTo().
2050 class GreaterThanMatcher : public MatcherInterface<int> {
2051 public:
2052 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
2053
2054 virtual bool Matches(int lhs) const { return lhs > rhs_; }
2055
2056 virtual void DescribeTo(::std::ostream* os) const {
2057 *os << "is greater than " << rhs_;
2058 }
2059
2060 virtual void ExplainMatchResultTo(int lhs, ::std::ostream* os) const {
2061 const int diff = lhs - rhs_;
2062 if (diff > 0) {
2063 *os << "is " << diff << " more than " << rhs_;
2064 } else if (diff == 0) {
2065 *os << "is the same as " << rhs_;
2066 } else {
2067 *os << "is " << -diff << " less than " << rhs_;
2068 }
2069 }
2070 private:
2071 const int rhs_;
2072 };
2073
2074 Matcher<int> GreaterThan(int n) {
2075 return MakeMatcher(new GreaterThanMatcher(n));
2076 }
2077
2078 TEST(PointeeTest, CanExplainMatchResult) {
2079 const Matcher<const string*> m = Pointee(StartsWith("Hi"));
2080
2081 EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL)));
2082
2083 const Matcher<int*> m2 = Pointee(GreaterThan(1));
2084 int n = 3;
2085 EXPECT_EQ("points to a value that is 2 more than 1", Explain(m2, &n));
2086 }
2087
2088 // An uncopyable class.
2089 class Uncopyable {
2090 public:
2091 explicit Uncopyable(int value) : value_(value) {}
2092
2093 int value() const { return value_; }
2094 private:
2095 const int value_;
2096 GTEST_DISALLOW_COPY_AND_ASSIGN_(Uncopyable);
2097 };
2098
2099 // Returns true iff x.value() is positive.
2100 bool ValueIsPositive(const Uncopyable& x) { return x.value() > 0; }
2101
2102 // A user-defined struct for testing Field().
2103 struct AStruct {
2104 AStruct() : x(0), y(1.0), z(5), p(NULL) {}
2105 AStruct(const AStruct& rhs)
2106 : x(rhs.x), y(rhs.y), z(rhs.z.value()), p(rhs.p) {}
2107
2108 int x; // A non-const field.
2109 const double y; // A const field.
2110 Uncopyable z; // An uncopyable field.
2111 const char* p; // A pointer field.
2112 };
2113
2114 // A derived struct for testing Field().
2115 struct DerivedStruct : public AStruct {
2116 char ch;
2117 };
2118
2119 // Tests that Field(&Foo::field, ...) works when field is non-const.
2120 TEST(FieldTest, WorksForNonConstField) {
2121 Matcher<AStruct> m = Field(&AStruct::x, Ge(0));
2122
2123 AStruct a;
2124 EXPECT_TRUE(m.Matches(a));
2125 a.x = -1;
2126 EXPECT_FALSE(m.Matches(a));
2127 }
2128
2129 // Tests that Field(&Foo::field, ...) works when field is const.
2130 TEST(FieldTest, WorksForConstField) {
2131 AStruct a;
2132
2133 Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0));
2134 EXPECT_TRUE(m.Matches(a));
2135 m = Field(&AStruct::y, Le(0.0));
2136 EXPECT_FALSE(m.Matches(a));
2137 }
2138
2139 // Tests that Field(&Foo::field, ...) works when field is not copyable.
2140 TEST(FieldTest, WorksForUncopyableField) {
2141 AStruct a;
2142
2143 Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive));
2144 EXPECT_TRUE(m.Matches(a));
2145 m = Field(&AStruct::z, Not(Truly(ValueIsPositive)));
2146 EXPECT_FALSE(m.Matches(a));
2147 }
2148
2149 // Tests that Field(&Foo::field, ...) works when field is a pointer.
2150 TEST(FieldTest, WorksForPointerField) {
2151 // Matching against NULL.
2152 Matcher<AStruct> m = Field(&AStruct::p, static_cast<const char*>(NULL));
2153 AStruct a;
2154 EXPECT_TRUE(m.Matches(a));
2155 a.p = "hi";
2156 EXPECT_FALSE(m.Matches(a));
2157
2158 // Matching a pointer that is not NULL.
2159 m = Field(&AStruct::p, StartsWith("hi"));
2160 a.p = "hill";
2161 EXPECT_TRUE(m.Matches(a));
2162 a.p = "hole";
2163 EXPECT_FALSE(m.Matches(a));
2164 }
2165
2166 // Tests that Field() works when the object is passed by reference.
2167 TEST(FieldTest, WorksForByRefArgument) {
2168 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2169
2170 AStruct a;
2171 EXPECT_TRUE(m.Matches(a));
2172 a.x = -1;
2173 EXPECT_FALSE(m.Matches(a));
2174 }
2175
2176 // Tests that Field(&Foo::field, ...) works when the argument's type
2177 // is a sub-type of Foo.
2178 TEST(FieldTest, WorksForArgumentOfSubType) {
2179 // Note that the matcher expects DerivedStruct but we say AStruct
2180 // inside Field().
2181 Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0));
2182
2183 DerivedStruct d;
2184 EXPECT_TRUE(m.Matches(d));
2185 d.x = -1;
2186 EXPECT_FALSE(m.Matches(d));
2187 }
2188
2189 // Tests that Field(&Foo::field, m) works when field's type and m's
2190 // argument type are compatible but not the same.
2191 TEST(FieldTest, WorksForCompatibleMatcherType) {
2192 // The field is an int, but the inner matcher expects a signed char.
2193 Matcher<const AStruct&> m = Field(&AStruct::x,
2194 Matcher<signed char>(Ge(0)));
2195
2196 AStruct a;
2197 EXPECT_TRUE(m.Matches(a));
2198 a.x = -1;
2199 EXPECT_FALSE(m.Matches(a));
2200 }
2201
2202 // Tests that Field() can describe itself.
2203 TEST(FieldTest, CanDescribeSelf) {
2204 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2205
2206 EXPECT_EQ("the given field is greater than or equal to 0", Describe(m));
2207 EXPECT_EQ("the given field is not greater than or equal to 0",
2208 DescribeNegation(m));
2209 }
2210
2211 // Tests that Field() can explain the match result.
2212 TEST(FieldTest, CanExplainMatchResult) {
2213 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2214
2215 AStruct a;
2216 a.x = 1;
2217 EXPECT_EQ("", Explain(m, a));
2218
2219 m = Field(&AStruct::x, GreaterThan(0));
2220 EXPECT_EQ("the given field is 1 more than 0", Explain(m, a));
2221 }
2222
2223 // Tests that Field() works when the argument is a pointer to const.
2224 TEST(FieldForPointerTest, WorksForPointerToConst) {
2225 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2226
2227 AStruct a;
2228 EXPECT_TRUE(m.Matches(&a));
2229 a.x = -1;
2230 EXPECT_FALSE(m.Matches(&a));
2231 }
2232
2233 // Tests that Field() works when the argument is a pointer to non-const.
2234 TEST(FieldForPointerTest, WorksForPointerToNonConst) {
2235 Matcher<AStruct*> m = Field(&AStruct::x, Ge(0));
2236
2237 AStruct a;
2238 EXPECT_TRUE(m.Matches(&a));
2239 a.x = -1;
2240 EXPECT_FALSE(m.Matches(&a));
2241 }
2242
2243 // Tests that Field() does not match the NULL pointer.
2244 TEST(FieldForPointerTest, DoesNotMatchNull) {
2245 Matcher<const AStruct*> m = Field(&AStruct::x, _);
2246 EXPECT_FALSE(m.Matches(NULL));
2247 }
2248
2249 // Tests that Field(&Foo::field, ...) works when the argument's type
2250 // is a sub-type of const Foo*.
2251 TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
2252 // Note that the matcher expects DerivedStruct but we say AStruct
2253 // inside Field().
2254 Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0));
2255
2256 DerivedStruct d;
2257 EXPECT_TRUE(m.Matches(&d));
2258 d.x = -1;
2259 EXPECT_FALSE(m.Matches(&d));
2260 }
2261
2262 // Tests that Field() can describe itself when used to match a pointer.
2263 TEST(FieldForPointerTest, CanDescribeSelf) {
2264 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2265
2266 EXPECT_EQ("the given field is greater than or equal to 0", Describe(m));
2267 EXPECT_EQ("the given field is not greater than or equal to 0",
2268 DescribeNegation(m));
2269 }
2270
2271 // Tests that Field() can explain the result of matching a pointer.
2272 TEST(FieldForPointerTest, CanExplainMatchResult) {
2273 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2274
2275 AStruct a;
2276 a.x = 1;
2277 EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(NULL)));
2278 EXPECT_EQ("", Explain(m, &a));
2279
2280 m = Field(&AStruct::x, GreaterThan(0));
2281 EXPECT_EQ("the given field is 1 more than 0", Explain(m, &a));
2282 }
2283
2284 // A user-defined class for testing Property().
2285 class AClass {
2286 public:
2287 AClass() : n_(0) {}
2288
2289 // A getter that returns a non-reference.
2290 int n() const { return n_; }
2291
2292 void set_n(int new_n) { n_ = new_n; }
2293
2294 // A getter that returns a reference to const.
2295 const string& s() const { return s_; }
2296
2297 void set_s(const string& new_s) { s_ = new_s; }
2298
2299 // A getter that returns a reference to non-const.
2300 double& x() const { return x_; }
2301 private:
2302 int n_;
2303 string s_;
2304
2305 static double x_;
2306 };
2307
2308 double AClass::x_ = 0.0;
2309
2310 // A derived class for testing Property().
2311 class DerivedClass : public AClass {
2312 private:
2313 int k_;
2314 };
2315
2316 // Tests that Property(&Foo::property, ...) works when property()
2317 // returns a non-reference.
2318 TEST(PropertyTest, WorksForNonReferenceProperty) {
2319 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
2320
2321 AClass a;
2322 a.set_n(1);
2323 EXPECT_TRUE(m.Matches(a));
2324
2325 a.set_n(-1);
2326 EXPECT_FALSE(m.Matches(a));
2327 }
2328
2329 // Tests that Property(&Foo::property, ...) works when property()
2330 // returns a reference to const.
2331 TEST(PropertyTest, WorksForReferenceToConstProperty) {
2332 Matcher<const AClass&> m = Property(&AClass::s, StartsWith("hi"));
2333
2334 AClass a;
2335 a.set_s("hill");
2336 EXPECT_TRUE(m.Matches(a));
2337
2338 a.set_s("hole");
2339 EXPECT_FALSE(m.Matches(a));
2340 }
2341
2342 // Tests that Property(&Foo::property, ...) works when property()
2343 // returns a reference to non-const.
2344 TEST(PropertyTest, WorksForReferenceToNonConstProperty) {
2345 double x = 0.0;
2346 AClass a;
2347
2348 Matcher<const AClass&> m = Property(&AClass::x, Ref(x));
2349 EXPECT_FALSE(m.Matches(a));
2350
2351 m = Property(&AClass::x, Not(Ref(x)));
2352 EXPECT_TRUE(m.Matches(a));
2353 }
2354
2355 // Tests that Property(&Foo::property, ...) works when the argument is
2356 // passed by value.
2357 TEST(PropertyTest, WorksForByValueArgument) {
2358 Matcher<AClass> m = Property(&AClass::s, StartsWith("hi"));
2359
2360 AClass a;
2361 a.set_s("hill");
2362 EXPECT_TRUE(m.Matches(a));
2363
2364 a.set_s("hole");
2365 EXPECT_FALSE(m.Matches(a));
2366 }
2367
2368 // Tests that Property(&Foo::property, ...) works when the argument's
2369 // type is a sub-type of Foo.
2370 TEST(PropertyTest, WorksForArgumentOfSubType) {
2371 // The matcher expects a DerivedClass, but inside the Property() we
2372 // say AClass.
2373 Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0));
2374
2375 DerivedClass d;
2376 d.set_n(1);
2377 EXPECT_TRUE(m.Matches(d));
2378
2379 d.set_n(-1);
2380 EXPECT_FALSE(m.Matches(d));
2381 }
2382
2383 // Tests that Property(&Foo::property, m) works when property()'s type
2384 // and m's argument type are compatible but different.
2385 TEST(PropertyTest, WorksForCompatibleMatcherType) {
2386 // n() returns an int but the inner matcher expects a signed char.
2387 Matcher<const AClass&> m = Property(&AClass::n,
2388 Matcher<signed char>(Ge(0)));
2389
2390 AClass a;
2391 EXPECT_TRUE(m.Matches(a));
2392 a.set_n(-1);
2393 EXPECT_FALSE(m.Matches(a));
2394 }
2395
2396 // Tests that Property() can describe itself.
2397 TEST(PropertyTest, CanDescribeSelf) {
2398 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
2399
2400 EXPECT_EQ("the given property is greater than or equal to 0", Describe(m));
2401 EXPECT_EQ("the given property is not greater than or equal to 0",
2402 DescribeNegation(m));
2403 }
2404
2405 // Tests that Property() can explain the match result.
2406 TEST(PropertyTest, CanExplainMatchResult) {
2407 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
2408
2409 AClass a;
2410 a.set_n(1);
2411 EXPECT_EQ("", Explain(m, a));
2412
2413 m = Property(&AClass::n, GreaterThan(0));
2414 EXPECT_EQ("the given property is 1 more than 0", Explain(m, a));
2415 }
2416
2417 // Tests that Property() works when the argument is a pointer to const.
2418 TEST(PropertyForPointerTest, WorksForPointerToConst) {
2419 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
2420
2421 AClass a;
2422 a.set_n(1);
2423 EXPECT_TRUE(m.Matches(&a));
2424
2425 a.set_n(-1);
2426 EXPECT_FALSE(m.Matches(&a));
2427 }
2428
2429 // Tests that Property() works when the argument is a pointer to non-const.
2430 TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
2431 Matcher<AClass*> m = Property(&AClass::s, StartsWith("hi"));
2432
2433 AClass a;
2434 a.set_s("hill");
2435 EXPECT_TRUE(m.Matches(&a));
2436
2437 a.set_s("hole");
2438 EXPECT_FALSE(m.Matches(&a));
2439 }
2440
2441 // Tests that Property() does not match the NULL pointer.
2442 TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
2443 Matcher<const AClass*> m = Property(&AClass::x, _);
2444 EXPECT_FALSE(m.Matches(NULL));
2445 }
2446
2447 // Tests that Property(&Foo::property, ...) works when the argument's
2448 // type is a sub-type of const Foo*.
2449 TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
2450 // The matcher expects a DerivedClass, but inside the Property() we
2451 // say AClass.
2452 Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0));
2453
2454 DerivedClass d;
2455 d.set_n(1);
2456 EXPECT_TRUE(m.Matches(&d));
2457
2458 d.set_n(-1);
2459 EXPECT_FALSE(m.Matches(&d));
2460 }
2461
2462 // Tests that Property() can describe itself when used to match a pointer.
2463 TEST(PropertyForPointerTest, CanDescribeSelf) {
2464 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
2465
2466 EXPECT_EQ("the given property is greater than or equal to 0", Describe(m));
2467 EXPECT_EQ("the given property is not greater than or equal to 0",
2468 DescribeNegation(m));
2469 }
2470
2471 // Tests that Property() can explain the result of matching a pointer.
2472 TEST(PropertyForPointerTest, CanExplainMatchResult) {
2473 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
2474
2475 AClass a;
2476 a.set_n(1);
2477 EXPECT_EQ("", Explain(m, static_cast<const AClass*>(NULL)));
2478 EXPECT_EQ("", Explain(m, &a));
2479
2480 m = Property(&AClass::n, GreaterThan(0));
2481 EXPECT_EQ("the given property is 1 more than 0", Explain(m, &a));
2482 }
2483
2484 // Tests ResultOf.
2485
2486 // Tests that ResultOf(f, ...) compiles and works as expected when f is a
2487 // function pointer.
2488 string IntToStringFunction(int input) { return input == 1 ? "foo" : "bar"; }
2489
2490 TEST(ResultOfTest, WorksForFunctionPointers) {
2491 Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(string("foo")));
2492
2493 EXPECT_TRUE(matcher.Matches(1));
2494 EXPECT_FALSE(matcher.Matches(2));
2495 }
2496
2497 // Tests that ResultOf() can describe itself.
2498 TEST(ResultOfTest, CanDescribeItself) {
2499 Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo"));
2500
2501 EXPECT_EQ("result of the given callable is equal to \"foo\"",
2502 Describe(matcher));
2503 EXPECT_EQ("result of the given callable is not equal to \"foo\"",
2504 DescribeNegation(matcher));
2505 }
2506
2507 // Tests that ResultOf() can explain the match result.
2508 int IntFunction(int input) { return input == 42 ? 80 : 90; }
2509
2510 TEST(ResultOfTest, CanExplainMatchResult) {
2511 Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
2512 EXPECT_EQ("", Explain(matcher, 36));
2513
2514 matcher = ResultOf(&IntFunction, GreaterThan(85));
2515 EXPECT_EQ("result of the given callable is 5 more than 85",
2516 Explain(matcher, 36));
2517 }
2518
2519 // Tests that ResultOf(f, ...) compiles and works as expected when f(x)
2520 // returns a non-reference.
2521 TEST(ResultOfTest, WorksForNonReferenceResults) {
2522 Matcher<int> matcher = ResultOf(&IntFunction, Eq(80));
2523
2524 EXPECT_TRUE(matcher.Matches(42));
2525 EXPECT_FALSE(matcher.Matches(36));
2526 }
2527
2528 // Tests that ResultOf(f, ...) compiles and works as expected when f(x)
2529 // returns a reference to non-const.
2530 double& DoubleFunction(double& input) { return input; }
2531
2532 Uncopyable& RefUncopyableFunction(Uncopyable& obj) {
2533 return obj;
2534 }
2535
2536 TEST(ResultOfTest, WorksForReferenceToNonConstResults) {
2537 double x = 3.14;
2538 double x2 = x;
2539 Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x));
2540
2541 EXPECT_TRUE(matcher.Matches(x));
2542 EXPECT_FALSE(matcher.Matches(x2));
2543
2544 // Test that ResultOf works with uncopyable objects
2545 Uncopyable obj(0);
2546 Uncopyable obj2(0);
2547 Matcher<Uncopyable&> matcher2 =
2548 ResultOf(&RefUncopyableFunction, Ref(obj));
2549
2550 EXPECT_TRUE(matcher2.Matches(obj));
2551 EXPECT_FALSE(matcher2.Matches(obj2));
2552 }
2553
2554 // Tests that ResultOf(f, ...) compiles and works as expected when f(x)
2555 // returns a reference to const.
2556 const string& StringFunction(const string& input) { return input; }
2557
2558 TEST(ResultOfTest, WorksForReferenceToConstResults) {
2559 string s = "foo";
2560 string s2 = s;
2561 Matcher<const string&> matcher = ResultOf(&StringFunction, Ref(s));
2562
2563 EXPECT_TRUE(matcher.Matches(s));
2564 EXPECT_FALSE(matcher.Matches(s2));
2565 }
2566
2567 // Tests that ResultOf(f, m) works when f(x) and m's
2568 // argument types are compatible but different.
2569 TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {
2570 // IntFunction() returns int but the inner matcher expects a signed char.
2571 Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85)));
2572
2573 EXPECT_TRUE(matcher.Matches(36));
2574 EXPECT_FALSE(matcher.Matches(42));
2575 }
2576
2577 #if GTEST_HAS_DEATH_TEST
2578 // Tests that the program aborts when ResultOf is passed
2579 // a NULL function pointer.
2580 TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {
2581 EXPECT_DEATH(
2582 ResultOf(static_cast<string(*)(int)>(NULL), Eq(string("foo"))),
2583 "NULL function pointer is passed into ResultOf\\(\\)\\.");
2584 }
2585 #endif // GTEST_HAS_DEATH_TEST
2586
2587 // Tests that ResultOf(f, ...) compiles and works as expected when f is a
2588 // function reference.
2589 TEST(ResultOfTest, WorksForFunctionReferences) {
2590 Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq("foo"));
2591 EXPECT_TRUE(matcher.Matches(1));
2592 EXPECT_FALSE(matcher.Matches(2));
2593 }
2594
2595 // Tests that ResultOf(f, ...) compiles and works as expected when f is a
2596 // function object.
2597 struct Functor : public ::std::unary_function<int, string> {
2598 result_type operator()(argument_type input) const {
2599 return IntToStringFunction(input);
2600 }
2601 };
2602
2603 TEST(ResultOfTest, WorksForFunctors) {
2604 Matcher<int> matcher = ResultOf(Functor(), Eq(string("foo")));
2605
2606 EXPECT_TRUE(matcher.Matches(1));
2607 EXPECT_FALSE(matcher.Matches(2));
2608 }
2609
2610 // Tests that ResultOf(f, ...) compiles and works as expected when f is a
2611 // functor with more then one operator() defined. ResultOf() must work
2612 // for each defined operator().
2613 struct PolymorphicFunctor {
2614 typedef int result_type;
2615 int operator()(int n) { return n; }
2616 int operator()(const char* s) { return static_cast<int>(strlen(s)); }
2617 };
2618
2619 TEST(ResultOfTest, WorksForPolymorphicFunctors) {
2620 Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5));
2621
2622 EXPECT_TRUE(matcher_int.Matches(10));
2623 EXPECT_FALSE(matcher_int.Matches(2));
2624
2625 Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5));
2626
2627 EXPECT_TRUE(matcher_string.Matches("long string"));
2628 EXPECT_FALSE(matcher_string.Matches("shrt"));
2629 }
2630
2631 const int* ReferencingFunction(const int& n) { return &n; }
2632
2633 struct ReferencingFunctor {
2634 typedef const int* result_type;
2635 result_type operator()(const int& n) { return &n; }
2636 };
2637
2638 TEST(ResultOfTest, WorksForReferencingCallables) {
2639 const int n = 1;
2640 const int n2 = 1;
2641 Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n));
2642 EXPECT_TRUE(matcher2.Matches(n));
2643 EXPECT_FALSE(matcher2.Matches(n2));
2644
2645 Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n));
2646 EXPECT_TRUE(matcher3.Matches(n));
2647 EXPECT_FALSE(matcher3.Matches(n2));
2648 }
2649
2650
2651 class DivisibleByImpl {
2652 public:
2653 explicit DivisibleByImpl(int divider) : divider_(divider) {}
2654
2655 template <typename T>
2656 bool Matches(const T& n) const {
2657 return (n % divider_) == 0;
2658 }
2659
2660 void DescribeTo(::std::ostream* os) const {
2661 *os << "is divisible by " << divider_;
2662 }
2663
2664 void DescribeNegationTo(::std::ostream* os) const {
2665 *os << "is not divisible by " << divider_;
2666 }
2667
2668 int divider() const { return divider_; }
2669 private:
2670 const int divider_;
2671 };
2672
2673 // For testing using ExplainMatchResultTo() with polymorphic matchers.
2674 template <typename T>
2675 void ExplainMatchResultTo(const DivisibleByImpl& impl, const T& n,
2676 ::std::ostream* os) {
2677 *os << "is " << (n % impl.divider()) << " modulo "
2678 << impl.divider();
2679 }
2680
2681 PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) {
2682 return MakePolymorphicMatcher(DivisibleByImpl(n));
2683 }
2684
2685 // Tests that when AllOf() fails, only the first failing matcher is
2686 // asked to explain why.
2687 TEST(ExplainMatchResultTest, AllOf_False_False) {
2688 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
2689 EXPECT_EQ("is 1 modulo 4", Explain(m, 5));
2690 }
2691
2692 // Tests that when AllOf() fails, only the first failing matcher is
2693 // asked to explain why.
2694 TEST(ExplainMatchResultTest, AllOf_False_True) {
2695 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
2696 EXPECT_EQ("is 2 modulo 4", Explain(m, 6));
2697 }
2698
2699 // Tests that when AllOf() fails, only the first failing matcher is
2700 // asked to explain why.
2701 TEST(ExplainMatchResultTest, AllOf_True_False) {
2702 const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));
2703 EXPECT_EQ("is 2 modulo 3", Explain(m, 5));
2704 }
2705
2706 // Tests that when AllOf() succeeds, all matchers are asked to explain
2707 // why.
2708 TEST(ExplainMatchResultTest, AllOf_True_True) {
2709 const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3));
2710 EXPECT_EQ("is 0 modulo 2; is 0 modulo 3", Explain(m, 6));
2711 }
2712
2713 TEST(ExplainMatchResultTest, AllOf_True_True_2) {
2714 const Matcher<int> m = AllOf(Ge(2), Le(3));
2715 EXPECT_EQ("", Explain(m, 2));
2716 }
2717
2718 TEST(ExplainmatcherResultTest, MonomorphicMatcher) {
2719 const Matcher<int> m = GreaterThan(5);
2720 EXPECT_EQ("is 1 more than 5", Explain(m, 6));
2721 }
2722
2723 // The following two tests verify that values without a public copy
2724 // ctor can be used as arguments to matchers like Eq(), Ge(), and etc
2725 // with the help of ByRef().
2726
2727 class NotCopyable {
2728 public:
2729 explicit NotCopyable(int value) : value_(value) {}
2730
2731 int value() const { return value_; }
2732
2733 bool operator==(const NotCopyable& rhs) const {
2734 return value() == rhs.value();
2735 }
2736
2737 bool operator>=(const NotCopyable& rhs) const {
2738 return value() >= rhs.value();
2739 }
2740 private:
2741 int value_;
2742
2743 GTEST_DISALLOW_COPY_AND_ASSIGN_(NotCopyable);
2744 };
2745
2746 TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) {
2747 const NotCopyable const_value1(1);
2748 const Matcher<const NotCopyable&> m = Eq(ByRef(const_value1));
2749
2750 const NotCopyable n1(1), n2(2);
2751 EXPECT_TRUE(m.Matches(n1));
2752 EXPECT_FALSE(m.Matches(n2));
2753 }
2754
2755 TEST(ByRefTest, AllowsNotCopyableValueInMatchers) {
2756 NotCopyable value2(2);
2757 const Matcher<NotCopyable&> m = Ge(ByRef(value2));
2758
2759 NotCopyable n1(1), n2(2);
2760 EXPECT_FALSE(m.Matches(n1));
2761 EXPECT_TRUE(m.Matches(n2));
2762 }
2763
2764 // Tests ContainerEq with different container types, and
2765 // different element types.
2766
2767 template <typename T>
2768 class ContainerEqTest : public testing::Test {
2769 public:
2770 };
2771
2772 typedef testing::Types<
2773 std::set<int>,
2774 std::vector<size_t>,
2775 std::multiset<size_t>,
2776 std::list<int> >
2777 ContainerEqTestTypes;
2778
2779 TYPED_TEST_CASE(ContainerEqTest, ContainerEqTestTypes);
2780
2781 // Tests that the filled container is equal to itself.
2782 TYPED_TEST(ContainerEqTest, EqualsSelf) {
2783 static const int vals[] = {1, 1, 2, 3, 5, 8};
2784 TypeParam my_set(vals, vals + 6);
2785 const Matcher<TypeParam> m = ContainerEq(my_set);
2786 EXPECT_TRUE(m.Matches(my_set));
2787 EXPECT_EQ("", Explain(m, my_set));
2788 }
2789
2790 // Tests that missing values are reported.
2791 TYPED_TEST(ContainerEqTest, ValueMissing) {
2792 static const int vals[] = {1, 1, 2, 3, 5, 8};
2793 static const int test_vals[] = {2, 1, 8, 5};
2794 TypeParam my_set(vals, vals + 6);
2795 TypeParam test_set(test_vals, test_vals + 4);
2796 const Matcher<TypeParam> m = ContainerEq(my_set);
2797 EXPECT_FALSE(m.Matches(test_set));
2798 EXPECT_EQ("Not in actual: 3", Explain(m, test_set));
2799 }
2800
2801 // Tests that added values are reported.
2802 TYPED_TEST(ContainerEqTest, ValueAdded) {
2803 static const int vals[] = {1, 1, 2, 3, 5, 8};
2804 static const int test_vals[] = {1, 2, 3, 5, 8, 46};
2805 TypeParam my_set(vals, vals + 6);
2806 TypeParam test_set(test_vals, test_vals + 6);
2807 const Matcher<const TypeParam&> m = ContainerEq(my_set);
2808 EXPECT_FALSE(m.Matches(test_set));
2809 EXPECT_EQ("Only in actual: 46", Explain(m, test_set));
2810 }
2811
2812 // Tests that added and missing values are reported together.
2813 TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) {
2814 static const int vals[] = {1, 1, 2, 3, 5, 8};
2815 static const int test_vals[] = {1, 2, 3, 8, 46};
2816 TypeParam my_set(vals, vals + 6);
2817 TypeParam test_set(test_vals, test_vals + 5);
2818 const Matcher<TypeParam> m = ContainerEq(my_set);
2819 EXPECT_FALSE(m.Matches(test_set));
2820 EXPECT_EQ("Only in actual: 46; not in actual: 5", Explain(m, test_set));
2821 }
2822
2823 // Tests duplicated value -- expect no explanation.
2824 TYPED_TEST(ContainerEqTest, DuplicateDifference) {
2825 static const int vals[] = {1, 1, 2, 3, 5, 8};
2826 static const int test_vals[] = {1, 2, 3, 5, 8};
2827 TypeParam my_set(vals, vals + 6);
2828 TypeParam test_set(test_vals, test_vals + 5);
2829 const Matcher<const TypeParam&> m = ContainerEq(my_set);
2830 // Depending on the container, match may be true or false
2831 // But in any case there should be no explanation.
2832 EXPECT_EQ("", Explain(m, test_set));
2833 }
2834
2835 // Tests that mutliple missing values are reported.
2836 // Using just vector here, so order is predicatble.
2837 TEST(ContainerEqExtraTest, MultipleValuesMissing) {
2838 static const int vals[] = {1, 1, 2, 3, 5, 8};
2839 static const int test_vals[] = {2, 1, 5};
2840 std::vector<int> my_set(vals, vals + 6);
2841 std::vector<int> test_set(test_vals, test_vals + 3);
2842 const Matcher<std::vector<int> > m = ContainerEq(my_set);
2843 EXPECT_FALSE(m.Matches(test_set));
2844 EXPECT_EQ("Not in actual: 3, 8", Explain(m, test_set));
2845 }
2846
2847 // Tests that added values are reported.
2848 // Using just vector here, so order is predicatble.
2849 TEST(ContainerEqExtraTest, MultipleValuesAdded) {
2850 static const int vals[] = {1, 1, 2, 3, 5, 8};
2851 static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46};
2852 std::list<size_t> my_set(vals, vals + 6);
2853 std::list<size_t> test_set(test_vals, test_vals + 7);
2854 const Matcher<const std::list<size_t>&> m = ContainerEq(my_set);
2855 EXPECT_FALSE(m.Matches(test_set));
2856 EXPECT_EQ("Only in actual: 92, 46", Explain(m, test_set));
2857 }
2858
2859 // Tests that added and missing values are reported together.
2860 TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) {
2861 static const int vals[] = {1, 1, 2, 3, 5, 8};
2862 static const int test_vals[] = {1, 2, 3, 92, 46};
2863 std::list<size_t> my_set(vals, vals + 6);
2864 std::list<size_t> test_set(test_vals, test_vals + 5);
2865 const Matcher<const std::list<size_t> > m = ContainerEq(my_set);
2866 EXPECT_FALSE(m.Matches(test_set));
2867 EXPECT_EQ("Only in actual: 92, 46; not in actual: 5, 8",
2868 Explain(m, test_set));
2869 }
2870
2871 // Tests to see that duplicate elements are detected,
2872 // but (as above) not reported in the explanation.
2873 TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) {
2874 static const int vals[] = {1, 1, 2, 3, 5, 8};
2875 static const int test_vals[] = {1, 2, 3, 5, 8};
2876 std::vector<int> my_set(vals, vals + 6);
2877 std::vector<int> test_set(test_vals, test_vals + 5);
2878 const Matcher<std::vector<int> > m = ContainerEq(my_set);
2879 EXPECT_TRUE(m.Matches(my_set));
2880 EXPECT_FALSE(m.Matches(test_set));
2881 // There is nothing to report when both sets contain all the same values.
2882 EXPECT_EQ("", Explain(m, test_set));
2883 }
2884
2885 // Tests that ContainerEq works for non-trivial associative containers,
2886 // like maps.
2887 TEST(ContainerEqExtraTest, WorksForMaps) {
2888 std::map<int, std::string> my_map;
2889 my_map[0] = "a";
2890 my_map[1] = "b";
2891
2892 std::map<int, std::string> test_map;
2893 test_map[0] = "aa";
2894 test_map[1] = "b";
2895
2896 const Matcher<const std::map<int, std::string>&> m = ContainerEq(my_map);
2897 EXPECT_TRUE(m.Matches(my_map));
2898 EXPECT_FALSE(m.Matches(test_map));
2899
2900 EXPECT_EQ("Only in actual: (0, \"aa\"); not in actual: (0, \"a\")",
2901 Explain(m, test_map));
2902 }
2903
2904 // Tests GetParamIndex().
2905
2906 TEST(GetParamIndexTest, WorksForEmptyParamList) {
2907 const char* params[] = { NULL };
2908 EXPECT_EQ(kTupleInterpolation, GetParamIndex(params, "*"));
2909 EXPECT_EQ(kInvalidInterpolation, GetParamIndex(params, "a"));
2910 }
2911
2912 TEST(GetParamIndexTest, RecognizesStar) {
2913 const char* params[] = { "a", "b", NULL };
2914 EXPECT_EQ(kTupleInterpolation, GetParamIndex(params, "*"));
2915 }
2916
2917 TEST(GetParamIndexTest, RecognizesKnownParam) {
2918 const char* params[] = { "foo", "bar", NULL };
2919 EXPECT_EQ(0, GetParamIndex(params, "foo"));
2920 EXPECT_EQ(1, GetParamIndex(params, "bar"));
2921 }
2922
2923 TEST(GetParamIndexTest, RejectsUnknownParam) {
2924 const char* params[] = { "foo", "bar", NULL };
2925 EXPECT_EQ(kInvalidInterpolation, GetParamIndex(params, "foobar"));
2926 }
2927
2928 // Tests SkipPrefix().
2929
2930 TEST(SkipPrefixTest, SkipsWhenPrefixMatches) {
2931 const char* const str = "hello";
2932
2933 const char* p = str;
2934 EXPECT_TRUE(SkipPrefix("", &p));
2935 EXPECT_EQ(str, p);
2936
2937 p = str;
2938 EXPECT_TRUE(SkipPrefix("hell", &p));
2939 EXPECT_EQ(str + 4, p);
2940 }
2941
2942 TEST(SkipPrefixTest, DoesNotSkipWhenPrefixDoesNotMatch) {
2943 const char* const str = "world";
2944
2945 const char* p = str;
2946 EXPECT_FALSE(SkipPrefix("W", &p));
2947 EXPECT_EQ(str, p);
2948
2949 p = str;
2950 EXPECT_FALSE(SkipPrefix("world!", &p));
2951 EXPECT_EQ(str, p);
2952 }
2953
2954 // Tests FormatMatcherDescriptionSyntaxError().
2955 TEST(FormatMatcherDescriptionSyntaxErrorTest, FormatsCorrectly) {
2956 const char* const description = "hello%world";
2957 EXPECT_EQ("Syntax error at index 5 in matcher description \"hello%world\": ",
2958 FormatMatcherDescriptionSyntaxError(description, description + 5));
2959 }
2960
2961 // Tests ValidateMatcherDescription().
2962
2963 TEST(ValidateMatcherDescriptionTest, AcceptsEmptyDescription) {
2964 const char* params[] = { "foo", "bar", NULL };
2965 EXPECT_THAT(ValidateMatcherDescription(params, ""),
2966 ElementsAre());
2967 }
2968
2969 TEST(ValidateMatcherDescriptionTest,
2970 AcceptsNonEmptyDescriptionWithNoInterpolation) {
2971 const char* params[] = { "foo", "bar", NULL };
2972 EXPECT_THAT(ValidateMatcherDescription(params, "a simple description"),
2973 ElementsAre());
2974 }
2975
2976 // We use MATCHER_P3() to define a matcher for testing
2977 // ValidateMatcherDescription(); otherwise we'll end up with much
2978 // plumbing code. This is not circular as
2979 // ValidateMatcherDescription() doesn't affect whether the matcher
2980 // matches a value or not.
2981 MATCHER_P3(EqInterpolation, start, end, index, "equals Interpolation%(*)s") {
2982 return arg.start_pos == start && arg.end_pos == end &&
2983 arg.param_index == index;
2984 }
2985
2986 TEST(ValidateMatcherDescriptionTest, AcceptsPercentInterpolation) {
2987 const char* params[] = { "foo", NULL };
2988 const char* const desc = "one %%";
2989 EXPECT_THAT(ValidateMatcherDescription(params, desc),
2990 ElementsAre(EqInterpolation(desc + 4, desc + 6,
2991 kPercentInterpolation)));
2992 }
2993
2994 TEST(ValidateMatcherDescriptionTest, AcceptsTupleInterpolation) {
2995 const char* params[] = { "foo", "bar", "baz", NULL };
2996 const char* const desc = "%(*)s after";
2997 EXPECT_THAT(ValidateMatcherDescription(params, desc),
2998 ElementsAre(EqInterpolation(desc, desc + 5,
2999 kTupleInterpolation)));
3000 }
3001
3002 TEST(ValidateMatcherDescriptionTest, AcceptsParamInterpolation) {
3003 const char* params[] = { "foo", "bar", "baz", NULL };
3004 const char* const desc = "a %(bar)s.";
3005 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3006 ElementsAre(EqInterpolation(desc + 2, desc + 9, 1)));
3007 }
3008
3009 TEST(ValidateMatcherDescriptionTest, AcceptsMultiplenterpolations) {
3010 const char* params[] = { "foo", "bar", "baz", NULL };
3011 const char* const desc = "%(baz)s %(foo)s %(bar)s";
3012 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3013 ElementsAre(EqInterpolation(desc, desc + 7, 2),
3014 EqInterpolation(desc + 8, desc + 15, 0),
3015 EqInterpolation(desc + 16, desc + 23, 1)));
3016 }
3017
3018 TEST(ValidateMatcherDescriptionTest, AcceptsRepeatedParams) {
3019 const char* params[] = { "foo", "bar", NULL };
3020 const char* const desc = "%(foo)s and %(foo)s";
3021 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3022 ElementsAre(EqInterpolation(desc, desc + 7, 0),
3023 EqInterpolation(desc + 12, desc + 19, 0)));
3024 }
3025
3026 TEST(ValidateMatcherDescriptionTest, RejectsUnknownParam) {
3027 const char* params[] = { "a", "bar", NULL };
3028 EXPECT_NONFATAL_FAILURE({
3029 EXPECT_THAT(ValidateMatcherDescription(params, "%(foo)s"),
3030 ElementsAre());
3031 }, "Syntax error at index 2 in matcher description \"%(foo)s\": "
3032 "\"foo\" is an invalid parameter name.");
3033 }
3034
3035 TEST(ValidateMatcherDescriptionTest, RejectsUnfinishedParam) {
3036 const char* params[] = { "a", "bar", NULL };
3037 EXPECT_NONFATAL_FAILURE({
3038 EXPECT_THAT(ValidateMatcherDescription(params, "%(foo)"),
3039 ElementsAre());
3040 }, "Syntax error at index 0 in matcher description \"%(foo)\": "
3041 "an interpolation must end with \")s\", but \"%(foo)\" does not.");
3042
3043 EXPECT_NONFATAL_FAILURE({
3044 EXPECT_THAT(ValidateMatcherDescription(params, "x%(a"),
3045 ElementsAre());
3046 }, "Syntax error at index 1 in matcher description \"x%(a\": "
3047 "an interpolation must end with \")s\", but \"%(a\" does not.");
3048 }
3049
3050 TEST(ValidateMatcherDescriptionTest, RejectsSinglePercent) {
3051 const char* params[] = { "a", NULL };
3052 EXPECT_NONFATAL_FAILURE({
3053 EXPECT_THAT(ValidateMatcherDescription(params, "a %."),
3054 ElementsAre());
3055 }, "Syntax error at index 2 in matcher description \"a %.\": "
3056 "use \"%%\" instead of \"%\" to print \"%\".");
3057
3058 }
3059
3060 // Tests JoinAsTuple().
3061
3062 TEST(JoinAsTupleTest, JoinsEmptyTuple) {
3063 EXPECT_EQ("", JoinAsTuple(Strings()));
3064 }
3065
3066 TEST(JoinAsTupleTest, JoinsOneTuple) {
3067 const char* fields[] = { "1" };
3068 EXPECT_EQ("1", JoinAsTuple(Strings(fields, fields + 1)));
3069 }
3070
3071 TEST(JoinAsTupleTest, JoinsTwoTuple) {
3072 const char* fields[] = { "1", "a" };
3073 EXPECT_EQ("(1, a)", JoinAsTuple(Strings(fields, fields + 2)));
3074 }
3075
3076 TEST(JoinAsTupleTest, JoinsTenTuple) {
3077 const char* fields[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
3078 EXPECT_EQ("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)",
3079 JoinAsTuple(Strings(fields, fields + 10)));
3080 }
3081
3082 // Tests FormatMatcherDescription().
3083
3084 TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) {
3085 EXPECT_EQ("is even",
3086 FormatMatcherDescription("IsEven", "", Interpolations(),
3087 Strings()));
3088
3089 const char* params[] = { "5" };
3090 EXPECT_EQ("equals 5",
3091 FormatMatcherDescription("Equals", "", Interpolations(),
3092 Strings(params, params + 1)));
3093
3094 const char* params2[] = { "5", "8" };
3095 EXPECT_EQ("is in range (5, 8)",
3096 FormatMatcherDescription("IsInRange", "", Interpolations(),
3097 Strings(params2, params2 + 2)));
3098 }
3099
3100 TEST(FormatMatcherDescriptionTest, WorksForDescriptionWithNoInterpolation) {
3101 EXPECT_EQ("is positive",
3102 FormatMatcherDescription("Gt0", "is positive", Interpolations(),
3103 Strings()));
3104
3105 const char* params[] = { "5", "6" };
3106 EXPECT_EQ("is negative",
3107 FormatMatcherDescription("Lt0", "is negative", Interpolations(),
3108 Strings(params, params + 2)));
3109 }
3110
3111 TEST(FormatMatcherDescriptionTest,
3112 WorksWhenDescriptionStartsWithInterpolation) {
3113 const char* params[] = { "5" };
3114 const char* const desc = "%(num)s times bigger";
3115 const Interpolation interp[] = { Interpolation(desc, desc + 7, 0) };
3116 EXPECT_EQ("5 times bigger",
3117 FormatMatcherDescription("Foo", desc,
3118 Interpolations(interp, interp + 1),
3119 Strings(params, params + 1)));
3120 }
3121
3122 TEST(FormatMatcherDescriptionTest,
3123 WorksWhenDescriptionEndsWithInterpolation) {
3124 const char* params[] = { "5", "6" };
3125 const char* const desc = "is bigger than %(y)s";
3126 const Interpolation interp[] = { Interpolation(desc + 15, desc + 20, 1) };
3127 EXPECT_EQ("is bigger than 6",
3128 FormatMatcherDescription("Foo", desc,
3129 Interpolations(interp, interp + 1),
3130 Strings(params, params + 2)));
3131 }
3132
3133 TEST(FormatMatcherDescriptionTest,
3134 WorksWhenDescriptionStartsAndEndsWithInterpolation) {
3135 const char* params[] = { "5", "6" };
3136 const char* const desc = "%(x)s <= arg <= %(y)s";
3137 const Interpolation interp[] = {
3138 Interpolation(desc, desc + 5, 0),
3139 Interpolation(desc + 16, desc + 21, 1)
3140 };
3141 EXPECT_EQ("5 <= arg <= 6",
3142 FormatMatcherDescription("Foo", desc,
3143 Interpolations(interp, interp + 2),
3144 Strings(params, params + 2)));
3145 }
3146
3147 TEST(FormatMatcherDescriptionTest,
3148 WorksWhenDescriptionDoesNotStartOrEndWithInterpolation) {
3149 const char* params[] = { "5.2" };
3150 const char* const desc = "has %(x)s cents";
3151 const Interpolation interp[] = { Interpolation(desc + 4, desc + 9, 0) };
3152 EXPECT_EQ("has 5.2 cents",
3153 FormatMatcherDescription("Foo", desc,
3154 Interpolations(interp, interp + 1),
3155 Strings(params, params + 1)));
3156 }
3157
3158 TEST(FormatMatcherDescriptionTest,
3159 WorksWhenDescriptionContainsMultipleInterpolations) {
3160 const char* params[] = { "5", "6" };
3161 const char* const desc = "in %(*)s or [%(x)s, %(y)s]";
3162 const Interpolation interp[] = {
3163 Interpolation(desc + 3, desc + 8, kTupleInterpolation),
3164 Interpolation(desc + 13, desc + 18, 0),
3165 Interpolation(desc + 20, desc + 25, 1)
3166 };
3167 EXPECT_EQ("in (5, 6) or [5, 6]",
3168 FormatMatcherDescription("Foo", desc,
3169 Interpolations(interp, interp + 3),
3170 Strings(params, params + 2)));
3171 }
3172
3173 TEST(FormatMatcherDescriptionTest,
3174 WorksWhenDescriptionContainsRepeatedParams) {
3175 const char* params[] = { "9" };
3176 const char* const desc = "in [-%(x)s, %(x)s]";
3177 const Interpolation interp[] = {
3178 Interpolation(desc + 5, desc + 10, 0),
3179 Interpolation(desc + 12, desc + 17, 0)
3180 };
3181 EXPECT_EQ("in [-9, 9]",
3182 FormatMatcherDescription("Foo", desc,
3183 Interpolations(interp, interp + 2),
3184 Strings(params, params + 1)));
3185 }
3186
3187 TEST(FormatMatcherDescriptionTest,
3188 WorksForDescriptionWithInvalidInterpolation) {
3189 const char* params[] = { "9" };
3190 const char* const desc = "> %(x)s %(x)";
3191 const Interpolation interp[] = { Interpolation(desc + 2, desc + 7, 0) };
3192 EXPECT_EQ("> 9 %(x)",
3193 FormatMatcherDescription("Foo", desc,
3194 Interpolations(interp, interp + 1),
3195 Strings(params, params + 1)));
3196 }
3197
3198 } // namespace gmock_matchers_test
3199 } // namespace testing
OLDNEW
« no previous file with comments | « testing/gmock/test/gmock-internal-utils_test.cc ('k') | testing/gmock/test/gmock-nice-strict_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698