blob: 3a834a180000dc05cc9479bf7380d40428ff224f [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>
40#include <functional>
zhanyong.wana862f1d2010-03-15 21:23:04 +000041#include <iostream>
zhanyong.wan6a896b52009-01-16 01:13:50 +000042#include <list>
43#include <map>
44#include <set>
shiqiane35fdd92008-12-10 05:08:54 +000045#include <sstream>
zhanyong.wan6a896b52009-01-16 01:13:50 +000046#include <string>
zhanyong.wanf5e1ce52009-09-16 07:02:02 +000047#include <utility>
zhanyong.wan6a896b52009-01-16 01:13:50 +000048#include <vector>
zhanyong.wan53e08c42010-09-14 05:38:21 +000049#include "gmock/gmock.h"
50#include "gtest/gtest.h"
51#include "gtest/gtest-spi.h"
shiqiane35fdd92008-12-10 05:08:54 +000052
53namespace testing {
zhanyong.wan4a5330d2009-02-19 00:36:44 +000054
55namespace internal {
vladlosev587c1b32011-05-20 00:42:22 +000056GTEST_API_ string JoinAsTuple(const Strings& fields);
zhanyong.wan4a5330d2009-02-19 00:36:44 +000057} // namespace internal
58
shiqiane35fdd92008-12-10 05:08:54 +000059namespace gmock_matchers_test {
60
zhanyong.wan898725c2011-09-16 16:45:39 +000061using std::greater;
62using std::less;
zhanyong.wanab5b77c2010-05-17 19:32:48 +000063using std::list;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000064using std::make_pair;
zhanyong.wanb5937da2009-07-16 20:26:41 +000065using std::map;
66using std::multimap;
zhanyong.wanab5b77c2010-05-17 19:32:48 +000067using std::multiset;
68using std::ostream;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000069using std::pair;
zhanyong.wan33605ba2010-04-22 23:37:47 +000070using std::set;
shiqiane35fdd92008-12-10 05:08:54 +000071using std::stringstream;
zhanyong.wanab5b77c2010-05-17 19:32:48 +000072using std::tr1::get;
zhanyong.wanb8243162009-06-04 05:48:20 +000073using std::tr1::make_tuple;
zhanyong.wanab5b77c2010-05-17 19:32:48 +000074using std::tr1::tuple;
zhanyong.wan33605ba2010-04-22 23:37:47 +000075using std::vector;
shiqiane35fdd92008-12-10 05:08:54 +000076using testing::A;
zhanyong.wanbf550852009-06-09 06:09:53 +000077using testing::AllArgs;
shiqiane35fdd92008-12-10 05:08:54 +000078using testing::AllOf;
79using testing::An;
80using testing::AnyOf;
81using testing::ByRef;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000082using testing::ContainsRegex;
shiqiane35fdd92008-12-10 05:08:54 +000083using testing::DoubleEq;
84using testing::EndsWith;
85using testing::Eq;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000086using testing::ExplainMatchResult;
shiqiane35fdd92008-12-10 05:08:54 +000087using testing::Field;
88using testing::FloatEq;
89using testing::Ge;
90using testing::Gt;
91using testing::HasSubstr;
zhanyong.wan320814a2013-03-01 00:20:30 +000092using testing::IsEmpty;
zhanyong.wan2d970ee2009-09-24 21:41:36 +000093using testing::IsNull;
zhanyong.wanb5937da2009-07-16 20:26:41 +000094using testing::Key;
shiqiane35fdd92008-12-10 05:08:54 +000095using testing::Le;
96using testing::Lt;
97using testing::MakeMatcher;
98using testing::MakePolymorphicMatcher;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000099using testing::MatchResultListener;
shiqiane35fdd92008-12-10 05:08:54 +0000100using testing::Matcher;
101using testing::MatcherCast;
102using testing::MatcherInterface;
103using testing::Matches;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000104using testing::MatchesRegex;
shiqiane35fdd92008-12-10 05:08:54 +0000105using testing::NanSensitiveDoubleEq;
106using testing::NanSensitiveFloatEq;
107using testing::Ne;
108using testing::Not;
109using testing::NotNull;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000110using testing::Pair;
shiqiane35fdd92008-12-10 05:08:54 +0000111using testing::Pointee;
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000112using testing::Pointwise;
shiqiane35fdd92008-12-10 05:08:54 +0000113using testing::PolymorphicMatcher;
114using testing::Property;
115using testing::Ref;
116using testing::ResultOf;
zhanyong.wana31d9ce2013-03-01 01:50:17 +0000117using testing::SizeIs;
shiqiane35fdd92008-12-10 05:08:54 +0000118using testing::StartsWith;
119using testing::StrCaseEq;
120using testing::StrCaseNe;
121using testing::StrEq;
122using testing::StrNe;
123using testing::Truly;
124using testing::TypedEq;
zhanyong.wanb8243162009-06-04 05:48:20 +0000125using testing::Value;
zhanyong.wan898725c2011-09-16 16:45:39 +0000126using testing::WhenSorted;
127using testing::WhenSortedBy;
shiqiane35fdd92008-12-10 05:08:54 +0000128using testing::_;
zhanyong.wana862f1d2010-03-15 21:23:04 +0000129using testing::internal::DummyMatchResultListener;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000130using testing::internal::ExplainMatchFailureTupleTo;
shiqiane35fdd92008-12-10 05:08:54 +0000131using testing::internal::FloatingEqMatcher;
zhanyong.wanb4140802010-06-08 22:53:57 +0000132using testing::internal::FormatMatcherDescription;
zhanyong.wan736baa82010-09-27 17:44:16 +0000133using testing::internal::IsReadableTypeName;
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000134using testing::internal::JoinAsTuple;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000135using testing::internal::RE;
zhanyong.wana862f1d2010-03-15 21:23:04 +0000136using testing::internal::StreamMatchResultListener;
zhanyong.wan34b034c2010-03-05 21:23:23 +0000137using testing::internal::StringMatchResultListener;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000138using testing::internal::Strings;
vladlosev79b83502009-11-18 00:43:37 +0000139using testing::internal::linked_ptr;
vladloseve56daa72009-11-18 01:08:08 +0000140using testing::internal::scoped_ptr;
shiqiane35fdd92008-12-10 05:08:54 +0000141using testing::internal::string;
142
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000143// For testing ExplainMatchResultTo().
144class GreaterThanMatcher : public MatcherInterface<int> {
145 public:
146 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
147
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000148 virtual void DescribeTo(ostream* os) const {
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000149 *os << "is > " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000150 }
151
zhanyong.wandb22c222010-01-28 21:52:29 +0000152 virtual bool MatchAndExplain(int lhs,
153 MatchResultListener* listener) const {
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000154 const int diff = lhs - rhs_;
155 if (diff > 0) {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000156 *listener << "which is " << diff << " more than " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000157 } else if (diff == 0) {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000158 *listener << "which is the same as " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000159 } else {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000160 *listener << "which is " << -diff << " less than " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000161 }
zhanyong.wandb22c222010-01-28 21:52:29 +0000162
163 return lhs > rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000164 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000165
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000166 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000167 int rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000168};
169
170Matcher<int> GreaterThan(int n) {
171 return MakeMatcher(new GreaterThanMatcher(n));
172}
173
zhanyong.wan736baa82010-09-27 17:44:16 +0000174string OfType(const string& type_name) {
175#if GTEST_HAS_RTTI
176 return " (of type " + type_name + ")";
177#else
178 return "";
179#endif
180}
181
shiqiane35fdd92008-12-10 05:08:54 +0000182// Returns the description of the given matcher.
183template <typename T>
184string Describe(const Matcher<T>& m) {
185 stringstream ss;
186 m.DescribeTo(&ss);
187 return ss.str();
188}
189
190// Returns the description of the negation of the given matcher.
191template <typename T>
192string DescribeNegation(const Matcher<T>& m) {
193 stringstream ss;
194 m.DescribeNegationTo(&ss);
195 return ss.str();
196}
197
198// Returns the reason why x matches, or doesn't match, m.
199template <typename MatcherType, typename Value>
200string Explain(const MatcherType& m, const Value& x) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000201 StringMatchResultListener listener;
202 ExplainMatchResult(m, x, &listener);
203 return listener.str();
shiqiane35fdd92008-12-10 05:08:54 +0000204}
205
zhanyong.wana862f1d2010-03-15 21:23:04 +0000206TEST(MatchResultListenerTest, StreamingWorks) {
207 StringMatchResultListener listener;
208 listener << "hi" << 5;
209 EXPECT_EQ("hi5", listener.str());
210
211 // Streaming shouldn't crash when the underlying ostream is NULL.
212 DummyMatchResultListener dummy;
213 dummy << "hi" << 5;
214}
215
216TEST(MatchResultListenerTest, CanAccessUnderlyingStream) {
217 EXPECT_TRUE(DummyMatchResultListener().stream() == NULL);
218 EXPECT_TRUE(StreamMatchResultListener(NULL).stream() == NULL);
219
220 EXPECT_EQ(&std::cout, StreamMatchResultListener(&std::cout).stream());
221}
222
223TEST(MatchResultListenerTest, IsInterestedWorks) {
224 EXPECT_TRUE(StringMatchResultListener().IsInterested());
225 EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested());
226
227 EXPECT_FALSE(DummyMatchResultListener().IsInterested());
228 EXPECT_FALSE(StreamMatchResultListener(NULL).IsInterested());
229}
230
shiqiane35fdd92008-12-10 05:08:54 +0000231// Makes sure that the MatcherInterface<T> interface doesn't
232// change.
233class EvenMatcherImpl : public MatcherInterface<int> {
234 public:
zhanyong.wandb22c222010-01-28 21:52:29 +0000235 virtual bool MatchAndExplain(int x,
236 MatchResultListener* /* listener */) const {
237 return x % 2 == 0;
238 }
shiqiane35fdd92008-12-10 05:08:54 +0000239
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000240 virtual void DescribeTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +0000241 *os << "is an even number";
242 }
243
244 // We deliberately don't define DescribeNegationTo() and
245 // ExplainMatchResultTo() here, to make sure the definition of these
246 // two methods is optional.
247};
248
zhanyong.wana862f1d2010-03-15 21:23:04 +0000249// Makes sure that the MatcherInterface API doesn't change.
250TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) {
shiqiane35fdd92008-12-10 05:08:54 +0000251 EvenMatcherImpl m;
252}
253
zhanyong.wan82113312010-01-08 21:55:40 +0000254// Tests implementing a monomorphic matcher using MatchAndExplain().
255
256class NewEvenMatcherImpl : public MatcherInterface<int> {
257 public:
258 virtual bool MatchAndExplain(int x, MatchResultListener* listener) const {
259 const bool match = x % 2 == 0;
260 // Verifies that we can stream to a listener directly.
261 *listener << "value % " << 2;
262 if (listener->stream() != NULL) {
263 // Verifies that we can stream to a listener's underlying stream
264 // too.
265 *listener->stream() << " == " << (x % 2);
266 }
267 return match;
268 }
269
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000270 virtual void DescribeTo(ostream* os) const {
zhanyong.wan82113312010-01-08 21:55:40 +0000271 *os << "is an even number";
272 }
273};
274
275TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {
276 Matcher<int> m = MakeMatcher(new NewEvenMatcherImpl);
277 EXPECT_TRUE(m.Matches(2));
278 EXPECT_FALSE(m.Matches(3));
279 EXPECT_EQ("value % 2 == 0", Explain(m, 2));
280 EXPECT_EQ("value % 2 == 1", Explain(m, 3));
281}
282
shiqiane35fdd92008-12-10 05:08:54 +0000283// Tests default-constructing a matcher.
284TEST(MatcherTest, CanBeDefaultConstructed) {
285 Matcher<double> m;
286}
287
288// Tests that Matcher<T> can be constructed from a MatcherInterface<T>*.
289TEST(MatcherTest, CanBeConstructedFromMatcherInterface) {
290 const MatcherInterface<int>* impl = new EvenMatcherImpl;
291 Matcher<int> m(impl);
292 EXPECT_TRUE(m.Matches(4));
293 EXPECT_FALSE(m.Matches(5));
294}
295
296// Tests that value can be used in place of Eq(value).
297TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) {
298 Matcher<int> m1 = 5;
299 EXPECT_TRUE(m1.Matches(5));
300 EXPECT_FALSE(m1.Matches(6));
301}
302
303// Tests that NULL can be used in place of Eq(NULL).
304TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) {
305 Matcher<int*> m1 = NULL;
306 EXPECT_TRUE(m1.Matches(NULL));
307 int n = 0;
308 EXPECT_FALSE(m1.Matches(&n));
309}
310
311// Tests that matchers are copyable.
312TEST(MatcherTest, IsCopyable) {
313 // Tests the copy constructor.
314 Matcher<bool> m1 = Eq(false);
315 EXPECT_TRUE(m1.Matches(false));
316 EXPECT_FALSE(m1.Matches(true));
317
318 // Tests the assignment operator.
319 m1 = Eq(true);
320 EXPECT_TRUE(m1.Matches(true));
321 EXPECT_FALSE(m1.Matches(false));
322}
323
324// Tests that Matcher<T>::DescribeTo() calls
325// MatcherInterface<T>::DescribeTo().
326TEST(MatcherTest, CanDescribeItself) {
327 EXPECT_EQ("is an even number",
328 Describe(Matcher<int>(new EvenMatcherImpl)));
329}
330
zhanyong.wan82113312010-01-08 21:55:40 +0000331// Tests Matcher<T>::MatchAndExplain().
332TEST(MatcherTest, MatchAndExplain) {
333 Matcher<int> m = GreaterThan(0);
zhanyong.wan34b034c2010-03-05 21:23:23 +0000334 StringMatchResultListener listener1;
zhanyong.wan82113312010-01-08 21:55:40 +0000335 EXPECT_TRUE(m.MatchAndExplain(42, &listener1));
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000336 EXPECT_EQ("which is 42 more than 0", listener1.str());
zhanyong.wan82113312010-01-08 21:55:40 +0000337
zhanyong.wan34b034c2010-03-05 21:23:23 +0000338 StringMatchResultListener listener2;
zhanyong.wan82113312010-01-08 21:55:40 +0000339 EXPECT_FALSE(m.MatchAndExplain(-9, &listener2));
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000340 EXPECT_EQ("which is 9 less than 0", listener2.str());
zhanyong.wan82113312010-01-08 21:55:40 +0000341}
342
shiqiane35fdd92008-12-10 05:08:54 +0000343// Tests that a C-string literal can be implicitly converted to a
344// Matcher<string> or Matcher<const string&>.
345TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
346 Matcher<string> m1 = "hi";
347 EXPECT_TRUE(m1.Matches("hi"));
348 EXPECT_FALSE(m1.Matches("hello"));
349
350 Matcher<const string&> m2 = "hi";
351 EXPECT_TRUE(m2.Matches("hi"));
352 EXPECT_FALSE(m2.Matches("hello"));
353}
354
355// Tests that a string object can be implicitly converted to a
356// Matcher<string> or Matcher<const string&>.
357TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {
358 Matcher<string> m1 = string("hi");
359 EXPECT_TRUE(m1.Matches("hi"));
360 EXPECT_FALSE(m1.Matches("hello"));
361
362 Matcher<const string&> m2 = string("hi");
363 EXPECT_TRUE(m2.Matches("hi"));
364 EXPECT_FALSE(m2.Matches("hello"));
365}
366
zhanyong.wan1f122a02013-03-25 16:27:03 +0000367#if GTEST_HAS_STRING_PIECE_
368// Tests that a C-string literal can be implicitly converted to a
369// Matcher<StringPiece> or Matcher<const StringPiece&>.
370TEST(StringPieceMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
371 Matcher<StringPiece> m1 = "cats";
372 EXPECT_TRUE(m1.Matches("cats"));
373 EXPECT_FALSE(m1.Matches("dogs"));
374
375 Matcher<const StringPiece&> m2 = "cats";
376 EXPECT_TRUE(m2.Matches("cats"));
377 EXPECT_FALSE(m2.Matches("dogs"));
378}
379
380// Tests that a string object can be implicitly converted to a
381// Matcher<StringPiece> or Matcher<const StringPiece&>.
382TEST(StringPieceMatcherTest, CanBeImplicitlyConstructedFromString) {
383 Matcher<StringPiece> m1 = string("cats");
384 EXPECT_TRUE(m1.Matches("cats"));
385 EXPECT_FALSE(m1.Matches("dogs"));
386
387 Matcher<const StringPiece&> m2 = string("cats");
388 EXPECT_TRUE(m2.Matches("cats"));
389 EXPECT_FALSE(m2.Matches("dogs"));
390}
391
392// Tests that a StringPiece object can be implicitly converted to a
393// Matcher<StringPiece> or Matcher<const StringPiece&>.
394TEST(StringPieceMatcherTest, CanBeImplicitlyConstructedFromStringPiece) {
395 Matcher<StringPiece> m1 = StringPiece("cats");
396 EXPECT_TRUE(m1.Matches("cats"));
397 EXPECT_FALSE(m1.Matches("dogs"));
398
399 Matcher<const StringPiece&> m2 = StringPiece("cats");
400 EXPECT_TRUE(m2.Matches("cats"));
401 EXPECT_FALSE(m2.Matches("dogs"));
402}
403#endif // GTEST_HAS_STRING_PIECE_
404
shiqiane35fdd92008-12-10 05:08:54 +0000405// Tests that MakeMatcher() constructs a Matcher<T> from a
406// MatcherInterface* without requiring the user to explicitly
407// write the type.
408TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) {
409 const MatcherInterface<int>* dummy_impl = NULL;
410 Matcher<int> m = MakeMatcher(dummy_impl);
411}
412
zhanyong.wan82113312010-01-08 21:55:40 +0000413// Tests that MakePolymorphicMatcher() can construct a polymorphic
414// matcher from its implementation using the old API.
zhanyong.wan33605ba2010-04-22 23:37:47 +0000415const int g_bar = 1;
shiqiane35fdd92008-12-10 05:08:54 +0000416class ReferencesBarOrIsZeroImpl {
417 public:
418 template <typename T>
zhanyong.wandb22c222010-01-28 21:52:29 +0000419 bool MatchAndExplain(const T& x,
420 MatchResultListener* /* listener */) const {
shiqiane35fdd92008-12-10 05:08:54 +0000421 const void* p = &x;
zhanyong.wan33605ba2010-04-22 23:37:47 +0000422 return p == &g_bar || x == 0;
shiqiane35fdd92008-12-10 05:08:54 +0000423 }
424
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000425 void DescribeTo(ostream* os) const { *os << "g_bar or zero"; }
shiqiane35fdd92008-12-10 05:08:54 +0000426
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000427 void DescribeNegationTo(ostream* os) const {
zhanyong.wan33605ba2010-04-22 23:37:47 +0000428 *os << "doesn't reference g_bar and is not zero";
shiqiane35fdd92008-12-10 05:08:54 +0000429 }
430};
431
432// This function verifies that MakePolymorphicMatcher() returns a
433// PolymorphicMatcher<T> where T is the argument's type.
434PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() {
435 return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl());
436}
437
zhanyong.wan82113312010-01-08 21:55:40 +0000438TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) {
shiqiane35fdd92008-12-10 05:08:54 +0000439 // Using a polymorphic matcher to match a reference type.
440 Matcher<const int&> m1 = ReferencesBarOrIsZero();
441 EXPECT_TRUE(m1.Matches(0));
442 // Verifies that the identity of a by-reference argument is preserved.
zhanyong.wan33605ba2010-04-22 23:37:47 +0000443 EXPECT_TRUE(m1.Matches(g_bar));
shiqiane35fdd92008-12-10 05:08:54 +0000444 EXPECT_FALSE(m1.Matches(1));
zhanyong.wan33605ba2010-04-22 23:37:47 +0000445 EXPECT_EQ("g_bar or zero", Describe(m1));
shiqiane35fdd92008-12-10 05:08:54 +0000446
447 // Using a polymorphic matcher to match a value type.
448 Matcher<double> m2 = ReferencesBarOrIsZero();
449 EXPECT_TRUE(m2.Matches(0.0));
450 EXPECT_FALSE(m2.Matches(0.1));
zhanyong.wan33605ba2010-04-22 23:37:47 +0000451 EXPECT_EQ("g_bar or zero", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +0000452}
453
zhanyong.wan82113312010-01-08 21:55:40 +0000454// Tests implementing a polymorphic matcher using MatchAndExplain().
455
456class PolymorphicIsEvenImpl {
457 public:
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000458 void DescribeTo(ostream* os) const { *os << "is even"; }
zhanyong.wan82113312010-01-08 21:55:40 +0000459
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000460 void DescribeNegationTo(ostream* os) const {
zhanyong.wan82113312010-01-08 21:55:40 +0000461 *os << "is odd";
462 }
zhanyong.wan82113312010-01-08 21:55:40 +0000463
zhanyong.wandb22c222010-01-28 21:52:29 +0000464 template <typename T>
465 bool MatchAndExplain(const T& x, MatchResultListener* listener) const {
466 // Verifies that we can stream to the listener directly.
467 *listener << "% " << 2;
468 if (listener->stream() != NULL) {
469 // Verifies that we can stream to the listener's underlying stream
470 // too.
471 *listener->stream() << " == " << (x % 2);
472 }
473 return (x % 2) == 0;
zhanyong.wan82113312010-01-08 21:55:40 +0000474 }
zhanyong.wandb22c222010-01-28 21:52:29 +0000475};
zhanyong.wan82113312010-01-08 21:55:40 +0000476
477PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() {
478 return MakePolymorphicMatcher(PolymorphicIsEvenImpl());
479}
480
481TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {
482 // Using PolymorphicIsEven() as a Matcher<int>.
483 const Matcher<int> m1 = PolymorphicIsEven();
484 EXPECT_TRUE(m1.Matches(42));
485 EXPECT_FALSE(m1.Matches(43));
486 EXPECT_EQ("is even", Describe(m1));
487
488 const Matcher<int> not_m1 = Not(m1);
489 EXPECT_EQ("is odd", Describe(not_m1));
490
491 EXPECT_EQ("% 2 == 0", Explain(m1, 42));
492
493 // Using PolymorphicIsEven() as a Matcher<char>.
494 const Matcher<char> m2 = PolymorphicIsEven();
495 EXPECT_TRUE(m2.Matches('\x42'));
496 EXPECT_FALSE(m2.Matches('\x43'));
497 EXPECT_EQ("is even", Describe(m2));
498
499 const Matcher<char> not_m2 = Not(m2);
500 EXPECT_EQ("is odd", Describe(not_m2));
501
502 EXPECT_EQ("% 2 == 0", Explain(m2, '\x42'));
503}
504
shiqiane35fdd92008-12-10 05:08:54 +0000505// Tests that MatcherCast<T>(m) works when m is a polymorphic matcher.
506TEST(MatcherCastTest, FromPolymorphicMatcher) {
507 Matcher<int> m = MatcherCast<int>(Eq(5));
508 EXPECT_TRUE(m.Matches(5));
509 EXPECT_FALSE(m.Matches(6));
510}
511
512// For testing casting matchers between compatible types.
513class IntValue {
514 public:
515 // An int can be statically (although not implicitly) cast to a
516 // IntValue.
zhanyong.wan32de5f52009-12-23 00:13:23 +0000517 explicit IntValue(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +0000518
519 int value() const { return value_; }
520 private:
521 int value_;
522};
523
524// For testing casting matchers between compatible types.
525bool IsPositiveIntValue(const IntValue& foo) {
526 return foo.value() > 0;
527}
528
529// Tests that MatcherCast<T>(m) works when m is a Matcher<U> where T
530// can be statically converted to U.
531TEST(MatcherCastTest, FromCompatibleType) {
532 Matcher<double> m1 = Eq(2.0);
533 Matcher<int> m2 = MatcherCast<int>(m1);
534 EXPECT_TRUE(m2.Matches(2));
535 EXPECT_FALSE(m2.Matches(3));
536
537 Matcher<IntValue> m3 = Truly(IsPositiveIntValue);
538 Matcher<int> m4 = MatcherCast<int>(m3);
539 // In the following, the arguments 1 and 0 are statically converted
540 // to IntValue objects, and then tested by the IsPositiveIntValue()
541 // predicate.
542 EXPECT_TRUE(m4.Matches(1));
543 EXPECT_FALSE(m4.Matches(0));
544}
545
546// Tests that MatcherCast<T>(m) works when m is a Matcher<const T&>.
547TEST(MatcherCastTest, FromConstReferenceToNonReference) {
548 Matcher<const int&> m1 = Eq(0);
549 Matcher<int> m2 = MatcherCast<int>(m1);
550 EXPECT_TRUE(m2.Matches(0));
551 EXPECT_FALSE(m2.Matches(1));
552}
553
554// Tests that MatcherCast<T>(m) works when m is a Matcher<T&>.
555TEST(MatcherCastTest, FromReferenceToNonReference) {
556 Matcher<int&> m1 = Eq(0);
557 Matcher<int> m2 = MatcherCast<int>(m1);
558 EXPECT_TRUE(m2.Matches(0));
559 EXPECT_FALSE(m2.Matches(1));
560}
561
562// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
563TEST(MatcherCastTest, FromNonReferenceToConstReference) {
564 Matcher<int> m1 = Eq(0);
565 Matcher<const int&> m2 = MatcherCast<const int&>(m1);
566 EXPECT_TRUE(m2.Matches(0));
567 EXPECT_FALSE(m2.Matches(1));
568}
569
570// Tests that MatcherCast<T&>(m) works when m is a Matcher<T>.
571TEST(MatcherCastTest, FromNonReferenceToReference) {
572 Matcher<int> m1 = Eq(0);
573 Matcher<int&> m2 = MatcherCast<int&>(m1);
574 int n = 0;
575 EXPECT_TRUE(m2.Matches(n));
576 n = 1;
577 EXPECT_FALSE(m2.Matches(n));
578}
579
580// Tests that MatcherCast<T>(m) works when m is a Matcher<T>.
581TEST(MatcherCastTest, FromSameType) {
582 Matcher<int> m1 = Eq(0);
583 Matcher<int> m2 = MatcherCast<int>(m1);
584 EXPECT_TRUE(m2.Matches(0));
585 EXPECT_FALSE(m2.Matches(1));
586}
587
jgm79a367e2012-04-10 16:02:11 +0000588// Implicitly convertible form any type.
589struct ConvertibleFromAny {
590 ConvertibleFromAny(int a_value) : value(a_value) {}
591 template <typename T>
592 ConvertibleFromAny(const T& a_value) : value(-1) {
593 ADD_FAILURE() << "Conversion constructor called";
594 }
595 int value;
596};
597
598bool operator==(const ConvertibleFromAny& a, const ConvertibleFromAny& b) {
599 return a.value == b.value;
600}
601
602ostream& operator<<(ostream& os, const ConvertibleFromAny& a) {
603 return os << a.value;
604}
605
606TEST(MatcherCastTest, ConversionConstructorIsUsed) {
607 Matcher<ConvertibleFromAny> m = MatcherCast<ConvertibleFromAny>(1);
608 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
609 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
610}
611
612TEST(MatcherCastTest, FromConvertibleFromAny) {
613 Matcher<ConvertibleFromAny> m =
614 MatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
615 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
616 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
617}
618
zhanyong.wan18490652009-05-11 18:54:08 +0000619class Base {};
620class Derived : public Base {};
621
622// Tests that SafeMatcherCast<T>(m) works when m is a polymorphic matcher.
623TEST(SafeMatcherCastTest, FromPolymorphicMatcher) {
624 Matcher<char> m2 = SafeMatcherCast<char>(Eq(32));
625 EXPECT_TRUE(m2.Matches(' '));
626 EXPECT_FALSE(m2.Matches('\n'));
627}
628
zhanyong.wan16cf4732009-05-14 20:55:30 +0000629// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where
630// T and U are arithmetic types and T can be losslessly converted to
631// U.
632TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) {
zhanyong.wan18490652009-05-11 18:54:08 +0000633 Matcher<double> m1 = DoubleEq(1.0);
zhanyong.wan16cf4732009-05-14 20:55:30 +0000634 Matcher<float> m2 = SafeMatcherCast<float>(m1);
635 EXPECT_TRUE(m2.Matches(1.0f));
636 EXPECT_FALSE(m2.Matches(2.0f));
637
638 Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>('a'));
639 EXPECT_TRUE(m3.Matches('a'));
640 EXPECT_FALSE(m3.Matches('b'));
zhanyong.wan18490652009-05-11 18:54:08 +0000641}
642
643// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where T and U
644// are pointers or references to a derived and a base class, correspondingly.
645TEST(SafeMatcherCastTest, FromBaseClass) {
646 Derived d, d2;
647 Matcher<Base*> m1 = Eq(&d);
648 Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1);
649 EXPECT_TRUE(m2.Matches(&d));
650 EXPECT_FALSE(m2.Matches(&d2));
651
652 Matcher<Base&> m3 = Ref(d);
653 Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3);
654 EXPECT_TRUE(m4.Matches(d));
655 EXPECT_FALSE(m4.Matches(d2));
656}
657
658// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<const T&>.
659TEST(SafeMatcherCastTest, FromConstReferenceToReference) {
660 int n = 0;
661 Matcher<const int&> m1 = Ref(n);
662 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
663 int n1 = 0;
664 EXPECT_TRUE(m2.Matches(n));
665 EXPECT_FALSE(m2.Matches(n1));
666}
667
668// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
669TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) {
670 Matcher<int> m1 = Eq(0);
671 Matcher<const int&> m2 = SafeMatcherCast<const int&>(m1);
672 EXPECT_TRUE(m2.Matches(0));
673 EXPECT_FALSE(m2.Matches(1));
674}
675
676// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<T>.
677TEST(SafeMatcherCastTest, FromNonReferenceToReference) {
678 Matcher<int> m1 = Eq(0);
679 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
680 int n = 0;
681 EXPECT_TRUE(m2.Matches(n));
682 n = 1;
683 EXPECT_FALSE(m2.Matches(n));
684}
685
686// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<T>.
687TEST(SafeMatcherCastTest, FromSameType) {
688 Matcher<int> m1 = Eq(0);
689 Matcher<int> m2 = SafeMatcherCast<int>(m1);
690 EXPECT_TRUE(m2.Matches(0));
691 EXPECT_FALSE(m2.Matches(1));
692}
693
jgm79a367e2012-04-10 16:02:11 +0000694TEST(SafeMatcherCastTest, ConversionConstructorIsUsed) {
695 Matcher<ConvertibleFromAny> m = SafeMatcherCast<ConvertibleFromAny>(1);
696 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
697 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
698}
699
700TEST(SafeMatcherCastTest, FromConvertibleFromAny) {
701 Matcher<ConvertibleFromAny> m =
702 SafeMatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
703 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
704 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
705}
706
shiqiane35fdd92008-12-10 05:08:54 +0000707// Tests that A<T>() matches any value of type T.
708TEST(ATest, MatchesAnyValue) {
709 // Tests a matcher for a value type.
710 Matcher<double> m1 = A<double>();
711 EXPECT_TRUE(m1.Matches(91.43));
712 EXPECT_TRUE(m1.Matches(-15.32));
713
714 // Tests a matcher for a reference type.
715 int a = 2;
716 int b = -6;
717 Matcher<int&> m2 = A<int&>();
718 EXPECT_TRUE(m2.Matches(a));
719 EXPECT_TRUE(m2.Matches(b));
720}
721
722// Tests that A<T>() describes itself properly.
723TEST(ATest, CanDescribeSelf) {
724 EXPECT_EQ("is anything", Describe(A<bool>()));
725}
726
727// Tests that An<T>() matches any value of type T.
728TEST(AnTest, MatchesAnyValue) {
729 // Tests a matcher for a value type.
730 Matcher<int> m1 = An<int>();
731 EXPECT_TRUE(m1.Matches(9143));
732 EXPECT_TRUE(m1.Matches(-1532));
733
734 // Tests a matcher for a reference type.
735 int a = 2;
736 int b = -6;
737 Matcher<int&> m2 = An<int&>();
738 EXPECT_TRUE(m2.Matches(a));
739 EXPECT_TRUE(m2.Matches(b));
740}
741
742// Tests that An<T>() describes itself properly.
743TEST(AnTest, CanDescribeSelf) {
744 EXPECT_EQ("is anything", Describe(An<int>()));
745}
746
747// Tests that _ can be used as a matcher for any type and matches any
748// value of that type.
749TEST(UnderscoreTest, MatchesAnyValue) {
750 // Uses _ as a matcher for a value type.
751 Matcher<int> m1 = _;
752 EXPECT_TRUE(m1.Matches(123));
753 EXPECT_TRUE(m1.Matches(-242));
754
755 // Uses _ as a matcher for a reference type.
756 bool a = false;
757 const bool b = true;
758 Matcher<const bool&> m2 = _;
759 EXPECT_TRUE(m2.Matches(a));
760 EXPECT_TRUE(m2.Matches(b));
761}
762
763// Tests that _ describes itself properly.
764TEST(UnderscoreTest, CanDescribeSelf) {
765 Matcher<int> m = _;
766 EXPECT_EQ("is anything", Describe(m));
767}
768
769// Tests that Eq(x) matches any value equal to x.
770TEST(EqTest, MatchesEqualValue) {
771 // 2 C-strings with same content but different addresses.
772 const char a1[] = "hi";
773 const char a2[] = "hi";
774
775 Matcher<const char*> m1 = Eq(a1);
776 EXPECT_TRUE(m1.Matches(a1));
777 EXPECT_FALSE(m1.Matches(a2));
778}
779
780// Tests that Eq(v) describes itself properly.
781
782class Unprintable {
783 public:
784 Unprintable() : c_('a') {}
785
zhanyong.wan32de5f52009-12-23 00:13:23 +0000786 bool operator==(const Unprintable& /* rhs */) { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000787 private:
788 char c_;
789};
790
791TEST(EqTest, CanDescribeSelf) {
792 Matcher<Unprintable> m = Eq(Unprintable());
793 EXPECT_EQ("is equal to 1-byte object <61>", Describe(m));
794}
795
796// Tests that Eq(v) can be used to match any type that supports
797// comparing with type T, where T is v's type.
798TEST(EqTest, IsPolymorphic) {
799 Matcher<int> m1 = Eq(1);
800 EXPECT_TRUE(m1.Matches(1));
801 EXPECT_FALSE(m1.Matches(2));
802
803 Matcher<char> m2 = Eq(1);
804 EXPECT_TRUE(m2.Matches('\1'));
805 EXPECT_FALSE(m2.Matches('a'));
806}
807
808// Tests that TypedEq<T>(v) matches values of type T that's equal to v.
809TEST(TypedEqTest, ChecksEqualityForGivenType) {
810 Matcher<char> m1 = TypedEq<char>('a');
811 EXPECT_TRUE(m1.Matches('a'));
812 EXPECT_FALSE(m1.Matches('b'));
813
814 Matcher<int> m2 = TypedEq<int>(6);
815 EXPECT_TRUE(m2.Matches(6));
816 EXPECT_FALSE(m2.Matches(7));
817}
818
819// Tests that TypedEq(v) describes itself properly.
820TEST(TypedEqTest, CanDescribeSelf) {
821 EXPECT_EQ("is equal to 2", Describe(TypedEq<int>(2)));
822}
823
824// Tests that TypedEq<T>(v) has type Matcher<T>.
825
826// Type<T>::IsTypeOf(v) compiles iff the type of value v is T, where T
827// is a "bare" type (i.e. not in the form of const U or U&). If v's
828// type is not T, the compiler will generate a message about
829// "undefined referece".
830template <typename T>
831struct Type {
zhanyong.wan32de5f52009-12-23 00:13:23 +0000832 static bool IsTypeOf(const T& /* v */) { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000833
834 template <typename T2>
835 static void IsTypeOf(T2 v);
836};
837
838TEST(TypedEqTest, HasSpecifiedType) {
839 // Verfies that the type of TypedEq<T>(v) is Matcher<T>.
840 Type<Matcher<int> >::IsTypeOf(TypedEq<int>(5));
841 Type<Matcher<double> >::IsTypeOf(TypedEq<double>(5));
842}
843
844// Tests that Ge(v) matches anything >= v.
845TEST(GeTest, ImplementsGreaterThanOrEqual) {
846 Matcher<int> m1 = Ge(0);
847 EXPECT_TRUE(m1.Matches(1));
848 EXPECT_TRUE(m1.Matches(0));
849 EXPECT_FALSE(m1.Matches(-1));
850}
851
852// Tests that Ge(v) describes itself properly.
853TEST(GeTest, CanDescribeSelf) {
854 Matcher<int> m = Ge(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000855 EXPECT_EQ("is >= 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000856}
857
858// Tests that Gt(v) matches anything > v.
859TEST(GtTest, ImplementsGreaterThan) {
860 Matcher<double> m1 = Gt(0);
861 EXPECT_TRUE(m1.Matches(1.0));
862 EXPECT_FALSE(m1.Matches(0.0));
863 EXPECT_FALSE(m1.Matches(-1.0));
864}
865
866// Tests that Gt(v) describes itself properly.
867TEST(GtTest, CanDescribeSelf) {
868 Matcher<int> m = Gt(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000869 EXPECT_EQ("is > 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000870}
871
872// Tests that Le(v) matches anything <= v.
873TEST(LeTest, ImplementsLessThanOrEqual) {
874 Matcher<char> m1 = Le('b');
875 EXPECT_TRUE(m1.Matches('a'));
876 EXPECT_TRUE(m1.Matches('b'));
877 EXPECT_FALSE(m1.Matches('c'));
878}
879
880// Tests that Le(v) describes itself properly.
881TEST(LeTest, CanDescribeSelf) {
882 Matcher<int> m = Le(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000883 EXPECT_EQ("is <= 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000884}
885
886// Tests that Lt(v) matches anything < v.
887TEST(LtTest, ImplementsLessThan) {
888 Matcher<const string&> m1 = Lt("Hello");
889 EXPECT_TRUE(m1.Matches("Abc"));
890 EXPECT_FALSE(m1.Matches("Hello"));
891 EXPECT_FALSE(m1.Matches("Hello, world!"));
892}
893
894// Tests that Lt(v) describes itself properly.
895TEST(LtTest, CanDescribeSelf) {
896 Matcher<int> m = Lt(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000897 EXPECT_EQ("is < 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000898}
899
900// Tests that Ne(v) matches anything != v.
901TEST(NeTest, ImplementsNotEqual) {
902 Matcher<int> m1 = Ne(0);
903 EXPECT_TRUE(m1.Matches(1));
904 EXPECT_TRUE(m1.Matches(-1));
905 EXPECT_FALSE(m1.Matches(0));
906}
907
908// Tests that Ne(v) describes itself properly.
909TEST(NeTest, CanDescribeSelf) {
910 Matcher<int> m = Ne(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000911 EXPECT_EQ("isn't equal to 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000912}
913
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000914// Tests that IsNull() matches any NULL pointer of any type.
915TEST(IsNullTest, MatchesNullPointer) {
916 Matcher<int*> m1 = IsNull();
917 int* p1 = NULL;
918 int n = 0;
919 EXPECT_TRUE(m1.Matches(p1));
920 EXPECT_FALSE(m1.Matches(&n));
921
922 Matcher<const char*> m2 = IsNull();
923 const char* p2 = NULL;
924 EXPECT_TRUE(m2.Matches(p2));
925 EXPECT_FALSE(m2.Matches("hi"));
926
zhanyong.wan95b12332009-09-25 18:55:50 +0000927#if !GTEST_OS_SYMBIAN
928 // Nokia's Symbian compiler generates:
929 // gmock-matchers.h: ambiguous access to overloaded function
930 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(void *)'
931 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(const testing::
932 // MatcherInterface<void *> *)'
933 // gmock-matchers.h: (point of instantiation: 'testing::
934 // gmock_matchers_test::IsNullTest_MatchesNullPointer_Test::TestBody()')
935 // gmock-matchers.h: (instantiating: 'testing::PolymorphicMatc
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000936 Matcher<void*> m3 = IsNull();
937 void* p3 = NULL;
938 EXPECT_TRUE(m3.Matches(p3));
939 EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef)));
zhanyong.wan95b12332009-09-25 18:55:50 +0000940#endif
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000941}
942
vladlosev79b83502009-11-18 00:43:37 +0000943TEST(IsNullTest, LinkedPtr) {
944 const Matcher<linked_ptr<int> > m = IsNull();
945 const linked_ptr<int> null_p;
946 const linked_ptr<int> non_null_p(new int);
947
948 EXPECT_TRUE(m.Matches(null_p));
949 EXPECT_FALSE(m.Matches(non_null_p));
950}
951
952TEST(IsNullTest, ReferenceToConstLinkedPtr) {
953 const Matcher<const linked_ptr<double>&> m = IsNull();
954 const linked_ptr<double> null_p;
955 const linked_ptr<double> non_null_p(new double);
956
957 EXPECT_TRUE(m.Matches(null_p));
958 EXPECT_FALSE(m.Matches(non_null_p));
959}
960
vladloseve56daa72009-11-18 01:08:08 +0000961TEST(IsNullTest, ReferenceToConstScopedPtr) {
962 const Matcher<const scoped_ptr<double>&> m = IsNull();
963 const scoped_ptr<double> null_p;
964 const scoped_ptr<double> non_null_p(new double);
965
966 EXPECT_TRUE(m.Matches(null_p));
967 EXPECT_FALSE(m.Matches(non_null_p));
968}
969
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000970// Tests that IsNull() describes itself properly.
971TEST(IsNullTest, CanDescribeSelf) {
972 Matcher<int*> m = IsNull();
973 EXPECT_EQ("is NULL", Describe(m));
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000974 EXPECT_EQ("isn't NULL", DescribeNegation(m));
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000975}
976
shiqiane35fdd92008-12-10 05:08:54 +0000977// Tests that NotNull() matches any non-NULL pointer of any type.
978TEST(NotNullTest, MatchesNonNullPointer) {
979 Matcher<int*> m1 = NotNull();
980 int* p1 = NULL;
981 int n = 0;
982 EXPECT_FALSE(m1.Matches(p1));
983 EXPECT_TRUE(m1.Matches(&n));
984
985 Matcher<const char*> m2 = NotNull();
986 const char* p2 = NULL;
987 EXPECT_FALSE(m2.Matches(p2));
988 EXPECT_TRUE(m2.Matches("hi"));
989}
990
vladlosev79b83502009-11-18 00:43:37 +0000991TEST(NotNullTest, LinkedPtr) {
992 const Matcher<linked_ptr<int> > m = NotNull();
993 const linked_ptr<int> null_p;
994 const linked_ptr<int> non_null_p(new int);
995
996 EXPECT_FALSE(m.Matches(null_p));
997 EXPECT_TRUE(m.Matches(non_null_p));
998}
999
1000TEST(NotNullTest, ReferenceToConstLinkedPtr) {
1001 const Matcher<const linked_ptr<double>&> m = NotNull();
1002 const linked_ptr<double> null_p;
1003 const linked_ptr<double> non_null_p(new double);
1004
1005 EXPECT_FALSE(m.Matches(null_p));
1006 EXPECT_TRUE(m.Matches(non_null_p));
1007}
1008
vladloseve56daa72009-11-18 01:08:08 +00001009TEST(NotNullTest, ReferenceToConstScopedPtr) {
1010 const Matcher<const scoped_ptr<double>&> m = NotNull();
1011 const scoped_ptr<double> null_p;
1012 const scoped_ptr<double> non_null_p(new double);
1013
1014 EXPECT_FALSE(m.Matches(null_p));
1015 EXPECT_TRUE(m.Matches(non_null_p));
1016}
1017
shiqiane35fdd92008-12-10 05:08:54 +00001018// Tests that NotNull() describes itself properly.
1019TEST(NotNullTest, CanDescribeSelf) {
1020 Matcher<int*> m = NotNull();
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001021 EXPECT_EQ("isn't NULL", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001022}
1023
1024// Tests that Ref(variable) matches an argument that references
1025// 'variable'.
1026TEST(RefTest, MatchesSameVariable) {
1027 int a = 0;
1028 int b = 0;
1029 Matcher<int&> m = Ref(a);
1030 EXPECT_TRUE(m.Matches(a));
1031 EXPECT_FALSE(m.Matches(b));
1032}
1033
1034// Tests that Ref(variable) describes itself properly.
1035TEST(RefTest, CanDescribeSelf) {
1036 int n = 5;
1037 Matcher<int&> m = Ref(n);
1038 stringstream ss;
1039 ss << "references the variable @" << &n << " 5";
1040 EXPECT_EQ(string(ss.str()), Describe(m));
1041}
1042
1043// Test that Ref(non_const_varialbe) can be used as a matcher for a
1044// const reference.
1045TEST(RefTest, CanBeUsedAsMatcherForConstReference) {
1046 int a = 0;
1047 int b = 0;
1048 Matcher<const int&> m = Ref(a);
1049 EXPECT_TRUE(m.Matches(a));
1050 EXPECT_FALSE(m.Matches(b));
1051}
1052
1053// Tests that Ref(variable) is covariant, i.e. Ref(derived) can be
1054// used wherever Ref(base) can be used (Ref(derived) is a sub-type
1055// of Ref(base), but not vice versa.
1056
shiqiane35fdd92008-12-10 05:08:54 +00001057TEST(RefTest, IsCovariant) {
1058 Base base, base2;
1059 Derived derived;
1060 Matcher<const Base&> m1 = Ref(base);
1061 EXPECT_TRUE(m1.Matches(base));
1062 EXPECT_FALSE(m1.Matches(base2));
1063 EXPECT_FALSE(m1.Matches(derived));
1064
1065 m1 = Ref(derived);
1066 EXPECT_TRUE(m1.Matches(derived));
1067 EXPECT_FALSE(m1.Matches(base));
1068 EXPECT_FALSE(m1.Matches(base2));
1069}
1070
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001071TEST(RefTest, ExplainsResult) {
1072 int n = 0;
1073 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), n),
1074 StartsWith("which is located @"));
1075
1076 int m = 0;
1077 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), m),
1078 StartsWith("which is located @"));
1079}
1080
shiqiane35fdd92008-12-10 05:08:54 +00001081// Tests string comparison matchers.
1082
1083TEST(StrEqTest, MatchesEqualString) {
1084 Matcher<const char*> m = StrEq(string("Hello"));
1085 EXPECT_TRUE(m.Matches("Hello"));
1086 EXPECT_FALSE(m.Matches("hello"));
1087 EXPECT_FALSE(m.Matches(NULL));
1088
1089 Matcher<const string&> m2 = StrEq("Hello");
1090 EXPECT_TRUE(m2.Matches("Hello"));
1091 EXPECT_FALSE(m2.Matches("Hi"));
1092}
1093
1094TEST(StrEqTest, CanDescribeSelf) {
vladlosevaa432202011-04-01 21:58:42 +00001095 Matcher<string> m = StrEq("Hi-\'\"?\\\a\b\f\n\r\t\v\xD3");
1096 EXPECT_EQ("is equal to \"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",
shiqiane35fdd92008-12-10 05:08:54 +00001097 Describe(m));
1098
1099 string str("01204500800");
1100 str[3] = '\0';
1101 Matcher<string> m2 = StrEq(str);
1102 EXPECT_EQ("is equal to \"012\\04500800\"", Describe(m2));
1103 str[0] = str[6] = str[7] = str[9] = str[10] = '\0';
1104 Matcher<string> m3 = StrEq(str);
1105 EXPECT_EQ("is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3));
1106}
1107
1108TEST(StrNeTest, MatchesUnequalString) {
1109 Matcher<const char*> m = StrNe("Hello");
1110 EXPECT_TRUE(m.Matches(""));
1111 EXPECT_TRUE(m.Matches(NULL));
1112 EXPECT_FALSE(m.Matches("Hello"));
1113
1114 Matcher<string> m2 = StrNe(string("Hello"));
1115 EXPECT_TRUE(m2.Matches("hello"));
1116 EXPECT_FALSE(m2.Matches("Hello"));
1117}
1118
1119TEST(StrNeTest, CanDescribeSelf) {
1120 Matcher<const char*> m = StrNe("Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001121 EXPECT_EQ("isn't equal to \"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001122}
1123
1124TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
1125 Matcher<const char*> m = StrCaseEq(string("Hello"));
1126 EXPECT_TRUE(m.Matches("Hello"));
1127 EXPECT_TRUE(m.Matches("hello"));
1128 EXPECT_FALSE(m.Matches("Hi"));
1129 EXPECT_FALSE(m.Matches(NULL));
1130
1131 Matcher<const string&> m2 = StrCaseEq("Hello");
1132 EXPECT_TRUE(m2.Matches("hello"));
1133 EXPECT_FALSE(m2.Matches("Hi"));
1134}
1135
1136TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1137 string str1("oabocdooeoo");
1138 string str2("OABOCDOOEOO");
1139 Matcher<const string&> m0 = StrCaseEq(str1);
1140 EXPECT_FALSE(m0.Matches(str2 + string(1, '\0')));
1141
1142 str1[3] = str2[3] = '\0';
1143 Matcher<const string&> m1 = StrCaseEq(str1);
1144 EXPECT_TRUE(m1.Matches(str2));
1145
1146 str1[0] = str1[6] = str1[7] = str1[10] = '\0';
1147 str2[0] = str2[6] = str2[7] = str2[10] = '\0';
1148 Matcher<const string&> m2 = StrCaseEq(str1);
1149 str1[9] = str2[9] = '\0';
1150 EXPECT_FALSE(m2.Matches(str2));
1151
1152 Matcher<const string&> m3 = StrCaseEq(str1);
1153 EXPECT_TRUE(m3.Matches(str2));
1154
1155 EXPECT_FALSE(m3.Matches(str2 + "x"));
1156 str2.append(1, '\0');
1157 EXPECT_FALSE(m3.Matches(str2));
1158 EXPECT_FALSE(m3.Matches(string(str2, 0, 9)));
1159}
1160
1161TEST(StrCaseEqTest, CanDescribeSelf) {
1162 Matcher<string> m = StrCaseEq("Hi");
1163 EXPECT_EQ("is equal to (ignoring case) \"Hi\"", Describe(m));
1164}
1165
1166TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1167 Matcher<const char*> m = StrCaseNe("Hello");
1168 EXPECT_TRUE(m.Matches("Hi"));
1169 EXPECT_TRUE(m.Matches(NULL));
1170 EXPECT_FALSE(m.Matches("Hello"));
1171 EXPECT_FALSE(m.Matches("hello"));
1172
1173 Matcher<string> m2 = StrCaseNe(string("Hello"));
1174 EXPECT_TRUE(m2.Matches(""));
1175 EXPECT_FALSE(m2.Matches("Hello"));
1176}
1177
1178TEST(StrCaseNeTest, CanDescribeSelf) {
1179 Matcher<const char*> m = StrCaseNe("Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001180 EXPECT_EQ("isn't equal to (ignoring case) \"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001181}
1182
1183// Tests that HasSubstr() works for matching string-typed values.
1184TEST(HasSubstrTest, WorksForStringClasses) {
1185 const Matcher<string> m1 = HasSubstr("foo");
1186 EXPECT_TRUE(m1.Matches(string("I love food.")));
1187 EXPECT_FALSE(m1.Matches(string("tofo")));
1188
1189 const Matcher<const std::string&> m2 = HasSubstr("foo");
1190 EXPECT_TRUE(m2.Matches(std::string("I love food.")));
1191 EXPECT_FALSE(m2.Matches(std::string("tofo")));
1192}
1193
1194// Tests that HasSubstr() works for matching C-string-typed values.
1195TEST(HasSubstrTest, WorksForCStrings) {
1196 const Matcher<char*> m1 = HasSubstr("foo");
1197 EXPECT_TRUE(m1.Matches(const_cast<char*>("I love food.")));
1198 EXPECT_FALSE(m1.Matches(const_cast<char*>("tofo")));
1199 EXPECT_FALSE(m1.Matches(NULL));
1200
1201 const Matcher<const char*> m2 = HasSubstr("foo");
1202 EXPECT_TRUE(m2.Matches("I love food."));
1203 EXPECT_FALSE(m2.Matches("tofo"));
1204 EXPECT_FALSE(m2.Matches(NULL));
1205}
1206
1207// Tests that HasSubstr(s) describes itself properly.
1208TEST(HasSubstrTest, CanDescribeSelf) {
1209 Matcher<string> m = HasSubstr("foo\n\"");
1210 EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m));
1211}
1212
zhanyong.wanb5937da2009-07-16 20:26:41 +00001213TEST(KeyTest, CanDescribeSelf) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001214 Matcher<const pair<std::string, int>&> m = Key("foo");
zhanyong.wanb5937da2009-07-16 20:26:41 +00001215 EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001216 EXPECT_EQ("doesn't have a key that is equal to \"foo\"", DescribeNegation(m));
1217}
1218
1219TEST(KeyTest, ExplainsResult) {
1220 Matcher<pair<int, bool> > m = Key(GreaterThan(10));
1221 EXPECT_EQ("whose first field is a value which is 5 less than 10",
1222 Explain(m, make_pair(5, true)));
1223 EXPECT_EQ("whose first field is a value which is 5 more than 10",
1224 Explain(m, make_pair(15, true)));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001225}
1226
1227TEST(KeyTest, MatchesCorrectly) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001228 pair<int, std::string> p(25, "foo");
zhanyong.wanb5937da2009-07-16 20:26:41 +00001229 EXPECT_THAT(p, Key(25));
1230 EXPECT_THAT(p, Not(Key(42)));
1231 EXPECT_THAT(p, Key(Ge(20)));
1232 EXPECT_THAT(p, Not(Key(Lt(25))));
1233}
1234
1235TEST(KeyTest, SafelyCastsInnerMatcher) {
1236 Matcher<int> is_positive = Gt(0);
1237 Matcher<int> is_negative = Lt(0);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001238 pair<char, bool> p('a', true);
zhanyong.wanb5937da2009-07-16 20:26:41 +00001239 EXPECT_THAT(p, Key(is_positive));
1240 EXPECT_THAT(p, Not(Key(is_negative)));
1241}
1242
1243TEST(KeyTest, InsideContainsUsingMap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001244 map<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001245 container.insert(make_pair(1, 'a'));
1246 container.insert(make_pair(2, 'b'));
1247 container.insert(make_pair(4, 'c'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001248 EXPECT_THAT(container, Contains(Key(1)));
1249 EXPECT_THAT(container, Not(Contains(Key(3))));
1250}
1251
1252TEST(KeyTest, InsideContainsUsingMultimap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001253 multimap<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001254 container.insert(make_pair(1, 'a'));
1255 container.insert(make_pair(2, 'b'));
1256 container.insert(make_pair(4, 'c'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001257
1258 EXPECT_THAT(container, Not(Contains(Key(25))));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001259 container.insert(make_pair(25, 'd'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001260 EXPECT_THAT(container, Contains(Key(25)));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001261 container.insert(make_pair(25, 'e'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001262 EXPECT_THAT(container, Contains(Key(25)));
1263
1264 EXPECT_THAT(container, Contains(Key(1)));
1265 EXPECT_THAT(container, Not(Contains(Key(3))));
1266}
1267
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001268TEST(PairTest, Typing) {
1269 // Test verifies the following type conversions can be compiled.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001270 Matcher<const pair<const char*, int>&> m1 = Pair("foo", 42);
1271 Matcher<const pair<const char*, int> > m2 = Pair("foo", 42);
1272 Matcher<pair<const char*, int> > m3 = Pair("foo", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001273
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001274 Matcher<pair<int, const std::string> > m4 = Pair(25, "42");
1275 Matcher<pair<const std::string, int> > m5 = Pair("25", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001276}
1277
1278TEST(PairTest, CanDescribeSelf) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001279 Matcher<const pair<std::string, int>&> m1 = Pair("foo", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001280 EXPECT_EQ("has a first field that is equal to \"foo\""
1281 ", and has a second field that is equal to 42",
1282 Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001283 EXPECT_EQ("has a first field that isn't equal to \"foo\""
1284 ", or has a second field that isn't equal to 42",
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001285 DescribeNegation(m1));
1286 // Double and triple negation (1 or 2 times not and description of negation).
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001287 Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42));
1288 EXPECT_EQ("has a first field that isn't equal to 13"
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001289 ", and has a second field that is equal to 42",
1290 DescribeNegation(m2));
1291}
1292
1293TEST(PairTest, CanExplainMatchResultTo) {
zhanyong.wan82113312010-01-08 21:55:40 +00001294 // If neither field matches, Pair() should explain about the first
1295 // field.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001296 const Matcher<pair<int, int> > m = Pair(GreaterThan(0), GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001297 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001298 Explain(m, make_pair(-1, -2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001299
zhanyong.wan82113312010-01-08 21:55:40 +00001300 // If the first field matches but the second doesn't, Pair() should
1301 // explain about the second field.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001302 EXPECT_EQ("whose second field does not match, which is 2 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001303 Explain(m, make_pair(1, -2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001304
zhanyong.wan82113312010-01-08 21:55:40 +00001305 // If the first field doesn't match but the second does, Pair()
1306 // should explain about the first field.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001307 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001308 Explain(m, make_pair(-1, 2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001309
zhanyong.wan82113312010-01-08 21:55:40 +00001310 // If both fields match, Pair() should explain about them both.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001311 EXPECT_EQ("whose both fields match, where the first field is a value "
1312 "which is 1 more than 0, and the second field is a value "
1313 "which is 2 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001314 Explain(m, make_pair(1, 2)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001315
1316 // If only the first match has an explanation, only this explanation should
1317 // be printed.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001318 const Matcher<pair<int, int> > explain_first = Pair(GreaterThan(0), 0);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001319 EXPECT_EQ("whose both fields match, where the first field is a value "
1320 "which is 1 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001321 Explain(explain_first, make_pair(1, 0)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001322
1323 // If only the second match has an explanation, only this explanation should
1324 // be printed.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001325 const Matcher<pair<int, int> > explain_second = Pair(0, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001326 EXPECT_EQ("whose both fields match, where the second field is a value "
1327 "which is 1 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001328 Explain(explain_second, make_pair(0, 1)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001329}
1330
1331TEST(PairTest, MatchesCorrectly) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001332 pair<int, std::string> p(25, "foo");
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001333
1334 // Both fields match.
1335 EXPECT_THAT(p, Pair(25, "foo"));
1336 EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o")));
1337
1338 // 'first' doesnt' match, but 'second' matches.
1339 EXPECT_THAT(p, Not(Pair(42, "foo")));
1340 EXPECT_THAT(p, Not(Pair(Lt(25), "foo")));
1341
1342 // 'first' matches, but 'second' doesn't match.
1343 EXPECT_THAT(p, Not(Pair(25, "bar")));
1344 EXPECT_THAT(p, Not(Pair(25, Not("foo"))));
1345
1346 // Neither field matches.
1347 EXPECT_THAT(p, Not(Pair(13, "bar")));
1348 EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a"))));
1349}
1350
1351TEST(PairTest, SafelyCastsInnerMatchers) {
1352 Matcher<int> is_positive = Gt(0);
1353 Matcher<int> is_negative = Lt(0);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001354 pair<char, bool> p('a', true);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001355 EXPECT_THAT(p, Pair(is_positive, _));
1356 EXPECT_THAT(p, Not(Pair(is_negative, _)));
1357 EXPECT_THAT(p, Pair(_, is_positive));
1358 EXPECT_THAT(p, Not(Pair(_, is_negative)));
1359}
1360
1361TEST(PairTest, InsideContainsUsingMap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001362 map<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001363 container.insert(make_pair(1, 'a'));
1364 container.insert(make_pair(2, 'b'));
1365 container.insert(make_pair(4, 'c'));
zhanyong.wan95b12332009-09-25 18:55:50 +00001366 EXPECT_THAT(container, Contains(Pair(1, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001367 EXPECT_THAT(container, Contains(Pair(1, _)));
zhanyong.wan95b12332009-09-25 18:55:50 +00001368 EXPECT_THAT(container, Contains(Pair(_, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001369 EXPECT_THAT(container, Not(Contains(Pair(3, _))));
1370}
1371
shiqiane35fdd92008-12-10 05:08:54 +00001372// Tests StartsWith(s).
1373
1374TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
1375 const Matcher<const char*> m1 = StartsWith(string(""));
1376 EXPECT_TRUE(m1.Matches("Hi"));
1377 EXPECT_TRUE(m1.Matches(""));
1378 EXPECT_FALSE(m1.Matches(NULL));
1379
1380 const Matcher<const string&> m2 = StartsWith("Hi");
1381 EXPECT_TRUE(m2.Matches("Hi"));
1382 EXPECT_TRUE(m2.Matches("Hi Hi!"));
1383 EXPECT_TRUE(m2.Matches("High"));
1384 EXPECT_FALSE(m2.Matches("H"));
1385 EXPECT_FALSE(m2.Matches(" Hi"));
1386}
1387
1388TEST(StartsWithTest, CanDescribeSelf) {
1389 Matcher<const std::string> m = StartsWith("Hi");
1390 EXPECT_EQ("starts with \"Hi\"", Describe(m));
1391}
1392
1393// Tests EndsWith(s).
1394
1395TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
1396 const Matcher<const char*> m1 = EndsWith("");
1397 EXPECT_TRUE(m1.Matches("Hi"));
1398 EXPECT_TRUE(m1.Matches(""));
1399 EXPECT_FALSE(m1.Matches(NULL));
1400
1401 const Matcher<const string&> m2 = EndsWith(string("Hi"));
1402 EXPECT_TRUE(m2.Matches("Hi"));
1403 EXPECT_TRUE(m2.Matches("Wow Hi Hi"));
1404 EXPECT_TRUE(m2.Matches("Super Hi"));
1405 EXPECT_FALSE(m2.Matches("i"));
1406 EXPECT_FALSE(m2.Matches("Hi "));
1407}
1408
1409TEST(EndsWithTest, CanDescribeSelf) {
1410 Matcher<const std::string> m = EndsWith("Hi");
1411 EXPECT_EQ("ends with \"Hi\"", Describe(m));
1412}
1413
shiqiane35fdd92008-12-10 05:08:54 +00001414// Tests MatchesRegex().
1415
1416TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
1417 const Matcher<const char*> m1 = MatchesRegex("a.*z");
1418 EXPECT_TRUE(m1.Matches("az"));
1419 EXPECT_TRUE(m1.Matches("abcz"));
1420 EXPECT_FALSE(m1.Matches(NULL));
1421
1422 const Matcher<const string&> m2 = MatchesRegex(new RE("a.*z"));
1423 EXPECT_TRUE(m2.Matches("azbz"));
1424 EXPECT_FALSE(m2.Matches("az1"));
1425 EXPECT_FALSE(m2.Matches("1az"));
1426}
1427
1428TEST(MatchesRegexTest, CanDescribeSelf) {
1429 Matcher<const std::string> m1 = MatchesRegex(string("Hi.*"));
1430 EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1));
1431
zhanyong.wand14aaed2010-01-14 05:36:32 +00001432 Matcher<const char*> m2 = MatchesRegex(new RE("a.*"));
1433 EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +00001434}
1435
1436// Tests ContainsRegex().
1437
1438TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
1439 const Matcher<const char*> m1 = ContainsRegex(string("a.*z"));
1440 EXPECT_TRUE(m1.Matches("az"));
1441 EXPECT_TRUE(m1.Matches("0abcz1"));
1442 EXPECT_FALSE(m1.Matches(NULL));
1443
1444 const Matcher<const string&> m2 = ContainsRegex(new RE("a.*z"));
1445 EXPECT_TRUE(m2.Matches("azbz"));
1446 EXPECT_TRUE(m2.Matches("az1"));
1447 EXPECT_FALSE(m2.Matches("1a"));
1448}
1449
1450TEST(ContainsRegexTest, CanDescribeSelf) {
1451 Matcher<const std::string> m1 = ContainsRegex("Hi.*");
1452 EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1));
1453
zhanyong.wand14aaed2010-01-14 05:36:32 +00001454 Matcher<const char*> m2 = ContainsRegex(new RE("a.*"));
1455 EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +00001456}
shiqiane35fdd92008-12-10 05:08:54 +00001457
1458// Tests for wide strings.
1459#if GTEST_HAS_STD_WSTRING
1460TEST(StdWideStrEqTest, MatchesEqual) {
1461 Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello"));
1462 EXPECT_TRUE(m.Matches(L"Hello"));
1463 EXPECT_FALSE(m.Matches(L"hello"));
1464 EXPECT_FALSE(m.Matches(NULL));
1465
1466 Matcher<const ::std::wstring&> m2 = StrEq(L"Hello");
1467 EXPECT_TRUE(m2.Matches(L"Hello"));
1468 EXPECT_FALSE(m2.Matches(L"Hi"));
1469
1470 Matcher<const ::std::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1471 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1472 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1473
1474 ::std::wstring str(L"01204500800");
1475 str[3] = L'\0';
1476 Matcher<const ::std::wstring&> m4 = StrEq(str);
1477 EXPECT_TRUE(m4.Matches(str));
1478 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1479 Matcher<const ::std::wstring&> m5 = StrEq(str);
1480 EXPECT_TRUE(m5.Matches(str));
1481}
1482
1483TEST(StdWideStrEqTest, CanDescribeSelf) {
vladlosevaa432202011-04-01 21:58:42 +00001484 Matcher< ::std::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v");
1485 EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
shiqiane35fdd92008-12-10 05:08:54 +00001486 Describe(m));
1487
1488 Matcher< ::std::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1489 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1490 Describe(m2));
1491
1492 ::std::wstring str(L"01204500800");
1493 str[3] = L'\0';
1494 Matcher<const ::std::wstring&> m4 = StrEq(str);
1495 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1496 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1497 Matcher<const ::std::wstring&> m5 = StrEq(str);
1498 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1499}
1500
1501TEST(StdWideStrNeTest, MatchesUnequalString) {
1502 Matcher<const wchar_t*> m = StrNe(L"Hello");
1503 EXPECT_TRUE(m.Matches(L""));
1504 EXPECT_TRUE(m.Matches(NULL));
1505 EXPECT_FALSE(m.Matches(L"Hello"));
1506
1507 Matcher< ::std::wstring> m2 = StrNe(::std::wstring(L"Hello"));
1508 EXPECT_TRUE(m2.Matches(L"hello"));
1509 EXPECT_FALSE(m2.Matches(L"Hello"));
1510}
1511
1512TEST(StdWideStrNeTest, CanDescribeSelf) {
1513 Matcher<const wchar_t*> m = StrNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001514 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001515}
1516
1517TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1518 Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello"));
1519 EXPECT_TRUE(m.Matches(L"Hello"));
1520 EXPECT_TRUE(m.Matches(L"hello"));
1521 EXPECT_FALSE(m.Matches(L"Hi"));
1522 EXPECT_FALSE(m.Matches(NULL));
1523
1524 Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello");
1525 EXPECT_TRUE(m2.Matches(L"hello"));
1526 EXPECT_FALSE(m2.Matches(L"Hi"));
1527}
1528
1529TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1530 ::std::wstring str1(L"oabocdooeoo");
1531 ::std::wstring str2(L"OABOCDOOEOO");
1532 Matcher<const ::std::wstring&> m0 = StrCaseEq(str1);
1533 EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L'\0')));
1534
1535 str1[3] = str2[3] = L'\0';
1536 Matcher<const ::std::wstring&> m1 = StrCaseEq(str1);
1537 EXPECT_TRUE(m1.Matches(str2));
1538
1539 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1540 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1541 Matcher<const ::std::wstring&> m2 = StrCaseEq(str1);
1542 str1[9] = str2[9] = L'\0';
1543 EXPECT_FALSE(m2.Matches(str2));
1544
1545 Matcher<const ::std::wstring&> m3 = StrCaseEq(str1);
1546 EXPECT_TRUE(m3.Matches(str2));
1547
1548 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1549 str2.append(1, L'\0');
1550 EXPECT_FALSE(m3.Matches(str2));
1551 EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9)));
1552}
1553
1554TEST(StdWideStrCaseEqTest, CanDescribeSelf) {
1555 Matcher< ::std::wstring> m = StrCaseEq(L"Hi");
1556 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1557}
1558
1559TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1560 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1561 EXPECT_TRUE(m.Matches(L"Hi"));
1562 EXPECT_TRUE(m.Matches(NULL));
1563 EXPECT_FALSE(m.Matches(L"Hello"));
1564 EXPECT_FALSE(m.Matches(L"hello"));
1565
1566 Matcher< ::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello"));
1567 EXPECT_TRUE(m2.Matches(L""));
1568 EXPECT_FALSE(m2.Matches(L"Hello"));
1569}
1570
1571TEST(StdWideStrCaseNeTest, CanDescribeSelf) {
1572 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001573 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001574}
1575
1576// Tests that HasSubstr() works for matching wstring-typed values.
1577TEST(StdWideHasSubstrTest, WorksForStringClasses) {
1578 const Matcher< ::std::wstring> m1 = HasSubstr(L"foo");
1579 EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food.")));
1580 EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo")));
1581
1582 const Matcher<const ::std::wstring&> m2 = HasSubstr(L"foo");
1583 EXPECT_TRUE(m2.Matches(::std::wstring(L"I love food.")));
1584 EXPECT_FALSE(m2.Matches(::std::wstring(L"tofo")));
1585}
1586
1587// Tests that HasSubstr() works for matching C-wide-string-typed values.
1588TEST(StdWideHasSubstrTest, WorksForCStrings) {
1589 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1590 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1591 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1592 EXPECT_FALSE(m1.Matches(NULL));
1593
1594 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1595 EXPECT_TRUE(m2.Matches(L"I love food."));
1596 EXPECT_FALSE(m2.Matches(L"tofo"));
1597 EXPECT_FALSE(m2.Matches(NULL));
1598}
1599
1600// Tests that HasSubstr(s) describes itself properly.
1601TEST(StdWideHasSubstrTest, CanDescribeSelf) {
1602 Matcher< ::std::wstring> m = HasSubstr(L"foo\n\"");
1603 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1604}
1605
1606// Tests StartsWith(s).
1607
1608TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) {
1609 const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L""));
1610 EXPECT_TRUE(m1.Matches(L"Hi"));
1611 EXPECT_TRUE(m1.Matches(L""));
1612 EXPECT_FALSE(m1.Matches(NULL));
1613
1614 const Matcher<const ::std::wstring&> m2 = StartsWith(L"Hi");
1615 EXPECT_TRUE(m2.Matches(L"Hi"));
1616 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1617 EXPECT_TRUE(m2.Matches(L"High"));
1618 EXPECT_FALSE(m2.Matches(L"H"));
1619 EXPECT_FALSE(m2.Matches(L" Hi"));
1620}
1621
1622TEST(StdWideStartsWithTest, CanDescribeSelf) {
1623 Matcher<const ::std::wstring> m = StartsWith(L"Hi");
1624 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1625}
1626
1627// Tests EndsWith(s).
1628
1629TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) {
1630 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1631 EXPECT_TRUE(m1.Matches(L"Hi"));
1632 EXPECT_TRUE(m1.Matches(L""));
1633 EXPECT_FALSE(m1.Matches(NULL));
1634
1635 const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L"Hi"));
1636 EXPECT_TRUE(m2.Matches(L"Hi"));
1637 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1638 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1639 EXPECT_FALSE(m2.Matches(L"i"));
1640 EXPECT_FALSE(m2.Matches(L"Hi "));
1641}
1642
1643TEST(StdWideEndsWithTest, CanDescribeSelf) {
1644 Matcher<const ::std::wstring> m = EndsWith(L"Hi");
1645 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1646}
1647
1648#endif // GTEST_HAS_STD_WSTRING
1649
1650#if GTEST_HAS_GLOBAL_WSTRING
1651TEST(GlobalWideStrEqTest, MatchesEqual) {
1652 Matcher<const wchar_t*> m = StrEq(::wstring(L"Hello"));
1653 EXPECT_TRUE(m.Matches(L"Hello"));
1654 EXPECT_FALSE(m.Matches(L"hello"));
1655 EXPECT_FALSE(m.Matches(NULL));
1656
1657 Matcher<const ::wstring&> m2 = StrEq(L"Hello");
1658 EXPECT_TRUE(m2.Matches(L"Hello"));
1659 EXPECT_FALSE(m2.Matches(L"Hi"));
1660
1661 Matcher<const ::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1662 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1663 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1664
1665 ::wstring str(L"01204500800");
1666 str[3] = L'\0';
1667 Matcher<const ::wstring&> m4 = StrEq(str);
1668 EXPECT_TRUE(m4.Matches(str));
1669 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1670 Matcher<const ::wstring&> m5 = StrEq(str);
1671 EXPECT_TRUE(m5.Matches(str));
1672}
1673
1674TEST(GlobalWideStrEqTest, CanDescribeSelf) {
vladlosevaa432202011-04-01 21:58:42 +00001675 Matcher< ::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v");
1676 EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
shiqiane35fdd92008-12-10 05:08:54 +00001677 Describe(m));
1678
1679 Matcher< ::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1680 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1681 Describe(m2));
1682
1683 ::wstring str(L"01204500800");
1684 str[3] = L'\0';
1685 Matcher<const ::wstring&> m4 = StrEq(str);
1686 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1687 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1688 Matcher<const ::wstring&> m5 = StrEq(str);
1689 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1690}
1691
1692TEST(GlobalWideStrNeTest, MatchesUnequalString) {
1693 Matcher<const wchar_t*> m = StrNe(L"Hello");
1694 EXPECT_TRUE(m.Matches(L""));
1695 EXPECT_TRUE(m.Matches(NULL));
1696 EXPECT_FALSE(m.Matches(L"Hello"));
1697
1698 Matcher< ::wstring> m2 = StrNe(::wstring(L"Hello"));
1699 EXPECT_TRUE(m2.Matches(L"hello"));
1700 EXPECT_FALSE(m2.Matches(L"Hello"));
1701}
1702
1703TEST(GlobalWideStrNeTest, CanDescribeSelf) {
1704 Matcher<const wchar_t*> m = StrNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001705 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001706}
1707
1708TEST(GlobalWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1709 Matcher<const wchar_t*> m = StrCaseEq(::wstring(L"Hello"));
1710 EXPECT_TRUE(m.Matches(L"Hello"));
1711 EXPECT_TRUE(m.Matches(L"hello"));
1712 EXPECT_FALSE(m.Matches(L"Hi"));
1713 EXPECT_FALSE(m.Matches(NULL));
1714
1715 Matcher<const ::wstring&> m2 = StrCaseEq(L"Hello");
1716 EXPECT_TRUE(m2.Matches(L"hello"));
1717 EXPECT_FALSE(m2.Matches(L"Hi"));
1718}
1719
1720TEST(GlobalWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1721 ::wstring str1(L"oabocdooeoo");
1722 ::wstring str2(L"OABOCDOOEOO");
1723 Matcher<const ::wstring&> m0 = StrCaseEq(str1);
1724 EXPECT_FALSE(m0.Matches(str2 + ::wstring(1, L'\0')));
1725
1726 str1[3] = str2[3] = L'\0';
1727 Matcher<const ::wstring&> m1 = StrCaseEq(str1);
1728 EXPECT_TRUE(m1.Matches(str2));
1729
1730 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1731 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1732 Matcher<const ::wstring&> m2 = StrCaseEq(str1);
1733 str1[9] = str2[9] = L'\0';
1734 EXPECT_FALSE(m2.Matches(str2));
1735
1736 Matcher<const ::wstring&> m3 = StrCaseEq(str1);
1737 EXPECT_TRUE(m3.Matches(str2));
1738
1739 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1740 str2.append(1, L'\0');
1741 EXPECT_FALSE(m3.Matches(str2));
1742 EXPECT_FALSE(m3.Matches(::wstring(str2, 0, 9)));
1743}
1744
1745TEST(GlobalWideStrCaseEqTest, CanDescribeSelf) {
1746 Matcher< ::wstring> m = StrCaseEq(L"Hi");
1747 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1748}
1749
1750TEST(GlobalWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1751 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1752 EXPECT_TRUE(m.Matches(L"Hi"));
1753 EXPECT_TRUE(m.Matches(NULL));
1754 EXPECT_FALSE(m.Matches(L"Hello"));
1755 EXPECT_FALSE(m.Matches(L"hello"));
1756
1757 Matcher< ::wstring> m2 = StrCaseNe(::wstring(L"Hello"));
1758 EXPECT_TRUE(m2.Matches(L""));
1759 EXPECT_FALSE(m2.Matches(L"Hello"));
1760}
1761
1762TEST(GlobalWideStrCaseNeTest, CanDescribeSelf) {
1763 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001764 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001765}
1766
1767// Tests that HasSubstr() works for matching wstring-typed values.
1768TEST(GlobalWideHasSubstrTest, WorksForStringClasses) {
1769 const Matcher< ::wstring> m1 = HasSubstr(L"foo");
1770 EXPECT_TRUE(m1.Matches(::wstring(L"I love food.")));
1771 EXPECT_FALSE(m1.Matches(::wstring(L"tofo")));
1772
1773 const Matcher<const ::wstring&> m2 = HasSubstr(L"foo");
1774 EXPECT_TRUE(m2.Matches(::wstring(L"I love food.")));
1775 EXPECT_FALSE(m2.Matches(::wstring(L"tofo")));
1776}
1777
1778// Tests that HasSubstr() works for matching C-wide-string-typed values.
1779TEST(GlobalWideHasSubstrTest, WorksForCStrings) {
1780 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1781 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1782 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1783 EXPECT_FALSE(m1.Matches(NULL));
1784
1785 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1786 EXPECT_TRUE(m2.Matches(L"I love food."));
1787 EXPECT_FALSE(m2.Matches(L"tofo"));
1788 EXPECT_FALSE(m2.Matches(NULL));
1789}
1790
1791// Tests that HasSubstr(s) describes itself properly.
1792TEST(GlobalWideHasSubstrTest, CanDescribeSelf) {
1793 Matcher< ::wstring> m = HasSubstr(L"foo\n\"");
1794 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1795}
1796
1797// Tests StartsWith(s).
1798
1799TEST(GlobalWideStartsWithTest, MatchesStringWithGivenPrefix) {
1800 const Matcher<const wchar_t*> m1 = StartsWith(::wstring(L""));
1801 EXPECT_TRUE(m1.Matches(L"Hi"));
1802 EXPECT_TRUE(m1.Matches(L""));
1803 EXPECT_FALSE(m1.Matches(NULL));
1804
1805 const Matcher<const ::wstring&> m2 = StartsWith(L"Hi");
1806 EXPECT_TRUE(m2.Matches(L"Hi"));
1807 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1808 EXPECT_TRUE(m2.Matches(L"High"));
1809 EXPECT_FALSE(m2.Matches(L"H"));
1810 EXPECT_FALSE(m2.Matches(L" Hi"));
1811}
1812
1813TEST(GlobalWideStartsWithTest, CanDescribeSelf) {
1814 Matcher<const ::wstring> m = StartsWith(L"Hi");
1815 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1816}
1817
1818// Tests EndsWith(s).
1819
1820TEST(GlobalWideEndsWithTest, MatchesStringWithGivenSuffix) {
1821 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1822 EXPECT_TRUE(m1.Matches(L"Hi"));
1823 EXPECT_TRUE(m1.Matches(L""));
1824 EXPECT_FALSE(m1.Matches(NULL));
1825
1826 const Matcher<const ::wstring&> m2 = EndsWith(::wstring(L"Hi"));
1827 EXPECT_TRUE(m2.Matches(L"Hi"));
1828 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1829 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1830 EXPECT_FALSE(m2.Matches(L"i"));
1831 EXPECT_FALSE(m2.Matches(L"Hi "));
1832}
1833
1834TEST(GlobalWideEndsWithTest, CanDescribeSelf) {
1835 Matcher<const ::wstring> m = EndsWith(L"Hi");
1836 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1837}
1838
1839#endif // GTEST_HAS_GLOBAL_WSTRING
1840
1841
1842typedef ::std::tr1::tuple<long, int> Tuple2; // NOLINT
1843
1844// Tests that Eq() matches a 2-tuple where the first field == the
1845// second field.
1846TEST(Eq2Test, MatchesEqualArguments) {
1847 Matcher<const Tuple2&> m = Eq();
1848 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1849 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1850}
1851
1852// Tests that Eq() describes itself properly.
1853TEST(Eq2Test, CanDescribeSelf) {
1854 Matcher<const Tuple2&> m = Eq();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001855 EXPECT_EQ("are an equal pair", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001856}
1857
1858// Tests that Ge() matches a 2-tuple where the first field >= the
1859// second field.
1860TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) {
1861 Matcher<const Tuple2&> m = Ge();
1862 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1863 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1864 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1865}
1866
1867// Tests that Ge() describes itself properly.
1868TEST(Ge2Test, CanDescribeSelf) {
1869 Matcher<const Tuple2&> m = Ge();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001870 EXPECT_EQ("are a pair where the first >= the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001871}
1872
1873// Tests that Gt() matches a 2-tuple where the first field > the
1874// second field.
1875TEST(Gt2Test, MatchesGreaterThanArguments) {
1876 Matcher<const Tuple2&> m = Gt();
1877 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1878 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1879 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1880}
1881
1882// Tests that Gt() describes itself properly.
1883TEST(Gt2Test, CanDescribeSelf) {
1884 Matcher<const Tuple2&> m = Gt();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001885 EXPECT_EQ("are a pair where the first > the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001886}
1887
1888// Tests that Le() matches a 2-tuple where the first field <= the
1889// second field.
1890TEST(Le2Test, MatchesLessThanOrEqualArguments) {
1891 Matcher<const Tuple2&> m = Le();
1892 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1893 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1894 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1895}
1896
1897// Tests that Le() describes itself properly.
1898TEST(Le2Test, CanDescribeSelf) {
1899 Matcher<const Tuple2&> m = Le();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001900 EXPECT_EQ("are a pair where the first <= the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001901}
1902
1903// Tests that Lt() matches a 2-tuple where the first field < the
1904// second field.
1905TEST(Lt2Test, MatchesLessThanArguments) {
1906 Matcher<const Tuple2&> m = Lt();
1907 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1908 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1909 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1910}
1911
1912// Tests that Lt() describes itself properly.
1913TEST(Lt2Test, CanDescribeSelf) {
1914 Matcher<const Tuple2&> m = Lt();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001915 EXPECT_EQ("are a pair where the first < the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001916}
1917
1918// Tests that Ne() matches a 2-tuple where the first field != the
1919// second field.
1920TEST(Ne2Test, MatchesUnequalArguments) {
1921 Matcher<const Tuple2&> m = Ne();
1922 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1923 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1924 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1925}
1926
1927// Tests that Ne() describes itself properly.
1928TEST(Ne2Test, CanDescribeSelf) {
1929 Matcher<const Tuple2&> m = Ne();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001930 EXPECT_EQ("are an unequal pair", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001931}
1932
1933// Tests that Not(m) matches any value that doesn't match m.
1934TEST(NotTest, NegatesMatcher) {
1935 Matcher<int> m;
1936 m = Not(Eq(2));
1937 EXPECT_TRUE(m.Matches(3));
1938 EXPECT_FALSE(m.Matches(2));
1939}
1940
1941// Tests that Not(m) describes itself properly.
1942TEST(NotTest, CanDescribeSelf) {
1943 Matcher<int> m = Not(Eq(5));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001944 EXPECT_EQ("isn't equal to 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001945}
1946
zhanyong.wan18490652009-05-11 18:54:08 +00001947// Tests that monomorphic matchers are safely cast by the Not matcher.
1948TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {
1949 // greater_than_5 is a monomorphic matcher.
1950 Matcher<int> greater_than_5 = Gt(5);
1951
1952 Matcher<const int&> m = Not(greater_than_5);
1953 Matcher<int&> m2 = Not(greater_than_5);
1954 Matcher<int&> m3 = Not(m);
1955}
1956
zhanyong.wan02c15052010-06-09 19:21:30 +00001957// Helper to allow easy testing of AllOf matchers with num parameters.
1958void AllOfMatches(int num, const Matcher<int>& m) {
1959 SCOPED_TRACE(Describe(m));
1960 EXPECT_TRUE(m.Matches(0));
1961 for (int i = 1; i <= num; ++i) {
1962 EXPECT_FALSE(m.Matches(i));
1963 }
1964 EXPECT_TRUE(m.Matches(num + 1));
1965}
1966
shiqiane35fdd92008-12-10 05:08:54 +00001967// Tests that AllOf(m1, ..., mn) matches any value that matches all of
1968// the given matchers.
1969TEST(AllOfTest, MatchesWhenAllMatch) {
1970 Matcher<int> m;
1971 m = AllOf(Le(2), Ge(1));
1972 EXPECT_TRUE(m.Matches(1));
1973 EXPECT_TRUE(m.Matches(2));
1974 EXPECT_FALSE(m.Matches(0));
1975 EXPECT_FALSE(m.Matches(3));
1976
1977 m = AllOf(Gt(0), Ne(1), Ne(2));
1978 EXPECT_TRUE(m.Matches(3));
1979 EXPECT_FALSE(m.Matches(2));
1980 EXPECT_FALSE(m.Matches(1));
1981 EXPECT_FALSE(m.Matches(0));
1982
1983 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
1984 EXPECT_TRUE(m.Matches(4));
1985 EXPECT_FALSE(m.Matches(3));
1986 EXPECT_FALSE(m.Matches(2));
1987 EXPECT_FALSE(m.Matches(1));
1988 EXPECT_FALSE(m.Matches(0));
1989
1990 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
1991 EXPECT_TRUE(m.Matches(0));
1992 EXPECT_TRUE(m.Matches(1));
1993 EXPECT_FALSE(m.Matches(3));
zhanyong.wan02c15052010-06-09 19:21:30 +00001994
1995 // The following tests for varying number of sub-matchers. Due to the way
1996 // the sub-matchers are handled it is enough to test every sub-matcher once
1997 // with sub-matchers using the same matcher type. Varying matcher types are
1998 // checked for above.
1999 AllOfMatches(2, AllOf(Ne(1), Ne(2)));
2000 AllOfMatches(3, AllOf(Ne(1), Ne(2), Ne(3)));
2001 AllOfMatches(4, AllOf(Ne(1), Ne(2), Ne(3), Ne(4)));
2002 AllOfMatches(5, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5)));
2003 AllOfMatches(6, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6)));
2004 AllOfMatches(7, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7)));
2005 AllOfMatches(8, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
2006 Ne(8)));
2007 AllOfMatches(9, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
2008 Ne(8), Ne(9)));
2009 AllOfMatches(10, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8),
2010 Ne(9), Ne(10)));
shiqiane35fdd92008-12-10 05:08:54 +00002011}
2012
2013// Tests that AllOf(m1, ..., mn) describes itself properly.
2014TEST(AllOfTest, CanDescribeSelf) {
2015 Matcher<int> m;
2016 m = AllOf(Le(2), Ge(1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002017 EXPECT_EQ("(is <= 2) and (is >= 1)", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00002018
2019 m = AllOf(Gt(0), Ne(1), Ne(2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002020 EXPECT_EQ("(is > 0) and "
2021 "((isn't equal to 1) and "
2022 "(isn't equal to 2))",
shiqiane35fdd92008-12-10 05:08:54 +00002023 Describe(m));
2024
2025
2026 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
jgm79a367e2012-04-10 16:02:11 +00002027 EXPECT_EQ("((is > 0) and "
2028 "(isn't equal to 1)) and "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002029 "((isn't equal to 2) and "
jgm79a367e2012-04-10 16:02:11 +00002030 "(isn't equal to 3))",
shiqiane35fdd92008-12-10 05:08:54 +00002031 Describe(m));
2032
2033
2034 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
jgm79a367e2012-04-10 16:02:11 +00002035 EXPECT_EQ("((is >= 0) and "
2036 "(is < 10)) and "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002037 "((isn't equal to 3) and "
2038 "((isn't equal to 5) and "
jgm79a367e2012-04-10 16:02:11 +00002039 "(isn't equal to 7)))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002040 Describe(m));
2041}
2042
2043// Tests that AllOf(m1, ..., mn) describes its negation properly.
2044TEST(AllOfTest, CanDescribeNegation) {
2045 Matcher<int> m;
2046 m = AllOf(Le(2), Ge(1));
2047 EXPECT_EQ("(isn't <= 2) or "
2048 "(isn't >= 1)",
2049 DescribeNegation(m));
2050
2051 m = AllOf(Gt(0), Ne(1), Ne(2));
2052 EXPECT_EQ("(isn't > 0) or "
2053 "((is equal to 1) or "
2054 "(is equal to 2))",
2055 DescribeNegation(m));
2056
2057
2058 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
jgm79a367e2012-04-10 16:02:11 +00002059 EXPECT_EQ("((isn't > 0) or "
2060 "(is equal to 1)) or "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002061 "((is equal to 2) or "
jgm79a367e2012-04-10 16:02:11 +00002062 "(is equal to 3))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002063 DescribeNegation(m));
2064
2065
2066 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
jgm79a367e2012-04-10 16:02:11 +00002067 EXPECT_EQ("((isn't >= 0) or "
2068 "(isn't < 10)) or "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002069 "((is equal to 3) or "
2070 "((is equal to 5) or "
jgm79a367e2012-04-10 16:02:11 +00002071 "(is equal to 7)))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002072 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00002073}
2074
zhanyong.wan18490652009-05-11 18:54:08 +00002075// Tests that monomorphic matchers are safely cast by the AllOf matcher.
2076TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
2077 // greater_than_5 and less_than_10 are monomorphic matchers.
2078 Matcher<int> greater_than_5 = Gt(5);
2079 Matcher<int> less_than_10 = Lt(10);
2080
2081 Matcher<const int&> m = AllOf(greater_than_5, less_than_10);
2082 Matcher<int&> m2 = AllOf(greater_than_5, less_than_10);
2083 Matcher<int&> m3 = AllOf(greater_than_5, m2);
2084
2085 // Tests that BothOf works when composing itself.
2086 Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10);
2087 Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);
2088}
2089
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002090TEST(AllOfTest, ExplainsResult) {
2091 Matcher<int> m;
2092
2093 // Successful match. Both matchers need to explain. The second
2094 // matcher doesn't give an explanation, so only the first matcher's
2095 // explanation is printed.
2096 m = AllOf(GreaterThan(10), Lt(30));
2097 EXPECT_EQ("which is 15 more than 10", Explain(m, 25));
2098
2099 // Successful match. Both matchers need to explain.
2100 m = AllOf(GreaterThan(10), GreaterThan(20));
2101 EXPECT_EQ("which is 20 more than 10, and which is 10 more than 20",
2102 Explain(m, 30));
2103
2104 // Successful match. All matchers need to explain. The second
2105 // matcher doesn't given an explanation.
2106 m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20));
2107 EXPECT_EQ("which is 15 more than 10, and which is 5 more than 20",
2108 Explain(m, 25));
2109
2110 // Successful match. All matchers need to explain.
2111 m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2112 EXPECT_EQ("which is 30 more than 10, and which is 20 more than 20, "
2113 "and which is 10 more than 30",
2114 Explain(m, 40));
2115
2116 // Failed match. The first matcher, which failed, needs to
2117 // explain.
2118 m = AllOf(GreaterThan(10), GreaterThan(20));
2119 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
2120
2121 // Failed match. The second matcher, which failed, needs to
2122 // explain. Since it doesn't given an explanation, nothing is
2123 // printed.
2124 m = AllOf(GreaterThan(10), Lt(30));
2125 EXPECT_EQ("", Explain(m, 40));
2126
2127 // Failed match. The second matcher, which failed, needs to
2128 // explain.
2129 m = AllOf(GreaterThan(10), GreaterThan(20));
2130 EXPECT_EQ("which is 5 less than 20", Explain(m, 15));
2131}
2132
zhanyong.wan02c15052010-06-09 19:21:30 +00002133// Helper to allow easy testing of AnyOf matchers with num parameters.
2134void AnyOfMatches(int num, const Matcher<int>& m) {
2135 SCOPED_TRACE(Describe(m));
2136 EXPECT_FALSE(m.Matches(0));
2137 for (int i = 1; i <= num; ++i) {
2138 EXPECT_TRUE(m.Matches(i));
2139 }
2140 EXPECT_FALSE(m.Matches(num + 1));
2141}
2142
shiqiane35fdd92008-12-10 05:08:54 +00002143// Tests that AnyOf(m1, ..., mn) matches any value that matches at
2144// least one of the given matchers.
2145TEST(AnyOfTest, MatchesWhenAnyMatches) {
2146 Matcher<int> m;
2147 m = AnyOf(Le(1), Ge(3));
2148 EXPECT_TRUE(m.Matches(1));
2149 EXPECT_TRUE(m.Matches(4));
2150 EXPECT_FALSE(m.Matches(2));
2151
2152 m = AnyOf(Lt(0), Eq(1), Eq(2));
2153 EXPECT_TRUE(m.Matches(-1));
2154 EXPECT_TRUE(m.Matches(1));
2155 EXPECT_TRUE(m.Matches(2));
2156 EXPECT_FALSE(m.Matches(0));
2157
2158 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2159 EXPECT_TRUE(m.Matches(-1));
2160 EXPECT_TRUE(m.Matches(1));
2161 EXPECT_TRUE(m.Matches(2));
2162 EXPECT_TRUE(m.Matches(3));
2163 EXPECT_FALSE(m.Matches(0));
2164
2165 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2166 EXPECT_TRUE(m.Matches(0));
2167 EXPECT_TRUE(m.Matches(11));
2168 EXPECT_TRUE(m.Matches(3));
2169 EXPECT_FALSE(m.Matches(2));
zhanyong.wan02c15052010-06-09 19:21:30 +00002170
2171 // The following tests for varying number of sub-matchers. Due to the way
2172 // the sub-matchers are handled it is enough to test every sub-matcher once
2173 // with sub-matchers using the same matcher type. Varying matcher types are
2174 // checked for above.
2175 AnyOfMatches(2, AnyOf(1, 2));
2176 AnyOfMatches(3, AnyOf(1, 2, 3));
2177 AnyOfMatches(4, AnyOf(1, 2, 3, 4));
2178 AnyOfMatches(5, AnyOf(1, 2, 3, 4, 5));
2179 AnyOfMatches(6, AnyOf(1, 2, 3, 4, 5, 6));
2180 AnyOfMatches(7, AnyOf(1, 2, 3, 4, 5, 6, 7));
2181 AnyOfMatches(8, AnyOf(1, 2, 3, 4, 5, 6, 7, 8));
2182 AnyOfMatches(9, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9));
2183 AnyOfMatches(10, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
shiqiane35fdd92008-12-10 05:08:54 +00002184}
2185
2186// Tests that AnyOf(m1, ..., mn) describes itself properly.
2187TEST(AnyOfTest, CanDescribeSelf) {
2188 Matcher<int> m;
2189 m = AnyOf(Le(1), Ge(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002190 EXPECT_EQ("(is <= 1) or (is >= 3)",
shiqiane35fdd92008-12-10 05:08:54 +00002191 Describe(m));
2192
2193 m = AnyOf(Lt(0), Eq(1), Eq(2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002194 EXPECT_EQ("(is < 0) or "
shiqiane35fdd92008-12-10 05:08:54 +00002195 "((is equal to 1) or (is equal to 2))",
2196 Describe(m));
2197
2198 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
jgm79a367e2012-04-10 16:02:11 +00002199 EXPECT_EQ("((is < 0) or "
2200 "(is equal to 1)) or "
shiqiane35fdd92008-12-10 05:08:54 +00002201 "((is equal to 2) or "
jgm79a367e2012-04-10 16:02:11 +00002202 "(is equal to 3))",
shiqiane35fdd92008-12-10 05:08:54 +00002203 Describe(m));
2204
2205 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
jgm79a367e2012-04-10 16:02:11 +00002206 EXPECT_EQ("((is <= 0) or "
2207 "(is > 10)) or "
shiqiane35fdd92008-12-10 05:08:54 +00002208 "((is equal to 3) or "
2209 "((is equal to 5) or "
jgm79a367e2012-04-10 16:02:11 +00002210 "(is equal to 7)))",
shiqiane35fdd92008-12-10 05:08:54 +00002211 Describe(m));
2212}
2213
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002214// Tests that AnyOf(m1, ..., mn) describes its negation properly.
2215TEST(AnyOfTest, CanDescribeNegation) {
2216 Matcher<int> m;
2217 m = AnyOf(Le(1), Ge(3));
2218 EXPECT_EQ("(isn't <= 1) and (isn't >= 3)",
2219 DescribeNegation(m));
2220
2221 m = AnyOf(Lt(0), Eq(1), Eq(2));
2222 EXPECT_EQ("(isn't < 0) and "
2223 "((isn't equal to 1) and (isn't equal to 2))",
2224 DescribeNegation(m));
2225
2226 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
jgm79a367e2012-04-10 16:02:11 +00002227 EXPECT_EQ("((isn't < 0) and "
2228 "(isn't equal to 1)) and "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002229 "((isn't equal to 2) and "
jgm79a367e2012-04-10 16:02:11 +00002230 "(isn't equal to 3))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002231 DescribeNegation(m));
2232
2233 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
jgm79a367e2012-04-10 16:02:11 +00002234 EXPECT_EQ("((isn't <= 0) and "
2235 "(isn't > 10)) and "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002236 "((isn't equal to 3) and "
2237 "((isn't equal to 5) and "
jgm79a367e2012-04-10 16:02:11 +00002238 "(isn't equal to 7)))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002239 DescribeNegation(m));
2240}
2241
zhanyong.wan18490652009-05-11 18:54:08 +00002242// Tests that monomorphic matchers are safely cast by the AnyOf matcher.
2243TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
2244 // greater_than_5 and less_than_10 are monomorphic matchers.
2245 Matcher<int> greater_than_5 = Gt(5);
2246 Matcher<int> less_than_10 = Lt(10);
2247
2248 Matcher<const int&> m = AnyOf(greater_than_5, less_than_10);
2249 Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10);
2250 Matcher<int&> m3 = AnyOf(greater_than_5, m2);
2251
2252 // Tests that EitherOf works when composing itself.
2253 Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10);
2254 Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);
2255}
2256
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002257TEST(AnyOfTest, ExplainsResult) {
2258 Matcher<int> m;
2259
2260 // Failed match. Both matchers need to explain. The second
2261 // matcher doesn't give an explanation, so only the first matcher's
2262 // explanation is printed.
2263 m = AnyOf(GreaterThan(10), Lt(0));
2264 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
2265
2266 // Failed match. Both matchers need to explain.
2267 m = AnyOf(GreaterThan(10), GreaterThan(20));
2268 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20",
2269 Explain(m, 5));
2270
2271 // Failed match. All matchers need to explain. The second
2272 // matcher doesn't given an explanation.
2273 m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30));
2274 EXPECT_EQ("which is 5 less than 10, and which is 25 less than 30",
2275 Explain(m, 5));
2276
2277 // Failed match. All matchers need to explain.
2278 m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2279 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20, "
2280 "and which is 25 less than 30",
2281 Explain(m, 5));
2282
2283 // Successful match. The first matcher, which succeeded, needs to
2284 // explain.
2285 m = AnyOf(GreaterThan(10), GreaterThan(20));
2286 EXPECT_EQ("which is 5 more than 10", Explain(m, 15));
2287
2288 // Successful match. The second matcher, which succeeded, needs to
2289 // explain. Since it doesn't given an explanation, nothing is
2290 // printed.
2291 m = AnyOf(GreaterThan(10), Lt(30));
2292 EXPECT_EQ("", Explain(m, 0));
2293
2294 // Successful match. The second matcher, which succeeded, needs to
2295 // explain.
2296 m = AnyOf(GreaterThan(30), GreaterThan(20));
2297 EXPECT_EQ("which is 5 more than 20", Explain(m, 25));
2298}
2299
shiqiane35fdd92008-12-10 05:08:54 +00002300// The following predicate function and predicate functor are for
2301// testing the Truly(predicate) matcher.
2302
2303// Returns non-zero if the input is positive. Note that the return
2304// type of this function is not bool. It's OK as Truly() accepts any
2305// unary function or functor whose return type can be implicitly
2306// converted to bool.
2307int IsPositive(double x) {
2308 return x > 0 ? 1 : 0;
2309}
2310
2311// This functor returns true if the input is greater than the given
2312// number.
2313class IsGreaterThan {
2314 public:
2315 explicit IsGreaterThan(int threshold) : threshold_(threshold) {}
2316
2317 bool operator()(int n) const { return n > threshold_; }
zhanyong.wan32de5f52009-12-23 00:13:23 +00002318
shiqiane35fdd92008-12-10 05:08:54 +00002319 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002320 int threshold_;
shiqiane35fdd92008-12-10 05:08:54 +00002321};
2322
2323// For testing Truly().
2324const int foo = 0;
2325
2326// This predicate returns true iff the argument references foo and has
2327// a zero value.
2328bool ReferencesFooAndIsZero(const int& n) {
2329 return (&n == &foo) && (n == 0);
2330}
2331
2332// Tests that Truly(predicate) matches what satisfies the given
2333// predicate.
2334TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {
2335 Matcher<double> m = Truly(IsPositive);
2336 EXPECT_TRUE(m.Matches(2.0));
2337 EXPECT_FALSE(m.Matches(-1.5));
2338}
2339
2340// Tests that Truly(predicate_functor) works too.
2341TEST(TrulyTest, CanBeUsedWithFunctor) {
2342 Matcher<int> m = Truly(IsGreaterThan(5));
2343 EXPECT_TRUE(m.Matches(6));
2344 EXPECT_FALSE(m.Matches(4));
2345}
2346
zhanyong.wan8d3dc0c2011-04-14 19:37:06 +00002347// A class that can be implicitly converted to bool.
2348class ConvertibleToBool {
2349 public:
2350 explicit ConvertibleToBool(int number) : number_(number) {}
2351 operator bool() const { return number_ != 0; }
2352
2353 private:
2354 int number_;
2355};
2356
2357ConvertibleToBool IsNotZero(int number) {
2358 return ConvertibleToBool(number);
2359}
2360
2361// Tests that the predicate used in Truly() may return a class that's
2362// implicitly convertible to bool, even when the class has no
2363// operator!().
2364TEST(TrulyTest, PredicateCanReturnAClassConvertibleToBool) {
2365 Matcher<int> m = Truly(IsNotZero);
2366 EXPECT_TRUE(m.Matches(1));
2367 EXPECT_FALSE(m.Matches(0));
2368}
2369
shiqiane35fdd92008-12-10 05:08:54 +00002370// Tests that Truly(predicate) can describe itself properly.
2371TEST(TrulyTest, CanDescribeSelf) {
2372 Matcher<double> m = Truly(IsPositive);
2373 EXPECT_EQ("satisfies the given predicate",
2374 Describe(m));
2375}
2376
2377// Tests that Truly(predicate) works when the matcher takes its
2378// argument by reference.
2379TEST(TrulyTest, WorksForByRefArguments) {
2380 Matcher<const int&> m = Truly(ReferencesFooAndIsZero);
2381 EXPECT_TRUE(m.Matches(foo));
2382 int n = 0;
2383 EXPECT_FALSE(m.Matches(n));
2384}
2385
2386// Tests that Matches(m) is a predicate satisfied by whatever that
2387// matches matcher m.
2388TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {
2389 EXPECT_TRUE(Matches(Ge(0))(1));
2390 EXPECT_FALSE(Matches(Eq('a'))('b'));
2391}
2392
2393// Tests that Matches(m) works when the matcher takes its argument by
2394// reference.
2395TEST(MatchesTest, WorksOnByRefArguments) {
2396 int m = 0, n = 0;
2397 EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n));
2398 EXPECT_FALSE(Matches(Ref(m))(n));
2399}
2400
2401// Tests that a Matcher on non-reference type can be used in
2402// Matches().
2403TEST(MatchesTest, WorksWithMatcherOnNonRefType) {
2404 Matcher<int> eq5 = Eq(5);
2405 EXPECT_TRUE(Matches(eq5)(5));
2406 EXPECT_FALSE(Matches(eq5)(2));
2407}
2408
zhanyong.wanb8243162009-06-04 05:48:20 +00002409// Tests Value(value, matcher). Since Value() is a simple wrapper for
2410// Matches(), which has been tested already, we don't spend a lot of
2411// effort on testing Value().
2412TEST(ValueTest, WorksWithPolymorphicMatcher) {
2413 EXPECT_TRUE(Value("hi", StartsWith("h")));
2414 EXPECT_FALSE(Value(5, Gt(10)));
2415}
2416
2417TEST(ValueTest, WorksWithMonomorphicMatcher) {
2418 const Matcher<int> is_zero = Eq(0);
2419 EXPECT_TRUE(Value(0, is_zero));
2420 EXPECT_FALSE(Value('a', is_zero));
2421
2422 int n = 0;
2423 const Matcher<const int&> ref_n = Ref(n);
2424 EXPECT_TRUE(Value(n, ref_n));
2425 EXPECT_FALSE(Value(1, ref_n));
2426}
2427
zhanyong.wana862f1d2010-03-15 21:23:04 +00002428TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) {
zhanyong.wan34b034c2010-03-05 21:23:23 +00002429 StringMatchResultListener listener1;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002430 EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002431 EXPECT_EQ("% 2 == 0", listener1.str());
2432
2433 StringMatchResultListener listener2;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002434 EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002435 EXPECT_EQ("", listener2.str());
2436}
2437
zhanyong.wana862f1d2010-03-15 21:23:04 +00002438TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) {
zhanyong.wan34b034c2010-03-05 21:23:23 +00002439 const Matcher<int> is_even = PolymorphicIsEven();
2440 StringMatchResultListener listener1;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002441 EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002442 EXPECT_EQ("% 2 == 0", listener1.str());
2443
2444 const Matcher<const double&> is_zero = Eq(0);
2445 StringMatchResultListener listener2;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002446 EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002447 EXPECT_EQ("", listener2.str());
2448}
2449
zhanyong.wana862f1d2010-03-15 21:23:04 +00002450MATCHER_P(Really, inner_matcher, "") {
2451 return ExplainMatchResult(inner_matcher, arg, result_listener);
2452}
2453
2454TEST(ExplainMatchResultTest, WorksInsideMATCHER) {
2455 EXPECT_THAT(0, Really(Eq(0)));
2456}
2457
zhanyong.wanbf550852009-06-09 06:09:53 +00002458TEST(AllArgsTest, WorksForTuple) {
2459 EXPECT_THAT(make_tuple(1, 2L), AllArgs(Lt()));
2460 EXPECT_THAT(make_tuple(2L, 1), Not(AllArgs(Lt())));
2461}
2462
2463TEST(AllArgsTest, WorksForNonTuple) {
2464 EXPECT_THAT(42, AllArgs(Gt(0)));
2465 EXPECT_THAT('a', Not(AllArgs(Eq('b'))));
2466}
2467
2468class AllArgsHelper {
2469 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002470 AllArgsHelper() {}
2471
zhanyong.wanbf550852009-06-09 06:09:53 +00002472 MOCK_METHOD2(Helper, int(char x, int y));
zhanyong.wan32de5f52009-12-23 00:13:23 +00002473
2474 private:
2475 GTEST_DISALLOW_COPY_AND_ASSIGN_(AllArgsHelper);
zhanyong.wanbf550852009-06-09 06:09:53 +00002476};
2477
2478TEST(AllArgsTest, WorksInWithClause) {
2479 AllArgsHelper helper;
2480 ON_CALL(helper, Helper(_, _))
2481 .With(AllArgs(Lt()))
2482 .WillByDefault(Return(1));
2483 EXPECT_CALL(helper, Helper(_, _));
2484 EXPECT_CALL(helper, Helper(_, _))
2485 .With(AllArgs(Gt()))
2486 .WillOnce(Return(2));
2487
2488 EXPECT_EQ(1, helper.Helper('\1', 2));
2489 EXPECT_EQ(2, helper.Helper('a', 1));
2490}
2491
shiqiane35fdd92008-12-10 05:08:54 +00002492// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2493// matches the matcher.
2494TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {
2495 ASSERT_THAT(5, Ge(2)) << "This should succeed.";
2496 ASSERT_THAT("Foo", EndsWith("oo"));
2497 EXPECT_THAT(2, AllOf(Le(7), Ge(0))) << "This should succeed too.";
2498 EXPECT_THAT("Hello", StartsWith("Hell"));
2499}
2500
2501// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2502// doesn't match the matcher.
2503TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {
2504 // 'n' must be static as it is used in an EXPECT_FATAL_FAILURE(),
2505 // which cannot reference auto variables.
zhanyong.wan736baa82010-09-27 17:44:16 +00002506 static unsigned short n; // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00002507 n = 5;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002508
2509 // VC++ prior to version 8.0 SP1 has a bug where it will not see any
2510 // functions declared in the namespace scope from within nested classes.
2511 // EXPECT/ASSERT_(NON)FATAL_FAILURE macros use nested classes so that all
2512 // namespace-level functions invoked inside them need to be explicitly
2513 // resolved.
2514 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Gt(10)),
shiqiane35fdd92008-12-10 05:08:54 +00002515 "Value of: n\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002516 "Expected: is > 10\n"
zhanyong.wan736baa82010-09-27 17:44:16 +00002517 " Actual: 5" + OfType("unsigned short"));
shiqiane35fdd92008-12-10 05:08:54 +00002518 n = 0;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002519 EXPECT_NONFATAL_FAILURE(
2520 EXPECT_THAT(n, ::testing::AllOf(::testing::Le(7), ::testing::Ge(5))),
2521 "Value of: n\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002522 "Expected: (is <= 7) and (is >= 5)\n"
zhanyong.wan736baa82010-09-27 17:44:16 +00002523 " Actual: 0" + OfType("unsigned short"));
shiqiane35fdd92008-12-10 05:08:54 +00002524}
2525
2526// Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument
2527// has a reference type.
2528TEST(MatcherAssertionTest, WorksForByRefArguments) {
2529 // We use a static variable here as EXPECT_FATAL_FAILURE() cannot
2530 // reference auto variables.
2531 static int n;
2532 n = 0;
2533 EXPECT_THAT(n, AllOf(Le(7), Ref(n)));
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002534 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
shiqiane35fdd92008-12-10 05:08:54 +00002535 "Value of: n\n"
2536 "Expected: does not reference the variable @");
2537 // Tests the "Actual" part.
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002538 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
zhanyong.wan736baa82010-09-27 17:44:16 +00002539 "Actual: 0" + OfType("int") + ", which is located @");
shiqiane35fdd92008-12-10 05:08:54 +00002540}
2541
zhanyong.wan95b12332009-09-25 18:55:50 +00002542#if !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002543// Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is
2544// monomorphic.
zhanyong.wan95b12332009-09-25 18:55:50 +00002545
2546// ASSERT_THAT("hello", starts_with_he) fails to compile with Nokia's
2547// Symbian compiler: it tries to compile
2548// template<T, U> class MatcherCastImpl { ...
zhanyong.wandb22c222010-01-28 21:52:29 +00002549// virtual bool MatchAndExplain(T x, ...) const {
2550// return source_matcher_.MatchAndExplain(static_cast<U>(x), ...);
zhanyong.wan95b12332009-09-25 18:55:50 +00002551// with U == string and T == const char*
2552// With ASSERT_THAT("hello"...) changed to ASSERT_THAT(string("hello") ... )
2553// the compiler silently crashes with no output.
2554// If MatcherCastImpl is changed to use U(x) instead of static_cast<U>(x)
2555// the code compiles but the converted string is bogus.
shiqiane35fdd92008-12-10 05:08:54 +00002556TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
2557 Matcher<const char*> starts_with_he = StartsWith("he");
2558 ASSERT_THAT("hello", starts_with_he);
2559
2560 Matcher<const string&> ends_with_ok = EndsWith("ok");
2561 ASSERT_THAT("book", ends_with_ok);
zhanyong.wan736baa82010-09-27 17:44:16 +00002562 const string bad = "bad";
2563 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(bad, ends_with_ok),
2564 "Value of: bad\n"
2565 "Expected: ends with \"ok\"\n"
2566 " Actual: \"bad\"");
shiqiane35fdd92008-12-10 05:08:54 +00002567 Matcher<int> is_greater_than_5 = Gt(5);
2568 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5),
2569 "Value of: 5\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002570 "Expected: is > 5\n"
zhanyong.wan736baa82010-09-27 17:44:16 +00002571 " Actual: 5" + OfType("int"));
shiqiane35fdd92008-12-10 05:08:54 +00002572}
zhanyong.wan95b12332009-09-25 18:55:50 +00002573#endif // !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002574
2575// Tests floating-point matchers.
2576template <typename RawType>
2577class FloatingPointTest : public testing::Test {
2578 protected:
2579 typedef typename testing::internal::FloatingPoint<RawType> Floating;
2580 typedef typename Floating::Bits Bits;
2581
2582 virtual void SetUp() {
2583 const size_t max_ulps = Floating::kMaxUlps;
2584
2585 // The bits that represent 0.0.
2586 const Bits zero_bits = Floating(0).bits();
2587
2588 // Makes some numbers close to 0.0.
2589 close_to_positive_zero_ = Floating::ReinterpretBits(zero_bits + max_ulps/2);
2590 close_to_negative_zero_ = -Floating::ReinterpretBits(
2591 zero_bits + max_ulps - max_ulps/2);
2592 further_from_negative_zero_ = -Floating::ReinterpretBits(
2593 zero_bits + max_ulps + 1 - max_ulps/2);
2594
2595 // The bits that represent 1.0.
2596 const Bits one_bits = Floating(1).bits();
2597
2598 // Makes some numbers close to 1.0.
2599 close_to_one_ = Floating::ReinterpretBits(one_bits + max_ulps);
2600 further_from_one_ = Floating::ReinterpretBits(one_bits + max_ulps + 1);
2601
2602 // +infinity.
2603 infinity_ = Floating::Infinity();
2604
2605 // The bits that represent +infinity.
2606 const Bits infinity_bits = Floating(infinity_).bits();
2607
2608 // Makes some numbers close to infinity.
2609 close_to_infinity_ = Floating::ReinterpretBits(infinity_bits - max_ulps);
2610 further_from_infinity_ = Floating::ReinterpretBits(
2611 infinity_bits - max_ulps - 1);
2612
2613 // Makes some NAN's.
2614 nan1_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 1);
2615 nan2_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 200);
2616 }
2617
2618 void TestSize() {
2619 EXPECT_EQ(sizeof(RawType), sizeof(Bits));
2620 }
2621
2622 // A battery of tests for FloatingEqMatcher::Matches.
2623 // matcher_maker is a pointer to a function which creates a FloatingEqMatcher.
2624 void TestMatches(
2625 testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) {
2626 Matcher<RawType> m1 = matcher_maker(0.0);
2627 EXPECT_TRUE(m1.Matches(-0.0));
2628 EXPECT_TRUE(m1.Matches(close_to_positive_zero_));
2629 EXPECT_TRUE(m1.Matches(close_to_negative_zero_));
2630 EXPECT_FALSE(m1.Matches(1.0));
2631
2632 Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_);
2633 EXPECT_FALSE(m2.Matches(further_from_negative_zero_));
2634
2635 Matcher<RawType> m3 = matcher_maker(1.0);
2636 EXPECT_TRUE(m3.Matches(close_to_one_));
2637 EXPECT_FALSE(m3.Matches(further_from_one_));
2638
2639 // Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above.
2640 EXPECT_FALSE(m3.Matches(0.0));
2641
2642 Matcher<RawType> m4 = matcher_maker(-infinity_);
2643 EXPECT_TRUE(m4.Matches(-close_to_infinity_));
2644
2645 Matcher<RawType> m5 = matcher_maker(infinity_);
2646 EXPECT_TRUE(m5.Matches(close_to_infinity_));
2647
2648 // This is interesting as the representations of infinity_ and nan1_
2649 // are only 1 DLP apart.
2650 EXPECT_FALSE(m5.Matches(nan1_));
2651
2652 // matcher_maker can produce a Matcher<const RawType&>, which is needed in
2653 // some cases.
2654 Matcher<const RawType&> m6 = matcher_maker(0.0);
2655 EXPECT_TRUE(m6.Matches(-0.0));
2656 EXPECT_TRUE(m6.Matches(close_to_positive_zero_));
2657 EXPECT_FALSE(m6.Matches(1.0));
2658
2659 // matcher_maker can produce a Matcher<RawType&>, which is needed in some
2660 // cases.
2661 Matcher<RawType&> m7 = matcher_maker(0.0);
2662 RawType x = 0.0;
2663 EXPECT_TRUE(m7.Matches(x));
2664 x = 0.01f;
2665 EXPECT_FALSE(m7.Matches(x));
2666 }
2667
2668 // Pre-calculated numbers to be used by the tests.
2669
2670 static RawType close_to_positive_zero_;
2671 static RawType close_to_negative_zero_;
2672 static RawType further_from_negative_zero_;
2673
2674 static RawType close_to_one_;
2675 static RawType further_from_one_;
2676
2677 static RawType infinity_;
2678 static RawType close_to_infinity_;
2679 static RawType further_from_infinity_;
2680
2681 static RawType nan1_;
2682 static RawType nan2_;
2683};
2684
2685template <typename RawType>
2686RawType FloatingPointTest<RawType>::close_to_positive_zero_;
2687
2688template <typename RawType>
2689RawType FloatingPointTest<RawType>::close_to_negative_zero_;
2690
2691template <typename RawType>
2692RawType FloatingPointTest<RawType>::further_from_negative_zero_;
2693
2694template <typename RawType>
2695RawType FloatingPointTest<RawType>::close_to_one_;
2696
2697template <typename RawType>
2698RawType FloatingPointTest<RawType>::further_from_one_;
2699
2700template <typename RawType>
2701RawType FloatingPointTest<RawType>::infinity_;
2702
2703template <typename RawType>
2704RawType FloatingPointTest<RawType>::close_to_infinity_;
2705
2706template <typename RawType>
2707RawType FloatingPointTest<RawType>::further_from_infinity_;
2708
2709template <typename RawType>
2710RawType FloatingPointTest<RawType>::nan1_;
2711
2712template <typename RawType>
2713RawType FloatingPointTest<RawType>::nan2_;
2714
2715// Instantiate FloatingPointTest for testing floats.
2716typedef FloatingPointTest<float> FloatTest;
2717
2718TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) {
2719 TestMatches(&FloatEq);
2720}
2721
2722TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
2723 TestMatches(&NanSensitiveFloatEq);
2724}
2725
2726TEST_F(FloatTest, FloatEqCannotMatchNaN) {
2727 // FloatEq never matches NaN.
2728 Matcher<float> m = FloatEq(nan1_);
2729 EXPECT_FALSE(m.Matches(nan1_));
2730 EXPECT_FALSE(m.Matches(nan2_));
2731 EXPECT_FALSE(m.Matches(1.0));
2732}
2733
2734TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {
2735 // NanSensitiveFloatEq will match NaN.
2736 Matcher<float> m = NanSensitiveFloatEq(nan1_);
2737 EXPECT_TRUE(m.Matches(nan1_));
2738 EXPECT_TRUE(m.Matches(nan2_));
2739 EXPECT_FALSE(m.Matches(1.0));
2740}
2741
2742TEST_F(FloatTest, FloatEqCanDescribeSelf) {
2743 Matcher<float> m1 = FloatEq(2.0f);
2744 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002745 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002746
2747 Matcher<float> m2 = FloatEq(0.5f);
2748 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002749 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002750
2751 Matcher<float> m3 = FloatEq(nan1_);
2752 EXPECT_EQ("never matches", Describe(m3));
2753 EXPECT_EQ("is anything", DescribeNegation(m3));
2754}
2755
2756TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {
2757 Matcher<float> m1 = NanSensitiveFloatEq(2.0f);
2758 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002759 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002760
2761 Matcher<float> m2 = NanSensitiveFloatEq(0.5f);
2762 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002763 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002764
2765 Matcher<float> m3 = NanSensitiveFloatEq(nan1_);
2766 EXPECT_EQ("is NaN", Describe(m3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002767 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
shiqiane35fdd92008-12-10 05:08:54 +00002768}
2769
2770// Instantiate FloatingPointTest for testing doubles.
2771typedef FloatingPointTest<double> DoubleTest;
2772
2773TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
2774 TestMatches(&DoubleEq);
2775}
2776
2777TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
2778 TestMatches(&NanSensitiveDoubleEq);
2779}
2780
2781TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {
2782 // DoubleEq never matches NaN.
2783 Matcher<double> m = DoubleEq(nan1_);
2784 EXPECT_FALSE(m.Matches(nan1_));
2785 EXPECT_FALSE(m.Matches(nan2_));
2786 EXPECT_FALSE(m.Matches(1.0));
2787}
2788
2789TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {
2790 // NanSensitiveDoubleEq will match NaN.
2791 Matcher<double> m = NanSensitiveDoubleEq(nan1_);
2792 EXPECT_TRUE(m.Matches(nan1_));
2793 EXPECT_TRUE(m.Matches(nan2_));
2794 EXPECT_FALSE(m.Matches(1.0));
2795}
2796
2797TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {
2798 Matcher<double> m1 = DoubleEq(2.0);
2799 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002800 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002801
2802 Matcher<double> m2 = DoubleEq(0.5);
2803 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002804 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002805
2806 Matcher<double> m3 = DoubleEq(nan1_);
2807 EXPECT_EQ("never matches", Describe(m3));
2808 EXPECT_EQ("is anything", DescribeNegation(m3));
2809}
2810
2811TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {
2812 Matcher<double> m1 = NanSensitiveDoubleEq(2.0);
2813 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002814 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002815
2816 Matcher<double> m2 = NanSensitiveDoubleEq(0.5);
2817 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002818 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002819
2820 Matcher<double> m3 = NanSensitiveDoubleEq(nan1_);
2821 EXPECT_EQ("is NaN", Describe(m3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002822 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
shiqiane35fdd92008-12-10 05:08:54 +00002823}
2824
2825TEST(PointeeTest, RawPointer) {
2826 const Matcher<int*> m = Pointee(Ge(0));
2827
2828 int n = 1;
2829 EXPECT_TRUE(m.Matches(&n));
2830 n = -1;
2831 EXPECT_FALSE(m.Matches(&n));
2832 EXPECT_FALSE(m.Matches(NULL));
2833}
2834
2835TEST(PointeeTest, RawPointerToConst) {
2836 const Matcher<const double*> m = Pointee(Ge(0));
2837
2838 double x = 1;
2839 EXPECT_TRUE(m.Matches(&x));
2840 x = -1;
2841 EXPECT_FALSE(m.Matches(&x));
2842 EXPECT_FALSE(m.Matches(NULL));
2843}
2844
2845TEST(PointeeTest, ReferenceToConstRawPointer) {
2846 const Matcher<int* const &> m = Pointee(Ge(0));
2847
2848 int n = 1;
2849 EXPECT_TRUE(m.Matches(&n));
2850 n = -1;
2851 EXPECT_FALSE(m.Matches(&n));
2852 EXPECT_FALSE(m.Matches(NULL));
2853}
2854
2855TEST(PointeeTest, ReferenceToNonConstRawPointer) {
2856 const Matcher<double* &> m = Pointee(Ge(0));
2857
2858 double x = 1.0;
2859 double* p = &x;
2860 EXPECT_TRUE(m.Matches(p));
2861 x = -1;
2862 EXPECT_FALSE(m.Matches(p));
2863 p = NULL;
2864 EXPECT_FALSE(m.Matches(p));
2865}
2866
vladlosevada23472012-08-14 15:38:49 +00002867// Minimal const-propagating pointer.
2868template <typename T>
2869class ConstPropagatingPtr {
2870 public:
2871 typedef T element_type;
2872
2873 ConstPropagatingPtr() : val_() {}
2874 explicit ConstPropagatingPtr(T* t) : val_(t) {}
2875 ConstPropagatingPtr(const ConstPropagatingPtr& other) : val_(other.val_) {}
2876
2877 T* get() { return val_; }
2878 T& operator*() { return *val_; }
2879 // Most smart pointers return non-const T* and T& from the next methods.
2880 const T* get() const { return val_; }
2881 const T& operator*() const { return *val_; }
2882
2883 private:
2884 T* val_;
2885};
2886
2887TEST(PointeeTest, WorksWithConstPropagatingPointers) {
2888 const Matcher< ConstPropagatingPtr<int> > m = Pointee(Lt(5));
2889 int three = 3;
2890 const ConstPropagatingPtr<int> co(&three);
2891 ConstPropagatingPtr<int> o(&three);
2892 EXPECT_TRUE(m.Matches(o));
2893 EXPECT_TRUE(m.Matches(co));
2894 *o = 6;
2895 EXPECT_FALSE(m.Matches(o));
2896 EXPECT_FALSE(m.Matches(ConstPropagatingPtr<int>()));
2897}
2898
shiqiane35fdd92008-12-10 05:08:54 +00002899TEST(PointeeTest, NeverMatchesNull) {
2900 const Matcher<const char*> m = Pointee(_);
2901 EXPECT_FALSE(m.Matches(NULL));
2902}
2903
2904// Tests that we can write Pointee(value) instead of Pointee(Eq(value)).
2905TEST(PointeeTest, MatchesAgainstAValue) {
2906 const Matcher<int*> m = Pointee(5);
2907
2908 int n = 5;
2909 EXPECT_TRUE(m.Matches(&n));
2910 n = -1;
2911 EXPECT_FALSE(m.Matches(&n));
2912 EXPECT_FALSE(m.Matches(NULL));
2913}
2914
2915TEST(PointeeTest, CanDescribeSelf) {
2916 const Matcher<int*> m = Pointee(Gt(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002917 EXPECT_EQ("points to a value that is > 3", Describe(m));
2918 EXPECT_EQ("does not point to a value that is > 3",
shiqiane35fdd92008-12-10 05:08:54 +00002919 DescribeNegation(m));
2920}
2921
shiqiane35fdd92008-12-10 05:08:54 +00002922TEST(PointeeTest, CanExplainMatchResult) {
2923 const Matcher<const string*> m = Pointee(StartsWith("Hi"));
2924
2925 EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL)));
2926
zhanyong.wan736baa82010-09-27 17:44:16 +00002927 const Matcher<long*> m2 = Pointee(GreaterThan(1)); // NOLINT
2928 long n = 3; // NOLINT
2929 EXPECT_EQ("which points to 3" + OfType("long") + ", which is 2 more than 1",
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002930 Explain(m2, &n));
2931}
2932
2933TEST(PointeeTest, AlwaysExplainsPointee) {
2934 const Matcher<int*> m = Pointee(0);
2935 int n = 42;
zhanyong.wan736baa82010-09-27 17:44:16 +00002936 EXPECT_EQ("which points to 42" + OfType("int"), Explain(m, &n));
shiqiane35fdd92008-12-10 05:08:54 +00002937}
2938
2939// An uncopyable class.
2940class Uncopyable {
2941 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002942 explicit Uncopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00002943
2944 int value() const { return value_; }
2945 private:
2946 const int value_;
2947 GTEST_DISALLOW_COPY_AND_ASSIGN_(Uncopyable);
2948};
2949
2950// Returns true iff x.value() is positive.
2951bool ValueIsPositive(const Uncopyable& x) { return x.value() > 0; }
2952
2953// A user-defined struct for testing Field().
2954struct AStruct {
2955 AStruct() : x(0), y(1.0), z(5), p(NULL) {}
2956 AStruct(const AStruct& rhs)
2957 : x(rhs.x), y(rhs.y), z(rhs.z.value()), p(rhs.p) {}
2958
2959 int x; // A non-const field.
2960 const double y; // A const field.
2961 Uncopyable z; // An uncopyable field.
2962 const char* p; // A pointer field.
zhanyong.wan32de5f52009-12-23 00:13:23 +00002963
2964 private:
2965 GTEST_DISALLOW_ASSIGN_(AStruct);
shiqiane35fdd92008-12-10 05:08:54 +00002966};
2967
2968// A derived struct for testing Field().
2969struct DerivedStruct : public AStruct {
2970 char ch;
zhanyong.wan32de5f52009-12-23 00:13:23 +00002971
2972 private:
2973 GTEST_DISALLOW_ASSIGN_(DerivedStruct);
shiqiane35fdd92008-12-10 05:08:54 +00002974};
2975
2976// Tests that Field(&Foo::field, ...) works when field is non-const.
2977TEST(FieldTest, WorksForNonConstField) {
2978 Matcher<AStruct> m = Field(&AStruct::x, Ge(0));
2979
2980 AStruct a;
2981 EXPECT_TRUE(m.Matches(a));
2982 a.x = -1;
2983 EXPECT_FALSE(m.Matches(a));
2984}
2985
2986// Tests that Field(&Foo::field, ...) works when field is const.
2987TEST(FieldTest, WorksForConstField) {
2988 AStruct a;
2989
2990 Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0));
2991 EXPECT_TRUE(m.Matches(a));
2992 m = Field(&AStruct::y, Le(0.0));
2993 EXPECT_FALSE(m.Matches(a));
2994}
2995
2996// Tests that Field(&Foo::field, ...) works when field is not copyable.
2997TEST(FieldTest, WorksForUncopyableField) {
2998 AStruct a;
2999
3000 Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive));
3001 EXPECT_TRUE(m.Matches(a));
3002 m = Field(&AStruct::z, Not(Truly(ValueIsPositive)));
3003 EXPECT_FALSE(m.Matches(a));
3004}
3005
3006// Tests that Field(&Foo::field, ...) works when field is a pointer.
3007TEST(FieldTest, WorksForPointerField) {
3008 // Matching against NULL.
3009 Matcher<AStruct> m = Field(&AStruct::p, static_cast<const char*>(NULL));
3010 AStruct a;
3011 EXPECT_TRUE(m.Matches(a));
3012 a.p = "hi";
3013 EXPECT_FALSE(m.Matches(a));
3014
3015 // Matching a pointer that is not NULL.
3016 m = Field(&AStruct::p, StartsWith("hi"));
3017 a.p = "hill";
3018 EXPECT_TRUE(m.Matches(a));
3019 a.p = "hole";
3020 EXPECT_FALSE(m.Matches(a));
3021}
3022
3023// Tests that Field() works when the object is passed by reference.
3024TEST(FieldTest, WorksForByRefArgument) {
3025 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
3026
3027 AStruct a;
3028 EXPECT_TRUE(m.Matches(a));
3029 a.x = -1;
3030 EXPECT_FALSE(m.Matches(a));
3031}
3032
3033// Tests that Field(&Foo::field, ...) works when the argument's type
3034// is a sub-type of Foo.
3035TEST(FieldTest, WorksForArgumentOfSubType) {
3036 // Note that the matcher expects DerivedStruct but we say AStruct
3037 // inside Field().
3038 Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0));
3039
3040 DerivedStruct d;
3041 EXPECT_TRUE(m.Matches(d));
3042 d.x = -1;
3043 EXPECT_FALSE(m.Matches(d));
3044}
3045
3046// Tests that Field(&Foo::field, m) works when field's type and m's
3047// argument type are compatible but not the same.
3048TEST(FieldTest, WorksForCompatibleMatcherType) {
3049 // The field is an int, but the inner matcher expects a signed char.
3050 Matcher<const AStruct&> m = Field(&AStruct::x,
3051 Matcher<signed char>(Ge(0)));
3052
3053 AStruct a;
3054 EXPECT_TRUE(m.Matches(a));
3055 a.x = -1;
3056 EXPECT_FALSE(m.Matches(a));
3057}
3058
3059// Tests that Field() can describe itself.
3060TEST(FieldTest, CanDescribeSelf) {
3061 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
3062
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003063 EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
3064 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003065}
3066
3067// Tests that Field() can explain the match result.
3068TEST(FieldTest, CanExplainMatchResult) {
3069 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
3070
3071 AStruct a;
3072 a.x = 1;
zhanyong.wan736baa82010-09-27 17:44:16 +00003073 EXPECT_EQ("whose given field is 1" + OfType("int"), Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003074
3075 m = Field(&AStruct::x, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003076 EXPECT_EQ(
3077 "whose given field is 1" + OfType("int") + ", which is 1 more than 0",
3078 Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003079}
3080
3081// Tests that Field() works when the argument is a pointer to const.
3082TEST(FieldForPointerTest, WorksForPointerToConst) {
3083 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
3084
3085 AStruct a;
3086 EXPECT_TRUE(m.Matches(&a));
3087 a.x = -1;
3088 EXPECT_FALSE(m.Matches(&a));
3089}
3090
3091// Tests that Field() works when the argument is a pointer to non-const.
3092TEST(FieldForPointerTest, WorksForPointerToNonConst) {
3093 Matcher<AStruct*> m = Field(&AStruct::x, Ge(0));
3094
3095 AStruct a;
3096 EXPECT_TRUE(m.Matches(&a));
3097 a.x = -1;
3098 EXPECT_FALSE(m.Matches(&a));
3099}
3100
zhanyong.wan6953a722010-01-13 05:15:07 +00003101// Tests that Field() works when the argument is a reference to a const pointer.
3102TEST(FieldForPointerTest, WorksForReferenceToConstPointer) {
3103 Matcher<AStruct* const&> m = Field(&AStruct::x, Ge(0));
3104
3105 AStruct a;
3106 EXPECT_TRUE(m.Matches(&a));
3107 a.x = -1;
3108 EXPECT_FALSE(m.Matches(&a));
3109}
3110
shiqiane35fdd92008-12-10 05:08:54 +00003111// Tests that Field() does not match the NULL pointer.
3112TEST(FieldForPointerTest, DoesNotMatchNull) {
3113 Matcher<const AStruct*> m = Field(&AStruct::x, _);
3114 EXPECT_FALSE(m.Matches(NULL));
3115}
3116
3117// Tests that Field(&Foo::field, ...) works when the argument's type
3118// is a sub-type of const Foo*.
3119TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
3120 // Note that the matcher expects DerivedStruct but we say AStruct
3121 // inside Field().
3122 Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0));
3123
3124 DerivedStruct d;
3125 EXPECT_TRUE(m.Matches(&d));
3126 d.x = -1;
3127 EXPECT_FALSE(m.Matches(&d));
3128}
3129
3130// Tests that Field() can describe itself when used to match a pointer.
3131TEST(FieldForPointerTest, CanDescribeSelf) {
3132 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
3133
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003134 EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
3135 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003136}
3137
3138// Tests that Field() can explain the result of matching a pointer.
3139TEST(FieldForPointerTest, CanExplainMatchResult) {
3140 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
3141
3142 AStruct a;
3143 a.x = 1;
3144 EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(NULL)));
zhanyong.wan736baa82010-09-27 17:44:16 +00003145 EXPECT_EQ("which points to an object whose given field is 1" + OfType("int"),
3146 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003147
3148 m = Field(&AStruct::x, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003149 EXPECT_EQ("which points to an object whose given field is 1" + OfType("int") +
3150 ", which is 1 more than 0", Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003151}
3152
3153// A user-defined class for testing Property().
3154class AClass {
3155 public:
3156 AClass() : n_(0) {}
3157
3158 // A getter that returns a non-reference.
3159 int n() const { return n_; }
3160
3161 void set_n(int new_n) { n_ = new_n; }
3162
3163 // A getter that returns a reference to const.
3164 const string& s() const { return s_; }
3165
3166 void set_s(const string& new_s) { s_ = new_s; }
3167
3168 // A getter that returns a reference to non-const.
3169 double& x() const { return x_; }
3170 private:
3171 int n_;
3172 string s_;
3173
3174 static double x_;
3175};
3176
3177double AClass::x_ = 0.0;
3178
3179// A derived class for testing Property().
3180class DerivedClass : public AClass {
3181 private:
3182 int k_;
3183};
3184
3185// Tests that Property(&Foo::property, ...) works when property()
3186// returns a non-reference.
3187TEST(PropertyTest, WorksForNonReferenceProperty) {
3188 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3189
3190 AClass a;
3191 a.set_n(1);
3192 EXPECT_TRUE(m.Matches(a));
3193
3194 a.set_n(-1);
3195 EXPECT_FALSE(m.Matches(a));
3196}
3197
3198// Tests that Property(&Foo::property, ...) works when property()
3199// returns a reference to const.
3200TEST(PropertyTest, WorksForReferenceToConstProperty) {
3201 Matcher<const AClass&> m = Property(&AClass::s, StartsWith("hi"));
3202
3203 AClass a;
3204 a.set_s("hill");
3205 EXPECT_TRUE(m.Matches(a));
3206
3207 a.set_s("hole");
3208 EXPECT_FALSE(m.Matches(a));
3209}
3210
3211// Tests that Property(&Foo::property, ...) works when property()
3212// returns a reference to non-const.
3213TEST(PropertyTest, WorksForReferenceToNonConstProperty) {
3214 double x = 0.0;
3215 AClass a;
3216
3217 Matcher<const AClass&> m = Property(&AClass::x, Ref(x));
3218 EXPECT_FALSE(m.Matches(a));
3219
3220 m = Property(&AClass::x, Not(Ref(x)));
3221 EXPECT_TRUE(m.Matches(a));
3222}
3223
3224// Tests that Property(&Foo::property, ...) works when the argument is
3225// passed by value.
3226TEST(PropertyTest, WorksForByValueArgument) {
3227 Matcher<AClass> m = Property(&AClass::s, StartsWith("hi"));
3228
3229 AClass a;
3230 a.set_s("hill");
3231 EXPECT_TRUE(m.Matches(a));
3232
3233 a.set_s("hole");
3234 EXPECT_FALSE(m.Matches(a));
3235}
3236
3237// Tests that Property(&Foo::property, ...) works when the argument's
3238// type is a sub-type of Foo.
3239TEST(PropertyTest, WorksForArgumentOfSubType) {
3240 // The matcher expects a DerivedClass, but inside the Property() we
3241 // say AClass.
3242 Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0));
3243
3244 DerivedClass d;
3245 d.set_n(1);
3246 EXPECT_TRUE(m.Matches(d));
3247
3248 d.set_n(-1);
3249 EXPECT_FALSE(m.Matches(d));
3250}
3251
3252// Tests that Property(&Foo::property, m) works when property()'s type
3253// and m's argument type are compatible but different.
3254TEST(PropertyTest, WorksForCompatibleMatcherType) {
3255 // n() returns an int but the inner matcher expects a signed char.
3256 Matcher<const AClass&> m = Property(&AClass::n,
3257 Matcher<signed char>(Ge(0)));
3258
3259 AClass a;
3260 EXPECT_TRUE(m.Matches(a));
3261 a.set_n(-1);
3262 EXPECT_FALSE(m.Matches(a));
3263}
3264
3265// Tests that Property() can describe itself.
3266TEST(PropertyTest, CanDescribeSelf) {
3267 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3268
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003269 EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
3270 EXPECT_EQ("is an object whose given property isn't >= 0",
3271 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003272}
3273
3274// Tests that Property() can explain the match result.
3275TEST(PropertyTest, CanExplainMatchResult) {
3276 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3277
3278 AClass a;
3279 a.set_n(1);
zhanyong.wan736baa82010-09-27 17:44:16 +00003280 EXPECT_EQ("whose given property is 1" + OfType("int"), Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003281
3282 m = Property(&AClass::n, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003283 EXPECT_EQ(
3284 "whose given property is 1" + OfType("int") + ", which is 1 more than 0",
3285 Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003286}
3287
3288// Tests that Property() works when the argument is a pointer to const.
3289TEST(PropertyForPointerTest, WorksForPointerToConst) {
3290 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3291
3292 AClass a;
3293 a.set_n(1);
3294 EXPECT_TRUE(m.Matches(&a));
3295
3296 a.set_n(-1);
3297 EXPECT_FALSE(m.Matches(&a));
3298}
3299
3300// Tests that Property() works when the argument is a pointer to non-const.
3301TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
3302 Matcher<AClass*> m = Property(&AClass::s, StartsWith("hi"));
3303
3304 AClass a;
3305 a.set_s("hill");
3306 EXPECT_TRUE(m.Matches(&a));
3307
3308 a.set_s("hole");
3309 EXPECT_FALSE(m.Matches(&a));
3310}
3311
zhanyong.wan6953a722010-01-13 05:15:07 +00003312// Tests that Property() works when the argument is a reference to a
3313// const pointer.
3314TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) {
3315 Matcher<AClass* const&> m = Property(&AClass::s, StartsWith("hi"));
3316
3317 AClass a;
3318 a.set_s("hill");
3319 EXPECT_TRUE(m.Matches(&a));
3320
3321 a.set_s("hole");
3322 EXPECT_FALSE(m.Matches(&a));
3323}
3324
shiqiane35fdd92008-12-10 05:08:54 +00003325// Tests that Property() does not match the NULL pointer.
3326TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
3327 Matcher<const AClass*> m = Property(&AClass::x, _);
3328 EXPECT_FALSE(m.Matches(NULL));
3329}
3330
3331// Tests that Property(&Foo::property, ...) works when the argument's
3332// type is a sub-type of const Foo*.
3333TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
3334 // The matcher expects a DerivedClass, but inside the Property() we
3335 // say AClass.
3336 Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0));
3337
3338 DerivedClass d;
3339 d.set_n(1);
3340 EXPECT_TRUE(m.Matches(&d));
3341
3342 d.set_n(-1);
3343 EXPECT_FALSE(m.Matches(&d));
3344}
3345
3346// Tests that Property() can describe itself when used to match a pointer.
3347TEST(PropertyForPointerTest, CanDescribeSelf) {
3348 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3349
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003350 EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
3351 EXPECT_EQ("is an object whose given property isn't >= 0",
3352 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003353}
3354
3355// Tests that Property() can explain the result of matching a pointer.
3356TEST(PropertyForPointerTest, CanExplainMatchResult) {
3357 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3358
3359 AClass a;
3360 a.set_n(1);
3361 EXPECT_EQ("", Explain(m, static_cast<const AClass*>(NULL)));
zhanyong.wan736baa82010-09-27 17:44:16 +00003362 EXPECT_EQ(
3363 "which points to an object whose given property is 1" + OfType("int"),
3364 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003365
3366 m = Property(&AClass::n, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003367 EXPECT_EQ("which points to an object whose given property is 1" +
3368 OfType("int") + ", which is 1 more than 0",
3369 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003370}
3371
3372// Tests ResultOf.
3373
3374// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3375// function pointer.
3376string IntToStringFunction(int input) { return input == 1 ? "foo" : "bar"; }
3377
3378TEST(ResultOfTest, WorksForFunctionPointers) {
3379 Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(string("foo")));
3380
3381 EXPECT_TRUE(matcher.Matches(1));
3382 EXPECT_FALSE(matcher.Matches(2));
3383}
3384
3385// Tests that ResultOf() can describe itself.
3386TEST(ResultOfTest, CanDescribeItself) {
3387 Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo"));
3388
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003389 EXPECT_EQ("is mapped by the given callable to a value that "
3390 "is equal to \"foo\"", Describe(matcher));
3391 EXPECT_EQ("is mapped by the given callable to a value that "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003392 "isn't equal to \"foo\"", DescribeNegation(matcher));
shiqiane35fdd92008-12-10 05:08:54 +00003393}
3394
3395// Tests that ResultOf() can explain the match result.
3396int IntFunction(int input) { return input == 42 ? 80 : 90; }
3397
3398TEST(ResultOfTest, CanExplainMatchResult) {
3399 Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
zhanyong.wan736baa82010-09-27 17:44:16 +00003400 EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int"),
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003401 Explain(matcher, 36));
shiqiane35fdd92008-12-10 05:08:54 +00003402
3403 matcher = ResultOf(&IntFunction, GreaterThan(85));
zhanyong.wan736baa82010-09-27 17:44:16 +00003404 EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int") +
3405 ", which is 5 more than 85", Explain(matcher, 36));
shiqiane35fdd92008-12-10 05:08:54 +00003406}
3407
3408// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3409// returns a non-reference.
3410TEST(ResultOfTest, WorksForNonReferenceResults) {
3411 Matcher<int> matcher = ResultOf(&IntFunction, Eq(80));
3412
3413 EXPECT_TRUE(matcher.Matches(42));
3414 EXPECT_FALSE(matcher.Matches(36));
3415}
3416
3417// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3418// returns a reference to non-const.
zhanyong.wan736baa82010-09-27 17:44:16 +00003419double& DoubleFunction(double& input) { return input; } // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00003420
zhanyong.wan736baa82010-09-27 17:44:16 +00003421Uncopyable& RefUncopyableFunction(Uncopyable& obj) { // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00003422 return obj;
3423}
3424
3425TEST(ResultOfTest, WorksForReferenceToNonConstResults) {
3426 double x = 3.14;
3427 double x2 = x;
3428 Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x));
3429
3430 EXPECT_TRUE(matcher.Matches(x));
3431 EXPECT_FALSE(matcher.Matches(x2));
3432
3433 // Test that ResultOf works with uncopyable objects
3434 Uncopyable obj(0);
3435 Uncopyable obj2(0);
3436 Matcher<Uncopyable&> matcher2 =
3437 ResultOf(&RefUncopyableFunction, Ref(obj));
3438
3439 EXPECT_TRUE(matcher2.Matches(obj));
3440 EXPECT_FALSE(matcher2.Matches(obj2));
3441}
3442
3443// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3444// returns a reference to const.
3445const string& StringFunction(const string& input) { return input; }
3446
3447TEST(ResultOfTest, WorksForReferenceToConstResults) {
3448 string s = "foo";
3449 string s2 = s;
3450 Matcher<const string&> matcher = ResultOf(&StringFunction, Ref(s));
3451
3452 EXPECT_TRUE(matcher.Matches(s));
3453 EXPECT_FALSE(matcher.Matches(s2));
3454}
3455
3456// Tests that ResultOf(f, m) works when f(x) and m's
3457// argument types are compatible but different.
3458TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {
3459 // IntFunction() returns int but the inner matcher expects a signed char.
3460 Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85)));
3461
3462 EXPECT_TRUE(matcher.Matches(36));
3463 EXPECT_FALSE(matcher.Matches(42));
3464}
3465
shiqiane35fdd92008-12-10 05:08:54 +00003466// Tests that the program aborts when ResultOf is passed
3467// a NULL function pointer.
3468TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +00003469 EXPECT_DEATH_IF_SUPPORTED(
zhanyong.wan736baa82010-09-27 17:44:16 +00003470 ResultOf(static_cast<string(*)(int dummy)>(NULL), Eq(string("foo"))),
shiqiane35fdd92008-12-10 05:08:54 +00003471 "NULL function pointer is passed into ResultOf\\(\\)\\.");
3472}
shiqiane35fdd92008-12-10 05:08:54 +00003473
3474// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3475// function reference.
3476TEST(ResultOfTest, WorksForFunctionReferences) {
3477 Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq("foo"));
3478 EXPECT_TRUE(matcher.Matches(1));
3479 EXPECT_FALSE(matcher.Matches(2));
3480}
3481
3482// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3483// function object.
3484struct Functor : public ::std::unary_function<int, string> {
3485 result_type operator()(argument_type input) const {
3486 return IntToStringFunction(input);
3487 }
3488};
3489
3490TEST(ResultOfTest, WorksForFunctors) {
3491 Matcher<int> matcher = ResultOf(Functor(), Eq(string("foo")));
3492
3493 EXPECT_TRUE(matcher.Matches(1));
3494 EXPECT_FALSE(matcher.Matches(2));
3495}
3496
3497// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3498// functor with more then one operator() defined. ResultOf() must work
3499// for each defined operator().
3500struct PolymorphicFunctor {
3501 typedef int result_type;
3502 int operator()(int n) { return n; }
3503 int operator()(const char* s) { return static_cast<int>(strlen(s)); }
3504};
3505
3506TEST(ResultOfTest, WorksForPolymorphicFunctors) {
3507 Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5));
3508
3509 EXPECT_TRUE(matcher_int.Matches(10));
3510 EXPECT_FALSE(matcher_int.Matches(2));
3511
3512 Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5));
3513
3514 EXPECT_TRUE(matcher_string.Matches("long string"));
3515 EXPECT_FALSE(matcher_string.Matches("shrt"));
3516}
3517
3518const int* ReferencingFunction(const int& n) { return &n; }
3519
3520struct ReferencingFunctor {
3521 typedef const int* result_type;
3522 result_type operator()(const int& n) { return &n; }
3523};
3524
3525TEST(ResultOfTest, WorksForReferencingCallables) {
3526 const int n = 1;
3527 const int n2 = 1;
3528 Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n));
3529 EXPECT_TRUE(matcher2.Matches(n));
3530 EXPECT_FALSE(matcher2.Matches(n2));
3531
3532 Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n));
3533 EXPECT_TRUE(matcher3.Matches(n));
3534 EXPECT_FALSE(matcher3.Matches(n2));
3535}
3536
shiqiane35fdd92008-12-10 05:08:54 +00003537class DivisibleByImpl {
3538 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003539 explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {}
shiqiane35fdd92008-12-10 05:08:54 +00003540
zhanyong.wandb22c222010-01-28 21:52:29 +00003541 // For testing using ExplainMatchResultTo() with polymorphic matchers.
shiqiane35fdd92008-12-10 05:08:54 +00003542 template <typename T>
zhanyong.wandb22c222010-01-28 21:52:29 +00003543 bool MatchAndExplain(const T& n, MatchResultListener* listener) const {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003544 *listener << "which is " << (n % divider_) << " modulo "
zhanyong.wandb22c222010-01-28 21:52:29 +00003545 << divider_;
shiqiane35fdd92008-12-10 05:08:54 +00003546 return (n % divider_) == 0;
3547 }
3548
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003549 void DescribeTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +00003550 *os << "is divisible by " << divider_;
3551 }
3552
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003553 void DescribeNegationTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +00003554 *os << "is not divisible by " << divider_;
3555 }
3556
zhanyong.wan32de5f52009-12-23 00:13:23 +00003557 void set_divider(int a_divider) { divider_ = a_divider; }
shiqiane35fdd92008-12-10 05:08:54 +00003558 int divider() const { return divider_; }
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003559
shiqiane35fdd92008-12-10 05:08:54 +00003560 private:
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003561 int divider_;
shiqiane35fdd92008-12-10 05:08:54 +00003562};
3563
shiqiane35fdd92008-12-10 05:08:54 +00003564PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) {
3565 return MakePolymorphicMatcher(DivisibleByImpl(n));
3566}
3567
3568// Tests that when AllOf() fails, only the first failing matcher is
3569// asked to explain why.
3570TEST(ExplainMatchResultTest, AllOf_False_False) {
3571 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003572 EXPECT_EQ("which is 1 modulo 4", Explain(m, 5));
shiqiane35fdd92008-12-10 05:08:54 +00003573}
3574
3575// Tests that when AllOf() fails, only the first failing matcher is
3576// asked to explain why.
3577TEST(ExplainMatchResultTest, AllOf_False_True) {
3578 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003579 EXPECT_EQ("which is 2 modulo 4", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003580}
3581
3582// Tests that when AllOf() fails, only the first failing matcher is
3583// asked to explain why.
3584TEST(ExplainMatchResultTest, AllOf_True_False) {
3585 const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003586 EXPECT_EQ("which is 2 modulo 3", Explain(m, 5));
shiqiane35fdd92008-12-10 05:08:54 +00003587}
3588
3589// Tests that when AllOf() succeeds, all matchers are asked to explain
3590// why.
3591TEST(ExplainMatchResultTest, AllOf_True_True) {
3592 const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003593 EXPECT_EQ("which is 0 modulo 2, and which is 0 modulo 3", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003594}
3595
3596TEST(ExplainMatchResultTest, AllOf_True_True_2) {
3597 const Matcher<int> m = AllOf(Ge(2), Le(3));
3598 EXPECT_EQ("", Explain(m, 2));
3599}
3600
3601TEST(ExplainmatcherResultTest, MonomorphicMatcher) {
3602 const Matcher<int> m = GreaterThan(5);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003603 EXPECT_EQ("which is 1 more than 5", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003604}
3605
3606// The following two tests verify that values without a public copy
3607// ctor can be used as arguments to matchers like Eq(), Ge(), and etc
3608// with the help of ByRef().
3609
3610class NotCopyable {
3611 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003612 explicit NotCopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00003613
3614 int value() const { return value_; }
3615
3616 bool operator==(const NotCopyable& rhs) const {
3617 return value() == rhs.value();
3618 }
3619
3620 bool operator>=(const NotCopyable& rhs) const {
3621 return value() >= rhs.value();
3622 }
3623 private:
3624 int value_;
3625
3626 GTEST_DISALLOW_COPY_AND_ASSIGN_(NotCopyable);
3627};
3628
3629TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) {
3630 const NotCopyable const_value1(1);
3631 const Matcher<const NotCopyable&> m = Eq(ByRef(const_value1));
3632
3633 const NotCopyable n1(1), n2(2);
3634 EXPECT_TRUE(m.Matches(n1));
3635 EXPECT_FALSE(m.Matches(n2));
3636}
3637
3638TEST(ByRefTest, AllowsNotCopyableValueInMatchers) {
3639 NotCopyable value2(2);
3640 const Matcher<NotCopyable&> m = Ge(ByRef(value2));
3641
3642 NotCopyable n1(1), n2(2);
3643 EXPECT_FALSE(m.Matches(n1));
3644 EXPECT_TRUE(m.Matches(n2));
3645}
3646
zhanyong.wan320814a2013-03-01 00:20:30 +00003647TEST(IsEmptyTest, ImplementsIsEmpty) {
3648 vector<int> container;
3649 EXPECT_THAT(container, IsEmpty());
3650 container.push_back(0);
3651 EXPECT_THAT(container, Not(IsEmpty()));
3652 container.push_back(1);
3653 EXPECT_THAT(container, Not(IsEmpty()));
3654}
3655
3656TEST(IsEmptyTest, WorksWithString) {
3657 string text;
3658 EXPECT_THAT(text, IsEmpty());
3659 text = "foo";
3660 EXPECT_THAT(text, Not(IsEmpty()));
3661 text = string("\0", 1);
3662 EXPECT_THAT(text, Not(IsEmpty()));
3663}
3664
3665TEST(IsEmptyTest, CanDescribeSelf) {
3666 Matcher<vector<int> > m = IsEmpty();
3667 EXPECT_EQ("is empty", Describe(m));
3668 EXPECT_EQ("isn't empty", DescribeNegation(m));
3669}
3670
3671TEST(IsEmptyTest, ExplainsResult) {
3672 Matcher<vector<int> > m = IsEmpty();
3673 vector<int> container;
3674 EXPECT_EQ("", Explain(m, container));
3675 container.push_back(0);
3676 EXPECT_EQ("whose size is 1", Explain(m, container));
3677}
3678
zhanyong.wana31d9ce2013-03-01 01:50:17 +00003679TEST(SizeIsTest, ImplementsSizeIs) {
3680 vector<int> container;
3681 EXPECT_THAT(container, SizeIs(0));
3682 EXPECT_THAT(container, Not(SizeIs(1)));
3683 container.push_back(0);
3684 EXPECT_THAT(container, Not(SizeIs(0)));
3685 EXPECT_THAT(container, SizeIs(1));
3686 container.push_back(0);
3687 EXPECT_THAT(container, Not(SizeIs(0)));
3688 EXPECT_THAT(container, SizeIs(2));
3689}
3690
3691TEST(SizeIsTest, WorksWithMap) {
3692 map<string, int> container;
3693 EXPECT_THAT(container, SizeIs(0));
3694 EXPECT_THAT(container, Not(SizeIs(1)));
3695 container.insert(make_pair("foo", 1));
3696 EXPECT_THAT(container, Not(SizeIs(0)));
3697 EXPECT_THAT(container, SizeIs(1));
3698 container.insert(make_pair("bar", 2));
3699 EXPECT_THAT(container, Not(SizeIs(0)));
3700 EXPECT_THAT(container, SizeIs(2));
3701}
3702
3703TEST(SizeIsTest, WorksWithReferences) {
3704 vector<int> container;
3705 Matcher<const vector<int>&> m = SizeIs(1);
3706 EXPECT_THAT(container, Not(m));
3707 container.push_back(0);
3708 EXPECT_THAT(container, m);
3709}
3710
3711TEST(SizeIsTest, CanDescribeSelf) {
3712 Matcher<vector<int> > m = SizeIs(2);
3713 EXPECT_EQ("size is equal to 2", Describe(m));
3714 EXPECT_EQ("size isn't equal to 2", DescribeNegation(m));
3715}
3716
3717TEST(SizeIsTest, ExplainsResult) {
3718 Matcher<vector<int> > m1 = SizeIs(2);
3719 Matcher<vector<int> > m2 = SizeIs(Lt(2u));
3720 Matcher<vector<int> > m3 = SizeIs(AnyOf(0, 3));
3721 Matcher<vector<int> > m4 = SizeIs(GreaterThan(1));
3722 vector<int> container;
3723 EXPECT_EQ("whose size 0 doesn't match", Explain(m1, container));
3724 EXPECT_EQ("whose size 0 matches", Explain(m2, container));
3725 EXPECT_EQ("whose size 0 matches", Explain(m3, container));
3726 EXPECT_EQ("whose size 0 doesn't match, which is 1 less than 1",
3727 Explain(m4, container));
3728 container.push_back(0);
3729 container.push_back(0);
3730 EXPECT_EQ("whose size 2 matches", Explain(m1, container));
3731 EXPECT_EQ("whose size 2 doesn't match", Explain(m2, container));
3732 EXPECT_EQ("whose size 2 doesn't match", Explain(m3, container));
3733 EXPECT_EQ("whose size 2 matches, which is 1 more than 1",
3734 Explain(m4, container));
3735}
3736
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003737#if GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003738// Tests ContainerEq with different container types, and
3739// different element types.
3740
3741template <typename T>
zhanyong.wanb8243162009-06-04 05:48:20 +00003742class ContainerEqTest : public testing::Test {};
zhanyong.wan6a896b52009-01-16 01:13:50 +00003743
3744typedef testing::Types<
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003745 set<int>,
3746 vector<size_t>,
3747 multiset<size_t>,
3748 list<int> >
zhanyong.wan6a896b52009-01-16 01:13:50 +00003749 ContainerEqTestTypes;
3750
3751TYPED_TEST_CASE(ContainerEqTest, ContainerEqTestTypes);
3752
3753// Tests that the filled container is equal to itself.
3754TYPED_TEST(ContainerEqTest, EqualsSelf) {
3755 static const int vals[] = {1, 1, 2, 3, 5, 8};
3756 TypeParam my_set(vals, vals + 6);
3757 const Matcher<TypeParam> m = ContainerEq(my_set);
3758 EXPECT_TRUE(m.Matches(my_set));
3759 EXPECT_EQ("", Explain(m, my_set));
3760}
3761
3762// Tests that missing values are reported.
3763TYPED_TEST(ContainerEqTest, ValueMissing) {
3764 static const int vals[] = {1, 1, 2, 3, 5, 8};
3765 static const int test_vals[] = {2, 1, 8, 5};
3766 TypeParam my_set(vals, vals + 6);
3767 TypeParam test_set(test_vals, test_vals + 4);
3768 const Matcher<TypeParam> m = ContainerEq(my_set);
3769 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003770 EXPECT_EQ("which doesn't have these expected elements: 3",
3771 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003772}
3773
3774// Tests that added values are reported.
3775TYPED_TEST(ContainerEqTest, ValueAdded) {
3776 static const int vals[] = {1, 1, 2, 3, 5, 8};
3777 static const int test_vals[] = {1, 2, 3, 5, 8, 46};
3778 TypeParam my_set(vals, vals + 6);
3779 TypeParam test_set(test_vals, test_vals + 6);
3780 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3781 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003782 EXPECT_EQ("which has these unexpected elements: 46", Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003783}
3784
3785// Tests that added and missing values are reported together.
3786TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) {
3787 static const int vals[] = {1, 1, 2, 3, 5, 8};
3788 static const int test_vals[] = {1, 2, 3, 8, 46};
3789 TypeParam my_set(vals, vals + 6);
3790 TypeParam test_set(test_vals, test_vals + 5);
3791 const Matcher<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,\n"
3794 "and doesn't have these expected elements: 5",
3795 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003796}
3797
3798// Tests duplicated value -- expect no explanation.
3799TYPED_TEST(ContainerEqTest, DuplicateDifference) {
3800 static const int vals[] = {1, 1, 2, 3, 5, 8};
3801 static const int test_vals[] = {1, 2, 3, 5, 8};
3802 TypeParam my_set(vals, vals + 6);
3803 TypeParam test_set(test_vals, test_vals + 5);
3804 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3805 // Depending on the container, match may be true or false
3806 // But in any case there should be no explanation.
3807 EXPECT_EQ("", Explain(m, test_set));
3808}
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003809#endif // GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003810
3811// Tests that mutliple missing values are reported.
3812// Using just vector here, so order is predicatble.
3813TEST(ContainerEqExtraTest, MultipleValuesMissing) {
3814 static const int vals[] = {1, 1, 2, 3, 5, 8};
3815 static const int test_vals[] = {2, 1, 5};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003816 vector<int> my_set(vals, vals + 6);
3817 vector<int> test_set(test_vals, test_vals + 3);
3818 const Matcher<vector<int> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003819 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003820 EXPECT_EQ("which doesn't have these expected elements: 3, 8",
3821 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003822}
3823
3824// Tests that added values are reported.
3825// Using just vector here, so order is predicatble.
3826TEST(ContainerEqExtraTest, MultipleValuesAdded) {
3827 static const int vals[] = {1, 1, 2, 3, 5, 8};
3828 static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003829 list<size_t> my_set(vals, vals + 6);
3830 list<size_t> test_set(test_vals, test_vals + 7);
3831 const Matcher<const list<size_t>&> m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003832 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003833 EXPECT_EQ("which has these unexpected elements: 92, 46",
3834 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003835}
3836
3837// Tests that added and missing values are reported together.
3838TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) {
3839 static const int vals[] = {1, 1, 2, 3, 5, 8};
3840 static const int test_vals[] = {1, 2, 3, 92, 46};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003841 list<size_t> my_set(vals, vals + 6);
3842 list<size_t> test_set(test_vals, test_vals + 5);
3843 const Matcher<const list<size_t> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003844 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003845 EXPECT_EQ("which has these unexpected elements: 92, 46,\n"
3846 "and doesn't have these expected elements: 5, 8",
zhanyong.wan6a896b52009-01-16 01:13:50 +00003847 Explain(m, test_set));
3848}
3849
3850// Tests to see that duplicate elements are detected,
3851// but (as above) not reported in the explanation.
3852TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) {
3853 static const int vals[] = {1, 1, 2, 3, 5, 8};
3854 static const int test_vals[] = {1, 2, 3, 5, 8};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003855 vector<int> my_set(vals, vals + 6);
3856 vector<int> test_set(test_vals, test_vals + 5);
3857 const Matcher<vector<int> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003858 EXPECT_TRUE(m.Matches(my_set));
3859 EXPECT_FALSE(m.Matches(test_set));
3860 // There is nothing to report when both sets contain all the same values.
3861 EXPECT_EQ("", Explain(m, test_set));
3862}
3863
3864// Tests that ContainerEq works for non-trivial associative containers,
3865// like maps.
3866TEST(ContainerEqExtraTest, WorksForMaps) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003867 map<int, std::string> my_map;
zhanyong.wan6a896b52009-01-16 01:13:50 +00003868 my_map[0] = "a";
3869 my_map[1] = "b";
3870
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003871 map<int, std::string> test_map;
zhanyong.wan6a896b52009-01-16 01:13:50 +00003872 test_map[0] = "aa";
3873 test_map[1] = "b";
3874
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003875 const Matcher<const map<int, std::string>&> m = ContainerEq(my_map);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003876 EXPECT_TRUE(m.Matches(my_map));
3877 EXPECT_FALSE(m.Matches(test_map));
3878
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003879 EXPECT_EQ("which has these unexpected elements: (0, \"aa\"),\n"
3880 "and doesn't have these expected elements: (0, \"a\")",
zhanyong.wan6a896b52009-01-16 01:13:50 +00003881 Explain(m, test_map));
3882}
3883
zhanyong.wanb8243162009-06-04 05:48:20 +00003884TEST(ContainerEqExtraTest, WorksForNativeArray) {
3885 int a1[] = { 1, 2, 3 };
3886 int a2[] = { 1, 2, 3 };
3887 int b[] = { 1, 2, 4 };
3888
3889 EXPECT_THAT(a1, ContainerEq(a2));
3890 EXPECT_THAT(a1, Not(ContainerEq(b)));
3891}
3892
3893TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) {
3894 const char a1[][3] = { "hi", "lo" };
3895 const char a2[][3] = { "hi", "lo" };
3896 const char b[][3] = { "lo", "hi" };
3897
3898 // Tests using ContainerEq() in the first dimension.
3899 EXPECT_THAT(a1, ContainerEq(a2));
3900 EXPECT_THAT(a1, Not(ContainerEq(b)));
3901
3902 // Tests using ContainerEq() in the second dimension.
3903 EXPECT_THAT(a1, ElementsAre(ContainerEq(a2[0]), ContainerEq(a2[1])));
3904 EXPECT_THAT(a1, ElementsAre(Not(ContainerEq(b[0])), ContainerEq(a2[1])));
3905}
3906
3907TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) {
3908 const int a1[] = { 1, 2, 3 };
3909 const int a2[] = { 1, 2, 3 };
3910 const int b[] = { 1, 2, 3, 4 };
3911
zhanyong.wan2661c682009-06-09 05:42:12 +00003912 const int* const p1 = a1;
3913 EXPECT_THAT(make_tuple(p1, 3), ContainerEq(a2));
3914 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(b)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003915
3916 const int c[] = { 1, 3, 2 };
zhanyong.wan2661c682009-06-09 05:42:12 +00003917 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(c)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003918}
3919
3920TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) {
3921 std::string a1[][3] = {
3922 { "hi", "hello", "ciao" },
3923 { "bye", "see you", "ciao" }
3924 };
3925
3926 std::string a2[][3] = {
3927 { "hi", "hello", "ciao" },
3928 { "bye", "see you", "ciao" }
3929 };
3930
3931 const Matcher<const std::string(&)[2][3]> m = ContainerEq(a2);
3932 EXPECT_THAT(a1, m);
3933
3934 a2[0][0] = "ha";
3935 EXPECT_THAT(a1, m);
3936}
3937
zhanyong.wan898725c2011-09-16 16:45:39 +00003938TEST(WhenSortedByTest, WorksForEmptyContainer) {
3939 const vector<int> numbers;
3940 EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre()));
3941 EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1))));
3942}
3943
3944TEST(WhenSortedByTest, WorksForNonEmptyContainer) {
3945 vector<unsigned> numbers;
3946 numbers.push_back(3);
3947 numbers.push_back(1);
3948 numbers.push_back(2);
3949 numbers.push_back(2);
3950 EXPECT_THAT(numbers, WhenSortedBy(greater<unsigned>(),
3951 ElementsAre(3, 2, 2, 1)));
3952 EXPECT_THAT(numbers, Not(WhenSortedBy(greater<unsigned>(),
3953 ElementsAre(1, 2, 2, 3))));
3954}
3955
3956TEST(WhenSortedByTest, WorksForNonVectorContainer) {
3957 list<string> words;
3958 words.push_back("say");
3959 words.push_back("hello");
3960 words.push_back("world");
3961 EXPECT_THAT(words, WhenSortedBy(less<string>(),
3962 ElementsAre("hello", "say", "world")));
3963 EXPECT_THAT(words, Not(WhenSortedBy(less<string>(),
3964 ElementsAre("say", "hello", "world"))));
3965}
3966
3967TEST(WhenSortedByTest, WorksForNativeArray) {
3968 const int numbers[] = { 1, 3, 2, 4 };
3969 const int sorted_numbers[] = { 1, 2, 3, 4 };
3970 EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre(1, 2, 3, 4)));
3971 EXPECT_THAT(numbers, WhenSortedBy(less<int>(),
3972 ElementsAreArray(sorted_numbers)));
3973 EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1, 3, 2, 4))));
3974}
3975
3976TEST(WhenSortedByTest, CanDescribeSelf) {
3977 const Matcher<vector<int> > m = WhenSortedBy(less<int>(), ElementsAre(1, 2));
3978 EXPECT_EQ("(when sorted) has 2 elements where\n"
3979 "element #0 is equal to 1,\n"
3980 "element #1 is equal to 2",
3981 Describe(m));
3982 EXPECT_EQ("(when sorted) doesn't have 2 elements, or\n"
3983 "element #0 isn't equal to 1, or\n"
3984 "element #1 isn't equal to 2",
3985 DescribeNegation(m));
3986}
3987
3988TEST(WhenSortedByTest, ExplainsMatchResult) {
3989 const int a[] = { 2, 1 };
3990 EXPECT_EQ("which is { 1, 2 } when sorted, whose element #0 doesn't match",
3991 Explain(WhenSortedBy(less<int>(), ElementsAre(2, 3)), a));
3992 EXPECT_EQ("which is { 1, 2 } when sorted",
3993 Explain(WhenSortedBy(less<int>(), ElementsAre(1, 2)), a));
3994}
3995
3996// WhenSorted() is a simple wrapper on WhenSortedBy(). Hence we don't
3997// need to test it as exhaustively as we test the latter.
3998
3999TEST(WhenSortedTest, WorksForEmptyContainer) {
4000 const vector<int> numbers;
4001 EXPECT_THAT(numbers, WhenSorted(ElementsAre()));
4002 EXPECT_THAT(numbers, Not(WhenSorted(ElementsAre(1))));
4003}
4004
4005TEST(WhenSortedTest, WorksForNonEmptyContainer) {
4006 list<string> words;
4007 words.push_back("3");
4008 words.push_back("1");
4009 words.push_back("2");
4010 words.push_back("2");
4011 EXPECT_THAT(words, WhenSorted(ElementsAre("1", "2", "2", "3")));
4012 EXPECT_THAT(words, Not(WhenSorted(ElementsAre("3", "1", "2", "2"))));
4013}
4014
zhanyong.wan736baa82010-09-27 17:44:16 +00004015// Tests IsReadableTypeName().
4016
4017TEST(IsReadableTypeNameTest, ReturnsTrueForShortNames) {
4018 EXPECT_TRUE(IsReadableTypeName("int"));
4019 EXPECT_TRUE(IsReadableTypeName("const unsigned char*"));
4020 EXPECT_TRUE(IsReadableTypeName("MyMap<int, void*>"));
4021 EXPECT_TRUE(IsReadableTypeName("void (*)(int, bool)"));
4022}
4023
4024TEST(IsReadableTypeNameTest, ReturnsTrueForLongNonTemplateNonFunctionNames) {
4025 EXPECT_TRUE(IsReadableTypeName("my_long_namespace::MyClassName"));
4026 EXPECT_TRUE(IsReadableTypeName("int [5][6][7][8][9][10][11]"));
4027 EXPECT_TRUE(IsReadableTypeName("my_namespace::MyOuterClass::MyInnerClass"));
4028}
4029
4030TEST(IsReadableTypeNameTest, ReturnsFalseForLongTemplateNames) {
4031 EXPECT_FALSE(
4032 IsReadableTypeName("basic_string<char, std::char_traits<char> >"));
4033 EXPECT_FALSE(IsReadableTypeName("std::vector<int, std::alloc_traits<int> >"));
4034}
4035
4036TEST(IsReadableTypeNameTest, ReturnsFalseForLongFunctionTypeNames) {
4037 EXPECT_FALSE(IsReadableTypeName("void (&)(int, bool, char, float)"));
4038}
4039
zhanyong.wan4a5330d2009-02-19 00:36:44 +00004040// Tests JoinAsTuple().
4041
4042TEST(JoinAsTupleTest, JoinsEmptyTuple) {
4043 EXPECT_EQ("", JoinAsTuple(Strings()));
4044}
4045
4046TEST(JoinAsTupleTest, JoinsOneTuple) {
4047 const char* fields[] = { "1" };
4048 EXPECT_EQ("1", JoinAsTuple(Strings(fields, fields + 1)));
4049}
4050
4051TEST(JoinAsTupleTest, JoinsTwoTuple) {
4052 const char* fields[] = { "1", "a" };
4053 EXPECT_EQ("(1, a)", JoinAsTuple(Strings(fields, fields + 2)));
4054}
4055
4056TEST(JoinAsTupleTest, JoinsTenTuple) {
4057 const char* fields[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
4058 EXPECT_EQ("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)",
4059 JoinAsTuple(Strings(fields, fields + 10)));
4060}
4061
4062// Tests FormatMatcherDescription().
4063
4064TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) {
4065 EXPECT_EQ("is even",
zhanyong.wanb4140802010-06-08 22:53:57 +00004066 FormatMatcherDescription(false, "IsEven", Strings()));
4067 EXPECT_EQ("not (is even)",
4068 FormatMatcherDescription(true, "IsEven", Strings()));
zhanyong.wan4a5330d2009-02-19 00:36:44 +00004069
4070 const char* params[] = { "5" };
4071 EXPECT_EQ("equals 5",
zhanyong.wanb4140802010-06-08 22:53:57 +00004072 FormatMatcherDescription(false, "Equals",
zhanyong.wan4a5330d2009-02-19 00:36:44 +00004073 Strings(params, params + 1)));
4074
4075 const char* params2[] = { "5", "8" };
4076 EXPECT_EQ("is in range (5, 8)",
zhanyong.wanb4140802010-06-08 22:53:57 +00004077 FormatMatcherDescription(false, "IsInRange",
zhanyong.wan4a5330d2009-02-19 00:36:44 +00004078 Strings(params2, params2 + 2)));
4079}
4080
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00004081// Tests PolymorphicMatcher::mutable_impl().
4082TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {
4083 PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
4084 DivisibleByImpl& impl = m.mutable_impl();
4085 EXPECT_EQ(42, impl.divider());
4086
4087 impl.set_divider(0);
4088 EXPECT_EQ(0, m.mutable_impl().divider());
4089}
4090
4091// Tests PolymorphicMatcher::impl().
4092TEST(PolymorphicMatcherTest, CanAccessImpl) {
4093 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
4094 const DivisibleByImpl& impl = m.impl();
4095 EXPECT_EQ(42, impl.divider());
4096}
4097
zhanyong.wanb1c7f932010-03-24 17:35:11 +00004098TEST(MatcherTupleTest, ExplainsMatchFailure) {
4099 stringstream ss1;
4100 ExplainMatchFailureTupleTo(make_tuple(Matcher<char>(Eq('a')), GreaterThan(5)),
4101 make_tuple('a', 10), &ss1);
4102 EXPECT_EQ("", ss1.str()); // Successful match.
4103
4104 stringstream ss2;
4105 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
4106 make_tuple(2, 'b'), &ss2);
4107 EXPECT_EQ(" Expected arg #0: is > 5\n"
4108 " Actual: 2, which is 3 less than 5\n"
zhanyong.wand60c5f42010-07-21 22:21:07 +00004109 " Expected arg #1: is equal to 'a' (97, 0x61)\n"
4110 " Actual: 'b' (98, 0x62)\n",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00004111 ss2.str()); // Failed match where both arguments need explanation.
4112
4113 stringstream ss3;
4114 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
4115 make_tuple(2, 'a'), &ss3);
4116 EXPECT_EQ(" Expected arg #0: is > 5\n"
4117 " Actual: 2, which is 3 less than 5\n",
4118 ss3.str()); // Failed match where only one argument needs
4119 // explanation.
4120}
4121
zhanyong.wan33605ba2010-04-22 23:37:47 +00004122// Tests Each().
4123
4124TEST(EachTest, ExplainsMatchResultCorrectly) {
4125 set<int> a; // empty
4126
4127 Matcher<set<int> > m = Each(2);
4128 EXPECT_EQ("", Explain(m, a));
4129
zhanyong.wan736baa82010-09-27 17:44:16 +00004130 Matcher<const int(&)[1]> n = Each(1); // NOLINT
zhanyong.wan33605ba2010-04-22 23:37:47 +00004131
4132 const int b[1] = { 1 };
4133 EXPECT_EQ("", Explain(n, b));
4134
4135 n = Each(3);
4136 EXPECT_EQ("whose element #0 doesn't match", Explain(n, b));
4137
4138 a.insert(1);
4139 a.insert(2);
4140 a.insert(3);
4141 m = Each(GreaterThan(0));
4142 EXPECT_EQ("", Explain(m, a));
4143
4144 m = Each(GreaterThan(10));
4145 EXPECT_EQ("whose element #0 doesn't match, which is 9 less than 10",
4146 Explain(m, a));
4147}
4148
4149TEST(EachTest, DescribesItselfCorrectly) {
4150 Matcher<vector<int> > m = Each(1);
4151 EXPECT_EQ("only contains elements that is equal to 1", Describe(m));
4152
4153 Matcher<vector<int> > m2 = Not(m);
4154 EXPECT_EQ("contains some element that isn't equal to 1", Describe(m2));
4155}
4156
4157TEST(EachTest, MatchesVectorWhenAllElementsMatch) {
4158 vector<int> some_vector;
4159 EXPECT_THAT(some_vector, Each(1));
4160 some_vector.push_back(3);
4161 EXPECT_THAT(some_vector, Not(Each(1)));
4162 EXPECT_THAT(some_vector, Each(3));
4163 some_vector.push_back(1);
4164 some_vector.push_back(2);
4165 EXPECT_THAT(some_vector, Not(Each(3)));
4166 EXPECT_THAT(some_vector, Each(Lt(3.5)));
4167
4168 vector<string> another_vector;
4169 another_vector.push_back("fee");
4170 EXPECT_THAT(another_vector, Each(string("fee")));
4171 another_vector.push_back("fie");
4172 another_vector.push_back("foe");
4173 another_vector.push_back("fum");
4174 EXPECT_THAT(another_vector, Not(Each(string("fee"))));
4175}
4176
4177TEST(EachTest, MatchesMapWhenAllElementsMatch) {
4178 map<const char*, int> my_map;
4179 const char* bar = "a string";
4180 my_map[bar] = 2;
4181 EXPECT_THAT(my_map, Each(make_pair(bar, 2)));
4182
4183 map<string, int> another_map;
4184 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1)));
4185 another_map["fee"] = 1;
4186 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1)));
4187 another_map["fie"] = 2;
4188 another_map["foe"] = 3;
4189 another_map["fum"] = 4;
4190 EXPECT_THAT(another_map, Not(Each(make_pair(string("fee"), 1))));
4191 EXPECT_THAT(another_map, Not(Each(make_pair(string("fum"), 1))));
4192 EXPECT_THAT(another_map, Each(Pair(_, Gt(0))));
4193}
4194
4195TEST(EachTest, AcceptsMatcher) {
4196 const int a[] = { 1, 2, 3 };
4197 EXPECT_THAT(a, Each(Gt(0)));
4198 EXPECT_THAT(a, Not(Each(Gt(1))));
4199}
4200
4201TEST(EachTest, WorksForNativeArrayAsTuple) {
4202 const int a[] = { 1, 2 };
4203 const int* const pointer = a;
4204 EXPECT_THAT(make_tuple(pointer, 2), Each(Gt(0)));
4205 EXPECT_THAT(make_tuple(pointer, 2), Not(Each(Gt(1))));
4206}
4207
zhanyong.wanab5b77c2010-05-17 19:32:48 +00004208// For testing Pointwise().
4209class IsHalfOfMatcher {
4210 public:
4211 template <typename T1, typename T2>
4212 bool MatchAndExplain(const tuple<T1, T2>& a_pair,
4213 MatchResultListener* listener) const {
4214 if (get<0>(a_pair) == get<1>(a_pair)/2) {
4215 *listener << "where the second is " << get<1>(a_pair);
4216 return true;
4217 } else {
4218 *listener << "where the second/2 is " << get<1>(a_pair)/2;
4219 return false;
4220 }
4221 }
4222
4223 void DescribeTo(ostream* os) const {
4224 *os << "are a pair where the first is half of the second";
4225 }
4226
4227 void DescribeNegationTo(ostream* os) const {
4228 *os << "are a pair where the first isn't half of the second";
4229 }
4230};
4231
4232PolymorphicMatcher<IsHalfOfMatcher> IsHalfOf() {
4233 return MakePolymorphicMatcher(IsHalfOfMatcher());
4234}
4235
4236TEST(PointwiseTest, DescribesSelf) {
4237 vector<int> rhs;
4238 rhs.push_back(1);
4239 rhs.push_back(2);
4240 rhs.push_back(3);
4241 const Matcher<const vector<int>&> m = Pointwise(IsHalfOf(), rhs);
4242 EXPECT_EQ("contains 3 values, where each value and its corresponding value "
4243 "in { 1, 2, 3 } are a pair where the first is half of the second",
4244 Describe(m));
4245 EXPECT_EQ("doesn't contain exactly 3 values, or contains a value x at some "
4246 "index i where x and the i-th value of { 1, 2, 3 } are a pair "
4247 "where the first isn't half of the second",
4248 DescribeNegation(m));
4249}
4250
4251TEST(PointwiseTest, MakesCopyOfRhs) {
4252 list<signed char> rhs;
4253 rhs.push_back(2);
4254 rhs.push_back(4);
4255
4256 int lhs[] = { 1, 2 };
4257 const Matcher<const int (&)[2]> m = Pointwise(IsHalfOf(), rhs);
4258 EXPECT_THAT(lhs, m);
4259
4260 // Changing rhs now shouldn't affect m, which made a copy of rhs.
4261 rhs.push_back(6);
4262 EXPECT_THAT(lhs, m);
4263}
4264
4265TEST(PointwiseTest, WorksForLhsNativeArray) {
4266 const int lhs[] = { 1, 2, 3 };
4267 vector<int> rhs;
4268 rhs.push_back(2);
4269 rhs.push_back(4);
4270 rhs.push_back(6);
4271 EXPECT_THAT(lhs, Pointwise(Lt(), rhs));
4272 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
4273}
4274
4275TEST(PointwiseTest, WorksForRhsNativeArray) {
4276 const int rhs[] = { 1, 2, 3 };
4277 vector<int> lhs;
4278 lhs.push_back(2);
4279 lhs.push_back(4);
4280 lhs.push_back(6);
4281 EXPECT_THAT(lhs, Pointwise(Gt(), rhs));
4282 EXPECT_THAT(lhs, Not(Pointwise(Lt(), rhs)));
4283}
4284
4285TEST(PointwiseTest, RejectsWrongSize) {
4286 const double lhs[2] = { 1, 2 };
4287 const int rhs[1] = { 0 };
4288 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
4289 EXPECT_EQ("which contains 2 values",
4290 Explain(Pointwise(Gt(), rhs), lhs));
4291
4292 const int rhs2[3] = { 0, 1, 2 };
4293 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs2)));
4294}
4295
4296TEST(PointwiseTest, RejectsWrongContent) {
4297 const double lhs[3] = { 1, 2, 3 };
4298 const int rhs[3] = { 2, 6, 4 };
4299 EXPECT_THAT(lhs, Not(Pointwise(IsHalfOf(), rhs)));
4300 EXPECT_EQ("where the value pair (2, 6) at index #1 don't match, "
4301 "where the second/2 is 3",
4302 Explain(Pointwise(IsHalfOf(), rhs), lhs));
4303}
4304
4305TEST(PointwiseTest, AcceptsCorrectContent) {
4306 const double lhs[3] = { 1, 2, 3 };
4307 const int rhs[3] = { 2, 4, 6 };
4308 EXPECT_THAT(lhs, Pointwise(IsHalfOf(), rhs));
4309 EXPECT_EQ("", Explain(Pointwise(IsHalfOf(), rhs), lhs));
4310}
4311
4312TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) {
4313 const double lhs[3] = { 1, 2, 3 };
4314 const int rhs[3] = { 2, 4, 6 };
4315 const Matcher<tuple<const double&, const int&> > m1 = IsHalfOf();
4316 EXPECT_THAT(lhs, Pointwise(m1, rhs));
4317 EXPECT_EQ("", Explain(Pointwise(m1, rhs), lhs));
4318
4319 // This type works as a tuple<const double&, const int&> can be
4320 // implicitly cast to tuple<double, int>.
4321 const Matcher<tuple<double, int> > m2 = IsHalfOf();
4322 EXPECT_THAT(lhs, Pointwise(m2, rhs));
4323 EXPECT_EQ("", Explain(Pointwise(m2, rhs), lhs));
4324}
4325
shiqiane35fdd92008-12-10 05:08:54 +00004326} // namespace gmock_matchers_test
4327} // namespace testing