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