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