blob: 3ec2989ef433c68a09ced249038217d86d7f8003 [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
zhanyong.wanf4274522013-04-24 02:49:43 +0000724TEST(ATest, WorksForDerivedClass) {
725 Base base;
726 Derived derived;
727 EXPECT_THAT(&base, A<Base*>());
728 // This shouldn't compile: EXPECT_THAT(&base, A<Derived*>());
729 EXPECT_THAT(&derived, A<Base*>());
730 EXPECT_THAT(&derived, A<Derived*>());
731}
732
shiqiane35fdd92008-12-10 05:08:54 +0000733// Tests that A<T>() describes itself properly.
734TEST(ATest, CanDescribeSelf) {
735 EXPECT_EQ("is anything", Describe(A<bool>()));
736}
737
738// Tests that An<T>() matches any value of type T.
739TEST(AnTest, MatchesAnyValue) {
740 // Tests a matcher for a value type.
741 Matcher<int> m1 = An<int>();
742 EXPECT_TRUE(m1.Matches(9143));
743 EXPECT_TRUE(m1.Matches(-1532));
744
745 // Tests a matcher for a reference type.
746 int a = 2;
747 int b = -6;
748 Matcher<int&> m2 = An<int&>();
749 EXPECT_TRUE(m2.Matches(a));
750 EXPECT_TRUE(m2.Matches(b));
751}
752
753// Tests that An<T>() describes itself properly.
754TEST(AnTest, CanDescribeSelf) {
755 EXPECT_EQ("is anything", Describe(An<int>()));
756}
757
758// Tests that _ can be used as a matcher for any type and matches any
759// value of that type.
760TEST(UnderscoreTest, MatchesAnyValue) {
761 // Uses _ as a matcher for a value type.
762 Matcher<int> m1 = _;
763 EXPECT_TRUE(m1.Matches(123));
764 EXPECT_TRUE(m1.Matches(-242));
765
766 // Uses _ as a matcher for a reference type.
767 bool a = false;
768 const bool b = true;
769 Matcher<const bool&> m2 = _;
770 EXPECT_TRUE(m2.Matches(a));
771 EXPECT_TRUE(m2.Matches(b));
772}
773
774// Tests that _ describes itself properly.
775TEST(UnderscoreTest, CanDescribeSelf) {
776 Matcher<int> m = _;
777 EXPECT_EQ("is anything", Describe(m));
778}
779
780// Tests that Eq(x) matches any value equal to x.
781TEST(EqTest, MatchesEqualValue) {
782 // 2 C-strings with same content but different addresses.
783 const char a1[] = "hi";
784 const char a2[] = "hi";
785
786 Matcher<const char*> m1 = Eq(a1);
787 EXPECT_TRUE(m1.Matches(a1));
788 EXPECT_FALSE(m1.Matches(a2));
789}
790
791// Tests that Eq(v) describes itself properly.
792
793class Unprintable {
794 public:
795 Unprintable() : c_('a') {}
796
zhanyong.wan32de5f52009-12-23 00:13:23 +0000797 bool operator==(const Unprintable& /* rhs */) { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000798 private:
799 char c_;
800};
801
802TEST(EqTest, CanDescribeSelf) {
803 Matcher<Unprintable> m = Eq(Unprintable());
804 EXPECT_EQ("is equal to 1-byte object <61>", Describe(m));
805}
806
807// Tests that Eq(v) can be used to match any type that supports
808// comparing with type T, where T is v's type.
809TEST(EqTest, IsPolymorphic) {
810 Matcher<int> m1 = Eq(1);
811 EXPECT_TRUE(m1.Matches(1));
812 EXPECT_FALSE(m1.Matches(2));
813
814 Matcher<char> m2 = Eq(1);
815 EXPECT_TRUE(m2.Matches('\1'));
816 EXPECT_FALSE(m2.Matches('a'));
817}
818
819// Tests that TypedEq<T>(v) matches values of type T that's equal to v.
820TEST(TypedEqTest, ChecksEqualityForGivenType) {
821 Matcher<char> m1 = TypedEq<char>('a');
822 EXPECT_TRUE(m1.Matches('a'));
823 EXPECT_FALSE(m1.Matches('b'));
824
825 Matcher<int> m2 = TypedEq<int>(6);
826 EXPECT_TRUE(m2.Matches(6));
827 EXPECT_FALSE(m2.Matches(7));
828}
829
830// Tests that TypedEq(v) describes itself properly.
831TEST(TypedEqTest, CanDescribeSelf) {
832 EXPECT_EQ("is equal to 2", Describe(TypedEq<int>(2)));
833}
834
835// Tests that TypedEq<T>(v) has type Matcher<T>.
836
837// Type<T>::IsTypeOf(v) compiles iff the type of value v is T, where T
838// is a "bare" type (i.e. not in the form of const U or U&). If v's
839// type is not T, the compiler will generate a message about
840// "undefined referece".
841template <typename T>
842struct Type {
zhanyong.wan32de5f52009-12-23 00:13:23 +0000843 static bool IsTypeOf(const T& /* v */) { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000844
845 template <typename T2>
846 static void IsTypeOf(T2 v);
847};
848
849TEST(TypedEqTest, HasSpecifiedType) {
850 // Verfies that the type of TypedEq<T>(v) is Matcher<T>.
851 Type<Matcher<int> >::IsTypeOf(TypedEq<int>(5));
852 Type<Matcher<double> >::IsTypeOf(TypedEq<double>(5));
853}
854
855// Tests that Ge(v) matches anything >= v.
856TEST(GeTest, ImplementsGreaterThanOrEqual) {
857 Matcher<int> m1 = Ge(0);
858 EXPECT_TRUE(m1.Matches(1));
859 EXPECT_TRUE(m1.Matches(0));
860 EXPECT_FALSE(m1.Matches(-1));
861}
862
863// Tests that Ge(v) describes itself properly.
864TEST(GeTest, CanDescribeSelf) {
865 Matcher<int> m = Ge(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000866 EXPECT_EQ("is >= 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000867}
868
869// Tests that Gt(v) matches anything > v.
870TEST(GtTest, ImplementsGreaterThan) {
871 Matcher<double> m1 = Gt(0);
872 EXPECT_TRUE(m1.Matches(1.0));
873 EXPECT_FALSE(m1.Matches(0.0));
874 EXPECT_FALSE(m1.Matches(-1.0));
875}
876
877// Tests that Gt(v) describes itself properly.
878TEST(GtTest, CanDescribeSelf) {
879 Matcher<int> m = Gt(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000880 EXPECT_EQ("is > 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000881}
882
883// Tests that Le(v) matches anything <= v.
884TEST(LeTest, ImplementsLessThanOrEqual) {
885 Matcher<char> m1 = Le('b');
886 EXPECT_TRUE(m1.Matches('a'));
887 EXPECT_TRUE(m1.Matches('b'));
888 EXPECT_FALSE(m1.Matches('c'));
889}
890
891// Tests that Le(v) describes itself properly.
892TEST(LeTest, CanDescribeSelf) {
893 Matcher<int> m = Le(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000894 EXPECT_EQ("is <= 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000895}
896
897// Tests that Lt(v) matches anything < v.
898TEST(LtTest, ImplementsLessThan) {
899 Matcher<const string&> m1 = Lt("Hello");
900 EXPECT_TRUE(m1.Matches("Abc"));
901 EXPECT_FALSE(m1.Matches("Hello"));
902 EXPECT_FALSE(m1.Matches("Hello, world!"));
903}
904
905// Tests that Lt(v) describes itself properly.
906TEST(LtTest, CanDescribeSelf) {
907 Matcher<int> m = Lt(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000908 EXPECT_EQ("is < 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000909}
910
911// Tests that Ne(v) matches anything != v.
912TEST(NeTest, ImplementsNotEqual) {
913 Matcher<int> m1 = Ne(0);
914 EXPECT_TRUE(m1.Matches(1));
915 EXPECT_TRUE(m1.Matches(-1));
916 EXPECT_FALSE(m1.Matches(0));
917}
918
919// Tests that Ne(v) describes itself properly.
920TEST(NeTest, CanDescribeSelf) {
921 Matcher<int> m = Ne(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000922 EXPECT_EQ("isn't equal to 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000923}
924
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000925// Tests that IsNull() matches any NULL pointer of any type.
926TEST(IsNullTest, MatchesNullPointer) {
927 Matcher<int*> m1 = IsNull();
928 int* p1 = NULL;
929 int n = 0;
930 EXPECT_TRUE(m1.Matches(p1));
931 EXPECT_FALSE(m1.Matches(&n));
932
933 Matcher<const char*> m2 = IsNull();
934 const char* p2 = NULL;
935 EXPECT_TRUE(m2.Matches(p2));
936 EXPECT_FALSE(m2.Matches("hi"));
937
zhanyong.wan95b12332009-09-25 18:55:50 +0000938#if !GTEST_OS_SYMBIAN
939 // Nokia's Symbian compiler generates:
940 // gmock-matchers.h: ambiguous access to overloaded function
941 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(void *)'
942 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(const testing::
943 // MatcherInterface<void *> *)'
944 // gmock-matchers.h: (point of instantiation: 'testing::
945 // gmock_matchers_test::IsNullTest_MatchesNullPointer_Test::TestBody()')
946 // gmock-matchers.h: (instantiating: 'testing::PolymorphicMatc
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000947 Matcher<void*> m3 = IsNull();
948 void* p3 = NULL;
949 EXPECT_TRUE(m3.Matches(p3));
950 EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef)));
zhanyong.wan95b12332009-09-25 18:55:50 +0000951#endif
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000952}
953
vladlosev79b83502009-11-18 00:43:37 +0000954TEST(IsNullTest, LinkedPtr) {
955 const Matcher<linked_ptr<int> > m = IsNull();
956 const linked_ptr<int> null_p;
957 const linked_ptr<int> non_null_p(new int);
958
959 EXPECT_TRUE(m.Matches(null_p));
960 EXPECT_FALSE(m.Matches(non_null_p));
961}
962
963TEST(IsNullTest, ReferenceToConstLinkedPtr) {
964 const Matcher<const linked_ptr<double>&> m = IsNull();
965 const linked_ptr<double> null_p;
966 const linked_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
vladloseve56daa72009-11-18 01:08:08 +0000972TEST(IsNullTest, ReferenceToConstScopedPtr) {
973 const Matcher<const scoped_ptr<double>&> m = IsNull();
974 const scoped_ptr<double> null_p;
975 const scoped_ptr<double> non_null_p(new double);
976
977 EXPECT_TRUE(m.Matches(null_p));
978 EXPECT_FALSE(m.Matches(non_null_p));
979}
980
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000981// Tests that IsNull() describes itself properly.
982TEST(IsNullTest, CanDescribeSelf) {
983 Matcher<int*> m = IsNull();
984 EXPECT_EQ("is NULL", Describe(m));
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000985 EXPECT_EQ("isn't NULL", DescribeNegation(m));
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000986}
987
shiqiane35fdd92008-12-10 05:08:54 +0000988// Tests that NotNull() matches any non-NULL pointer of any type.
989TEST(NotNullTest, MatchesNonNullPointer) {
990 Matcher<int*> m1 = NotNull();
991 int* p1 = NULL;
992 int n = 0;
993 EXPECT_FALSE(m1.Matches(p1));
994 EXPECT_TRUE(m1.Matches(&n));
995
996 Matcher<const char*> m2 = NotNull();
997 const char* p2 = NULL;
998 EXPECT_FALSE(m2.Matches(p2));
999 EXPECT_TRUE(m2.Matches("hi"));
1000}
1001
vladlosev79b83502009-11-18 00:43:37 +00001002TEST(NotNullTest, LinkedPtr) {
1003 const Matcher<linked_ptr<int> > m = NotNull();
1004 const linked_ptr<int> null_p;
1005 const linked_ptr<int> non_null_p(new int);
1006
1007 EXPECT_FALSE(m.Matches(null_p));
1008 EXPECT_TRUE(m.Matches(non_null_p));
1009}
1010
1011TEST(NotNullTest, ReferenceToConstLinkedPtr) {
1012 const Matcher<const linked_ptr<double>&> m = NotNull();
1013 const linked_ptr<double> null_p;
1014 const linked_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
vladloseve56daa72009-11-18 01:08:08 +00001020TEST(NotNullTest, ReferenceToConstScopedPtr) {
1021 const Matcher<const scoped_ptr<double>&> m = NotNull();
1022 const scoped_ptr<double> null_p;
1023 const scoped_ptr<double> non_null_p(new double);
1024
1025 EXPECT_FALSE(m.Matches(null_p));
1026 EXPECT_TRUE(m.Matches(non_null_p));
1027}
1028
shiqiane35fdd92008-12-10 05:08:54 +00001029// Tests that NotNull() describes itself properly.
1030TEST(NotNullTest, CanDescribeSelf) {
1031 Matcher<int*> m = NotNull();
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001032 EXPECT_EQ("isn't NULL", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001033}
1034
1035// Tests that Ref(variable) matches an argument that references
1036// 'variable'.
1037TEST(RefTest, MatchesSameVariable) {
1038 int a = 0;
1039 int b = 0;
1040 Matcher<int&> m = Ref(a);
1041 EXPECT_TRUE(m.Matches(a));
1042 EXPECT_FALSE(m.Matches(b));
1043}
1044
1045// Tests that Ref(variable) describes itself properly.
1046TEST(RefTest, CanDescribeSelf) {
1047 int n = 5;
1048 Matcher<int&> m = Ref(n);
1049 stringstream ss;
1050 ss << "references the variable @" << &n << " 5";
1051 EXPECT_EQ(string(ss.str()), Describe(m));
1052}
1053
1054// Test that Ref(non_const_varialbe) can be used as a matcher for a
1055// const reference.
1056TEST(RefTest, CanBeUsedAsMatcherForConstReference) {
1057 int a = 0;
1058 int b = 0;
1059 Matcher<const int&> m = Ref(a);
1060 EXPECT_TRUE(m.Matches(a));
1061 EXPECT_FALSE(m.Matches(b));
1062}
1063
1064// Tests that Ref(variable) is covariant, i.e. Ref(derived) can be
1065// used wherever Ref(base) can be used (Ref(derived) is a sub-type
1066// of Ref(base), but not vice versa.
1067
shiqiane35fdd92008-12-10 05:08:54 +00001068TEST(RefTest, IsCovariant) {
1069 Base base, base2;
1070 Derived derived;
1071 Matcher<const Base&> m1 = Ref(base);
1072 EXPECT_TRUE(m1.Matches(base));
1073 EXPECT_FALSE(m1.Matches(base2));
1074 EXPECT_FALSE(m1.Matches(derived));
1075
1076 m1 = Ref(derived);
1077 EXPECT_TRUE(m1.Matches(derived));
1078 EXPECT_FALSE(m1.Matches(base));
1079 EXPECT_FALSE(m1.Matches(base2));
1080}
1081
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001082TEST(RefTest, ExplainsResult) {
1083 int n = 0;
1084 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), n),
1085 StartsWith("which is located @"));
1086
1087 int m = 0;
1088 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), m),
1089 StartsWith("which is located @"));
1090}
1091
shiqiane35fdd92008-12-10 05:08:54 +00001092// Tests string comparison matchers.
1093
1094TEST(StrEqTest, MatchesEqualString) {
1095 Matcher<const char*> m = StrEq(string("Hello"));
1096 EXPECT_TRUE(m.Matches("Hello"));
1097 EXPECT_FALSE(m.Matches("hello"));
1098 EXPECT_FALSE(m.Matches(NULL));
1099
1100 Matcher<const string&> m2 = StrEq("Hello");
1101 EXPECT_TRUE(m2.Matches("Hello"));
1102 EXPECT_FALSE(m2.Matches("Hi"));
1103}
1104
1105TEST(StrEqTest, CanDescribeSelf) {
vladlosevaa432202011-04-01 21:58:42 +00001106 Matcher<string> m = StrEq("Hi-\'\"?\\\a\b\f\n\r\t\v\xD3");
1107 EXPECT_EQ("is equal to \"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",
shiqiane35fdd92008-12-10 05:08:54 +00001108 Describe(m));
1109
1110 string str("01204500800");
1111 str[3] = '\0';
1112 Matcher<string> m2 = StrEq(str);
1113 EXPECT_EQ("is equal to \"012\\04500800\"", Describe(m2));
1114 str[0] = str[6] = str[7] = str[9] = str[10] = '\0';
1115 Matcher<string> m3 = StrEq(str);
1116 EXPECT_EQ("is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3));
1117}
1118
1119TEST(StrNeTest, MatchesUnequalString) {
1120 Matcher<const char*> m = StrNe("Hello");
1121 EXPECT_TRUE(m.Matches(""));
1122 EXPECT_TRUE(m.Matches(NULL));
1123 EXPECT_FALSE(m.Matches("Hello"));
1124
1125 Matcher<string> m2 = StrNe(string("Hello"));
1126 EXPECT_TRUE(m2.Matches("hello"));
1127 EXPECT_FALSE(m2.Matches("Hello"));
1128}
1129
1130TEST(StrNeTest, CanDescribeSelf) {
1131 Matcher<const char*> m = StrNe("Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001132 EXPECT_EQ("isn't equal to \"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001133}
1134
1135TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
1136 Matcher<const char*> m = StrCaseEq(string("Hello"));
1137 EXPECT_TRUE(m.Matches("Hello"));
1138 EXPECT_TRUE(m.Matches("hello"));
1139 EXPECT_FALSE(m.Matches("Hi"));
1140 EXPECT_FALSE(m.Matches(NULL));
1141
1142 Matcher<const string&> m2 = StrCaseEq("Hello");
1143 EXPECT_TRUE(m2.Matches("hello"));
1144 EXPECT_FALSE(m2.Matches("Hi"));
1145}
1146
1147TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1148 string str1("oabocdooeoo");
1149 string str2("OABOCDOOEOO");
1150 Matcher<const string&> m0 = StrCaseEq(str1);
1151 EXPECT_FALSE(m0.Matches(str2 + string(1, '\0')));
1152
1153 str1[3] = str2[3] = '\0';
1154 Matcher<const string&> m1 = StrCaseEq(str1);
1155 EXPECT_TRUE(m1.Matches(str2));
1156
1157 str1[0] = str1[6] = str1[7] = str1[10] = '\0';
1158 str2[0] = str2[6] = str2[7] = str2[10] = '\0';
1159 Matcher<const string&> m2 = StrCaseEq(str1);
1160 str1[9] = str2[9] = '\0';
1161 EXPECT_FALSE(m2.Matches(str2));
1162
1163 Matcher<const string&> m3 = StrCaseEq(str1);
1164 EXPECT_TRUE(m3.Matches(str2));
1165
1166 EXPECT_FALSE(m3.Matches(str2 + "x"));
1167 str2.append(1, '\0');
1168 EXPECT_FALSE(m3.Matches(str2));
1169 EXPECT_FALSE(m3.Matches(string(str2, 0, 9)));
1170}
1171
1172TEST(StrCaseEqTest, CanDescribeSelf) {
1173 Matcher<string> m = StrCaseEq("Hi");
1174 EXPECT_EQ("is equal to (ignoring case) \"Hi\"", Describe(m));
1175}
1176
1177TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1178 Matcher<const char*> m = StrCaseNe("Hello");
1179 EXPECT_TRUE(m.Matches("Hi"));
1180 EXPECT_TRUE(m.Matches(NULL));
1181 EXPECT_FALSE(m.Matches("Hello"));
1182 EXPECT_FALSE(m.Matches("hello"));
1183
1184 Matcher<string> m2 = StrCaseNe(string("Hello"));
1185 EXPECT_TRUE(m2.Matches(""));
1186 EXPECT_FALSE(m2.Matches("Hello"));
1187}
1188
1189TEST(StrCaseNeTest, CanDescribeSelf) {
1190 Matcher<const char*> m = StrCaseNe("Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001191 EXPECT_EQ("isn't equal to (ignoring case) \"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001192}
1193
1194// Tests that HasSubstr() works for matching string-typed values.
1195TEST(HasSubstrTest, WorksForStringClasses) {
1196 const Matcher<string> m1 = HasSubstr("foo");
1197 EXPECT_TRUE(m1.Matches(string("I love food.")));
1198 EXPECT_FALSE(m1.Matches(string("tofo")));
1199
1200 const Matcher<const std::string&> m2 = HasSubstr("foo");
1201 EXPECT_TRUE(m2.Matches(std::string("I love food.")));
1202 EXPECT_FALSE(m2.Matches(std::string("tofo")));
1203}
1204
1205// Tests that HasSubstr() works for matching C-string-typed values.
1206TEST(HasSubstrTest, WorksForCStrings) {
1207 const Matcher<char*> m1 = HasSubstr("foo");
1208 EXPECT_TRUE(m1.Matches(const_cast<char*>("I love food.")));
1209 EXPECT_FALSE(m1.Matches(const_cast<char*>("tofo")));
1210 EXPECT_FALSE(m1.Matches(NULL));
1211
1212 const Matcher<const char*> m2 = HasSubstr("foo");
1213 EXPECT_TRUE(m2.Matches("I love food."));
1214 EXPECT_FALSE(m2.Matches("tofo"));
1215 EXPECT_FALSE(m2.Matches(NULL));
1216}
1217
1218// Tests that HasSubstr(s) describes itself properly.
1219TEST(HasSubstrTest, CanDescribeSelf) {
1220 Matcher<string> m = HasSubstr("foo\n\"");
1221 EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m));
1222}
1223
zhanyong.wanb5937da2009-07-16 20:26:41 +00001224TEST(KeyTest, CanDescribeSelf) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001225 Matcher<const pair<std::string, int>&> m = Key("foo");
zhanyong.wanb5937da2009-07-16 20:26:41 +00001226 EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001227 EXPECT_EQ("doesn't have a key that is equal to \"foo\"", DescribeNegation(m));
1228}
1229
1230TEST(KeyTest, ExplainsResult) {
1231 Matcher<pair<int, bool> > m = Key(GreaterThan(10));
1232 EXPECT_EQ("whose first field is a value which is 5 less than 10",
1233 Explain(m, make_pair(5, true)));
1234 EXPECT_EQ("whose first field is a value which is 5 more than 10",
1235 Explain(m, make_pair(15, true)));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001236}
1237
1238TEST(KeyTest, MatchesCorrectly) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001239 pair<int, std::string> p(25, "foo");
zhanyong.wanb5937da2009-07-16 20:26:41 +00001240 EXPECT_THAT(p, Key(25));
1241 EXPECT_THAT(p, Not(Key(42)));
1242 EXPECT_THAT(p, Key(Ge(20)));
1243 EXPECT_THAT(p, Not(Key(Lt(25))));
1244}
1245
1246TEST(KeyTest, SafelyCastsInnerMatcher) {
1247 Matcher<int> is_positive = Gt(0);
1248 Matcher<int> is_negative = Lt(0);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001249 pair<char, bool> p('a', true);
zhanyong.wanb5937da2009-07-16 20:26:41 +00001250 EXPECT_THAT(p, Key(is_positive));
1251 EXPECT_THAT(p, Not(Key(is_negative)));
1252}
1253
1254TEST(KeyTest, InsideContainsUsingMap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001255 map<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 EXPECT_THAT(container, Contains(Key(1)));
1260 EXPECT_THAT(container, Not(Contains(Key(3))));
1261}
1262
1263TEST(KeyTest, InsideContainsUsingMultimap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001264 multimap<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001265 container.insert(make_pair(1, 'a'));
1266 container.insert(make_pair(2, 'b'));
1267 container.insert(make_pair(4, 'c'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001268
1269 EXPECT_THAT(container, Not(Contains(Key(25))));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001270 container.insert(make_pair(25, 'd'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001271 EXPECT_THAT(container, Contains(Key(25)));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001272 container.insert(make_pair(25, 'e'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001273 EXPECT_THAT(container, Contains(Key(25)));
1274
1275 EXPECT_THAT(container, Contains(Key(1)));
1276 EXPECT_THAT(container, Not(Contains(Key(3))));
1277}
1278
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001279TEST(PairTest, Typing) {
1280 // Test verifies the following type conversions can be compiled.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001281 Matcher<const pair<const char*, int>&> m1 = Pair("foo", 42);
1282 Matcher<const pair<const char*, int> > m2 = Pair("foo", 42);
1283 Matcher<pair<const char*, int> > m3 = Pair("foo", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001284
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001285 Matcher<pair<int, const std::string> > m4 = Pair(25, "42");
1286 Matcher<pair<const std::string, int> > m5 = Pair("25", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001287}
1288
1289TEST(PairTest, CanDescribeSelf) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001290 Matcher<const pair<std::string, int>&> m1 = Pair("foo", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001291 EXPECT_EQ("has a first field that is equal to \"foo\""
1292 ", and has a second field that is equal to 42",
1293 Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001294 EXPECT_EQ("has a first field that isn't equal to \"foo\""
1295 ", or has a second field that isn't equal to 42",
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001296 DescribeNegation(m1));
1297 // Double and triple negation (1 or 2 times not and description of negation).
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001298 Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42));
1299 EXPECT_EQ("has a first field that isn't equal to 13"
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001300 ", and has a second field that is equal to 42",
1301 DescribeNegation(m2));
1302}
1303
1304TEST(PairTest, CanExplainMatchResultTo) {
zhanyong.wan82113312010-01-08 21:55:40 +00001305 // If neither field matches, Pair() should explain about the first
1306 // field.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001307 const Matcher<pair<int, int> > m = Pair(GreaterThan(0), GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001308 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001309 Explain(m, make_pair(-1, -2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001310
zhanyong.wan82113312010-01-08 21:55:40 +00001311 // If the first field matches but the second doesn't, Pair() should
1312 // explain about the second field.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001313 EXPECT_EQ("whose second field does not match, which is 2 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001314 Explain(m, make_pair(1, -2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001315
zhanyong.wan82113312010-01-08 21:55:40 +00001316 // If the first field doesn't match but the second does, Pair()
1317 // should explain about the first field.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001318 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001319 Explain(m, make_pair(-1, 2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001320
zhanyong.wan82113312010-01-08 21:55:40 +00001321 // If both fields match, Pair() should explain about them both.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001322 EXPECT_EQ("whose both fields match, where the first field is a value "
1323 "which is 1 more than 0, and the second field is a value "
1324 "which is 2 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001325 Explain(m, make_pair(1, 2)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001326
1327 // If only the first match has an explanation, only this explanation should
1328 // be printed.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001329 const Matcher<pair<int, int> > explain_first = Pair(GreaterThan(0), 0);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001330 EXPECT_EQ("whose both fields match, where the first field is a value "
1331 "which is 1 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001332 Explain(explain_first, make_pair(1, 0)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001333
1334 // If only the second match has an explanation, only this explanation should
1335 // be printed.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001336 const Matcher<pair<int, int> > explain_second = Pair(0, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001337 EXPECT_EQ("whose both fields match, where the second field is a value "
1338 "which is 1 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001339 Explain(explain_second, make_pair(0, 1)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001340}
1341
1342TEST(PairTest, MatchesCorrectly) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001343 pair<int, std::string> p(25, "foo");
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001344
1345 // Both fields match.
1346 EXPECT_THAT(p, Pair(25, "foo"));
1347 EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o")));
1348
1349 // 'first' doesnt' match, but 'second' matches.
1350 EXPECT_THAT(p, Not(Pair(42, "foo")));
1351 EXPECT_THAT(p, Not(Pair(Lt(25), "foo")));
1352
1353 // 'first' matches, but 'second' doesn't match.
1354 EXPECT_THAT(p, Not(Pair(25, "bar")));
1355 EXPECT_THAT(p, Not(Pair(25, Not("foo"))));
1356
1357 // Neither field matches.
1358 EXPECT_THAT(p, Not(Pair(13, "bar")));
1359 EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a"))));
1360}
1361
1362TEST(PairTest, SafelyCastsInnerMatchers) {
1363 Matcher<int> is_positive = Gt(0);
1364 Matcher<int> is_negative = Lt(0);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001365 pair<char, bool> p('a', true);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001366 EXPECT_THAT(p, Pair(is_positive, _));
1367 EXPECT_THAT(p, Not(Pair(is_negative, _)));
1368 EXPECT_THAT(p, Pair(_, is_positive));
1369 EXPECT_THAT(p, Not(Pair(_, is_negative)));
1370}
1371
1372TEST(PairTest, InsideContainsUsingMap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001373 map<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001374 container.insert(make_pair(1, 'a'));
1375 container.insert(make_pair(2, 'b'));
1376 container.insert(make_pair(4, 'c'));
zhanyong.wan95b12332009-09-25 18:55:50 +00001377 EXPECT_THAT(container, Contains(Pair(1, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001378 EXPECT_THAT(container, Contains(Pair(1, _)));
zhanyong.wan95b12332009-09-25 18:55:50 +00001379 EXPECT_THAT(container, Contains(Pair(_, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001380 EXPECT_THAT(container, Not(Contains(Pair(3, _))));
1381}
1382
shiqiane35fdd92008-12-10 05:08:54 +00001383// Tests StartsWith(s).
1384
1385TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
1386 const Matcher<const char*> m1 = StartsWith(string(""));
1387 EXPECT_TRUE(m1.Matches("Hi"));
1388 EXPECT_TRUE(m1.Matches(""));
1389 EXPECT_FALSE(m1.Matches(NULL));
1390
1391 const Matcher<const string&> m2 = StartsWith("Hi");
1392 EXPECT_TRUE(m2.Matches("Hi"));
1393 EXPECT_TRUE(m2.Matches("Hi Hi!"));
1394 EXPECT_TRUE(m2.Matches("High"));
1395 EXPECT_FALSE(m2.Matches("H"));
1396 EXPECT_FALSE(m2.Matches(" Hi"));
1397}
1398
1399TEST(StartsWithTest, CanDescribeSelf) {
1400 Matcher<const std::string> m = StartsWith("Hi");
1401 EXPECT_EQ("starts with \"Hi\"", Describe(m));
1402}
1403
1404// Tests EndsWith(s).
1405
1406TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
1407 const Matcher<const char*> m1 = EndsWith("");
1408 EXPECT_TRUE(m1.Matches("Hi"));
1409 EXPECT_TRUE(m1.Matches(""));
1410 EXPECT_FALSE(m1.Matches(NULL));
1411
1412 const Matcher<const string&> m2 = EndsWith(string("Hi"));
1413 EXPECT_TRUE(m2.Matches("Hi"));
1414 EXPECT_TRUE(m2.Matches("Wow Hi Hi"));
1415 EXPECT_TRUE(m2.Matches("Super Hi"));
1416 EXPECT_FALSE(m2.Matches("i"));
1417 EXPECT_FALSE(m2.Matches("Hi "));
1418}
1419
1420TEST(EndsWithTest, CanDescribeSelf) {
1421 Matcher<const std::string> m = EndsWith("Hi");
1422 EXPECT_EQ("ends with \"Hi\"", Describe(m));
1423}
1424
shiqiane35fdd92008-12-10 05:08:54 +00001425// Tests MatchesRegex().
1426
1427TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
1428 const Matcher<const char*> m1 = MatchesRegex("a.*z");
1429 EXPECT_TRUE(m1.Matches("az"));
1430 EXPECT_TRUE(m1.Matches("abcz"));
1431 EXPECT_FALSE(m1.Matches(NULL));
1432
1433 const Matcher<const string&> m2 = MatchesRegex(new RE("a.*z"));
1434 EXPECT_TRUE(m2.Matches("azbz"));
1435 EXPECT_FALSE(m2.Matches("az1"));
1436 EXPECT_FALSE(m2.Matches("1az"));
1437}
1438
1439TEST(MatchesRegexTest, CanDescribeSelf) {
1440 Matcher<const std::string> m1 = MatchesRegex(string("Hi.*"));
1441 EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1));
1442
zhanyong.wand14aaed2010-01-14 05:36:32 +00001443 Matcher<const char*> m2 = MatchesRegex(new RE("a.*"));
1444 EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +00001445}
1446
1447// Tests ContainsRegex().
1448
1449TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
1450 const Matcher<const char*> m1 = ContainsRegex(string("a.*z"));
1451 EXPECT_TRUE(m1.Matches("az"));
1452 EXPECT_TRUE(m1.Matches("0abcz1"));
1453 EXPECT_FALSE(m1.Matches(NULL));
1454
1455 const Matcher<const string&> m2 = ContainsRegex(new RE("a.*z"));
1456 EXPECT_TRUE(m2.Matches("azbz"));
1457 EXPECT_TRUE(m2.Matches("az1"));
1458 EXPECT_FALSE(m2.Matches("1a"));
1459}
1460
1461TEST(ContainsRegexTest, CanDescribeSelf) {
1462 Matcher<const std::string> m1 = ContainsRegex("Hi.*");
1463 EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1));
1464
zhanyong.wand14aaed2010-01-14 05:36:32 +00001465 Matcher<const char*> m2 = ContainsRegex(new RE("a.*"));
1466 EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +00001467}
shiqiane35fdd92008-12-10 05:08:54 +00001468
1469// Tests for wide strings.
1470#if GTEST_HAS_STD_WSTRING
1471TEST(StdWideStrEqTest, MatchesEqual) {
1472 Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello"));
1473 EXPECT_TRUE(m.Matches(L"Hello"));
1474 EXPECT_FALSE(m.Matches(L"hello"));
1475 EXPECT_FALSE(m.Matches(NULL));
1476
1477 Matcher<const ::std::wstring&> m2 = StrEq(L"Hello");
1478 EXPECT_TRUE(m2.Matches(L"Hello"));
1479 EXPECT_FALSE(m2.Matches(L"Hi"));
1480
1481 Matcher<const ::std::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1482 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1483 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1484
1485 ::std::wstring str(L"01204500800");
1486 str[3] = L'\0';
1487 Matcher<const ::std::wstring&> m4 = StrEq(str);
1488 EXPECT_TRUE(m4.Matches(str));
1489 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1490 Matcher<const ::std::wstring&> m5 = StrEq(str);
1491 EXPECT_TRUE(m5.Matches(str));
1492}
1493
1494TEST(StdWideStrEqTest, CanDescribeSelf) {
vladlosevaa432202011-04-01 21:58:42 +00001495 Matcher< ::std::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v");
1496 EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
shiqiane35fdd92008-12-10 05:08:54 +00001497 Describe(m));
1498
1499 Matcher< ::std::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1500 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1501 Describe(m2));
1502
1503 ::std::wstring str(L"01204500800");
1504 str[3] = L'\0';
1505 Matcher<const ::std::wstring&> m4 = StrEq(str);
1506 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1507 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1508 Matcher<const ::std::wstring&> m5 = StrEq(str);
1509 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1510}
1511
1512TEST(StdWideStrNeTest, MatchesUnequalString) {
1513 Matcher<const wchar_t*> m = StrNe(L"Hello");
1514 EXPECT_TRUE(m.Matches(L""));
1515 EXPECT_TRUE(m.Matches(NULL));
1516 EXPECT_FALSE(m.Matches(L"Hello"));
1517
1518 Matcher< ::std::wstring> m2 = StrNe(::std::wstring(L"Hello"));
1519 EXPECT_TRUE(m2.Matches(L"hello"));
1520 EXPECT_FALSE(m2.Matches(L"Hello"));
1521}
1522
1523TEST(StdWideStrNeTest, CanDescribeSelf) {
1524 Matcher<const wchar_t*> m = StrNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001525 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001526}
1527
1528TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1529 Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello"));
1530 EXPECT_TRUE(m.Matches(L"Hello"));
1531 EXPECT_TRUE(m.Matches(L"hello"));
1532 EXPECT_FALSE(m.Matches(L"Hi"));
1533 EXPECT_FALSE(m.Matches(NULL));
1534
1535 Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello");
1536 EXPECT_TRUE(m2.Matches(L"hello"));
1537 EXPECT_FALSE(m2.Matches(L"Hi"));
1538}
1539
1540TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1541 ::std::wstring str1(L"oabocdooeoo");
1542 ::std::wstring str2(L"OABOCDOOEOO");
1543 Matcher<const ::std::wstring&> m0 = StrCaseEq(str1);
1544 EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L'\0')));
1545
1546 str1[3] = str2[3] = L'\0';
1547 Matcher<const ::std::wstring&> m1 = StrCaseEq(str1);
1548 EXPECT_TRUE(m1.Matches(str2));
1549
1550 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1551 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1552 Matcher<const ::std::wstring&> m2 = StrCaseEq(str1);
1553 str1[9] = str2[9] = L'\0';
1554 EXPECT_FALSE(m2.Matches(str2));
1555
1556 Matcher<const ::std::wstring&> m3 = StrCaseEq(str1);
1557 EXPECT_TRUE(m3.Matches(str2));
1558
1559 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1560 str2.append(1, L'\0');
1561 EXPECT_FALSE(m3.Matches(str2));
1562 EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9)));
1563}
1564
1565TEST(StdWideStrCaseEqTest, CanDescribeSelf) {
1566 Matcher< ::std::wstring> m = StrCaseEq(L"Hi");
1567 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1568}
1569
1570TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1571 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1572 EXPECT_TRUE(m.Matches(L"Hi"));
1573 EXPECT_TRUE(m.Matches(NULL));
1574 EXPECT_FALSE(m.Matches(L"Hello"));
1575 EXPECT_FALSE(m.Matches(L"hello"));
1576
1577 Matcher< ::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello"));
1578 EXPECT_TRUE(m2.Matches(L""));
1579 EXPECT_FALSE(m2.Matches(L"Hello"));
1580}
1581
1582TEST(StdWideStrCaseNeTest, CanDescribeSelf) {
1583 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001584 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001585}
1586
1587// Tests that HasSubstr() works for matching wstring-typed values.
1588TEST(StdWideHasSubstrTest, WorksForStringClasses) {
1589 const Matcher< ::std::wstring> m1 = HasSubstr(L"foo");
1590 EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food.")));
1591 EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo")));
1592
1593 const Matcher<const ::std::wstring&> m2 = HasSubstr(L"foo");
1594 EXPECT_TRUE(m2.Matches(::std::wstring(L"I love food.")));
1595 EXPECT_FALSE(m2.Matches(::std::wstring(L"tofo")));
1596}
1597
1598// Tests that HasSubstr() works for matching C-wide-string-typed values.
1599TEST(StdWideHasSubstrTest, WorksForCStrings) {
1600 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1601 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1602 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1603 EXPECT_FALSE(m1.Matches(NULL));
1604
1605 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1606 EXPECT_TRUE(m2.Matches(L"I love food."));
1607 EXPECT_FALSE(m2.Matches(L"tofo"));
1608 EXPECT_FALSE(m2.Matches(NULL));
1609}
1610
1611// Tests that HasSubstr(s) describes itself properly.
1612TEST(StdWideHasSubstrTest, CanDescribeSelf) {
1613 Matcher< ::std::wstring> m = HasSubstr(L"foo\n\"");
1614 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1615}
1616
1617// Tests StartsWith(s).
1618
1619TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) {
1620 const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L""));
1621 EXPECT_TRUE(m1.Matches(L"Hi"));
1622 EXPECT_TRUE(m1.Matches(L""));
1623 EXPECT_FALSE(m1.Matches(NULL));
1624
1625 const Matcher<const ::std::wstring&> m2 = StartsWith(L"Hi");
1626 EXPECT_TRUE(m2.Matches(L"Hi"));
1627 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1628 EXPECT_TRUE(m2.Matches(L"High"));
1629 EXPECT_FALSE(m2.Matches(L"H"));
1630 EXPECT_FALSE(m2.Matches(L" Hi"));
1631}
1632
1633TEST(StdWideStartsWithTest, CanDescribeSelf) {
1634 Matcher<const ::std::wstring> m = StartsWith(L"Hi");
1635 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1636}
1637
1638// Tests EndsWith(s).
1639
1640TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) {
1641 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1642 EXPECT_TRUE(m1.Matches(L"Hi"));
1643 EXPECT_TRUE(m1.Matches(L""));
1644 EXPECT_FALSE(m1.Matches(NULL));
1645
1646 const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L"Hi"));
1647 EXPECT_TRUE(m2.Matches(L"Hi"));
1648 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1649 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1650 EXPECT_FALSE(m2.Matches(L"i"));
1651 EXPECT_FALSE(m2.Matches(L"Hi "));
1652}
1653
1654TEST(StdWideEndsWithTest, CanDescribeSelf) {
1655 Matcher<const ::std::wstring> m = EndsWith(L"Hi");
1656 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1657}
1658
1659#endif // GTEST_HAS_STD_WSTRING
1660
1661#if GTEST_HAS_GLOBAL_WSTRING
1662TEST(GlobalWideStrEqTest, MatchesEqual) {
1663 Matcher<const wchar_t*> m = StrEq(::wstring(L"Hello"));
1664 EXPECT_TRUE(m.Matches(L"Hello"));
1665 EXPECT_FALSE(m.Matches(L"hello"));
1666 EXPECT_FALSE(m.Matches(NULL));
1667
1668 Matcher<const ::wstring&> m2 = StrEq(L"Hello");
1669 EXPECT_TRUE(m2.Matches(L"Hello"));
1670 EXPECT_FALSE(m2.Matches(L"Hi"));
1671
1672 Matcher<const ::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1673 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1674 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1675
1676 ::wstring str(L"01204500800");
1677 str[3] = L'\0';
1678 Matcher<const ::wstring&> m4 = StrEq(str);
1679 EXPECT_TRUE(m4.Matches(str));
1680 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1681 Matcher<const ::wstring&> m5 = StrEq(str);
1682 EXPECT_TRUE(m5.Matches(str));
1683}
1684
1685TEST(GlobalWideStrEqTest, CanDescribeSelf) {
vladlosevaa432202011-04-01 21:58:42 +00001686 Matcher< ::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v");
1687 EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
shiqiane35fdd92008-12-10 05:08:54 +00001688 Describe(m));
1689
1690 Matcher< ::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1691 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1692 Describe(m2));
1693
1694 ::wstring str(L"01204500800");
1695 str[3] = L'\0';
1696 Matcher<const ::wstring&> m4 = StrEq(str);
1697 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1698 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1699 Matcher<const ::wstring&> m5 = StrEq(str);
1700 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1701}
1702
1703TEST(GlobalWideStrNeTest, MatchesUnequalString) {
1704 Matcher<const wchar_t*> m = StrNe(L"Hello");
1705 EXPECT_TRUE(m.Matches(L""));
1706 EXPECT_TRUE(m.Matches(NULL));
1707 EXPECT_FALSE(m.Matches(L"Hello"));
1708
1709 Matcher< ::wstring> m2 = StrNe(::wstring(L"Hello"));
1710 EXPECT_TRUE(m2.Matches(L"hello"));
1711 EXPECT_FALSE(m2.Matches(L"Hello"));
1712}
1713
1714TEST(GlobalWideStrNeTest, CanDescribeSelf) {
1715 Matcher<const wchar_t*> m = StrNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001716 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001717}
1718
1719TEST(GlobalWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1720 Matcher<const wchar_t*> m = StrCaseEq(::wstring(L"Hello"));
1721 EXPECT_TRUE(m.Matches(L"Hello"));
1722 EXPECT_TRUE(m.Matches(L"hello"));
1723 EXPECT_FALSE(m.Matches(L"Hi"));
1724 EXPECT_FALSE(m.Matches(NULL));
1725
1726 Matcher<const ::wstring&> m2 = StrCaseEq(L"Hello");
1727 EXPECT_TRUE(m2.Matches(L"hello"));
1728 EXPECT_FALSE(m2.Matches(L"Hi"));
1729}
1730
1731TEST(GlobalWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1732 ::wstring str1(L"oabocdooeoo");
1733 ::wstring str2(L"OABOCDOOEOO");
1734 Matcher<const ::wstring&> m0 = StrCaseEq(str1);
1735 EXPECT_FALSE(m0.Matches(str2 + ::wstring(1, L'\0')));
1736
1737 str1[3] = str2[3] = L'\0';
1738 Matcher<const ::wstring&> m1 = StrCaseEq(str1);
1739 EXPECT_TRUE(m1.Matches(str2));
1740
1741 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1742 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1743 Matcher<const ::wstring&> m2 = StrCaseEq(str1);
1744 str1[9] = str2[9] = L'\0';
1745 EXPECT_FALSE(m2.Matches(str2));
1746
1747 Matcher<const ::wstring&> m3 = StrCaseEq(str1);
1748 EXPECT_TRUE(m3.Matches(str2));
1749
1750 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1751 str2.append(1, L'\0');
1752 EXPECT_FALSE(m3.Matches(str2));
1753 EXPECT_FALSE(m3.Matches(::wstring(str2, 0, 9)));
1754}
1755
1756TEST(GlobalWideStrCaseEqTest, CanDescribeSelf) {
1757 Matcher< ::wstring> m = StrCaseEq(L"Hi");
1758 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1759}
1760
1761TEST(GlobalWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1762 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1763 EXPECT_TRUE(m.Matches(L"Hi"));
1764 EXPECT_TRUE(m.Matches(NULL));
1765 EXPECT_FALSE(m.Matches(L"Hello"));
1766 EXPECT_FALSE(m.Matches(L"hello"));
1767
1768 Matcher< ::wstring> m2 = StrCaseNe(::wstring(L"Hello"));
1769 EXPECT_TRUE(m2.Matches(L""));
1770 EXPECT_FALSE(m2.Matches(L"Hello"));
1771}
1772
1773TEST(GlobalWideStrCaseNeTest, CanDescribeSelf) {
1774 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001775 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001776}
1777
1778// Tests that HasSubstr() works for matching wstring-typed values.
1779TEST(GlobalWideHasSubstrTest, WorksForStringClasses) {
1780 const Matcher< ::wstring> m1 = HasSubstr(L"foo");
1781 EXPECT_TRUE(m1.Matches(::wstring(L"I love food.")));
1782 EXPECT_FALSE(m1.Matches(::wstring(L"tofo")));
1783
1784 const Matcher<const ::wstring&> m2 = HasSubstr(L"foo");
1785 EXPECT_TRUE(m2.Matches(::wstring(L"I love food.")));
1786 EXPECT_FALSE(m2.Matches(::wstring(L"tofo")));
1787}
1788
1789// Tests that HasSubstr() works for matching C-wide-string-typed values.
1790TEST(GlobalWideHasSubstrTest, WorksForCStrings) {
1791 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1792 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1793 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1794 EXPECT_FALSE(m1.Matches(NULL));
1795
1796 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1797 EXPECT_TRUE(m2.Matches(L"I love food."));
1798 EXPECT_FALSE(m2.Matches(L"tofo"));
1799 EXPECT_FALSE(m2.Matches(NULL));
1800}
1801
1802// Tests that HasSubstr(s) describes itself properly.
1803TEST(GlobalWideHasSubstrTest, CanDescribeSelf) {
1804 Matcher< ::wstring> m = HasSubstr(L"foo\n\"");
1805 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1806}
1807
1808// Tests StartsWith(s).
1809
1810TEST(GlobalWideStartsWithTest, MatchesStringWithGivenPrefix) {
1811 const Matcher<const wchar_t*> m1 = StartsWith(::wstring(L""));
1812 EXPECT_TRUE(m1.Matches(L"Hi"));
1813 EXPECT_TRUE(m1.Matches(L""));
1814 EXPECT_FALSE(m1.Matches(NULL));
1815
1816 const Matcher<const ::wstring&> m2 = StartsWith(L"Hi");
1817 EXPECT_TRUE(m2.Matches(L"Hi"));
1818 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1819 EXPECT_TRUE(m2.Matches(L"High"));
1820 EXPECT_FALSE(m2.Matches(L"H"));
1821 EXPECT_FALSE(m2.Matches(L" Hi"));
1822}
1823
1824TEST(GlobalWideStartsWithTest, CanDescribeSelf) {
1825 Matcher<const ::wstring> m = StartsWith(L"Hi");
1826 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1827}
1828
1829// Tests EndsWith(s).
1830
1831TEST(GlobalWideEndsWithTest, MatchesStringWithGivenSuffix) {
1832 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1833 EXPECT_TRUE(m1.Matches(L"Hi"));
1834 EXPECT_TRUE(m1.Matches(L""));
1835 EXPECT_FALSE(m1.Matches(NULL));
1836
1837 const Matcher<const ::wstring&> m2 = EndsWith(::wstring(L"Hi"));
1838 EXPECT_TRUE(m2.Matches(L"Hi"));
1839 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1840 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1841 EXPECT_FALSE(m2.Matches(L"i"));
1842 EXPECT_FALSE(m2.Matches(L"Hi "));
1843}
1844
1845TEST(GlobalWideEndsWithTest, CanDescribeSelf) {
1846 Matcher<const ::wstring> m = EndsWith(L"Hi");
1847 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1848}
1849
1850#endif // GTEST_HAS_GLOBAL_WSTRING
1851
1852
1853typedef ::std::tr1::tuple<long, int> Tuple2; // NOLINT
1854
1855// Tests that Eq() matches a 2-tuple where the first field == the
1856// second field.
1857TEST(Eq2Test, MatchesEqualArguments) {
1858 Matcher<const Tuple2&> m = Eq();
1859 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1860 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1861}
1862
1863// Tests that Eq() describes itself properly.
1864TEST(Eq2Test, CanDescribeSelf) {
1865 Matcher<const Tuple2&> m = Eq();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001866 EXPECT_EQ("are an equal pair", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001867}
1868
1869// Tests that Ge() matches a 2-tuple where the first field >= the
1870// second field.
1871TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) {
1872 Matcher<const Tuple2&> m = Ge();
1873 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1874 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1875 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1876}
1877
1878// Tests that Ge() describes itself properly.
1879TEST(Ge2Test, CanDescribeSelf) {
1880 Matcher<const Tuple2&> m = Ge();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001881 EXPECT_EQ("are a pair where the first >= the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001882}
1883
1884// Tests that Gt() matches a 2-tuple where the first field > the
1885// second field.
1886TEST(Gt2Test, MatchesGreaterThanArguments) {
1887 Matcher<const Tuple2&> m = Gt();
1888 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1889 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1890 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1891}
1892
1893// Tests that Gt() describes itself properly.
1894TEST(Gt2Test, CanDescribeSelf) {
1895 Matcher<const Tuple2&> m = Gt();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001896 EXPECT_EQ("are a pair where the first > the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001897}
1898
1899// Tests that Le() matches a 2-tuple where the first field <= the
1900// second field.
1901TEST(Le2Test, MatchesLessThanOrEqualArguments) {
1902 Matcher<const Tuple2&> m = Le();
1903 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1904 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1905 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1906}
1907
1908// Tests that Le() describes itself properly.
1909TEST(Le2Test, CanDescribeSelf) {
1910 Matcher<const Tuple2&> m = Le();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001911 EXPECT_EQ("are a pair where the first <= the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001912}
1913
1914// Tests that Lt() matches a 2-tuple where the first field < the
1915// second field.
1916TEST(Lt2Test, MatchesLessThanArguments) {
1917 Matcher<const Tuple2&> m = Lt();
1918 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1919 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1920 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1921}
1922
1923// Tests that Lt() describes itself properly.
1924TEST(Lt2Test, CanDescribeSelf) {
1925 Matcher<const Tuple2&> m = Lt();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001926 EXPECT_EQ("are a pair where the first < the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001927}
1928
1929// Tests that Ne() matches a 2-tuple where the first field != the
1930// second field.
1931TEST(Ne2Test, MatchesUnequalArguments) {
1932 Matcher<const Tuple2&> m = Ne();
1933 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1934 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1935 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1936}
1937
1938// Tests that Ne() describes itself properly.
1939TEST(Ne2Test, CanDescribeSelf) {
1940 Matcher<const Tuple2&> m = Ne();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001941 EXPECT_EQ("are an unequal pair", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001942}
1943
1944// Tests that Not(m) matches any value that doesn't match m.
1945TEST(NotTest, NegatesMatcher) {
1946 Matcher<int> m;
1947 m = Not(Eq(2));
1948 EXPECT_TRUE(m.Matches(3));
1949 EXPECT_FALSE(m.Matches(2));
1950}
1951
1952// Tests that Not(m) describes itself properly.
1953TEST(NotTest, CanDescribeSelf) {
1954 Matcher<int> m = Not(Eq(5));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001955 EXPECT_EQ("isn't equal to 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001956}
1957
zhanyong.wan18490652009-05-11 18:54:08 +00001958// Tests that monomorphic matchers are safely cast by the Not matcher.
1959TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {
1960 // greater_than_5 is a monomorphic matcher.
1961 Matcher<int> greater_than_5 = Gt(5);
1962
1963 Matcher<const int&> m = Not(greater_than_5);
1964 Matcher<int&> m2 = Not(greater_than_5);
1965 Matcher<int&> m3 = Not(m);
1966}
1967
zhanyong.wan02c15052010-06-09 19:21:30 +00001968// Helper to allow easy testing of AllOf matchers with num parameters.
1969void AllOfMatches(int num, const Matcher<int>& m) {
1970 SCOPED_TRACE(Describe(m));
1971 EXPECT_TRUE(m.Matches(0));
1972 for (int i = 1; i <= num; ++i) {
1973 EXPECT_FALSE(m.Matches(i));
1974 }
1975 EXPECT_TRUE(m.Matches(num + 1));
1976}
1977
shiqiane35fdd92008-12-10 05:08:54 +00001978// Tests that AllOf(m1, ..., mn) matches any value that matches all of
1979// the given matchers.
1980TEST(AllOfTest, MatchesWhenAllMatch) {
1981 Matcher<int> m;
1982 m = AllOf(Le(2), Ge(1));
1983 EXPECT_TRUE(m.Matches(1));
1984 EXPECT_TRUE(m.Matches(2));
1985 EXPECT_FALSE(m.Matches(0));
1986 EXPECT_FALSE(m.Matches(3));
1987
1988 m = AllOf(Gt(0), Ne(1), Ne(2));
1989 EXPECT_TRUE(m.Matches(3));
1990 EXPECT_FALSE(m.Matches(2));
1991 EXPECT_FALSE(m.Matches(1));
1992 EXPECT_FALSE(m.Matches(0));
1993
1994 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
1995 EXPECT_TRUE(m.Matches(4));
1996 EXPECT_FALSE(m.Matches(3));
1997 EXPECT_FALSE(m.Matches(2));
1998 EXPECT_FALSE(m.Matches(1));
1999 EXPECT_FALSE(m.Matches(0));
2000
2001 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
2002 EXPECT_TRUE(m.Matches(0));
2003 EXPECT_TRUE(m.Matches(1));
2004 EXPECT_FALSE(m.Matches(3));
zhanyong.wan02c15052010-06-09 19:21:30 +00002005
2006 // The following tests for varying number of sub-matchers. Due to the way
2007 // the sub-matchers are handled it is enough to test every sub-matcher once
2008 // with sub-matchers using the same matcher type. Varying matcher types are
2009 // checked for above.
2010 AllOfMatches(2, AllOf(Ne(1), Ne(2)));
2011 AllOfMatches(3, AllOf(Ne(1), Ne(2), Ne(3)));
2012 AllOfMatches(4, AllOf(Ne(1), Ne(2), Ne(3), Ne(4)));
2013 AllOfMatches(5, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5)));
2014 AllOfMatches(6, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6)));
2015 AllOfMatches(7, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7)));
2016 AllOfMatches(8, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
2017 Ne(8)));
2018 AllOfMatches(9, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
2019 Ne(8), Ne(9)));
2020 AllOfMatches(10, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8),
2021 Ne(9), Ne(10)));
shiqiane35fdd92008-12-10 05:08:54 +00002022}
2023
2024// Tests that AllOf(m1, ..., mn) describes itself properly.
2025TEST(AllOfTest, CanDescribeSelf) {
2026 Matcher<int> m;
2027 m = AllOf(Le(2), Ge(1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002028 EXPECT_EQ("(is <= 2) and (is >= 1)", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00002029
2030 m = AllOf(Gt(0), Ne(1), Ne(2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002031 EXPECT_EQ("(is > 0) and "
2032 "((isn't equal to 1) and "
2033 "(isn't equal to 2))",
shiqiane35fdd92008-12-10 05:08:54 +00002034 Describe(m));
2035
2036
2037 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
jgm79a367e2012-04-10 16:02:11 +00002038 EXPECT_EQ("((is > 0) and "
2039 "(isn't equal to 1)) and "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002040 "((isn't equal to 2) and "
jgm79a367e2012-04-10 16:02:11 +00002041 "(isn't equal to 3))",
shiqiane35fdd92008-12-10 05:08:54 +00002042 Describe(m));
2043
2044
2045 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
jgm79a367e2012-04-10 16:02:11 +00002046 EXPECT_EQ("((is >= 0) and "
2047 "(is < 10)) and "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002048 "((isn't equal to 3) and "
2049 "((isn't equal to 5) and "
jgm79a367e2012-04-10 16:02:11 +00002050 "(isn't equal to 7)))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002051 Describe(m));
2052}
2053
2054// Tests that AllOf(m1, ..., mn) describes its negation properly.
2055TEST(AllOfTest, CanDescribeNegation) {
2056 Matcher<int> m;
2057 m = AllOf(Le(2), Ge(1));
2058 EXPECT_EQ("(isn't <= 2) or "
2059 "(isn't >= 1)",
2060 DescribeNegation(m));
2061
2062 m = AllOf(Gt(0), Ne(1), Ne(2));
2063 EXPECT_EQ("(isn't > 0) or "
2064 "((is equal to 1) or "
2065 "(is equal to 2))",
2066 DescribeNegation(m));
2067
2068
2069 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
jgm79a367e2012-04-10 16:02:11 +00002070 EXPECT_EQ("((isn't > 0) or "
2071 "(is equal to 1)) or "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002072 "((is equal to 2) or "
jgm79a367e2012-04-10 16:02:11 +00002073 "(is equal to 3))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002074 DescribeNegation(m));
2075
2076
2077 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
jgm79a367e2012-04-10 16:02:11 +00002078 EXPECT_EQ("((isn't >= 0) or "
2079 "(isn't < 10)) or "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002080 "((is equal to 3) or "
2081 "((is equal to 5) or "
jgm79a367e2012-04-10 16:02:11 +00002082 "(is equal to 7)))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002083 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00002084}
2085
zhanyong.wan18490652009-05-11 18:54:08 +00002086// Tests that monomorphic matchers are safely cast by the AllOf matcher.
2087TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
2088 // greater_than_5 and less_than_10 are monomorphic matchers.
2089 Matcher<int> greater_than_5 = Gt(5);
2090 Matcher<int> less_than_10 = Lt(10);
2091
2092 Matcher<const int&> m = AllOf(greater_than_5, less_than_10);
2093 Matcher<int&> m2 = AllOf(greater_than_5, less_than_10);
2094 Matcher<int&> m3 = AllOf(greater_than_5, m2);
2095
2096 // Tests that BothOf works when composing itself.
2097 Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10);
2098 Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);
2099}
2100
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002101TEST(AllOfTest, ExplainsResult) {
2102 Matcher<int> m;
2103
2104 // Successful match. Both matchers need to explain. The second
2105 // matcher doesn't give an explanation, so only the first matcher's
2106 // explanation is printed.
2107 m = AllOf(GreaterThan(10), Lt(30));
2108 EXPECT_EQ("which is 15 more than 10", Explain(m, 25));
2109
2110 // Successful match. Both matchers need to explain.
2111 m = AllOf(GreaterThan(10), GreaterThan(20));
2112 EXPECT_EQ("which is 20 more than 10, and which is 10 more than 20",
2113 Explain(m, 30));
2114
2115 // Successful match. All matchers need to explain. The second
2116 // matcher doesn't given an explanation.
2117 m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20));
2118 EXPECT_EQ("which is 15 more than 10, and which is 5 more than 20",
2119 Explain(m, 25));
2120
2121 // Successful match. All matchers need to explain.
2122 m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2123 EXPECT_EQ("which is 30 more than 10, and which is 20 more than 20, "
2124 "and which is 10 more than 30",
2125 Explain(m, 40));
2126
2127 // Failed match. The first matcher, which failed, needs to
2128 // explain.
2129 m = AllOf(GreaterThan(10), GreaterThan(20));
2130 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
2131
2132 // Failed match. The second matcher, which failed, needs to
2133 // explain. Since it doesn't given an explanation, nothing is
2134 // printed.
2135 m = AllOf(GreaterThan(10), Lt(30));
2136 EXPECT_EQ("", Explain(m, 40));
2137
2138 // Failed match. The second matcher, which failed, needs to
2139 // explain.
2140 m = AllOf(GreaterThan(10), GreaterThan(20));
2141 EXPECT_EQ("which is 5 less than 20", Explain(m, 15));
2142}
2143
zhanyong.wan02c15052010-06-09 19:21:30 +00002144// Helper to allow easy testing of AnyOf matchers with num parameters.
2145void AnyOfMatches(int num, const Matcher<int>& m) {
2146 SCOPED_TRACE(Describe(m));
2147 EXPECT_FALSE(m.Matches(0));
2148 for (int i = 1; i <= num; ++i) {
2149 EXPECT_TRUE(m.Matches(i));
2150 }
2151 EXPECT_FALSE(m.Matches(num + 1));
2152}
2153
shiqiane35fdd92008-12-10 05:08:54 +00002154// Tests that AnyOf(m1, ..., mn) matches any value that matches at
2155// least one of the given matchers.
2156TEST(AnyOfTest, MatchesWhenAnyMatches) {
2157 Matcher<int> m;
2158 m = AnyOf(Le(1), Ge(3));
2159 EXPECT_TRUE(m.Matches(1));
2160 EXPECT_TRUE(m.Matches(4));
2161 EXPECT_FALSE(m.Matches(2));
2162
2163 m = AnyOf(Lt(0), Eq(1), Eq(2));
2164 EXPECT_TRUE(m.Matches(-1));
2165 EXPECT_TRUE(m.Matches(1));
2166 EXPECT_TRUE(m.Matches(2));
2167 EXPECT_FALSE(m.Matches(0));
2168
2169 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2170 EXPECT_TRUE(m.Matches(-1));
2171 EXPECT_TRUE(m.Matches(1));
2172 EXPECT_TRUE(m.Matches(2));
2173 EXPECT_TRUE(m.Matches(3));
2174 EXPECT_FALSE(m.Matches(0));
2175
2176 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2177 EXPECT_TRUE(m.Matches(0));
2178 EXPECT_TRUE(m.Matches(11));
2179 EXPECT_TRUE(m.Matches(3));
2180 EXPECT_FALSE(m.Matches(2));
zhanyong.wan02c15052010-06-09 19:21:30 +00002181
2182 // The following tests for varying number of sub-matchers. Due to the way
2183 // the sub-matchers are handled it is enough to test every sub-matcher once
2184 // with sub-matchers using the same matcher type. Varying matcher types are
2185 // checked for above.
2186 AnyOfMatches(2, AnyOf(1, 2));
2187 AnyOfMatches(3, AnyOf(1, 2, 3));
2188 AnyOfMatches(4, AnyOf(1, 2, 3, 4));
2189 AnyOfMatches(5, AnyOf(1, 2, 3, 4, 5));
2190 AnyOfMatches(6, AnyOf(1, 2, 3, 4, 5, 6));
2191 AnyOfMatches(7, AnyOf(1, 2, 3, 4, 5, 6, 7));
2192 AnyOfMatches(8, AnyOf(1, 2, 3, 4, 5, 6, 7, 8));
2193 AnyOfMatches(9, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9));
2194 AnyOfMatches(10, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
shiqiane35fdd92008-12-10 05:08:54 +00002195}
2196
2197// Tests that AnyOf(m1, ..., mn) describes itself properly.
2198TEST(AnyOfTest, CanDescribeSelf) {
2199 Matcher<int> m;
2200 m = AnyOf(Le(1), Ge(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002201 EXPECT_EQ("(is <= 1) or (is >= 3)",
shiqiane35fdd92008-12-10 05:08:54 +00002202 Describe(m));
2203
2204 m = AnyOf(Lt(0), Eq(1), Eq(2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002205 EXPECT_EQ("(is < 0) or "
shiqiane35fdd92008-12-10 05:08:54 +00002206 "((is equal to 1) or (is equal to 2))",
2207 Describe(m));
2208
2209 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
jgm79a367e2012-04-10 16:02:11 +00002210 EXPECT_EQ("((is < 0) or "
2211 "(is equal to 1)) or "
shiqiane35fdd92008-12-10 05:08:54 +00002212 "((is equal to 2) or "
jgm79a367e2012-04-10 16:02:11 +00002213 "(is equal to 3))",
shiqiane35fdd92008-12-10 05:08:54 +00002214 Describe(m));
2215
2216 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
jgm79a367e2012-04-10 16:02:11 +00002217 EXPECT_EQ("((is <= 0) or "
2218 "(is > 10)) or "
shiqiane35fdd92008-12-10 05:08:54 +00002219 "((is equal to 3) or "
2220 "((is equal to 5) or "
jgm79a367e2012-04-10 16:02:11 +00002221 "(is equal to 7)))",
shiqiane35fdd92008-12-10 05:08:54 +00002222 Describe(m));
2223}
2224
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002225// Tests that AnyOf(m1, ..., mn) describes its negation properly.
2226TEST(AnyOfTest, CanDescribeNegation) {
2227 Matcher<int> m;
2228 m = AnyOf(Le(1), Ge(3));
2229 EXPECT_EQ("(isn't <= 1) and (isn't >= 3)",
2230 DescribeNegation(m));
2231
2232 m = AnyOf(Lt(0), Eq(1), Eq(2));
2233 EXPECT_EQ("(isn't < 0) and "
2234 "((isn't equal to 1) and (isn't equal to 2))",
2235 DescribeNegation(m));
2236
2237 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
jgm79a367e2012-04-10 16:02:11 +00002238 EXPECT_EQ("((isn't < 0) and "
2239 "(isn't equal to 1)) and "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002240 "((isn't equal to 2) and "
jgm79a367e2012-04-10 16:02:11 +00002241 "(isn't equal to 3))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002242 DescribeNegation(m));
2243
2244 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
jgm79a367e2012-04-10 16:02:11 +00002245 EXPECT_EQ("((isn't <= 0) and "
2246 "(isn't > 10)) and "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002247 "((isn't equal to 3) and "
2248 "((isn't equal to 5) and "
jgm79a367e2012-04-10 16:02:11 +00002249 "(isn't equal to 7)))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002250 DescribeNegation(m));
2251}
2252
zhanyong.wan18490652009-05-11 18:54:08 +00002253// Tests that monomorphic matchers are safely cast by the AnyOf matcher.
2254TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
2255 // greater_than_5 and less_than_10 are monomorphic matchers.
2256 Matcher<int> greater_than_5 = Gt(5);
2257 Matcher<int> less_than_10 = Lt(10);
2258
2259 Matcher<const int&> m = AnyOf(greater_than_5, less_than_10);
2260 Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10);
2261 Matcher<int&> m3 = AnyOf(greater_than_5, m2);
2262
2263 // Tests that EitherOf works when composing itself.
2264 Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10);
2265 Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);
2266}
2267
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002268TEST(AnyOfTest, ExplainsResult) {
2269 Matcher<int> m;
2270
2271 // Failed match. Both matchers need to explain. The second
2272 // matcher doesn't give an explanation, so only the first matcher's
2273 // explanation is printed.
2274 m = AnyOf(GreaterThan(10), Lt(0));
2275 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
2276
2277 // Failed match. Both matchers need to explain.
2278 m = AnyOf(GreaterThan(10), GreaterThan(20));
2279 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20",
2280 Explain(m, 5));
2281
2282 // Failed match. All matchers need to explain. The second
2283 // matcher doesn't given an explanation.
2284 m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30));
2285 EXPECT_EQ("which is 5 less than 10, and which is 25 less than 30",
2286 Explain(m, 5));
2287
2288 // Failed match. All matchers need to explain.
2289 m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2290 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20, "
2291 "and which is 25 less than 30",
2292 Explain(m, 5));
2293
2294 // Successful match. The first matcher, which succeeded, needs to
2295 // explain.
2296 m = AnyOf(GreaterThan(10), GreaterThan(20));
2297 EXPECT_EQ("which is 5 more than 10", Explain(m, 15));
2298
2299 // Successful match. The second matcher, which succeeded, needs to
2300 // explain. Since it doesn't given an explanation, nothing is
2301 // printed.
2302 m = AnyOf(GreaterThan(10), Lt(30));
2303 EXPECT_EQ("", Explain(m, 0));
2304
2305 // Successful match. The second matcher, which succeeded, needs to
2306 // explain.
2307 m = AnyOf(GreaterThan(30), GreaterThan(20));
2308 EXPECT_EQ("which is 5 more than 20", Explain(m, 25));
2309}
2310
shiqiane35fdd92008-12-10 05:08:54 +00002311// The following predicate function and predicate functor are for
2312// testing the Truly(predicate) matcher.
2313
2314// Returns non-zero if the input is positive. Note that the return
2315// type of this function is not bool. It's OK as Truly() accepts any
2316// unary function or functor whose return type can be implicitly
2317// converted to bool.
2318int IsPositive(double x) {
2319 return x > 0 ? 1 : 0;
2320}
2321
2322// This functor returns true if the input is greater than the given
2323// number.
2324class IsGreaterThan {
2325 public:
2326 explicit IsGreaterThan(int threshold) : threshold_(threshold) {}
2327
2328 bool operator()(int n) const { return n > threshold_; }
zhanyong.wan32de5f52009-12-23 00:13:23 +00002329
shiqiane35fdd92008-12-10 05:08:54 +00002330 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002331 int threshold_;
shiqiane35fdd92008-12-10 05:08:54 +00002332};
2333
2334// For testing Truly().
2335const int foo = 0;
2336
2337// This predicate returns true iff the argument references foo and has
2338// a zero value.
2339bool ReferencesFooAndIsZero(const int& n) {
2340 return (&n == &foo) && (n == 0);
2341}
2342
2343// Tests that Truly(predicate) matches what satisfies the given
2344// predicate.
2345TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {
2346 Matcher<double> m = Truly(IsPositive);
2347 EXPECT_TRUE(m.Matches(2.0));
2348 EXPECT_FALSE(m.Matches(-1.5));
2349}
2350
2351// Tests that Truly(predicate_functor) works too.
2352TEST(TrulyTest, CanBeUsedWithFunctor) {
2353 Matcher<int> m = Truly(IsGreaterThan(5));
2354 EXPECT_TRUE(m.Matches(6));
2355 EXPECT_FALSE(m.Matches(4));
2356}
2357
zhanyong.wan8d3dc0c2011-04-14 19:37:06 +00002358// A class that can be implicitly converted to bool.
2359class ConvertibleToBool {
2360 public:
2361 explicit ConvertibleToBool(int number) : number_(number) {}
2362 operator bool() const { return number_ != 0; }
2363
2364 private:
2365 int number_;
2366};
2367
2368ConvertibleToBool IsNotZero(int number) {
2369 return ConvertibleToBool(number);
2370}
2371
2372// Tests that the predicate used in Truly() may return a class that's
2373// implicitly convertible to bool, even when the class has no
2374// operator!().
2375TEST(TrulyTest, PredicateCanReturnAClassConvertibleToBool) {
2376 Matcher<int> m = Truly(IsNotZero);
2377 EXPECT_TRUE(m.Matches(1));
2378 EXPECT_FALSE(m.Matches(0));
2379}
2380
shiqiane35fdd92008-12-10 05:08:54 +00002381// Tests that Truly(predicate) can describe itself properly.
2382TEST(TrulyTest, CanDescribeSelf) {
2383 Matcher<double> m = Truly(IsPositive);
2384 EXPECT_EQ("satisfies the given predicate",
2385 Describe(m));
2386}
2387
2388// Tests that Truly(predicate) works when the matcher takes its
2389// argument by reference.
2390TEST(TrulyTest, WorksForByRefArguments) {
2391 Matcher<const int&> m = Truly(ReferencesFooAndIsZero);
2392 EXPECT_TRUE(m.Matches(foo));
2393 int n = 0;
2394 EXPECT_FALSE(m.Matches(n));
2395}
2396
2397// Tests that Matches(m) is a predicate satisfied by whatever that
2398// matches matcher m.
2399TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {
2400 EXPECT_TRUE(Matches(Ge(0))(1));
2401 EXPECT_FALSE(Matches(Eq('a'))('b'));
2402}
2403
2404// Tests that Matches(m) works when the matcher takes its argument by
2405// reference.
2406TEST(MatchesTest, WorksOnByRefArguments) {
2407 int m = 0, n = 0;
2408 EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n));
2409 EXPECT_FALSE(Matches(Ref(m))(n));
2410}
2411
2412// Tests that a Matcher on non-reference type can be used in
2413// Matches().
2414TEST(MatchesTest, WorksWithMatcherOnNonRefType) {
2415 Matcher<int> eq5 = Eq(5);
2416 EXPECT_TRUE(Matches(eq5)(5));
2417 EXPECT_FALSE(Matches(eq5)(2));
2418}
2419
zhanyong.wanb8243162009-06-04 05:48:20 +00002420// Tests Value(value, matcher). Since Value() is a simple wrapper for
2421// Matches(), which has been tested already, we don't spend a lot of
2422// effort on testing Value().
2423TEST(ValueTest, WorksWithPolymorphicMatcher) {
2424 EXPECT_TRUE(Value("hi", StartsWith("h")));
2425 EXPECT_FALSE(Value(5, Gt(10)));
2426}
2427
2428TEST(ValueTest, WorksWithMonomorphicMatcher) {
2429 const Matcher<int> is_zero = Eq(0);
2430 EXPECT_TRUE(Value(0, is_zero));
2431 EXPECT_FALSE(Value('a', is_zero));
2432
2433 int n = 0;
2434 const Matcher<const int&> ref_n = Ref(n);
2435 EXPECT_TRUE(Value(n, ref_n));
2436 EXPECT_FALSE(Value(1, ref_n));
2437}
2438
zhanyong.wana862f1d2010-03-15 21:23:04 +00002439TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) {
zhanyong.wan34b034c2010-03-05 21:23:23 +00002440 StringMatchResultListener listener1;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002441 EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002442 EXPECT_EQ("% 2 == 0", listener1.str());
2443
2444 StringMatchResultListener listener2;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002445 EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002446 EXPECT_EQ("", listener2.str());
2447}
2448
zhanyong.wana862f1d2010-03-15 21:23:04 +00002449TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) {
zhanyong.wan34b034c2010-03-05 21:23:23 +00002450 const Matcher<int> is_even = PolymorphicIsEven();
2451 StringMatchResultListener listener1;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002452 EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002453 EXPECT_EQ("% 2 == 0", listener1.str());
2454
2455 const Matcher<const double&> is_zero = Eq(0);
2456 StringMatchResultListener listener2;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002457 EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002458 EXPECT_EQ("", listener2.str());
2459}
2460
zhanyong.wana862f1d2010-03-15 21:23:04 +00002461MATCHER_P(Really, inner_matcher, "") {
2462 return ExplainMatchResult(inner_matcher, arg, result_listener);
2463}
2464
2465TEST(ExplainMatchResultTest, WorksInsideMATCHER) {
2466 EXPECT_THAT(0, Really(Eq(0)));
2467}
2468
zhanyong.wanbf550852009-06-09 06:09:53 +00002469TEST(AllArgsTest, WorksForTuple) {
2470 EXPECT_THAT(make_tuple(1, 2L), AllArgs(Lt()));
2471 EXPECT_THAT(make_tuple(2L, 1), Not(AllArgs(Lt())));
2472}
2473
2474TEST(AllArgsTest, WorksForNonTuple) {
2475 EXPECT_THAT(42, AllArgs(Gt(0)));
2476 EXPECT_THAT('a', Not(AllArgs(Eq('b'))));
2477}
2478
2479class AllArgsHelper {
2480 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002481 AllArgsHelper() {}
2482
zhanyong.wanbf550852009-06-09 06:09:53 +00002483 MOCK_METHOD2(Helper, int(char x, int y));
zhanyong.wan32de5f52009-12-23 00:13:23 +00002484
2485 private:
2486 GTEST_DISALLOW_COPY_AND_ASSIGN_(AllArgsHelper);
zhanyong.wanbf550852009-06-09 06:09:53 +00002487};
2488
2489TEST(AllArgsTest, WorksInWithClause) {
2490 AllArgsHelper helper;
2491 ON_CALL(helper, Helper(_, _))
2492 .With(AllArgs(Lt()))
2493 .WillByDefault(Return(1));
2494 EXPECT_CALL(helper, Helper(_, _));
2495 EXPECT_CALL(helper, Helper(_, _))
2496 .With(AllArgs(Gt()))
2497 .WillOnce(Return(2));
2498
2499 EXPECT_EQ(1, helper.Helper('\1', 2));
2500 EXPECT_EQ(2, helper.Helper('a', 1));
2501}
2502
shiqiane35fdd92008-12-10 05:08:54 +00002503// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2504// matches the matcher.
2505TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {
2506 ASSERT_THAT(5, Ge(2)) << "This should succeed.";
2507 ASSERT_THAT("Foo", EndsWith("oo"));
2508 EXPECT_THAT(2, AllOf(Le(7), Ge(0))) << "This should succeed too.";
2509 EXPECT_THAT("Hello", StartsWith("Hell"));
2510}
2511
2512// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2513// doesn't match the matcher.
2514TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {
2515 // 'n' must be static as it is used in an EXPECT_FATAL_FAILURE(),
2516 // which cannot reference auto variables.
zhanyong.wan736baa82010-09-27 17:44:16 +00002517 static unsigned short n; // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00002518 n = 5;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002519
2520 // VC++ prior to version 8.0 SP1 has a bug where it will not see any
2521 // functions declared in the namespace scope from within nested classes.
2522 // EXPECT/ASSERT_(NON)FATAL_FAILURE macros use nested classes so that all
2523 // namespace-level functions invoked inside them need to be explicitly
2524 // resolved.
2525 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Gt(10)),
shiqiane35fdd92008-12-10 05:08:54 +00002526 "Value of: n\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002527 "Expected: is > 10\n"
zhanyong.wan736baa82010-09-27 17:44:16 +00002528 " Actual: 5" + OfType("unsigned short"));
shiqiane35fdd92008-12-10 05:08:54 +00002529 n = 0;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002530 EXPECT_NONFATAL_FAILURE(
2531 EXPECT_THAT(n, ::testing::AllOf(::testing::Le(7), ::testing::Ge(5))),
2532 "Value of: n\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002533 "Expected: (is <= 7) and (is >= 5)\n"
zhanyong.wan736baa82010-09-27 17:44:16 +00002534 " Actual: 0" + OfType("unsigned short"));
shiqiane35fdd92008-12-10 05:08:54 +00002535}
2536
2537// Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument
2538// has a reference type.
2539TEST(MatcherAssertionTest, WorksForByRefArguments) {
2540 // We use a static variable here as EXPECT_FATAL_FAILURE() cannot
2541 // reference auto variables.
2542 static int n;
2543 n = 0;
2544 EXPECT_THAT(n, AllOf(Le(7), Ref(n)));
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002545 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
shiqiane35fdd92008-12-10 05:08:54 +00002546 "Value of: n\n"
2547 "Expected: does not reference the variable @");
2548 // Tests the "Actual" part.
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002549 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
zhanyong.wan736baa82010-09-27 17:44:16 +00002550 "Actual: 0" + OfType("int") + ", which is located @");
shiqiane35fdd92008-12-10 05:08:54 +00002551}
2552
zhanyong.wan95b12332009-09-25 18:55:50 +00002553#if !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002554// Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is
2555// monomorphic.
zhanyong.wan95b12332009-09-25 18:55:50 +00002556
2557// ASSERT_THAT("hello", starts_with_he) fails to compile with Nokia's
2558// Symbian compiler: it tries to compile
2559// template<T, U> class MatcherCastImpl { ...
zhanyong.wandb22c222010-01-28 21:52:29 +00002560// virtual bool MatchAndExplain(T x, ...) const {
2561// return source_matcher_.MatchAndExplain(static_cast<U>(x), ...);
zhanyong.wan95b12332009-09-25 18:55:50 +00002562// with U == string and T == const char*
2563// With ASSERT_THAT("hello"...) changed to ASSERT_THAT(string("hello") ... )
2564// the compiler silently crashes with no output.
2565// If MatcherCastImpl is changed to use U(x) instead of static_cast<U>(x)
2566// the code compiles but the converted string is bogus.
shiqiane35fdd92008-12-10 05:08:54 +00002567TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
2568 Matcher<const char*> starts_with_he = StartsWith("he");
2569 ASSERT_THAT("hello", starts_with_he);
2570
2571 Matcher<const string&> ends_with_ok = EndsWith("ok");
2572 ASSERT_THAT("book", ends_with_ok);
zhanyong.wan736baa82010-09-27 17:44:16 +00002573 const string bad = "bad";
2574 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(bad, ends_with_ok),
2575 "Value of: bad\n"
2576 "Expected: ends with \"ok\"\n"
2577 " Actual: \"bad\"");
shiqiane35fdd92008-12-10 05:08:54 +00002578 Matcher<int> is_greater_than_5 = Gt(5);
2579 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5),
2580 "Value of: 5\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002581 "Expected: is > 5\n"
zhanyong.wan736baa82010-09-27 17:44:16 +00002582 " Actual: 5" + OfType("int"));
shiqiane35fdd92008-12-10 05:08:54 +00002583}
zhanyong.wan95b12332009-09-25 18:55:50 +00002584#endif // !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002585
2586// Tests floating-point matchers.
2587template <typename RawType>
2588class FloatingPointTest : public testing::Test {
2589 protected:
2590 typedef typename testing::internal::FloatingPoint<RawType> Floating;
2591 typedef typename Floating::Bits Bits;
2592
2593 virtual void SetUp() {
2594 const size_t max_ulps = Floating::kMaxUlps;
2595
2596 // The bits that represent 0.0.
2597 const Bits zero_bits = Floating(0).bits();
2598
2599 // Makes some numbers close to 0.0.
2600 close_to_positive_zero_ = Floating::ReinterpretBits(zero_bits + max_ulps/2);
2601 close_to_negative_zero_ = -Floating::ReinterpretBits(
2602 zero_bits + max_ulps - max_ulps/2);
2603 further_from_negative_zero_ = -Floating::ReinterpretBits(
2604 zero_bits + max_ulps + 1 - max_ulps/2);
2605
2606 // The bits that represent 1.0.
2607 const Bits one_bits = Floating(1).bits();
2608
2609 // Makes some numbers close to 1.0.
2610 close_to_one_ = Floating::ReinterpretBits(one_bits + max_ulps);
2611 further_from_one_ = Floating::ReinterpretBits(one_bits + max_ulps + 1);
2612
2613 // +infinity.
2614 infinity_ = Floating::Infinity();
2615
2616 // The bits that represent +infinity.
2617 const Bits infinity_bits = Floating(infinity_).bits();
2618
2619 // Makes some numbers close to infinity.
2620 close_to_infinity_ = Floating::ReinterpretBits(infinity_bits - max_ulps);
2621 further_from_infinity_ = Floating::ReinterpretBits(
2622 infinity_bits - max_ulps - 1);
2623
2624 // Makes some NAN's.
2625 nan1_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 1);
2626 nan2_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 200);
2627 }
2628
2629 void TestSize() {
2630 EXPECT_EQ(sizeof(RawType), sizeof(Bits));
2631 }
2632
2633 // A battery of tests for FloatingEqMatcher::Matches.
2634 // matcher_maker is a pointer to a function which creates a FloatingEqMatcher.
2635 void TestMatches(
2636 testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) {
2637 Matcher<RawType> m1 = matcher_maker(0.0);
2638 EXPECT_TRUE(m1.Matches(-0.0));
2639 EXPECT_TRUE(m1.Matches(close_to_positive_zero_));
2640 EXPECT_TRUE(m1.Matches(close_to_negative_zero_));
2641 EXPECT_FALSE(m1.Matches(1.0));
2642
2643 Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_);
2644 EXPECT_FALSE(m2.Matches(further_from_negative_zero_));
2645
2646 Matcher<RawType> m3 = matcher_maker(1.0);
2647 EXPECT_TRUE(m3.Matches(close_to_one_));
2648 EXPECT_FALSE(m3.Matches(further_from_one_));
2649
2650 // Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above.
2651 EXPECT_FALSE(m3.Matches(0.0));
2652
2653 Matcher<RawType> m4 = matcher_maker(-infinity_);
2654 EXPECT_TRUE(m4.Matches(-close_to_infinity_));
2655
2656 Matcher<RawType> m5 = matcher_maker(infinity_);
2657 EXPECT_TRUE(m5.Matches(close_to_infinity_));
2658
2659 // This is interesting as the representations of infinity_ and nan1_
2660 // are only 1 DLP apart.
2661 EXPECT_FALSE(m5.Matches(nan1_));
2662
2663 // matcher_maker can produce a Matcher<const RawType&>, which is needed in
2664 // some cases.
2665 Matcher<const RawType&> m6 = matcher_maker(0.0);
2666 EXPECT_TRUE(m6.Matches(-0.0));
2667 EXPECT_TRUE(m6.Matches(close_to_positive_zero_));
2668 EXPECT_FALSE(m6.Matches(1.0));
2669
2670 // matcher_maker can produce a Matcher<RawType&>, which is needed in some
2671 // cases.
2672 Matcher<RawType&> m7 = matcher_maker(0.0);
2673 RawType x = 0.0;
2674 EXPECT_TRUE(m7.Matches(x));
2675 x = 0.01f;
2676 EXPECT_FALSE(m7.Matches(x));
2677 }
2678
2679 // Pre-calculated numbers to be used by the tests.
2680
2681 static RawType close_to_positive_zero_;
2682 static RawType close_to_negative_zero_;
2683 static RawType further_from_negative_zero_;
2684
2685 static RawType close_to_one_;
2686 static RawType further_from_one_;
2687
2688 static RawType infinity_;
2689 static RawType close_to_infinity_;
2690 static RawType further_from_infinity_;
2691
2692 static RawType nan1_;
2693 static RawType nan2_;
2694};
2695
2696template <typename RawType>
2697RawType FloatingPointTest<RawType>::close_to_positive_zero_;
2698
2699template <typename RawType>
2700RawType FloatingPointTest<RawType>::close_to_negative_zero_;
2701
2702template <typename RawType>
2703RawType FloatingPointTest<RawType>::further_from_negative_zero_;
2704
2705template <typename RawType>
2706RawType FloatingPointTest<RawType>::close_to_one_;
2707
2708template <typename RawType>
2709RawType FloatingPointTest<RawType>::further_from_one_;
2710
2711template <typename RawType>
2712RawType FloatingPointTest<RawType>::infinity_;
2713
2714template <typename RawType>
2715RawType FloatingPointTest<RawType>::close_to_infinity_;
2716
2717template <typename RawType>
2718RawType FloatingPointTest<RawType>::further_from_infinity_;
2719
2720template <typename RawType>
2721RawType FloatingPointTest<RawType>::nan1_;
2722
2723template <typename RawType>
2724RawType FloatingPointTest<RawType>::nan2_;
2725
2726// Instantiate FloatingPointTest for testing floats.
2727typedef FloatingPointTest<float> FloatTest;
2728
2729TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) {
2730 TestMatches(&FloatEq);
2731}
2732
2733TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
2734 TestMatches(&NanSensitiveFloatEq);
2735}
2736
2737TEST_F(FloatTest, FloatEqCannotMatchNaN) {
2738 // FloatEq never matches NaN.
2739 Matcher<float> m = FloatEq(nan1_);
2740 EXPECT_FALSE(m.Matches(nan1_));
2741 EXPECT_FALSE(m.Matches(nan2_));
2742 EXPECT_FALSE(m.Matches(1.0));
2743}
2744
2745TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {
2746 // NanSensitiveFloatEq will match NaN.
2747 Matcher<float> m = NanSensitiveFloatEq(nan1_);
2748 EXPECT_TRUE(m.Matches(nan1_));
2749 EXPECT_TRUE(m.Matches(nan2_));
2750 EXPECT_FALSE(m.Matches(1.0));
2751}
2752
2753TEST_F(FloatTest, FloatEqCanDescribeSelf) {
2754 Matcher<float> m1 = FloatEq(2.0f);
2755 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002756 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002757
2758 Matcher<float> m2 = FloatEq(0.5f);
2759 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002760 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002761
2762 Matcher<float> m3 = FloatEq(nan1_);
2763 EXPECT_EQ("never matches", Describe(m3));
2764 EXPECT_EQ("is anything", DescribeNegation(m3));
2765}
2766
2767TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {
2768 Matcher<float> m1 = NanSensitiveFloatEq(2.0f);
2769 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002770 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002771
2772 Matcher<float> m2 = NanSensitiveFloatEq(0.5f);
2773 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002774 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002775
2776 Matcher<float> m3 = NanSensitiveFloatEq(nan1_);
2777 EXPECT_EQ("is NaN", Describe(m3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002778 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
shiqiane35fdd92008-12-10 05:08:54 +00002779}
2780
2781// Instantiate FloatingPointTest for testing doubles.
2782typedef FloatingPointTest<double> DoubleTest;
2783
2784TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
2785 TestMatches(&DoubleEq);
2786}
2787
2788TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
2789 TestMatches(&NanSensitiveDoubleEq);
2790}
2791
2792TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {
2793 // DoubleEq never matches NaN.
2794 Matcher<double> m = DoubleEq(nan1_);
2795 EXPECT_FALSE(m.Matches(nan1_));
2796 EXPECT_FALSE(m.Matches(nan2_));
2797 EXPECT_FALSE(m.Matches(1.0));
2798}
2799
2800TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {
2801 // NanSensitiveDoubleEq will match NaN.
2802 Matcher<double> m = NanSensitiveDoubleEq(nan1_);
2803 EXPECT_TRUE(m.Matches(nan1_));
2804 EXPECT_TRUE(m.Matches(nan2_));
2805 EXPECT_FALSE(m.Matches(1.0));
2806}
2807
2808TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {
2809 Matcher<double> m1 = DoubleEq(2.0);
2810 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002811 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002812
2813 Matcher<double> m2 = DoubleEq(0.5);
2814 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002815 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002816
2817 Matcher<double> m3 = DoubleEq(nan1_);
2818 EXPECT_EQ("never matches", Describe(m3));
2819 EXPECT_EQ("is anything", DescribeNegation(m3));
2820}
2821
2822TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {
2823 Matcher<double> m1 = NanSensitiveDoubleEq(2.0);
2824 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002825 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002826
2827 Matcher<double> m2 = NanSensitiveDoubleEq(0.5);
2828 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002829 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002830
2831 Matcher<double> m3 = NanSensitiveDoubleEq(nan1_);
2832 EXPECT_EQ("is NaN", Describe(m3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002833 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
shiqiane35fdd92008-12-10 05:08:54 +00002834}
2835
2836TEST(PointeeTest, RawPointer) {
2837 const Matcher<int*> m = Pointee(Ge(0));
2838
2839 int n = 1;
2840 EXPECT_TRUE(m.Matches(&n));
2841 n = -1;
2842 EXPECT_FALSE(m.Matches(&n));
2843 EXPECT_FALSE(m.Matches(NULL));
2844}
2845
2846TEST(PointeeTest, RawPointerToConst) {
2847 const Matcher<const double*> m = Pointee(Ge(0));
2848
2849 double x = 1;
2850 EXPECT_TRUE(m.Matches(&x));
2851 x = -1;
2852 EXPECT_FALSE(m.Matches(&x));
2853 EXPECT_FALSE(m.Matches(NULL));
2854}
2855
2856TEST(PointeeTest, ReferenceToConstRawPointer) {
2857 const Matcher<int* const &> m = Pointee(Ge(0));
2858
2859 int n = 1;
2860 EXPECT_TRUE(m.Matches(&n));
2861 n = -1;
2862 EXPECT_FALSE(m.Matches(&n));
2863 EXPECT_FALSE(m.Matches(NULL));
2864}
2865
2866TEST(PointeeTest, ReferenceToNonConstRawPointer) {
2867 const Matcher<double* &> m = Pointee(Ge(0));
2868
2869 double x = 1.0;
2870 double* p = &x;
2871 EXPECT_TRUE(m.Matches(p));
2872 x = -1;
2873 EXPECT_FALSE(m.Matches(p));
2874 p = NULL;
2875 EXPECT_FALSE(m.Matches(p));
2876}
2877
vladlosevada23472012-08-14 15:38:49 +00002878// Minimal const-propagating pointer.
2879template <typename T>
2880class ConstPropagatingPtr {
2881 public:
2882 typedef T element_type;
2883
2884 ConstPropagatingPtr() : val_() {}
2885 explicit ConstPropagatingPtr(T* t) : val_(t) {}
2886 ConstPropagatingPtr(const ConstPropagatingPtr& other) : val_(other.val_) {}
2887
2888 T* get() { return val_; }
2889 T& operator*() { return *val_; }
2890 // Most smart pointers return non-const T* and T& from the next methods.
2891 const T* get() const { return val_; }
2892 const T& operator*() const { return *val_; }
2893
2894 private:
2895 T* val_;
2896};
2897
2898TEST(PointeeTest, WorksWithConstPropagatingPointers) {
2899 const Matcher< ConstPropagatingPtr<int> > m = Pointee(Lt(5));
2900 int three = 3;
2901 const ConstPropagatingPtr<int> co(&three);
2902 ConstPropagatingPtr<int> o(&three);
2903 EXPECT_TRUE(m.Matches(o));
2904 EXPECT_TRUE(m.Matches(co));
2905 *o = 6;
2906 EXPECT_FALSE(m.Matches(o));
2907 EXPECT_FALSE(m.Matches(ConstPropagatingPtr<int>()));
2908}
2909
shiqiane35fdd92008-12-10 05:08:54 +00002910TEST(PointeeTest, NeverMatchesNull) {
2911 const Matcher<const char*> m = Pointee(_);
2912 EXPECT_FALSE(m.Matches(NULL));
2913}
2914
2915// Tests that we can write Pointee(value) instead of Pointee(Eq(value)).
2916TEST(PointeeTest, MatchesAgainstAValue) {
2917 const Matcher<int*> m = Pointee(5);
2918
2919 int n = 5;
2920 EXPECT_TRUE(m.Matches(&n));
2921 n = -1;
2922 EXPECT_FALSE(m.Matches(&n));
2923 EXPECT_FALSE(m.Matches(NULL));
2924}
2925
2926TEST(PointeeTest, CanDescribeSelf) {
2927 const Matcher<int*> m = Pointee(Gt(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002928 EXPECT_EQ("points to a value that is > 3", Describe(m));
2929 EXPECT_EQ("does not point to a value that is > 3",
shiqiane35fdd92008-12-10 05:08:54 +00002930 DescribeNegation(m));
2931}
2932
shiqiane35fdd92008-12-10 05:08:54 +00002933TEST(PointeeTest, CanExplainMatchResult) {
2934 const Matcher<const string*> m = Pointee(StartsWith("Hi"));
2935
2936 EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL)));
2937
zhanyong.wan736baa82010-09-27 17:44:16 +00002938 const Matcher<long*> m2 = Pointee(GreaterThan(1)); // NOLINT
2939 long n = 3; // NOLINT
2940 EXPECT_EQ("which points to 3" + OfType("long") + ", which is 2 more than 1",
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002941 Explain(m2, &n));
2942}
2943
2944TEST(PointeeTest, AlwaysExplainsPointee) {
2945 const Matcher<int*> m = Pointee(0);
2946 int n = 42;
zhanyong.wan736baa82010-09-27 17:44:16 +00002947 EXPECT_EQ("which points to 42" + OfType("int"), Explain(m, &n));
shiqiane35fdd92008-12-10 05:08:54 +00002948}
2949
2950// An uncopyable class.
2951class Uncopyable {
2952 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002953 explicit Uncopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00002954
2955 int value() const { return value_; }
2956 private:
2957 const int value_;
2958 GTEST_DISALLOW_COPY_AND_ASSIGN_(Uncopyable);
2959};
2960
2961// Returns true iff x.value() is positive.
2962bool ValueIsPositive(const Uncopyable& x) { return x.value() > 0; }
2963
2964// A user-defined struct for testing Field().
2965struct AStruct {
2966 AStruct() : x(0), y(1.0), z(5), p(NULL) {}
2967 AStruct(const AStruct& rhs)
2968 : x(rhs.x), y(rhs.y), z(rhs.z.value()), p(rhs.p) {}
2969
2970 int x; // A non-const field.
2971 const double y; // A const field.
2972 Uncopyable z; // An uncopyable field.
2973 const char* p; // A pointer field.
zhanyong.wan32de5f52009-12-23 00:13:23 +00002974
2975 private:
2976 GTEST_DISALLOW_ASSIGN_(AStruct);
shiqiane35fdd92008-12-10 05:08:54 +00002977};
2978
2979// A derived struct for testing Field().
2980struct DerivedStruct : public AStruct {
2981 char ch;
zhanyong.wan32de5f52009-12-23 00:13:23 +00002982
2983 private:
2984 GTEST_DISALLOW_ASSIGN_(DerivedStruct);
shiqiane35fdd92008-12-10 05:08:54 +00002985};
2986
2987// Tests that Field(&Foo::field, ...) works when field is non-const.
2988TEST(FieldTest, WorksForNonConstField) {
2989 Matcher<AStruct> m = Field(&AStruct::x, Ge(0));
2990
2991 AStruct a;
2992 EXPECT_TRUE(m.Matches(a));
2993 a.x = -1;
2994 EXPECT_FALSE(m.Matches(a));
2995}
2996
2997// Tests that Field(&Foo::field, ...) works when field is const.
2998TEST(FieldTest, WorksForConstField) {
2999 AStruct a;
3000
3001 Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0));
3002 EXPECT_TRUE(m.Matches(a));
3003 m = Field(&AStruct::y, Le(0.0));
3004 EXPECT_FALSE(m.Matches(a));
3005}
3006
3007// Tests that Field(&Foo::field, ...) works when field is not copyable.
3008TEST(FieldTest, WorksForUncopyableField) {
3009 AStruct a;
3010
3011 Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive));
3012 EXPECT_TRUE(m.Matches(a));
3013 m = Field(&AStruct::z, Not(Truly(ValueIsPositive)));
3014 EXPECT_FALSE(m.Matches(a));
3015}
3016
3017// Tests that Field(&Foo::field, ...) works when field is a pointer.
3018TEST(FieldTest, WorksForPointerField) {
3019 // Matching against NULL.
3020 Matcher<AStruct> m = Field(&AStruct::p, static_cast<const char*>(NULL));
3021 AStruct a;
3022 EXPECT_TRUE(m.Matches(a));
3023 a.p = "hi";
3024 EXPECT_FALSE(m.Matches(a));
3025
3026 // Matching a pointer that is not NULL.
3027 m = Field(&AStruct::p, StartsWith("hi"));
3028 a.p = "hill";
3029 EXPECT_TRUE(m.Matches(a));
3030 a.p = "hole";
3031 EXPECT_FALSE(m.Matches(a));
3032}
3033
3034// Tests that Field() works when the object is passed by reference.
3035TEST(FieldTest, WorksForByRefArgument) {
3036 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
3037
3038 AStruct a;
3039 EXPECT_TRUE(m.Matches(a));
3040 a.x = -1;
3041 EXPECT_FALSE(m.Matches(a));
3042}
3043
3044// Tests that Field(&Foo::field, ...) works when the argument's type
3045// is a sub-type of Foo.
3046TEST(FieldTest, WorksForArgumentOfSubType) {
3047 // Note that the matcher expects DerivedStruct but we say AStruct
3048 // inside Field().
3049 Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0));
3050
3051 DerivedStruct d;
3052 EXPECT_TRUE(m.Matches(d));
3053 d.x = -1;
3054 EXPECT_FALSE(m.Matches(d));
3055}
3056
3057// Tests that Field(&Foo::field, m) works when field's type and m's
3058// argument type are compatible but not the same.
3059TEST(FieldTest, WorksForCompatibleMatcherType) {
3060 // The field is an int, but the inner matcher expects a signed char.
3061 Matcher<const AStruct&> m = Field(&AStruct::x,
3062 Matcher<signed char>(Ge(0)));
3063
3064 AStruct a;
3065 EXPECT_TRUE(m.Matches(a));
3066 a.x = -1;
3067 EXPECT_FALSE(m.Matches(a));
3068}
3069
3070// Tests that Field() can describe itself.
3071TEST(FieldTest, CanDescribeSelf) {
3072 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
3073
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003074 EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
3075 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003076}
3077
3078// Tests that Field() can explain the match result.
3079TEST(FieldTest, CanExplainMatchResult) {
3080 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
3081
3082 AStruct a;
3083 a.x = 1;
zhanyong.wan736baa82010-09-27 17:44:16 +00003084 EXPECT_EQ("whose given field is 1" + OfType("int"), Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003085
3086 m = Field(&AStruct::x, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003087 EXPECT_EQ(
3088 "whose given field is 1" + OfType("int") + ", which is 1 more than 0",
3089 Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003090}
3091
3092// Tests that Field() works when the argument is a pointer to const.
3093TEST(FieldForPointerTest, WorksForPointerToConst) {
3094 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
3095
3096 AStruct a;
3097 EXPECT_TRUE(m.Matches(&a));
3098 a.x = -1;
3099 EXPECT_FALSE(m.Matches(&a));
3100}
3101
3102// Tests that Field() works when the argument is a pointer to non-const.
3103TEST(FieldForPointerTest, WorksForPointerToNonConst) {
3104 Matcher<AStruct*> m = Field(&AStruct::x, Ge(0));
3105
3106 AStruct a;
3107 EXPECT_TRUE(m.Matches(&a));
3108 a.x = -1;
3109 EXPECT_FALSE(m.Matches(&a));
3110}
3111
zhanyong.wan6953a722010-01-13 05:15:07 +00003112// Tests that Field() works when the argument is a reference to a const pointer.
3113TEST(FieldForPointerTest, WorksForReferenceToConstPointer) {
3114 Matcher<AStruct* const&> m = Field(&AStruct::x, Ge(0));
3115
3116 AStruct a;
3117 EXPECT_TRUE(m.Matches(&a));
3118 a.x = -1;
3119 EXPECT_FALSE(m.Matches(&a));
3120}
3121
shiqiane35fdd92008-12-10 05:08:54 +00003122// Tests that Field() does not match the NULL pointer.
3123TEST(FieldForPointerTest, DoesNotMatchNull) {
3124 Matcher<const AStruct*> m = Field(&AStruct::x, _);
3125 EXPECT_FALSE(m.Matches(NULL));
3126}
3127
3128// Tests that Field(&Foo::field, ...) works when the argument's type
3129// is a sub-type of const Foo*.
3130TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
3131 // Note that the matcher expects DerivedStruct but we say AStruct
3132 // inside Field().
3133 Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0));
3134
3135 DerivedStruct d;
3136 EXPECT_TRUE(m.Matches(&d));
3137 d.x = -1;
3138 EXPECT_FALSE(m.Matches(&d));
3139}
3140
3141// Tests that Field() can describe itself when used to match a pointer.
3142TEST(FieldForPointerTest, CanDescribeSelf) {
3143 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
3144
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003145 EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
3146 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003147}
3148
3149// Tests that Field() can explain the result of matching a pointer.
3150TEST(FieldForPointerTest, CanExplainMatchResult) {
3151 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
3152
3153 AStruct a;
3154 a.x = 1;
3155 EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(NULL)));
zhanyong.wan736baa82010-09-27 17:44:16 +00003156 EXPECT_EQ("which points to an object whose given field is 1" + OfType("int"),
3157 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003158
3159 m = Field(&AStruct::x, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003160 EXPECT_EQ("which points to an object whose given field is 1" + OfType("int") +
3161 ", which is 1 more than 0", Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003162}
3163
3164// A user-defined class for testing Property().
3165class AClass {
3166 public:
3167 AClass() : n_(0) {}
3168
3169 // A getter that returns a non-reference.
3170 int n() const { return n_; }
3171
3172 void set_n(int new_n) { n_ = new_n; }
3173
3174 // A getter that returns a reference to const.
3175 const string& s() const { return s_; }
3176
3177 void set_s(const string& new_s) { s_ = new_s; }
3178
3179 // A getter that returns a reference to non-const.
3180 double& x() const { return x_; }
3181 private:
3182 int n_;
3183 string s_;
3184
3185 static double x_;
3186};
3187
3188double AClass::x_ = 0.0;
3189
3190// A derived class for testing Property().
3191class DerivedClass : public AClass {
3192 private:
3193 int k_;
3194};
3195
3196// Tests that Property(&Foo::property, ...) works when property()
3197// returns a non-reference.
3198TEST(PropertyTest, WorksForNonReferenceProperty) {
3199 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3200
3201 AClass a;
3202 a.set_n(1);
3203 EXPECT_TRUE(m.Matches(a));
3204
3205 a.set_n(-1);
3206 EXPECT_FALSE(m.Matches(a));
3207}
3208
3209// Tests that Property(&Foo::property, ...) works when property()
3210// returns a reference to const.
3211TEST(PropertyTest, WorksForReferenceToConstProperty) {
3212 Matcher<const AClass&> m = Property(&AClass::s, StartsWith("hi"));
3213
3214 AClass a;
3215 a.set_s("hill");
3216 EXPECT_TRUE(m.Matches(a));
3217
3218 a.set_s("hole");
3219 EXPECT_FALSE(m.Matches(a));
3220}
3221
3222// Tests that Property(&Foo::property, ...) works when property()
3223// returns a reference to non-const.
3224TEST(PropertyTest, WorksForReferenceToNonConstProperty) {
3225 double x = 0.0;
3226 AClass a;
3227
3228 Matcher<const AClass&> m = Property(&AClass::x, Ref(x));
3229 EXPECT_FALSE(m.Matches(a));
3230
3231 m = Property(&AClass::x, Not(Ref(x)));
3232 EXPECT_TRUE(m.Matches(a));
3233}
3234
3235// Tests that Property(&Foo::property, ...) works when the argument is
3236// passed by value.
3237TEST(PropertyTest, WorksForByValueArgument) {
3238 Matcher<AClass> m = Property(&AClass::s, StartsWith("hi"));
3239
3240 AClass a;
3241 a.set_s("hill");
3242 EXPECT_TRUE(m.Matches(a));
3243
3244 a.set_s("hole");
3245 EXPECT_FALSE(m.Matches(a));
3246}
3247
3248// Tests that Property(&Foo::property, ...) works when the argument's
3249// type is a sub-type of Foo.
3250TEST(PropertyTest, WorksForArgumentOfSubType) {
3251 // The matcher expects a DerivedClass, but inside the Property() we
3252 // say AClass.
3253 Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0));
3254
3255 DerivedClass d;
3256 d.set_n(1);
3257 EXPECT_TRUE(m.Matches(d));
3258
3259 d.set_n(-1);
3260 EXPECT_FALSE(m.Matches(d));
3261}
3262
3263// Tests that Property(&Foo::property, m) works when property()'s type
3264// and m's argument type are compatible but different.
3265TEST(PropertyTest, WorksForCompatibleMatcherType) {
3266 // n() returns an int but the inner matcher expects a signed char.
3267 Matcher<const AClass&> m = Property(&AClass::n,
3268 Matcher<signed char>(Ge(0)));
3269
3270 AClass a;
3271 EXPECT_TRUE(m.Matches(a));
3272 a.set_n(-1);
3273 EXPECT_FALSE(m.Matches(a));
3274}
3275
3276// Tests that Property() can describe itself.
3277TEST(PropertyTest, CanDescribeSelf) {
3278 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3279
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003280 EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
3281 EXPECT_EQ("is an object whose given property isn't >= 0",
3282 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003283}
3284
3285// Tests that Property() can explain the match result.
3286TEST(PropertyTest, CanExplainMatchResult) {
3287 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3288
3289 AClass a;
3290 a.set_n(1);
zhanyong.wan736baa82010-09-27 17:44:16 +00003291 EXPECT_EQ("whose given property is 1" + OfType("int"), Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003292
3293 m = Property(&AClass::n, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003294 EXPECT_EQ(
3295 "whose given property is 1" + OfType("int") + ", which is 1 more than 0",
3296 Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003297}
3298
3299// Tests that Property() works when the argument is a pointer to const.
3300TEST(PropertyForPointerTest, WorksForPointerToConst) {
3301 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3302
3303 AClass a;
3304 a.set_n(1);
3305 EXPECT_TRUE(m.Matches(&a));
3306
3307 a.set_n(-1);
3308 EXPECT_FALSE(m.Matches(&a));
3309}
3310
3311// Tests that Property() works when the argument is a pointer to non-const.
3312TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
3313 Matcher<AClass*> m = Property(&AClass::s, StartsWith("hi"));
3314
3315 AClass a;
3316 a.set_s("hill");
3317 EXPECT_TRUE(m.Matches(&a));
3318
3319 a.set_s("hole");
3320 EXPECT_FALSE(m.Matches(&a));
3321}
3322
zhanyong.wan6953a722010-01-13 05:15:07 +00003323// Tests that Property() works when the argument is a reference to a
3324// const pointer.
3325TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) {
3326 Matcher<AClass* const&> m = Property(&AClass::s, StartsWith("hi"));
3327
3328 AClass a;
3329 a.set_s("hill");
3330 EXPECT_TRUE(m.Matches(&a));
3331
3332 a.set_s("hole");
3333 EXPECT_FALSE(m.Matches(&a));
3334}
3335
shiqiane35fdd92008-12-10 05:08:54 +00003336// Tests that Property() does not match the NULL pointer.
3337TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
3338 Matcher<const AClass*> m = Property(&AClass::x, _);
3339 EXPECT_FALSE(m.Matches(NULL));
3340}
3341
3342// Tests that Property(&Foo::property, ...) works when the argument's
3343// type is a sub-type of const Foo*.
3344TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
3345 // The matcher expects a DerivedClass, but inside the Property() we
3346 // say AClass.
3347 Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0));
3348
3349 DerivedClass d;
3350 d.set_n(1);
3351 EXPECT_TRUE(m.Matches(&d));
3352
3353 d.set_n(-1);
3354 EXPECT_FALSE(m.Matches(&d));
3355}
3356
3357// Tests that Property() can describe itself when used to match a pointer.
3358TEST(PropertyForPointerTest, CanDescribeSelf) {
3359 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3360
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003361 EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
3362 EXPECT_EQ("is an object whose given property isn't >= 0",
3363 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003364}
3365
3366// Tests that Property() can explain the result of matching a pointer.
3367TEST(PropertyForPointerTest, CanExplainMatchResult) {
3368 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3369
3370 AClass a;
3371 a.set_n(1);
3372 EXPECT_EQ("", Explain(m, static_cast<const AClass*>(NULL)));
zhanyong.wan736baa82010-09-27 17:44:16 +00003373 EXPECT_EQ(
3374 "which points to an object whose given property is 1" + OfType("int"),
3375 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003376
3377 m = Property(&AClass::n, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003378 EXPECT_EQ("which points to an object whose given property is 1" +
3379 OfType("int") + ", which is 1 more than 0",
3380 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003381}
3382
3383// Tests ResultOf.
3384
3385// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3386// function pointer.
3387string IntToStringFunction(int input) { return input == 1 ? "foo" : "bar"; }
3388
3389TEST(ResultOfTest, WorksForFunctionPointers) {
3390 Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(string("foo")));
3391
3392 EXPECT_TRUE(matcher.Matches(1));
3393 EXPECT_FALSE(matcher.Matches(2));
3394}
3395
3396// Tests that ResultOf() can describe itself.
3397TEST(ResultOfTest, CanDescribeItself) {
3398 Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo"));
3399
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003400 EXPECT_EQ("is mapped by the given callable to a value that "
3401 "is equal to \"foo\"", Describe(matcher));
3402 EXPECT_EQ("is mapped by the given callable to a value that "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003403 "isn't equal to \"foo\"", DescribeNegation(matcher));
shiqiane35fdd92008-12-10 05:08:54 +00003404}
3405
3406// Tests that ResultOf() can explain the match result.
3407int IntFunction(int input) { return input == 42 ? 80 : 90; }
3408
3409TEST(ResultOfTest, CanExplainMatchResult) {
3410 Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
zhanyong.wan736baa82010-09-27 17:44:16 +00003411 EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int"),
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003412 Explain(matcher, 36));
shiqiane35fdd92008-12-10 05:08:54 +00003413
3414 matcher = ResultOf(&IntFunction, GreaterThan(85));
zhanyong.wan736baa82010-09-27 17:44:16 +00003415 EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int") +
3416 ", which is 5 more than 85", Explain(matcher, 36));
shiqiane35fdd92008-12-10 05:08:54 +00003417}
3418
3419// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3420// returns a non-reference.
3421TEST(ResultOfTest, WorksForNonReferenceResults) {
3422 Matcher<int> matcher = ResultOf(&IntFunction, Eq(80));
3423
3424 EXPECT_TRUE(matcher.Matches(42));
3425 EXPECT_FALSE(matcher.Matches(36));
3426}
3427
3428// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3429// returns a reference to non-const.
zhanyong.wan736baa82010-09-27 17:44:16 +00003430double& DoubleFunction(double& input) { return input; } // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00003431
zhanyong.wan736baa82010-09-27 17:44:16 +00003432Uncopyable& RefUncopyableFunction(Uncopyable& obj) { // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00003433 return obj;
3434}
3435
3436TEST(ResultOfTest, WorksForReferenceToNonConstResults) {
3437 double x = 3.14;
3438 double x2 = x;
3439 Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x));
3440
3441 EXPECT_TRUE(matcher.Matches(x));
3442 EXPECT_FALSE(matcher.Matches(x2));
3443
3444 // Test that ResultOf works with uncopyable objects
3445 Uncopyable obj(0);
3446 Uncopyable obj2(0);
3447 Matcher<Uncopyable&> matcher2 =
3448 ResultOf(&RefUncopyableFunction, Ref(obj));
3449
3450 EXPECT_TRUE(matcher2.Matches(obj));
3451 EXPECT_FALSE(matcher2.Matches(obj2));
3452}
3453
3454// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3455// returns a reference to const.
3456const string& StringFunction(const string& input) { return input; }
3457
3458TEST(ResultOfTest, WorksForReferenceToConstResults) {
3459 string s = "foo";
3460 string s2 = s;
3461 Matcher<const string&> matcher = ResultOf(&StringFunction, Ref(s));
3462
3463 EXPECT_TRUE(matcher.Matches(s));
3464 EXPECT_FALSE(matcher.Matches(s2));
3465}
3466
3467// Tests that ResultOf(f, m) works when f(x) and m's
3468// argument types are compatible but different.
3469TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {
3470 // IntFunction() returns int but the inner matcher expects a signed char.
3471 Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85)));
3472
3473 EXPECT_TRUE(matcher.Matches(36));
3474 EXPECT_FALSE(matcher.Matches(42));
3475}
3476
shiqiane35fdd92008-12-10 05:08:54 +00003477// Tests that the program aborts when ResultOf is passed
3478// a NULL function pointer.
3479TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +00003480 EXPECT_DEATH_IF_SUPPORTED(
zhanyong.wan736baa82010-09-27 17:44:16 +00003481 ResultOf(static_cast<string(*)(int dummy)>(NULL), Eq(string("foo"))),
shiqiane35fdd92008-12-10 05:08:54 +00003482 "NULL function pointer is passed into ResultOf\\(\\)\\.");
3483}
shiqiane35fdd92008-12-10 05:08:54 +00003484
3485// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3486// function reference.
3487TEST(ResultOfTest, WorksForFunctionReferences) {
3488 Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq("foo"));
3489 EXPECT_TRUE(matcher.Matches(1));
3490 EXPECT_FALSE(matcher.Matches(2));
3491}
3492
3493// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3494// function object.
3495struct Functor : public ::std::unary_function<int, string> {
3496 result_type operator()(argument_type input) const {
3497 return IntToStringFunction(input);
3498 }
3499};
3500
3501TEST(ResultOfTest, WorksForFunctors) {
3502 Matcher<int> matcher = ResultOf(Functor(), Eq(string("foo")));
3503
3504 EXPECT_TRUE(matcher.Matches(1));
3505 EXPECT_FALSE(matcher.Matches(2));
3506}
3507
3508// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3509// functor with more then one operator() defined. ResultOf() must work
3510// for each defined operator().
3511struct PolymorphicFunctor {
3512 typedef int result_type;
3513 int operator()(int n) { return n; }
3514 int operator()(const char* s) { return static_cast<int>(strlen(s)); }
3515};
3516
3517TEST(ResultOfTest, WorksForPolymorphicFunctors) {
3518 Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5));
3519
3520 EXPECT_TRUE(matcher_int.Matches(10));
3521 EXPECT_FALSE(matcher_int.Matches(2));
3522
3523 Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5));
3524
3525 EXPECT_TRUE(matcher_string.Matches("long string"));
3526 EXPECT_FALSE(matcher_string.Matches("shrt"));
3527}
3528
3529const int* ReferencingFunction(const int& n) { return &n; }
3530
3531struct ReferencingFunctor {
3532 typedef const int* result_type;
3533 result_type operator()(const int& n) { return &n; }
3534};
3535
3536TEST(ResultOfTest, WorksForReferencingCallables) {
3537 const int n = 1;
3538 const int n2 = 1;
3539 Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n));
3540 EXPECT_TRUE(matcher2.Matches(n));
3541 EXPECT_FALSE(matcher2.Matches(n2));
3542
3543 Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n));
3544 EXPECT_TRUE(matcher3.Matches(n));
3545 EXPECT_FALSE(matcher3.Matches(n2));
3546}
3547
shiqiane35fdd92008-12-10 05:08:54 +00003548class DivisibleByImpl {
3549 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003550 explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {}
shiqiane35fdd92008-12-10 05:08:54 +00003551
zhanyong.wandb22c222010-01-28 21:52:29 +00003552 // For testing using ExplainMatchResultTo() with polymorphic matchers.
shiqiane35fdd92008-12-10 05:08:54 +00003553 template <typename T>
zhanyong.wandb22c222010-01-28 21:52:29 +00003554 bool MatchAndExplain(const T& n, MatchResultListener* listener) const {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003555 *listener << "which is " << (n % divider_) << " modulo "
zhanyong.wandb22c222010-01-28 21:52:29 +00003556 << divider_;
shiqiane35fdd92008-12-10 05:08:54 +00003557 return (n % divider_) == 0;
3558 }
3559
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003560 void DescribeTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +00003561 *os << "is divisible by " << divider_;
3562 }
3563
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003564 void DescribeNegationTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +00003565 *os << "is not divisible by " << divider_;
3566 }
3567
zhanyong.wan32de5f52009-12-23 00:13:23 +00003568 void set_divider(int a_divider) { divider_ = a_divider; }
shiqiane35fdd92008-12-10 05:08:54 +00003569 int divider() const { return divider_; }
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003570
shiqiane35fdd92008-12-10 05:08:54 +00003571 private:
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003572 int divider_;
shiqiane35fdd92008-12-10 05:08:54 +00003573};
3574
shiqiane35fdd92008-12-10 05:08:54 +00003575PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) {
3576 return MakePolymorphicMatcher(DivisibleByImpl(n));
3577}
3578
3579// Tests that when AllOf() fails, only the first failing matcher is
3580// asked to explain why.
3581TEST(ExplainMatchResultTest, AllOf_False_False) {
3582 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003583 EXPECT_EQ("which is 1 modulo 4", Explain(m, 5));
shiqiane35fdd92008-12-10 05:08:54 +00003584}
3585
3586// Tests that when AllOf() fails, only the first failing matcher is
3587// asked to explain why.
3588TEST(ExplainMatchResultTest, AllOf_False_True) {
3589 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003590 EXPECT_EQ("which is 2 modulo 4", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003591}
3592
3593// Tests that when AllOf() fails, only the first failing matcher is
3594// asked to explain why.
3595TEST(ExplainMatchResultTest, AllOf_True_False) {
3596 const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003597 EXPECT_EQ("which is 2 modulo 3", Explain(m, 5));
shiqiane35fdd92008-12-10 05:08:54 +00003598}
3599
3600// Tests that when AllOf() succeeds, all matchers are asked to explain
3601// why.
3602TEST(ExplainMatchResultTest, AllOf_True_True) {
3603 const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003604 EXPECT_EQ("which is 0 modulo 2, and which is 0 modulo 3", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003605}
3606
3607TEST(ExplainMatchResultTest, AllOf_True_True_2) {
3608 const Matcher<int> m = AllOf(Ge(2), Le(3));
3609 EXPECT_EQ("", Explain(m, 2));
3610}
3611
3612TEST(ExplainmatcherResultTest, MonomorphicMatcher) {
3613 const Matcher<int> m = GreaterThan(5);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003614 EXPECT_EQ("which is 1 more than 5", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003615}
3616
3617// The following two tests verify that values without a public copy
3618// ctor can be used as arguments to matchers like Eq(), Ge(), and etc
3619// with the help of ByRef().
3620
3621class NotCopyable {
3622 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003623 explicit NotCopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00003624
3625 int value() const { return value_; }
3626
3627 bool operator==(const NotCopyable& rhs) const {
3628 return value() == rhs.value();
3629 }
3630
3631 bool operator>=(const NotCopyable& rhs) const {
3632 return value() >= rhs.value();
3633 }
3634 private:
3635 int value_;
3636
3637 GTEST_DISALLOW_COPY_AND_ASSIGN_(NotCopyable);
3638};
3639
3640TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) {
3641 const NotCopyable const_value1(1);
3642 const Matcher<const NotCopyable&> m = Eq(ByRef(const_value1));
3643
3644 const NotCopyable n1(1), n2(2);
3645 EXPECT_TRUE(m.Matches(n1));
3646 EXPECT_FALSE(m.Matches(n2));
3647}
3648
3649TEST(ByRefTest, AllowsNotCopyableValueInMatchers) {
3650 NotCopyable value2(2);
3651 const Matcher<NotCopyable&> m = Ge(ByRef(value2));
3652
3653 NotCopyable n1(1), n2(2);
3654 EXPECT_FALSE(m.Matches(n1));
3655 EXPECT_TRUE(m.Matches(n2));
3656}
3657
zhanyong.wan320814a2013-03-01 00:20:30 +00003658TEST(IsEmptyTest, ImplementsIsEmpty) {
3659 vector<int> container;
3660 EXPECT_THAT(container, IsEmpty());
3661 container.push_back(0);
3662 EXPECT_THAT(container, Not(IsEmpty()));
3663 container.push_back(1);
3664 EXPECT_THAT(container, Not(IsEmpty()));
3665}
3666
3667TEST(IsEmptyTest, WorksWithString) {
3668 string text;
3669 EXPECT_THAT(text, IsEmpty());
3670 text = "foo";
3671 EXPECT_THAT(text, Not(IsEmpty()));
3672 text = string("\0", 1);
3673 EXPECT_THAT(text, Not(IsEmpty()));
3674}
3675
3676TEST(IsEmptyTest, CanDescribeSelf) {
3677 Matcher<vector<int> > m = IsEmpty();
3678 EXPECT_EQ("is empty", Describe(m));
3679 EXPECT_EQ("isn't empty", DescribeNegation(m));
3680}
3681
3682TEST(IsEmptyTest, ExplainsResult) {
3683 Matcher<vector<int> > m = IsEmpty();
3684 vector<int> container;
3685 EXPECT_EQ("", Explain(m, container));
3686 container.push_back(0);
3687 EXPECT_EQ("whose size is 1", Explain(m, container));
3688}
3689
zhanyong.wana31d9ce2013-03-01 01:50:17 +00003690TEST(SizeIsTest, ImplementsSizeIs) {
3691 vector<int> container;
3692 EXPECT_THAT(container, SizeIs(0));
3693 EXPECT_THAT(container, Not(SizeIs(1)));
3694 container.push_back(0);
3695 EXPECT_THAT(container, Not(SizeIs(0)));
3696 EXPECT_THAT(container, SizeIs(1));
3697 container.push_back(0);
3698 EXPECT_THAT(container, Not(SizeIs(0)));
3699 EXPECT_THAT(container, SizeIs(2));
3700}
3701
3702TEST(SizeIsTest, WorksWithMap) {
3703 map<string, int> container;
3704 EXPECT_THAT(container, SizeIs(0));
3705 EXPECT_THAT(container, Not(SizeIs(1)));
3706 container.insert(make_pair("foo", 1));
3707 EXPECT_THAT(container, Not(SizeIs(0)));
3708 EXPECT_THAT(container, SizeIs(1));
3709 container.insert(make_pair("bar", 2));
3710 EXPECT_THAT(container, Not(SizeIs(0)));
3711 EXPECT_THAT(container, SizeIs(2));
3712}
3713
3714TEST(SizeIsTest, WorksWithReferences) {
3715 vector<int> container;
3716 Matcher<const vector<int>&> m = SizeIs(1);
3717 EXPECT_THAT(container, Not(m));
3718 container.push_back(0);
3719 EXPECT_THAT(container, m);
3720}
3721
3722TEST(SizeIsTest, CanDescribeSelf) {
3723 Matcher<vector<int> > m = SizeIs(2);
3724 EXPECT_EQ("size is equal to 2", Describe(m));
3725 EXPECT_EQ("size isn't equal to 2", DescribeNegation(m));
3726}
3727
3728TEST(SizeIsTest, ExplainsResult) {
3729 Matcher<vector<int> > m1 = SizeIs(2);
3730 Matcher<vector<int> > m2 = SizeIs(Lt(2u));
3731 Matcher<vector<int> > m3 = SizeIs(AnyOf(0, 3));
3732 Matcher<vector<int> > m4 = SizeIs(GreaterThan(1));
3733 vector<int> container;
3734 EXPECT_EQ("whose size 0 doesn't match", Explain(m1, container));
3735 EXPECT_EQ("whose size 0 matches", Explain(m2, container));
3736 EXPECT_EQ("whose size 0 matches", Explain(m3, container));
3737 EXPECT_EQ("whose size 0 doesn't match, which is 1 less than 1",
3738 Explain(m4, container));
3739 container.push_back(0);
3740 container.push_back(0);
3741 EXPECT_EQ("whose size 2 matches", Explain(m1, container));
3742 EXPECT_EQ("whose size 2 doesn't match", Explain(m2, container));
3743 EXPECT_EQ("whose size 2 doesn't match", Explain(m3, container));
3744 EXPECT_EQ("whose size 2 matches, which is 1 more than 1",
3745 Explain(m4, container));
3746}
3747
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003748#if GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003749// Tests ContainerEq with different container types, and
3750// different element types.
3751
3752template <typename T>
zhanyong.wanb8243162009-06-04 05:48:20 +00003753class ContainerEqTest : public testing::Test {};
zhanyong.wan6a896b52009-01-16 01:13:50 +00003754
3755typedef testing::Types<
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003756 set<int>,
3757 vector<size_t>,
3758 multiset<size_t>,
3759 list<int> >
zhanyong.wan6a896b52009-01-16 01:13:50 +00003760 ContainerEqTestTypes;
3761
3762TYPED_TEST_CASE(ContainerEqTest, ContainerEqTestTypes);
3763
3764// Tests that the filled container is equal to itself.
3765TYPED_TEST(ContainerEqTest, EqualsSelf) {
3766 static const int vals[] = {1, 1, 2, 3, 5, 8};
3767 TypeParam my_set(vals, vals + 6);
3768 const Matcher<TypeParam> m = ContainerEq(my_set);
3769 EXPECT_TRUE(m.Matches(my_set));
3770 EXPECT_EQ("", Explain(m, my_set));
3771}
3772
3773// Tests that missing values are reported.
3774TYPED_TEST(ContainerEqTest, ValueMissing) {
3775 static const int vals[] = {1, 1, 2, 3, 5, 8};
3776 static const int test_vals[] = {2, 1, 8, 5};
3777 TypeParam my_set(vals, vals + 6);
3778 TypeParam test_set(test_vals, test_vals + 4);
3779 const Matcher<TypeParam> m = ContainerEq(my_set);
3780 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003781 EXPECT_EQ("which doesn't have these expected elements: 3",
3782 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003783}
3784
3785// Tests that added values are reported.
3786TYPED_TEST(ContainerEqTest, ValueAdded) {
3787 static const int vals[] = {1, 1, 2, 3, 5, 8};
3788 static const int test_vals[] = {1, 2, 3, 5, 8, 46};
3789 TypeParam my_set(vals, vals + 6);
3790 TypeParam test_set(test_vals, test_vals + 6);
3791 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3792 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003793 EXPECT_EQ("which has these unexpected elements: 46", Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003794}
3795
3796// Tests that added and missing values are reported together.
3797TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) {
3798 static const int vals[] = {1, 1, 2, 3, 5, 8};
3799 static const int test_vals[] = {1, 2, 3, 8, 46};
3800 TypeParam my_set(vals, vals + 6);
3801 TypeParam test_set(test_vals, test_vals + 5);
3802 const Matcher<TypeParam> m = ContainerEq(my_set);
3803 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003804 EXPECT_EQ("which has these unexpected elements: 46,\n"
3805 "and doesn't have these expected elements: 5",
3806 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003807}
3808
3809// Tests duplicated value -- expect no explanation.
3810TYPED_TEST(ContainerEqTest, DuplicateDifference) {
3811 static const int vals[] = {1, 1, 2, 3, 5, 8};
3812 static const int test_vals[] = {1, 2, 3, 5, 8};
3813 TypeParam my_set(vals, vals + 6);
3814 TypeParam test_set(test_vals, test_vals + 5);
3815 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3816 // Depending on the container, match may be true or false
3817 // But in any case there should be no explanation.
3818 EXPECT_EQ("", Explain(m, test_set));
3819}
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003820#endif // GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003821
3822// Tests that mutliple missing values are reported.
3823// Using just vector here, so order is predicatble.
3824TEST(ContainerEqExtraTest, MultipleValuesMissing) {
3825 static const int vals[] = {1, 1, 2, 3, 5, 8};
3826 static const int test_vals[] = {2, 1, 5};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003827 vector<int> my_set(vals, vals + 6);
3828 vector<int> test_set(test_vals, test_vals + 3);
3829 const Matcher<vector<int> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003830 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003831 EXPECT_EQ("which doesn't have these expected elements: 3, 8",
3832 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003833}
3834
3835// Tests that added values are reported.
3836// Using just vector here, so order is predicatble.
3837TEST(ContainerEqExtraTest, MultipleValuesAdded) {
3838 static const int vals[] = {1, 1, 2, 3, 5, 8};
3839 static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003840 list<size_t> my_set(vals, vals + 6);
3841 list<size_t> test_set(test_vals, test_vals + 7);
3842 const Matcher<const list<size_t>&> m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003843 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003844 EXPECT_EQ("which has these unexpected elements: 92, 46",
3845 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003846}
3847
3848// Tests that added and missing values are reported together.
3849TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) {
3850 static const int vals[] = {1, 1, 2, 3, 5, 8};
3851 static const int test_vals[] = {1, 2, 3, 92, 46};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003852 list<size_t> my_set(vals, vals + 6);
3853 list<size_t> test_set(test_vals, test_vals + 5);
3854 const Matcher<const list<size_t> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003855 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003856 EXPECT_EQ("which has these unexpected elements: 92, 46,\n"
3857 "and doesn't have these expected elements: 5, 8",
zhanyong.wan6a896b52009-01-16 01:13:50 +00003858 Explain(m, test_set));
3859}
3860
3861// Tests to see that duplicate elements are detected,
3862// but (as above) not reported in the explanation.
3863TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) {
3864 static const int vals[] = {1, 1, 2, 3, 5, 8};
3865 static const int test_vals[] = {1, 2, 3, 5, 8};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003866 vector<int> my_set(vals, vals + 6);
3867 vector<int> test_set(test_vals, test_vals + 5);
3868 const Matcher<vector<int> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003869 EXPECT_TRUE(m.Matches(my_set));
3870 EXPECT_FALSE(m.Matches(test_set));
3871 // There is nothing to report when both sets contain all the same values.
3872 EXPECT_EQ("", Explain(m, test_set));
3873}
3874
3875// Tests that ContainerEq works for non-trivial associative containers,
3876// like maps.
3877TEST(ContainerEqExtraTest, WorksForMaps) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003878 map<int, std::string> my_map;
zhanyong.wan6a896b52009-01-16 01:13:50 +00003879 my_map[0] = "a";
3880 my_map[1] = "b";
3881
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003882 map<int, std::string> test_map;
zhanyong.wan6a896b52009-01-16 01:13:50 +00003883 test_map[0] = "aa";
3884 test_map[1] = "b";
3885
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003886 const Matcher<const map<int, std::string>&> m = ContainerEq(my_map);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003887 EXPECT_TRUE(m.Matches(my_map));
3888 EXPECT_FALSE(m.Matches(test_map));
3889
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003890 EXPECT_EQ("which has these unexpected elements: (0, \"aa\"),\n"
3891 "and doesn't have these expected elements: (0, \"a\")",
zhanyong.wan6a896b52009-01-16 01:13:50 +00003892 Explain(m, test_map));
3893}
3894
zhanyong.wanb8243162009-06-04 05:48:20 +00003895TEST(ContainerEqExtraTest, WorksForNativeArray) {
3896 int a1[] = { 1, 2, 3 };
3897 int a2[] = { 1, 2, 3 };
3898 int b[] = { 1, 2, 4 };
3899
3900 EXPECT_THAT(a1, ContainerEq(a2));
3901 EXPECT_THAT(a1, Not(ContainerEq(b)));
3902}
3903
3904TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) {
3905 const char a1[][3] = { "hi", "lo" };
3906 const char a2[][3] = { "hi", "lo" };
3907 const char b[][3] = { "lo", "hi" };
3908
3909 // Tests using ContainerEq() in the first dimension.
3910 EXPECT_THAT(a1, ContainerEq(a2));
3911 EXPECT_THAT(a1, Not(ContainerEq(b)));
3912
3913 // Tests using ContainerEq() in the second dimension.
3914 EXPECT_THAT(a1, ElementsAre(ContainerEq(a2[0]), ContainerEq(a2[1])));
3915 EXPECT_THAT(a1, ElementsAre(Not(ContainerEq(b[0])), ContainerEq(a2[1])));
3916}
3917
3918TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) {
3919 const int a1[] = { 1, 2, 3 };
3920 const int a2[] = { 1, 2, 3 };
3921 const int b[] = { 1, 2, 3, 4 };
3922
zhanyong.wan2661c682009-06-09 05:42:12 +00003923 const int* const p1 = a1;
3924 EXPECT_THAT(make_tuple(p1, 3), ContainerEq(a2));
3925 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(b)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003926
3927 const int c[] = { 1, 3, 2 };
zhanyong.wan2661c682009-06-09 05:42:12 +00003928 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(c)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003929}
3930
3931TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) {
3932 std::string a1[][3] = {
3933 { "hi", "hello", "ciao" },
3934 { "bye", "see you", "ciao" }
3935 };
3936
3937 std::string a2[][3] = {
3938 { "hi", "hello", "ciao" },
3939 { "bye", "see you", "ciao" }
3940 };
3941
3942 const Matcher<const std::string(&)[2][3]> m = ContainerEq(a2);
3943 EXPECT_THAT(a1, m);
3944
3945 a2[0][0] = "ha";
3946 EXPECT_THAT(a1, m);
3947}
3948
zhanyong.wan898725c2011-09-16 16:45:39 +00003949TEST(WhenSortedByTest, WorksForEmptyContainer) {
3950 const vector<int> numbers;
3951 EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre()));
3952 EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1))));
3953}
3954
3955TEST(WhenSortedByTest, WorksForNonEmptyContainer) {
3956 vector<unsigned> numbers;
3957 numbers.push_back(3);
3958 numbers.push_back(1);
3959 numbers.push_back(2);
3960 numbers.push_back(2);
3961 EXPECT_THAT(numbers, WhenSortedBy(greater<unsigned>(),
3962 ElementsAre(3, 2, 2, 1)));
3963 EXPECT_THAT(numbers, Not(WhenSortedBy(greater<unsigned>(),
3964 ElementsAre(1, 2, 2, 3))));
3965}
3966
3967TEST(WhenSortedByTest, WorksForNonVectorContainer) {
3968 list<string> words;
3969 words.push_back("say");
3970 words.push_back("hello");
3971 words.push_back("world");
3972 EXPECT_THAT(words, WhenSortedBy(less<string>(),
3973 ElementsAre("hello", "say", "world")));
3974 EXPECT_THAT(words, Not(WhenSortedBy(less<string>(),
3975 ElementsAre("say", "hello", "world"))));
3976}
3977
3978TEST(WhenSortedByTest, WorksForNativeArray) {
3979 const int numbers[] = { 1, 3, 2, 4 };
3980 const int sorted_numbers[] = { 1, 2, 3, 4 };
3981 EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre(1, 2, 3, 4)));
3982 EXPECT_THAT(numbers, WhenSortedBy(less<int>(),
3983 ElementsAreArray(sorted_numbers)));
3984 EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1, 3, 2, 4))));
3985}
3986
3987TEST(WhenSortedByTest, CanDescribeSelf) {
3988 const Matcher<vector<int> > m = WhenSortedBy(less<int>(), ElementsAre(1, 2));
3989 EXPECT_EQ("(when sorted) has 2 elements where\n"
3990 "element #0 is equal to 1,\n"
3991 "element #1 is equal to 2",
3992 Describe(m));
3993 EXPECT_EQ("(when sorted) doesn't have 2 elements, or\n"
3994 "element #0 isn't equal to 1, or\n"
3995 "element #1 isn't equal to 2",
3996 DescribeNegation(m));
3997}
3998
3999TEST(WhenSortedByTest, ExplainsMatchResult) {
4000 const int a[] = { 2, 1 };
4001 EXPECT_EQ("which is { 1, 2 } when sorted, whose element #0 doesn't match",
4002 Explain(WhenSortedBy(less<int>(), ElementsAre(2, 3)), a));
4003 EXPECT_EQ("which is { 1, 2 } when sorted",
4004 Explain(WhenSortedBy(less<int>(), ElementsAre(1, 2)), a));
4005}
4006
4007// WhenSorted() is a simple wrapper on WhenSortedBy(). Hence we don't
4008// need to test it as exhaustively as we test the latter.
4009
4010TEST(WhenSortedTest, WorksForEmptyContainer) {
4011 const vector<int> numbers;
4012 EXPECT_THAT(numbers, WhenSorted(ElementsAre()));
4013 EXPECT_THAT(numbers, Not(WhenSorted(ElementsAre(1))));
4014}
4015
4016TEST(WhenSortedTest, WorksForNonEmptyContainer) {
4017 list<string> words;
4018 words.push_back("3");
4019 words.push_back("1");
4020 words.push_back("2");
4021 words.push_back("2");
4022 EXPECT_THAT(words, WhenSorted(ElementsAre("1", "2", "2", "3")));
4023 EXPECT_THAT(words, Not(WhenSorted(ElementsAre("3", "1", "2", "2"))));
4024}
4025
zhanyong.wana9a59e02013-03-27 16:14:55 +00004026TEST(WhenSortedTest, WorksForMapTypes) {
4027 map<string, int> word_counts;
4028 word_counts["and"] = 1;
4029 word_counts["the"] = 1;
4030 word_counts["buffalo"] = 2;
4031 EXPECT_THAT(word_counts, WhenSorted(ElementsAre(
4032 Pair("and", 1), Pair("buffalo", 2), Pair("the", 1))));
4033 EXPECT_THAT(word_counts, Not(WhenSorted(ElementsAre(
4034 Pair("and", 1), Pair("the", 1), Pair("buffalo", 2)))));
4035}
4036
4037TEST(WhenSortedTest, WorksForMultiMapTypes) {
4038 multimap<int, int> ifib;
4039 ifib.insert(make_pair(8, 6));
4040 ifib.insert(make_pair(2, 3));
4041 ifib.insert(make_pair(1, 1));
4042 ifib.insert(make_pair(3, 4));
4043 ifib.insert(make_pair(1, 2));
4044 ifib.insert(make_pair(5, 5));
4045 EXPECT_THAT(ifib, WhenSorted(ElementsAre(Pair(1, 1),
4046 Pair(1, 2),
4047 Pair(2, 3),
4048 Pair(3, 4),
4049 Pair(5, 5),
4050 Pair(8, 6))));
4051 EXPECT_THAT(ifib, Not(WhenSorted(ElementsAre(Pair(8, 6),
4052 Pair(2, 3),
4053 Pair(1, 1),
4054 Pair(3, 4),
4055 Pair(1, 2),
4056 Pair(5, 5)))));
4057}
4058
4059TEST(WhenSortedTest, WorksForPolymorphicMatcher) {
4060 std::deque<int> d;
4061 d.push_back(2);
4062 d.push_back(1);
4063 EXPECT_THAT(d, WhenSorted(ElementsAre(1, 2)));
4064 EXPECT_THAT(d, Not(WhenSorted(ElementsAre(2, 1))));
4065}
4066
4067TEST(WhenSortedTest, WorksForVectorConstRefMatcher) {
4068 std::deque<int> d;
4069 d.push_back(2);
4070 d.push_back(1);
4071 Matcher<const std::vector<int>&> vector_match = ElementsAre(1, 2);
4072 EXPECT_THAT(d, WhenSorted(vector_match));
4073 Matcher<const std::vector<int>&> not_vector_match = ElementsAre(2, 1);
4074 EXPECT_THAT(d, Not(WhenSorted(not_vector_match)));
4075}
4076
4077// Deliberately bare pseudo-container.
4078// Offers only begin() and end() accessors, yielding InputIterator.
4079template <typename T>
4080class Streamlike {
4081 private:
4082 class ConstIter;
4083 public:
4084 typedef ConstIter const_iterator;
4085 typedef T value_type;
4086
4087 template <typename InIter>
4088 Streamlike(InIter first, InIter last) : remainder_(first, last) {}
4089
4090 const_iterator begin() const {
4091 return const_iterator(this, remainder_.begin());
4092 }
4093 const_iterator end() const {
4094 return const_iterator(this, remainder_.end());
4095 }
4096
4097 private:
4098 class ConstIter : public std::iterator<std::input_iterator_tag,
4099 value_type,
4100 ptrdiff_t,
4101 const value_type&,
4102 const value_type*> {
4103 public:
4104 ConstIter(const Streamlike* s,
4105 typename std::list<value_type>::iterator pos)
4106 : s_(s), pos_(pos) {}
4107
4108 const value_type& operator*() const { return *pos_; }
4109 const value_type* operator->() const { return &*pos_; }
4110 ConstIter& operator++() {
4111 s_->remainder_.erase(pos_++);
4112 return *this;
4113 }
4114
4115 // *iter++ is required to work (see std::istreambuf_iterator).
4116 // (void)iter++ is also required to work.
4117 class PostIncrProxy {
4118 public:
4119 explicit PostIncrProxy(const value_type& value) : value_(value) {}
4120 value_type operator*() const { return value_; }
4121 private:
4122 value_type value_;
4123 };
4124 PostIncrProxy operator++(int) {
4125 PostIncrProxy proxy(**this);
4126 ++(*this);
4127 return proxy;
4128 }
4129
4130 friend bool operator==(const ConstIter& a, const ConstIter& b) {
4131 return a.s_ == b.s_ && a.pos_ == b.pos_;
4132 }
4133 friend bool operator!=(const ConstIter& a, const ConstIter& b) {
4134 return !(a == b);
4135 }
4136
4137 private:
4138 const Streamlike* s_;
4139 typename std::list<value_type>::iterator pos_;
4140 };
4141
4142 friend std::ostream& operator<<(std::ostream& os, const Streamlike& s) {
4143 os << "[";
4144 typedef typename std::list<value_type>::const_iterator Iter;
4145 const char* sep = "";
4146 for (Iter it = s.remainder_.begin(); it != s.remainder_.end(); ++it) {
4147 os << sep << *it;
4148 sep = ",";
4149 }
4150 os << "]";
4151 return os;
4152 }
4153
4154 mutable std::list<value_type> remainder_; // modified by iteration
4155};
4156
4157TEST(StreamlikeTest, Iteration) {
4158 const int a[5] = { 2, 1, 4, 5, 3 };
4159 Streamlike<int> s(a, a + 5);
4160 Streamlike<int>::const_iterator it = s.begin();
4161 const int* ip = a;
4162 while (it != s.end()) {
4163 SCOPED_TRACE(ip - a);
4164 EXPECT_EQ(*ip++, *it++);
4165 }
4166}
4167
4168TEST(WhenSortedTest, WorksForStreamlike) {
4169 // Streamlike 'container' provides only minimal iterator support.
4170 // Its iterators are tagged with input_iterator_tag.
4171 const int a[5] = { 2, 1, 4, 5, 3 };
4172 Streamlike<int> s(a, a + 5);
4173 EXPECT_THAT(s, WhenSorted(ElementsAre(1, 2, 3, 4, 5)));
4174 EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3))));
4175}
4176
4177TEST(WhenSortedTest, WorksForVectorConstRefMatcherOnStreamlike) {
4178 const int a[5] = { 2, 1, 4, 5, 3 };
4179 Streamlike<int> s(a, a + 5);
4180 Matcher<const std::vector<int>&> vector_match = ElementsAre(1, 2, 3, 4, 5);
4181 EXPECT_THAT(s, WhenSorted(vector_match));
4182 EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3))));
4183}
4184
zhanyong.wan736baa82010-09-27 17:44:16 +00004185// Tests IsReadableTypeName().
4186
4187TEST(IsReadableTypeNameTest, ReturnsTrueForShortNames) {
4188 EXPECT_TRUE(IsReadableTypeName("int"));
4189 EXPECT_TRUE(IsReadableTypeName("const unsigned char*"));
4190 EXPECT_TRUE(IsReadableTypeName("MyMap<int, void*>"));
4191 EXPECT_TRUE(IsReadableTypeName("void (*)(int, bool)"));
4192}
4193
4194TEST(IsReadableTypeNameTest, ReturnsTrueForLongNonTemplateNonFunctionNames) {
4195 EXPECT_TRUE(IsReadableTypeName("my_long_namespace::MyClassName"));
4196 EXPECT_TRUE(IsReadableTypeName("int [5][6][7][8][9][10][11]"));
4197 EXPECT_TRUE(IsReadableTypeName("my_namespace::MyOuterClass::MyInnerClass"));
4198}
4199
4200TEST(IsReadableTypeNameTest, ReturnsFalseForLongTemplateNames) {
4201 EXPECT_FALSE(
4202 IsReadableTypeName("basic_string<char, std::char_traits<char> >"));
4203 EXPECT_FALSE(IsReadableTypeName("std::vector<int, std::alloc_traits<int> >"));
4204}
4205
4206TEST(IsReadableTypeNameTest, ReturnsFalseForLongFunctionTypeNames) {
4207 EXPECT_FALSE(IsReadableTypeName("void (&)(int, bool, char, float)"));
4208}
4209
zhanyong.wan4a5330d2009-02-19 00:36:44 +00004210// Tests JoinAsTuple().
4211
4212TEST(JoinAsTupleTest, JoinsEmptyTuple) {
4213 EXPECT_EQ("", JoinAsTuple(Strings()));
4214}
4215
4216TEST(JoinAsTupleTest, JoinsOneTuple) {
4217 const char* fields[] = { "1" };
4218 EXPECT_EQ("1", JoinAsTuple(Strings(fields, fields + 1)));
4219}
4220
4221TEST(JoinAsTupleTest, JoinsTwoTuple) {
4222 const char* fields[] = { "1", "a" };
4223 EXPECT_EQ("(1, a)", JoinAsTuple(Strings(fields, fields + 2)));
4224}
4225
4226TEST(JoinAsTupleTest, JoinsTenTuple) {
4227 const char* fields[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
4228 EXPECT_EQ("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)",
4229 JoinAsTuple(Strings(fields, fields + 10)));
4230}
4231
4232// Tests FormatMatcherDescription().
4233
4234TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) {
4235 EXPECT_EQ("is even",
zhanyong.wanb4140802010-06-08 22:53:57 +00004236 FormatMatcherDescription(false, "IsEven", Strings()));
4237 EXPECT_EQ("not (is even)",
4238 FormatMatcherDescription(true, "IsEven", Strings()));
zhanyong.wan4a5330d2009-02-19 00:36:44 +00004239
4240 const char* params[] = { "5" };
4241 EXPECT_EQ("equals 5",
zhanyong.wanb4140802010-06-08 22:53:57 +00004242 FormatMatcherDescription(false, "Equals",
zhanyong.wan4a5330d2009-02-19 00:36:44 +00004243 Strings(params, params + 1)));
4244
4245 const char* params2[] = { "5", "8" };
4246 EXPECT_EQ("is in range (5, 8)",
zhanyong.wanb4140802010-06-08 22:53:57 +00004247 FormatMatcherDescription(false, "IsInRange",
zhanyong.wan4a5330d2009-02-19 00:36:44 +00004248 Strings(params2, params2 + 2)));
4249}
4250
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00004251// Tests PolymorphicMatcher::mutable_impl().
4252TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {
4253 PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
4254 DivisibleByImpl& impl = m.mutable_impl();
4255 EXPECT_EQ(42, impl.divider());
4256
4257 impl.set_divider(0);
4258 EXPECT_EQ(0, m.mutable_impl().divider());
4259}
4260
4261// Tests PolymorphicMatcher::impl().
4262TEST(PolymorphicMatcherTest, CanAccessImpl) {
4263 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
4264 const DivisibleByImpl& impl = m.impl();
4265 EXPECT_EQ(42, impl.divider());
4266}
4267
zhanyong.wanb1c7f932010-03-24 17:35:11 +00004268TEST(MatcherTupleTest, ExplainsMatchFailure) {
4269 stringstream ss1;
4270 ExplainMatchFailureTupleTo(make_tuple(Matcher<char>(Eq('a')), GreaterThan(5)),
4271 make_tuple('a', 10), &ss1);
4272 EXPECT_EQ("", ss1.str()); // Successful match.
4273
4274 stringstream ss2;
4275 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
4276 make_tuple(2, 'b'), &ss2);
4277 EXPECT_EQ(" Expected arg #0: is > 5\n"
4278 " Actual: 2, which is 3 less than 5\n"
zhanyong.wand60c5f42010-07-21 22:21:07 +00004279 " Expected arg #1: is equal to 'a' (97, 0x61)\n"
4280 " Actual: 'b' (98, 0x62)\n",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00004281 ss2.str()); // Failed match where both arguments need explanation.
4282
4283 stringstream ss3;
4284 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
4285 make_tuple(2, 'a'), &ss3);
4286 EXPECT_EQ(" Expected arg #0: is > 5\n"
4287 " Actual: 2, which is 3 less than 5\n",
4288 ss3.str()); // Failed match where only one argument needs
4289 // explanation.
4290}
4291
zhanyong.wan33605ba2010-04-22 23:37:47 +00004292// Tests Each().
4293
4294TEST(EachTest, ExplainsMatchResultCorrectly) {
4295 set<int> a; // empty
4296
4297 Matcher<set<int> > m = Each(2);
4298 EXPECT_EQ("", Explain(m, a));
4299
zhanyong.wan736baa82010-09-27 17:44:16 +00004300 Matcher<const int(&)[1]> n = Each(1); // NOLINT
zhanyong.wan33605ba2010-04-22 23:37:47 +00004301
4302 const int b[1] = { 1 };
4303 EXPECT_EQ("", Explain(n, b));
4304
4305 n = Each(3);
4306 EXPECT_EQ("whose element #0 doesn't match", Explain(n, b));
4307
4308 a.insert(1);
4309 a.insert(2);
4310 a.insert(3);
4311 m = Each(GreaterThan(0));
4312 EXPECT_EQ("", Explain(m, a));
4313
4314 m = Each(GreaterThan(10));
4315 EXPECT_EQ("whose element #0 doesn't match, which is 9 less than 10",
4316 Explain(m, a));
4317}
4318
4319TEST(EachTest, DescribesItselfCorrectly) {
4320 Matcher<vector<int> > m = Each(1);
4321 EXPECT_EQ("only contains elements that is equal to 1", Describe(m));
4322
4323 Matcher<vector<int> > m2 = Not(m);
4324 EXPECT_EQ("contains some element that isn't equal to 1", Describe(m2));
4325}
4326
4327TEST(EachTest, MatchesVectorWhenAllElementsMatch) {
4328 vector<int> some_vector;
4329 EXPECT_THAT(some_vector, Each(1));
4330 some_vector.push_back(3);
4331 EXPECT_THAT(some_vector, Not(Each(1)));
4332 EXPECT_THAT(some_vector, Each(3));
4333 some_vector.push_back(1);
4334 some_vector.push_back(2);
4335 EXPECT_THAT(some_vector, Not(Each(3)));
4336 EXPECT_THAT(some_vector, Each(Lt(3.5)));
4337
4338 vector<string> another_vector;
4339 another_vector.push_back("fee");
4340 EXPECT_THAT(another_vector, Each(string("fee")));
4341 another_vector.push_back("fie");
4342 another_vector.push_back("foe");
4343 another_vector.push_back("fum");
4344 EXPECT_THAT(another_vector, Not(Each(string("fee"))));
4345}
4346
4347TEST(EachTest, MatchesMapWhenAllElementsMatch) {
4348 map<const char*, int> my_map;
4349 const char* bar = "a string";
4350 my_map[bar] = 2;
4351 EXPECT_THAT(my_map, Each(make_pair(bar, 2)));
4352
4353 map<string, int> another_map;
4354 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1)));
4355 another_map["fee"] = 1;
4356 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1)));
4357 another_map["fie"] = 2;
4358 another_map["foe"] = 3;
4359 another_map["fum"] = 4;
4360 EXPECT_THAT(another_map, Not(Each(make_pair(string("fee"), 1))));
4361 EXPECT_THAT(another_map, Not(Each(make_pair(string("fum"), 1))));
4362 EXPECT_THAT(another_map, Each(Pair(_, Gt(0))));
4363}
4364
4365TEST(EachTest, AcceptsMatcher) {
4366 const int a[] = { 1, 2, 3 };
4367 EXPECT_THAT(a, Each(Gt(0)));
4368 EXPECT_THAT(a, Not(Each(Gt(1))));
4369}
4370
4371TEST(EachTest, WorksForNativeArrayAsTuple) {
4372 const int a[] = { 1, 2 };
4373 const int* const pointer = a;
4374 EXPECT_THAT(make_tuple(pointer, 2), Each(Gt(0)));
4375 EXPECT_THAT(make_tuple(pointer, 2), Not(Each(Gt(1))));
4376}
4377
zhanyong.wanab5b77c2010-05-17 19:32:48 +00004378// For testing Pointwise().
4379class IsHalfOfMatcher {
4380 public:
4381 template <typename T1, typename T2>
4382 bool MatchAndExplain(const tuple<T1, T2>& a_pair,
4383 MatchResultListener* listener) const {
4384 if (get<0>(a_pair) == get<1>(a_pair)/2) {
4385 *listener << "where the second is " << get<1>(a_pair);
4386 return true;
4387 } else {
4388 *listener << "where the second/2 is " << get<1>(a_pair)/2;
4389 return false;
4390 }
4391 }
4392
4393 void DescribeTo(ostream* os) const {
4394 *os << "are a pair where the first is half of the second";
4395 }
4396
4397 void DescribeNegationTo(ostream* os) const {
4398 *os << "are a pair where the first isn't half of the second";
4399 }
4400};
4401
4402PolymorphicMatcher<IsHalfOfMatcher> IsHalfOf() {
4403 return MakePolymorphicMatcher(IsHalfOfMatcher());
4404}
4405
4406TEST(PointwiseTest, DescribesSelf) {
4407 vector<int> rhs;
4408 rhs.push_back(1);
4409 rhs.push_back(2);
4410 rhs.push_back(3);
4411 const Matcher<const vector<int>&> m = Pointwise(IsHalfOf(), rhs);
4412 EXPECT_EQ("contains 3 values, where each value and its corresponding value "
4413 "in { 1, 2, 3 } are a pair where the first is half of the second",
4414 Describe(m));
4415 EXPECT_EQ("doesn't contain exactly 3 values, or contains a value x at some "
4416 "index i where x and the i-th value of { 1, 2, 3 } are a pair "
4417 "where the first isn't half of the second",
4418 DescribeNegation(m));
4419}
4420
4421TEST(PointwiseTest, MakesCopyOfRhs) {
4422 list<signed char> rhs;
4423 rhs.push_back(2);
4424 rhs.push_back(4);
4425
4426 int lhs[] = { 1, 2 };
4427 const Matcher<const int (&)[2]> m = Pointwise(IsHalfOf(), rhs);
4428 EXPECT_THAT(lhs, m);
4429
4430 // Changing rhs now shouldn't affect m, which made a copy of rhs.
4431 rhs.push_back(6);
4432 EXPECT_THAT(lhs, m);
4433}
4434
4435TEST(PointwiseTest, WorksForLhsNativeArray) {
4436 const int lhs[] = { 1, 2, 3 };
4437 vector<int> rhs;
4438 rhs.push_back(2);
4439 rhs.push_back(4);
4440 rhs.push_back(6);
4441 EXPECT_THAT(lhs, Pointwise(Lt(), rhs));
4442 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
4443}
4444
4445TEST(PointwiseTest, WorksForRhsNativeArray) {
4446 const int rhs[] = { 1, 2, 3 };
4447 vector<int> lhs;
4448 lhs.push_back(2);
4449 lhs.push_back(4);
4450 lhs.push_back(6);
4451 EXPECT_THAT(lhs, Pointwise(Gt(), rhs));
4452 EXPECT_THAT(lhs, Not(Pointwise(Lt(), rhs)));
4453}
4454
4455TEST(PointwiseTest, RejectsWrongSize) {
4456 const double lhs[2] = { 1, 2 };
4457 const int rhs[1] = { 0 };
4458 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
4459 EXPECT_EQ("which contains 2 values",
4460 Explain(Pointwise(Gt(), rhs), lhs));
4461
4462 const int rhs2[3] = { 0, 1, 2 };
4463 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs2)));
4464}
4465
4466TEST(PointwiseTest, RejectsWrongContent) {
4467 const double lhs[3] = { 1, 2, 3 };
4468 const int rhs[3] = { 2, 6, 4 };
4469 EXPECT_THAT(lhs, Not(Pointwise(IsHalfOf(), rhs)));
4470 EXPECT_EQ("where the value pair (2, 6) at index #1 don't match, "
4471 "where the second/2 is 3",
4472 Explain(Pointwise(IsHalfOf(), rhs), lhs));
4473}
4474
4475TEST(PointwiseTest, AcceptsCorrectContent) {
4476 const double lhs[3] = { 1, 2, 3 };
4477 const int rhs[3] = { 2, 4, 6 };
4478 EXPECT_THAT(lhs, Pointwise(IsHalfOf(), rhs));
4479 EXPECT_EQ("", Explain(Pointwise(IsHalfOf(), rhs), lhs));
4480}
4481
4482TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) {
4483 const double lhs[3] = { 1, 2, 3 };
4484 const int rhs[3] = { 2, 4, 6 };
4485 const Matcher<tuple<const double&, const int&> > m1 = IsHalfOf();
4486 EXPECT_THAT(lhs, Pointwise(m1, rhs));
4487 EXPECT_EQ("", Explain(Pointwise(m1, rhs), lhs));
4488
4489 // This type works as a tuple<const double&, const int&> can be
4490 // implicitly cast to tuple<double, int>.
4491 const Matcher<tuple<double, int> > m2 = IsHalfOf();
4492 EXPECT_THAT(lhs, Pointwise(m2, rhs));
4493 EXPECT_EQ("", Explain(Pointwise(m2, rhs), lhs));
4494}
4495
shiqiane35fdd92008-12-10 05:08:54 +00004496} // namespace gmock_matchers_test
4497} // namespace testing