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