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