blob: 052202d71cd975f050cb1236a936b8518d1d43ee [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;
1787 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Gt(10)) << "This should fail.",
1788 "Value of: n\n"
1789 "Expected: is greater than 10\n"
1790 " Actual: 5\n"
1791 "This should fail.");
1792 n = 0;
1793 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(n, AllOf(Le(7), Ge(5))),
1794 "Value of: n\n"
1795 "Expected: (is less than or equal to 7) and "
1796 "(is greater than or equal to 5)\n"
1797 " Actual: 0");
1798}
1799
1800// Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument
1801// has a reference type.
1802TEST(MatcherAssertionTest, WorksForByRefArguments) {
1803 // We use a static variable here as EXPECT_FATAL_FAILURE() cannot
1804 // reference auto variables.
1805 static int n;
1806 n = 0;
1807 EXPECT_THAT(n, AllOf(Le(7), Ref(n)));
1808 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Not(Ref(n))),
1809 "Value of: n\n"
1810 "Expected: does not reference the variable @");
1811 // Tests the "Actual" part.
1812 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Not(Ref(n))),
1813 "Actual: 0 (is located @");
1814}
1815
1816// Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is
1817// monomorphic.
1818TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
1819 Matcher<const char*> starts_with_he = StartsWith("he");
1820 ASSERT_THAT("hello", starts_with_he);
1821
1822 Matcher<const string&> ends_with_ok = EndsWith("ok");
1823 ASSERT_THAT("book", ends_with_ok);
1824
1825 Matcher<int> is_greater_than_5 = Gt(5);
1826 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5),
1827 "Value of: 5\n"
1828 "Expected: is greater than 5\n"
1829 " Actual: 5");
1830}
1831
1832// Tests floating-point matchers.
1833template <typename RawType>
1834class FloatingPointTest : public testing::Test {
1835 protected:
1836 typedef typename testing::internal::FloatingPoint<RawType> Floating;
1837 typedef typename Floating::Bits Bits;
1838
1839 virtual void SetUp() {
1840 const size_t max_ulps = Floating::kMaxUlps;
1841
1842 // The bits that represent 0.0.
1843 const Bits zero_bits = Floating(0).bits();
1844
1845 // Makes some numbers close to 0.0.
1846 close_to_positive_zero_ = Floating::ReinterpretBits(zero_bits + max_ulps/2);
1847 close_to_negative_zero_ = -Floating::ReinterpretBits(
1848 zero_bits + max_ulps - max_ulps/2);
1849 further_from_negative_zero_ = -Floating::ReinterpretBits(
1850 zero_bits + max_ulps + 1 - max_ulps/2);
1851
1852 // The bits that represent 1.0.
1853 const Bits one_bits = Floating(1).bits();
1854
1855 // Makes some numbers close to 1.0.
1856 close_to_one_ = Floating::ReinterpretBits(one_bits + max_ulps);
1857 further_from_one_ = Floating::ReinterpretBits(one_bits + max_ulps + 1);
1858
1859 // +infinity.
1860 infinity_ = Floating::Infinity();
1861
1862 // The bits that represent +infinity.
1863 const Bits infinity_bits = Floating(infinity_).bits();
1864
1865 // Makes some numbers close to infinity.
1866 close_to_infinity_ = Floating::ReinterpretBits(infinity_bits - max_ulps);
1867 further_from_infinity_ = Floating::ReinterpretBits(
1868 infinity_bits - max_ulps - 1);
1869
1870 // Makes some NAN's.
1871 nan1_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 1);
1872 nan2_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 200);
1873 }
1874
1875 void TestSize() {
1876 EXPECT_EQ(sizeof(RawType), sizeof(Bits));
1877 }
1878
1879 // A battery of tests for FloatingEqMatcher::Matches.
1880 // matcher_maker is a pointer to a function which creates a FloatingEqMatcher.
1881 void TestMatches(
1882 testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) {
1883 Matcher<RawType> m1 = matcher_maker(0.0);
1884 EXPECT_TRUE(m1.Matches(-0.0));
1885 EXPECT_TRUE(m1.Matches(close_to_positive_zero_));
1886 EXPECT_TRUE(m1.Matches(close_to_negative_zero_));
1887 EXPECT_FALSE(m1.Matches(1.0));
1888
1889 Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_);
1890 EXPECT_FALSE(m2.Matches(further_from_negative_zero_));
1891
1892 Matcher<RawType> m3 = matcher_maker(1.0);
1893 EXPECT_TRUE(m3.Matches(close_to_one_));
1894 EXPECT_FALSE(m3.Matches(further_from_one_));
1895
1896 // Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above.
1897 EXPECT_FALSE(m3.Matches(0.0));
1898
1899 Matcher<RawType> m4 = matcher_maker(-infinity_);
1900 EXPECT_TRUE(m4.Matches(-close_to_infinity_));
1901
1902 Matcher<RawType> m5 = matcher_maker(infinity_);
1903 EXPECT_TRUE(m5.Matches(close_to_infinity_));
1904
1905 // This is interesting as the representations of infinity_ and nan1_
1906 // are only 1 DLP apart.
1907 EXPECT_FALSE(m5.Matches(nan1_));
1908
1909 // matcher_maker can produce a Matcher<const RawType&>, which is needed in
1910 // some cases.
1911 Matcher<const RawType&> m6 = matcher_maker(0.0);
1912 EXPECT_TRUE(m6.Matches(-0.0));
1913 EXPECT_TRUE(m6.Matches(close_to_positive_zero_));
1914 EXPECT_FALSE(m6.Matches(1.0));
1915
1916 // matcher_maker can produce a Matcher<RawType&>, which is needed in some
1917 // cases.
1918 Matcher<RawType&> m7 = matcher_maker(0.0);
1919 RawType x = 0.0;
1920 EXPECT_TRUE(m7.Matches(x));
1921 x = 0.01f;
1922 EXPECT_FALSE(m7.Matches(x));
1923 }
1924
1925 // Pre-calculated numbers to be used by the tests.
1926
1927 static RawType close_to_positive_zero_;
1928 static RawType close_to_negative_zero_;
1929 static RawType further_from_negative_zero_;
1930
1931 static RawType close_to_one_;
1932 static RawType further_from_one_;
1933
1934 static RawType infinity_;
1935 static RawType close_to_infinity_;
1936 static RawType further_from_infinity_;
1937
1938 static RawType nan1_;
1939 static RawType nan2_;
1940};
1941
1942template <typename RawType>
1943RawType FloatingPointTest<RawType>::close_to_positive_zero_;
1944
1945template <typename RawType>
1946RawType FloatingPointTest<RawType>::close_to_negative_zero_;
1947
1948template <typename RawType>
1949RawType FloatingPointTest<RawType>::further_from_negative_zero_;
1950
1951template <typename RawType>
1952RawType FloatingPointTest<RawType>::close_to_one_;
1953
1954template <typename RawType>
1955RawType FloatingPointTest<RawType>::further_from_one_;
1956
1957template <typename RawType>
1958RawType FloatingPointTest<RawType>::infinity_;
1959
1960template <typename RawType>
1961RawType FloatingPointTest<RawType>::close_to_infinity_;
1962
1963template <typename RawType>
1964RawType FloatingPointTest<RawType>::further_from_infinity_;
1965
1966template <typename RawType>
1967RawType FloatingPointTest<RawType>::nan1_;
1968
1969template <typename RawType>
1970RawType FloatingPointTest<RawType>::nan2_;
1971
1972// Instantiate FloatingPointTest for testing floats.
1973typedef FloatingPointTest<float> FloatTest;
1974
1975TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) {
1976 TestMatches(&FloatEq);
1977}
1978
1979TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
1980 TestMatches(&NanSensitiveFloatEq);
1981}
1982
1983TEST_F(FloatTest, FloatEqCannotMatchNaN) {
1984 // FloatEq never matches NaN.
1985 Matcher<float> m = FloatEq(nan1_);
1986 EXPECT_FALSE(m.Matches(nan1_));
1987 EXPECT_FALSE(m.Matches(nan2_));
1988 EXPECT_FALSE(m.Matches(1.0));
1989}
1990
1991TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {
1992 // NanSensitiveFloatEq will match NaN.
1993 Matcher<float> m = NanSensitiveFloatEq(nan1_);
1994 EXPECT_TRUE(m.Matches(nan1_));
1995 EXPECT_TRUE(m.Matches(nan2_));
1996 EXPECT_FALSE(m.Matches(1.0));
1997}
1998
1999TEST_F(FloatTest, FloatEqCanDescribeSelf) {
2000 Matcher<float> m1 = FloatEq(2.0f);
2001 EXPECT_EQ("is approximately 2", Describe(m1));
2002 EXPECT_EQ("is not approximately 2", DescribeNegation(m1));
2003
2004 Matcher<float> m2 = FloatEq(0.5f);
2005 EXPECT_EQ("is approximately 0.5", Describe(m2));
2006 EXPECT_EQ("is not approximately 0.5", DescribeNegation(m2));
2007
2008 Matcher<float> m3 = FloatEq(nan1_);
2009 EXPECT_EQ("never matches", Describe(m3));
2010 EXPECT_EQ("is anything", DescribeNegation(m3));
2011}
2012
2013TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {
2014 Matcher<float> m1 = NanSensitiveFloatEq(2.0f);
2015 EXPECT_EQ("is approximately 2", Describe(m1));
2016 EXPECT_EQ("is not approximately 2", DescribeNegation(m1));
2017
2018 Matcher<float> m2 = NanSensitiveFloatEq(0.5f);
2019 EXPECT_EQ("is approximately 0.5", Describe(m2));
2020 EXPECT_EQ("is not approximately 0.5", DescribeNegation(m2));
2021
2022 Matcher<float> m3 = NanSensitiveFloatEq(nan1_);
2023 EXPECT_EQ("is NaN", Describe(m3));
2024 EXPECT_EQ("is not NaN", DescribeNegation(m3));
2025}
2026
2027// Instantiate FloatingPointTest for testing doubles.
2028typedef FloatingPointTest<double> DoubleTest;
2029
2030TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
2031 TestMatches(&DoubleEq);
2032}
2033
2034TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
2035 TestMatches(&NanSensitiveDoubleEq);
2036}
2037
2038TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {
2039 // DoubleEq never matches NaN.
2040 Matcher<double> m = DoubleEq(nan1_);
2041 EXPECT_FALSE(m.Matches(nan1_));
2042 EXPECT_FALSE(m.Matches(nan2_));
2043 EXPECT_FALSE(m.Matches(1.0));
2044}
2045
2046TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {
2047 // NanSensitiveDoubleEq will match NaN.
2048 Matcher<double> m = NanSensitiveDoubleEq(nan1_);
2049 EXPECT_TRUE(m.Matches(nan1_));
2050 EXPECT_TRUE(m.Matches(nan2_));
2051 EXPECT_FALSE(m.Matches(1.0));
2052}
2053
2054TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {
2055 Matcher<double> m1 = DoubleEq(2.0);
2056 EXPECT_EQ("is approximately 2", Describe(m1));
2057 EXPECT_EQ("is not approximately 2", DescribeNegation(m1));
2058
2059 Matcher<double> m2 = DoubleEq(0.5);
2060 EXPECT_EQ("is approximately 0.5", Describe(m2));
2061 EXPECT_EQ("is not approximately 0.5", DescribeNegation(m2));
2062
2063 Matcher<double> m3 = DoubleEq(nan1_);
2064 EXPECT_EQ("never matches", Describe(m3));
2065 EXPECT_EQ("is anything", DescribeNegation(m3));
2066}
2067
2068TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {
2069 Matcher<double> m1 = NanSensitiveDoubleEq(2.0);
2070 EXPECT_EQ("is approximately 2", Describe(m1));
2071 EXPECT_EQ("is not approximately 2", DescribeNegation(m1));
2072
2073 Matcher<double> m2 = NanSensitiveDoubleEq(0.5);
2074 EXPECT_EQ("is approximately 0.5", Describe(m2));
2075 EXPECT_EQ("is not approximately 0.5", DescribeNegation(m2));
2076
2077 Matcher<double> m3 = NanSensitiveDoubleEq(nan1_);
2078 EXPECT_EQ("is NaN", Describe(m3));
2079 EXPECT_EQ("is not NaN", DescribeNegation(m3));
2080}
2081
2082TEST(PointeeTest, RawPointer) {
2083 const Matcher<int*> m = Pointee(Ge(0));
2084
2085 int n = 1;
2086 EXPECT_TRUE(m.Matches(&n));
2087 n = -1;
2088 EXPECT_FALSE(m.Matches(&n));
2089 EXPECT_FALSE(m.Matches(NULL));
2090}
2091
2092TEST(PointeeTest, RawPointerToConst) {
2093 const Matcher<const double*> m = Pointee(Ge(0));
2094
2095 double x = 1;
2096 EXPECT_TRUE(m.Matches(&x));
2097 x = -1;
2098 EXPECT_FALSE(m.Matches(&x));
2099 EXPECT_FALSE(m.Matches(NULL));
2100}
2101
2102TEST(PointeeTest, ReferenceToConstRawPointer) {
2103 const Matcher<int* const &> m = Pointee(Ge(0));
2104
2105 int n = 1;
2106 EXPECT_TRUE(m.Matches(&n));
2107 n = -1;
2108 EXPECT_FALSE(m.Matches(&n));
2109 EXPECT_FALSE(m.Matches(NULL));
2110}
2111
2112TEST(PointeeTest, ReferenceToNonConstRawPointer) {
2113 const Matcher<double* &> m = Pointee(Ge(0));
2114
2115 double x = 1.0;
2116 double* p = &x;
2117 EXPECT_TRUE(m.Matches(p));
2118 x = -1;
2119 EXPECT_FALSE(m.Matches(p));
2120 p = NULL;
2121 EXPECT_FALSE(m.Matches(p));
2122}
2123
2124TEST(PointeeTest, NeverMatchesNull) {
2125 const Matcher<const char*> m = Pointee(_);
2126 EXPECT_FALSE(m.Matches(NULL));
2127}
2128
2129// Tests that we can write Pointee(value) instead of Pointee(Eq(value)).
2130TEST(PointeeTest, MatchesAgainstAValue) {
2131 const Matcher<int*> m = Pointee(5);
2132
2133 int n = 5;
2134 EXPECT_TRUE(m.Matches(&n));
2135 n = -1;
2136 EXPECT_FALSE(m.Matches(&n));
2137 EXPECT_FALSE(m.Matches(NULL));
2138}
2139
2140TEST(PointeeTest, CanDescribeSelf) {
2141 const Matcher<int*> m = Pointee(Gt(3));
2142 EXPECT_EQ("points to a value that is greater than 3", Describe(m));
2143 EXPECT_EQ("does not point to a value that is greater than 3",
2144 DescribeNegation(m));
2145}
2146
2147// For testing ExplainMatchResultTo().
2148class GreaterThanMatcher : public MatcherInterface<int> {
2149 public:
2150 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
2151
2152 virtual bool Matches(int lhs) const { return lhs > rhs_; }
2153
2154 virtual void DescribeTo(::std::ostream* os) const {
2155 *os << "is greater than " << rhs_;
2156 }
2157
2158 virtual void ExplainMatchResultTo(int lhs, ::std::ostream* os) const {
2159 const int diff = lhs - rhs_;
2160 if (diff > 0) {
2161 *os << "is " << diff << " more than " << rhs_;
2162 } else if (diff == 0) {
2163 *os << "is the same as " << rhs_;
2164 } else {
2165 *os << "is " << -diff << " less than " << rhs_;
2166 }
2167 }
2168 private:
2169 const int rhs_;
2170};
2171
2172Matcher<int> GreaterThan(int n) {
2173 return MakeMatcher(new GreaterThanMatcher(n));
2174}
2175
2176TEST(PointeeTest, CanExplainMatchResult) {
2177 const Matcher<const string*> m = Pointee(StartsWith("Hi"));
2178
2179 EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL)));
2180
2181 const Matcher<int*> m2 = Pointee(GreaterThan(1));
2182 int n = 3;
2183 EXPECT_EQ("points to a value that is 2 more than 1", Explain(m2, &n));
2184}
2185
2186// An uncopyable class.
2187class Uncopyable {
2188 public:
2189 explicit Uncopyable(int value) : value_(value) {}
2190
2191 int value() const { return value_; }
2192 private:
2193 const int value_;
2194 GTEST_DISALLOW_COPY_AND_ASSIGN_(Uncopyable);
2195};
2196
2197// Returns true iff x.value() is positive.
2198bool ValueIsPositive(const Uncopyable& x) { return x.value() > 0; }
2199
2200// A user-defined struct for testing Field().
2201struct AStruct {
2202 AStruct() : x(0), y(1.0), z(5), p(NULL) {}
2203 AStruct(const AStruct& rhs)
2204 : x(rhs.x), y(rhs.y), z(rhs.z.value()), p(rhs.p) {}
2205
2206 int x; // A non-const field.
2207 const double y; // A const field.
2208 Uncopyable z; // An uncopyable field.
2209 const char* p; // A pointer field.
2210};
2211
2212// A derived struct for testing Field().
2213struct DerivedStruct : public AStruct {
2214 char ch;
2215};
2216
2217// Tests that Field(&Foo::field, ...) works when field is non-const.
2218TEST(FieldTest, WorksForNonConstField) {
2219 Matcher<AStruct> m = Field(&AStruct::x, Ge(0));
2220
2221 AStruct a;
2222 EXPECT_TRUE(m.Matches(a));
2223 a.x = -1;
2224 EXPECT_FALSE(m.Matches(a));
2225}
2226
2227// Tests that Field(&Foo::field, ...) works when field is const.
2228TEST(FieldTest, WorksForConstField) {
2229 AStruct a;
2230
2231 Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0));
2232 EXPECT_TRUE(m.Matches(a));
2233 m = Field(&AStruct::y, Le(0.0));
2234 EXPECT_FALSE(m.Matches(a));
2235}
2236
2237// Tests that Field(&Foo::field, ...) works when field is not copyable.
2238TEST(FieldTest, WorksForUncopyableField) {
2239 AStruct a;
2240
2241 Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive));
2242 EXPECT_TRUE(m.Matches(a));
2243 m = Field(&AStruct::z, Not(Truly(ValueIsPositive)));
2244 EXPECT_FALSE(m.Matches(a));
2245}
2246
2247// Tests that Field(&Foo::field, ...) works when field is a pointer.
2248TEST(FieldTest, WorksForPointerField) {
2249 // Matching against NULL.
2250 Matcher<AStruct> m = Field(&AStruct::p, static_cast<const char*>(NULL));
2251 AStruct a;
2252 EXPECT_TRUE(m.Matches(a));
2253 a.p = "hi";
2254 EXPECT_FALSE(m.Matches(a));
2255
2256 // Matching a pointer that is not NULL.
2257 m = Field(&AStruct::p, StartsWith("hi"));
2258 a.p = "hill";
2259 EXPECT_TRUE(m.Matches(a));
2260 a.p = "hole";
2261 EXPECT_FALSE(m.Matches(a));
2262}
2263
2264// Tests that Field() works when the object is passed by reference.
2265TEST(FieldTest, WorksForByRefArgument) {
2266 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2267
2268 AStruct a;
2269 EXPECT_TRUE(m.Matches(a));
2270 a.x = -1;
2271 EXPECT_FALSE(m.Matches(a));
2272}
2273
2274// Tests that Field(&Foo::field, ...) works when the argument's type
2275// is a sub-type of Foo.
2276TEST(FieldTest, WorksForArgumentOfSubType) {
2277 // Note that the matcher expects DerivedStruct but we say AStruct
2278 // inside Field().
2279 Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0));
2280
2281 DerivedStruct d;
2282 EXPECT_TRUE(m.Matches(d));
2283 d.x = -1;
2284 EXPECT_FALSE(m.Matches(d));
2285}
2286
2287// Tests that Field(&Foo::field, m) works when field's type and m's
2288// argument type are compatible but not the same.
2289TEST(FieldTest, WorksForCompatibleMatcherType) {
2290 // The field is an int, but the inner matcher expects a signed char.
2291 Matcher<const AStruct&> m = Field(&AStruct::x,
2292 Matcher<signed char>(Ge(0)));
2293
2294 AStruct a;
2295 EXPECT_TRUE(m.Matches(a));
2296 a.x = -1;
2297 EXPECT_FALSE(m.Matches(a));
2298}
2299
2300// Tests that Field() can describe itself.
2301TEST(FieldTest, CanDescribeSelf) {
2302 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2303
2304 EXPECT_EQ("the given field is greater than or equal to 0", Describe(m));
2305 EXPECT_EQ("the given field is not greater than or equal to 0",
2306 DescribeNegation(m));
2307}
2308
2309// Tests that Field() can explain the match result.
2310TEST(FieldTest, CanExplainMatchResult) {
2311 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2312
2313 AStruct a;
2314 a.x = 1;
2315 EXPECT_EQ("", Explain(m, a));
2316
2317 m = Field(&AStruct::x, GreaterThan(0));
2318 EXPECT_EQ("the given field is 1 more than 0", Explain(m, a));
2319}
2320
2321// Tests that Field() works when the argument is a pointer to const.
2322TEST(FieldForPointerTest, WorksForPointerToConst) {
2323 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2324
2325 AStruct a;
2326 EXPECT_TRUE(m.Matches(&a));
2327 a.x = -1;
2328 EXPECT_FALSE(m.Matches(&a));
2329}
2330
2331// Tests that Field() works when the argument is a pointer to non-const.
2332TEST(FieldForPointerTest, WorksForPointerToNonConst) {
2333 Matcher<AStruct*> m = Field(&AStruct::x, Ge(0));
2334
2335 AStruct a;
2336 EXPECT_TRUE(m.Matches(&a));
2337 a.x = -1;
2338 EXPECT_FALSE(m.Matches(&a));
2339}
2340
2341// Tests that Field() does not match the NULL pointer.
2342TEST(FieldForPointerTest, DoesNotMatchNull) {
2343 Matcher<const AStruct*> m = Field(&AStruct::x, _);
2344 EXPECT_FALSE(m.Matches(NULL));
2345}
2346
2347// Tests that Field(&Foo::field, ...) works when the argument's type
2348// is a sub-type of const Foo*.
2349TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
2350 // Note that the matcher expects DerivedStruct but we say AStruct
2351 // inside Field().
2352 Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0));
2353
2354 DerivedStruct d;
2355 EXPECT_TRUE(m.Matches(&d));
2356 d.x = -1;
2357 EXPECT_FALSE(m.Matches(&d));
2358}
2359
2360// Tests that Field() can describe itself when used to match a pointer.
2361TEST(FieldForPointerTest, CanDescribeSelf) {
2362 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2363
2364 EXPECT_EQ("the given field is greater than or equal to 0", Describe(m));
2365 EXPECT_EQ("the given field is not greater than or equal to 0",
2366 DescribeNegation(m));
2367}
2368
2369// Tests that Field() can explain the result of matching a pointer.
2370TEST(FieldForPointerTest, CanExplainMatchResult) {
2371 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2372
2373 AStruct a;
2374 a.x = 1;
2375 EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(NULL)));
2376 EXPECT_EQ("", Explain(m, &a));
2377
2378 m = Field(&AStruct::x, GreaterThan(0));
2379 EXPECT_EQ("the given field is 1 more than 0", Explain(m, &a));
2380}
2381
2382// A user-defined class for testing Property().
2383class AClass {
2384 public:
2385 AClass() : n_(0) {}
2386
2387 // A getter that returns a non-reference.
2388 int n() const { return n_; }
2389
2390 void set_n(int new_n) { n_ = new_n; }
2391
2392 // A getter that returns a reference to const.
2393 const string& s() const { return s_; }
2394
2395 void set_s(const string& new_s) { s_ = new_s; }
2396
2397 // A getter that returns a reference to non-const.
2398 double& x() const { return x_; }
2399 private:
2400 int n_;
2401 string s_;
2402
2403 static double x_;
2404};
2405
2406double AClass::x_ = 0.0;
2407
2408// A derived class for testing Property().
2409class DerivedClass : public AClass {
2410 private:
2411 int k_;
2412};
2413
2414// Tests that Property(&Foo::property, ...) works when property()
2415// returns a non-reference.
2416TEST(PropertyTest, WorksForNonReferenceProperty) {
2417 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
2418
2419 AClass a;
2420 a.set_n(1);
2421 EXPECT_TRUE(m.Matches(a));
2422
2423 a.set_n(-1);
2424 EXPECT_FALSE(m.Matches(a));
2425}
2426
2427// Tests that Property(&Foo::property, ...) works when property()
2428// returns a reference to const.
2429TEST(PropertyTest, WorksForReferenceToConstProperty) {
2430 Matcher<const AClass&> m = Property(&AClass::s, StartsWith("hi"));
2431
2432 AClass a;
2433 a.set_s("hill");
2434 EXPECT_TRUE(m.Matches(a));
2435
2436 a.set_s("hole");
2437 EXPECT_FALSE(m.Matches(a));
2438}
2439
2440// Tests that Property(&Foo::property, ...) works when property()
2441// returns a reference to non-const.
2442TEST(PropertyTest, WorksForReferenceToNonConstProperty) {
2443 double x = 0.0;
2444 AClass a;
2445
2446 Matcher<const AClass&> m = Property(&AClass::x, Ref(x));
2447 EXPECT_FALSE(m.Matches(a));
2448
2449 m = Property(&AClass::x, Not(Ref(x)));
2450 EXPECT_TRUE(m.Matches(a));
2451}
2452
2453// Tests that Property(&Foo::property, ...) works when the argument is
2454// passed by value.
2455TEST(PropertyTest, WorksForByValueArgument) {
2456 Matcher<AClass> m = Property(&AClass::s, StartsWith("hi"));
2457
2458 AClass a;
2459 a.set_s("hill");
2460 EXPECT_TRUE(m.Matches(a));
2461
2462 a.set_s("hole");
2463 EXPECT_FALSE(m.Matches(a));
2464}
2465
2466// Tests that Property(&Foo::property, ...) works when the argument's
2467// type is a sub-type of Foo.
2468TEST(PropertyTest, WorksForArgumentOfSubType) {
2469 // The matcher expects a DerivedClass, but inside the Property() we
2470 // say AClass.
2471 Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0));
2472
2473 DerivedClass d;
2474 d.set_n(1);
2475 EXPECT_TRUE(m.Matches(d));
2476
2477 d.set_n(-1);
2478 EXPECT_FALSE(m.Matches(d));
2479}
2480
2481// Tests that Property(&Foo::property, m) works when property()'s type
2482// and m's argument type are compatible but different.
2483TEST(PropertyTest, WorksForCompatibleMatcherType) {
2484 // n() returns an int but the inner matcher expects a signed char.
2485 Matcher<const AClass&> m = Property(&AClass::n,
2486 Matcher<signed char>(Ge(0)));
2487
2488 AClass a;
2489 EXPECT_TRUE(m.Matches(a));
2490 a.set_n(-1);
2491 EXPECT_FALSE(m.Matches(a));
2492}
2493
2494// Tests that Property() can describe itself.
2495TEST(PropertyTest, CanDescribeSelf) {
2496 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
2497
2498 EXPECT_EQ("the given property is greater than or equal to 0", Describe(m));
2499 EXPECT_EQ("the given property is not greater than or equal to 0",
2500 DescribeNegation(m));
2501}
2502
2503// Tests that Property() can explain the match result.
2504TEST(PropertyTest, CanExplainMatchResult) {
2505 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
2506
2507 AClass a;
2508 a.set_n(1);
2509 EXPECT_EQ("", Explain(m, a));
2510
2511 m = Property(&AClass::n, GreaterThan(0));
2512 EXPECT_EQ("the given property is 1 more than 0", Explain(m, a));
2513}
2514
2515// Tests that Property() works when the argument is a pointer to const.
2516TEST(PropertyForPointerTest, WorksForPointerToConst) {
2517 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
2518
2519 AClass a;
2520 a.set_n(1);
2521 EXPECT_TRUE(m.Matches(&a));
2522
2523 a.set_n(-1);
2524 EXPECT_FALSE(m.Matches(&a));
2525}
2526
2527// Tests that Property() works when the argument is a pointer to non-const.
2528TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
2529 Matcher<AClass*> m = Property(&AClass::s, StartsWith("hi"));
2530
2531 AClass a;
2532 a.set_s("hill");
2533 EXPECT_TRUE(m.Matches(&a));
2534
2535 a.set_s("hole");
2536 EXPECT_FALSE(m.Matches(&a));
2537}
2538
2539// Tests that Property() does not match the NULL pointer.
2540TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
2541 Matcher<const AClass*> m = Property(&AClass::x, _);
2542 EXPECT_FALSE(m.Matches(NULL));
2543}
2544
2545// Tests that Property(&Foo::property, ...) works when the argument's
2546// type is a sub-type of const Foo*.
2547TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
2548 // The matcher expects a DerivedClass, but inside the Property() we
2549 // say AClass.
2550 Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0));
2551
2552 DerivedClass d;
2553 d.set_n(1);
2554 EXPECT_TRUE(m.Matches(&d));
2555
2556 d.set_n(-1);
2557 EXPECT_FALSE(m.Matches(&d));
2558}
2559
2560// Tests that Property() can describe itself when used to match a pointer.
2561TEST(PropertyForPointerTest, CanDescribeSelf) {
2562 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
2563
2564 EXPECT_EQ("the given property is greater than or equal to 0", Describe(m));
2565 EXPECT_EQ("the given property is not greater than or equal to 0",
2566 DescribeNegation(m));
2567}
2568
2569// Tests that Property() can explain the result of matching a pointer.
2570TEST(PropertyForPointerTest, CanExplainMatchResult) {
2571 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
2572
2573 AClass a;
2574 a.set_n(1);
2575 EXPECT_EQ("", Explain(m, static_cast<const AClass*>(NULL)));
2576 EXPECT_EQ("", Explain(m, &a));
2577
2578 m = Property(&AClass::n, GreaterThan(0));
2579 EXPECT_EQ("the given property is 1 more than 0", Explain(m, &a));
2580}
2581
2582// Tests ResultOf.
2583
2584// Tests that ResultOf(f, ...) compiles and works as expected when f is a
2585// function pointer.
2586string IntToStringFunction(int input) { return input == 1 ? "foo" : "bar"; }
2587
2588TEST(ResultOfTest, WorksForFunctionPointers) {
2589 Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(string("foo")));
2590
2591 EXPECT_TRUE(matcher.Matches(1));
2592 EXPECT_FALSE(matcher.Matches(2));
2593}
2594
2595// Tests that ResultOf() can describe itself.
2596TEST(ResultOfTest, CanDescribeItself) {
2597 Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo"));
2598
2599 EXPECT_EQ("result of the given callable is equal to \"foo\"",
2600 Describe(matcher));
2601 EXPECT_EQ("result of the given callable is not equal to \"foo\"",
2602 DescribeNegation(matcher));
2603}
2604
2605// Tests that ResultOf() can explain the match result.
2606int IntFunction(int input) { return input == 42 ? 80 : 90; }
2607
2608TEST(ResultOfTest, CanExplainMatchResult) {
2609 Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
2610 EXPECT_EQ("", Explain(matcher, 36));
2611
2612 matcher = ResultOf(&IntFunction, GreaterThan(85));
2613 EXPECT_EQ("result of the given callable is 5 more than 85",
2614 Explain(matcher, 36));
2615}
2616
2617// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
2618// returns a non-reference.
2619TEST(ResultOfTest, WorksForNonReferenceResults) {
2620 Matcher<int> matcher = ResultOf(&IntFunction, Eq(80));
2621
2622 EXPECT_TRUE(matcher.Matches(42));
2623 EXPECT_FALSE(matcher.Matches(36));
2624}
2625
2626// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
2627// returns a reference to non-const.
2628double& DoubleFunction(double& input) { return input; }
2629
2630Uncopyable& RefUncopyableFunction(Uncopyable& obj) {
2631 return obj;
2632}
2633
2634TEST(ResultOfTest, WorksForReferenceToNonConstResults) {
2635 double x = 3.14;
2636 double x2 = x;
2637 Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x));
2638
2639 EXPECT_TRUE(matcher.Matches(x));
2640 EXPECT_FALSE(matcher.Matches(x2));
2641
2642 // Test that ResultOf works with uncopyable objects
2643 Uncopyable obj(0);
2644 Uncopyable obj2(0);
2645 Matcher<Uncopyable&> matcher2 =
2646 ResultOf(&RefUncopyableFunction, Ref(obj));
2647
2648 EXPECT_TRUE(matcher2.Matches(obj));
2649 EXPECT_FALSE(matcher2.Matches(obj2));
2650}
2651
2652// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
2653// returns a reference to const.
2654const string& StringFunction(const string& input) { return input; }
2655
2656TEST(ResultOfTest, WorksForReferenceToConstResults) {
2657 string s = "foo";
2658 string s2 = s;
2659 Matcher<const string&> matcher = ResultOf(&StringFunction, Ref(s));
2660
2661 EXPECT_TRUE(matcher.Matches(s));
2662 EXPECT_FALSE(matcher.Matches(s2));
2663}
2664
2665// Tests that ResultOf(f, m) works when f(x) and m's
2666// argument types are compatible but different.
2667TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {
2668 // IntFunction() returns int but the inner matcher expects a signed char.
2669 Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85)));
2670
2671 EXPECT_TRUE(matcher.Matches(36));
2672 EXPECT_FALSE(matcher.Matches(42));
2673}
2674
zhanyong.wan652540a2009-02-23 23:37:29 +00002675#if GTEST_HAS_DEATH_TEST
shiqiane35fdd92008-12-10 05:08:54 +00002676// Tests that the program aborts when ResultOf is passed
2677// a NULL function pointer.
2678TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {
2679 EXPECT_DEATH(
2680 ResultOf(static_cast<string(*)(int)>(NULL), Eq(string("foo"))),
2681 "NULL function pointer is passed into ResultOf\\(\\)\\.");
2682}
2683#endif // GTEST_HAS_DEATH_TEST
2684
2685// Tests that ResultOf(f, ...) compiles and works as expected when f is a
2686// function reference.
2687TEST(ResultOfTest, WorksForFunctionReferences) {
2688 Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq("foo"));
2689 EXPECT_TRUE(matcher.Matches(1));
2690 EXPECT_FALSE(matcher.Matches(2));
2691}
2692
2693// Tests that ResultOf(f, ...) compiles and works as expected when f is a
2694// function object.
2695struct Functor : public ::std::unary_function<int, string> {
2696 result_type operator()(argument_type input) const {
2697 return IntToStringFunction(input);
2698 }
2699};
2700
2701TEST(ResultOfTest, WorksForFunctors) {
2702 Matcher<int> matcher = ResultOf(Functor(), Eq(string("foo")));
2703
2704 EXPECT_TRUE(matcher.Matches(1));
2705 EXPECT_FALSE(matcher.Matches(2));
2706}
2707
2708// Tests that ResultOf(f, ...) compiles and works as expected when f is a
2709// functor with more then one operator() defined. ResultOf() must work
2710// for each defined operator().
2711struct PolymorphicFunctor {
2712 typedef int result_type;
2713 int operator()(int n) { return n; }
2714 int operator()(const char* s) { return static_cast<int>(strlen(s)); }
2715};
2716
2717TEST(ResultOfTest, WorksForPolymorphicFunctors) {
2718 Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5));
2719
2720 EXPECT_TRUE(matcher_int.Matches(10));
2721 EXPECT_FALSE(matcher_int.Matches(2));
2722
2723 Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5));
2724
2725 EXPECT_TRUE(matcher_string.Matches("long string"));
2726 EXPECT_FALSE(matcher_string.Matches("shrt"));
2727}
2728
2729const int* ReferencingFunction(const int& n) { return &n; }
2730
2731struct ReferencingFunctor {
2732 typedef const int* result_type;
2733 result_type operator()(const int& n) { return &n; }
2734};
2735
2736TEST(ResultOfTest, WorksForReferencingCallables) {
2737 const int n = 1;
2738 const int n2 = 1;
2739 Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n));
2740 EXPECT_TRUE(matcher2.Matches(n));
2741 EXPECT_FALSE(matcher2.Matches(n2));
2742
2743 Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n));
2744 EXPECT_TRUE(matcher3.Matches(n));
2745 EXPECT_FALSE(matcher3.Matches(n2));
2746}
2747
2748
2749class DivisibleByImpl {
2750 public:
2751 explicit DivisibleByImpl(int divider) : divider_(divider) {}
2752
2753 template <typename T>
2754 bool Matches(const T& n) const {
2755 return (n % divider_) == 0;
2756 }
2757
2758 void DescribeTo(::std::ostream* os) const {
2759 *os << "is divisible by " << divider_;
2760 }
2761
2762 void DescribeNegationTo(::std::ostream* os) const {
2763 *os << "is not divisible by " << divider_;
2764 }
2765
2766 int divider() const { return divider_; }
2767 private:
2768 const int divider_;
2769};
2770
2771// For testing using ExplainMatchResultTo() with polymorphic matchers.
2772template <typename T>
2773void ExplainMatchResultTo(const DivisibleByImpl& impl, const T& n,
2774 ::std::ostream* os) {
2775 *os << "is " << (n % impl.divider()) << " modulo "
2776 << impl.divider();
2777}
2778
2779PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) {
2780 return MakePolymorphicMatcher(DivisibleByImpl(n));
2781}
2782
2783// Tests that when AllOf() fails, only the first failing matcher is
2784// asked to explain why.
2785TEST(ExplainMatchResultTest, AllOf_False_False) {
2786 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
2787 EXPECT_EQ("is 1 modulo 4", Explain(m, 5));
2788}
2789
2790// Tests that when AllOf() fails, only the first failing matcher is
2791// asked to explain why.
2792TEST(ExplainMatchResultTest, AllOf_False_True) {
2793 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
2794 EXPECT_EQ("is 2 modulo 4", Explain(m, 6));
2795}
2796
2797// Tests that when AllOf() fails, only the first failing matcher is
2798// asked to explain why.
2799TEST(ExplainMatchResultTest, AllOf_True_False) {
2800 const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));
2801 EXPECT_EQ("is 2 modulo 3", Explain(m, 5));
2802}
2803
2804// Tests that when AllOf() succeeds, all matchers are asked to explain
2805// why.
2806TEST(ExplainMatchResultTest, AllOf_True_True) {
2807 const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3));
2808 EXPECT_EQ("is 0 modulo 2; is 0 modulo 3", Explain(m, 6));
2809}
2810
2811TEST(ExplainMatchResultTest, AllOf_True_True_2) {
2812 const Matcher<int> m = AllOf(Ge(2), Le(3));
2813 EXPECT_EQ("", Explain(m, 2));
2814}
2815
2816TEST(ExplainmatcherResultTest, MonomorphicMatcher) {
2817 const Matcher<int> m = GreaterThan(5);
2818 EXPECT_EQ("is 1 more than 5", Explain(m, 6));
2819}
2820
2821// The following two tests verify that values without a public copy
2822// ctor can be used as arguments to matchers like Eq(), Ge(), and etc
2823// with the help of ByRef().
2824
2825class NotCopyable {
2826 public:
2827 explicit NotCopyable(int value) : value_(value) {}
2828
2829 int value() const { return value_; }
2830
2831 bool operator==(const NotCopyable& rhs) const {
2832 return value() == rhs.value();
2833 }
2834
2835 bool operator>=(const NotCopyable& rhs) const {
2836 return value() >= rhs.value();
2837 }
2838 private:
2839 int value_;
2840
2841 GTEST_DISALLOW_COPY_AND_ASSIGN_(NotCopyable);
2842};
2843
2844TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) {
2845 const NotCopyable const_value1(1);
2846 const Matcher<const NotCopyable&> m = Eq(ByRef(const_value1));
2847
2848 const NotCopyable n1(1), n2(2);
2849 EXPECT_TRUE(m.Matches(n1));
2850 EXPECT_FALSE(m.Matches(n2));
2851}
2852
2853TEST(ByRefTest, AllowsNotCopyableValueInMatchers) {
2854 NotCopyable value2(2);
2855 const Matcher<NotCopyable&> m = Ge(ByRef(value2));
2856
2857 NotCopyable n1(1), n2(2);
2858 EXPECT_FALSE(m.Matches(n1));
2859 EXPECT_TRUE(m.Matches(n2));
2860}
2861
zhanyong.wan6a896b52009-01-16 01:13:50 +00002862// Tests ContainerEq with different container types, and
2863// different element types.
2864
2865template <typename T>
zhanyong.wanb8243162009-06-04 05:48:20 +00002866class ContainerEqTest : public testing::Test {};
zhanyong.wan6a896b52009-01-16 01:13:50 +00002867
2868typedef testing::Types<
2869 std::set<int>,
2870 std::vector<size_t>,
2871 std::multiset<size_t>,
2872 std::list<int> >
2873 ContainerEqTestTypes;
2874
2875TYPED_TEST_CASE(ContainerEqTest, ContainerEqTestTypes);
2876
2877// Tests that the filled container is equal to itself.
2878TYPED_TEST(ContainerEqTest, EqualsSelf) {
2879 static const int vals[] = {1, 1, 2, 3, 5, 8};
2880 TypeParam my_set(vals, vals + 6);
2881 const Matcher<TypeParam> m = ContainerEq(my_set);
2882 EXPECT_TRUE(m.Matches(my_set));
2883 EXPECT_EQ("", Explain(m, my_set));
2884}
2885
2886// Tests that missing values are reported.
2887TYPED_TEST(ContainerEqTest, ValueMissing) {
2888 static const int vals[] = {1, 1, 2, 3, 5, 8};
2889 static const int test_vals[] = {2, 1, 8, 5};
2890 TypeParam my_set(vals, vals + 6);
2891 TypeParam test_set(test_vals, test_vals + 4);
2892 const Matcher<TypeParam> m = ContainerEq(my_set);
2893 EXPECT_FALSE(m.Matches(test_set));
2894 EXPECT_EQ("Not in actual: 3", Explain(m, test_set));
2895}
2896
2897// Tests that added values are reported.
2898TYPED_TEST(ContainerEqTest, ValueAdded) {
2899 static const int vals[] = {1, 1, 2, 3, 5, 8};
2900 static const int test_vals[] = {1, 2, 3, 5, 8, 46};
2901 TypeParam my_set(vals, vals + 6);
2902 TypeParam test_set(test_vals, test_vals + 6);
2903 const Matcher<const TypeParam&> m = ContainerEq(my_set);
2904 EXPECT_FALSE(m.Matches(test_set));
2905 EXPECT_EQ("Only in actual: 46", Explain(m, test_set));
2906}
2907
2908// Tests that added and missing values are reported together.
2909TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) {
2910 static const int vals[] = {1, 1, 2, 3, 5, 8};
2911 static const int test_vals[] = {1, 2, 3, 8, 46};
2912 TypeParam my_set(vals, vals + 6);
2913 TypeParam test_set(test_vals, test_vals + 5);
2914 const Matcher<TypeParam> m = ContainerEq(my_set);
2915 EXPECT_FALSE(m.Matches(test_set));
2916 EXPECT_EQ("Only in actual: 46; not in actual: 5", Explain(m, test_set));
2917}
2918
2919// Tests duplicated value -- expect no explanation.
2920TYPED_TEST(ContainerEqTest, DuplicateDifference) {
2921 static const int vals[] = {1, 1, 2, 3, 5, 8};
2922 static const int test_vals[] = {1, 2, 3, 5, 8};
2923 TypeParam my_set(vals, vals + 6);
2924 TypeParam test_set(test_vals, test_vals + 5);
2925 const Matcher<const TypeParam&> m = ContainerEq(my_set);
2926 // Depending on the container, match may be true or false
2927 // But in any case there should be no explanation.
2928 EXPECT_EQ("", Explain(m, test_set));
2929}
2930
2931// Tests that mutliple missing values are reported.
2932// Using just vector here, so order is predicatble.
2933TEST(ContainerEqExtraTest, MultipleValuesMissing) {
2934 static const int vals[] = {1, 1, 2, 3, 5, 8};
2935 static const int test_vals[] = {2, 1, 5};
2936 std::vector<int> my_set(vals, vals + 6);
2937 std::vector<int> test_set(test_vals, test_vals + 3);
2938 const Matcher<std::vector<int> > m = ContainerEq(my_set);
2939 EXPECT_FALSE(m.Matches(test_set));
2940 EXPECT_EQ("Not in actual: 3, 8", Explain(m, test_set));
2941}
2942
2943// Tests that added values are reported.
2944// Using just vector here, so order is predicatble.
2945TEST(ContainerEqExtraTest, MultipleValuesAdded) {
2946 static const int vals[] = {1, 1, 2, 3, 5, 8};
2947 static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46};
2948 std::list<size_t> my_set(vals, vals + 6);
2949 std::list<size_t> test_set(test_vals, test_vals + 7);
2950 const Matcher<const std::list<size_t>&> m = ContainerEq(my_set);
2951 EXPECT_FALSE(m.Matches(test_set));
2952 EXPECT_EQ("Only in actual: 92, 46", Explain(m, test_set));
2953}
2954
2955// Tests that added and missing values are reported together.
2956TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) {
2957 static const int vals[] = {1, 1, 2, 3, 5, 8};
2958 static const int test_vals[] = {1, 2, 3, 92, 46};
2959 std::list<size_t> my_set(vals, vals + 6);
2960 std::list<size_t> test_set(test_vals, test_vals + 5);
2961 const Matcher<const std::list<size_t> > m = ContainerEq(my_set);
2962 EXPECT_FALSE(m.Matches(test_set));
2963 EXPECT_EQ("Only in actual: 92, 46; not in actual: 5, 8",
2964 Explain(m, test_set));
2965}
2966
2967// Tests to see that duplicate elements are detected,
2968// but (as above) not reported in the explanation.
2969TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) {
2970 static const int vals[] = {1, 1, 2, 3, 5, 8};
2971 static const int test_vals[] = {1, 2, 3, 5, 8};
2972 std::vector<int> my_set(vals, vals + 6);
2973 std::vector<int> test_set(test_vals, test_vals + 5);
2974 const Matcher<std::vector<int> > m = ContainerEq(my_set);
2975 EXPECT_TRUE(m.Matches(my_set));
2976 EXPECT_FALSE(m.Matches(test_set));
2977 // There is nothing to report when both sets contain all the same values.
2978 EXPECT_EQ("", Explain(m, test_set));
2979}
2980
2981// Tests that ContainerEq works for non-trivial associative containers,
2982// like maps.
2983TEST(ContainerEqExtraTest, WorksForMaps) {
2984 std::map<int, std::string> my_map;
2985 my_map[0] = "a";
2986 my_map[1] = "b";
2987
2988 std::map<int, std::string> test_map;
2989 test_map[0] = "aa";
2990 test_map[1] = "b";
2991
2992 const Matcher<const std::map<int, std::string>&> m = ContainerEq(my_map);
2993 EXPECT_TRUE(m.Matches(my_map));
2994 EXPECT_FALSE(m.Matches(test_map));
2995
2996 EXPECT_EQ("Only in actual: (0, \"aa\"); not in actual: (0, \"a\")",
2997 Explain(m, test_map));
2998}
2999
zhanyong.wanb8243162009-06-04 05:48:20 +00003000TEST(ContainerEqExtraTest, WorksForNativeArray) {
3001 int a1[] = { 1, 2, 3 };
3002 int a2[] = { 1, 2, 3 };
3003 int b[] = { 1, 2, 4 };
3004
3005 EXPECT_THAT(a1, ContainerEq(a2));
3006 EXPECT_THAT(a1, Not(ContainerEq(b)));
3007}
3008
3009TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) {
3010 const char a1[][3] = { "hi", "lo" };
3011 const char a2[][3] = { "hi", "lo" };
3012 const char b[][3] = { "lo", "hi" };
3013
3014 // Tests using ContainerEq() in the first dimension.
3015 EXPECT_THAT(a1, ContainerEq(a2));
3016 EXPECT_THAT(a1, Not(ContainerEq(b)));
3017
3018 // Tests using ContainerEq() in the second dimension.
3019 EXPECT_THAT(a1, ElementsAre(ContainerEq(a2[0]), ContainerEq(a2[1])));
3020 EXPECT_THAT(a1, ElementsAre(Not(ContainerEq(b[0])), ContainerEq(a2[1])));
3021}
3022
3023TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) {
3024 const int a1[] = { 1, 2, 3 };
3025 const int a2[] = { 1, 2, 3 };
3026 const int b[] = { 1, 2, 3, 4 };
3027
zhanyong.wan2661c682009-06-09 05:42:12 +00003028 const int* const p1 = a1;
3029 EXPECT_THAT(make_tuple(p1, 3), ContainerEq(a2));
3030 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(b)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003031
3032 const int c[] = { 1, 3, 2 };
zhanyong.wan2661c682009-06-09 05:42:12 +00003033 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(c)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003034}
3035
3036TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) {
3037 std::string a1[][3] = {
3038 { "hi", "hello", "ciao" },
3039 { "bye", "see you", "ciao" }
3040 };
3041
3042 std::string a2[][3] = {
3043 { "hi", "hello", "ciao" },
3044 { "bye", "see you", "ciao" }
3045 };
3046
3047 const Matcher<const std::string(&)[2][3]> m = ContainerEq(a2);
3048 EXPECT_THAT(a1, m);
3049
3050 a2[0][0] = "ha";
3051 EXPECT_THAT(a1, m);
3052}
3053
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003054// Tests GetParamIndex().
3055
3056TEST(GetParamIndexTest, WorksForEmptyParamList) {
3057 const char* params[] = { NULL };
3058 EXPECT_EQ(kTupleInterpolation, GetParamIndex(params, "*"));
3059 EXPECT_EQ(kInvalidInterpolation, GetParamIndex(params, "a"));
3060}
3061
3062TEST(GetParamIndexTest, RecognizesStar) {
3063 const char* params[] = { "a", "b", NULL };
3064 EXPECT_EQ(kTupleInterpolation, GetParamIndex(params, "*"));
3065}
3066
3067TEST(GetParamIndexTest, RecognizesKnownParam) {
3068 const char* params[] = { "foo", "bar", NULL };
3069 EXPECT_EQ(0, GetParamIndex(params, "foo"));
3070 EXPECT_EQ(1, GetParamIndex(params, "bar"));
3071}
3072
3073TEST(GetParamIndexTest, RejectsUnknownParam) {
3074 const char* params[] = { "foo", "bar", NULL };
3075 EXPECT_EQ(kInvalidInterpolation, GetParamIndex(params, "foobar"));
3076}
3077
3078// Tests SkipPrefix().
3079
3080TEST(SkipPrefixTest, SkipsWhenPrefixMatches) {
3081 const char* const str = "hello";
3082
3083 const char* p = str;
3084 EXPECT_TRUE(SkipPrefix("", &p));
3085 EXPECT_EQ(str, p);
3086
3087 p = str;
3088 EXPECT_TRUE(SkipPrefix("hell", &p));
3089 EXPECT_EQ(str + 4, p);
3090}
3091
3092TEST(SkipPrefixTest, DoesNotSkipWhenPrefixDoesNotMatch) {
3093 const char* const str = "world";
3094
3095 const char* p = str;
3096 EXPECT_FALSE(SkipPrefix("W", &p));
3097 EXPECT_EQ(str, p);
3098
3099 p = str;
3100 EXPECT_FALSE(SkipPrefix("world!", &p));
3101 EXPECT_EQ(str, p);
3102}
3103
3104// Tests FormatMatcherDescriptionSyntaxError().
3105TEST(FormatMatcherDescriptionSyntaxErrorTest, FormatsCorrectly) {
3106 const char* const description = "hello%world";
3107 EXPECT_EQ("Syntax error at index 5 in matcher description \"hello%world\": ",
3108 FormatMatcherDescriptionSyntaxError(description, description + 5));
3109}
3110
3111// Tests ValidateMatcherDescription().
3112
3113TEST(ValidateMatcherDescriptionTest, AcceptsEmptyDescription) {
3114 const char* params[] = { "foo", "bar", NULL };
3115 EXPECT_THAT(ValidateMatcherDescription(params, ""),
3116 ElementsAre());
3117}
3118
3119TEST(ValidateMatcherDescriptionTest,
3120 AcceptsNonEmptyDescriptionWithNoInterpolation) {
3121 const char* params[] = { "foo", "bar", NULL };
3122 EXPECT_THAT(ValidateMatcherDescription(params, "a simple description"),
3123 ElementsAre());
3124}
3125
3126// We use MATCHER_P3() to define a matcher for testing
3127// ValidateMatcherDescription(); otherwise we'll end up with much
3128// plumbing code. This is not circular as
3129// ValidateMatcherDescription() doesn't affect whether the matcher
3130// matches a value or not.
3131MATCHER_P3(EqInterpolation, start, end, index, "equals Interpolation%(*)s") {
3132 return arg.start_pos == start && arg.end_pos == end &&
3133 arg.param_index == index;
3134}
3135
3136TEST(ValidateMatcherDescriptionTest, AcceptsPercentInterpolation) {
3137 const char* params[] = { "foo", NULL };
3138 const char* const desc = "one %%";
3139 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3140 ElementsAre(EqInterpolation(desc + 4, desc + 6,
3141 kPercentInterpolation)));
3142}
3143
3144TEST(ValidateMatcherDescriptionTest, AcceptsTupleInterpolation) {
3145 const char* params[] = { "foo", "bar", "baz", NULL };
3146 const char* const desc = "%(*)s after";
3147 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3148 ElementsAre(EqInterpolation(desc, desc + 5,
3149 kTupleInterpolation)));
3150}
3151
3152TEST(ValidateMatcherDescriptionTest, AcceptsParamInterpolation) {
3153 const char* params[] = { "foo", "bar", "baz", NULL };
3154 const char* const desc = "a %(bar)s.";
3155 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3156 ElementsAre(EqInterpolation(desc + 2, desc + 9, 1)));
3157}
3158
3159TEST(ValidateMatcherDescriptionTest, AcceptsMultiplenterpolations) {
3160 const char* params[] = { "foo", "bar", "baz", NULL };
3161 const char* const desc = "%(baz)s %(foo)s %(bar)s";
3162 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3163 ElementsAre(EqInterpolation(desc, desc + 7, 2),
3164 EqInterpolation(desc + 8, desc + 15, 0),
3165 EqInterpolation(desc + 16, desc + 23, 1)));
3166}
3167
3168TEST(ValidateMatcherDescriptionTest, AcceptsRepeatedParams) {
3169 const char* params[] = { "foo", "bar", NULL };
3170 const char* const desc = "%(foo)s and %(foo)s";
3171 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3172 ElementsAre(EqInterpolation(desc, desc + 7, 0),
3173 EqInterpolation(desc + 12, desc + 19, 0)));
3174}
3175
3176TEST(ValidateMatcherDescriptionTest, RejectsUnknownParam) {
3177 const char* params[] = { "a", "bar", NULL };
3178 EXPECT_NONFATAL_FAILURE({
3179 EXPECT_THAT(ValidateMatcherDescription(params, "%(foo)s"),
3180 ElementsAre());
3181 }, "Syntax error at index 2 in matcher description \"%(foo)s\": "
3182 "\"foo\" is an invalid parameter name.");
3183}
3184
3185TEST(ValidateMatcherDescriptionTest, RejectsUnfinishedParam) {
3186 const char* params[] = { "a", "bar", NULL };
3187 EXPECT_NONFATAL_FAILURE({
3188 EXPECT_THAT(ValidateMatcherDescription(params, "%(foo)"),
3189 ElementsAre());
3190 }, "Syntax error at index 0 in matcher description \"%(foo)\": "
3191 "an interpolation must end with \")s\", but \"%(foo)\" does not.");
3192
3193 EXPECT_NONFATAL_FAILURE({
3194 EXPECT_THAT(ValidateMatcherDescription(params, "x%(a"),
3195 ElementsAre());
3196 }, "Syntax error at index 1 in matcher description \"x%(a\": "
3197 "an interpolation must end with \")s\", but \"%(a\" does not.");
3198}
3199
3200TEST(ValidateMatcherDescriptionTest, RejectsSinglePercent) {
3201 const char* params[] = { "a", NULL };
3202 EXPECT_NONFATAL_FAILURE({
3203 EXPECT_THAT(ValidateMatcherDescription(params, "a %."),
3204 ElementsAre());
3205 }, "Syntax error at index 2 in matcher description \"a %.\": "
3206 "use \"%%\" instead of \"%\" to print \"%\".");
3207
3208}
3209
3210// Tests JoinAsTuple().
3211
3212TEST(JoinAsTupleTest, JoinsEmptyTuple) {
3213 EXPECT_EQ("", JoinAsTuple(Strings()));
3214}
3215
3216TEST(JoinAsTupleTest, JoinsOneTuple) {
3217 const char* fields[] = { "1" };
3218 EXPECT_EQ("1", JoinAsTuple(Strings(fields, fields + 1)));
3219}
3220
3221TEST(JoinAsTupleTest, JoinsTwoTuple) {
3222 const char* fields[] = { "1", "a" };
3223 EXPECT_EQ("(1, a)", JoinAsTuple(Strings(fields, fields + 2)));
3224}
3225
3226TEST(JoinAsTupleTest, JoinsTenTuple) {
3227 const char* fields[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
3228 EXPECT_EQ("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)",
3229 JoinAsTuple(Strings(fields, fields + 10)));
3230}
3231
3232// Tests FormatMatcherDescription().
3233
3234TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) {
3235 EXPECT_EQ("is even",
3236 FormatMatcherDescription("IsEven", "", Interpolations(),
3237 Strings()));
3238
3239 const char* params[] = { "5" };
3240 EXPECT_EQ("equals 5",
3241 FormatMatcherDescription("Equals", "", Interpolations(),
3242 Strings(params, params + 1)));
3243
3244 const char* params2[] = { "5", "8" };
3245 EXPECT_EQ("is in range (5, 8)",
3246 FormatMatcherDescription("IsInRange", "", Interpolations(),
3247 Strings(params2, params2 + 2)));
3248}
3249
3250TEST(FormatMatcherDescriptionTest, WorksForDescriptionWithNoInterpolation) {
3251 EXPECT_EQ("is positive",
3252 FormatMatcherDescription("Gt0", "is positive", Interpolations(),
3253 Strings()));
3254
3255 const char* params[] = { "5", "6" };
3256 EXPECT_EQ("is negative",
3257 FormatMatcherDescription("Lt0", "is negative", Interpolations(),
3258 Strings(params, params + 2)));
3259}
3260
3261TEST(FormatMatcherDescriptionTest,
3262 WorksWhenDescriptionStartsWithInterpolation) {
3263 const char* params[] = { "5" };
3264 const char* const desc = "%(num)s times bigger";
3265 const Interpolation interp[] = { Interpolation(desc, desc + 7, 0) };
3266 EXPECT_EQ("5 times bigger",
3267 FormatMatcherDescription("Foo", desc,
3268 Interpolations(interp, interp + 1),
3269 Strings(params, params + 1)));
3270}
3271
3272TEST(FormatMatcherDescriptionTest,
3273 WorksWhenDescriptionEndsWithInterpolation) {
3274 const char* params[] = { "5", "6" };
3275 const char* const desc = "is bigger than %(y)s";
3276 const Interpolation interp[] = { Interpolation(desc + 15, desc + 20, 1) };
3277 EXPECT_EQ("is bigger than 6",
3278 FormatMatcherDescription("Foo", desc,
3279 Interpolations(interp, interp + 1),
3280 Strings(params, params + 2)));
3281}
3282
3283TEST(FormatMatcherDescriptionTest,
3284 WorksWhenDescriptionStartsAndEndsWithInterpolation) {
3285 const char* params[] = { "5", "6" };
3286 const char* const desc = "%(x)s <= arg <= %(y)s";
3287 const Interpolation interp[] = {
3288 Interpolation(desc, desc + 5, 0),
3289 Interpolation(desc + 16, desc + 21, 1)
3290 };
3291 EXPECT_EQ("5 <= arg <= 6",
3292 FormatMatcherDescription("Foo", desc,
3293 Interpolations(interp, interp + 2),
3294 Strings(params, params + 2)));
3295}
3296
3297TEST(FormatMatcherDescriptionTest,
3298 WorksWhenDescriptionDoesNotStartOrEndWithInterpolation) {
3299 const char* params[] = { "5.2" };
3300 const char* const desc = "has %(x)s cents";
3301 const Interpolation interp[] = { Interpolation(desc + 4, desc + 9, 0) };
3302 EXPECT_EQ("has 5.2 cents",
3303 FormatMatcherDescription("Foo", desc,
3304 Interpolations(interp, interp + 1),
3305 Strings(params, params + 1)));
3306}
3307
3308TEST(FormatMatcherDescriptionTest,
3309 WorksWhenDescriptionContainsMultipleInterpolations) {
3310 const char* params[] = { "5", "6" };
3311 const char* const desc = "in %(*)s or [%(x)s, %(y)s]";
3312 const Interpolation interp[] = {
3313 Interpolation(desc + 3, desc + 8, kTupleInterpolation),
3314 Interpolation(desc + 13, desc + 18, 0),
3315 Interpolation(desc + 20, desc + 25, 1)
3316 };
3317 EXPECT_EQ("in (5, 6) or [5, 6]",
3318 FormatMatcherDescription("Foo", desc,
3319 Interpolations(interp, interp + 3),
3320 Strings(params, params + 2)));
3321}
3322
3323TEST(FormatMatcherDescriptionTest,
3324 WorksWhenDescriptionContainsRepeatedParams) {
3325 const char* params[] = { "9" };
3326 const char* const desc = "in [-%(x)s, %(x)s]";
3327 const Interpolation interp[] = {
3328 Interpolation(desc + 5, desc + 10, 0),
3329 Interpolation(desc + 12, desc + 17, 0)
3330 };
3331 EXPECT_EQ("in [-9, 9]",
3332 FormatMatcherDescription("Foo", desc,
3333 Interpolations(interp, interp + 2),
3334 Strings(params, params + 1)));
3335}
3336
3337TEST(FormatMatcherDescriptionTest,
3338 WorksForDescriptionWithInvalidInterpolation) {
3339 const char* params[] = { "9" };
3340 const char* const desc = "> %(x)s %(x)";
3341 const Interpolation interp[] = { Interpolation(desc + 2, desc + 7, 0) };
3342 EXPECT_EQ("> 9 %(x)",
3343 FormatMatcherDescription("Foo", desc,
3344 Interpolations(interp, interp + 1),
3345 Strings(params, params + 1)));
3346}
3347
shiqiane35fdd92008-12-10 05:08:54 +00003348} // namespace gmock_matchers_test
3349} // namespace testing