blob: 1226a1d18dc2ea5175308144187a1c92d32d6f58 [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();
1338 EXPECT_EQ("argument #0 is equal to argument #1", Describe(m));
1339}
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();
1353 EXPECT_EQ("argument #0 is greater than or equal to argument #1",
1354 Describe(m));
1355}
1356
1357// Tests that Gt() matches a 2-tuple where the first field > the
1358// second field.
1359TEST(Gt2Test, MatchesGreaterThanArguments) {
1360 Matcher<const Tuple2&> m = Gt();
1361 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1362 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1363 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1364}
1365
1366// Tests that Gt() describes itself properly.
1367TEST(Gt2Test, CanDescribeSelf) {
1368 Matcher<const Tuple2&> m = Gt();
1369 EXPECT_EQ("argument #0 is greater than argument #1", Describe(m));
1370}
1371
1372// Tests that Le() matches a 2-tuple where the first field <= the
1373// second field.
1374TEST(Le2Test, MatchesLessThanOrEqualArguments) {
1375 Matcher<const Tuple2&> m = Le();
1376 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1377 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1378 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1379}
1380
1381// Tests that Le() describes itself properly.
1382TEST(Le2Test, CanDescribeSelf) {
1383 Matcher<const Tuple2&> m = Le();
1384 EXPECT_EQ("argument #0 is less than or equal to argument #1",
1385 Describe(m));
1386}
1387
1388// Tests that Lt() matches a 2-tuple where the first field < the
1389// second field.
1390TEST(Lt2Test, MatchesLessThanArguments) {
1391 Matcher<const Tuple2&> m = Lt();
1392 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1393 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1394 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1395}
1396
1397// Tests that Lt() describes itself properly.
1398TEST(Lt2Test, CanDescribeSelf) {
1399 Matcher<const Tuple2&> m = Lt();
1400 EXPECT_EQ("argument #0 is less than argument #1", Describe(m));
1401}
1402
1403// Tests that Ne() matches a 2-tuple where the first field != the
1404// second field.
1405TEST(Ne2Test, MatchesUnequalArguments) {
1406 Matcher<const Tuple2&> m = Ne();
1407 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1408 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1409 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1410}
1411
1412// Tests that Ne() describes itself properly.
1413TEST(Ne2Test, CanDescribeSelf) {
1414 Matcher<const Tuple2&> m = Ne();
1415 EXPECT_EQ("argument #0 is not equal to argument #1", Describe(m));
1416}
1417
1418// Tests that Not(m) matches any value that doesn't match m.
1419TEST(NotTest, NegatesMatcher) {
1420 Matcher<int> m;
1421 m = Not(Eq(2));
1422 EXPECT_TRUE(m.Matches(3));
1423 EXPECT_FALSE(m.Matches(2));
1424}
1425
1426// Tests that Not(m) describes itself properly.
1427TEST(NotTest, CanDescribeSelf) {
1428 Matcher<int> m = Not(Eq(5));
1429 EXPECT_EQ("is not equal to 5", Describe(m));
1430}
1431
zhanyong.wan18490652009-05-11 18:54:08 +00001432// Tests that monomorphic matchers are safely cast by the Not matcher.
1433TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {
1434 // greater_than_5 is a monomorphic matcher.
1435 Matcher<int> greater_than_5 = Gt(5);
1436
1437 Matcher<const int&> m = Not(greater_than_5);
1438 Matcher<int&> m2 = Not(greater_than_5);
1439 Matcher<int&> m3 = Not(m);
1440}
1441
shiqiane35fdd92008-12-10 05:08:54 +00001442// Tests that AllOf(m1, ..., mn) matches any value that matches all of
1443// the given matchers.
1444TEST(AllOfTest, MatchesWhenAllMatch) {
1445 Matcher<int> m;
1446 m = AllOf(Le(2), Ge(1));
1447 EXPECT_TRUE(m.Matches(1));
1448 EXPECT_TRUE(m.Matches(2));
1449 EXPECT_FALSE(m.Matches(0));
1450 EXPECT_FALSE(m.Matches(3));
1451
1452 m = AllOf(Gt(0), Ne(1), Ne(2));
1453 EXPECT_TRUE(m.Matches(3));
1454 EXPECT_FALSE(m.Matches(2));
1455 EXPECT_FALSE(m.Matches(1));
1456 EXPECT_FALSE(m.Matches(0));
1457
1458 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
1459 EXPECT_TRUE(m.Matches(4));
1460 EXPECT_FALSE(m.Matches(3));
1461 EXPECT_FALSE(m.Matches(2));
1462 EXPECT_FALSE(m.Matches(1));
1463 EXPECT_FALSE(m.Matches(0));
1464
1465 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
1466 EXPECT_TRUE(m.Matches(0));
1467 EXPECT_TRUE(m.Matches(1));
1468 EXPECT_FALSE(m.Matches(3));
1469}
1470
1471// Tests that AllOf(m1, ..., mn) describes itself properly.
1472TEST(AllOfTest, CanDescribeSelf) {
1473 Matcher<int> m;
1474 m = AllOf(Le(2), Ge(1));
1475 EXPECT_EQ("(is less than or equal to 2) and "
1476 "(is greater than or equal to 1)",
1477 Describe(m));
1478
1479 m = AllOf(Gt(0), Ne(1), Ne(2));
1480 EXPECT_EQ("(is greater than 0) and "
1481 "((is not equal to 1) and "
1482 "(is not equal to 2))",
1483 Describe(m));
1484
1485
1486 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
1487 EXPECT_EQ("(is greater than 0) and "
1488 "((is not equal to 1) and "
1489 "((is not equal to 2) and "
1490 "(is not equal to 3)))",
1491 Describe(m));
1492
1493
1494 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
1495 EXPECT_EQ("(is greater than or equal to 0) and "
1496 "((is less than 10) and "
1497 "((is not equal to 3) and "
1498 "((is not equal to 5) and "
1499 "(is not equal to 7))))", Describe(m));
1500}
1501
zhanyong.wan18490652009-05-11 18:54:08 +00001502// Tests that monomorphic matchers are safely cast by the AllOf matcher.
1503TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
1504 // greater_than_5 and less_than_10 are monomorphic matchers.
1505 Matcher<int> greater_than_5 = Gt(5);
1506 Matcher<int> less_than_10 = Lt(10);
1507
1508 Matcher<const int&> m = AllOf(greater_than_5, less_than_10);
1509 Matcher<int&> m2 = AllOf(greater_than_5, less_than_10);
1510 Matcher<int&> m3 = AllOf(greater_than_5, m2);
1511
1512 // Tests that BothOf works when composing itself.
1513 Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10);
1514 Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);
1515}
1516
shiqiane35fdd92008-12-10 05:08:54 +00001517// Tests that AnyOf(m1, ..., mn) matches any value that matches at
1518// least one of the given matchers.
1519TEST(AnyOfTest, MatchesWhenAnyMatches) {
1520 Matcher<int> m;
1521 m = AnyOf(Le(1), Ge(3));
1522 EXPECT_TRUE(m.Matches(1));
1523 EXPECT_TRUE(m.Matches(4));
1524 EXPECT_FALSE(m.Matches(2));
1525
1526 m = AnyOf(Lt(0), Eq(1), Eq(2));
1527 EXPECT_TRUE(m.Matches(-1));
1528 EXPECT_TRUE(m.Matches(1));
1529 EXPECT_TRUE(m.Matches(2));
1530 EXPECT_FALSE(m.Matches(0));
1531
1532 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
1533 EXPECT_TRUE(m.Matches(-1));
1534 EXPECT_TRUE(m.Matches(1));
1535 EXPECT_TRUE(m.Matches(2));
1536 EXPECT_TRUE(m.Matches(3));
1537 EXPECT_FALSE(m.Matches(0));
1538
1539 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
1540 EXPECT_TRUE(m.Matches(0));
1541 EXPECT_TRUE(m.Matches(11));
1542 EXPECT_TRUE(m.Matches(3));
1543 EXPECT_FALSE(m.Matches(2));
1544}
1545
1546// Tests that AnyOf(m1, ..., mn) describes itself properly.
1547TEST(AnyOfTest, CanDescribeSelf) {
1548 Matcher<int> m;
1549 m = AnyOf(Le(1), Ge(3));
1550 EXPECT_EQ("(is less than or equal to 1) or "
1551 "(is greater than or equal to 3)",
1552 Describe(m));
1553
1554 m = AnyOf(Lt(0), Eq(1), Eq(2));
1555 EXPECT_EQ("(is less than 0) or "
1556 "((is equal to 1) or (is equal to 2))",
1557 Describe(m));
1558
1559 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
1560 EXPECT_EQ("(is less than 0) or "
1561 "((is equal to 1) or "
1562 "((is equal to 2) or "
1563 "(is equal to 3)))",
1564 Describe(m));
1565
1566 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
1567 EXPECT_EQ("(is less than or equal to 0) or "
1568 "((is greater than 10) or "
1569 "((is equal to 3) or "
1570 "((is equal to 5) or "
1571 "(is equal to 7))))",
1572 Describe(m));
1573}
1574
zhanyong.wan18490652009-05-11 18:54:08 +00001575// Tests that monomorphic matchers are safely cast by the AnyOf matcher.
1576TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
1577 // greater_than_5 and less_than_10 are monomorphic matchers.
1578 Matcher<int> greater_than_5 = Gt(5);
1579 Matcher<int> less_than_10 = Lt(10);
1580
1581 Matcher<const int&> m = AnyOf(greater_than_5, less_than_10);
1582 Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10);
1583 Matcher<int&> m3 = AnyOf(greater_than_5, m2);
1584
1585 // Tests that EitherOf works when composing itself.
1586 Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10);
1587 Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);
1588}
1589
shiqiane35fdd92008-12-10 05:08:54 +00001590// The following predicate function and predicate functor are for
1591// testing the Truly(predicate) matcher.
1592
1593// Returns non-zero if the input is positive. Note that the return
1594// type of this function is not bool. It's OK as Truly() accepts any
1595// unary function or functor whose return type can be implicitly
1596// converted to bool.
1597int IsPositive(double x) {
1598 return x > 0 ? 1 : 0;
1599}
1600
1601// This functor returns true if the input is greater than the given
1602// number.
1603class IsGreaterThan {
1604 public:
1605 explicit IsGreaterThan(int threshold) : threshold_(threshold) {}
1606
1607 bool operator()(int n) const { return n > threshold_; }
1608 private:
1609 const int threshold_;
1610};
1611
1612// For testing Truly().
1613const int foo = 0;
1614
1615// This predicate returns true iff the argument references foo and has
1616// a zero value.
1617bool ReferencesFooAndIsZero(const int& n) {
1618 return (&n == &foo) && (n == 0);
1619}
1620
1621// Tests that Truly(predicate) matches what satisfies the given
1622// predicate.
1623TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {
1624 Matcher<double> m = Truly(IsPositive);
1625 EXPECT_TRUE(m.Matches(2.0));
1626 EXPECT_FALSE(m.Matches(-1.5));
1627}
1628
1629// Tests that Truly(predicate_functor) works too.
1630TEST(TrulyTest, CanBeUsedWithFunctor) {
1631 Matcher<int> m = Truly(IsGreaterThan(5));
1632 EXPECT_TRUE(m.Matches(6));
1633 EXPECT_FALSE(m.Matches(4));
1634}
1635
1636// Tests that Truly(predicate) can describe itself properly.
1637TEST(TrulyTest, CanDescribeSelf) {
1638 Matcher<double> m = Truly(IsPositive);
1639 EXPECT_EQ("satisfies the given predicate",
1640 Describe(m));
1641}
1642
1643// Tests that Truly(predicate) works when the matcher takes its
1644// argument by reference.
1645TEST(TrulyTest, WorksForByRefArguments) {
1646 Matcher<const int&> m = Truly(ReferencesFooAndIsZero);
1647 EXPECT_TRUE(m.Matches(foo));
1648 int n = 0;
1649 EXPECT_FALSE(m.Matches(n));
1650}
1651
1652// Tests that Matches(m) is a predicate satisfied by whatever that
1653// matches matcher m.
1654TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {
1655 EXPECT_TRUE(Matches(Ge(0))(1));
1656 EXPECT_FALSE(Matches(Eq('a'))('b'));
1657}
1658
1659// Tests that Matches(m) works when the matcher takes its argument by
1660// reference.
1661TEST(MatchesTest, WorksOnByRefArguments) {
1662 int m = 0, n = 0;
1663 EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n));
1664 EXPECT_FALSE(Matches(Ref(m))(n));
1665}
1666
1667// Tests that a Matcher on non-reference type can be used in
1668// Matches().
1669TEST(MatchesTest, WorksWithMatcherOnNonRefType) {
1670 Matcher<int> eq5 = Eq(5);
1671 EXPECT_TRUE(Matches(eq5)(5));
1672 EXPECT_FALSE(Matches(eq5)(2));
1673}
1674
zhanyong.wanb8243162009-06-04 05:48:20 +00001675// Tests Value(value, matcher). Since Value() is a simple wrapper for
1676// Matches(), which has been tested already, we don't spend a lot of
1677// effort on testing Value().
1678TEST(ValueTest, WorksWithPolymorphicMatcher) {
1679 EXPECT_TRUE(Value("hi", StartsWith("h")));
1680 EXPECT_FALSE(Value(5, Gt(10)));
1681}
1682
1683TEST(ValueTest, WorksWithMonomorphicMatcher) {
1684 const Matcher<int> is_zero = Eq(0);
1685 EXPECT_TRUE(Value(0, is_zero));
1686 EXPECT_FALSE(Value('a', is_zero));
1687
1688 int n = 0;
1689 const Matcher<const int&> ref_n = Ref(n);
1690 EXPECT_TRUE(Value(n, ref_n));
1691 EXPECT_FALSE(Value(1, ref_n));
1692}
1693
shiqiane35fdd92008-12-10 05:08:54 +00001694// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
1695// matches the matcher.
1696TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {
1697 ASSERT_THAT(5, Ge(2)) << "This should succeed.";
1698 ASSERT_THAT("Foo", EndsWith("oo"));
1699 EXPECT_THAT(2, AllOf(Le(7), Ge(0))) << "This should succeed too.";
1700 EXPECT_THAT("Hello", StartsWith("Hell"));
1701}
1702
1703// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
1704// doesn't match the matcher.
1705TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {
1706 // 'n' must be static as it is used in an EXPECT_FATAL_FAILURE(),
1707 // which cannot reference auto variables.
1708 static int n;
1709 n = 5;
1710 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Gt(10)) << "This should fail.",
1711 "Value of: n\n"
1712 "Expected: is greater than 10\n"
1713 " Actual: 5\n"
1714 "This should fail.");
1715 n = 0;
1716 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(n, AllOf(Le(7), Ge(5))),
1717 "Value of: n\n"
1718 "Expected: (is less than or equal to 7) and "
1719 "(is greater than or equal to 5)\n"
1720 " Actual: 0");
1721}
1722
1723// Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument
1724// has a reference type.
1725TEST(MatcherAssertionTest, WorksForByRefArguments) {
1726 // We use a static variable here as EXPECT_FATAL_FAILURE() cannot
1727 // reference auto variables.
1728 static int n;
1729 n = 0;
1730 EXPECT_THAT(n, AllOf(Le(7), Ref(n)));
1731 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Not(Ref(n))),
1732 "Value of: n\n"
1733 "Expected: does not reference the variable @");
1734 // Tests the "Actual" part.
1735 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Not(Ref(n))),
1736 "Actual: 0 (is located @");
1737}
1738
1739// Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is
1740// monomorphic.
1741TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
1742 Matcher<const char*> starts_with_he = StartsWith("he");
1743 ASSERT_THAT("hello", starts_with_he);
1744
1745 Matcher<const string&> ends_with_ok = EndsWith("ok");
1746 ASSERT_THAT("book", ends_with_ok);
1747
1748 Matcher<int> is_greater_than_5 = Gt(5);
1749 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5),
1750 "Value of: 5\n"
1751 "Expected: is greater than 5\n"
1752 " Actual: 5");
1753}
1754
1755// Tests floating-point matchers.
1756template <typename RawType>
1757class FloatingPointTest : public testing::Test {
1758 protected:
1759 typedef typename testing::internal::FloatingPoint<RawType> Floating;
1760 typedef typename Floating::Bits Bits;
1761
1762 virtual void SetUp() {
1763 const size_t max_ulps = Floating::kMaxUlps;
1764
1765 // The bits that represent 0.0.
1766 const Bits zero_bits = Floating(0).bits();
1767
1768 // Makes some numbers close to 0.0.
1769 close_to_positive_zero_ = Floating::ReinterpretBits(zero_bits + max_ulps/2);
1770 close_to_negative_zero_ = -Floating::ReinterpretBits(
1771 zero_bits + max_ulps - max_ulps/2);
1772 further_from_negative_zero_ = -Floating::ReinterpretBits(
1773 zero_bits + max_ulps + 1 - max_ulps/2);
1774
1775 // The bits that represent 1.0.
1776 const Bits one_bits = Floating(1).bits();
1777
1778 // Makes some numbers close to 1.0.
1779 close_to_one_ = Floating::ReinterpretBits(one_bits + max_ulps);
1780 further_from_one_ = Floating::ReinterpretBits(one_bits + max_ulps + 1);
1781
1782 // +infinity.
1783 infinity_ = Floating::Infinity();
1784
1785 // The bits that represent +infinity.
1786 const Bits infinity_bits = Floating(infinity_).bits();
1787
1788 // Makes some numbers close to infinity.
1789 close_to_infinity_ = Floating::ReinterpretBits(infinity_bits - max_ulps);
1790 further_from_infinity_ = Floating::ReinterpretBits(
1791 infinity_bits - max_ulps - 1);
1792
1793 // Makes some NAN's.
1794 nan1_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 1);
1795 nan2_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 200);
1796 }
1797
1798 void TestSize() {
1799 EXPECT_EQ(sizeof(RawType), sizeof(Bits));
1800 }
1801
1802 // A battery of tests for FloatingEqMatcher::Matches.
1803 // matcher_maker is a pointer to a function which creates a FloatingEqMatcher.
1804 void TestMatches(
1805 testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) {
1806 Matcher<RawType> m1 = matcher_maker(0.0);
1807 EXPECT_TRUE(m1.Matches(-0.0));
1808 EXPECT_TRUE(m1.Matches(close_to_positive_zero_));
1809 EXPECT_TRUE(m1.Matches(close_to_negative_zero_));
1810 EXPECT_FALSE(m1.Matches(1.0));
1811
1812 Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_);
1813 EXPECT_FALSE(m2.Matches(further_from_negative_zero_));
1814
1815 Matcher<RawType> m3 = matcher_maker(1.0);
1816 EXPECT_TRUE(m3.Matches(close_to_one_));
1817 EXPECT_FALSE(m3.Matches(further_from_one_));
1818
1819 // Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above.
1820 EXPECT_FALSE(m3.Matches(0.0));
1821
1822 Matcher<RawType> m4 = matcher_maker(-infinity_);
1823 EXPECT_TRUE(m4.Matches(-close_to_infinity_));
1824
1825 Matcher<RawType> m5 = matcher_maker(infinity_);
1826 EXPECT_TRUE(m5.Matches(close_to_infinity_));
1827
1828 // This is interesting as the representations of infinity_ and nan1_
1829 // are only 1 DLP apart.
1830 EXPECT_FALSE(m5.Matches(nan1_));
1831
1832 // matcher_maker can produce a Matcher<const RawType&>, which is needed in
1833 // some cases.
1834 Matcher<const RawType&> m6 = matcher_maker(0.0);
1835 EXPECT_TRUE(m6.Matches(-0.0));
1836 EXPECT_TRUE(m6.Matches(close_to_positive_zero_));
1837 EXPECT_FALSE(m6.Matches(1.0));
1838
1839 // matcher_maker can produce a Matcher<RawType&>, which is needed in some
1840 // cases.
1841 Matcher<RawType&> m7 = matcher_maker(0.0);
1842 RawType x = 0.0;
1843 EXPECT_TRUE(m7.Matches(x));
1844 x = 0.01f;
1845 EXPECT_FALSE(m7.Matches(x));
1846 }
1847
1848 // Pre-calculated numbers to be used by the tests.
1849
1850 static RawType close_to_positive_zero_;
1851 static RawType close_to_negative_zero_;
1852 static RawType further_from_negative_zero_;
1853
1854 static RawType close_to_one_;
1855 static RawType further_from_one_;
1856
1857 static RawType infinity_;
1858 static RawType close_to_infinity_;
1859 static RawType further_from_infinity_;
1860
1861 static RawType nan1_;
1862 static RawType nan2_;
1863};
1864
1865template <typename RawType>
1866RawType FloatingPointTest<RawType>::close_to_positive_zero_;
1867
1868template <typename RawType>
1869RawType FloatingPointTest<RawType>::close_to_negative_zero_;
1870
1871template <typename RawType>
1872RawType FloatingPointTest<RawType>::further_from_negative_zero_;
1873
1874template <typename RawType>
1875RawType FloatingPointTest<RawType>::close_to_one_;
1876
1877template <typename RawType>
1878RawType FloatingPointTest<RawType>::further_from_one_;
1879
1880template <typename RawType>
1881RawType FloatingPointTest<RawType>::infinity_;
1882
1883template <typename RawType>
1884RawType FloatingPointTest<RawType>::close_to_infinity_;
1885
1886template <typename RawType>
1887RawType FloatingPointTest<RawType>::further_from_infinity_;
1888
1889template <typename RawType>
1890RawType FloatingPointTest<RawType>::nan1_;
1891
1892template <typename RawType>
1893RawType FloatingPointTest<RawType>::nan2_;
1894
1895// Instantiate FloatingPointTest for testing floats.
1896typedef FloatingPointTest<float> FloatTest;
1897
1898TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) {
1899 TestMatches(&FloatEq);
1900}
1901
1902TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
1903 TestMatches(&NanSensitiveFloatEq);
1904}
1905
1906TEST_F(FloatTest, FloatEqCannotMatchNaN) {
1907 // FloatEq never matches NaN.
1908 Matcher<float> m = FloatEq(nan1_);
1909 EXPECT_FALSE(m.Matches(nan1_));
1910 EXPECT_FALSE(m.Matches(nan2_));
1911 EXPECT_FALSE(m.Matches(1.0));
1912}
1913
1914TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {
1915 // NanSensitiveFloatEq will match NaN.
1916 Matcher<float> m = NanSensitiveFloatEq(nan1_);
1917 EXPECT_TRUE(m.Matches(nan1_));
1918 EXPECT_TRUE(m.Matches(nan2_));
1919 EXPECT_FALSE(m.Matches(1.0));
1920}
1921
1922TEST_F(FloatTest, FloatEqCanDescribeSelf) {
1923 Matcher<float> m1 = FloatEq(2.0f);
1924 EXPECT_EQ("is approximately 2", Describe(m1));
1925 EXPECT_EQ("is not approximately 2", DescribeNegation(m1));
1926
1927 Matcher<float> m2 = FloatEq(0.5f);
1928 EXPECT_EQ("is approximately 0.5", Describe(m2));
1929 EXPECT_EQ("is not approximately 0.5", DescribeNegation(m2));
1930
1931 Matcher<float> m3 = FloatEq(nan1_);
1932 EXPECT_EQ("never matches", Describe(m3));
1933 EXPECT_EQ("is anything", DescribeNegation(m3));
1934}
1935
1936TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {
1937 Matcher<float> m1 = NanSensitiveFloatEq(2.0f);
1938 EXPECT_EQ("is approximately 2", Describe(m1));
1939 EXPECT_EQ("is not approximately 2", DescribeNegation(m1));
1940
1941 Matcher<float> m2 = NanSensitiveFloatEq(0.5f);
1942 EXPECT_EQ("is approximately 0.5", Describe(m2));
1943 EXPECT_EQ("is not approximately 0.5", DescribeNegation(m2));
1944
1945 Matcher<float> m3 = NanSensitiveFloatEq(nan1_);
1946 EXPECT_EQ("is NaN", Describe(m3));
1947 EXPECT_EQ("is not NaN", DescribeNegation(m3));
1948}
1949
1950// Instantiate FloatingPointTest for testing doubles.
1951typedef FloatingPointTest<double> DoubleTest;
1952
1953TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
1954 TestMatches(&DoubleEq);
1955}
1956
1957TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
1958 TestMatches(&NanSensitiveDoubleEq);
1959}
1960
1961TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {
1962 // DoubleEq never matches NaN.
1963 Matcher<double> m = DoubleEq(nan1_);
1964 EXPECT_FALSE(m.Matches(nan1_));
1965 EXPECT_FALSE(m.Matches(nan2_));
1966 EXPECT_FALSE(m.Matches(1.0));
1967}
1968
1969TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {
1970 // NanSensitiveDoubleEq will match NaN.
1971 Matcher<double> m = NanSensitiveDoubleEq(nan1_);
1972 EXPECT_TRUE(m.Matches(nan1_));
1973 EXPECT_TRUE(m.Matches(nan2_));
1974 EXPECT_FALSE(m.Matches(1.0));
1975}
1976
1977TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {
1978 Matcher<double> m1 = DoubleEq(2.0);
1979 EXPECT_EQ("is approximately 2", Describe(m1));
1980 EXPECT_EQ("is not approximately 2", DescribeNegation(m1));
1981
1982 Matcher<double> m2 = DoubleEq(0.5);
1983 EXPECT_EQ("is approximately 0.5", Describe(m2));
1984 EXPECT_EQ("is not approximately 0.5", DescribeNegation(m2));
1985
1986 Matcher<double> m3 = DoubleEq(nan1_);
1987 EXPECT_EQ("never matches", Describe(m3));
1988 EXPECT_EQ("is anything", DescribeNegation(m3));
1989}
1990
1991TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {
1992 Matcher<double> m1 = NanSensitiveDoubleEq(2.0);
1993 EXPECT_EQ("is approximately 2", Describe(m1));
1994 EXPECT_EQ("is not approximately 2", DescribeNegation(m1));
1995
1996 Matcher<double> m2 = NanSensitiveDoubleEq(0.5);
1997 EXPECT_EQ("is approximately 0.5", Describe(m2));
1998 EXPECT_EQ("is not approximately 0.5", DescribeNegation(m2));
1999
2000 Matcher<double> m3 = NanSensitiveDoubleEq(nan1_);
2001 EXPECT_EQ("is NaN", Describe(m3));
2002 EXPECT_EQ("is not NaN", DescribeNegation(m3));
2003}
2004
2005TEST(PointeeTest, RawPointer) {
2006 const Matcher<int*> m = Pointee(Ge(0));
2007
2008 int n = 1;
2009 EXPECT_TRUE(m.Matches(&n));
2010 n = -1;
2011 EXPECT_FALSE(m.Matches(&n));
2012 EXPECT_FALSE(m.Matches(NULL));
2013}
2014
2015TEST(PointeeTest, RawPointerToConst) {
2016 const Matcher<const double*> m = Pointee(Ge(0));
2017
2018 double x = 1;
2019 EXPECT_TRUE(m.Matches(&x));
2020 x = -1;
2021 EXPECT_FALSE(m.Matches(&x));
2022 EXPECT_FALSE(m.Matches(NULL));
2023}
2024
2025TEST(PointeeTest, ReferenceToConstRawPointer) {
2026 const Matcher<int* const &> m = Pointee(Ge(0));
2027
2028 int n = 1;
2029 EXPECT_TRUE(m.Matches(&n));
2030 n = -1;
2031 EXPECT_FALSE(m.Matches(&n));
2032 EXPECT_FALSE(m.Matches(NULL));
2033}
2034
2035TEST(PointeeTest, ReferenceToNonConstRawPointer) {
2036 const Matcher<double* &> m = Pointee(Ge(0));
2037
2038 double x = 1.0;
2039 double* p = &x;
2040 EXPECT_TRUE(m.Matches(p));
2041 x = -1;
2042 EXPECT_FALSE(m.Matches(p));
2043 p = NULL;
2044 EXPECT_FALSE(m.Matches(p));
2045}
2046
2047TEST(PointeeTest, NeverMatchesNull) {
2048 const Matcher<const char*> m = Pointee(_);
2049 EXPECT_FALSE(m.Matches(NULL));
2050}
2051
2052// Tests that we can write Pointee(value) instead of Pointee(Eq(value)).
2053TEST(PointeeTest, MatchesAgainstAValue) {
2054 const Matcher<int*> m = Pointee(5);
2055
2056 int n = 5;
2057 EXPECT_TRUE(m.Matches(&n));
2058 n = -1;
2059 EXPECT_FALSE(m.Matches(&n));
2060 EXPECT_FALSE(m.Matches(NULL));
2061}
2062
2063TEST(PointeeTest, CanDescribeSelf) {
2064 const Matcher<int*> m = Pointee(Gt(3));
2065 EXPECT_EQ("points to a value that is greater than 3", Describe(m));
2066 EXPECT_EQ("does not point to a value that is greater than 3",
2067 DescribeNegation(m));
2068}
2069
2070// For testing ExplainMatchResultTo().
2071class GreaterThanMatcher : public MatcherInterface<int> {
2072 public:
2073 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
2074
2075 virtual bool Matches(int lhs) const { return lhs > rhs_; }
2076
2077 virtual void DescribeTo(::std::ostream* os) const {
2078 *os << "is greater than " << rhs_;
2079 }
2080
2081 virtual void ExplainMatchResultTo(int lhs, ::std::ostream* os) const {
2082 const int diff = lhs - rhs_;
2083 if (diff > 0) {
2084 *os << "is " << diff << " more than " << rhs_;
2085 } else if (diff == 0) {
2086 *os << "is the same as " << rhs_;
2087 } else {
2088 *os << "is " << -diff << " less than " << rhs_;
2089 }
2090 }
2091 private:
2092 const int rhs_;
2093};
2094
2095Matcher<int> GreaterThan(int n) {
2096 return MakeMatcher(new GreaterThanMatcher(n));
2097}
2098
2099TEST(PointeeTest, CanExplainMatchResult) {
2100 const Matcher<const string*> m = Pointee(StartsWith("Hi"));
2101
2102 EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL)));
2103
2104 const Matcher<int*> m2 = Pointee(GreaterThan(1));
2105 int n = 3;
2106 EXPECT_EQ("points to a value that is 2 more than 1", Explain(m2, &n));
2107}
2108
2109// An uncopyable class.
2110class Uncopyable {
2111 public:
2112 explicit Uncopyable(int value) : value_(value) {}
2113
2114 int value() const { return value_; }
2115 private:
2116 const int value_;
2117 GTEST_DISALLOW_COPY_AND_ASSIGN_(Uncopyable);
2118};
2119
2120// Returns true iff x.value() is positive.
2121bool ValueIsPositive(const Uncopyable& x) { return x.value() > 0; }
2122
2123// A user-defined struct for testing Field().
2124struct AStruct {
2125 AStruct() : x(0), y(1.0), z(5), p(NULL) {}
2126 AStruct(const AStruct& rhs)
2127 : x(rhs.x), y(rhs.y), z(rhs.z.value()), p(rhs.p) {}
2128
2129 int x; // A non-const field.
2130 const double y; // A const field.
2131 Uncopyable z; // An uncopyable field.
2132 const char* p; // A pointer field.
2133};
2134
2135// A derived struct for testing Field().
2136struct DerivedStruct : public AStruct {
2137 char ch;
2138};
2139
2140// Tests that Field(&Foo::field, ...) works when field is non-const.
2141TEST(FieldTest, WorksForNonConstField) {
2142 Matcher<AStruct> m = Field(&AStruct::x, Ge(0));
2143
2144 AStruct a;
2145 EXPECT_TRUE(m.Matches(a));
2146 a.x = -1;
2147 EXPECT_FALSE(m.Matches(a));
2148}
2149
2150// Tests that Field(&Foo::field, ...) works when field is const.
2151TEST(FieldTest, WorksForConstField) {
2152 AStruct a;
2153
2154 Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0));
2155 EXPECT_TRUE(m.Matches(a));
2156 m = Field(&AStruct::y, Le(0.0));
2157 EXPECT_FALSE(m.Matches(a));
2158}
2159
2160// Tests that Field(&Foo::field, ...) works when field is not copyable.
2161TEST(FieldTest, WorksForUncopyableField) {
2162 AStruct a;
2163
2164 Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive));
2165 EXPECT_TRUE(m.Matches(a));
2166 m = Field(&AStruct::z, Not(Truly(ValueIsPositive)));
2167 EXPECT_FALSE(m.Matches(a));
2168}
2169
2170// Tests that Field(&Foo::field, ...) works when field is a pointer.
2171TEST(FieldTest, WorksForPointerField) {
2172 // Matching against NULL.
2173 Matcher<AStruct> m = Field(&AStruct::p, static_cast<const char*>(NULL));
2174 AStruct a;
2175 EXPECT_TRUE(m.Matches(a));
2176 a.p = "hi";
2177 EXPECT_FALSE(m.Matches(a));
2178
2179 // Matching a pointer that is not NULL.
2180 m = Field(&AStruct::p, StartsWith("hi"));
2181 a.p = "hill";
2182 EXPECT_TRUE(m.Matches(a));
2183 a.p = "hole";
2184 EXPECT_FALSE(m.Matches(a));
2185}
2186
2187// Tests that Field() works when the object is passed by reference.
2188TEST(FieldTest, WorksForByRefArgument) {
2189 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2190
2191 AStruct a;
2192 EXPECT_TRUE(m.Matches(a));
2193 a.x = -1;
2194 EXPECT_FALSE(m.Matches(a));
2195}
2196
2197// Tests that Field(&Foo::field, ...) works when the argument's type
2198// is a sub-type of Foo.
2199TEST(FieldTest, WorksForArgumentOfSubType) {
2200 // Note that the matcher expects DerivedStruct but we say AStruct
2201 // inside Field().
2202 Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0));
2203
2204 DerivedStruct d;
2205 EXPECT_TRUE(m.Matches(d));
2206 d.x = -1;
2207 EXPECT_FALSE(m.Matches(d));
2208}
2209
2210// Tests that Field(&Foo::field, m) works when field's type and m's
2211// argument type are compatible but not the same.
2212TEST(FieldTest, WorksForCompatibleMatcherType) {
2213 // The field is an int, but the inner matcher expects a signed char.
2214 Matcher<const AStruct&> m = Field(&AStruct::x,
2215 Matcher<signed char>(Ge(0)));
2216
2217 AStruct a;
2218 EXPECT_TRUE(m.Matches(a));
2219 a.x = -1;
2220 EXPECT_FALSE(m.Matches(a));
2221}
2222
2223// Tests that Field() can describe itself.
2224TEST(FieldTest, CanDescribeSelf) {
2225 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2226
2227 EXPECT_EQ("the given field is greater than or equal to 0", Describe(m));
2228 EXPECT_EQ("the given field is not greater than or equal to 0",
2229 DescribeNegation(m));
2230}
2231
2232// Tests that Field() can explain the match result.
2233TEST(FieldTest, CanExplainMatchResult) {
2234 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2235
2236 AStruct a;
2237 a.x = 1;
2238 EXPECT_EQ("", Explain(m, a));
2239
2240 m = Field(&AStruct::x, GreaterThan(0));
2241 EXPECT_EQ("the given field is 1 more than 0", Explain(m, a));
2242}
2243
2244// Tests that Field() works when the argument is a pointer to const.
2245TEST(FieldForPointerTest, WorksForPointerToConst) {
2246 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2247
2248 AStruct a;
2249 EXPECT_TRUE(m.Matches(&a));
2250 a.x = -1;
2251 EXPECT_FALSE(m.Matches(&a));
2252}
2253
2254// Tests that Field() works when the argument is a pointer to non-const.
2255TEST(FieldForPointerTest, WorksForPointerToNonConst) {
2256 Matcher<AStruct*> m = Field(&AStruct::x, Ge(0));
2257
2258 AStruct a;
2259 EXPECT_TRUE(m.Matches(&a));
2260 a.x = -1;
2261 EXPECT_FALSE(m.Matches(&a));
2262}
2263
2264// Tests that Field() does not match the NULL pointer.
2265TEST(FieldForPointerTest, DoesNotMatchNull) {
2266 Matcher<const AStruct*> m = Field(&AStruct::x, _);
2267 EXPECT_FALSE(m.Matches(NULL));
2268}
2269
2270// Tests that Field(&Foo::field, ...) works when the argument's type
2271// is a sub-type of const Foo*.
2272TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
2273 // Note that the matcher expects DerivedStruct but we say AStruct
2274 // inside Field().
2275 Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0));
2276
2277 DerivedStruct d;
2278 EXPECT_TRUE(m.Matches(&d));
2279 d.x = -1;
2280 EXPECT_FALSE(m.Matches(&d));
2281}
2282
2283// Tests that Field() can describe itself when used to match a pointer.
2284TEST(FieldForPointerTest, CanDescribeSelf) {
2285 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2286
2287 EXPECT_EQ("the given field is greater than or equal to 0", Describe(m));
2288 EXPECT_EQ("the given field is not greater than or equal to 0",
2289 DescribeNegation(m));
2290}
2291
2292// Tests that Field() can explain the result of matching a pointer.
2293TEST(FieldForPointerTest, CanExplainMatchResult) {
2294 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2295
2296 AStruct a;
2297 a.x = 1;
2298 EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(NULL)));
2299 EXPECT_EQ("", Explain(m, &a));
2300
2301 m = Field(&AStruct::x, GreaterThan(0));
2302 EXPECT_EQ("the given field is 1 more than 0", Explain(m, &a));
2303}
2304
2305// A user-defined class for testing Property().
2306class AClass {
2307 public:
2308 AClass() : n_(0) {}
2309
2310 // A getter that returns a non-reference.
2311 int n() const { return n_; }
2312
2313 void set_n(int new_n) { n_ = new_n; }
2314
2315 // A getter that returns a reference to const.
2316 const string& s() const { return s_; }
2317
2318 void set_s(const string& new_s) { s_ = new_s; }
2319
2320 // A getter that returns a reference to non-const.
2321 double& x() const { return x_; }
2322 private:
2323 int n_;
2324 string s_;
2325
2326 static double x_;
2327};
2328
2329double AClass::x_ = 0.0;
2330
2331// A derived class for testing Property().
2332class DerivedClass : public AClass {
2333 private:
2334 int k_;
2335};
2336
2337// Tests that Property(&Foo::property, ...) works when property()
2338// returns a non-reference.
2339TEST(PropertyTest, WorksForNonReferenceProperty) {
2340 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
2341
2342 AClass a;
2343 a.set_n(1);
2344 EXPECT_TRUE(m.Matches(a));
2345
2346 a.set_n(-1);
2347 EXPECT_FALSE(m.Matches(a));
2348}
2349
2350// Tests that Property(&Foo::property, ...) works when property()
2351// returns a reference to const.
2352TEST(PropertyTest, WorksForReferenceToConstProperty) {
2353 Matcher<const AClass&> m = Property(&AClass::s, StartsWith("hi"));
2354
2355 AClass a;
2356 a.set_s("hill");
2357 EXPECT_TRUE(m.Matches(a));
2358
2359 a.set_s("hole");
2360 EXPECT_FALSE(m.Matches(a));
2361}
2362
2363// Tests that Property(&Foo::property, ...) works when property()
2364// returns a reference to non-const.
2365TEST(PropertyTest, WorksForReferenceToNonConstProperty) {
2366 double x = 0.0;
2367 AClass a;
2368
2369 Matcher<const AClass&> m = Property(&AClass::x, Ref(x));
2370 EXPECT_FALSE(m.Matches(a));
2371
2372 m = Property(&AClass::x, Not(Ref(x)));
2373 EXPECT_TRUE(m.Matches(a));
2374}
2375
2376// Tests that Property(&Foo::property, ...) works when the argument is
2377// passed by value.
2378TEST(PropertyTest, WorksForByValueArgument) {
2379 Matcher<AClass> m = Property(&AClass::s, StartsWith("hi"));
2380
2381 AClass a;
2382 a.set_s("hill");
2383 EXPECT_TRUE(m.Matches(a));
2384
2385 a.set_s("hole");
2386 EXPECT_FALSE(m.Matches(a));
2387}
2388
2389// Tests that Property(&Foo::property, ...) works when the argument's
2390// type is a sub-type of Foo.
2391TEST(PropertyTest, WorksForArgumentOfSubType) {
2392 // The matcher expects a DerivedClass, but inside the Property() we
2393 // say AClass.
2394 Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0));
2395
2396 DerivedClass d;
2397 d.set_n(1);
2398 EXPECT_TRUE(m.Matches(d));
2399
2400 d.set_n(-1);
2401 EXPECT_FALSE(m.Matches(d));
2402}
2403
2404// Tests that Property(&Foo::property, m) works when property()'s type
2405// and m's argument type are compatible but different.
2406TEST(PropertyTest, WorksForCompatibleMatcherType) {
2407 // n() returns an int but the inner matcher expects a signed char.
2408 Matcher<const AClass&> m = Property(&AClass::n,
2409 Matcher<signed char>(Ge(0)));
2410
2411 AClass a;
2412 EXPECT_TRUE(m.Matches(a));
2413 a.set_n(-1);
2414 EXPECT_FALSE(m.Matches(a));
2415}
2416
2417// Tests that Property() can describe itself.
2418TEST(PropertyTest, CanDescribeSelf) {
2419 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
2420
2421 EXPECT_EQ("the given property is greater than or equal to 0", Describe(m));
2422 EXPECT_EQ("the given property is not greater than or equal to 0",
2423 DescribeNegation(m));
2424}
2425
2426// Tests that Property() can explain the match result.
2427TEST(PropertyTest, CanExplainMatchResult) {
2428 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
2429
2430 AClass a;
2431 a.set_n(1);
2432 EXPECT_EQ("", Explain(m, a));
2433
2434 m = Property(&AClass::n, GreaterThan(0));
2435 EXPECT_EQ("the given property is 1 more than 0", Explain(m, a));
2436}
2437
2438// Tests that Property() works when the argument is a pointer to const.
2439TEST(PropertyForPointerTest, WorksForPointerToConst) {
2440 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
2441
2442 AClass a;
2443 a.set_n(1);
2444 EXPECT_TRUE(m.Matches(&a));
2445
2446 a.set_n(-1);
2447 EXPECT_FALSE(m.Matches(&a));
2448}
2449
2450// Tests that Property() works when the argument is a pointer to non-const.
2451TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
2452 Matcher<AClass*> m = Property(&AClass::s, StartsWith("hi"));
2453
2454 AClass a;
2455 a.set_s("hill");
2456 EXPECT_TRUE(m.Matches(&a));
2457
2458 a.set_s("hole");
2459 EXPECT_FALSE(m.Matches(&a));
2460}
2461
2462// Tests that Property() does not match the NULL pointer.
2463TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
2464 Matcher<const AClass*> m = Property(&AClass::x, _);
2465 EXPECT_FALSE(m.Matches(NULL));
2466}
2467
2468// Tests that Property(&Foo::property, ...) works when the argument's
2469// type is a sub-type of const Foo*.
2470TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
2471 // The matcher expects a DerivedClass, but inside the Property() we
2472 // say AClass.
2473 Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0));
2474
2475 DerivedClass d;
2476 d.set_n(1);
2477 EXPECT_TRUE(m.Matches(&d));
2478
2479 d.set_n(-1);
2480 EXPECT_FALSE(m.Matches(&d));
2481}
2482
2483// Tests that Property() can describe itself when used to match a pointer.
2484TEST(PropertyForPointerTest, CanDescribeSelf) {
2485 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
2486
2487 EXPECT_EQ("the given property is greater than or equal to 0", Describe(m));
2488 EXPECT_EQ("the given property is not greater than or equal to 0",
2489 DescribeNegation(m));
2490}
2491
2492// Tests that Property() can explain the result of matching a pointer.
2493TEST(PropertyForPointerTest, CanExplainMatchResult) {
2494 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
2495
2496 AClass a;
2497 a.set_n(1);
2498 EXPECT_EQ("", Explain(m, static_cast<const AClass*>(NULL)));
2499 EXPECT_EQ("", Explain(m, &a));
2500
2501 m = Property(&AClass::n, GreaterThan(0));
2502 EXPECT_EQ("the given property is 1 more than 0", Explain(m, &a));
2503}
2504
2505// Tests ResultOf.
2506
2507// Tests that ResultOf(f, ...) compiles and works as expected when f is a
2508// function pointer.
2509string IntToStringFunction(int input) { return input == 1 ? "foo" : "bar"; }
2510
2511TEST(ResultOfTest, WorksForFunctionPointers) {
2512 Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(string("foo")));
2513
2514 EXPECT_TRUE(matcher.Matches(1));
2515 EXPECT_FALSE(matcher.Matches(2));
2516}
2517
2518// Tests that ResultOf() can describe itself.
2519TEST(ResultOfTest, CanDescribeItself) {
2520 Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo"));
2521
2522 EXPECT_EQ("result of the given callable is equal to \"foo\"",
2523 Describe(matcher));
2524 EXPECT_EQ("result of the given callable is not equal to \"foo\"",
2525 DescribeNegation(matcher));
2526}
2527
2528// Tests that ResultOf() can explain the match result.
2529int IntFunction(int input) { return input == 42 ? 80 : 90; }
2530
2531TEST(ResultOfTest, CanExplainMatchResult) {
2532 Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
2533 EXPECT_EQ("", Explain(matcher, 36));
2534
2535 matcher = ResultOf(&IntFunction, GreaterThan(85));
2536 EXPECT_EQ("result of the given callable is 5 more than 85",
2537 Explain(matcher, 36));
2538}
2539
2540// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
2541// returns a non-reference.
2542TEST(ResultOfTest, WorksForNonReferenceResults) {
2543 Matcher<int> matcher = ResultOf(&IntFunction, Eq(80));
2544
2545 EXPECT_TRUE(matcher.Matches(42));
2546 EXPECT_FALSE(matcher.Matches(36));
2547}
2548
2549// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
2550// returns a reference to non-const.
2551double& DoubleFunction(double& input) { return input; }
2552
2553Uncopyable& RefUncopyableFunction(Uncopyable& obj) {
2554 return obj;
2555}
2556
2557TEST(ResultOfTest, WorksForReferenceToNonConstResults) {
2558 double x = 3.14;
2559 double x2 = x;
2560 Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x));
2561
2562 EXPECT_TRUE(matcher.Matches(x));
2563 EXPECT_FALSE(matcher.Matches(x2));
2564
2565 // Test that ResultOf works with uncopyable objects
2566 Uncopyable obj(0);
2567 Uncopyable obj2(0);
2568 Matcher<Uncopyable&> matcher2 =
2569 ResultOf(&RefUncopyableFunction, Ref(obj));
2570
2571 EXPECT_TRUE(matcher2.Matches(obj));
2572 EXPECT_FALSE(matcher2.Matches(obj2));
2573}
2574
2575// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
2576// returns a reference to const.
2577const string& StringFunction(const string& input) { return input; }
2578
2579TEST(ResultOfTest, WorksForReferenceToConstResults) {
2580 string s = "foo";
2581 string s2 = s;
2582 Matcher<const string&> matcher = ResultOf(&StringFunction, Ref(s));
2583
2584 EXPECT_TRUE(matcher.Matches(s));
2585 EXPECT_FALSE(matcher.Matches(s2));
2586}
2587
2588// Tests that ResultOf(f, m) works when f(x) and m's
2589// argument types are compatible but different.
2590TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {
2591 // IntFunction() returns int but the inner matcher expects a signed char.
2592 Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85)));
2593
2594 EXPECT_TRUE(matcher.Matches(36));
2595 EXPECT_FALSE(matcher.Matches(42));
2596}
2597
zhanyong.wan652540a2009-02-23 23:37:29 +00002598#if GTEST_HAS_DEATH_TEST
shiqiane35fdd92008-12-10 05:08:54 +00002599// Tests that the program aborts when ResultOf is passed
2600// a NULL function pointer.
2601TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {
2602 EXPECT_DEATH(
2603 ResultOf(static_cast<string(*)(int)>(NULL), Eq(string("foo"))),
2604 "NULL function pointer is passed into ResultOf\\(\\)\\.");
2605}
2606#endif // GTEST_HAS_DEATH_TEST
2607
2608// Tests that ResultOf(f, ...) compiles and works as expected when f is a
2609// function reference.
2610TEST(ResultOfTest, WorksForFunctionReferences) {
2611 Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq("foo"));
2612 EXPECT_TRUE(matcher.Matches(1));
2613 EXPECT_FALSE(matcher.Matches(2));
2614}
2615
2616// Tests that ResultOf(f, ...) compiles and works as expected when f is a
2617// function object.
2618struct Functor : public ::std::unary_function<int, string> {
2619 result_type operator()(argument_type input) const {
2620 return IntToStringFunction(input);
2621 }
2622};
2623
2624TEST(ResultOfTest, WorksForFunctors) {
2625 Matcher<int> matcher = ResultOf(Functor(), Eq(string("foo")));
2626
2627 EXPECT_TRUE(matcher.Matches(1));
2628 EXPECT_FALSE(matcher.Matches(2));
2629}
2630
2631// Tests that ResultOf(f, ...) compiles and works as expected when f is a
2632// functor with more then one operator() defined. ResultOf() must work
2633// for each defined operator().
2634struct PolymorphicFunctor {
2635 typedef int result_type;
2636 int operator()(int n) { return n; }
2637 int operator()(const char* s) { return static_cast<int>(strlen(s)); }
2638};
2639
2640TEST(ResultOfTest, WorksForPolymorphicFunctors) {
2641 Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5));
2642
2643 EXPECT_TRUE(matcher_int.Matches(10));
2644 EXPECT_FALSE(matcher_int.Matches(2));
2645
2646 Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5));
2647
2648 EXPECT_TRUE(matcher_string.Matches("long string"));
2649 EXPECT_FALSE(matcher_string.Matches("shrt"));
2650}
2651
2652const int* ReferencingFunction(const int& n) { return &n; }
2653
2654struct ReferencingFunctor {
2655 typedef const int* result_type;
2656 result_type operator()(const int& n) { return &n; }
2657};
2658
2659TEST(ResultOfTest, WorksForReferencingCallables) {
2660 const int n = 1;
2661 const int n2 = 1;
2662 Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n));
2663 EXPECT_TRUE(matcher2.Matches(n));
2664 EXPECT_FALSE(matcher2.Matches(n2));
2665
2666 Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n));
2667 EXPECT_TRUE(matcher3.Matches(n));
2668 EXPECT_FALSE(matcher3.Matches(n2));
2669}
2670
2671
2672class DivisibleByImpl {
2673 public:
2674 explicit DivisibleByImpl(int divider) : divider_(divider) {}
2675
2676 template <typename T>
2677 bool Matches(const T& n) const {
2678 return (n % divider_) == 0;
2679 }
2680
2681 void DescribeTo(::std::ostream* os) const {
2682 *os << "is divisible by " << divider_;
2683 }
2684
2685 void DescribeNegationTo(::std::ostream* os) const {
2686 *os << "is not divisible by " << divider_;
2687 }
2688
2689 int divider() const { return divider_; }
2690 private:
2691 const int divider_;
2692};
2693
2694// For testing using ExplainMatchResultTo() with polymorphic matchers.
2695template <typename T>
2696void ExplainMatchResultTo(const DivisibleByImpl& impl, const T& n,
2697 ::std::ostream* os) {
2698 *os << "is " << (n % impl.divider()) << " modulo "
2699 << impl.divider();
2700}
2701
2702PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) {
2703 return MakePolymorphicMatcher(DivisibleByImpl(n));
2704}
2705
2706// Tests that when AllOf() fails, only the first failing matcher is
2707// asked to explain why.
2708TEST(ExplainMatchResultTest, AllOf_False_False) {
2709 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
2710 EXPECT_EQ("is 1 modulo 4", Explain(m, 5));
2711}
2712
2713// Tests that when AllOf() fails, only the first failing matcher is
2714// asked to explain why.
2715TEST(ExplainMatchResultTest, AllOf_False_True) {
2716 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
2717 EXPECT_EQ("is 2 modulo 4", Explain(m, 6));
2718}
2719
2720// Tests that when AllOf() fails, only the first failing matcher is
2721// asked to explain why.
2722TEST(ExplainMatchResultTest, AllOf_True_False) {
2723 const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));
2724 EXPECT_EQ("is 2 modulo 3", Explain(m, 5));
2725}
2726
2727// Tests that when AllOf() succeeds, all matchers are asked to explain
2728// why.
2729TEST(ExplainMatchResultTest, AllOf_True_True) {
2730 const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3));
2731 EXPECT_EQ("is 0 modulo 2; is 0 modulo 3", Explain(m, 6));
2732}
2733
2734TEST(ExplainMatchResultTest, AllOf_True_True_2) {
2735 const Matcher<int> m = AllOf(Ge(2), Le(3));
2736 EXPECT_EQ("", Explain(m, 2));
2737}
2738
2739TEST(ExplainmatcherResultTest, MonomorphicMatcher) {
2740 const Matcher<int> m = GreaterThan(5);
2741 EXPECT_EQ("is 1 more than 5", Explain(m, 6));
2742}
2743
2744// The following two tests verify that values without a public copy
2745// ctor can be used as arguments to matchers like Eq(), Ge(), and etc
2746// with the help of ByRef().
2747
2748class NotCopyable {
2749 public:
2750 explicit NotCopyable(int value) : value_(value) {}
2751
2752 int value() const { return value_; }
2753
2754 bool operator==(const NotCopyable& rhs) const {
2755 return value() == rhs.value();
2756 }
2757
2758 bool operator>=(const NotCopyable& rhs) const {
2759 return value() >= rhs.value();
2760 }
2761 private:
2762 int value_;
2763
2764 GTEST_DISALLOW_COPY_AND_ASSIGN_(NotCopyable);
2765};
2766
2767TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) {
2768 const NotCopyable const_value1(1);
2769 const Matcher<const NotCopyable&> m = Eq(ByRef(const_value1));
2770
2771 const NotCopyable n1(1), n2(2);
2772 EXPECT_TRUE(m.Matches(n1));
2773 EXPECT_FALSE(m.Matches(n2));
2774}
2775
2776TEST(ByRefTest, AllowsNotCopyableValueInMatchers) {
2777 NotCopyable value2(2);
2778 const Matcher<NotCopyable&> m = Ge(ByRef(value2));
2779
2780 NotCopyable n1(1), n2(2);
2781 EXPECT_FALSE(m.Matches(n1));
2782 EXPECT_TRUE(m.Matches(n2));
2783}
2784
zhanyong.wan6a896b52009-01-16 01:13:50 +00002785// Tests ContainerEq with different container types, and
2786// different element types.
2787
2788template <typename T>
zhanyong.wanb8243162009-06-04 05:48:20 +00002789class ContainerEqTest : public testing::Test {};
zhanyong.wan6a896b52009-01-16 01:13:50 +00002790
2791typedef testing::Types<
2792 std::set<int>,
2793 std::vector<size_t>,
2794 std::multiset<size_t>,
2795 std::list<int> >
2796 ContainerEqTestTypes;
2797
2798TYPED_TEST_CASE(ContainerEqTest, ContainerEqTestTypes);
2799
2800// Tests that the filled container is equal to itself.
2801TYPED_TEST(ContainerEqTest, EqualsSelf) {
2802 static const int vals[] = {1, 1, 2, 3, 5, 8};
2803 TypeParam my_set(vals, vals + 6);
2804 const Matcher<TypeParam> m = ContainerEq(my_set);
2805 EXPECT_TRUE(m.Matches(my_set));
2806 EXPECT_EQ("", Explain(m, my_set));
2807}
2808
2809// Tests that missing values are reported.
2810TYPED_TEST(ContainerEqTest, ValueMissing) {
2811 static const int vals[] = {1, 1, 2, 3, 5, 8};
2812 static const int test_vals[] = {2, 1, 8, 5};
2813 TypeParam my_set(vals, vals + 6);
2814 TypeParam test_set(test_vals, test_vals + 4);
2815 const Matcher<TypeParam> m = ContainerEq(my_set);
2816 EXPECT_FALSE(m.Matches(test_set));
2817 EXPECT_EQ("Not in actual: 3", Explain(m, test_set));
2818}
2819
2820// Tests that added values are reported.
2821TYPED_TEST(ContainerEqTest, ValueAdded) {
2822 static const int vals[] = {1, 1, 2, 3, 5, 8};
2823 static const int test_vals[] = {1, 2, 3, 5, 8, 46};
2824 TypeParam my_set(vals, vals + 6);
2825 TypeParam test_set(test_vals, test_vals + 6);
2826 const Matcher<const TypeParam&> m = ContainerEq(my_set);
2827 EXPECT_FALSE(m.Matches(test_set));
2828 EXPECT_EQ("Only in actual: 46", Explain(m, test_set));
2829}
2830
2831// Tests that added and missing values are reported together.
2832TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) {
2833 static const int vals[] = {1, 1, 2, 3, 5, 8};
2834 static const int test_vals[] = {1, 2, 3, 8, 46};
2835 TypeParam my_set(vals, vals + 6);
2836 TypeParam test_set(test_vals, test_vals + 5);
2837 const Matcher<TypeParam> m = ContainerEq(my_set);
2838 EXPECT_FALSE(m.Matches(test_set));
2839 EXPECT_EQ("Only in actual: 46; not in actual: 5", Explain(m, test_set));
2840}
2841
2842// Tests duplicated value -- expect no explanation.
2843TYPED_TEST(ContainerEqTest, DuplicateDifference) {
2844 static const int vals[] = {1, 1, 2, 3, 5, 8};
2845 static const int test_vals[] = {1, 2, 3, 5, 8};
2846 TypeParam my_set(vals, vals + 6);
2847 TypeParam test_set(test_vals, test_vals + 5);
2848 const Matcher<const TypeParam&> m = ContainerEq(my_set);
2849 // Depending on the container, match may be true or false
2850 // But in any case there should be no explanation.
2851 EXPECT_EQ("", Explain(m, test_set));
2852}
2853
2854// Tests that mutliple missing values are reported.
2855// Using just vector here, so order is predicatble.
2856TEST(ContainerEqExtraTest, MultipleValuesMissing) {
2857 static const int vals[] = {1, 1, 2, 3, 5, 8};
2858 static const int test_vals[] = {2, 1, 5};
2859 std::vector<int> my_set(vals, vals + 6);
2860 std::vector<int> test_set(test_vals, test_vals + 3);
2861 const Matcher<std::vector<int> > m = ContainerEq(my_set);
2862 EXPECT_FALSE(m.Matches(test_set));
2863 EXPECT_EQ("Not in actual: 3, 8", Explain(m, test_set));
2864}
2865
2866// Tests that added values are reported.
2867// Using just vector here, so order is predicatble.
2868TEST(ContainerEqExtraTest, MultipleValuesAdded) {
2869 static const int vals[] = {1, 1, 2, 3, 5, 8};
2870 static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46};
2871 std::list<size_t> my_set(vals, vals + 6);
2872 std::list<size_t> test_set(test_vals, test_vals + 7);
2873 const Matcher<const std::list<size_t>&> m = ContainerEq(my_set);
2874 EXPECT_FALSE(m.Matches(test_set));
2875 EXPECT_EQ("Only in actual: 92, 46", Explain(m, test_set));
2876}
2877
2878// Tests that added and missing values are reported together.
2879TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) {
2880 static const int vals[] = {1, 1, 2, 3, 5, 8};
2881 static const int test_vals[] = {1, 2, 3, 92, 46};
2882 std::list<size_t> my_set(vals, vals + 6);
2883 std::list<size_t> test_set(test_vals, test_vals + 5);
2884 const Matcher<const std::list<size_t> > m = ContainerEq(my_set);
2885 EXPECT_FALSE(m.Matches(test_set));
2886 EXPECT_EQ("Only in actual: 92, 46; not in actual: 5, 8",
2887 Explain(m, test_set));
2888}
2889
2890// Tests to see that duplicate elements are detected,
2891// but (as above) not reported in the explanation.
2892TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) {
2893 static const int vals[] = {1, 1, 2, 3, 5, 8};
2894 static const int test_vals[] = {1, 2, 3, 5, 8};
2895 std::vector<int> my_set(vals, vals + 6);
2896 std::vector<int> test_set(test_vals, test_vals + 5);
2897 const Matcher<std::vector<int> > m = ContainerEq(my_set);
2898 EXPECT_TRUE(m.Matches(my_set));
2899 EXPECT_FALSE(m.Matches(test_set));
2900 // There is nothing to report when both sets contain all the same values.
2901 EXPECT_EQ("", Explain(m, test_set));
2902}
2903
2904// Tests that ContainerEq works for non-trivial associative containers,
2905// like maps.
2906TEST(ContainerEqExtraTest, WorksForMaps) {
2907 std::map<int, std::string> my_map;
2908 my_map[0] = "a";
2909 my_map[1] = "b";
2910
2911 std::map<int, std::string> test_map;
2912 test_map[0] = "aa";
2913 test_map[1] = "b";
2914
2915 const Matcher<const std::map<int, std::string>&> m = ContainerEq(my_map);
2916 EXPECT_TRUE(m.Matches(my_map));
2917 EXPECT_FALSE(m.Matches(test_map));
2918
2919 EXPECT_EQ("Only in actual: (0, \"aa\"); not in actual: (0, \"a\")",
2920 Explain(m, test_map));
2921}
2922
zhanyong.wanb8243162009-06-04 05:48:20 +00002923TEST(ContainerEqExtraTest, WorksForNativeArray) {
2924 int a1[] = { 1, 2, 3 };
2925 int a2[] = { 1, 2, 3 };
2926 int b[] = { 1, 2, 4 };
2927
2928 EXPECT_THAT(a1, ContainerEq(a2));
2929 EXPECT_THAT(a1, Not(ContainerEq(b)));
2930}
2931
2932TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) {
2933 const char a1[][3] = { "hi", "lo" };
2934 const char a2[][3] = { "hi", "lo" };
2935 const char b[][3] = { "lo", "hi" };
2936
2937 // Tests using ContainerEq() in the first dimension.
2938 EXPECT_THAT(a1, ContainerEq(a2));
2939 EXPECT_THAT(a1, Not(ContainerEq(b)));
2940
2941 // Tests using ContainerEq() in the second dimension.
2942 EXPECT_THAT(a1, ElementsAre(ContainerEq(a2[0]), ContainerEq(a2[1])));
2943 EXPECT_THAT(a1, ElementsAre(Not(ContainerEq(b[0])), ContainerEq(a2[1])));
2944}
2945
2946TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) {
2947 const int a1[] = { 1, 2, 3 };
2948 const int a2[] = { 1, 2, 3 };
2949 const int b[] = { 1, 2, 3, 4 };
2950
2951 EXPECT_THAT(make_tuple(a1, 3), ContainerEq(a2));
2952 EXPECT_THAT(make_tuple(a1, 3), Not(ContainerEq(b)));
2953
2954 const int c[] = { 1, 3, 2 };
2955 EXPECT_THAT(make_tuple(a1, 3), Not(ContainerEq(c)));
2956}
2957
2958TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) {
2959 std::string a1[][3] = {
2960 { "hi", "hello", "ciao" },
2961 { "bye", "see you", "ciao" }
2962 };
2963
2964 std::string a2[][3] = {
2965 { "hi", "hello", "ciao" },
2966 { "bye", "see you", "ciao" }
2967 };
2968
2969 const Matcher<const std::string(&)[2][3]> m = ContainerEq(a2);
2970 EXPECT_THAT(a1, m);
2971
2972 a2[0][0] = "ha";
2973 EXPECT_THAT(a1, m);
2974}
2975
zhanyong.wan4a5330d2009-02-19 00:36:44 +00002976// Tests GetParamIndex().
2977
2978TEST(GetParamIndexTest, WorksForEmptyParamList) {
2979 const char* params[] = { NULL };
2980 EXPECT_EQ(kTupleInterpolation, GetParamIndex(params, "*"));
2981 EXPECT_EQ(kInvalidInterpolation, GetParamIndex(params, "a"));
2982}
2983
2984TEST(GetParamIndexTest, RecognizesStar) {
2985 const char* params[] = { "a", "b", NULL };
2986 EXPECT_EQ(kTupleInterpolation, GetParamIndex(params, "*"));
2987}
2988
2989TEST(GetParamIndexTest, RecognizesKnownParam) {
2990 const char* params[] = { "foo", "bar", NULL };
2991 EXPECT_EQ(0, GetParamIndex(params, "foo"));
2992 EXPECT_EQ(1, GetParamIndex(params, "bar"));
2993}
2994
2995TEST(GetParamIndexTest, RejectsUnknownParam) {
2996 const char* params[] = { "foo", "bar", NULL };
2997 EXPECT_EQ(kInvalidInterpolation, GetParamIndex(params, "foobar"));
2998}
2999
3000// Tests SkipPrefix().
3001
3002TEST(SkipPrefixTest, SkipsWhenPrefixMatches) {
3003 const char* const str = "hello";
3004
3005 const char* p = str;
3006 EXPECT_TRUE(SkipPrefix("", &p));
3007 EXPECT_EQ(str, p);
3008
3009 p = str;
3010 EXPECT_TRUE(SkipPrefix("hell", &p));
3011 EXPECT_EQ(str + 4, p);
3012}
3013
3014TEST(SkipPrefixTest, DoesNotSkipWhenPrefixDoesNotMatch) {
3015 const char* const str = "world";
3016
3017 const char* p = str;
3018 EXPECT_FALSE(SkipPrefix("W", &p));
3019 EXPECT_EQ(str, p);
3020
3021 p = str;
3022 EXPECT_FALSE(SkipPrefix("world!", &p));
3023 EXPECT_EQ(str, p);
3024}
3025
3026// Tests FormatMatcherDescriptionSyntaxError().
3027TEST(FormatMatcherDescriptionSyntaxErrorTest, FormatsCorrectly) {
3028 const char* const description = "hello%world";
3029 EXPECT_EQ("Syntax error at index 5 in matcher description \"hello%world\": ",
3030 FormatMatcherDescriptionSyntaxError(description, description + 5));
3031}
3032
3033// Tests ValidateMatcherDescription().
3034
3035TEST(ValidateMatcherDescriptionTest, AcceptsEmptyDescription) {
3036 const char* params[] = { "foo", "bar", NULL };
3037 EXPECT_THAT(ValidateMatcherDescription(params, ""),
3038 ElementsAre());
3039}
3040
3041TEST(ValidateMatcherDescriptionTest,
3042 AcceptsNonEmptyDescriptionWithNoInterpolation) {
3043 const char* params[] = { "foo", "bar", NULL };
3044 EXPECT_THAT(ValidateMatcherDescription(params, "a simple description"),
3045 ElementsAre());
3046}
3047
3048// We use MATCHER_P3() to define a matcher for testing
3049// ValidateMatcherDescription(); otherwise we'll end up with much
3050// plumbing code. This is not circular as
3051// ValidateMatcherDescription() doesn't affect whether the matcher
3052// matches a value or not.
3053MATCHER_P3(EqInterpolation, start, end, index, "equals Interpolation%(*)s") {
3054 return arg.start_pos == start && arg.end_pos == end &&
3055 arg.param_index == index;
3056}
3057
3058TEST(ValidateMatcherDescriptionTest, AcceptsPercentInterpolation) {
3059 const char* params[] = { "foo", NULL };
3060 const char* const desc = "one %%";
3061 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3062 ElementsAre(EqInterpolation(desc + 4, desc + 6,
3063 kPercentInterpolation)));
3064}
3065
3066TEST(ValidateMatcherDescriptionTest, AcceptsTupleInterpolation) {
3067 const char* params[] = { "foo", "bar", "baz", NULL };
3068 const char* const desc = "%(*)s after";
3069 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3070 ElementsAre(EqInterpolation(desc, desc + 5,
3071 kTupleInterpolation)));
3072}
3073
3074TEST(ValidateMatcherDescriptionTest, AcceptsParamInterpolation) {
3075 const char* params[] = { "foo", "bar", "baz", NULL };
3076 const char* const desc = "a %(bar)s.";
3077 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3078 ElementsAre(EqInterpolation(desc + 2, desc + 9, 1)));
3079}
3080
3081TEST(ValidateMatcherDescriptionTest, AcceptsMultiplenterpolations) {
3082 const char* params[] = { "foo", "bar", "baz", NULL };
3083 const char* const desc = "%(baz)s %(foo)s %(bar)s";
3084 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3085 ElementsAre(EqInterpolation(desc, desc + 7, 2),
3086 EqInterpolation(desc + 8, desc + 15, 0),
3087 EqInterpolation(desc + 16, desc + 23, 1)));
3088}
3089
3090TEST(ValidateMatcherDescriptionTest, AcceptsRepeatedParams) {
3091 const char* params[] = { "foo", "bar", NULL };
3092 const char* const desc = "%(foo)s and %(foo)s";
3093 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3094 ElementsAre(EqInterpolation(desc, desc + 7, 0),
3095 EqInterpolation(desc + 12, desc + 19, 0)));
3096}
3097
3098TEST(ValidateMatcherDescriptionTest, RejectsUnknownParam) {
3099 const char* params[] = { "a", "bar", NULL };
3100 EXPECT_NONFATAL_FAILURE({
3101 EXPECT_THAT(ValidateMatcherDescription(params, "%(foo)s"),
3102 ElementsAre());
3103 }, "Syntax error at index 2 in matcher description \"%(foo)s\": "
3104 "\"foo\" is an invalid parameter name.");
3105}
3106
3107TEST(ValidateMatcherDescriptionTest, RejectsUnfinishedParam) {
3108 const char* params[] = { "a", "bar", NULL };
3109 EXPECT_NONFATAL_FAILURE({
3110 EXPECT_THAT(ValidateMatcherDescription(params, "%(foo)"),
3111 ElementsAre());
3112 }, "Syntax error at index 0 in matcher description \"%(foo)\": "
3113 "an interpolation must end with \")s\", but \"%(foo)\" does not.");
3114
3115 EXPECT_NONFATAL_FAILURE({
3116 EXPECT_THAT(ValidateMatcherDescription(params, "x%(a"),
3117 ElementsAre());
3118 }, "Syntax error at index 1 in matcher description \"x%(a\": "
3119 "an interpolation must end with \")s\", but \"%(a\" does not.");
3120}
3121
3122TEST(ValidateMatcherDescriptionTest, RejectsSinglePercent) {
3123 const char* params[] = { "a", NULL };
3124 EXPECT_NONFATAL_FAILURE({
3125 EXPECT_THAT(ValidateMatcherDescription(params, "a %."),
3126 ElementsAre());
3127 }, "Syntax error at index 2 in matcher description \"a %.\": "
3128 "use \"%%\" instead of \"%\" to print \"%\".");
3129
3130}
3131
3132// Tests JoinAsTuple().
3133
3134TEST(JoinAsTupleTest, JoinsEmptyTuple) {
3135 EXPECT_EQ("", JoinAsTuple(Strings()));
3136}
3137
3138TEST(JoinAsTupleTest, JoinsOneTuple) {
3139 const char* fields[] = { "1" };
3140 EXPECT_EQ("1", JoinAsTuple(Strings(fields, fields + 1)));
3141}
3142
3143TEST(JoinAsTupleTest, JoinsTwoTuple) {
3144 const char* fields[] = { "1", "a" };
3145 EXPECT_EQ("(1, a)", JoinAsTuple(Strings(fields, fields + 2)));
3146}
3147
3148TEST(JoinAsTupleTest, JoinsTenTuple) {
3149 const char* fields[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
3150 EXPECT_EQ("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)",
3151 JoinAsTuple(Strings(fields, fields + 10)));
3152}
3153
3154// Tests FormatMatcherDescription().
3155
3156TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) {
3157 EXPECT_EQ("is even",
3158 FormatMatcherDescription("IsEven", "", Interpolations(),
3159 Strings()));
3160
3161 const char* params[] = { "5" };
3162 EXPECT_EQ("equals 5",
3163 FormatMatcherDescription("Equals", "", Interpolations(),
3164 Strings(params, params + 1)));
3165
3166 const char* params2[] = { "5", "8" };
3167 EXPECT_EQ("is in range (5, 8)",
3168 FormatMatcherDescription("IsInRange", "", Interpolations(),
3169 Strings(params2, params2 + 2)));
3170}
3171
3172TEST(FormatMatcherDescriptionTest, WorksForDescriptionWithNoInterpolation) {
3173 EXPECT_EQ("is positive",
3174 FormatMatcherDescription("Gt0", "is positive", Interpolations(),
3175 Strings()));
3176
3177 const char* params[] = { "5", "6" };
3178 EXPECT_EQ("is negative",
3179 FormatMatcherDescription("Lt0", "is negative", Interpolations(),
3180 Strings(params, params + 2)));
3181}
3182
3183TEST(FormatMatcherDescriptionTest,
3184 WorksWhenDescriptionStartsWithInterpolation) {
3185 const char* params[] = { "5" };
3186 const char* const desc = "%(num)s times bigger";
3187 const Interpolation interp[] = { Interpolation(desc, desc + 7, 0) };
3188 EXPECT_EQ("5 times bigger",
3189 FormatMatcherDescription("Foo", desc,
3190 Interpolations(interp, interp + 1),
3191 Strings(params, params + 1)));
3192}
3193
3194TEST(FormatMatcherDescriptionTest,
3195 WorksWhenDescriptionEndsWithInterpolation) {
3196 const char* params[] = { "5", "6" };
3197 const char* const desc = "is bigger than %(y)s";
3198 const Interpolation interp[] = { Interpolation(desc + 15, desc + 20, 1) };
3199 EXPECT_EQ("is bigger than 6",
3200 FormatMatcherDescription("Foo", desc,
3201 Interpolations(interp, interp + 1),
3202 Strings(params, params + 2)));
3203}
3204
3205TEST(FormatMatcherDescriptionTest,
3206 WorksWhenDescriptionStartsAndEndsWithInterpolation) {
3207 const char* params[] = { "5", "6" };
3208 const char* const desc = "%(x)s <= arg <= %(y)s";
3209 const Interpolation interp[] = {
3210 Interpolation(desc, desc + 5, 0),
3211 Interpolation(desc + 16, desc + 21, 1)
3212 };
3213 EXPECT_EQ("5 <= arg <= 6",
3214 FormatMatcherDescription("Foo", desc,
3215 Interpolations(interp, interp + 2),
3216 Strings(params, params + 2)));
3217}
3218
3219TEST(FormatMatcherDescriptionTest,
3220 WorksWhenDescriptionDoesNotStartOrEndWithInterpolation) {
3221 const char* params[] = { "5.2" };
3222 const char* const desc = "has %(x)s cents";
3223 const Interpolation interp[] = { Interpolation(desc + 4, desc + 9, 0) };
3224 EXPECT_EQ("has 5.2 cents",
3225 FormatMatcherDescription("Foo", desc,
3226 Interpolations(interp, interp + 1),
3227 Strings(params, params + 1)));
3228}
3229
3230TEST(FormatMatcherDescriptionTest,
3231 WorksWhenDescriptionContainsMultipleInterpolations) {
3232 const char* params[] = { "5", "6" };
3233 const char* const desc = "in %(*)s or [%(x)s, %(y)s]";
3234 const Interpolation interp[] = {
3235 Interpolation(desc + 3, desc + 8, kTupleInterpolation),
3236 Interpolation(desc + 13, desc + 18, 0),
3237 Interpolation(desc + 20, desc + 25, 1)
3238 };
3239 EXPECT_EQ("in (5, 6) or [5, 6]",
3240 FormatMatcherDescription("Foo", desc,
3241 Interpolations(interp, interp + 3),
3242 Strings(params, params + 2)));
3243}
3244
3245TEST(FormatMatcherDescriptionTest,
3246 WorksWhenDescriptionContainsRepeatedParams) {
3247 const char* params[] = { "9" };
3248 const char* const desc = "in [-%(x)s, %(x)s]";
3249 const Interpolation interp[] = {
3250 Interpolation(desc + 5, desc + 10, 0),
3251 Interpolation(desc + 12, desc + 17, 0)
3252 };
3253 EXPECT_EQ("in [-9, 9]",
3254 FormatMatcherDescription("Foo", desc,
3255 Interpolations(interp, interp + 2),
3256 Strings(params, params + 1)));
3257}
3258
3259TEST(FormatMatcherDescriptionTest,
3260 WorksForDescriptionWithInvalidInterpolation) {
3261 const char* params[] = { "9" };
3262 const char* const desc = "> %(x)s %(x)";
3263 const Interpolation interp[] = { Interpolation(desc + 2, desc + 7, 0) };
3264 EXPECT_EQ("> 9 %(x)",
3265 FormatMatcherDescription("Foo", desc,
3266 Interpolations(interp, interp + 1),
3267 Strings(params, params + 1)));
3268}
3269
shiqiane35fdd92008-12-10 05:08:54 +00003270} // namespace gmock_matchers_test
3271} // namespace testing