blob: 29d9ed04dff3e60910b08f3df85dc3a7a9bfd81f [file] [log] [blame]
shiqiane35fdd92008-12-10 05:08:54 +00001// Copyright 2007, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29//
30// Author: wan@google.com (Zhanyong Wan)
31
32// Google Mock - a framework for writing C++ mock classes.
33//
34// This file tests some commonly used argument matchers.
35
zhanyong.wan53e08c42010-09-14 05:38:21 +000036#include "gmock/gmock-matchers.h"
zhanyong.wan320814a2013-03-01 00:20:30 +000037#include "gmock/gmock-more-matchers.h"
shiqiane35fdd92008-12-10 05:08:54 +000038
39#include <string.h>
zhanyong.wana9a59e02013-03-27 16:14:55 +000040#include <deque>
shiqiane35fdd92008-12-10 05:08:54 +000041#include <functional>
zhanyong.wana862f1d2010-03-15 21:23:04 +000042#include <iostream>
zhanyong.wana9a59e02013-03-27 16:14:55 +000043#include <iterator>
zhanyong.wan616180e2013-06-18 18:49:51 +000044#include <limits>
zhanyong.wan6a896b52009-01-16 01:13:50 +000045#include <list>
46#include <map>
47#include <set>
shiqiane35fdd92008-12-10 05:08:54 +000048#include <sstream>
zhanyong.wan6a896b52009-01-16 01:13:50 +000049#include <string>
zhanyong.wanf5e1ce52009-09-16 07:02:02 +000050#include <utility>
zhanyong.wan6a896b52009-01-16 01:13:50 +000051#include <vector>
zhanyong.wan53e08c42010-09-14 05:38:21 +000052#include "gmock/gmock.h"
53#include "gtest/gtest.h"
54#include "gtest/gtest-spi.h"
shiqiane35fdd92008-12-10 05:08:54 +000055
56namespace testing {
zhanyong.wan4a5330d2009-02-19 00:36:44 +000057
58namespace internal {
vladlosev587c1b32011-05-20 00:42:22 +000059GTEST_API_ string JoinAsTuple(const Strings& fields);
zhanyong.wan4a5330d2009-02-19 00:36:44 +000060} // namespace internal
61
shiqiane35fdd92008-12-10 05:08:54 +000062namespace gmock_matchers_test {
63
zhanyong.wan898725c2011-09-16 16:45:39 +000064using std::greater;
65using std::less;
zhanyong.wanab5b77c2010-05-17 19:32:48 +000066using std::list;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000067using std::make_pair;
zhanyong.wanb5937da2009-07-16 20:26:41 +000068using std::map;
69using std::multimap;
zhanyong.wanab5b77c2010-05-17 19:32:48 +000070using std::multiset;
71using std::ostream;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000072using std::pair;
zhanyong.wan33605ba2010-04-22 23:37:47 +000073using std::set;
shiqiane35fdd92008-12-10 05:08:54 +000074using std::stringstream;
zhanyong.wanab5b77c2010-05-17 19:32:48 +000075using std::tr1::get;
zhanyong.wanb8243162009-06-04 05:48:20 +000076using std::tr1::make_tuple;
zhanyong.wanab5b77c2010-05-17 19:32:48 +000077using std::tr1::tuple;
zhanyong.wan33605ba2010-04-22 23:37:47 +000078using std::vector;
shiqiane35fdd92008-12-10 05:08:54 +000079using testing::A;
zhanyong.wanbf550852009-06-09 06:09:53 +000080using testing::AllArgs;
shiqiane35fdd92008-12-10 05:08:54 +000081using testing::AllOf;
82using testing::An;
83using testing::AnyOf;
84using testing::ByRef;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000085using testing::ContainsRegex;
shiqiane35fdd92008-12-10 05:08:54 +000086using testing::DoubleEq;
zhanyong.wan616180e2013-06-18 18:49:51 +000087using testing::DoubleNear;
shiqiane35fdd92008-12-10 05:08:54 +000088using testing::EndsWith;
89using testing::Eq;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000090using testing::ExplainMatchResult;
shiqiane35fdd92008-12-10 05:08:54 +000091using testing::Field;
92using testing::FloatEq;
zhanyong.wan616180e2013-06-18 18:49:51 +000093using testing::FloatNear;
shiqiane35fdd92008-12-10 05:08:54 +000094using testing::Ge;
95using testing::Gt;
96using testing::HasSubstr;
zhanyong.wan320814a2013-03-01 00:20:30 +000097using testing::IsEmpty;
zhanyong.wan2d970ee2009-09-24 21:41:36 +000098using testing::IsNull;
zhanyong.wanb5937da2009-07-16 20:26:41 +000099using testing::Key;
shiqiane35fdd92008-12-10 05:08:54 +0000100using testing::Le;
101using testing::Lt;
102using testing::MakeMatcher;
103using testing::MakePolymorphicMatcher;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000104using testing::MatchResultListener;
shiqiane35fdd92008-12-10 05:08:54 +0000105using testing::Matcher;
106using testing::MatcherCast;
107using testing::MatcherInterface;
108using testing::Matches;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000109using testing::MatchesRegex;
shiqiane35fdd92008-12-10 05:08:54 +0000110using testing::NanSensitiveDoubleEq;
zhanyong.wan616180e2013-06-18 18:49:51 +0000111using testing::NanSensitiveDoubleNear;
shiqiane35fdd92008-12-10 05:08:54 +0000112using testing::NanSensitiveFloatEq;
zhanyong.wan616180e2013-06-18 18:49:51 +0000113using testing::NanSensitiveFloatNear;
shiqiane35fdd92008-12-10 05:08:54 +0000114using testing::Ne;
115using testing::Not;
116using testing::NotNull;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000117using testing::Pair;
shiqiane35fdd92008-12-10 05:08:54 +0000118using testing::Pointee;
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000119using testing::Pointwise;
shiqiane35fdd92008-12-10 05:08:54 +0000120using testing::PolymorphicMatcher;
121using testing::Property;
122using testing::Ref;
123using testing::ResultOf;
zhanyong.wana31d9ce2013-03-01 01:50:17 +0000124using testing::SizeIs;
shiqiane35fdd92008-12-10 05:08:54 +0000125using testing::StartsWith;
126using testing::StrCaseEq;
127using testing::StrCaseNe;
128using testing::StrEq;
129using testing::StrNe;
130using testing::Truly;
131using testing::TypedEq;
zhanyong.wanb8243162009-06-04 05:48:20 +0000132using testing::Value;
zhanyong.wan898725c2011-09-16 16:45:39 +0000133using testing::WhenSorted;
134using testing::WhenSortedBy;
shiqiane35fdd92008-12-10 05:08:54 +0000135using testing::_;
zhanyong.wana862f1d2010-03-15 21:23:04 +0000136using testing::internal::DummyMatchResultListener;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000137using testing::internal::ExplainMatchFailureTupleTo;
shiqiane35fdd92008-12-10 05:08:54 +0000138using testing::internal::FloatingEqMatcher;
zhanyong.wanb4140802010-06-08 22:53:57 +0000139using testing::internal::FormatMatcherDescription;
zhanyong.wan736baa82010-09-27 17:44:16 +0000140using testing::internal::IsReadableTypeName;
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000141using testing::internal::JoinAsTuple;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000142using testing::internal::RE;
zhanyong.wana862f1d2010-03-15 21:23:04 +0000143using testing::internal::StreamMatchResultListener;
zhanyong.wan34b034c2010-03-05 21:23:23 +0000144using testing::internal::StringMatchResultListener;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000145using testing::internal::Strings;
vladlosev79b83502009-11-18 00:43:37 +0000146using testing::internal::linked_ptr;
vladloseve56daa72009-11-18 01:08:08 +0000147using testing::internal::scoped_ptr;
shiqiane35fdd92008-12-10 05:08:54 +0000148using testing::internal::string;
149
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000150// For testing ExplainMatchResultTo().
151class GreaterThanMatcher : public MatcherInterface<int> {
152 public:
153 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
154
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000155 virtual void DescribeTo(ostream* os) const {
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000156 *os << "is > " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000157 }
158
zhanyong.wandb22c222010-01-28 21:52:29 +0000159 virtual bool MatchAndExplain(int lhs,
160 MatchResultListener* listener) const {
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000161 const int diff = lhs - rhs_;
162 if (diff > 0) {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000163 *listener << "which is " << diff << " more than " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000164 } else if (diff == 0) {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000165 *listener << "which is the same as " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000166 } else {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000167 *listener << "which is " << -diff << " less than " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000168 }
zhanyong.wandb22c222010-01-28 21:52:29 +0000169
170 return lhs > rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000171 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000172
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000173 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000174 int rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000175};
176
177Matcher<int> GreaterThan(int n) {
178 return MakeMatcher(new GreaterThanMatcher(n));
179}
180
zhanyong.wan736baa82010-09-27 17:44:16 +0000181string OfType(const string& type_name) {
182#if GTEST_HAS_RTTI
183 return " (of type " + type_name + ")";
184#else
185 return "";
186#endif
187}
188
shiqiane35fdd92008-12-10 05:08:54 +0000189// Returns the description of the given matcher.
190template <typename T>
191string Describe(const Matcher<T>& m) {
192 stringstream ss;
193 m.DescribeTo(&ss);
194 return ss.str();
195}
196
197// Returns the description of the negation of the given matcher.
198template <typename T>
199string DescribeNegation(const Matcher<T>& m) {
200 stringstream ss;
201 m.DescribeNegationTo(&ss);
202 return ss.str();
203}
204
205// Returns the reason why x matches, or doesn't match, m.
206template <typename MatcherType, typename Value>
207string Explain(const MatcherType& m, const Value& x) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000208 StringMatchResultListener listener;
209 ExplainMatchResult(m, x, &listener);
210 return listener.str();
shiqiane35fdd92008-12-10 05:08:54 +0000211}
212
zhanyong.wana862f1d2010-03-15 21:23:04 +0000213TEST(MatchResultListenerTest, StreamingWorks) {
214 StringMatchResultListener listener;
215 listener << "hi" << 5;
216 EXPECT_EQ("hi5", listener.str());
217
218 // Streaming shouldn't crash when the underlying ostream is NULL.
219 DummyMatchResultListener dummy;
220 dummy << "hi" << 5;
221}
222
223TEST(MatchResultListenerTest, CanAccessUnderlyingStream) {
224 EXPECT_TRUE(DummyMatchResultListener().stream() == NULL);
225 EXPECT_TRUE(StreamMatchResultListener(NULL).stream() == NULL);
226
227 EXPECT_EQ(&std::cout, StreamMatchResultListener(&std::cout).stream());
228}
229
230TEST(MatchResultListenerTest, IsInterestedWorks) {
231 EXPECT_TRUE(StringMatchResultListener().IsInterested());
232 EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested());
233
234 EXPECT_FALSE(DummyMatchResultListener().IsInterested());
235 EXPECT_FALSE(StreamMatchResultListener(NULL).IsInterested());
236}
237
shiqiane35fdd92008-12-10 05:08:54 +0000238// Makes sure that the MatcherInterface<T> interface doesn't
239// change.
240class EvenMatcherImpl : public MatcherInterface<int> {
241 public:
zhanyong.wandb22c222010-01-28 21:52:29 +0000242 virtual bool MatchAndExplain(int x,
243 MatchResultListener* /* listener */) const {
244 return x % 2 == 0;
245 }
shiqiane35fdd92008-12-10 05:08:54 +0000246
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000247 virtual void DescribeTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +0000248 *os << "is an even number";
249 }
250
251 // We deliberately don't define DescribeNegationTo() and
252 // ExplainMatchResultTo() here, to make sure the definition of these
253 // two methods is optional.
254};
255
zhanyong.wana862f1d2010-03-15 21:23:04 +0000256// Makes sure that the MatcherInterface API doesn't change.
257TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) {
shiqiane35fdd92008-12-10 05:08:54 +0000258 EvenMatcherImpl m;
259}
260
zhanyong.wan82113312010-01-08 21:55:40 +0000261// Tests implementing a monomorphic matcher using MatchAndExplain().
262
263class NewEvenMatcherImpl : public MatcherInterface<int> {
264 public:
265 virtual bool MatchAndExplain(int x, MatchResultListener* listener) const {
266 const bool match = x % 2 == 0;
267 // Verifies that we can stream to a listener directly.
268 *listener << "value % " << 2;
269 if (listener->stream() != NULL) {
270 // Verifies that we can stream to a listener's underlying stream
271 // too.
272 *listener->stream() << " == " << (x % 2);
273 }
274 return match;
275 }
276
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000277 virtual void DescribeTo(ostream* os) const {
zhanyong.wan82113312010-01-08 21:55:40 +0000278 *os << "is an even number";
279 }
280};
281
282TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {
283 Matcher<int> m = MakeMatcher(new NewEvenMatcherImpl);
284 EXPECT_TRUE(m.Matches(2));
285 EXPECT_FALSE(m.Matches(3));
286 EXPECT_EQ("value % 2 == 0", Explain(m, 2));
287 EXPECT_EQ("value % 2 == 1", Explain(m, 3));
288}
289
shiqiane35fdd92008-12-10 05:08:54 +0000290// Tests default-constructing a matcher.
291TEST(MatcherTest, CanBeDefaultConstructed) {
292 Matcher<double> m;
293}
294
295// Tests that Matcher<T> can be constructed from a MatcherInterface<T>*.
296TEST(MatcherTest, CanBeConstructedFromMatcherInterface) {
297 const MatcherInterface<int>* impl = new EvenMatcherImpl;
298 Matcher<int> m(impl);
299 EXPECT_TRUE(m.Matches(4));
300 EXPECT_FALSE(m.Matches(5));
301}
302
303// Tests that value can be used in place of Eq(value).
304TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) {
305 Matcher<int> m1 = 5;
306 EXPECT_TRUE(m1.Matches(5));
307 EXPECT_FALSE(m1.Matches(6));
308}
309
310// Tests that NULL can be used in place of Eq(NULL).
311TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) {
312 Matcher<int*> m1 = NULL;
313 EXPECT_TRUE(m1.Matches(NULL));
314 int n = 0;
315 EXPECT_FALSE(m1.Matches(&n));
316}
317
318// Tests that matchers are copyable.
319TEST(MatcherTest, IsCopyable) {
320 // Tests the copy constructor.
321 Matcher<bool> m1 = Eq(false);
322 EXPECT_TRUE(m1.Matches(false));
323 EXPECT_FALSE(m1.Matches(true));
324
325 // Tests the assignment operator.
326 m1 = Eq(true);
327 EXPECT_TRUE(m1.Matches(true));
328 EXPECT_FALSE(m1.Matches(false));
329}
330
331// Tests that Matcher<T>::DescribeTo() calls
332// MatcherInterface<T>::DescribeTo().
333TEST(MatcherTest, CanDescribeItself) {
334 EXPECT_EQ("is an even number",
335 Describe(Matcher<int>(new EvenMatcherImpl)));
336}
337
zhanyong.wan82113312010-01-08 21:55:40 +0000338// Tests Matcher<T>::MatchAndExplain().
339TEST(MatcherTest, MatchAndExplain) {
340 Matcher<int> m = GreaterThan(0);
zhanyong.wan34b034c2010-03-05 21:23:23 +0000341 StringMatchResultListener listener1;
zhanyong.wan82113312010-01-08 21:55:40 +0000342 EXPECT_TRUE(m.MatchAndExplain(42, &listener1));
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000343 EXPECT_EQ("which is 42 more than 0", listener1.str());
zhanyong.wan82113312010-01-08 21:55:40 +0000344
zhanyong.wan34b034c2010-03-05 21:23:23 +0000345 StringMatchResultListener listener2;
zhanyong.wan82113312010-01-08 21:55:40 +0000346 EXPECT_FALSE(m.MatchAndExplain(-9, &listener2));
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000347 EXPECT_EQ("which is 9 less than 0", listener2.str());
zhanyong.wan82113312010-01-08 21:55:40 +0000348}
349
shiqiane35fdd92008-12-10 05:08:54 +0000350// Tests that a C-string literal can be implicitly converted to a
351// Matcher<string> or Matcher<const string&>.
352TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
353 Matcher<string> m1 = "hi";
354 EXPECT_TRUE(m1.Matches("hi"));
355 EXPECT_FALSE(m1.Matches("hello"));
356
357 Matcher<const string&> m2 = "hi";
358 EXPECT_TRUE(m2.Matches("hi"));
359 EXPECT_FALSE(m2.Matches("hello"));
360}
361
362// Tests that a string object can be implicitly converted to a
363// Matcher<string> or Matcher<const string&>.
364TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {
365 Matcher<string> m1 = string("hi");
366 EXPECT_TRUE(m1.Matches("hi"));
367 EXPECT_FALSE(m1.Matches("hello"));
368
369 Matcher<const string&> m2 = string("hi");
370 EXPECT_TRUE(m2.Matches("hi"));
371 EXPECT_FALSE(m2.Matches("hello"));
372}
373
zhanyong.wan1f122a02013-03-25 16:27:03 +0000374#if GTEST_HAS_STRING_PIECE_
375// Tests that a C-string literal can be implicitly converted to a
376// Matcher<StringPiece> or Matcher<const StringPiece&>.
377TEST(StringPieceMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
378 Matcher<StringPiece> m1 = "cats";
379 EXPECT_TRUE(m1.Matches("cats"));
380 EXPECT_FALSE(m1.Matches("dogs"));
381
382 Matcher<const StringPiece&> m2 = "cats";
383 EXPECT_TRUE(m2.Matches("cats"));
384 EXPECT_FALSE(m2.Matches("dogs"));
385}
386
387// Tests that a string object can be implicitly converted to a
388// Matcher<StringPiece> or Matcher<const StringPiece&>.
389TEST(StringPieceMatcherTest, CanBeImplicitlyConstructedFromString) {
390 Matcher<StringPiece> m1 = string("cats");
391 EXPECT_TRUE(m1.Matches("cats"));
392 EXPECT_FALSE(m1.Matches("dogs"));
393
394 Matcher<const StringPiece&> m2 = string("cats");
395 EXPECT_TRUE(m2.Matches("cats"));
396 EXPECT_FALSE(m2.Matches("dogs"));
397}
398
399// Tests that a StringPiece object can be implicitly converted to a
400// Matcher<StringPiece> or Matcher<const StringPiece&>.
401TEST(StringPieceMatcherTest, CanBeImplicitlyConstructedFromStringPiece) {
402 Matcher<StringPiece> m1 = StringPiece("cats");
403 EXPECT_TRUE(m1.Matches("cats"));
404 EXPECT_FALSE(m1.Matches("dogs"));
405
406 Matcher<const StringPiece&> m2 = StringPiece("cats");
407 EXPECT_TRUE(m2.Matches("cats"));
408 EXPECT_FALSE(m2.Matches("dogs"));
409}
410#endif // GTEST_HAS_STRING_PIECE_
411
shiqiane35fdd92008-12-10 05:08:54 +0000412// Tests that MakeMatcher() constructs a Matcher<T> from a
413// MatcherInterface* without requiring the user to explicitly
414// write the type.
415TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) {
416 const MatcherInterface<int>* dummy_impl = NULL;
417 Matcher<int> m = MakeMatcher(dummy_impl);
418}
419
zhanyong.wan82113312010-01-08 21:55:40 +0000420// Tests that MakePolymorphicMatcher() can construct a polymorphic
421// matcher from its implementation using the old API.
zhanyong.wan33605ba2010-04-22 23:37:47 +0000422const int g_bar = 1;
shiqiane35fdd92008-12-10 05:08:54 +0000423class ReferencesBarOrIsZeroImpl {
424 public:
425 template <typename T>
zhanyong.wandb22c222010-01-28 21:52:29 +0000426 bool MatchAndExplain(const T& x,
427 MatchResultListener* /* listener */) const {
shiqiane35fdd92008-12-10 05:08:54 +0000428 const void* p = &x;
zhanyong.wan33605ba2010-04-22 23:37:47 +0000429 return p == &g_bar || x == 0;
shiqiane35fdd92008-12-10 05:08:54 +0000430 }
431
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000432 void DescribeTo(ostream* os) const { *os << "g_bar or zero"; }
shiqiane35fdd92008-12-10 05:08:54 +0000433
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000434 void DescribeNegationTo(ostream* os) const {
zhanyong.wan33605ba2010-04-22 23:37:47 +0000435 *os << "doesn't reference g_bar and is not zero";
shiqiane35fdd92008-12-10 05:08:54 +0000436 }
437};
438
439// This function verifies that MakePolymorphicMatcher() returns a
440// PolymorphicMatcher<T> where T is the argument's type.
441PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() {
442 return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl());
443}
444
zhanyong.wan82113312010-01-08 21:55:40 +0000445TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) {
shiqiane35fdd92008-12-10 05:08:54 +0000446 // Using a polymorphic matcher to match a reference type.
447 Matcher<const int&> m1 = ReferencesBarOrIsZero();
448 EXPECT_TRUE(m1.Matches(0));
449 // Verifies that the identity of a by-reference argument is preserved.
zhanyong.wan33605ba2010-04-22 23:37:47 +0000450 EXPECT_TRUE(m1.Matches(g_bar));
shiqiane35fdd92008-12-10 05:08:54 +0000451 EXPECT_FALSE(m1.Matches(1));
zhanyong.wan33605ba2010-04-22 23:37:47 +0000452 EXPECT_EQ("g_bar or zero", Describe(m1));
shiqiane35fdd92008-12-10 05:08:54 +0000453
454 // Using a polymorphic matcher to match a value type.
455 Matcher<double> m2 = ReferencesBarOrIsZero();
456 EXPECT_TRUE(m2.Matches(0.0));
457 EXPECT_FALSE(m2.Matches(0.1));
zhanyong.wan33605ba2010-04-22 23:37:47 +0000458 EXPECT_EQ("g_bar or zero", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +0000459}
460
zhanyong.wan82113312010-01-08 21:55:40 +0000461// Tests implementing a polymorphic matcher using MatchAndExplain().
462
463class PolymorphicIsEvenImpl {
464 public:
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000465 void DescribeTo(ostream* os) const { *os << "is even"; }
zhanyong.wan82113312010-01-08 21:55:40 +0000466
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000467 void DescribeNegationTo(ostream* os) const {
zhanyong.wan82113312010-01-08 21:55:40 +0000468 *os << "is odd";
469 }
zhanyong.wan82113312010-01-08 21:55:40 +0000470
zhanyong.wandb22c222010-01-28 21:52:29 +0000471 template <typename T>
472 bool MatchAndExplain(const T& x, MatchResultListener* listener) const {
473 // Verifies that we can stream to the listener directly.
474 *listener << "% " << 2;
475 if (listener->stream() != NULL) {
476 // Verifies that we can stream to the listener's underlying stream
477 // too.
478 *listener->stream() << " == " << (x % 2);
479 }
480 return (x % 2) == 0;
zhanyong.wan82113312010-01-08 21:55:40 +0000481 }
zhanyong.wandb22c222010-01-28 21:52:29 +0000482};
zhanyong.wan82113312010-01-08 21:55:40 +0000483
484PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() {
485 return MakePolymorphicMatcher(PolymorphicIsEvenImpl());
486}
487
488TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {
489 // Using PolymorphicIsEven() as a Matcher<int>.
490 const Matcher<int> m1 = PolymorphicIsEven();
491 EXPECT_TRUE(m1.Matches(42));
492 EXPECT_FALSE(m1.Matches(43));
493 EXPECT_EQ("is even", Describe(m1));
494
495 const Matcher<int> not_m1 = Not(m1);
496 EXPECT_EQ("is odd", Describe(not_m1));
497
498 EXPECT_EQ("% 2 == 0", Explain(m1, 42));
499
500 // Using PolymorphicIsEven() as a Matcher<char>.
501 const Matcher<char> m2 = PolymorphicIsEven();
502 EXPECT_TRUE(m2.Matches('\x42'));
503 EXPECT_FALSE(m2.Matches('\x43'));
504 EXPECT_EQ("is even", Describe(m2));
505
506 const Matcher<char> not_m2 = Not(m2);
507 EXPECT_EQ("is odd", Describe(not_m2));
508
509 EXPECT_EQ("% 2 == 0", Explain(m2, '\x42'));
510}
511
shiqiane35fdd92008-12-10 05:08:54 +0000512// Tests that MatcherCast<T>(m) works when m is a polymorphic matcher.
513TEST(MatcherCastTest, FromPolymorphicMatcher) {
514 Matcher<int> m = MatcherCast<int>(Eq(5));
515 EXPECT_TRUE(m.Matches(5));
516 EXPECT_FALSE(m.Matches(6));
517}
518
519// For testing casting matchers between compatible types.
520class IntValue {
521 public:
522 // An int can be statically (although not implicitly) cast to a
523 // IntValue.
zhanyong.wan32de5f52009-12-23 00:13:23 +0000524 explicit IntValue(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +0000525
526 int value() const { return value_; }
527 private:
528 int value_;
529};
530
531// For testing casting matchers between compatible types.
532bool IsPositiveIntValue(const IntValue& foo) {
533 return foo.value() > 0;
534}
535
536// Tests that MatcherCast<T>(m) works when m is a Matcher<U> where T
537// can be statically converted to U.
538TEST(MatcherCastTest, FromCompatibleType) {
539 Matcher<double> m1 = Eq(2.0);
540 Matcher<int> m2 = MatcherCast<int>(m1);
541 EXPECT_TRUE(m2.Matches(2));
542 EXPECT_FALSE(m2.Matches(3));
543
544 Matcher<IntValue> m3 = Truly(IsPositiveIntValue);
545 Matcher<int> m4 = MatcherCast<int>(m3);
546 // In the following, the arguments 1 and 0 are statically converted
547 // to IntValue objects, and then tested by the IsPositiveIntValue()
548 // predicate.
549 EXPECT_TRUE(m4.Matches(1));
550 EXPECT_FALSE(m4.Matches(0));
551}
552
553// Tests that MatcherCast<T>(m) works when m is a Matcher<const T&>.
554TEST(MatcherCastTest, FromConstReferenceToNonReference) {
555 Matcher<const int&> m1 = Eq(0);
556 Matcher<int> m2 = MatcherCast<int>(m1);
557 EXPECT_TRUE(m2.Matches(0));
558 EXPECT_FALSE(m2.Matches(1));
559}
560
561// Tests that MatcherCast<T>(m) works when m is a Matcher<T&>.
562TEST(MatcherCastTest, FromReferenceToNonReference) {
563 Matcher<int&> m1 = Eq(0);
564 Matcher<int> m2 = MatcherCast<int>(m1);
565 EXPECT_TRUE(m2.Matches(0));
566 EXPECT_FALSE(m2.Matches(1));
567}
568
569// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
570TEST(MatcherCastTest, FromNonReferenceToConstReference) {
571 Matcher<int> m1 = Eq(0);
572 Matcher<const int&> m2 = MatcherCast<const int&>(m1);
573 EXPECT_TRUE(m2.Matches(0));
574 EXPECT_FALSE(m2.Matches(1));
575}
576
577// Tests that MatcherCast<T&>(m) works when m is a Matcher<T>.
578TEST(MatcherCastTest, FromNonReferenceToReference) {
579 Matcher<int> m1 = Eq(0);
580 Matcher<int&> m2 = MatcherCast<int&>(m1);
581 int n = 0;
582 EXPECT_TRUE(m2.Matches(n));
583 n = 1;
584 EXPECT_FALSE(m2.Matches(n));
585}
586
587// Tests that MatcherCast<T>(m) works when m is a Matcher<T>.
588TEST(MatcherCastTest, FromSameType) {
589 Matcher<int> m1 = Eq(0);
590 Matcher<int> m2 = MatcherCast<int>(m1);
591 EXPECT_TRUE(m2.Matches(0));
592 EXPECT_FALSE(m2.Matches(1));
593}
594
jgm79a367e2012-04-10 16:02:11 +0000595// Implicitly convertible form any type.
596struct ConvertibleFromAny {
597 ConvertibleFromAny(int a_value) : value(a_value) {}
598 template <typename T>
599 ConvertibleFromAny(const T& a_value) : value(-1) {
600 ADD_FAILURE() << "Conversion constructor called";
601 }
602 int value;
603};
604
605bool operator==(const ConvertibleFromAny& a, const ConvertibleFromAny& b) {
606 return a.value == b.value;
607}
608
609ostream& operator<<(ostream& os, const ConvertibleFromAny& a) {
610 return os << a.value;
611}
612
613TEST(MatcherCastTest, ConversionConstructorIsUsed) {
614 Matcher<ConvertibleFromAny> m = MatcherCast<ConvertibleFromAny>(1);
615 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
616 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
617}
618
619TEST(MatcherCastTest, FromConvertibleFromAny) {
620 Matcher<ConvertibleFromAny> m =
621 MatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
622 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
623 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
624}
625
zhanyong.wan18490652009-05-11 18:54:08 +0000626class Base {};
627class Derived : public Base {};
628
629// Tests that SafeMatcherCast<T>(m) works when m is a polymorphic matcher.
630TEST(SafeMatcherCastTest, FromPolymorphicMatcher) {
631 Matcher<char> m2 = SafeMatcherCast<char>(Eq(32));
632 EXPECT_TRUE(m2.Matches(' '));
633 EXPECT_FALSE(m2.Matches('\n'));
634}
635
zhanyong.wan16cf4732009-05-14 20:55:30 +0000636// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where
637// T and U are arithmetic types and T can be losslessly converted to
638// U.
639TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) {
zhanyong.wan18490652009-05-11 18:54:08 +0000640 Matcher<double> m1 = DoubleEq(1.0);
zhanyong.wan16cf4732009-05-14 20:55:30 +0000641 Matcher<float> m2 = SafeMatcherCast<float>(m1);
642 EXPECT_TRUE(m2.Matches(1.0f));
643 EXPECT_FALSE(m2.Matches(2.0f));
644
645 Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>('a'));
646 EXPECT_TRUE(m3.Matches('a'));
647 EXPECT_FALSE(m3.Matches('b'));
zhanyong.wan18490652009-05-11 18:54:08 +0000648}
649
650// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where T and U
651// are pointers or references to a derived and a base class, correspondingly.
652TEST(SafeMatcherCastTest, FromBaseClass) {
653 Derived d, d2;
654 Matcher<Base*> m1 = Eq(&d);
655 Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1);
656 EXPECT_TRUE(m2.Matches(&d));
657 EXPECT_FALSE(m2.Matches(&d2));
658
659 Matcher<Base&> m3 = Ref(d);
660 Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3);
661 EXPECT_TRUE(m4.Matches(d));
662 EXPECT_FALSE(m4.Matches(d2));
663}
664
665// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<const T&>.
666TEST(SafeMatcherCastTest, FromConstReferenceToReference) {
667 int n = 0;
668 Matcher<const int&> m1 = Ref(n);
669 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
670 int n1 = 0;
671 EXPECT_TRUE(m2.Matches(n));
672 EXPECT_FALSE(m2.Matches(n1));
673}
674
675// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
676TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) {
677 Matcher<int> m1 = Eq(0);
678 Matcher<const int&> m2 = SafeMatcherCast<const int&>(m1);
679 EXPECT_TRUE(m2.Matches(0));
680 EXPECT_FALSE(m2.Matches(1));
681}
682
683// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<T>.
684TEST(SafeMatcherCastTest, FromNonReferenceToReference) {
685 Matcher<int> m1 = Eq(0);
686 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
687 int n = 0;
688 EXPECT_TRUE(m2.Matches(n));
689 n = 1;
690 EXPECT_FALSE(m2.Matches(n));
691}
692
693// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<T>.
694TEST(SafeMatcherCastTest, FromSameType) {
695 Matcher<int> m1 = Eq(0);
696 Matcher<int> m2 = SafeMatcherCast<int>(m1);
697 EXPECT_TRUE(m2.Matches(0));
698 EXPECT_FALSE(m2.Matches(1));
699}
700
jgm79a367e2012-04-10 16:02:11 +0000701TEST(SafeMatcherCastTest, ConversionConstructorIsUsed) {
702 Matcher<ConvertibleFromAny> m = SafeMatcherCast<ConvertibleFromAny>(1);
703 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
704 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
705}
706
707TEST(SafeMatcherCastTest, FromConvertibleFromAny) {
708 Matcher<ConvertibleFromAny> m =
709 SafeMatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
710 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
711 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
712}
713
shiqiane35fdd92008-12-10 05:08:54 +0000714// Tests that A<T>() matches any value of type T.
715TEST(ATest, MatchesAnyValue) {
716 // Tests a matcher for a value type.
717 Matcher<double> m1 = A<double>();
718 EXPECT_TRUE(m1.Matches(91.43));
719 EXPECT_TRUE(m1.Matches(-15.32));
720
721 // Tests a matcher for a reference type.
722 int a = 2;
723 int b = -6;
724 Matcher<int&> m2 = A<int&>();
725 EXPECT_TRUE(m2.Matches(a));
726 EXPECT_TRUE(m2.Matches(b));
727}
728
zhanyong.wanf4274522013-04-24 02:49:43 +0000729TEST(ATest, WorksForDerivedClass) {
730 Base base;
731 Derived derived;
732 EXPECT_THAT(&base, A<Base*>());
733 // This shouldn't compile: EXPECT_THAT(&base, A<Derived*>());
734 EXPECT_THAT(&derived, A<Base*>());
735 EXPECT_THAT(&derived, A<Derived*>());
736}
737
shiqiane35fdd92008-12-10 05:08:54 +0000738// Tests that A<T>() describes itself properly.
739TEST(ATest, CanDescribeSelf) {
740 EXPECT_EQ("is anything", Describe(A<bool>()));
741}
742
743// Tests that An<T>() matches any value of type T.
744TEST(AnTest, MatchesAnyValue) {
745 // Tests a matcher for a value type.
746 Matcher<int> m1 = An<int>();
747 EXPECT_TRUE(m1.Matches(9143));
748 EXPECT_TRUE(m1.Matches(-1532));
749
750 // Tests a matcher for a reference type.
751 int a = 2;
752 int b = -6;
753 Matcher<int&> m2 = An<int&>();
754 EXPECT_TRUE(m2.Matches(a));
755 EXPECT_TRUE(m2.Matches(b));
756}
757
758// Tests that An<T>() describes itself properly.
759TEST(AnTest, CanDescribeSelf) {
760 EXPECT_EQ("is anything", Describe(An<int>()));
761}
762
763// Tests that _ can be used as a matcher for any type and matches any
764// value of that type.
765TEST(UnderscoreTest, MatchesAnyValue) {
766 // Uses _ as a matcher for a value type.
767 Matcher<int> m1 = _;
768 EXPECT_TRUE(m1.Matches(123));
769 EXPECT_TRUE(m1.Matches(-242));
770
771 // Uses _ as a matcher for a reference type.
772 bool a = false;
773 const bool b = true;
774 Matcher<const bool&> m2 = _;
775 EXPECT_TRUE(m2.Matches(a));
776 EXPECT_TRUE(m2.Matches(b));
777}
778
779// Tests that _ describes itself properly.
780TEST(UnderscoreTest, CanDescribeSelf) {
781 Matcher<int> m = _;
782 EXPECT_EQ("is anything", Describe(m));
783}
784
785// Tests that Eq(x) matches any value equal to x.
786TEST(EqTest, MatchesEqualValue) {
787 // 2 C-strings with same content but different addresses.
788 const char a1[] = "hi";
789 const char a2[] = "hi";
790
791 Matcher<const char*> m1 = Eq(a1);
792 EXPECT_TRUE(m1.Matches(a1));
793 EXPECT_FALSE(m1.Matches(a2));
794}
795
796// Tests that Eq(v) describes itself properly.
797
798class Unprintable {
799 public:
800 Unprintable() : c_('a') {}
801
zhanyong.wan32de5f52009-12-23 00:13:23 +0000802 bool operator==(const Unprintable& /* rhs */) { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000803 private:
804 char c_;
805};
806
807TEST(EqTest, CanDescribeSelf) {
808 Matcher<Unprintable> m = Eq(Unprintable());
809 EXPECT_EQ("is equal to 1-byte object <61>", Describe(m));
810}
811
812// Tests that Eq(v) can be used to match any type that supports
813// comparing with type T, where T is v's type.
814TEST(EqTest, IsPolymorphic) {
815 Matcher<int> m1 = Eq(1);
816 EXPECT_TRUE(m1.Matches(1));
817 EXPECT_FALSE(m1.Matches(2));
818
819 Matcher<char> m2 = Eq(1);
820 EXPECT_TRUE(m2.Matches('\1'));
821 EXPECT_FALSE(m2.Matches('a'));
822}
823
824// Tests that TypedEq<T>(v) matches values of type T that's equal to v.
825TEST(TypedEqTest, ChecksEqualityForGivenType) {
826 Matcher<char> m1 = TypedEq<char>('a');
827 EXPECT_TRUE(m1.Matches('a'));
828 EXPECT_FALSE(m1.Matches('b'));
829
830 Matcher<int> m2 = TypedEq<int>(6);
831 EXPECT_TRUE(m2.Matches(6));
832 EXPECT_FALSE(m2.Matches(7));
833}
834
835// Tests that TypedEq(v) describes itself properly.
836TEST(TypedEqTest, CanDescribeSelf) {
837 EXPECT_EQ("is equal to 2", Describe(TypedEq<int>(2)));
838}
839
840// Tests that TypedEq<T>(v) has type Matcher<T>.
841
842// Type<T>::IsTypeOf(v) compiles iff the type of value v is T, where T
843// is a "bare" type (i.e. not in the form of const U or U&). If v's
844// type is not T, the compiler will generate a message about
845// "undefined referece".
846template <typename T>
847struct Type {
zhanyong.wan32de5f52009-12-23 00:13:23 +0000848 static bool IsTypeOf(const T& /* v */) { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000849
850 template <typename T2>
851 static void IsTypeOf(T2 v);
852};
853
854TEST(TypedEqTest, HasSpecifiedType) {
855 // Verfies that the type of TypedEq<T>(v) is Matcher<T>.
856 Type<Matcher<int> >::IsTypeOf(TypedEq<int>(5));
857 Type<Matcher<double> >::IsTypeOf(TypedEq<double>(5));
858}
859
860// Tests that Ge(v) matches anything >= v.
861TEST(GeTest, ImplementsGreaterThanOrEqual) {
862 Matcher<int> m1 = Ge(0);
863 EXPECT_TRUE(m1.Matches(1));
864 EXPECT_TRUE(m1.Matches(0));
865 EXPECT_FALSE(m1.Matches(-1));
866}
867
868// Tests that Ge(v) describes itself properly.
869TEST(GeTest, CanDescribeSelf) {
870 Matcher<int> m = Ge(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000871 EXPECT_EQ("is >= 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000872}
873
874// Tests that Gt(v) matches anything > v.
875TEST(GtTest, ImplementsGreaterThan) {
876 Matcher<double> m1 = Gt(0);
877 EXPECT_TRUE(m1.Matches(1.0));
878 EXPECT_FALSE(m1.Matches(0.0));
879 EXPECT_FALSE(m1.Matches(-1.0));
880}
881
882// Tests that Gt(v) describes itself properly.
883TEST(GtTest, CanDescribeSelf) {
884 Matcher<int> m = Gt(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000885 EXPECT_EQ("is > 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000886}
887
888// Tests that Le(v) matches anything <= v.
889TEST(LeTest, ImplementsLessThanOrEqual) {
890 Matcher<char> m1 = Le('b');
891 EXPECT_TRUE(m1.Matches('a'));
892 EXPECT_TRUE(m1.Matches('b'));
893 EXPECT_FALSE(m1.Matches('c'));
894}
895
896// Tests that Le(v) describes itself properly.
897TEST(LeTest, CanDescribeSelf) {
898 Matcher<int> m = Le(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000899 EXPECT_EQ("is <= 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000900}
901
902// Tests that Lt(v) matches anything < v.
903TEST(LtTest, ImplementsLessThan) {
904 Matcher<const string&> m1 = Lt("Hello");
905 EXPECT_TRUE(m1.Matches("Abc"));
906 EXPECT_FALSE(m1.Matches("Hello"));
907 EXPECT_FALSE(m1.Matches("Hello, world!"));
908}
909
910// Tests that Lt(v) describes itself properly.
911TEST(LtTest, CanDescribeSelf) {
912 Matcher<int> m = Lt(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000913 EXPECT_EQ("is < 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000914}
915
916// Tests that Ne(v) matches anything != v.
917TEST(NeTest, ImplementsNotEqual) {
918 Matcher<int> m1 = Ne(0);
919 EXPECT_TRUE(m1.Matches(1));
920 EXPECT_TRUE(m1.Matches(-1));
921 EXPECT_FALSE(m1.Matches(0));
922}
923
924// Tests that Ne(v) describes itself properly.
925TEST(NeTest, CanDescribeSelf) {
926 Matcher<int> m = Ne(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000927 EXPECT_EQ("isn't equal to 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000928}
929
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000930// Tests that IsNull() matches any NULL pointer of any type.
931TEST(IsNullTest, MatchesNullPointer) {
932 Matcher<int*> m1 = IsNull();
933 int* p1 = NULL;
934 int n = 0;
935 EXPECT_TRUE(m1.Matches(p1));
936 EXPECT_FALSE(m1.Matches(&n));
937
938 Matcher<const char*> m2 = IsNull();
939 const char* p2 = NULL;
940 EXPECT_TRUE(m2.Matches(p2));
941 EXPECT_FALSE(m2.Matches("hi"));
942
zhanyong.wan95b12332009-09-25 18:55:50 +0000943#if !GTEST_OS_SYMBIAN
944 // Nokia's Symbian compiler generates:
945 // gmock-matchers.h: ambiguous access to overloaded function
946 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(void *)'
947 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(const testing::
948 // MatcherInterface<void *> *)'
949 // gmock-matchers.h: (point of instantiation: 'testing::
950 // gmock_matchers_test::IsNullTest_MatchesNullPointer_Test::TestBody()')
951 // gmock-matchers.h: (instantiating: 'testing::PolymorphicMatc
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000952 Matcher<void*> m3 = IsNull();
953 void* p3 = NULL;
954 EXPECT_TRUE(m3.Matches(p3));
955 EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef)));
zhanyong.wan95b12332009-09-25 18:55:50 +0000956#endif
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000957}
958
vladlosev79b83502009-11-18 00:43:37 +0000959TEST(IsNullTest, LinkedPtr) {
960 const Matcher<linked_ptr<int> > m = IsNull();
961 const linked_ptr<int> null_p;
962 const linked_ptr<int> non_null_p(new int);
963
964 EXPECT_TRUE(m.Matches(null_p));
965 EXPECT_FALSE(m.Matches(non_null_p));
966}
967
968TEST(IsNullTest, ReferenceToConstLinkedPtr) {
969 const Matcher<const linked_ptr<double>&> m = IsNull();
970 const linked_ptr<double> null_p;
971 const linked_ptr<double> non_null_p(new double);
972
973 EXPECT_TRUE(m.Matches(null_p));
974 EXPECT_FALSE(m.Matches(non_null_p));
975}
976
vladloseve56daa72009-11-18 01:08:08 +0000977TEST(IsNullTest, ReferenceToConstScopedPtr) {
978 const Matcher<const scoped_ptr<double>&> m = IsNull();
979 const scoped_ptr<double> null_p;
980 const scoped_ptr<double> non_null_p(new double);
981
982 EXPECT_TRUE(m.Matches(null_p));
983 EXPECT_FALSE(m.Matches(non_null_p));
984}
985
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000986// Tests that IsNull() describes itself properly.
987TEST(IsNullTest, CanDescribeSelf) {
988 Matcher<int*> m = IsNull();
989 EXPECT_EQ("is NULL", Describe(m));
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000990 EXPECT_EQ("isn't NULL", DescribeNegation(m));
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000991}
992
shiqiane35fdd92008-12-10 05:08:54 +0000993// Tests that NotNull() matches any non-NULL pointer of any type.
994TEST(NotNullTest, MatchesNonNullPointer) {
995 Matcher<int*> m1 = NotNull();
996 int* p1 = NULL;
997 int n = 0;
998 EXPECT_FALSE(m1.Matches(p1));
999 EXPECT_TRUE(m1.Matches(&n));
1000
1001 Matcher<const char*> m2 = NotNull();
1002 const char* p2 = NULL;
1003 EXPECT_FALSE(m2.Matches(p2));
1004 EXPECT_TRUE(m2.Matches("hi"));
1005}
1006
vladlosev79b83502009-11-18 00:43:37 +00001007TEST(NotNullTest, LinkedPtr) {
1008 const Matcher<linked_ptr<int> > m = NotNull();
1009 const linked_ptr<int> null_p;
1010 const linked_ptr<int> non_null_p(new int);
1011
1012 EXPECT_FALSE(m.Matches(null_p));
1013 EXPECT_TRUE(m.Matches(non_null_p));
1014}
1015
1016TEST(NotNullTest, ReferenceToConstLinkedPtr) {
1017 const Matcher<const linked_ptr<double>&> m = NotNull();
1018 const linked_ptr<double> null_p;
1019 const linked_ptr<double> non_null_p(new double);
1020
1021 EXPECT_FALSE(m.Matches(null_p));
1022 EXPECT_TRUE(m.Matches(non_null_p));
1023}
1024
vladloseve56daa72009-11-18 01:08:08 +00001025TEST(NotNullTest, ReferenceToConstScopedPtr) {
1026 const Matcher<const scoped_ptr<double>&> m = NotNull();
1027 const scoped_ptr<double> null_p;
1028 const scoped_ptr<double> non_null_p(new double);
1029
1030 EXPECT_FALSE(m.Matches(null_p));
1031 EXPECT_TRUE(m.Matches(non_null_p));
1032}
1033
shiqiane35fdd92008-12-10 05:08:54 +00001034// Tests that NotNull() describes itself properly.
1035TEST(NotNullTest, CanDescribeSelf) {
1036 Matcher<int*> m = NotNull();
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001037 EXPECT_EQ("isn't NULL", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001038}
1039
1040// Tests that Ref(variable) matches an argument that references
1041// 'variable'.
1042TEST(RefTest, MatchesSameVariable) {
1043 int a = 0;
1044 int b = 0;
1045 Matcher<int&> m = Ref(a);
1046 EXPECT_TRUE(m.Matches(a));
1047 EXPECT_FALSE(m.Matches(b));
1048}
1049
1050// Tests that Ref(variable) describes itself properly.
1051TEST(RefTest, CanDescribeSelf) {
1052 int n = 5;
1053 Matcher<int&> m = Ref(n);
1054 stringstream ss;
1055 ss << "references the variable @" << &n << " 5";
1056 EXPECT_EQ(string(ss.str()), Describe(m));
1057}
1058
1059// Test that Ref(non_const_varialbe) can be used as a matcher for a
1060// const reference.
1061TEST(RefTest, CanBeUsedAsMatcherForConstReference) {
1062 int a = 0;
1063 int b = 0;
1064 Matcher<const int&> m = Ref(a);
1065 EXPECT_TRUE(m.Matches(a));
1066 EXPECT_FALSE(m.Matches(b));
1067}
1068
1069// Tests that Ref(variable) is covariant, i.e. Ref(derived) can be
1070// used wherever Ref(base) can be used (Ref(derived) is a sub-type
1071// of Ref(base), but not vice versa.
1072
shiqiane35fdd92008-12-10 05:08:54 +00001073TEST(RefTest, IsCovariant) {
1074 Base base, base2;
1075 Derived derived;
1076 Matcher<const Base&> m1 = Ref(base);
1077 EXPECT_TRUE(m1.Matches(base));
1078 EXPECT_FALSE(m1.Matches(base2));
1079 EXPECT_FALSE(m1.Matches(derived));
1080
1081 m1 = Ref(derived);
1082 EXPECT_TRUE(m1.Matches(derived));
1083 EXPECT_FALSE(m1.Matches(base));
1084 EXPECT_FALSE(m1.Matches(base2));
1085}
1086
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001087TEST(RefTest, ExplainsResult) {
1088 int n = 0;
1089 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), n),
1090 StartsWith("which is located @"));
1091
1092 int m = 0;
1093 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), m),
1094 StartsWith("which is located @"));
1095}
1096
shiqiane35fdd92008-12-10 05:08:54 +00001097// Tests string comparison matchers.
1098
1099TEST(StrEqTest, MatchesEqualString) {
1100 Matcher<const char*> m = StrEq(string("Hello"));
1101 EXPECT_TRUE(m.Matches("Hello"));
1102 EXPECT_FALSE(m.Matches("hello"));
1103 EXPECT_FALSE(m.Matches(NULL));
1104
1105 Matcher<const string&> m2 = StrEq("Hello");
1106 EXPECT_TRUE(m2.Matches("Hello"));
1107 EXPECT_FALSE(m2.Matches("Hi"));
1108}
1109
1110TEST(StrEqTest, CanDescribeSelf) {
vladlosevaa432202011-04-01 21:58:42 +00001111 Matcher<string> m = StrEq("Hi-\'\"?\\\a\b\f\n\r\t\v\xD3");
1112 EXPECT_EQ("is equal to \"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",
shiqiane35fdd92008-12-10 05:08:54 +00001113 Describe(m));
1114
1115 string str("01204500800");
1116 str[3] = '\0';
1117 Matcher<string> m2 = StrEq(str);
1118 EXPECT_EQ("is equal to \"012\\04500800\"", Describe(m2));
1119 str[0] = str[6] = str[7] = str[9] = str[10] = '\0';
1120 Matcher<string> m3 = StrEq(str);
1121 EXPECT_EQ("is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3));
1122}
1123
1124TEST(StrNeTest, MatchesUnequalString) {
1125 Matcher<const char*> m = StrNe("Hello");
1126 EXPECT_TRUE(m.Matches(""));
1127 EXPECT_TRUE(m.Matches(NULL));
1128 EXPECT_FALSE(m.Matches("Hello"));
1129
1130 Matcher<string> m2 = StrNe(string("Hello"));
1131 EXPECT_TRUE(m2.Matches("hello"));
1132 EXPECT_FALSE(m2.Matches("Hello"));
1133}
1134
1135TEST(StrNeTest, CanDescribeSelf) {
1136 Matcher<const char*> m = StrNe("Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001137 EXPECT_EQ("isn't equal to \"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001138}
1139
1140TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
1141 Matcher<const char*> m = StrCaseEq(string("Hello"));
1142 EXPECT_TRUE(m.Matches("Hello"));
1143 EXPECT_TRUE(m.Matches("hello"));
1144 EXPECT_FALSE(m.Matches("Hi"));
1145 EXPECT_FALSE(m.Matches(NULL));
1146
1147 Matcher<const string&> m2 = StrCaseEq("Hello");
1148 EXPECT_TRUE(m2.Matches("hello"));
1149 EXPECT_FALSE(m2.Matches("Hi"));
1150}
1151
1152TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1153 string str1("oabocdooeoo");
1154 string str2("OABOCDOOEOO");
1155 Matcher<const string&> m0 = StrCaseEq(str1);
1156 EXPECT_FALSE(m0.Matches(str2 + string(1, '\0')));
1157
1158 str1[3] = str2[3] = '\0';
1159 Matcher<const string&> m1 = StrCaseEq(str1);
1160 EXPECT_TRUE(m1.Matches(str2));
1161
1162 str1[0] = str1[6] = str1[7] = str1[10] = '\0';
1163 str2[0] = str2[6] = str2[7] = str2[10] = '\0';
1164 Matcher<const string&> m2 = StrCaseEq(str1);
1165 str1[9] = str2[9] = '\0';
1166 EXPECT_FALSE(m2.Matches(str2));
1167
1168 Matcher<const string&> m3 = StrCaseEq(str1);
1169 EXPECT_TRUE(m3.Matches(str2));
1170
1171 EXPECT_FALSE(m3.Matches(str2 + "x"));
1172 str2.append(1, '\0');
1173 EXPECT_FALSE(m3.Matches(str2));
1174 EXPECT_FALSE(m3.Matches(string(str2, 0, 9)));
1175}
1176
1177TEST(StrCaseEqTest, CanDescribeSelf) {
1178 Matcher<string> m = StrCaseEq("Hi");
1179 EXPECT_EQ("is equal to (ignoring case) \"Hi\"", Describe(m));
1180}
1181
1182TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1183 Matcher<const char*> m = StrCaseNe("Hello");
1184 EXPECT_TRUE(m.Matches("Hi"));
1185 EXPECT_TRUE(m.Matches(NULL));
1186 EXPECT_FALSE(m.Matches("Hello"));
1187 EXPECT_FALSE(m.Matches("hello"));
1188
1189 Matcher<string> m2 = StrCaseNe(string("Hello"));
1190 EXPECT_TRUE(m2.Matches(""));
1191 EXPECT_FALSE(m2.Matches("Hello"));
1192}
1193
1194TEST(StrCaseNeTest, CanDescribeSelf) {
1195 Matcher<const char*> m = StrCaseNe("Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001196 EXPECT_EQ("isn't equal to (ignoring case) \"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001197}
1198
1199// Tests that HasSubstr() works for matching string-typed values.
1200TEST(HasSubstrTest, WorksForStringClasses) {
1201 const Matcher<string> m1 = HasSubstr("foo");
1202 EXPECT_TRUE(m1.Matches(string("I love food.")));
1203 EXPECT_FALSE(m1.Matches(string("tofo")));
1204
1205 const Matcher<const std::string&> m2 = HasSubstr("foo");
1206 EXPECT_TRUE(m2.Matches(std::string("I love food.")));
1207 EXPECT_FALSE(m2.Matches(std::string("tofo")));
1208}
1209
1210// Tests that HasSubstr() works for matching C-string-typed values.
1211TEST(HasSubstrTest, WorksForCStrings) {
1212 const Matcher<char*> m1 = HasSubstr("foo");
1213 EXPECT_TRUE(m1.Matches(const_cast<char*>("I love food.")));
1214 EXPECT_FALSE(m1.Matches(const_cast<char*>("tofo")));
1215 EXPECT_FALSE(m1.Matches(NULL));
1216
1217 const Matcher<const char*> m2 = HasSubstr("foo");
1218 EXPECT_TRUE(m2.Matches("I love food."));
1219 EXPECT_FALSE(m2.Matches("tofo"));
1220 EXPECT_FALSE(m2.Matches(NULL));
1221}
1222
1223// Tests that HasSubstr(s) describes itself properly.
1224TEST(HasSubstrTest, CanDescribeSelf) {
1225 Matcher<string> m = HasSubstr("foo\n\"");
1226 EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m));
1227}
1228
zhanyong.wanb5937da2009-07-16 20:26:41 +00001229TEST(KeyTest, CanDescribeSelf) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001230 Matcher<const pair<std::string, int>&> m = Key("foo");
zhanyong.wanb5937da2009-07-16 20:26:41 +00001231 EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001232 EXPECT_EQ("doesn't have a key that is equal to \"foo\"", DescribeNegation(m));
1233}
1234
1235TEST(KeyTest, ExplainsResult) {
1236 Matcher<pair<int, bool> > m = Key(GreaterThan(10));
1237 EXPECT_EQ("whose first field is a value which is 5 less than 10",
1238 Explain(m, make_pair(5, true)));
1239 EXPECT_EQ("whose first field is a value which is 5 more than 10",
1240 Explain(m, make_pair(15, true)));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001241}
1242
1243TEST(KeyTest, MatchesCorrectly) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001244 pair<int, std::string> p(25, "foo");
zhanyong.wanb5937da2009-07-16 20:26:41 +00001245 EXPECT_THAT(p, Key(25));
1246 EXPECT_THAT(p, Not(Key(42)));
1247 EXPECT_THAT(p, Key(Ge(20)));
1248 EXPECT_THAT(p, Not(Key(Lt(25))));
1249}
1250
1251TEST(KeyTest, SafelyCastsInnerMatcher) {
1252 Matcher<int> is_positive = Gt(0);
1253 Matcher<int> is_negative = Lt(0);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001254 pair<char, bool> p('a', true);
zhanyong.wanb5937da2009-07-16 20:26:41 +00001255 EXPECT_THAT(p, Key(is_positive));
1256 EXPECT_THAT(p, Not(Key(is_negative)));
1257}
1258
1259TEST(KeyTest, InsideContainsUsingMap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001260 map<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001261 container.insert(make_pair(1, 'a'));
1262 container.insert(make_pair(2, 'b'));
1263 container.insert(make_pair(4, 'c'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001264 EXPECT_THAT(container, Contains(Key(1)));
1265 EXPECT_THAT(container, Not(Contains(Key(3))));
1266}
1267
1268TEST(KeyTest, InsideContainsUsingMultimap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001269 multimap<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001270 container.insert(make_pair(1, 'a'));
1271 container.insert(make_pair(2, 'b'));
1272 container.insert(make_pair(4, 'c'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001273
1274 EXPECT_THAT(container, Not(Contains(Key(25))));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001275 container.insert(make_pair(25, 'd'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001276 EXPECT_THAT(container, Contains(Key(25)));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001277 container.insert(make_pair(25, 'e'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001278 EXPECT_THAT(container, Contains(Key(25)));
1279
1280 EXPECT_THAT(container, Contains(Key(1)));
1281 EXPECT_THAT(container, Not(Contains(Key(3))));
1282}
1283
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001284TEST(PairTest, Typing) {
1285 // Test verifies the following type conversions can be compiled.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001286 Matcher<const pair<const char*, int>&> m1 = Pair("foo", 42);
1287 Matcher<const pair<const char*, int> > m2 = Pair("foo", 42);
1288 Matcher<pair<const char*, int> > m3 = Pair("foo", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001289
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001290 Matcher<pair<int, const std::string> > m4 = Pair(25, "42");
1291 Matcher<pair<const std::string, int> > m5 = Pair("25", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001292}
1293
1294TEST(PairTest, CanDescribeSelf) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001295 Matcher<const pair<std::string, int>&> m1 = Pair("foo", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001296 EXPECT_EQ("has a first field that is equal to \"foo\""
1297 ", and has a second field that is equal to 42",
1298 Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001299 EXPECT_EQ("has a first field that isn't equal to \"foo\""
1300 ", or has a second field that isn't equal to 42",
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001301 DescribeNegation(m1));
1302 // Double and triple negation (1 or 2 times not and description of negation).
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001303 Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42));
1304 EXPECT_EQ("has a first field that isn't equal to 13"
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001305 ", and has a second field that is equal to 42",
1306 DescribeNegation(m2));
1307}
1308
1309TEST(PairTest, CanExplainMatchResultTo) {
zhanyong.wan82113312010-01-08 21:55:40 +00001310 // If neither field matches, Pair() should explain about the first
1311 // field.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001312 const Matcher<pair<int, int> > m = Pair(GreaterThan(0), GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001313 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001314 Explain(m, make_pair(-1, -2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001315
zhanyong.wan82113312010-01-08 21:55:40 +00001316 // If the first field matches but the second doesn't, Pair() should
1317 // explain about the second field.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001318 EXPECT_EQ("whose second field does not match, which is 2 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001319 Explain(m, make_pair(1, -2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001320
zhanyong.wan82113312010-01-08 21:55:40 +00001321 // If the first field doesn't match but the second does, Pair()
1322 // should explain about the first field.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001323 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001324 Explain(m, make_pair(-1, 2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001325
zhanyong.wan82113312010-01-08 21:55:40 +00001326 // If both fields match, Pair() should explain about them both.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001327 EXPECT_EQ("whose both fields match, where the first field is a value "
1328 "which is 1 more than 0, and the second field is a value "
1329 "which is 2 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001330 Explain(m, make_pair(1, 2)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001331
1332 // If only the first match has an explanation, only this explanation should
1333 // be printed.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001334 const Matcher<pair<int, int> > explain_first = Pair(GreaterThan(0), 0);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001335 EXPECT_EQ("whose both fields match, where the first field is a value "
1336 "which is 1 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001337 Explain(explain_first, make_pair(1, 0)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001338
1339 // If only the second match has an explanation, only this explanation should
1340 // be printed.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001341 const Matcher<pair<int, int> > explain_second = Pair(0, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001342 EXPECT_EQ("whose both fields match, where the second field is a value "
1343 "which is 1 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001344 Explain(explain_second, make_pair(0, 1)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001345}
1346
1347TEST(PairTest, MatchesCorrectly) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001348 pair<int, std::string> p(25, "foo");
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001349
1350 // Both fields match.
1351 EXPECT_THAT(p, Pair(25, "foo"));
1352 EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o")));
1353
1354 // 'first' doesnt' match, but 'second' matches.
1355 EXPECT_THAT(p, Not(Pair(42, "foo")));
1356 EXPECT_THAT(p, Not(Pair(Lt(25), "foo")));
1357
1358 // 'first' matches, but 'second' doesn't match.
1359 EXPECT_THAT(p, Not(Pair(25, "bar")));
1360 EXPECT_THAT(p, Not(Pair(25, Not("foo"))));
1361
1362 // Neither field matches.
1363 EXPECT_THAT(p, Not(Pair(13, "bar")));
1364 EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a"))));
1365}
1366
1367TEST(PairTest, SafelyCastsInnerMatchers) {
1368 Matcher<int> is_positive = Gt(0);
1369 Matcher<int> is_negative = Lt(0);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001370 pair<char, bool> p('a', true);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001371 EXPECT_THAT(p, Pair(is_positive, _));
1372 EXPECT_THAT(p, Not(Pair(is_negative, _)));
1373 EXPECT_THAT(p, Pair(_, is_positive));
1374 EXPECT_THAT(p, Not(Pair(_, is_negative)));
1375}
1376
1377TEST(PairTest, InsideContainsUsingMap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001378 map<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001379 container.insert(make_pair(1, 'a'));
1380 container.insert(make_pair(2, 'b'));
1381 container.insert(make_pair(4, 'c'));
zhanyong.wan95b12332009-09-25 18:55:50 +00001382 EXPECT_THAT(container, Contains(Pair(1, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001383 EXPECT_THAT(container, Contains(Pair(1, _)));
zhanyong.wan95b12332009-09-25 18:55:50 +00001384 EXPECT_THAT(container, Contains(Pair(_, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001385 EXPECT_THAT(container, Not(Contains(Pair(3, _))));
1386}
1387
shiqiane35fdd92008-12-10 05:08:54 +00001388// Tests StartsWith(s).
1389
1390TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
1391 const Matcher<const char*> m1 = StartsWith(string(""));
1392 EXPECT_TRUE(m1.Matches("Hi"));
1393 EXPECT_TRUE(m1.Matches(""));
1394 EXPECT_FALSE(m1.Matches(NULL));
1395
1396 const Matcher<const string&> m2 = StartsWith("Hi");
1397 EXPECT_TRUE(m2.Matches("Hi"));
1398 EXPECT_TRUE(m2.Matches("Hi Hi!"));
1399 EXPECT_TRUE(m2.Matches("High"));
1400 EXPECT_FALSE(m2.Matches("H"));
1401 EXPECT_FALSE(m2.Matches(" Hi"));
1402}
1403
1404TEST(StartsWithTest, CanDescribeSelf) {
1405 Matcher<const std::string> m = StartsWith("Hi");
1406 EXPECT_EQ("starts with \"Hi\"", Describe(m));
1407}
1408
1409// Tests EndsWith(s).
1410
1411TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
1412 const Matcher<const char*> m1 = EndsWith("");
1413 EXPECT_TRUE(m1.Matches("Hi"));
1414 EXPECT_TRUE(m1.Matches(""));
1415 EXPECT_FALSE(m1.Matches(NULL));
1416
1417 const Matcher<const string&> m2 = EndsWith(string("Hi"));
1418 EXPECT_TRUE(m2.Matches("Hi"));
1419 EXPECT_TRUE(m2.Matches("Wow Hi Hi"));
1420 EXPECT_TRUE(m2.Matches("Super Hi"));
1421 EXPECT_FALSE(m2.Matches("i"));
1422 EXPECT_FALSE(m2.Matches("Hi "));
1423}
1424
1425TEST(EndsWithTest, CanDescribeSelf) {
1426 Matcher<const std::string> m = EndsWith("Hi");
1427 EXPECT_EQ("ends with \"Hi\"", Describe(m));
1428}
1429
shiqiane35fdd92008-12-10 05:08:54 +00001430// Tests MatchesRegex().
1431
1432TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
1433 const Matcher<const char*> m1 = MatchesRegex("a.*z");
1434 EXPECT_TRUE(m1.Matches("az"));
1435 EXPECT_TRUE(m1.Matches("abcz"));
1436 EXPECT_FALSE(m1.Matches(NULL));
1437
1438 const Matcher<const string&> m2 = MatchesRegex(new RE("a.*z"));
1439 EXPECT_TRUE(m2.Matches("azbz"));
1440 EXPECT_FALSE(m2.Matches("az1"));
1441 EXPECT_FALSE(m2.Matches("1az"));
1442}
1443
1444TEST(MatchesRegexTest, CanDescribeSelf) {
1445 Matcher<const std::string> m1 = MatchesRegex(string("Hi.*"));
1446 EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1));
1447
zhanyong.wand14aaed2010-01-14 05:36:32 +00001448 Matcher<const char*> m2 = MatchesRegex(new RE("a.*"));
1449 EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +00001450}
1451
1452// Tests ContainsRegex().
1453
1454TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
1455 const Matcher<const char*> m1 = ContainsRegex(string("a.*z"));
1456 EXPECT_TRUE(m1.Matches("az"));
1457 EXPECT_TRUE(m1.Matches("0abcz1"));
1458 EXPECT_FALSE(m1.Matches(NULL));
1459
1460 const Matcher<const string&> m2 = ContainsRegex(new RE("a.*z"));
1461 EXPECT_TRUE(m2.Matches("azbz"));
1462 EXPECT_TRUE(m2.Matches("az1"));
1463 EXPECT_FALSE(m2.Matches("1a"));
1464}
1465
1466TEST(ContainsRegexTest, CanDescribeSelf) {
1467 Matcher<const std::string> m1 = ContainsRegex("Hi.*");
1468 EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1));
1469
zhanyong.wand14aaed2010-01-14 05:36:32 +00001470 Matcher<const char*> m2 = ContainsRegex(new RE("a.*"));
1471 EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +00001472}
shiqiane35fdd92008-12-10 05:08:54 +00001473
1474// Tests for wide strings.
1475#if GTEST_HAS_STD_WSTRING
1476TEST(StdWideStrEqTest, MatchesEqual) {
1477 Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello"));
1478 EXPECT_TRUE(m.Matches(L"Hello"));
1479 EXPECT_FALSE(m.Matches(L"hello"));
1480 EXPECT_FALSE(m.Matches(NULL));
1481
1482 Matcher<const ::std::wstring&> m2 = StrEq(L"Hello");
1483 EXPECT_TRUE(m2.Matches(L"Hello"));
1484 EXPECT_FALSE(m2.Matches(L"Hi"));
1485
1486 Matcher<const ::std::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1487 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1488 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1489
1490 ::std::wstring str(L"01204500800");
1491 str[3] = L'\0';
1492 Matcher<const ::std::wstring&> m4 = StrEq(str);
1493 EXPECT_TRUE(m4.Matches(str));
1494 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1495 Matcher<const ::std::wstring&> m5 = StrEq(str);
1496 EXPECT_TRUE(m5.Matches(str));
1497}
1498
1499TEST(StdWideStrEqTest, CanDescribeSelf) {
vladlosevaa432202011-04-01 21:58:42 +00001500 Matcher< ::std::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v");
1501 EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
shiqiane35fdd92008-12-10 05:08:54 +00001502 Describe(m));
1503
1504 Matcher< ::std::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1505 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1506 Describe(m2));
1507
1508 ::std::wstring str(L"01204500800");
1509 str[3] = L'\0';
1510 Matcher<const ::std::wstring&> m4 = StrEq(str);
1511 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1512 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1513 Matcher<const ::std::wstring&> m5 = StrEq(str);
1514 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1515}
1516
1517TEST(StdWideStrNeTest, MatchesUnequalString) {
1518 Matcher<const wchar_t*> m = StrNe(L"Hello");
1519 EXPECT_TRUE(m.Matches(L""));
1520 EXPECT_TRUE(m.Matches(NULL));
1521 EXPECT_FALSE(m.Matches(L"Hello"));
1522
1523 Matcher< ::std::wstring> m2 = StrNe(::std::wstring(L"Hello"));
1524 EXPECT_TRUE(m2.Matches(L"hello"));
1525 EXPECT_FALSE(m2.Matches(L"Hello"));
1526}
1527
1528TEST(StdWideStrNeTest, CanDescribeSelf) {
1529 Matcher<const wchar_t*> m = StrNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001530 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001531}
1532
1533TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1534 Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello"));
1535 EXPECT_TRUE(m.Matches(L"Hello"));
1536 EXPECT_TRUE(m.Matches(L"hello"));
1537 EXPECT_FALSE(m.Matches(L"Hi"));
1538 EXPECT_FALSE(m.Matches(NULL));
1539
1540 Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello");
1541 EXPECT_TRUE(m2.Matches(L"hello"));
1542 EXPECT_FALSE(m2.Matches(L"Hi"));
1543}
1544
1545TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1546 ::std::wstring str1(L"oabocdooeoo");
1547 ::std::wstring str2(L"OABOCDOOEOO");
1548 Matcher<const ::std::wstring&> m0 = StrCaseEq(str1);
1549 EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L'\0')));
1550
1551 str1[3] = str2[3] = L'\0';
1552 Matcher<const ::std::wstring&> m1 = StrCaseEq(str1);
1553 EXPECT_TRUE(m1.Matches(str2));
1554
1555 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1556 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1557 Matcher<const ::std::wstring&> m2 = StrCaseEq(str1);
1558 str1[9] = str2[9] = L'\0';
1559 EXPECT_FALSE(m2.Matches(str2));
1560
1561 Matcher<const ::std::wstring&> m3 = StrCaseEq(str1);
1562 EXPECT_TRUE(m3.Matches(str2));
1563
1564 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1565 str2.append(1, L'\0');
1566 EXPECT_FALSE(m3.Matches(str2));
1567 EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9)));
1568}
1569
1570TEST(StdWideStrCaseEqTest, CanDescribeSelf) {
1571 Matcher< ::std::wstring> m = StrCaseEq(L"Hi");
1572 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1573}
1574
1575TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1576 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1577 EXPECT_TRUE(m.Matches(L"Hi"));
1578 EXPECT_TRUE(m.Matches(NULL));
1579 EXPECT_FALSE(m.Matches(L"Hello"));
1580 EXPECT_FALSE(m.Matches(L"hello"));
1581
1582 Matcher< ::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello"));
1583 EXPECT_TRUE(m2.Matches(L""));
1584 EXPECT_FALSE(m2.Matches(L"Hello"));
1585}
1586
1587TEST(StdWideStrCaseNeTest, CanDescribeSelf) {
1588 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001589 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001590}
1591
1592// Tests that HasSubstr() works for matching wstring-typed values.
1593TEST(StdWideHasSubstrTest, WorksForStringClasses) {
1594 const Matcher< ::std::wstring> m1 = HasSubstr(L"foo");
1595 EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food.")));
1596 EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo")));
1597
1598 const Matcher<const ::std::wstring&> m2 = HasSubstr(L"foo");
1599 EXPECT_TRUE(m2.Matches(::std::wstring(L"I love food.")));
1600 EXPECT_FALSE(m2.Matches(::std::wstring(L"tofo")));
1601}
1602
1603// Tests that HasSubstr() works for matching C-wide-string-typed values.
1604TEST(StdWideHasSubstrTest, WorksForCStrings) {
1605 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1606 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1607 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1608 EXPECT_FALSE(m1.Matches(NULL));
1609
1610 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1611 EXPECT_TRUE(m2.Matches(L"I love food."));
1612 EXPECT_FALSE(m2.Matches(L"tofo"));
1613 EXPECT_FALSE(m2.Matches(NULL));
1614}
1615
1616// Tests that HasSubstr(s) describes itself properly.
1617TEST(StdWideHasSubstrTest, CanDescribeSelf) {
1618 Matcher< ::std::wstring> m = HasSubstr(L"foo\n\"");
1619 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1620}
1621
1622// Tests StartsWith(s).
1623
1624TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) {
1625 const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L""));
1626 EXPECT_TRUE(m1.Matches(L"Hi"));
1627 EXPECT_TRUE(m1.Matches(L""));
1628 EXPECT_FALSE(m1.Matches(NULL));
1629
1630 const Matcher<const ::std::wstring&> m2 = StartsWith(L"Hi");
1631 EXPECT_TRUE(m2.Matches(L"Hi"));
1632 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1633 EXPECT_TRUE(m2.Matches(L"High"));
1634 EXPECT_FALSE(m2.Matches(L"H"));
1635 EXPECT_FALSE(m2.Matches(L" Hi"));
1636}
1637
1638TEST(StdWideStartsWithTest, CanDescribeSelf) {
1639 Matcher<const ::std::wstring> m = StartsWith(L"Hi");
1640 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1641}
1642
1643// Tests EndsWith(s).
1644
1645TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) {
1646 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1647 EXPECT_TRUE(m1.Matches(L"Hi"));
1648 EXPECT_TRUE(m1.Matches(L""));
1649 EXPECT_FALSE(m1.Matches(NULL));
1650
1651 const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L"Hi"));
1652 EXPECT_TRUE(m2.Matches(L"Hi"));
1653 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1654 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1655 EXPECT_FALSE(m2.Matches(L"i"));
1656 EXPECT_FALSE(m2.Matches(L"Hi "));
1657}
1658
1659TEST(StdWideEndsWithTest, CanDescribeSelf) {
1660 Matcher<const ::std::wstring> m = EndsWith(L"Hi");
1661 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1662}
1663
1664#endif // GTEST_HAS_STD_WSTRING
1665
1666#if GTEST_HAS_GLOBAL_WSTRING
1667TEST(GlobalWideStrEqTest, MatchesEqual) {
1668 Matcher<const wchar_t*> m = StrEq(::wstring(L"Hello"));
1669 EXPECT_TRUE(m.Matches(L"Hello"));
1670 EXPECT_FALSE(m.Matches(L"hello"));
1671 EXPECT_FALSE(m.Matches(NULL));
1672
1673 Matcher<const ::wstring&> m2 = StrEq(L"Hello");
1674 EXPECT_TRUE(m2.Matches(L"Hello"));
1675 EXPECT_FALSE(m2.Matches(L"Hi"));
1676
1677 Matcher<const ::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1678 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1679 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1680
1681 ::wstring str(L"01204500800");
1682 str[3] = L'\0';
1683 Matcher<const ::wstring&> m4 = StrEq(str);
1684 EXPECT_TRUE(m4.Matches(str));
1685 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1686 Matcher<const ::wstring&> m5 = StrEq(str);
1687 EXPECT_TRUE(m5.Matches(str));
1688}
1689
1690TEST(GlobalWideStrEqTest, CanDescribeSelf) {
vladlosevaa432202011-04-01 21:58:42 +00001691 Matcher< ::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v");
1692 EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
shiqiane35fdd92008-12-10 05:08:54 +00001693 Describe(m));
1694
1695 Matcher< ::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1696 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1697 Describe(m2));
1698
1699 ::wstring str(L"01204500800");
1700 str[3] = L'\0';
1701 Matcher<const ::wstring&> m4 = StrEq(str);
1702 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1703 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1704 Matcher<const ::wstring&> m5 = StrEq(str);
1705 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1706}
1707
1708TEST(GlobalWideStrNeTest, MatchesUnequalString) {
1709 Matcher<const wchar_t*> m = StrNe(L"Hello");
1710 EXPECT_TRUE(m.Matches(L""));
1711 EXPECT_TRUE(m.Matches(NULL));
1712 EXPECT_FALSE(m.Matches(L"Hello"));
1713
1714 Matcher< ::wstring> m2 = StrNe(::wstring(L"Hello"));
1715 EXPECT_TRUE(m2.Matches(L"hello"));
1716 EXPECT_FALSE(m2.Matches(L"Hello"));
1717}
1718
1719TEST(GlobalWideStrNeTest, CanDescribeSelf) {
1720 Matcher<const wchar_t*> m = StrNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001721 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001722}
1723
1724TEST(GlobalWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1725 Matcher<const wchar_t*> m = StrCaseEq(::wstring(L"Hello"));
1726 EXPECT_TRUE(m.Matches(L"Hello"));
1727 EXPECT_TRUE(m.Matches(L"hello"));
1728 EXPECT_FALSE(m.Matches(L"Hi"));
1729 EXPECT_FALSE(m.Matches(NULL));
1730
1731 Matcher<const ::wstring&> m2 = StrCaseEq(L"Hello");
1732 EXPECT_TRUE(m2.Matches(L"hello"));
1733 EXPECT_FALSE(m2.Matches(L"Hi"));
1734}
1735
1736TEST(GlobalWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1737 ::wstring str1(L"oabocdooeoo");
1738 ::wstring str2(L"OABOCDOOEOO");
1739 Matcher<const ::wstring&> m0 = StrCaseEq(str1);
1740 EXPECT_FALSE(m0.Matches(str2 + ::wstring(1, L'\0')));
1741
1742 str1[3] = str2[3] = L'\0';
1743 Matcher<const ::wstring&> m1 = StrCaseEq(str1);
1744 EXPECT_TRUE(m1.Matches(str2));
1745
1746 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1747 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1748 Matcher<const ::wstring&> m2 = StrCaseEq(str1);
1749 str1[9] = str2[9] = L'\0';
1750 EXPECT_FALSE(m2.Matches(str2));
1751
1752 Matcher<const ::wstring&> m3 = StrCaseEq(str1);
1753 EXPECT_TRUE(m3.Matches(str2));
1754
1755 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1756 str2.append(1, L'\0');
1757 EXPECT_FALSE(m3.Matches(str2));
1758 EXPECT_FALSE(m3.Matches(::wstring(str2, 0, 9)));
1759}
1760
1761TEST(GlobalWideStrCaseEqTest, CanDescribeSelf) {
1762 Matcher< ::wstring> m = StrCaseEq(L"Hi");
1763 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1764}
1765
1766TEST(GlobalWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1767 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1768 EXPECT_TRUE(m.Matches(L"Hi"));
1769 EXPECT_TRUE(m.Matches(NULL));
1770 EXPECT_FALSE(m.Matches(L"Hello"));
1771 EXPECT_FALSE(m.Matches(L"hello"));
1772
1773 Matcher< ::wstring> m2 = StrCaseNe(::wstring(L"Hello"));
1774 EXPECT_TRUE(m2.Matches(L""));
1775 EXPECT_FALSE(m2.Matches(L"Hello"));
1776}
1777
1778TEST(GlobalWideStrCaseNeTest, CanDescribeSelf) {
1779 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001780 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001781}
1782
1783// Tests that HasSubstr() works for matching wstring-typed values.
1784TEST(GlobalWideHasSubstrTest, WorksForStringClasses) {
1785 const Matcher< ::wstring> m1 = HasSubstr(L"foo");
1786 EXPECT_TRUE(m1.Matches(::wstring(L"I love food.")));
1787 EXPECT_FALSE(m1.Matches(::wstring(L"tofo")));
1788
1789 const Matcher<const ::wstring&> m2 = HasSubstr(L"foo");
1790 EXPECT_TRUE(m2.Matches(::wstring(L"I love food.")));
1791 EXPECT_FALSE(m2.Matches(::wstring(L"tofo")));
1792}
1793
1794// Tests that HasSubstr() works for matching C-wide-string-typed values.
1795TEST(GlobalWideHasSubstrTest, WorksForCStrings) {
1796 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1797 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1798 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1799 EXPECT_FALSE(m1.Matches(NULL));
1800
1801 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1802 EXPECT_TRUE(m2.Matches(L"I love food."));
1803 EXPECT_FALSE(m2.Matches(L"tofo"));
1804 EXPECT_FALSE(m2.Matches(NULL));
1805}
1806
1807// Tests that HasSubstr(s) describes itself properly.
1808TEST(GlobalWideHasSubstrTest, CanDescribeSelf) {
1809 Matcher< ::wstring> m = HasSubstr(L"foo\n\"");
1810 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1811}
1812
1813// Tests StartsWith(s).
1814
1815TEST(GlobalWideStartsWithTest, MatchesStringWithGivenPrefix) {
1816 const Matcher<const wchar_t*> m1 = StartsWith(::wstring(L""));
1817 EXPECT_TRUE(m1.Matches(L"Hi"));
1818 EXPECT_TRUE(m1.Matches(L""));
1819 EXPECT_FALSE(m1.Matches(NULL));
1820
1821 const Matcher<const ::wstring&> m2 = StartsWith(L"Hi");
1822 EXPECT_TRUE(m2.Matches(L"Hi"));
1823 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1824 EXPECT_TRUE(m2.Matches(L"High"));
1825 EXPECT_FALSE(m2.Matches(L"H"));
1826 EXPECT_FALSE(m2.Matches(L" Hi"));
1827}
1828
1829TEST(GlobalWideStartsWithTest, CanDescribeSelf) {
1830 Matcher<const ::wstring> m = StartsWith(L"Hi");
1831 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1832}
1833
1834// Tests EndsWith(s).
1835
1836TEST(GlobalWideEndsWithTest, MatchesStringWithGivenSuffix) {
1837 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1838 EXPECT_TRUE(m1.Matches(L"Hi"));
1839 EXPECT_TRUE(m1.Matches(L""));
1840 EXPECT_FALSE(m1.Matches(NULL));
1841
1842 const Matcher<const ::wstring&> m2 = EndsWith(::wstring(L"Hi"));
1843 EXPECT_TRUE(m2.Matches(L"Hi"));
1844 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1845 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1846 EXPECT_FALSE(m2.Matches(L"i"));
1847 EXPECT_FALSE(m2.Matches(L"Hi "));
1848}
1849
1850TEST(GlobalWideEndsWithTest, CanDescribeSelf) {
1851 Matcher<const ::wstring> m = EndsWith(L"Hi");
1852 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1853}
1854
1855#endif // GTEST_HAS_GLOBAL_WSTRING
1856
1857
1858typedef ::std::tr1::tuple<long, int> Tuple2; // NOLINT
1859
1860// Tests that Eq() matches a 2-tuple where the first field == the
1861// second field.
1862TEST(Eq2Test, MatchesEqualArguments) {
1863 Matcher<const Tuple2&> m = Eq();
1864 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1865 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1866}
1867
1868// Tests that Eq() describes itself properly.
1869TEST(Eq2Test, CanDescribeSelf) {
1870 Matcher<const Tuple2&> m = Eq();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001871 EXPECT_EQ("are an equal pair", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001872}
1873
1874// Tests that Ge() matches a 2-tuple where the first field >= the
1875// second field.
1876TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) {
1877 Matcher<const Tuple2&> m = Ge();
1878 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1879 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1880 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1881}
1882
1883// Tests that Ge() describes itself properly.
1884TEST(Ge2Test, CanDescribeSelf) {
1885 Matcher<const Tuple2&> m = Ge();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001886 EXPECT_EQ("are a pair where the first >= the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001887}
1888
1889// Tests that Gt() matches a 2-tuple where the first field > the
1890// second field.
1891TEST(Gt2Test, MatchesGreaterThanArguments) {
1892 Matcher<const Tuple2&> m = Gt();
1893 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1894 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1895 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1896}
1897
1898// Tests that Gt() describes itself properly.
1899TEST(Gt2Test, CanDescribeSelf) {
1900 Matcher<const Tuple2&> m = Gt();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001901 EXPECT_EQ("are a pair where the first > the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001902}
1903
1904// Tests that Le() matches a 2-tuple where the first field <= the
1905// second field.
1906TEST(Le2Test, MatchesLessThanOrEqualArguments) {
1907 Matcher<const Tuple2&> m = Le();
1908 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1909 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1910 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1911}
1912
1913// Tests that Le() describes itself properly.
1914TEST(Le2Test, CanDescribeSelf) {
1915 Matcher<const Tuple2&> m = Le();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001916 EXPECT_EQ("are a pair where the first <= the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001917}
1918
1919// Tests that Lt() matches a 2-tuple where the first field < the
1920// second field.
1921TEST(Lt2Test, MatchesLessThanArguments) {
1922 Matcher<const Tuple2&> m = Lt();
1923 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1924 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1925 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1926}
1927
1928// Tests that Lt() describes itself properly.
1929TEST(Lt2Test, CanDescribeSelf) {
1930 Matcher<const Tuple2&> m = Lt();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001931 EXPECT_EQ("are a pair where the first < the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001932}
1933
1934// Tests that Ne() matches a 2-tuple where the first field != the
1935// second field.
1936TEST(Ne2Test, MatchesUnequalArguments) {
1937 Matcher<const Tuple2&> m = Ne();
1938 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1939 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1940 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1941}
1942
1943// Tests that Ne() describes itself properly.
1944TEST(Ne2Test, CanDescribeSelf) {
1945 Matcher<const Tuple2&> m = Ne();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001946 EXPECT_EQ("are an unequal pair", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001947}
1948
1949// Tests that Not(m) matches any value that doesn't match m.
1950TEST(NotTest, NegatesMatcher) {
1951 Matcher<int> m;
1952 m = Not(Eq(2));
1953 EXPECT_TRUE(m.Matches(3));
1954 EXPECT_FALSE(m.Matches(2));
1955}
1956
1957// Tests that Not(m) describes itself properly.
1958TEST(NotTest, CanDescribeSelf) {
1959 Matcher<int> m = Not(Eq(5));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001960 EXPECT_EQ("isn't equal to 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001961}
1962
zhanyong.wan18490652009-05-11 18:54:08 +00001963// Tests that monomorphic matchers are safely cast by the Not matcher.
1964TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {
1965 // greater_than_5 is a monomorphic matcher.
1966 Matcher<int> greater_than_5 = Gt(5);
1967
1968 Matcher<const int&> m = Not(greater_than_5);
1969 Matcher<int&> m2 = Not(greater_than_5);
1970 Matcher<int&> m3 = Not(m);
1971}
1972
zhanyong.wan02c15052010-06-09 19:21:30 +00001973// Helper to allow easy testing of AllOf matchers with num parameters.
1974void AllOfMatches(int num, const Matcher<int>& m) {
1975 SCOPED_TRACE(Describe(m));
1976 EXPECT_TRUE(m.Matches(0));
1977 for (int i = 1; i <= num; ++i) {
1978 EXPECT_FALSE(m.Matches(i));
1979 }
1980 EXPECT_TRUE(m.Matches(num + 1));
1981}
1982
shiqiane35fdd92008-12-10 05:08:54 +00001983// Tests that AllOf(m1, ..., mn) matches any value that matches all of
1984// the given matchers.
1985TEST(AllOfTest, MatchesWhenAllMatch) {
1986 Matcher<int> m;
1987 m = AllOf(Le(2), Ge(1));
1988 EXPECT_TRUE(m.Matches(1));
1989 EXPECT_TRUE(m.Matches(2));
1990 EXPECT_FALSE(m.Matches(0));
1991 EXPECT_FALSE(m.Matches(3));
1992
1993 m = AllOf(Gt(0), Ne(1), Ne(2));
1994 EXPECT_TRUE(m.Matches(3));
1995 EXPECT_FALSE(m.Matches(2));
1996 EXPECT_FALSE(m.Matches(1));
1997 EXPECT_FALSE(m.Matches(0));
1998
1999 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
2000 EXPECT_TRUE(m.Matches(4));
2001 EXPECT_FALSE(m.Matches(3));
2002 EXPECT_FALSE(m.Matches(2));
2003 EXPECT_FALSE(m.Matches(1));
2004 EXPECT_FALSE(m.Matches(0));
2005
2006 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
2007 EXPECT_TRUE(m.Matches(0));
2008 EXPECT_TRUE(m.Matches(1));
2009 EXPECT_FALSE(m.Matches(3));
zhanyong.wan02c15052010-06-09 19:21:30 +00002010
2011 // The following tests for varying number of sub-matchers. Due to the way
2012 // the sub-matchers are handled it is enough to test every sub-matcher once
2013 // with sub-matchers using the same matcher type. Varying matcher types are
2014 // checked for above.
2015 AllOfMatches(2, AllOf(Ne(1), Ne(2)));
2016 AllOfMatches(3, AllOf(Ne(1), Ne(2), Ne(3)));
2017 AllOfMatches(4, AllOf(Ne(1), Ne(2), Ne(3), Ne(4)));
2018 AllOfMatches(5, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5)));
2019 AllOfMatches(6, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6)));
2020 AllOfMatches(7, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7)));
2021 AllOfMatches(8, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
2022 Ne(8)));
2023 AllOfMatches(9, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
2024 Ne(8), Ne(9)));
2025 AllOfMatches(10, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8),
2026 Ne(9), Ne(10)));
shiqiane35fdd92008-12-10 05:08:54 +00002027}
2028
zhanyong.wan616180e2013-06-18 18:49:51 +00002029#if GTEST_LANG_CXX11
2030// Tests the variadic version of the AllOfMatcher.
2031TEST(AllOfTest, VariadicMatchesWhenAllMatch) {
2032 // Make sure AllOf is defined in the right namespace and does not depend on
2033 // ADL.
2034 ::testing::AllOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
2035 Matcher<int> m = AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8),
2036 Ne(9), Ne(10), Ne(11));
2037 EXPECT_THAT(Describe(m), EndsWith("and (isn't equal to 11))))))))))"));
2038 AllOfMatches(11, m);
2039 AllOfMatches(50, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8),
2040 Ne(9), Ne(10), Ne(11), Ne(12), Ne(13), Ne(14), Ne(15),
2041 Ne(16), Ne(17), Ne(18), Ne(19), Ne(20), Ne(21), Ne(22),
2042 Ne(23), Ne(24), Ne(25), Ne(26), Ne(27), Ne(28), Ne(29),
2043 Ne(30), Ne(31), Ne(32), Ne(33), Ne(34), Ne(35), Ne(36),
2044 Ne(37), Ne(38), Ne(39), Ne(40), Ne(41), Ne(42), Ne(43),
2045 Ne(44), Ne(45), Ne(46), Ne(47), Ne(48), Ne(49),
2046 Ne(50)));
2047}
2048
2049#endif // GTEST_LANG_CXX11
2050
shiqiane35fdd92008-12-10 05:08:54 +00002051// Tests that AllOf(m1, ..., mn) describes itself properly.
2052TEST(AllOfTest, CanDescribeSelf) {
2053 Matcher<int> m;
2054 m = AllOf(Le(2), Ge(1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002055 EXPECT_EQ("(is <= 2) and (is >= 1)", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00002056
2057 m = AllOf(Gt(0), Ne(1), Ne(2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002058 EXPECT_EQ("(is > 0) and "
2059 "((isn't equal to 1) and "
2060 "(isn't equal to 2))",
shiqiane35fdd92008-12-10 05:08:54 +00002061 Describe(m));
2062
2063
2064 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
jgm79a367e2012-04-10 16:02:11 +00002065 EXPECT_EQ("((is > 0) and "
2066 "(isn't equal to 1)) and "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002067 "((isn't equal to 2) and "
jgm79a367e2012-04-10 16:02:11 +00002068 "(isn't equal to 3))",
shiqiane35fdd92008-12-10 05:08:54 +00002069 Describe(m));
2070
2071
2072 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
jgm79a367e2012-04-10 16:02:11 +00002073 EXPECT_EQ("((is >= 0) and "
2074 "(is < 10)) and "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002075 "((isn't equal to 3) and "
2076 "((isn't equal to 5) and "
jgm79a367e2012-04-10 16:02:11 +00002077 "(isn't equal to 7)))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002078 Describe(m));
2079}
2080
2081// Tests that AllOf(m1, ..., mn) describes its negation properly.
2082TEST(AllOfTest, CanDescribeNegation) {
2083 Matcher<int> m;
2084 m = AllOf(Le(2), Ge(1));
2085 EXPECT_EQ("(isn't <= 2) or "
2086 "(isn't >= 1)",
2087 DescribeNegation(m));
2088
2089 m = AllOf(Gt(0), Ne(1), Ne(2));
2090 EXPECT_EQ("(isn't > 0) or "
2091 "((is equal to 1) or "
2092 "(is equal to 2))",
2093 DescribeNegation(m));
2094
2095
2096 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
jgm79a367e2012-04-10 16:02:11 +00002097 EXPECT_EQ("((isn't > 0) or "
2098 "(is equal to 1)) or "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002099 "((is equal to 2) or "
jgm79a367e2012-04-10 16:02:11 +00002100 "(is equal to 3))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002101 DescribeNegation(m));
2102
2103
2104 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
jgm79a367e2012-04-10 16:02:11 +00002105 EXPECT_EQ("((isn't >= 0) or "
2106 "(isn't < 10)) or "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002107 "((is equal to 3) or "
2108 "((is equal to 5) or "
jgm79a367e2012-04-10 16:02:11 +00002109 "(is equal to 7)))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002110 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00002111}
2112
zhanyong.wan18490652009-05-11 18:54:08 +00002113// Tests that monomorphic matchers are safely cast by the AllOf matcher.
2114TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
2115 // greater_than_5 and less_than_10 are monomorphic matchers.
2116 Matcher<int> greater_than_5 = Gt(5);
2117 Matcher<int> less_than_10 = Lt(10);
2118
2119 Matcher<const int&> m = AllOf(greater_than_5, less_than_10);
2120 Matcher<int&> m2 = AllOf(greater_than_5, less_than_10);
2121 Matcher<int&> m3 = AllOf(greater_than_5, m2);
2122
2123 // Tests that BothOf works when composing itself.
2124 Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10);
2125 Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);
2126}
2127
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002128TEST(AllOfTest, ExplainsResult) {
2129 Matcher<int> m;
2130
2131 // Successful match. Both matchers need to explain. The second
2132 // matcher doesn't give an explanation, so only the first matcher's
2133 // explanation is printed.
2134 m = AllOf(GreaterThan(10), Lt(30));
2135 EXPECT_EQ("which is 15 more than 10", Explain(m, 25));
2136
2137 // Successful match. Both matchers need to explain.
2138 m = AllOf(GreaterThan(10), GreaterThan(20));
2139 EXPECT_EQ("which is 20 more than 10, and which is 10 more than 20",
2140 Explain(m, 30));
2141
2142 // Successful match. All matchers need to explain. The second
2143 // matcher doesn't given an explanation.
2144 m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20));
2145 EXPECT_EQ("which is 15 more than 10, and which is 5 more than 20",
2146 Explain(m, 25));
2147
2148 // Successful match. All matchers need to explain.
2149 m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2150 EXPECT_EQ("which is 30 more than 10, and which is 20 more than 20, "
2151 "and which is 10 more than 30",
2152 Explain(m, 40));
2153
2154 // Failed match. The first matcher, which failed, needs to
2155 // explain.
2156 m = AllOf(GreaterThan(10), GreaterThan(20));
2157 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
2158
2159 // Failed match. The second matcher, which failed, needs to
2160 // explain. Since it doesn't given an explanation, nothing is
2161 // printed.
2162 m = AllOf(GreaterThan(10), Lt(30));
2163 EXPECT_EQ("", Explain(m, 40));
2164
2165 // Failed match. The second matcher, which failed, needs to
2166 // explain.
2167 m = AllOf(GreaterThan(10), GreaterThan(20));
2168 EXPECT_EQ("which is 5 less than 20", Explain(m, 15));
2169}
2170
zhanyong.wan02c15052010-06-09 19:21:30 +00002171// Helper to allow easy testing of AnyOf matchers with num parameters.
2172void AnyOfMatches(int num, const Matcher<int>& m) {
2173 SCOPED_TRACE(Describe(m));
2174 EXPECT_FALSE(m.Matches(0));
2175 for (int i = 1; i <= num; ++i) {
2176 EXPECT_TRUE(m.Matches(i));
2177 }
2178 EXPECT_FALSE(m.Matches(num + 1));
2179}
2180
shiqiane35fdd92008-12-10 05:08:54 +00002181// Tests that AnyOf(m1, ..., mn) matches any value that matches at
2182// least one of the given matchers.
2183TEST(AnyOfTest, MatchesWhenAnyMatches) {
2184 Matcher<int> m;
2185 m = AnyOf(Le(1), Ge(3));
2186 EXPECT_TRUE(m.Matches(1));
2187 EXPECT_TRUE(m.Matches(4));
2188 EXPECT_FALSE(m.Matches(2));
2189
2190 m = AnyOf(Lt(0), Eq(1), Eq(2));
2191 EXPECT_TRUE(m.Matches(-1));
2192 EXPECT_TRUE(m.Matches(1));
2193 EXPECT_TRUE(m.Matches(2));
2194 EXPECT_FALSE(m.Matches(0));
2195
2196 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2197 EXPECT_TRUE(m.Matches(-1));
2198 EXPECT_TRUE(m.Matches(1));
2199 EXPECT_TRUE(m.Matches(2));
2200 EXPECT_TRUE(m.Matches(3));
2201 EXPECT_FALSE(m.Matches(0));
2202
2203 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2204 EXPECT_TRUE(m.Matches(0));
2205 EXPECT_TRUE(m.Matches(11));
2206 EXPECT_TRUE(m.Matches(3));
2207 EXPECT_FALSE(m.Matches(2));
zhanyong.wan02c15052010-06-09 19:21:30 +00002208
2209 // The following tests for varying number of sub-matchers. Due to the way
2210 // the sub-matchers are handled it is enough to test every sub-matcher once
2211 // with sub-matchers using the same matcher type. Varying matcher types are
2212 // checked for above.
2213 AnyOfMatches(2, AnyOf(1, 2));
2214 AnyOfMatches(3, AnyOf(1, 2, 3));
2215 AnyOfMatches(4, AnyOf(1, 2, 3, 4));
2216 AnyOfMatches(5, AnyOf(1, 2, 3, 4, 5));
2217 AnyOfMatches(6, AnyOf(1, 2, 3, 4, 5, 6));
2218 AnyOfMatches(7, AnyOf(1, 2, 3, 4, 5, 6, 7));
2219 AnyOfMatches(8, AnyOf(1, 2, 3, 4, 5, 6, 7, 8));
2220 AnyOfMatches(9, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9));
2221 AnyOfMatches(10, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
shiqiane35fdd92008-12-10 05:08:54 +00002222}
2223
zhanyong.wan616180e2013-06-18 18:49:51 +00002224#if GTEST_LANG_CXX11
2225// Tests the variadic version of the AnyOfMatcher.
2226TEST(AnyOfTest, VariadicMatchesWhenAnyMatches) {
2227 // Also make sure AnyOf is defined in the right namespace and does not depend
2228 // on ADL.
2229 Matcher<int> m = ::testing::AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
2230
2231 EXPECT_THAT(Describe(m), EndsWith("or (is equal to 11))))))))))"));
2232 AnyOfMatches(11, m);
2233 AnyOfMatches(50, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
2234 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
2235 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
2236 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
2237 41, 42, 43, 44, 45, 46, 47, 48, 49, 50));
2238}
2239
2240#endif // GTEST_LANG_CXX11
2241
shiqiane35fdd92008-12-10 05:08:54 +00002242// Tests that AnyOf(m1, ..., mn) describes itself properly.
2243TEST(AnyOfTest, CanDescribeSelf) {
2244 Matcher<int> m;
2245 m = AnyOf(Le(1), Ge(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002246 EXPECT_EQ("(is <= 1) or (is >= 3)",
shiqiane35fdd92008-12-10 05:08:54 +00002247 Describe(m));
2248
2249 m = AnyOf(Lt(0), Eq(1), Eq(2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002250 EXPECT_EQ("(is < 0) or "
shiqiane35fdd92008-12-10 05:08:54 +00002251 "((is equal to 1) or (is equal to 2))",
2252 Describe(m));
2253
2254 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
jgm79a367e2012-04-10 16:02:11 +00002255 EXPECT_EQ("((is < 0) or "
2256 "(is equal to 1)) or "
shiqiane35fdd92008-12-10 05:08:54 +00002257 "((is equal to 2) or "
jgm79a367e2012-04-10 16:02:11 +00002258 "(is equal to 3))",
shiqiane35fdd92008-12-10 05:08:54 +00002259 Describe(m));
2260
2261 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
jgm79a367e2012-04-10 16:02:11 +00002262 EXPECT_EQ("((is <= 0) or "
2263 "(is > 10)) or "
shiqiane35fdd92008-12-10 05:08:54 +00002264 "((is equal to 3) or "
2265 "((is equal to 5) or "
jgm79a367e2012-04-10 16:02:11 +00002266 "(is equal to 7)))",
shiqiane35fdd92008-12-10 05:08:54 +00002267 Describe(m));
2268}
2269
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002270// Tests that AnyOf(m1, ..., mn) describes its negation properly.
2271TEST(AnyOfTest, CanDescribeNegation) {
2272 Matcher<int> m;
2273 m = AnyOf(Le(1), Ge(3));
2274 EXPECT_EQ("(isn't <= 1) and (isn't >= 3)",
2275 DescribeNegation(m));
2276
2277 m = AnyOf(Lt(0), Eq(1), Eq(2));
2278 EXPECT_EQ("(isn't < 0) and "
2279 "((isn't equal to 1) and (isn't equal to 2))",
2280 DescribeNegation(m));
2281
2282 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
jgm79a367e2012-04-10 16:02:11 +00002283 EXPECT_EQ("((isn't < 0) and "
2284 "(isn't equal to 1)) and "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002285 "((isn't equal to 2) and "
jgm79a367e2012-04-10 16:02:11 +00002286 "(isn't equal to 3))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002287 DescribeNegation(m));
2288
2289 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
jgm79a367e2012-04-10 16:02:11 +00002290 EXPECT_EQ("((isn't <= 0) and "
2291 "(isn't > 10)) and "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002292 "((isn't equal to 3) and "
2293 "((isn't equal to 5) and "
jgm79a367e2012-04-10 16:02:11 +00002294 "(isn't equal to 7)))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002295 DescribeNegation(m));
2296}
2297
zhanyong.wan18490652009-05-11 18:54:08 +00002298// Tests that monomorphic matchers are safely cast by the AnyOf matcher.
2299TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
2300 // greater_than_5 and less_than_10 are monomorphic matchers.
2301 Matcher<int> greater_than_5 = Gt(5);
2302 Matcher<int> less_than_10 = Lt(10);
2303
2304 Matcher<const int&> m = AnyOf(greater_than_5, less_than_10);
2305 Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10);
2306 Matcher<int&> m3 = AnyOf(greater_than_5, m2);
2307
2308 // Tests that EitherOf works when composing itself.
2309 Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10);
2310 Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);
2311}
2312
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002313TEST(AnyOfTest, ExplainsResult) {
2314 Matcher<int> m;
2315
2316 // Failed match. Both matchers need to explain. The second
2317 // matcher doesn't give an explanation, so only the first matcher's
2318 // explanation is printed.
2319 m = AnyOf(GreaterThan(10), Lt(0));
2320 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
2321
2322 // Failed match. Both matchers need to explain.
2323 m = AnyOf(GreaterThan(10), GreaterThan(20));
2324 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20",
2325 Explain(m, 5));
2326
2327 // Failed match. All matchers need to explain. The second
2328 // matcher doesn't given an explanation.
2329 m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30));
2330 EXPECT_EQ("which is 5 less than 10, and which is 25 less than 30",
2331 Explain(m, 5));
2332
2333 // Failed match. All matchers need to explain.
2334 m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2335 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20, "
2336 "and which is 25 less than 30",
2337 Explain(m, 5));
2338
2339 // Successful match. The first matcher, which succeeded, needs to
2340 // explain.
2341 m = AnyOf(GreaterThan(10), GreaterThan(20));
2342 EXPECT_EQ("which is 5 more than 10", Explain(m, 15));
2343
2344 // Successful match. The second matcher, which succeeded, needs to
2345 // explain. Since it doesn't given an explanation, nothing is
2346 // printed.
2347 m = AnyOf(GreaterThan(10), Lt(30));
2348 EXPECT_EQ("", Explain(m, 0));
2349
2350 // Successful match. The second matcher, which succeeded, needs to
2351 // explain.
2352 m = AnyOf(GreaterThan(30), GreaterThan(20));
2353 EXPECT_EQ("which is 5 more than 20", Explain(m, 25));
2354}
2355
shiqiane35fdd92008-12-10 05:08:54 +00002356// The following predicate function and predicate functor are for
2357// testing the Truly(predicate) matcher.
2358
2359// Returns non-zero if the input is positive. Note that the return
2360// type of this function is not bool. It's OK as Truly() accepts any
2361// unary function or functor whose return type can be implicitly
2362// converted to bool.
2363int IsPositive(double x) {
2364 return x > 0 ? 1 : 0;
2365}
2366
2367// This functor returns true if the input is greater than the given
2368// number.
2369class IsGreaterThan {
2370 public:
2371 explicit IsGreaterThan(int threshold) : threshold_(threshold) {}
2372
2373 bool operator()(int n) const { return n > threshold_; }
zhanyong.wan32de5f52009-12-23 00:13:23 +00002374
shiqiane35fdd92008-12-10 05:08:54 +00002375 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002376 int threshold_;
shiqiane35fdd92008-12-10 05:08:54 +00002377};
2378
2379// For testing Truly().
2380const int foo = 0;
2381
2382// This predicate returns true iff the argument references foo and has
2383// a zero value.
2384bool ReferencesFooAndIsZero(const int& n) {
2385 return (&n == &foo) && (n == 0);
2386}
2387
2388// Tests that Truly(predicate) matches what satisfies the given
2389// predicate.
2390TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {
2391 Matcher<double> m = Truly(IsPositive);
2392 EXPECT_TRUE(m.Matches(2.0));
2393 EXPECT_FALSE(m.Matches(-1.5));
2394}
2395
2396// Tests that Truly(predicate_functor) works too.
2397TEST(TrulyTest, CanBeUsedWithFunctor) {
2398 Matcher<int> m = Truly(IsGreaterThan(5));
2399 EXPECT_TRUE(m.Matches(6));
2400 EXPECT_FALSE(m.Matches(4));
2401}
2402
zhanyong.wan8d3dc0c2011-04-14 19:37:06 +00002403// A class that can be implicitly converted to bool.
2404class ConvertibleToBool {
2405 public:
2406 explicit ConvertibleToBool(int number) : number_(number) {}
2407 operator bool() const { return number_ != 0; }
2408
2409 private:
2410 int number_;
2411};
2412
2413ConvertibleToBool IsNotZero(int number) {
2414 return ConvertibleToBool(number);
2415}
2416
2417// Tests that the predicate used in Truly() may return a class that's
2418// implicitly convertible to bool, even when the class has no
2419// operator!().
2420TEST(TrulyTest, PredicateCanReturnAClassConvertibleToBool) {
2421 Matcher<int> m = Truly(IsNotZero);
2422 EXPECT_TRUE(m.Matches(1));
2423 EXPECT_FALSE(m.Matches(0));
2424}
2425
shiqiane35fdd92008-12-10 05:08:54 +00002426// Tests that Truly(predicate) can describe itself properly.
2427TEST(TrulyTest, CanDescribeSelf) {
2428 Matcher<double> m = Truly(IsPositive);
2429 EXPECT_EQ("satisfies the given predicate",
2430 Describe(m));
2431}
2432
2433// Tests that Truly(predicate) works when the matcher takes its
2434// argument by reference.
2435TEST(TrulyTest, WorksForByRefArguments) {
2436 Matcher<const int&> m = Truly(ReferencesFooAndIsZero);
2437 EXPECT_TRUE(m.Matches(foo));
2438 int n = 0;
2439 EXPECT_FALSE(m.Matches(n));
2440}
2441
2442// Tests that Matches(m) is a predicate satisfied by whatever that
2443// matches matcher m.
2444TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {
2445 EXPECT_TRUE(Matches(Ge(0))(1));
2446 EXPECT_FALSE(Matches(Eq('a'))('b'));
2447}
2448
2449// Tests that Matches(m) works when the matcher takes its argument by
2450// reference.
2451TEST(MatchesTest, WorksOnByRefArguments) {
2452 int m = 0, n = 0;
2453 EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n));
2454 EXPECT_FALSE(Matches(Ref(m))(n));
2455}
2456
2457// Tests that a Matcher on non-reference type can be used in
2458// Matches().
2459TEST(MatchesTest, WorksWithMatcherOnNonRefType) {
2460 Matcher<int> eq5 = Eq(5);
2461 EXPECT_TRUE(Matches(eq5)(5));
2462 EXPECT_FALSE(Matches(eq5)(2));
2463}
2464
zhanyong.wanb8243162009-06-04 05:48:20 +00002465// Tests Value(value, matcher). Since Value() is a simple wrapper for
2466// Matches(), which has been tested already, we don't spend a lot of
2467// effort on testing Value().
2468TEST(ValueTest, WorksWithPolymorphicMatcher) {
2469 EXPECT_TRUE(Value("hi", StartsWith("h")));
2470 EXPECT_FALSE(Value(5, Gt(10)));
2471}
2472
2473TEST(ValueTest, WorksWithMonomorphicMatcher) {
2474 const Matcher<int> is_zero = Eq(0);
2475 EXPECT_TRUE(Value(0, is_zero));
2476 EXPECT_FALSE(Value('a', is_zero));
2477
2478 int n = 0;
2479 const Matcher<const int&> ref_n = Ref(n);
2480 EXPECT_TRUE(Value(n, ref_n));
2481 EXPECT_FALSE(Value(1, ref_n));
2482}
2483
zhanyong.wana862f1d2010-03-15 21:23:04 +00002484TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) {
zhanyong.wan34b034c2010-03-05 21:23:23 +00002485 StringMatchResultListener listener1;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002486 EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002487 EXPECT_EQ("% 2 == 0", listener1.str());
2488
2489 StringMatchResultListener listener2;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002490 EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002491 EXPECT_EQ("", listener2.str());
2492}
2493
zhanyong.wana862f1d2010-03-15 21:23:04 +00002494TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) {
zhanyong.wan34b034c2010-03-05 21:23:23 +00002495 const Matcher<int> is_even = PolymorphicIsEven();
2496 StringMatchResultListener listener1;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002497 EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002498 EXPECT_EQ("% 2 == 0", listener1.str());
2499
2500 const Matcher<const double&> is_zero = Eq(0);
2501 StringMatchResultListener listener2;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002502 EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002503 EXPECT_EQ("", listener2.str());
2504}
2505
zhanyong.wana862f1d2010-03-15 21:23:04 +00002506MATCHER_P(Really, inner_matcher, "") {
2507 return ExplainMatchResult(inner_matcher, arg, result_listener);
2508}
2509
2510TEST(ExplainMatchResultTest, WorksInsideMATCHER) {
2511 EXPECT_THAT(0, Really(Eq(0)));
2512}
2513
zhanyong.wanbf550852009-06-09 06:09:53 +00002514TEST(AllArgsTest, WorksForTuple) {
2515 EXPECT_THAT(make_tuple(1, 2L), AllArgs(Lt()));
2516 EXPECT_THAT(make_tuple(2L, 1), Not(AllArgs(Lt())));
2517}
2518
2519TEST(AllArgsTest, WorksForNonTuple) {
2520 EXPECT_THAT(42, AllArgs(Gt(0)));
2521 EXPECT_THAT('a', Not(AllArgs(Eq('b'))));
2522}
2523
2524class AllArgsHelper {
2525 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002526 AllArgsHelper() {}
2527
zhanyong.wanbf550852009-06-09 06:09:53 +00002528 MOCK_METHOD2(Helper, int(char x, int y));
zhanyong.wan32de5f52009-12-23 00:13:23 +00002529
2530 private:
2531 GTEST_DISALLOW_COPY_AND_ASSIGN_(AllArgsHelper);
zhanyong.wanbf550852009-06-09 06:09:53 +00002532};
2533
2534TEST(AllArgsTest, WorksInWithClause) {
2535 AllArgsHelper helper;
2536 ON_CALL(helper, Helper(_, _))
2537 .With(AllArgs(Lt()))
2538 .WillByDefault(Return(1));
2539 EXPECT_CALL(helper, Helper(_, _));
2540 EXPECT_CALL(helper, Helper(_, _))
2541 .With(AllArgs(Gt()))
2542 .WillOnce(Return(2));
2543
2544 EXPECT_EQ(1, helper.Helper('\1', 2));
2545 EXPECT_EQ(2, helper.Helper('a', 1));
2546}
2547
shiqiane35fdd92008-12-10 05:08:54 +00002548// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2549// matches the matcher.
2550TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {
2551 ASSERT_THAT(5, Ge(2)) << "This should succeed.";
2552 ASSERT_THAT("Foo", EndsWith("oo"));
2553 EXPECT_THAT(2, AllOf(Le(7), Ge(0))) << "This should succeed too.";
2554 EXPECT_THAT("Hello", StartsWith("Hell"));
2555}
2556
2557// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2558// doesn't match the matcher.
2559TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {
2560 // 'n' must be static as it is used in an EXPECT_FATAL_FAILURE(),
2561 // which cannot reference auto variables.
zhanyong.wan736baa82010-09-27 17:44:16 +00002562 static unsigned short n; // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00002563 n = 5;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002564
2565 // VC++ prior to version 8.0 SP1 has a bug where it will not see any
2566 // functions declared in the namespace scope from within nested classes.
2567 // EXPECT/ASSERT_(NON)FATAL_FAILURE macros use nested classes so that all
2568 // namespace-level functions invoked inside them need to be explicitly
2569 // resolved.
2570 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Gt(10)),
shiqiane35fdd92008-12-10 05:08:54 +00002571 "Value of: n\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002572 "Expected: is > 10\n"
zhanyong.wan736baa82010-09-27 17:44:16 +00002573 " Actual: 5" + OfType("unsigned short"));
shiqiane35fdd92008-12-10 05:08:54 +00002574 n = 0;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002575 EXPECT_NONFATAL_FAILURE(
2576 EXPECT_THAT(n, ::testing::AllOf(::testing::Le(7), ::testing::Ge(5))),
2577 "Value of: n\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002578 "Expected: (is <= 7) and (is >= 5)\n"
zhanyong.wan736baa82010-09-27 17:44:16 +00002579 " Actual: 0" + OfType("unsigned short"));
shiqiane35fdd92008-12-10 05:08:54 +00002580}
2581
2582// Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument
2583// has a reference type.
2584TEST(MatcherAssertionTest, WorksForByRefArguments) {
2585 // We use a static variable here as EXPECT_FATAL_FAILURE() cannot
2586 // reference auto variables.
2587 static int n;
2588 n = 0;
2589 EXPECT_THAT(n, AllOf(Le(7), Ref(n)));
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002590 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
shiqiane35fdd92008-12-10 05:08:54 +00002591 "Value of: n\n"
2592 "Expected: does not reference the variable @");
2593 // Tests the "Actual" part.
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002594 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
zhanyong.wan736baa82010-09-27 17:44:16 +00002595 "Actual: 0" + OfType("int") + ", which is located @");
shiqiane35fdd92008-12-10 05:08:54 +00002596}
2597
zhanyong.wan95b12332009-09-25 18:55:50 +00002598#if !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002599// Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is
2600// monomorphic.
zhanyong.wan95b12332009-09-25 18:55:50 +00002601
2602// ASSERT_THAT("hello", starts_with_he) fails to compile with Nokia's
2603// Symbian compiler: it tries to compile
2604// template<T, U> class MatcherCastImpl { ...
zhanyong.wandb22c222010-01-28 21:52:29 +00002605// virtual bool MatchAndExplain(T x, ...) const {
2606// return source_matcher_.MatchAndExplain(static_cast<U>(x), ...);
zhanyong.wan95b12332009-09-25 18:55:50 +00002607// with U == string and T == const char*
2608// With ASSERT_THAT("hello"...) changed to ASSERT_THAT(string("hello") ... )
2609// the compiler silently crashes with no output.
2610// If MatcherCastImpl is changed to use U(x) instead of static_cast<U>(x)
2611// the code compiles but the converted string is bogus.
shiqiane35fdd92008-12-10 05:08:54 +00002612TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
2613 Matcher<const char*> starts_with_he = StartsWith("he");
2614 ASSERT_THAT("hello", starts_with_he);
2615
2616 Matcher<const string&> ends_with_ok = EndsWith("ok");
2617 ASSERT_THAT("book", ends_with_ok);
zhanyong.wan736baa82010-09-27 17:44:16 +00002618 const string bad = "bad";
2619 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(bad, ends_with_ok),
2620 "Value of: bad\n"
2621 "Expected: ends with \"ok\"\n"
2622 " Actual: \"bad\"");
shiqiane35fdd92008-12-10 05:08:54 +00002623 Matcher<int> is_greater_than_5 = Gt(5);
2624 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5),
2625 "Value of: 5\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002626 "Expected: is > 5\n"
zhanyong.wan736baa82010-09-27 17:44:16 +00002627 " Actual: 5" + OfType("int"));
shiqiane35fdd92008-12-10 05:08:54 +00002628}
zhanyong.wan95b12332009-09-25 18:55:50 +00002629#endif // !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002630
2631// Tests floating-point matchers.
2632template <typename RawType>
2633class FloatingPointTest : public testing::Test {
2634 protected:
2635 typedef typename testing::internal::FloatingPoint<RawType> Floating;
2636 typedef typename Floating::Bits Bits;
2637
2638 virtual void SetUp() {
2639 const size_t max_ulps = Floating::kMaxUlps;
2640
2641 // The bits that represent 0.0.
2642 const Bits zero_bits = Floating(0).bits();
2643
2644 // Makes some numbers close to 0.0.
2645 close_to_positive_zero_ = Floating::ReinterpretBits(zero_bits + max_ulps/2);
2646 close_to_negative_zero_ = -Floating::ReinterpretBits(
2647 zero_bits + max_ulps - max_ulps/2);
2648 further_from_negative_zero_ = -Floating::ReinterpretBits(
2649 zero_bits + max_ulps + 1 - max_ulps/2);
2650
2651 // The bits that represent 1.0.
2652 const Bits one_bits = Floating(1).bits();
2653
2654 // Makes some numbers close to 1.0.
2655 close_to_one_ = Floating::ReinterpretBits(one_bits + max_ulps);
2656 further_from_one_ = Floating::ReinterpretBits(one_bits + max_ulps + 1);
2657
2658 // +infinity.
2659 infinity_ = Floating::Infinity();
2660
2661 // The bits that represent +infinity.
2662 const Bits infinity_bits = Floating(infinity_).bits();
2663
2664 // Makes some numbers close to infinity.
2665 close_to_infinity_ = Floating::ReinterpretBits(infinity_bits - max_ulps);
2666 further_from_infinity_ = Floating::ReinterpretBits(
2667 infinity_bits - max_ulps - 1);
2668
2669 // Makes some NAN's.
2670 nan1_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 1);
2671 nan2_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 200);
2672 }
2673
2674 void TestSize() {
2675 EXPECT_EQ(sizeof(RawType), sizeof(Bits));
2676 }
2677
2678 // A battery of tests for FloatingEqMatcher::Matches.
2679 // matcher_maker is a pointer to a function which creates a FloatingEqMatcher.
2680 void TestMatches(
2681 testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) {
2682 Matcher<RawType> m1 = matcher_maker(0.0);
2683 EXPECT_TRUE(m1.Matches(-0.0));
2684 EXPECT_TRUE(m1.Matches(close_to_positive_zero_));
2685 EXPECT_TRUE(m1.Matches(close_to_negative_zero_));
2686 EXPECT_FALSE(m1.Matches(1.0));
2687
2688 Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_);
2689 EXPECT_FALSE(m2.Matches(further_from_negative_zero_));
2690
2691 Matcher<RawType> m3 = matcher_maker(1.0);
2692 EXPECT_TRUE(m3.Matches(close_to_one_));
2693 EXPECT_FALSE(m3.Matches(further_from_one_));
2694
2695 // Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above.
2696 EXPECT_FALSE(m3.Matches(0.0));
2697
2698 Matcher<RawType> m4 = matcher_maker(-infinity_);
2699 EXPECT_TRUE(m4.Matches(-close_to_infinity_));
2700
2701 Matcher<RawType> m5 = matcher_maker(infinity_);
2702 EXPECT_TRUE(m5.Matches(close_to_infinity_));
2703
2704 // This is interesting as the representations of infinity_ and nan1_
2705 // are only 1 DLP apart.
2706 EXPECT_FALSE(m5.Matches(nan1_));
2707
2708 // matcher_maker can produce a Matcher<const RawType&>, which is needed in
2709 // some cases.
2710 Matcher<const RawType&> m6 = matcher_maker(0.0);
2711 EXPECT_TRUE(m6.Matches(-0.0));
2712 EXPECT_TRUE(m6.Matches(close_to_positive_zero_));
2713 EXPECT_FALSE(m6.Matches(1.0));
2714
2715 // matcher_maker can produce a Matcher<RawType&>, which is needed in some
2716 // cases.
2717 Matcher<RawType&> m7 = matcher_maker(0.0);
2718 RawType x = 0.0;
2719 EXPECT_TRUE(m7.Matches(x));
2720 x = 0.01f;
2721 EXPECT_FALSE(m7.Matches(x));
2722 }
2723
2724 // Pre-calculated numbers to be used by the tests.
2725
2726 static RawType close_to_positive_zero_;
2727 static RawType close_to_negative_zero_;
2728 static RawType further_from_negative_zero_;
2729
2730 static RawType close_to_one_;
2731 static RawType further_from_one_;
2732
2733 static RawType infinity_;
2734 static RawType close_to_infinity_;
2735 static RawType further_from_infinity_;
2736
2737 static RawType nan1_;
2738 static RawType nan2_;
2739};
2740
2741template <typename RawType>
2742RawType FloatingPointTest<RawType>::close_to_positive_zero_;
2743
2744template <typename RawType>
2745RawType FloatingPointTest<RawType>::close_to_negative_zero_;
2746
2747template <typename RawType>
2748RawType FloatingPointTest<RawType>::further_from_negative_zero_;
2749
2750template <typename RawType>
2751RawType FloatingPointTest<RawType>::close_to_one_;
2752
2753template <typename RawType>
2754RawType FloatingPointTest<RawType>::further_from_one_;
2755
2756template <typename RawType>
2757RawType FloatingPointTest<RawType>::infinity_;
2758
2759template <typename RawType>
2760RawType FloatingPointTest<RawType>::close_to_infinity_;
2761
2762template <typename RawType>
2763RawType FloatingPointTest<RawType>::further_from_infinity_;
2764
2765template <typename RawType>
2766RawType FloatingPointTest<RawType>::nan1_;
2767
2768template <typename RawType>
2769RawType FloatingPointTest<RawType>::nan2_;
2770
zhanyong.wan616180e2013-06-18 18:49:51 +00002771// Tests floating-point matchers with fixed epsilons.
2772template <typename RawType>
2773class FloatingPointNearTest : public FloatingPointTest<RawType> {
2774 protected:
2775 typedef FloatingPointTest<RawType> ParentType;
2776
2777 // A battery of tests for FloatingEqMatcher::Matches with a fixed epsilon.
2778 // matcher_maker is a pointer to a function which creates a FloatingEqMatcher.
2779 void TestNearMatches(
2780 testing::internal::FloatingEqMatcher<RawType>
2781 (*matcher_maker)(RawType, RawType)) {
2782 Matcher<RawType> m1 = matcher_maker(0.0, 0.0);
2783 EXPECT_TRUE(m1.Matches(0.0));
2784 EXPECT_TRUE(m1.Matches(-0.0));
2785 EXPECT_FALSE(m1.Matches(ParentType::close_to_positive_zero_));
2786 EXPECT_FALSE(m1.Matches(ParentType::close_to_negative_zero_));
2787 EXPECT_FALSE(m1.Matches(1.0));
2788
2789 Matcher<RawType> m2 = matcher_maker(0.0, 1.0);
2790 EXPECT_TRUE(m2.Matches(0.0));
2791 EXPECT_TRUE(m2.Matches(-0.0));
2792 EXPECT_TRUE(m2.Matches(1.0));
2793 EXPECT_TRUE(m2.Matches(-1.0));
2794 EXPECT_FALSE(m2.Matches(ParentType::close_to_one_));
2795 EXPECT_FALSE(m2.Matches(-ParentType::close_to_one_));
2796
2797 // Check that inf matches inf, regardless of the of the specified max
2798 // absolute error.
2799 Matcher<RawType> m3 = matcher_maker(ParentType::infinity_, 0.0);
2800 EXPECT_TRUE(m3.Matches(ParentType::infinity_));
2801 EXPECT_FALSE(m3.Matches(ParentType::close_to_infinity_));
2802 EXPECT_FALSE(m3.Matches(-ParentType::infinity_));
2803
2804 Matcher<RawType> m4 = matcher_maker(-ParentType::infinity_, 0.0);
2805 EXPECT_TRUE(m4.Matches(-ParentType::infinity_));
2806 EXPECT_FALSE(m4.Matches(-ParentType::close_to_infinity_));
2807 EXPECT_FALSE(m4.Matches(ParentType::infinity_));
2808
2809 // Test various overflow scenarios.
2810 Matcher<RawType> m5 = matcher_maker(
2811 std::numeric_limits<RawType>::max(),
2812 std::numeric_limits<RawType>::max());
2813 EXPECT_TRUE(m5.Matches(std::numeric_limits<RawType>::max()));
2814 EXPECT_FALSE(m5.Matches(-std::numeric_limits<RawType>::max()));
2815
2816 Matcher<RawType> m6 = matcher_maker(
2817 -std::numeric_limits<RawType>::max(),
2818 std::numeric_limits<RawType>::max());
2819 EXPECT_FALSE(m6.Matches(std::numeric_limits<RawType>::max()));
2820 EXPECT_TRUE(m6.Matches(-std::numeric_limits<RawType>::max()));
2821
2822 Matcher<RawType> m7 = matcher_maker(std::numeric_limits<RawType>::max(), 0);
2823 EXPECT_TRUE(m7.Matches(std::numeric_limits<RawType>::max()));
2824 EXPECT_FALSE(m7.Matches(-std::numeric_limits<RawType>::max()));
2825
2826 Matcher<RawType> m8 = matcher_maker(
2827 -std::numeric_limits<RawType>::max(), 0);
2828 EXPECT_FALSE(m8.Matches(std::numeric_limits<RawType>::max()));
2829 EXPECT_TRUE(m8.Matches(-std::numeric_limits<RawType>::max()));
2830
2831 // The difference between max() and -max() normally overflows to infinity,
2832 // but it should still match if the max_abs_error is also infinity.
2833 Matcher<RawType> m9 = matcher_maker(
2834 std::numeric_limits<RawType>::max(), ParentType::infinity_);
2835 EXPECT_TRUE(m8.Matches(-std::numeric_limits<RawType>::max()));
2836
2837 // matcher_maker can produce a Matcher<const RawType&>, which is needed in
2838 // some cases.
2839 Matcher<const RawType&> m10 = matcher_maker(0.0, 1.0);
2840 EXPECT_TRUE(m10.Matches(-0.0));
2841 EXPECT_TRUE(m10.Matches(ParentType::close_to_positive_zero_));
2842 EXPECT_FALSE(m10.Matches(ParentType::close_to_one_));
2843
2844 // matcher_maker can produce a Matcher<RawType&>, which is needed in some
2845 // cases.
2846 Matcher<RawType&> m11 = matcher_maker(0.0, 1.0);
2847 RawType x = 0.0;
2848 EXPECT_TRUE(m11.Matches(x));
2849 x = 1.0f;
2850 EXPECT_TRUE(m11.Matches(x));
2851 x = -1.0f;
2852 EXPECT_TRUE(m11.Matches(x));
2853 x = 1.1f;
2854 EXPECT_FALSE(m11.Matches(x));
2855 x = -1.1f;
2856 EXPECT_FALSE(m11.Matches(x));
2857 }
2858};
2859
shiqiane35fdd92008-12-10 05:08:54 +00002860// Instantiate FloatingPointTest for testing floats.
2861typedef FloatingPointTest<float> FloatTest;
2862
2863TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) {
2864 TestMatches(&FloatEq);
2865}
2866
2867TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
2868 TestMatches(&NanSensitiveFloatEq);
2869}
2870
2871TEST_F(FloatTest, FloatEqCannotMatchNaN) {
2872 // FloatEq never matches NaN.
2873 Matcher<float> m = FloatEq(nan1_);
2874 EXPECT_FALSE(m.Matches(nan1_));
2875 EXPECT_FALSE(m.Matches(nan2_));
2876 EXPECT_FALSE(m.Matches(1.0));
2877}
2878
2879TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {
2880 // NanSensitiveFloatEq will match NaN.
2881 Matcher<float> m = NanSensitiveFloatEq(nan1_);
2882 EXPECT_TRUE(m.Matches(nan1_));
2883 EXPECT_TRUE(m.Matches(nan2_));
2884 EXPECT_FALSE(m.Matches(1.0));
2885}
2886
2887TEST_F(FloatTest, FloatEqCanDescribeSelf) {
2888 Matcher<float> m1 = FloatEq(2.0f);
2889 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002890 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002891
2892 Matcher<float> m2 = FloatEq(0.5f);
2893 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002894 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002895
2896 Matcher<float> m3 = FloatEq(nan1_);
2897 EXPECT_EQ("never matches", Describe(m3));
2898 EXPECT_EQ("is anything", DescribeNegation(m3));
2899}
2900
2901TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {
2902 Matcher<float> m1 = NanSensitiveFloatEq(2.0f);
2903 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002904 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002905
2906 Matcher<float> m2 = NanSensitiveFloatEq(0.5f);
2907 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002908 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002909
2910 Matcher<float> m3 = NanSensitiveFloatEq(nan1_);
2911 EXPECT_EQ("is NaN", Describe(m3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002912 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
shiqiane35fdd92008-12-10 05:08:54 +00002913}
2914
zhanyong.wan616180e2013-06-18 18:49:51 +00002915// Instantiate FloatingPointTest for testing floats with a user-specified
2916// max absolute error.
2917typedef FloatingPointNearTest<float> FloatNearTest;
2918
2919TEST_F(FloatNearTest, FloatNearMatches) {
2920 TestNearMatches(&FloatNear);
2921}
2922
2923TEST_F(FloatNearTest, NanSensitiveFloatNearApproximatelyMatchesFloats) {
2924 TestNearMatches(&NanSensitiveFloatNear);
2925}
2926
2927TEST_F(FloatNearTest, FloatNearCanDescribeSelf) {
2928 Matcher<float> m1 = FloatNear(2.0f, 0.5f);
2929 EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1));
2930 EXPECT_EQ(
2931 "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1));
2932
2933 Matcher<float> m2 = FloatNear(0.5f, 0.5f);
2934 EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
2935 EXPECT_EQ(
2936 "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2));
2937
2938 Matcher<float> m3 = FloatNear(nan1_, 0.0);
2939 EXPECT_EQ("never matches", Describe(m3));
2940 EXPECT_EQ("is anything", DescribeNegation(m3));
2941}
2942
2943TEST_F(FloatNearTest, NanSensitiveFloatNearCanDescribeSelf) {
2944 Matcher<float> m1 = NanSensitiveFloatNear(2.0f, 0.5f);
2945 EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1));
2946 EXPECT_EQ(
2947 "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1));
2948
2949 Matcher<float> m2 = NanSensitiveFloatNear(0.5f, 0.5f);
2950 EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
2951 EXPECT_EQ(
2952 "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2));
2953
2954 Matcher<float> m3 = NanSensitiveFloatNear(nan1_, 0.1f);
2955 EXPECT_EQ("is NaN", Describe(m3));
2956 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
2957}
2958
2959TEST_F(FloatNearTest, FloatNearCannotMatchNaN) {
2960 // FloatNear never matches NaN.
2961 Matcher<float> m = FloatNear(ParentType::nan1_, 0.1f);
2962 EXPECT_FALSE(m.Matches(nan1_));
2963 EXPECT_FALSE(m.Matches(nan2_));
2964 EXPECT_FALSE(m.Matches(1.0));
2965}
2966
2967TEST_F(FloatNearTest, NanSensitiveFloatNearCanMatchNaN) {
2968 // NanSensitiveFloatNear will match NaN.
2969 Matcher<float> m = NanSensitiveFloatNear(nan1_, 0.1f);
2970 EXPECT_TRUE(m.Matches(nan1_));
2971 EXPECT_TRUE(m.Matches(nan2_));
2972 EXPECT_FALSE(m.Matches(1.0));
2973}
2974
shiqiane35fdd92008-12-10 05:08:54 +00002975// Instantiate FloatingPointTest for testing doubles.
2976typedef FloatingPointTest<double> DoubleTest;
2977
2978TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
2979 TestMatches(&DoubleEq);
2980}
2981
2982TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
2983 TestMatches(&NanSensitiveDoubleEq);
2984}
2985
2986TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {
2987 // DoubleEq never matches NaN.
2988 Matcher<double> m = DoubleEq(nan1_);
2989 EXPECT_FALSE(m.Matches(nan1_));
2990 EXPECT_FALSE(m.Matches(nan2_));
2991 EXPECT_FALSE(m.Matches(1.0));
2992}
2993
2994TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {
2995 // NanSensitiveDoubleEq will match NaN.
2996 Matcher<double> m = NanSensitiveDoubleEq(nan1_);
2997 EXPECT_TRUE(m.Matches(nan1_));
2998 EXPECT_TRUE(m.Matches(nan2_));
2999 EXPECT_FALSE(m.Matches(1.0));
3000}
3001
3002TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {
3003 Matcher<double> m1 = DoubleEq(2.0);
3004 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003005 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00003006
3007 Matcher<double> m2 = DoubleEq(0.5);
3008 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003009 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00003010
3011 Matcher<double> m3 = DoubleEq(nan1_);
3012 EXPECT_EQ("never matches", Describe(m3));
3013 EXPECT_EQ("is anything", DescribeNegation(m3));
3014}
3015
3016TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {
3017 Matcher<double> m1 = NanSensitiveDoubleEq(2.0);
3018 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003019 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00003020
3021 Matcher<double> m2 = NanSensitiveDoubleEq(0.5);
3022 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003023 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00003024
3025 Matcher<double> m3 = NanSensitiveDoubleEq(nan1_);
3026 EXPECT_EQ("is NaN", Describe(m3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003027 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
shiqiane35fdd92008-12-10 05:08:54 +00003028}
3029
zhanyong.wan616180e2013-06-18 18:49:51 +00003030// Instantiate FloatingPointTest for testing floats with a user-specified
3031// max absolute error.
3032typedef FloatingPointNearTest<double> DoubleNearTest;
3033
3034TEST_F(DoubleNearTest, DoubleNearMatches) {
3035 TestNearMatches(&DoubleNear);
3036}
3037
3038TEST_F(DoubleNearTest, NanSensitiveDoubleNearApproximatelyMatchesDoubles) {
3039 TestNearMatches(&NanSensitiveDoubleNear);
3040}
3041
3042TEST_F(DoubleNearTest, DoubleNearCanDescribeSelf) {
3043 Matcher<double> m1 = DoubleNear(2.0, 0.5);
3044 EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1));
3045 EXPECT_EQ(
3046 "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1));
3047
3048 Matcher<double> m2 = DoubleNear(0.5, 0.5);
3049 EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
3050 EXPECT_EQ(
3051 "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2));
3052
3053 Matcher<double> m3 = DoubleNear(nan1_, 0.0);
3054 EXPECT_EQ("never matches", Describe(m3));
3055 EXPECT_EQ("is anything", DescribeNegation(m3));
3056}
3057
3058TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanDescribeSelf) {
3059 Matcher<double> m1 = NanSensitiveDoubleNear(2.0, 0.5);
3060 EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1));
3061 EXPECT_EQ(
3062 "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1));
3063
3064 Matcher<double> m2 = NanSensitiveDoubleNear(0.5, 0.5);
3065 EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
3066 EXPECT_EQ(
3067 "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2));
3068
3069 Matcher<double> m3 = NanSensitiveDoubleNear(nan1_, 0.1);
3070 EXPECT_EQ("is NaN", Describe(m3));
3071 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
3072}
3073
3074TEST_F(DoubleNearTest, DoubleNearCannotMatchNaN) {
3075 // DoubleNear never matches NaN.
3076 Matcher<double> m = DoubleNear(ParentType::nan1_, 0.1);
3077 EXPECT_FALSE(m.Matches(nan1_));
3078 EXPECT_FALSE(m.Matches(nan2_));
3079 EXPECT_FALSE(m.Matches(1.0));
3080}
3081
3082TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanMatchNaN) {
3083 // NanSensitiveDoubleNear will match NaN.
3084 Matcher<double> m = NanSensitiveDoubleNear(nan1_, 0.1);
3085 EXPECT_TRUE(m.Matches(nan1_));
3086 EXPECT_TRUE(m.Matches(nan2_));
3087 EXPECT_FALSE(m.Matches(1.0));
3088}
3089
shiqiane35fdd92008-12-10 05:08:54 +00003090TEST(PointeeTest, RawPointer) {
3091 const Matcher<int*> m = Pointee(Ge(0));
3092
3093 int n = 1;
3094 EXPECT_TRUE(m.Matches(&n));
3095 n = -1;
3096 EXPECT_FALSE(m.Matches(&n));
3097 EXPECT_FALSE(m.Matches(NULL));
3098}
3099
3100TEST(PointeeTest, RawPointerToConst) {
3101 const Matcher<const double*> m = Pointee(Ge(0));
3102
3103 double x = 1;
3104 EXPECT_TRUE(m.Matches(&x));
3105 x = -1;
3106 EXPECT_FALSE(m.Matches(&x));
3107 EXPECT_FALSE(m.Matches(NULL));
3108}
3109
3110TEST(PointeeTest, ReferenceToConstRawPointer) {
3111 const Matcher<int* const &> m = Pointee(Ge(0));
3112
3113 int n = 1;
3114 EXPECT_TRUE(m.Matches(&n));
3115 n = -1;
3116 EXPECT_FALSE(m.Matches(&n));
3117 EXPECT_FALSE(m.Matches(NULL));
3118}
3119
3120TEST(PointeeTest, ReferenceToNonConstRawPointer) {
3121 const Matcher<double* &> m = Pointee(Ge(0));
3122
3123 double x = 1.0;
3124 double* p = &x;
3125 EXPECT_TRUE(m.Matches(p));
3126 x = -1;
3127 EXPECT_FALSE(m.Matches(p));
3128 p = NULL;
3129 EXPECT_FALSE(m.Matches(p));
3130}
3131
vladlosevada23472012-08-14 15:38:49 +00003132// Minimal const-propagating pointer.
3133template <typename T>
3134class ConstPropagatingPtr {
3135 public:
3136 typedef T element_type;
3137
3138 ConstPropagatingPtr() : val_() {}
3139 explicit ConstPropagatingPtr(T* t) : val_(t) {}
3140 ConstPropagatingPtr(const ConstPropagatingPtr& other) : val_(other.val_) {}
3141
3142 T* get() { return val_; }
3143 T& operator*() { return *val_; }
3144 // Most smart pointers return non-const T* and T& from the next methods.
3145 const T* get() const { return val_; }
3146 const T& operator*() const { return *val_; }
3147
3148 private:
3149 T* val_;
3150};
3151
3152TEST(PointeeTest, WorksWithConstPropagatingPointers) {
3153 const Matcher< ConstPropagatingPtr<int> > m = Pointee(Lt(5));
3154 int three = 3;
3155 const ConstPropagatingPtr<int> co(&three);
3156 ConstPropagatingPtr<int> o(&three);
3157 EXPECT_TRUE(m.Matches(o));
3158 EXPECT_TRUE(m.Matches(co));
3159 *o = 6;
3160 EXPECT_FALSE(m.Matches(o));
3161 EXPECT_FALSE(m.Matches(ConstPropagatingPtr<int>()));
3162}
3163
shiqiane35fdd92008-12-10 05:08:54 +00003164TEST(PointeeTest, NeverMatchesNull) {
3165 const Matcher<const char*> m = Pointee(_);
3166 EXPECT_FALSE(m.Matches(NULL));
3167}
3168
3169// Tests that we can write Pointee(value) instead of Pointee(Eq(value)).
3170TEST(PointeeTest, MatchesAgainstAValue) {
3171 const Matcher<int*> m = Pointee(5);
3172
3173 int n = 5;
3174 EXPECT_TRUE(m.Matches(&n));
3175 n = -1;
3176 EXPECT_FALSE(m.Matches(&n));
3177 EXPECT_FALSE(m.Matches(NULL));
3178}
3179
3180TEST(PointeeTest, CanDescribeSelf) {
3181 const Matcher<int*> m = Pointee(Gt(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003182 EXPECT_EQ("points to a value that is > 3", Describe(m));
3183 EXPECT_EQ("does not point to a value that is > 3",
shiqiane35fdd92008-12-10 05:08:54 +00003184 DescribeNegation(m));
3185}
3186
shiqiane35fdd92008-12-10 05:08:54 +00003187TEST(PointeeTest, CanExplainMatchResult) {
3188 const Matcher<const string*> m = Pointee(StartsWith("Hi"));
3189
3190 EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL)));
3191
zhanyong.wan736baa82010-09-27 17:44:16 +00003192 const Matcher<long*> m2 = Pointee(GreaterThan(1)); // NOLINT
3193 long n = 3; // NOLINT
3194 EXPECT_EQ("which points to 3" + OfType("long") + ", which is 2 more than 1",
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003195 Explain(m2, &n));
3196}
3197
3198TEST(PointeeTest, AlwaysExplainsPointee) {
3199 const Matcher<int*> m = Pointee(0);
3200 int n = 42;
zhanyong.wan736baa82010-09-27 17:44:16 +00003201 EXPECT_EQ("which points to 42" + OfType("int"), Explain(m, &n));
shiqiane35fdd92008-12-10 05:08:54 +00003202}
3203
3204// An uncopyable class.
3205class Uncopyable {
3206 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003207 explicit Uncopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00003208
3209 int value() const { return value_; }
3210 private:
3211 const int value_;
3212 GTEST_DISALLOW_COPY_AND_ASSIGN_(Uncopyable);
3213};
3214
3215// Returns true iff x.value() is positive.
3216bool ValueIsPositive(const Uncopyable& x) { return x.value() > 0; }
3217
3218// A user-defined struct for testing Field().
3219struct AStruct {
3220 AStruct() : x(0), y(1.0), z(5), p(NULL) {}
3221 AStruct(const AStruct& rhs)
3222 : x(rhs.x), y(rhs.y), z(rhs.z.value()), p(rhs.p) {}
3223
3224 int x; // A non-const field.
3225 const double y; // A const field.
3226 Uncopyable z; // An uncopyable field.
3227 const char* p; // A pointer field.
zhanyong.wan32de5f52009-12-23 00:13:23 +00003228
3229 private:
3230 GTEST_DISALLOW_ASSIGN_(AStruct);
shiqiane35fdd92008-12-10 05:08:54 +00003231};
3232
3233// A derived struct for testing Field().
3234struct DerivedStruct : public AStruct {
3235 char ch;
zhanyong.wan32de5f52009-12-23 00:13:23 +00003236
3237 private:
3238 GTEST_DISALLOW_ASSIGN_(DerivedStruct);
shiqiane35fdd92008-12-10 05:08:54 +00003239};
3240
3241// Tests that Field(&Foo::field, ...) works when field is non-const.
3242TEST(FieldTest, WorksForNonConstField) {
3243 Matcher<AStruct> m = Field(&AStruct::x, Ge(0));
3244
3245 AStruct a;
3246 EXPECT_TRUE(m.Matches(a));
3247 a.x = -1;
3248 EXPECT_FALSE(m.Matches(a));
3249}
3250
3251// Tests that Field(&Foo::field, ...) works when field is const.
3252TEST(FieldTest, WorksForConstField) {
3253 AStruct a;
3254
3255 Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0));
3256 EXPECT_TRUE(m.Matches(a));
3257 m = Field(&AStruct::y, Le(0.0));
3258 EXPECT_FALSE(m.Matches(a));
3259}
3260
3261// Tests that Field(&Foo::field, ...) works when field is not copyable.
3262TEST(FieldTest, WorksForUncopyableField) {
3263 AStruct a;
3264
3265 Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive));
3266 EXPECT_TRUE(m.Matches(a));
3267 m = Field(&AStruct::z, Not(Truly(ValueIsPositive)));
3268 EXPECT_FALSE(m.Matches(a));
3269}
3270
3271// Tests that Field(&Foo::field, ...) works when field is a pointer.
3272TEST(FieldTest, WorksForPointerField) {
3273 // Matching against NULL.
3274 Matcher<AStruct> m = Field(&AStruct::p, static_cast<const char*>(NULL));
3275 AStruct a;
3276 EXPECT_TRUE(m.Matches(a));
3277 a.p = "hi";
3278 EXPECT_FALSE(m.Matches(a));
3279
3280 // Matching a pointer that is not NULL.
3281 m = Field(&AStruct::p, StartsWith("hi"));
3282 a.p = "hill";
3283 EXPECT_TRUE(m.Matches(a));
3284 a.p = "hole";
3285 EXPECT_FALSE(m.Matches(a));
3286}
3287
3288// Tests that Field() works when the object is passed by reference.
3289TEST(FieldTest, WorksForByRefArgument) {
3290 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
3291
3292 AStruct a;
3293 EXPECT_TRUE(m.Matches(a));
3294 a.x = -1;
3295 EXPECT_FALSE(m.Matches(a));
3296}
3297
3298// Tests that Field(&Foo::field, ...) works when the argument's type
3299// is a sub-type of Foo.
3300TEST(FieldTest, WorksForArgumentOfSubType) {
3301 // Note that the matcher expects DerivedStruct but we say AStruct
3302 // inside Field().
3303 Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0));
3304
3305 DerivedStruct d;
3306 EXPECT_TRUE(m.Matches(d));
3307 d.x = -1;
3308 EXPECT_FALSE(m.Matches(d));
3309}
3310
3311// Tests that Field(&Foo::field, m) works when field's type and m's
3312// argument type are compatible but not the same.
3313TEST(FieldTest, WorksForCompatibleMatcherType) {
3314 // The field is an int, but the inner matcher expects a signed char.
3315 Matcher<const AStruct&> m = Field(&AStruct::x,
3316 Matcher<signed char>(Ge(0)));
3317
3318 AStruct a;
3319 EXPECT_TRUE(m.Matches(a));
3320 a.x = -1;
3321 EXPECT_FALSE(m.Matches(a));
3322}
3323
3324// Tests that Field() can describe itself.
3325TEST(FieldTest, CanDescribeSelf) {
3326 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
3327
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003328 EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
3329 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003330}
3331
3332// Tests that Field() can explain the match result.
3333TEST(FieldTest, CanExplainMatchResult) {
3334 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
3335
3336 AStruct a;
3337 a.x = 1;
zhanyong.wan736baa82010-09-27 17:44:16 +00003338 EXPECT_EQ("whose given field is 1" + OfType("int"), Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003339
3340 m = Field(&AStruct::x, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003341 EXPECT_EQ(
3342 "whose given field is 1" + OfType("int") + ", which is 1 more than 0",
3343 Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003344}
3345
3346// Tests that Field() works when the argument is a pointer to const.
3347TEST(FieldForPointerTest, WorksForPointerToConst) {
3348 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
3349
3350 AStruct a;
3351 EXPECT_TRUE(m.Matches(&a));
3352 a.x = -1;
3353 EXPECT_FALSE(m.Matches(&a));
3354}
3355
3356// Tests that Field() works when the argument is a pointer to non-const.
3357TEST(FieldForPointerTest, WorksForPointerToNonConst) {
3358 Matcher<AStruct*> m = Field(&AStruct::x, Ge(0));
3359
3360 AStruct a;
3361 EXPECT_TRUE(m.Matches(&a));
3362 a.x = -1;
3363 EXPECT_FALSE(m.Matches(&a));
3364}
3365
zhanyong.wan6953a722010-01-13 05:15:07 +00003366// Tests that Field() works when the argument is a reference to a const pointer.
3367TEST(FieldForPointerTest, WorksForReferenceToConstPointer) {
3368 Matcher<AStruct* const&> m = Field(&AStruct::x, Ge(0));
3369
3370 AStruct a;
3371 EXPECT_TRUE(m.Matches(&a));
3372 a.x = -1;
3373 EXPECT_FALSE(m.Matches(&a));
3374}
3375
shiqiane35fdd92008-12-10 05:08:54 +00003376// Tests that Field() does not match the NULL pointer.
3377TEST(FieldForPointerTest, DoesNotMatchNull) {
3378 Matcher<const AStruct*> m = Field(&AStruct::x, _);
3379 EXPECT_FALSE(m.Matches(NULL));
3380}
3381
3382// Tests that Field(&Foo::field, ...) works when the argument's type
3383// is a sub-type of const Foo*.
3384TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
3385 // Note that the matcher expects DerivedStruct but we say AStruct
3386 // inside Field().
3387 Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0));
3388
3389 DerivedStruct d;
3390 EXPECT_TRUE(m.Matches(&d));
3391 d.x = -1;
3392 EXPECT_FALSE(m.Matches(&d));
3393}
3394
3395// Tests that Field() can describe itself when used to match a pointer.
3396TEST(FieldForPointerTest, CanDescribeSelf) {
3397 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
3398
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003399 EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
3400 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003401}
3402
3403// Tests that Field() can explain the result of matching a pointer.
3404TEST(FieldForPointerTest, CanExplainMatchResult) {
3405 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
3406
3407 AStruct a;
3408 a.x = 1;
3409 EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(NULL)));
zhanyong.wan736baa82010-09-27 17:44:16 +00003410 EXPECT_EQ("which points to an object whose given field is 1" + OfType("int"),
3411 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003412
3413 m = Field(&AStruct::x, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003414 EXPECT_EQ("which points to an object whose given field is 1" + OfType("int") +
3415 ", which is 1 more than 0", Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003416}
3417
3418// A user-defined class for testing Property().
3419class AClass {
3420 public:
3421 AClass() : n_(0) {}
3422
3423 // A getter that returns a non-reference.
3424 int n() const { return n_; }
3425
3426 void set_n(int new_n) { n_ = new_n; }
3427
3428 // A getter that returns a reference to const.
3429 const string& s() const { return s_; }
3430
3431 void set_s(const string& new_s) { s_ = new_s; }
3432
3433 // A getter that returns a reference to non-const.
3434 double& x() const { return x_; }
3435 private:
3436 int n_;
3437 string s_;
3438
3439 static double x_;
3440};
3441
3442double AClass::x_ = 0.0;
3443
3444// A derived class for testing Property().
3445class DerivedClass : public AClass {
3446 private:
3447 int k_;
3448};
3449
3450// Tests that Property(&Foo::property, ...) works when property()
3451// returns a non-reference.
3452TEST(PropertyTest, WorksForNonReferenceProperty) {
3453 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3454
3455 AClass a;
3456 a.set_n(1);
3457 EXPECT_TRUE(m.Matches(a));
3458
3459 a.set_n(-1);
3460 EXPECT_FALSE(m.Matches(a));
3461}
3462
3463// Tests that Property(&Foo::property, ...) works when property()
3464// returns a reference to const.
3465TEST(PropertyTest, WorksForReferenceToConstProperty) {
3466 Matcher<const AClass&> m = Property(&AClass::s, StartsWith("hi"));
3467
3468 AClass a;
3469 a.set_s("hill");
3470 EXPECT_TRUE(m.Matches(a));
3471
3472 a.set_s("hole");
3473 EXPECT_FALSE(m.Matches(a));
3474}
3475
3476// Tests that Property(&Foo::property, ...) works when property()
3477// returns a reference to non-const.
3478TEST(PropertyTest, WorksForReferenceToNonConstProperty) {
3479 double x = 0.0;
3480 AClass a;
3481
3482 Matcher<const AClass&> m = Property(&AClass::x, Ref(x));
3483 EXPECT_FALSE(m.Matches(a));
3484
3485 m = Property(&AClass::x, Not(Ref(x)));
3486 EXPECT_TRUE(m.Matches(a));
3487}
3488
3489// Tests that Property(&Foo::property, ...) works when the argument is
3490// passed by value.
3491TEST(PropertyTest, WorksForByValueArgument) {
3492 Matcher<AClass> m = Property(&AClass::s, StartsWith("hi"));
3493
3494 AClass a;
3495 a.set_s("hill");
3496 EXPECT_TRUE(m.Matches(a));
3497
3498 a.set_s("hole");
3499 EXPECT_FALSE(m.Matches(a));
3500}
3501
3502// Tests that Property(&Foo::property, ...) works when the argument's
3503// type is a sub-type of Foo.
3504TEST(PropertyTest, WorksForArgumentOfSubType) {
3505 // The matcher expects a DerivedClass, but inside the Property() we
3506 // say AClass.
3507 Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0));
3508
3509 DerivedClass d;
3510 d.set_n(1);
3511 EXPECT_TRUE(m.Matches(d));
3512
3513 d.set_n(-1);
3514 EXPECT_FALSE(m.Matches(d));
3515}
3516
3517// Tests that Property(&Foo::property, m) works when property()'s type
3518// and m's argument type are compatible but different.
3519TEST(PropertyTest, WorksForCompatibleMatcherType) {
3520 // n() returns an int but the inner matcher expects a signed char.
3521 Matcher<const AClass&> m = Property(&AClass::n,
3522 Matcher<signed char>(Ge(0)));
3523
3524 AClass a;
3525 EXPECT_TRUE(m.Matches(a));
3526 a.set_n(-1);
3527 EXPECT_FALSE(m.Matches(a));
3528}
3529
3530// Tests that Property() can describe itself.
3531TEST(PropertyTest, CanDescribeSelf) {
3532 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3533
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003534 EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
3535 EXPECT_EQ("is an object whose given property isn't >= 0",
3536 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003537}
3538
3539// Tests that Property() can explain the match result.
3540TEST(PropertyTest, CanExplainMatchResult) {
3541 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3542
3543 AClass a;
3544 a.set_n(1);
zhanyong.wan736baa82010-09-27 17:44:16 +00003545 EXPECT_EQ("whose given property is 1" + OfType("int"), Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003546
3547 m = Property(&AClass::n, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003548 EXPECT_EQ(
3549 "whose given property is 1" + OfType("int") + ", which is 1 more than 0",
3550 Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003551}
3552
3553// Tests that Property() works when the argument is a pointer to const.
3554TEST(PropertyForPointerTest, WorksForPointerToConst) {
3555 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3556
3557 AClass a;
3558 a.set_n(1);
3559 EXPECT_TRUE(m.Matches(&a));
3560
3561 a.set_n(-1);
3562 EXPECT_FALSE(m.Matches(&a));
3563}
3564
3565// Tests that Property() works when the argument is a pointer to non-const.
3566TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
3567 Matcher<AClass*> m = Property(&AClass::s, StartsWith("hi"));
3568
3569 AClass a;
3570 a.set_s("hill");
3571 EXPECT_TRUE(m.Matches(&a));
3572
3573 a.set_s("hole");
3574 EXPECT_FALSE(m.Matches(&a));
3575}
3576
zhanyong.wan6953a722010-01-13 05:15:07 +00003577// Tests that Property() works when the argument is a reference to a
3578// const pointer.
3579TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) {
3580 Matcher<AClass* const&> m = Property(&AClass::s, StartsWith("hi"));
3581
3582 AClass a;
3583 a.set_s("hill");
3584 EXPECT_TRUE(m.Matches(&a));
3585
3586 a.set_s("hole");
3587 EXPECT_FALSE(m.Matches(&a));
3588}
3589
shiqiane35fdd92008-12-10 05:08:54 +00003590// Tests that Property() does not match the NULL pointer.
3591TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
3592 Matcher<const AClass*> m = Property(&AClass::x, _);
3593 EXPECT_FALSE(m.Matches(NULL));
3594}
3595
3596// Tests that Property(&Foo::property, ...) works when the argument's
3597// type is a sub-type of const Foo*.
3598TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
3599 // The matcher expects a DerivedClass, but inside the Property() we
3600 // say AClass.
3601 Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0));
3602
3603 DerivedClass d;
3604 d.set_n(1);
3605 EXPECT_TRUE(m.Matches(&d));
3606
3607 d.set_n(-1);
3608 EXPECT_FALSE(m.Matches(&d));
3609}
3610
3611// Tests that Property() can describe itself when used to match a pointer.
3612TEST(PropertyForPointerTest, CanDescribeSelf) {
3613 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3614
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003615 EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
3616 EXPECT_EQ("is an object whose given property isn't >= 0",
3617 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003618}
3619
3620// Tests that Property() can explain the result of matching a pointer.
3621TEST(PropertyForPointerTest, CanExplainMatchResult) {
3622 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3623
3624 AClass a;
3625 a.set_n(1);
3626 EXPECT_EQ("", Explain(m, static_cast<const AClass*>(NULL)));
zhanyong.wan736baa82010-09-27 17:44:16 +00003627 EXPECT_EQ(
3628 "which points to an object whose given property is 1" + OfType("int"),
3629 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003630
3631 m = Property(&AClass::n, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003632 EXPECT_EQ("which points to an object whose given property is 1" +
3633 OfType("int") + ", which is 1 more than 0",
3634 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003635}
3636
3637// Tests ResultOf.
3638
3639// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3640// function pointer.
3641string IntToStringFunction(int input) { return input == 1 ? "foo" : "bar"; }
3642
3643TEST(ResultOfTest, WorksForFunctionPointers) {
3644 Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(string("foo")));
3645
3646 EXPECT_TRUE(matcher.Matches(1));
3647 EXPECT_FALSE(matcher.Matches(2));
3648}
3649
3650// Tests that ResultOf() can describe itself.
3651TEST(ResultOfTest, CanDescribeItself) {
3652 Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo"));
3653
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003654 EXPECT_EQ("is mapped by the given callable to a value that "
3655 "is equal to \"foo\"", Describe(matcher));
3656 EXPECT_EQ("is mapped by the given callable to a value that "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003657 "isn't equal to \"foo\"", DescribeNegation(matcher));
shiqiane35fdd92008-12-10 05:08:54 +00003658}
3659
3660// Tests that ResultOf() can explain the match result.
3661int IntFunction(int input) { return input == 42 ? 80 : 90; }
3662
3663TEST(ResultOfTest, CanExplainMatchResult) {
3664 Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
zhanyong.wan736baa82010-09-27 17:44:16 +00003665 EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int"),
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003666 Explain(matcher, 36));
shiqiane35fdd92008-12-10 05:08:54 +00003667
3668 matcher = ResultOf(&IntFunction, GreaterThan(85));
zhanyong.wan736baa82010-09-27 17:44:16 +00003669 EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int") +
3670 ", which is 5 more than 85", Explain(matcher, 36));
shiqiane35fdd92008-12-10 05:08:54 +00003671}
3672
3673// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3674// returns a non-reference.
3675TEST(ResultOfTest, WorksForNonReferenceResults) {
3676 Matcher<int> matcher = ResultOf(&IntFunction, Eq(80));
3677
3678 EXPECT_TRUE(matcher.Matches(42));
3679 EXPECT_FALSE(matcher.Matches(36));
3680}
3681
3682// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3683// returns a reference to non-const.
zhanyong.wan736baa82010-09-27 17:44:16 +00003684double& DoubleFunction(double& input) { return input; } // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00003685
zhanyong.wan736baa82010-09-27 17:44:16 +00003686Uncopyable& RefUncopyableFunction(Uncopyable& obj) { // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00003687 return obj;
3688}
3689
3690TEST(ResultOfTest, WorksForReferenceToNonConstResults) {
3691 double x = 3.14;
3692 double x2 = x;
3693 Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x));
3694
3695 EXPECT_TRUE(matcher.Matches(x));
3696 EXPECT_FALSE(matcher.Matches(x2));
3697
3698 // Test that ResultOf works with uncopyable objects
3699 Uncopyable obj(0);
3700 Uncopyable obj2(0);
3701 Matcher<Uncopyable&> matcher2 =
3702 ResultOf(&RefUncopyableFunction, Ref(obj));
3703
3704 EXPECT_TRUE(matcher2.Matches(obj));
3705 EXPECT_FALSE(matcher2.Matches(obj2));
3706}
3707
3708// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3709// returns a reference to const.
3710const string& StringFunction(const string& input) { return input; }
3711
3712TEST(ResultOfTest, WorksForReferenceToConstResults) {
3713 string s = "foo";
3714 string s2 = s;
3715 Matcher<const string&> matcher = ResultOf(&StringFunction, Ref(s));
3716
3717 EXPECT_TRUE(matcher.Matches(s));
3718 EXPECT_FALSE(matcher.Matches(s2));
3719}
3720
3721// Tests that ResultOf(f, m) works when f(x) and m's
3722// argument types are compatible but different.
3723TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {
3724 // IntFunction() returns int but the inner matcher expects a signed char.
3725 Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85)));
3726
3727 EXPECT_TRUE(matcher.Matches(36));
3728 EXPECT_FALSE(matcher.Matches(42));
3729}
3730
shiqiane35fdd92008-12-10 05:08:54 +00003731// Tests that the program aborts when ResultOf is passed
3732// a NULL function pointer.
3733TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +00003734 EXPECT_DEATH_IF_SUPPORTED(
zhanyong.wan736baa82010-09-27 17:44:16 +00003735 ResultOf(static_cast<string(*)(int dummy)>(NULL), Eq(string("foo"))),
shiqiane35fdd92008-12-10 05:08:54 +00003736 "NULL function pointer is passed into ResultOf\\(\\)\\.");
3737}
shiqiane35fdd92008-12-10 05:08:54 +00003738
3739// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3740// function reference.
3741TEST(ResultOfTest, WorksForFunctionReferences) {
3742 Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq("foo"));
3743 EXPECT_TRUE(matcher.Matches(1));
3744 EXPECT_FALSE(matcher.Matches(2));
3745}
3746
3747// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3748// function object.
3749struct Functor : public ::std::unary_function<int, string> {
3750 result_type operator()(argument_type input) const {
3751 return IntToStringFunction(input);
3752 }
3753};
3754
3755TEST(ResultOfTest, WorksForFunctors) {
3756 Matcher<int> matcher = ResultOf(Functor(), Eq(string("foo")));
3757
3758 EXPECT_TRUE(matcher.Matches(1));
3759 EXPECT_FALSE(matcher.Matches(2));
3760}
3761
3762// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3763// functor with more then one operator() defined. ResultOf() must work
3764// for each defined operator().
3765struct PolymorphicFunctor {
3766 typedef int result_type;
3767 int operator()(int n) { return n; }
3768 int operator()(const char* s) { return static_cast<int>(strlen(s)); }
3769};
3770
3771TEST(ResultOfTest, WorksForPolymorphicFunctors) {
3772 Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5));
3773
3774 EXPECT_TRUE(matcher_int.Matches(10));
3775 EXPECT_FALSE(matcher_int.Matches(2));
3776
3777 Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5));
3778
3779 EXPECT_TRUE(matcher_string.Matches("long string"));
3780 EXPECT_FALSE(matcher_string.Matches("shrt"));
3781}
3782
3783const int* ReferencingFunction(const int& n) { return &n; }
3784
3785struct ReferencingFunctor {
3786 typedef const int* result_type;
3787 result_type operator()(const int& n) { return &n; }
3788};
3789
3790TEST(ResultOfTest, WorksForReferencingCallables) {
3791 const int n = 1;
3792 const int n2 = 1;
3793 Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n));
3794 EXPECT_TRUE(matcher2.Matches(n));
3795 EXPECT_FALSE(matcher2.Matches(n2));
3796
3797 Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n));
3798 EXPECT_TRUE(matcher3.Matches(n));
3799 EXPECT_FALSE(matcher3.Matches(n2));
3800}
3801
shiqiane35fdd92008-12-10 05:08:54 +00003802class DivisibleByImpl {
3803 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003804 explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {}
shiqiane35fdd92008-12-10 05:08:54 +00003805
zhanyong.wandb22c222010-01-28 21:52:29 +00003806 // For testing using ExplainMatchResultTo() with polymorphic matchers.
shiqiane35fdd92008-12-10 05:08:54 +00003807 template <typename T>
zhanyong.wandb22c222010-01-28 21:52:29 +00003808 bool MatchAndExplain(const T& n, MatchResultListener* listener) const {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003809 *listener << "which is " << (n % divider_) << " modulo "
zhanyong.wandb22c222010-01-28 21:52:29 +00003810 << divider_;
shiqiane35fdd92008-12-10 05:08:54 +00003811 return (n % divider_) == 0;
3812 }
3813
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003814 void DescribeTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +00003815 *os << "is divisible by " << divider_;
3816 }
3817
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003818 void DescribeNegationTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +00003819 *os << "is not divisible by " << divider_;
3820 }
3821
zhanyong.wan32de5f52009-12-23 00:13:23 +00003822 void set_divider(int a_divider) { divider_ = a_divider; }
shiqiane35fdd92008-12-10 05:08:54 +00003823 int divider() const { return divider_; }
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003824
shiqiane35fdd92008-12-10 05:08:54 +00003825 private:
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003826 int divider_;
shiqiane35fdd92008-12-10 05:08:54 +00003827};
3828
shiqiane35fdd92008-12-10 05:08:54 +00003829PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) {
3830 return MakePolymorphicMatcher(DivisibleByImpl(n));
3831}
3832
3833// Tests that when AllOf() fails, only the first failing matcher is
3834// asked to explain why.
3835TEST(ExplainMatchResultTest, AllOf_False_False) {
3836 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003837 EXPECT_EQ("which is 1 modulo 4", Explain(m, 5));
shiqiane35fdd92008-12-10 05:08:54 +00003838}
3839
3840// Tests that when AllOf() fails, only the first failing matcher is
3841// asked to explain why.
3842TEST(ExplainMatchResultTest, AllOf_False_True) {
3843 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003844 EXPECT_EQ("which is 2 modulo 4", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003845}
3846
3847// Tests that when AllOf() fails, only the first failing matcher is
3848// asked to explain why.
3849TEST(ExplainMatchResultTest, AllOf_True_False) {
3850 const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003851 EXPECT_EQ("which is 2 modulo 3", Explain(m, 5));
shiqiane35fdd92008-12-10 05:08:54 +00003852}
3853
3854// Tests that when AllOf() succeeds, all matchers are asked to explain
3855// why.
3856TEST(ExplainMatchResultTest, AllOf_True_True) {
3857 const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003858 EXPECT_EQ("which is 0 modulo 2, and which is 0 modulo 3", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003859}
3860
3861TEST(ExplainMatchResultTest, AllOf_True_True_2) {
3862 const Matcher<int> m = AllOf(Ge(2), Le(3));
3863 EXPECT_EQ("", Explain(m, 2));
3864}
3865
3866TEST(ExplainmatcherResultTest, MonomorphicMatcher) {
3867 const Matcher<int> m = GreaterThan(5);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003868 EXPECT_EQ("which is 1 more than 5", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003869}
3870
3871// The following two tests verify that values without a public copy
3872// ctor can be used as arguments to matchers like Eq(), Ge(), and etc
3873// with the help of ByRef().
3874
3875class NotCopyable {
3876 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003877 explicit NotCopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00003878
3879 int value() const { return value_; }
3880
3881 bool operator==(const NotCopyable& rhs) const {
3882 return value() == rhs.value();
3883 }
3884
3885 bool operator>=(const NotCopyable& rhs) const {
3886 return value() >= rhs.value();
3887 }
3888 private:
3889 int value_;
3890
3891 GTEST_DISALLOW_COPY_AND_ASSIGN_(NotCopyable);
3892};
3893
3894TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) {
3895 const NotCopyable const_value1(1);
3896 const Matcher<const NotCopyable&> m = Eq(ByRef(const_value1));
3897
3898 const NotCopyable n1(1), n2(2);
3899 EXPECT_TRUE(m.Matches(n1));
3900 EXPECT_FALSE(m.Matches(n2));
3901}
3902
3903TEST(ByRefTest, AllowsNotCopyableValueInMatchers) {
3904 NotCopyable value2(2);
3905 const Matcher<NotCopyable&> m = Ge(ByRef(value2));
3906
3907 NotCopyable n1(1), n2(2);
3908 EXPECT_FALSE(m.Matches(n1));
3909 EXPECT_TRUE(m.Matches(n2));
3910}
3911
zhanyong.wan320814a2013-03-01 00:20:30 +00003912TEST(IsEmptyTest, ImplementsIsEmpty) {
3913 vector<int> container;
3914 EXPECT_THAT(container, IsEmpty());
3915 container.push_back(0);
3916 EXPECT_THAT(container, Not(IsEmpty()));
3917 container.push_back(1);
3918 EXPECT_THAT(container, Not(IsEmpty()));
3919}
3920
3921TEST(IsEmptyTest, WorksWithString) {
3922 string text;
3923 EXPECT_THAT(text, IsEmpty());
3924 text = "foo";
3925 EXPECT_THAT(text, Not(IsEmpty()));
3926 text = string("\0", 1);
3927 EXPECT_THAT(text, Not(IsEmpty()));
3928}
3929
3930TEST(IsEmptyTest, CanDescribeSelf) {
3931 Matcher<vector<int> > m = IsEmpty();
3932 EXPECT_EQ("is empty", Describe(m));
3933 EXPECT_EQ("isn't empty", DescribeNegation(m));
3934}
3935
3936TEST(IsEmptyTest, ExplainsResult) {
3937 Matcher<vector<int> > m = IsEmpty();
3938 vector<int> container;
3939 EXPECT_EQ("", Explain(m, container));
3940 container.push_back(0);
3941 EXPECT_EQ("whose size is 1", Explain(m, container));
3942}
3943
zhanyong.wana31d9ce2013-03-01 01:50:17 +00003944TEST(SizeIsTest, ImplementsSizeIs) {
3945 vector<int> container;
3946 EXPECT_THAT(container, SizeIs(0));
3947 EXPECT_THAT(container, Not(SizeIs(1)));
3948 container.push_back(0);
3949 EXPECT_THAT(container, Not(SizeIs(0)));
3950 EXPECT_THAT(container, SizeIs(1));
3951 container.push_back(0);
3952 EXPECT_THAT(container, Not(SizeIs(0)));
3953 EXPECT_THAT(container, SizeIs(2));
3954}
3955
3956TEST(SizeIsTest, WorksWithMap) {
3957 map<string, int> container;
3958 EXPECT_THAT(container, SizeIs(0));
3959 EXPECT_THAT(container, Not(SizeIs(1)));
3960 container.insert(make_pair("foo", 1));
3961 EXPECT_THAT(container, Not(SizeIs(0)));
3962 EXPECT_THAT(container, SizeIs(1));
3963 container.insert(make_pair("bar", 2));
3964 EXPECT_THAT(container, Not(SizeIs(0)));
3965 EXPECT_THAT(container, SizeIs(2));
3966}
3967
3968TEST(SizeIsTest, WorksWithReferences) {
3969 vector<int> container;
3970 Matcher<const vector<int>&> m = SizeIs(1);
3971 EXPECT_THAT(container, Not(m));
3972 container.push_back(0);
3973 EXPECT_THAT(container, m);
3974}
3975
3976TEST(SizeIsTest, CanDescribeSelf) {
3977 Matcher<vector<int> > m = SizeIs(2);
3978 EXPECT_EQ("size is equal to 2", Describe(m));
3979 EXPECT_EQ("size isn't equal to 2", DescribeNegation(m));
3980}
3981
3982TEST(SizeIsTest, ExplainsResult) {
3983 Matcher<vector<int> > m1 = SizeIs(2);
3984 Matcher<vector<int> > m2 = SizeIs(Lt(2u));
3985 Matcher<vector<int> > m3 = SizeIs(AnyOf(0, 3));
3986 Matcher<vector<int> > m4 = SizeIs(GreaterThan(1));
3987 vector<int> container;
3988 EXPECT_EQ("whose size 0 doesn't match", Explain(m1, container));
3989 EXPECT_EQ("whose size 0 matches", Explain(m2, container));
3990 EXPECT_EQ("whose size 0 matches", Explain(m3, container));
3991 EXPECT_EQ("whose size 0 doesn't match, which is 1 less than 1",
3992 Explain(m4, container));
3993 container.push_back(0);
3994 container.push_back(0);
3995 EXPECT_EQ("whose size 2 matches", Explain(m1, container));
3996 EXPECT_EQ("whose size 2 doesn't match", Explain(m2, container));
3997 EXPECT_EQ("whose size 2 doesn't match", Explain(m3, container));
3998 EXPECT_EQ("whose size 2 matches, which is 1 more than 1",
3999 Explain(m4, container));
4000}
4001
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00004002#if GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00004003// Tests ContainerEq with different container types, and
4004// different element types.
4005
4006template <typename T>
zhanyong.wanb8243162009-06-04 05:48:20 +00004007class ContainerEqTest : public testing::Test {};
zhanyong.wan6a896b52009-01-16 01:13:50 +00004008
4009typedef testing::Types<
zhanyong.wanab5b77c2010-05-17 19:32:48 +00004010 set<int>,
4011 vector<size_t>,
4012 multiset<size_t>,
4013 list<int> >
zhanyong.wan6a896b52009-01-16 01:13:50 +00004014 ContainerEqTestTypes;
4015
4016TYPED_TEST_CASE(ContainerEqTest, ContainerEqTestTypes);
4017
4018// Tests that the filled container is equal to itself.
4019TYPED_TEST(ContainerEqTest, EqualsSelf) {
4020 static const int vals[] = {1, 1, 2, 3, 5, 8};
4021 TypeParam my_set(vals, vals + 6);
4022 const Matcher<TypeParam> m = ContainerEq(my_set);
4023 EXPECT_TRUE(m.Matches(my_set));
4024 EXPECT_EQ("", Explain(m, my_set));
4025}
4026
4027// Tests that missing values are reported.
4028TYPED_TEST(ContainerEqTest, ValueMissing) {
4029 static const int vals[] = {1, 1, 2, 3, 5, 8};
4030 static const int test_vals[] = {2, 1, 8, 5};
4031 TypeParam my_set(vals, vals + 6);
4032 TypeParam test_set(test_vals, test_vals + 4);
4033 const Matcher<TypeParam> m = ContainerEq(my_set);
4034 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00004035 EXPECT_EQ("which doesn't have these expected elements: 3",
4036 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00004037}
4038
4039// Tests that added values are reported.
4040TYPED_TEST(ContainerEqTest, ValueAdded) {
4041 static const int vals[] = {1, 1, 2, 3, 5, 8};
4042 static const int test_vals[] = {1, 2, 3, 5, 8, 46};
4043 TypeParam my_set(vals, vals + 6);
4044 TypeParam test_set(test_vals, test_vals + 6);
4045 const Matcher<const TypeParam&> m = ContainerEq(my_set);
4046 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00004047 EXPECT_EQ("which has these unexpected elements: 46", Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00004048}
4049
4050// Tests that added and missing values are reported together.
4051TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) {
4052 static const int vals[] = {1, 1, 2, 3, 5, 8};
4053 static const int test_vals[] = {1, 2, 3, 8, 46};
4054 TypeParam my_set(vals, vals + 6);
4055 TypeParam test_set(test_vals, test_vals + 5);
4056 const Matcher<TypeParam> m = ContainerEq(my_set);
4057 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00004058 EXPECT_EQ("which has these unexpected elements: 46,\n"
4059 "and doesn't have these expected elements: 5",
4060 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00004061}
4062
4063// Tests duplicated value -- expect no explanation.
4064TYPED_TEST(ContainerEqTest, DuplicateDifference) {
4065 static const int vals[] = {1, 1, 2, 3, 5, 8};
4066 static const int test_vals[] = {1, 2, 3, 5, 8};
4067 TypeParam my_set(vals, vals + 6);
4068 TypeParam test_set(test_vals, test_vals + 5);
4069 const Matcher<const TypeParam&> m = ContainerEq(my_set);
4070 // Depending on the container, match may be true or false
4071 // But in any case there should be no explanation.
4072 EXPECT_EQ("", Explain(m, test_set));
4073}
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00004074#endif // GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00004075
4076// Tests that mutliple missing values are reported.
4077// Using just vector here, so order is predicatble.
4078TEST(ContainerEqExtraTest, MultipleValuesMissing) {
4079 static const int vals[] = {1, 1, 2, 3, 5, 8};
4080 static const int test_vals[] = {2, 1, 5};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00004081 vector<int> my_set(vals, vals + 6);
4082 vector<int> test_set(test_vals, test_vals + 3);
4083 const Matcher<vector<int> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00004084 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00004085 EXPECT_EQ("which doesn't have these expected elements: 3, 8",
4086 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00004087}
4088
4089// Tests that added values are reported.
4090// Using just vector here, so order is predicatble.
4091TEST(ContainerEqExtraTest, MultipleValuesAdded) {
4092 static const int vals[] = {1, 1, 2, 3, 5, 8};
4093 static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00004094 list<size_t> my_set(vals, vals + 6);
4095 list<size_t> test_set(test_vals, test_vals + 7);
4096 const Matcher<const list<size_t>&> m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00004097 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00004098 EXPECT_EQ("which has these unexpected elements: 92, 46",
4099 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00004100}
4101
4102// Tests that added and missing values are reported together.
4103TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) {
4104 static const int vals[] = {1, 1, 2, 3, 5, 8};
4105 static const int test_vals[] = {1, 2, 3, 92, 46};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00004106 list<size_t> my_set(vals, vals + 6);
4107 list<size_t> test_set(test_vals, test_vals + 5);
4108 const Matcher<const list<size_t> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00004109 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00004110 EXPECT_EQ("which has these unexpected elements: 92, 46,\n"
4111 "and doesn't have these expected elements: 5, 8",
zhanyong.wan6a896b52009-01-16 01:13:50 +00004112 Explain(m, test_set));
4113}
4114
4115// Tests to see that duplicate elements are detected,
4116// but (as above) not reported in the explanation.
4117TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) {
4118 static const int vals[] = {1, 1, 2, 3, 5, 8};
4119 static const int test_vals[] = {1, 2, 3, 5, 8};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00004120 vector<int> my_set(vals, vals + 6);
4121 vector<int> test_set(test_vals, test_vals + 5);
4122 const Matcher<vector<int> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00004123 EXPECT_TRUE(m.Matches(my_set));
4124 EXPECT_FALSE(m.Matches(test_set));
4125 // There is nothing to report when both sets contain all the same values.
4126 EXPECT_EQ("", Explain(m, test_set));
4127}
4128
4129// Tests that ContainerEq works for non-trivial associative containers,
4130// like maps.
4131TEST(ContainerEqExtraTest, WorksForMaps) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00004132 map<int, std::string> my_map;
zhanyong.wan6a896b52009-01-16 01:13:50 +00004133 my_map[0] = "a";
4134 my_map[1] = "b";
4135
zhanyong.wanab5b77c2010-05-17 19:32:48 +00004136 map<int, std::string> test_map;
zhanyong.wan6a896b52009-01-16 01:13:50 +00004137 test_map[0] = "aa";
4138 test_map[1] = "b";
4139
zhanyong.wanab5b77c2010-05-17 19:32:48 +00004140 const Matcher<const map<int, std::string>&> m = ContainerEq(my_map);
zhanyong.wan6a896b52009-01-16 01:13:50 +00004141 EXPECT_TRUE(m.Matches(my_map));
4142 EXPECT_FALSE(m.Matches(test_map));
4143
zhanyong.wanb1c7f932010-03-24 17:35:11 +00004144 EXPECT_EQ("which has these unexpected elements: (0, \"aa\"),\n"
4145 "and doesn't have these expected elements: (0, \"a\")",
zhanyong.wan6a896b52009-01-16 01:13:50 +00004146 Explain(m, test_map));
4147}
4148
zhanyong.wanb8243162009-06-04 05:48:20 +00004149TEST(ContainerEqExtraTest, WorksForNativeArray) {
4150 int a1[] = { 1, 2, 3 };
4151 int a2[] = { 1, 2, 3 };
4152 int b[] = { 1, 2, 4 };
4153
4154 EXPECT_THAT(a1, ContainerEq(a2));
4155 EXPECT_THAT(a1, Not(ContainerEq(b)));
4156}
4157
4158TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) {
4159 const char a1[][3] = { "hi", "lo" };
4160 const char a2[][3] = { "hi", "lo" };
4161 const char b[][3] = { "lo", "hi" };
4162
4163 // Tests using ContainerEq() in the first dimension.
4164 EXPECT_THAT(a1, ContainerEq(a2));
4165 EXPECT_THAT(a1, Not(ContainerEq(b)));
4166
4167 // Tests using ContainerEq() in the second dimension.
4168 EXPECT_THAT(a1, ElementsAre(ContainerEq(a2[0]), ContainerEq(a2[1])));
4169 EXPECT_THAT(a1, ElementsAre(Not(ContainerEq(b[0])), ContainerEq(a2[1])));
4170}
4171
4172TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) {
4173 const int a1[] = { 1, 2, 3 };
4174 const int a2[] = { 1, 2, 3 };
4175 const int b[] = { 1, 2, 3, 4 };
4176
zhanyong.wan2661c682009-06-09 05:42:12 +00004177 const int* const p1 = a1;
4178 EXPECT_THAT(make_tuple(p1, 3), ContainerEq(a2));
4179 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(b)));
zhanyong.wanb8243162009-06-04 05:48:20 +00004180
4181 const int c[] = { 1, 3, 2 };
zhanyong.wan2661c682009-06-09 05:42:12 +00004182 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(c)));
zhanyong.wanb8243162009-06-04 05:48:20 +00004183}
4184
4185TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) {
4186 std::string a1[][3] = {
4187 { "hi", "hello", "ciao" },
4188 { "bye", "see you", "ciao" }
4189 };
4190
4191 std::string a2[][3] = {
4192 { "hi", "hello", "ciao" },
4193 { "bye", "see you", "ciao" }
4194 };
4195
4196 const Matcher<const std::string(&)[2][3]> m = ContainerEq(a2);
4197 EXPECT_THAT(a1, m);
4198
4199 a2[0][0] = "ha";
4200 EXPECT_THAT(a1, m);
4201}
4202
zhanyong.wan898725c2011-09-16 16:45:39 +00004203TEST(WhenSortedByTest, WorksForEmptyContainer) {
4204 const vector<int> numbers;
4205 EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre()));
4206 EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1))));
4207}
4208
4209TEST(WhenSortedByTest, WorksForNonEmptyContainer) {
4210 vector<unsigned> numbers;
4211 numbers.push_back(3);
4212 numbers.push_back(1);
4213 numbers.push_back(2);
4214 numbers.push_back(2);
4215 EXPECT_THAT(numbers, WhenSortedBy(greater<unsigned>(),
4216 ElementsAre(3, 2, 2, 1)));
4217 EXPECT_THAT(numbers, Not(WhenSortedBy(greater<unsigned>(),
4218 ElementsAre(1, 2, 2, 3))));
4219}
4220
4221TEST(WhenSortedByTest, WorksForNonVectorContainer) {
4222 list<string> words;
4223 words.push_back("say");
4224 words.push_back("hello");
4225 words.push_back("world");
4226 EXPECT_THAT(words, WhenSortedBy(less<string>(),
4227 ElementsAre("hello", "say", "world")));
4228 EXPECT_THAT(words, Not(WhenSortedBy(less<string>(),
4229 ElementsAre("say", "hello", "world"))));
4230}
4231
4232TEST(WhenSortedByTest, WorksForNativeArray) {
4233 const int numbers[] = { 1, 3, 2, 4 };
4234 const int sorted_numbers[] = { 1, 2, 3, 4 };
4235 EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre(1, 2, 3, 4)));
4236 EXPECT_THAT(numbers, WhenSortedBy(less<int>(),
4237 ElementsAreArray(sorted_numbers)));
4238 EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1, 3, 2, 4))));
4239}
4240
4241TEST(WhenSortedByTest, CanDescribeSelf) {
4242 const Matcher<vector<int> > m = WhenSortedBy(less<int>(), ElementsAre(1, 2));
4243 EXPECT_EQ("(when sorted) has 2 elements where\n"
4244 "element #0 is equal to 1,\n"
4245 "element #1 is equal to 2",
4246 Describe(m));
4247 EXPECT_EQ("(when sorted) doesn't have 2 elements, or\n"
4248 "element #0 isn't equal to 1, or\n"
4249 "element #1 isn't equal to 2",
4250 DescribeNegation(m));
4251}
4252
4253TEST(WhenSortedByTest, ExplainsMatchResult) {
4254 const int a[] = { 2, 1 };
4255 EXPECT_EQ("which is { 1, 2 } when sorted, whose element #0 doesn't match",
4256 Explain(WhenSortedBy(less<int>(), ElementsAre(2, 3)), a));
4257 EXPECT_EQ("which is { 1, 2 } when sorted",
4258 Explain(WhenSortedBy(less<int>(), ElementsAre(1, 2)), a));
4259}
4260
4261// WhenSorted() is a simple wrapper on WhenSortedBy(). Hence we don't
4262// need to test it as exhaustively as we test the latter.
4263
4264TEST(WhenSortedTest, WorksForEmptyContainer) {
4265 const vector<int> numbers;
4266 EXPECT_THAT(numbers, WhenSorted(ElementsAre()));
4267 EXPECT_THAT(numbers, Not(WhenSorted(ElementsAre(1))));
4268}
4269
4270TEST(WhenSortedTest, WorksForNonEmptyContainer) {
4271 list<string> words;
4272 words.push_back("3");
4273 words.push_back("1");
4274 words.push_back("2");
4275 words.push_back("2");
4276 EXPECT_THAT(words, WhenSorted(ElementsAre("1", "2", "2", "3")));
4277 EXPECT_THAT(words, Not(WhenSorted(ElementsAre("3", "1", "2", "2"))));
4278}
4279
zhanyong.wana9a59e02013-03-27 16:14:55 +00004280TEST(WhenSortedTest, WorksForMapTypes) {
4281 map<string, int> word_counts;
4282 word_counts["and"] = 1;
4283 word_counts["the"] = 1;
4284 word_counts["buffalo"] = 2;
4285 EXPECT_THAT(word_counts, WhenSorted(ElementsAre(
4286 Pair("and", 1), Pair("buffalo", 2), Pair("the", 1))));
4287 EXPECT_THAT(word_counts, Not(WhenSorted(ElementsAre(
4288 Pair("and", 1), Pair("the", 1), Pair("buffalo", 2)))));
4289}
4290
4291TEST(WhenSortedTest, WorksForMultiMapTypes) {
4292 multimap<int, int> ifib;
4293 ifib.insert(make_pair(8, 6));
4294 ifib.insert(make_pair(2, 3));
4295 ifib.insert(make_pair(1, 1));
4296 ifib.insert(make_pair(3, 4));
4297 ifib.insert(make_pair(1, 2));
4298 ifib.insert(make_pair(5, 5));
4299 EXPECT_THAT(ifib, WhenSorted(ElementsAre(Pair(1, 1),
4300 Pair(1, 2),
4301 Pair(2, 3),
4302 Pair(3, 4),
4303 Pair(5, 5),
4304 Pair(8, 6))));
4305 EXPECT_THAT(ifib, Not(WhenSorted(ElementsAre(Pair(8, 6),
4306 Pair(2, 3),
4307 Pair(1, 1),
4308 Pair(3, 4),
4309 Pair(1, 2),
4310 Pair(5, 5)))));
4311}
4312
4313TEST(WhenSortedTest, WorksForPolymorphicMatcher) {
4314 std::deque<int> d;
4315 d.push_back(2);
4316 d.push_back(1);
4317 EXPECT_THAT(d, WhenSorted(ElementsAre(1, 2)));
4318 EXPECT_THAT(d, Not(WhenSorted(ElementsAre(2, 1))));
4319}
4320
4321TEST(WhenSortedTest, WorksForVectorConstRefMatcher) {
4322 std::deque<int> d;
4323 d.push_back(2);
4324 d.push_back(1);
4325 Matcher<const std::vector<int>&> vector_match = ElementsAre(1, 2);
4326 EXPECT_THAT(d, WhenSorted(vector_match));
4327 Matcher<const std::vector<int>&> not_vector_match = ElementsAre(2, 1);
4328 EXPECT_THAT(d, Not(WhenSorted(not_vector_match)));
4329}
4330
4331// Deliberately bare pseudo-container.
4332// Offers only begin() and end() accessors, yielding InputIterator.
4333template <typename T>
4334class Streamlike {
4335 private:
4336 class ConstIter;
4337 public:
4338 typedef ConstIter const_iterator;
4339 typedef T value_type;
4340
4341 template <typename InIter>
4342 Streamlike(InIter first, InIter last) : remainder_(first, last) {}
4343
4344 const_iterator begin() const {
4345 return const_iterator(this, remainder_.begin());
4346 }
4347 const_iterator end() const {
4348 return const_iterator(this, remainder_.end());
4349 }
4350
4351 private:
4352 class ConstIter : public std::iterator<std::input_iterator_tag,
4353 value_type,
4354 ptrdiff_t,
4355 const value_type&,
4356 const value_type*> {
4357 public:
4358 ConstIter(const Streamlike* s,
4359 typename std::list<value_type>::iterator pos)
4360 : s_(s), pos_(pos) {}
4361
4362 const value_type& operator*() const { return *pos_; }
4363 const value_type* operator->() const { return &*pos_; }
4364 ConstIter& operator++() {
4365 s_->remainder_.erase(pos_++);
4366 return *this;
4367 }
4368
4369 // *iter++ is required to work (see std::istreambuf_iterator).
4370 // (void)iter++ is also required to work.
4371 class PostIncrProxy {
4372 public:
4373 explicit PostIncrProxy(const value_type& value) : value_(value) {}
4374 value_type operator*() const { return value_; }
4375 private:
4376 value_type value_;
4377 };
4378 PostIncrProxy operator++(int) {
4379 PostIncrProxy proxy(**this);
4380 ++(*this);
4381 return proxy;
4382 }
4383
4384 friend bool operator==(const ConstIter& a, const ConstIter& b) {
4385 return a.s_ == b.s_ && a.pos_ == b.pos_;
4386 }
4387 friend bool operator!=(const ConstIter& a, const ConstIter& b) {
4388 return !(a == b);
4389 }
4390
4391 private:
4392 const Streamlike* s_;
4393 typename std::list<value_type>::iterator pos_;
4394 };
4395
4396 friend std::ostream& operator<<(std::ostream& os, const Streamlike& s) {
4397 os << "[";
4398 typedef typename std::list<value_type>::const_iterator Iter;
4399 const char* sep = "";
4400 for (Iter it = s.remainder_.begin(); it != s.remainder_.end(); ++it) {
4401 os << sep << *it;
4402 sep = ",";
4403 }
4404 os << "]";
4405 return os;
4406 }
4407
4408 mutable std::list<value_type> remainder_; // modified by iteration
4409};
4410
4411TEST(StreamlikeTest, Iteration) {
4412 const int a[5] = { 2, 1, 4, 5, 3 };
4413 Streamlike<int> s(a, a + 5);
4414 Streamlike<int>::const_iterator it = s.begin();
4415 const int* ip = a;
4416 while (it != s.end()) {
4417 SCOPED_TRACE(ip - a);
4418 EXPECT_EQ(*ip++, *it++);
4419 }
4420}
4421
4422TEST(WhenSortedTest, WorksForStreamlike) {
4423 // Streamlike 'container' provides only minimal iterator support.
4424 // Its iterators are tagged with input_iterator_tag.
4425 const int a[5] = { 2, 1, 4, 5, 3 };
4426 Streamlike<int> s(a, a + 5);
4427 EXPECT_THAT(s, WhenSorted(ElementsAre(1, 2, 3, 4, 5)));
4428 EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3))));
4429}
4430
4431TEST(WhenSortedTest, WorksForVectorConstRefMatcherOnStreamlike) {
4432 const int a[5] = { 2, 1, 4, 5, 3 };
4433 Streamlike<int> s(a, a + 5);
4434 Matcher<const std::vector<int>&> vector_match = ElementsAre(1, 2, 3, 4, 5);
4435 EXPECT_THAT(s, WhenSorted(vector_match));
4436 EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3))));
4437}
4438
zhanyong.wan736baa82010-09-27 17:44:16 +00004439// Tests IsReadableTypeName().
4440
4441TEST(IsReadableTypeNameTest, ReturnsTrueForShortNames) {
4442 EXPECT_TRUE(IsReadableTypeName("int"));
4443 EXPECT_TRUE(IsReadableTypeName("const unsigned char*"));
4444 EXPECT_TRUE(IsReadableTypeName("MyMap<int, void*>"));
4445 EXPECT_TRUE(IsReadableTypeName("void (*)(int, bool)"));
4446}
4447
4448TEST(IsReadableTypeNameTest, ReturnsTrueForLongNonTemplateNonFunctionNames) {
4449 EXPECT_TRUE(IsReadableTypeName("my_long_namespace::MyClassName"));
4450 EXPECT_TRUE(IsReadableTypeName("int [5][6][7][8][9][10][11]"));
4451 EXPECT_TRUE(IsReadableTypeName("my_namespace::MyOuterClass::MyInnerClass"));
4452}
4453
4454TEST(IsReadableTypeNameTest, ReturnsFalseForLongTemplateNames) {
4455 EXPECT_FALSE(
4456 IsReadableTypeName("basic_string<char, std::char_traits<char> >"));
4457 EXPECT_FALSE(IsReadableTypeName("std::vector<int, std::alloc_traits<int> >"));
4458}
4459
4460TEST(IsReadableTypeNameTest, ReturnsFalseForLongFunctionTypeNames) {
4461 EXPECT_FALSE(IsReadableTypeName("void (&)(int, bool, char, float)"));
4462}
4463
zhanyong.wan4a5330d2009-02-19 00:36:44 +00004464// Tests JoinAsTuple().
4465
4466TEST(JoinAsTupleTest, JoinsEmptyTuple) {
4467 EXPECT_EQ("", JoinAsTuple(Strings()));
4468}
4469
4470TEST(JoinAsTupleTest, JoinsOneTuple) {
4471 const char* fields[] = { "1" };
4472 EXPECT_EQ("1", JoinAsTuple(Strings(fields, fields + 1)));
4473}
4474
4475TEST(JoinAsTupleTest, JoinsTwoTuple) {
4476 const char* fields[] = { "1", "a" };
4477 EXPECT_EQ("(1, a)", JoinAsTuple(Strings(fields, fields + 2)));
4478}
4479
4480TEST(JoinAsTupleTest, JoinsTenTuple) {
4481 const char* fields[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
4482 EXPECT_EQ("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)",
4483 JoinAsTuple(Strings(fields, fields + 10)));
4484}
4485
4486// Tests FormatMatcherDescription().
4487
4488TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) {
4489 EXPECT_EQ("is even",
zhanyong.wanb4140802010-06-08 22:53:57 +00004490 FormatMatcherDescription(false, "IsEven", Strings()));
4491 EXPECT_EQ("not (is even)",
4492 FormatMatcherDescription(true, "IsEven", Strings()));
zhanyong.wan4a5330d2009-02-19 00:36:44 +00004493
4494 const char* params[] = { "5" };
4495 EXPECT_EQ("equals 5",
zhanyong.wanb4140802010-06-08 22:53:57 +00004496 FormatMatcherDescription(false, "Equals",
zhanyong.wan4a5330d2009-02-19 00:36:44 +00004497 Strings(params, params + 1)));
4498
4499 const char* params2[] = { "5", "8" };
4500 EXPECT_EQ("is in range (5, 8)",
zhanyong.wanb4140802010-06-08 22:53:57 +00004501 FormatMatcherDescription(false, "IsInRange",
zhanyong.wan4a5330d2009-02-19 00:36:44 +00004502 Strings(params2, params2 + 2)));
4503}
4504
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00004505// Tests PolymorphicMatcher::mutable_impl().
4506TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {
4507 PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
4508 DivisibleByImpl& impl = m.mutable_impl();
4509 EXPECT_EQ(42, impl.divider());
4510
4511 impl.set_divider(0);
4512 EXPECT_EQ(0, m.mutable_impl().divider());
4513}
4514
4515// Tests PolymorphicMatcher::impl().
4516TEST(PolymorphicMatcherTest, CanAccessImpl) {
4517 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
4518 const DivisibleByImpl& impl = m.impl();
4519 EXPECT_EQ(42, impl.divider());
4520}
4521
zhanyong.wanb1c7f932010-03-24 17:35:11 +00004522TEST(MatcherTupleTest, ExplainsMatchFailure) {
4523 stringstream ss1;
4524 ExplainMatchFailureTupleTo(make_tuple(Matcher<char>(Eq('a')), GreaterThan(5)),
4525 make_tuple('a', 10), &ss1);
4526 EXPECT_EQ("", ss1.str()); // Successful match.
4527
4528 stringstream ss2;
4529 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
4530 make_tuple(2, 'b'), &ss2);
4531 EXPECT_EQ(" Expected arg #0: is > 5\n"
4532 " Actual: 2, which is 3 less than 5\n"
zhanyong.wand60c5f42010-07-21 22:21:07 +00004533 " Expected arg #1: is equal to 'a' (97, 0x61)\n"
4534 " Actual: 'b' (98, 0x62)\n",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00004535 ss2.str()); // Failed match where both arguments need explanation.
4536
4537 stringstream ss3;
4538 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
4539 make_tuple(2, 'a'), &ss3);
4540 EXPECT_EQ(" Expected arg #0: is > 5\n"
4541 " Actual: 2, which is 3 less than 5\n",
4542 ss3.str()); // Failed match where only one argument needs
4543 // explanation.
4544}
4545
zhanyong.wan33605ba2010-04-22 23:37:47 +00004546// Tests Each().
4547
4548TEST(EachTest, ExplainsMatchResultCorrectly) {
4549 set<int> a; // empty
4550
4551 Matcher<set<int> > m = Each(2);
4552 EXPECT_EQ("", Explain(m, a));
4553
zhanyong.wan736baa82010-09-27 17:44:16 +00004554 Matcher<const int(&)[1]> n = Each(1); // NOLINT
zhanyong.wan33605ba2010-04-22 23:37:47 +00004555
4556 const int b[1] = { 1 };
4557 EXPECT_EQ("", Explain(n, b));
4558
4559 n = Each(3);
4560 EXPECT_EQ("whose element #0 doesn't match", Explain(n, b));
4561
4562 a.insert(1);
4563 a.insert(2);
4564 a.insert(3);
4565 m = Each(GreaterThan(0));
4566 EXPECT_EQ("", Explain(m, a));
4567
4568 m = Each(GreaterThan(10));
4569 EXPECT_EQ("whose element #0 doesn't match, which is 9 less than 10",
4570 Explain(m, a));
4571}
4572
4573TEST(EachTest, DescribesItselfCorrectly) {
4574 Matcher<vector<int> > m = Each(1);
4575 EXPECT_EQ("only contains elements that is equal to 1", Describe(m));
4576
4577 Matcher<vector<int> > m2 = Not(m);
4578 EXPECT_EQ("contains some element that isn't equal to 1", Describe(m2));
4579}
4580
4581TEST(EachTest, MatchesVectorWhenAllElementsMatch) {
4582 vector<int> some_vector;
4583 EXPECT_THAT(some_vector, Each(1));
4584 some_vector.push_back(3);
4585 EXPECT_THAT(some_vector, Not(Each(1)));
4586 EXPECT_THAT(some_vector, Each(3));
4587 some_vector.push_back(1);
4588 some_vector.push_back(2);
4589 EXPECT_THAT(some_vector, Not(Each(3)));
4590 EXPECT_THAT(some_vector, Each(Lt(3.5)));
4591
4592 vector<string> another_vector;
4593 another_vector.push_back("fee");
4594 EXPECT_THAT(another_vector, Each(string("fee")));
4595 another_vector.push_back("fie");
4596 another_vector.push_back("foe");
4597 another_vector.push_back("fum");
4598 EXPECT_THAT(another_vector, Not(Each(string("fee"))));
4599}
4600
4601TEST(EachTest, MatchesMapWhenAllElementsMatch) {
4602 map<const char*, int> my_map;
4603 const char* bar = "a string";
4604 my_map[bar] = 2;
4605 EXPECT_THAT(my_map, Each(make_pair(bar, 2)));
4606
4607 map<string, int> another_map;
4608 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1)));
4609 another_map["fee"] = 1;
4610 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1)));
4611 another_map["fie"] = 2;
4612 another_map["foe"] = 3;
4613 another_map["fum"] = 4;
4614 EXPECT_THAT(another_map, Not(Each(make_pair(string("fee"), 1))));
4615 EXPECT_THAT(another_map, Not(Each(make_pair(string("fum"), 1))));
4616 EXPECT_THAT(another_map, Each(Pair(_, Gt(0))));
4617}
4618
4619TEST(EachTest, AcceptsMatcher) {
4620 const int a[] = { 1, 2, 3 };
4621 EXPECT_THAT(a, Each(Gt(0)));
4622 EXPECT_THAT(a, Not(Each(Gt(1))));
4623}
4624
4625TEST(EachTest, WorksForNativeArrayAsTuple) {
4626 const int a[] = { 1, 2 };
4627 const int* const pointer = a;
4628 EXPECT_THAT(make_tuple(pointer, 2), Each(Gt(0)));
4629 EXPECT_THAT(make_tuple(pointer, 2), Not(Each(Gt(1))));
4630}
4631
zhanyong.wanab5b77c2010-05-17 19:32:48 +00004632// For testing Pointwise().
4633class IsHalfOfMatcher {
4634 public:
4635 template <typename T1, typename T2>
4636 bool MatchAndExplain(const tuple<T1, T2>& a_pair,
4637 MatchResultListener* listener) const {
4638 if (get<0>(a_pair) == get<1>(a_pair)/2) {
4639 *listener << "where the second is " << get<1>(a_pair);
4640 return true;
4641 } else {
4642 *listener << "where the second/2 is " << get<1>(a_pair)/2;
4643 return false;
4644 }
4645 }
4646
4647 void DescribeTo(ostream* os) const {
4648 *os << "are a pair where the first is half of the second";
4649 }
4650
4651 void DescribeNegationTo(ostream* os) const {
4652 *os << "are a pair where the first isn't half of the second";
4653 }
4654};
4655
4656PolymorphicMatcher<IsHalfOfMatcher> IsHalfOf() {
4657 return MakePolymorphicMatcher(IsHalfOfMatcher());
4658}
4659
4660TEST(PointwiseTest, DescribesSelf) {
4661 vector<int> rhs;
4662 rhs.push_back(1);
4663 rhs.push_back(2);
4664 rhs.push_back(3);
4665 const Matcher<const vector<int>&> m = Pointwise(IsHalfOf(), rhs);
4666 EXPECT_EQ("contains 3 values, where each value and its corresponding value "
4667 "in { 1, 2, 3 } are a pair where the first is half of the second",
4668 Describe(m));
4669 EXPECT_EQ("doesn't contain exactly 3 values, or contains a value x at some "
4670 "index i where x and the i-th value of { 1, 2, 3 } are a pair "
4671 "where the first isn't half of the second",
4672 DescribeNegation(m));
4673}
4674
4675TEST(PointwiseTest, MakesCopyOfRhs) {
4676 list<signed char> rhs;
4677 rhs.push_back(2);
4678 rhs.push_back(4);
4679
4680 int lhs[] = { 1, 2 };
4681 const Matcher<const int (&)[2]> m = Pointwise(IsHalfOf(), rhs);
4682 EXPECT_THAT(lhs, m);
4683
4684 // Changing rhs now shouldn't affect m, which made a copy of rhs.
4685 rhs.push_back(6);
4686 EXPECT_THAT(lhs, m);
4687}
4688
4689TEST(PointwiseTest, WorksForLhsNativeArray) {
4690 const int lhs[] = { 1, 2, 3 };
4691 vector<int> rhs;
4692 rhs.push_back(2);
4693 rhs.push_back(4);
4694 rhs.push_back(6);
4695 EXPECT_THAT(lhs, Pointwise(Lt(), rhs));
4696 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
4697}
4698
4699TEST(PointwiseTest, WorksForRhsNativeArray) {
4700 const int rhs[] = { 1, 2, 3 };
4701 vector<int> lhs;
4702 lhs.push_back(2);
4703 lhs.push_back(4);
4704 lhs.push_back(6);
4705 EXPECT_THAT(lhs, Pointwise(Gt(), rhs));
4706 EXPECT_THAT(lhs, Not(Pointwise(Lt(), rhs)));
4707}
4708
4709TEST(PointwiseTest, RejectsWrongSize) {
4710 const double lhs[2] = { 1, 2 };
4711 const int rhs[1] = { 0 };
4712 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
4713 EXPECT_EQ("which contains 2 values",
4714 Explain(Pointwise(Gt(), rhs), lhs));
4715
4716 const int rhs2[3] = { 0, 1, 2 };
4717 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs2)));
4718}
4719
4720TEST(PointwiseTest, RejectsWrongContent) {
4721 const double lhs[3] = { 1, 2, 3 };
4722 const int rhs[3] = { 2, 6, 4 };
4723 EXPECT_THAT(lhs, Not(Pointwise(IsHalfOf(), rhs)));
4724 EXPECT_EQ("where the value pair (2, 6) at index #1 don't match, "
4725 "where the second/2 is 3",
4726 Explain(Pointwise(IsHalfOf(), rhs), lhs));
4727}
4728
4729TEST(PointwiseTest, AcceptsCorrectContent) {
4730 const double lhs[3] = { 1, 2, 3 };
4731 const int rhs[3] = { 2, 4, 6 };
4732 EXPECT_THAT(lhs, Pointwise(IsHalfOf(), rhs));
4733 EXPECT_EQ("", Explain(Pointwise(IsHalfOf(), rhs), lhs));
4734}
4735
4736TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) {
4737 const double lhs[3] = { 1, 2, 3 };
4738 const int rhs[3] = { 2, 4, 6 };
4739 const Matcher<tuple<const double&, const int&> > m1 = IsHalfOf();
4740 EXPECT_THAT(lhs, Pointwise(m1, rhs));
4741 EXPECT_EQ("", Explain(Pointwise(m1, rhs), lhs));
4742
4743 // This type works as a tuple<const double&, const int&> can be
4744 // implicitly cast to tuple<double, int>.
4745 const Matcher<tuple<double, int> > m2 = IsHalfOf();
4746 EXPECT_THAT(lhs, Pointwise(m2, rhs));
4747 EXPECT_EQ("", Explain(Pointwise(m2, rhs), lhs));
4748}
4749
shiqiane35fdd92008-12-10 05:08:54 +00004750} // namespace gmock_matchers_test
4751} // namespace testing