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