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