blob: 66459464e4748f4360130f367d731fa3bc2cf996 [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
zhanyong.wan53e08c42010-09-14 05:38:21 +000036#include "gmock/gmock-matchers.h"
zhanyong.wan320814a2013-03-01 00:20:30 +000037#include "gmock/gmock-more-matchers.h"
shiqiane35fdd92008-12-10 05:08:54 +000038
39#include <string.h>
zhanyong.wana9a59e02013-03-27 16:14:55 +000040#include <deque>
shiqiane35fdd92008-12-10 05:08:54 +000041#include <functional>
zhanyong.wana862f1d2010-03-15 21:23:04 +000042#include <iostream>
zhanyong.wana9a59e02013-03-27 16:14:55 +000043#include <iterator>
zhanyong.wan6a896b52009-01-16 01:13:50 +000044#include <list>
45#include <map>
46#include <set>
shiqiane35fdd92008-12-10 05:08:54 +000047#include <sstream>
zhanyong.wan6a896b52009-01-16 01:13:50 +000048#include <string>
zhanyong.wanf5e1ce52009-09-16 07:02:02 +000049#include <utility>
zhanyong.wan6a896b52009-01-16 01:13:50 +000050#include <vector>
zhanyong.wan53e08c42010-09-14 05:38:21 +000051#include "gmock/gmock.h"
52#include "gtest/gtest.h"
53#include "gtest/gtest-spi.h"
shiqiane35fdd92008-12-10 05:08:54 +000054
55namespace testing {
zhanyong.wan4a5330d2009-02-19 00:36:44 +000056
57namespace internal {
vladlosev587c1b32011-05-20 00:42:22 +000058GTEST_API_ string JoinAsTuple(const Strings& fields);
zhanyong.wan4a5330d2009-02-19 00:36:44 +000059} // namespace internal
60
shiqiane35fdd92008-12-10 05:08:54 +000061namespace gmock_matchers_test {
62
zhanyong.wan898725c2011-09-16 16:45:39 +000063using std::greater;
64using std::less;
zhanyong.wanab5b77c2010-05-17 19:32:48 +000065using std::list;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000066using std::make_pair;
zhanyong.wanb5937da2009-07-16 20:26:41 +000067using std::map;
68using std::multimap;
zhanyong.wanab5b77c2010-05-17 19:32:48 +000069using std::multiset;
70using std::ostream;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000071using std::pair;
zhanyong.wan33605ba2010-04-22 23:37:47 +000072using std::set;
shiqiane35fdd92008-12-10 05:08:54 +000073using std::stringstream;
zhanyong.wanab5b77c2010-05-17 19:32:48 +000074using std::tr1::get;
zhanyong.wanb8243162009-06-04 05:48:20 +000075using std::tr1::make_tuple;
zhanyong.wanab5b77c2010-05-17 19:32:48 +000076using std::tr1::tuple;
zhanyong.wan33605ba2010-04-22 23:37:47 +000077using std::vector;
shiqiane35fdd92008-12-10 05:08:54 +000078using testing::A;
zhanyong.wanbf550852009-06-09 06:09:53 +000079using testing::AllArgs;
shiqiane35fdd92008-12-10 05:08:54 +000080using testing::AllOf;
81using testing::An;
82using testing::AnyOf;
83using testing::ByRef;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000084using testing::ContainsRegex;
shiqiane35fdd92008-12-10 05:08:54 +000085using testing::DoubleEq;
86using testing::EndsWith;
87using testing::Eq;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000088using testing::ExplainMatchResult;
shiqiane35fdd92008-12-10 05:08:54 +000089using testing::Field;
90using testing::FloatEq;
91using testing::Ge;
92using testing::Gt;
93using testing::HasSubstr;
zhanyong.wan320814a2013-03-01 00:20:30 +000094using testing::IsEmpty;
zhanyong.wan2d970ee2009-09-24 21:41:36 +000095using testing::IsNull;
zhanyong.wanb5937da2009-07-16 20:26:41 +000096using testing::Key;
shiqiane35fdd92008-12-10 05:08:54 +000097using testing::Le;
98using testing::Lt;
99using testing::MakeMatcher;
100using testing::MakePolymorphicMatcher;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000101using testing::MatchResultListener;
shiqiane35fdd92008-12-10 05:08:54 +0000102using testing::Matcher;
103using testing::MatcherCast;
104using testing::MatcherInterface;
105using testing::Matches;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000106using testing::MatchesRegex;
shiqiane35fdd92008-12-10 05:08:54 +0000107using testing::NanSensitiveDoubleEq;
108using testing::NanSensitiveFloatEq;
109using testing::Ne;
110using testing::Not;
111using testing::NotNull;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000112using testing::Pair;
shiqiane35fdd92008-12-10 05:08:54 +0000113using testing::Pointee;
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000114using testing::Pointwise;
shiqiane35fdd92008-12-10 05:08:54 +0000115using testing::PolymorphicMatcher;
116using testing::Property;
117using testing::Ref;
118using testing::ResultOf;
zhanyong.wana31d9ce2013-03-01 01:50:17 +0000119using testing::SizeIs;
shiqiane35fdd92008-12-10 05:08:54 +0000120using testing::StartsWith;
121using testing::StrCaseEq;
122using testing::StrCaseNe;
123using testing::StrEq;
124using testing::StrNe;
125using testing::Truly;
126using testing::TypedEq;
zhanyong.wanb8243162009-06-04 05:48:20 +0000127using testing::Value;
zhanyong.wan898725c2011-09-16 16:45:39 +0000128using testing::WhenSorted;
129using testing::WhenSortedBy;
shiqiane35fdd92008-12-10 05:08:54 +0000130using testing::_;
zhanyong.wana862f1d2010-03-15 21:23:04 +0000131using testing::internal::DummyMatchResultListener;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000132using testing::internal::ExplainMatchFailureTupleTo;
shiqiane35fdd92008-12-10 05:08:54 +0000133using testing::internal::FloatingEqMatcher;
zhanyong.wanb4140802010-06-08 22:53:57 +0000134using testing::internal::FormatMatcherDescription;
zhanyong.wan736baa82010-09-27 17:44:16 +0000135using testing::internal::IsReadableTypeName;
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000136using testing::internal::JoinAsTuple;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000137using testing::internal::RE;
zhanyong.wana862f1d2010-03-15 21:23:04 +0000138using testing::internal::StreamMatchResultListener;
zhanyong.wan34b034c2010-03-05 21:23:23 +0000139using testing::internal::StringMatchResultListener;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000140using testing::internal::Strings;
vladlosev79b83502009-11-18 00:43:37 +0000141using testing::internal::linked_ptr;
vladloseve56daa72009-11-18 01:08:08 +0000142using testing::internal::scoped_ptr;
shiqiane35fdd92008-12-10 05:08:54 +0000143using testing::internal::string;
144
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000145// For testing ExplainMatchResultTo().
146class GreaterThanMatcher : public MatcherInterface<int> {
147 public:
148 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
149
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000150 virtual void DescribeTo(ostream* os) const {
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000151 *os << "is > " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000152 }
153
zhanyong.wandb22c222010-01-28 21:52:29 +0000154 virtual bool MatchAndExplain(int lhs,
155 MatchResultListener* listener) const {
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000156 const int diff = lhs - rhs_;
157 if (diff > 0) {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000158 *listener << "which is " << diff << " more than " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000159 } else if (diff == 0) {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000160 *listener << "which is the same as " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000161 } else {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000162 *listener << "which is " << -diff << " less than " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000163 }
zhanyong.wandb22c222010-01-28 21:52:29 +0000164
165 return lhs > rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000166 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000167
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000168 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000169 int rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000170};
171
172Matcher<int> GreaterThan(int n) {
173 return MakeMatcher(new GreaterThanMatcher(n));
174}
175
zhanyong.wan736baa82010-09-27 17:44:16 +0000176string OfType(const string& type_name) {
177#if GTEST_HAS_RTTI
178 return " (of type " + type_name + ")";
179#else
180 return "";
181#endif
182}
183
shiqiane35fdd92008-12-10 05:08:54 +0000184// Returns the description of the given matcher.
185template <typename T>
186string Describe(const Matcher<T>& m) {
187 stringstream ss;
188 m.DescribeTo(&ss);
189 return ss.str();
190}
191
192// Returns the description of the negation of the given matcher.
193template <typename T>
194string DescribeNegation(const Matcher<T>& m) {
195 stringstream ss;
196 m.DescribeNegationTo(&ss);
197 return ss.str();
198}
199
200// Returns the reason why x matches, or doesn't match, m.
201template <typename MatcherType, typename Value>
202string Explain(const MatcherType& m, const Value& x) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000203 StringMatchResultListener listener;
204 ExplainMatchResult(m, x, &listener);
205 return listener.str();
shiqiane35fdd92008-12-10 05:08:54 +0000206}
207
zhanyong.wana862f1d2010-03-15 21:23:04 +0000208TEST(MatchResultListenerTest, StreamingWorks) {
209 StringMatchResultListener listener;
210 listener << "hi" << 5;
211 EXPECT_EQ("hi5", listener.str());
212
213 // Streaming shouldn't crash when the underlying ostream is NULL.
214 DummyMatchResultListener dummy;
215 dummy << "hi" << 5;
216}
217
218TEST(MatchResultListenerTest, CanAccessUnderlyingStream) {
219 EXPECT_TRUE(DummyMatchResultListener().stream() == NULL);
220 EXPECT_TRUE(StreamMatchResultListener(NULL).stream() == NULL);
221
222 EXPECT_EQ(&std::cout, StreamMatchResultListener(&std::cout).stream());
223}
224
225TEST(MatchResultListenerTest, IsInterestedWorks) {
226 EXPECT_TRUE(StringMatchResultListener().IsInterested());
227 EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested());
228
229 EXPECT_FALSE(DummyMatchResultListener().IsInterested());
230 EXPECT_FALSE(StreamMatchResultListener(NULL).IsInterested());
231}
232
shiqiane35fdd92008-12-10 05:08:54 +0000233// Makes sure that the MatcherInterface<T> interface doesn't
234// change.
235class EvenMatcherImpl : public MatcherInterface<int> {
236 public:
zhanyong.wandb22c222010-01-28 21:52:29 +0000237 virtual bool MatchAndExplain(int x,
238 MatchResultListener* /* listener */) const {
239 return x % 2 == 0;
240 }
shiqiane35fdd92008-12-10 05:08:54 +0000241
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000242 virtual void DescribeTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +0000243 *os << "is an even number";
244 }
245
246 // We deliberately don't define DescribeNegationTo() and
247 // ExplainMatchResultTo() here, to make sure the definition of these
248 // two methods is optional.
249};
250
zhanyong.wana862f1d2010-03-15 21:23:04 +0000251// Makes sure that the MatcherInterface API doesn't change.
252TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) {
shiqiane35fdd92008-12-10 05:08:54 +0000253 EvenMatcherImpl m;
254}
255
zhanyong.wan82113312010-01-08 21:55:40 +0000256// Tests implementing a monomorphic matcher using MatchAndExplain().
257
258class NewEvenMatcherImpl : public MatcherInterface<int> {
259 public:
260 virtual bool MatchAndExplain(int x, MatchResultListener* listener) const {
261 const bool match = x % 2 == 0;
262 // Verifies that we can stream to a listener directly.
263 *listener << "value % " << 2;
264 if (listener->stream() != NULL) {
265 // Verifies that we can stream to a listener's underlying stream
266 // too.
267 *listener->stream() << " == " << (x % 2);
268 }
269 return match;
270 }
271
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000272 virtual void DescribeTo(ostream* os) const {
zhanyong.wan82113312010-01-08 21:55:40 +0000273 *os << "is an even number";
274 }
275};
276
277TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {
278 Matcher<int> m = MakeMatcher(new NewEvenMatcherImpl);
279 EXPECT_TRUE(m.Matches(2));
280 EXPECT_FALSE(m.Matches(3));
281 EXPECT_EQ("value % 2 == 0", Explain(m, 2));
282 EXPECT_EQ("value % 2 == 1", Explain(m, 3));
283}
284
shiqiane35fdd92008-12-10 05:08:54 +0000285// Tests default-constructing a matcher.
286TEST(MatcherTest, CanBeDefaultConstructed) {
287 Matcher<double> m;
288}
289
290// Tests that Matcher<T> can be constructed from a MatcherInterface<T>*.
291TEST(MatcherTest, CanBeConstructedFromMatcherInterface) {
292 const MatcherInterface<int>* impl = new EvenMatcherImpl;
293 Matcher<int> m(impl);
294 EXPECT_TRUE(m.Matches(4));
295 EXPECT_FALSE(m.Matches(5));
296}
297
298// Tests that value can be used in place of Eq(value).
299TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) {
300 Matcher<int> m1 = 5;
301 EXPECT_TRUE(m1.Matches(5));
302 EXPECT_FALSE(m1.Matches(6));
303}
304
305// Tests that NULL can be used in place of Eq(NULL).
306TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) {
307 Matcher<int*> m1 = NULL;
308 EXPECT_TRUE(m1.Matches(NULL));
309 int n = 0;
310 EXPECT_FALSE(m1.Matches(&n));
311}
312
313// Tests that matchers are copyable.
314TEST(MatcherTest, IsCopyable) {
315 // Tests the copy constructor.
316 Matcher<bool> m1 = Eq(false);
317 EXPECT_TRUE(m1.Matches(false));
318 EXPECT_FALSE(m1.Matches(true));
319
320 // Tests the assignment operator.
321 m1 = Eq(true);
322 EXPECT_TRUE(m1.Matches(true));
323 EXPECT_FALSE(m1.Matches(false));
324}
325
326// Tests that Matcher<T>::DescribeTo() calls
327// MatcherInterface<T>::DescribeTo().
328TEST(MatcherTest, CanDescribeItself) {
329 EXPECT_EQ("is an even number",
330 Describe(Matcher<int>(new EvenMatcherImpl)));
331}
332
zhanyong.wan82113312010-01-08 21:55:40 +0000333// Tests Matcher<T>::MatchAndExplain().
334TEST(MatcherTest, MatchAndExplain) {
335 Matcher<int> m = GreaterThan(0);
zhanyong.wan34b034c2010-03-05 21:23:23 +0000336 StringMatchResultListener listener1;
zhanyong.wan82113312010-01-08 21:55:40 +0000337 EXPECT_TRUE(m.MatchAndExplain(42, &listener1));
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000338 EXPECT_EQ("which is 42 more than 0", listener1.str());
zhanyong.wan82113312010-01-08 21:55:40 +0000339
zhanyong.wan34b034c2010-03-05 21:23:23 +0000340 StringMatchResultListener listener2;
zhanyong.wan82113312010-01-08 21:55:40 +0000341 EXPECT_FALSE(m.MatchAndExplain(-9, &listener2));
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000342 EXPECT_EQ("which is 9 less than 0", listener2.str());
zhanyong.wan82113312010-01-08 21:55:40 +0000343}
344
shiqiane35fdd92008-12-10 05:08:54 +0000345// Tests that a C-string literal can be implicitly converted to a
346// Matcher<string> or Matcher<const string&>.
347TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
348 Matcher<string> m1 = "hi";
349 EXPECT_TRUE(m1.Matches("hi"));
350 EXPECT_FALSE(m1.Matches("hello"));
351
352 Matcher<const string&> m2 = "hi";
353 EXPECT_TRUE(m2.Matches("hi"));
354 EXPECT_FALSE(m2.Matches("hello"));
355}
356
357// Tests that a string object can be implicitly converted to a
358// Matcher<string> or Matcher<const string&>.
359TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {
360 Matcher<string> m1 = string("hi");
361 EXPECT_TRUE(m1.Matches("hi"));
362 EXPECT_FALSE(m1.Matches("hello"));
363
364 Matcher<const string&> m2 = string("hi");
365 EXPECT_TRUE(m2.Matches("hi"));
366 EXPECT_FALSE(m2.Matches("hello"));
367}
368
zhanyong.wan1f122a02013-03-25 16:27:03 +0000369#if GTEST_HAS_STRING_PIECE_
370// Tests that a C-string literal can be implicitly converted to a
371// Matcher<StringPiece> or Matcher<const StringPiece&>.
372TEST(StringPieceMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
373 Matcher<StringPiece> m1 = "cats";
374 EXPECT_TRUE(m1.Matches("cats"));
375 EXPECT_FALSE(m1.Matches("dogs"));
376
377 Matcher<const StringPiece&> m2 = "cats";
378 EXPECT_TRUE(m2.Matches("cats"));
379 EXPECT_FALSE(m2.Matches("dogs"));
380}
381
382// Tests that a string object can be implicitly converted to a
383// Matcher<StringPiece> or Matcher<const StringPiece&>.
384TEST(StringPieceMatcherTest, CanBeImplicitlyConstructedFromString) {
385 Matcher<StringPiece> m1 = string("cats");
386 EXPECT_TRUE(m1.Matches("cats"));
387 EXPECT_FALSE(m1.Matches("dogs"));
388
389 Matcher<const StringPiece&> m2 = string("cats");
390 EXPECT_TRUE(m2.Matches("cats"));
391 EXPECT_FALSE(m2.Matches("dogs"));
392}
393
394// Tests that a StringPiece object can be implicitly converted to a
395// Matcher<StringPiece> or Matcher<const StringPiece&>.
396TEST(StringPieceMatcherTest, CanBeImplicitlyConstructedFromStringPiece) {
397 Matcher<StringPiece> m1 = StringPiece("cats");
398 EXPECT_TRUE(m1.Matches("cats"));
399 EXPECT_FALSE(m1.Matches("dogs"));
400
401 Matcher<const StringPiece&> m2 = StringPiece("cats");
402 EXPECT_TRUE(m2.Matches("cats"));
403 EXPECT_FALSE(m2.Matches("dogs"));
404}
405#endif // GTEST_HAS_STRING_PIECE_
406
shiqiane35fdd92008-12-10 05:08:54 +0000407// Tests that MakeMatcher() constructs a Matcher<T> from a
408// MatcherInterface* without requiring the user to explicitly
409// write the type.
410TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) {
411 const MatcherInterface<int>* dummy_impl = NULL;
412 Matcher<int> m = MakeMatcher(dummy_impl);
413}
414
zhanyong.wan82113312010-01-08 21:55:40 +0000415// Tests that MakePolymorphicMatcher() can construct a polymorphic
416// matcher from its implementation using the old API.
zhanyong.wan33605ba2010-04-22 23:37:47 +0000417const int g_bar = 1;
shiqiane35fdd92008-12-10 05:08:54 +0000418class ReferencesBarOrIsZeroImpl {
419 public:
420 template <typename T>
zhanyong.wandb22c222010-01-28 21:52:29 +0000421 bool MatchAndExplain(const T& x,
422 MatchResultListener* /* listener */) const {
shiqiane35fdd92008-12-10 05:08:54 +0000423 const void* p = &x;
zhanyong.wan33605ba2010-04-22 23:37:47 +0000424 return p == &g_bar || x == 0;
shiqiane35fdd92008-12-10 05:08:54 +0000425 }
426
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000427 void DescribeTo(ostream* os) const { *os << "g_bar or zero"; }
shiqiane35fdd92008-12-10 05:08:54 +0000428
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000429 void DescribeNegationTo(ostream* os) const {
zhanyong.wan33605ba2010-04-22 23:37:47 +0000430 *os << "doesn't reference g_bar and is not zero";
shiqiane35fdd92008-12-10 05:08:54 +0000431 }
432};
433
434// This function verifies that MakePolymorphicMatcher() returns a
435// PolymorphicMatcher<T> where T is the argument's type.
436PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() {
437 return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl());
438}
439
zhanyong.wan82113312010-01-08 21:55:40 +0000440TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) {
shiqiane35fdd92008-12-10 05:08:54 +0000441 // Using a polymorphic matcher to match a reference type.
442 Matcher<const int&> m1 = ReferencesBarOrIsZero();
443 EXPECT_TRUE(m1.Matches(0));
444 // Verifies that the identity of a by-reference argument is preserved.
zhanyong.wan33605ba2010-04-22 23:37:47 +0000445 EXPECT_TRUE(m1.Matches(g_bar));
shiqiane35fdd92008-12-10 05:08:54 +0000446 EXPECT_FALSE(m1.Matches(1));
zhanyong.wan33605ba2010-04-22 23:37:47 +0000447 EXPECT_EQ("g_bar or zero", Describe(m1));
shiqiane35fdd92008-12-10 05:08:54 +0000448
449 // Using a polymorphic matcher to match a value type.
450 Matcher<double> m2 = ReferencesBarOrIsZero();
451 EXPECT_TRUE(m2.Matches(0.0));
452 EXPECT_FALSE(m2.Matches(0.1));
zhanyong.wan33605ba2010-04-22 23:37:47 +0000453 EXPECT_EQ("g_bar or zero", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +0000454}
455
zhanyong.wan82113312010-01-08 21:55:40 +0000456// Tests implementing a polymorphic matcher using MatchAndExplain().
457
458class PolymorphicIsEvenImpl {
459 public:
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000460 void DescribeTo(ostream* os) const { *os << "is even"; }
zhanyong.wan82113312010-01-08 21:55:40 +0000461
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000462 void DescribeNegationTo(ostream* os) const {
zhanyong.wan82113312010-01-08 21:55:40 +0000463 *os << "is odd";
464 }
zhanyong.wan82113312010-01-08 21:55:40 +0000465
zhanyong.wandb22c222010-01-28 21:52:29 +0000466 template <typename T>
467 bool MatchAndExplain(const T& x, MatchResultListener* listener) const {
468 // Verifies that we can stream to the listener directly.
469 *listener << "% " << 2;
470 if (listener->stream() != NULL) {
471 // Verifies that we can stream to the listener's underlying stream
472 // too.
473 *listener->stream() << " == " << (x % 2);
474 }
475 return (x % 2) == 0;
zhanyong.wan82113312010-01-08 21:55:40 +0000476 }
zhanyong.wandb22c222010-01-28 21:52:29 +0000477};
zhanyong.wan82113312010-01-08 21:55:40 +0000478
479PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() {
480 return MakePolymorphicMatcher(PolymorphicIsEvenImpl());
481}
482
483TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {
484 // Using PolymorphicIsEven() as a Matcher<int>.
485 const Matcher<int> m1 = PolymorphicIsEven();
486 EXPECT_TRUE(m1.Matches(42));
487 EXPECT_FALSE(m1.Matches(43));
488 EXPECT_EQ("is even", Describe(m1));
489
490 const Matcher<int> not_m1 = Not(m1);
491 EXPECT_EQ("is odd", Describe(not_m1));
492
493 EXPECT_EQ("% 2 == 0", Explain(m1, 42));
494
495 // Using PolymorphicIsEven() as a Matcher<char>.
496 const Matcher<char> m2 = PolymorphicIsEven();
497 EXPECT_TRUE(m2.Matches('\x42'));
498 EXPECT_FALSE(m2.Matches('\x43'));
499 EXPECT_EQ("is even", Describe(m2));
500
501 const Matcher<char> not_m2 = Not(m2);
502 EXPECT_EQ("is odd", Describe(not_m2));
503
504 EXPECT_EQ("% 2 == 0", Explain(m2, '\x42'));
505}
506
shiqiane35fdd92008-12-10 05:08:54 +0000507// Tests that MatcherCast<T>(m) works when m is a polymorphic matcher.
508TEST(MatcherCastTest, FromPolymorphicMatcher) {
509 Matcher<int> m = MatcherCast<int>(Eq(5));
510 EXPECT_TRUE(m.Matches(5));
511 EXPECT_FALSE(m.Matches(6));
512}
513
514// For testing casting matchers between compatible types.
515class IntValue {
516 public:
517 // An int can be statically (although not implicitly) cast to a
518 // IntValue.
zhanyong.wan32de5f52009-12-23 00:13:23 +0000519 explicit IntValue(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +0000520
521 int value() const { return value_; }
522 private:
523 int value_;
524};
525
526// For testing casting matchers between compatible types.
527bool IsPositiveIntValue(const IntValue& foo) {
528 return foo.value() > 0;
529}
530
531// Tests that MatcherCast<T>(m) works when m is a Matcher<U> where T
532// can be statically converted to U.
533TEST(MatcherCastTest, FromCompatibleType) {
534 Matcher<double> m1 = Eq(2.0);
535 Matcher<int> m2 = MatcherCast<int>(m1);
536 EXPECT_TRUE(m2.Matches(2));
537 EXPECT_FALSE(m2.Matches(3));
538
539 Matcher<IntValue> m3 = Truly(IsPositiveIntValue);
540 Matcher<int> m4 = MatcherCast<int>(m3);
541 // In the following, the arguments 1 and 0 are statically converted
542 // to IntValue objects, and then tested by the IsPositiveIntValue()
543 // predicate.
544 EXPECT_TRUE(m4.Matches(1));
545 EXPECT_FALSE(m4.Matches(0));
546}
547
548// Tests that MatcherCast<T>(m) works when m is a Matcher<const T&>.
549TEST(MatcherCastTest, FromConstReferenceToNonReference) {
550 Matcher<const int&> m1 = Eq(0);
551 Matcher<int> m2 = MatcherCast<int>(m1);
552 EXPECT_TRUE(m2.Matches(0));
553 EXPECT_FALSE(m2.Matches(1));
554}
555
556// Tests that MatcherCast<T>(m) works when m is a Matcher<T&>.
557TEST(MatcherCastTest, FromReferenceToNonReference) {
558 Matcher<int&> m1 = Eq(0);
559 Matcher<int> m2 = MatcherCast<int>(m1);
560 EXPECT_TRUE(m2.Matches(0));
561 EXPECT_FALSE(m2.Matches(1));
562}
563
564// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
565TEST(MatcherCastTest, FromNonReferenceToConstReference) {
566 Matcher<int> m1 = Eq(0);
567 Matcher<const int&> m2 = MatcherCast<const int&>(m1);
568 EXPECT_TRUE(m2.Matches(0));
569 EXPECT_FALSE(m2.Matches(1));
570}
571
572// Tests that MatcherCast<T&>(m) works when m is a Matcher<T>.
573TEST(MatcherCastTest, FromNonReferenceToReference) {
574 Matcher<int> m1 = Eq(0);
575 Matcher<int&> m2 = MatcherCast<int&>(m1);
576 int n = 0;
577 EXPECT_TRUE(m2.Matches(n));
578 n = 1;
579 EXPECT_FALSE(m2.Matches(n));
580}
581
582// Tests that MatcherCast<T>(m) works when m is a Matcher<T>.
583TEST(MatcherCastTest, FromSameType) {
584 Matcher<int> m1 = Eq(0);
585 Matcher<int> m2 = MatcherCast<int>(m1);
586 EXPECT_TRUE(m2.Matches(0));
587 EXPECT_FALSE(m2.Matches(1));
588}
589
jgm79a367e2012-04-10 16:02:11 +0000590// Implicitly convertible form any type.
591struct ConvertibleFromAny {
592 ConvertibleFromAny(int a_value) : value(a_value) {}
593 template <typename T>
594 ConvertibleFromAny(const T& a_value) : value(-1) {
595 ADD_FAILURE() << "Conversion constructor called";
596 }
597 int value;
598};
599
600bool operator==(const ConvertibleFromAny& a, const ConvertibleFromAny& b) {
601 return a.value == b.value;
602}
603
604ostream& operator<<(ostream& os, const ConvertibleFromAny& a) {
605 return os << a.value;
606}
607
608TEST(MatcherCastTest, ConversionConstructorIsUsed) {
609 Matcher<ConvertibleFromAny> m = MatcherCast<ConvertibleFromAny>(1);
610 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
611 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
612}
613
614TEST(MatcherCastTest, FromConvertibleFromAny) {
615 Matcher<ConvertibleFromAny> m =
616 MatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
617 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
618 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
619}
620
zhanyong.wan18490652009-05-11 18:54:08 +0000621class Base {};
622class Derived : public Base {};
623
624// Tests that SafeMatcherCast<T>(m) works when m is a polymorphic matcher.
625TEST(SafeMatcherCastTest, FromPolymorphicMatcher) {
626 Matcher<char> m2 = SafeMatcherCast<char>(Eq(32));
627 EXPECT_TRUE(m2.Matches(' '));
628 EXPECT_FALSE(m2.Matches('\n'));
629}
630
zhanyong.wan16cf4732009-05-14 20:55:30 +0000631// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where
632// T and U are arithmetic types and T can be losslessly converted to
633// U.
634TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) {
zhanyong.wan18490652009-05-11 18:54:08 +0000635 Matcher<double> m1 = DoubleEq(1.0);
zhanyong.wan16cf4732009-05-14 20:55:30 +0000636 Matcher<float> m2 = SafeMatcherCast<float>(m1);
637 EXPECT_TRUE(m2.Matches(1.0f));
638 EXPECT_FALSE(m2.Matches(2.0f));
639
640 Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>('a'));
641 EXPECT_TRUE(m3.Matches('a'));
642 EXPECT_FALSE(m3.Matches('b'));
zhanyong.wan18490652009-05-11 18:54:08 +0000643}
644
645// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where T and U
646// are pointers or references to a derived and a base class, correspondingly.
647TEST(SafeMatcherCastTest, FromBaseClass) {
648 Derived d, d2;
649 Matcher<Base*> m1 = Eq(&d);
650 Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1);
651 EXPECT_TRUE(m2.Matches(&d));
652 EXPECT_FALSE(m2.Matches(&d2));
653
654 Matcher<Base&> m3 = Ref(d);
655 Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3);
656 EXPECT_TRUE(m4.Matches(d));
657 EXPECT_FALSE(m4.Matches(d2));
658}
659
660// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<const T&>.
661TEST(SafeMatcherCastTest, FromConstReferenceToReference) {
662 int n = 0;
663 Matcher<const int&> m1 = Ref(n);
664 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
665 int n1 = 0;
666 EXPECT_TRUE(m2.Matches(n));
667 EXPECT_FALSE(m2.Matches(n1));
668}
669
670// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
671TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) {
672 Matcher<int> m1 = Eq(0);
673 Matcher<const int&> m2 = SafeMatcherCast<const int&>(m1);
674 EXPECT_TRUE(m2.Matches(0));
675 EXPECT_FALSE(m2.Matches(1));
676}
677
678// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<T>.
679TEST(SafeMatcherCastTest, FromNonReferenceToReference) {
680 Matcher<int> m1 = Eq(0);
681 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
682 int n = 0;
683 EXPECT_TRUE(m2.Matches(n));
684 n = 1;
685 EXPECT_FALSE(m2.Matches(n));
686}
687
688// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<T>.
689TEST(SafeMatcherCastTest, FromSameType) {
690 Matcher<int> m1 = Eq(0);
691 Matcher<int> m2 = SafeMatcherCast<int>(m1);
692 EXPECT_TRUE(m2.Matches(0));
693 EXPECT_FALSE(m2.Matches(1));
694}
695
jgm79a367e2012-04-10 16:02:11 +0000696TEST(SafeMatcherCastTest, ConversionConstructorIsUsed) {
697 Matcher<ConvertibleFromAny> m = SafeMatcherCast<ConvertibleFromAny>(1);
698 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
699 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
700}
701
702TEST(SafeMatcherCastTest, FromConvertibleFromAny) {
703 Matcher<ConvertibleFromAny> m =
704 SafeMatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
705 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
706 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
707}
708
shiqiane35fdd92008-12-10 05:08:54 +0000709// Tests that A<T>() matches any value of type T.
710TEST(ATest, MatchesAnyValue) {
711 // Tests a matcher for a value type.
712 Matcher<double> m1 = A<double>();
713 EXPECT_TRUE(m1.Matches(91.43));
714 EXPECT_TRUE(m1.Matches(-15.32));
715
716 // Tests a matcher for a reference type.
717 int a = 2;
718 int b = -6;
719 Matcher<int&> m2 = A<int&>();
720 EXPECT_TRUE(m2.Matches(a));
721 EXPECT_TRUE(m2.Matches(b));
722}
723
724// Tests that A<T>() describes itself properly.
725TEST(ATest, CanDescribeSelf) {
726 EXPECT_EQ("is anything", Describe(A<bool>()));
727}
728
729// Tests that An<T>() matches any value of type T.
730TEST(AnTest, MatchesAnyValue) {
731 // Tests a matcher for a value type.
732 Matcher<int> m1 = An<int>();
733 EXPECT_TRUE(m1.Matches(9143));
734 EXPECT_TRUE(m1.Matches(-1532));
735
736 // Tests a matcher for a reference type.
737 int a = 2;
738 int b = -6;
739 Matcher<int&> m2 = An<int&>();
740 EXPECT_TRUE(m2.Matches(a));
741 EXPECT_TRUE(m2.Matches(b));
742}
743
744// Tests that An<T>() describes itself properly.
745TEST(AnTest, CanDescribeSelf) {
746 EXPECT_EQ("is anything", Describe(An<int>()));
747}
748
749// Tests that _ can be used as a matcher for any type and matches any
750// value of that type.
751TEST(UnderscoreTest, MatchesAnyValue) {
752 // Uses _ as a matcher for a value type.
753 Matcher<int> m1 = _;
754 EXPECT_TRUE(m1.Matches(123));
755 EXPECT_TRUE(m1.Matches(-242));
756
757 // Uses _ as a matcher for a reference type.
758 bool a = false;
759 const bool b = true;
760 Matcher<const bool&> m2 = _;
761 EXPECT_TRUE(m2.Matches(a));
762 EXPECT_TRUE(m2.Matches(b));
763}
764
765// Tests that _ describes itself properly.
766TEST(UnderscoreTest, CanDescribeSelf) {
767 Matcher<int> m = _;
768 EXPECT_EQ("is anything", Describe(m));
769}
770
771// Tests that Eq(x) matches any value equal to x.
772TEST(EqTest, MatchesEqualValue) {
773 // 2 C-strings with same content but different addresses.
774 const char a1[] = "hi";
775 const char a2[] = "hi";
776
777 Matcher<const char*> m1 = Eq(a1);
778 EXPECT_TRUE(m1.Matches(a1));
779 EXPECT_FALSE(m1.Matches(a2));
780}
781
782// Tests that Eq(v) describes itself properly.
783
784class Unprintable {
785 public:
786 Unprintable() : c_('a') {}
787
zhanyong.wan32de5f52009-12-23 00:13:23 +0000788 bool operator==(const Unprintable& /* rhs */) { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000789 private:
790 char c_;
791};
792
793TEST(EqTest, CanDescribeSelf) {
794 Matcher<Unprintable> m = Eq(Unprintable());
795 EXPECT_EQ("is equal to 1-byte object <61>", Describe(m));
796}
797
798// Tests that Eq(v) can be used to match any type that supports
799// comparing with type T, where T is v's type.
800TEST(EqTest, IsPolymorphic) {
801 Matcher<int> m1 = Eq(1);
802 EXPECT_TRUE(m1.Matches(1));
803 EXPECT_FALSE(m1.Matches(2));
804
805 Matcher<char> m2 = Eq(1);
806 EXPECT_TRUE(m2.Matches('\1'));
807 EXPECT_FALSE(m2.Matches('a'));
808}
809
810// Tests that TypedEq<T>(v) matches values of type T that's equal to v.
811TEST(TypedEqTest, ChecksEqualityForGivenType) {
812 Matcher<char> m1 = TypedEq<char>('a');
813 EXPECT_TRUE(m1.Matches('a'));
814 EXPECT_FALSE(m1.Matches('b'));
815
816 Matcher<int> m2 = TypedEq<int>(6);
817 EXPECT_TRUE(m2.Matches(6));
818 EXPECT_FALSE(m2.Matches(7));
819}
820
821// Tests that TypedEq(v) describes itself properly.
822TEST(TypedEqTest, CanDescribeSelf) {
823 EXPECT_EQ("is equal to 2", Describe(TypedEq<int>(2)));
824}
825
826// Tests that TypedEq<T>(v) has type Matcher<T>.
827
828// Type<T>::IsTypeOf(v) compiles iff the type of value v is T, where T
829// is a "bare" type (i.e. not in the form of const U or U&). If v's
830// type is not T, the compiler will generate a message about
831// "undefined referece".
832template <typename T>
833struct Type {
zhanyong.wan32de5f52009-12-23 00:13:23 +0000834 static bool IsTypeOf(const T& /* v */) { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000835
836 template <typename T2>
837 static void IsTypeOf(T2 v);
838};
839
840TEST(TypedEqTest, HasSpecifiedType) {
841 // Verfies that the type of TypedEq<T>(v) is Matcher<T>.
842 Type<Matcher<int> >::IsTypeOf(TypedEq<int>(5));
843 Type<Matcher<double> >::IsTypeOf(TypedEq<double>(5));
844}
845
846// Tests that Ge(v) matches anything >= v.
847TEST(GeTest, ImplementsGreaterThanOrEqual) {
848 Matcher<int> m1 = Ge(0);
849 EXPECT_TRUE(m1.Matches(1));
850 EXPECT_TRUE(m1.Matches(0));
851 EXPECT_FALSE(m1.Matches(-1));
852}
853
854// Tests that Ge(v) describes itself properly.
855TEST(GeTest, CanDescribeSelf) {
856 Matcher<int> m = Ge(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000857 EXPECT_EQ("is >= 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000858}
859
860// Tests that Gt(v) matches anything > v.
861TEST(GtTest, ImplementsGreaterThan) {
862 Matcher<double> m1 = Gt(0);
863 EXPECT_TRUE(m1.Matches(1.0));
864 EXPECT_FALSE(m1.Matches(0.0));
865 EXPECT_FALSE(m1.Matches(-1.0));
866}
867
868// Tests that Gt(v) describes itself properly.
869TEST(GtTest, CanDescribeSelf) {
870 Matcher<int> m = Gt(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000871 EXPECT_EQ("is > 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000872}
873
874// Tests that Le(v) matches anything <= v.
875TEST(LeTest, ImplementsLessThanOrEqual) {
876 Matcher<char> m1 = Le('b');
877 EXPECT_TRUE(m1.Matches('a'));
878 EXPECT_TRUE(m1.Matches('b'));
879 EXPECT_FALSE(m1.Matches('c'));
880}
881
882// Tests that Le(v) describes itself properly.
883TEST(LeTest, CanDescribeSelf) {
884 Matcher<int> m = Le(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000885 EXPECT_EQ("is <= 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000886}
887
888// Tests that Lt(v) matches anything < v.
889TEST(LtTest, ImplementsLessThan) {
890 Matcher<const string&> m1 = Lt("Hello");
891 EXPECT_TRUE(m1.Matches("Abc"));
892 EXPECT_FALSE(m1.Matches("Hello"));
893 EXPECT_FALSE(m1.Matches("Hello, world!"));
894}
895
896// Tests that Lt(v) describes itself properly.
897TEST(LtTest, CanDescribeSelf) {
898 Matcher<int> m = Lt(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000899 EXPECT_EQ("is < 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000900}
901
902// Tests that Ne(v) matches anything != v.
903TEST(NeTest, ImplementsNotEqual) {
904 Matcher<int> m1 = Ne(0);
905 EXPECT_TRUE(m1.Matches(1));
906 EXPECT_TRUE(m1.Matches(-1));
907 EXPECT_FALSE(m1.Matches(0));
908}
909
910// Tests that Ne(v) describes itself properly.
911TEST(NeTest, CanDescribeSelf) {
912 Matcher<int> m = Ne(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000913 EXPECT_EQ("isn't equal to 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000914}
915
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000916// Tests that IsNull() matches any NULL pointer of any type.
917TEST(IsNullTest, MatchesNullPointer) {
918 Matcher<int*> m1 = IsNull();
919 int* p1 = NULL;
920 int n = 0;
921 EXPECT_TRUE(m1.Matches(p1));
922 EXPECT_FALSE(m1.Matches(&n));
923
924 Matcher<const char*> m2 = IsNull();
925 const char* p2 = NULL;
926 EXPECT_TRUE(m2.Matches(p2));
927 EXPECT_FALSE(m2.Matches("hi"));
928
zhanyong.wan95b12332009-09-25 18:55:50 +0000929#if !GTEST_OS_SYMBIAN
930 // Nokia's Symbian compiler generates:
931 // gmock-matchers.h: ambiguous access to overloaded function
932 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(void *)'
933 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(const testing::
934 // MatcherInterface<void *> *)'
935 // gmock-matchers.h: (point of instantiation: 'testing::
936 // gmock_matchers_test::IsNullTest_MatchesNullPointer_Test::TestBody()')
937 // gmock-matchers.h: (instantiating: 'testing::PolymorphicMatc
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000938 Matcher<void*> m3 = IsNull();
939 void* p3 = NULL;
940 EXPECT_TRUE(m3.Matches(p3));
941 EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef)));
zhanyong.wan95b12332009-09-25 18:55:50 +0000942#endif
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000943}
944
vladlosev79b83502009-11-18 00:43:37 +0000945TEST(IsNullTest, LinkedPtr) {
946 const Matcher<linked_ptr<int> > m = IsNull();
947 const linked_ptr<int> null_p;
948 const linked_ptr<int> non_null_p(new int);
949
950 EXPECT_TRUE(m.Matches(null_p));
951 EXPECT_FALSE(m.Matches(non_null_p));
952}
953
954TEST(IsNullTest, ReferenceToConstLinkedPtr) {
955 const Matcher<const linked_ptr<double>&> m = IsNull();
956 const linked_ptr<double> null_p;
957 const linked_ptr<double> non_null_p(new double);
958
959 EXPECT_TRUE(m.Matches(null_p));
960 EXPECT_FALSE(m.Matches(non_null_p));
961}
962
vladloseve56daa72009-11-18 01:08:08 +0000963TEST(IsNullTest, ReferenceToConstScopedPtr) {
964 const Matcher<const scoped_ptr<double>&> m = IsNull();
965 const scoped_ptr<double> null_p;
966 const scoped_ptr<double> non_null_p(new double);
967
968 EXPECT_TRUE(m.Matches(null_p));
969 EXPECT_FALSE(m.Matches(non_null_p));
970}
971
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000972// Tests that IsNull() describes itself properly.
973TEST(IsNullTest, CanDescribeSelf) {
974 Matcher<int*> m = IsNull();
975 EXPECT_EQ("is NULL", Describe(m));
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000976 EXPECT_EQ("isn't NULL", DescribeNegation(m));
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000977}
978
shiqiane35fdd92008-12-10 05:08:54 +0000979// Tests that NotNull() matches any non-NULL pointer of any type.
980TEST(NotNullTest, MatchesNonNullPointer) {
981 Matcher<int*> m1 = NotNull();
982 int* p1 = NULL;
983 int n = 0;
984 EXPECT_FALSE(m1.Matches(p1));
985 EXPECT_TRUE(m1.Matches(&n));
986
987 Matcher<const char*> m2 = NotNull();
988 const char* p2 = NULL;
989 EXPECT_FALSE(m2.Matches(p2));
990 EXPECT_TRUE(m2.Matches("hi"));
991}
992
vladlosev79b83502009-11-18 00:43:37 +0000993TEST(NotNullTest, LinkedPtr) {
994 const Matcher<linked_ptr<int> > m = NotNull();
995 const linked_ptr<int> null_p;
996 const linked_ptr<int> non_null_p(new int);
997
998 EXPECT_FALSE(m.Matches(null_p));
999 EXPECT_TRUE(m.Matches(non_null_p));
1000}
1001
1002TEST(NotNullTest, ReferenceToConstLinkedPtr) {
1003 const Matcher<const linked_ptr<double>&> m = NotNull();
1004 const linked_ptr<double> null_p;
1005 const linked_ptr<double> non_null_p(new double);
1006
1007 EXPECT_FALSE(m.Matches(null_p));
1008 EXPECT_TRUE(m.Matches(non_null_p));
1009}
1010
vladloseve56daa72009-11-18 01:08:08 +00001011TEST(NotNullTest, ReferenceToConstScopedPtr) {
1012 const Matcher<const scoped_ptr<double>&> m = NotNull();
1013 const scoped_ptr<double> null_p;
1014 const scoped_ptr<double> non_null_p(new double);
1015
1016 EXPECT_FALSE(m.Matches(null_p));
1017 EXPECT_TRUE(m.Matches(non_null_p));
1018}
1019
shiqiane35fdd92008-12-10 05:08:54 +00001020// Tests that NotNull() describes itself properly.
1021TEST(NotNullTest, CanDescribeSelf) {
1022 Matcher<int*> m = NotNull();
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001023 EXPECT_EQ("isn't NULL", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001024}
1025
1026// Tests that Ref(variable) matches an argument that references
1027// 'variable'.
1028TEST(RefTest, MatchesSameVariable) {
1029 int a = 0;
1030 int b = 0;
1031 Matcher<int&> m = Ref(a);
1032 EXPECT_TRUE(m.Matches(a));
1033 EXPECT_FALSE(m.Matches(b));
1034}
1035
1036// Tests that Ref(variable) describes itself properly.
1037TEST(RefTest, CanDescribeSelf) {
1038 int n = 5;
1039 Matcher<int&> m = Ref(n);
1040 stringstream ss;
1041 ss << "references the variable @" << &n << " 5";
1042 EXPECT_EQ(string(ss.str()), Describe(m));
1043}
1044
1045// Test that Ref(non_const_varialbe) can be used as a matcher for a
1046// const reference.
1047TEST(RefTest, CanBeUsedAsMatcherForConstReference) {
1048 int a = 0;
1049 int b = 0;
1050 Matcher<const int&> m = Ref(a);
1051 EXPECT_TRUE(m.Matches(a));
1052 EXPECT_FALSE(m.Matches(b));
1053}
1054
1055// Tests that Ref(variable) is covariant, i.e. Ref(derived) can be
1056// used wherever Ref(base) can be used (Ref(derived) is a sub-type
1057// of Ref(base), but not vice versa.
1058
shiqiane35fdd92008-12-10 05:08:54 +00001059TEST(RefTest, IsCovariant) {
1060 Base base, base2;
1061 Derived derived;
1062 Matcher<const Base&> m1 = Ref(base);
1063 EXPECT_TRUE(m1.Matches(base));
1064 EXPECT_FALSE(m1.Matches(base2));
1065 EXPECT_FALSE(m1.Matches(derived));
1066
1067 m1 = Ref(derived);
1068 EXPECT_TRUE(m1.Matches(derived));
1069 EXPECT_FALSE(m1.Matches(base));
1070 EXPECT_FALSE(m1.Matches(base2));
1071}
1072
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001073TEST(RefTest, ExplainsResult) {
1074 int n = 0;
1075 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), n),
1076 StartsWith("which is located @"));
1077
1078 int m = 0;
1079 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), m),
1080 StartsWith("which is located @"));
1081}
1082
shiqiane35fdd92008-12-10 05:08:54 +00001083// Tests string comparison matchers.
1084
1085TEST(StrEqTest, MatchesEqualString) {
1086 Matcher<const char*> m = StrEq(string("Hello"));
1087 EXPECT_TRUE(m.Matches("Hello"));
1088 EXPECT_FALSE(m.Matches("hello"));
1089 EXPECT_FALSE(m.Matches(NULL));
1090
1091 Matcher<const string&> m2 = StrEq("Hello");
1092 EXPECT_TRUE(m2.Matches("Hello"));
1093 EXPECT_FALSE(m2.Matches("Hi"));
1094}
1095
1096TEST(StrEqTest, CanDescribeSelf) {
vladlosevaa432202011-04-01 21:58:42 +00001097 Matcher<string> m = StrEq("Hi-\'\"?\\\a\b\f\n\r\t\v\xD3");
1098 EXPECT_EQ("is equal to \"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",
shiqiane35fdd92008-12-10 05:08:54 +00001099 Describe(m));
1100
1101 string str("01204500800");
1102 str[3] = '\0';
1103 Matcher<string> m2 = StrEq(str);
1104 EXPECT_EQ("is equal to \"012\\04500800\"", Describe(m2));
1105 str[0] = str[6] = str[7] = str[9] = str[10] = '\0';
1106 Matcher<string> m3 = StrEq(str);
1107 EXPECT_EQ("is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3));
1108}
1109
1110TEST(StrNeTest, MatchesUnequalString) {
1111 Matcher<const char*> m = StrNe("Hello");
1112 EXPECT_TRUE(m.Matches(""));
1113 EXPECT_TRUE(m.Matches(NULL));
1114 EXPECT_FALSE(m.Matches("Hello"));
1115
1116 Matcher<string> m2 = StrNe(string("Hello"));
1117 EXPECT_TRUE(m2.Matches("hello"));
1118 EXPECT_FALSE(m2.Matches("Hello"));
1119}
1120
1121TEST(StrNeTest, CanDescribeSelf) {
1122 Matcher<const char*> m = StrNe("Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001123 EXPECT_EQ("isn't equal to \"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001124}
1125
1126TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
1127 Matcher<const char*> m = StrCaseEq(string("Hello"));
1128 EXPECT_TRUE(m.Matches("Hello"));
1129 EXPECT_TRUE(m.Matches("hello"));
1130 EXPECT_FALSE(m.Matches("Hi"));
1131 EXPECT_FALSE(m.Matches(NULL));
1132
1133 Matcher<const string&> m2 = StrCaseEq("Hello");
1134 EXPECT_TRUE(m2.Matches("hello"));
1135 EXPECT_FALSE(m2.Matches("Hi"));
1136}
1137
1138TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1139 string str1("oabocdooeoo");
1140 string str2("OABOCDOOEOO");
1141 Matcher<const string&> m0 = StrCaseEq(str1);
1142 EXPECT_FALSE(m0.Matches(str2 + string(1, '\0')));
1143
1144 str1[3] = str2[3] = '\0';
1145 Matcher<const string&> m1 = StrCaseEq(str1);
1146 EXPECT_TRUE(m1.Matches(str2));
1147
1148 str1[0] = str1[6] = str1[7] = str1[10] = '\0';
1149 str2[0] = str2[6] = str2[7] = str2[10] = '\0';
1150 Matcher<const string&> m2 = StrCaseEq(str1);
1151 str1[9] = str2[9] = '\0';
1152 EXPECT_FALSE(m2.Matches(str2));
1153
1154 Matcher<const string&> m3 = StrCaseEq(str1);
1155 EXPECT_TRUE(m3.Matches(str2));
1156
1157 EXPECT_FALSE(m3.Matches(str2 + "x"));
1158 str2.append(1, '\0');
1159 EXPECT_FALSE(m3.Matches(str2));
1160 EXPECT_FALSE(m3.Matches(string(str2, 0, 9)));
1161}
1162
1163TEST(StrCaseEqTest, CanDescribeSelf) {
1164 Matcher<string> m = StrCaseEq("Hi");
1165 EXPECT_EQ("is equal to (ignoring case) \"Hi\"", Describe(m));
1166}
1167
1168TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1169 Matcher<const char*> m = StrCaseNe("Hello");
1170 EXPECT_TRUE(m.Matches("Hi"));
1171 EXPECT_TRUE(m.Matches(NULL));
1172 EXPECT_FALSE(m.Matches("Hello"));
1173 EXPECT_FALSE(m.Matches("hello"));
1174
1175 Matcher<string> m2 = StrCaseNe(string("Hello"));
1176 EXPECT_TRUE(m2.Matches(""));
1177 EXPECT_FALSE(m2.Matches("Hello"));
1178}
1179
1180TEST(StrCaseNeTest, CanDescribeSelf) {
1181 Matcher<const char*> m = StrCaseNe("Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001182 EXPECT_EQ("isn't equal to (ignoring case) \"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001183}
1184
1185// Tests that HasSubstr() works for matching string-typed values.
1186TEST(HasSubstrTest, WorksForStringClasses) {
1187 const Matcher<string> m1 = HasSubstr("foo");
1188 EXPECT_TRUE(m1.Matches(string("I love food.")));
1189 EXPECT_FALSE(m1.Matches(string("tofo")));
1190
1191 const Matcher<const std::string&> m2 = HasSubstr("foo");
1192 EXPECT_TRUE(m2.Matches(std::string("I love food.")));
1193 EXPECT_FALSE(m2.Matches(std::string("tofo")));
1194}
1195
1196// Tests that HasSubstr() works for matching C-string-typed values.
1197TEST(HasSubstrTest, WorksForCStrings) {
1198 const Matcher<char*> m1 = HasSubstr("foo");
1199 EXPECT_TRUE(m1.Matches(const_cast<char*>("I love food.")));
1200 EXPECT_FALSE(m1.Matches(const_cast<char*>("tofo")));
1201 EXPECT_FALSE(m1.Matches(NULL));
1202
1203 const Matcher<const char*> m2 = HasSubstr("foo");
1204 EXPECT_TRUE(m2.Matches("I love food."));
1205 EXPECT_FALSE(m2.Matches("tofo"));
1206 EXPECT_FALSE(m2.Matches(NULL));
1207}
1208
1209// Tests that HasSubstr(s) describes itself properly.
1210TEST(HasSubstrTest, CanDescribeSelf) {
1211 Matcher<string> m = HasSubstr("foo\n\"");
1212 EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m));
1213}
1214
zhanyong.wanb5937da2009-07-16 20:26:41 +00001215TEST(KeyTest, CanDescribeSelf) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001216 Matcher<const pair<std::string, int>&> m = Key("foo");
zhanyong.wanb5937da2009-07-16 20:26:41 +00001217 EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001218 EXPECT_EQ("doesn't have a key that is equal to \"foo\"", DescribeNegation(m));
1219}
1220
1221TEST(KeyTest, ExplainsResult) {
1222 Matcher<pair<int, bool> > m = Key(GreaterThan(10));
1223 EXPECT_EQ("whose first field is a value which is 5 less than 10",
1224 Explain(m, make_pair(5, true)));
1225 EXPECT_EQ("whose first field is a value which is 5 more than 10",
1226 Explain(m, make_pair(15, true)));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001227}
1228
1229TEST(KeyTest, MatchesCorrectly) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001230 pair<int, std::string> p(25, "foo");
zhanyong.wanb5937da2009-07-16 20:26:41 +00001231 EXPECT_THAT(p, Key(25));
1232 EXPECT_THAT(p, Not(Key(42)));
1233 EXPECT_THAT(p, Key(Ge(20)));
1234 EXPECT_THAT(p, Not(Key(Lt(25))));
1235}
1236
1237TEST(KeyTest, SafelyCastsInnerMatcher) {
1238 Matcher<int> is_positive = Gt(0);
1239 Matcher<int> is_negative = Lt(0);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001240 pair<char, bool> p('a', true);
zhanyong.wanb5937da2009-07-16 20:26:41 +00001241 EXPECT_THAT(p, Key(is_positive));
1242 EXPECT_THAT(p, Not(Key(is_negative)));
1243}
1244
1245TEST(KeyTest, InsideContainsUsingMap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001246 map<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001247 container.insert(make_pair(1, 'a'));
1248 container.insert(make_pair(2, 'b'));
1249 container.insert(make_pair(4, 'c'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001250 EXPECT_THAT(container, Contains(Key(1)));
1251 EXPECT_THAT(container, Not(Contains(Key(3))));
1252}
1253
1254TEST(KeyTest, InsideContainsUsingMultimap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001255 multimap<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001256 container.insert(make_pair(1, 'a'));
1257 container.insert(make_pair(2, 'b'));
1258 container.insert(make_pair(4, 'c'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001259
1260 EXPECT_THAT(container, Not(Contains(Key(25))));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001261 container.insert(make_pair(25, 'd'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001262 EXPECT_THAT(container, Contains(Key(25)));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001263 container.insert(make_pair(25, 'e'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001264 EXPECT_THAT(container, Contains(Key(25)));
1265
1266 EXPECT_THAT(container, Contains(Key(1)));
1267 EXPECT_THAT(container, Not(Contains(Key(3))));
1268}
1269
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001270TEST(PairTest, Typing) {
1271 // Test verifies the following type conversions can be compiled.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001272 Matcher<const pair<const char*, int>&> m1 = Pair("foo", 42);
1273 Matcher<const pair<const char*, int> > m2 = Pair("foo", 42);
1274 Matcher<pair<const char*, int> > m3 = Pair("foo", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001275
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001276 Matcher<pair<int, const std::string> > m4 = Pair(25, "42");
1277 Matcher<pair<const std::string, int> > m5 = Pair("25", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001278}
1279
1280TEST(PairTest, CanDescribeSelf) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001281 Matcher<const pair<std::string, int>&> m1 = Pair("foo", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001282 EXPECT_EQ("has a first field that is equal to \"foo\""
1283 ", and has a second field that is equal to 42",
1284 Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001285 EXPECT_EQ("has a first field that isn't equal to \"foo\""
1286 ", or has a second field that isn't equal to 42",
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001287 DescribeNegation(m1));
1288 // Double and triple negation (1 or 2 times not and description of negation).
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001289 Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42));
1290 EXPECT_EQ("has a first field that isn't equal to 13"
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001291 ", and has a second field that is equal to 42",
1292 DescribeNegation(m2));
1293}
1294
1295TEST(PairTest, CanExplainMatchResultTo) {
zhanyong.wan82113312010-01-08 21:55:40 +00001296 // If neither field matches, Pair() should explain about the first
1297 // field.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001298 const Matcher<pair<int, int> > m = Pair(GreaterThan(0), GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001299 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001300 Explain(m, make_pair(-1, -2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001301
zhanyong.wan82113312010-01-08 21:55:40 +00001302 // If the first field matches but the second doesn't, Pair() should
1303 // explain about the second field.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001304 EXPECT_EQ("whose second field does not match, which is 2 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001305 Explain(m, make_pair(1, -2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001306
zhanyong.wan82113312010-01-08 21:55:40 +00001307 // If the first field doesn't match but the second does, Pair()
1308 // should explain about the first field.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001309 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001310 Explain(m, make_pair(-1, 2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001311
zhanyong.wan82113312010-01-08 21:55:40 +00001312 // If both fields match, Pair() should explain about them both.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001313 EXPECT_EQ("whose both fields match, where the first field is a value "
1314 "which is 1 more than 0, and the second field is a value "
1315 "which is 2 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001316 Explain(m, make_pair(1, 2)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001317
1318 // If only the first match has an explanation, only this explanation should
1319 // be printed.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001320 const Matcher<pair<int, int> > explain_first = Pair(GreaterThan(0), 0);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001321 EXPECT_EQ("whose both fields match, where the first field is a value "
1322 "which is 1 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001323 Explain(explain_first, make_pair(1, 0)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001324
1325 // If only the second match has an explanation, only this explanation should
1326 // be printed.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001327 const Matcher<pair<int, int> > explain_second = Pair(0, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001328 EXPECT_EQ("whose both fields match, where the second field is a value "
1329 "which is 1 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001330 Explain(explain_second, make_pair(0, 1)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001331}
1332
1333TEST(PairTest, MatchesCorrectly) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001334 pair<int, std::string> p(25, "foo");
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001335
1336 // Both fields match.
1337 EXPECT_THAT(p, Pair(25, "foo"));
1338 EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o")));
1339
1340 // 'first' doesnt' match, but 'second' matches.
1341 EXPECT_THAT(p, Not(Pair(42, "foo")));
1342 EXPECT_THAT(p, Not(Pair(Lt(25), "foo")));
1343
1344 // 'first' matches, but 'second' doesn't match.
1345 EXPECT_THAT(p, Not(Pair(25, "bar")));
1346 EXPECT_THAT(p, Not(Pair(25, Not("foo"))));
1347
1348 // Neither field matches.
1349 EXPECT_THAT(p, Not(Pair(13, "bar")));
1350 EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a"))));
1351}
1352
1353TEST(PairTest, SafelyCastsInnerMatchers) {
1354 Matcher<int> is_positive = Gt(0);
1355 Matcher<int> is_negative = Lt(0);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001356 pair<char, bool> p('a', true);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001357 EXPECT_THAT(p, Pair(is_positive, _));
1358 EXPECT_THAT(p, Not(Pair(is_negative, _)));
1359 EXPECT_THAT(p, Pair(_, is_positive));
1360 EXPECT_THAT(p, Not(Pair(_, is_negative)));
1361}
1362
1363TEST(PairTest, InsideContainsUsingMap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001364 map<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001365 container.insert(make_pair(1, 'a'));
1366 container.insert(make_pair(2, 'b'));
1367 container.insert(make_pair(4, 'c'));
zhanyong.wan95b12332009-09-25 18:55:50 +00001368 EXPECT_THAT(container, Contains(Pair(1, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001369 EXPECT_THAT(container, Contains(Pair(1, _)));
zhanyong.wan95b12332009-09-25 18:55:50 +00001370 EXPECT_THAT(container, Contains(Pair(_, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001371 EXPECT_THAT(container, Not(Contains(Pair(3, _))));
1372}
1373
shiqiane35fdd92008-12-10 05:08:54 +00001374// Tests StartsWith(s).
1375
1376TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
1377 const Matcher<const char*> m1 = StartsWith(string(""));
1378 EXPECT_TRUE(m1.Matches("Hi"));
1379 EXPECT_TRUE(m1.Matches(""));
1380 EXPECT_FALSE(m1.Matches(NULL));
1381
1382 const Matcher<const string&> m2 = StartsWith("Hi");
1383 EXPECT_TRUE(m2.Matches("Hi"));
1384 EXPECT_TRUE(m2.Matches("Hi Hi!"));
1385 EXPECT_TRUE(m2.Matches("High"));
1386 EXPECT_FALSE(m2.Matches("H"));
1387 EXPECT_FALSE(m2.Matches(" Hi"));
1388}
1389
1390TEST(StartsWithTest, CanDescribeSelf) {
1391 Matcher<const std::string> m = StartsWith("Hi");
1392 EXPECT_EQ("starts with \"Hi\"", Describe(m));
1393}
1394
1395// Tests EndsWith(s).
1396
1397TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
1398 const Matcher<const char*> m1 = EndsWith("");
1399 EXPECT_TRUE(m1.Matches("Hi"));
1400 EXPECT_TRUE(m1.Matches(""));
1401 EXPECT_FALSE(m1.Matches(NULL));
1402
1403 const Matcher<const string&> m2 = EndsWith(string("Hi"));
1404 EXPECT_TRUE(m2.Matches("Hi"));
1405 EXPECT_TRUE(m2.Matches("Wow Hi Hi"));
1406 EXPECT_TRUE(m2.Matches("Super Hi"));
1407 EXPECT_FALSE(m2.Matches("i"));
1408 EXPECT_FALSE(m2.Matches("Hi "));
1409}
1410
1411TEST(EndsWithTest, CanDescribeSelf) {
1412 Matcher<const std::string> m = EndsWith("Hi");
1413 EXPECT_EQ("ends with \"Hi\"", Describe(m));
1414}
1415
shiqiane35fdd92008-12-10 05:08:54 +00001416// Tests MatchesRegex().
1417
1418TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
1419 const Matcher<const char*> m1 = MatchesRegex("a.*z");
1420 EXPECT_TRUE(m1.Matches("az"));
1421 EXPECT_TRUE(m1.Matches("abcz"));
1422 EXPECT_FALSE(m1.Matches(NULL));
1423
1424 const Matcher<const string&> m2 = MatchesRegex(new RE("a.*z"));
1425 EXPECT_TRUE(m2.Matches("azbz"));
1426 EXPECT_FALSE(m2.Matches("az1"));
1427 EXPECT_FALSE(m2.Matches("1az"));
1428}
1429
1430TEST(MatchesRegexTest, CanDescribeSelf) {
1431 Matcher<const std::string> m1 = MatchesRegex(string("Hi.*"));
1432 EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1));
1433
zhanyong.wand14aaed2010-01-14 05:36:32 +00001434 Matcher<const char*> m2 = MatchesRegex(new RE("a.*"));
1435 EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +00001436}
1437
1438// Tests ContainsRegex().
1439
1440TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
1441 const Matcher<const char*> m1 = ContainsRegex(string("a.*z"));
1442 EXPECT_TRUE(m1.Matches("az"));
1443 EXPECT_TRUE(m1.Matches("0abcz1"));
1444 EXPECT_FALSE(m1.Matches(NULL));
1445
1446 const Matcher<const string&> m2 = ContainsRegex(new RE("a.*z"));
1447 EXPECT_TRUE(m2.Matches("azbz"));
1448 EXPECT_TRUE(m2.Matches("az1"));
1449 EXPECT_FALSE(m2.Matches("1a"));
1450}
1451
1452TEST(ContainsRegexTest, CanDescribeSelf) {
1453 Matcher<const std::string> m1 = ContainsRegex("Hi.*");
1454 EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1));
1455
zhanyong.wand14aaed2010-01-14 05:36:32 +00001456 Matcher<const char*> m2 = ContainsRegex(new RE("a.*"));
1457 EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +00001458}
shiqiane35fdd92008-12-10 05:08:54 +00001459
1460// Tests for wide strings.
1461#if GTEST_HAS_STD_WSTRING
1462TEST(StdWideStrEqTest, MatchesEqual) {
1463 Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello"));
1464 EXPECT_TRUE(m.Matches(L"Hello"));
1465 EXPECT_FALSE(m.Matches(L"hello"));
1466 EXPECT_FALSE(m.Matches(NULL));
1467
1468 Matcher<const ::std::wstring&> m2 = StrEq(L"Hello");
1469 EXPECT_TRUE(m2.Matches(L"Hello"));
1470 EXPECT_FALSE(m2.Matches(L"Hi"));
1471
1472 Matcher<const ::std::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1473 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1474 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1475
1476 ::std::wstring str(L"01204500800");
1477 str[3] = L'\0';
1478 Matcher<const ::std::wstring&> m4 = StrEq(str);
1479 EXPECT_TRUE(m4.Matches(str));
1480 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1481 Matcher<const ::std::wstring&> m5 = StrEq(str);
1482 EXPECT_TRUE(m5.Matches(str));
1483}
1484
1485TEST(StdWideStrEqTest, CanDescribeSelf) {
vladlosevaa432202011-04-01 21:58:42 +00001486 Matcher< ::std::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v");
1487 EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
shiqiane35fdd92008-12-10 05:08:54 +00001488 Describe(m));
1489
1490 Matcher< ::std::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1491 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1492 Describe(m2));
1493
1494 ::std::wstring str(L"01204500800");
1495 str[3] = L'\0';
1496 Matcher<const ::std::wstring&> m4 = StrEq(str);
1497 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1498 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1499 Matcher<const ::std::wstring&> m5 = StrEq(str);
1500 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1501}
1502
1503TEST(StdWideStrNeTest, MatchesUnequalString) {
1504 Matcher<const wchar_t*> m = StrNe(L"Hello");
1505 EXPECT_TRUE(m.Matches(L""));
1506 EXPECT_TRUE(m.Matches(NULL));
1507 EXPECT_FALSE(m.Matches(L"Hello"));
1508
1509 Matcher< ::std::wstring> m2 = StrNe(::std::wstring(L"Hello"));
1510 EXPECT_TRUE(m2.Matches(L"hello"));
1511 EXPECT_FALSE(m2.Matches(L"Hello"));
1512}
1513
1514TEST(StdWideStrNeTest, CanDescribeSelf) {
1515 Matcher<const wchar_t*> m = StrNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001516 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001517}
1518
1519TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1520 Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello"));
1521 EXPECT_TRUE(m.Matches(L"Hello"));
1522 EXPECT_TRUE(m.Matches(L"hello"));
1523 EXPECT_FALSE(m.Matches(L"Hi"));
1524 EXPECT_FALSE(m.Matches(NULL));
1525
1526 Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello");
1527 EXPECT_TRUE(m2.Matches(L"hello"));
1528 EXPECT_FALSE(m2.Matches(L"Hi"));
1529}
1530
1531TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1532 ::std::wstring str1(L"oabocdooeoo");
1533 ::std::wstring str2(L"OABOCDOOEOO");
1534 Matcher<const ::std::wstring&> m0 = StrCaseEq(str1);
1535 EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L'\0')));
1536
1537 str1[3] = str2[3] = L'\0';
1538 Matcher<const ::std::wstring&> m1 = StrCaseEq(str1);
1539 EXPECT_TRUE(m1.Matches(str2));
1540
1541 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1542 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1543 Matcher<const ::std::wstring&> m2 = StrCaseEq(str1);
1544 str1[9] = str2[9] = L'\0';
1545 EXPECT_FALSE(m2.Matches(str2));
1546
1547 Matcher<const ::std::wstring&> m3 = StrCaseEq(str1);
1548 EXPECT_TRUE(m3.Matches(str2));
1549
1550 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1551 str2.append(1, L'\0');
1552 EXPECT_FALSE(m3.Matches(str2));
1553 EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9)));
1554}
1555
1556TEST(StdWideStrCaseEqTest, CanDescribeSelf) {
1557 Matcher< ::std::wstring> m = StrCaseEq(L"Hi");
1558 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1559}
1560
1561TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1562 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1563 EXPECT_TRUE(m.Matches(L"Hi"));
1564 EXPECT_TRUE(m.Matches(NULL));
1565 EXPECT_FALSE(m.Matches(L"Hello"));
1566 EXPECT_FALSE(m.Matches(L"hello"));
1567
1568 Matcher< ::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello"));
1569 EXPECT_TRUE(m2.Matches(L""));
1570 EXPECT_FALSE(m2.Matches(L"Hello"));
1571}
1572
1573TEST(StdWideStrCaseNeTest, CanDescribeSelf) {
1574 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001575 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001576}
1577
1578// Tests that HasSubstr() works for matching wstring-typed values.
1579TEST(StdWideHasSubstrTest, WorksForStringClasses) {
1580 const Matcher< ::std::wstring> m1 = HasSubstr(L"foo");
1581 EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food.")));
1582 EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo")));
1583
1584 const Matcher<const ::std::wstring&> m2 = HasSubstr(L"foo");
1585 EXPECT_TRUE(m2.Matches(::std::wstring(L"I love food.")));
1586 EXPECT_FALSE(m2.Matches(::std::wstring(L"tofo")));
1587}
1588
1589// Tests that HasSubstr() works for matching C-wide-string-typed values.
1590TEST(StdWideHasSubstrTest, WorksForCStrings) {
1591 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1592 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1593 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1594 EXPECT_FALSE(m1.Matches(NULL));
1595
1596 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1597 EXPECT_TRUE(m2.Matches(L"I love food."));
1598 EXPECT_FALSE(m2.Matches(L"tofo"));
1599 EXPECT_FALSE(m2.Matches(NULL));
1600}
1601
1602// Tests that HasSubstr(s) describes itself properly.
1603TEST(StdWideHasSubstrTest, CanDescribeSelf) {
1604 Matcher< ::std::wstring> m = HasSubstr(L"foo\n\"");
1605 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1606}
1607
1608// Tests StartsWith(s).
1609
1610TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) {
1611 const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L""));
1612 EXPECT_TRUE(m1.Matches(L"Hi"));
1613 EXPECT_TRUE(m1.Matches(L""));
1614 EXPECT_FALSE(m1.Matches(NULL));
1615
1616 const Matcher<const ::std::wstring&> m2 = StartsWith(L"Hi");
1617 EXPECT_TRUE(m2.Matches(L"Hi"));
1618 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1619 EXPECT_TRUE(m2.Matches(L"High"));
1620 EXPECT_FALSE(m2.Matches(L"H"));
1621 EXPECT_FALSE(m2.Matches(L" Hi"));
1622}
1623
1624TEST(StdWideStartsWithTest, CanDescribeSelf) {
1625 Matcher<const ::std::wstring> m = StartsWith(L"Hi");
1626 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1627}
1628
1629// Tests EndsWith(s).
1630
1631TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) {
1632 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1633 EXPECT_TRUE(m1.Matches(L"Hi"));
1634 EXPECT_TRUE(m1.Matches(L""));
1635 EXPECT_FALSE(m1.Matches(NULL));
1636
1637 const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L"Hi"));
1638 EXPECT_TRUE(m2.Matches(L"Hi"));
1639 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1640 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1641 EXPECT_FALSE(m2.Matches(L"i"));
1642 EXPECT_FALSE(m2.Matches(L"Hi "));
1643}
1644
1645TEST(StdWideEndsWithTest, CanDescribeSelf) {
1646 Matcher<const ::std::wstring> m = EndsWith(L"Hi");
1647 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1648}
1649
1650#endif // GTEST_HAS_STD_WSTRING
1651
1652#if GTEST_HAS_GLOBAL_WSTRING
1653TEST(GlobalWideStrEqTest, MatchesEqual) {
1654 Matcher<const wchar_t*> m = StrEq(::wstring(L"Hello"));
1655 EXPECT_TRUE(m.Matches(L"Hello"));
1656 EXPECT_FALSE(m.Matches(L"hello"));
1657 EXPECT_FALSE(m.Matches(NULL));
1658
1659 Matcher<const ::wstring&> m2 = StrEq(L"Hello");
1660 EXPECT_TRUE(m2.Matches(L"Hello"));
1661 EXPECT_FALSE(m2.Matches(L"Hi"));
1662
1663 Matcher<const ::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1664 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1665 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1666
1667 ::wstring str(L"01204500800");
1668 str[3] = L'\0';
1669 Matcher<const ::wstring&> m4 = StrEq(str);
1670 EXPECT_TRUE(m4.Matches(str));
1671 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1672 Matcher<const ::wstring&> m5 = StrEq(str);
1673 EXPECT_TRUE(m5.Matches(str));
1674}
1675
1676TEST(GlobalWideStrEqTest, CanDescribeSelf) {
vladlosevaa432202011-04-01 21:58:42 +00001677 Matcher< ::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v");
1678 EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
shiqiane35fdd92008-12-10 05:08:54 +00001679 Describe(m));
1680
1681 Matcher< ::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1682 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1683 Describe(m2));
1684
1685 ::wstring str(L"01204500800");
1686 str[3] = L'\0';
1687 Matcher<const ::wstring&> m4 = StrEq(str);
1688 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1689 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1690 Matcher<const ::wstring&> m5 = StrEq(str);
1691 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1692}
1693
1694TEST(GlobalWideStrNeTest, MatchesUnequalString) {
1695 Matcher<const wchar_t*> m = StrNe(L"Hello");
1696 EXPECT_TRUE(m.Matches(L""));
1697 EXPECT_TRUE(m.Matches(NULL));
1698 EXPECT_FALSE(m.Matches(L"Hello"));
1699
1700 Matcher< ::wstring> m2 = StrNe(::wstring(L"Hello"));
1701 EXPECT_TRUE(m2.Matches(L"hello"));
1702 EXPECT_FALSE(m2.Matches(L"Hello"));
1703}
1704
1705TEST(GlobalWideStrNeTest, CanDescribeSelf) {
1706 Matcher<const wchar_t*> m = StrNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001707 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001708}
1709
1710TEST(GlobalWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1711 Matcher<const wchar_t*> m = StrCaseEq(::wstring(L"Hello"));
1712 EXPECT_TRUE(m.Matches(L"Hello"));
1713 EXPECT_TRUE(m.Matches(L"hello"));
1714 EXPECT_FALSE(m.Matches(L"Hi"));
1715 EXPECT_FALSE(m.Matches(NULL));
1716
1717 Matcher<const ::wstring&> m2 = StrCaseEq(L"Hello");
1718 EXPECT_TRUE(m2.Matches(L"hello"));
1719 EXPECT_FALSE(m2.Matches(L"Hi"));
1720}
1721
1722TEST(GlobalWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1723 ::wstring str1(L"oabocdooeoo");
1724 ::wstring str2(L"OABOCDOOEOO");
1725 Matcher<const ::wstring&> m0 = StrCaseEq(str1);
1726 EXPECT_FALSE(m0.Matches(str2 + ::wstring(1, L'\0')));
1727
1728 str1[3] = str2[3] = L'\0';
1729 Matcher<const ::wstring&> m1 = StrCaseEq(str1);
1730 EXPECT_TRUE(m1.Matches(str2));
1731
1732 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1733 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1734 Matcher<const ::wstring&> m2 = StrCaseEq(str1);
1735 str1[9] = str2[9] = L'\0';
1736 EXPECT_FALSE(m2.Matches(str2));
1737
1738 Matcher<const ::wstring&> m3 = StrCaseEq(str1);
1739 EXPECT_TRUE(m3.Matches(str2));
1740
1741 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1742 str2.append(1, L'\0');
1743 EXPECT_FALSE(m3.Matches(str2));
1744 EXPECT_FALSE(m3.Matches(::wstring(str2, 0, 9)));
1745}
1746
1747TEST(GlobalWideStrCaseEqTest, CanDescribeSelf) {
1748 Matcher< ::wstring> m = StrCaseEq(L"Hi");
1749 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1750}
1751
1752TEST(GlobalWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1753 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1754 EXPECT_TRUE(m.Matches(L"Hi"));
1755 EXPECT_TRUE(m.Matches(NULL));
1756 EXPECT_FALSE(m.Matches(L"Hello"));
1757 EXPECT_FALSE(m.Matches(L"hello"));
1758
1759 Matcher< ::wstring> m2 = StrCaseNe(::wstring(L"Hello"));
1760 EXPECT_TRUE(m2.Matches(L""));
1761 EXPECT_FALSE(m2.Matches(L"Hello"));
1762}
1763
1764TEST(GlobalWideStrCaseNeTest, CanDescribeSelf) {
1765 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001766 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001767}
1768
1769// Tests that HasSubstr() works for matching wstring-typed values.
1770TEST(GlobalWideHasSubstrTest, WorksForStringClasses) {
1771 const Matcher< ::wstring> m1 = HasSubstr(L"foo");
1772 EXPECT_TRUE(m1.Matches(::wstring(L"I love food.")));
1773 EXPECT_FALSE(m1.Matches(::wstring(L"tofo")));
1774
1775 const Matcher<const ::wstring&> m2 = HasSubstr(L"foo");
1776 EXPECT_TRUE(m2.Matches(::wstring(L"I love food.")));
1777 EXPECT_FALSE(m2.Matches(::wstring(L"tofo")));
1778}
1779
1780// Tests that HasSubstr() works for matching C-wide-string-typed values.
1781TEST(GlobalWideHasSubstrTest, WorksForCStrings) {
1782 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1783 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1784 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1785 EXPECT_FALSE(m1.Matches(NULL));
1786
1787 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1788 EXPECT_TRUE(m2.Matches(L"I love food."));
1789 EXPECT_FALSE(m2.Matches(L"tofo"));
1790 EXPECT_FALSE(m2.Matches(NULL));
1791}
1792
1793// Tests that HasSubstr(s) describes itself properly.
1794TEST(GlobalWideHasSubstrTest, CanDescribeSelf) {
1795 Matcher< ::wstring> m = HasSubstr(L"foo\n\"");
1796 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1797}
1798
1799// Tests StartsWith(s).
1800
1801TEST(GlobalWideStartsWithTest, MatchesStringWithGivenPrefix) {
1802 const Matcher<const wchar_t*> m1 = StartsWith(::wstring(L""));
1803 EXPECT_TRUE(m1.Matches(L"Hi"));
1804 EXPECT_TRUE(m1.Matches(L""));
1805 EXPECT_FALSE(m1.Matches(NULL));
1806
1807 const Matcher<const ::wstring&> m2 = StartsWith(L"Hi");
1808 EXPECT_TRUE(m2.Matches(L"Hi"));
1809 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1810 EXPECT_TRUE(m2.Matches(L"High"));
1811 EXPECT_FALSE(m2.Matches(L"H"));
1812 EXPECT_FALSE(m2.Matches(L" Hi"));
1813}
1814
1815TEST(GlobalWideStartsWithTest, CanDescribeSelf) {
1816 Matcher<const ::wstring> m = StartsWith(L"Hi");
1817 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1818}
1819
1820// Tests EndsWith(s).
1821
1822TEST(GlobalWideEndsWithTest, MatchesStringWithGivenSuffix) {
1823 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1824 EXPECT_TRUE(m1.Matches(L"Hi"));
1825 EXPECT_TRUE(m1.Matches(L""));
1826 EXPECT_FALSE(m1.Matches(NULL));
1827
1828 const Matcher<const ::wstring&> m2 = EndsWith(::wstring(L"Hi"));
1829 EXPECT_TRUE(m2.Matches(L"Hi"));
1830 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1831 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1832 EXPECT_FALSE(m2.Matches(L"i"));
1833 EXPECT_FALSE(m2.Matches(L"Hi "));
1834}
1835
1836TEST(GlobalWideEndsWithTest, CanDescribeSelf) {
1837 Matcher<const ::wstring> m = EndsWith(L"Hi");
1838 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1839}
1840
1841#endif // GTEST_HAS_GLOBAL_WSTRING
1842
1843
1844typedef ::std::tr1::tuple<long, int> Tuple2; // NOLINT
1845
1846// Tests that Eq() matches a 2-tuple where the first field == the
1847// second field.
1848TEST(Eq2Test, MatchesEqualArguments) {
1849 Matcher<const Tuple2&> m = Eq();
1850 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1851 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1852}
1853
1854// Tests that Eq() describes itself properly.
1855TEST(Eq2Test, CanDescribeSelf) {
1856 Matcher<const Tuple2&> m = Eq();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001857 EXPECT_EQ("are an equal pair", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001858}
1859
1860// Tests that Ge() matches a 2-tuple where the first field >= the
1861// second field.
1862TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) {
1863 Matcher<const Tuple2&> m = Ge();
1864 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1865 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1866 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1867}
1868
1869// Tests that Ge() describes itself properly.
1870TEST(Ge2Test, CanDescribeSelf) {
1871 Matcher<const Tuple2&> m = Ge();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001872 EXPECT_EQ("are a pair where the first >= the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001873}
1874
1875// Tests that Gt() matches a 2-tuple where the first field > the
1876// second field.
1877TEST(Gt2Test, MatchesGreaterThanArguments) {
1878 Matcher<const Tuple2&> m = Gt();
1879 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1880 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1881 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1882}
1883
1884// Tests that Gt() describes itself properly.
1885TEST(Gt2Test, CanDescribeSelf) {
1886 Matcher<const Tuple2&> m = Gt();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001887 EXPECT_EQ("are a pair where the first > the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001888}
1889
1890// Tests that Le() matches a 2-tuple where the first field <= the
1891// second field.
1892TEST(Le2Test, MatchesLessThanOrEqualArguments) {
1893 Matcher<const Tuple2&> m = Le();
1894 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1895 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1896 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1897}
1898
1899// Tests that Le() describes itself properly.
1900TEST(Le2Test, CanDescribeSelf) {
1901 Matcher<const Tuple2&> m = Le();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001902 EXPECT_EQ("are a pair where the first <= the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001903}
1904
1905// Tests that Lt() matches a 2-tuple where the first field < the
1906// second field.
1907TEST(Lt2Test, MatchesLessThanArguments) {
1908 Matcher<const Tuple2&> m = Lt();
1909 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1910 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1911 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1912}
1913
1914// Tests that Lt() describes itself properly.
1915TEST(Lt2Test, CanDescribeSelf) {
1916 Matcher<const Tuple2&> m = Lt();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001917 EXPECT_EQ("are a pair where the first < the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001918}
1919
1920// Tests that Ne() matches a 2-tuple where the first field != the
1921// second field.
1922TEST(Ne2Test, MatchesUnequalArguments) {
1923 Matcher<const Tuple2&> m = Ne();
1924 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1925 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1926 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1927}
1928
1929// Tests that Ne() describes itself properly.
1930TEST(Ne2Test, CanDescribeSelf) {
1931 Matcher<const Tuple2&> m = Ne();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001932 EXPECT_EQ("are an unequal pair", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001933}
1934
1935// Tests that Not(m) matches any value that doesn't match m.
1936TEST(NotTest, NegatesMatcher) {
1937 Matcher<int> m;
1938 m = Not(Eq(2));
1939 EXPECT_TRUE(m.Matches(3));
1940 EXPECT_FALSE(m.Matches(2));
1941}
1942
1943// Tests that Not(m) describes itself properly.
1944TEST(NotTest, CanDescribeSelf) {
1945 Matcher<int> m = Not(Eq(5));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001946 EXPECT_EQ("isn't equal to 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001947}
1948
zhanyong.wan18490652009-05-11 18:54:08 +00001949// Tests that monomorphic matchers are safely cast by the Not matcher.
1950TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {
1951 // greater_than_5 is a monomorphic matcher.
1952 Matcher<int> greater_than_5 = Gt(5);
1953
1954 Matcher<const int&> m = Not(greater_than_5);
1955 Matcher<int&> m2 = Not(greater_than_5);
1956 Matcher<int&> m3 = Not(m);
1957}
1958
zhanyong.wan02c15052010-06-09 19:21:30 +00001959// Helper to allow easy testing of AllOf matchers with num parameters.
1960void AllOfMatches(int num, const Matcher<int>& m) {
1961 SCOPED_TRACE(Describe(m));
1962 EXPECT_TRUE(m.Matches(0));
1963 for (int i = 1; i <= num; ++i) {
1964 EXPECT_FALSE(m.Matches(i));
1965 }
1966 EXPECT_TRUE(m.Matches(num + 1));
1967}
1968
shiqiane35fdd92008-12-10 05:08:54 +00001969// Tests that AllOf(m1, ..., mn) matches any value that matches all of
1970// the given matchers.
1971TEST(AllOfTest, MatchesWhenAllMatch) {
1972 Matcher<int> m;
1973 m = AllOf(Le(2), Ge(1));
1974 EXPECT_TRUE(m.Matches(1));
1975 EXPECT_TRUE(m.Matches(2));
1976 EXPECT_FALSE(m.Matches(0));
1977 EXPECT_FALSE(m.Matches(3));
1978
1979 m = AllOf(Gt(0), Ne(1), Ne(2));
1980 EXPECT_TRUE(m.Matches(3));
1981 EXPECT_FALSE(m.Matches(2));
1982 EXPECT_FALSE(m.Matches(1));
1983 EXPECT_FALSE(m.Matches(0));
1984
1985 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
1986 EXPECT_TRUE(m.Matches(4));
1987 EXPECT_FALSE(m.Matches(3));
1988 EXPECT_FALSE(m.Matches(2));
1989 EXPECT_FALSE(m.Matches(1));
1990 EXPECT_FALSE(m.Matches(0));
1991
1992 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
1993 EXPECT_TRUE(m.Matches(0));
1994 EXPECT_TRUE(m.Matches(1));
1995 EXPECT_FALSE(m.Matches(3));
zhanyong.wan02c15052010-06-09 19:21:30 +00001996
1997 // The following tests for varying number of sub-matchers. Due to the way
1998 // the sub-matchers are handled it is enough to test every sub-matcher once
1999 // with sub-matchers using the same matcher type. Varying matcher types are
2000 // checked for above.
2001 AllOfMatches(2, AllOf(Ne(1), Ne(2)));
2002 AllOfMatches(3, AllOf(Ne(1), Ne(2), Ne(3)));
2003 AllOfMatches(4, AllOf(Ne(1), Ne(2), Ne(3), Ne(4)));
2004 AllOfMatches(5, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5)));
2005 AllOfMatches(6, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6)));
2006 AllOfMatches(7, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7)));
2007 AllOfMatches(8, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
2008 Ne(8)));
2009 AllOfMatches(9, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
2010 Ne(8), Ne(9)));
2011 AllOfMatches(10, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8),
2012 Ne(9), Ne(10)));
shiqiane35fdd92008-12-10 05:08:54 +00002013}
2014
2015// Tests that AllOf(m1, ..., mn) describes itself properly.
2016TEST(AllOfTest, CanDescribeSelf) {
2017 Matcher<int> m;
2018 m = AllOf(Le(2), Ge(1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002019 EXPECT_EQ("(is <= 2) and (is >= 1)", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00002020
2021 m = AllOf(Gt(0), Ne(1), Ne(2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002022 EXPECT_EQ("(is > 0) and "
2023 "((isn't equal to 1) and "
2024 "(isn't equal to 2))",
shiqiane35fdd92008-12-10 05:08:54 +00002025 Describe(m));
2026
2027
2028 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
jgm79a367e2012-04-10 16:02:11 +00002029 EXPECT_EQ("((is > 0) and "
2030 "(isn't equal to 1)) and "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002031 "((isn't equal to 2) and "
jgm79a367e2012-04-10 16:02:11 +00002032 "(isn't equal to 3))",
shiqiane35fdd92008-12-10 05:08:54 +00002033 Describe(m));
2034
2035
2036 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
jgm79a367e2012-04-10 16:02:11 +00002037 EXPECT_EQ("((is >= 0) and "
2038 "(is < 10)) and "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002039 "((isn't equal to 3) and "
2040 "((isn't equal to 5) and "
jgm79a367e2012-04-10 16:02:11 +00002041 "(isn't equal to 7)))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002042 Describe(m));
2043}
2044
2045// Tests that AllOf(m1, ..., mn) describes its negation properly.
2046TEST(AllOfTest, CanDescribeNegation) {
2047 Matcher<int> m;
2048 m = AllOf(Le(2), Ge(1));
2049 EXPECT_EQ("(isn't <= 2) or "
2050 "(isn't >= 1)",
2051 DescribeNegation(m));
2052
2053 m = AllOf(Gt(0), Ne(1), Ne(2));
2054 EXPECT_EQ("(isn't > 0) or "
2055 "((is equal to 1) or "
2056 "(is equal to 2))",
2057 DescribeNegation(m));
2058
2059
2060 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
jgm79a367e2012-04-10 16:02:11 +00002061 EXPECT_EQ("((isn't > 0) or "
2062 "(is equal to 1)) or "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002063 "((is equal to 2) or "
jgm79a367e2012-04-10 16:02:11 +00002064 "(is equal to 3))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002065 DescribeNegation(m));
2066
2067
2068 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
jgm79a367e2012-04-10 16:02:11 +00002069 EXPECT_EQ("((isn't >= 0) or "
2070 "(isn't < 10)) or "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002071 "((is equal to 3) or "
2072 "((is equal to 5) or "
jgm79a367e2012-04-10 16:02:11 +00002073 "(is equal to 7)))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002074 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00002075}
2076
zhanyong.wan18490652009-05-11 18:54:08 +00002077// Tests that monomorphic matchers are safely cast by the AllOf matcher.
2078TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
2079 // greater_than_5 and less_than_10 are monomorphic matchers.
2080 Matcher<int> greater_than_5 = Gt(5);
2081 Matcher<int> less_than_10 = Lt(10);
2082
2083 Matcher<const int&> m = AllOf(greater_than_5, less_than_10);
2084 Matcher<int&> m2 = AllOf(greater_than_5, less_than_10);
2085 Matcher<int&> m3 = AllOf(greater_than_5, m2);
2086
2087 // Tests that BothOf works when composing itself.
2088 Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10);
2089 Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);
2090}
2091
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002092TEST(AllOfTest, ExplainsResult) {
2093 Matcher<int> m;
2094
2095 // Successful match. Both matchers need to explain. The second
2096 // matcher doesn't give an explanation, so only the first matcher's
2097 // explanation is printed.
2098 m = AllOf(GreaterThan(10), Lt(30));
2099 EXPECT_EQ("which is 15 more than 10", Explain(m, 25));
2100
2101 // Successful match. Both matchers need to explain.
2102 m = AllOf(GreaterThan(10), GreaterThan(20));
2103 EXPECT_EQ("which is 20 more than 10, and which is 10 more than 20",
2104 Explain(m, 30));
2105
2106 // Successful match. All matchers need to explain. The second
2107 // matcher doesn't given an explanation.
2108 m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20));
2109 EXPECT_EQ("which is 15 more than 10, and which is 5 more than 20",
2110 Explain(m, 25));
2111
2112 // Successful match. All matchers need to explain.
2113 m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2114 EXPECT_EQ("which is 30 more than 10, and which is 20 more than 20, "
2115 "and which is 10 more than 30",
2116 Explain(m, 40));
2117
2118 // Failed match. The first matcher, which failed, needs to
2119 // explain.
2120 m = AllOf(GreaterThan(10), GreaterThan(20));
2121 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
2122
2123 // Failed match. The second matcher, which failed, needs to
2124 // explain. Since it doesn't given an explanation, nothing is
2125 // printed.
2126 m = AllOf(GreaterThan(10), Lt(30));
2127 EXPECT_EQ("", Explain(m, 40));
2128
2129 // Failed match. The second matcher, which failed, needs to
2130 // explain.
2131 m = AllOf(GreaterThan(10), GreaterThan(20));
2132 EXPECT_EQ("which is 5 less than 20", Explain(m, 15));
2133}
2134
zhanyong.wan02c15052010-06-09 19:21:30 +00002135// Helper to allow easy testing of AnyOf matchers with num parameters.
2136void AnyOfMatches(int num, const Matcher<int>& m) {
2137 SCOPED_TRACE(Describe(m));
2138 EXPECT_FALSE(m.Matches(0));
2139 for (int i = 1; i <= num; ++i) {
2140 EXPECT_TRUE(m.Matches(i));
2141 }
2142 EXPECT_FALSE(m.Matches(num + 1));
2143}
2144
shiqiane35fdd92008-12-10 05:08:54 +00002145// Tests that AnyOf(m1, ..., mn) matches any value that matches at
2146// least one of the given matchers.
2147TEST(AnyOfTest, MatchesWhenAnyMatches) {
2148 Matcher<int> m;
2149 m = AnyOf(Le(1), Ge(3));
2150 EXPECT_TRUE(m.Matches(1));
2151 EXPECT_TRUE(m.Matches(4));
2152 EXPECT_FALSE(m.Matches(2));
2153
2154 m = AnyOf(Lt(0), Eq(1), Eq(2));
2155 EXPECT_TRUE(m.Matches(-1));
2156 EXPECT_TRUE(m.Matches(1));
2157 EXPECT_TRUE(m.Matches(2));
2158 EXPECT_FALSE(m.Matches(0));
2159
2160 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2161 EXPECT_TRUE(m.Matches(-1));
2162 EXPECT_TRUE(m.Matches(1));
2163 EXPECT_TRUE(m.Matches(2));
2164 EXPECT_TRUE(m.Matches(3));
2165 EXPECT_FALSE(m.Matches(0));
2166
2167 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2168 EXPECT_TRUE(m.Matches(0));
2169 EXPECT_TRUE(m.Matches(11));
2170 EXPECT_TRUE(m.Matches(3));
2171 EXPECT_FALSE(m.Matches(2));
zhanyong.wan02c15052010-06-09 19:21:30 +00002172
2173 // The following tests for varying number of sub-matchers. Due to the way
2174 // the sub-matchers are handled it is enough to test every sub-matcher once
2175 // with sub-matchers using the same matcher type. Varying matcher types are
2176 // checked for above.
2177 AnyOfMatches(2, AnyOf(1, 2));
2178 AnyOfMatches(3, AnyOf(1, 2, 3));
2179 AnyOfMatches(4, AnyOf(1, 2, 3, 4));
2180 AnyOfMatches(5, AnyOf(1, 2, 3, 4, 5));
2181 AnyOfMatches(6, AnyOf(1, 2, 3, 4, 5, 6));
2182 AnyOfMatches(7, AnyOf(1, 2, 3, 4, 5, 6, 7));
2183 AnyOfMatches(8, AnyOf(1, 2, 3, 4, 5, 6, 7, 8));
2184 AnyOfMatches(9, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9));
2185 AnyOfMatches(10, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
shiqiane35fdd92008-12-10 05:08:54 +00002186}
2187
2188// Tests that AnyOf(m1, ..., mn) describes itself properly.
2189TEST(AnyOfTest, CanDescribeSelf) {
2190 Matcher<int> m;
2191 m = AnyOf(Le(1), Ge(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002192 EXPECT_EQ("(is <= 1) or (is >= 3)",
shiqiane35fdd92008-12-10 05:08:54 +00002193 Describe(m));
2194
2195 m = AnyOf(Lt(0), Eq(1), Eq(2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002196 EXPECT_EQ("(is < 0) or "
shiqiane35fdd92008-12-10 05:08:54 +00002197 "((is equal to 1) or (is equal to 2))",
2198 Describe(m));
2199
2200 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
jgm79a367e2012-04-10 16:02:11 +00002201 EXPECT_EQ("((is < 0) or "
2202 "(is equal to 1)) or "
shiqiane35fdd92008-12-10 05:08:54 +00002203 "((is equal to 2) or "
jgm79a367e2012-04-10 16:02:11 +00002204 "(is equal to 3))",
shiqiane35fdd92008-12-10 05:08:54 +00002205 Describe(m));
2206
2207 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
jgm79a367e2012-04-10 16:02:11 +00002208 EXPECT_EQ("((is <= 0) or "
2209 "(is > 10)) or "
shiqiane35fdd92008-12-10 05:08:54 +00002210 "((is equal to 3) or "
2211 "((is equal to 5) or "
jgm79a367e2012-04-10 16:02:11 +00002212 "(is equal to 7)))",
shiqiane35fdd92008-12-10 05:08:54 +00002213 Describe(m));
2214}
2215
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002216// Tests that AnyOf(m1, ..., mn) describes its negation properly.
2217TEST(AnyOfTest, CanDescribeNegation) {
2218 Matcher<int> m;
2219 m = AnyOf(Le(1), Ge(3));
2220 EXPECT_EQ("(isn't <= 1) and (isn't >= 3)",
2221 DescribeNegation(m));
2222
2223 m = AnyOf(Lt(0), Eq(1), Eq(2));
2224 EXPECT_EQ("(isn't < 0) and "
2225 "((isn't equal to 1) and (isn't equal to 2))",
2226 DescribeNegation(m));
2227
2228 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
jgm79a367e2012-04-10 16:02:11 +00002229 EXPECT_EQ("((isn't < 0) and "
2230 "(isn't equal to 1)) and "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002231 "((isn't equal to 2) and "
jgm79a367e2012-04-10 16:02:11 +00002232 "(isn't equal to 3))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002233 DescribeNegation(m));
2234
2235 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
jgm79a367e2012-04-10 16:02:11 +00002236 EXPECT_EQ("((isn't <= 0) and "
2237 "(isn't > 10)) and "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002238 "((isn't equal to 3) and "
2239 "((isn't equal to 5) and "
jgm79a367e2012-04-10 16:02:11 +00002240 "(isn't equal to 7)))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002241 DescribeNegation(m));
2242}
2243
zhanyong.wan18490652009-05-11 18:54:08 +00002244// Tests that monomorphic matchers are safely cast by the AnyOf matcher.
2245TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
2246 // greater_than_5 and less_than_10 are monomorphic matchers.
2247 Matcher<int> greater_than_5 = Gt(5);
2248 Matcher<int> less_than_10 = Lt(10);
2249
2250 Matcher<const int&> m = AnyOf(greater_than_5, less_than_10);
2251 Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10);
2252 Matcher<int&> m3 = AnyOf(greater_than_5, m2);
2253
2254 // Tests that EitherOf works when composing itself.
2255 Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10);
2256 Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);
2257}
2258
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002259TEST(AnyOfTest, ExplainsResult) {
2260 Matcher<int> m;
2261
2262 // Failed match. Both matchers need to explain. The second
2263 // matcher doesn't give an explanation, so only the first matcher's
2264 // explanation is printed.
2265 m = AnyOf(GreaterThan(10), Lt(0));
2266 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
2267
2268 // Failed match. Both matchers need to explain.
2269 m = AnyOf(GreaterThan(10), GreaterThan(20));
2270 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20",
2271 Explain(m, 5));
2272
2273 // Failed match. All matchers need to explain. The second
2274 // matcher doesn't given an explanation.
2275 m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30));
2276 EXPECT_EQ("which is 5 less than 10, and which is 25 less than 30",
2277 Explain(m, 5));
2278
2279 // Failed match. All matchers need to explain.
2280 m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2281 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20, "
2282 "and which is 25 less than 30",
2283 Explain(m, 5));
2284
2285 // Successful match. The first matcher, which succeeded, needs to
2286 // explain.
2287 m = AnyOf(GreaterThan(10), GreaterThan(20));
2288 EXPECT_EQ("which is 5 more than 10", Explain(m, 15));
2289
2290 // Successful match. The second matcher, which succeeded, needs to
2291 // explain. Since it doesn't given an explanation, nothing is
2292 // printed.
2293 m = AnyOf(GreaterThan(10), Lt(30));
2294 EXPECT_EQ("", Explain(m, 0));
2295
2296 // Successful match. The second matcher, which succeeded, needs to
2297 // explain.
2298 m = AnyOf(GreaterThan(30), GreaterThan(20));
2299 EXPECT_EQ("which is 5 more than 20", Explain(m, 25));
2300}
2301
shiqiane35fdd92008-12-10 05:08:54 +00002302// The following predicate function and predicate functor are for
2303// testing the Truly(predicate) matcher.
2304
2305// Returns non-zero if the input is positive. Note that the return
2306// type of this function is not bool. It's OK as Truly() accepts any
2307// unary function or functor whose return type can be implicitly
2308// converted to bool.
2309int IsPositive(double x) {
2310 return x > 0 ? 1 : 0;
2311}
2312
2313// This functor returns true if the input is greater than the given
2314// number.
2315class IsGreaterThan {
2316 public:
2317 explicit IsGreaterThan(int threshold) : threshold_(threshold) {}
2318
2319 bool operator()(int n) const { return n > threshold_; }
zhanyong.wan32de5f52009-12-23 00:13:23 +00002320
shiqiane35fdd92008-12-10 05:08:54 +00002321 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002322 int threshold_;
shiqiane35fdd92008-12-10 05:08:54 +00002323};
2324
2325// For testing Truly().
2326const int foo = 0;
2327
2328// This predicate returns true iff the argument references foo and has
2329// a zero value.
2330bool ReferencesFooAndIsZero(const int& n) {
2331 return (&n == &foo) && (n == 0);
2332}
2333
2334// Tests that Truly(predicate) matches what satisfies the given
2335// predicate.
2336TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {
2337 Matcher<double> m = Truly(IsPositive);
2338 EXPECT_TRUE(m.Matches(2.0));
2339 EXPECT_FALSE(m.Matches(-1.5));
2340}
2341
2342// Tests that Truly(predicate_functor) works too.
2343TEST(TrulyTest, CanBeUsedWithFunctor) {
2344 Matcher<int> m = Truly(IsGreaterThan(5));
2345 EXPECT_TRUE(m.Matches(6));
2346 EXPECT_FALSE(m.Matches(4));
2347}
2348
zhanyong.wan8d3dc0c2011-04-14 19:37:06 +00002349// A class that can be implicitly converted to bool.
2350class ConvertibleToBool {
2351 public:
2352 explicit ConvertibleToBool(int number) : number_(number) {}
2353 operator bool() const { return number_ != 0; }
2354
2355 private:
2356 int number_;
2357};
2358
2359ConvertibleToBool IsNotZero(int number) {
2360 return ConvertibleToBool(number);
2361}
2362
2363// Tests that the predicate used in Truly() may return a class that's
2364// implicitly convertible to bool, even when the class has no
2365// operator!().
2366TEST(TrulyTest, PredicateCanReturnAClassConvertibleToBool) {
2367 Matcher<int> m = Truly(IsNotZero);
2368 EXPECT_TRUE(m.Matches(1));
2369 EXPECT_FALSE(m.Matches(0));
2370}
2371
shiqiane35fdd92008-12-10 05:08:54 +00002372// Tests that Truly(predicate) can describe itself properly.
2373TEST(TrulyTest, CanDescribeSelf) {
2374 Matcher<double> m = Truly(IsPositive);
2375 EXPECT_EQ("satisfies the given predicate",
2376 Describe(m));
2377}
2378
2379// Tests that Truly(predicate) works when the matcher takes its
2380// argument by reference.
2381TEST(TrulyTest, WorksForByRefArguments) {
2382 Matcher<const int&> m = Truly(ReferencesFooAndIsZero);
2383 EXPECT_TRUE(m.Matches(foo));
2384 int n = 0;
2385 EXPECT_FALSE(m.Matches(n));
2386}
2387
2388// Tests that Matches(m) is a predicate satisfied by whatever that
2389// matches matcher m.
2390TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {
2391 EXPECT_TRUE(Matches(Ge(0))(1));
2392 EXPECT_FALSE(Matches(Eq('a'))('b'));
2393}
2394
2395// Tests that Matches(m) works when the matcher takes its argument by
2396// reference.
2397TEST(MatchesTest, WorksOnByRefArguments) {
2398 int m = 0, n = 0;
2399 EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n));
2400 EXPECT_FALSE(Matches(Ref(m))(n));
2401}
2402
2403// Tests that a Matcher on non-reference type can be used in
2404// Matches().
2405TEST(MatchesTest, WorksWithMatcherOnNonRefType) {
2406 Matcher<int> eq5 = Eq(5);
2407 EXPECT_TRUE(Matches(eq5)(5));
2408 EXPECT_FALSE(Matches(eq5)(2));
2409}
2410
zhanyong.wanb8243162009-06-04 05:48:20 +00002411// Tests Value(value, matcher). Since Value() is a simple wrapper for
2412// Matches(), which has been tested already, we don't spend a lot of
2413// effort on testing Value().
2414TEST(ValueTest, WorksWithPolymorphicMatcher) {
2415 EXPECT_TRUE(Value("hi", StartsWith("h")));
2416 EXPECT_FALSE(Value(5, Gt(10)));
2417}
2418
2419TEST(ValueTest, WorksWithMonomorphicMatcher) {
2420 const Matcher<int> is_zero = Eq(0);
2421 EXPECT_TRUE(Value(0, is_zero));
2422 EXPECT_FALSE(Value('a', is_zero));
2423
2424 int n = 0;
2425 const Matcher<const int&> ref_n = Ref(n);
2426 EXPECT_TRUE(Value(n, ref_n));
2427 EXPECT_FALSE(Value(1, ref_n));
2428}
2429
zhanyong.wana862f1d2010-03-15 21:23:04 +00002430TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) {
zhanyong.wan34b034c2010-03-05 21:23:23 +00002431 StringMatchResultListener listener1;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002432 EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002433 EXPECT_EQ("% 2 == 0", listener1.str());
2434
2435 StringMatchResultListener listener2;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002436 EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002437 EXPECT_EQ("", listener2.str());
2438}
2439
zhanyong.wana862f1d2010-03-15 21:23:04 +00002440TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) {
zhanyong.wan34b034c2010-03-05 21:23:23 +00002441 const Matcher<int> is_even = PolymorphicIsEven();
2442 StringMatchResultListener listener1;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002443 EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002444 EXPECT_EQ("% 2 == 0", listener1.str());
2445
2446 const Matcher<const double&> is_zero = Eq(0);
2447 StringMatchResultListener listener2;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002448 EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002449 EXPECT_EQ("", listener2.str());
2450}
2451
zhanyong.wana862f1d2010-03-15 21:23:04 +00002452MATCHER_P(Really, inner_matcher, "") {
2453 return ExplainMatchResult(inner_matcher, arg, result_listener);
2454}
2455
2456TEST(ExplainMatchResultTest, WorksInsideMATCHER) {
2457 EXPECT_THAT(0, Really(Eq(0)));
2458}
2459
zhanyong.wanbf550852009-06-09 06:09:53 +00002460TEST(AllArgsTest, WorksForTuple) {
2461 EXPECT_THAT(make_tuple(1, 2L), AllArgs(Lt()));
2462 EXPECT_THAT(make_tuple(2L, 1), Not(AllArgs(Lt())));
2463}
2464
2465TEST(AllArgsTest, WorksForNonTuple) {
2466 EXPECT_THAT(42, AllArgs(Gt(0)));
2467 EXPECT_THAT('a', Not(AllArgs(Eq('b'))));
2468}
2469
2470class AllArgsHelper {
2471 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002472 AllArgsHelper() {}
2473
zhanyong.wanbf550852009-06-09 06:09:53 +00002474 MOCK_METHOD2(Helper, int(char x, int y));
zhanyong.wan32de5f52009-12-23 00:13:23 +00002475
2476 private:
2477 GTEST_DISALLOW_COPY_AND_ASSIGN_(AllArgsHelper);
zhanyong.wanbf550852009-06-09 06:09:53 +00002478};
2479
2480TEST(AllArgsTest, WorksInWithClause) {
2481 AllArgsHelper helper;
2482 ON_CALL(helper, Helper(_, _))
2483 .With(AllArgs(Lt()))
2484 .WillByDefault(Return(1));
2485 EXPECT_CALL(helper, Helper(_, _));
2486 EXPECT_CALL(helper, Helper(_, _))
2487 .With(AllArgs(Gt()))
2488 .WillOnce(Return(2));
2489
2490 EXPECT_EQ(1, helper.Helper('\1', 2));
2491 EXPECT_EQ(2, helper.Helper('a', 1));
2492}
2493
shiqiane35fdd92008-12-10 05:08:54 +00002494// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2495// matches the matcher.
2496TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {
2497 ASSERT_THAT(5, Ge(2)) << "This should succeed.";
2498 ASSERT_THAT("Foo", EndsWith("oo"));
2499 EXPECT_THAT(2, AllOf(Le(7), Ge(0))) << "This should succeed too.";
2500 EXPECT_THAT("Hello", StartsWith("Hell"));
2501}
2502
2503// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2504// doesn't match the matcher.
2505TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {
2506 // 'n' must be static as it is used in an EXPECT_FATAL_FAILURE(),
2507 // which cannot reference auto variables.
zhanyong.wan736baa82010-09-27 17:44:16 +00002508 static unsigned short n; // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00002509 n = 5;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002510
2511 // VC++ prior to version 8.0 SP1 has a bug where it will not see any
2512 // functions declared in the namespace scope from within nested classes.
2513 // EXPECT/ASSERT_(NON)FATAL_FAILURE macros use nested classes so that all
2514 // namespace-level functions invoked inside them need to be explicitly
2515 // resolved.
2516 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Gt(10)),
shiqiane35fdd92008-12-10 05:08:54 +00002517 "Value of: n\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002518 "Expected: is > 10\n"
zhanyong.wan736baa82010-09-27 17:44:16 +00002519 " Actual: 5" + OfType("unsigned short"));
shiqiane35fdd92008-12-10 05:08:54 +00002520 n = 0;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002521 EXPECT_NONFATAL_FAILURE(
2522 EXPECT_THAT(n, ::testing::AllOf(::testing::Le(7), ::testing::Ge(5))),
2523 "Value of: n\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002524 "Expected: (is <= 7) and (is >= 5)\n"
zhanyong.wan736baa82010-09-27 17:44:16 +00002525 " Actual: 0" + OfType("unsigned short"));
shiqiane35fdd92008-12-10 05:08:54 +00002526}
2527
2528// Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument
2529// has a reference type.
2530TEST(MatcherAssertionTest, WorksForByRefArguments) {
2531 // We use a static variable here as EXPECT_FATAL_FAILURE() cannot
2532 // reference auto variables.
2533 static int n;
2534 n = 0;
2535 EXPECT_THAT(n, AllOf(Le(7), Ref(n)));
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002536 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
shiqiane35fdd92008-12-10 05:08:54 +00002537 "Value of: n\n"
2538 "Expected: does not reference the variable @");
2539 // Tests the "Actual" part.
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002540 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
zhanyong.wan736baa82010-09-27 17:44:16 +00002541 "Actual: 0" + OfType("int") + ", which is located @");
shiqiane35fdd92008-12-10 05:08:54 +00002542}
2543
zhanyong.wan95b12332009-09-25 18:55:50 +00002544#if !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002545// Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is
2546// monomorphic.
zhanyong.wan95b12332009-09-25 18:55:50 +00002547
2548// ASSERT_THAT("hello", starts_with_he) fails to compile with Nokia's
2549// Symbian compiler: it tries to compile
2550// template<T, U> class MatcherCastImpl { ...
zhanyong.wandb22c222010-01-28 21:52:29 +00002551// virtual bool MatchAndExplain(T x, ...) const {
2552// return source_matcher_.MatchAndExplain(static_cast<U>(x), ...);
zhanyong.wan95b12332009-09-25 18:55:50 +00002553// with U == string and T == const char*
2554// With ASSERT_THAT("hello"...) changed to ASSERT_THAT(string("hello") ... )
2555// the compiler silently crashes with no output.
2556// If MatcherCastImpl is changed to use U(x) instead of static_cast<U>(x)
2557// the code compiles but the converted string is bogus.
shiqiane35fdd92008-12-10 05:08:54 +00002558TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
2559 Matcher<const char*> starts_with_he = StartsWith("he");
2560 ASSERT_THAT("hello", starts_with_he);
2561
2562 Matcher<const string&> ends_with_ok = EndsWith("ok");
2563 ASSERT_THAT("book", ends_with_ok);
zhanyong.wan736baa82010-09-27 17:44:16 +00002564 const string bad = "bad";
2565 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(bad, ends_with_ok),
2566 "Value of: bad\n"
2567 "Expected: ends with \"ok\"\n"
2568 " Actual: \"bad\"");
shiqiane35fdd92008-12-10 05:08:54 +00002569 Matcher<int> is_greater_than_5 = Gt(5);
2570 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5),
2571 "Value of: 5\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002572 "Expected: is > 5\n"
zhanyong.wan736baa82010-09-27 17:44:16 +00002573 " Actual: 5" + OfType("int"));
shiqiane35fdd92008-12-10 05:08:54 +00002574}
zhanyong.wan95b12332009-09-25 18:55:50 +00002575#endif // !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002576
2577// Tests floating-point matchers.
2578template <typename RawType>
2579class FloatingPointTest : public testing::Test {
2580 protected:
2581 typedef typename testing::internal::FloatingPoint<RawType> Floating;
2582 typedef typename Floating::Bits Bits;
2583
2584 virtual void SetUp() {
2585 const size_t max_ulps = Floating::kMaxUlps;
2586
2587 // The bits that represent 0.0.
2588 const Bits zero_bits = Floating(0).bits();
2589
2590 // Makes some numbers close to 0.0.
2591 close_to_positive_zero_ = Floating::ReinterpretBits(zero_bits + max_ulps/2);
2592 close_to_negative_zero_ = -Floating::ReinterpretBits(
2593 zero_bits + max_ulps - max_ulps/2);
2594 further_from_negative_zero_ = -Floating::ReinterpretBits(
2595 zero_bits + max_ulps + 1 - max_ulps/2);
2596
2597 // The bits that represent 1.0.
2598 const Bits one_bits = Floating(1).bits();
2599
2600 // Makes some numbers close to 1.0.
2601 close_to_one_ = Floating::ReinterpretBits(one_bits + max_ulps);
2602 further_from_one_ = Floating::ReinterpretBits(one_bits + max_ulps + 1);
2603
2604 // +infinity.
2605 infinity_ = Floating::Infinity();
2606
2607 // The bits that represent +infinity.
2608 const Bits infinity_bits = Floating(infinity_).bits();
2609
2610 // Makes some numbers close to infinity.
2611 close_to_infinity_ = Floating::ReinterpretBits(infinity_bits - max_ulps);
2612 further_from_infinity_ = Floating::ReinterpretBits(
2613 infinity_bits - max_ulps - 1);
2614
2615 // Makes some NAN's.
2616 nan1_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 1);
2617 nan2_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 200);
2618 }
2619
2620 void TestSize() {
2621 EXPECT_EQ(sizeof(RawType), sizeof(Bits));
2622 }
2623
2624 // A battery of tests for FloatingEqMatcher::Matches.
2625 // matcher_maker is a pointer to a function which creates a FloatingEqMatcher.
2626 void TestMatches(
2627 testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) {
2628 Matcher<RawType> m1 = matcher_maker(0.0);
2629 EXPECT_TRUE(m1.Matches(-0.0));
2630 EXPECT_TRUE(m1.Matches(close_to_positive_zero_));
2631 EXPECT_TRUE(m1.Matches(close_to_negative_zero_));
2632 EXPECT_FALSE(m1.Matches(1.0));
2633
2634 Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_);
2635 EXPECT_FALSE(m2.Matches(further_from_negative_zero_));
2636
2637 Matcher<RawType> m3 = matcher_maker(1.0);
2638 EXPECT_TRUE(m3.Matches(close_to_one_));
2639 EXPECT_FALSE(m3.Matches(further_from_one_));
2640
2641 // Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above.
2642 EXPECT_FALSE(m3.Matches(0.0));
2643
2644 Matcher<RawType> m4 = matcher_maker(-infinity_);
2645 EXPECT_TRUE(m4.Matches(-close_to_infinity_));
2646
2647 Matcher<RawType> m5 = matcher_maker(infinity_);
2648 EXPECT_TRUE(m5.Matches(close_to_infinity_));
2649
2650 // This is interesting as the representations of infinity_ and nan1_
2651 // are only 1 DLP apart.
2652 EXPECT_FALSE(m5.Matches(nan1_));
2653
2654 // matcher_maker can produce a Matcher<const RawType&>, which is needed in
2655 // some cases.
2656 Matcher<const RawType&> m6 = matcher_maker(0.0);
2657 EXPECT_TRUE(m6.Matches(-0.0));
2658 EXPECT_TRUE(m6.Matches(close_to_positive_zero_));
2659 EXPECT_FALSE(m6.Matches(1.0));
2660
2661 // matcher_maker can produce a Matcher<RawType&>, which is needed in some
2662 // cases.
2663 Matcher<RawType&> m7 = matcher_maker(0.0);
2664 RawType x = 0.0;
2665 EXPECT_TRUE(m7.Matches(x));
2666 x = 0.01f;
2667 EXPECT_FALSE(m7.Matches(x));
2668 }
2669
2670 // Pre-calculated numbers to be used by the tests.
2671
2672 static RawType close_to_positive_zero_;
2673 static RawType close_to_negative_zero_;
2674 static RawType further_from_negative_zero_;
2675
2676 static RawType close_to_one_;
2677 static RawType further_from_one_;
2678
2679 static RawType infinity_;
2680 static RawType close_to_infinity_;
2681 static RawType further_from_infinity_;
2682
2683 static RawType nan1_;
2684 static RawType nan2_;
2685};
2686
2687template <typename RawType>
2688RawType FloatingPointTest<RawType>::close_to_positive_zero_;
2689
2690template <typename RawType>
2691RawType FloatingPointTest<RawType>::close_to_negative_zero_;
2692
2693template <typename RawType>
2694RawType FloatingPointTest<RawType>::further_from_negative_zero_;
2695
2696template <typename RawType>
2697RawType FloatingPointTest<RawType>::close_to_one_;
2698
2699template <typename RawType>
2700RawType FloatingPointTest<RawType>::further_from_one_;
2701
2702template <typename RawType>
2703RawType FloatingPointTest<RawType>::infinity_;
2704
2705template <typename RawType>
2706RawType FloatingPointTest<RawType>::close_to_infinity_;
2707
2708template <typename RawType>
2709RawType FloatingPointTest<RawType>::further_from_infinity_;
2710
2711template <typename RawType>
2712RawType FloatingPointTest<RawType>::nan1_;
2713
2714template <typename RawType>
2715RawType FloatingPointTest<RawType>::nan2_;
2716
2717// Instantiate FloatingPointTest for testing floats.
2718typedef FloatingPointTest<float> FloatTest;
2719
2720TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) {
2721 TestMatches(&FloatEq);
2722}
2723
2724TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
2725 TestMatches(&NanSensitiveFloatEq);
2726}
2727
2728TEST_F(FloatTest, FloatEqCannotMatchNaN) {
2729 // FloatEq never matches NaN.
2730 Matcher<float> m = FloatEq(nan1_);
2731 EXPECT_FALSE(m.Matches(nan1_));
2732 EXPECT_FALSE(m.Matches(nan2_));
2733 EXPECT_FALSE(m.Matches(1.0));
2734}
2735
2736TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {
2737 // NanSensitiveFloatEq will match NaN.
2738 Matcher<float> m = NanSensitiveFloatEq(nan1_);
2739 EXPECT_TRUE(m.Matches(nan1_));
2740 EXPECT_TRUE(m.Matches(nan2_));
2741 EXPECT_FALSE(m.Matches(1.0));
2742}
2743
2744TEST_F(FloatTest, FloatEqCanDescribeSelf) {
2745 Matcher<float> m1 = FloatEq(2.0f);
2746 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002747 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002748
2749 Matcher<float> m2 = FloatEq(0.5f);
2750 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002751 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002752
2753 Matcher<float> m3 = FloatEq(nan1_);
2754 EXPECT_EQ("never matches", Describe(m3));
2755 EXPECT_EQ("is anything", DescribeNegation(m3));
2756}
2757
2758TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {
2759 Matcher<float> m1 = NanSensitiveFloatEq(2.0f);
2760 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002761 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002762
2763 Matcher<float> m2 = NanSensitiveFloatEq(0.5f);
2764 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002765 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002766
2767 Matcher<float> m3 = NanSensitiveFloatEq(nan1_);
2768 EXPECT_EQ("is NaN", Describe(m3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002769 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
shiqiane35fdd92008-12-10 05:08:54 +00002770}
2771
2772// Instantiate FloatingPointTest for testing doubles.
2773typedef FloatingPointTest<double> DoubleTest;
2774
2775TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
2776 TestMatches(&DoubleEq);
2777}
2778
2779TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
2780 TestMatches(&NanSensitiveDoubleEq);
2781}
2782
2783TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {
2784 // DoubleEq never matches NaN.
2785 Matcher<double> m = DoubleEq(nan1_);
2786 EXPECT_FALSE(m.Matches(nan1_));
2787 EXPECT_FALSE(m.Matches(nan2_));
2788 EXPECT_FALSE(m.Matches(1.0));
2789}
2790
2791TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {
2792 // NanSensitiveDoubleEq will match NaN.
2793 Matcher<double> m = NanSensitiveDoubleEq(nan1_);
2794 EXPECT_TRUE(m.Matches(nan1_));
2795 EXPECT_TRUE(m.Matches(nan2_));
2796 EXPECT_FALSE(m.Matches(1.0));
2797}
2798
2799TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {
2800 Matcher<double> m1 = DoubleEq(2.0);
2801 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002802 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002803
2804 Matcher<double> m2 = DoubleEq(0.5);
2805 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002806 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002807
2808 Matcher<double> m3 = DoubleEq(nan1_);
2809 EXPECT_EQ("never matches", Describe(m3));
2810 EXPECT_EQ("is anything", DescribeNegation(m3));
2811}
2812
2813TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {
2814 Matcher<double> m1 = NanSensitiveDoubleEq(2.0);
2815 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002816 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002817
2818 Matcher<double> m2 = NanSensitiveDoubleEq(0.5);
2819 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002820 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002821
2822 Matcher<double> m3 = NanSensitiveDoubleEq(nan1_);
2823 EXPECT_EQ("is NaN", Describe(m3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002824 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
shiqiane35fdd92008-12-10 05:08:54 +00002825}
2826
2827TEST(PointeeTest, RawPointer) {
2828 const Matcher<int*> m = Pointee(Ge(0));
2829
2830 int n = 1;
2831 EXPECT_TRUE(m.Matches(&n));
2832 n = -1;
2833 EXPECT_FALSE(m.Matches(&n));
2834 EXPECT_FALSE(m.Matches(NULL));
2835}
2836
2837TEST(PointeeTest, RawPointerToConst) {
2838 const Matcher<const double*> m = Pointee(Ge(0));
2839
2840 double x = 1;
2841 EXPECT_TRUE(m.Matches(&x));
2842 x = -1;
2843 EXPECT_FALSE(m.Matches(&x));
2844 EXPECT_FALSE(m.Matches(NULL));
2845}
2846
2847TEST(PointeeTest, ReferenceToConstRawPointer) {
2848 const Matcher<int* const &> m = Pointee(Ge(0));
2849
2850 int n = 1;
2851 EXPECT_TRUE(m.Matches(&n));
2852 n = -1;
2853 EXPECT_FALSE(m.Matches(&n));
2854 EXPECT_FALSE(m.Matches(NULL));
2855}
2856
2857TEST(PointeeTest, ReferenceToNonConstRawPointer) {
2858 const Matcher<double* &> m = Pointee(Ge(0));
2859
2860 double x = 1.0;
2861 double* p = &x;
2862 EXPECT_TRUE(m.Matches(p));
2863 x = -1;
2864 EXPECT_FALSE(m.Matches(p));
2865 p = NULL;
2866 EXPECT_FALSE(m.Matches(p));
2867}
2868
vladlosevada23472012-08-14 15:38:49 +00002869// Minimal const-propagating pointer.
2870template <typename T>
2871class ConstPropagatingPtr {
2872 public:
2873 typedef T element_type;
2874
2875 ConstPropagatingPtr() : val_() {}
2876 explicit ConstPropagatingPtr(T* t) : val_(t) {}
2877 ConstPropagatingPtr(const ConstPropagatingPtr& other) : val_(other.val_) {}
2878
2879 T* get() { return val_; }
2880 T& operator*() { return *val_; }
2881 // Most smart pointers return non-const T* and T& from the next methods.
2882 const T* get() const { return val_; }
2883 const T& operator*() const { return *val_; }
2884
2885 private:
2886 T* val_;
2887};
2888
2889TEST(PointeeTest, WorksWithConstPropagatingPointers) {
2890 const Matcher< ConstPropagatingPtr<int> > m = Pointee(Lt(5));
2891 int three = 3;
2892 const ConstPropagatingPtr<int> co(&three);
2893 ConstPropagatingPtr<int> o(&three);
2894 EXPECT_TRUE(m.Matches(o));
2895 EXPECT_TRUE(m.Matches(co));
2896 *o = 6;
2897 EXPECT_FALSE(m.Matches(o));
2898 EXPECT_FALSE(m.Matches(ConstPropagatingPtr<int>()));
2899}
2900
shiqiane35fdd92008-12-10 05:08:54 +00002901TEST(PointeeTest, NeverMatchesNull) {
2902 const Matcher<const char*> m = Pointee(_);
2903 EXPECT_FALSE(m.Matches(NULL));
2904}
2905
2906// Tests that we can write Pointee(value) instead of Pointee(Eq(value)).
2907TEST(PointeeTest, MatchesAgainstAValue) {
2908 const Matcher<int*> m = Pointee(5);
2909
2910 int n = 5;
2911 EXPECT_TRUE(m.Matches(&n));
2912 n = -1;
2913 EXPECT_FALSE(m.Matches(&n));
2914 EXPECT_FALSE(m.Matches(NULL));
2915}
2916
2917TEST(PointeeTest, CanDescribeSelf) {
2918 const Matcher<int*> m = Pointee(Gt(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002919 EXPECT_EQ("points to a value that is > 3", Describe(m));
2920 EXPECT_EQ("does not point to a value that is > 3",
shiqiane35fdd92008-12-10 05:08:54 +00002921 DescribeNegation(m));
2922}
2923
shiqiane35fdd92008-12-10 05:08:54 +00002924TEST(PointeeTest, CanExplainMatchResult) {
2925 const Matcher<const string*> m = Pointee(StartsWith("Hi"));
2926
2927 EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL)));
2928
zhanyong.wan736baa82010-09-27 17:44:16 +00002929 const Matcher<long*> m2 = Pointee(GreaterThan(1)); // NOLINT
2930 long n = 3; // NOLINT
2931 EXPECT_EQ("which points to 3" + OfType("long") + ", which is 2 more than 1",
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002932 Explain(m2, &n));
2933}
2934
2935TEST(PointeeTest, AlwaysExplainsPointee) {
2936 const Matcher<int*> m = Pointee(0);
2937 int n = 42;
zhanyong.wan736baa82010-09-27 17:44:16 +00002938 EXPECT_EQ("which points to 42" + OfType("int"), Explain(m, &n));
shiqiane35fdd92008-12-10 05:08:54 +00002939}
2940
2941// An uncopyable class.
2942class Uncopyable {
2943 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002944 explicit Uncopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00002945
2946 int value() const { return value_; }
2947 private:
2948 const int value_;
2949 GTEST_DISALLOW_COPY_AND_ASSIGN_(Uncopyable);
2950};
2951
2952// Returns true iff x.value() is positive.
2953bool ValueIsPositive(const Uncopyable& x) { return x.value() > 0; }
2954
2955// A user-defined struct for testing Field().
2956struct AStruct {
2957 AStruct() : x(0), y(1.0), z(5), p(NULL) {}
2958 AStruct(const AStruct& rhs)
2959 : x(rhs.x), y(rhs.y), z(rhs.z.value()), p(rhs.p) {}
2960
2961 int x; // A non-const field.
2962 const double y; // A const field.
2963 Uncopyable z; // An uncopyable field.
2964 const char* p; // A pointer field.
zhanyong.wan32de5f52009-12-23 00:13:23 +00002965
2966 private:
2967 GTEST_DISALLOW_ASSIGN_(AStruct);
shiqiane35fdd92008-12-10 05:08:54 +00002968};
2969
2970// A derived struct for testing Field().
2971struct DerivedStruct : public AStruct {
2972 char ch;
zhanyong.wan32de5f52009-12-23 00:13:23 +00002973
2974 private:
2975 GTEST_DISALLOW_ASSIGN_(DerivedStruct);
shiqiane35fdd92008-12-10 05:08:54 +00002976};
2977
2978// Tests that Field(&Foo::field, ...) works when field is non-const.
2979TEST(FieldTest, WorksForNonConstField) {
2980 Matcher<AStruct> m = Field(&AStruct::x, Ge(0));
2981
2982 AStruct a;
2983 EXPECT_TRUE(m.Matches(a));
2984 a.x = -1;
2985 EXPECT_FALSE(m.Matches(a));
2986}
2987
2988// Tests that Field(&Foo::field, ...) works when field is const.
2989TEST(FieldTest, WorksForConstField) {
2990 AStruct a;
2991
2992 Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0));
2993 EXPECT_TRUE(m.Matches(a));
2994 m = Field(&AStruct::y, Le(0.0));
2995 EXPECT_FALSE(m.Matches(a));
2996}
2997
2998// Tests that Field(&Foo::field, ...) works when field is not copyable.
2999TEST(FieldTest, WorksForUncopyableField) {
3000 AStruct a;
3001
3002 Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive));
3003 EXPECT_TRUE(m.Matches(a));
3004 m = Field(&AStruct::z, Not(Truly(ValueIsPositive)));
3005 EXPECT_FALSE(m.Matches(a));
3006}
3007
3008// Tests that Field(&Foo::field, ...) works when field is a pointer.
3009TEST(FieldTest, WorksForPointerField) {
3010 // Matching against NULL.
3011 Matcher<AStruct> m = Field(&AStruct::p, static_cast<const char*>(NULL));
3012 AStruct a;
3013 EXPECT_TRUE(m.Matches(a));
3014 a.p = "hi";
3015 EXPECT_FALSE(m.Matches(a));
3016
3017 // Matching a pointer that is not NULL.
3018 m = Field(&AStruct::p, StartsWith("hi"));
3019 a.p = "hill";
3020 EXPECT_TRUE(m.Matches(a));
3021 a.p = "hole";
3022 EXPECT_FALSE(m.Matches(a));
3023}
3024
3025// Tests that Field() works when the object is passed by reference.
3026TEST(FieldTest, WorksForByRefArgument) {
3027 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
3028
3029 AStruct a;
3030 EXPECT_TRUE(m.Matches(a));
3031 a.x = -1;
3032 EXPECT_FALSE(m.Matches(a));
3033}
3034
3035// Tests that Field(&Foo::field, ...) works when the argument's type
3036// is a sub-type of Foo.
3037TEST(FieldTest, WorksForArgumentOfSubType) {
3038 // Note that the matcher expects DerivedStruct but we say AStruct
3039 // inside Field().
3040 Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0));
3041
3042 DerivedStruct d;
3043 EXPECT_TRUE(m.Matches(d));
3044 d.x = -1;
3045 EXPECT_FALSE(m.Matches(d));
3046}
3047
3048// Tests that Field(&Foo::field, m) works when field's type and m's
3049// argument type are compatible but not the same.
3050TEST(FieldTest, WorksForCompatibleMatcherType) {
3051 // The field is an int, but the inner matcher expects a signed char.
3052 Matcher<const AStruct&> m = Field(&AStruct::x,
3053 Matcher<signed char>(Ge(0)));
3054
3055 AStruct a;
3056 EXPECT_TRUE(m.Matches(a));
3057 a.x = -1;
3058 EXPECT_FALSE(m.Matches(a));
3059}
3060
3061// Tests that Field() can describe itself.
3062TEST(FieldTest, CanDescribeSelf) {
3063 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
3064
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003065 EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
3066 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003067}
3068
3069// Tests that Field() can explain the match result.
3070TEST(FieldTest, CanExplainMatchResult) {
3071 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
3072
3073 AStruct a;
3074 a.x = 1;
zhanyong.wan736baa82010-09-27 17:44:16 +00003075 EXPECT_EQ("whose given field is 1" + OfType("int"), Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003076
3077 m = Field(&AStruct::x, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003078 EXPECT_EQ(
3079 "whose given field is 1" + OfType("int") + ", which is 1 more than 0",
3080 Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003081}
3082
3083// Tests that Field() works when the argument is a pointer to const.
3084TEST(FieldForPointerTest, WorksForPointerToConst) {
3085 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
3086
3087 AStruct a;
3088 EXPECT_TRUE(m.Matches(&a));
3089 a.x = -1;
3090 EXPECT_FALSE(m.Matches(&a));
3091}
3092
3093// Tests that Field() works when the argument is a pointer to non-const.
3094TEST(FieldForPointerTest, WorksForPointerToNonConst) {
3095 Matcher<AStruct*> m = Field(&AStruct::x, Ge(0));
3096
3097 AStruct a;
3098 EXPECT_TRUE(m.Matches(&a));
3099 a.x = -1;
3100 EXPECT_FALSE(m.Matches(&a));
3101}
3102
zhanyong.wan6953a722010-01-13 05:15:07 +00003103// Tests that Field() works when the argument is a reference to a const pointer.
3104TEST(FieldForPointerTest, WorksForReferenceToConstPointer) {
3105 Matcher<AStruct* const&> m = Field(&AStruct::x, Ge(0));
3106
3107 AStruct a;
3108 EXPECT_TRUE(m.Matches(&a));
3109 a.x = -1;
3110 EXPECT_FALSE(m.Matches(&a));
3111}
3112
shiqiane35fdd92008-12-10 05:08:54 +00003113// Tests that Field() does not match the NULL pointer.
3114TEST(FieldForPointerTest, DoesNotMatchNull) {
3115 Matcher<const AStruct*> m = Field(&AStruct::x, _);
3116 EXPECT_FALSE(m.Matches(NULL));
3117}
3118
3119// Tests that Field(&Foo::field, ...) works when the argument's type
3120// is a sub-type of const Foo*.
3121TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
3122 // Note that the matcher expects DerivedStruct but we say AStruct
3123 // inside Field().
3124 Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0));
3125
3126 DerivedStruct d;
3127 EXPECT_TRUE(m.Matches(&d));
3128 d.x = -1;
3129 EXPECT_FALSE(m.Matches(&d));
3130}
3131
3132// Tests that Field() can describe itself when used to match a pointer.
3133TEST(FieldForPointerTest, CanDescribeSelf) {
3134 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
3135
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003136 EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
3137 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003138}
3139
3140// Tests that Field() can explain the result of matching a pointer.
3141TEST(FieldForPointerTest, CanExplainMatchResult) {
3142 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
3143
3144 AStruct a;
3145 a.x = 1;
3146 EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(NULL)));
zhanyong.wan736baa82010-09-27 17:44:16 +00003147 EXPECT_EQ("which points to an object whose given field is 1" + OfType("int"),
3148 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003149
3150 m = Field(&AStruct::x, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003151 EXPECT_EQ("which points to an object whose given field is 1" + OfType("int") +
3152 ", which is 1 more than 0", Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003153}
3154
3155// A user-defined class for testing Property().
3156class AClass {
3157 public:
3158 AClass() : n_(0) {}
3159
3160 // A getter that returns a non-reference.
3161 int n() const { return n_; }
3162
3163 void set_n(int new_n) { n_ = new_n; }
3164
3165 // A getter that returns a reference to const.
3166 const string& s() const { return s_; }
3167
3168 void set_s(const string& new_s) { s_ = new_s; }
3169
3170 // A getter that returns a reference to non-const.
3171 double& x() const { return x_; }
3172 private:
3173 int n_;
3174 string s_;
3175
3176 static double x_;
3177};
3178
3179double AClass::x_ = 0.0;
3180
3181// A derived class for testing Property().
3182class DerivedClass : public AClass {
3183 private:
3184 int k_;
3185};
3186
3187// Tests that Property(&Foo::property, ...) works when property()
3188// returns a non-reference.
3189TEST(PropertyTest, WorksForNonReferenceProperty) {
3190 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3191
3192 AClass a;
3193 a.set_n(1);
3194 EXPECT_TRUE(m.Matches(a));
3195
3196 a.set_n(-1);
3197 EXPECT_FALSE(m.Matches(a));
3198}
3199
3200// Tests that Property(&Foo::property, ...) works when property()
3201// returns a reference to const.
3202TEST(PropertyTest, WorksForReferenceToConstProperty) {
3203 Matcher<const AClass&> m = Property(&AClass::s, StartsWith("hi"));
3204
3205 AClass a;
3206 a.set_s("hill");
3207 EXPECT_TRUE(m.Matches(a));
3208
3209 a.set_s("hole");
3210 EXPECT_FALSE(m.Matches(a));
3211}
3212
3213// Tests that Property(&Foo::property, ...) works when property()
3214// returns a reference to non-const.
3215TEST(PropertyTest, WorksForReferenceToNonConstProperty) {
3216 double x = 0.0;
3217 AClass a;
3218
3219 Matcher<const AClass&> m = Property(&AClass::x, Ref(x));
3220 EXPECT_FALSE(m.Matches(a));
3221
3222 m = Property(&AClass::x, Not(Ref(x)));
3223 EXPECT_TRUE(m.Matches(a));
3224}
3225
3226// Tests that Property(&Foo::property, ...) works when the argument is
3227// passed by value.
3228TEST(PropertyTest, WorksForByValueArgument) {
3229 Matcher<AClass> m = Property(&AClass::s, StartsWith("hi"));
3230
3231 AClass a;
3232 a.set_s("hill");
3233 EXPECT_TRUE(m.Matches(a));
3234
3235 a.set_s("hole");
3236 EXPECT_FALSE(m.Matches(a));
3237}
3238
3239// Tests that Property(&Foo::property, ...) works when the argument's
3240// type is a sub-type of Foo.
3241TEST(PropertyTest, WorksForArgumentOfSubType) {
3242 // The matcher expects a DerivedClass, but inside the Property() we
3243 // say AClass.
3244 Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0));
3245
3246 DerivedClass d;
3247 d.set_n(1);
3248 EXPECT_TRUE(m.Matches(d));
3249
3250 d.set_n(-1);
3251 EXPECT_FALSE(m.Matches(d));
3252}
3253
3254// Tests that Property(&Foo::property, m) works when property()'s type
3255// and m's argument type are compatible but different.
3256TEST(PropertyTest, WorksForCompatibleMatcherType) {
3257 // n() returns an int but the inner matcher expects a signed char.
3258 Matcher<const AClass&> m = Property(&AClass::n,
3259 Matcher<signed char>(Ge(0)));
3260
3261 AClass a;
3262 EXPECT_TRUE(m.Matches(a));
3263 a.set_n(-1);
3264 EXPECT_FALSE(m.Matches(a));
3265}
3266
3267// Tests that Property() can describe itself.
3268TEST(PropertyTest, CanDescribeSelf) {
3269 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3270
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003271 EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
3272 EXPECT_EQ("is an object whose given property isn't >= 0",
3273 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003274}
3275
3276// Tests that Property() can explain the match result.
3277TEST(PropertyTest, CanExplainMatchResult) {
3278 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3279
3280 AClass a;
3281 a.set_n(1);
zhanyong.wan736baa82010-09-27 17:44:16 +00003282 EXPECT_EQ("whose given property is 1" + OfType("int"), Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003283
3284 m = Property(&AClass::n, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003285 EXPECT_EQ(
3286 "whose given property is 1" + OfType("int") + ", which is 1 more than 0",
3287 Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003288}
3289
3290// Tests that Property() works when the argument is a pointer to const.
3291TEST(PropertyForPointerTest, WorksForPointerToConst) {
3292 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3293
3294 AClass a;
3295 a.set_n(1);
3296 EXPECT_TRUE(m.Matches(&a));
3297
3298 a.set_n(-1);
3299 EXPECT_FALSE(m.Matches(&a));
3300}
3301
3302// Tests that Property() works when the argument is a pointer to non-const.
3303TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
3304 Matcher<AClass*> m = Property(&AClass::s, StartsWith("hi"));
3305
3306 AClass a;
3307 a.set_s("hill");
3308 EXPECT_TRUE(m.Matches(&a));
3309
3310 a.set_s("hole");
3311 EXPECT_FALSE(m.Matches(&a));
3312}
3313
zhanyong.wan6953a722010-01-13 05:15:07 +00003314// Tests that Property() works when the argument is a reference to a
3315// const pointer.
3316TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) {
3317 Matcher<AClass* const&> m = Property(&AClass::s, StartsWith("hi"));
3318
3319 AClass a;
3320 a.set_s("hill");
3321 EXPECT_TRUE(m.Matches(&a));
3322
3323 a.set_s("hole");
3324 EXPECT_FALSE(m.Matches(&a));
3325}
3326
shiqiane35fdd92008-12-10 05:08:54 +00003327// Tests that Property() does not match the NULL pointer.
3328TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
3329 Matcher<const AClass*> m = Property(&AClass::x, _);
3330 EXPECT_FALSE(m.Matches(NULL));
3331}
3332
3333// Tests that Property(&Foo::property, ...) works when the argument's
3334// type is a sub-type of const Foo*.
3335TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
3336 // The matcher expects a DerivedClass, but inside the Property() we
3337 // say AClass.
3338 Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0));
3339
3340 DerivedClass d;
3341 d.set_n(1);
3342 EXPECT_TRUE(m.Matches(&d));
3343
3344 d.set_n(-1);
3345 EXPECT_FALSE(m.Matches(&d));
3346}
3347
3348// Tests that Property() can describe itself when used to match a pointer.
3349TEST(PropertyForPointerTest, CanDescribeSelf) {
3350 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3351
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003352 EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
3353 EXPECT_EQ("is an object whose given property isn't >= 0",
3354 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003355}
3356
3357// Tests that Property() can explain the result of matching a pointer.
3358TEST(PropertyForPointerTest, CanExplainMatchResult) {
3359 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3360
3361 AClass a;
3362 a.set_n(1);
3363 EXPECT_EQ("", Explain(m, static_cast<const AClass*>(NULL)));
zhanyong.wan736baa82010-09-27 17:44:16 +00003364 EXPECT_EQ(
3365 "which points to an object whose given property is 1" + OfType("int"),
3366 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003367
3368 m = Property(&AClass::n, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003369 EXPECT_EQ("which points to an object whose given property is 1" +
3370 OfType("int") + ", which is 1 more than 0",
3371 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003372}
3373
3374// Tests ResultOf.
3375
3376// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3377// function pointer.
3378string IntToStringFunction(int input) { return input == 1 ? "foo" : "bar"; }
3379
3380TEST(ResultOfTest, WorksForFunctionPointers) {
3381 Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(string("foo")));
3382
3383 EXPECT_TRUE(matcher.Matches(1));
3384 EXPECT_FALSE(matcher.Matches(2));
3385}
3386
3387// Tests that ResultOf() can describe itself.
3388TEST(ResultOfTest, CanDescribeItself) {
3389 Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo"));
3390
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003391 EXPECT_EQ("is mapped by the given callable to a value that "
3392 "is equal to \"foo\"", Describe(matcher));
3393 EXPECT_EQ("is mapped by the given callable to a value that "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003394 "isn't equal to \"foo\"", DescribeNegation(matcher));
shiqiane35fdd92008-12-10 05:08:54 +00003395}
3396
3397// Tests that ResultOf() can explain the match result.
3398int IntFunction(int input) { return input == 42 ? 80 : 90; }
3399
3400TEST(ResultOfTest, CanExplainMatchResult) {
3401 Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
zhanyong.wan736baa82010-09-27 17:44:16 +00003402 EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int"),
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003403 Explain(matcher, 36));
shiqiane35fdd92008-12-10 05:08:54 +00003404
3405 matcher = ResultOf(&IntFunction, GreaterThan(85));
zhanyong.wan736baa82010-09-27 17:44:16 +00003406 EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int") +
3407 ", which is 5 more than 85", Explain(matcher, 36));
shiqiane35fdd92008-12-10 05:08:54 +00003408}
3409
3410// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3411// returns a non-reference.
3412TEST(ResultOfTest, WorksForNonReferenceResults) {
3413 Matcher<int> matcher = ResultOf(&IntFunction, Eq(80));
3414
3415 EXPECT_TRUE(matcher.Matches(42));
3416 EXPECT_FALSE(matcher.Matches(36));
3417}
3418
3419// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3420// returns a reference to non-const.
zhanyong.wan736baa82010-09-27 17:44:16 +00003421double& DoubleFunction(double& input) { return input; } // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00003422
zhanyong.wan736baa82010-09-27 17:44:16 +00003423Uncopyable& RefUncopyableFunction(Uncopyable& obj) { // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00003424 return obj;
3425}
3426
3427TEST(ResultOfTest, WorksForReferenceToNonConstResults) {
3428 double x = 3.14;
3429 double x2 = x;
3430 Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x));
3431
3432 EXPECT_TRUE(matcher.Matches(x));
3433 EXPECT_FALSE(matcher.Matches(x2));
3434
3435 // Test that ResultOf works with uncopyable objects
3436 Uncopyable obj(0);
3437 Uncopyable obj2(0);
3438 Matcher<Uncopyable&> matcher2 =
3439 ResultOf(&RefUncopyableFunction, Ref(obj));
3440
3441 EXPECT_TRUE(matcher2.Matches(obj));
3442 EXPECT_FALSE(matcher2.Matches(obj2));
3443}
3444
3445// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3446// returns a reference to const.
3447const string& StringFunction(const string& input) { return input; }
3448
3449TEST(ResultOfTest, WorksForReferenceToConstResults) {
3450 string s = "foo";
3451 string s2 = s;
3452 Matcher<const string&> matcher = ResultOf(&StringFunction, Ref(s));
3453
3454 EXPECT_TRUE(matcher.Matches(s));
3455 EXPECT_FALSE(matcher.Matches(s2));
3456}
3457
3458// Tests that ResultOf(f, m) works when f(x) and m's
3459// argument types are compatible but different.
3460TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {
3461 // IntFunction() returns int but the inner matcher expects a signed char.
3462 Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85)));
3463
3464 EXPECT_TRUE(matcher.Matches(36));
3465 EXPECT_FALSE(matcher.Matches(42));
3466}
3467
shiqiane35fdd92008-12-10 05:08:54 +00003468// Tests that the program aborts when ResultOf is passed
3469// a NULL function pointer.
3470TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +00003471 EXPECT_DEATH_IF_SUPPORTED(
zhanyong.wan736baa82010-09-27 17:44:16 +00003472 ResultOf(static_cast<string(*)(int dummy)>(NULL), Eq(string("foo"))),
shiqiane35fdd92008-12-10 05:08:54 +00003473 "NULL function pointer is passed into ResultOf\\(\\)\\.");
3474}
shiqiane35fdd92008-12-10 05:08:54 +00003475
3476// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3477// function reference.
3478TEST(ResultOfTest, WorksForFunctionReferences) {
3479 Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq("foo"));
3480 EXPECT_TRUE(matcher.Matches(1));
3481 EXPECT_FALSE(matcher.Matches(2));
3482}
3483
3484// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3485// function object.
3486struct Functor : public ::std::unary_function<int, string> {
3487 result_type operator()(argument_type input) const {
3488 return IntToStringFunction(input);
3489 }
3490};
3491
3492TEST(ResultOfTest, WorksForFunctors) {
3493 Matcher<int> matcher = ResultOf(Functor(), Eq(string("foo")));
3494
3495 EXPECT_TRUE(matcher.Matches(1));
3496 EXPECT_FALSE(matcher.Matches(2));
3497}
3498
3499// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3500// functor with more then one operator() defined. ResultOf() must work
3501// for each defined operator().
3502struct PolymorphicFunctor {
3503 typedef int result_type;
3504 int operator()(int n) { return n; }
3505 int operator()(const char* s) { return static_cast<int>(strlen(s)); }
3506};
3507
3508TEST(ResultOfTest, WorksForPolymorphicFunctors) {
3509 Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5));
3510
3511 EXPECT_TRUE(matcher_int.Matches(10));
3512 EXPECT_FALSE(matcher_int.Matches(2));
3513
3514 Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5));
3515
3516 EXPECT_TRUE(matcher_string.Matches("long string"));
3517 EXPECT_FALSE(matcher_string.Matches("shrt"));
3518}
3519
3520const int* ReferencingFunction(const int& n) { return &n; }
3521
3522struct ReferencingFunctor {
3523 typedef const int* result_type;
3524 result_type operator()(const int& n) { return &n; }
3525};
3526
3527TEST(ResultOfTest, WorksForReferencingCallables) {
3528 const int n = 1;
3529 const int n2 = 1;
3530 Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n));
3531 EXPECT_TRUE(matcher2.Matches(n));
3532 EXPECT_FALSE(matcher2.Matches(n2));
3533
3534 Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n));
3535 EXPECT_TRUE(matcher3.Matches(n));
3536 EXPECT_FALSE(matcher3.Matches(n2));
3537}
3538
shiqiane35fdd92008-12-10 05:08:54 +00003539class DivisibleByImpl {
3540 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003541 explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {}
shiqiane35fdd92008-12-10 05:08:54 +00003542
zhanyong.wandb22c222010-01-28 21:52:29 +00003543 // For testing using ExplainMatchResultTo() with polymorphic matchers.
shiqiane35fdd92008-12-10 05:08:54 +00003544 template <typename T>
zhanyong.wandb22c222010-01-28 21:52:29 +00003545 bool MatchAndExplain(const T& n, MatchResultListener* listener) const {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003546 *listener << "which is " << (n % divider_) << " modulo "
zhanyong.wandb22c222010-01-28 21:52:29 +00003547 << divider_;
shiqiane35fdd92008-12-10 05:08:54 +00003548 return (n % divider_) == 0;
3549 }
3550
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003551 void DescribeTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +00003552 *os << "is divisible by " << divider_;
3553 }
3554
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003555 void DescribeNegationTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +00003556 *os << "is not divisible by " << divider_;
3557 }
3558
zhanyong.wan32de5f52009-12-23 00:13:23 +00003559 void set_divider(int a_divider) { divider_ = a_divider; }
shiqiane35fdd92008-12-10 05:08:54 +00003560 int divider() const { return divider_; }
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003561
shiqiane35fdd92008-12-10 05:08:54 +00003562 private:
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003563 int divider_;
shiqiane35fdd92008-12-10 05:08:54 +00003564};
3565
shiqiane35fdd92008-12-10 05:08:54 +00003566PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) {
3567 return MakePolymorphicMatcher(DivisibleByImpl(n));
3568}
3569
3570// Tests that when AllOf() fails, only the first failing matcher is
3571// asked to explain why.
3572TEST(ExplainMatchResultTest, AllOf_False_False) {
3573 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003574 EXPECT_EQ("which is 1 modulo 4", Explain(m, 5));
shiqiane35fdd92008-12-10 05:08:54 +00003575}
3576
3577// Tests that when AllOf() fails, only the first failing matcher is
3578// asked to explain why.
3579TEST(ExplainMatchResultTest, AllOf_False_True) {
3580 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003581 EXPECT_EQ("which is 2 modulo 4", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003582}
3583
3584// Tests that when AllOf() fails, only the first failing matcher is
3585// asked to explain why.
3586TEST(ExplainMatchResultTest, AllOf_True_False) {
3587 const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003588 EXPECT_EQ("which is 2 modulo 3", Explain(m, 5));
shiqiane35fdd92008-12-10 05:08:54 +00003589}
3590
3591// Tests that when AllOf() succeeds, all matchers are asked to explain
3592// why.
3593TEST(ExplainMatchResultTest, AllOf_True_True) {
3594 const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003595 EXPECT_EQ("which is 0 modulo 2, and which is 0 modulo 3", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003596}
3597
3598TEST(ExplainMatchResultTest, AllOf_True_True_2) {
3599 const Matcher<int> m = AllOf(Ge(2), Le(3));
3600 EXPECT_EQ("", Explain(m, 2));
3601}
3602
3603TEST(ExplainmatcherResultTest, MonomorphicMatcher) {
3604 const Matcher<int> m = GreaterThan(5);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003605 EXPECT_EQ("which is 1 more than 5", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003606}
3607
3608// The following two tests verify that values without a public copy
3609// ctor can be used as arguments to matchers like Eq(), Ge(), and etc
3610// with the help of ByRef().
3611
3612class NotCopyable {
3613 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003614 explicit NotCopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00003615
3616 int value() const { return value_; }
3617
3618 bool operator==(const NotCopyable& rhs) const {
3619 return value() == rhs.value();
3620 }
3621
3622 bool operator>=(const NotCopyable& rhs) const {
3623 return value() >= rhs.value();
3624 }
3625 private:
3626 int value_;
3627
3628 GTEST_DISALLOW_COPY_AND_ASSIGN_(NotCopyable);
3629};
3630
3631TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) {
3632 const NotCopyable const_value1(1);
3633 const Matcher<const NotCopyable&> m = Eq(ByRef(const_value1));
3634
3635 const NotCopyable n1(1), n2(2);
3636 EXPECT_TRUE(m.Matches(n1));
3637 EXPECT_FALSE(m.Matches(n2));
3638}
3639
3640TEST(ByRefTest, AllowsNotCopyableValueInMatchers) {
3641 NotCopyable value2(2);
3642 const Matcher<NotCopyable&> m = Ge(ByRef(value2));
3643
3644 NotCopyable n1(1), n2(2);
3645 EXPECT_FALSE(m.Matches(n1));
3646 EXPECT_TRUE(m.Matches(n2));
3647}
3648
zhanyong.wan320814a2013-03-01 00:20:30 +00003649TEST(IsEmptyTest, ImplementsIsEmpty) {
3650 vector<int> container;
3651 EXPECT_THAT(container, IsEmpty());
3652 container.push_back(0);
3653 EXPECT_THAT(container, Not(IsEmpty()));
3654 container.push_back(1);
3655 EXPECT_THAT(container, Not(IsEmpty()));
3656}
3657
3658TEST(IsEmptyTest, WorksWithString) {
3659 string text;
3660 EXPECT_THAT(text, IsEmpty());
3661 text = "foo";
3662 EXPECT_THAT(text, Not(IsEmpty()));
3663 text = string("\0", 1);
3664 EXPECT_THAT(text, Not(IsEmpty()));
3665}
3666
3667TEST(IsEmptyTest, CanDescribeSelf) {
3668 Matcher<vector<int> > m = IsEmpty();
3669 EXPECT_EQ("is empty", Describe(m));
3670 EXPECT_EQ("isn't empty", DescribeNegation(m));
3671}
3672
3673TEST(IsEmptyTest, ExplainsResult) {
3674 Matcher<vector<int> > m = IsEmpty();
3675 vector<int> container;
3676 EXPECT_EQ("", Explain(m, container));
3677 container.push_back(0);
3678 EXPECT_EQ("whose size is 1", Explain(m, container));
3679}
3680
zhanyong.wana31d9ce2013-03-01 01:50:17 +00003681TEST(SizeIsTest, ImplementsSizeIs) {
3682 vector<int> container;
3683 EXPECT_THAT(container, SizeIs(0));
3684 EXPECT_THAT(container, Not(SizeIs(1)));
3685 container.push_back(0);
3686 EXPECT_THAT(container, Not(SizeIs(0)));
3687 EXPECT_THAT(container, SizeIs(1));
3688 container.push_back(0);
3689 EXPECT_THAT(container, Not(SizeIs(0)));
3690 EXPECT_THAT(container, SizeIs(2));
3691}
3692
3693TEST(SizeIsTest, WorksWithMap) {
3694 map<string, int> container;
3695 EXPECT_THAT(container, SizeIs(0));
3696 EXPECT_THAT(container, Not(SizeIs(1)));
3697 container.insert(make_pair("foo", 1));
3698 EXPECT_THAT(container, Not(SizeIs(0)));
3699 EXPECT_THAT(container, SizeIs(1));
3700 container.insert(make_pair("bar", 2));
3701 EXPECT_THAT(container, Not(SizeIs(0)));
3702 EXPECT_THAT(container, SizeIs(2));
3703}
3704
3705TEST(SizeIsTest, WorksWithReferences) {
3706 vector<int> container;
3707 Matcher<const vector<int>&> m = SizeIs(1);
3708 EXPECT_THAT(container, Not(m));
3709 container.push_back(0);
3710 EXPECT_THAT(container, m);
3711}
3712
3713TEST(SizeIsTest, CanDescribeSelf) {
3714 Matcher<vector<int> > m = SizeIs(2);
3715 EXPECT_EQ("size is equal to 2", Describe(m));
3716 EXPECT_EQ("size isn't equal to 2", DescribeNegation(m));
3717}
3718
3719TEST(SizeIsTest, ExplainsResult) {
3720 Matcher<vector<int> > m1 = SizeIs(2);
3721 Matcher<vector<int> > m2 = SizeIs(Lt(2u));
3722 Matcher<vector<int> > m3 = SizeIs(AnyOf(0, 3));
3723 Matcher<vector<int> > m4 = SizeIs(GreaterThan(1));
3724 vector<int> container;
3725 EXPECT_EQ("whose size 0 doesn't match", Explain(m1, container));
3726 EXPECT_EQ("whose size 0 matches", Explain(m2, container));
3727 EXPECT_EQ("whose size 0 matches", Explain(m3, container));
3728 EXPECT_EQ("whose size 0 doesn't match, which is 1 less than 1",
3729 Explain(m4, container));
3730 container.push_back(0);
3731 container.push_back(0);
3732 EXPECT_EQ("whose size 2 matches", Explain(m1, container));
3733 EXPECT_EQ("whose size 2 doesn't match", Explain(m2, container));
3734 EXPECT_EQ("whose size 2 doesn't match", Explain(m3, container));
3735 EXPECT_EQ("whose size 2 matches, which is 1 more than 1",
3736 Explain(m4, container));
3737}
3738
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003739#if GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003740// Tests ContainerEq with different container types, and
3741// different element types.
3742
3743template <typename T>
zhanyong.wanb8243162009-06-04 05:48:20 +00003744class ContainerEqTest : public testing::Test {};
zhanyong.wan6a896b52009-01-16 01:13:50 +00003745
3746typedef testing::Types<
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003747 set<int>,
3748 vector<size_t>,
3749 multiset<size_t>,
3750 list<int> >
zhanyong.wan6a896b52009-01-16 01:13:50 +00003751 ContainerEqTestTypes;
3752
3753TYPED_TEST_CASE(ContainerEqTest, ContainerEqTestTypes);
3754
3755// Tests that the filled container is equal to itself.
3756TYPED_TEST(ContainerEqTest, EqualsSelf) {
3757 static const int vals[] = {1, 1, 2, 3, 5, 8};
3758 TypeParam my_set(vals, vals + 6);
3759 const Matcher<TypeParam> m = ContainerEq(my_set);
3760 EXPECT_TRUE(m.Matches(my_set));
3761 EXPECT_EQ("", Explain(m, my_set));
3762}
3763
3764// Tests that missing values are reported.
3765TYPED_TEST(ContainerEqTest, ValueMissing) {
3766 static const int vals[] = {1, 1, 2, 3, 5, 8};
3767 static const int test_vals[] = {2, 1, 8, 5};
3768 TypeParam my_set(vals, vals + 6);
3769 TypeParam test_set(test_vals, test_vals + 4);
3770 const Matcher<TypeParam> m = ContainerEq(my_set);
3771 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003772 EXPECT_EQ("which doesn't have these expected elements: 3",
3773 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003774}
3775
3776// Tests that added values are reported.
3777TYPED_TEST(ContainerEqTest, ValueAdded) {
3778 static const int vals[] = {1, 1, 2, 3, 5, 8};
3779 static const int test_vals[] = {1, 2, 3, 5, 8, 46};
3780 TypeParam my_set(vals, vals + 6);
3781 TypeParam test_set(test_vals, test_vals + 6);
3782 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3783 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003784 EXPECT_EQ("which has these unexpected elements: 46", Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003785}
3786
3787// Tests that added and missing values are reported together.
3788TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) {
3789 static const int vals[] = {1, 1, 2, 3, 5, 8};
3790 static const int test_vals[] = {1, 2, 3, 8, 46};
3791 TypeParam my_set(vals, vals + 6);
3792 TypeParam test_set(test_vals, test_vals + 5);
3793 const Matcher<TypeParam> m = ContainerEq(my_set);
3794 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003795 EXPECT_EQ("which has these unexpected elements: 46,\n"
3796 "and doesn't have these expected elements: 5",
3797 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003798}
3799
3800// Tests duplicated value -- expect no explanation.
3801TYPED_TEST(ContainerEqTest, DuplicateDifference) {
3802 static const int vals[] = {1, 1, 2, 3, 5, 8};
3803 static const int test_vals[] = {1, 2, 3, 5, 8};
3804 TypeParam my_set(vals, vals + 6);
3805 TypeParam test_set(test_vals, test_vals + 5);
3806 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3807 // Depending on the container, match may be true or false
3808 // But in any case there should be no explanation.
3809 EXPECT_EQ("", Explain(m, test_set));
3810}
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003811#endif // GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003812
3813// Tests that mutliple missing values are reported.
3814// Using just vector here, so order is predicatble.
3815TEST(ContainerEqExtraTest, MultipleValuesMissing) {
3816 static const int vals[] = {1, 1, 2, 3, 5, 8};
3817 static const int test_vals[] = {2, 1, 5};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003818 vector<int> my_set(vals, vals + 6);
3819 vector<int> test_set(test_vals, test_vals + 3);
3820 const Matcher<vector<int> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003821 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003822 EXPECT_EQ("which doesn't have these expected elements: 3, 8",
3823 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003824}
3825
3826// Tests that added values are reported.
3827// Using just vector here, so order is predicatble.
3828TEST(ContainerEqExtraTest, MultipleValuesAdded) {
3829 static const int vals[] = {1, 1, 2, 3, 5, 8};
3830 static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003831 list<size_t> my_set(vals, vals + 6);
3832 list<size_t> test_set(test_vals, test_vals + 7);
3833 const Matcher<const list<size_t>&> m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003834 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003835 EXPECT_EQ("which has these unexpected elements: 92, 46",
3836 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003837}
3838
3839// Tests that added and missing values are reported together.
3840TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) {
3841 static const int vals[] = {1, 1, 2, 3, 5, 8};
3842 static const int test_vals[] = {1, 2, 3, 92, 46};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003843 list<size_t> my_set(vals, vals + 6);
3844 list<size_t> test_set(test_vals, test_vals + 5);
3845 const Matcher<const list<size_t> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003846 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003847 EXPECT_EQ("which has these unexpected elements: 92, 46,\n"
3848 "and doesn't have these expected elements: 5, 8",
zhanyong.wan6a896b52009-01-16 01:13:50 +00003849 Explain(m, test_set));
3850}
3851
3852// Tests to see that duplicate elements are detected,
3853// but (as above) not reported in the explanation.
3854TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) {
3855 static const int vals[] = {1, 1, 2, 3, 5, 8};
3856 static const int test_vals[] = {1, 2, 3, 5, 8};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003857 vector<int> my_set(vals, vals + 6);
3858 vector<int> test_set(test_vals, test_vals + 5);
3859 const Matcher<vector<int> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003860 EXPECT_TRUE(m.Matches(my_set));
3861 EXPECT_FALSE(m.Matches(test_set));
3862 // There is nothing to report when both sets contain all the same values.
3863 EXPECT_EQ("", Explain(m, test_set));
3864}
3865
3866// Tests that ContainerEq works for non-trivial associative containers,
3867// like maps.
3868TEST(ContainerEqExtraTest, WorksForMaps) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003869 map<int, std::string> my_map;
zhanyong.wan6a896b52009-01-16 01:13:50 +00003870 my_map[0] = "a";
3871 my_map[1] = "b";
3872
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003873 map<int, std::string> test_map;
zhanyong.wan6a896b52009-01-16 01:13:50 +00003874 test_map[0] = "aa";
3875 test_map[1] = "b";
3876
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003877 const Matcher<const map<int, std::string>&> m = ContainerEq(my_map);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003878 EXPECT_TRUE(m.Matches(my_map));
3879 EXPECT_FALSE(m.Matches(test_map));
3880
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003881 EXPECT_EQ("which has these unexpected elements: (0, \"aa\"),\n"
3882 "and doesn't have these expected elements: (0, \"a\")",
zhanyong.wan6a896b52009-01-16 01:13:50 +00003883 Explain(m, test_map));
3884}
3885
zhanyong.wanb8243162009-06-04 05:48:20 +00003886TEST(ContainerEqExtraTest, WorksForNativeArray) {
3887 int a1[] = { 1, 2, 3 };
3888 int a2[] = { 1, 2, 3 };
3889 int b[] = { 1, 2, 4 };
3890
3891 EXPECT_THAT(a1, ContainerEq(a2));
3892 EXPECT_THAT(a1, Not(ContainerEq(b)));
3893}
3894
3895TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) {
3896 const char a1[][3] = { "hi", "lo" };
3897 const char a2[][3] = { "hi", "lo" };
3898 const char b[][3] = { "lo", "hi" };
3899
3900 // Tests using ContainerEq() in the first dimension.
3901 EXPECT_THAT(a1, ContainerEq(a2));
3902 EXPECT_THAT(a1, Not(ContainerEq(b)));
3903
3904 // Tests using ContainerEq() in the second dimension.
3905 EXPECT_THAT(a1, ElementsAre(ContainerEq(a2[0]), ContainerEq(a2[1])));
3906 EXPECT_THAT(a1, ElementsAre(Not(ContainerEq(b[0])), ContainerEq(a2[1])));
3907}
3908
3909TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) {
3910 const int a1[] = { 1, 2, 3 };
3911 const int a2[] = { 1, 2, 3 };
3912 const int b[] = { 1, 2, 3, 4 };
3913
zhanyong.wan2661c682009-06-09 05:42:12 +00003914 const int* const p1 = a1;
3915 EXPECT_THAT(make_tuple(p1, 3), ContainerEq(a2));
3916 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(b)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003917
3918 const int c[] = { 1, 3, 2 };
zhanyong.wan2661c682009-06-09 05:42:12 +00003919 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(c)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003920}
3921
3922TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) {
3923 std::string a1[][3] = {
3924 { "hi", "hello", "ciao" },
3925 { "bye", "see you", "ciao" }
3926 };
3927
3928 std::string a2[][3] = {
3929 { "hi", "hello", "ciao" },
3930 { "bye", "see you", "ciao" }
3931 };
3932
3933 const Matcher<const std::string(&)[2][3]> m = ContainerEq(a2);
3934 EXPECT_THAT(a1, m);
3935
3936 a2[0][0] = "ha";
3937 EXPECT_THAT(a1, m);
3938}
3939
zhanyong.wan898725c2011-09-16 16:45:39 +00003940TEST(WhenSortedByTest, WorksForEmptyContainer) {
3941 const vector<int> numbers;
3942 EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre()));
3943 EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1))));
3944}
3945
3946TEST(WhenSortedByTest, WorksForNonEmptyContainer) {
3947 vector<unsigned> numbers;
3948 numbers.push_back(3);
3949 numbers.push_back(1);
3950 numbers.push_back(2);
3951 numbers.push_back(2);
3952 EXPECT_THAT(numbers, WhenSortedBy(greater<unsigned>(),
3953 ElementsAre(3, 2, 2, 1)));
3954 EXPECT_THAT(numbers, Not(WhenSortedBy(greater<unsigned>(),
3955 ElementsAre(1, 2, 2, 3))));
3956}
3957
3958TEST(WhenSortedByTest, WorksForNonVectorContainer) {
3959 list<string> words;
3960 words.push_back("say");
3961 words.push_back("hello");
3962 words.push_back("world");
3963 EXPECT_THAT(words, WhenSortedBy(less<string>(),
3964 ElementsAre("hello", "say", "world")));
3965 EXPECT_THAT(words, Not(WhenSortedBy(less<string>(),
3966 ElementsAre("say", "hello", "world"))));
3967}
3968
3969TEST(WhenSortedByTest, WorksForNativeArray) {
3970 const int numbers[] = { 1, 3, 2, 4 };
3971 const int sorted_numbers[] = { 1, 2, 3, 4 };
3972 EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre(1, 2, 3, 4)));
3973 EXPECT_THAT(numbers, WhenSortedBy(less<int>(),
3974 ElementsAreArray(sorted_numbers)));
3975 EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1, 3, 2, 4))));
3976}
3977
3978TEST(WhenSortedByTest, CanDescribeSelf) {
3979 const Matcher<vector<int> > m = WhenSortedBy(less<int>(), ElementsAre(1, 2));
3980 EXPECT_EQ("(when sorted) has 2 elements where\n"
3981 "element #0 is equal to 1,\n"
3982 "element #1 is equal to 2",
3983 Describe(m));
3984 EXPECT_EQ("(when sorted) doesn't have 2 elements, or\n"
3985 "element #0 isn't equal to 1, or\n"
3986 "element #1 isn't equal to 2",
3987 DescribeNegation(m));
3988}
3989
3990TEST(WhenSortedByTest, ExplainsMatchResult) {
3991 const int a[] = { 2, 1 };
3992 EXPECT_EQ("which is { 1, 2 } when sorted, whose element #0 doesn't match",
3993 Explain(WhenSortedBy(less<int>(), ElementsAre(2, 3)), a));
3994 EXPECT_EQ("which is { 1, 2 } when sorted",
3995 Explain(WhenSortedBy(less<int>(), ElementsAre(1, 2)), a));
3996}
3997
3998// WhenSorted() is a simple wrapper on WhenSortedBy(). Hence we don't
3999// need to test it as exhaustively as we test the latter.
4000
4001TEST(WhenSortedTest, WorksForEmptyContainer) {
4002 const vector<int> numbers;
4003 EXPECT_THAT(numbers, WhenSorted(ElementsAre()));
4004 EXPECT_THAT(numbers, Not(WhenSorted(ElementsAre(1))));
4005}
4006
4007TEST(WhenSortedTest, WorksForNonEmptyContainer) {
4008 list<string> words;
4009 words.push_back("3");
4010 words.push_back("1");
4011 words.push_back("2");
4012 words.push_back("2");
4013 EXPECT_THAT(words, WhenSorted(ElementsAre("1", "2", "2", "3")));
4014 EXPECT_THAT(words, Not(WhenSorted(ElementsAre("3", "1", "2", "2"))));
4015}
4016
zhanyong.wana9a59e02013-03-27 16:14:55 +00004017TEST(WhenSortedTest, WorksForMapTypes) {
4018 map<string, int> word_counts;
4019 word_counts["and"] = 1;
4020 word_counts["the"] = 1;
4021 word_counts["buffalo"] = 2;
4022 EXPECT_THAT(word_counts, WhenSorted(ElementsAre(
4023 Pair("and", 1), Pair("buffalo", 2), Pair("the", 1))));
4024 EXPECT_THAT(word_counts, Not(WhenSorted(ElementsAre(
4025 Pair("and", 1), Pair("the", 1), Pair("buffalo", 2)))));
4026}
4027
4028TEST(WhenSortedTest, WorksForMultiMapTypes) {
4029 multimap<int, int> ifib;
4030 ifib.insert(make_pair(8, 6));
4031 ifib.insert(make_pair(2, 3));
4032 ifib.insert(make_pair(1, 1));
4033 ifib.insert(make_pair(3, 4));
4034 ifib.insert(make_pair(1, 2));
4035 ifib.insert(make_pair(5, 5));
4036 EXPECT_THAT(ifib, WhenSorted(ElementsAre(Pair(1, 1),
4037 Pair(1, 2),
4038 Pair(2, 3),
4039 Pair(3, 4),
4040 Pair(5, 5),
4041 Pair(8, 6))));
4042 EXPECT_THAT(ifib, Not(WhenSorted(ElementsAre(Pair(8, 6),
4043 Pair(2, 3),
4044 Pair(1, 1),
4045 Pair(3, 4),
4046 Pair(1, 2),
4047 Pair(5, 5)))));
4048}
4049
4050TEST(WhenSortedTest, WorksForPolymorphicMatcher) {
4051 std::deque<int> d;
4052 d.push_back(2);
4053 d.push_back(1);
4054 EXPECT_THAT(d, WhenSorted(ElementsAre(1, 2)));
4055 EXPECT_THAT(d, Not(WhenSorted(ElementsAre(2, 1))));
4056}
4057
4058TEST(WhenSortedTest, WorksForVectorConstRefMatcher) {
4059 std::deque<int> d;
4060 d.push_back(2);
4061 d.push_back(1);
4062 Matcher<const std::vector<int>&> vector_match = ElementsAre(1, 2);
4063 EXPECT_THAT(d, WhenSorted(vector_match));
4064 Matcher<const std::vector<int>&> not_vector_match = ElementsAre(2, 1);
4065 EXPECT_THAT(d, Not(WhenSorted(not_vector_match)));
4066}
4067
4068// Deliberately bare pseudo-container.
4069// Offers only begin() and end() accessors, yielding InputIterator.
4070template <typename T>
4071class Streamlike {
4072 private:
4073 class ConstIter;
4074 public:
4075 typedef ConstIter const_iterator;
4076 typedef T value_type;
4077
4078 template <typename InIter>
4079 Streamlike(InIter first, InIter last) : remainder_(first, last) {}
4080
4081 const_iterator begin() const {
4082 return const_iterator(this, remainder_.begin());
4083 }
4084 const_iterator end() const {
4085 return const_iterator(this, remainder_.end());
4086 }
4087
4088 private:
4089 class ConstIter : public std::iterator<std::input_iterator_tag,
4090 value_type,
4091 ptrdiff_t,
4092 const value_type&,
4093 const value_type*> {
4094 public:
4095 ConstIter(const Streamlike* s,
4096 typename std::list<value_type>::iterator pos)
4097 : s_(s), pos_(pos) {}
4098
4099 const value_type& operator*() const { return *pos_; }
4100 const value_type* operator->() const { return &*pos_; }
4101 ConstIter& operator++() {
4102 s_->remainder_.erase(pos_++);
4103 return *this;
4104 }
4105
4106 // *iter++ is required to work (see std::istreambuf_iterator).
4107 // (void)iter++ is also required to work.
4108 class PostIncrProxy {
4109 public:
4110 explicit PostIncrProxy(const value_type& value) : value_(value) {}
4111 value_type operator*() const { return value_; }
4112 private:
4113 value_type value_;
4114 };
4115 PostIncrProxy operator++(int) {
4116 PostIncrProxy proxy(**this);
4117 ++(*this);
4118 return proxy;
4119 }
4120
4121 friend bool operator==(const ConstIter& a, const ConstIter& b) {
4122 return a.s_ == b.s_ && a.pos_ == b.pos_;
4123 }
4124 friend bool operator!=(const ConstIter& a, const ConstIter& b) {
4125 return !(a == b);
4126 }
4127
4128 private:
4129 const Streamlike* s_;
4130 typename std::list<value_type>::iterator pos_;
4131 };
4132
4133 friend std::ostream& operator<<(std::ostream& os, const Streamlike& s) {
4134 os << "[";
4135 typedef typename std::list<value_type>::const_iterator Iter;
4136 const char* sep = "";
4137 for (Iter it = s.remainder_.begin(); it != s.remainder_.end(); ++it) {
4138 os << sep << *it;
4139 sep = ",";
4140 }
4141 os << "]";
4142 return os;
4143 }
4144
4145 mutable std::list<value_type> remainder_; // modified by iteration
4146};
4147
4148TEST(StreamlikeTest, Iteration) {
4149 const int a[5] = { 2, 1, 4, 5, 3 };
4150 Streamlike<int> s(a, a + 5);
4151 Streamlike<int>::const_iterator it = s.begin();
4152 const int* ip = a;
4153 while (it != s.end()) {
4154 SCOPED_TRACE(ip - a);
4155 EXPECT_EQ(*ip++, *it++);
4156 }
4157}
4158
4159TEST(WhenSortedTest, WorksForStreamlike) {
4160 // Streamlike 'container' provides only minimal iterator support.
4161 // Its iterators are tagged with input_iterator_tag.
4162 const int a[5] = { 2, 1, 4, 5, 3 };
4163 Streamlike<int> s(a, a + 5);
4164 EXPECT_THAT(s, WhenSorted(ElementsAre(1, 2, 3, 4, 5)));
4165 EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3))));
4166}
4167
4168TEST(WhenSortedTest, WorksForVectorConstRefMatcherOnStreamlike) {
4169 const int a[5] = { 2, 1, 4, 5, 3 };
4170 Streamlike<int> s(a, a + 5);
4171 Matcher<const std::vector<int>&> vector_match = ElementsAre(1, 2, 3, 4, 5);
4172 EXPECT_THAT(s, WhenSorted(vector_match));
4173 EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3))));
4174}
4175
zhanyong.wan736baa82010-09-27 17:44:16 +00004176// Tests IsReadableTypeName().
4177
4178TEST(IsReadableTypeNameTest, ReturnsTrueForShortNames) {
4179 EXPECT_TRUE(IsReadableTypeName("int"));
4180 EXPECT_TRUE(IsReadableTypeName("const unsigned char*"));
4181 EXPECT_TRUE(IsReadableTypeName("MyMap<int, void*>"));
4182 EXPECT_TRUE(IsReadableTypeName("void (*)(int, bool)"));
4183}
4184
4185TEST(IsReadableTypeNameTest, ReturnsTrueForLongNonTemplateNonFunctionNames) {
4186 EXPECT_TRUE(IsReadableTypeName("my_long_namespace::MyClassName"));
4187 EXPECT_TRUE(IsReadableTypeName("int [5][6][7][8][9][10][11]"));
4188 EXPECT_TRUE(IsReadableTypeName("my_namespace::MyOuterClass::MyInnerClass"));
4189}
4190
4191TEST(IsReadableTypeNameTest, ReturnsFalseForLongTemplateNames) {
4192 EXPECT_FALSE(
4193 IsReadableTypeName("basic_string<char, std::char_traits<char> >"));
4194 EXPECT_FALSE(IsReadableTypeName("std::vector<int, std::alloc_traits<int> >"));
4195}
4196
4197TEST(IsReadableTypeNameTest, ReturnsFalseForLongFunctionTypeNames) {
4198 EXPECT_FALSE(IsReadableTypeName("void (&)(int, bool, char, float)"));
4199}
4200
zhanyong.wan4a5330d2009-02-19 00:36:44 +00004201// Tests JoinAsTuple().
4202
4203TEST(JoinAsTupleTest, JoinsEmptyTuple) {
4204 EXPECT_EQ("", JoinAsTuple(Strings()));
4205}
4206
4207TEST(JoinAsTupleTest, JoinsOneTuple) {
4208 const char* fields[] = { "1" };
4209 EXPECT_EQ("1", JoinAsTuple(Strings(fields, fields + 1)));
4210}
4211
4212TEST(JoinAsTupleTest, JoinsTwoTuple) {
4213 const char* fields[] = { "1", "a" };
4214 EXPECT_EQ("(1, a)", JoinAsTuple(Strings(fields, fields + 2)));
4215}
4216
4217TEST(JoinAsTupleTest, JoinsTenTuple) {
4218 const char* fields[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
4219 EXPECT_EQ("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)",
4220 JoinAsTuple(Strings(fields, fields + 10)));
4221}
4222
4223// Tests FormatMatcherDescription().
4224
4225TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) {
4226 EXPECT_EQ("is even",
zhanyong.wanb4140802010-06-08 22:53:57 +00004227 FormatMatcherDescription(false, "IsEven", Strings()));
4228 EXPECT_EQ("not (is even)",
4229 FormatMatcherDescription(true, "IsEven", Strings()));
zhanyong.wan4a5330d2009-02-19 00:36:44 +00004230
4231 const char* params[] = { "5" };
4232 EXPECT_EQ("equals 5",
zhanyong.wanb4140802010-06-08 22:53:57 +00004233 FormatMatcherDescription(false, "Equals",
zhanyong.wan4a5330d2009-02-19 00:36:44 +00004234 Strings(params, params + 1)));
4235
4236 const char* params2[] = { "5", "8" };
4237 EXPECT_EQ("is in range (5, 8)",
zhanyong.wanb4140802010-06-08 22:53:57 +00004238 FormatMatcherDescription(false, "IsInRange",
zhanyong.wan4a5330d2009-02-19 00:36:44 +00004239 Strings(params2, params2 + 2)));
4240}
4241
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00004242// Tests PolymorphicMatcher::mutable_impl().
4243TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {
4244 PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
4245 DivisibleByImpl& impl = m.mutable_impl();
4246 EXPECT_EQ(42, impl.divider());
4247
4248 impl.set_divider(0);
4249 EXPECT_EQ(0, m.mutable_impl().divider());
4250}
4251
4252// Tests PolymorphicMatcher::impl().
4253TEST(PolymorphicMatcherTest, CanAccessImpl) {
4254 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
4255 const DivisibleByImpl& impl = m.impl();
4256 EXPECT_EQ(42, impl.divider());
4257}
4258
zhanyong.wanb1c7f932010-03-24 17:35:11 +00004259TEST(MatcherTupleTest, ExplainsMatchFailure) {
4260 stringstream ss1;
4261 ExplainMatchFailureTupleTo(make_tuple(Matcher<char>(Eq('a')), GreaterThan(5)),
4262 make_tuple('a', 10), &ss1);
4263 EXPECT_EQ("", ss1.str()); // Successful match.
4264
4265 stringstream ss2;
4266 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
4267 make_tuple(2, 'b'), &ss2);
4268 EXPECT_EQ(" Expected arg #0: is > 5\n"
4269 " Actual: 2, which is 3 less than 5\n"
zhanyong.wand60c5f42010-07-21 22:21:07 +00004270 " Expected arg #1: is equal to 'a' (97, 0x61)\n"
4271 " Actual: 'b' (98, 0x62)\n",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00004272 ss2.str()); // Failed match where both arguments need explanation.
4273
4274 stringstream ss3;
4275 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
4276 make_tuple(2, 'a'), &ss3);
4277 EXPECT_EQ(" Expected arg #0: is > 5\n"
4278 " Actual: 2, which is 3 less than 5\n",
4279 ss3.str()); // Failed match where only one argument needs
4280 // explanation.
4281}
4282
zhanyong.wan33605ba2010-04-22 23:37:47 +00004283// Tests Each().
4284
4285TEST(EachTest, ExplainsMatchResultCorrectly) {
4286 set<int> a; // empty
4287
4288 Matcher<set<int> > m = Each(2);
4289 EXPECT_EQ("", Explain(m, a));
4290
zhanyong.wan736baa82010-09-27 17:44:16 +00004291 Matcher<const int(&)[1]> n = Each(1); // NOLINT
zhanyong.wan33605ba2010-04-22 23:37:47 +00004292
4293 const int b[1] = { 1 };
4294 EXPECT_EQ("", Explain(n, b));
4295
4296 n = Each(3);
4297 EXPECT_EQ("whose element #0 doesn't match", Explain(n, b));
4298
4299 a.insert(1);
4300 a.insert(2);
4301 a.insert(3);
4302 m = Each(GreaterThan(0));
4303 EXPECT_EQ("", Explain(m, a));
4304
4305 m = Each(GreaterThan(10));
4306 EXPECT_EQ("whose element #0 doesn't match, which is 9 less than 10",
4307 Explain(m, a));
4308}
4309
4310TEST(EachTest, DescribesItselfCorrectly) {
4311 Matcher<vector<int> > m = Each(1);
4312 EXPECT_EQ("only contains elements that is equal to 1", Describe(m));
4313
4314 Matcher<vector<int> > m2 = Not(m);
4315 EXPECT_EQ("contains some element that isn't equal to 1", Describe(m2));
4316}
4317
4318TEST(EachTest, MatchesVectorWhenAllElementsMatch) {
4319 vector<int> some_vector;
4320 EXPECT_THAT(some_vector, Each(1));
4321 some_vector.push_back(3);
4322 EXPECT_THAT(some_vector, Not(Each(1)));
4323 EXPECT_THAT(some_vector, Each(3));
4324 some_vector.push_back(1);
4325 some_vector.push_back(2);
4326 EXPECT_THAT(some_vector, Not(Each(3)));
4327 EXPECT_THAT(some_vector, Each(Lt(3.5)));
4328
4329 vector<string> another_vector;
4330 another_vector.push_back("fee");
4331 EXPECT_THAT(another_vector, Each(string("fee")));
4332 another_vector.push_back("fie");
4333 another_vector.push_back("foe");
4334 another_vector.push_back("fum");
4335 EXPECT_THAT(another_vector, Not(Each(string("fee"))));
4336}
4337
4338TEST(EachTest, MatchesMapWhenAllElementsMatch) {
4339 map<const char*, int> my_map;
4340 const char* bar = "a string";
4341 my_map[bar] = 2;
4342 EXPECT_THAT(my_map, Each(make_pair(bar, 2)));
4343
4344 map<string, int> another_map;
4345 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1)));
4346 another_map["fee"] = 1;
4347 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1)));
4348 another_map["fie"] = 2;
4349 another_map["foe"] = 3;
4350 another_map["fum"] = 4;
4351 EXPECT_THAT(another_map, Not(Each(make_pair(string("fee"), 1))));
4352 EXPECT_THAT(another_map, Not(Each(make_pair(string("fum"), 1))));
4353 EXPECT_THAT(another_map, Each(Pair(_, Gt(0))));
4354}
4355
4356TEST(EachTest, AcceptsMatcher) {
4357 const int a[] = { 1, 2, 3 };
4358 EXPECT_THAT(a, Each(Gt(0)));
4359 EXPECT_THAT(a, Not(Each(Gt(1))));
4360}
4361
4362TEST(EachTest, WorksForNativeArrayAsTuple) {
4363 const int a[] = { 1, 2 };
4364 const int* const pointer = a;
4365 EXPECT_THAT(make_tuple(pointer, 2), Each(Gt(0)));
4366 EXPECT_THAT(make_tuple(pointer, 2), Not(Each(Gt(1))));
4367}
4368
zhanyong.wanab5b77c2010-05-17 19:32:48 +00004369// For testing Pointwise().
4370class IsHalfOfMatcher {
4371 public:
4372 template <typename T1, typename T2>
4373 bool MatchAndExplain(const tuple<T1, T2>& a_pair,
4374 MatchResultListener* listener) const {
4375 if (get<0>(a_pair) == get<1>(a_pair)/2) {
4376 *listener << "where the second is " << get<1>(a_pair);
4377 return true;
4378 } else {
4379 *listener << "where the second/2 is " << get<1>(a_pair)/2;
4380 return false;
4381 }
4382 }
4383
4384 void DescribeTo(ostream* os) const {
4385 *os << "are a pair where the first is half of the second";
4386 }
4387
4388 void DescribeNegationTo(ostream* os) const {
4389 *os << "are a pair where the first isn't half of the second";
4390 }
4391};
4392
4393PolymorphicMatcher<IsHalfOfMatcher> IsHalfOf() {
4394 return MakePolymorphicMatcher(IsHalfOfMatcher());
4395}
4396
4397TEST(PointwiseTest, DescribesSelf) {
4398 vector<int> rhs;
4399 rhs.push_back(1);
4400 rhs.push_back(2);
4401 rhs.push_back(3);
4402 const Matcher<const vector<int>&> m = Pointwise(IsHalfOf(), rhs);
4403 EXPECT_EQ("contains 3 values, where each value and its corresponding value "
4404 "in { 1, 2, 3 } are a pair where the first is half of the second",
4405 Describe(m));
4406 EXPECT_EQ("doesn't contain exactly 3 values, or contains a value x at some "
4407 "index i where x and the i-th value of { 1, 2, 3 } are a pair "
4408 "where the first isn't half of the second",
4409 DescribeNegation(m));
4410}
4411
4412TEST(PointwiseTest, MakesCopyOfRhs) {
4413 list<signed char> rhs;
4414 rhs.push_back(2);
4415 rhs.push_back(4);
4416
4417 int lhs[] = { 1, 2 };
4418 const Matcher<const int (&)[2]> m = Pointwise(IsHalfOf(), rhs);
4419 EXPECT_THAT(lhs, m);
4420
4421 // Changing rhs now shouldn't affect m, which made a copy of rhs.
4422 rhs.push_back(6);
4423 EXPECT_THAT(lhs, m);
4424}
4425
4426TEST(PointwiseTest, WorksForLhsNativeArray) {
4427 const int lhs[] = { 1, 2, 3 };
4428 vector<int> rhs;
4429 rhs.push_back(2);
4430 rhs.push_back(4);
4431 rhs.push_back(6);
4432 EXPECT_THAT(lhs, Pointwise(Lt(), rhs));
4433 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
4434}
4435
4436TEST(PointwiseTest, WorksForRhsNativeArray) {
4437 const int rhs[] = { 1, 2, 3 };
4438 vector<int> lhs;
4439 lhs.push_back(2);
4440 lhs.push_back(4);
4441 lhs.push_back(6);
4442 EXPECT_THAT(lhs, Pointwise(Gt(), rhs));
4443 EXPECT_THAT(lhs, Not(Pointwise(Lt(), rhs)));
4444}
4445
4446TEST(PointwiseTest, RejectsWrongSize) {
4447 const double lhs[2] = { 1, 2 };
4448 const int rhs[1] = { 0 };
4449 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
4450 EXPECT_EQ("which contains 2 values",
4451 Explain(Pointwise(Gt(), rhs), lhs));
4452
4453 const int rhs2[3] = { 0, 1, 2 };
4454 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs2)));
4455}
4456
4457TEST(PointwiseTest, RejectsWrongContent) {
4458 const double lhs[3] = { 1, 2, 3 };
4459 const int rhs[3] = { 2, 6, 4 };
4460 EXPECT_THAT(lhs, Not(Pointwise(IsHalfOf(), rhs)));
4461 EXPECT_EQ("where the value pair (2, 6) at index #1 don't match, "
4462 "where the second/2 is 3",
4463 Explain(Pointwise(IsHalfOf(), rhs), lhs));
4464}
4465
4466TEST(PointwiseTest, AcceptsCorrectContent) {
4467 const double lhs[3] = { 1, 2, 3 };
4468 const int rhs[3] = { 2, 4, 6 };
4469 EXPECT_THAT(lhs, Pointwise(IsHalfOf(), rhs));
4470 EXPECT_EQ("", Explain(Pointwise(IsHalfOf(), rhs), lhs));
4471}
4472
4473TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) {
4474 const double lhs[3] = { 1, 2, 3 };
4475 const int rhs[3] = { 2, 4, 6 };
4476 const Matcher<tuple<const double&, const int&> > m1 = IsHalfOf();
4477 EXPECT_THAT(lhs, Pointwise(m1, rhs));
4478 EXPECT_EQ("", Explain(Pointwise(m1, rhs), lhs));
4479
4480 // This type works as a tuple<const double&, const int&> can be
4481 // implicitly cast to tuple<double, int>.
4482 const Matcher<tuple<double, int> > m2 = IsHalfOf();
4483 EXPECT_THAT(lhs, Pointwise(m2, rhs));
4484 EXPECT_EQ("", Explain(Pointwise(m2, rhs), lhs));
4485}
4486
shiqiane35fdd92008-12-10 05:08:54 +00004487} // namespace gmock_matchers_test
4488} // namespace testing