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