blob: 5557983c732c4a31720b2b954f591f621eec4f5e [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.wan676e8cc2010-03-16 20:01:51 +0000151 *listener << "which is " << diff << " more than " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000152 } else if (diff == 0) {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000153 *listener << "which is the same as " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000154 } else {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000155 *listener << "which 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));
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000323 EXPECT_EQ("which is 42 more than 0", listener1.str());
zhanyong.wan82113312010-01-08 21:55:40 +0000324
zhanyong.wan34b034c2010-03-05 21:23:23 +0000325 StringMatchResultListener listener2;
zhanyong.wan82113312010-01-08 21:55:40 +0000326 EXPECT_FALSE(m.MatchAndExplain(-9, &listener2));
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000327 EXPECT_EQ("which is 9 less than 0", listener2.str());
zhanyong.wan82113312010-01-08 21:55:40 +0000328}
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));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001183 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
zhanyong.wan82113312010-01-08 21:55:40 +00001184 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.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001188 EXPECT_EQ("whose second field does not match, which is 2 less than 0",
zhanyong.wan82113312010-01-08 21:55:40 +00001189 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.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001193 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
zhanyong.wan82113312010-01-08 21:55:40 +00001194 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.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001197 EXPECT_EQ("whose both fields match, where the first field is a value "
1198 "which is 1 more than 0, and the second field is a value "
1199 "which is 2 more than 0",
zhanyong.wan82113312010-01-08 21:55:40 +00001200 Explain(m, std::make_pair(1, 2)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001201
1202 // If only the first match has an explanation, only this explanation should
1203 // be printed.
1204 const Matcher<std::pair<int, int> > explain_first = Pair(GreaterThan(0), 0);
1205 EXPECT_EQ("whose both fields match, where the first field is a value "
1206 "which is 1 more than 0",
1207 Explain(explain_first, std::make_pair(1, 0)));
1208
1209 // If only the second match has an explanation, only this explanation should
1210 // be printed.
1211 const Matcher<std::pair<int, int> > explain_second = Pair(0, GreaterThan(0));
1212 EXPECT_EQ("whose both fields match, where the second field is a value "
1213 "which is 1 more than 0",
1214 Explain(explain_second, std::make_pair(0, 1)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001215}
1216
1217TEST(PairTest, MatchesCorrectly) {
1218 std::pair<int, std::string> p(25, "foo");
1219
1220 // Both fields match.
1221 EXPECT_THAT(p, Pair(25, "foo"));
1222 EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o")));
1223
1224 // 'first' doesnt' match, but 'second' matches.
1225 EXPECT_THAT(p, Not(Pair(42, "foo")));
1226 EXPECT_THAT(p, Not(Pair(Lt(25), "foo")));
1227
1228 // 'first' matches, but 'second' doesn't match.
1229 EXPECT_THAT(p, Not(Pair(25, "bar")));
1230 EXPECT_THAT(p, Not(Pair(25, Not("foo"))));
1231
1232 // Neither field matches.
1233 EXPECT_THAT(p, Not(Pair(13, "bar")));
1234 EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a"))));
1235}
1236
1237TEST(PairTest, SafelyCastsInnerMatchers) {
1238 Matcher<int> is_positive = Gt(0);
1239 Matcher<int> is_negative = Lt(0);
1240 std::pair<char, bool> p('a', true);
1241 EXPECT_THAT(p, Pair(is_positive, _));
1242 EXPECT_THAT(p, Not(Pair(is_negative, _)));
1243 EXPECT_THAT(p, Pair(_, is_positive));
1244 EXPECT_THAT(p, Not(Pair(_, is_negative)));
1245}
1246
1247TEST(PairTest, InsideContainsUsingMap) {
zhanyong.wan95b12332009-09-25 18:55:50 +00001248 std::map<int, char> container;
1249 container.insert(std::make_pair(1, 'a'));
1250 container.insert(std::make_pair(2, 'b'));
1251 container.insert(std::make_pair(4, 'c'));
1252 EXPECT_THAT(container, Contains(Pair(1, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001253 EXPECT_THAT(container, Contains(Pair(1, _)));
zhanyong.wan95b12332009-09-25 18:55:50 +00001254 EXPECT_THAT(container, Contains(Pair(_, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001255 EXPECT_THAT(container, Not(Contains(Pair(3, _))));
1256}
1257
shiqiane35fdd92008-12-10 05:08:54 +00001258// Tests StartsWith(s).
1259
1260TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
1261 const Matcher<const char*> m1 = StartsWith(string(""));
1262 EXPECT_TRUE(m1.Matches("Hi"));
1263 EXPECT_TRUE(m1.Matches(""));
1264 EXPECT_FALSE(m1.Matches(NULL));
1265
1266 const Matcher<const string&> m2 = StartsWith("Hi");
1267 EXPECT_TRUE(m2.Matches("Hi"));
1268 EXPECT_TRUE(m2.Matches("Hi Hi!"));
1269 EXPECT_TRUE(m2.Matches("High"));
1270 EXPECT_FALSE(m2.Matches("H"));
1271 EXPECT_FALSE(m2.Matches(" Hi"));
1272}
1273
1274TEST(StartsWithTest, CanDescribeSelf) {
1275 Matcher<const std::string> m = StartsWith("Hi");
1276 EXPECT_EQ("starts with \"Hi\"", Describe(m));
1277}
1278
1279// Tests EndsWith(s).
1280
1281TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
1282 const Matcher<const char*> m1 = EndsWith("");
1283 EXPECT_TRUE(m1.Matches("Hi"));
1284 EXPECT_TRUE(m1.Matches(""));
1285 EXPECT_FALSE(m1.Matches(NULL));
1286
1287 const Matcher<const string&> m2 = EndsWith(string("Hi"));
1288 EXPECT_TRUE(m2.Matches("Hi"));
1289 EXPECT_TRUE(m2.Matches("Wow Hi Hi"));
1290 EXPECT_TRUE(m2.Matches("Super Hi"));
1291 EXPECT_FALSE(m2.Matches("i"));
1292 EXPECT_FALSE(m2.Matches("Hi "));
1293}
1294
1295TEST(EndsWithTest, CanDescribeSelf) {
1296 Matcher<const std::string> m = EndsWith("Hi");
1297 EXPECT_EQ("ends with \"Hi\"", Describe(m));
1298}
1299
shiqiane35fdd92008-12-10 05:08:54 +00001300// Tests MatchesRegex().
1301
1302TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
1303 const Matcher<const char*> m1 = MatchesRegex("a.*z");
1304 EXPECT_TRUE(m1.Matches("az"));
1305 EXPECT_TRUE(m1.Matches("abcz"));
1306 EXPECT_FALSE(m1.Matches(NULL));
1307
1308 const Matcher<const string&> m2 = MatchesRegex(new RE("a.*z"));
1309 EXPECT_TRUE(m2.Matches("azbz"));
1310 EXPECT_FALSE(m2.Matches("az1"));
1311 EXPECT_FALSE(m2.Matches("1az"));
1312}
1313
1314TEST(MatchesRegexTest, CanDescribeSelf) {
1315 Matcher<const std::string> m1 = MatchesRegex(string("Hi.*"));
1316 EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1));
1317
zhanyong.wand14aaed2010-01-14 05:36:32 +00001318 Matcher<const char*> m2 = MatchesRegex(new RE("a.*"));
1319 EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +00001320}
1321
1322// Tests ContainsRegex().
1323
1324TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
1325 const Matcher<const char*> m1 = ContainsRegex(string("a.*z"));
1326 EXPECT_TRUE(m1.Matches("az"));
1327 EXPECT_TRUE(m1.Matches("0abcz1"));
1328 EXPECT_FALSE(m1.Matches(NULL));
1329
1330 const Matcher<const string&> m2 = ContainsRegex(new RE("a.*z"));
1331 EXPECT_TRUE(m2.Matches("azbz"));
1332 EXPECT_TRUE(m2.Matches("az1"));
1333 EXPECT_FALSE(m2.Matches("1a"));
1334}
1335
1336TEST(ContainsRegexTest, CanDescribeSelf) {
1337 Matcher<const std::string> m1 = ContainsRegex("Hi.*");
1338 EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1));
1339
zhanyong.wand14aaed2010-01-14 05:36:32 +00001340 Matcher<const char*> m2 = ContainsRegex(new RE("a.*"));
1341 EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +00001342}
shiqiane35fdd92008-12-10 05:08:54 +00001343
1344// Tests for wide strings.
1345#if GTEST_HAS_STD_WSTRING
1346TEST(StdWideStrEqTest, MatchesEqual) {
1347 Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello"));
1348 EXPECT_TRUE(m.Matches(L"Hello"));
1349 EXPECT_FALSE(m.Matches(L"hello"));
1350 EXPECT_FALSE(m.Matches(NULL));
1351
1352 Matcher<const ::std::wstring&> m2 = StrEq(L"Hello");
1353 EXPECT_TRUE(m2.Matches(L"Hello"));
1354 EXPECT_FALSE(m2.Matches(L"Hi"));
1355
1356 Matcher<const ::std::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1357 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1358 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1359
1360 ::std::wstring str(L"01204500800");
1361 str[3] = L'\0';
1362 Matcher<const ::std::wstring&> m4 = StrEq(str);
1363 EXPECT_TRUE(m4.Matches(str));
1364 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1365 Matcher<const ::std::wstring&> m5 = StrEq(str);
1366 EXPECT_TRUE(m5.Matches(str));
1367}
1368
1369TEST(StdWideStrEqTest, CanDescribeSelf) {
1370 Matcher< ::std::wstring> m = StrEq(L"Hi-\'\"\?\\\a\b\f\n\r\t\v");
1371 EXPECT_EQ("is equal to L\"Hi-\'\\\"\\?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
1372 Describe(m));
1373
1374 Matcher< ::std::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1375 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1376 Describe(m2));
1377
1378 ::std::wstring str(L"01204500800");
1379 str[3] = L'\0';
1380 Matcher<const ::std::wstring&> m4 = StrEq(str);
1381 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1382 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1383 Matcher<const ::std::wstring&> m5 = StrEq(str);
1384 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1385}
1386
1387TEST(StdWideStrNeTest, MatchesUnequalString) {
1388 Matcher<const wchar_t*> m = StrNe(L"Hello");
1389 EXPECT_TRUE(m.Matches(L""));
1390 EXPECT_TRUE(m.Matches(NULL));
1391 EXPECT_FALSE(m.Matches(L"Hello"));
1392
1393 Matcher< ::std::wstring> m2 = StrNe(::std::wstring(L"Hello"));
1394 EXPECT_TRUE(m2.Matches(L"hello"));
1395 EXPECT_FALSE(m2.Matches(L"Hello"));
1396}
1397
1398TEST(StdWideStrNeTest, CanDescribeSelf) {
1399 Matcher<const wchar_t*> m = StrNe(L"Hi");
1400 EXPECT_EQ("is not equal to L\"Hi\"", Describe(m));
1401}
1402
1403TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1404 Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello"));
1405 EXPECT_TRUE(m.Matches(L"Hello"));
1406 EXPECT_TRUE(m.Matches(L"hello"));
1407 EXPECT_FALSE(m.Matches(L"Hi"));
1408 EXPECT_FALSE(m.Matches(NULL));
1409
1410 Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello");
1411 EXPECT_TRUE(m2.Matches(L"hello"));
1412 EXPECT_FALSE(m2.Matches(L"Hi"));
1413}
1414
1415TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1416 ::std::wstring str1(L"oabocdooeoo");
1417 ::std::wstring str2(L"OABOCDOOEOO");
1418 Matcher<const ::std::wstring&> m0 = StrCaseEq(str1);
1419 EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L'\0')));
1420
1421 str1[3] = str2[3] = L'\0';
1422 Matcher<const ::std::wstring&> m1 = StrCaseEq(str1);
1423 EXPECT_TRUE(m1.Matches(str2));
1424
1425 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1426 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1427 Matcher<const ::std::wstring&> m2 = StrCaseEq(str1);
1428 str1[9] = str2[9] = L'\0';
1429 EXPECT_FALSE(m2.Matches(str2));
1430
1431 Matcher<const ::std::wstring&> m3 = StrCaseEq(str1);
1432 EXPECT_TRUE(m3.Matches(str2));
1433
1434 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1435 str2.append(1, L'\0');
1436 EXPECT_FALSE(m3.Matches(str2));
1437 EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9)));
1438}
1439
1440TEST(StdWideStrCaseEqTest, CanDescribeSelf) {
1441 Matcher< ::std::wstring> m = StrCaseEq(L"Hi");
1442 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1443}
1444
1445TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1446 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1447 EXPECT_TRUE(m.Matches(L"Hi"));
1448 EXPECT_TRUE(m.Matches(NULL));
1449 EXPECT_FALSE(m.Matches(L"Hello"));
1450 EXPECT_FALSE(m.Matches(L"hello"));
1451
1452 Matcher< ::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello"));
1453 EXPECT_TRUE(m2.Matches(L""));
1454 EXPECT_FALSE(m2.Matches(L"Hello"));
1455}
1456
1457TEST(StdWideStrCaseNeTest, CanDescribeSelf) {
1458 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
1459 EXPECT_EQ("is not equal to (ignoring case) L\"Hi\"", Describe(m));
1460}
1461
1462// Tests that HasSubstr() works for matching wstring-typed values.
1463TEST(StdWideHasSubstrTest, WorksForStringClasses) {
1464 const Matcher< ::std::wstring> m1 = HasSubstr(L"foo");
1465 EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food.")));
1466 EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo")));
1467
1468 const Matcher<const ::std::wstring&> m2 = HasSubstr(L"foo");
1469 EXPECT_TRUE(m2.Matches(::std::wstring(L"I love food.")));
1470 EXPECT_FALSE(m2.Matches(::std::wstring(L"tofo")));
1471}
1472
1473// Tests that HasSubstr() works for matching C-wide-string-typed values.
1474TEST(StdWideHasSubstrTest, WorksForCStrings) {
1475 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1476 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1477 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1478 EXPECT_FALSE(m1.Matches(NULL));
1479
1480 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1481 EXPECT_TRUE(m2.Matches(L"I love food."));
1482 EXPECT_FALSE(m2.Matches(L"tofo"));
1483 EXPECT_FALSE(m2.Matches(NULL));
1484}
1485
1486// Tests that HasSubstr(s) describes itself properly.
1487TEST(StdWideHasSubstrTest, CanDescribeSelf) {
1488 Matcher< ::std::wstring> m = HasSubstr(L"foo\n\"");
1489 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1490}
1491
1492// Tests StartsWith(s).
1493
1494TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) {
1495 const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L""));
1496 EXPECT_TRUE(m1.Matches(L"Hi"));
1497 EXPECT_TRUE(m1.Matches(L""));
1498 EXPECT_FALSE(m1.Matches(NULL));
1499
1500 const Matcher<const ::std::wstring&> m2 = StartsWith(L"Hi");
1501 EXPECT_TRUE(m2.Matches(L"Hi"));
1502 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1503 EXPECT_TRUE(m2.Matches(L"High"));
1504 EXPECT_FALSE(m2.Matches(L"H"));
1505 EXPECT_FALSE(m2.Matches(L" Hi"));
1506}
1507
1508TEST(StdWideStartsWithTest, CanDescribeSelf) {
1509 Matcher<const ::std::wstring> m = StartsWith(L"Hi");
1510 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1511}
1512
1513// Tests EndsWith(s).
1514
1515TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) {
1516 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1517 EXPECT_TRUE(m1.Matches(L"Hi"));
1518 EXPECT_TRUE(m1.Matches(L""));
1519 EXPECT_FALSE(m1.Matches(NULL));
1520
1521 const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L"Hi"));
1522 EXPECT_TRUE(m2.Matches(L"Hi"));
1523 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1524 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1525 EXPECT_FALSE(m2.Matches(L"i"));
1526 EXPECT_FALSE(m2.Matches(L"Hi "));
1527}
1528
1529TEST(StdWideEndsWithTest, CanDescribeSelf) {
1530 Matcher<const ::std::wstring> m = EndsWith(L"Hi");
1531 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1532}
1533
1534#endif // GTEST_HAS_STD_WSTRING
1535
1536#if GTEST_HAS_GLOBAL_WSTRING
1537TEST(GlobalWideStrEqTest, MatchesEqual) {
1538 Matcher<const wchar_t*> m = StrEq(::wstring(L"Hello"));
1539 EXPECT_TRUE(m.Matches(L"Hello"));
1540 EXPECT_FALSE(m.Matches(L"hello"));
1541 EXPECT_FALSE(m.Matches(NULL));
1542
1543 Matcher<const ::wstring&> m2 = StrEq(L"Hello");
1544 EXPECT_TRUE(m2.Matches(L"Hello"));
1545 EXPECT_FALSE(m2.Matches(L"Hi"));
1546
1547 Matcher<const ::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1548 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1549 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1550
1551 ::wstring str(L"01204500800");
1552 str[3] = L'\0';
1553 Matcher<const ::wstring&> m4 = StrEq(str);
1554 EXPECT_TRUE(m4.Matches(str));
1555 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1556 Matcher<const ::wstring&> m5 = StrEq(str);
1557 EXPECT_TRUE(m5.Matches(str));
1558}
1559
1560TEST(GlobalWideStrEqTest, CanDescribeSelf) {
1561 Matcher< ::wstring> m = StrEq(L"Hi-\'\"\?\\\a\b\f\n\r\t\v");
1562 EXPECT_EQ("is equal to L\"Hi-\'\\\"\\?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
1563 Describe(m));
1564
1565 Matcher< ::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1566 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1567 Describe(m2));
1568
1569 ::wstring str(L"01204500800");
1570 str[3] = L'\0';
1571 Matcher<const ::wstring&> m4 = StrEq(str);
1572 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1573 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1574 Matcher<const ::wstring&> m5 = StrEq(str);
1575 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1576}
1577
1578TEST(GlobalWideStrNeTest, MatchesUnequalString) {
1579 Matcher<const wchar_t*> m = StrNe(L"Hello");
1580 EXPECT_TRUE(m.Matches(L""));
1581 EXPECT_TRUE(m.Matches(NULL));
1582 EXPECT_FALSE(m.Matches(L"Hello"));
1583
1584 Matcher< ::wstring> m2 = StrNe(::wstring(L"Hello"));
1585 EXPECT_TRUE(m2.Matches(L"hello"));
1586 EXPECT_FALSE(m2.Matches(L"Hello"));
1587}
1588
1589TEST(GlobalWideStrNeTest, CanDescribeSelf) {
1590 Matcher<const wchar_t*> m = StrNe(L"Hi");
1591 EXPECT_EQ("is not equal to L\"Hi\"", Describe(m));
1592}
1593
1594TEST(GlobalWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1595 Matcher<const wchar_t*> m = StrCaseEq(::wstring(L"Hello"));
1596 EXPECT_TRUE(m.Matches(L"Hello"));
1597 EXPECT_TRUE(m.Matches(L"hello"));
1598 EXPECT_FALSE(m.Matches(L"Hi"));
1599 EXPECT_FALSE(m.Matches(NULL));
1600
1601 Matcher<const ::wstring&> m2 = StrCaseEq(L"Hello");
1602 EXPECT_TRUE(m2.Matches(L"hello"));
1603 EXPECT_FALSE(m2.Matches(L"Hi"));
1604}
1605
1606TEST(GlobalWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1607 ::wstring str1(L"oabocdooeoo");
1608 ::wstring str2(L"OABOCDOOEOO");
1609 Matcher<const ::wstring&> m0 = StrCaseEq(str1);
1610 EXPECT_FALSE(m0.Matches(str2 + ::wstring(1, L'\0')));
1611
1612 str1[3] = str2[3] = L'\0';
1613 Matcher<const ::wstring&> m1 = StrCaseEq(str1);
1614 EXPECT_TRUE(m1.Matches(str2));
1615
1616 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1617 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1618 Matcher<const ::wstring&> m2 = StrCaseEq(str1);
1619 str1[9] = str2[9] = L'\0';
1620 EXPECT_FALSE(m2.Matches(str2));
1621
1622 Matcher<const ::wstring&> m3 = StrCaseEq(str1);
1623 EXPECT_TRUE(m3.Matches(str2));
1624
1625 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1626 str2.append(1, L'\0');
1627 EXPECT_FALSE(m3.Matches(str2));
1628 EXPECT_FALSE(m3.Matches(::wstring(str2, 0, 9)));
1629}
1630
1631TEST(GlobalWideStrCaseEqTest, CanDescribeSelf) {
1632 Matcher< ::wstring> m = StrCaseEq(L"Hi");
1633 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1634}
1635
1636TEST(GlobalWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1637 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1638 EXPECT_TRUE(m.Matches(L"Hi"));
1639 EXPECT_TRUE(m.Matches(NULL));
1640 EXPECT_FALSE(m.Matches(L"Hello"));
1641 EXPECT_FALSE(m.Matches(L"hello"));
1642
1643 Matcher< ::wstring> m2 = StrCaseNe(::wstring(L"Hello"));
1644 EXPECT_TRUE(m2.Matches(L""));
1645 EXPECT_FALSE(m2.Matches(L"Hello"));
1646}
1647
1648TEST(GlobalWideStrCaseNeTest, CanDescribeSelf) {
1649 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
1650 EXPECT_EQ("is not equal to (ignoring case) L\"Hi\"", Describe(m));
1651}
1652
1653// Tests that HasSubstr() works for matching wstring-typed values.
1654TEST(GlobalWideHasSubstrTest, WorksForStringClasses) {
1655 const Matcher< ::wstring> m1 = HasSubstr(L"foo");
1656 EXPECT_TRUE(m1.Matches(::wstring(L"I love food.")));
1657 EXPECT_FALSE(m1.Matches(::wstring(L"tofo")));
1658
1659 const Matcher<const ::wstring&> m2 = HasSubstr(L"foo");
1660 EXPECT_TRUE(m2.Matches(::wstring(L"I love food.")));
1661 EXPECT_FALSE(m2.Matches(::wstring(L"tofo")));
1662}
1663
1664// Tests that HasSubstr() works for matching C-wide-string-typed values.
1665TEST(GlobalWideHasSubstrTest, WorksForCStrings) {
1666 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1667 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1668 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1669 EXPECT_FALSE(m1.Matches(NULL));
1670
1671 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1672 EXPECT_TRUE(m2.Matches(L"I love food."));
1673 EXPECT_FALSE(m2.Matches(L"tofo"));
1674 EXPECT_FALSE(m2.Matches(NULL));
1675}
1676
1677// Tests that HasSubstr(s) describes itself properly.
1678TEST(GlobalWideHasSubstrTest, CanDescribeSelf) {
1679 Matcher< ::wstring> m = HasSubstr(L"foo\n\"");
1680 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1681}
1682
1683// Tests StartsWith(s).
1684
1685TEST(GlobalWideStartsWithTest, MatchesStringWithGivenPrefix) {
1686 const Matcher<const wchar_t*> m1 = StartsWith(::wstring(L""));
1687 EXPECT_TRUE(m1.Matches(L"Hi"));
1688 EXPECT_TRUE(m1.Matches(L""));
1689 EXPECT_FALSE(m1.Matches(NULL));
1690
1691 const Matcher<const ::wstring&> m2 = StartsWith(L"Hi");
1692 EXPECT_TRUE(m2.Matches(L"Hi"));
1693 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1694 EXPECT_TRUE(m2.Matches(L"High"));
1695 EXPECT_FALSE(m2.Matches(L"H"));
1696 EXPECT_FALSE(m2.Matches(L" Hi"));
1697}
1698
1699TEST(GlobalWideStartsWithTest, CanDescribeSelf) {
1700 Matcher<const ::wstring> m = StartsWith(L"Hi");
1701 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1702}
1703
1704// Tests EndsWith(s).
1705
1706TEST(GlobalWideEndsWithTest, MatchesStringWithGivenSuffix) {
1707 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1708 EXPECT_TRUE(m1.Matches(L"Hi"));
1709 EXPECT_TRUE(m1.Matches(L""));
1710 EXPECT_FALSE(m1.Matches(NULL));
1711
1712 const Matcher<const ::wstring&> m2 = EndsWith(::wstring(L"Hi"));
1713 EXPECT_TRUE(m2.Matches(L"Hi"));
1714 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1715 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1716 EXPECT_FALSE(m2.Matches(L"i"));
1717 EXPECT_FALSE(m2.Matches(L"Hi "));
1718}
1719
1720TEST(GlobalWideEndsWithTest, CanDescribeSelf) {
1721 Matcher<const ::wstring> m = EndsWith(L"Hi");
1722 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1723}
1724
1725#endif // GTEST_HAS_GLOBAL_WSTRING
1726
1727
1728typedef ::std::tr1::tuple<long, int> Tuple2; // NOLINT
1729
1730// Tests that Eq() matches a 2-tuple where the first field == the
1731// second field.
1732TEST(Eq2Test, MatchesEqualArguments) {
1733 Matcher<const Tuple2&> m = Eq();
1734 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1735 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1736}
1737
1738// Tests that Eq() describes itself properly.
1739TEST(Eq2Test, CanDescribeSelf) {
1740 Matcher<const Tuple2&> m = Eq();
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 Ge() matches a 2-tuple where the first field >= the
1745// second field.
1746TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) {
1747 Matcher<const Tuple2&> m = Ge();
1748 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1749 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1750 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1751}
1752
1753// Tests that Ge() describes itself properly.
1754TEST(Ge2Test, CanDescribeSelf) {
1755 Matcher<const Tuple2&> m = Ge();
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 Gt() matches a 2-tuple where the first field > the
1760// second field.
1761TEST(Gt2Test, MatchesGreaterThanArguments) {
1762 Matcher<const Tuple2&> m = Gt();
1763 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1764 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1765 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1766}
1767
1768// Tests that Gt() describes itself properly.
1769TEST(Gt2Test, CanDescribeSelf) {
1770 Matcher<const Tuple2&> m = Gt();
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 Le() matches a 2-tuple where the first field <= the
1775// second field.
1776TEST(Le2Test, MatchesLessThanOrEqualArguments) {
1777 Matcher<const Tuple2&> m = Le();
1778 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1779 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1780 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1781}
1782
1783// Tests that Le() describes itself properly.
1784TEST(Le2Test, CanDescribeSelf) {
1785 Matcher<const Tuple2&> m = Le();
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 Lt() matches a 2-tuple where the first field < the
1790// second field.
1791TEST(Lt2Test, MatchesLessThanArguments) {
1792 Matcher<const Tuple2&> m = Lt();
1793 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1794 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1795 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1796}
1797
1798// Tests that Lt() describes itself properly.
1799TEST(Lt2Test, CanDescribeSelf) {
1800 Matcher<const Tuple2&> m = Lt();
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 Ne() matches a 2-tuple where the first field != the
1805// second field.
1806TEST(Ne2Test, MatchesUnequalArguments) {
1807 Matcher<const Tuple2&> m = Ne();
1808 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1809 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1810 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1811}
1812
1813// Tests that Ne() describes itself properly.
1814TEST(Ne2Test, CanDescribeSelf) {
1815 Matcher<const Tuple2&> m = Ne();
zhanyong.wan2661c682009-06-09 05:42:12 +00001816 EXPECT_EQ("are a pair (x, y) where x != y", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001817}
1818
1819// Tests that Not(m) matches any value that doesn't match m.
1820TEST(NotTest, NegatesMatcher) {
1821 Matcher<int> m;
1822 m = Not(Eq(2));
1823 EXPECT_TRUE(m.Matches(3));
1824 EXPECT_FALSE(m.Matches(2));
1825}
1826
1827// Tests that Not(m) describes itself properly.
1828TEST(NotTest, CanDescribeSelf) {
1829 Matcher<int> m = Not(Eq(5));
1830 EXPECT_EQ("is not equal to 5", Describe(m));
1831}
1832
zhanyong.wan18490652009-05-11 18:54:08 +00001833// Tests that monomorphic matchers are safely cast by the Not matcher.
1834TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {
1835 // greater_than_5 is a monomorphic matcher.
1836 Matcher<int> greater_than_5 = Gt(5);
1837
1838 Matcher<const int&> m = Not(greater_than_5);
1839 Matcher<int&> m2 = Not(greater_than_5);
1840 Matcher<int&> m3 = Not(m);
1841}
1842
shiqiane35fdd92008-12-10 05:08:54 +00001843// Tests that AllOf(m1, ..., mn) matches any value that matches all of
1844// the given matchers.
1845TEST(AllOfTest, MatchesWhenAllMatch) {
1846 Matcher<int> m;
1847 m = AllOf(Le(2), Ge(1));
1848 EXPECT_TRUE(m.Matches(1));
1849 EXPECT_TRUE(m.Matches(2));
1850 EXPECT_FALSE(m.Matches(0));
1851 EXPECT_FALSE(m.Matches(3));
1852
1853 m = AllOf(Gt(0), Ne(1), Ne(2));
1854 EXPECT_TRUE(m.Matches(3));
1855 EXPECT_FALSE(m.Matches(2));
1856 EXPECT_FALSE(m.Matches(1));
1857 EXPECT_FALSE(m.Matches(0));
1858
1859 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
1860 EXPECT_TRUE(m.Matches(4));
1861 EXPECT_FALSE(m.Matches(3));
1862 EXPECT_FALSE(m.Matches(2));
1863 EXPECT_FALSE(m.Matches(1));
1864 EXPECT_FALSE(m.Matches(0));
1865
1866 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
1867 EXPECT_TRUE(m.Matches(0));
1868 EXPECT_TRUE(m.Matches(1));
1869 EXPECT_FALSE(m.Matches(3));
1870}
1871
1872// Tests that AllOf(m1, ..., mn) describes itself properly.
1873TEST(AllOfTest, CanDescribeSelf) {
1874 Matcher<int> m;
1875 m = AllOf(Le(2), Ge(1));
1876 EXPECT_EQ("(is less than or equal to 2) and "
1877 "(is greater than or equal to 1)",
1878 Describe(m));
1879
1880 m = AllOf(Gt(0), Ne(1), Ne(2));
1881 EXPECT_EQ("(is greater than 0) and "
1882 "((is not equal to 1) and "
1883 "(is not equal to 2))",
1884 Describe(m));
1885
1886
1887 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
1888 EXPECT_EQ("(is greater than 0) and "
1889 "((is not equal to 1) and "
1890 "((is not equal to 2) and "
1891 "(is not equal to 3)))",
1892 Describe(m));
1893
1894
1895 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
1896 EXPECT_EQ("(is greater than or equal to 0) and "
1897 "((is less than 10) and "
1898 "((is not equal to 3) and "
1899 "((is not equal to 5) and "
1900 "(is not equal to 7))))", Describe(m));
1901}
1902
zhanyong.wan18490652009-05-11 18:54:08 +00001903// Tests that monomorphic matchers are safely cast by the AllOf matcher.
1904TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
1905 // greater_than_5 and less_than_10 are monomorphic matchers.
1906 Matcher<int> greater_than_5 = Gt(5);
1907 Matcher<int> less_than_10 = Lt(10);
1908
1909 Matcher<const int&> m = AllOf(greater_than_5, less_than_10);
1910 Matcher<int&> m2 = AllOf(greater_than_5, less_than_10);
1911 Matcher<int&> m3 = AllOf(greater_than_5, m2);
1912
1913 // Tests that BothOf works when composing itself.
1914 Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10);
1915 Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);
1916}
1917
shiqiane35fdd92008-12-10 05:08:54 +00001918// Tests that AnyOf(m1, ..., mn) matches any value that matches at
1919// least one of the given matchers.
1920TEST(AnyOfTest, MatchesWhenAnyMatches) {
1921 Matcher<int> m;
1922 m = AnyOf(Le(1), Ge(3));
1923 EXPECT_TRUE(m.Matches(1));
1924 EXPECT_TRUE(m.Matches(4));
1925 EXPECT_FALSE(m.Matches(2));
1926
1927 m = AnyOf(Lt(0), Eq(1), Eq(2));
1928 EXPECT_TRUE(m.Matches(-1));
1929 EXPECT_TRUE(m.Matches(1));
1930 EXPECT_TRUE(m.Matches(2));
1931 EXPECT_FALSE(m.Matches(0));
1932
1933 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
1934 EXPECT_TRUE(m.Matches(-1));
1935 EXPECT_TRUE(m.Matches(1));
1936 EXPECT_TRUE(m.Matches(2));
1937 EXPECT_TRUE(m.Matches(3));
1938 EXPECT_FALSE(m.Matches(0));
1939
1940 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
1941 EXPECT_TRUE(m.Matches(0));
1942 EXPECT_TRUE(m.Matches(11));
1943 EXPECT_TRUE(m.Matches(3));
1944 EXPECT_FALSE(m.Matches(2));
1945}
1946
1947// Tests that AnyOf(m1, ..., mn) describes itself properly.
1948TEST(AnyOfTest, CanDescribeSelf) {
1949 Matcher<int> m;
1950 m = AnyOf(Le(1), Ge(3));
1951 EXPECT_EQ("(is less than or equal to 1) or "
1952 "(is greater than or equal to 3)",
1953 Describe(m));
1954
1955 m = AnyOf(Lt(0), Eq(1), Eq(2));
1956 EXPECT_EQ("(is less than 0) or "
1957 "((is equal to 1) or (is equal to 2))",
1958 Describe(m));
1959
1960 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
1961 EXPECT_EQ("(is less than 0) or "
1962 "((is equal to 1) or "
1963 "((is equal to 2) or "
1964 "(is equal to 3)))",
1965 Describe(m));
1966
1967 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
1968 EXPECT_EQ("(is less than or equal to 0) or "
1969 "((is greater than 10) or "
1970 "((is equal to 3) or "
1971 "((is equal to 5) or "
1972 "(is equal to 7))))",
1973 Describe(m));
1974}
1975
zhanyong.wan18490652009-05-11 18:54:08 +00001976// Tests that monomorphic matchers are safely cast by the AnyOf matcher.
1977TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
1978 // greater_than_5 and less_than_10 are monomorphic matchers.
1979 Matcher<int> greater_than_5 = Gt(5);
1980 Matcher<int> less_than_10 = Lt(10);
1981
1982 Matcher<const int&> m = AnyOf(greater_than_5, less_than_10);
1983 Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10);
1984 Matcher<int&> m3 = AnyOf(greater_than_5, m2);
1985
1986 // Tests that EitherOf works when composing itself.
1987 Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10);
1988 Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);
1989}
1990
shiqiane35fdd92008-12-10 05:08:54 +00001991// The following predicate function and predicate functor are for
1992// testing the Truly(predicate) matcher.
1993
1994// Returns non-zero if the input is positive. Note that the return
1995// type of this function is not bool. It's OK as Truly() accepts any
1996// unary function or functor whose return type can be implicitly
1997// converted to bool.
1998int IsPositive(double x) {
1999 return x > 0 ? 1 : 0;
2000}
2001
2002// This functor returns true if the input is greater than the given
2003// number.
2004class IsGreaterThan {
2005 public:
2006 explicit IsGreaterThan(int threshold) : threshold_(threshold) {}
2007
2008 bool operator()(int n) const { return n > threshold_; }
zhanyong.wan32de5f52009-12-23 00:13:23 +00002009
shiqiane35fdd92008-12-10 05:08:54 +00002010 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002011 int threshold_;
shiqiane35fdd92008-12-10 05:08:54 +00002012};
2013
2014// For testing Truly().
2015const int foo = 0;
2016
2017// This predicate returns true iff the argument references foo and has
2018// a zero value.
2019bool ReferencesFooAndIsZero(const int& n) {
2020 return (&n == &foo) && (n == 0);
2021}
2022
2023// Tests that Truly(predicate) matches what satisfies the given
2024// predicate.
2025TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {
2026 Matcher<double> m = Truly(IsPositive);
2027 EXPECT_TRUE(m.Matches(2.0));
2028 EXPECT_FALSE(m.Matches(-1.5));
2029}
2030
2031// Tests that Truly(predicate_functor) works too.
2032TEST(TrulyTest, CanBeUsedWithFunctor) {
2033 Matcher<int> m = Truly(IsGreaterThan(5));
2034 EXPECT_TRUE(m.Matches(6));
2035 EXPECT_FALSE(m.Matches(4));
2036}
2037
2038// Tests that Truly(predicate) can describe itself properly.
2039TEST(TrulyTest, CanDescribeSelf) {
2040 Matcher<double> m = Truly(IsPositive);
2041 EXPECT_EQ("satisfies the given predicate",
2042 Describe(m));
2043}
2044
2045// Tests that Truly(predicate) works when the matcher takes its
2046// argument by reference.
2047TEST(TrulyTest, WorksForByRefArguments) {
2048 Matcher<const int&> m = Truly(ReferencesFooAndIsZero);
2049 EXPECT_TRUE(m.Matches(foo));
2050 int n = 0;
2051 EXPECT_FALSE(m.Matches(n));
2052}
2053
2054// Tests that Matches(m) is a predicate satisfied by whatever that
2055// matches matcher m.
2056TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {
2057 EXPECT_TRUE(Matches(Ge(0))(1));
2058 EXPECT_FALSE(Matches(Eq('a'))('b'));
2059}
2060
2061// Tests that Matches(m) works when the matcher takes its argument by
2062// reference.
2063TEST(MatchesTest, WorksOnByRefArguments) {
2064 int m = 0, n = 0;
2065 EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n));
2066 EXPECT_FALSE(Matches(Ref(m))(n));
2067}
2068
2069// Tests that a Matcher on non-reference type can be used in
2070// Matches().
2071TEST(MatchesTest, WorksWithMatcherOnNonRefType) {
2072 Matcher<int> eq5 = Eq(5);
2073 EXPECT_TRUE(Matches(eq5)(5));
2074 EXPECT_FALSE(Matches(eq5)(2));
2075}
2076
zhanyong.wanb8243162009-06-04 05:48:20 +00002077// Tests Value(value, matcher). Since Value() is a simple wrapper for
2078// Matches(), which has been tested already, we don't spend a lot of
2079// effort on testing Value().
2080TEST(ValueTest, WorksWithPolymorphicMatcher) {
2081 EXPECT_TRUE(Value("hi", StartsWith("h")));
2082 EXPECT_FALSE(Value(5, Gt(10)));
2083}
2084
2085TEST(ValueTest, WorksWithMonomorphicMatcher) {
2086 const Matcher<int> is_zero = Eq(0);
2087 EXPECT_TRUE(Value(0, is_zero));
2088 EXPECT_FALSE(Value('a', is_zero));
2089
2090 int n = 0;
2091 const Matcher<const int&> ref_n = Ref(n);
2092 EXPECT_TRUE(Value(n, ref_n));
2093 EXPECT_FALSE(Value(1, ref_n));
2094}
2095
zhanyong.wana862f1d2010-03-15 21:23:04 +00002096TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) {
zhanyong.wan34b034c2010-03-05 21:23:23 +00002097 StringMatchResultListener listener1;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002098 EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002099 EXPECT_EQ("% 2 == 0", listener1.str());
2100
2101 StringMatchResultListener listener2;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002102 EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002103 EXPECT_EQ("", listener2.str());
2104}
2105
zhanyong.wana862f1d2010-03-15 21:23:04 +00002106TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) {
zhanyong.wan34b034c2010-03-05 21:23:23 +00002107 const Matcher<int> is_even = PolymorphicIsEven();
2108 StringMatchResultListener listener1;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002109 EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002110 EXPECT_EQ("% 2 == 0", listener1.str());
2111
2112 const Matcher<const double&> is_zero = Eq(0);
2113 StringMatchResultListener listener2;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002114 EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002115 EXPECT_EQ("", listener2.str());
2116}
2117
zhanyong.wana862f1d2010-03-15 21:23:04 +00002118MATCHER_P(Really, inner_matcher, "") {
2119 return ExplainMatchResult(inner_matcher, arg, result_listener);
2120}
2121
2122TEST(ExplainMatchResultTest, WorksInsideMATCHER) {
2123 EXPECT_THAT(0, Really(Eq(0)));
2124}
2125
zhanyong.wanbf550852009-06-09 06:09:53 +00002126TEST(AllArgsTest, WorksForTuple) {
2127 EXPECT_THAT(make_tuple(1, 2L), AllArgs(Lt()));
2128 EXPECT_THAT(make_tuple(2L, 1), Not(AllArgs(Lt())));
2129}
2130
2131TEST(AllArgsTest, WorksForNonTuple) {
2132 EXPECT_THAT(42, AllArgs(Gt(0)));
2133 EXPECT_THAT('a', Not(AllArgs(Eq('b'))));
2134}
2135
2136class AllArgsHelper {
2137 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002138 AllArgsHelper() {}
2139
zhanyong.wanbf550852009-06-09 06:09:53 +00002140 MOCK_METHOD2(Helper, int(char x, int y));
zhanyong.wan32de5f52009-12-23 00:13:23 +00002141
2142 private:
2143 GTEST_DISALLOW_COPY_AND_ASSIGN_(AllArgsHelper);
zhanyong.wanbf550852009-06-09 06:09:53 +00002144};
2145
2146TEST(AllArgsTest, WorksInWithClause) {
2147 AllArgsHelper helper;
2148 ON_CALL(helper, Helper(_, _))
2149 .With(AllArgs(Lt()))
2150 .WillByDefault(Return(1));
2151 EXPECT_CALL(helper, Helper(_, _));
2152 EXPECT_CALL(helper, Helper(_, _))
2153 .With(AllArgs(Gt()))
2154 .WillOnce(Return(2));
2155
2156 EXPECT_EQ(1, helper.Helper('\1', 2));
2157 EXPECT_EQ(2, helper.Helper('a', 1));
2158}
2159
shiqiane35fdd92008-12-10 05:08:54 +00002160// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2161// matches the matcher.
2162TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {
2163 ASSERT_THAT(5, Ge(2)) << "This should succeed.";
2164 ASSERT_THAT("Foo", EndsWith("oo"));
2165 EXPECT_THAT(2, AllOf(Le(7), Ge(0))) << "This should succeed too.";
2166 EXPECT_THAT("Hello", StartsWith("Hell"));
2167}
2168
2169// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2170// doesn't match the matcher.
2171TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {
2172 // 'n' must be static as it is used in an EXPECT_FATAL_FAILURE(),
2173 // which cannot reference auto variables.
2174 static int n;
2175 n = 5;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002176
2177 // VC++ prior to version 8.0 SP1 has a bug where it will not see any
2178 // functions declared in the namespace scope from within nested classes.
2179 // EXPECT/ASSERT_(NON)FATAL_FAILURE macros use nested classes so that all
2180 // namespace-level functions invoked inside them need to be explicitly
2181 // resolved.
2182 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Gt(10)),
shiqiane35fdd92008-12-10 05:08:54 +00002183 "Value of: n\n"
2184 "Expected: is greater than 10\n"
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002185 " Actual: 5");
shiqiane35fdd92008-12-10 05:08:54 +00002186 n = 0;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002187 EXPECT_NONFATAL_FAILURE(
2188 EXPECT_THAT(n, ::testing::AllOf(::testing::Le(7), ::testing::Ge(5))),
2189 "Value of: n\n"
2190 "Expected: (is less than or equal to 7) and "
2191 "(is greater than or equal to 5)\n"
2192 " Actual: 0");
shiqiane35fdd92008-12-10 05:08:54 +00002193}
2194
2195// Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument
2196// has a reference type.
2197TEST(MatcherAssertionTest, WorksForByRefArguments) {
2198 // We use a static variable here as EXPECT_FATAL_FAILURE() cannot
2199 // reference auto variables.
2200 static int n;
2201 n = 0;
2202 EXPECT_THAT(n, AllOf(Le(7), Ref(n)));
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002203 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
shiqiane35fdd92008-12-10 05:08:54 +00002204 "Value of: n\n"
2205 "Expected: does not reference the variable @");
2206 // Tests the "Actual" part.
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002207 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
shiqiane35fdd92008-12-10 05:08:54 +00002208 "Actual: 0 (is located @");
2209}
2210
zhanyong.wan95b12332009-09-25 18:55:50 +00002211#if !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002212// Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is
2213// monomorphic.
zhanyong.wan95b12332009-09-25 18:55:50 +00002214
2215// ASSERT_THAT("hello", starts_with_he) fails to compile with Nokia's
2216// Symbian compiler: it tries to compile
2217// template<T, U> class MatcherCastImpl { ...
zhanyong.wandb22c222010-01-28 21:52:29 +00002218// virtual bool MatchAndExplain(T x, ...) const {
2219// return source_matcher_.MatchAndExplain(static_cast<U>(x), ...);
zhanyong.wan95b12332009-09-25 18:55:50 +00002220// with U == string and T == const char*
2221// With ASSERT_THAT("hello"...) changed to ASSERT_THAT(string("hello") ... )
2222// the compiler silently crashes with no output.
2223// If MatcherCastImpl is changed to use U(x) instead of static_cast<U>(x)
2224// the code compiles but the converted string is bogus.
shiqiane35fdd92008-12-10 05:08:54 +00002225TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
2226 Matcher<const char*> starts_with_he = StartsWith("he");
2227 ASSERT_THAT("hello", starts_with_he);
2228
2229 Matcher<const string&> ends_with_ok = EndsWith("ok");
2230 ASSERT_THAT("book", ends_with_ok);
2231
2232 Matcher<int> is_greater_than_5 = Gt(5);
2233 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5),
2234 "Value of: 5\n"
2235 "Expected: is greater than 5\n"
2236 " Actual: 5");
2237}
zhanyong.wan95b12332009-09-25 18:55:50 +00002238#endif // !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002239
2240// Tests floating-point matchers.
2241template <typename RawType>
2242class FloatingPointTest : public testing::Test {
2243 protected:
2244 typedef typename testing::internal::FloatingPoint<RawType> Floating;
2245 typedef typename Floating::Bits Bits;
2246
2247 virtual void SetUp() {
2248 const size_t max_ulps = Floating::kMaxUlps;
2249
2250 // The bits that represent 0.0.
2251 const Bits zero_bits = Floating(0).bits();
2252
2253 // Makes some numbers close to 0.0.
2254 close_to_positive_zero_ = Floating::ReinterpretBits(zero_bits + max_ulps/2);
2255 close_to_negative_zero_ = -Floating::ReinterpretBits(
2256 zero_bits + max_ulps - max_ulps/2);
2257 further_from_negative_zero_ = -Floating::ReinterpretBits(
2258 zero_bits + max_ulps + 1 - max_ulps/2);
2259
2260 // The bits that represent 1.0.
2261 const Bits one_bits = Floating(1).bits();
2262
2263 // Makes some numbers close to 1.0.
2264 close_to_one_ = Floating::ReinterpretBits(one_bits + max_ulps);
2265 further_from_one_ = Floating::ReinterpretBits(one_bits + max_ulps + 1);
2266
2267 // +infinity.
2268 infinity_ = Floating::Infinity();
2269
2270 // The bits that represent +infinity.
2271 const Bits infinity_bits = Floating(infinity_).bits();
2272
2273 // Makes some numbers close to infinity.
2274 close_to_infinity_ = Floating::ReinterpretBits(infinity_bits - max_ulps);
2275 further_from_infinity_ = Floating::ReinterpretBits(
2276 infinity_bits - max_ulps - 1);
2277
2278 // Makes some NAN's.
2279 nan1_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 1);
2280 nan2_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 200);
2281 }
2282
2283 void TestSize() {
2284 EXPECT_EQ(sizeof(RawType), sizeof(Bits));
2285 }
2286
2287 // A battery of tests for FloatingEqMatcher::Matches.
2288 // matcher_maker is a pointer to a function which creates a FloatingEqMatcher.
2289 void TestMatches(
2290 testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) {
2291 Matcher<RawType> m1 = matcher_maker(0.0);
2292 EXPECT_TRUE(m1.Matches(-0.0));
2293 EXPECT_TRUE(m1.Matches(close_to_positive_zero_));
2294 EXPECT_TRUE(m1.Matches(close_to_negative_zero_));
2295 EXPECT_FALSE(m1.Matches(1.0));
2296
2297 Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_);
2298 EXPECT_FALSE(m2.Matches(further_from_negative_zero_));
2299
2300 Matcher<RawType> m3 = matcher_maker(1.0);
2301 EXPECT_TRUE(m3.Matches(close_to_one_));
2302 EXPECT_FALSE(m3.Matches(further_from_one_));
2303
2304 // Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above.
2305 EXPECT_FALSE(m3.Matches(0.0));
2306
2307 Matcher<RawType> m4 = matcher_maker(-infinity_);
2308 EXPECT_TRUE(m4.Matches(-close_to_infinity_));
2309
2310 Matcher<RawType> m5 = matcher_maker(infinity_);
2311 EXPECT_TRUE(m5.Matches(close_to_infinity_));
2312
2313 // This is interesting as the representations of infinity_ and nan1_
2314 // are only 1 DLP apart.
2315 EXPECT_FALSE(m5.Matches(nan1_));
2316
2317 // matcher_maker can produce a Matcher<const RawType&>, which is needed in
2318 // some cases.
2319 Matcher<const RawType&> m6 = matcher_maker(0.0);
2320 EXPECT_TRUE(m6.Matches(-0.0));
2321 EXPECT_TRUE(m6.Matches(close_to_positive_zero_));
2322 EXPECT_FALSE(m6.Matches(1.0));
2323
2324 // matcher_maker can produce a Matcher<RawType&>, which is needed in some
2325 // cases.
2326 Matcher<RawType&> m7 = matcher_maker(0.0);
2327 RawType x = 0.0;
2328 EXPECT_TRUE(m7.Matches(x));
2329 x = 0.01f;
2330 EXPECT_FALSE(m7.Matches(x));
2331 }
2332
2333 // Pre-calculated numbers to be used by the tests.
2334
2335 static RawType close_to_positive_zero_;
2336 static RawType close_to_negative_zero_;
2337 static RawType further_from_negative_zero_;
2338
2339 static RawType close_to_one_;
2340 static RawType further_from_one_;
2341
2342 static RawType infinity_;
2343 static RawType close_to_infinity_;
2344 static RawType further_from_infinity_;
2345
2346 static RawType nan1_;
2347 static RawType nan2_;
2348};
2349
2350template <typename RawType>
2351RawType FloatingPointTest<RawType>::close_to_positive_zero_;
2352
2353template <typename RawType>
2354RawType FloatingPointTest<RawType>::close_to_negative_zero_;
2355
2356template <typename RawType>
2357RawType FloatingPointTest<RawType>::further_from_negative_zero_;
2358
2359template <typename RawType>
2360RawType FloatingPointTest<RawType>::close_to_one_;
2361
2362template <typename RawType>
2363RawType FloatingPointTest<RawType>::further_from_one_;
2364
2365template <typename RawType>
2366RawType FloatingPointTest<RawType>::infinity_;
2367
2368template <typename RawType>
2369RawType FloatingPointTest<RawType>::close_to_infinity_;
2370
2371template <typename RawType>
2372RawType FloatingPointTest<RawType>::further_from_infinity_;
2373
2374template <typename RawType>
2375RawType FloatingPointTest<RawType>::nan1_;
2376
2377template <typename RawType>
2378RawType FloatingPointTest<RawType>::nan2_;
2379
2380// Instantiate FloatingPointTest for testing floats.
2381typedef FloatingPointTest<float> FloatTest;
2382
2383TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) {
2384 TestMatches(&FloatEq);
2385}
2386
2387TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
2388 TestMatches(&NanSensitiveFloatEq);
2389}
2390
2391TEST_F(FloatTest, FloatEqCannotMatchNaN) {
2392 // FloatEq never matches NaN.
2393 Matcher<float> m = FloatEq(nan1_);
2394 EXPECT_FALSE(m.Matches(nan1_));
2395 EXPECT_FALSE(m.Matches(nan2_));
2396 EXPECT_FALSE(m.Matches(1.0));
2397}
2398
2399TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {
2400 // NanSensitiveFloatEq will match NaN.
2401 Matcher<float> m = NanSensitiveFloatEq(nan1_);
2402 EXPECT_TRUE(m.Matches(nan1_));
2403 EXPECT_TRUE(m.Matches(nan2_));
2404 EXPECT_FALSE(m.Matches(1.0));
2405}
2406
2407TEST_F(FloatTest, FloatEqCanDescribeSelf) {
2408 Matcher<float> m1 = FloatEq(2.0f);
2409 EXPECT_EQ("is approximately 2", Describe(m1));
2410 EXPECT_EQ("is not approximately 2", DescribeNegation(m1));
2411
2412 Matcher<float> m2 = FloatEq(0.5f);
2413 EXPECT_EQ("is approximately 0.5", Describe(m2));
2414 EXPECT_EQ("is not approximately 0.5", DescribeNegation(m2));
2415
2416 Matcher<float> m3 = FloatEq(nan1_);
2417 EXPECT_EQ("never matches", Describe(m3));
2418 EXPECT_EQ("is anything", DescribeNegation(m3));
2419}
2420
2421TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {
2422 Matcher<float> m1 = NanSensitiveFloatEq(2.0f);
2423 EXPECT_EQ("is approximately 2", Describe(m1));
2424 EXPECT_EQ("is not approximately 2", DescribeNegation(m1));
2425
2426 Matcher<float> m2 = NanSensitiveFloatEq(0.5f);
2427 EXPECT_EQ("is approximately 0.5", Describe(m2));
2428 EXPECT_EQ("is not approximately 0.5", DescribeNegation(m2));
2429
2430 Matcher<float> m3 = NanSensitiveFloatEq(nan1_);
2431 EXPECT_EQ("is NaN", Describe(m3));
2432 EXPECT_EQ("is not NaN", DescribeNegation(m3));
2433}
2434
2435// Instantiate FloatingPointTest for testing doubles.
2436typedef FloatingPointTest<double> DoubleTest;
2437
2438TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
2439 TestMatches(&DoubleEq);
2440}
2441
2442TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
2443 TestMatches(&NanSensitiveDoubleEq);
2444}
2445
2446TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {
2447 // DoubleEq never matches NaN.
2448 Matcher<double> m = DoubleEq(nan1_);
2449 EXPECT_FALSE(m.Matches(nan1_));
2450 EXPECT_FALSE(m.Matches(nan2_));
2451 EXPECT_FALSE(m.Matches(1.0));
2452}
2453
2454TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {
2455 // NanSensitiveDoubleEq will match NaN.
2456 Matcher<double> m = NanSensitiveDoubleEq(nan1_);
2457 EXPECT_TRUE(m.Matches(nan1_));
2458 EXPECT_TRUE(m.Matches(nan2_));
2459 EXPECT_FALSE(m.Matches(1.0));
2460}
2461
2462TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {
2463 Matcher<double> m1 = DoubleEq(2.0);
2464 EXPECT_EQ("is approximately 2", Describe(m1));
2465 EXPECT_EQ("is not approximately 2", DescribeNegation(m1));
2466
2467 Matcher<double> m2 = DoubleEq(0.5);
2468 EXPECT_EQ("is approximately 0.5", Describe(m2));
2469 EXPECT_EQ("is not approximately 0.5", DescribeNegation(m2));
2470
2471 Matcher<double> m3 = DoubleEq(nan1_);
2472 EXPECT_EQ("never matches", Describe(m3));
2473 EXPECT_EQ("is anything", DescribeNegation(m3));
2474}
2475
2476TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {
2477 Matcher<double> m1 = NanSensitiveDoubleEq(2.0);
2478 EXPECT_EQ("is approximately 2", Describe(m1));
2479 EXPECT_EQ("is not approximately 2", DescribeNegation(m1));
2480
2481 Matcher<double> m2 = NanSensitiveDoubleEq(0.5);
2482 EXPECT_EQ("is approximately 0.5", Describe(m2));
2483 EXPECT_EQ("is not approximately 0.5", DescribeNegation(m2));
2484
2485 Matcher<double> m3 = NanSensitiveDoubleEq(nan1_);
2486 EXPECT_EQ("is NaN", Describe(m3));
2487 EXPECT_EQ("is not NaN", DescribeNegation(m3));
2488}
2489
2490TEST(PointeeTest, RawPointer) {
2491 const Matcher<int*> m = Pointee(Ge(0));
2492
2493 int n = 1;
2494 EXPECT_TRUE(m.Matches(&n));
2495 n = -1;
2496 EXPECT_FALSE(m.Matches(&n));
2497 EXPECT_FALSE(m.Matches(NULL));
2498}
2499
2500TEST(PointeeTest, RawPointerToConst) {
2501 const Matcher<const double*> m = Pointee(Ge(0));
2502
2503 double x = 1;
2504 EXPECT_TRUE(m.Matches(&x));
2505 x = -1;
2506 EXPECT_FALSE(m.Matches(&x));
2507 EXPECT_FALSE(m.Matches(NULL));
2508}
2509
2510TEST(PointeeTest, ReferenceToConstRawPointer) {
2511 const Matcher<int* const &> m = Pointee(Ge(0));
2512
2513 int n = 1;
2514 EXPECT_TRUE(m.Matches(&n));
2515 n = -1;
2516 EXPECT_FALSE(m.Matches(&n));
2517 EXPECT_FALSE(m.Matches(NULL));
2518}
2519
2520TEST(PointeeTest, ReferenceToNonConstRawPointer) {
2521 const Matcher<double* &> m = Pointee(Ge(0));
2522
2523 double x = 1.0;
2524 double* p = &x;
2525 EXPECT_TRUE(m.Matches(p));
2526 x = -1;
2527 EXPECT_FALSE(m.Matches(p));
2528 p = NULL;
2529 EXPECT_FALSE(m.Matches(p));
2530}
2531
2532TEST(PointeeTest, NeverMatchesNull) {
2533 const Matcher<const char*> m = Pointee(_);
2534 EXPECT_FALSE(m.Matches(NULL));
2535}
2536
2537// Tests that we can write Pointee(value) instead of Pointee(Eq(value)).
2538TEST(PointeeTest, MatchesAgainstAValue) {
2539 const Matcher<int*> m = Pointee(5);
2540
2541 int n = 5;
2542 EXPECT_TRUE(m.Matches(&n));
2543 n = -1;
2544 EXPECT_FALSE(m.Matches(&n));
2545 EXPECT_FALSE(m.Matches(NULL));
2546}
2547
2548TEST(PointeeTest, CanDescribeSelf) {
2549 const Matcher<int*> m = Pointee(Gt(3));
2550 EXPECT_EQ("points to a value that is greater than 3", Describe(m));
2551 EXPECT_EQ("does not point to a value that is greater than 3",
2552 DescribeNegation(m));
2553}
2554
shiqiane35fdd92008-12-10 05:08:54 +00002555TEST(PointeeTest, CanExplainMatchResult) {
2556 const Matcher<const string*> m = Pointee(StartsWith("Hi"));
2557
2558 EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL)));
2559
2560 const Matcher<int*> m2 = Pointee(GreaterThan(1));
2561 int n = 3;
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002562 EXPECT_EQ("which points to 3, which is 2 more than 1",
2563 Explain(m2, &n));
2564}
2565
2566TEST(PointeeTest, AlwaysExplainsPointee) {
2567 const Matcher<int*> m = Pointee(0);
2568 int n = 42;
2569 EXPECT_EQ("which points to 42", Explain(m, &n));
shiqiane35fdd92008-12-10 05:08:54 +00002570}
2571
2572// An uncopyable class.
2573class Uncopyable {
2574 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002575 explicit Uncopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00002576
2577 int value() const { return value_; }
2578 private:
2579 const int value_;
2580 GTEST_DISALLOW_COPY_AND_ASSIGN_(Uncopyable);
2581};
2582
2583// Returns true iff x.value() is positive.
2584bool ValueIsPositive(const Uncopyable& x) { return x.value() > 0; }
2585
2586// A user-defined struct for testing Field().
2587struct AStruct {
2588 AStruct() : x(0), y(1.0), z(5), p(NULL) {}
2589 AStruct(const AStruct& rhs)
2590 : x(rhs.x), y(rhs.y), z(rhs.z.value()), p(rhs.p) {}
2591
2592 int x; // A non-const field.
2593 const double y; // A const field.
2594 Uncopyable z; // An uncopyable field.
2595 const char* p; // A pointer field.
zhanyong.wan32de5f52009-12-23 00:13:23 +00002596
2597 private:
2598 GTEST_DISALLOW_ASSIGN_(AStruct);
shiqiane35fdd92008-12-10 05:08:54 +00002599};
2600
2601// A derived struct for testing Field().
2602struct DerivedStruct : public AStruct {
2603 char ch;
zhanyong.wan32de5f52009-12-23 00:13:23 +00002604
2605 private:
2606 GTEST_DISALLOW_ASSIGN_(DerivedStruct);
shiqiane35fdd92008-12-10 05:08:54 +00002607};
2608
2609// Tests that Field(&Foo::field, ...) works when field is non-const.
2610TEST(FieldTest, WorksForNonConstField) {
2611 Matcher<AStruct> m = Field(&AStruct::x, Ge(0));
2612
2613 AStruct a;
2614 EXPECT_TRUE(m.Matches(a));
2615 a.x = -1;
2616 EXPECT_FALSE(m.Matches(a));
2617}
2618
2619// Tests that Field(&Foo::field, ...) works when field is const.
2620TEST(FieldTest, WorksForConstField) {
2621 AStruct a;
2622
2623 Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0));
2624 EXPECT_TRUE(m.Matches(a));
2625 m = Field(&AStruct::y, Le(0.0));
2626 EXPECT_FALSE(m.Matches(a));
2627}
2628
2629// Tests that Field(&Foo::field, ...) works when field is not copyable.
2630TEST(FieldTest, WorksForUncopyableField) {
2631 AStruct a;
2632
2633 Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive));
2634 EXPECT_TRUE(m.Matches(a));
2635 m = Field(&AStruct::z, Not(Truly(ValueIsPositive)));
2636 EXPECT_FALSE(m.Matches(a));
2637}
2638
2639// Tests that Field(&Foo::field, ...) works when field is a pointer.
2640TEST(FieldTest, WorksForPointerField) {
2641 // Matching against NULL.
2642 Matcher<AStruct> m = Field(&AStruct::p, static_cast<const char*>(NULL));
2643 AStruct a;
2644 EXPECT_TRUE(m.Matches(a));
2645 a.p = "hi";
2646 EXPECT_FALSE(m.Matches(a));
2647
2648 // Matching a pointer that is not NULL.
2649 m = Field(&AStruct::p, StartsWith("hi"));
2650 a.p = "hill";
2651 EXPECT_TRUE(m.Matches(a));
2652 a.p = "hole";
2653 EXPECT_FALSE(m.Matches(a));
2654}
2655
2656// Tests that Field() works when the object is passed by reference.
2657TEST(FieldTest, WorksForByRefArgument) {
2658 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2659
2660 AStruct a;
2661 EXPECT_TRUE(m.Matches(a));
2662 a.x = -1;
2663 EXPECT_FALSE(m.Matches(a));
2664}
2665
2666// Tests that Field(&Foo::field, ...) works when the argument's type
2667// is a sub-type of Foo.
2668TEST(FieldTest, WorksForArgumentOfSubType) {
2669 // Note that the matcher expects DerivedStruct but we say AStruct
2670 // inside Field().
2671 Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0));
2672
2673 DerivedStruct d;
2674 EXPECT_TRUE(m.Matches(d));
2675 d.x = -1;
2676 EXPECT_FALSE(m.Matches(d));
2677}
2678
2679// Tests that Field(&Foo::field, m) works when field's type and m's
2680// argument type are compatible but not the same.
2681TEST(FieldTest, WorksForCompatibleMatcherType) {
2682 // The field is an int, but the inner matcher expects a signed char.
2683 Matcher<const AStruct&> m = Field(&AStruct::x,
2684 Matcher<signed char>(Ge(0)));
2685
2686 AStruct a;
2687 EXPECT_TRUE(m.Matches(a));
2688 a.x = -1;
2689 EXPECT_FALSE(m.Matches(a));
2690}
2691
2692// Tests that Field() can describe itself.
2693TEST(FieldTest, CanDescribeSelf) {
2694 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2695
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002696 EXPECT_EQ("is an object whose given field is greater than or equal to 0",
2697 Describe(m));
2698 EXPECT_EQ("is an object whose given field is not greater than or equal to 0",
shiqiane35fdd92008-12-10 05:08:54 +00002699 DescribeNegation(m));
2700}
2701
2702// Tests that Field() can explain the match result.
2703TEST(FieldTest, CanExplainMatchResult) {
2704 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2705
2706 AStruct a;
2707 a.x = 1;
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002708 EXPECT_EQ("whose given field is 1", Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00002709
2710 m = Field(&AStruct::x, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002711 EXPECT_EQ("whose given field is 1, which is 1 more than 0", Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00002712}
2713
2714// Tests that Field() works when the argument is a pointer to const.
2715TEST(FieldForPointerTest, WorksForPointerToConst) {
2716 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2717
2718 AStruct a;
2719 EXPECT_TRUE(m.Matches(&a));
2720 a.x = -1;
2721 EXPECT_FALSE(m.Matches(&a));
2722}
2723
2724// Tests that Field() works when the argument is a pointer to non-const.
2725TEST(FieldForPointerTest, WorksForPointerToNonConst) {
2726 Matcher<AStruct*> m = Field(&AStruct::x, Ge(0));
2727
2728 AStruct a;
2729 EXPECT_TRUE(m.Matches(&a));
2730 a.x = -1;
2731 EXPECT_FALSE(m.Matches(&a));
2732}
2733
zhanyong.wan6953a722010-01-13 05:15:07 +00002734// Tests that Field() works when the argument is a reference to a const pointer.
2735TEST(FieldForPointerTest, WorksForReferenceToConstPointer) {
2736 Matcher<AStruct* const&> m = Field(&AStruct::x, Ge(0));
2737
2738 AStruct a;
2739 EXPECT_TRUE(m.Matches(&a));
2740 a.x = -1;
2741 EXPECT_FALSE(m.Matches(&a));
2742}
2743
shiqiane35fdd92008-12-10 05:08:54 +00002744// Tests that Field() does not match the NULL pointer.
2745TEST(FieldForPointerTest, DoesNotMatchNull) {
2746 Matcher<const AStruct*> m = Field(&AStruct::x, _);
2747 EXPECT_FALSE(m.Matches(NULL));
2748}
2749
2750// Tests that Field(&Foo::field, ...) works when the argument's type
2751// is a sub-type of const Foo*.
2752TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
2753 // Note that the matcher expects DerivedStruct but we say AStruct
2754 // inside Field().
2755 Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0));
2756
2757 DerivedStruct d;
2758 EXPECT_TRUE(m.Matches(&d));
2759 d.x = -1;
2760 EXPECT_FALSE(m.Matches(&d));
2761}
2762
2763// Tests that Field() can describe itself when used to match a pointer.
2764TEST(FieldForPointerTest, CanDescribeSelf) {
2765 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2766
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002767 EXPECT_EQ("is an object whose given field is greater than or equal to 0",
2768 Describe(m));
2769 EXPECT_EQ("is an object whose given field is not greater than or equal to 0",
shiqiane35fdd92008-12-10 05:08:54 +00002770 DescribeNegation(m));
2771}
2772
2773// Tests that Field() can explain the result of matching a pointer.
2774TEST(FieldForPointerTest, CanExplainMatchResult) {
2775 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2776
2777 AStruct a;
2778 a.x = 1;
2779 EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(NULL)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002780 EXPECT_EQ("which points to an object whose given field is 1", Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00002781
2782 m = Field(&AStruct::x, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002783 EXPECT_EQ("which points to an object whose given field is 1, "
2784 "which is 1 more than 0", Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00002785}
2786
2787// A user-defined class for testing Property().
2788class AClass {
2789 public:
2790 AClass() : n_(0) {}
2791
2792 // A getter that returns a non-reference.
2793 int n() const { return n_; }
2794
2795 void set_n(int new_n) { n_ = new_n; }
2796
2797 // A getter that returns a reference to const.
2798 const string& s() const { return s_; }
2799
2800 void set_s(const string& new_s) { s_ = new_s; }
2801
2802 // A getter that returns a reference to non-const.
2803 double& x() const { return x_; }
2804 private:
2805 int n_;
2806 string s_;
2807
2808 static double x_;
2809};
2810
2811double AClass::x_ = 0.0;
2812
2813// A derived class for testing Property().
2814class DerivedClass : public AClass {
2815 private:
2816 int k_;
2817};
2818
2819// Tests that Property(&Foo::property, ...) works when property()
2820// returns a non-reference.
2821TEST(PropertyTest, WorksForNonReferenceProperty) {
2822 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
2823
2824 AClass a;
2825 a.set_n(1);
2826 EXPECT_TRUE(m.Matches(a));
2827
2828 a.set_n(-1);
2829 EXPECT_FALSE(m.Matches(a));
2830}
2831
2832// Tests that Property(&Foo::property, ...) works when property()
2833// returns a reference to const.
2834TEST(PropertyTest, WorksForReferenceToConstProperty) {
2835 Matcher<const AClass&> m = Property(&AClass::s, StartsWith("hi"));
2836
2837 AClass a;
2838 a.set_s("hill");
2839 EXPECT_TRUE(m.Matches(a));
2840
2841 a.set_s("hole");
2842 EXPECT_FALSE(m.Matches(a));
2843}
2844
2845// Tests that Property(&Foo::property, ...) works when property()
2846// returns a reference to non-const.
2847TEST(PropertyTest, WorksForReferenceToNonConstProperty) {
2848 double x = 0.0;
2849 AClass a;
2850
2851 Matcher<const AClass&> m = Property(&AClass::x, Ref(x));
2852 EXPECT_FALSE(m.Matches(a));
2853
2854 m = Property(&AClass::x, Not(Ref(x)));
2855 EXPECT_TRUE(m.Matches(a));
2856}
2857
2858// Tests that Property(&Foo::property, ...) works when the argument is
2859// passed by value.
2860TEST(PropertyTest, WorksForByValueArgument) {
2861 Matcher<AClass> m = Property(&AClass::s, StartsWith("hi"));
2862
2863 AClass a;
2864 a.set_s("hill");
2865 EXPECT_TRUE(m.Matches(a));
2866
2867 a.set_s("hole");
2868 EXPECT_FALSE(m.Matches(a));
2869}
2870
2871// Tests that Property(&Foo::property, ...) works when the argument's
2872// type is a sub-type of Foo.
2873TEST(PropertyTest, WorksForArgumentOfSubType) {
2874 // The matcher expects a DerivedClass, but inside the Property() we
2875 // say AClass.
2876 Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0));
2877
2878 DerivedClass d;
2879 d.set_n(1);
2880 EXPECT_TRUE(m.Matches(d));
2881
2882 d.set_n(-1);
2883 EXPECT_FALSE(m.Matches(d));
2884}
2885
2886// Tests that Property(&Foo::property, m) works when property()'s type
2887// and m's argument type are compatible but different.
2888TEST(PropertyTest, WorksForCompatibleMatcherType) {
2889 // n() returns an int but the inner matcher expects a signed char.
2890 Matcher<const AClass&> m = Property(&AClass::n,
2891 Matcher<signed char>(Ge(0)));
2892
2893 AClass a;
2894 EXPECT_TRUE(m.Matches(a));
2895 a.set_n(-1);
2896 EXPECT_FALSE(m.Matches(a));
2897}
2898
2899// Tests that Property() can describe itself.
2900TEST(PropertyTest, CanDescribeSelf) {
2901 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
2902
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002903 EXPECT_EQ("is an object whose given property is greater than or equal to 0",
2904 Describe(m));
2905 EXPECT_EQ("is an object whose given property "
2906 "is not greater than or equal to 0", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00002907}
2908
2909// Tests that Property() can explain the match result.
2910TEST(PropertyTest, CanExplainMatchResult) {
2911 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
2912
2913 AClass a;
2914 a.set_n(1);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002915 EXPECT_EQ("whose given property is 1", Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00002916
2917 m = Property(&AClass::n, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002918 EXPECT_EQ("whose given property is 1, which is 1 more than 0", Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00002919}
2920
2921// Tests that Property() works when the argument is a pointer to const.
2922TEST(PropertyForPointerTest, WorksForPointerToConst) {
2923 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
2924
2925 AClass a;
2926 a.set_n(1);
2927 EXPECT_TRUE(m.Matches(&a));
2928
2929 a.set_n(-1);
2930 EXPECT_FALSE(m.Matches(&a));
2931}
2932
2933// Tests that Property() works when the argument is a pointer to non-const.
2934TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
2935 Matcher<AClass*> m = Property(&AClass::s, StartsWith("hi"));
2936
2937 AClass a;
2938 a.set_s("hill");
2939 EXPECT_TRUE(m.Matches(&a));
2940
2941 a.set_s("hole");
2942 EXPECT_FALSE(m.Matches(&a));
2943}
2944
zhanyong.wan6953a722010-01-13 05:15:07 +00002945// Tests that Property() works when the argument is a reference to a
2946// const pointer.
2947TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) {
2948 Matcher<AClass* const&> m = Property(&AClass::s, StartsWith("hi"));
2949
2950 AClass a;
2951 a.set_s("hill");
2952 EXPECT_TRUE(m.Matches(&a));
2953
2954 a.set_s("hole");
2955 EXPECT_FALSE(m.Matches(&a));
2956}
2957
shiqiane35fdd92008-12-10 05:08:54 +00002958// Tests that Property() does not match the NULL pointer.
2959TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
2960 Matcher<const AClass*> m = Property(&AClass::x, _);
2961 EXPECT_FALSE(m.Matches(NULL));
2962}
2963
2964// Tests that Property(&Foo::property, ...) works when the argument's
2965// type is a sub-type of const Foo*.
2966TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
2967 // The matcher expects a DerivedClass, but inside the Property() we
2968 // say AClass.
2969 Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0));
2970
2971 DerivedClass d;
2972 d.set_n(1);
2973 EXPECT_TRUE(m.Matches(&d));
2974
2975 d.set_n(-1);
2976 EXPECT_FALSE(m.Matches(&d));
2977}
2978
2979// Tests that Property() can describe itself when used to match a pointer.
2980TEST(PropertyForPointerTest, CanDescribeSelf) {
2981 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
2982
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002983 EXPECT_EQ("is an object whose given property is greater than or equal to 0",
2984 Describe(m));
2985 EXPECT_EQ("is an object whose given property "
2986 "is not greater than or equal to 0", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00002987}
2988
2989// Tests that Property() can explain the result of matching a pointer.
2990TEST(PropertyForPointerTest, CanExplainMatchResult) {
2991 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
2992
2993 AClass a;
2994 a.set_n(1);
2995 EXPECT_EQ("", Explain(m, static_cast<const AClass*>(NULL)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002996 EXPECT_EQ("which points to an object whose given property is 1",
2997 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00002998
2999 m = Property(&AClass::n, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003000 EXPECT_EQ("which points to an object whose given property is 1, "
3001 "which is 1 more than 0", Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003002}
3003
3004// Tests ResultOf.
3005
3006// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3007// function pointer.
3008string IntToStringFunction(int input) { return input == 1 ? "foo" : "bar"; }
3009
3010TEST(ResultOfTest, WorksForFunctionPointers) {
3011 Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(string("foo")));
3012
3013 EXPECT_TRUE(matcher.Matches(1));
3014 EXPECT_FALSE(matcher.Matches(2));
3015}
3016
3017// Tests that ResultOf() can describe itself.
3018TEST(ResultOfTest, CanDescribeItself) {
3019 Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo"));
3020
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003021 EXPECT_EQ("is mapped by the given callable to a value that "
3022 "is equal to \"foo\"", Describe(matcher));
3023 EXPECT_EQ("is mapped by the given callable to a value that "
3024 "is not equal to \"foo\"", DescribeNegation(matcher));
shiqiane35fdd92008-12-10 05:08:54 +00003025}
3026
3027// Tests that ResultOf() can explain the match result.
3028int IntFunction(int input) { return input == 42 ? 80 : 90; }
3029
3030TEST(ResultOfTest, CanExplainMatchResult) {
3031 Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003032 EXPECT_EQ("which is mapped by the given callable to 90",
3033 Explain(matcher, 36));
shiqiane35fdd92008-12-10 05:08:54 +00003034
3035 matcher = ResultOf(&IntFunction, GreaterThan(85));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003036 EXPECT_EQ("which is mapped by the given callable to 90, "
3037 "which is 5 more than 85", Explain(matcher, 36));
shiqiane35fdd92008-12-10 05:08:54 +00003038}
3039
3040// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3041// returns a non-reference.
3042TEST(ResultOfTest, WorksForNonReferenceResults) {
3043 Matcher<int> matcher = ResultOf(&IntFunction, Eq(80));
3044
3045 EXPECT_TRUE(matcher.Matches(42));
3046 EXPECT_FALSE(matcher.Matches(36));
3047}
3048
3049// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3050// returns a reference to non-const.
3051double& DoubleFunction(double& input) { return input; }
3052
3053Uncopyable& RefUncopyableFunction(Uncopyable& obj) {
3054 return obj;
3055}
3056
3057TEST(ResultOfTest, WorksForReferenceToNonConstResults) {
3058 double x = 3.14;
3059 double x2 = x;
3060 Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x));
3061
3062 EXPECT_TRUE(matcher.Matches(x));
3063 EXPECT_FALSE(matcher.Matches(x2));
3064
3065 // Test that ResultOf works with uncopyable objects
3066 Uncopyable obj(0);
3067 Uncopyable obj2(0);
3068 Matcher<Uncopyable&> matcher2 =
3069 ResultOf(&RefUncopyableFunction, Ref(obj));
3070
3071 EXPECT_TRUE(matcher2.Matches(obj));
3072 EXPECT_FALSE(matcher2.Matches(obj2));
3073}
3074
3075// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3076// returns a reference to const.
3077const string& StringFunction(const string& input) { return input; }
3078
3079TEST(ResultOfTest, WorksForReferenceToConstResults) {
3080 string s = "foo";
3081 string s2 = s;
3082 Matcher<const string&> matcher = ResultOf(&StringFunction, Ref(s));
3083
3084 EXPECT_TRUE(matcher.Matches(s));
3085 EXPECT_FALSE(matcher.Matches(s2));
3086}
3087
3088// Tests that ResultOf(f, m) works when f(x) and m's
3089// argument types are compatible but different.
3090TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {
3091 // IntFunction() returns int but the inner matcher expects a signed char.
3092 Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85)));
3093
3094 EXPECT_TRUE(matcher.Matches(36));
3095 EXPECT_FALSE(matcher.Matches(42));
3096}
3097
shiqiane35fdd92008-12-10 05:08:54 +00003098// Tests that the program aborts when ResultOf is passed
3099// a NULL function pointer.
3100TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +00003101 EXPECT_DEATH_IF_SUPPORTED(
shiqiane35fdd92008-12-10 05:08:54 +00003102 ResultOf(static_cast<string(*)(int)>(NULL), Eq(string("foo"))),
3103 "NULL function pointer is passed into ResultOf\\(\\)\\.");
3104}
shiqiane35fdd92008-12-10 05:08:54 +00003105
3106// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3107// function reference.
3108TEST(ResultOfTest, WorksForFunctionReferences) {
3109 Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq("foo"));
3110 EXPECT_TRUE(matcher.Matches(1));
3111 EXPECT_FALSE(matcher.Matches(2));
3112}
3113
3114// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3115// function object.
3116struct Functor : public ::std::unary_function<int, string> {
3117 result_type operator()(argument_type input) const {
3118 return IntToStringFunction(input);
3119 }
3120};
3121
3122TEST(ResultOfTest, WorksForFunctors) {
3123 Matcher<int> matcher = ResultOf(Functor(), Eq(string("foo")));
3124
3125 EXPECT_TRUE(matcher.Matches(1));
3126 EXPECT_FALSE(matcher.Matches(2));
3127}
3128
3129// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3130// functor with more then one operator() defined. ResultOf() must work
3131// for each defined operator().
3132struct PolymorphicFunctor {
3133 typedef int result_type;
3134 int operator()(int n) { return n; }
3135 int operator()(const char* s) { return static_cast<int>(strlen(s)); }
3136};
3137
3138TEST(ResultOfTest, WorksForPolymorphicFunctors) {
3139 Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5));
3140
3141 EXPECT_TRUE(matcher_int.Matches(10));
3142 EXPECT_FALSE(matcher_int.Matches(2));
3143
3144 Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5));
3145
3146 EXPECT_TRUE(matcher_string.Matches("long string"));
3147 EXPECT_FALSE(matcher_string.Matches("shrt"));
3148}
3149
3150const int* ReferencingFunction(const int& n) { return &n; }
3151
3152struct ReferencingFunctor {
3153 typedef const int* result_type;
3154 result_type operator()(const int& n) { return &n; }
3155};
3156
3157TEST(ResultOfTest, WorksForReferencingCallables) {
3158 const int n = 1;
3159 const int n2 = 1;
3160 Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n));
3161 EXPECT_TRUE(matcher2.Matches(n));
3162 EXPECT_FALSE(matcher2.Matches(n2));
3163
3164 Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n));
3165 EXPECT_TRUE(matcher3.Matches(n));
3166 EXPECT_FALSE(matcher3.Matches(n2));
3167}
3168
shiqiane35fdd92008-12-10 05:08:54 +00003169class DivisibleByImpl {
3170 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003171 explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {}
shiqiane35fdd92008-12-10 05:08:54 +00003172
zhanyong.wandb22c222010-01-28 21:52:29 +00003173 // For testing using ExplainMatchResultTo() with polymorphic matchers.
shiqiane35fdd92008-12-10 05:08:54 +00003174 template <typename T>
zhanyong.wandb22c222010-01-28 21:52:29 +00003175 bool MatchAndExplain(const T& n, MatchResultListener* listener) const {
3176 *listener << "is " << (n % divider_) << " modulo "
3177 << divider_;
shiqiane35fdd92008-12-10 05:08:54 +00003178 return (n % divider_) == 0;
3179 }
3180
3181 void DescribeTo(::std::ostream* os) const {
3182 *os << "is divisible by " << divider_;
3183 }
3184
3185 void DescribeNegationTo(::std::ostream* os) const {
3186 *os << "is not divisible by " << divider_;
3187 }
3188
zhanyong.wan32de5f52009-12-23 00:13:23 +00003189 void set_divider(int a_divider) { divider_ = a_divider; }
shiqiane35fdd92008-12-10 05:08:54 +00003190 int divider() const { return divider_; }
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003191
shiqiane35fdd92008-12-10 05:08:54 +00003192 private:
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003193 int divider_;
shiqiane35fdd92008-12-10 05:08:54 +00003194};
3195
shiqiane35fdd92008-12-10 05:08:54 +00003196PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) {
3197 return MakePolymorphicMatcher(DivisibleByImpl(n));
3198}
3199
3200// Tests that when AllOf() fails, only the first failing matcher is
3201// asked to explain why.
3202TEST(ExplainMatchResultTest, AllOf_False_False) {
3203 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
3204 EXPECT_EQ("is 1 modulo 4", Explain(m, 5));
3205}
3206
3207// Tests that when AllOf() fails, only the first failing matcher is
3208// asked to explain why.
3209TEST(ExplainMatchResultTest, AllOf_False_True) {
3210 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
3211 EXPECT_EQ("is 2 modulo 4", Explain(m, 6));
3212}
3213
3214// Tests that when AllOf() fails, only the first failing matcher is
3215// asked to explain why.
3216TEST(ExplainMatchResultTest, AllOf_True_False) {
3217 const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));
3218 EXPECT_EQ("is 2 modulo 3", Explain(m, 5));
3219}
3220
3221// Tests that when AllOf() succeeds, all matchers are asked to explain
3222// why.
3223TEST(ExplainMatchResultTest, AllOf_True_True) {
3224 const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3));
3225 EXPECT_EQ("is 0 modulo 2; is 0 modulo 3", Explain(m, 6));
3226}
3227
3228TEST(ExplainMatchResultTest, AllOf_True_True_2) {
3229 const Matcher<int> m = AllOf(Ge(2), Le(3));
3230 EXPECT_EQ("", Explain(m, 2));
3231}
3232
3233TEST(ExplainmatcherResultTest, MonomorphicMatcher) {
3234 const Matcher<int> m = GreaterThan(5);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003235 EXPECT_EQ("which is 1 more than 5", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003236}
3237
3238// The following two tests verify that values without a public copy
3239// ctor can be used as arguments to matchers like Eq(), Ge(), and etc
3240// with the help of ByRef().
3241
3242class NotCopyable {
3243 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003244 explicit NotCopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00003245
3246 int value() const { return value_; }
3247
3248 bool operator==(const NotCopyable& rhs) const {
3249 return value() == rhs.value();
3250 }
3251
3252 bool operator>=(const NotCopyable& rhs) const {
3253 return value() >= rhs.value();
3254 }
3255 private:
3256 int value_;
3257
3258 GTEST_DISALLOW_COPY_AND_ASSIGN_(NotCopyable);
3259};
3260
3261TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) {
3262 const NotCopyable const_value1(1);
3263 const Matcher<const NotCopyable&> m = Eq(ByRef(const_value1));
3264
3265 const NotCopyable n1(1), n2(2);
3266 EXPECT_TRUE(m.Matches(n1));
3267 EXPECT_FALSE(m.Matches(n2));
3268}
3269
3270TEST(ByRefTest, AllowsNotCopyableValueInMatchers) {
3271 NotCopyable value2(2);
3272 const Matcher<NotCopyable&> m = Ge(ByRef(value2));
3273
3274 NotCopyable n1(1), n2(2);
3275 EXPECT_FALSE(m.Matches(n1));
3276 EXPECT_TRUE(m.Matches(n2));
3277}
3278
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003279#if GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003280// Tests ContainerEq with different container types, and
3281// different element types.
3282
3283template <typename T>
zhanyong.wanb8243162009-06-04 05:48:20 +00003284class ContainerEqTest : public testing::Test {};
zhanyong.wan6a896b52009-01-16 01:13:50 +00003285
3286typedef testing::Types<
3287 std::set<int>,
3288 std::vector<size_t>,
3289 std::multiset<size_t>,
3290 std::list<int> >
3291 ContainerEqTestTypes;
3292
3293TYPED_TEST_CASE(ContainerEqTest, ContainerEqTestTypes);
3294
3295// Tests that the filled container is equal to itself.
3296TYPED_TEST(ContainerEqTest, EqualsSelf) {
3297 static const int vals[] = {1, 1, 2, 3, 5, 8};
3298 TypeParam my_set(vals, vals + 6);
3299 const Matcher<TypeParam> m = ContainerEq(my_set);
3300 EXPECT_TRUE(m.Matches(my_set));
3301 EXPECT_EQ("", Explain(m, my_set));
3302}
3303
3304// Tests that missing values are reported.
3305TYPED_TEST(ContainerEqTest, ValueMissing) {
3306 static const int vals[] = {1, 1, 2, 3, 5, 8};
3307 static const int test_vals[] = {2, 1, 8, 5};
3308 TypeParam my_set(vals, vals + 6);
3309 TypeParam test_set(test_vals, test_vals + 4);
3310 const Matcher<TypeParam> m = ContainerEq(my_set);
3311 EXPECT_FALSE(m.Matches(test_set));
3312 EXPECT_EQ("Not in actual: 3", Explain(m, test_set));
3313}
3314
3315// Tests that added values are reported.
3316TYPED_TEST(ContainerEqTest, ValueAdded) {
3317 static const int vals[] = {1, 1, 2, 3, 5, 8};
3318 static const int test_vals[] = {1, 2, 3, 5, 8, 46};
3319 TypeParam my_set(vals, vals + 6);
3320 TypeParam test_set(test_vals, test_vals + 6);
3321 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3322 EXPECT_FALSE(m.Matches(test_set));
3323 EXPECT_EQ("Only in actual: 46", Explain(m, test_set));
3324}
3325
3326// Tests that added and missing values are reported together.
3327TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) {
3328 static const int vals[] = {1, 1, 2, 3, 5, 8};
3329 static const int test_vals[] = {1, 2, 3, 8, 46};
3330 TypeParam my_set(vals, vals + 6);
3331 TypeParam test_set(test_vals, test_vals + 5);
3332 const Matcher<TypeParam> m = ContainerEq(my_set);
3333 EXPECT_FALSE(m.Matches(test_set));
3334 EXPECT_EQ("Only in actual: 46; not in actual: 5", Explain(m, test_set));
3335}
3336
3337// Tests duplicated value -- expect no explanation.
3338TYPED_TEST(ContainerEqTest, DuplicateDifference) {
3339 static const int vals[] = {1, 1, 2, 3, 5, 8};
3340 static const int test_vals[] = {1, 2, 3, 5, 8};
3341 TypeParam my_set(vals, vals + 6);
3342 TypeParam test_set(test_vals, test_vals + 5);
3343 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3344 // Depending on the container, match may be true or false
3345 // But in any case there should be no explanation.
3346 EXPECT_EQ("", Explain(m, test_set));
3347}
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003348#endif // GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003349
3350// Tests that mutliple missing values are reported.
3351// Using just vector here, so order is predicatble.
3352TEST(ContainerEqExtraTest, MultipleValuesMissing) {
3353 static const int vals[] = {1, 1, 2, 3, 5, 8};
3354 static const int test_vals[] = {2, 1, 5};
3355 std::vector<int> my_set(vals, vals + 6);
3356 std::vector<int> test_set(test_vals, test_vals + 3);
3357 const Matcher<std::vector<int> > m = ContainerEq(my_set);
3358 EXPECT_FALSE(m.Matches(test_set));
3359 EXPECT_EQ("Not in actual: 3, 8", Explain(m, test_set));
3360}
3361
3362// Tests that added values are reported.
3363// Using just vector here, so order is predicatble.
3364TEST(ContainerEqExtraTest, MultipleValuesAdded) {
3365 static const int vals[] = {1, 1, 2, 3, 5, 8};
3366 static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46};
3367 std::list<size_t> my_set(vals, vals + 6);
3368 std::list<size_t> test_set(test_vals, test_vals + 7);
3369 const Matcher<const std::list<size_t>&> m = ContainerEq(my_set);
3370 EXPECT_FALSE(m.Matches(test_set));
3371 EXPECT_EQ("Only in actual: 92, 46", Explain(m, test_set));
3372}
3373
3374// Tests that added and missing values are reported together.
3375TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) {
3376 static const int vals[] = {1, 1, 2, 3, 5, 8};
3377 static const int test_vals[] = {1, 2, 3, 92, 46};
3378 std::list<size_t> my_set(vals, vals + 6);
3379 std::list<size_t> test_set(test_vals, test_vals + 5);
3380 const Matcher<const std::list<size_t> > m = ContainerEq(my_set);
3381 EXPECT_FALSE(m.Matches(test_set));
3382 EXPECT_EQ("Only in actual: 92, 46; not in actual: 5, 8",
3383 Explain(m, test_set));
3384}
3385
3386// Tests to see that duplicate elements are detected,
3387// but (as above) not reported in the explanation.
3388TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) {
3389 static const int vals[] = {1, 1, 2, 3, 5, 8};
3390 static const int test_vals[] = {1, 2, 3, 5, 8};
3391 std::vector<int> my_set(vals, vals + 6);
3392 std::vector<int> test_set(test_vals, test_vals + 5);
3393 const Matcher<std::vector<int> > m = ContainerEq(my_set);
3394 EXPECT_TRUE(m.Matches(my_set));
3395 EXPECT_FALSE(m.Matches(test_set));
3396 // There is nothing to report when both sets contain all the same values.
3397 EXPECT_EQ("", Explain(m, test_set));
3398}
3399
3400// Tests that ContainerEq works for non-trivial associative containers,
3401// like maps.
3402TEST(ContainerEqExtraTest, WorksForMaps) {
3403 std::map<int, std::string> my_map;
3404 my_map[0] = "a";
3405 my_map[1] = "b";
3406
3407 std::map<int, std::string> test_map;
3408 test_map[0] = "aa";
3409 test_map[1] = "b";
3410
3411 const Matcher<const std::map<int, std::string>&> m = ContainerEq(my_map);
3412 EXPECT_TRUE(m.Matches(my_map));
3413 EXPECT_FALSE(m.Matches(test_map));
3414
3415 EXPECT_EQ("Only in actual: (0, \"aa\"); not in actual: (0, \"a\")",
3416 Explain(m, test_map));
3417}
3418
zhanyong.wanb8243162009-06-04 05:48:20 +00003419TEST(ContainerEqExtraTest, WorksForNativeArray) {
3420 int a1[] = { 1, 2, 3 };
3421 int a2[] = { 1, 2, 3 };
3422 int b[] = { 1, 2, 4 };
3423
3424 EXPECT_THAT(a1, ContainerEq(a2));
3425 EXPECT_THAT(a1, Not(ContainerEq(b)));
3426}
3427
3428TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) {
3429 const char a1[][3] = { "hi", "lo" };
3430 const char a2[][3] = { "hi", "lo" };
3431 const char b[][3] = { "lo", "hi" };
3432
3433 // Tests using ContainerEq() in the first dimension.
3434 EXPECT_THAT(a1, ContainerEq(a2));
3435 EXPECT_THAT(a1, Not(ContainerEq(b)));
3436
3437 // Tests using ContainerEq() in the second dimension.
3438 EXPECT_THAT(a1, ElementsAre(ContainerEq(a2[0]), ContainerEq(a2[1])));
3439 EXPECT_THAT(a1, ElementsAre(Not(ContainerEq(b[0])), ContainerEq(a2[1])));
3440}
3441
3442TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) {
3443 const int a1[] = { 1, 2, 3 };
3444 const int a2[] = { 1, 2, 3 };
3445 const int b[] = { 1, 2, 3, 4 };
3446
zhanyong.wan2661c682009-06-09 05:42:12 +00003447 const int* const p1 = a1;
3448 EXPECT_THAT(make_tuple(p1, 3), ContainerEq(a2));
3449 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(b)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003450
3451 const int c[] = { 1, 3, 2 };
zhanyong.wan2661c682009-06-09 05:42:12 +00003452 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(c)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003453}
3454
3455TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) {
3456 std::string a1[][3] = {
3457 { "hi", "hello", "ciao" },
3458 { "bye", "see you", "ciao" }
3459 };
3460
3461 std::string a2[][3] = {
3462 { "hi", "hello", "ciao" },
3463 { "bye", "see you", "ciao" }
3464 };
3465
3466 const Matcher<const std::string(&)[2][3]> m = ContainerEq(a2);
3467 EXPECT_THAT(a1, m);
3468
3469 a2[0][0] = "ha";
3470 EXPECT_THAT(a1, m);
3471}
3472
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003473// Tests GetParamIndex().
3474
3475TEST(GetParamIndexTest, WorksForEmptyParamList) {
3476 const char* params[] = { NULL };
3477 EXPECT_EQ(kTupleInterpolation, GetParamIndex(params, "*"));
3478 EXPECT_EQ(kInvalidInterpolation, GetParamIndex(params, "a"));
3479}
3480
3481TEST(GetParamIndexTest, RecognizesStar) {
3482 const char* params[] = { "a", "b", NULL };
3483 EXPECT_EQ(kTupleInterpolation, GetParamIndex(params, "*"));
3484}
3485
3486TEST(GetParamIndexTest, RecognizesKnownParam) {
3487 const char* params[] = { "foo", "bar", NULL };
3488 EXPECT_EQ(0, GetParamIndex(params, "foo"));
3489 EXPECT_EQ(1, GetParamIndex(params, "bar"));
3490}
3491
3492TEST(GetParamIndexTest, RejectsUnknownParam) {
3493 const char* params[] = { "foo", "bar", NULL };
3494 EXPECT_EQ(kInvalidInterpolation, GetParamIndex(params, "foobar"));
3495}
3496
3497// Tests SkipPrefix().
3498
3499TEST(SkipPrefixTest, SkipsWhenPrefixMatches) {
3500 const char* const str = "hello";
3501
3502 const char* p = str;
3503 EXPECT_TRUE(SkipPrefix("", &p));
3504 EXPECT_EQ(str, p);
3505
3506 p = str;
3507 EXPECT_TRUE(SkipPrefix("hell", &p));
3508 EXPECT_EQ(str + 4, p);
3509}
3510
3511TEST(SkipPrefixTest, DoesNotSkipWhenPrefixDoesNotMatch) {
3512 const char* const str = "world";
3513
3514 const char* p = str;
3515 EXPECT_FALSE(SkipPrefix("W", &p));
3516 EXPECT_EQ(str, p);
3517
3518 p = str;
3519 EXPECT_FALSE(SkipPrefix("world!", &p));
3520 EXPECT_EQ(str, p);
3521}
3522
3523// Tests FormatMatcherDescriptionSyntaxError().
3524TEST(FormatMatcherDescriptionSyntaxErrorTest, FormatsCorrectly) {
3525 const char* const description = "hello%world";
3526 EXPECT_EQ("Syntax error at index 5 in matcher description \"hello%world\": ",
3527 FormatMatcherDescriptionSyntaxError(description, description + 5));
3528}
3529
3530// Tests ValidateMatcherDescription().
3531
3532TEST(ValidateMatcherDescriptionTest, AcceptsEmptyDescription) {
3533 const char* params[] = { "foo", "bar", NULL };
3534 EXPECT_THAT(ValidateMatcherDescription(params, ""),
3535 ElementsAre());
3536}
3537
3538TEST(ValidateMatcherDescriptionTest,
3539 AcceptsNonEmptyDescriptionWithNoInterpolation) {
3540 const char* params[] = { "foo", "bar", NULL };
3541 EXPECT_THAT(ValidateMatcherDescription(params, "a simple description"),
3542 ElementsAre());
3543}
3544
zhanyong.wan82113312010-01-08 21:55:40 +00003545// The MATCHER*() macros trigger warning C4100 (unreferenced formal
3546// parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
3547// the macro definition, as the warnings are generated when the macro
3548// is expanded and macro expansion cannot contain #pragma. Therefore
3549// we suppress them here.
3550#ifdef _MSC_VER
3551#pragma warning(push)
3552#pragma warning(disable:4100)
3553#endif
3554
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003555// We use MATCHER_P3() to define a matcher for testing
3556// ValidateMatcherDescription(); otherwise we'll end up with much
3557// plumbing code. This is not circular as
3558// ValidateMatcherDescription() doesn't affect whether the matcher
3559// matches a value or not.
3560MATCHER_P3(EqInterpolation, start, end, index, "equals Interpolation%(*)s") {
3561 return arg.start_pos == start && arg.end_pos == end &&
3562 arg.param_index == index;
3563}
3564
zhanyong.wan82113312010-01-08 21:55:40 +00003565#ifdef _MSC_VER
3566#pragma warning(pop)
3567#endif
3568
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003569TEST(ValidateMatcherDescriptionTest, AcceptsPercentInterpolation) {
3570 const char* params[] = { "foo", NULL };
3571 const char* const desc = "one %%";
3572 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3573 ElementsAre(EqInterpolation(desc + 4, desc + 6,
3574 kPercentInterpolation)));
3575}
3576
3577TEST(ValidateMatcherDescriptionTest, AcceptsTupleInterpolation) {
3578 const char* params[] = { "foo", "bar", "baz", NULL };
3579 const char* const desc = "%(*)s after";
3580 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3581 ElementsAre(EqInterpolation(desc, desc + 5,
3582 kTupleInterpolation)));
3583}
3584
3585TEST(ValidateMatcherDescriptionTest, AcceptsParamInterpolation) {
3586 const char* params[] = { "foo", "bar", "baz", NULL };
3587 const char* const desc = "a %(bar)s.";
3588 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3589 ElementsAre(EqInterpolation(desc + 2, desc + 9, 1)));
3590}
3591
3592TEST(ValidateMatcherDescriptionTest, AcceptsMultiplenterpolations) {
3593 const char* params[] = { "foo", "bar", "baz", NULL };
3594 const char* const desc = "%(baz)s %(foo)s %(bar)s";
3595 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3596 ElementsAre(EqInterpolation(desc, desc + 7, 2),
3597 EqInterpolation(desc + 8, desc + 15, 0),
3598 EqInterpolation(desc + 16, desc + 23, 1)));
3599}
3600
3601TEST(ValidateMatcherDescriptionTest, AcceptsRepeatedParams) {
3602 const char* params[] = { "foo", "bar", NULL };
3603 const char* const desc = "%(foo)s and %(foo)s";
3604 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3605 ElementsAre(EqInterpolation(desc, desc + 7, 0),
3606 EqInterpolation(desc + 12, desc + 19, 0)));
3607}
3608
3609TEST(ValidateMatcherDescriptionTest, RejectsUnknownParam) {
3610 const char* params[] = { "a", "bar", NULL };
3611 EXPECT_NONFATAL_FAILURE({
3612 EXPECT_THAT(ValidateMatcherDescription(params, "%(foo)s"),
3613 ElementsAre());
3614 }, "Syntax error at index 2 in matcher description \"%(foo)s\": "
3615 "\"foo\" is an invalid parameter name.");
3616}
3617
3618TEST(ValidateMatcherDescriptionTest, RejectsUnfinishedParam) {
3619 const char* params[] = { "a", "bar", NULL };
3620 EXPECT_NONFATAL_FAILURE({
3621 EXPECT_THAT(ValidateMatcherDescription(params, "%(foo)"),
3622 ElementsAre());
3623 }, "Syntax error at index 0 in matcher description \"%(foo)\": "
3624 "an interpolation must end with \")s\", but \"%(foo)\" does not.");
3625
3626 EXPECT_NONFATAL_FAILURE({
3627 EXPECT_THAT(ValidateMatcherDescription(params, "x%(a"),
3628 ElementsAre());
3629 }, "Syntax error at index 1 in matcher description \"x%(a\": "
3630 "an interpolation must end with \")s\", but \"%(a\" does not.");
3631}
3632
3633TEST(ValidateMatcherDescriptionTest, RejectsSinglePercent) {
3634 const char* params[] = { "a", NULL };
3635 EXPECT_NONFATAL_FAILURE({
3636 EXPECT_THAT(ValidateMatcherDescription(params, "a %."),
3637 ElementsAre());
3638 }, "Syntax error at index 2 in matcher description \"a %.\": "
3639 "use \"%%\" instead of \"%\" to print \"%\".");
3640
3641}
3642
3643// Tests JoinAsTuple().
3644
3645TEST(JoinAsTupleTest, JoinsEmptyTuple) {
3646 EXPECT_EQ("", JoinAsTuple(Strings()));
3647}
3648
3649TEST(JoinAsTupleTest, JoinsOneTuple) {
3650 const char* fields[] = { "1" };
3651 EXPECT_EQ("1", JoinAsTuple(Strings(fields, fields + 1)));
3652}
3653
3654TEST(JoinAsTupleTest, JoinsTwoTuple) {
3655 const char* fields[] = { "1", "a" };
3656 EXPECT_EQ("(1, a)", JoinAsTuple(Strings(fields, fields + 2)));
3657}
3658
3659TEST(JoinAsTupleTest, JoinsTenTuple) {
3660 const char* fields[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
3661 EXPECT_EQ("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)",
3662 JoinAsTuple(Strings(fields, fields + 10)));
3663}
3664
3665// Tests FormatMatcherDescription().
3666
3667TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) {
3668 EXPECT_EQ("is even",
3669 FormatMatcherDescription("IsEven", "", Interpolations(),
3670 Strings()));
3671
3672 const char* params[] = { "5" };
3673 EXPECT_EQ("equals 5",
3674 FormatMatcherDescription("Equals", "", Interpolations(),
3675 Strings(params, params + 1)));
3676
3677 const char* params2[] = { "5", "8" };
3678 EXPECT_EQ("is in range (5, 8)",
3679 FormatMatcherDescription("IsInRange", "", Interpolations(),
3680 Strings(params2, params2 + 2)));
3681}
3682
3683TEST(FormatMatcherDescriptionTest, WorksForDescriptionWithNoInterpolation) {
3684 EXPECT_EQ("is positive",
3685 FormatMatcherDescription("Gt0", "is positive", Interpolations(),
3686 Strings()));
3687
3688 const char* params[] = { "5", "6" };
3689 EXPECT_EQ("is negative",
3690 FormatMatcherDescription("Lt0", "is negative", Interpolations(),
3691 Strings(params, params + 2)));
3692}
3693
3694TEST(FormatMatcherDescriptionTest,
3695 WorksWhenDescriptionStartsWithInterpolation) {
3696 const char* params[] = { "5" };
3697 const char* const desc = "%(num)s times bigger";
3698 const Interpolation interp[] = { Interpolation(desc, desc + 7, 0) };
3699 EXPECT_EQ("5 times bigger",
3700 FormatMatcherDescription("Foo", desc,
3701 Interpolations(interp, interp + 1),
3702 Strings(params, params + 1)));
3703}
3704
3705TEST(FormatMatcherDescriptionTest,
3706 WorksWhenDescriptionEndsWithInterpolation) {
3707 const char* params[] = { "5", "6" };
3708 const char* const desc = "is bigger than %(y)s";
3709 const Interpolation interp[] = { Interpolation(desc + 15, desc + 20, 1) };
3710 EXPECT_EQ("is bigger than 6",
3711 FormatMatcherDescription("Foo", desc,
3712 Interpolations(interp, interp + 1),
3713 Strings(params, params + 2)));
3714}
3715
3716TEST(FormatMatcherDescriptionTest,
3717 WorksWhenDescriptionStartsAndEndsWithInterpolation) {
3718 const char* params[] = { "5", "6" };
3719 const char* const desc = "%(x)s <= arg <= %(y)s";
3720 const Interpolation interp[] = {
3721 Interpolation(desc, desc + 5, 0),
3722 Interpolation(desc + 16, desc + 21, 1)
3723 };
3724 EXPECT_EQ("5 <= arg <= 6",
3725 FormatMatcherDescription("Foo", desc,
3726 Interpolations(interp, interp + 2),
3727 Strings(params, params + 2)));
3728}
3729
3730TEST(FormatMatcherDescriptionTest,
3731 WorksWhenDescriptionDoesNotStartOrEndWithInterpolation) {
3732 const char* params[] = { "5.2" };
3733 const char* const desc = "has %(x)s cents";
3734 const Interpolation interp[] = { Interpolation(desc + 4, desc + 9, 0) };
3735 EXPECT_EQ("has 5.2 cents",
3736 FormatMatcherDescription("Foo", desc,
3737 Interpolations(interp, interp + 1),
3738 Strings(params, params + 1)));
3739}
3740
3741TEST(FormatMatcherDescriptionTest,
3742 WorksWhenDescriptionContainsMultipleInterpolations) {
3743 const char* params[] = { "5", "6" };
3744 const char* const desc = "in %(*)s or [%(x)s, %(y)s]";
3745 const Interpolation interp[] = {
3746 Interpolation(desc + 3, desc + 8, kTupleInterpolation),
3747 Interpolation(desc + 13, desc + 18, 0),
3748 Interpolation(desc + 20, desc + 25, 1)
3749 };
3750 EXPECT_EQ("in (5, 6) or [5, 6]",
3751 FormatMatcherDescription("Foo", desc,
3752 Interpolations(interp, interp + 3),
3753 Strings(params, params + 2)));
3754}
3755
3756TEST(FormatMatcherDescriptionTest,
3757 WorksWhenDescriptionContainsRepeatedParams) {
3758 const char* params[] = { "9" };
3759 const char* const desc = "in [-%(x)s, %(x)s]";
3760 const Interpolation interp[] = {
3761 Interpolation(desc + 5, desc + 10, 0),
3762 Interpolation(desc + 12, desc + 17, 0)
3763 };
3764 EXPECT_EQ("in [-9, 9]",
3765 FormatMatcherDescription("Foo", desc,
3766 Interpolations(interp, interp + 2),
3767 Strings(params, params + 1)));
3768}
3769
3770TEST(FormatMatcherDescriptionTest,
3771 WorksForDescriptionWithInvalidInterpolation) {
3772 const char* params[] = { "9" };
3773 const char* const desc = "> %(x)s %(x)";
3774 const Interpolation interp[] = { Interpolation(desc + 2, desc + 7, 0) };
3775 EXPECT_EQ("> 9 %(x)",
3776 FormatMatcherDescription("Foo", desc,
3777 Interpolations(interp, interp + 1),
3778 Strings(params, params + 1)));
3779}
3780
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003781// Tests PolymorphicMatcher::mutable_impl().
3782TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {
3783 PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
3784 DivisibleByImpl& impl = m.mutable_impl();
3785 EXPECT_EQ(42, impl.divider());
3786
3787 impl.set_divider(0);
3788 EXPECT_EQ(0, m.mutable_impl().divider());
3789}
3790
3791// Tests PolymorphicMatcher::impl().
3792TEST(PolymorphicMatcherTest, CanAccessImpl) {
3793 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
3794 const DivisibleByImpl& impl = m.impl();
3795 EXPECT_EQ(42, impl.divider());
3796}
3797
shiqiane35fdd92008-12-10 05:08:54 +00003798} // namespace gmock_matchers_test
3799} // namespace testing