blob: 6d784f16cb97783359b77df080a0d9459ef5c59d [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.wanb1c7f932010-03-24 17:35:11 +000064using std::make_pair;
zhanyong.wanb5937da2009-07-16 20:26:41 +000065using std::map;
66using std::multimap;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000067using std::pair;
shiqiane35fdd92008-12-10 05:08:54 +000068using std::stringstream;
zhanyong.wanb8243162009-06-04 05:48:20 +000069using std::tr1::make_tuple;
shiqiane35fdd92008-12-10 05:08:54 +000070using testing::A;
zhanyong.wanbf550852009-06-09 06:09:53 +000071using testing::AllArgs;
shiqiane35fdd92008-12-10 05:08:54 +000072using testing::AllOf;
73using testing::An;
74using testing::AnyOf;
75using testing::ByRef;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000076using testing::ContainsRegex;
shiqiane35fdd92008-12-10 05:08:54 +000077using testing::DoubleEq;
78using testing::EndsWith;
79using testing::Eq;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000080using testing::ExplainMatchResult;
shiqiane35fdd92008-12-10 05:08:54 +000081using testing::Field;
82using testing::FloatEq;
83using testing::Ge;
84using testing::Gt;
85using testing::HasSubstr;
zhanyong.wan2d970ee2009-09-24 21:41:36 +000086using testing::IsNull;
zhanyong.wanb5937da2009-07-16 20:26:41 +000087using testing::Key;
shiqiane35fdd92008-12-10 05:08:54 +000088using testing::Le;
89using testing::Lt;
90using testing::MakeMatcher;
91using testing::MakePolymorphicMatcher;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000092using testing::MatchResultListener;
shiqiane35fdd92008-12-10 05:08:54 +000093using testing::Matcher;
94using testing::MatcherCast;
95using testing::MatcherInterface;
96using testing::Matches;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000097using testing::MatchesRegex;
shiqiane35fdd92008-12-10 05:08:54 +000098using testing::NanSensitiveDoubleEq;
99using testing::NanSensitiveFloatEq;
100using testing::Ne;
101using testing::Not;
102using testing::NotNull;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000103using testing::Pair;
shiqiane35fdd92008-12-10 05:08:54 +0000104using testing::Pointee;
105using testing::PolymorphicMatcher;
106using testing::Property;
107using testing::Ref;
108using testing::ResultOf;
109using testing::StartsWith;
110using testing::StrCaseEq;
111using testing::StrCaseNe;
112using testing::StrEq;
113using testing::StrNe;
114using testing::Truly;
115using testing::TypedEq;
zhanyong.wanb8243162009-06-04 05:48:20 +0000116using testing::Value;
shiqiane35fdd92008-12-10 05:08:54 +0000117using testing::_;
zhanyong.wana862f1d2010-03-15 21:23:04 +0000118using testing::internal::DummyMatchResultListener;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000119using testing::internal::ExplainMatchFailureTupleTo;
shiqiane35fdd92008-12-10 05:08:54 +0000120using testing::internal::FloatingEqMatcher;
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000121using testing::internal::FormatMatcherDescriptionSyntaxError;
122using testing::internal::GetParamIndex;
123using testing::internal::Interpolation;
124using testing::internal::Interpolations;
125using testing::internal::JoinAsTuple;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000126using testing::internal::RE;
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000127using testing::internal::SkipPrefix;
zhanyong.wana862f1d2010-03-15 21:23:04 +0000128using testing::internal::StreamMatchResultListener;
shiqiane35fdd92008-12-10 05:08:54 +0000129using testing::internal::String;
zhanyong.wan34b034c2010-03-05 21:23:23 +0000130using testing::internal::StringMatchResultListener;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000131using testing::internal::Strings;
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000132using testing::internal::ValidateMatcherDescription;
133using testing::internal::kInvalidInterpolation;
134using testing::internal::kPercentInterpolation;
135using testing::internal::kTupleInterpolation;
vladlosev79b83502009-11-18 00:43:37 +0000136using testing::internal::linked_ptr;
vladloseve56daa72009-11-18 01:08:08 +0000137using testing::internal::scoped_ptr;
shiqiane35fdd92008-12-10 05:08:54 +0000138using testing::internal::string;
139
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000140// For testing ExplainMatchResultTo().
141class GreaterThanMatcher : public MatcherInterface<int> {
142 public:
143 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
144
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000145 virtual void DescribeTo(::std::ostream* os) const {
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000146 *os << "is > " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000147 }
148
zhanyong.wandb22c222010-01-28 21:52:29 +0000149 virtual bool MatchAndExplain(int lhs,
150 MatchResultListener* listener) const {
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000151 const int diff = lhs - rhs_;
152 if (diff > 0) {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000153 *listener << "which is " << diff << " more than " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000154 } else if (diff == 0) {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000155 *listener << "which is the same as " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000156 } else {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000157 *listener << "which is " << -diff << " less than " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000158 }
zhanyong.wandb22c222010-01-28 21:52:29 +0000159
160 return lhs > rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000161 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000162
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000163 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000164 int rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000165};
166
167Matcher<int> GreaterThan(int n) {
168 return MakeMatcher(new GreaterThanMatcher(n));
169}
170
shiqiane35fdd92008-12-10 05:08:54 +0000171// Returns the description of the given matcher.
172template <typename T>
173string Describe(const Matcher<T>& m) {
174 stringstream ss;
175 m.DescribeTo(&ss);
176 return ss.str();
177}
178
179// Returns the description of the negation of the given matcher.
180template <typename T>
181string DescribeNegation(const Matcher<T>& m) {
182 stringstream ss;
183 m.DescribeNegationTo(&ss);
184 return ss.str();
185}
186
187// Returns the reason why x matches, or doesn't match, m.
188template <typename MatcherType, typename Value>
189string Explain(const MatcherType& m, const Value& x) {
190 stringstream ss;
191 m.ExplainMatchResultTo(x, &ss);
192 return ss.str();
193}
194
zhanyong.wana862f1d2010-03-15 21:23:04 +0000195TEST(MatchResultListenerTest, StreamingWorks) {
196 StringMatchResultListener listener;
197 listener << "hi" << 5;
198 EXPECT_EQ("hi5", listener.str());
199
200 // Streaming shouldn't crash when the underlying ostream is NULL.
201 DummyMatchResultListener dummy;
202 dummy << "hi" << 5;
203}
204
205TEST(MatchResultListenerTest, CanAccessUnderlyingStream) {
206 EXPECT_TRUE(DummyMatchResultListener().stream() == NULL);
207 EXPECT_TRUE(StreamMatchResultListener(NULL).stream() == NULL);
208
209 EXPECT_EQ(&std::cout, StreamMatchResultListener(&std::cout).stream());
210}
211
212TEST(MatchResultListenerTest, IsInterestedWorks) {
213 EXPECT_TRUE(StringMatchResultListener().IsInterested());
214 EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested());
215
216 EXPECT_FALSE(DummyMatchResultListener().IsInterested());
217 EXPECT_FALSE(StreamMatchResultListener(NULL).IsInterested());
218}
219
shiqiane35fdd92008-12-10 05:08:54 +0000220// Makes sure that the MatcherInterface<T> interface doesn't
221// change.
222class EvenMatcherImpl : public MatcherInterface<int> {
223 public:
zhanyong.wandb22c222010-01-28 21:52:29 +0000224 virtual bool MatchAndExplain(int x,
225 MatchResultListener* /* listener */) const {
226 return x % 2 == 0;
227 }
shiqiane35fdd92008-12-10 05:08:54 +0000228
229 virtual void DescribeTo(::std::ostream* os) const {
230 *os << "is an even number";
231 }
232
233 // We deliberately don't define DescribeNegationTo() and
234 // ExplainMatchResultTo() here, to make sure the definition of these
235 // two methods is optional.
236};
237
zhanyong.wana862f1d2010-03-15 21:23:04 +0000238// Makes sure that the MatcherInterface API doesn't change.
239TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) {
shiqiane35fdd92008-12-10 05:08:54 +0000240 EvenMatcherImpl m;
241}
242
zhanyong.wan82113312010-01-08 21:55:40 +0000243// Tests implementing a monomorphic matcher using MatchAndExplain().
244
245class NewEvenMatcherImpl : public MatcherInterface<int> {
246 public:
247 virtual bool MatchAndExplain(int x, MatchResultListener* listener) const {
248 const bool match = x % 2 == 0;
249 // Verifies that we can stream to a listener directly.
250 *listener << "value % " << 2;
251 if (listener->stream() != NULL) {
252 // Verifies that we can stream to a listener's underlying stream
253 // too.
254 *listener->stream() << " == " << (x % 2);
255 }
256 return match;
257 }
258
259 virtual void DescribeTo(::std::ostream* os) const {
260 *os << "is an even number";
261 }
262};
263
264TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {
265 Matcher<int> m = MakeMatcher(new NewEvenMatcherImpl);
266 EXPECT_TRUE(m.Matches(2));
267 EXPECT_FALSE(m.Matches(3));
268 EXPECT_EQ("value % 2 == 0", Explain(m, 2));
269 EXPECT_EQ("value % 2 == 1", Explain(m, 3));
270}
271
shiqiane35fdd92008-12-10 05:08:54 +0000272// Tests default-constructing a matcher.
273TEST(MatcherTest, CanBeDefaultConstructed) {
274 Matcher<double> m;
275}
276
277// Tests that Matcher<T> can be constructed from a MatcherInterface<T>*.
278TEST(MatcherTest, CanBeConstructedFromMatcherInterface) {
279 const MatcherInterface<int>* impl = new EvenMatcherImpl;
280 Matcher<int> m(impl);
281 EXPECT_TRUE(m.Matches(4));
282 EXPECT_FALSE(m.Matches(5));
283}
284
285// Tests that value can be used in place of Eq(value).
286TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) {
287 Matcher<int> m1 = 5;
288 EXPECT_TRUE(m1.Matches(5));
289 EXPECT_FALSE(m1.Matches(6));
290}
291
292// Tests that NULL can be used in place of Eq(NULL).
293TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) {
294 Matcher<int*> m1 = NULL;
295 EXPECT_TRUE(m1.Matches(NULL));
296 int n = 0;
297 EXPECT_FALSE(m1.Matches(&n));
298}
299
300// Tests that matchers are copyable.
301TEST(MatcherTest, IsCopyable) {
302 // Tests the copy constructor.
303 Matcher<bool> m1 = Eq(false);
304 EXPECT_TRUE(m1.Matches(false));
305 EXPECT_FALSE(m1.Matches(true));
306
307 // Tests the assignment operator.
308 m1 = Eq(true);
309 EXPECT_TRUE(m1.Matches(true));
310 EXPECT_FALSE(m1.Matches(false));
311}
312
313// Tests that Matcher<T>::DescribeTo() calls
314// MatcherInterface<T>::DescribeTo().
315TEST(MatcherTest, CanDescribeItself) {
316 EXPECT_EQ("is an even number",
317 Describe(Matcher<int>(new EvenMatcherImpl)));
318}
319
zhanyong.wan82113312010-01-08 21:55:40 +0000320// Tests Matcher<T>::MatchAndExplain().
321TEST(MatcherTest, MatchAndExplain) {
322 Matcher<int> m = GreaterThan(0);
zhanyong.wan34b034c2010-03-05 21:23:23 +0000323 StringMatchResultListener listener1;
zhanyong.wan82113312010-01-08 21:55:40 +0000324 EXPECT_TRUE(m.MatchAndExplain(42, &listener1));
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000325 EXPECT_EQ("which is 42 more than 0", listener1.str());
zhanyong.wan82113312010-01-08 21:55:40 +0000326
zhanyong.wan34b034c2010-03-05 21:23:23 +0000327 StringMatchResultListener listener2;
zhanyong.wan82113312010-01-08 21:55:40 +0000328 EXPECT_FALSE(m.MatchAndExplain(-9, &listener2));
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000329 EXPECT_EQ("which is 9 less than 0", listener2.str());
zhanyong.wan82113312010-01-08 21:55:40 +0000330}
331
shiqiane35fdd92008-12-10 05:08:54 +0000332// Tests that a C-string literal can be implicitly converted to a
333// Matcher<string> or Matcher<const string&>.
334TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
335 Matcher<string> m1 = "hi";
336 EXPECT_TRUE(m1.Matches("hi"));
337 EXPECT_FALSE(m1.Matches("hello"));
338
339 Matcher<const string&> m2 = "hi";
340 EXPECT_TRUE(m2.Matches("hi"));
341 EXPECT_FALSE(m2.Matches("hello"));
342}
343
344// Tests that a string object can be implicitly converted to a
345// Matcher<string> or Matcher<const string&>.
346TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {
347 Matcher<string> m1 = string("hi");
348 EXPECT_TRUE(m1.Matches("hi"));
349 EXPECT_FALSE(m1.Matches("hello"));
350
351 Matcher<const string&> m2 = string("hi");
352 EXPECT_TRUE(m2.Matches("hi"));
353 EXPECT_FALSE(m2.Matches("hello"));
354}
355
356// Tests that MakeMatcher() constructs a Matcher<T> from a
357// MatcherInterface* without requiring the user to explicitly
358// write the type.
359TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) {
360 const MatcherInterface<int>* dummy_impl = NULL;
361 Matcher<int> m = MakeMatcher(dummy_impl);
362}
363
zhanyong.wan82113312010-01-08 21:55:40 +0000364// Tests that MakePolymorphicMatcher() can construct a polymorphic
365// matcher from its implementation using the old API.
shiqiane35fdd92008-12-10 05:08:54 +0000366const int bar = 1;
367class ReferencesBarOrIsZeroImpl {
368 public:
369 template <typename T>
zhanyong.wandb22c222010-01-28 21:52:29 +0000370 bool MatchAndExplain(const T& x,
371 MatchResultListener* /* listener */) const {
shiqiane35fdd92008-12-10 05:08:54 +0000372 const void* p = &x;
373 return p == &bar || x == 0;
374 }
375
376 void DescribeTo(::std::ostream* os) const { *os << "bar or zero"; }
377
378 void DescribeNegationTo(::std::ostream* os) const {
379 *os << "doesn't reference bar and is not zero";
380 }
381};
382
383// This function verifies that MakePolymorphicMatcher() returns a
384// PolymorphicMatcher<T> where T is the argument's type.
385PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() {
386 return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl());
387}
388
zhanyong.wan82113312010-01-08 21:55:40 +0000389TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) {
shiqiane35fdd92008-12-10 05:08:54 +0000390 // Using a polymorphic matcher to match a reference type.
391 Matcher<const int&> m1 = ReferencesBarOrIsZero();
392 EXPECT_TRUE(m1.Matches(0));
393 // Verifies that the identity of a by-reference argument is preserved.
394 EXPECT_TRUE(m1.Matches(bar));
395 EXPECT_FALSE(m1.Matches(1));
396 EXPECT_EQ("bar or zero", Describe(m1));
397
398 // Using a polymorphic matcher to match a value type.
399 Matcher<double> m2 = ReferencesBarOrIsZero();
400 EXPECT_TRUE(m2.Matches(0.0));
401 EXPECT_FALSE(m2.Matches(0.1));
402 EXPECT_EQ("bar or zero", Describe(m2));
403}
404
zhanyong.wan82113312010-01-08 21:55:40 +0000405// Tests implementing a polymorphic matcher using MatchAndExplain().
406
407class PolymorphicIsEvenImpl {
408 public:
409 void DescribeTo(::std::ostream* os) const { *os << "is even"; }
410
411 void DescribeNegationTo(::std::ostream* os) const {
412 *os << "is odd";
413 }
zhanyong.wan82113312010-01-08 21:55:40 +0000414
zhanyong.wandb22c222010-01-28 21:52:29 +0000415 template <typename T>
416 bool MatchAndExplain(const T& x, MatchResultListener* listener) const {
417 // Verifies that we can stream to the listener directly.
418 *listener << "% " << 2;
419 if (listener->stream() != NULL) {
420 // Verifies that we can stream to the listener's underlying stream
421 // too.
422 *listener->stream() << " == " << (x % 2);
423 }
424 return (x % 2) == 0;
zhanyong.wan82113312010-01-08 21:55:40 +0000425 }
zhanyong.wandb22c222010-01-28 21:52:29 +0000426};
zhanyong.wan82113312010-01-08 21:55:40 +0000427
428PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() {
429 return MakePolymorphicMatcher(PolymorphicIsEvenImpl());
430}
431
432TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {
433 // Using PolymorphicIsEven() as a Matcher<int>.
434 const Matcher<int> m1 = PolymorphicIsEven();
435 EXPECT_TRUE(m1.Matches(42));
436 EXPECT_FALSE(m1.Matches(43));
437 EXPECT_EQ("is even", Describe(m1));
438
439 const Matcher<int> not_m1 = Not(m1);
440 EXPECT_EQ("is odd", Describe(not_m1));
441
442 EXPECT_EQ("% 2 == 0", Explain(m1, 42));
443
444 // Using PolymorphicIsEven() as a Matcher<char>.
445 const Matcher<char> m2 = PolymorphicIsEven();
446 EXPECT_TRUE(m2.Matches('\x42'));
447 EXPECT_FALSE(m2.Matches('\x43'));
448 EXPECT_EQ("is even", Describe(m2));
449
450 const Matcher<char> not_m2 = Not(m2);
451 EXPECT_EQ("is odd", Describe(not_m2));
452
453 EXPECT_EQ("% 2 == 0", Explain(m2, '\x42'));
454}
455
shiqiane35fdd92008-12-10 05:08:54 +0000456// Tests that MatcherCast<T>(m) works when m is a polymorphic matcher.
457TEST(MatcherCastTest, FromPolymorphicMatcher) {
458 Matcher<int> m = MatcherCast<int>(Eq(5));
459 EXPECT_TRUE(m.Matches(5));
460 EXPECT_FALSE(m.Matches(6));
461}
462
463// For testing casting matchers between compatible types.
464class IntValue {
465 public:
466 // An int can be statically (although not implicitly) cast to a
467 // IntValue.
zhanyong.wan32de5f52009-12-23 00:13:23 +0000468 explicit IntValue(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +0000469
470 int value() const { return value_; }
471 private:
472 int value_;
473};
474
475// For testing casting matchers between compatible types.
476bool IsPositiveIntValue(const IntValue& foo) {
477 return foo.value() > 0;
478}
479
480// Tests that MatcherCast<T>(m) works when m is a Matcher<U> where T
481// can be statically converted to U.
482TEST(MatcherCastTest, FromCompatibleType) {
483 Matcher<double> m1 = Eq(2.0);
484 Matcher<int> m2 = MatcherCast<int>(m1);
485 EXPECT_TRUE(m2.Matches(2));
486 EXPECT_FALSE(m2.Matches(3));
487
488 Matcher<IntValue> m3 = Truly(IsPositiveIntValue);
489 Matcher<int> m4 = MatcherCast<int>(m3);
490 // In the following, the arguments 1 and 0 are statically converted
491 // to IntValue objects, and then tested by the IsPositiveIntValue()
492 // predicate.
493 EXPECT_TRUE(m4.Matches(1));
494 EXPECT_FALSE(m4.Matches(0));
495}
496
497// Tests that MatcherCast<T>(m) works when m is a Matcher<const T&>.
498TEST(MatcherCastTest, FromConstReferenceToNonReference) {
499 Matcher<const int&> m1 = Eq(0);
500 Matcher<int> m2 = MatcherCast<int>(m1);
501 EXPECT_TRUE(m2.Matches(0));
502 EXPECT_FALSE(m2.Matches(1));
503}
504
505// Tests that MatcherCast<T>(m) works when m is a Matcher<T&>.
506TEST(MatcherCastTest, FromReferenceToNonReference) {
507 Matcher<int&> m1 = Eq(0);
508 Matcher<int> m2 = MatcherCast<int>(m1);
509 EXPECT_TRUE(m2.Matches(0));
510 EXPECT_FALSE(m2.Matches(1));
511}
512
513// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
514TEST(MatcherCastTest, FromNonReferenceToConstReference) {
515 Matcher<int> m1 = Eq(0);
516 Matcher<const int&> m2 = MatcherCast<const int&>(m1);
517 EXPECT_TRUE(m2.Matches(0));
518 EXPECT_FALSE(m2.Matches(1));
519}
520
521// Tests that MatcherCast<T&>(m) works when m is a Matcher<T>.
522TEST(MatcherCastTest, FromNonReferenceToReference) {
523 Matcher<int> m1 = Eq(0);
524 Matcher<int&> m2 = MatcherCast<int&>(m1);
525 int n = 0;
526 EXPECT_TRUE(m2.Matches(n));
527 n = 1;
528 EXPECT_FALSE(m2.Matches(n));
529}
530
531// Tests that MatcherCast<T>(m) works when m is a Matcher<T>.
532TEST(MatcherCastTest, FromSameType) {
533 Matcher<int> m1 = Eq(0);
534 Matcher<int> m2 = MatcherCast<int>(m1);
535 EXPECT_TRUE(m2.Matches(0));
536 EXPECT_FALSE(m2.Matches(1));
537}
538
zhanyong.wan18490652009-05-11 18:54:08 +0000539class Base {};
540class Derived : public Base {};
541
542// Tests that SafeMatcherCast<T>(m) works when m is a polymorphic matcher.
543TEST(SafeMatcherCastTest, FromPolymorphicMatcher) {
544 Matcher<char> m2 = SafeMatcherCast<char>(Eq(32));
545 EXPECT_TRUE(m2.Matches(' '));
546 EXPECT_FALSE(m2.Matches('\n'));
547}
548
zhanyong.wan16cf4732009-05-14 20:55:30 +0000549// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where
550// T and U are arithmetic types and T can be losslessly converted to
551// U.
552TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) {
zhanyong.wan18490652009-05-11 18:54:08 +0000553 Matcher<double> m1 = DoubleEq(1.0);
zhanyong.wan16cf4732009-05-14 20:55:30 +0000554 Matcher<float> m2 = SafeMatcherCast<float>(m1);
555 EXPECT_TRUE(m2.Matches(1.0f));
556 EXPECT_FALSE(m2.Matches(2.0f));
557
558 Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>('a'));
559 EXPECT_TRUE(m3.Matches('a'));
560 EXPECT_FALSE(m3.Matches('b'));
zhanyong.wan18490652009-05-11 18:54:08 +0000561}
562
563// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where T and U
564// are pointers or references to a derived and a base class, correspondingly.
565TEST(SafeMatcherCastTest, FromBaseClass) {
566 Derived d, d2;
567 Matcher<Base*> m1 = Eq(&d);
568 Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1);
569 EXPECT_TRUE(m2.Matches(&d));
570 EXPECT_FALSE(m2.Matches(&d2));
571
572 Matcher<Base&> m3 = Ref(d);
573 Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3);
574 EXPECT_TRUE(m4.Matches(d));
575 EXPECT_FALSE(m4.Matches(d2));
576}
577
578// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<const T&>.
579TEST(SafeMatcherCastTest, FromConstReferenceToReference) {
580 int n = 0;
581 Matcher<const int&> m1 = Ref(n);
582 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
583 int n1 = 0;
584 EXPECT_TRUE(m2.Matches(n));
585 EXPECT_FALSE(m2.Matches(n1));
586}
587
588// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
589TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) {
590 Matcher<int> m1 = Eq(0);
591 Matcher<const int&> m2 = SafeMatcherCast<const int&>(m1);
592 EXPECT_TRUE(m2.Matches(0));
593 EXPECT_FALSE(m2.Matches(1));
594}
595
596// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<T>.
597TEST(SafeMatcherCastTest, FromNonReferenceToReference) {
598 Matcher<int> m1 = Eq(0);
599 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
600 int n = 0;
601 EXPECT_TRUE(m2.Matches(n));
602 n = 1;
603 EXPECT_FALSE(m2.Matches(n));
604}
605
606// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<T>.
607TEST(SafeMatcherCastTest, FromSameType) {
608 Matcher<int> m1 = Eq(0);
609 Matcher<int> m2 = SafeMatcherCast<int>(m1);
610 EXPECT_TRUE(m2.Matches(0));
611 EXPECT_FALSE(m2.Matches(1));
612}
613
shiqiane35fdd92008-12-10 05:08:54 +0000614// Tests that A<T>() matches any value of type T.
615TEST(ATest, MatchesAnyValue) {
616 // Tests a matcher for a value type.
617 Matcher<double> m1 = A<double>();
618 EXPECT_TRUE(m1.Matches(91.43));
619 EXPECT_TRUE(m1.Matches(-15.32));
620
621 // Tests a matcher for a reference type.
622 int a = 2;
623 int b = -6;
624 Matcher<int&> m2 = A<int&>();
625 EXPECT_TRUE(m2.Matches(a));
626 EXPECT_TRUE(m2.Matches(b));
627}
628
629// Tests that A<T>() describes itself properly.
630TEST(ATest, CanDescribeSelf) {
631 EXPECT_EQ("is anything", Describe(A<bool>()));
632}
633
634// Tests that An<T>() matches any value of type T.
635TEST(AnTest, MatchesAnyValue) {
636 // Tests a matcher for a value type.
637 Matcher<int> m1 = An<int>();
638 EXPECT_TRUE(m1.Matches(9143));
639 EXPECT_TRUE(m1.Matches(-1532));
640
641 // Tests a matcher for a reference type.
642 int a = 2;
643 int b = -6;
644 Matcher<int&> m2 = An<int&>();
645 EXPECT_TRUE(m2.Matches(a));
646 EXPECT_TRUE(m2.Matches(b));
647}
648
649// Tests that An<T>() describes itself properly.
650TEST(AnTest, CanDescribeSelf) {
651 EXPECT_EQ("is anything", Describe(An<int>()));
652}
653
654// Tests that _ can be used as a matcher for any type and matches any
655// value of that type.
656TEST(UnderscoreTest, MatchesAnyValue) {
657 // Uses _ as a matcher for a value type.
658 Matcher<int> m1 = _;
659 EXPECT_TRUE(m1.Matches(123));
660 EXPECT_TRUE(m1.Matches(-242));
661
662 // Uses _ as a matcher for a reference type.
663 bool a = false;
664 const bool b = true;
665 Matcher<const bool&> m2 = _;
666 EXPECT_TRUE(m2.Matches(a));
667 EXPECT_TRUE(m2.Matches(b));
668}
669
670// Tests that _ describes itself properly.
671TEST(UnderscoreTest, CanDescribeSelf) {
672 Matcher<int> m = _;
673 EXPECT_EQ("is anything", Describe(m));
674}
675
676// Tests that Eq(x) matches any value equal to x.
677TEST(EqTest, MatchesEqualValue) {
678 // 2 C-strings with same content but different addresses.
679 const char a1[] = "hi";
680 const char a2[] = "hi";
681
682 Matcher<const char*> m1 = Eq(a1);
683 EXPECT_TRUE(m1.Matches(a1));
684 EXPECT_FALSE(m1.Matches(a2));
685}
686
687// Tests that Eq(v) describes itself properly.
688
689class Unprintable {
690 public:
691 Unprintable() : c_('a') {}
692
zhanyong.wan32de5f52009-12-23 00:13:23 +0000693 bool operator==(const Unprintable& /* rhs */) { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000694 private:
695 char c_;
696};
697
698TEST(EqTest, CanDescribeSelf) {
699 Matcher<Unprintable> m = Eq(Unprintable());
700 EXPECT_EQ("is equal to 1-byte object <61>", Describe(m));
701}
702
703// Tests that Eq(v) can be used to match any type that supports
704// comparing with type T, where T is v's type.
705TEST(EqTest, IsPolymorphic) {
706 Matcher<int> m1 = Eq(1);
707 EXPECT_TRUE(m1.Matches(1));
708 EXPECT_FALSE(m1.Matches(2));
709
710 Matcher<char> m2 = Eq(1);
711 EXPECT_TRUE(m2.Matches('\1'));
712 EXPECT_FALSE(m2.Matches('a'));
713}
714
715// Tests that TypedEq<T>(v) matches values of type T that's equal to v.
716TEST(TypedEqTest, ChecksEqualityForGivenType) {
717 Matcher<char> m1 = TypedEq<char>('a');
718 EXPECT_TRUE(m1.Matches('a'));
719 EXPECT_FALSE(m1.Matches('b'));
720
721 Matcher<int> m2 = TypedEq<int>(6);
722 EXPECT_TRUE(m2.Matches(6));
723 EXPECT_FALSE(m2.Matches(7));
724}
725
726// Tests that TypedEq(v) describes itself properly.
727TEST(TypedEqTest, CanDescribeSelf) {
728 EXPECT_EQ("is equal to 2", Describe(TypedEq<int>(2)));
729}
730
731// Tests that TypedEq<T>(v) has type Matcher<T>.
732
733// Type<T>::IsTypeOf(v) compiles iff the type of value v is T, where T
734// is a "bare" type (i.e. not in the form of const U or U&). If v's
735// type is not T, the compiler will generate a message about
736// "undefined referece".
737template <typename T>
738struct Type {
zhanyong.wan32de5f52009-12-23 00:13:23 +0000739 static bool IsTypeOf(const T& /* v */) { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000740
741 template <typename T2>
742 static void IsTypeOf(T2 v);
743};
744
745TEST(TypedEqTest, HasSpecifiedType) {
746 // Verfies that the type of TypedEq<T>(v) is Matcher<T>.
747 Type<Matcher<int> >::IsTypeOf(TypedEq<int>(5));
748 Type<Matcher<double> >::IsTypeOf(TypedEq<double>(5));
749}
750
751// Tests that Ge(v) matches anything >= v.
752TEST(GeTest, ImplementsGreaterThanOrEqual) {
753 Matcher<int> m1 = Ge(0);
754 EXPECT_TRUE(m1.Matches(1));
755 EXPECT_TRUE(m1.Matches(0));
756 EXPECT_FALSE(m1.Matches(-1));
757}
758
759// Tests that Ge(v) describes itself properly.
760TEST(GeTest, CanDescribeSelf) {
761 Matcher<int> m = Ge(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000762 EXPECT_EQ("is >= 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000763}
764
765// Tests that Gt(v) matches anything > v.
766TEST(GtTest, ImplementsGreaterThan) {
767 Matcher<double> m1 = Gt(0);
768 EXPECT_TRUE(m1.Matches(1.0));
769 EXPECT_FALSE(m1.Matches(0.0));
770 EXPECT_FALSE(m1.Matches(-1.0));
771}
772
773// Tests that Gt(v) describes itself properly.
774TEST(GtTest, CanDescribeSelf) {
775 Matcher<int> m = Gt(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000776 EXPECT_EQ("is > 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000777}
778
779// Tests that Le(v) matches anything <= v.
780TEST(LeTest, ImplementsLessThanOrEqual) {
781 Matcher<char> m1 = Le('b');
782 EXPECT_TRUE(m1.Matches('a'));
783 EXPECT_TRUE(m1.Matches('b'));
784 EXPECT_FALSE(m1.Matches('c'));
785}
786
787// Tests that Le(v) describes itself properly.
788TEST(LeTest, CanDescribeSelf) {
789 Matcher<int> m = Le(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000790 EXPECT_EQ("is <= 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000791}
792
793// Tests that Lt(v) matches anything < v.
794TEST(LtTest, ImplementsLessThan) {
795 Matcher<const string&> m1 = Lt("Hello");
796 EXPECT_TRUE(m1.Matches("Abc"));
797 EXPECT_FALSE(m1.Matches("Hello"));
798 EXPECT_FALSE(m1.Matches("Hello, world!"));
799}
800
801// Tests that Lt(v) describes itself properly.
802TEST(LtTest, CanDescribeSelf) {
803 Matcher<int> m = Lt(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000804 EXPECT_EQ("is < 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000805}
806
807// Tests that Ne(v) matches anything != v.
808TEST(NeTest, ImplementsNotEqual) {
809 Matcher<int> m1 = Ne(0);
810 EXPECT_TRUE(m1.Matches(1));
811 EXPECT_TRUE(m1.Matches(-1));
812 EXPECT_FALSE(m1.Matches(0));
813}
814
815// Tests that Ne(v) describes itself properly.
816TEST(NeTest, CanDescribeSelf) {
817 Matcher<int> m = Ne(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000818 EXPECT_EQ("isn't equal to 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000819}
820
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000821// Tests that IsNull() matches any NULL pointer of any type.
822TEST(IsNullTest, MatchesNullPointer) {
823 Matcher<int*> m1 = IsNull();
824 int* p1 = NULL;
825 int n = 0;
826 EXPECT_TRUE(m1.Matches(p1));
827 EXPECT_FALSE(m1.Matches(&n));
828
829 Matcher<const char*> m2 = IsNull();
830 const char* p2 = NULL;
831 EXPECT_TRUE(m2.Matches(p2));
832 EXPECT_FALSE(m2.Matches("hi"));
833
zhanyong.wan95b12332009-09-25 18:55:50 +0000834#if !GTEST_OS_SYMBIAN
835 // Nokia's Symbian compiler generates:
836 // gmock-matchers.h: ambiguous access to overloaded function
837 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(void *)'
838 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(const testing::
839 // MatcherInterface<void *> *)'
840 // gmock-matchers.h: (point of instantiation: 'testing::
841 // gmock_matchers_test::IsNullTest_MatchesNullPointer_Test::TestBody()')
842 // gmock-matchers.h: (instantiating: 'testing::PolymorphicMatc
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000843 Matcher<void*> m3 = IsNull();
844 void* p3 = NULL;
845 EXPECT_TRUE(m3.Matches(p3));
846 EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef)));
zhanyong.wan95b12332009-09-25 18:55:50 +0000847#endif
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000848}
849
vladlosev79b83502009-11-18 00:43:37 +0000850TEST(IsNullTest, LinkedPtr) {
851 const Matcher<linked_ptr<int> > m = IsNull();
852 const linked_ptr<int> null_p;
853 const linked_ptr<int> non_null_p(new int);
854
855 EXPECT_TRUE(m.Matches(null_p));
856 EXPECT_FALSE(m.Matches(non_null_p));
857}
858
859TEST(IsNullTest, ReferenceToConstLinkedPtr) {
860 const Matcher<const linked_ptr<double>&> m = IsNull();
861 const linked_ptr<double> null_p;
862 const linked_ptr<double> non_null_p(new double);
863
864 EXPECT_TRUE(m.Matches(null_p));
865 EXPECT_FALSE(m.Matches(non_null_p));
866}
867
vladloseve56daa72009-11-18 01:08:08 +0000868TEST(IsNullTest, ReferenceToConstScopedPtr) {
869 const Matcher<const scoped_ptr<double>&> m = IsNull();
870 const scoped_ptr<double> null_p;
871 const scoped_ptr<double> non_null_p(new double);
872
873 EXPECT_TRUE(m.Matches(null_p));
874 EXPECT_FALSE(m.Matches(non_null_p));
875}
876
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000877// Tests that IsNull() describes itself properly.
878TEST(IsNullTest, CanDescribeSelf) {
879 Matcher<int*> m = IsNull();
880 EXPECT_EQ("is NULL", Describe(m));
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000881 EXPECT_EQ("isn't NULL", DescribeNegation(m));
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000882}
883
shiqiane35fdd92008-12-10 05:08:54 +0000884// Tests that NotNull() matches any non-NULL pointer of any type.
885TEST(NotNullTest, MatchesNonNullPointer) {
886 Matcher<int*> m1 = NotNull();
887 int* p1 = NULL;
888 int n = 0;
889 EXPECT_FALSE(m1.Matches(p1));
890 EXPECT_TRUE(m1.Matches(&n));
891
892 Matcher<const char*> m2 = NotNull();
893 const char* p2 = NULL;
894 EXPECT_FALSE(m2.Matches(p2));
895 EXPECT_TRUE(m2.Matches("hi"));
896}
897
vladlosev79b83502009-11-18 00:43:37 +0000898TEST(NotNullTest, LinkedPtr) {
899 const Matcher<linked_ptr<int> > m = NotNull();
900 const linked_ptr<int> null_p;
901 const linked_ptr<int> non_null_p(new int);
902
903 EXPECT_FALSE(m.Matches(null_p));
904 EXPECT_TRUE(m.Matches(non_null_p));
905}
906
907TEST(NotNullTest, ReferenceToConstLinkedPtr) {
908 const Matcher<const linked_ptr<double>&> m = NotNull();
909 const linked_ptr<double> null_p;
910 const linked_ptr<double> non_null_p(new double);
911
912 EXPECT_FALSE(m.Matches(null_p));
913 EXPECT_TRUE(m.Matches(non_null_p));
914}
915
vladloseve56daa72009-11-18 01:08:08 +0000916TEST(NotNullTest, ReferenceToConstScopedPtr) {
917 const Matcher<const scoped_ptr<double>&> m = NotNull();
918 const scoped_ptr<double> null_p;
919 const scoped_ptr<double> non_null_p(new double);
920
921 EXPECT_FALSE(m.Matches(null_p));
922 EXPECT_TRUE(m.Matches(non_null_p));
923}
924
shiqiane35fdd92008-12-10 05:08:54 +0000925// Tests that NotNull() describes itself properly.
926TEST(NotNullTest, CanDescribeSelf) {
927 Matcher<int*> m = NotNull();
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000928 EXPECT_EQ("isn't NULL", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000929}
930
931// Tests that Ref(variable) matches an argument that references
932// 'variable'.
933TEST(RefTest, MatchesSameVariable) {
934 int a = 0;
935 int b = 0;
936 Matcher<int&> m = Ref(a);
937 EXPECT_TRUE(m.Matches(a));
938 EXPECT_FALSE(m.Matches(b));
939}
940
941// Tests that Ref(variable) describes itself properly.
942TEST(RefTest, CanDescribeSelf) {
943 int n = 5;
944 Matcher<int&> m = Ref(n);
945 stringstream ss;
946 ss << "references the variable @" << &n << " 5";
947 EXPECT_EQ(string(ss.str()), Describe(m));
948}
949
950// Test that Ref(non_const_varialbe) can be used as a matcher for a
951// const reference.
952TEST(RefTest, CanBeUsedAsMatcherForConstReference) {
953 int a = 0;
954 int b = 0;
955 Matcher<const int&> m = Ref(a);
956 EXPECT_TRUE(m.Matches(a));
957 EXPECT_FALSE(m.Matches(b));
958}
959
960// Tests that Ref(variable) is covariant, i.e. Ref(derived) can be
961// used wherever Ref(base) can be used (Ref(derived) is a sub-type
962// of Ref(base), but not vice versa.
963
shiqiane35fdd92008-12-10 05:08:54 +0000964TEST(RefTest, IsCovariant) {
965 Base base, base2;
966 Derived derived;
967 Matcher<const Base&> m1 = Ref(base);
968 EXPECT_TRUE(m1.Matches(base));
969 EXPECT_FALSE(m1.Matches(base2));
970 EXPECT_FALSE(m1.Matches(derived));
971
972 m1 = Ref(derived);
973 EXPECT_TRUE(m1.Matches(derived));
974 EXPECT_FALSE(m1.Matches(base));
975 EXPECT_FALSE(m1.Matches(base2));
976}
977
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000978TEST(RefTest, ExplainsResult) {
979 int n = 0;
980 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), n),
981 StartsWith("which is located @"));
982
983 int m = 0;
984 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), m),
985 StartsWith("which is located @"));
986}
987
shiqiane35fdd92008-12-10 05:08:54 +0000988// Tests string comparison matchers.
989
990TEST(StrEqTest, MatchesEqualString) {
991 Matcher<const char*> m = StrEq(string("Hello"));
992 EXPECT_TRUE(m.Matches("Hello"));
993 EXPECT_FALSE(m.Matches("hello"));
994 EXPECT_FALSE(m.Matches(NULL));
995
996 Matcher<const string&> m2 = StrEq("Hello");
997 EXPECT_TRUE(m2.Matches("Hello"));
998 EXPECT_FALSE(m2.Matches("Hi"));
999}
1000
1001TEST(StrEqTest, CanDescribeSelf) {
1002 Matcher<string> m = StrEq("Hi-\'\"\?\\\a\b\f\n\r\t\v\xD3");
1003 EXPECT_EQ("is equal to \"Hi-\'\\\"\\?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",
1004 Describe(m));
1005
1006 string str("01204500800");
1007 str[3] = '\0';
1008 Matcher<string> m2 = StrEq(str);
1009 EXPECT_EQ("is equal to \"012\\04500800\"", Describe(m2));
1010 str[0] = str[6] = str[7] = str[9] = str[10] = '\0';
1011 Matcher<string> m3 = StrEq(str);
1012 EXPECT_EQ("is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3));
1013}
1014
1015TEST(StrNeTest, MatchesUnequalString) {
1016 Matcher<const char*> m = StrNe("Hello");
1017 EXPECT_TRUE(m.Matches(""));
1018 EXPECT_TRUE(m.Matches(NULL));
1019 EXPECT_FALSE(m.Matches("Hello"));
1020
1021 Matcher<string> m2 = StrNe(string("Hello"));
1022 EXPECT_TRUE(m2.Matches("hello"));
1023 EXPECT_FALSE(m2.Matches("Hello"));
1024}
1025
1026TEST(StrNeTest, CanDescribeSelf) {
1027 Matcher<const char*> m = StrNe("Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001028 EXPECT_EQ("isn't equal to \"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001029}
1030
1031TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
1032 Matcher<const char*> m = StrCaseEq(string("Hello"));
1033 EXPECT_TRUE(m.Matches("Hello"));
1034 EXPECT_TRUE(m.Matches("hello"));
1035 EXPECT_FALSE(m.Matches("Hi"));
1036 EXPECT_FALSE(m.Matches(NULL));
1037
1038 Matcher<const string&> m2 = StrCaseEq("Hello");
1039 EXPECT_TRUE(m2.Matches("hello"));
1040 EXPECT_FALSE(m2.Matches("Hi"));
1041}
1042
1043TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1044 string str1("oabocdooeoo");
1045 string str2("OABOCDOOEOO");
1046 Matcher<const string&> m0 = StrCaseEq(str1);
1047 EXPECT_FALSE(m0.Matches(str2 + string(1, '\0')));
1048
1049 str1[3] = str2[3] = '\0';
1050 Matcher<const string&> m1 = StrCaseEq(str1);
1051 EXPECT_TRUE(m1.Matches(str2));
1052
1053 str1[0] = str1[6] = str1[7] = str1[10] = '\0';
1054 str2[0] = str2[6] = str2[7] = str2[10] = '\0';
1055 Matcher<const string&> m2 = StrCaseEq(str1);
1056 str1[9] = str2[9] = '\0';
1057 EXPECT_FALSE(m2.Matches(str2));
1058
1059 Matcher<const string&> m3 = StrCaseEq(str1);
1060 EXPECT_TRUE(m3.Matches(str2));
1061
1062 EXPECT_FALSE(m3.Matches(str2 + "x"));
1063 str2.append(1, '\0');
1064 EXPECT_FALSE(m3.Matches(str2));
1065 EXPECT_FALSE(m3.Matches(string(str2, 0, 9)));
1066}
1067
1068TEST(StrCaseEqTest, CanDescribeSelf) {
1069 Matcher<string> m = StrCaseEq("Hi");
1070 EXPECT_EQ("is equal to (ignoring case) \"Hi\"", Describe(m));
1071}
1072
1073TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1074 Matcher<const char*> m = StrCaseNe("Hello");
1075 EXPECT_TRUE(m.Matches("Hi"));
1076 EXPECT_TRUE(m.Matches(NULL));
1077 EXPECT_FALSE(m.Matches("Hello"));
1078 EXPECT_FALSE(m.Matches("hello"));
1079
1080 Matcher<string> m2 = StrCaseNe(string("Hello"));
1081 EXPECT_TRUE(m2.Matches(""));
1082 EXPECT_FALSE(m2.Matches("Hello"));
1083}
1084
1085TEST(StrCaseNeTest, CanDescribeSelf) {
1086 Matcher<const char*> m = StrCaseNe("Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001087 EXPECT_EQ("isn't equal to (ignoring case) \"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001088}
1089
1090// Tests that HasSubstr() works for matching string-typed values.
1091TEST(HasSubstrTest, WorksForStringClasses) {
1092 const Matcher<string> m1 = HasSubstr("foo");
1093 EXPECT_TRUE(m1.Matches(string("I love food.")));
1094 EXPECT_FALSE(m1.Matches(string("tofo")));
1095
1096 const Matcher<const std::string&> m2 = HasSubstr("foo");
1097 EXPECT_TRUE(m2.Matches(std::string("I love food.")));
1098 EXPECT_FALSE(m2.Matches(std::string("tofo")));
1099}
1100
1101// Tests that HasSubstr() works for matching C-string-typed values.
1102TEST(HasSubstrTest, WorksForCStrings) {
1103 const Matcher<char*> m1 = HasSubstr("foo");
1104 EXPECT_TRUE(m1.Matches(const_cast<char*>("I love food.")));
1105 EXPECT_FALSE(m1.Matches(const_cast<char*>("tofo")));
1106 EXPECT_FALSE(m1.Matches(NULL));
1107
1108 const Matcher<const char*> m2 = HasSubstr("foo");
1109 EXPECT_TRUE(m2.Matches("I love food."));
1110 EXPECT_FALSE(m2.Matches("tofo"));
1111 EXPECT_FALSE(m2.Matches(NULL));
1112}
1113
1114// Tests that HasSubstr(s) describes itself properly.
1115TEST(HasSubstrTest, CanDescribeSelf) {
1116 Matcher<string> m = HasSubstr("foo\n\"");
1117 EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m));
1118}
1119
zhanyong.wanb5937da2009-07-16 20:26:41 +00001120TEST(KeyTest, CanDescribeSelf) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001121 Matcher<const pair<std::string, int>&> m = Key("foo");
zhanyong.wanb5937da2009-07-16 20:26:41 +00001122 EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001123 EXPECT_EQ("doesn't have a key that is equal to \"foo\"", DescribeNegation(m));
1124}
1125
1126TEST(KeyTest, ExplainsResult) {
1127 Matcher<pair<int, bool> > m = Key(GreaterThan(10));
1128 EXPECT_EQ("whose first field is a value which is 5 less than 10",
1129 Explain(m, make_pair(5, true)));
1130 EXPECT_EQ("whose first field is a value which is 5 more than 10",
1131 Explain(m, make_pair(15, true)));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001132}
1133
1134TEST(KeyTest, MatchesCorrectly) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001135 pair<int, std::string> p(25, "foo");
zhanyong.wanb5937da2009-07-16 20:26:41 +00001136 EXPECT_THAT(p, Key(25));
1137 EXPECT_THAT(p, Not(Key(42)));
1138 EXPECT_THAT(p, Key(Ge(20)));
1139 EXPECT_THAT(p, Not(Key(Lt(25))));
1140}
1141
1142TEST(KeyTest, SafelyCastsInnerMatcher) {
1143 Matcher<int> is_positive = Gt(0);
1144 Matcher<int> is_negative = Lt(0);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001145 pair<char, bool> p('a', true);
zhanyong.wanb5937da2009-07-16 20:26:41 +00001146 EXPECT_THAT(p, Key(is_positive));
1147 EXPECT_THAT(p, Not(Key(is_negative)));
1148}
1149
1150TEST(KeyTest, InsideContainsUsingMap) {
zhanyong.wan95b12332009-09-25 18:55:50 +00001151 std::map<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001152 container.insert(make_pair(1, 'a'));
1153 container.insert(make_pair(2, 'b'));
1154 container.insert(make_pair(4, 'c'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001155 EXPECT_THAT(container, Contains(Key(1)));
1156 EXPECT_THAT(container, Not(Contains(Key(3))));
1157}
1158
1159TEST(KeyTest, InsideContainsUsingMultimap) {
zhanyong.wan95b12332009-09-25 18:55:50 +00001160 std::multimap<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001161 container.insert(make_pair(1, 'a'));
1162 container.insert(make_pair(2, 'b'));
1163 container.insert(make_pair(4, 'c'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001164
1165 EXPECT_THAT(container, Not(Contains(Key(25))));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001166 container.insert(make_pair(25, 'd'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001167 EXPECT_THAT(container, Contains(Key(25)));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001168 container.insert(make_pair(25, 'e'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001169 EXPECT_THAT(container, Contains(Key(25)));
1170
1171 EXPECT_THAT(container, Contains(Key(1)));
1172 EXPECT_THAT(container, Not(Contains(Key(3))));
1173}
1174
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001175TEST(PairTest, Typing) {
1176 // Test verifies the following type conversions can be compiled.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001177 Matcher<const pair<const char*, int>&> m1 = Pair("foo", 42);
1178 Matcher<const pair<const char*, int> > m2 = Pair("foo", 42);
1179 Matcher<pair<const char*, int> > m3 = Pair("foo", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001180
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001181 Matcher<pair<int, const std::string> > m4 = Pair(25, "42");
1182 Matcher<pair<const std::string, int> > m5 = Pair("25", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001183}
1184
1185TEST(PairTest, CanDescribeSelf) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001186 Matcher<const pair<std::string, int>&> m1 = Pair("foo", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001187 EXPECT_EQ("has a first field that is equal to \"foo\""
1188 ", and has a second field that is equal to 42",
1189 Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001190 EXPECT_EQ("has a first field that isn't equal to \"foo\""
1191 ", or has a second field that isn't equal to 42",
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001192 DescribeNegation(m1));
1193 // Double and triple negation (1 or 2 times not and description of negation).
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001194 Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42));
1195 EXPECT_EQ("has a first field that isn't equal to 13"
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001196 ", and has a second field that is equal to 42",
1197 DescribeNegation(m2));
1198}
1199
1200TEST(PairTest, CanExplainMatchResultTo) {
zhanyong.wan82113312010-01-08 21:55:40 +00001201 // If neither field matches, Pair() should explain about the first
1202 // field.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001203 const Matcher<pair<int, int> > m = Pair(GreaterThan(0), GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001204 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001205 Explain(m, make_pair(-1, -2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001206
zhanyong.wan82113312010-01-08 21:55:40 +00001207 // If the first field matches but the second doesn't, Pair() should
1208 // explain about the second field.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001209 EXPECT_EQ("whose second field does not match, which is 2 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001210 Explain(m, make_pair(1, -2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001211
zhanyong.wan82113312010-01-08 21:55:40 +00001212 // If the first field doesn't match but the second does, Pair()
1213 // should explain about the first field.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001214 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001215 Explain(m, make_pair(-1, 2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001216
zhanyong.wan82113312010-01-08 21:55:40 +00001217 // If both fields match, Pair() should explain about them both.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001218 EXPECT_EQ("whose both fields match, where the first field is a value "
1219 "which is 1 more than 0, and the second field is a value "
1220 "which is 2 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001221 Explain(m, make_pair(1, 2)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001222
1223 // If only the first match has an explanation, only this explanation should
1224 // be printed.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001225 const Matcher<pair<int, int> > explain_first = Pair(GreaterThan(0), 0);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001226 EXPECT_EQ("whose both fields match, where the first field is a value "
1227 "which is 1 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001228 Explain(explain_first, make_pair(1, 0)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001229
1230 // If only the second match has an explanation, only this explanation should
1231 // be printed.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001232 const Matcher<pair<int, int> > explain_second = Pair(0, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001233 EXPECT_EQ("whose both fields match, where the second field is a value "
1234 "which is 1 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001235 Explain(explain_second, make_pair(0, 1)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001236}
1237
1238TEST(PairTest, MatchesCorrectly) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001239 pair<int, std::string> p(25, "foo");
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001240
1241 // Both fields match.
1242 EXPECT_THAT(p, Pair(25, "foo"));
1243 EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o")));
1244
1245 // 'first' doesnt' match, but 'second' matches.
1246 EXPECT_THAT(p, Not(Pair(42, "foo")));
1247 EXPECT_THAT(p, Not(Pair(Lt(25), "foo")));
1248
1249 // 'first' matches, but 'second' doesn't match.
1250 EXPECT_THAT(p, Not(Pair(25, "bar")));
1251 EXPECT_THAT(p, Not(Pair(25, Not("foo"))));
1252
1253 // Neither field matches.
1254 EXPECT_THAT(p, Not(Pair(13, "bar")));
1255 EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a"))));
1256}
1257
1258TEST(PairTest, SafelyCastsInnerMatchers) {
1259 Matcher<int> is_positive = Gt(0);
1260 Matcher<int> is_negative = Lt(0);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001261 pair<char, bool> p('a', true);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001262 EXPECT_THAT(p, Pair(is_positive, _));
1263 EXPECT_THAT(p, Not(Pair(is_negative, _)));
1264 EXPECT_THAT(p, Pair(_, is_positive));
1265 EXPECT_THAT(p, Not(Pair(_, is_negative)));
1266}
1267
1268TEST(PairTest, InsideContainsUsingMap) {
zhanyong.wan95b12332009-09-25 18:55:50 +00001269 std::map<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001270 container.insert(make_pair(1, 'a'));
1271 container.insert(make_pair(2, 'b'));
1272 container.insert(make_pair(4, 'c'));
zhanyong.wan95b12332009-09-25 18:55:50 +00001273 EXPECT_THAT(container, Contains(Pair(1, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001274 EXPECT_THAT(container, Contains(Pair(1, _)));
zhanyong.wan95b12332009-09-25 18:55:50 +00001275 EXPECT_THAT(container, Contains(Pair(_, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001276 EXPECT_THAT(container, Not(Contains(Pair(3, _))));
1277}
1278
shiqiane35fdd92008-12-10 05:08:54 +00001279// Tests StartsWith(s).
1280
1281TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
1282 const Matcher<const char*> m1 = StartsWith(string(""));
1283 EXPECT_TRUE(m1.Matches("Hi"));
1284 EXPECT_TRUE(m1.Matches(""));
1285 EXPECT_FALSE(m1.Matches(NULL));
1286
1287 const Matcher<const string&> m2 = StartsWith("Hi");
1288 EXPECT_TRUE(m2.Matches("Hi"));
1289 EXPECT_TRUE(m2.Matches("Hi Hi!"));
1290 EXPECT_TRUE(m2.Matches("High"));
1291 EXPECT_FALSE(m2.Matches("H"));
1292 EXPECT_FALSE(m2.Matches(" Hi"));
1293}
1294
1295TEST(StartsWithTest, CanDescribeSelf) {
1296 Matcher<const std::string> m = StartsWith("Hi");
1297 EXPECT_EQ("starts with \"Hi\"", Describe(m));
1298}
1299
1300// Tests EndsWith(s).
1301
1302TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
1303 const Matcher<const char*> m1 = EndsWith("");
1304 EXPECT_TRUE(m1.Matches("Hi"));
1305 EXPECT_TRUE(m1.Matches(""));
1306 EXPECT_FALSE(m1.Matches(NULL));
1307
1308 const Matcher<const string&> m2 = EndsWith(string("Hi"));
1309 EXPECT_TRUE(m2.Matches("Hi"));
1310 EXPECT_TRUE(m2.Matches("Wow Hi Hi"));
1311 EXPECT_TRUE(m2.Matches("Super Hi"));
1312 EXPECT_FALSE(m2.Matches("i"));
1313 EXPECT_FALSE(m2.Matches("Hi "));
1314}
1315
1316TEST(EndsWithTest, CanDescribeSelf) {
1317 Matcher<const std::string> m = EndsWith("Hi");
1318 EXPECT_EQ("ends with \"Hi\"", Describe(m));
1319}
1320
shiqiane35fdd92008-12-10 05:08:54 +00001321// Tests MatchesRegex().
1322
1323TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
1324 const Matcher<const char*> m1 = MatchesRegex("a.*z");
1325 EXPECT_TRUE(m1.Matches("az"));
1326 EXPECT_TRUE(m1.Matches("abcz"));
1327 EXPECT_FALSE(m1.Matches(NULL));
1328
1329 const Matcher<const string&> m2 = MatchesRegex(new RE("a.*z"));
1330 EXPECT_TRUE(m2.Matches("azbz"));
1331 EXPECT_FALSE(m2.Matches("az1"));
1332 EXPECT_FALSE(m2.Matches("1az"));
1333}
1334
1335TEST(MatchesRegexTest, CanDescribeSelf) {
1336 Matcher<const std::string> m1 = MatchesRegex(string("Hi.*"));
1337 EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1));
1338
zhanyong.wand14aaed2010-01-14 05:36:32 +00001339 Matcher<const char*> m2 = MatchesRegex(new RE("a.*"));
1340 EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +00001341}
1342
1343// Tests ContainsRegex().
1344
1345TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
1346 const Matcher<const char*> m1 = ContainsRegex(string("a.*z"));
1347 EXPECT_TRUE(m1.Matches("az"));
1348 EXPECT_TRUE(m1.Matches("0abcz1"));
1349 EXPECT_FALSE(m1.Matches(NULL));
1350
1351 const Matcher<const string&> m2 = ContainsRegex(new RE("a.*z"));
1352 EXPECT_TRUE(m2.Matches("azbz"));
1353 EXPECT_TRUE(m2.Matches("az1"));
1354 EXPECT_FALSE(m2.Matches("1a"));
1355}
1356
1357TEST(ContainsRegexTest, CanDescribeSelf) {
1358 Matcher<const std::string> m1 = ContainsRegex("Hi.*");
1359 EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1));
1360
zhanyong.wand14aaed2010-01-14 05:36:32 +00001361 Matcher<const char*> m2 = ContainsRegex(new RE("a.*"));
1362 EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +00001363}
shiqiane35fdd92008-12-10 05:08:54 +00001364
1365// Tests for wide strings.
1366#if GTEST_HAS_STD_WSTRING
1367TEST(StdWideStrEqTest, MatchesEqual) {
1368 Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello"));
1369 EXPECT_TRUE(m.Matches(L"Hello"));
1370 EXPECT_FALSE(m.Matches(L"hello"));
1371 EXPECT_FALSE(m.Matches(NULL));
1372
1373 Matcher<const ::std::wstring&> m2 = StrEq(L"Hello");
1374 EXPECT_TRUE(m2.Matches(L"Hello"));
1375 EXPECT_FALSE(m2.Matches(L"Hi"));
1376
1377 Matcher<const ::std::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1378 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1379 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1380
1381 ::std::wstring str(L"01204500800");
1382 str[3] = L'\0';
1383 Matcher<const ::std::wstring&> m4 = StrEq(str);
1384 EXPECT_TRUE(m4.Matches(str));
1385 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1386 Matcher<const ::std::wstring&> m5 = StrEq(str);
1387 EXPECT_TRUE(m5.Matches(str));
1388}
1389
1390TEST(StdWideStrEqTest, CanDescribeSelf) {
1391 Matcher< ::std::wstring> m = StrEq(L"Hi-\'\"\?\\\a\b\f\n\r\t\v");
1392 EXPECT_EQ("is equal to L\"Hi-\'\\\"\\?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
1393 Describe(m));
1394
1395 Matcher< ::std::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1396 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1397 Describe(m2));
1398
1399 ::std::wstring str(L"01204500800");
1400 str[3] = L'\0';
1401 Matcher<const ::std::wstring&> m4 = StrEq(str);
1402 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1403 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1404 Matcher<const ::std::wstring&> m5 = StrEq(str);
1405 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1406}
1407
1408TEST(StdWideStrNeTest, MatchesUnequalString) {
1409 Matcher<const wchar_t*> m = StrNe(L"Hello");
1410 EXPECT_TRUE(m.Matches(L""));
1411 EXPECT_TRUE(m.Matches(NULL));
1412 EXPECT_FALSE(m.Matches(L"Hello"));
1413
1414 Matcher< ::std::wstring> m2 = StrNe(::std::wstring(L"Hello"));
1415 EXPECT_TRUE(m2.Matches(L"hello"));
1416 EXPECT_FALSE(m2.Matches(L"Hello"));
1417}
1418
1419TEST(StdWideStrNeTest, CanDescribeSelf) {
1420 Matcher<const wchar_t*> m = StrNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001421 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001422}
1423
1424TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1425 Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello"));
1426 EXPECT_TRUE(m.Matches(L"Hello"));
1427 EXPECT_TRUE(m.Matches(L"hello"));
1428 EXPECT_FALSE(m.Matches(L"Hi"));
1429 EXPECT_FALSE(m.Matches(NULL));
1430
1431 Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello");
1432 EXPECT_TRUE(m2.Matches(L"hello"));
1433 EXPECT_FALSE(m2.Matches(L"Hi"));
1434}
1435
1436TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1437 ::std::wstring str1(L"oabocdooeoo");
1438 ::std::wstring str2(L"OABOCDOOEOO");
1439 Matcher<const ::std::wstring&> m0 = StrCaseEq(str1);
1440 EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L'\0')));
1441
1442 str1[3] = str2[3] = L'\0';
1443 Matcher<const ::std::wstring&> m1 = StrCaseEq(str1);
1444 EXPECT_TRUE(m1.Matches(str2));
1445
1446 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1447 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1448 Matcher<const ::std::wstring&> m2 = StrCaseEq(str1);
1449 str1[9] = str2[9] = L'\0';
1450 EXPECT_FALSE(m2.Matches(str2));
1451
1452 Matcher<const ::std::wstring&> m3 = StrCaseEq(str1);
1453 EXPECT_TRUE(m3.Matches(str2));
1454
1455 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1456 str2.append(1, L'\0');
1457 EXPECT_FALSE(m3.Matches(str2));
1458 EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9)));
1459}
1460
1461TEST(StdWideStrCaseEqTest, CanDescribeSelf) {
1462 Matcher< ::std::wstring> m = StrCaseEq(L"Hi");
1463 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1464}
1465
1466TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1467 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1468 EXPECT_TRUE(m.Matches(L"Hi"));
1469 EXPECT_TRUE(m.Matches(NULL));
1470 EXPECT_FALSE(m.Matches(L"Hello"));
1471 EXPECT_FALSE(m.Matches(L"hello"));
1472
1473 Matcher< ::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello"));
1474 EXPECT_TRUE(m2.Matches(L""));
1475 EXPECT_FALSE(m2.Matches(L"Hello"));
1476}
1477
1478TEST(StdWideStrCaseNeTest, CanDescribeSelf) {
1479 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001480 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001481}
1482
1483// Tests that HasSubstr() works for matching wstring-typed values.
1484TEST(StdWideHasSubstrTest, WorksForStringClasses) {
1485 const Matcher< ::std::wstring> m1 = HasSubstr(L"foo");
1486 EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food.")));
1487 EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo")));
1488
1489 const Matcher<const ::std::wstring&> m2 = HasSubstr(L"foo");
1490 EXPECT_TRUE(m2.Matches(::std::wstring(L"I love food.")));
1491 EXPECT_FALSE(m2.Matches(::std::wstring(L"tofo")));
1492}
1493
1494// Tests that HasSubstr() works for matching C-wide-string-typed values.
1495TEST(StdWideHasSubstrTest, WorksForCStrings) {
1496 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1497 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1498 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1499 EXPECT_FALSE(m1.Matches(NULL));
1500
1501 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1502 EXPECT_TRUE(m2.Matches(L"I love food."));
1503 EXPECT_FALSE(m2.Matches(L"tofo"));
1504 EXPECT_FALSE(m2.Matches(NULL));
1505}
1506
1507// Tests that HasSubstr(s) describes itself properly.
1508TEST(StdWideHasSubstrTest, CanDescribeSelf) {
1509 Matcher< ::std::wstring> m = HasSubstr(L"foo\n\"");
1510 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1511}
1512
1513// Tests StartsWith(s).
1514
1515TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) {
1516 const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(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 = StartsWith(L"Hi");
1522 EXPECT_TRUE(m2.Matches(L"Hi"));
1523 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1524 EXPECT_TRUE(m2.Matches(L"High"));
1525 EXPECT_FALSE(m2.Matches(L"H"));
1526 EXPECT_FALSE(m2.Matches(L" Hi"));
1527}
1528
1529TEST(StdWideStartsWithTest, CanDescribeSelf) {
1530 Matcher<const ::std::wstring> m = StartsWith(L"Hi");
1531 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1532}
1533
1534// Tests EndsWith(s).
1535
1536TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) {
1537 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1538 EXPECT_TRUE(m1.Matches(L"Hi"));
1539 EXPECT_TRUE(m1.Matches(L""));
1540 EXPECT_FALSE(m1.Matches(NULL));
1541
1542 const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L"Hi"));
1543 EXPECT_TRUE(m2.Matches(L"Hi"));
1544 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1545 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1546 EXPECT_FALSE(m2.Matches(L"i"));
1547 EXPECT_FALSE(m2.Matches(L"Hi "));
1548}
1549
1550TEST(StdWideEndsWithTest, CanDescribeSelf) {
1551 Matcher<const ::std::wstring> m = EndsWith(L"Hi");
1552 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1553}
1554
1555#endif // GTEST_HAS_STD_WSTRING
1556
1557#if GTEST_HAS_GLOBAL_WSTRING
1558TEST(GlobalWideStrEqTest, MatchesEqual) {
1559 Matcher<const wchar_t*> m = StrEq(::wstring(L"Hello"));
1560 EXPECT_TRUE(m.Matches(L"Hello"));
1561 EXPECT_FALSE(m.Matches(L"hello"));
1562 EXPECT_FALSE(m.Matches(NULL));
1563
1564 Matcher<const ::wstring&> m2 = StrEq(L"Hello");
1565 EXPECT_TRUE(m2.Matches(L"Hello"));
1566 EXPECT_FALSE(m2.Matches(L"Hi"));
1567
1568 Matcher<const ::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1569 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1570 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1571
1572 ::wstring str(L"01204500800");
1573 str[3] = L'\0';
1574 Matcher<const ::wstring&> m4 = StrEq(str);
1575 EXPECT_TRUE(m4.Matches(str));
1576 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1577 Matcher<const ::wstring&> m5 = StrEq(str);
1578 EXPECT_TRUE(m5.Matches(str));
1579}
1580
1581TEST(GlobalWideStrEqTest, CanDescribeSelf) {
1582 Matcher< ::wstring> m = StrEq(L"Hi-\'\"\?\\\a\b\f\n\r\t\v");
1583 EXPECT_EQ("is equal to L\"Hi-\'\\\"\\?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
1584 Describe(m));
1585
1586 Matcher< ::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1587 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1588 Describe(m2));
1589
1590 ::wstring str(L"01204500800");
1591 str[3] = L'\0';
1592 Matcher<const ::wstring&> m4 = StrEq(str);
1593 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1594 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1595 Matcher<const ::wstring&> m5 = StrEq(str);
1596 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1597}
1598
1599TEST(GlobalWideStrNeTest, MatchesUnequalString) {
1600 Matcher<const wchar_t*> m = StrNe(L"Hello");
1601 EXPECT_TRUE(m.Matches(L""));
1602 EXPECT_TRUE(m.Matches(NULL));
1603 EXPECT_FALSE(m.Matches(L"Hello"));
1604
1605 Matcher< ::wstring> m2 = StrNe(::wstring(L"Hello"));
1606 EXPECT_TRUE(m2.Matches(L"hello"));
1607 EXPECT_FALSE(m2.Matches(L"Hello"));
1608}
1609
1610TEST(GlobalWideStrNeTest, CanDescribeSelf) {
1611 Matcher<const wchar_t*> m = StrNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001612 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001613}
1614
1615TEST(GlobalWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1616 Matcher<const wchar_t*> m = StrCaseEq(::wstring(L"Hello"));
1617 EXPECT_TRUE(m.Matches(L"Hello"));
1618 EXPECT_TRUE(m.Matches(L"hello"));
1619 EXPECT_FALSE(m.Matches(L"Hi"));
1620 EXPECT_FALSE(m.Matches(NULL));
1621
1622 Matcher<const ::wstring&> m2 = StrCaseEq(L"Hello");
1623 EXPECT_TRUE(m2.Matches(L"hello"));
1624 EXPECT_FALSE(m2.Matches(L"Hi"));
1625}
1626
1627TEST(GlobalWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1628 ::wstring str1(L"oabocdooeoo");
1629 ::wstring str2(L"OABOCDOOEOO");
1630 Matcher<const ::wstring&> m0 = StrCaseEq(str1);
1631 EXPECT_FALSE(m0.Matches(str2 + ::wstring(1, L'\0')));
1632
1633 str1[3] = str2[3] = L'\0';
1634 Matcher<const ::wstring&> m1 = StrCaseEq(str1);
1635 EXPECT_TRUE(m1.Matches(str2));
1636
1637 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1638 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1639 Matcher<const ::wstring&> m2 = StrCaseEq(str1);
1640 str1[9] = str2[9] = L'\0';
1641 EXPECT_FALSE(m2.Matches(str2));
1642
1643 Matcher<const ::wstring&> m3 = StrCaseEq(str1);
1644 EXPECT_TRUE(m3.Matches(str2));
1645
1646 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1647 str2.append(1, L'\0');
1648 EXPECT_FALSE(m3.Matches(str2));
1649 EXPECT_FALSE(m3.Matches(::wstring(str2, 0, 9)));
1650}
1651
1652TEST(GlobalWideStrCaseEqTest, CanDescribeSelf) {
1653 Matcher< ::wstring> m = StrCaseEq(L"Hi");
1654 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1655}
1656
1657TEST(GlobalWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1658 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1659 EXPECT_TRUE(m.Matches(L"Hi"));
1660 EXPECT_TRUE(m.Matches(NULL));
1661 EXPECT_FALSE(m.Matches(L"Hello"));
1662 EXPECT_FALSE(m.Matches(L"hello"));
1663
1664 Matcher< ::wstring> m2 = StrCaseNe(::wstring(L"Hello"));
1665 EXPECT_TRUE(m2.Matches(L""));
1666 EXPECT_FALSE(m2.Matches(L"Hello"));
1667}
1668
1669TEST(GlobalWideStrCaseNeTest, CanDescribeSelf) {
1670 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001671 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001672}
1673
1674// Tests that HasSubstr() works for matching wstring-typed values.
1675TEST(GlobalWideHasSubstrTest, WorksForStringClasses) {
1676 const Matcher< ::wstring> m1 = HasSubstr(L"foo");
1677 EXPECT_TRUE(m1.Matches(::wstring(L"I love food.")));
1678 EXPECT_FALSE(m1.Matches(::wstring(L"tofo")));
1679
1680 const Matcher<const ::wstring&> m2 = HasSubstr(L"foo");
1681 EXPECT_TRUE(m2.Matches(::wstring(L"I love food.")));
1682 EXPECT_FALSE(m2.Matches(::wstring(L"tofo")));
1683}
1684
1685// Tests that HasSubstr() works for matching C-wide-string-typed values.
1686TEST(GlobalWideHasSubstrTest, WorksForCStrings) {
1687 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1688 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1689 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1690 EXPECT_FALSE(m1.Matches(NULL));
1691
1692 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1693 EXPECT_TRUE(m2.Matches(L"I love food."));
1694 EXPECT_FALSE(m2.Matches(L"tofo"));
1695 EXPECT_FALSE(m2.Matches(NULL));
1696}
1697
1698// Tests that HasSubstr(s) describes itself properly.
1699TEST(GlobalWideHasSubstrTest, CanDescribeSelf) {
1700 Matcher< ::wstring> m = HasSubstr(L"foo\n\"");
1701 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1702}
1703
1704// Tests StartsWith(s).
1705
1706TEST(GlobalWideStartsWithTest, MatchesStringWithGivenPrefix) {
1707 const Matcher<const wchar_t*> m1 = StartsWith(::wstring(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 = StartsWith(L"Hi");
1713 EXPECT_TRUE(m2.Matches(L"Hi"));
1714 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1715 EXPECT_TRUE(m2.Matches(L"High"));
1716 EXPECT_FALSE(m2.Matches(L"H"));
1717 EXPECT_FALSE(m2.Matches(L" Hi"));
1718}
1719
1720TEST(GlobalWideStartsWithTest, CanDescribeSelf) {
1721 Matcher<const ::wstring> m = StartsWith(L"Hi");
1722 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1723}
1724
1725// Tests EndsWith(s).
1726
1727TEST(GlobalWideEndsWithTest, MatchesStringWithGivenSuffix) {
1728 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1729 EXPECT_TRUE(m1.Matches(L"Hi"));
1730 EXPECT_TRUE(m1.Matches(L""));
1731 EXPECT_FALSE(m1.Matches(NULL));
1732
1733 const Matcher<const ::wstring&> m2 = EndsWith(::wstring(L"Hi"));
1734 EXPECT_TRUE(m2.Matches(L"Hi"));
1735 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1736 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1737 EXPECT_FALSE(m2.Matches(L"i"));
1738 EXPECT_FALSE(m2.Matches(L"Hi "));
1739}
1740
1741TEST(GlobalWideEndsWithTest, CanDescribeSelf) {
1742 Matcher<const ::wstring> m = EndsWith(L"Hi");
1743 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1744}
1745
1746#endif // GTEST_HAS_GLOBAL_WSTRING
1747
1748
1749typedef ::std::tr1::tuple<long, int> Tuple2; // NOLINT
1750
1751// Tests that Eq() matches a 2-tuple where the first field == the
1752// second field.
1753TEST(Eq2Test, MatchesEqualArguments) {
1754 Matcher<const Tuple2&> m = Eq();
1755 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1756 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1757}
1758
1759// Tests that Eq() describes itself properly.
1760TEST(Eq2Test, CanDescribeSelf) {
1761 Matcher<const Tuple2&> m = Eq();
zhanyong.wan2661c682009-06-09 05:42:12 +00001762 EXPECT_EQ("are a pair (x, y) where x == y", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001763}
1764
1765// Tests that Ge() matches a 2-tuple where the first field >= the
1766// second field.
1767TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) {
1768 Matcher<const Tuple2&> m = Ge();
1769 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1770 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1771 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1772}
1773
1774// Tests that Ge() describes itself properly.
1775TEST(Ge2Test, CanDescribeSelf) {
1776 Matcher<const Tuple2&> m = Ge();
zhanyong.wan2661c682009-06-09 05:42:12 +00001777 EXPECT_EQ("are a pair (x, y) where x >= y", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001778}
1779
1780// Tests that Gt() matches a 2-tuple where the first field > the
1781// second field.
1782TEST(Gt2Test, MatchesGreaterThanArguments) {
1783 Matcher<const Tuple2&> m = Gt();
1784 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1785 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1786 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1787}
1788
1789// Tests that Gt() describes itself properly.
1790TEST(Gt2Test, CanDescribeSelf) {
1791 Matcher<const Tuple2&> m = Gt();
zhanyong.wan2661c682009-06-09 05:42:12 +00001792 EXPECT_EQ("are a pair (x, y) where x > y", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001793}
1794
1795// Tests that Le() matches a 2-tuple where the first field <= the
1796// second field.
1797TEST(Le2Test, MatchesLessThanOrEqualArguments) {
1798 Matcher<const Tuple2&> m = Le();
1799 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1800 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1801 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1802}
1803
1804// Tests that Le() describes itself properly.
1805TEST(Le2Test, CanDescribeSelf) {
1806 Matcher<const Tuple2&> m = Le();
zhanyong.wan2661c682009-06-09 05:42:12 +00001807 EXPECT_EQ("are a pair (x, y) where x <= y", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001808}
1809
1810// Tests that Lt() matches a 2-tuple where the first field < the
1811// second field.
1812TEST(Lt2Test, MatchesLessThanArguments) {
1813 Matcher<const Tuple2&> m = Lt();
1814 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1815 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1816 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1817}
1818
1819// Tests that Lt() describes itself properly.
1820TEST(Lt2Test, CanDescribeSelf) {
1821 Matcher<const Tuple2&> m = Lt();
zhanyong.wan2661c682009-06-09 05:42:12 +00001822 EXPECT_EQ("are a pair (x, y) where x < y", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001823}
1824
1825// Tests that Ne() matches a 2-tuple where the first field != the
1826// second field.
1827TEST(Ne2Test, MatchesUnequalArguments) {
1828 Matcher<const Tuple2&> m = Ne();
1829 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1830 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1831 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1832}
1833
1834// Tests that Ne() describes itself properly.
1835TEST(Ne2Test, CanDescribeSelf) {
1836 Matcher<const Tuple2&> m = Ne();
zhanyong.wan2661c682009-06-09 05:42:12 +00001837 EXPECT_EQ("are a pair (x, y) where x != y", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001838}
1839
1840// Tests that Not(m) matches any value that doesn't match m.
1841TEST(NotTest, NegatesMatcher) {
1842 Matcher<int> m;
1843 m = Not(Eq(2));
1844 EXPECT_TRUE(m.Matches(3));
1845 EXPECT_FALSE(m.Matches(2));
1846}
1847
1848// Tests that Not(m) describes itself properly.
1849TEST(NotTest, CanDescribeSelf) {
1850 Matcher<int> m = Not(Eq(5));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001851 EXPECT_EQ("isn't equal to 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001852}
1853
zhanyong.wan18490652009-05-11 18:54:08 +00001854// Tests that monomorphic matchers are safely cast by the Not matcher.
1855TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {
1856 // greater_than_5 is a monomorphic matcher.
1857 Matcher<int> greater_than_5 = Gt(5);
1858
1859 Matcher<const int&> m = Not(greater_than_5);
1860 Matcher<int&> m2 = Not(greater_than_5);
1861 Matcher<int&> m3 = Not(m);
1862}
1863
shiqiane35fdd92008-12-10 05:08:54 +00001864// Tests that AllOf(m1, ..., mn) matches any value that matches all of
1865// the given matchers.
1866TEST(AllOfTest, MatchesWhenAllMatch) {
1867 Matcher<int> m;
1868 m = AllOf(Le(2), Ge(1));
1869 EXPECT_TRUE(m.Matches(1));
1870 EXPECT_TRUE(m.Matches(2));
1871 EXPECT_FALSE(m.Matches(0));
1872 EXPECT_FALSE(m.Matches(3));
1873
1874 m = AllOf(Gt(0), Ne(1), Ne(2));
1875 EXPECT_TRUE(m.Matches(3));
1876 EXPECT_FALSE(m.Matches(2));
1877 EXPECT_FALSE(m.Matches(1));
1878 EXPECT_FALSE(m.Matches(0));
1879
1880 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
1881 EXPECT_TRUE(m.Matches(4));
1882 EXPECT_FALSE(m.Matches(3));
1883 EXPECT_FALSE(m.Matches(2));
1884 EXPECT_FALSE(m.Matches(1));
1885 EXPECT_FALSE(m.Matches(0));
1886
1887 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
1888 EXPECT_TRUE(m.Matches(0));
1889 EXPECT_TRUE(m.Matches(1));
1890 EXPECT_FALSE(m.Matches(3));
1891}
1892
1893// Tests that AllOf(m1, ..., mn) describes itself properly.
1894TEST(AllOfTest, CanDescribeSelf) {
1895 Matcher<int> m;
1896 m = AllOf(Le(2), Ge(1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001897 EXPECT_EQ("(is <= 2) and (is >= 1)", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001898
1899 m = AllOf(Gt(0), Ne(1), Ne(2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001900 EXPECT_EQ("(is > 0) and "
1901 "((isn't equal to 1) and "
1902 "(isn't equal to 2))",
shiqiane35fdd92008-12-10 05:08:54 +00001903 Describe(m));
1904
1905
1906 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001907 EXPECT_EQ("(is > 0) and "
1908 "((isn't equal to 1) and "
1909 "((isn't equal to 2) and "
1910 "(isn't equal to 3)))",
shiqiane35fdd92008-12-10 05:08:54 +00001911 Describe(m));
1912
1913
1914 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001915 EXPECT_EQ("(is >= 0) and "
1916 "((is < 10) and "
1917 "((isn't equal to 3) and "
1918 "((isn't equal to 5) and "
1919 "(isn't equal to 7))))",
1920 Describe(m));
1921}
1922
1923// Tests that AllOf(m1, ..., mn) describes its negation properly.
1924TEST(AllOfTest, CanDescribeNegation) {
1925 Matcher<int> m;
1926 m = AllOf(Le(2), Ge(1));
1927 EXPECT_EQ("(isn't <= 2) or "
1928 "(isn't >= 1)",
1929 DescribeNegation(m));
1930
1931 m = AllOf(Gt(0), Ne(1), Ne(2));
1932 EXPECT_EQ("(isn't > 0) or "
1933 "((is equal to 1) or "
1934 "(is equal to 2))",
1935 DescribeNegation(m));
1936
1937
1938 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
1939 EXPECT_EQ("(isn't > 0) or "
1940 "((is equal to 1) or "
1941 "((is equal to 2) or "
1942 "(is equal to 3)))",
1943 DescribeNegation(m));
1944
1945
1946 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
1947 EXPECT_EQ("(isn't >= 0) or "
1948 "((isn't < 10) or "
1949 "((is equal to 3) or "
1950 "((is equal to 5) or "
1951 "(is equal to 7))))",
1952 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00001953}
1954
zhanyong.wan18490652009-05-11 18:54:08 +00001955// Tests that monomorphic matchers are safely cast by the AllOf matcher.
1956TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
1957 // greater_than_5 and less_than_10 are monomorphic matchers.
1958 Matcher<int> greater_than_5 = Gt(5);
1959 Matcher<int> less_than_10 = Lt(10);
1960
1961 Matcher<const int&> m = AllOf(greater_than_5, less_than_10);
1962 Matcher<int&> m2 = AllOf(greater_than_5, less_than_10);
1963 Matcher<int&> m3 = AllOf(greater_than_5, m2);
1964
1965 // Tests that BothOf works when composing itself.
1966 Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10);
1967 Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);
1968}
1969
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001970TEST(AllOfTest, ExplainsResult) {
1971 Matcher<int> m;
1972
1973 // Successful match. Both matchers need to explain. The second
1974 // matcher doesn't give an explanation, so only the first matcher's
1975 // explanation is printed.
1976 m = AllOf(GreaterThan(10), Lt(30));
1977 EXPECT_EQ("which is 15 more than 10", Explain(m, 25));
1978
1979 // Successful match. Both matchers need to explain.
1980 m = AllOf(GreaterThan(10), GreaterThan(20));
1981 EXPECT_EQ("which is 20 more than 10, and which is 10 more than 20",
1982 Explain(m, 30));
1983
1984 // Successful match. All matchers need to explain. The second
1985 // matcher doesn't given an explanation.
1986 m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20));
1987 EXPECT_EQ("which is 15 more than 10, and which is 5 more than 20",
1988 Explain(m, 25));
1989
1990 // Successful match. All matchers need to explain.
1991 m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
1992 EXPECT_EQ("which is 30 more than 10, and which is 20 more than 20, "
1993 "and which is 10 more than 30",
1994 Explain(m, 40));
1995
1996 // Failed match. The first matcher, which failed, needs to
1997 // explain.
1998 m = AllOf(GreaterThan(10), GreaterThan(20));
1999 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
2000
2001 // Failed match. The second matcher, which failed, needs to
2002 // explain. Since it doesn't given an explanation, nothing is
2003 // printed.
2004 m = AllOf(GreaterThan(10), Lt(30));
2005 EXPECT_EQ("", Explain(m, 40));
2006
2007 // Failed match. The second matcher, which failed, needs to
2008 // explain.
2009 m = AllOf(GreaterThan(10), GreaterThan(20));
2010 EXPECT_EQ("which is 5 less than 20", Explain(m, 15));
2011}
2012
shiqiane35fdd92008-12-10 05:08:54 +00002013// Tests that AnyOf(m1, ..., mn) matches any value that matches at
2014// least one of the given matchers.
2015TEST(AnyOfTest, MatchesWhenAnyMatches) {
2016 Matcher<int> m;
2017 m = AnyOf(Le(1), Ge(3));
2018 EXPECT_TRUE(m.Matches(1));
2019 EXPECT_TRUE(m.Matches(4));
2020 EXPECT_FALSE(m.Matches(2));
2021
2022 m = AnyOf(Lt(0), Eq(1), Eq(2));
2023 EXPECT_TRUE(m.Matches(-1));
2024 EXPECT_TRUE(m.Matches(1));
2025 EXPECT_TRUE(m.Matches(2));
2026 EXPECT_FALSE(m.Matches(0));
2027
2028 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2029 EXPECT_TRUE(m.Matches(-1));
2030 EXPECT_TRUE(m.Matches(1));
2031 EXPECT_TRUE(m.Matches(2));
2032 EXPECT_TRUE(m.Matches(3));
2033 EXPECT_FALSE(m.Matches(0));
2034
2035 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2036 EXPECT_TRUE(m.Matches(0));
2037 EXPECT_TRUE(m.Matches(11));
2038 EXPECT_TRUE(m.Matches(3));
2039 EXPECT_FALSE(m.Matches(2));
2040}
2041
2042// Tests that AnyOf(m1, ..., mn) describes itself properly.
2043TEST(AnyOfTest, CanDescribeSelf) {
2044 Matcher<int> m;
2045 m = AnyOf(Le(1), Ge(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002046 EXPECT_EQ("(is <= 1) or (is >= 3)",
shiqiane35fdd92008-12-10 05:08:54 +00002047 Describe(m));
2048
2049 m = AnyOf(Lt(0), Eq(1), Eq(2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002050 EXPECT_EQ("(is < 0) or "
shiqiane35fdd92008-12-10 05:08:54 +00002051 "((is equal to 1) or (is equal to 2))",
2052 Describe(m));
2053
2054 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002055 EXPECT_EQ("(is < 0) or "
shiqiane35fdd92008-12-10 05:08:54 +00002056 "((is equal to 1) or "
2057 "((is equal to 2) or "
2058 "(is equal to 3)))",
2059 Describe(m));
2060
2061 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002062 EXPECT_EQ("(is <= 0) or "
2063 "((is > 10) or "
shiqiane35fdd92008-12-10 05:08:54 +00002064 "((is equal to 3) or "
2065 "((is equal to 5) or "
2066 "(is equal to 7))))",
2067 Describe(m));
2068}
2069
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002070// Tests that AnyOf(m1, ..., mn) describes its negation properly.
2071TEST(AnyOfTest, CanDescribeNegation) {
2072 Matcher<int> m;
2073 m = AnyOf(Le(1), Ge(3));
2074 EXPECT_EQ("(isn't <= 1) and (isn't >= 3)",
2075 DescribeNegation(m));
2076
2077 m = AnyOf(Lt(0), Eq(1), Eq(2));
2078 EXPECT_EQ("(isn't < 0) and "
2079 "((isn't equal to 1) and (isn't equal to 2))",
2080 DescribeNegation(m));
2081
2082 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2083 EXPECT_EQ("(isn't < 0) and "
2084 "((isn't equal to 1) and "
2085 "((isn't equal to 2) and "
2086 "(isn't equal to 3)))",
2087 DescribeNegation(m));
2088
2089 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2090 EXPECT_EQ("(isn't <= 0) and "
2091 "((isn't > 10) and "
2092 "((isn't equal to 3) and "
2093 "((isn't equal to 5) and "
2094 "(isn't equal to 7))))",
2095 DescribeNegation(m));
2096}
2097
zhanyong.wan18490652009-05-11 18:54:08 +00002098// Tests that monomorphic matchers are safely cast by the AnyOf matcher.
2099TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
2100 // greater_than_5 and less_than_10 are monomorphic matchers.
2101 Matcher<int> greater_than_5 = Gt(5);
2102 Matcher<int> less_than_10 = Lt(10);
2103
2104 Matcher<const int&> m = AnyOf(greater_than_5, less_than_10);
2105 Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10);
2106 Matcher<int&> m3 = AnyOf(greater_than_5, m2);
2107
2108 // Tests that EitherOf works when composing itself.
2109 Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10);
2110 Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);
2111}
2112
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002113TEST(AnyOfTest, ExplainsResult) {
2114 Matcher<int> m;
2115
2116 // Failed match. Both matchers need to explain. The second
2117 // matcher doesn't give an explanation, so only the first matcher's
2118 // explanation is printed.
2119 m = AnyOf(GreaterThan(10), Lt(0));
2120 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
2121
2122 // Failed match. Both matchers need to explain.
2123 m = AnyOf(GreaterThan(10), GreaterThan(20));
2124 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20",
2125 Explain(m, 5));
2126
2127 // Failed match. All matchers need to explain. The second
2128 // matcher doesn't given an explanation.
2129 m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30));
2130 EXPECT_EQ("which is 5 less than 10, and which is 25 less than 30",
2131 Explain(m, 5));
2132
2133 // Failed match. All matchers need to explain.
2134 m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2135 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20, "
2136 "and which is 25 less than 30",
2137 Explain(m, 5));
2138
2139 // Successful match. The first matcher, which succeeded, needs to
2140 // explain.
2141 m = AnyOf(GreaterThan(10), GreaterThan(20));
2142 EXPECT_EQ("which is 5 more than 10", Explain(m, 15));
2143
2144 // Successful match. The second matcher, which succeeded, needs to
2145 // explain. Since it doesn't given an explanation, nothing is
2146 // printed.
2147 m = AnyOf(GreaterThan(10), Lt(30));
2148 EXPECT_EQ("", Explain(m, 0));
2149
2150 // Successful match. The second matcher, which succeeded, needs to
2151 // explain.
2152 m = AnyOf(GreaterThan(30), GreaterThan(20));
2153 EXPECT_EQ("which is 5 more than 20", Explain(m, 25));
2154}
2155
shiqiane35fdd92008-12-10 05:08:54 +00002156// The following predicate function and predicate functor are for
2157// testing the Truly(predicate) matcher.
2158
2159// Returns non-zero if the input is positive. Note that the return
2160// type of this function is not bool. It's OK as Truly() accepts any
2161// unary function or functor whose return type can be implicitly
2162// converted to bool.
2163int IsPositive(double x) {
2164 return x > 0 ? 1 : 0;
2165}
2166
2167// This functor returns true if the input is greater than the given
2168// number.
2169class IsGreaterThan {
2170 public:
2171 explicit IsGreaterThan(int threshold) : threshold_(threshold) {}
2172
2173 bool operator()(int n) const { return n > threshold_; }
zhanyong.wan32de5f52009-12-23 00:13:23 +00002174
shiqiane35fdd92008-12-10 05:08:54 +00002175 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002176 int threshold_;
shiqiane35fdd92008-12-10 05:08:54 +00002177};
2178
2179// For testing Truly().
2180const int foo = 0;
2181
2182// This predicate returns true iff the argument references foo and has
2183// a zero value.
2184bool ReferencesFooAndIsZero(const int& n) {
2185 return (&n == &foo) && (n == 0);
2186}
2187
2188// Tests that Truly(predicate) matches what satisfies the given
2189// predicate.
2190TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {
2191 Matcher<double> m = Truly(IsPositive);
2192 EXPECT_TRUE(m.Matches(2.0));
2193 EXPECT_FALSE(m.Matches(-1.5));
2194}
2195
2196// Tests that Truly(predicate_functor) works too.
2197TEST(TrulyTest, CanBeUsedWithFunctor) {
2198 Matcher<int> m = Truly(IsGreaterThan(5));
2199 EXPECT_TRUE(m.Matches(6));
2200 EXPECT_FALSE(m.Matches(4));
2201}
2202
2203// Tests that Truly(predicate) can describe itself properly.
2204TEST(TrulyTest, CanDescribeSelf) {
2205 Matcher<double> m = Truly(IsPositive);
2206 EXPECT_EQ("satisfies the given predicate",
2207 Describe(m));
2208}
2209
2210// Tests that Truly(predicate) works when the matcher takes its
2211// argument by reference.
2212TEST(TrulyTest, WorksForByRefArguments) {
2213 Matcher<const int&> m = Truly(ReferencesFooAndIsZero);
2214 EXPECT_TRUE(m.Matches(foo));
2215 int n = 0;
2216 EXPECT_FALSE(m.Matches(n));
2217}
2218
2219// Tests that Matches(m) is a predicate satisfied by whatever that
2220// matches matcher m.
2221TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {
2222 EXPECT_TRUE(Matches(Ge(0))(1));
2223 EXPECT_FALSE(Matches(Eq('a'))('b'));
2224}
2225
2226// Tests that Matches(m) works when the matcher takes its argument by
2227// reference.
2228TEST(MatchesTest, WorksOnByRefArguments) {
2229 int m = 0, n = 0;
2230 EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n));
2231 EXPECT_FALSE(Matches(Ref(m))(n));
2232}
2233
2234// Tests that a Matcher on non-reference type can be used in
2235// Matches().
2236TEST(MatchesTest, WorksWithMatcherOnNonRefType) {
2237 Matcher<int> eq5 = Eq(5);
2238 EXPECT_TRUE(Matches(eq5)(5));
2239 EXPECT_FALSE(Matches(eq5)(2));
2240}
2241
zhanyong.wanb8243162009-06-04 05:48:20 +00002242// Tests Value(value, matcher). Since Value() is a simple wrapper for
2243// Matches(), which has been tested already, we don't spend a lot of
2244// effort on testing Value().
2245TEST(ValueTest, WorksWithPolymorphicMatcher) {
2246 EXPECT_TRUE(Value("hi", StartsWith("h")));
2247 EXPECT_FALSE(Value(5, Gt(10)));
2248}
2249
2250TEST(ValueTest, WorksWithMonomorphicMatcher) {
2251 const Matcher<int> is_zero = Eq(0);
2252 EXPECT_TRUE(Value(0, is_zero));
2253 EXPECT_FALSE(Value('a', is_zero));
2254
2255 int n = 0;
2256 const Matcher<const int&> ref_n = Ref(n);
2257 EXPECT_TRUE(Value(n, ref_n));
2258 EXPECT_FALSE(Value(1, ref_n));
2259}
2260
zhanyong.wana862f1d2010-03-15 21:23:04 +00002261TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) {
zhanyong.wan34b034c2010-03-05 21:23:23 +00002262 StringMatchResultListener listener1;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002263 EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002264 EXPECT_EQ("% 2 == 0", listener1.str());
2265
2266 StringMatchResultListener listener2;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002267 EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002268 EXPECT_EQ("", listener2.str());
2269}
2270
zhanyong.wana862f1d2010-03-15 21:23:04 +00002271TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) {
zhanyong.wan34b034c2010-03-05 21:23:23 +00002272 const Matcher<int> is_even = PolymorphicIsEven();
2273 StringMatchResultListener listener1;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002274 EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002275 EXPECT_EQ("% 2 == 0", listener1.str());
2276
2277 const Matcher<const double&> is_zero = Eq(0);
2278 StringMatchResultListener listener2;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002279 EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002280 EXPECT_EQ("", listener2.str());
2281}
2282
zhanyong.wana862f1d2010-03-15 21:23:04 +00002283MATCHER_P(Really, inner_matcher, "") {
2284 return ExplainMatchResult(inner_matcher, arg, result_listener);
2285}
2286
2287TEST(ExplainMatchResultTest, WorksInsideMATCHER) {
2288 EXPECT_THAT(0, Really(Eq(0)));
2289}
2290
zhanyong.wanbf550852009-06-09 06:09:53 +00002291TEST(AllArgsTest, WorksForTuple) {
2292 EXPECT_THAT(make_tuple(1, 2L), AllArgs(Lt()));
2293 EXPECT_THAT(make_tuple(2L, 1), Not(AllArgs(Lt())));
2294}
2295
2296TEST(AllArgsTest, WorksForNonTuple) {
2297 EXPECT_THAT(42, AllArgs(Gt(0)));
2298 EXPECT_THAT('a', Not(AllArgs(Eq('b'))));
2299}
2300
2301class AllArgsHelper {
2302 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002303 AllArgsHelper() {}
2304
zhanyong.wanbf550852009-06-09 06:09:53 +00002305 MOCK_METHOD2(Helper, int(char x, int y));
zhanyong.wan32de5f52009-12-23 00:13:23 +00002306
2307 private:
2308 GTEST_DISALLOW_COPY_AND_ASSIGN_(AllArgsHelper);
zhanyong.wanbf550852009-06-09 06:09:53 +00002309};
2310
2311TEST(AllArgsTest, WorksInWithClause) {
2312 AllArgsHelper helper;
2313 ON_CALL(helper, Helper(_, _))
2314 .With(AllArgs(Lt()))
2315 .WillByDefault(Return(1));
2316 EXPECT_CALL(helper, Helper(_, _));
2317 EXPECT_CALL(helper, Helper(_, _))
2318 .With(AllArgs(Gt()))
2319 .WillOnce(Return(2));
2320
2321 EXPECT_EQ(1, helper.Helper('\1', 2));
2322 EXPECT_EQ(2, helper.Helper('a', 1));
2323}
2324
shiqiane35fdd92008-12-10 05:08:54 +00002325// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2326// matches the matcher.
2327TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {
2328 ASSERT_THAT(5, Ge(2)) << "This should succeed.";
2329 ASSERT_THAT("Foo", EndsWith("oo"));
2330 EXPECT_THAT(2, AllOf(Le(7), Ge(0))) << "This should succeed too.";
2331 EXPECT_THAT("Hello", StartsWith("Hell"));
2332}
2333
2334// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2335// doesn't match the matcher.
2336TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {
2337 // 'n' must be static as it is used in an EXPECT_FATAL_FAILURE(),
2338 // which cannot reference auto variables.
2339 static int n;
2340 n = 5;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002341
2342 // VC++ prior to version 8.0 SP1 has a bug where it will not see any
2343 // functions declared in the namespace scope from within nested classes.
2344 // EXPECT/ASSERT_(NON)FATAL_FAILURE macros use nested classes so that all
2345 // namespace-level functions invoked inside them need to be explicitly
2346 // resolved.
2347 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Gt(10)),
shiqiane35fdd92008-12-10 05:08:54 +00002348 "Value of: n\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002349 "Expected: is > 10\n"
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002350 " Actual: 5");
shiqiane35fdd92008-12-10 05:08:54 +00002351 n = 0;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002352 EXPECT_NONFATAL_FAILURE(
2353 EXPECT_THAT(n, ::testing::AllOf(::testing::Le(7), ::testing::Ge(5))),
2354 "Value of: n\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002355 "Expected: (is <= 7) and (is >= 5)\n"
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002356 " Actual: 0");
shiqiane35fdd92008-12-10 05:08:54 +00002357}
2358
2359// Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument
2360// has a reference type.
2361TEST(MatcherAssertionTest, WorksForByRefArguments) {
2362 // We use a static variable here as EXPECT_FATAL_FAILURE() cannot
2363 // reference auto variables.
2364 static int n;
2365 n = 0;
2366 EXPECT_THAT(n, AllOf(Le(7), Ref(n)));
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002367 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
shiqiane35fdd92008-12-10 05:08:54 +00002368 "Value of: n\n"
2369 "Expected: does not reference the variable @");
2370 // Tests the "Actual" part.
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002371 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002372 "Actual: 0, which is located @");
shiqiane35fdd92008-12-10 05:08:54 +00002373}
2374
zhanyong.wan95b12332009-09-25 18:55:50 +00002375#if !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002376// Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is
2377// monomorphic.
zhanyong.wan95b12332009-09-25 18:55:50 +00002378
2379// ASSERT_THAT("hello", starts_with_he) fails to compile with Nokia's
2380// Symbian compiler: it tries to compile
2381// template<T, U> class MatcherCastImpl { ...
zhanyong.wandb22c222010-01-28 21:52:29 +00002382// virtual bool MatchAndExplain(T x, ...) const {
2383// return source_matcher_.MatchAndExplain(static_cast<U>(x), ...);
zhanyong.wan95b12332009-09-25 18:55:50 +00002384// with U == string and T == const char*
2385// With ASSERT_THAT("hello"...) changed to ASSERT_THAT(string("hello") ... )
2386// the compiler silently crashes with no output.
2387// If MatcherCastImpl is changed to use U(x) instead of static_cast<U>(x)
2388// the code compiles but the converted string is bogus.
shiqiane35fdd92008-12-10 05:08:54 +00002389TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
2390 Matcher<const char*> starts_with_he = StartsWith("he");
2391 ASSERT_THAT("hello", starts_with_he);
2392
2393 Matcher<const string&> ends_with_ok = EndsWith("ok");
2394 ASSERT_THAT("book", ends_with_ok);
2395
2396 Matcher<int> is_greater_than_5 = Gt(5);
2397 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5),
2398 "Value of: 5\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002399 "Expected: is > 5\n"
shiqiane35fdd92008-12-10 05:08:54 +00002400 " Actual: 5");
2401}
zhanyong.wan95b12332009-09-25 18:55:50 +00002402#endif // !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002403
2404// Tests floating-point matchers.
2405template <typename RawType>
2406class FloatingPointTest : public testing::Test {
2407 protected:
2408 typedef typename testing::internal::FloatingPoint<RawType> Floating;
2409 typedef typename Floating::Bits Bits;
2410
2411 virtual void SetUp() {
2412 const size_t max_ulps = Floating::kMaxUlps;
2413
2414 // The bits that represent 0.0.
2415 const Bits zero_bits = Floating(0).bits();
2416
2417 // Makes some numbers close to 0.0.
2418 close_to_positive_zero_ = Floating::ReinterpretBits(zero_bits + max_ulps/2);
2419 close_to_negative_zero_ = -Floating::ReinterpretBits(
2420 zero_bits + max_ulps - max_ulps/2);
2421 further_from_negative_zero_ = -Floating::ReinterpretBits(
2422 zero_bits + max_ulps + 1 - max_ulps/2);
2423
2424 // The bits that represent 1.0.
2425 const Bits one_bits = Floating(1).bits();
2426
2427 // Makes some numbers close to 1.0.
2428 close_to_one_ = Floating::ReinterpretBits(one_bits + max_ulps);
2429 further_from_one_ = Floating::ReinterpretBits(one_bits + max_ulps + 1);
2430
2431 // +infinity.
2432 infinity_ = Floating::Infinity();
2433
2434 // The bits that represent +infinity.
2435 const Bits infinity_bits = Floating(infinity_).bits();
2436
2437 // Makes some numbers close to infinity.
2438 close_to_infinity_ = Floating::ReinterpretBits(infinity_bits - max_ulps);
2439 further_from_infinity_ = Floating::ReinterpretBits(
2440 infinity_bits - max_ulps - 1);
2441
2442 // Makes some NAN's.
2443 nan1_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 1);
2444 nan2_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 200);
2445 }
2446
2447 void TestSize() {
2448 EXPECT_EQ(sizeof(RawType), sizeof(Bits));
2449 }
2450
2451 // A battery of tests for FloatingEqMatcher::Matches.
2452 // matcher_maker is a pointer to a function which creates a FloatingEqMatcher.
2453 void TestMatches(
2454 testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) {
2455 Matcher<RawType> m1 = matcher_maker(0.0);
2456 EXPECT_TRUE(m1.Matches(-0.0));
2457 EXPECT_TRUE(m1.Matches(close_to_positive_zero_));
2458 EXPECT_TRUE(m1.Matches(close_to_negative_zero_));
2459 EXPECT_FALSE(m1.Matches(1.0));
2460
2461 Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_);
2462 EXPECT_FALSE(m2.Matches(further_from_negative_zero_));
2463
2464 Matcher<RawType> m3 = matcher_maker(1.0);
2465 EXPECT_TRUE(m3.Matches(close_to_one_));
2466 EXPECT_FALSE(m3.Matches(further_from_one_));
2467
2468 // Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above.
2469 EXPECT_FALSE(m3.Matches(0.0));
2470
2471 Matcher<RawType> m4 = matcher_maker(-infinity_);
2472 EXPECT_TRUE(m4.Matches(-close_to_infinity_));
2473
2474 Matcher<RawType> m5 = matcher_maker(infinity_);
2475 EXPECT_TRUE(m5.Matches(close_to_infinity_));
2476
2477 // This is interesting as the representations of infinity_ and nan1_
2478 // are only 1 DLP apart.
2479 EXPECT_FALSE(m5.Matches(nan1_));
2480
2481 // matcher_maker can produce a Matcher<const RawType&>, which is needed in
2482 // some cases.
2483 Matcher<const RawType&> m6 = matcher_maker(0.0);
2484 EXPECT_TRUE(m6.Matches(-0.0));
2485 EXPECT_TRUE(m6.Matches(close_to_positive_zero_));
2486 EXPECT_FALSE(m6.Matches(1.0));
2487
2488 // matcher_maker can produce a Matcher<RawType&>, which is needed in some
2489 // cases.
2490 Matcher<RawType&> m7 = matcher_maker(0.0);
2491 RawType x = 0.0;
2492 EXPECT_TRUE(m7.Matches(x));
2493 x = 0.01f;
2494 EXPECT_FALSE(m7.Matches(x));
2495 }
2496
2497 // Pre-calculated numbers to be used by the tests.
2498
2499 static RawType close_to_positive_zero_;
2500 static RawType close_to_negative_zero_;
2501 static RawType further_from_negative_zero_;
2502
2503 static RawType close_to_one_;
2504 static RawType further_from_one_;
2505
2506 static RawType infinity_;
2507 static RawType close_to_infinity_;
2508 static RawType further_from_infinity_;
2509
2510 static RawType nan1_;
2511 static RawType nan2_;
2512};
2513
2514template <typename RawType>
2515RawType FloatingPointTest<RawType>::close_to_positive_zero_;
2516
2517template <typename RawType>
2518RawType FloatingPointTest<RawType>::close_to_negative_zero_;
2519
2520template <typename RawType>
2521RawType FloatingPointTest<RawType>::further_from_negative_zero_;
2522
2523template <typename RawType>
2524RawType FloatingPointTest<RawType>::close_to_one_;
2525
2526template <typename RawType>
2527RawType FloatingPointTest<RawType>::further_from_one_;
2528
2529template <typename RawType>
2530RawType FloatingPointTest<RawType>::infinity_;
2531
2532template <typename RawType>
2533RawType FloatingPointTest<RawType>::close_to_infinity_;
2534
2535template <typename RawType>
2536RawType FloatingPointTest<RawType>::further_from_infinity_;
2537
2538template <typename RawType>
2539RawType FloatingPointTest<RawType>::nan1_;
2540
2541template <typename RawType>
2542RawType FloatingPointTest<RawType>::nan2_;
2543
2544// Instantiate FloatingPointTest for testing floats.
2545typedef FloatingPointTest<float> FloatTest;
2546
2547TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) {
2548 TestMatches(&FloatEq);
2549}
2550
2551TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
2552 TestMatches(&NanSensitiveFloatEq);
2553}
2554
2555TEST_F(FloatTest, FloatEqCannotMatchNaN) {
2556 // FloatEq never matches NaN.
2557 Matcher<float> m = FloatEq(nan1_);
2558 EXPECT_FALSE(m.Matches(nan1_));
2559 EXPECT_FALSE(m.Matches(nan2_));
2560 EXPECT_FALSE(m.Matches(1.0));
2561}
2562
2563TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {
2564 // NanSensitiveFloatEq will match NaN.
2565 Matcher<float> m = NanSensitiveFloatEq(nan1_);
2566 EXPECT_TRUE(m.Matches(nan1_));
2567 EXPECT_TRUE(m.Matches(nan2_));
2568 EXPECT_FALSE(m.Matches(1.0));
2569}
2570
2571TEST_F(FloatTest, FloatEqCanDescribeSelf) {
2572 Matcher<float> m1 = FloatEq(2.0f);
2573 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002574 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002575
2576 Matcher<float> m2 = FloatEq(0.5f);
2577 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002578 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002579
2580 Matcher<float> m3 = FloatEq(nan1_);
2581 EXPECT_EQ("never matches", Describe(m3));
2582 EXPECT_EQ("is anything", DescribeNegation(m3));
2583}
2584
2585TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {
2586 Matcher<float> m1 = NanSensitiveFloatEq(2.0f);
2587 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002588 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002589
2590 Matcher<float> m2 = NanSensitiveFloatEq(0.5f);
2591 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002592 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002593
2594 Matcher<float> m3 = NanSensitiveFloatEq(nan1_);
2595 EXPECT_EQ("is NaN", Describe(m3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002596 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
shiqiane35fdd92008-12-10 05:08:54 +00002597}
2598
2599// Instantiate FloatingPointTest for testing doubles.
2600typedef FloatingPointTest<double> DoubleTest;
2601
2602TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
2603 TestMatches(&DoubleEq);
2604}
2605
2606TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
2607 TestMatches(&NanSensitiveDoubleEq);
2608}
2609
2610TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {
2611 // DoubleEq never matches NaN.
2612 Matcher<double> m = DoubleEq(nan1_);
2613 EXPECT_FALSE(m.Matches(nan1_));
2614 EXPECT_FALSE(m.Matches(nan2_));
2615 EXPECT_FALSE(m.Matches(1.0));
2616}
2617
2618TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {
2619 // NanSensitiveDoubleEq will match NaN.
2620 Matcher<double> m = NanSensitiveDoubleEq(nan1_);
2621 EXPECT_TRUE(m.Matches(nan1_));
2622 EXPECT_TRUE(m.Matches(nan2_));
2623 EXPECT_FALSE(m.Matches(1.0));
2624}
2625
2626TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {
2627 Matcher<double> m1 = DoubleEq(2.0);
2628 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002629 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002630
2631 Matcher<double> m2 = DoubleEq(0.5);
2632 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002633 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002634
2635 Matcher<double> m3 = DoubleEq(nan1_);
2636 EXPECT_EQ("never matches", Describe(m3));
2637 EXPECT_EQ("is anything", DescribeNegation(m3));
2638}
2639
2640TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {
2641 Matcher<double> m1 = NanSensitiveDoubleEq(2.0);
2642 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002643 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002644
2645 Matcher<double> m2 = NanSensitiveDoubleEq(0.5);
2646 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002647 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002648
2649 Matcher<double> m3 = NanSensitiveDoubleEq(nan1_);
2650 EXPECT_EQ("is NaN", Describe(m3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002651 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
shiqiane35fdd92008-12-10 05:08:54 +00002652}
2653
2654TEST(PointeeTest, RawPointer) {
2655 const Matcher<int*> m = Pointee(Ge(0));
2656
2657 int n = 1;
2658 EXPECT_TRUE(m.Matches(&n));
2659 n = -1;
2660 EXPECT_FALSE(m.Matches(&n));
2661 EXPECT_FALSE(m.Matches(NULL));
2662}
2663
2664TEST(PointeeTest, RawPointerToConst) {
2665 const Matcher<const double*> m = Pointee(Ge(0));
2666
2667 double x = 1;
2668 EXPECT_TRUE(m.Matches(&x));
2669 x = -1;
2670 EXPECT_FALSE(m.Matches(&x));
2671 EXPECT_FALSE(m.Matches(NULL));
2672}
2673
2674TEST(PointeeTest, ReferenceToConstRawPointer) {
2675 const Matcher<int* const &> m = Pointee(Ge(0));
2676
2677 int n = 1;
2678 EXPECT_TRUE(m.Matches(&n));
2679 n = -1;
2680 EXPECT_FALSE(m.Matches(&n));
2681 EXPECT_FALSE(m.Matches(NULL));
2682}
2683
2684TEST(PointeeTest, ReferenceToNonConstRawPointer) {
2685 const Matcher<double* &> m = Pointee(Ge(0));
2686
2687 double x = 1.0;
2688 double* p = &x;
2689 EXPECT_TRUE(m.Matches(p));
2690 x = -1;
2691 EXPECT_FALSE(m.Matches(p));
2692 p = NULL;
2693 EXPECT_FALSE(m.Matches(p));
2694}
2695
2696TEST(PointeeTest, NeverMatchesNull) {
2697 const Matcher<const char*> m = Pointee(_);
2698 EXPECT_FALSE(m.Matches(NULL));
2699}
2700
2701// Tests that we can write Pointee(value) instead of Pointee(Eq(value)).
2702TEST(PointeeTest, MatchesAgainstAValue) {
2703 const Matcher<int*> m = Pointee(5);
2704
2705 int n = 5;
2706 EXPECT_TRUE(m.Matches(&n));
2707 n = -1;
2708 EXPECT_FALSE(m.Matches(&n));
2709 EXPECT_FALSE(m.Matches(NULL));
2710}
2711
2712TEST(PointeeTest, CanDescribeSelf) {
2713 const Matcher<int*> m = Pointee(Gt(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002714 EXPECT_EQ("points to a value that is > 3", Describe(m));
2715 EXPECT_EQ("does not point to a value that is > 3",
shiqiane35fdd92008-12-10 05:08:54 +00002716 DescribeNegation(m));
2717}
2718
shiqiane35fdd92008-12-10 05:08:54 +00002719TEST(PointeeTest, CanExplainMatchResult) {
2720 const Matcher<const string*> m = Pointee(StartsWith("Hi"));
2721
2722 EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL)));
2723
2724 const Matcher<int*> m2 = Pointee(GreaterThan(1));
2725 int n = 3;
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002726 EXPECT_EQ("which points to 3, which is 2 more than 1",
2727 Explain(m2, &n));
2728}
2729
2730TEST(PointeeTest, AlwaysExplainsPointee) {
2731 const Matcher<int*> m = Pointee(0);
2732 int n = 42;
2733 EXPECT_EQ("which points to 42", Explain(m, &n));
shiqiane35fdd92008-12-10 05:08:54 +00002734}
2735
2736// An uncopyable class.
2737class Uncopyable {
2738 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002739 explicit Uncopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00002740
2741 int value() const { return value_; }
2742 private:
2743 const int value_;
2744 GTEST_DISALLOW_COPY_AND_ASSIGN_(Uncopyable);
2745};
2746
2747// Returns true iff x.value() is positive.
2748bool ValueIsPositive(const Uncopyable& x) { return x.value() > 0; }
2749
2750// A user-defined struct for testing Field().
2751struct AStruct {
2752 AStruct() : x(0), y(1.0), z(5), p(NULL) {}
2753 AStruct(const AStruct& rhs)
2754 : x(rhs.x), y(rhs.y), z(rhs.z.value()), p(rhs.p) {}
2755
2756 int x; // A non-const field.
2757 const double y; // A const field.
2758 Uncopyable z; // An uncopyable field.
2759 const char* p; // A pointer field.
zhanyong.wan32de5f52009-12-23 00:13:23 +00002760
2761 private:
2762 GTEST_DISALLOW_ASSIGN_(AStruct);
shiqiane35fdd92008-12-10 05:08:54 +00002763};
2764
2765// A derived struct for testing Field().
2766struct DerivedStruct : public AStruct {
2767 char ch;
zhanyong.wan32de5f52009-12-23 00:13:23 +00002768
2769 private:
2770 GTEST_DISALLOW_ASSIGN_(DerivedStruct);
shiqiane35fdd92008-12-10 05:08:54 +00002771};
2772
2773// Tests that Field(&Foo::field, ...) works when field is non-const.
2774TEST(FieldTest, WorksForNonConstField) {
2775 Matcher<AStruct> m = Field(&AStruct::x, Ge(0));
2776
2777 AStruct a;
2778 EXPECT_TRUE(m.Matches(a));
2779 a.x = -1;
2780 EXPECT_FALSE(m.Matches(a));
2781}
2782
2783// Tests that Field(&Foo::field, ...) works when field is const.
2784TEST(FieldTest, WorksForConstField) {
2785 AStruct a;
2786
2787 Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0));
2788 EXPECT_TRUE(m.Matches(a));
2789 m = Field(&AStruct::y, Le(0.0));
2790 EXPECT_FALSE(m.Matches(a));
2791}
2792
2793// Tests that Field(&Foo::field, ...) works when field is not copyable.
2794TEST(FieldTest, WorksForUncopyableField) {
2795 AStruct a;
2796
2797 Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive));
2798 EXPECT_TRUE(m.Matches(a));
2799 m = Field(&AStruct::z, Not(Truly(ValueIsPositive)));
2800 EXPECT_FALSE(m.Matches(a));
2801}
2802
2803// Tests that Field(&Foo::field, ...) works when field is a pointer.
2804TEST(FieldTest, WorksForPointerField) {
2805 // Matching against NULL.
2806 Matcher<AStruct> m = Field(&AStruct::p, static_cast<const char*>(NULL));
2807 AStruct a;
2808 EXPECT_TRUE(m.Matches(a));
2809 a.p = "hi";
2810 EXPECT_FALSE(m.Matches(a));
2811
2812 // Matching a pointer that is not NULL.
2813 m = Field(&AStruct::p, StartsWith("hi"));
2814 a.p = "hill";
2815 EXPECT_TRUE(m.Matches(a));
2816 a.p = "hole";
2817 EXPECT_FALSE(m.Matches(a));
2818}
2819
2820// Tests that Field() works when the object is passed by reference.
2821TEST(FieldTest, WorksForByRefArgument) {
2822 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2823
2824 AStruct a;
2825 EXPECT_TRUE(m.Matches(a));
2826 a.x = -1;
2827 EXPECT_FALSE(m.Matches(a));
2828}
2829
2830// Tests that Field(&Foo::field, ...) works when the argument's type
2831// is a sub-type of Foo.
2832TEST(FieldTest, WorksForArgumentOfSubType) {
2833 // Note that the matcher expects DerivedStruct but we say AStruct
2834 // inside Field().
2835 Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0));
2836
2837 DerivedStruct d;
2838 EXPECT_TRUE(m.Matches(d));
2839 d.x = -1;
2840 EXPECT_FALSE(m.Matches(d));
2841}
2842
2843// Tests that Field(&Foo::field, m) works when field's type and m's
2844// argument type are compatible but not the same.
2845TEST(FieldTest, WorksForCompatibleMatcherType) {
2846 // The field is an int, but the inner matcher expects a signed char.
2847 Matcher<const AStruct&> m = Field(&AStruct::x,
2848 Matcher<signed char>(Ge(0)));
2849
2850 AStruct a;
2851 EXPECT_TRUE(m.Matches(a));
2852 a.x = -1;
2853 EXPECT_FALSE(m.Matches(a));
2854}
2855
2856// Tests that Field() can describe itself.
2857TEST(FieldTest, CanDescribeSelf) {
2858 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2859
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002860 EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
2861 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00002862}
2863
2864// Tests that Field() can explain the match result.
2865TEST(FieldTest, CanExplainMatchResult) {
2866 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2867
2868 AStruct a;
2869 a.x = 1;
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002870 EXPECT_EQ("whose given field is 1", Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00002871
2872 m = Field(&AStruct::x, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002873 EXPECT_EQ("whose given field is 1, which is 1 more than 0", Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00002874}
2875
2876// Tests that Field() works when the argument is a pointer to const.
2877TEST(FieldForPointerTest, WorksForPointerToConst) {
2878 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2879
2880 AStruct a;
2881 EXPECT_TRUE(m.Matches(&a));
2882 a.x = -1;
2883 EXPECT_FALSE(m.Matches(&a));
2884}
2885
2886// Tests that Field() works when the argument is a pointer to non-const.
2887TEST(FieldForPointerTest, WorksForPointerToNonConst) {
2888 Matcher<AStruct*> m = Field(&AStruct::x, Ge(0));
2889
2890 AStruct a;
2891 EXPECT_TRUE(m.Matches(&a));
2892 a.x = -1;
2893 EXPECT_FALSE(m.Matches(&a));
2894}
2895
zhanyong.wan6953a722010-01-13 05:15:07 +00002896// Tests that Field() works when the argument is a reference to a const pointer.
2897TEST(FieldForPointerTest, WorksForReferenceToConstPointer) {
2898 Matcher<AStruct* const&> m = Field(&AStruct::x, Ge(0));
2899
2900 AStruct a;
2901 EXPECT_TRUE(m.Matches(&a));
2902 a.x = -1;
2903 EXPECT_FALSE(m.Matches(&a));
2904}
2905
shiqiane35fdd92008-12-10 05:08:54 +00002906// Tests that Field() does not match the NULL pointer.
2907TEST(FieldForPointerTest, DoesNotMatchNull) {
2908 Matcher<const AStruct*> m = Field(&AStruct::x, _);
2909 EXPECT_FALSE(m.Matches(NULL));
2910}
2911
2912// Tests that Field(&Foo::field, ...) works when the argument's type
2913// is a sub-type of const Foo*.
2914TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
2915 // Note that the matcher expects DerivedStruct but we say AStruct
2916 // inside Field().
2917 Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0));
2918
2919 DerivedStruct d;
2920 EXPECT_TRUE(m.Matches(&d));
2921 d.x = -1;
2922 EXPECT_FALSE(m.Matches(&d));
2923}
2924
2925// Tests that Field() can describe itself when used to match a pointer.
2926TEST(FieldForPointerTest, CanDescribeSelf) {
2927 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2928
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002929 EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
2930 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00002931}
2932
2933// Tests that Field() can explain the result of matching a pointer.
2934TEST(FieldForPointerTest, CanExplainMatchResult) {
2935 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2936
2937 AStruct a;
2938 a.x = 1;
2939 EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(NULL)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002940 EXPECT_EQ("which points to an object whose given field is 1", Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00002941
2942 m = Field(&AStruct::x, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002943 EXPECT_EQ("which points to an object whose given field is 1, "
2944 "which is 1 more than 0", Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00002945}
2946
2947// A user-defined class for testing Property().
2948class AClass {
2949 public:
2950 AClass() : n_(0) {}
2951
2952 // A getter that returns a non-reference.
2953 int n() const { return n_; }
2954
2955 void set_n(int new_n) { n_ = new_n; }
2956
2957 // A getter that returns a reference to const.
2958 const string& s() const { return s_; }
2959
2960 void set_s(const string& new_s) { s_ = new_s; }
2961
2962 // A getter that returns a reference to non-const.
2963 double& x() const { return x_; }
2964 private:
2965 int n_;
2966 string s_;
2967
2968 static double x_;
2969};
2970
2971double AClass::x_ = 0.0;
2972
2973// A derived class for testing Property().
2974class DerivedClass : public AClass {
2975 private:
2976 int k_;
2977};
2978
2979// Tests that Property(&Foo::property, ...) works when property()
2980// returns a non-reference.
2981TEST(PropertyTest, WorksForNonReferenceProperty) {
2982 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
2983
2984 AClass a;
2985 a.set_n(1);
2986 EXPECT_TRUE(m.Matches(a));
2987
2988 a.set_n(-1);
2989 EXPECT_FALSE(m.Matches(a));
2990}
2991
2992// Tests that Property(&Foo::property, ...) works when property()
2993// returns a reference to const.
2994TEST(PropertyTest, WorksForReferenceToConstProperty) {
2995 Matcher<const AClass&> m = Property(&AClass::s, StartsWith("hi"));
2996
2997 AClass a;
2998 a.set_s("hill");
2999 EXPECT_TRUE(m.Matches(a));
3000
3001 a.set_s("hole");
3002 EXPECT_FALSE(m.Matches(a));
3003}
3004
3005// Tests that Property(&Foo::property, ...) works when property()
3006// returns a reference to non-const.
3007TEST(PropertyTest, WorksForReferenceToNonConstProperty) {
3008 double x = 0.0;
3009 AClass a;
3010
3011 Matcher<const AClass&> m = Property(&AClass::x, Ref(x));
3012 EXPECT_FALSE(m.Matches(a));
3013
3014 m = Property(&AClass::x, Not(Ref(x)));
3015 EXPECT_TRUE(m.Matches(a));
3016}
3017
3018// Tests that Property(&Foo::property, ...) works when the argument is
3019// passed by value.
3020TEST(PropertyTest, WorksForByValueArgument) {
3021 Matcher<AClass> m = Property(&AClass::s, StartsWith("hi"));
3022
3023 AClass a;
3024 a.set_s("hill");
3025 EXPECT_TRUE(m.Matches(a));
3026
3027 a.set_s("hole");
3028 EXPECT_FALSE(m.Matches(a));
3029}
3030
3031// Tests that Property(&Foo::property, ...) works when the argument's
3032// type is a sub-type of Foo.
3033TEST(PropertyTest, WorksForArgumentOfSubType) {
3034 // The matcher expects a DerivedClass, but inside the Property() we
3035 // say AClass.
3036 Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0));
3037
3038 DerivedClass d;
3039 d.set_n(1);
3040 EXPECT_TRUE(m.Matches(d));
3041
3042 d.set_n(-1);
3043 EXPECT_FALSE(m.Matches(d));
3044}
3045
3046// Tests that Property(&Foo::property, m) works when property()'s type
3047// and m's argument type are compatible but different.
3048TEST(PropertyTest, WorksForCompatibleMatcherType) {
3049 // n() returns an int but the inner matcher expects a signed char.
3050 Matcher<const AClass&> m = Property(&AClass::n,
3051 Matcher<signed char>(Ge(0)));
3052
3053 AClass a;
3054 EXPECT_TRUE(m.Matches(a));
3055 a.set_n(-1);
3056 EXPECT_FALSE(m.Matches(a));
3057}
3058
3059// Tests that Property() can describe itself.
3060TEST(PropertyTest, CanDescribeSelf) {
3061 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3062
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003063 EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
3064 EXPECT_EQ("is an object whose given property isn't >= 0",
3065 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003066}
3067
3068// Tests that Property() can explain the match result.
3069TEST(PropertyTest, CanExplainMatchResult) {
3070 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3071
3072 AClass a;
3073 a.set_n(1);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003074 EXPECT_EQ("whose given property is 1", Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003075
3076 m = Property(&AClass::n, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003077 EXPECT_EQ("whose given property is 1, which is 1 more than 0", Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003078}
3079
3080// Tests that Property() works when the argument is a pointer to const.
3081TEST(PropertyForPointerTest, WorksForPointerToConst) {
3082 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3083
3084 AClass a;
3085 a.set_n(1);
3086 EXPECT_TRUE(m.Matches(&a));
3087
3088 a.set_n(-1);
3089 EXPECT_FALSE(m.Matches(&a));
3090}
3091
3092// Tests that Property() works when the argument is a pointer to non-const.
3093TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
3094 Matcher<AClass*> m = Property(&AClass::s, StartsWith("hi"));
3095
3096 AClass a;
3097 a.set_s("hill");
3098 EXPECT_TRUE(m.Matches(&a));
3099
3100 a.set_s("hole");
3101 EXPECT_FALSE(m.Matches(&a));
3102}
3103
zhanyong.wan6953a722010-01-13 05:15:07 +00003104// Tests that Property() works when the argument is a reference to a
3105// const pointer.
3106TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) {
3107 Matcher<AClass* const&> m = Property(&AClass::s, StartsWith("hi"));
3108
3109 AClass a;
3110 a.set_s("hill");
3111 EXPECT_TRUE(m.Matches(&a));
3112
3113 a.set_s("hole");
3114 EXPECT_FALSE(m.Matches(&a));
3115}
3116
shiqiane35fdd92008-12-10 05:08:54 +00003117// Tests that Property() does not match the NULL pointer.
3118TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
3119 Matcher<const AClass*> m = Property(&AClass::x, _);
3120 EXPECT_FALSE(m.Matches(NULL));
3121}
3122
3123// Tests that Property(&Foo::property, ...) works when the argument's
3124// type is a sub-type of const Foo*.
3125TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
3126 // The matcher expects a DerivedClass, but inside the Property() we
3127 // say AClass.
3128 Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0));
3129
3130 DerivedClass d;
3131 d.set_n(1);
3132 EXPECT_TRUE(m.Matches(&d));
3133
3134 d.set_n(-1);
3135 EXPECT_FALSE(m.Matches(&d));
3136}
3137
3138// Tests that Property() can describe itself when used to match a pointer.
3139TEST(PropertyForPointerTest, CanDescribeSelf) {
3140 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3141
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003142 EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
3143 EXPECT_EQ("is an object whose given property isn't >= 0",
3144 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003145}
3146
3147// Tests that Property() can explain the result of matching a pointer.
3148TEST(PropertyForPointerTest, CanExplainMatchResult) {
3149 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3150
3151 AClass a;
3152 a.set_n(1);
3153 EXPECT_EQ("", Explain(m, static_cast<const AClass*>(NULL)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003154 EXPECT_EQ("which points to an object whose given property is 1",
3155 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003156
3157 m = Property(&AClass::n, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003158 EXPECT_EQ("which points to an object whose given property is 1, "
3159 "which is 1 more than 0", Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003160}
3161
3162// Tests ResultOf.
3163
3164// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3165// function pointer.
3166string IntToStringFunction(int input) { return input == 1 ? "foo" : "bar"; }
3167
3168TEST(ResultOfTest, WorksForFunctionPointers) {
3169 Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(string("foo")));
3170
3171 EXPECT_TRUE(matcher.Matches(1));
3172 EXPECT_FALSE(matcher.Matches(2));
3173}
3174
3175// Tests that ResultOf() can describe itself.
3176TEST(ResultOfTest, CanDescribeItself) {
3177 Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo"));
3178
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003179 EXPECT_EQ("is mapped by the given callable to a value that "
3180 "is equal to \"foo\"", Describe(matcher));
3181 EXPECT_EQ("is mapped by the given callable to a value that "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003182 "isn't equal to \"foo\"", DescribeNegation(matcher));
shiqiane35fdd92008-12-10 05:08:54 +00003183}
3184
3185// Tests that ResultOf() can explain the match result.
3186int IntFunction(int input) { return input == 42 ? 80 : 90; }
3187
3188TEST(ResultOfTest, CanExplainMatchResult) {
3189 Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003190 EXPECT_EQ("which is mapped by the given callable to 90",
3191 Explain(matcher, 36));
shiqiane35fdd92008-12-10 05:08:54 +00003192
3193 matcher = ResultOf(&IntFunction, GreaterThan(85));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003194 EXPECT_EQ("which is mapped by the given callable to 90, "
3195 "which is 5 more than 85", Explain(matcher, 36));
shiqiane35fdd92008-12-10 05:08:54 +00003196}
3197
3198// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3199// returns a non-reference.
3200TEST(ResultOfTest, WorksForNonReferenceResults) {
3201 Matcher<int> matcher = ResultOf(&IntFunction, Eq(80));
3202
3203 EXPECT_TRUE(matcher.Matches(42));
3204 EXPECT_FALSE(matcher.Matches(36));
3205}
3206
3207// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3208// returns a reference to non-const.
3209double& DoubleFunction(double& input) { return input; }
3210
3211Uncopyable& RefUncopyableFunction(Uncopyable& obj) {
3212 return obj;
3213}
3214
3215TEST(ResultOfTest, WorksForReferenceToNonConstResults) {
3216 double x = 3.14;
3217 double x2 = x;
3218 Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x));
3219
3220 EXPECT_TRUE(matcher.Matches(x));
3221 EXPECT_FALSE(matcher.Matches(x2));
3222
3223 // Test that ResultOf works with uncopyable objects
3224 Uncopyable obj(0);
3225 Uncopyable obj2(0);
3226 Matcher<Uncopyable&> matcher2 =
3227 ResultOf(&RefUncopyableFunction, Ref(obj));
3228
3229 EXPECT_TRUE(matcher2.Matches(obj));
3230 EXPECT_FALSE(matcher2.Matches(obj2));
3231}
3232
3233// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3234// returns a reference to const.
3235const string& StringFunction(const string& input) { return input; }
3236
3237TEST(ResultOfTest, WorksForReferenceToConstResults) {
3238 string s = "foo";
3239 string s2 = s;
3240 Matcher<const string&> matcher = ResultOf(&StringFunction, Ref(s));
3241
3242 EXPECT_TRUE(matcher.Matches(s));
3243 EXPECT_FALSE(matcher.Matches(s2));
3244}
3245
3246// Tests that ResultOf(f, m) works when f(x) and m's
3247// argument types are compatible but different.
3248TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {
3249 // IntFunction() returns int but the inner matcher expects a signed char.
3250 Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85)));
3251
3252 EXPECT_TRUE(matcher.Matches(36));
3253 EXPECT_FALSE(matcher.Matches(42));
3254}
3255
shiqiane35fdd92008-12-10 05:08:54 +00003256// Tests that the program aborts when ResultOf is passed
3257// a NULL function pointer.
3258TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +00003259 EXPECT_DEATH_IF_SUPPORTED(
shiqiane35fdd92008-12-10 05:08:54 +00003260 ResultOf(static_cast<string(*)(int)>(NULL), Eq(string("foo"))),
3261 "NULL function pointer is passed into ResultOf\\(\\)\\.");
3262}
shiqiane35fdd92008-12-10 05:08:54 +00003263
3264// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3265// function reference.
3266TEST(ResultOfTest, WorksForFunctionReferences) {
3267 Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq("foo"));
3268 EXPECT_TRUE(matcher.Matches(1));
3269 EXPECT_FALSE(matcher.Matches(2));
3270}
3271
3272// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3273// function object.
3274struct Functor : public ::std::unary_function<int, string> {
3275 result_type operator()(argument_type input) const {
3276 return IntToStringFunction(input);
3277 }
3278};
3279
3280TEST(ResultOfTest, WorksForFunctors) {
3281 Matcher<int> matcher = ResultOf(Functor(), Eq(string("foo")));
3282
3283 EXPECT_TRUE(matcher.Matches(1));
3284 EXPECT_FALSE(matcher.Matches(2));
3285}
3286
3287// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3288// functor with more then one operator() defined. ResultOf() must work
3289// for each defined operator().
3290struct PolymorphicFunctor {
3291 typedef int result_type;
3292 int operator()(int n) { return n; }
3293 int operator()(const char* s) { return static_cast<int>(strlen(s)); }
3294};
3295
3296TEST(ResultOfTest, WorksForPolymorphicFunctors) {
3297 Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5));
3298
3299 EXPECT_TRUE(matcher_int.Matches(10));
3300 EXPECT_FALSE(matcher_int.Matches(2));
3301
3302 Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5));
3303
3304 EXPECT_TRUE(matcher_string.Matches("long string"));
3305 EXPECT_FALSE(matcher_string.Matches("shrt"));
3306}
3307
3308const int* ReferencingFunction(const int& n) { return &n; }
3309
3310struct ReferencingFunctor {
3311 typedef const int* result_type;
3312 result_type operator()(const int& n) { return &n; }
3313};
3314
3315TEST(ResultOfTest, WorksForReferencingCallables) {
3316 const int n = 1;
3317 const int n2 = 1;
3318 Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n));
3319 EXPECT_TRUE(matcher2.Matches(n));
3320 EXPECT_FALSE(matcher2.Matches(n2));
3321
3322 Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n));
3323 EXPECT_TRUE(matcher3.Matches(n));
3324 EXPECT_FALSE(matcher3.Matches(n2));
3325}
3326
shiqiane35fdd92008-12-10 05:08:54 +00003327class DivisibleByImpl {
3328 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003329 explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {}
shiqiane35fdd92008-12-10 05:08:54 +00003330
zhanyong.wandb22c222010-01-28 21:52:29 +00003331 // For testing using ExplainMatchResultTo() with polymorphic matchers.
shiqiane35fdd92008-12-10 05:08:54 +00003332 template <typename T>
zhanyong.wandb22c222010-01-28 21:52:29 +00003333 bool MatchAndExplain(const T& n, MatchResultListener* listener) const {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003334 *listener << "which is " << (n % divider_) << " modulo "
zhanyong.wandb22c222010-01-28 21:52:29 +00003335 << divider_;
shiqiane35fdd92008-12-10 05:08:54 +00003336 return (n % divider_) == 0;
3337 }
3338
3339 void DescribeTo(::std::ostream* os) const {
3340 *os << "is divisible by " << divider_;
3341 }
3342
3343 void DescribeNegationTo(::std::ostream* os) const {
3344 *os << "is not divisible by " << divider_;
3345 }
3346
zhanyong.wan32de5f52009-12-23 00:13:23 +00003347 void set_divider(int a_divider) { divider_ = a_divider; }
shiqiane35fdd92008-12-10 05:08:54 +00003348 int divider() const { return divider_; }
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003349
shiqiane35fdd92008-12-10 05:08:54 +00003350 private:
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003351 int divider_;
shiqiane35fdd92008-12-10 05:08:54 +00003352};
3353
shiqiane35fdd92008-12-10 05:08:54 +00003354PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) {
3355 return MakePolymorphicMatcher(DivisibleByImpl(n));
3356}
3357
3358// Tests that when AllOf() fails, only the first failing matcher is
3359// asked to explain why.
3360TEST(ExplainMatchResultTest, AllOf_False_False) {
3361 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003362 EXPECT_EQ("which is 1 modulo 4", Explain(m, 5));
shiqiane35fdd92008-12-10 05:08:54 +00003363}
3364
3365// Tests that when AllOf() fails, only the first failing matcher is
3366// asked to explain why.
3367TEST(ExplainMatchResultTest, AllOf_False_True) {
3368 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003369 EXPECT_EQ("which is 2 modulo 4", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003370}
3371
3372// Tests that when AllOf() fails, only the first failing matcher is
3373// asked to explain why.
3374TEST(ExplainMatchResultTest, AllOf_True_False) {
3375 const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003376 EXPECT_EQ("which is 2 modulo 3", Explain(m, 5));
shiqiane35fdd92008-12-10 05:08:54 +00003377}
3378
3379// Tests that when AllOf() succeeds, all matchers are asked to explain
3380// why.
3381TEST(ExplainMatchResultTest, AllOf_True_True) {
3382 const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003383 EXPECT_EQ("which is 0 modulo 2, and which is 0 modulo 3", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003384}
3385
3386TEST(ExplainMatchResultTest, AllOf_True_True_2) {
3387 const Matcher<int> m = AllOf(Ge(2), Le(3));
3388 EXPECT_EQ("", Explain(m, 2));
3389}
3390
3391TEST(ExplainmatcherResultTest, MonomorphicMatcher) {
3392 const Matcher<int> m = GreaterThan(5);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003393 EXPECT_EQ("which is 1 more than 5", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003394}
3395
3396// The following two tests verify that values without a public copy
3397// ctor can be used as arguments to matchers like Eq(), Ge(), and etc
3398// with the help of ByRef().
3399
3400class NotCopyable {
3401 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003402 explicit NotCopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00003403
3404 int value() const { return value_; }
3405
3406 bool operator==(const NotCopyable& rhs) const {
3407 return value() == rhs.value();
3408 }
3409
3410 bool operator>=(const NotCopyable& rhs) const {
3411 return value() >= rhs.value();
3412 }
3413 private:
3414 int value_;
3415
3416 GTEST_DISALLOW_COPY_AND_ASSIGN_(NotCopyable);
3417};
3418
3419TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) {
3420 const NotCopyable const_value1(1);
3421 const Matcher<const NotCopyable&> m = Eq(ByRef(const_value1));
3422
3423 const NotCopyable n1(1), n2(2);
3424 EXPECT_TRUE(m.Matches(n1));
3425 EXPECT_FALSE(m.Matches(n2));
3426}
3427
3428TEST(ByRefTest, AllowsNotCopyableValueInMatchers) {
3429 NotCopyable value2(2);
3430 const Matcher<NotCopyable&> m = Ge(ByRef(value2));
3431
3432 NotCopyable n1(1), n2(2);
3433 EXPECT_FALSE(m.Matches(n1));
3434 EXPECT_TRUE(m.Matches(n2));
3435}
3436
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003437#if GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003438// Tests ContainerEq with different container types, and
3439// different element types.
3440
3441template <typename T>
zhanyong.wanb8243162009-06-04 05:48:20 +00003442class ContainerEqTest : public testing::Test {};
zhanyong.wan6a896b52009-01-16 01:13:50 +00003443
3444typedef testing::Types<
3445 std::set<int>,
3446 std::vector<size_t>,
3447 std::multiset<size_t>,
3448 std::list<int> >
3449 ContainerEqTestTypes;
3450
3451TYPED_TEST_CASE(ContainerEqTest, ContainerEqTestTypes);
3452
3453// Tests that the filled container is equal to itself.
3454TYPED_TEST(ContainerEqTest, EqualsSelf) {
3455 static const int vals[] = {1, 1, 2, 3, 5, 8};
3456 TypeParam my_set(vals, vals + 6);
3457 const Matcher<TypeParam> m = ContainerEq(my_set);
3458 EXPECT_TRUE(m.Matches(my_set));
3459 EXPECT_EQ("", Explain(m, my_set));
3460}
3461
3462// Tests that missing values are reported.
3463TYPED_TEST(ContainerEqTest, ValueMissing) {
3464 static const int vals[] = {1, 1, 2, 3, 5, 8};
3465 static const int test_vals[] = {2, 1, 8, 5};
3466 TypeParam my_set(vals, vals + 6);
3467 TypeParam test_set(test_vals, test_vals + 4);
3468 const Matcher<TypeParam> m = ContainerEq(my_set);
3469 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003470 EXPECT_EQ("which doesn't have these expected elements: 3",
3471 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003472}
3473
3474// Tests that added values are reported.
3475TYPED_TEST(ContainerEqTest, ValueAdded) {
3476 static const int vals[] = {1, 1, 2, 3, 5, 8};
3477 static const int test_vals[] = {1, 2, 3, 5, 8, 46};
3478 TypeParam my_set(vals, vals + 6);
3479 TypeParam test_set(test_vals, test_vals + 6);
3480 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3481 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003482 EXPECT_EQ("which has these unexpected elements: 46", Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003483}
3484
3485// Tests that added and missing values are reported together.
3486TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) {
3487 static const int vals[] = {1, 1, 2, 3, 5, 8};
3488 static const int test_vals[] = {1, 2, 3, 8, 46};
3489 TypeParam my_set(vals, vals + 6);
3490 TypeParam test_set(test_vals, test_vals + 5);
3491 const Matcher<TypeParam> m = ContainerEq(my_set);
3492 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003493 EXPECT_EQ("which has these unexpected elements: 46,\n"
3494 "and doesn't have these expected elements: 5",
3495 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003496}
3497
3498// Tests duplicated value -- expect no explanation.
3499TYPED_TEST(ContainerEqTest, DuplicateDifference) {
3500 static const int vals[] = {1, 1, 2, 3, 5, 8};
3501 static const int test_vals[] = {1, 2, 3, 5, 8};
3502 TypeParam my_set(vals, vals + 6);
3503 TypeParam test_set(test_vals, test_vals + 5);
3504 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3505 // Depending on the container, match may be true or false
3506 // But in any case there should be no explanation.
3507 EXPECT_EQ("", Explain(m, test_set));
3508}
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003509#endif // GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003510
3511// Tests that mutliple missing values are reported.
3512// Using just vector here, so order is predicatble.
3513TEST(ContainerEqExtraTest, MultipleValuesMissing) {
3514 static const int vals[] = {1, 1, 2, 3, 5, 8};
3515 static const int test_vals[] = {2, 1, 5};
3516 std::vector<int> my_set(vals, vals + 6);
3517 std::vector<int> test_set(test_vals, test_vals + 3);
3518 const Matcher<std::vector<int> > m = ContainerEq(my_set);
3519 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003520 EXPECT_EQ("which doesn't have these expected elements: 3, 8",
3521 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003522}
3523
3524// Tests that added values are reported.
3525// Using just vector here, so order is predicatble.
3526TEST(ContainerEqExtraTest, MultipleValuesAdded) {
3527 static const int vals[] = {1, 1, 2, 3, 5, 8};
3528 static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46};
3529 std::list<size_t> my_set(vals, vals + 6);
3530 std::list<size_t> test_set(test_vals, test_vals + 7);
3531 const Matcher<const std::list<size_t>&> m = ContainerEq(my_set);
3532 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003533 EXPECT_EQ("which has these unexpected elements: 92, 46",
3534 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003535}
3536
3537// Tests that added and missing values are reported together.
3538TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) {
3539 static const int vals[] = {1, 1, 2, 3, 5, 8};
3540 static const int test_vals[] = {1, 2, 3, 92, 46};
3541 std::list<size_t> my_set(vals, vals + 6);
3542 std::list<size_t> test_set(test_vals, test_vals + 5);
3543 const Matcher<const std::list<size_t> > m = ContainerEq(my_set);
3544 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003545 EXPECT_EQ("which has these unexpected elements: 92, 46,\n"
3546 "and doesn't have these expected elements: 5, 8",
zhanyong.wan6a896b52009-01-16 01:13:50 +00003547 Explain(m, test_set));
3548}
3549
3550// Tests to see that duplicate elements are detected,
3551// but (as above) not reported in the explanation.
3552TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) {
3553 static const int vals[] = {1, 1, 2, 3, 5, 8};
3554 static const int test_vals[] = {1, 2, 3, 5, 8};
3555 std::vector<int> my_set(vals, vals + 6);
3556 std::vector<int> test_set(test_vals, test_vals + 5);
3557 const Matcher<std::vector<int> > m = ContainerEq(my_set);
3558 EXPECT_TRUE(m.Matches(my_set));
3559 EXPECT_FALSE(m.Matches(test_set));
3560 // There is nothing to report when both sets contain all the same values.
3561 EXPECT_EQ("", Explain(m, test_set));
3562}
3563
3564// Tests that ContainerEq works for non-trivial associative containers,
3565// like maps.
3566TEST(ContainerEqExtraTest, WorksForMaps) {
3567 std::map<int, std::string> my_map;
3568 my_map[0] = "a";
3569 my_map[1] = "b";
3570
3571 std::map<int, std::string> test_map;
3572 test_map[0] = "aa";
3573 test_map[1] = "b";
3574
3575 const Matcher<const std::map<int, std::string>&> m = ContainerEq(my_map);
3576 EXPECT_TRUE(m.Matches(my_map));
3577 EXPECT_FALSE(m.Matches(test_map));
3578
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003579 EXPECT_EQ("which has these unexpected elements: (0, \"aa\"),\n"
3580 "and doesn't have these expected elements: (0, \"a\")",
zhanyong.wan6a896b52009-01-16 01:13:50 +00003581 Explain(m, test_map));
3582}
3583
zhanyong.wanb8243162009-06-04 05:48:20 +00003584TEST(ContainerEqExtraTest, WorksForNativeArray) {
3585 int a1[] = { 1, 2, 3 };
3586 int a2[] = { 1, 2, 3 };
3587 int b[] = { 1, 2, 4 };
3588
3589 EXPECT_THAT(a1, ContainerEq(a2));
3590 EXPECT_THAT(a1, Not(ContainerEq(b)));
3591}
3592
3593TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) {
3594 const char a1[][3] = { "hi", "lo" };
3595 const char a2[][3] = { "hi", "lo" };
3596 const char b[][3] = { "lo", "hi" };
3597
3598 // Tests using ContainerEq() in the first dimension.
3599 EXPECT_THAT(a1, ContainerEq(a2));
3600 EXPECT_THAT(a1, Not(ContainerEq(b)));
3601
3602 // Tests using ContainerEq() in the second dimension.
3603 EXPECT_THAT(a1, ElementsAre(ContainerEq(a2[0]), ContainerEq(a2[1])));
3604 EXPECT_THAT(a1, ElementsAre(Not(ContainerEq(b[0])), ContainerEq(a2[1])));
3605}
3606
3607TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) {
3608 const int a1[] = { 1, 2, 3 };
3609 const int a2[] = { 1, 2, 3 };
3610 const int b[] = { 1, 2, 3, 4 };
3611
zhanyong.wan2661c682009-06-09 05:42:12 +00003612 const int* const p1 = a1;
3613 EXPECT_THAT(make_tuple(p1, 3), ContainerEq(a2));
3614 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(b)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003615
3616 const int c[] = { 1, 3, 2 };
zhanyong.wan2661c682009-06-09 05:42:12 +00003617 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(c)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003618}
3619
3620TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) {
3621 std::string a1[][3] = {
3622 { "hi", "hello", "ciao" },
3623 { "bye", "see you", "ciao" }
3624 };
3625
3626 std::string a2[][3] = {
3627 { "hi", "hello", "ciao" },
3628 { "bye", "see you", "ciao" }
3629 };
3630
3631 const Matcher<const std::string(&)[2][3]> m = ContainerEq(a2);
3632 EXPECT_THAT(a1, m);
3633
3634 a2[0][0] = "ha";
3635 EXPECT_THAT(a1, m);
3636}
3637
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003638// Tests GetParamIndex().
3639
3640TEST(GetParamIndexTest, WorksForEmptyParamList) {
3641 const char* params[] = { NULL };
3642 EXPECT_EQ(kTupleInterpolation, GetParamIndex(params, "*"));
3643 EXPECT_EQ(kInvalidInterpolation, GetParamIndex(params, "a"));
3644}
3645
3646TEST(GetParamIndexTest, RecognizesStar) {
3647 const char* params[] = { "a", "b", NULL };
3648 EXPECT_EQ(kTupleInterpolation, GetParamIndex(params, "*"));
3649}
3650
3651TEST(GetParamIndexTest, RecognizesKnownParam) {
3652 const char* params[] = { "foo", "bar", NULL };
3653 EXPECT_EQ(0, GetParamIndex(params, "foo"));
3654 EXPECT_EQ(1, GetParamIndex(params, "bar"));
3655}
3656
3657TEST(GetParamIndexTest, RejectsUnknownParam) {
3658 const char* params[] = { "foo", "bar", NULL };
3659 EXPECT_EQ(kInvalidInterpolation, GetParamIndex(params, "foobar"));
3660}
3661
3662// Tests SkipPrefix().
3663
3664TEST(SkipPrefixTest, SkipsWhenPrefixMatches) {
3665 const char* const str = "hello";
3666
3667 const char* p = str;
3668 EXPECT_TRUE(SkipPrefix("", &p));
3669 EXPECT_EQ(str, p);
3670
3671 p = str;
3672 EXPECT_TRUE(SkipPrefix("hell", &p));
3673 EXPECT_EQ(str + 4, p);
3674}
3675
3676TEST(SkipPrefixTest, DoesNotSkipWhenPrefixDoesNotMatch) {
3677 const char* const str = "world";
3678
3679 const char* p = str;
3680 EXPECT_FALSE(SkipPrefix("W", &p));
3681 EXPECT_EQ(str, p);
3682
3683 p = str;
3684 EXPECT_FALSE(SkipPrefix("world!", &p));
3685 EXPECT_EQ(str, p);
3686}
3687
3688// Tests FormatMatcherDescriptionSyntaxError().
3689TEST(FormatMatcherDescriptionSyntaxErrorTest, FormatsCorrectly) {
3690 const char* const description = "hello%world";
3691 EXPECT_EQ("Syntax error at index 5 in matcher description \"hello%world\": ",
3692 FormatMatcherDescriptionSyntaxError(description, description + 5));
3693}
3694
3695// Tests ValidateMatcherDescription().
3696
3697TEST(ValidateMatcherDescriptionTest, AcceptsEmptyDescription) {
3698 const char* params[] = { "foo", "bar", NULL };
3699 EXPECT_THAT(ValidateMatcherDescription(params, ""),
3700 ElementsAre());
3701}
3702
3703TEST(ValidateMatcherDescriptionTest,
3704 AcceptsNonEmptyDescriptionWithNoInterpolation) {
3705 const char* params[] = { "foo", "bar", NULL };
3706 EXPECT_THAT(ValidateMatcherDescription(params, "a simple description"),
3707 ElementsAre());
3708}
3709
zhanyong.wan82113312010-01-08 21:55:40 +00003710// The MATCHER*() macros trigger warning C4100 (unreferenced formal
3711// parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
3712// the macro definition, as the warnings are generated when the macro
3713// is expanded and macro expansion cannot contain #pragma. Therefore
3714// we suppress them here.
3715#ifdef _MSC_VER
3716#pragma warning(push)
3717#pragma warning(disable:4100)
3718#endif
3719
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003720// We use MATCHER_P3() to define a matcher for testing
3721// ValidateMatcherDescription(); otherwise we'll end up with much
3722// plumbing code. This is not circular as
3723// ValidateMatcherDescription() doesn't affect whether the matcher
3724// matches a value or not.
3725MATCHER_P3(EqInterpolation, start, end, index, "equals Interpolation%(*)s") {
3726 return arg.start_pos == start && arg.end_pos == end &&
3727 arg.param_index == index;
3728}
3729
zhanyong.wan82113312010-01-08 21:55:40 +00003730#ifdef _MSC_VER
3731#pragma warning(pop)
3732#endif
3733
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003734TEST(ValidateMatcherDescriptionTest, AcceptsPercentInterpolation) {
3735 const char* params[] = { "foo", NULL };
3736 const char* const desc = "one %%";
3737 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3738 ElementsAre(EqInterpolation(desc + 4, desc + 6,
3739 kPercentInterpolation)));
3740}
3741
3742TEST(ValidateMatcherDescriptionTest, AcceptsTupleInterpolation) {
3743 const char* params[] = { "foo", "bar", "baz", NULL };
3744 const char* const desc = "%(*)s after";
3745 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3746 ElementsAre(EqInterpolation(desc, desc + 5,
3747 kTupleInterpolation)));
3748}
3749
3750TEST(ValidateMatcherDescriptionTest, AcceptsParamInterpolation) {
3751 const char* params[] = { "foo", "bar", "baz", NULL };
3752 const char* const desc = "a %(bar)s.";
3753 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3754 ElementsAre(EqInterpolation(desc + 2, desc + 9, 1)));
3755}
3756
3757TEST(ValidateMatcherDescriptionTest, AcceptsMultiplenterpolations) {
3758 const char* params[] = { "foo", "bar", "baz", NULL };
3759 const char* const desc = "%(baz)s %(foo)s %(bar)s";
3760 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3761 ElementsAre(EqInterpolation(desc, desc + 7, 2),
3762 EqInterpolation(desc + 8, desc + 15, 0),
3763 EqInterpolation(desc + 16, desc + 23, 1)));
3764}
3765
3766TEST(ValidateMatcherDescriptionTest, AcceptsRepeatedParams) {
3767 const char* params[] = { "foo", "bar", NULL };
3768 const char* const desc = "%(foo)s and %(foo)s";
3769 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3770 ElementsAre(EqInterpolation(desc, desc + 7, 0),
3771 EqInterpolation(desc + 12, desc + 19, 0)));
3772}
3773
3774TEST(ValidateMatcherDescriptionTest, RejectsUnknownParam) {
3775 const char* params[] = { "a", "bar", NULL };
3776 EXPECT_NONFATAL_FAILURE({
3777 EXPECT_THAT(ValidateMatcherDescription(params, "%(foo)s"),
3778 ElementsAre());
3779 }, "Syntax error at index 2 in matcher description \"%(foo)s\": "
3780 "\"foo\" is an invalid parameter name.");
3781}
3782
3783TEST(ValidateMatcherDescriptionTest, RejectsUnfinishedParam) {
3784 const char* params[] = { "a", "bar", NULL };
3785 EXPECT_NONFATAL_FAILURE({
3786 EXPECT_THAT(ValidateMatcherDescription(params, "%(foo)"),
3787 ElementsAre());
3788 }, "Syntax error at index 0 in matcher description \"%(foo)\": "
3789 "an interpolation must end with \")s\", but \"%(foo)\" does not.");
3790
3791 EXPECT_NONFATAL_FAILURE({
3792 EXPECT_THAT(ValidateMatcherDescription(params, "x%(a"),
3793 ElementsAre());
3794 }, "Syntax error at index 1 in matcher description \"x%(a\": "
3795 "an interpolation must end with \")s\", but \"%(a\" does not.");
3796}
3797
3798TEST(ValidateMatcherDescriptionTest, RejectsSinglePercent) {
3799 const char* params[] = { "a", NULL };
3800 EXPECT_NONFATAL_FAILURE({
3801 EXPECT_THAT(ValidateMatcherDescription(params, "a %."),
3802 ElementsAre());
3803 }, "Syntax error at index 2 in matcher description \"a %.\": "
3804 "use \"%%\" instead of \"%\" to print \"%\".");
3805
3806}
3807
3808// Tests JoinAsTuple().
3809
3810TEST(JoinAsTupleTest, JoinsEmptyTuple) {
3811 EXPECT_EQ("", JoinAsTuple(Strings()));
3812}
3813
3814TEST(JoinAsTupleTest, JoinsOneTuple) {
3815 const char* fields[] = { "1" };
3816 EXPECT_EQ("1", JoinAsTuple(Strings(fields, fields + 1)));
3817}
3818
3819TEST(JoinAsTupleTest, JoinsTwoTuple) {
3820 const char* fields[] = { "1", "a" };
3821 EXPECT_EQ("(1, a)", JoinAsTuple(Strings(fields, fields + 2)));
3822}
3823
3824TEST(JoinAsTupleTest, JoinsTenTuple) {
3825 const char* fields[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
3826 EXPECT_EQ("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)",
3827 JoinAsTuple(Strings(fields, fields + 10)));
3828}
3829
3830// Tests FormatMatcherDescription().
3831
3832TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) {
3833 EXPECT_EQ("is even",
3834 FormatMatcherDescription("IsEven", "", Interpolations(),
3835 Strings()));
3836
3837 const char* params[] = { "5" };
3838 EXPECT_EQ("equals 5",
3839 FormatMatcherDescription("Equals", "", Interpolations(),
3840 Strings(params, params + 1)));
3841
3842 const char* params2[] = { "5", "8" };
3843 EXPECT_EQ("is in range (5, 8)",
3844 FormatMatcherDescription("IsInRange", "", Interpolations(),
3845 Strings(params2, params2 + 2)));
3846}
3847
3848TEST(FormatMatcherDescriptionTest, WorksForDescriptionWithNoInterpolation) {
3849 EXPECT_EQ("is positive",
3850 FormatMatcherDescription("Gt0", "is positive", Interpolations(),
3851 Strings()));
3852
3853 const char* params[] = { "5", "6" };
3854 EXPECT_EQ("is negative",
3855 FormatMatcherDescription("Lt0", "is negative", Interpolations(),
3856 Strings(params, params + 2)));
3857}
3858
3859TEST(FormatMatcherDescriptionTest,
3860 WorksWhenDescriptionStartsWithInterpolation) {
3861 const char* params[] = { "5" };
3862 const char* const desc = "%(num)s times bigger";
3863 const Interpolation interp[] = { Interpolation(desc, desc + 7, 0) };
3864 EXPECT_EQ("5 times bigger",
3865 FormatMatcherDescription("Foo", desc,
3866 Interpolations(interp, interp + 1),
3867 Strings(params, params + 1)));
3868}
3869
3870TEST(FormatMatcherDescriptionTest,
3871 WorksWhenDescriptionEndsWithInterpolation) {
3872 const char* params[] = { "5", "6" };
3873 const char* const desc = "is bigger than %(y)s";
3874 const Interpolation interp[] = { Interpolation(desc + 15, desc + 20, 1) };
3875 EXPECT_EQ("is bigger than 6",
3876 FormatMatcherDescription("Foo", desc,
3877 Interpolations(interp, interp + 1),
3878 Strings(params, params + 2)));
3879}
3880
3881TEST(FormatMatcherDescriptionTest,
3882 WorksWhenDescriptionStartsAndEndsWithInterpolation) {
3883 const char* params[] = { "5", "6" };
3884 const char* const desc = "%(x)s <= arg <= %(y)s";
3885 const Interpolation interp[] = {
3886 Interpolation(desc, desc + 5, 0),
3887 Interpolation(desc + 16, desc + 21, 1)
3888 };
3889 EXPECT_EQ("5 <= arg <= 6",
3890 FormatMatcherDescription("Foo", desc,
3891 Interpolations(interp, interp + 2),
3892 Strings(params, params + 2)));
3893}
3894
3895TEST(FormatMatcherDescriptionTest,
3896 WorksWhenDescriptionDoesNotStartOrEndWithInterpolation) {
3897 const char* params[] = { "5.2" };
3898 const char* const desc = "has %(x)s cents";
3899 const Interpolation interp[] = { Interpolation(desc + 4, desc + 9, 0) };
3900 EXPECT_EQ("has 5.2 cents",
3901 FormatMatcherDescription("Foo", desc,
3902 Interpolations(interp, interp + 1),
3903 Strings(params, params + 1)));
3904}
3905
3906TEST(FormatMatcherDescriptionTest,
3907 WorksWhenDescriptionContainsMultipleInterpolations) {
3908 const char* params[] = { "5", "6" };
3909 const char* const desc = "in %(*)s or [%(x)s, %(y)s]";
3910 const Interpolation interp[] = {
3911 Interpolation(desc + 3, desc + 8, kTupleInterpolation),
3912 Interpolation(desc + 13, desc + 18, 0),
3913 Interpolation(desc + 20, desc + 25, 1)
3914 };
3915 EXPECT_EQ("in (5, 6) or [5, 6]",
3916 FormatMatcherDescription("Foo", desc,
3917 Interpolations(interp, interp + 3),
3918 Strings(params, params + 2)));
3919}
3920
3921TEST(FormatMatcherDescriptionTest,
3922 WorksWhenDescriptionContainsRepeatedParams) {
3923 const char* params[] = { "9" };
3924 const char* const desc = "in [-%(x)s, %(x)s]";
3925 const Interpolation interp[] = {
3926 Interpolation(desc + 5, desc + 10, 0),
3927 Interpolation(desc + 12, desc + 17, 0)
3928 };
3929 EXPECT_EQ("in [-9, 9]",
3930 FormatMatcherDescription("Foo", desc,
3931 Interpolations(interp, interp + 2),
3932 Strings(params, params + 1)));
3933}
3934
3935TEST(FormatMatcherDescriptionTest,
3936 WorksForDescriptionWithInvalidInterpolation) {
3937 const char* params[] = { "9" };
3938 const char* const desc = "> %(x)s %(x)";
3939 const Interpolation interp[] = { Interpolation(desc + 2, desc + 7, 0) };
3940 EXPECT_EQ("> 9 %(x)",
3941 FormatMatcherDescription("Foo", desc,
3942 Interpolations(interp, interp + 1),
3943 Strings(params, params + 1)));
3944}
3945
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003946// Tests PolymorphicMatcher::mutable_impl().
3947TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {
3948 PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
3949 DivisibleByImpl& impl = m.mutable_impl();
3950 EXPECT_EQ(42, impl.divider());
3951
3952 impl.set_divider(0);
3953 EXPECT_EQ(0, m.mutable_impl().divider());
3954}
3955
3956// Tests PolymorphicMatcher::impl().
3957TEST(PolymorphicMatcherTest, CanAccessImpl) {
3958 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
3959 const DivisibleByImpl& impl = m.impl();
3960 EXPECT_EQ(42, impl.divider());
3961}
3962
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003963TEST(MatcherTupleTest, ExplainsMatchFailure) {
3964 stringstream ss1;
3965 ExplainMatchFailureTupleTo(make_tuple(Matcher<char>(Eq('a')), GreaterThan(5)),
3966 make_tuple('a', 10), &ss1);
3967 EXPECT_EQ("", ss1.str()); // Successful match.
3968
3969 stringstream ss2;
3970 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
3971 make_tuple(2, 'b'), &ss2);
3972 EXPECT_EQ(" Expected arg #0: is > 5\n"
3973 " Actual: 2, which is 3 less than 5\n"
3974 " Expected arg #1: is equal to 'a' (97)\n"
3975 " Actual: 'b' (98)\n",
3976 ss2.str()); // Failed match where both arguments need explanation.
3977
3978 stringstream ss3;
3979 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
3980 make_tuple(2, 'a'), &ss3);
3981 EXPECT_EQ(" Expected arg #0: is > 5\n"
3982 " Actual: 2, which is 3 less than 5\n",
3983 ss3.str()); // Failed match where only one argument needs
3984 // explanation.
3985}
3986
shiqiane35fdd92008-12-10 05:08:54 +00003987} // namespace gmock_matchers_test
3988} // namespace testing