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