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