blob: 20b9387f6b093fed66e8630026752e01eb107052 [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
zhanyong.wan95b12332009-09-25 18:55:50 +0000702#if !GTEST_OS_SYMBIAN
703 // Nokia's Symbian compiler generates:
704 // gmock-matchers.h: ambiguous access to overloaded function
705 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(void *)'
706 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(const testing::
707 // MatcherInterface<void *> *)'
708 // gmock-matchers.h: (point of instantiation: 'testing::
709 // gmock_matchers_test::IsNullTest_MatchesNullPointer_Test::TestBody()')
710 // gmock-matchers.h: (instantiating: 'testing::PolymorphicMatc
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000711 Matcher<void*> m3 = IsNull();
712 void* p3 = NULL;
713 EXPECT_TRUE(m3.Matches(p3));
714 EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef)));
zhanyong.wan95b12332009-09-25 18:55:50 +0000715#endif
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000716}
717
718// Tests that IsNull() describes itself properly.
719TEST(IsNullTest, CanDescribeSelf) {
720 Matcher<int*> m = IsNull();
721 EXPECT_EQ("is NULL", Describe(m));
722 EXPECT_EQ("is not NULL", DescribeNegation(m));
723}
724
shiqiane35fdd92008-12-10 05:08:54 +0000725// Tests that NotNull() matches any non-NULL pointer of any type.
726TEST(NotNullTest, MatchesNonNullPointer) {
727 Matcher<int*> m1 = NotNull();
728 int* p1 = NULL;
729 int n = 0;
730 EXPECT_FALSE(m1.Matches(p1));
731 EXPECT_TRUE(m1.Matches(&n));
732
733 Matcher<const char*> m2 = NotNull();
734 const char* p2 = NULL;
735 EXPECT_FALSE(m2.Matches(p2));
736 EXPECT_TRUE(m2.Matches("hi"));
737}
738
739// Tests that NotNull() describes itself properly.
740TEST(NotNullTest, CanDescribeSelf) {
741 Matcher<int*> m = NotNull();
742 EXPECT_EQ("is not NULL", Describe(m));
743}
744
745// Tests that Ref(variable) matches an argument that references
746// 'variable'.
747TEST(RefTest, MatchesSameVariable) {
748 int a = 0;
749 int b = 0;
750 Matcher<int&> m = Ref(a);
751 EXPECT_TRUE(m.Matches(a));
752 EXPECT_FALSE(m.Matches(b));
753}
754
755// Tests that Ref(variable) describes itself properly.
756TEST(RefTest, CanDescribeSelf) {
757 int n = 5;
758 Matcher<int&> m = Ref(n);
759 stringstream ss;
760 ss << "references the variable @" << &n << " 5";
761 EXPECT_EQ(string(ss.str()), Describe(m));
762}
763
764// Test that Ref(non_const_varialbe) can be used as a matcher for a
765// const reference.
766TEST(RefTest, CanBeUsedAsMatcherForConstReference) {
767 int a = 0;
768 int b = 0;
769 Matcher<const int&> m = Ref(a);
770 EXPECT_TRUE(m.Matches(a));
771 EXPECT_FALSE(m.Matches(b));
772}
773
774// Tests that Ref(variable) is covariant, i.e. Ref(derived) can be
775// used wherever Ref(base) can be used (Ref(derived) is a sub-type
776// of Ref(base), but not vice versa.
777
shiqiane35fdd92008-12-10 05:08:54 +0000778TEST(RefTest, IsCovariant) {
779 Base base, base2;
780 Derived derived;
781 Matcher<const Base&> m1 = Ref(base);
782 EXPECT_TRUE(m1.Matches(base));
783 EXPECT_FALSE(m1.Matches(base2));
784 EXPECT_FALSE(m1.Matches(derived));
785
786 m1 = Ref(derived);
787 EXPECT_TRUE(m1.Matches(derived));
788 EXPECT_FALSE(m1.Matches(base));
789 EXPECT_FALSE(m1.Matches(base2));
790}
791
792// Tests string comparison matchers.
793
794TEST(StrEqTest, MatchesEqualString) {
795 Matcher<const char*> m = StrEq(string("Hello"));
796 EXPECT_TRUE(m.Matches("Hello"));
797 EXPECT_FALSE(m.Matches("hello"));
798 EXPECT_FALSE(m.Matches(NULL));
799
800 Matcher<const string&> m2 = StrEq("Hello");
801 EXPECT_TRUE(m2.Matches("Hello"));
802 EXPECT_FALSE(m2.Matches("Hi"));
803}
804
805TEST(StrEqTest, CanDescribeSelf) {
806 Matcher<string> m = StrEq("Hi-\'\"\?\\\a\b\f\n\r\t\v\xD3");
807 EXPECT_EQ("is equal to \"Hi-\'\\\"\\?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",
808 Describe(m));
809
810 string str("01204500800");
811 str[3] = '\0';
812 Matcher<string> m2 = StrEq(str);
813 EXPECT_EQ("is equal to \"012\\04500800\"", Describe(m2));
814 str[0] = str[6] = str[7] = str[9] = str[10] = '\0';
815 Matcher<string> m3 = StrEq(str);
816 EXPECT_EQ("is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3));
817}
818
819TEST(StrNeTest, MatchesUnequalString) {
820 Matcher<const char*> m = StrNe("Hello");
821 EXPECT_TRUE(m.Matches(""));
822 EXPECT_TRUE(m.Matches(NULL));
823 EXPECT_FALSE(m.Matches("Hello"));
824
825 Matcher<string> m2 = StrNe(string("Hello"));
826 EXPECT_TRUE(m2.Matches("hello"));
827 EXPECT_FALSE(m2.Matches("Hello"));
828}
829
830TEST(StrNeTest, CanDescribeSelf) {
831 Matcher<const char*> m = StrNe("Hi");
832 EXPECT_EQ("is not equal to \"Hi\"", Describe(m));
833}
834
835TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
836 Matcher<const char*> m = StrCaseEq(string("Hello"));
837 EXPECT_TRUE(m.Matches("Hello"));
838 EXPECT_TRUE(m.Matches("hello"));
839 EXPECT_FALSE(m.Matches("Hi"));
840 EXPECT_FALSE(m.Matches(NULL));
841
842 Matcher<const string&> m2 = StrCaseEq("Hello");
843 EXPECT_TRUE(m2.Matches("hello"));
844 EXPECT_FALSE(m2.Matches("Hi"));
845}
846
847TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
848 string str1("oabocdooeoo");
849 string str2("OABOCDOOEOO");
850 Matcher<const string&> m0 = StrCaseEq(str1);
851 EXPECT_FALSE(m0.Matches(str2 + string(1, '\0')));
852
853 str1[3] = str2[3] = '\0';
854 Matcher<const string&> m1 = StrCaseEq(str1);
855 EXPECT_TRUE(m1.Matches(str2));
856
857 str1[0] = str1[6] = str1[7] = str1[10] = '\0';
858 str2[0] = str2[6] = str2[7] = str2[10] = '\0';
859 Matcher<const string&> m2 = StrCaseEq(str1);
860 str1[9] = str2[9] = '\0';
861 EXPECT_FALSE(m2.Matches(str2));
862
863 Matcher<const string&> m3 = StrCaseEq(str1);
864 EXPECT_TRUE(m3.Matches(str2));
865
866 EXPECT_FALSE(m3.Matches(str2 + "x"));
867 str2.append(1, '\0');
868 EXPECT_FALSE(m3.Matches(str2));
869 EXPECT_FALSE(m3.Matches(string(str2, 0, 9)));
870}
871
872TEST(StrCaseEqTest, CanDescribeSelf) {
873 Matcher<string> m = StrCaseEq("Hi");
874 EXPECT_EQ("is equal to (ignoring case) \"Hi\"", Describe(m));
875}
876
877TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
878 Matcher<const char*> m = StrCaseNe("Hello");
879 EXPECT_TRUE(m.Matches("Hi"));
880 EXPECT_TRUE(m.Matches(NULL));
881 EXPECT_FALSE(m.Matches("Hello"));
882 EXPECT_FALSE(m.Matches("hello"));
883
884 Matcher<string> m2 = StrCaseNe(string("Hello"));
885 EXPECT_TRUE(m2.Matches(""));
886 EXPECT_FALSE(m2.Matches("Hello"));
887}
888
889TEST(StrCaseNeTest, CanDescribeSelf) {
890 Matcher<const char*> m = StrCaseNe("Hi");
891 EXPECT_EQ("is not equal to (ignoring case) \"Hi\"", Describe(m));
892}
893
894// Tests that HasSubstr() works for matching string-typed values.
895TEST(HasSubstrTest, WorksForStringClasses) {
896 const Matcher<string> m1 = HasSubstr("foo");
897 EXPECT_TRUE(m1.Matches(string("I love food.")));
898 EXPECT_FALSE(m1.Matches(string("tofo")));
899
900 const Matcher<const std::string&> m2 = HasSubstr("foo");
901 EXPECT_TRUE(m2.Matches(std::string("I love food.")));
902 EXPECT_FALSE(m2.Matches(std::string("tofo")));
903}
904
905// Tests that HasSubstr() works for matching C-string-typed values.
906TEST(HasSubstrTest, WorksForCStrings) {
907 const Matcher<char*> m1 = HasSubstr("foo");
908 EXPECT_TRUE(m1.Matches(const_cast<char*>("I love food.")));
909 EXPECT_FALSE(m1.Matches(const_cast<char*>("tofo")));
910 EXPECT_FALSE(m1.Matches(NULL));
911
912 const Matcher<const char*> m2 = HasSubstr("foo");
913 EXPECT_TRUE(m2.Matches("I love food."));
914 EXPECT_FALSE(m2.Matches("tofo"));
915 EXPECT_FALSE(m2.Matches(NULL));
916}
917
918// Tests that HasSubstr(s) describes itself properly.
919TEST(HasSubstrTest, CanDescribeSelf) {
920 Matcher<string> m = HasSubstr("foo\n\"");
921 EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m));
922}
923
zhanyong.wanb5937da2009-07-16 20:26:41 +0000924TEST(KeyTest, CanDescribeSelf) {
925 Matcher<const std::pair<std::string, int>&> m = Key("foo");
926 EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m));
927}
928
929TEST(KeyTest, MatchesCorrectly) {
930 std::pair<int, std::string> p(25, "foo");
931 EXPECT_THAT(p, Key(25));
932 EXPECT_THAT(p, Not(Key(42)));
933 EXPECT_THAT(p, Key(Ge(20)));
934 EXPECT_THAT(p, Not(Key(Lt(25))));
935}
936
937TEST(KeyTest, SafelyCastsInnerMatcher) {
938 Matcher<int> is_positive = Gt(0);
939 Matcher<int> is_negative = Lt(0);
940 std::pair<char, bool> p('a', true);
941 EXPECT_THAT(p, Key(is_positive));
942 EXPECT_THAT(p, Not(Key(is_negative)));
943}
944
945TEST(KeyTest, InsideContainsUsingMap) {
zhanyong.wan95b12332009-09-25 18:55:50 +0000946 std::map<int, char> container;
947 container.insert(std::make_pair(1, 'a'));
948 container.insert(std::make_pair(2, 'b'));
949 container.insert(std::make_pair(4, 'c'));
zhanyong.wanb5937da2009-07-16 20:26:41 +0000950 EXPECT_THAT(container, Contains(Key(1)));
951 EXPECT_THAT(container, Not(Contains(Key(3))));
952}
953
954TEST(KeyTest, InsideContainsUsingMultimap) {
zhanyong.wan95b12332009-09-25 18:55:50 +0000955 std::multimap<int, char> container;
956 container.insert(std::make_pair(1, 'a'));
957 container.insert(std::make_pair(2, 'b'));
958 container.insert(std::make_pair(4, 'c'));
zhanyong.wanb5937da2009-07-16 20:26:41 +0000959
960 EXPECT_THAT(container, Not(Contains(Key(25))));
zhanyong.wan95b12332009-09-25 18:55:50 +0000961 container.insert(std::make_pair(25, 'd'));
zhanyong.wanb5937da2009-07-16 20:26:41 +0000962 EXPECT_THAT(container, Contains(Key(25)));
zhanyong.wan95b12332009-09-25 18:55:50 +0000963 container.insert(std::make_pair(25, 'e'));
zhanyong.wanb5937da2009-07-16 20:26:41 +0000964 EXPECT_THAT(container, Contains(Key(25)));
965
966 EXPECT_THAT(container, Contains(Key(1)));
967 EXPECT_THAT(container, Not(Contains(Key(3))));
968}
969
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000970TEST(PairTest, Typing) {
971 // Test verifies the following type conversions can be compiled.
972 Matcher<const std::pair<const char*, int>&> m1 = Pair("foo", 42);
973 Matcher<const std::pair<const char*, int> > m2 = Pair("foo", 42);
974 Matcher<std::pair<const char*, int> > m3 = Pair("foo", 42);
975
976 Matcher<std::pair<int, const std::string> > m4 = Pair(25, "42");
977 Matcher<std::pair<const std::string, int> > m5 = Pair("25", 42);
978}
979
980TEST(PairTest, CanDescribeSelf) {
981 Matcher<const std::pair<std::string, int>&> m1 = Pair("foo", 42);
982 EXPECT_EQ("has a first field that is equal to \"foo\""
983 ", and has a second field that is equal to 42",
984 Describe(m1));
985 EXPECT_EQ("has a first field that is not equal to \"foo\""
986 ", or has a second field that is not equal to 42",
987 DescribeNegation(m1));
988 // Double and triple negation (1 or 2 times not and description of negation).
989 Matcher<const std::pair<int, int>&> m2 = Not(Pair(Not(13), 42));
990 EXPECT_EQ("has a first field that is not equal to 13"
991 ", and has a second field that is equal to 42",
992 DescribeNegation(m2));
993}
994
995TEST(PairTest, CanExplainMatchResultTo) {
996 const Matcher<std::pair<int, int> > m0 = Pair(0, 0);
997 EXPECT_EQ("", Explain(m0, std::make_pair(25, 42)));
998
999 const Matcher<std::pair<int, int> > m1 = Pair(GreaterThan(0), 0);
1000 EXPECT_EQ("the first field is 25 more than 0",
1001 Explain(m1, std::make_pair(25, 42)));
1002
1003 const Matcher<std::pair<int, int> > m2 = Pair(0, GreaterThan(0));
1004 EXPECT_EQ("the second field is 42 more than 0",
1005 Explain(m2, std::make_pair(25, 42)));
1006
1007 const Matcher<std::pair<int, int> > m3 = Pair(GreaterThan(0), GreaterThan(0));
1008 EXPECT_EQ("the first field is 25 more than 0"
1009 ", and the second field is 42 more than 0",
1010 Explain(m3, std::make_pair(25, 42)));
1011}
1012
1013TEST(PairTest, MatchesCorrectly) {
1014 std::pair<int, std::string> p(25, "foo");
1015
1016 // Both fields match.
1017 EXPECT_THAT(p, Pair(25, "foo"));
1018 EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o")));
1019
1020 // 'first' doesnt' match, but 'second' matches.
1021 EXPECT_THAT(p, Not(Pair(42, "foo")));
1022 EXPECT_THAT(p, Not(Pair(Lt(25), "foo")));
1023
1024 // 'first' matches, but 'second' doesn't match.
1025 EXPECT_THAT(p, Not(Pair(25, "bar")));
1026 EXPECT_THAT(p, Not(Pair(25, Not("foo"))));
1027
1028 // Neither field matches.
1029 EXPECT_THAT(p, Not(Pair(13, "bar")));
1030 EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a"))));
1031}
1032
1033TEST(PairTest, SafelyCastsInnerMatchers) {
1034 Matcher<int> is_positive = Gt(0);
1035 Matcher<int> is_negative = Lt(0);
1036 std::pair<char, bool> p('a', true);
1037 EXPECT_THAT(p, Pair(is_positive, _));
1038 EXPECT_THAT(p, Not(Pair(is_negative, _)));
1039 EXPECT_THAT(p, Pair(_, is_positive));
1040 EXPECT_THAT(p, Not(Pair(_, is_negative)));
1041}
1042
1043TEST(PairTest, InsideContainsUsingMap) {
zhanyong.wan95b12332009-09-25 18:55:50 +00001044 std::map<int, char> container;
1045 container.insert(std::make_pair(1, 'a'));
1046 container.insert(std::make_pair(2, 'b'));
1047 container.insert(std::make_pair(4, 'c'));
1048 EXPECT_THAT(container, Contains(Pair(1, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001049 EXPECT_THAT(container, Contains(Pair(1, _)));
zhanyong.wan95b12332009-09-25 18:55:50 +00001050 EXPECT_THAT(container, Contains(Pair(_, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001051 EXPECT_THAT(container, Not(Contains(Pair(3, _))));
1052}
1053
shiqiane35fdd92008-12-10 05:08:54 +00001054// Tests StartsWith(s).
1055
1056TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
1057 const Matcher<const char*> m1 = StartsWith(string(""));
1058 EXPECT_TRUE(m1.Matches("Hi"));
1059 EXPECT_TRUE(m1.Matches(""));
1060 EXPECT_FALSE(m1.Matches(NULL));
1061
1062 const Matcher<const string&> m2 = StartsWith("Hi");
1063 EXPECT_TRUE(m2.Matches("Hi"));
1064 EXPECT_TRUE(m2.Matches("Hi Hi!"));
1065 EXPECT_TRUE(m2.Matches("High"));
1066 EXPECT_FALSE(m2.Matches("H"));
1067 EXPECT_FALSE(m2.Matches(" Hi"));
1068}
1069
1070TEST(StartsWithTest, CanDescribeSelf) {
1071 Matcher<const std::string> m = StartsWith("Hi");
1072 EXPECT_EQ("starts with \"Hi\"", Describe(m));
1073}
1074
1075// Tests EndsWith(s).
1076
1077TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
1078 const Matcher<const char*> m1 = EndsWith("");
1079 EXPECT_TRUE(m1.Matches("Hi"));
1080 EXPECT_TRUE(m1.Matches(""));
1081 EXPECT_FALSE(m1.Matches(NULL));
1082
1083 const Matcher<const string&> m2 = EndsWith(string("Hi"));
1084 EXPECT_TRUE(m2.Matches("Hi"));
1085 EXPECT_TRUE(m2.Matches("Wow Hi Hi"));
1086 EXPECT_TRUE(m2.Matches("Super Hi"));
1087 EXPECT_FALSE(m2.Matches("i"));
1088 EXPECT_FALSE(m2.Matches("Hi "));
1089}
1090
1091TEST(EndsWithTest, CanDescribeSelf) {
1092 Matcher<const std::string> m = EndsWith("Hi");
1093 EXPECT_EQ("ends with \"Hi\"", Describe(m));
1094}
1095
1096#ifdef GMOCK_HAS_REGEX
1097
1098// Tests MatchesRegex().
1099
1100TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
1101 const Matcher<const char*> m1 = MatchesRegex("a.*z");
1102 EXPECT_TRUE(m1.Matches("az"));
1103 EXPECT_TRUE(m1.Matches("abcz"));
1104 EXPECT_FALSE(m1.Matches(NULL));
1105
1106 const Matcher<const string&> m2 = MatchesRegex(new RE("a.*z"));
1107 EXPECT_TRUE(m2.Matches("azbz"));
1108 EXPECT_FALSE(m2.Matches("az1"));
1109 EXPECT_FALSE(m2.Matches("1az"));
1110}
1111
1112TEST(MatchesRegexTest, CanDescribeSelf) {
1113 Matcher<const std::string> m1 = MatchesRegex(string("Hi.*"));
1114 EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1));
1115
1116 Matcher<const char*> m2 = MatchesRegex(new RE("[a-z].*"));
1117 EXPECT_EQ("matches regular expression \"[a-z].*\"", Describe(m2));
1118}
1119
1120// Tests ContainsRegex().
1121
1122TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
1123 const Matcher<const char*> m1 = ContainsRegex(string("a.*z"));
1124 EXPECT_TRUE(m1.Matches("az"));
1125 EXPECT_TRUE(m1.Matches("0abcz1"));
1126 EXPECT_FALSE(m1.Matches(NULL));
1127
1128 const Matcher<const string&> m2 = ContainsRegex(new RE("a.*z"));
1129 EXPECT_TRUE(m2.Matches("azbz"));
1130 EXPECT_TRUE(m2.Matches("az1"));
1131 EXPECT_FALSE(m2.Matches("1a"));
1132}
1133
1134TEST(ContainsRegexTest, CanDescribeSelf) {
1135 Matcher<const std::string> m1 = ContainsRegex("Hi.*");
1136 EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1));
1137
1138 Matcher<const char*> m2 = ContainsRegex(new RE("[a-z].*"));
1139 EXPECT_EQ("contains regular expression \"[a-z].*\"", Describe(m2));
1140}
1141#endif // GMOCK_HAS_REGEX
1142
1143// Tests for wide strings.
1144#if GTEST_HAS_STD_WSTRING
1145TEST(StdWideStrEqTest, MatchesEqual) {
1146 Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello"));
1147 EXPECT_TRUE(m.Matches(L"Hello"));
1148 EXPECT_FALSE(m.Matches(L"hello"));
1149 EXPECT_FALSE(m.Matches(NULL));
1150
1151 Matcher<const ::std::wstring&> m2 = StrEq(L"Hello");
1152 EXPECT_TRUE(m2.Matches(L"Hello"));
1153 EXPECT_FALSE(m2.Matches(L"Hi"));
1154
1155 Matcher<const ::std::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1156 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1157 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1158
1159 ::std::wstring str(L"01204500800");
1160 str[3] = L'\0';
1161 Matcher<const ::std::wstring&> m4 = StrEq(str);
1162 EXPECT_TRUE(m4.Matches(str));
1163 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1164 Matcher<const ::std::wstring&> m5 = StrEq(str);
1165 EXPECT_TRUE(m5.Matches(str));
1166}
1167
1168TEST(StdWideStrEqTest, CanDescribeSelf) {
1169 Matcher< ::std::wstring> m = StrEq(L"Hi-\'\"\?\\\a\b\f\n\r\t\v");
1170 EXPECT_EQ("is equal to L\"Hi-\'\\\"\\?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
1171 Describe(m));
1172
1173 Matcher< ::std::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1174 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1175 Describe(m2));
1176
1177 ::std::wstring str(L"01204500800");
1178 str[3] = L'\0';
1179 Matcher<const ::std::wstring&> m4 = StrEq(str);
1180 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1181 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1182 Matcher<const ::std::wstring&> m5 = StrEq(str);
1183 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1184}
1185
1186TEST(StdWideStrNeTest, MatchesUnequalString) {
1187 Matcher<const wchar_t*> m = StrNe(L"Hello");
1188 EXPECT_TRUE(m.Matches(L""));
1189 EXPECT_TRUE(m.Matches(NULL));
1190 EXPECT_FALSE(m.Matches(L"Hello"));
1191
1192 Matcher< ::std::wstring> m2 = StrNe(::std::wstring(L"Hello"));
1193 EXPECT_TRUE(m2.Matches(L"hello"));
1194 EXPECT_FALSE(m2.Matches(L"Hello"));
1195}
1196
1197TEST(StdWideStrNeTest, CanDescribeSelf) {
1198 Matcher<const wchar_t*> m = StrNe(L"Hi");
1199 EXPECT_EQ("is not equal to L\"Hi\"", Describe(m));
1200}
1201
1202TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1203 Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello"));
1204 EXPECT_TRUE(m.Matches(L"Hello"));
1205 EXPECT_TRUE(m.Matches(L"hello"));
1206 EXPECT_FALSE(m.Matches(L"Hi"));
1207 EXPECT_FALSE(m.Matches(NULL));
1208
1209 Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello");
1210 EXPECT_TRUE(m2.Matches(L"hello"));
1211 EXPECT_FALSE(m2.Matches(L"Hi"));
1212}
1213
1214TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1215 ::std::wstring str1(L"oabocdooeoo");
1216 ::std::wstring str2(L"OABOCDOOEOO");
1217 Matcher<const ::std::wstring&> m0 = StrCaseEq(str1);
1218 EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L'\0')));
1219
1220 str1[3] = str2[3] = L'\0';
1221 Matcher<const ::std::wstring&> m1 = StrCaseEq(str1);
1222 EXPECT_TRUE(m1.Matches(str2));
1223
1224 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1225 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1226 Matcher<const ::std::wstring&> m2 = StrCaseEq(str1);
1227 str1[9] = str2[9] = L'\0';
1228 EXPECT_FALSE(m2.Matches(str2));
1229
1230 Matcher<const ::std::wstring&> m3 = StrCaseEq(str1);
1231 EXPECT_TRUE(m3.Matches(str2));
1232
1233 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1234 str2.append(1, L'\0');
1235 EXPECT_FALSE(m3.Matches(str2));
1236 EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9)));
1237}
1238
1239TEST(StdWideStrCaseEqTest, CanDescribeSelf) {
1240 Matcher< ::std::wstring> m = StrCaseEq(L"Hi");
1241 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1242}
1243
1244TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1245 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1246 EXPECT_TRUE(m.Matches(L"Hi"));
1247 EXPECT_TRUE(m.Matches(NULL));
1248 EXPECT_FALSE(m.Matches(L"Hello"));
1249 EXPECT_FALSE(m.Matches(L"hello"));
1250
1251 Matcher< ::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello"));
1252 EXPECT_TRUE(m2.Matches(L""));
1253 EXPECT_FALSE(m2.Matches(L"Hello"));
1254}
1255
1256TEST(StdWideStrCaseNeTest, CanDescribeSelf) {
1257 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
1258 EXPECT_EQ("is not equal to (ignoring case) L\"Hi\"", Describe(m));
1259}
1260
1261// Tests that HasSubstr() works for matching wstring-typed values.
1262TEST(StdWideHasSubstrTest, WorksForStringClasses) {
1263 const Matcher< ::std::wstring> m1 = HasSubstr(L"foo");
1264 EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food.")));
1265 EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo")));
1266
1267 const Matcher<const ::std::wstring&> m2 = HasSubstr(L"foo");
1268 EXPECT_TRUE(m2.Matches(::std::wstring(L"I love food.")));
1269 EXPECT_FALSE(m2.Matches(::std::wstring(L"tofo")));
1270}
1271
1272// Tests that HasSubstr() works for matching C-wide-string-typed values.
1273TEST(StdWideHasSubstrTest, WorksForCStrings) {
1274 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1275 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1276 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1277 EXPECT_FALSE(m1.Matches(NULL));
1278
1279 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1280 EXPECT_TRUE(m2.Matches(L"I love food."));
1281 EXPECT_FALSE(m2.Matches(L"tofo"));
1282 EXPECT_FALSE(m2.Matches(NULL));
1283}
1284
1285// Tests that HasSubstr(s) describes itself properly.
1286TEST(StdWideHasSubstrTest, CanDescribeSelf) {
1287 Matcher< ::std::wstring> m = HasSubstr(L"foo\n\"");
1288 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1289}
1290
1291// Tests StartsWith(s).
1292
1293TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) {
1294 const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L""));
1295 EXPECT_TRUE(m1.Matches(L"Hi"));
1296 EXPECT_TRUE(m1.Matches(L""));
1297 EXPECT_FALSE(m1.Matches(NULL));
1298
1299 const Matcher<const ::std::wstring&> m2 = StartsWith(L"Hi");
1300 EXPECT_TRUE(m2.Matches(L"Hi"));
1301 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1302 EXPECT_TRUE(m2.Matches(L"High"));
1303 EXPECT_FALSE(m2.Matches(L"H"));
1304 EXPECT_FALSE(m2.Matches(L" Hi"));
1305}
1306
1307TEST(StdWideStartsWithTest, CanDescribeSelf) {
1308 Matcher<const ::std::wstring> m = StartsWith(L"Hi");
1309 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1310}
1311
1312// Tests EndsWith(s).
1313
1314TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) {
1315 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1316 EXPECT_TRUE(m1.Matches(L"Hi"));
1317 EXPECT_TRUE(m1.Matches(L""));
1318 EXPECT_FALSE(m1.Matches(NULL));
1319
1320 const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L"Hi"));
1321 EXPECT_TRUE(m2.Matches(L"Hi"));
1322 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1323 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1324 EXPECT_FALSE(m2.Matches(L"i"));
1325 EXPECT_FALSE(m2.Matches(L"Hi "));
1326}
1327
1328TEST(StdWideEndsWithTest, CanDescribeSelf) {
1329 Matcher<const ::std::wstring> m = EndsWith(L"Hi");
1330 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1331}
1332
1333#endif // GTEST_HAS_STD_WSTRING
1334
1335#if GTEST_HAS_GLOBAL_WSTRING
1336TEST(GlobalWideStrEqTest, MatchesEqual) {
1337 Matcher<const wchar_t*> m = StrEq(::wstring(L"Hello"));
1338 EXPECT_TRUE(m.Matches(L"Hello"));
1339 EXPECT_FALSE(m.Matches(L"hello"));
1340 EXPECT_FALSE(m.Matches(NULL));
1341
1342 Matcher<const ::wstring&> m2 = StrEq(L"Hello");
1343 EXPECT_TRUE(m2.Matches(L"Hello"));
1344 EXPECT_FALSE(m2.Matches(L"Hi"));
1345
1346 Matcher<const ::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1347 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1348 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1349
1350 ::wstring str(L"01204500800");
1351 str[3] = L'\0';
1352 Matcher<const ::wstring&> m4 = StrEq(str);
1353 EXPECT_TRUE(m4.Matches(str));
1354 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1355 Matcher<const ::wstring&> m5 = StrEq(str);
1356 EXPECT_TRUE(m5.Matches(str));
1357}
1358
1359TEST(GlobalWideStrEqTest, CanDescribeSelf) {
1360 Matcher< ::wstring> m = StrEq(L"Hi-\'\"\?\\\a\b\f\n\r\t\v");
1361 EXPECT_EQ("is equal to L\"Hi-\'\\\"\\?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
1362 Describe(m));
1363
1364 Matcher< ::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1365 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1366 Describe(m2));
1367
1368 ::wstring str(L"01204500800");
1369 str[3] = L'\0';
1370 Matcher<const ::wstring&> m4 = StrEq(str);
1371 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1372 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1373 Matcher<const ::wstring&> m5 = StrEq(str);
1374 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1375}
1376
1377TEST(GlobalWideStrNeTest, MatchesUnequalString) {
1378 Matcher<const wchar_t*> m = StrNe(L"Hello");
1379 EXPECT_TRUE(m.Matches(L""));
1380 EXPECT_TRUE(m.Matches(NULL));
1381 EXPECT_FALSE(m.Matches(L"Hello"));
1382
1383 Matcher< ::wstring> m2 = StrNe(::wstring(L"Hello"));
1384 EXPECT_TRUE(m2.Matches(L"hello"));
1385 EXPECT_FALSE(m2.Matches(L"Hello"));
1386}
1387
1388TEST(GlobalWideStrNeTest, CanDescribeSelf) {
1389 Matcher<const wchar_t*> m = StrNe(L"Hi");
1390 EXPECT_EQ("is not equal to L\"Hi\"", Describe(m));
1391}
1392
1393TEST(GlobalWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1394 Matcher<const wchar_t*> m = StrCaseEq(::wstring(L"Hello"));
1395 EXPECT_TRUE(m.Matches(L"Hello"));
1396 EXPECT_TRUE(m.Matches(L"hello"));
1397 EXPECT_FALSE(m.Matches(L"Hi"));
1398 EXPECT_FALSE(m.Matches(NULL));
1399
1400 Matcher<const ::wstring&> m2 = StrCaseEq(L"Hello");
1401 EXPECT_TRUE(m2.Matches(L"hello"));
1402 EXPECT_FALSE(m2.Matches(L"Hi"));
1403}
1404
1405TEST(GlobalWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1406 ::wstring str1(L"oabocdooeoo");
1407 ::wstring str2(L"OABOCDOOEOO");
1408 Matcher<const ::wstring&> m0 = StrCaseEq(str1);
1409 EXPECT_FALSE(m0.Matches(str2 + ::wstring(1, L'\0')));
1410
1411 str1[3] = str2[3] = L'\0';
1412 Matcher<const ::wstring&> m1 = StrCaseEq(str1);
1413 EXPECT_TRUE(m1.Matches(str2));
1414
1415 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1416 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1417 Matcher<const ::wstring&> m2 = StrCaseEq(str1);
1418 str1[9] = str2[9] = L'\0';
1419 EXPECT_FALSE(m2.Matches(str2));
1420
1421 Matcher<const ::wstring&> m3 = StrCaseEq(str1);
1422 EXPECT_TRUE(m3.Matches(str2));
1423
1424 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1425 str2.append(1, L'\0');
1426 EXPECT_FALSE(m3.Matches(str2));
1427 EXPECT_FALSE(m3.Matches(::wstring(str2, 0, 9)));
1428}
1429
1430TEST(GlobalWideStrCaseEqTest, CanDescribeSelf) {
1431 Matcher< ::wstring> m = StrCaseEq(L"Hi");
1432 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1433}
1434
1435TEST(GlobalWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1436 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1437 EXPECT_TRUE(m.Matches(L"Hi"));
1438 EXPECT_TRUE(m.Matches(NULL));
1439 EXPECT_FALSE(m.Matches(L"Hello"));
1440 EXPECT_FALSE(m.Matches(L"hello"));
1441
1442 Matcher< ::wstring> m2 = StrCaseNe(::wstring(L"Hello"));
1443 EXPECT_TRUE(m2.Matches(L""));
1444 EXPECT_FALSE(m2.Matches(L"Hello"));
1445}
1446
1447TEST(GlobalWideStrCaseNeTest, CanDescribeSelf) {
1448 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
1449 EXPECT_EQ("is not equal to (ignoring case) L\"Hi\"", Describe(m));
1450}
1451
1452// Tests that HasSubstr() works for matching wstring-typed values.
1453TEST(GlobalWideHasSubstrTest, WorksForStringClasses) {
1454 const Matcher< ::wstring> m1 = HasSubstr(L"foo");
1455 EXPECT_TRUE(m1.Matches(::wstring(L"I love food.")));
1456 EXPECT_FALSE(m1.Matches(::wstring(L"tofo")));
1457
1458 const Matcher<const ::wstring&> m2 = HasSubstr(L"foo");
1459 EXPECT_TRUE(m2.Matches(::wstring(L"I love food.")));
1460 EXPECT_FALSE(m2.Matches(::wstring(L"tofo")));
1461}
1462
1463// Tests that HasSubstr() works for matching C-wide-string-typed values.
1464TEST(GlobalWideHasSubstrTest, WorksForCStrings) {
1465 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1466 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1467 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1468 EXPECT_FALSE(m1.Matches(NULL));
1469
1470 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1471 EXPECT_TRUE(m2.Matches(L"I love food."));
1472 EXPECT_FALSE(m2.Matches(L"tofo"));
1473 EXPECT_FALSE(m2.Matches(NULL));
1474}
1475
1476// Tests that HasSubstr(s) describes itself properly.
1477TEST(GlobalWideHasSubstrTest, CanDescribeSelf) {
1478 Matcher< ::wstring> m = HasSubstr(L"foo\n\"");
1479 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1480}
1481
1482// Tests StartsWith(s).
1483
1484TEST(GlobalWideStartsWithTest, MatchesStringWithGivenPrefix) {
1485 const Matcher<const wchar_t*> m1 = StartsWith(::wstring(L""));
1486 EXPECT_TRUE(m1.Matches(L"Hi"));
1487 EXPECT_TRUE(m1.Matches(L""));
1488 EXPECT_FALSE(m1.Matches(NULL));
1489
1490 const Matcher<const ::wstring&> m2 = StartsWith(L"Hi");
1491 EXPECT_TRUE(m2.Matches(L"Hi"));
1492 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1493 EXPECT_TRUE(m2.Matches(L"High"));
1494 EXPECT_FALSE(m2.Matches(L"H"));
1495 EXPECT_FALSE(m2.Matches(L" Hi"));
1496}
1497
1498TEST(GlobalWideStartsWithTest, CanDescribeSelf) {
1499 Matcher<const ::wstring> m = StartsWith(L"Hi");
1500 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1501}
1502
1503// Tests EndsWith(s).
1504
1505TEST(GlobalWideEndsWithTest, MatchesStringWithGivenSuffix) {
1506 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1507 EXPECT_TRUE(m1.Matches(L"Hi"));
1508 EXPECT_TRUE(m1.Matches(L""));
1509 EXPECT_FALSE(m1.Matches(NULL));
1510
1511 const Matcher<const ::wstring&> m2 = EndsWith(::wstring(L"Hi"));
1512 EXPECT_TRUE(m2.Matches(L"Hi"));
1513 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1514 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1515 EXPECT_FALSE(m2.Matches(L"i"));
1516 EXPECT_FALSE(m2.Matches(L"Hi "));
1517}
1518
1519TEST(GlobalWideEndsWithTest, CanDescribeSelf) {
1520 Matcher<const ::wstring> m = EndsWith(L"Hi");
1521 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1522}
1523
1524#endif // GTEST_HAS_GLOBAL_WSTRING
1525
1526
1527typedef ::std::tr1::tuple<long, int> Tuple2; // NOLINT
1528
1529// Tests that Eq() matches a 2-tuple where the first field == the
1530// second field.
1531TEST(Eq2Test, MatchesEqualArguments) {
1532 Matcher<const Tuple2&> m = Eq();
1533 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1534 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1535}
1536
1537// Tests that Eq() describes itself properly.
1538TEST(Eq2Test, CanDescribeSelf) {
1539 Matcher<const Tuple2&> m = Eq();
zhanyong.wan2661c682009-06-09 05:42:12 +00001540 EXPECT_EQ("are a pair (x, y) where x == y", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001541}
1542
1543// Tests that Ge() matches a 2-tuple where the first field >= the
1544// second field.
1545TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) {
1546 Matcher<const Tuple2&> m = Ge();
1547 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1548 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1549 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1550}
1551
1552// Tests that Ge() describes itself properly.
1553TEST(Ge2Test, CanDescribeSelf) {
1554 Matcher<const Tuple2&> m = Ge();
zhanyong.wan2661c682009-06-09 05:42:12 +00001555 EXPECT_EQ("are a pair (x, y) where x >= y", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001556}
1557
1558// Tests that Gt() matches a 2-tuple where the first field > the
1559// second field.
1560TEST(Gt2Test, MatchesGreaterThanArguments) {
1561 Matcher<const Tuple2&> m = Gt();
1562 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1563 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1564 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1565}
1566
1567// Tests that Gt() describes itself properly.
1568TEST(Gt2Test, CanDescribeSelf) {
1569 Matcher<const Tuple2&> m = Gt();
zhanyong.wan2661c682009-06-09 05:42:12 +00001570 EXPECT_EQ("are a pair (x, y) where x > y", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001571}
1572
1573// Tests that Le() matches a 2-tuple where the first field <= the
1574// second field.
1575TEST(Le2Test, MatchesLessThanOrEqualArguments) {
1576 Matcher<const Tuple2&> m = Le();
1577 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1578 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1579 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1580}
1581
1582// Tests that Le() describes itself properly.
1583TEST(Le2Test, CanDescribeSelf) {
1584 Matcher<const Tuple2&> m = Le();
zhanyong.wan2661c682009-06-09 05:42:12 +00001585 EXPECT_EQ("are a pair (x, y) where x <= y", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001586}
1587
1588// Tests that Lt() matches a 2-tuple where the first field < the
1589// second field.
1590TEST(Lt2Test, MatchesLessThanArguments) {
1591 Matcher<const Tuple2&> m = Lt();
1592 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1593 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1594 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1595}
1596
1597// Tests that Lt() describes itself properly.
1598TEST(Lt2Test, CanDescribeSelf) {
1599 Matcher<const Tuple2&> m = Lt();
zhanyong.wan2661c682009-06-09 05:42:12 +00001600 EXPECT_EQ("are a pair (x, y) where x < y", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001601}
1602
1603// Tests that Ne() matches a 2-tuple where the first field != the
1604// second field.
1605TEST(Ne2Test, MatchesUnequalArguments) {
1606 Matcher<const Tuple2&> m = Ne();
1607 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1608 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1609 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1610}
1611
1612// Tests that Ne() describes itself properly.
1613TEST(Ne2Test, CanDescribeSelf) {
1614 Matcher<const Tuple2&> m = Ne();
zhanyong.wan2661c682009-06-09 05:42:12 +00001615 EXPECT_EQ("are a pair (x, y) where x != y", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001616}
1617
1618// Tests that Not(m) matches any value that doesn't match m.
1619TEST(NotTest, NegatesMatcher) {
1620 Matcher<int> m;
1621 m = Not(Eq(2));
1622 EXPECT_TRUE(m.Matches(3));
1623 EXPECT_FALSE(m.Matches(2));
1624}
1625
1626// Tests that Not(m) describes itself properly.
1627TEST(NotTest, CanDescribeSelf) {
1628 Matcher<int> m = Not(Eq(5));
1629 EXPECT_EQ("is not equal to 5", Describe(m));
1630}
1631
zhanyong.wan18490652009-05-11 18:54:08 +00001632// Tests that monomorphic matchers are safely cast by the Not matcher.
1633TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {
1634 // greater_than_5 is a monomorphic matcher.
1635 Matcher<int> greater_than_5 = Gt(5);
1636
1637 Matcher<const int&> m = Not(greater_than_5);
1638 Matcher<int&> m2 = Not(greater_than_5);
1639 Matcher<int&> m3 = Not(m);
1640}
1641
shiqiane35fdd92008-12-10 05:08:54 +00001642// Tests that AllOf(m1, ..., mn) matches any value that matches all of
1643// the given matchers.
1644TEST(AllOfTest, MatchesWhenAllMatch) {
1645 Matcher<int> m;
1646 m = AllOf(Le(2), Ge(1));
1647 EXPECT_TRUE(m.Matches(1));
1648 EXPECT_TRUE(m.Matches(2));
1649 EXPECT_FALSE(m.Matches(0));
1650 EXPECT_FALSE(m.Matches(3));
1651
1652 m = AllOf(Gt(0), Ne(1), Ne(2));
1653 EXPECT_TRUE(m.Matches(3));
1654 EXPECT_FALSE(m.Matches(2));
1655 EXPECT_FALSE(m.Matches(1));
1656 EXPECT_FALSE(m.Matches(0));
1657
1658 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
1659 EXPECT_TRUE(m.Matches(4));
1660 EXPECT_FALSE(m.Matches(3));
1661 EXPECT_FALSE(m.Matches(2));
1662 EXPECT_FALSE(m.Matches(1));
1663 EXPECT_FALSE(m.Matches(0));
1664
1665 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
1666 EXPECT_TRUE(m.Matches(0));
1667 EXPECT_TRUE(m.Matches(1));
1668 EXPECT_FALSE(m.Matches(3));
1669}
1670
1671// Tests that AllOf(m1, ..., mn) describes itself properly.
1672TEST(AllOfTest, CanDescribeSelf) {
1673 Matcher<int> m;
1674 m = AllOf(Le(2), Ge(1));
1675 EXPECT_EQ("(is less than or equal to 2) and "
1676 "(is greater than or equal to 1)",
1677 Describe(m));
1678
1679 m = AllOf(Gt(0), Ne(1), Ne(2));
1680 EXPECT_EQ("(is greater than 0) and "
1681 "((is not equal to 1) and "
1682 "(is not equal to 2))",
1683 Describe(m));
1684
1685
1686 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
1687 EXPECT_EQ("(is greater than 0) and "
1688 "((is not equal to 1) and "
1689 "((is not equal to 2) and "
1690 "(is not equal to 3)))",
1691 Describe(m));
1692
1693
1694 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
1695 EXPECT_EQ("(is greater than or equal to 0) and "
1696 "((is less than 10) and "
1697 "((is not equal to 3) and "
1698 "((is not equal to 5) and "
1699 "(is not equal to 7))))", Describe(m));
1700}
1701
zhanyong.wan18490652009-05-11 18:54:08 +00001702// Tests that monomorphic matchers are safely cast by the AllOf matcher.
1703TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
1704 // greater_than_5 and less_than_10 are monomorphic matchers.
1705 Matcher<int> greater_than_5 = Gt(5);
1706 Matcher<int> less_than_10 = Lt(10);
1707
1708 Matcher<const int&> m = AllOf(greater_than_5, less_than_10);
1709 Matcher<int&> m2 = AllOf(greater_than_5, less_than_10);
1710 Matcher<int&> m3 = AllOf(greater_than_5, m2);
1711
1712 // Tests that BothOf works when composing itself.
1713 Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10);
1714 Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);
1715}
1716
shiqiane35fdd92008-12-10 05:08:54 +00001717// Tests that AnyOf(m1, ..., mn) matches any value that matches at
1718// least one of the given matchers.
1719TEST(AnyOfTest, MatchesWhenAnyMatches) {
1720 Matcher<int> m;
1721 m = AnyOf(Le(1), Ge(3));
1722 EXPECT_TRUE(m.Matches(1));
1723 EXPECT_TRUE(m.Matches(4));
1724 EXPECT_FALSE(m.Matches(2));
1725
1726 m = AnyOf(Lt(0), Eq(1), Eq(2));
1727 EXPECT_TRUE(m.Matches(-1));
1728 EXPECT_TRUE(m.Matches(1));
1729 EXPECT_TRUE(m.Matches(2));
1730 EXPECT_FALSE(m.Matches(0));
1731
1732 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
1733 EXPECT_TRUE(m.Matches(-1));
1734 EXPECT_TRUE(m.Matches(1));
1735 EXPECT_TRUE(m.Matches(2));
1736 EXPECT_TRUE(m.Matches(3));
1737 EXPECT_FALSE(m.Matches(0));
1738
1739 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
1740 EXPECT_TRUE(m.Matches(0));
1741 EXPECT_TRUE(m.Matches(11));
1742 EXPECT_TRUE(m.Matches(3));
1743 EXPECT_FALSE(m.Matches(2));
1744}
1745
1746// Tests that AnyOf(m1, ..., mn) describes itself properly.
1747TEST(AnyOfTest, CanDescribeSelf) {
1748 Matcher<int> m;
1749 m = AnyOf(Le(1), Ge(3));
1750 EXPECT_EQ("(is less than or equal to 1) or "
1751 "(is greater than or equal to 3)",
1752 Describe(m));
1753
1754 m = AnyOf(Lt(0), Eq(1), Eq(2));
1755 EXPECT_EQ("(is less than 0) or "
1756 "((is equal to 1) or (is equal to 2))",
1757 Describe(m));
1758
1759 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
1760 EXPECT_EQ("(is less than 0) or "
1761 "((is equal to 1) or "
1762 "((is equal to 2) or "
1763 "(is equal to 3)))",
1764 Describe(m));
1765
1766 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
1767 EXPECT_EQ("(is less than or equal to 0) or "
1768 "((is greater than 10) or "
1769 "((is equal to 3) or "
1770 "((is equal to 5) or "
1771 "(is equal to 7))))",
1772 Describe(m));
1773}
1774
zhanyong.wan18490652009-05-11 18:54:08 +00001775// Tests that monomorphic matchers are safely cast by the AnyOf matcher.
1776TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
1777 // greater_than_5 and less_than_10 are monomorphic matchers.
1778 Matcher<int> greater_than_5 = Gt(5);
1779 Matcher<int> less_than_10 = Lt(10);
1780
1781 Matcher<const int&> m = AnyOf(greater_than_5, less_than_10);
1782 Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10);
1783 Matcher<int&> m3 = AnyOf(greater_than_5, m2);
1784
1785 // Tests that EitherOf works when composing itself.
1786 Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10);
1787 Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);
1788}
1789
shiqiane35fdd92008-12-10 05:08:54 +00001790// The following predicate function and predicate functor are for
1791// testing the Truly(predicate) matcher.
1792
1793// Returns non-zero if the input is positive. Note that the return
1794// type of this function is not bool. It's OK as Truly() accepts any
1795// unary function or functor whose return type can be implicitly
1796// converted to bool.
1797int IsPositive(double x) {
1798 return x > 0 ? 1 : 0;
1799}
1800
1801// This functor returns true if the input is greater than the given
1802// number.
1803class IsGreaterThan {
1804 public:
1805 explicit IsGreaterThan(int threshold) : threshold_(threshold) {}
1806
1807 bool operator()(int n) const { return n > threshold_; }
1808 private:
1809 const int threshold_;
1810};
1811
1812// For testing Truly().
1813const int foo = 0;
1814
1815// This predicate returns true iff the argument references foo and has
1816// a zero value.
1817bool ReferencesFooAndIsZero(const int& n) {
1818 return (&n == &foo) && (n == 0);
1819}
1820
1821// Tests that Truly(predicate) matches what satisfies the given
1822// predicate.
1823TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {
1824 Matcher<double> m = Truly(IsPositive);
1825 EXPECT_TRUE(m.Matches(2.0));
1826 EXPECT_FALSE(m.Matches(-1.5));
1827}
1828
1829// Tests that Truly(predicate_functor) works too.
1830TEST(TrulyTest, CanBeUsedWithFunctor) {
1831 Matcher<int> m = Truly(IsGreaterThan(5));
1832 EXPECT_TRUE(m.Matches(6));
1833 EXPECT_FALSE(m.Matches(4));
1834}
1835
1836// Tests that Truly(predicate) can describe itself properly.
1837TEST(TrulyTest, CanDescribeSelf) {
1838 Matcher<double> m = Truly(IsPositive);
1839 EXPECT_EQ("satisfies the given predicate",
1840 Describe(m));
1841}
1842
1843// Tests that Truly(predicate) works when the matcher takes its
1844// argument by reference.
1845TEST(TrulyTest, WorksForByRefArguments) {
1846 Matcher<const int&> m = Truly(ReferencesFooAndIsZero);
1847 EXPECT_TRUE(m.Matches(foo));
1848 int n = 0;
1849 EXPECT_FALSE(m.Matches(n));
1850}
1851
1852// Tests that Matches(m) is a predicate satisfied by whatever that
1853// matches matcher m.
1854TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {
1855 EXPECT_TRUE(Matches(Ge(0))(1));
1856 EXPECT_FALSE(Matches(Eq('a'))('b'));
1857}
1858
1859// Tests that Matches(m) works when the matcher takes its argument by
1860// reference.
1861TEST(MatchesTest, WorksOnByRefArguments) {
1862 int m = 0, n = 0;
1863 EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n));
1864 EXPECT_FALSE(Matches(Ref(m))(n));
1865}
1866
1867// Tests that a Matcher on non-reference type can be used in
1868// Matches().
1869TEST(MatchesTest, WorksWithMatcherOnNonRefType) {
1870 Matcher<int> eq5 = Eq(5);
1871 EXPECT_TRUE(Matches(eq5)(5));
1872 EXPECT_FALSE(Matches(eq5)(2));
1873}
1874
zhanyong.wanb8243162009-06-04 05:48:20 +00001875// Tests Value(value, matcher). Since Value() is a simple wrapper for
1876// Matches(), which has been tested already, we don't spend a lot of
1877// effort on testing Value().
1878TEST(ValueTest, WorksWithPolymorphicMatcher) {
1879 EXPECT_TRUE(Value("hi", StartsWith("h")));
1880 EXPECT_FALSE(Value(5, Gt(10)));
1881}
1882
1883TEST(ValueTest, WorksWithMonomorphicMatcher) {
1884 const Matcher<int> is_zero = Eq(0);
1885 EXPECT_TRUE(Value(0, is_zero));
1886 EXPECT_FALSE(Value('a', is_zero));
1887
1888 int n = 0;
1889 const Matcher<const int&> ref_n = Ref(n);
1890 EXPECT_TRUE(Value(n, ref_n));
1891 EXPECT_FALSE(Value(1, ref_n));
1892}
1893
zhanyong.wanbf550852009-06-09 06:09:53 +00001894TEST(AllArgsTest, WorksForTuple) {
1895 EXPECT_THAT(make_tuple(1, 2L), AllArgs(Lt()));
1896 EXPECT_THAT(make_tuple(2L, 1), Not(AllArgs(Lt())));
1897}
1898
1899TEST(AllArgsTest, WorksForNonTuple) {
1900 EXPECT_THAT(42, AllArgs(Gt(0)));
1901 EXPECT_THAT('a', Not(AllArgs(Eq('b'))));
1902}
1903
1904class AllArgsHelper {
1905 public:
1906 MOCK_METHOD2(Helper, int(char x, int y));
1907};
1908
1909TEST(AllArgsTest, WorksInWithClause) {
1910 AllArgsHelper helper;
1911 ON_CALL(helper, Helper(_, _))
1912 .With(AllArgs(Lt()))
1913 .WillByDefault(Return(1));
1914 EXPECT_CALL(helper, Helper(_, _));
1915 EXPECT_CALL(helper, Helper(_, _))
1916 .With(AllArgs(Gt()))
1917 .WillOnce(Return(2));
1918
1919 EXPECT_EQ(1, helper.Helper('\1', 2));
1920 EXPECT_EQ(2, helper.Helper('a', 1));
1921}
1922
shiqiane35fdd92008-12-10 05:08:54 +00001923// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
1924// matches the matcher.
1925TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {
1926 ASSERT_THAT(5, Ge(2)) << "This should succeed.";
1927 ASSERT_THAT("Foo", EndsWith("oo"));
1928 EXPECT_THAT(2, AllOf(Le(7), Ge(0))) << "This should succeed too.";
1929 EXPECT_THAT("Hello", StartsWith("Hell"));
1930}
1931
1932// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
1933// doesn't match the matcher.
1934TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {
1935 // 'n' must be static as it is used in an EXPECT_FATAL_FAILURE(),
1936 // which cannot reference auto variables.
1937 static int n;
1938 n = 5;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00001939
1940 // VC++ prior to version 8.0 SP1 has a bug where it will not see any
1941 // functions declared in the namespace scope from within nested classes.
1942 // EXPECT/ASSERT_(NON)FATAL_FAILURE macros use nested classes so that all
1943 // namespace-level functions invoked inside them need to be explicitly
1944 // resolved.
1945 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Gt(10)),
shiqiane35fdd92008-12-10 05:08:54 +00001946 "Value of: n\n"
1947 "Expected: is greater than 10\n"
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00001948 " Actual: 5");
shiqiane35fdd92008-12-10 05:08:54 +00001949 n = 0;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00001950 EXPECT_NONFATAL_FAILURE(
1951 EXPECT_THAT(n, ::testing::AllOf(::testing::Le(7), ::testing::Ge(5))),
1952 "Value of: n\n"
1953 "Expected: (is less than or equal to 7) and "
1954 "(is greater than or equal to 5)\n"
1955 " Actual: 0");
shiqiane35fdd92008-12-10 05:08:54 +00001956}
1957
1958// Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument
1959// has a reference type.
1960TEST(MatcherAssertionTest, WorksForByRefArguments) {
1961 // We use a static variable here as EXPECT_FATAL_FAILURE() cannot
1962 // reference auto variables.
1963 static int n;
1964 n = 0;
1965 EXPECT_THAT(n, AllOf(Le(7), Ref(n)));
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00001966 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
shiqiane35fdd92008-12-10 05:08:54 +00001967 "Value of: n\n"
1968 "Expected: does not reference the variable @");
1969 // Tests the "Actual" part.
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00001970 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
shiqiane35fdd92008-12-10 05:08:54 +00001971 "Actual: 0 (is located @");
1972}
1973
zhanyong.wan95b12332009-09-25 18:55:50 +00001974#if !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00001975// Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is
1976// monomorphic.
zhanyong.wan95b12332009-09-25 18:55:50 +00001977
1978// ASSERT_THAT("hello", starts_with_he) fails to compile with Nokia's
1979// Symbian compiler: it tries to compile
1980// template<T, U> class MatcherCastImpl { ...
1981// virtual bool Matches(T x) const {
1982// return source_matcher_.Matches(static_cast<U>(x));
1983// with U == string and T == const char*
1984// With ASSERT_THAT("hello"...) changed to ASSERT_THAT(string("hello") ... )
1985// the compiler silently crashes with no output.
1986// If MatcherCastImpl is changed to use U(x) instead of static_cast<U>(x)
1987// the code compiles but the converted string is bogus.
shiqiane35fdd92008-12-10 05:08:54 +00001988TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
1989 Matcher<const char*> starts_with_he = StartsWith("he");
1990 ASSERT_THAT("hello", starts_with_he);
1991
1992 Matcher<const string&> ends_with_ok = EndsWith("ok");
1993 ASSERT_THAT("book", ends_with_ok);
1994
1995 Matcher<int> is_greater_than_5 = Gt(5);
1996 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5),
1997 "Value of: 5\n"
1998 "Expected: is greater than 5\n"
1999 " Actual: 5");
2000}
zhanyong.wan95b12332009-09-25 18:55:50 +00002001#endif // !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002002
2003// Tests floating-point matchers.
2004template <typename RawType>
2005class FloatingPointTest : public testing::Test {
2006 protected:
2007 typedef typename testing::internal::FloatingPoint<RawType> Floating;
2008 typedef typename Floating::Bits Bits;
2009
2010 virtual void SetUp() {
2011 const size_t max_ulps = Floating::kMaxUlps;
2012
2013 // The bits that represent 0.0.
2014 const Bits zero_bits = Floating(0).bits();
2015
2016 // Makes some numbers close to 0.0.
2017 close_to_positive_zero_ = Floating::ReinterpretBits(zero_bits + max_ulps/2);
2018 close_to_negative_zero_ = -Floating::ReinterpretBits(
2019 zero_bits + max_ulps - max_ulps/2);
2020 further_from_negative_zero_ = -Floating::ReinterpretBits(
2021 zero_bits + max_ulps + 1 - max_ulps/2);
2022
2023 // The bits that represent 1.0.
2024 const Bits one_bits = Floating(1).bits();
2025
2026 // Makes some numbers close to 1.0.
2027 close_to_one_ = Floating::ReinterpretBits(one_bits + max_ulps);
2028 further_from_one_ = Floating::ReinterpretBits(one_bits + max_ulps + 1);
2029
2030 // +infinity.
2031 infinity_ = Floating::Infinity();
2032
2033 // The bits that represent +infinity.
2034 const Bits infinity_bits = Floating(infinity_).bits();
2035
2036 // Makes some numbers close to infinity.
2037 close_to_infinity_ = Floating::ReinterpretBits(infinity_bits - max_ulps);
2038 further_from_infinity_ = Floating::ReinterpretBits(
2039 infinity_bits - max_ulps - 1);
2040
2041 // Makes some NAN's.
2042 nan1_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 1);
2043 nan2_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 200);
2044 }
2045
2046 void TestSize() {
2047 EXPECT_EQ(sizeof(RawType), sizeof(Bits));
2048 }
2049
2050 // A battery of tests for FloatingEqMatcher::Matches.
2051 // matcher_maker is a pointer to a function which creates a FloatingEqMatcher.
2052 void TestMatches(
2053 testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) {
2054 Matcher<RawType> m1 = matcher_maker(0.0);
2055 EXPECT_TRUE(m1.Matches(-0.0));
2056 EXPECT_TRUE(m1.Matches(close_to_positive_zero_));
2057 EXPECT_TRUE(m1.Matches(close_to_negative_zero_));
2058 EXPECT_FALSE(m1.Matches(1.0));
2059
2060 Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_);
2061 EXPECT_FALSE(m2.Matches(further_from_negative_zero_));
2062
2063 Matcher<RawType> m3 = matcher_maker(1.0);
2064 EXPECT_TRUE(m3.Matches(close_to_one_));
2065 EXPECT_FALSE(m3.Matches(further_from_one_));
2066
2067 // Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above.
2068 EXPECT_FALSE(m3.Matches(0.0));
2069
2070 Matcher<RawType> m4 = matcher_maker(-infinity_);
2071 EXPECT_TRUE(m4.Matches(-close_to_infinity_));
2072
2073 Matcher<RawType> m5 = matcher_maker(infinity_);
2074 EXPECT_TRUE(m5.Matches(close_to_infinity_));
2075
2076 // This is interesting as the representations of infinity_ and nan1_
2077 // are only 1 DLP apart.
2078 EXPECT_FALSE(m5.Matches(nan1_));
2079
2080 // matcher_maker can produce a Matcher<const RawType&>, which is needed in
2081 // some cases.
2082 Matcher<const RawType&> m6 = matcher_maker(0.0);
2083 EXPECT_TRUE(m6.Matches(-0.0));
2084 EXPECT_TRUE(m6.Matches(close_to_positive_zero_));
2085 EXPECT_FALSE(m6.Matches(1.0));
2086
2087 // matcher_maker can produce a Matcher<RawType&>, which is needed in some
2088 // cases.
2089 Matcher<RawType&> m7 = matcher_maker(0.0);
2090 RawType x = 0.0;
2091 EXPECT_TRUE(m7.Matches(x));
2092 x = 0.01f;
2093 EXPECT_FALSE(m7.Matches(x));
2094 }
2095
2096 // Pre-calculated numbers to be used by the tests.
2097
2098 static RawType close_to_positive_zero_;
2099 static RawType close_to_negative_zero_;
2100 static RawType further_from_negative_zero_;
2101
2102 static RawType close_to_one_;
2103 static RawType further_from_one_;
2104
2105 static RawType infinity_;
2106 static RawType close_to_infinity_;
2107 static RawType further_from_infinity_;
2108
2109 static RawType nan1_;
2110 static RawType nan2_;
2111};
2112
2113template <typename RawType>
2114RawType FloatingPointTest<RawType>::close_to_positive_zero_;
2115
2116template <typename RawType>
2117RawType FloatingPointTest<RawType>::close_to_negative_zero_;
2118
2119template <typename RawType>
2120RawType FloatingPointTest<RawType>::further_from_negative_zero_;
2121
2122template <typename RawType>
2123RawType FloatingPointTest<RawType>::close_to_one_;
2124
2125template <typename RawType>
2126RawType FloatingPointTest<RawType>::further_from_one_;
2127
2128template <typename RawType>
2129RawType FloatingPointTest<RawType>::infinity_;
2130
2131template <typename RawType>
2132RawType FloatingPointTest<RawType>::close_to_infinity_;
2133
2134template <typename RawType>
2135RawType FloatingPointTest<RawType>::further_from_infinity_;
2136
2137template <typename RawType>
2138RawType FloatingPointTest<RawType>::nan1_;
2139
2140template <typename RawType>
2141RawType FloatingPointTest<RawType>::nan2_;
2142
2143// Instantiate FloatingPointTest for testing floats.
2144typedef FloatingPointTest<float> FloatTest;
2145
2146TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) {
2147 TestMatches(&FloatEq);
2148}
2149
2150TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
2151 TestMatches(&NanSensitiveFloatEq);
2152}
2153
2154TEST_F(FloatTest, FloatEqCannotMatchNaN) {
2155 // FloatEq never matches NaN.
2156 Matcher<float> m = FloatEq(nan1_);
2157 EXPECT_FALSE(m.Matches(nan1_));
2158 EXPECT_FALSE(m.Matches(nan2_));
2159 EXPECT_FALSE(m.Matches(1.0));
2160}
2161
2162TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {
2163 // NanSensitiveFloatEq will match NaN.
2164 Matcher<float> m = NanSensitiveFloatEq(nan1_);
2165 EXPECT_TRUE(m.Matches(nan1_));
2166 EXPECT_TRUE(m.Matches(nan2_));
2167 EXPECT_FALSE(m.Matches(1.0));
2168}
2169
2170TEST_F(FloatTest, FloatEqCanDescribeSelf) {
2171 Matcher<float> m1 = FloatEq(2.0f);
2172 EXPECT_EQ("is approximately 2", Describe(m1));
2173 EXPECT_EQ("is not approximately 2", DescribeNegation(m1));
2174
2175 Matcher<float> m2 = FloatEq(0.5f);
2176 EXPECT_EQ("is approximately 0.5", Describe(m2));
2177 EXPECT_EQ("is not approximately 0.5", DescribeNegation(m2));
2178
2179 Matcher<float> m3 = FloatEq(nan1_);
2180 EXPECT_EQ("never matches", Describe(m3));
2181 EXPECT_EQ("is anything", DescribeNegation(m3));
2182}
2183
2184TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {
2185 Matcher<float> m1 = NanSensitiveFloatEq(2.0f);
2186 EXPECT_EQ("is approximately 2", Describe(m1));
2187 EXPECT_EQ("is not approximately 2", DescribeNegation(m1));
2188
2189 Matcher<float> m2 = NanSensitiveFloatEq(0.5f);
2190 EXPECT_EQ("is approximately 0.5", Describe(m2));
2191 EXPECT_EQ("is not approximately 0.5", DescribeNegation(m2));
2192
2193 Matcher<float> m3 = NanSensitiveFloatEq(nan1_);
2194 EXPECT_EQ("is NaN", Describe(m3));
2195 EXPECT_EQ("is not NaN", DescribeNegation(m3));
2196}
2197
2198// Instantiate FloatingPointTest for testing doubles.
2199typedef FloatingPointTest<double> DoubleTest;
2200
2201TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
2202 TestMatches(&DoubleEq);
2203}
2204
2205TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
2206 TestMatches(&NanSensitiveDoubleEq);
2207}
2208
2209TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {
2210 // DoubleEq never matches NaN.
2211 Matcher<double> m = DoubleEq(nan1_);
2212 EXPECT_FALSE(m.Matches(nan1_));
2213 EXPECT_FALSE(m.Matches(nan2_));
2214 EXPECT_FALSE(m.Matches(1.0));
2215}
2216
2217TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {
2218 // NanSensitiveDoubleEq will match NaN.
2219 Matcher<double> m = NanSensitiveDoubleEq(nan1_);
2220 EXPECT_TRUE(m.Matches(nan1_));
2221 EXPECT_TRUE(m.Matches(nan2_));
2222 EXPECT_FALSE(m.Matches(1.0));
2223}
2224
2225TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {
2226 Matcher<double> m1 = DoubleEq(2.0);
2227 EXPECT_EQ("is approximately 2", Describe(m1));
2228 EXPECT_EQ("is not approximately 2", DescribeNegation(m1));
2229
2230 Matcher<double> m2 = DoubleEq(0.5);
2231 EXPECT_EQ("is approximately 0.5", Describe(m2));
2232 EXPECT_EQ("is not approximately 0.5", DescribeNegation(m2));
2233
2234 Matcher<double> m3 = DoubleEq(nan1_);
2235 EXPECT_EQ("never matches", Describe(m3));
2236 EXPECT_EQ("is anything", DescribeNegation(m3));
2237}
2238
2239TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {
2240 Matcher<double> m1 = NanSensitiveDoubleEq(2.0);
2241 EXPECT_EQ("is approximately 2", Describe(m1));
2242 EXPECT_EQ("is not approximately 2", DescribeNegation(m1));
2243
2244 Matcher<double> m2 = NanSensitiveDoubleEq(0.5);
2245 EXPECT_EQ("is approximately 0.5", Describe(m2));
2246 EXPECT_EQ("is not approximately 0.5", DescribeNegation(m2));
2247
2248 Matcher<double> m3 = NanSensitiveDoubleEq(nan1_);
2249 EXPECT_EQ("is NaN", Describe(m3));
2250 EXPECT_EQ("is not NaN", DescribeNegation(m3));
2251}
2252
2253TEST(PointeeTest, RawPointer) {
2254 const Matcher<int*> m = Pointee(Ge(0));
2255
2256 int n = 1;
2257 EXPECT_TRUE(m.Matches(&n));
2258 n = -1;
2259 EXPECT_FALSE(m.Matches(&n));
2260 EXPECT_FALSE(m.Matches(NULL));
2261}
2262
2263TEST(PointeeTest, RawPointerToConst) {
2264 const Matcher<const double*> m = Pointee(Ge(0));
2265
2266 double x = 1;
2267 EXPECT_TRUE(m.Matches(&x));
2268 x = -1;
2269 EXPECT_FALSE(m.Matches(&x));
2270 EXPECT_FALSE(m.Matches(NULL));
2271}
2272
2273TEST(PointeeTest, ReferenceToConstRawPointer) {
2274 const Matcher<int* const &> m = Pointee(Ge(0));
2275
2276 int n = 1;
2277 EXPECT_TRUE(m.Matches(&n));
2278 n = -1;
2279 EXPECT_FALSE(m.Matches(&n));
2280 EXPECT_FALSE(m.Matches(NULL));
2281}
2282
2283TEST(PointeeTest, ReferenceToNonConstRawPointer) {
2284 const Matcher<double* &> m = Pointee(Ge(0));
2285
2286 double x = 1.0;
2287 double* p = &x;
2288 EXPECT_TRUE(m.Matches(p));
2289 x = -1;
2290 EXPECT_FALSE(m.Matches(p));
2291 p = NULL;
2292 EXPECT_FALSE(m.Matches(p));
2293}
2294
2295TEST(PointeeTest, NeverMatchesNull) {
2296 const Matcher<const char*> m = Pointee(_);
2297 EXPECT_FALSE(m.Matches(NULL));
2298}
2299
2300// Tests that we can write Pointee(value) instead of Pointee(Eq(value)).
2301TEST(PointeeTest, MatchesAgainstAValue) {
2302 const Matcher<int*> m = Pointee(5);
2303
2304 int n = 5;
2305 EXPECT_TRUE(m.Matches(&n));
2306 n = -1;
2307 EXPECT_FALSE(m.Matches(&n));
2308 EXPECT_FALSE(m.Matches(NULL));
2309}
2310
2311TEST(PointeeTest, CanDescribeSelf) {
2312 const Matcher<int*> m = Pointee(Gt(3));
2313 EXPECT_EQ("points to a value that is greater than 3", Describe(m));
2314 EXPECT_EQ("does not point to a value that is greater than 3",
2315 DescribeNegation(m));
2316}
2317
shiqiane35fdd92008-12-10 05:08:54 +00002318TEST(PointeeTest, CanExplainMatchResult) {
2319 const Matcher<const string*> m = Pointee(StartsWith("Hi"));
2320
2321 EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL)));
2322
2323 const Matcher<int*> m2 = Pointee(GreaterThan(1));
2324 int n = 3;
2325 EXPECT_EQ("points to a value that is 2 more than 1", Explain(m2, &n));
2326}
2327
2328// An uncopyable class.
2329class Uncopyable {
2330 public:
2331 explicit Uncopyable(int value) : value_(value) {}
2332
2333 int value() const { return value_; }
2334 private:
2335 const int value_;
2336 GTEST_DISALLOW_COPY_AND_ASSIGN_(Uncopyable);
2337};
2338
2339// Returns true iff x.value() is positive.
2340bool ValueIsPositive(const Uncopyable& x) { return x.value() > 0; }
2341
2342// A user-defined struct for testing Field().
2343struct AStruct {
2344 AStruct() : x(0), y(1.0), z(5), p(NULL) {}
2345 AStruct(const AStruct& rhs)
2346 : x(rhs.x), y(rhs.y), z(rhs.z.value()), p(rhs.p) {}
2347
2348 int x; // A non-const field.
2349 const double y; // A const field.
2350 Uncopyable z; // An uncopyable field.
2351 const char* p; // A pointer field.
2352};
2353
2354// A derived struct for testing Field().
2355struct DerivedStruct : public AStruct {
2356 char ch;
2357};
2358
2359// Tests that Field(&Foo::field, ...) works when field is non-const.
2360TEST(FieldTest, WorksForNonConstField) {
2361 Matcher<AStruct> m = Field(&AStruct::x, Ge(0));
2362
2363 AStruct a;
2364 EXPECT_TRUE(m.Matches(a));
2365 a.x = -1;
2366 EXPECT_FALSE(m.Matches(a));
2367}
2368
2369// Tests that Field(&Foo::field, ...) works when field is const.
2370TEST(FieldTest, WorksForConstField) {
2371 AStruct a;
2372
2373 Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0));
2374 EXPECT_TRUE(m.Matches(a));
2375 m = Field(&AStruct::y, Le(0.0));
2376 EXPECT_FALSE(m.Matches(a));
2377}
2378
2379// Tests that Field(&Foo::field, ...) works when field is not copyable.
2380TEST(FieldTest, WorksForUncopyableField) {
2381 AStruct a;
2382
2383 Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive));
2384 EXPECT_TRUE(m.Matches(a));
2385 m = Field(&AStruct::z, Not(Truly(ValueIsPositive)));
2386 EXPECT_FALSE(m.Matches(a));
2387}
2388
2389// Tests that Field(&Foo::field, ...) works when field is a pointer.
2390TEST(FieldTest, WorksForPointerField) {
2391 // Matching against NULL.
2392 Matcher<AStruct> m = Field(&AStruct::p, static_cast<const char*>(NULL));
2393 AStruct a;
2394 EXPECT_TRUE(m.Matches(a));
2395 a.p = "hi";
2396 EXPECT_FALSE(m.Matches(a));
2397
2398 // Matching a pointer that is not NULL.
2399 m = Field(&AStruct::p, StartsWith("hi"));
2400 a.p = "hill";
2401 EXPECT_TRUE(m.Matches(a));
2402 a.p = "hole";
2403 EXPECT_FALSE(m.Matches(a));
2404}
2405
2406// Tests that Field() works when the object is passed by reference.
2407TEST(FieldTest, WorksForByRefArgument) {
2408 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2409
2410 AStruct a;
2411 EXPECT_TRUE(m.Matches(a));
2412 a.x = -1;
2413 EXPECT_FALSE(m.Matches(a));
2414}
2415
2416// Tests that Field(&Foo::field, ...) works when the argument's type
2417// is a sub-type of Foo.
2418TEST(FieldTest, WorksForArgumentOfSubType) {
2419 // Note that the matcher expects DerivedStruct but we say AStruct
2420 // inside Field().
2421 Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0));
2422
2423 DerivedStruct d;
2424 EXPECT_TRUE(m.Matches(d));
2425 d.x = -1;
2426 EXPECT_FALSE(m.Matches(d));
2427}
2428
2429// Tests that Field(&Foo::field, m) works when field's type and m's
2430// argument type are compatible but not the same.
2431TEST(FieldTest, WorksForCompatibleMatcherType) {
2432 // The field is an int, but the inner matcher expects a signed char.
2433 Matcher<const AStruct&> m = Field(&AStruct::x,
2434 Matcher<signed char>(Ge(0)));
2435
2436 AStruct a;
2437 EXPECT_TRUE(m.Matches(a));
2438 a.x = -1;
2439 EXPECT_FALSE(m.Matches(a));
2440}
2441
2442// Tests that Field() can describe itself.
2443TEST(FieldTest, CanDescribeSelf) {
2444 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2445
2446 EXPECT_EQ("the given field is greater than or equal to 0", Describe(m));
2447 EXPECT_EQ("the given field is not greater than or equal to 0",
2448 DescribeNegation(m));
2449}
2450
2451// Tests that Field() can explain the match result.
2452TEST(FieldTest, CanExplainMatchResult) {
2453 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2454
2455 AStruct a;
2456 a.x = 1;
2457 EXPECT_EQ("", Explain(m, a));
2458
2459 m = Field(&AStruct::x, GreaterThan(0));
2460 EXPECT_EQ("the given field is 1 more than 0", Explain(m, a));
2461}
2462
2463// Tests that Field() works when the argument is a pointer to const.
2464TEST(FieldForPointerTest, WorksForPointerToConst) {
2465 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2466
2467 AStruct a;
2468 EXPECT_TRUE(m.Matches(&a));
2469 a.x = -1;
2470 EXPECT_FALSE(m.Matches(&a));
2471}
2472
2473// Tests that Field() works when the argument is a pointer to non-const.
2474TEST(FieldForPointerTest, WorksForPointerToNonConst) {
2475 Matcher<AStruct*> m = Field(&AStruct::x, Ge(0));
2476
2477 AStruct a;
2478 EXPECT_TRUE(m.Matches(&a));
2479 a.x = -1;
2480 EXPECT_FALSE(m.Matches(&a));
2481}
2482
2483// Tests that Field() does not match the NULL pointer.
2484TEST(FieldForPointerTest, DoesNotMatchNull) {
2485 Matcher<const AStruct*> m = Field(&AStruct::x, _);
2486 EXPECT_FALSE(m.Matches(NULL));
2487}
2488
2489// Tests that Field(&Foo::field, ...) works when the argument's type
2490// is a sub-type of const Foo*.
2491TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
2492 // Note that the matcher expects DerivedStruct but we say AStruct
2493 // inside Field().
2494 Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0));
2495
2496 DerivedStruct d;
2497 EXPECT_TRUE(m.Matches(&d));
2498 d.x = -1;
2499 EXPECT_FALSE(m.Matches(&d));
2500}
2501
2502// Tests that Field() can describe itself when used to match a pointer.
2503TEST(FieldForPointerTest, CanDescribeSelf) {
2504 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2505
2506 EXPECT_EQ("the given field is greater than or equal to 0", Describe(m));
2507 EXPECT_EQ("the given field is not greater than or equal to 0",
2508 DescribeNegation(m));
2509}
2510
2511// Tests that Field() can explain the result of matching a pointer.
2512TEST(FieldForPointerTest, CanExplainMatchResult) {
2513 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2514
2515 AStruct a;
2516 a.x = 1;
2517 EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(NULL)));
2518 EXPECT_EQ("", Explain(m, &a));
2519
2520 m = Field(&AStruct::x, GreaterThan(0));
2521 EXPECT_EQ("the given field is 1 more than 0", Explain(m, &a));
2522}
2523
2524// A user-defined class for testing Property().
2525class AClass {
2526 public:
2527 AClass() : n_(0) {}
2528
2529 // A getter that returns a non-reference.
2530 int n() const { return n_; }
2531
2532 void set_n(int new_n) { n_ = new_n; }
2533
2534 // A getter that returns a reference to const.
2535 const string& s() const { return s_; }
2536
2537 void set_s(const string& new_s) { s_ = new_s; }
2538
2539 // A getter that returns a reference to non-const.
2540 double& x() const { return x_; }
2541 private:
2542 int n_;
2543 string s_;
2544
2545 static double x_;
2546};
2547
2548double AClass::x_ = 0.0;
2549
2550// A derived class for testing Property().
2551class DerivedClass : public AClass {
2552 private:
2553 int k_;
2554};
2555
2556// Tests that Property(&Foo::property, ...) works when property()
2557// returns a non-reference.
2558TEST(PropertyTest, WorksForNonReferenceProperty) {
2559 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
2560
2561 AClass a;
2562 a.set_n(1);
2563 EXPECT_TRUE(m.Matches(a));
2564
2565 a.set_n(-1);
2566 EXPECT_FALSE(m.Matches(a));
2567}
2568
2569// Tests that Property(&Foo::property, ...) works when property()
2570// returns a reference to const.
2571TEST(PropertyTest, WorksForReferenceToConstProperty) {
2572 Matcher<const AClass&> m = Property(&AClass::s, StartsWith("hi"));
2573
2574 AClass a;
2575 a.set_s("hill");
2576 EXPECT_TRUE(m.Matches(a));
2577
2578 a.set_s("hole");
2579 EXPECT_FALSE(m.Matches(a));
2580}
2581
2582// Tests that Property(&Foo::property, ...) works when property()
2583// returns a reference to non-const.
2584TEST(PropertyTest, WorksForReferenceToNonConstProperty) {
2585 double x = 0.0;
2586 AClass a;
2587
2588 Matcher<const AClass&> m = Property(&AClass::x, Ref(x));
2589 EXPECT_FALSE(m.Matches(a));
2590
2591 m = Property(&AClass::x, Not(Ref(x)));
2592 EXPECT_TRUE(m.Matches(a));
2593}
2594
2595// Tests that Property(&Foo::property, ...) works when the argument is
2596// passed by value.
2597TEST(PropertyTest, WorksForByValueArgument) {
2598 Matcher<AClass> m = Property(&AClass::s, StartsWith("hi"));
2599
2600 AClass a;
2601 a.set_s("hill");
2602 EXPECT_TRUE(m.Matches(a));
2603
2604 a.set_s("hole");
2605 EXPECT_FALSE(m.Matches(a));
2606}
2607
2608// Tests that Property(&Foo::property, ...) works when the argument's
2609// type is a sub-type of Foo.
2610TEST(PropertyTest, WorksForArgumentOfSubType) {
2611 // The matcher expects a DerivedClass, but inside the Property() we
2612 // say AClass.
2613 Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0));
2614
2615 DerivedClass d;
2616 d.set_n(1);
2617 EXPECT_TRUE(m.Matches(d));
2618
2619 d.set_n(-1);
2620 EXPECT_FALSE(m.Matches(d));
2621}
2622
2623// Tests that Property(&Foo::property, m) works when property()'s type
2624// and m's argument type are compatible but different.
2625TEST(PropertyTest, WorksForCompatibleMatcherType) {
2626 // n() returns an int but the inner matcher expects a signed char.
2627 Matcher<const AClass&> m = Property(&AClass::n,
2628 Matcher<signed char>(Ge(0)));
2629
2630 AClass a;
2631 EXPECT_TRUE(m.Matches(a));
2632 a.set_n(-1);
2633 EXPECT_FALSE(m.Matches(a));
2634}
2635
2636// Tests that Property() can describe itself.
2637TEST(PropertyTest, CanDescribeSelf) {
2638 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
2639
2640 EXPECT_EQ("the given property is greater than or equal to 0", Describe(m));
2641 EXPECT_EQ("the given property is not greater than or equal to 0",
2642 DescribeNegation(m));
2643}
2644
2645// Tests that Property() can explain the match result.
2646TEST(PropertyTest, CanExplainMatchResult) {
2647 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
2648
2649 AClass a;
2650 a.set_n(1);
2651 EXPECT_EQ("", Explain(m, a));
2652
2653 m = Property(&AClass::n, GreaterThan(0));
2654 EXPECT_EQ("the given property is 1 more than 0", Explain(m, a));
2655}
2656
2657// Tests that Property() works when the argument is a pointer to const.
2658TEST(PropertyForPointerTest, WorksForPointerToConst) {
2659 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
2660
2661 AClass a;
2662 a.set_n(1);
2663 EXPECT_TRUE(m.Matches(&a));
2664
2665 a.set_n(-1);
2666 EXPECT_FALSE(m.Matches(&a));
2667}
2668
2669// Tests that Property() works when the argument is a pointer to non-const.
2670TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
2671 Matcher<AClass*> m = Property(&AClass::s, StartsWith("hi"));
2672
2673 AClass a;
2674 a.set_s("hill");
2675 EXPECT_TRUE(m.Matches(&a));
2676
2677 a.set_s("hole");
2678 EXPECT_FALSE(m.Matches(&a));
2679}
2680
2681// Tests that Property() does not match the NULL pointer.
2682TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
2683 Matcher<const AClass*> m = Property(&AClass::x, _);
2684 EXPECT_FALSE(m.Matches(NULL));
2685}
2686
2687// Tests that Property(&Foo::property, ...) works when the argument's
2688// type is a sub-type of const Foo*.
2689TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
2690 // The matcher expects a DerivedClass, but inside the Property() we
2691 // say AClass.
2692 Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0));
2693
2694 DerivedClass d;
2695 d.set_n(1);
2696 EXPECT_TRUE(m.Matches(&d));
2697
2698 d.set_n(-1);
2699 EXPECT_FALSE(m.Matches(&d));
2700}
2701
2702// Tests that Property() can describe itself when used to match a pointer.
2703TEST(PropertyForPointerTest, CanDescribeSelf) {
2704 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
2705
2706 EXPECT_EQ("the given property is greater than or equal to 0", Describe(m));
2707 EXPECT_EQ("the given property is not greater than or equal to 0",
2708 DescribeNegation(m));
2709}
2710
2711// Tests that Property() can explain the result of matching a pointer.
2712TEST(PropertyForPointerTest, CanExplainMatchResult) {
2713 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
2714
2715 AClass a;
2716 a.set_n(1);
2717 EXPECT_EQ("", Explain(m, static_cast<const AClass*>(NULL)));
2718 EXPECT_EQ("", Explain(m, &a));
2719
2720 m = Property(&AClass::n, GreaterThan(0));
2721 EXPECT_EQ("the given property is 1 more than 0", Explain(m, &a));
2722}
2723
2724// Tests ResultOf.
2725
2726// Tests that ResultOf(f, ...) compiles and works as expected when f is a
2727// function pointer.
2728string IntToStringFunction(int input) { return input == 1 ? "foo" : "bar"; }
2729
2730TEST(ResultOfTest, WorksForFunctionPointers) {
2731 Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(string("foo")));
2732
2733 EXPECT_TRUE(matcher.Matches(1));
2734 EXPECT_FALSE(matcher.Matches(2));
2735}
2736
2737// Tests that ResultOf() can describe itself.
2738TEST(ResultOfTest, CanDescribeItself) {
2739 Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo"));
2740
2741 EXPECT_EQ("result of the given callable is equal to \"foo\"",
2742 Describe(matcher));
2743 EXPECT_EQ("result of the given callable is not equal to \"foo\"",
2744 DescribeNegation(matcher));
2745}
2746
2747// Tests that ResultOf() can explain the match result.
2748int IntFunction(int input) { return input == 42 ? 80 : 90; }
2749
2750TEST(ResultOfTest, CanExplainMatchResult) {
2751 Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
2752 EXPECT_EQ("", Explain(matcher, 36));
2753
2754 matcher = ResultOf(&IntFunction, GreaterThan(85));
2755 EXPECT_EQ("result of the given callable is 5 more than 85",
2756 Explain(matcher, 36));
2757}
2758
2759// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
2760// returns a non-reference.
2761TEST(ResultOfTest, WorksForNonReferenceResults) {
2762 Matcher<int> matcher = ResultOf(&IntFunction, Eq(80));
2763
2764 EXPECT_TRUE(matcher.Matches(42));
2765 EXPECT_FALSE(matcher.Matches(36));
2766}
2767
2768// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
2769// returns a reference to non-const.
2770double& DoubleFunction(double& input) { return input; }
2771
2772Uncopyable& RefUncopyableFunction(Uncopyable& obj) {
2773 return obj;
2774}
2775
2776TEST(ResultOfTest, WorksForReferenceToNonConstResults) {
2777 double x = 3.14;
2778 double x2 = x;
2779 Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x));
2780
2781 EXPECT_TRUE(matcher.Matches(x));
2782 EXPECT_FALSE(matcher.Matches(x2));
2783
2784 // Test that ResultOf works with uncopyable objects
2785 Uncopyable obj(0);
2786 Uncopyable obj2(0);
2787 Matcher<Uncopyable&> matcher2 =
2788 ResultOf(&RefUncopyableFunction, Ref(obj));
2789
2790 EXPECT_TRUE(matcher2.Matches(obj));
2791 EXPECT_FALSE(matcher2.Matches(obj2));
2792}
2793
2794// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
2795// returns a reference to const.
2796const string& StringFunction(const string& input) { return input; }
2797
2798TEST(ResultOfTest, WorksForReferenceToConstResults) {
2799 string s = "foo";
2800 string s2 = s;
2801 Matcher<const string&> matcher = ResultOf(&StringFunction, Ref(s));
2802
2803 EXPECT_TRUE(matcher.Matches(s));
2804 EXPECT_FALSE(matcher.Matches(s2));
2805}
2806
2807// Tests that ResultOf(f, m) works when f(x) and m's
2808// argument types are compatible but different.
2809TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {
2810 // IntFunction() returns int but the inner matcher expects a signed char.
2811 Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85)));
2812
2813 EXPECT_TRUE(matcher.Matches(36));
2814 EXPECT_FALSE(matcher.Matches(42));
2815}
2816
shiqiane35fdd92008-12-10 05:08:54 +00002817// Tests that the program aborts when ResultOf is passed
2818// a NULL function pointer.
2819TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +00002820 EXPECT_DEATH_IF_SUPPORTED(
shiqiane35fdd92008-12-10 05:08:54 +00002821 ResultOf(static_cast<string(*)(int)>(NULL), Eq(string("foo"))),
2822 "NULL function pointer is passed into ResultOf\\(\\)\\.");
2823}
shiqiane35fdd92008-12-10 05:08:54 +00002824
2825// Tests that ResultOf(f, ...) compiles and works as expected when f is a
2826// function reference.
2827TEST(ResultOfTest, WorksForFunctionReferences) {
2828 Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq("foo"));
2829 EXPECT_TRUE(matcher.Matches(1));
2830 EXPECT_FALSE(matcher.Matches(2));
2831}
2832
2833// Tests that ResultOf(f, ...) compiles and works as expected when f is a
2834// function object.
2835struct Functor : public ::std::unary_function<int, string> {
2836 result_type operator()(argument_type input) const {
2837 return IntToStringFunction(input);
2838 }
2839};
2840
2841TEST(ResultOfTest, WorksForFunctors) {
2842 Matcher<int> matcher = ResultOf(Functor(), Eq(string("foo")));
2843
2844 EXPECT_TRUE(matcher.Matches(1));
2845 EXPECT_FALSE(matcher.Matches(2));
2846}
2847
2848// Tests that ResultOf(f, ...) compiles and works as expected when f is a
2849// functor with more then one operator() defined. ResultOf() must work
2850// for each defined operator().
2851struct PolymorphicFunctor {
2852 typedef int result_type;
2853 int operator()(int n) { return n; }
2854 int operator()(const char* s) { return static_cast<int>(strlen(s)); }
2855};
2856
2857TEST(ResultOfTest, WorksForPolymorphicFunctors) {
2858 Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5));
2859
2860 EXPECT_TRUE(matcher_int.Matches(10));
2861 EXPECT_FALSE(matcher_int.Matches(2));
2862
2863 Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5));
2864
2865 EXPECT_TRUE(matcher_string.Matches("long string"));
2866 EXPECT_FALSE(matcher_string.Matches("shrt"));
2867}
2868
2869const int* ReferencingFunction(const int& n) { return &n; }
2870
2871struct ReferencingFunctor {
2872 typedef const int* result_type;
2873 result_type operator()(const int& n) { return &n; }
2874};
2875
2876TEST(ResultOfTest, WorksForReferencingCallables) {
2877 const int n = 1;
2878 const int n2 = 1;
2879 Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n));
2880 EXPECT_TRUE(matcher2.Matches(n));
2881 EXPECT_FALSE(matcher2.Matches(n2));
2882
2883 Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n));
2884 EXPECT_TRUE(matcher3.Matches(n));
2885 EXPECT_FALSE(matcher3.Matches(n2));
2886}
2887
shiqiane35fdd92008-12-10 05:08:54 +00002888class DivisibleByImpl {
2889 public:
2890 explicit DivisibleByImpl(int divider) : divider_(divider) {}
2891
2892 template <typename T>
2893 bool Matches(const T& n) const {
2894 return (n % divider_) == 0;
2895 }
2896
2897 void DescribeTo(::std::ostream* os) const {
2898 *os << "is divisible by " << divider_;
2899 }
2900
2901 void DescribeNegationTo(::std::ostream* os) const {
2902 *os << "is not divisible by " << divider_;
2903 }
2904
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002905 void set_divider(int divider) { divider_ = divider; }
shiqiane35fdd92008-12-10 05:08:54 +00002906 int divider() const { return divider_; }
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002907
shiqiane35fdd92008-12-10 05:08:54 +00002908 private:
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002909 int divider_;
shiqiane35fdd92008-12-10 05:08:54 +00002910};
2911
2912// For testing using ExplainMatchResultTo() with polymorphic matchers.
2913template <typename T>
2914void ExplainMatchResultTo(const DivisibleByImpl& impl, const T& n,
2915 ::std::ostream* os) {
2916 *os << "is " << (n % impl.divider()) << " modulo "
2917 << impl.divider();
2918}
2919
2920PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) {
2921 return MakePolymorphicMatcher(DivisibleByImpl(n));
2922}
2923
2924// Tests that when AllOf() fails, only the first failing matcher is
2925// asked to explain why.
2926TEST(ExplainMatchResultTest, AllOf_False_False) {
2927 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
2928 EXPECT_EQ("is 1 modulo 4", Explain(m, 5));
2929}
2930
2931// Tests that when AllOf() fails, only the first failing matcher is
2932// asked to explain why.
2933TEST(ExplainMatchResultTest, AllOf_False_True) {
2934 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
2935 EXPECT_EQ("is 2 modulo 4", Explain(m, 6));
2936}
2937
2938// Tests that when AllOf() fails, only the first failing matcher is
2939// asked to explain why.
2940TEST(ExplainMatchResultTest, AllOf_True_False) {
2941 const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));
2942 EXPECT_EQ("is 2 modulo 3", Explain(m, 5));
2943}
2944
2945// Tests that when AllOf() succeeds, all matchers are asked to explain
2946// why.
2947TEST(ExplainMatchResultTest, AllOf_True_True) {
2948 const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3));
2949 EXPECT_EQ("is 0 modulo 2; is 0 modulo 3", Explain(m, 6));
2950}
2951
2952TEST(ExplainMatchResultTest, AllOf_True_True_2) {
2953 const Matcher<int> m = AllOf(Ge(2), Le(3));
2954 EXPECT_EQ("", Explain(m, 2));
2955}
2956
2957TEST(ExplainmatcherResultTest, MonomorphicMatcher) {
2958 const Matcher<int> m = GreaterThan(5);
2959 EXPECT_EQ("is 1 more than 5", Explain(m, 6));
2960}
2961
2962// The following two tests verify that values without a public copy
2963// ctor can be used as arguments to matchers like Eq(), Ge(), and etc
2964// with the help of ByRef().
2965
2966class NotCopyable {
2967 public:
2968 explicit NotCopyable(int value) : value_(value) {}
2969
2970 int value() const { return value_; }
2971
2972 bool operator==(const NotCopyable& rhs) const {
2973 return value() == rhs.value();
2974 }
2975
2976 bool operator>=(const NotCopyable& rhs) const {
2977 return value() >= rhs.value();
2978 }
2979 private:
2980 int value_;
2981
2982 GTEST_DISALLOW_COPY_AND_ASSIGN_(NotCopyable);
2983};
2984
2985TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) {
2986 const NotCopyable const_value1(1);
2987 const Matcher<const NotCopyable&> m = Eq(ByRef(const_value1));
2988
2989 const NotCopyable n1(1), n2(2);
2990 EXPECT_TRUE(m.Matches(n1));
2991 EXPECT_FALSE(m.Matches(n2));
2992}
2993
2994TEST(ByRefTest, AllowsNotCopyableValueInMatchers) {
2995 NotCopyable value2(2);
2996 const Matcher<NotCopyable&> m = Ge(ByRef(value2));
2997
2998 NotCopyable n1(1), n2(2);
2999 EXPECT_FALSE(m.Matches(n1));
3000 EXPECT_TRUE(m.Matches(n2));
3001}
3002
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003003#if GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003004// Tests ContainerEq with different container types, and
3005// different element types.
3006
3007template <typename T>
zhanyong.wanb8243162009-06-04 05:48:20 +00003008class ContainerEqTest : public testing::Test {};
zhanyong.wan6a896b52009-01-16 01:13:50 +00003009
3010typedef testing::Types<
3011 std::set<int>,
3012 std::vector<size_t>,
3013 std::multiset<size_t>,
3014 std::list<int> >
3015 ContainerEqTestTypes;
3016
3017TYPED_TEST_CASE(ContainerEqTest, ContainerEqTestTypes);
3018
3019// Tests that the filled container is equal to itself.
3020TYPED_TEST(ContainerEqTest, EqualsSelf) {
3021 static const int vals[] = {1, 1, 2, 3, 5, 8};
3022 TypeParam my_set(vals, vals + 6);
3023 const Matcher<TypeParam> m = ContainerEq(my_set);
3024 EXPECT_TRUE(m.Matches(my_set));
3025 EXPECT_EQ("", Explain(m, my_set));
3026}
3027
3028// Tests that missing values are reported.
3029TYPED_TEST(ContainerEqTest, ValueMissing) {
3030 static const int vals[] = {1, 1, 2, 3, 5, 8};
3031 static const int test_vals[] = {2, 1, 8, 5};
3032 TypeParam my_set(vals, vals + 6);
3033 TypeParam test_set(test_vals, test_vals + 4);
3034 const Matcher<TypeParam> m = ContainerEq(my_set);
3035 EXPECT_FALSE(m.Matches(test_set));
3036 EXPECT_EQ("Not in actual: 3", Explain(m, test_set));
3037}
3038
3039// Tests that added values are reported.
3040TYPED_TEST(ContainerEqTest, ValueAdded) {
3041 static const int vals[] = {1, 1, 2, 3, 5, 8};
3042 static const int test_vals[] = {1, 2, 3, 5, 8, 46};
3043 TypeParam my_set(vals, vals + 6);
3044 TypeParam test_set(test_vals, test_vals + 6);
3045 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3046 EXPECT_FALSE(m.Matches(test_set));
3047 EXPECT_EQ("Only in actual: 46", Explain(m, test_set));
3048}
3049
3050// Tests that added and missing values are reported together.
3051TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) {
3052 static const int vals[] = {1, 1, 2, 3, 5, 8};
3053 static const int test_vals[] = {1, 2, 3, 8, 46};
3054 TypeParam my_set(vals, vals + 6);
3055 TypeParam test_set(test_vals, test_vals + 5);
3056 const Matcher<TypeParam> m = ContainerEq(my_set);
3057 EXPECT_FALSE(m.Matches(test_set));
3058 EXPECT_EQ("Only in actual: 46; not in actual: 5", Explain(m, test_set));
3059}
3060
3061// Tests duplicated value -- expect no explanation.
3062TYPED_TEST(ContainerEqTest, DuplicateDifference) {
3063 static const int vals[] = {1, 1, 2, 3, 5, 8};
3064 static const int test_vals[] = {1, 2, 3, 5, 8};
3065 TypeParam my_set(vals, vals + 6);
3066 TypeParam test_set(test_vals, test_vals + 5);
3067 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3068 // Depending on the container, match may be true or false
3069 // But in any case there should be no explanation.
3070 EXPECT_EQ("", Explain(m, test_set));
3071}
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003072#endif // GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003073
3074// Tests that mutliple missing values are reported.
3075// Using just vector here, so order is predicatble.
3076TEST(ContainerEqExtraTest, MultipleValuesMissing) {
3077 static const int vals[] = {1, 1, 2, 3, 5, 8};
3078 static const int test_vals[] = {2, 1, 5};
3079 std::vector<int> my_set(vals, vals + 6);
3080 std::vector<int> test_set(test_vals, test_vals + 3);
3081 const Matcher<std::vector<int> > m = ContainerEq(my_set);
3082 EXPECT_FALSE(m.Matches(test_set));
3083 EXPECT_EQ("Not in actual: 3, 8", Explain(m, test_set));
3084}
3085
3086// Tests that added values are reported.
3087// Using just vector here, so order is predicatble.
3088TEST(ContainerEqExtraTest, MultipleValuesAdded) {
3089 static const int vals[] = {1, 1, 2, 3, 5, 8};
3090 static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46};
3091 std::list<size_t> my_set(vals, vals + 6);
3092 std::list<size_t> test_set(test_vals, test_vals + 7);
3093 const Matcher<const std::list<size_t>&> m = ContainerEq(my_set);
3094 EXPECT_FALSE(m.Matches(test_set));
3095 EXPECT_EQ("Only in actual: 92, 46", Explain(m, test_set));
3096}
3097
3098// Tests that added and missing values are reported together.
3099TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) {
3100 static const int vals[] = {1, 1, 2, 3, 5, 8};
3101 static const int test_vals[] = {1, 2, 3, 92, 46};
3102 std::list<size_t> my_set(vals, vals + 6);
3103 std::list<size_t> test_set(test_vals, test_vals + 5);
3104 const Matcher<const std::list<size_t> > m = ContainerEq(my_set);
3105 EXPECT_FALSE(m.Matches(test_set));
3106 EXPECT_EQ("Only in actual: 92, 46; not in actual: 5, 8",
3107 Explain(m, test_set));
3108}
3109
3110// Tests to see that duplicate elements are detected,
3111// but (as above) not reported in the explanation.
3112TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) {
3113 static const int vals[] = {1, 1, 2, 3, 5, 8};
3114 static const int test_vals[] = {1, 2, 3, 5, 8};
3115 std::vector<int> my_set(vals, vals + 6);
3116 std::vector<int> test_set(test_vals, test_vals + 5);
3117 const Matcher<std::vector<int> > m = ContainerEq(my_set);
3118 EXPECT_TRUE(m.Matches(my_set));
3119 EXPECT_FALSE(m.Matches(test_set));
3120 // There is nothing to report when both sets contain all the same values.
3121 EXPECT_EQ("", Explain(m, test_set));
3122}
3123
3124// Tests that ContainerEq works for non-trivial associative containers,
3125// like maps.
3126TEST(ContainerEqExtraTest, WorksForMaps) {
3127 std::map<int, std::string> my_map;
3128 my_map[0] = "a";
3129 my_map[1] = "b";
3130
3131 std::map<int, std::string> test_map;
3132 test_map[0] = "aa";
3133 test_map[1] = "b";
3134
3135 const Matcher<const std::map<int, std::string>&> m = ContainerEq(my_map);
3136 EXPECT_TRUE(m.Matches(my_map));
3137 EXPECT_FALSE(m.Matches(test_map));
3138
3139 EXPECT_EQ("Only in actual: (0, \"aa\"); not in actual: (0, \"a\")",
3140 Explain(m, test_map));
3141}
3142
zhanyong.wanb8243162009-06-04 05:48:20 +00003143TEST(ContainerEqExtraTest, WorksForNativeArray) {
3144 int a1[] = { 1, 2, 3 };
3145 int a2[] = { 1, 2, 3 };
3146 int b[] = { 1, 2, 4 };
3147
3148 EXPECT_THAT(a1, ContainerEq(a2));
3149 EXPECT_THAT(a1, Not(ContainerEq(b)));
3150}
3151
3152TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) {
3153 const char a1[][3] = { "hi", "lo" };
3154 const char a2[][3] = { "hi", "lo" };
3155 const char b[][3] = { "lo", "hi" };
3156
3157 // Tests using ContainerEq() in the first dimension.
3158 EXPECT_THAT(a1, ContainerEq(a2));
3159 EXPECT_THAT(a1, Not(ContainerEq(b)));
3160
3161 // Tests using ContainerEq() in the second dimension.
3162 EXPECT_THAT(a1, ElementsAre(ContainerEq(a2[0]), ContainerEq(a2[1])));
3163 EXPECT_THAT(a1, ElementsAre(Not(ContainerEq(b[0])), ContainerEq(a2[1])));
3164}
3165
3166TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) {
3167 const int a1[] = { 1, 2, 3 };
3168 const int a2[] = { 1, 2, 3 };
3169 const int b[] = { 1, 2, 3, 4 };
3170
zhanyong.wan2661c682009-06-09 05:42:12 +00003171 const int* const p1 = a1;
3172 EXPECT_THAT(make_tuple(p1, 3), ContainerEq(a2));
3173 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(b)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003174
3175 const int c[] = { 1, 3, 2 };
zhanyong.wan2661c682009-06-09 05:42:12 +00003176 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(c)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003177}
3178
3179TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) {
3180 std::string a1[][3] = {
3181 { "hi", "hello", "ciao" },
3182 { "bye", "see you", "ciao" }
3183 };
3184
3185 std::string a2[][3] = {
3186 { "hi", "hello", "ciao" },
3187 { "bye", "see you", "ciao" }
3188 };
3189
3190 const Matcher<const std::string(&)[2][3]> m = ContainerEq(a2);
3191 EXPECT_THAT(a1, m);
3192
3193 a2[0][0] = "ha";
3194 EXPECT_THAT(a1, m);
3195}
3196
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003197// Tests GetParamIndex().
3198
3199TEST(GetParamIndexTest, WorksForEmptyParamList) {
3200 const char* params[] = { NULL };
3201 EXPECT_EQ(kTupleInterpolation, GetParamIndex(params, "*"));
3202 EXPECT_EQ(kInvalidInterpolation, GetParamIndex(params, "a"));
3203}
3204
3205TEST(GetParamIndexTest, RecognizesStar) {
3206 const char* params[] = { "a", "b", NULL };
3207 EXPECT_EQ(kTupleInterpolation, GetParamIndex(params, "*"));
3208}
3209
3210TEST(GetParamIndexTest, RecognizesKnownParam) {
3211 const char* params[] = { "foo", "bar", NULL };
3212 EXPECT_EQ(0, GetParamIndex(params, "foo"));
3213 EXPECT_EQ(1, GetParamIndex(params, "bar"));
3214}
3215
3216TEST(GetParamIndexTest, RejectsUnknownParam) {
3217 const char* params[] = { "foo", "bar", NULL };
3218 EXPECT_EQ(kInvalidInterpolation, GetParamIndex(params, "foobar"));
3219}
3220
3221// Tests SkipPrefix().
3222
3223TEST(SkipPrefixTest, SkipsWhenPrefixMatches) {
3224 const char* const str = "hello";
3225
3226 const char* p = str;
3227 EXPECT_TRUE(SkipPrefix("", &p));
3228 EXPECT_EQ(str, p);
3229
3230 p = str;
3231 EXPECT_TRUE(SkipPrefix("hell", &p));
3232 EXPECT_EQ(str + 4, p);
3233}
3234
3235TEST(SkipPrefixTest, DoesNotSkipWhenPrefixDoesNotMatch) {
3236 const char* const str = "world";
3237
3238 const char* p = str;
3239 EXPECT_FALSE(SkipPrefix("W", &p));
3240 EXPECT_EQ(str, p);
3241
3242 p = str;
3243 EXPECT_FALSE(SkipPrefix("world!", &p));
3244 EXPECT_EQ(str, p);
3245}
3246
3247// Tests FormatMatcherDescriptionSyntaxError().
3248TEST(FormatMatcherDescriptionSyntaxErrorTest, FormatsCorrectly) {
3249 const char* const description = "hello%world";
3250 EXPECT_EQ("Syntax error at index 5 in matcher description \"hello%world\": ",
3251 FormatMatcherDescriptionSyntaxError(description, description + 5));
3252}
3253
3254// Tests ValidateMatcherDescription().
3255
3256TEST(ValidateMatcherDescriptionTest, AcceptsEmptyDescription) {
3257 const char* params[] = { "foo", "bar", NULL };
3258 EXPECT_THAT(ValidateMatcherDescription(params, ""),
3259 ElementsAre());
3260}
3261
3262TEST(ValidateMatcherDescriptionTest,
3263 AcceptsNonEmptyDescriptionWithNoInterpolation) {
3264 const char* params[] = { "foo", "bar", NULL };
3265 EXPECT_THAT(ValidateMatcherDescription(params, "a simple description"),
3266 ElementsAre());
3267}
3268
3269// We use MATCHER_P3() to define a matcher for testing
3270// ValidateMatcherDescription(); otherwise we'll end up with much
3271// plumbing code. This is not circular as
3272// ValidateMatcherDescription() doesn't affect whether the matcher
3273// matches a value or not.
3274MATCHER_P3(EqInterpolation, start, end, index, "equals Interpolation%(*)s") {
3275 return arg.start_pos == start && arg.end_pos == end &&
3276 arg.param_index == index;
3277}
3278
3279TEST(ValidateMatcherDescriptionTest, AcceptsPercentInterpolation) {
3280 const char* params[] = { "foo", NULL };
3281 const char* const desc = "one %%";
3282 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3283 ElementsAre(EqInterpolation(desc + 4, desc + 6,
3284 kPercentInterpolation)));
3285}
3286
3287TEST(ValidateMatcherDescriptionTest, AcceptsTupleInterpolation) {
3288 const char* params[] = { "foo", "bar", "baz", NULL };
3289 const char* const desc = "%(*)s after";
3290 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3291 ElementsAre(EqInterpolation(desc, desc + 5,
3292 kTupleInterpolation)));
3293}
3294
3295TEST(ValidateMatcherDescriptionTest, AcceptsParamInterpolation) {
3296 const char* params[] = { "foo", "bar", "baz", NULL };
3297 const char* const desc = "a %(bar)s.";
3298 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3299 ElementsAre(EqInterpolation(desc + 2, desc + 9, 1)));
3300}
3301
3302TEST(ValidateMatcherDescriptionTest, AcceptsMultiplenterpolations) {
3303 const char* params[] = { "foo", "bar", "baz", NULL };
3304 const char* const desc = "%(baz)s %(foo)s %(bar)s";
3305 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3306 ElementsAre(EqInterpolation(desc, desc + 7, 2),
3307 EqInterpolation(desc + 8, desc + 15, 0),
3308 EqInterpolation(desc + 16, desc + 23, 1)));
3309}
3310
3311TEST(ValidateMatcherDescriptionTest, AcceptsRepeatedParams) {
3312 const char* params[] = { "foo", "bar", NULL };
3313 const char* const desc = "%(foo)s and %(foo)s";
3314 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3315 ElementsAre(EqInterpolation(desc, desc + 7, 0),
3316 EqInterpolation(desc + 12, desc + 19, 0)));
3317}
3318
3319TEST(ValidateMatcherDescriptionTest, RejectsUnknownParam) {
3320 const char* params[] = { "a", "bar", NULL };
3321 EXPECT_NONFATAL_FAILURE({
3322 EXPECT_THAT(ValidateMatcherDescription(params, "%(foo)s"),
3323 ElementsAre());
3324 }, "Syntax error at index 2 in matcher description \"%(foo)s\": "
3325 "\"foo\" is an invalid parameter name.");
3326}
3327
3328TEST(ValidateMatcherDescriptionTest, RejectsUnfinishedParam) {
3329 const char* params[] = { "a", "bar", NULL };
3330 EXPECT_NONFATAL_FAILURE({
3331 EXPECT_THAT(ValidateMatcherDescription(params, "%(foo)"),
3332 ElementsAre());
3333 }, "Syntax error at index 0 in matcher description \"%(foo)\": "
3334 "an interpolation must end with \")s\", but \"%(foo)\" does not.");
3335
3336 EXPECT_NONFATAL_FAILURE({
3337 EXPECT_THAT(ValidateMatcherDescription(params, "x%(a"),
3338 ElementsAre());
3339 }, "Syntax error at index 1 in matcher description \"x%(a\": "
3340 "an interpolation must end with \")s\", but \"%(a\" does not.");
3341}
3342
3343TEST(ValidateMatcherDescriptionTest, RejectsSinglePercent) {
3344 const char* params[] = { "a", NULL };
3345 EXPECT_NONFATAL_FAILURE({
3346 EXPECT_THAT(ValidateMatcherDescription(params, "a %."),
3347 ElementsAre());
3348 }, "Syntax error at index 2 in matcher description \"a %.\": "
3349 "use \"%%\" instead of \"%\" to print \"%\".");
3350
3351}
3352
3353// Tests JoinAsTuple().
3354
3355TEST(JoinAsTupleTest, JoinsEmptyTuple) {
3356 EXPECT_EQ("", JoinAsTuple(Strings()));
3357}
3358
3359TEST(JoinAsTupleTest, JoinsOneTuple) {
3360 const char* fields[] = { "1" };
3361 EXPECT_EQ("1", JoinAsTuple(Strings(fields, fields + 1)));
3362}
3363
3364TEST(JoinAsTupleTest, JoinsTwoTuple) {
3365 const char* fields[] = { "1", "a" };
3366 EXPECT_EQ("(1, a)", JoinAsTuple(Strings(fields, fields + 2)));
3367}
3368
3369TEST(JoinAsTupleTest, JoinsTenTuple) {
3370 const char* fields[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
3371 EXPECT_EQ("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)",
3372 JoinAsTuple(Strings(fields, fields + 10)));
3373}
3374
3375// Tests FormatMatcherDescription().
3376
3377TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) {
3378 EXPECT_EQ("is even",
3379 FormatMatcherDescription("IsEven", "", Interpolations(),
3380 Strings()));
3381
3382 const char* params[] = { "5" };
3383 EXPECT_EQ("equals 5",
3384 FormatMatcherDescription("Equals", "", Interpolations(),
3385 Strings(params, params + 1)));
3386
3387 const char* params2[] = { "5", "8" };
3388 EXPECT_EQ("is in range (5, 8)",
3389 FormatMatcherDescription("IsInRange", "", Interpolations(),
3390 Strings(params2, params2 + 2)));
3391}
3392
3393TEST(FormatMatcherDescriptionTest, WorksForDescriptionWithNoInterpolation) {
3394 EXPECT_EQ("is positive",
3395 FormatMatcherDescription("Gt0", "is positive", Interpolations(),
3396 Strings()));
3397
3398 const char* params[] = { "5", "6" };
3399 EXPECT_EQ("is negative",
3400 FormatMatcherDescription("Lt0", "is negative", Interpolations(),
3401 Strings(params, params + 2)));
3402}
3403
3404TEST(FormatMatcherDescriptionTest,
3405 WorksWhenDescriptionStartsWithInterpolation) {
3406 const char* params[] = { "5" };
3407 const char* const desc = "%(num)s times bigger";
3408 const Interpolation interp[] = { Interpolation(desc, desc + 7, 0) };
3409 EXPECT_EQ("5 times bigger",
3410 FormatMatcherDescription("Foo", desc,
3411 Interpolations(interp, interp + 1),
3412 Strings(params, params + 1)));
3413}
3414
3415TEST(FormatMatcherDescriptionTest,
3416 WorksWhenDescriptionEndsWithInterpolation) {
3417 const char* params[] = { "5", "6" };
3418 const char* const desc = "is bigger than %(y)s";
3419 const Interpolation interp[] = { Interpolation(desc + 15, desc + 20, 1) };
3420 EXPECT_EQ("is bigger than 6",
3421 FormatMatcherDescription("Foo", desc,
3422 Interpolations(interp, interp + 1),
3423 Strings(params, params + 2)));
3424}
3425
3426TEST(FormatMatcherDescriptionTest,
3427 WorksWhenDescriptionStartsAndEndsWithInterpolation) {
3428 const char* params[] = { "5", "6" };
3429 const char* const desc = "%(x)s <= arg <= %(y)s";
3430 const Interpolation interp[] = {
3431 Interpolation(desc, desc + 5, 0),
3432 Interpolation(desc + 16, desc + 21, 1)
3433 };
3434 EXPECT_EQ("5 <= arg <= 6",
3435 FormatMatcherDescription("Foo", desc,
3436 Interpolations(interp, interp + 2),
3437 Strings(params, params + 2)));
3438}
3439
3440TEST(FormatMatcherDescriptionTest,
3441 WorksWhenDescriptionDoesNotStartOrEndWithInterpolation) {
3442 const char* params[] = { "5.2" };
3443 const char* const desc = "has %(x)s cents";
3444 const Interpolation interp[] = { Interpolation(desc + 4, desc + 9, 0) };
3445 EXPECT_EQ("has 5.2 cents",
3446 FormatMatcherDescription("Foo", desc,
3447 Interpolations(interp, interp + 1),
3448 Strings(params, params + 1)));
3449}
3450
3451TEST(FormatMatcherDescriptionTest,
3452 WorksWhenDescriptionContainsMultipleInterpolations) {
3453 const char* params[] = { "5", "6" };
3454 const char* const desc = "in %(*)s or [%(x)s, %(y)s]";
3455 const Interpolation interp[] = {
3456 Interpolation(desc + 3, desc + 8, kTupleInterpolation),
3457 Interpolation(desc + 13, desc + 18, 0),
3458 Interpolation(desc + 20, desc + 25, 1)
3459 };
3460 EXPECT_EQ("in (5, 6) or [5, 6]",
3461 FormatMatcherDescription("Foo", desc,
3462 Interpolations(interp, interp + 3),
3463 Strings(params, params + 2)));
3464}
3465
3466TEST(FormatMatcherDescriptionTest,
3467 WorksWhenDescriptionContainsRepeatedParams) {
3468 const char* params[] = { "9" };
3469 const char* const desc = "in [-%(x)s, %(x)s]";
3470 const Interpolation interp[] = {
3471 Interpolation(desc + 5, desc + 10, 0),
3472 Interpolation(desc + 12, desc + 17, 0)
3473 };
3474 EXPECT_EQ("in [-9, 9]",
3475 FormatMatcherDescription("Foo", desc,
3476 Interpolations(interp, interp + 2),
3477 Strings(params, params + 1)));
3478}
3479
3480TEST(FormatMatcherDescriptionTest,
3481 WorksForDescriptionWithInvalidInterpolation) {
3482 const char* params[] = { "9" };
3483 const char* const desc = "> %(x)s %(x)";
3484 const Interpolation interp[] = { Interpolation(desc + 2, desc + 7, 0) };
3485 EXPECT_EQ("> 9 %(x)",
3486 FormatMatcherDescription("Foo", desc,
3487 Interpolations(interp, interp + 1),
3488 Strings(params, params + 1)));
3489}
3490
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003491// Tests PolymorphicMatcher::mutable_impl().
3492TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {
3493 PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
3494 DivisibleByImpl& impl = m.mutable_impl();
3495 EXPECT_EQ(42, impl.divider());
3496
3497 impl.set_divider(0);
3498 EXPECT_EQ(0, m.mutable_impl().divider());
3499}
3500
3501// Tests PolymorphicMatcher::impl().
3502TEST(PolymorphicMatcherTest, CanAccessImpl) {
3503 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
3504 const DivisibleByImpl& impl = m.impl();
3505 EXPECT_EQ(42, impl.divider());
3506}
3507
shiqiane35fdd92008-12-10 05:08:54 +00003508} // namespace gmock_matchers_test
3509} // namespace testing