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