blob: 81460925c14c7f995e052c8cddd13c7630de61f6 [file] [log] [blame]
shiqiane35fdd92008-12-10 05:08:54 +00001// Copyright 2007, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29//
30// Author: wan@google.com (Zhanyong Wan)
31
32// Google Mock - a framework for writing C++ mock classes.
33//
34// This file tests some commonly used argument matchers.
35
zhanyong.wan53e08c42010-09-14 05:38:21 +000036#include "gmock/gmock-matchers.h"
zhanyong.wan320814a2013-03-01 00:20:30 +000037#include "gmock/gmock-more-matchers.h"
shiqiane35fdd92008-12-10 05:08:54 +000038
39#include <string.h>
40#include <functional>
zhanyong.wana862f1d2010-03-15 21:23:04 +000041#include <iostream>
zhanyong.wan6a896b52009-01-16 01:13:50 +000042#include <list>
43#include <map>
44#include <set>
shiqiane35fdd92008-12-10 05:08:54 +000045#include <sstream>
zhanyong.wan6a896b52009-01-16 01:13:50 +000046#include <string>
zhanyong.wanf5e1ce52009-09-16 07:02:02 +000047#include <utility>
zhanyong.wan6a896b52009-01-16 01:13:50 +000048#include <vector>
zhanyong.wan53e08c42010-09-14 05:38:21 +000049#include "gmock/gmock.h"
50#include "gtest/gtest.h"
51#include "gtest/gtest-spi.h"
shiqiane35fdd92008-12-10 05:08:54 +000052
53namespace testing {
zhanyong.wan4a5330d2009-02-19 00:36:44 +000054
55namespace internal {
vladlosev587c1b32011-05-20 00:42:22 +000056GTEST_API_ string JoinAsTuple(const Strings& fields);
zhanyong.wan4a5330d2009-02-19 00:36:44 +000057} // namespace internal
58
shiqiane35fdd92008-12-10 05:08:54 +000059namespace gmock_matchers_test {
60
zhanyong.wan898725c2011-09-16 16:45:39 +000061using std::greater;
62using std::less;
zhanyong.wanab5b77c2010-05-17 19:32:48 +000063using std::list;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000064using std::make_pair;
zhanyong.wanb5937da2009-07-16 20:26:41 +000065using std::map;
66using std::multimap;
zhanyong.wanab5b77c2010-05-17 19:32:48 +000067using std::multiset;
68using std::ostream;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000069using std::pair;
zhanyong.wan33605ba2010-04-22 23:37:47 +000070using std::set;
shiqiane35fdd92008-12-10 05:08:54 +000071using std::stringstream;
zhanyong.wanab5b77c2010-05-17 19:32:48 +000072using std::tr1::get;
zhanyong.wanb8243162009-06-04 05:48:20 +000073using std::tr1::make_tuple;
zhanyong.wanab5b77c2010-05-17 19:32:48 +000074using std::tr1::tuple;
zhanyong.wan33605ba2010-04-22 23:37:47 +000075using std::vector;
shiqiane35fdd92008-12-10 05:08:54 +000076using testing::A;
zhanyong.wanbf550852009-06-09 06:09:53 +000077using testing::AllArgs;
shiqiane35fdd92008-12-10 05:08:54 +000078using testing::AllOf;
79using testing::An;
80using testing::AnyOf;
81using testing::ByRef;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000082using testing::ContainsRegex;
shiqiane35fdd92008-12-10 05:08:54 +000083using testing::DoubleEq;
84using testing::EndsWith;
85using testing::Eq;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000086using testing::ExplainMatchResult;
shiqiane35fdd92008-12-10 05:08:54 +000087using testing::Field;
88using testing::FloatEq;
89using testing::Ge;
90using testing::Gt;
91using testing::HasSubstr;
zhanyong.wan320814a2013-03-01 00:20:30 +000092using testing::IsEmpty;
zhanyong.wan2d970ee2009-09-24 21:41:36 +000093using testing::IsNull;
zhanyong.wanb5937da2009-07-16 20:26:41 +000094using testing::Key;
shiqiane35fdd92008-12-10 05:08:54 +000095using testing::Le;
96using testing::Lt;
97using testing::MakeMatcher;
98using testing::MakePolymorphicMatcher;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000099using testing::MatchResultListener;
shiqiane35fdd92008-12-10 05:08:54 +0000100using testing::Matcher;
101using testing::MatcherCast;
102using testing::MatcherInterface;
103using testing::Matches;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000104using testing::MatchesRegex;
shiqiane35fdd92008-12-10 05:08:54 +0000105using testing::NanSensitiveDoubleEq;
106using testing::NanSensitiveFloatEq;
107using testing::Ne;
108using testing::Not;
109using testing::NotNull;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000110using testing::Pair;
shiqiane35fdd92008-12-10 05:08:54 +0000111using testing::Pointee;
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000112using testing::Pointwise;
shiqiane35fdd92008-12-10 05:08:54 +0000113using testing::PolymorphicMatcher;
114using testing::Property;
115using testing::Ref;
116using testing::ResultOf;
zhanyong.wana31d9ce2013-03-01 01:50:17 +0000117using testing::SizeIs;
shiqiane35fdd92008-12-10 05:08:54 +0000118using testing::StartsWith;
119using testing::StrCaseEq;
120using testing::StrCaseNe;
121using testing::StrEq;
122using testing::StrNe;
123using testing::Truly;
124using testing::TypedEq;
zhanyong.wanb8243162009-06-04 05:48:20 +0000125using testing::Value;
zhanyong.wan898725c2011-09-16 16:45:39 +0000126using testing::WhenSorted;
127using testing::WhenSortedBy;
shiqiane35fdd92008-12-10 05:08:54 +0000128using testing::_;
zhanyong.wana862f1d2010-03-15 21:23:04 +0000129using testing::internal::DummyMatchResultListener;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000130using testing::internal::ExplainMatchFailureTupleTo;
shiqiane35fdd92008-12-10 05:08:54 +0000131using testing::internal::FloatingEqMatcher;
zhanyong.wanb4140802010-06-08 22:53:57 +0000132using testing::internal::FormatMatcherDescription;
zhanyong.wan736baa82010-09-27 17:44:16 +0000133using testing::internal::IsReadableTypeName;
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000134using testing::internal::JoinAsTuple;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000135using testing::internal::RE;
zhanyong.wana862f1d2010-03-15 21:23:04 +0000136using testing::internal::StreamMatchResultListener;
zhanyong.wan34b034c2010-03-05 21:23:23 +0000137using testing::internal::StringMatchResultListener;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000138using testing::internal::Strings;
vladlosev79b83502009-11-18 00:43:37 +0000139using testing::internal::linked_ptr;
vladloseve56daa72009-11-18 01:08:08 +0000140using testing::internal::scoped_ptr;
shiqiane35fdd92008-12-10 05:08:54 +0000141using testing::internal::string;
142
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000143// For testing ExplainMatchResultTo().
144class GreaterThanMatcher : public MatcherInterface<int> {
145 public:
146 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
147
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000148 virtual void DescribeTo(ostream* os) const {
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000149 *os << "is > " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000150 }
151
zhanyong.wandb22c222010-01-28 21:52:29 +0000152 virtual bool MatchAndExplain(int lhs,
153 MatchResultListener* listener) const {
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000154 const int diff = lhs - rhs_;
155 if (diff > 0) {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000156 *listener << "which is " << diff << " more than " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000157 } else if (diff == 0) {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000158 *listener << "which is the same as " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000159 } else {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000160 *listener << "which is " << -diff << " less than " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000161 }
zhanyong.wandb22c222010-01-28 21:52:29 +0000162
163 return lhs > rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000164 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000165
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000166 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000167 int rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000168};
169
170Matcher<int> GreaterThan(int n) {
171 return MakeMatcher(new GreaterThanMatcher(n));
172}
173
zhanyong.wan736baa82010-09-27 17:44:16 +0000174string OfType(const string& type_name) {
175#if GTEST_HAS_RTTI
176 return " (of type " + type_name + ")";
177#else
178 return "";
179#endif
180}
181
shiqiane35fdd92008-12-10 05:08:54 +0000182// Returns the description of the given matcher.
183template <typename T>
184string Describe(const Matcher<T>& m) {
185 stringstream ss;
186 m.DescribeTo(&ss);
187 return ss.str();
188}
189
190// Returns the description of the negation of the given matcher.
191template <typename T>
192string DescribeNegation(const Matcher<T>& m) {
193 stringstream ss;
194 m.DescribeNegationTo(&ss);
195 return ss.str();
196}
197
198// Returns the reason why x matches, or doesn't match, m.
199template <typename MatcherType, typename Value>
200string Explain(const MatcherType& m, const Value& x) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000201 StringMatchResultListener listener;
202 ExplainMatchResult(m, x, &listener);
203 return listener.str();
shiqiane35fdd92008-12-10 05:08:54 +0000204}
205
zhanyong.wana862f1d2010-03-15 21:23:04 +0000206TEST(MatchResultListenerTest, StreamingWorks) {
207 StringMatchResultListener listener;
208 listener << "hi" << 5;
209 EXPECT_EQ("hi5", listener.str());
210
211 // Streaming shouldn't crash when the underlying ostream is NULL.
212 DummyMatchResultListener dummy;
213 dummy << "hi" << 5;
214}
215
216TEST(MatchResultListenerTest, CanAccessUnderlyingStream) {
217 EXPECT_TRUE(DummyMatchResultListener().stream() == NULL);
218 EXPECT_TRUE(StreamMatchResultListener(NULL).stream() == NULL);
219
220 EXPECT_EQ(&std::cout, StreamMatchResultListener(&std::cout).stream());
221}
222
223TEST(MatchResultListenerTest, IsInterestedWorks) {
224 EXPECT_TRUE(StringMatchResultListener().IsInterested());
225 EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested());
226
227 EXPECT_FALSE(DummyMatchResultListener().IsInterested());
228 EXPECT_FALSE(StreamMatchResultListener(NULL).IsInterested());
229}
230
shiqiane35fdd92008-12-10 05:08:54 +0000231// Makes sure that the MatcherInterface<T> interface doesn't
232// change.
233class EvenMatcherImpl : public MatcherInterface<int> {
234 public:
zhanyong.wandb22c222010-01-28 21:52:29 +0000235 virtual bool MatchAndExplain(int x,
236 MatchResultListener* /* listener */) const {
237 return x % 2 == 0;
238 }
shiqiane35fdd92008-12-10 05:08:54 +0000239
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000240 virtual void DescribeTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +0000241 *os << "is an even number";
242 }
243
244 // We deliberately don't define DescribeNegationTo() and
245 // ExplainMatchResultTo() here, to make sure the definition of these
246 // two methods is optional.
247};
248
zhanyong.wana862f1d2010-03-15 21:23:04 +0000249// Makes sure that the MatcherInterface API doesn't change.
250TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) {
shiqiane35fdd92008-12-10 05:08:54 +0000251 EvenMatcherImpl m;
252}
253
zhanyong.wan82113312010-01-08 21:55:40 +0000254// Tests implementing a monomorphic matcher using MatchAndExplain().
255
256class NewEvenMatcherImpl : public MatcherInterface<int> {
257 public:
258 virtual bool MatchAndExplain(int x, MatchResultListener* listener) const {
259 const bool match = x % 2 == 0;
260 // Verifies that we can stream to a listener directly.
261 *listener << "value % " << 2;
262 if (listener->stream() != NULL) {
263 // Verifies that we can stream to a listener's underlying stream
264 // too.
265 *listener->stream() << " == " << (x % 2);
266 }
267 return match;
268 }
269
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000270 virtual void DescribeTo(ostream* os) const {
zhanyong.wan82113312010-01-08 21:55:40 +0000271 *os << "is an even number";
272 }
273};
274
275TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {
276 Matcher<int> m = MakeMatcher(new NewEvenMatcherImpl);
277 EXPECT_TRUE(m.Matches(2));
278 EXPECT_FALSE(m.Matches(3));
279 EXPECT_EQ("value % 2 == 0", Explain(m, 2));
280 EXPECT_EQ("value % 2 == 1", Explain(m, 3));
281}
282
shiqiane35fdd92008-12-10 05:08:54 +0000283// Tests default-constructing a matcher.
284TEST(MatcherTest, CanBeDefaultConstructed) {
285 Matcher<double> m;
286}
287
288// Tests that Matcher<T> can be constructed from a MatcherInterface<T>*.
289TEST(MatcherTest, CanBeConstructedFromMatcherInterface) {
290 const MatcherInterface<int>* impl = new EvenMatcherImpl;
291 Matcher<int> m(impl);
292 EXPECT_TRUE(m.Matches(4));
293 EXPECT_FALSE(m.Matches(5));
294}
295
296// Tests that value can be used in place of Eq(value).
297TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) {
298 Matcher<int> m1 = 5;
299 EXPECT_TRUE(m1.Matches(5));
300 EXPECT_FALSE(m1.Matches(6));
301}
302
303// Tests that NULL can be used in place of Eq(NULL).
304TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) {
305 Matcher<int*> m1 = NULL;
306 EXPECT_TRUE(m1.Matches(NULL));
307 int n = 0;
308 EXPECT_FALSE(m1.Matches(&n));
309}
310
311// Tests that matchers are copyable.
312TEST(MatcherTest, IsCopyable) {
313 // Tests the copy constructor.
314 Matcher<bool> m1 = Eq(false);
315 EXPECT_TRUE(m1.Matches(false));
316 EXPECT_FALSE(m1.Matches(true));
317
318 // Tests the assignment operator.
319 m1 = Eq(true);
320 EXPECT_TRUE(m1.Matches(true));
321 EXPECT_FALSE(m1.Matches(false));
322}
323
324// Tests that Matcher<T>::DescribeTo() calls
325// MatcherInterface<T>::DescribeTo().
326TEST(MatcherTest, CanDescribeItself) {
327 EXPECT_EQ("is an even number",
328 Describe(Matcher<int>(new EvenMatcherImpl)));
329}
330
zhanyong.wan82113312010-01-08 21:55:40 +0000331// Tests Matcher<T>::MatchAndExplain().
332TEST(MatcherTest, MatchAndExplain) {
333 Matcher<int> m = GreaterThan(0);
zhanyong.wan34b034c2010-03-05 21:23:23 +0000334 StringMatchResultListener listener1;
zhanyong.wan82113312010-01-08 21:55:40 +0000335 EXPECT_TRUE(m.MatchAndExplain(42, &listener1));
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000336 EXPECT_EQ("which is 42 more than 0", listener1.str());
zhanyong.wan82113312010-01-08 21:55:40 +0000337
zhanyong.wan34b034c2010-03-05 21:23:23 +0000338 StringMatchResultListener listener2;
zhanyong.wan82113312010-01-08 21:55:40 +0000339 EXPECT_FALSE(m.MatchAndExplain(-9, &listener2));
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000340 EXPECT_EQ("which is 9 less than 0", listener2.str());
zhanyong.wan82113312010-01-08 21:55:40 +0000341}
342
shiqiane35fdd92008-12-10 05:08:54 +0000343// Tests that a C-string literal can be implicitly converted to a
344// Matcher<string> or Matcher<const string&>.
345TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
346 Matcher<string> m1 = "hi";
347 EXPECT_TRUE(m1.Matches("hi"));
348 EXPECT_FALSE(m1.Matches("hello"));
349
350 Matcher<const string&> m2 = "hi";
351 EXPECT_TRUE(m2.Matches("hi"));
352 EXPECT_FALSE(m2.Matches("hello"));
353}
354
355// Tests that a string object can be implicitly converted to a
356// Matcher<string> or Matcher<const string&>.
357TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {
358 Matcher<string> m1 = string("hi");
359 EXPECT_TRUE(m1.Matches("hi"));
360 EXPECT_FALSE(m1.Matches("hello"));
361
362 Matcher<const string&> m2 = string("hi");
363 EXPECT_TRUE(m2.Matches("hi"));
364 EXPECT_FALSE(m2.Matches("hello"));
365}
366
367// Tests that MakeMatcher() constructs a Matcher<T> from a
368// MatcherInterface* without requiring the user to explicitly
369// write the type.
370TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) {
371 const MatcherInterface<int>* dummy_impl = NULL;
372 Matcher<int> m = MakeMatcher(dummy_impl);
373}
374
zhanyong.wan82113312010-01-08 21:55:40 +0000375// Tests that MakePolymorphicMatcher() can construct a polymorphic
376// matcher from its implementation using the old API.
zhanyong.wan33605ba2010-04-22 23:37:47 +0000377const int g_bar = 1;
shiqiane35fdd92008-12-10 05:08:54 +0000378class ReferencesBarOrIsZeroImpl {
379 public:
380 template <typename T>
zhanyong.wandb22c222010-01-28 21:52:29 +0000381 bool MatchAndExplain(const T& x,
382 MatchResultListener* /* listener */) const {
shiqiane35fdd92008-12-10 05:08:54 +0000383 const void* p = &x;
zhanyong.wan33605ba2010-04-22 23:37:47 +0000384 return p == &g_bar || x == 0;
shiqiane35fdd92008-12-10 05:08:54 +0000385 }
386
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000387 void DescribeTo(ostream* os) const { *os << "g_bar or zero"; }
shiqiane35fdd92008-12-10 05:08:54 +0000388
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000389 void DescribeNegationTo(ostream* os) const {
zhanyong.wan33605ba2010-04-22 23:37:47 +0000390 *os << "doesn't reference g_bar and is not zero";
shiqiane35fdd92008-12-10 05:08:54 +0000391 }
392};
393
394// This function verifies that MakePolymorphicMatcher() returns a
395// PolymorphicMatcher<T> where T is the argument's type.
396PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() {
397 return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl());
398}
399
zhanyong.wan82113312010-01-08 21:55:40 +0000400TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) {
shiqiane35fdd92008-12-10 05:08:54 +0000401 // Using a polymorphic matcher to match a reference type.
402 Matcher<const int&> m1 = ReferencesBarOrIsZero();
403 EXPECT_TRUE(m1.Matches(0));
404 // Verifies that the identity of a by-reference argument is preserved.
zhanyong.wan33605ba2010-04-22 23:37:47 +0000405 EXPECT_TRUE(m1.Matches(g_bar));
shiqiane35fdd92008-12-10 05:08:54 +0000406 EXPECT_FALSE(m1.Matches(1));
zhanyong.wan33605ba2010-04-22 23:37:47 +0000407 EXPECT_EQ("g_bar or zero", Describe(m1));
shiqiane35fdd92008-12-10 05:08:54 +0000408
409 // Using a polymorphic matcher to match a value type.
410 Matcher<double> m2 = ReferencesBarOrIsZero();
411 EXPECT_TRUE(m2.Matches(0.0));
412 EXPECT_FALSE(m2.Matches(0.1));
zhanyong.wan33605ba2010-04-22 23:37:47 +0000413 EXPECT_EQ("g_bar or zero", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +0000414}
415
zhanyong.wan82113312010-01-08 21:55:40 +0000416// Tests implementing a polymorphic matcher using MatchAndExplain().
417
418class PolymorphicIsEvenImpl {
419 public:
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000420 void DescribeTo(ostream* os) const { *os << "is even"; }
zhanyong.wan82113312010-01-08 21:55:40 +0000421
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000422 void DescribeNegationTo(ostream* os) const {
zhanyong.wan82113312010-01-08 21:55:40 +0000423 *os << "is odd";
424 }
zhanyong.wan82113312010-01-08 21:55:40 +0000425
zhanyong.wandb22c222010-01-28 21:52:29 +0000426 template <typename T>
427 bool MatchAndExplain(const T& x, MatchResultListener* listener) const {
428 // Verifies that we can stream to the listener directly.
429 *listener << "% " << 2;
430 if (listener->stream() != NULL) {
431 // Verifies that we can stream to the listener's underlying stream
432 // too.
433 *listener->stream() << " == " << (x % 2);
434 }
435 return (x % 2) == 0;
zhanyong.wan82113312010-01-08 21:55:40 +0000436 }
zhanyong.wandb22c222010-01-28 21:52:29 +0000437};
zhanyong.wan82113312010-01-08 21:55:40 +0000438
439PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() {
440 return MakePolymorphicMatcher(PolymorphicIsEvenImpl());
441}
442
443TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {
444 // Using PolymorphicIsEven() as a Matcher<int>.
445 const Matcher<int> m1 = PolymorphicIsEven();
446 EXPECT_TRUE(m1.Matches(42));
447 EXPECT_FALSE(m1.Matches(43));
448 EXPECT_EQ("is even", Describe(m1));
449
450 const Matcher<int> not_m1 = Not(m1);
451 EXPECT_EQ("is odd", Describe(not_m1));
452
453 EXPECT_EQ("% 2 == 0", Explain(m1, 42));
454
455 // Using PolymorphicIsEven() as a Matcher<char>.
456 const Matcher<char> m2 = PolymorphicIsEven();
457 EXPECT_TRUE(m2.Matches('\x42'));
458 EXPECT_FALSE(m2.Matches('\x43'));
459 EXPECT_EQ("is even", Describe(m2));
460
461 const Matcher<char> not_m2 = Not(m2);
462 EXPECT_EQ("is odd", Describe(not_m2));
463
464 EXPECT_EQ("% 2 == 0", Explain(m2, '\x42'));
465}
466
shiqiane35fdd92008-12-10 05:08:54 +0000467// Tests that MatcherCast<T>(m) works when m is a polymorphic matcher.
468TEST(MatcherCastTest, FromPolymorphicMatcher) {
469 Matcher<int> m = MatcherCast<int>(Eq(5));
470 EXPECT_TRUE(m.Matches(5));
471 EXPECT_FALSE(m.Matches(6));
472}
473
474// For testing casting matchers between compatible types.
475class IntValue {
476 public:
477 // An int can be statically (although not implicitly) cast to a
478 // IntValue.
zhanyong.wan32de5f52009-12-23 00:13:23 +0000479 explicit IntValue(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +0000480
481 int value() const { return value_; }
482 private:
483 int value_;
484};
485
486// For testing casting matchers between compatible types.
487bool IsPositiveIntValue(const IntValue& foo) {
488 return foo.value() > 0;
489}
490
491// Tests that MatcherCast<T>(m) works when m is a Matcher<U> where T
492// can be statically converted to U.
493TEST(MatcherCastTest, FromCompatibleType) {
494 Matcher<double> m1 = Eq(2.0);
495 Matcher<int> m2 = MatcherCast<int>(m1);
496 EXPECT_TRUE(m2.Matches(2));
497 EXPECT_FALSE(m2.Matches(3));
498
499 Matcher<IntValue> m3 = Truly(IsPositiveIntValue);
500 Matcher<int> m4 = MatcherCast<int>(m3);
501 // In the following, the arguments 1 and 0 are statically converted
502 // to IntValue objects, and then tested by the IsPositiveIntValue()
503 // predicate.
504 EXPECT_TRUE(m4.Matches(1));
505 EXPECT_FALSE(m4.Matches(0));
506}
507
508// Tests that MatcherCast<T>(m) works when m is a Matcher<const T&>.
509TEST(MatcherCastTest, FromConstReferenceToNonReference) {
510 Matcher<const int&> m1 = Eq(0);
511 Matcher<int> m2 = MatcherCast<int>(m1);
512 EXPECT_TRUE(m2.Matches(0));
513 EXPECT_FALSE(m2.Matches(1));
514}
515
516// Tests that MatcherCast<T>(m) works when m is a Matcher<T&>.
517TEST(MatcherCastTest, FromReferenceToNonReference) {
518 Matcher<int&> m1 = Eq(0);
519 Matcher<int> m2 = MatcherCast<int>(m1);
520 EXPECT_TRUE(m2.Matches(0));
521 EXPECT_FALSE(m2.Matches(1));
522}
523
524// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
525TEST(MatcherCastTest, FromNonReferenceToConstReference) {
526 Matcher<int> m1 = Eq(0);
527 Matcher<const int&> m2 = MatcherCast<const int&>(m1);
528 EXPECT_TRUE(m2.Matches(0));
529 EXPECT_FALSE(m2.Matches(1));
530}
531
532// Tests that MatcherCast<T&>(m) works when m is a Matcher<T>.
533TEST(MatcherCastTest, FromNonReferenceToReference) {
534 Matcher<int> m1 = Eq(0);
535 Matcher<int&> m2 = MatcherCast<int&>(m1);
536 int n = 0;
537 EXPECT_TRUE(m2.Matches(n));
538 n = 1;
539 EXPECT_FALSE(m2.Matches(n));
540}
541
542// Tests that MatcherCast<T>(m) works when m is a Matcher<T>.
543TEST(MatcherCastTest, FromSameType) {
544 Matcher<int> m1 = Eq(0);
545 Matcher<int> m2 = MatcherCast<int>(m1);
546 EXPECT_TRUE(m2.Matches(0));
547 EXPECT_FALSE(m2.Matches(1));
548}
549
jgm79a367e2012-04-10 16:02:11 +0000550// Implicitly convertible form any type.
551struct ConvertibleFromAny {
552 ConvertibleFromAny(int a_value) : value(a_value) {}
553 template <typename T>
554 ConvertibleFromAny(const T& a_value) : value(-1) {
555 ADD_FAILURE() << "Conversion constructor called";
556 }
557 int value;
558};
559
560bool operator==(const ConvertibleFromAny& a, const ConvertibleFromAny& b) {
561 return a.value == b.value;
562}
563
564ostream& operator<<(ostream& os, const ConvertibleFromAny& a) {
565 return os << a.value;
566}
567
568TEST(MatcherCastTest, ConversionConstructorIsUsed) {
569 Matcher<ConvertibleFromAny> m = MatcherCast<ConvertibleFromAny>(1);
570 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
571 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
572}
573
574TEST(MatcherCastTest, FromConvertibleFromAny) {
575 Matcher<ConvertibleFromAny> m =
576 MatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
577 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
578 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
579}
580
zhanyong.wan18490652009-05-11 18:54:08 +0000581class Base {};
582class Derived : public Base {};
583
584// Tests that SafeMatcherCast<T>(m) works when m is a polymorphic matcher.
585TEST(SafeMatcherCastTest, FromPolymorphicMatcher) {
586 Matcher<char> m2 = SafeMatcherCast<char>(Eq(32));
587 EXPECT_TRUE(m2.Matches(' '));
588 EXPECT_FALSE(m2.Matches('\n'));
589}
590
zhanyong.wan16cf4732009-05-14 20:55:30 +0000591// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where
592// T and U are arithmetic types and T can be losslessly converted to
593// U.
594TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) {
zhanyong.wan18490652009-05-11 18:54:08 +0000595 Matcher<double> m1 = DoubleEq(1.0);
zhanyong.wan16cf4732009-05-14 20:55:30 +0000596 Matcher<float> m2 = SafeMatcherCast<float>(m1);
597 EXPECT_TRUE(m2.Matches(1.0f));
598 EXPECT_FALSE(m2.Matches(2.0f));
599
600 Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>('a'));
601 EXPECT_TRUE(m3.Matches('a'));
602 EXPECT_FALSE(m3.Matches('b'));
zhanyong.wan18490652009-05-11 18:54:08 +0000603}
604
605// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where T and U
606// are pointers or references to a derived and a base class, correspondingly.
607TEST(SafeMatcherCastTest, FromBaseClass) {
608 Derived d, d2;
609 Matcher<Base*> m1 = Eq(&d);
610 Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1);
611 EXPECT_TRUE(m2.Matches(&d));
612 EXPECT_FALSE(m2.Matches(&d2));
613
614 Matcher<Base&> m3 = Ref(d);
615 Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3);
616 EXPECT_TRUE(m4.Matches(d));
617 EXPECT_FALSE(m4.Matches(d2));
618}
619
620// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<const T&>.
621TEST(SafeMatcherCastTest, FromConstReferenceToReference) {
622 int n = 0;
623 Matcher<const int&> m1 = Ref(n);
624 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
625 int n1 = 0;
626 EXPECT_TRUE(m2.Matches(n));
627 EXPECT_FALSE(m2.Matches(n1));
628}
629
630// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
631TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) {
632 Matcher<int> m1 = Eq(0);
633 Matcher<const int&> m2 = SafeMatcherCast<const int&>(m1);
634 EXPECT_TRUE(m2.Matches(0));
635 EXPECT_FALSE(m2.Matches(1));
636}
637
638// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<T>.
639TEST(SafeMatcherCastTest, FromNonReferenceToReference) {
640 Matcher<int> m1 = Eq(0);
641 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
642 int n = 0;
643 EXPECT_TRUE(m2.Matches(n));
644 n = 1;
645 EXPECT_FALSE(m2.Matches(n));
646}
647
648// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<T>.
649TEST(SafeMatcherCastTest, FromSameType) {
650 Matcher<int> m1 = Eq(0);
651 Matcher<int> m2 = SafeMatcherCast<int>(m1);
652 EXPECT_TRUE(m2.Matches(0));
653 EXPECT_FALSE(m2.Matches(1));
654}
655
jgm79a367e2012-04-10 16:02:11 +0000656TEST(SafeMatcherCastTest, ConversionConstructorIsUsed) {
657 Matcher<ConvertibleFromAny> m = SafeMatcherCast<ConvertibleFromAny>(1);
658 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
659 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
660}
661
662TEST(SafeMatcherCastTest, FromConvertibleFromAny) {
663 Matcher<ConvertibleFromAny> m =
664 SafeMatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
665 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
666 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
667}
668
shiqiane35fdd92008-12-10 05:08:54 +0000669// Tests that A<T>() matches any value of type T.
670TEST(ATest, MatchesAnyValue) {
671 // Tests a matcher for a value type.
672 Matcher<double> m1 = A<double>();
673 EXPECT_TRUE(m1.Matches(91.43));
674 EXPECT_TRUE(m1.Matches(-15.32));
675
676 // Tests a matcher for a reference type.
677 int a = 2;
678 int b = -6;
679 Matcher<int&> m2 = A<int&>();
680 EXPECT_TRUE(m2.Matches(a));
681 EXPECT_TRUE(m2.Matches(b));
682}
683
684// Tests that A<T>() describes itself properly.
685TEST(ATest, CanDescribeSelf) {
686 EXPECT_EQ("is anything", Describe(A<bool>()));
687}
688
689// Tests that An<T>() matches any value of type T.
690TEST(AnTest, MatchesAnyValue) {
691 // Tests a matcher for a value type.
692 Matcher<int> m1 = An<int>();
693 EXPECT_TRUE(m1.Matches(9143));
694 EXPECT_TRUE(m1.Matches(-1532));
695
696 // Tests a matcher for a reference type.
697 int a = 2;
698 int b = -6;
699 Matcher<int&> m2 = An<int&>();
700 EXPECT_TRUE(m2.Matches(a));
701 EXPECT_TRUE(m2.Matches(b));
702}
703
704// Tests that An<T>() describes itself properly.
705TEST(AnTest, CanDescribeSelf) {
706 EXPECT_EQ("is anything", Describe(An<int>()));
707}
708
709// Tests that _ can be used as a matcher for any type and matches any
710// value of that type.
711TEST(UnderscoreTest, MatchesAnyValue) {
712 // Uses _ as a matcher for a value type.
713 Matcher<int> m1 = _;
714 EXPECT_TRUE(m1.Matches(123));
715 EXPECT_TRUE(m1.Matches(-242));
716
717 // Uses _ as a matcher for a reference type.
718 bool a = false;
719 const bool b = true;
720 Matcher<const bool&> m2 = _;
721 EXPECT_TRUE(m2.Matches(a));
722 EXPECT_TRUE(m2.Matches(b));
723}
724
725// Tests that _ describes itself properly.
726TEST(UnderscoreTest, CanDescribeSelf) {
727 Matcher<int> m = _;
728 EXPECT_EQ("is anything", Describe(m));
729}
730
731// Tests that Eq(x) matches any value equal to x.
732TEST(EqTest, MatchesEqualValue) {
733 // 2 C-strings with same content but different addresses.
734 const char a1[] = "hi";
735 const char a2[] = "hi";
736
737 Matcher<const char*> m1 = Eq(a1);
738 EXPECT_TRUE(m1.Matches(a1));
739 EXPECT_FALSE(m1.Matches(a2));
740}
741
742// Tests that Eq(v) describes itself properly.
743
744class Unprintable {
745 public:
746 Unprintable() : c_('a') {}
747
zhanyong.wan32de5f52009-12-23 00:13:23 +0000748 bool operator==(const Unprintable& /* rhs */) { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000749 private:
750 char c_;
751};
752
753TEST(EqTest, CanDescribeSelf) {
754 Matcher<Unprintable> m = Eq(Unprintable());
755 EXPECT_EQ("is equal to 1-byte object <61>", Describe(m));
756}
757
758// Tests that Eq(v) can be used to match any type that supports
759// comparing with type T, where T is v's type.
760TEST(EqTest, IsPolymorphic) {
761 Matcher<int> m1 = Eq(1);
762 EXPECT_TRUE(m1.Matches(1));
763 EXPECT_FALSE(m1.Matches(2));
764
765 Matcher<char> m2 = Eq(1);
766 EXPECT_TRUE(m2.Matches('\1'));
767 EXPECT_FALSE(m2.Matches('a'));
768}
769
770// Tests that TypedEq<T>(v) matches values of type T that's equal to v.
771TEST(TypedEqTest, ChecksEqualityForGivenType) {
772 Matcher<char> m1 = TypedEq<char>('a');
773 EXPECT_TRUE(m1.Matches('a'));
774 EXPECT_FALSE(m1.Matches('b'));
775
776 Matcher<int> m2 = TypedEq<int>(6);
777 EXPECT_TRUE(m2.Matches(6));
778 EXPECT_FALSE(m2.Matches(7));
779}
780
781// Tests that TypedEq(v) describes itself properly.
782TEST(TypedEqTest, CanDescribeSelf) {
783 EXPECT_EQ("is equal to 2", Describe(TypedEq<int>(2)));
784}
785
786// Tests that TypedEq<T>(v) has type Matcher<T>.
787
788// Type<T>::IsTypeOf(v) compiles iff the type of value v is T, where T
789// is a "bare" type (i.e. not in the form of const U or U&). If v's
790// type is not T, the compiler will generate a message about
791// "undefined referece".
792template <typename T>
793struct Type {
zhanyong.wan32de5f52009-12-23 00:13:23 +0000794 static bool IsTypeOf(const T& /* v */) { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000795
796 template <typename T2>
797 static void IsTypeOf(T2 v);
798};
799
800TEST(TypedEqTest, HasSpecifiedType) {
801 // Verfies that the type of TypedEq<T>(v) is Matcher<T>.
802 Type<Matcher<int> >::IsTypeOf(TypedEq<int>(5));
803 Type<Matcher<double> >::IsTypeOf(TypedEq<double>(5));
804}
805
806// Tests that Ge(v) matches anything >= v.
807TEST(GeTest, ImplementsGreaterThanOrEqual) {
808 Matcher<int> m1 = Ge(0);
809 EXPECT_TRUE(m1.Matches(1));
810 EXPECT_TRUE(m1.Matches(0));
811 EXPECT_FALSE(m1.Matches(-1));
812}
813
814// Tests that Ge(v) describes itself properly.
815TEST(GeTest, CanDescribeSelf) {
816 Matcher<int> m = Ge(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000817 EXPECT_EQ("is >= 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000818}
819
820// Tests that Gt(v) matches anything > v.
821TEST(GtTest, ImplementsGreaterThan) {
822 Matcher<double> m1 = Gt(0);
823 EXPECT_TRUE(m1.Matches(1.0));
824 EXPECT_FALSE(m1.Matches(0.0));
825 EXPECT_FALSE(m1.Matches(-1.0));
826}
827
828// Tests that Gt(v) describes itself properly.
829TEST(GtTest, CanDescribeSelf) {
830 Matcher<int> m = Gt(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000831 EXPECT_EQ("is > 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000832}
833
834// Tests that Le(v) matches anything <= v.
835TEST(LeTest, ImplementsLessThanOrEqual) {
836 Matcher<char> m1 = Le('b');
837 EXPECT_TRUE(m1.Matches('a'));
838 EXPECT_TRUE(m1.Matches('b'));
839 EXPECT_FALSE(m1.Matches('c'));
840}
841
842// Tests that Le(v) describes itself properly.
843TEST(LeTest, CanDescribeSelf) {
844 Matcher<int> m = Le(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000845 EXPECT_EQ("is <= 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000846}
847
848// Tests that Lt(v) matches anything < v.
849TEST(LtTest, ImplementsLessThan) {
850 Matcher<const string&> m1 = Lt("Hello");
851 EXPECT_TRUE(m1.Matches("Abc"));
852 EXPECT_FALSE(m1.Matches("Hello"));
853 EXPECT_FALSE(m1.Matches("Hello, world!"));
854}
855
856// Tests that Lt(v) describes itself properly.
857TEST(LtTest, CanDescribeSelf) {
858 Matcher<int> m = Lt(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000859 EXPECT_EQ("is < 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000860}
861
862// Tests that Ne(v) matches anything != v.
863TEST(NeTest, ImplementsNotEqual) {
864 Matcher<int> m1 = Ne(0);
865 EXPECT_TRUE(m1.Matches(1));
866 EXPECT_TRUE(m1.Matches(-1));
867 EXPECT_FALSE(m1.Matches(0));
868}
869
870// Tests that Ne(v) describes itself properly.
871TEST(NeTest, CanDescribeSelf) {
872 Matcher<int> m = Ne(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000873 EXPECT_EQ("isn't equal to 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000874}
875
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000876// Tests that IsNull() matches any NULL pointer of any type.
877TEST(IsNullTest, MatchesNullPointer) {
878 Matcher<int*> m1 = IsNull();
879 int* p1 = NULL;
880 int n = 0;
881 EXPECT_TRUE(m1.Matches(p1));
882 EXPECT_FALSE(m1.Matches(&n));
883
884 Matcher<const char*> m2 = IsNull();
885 const char* p2 = NULL;
886 EXPECT_TRUE(m2.Matches(p2));
887 EXPECT_FALSE(m2.Matches("hi"));
888
zhanyong.wan95b12332009-09-25 18:55:50 +0000889#if !GTEST_OS_SYMBIAN
890 // Nokia's Symbian compiler generates:
891 // gmock-matchers.h: ambiguous access to overloaded function
892 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(void *)'
893 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(const testing::
894 // MatcherInterface<void *> *)'
895 // gmock-matchers.h: (point of instantiation: 'testing::
896 // gmock_matchers_test::IsNullTest_MatchesNullPointer_Test::TestBody()')
897 // gmock-matchers.h: (instantiating: 'testing::PolymorphicMatc
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000898 Matcher<void*> m3 = IsNull();
899 void* p3 = NULL;
900 EXPECT_TRUE(m3.Matches(p3));
901 EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef)));
zhanyong.wan95b12332009-09-25 18:55:50 +0000902#endif
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000903}
904
vladlosev79b83502009-11-18 00:43:37 +0000905TEST(IsNullTest, LinkedPtr) {
906 const Matcher<linked_ptr<int> > m = IsNull();
907 const linked_ptr<int> null_p;
908 const linked_ptr<int> non_null_p(new int);
909
910 EXPECT_TRUE(m.Matches(null_p));
911 EXPECT_FALSE(m.Matches(non_null_p));
912}
913
914TEST(IsNullTest, ReferenceToConstLinkedPtr) {
915 const Matcher<const linked_ptr<double>&> m = IsNull();
916 const linked_ptr<double> null_p;
917 const linked_ptr<double> non_null_p(new double);
918
919 EXPECT_TRUE(m.Matches(null_p));
920 EXPECT_FALSE(m.Matches(non_null_p));
921}
922
vladloseve56daa72009-11-18 01:08:08 +0000923TEST(IsNullTest, ReferenceToConstScopedPtr) {
924 const Matcher<const scoped_ptr<double>&> m = IsNull();
925 const scoped_ptr<double> null_p;
926 const scoped_ptr<double> non_null_p(new double);
927
928 EXPECT_TRUE(m.Matches(null_p));
929 EXPECT_FALSE(m.Matches(non_null_p));
930}
931
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000932// Tests that IsNull() describes itself properly.
933TEST(IsNullTest, CanDescribeSelf) {
934 Matcher<int*> m = IsNull();
935 EXPECT_EQ("is NULL", Describe(m));
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000936 EXPECT_EQ("isn't NULL", DescribeNegation(m));
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000937}
938
shiqiane35fdd92008-12-10 05:08:54 +0000939// Tests that NotNull() matches any non-NULL pointer of any type.
940TEST(NotNullTest, MatchesNonNullPointer) {
941 Matcher<int*> m1 = NotNull();
942 int* p1 = NULL;
943 int n = 0;
944 EXPECT_FALSE(m1.Matches(p1));
945 EXPECT_TRUE(m1.Matches(&n));
946
947 Matcher<const char*> m2 = NotNull();
948 const char* p2 = NULL;
949 EXPECT_FALSE(m2.Matches(p2));
950 EXPECT_TRUE(m2.Matches("hi"));
951}
952
vladlosev79b83502009-11-18 00:43:37 +0000953TEST(NotNullTest, LinkedPtr) {
954 const Matcher<linked_ptr<int> > m = NotNull();
955 const linked_ptr<int> null_p;
956 const linked_ptr<int> non_null_p(new int);
957
958 EXPECT_FALSE(m.Matches(null_p));
959 EXPECT_TRUE(m.Matches(non_null_p));
960}
961
962TEST(NotNullTest, ReferenceToConstLinkedPtr) {
963 const Matcher<const linked_ptr<double>&> m = NotNull();
964 const linked_ptr<double> null_p;
965 const linked_ptr<double> non_null_p(new double);
966
967 EXPECT_FALSE(m.Matches(null_p));
968 EXPECT_TRUE(m.Matches(non_null_p));
969}
970
vladloseve56daa72009-11-18 01:08:08 +0000971TEST(NotNullTest, ReferenceToConstScopedPtr) {
972 const Matcher<const scoped_ptr<double>&> m = NotNull();
973 const scoped_ptr<double> null_p;
974 const scoped_ptr<double> non_null_p(new double);
975
976 EXPECT_FALSE(m.Matches(null_p));
977 EXPECT_TRUE(m.Matches(non_null_p));
978}
979
shiqiane35fdd92008-12-10 05:08:54 +0000980// Tests that NotNull() describes itself properly.
981TEST(NotNullTest, CanDescribeSelf) {
982 Matcher<int*> m = NotNull();
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000983 EXPECT_EQ("isn't NULL", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000984}
985
986// Tests that Ref(variable) matches an argument that references
987// 'variable'.
988TEST(RefTest, MatchesSameVariable) {
989 int a = 0;
990 int b = 0;
991 Matcher<int&> m = Ref(a);
992 EXPECT_TRUE(m.Matches(a));
993 EXPECT_FALSE(m.Matches(b));
994}
995
996// Tests that Ref(variable) describes itself properly.
997TEST(RefTest, CanDescribeSelf) {
998 int n = 5;
999 Matcher<int&> m = Ref(n);
1000 stringstream ss;
1001 ss << "references the variable @" << &n << " 5";
1002 EXPECT_EQ(string(ss.str()), Describe(m));
1003}
1004
1005// Test that Ref(non_const_varialbe) can be used as a matcher for a
1006// const reference.
1007TEST(RefTest, CanBeUsedAsMatcherForConstReference) {
1008 int a = 0;
1009 int b = 0;
1010 Matcher<const int&> m = Ref(a);
1011 EXPECT_TRUE(m.Matches(a));
1012 EXPECT_FALSE(m.Matches(b));
1013}
1014
1015// Tests that Ref(variable) is covariant, i.e. Ref(derived) can be
1016// used wherever Ref(base) can be used (Ref(derived) is a sub-type
1017// of Ref(base), but not vice versa.
1018
shiqiane35fdd92008-12-10 05:08:54 +00001019TEST(RefTest, IsCovariant) {
1020 Base base, base2;
1021 Derived derived;
1022 Matcher<const Base&> m1 = Ref(base);
1023 EXPECT_TRUE(m1.Matches(base));
1024 EXPECT_FALSE(m1.Matches(base2));
1025 EXPECT_FALSE(m1.Matches(derived));
1026
1027 m1 = Ref(derived);
1028 EXPECT_TRUE(m1.Matches(derived));
1029 EXPECT_FALSE(m1.Matches(base));
1030 EXPECT_FALSE(m1.Matches(base2));
1031}
1032
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001033TEST(RefTest, ExplainsResult) {
1034 int n = 0;
1035 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), n),
1036 StartsWith("which is located @"));
1037
1038 int m = 0;
1039 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), m),
1040 StartsWith("which is located @"));
1041}
1042
shiqiane35fdd92008-12-10 05:08:54 +00001043// Tests string comparison matchers.
1044
1045TEST(StrEqTest, MatchesEqualString) {
1046 Matcher<const char*> m = StrEq(string("Hello"));
1047 EXPECT_TRUE(m.Matches("Hello"));
1048 EXPECT_FALSE(m.Matches("hello"));
1049 EXPECT_FALSE(m.Matches(NULL));
1050
1051 Matcher<const string&> m2 = StrEq("Hello");
1052 EXPECT_TRUE(m2.Matches("Hello"));
1053 EXPECT_FALSE(m2.Matches("Hi"));
1054}
1055
1056TEST(StrEqTest, CanDescribeSelf) {
vladlosevaa432202011-04-01 21:58:42 +00001057 Matcher<string> m = StrEq("Hi-\'\"?\\\a\b\f\n\r\t\v\xD3");
1058 EXPECT_EQ("is equal to \"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",
shiqiane35fdd92008-12-10 05:08:54 +00001059 Describe(m));
1060
1061 string str("01204500800");
1062 str[3] = '\0';
1063 Matcher<string> m2 = StrEq(str);
1064 EXPECT_EQ("is equal to \"012\\04500800\"", Describe(m2));
1065 str[0] = str[6] = str[7] = str[9] = str[10] = '\0';
1066 Matcher<string> m3 = StrEq(str);
1067 EXPECT_EQ("is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3));
1068}
1069
1070TEST(StrNeTest, MatchesUnequalString) {
1071 Matcher<const char*> m = StrNe("Hello");
1072 EXPECT_TRUE(m.Matches(""));
1073 EXPECT_TRUE(m.Matches(NULL));
1074 EXPECT_FALSE(m.Matches("Hello"));
1075
1076 Matcher<string> m2 = StrNe(string("Hello"));
1077 EXPECT_TRUE(m2.Matches("hello"));
1078 EXPECT_FALSE(m2.Matches("Hello"));
1079}
1080
1081TEST(StrNeTest, CanDescribeSelf) {
1082 Matcher<const char*> m = StrNe("Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001083 EXPECT_EQ("isn't equal to \"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001084}
1085
1086TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
1087 Matcher<const char*> m = StrCaseEq(string("Hello"));
1088 EXPECT_TRUE(m.Matches("Hello"));
1089 EXPECT_TRUE(m.Matches("hello"));
1090 EXPECT_FALSE(m.Matches("Hi"));
1091 EXPECT_FALSE(m.Matches(NULL));
1092
1093 Matcher<const string&> m2 = StrCaseEq("Hello");
1094 EXPECT_TRUE(m2.Matches("hello"));
1095 EXPECT_FALSE(m2.Matches("Hi"));
1096}
1097
1098TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1099 string str1("oabocdooeoo");
1100 string str2("OABOCDOOEOO");
1101 Matcher<const string&> m0 = StrCaseEq(str1);
1102 EXPECT_FALSE(m0.Matches(str2 + string(1, '\0')));
1103
1104 str1[3] = str2[3] = '\0';
1105 Matcher<const string&> m1 = StrCaseEq(str1);
1106 EXPECT_TRUE(m1.Matches(str2));
1107
1108 str1[0] = str1[6] = str1[7] = str1[10] = '\0';
1109 str2[0] = str2[6] = str2[7] = str2[10] = '\0';
1110 Matcher<const string&> m2 = StrCaseEq(str1);
1111 str1[9] = str2[9] = '\0';
1112 EXPECT_FALSE(m2.Matches(str2));
1113
1114 Matcher<const string&> m3 = StrCaseEq(str1);
1115 EXPECT_TRUE(m3.Matches(str2));
1116
1117 EXPECT_FALSE(m3.Matches(str2 + "x"));
1118 str2.append(1, '\0');
1119 EXPECT_FALSE(m3.Matches(str2));
1120 EXPECT_FALSE(m3.Matches(string(str2, 0, 9)));
1121}
1122
1123TEST(StrCaseEqTest, CanDescribeSelf) {
1124 Matcher<string> m = StrCaseEq("Hi");
1125 EXPECT_EQ("is equal to (ignoring case) \"Hi\"", Describe(m));
1126}
1127
1128TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1129 Matcher<const char*> m = StrCaseNe("Hello");
1130 EXPECT_TRUE(m.Matches("Hi"));
1131 EXPECT_TRUE(m.Matches(NULL));
1132 EXPECT_FALSE(m.Matches("Hello"));
1133 EXPECT_FALSE(m.Matches("hello"));
1134
1135 Matcher<string> m2 = StrCaseNe(string("Hello"));
1136 EXPECT_TRUE(m2.Matches(""));
1137 EXPECT_FALSE(m2.Matches("Hello"));
1138}
1139
1140TEST(StrCaseNeTest, CanDescribeSelf) {
1141 Matcher<const char*> m = StrCaseNe("Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001142 EXPECT_EQ("isn't equal to (ignoring case) \"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001143}
1144
1145// Tests that HasSubstr() works for matching string-typed values.
1146TEST(HasSubstrTest, WorksForStringClasses) {
1147 const Matcher<string> m1 = HasSubstr("foo");
1148 EXPECT_TRUE(m1.Matches(string("I love food.")));
1149 EXPECT_FALSE(m1.Matches(string("tofo")));
1150
1151 const Matcher<const std::string&> m2 = HasSubstr("foo");
1152 EXPECT_TRUE(m2.Matches(std::string("I love food.")));
1153 EXPECT_FALSE(m2.Matches(std::string("tofo")));
1154}
1155
1156// Tests that HasSubstr() works for matching C-string-typed values.
1157TEST(HasSubstrTest, WorksForCStrings) {
1158 const Matcher<char*> m1 = HasSubstr("foo");
1159 EXPECT_TRUE(m1.Matches(const_cast<char*>("I love food.")));
1160 EXPECT_FALSE(m1.Matches(const_cast<char*>("tofo")));
1161 EXPECT_FALSE(m1.Matches(NULL));
1162
1163 const Matcher<const char*> m2 = HasSubstr("foo");
1164 EXPECT_TRUE(m2.Matches("I love food."));
1165 EXPECT_FALSE(m2.Matches("tofo"));
1166 EXPECT_FALSE(m2.Matches(NULL));
1167}
1168
1169// Tests that HasSubstr(s) describes itself properly.
1170TEST(HasSubstrTest, CanDescribeSelf) {
1171 Matcher<string> m = HasSubstr("foo\n\"");
1172 EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m));
1173}
1174
zhanyong.wanb5937da2009-07-16 20:26:41 +00001175TEST(KeyTest, CanDescribeSelf) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001176 Matcher<const pair<std::string, int>&> m = Key("foo");
zhanyong.wanb5937da2009-07-16 20:26:41 +00001177 EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001178 EXPECT_EQ("doesn't have a key that is equal to \"foo\"", DescribeNegation(m));
1179}
1180
1181TEST(KeyTest, ExplainsResult) {
1182 Matcher<pair<int, bool> > m = Key(GreaterThan(10));
1183 EXPECT_EQ("whose first field is a value which is 5 less than 10",
1184 Explain(m, make_pair(5, true)));
1185 EXPECT_EQ("whose first field is a value which is 5 more than 10",
1186 Explain(m, make_pair(15, true)));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001187}
1188
1189TEST(KeyTest, MatchesCorrectly) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001190 pair<int, std::string> p(25, "foo");
zhanyong.wanb5937da2009-07-16 20:26:41 +00001191 EXPECT_THAT(p, Key(25));
1192 EXPECT_THAT(p, Not(Key(42)));
1193 EXPECT_THAT(p, Key(Ge(20)));
1194 EXPECT_THAT(p, Not(Key(Lt(25))));
1195}
1196
1197TEST(KeyTest, SafelyCastsInnerMatcher) {
1198 Matcher<int> is_positive = Gt(0);
1199 Matcher<int> is_negative = Lt(0);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001200 pair<char, bool> p('a', true);
zhanyong.wanb5937da2009-07-16 20:26:41 +00001201 EXPECT_THAT(p, Key(is_positive));
1202 EXPECT_THAT(p, Not(Key(is_negative)));
1203}
1204
1205TEST(KeyTest, InsideContainsUsingMap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001206 map<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001207 container.insert(make_pair(1, 'a'));
1208 container.insert(make_pair(2, 'b'));
1209 container.insert(make_pair(4, 'c'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001210 EXPECT_THAT(container, Contains(Key(1)));
1211 EXPECT_THAT(container, Not(Contains(Key(3))));
1212}
1213
1214TEST(KeyTest, InsideContainsUsingMultimap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001215 multimap<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001216 container.insert(make_pair(1, 'a'));
1217 container.insert(make_pair(2, 'b'));
1218 container.insert(make_pair(4, 'c'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001219
1220 EXPECT_THAT(container, Not(Contains(Key(25))));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001221 container.insert(make_pair(25, 'd'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001222 EXPECT_THAT(container, Contains(Key(25)));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001223 container.insert(make_pair(25, 'e'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001224 EXPECT_THAT(container, Contains(Key(25)));
1225
1226 EXPECT_THAT(container, Contains(Key(1)));
1227 EXPECT_THAT(container, Not(Contains(Key(3))));
1228}
1229
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001230TEST(PairTest, Typing) {
1231 // Test verifies the following type conversions can be compiled.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001232 Matcher<const pair<const char*, int>&> m1 = Pair("foo", 42);
1233 Matcher<const pair<const char*, int> > m2 = Pair("foo", 42);
1234 Matcher<pair<const char*, int> > m3 = Pair("foo", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001235
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001236 Matcher<pair<int, const std::string> > m4 = Pair(25, "42");
1237 Matcher<pair<const std::string, int> > m5 = Pair("25", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001238}
1239
1240TEST(PairTest, CanDescribeSelf) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001241 Matcher<const pair<std::string, int>&> m1 = Pair("foo", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001242 EXPECT_EQ("has a first field that is equal to \"foo\""
1243 ", and has a second field that is equal to 42",
1244 Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001245 EXPECT_EQ("has a first field that isn't equal to \"foo\""
1246 ", or has a second field that isn't equal to 42",
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001247 DescribeNegation(m1));
1248 // Double and triple negation (1 or 2 times not and description of negation).
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001249 Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42));
1250 EXPECT_EQ("has a first field that isn't equal to 13"
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001251 ", and has a second field that is equal to 42",
1252 DescribeNegation(m2));
1253}
1254
1255TEST(PairTest, CanExplainMatchResultTo) {
zhanyong.wan82113312010-01-08 21:55:40 +00001256 // If neither field matches, Pair() should explain about the first
1257 // field.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001258 const Matcher<pair<int, int> > m = Pair(GreaterThan(0), GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001259 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001260 Explain(m, make_pair(-1, -2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001261
zhanyong.wan82113312010-01-08 21:55:40 +00001262 // If the first field matches but the second doesn't, Pair() should
1263 // explain about the second field.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001264 EXPECT_EQ("whose second field does not match, which is 2 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001265 Explain(m, make_pair(1, -2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001266
zhanyong.wan82113312010-01-08 21:55:40 +00001267 // If the first field doesn't match but the second does, Pair()
1268 // should explain about the first field.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001269 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001270 Explain(m, make_pair(-1, 2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001271
zhanyong.wan82113312010-01-08 21:55:40 +00001272 // If both fields match, Pair() should explain about them both.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001273 EXPECT_EQ("whose both fields match, where the first field is a value "
1274 "which is 1 more than 0, and the second field is a value "
1275 "which is 2 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001276 Explain(m, make_pair(1, 2)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001277
1278 // If only the first match has an explanation, only this explanation should
1279 // be printed.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001280 const Matcher<pair<int, int> > explain_first = Pair(GreaterThan(0), 0);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001281 EXPECT_EQ("whose both fields match, where the first field is a value "
1282 "which is 1 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001283 Explain(explain_first, make_pair(1, 0)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001284
1285 // If only the second match has an explanation, only this explanation should
1286 // be printed.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001287 const Matcher<pair<int, int> > explain_second = Pair(0, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001288 EXPECT_EQ("whose both fields match, where the second field is a value "
1289 "which is 1 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001290 Explain(explain_second, make_pair(0, 1)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001291}
1292
1293TEST(PairTest, MatchesCorrectly) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001294 pair<int, std::string> p(25, "foo");
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001295
1296 // Both fields match.
1297 EXPECT_THAT(p, Pair(25, "foo"));
1298 EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o")));
1299
1300 // 'first' doesnt' match, but 'second' matches.
1301 EXPECT_THAT(p, Not(Pair(42, "foo")));
1302 EXPECT_THAT(p, Not(Pair(Lt(25), "foo")));
1303
1304 // 'first' matches, but 'second' doesn't match.
1305 EXPECT_THAT(p, Not(Pair(25, "bar")));
1306 EXPECT_THAT(p, Not(Pair(25, Not("foo"))));
1307
1308 // Neither field matches.
1309 EXPECT_THAT(p, Not(Pair(13, "bar")));
1310 EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a"))));
1311}
1312
1313TEST(PairTest, SafelyCastsInnerMatchers) {
1314 Matcher<int> is_positive = Gt(0);
1315 Matcher<int> is_negative = Lt(0);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001316 pair<char, bool> p('a', true);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001317 EXPECT_THAT(p, Pair(is_positive, _));
1318 EXPECT_THAT(p, Not(Pair(is_negative, _)));
1319 EXPECT_THAT(p, Pair(_, is_positive));
1320 EXPECT_THAT(p, Not(Pair(_, is_negative)));
1321}
1322
1323TEST(PairTest, InsideContainsUsingMap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001324 map<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001325 container.insert(make_pair(1, 'a'));
1326 container.insert(make_pair(2, 'b'));
1327 container.insert(make_pair(4, 'c'));
zhanyong.wan95b12332009-09-25 18:55:50 +00001328 EXPECT_THAT(container, Contains(Pair(1, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001329 EXPECT_THAT(container, Contains(Pair(1, _)));
zhanyong.wan95b12332009-09-25 18:55:50 +00001330 EXPECT_THAT(container, Contains(Pair(_, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001331 EXPECT_THAT(container, Not(Contains(Pair(3, _))));
1332}
1333
shiqiane35fdd92008-12-10 05:08:54 +00001334// Tests StartsWith(s).
1335
1336TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
1337 const Matcher<const char*> m1 = StartsWith(string(""));
1338 EXPECT_TRUE(m1.Matches("Hi"));
1339 EXPECT_TRUE(m1.Matches(""));
1340 EXPECT_FALSE(m1.Matches(NULL));
1341
1342 const Matcher<const string&> m2 = StartsWith("Hi");
1343 EXPECT_TRUE(m2.Matches("Hi"));
1344 EXPECT_TRUE(m2.Matches("Hi Hi!"));
1345 EXPECT_TRUE(m2.Matches("High"));
1346 EXPECT_FALSE(m2.Matches("H"));
1347 EXPECT_FALSE(m2.Matches(" Hi"));
1348}
1349
1350TEST(StartsWithTest, CanDescribeSelf) {
1351 Matcher<const std::string> m = StartsWith("Hi");
1352 EXPECT_EQ("starts with \"Hi\"", Describe(m));
1353}
1354
1355// Tests EndsWith(s).
1356
1357TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
1358 const Matcher<const char*> m1 = EndsWith("");
1359 EXPECT_TRUE(m1.Matches("Hi"));
1360 EXPECT_TRUE(m1.Matches(""));
1361 EXPECT_FALSE(m1.Matches(NULL));
1362
1363 const Matcher<const string&> m2 = EndsWith(string("Hi"));
1364 EXPECT_TRUE(m2.Matches("Hi"));
1365 EXPECT_TRUE(m2.Matches("Wow Hi Hi"));
1366 EXPECT_TRUE(m2.Matches("Super Hi"));
1367 EXPECT_FALSE(m2.Matches("i"));
1368 EXPECT_FALSE(m2.Matches("Hi "));
1369}
1370
1371TEST(EndsWithTest, CanDescribeSelf) {
1372 Matcher<const std::string> m = EndsWith("Hi");
1373 EXPECT_EQ("ends with \"Hi\"", Describe(m));
1374}
1375
shiqiane35fdd92008-12-10 05:08:54 +00001376// Tests MatchesRegex().
1377
1378TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
1379 const Matcher<const char*> m1 = MatchesRegex("a.*z");
1380 EXPECT_TRUE(m1.Matches("az"));
1381 EXPECT_TRUE(m1.Matches("abcz"));
1382 EXPECT_FALSE(m1.Matches(NULL));
1383
1384 const Matcher<const string&> m2 = MatchesRegex(new RE("a.*z"));
1385 EXPECT_TRUE(m2.Matches("azbz"));
1386 EXPECT_FALSE(m2.Matches("az1"));
1387 EXPECT_FALSE(m2.Matches("1az"));
1388}
1389
1390TEST(MatchesRegexTest, CanDescribeSelf) {
1391 Matcher<const std::string> m1 = MatchesRegex(string("Hi.*"));
1392 EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1));
1393
zhanyong.wand14aaed2010-01-14 05:36:32 +00001394 Matcher<const char*> m2 = MatchesRegex(new RE("a.*"));
1395 EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +00001396}
1397
1398// Tests ContainsRegex().
1399
1400TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
1401 const Matcher<const char*> m1 = ContainsRegex(string("a.*z"));
1402 EXPECT_TRUE(m1.Matches("az"));
1403 EXPECT_TRUE(m1.Matches("0abcz1"));
1404 EXPECT_FALSE(m1.Matches(NULL));
1405
1406 const Matcher<const string&> m2 = ContainsRegex(new RE("a.*z"));
1407 EXPECT_TRUE(m2.Matches("azbz"));
1408 EXPECT_TRUE(m2.Matches("az1"));
1409 EXPECT_FALSE(m2.Matches("1a"));
1410}
1411
1412TEST(ContainsRegexTest, CanDescribeSelf) {
1413 Matcher<const std::string> m1 = ContainsRegex("Hi.*");
1414 EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1));
1415
zhanyong.wand14aaed2010-01-14 05:36:32 +00001416 Matcher<const char*> m2 = ContainsRegex(new RE("a.*"));
1417 EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +00001418}
shiqiane35fdd92008-12-10 05:08:54 +00001419
1420// Tests for wide strings.
1421#if GTEST_HAS_STD_WSTRING
1422TEST(StdWideStrEqTest, MatchesEqual) {
1423 Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello"));
1424 EXPECT_TRUE(m.Matches(L"Hello"));
1425 EXPECT_FALSE(m.Matches(L"hello"));
1426 EXPECT_FALSE(m.Matches(NULL));
1427
1428 Matcher<const ::std::wstring&> m2 = StrEq(L"Hello");
1429 EXPECT_TRUE(m2.Matches(L"Hello"));
1430 EXPECT_FALSE(m2.Matches(L"Hi"));
1431
1432 Matcher<const ::std::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1433 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1434 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1435
1436 ::std::wstring str(L"01204500800");
1437 str[3] = L'\0';
1438 Matcher<const ::std::wstring&> m4 = StrEq(str);
1439 EXPECT_TRUE(m4.Matches(str));
1440 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1441 Matcher<const ::std::wstring&> m5 = StrEq(str);
1442 EXPECT_TRUE(m5.Matches(str));
1443}
1444
1445TEST(StdWideStrEqTest, CanDescribeSelf) {
vladlosevaa432202011-04-01 21:58:42 +00001446 Matcher< ::std::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v");
1447 EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
shiqiane35fdd92008-12-10 05:08:54 +00001448 Describe(m));
1449
1450 Matcher< ::std::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1451 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1452 Describe(m2));
1453
1454 ::std::wstring str(L"01204500800");
1455 str[3] = L'\0';
1456 Matcher<const ::std::wstring&> m4 = StrEq(str);
1457 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1458 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1459 Matcher<const ::std::wstring&> m5 = StrEq(str);
1460 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1461}
1462
1463TEST(StdWideStrNeTest, MatchesUnequalString) {
1464 Matcher<const wchar_t*> m = StrNe(L"Hello");
1465 EXPECT_TRUE(m.Matches(L""));
1466 EXPECT_TRUE(m.Matches(NULL));
1467 EXPECT_FALSE(m.Matches(L"Hello"));
1468
1469 Matcher< ::std::wstring> m2 = StrNe(::std::wstring(L"Hello"));
1470 EXPECT_TRUE(m2.Matches(L"hello"));
1471 EXPECT_FALSE(m2.Matches(L"Hello"));
1472}
1473
1474TEST(StdWideStrNeTest, CanDescribeSelf) {
1475 Matcher<const wchar_t*> m = StrNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001476 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001477}
1478
1479TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1480 Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello"));
1481 EXPECT_TRUE(m.Matches(L"Hello"));
1482 EXPECT_TRUE(m.Matches(L"hello"));
1483 EXPECT_FALSE(m.Matches(L"Hi"));
1484 EXPECT_FALSE(m.Matches(NULL));
1485
1486 Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello");
1487 EXPECT_TRUE(m2.Matches(L"hello"));
1488 EXPECT_FALSE(m2.Matches(L"Hi"));
1489}
1490
1491TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1492 ::std::wstring str1(L"oabocdooeoo");
1493 ::std::wstring str2(L"OABOCDOOEOO");
1494 Matcher<const ::std::wstring&> m0 = StrCaseEq(str1);
1495 EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L'\0')));
1496
1497 str1[3] = str2[3] = L'\0';
1498 Matcher<const ::std::wstring&> m1 = StrCaseEq(str1);
1499 EXPECT_TRUE(m1.Matches(str2));
1500
1501 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1502 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1503 Matcher<const ::std::wstring&> m2 = StrCaseEq(str1);
1504 str1[9] = str2[9] = L'\0';
1505 EXPECT_FALSE(m2.Matches(str2));
1506
1507 Matcher<const ::std::wstring&> m3 = StrCaseEq(str1);
1508 EXPECT_TRUE(m3.Matches(str2));
1509
1510 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1511 str2.append(1, L'\0');
1512 EXPECT_FALSE(m3.Matches(str2));
1513 EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9)));
1514}
1515
1516TEST(StdWideStrCaseEqTest, CanDescribeSelf) {
1517 Matcher< ::std::wstring> m = StrCaseEq(L"Hi");
1518 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1519}
1520
1521TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1522 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1523 EXPECT_TRUE(m.Matches(L"Hi"));
1524 EXPECT_TRUE(m.Matches(NULL));
1525 EXPECT_FALSE(m.Matches(L"Hello"));
1526 EXPECT_FALSE(m.Matches(L"hello"));
1527
1528 Matcher< ::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello"));
1529 EXPECT_TRUE(m2.Matches(L""));
1530 EXPECT_FALSE(m2.Matches(L"Hello"));
1531}
1532
1533TEST(StdWideStrCaseNeTest, CanDescribeSelf) {
1534 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001535 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001536}
1537
1538// Tests that HasSubstr() works for matching wstring-typed values.
1539TEST(StdWideHasSubstrTest, WorksForStringClasses) {
1540 const Matcher< ::std::wstring> m1 = HasSubstr(L"foo");
1541 EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food.")));
1542 EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo")));
1543
1544 const Matcher<const ::std::wstring&> m2 = HasSubstr(L"foo");
1545 EXPECT_TRUE(m2.Matches(::std::wstring(L"I love food.")));
1546 EXPECT_FALSE(m2.Matches(::std::wstring(L"tofo")));
1547}
1548
1549// Tests that HasSubstr() works for matching C-wide-string-typed values.
1550TEST(StdWideHasSubstrTest, WorksForCStrings) {
1551 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1552 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1553 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1554 EXPECT_FALSE(m1.Matches(NULL));
1555
1556 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1557 EXPECT_TRUE(m2.Matches(L"I love food."));
1558 EXPECT_FALSE(m2.Matches(L"tofo"));
1559 EXPECT_FALSE(m2.Matches(NULL));
1560}
1561
1562// Tests that HasSubstr(s) describes itself properly.
1563TEST(StdWideHasSubstrTest, CanDescribeSelf) {
1564 Matcher< ::std::wstring> m = HasSubstr(L"foo\n\"");
1565 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1566}
1567
1568// Tests StartsWith(s).
1569
1570TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) {
1571 const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L""));
1572 EXPECT_TRUE(m1.Matches(L"Hi"));
1573 EXPECT_TRUE(m1.Matches(L""));
1574 EXPECT_FALSE(m1.Matches(NULL));
1575
1576 const Matcher<const ::std::wstring&> m2 = StartsWith(L"Hi");
1577 EXPECT_TRUE(m2.Matches(L"Hi"));
1578 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1579 EXPECT_TRUE(m2.Matches(L"High"));
1580 EXPECT_FALSE(m2.Matches(L"H"));
1581 EXPECT_FALSE(m2.Matches(L" Hi"));
1582}
1583
1584TEST(StdWideStartsWithTest, CanDescribeSelf) {
1585 Matcher<const ::std::wstring> m = StartsWith(L"Hi");
1586 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1587}
1588
1589// Tests EndsWith(s).
1590
1591TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) {
1592 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1593 EXPECT_TRUE(m1.Matches(L"Hi"));
1594 EXPECT_TRUE(m1.Matches(L""));
1595 EXPECT_FALSE(m1.Matches(NULL));
1596
1597 const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L"Hi"));
1598 EXPECT_TRUE(m2.Matches(L"Hi"));
1599 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1600 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1601 EXPECT_FALSE(m2.Matches(L"i"));
1602 EXPECT_FALSE(m2.Matches(L"Hi "));
1603}
1604
1605TEST(StdWideEndsWithTest, CanDescribeSelf) {
1606 Matcher<const ::std::wstring> m = EndsWith(L"Hi");
1607 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1608}
1609
1610#endif // GTEST_HAS_STD_WSTRING
1611
1612#if GTEST_HAS_GLOBAL_WSTRING
1613TEST(GlobalWideStrEqTest, MatchesEqual) {
1614 Matcher<const wchar_t*> m = StrEq(::wstring(L"Hello"));
1615 EXPECT_TRUE(m.Matches(L"Hello"));
1616 EXPECT_FALSE(m.Matches(L"hello"));
1617 EXPECT_FALSE(m.Matches(NULL));
1618
1619 Matcher<const ::wstring&> m2 = StrEq(L"Hello");
1620 EXPECT_TRUE(m2.Matches(L"Hello"));
1621 EXPECT_FALSE(m2.Matches(L"Hi"));
1622
1623 Matcher<const ::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1624 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1625 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1626
1627 ::wstring str(L"01204500800");
1628 str[3] = L'\0';
1629 Matcher<const ::wstring&> m4 = StrEq(str);
1630 EXPECT_TRUE(m4.Matches(str));
1631 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1632 Matcher<const ::wstring&> m5 = StrEq(str);
1633 EXPECT_TRUE(m5.Matches(str));
1634}
1635
1636TEST(GlobalWideStrEqTest, CanDescribeSelf) {
vladlosevaa432202011-04-01 21:58:42 +00001637 Matcher< ::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v");
1638 EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
shiqiane35fdd92008-12-10 05:08:54 +00001639 Describe(m));
1640
1641 Matcher< ::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1642 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1643 Describe(m2));
1644
1645 ::wstring str(L"01204500800");
1646 str[3] = L'\0';
1647 Matcher<const ::wstring&> m4 = StrEq(str);
1648 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1649 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1650 Matcher<const ::wstring&> m5 = StrEq(str);
1651 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1652}
1653
1654TEST(GlobalWideStrNeTest, MatchesUnequalString) {
1655 Matcher<const wchar_t*> m = StrNe(L"Hello");
1656 EXPECT_TRUE(m.Matches(L""));
1657 EXPECT_TRUE(m.Matches(NULL));
1658 EXPECT_FALSE(m.Matches(L"Hello"));
1659
1660 Matcher< ::wstring> m2 = StrNe(::wstring(L"Hello"));
1661 EXPECT_TRUE(m2.Matches(L"hello"));
1662 EXPECT_FALSE(m2.Matches(L"Hello"));
1663}
1664
1665TEST(GlobalWideStrNeTest, CanDescribeSelf) {
1666 Matcher<const wchar_t*> m = StrNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001667 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001668}
1669
1670TEST(GlobalWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1671 Matcher<const wchar_t*> m = StrCaseEq(::wstring(L"Hello"));
1672 EXPECT_TRUE(m.Matches(L"Hello"));
1673 EXPECT_TRUE(m.Matches(L"hello"));
1674 EXPECT_FALSE(m.Matches(L"Hi"));
1675 EXPECT_FALSE(m.Matches(NULL));
1676
1677 Matcher<const ::wstring&> m2 = StrCaseEq(L"Hello");
1678 EXPECT_TRUE(m2.Matches(L"hello"));
1679 EXPECT_FALSE(m2.Matches(L"Hi"));
1680}
1681
1682TEST(GlobalWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1683 ::wstring str1(L"oabocdooeoo");
1684 ::wstring str2(L"OABOCDOOEOO");
1685 Matcher<const ::wstring&> m0 = StrCaseEq(str1);
1686 EXPECT_FALSE(m0.Matches(str2 + ::wstring(1, L'\0')));
1687
1688 str1[3] = str2[3] = L'\0';
1689 Matcher<const ::wstring&> m1 = StrCaseEq(str1);
1690 EXPECT_TRUE(m1.Matches(str2));
1691
1692 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1693 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1694 Matcher<const ::wstring&> m2 = StrCaseEq(str1);
1695 str1[9] = str2[9] = L'\0';
1696 EXPECT_FALSE(m2.Matches(str2));
1697
1698 Matcher<const ::wstring&> m3 = StrCaseEq(str1);
1699 EXPECT_TRUE(m3.Matches(str2));
1700
1701 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1702 str2.append(1, L'\0');
1703 EXPECT_FALSE(m3.Matches(str2));
1704 EXPECT_FALSE(m3.Matches(::wstring(str2, 0, 9)));
1705}
1706
1707TEST(GlobalWideStrCaseEqTest, CanDescribeSelf) {
1708 Matcher< ::wstring> m = StrCaseEq(L"Hi");
1709 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1710}
1711
1712TEST(GlobalWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1713 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1714 EXPECT_TRUE(m.Matches(L"Hi"));
1715 EXPECT_TRUE(m.Matches(NULL));
1716 EXPECT_FALSE(m.Matches(L"Hello"));
1717 EXPECT_FALSE(m.Matches(L"hello"));
1718
1719 Matcher< ::wstring> m2 = StrCaseNe(::wstring(L"Hello"));
1720 EXPECT_TRUE(m2.Matches(L""));
1721 EXPECT_FALSE(m2.Matches(L"Hello"));
1722}
1723
1724TEST(GlobalWideStrCaseNeTest, CanDescribeSelf) {
1725 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001726 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001727}
1728
1729// Tests that HasSubstr() works for matching wstring-typed values.
1730TEST(GlobalWideHasSubstrTest, WorksForStringClasses) {
1731 const Matcher< ::wstring> m1 = HasSubstr(L"foo");
1732 EXPECT_TRUE(m1.Matches(::wstring(L"I love food.")));
1733 EXPECT_FALSE(m1.Matches(::wstring(L"tofo")));
1734
1735 const Matcher<const ::wstring&> m2 = HasSubstr(L"foo");
1736 EXPECT_TRUE(m2.Matches(::wstring(L"I love food.")));
1737 EXPECT_FALSE(m2.Matches(::wstring(L"tofo")));
1738}
1739
1740// Tests that HasSubstr() works for matching C-wide-string-typed values.
1741TEST(GlobalWideHasSubstrTest, WorksForCStrings) {
1742 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1743 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1744 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1745 EXPECT_FALSE(m1.Matches(NULL));
1746
1747 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1748 EXPECT_TRUE(m2.Matches(L"I love food."));
1749 EXPECT_FALSE(m2.Matches(L"tofo"));
1750 EXPECT_FALSE(m2.Matches(NULL));
1751}
1752
1753// Tests that HasSubstr(s) describes itself properly.
1754TEST(GlobalWideHasSubstrTest, CanDescribeSelf) {
1755 Matcher< ::wstring> m = HasSubstr(L"foo\n\"");
1756 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1757}
1758
1759// Tests StartsWith(s).
1760
1761TEST(GlobalWideStartsWithTest, MatchesStringWithGivenPrefix) {
1762 const Matcher<const wchar_t*> m1 = StartsWith(::wstring(L""));
1763 EXPECT_TRUE(m1.Matches(L"Hi"));
1764 EXPECT_TRUE(m1.Matches(L""));
1765 EXPECT_FALSE(m1.Matches(NULL));
1766
1767 const Matcher<const ::wstring&> m2 = StartsWith(L"Hi");
1768 EXPECT_TRUE(m2.Matches(L"Hi"));
1769 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1770 EXPECT_TRUE(m2.Matches(L"High"));
1771 EXPECT_FALSE(m2.Matches(L"H"));
1772 EXPECT_FALSE(m2.Matches(L" Hi"));
1773}
1774
1775TEST(GlobalWideStartsWithTest, CanDescribeSelf) {
1776 Matcher<const ::wstring> m = StartsWith(L"Hi");
1777 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1778}
1779
1780// Tests EndsWith(s).
1781
1782TEST(GlobalWideEndsWithTest, MatchesStringWithGivenSuffix) {
1783 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1784 EXPECT_TRUE(m1.Matches(L"Hi"));
1785 EXPECT_TRUE(m1.Matches(L""));
1786 EXPECT_FALSE(m1.Matches(NULL));
1787
1788 const Matcher<const ::wstring&> m2 = EndsWith(::wstring(L"Hi"));
1789 EXPECT_TRUE(m2.Matches(L"Hi"));
1790 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1791 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1792 EXPECT_FALSE(m2.Matches(L"i"));
1793 EXPECT_FALSE(m2.Matches(L"Hi "));
1794}
1795
1796TEST(GlobalWideEndsWithTest, CanDescribeSelf) {
1797 Matcher<const ::wstring> m = EndsWith(L"Hi");
1798 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1799}
1800
1801#endif // GTEST_HAS_GLOBAL_WSTRING
1802
1803
1804typedef ::std::tr1::tuple<long, int> Tuple2; // NOLINT
1805
1806// Tests that Eq() matches a 2-tuple where the first field == the
1807// second field.
1808TEST(Eq2Test, MatchesEqualArguments) {
1809 Matcher<const Tuple2&> m = Eq();
1810 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1811 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1812}
1813
1814// Tests that Eq() describes itself properly.
1815TEST(Eq2Test, CanDescribeSelf) {
1816 Matcher<const Tuple2&> m = Eq();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001817 EXPECT_EQ("are an equal pair", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001818}
1819
1820// Tests that Ge() matches a 2-tuple where the first field >= the
1821// second field.
1822TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) {
1823 Matcher<const Tuple2&> m = Ge();
1824 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1825 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1826 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1827}
1828
1829// Tests that Ge() describes itself properly.
1830TEST(Ge2Test, CanDescribeSelf) {
1831 Matcher<const Tuple2&> m = Ge();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001832 EXPECT_EQ("are a pair where the first >= the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001833}
1834
1835// Tests that Gt() matches a 2-tuple where the first field > the
1836// second field.
1837TEST(Gt2Test, MatchesGreaterThanArguments) {
1838 Matcher<const Tuple2&> m = Gt();
1839 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1840 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1841 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1842}
1843
1844// Tests that Gt() describes itself properly.
1845TEST(Gt2Test, CanDescribeSelf) {
1846 Matcher<const Tuple2&> m = Gt();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001847 EXPECT_EQ("are a pair where the first > the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001848}
1849
1850// Tests that Le() matches a 2-tuple where the first field <= the
1851// second field.
1852TEST(Le2Test, MatchesLessThanOrEqualArguments) {
1853 Matcher<const Tuple2&> m = Le();
1854 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1855 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1856 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1857}
1858
1859// Tests that Le() describes itself properly.
1860TEST(Le2Test, CanDescribeSelf) {
1861 Matcher<const Tuple2&> m = Le();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001862 EXPECT_EQ("are a pair where the first <= the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001863}
1864
1865// Tests that Lt() matches a 2-tuple where the first field < the
1866// second field.
1867TEST(Lt2Test, MatchesLessThanArguments) {
1868 Matcher<const Tuple2&> m = Lt();
1869 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1870 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1871 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1872}
1873
1874// Tests that Lt() describes itself properly.
1875TEST(Lt2Test, CanDescribeSelf) {
1876 Matcher<const Tuple2&> m = Lt();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001877 EXPECT_EQ("are a pair where the first < the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001878}
1879
1880// Tests that Ne() matches a 2-tuple where the first field != the
1881// second field.
1882TEST(Ne2Test, MatchesUnequalArguments) {
1883 Matcher<const Tuple2&> m = Ne();
1884 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1885 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1886 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1887}
1888
1889// Tests that Ne() describes itself properly.
1890TEST(Ne2Test, CanDescribeSelf) {
1891 Matcher<const Tuple2&> m = Ne();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001892 EXPECT_EQ("are an unequal pair", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001893}
1894
1895// Tests that Not(m) matches any value that doesn't match m.
1896TEST(NotTest, NegatesMatcher) {
1897 Matcher<int> m;
1898 m = Not(Eq(2));
1899 EXPECT_TRUE(m.Matches(3));
1900 EXPECT_FALSE(m.Matches(2));
1901}
1902
1903// Tests that Not(m) describes itself properly.
1904TEST(NotTest, CanDescribeSelf) {
1905 Matcher<int> m = Not(Eq(5));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001906 EXPECT_EQ("isn't equal to 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001907}
1908
zhanyong.wan18490652009-05-11 18:54:08 +00001909// Tests that monomorphic matchers are safely cast by the Not matcher.
1910TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {
1911 // greater_than_5 is a monomorphic matcher.
1912 Matcher<int> greater_than_5 = Gt(5);
1913
1914 Matcher<const int&> m = Not(greater_than_5);
1915 Matcher<int&> m2 = Not(greater_than_5);
1916 Matcher<int&> m3 = Not(m);
1917}
1918
zhanyong.wan02c15052010-06-09 19:21:30 +00001919// Helper to allow easy testing of AllOf matchers with num parameters.
1920void AllOfMatches(int num, const Matcher<int>& m) {
1921 SCOPED_TRACE(Describe(m));
1922 EXPECT_TRUE(m.Matches(0));
1923 for (int i = 1; i <= num; ++i) {
1924 EXPECT_FALSE(m.Matches(i));
1925 }
1926 EXPECT_TRUE(m.Matches(num + 1));
1927}
1928
shiqiane35fdd92008-12-10 05:08:54 +00001929// Tests that AllOf(m1, ..., mn) matches any value that matches all of
1930// the given matchers.
1931TEST(AllOfTest, MatchesWhenAllMatch) {
1932 Matcher<int> m;
1933 m = AllOf(Le(2), Ge(1));
1934 EXPECT_TRUE(m.Matches(1));
1935 EXPECT_TRUE(m.Matches(2));
1936 EXPECT_FALSE(m.Matches(0));
1937 EXPECT_FALSE(m.Matches(3));
1938
1939 m = AllOf(Gt(0), Ne(1), Ne(2));
1940 EXPECT_TRUE(m.Matches(3));
1941 EXPECT_FALSE(m.Matches(2));
1942 EXPECT_FALSE(m.Matches(1));
1943 EXPECT_FALSE(m.Matches(0));
1944
1945 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
1946 EXPECT_TRUE(m.Matches(4));
1947 EXPECT_FALSE(m.Matches(3));
1948 EXPECT_FALSE(m.Matches(2));
1949 EXPECT_FALSE(m.Matches(1));
1950 EXPECT_FALSE(m.Matches(0));
1951
1952 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
1953 EXPECT_TRUE(m.Matches(0));
1954 EXPECT_TRUE(m.Matches(1));
1955 EXPECT_FALSE(m.Matches(3));
zhanyong.wan02c15052010-06-09 19:21:30 +00001956
1957 // The following tests for varying number of sub-matchers. Due to the way
1958 // the sub-matchers are handled it is enough to test every sub-matcher once
1959 // with sub-matchers using the same matcher type. Varying matcher types are
1960 // checked for above.
1961 AllOfMatches(2, AllOf(Ne(1), Ne(2)));
1962 AllOfMatches(3, AllOf(Ne(1), Ne(2), Ne(3)));
1963 AllOfMatches(4, AllOf(Ne(1), Ne(2), Ne(3), Ne(4)));
1964 AllOfMatches(5, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5)));
1965 AllOfMatches(6, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6)));
1966 AllOfMatches(7, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7)));
1967 AllOfMatches(8, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
1968 Ne(8)));
1969 AllOfMatches(9, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
1970 Ne(8), Ne(9)));
1971 AllOfMatches(10, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8),
1972 Ne(9), Ne(10)));
shiqiane35fdd92008-12-10 05:08:54 +00001973}
1974
1975// Tests that AllOf(m1, ..., mn) describes itself properly.
1976TEST(AllOfTest, CanDescribeSelf) {
1977 Matcher<int> m;
1978 m = AllOf(Le(2), Ge(1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001979 EXPECT_EQ("(is <= 2) and (is >= 1)", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001980
1981 m = AllOf(Gt(0), Ne(1), Ne(2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001982 EXPECT_EQ("(is > 0) and "
1983 "((isn't equal to 1) and "
1984 "(isn't equal to 2))",
shiqiane35fdd92008-12-10 05:08:54 +00001985 Describe(m));
1986
1987
1988 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
jgm79a367e2012-04-10 16:02:11 +00001989 EXPECT_EQ("((is > 0) and "
1990 "(isn't equal to 1)) and "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001991 "((isn't equal to 2) and "
jgm79a367e2012-04-10 16:02:11 +00001992 "(isn't equal to 3))",
shiqiane35fdd92008-12-10 05:08:54 +00001993 Describe(m));
1994
1995
1996 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
jgm79a367e2012-04-10 16:02:11 +00001997 EXPECT_EQ("((is >= 0) and "
1998 "(is < 10)) and "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001999 "((isn't equal to 3) and "
2000 "((isn't equal to 5) and "
jgm79a367e2012-04-10 16:02:11 +00002001 "(isn't equal to 7)))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002002 Describe(m));
2003}
2004
2005// Tests that AllOf(m1, ..., mn) describes its negation properly.
2006TEST(AllOfTest, CanDescribeNegation) {
2007 Matcher<int> m;
2008 m = AllOf(Le(2), Ge(1));
2009 EXPECT_EQ("(isn't <= 2) or "
2010 "(isn't >= 1)",
2011 DescribeNegation(m));
2012
2013 m = AllOf(Gt(0), Ne(1), Ne(2));
2014 EXPECT_EQ("(isn't > 0) or "
2015 "((is equal to 1) or "
2016 "(is equal to 2))",
2017 DescribeNegation(m));
2018
2019
2020 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
jgm79a367e2012-04-10 16:02:11 +00002021 EXPECT_EQ("((isn't > 0) or "
2022 "(is equal to 1)) or "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002023 "((is equal to 2) or "
jgm79a367e2012-04-10 16:02:11 +00002024 "(is equal to 3))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002025 DescribeNegation(m));
2026
2027
2028 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
jgm79a367e2012-04-10 16:02:11 +00002029 EXPECT_EQ("((isn't >= 0) or "
2030 "(isn't < 10)) or "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002031 "((is equal to 3) or "
2032 "((is equal to 5) or "
jgm79a367e2012-04-10 16:02:11 +00002033 "(is equal to 7)))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002034 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00002035}
2036
zhanyong.wan18490652009-05-11 18:54:08 +00002037// Tests that monomorphic matchers are safely cast by the AllOf matcher.
2038TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
2039 // greater_than_5 and less_than_10 are monomorphic matchers.
2040 Matcher<int> greater_than_5 = Gt(5);
2041 Matcher<int> less_than_10 = Lt(10);
2042
2043 Matcher<const int&> m = AllOf(greater_than_5, less_than_10);
2044 Matcher<int&> m2 = AllOf(greater_than_5, less_than_10);
2045 Matcher<int&> m3 = AllOf(greater_than_5, m2);
2046
2047 // Tests that BothOf works when composing itself.
2048 Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10);
2049 Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);
2050}
2051
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002052TEST(AllOfTest, ExplainsResult) {
2053 Matcher<int> m;
2054
2055 // Successful match. Both matchers need to explain. The second
2056 // matcher doesn't give an explanation, so only the first matcher's
2057 // explanation is printed.
2058 m = AllOf(GreaterThan(10), Lt(30));
2059 EXPECT_EQ("which is 15 more than 10", Explain(m, 25));
2060
2061 // Successful match. Both matchers need to explain.
2062 m = AllOf(GreaterThan(10), GreaterThan(20));
2063 EXPECT_EQ("which is 20 more than 10, and which is 10 more than 20",
2064 Explain(m, 30));
2065
2066 // Successful match. All matchers need to explain. The second
2067 // matcher doesn't given an explanation.
2068 m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20));
2069 EXPECT_EQ("which is 15 more than 10, and which is 5 more than 20",
2070 Explain(m, 25));
2071
2072 // Successful match. All matchers need to explain.
2073 m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2074 EXPECT_EQ("which is 30 more than 10, and which is 20 more than 20, "
2075 "and which is 10 more than 30",
2076 Explain(m, 40));
2077
2078 // Failed match. The first matcher, which failed, needs to
2079 // explain.
2080 m = AllOf(GreaterThan(10), GreaterThan(20));
2081 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
2082
2083 // Failed match. The second matcher, which failed, needs to
2084 // explain. Since it doesn't given an explanation, nothing is
2085 // printed.
2086 m = AllOf(GreaterThan(10), Lt(30));
2087 EXPECT_EQ("", Explain(m, 40));
2088
2089 // Failed match. The second matcher, which failed, needs to
2090 // explain.
2091 m = AllOf(GreaterThan(10), GreaterThan(20));
2092 EXPECT_EQ("which is 5 less than 20", Explain(m, 15));
2093}
2094
zhanyong.wan02c15052010-06-09 19:21:30 +00002095// Helper to allow easy testing of AnyOf matchers with num parameters.
2096void AnyOfMatches(int num, const Matcher<int>& m) {
2097 SCOPED_TRACE(Describe(m));
2098 EXPECT_FALSE(m.Matches(0));
2099 for (int i = 1; i <= num; ++i) {
2100 EXPECT_TRUE(m.Matches(i));
2101 }
2102 EXPECT_FALSE(m.Matches(num + 1));
2103}
2104
shiqiane35fdd92008-12-10 05:08:54 +00002105// Tests that AnyOf(m1, ..., mn) matches any value that matches at
2106// least one of the given matchers.
2107TEST(AnyOfTest, MatchesWhenAnyMatches) {
2108 Matcher<int> m;
2109 m = AnyOf(Le(1), Ge(3));
2110 EXPECT_TRUE(m.Matches(1));
2111 EXPECT_TRUE(m.Matches(4));
2112 EXPECT_FALSE(m.Matches(2));
2113
2114 m = AnyOf(Lt(0), Eq(1), Eq(2));
2115 EXPECT_TRUE(m.Matches(-1));
2116 EXPECT_TRUE(m.Matches(1));
2117 EXPECT_TRUE(m.Matches(2));
2118 EXPECT_FALSE(m.Matches(0));
2119
2120 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2121 EXPECT_TRUE(m.Matches(-1));
2122 EXPECT_TRUE(m.Matches(1));
2123 EXPECT_TRUE(m.Matches(2));
2124 EXPECT_TRUE(m.Matches(3));
2125 EXPECT_FALSE(m.Matches(0));
2126
2127 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2128 EXPECT_TRUE(m.Matches(0));
2129 EXPECT_TRUE(m.Matches(11));
2130 EXPECT_TRUE(m.Matches(3));
2131 EXPECT_FALSE(m.Matches(2));
zhanyong.wan02c15052010-06-09 19:21:30 +00002132
2133 // The following tests for varying number of sub-matchers. Due to the way
2134 // the sub-matchers are handled it is enough to test every sub-matcher once
2135 // with sub-matchers using the same matcher type. Varying matcher types are
2136 // checked for above.
2137 AnyOfMatches(2, AnyOf(1, 2));
2138 AnyOfMatches(3, AnyOf(1, 2, 3));
2139 AnyOfMatches(4, AnyOf(1, 2, 3, 4));
2140 AnyOfMatches(5, AnyOf(1, 2, 3, 4, 5));
2141 AnyOfMatches(6, AnyOf(1, 2, 3, 4, 5, 6));
2142 AnyOfMatches(7, AnyOf(1, 2, 3, 4, 5, 6, 7));
2143 AnyOfMatches(8, AnyOf(1, 2, 3, 4, 5, 6, 7, 8));
2144 AnyOfMatches(9, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9));
2145 AnyOfMatches(10, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
shiqiane35fdd92008-12-10 05:08:54 +00002146}
2147
2148// Tests that AnyOf(m1, ..., mn) describes itself properly.
2149TEST(AnyOfTest, CanDescribeSelf) {
2150 Matcher<int> m;
2151 m = AnyOf(Le(1), Ge(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002152 EXPECT_EQ("(is <= 1) or (is >= 3)",
shiqiane35fdd92008-12-10 05:08:54 +00002153 Describe(m));
2154
2155 m = AnyOf(Lt(0), Eq(1), Eq(2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002156 EXPECT_EQ("(is < 0) or "
shiqiane35fdd92008-12-10 05:08:54 +00002157 "((is equal to 1) or (is equal to 2))",
2158 Describe(m));
2159
2160 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
jgm79a367e2012-04-10 16:02:11 +00002161 EXPECT_EQ("((is < 0) or "
2162 "(is equal to 1)) or "
shiqiane35fdd92008-12-10 05:08:54 +00002163 "((is equal to 2) or "
jgm79a367e2012-04-10 16:02:11 +00002164 "(is equal to 3))",
shiqiane35fdd92008-12-10 05:08:54 +00002165 Describe(m));
2166
2167 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
jgm79a367e2012-04-10 16:02:11 +00002168 EXPECT_EQ("((is <= 0) or "
2169 "(is > 10)) or "
shiqiane35fdd92008-12-10 05:08:54 +00002170 "((is equal to 3) or "
2171 "((is equal to 5) or "
jgm79a367e2012-04-10 16:02:11 +00002172 "(is equal to 7)))",
shiqiane35fdd92008-12-10 05:08:54 +00002173 Describe(m));
2174}
2175
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002176// Tests that AnyOf(m1, ..., mn) describes its negation properly.
2177TEST(AnyOfTest, CanDescribeNegation) {
2178 Matcher<int> m;
2179 m = AnyOf(Le(1), Ge(3));
2180 EXPECT_EQ("(isn't <= 1) and (isn't >= 3)",
2181 DescribeNegation(m));
2182
2183 m = AnyOf(Lt(0), Eq(1), Eq(2));
2184 EXPECT_EQ("(isn't < 0) and "
2185 "((isn't equal to 1) and (isn't equal to 2))",
2186 DescribeNegation(m));
2187
2188 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
jgm79a367e2012-04-10 16:02:11 +00002189 EXPECT_EQ("((isn't < 0) and "
2190 "(isn't equal to 1)) and "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002191 "((isn't equal to 2) and "
jgm79a367e2012-04-10 16:02:11 +00002192 "(isn't equal to 3))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002193 DescribeNegation(m));
2194
2195 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
jgm79a367e2012-04-10 16:02:11 +00002196 EXPECT_EQ("((isn't <= 0) and "
2197 "(isn't > 10)) and "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002198 "((isn't equal to 3) and "
2199 "((isn't equal to 5) and "
jgm79a367e2012-04-10 16:02:11 +00002200 "(isn't equal to 7)))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002201 DescribeNegation(m));
2202}
2203
zhanyong.wan18490652009-05-11 18:54:08 +00002204// Tests that monomorphic matchers are safely cast by the AnyOf matcher.
2205TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
2206 // greater_than_5 and less_than_10 are monomorphic matchers.
2207 Matcher<int> greater_than_5 = Gt(5);
2208 Matcher<int> less_than_10 = Lt(10);
2209
2210 Matcher<const int&> m = AnyOf(greater_than_5, less_than_10);
2211 Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10);
2212 Matcher<int&> m3 = AnyOf(greater_than_5, m2);
2213
2214 // Tests that EitherOf works when composing itself.
2215 Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10);
2216 Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);
2217}
2218
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002219TEST(AnyOfTest, ExplainsResult) {
2220 Matcher<int> m;
2221
2222 // Failed match. Both matchers need to explain. The second
2223 // matcher doesn't give an explanation, so only the first matcher's
2224 // explanation is printed.
2225 m = AnyOf(GreaterThan(10), Lt(0));
2226 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
2227
2228 // Failed match. Both matchers need to explain.
2229 m = AnyOf(GreaterThan(10), GreaterThan(20));
2230 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20",
2231 Explain(m, 5));
2232
2233 // Failed match. All matchers need to explain. The second
2234 // matcher doesn't given an explanation.
2235 m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30));
2236 EXPECT_EQ("which is 5 less than 10, and which is 25 less than 30",
2237 Explain(m, 5));
2238
2239 // Failed match. All matchers need to explain.
2240 m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2241 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20, "
2242 "and which is 25 less than 30",
2243 Explain(m, 5));
2244
2245 // Successful match. The first matcher, which succeeded, needs to
2246 // explain.
2247 m = AnyOf(GreaterThan(10), GreaterThan(20));
2248 EXPECT_EQ("which is 5 more than 10", Explain(m, 15));
2249
2250 // Successful match. The second matcher, which succeeded, needs to
2251 // explain. Since it doesn't given an explanation, nothing is
2252 // printed.
2253 m = AnyOf(GreaterThan(10), Lt(30));
2254 EXPECT_EQ("", Explain(m, 0));
2255
2256 // Successful match. The second matcher, which succeeded, needs to
2257 // explain.
2258 m = AnyOf(GreaterThan(30), GreaterThan(20));
2259 EXPECT_EQ("which is 5 more than 20", Explain(m, 25));
2260}
2261
shiqiane35fdd92008-12-10 05:08:54 +00002262// The following predicate function and predicate functor are for
2263// testing the Truly(predicate) matcher.
2264
2265// Returns non-zero if the input is positive. Note that the return
2266// type of this function is not bool. It's OK as Truly() accepts any
2267// unary function or functor whose return type can be implicitly
2268// converted to bool.
2269int IsPositive(double x) {
2270 return x > 0 ? 1 : 0;
2271}
2272
2273// This functor returns true if the input is greater than the given
2274// number.
2275class IsGreaterThan {
2276 public:
2277 explicit IsGreaterThan(int threshold) : threshold_(threshold) {}
2278
2279 bool operator()(int n) const { return n > threshold_; }
zhanyong.wan32de5f52009-12-23 00:13:23 +00002280
shiqiane35fdd92008-12-10 05:08:54 +00002281 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002282 int threshold_;
shiqiane35fdd92008-12-10 05:08:54 +00002283};
2284
2285// For testing Truly().
2286const int foo = 0;
2287
2288// This predicate returns true iff the argument references foo and has
2289// a zero value.
2290bool ReferencesFooAndIsZero(const int& n) {
2291 return (&n == &foo) && (n == 0);
2292}
2293
2294// Tests that Truly(predicate) matches what satisfies the given
2295// predicate.
2296TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {
2297 Matcher<double> m = Truly(IsPositive);
2298 EXPECT_TRUE(m.Matches(2.0));
2299 EXPECT_FALSE(m.Matches(-1.5));
2300}
2301
2302// Tests that Truly(predicate_functor) works too.
2303TEST(TrulyTest, CanBeUsedWithFunctor) {
2304 Matcher<int> m = Truly(IsGreaterThan(5));
2305 EXPECT_TRUE(m.Matches(6));
2306 EXPECT_FALSE(m.Matches(4));
2307}
2308
zhanyong.wan8d3dc0c2011-04-14 19:37:06 +00002309// A class that can be implicitly converted to bool.
2310class ConvertibleToBool {
2311 public:
2312 explicit ConvertibleToBool(int number) : number_(number) {}
2313 operator bool() const { return number_ != 0; }
2314
2315 private:
2316 int number_;
2317};
2318
2319ConvertibleToBool IsNotZero(int number) {
2320 return ConvertibleToBool(number);
2321}
2322
2323// Tests that the predicate used in Truly() may return a class that's
2324// implicitly convertible to bool, even when the class has no
2325// operator!().
2326TEST(TrulyTest, PredicateCanReturnAClassConvertibleToBool) {
2327 Matcher<int> m = Truly(IsNotZero);
2328 EXPECT_TRUE(m.Matches(1));
2329 EXPECT_FALSE(m.Matches(0));
2330}
2331
shiqiane35fdd92008-12-10 05:08:54 +00002332// Tests that Truly(predicate) can describe itself properly.
2333TEST(TrulyTest, CanDescribeSelf) {
2334 Matcher<double> m = Truly(IsPositive);
2335 EXPECT_EQ("satisfies the given predicate",
2336 Describe(m));
2337}
2338
2339// Tests that Truly(predicate) works when the matcher takes its
2340// argument by reference.
2341TEST(TrulyTest, WorksForByRefArguments) {
2342 Matcher<const int&> m = Truly(ReferencesFooAndIsZero);
2343 EXPECT_TRUE(m.Matches(foo));
2344 int n = 0;
2345 EXPECT_FALSE(m.Matches(n));
2346}
2347
2348// Tests that Matches(m) is a predicate satisfied by whatever that
2349// matches matcher m.
2350TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {
2351 EXPECT_TRUE(Matches(Ge(0))(1));
2352 EXPECT_FALSE(Matches(Eq('a'))('b'));
2353}
2354
2355// Tests that Matches(m) works when the matcher takes its argument by
2356// reference.
2357TEST(MatchesTest, WorksOnByRefArguments) {
2358 int m = 0, n = 0;
2359 EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n));
2360 EXPECT_FALSE(Matches(Ref(m))(n));
2361}
2362
2363// Tests that a Matcher on non-reference type can be used in
2364// Matches().
2365TEST(MatchesTest, WorksWithMatcherOnNonRefType) {
2366 Matcher<int> eq5 = Eq(5);
2367 EXPECT_TRUE(Matches(eq5)(5));
2368 EXPECT_FALSE(Matches(eq5)(2));
2369}
2370
zhanyong.wanb8243162009-06-04 05:48:20 +00002371// Tests Value(value, matcher). Since Value() is a simple wrapper for
2372// Matches(), which has been tested already, we don't spend a lot of
2373// effort on testing Value().
2374TEST(ValueTest, WorksWithPolymorphicMatcher) {
2375 EXPECT_TRUE(Value("hi", StartsWith("h")));
2376 EXPECT_FALSE(Value(5, Gt(10)));
2377}
2378
2379TEST(ValueTest, WorksWithMonomorphicMatcher) {
2380 const Matcher<int> is_zero = Eq(0);
2381 EXPECT_TRUE(Value(0, is_zero));
2382 EXPECT_FALSE(Value('a', is_zero));
2383
2384 int n = 0;
2385 const Matcher<const int&> ref_n = Ref(n);
2386 EXPECT_TRUE(Value(n, ref_n));
2387 EXPECT_FALSE(Value(1, ref_n));
2388}
2389
zhanyong.wana862f1d2010-03-15 21:23:04 +00002390TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) {
zhanyong.wan34b034c2010-03-05 21:23:23 +00002391 StringMatchResultListener listener1;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002392 EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002393 EXPECT_EQ("% 2 == 0", listener1.str());
2394
2395 StringMatchResultListener listener2;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002396 EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002397 EXPECT_EQ("", listener2.str());
2398}
2399
zhanyong.wana862f1d2010-03-15 21:23:04 +00002400TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) {
zhanyong.wan34b034c2010-03-05 21:23:23 +00002401 const Matcher<int> is_even = PolymorphicIsEven();
2402 StringMatchResultListener listener1;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002403 EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002404 EXPECT_EQ("% 2 == 0", listener1.str());
2405
2406 const Matcher<const double&> is_zero = Eq(0);
2407 StringMatchResultListener listener2;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002408 EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002409 EXPECT_EQ("", listener2.str());
2410}
2411
zhanyong.wana862f1d2010-03-15 21:23:04 +00002412MATCHER_P(Really, inner_matcher, "") {
2413 return ExplainMatchResult(inner_matcher, arg, result_listener);
2414}
2415
2416TEST(ExplainMatchResultTest, WorksInsideMATCHER) {
2417 EXPECT_THAT(0, Really(Eq(0)));
2418}
2419
zhanyong.wanbf550852009-06-09 06:09:53 +00002420TEST(AllArgsTest, WorksForTuple) {
2421 EXPECT_THAT(make_tuple(1, 2L), AllArgs(Lt()));
2422 EXPECT_THAT(make_tuple(2L, 1), Not(AllArgs(Lt())));
2423}
2424
2425TEST(AllArgsTest, WorksForNonTuple) {
2426 EXPECT_THAT(42, AllArgs(Gt(0)));
2427 EXPECT_THAT('a', Not(AllArgs(Eq('b'))));
2428}
2429
2430class AllArgsHelper {
2431 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002432 AllArgsHelper() {}
2433
zhanyong.wanbf550852009-06-09 06:09:53 +00002434 MOCK_METHOD2(Helper, int(char x, int y));
zhanyong.wan32de5f52009-12-23 00:13:23 +00002435
2436 private:
2437 GTEST_DISALLOW_COPY_AND_ASSIGN_(AllArgsHelper);
zhanyong.wanbf550852009-06-09 06:09:53 +00002438};
2439
2440TEST(AllArgsTest, WorksInWithClause) {
2441 AllArgsHelper helper;
2442 ON_CALL(helper, Helper(_, _))
2443 .With(AllArgs(Lt()))
2444 .WillByDefault(Return(1));
2445 EXPECT_CALL(helper, Helper(_, _));
2446 EXPECT_CALL(helper, Helper(_, _))
2447 .With(AllArgs(Gt()))
2448 .WillOnce(Return(2));
2449
2450 EXPECT_EQ(1, helper.Helper('\1', 2));
2451 EXPECT_EQ(2, helper.Helper('a', 1));
2452}
2453
shiqiane35fdd92008-12-10 05:08:54 +00002454// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2455// matches the matcher.
2456TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {
2457 ASSERT_THAT(5, Ge(2)) << "This should succeed.";
2458 ASSERT_THAT("Foo", EndsWith("oo"));
2459 EXPECT_THAT(2, AllOf(Le(7), Ge(0))) << "This should succeed too.";
2460 EXPECT_THAT("Hello", StartsWith("Hell"));
2461}
2462
2463// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2464// doesn't match the matcher.
2465TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {
2466 // 'n' must be static as it is used in an EXPECT_FATAL_FAILURE(),
2467 // which cannot reference auto variables.
zhanyong.wan736baa82010-09-27 17:44:16 +00002468 static unsigned short n; // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00002469 n = 5;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002470
2471 // VC++ prior to version 8.0 SP1 has a bug where it will not see any
2472 // functions declared in the namespace scope from within nested classes.
2473 // EXPECT/ASSERT_(NON)FATAL_FAILURE macros use nested classes so that all
2474 // namespace-level functions invoked inside them need to be explicitly
2475 // resolved.
2476 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Gt(10)),
shiqiane35fdd92008-12-10 05:08:54 +00002477 "Value of: n\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002478 "Expected: is > 10\n"
zhanyong.wan736baa82010-09-27 17:44:16 +00002479 " Actual: 5" + OfType("unsigned short"));
shiqiane35fdd92008-12-10 05:08:54 +00002480 n = 0;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002481 EXPECT_NONFATAL_FAILURE(
2482 EXPECT_THAT(n, ::testing::AllOf(::testing::Le(7), ::testing::Ge(5))),
2483 "Value of: n\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002484 "Expected: (is <= 7) and (is >= 5)\n"
zhanyong.wan736baa82010-09-27 17:44:16 +00002485 " Actual: 0" + OfType("unsigned short"));
shiqiane35fdd92008-12-10 05:08:54 +00002486}
2487
2488// Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument
2489// has a reference type.
2490TEST(MatcherAssertionTest, WorksForByRefArguments) {
2491 // We use a static variable here as EXPECT_FATAL_FAILURE() cannot
2492 // reference auto variables.
2493 static int n;
2494 n = 0;
2495 EXPECT_THAT(n, AllOf(Le(7), Ref(n)));
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002496 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
shiqiane35fdd92008-12-10 05:08:54 +00002497 "Value of: n\n"
2498 "Expected: does not reference the variable @");
2499 // Tests the "Actual" part.
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002500 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
zhanyong.wan736baa82010-09-27 17:44:16 +00002501 "Actual: 0" + OfType("int") + ", which is located @");
shiqiane35fdd92008-12-10 05:08:54 +00002502}
2503
zhanyong.wan95b12332009-09-25 18:55:50 +00002504#if !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002505// Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is
2506// monomorphic.
zhanyong.wan95b12332009-09-25 18:55:50 +00002507
2508// ASSERT_THAT("hello", starts_with_he) fails to compile with Nokia's
2509// Symbian compiler: it tries to compile
2510// template<T, U> class MatcherCastImpl { ...
zhanyong.wandb22c222010-01-28 21:52:29 +00002511// virtual bool MatchAndExplain(T x, ...) const {
2512// return source_matcher_.MatchAndExplain(static_cast<U>(x), ...);
zhanyong.wan95b12332009-09-25 18:55:50 +00002513// with U == string and T == const char*
2514// With ASSERT_THAT("hello"...) changed to ASSERT_THAT(string("hello") ... )
2515// the compiler silently crashes with no output.
2516// If MatcherCastImpl is changed to use U(x) instead of static_cast<U>(x)
2517// the code compiles but the converted string is bogus.
shiqiane35fdd92008-12-10 05:08:54 +00002518TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
2519 Matcher<const char*> starts_with_he = StartsWith("he");
2520 ASSERT_THAT("hello", starts_with_he);
2521
2522 Matcher<const string&> ends_with_ok = EndsWith("ok");
2523 ASSERT_THAT("book", ends_with_ok);
zhanyong.wan736baa82010-09-27 17:44:16 +00002524 const string bad = "bad";
2525 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(bad, ends_with_ok),
2526 "Value of: bad\n"
2527 "Expected: ends with \"ok\"\n"
2528 " Actual: \"bad\"");
shiqiane35fdd92008-12-10 05:08:54 +00002529 Matcher<int> is_greater_than_5 = Gt(5);
2530 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5),
2531 "Value of: 5\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002532 "Expected: is > 5\n"
zhanyong.wan736baa82010-09-27 17:44:16 +00002533 " Actual: 5" + OfType("int"));
shiqiane35fdd92008-12-10 05:08:54 +00002534}
zhanyong.wan95b12332009-09-25 18:55:50 +00002535#endif // !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002536
2537// Tests floating-point matchers.
2538template <typename RawType>
2539class FloatingPointTest : public testing::Test {
2540 protected:
2541 typedef typename testing::internal::FloatingPoint<RawType> Floating;
2542 typedef typename Floating::Bits Bits;
2543
2544 virtual void SetUp() {
2545 const size_t max_ulps = Floating::kMaxUlps;
2546
2547 // The bits that represent 0.0.
2548 const Bits zero_bits = Floating(0).bits();
2549
2550 // Makes some numbers close to 0.0.
2551 close_to_positive_zero_ = Floating::ReinterpretBits(zero_bits + max_ulps/2);
2552 close_to_negative_zero_ = -Floating::ReinterpretBits(
2553 zero_bits + max_ulps - max_ulps/2);
2554 further_from_negative_zero_ = -Floating::ReinterpretBits(
2555 zero_bits + max_ulps + 1 - max_ulps/2);
2556
2557 // The bits that represent 1.0.
2558 const Bits one_bits = Floating(1).bits();
2559
2560 // Makes some numbers close to 1.0.
2561 close_to_one_ = Floating::ReinterpretBits(one_bits + max_ulps);
2562 further_from_one_ = Floating::ReinterpretBits(one_bits + max_ulps + 1);
2563
2564 // +infinity.
2565 infinity_ = Floating::Infinity();
2566
2567 // The bits that represent +infinity.
2568 const Bits infinity_bits = Floating(infinity_).bits();
2569
2570 // Makes some numbers close to infinity.
2571 close_to_infinity_ = Floating::ReinterpretBits(infinity_bits - max_ulps);
2572 further_from_infinity_ = Floating::ReinterpretBits(
2573 infinity_bits - max_ulps - 1);
2574
2575 // Makes some NAN's.
2576 nan1_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 1);
2577 nan2_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 200);
2578 }
2579
2580 void TestSize() {
2581 EXPECT_EQ(sizeof(RawType), sizeof(Bits));
2582 }
2583
2584 // A battery of tests for FloatingEqMatcher::Matches.
2585 // matcher_maker is a pointer to a function which creates a FloatingEqMatcher.
2586 void TestMatches(
2587 testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) {
2588 Matcher<RawType> m1 = matcher_maker(0.0);
2589 EXPECT_TRUE(m1.Matches(-0.0));
2590 EXPECT_TRUE(m1.Matches(close_to_positive_zero_));
2591 EXPECT_TRUE(m1.Matches(close_to_negative_zero_));
2592 EXPECT_FALSE(m1.Matches(1.0));
2593
2594 Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_);
2595 EXPECT_FALSE(m2.Matches(further_from_negative_zero_));
2596
2597 Matcher<RawType> m3 = matcher_maker(1.0);
2598 EXPECT_TRUE(m3.Matches(close_to_one_));
2599 EXPECT_FALSE(m3.Matches(further_from_one_));
2600
2601 // Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above.
2602 EXPECT_FALSE(m3.Matches(0.0));
2603
2604 Matcher<RawType> m4 = matcher_maker(-infinity_);
2605 EXPECT_TRUE(m4.Matches(-close_to_infinity_));
2606
2607 Matcher<RawType> m5 = matcher_maker(infinity_);
2608 EXPECT_TRUE(m5.Matches(close_to_infinity_));
2609
2610 // This is interesting as the representations of infinity_ and nan1_
2611 // are only 1 DLP apart.
2612 EXPECT_FALSE(m5.Matches(nan1_));
2613
2614 // matcher_maker can produce a Matcher<const RawType&>, which is needed in
2615 // some cases.
2616 Matcher<const RawType&> m6 = matcher_maker(0.0);
2617 EXPECT_TRUE(m6.Matches(-0.0));
2618 EXPECT_TRUE(m6.Matches(close_to_positive_zero_));
2619 EXPECT_FALSE(m6.Matches(1.0));
2620
2621 // matcher_maker can produce a Matcher<RawType&>, which is needed in some
2622 // cases.
2623 Matcher<RawType&> m7 = matcher_maker(0.0);
2624 RawType x = 0.0;
2625 EXPECT_TRUE(m7.Matches(x));
2626 x = 0.01f;
2627 EXPECT_FALSE(m7.Matches(x));
2628 }
2629
2630 // Pre-calculated numbers to be used by the tests.
2631
2632 static RawType close_to_positive_zero_;
2633 static RawType close_to_negative_zero_;
2634 static RawType further_from_negative_zero_;
2635
2636 static RawType close_to_one_;
2637 static RawType further_from_one_;
2638
2639 static RawType infinity_;
2640 static RawType close_to_infinity_;
2641 static RawType further_from_infinity_;
2642
2643 static RawType nan1_;
2644 static RawType nan2_;
2645};
2646
2647template <typename RawType>
2648RawType FloatingPointTest<RawType>::close_to_positive_zero_;
2649
2650template <typename RawType>
2651RawType FloatingPointTest<RawType>::close_to_negative_zero_;
2652
2653template <typename RawType>
2654RawType FloatingPointTest<RawType>::further_from_negative_zero_;
2655
2656template <typename RawType>
2657RawType FloatingPointTest<RawType>::close_to_one_;
2658
2659template <typename RawType>
2660RawType FloatingPointTest<RawType>::further_from_one_;
2661
2662template <typename RawType>
2663RawType FloatingPointTest<RawType>::infinity_;
2664
2665template <typename RawType>
2666RawType FloatingPointTest<RawType>::close_to_infinity_;
2667
2668template <typename RawType>
2669RawType FloatingPointTest<RawType>::further_from_infinity_;
2670
2671template <typename RawType>
2672RawType FloatingPointTest<RawType>::nan1_;
2673
2674template <typename RawType>
2675RawType FloatingPointTest<RawType>::nan2_;
2676
2677// Instantiate FloatingPointTest for testing floats.
2678typedef FloatingPointTest<float> FloatTest;
2679
2680TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) {
2681 TestMatches(&FloatEq);
2682}
2683
2684TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
2685 TestMatches(&NanSensitiveFloatEq);
2686}
2687
2688TEST_F(FloatTest, FloatEqCannotMatchNaN) {
2689 // FloatEq never matches NaN.
2690 Matcher<float> m = FloatEq(nan1_);
2691 EXPECT_FALSE(m.Matches(nan1_));
2692 EXPECT_FALSE(m.Matches(nan2_));
2693 EXPECT_FALSE(m.Matches(1.0));
2694}
2695
2696TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {
2697 // NanSensitiveFloatEq will match NaN.
2698 Matcher<float> m = NanSensitiveFloatEq(nan1_);
2699 EXPECT_TRUE(m.Matches(nan1_));
2700 EXPECT_TRUE(m.Matches(nan2_));
2701 EXPECT_FALSE(m.Matches(1.0));
2702}
2703
2704TEST_F(FloatTest, FloatEqCanDescribeSelf) {
2705 Matcher<float> m1 = FloatEq(2.0f);
2706 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002707 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002708
2709 Matcher<float> m2 = FloatEq(0.5f);
2710 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002711 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002712
2713 Matcher<float> m3 = FloatEq(nan1_);
2714 EXPECT_EQ("never matches", Describe(m3));
2715 EXPECT_EQ("is anything", DescribeNegation(m3));
2716}
2717
2718TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {
2719 Matcher<float> m1 = NanSensitiveFloatEq(2.0f);
2720 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002721 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002722
2723 Matcher<float> m2 = NanSensitiveFloatEq(0.5f);
2724 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002725 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002726
2727 Matcher<float> m3 = NanSensitiveFloatEq(nan1_);
2728 EXPECT_EQ("is NaN", Describe(m3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002729 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
shiqiane35fdd92008-12-10 05:08:54 +00002730}
2731
2732// Instantiate FloatingPointTest for testing doubles.
2733typedef FloatingPointTest<double> DoubleTest;
2734
2735TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
2736 TestMatches(&DoubleEq);
2737}
2738
2739TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
2740 TestMatches(&NanSensitiveDoubleEq);
2741}
2742
2743TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {
2744 // DoubleEq never matches NaN.
2745 Matcher<double> m = DoubleEq(nan1_);
2746 EXPECT_FALSE(m.Matches(nan1_));
2747 EXPECT_FALSE(m.Matches(nan2_));
2748 EXPECT_FALSE(m.Matches(1.0));
2749}
2750
2751TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {
2752 // NanSensitiveDoubleEq will match NaN.
2753 Matcher<double> m = NanSensitiveDoubleEq(nan1_);
2754 EXPECT_TRUE(m.Matches(nan1_));
2755 EXPECT_TRUE(m.Matches(nan2_));
2756 EXPECT_FALSE(m.Matches(1.0));
2757}
2758
2759TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {
2760 Matcher<double> m1 = DoubleEq(2.0);
2761 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002762 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002763
2764 Matcher<double> m2 = DoubleEq(0.5);
2765 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002766 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002767
2768 Matcher<double> m3 = DoubleEq(nan1_);
2769 EXPECT_EQ("never matches", Describe(m3));
2770 EXPECT_EQ("is anything", DescribeNegation(m3));
2771}
2772
2773TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {
2774 Matcher<double> m1 = NanSensitiveDoubleEq(2.0);
2775 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002776 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002777
2778 Matcher<double> m2 = NanSensitiveDoubleEq(0.5);
2779 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002780 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002781
2782 Matcher<double> m3 = NanSensitiveDoubleEq(nan1_);
2783 EXPECT_EQ("is NaN", Describe(m3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002784 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
shiqiane35fdd92008-12-10 05:08:54 +00002785}
2786
2787TEST(PointeeTest, RawPointer) {
2788 const Matcher<int*> m = Pointee(Ge(0));
2789
2790 int n = 1;
2791 EXPECT_TRUE(m.Matches(&n));
2792 n = -1;
2793 EXPECT_FALSE(m.Matches(&n));
2794 EXPECT_FALSE(m.Matches(NULL));
2795}
2796
2797TEST(PointeeTest, RawPointerToConst) {
2798 const Matcher<const double*> m = Pointee(Ge(0));
2799
2800 double x = 1;
2801 EXPECT_TRUE(m.Matches(&x));
2802 x = -1;
2803 EXPECT_FALSE(m.Matches(&x));
2804 EXPECT_FALSE(m.Matches(NULL));
2805}
2806
2807TEST(PointeeTest, ReferenceToConstRawPointer) {
2808 const Matcher<int* const &> m = Pointee(Ge(0));
2809
2810 int n = 1;
2811 EXPECT_TRUE(m.Matches(&n));
2812 n = -1;
2813 EXPECT_FALSE(m.Matches(&n));
2814 EXPECT_FALSE(m.Matches(NULL));
2815}
2816
2817TEST(PointeeTest, ReferenceToNonConstRawPointer) {
2818 const Matcher<double* &> m = Pointee(Ge(0));
2819
2820 double x = 1.0;
2821 double* p = &x;
2822 EXPECT_TRUE(m.Matches(p));
2823 x = -1;
2824 EXPECT_FALSE(m.Matches(p));
2825 p = NULL;
2826 EXPECT_FALSE(m.Matches(p));
2827}
2828
vladlosevada23472012-08-14 15:38:49 +00002829// Minimal const-propagating pointer.
2830template <typename T>
2831class ConstPropagatingPtr {
2832 public:
2833 typedef T element_type;
2834
2835 ConstPropagatingPtr() : val_() {}
2836 explicit ConstPropagatingPtr(T* t) : val_(t) {}
2837 ConstPropagatingPtr(const ConstPropagatingPtr& other) : val_(other.val_) {}
2838
2839 T* get() { return val_; }
2840 T& operator*() { return *val_; }
2841 // Most smart pointers return non-const T* and T& from the next methods.
2842 const T* get() const { return val_; }
2843 const T& operator*() const { return *val_; }
2844
2845 private:
2846 T* val_;
2847};
2848
2849TEST(PointeeTest, WorksWithConstPropagatingPointers) {
2850 const Matcher< ConstPropagatingPtr<int> > m = Pointee(Lt(5));
2851 int three = 3;
2852 const ConstPropagatingPtr<int> co(&three);
2853 ConstPropagatingPtr<int> o(&three);
2854 EXPECT_TRUE(m.Matches(o));
2855 EXPECT_TRUE(m.Matches(co));
2856 *o = 6;
2857 EXPECT_FALSE(m.Matches(o));
2858 EXPECT_FALSE(m.Matches(ConstPropagatingPtr<int>()));
2859}
2860
shiqiane35fdd92008-12-10 05:08:54 +00002861TEST(PointeeTest, NeverMatchesNull) {
2862 const Matcher<const char*> m = Pointee(_);
2863 EXPECT_FALSE(m.Matches(NULL));
2864}
2865
2866// Tests that we can write Pointee(value) instead of Pointee(Eq(value)).
2867TEST(PointeeTest, MatchesAgainstAValue) {
2868 const Matcher<int*> m = Pointee(5);
2869
2870 int n = 5;
2871 EXPECT_TRUE(m.Matches(&n));
2872 n = -1;
2873 EXPECT_FALSE(m.Matches(&n));
2874 EXPECT_FALSE(m.Matches(NULL));
2875}
2876
2877TEST(PointeeTest, CanDescribeSelf) {
2878 const Matcher<int*> m = Pointee(Gt(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002879 EXPECT_EQ("points to a value that is > 3", Describe(m));
2880 EXPECT_EQ("does not point to a value that is > 3",
shiqiane35fdd92008-12-10 05:08:54 +00002881 DescribeNegation(m));
2882}
2883
shiqiane35fdd92008-12-10 05:08:54 +00002884TEST(PointeeTest, CanExplainMatchResult) {
2885 const Matcher<const string*> m = Pointee(StartsWith("Hi"));
2886
2887 EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL)));
2888
zhanyong.wan736baa82010-09-27 17:44:16 +00002889 const Matcher<long*> m2 = Pointee(GreaterThan(1)); // NOLINT
2890 long n = 3; // NOLINT
2891 EXPECT_EQ("which points to 3" + OfType("long") + ", which is 2 more than 1",
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002892 Explain(m2, &n));
2893}
2894
2895TEST(PointeeTest, AlwaysExplainsPointee) {
2896 const Matcher<int*> m = Pointee(0);
2897 int n = 42;
zhanyong.wan736baa82010-09-27 17:44:16 +00002898 EXPECT_EQ("which points to 42" + OfType("int"), Explain(m, &n));
shiqiane35fdd92008-12-10 05:08:54 +00002899}
2900
2901// An uncopyable class.
2902class Uncopyable {
2903 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002904 explicit Uncopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00002905
2906 int value() const { return value_; }
2907 private:
2908 const int value_;
2909 GTEST_DISALLOW_COPY_AND_ASSIGN_(Uncopyable);
2910};
2911
2912// Returns true iff x.value() is positive.
2913bool ValueIsPositive(const Uncopyable& x) { return x.value() > 0; }
2914
2915// A user-defined struct for testing Field().
2916struct AStruct {
2917 AStruct() : x(0), y(1.0), z(5), p(NULL) {}
2918 AStruct(const AStruct& rhs)
2919 : x(rhs.x), y(rhs.y), z(rhs.z.value()), p(rhs.p) {}
2920
2921 int x; // A non-const field.
2922 const double y; // A const field.
2923 Uncopyable z; // An uncopyable field.
2924 const char* p; // A pointer field.
zhanyong.wan32de5f52009-12-23 00:13:23 +00002925
2926 private:
2927 GTEST_DISALLOW_ASSIGN_(AStruct);
shiqiane35fdd92008-12-10 05:08:54 +00002928};
2929
2930// A derived struct for testing Field().
2931struct DerivedStruct : public AStruct {
2932 char ch;
zhanyong.wan32de5f52009-12-23 00:13:23 +00002933
2934 private:
2935 GTEST_DISALLOW_ASSIGN_(DerivedStruct);
shiqiane35fdd92008-12-10 05:08:54 +00002936};
2937
2938// Tests that Field(&Foo::field, ...) works when field is non-const.
2939TEST(FieldTest, WorksForNonConstField) {
2940 Matcher<AStruct> m = Field(&AStruct::x, Ge(0));
2941
2942 AStruct a;
2943 EXPECT_TRUE(m.Matches(a));
2944 a.x = -1;
2945 EXPECT_FALSE(m.Matches(a));
2946}
2947
2948// Tests that Field(&Foo::field, ...) works when field is const.
2949TEST(FieldTest, WorksForConstField) {
2950 AStruct a;
2951
2952 Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0));
2953 EXPECT_TRUE(m.Matches(a));
2954 m = Field(&AStruct::y, Le(0.0));
2955 EXPECT_FALSE(m.Matches(a));
2956}
2957
2958// Tests that Field(&Foo::field, ...) works when field is not copyable.
2959TEST(FieldTest, WorksForUncopyableField) {
2960 AStruct a;
2961
2962 Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive));
2963 EXPECT_TRUE(m.Matches(a));
2964 m = Field(&AStruct::z, Not(Truly(ValueIsPositive)));
2965 EXPECT_FALSE(m.Matches(a));
2966}
2967
2968// Tests that Field(&Foo::field, ...) works when field is a pointer.
2969TEST(FieldTest, WorksForPointerField) {
2970 // Matching against NULL.
2971 Matcher<AStruct> m = Field(&AStruct::p, static_cast<const char*>(NULL));
2972 AStruct a;
2973 EXPECT_TRUE(m.Matches(a));
2974 a.p = "hi";
2975 EXPECT_FALSE(m.Matches(a));
2976
2977 // Matching a pointer that is not NULL.
2978 m = Field(&AStruct::p, StartsWith("hi"));
2979 a.p = "hill";
2980 EXPECT_TRUE(m.Matches(a));
2981 a.p = "hole";
2982 EXPECT_FALSE(m.Matches(a));
2983}
2984
2985// Tests that Field() works when the object is passed by reference.
2986TEST(FieldTest, WorksForByRefArgument) {
2987 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2988
2989 AStruct a;
2990 EXPECT_TRUE(m.Matches(a));
2991 a.x = -1;
2992 EXPECT_FALSE(m.Matches(a));
2993}
2994
2995// Tests that Field(&Foo::field, ...) works when the argument's type
2996// is a sub-type of Foo.
2997TEST(FieldTest, WorksForArgumentOfSubType) {
2998 // Note that the matcher expects DerivedStruct but we say AStruct
2999 // inside Field().
3000 Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0));
3001
3002 DerivedStruct d;
3003 EXPECT_TRUE(m.Matches(d));
3004 d.x = -1;
3005 EXPECT_FALSE(m.Matches(d));
3006}
3007
3008// Tests that Field(&Foo::field, m) works when field's type and m's
3009// argument type are compatible but not the same.
3010TEST(FieldTest, WorksForCompatibleMatcherType) {
3011 // The field is an int, but the inner matcher expects a signed char.
3012 Matcher<const AStruct&> m = Field(&AStruct::x,
3013 Matcher<signed char>(Ge(0)));
3014
3015 AStruct a;
3016 EXPECT_TRUE(m.Matches(a));
3017 a.x = -1;
3018 EXPECT_FALSE(m.Matches(a));
3019}
3020
3021// Tests that Field() can describe itself.
3022TEST(FieldTest, CanDescribeSelf) {
3023 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
3024
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003025 EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
3026 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003027}
3028
3029// Tests that Field() can explain the match result.
3030TEST(FieldTest, CanExplainMatchResult) {
3031 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
3032
3033 AStruct a;
3034 a.x = 1;
zhanyong.wan736baa82010-09-27 17:44:16 +00003035 EXPECT_EQ("whose given field is 1" + OfType("int"), Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003036
3037 m = Field(&AStruct::x, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003038 EXPECT_EQ(
3039 "whose given field is 1" + OfType("int") + ", which is 1 more than 0",
3040 Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003041}
3042
3043// Tests that Field() works when the argument is a pointer to const.
3044TEST(FieldForPointerTest, WorksForPointerToConst) {
3045 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
3046
3047 AStruct a;
3048 EXPECT_TRUE(m.Matches(&a));
3049 a.x = -1;
3050 EXPECT_FALSE(m.Matches(&a));
3051}
3052
3053// Tests that Field() works when the argument is a pointer to non-const.
3054TEST(FieldForPointerTest, WorksForPointerToNonConst) {
3055 Matcher<AStruct*> m = Field(&AStruct::x, Ge(0));
3056
3057 AStruct a;
3058 EXPECT_TRUE(m.Matches(&a));
3059 a.x = -1;
3060 EXPECT_FALSE(m.Matches(&a));
3061}
3062
zhanyong.wan6953a722010-01-13 05:15:07 +00003063// Tests that Field() works when the argument is a reference to a const pointer.
3064TEST(FieldForPointerTest, WorksForReferenceToConstPointer) {
3065 Matcher<AStruct* const&> m = Field(&AStruct::x, Ge(0));
3066
3067 AStruct a;
3068 EXPECT_TRUE(m.Matches(&a));
3069 a.x = -1;
3070 EXPECT_FALSE(m.Matches(&a));
3071}
3072
shiqiane35fdd92008-12-10 05:08:54 +00003073// Tests that Field() does not match the NULL pointer.
3074TEST(FieldForPointerTest, DoesNotMatchNull) {
3075 Matcher<const AStruct*> m = Field(&AStruct::x, _);
3076 EXPECT_FALSE(m.Matches(NULL));
3077}
3078
3079// Tests that Field(&Foo::field, ...) works when the argument's type
3080// is a sub-type of const Foo*.
3081TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
3082 // Note that the matcher expects DerivedStruct but we say AStruct
3083 // inside Field().
3084 Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0));
3085
3086 DerivedStruct d;
3087 EXPECT_TRUE(m.Matches(&d));
3088 d.x = -1;
3089 EXPECT_FALSE(m.Matches(&d));
3090}
3091
3092// Tests that Field() can describe itself when used to match a pointer.
3093TEST(FieldForPointerTest, CanDescribeSelf) {
3094 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
3095
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003096 EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
3097 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003098}
3099
3100// Tests that Field() can explain the result of matching a pointer.
3101TEST(FieldForPointerTest, CanExplainMatchResult) {
3102 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
3103
3104 AStruct a;
3105 a.x = 1;
3106 EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(NULL)));
zhanyong.wan736baa82010-09-27 17:44:16 +00003107 EXPECT_EQ("which points to an object whose given field is 1" + OfType("int"),
3108 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003109
3110 m = Field(&AStruct::x, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003111 EXPECT_EQ("which points to an object whose given field is 1" + OfType("int") +
3112 ", which is 1 more than 0", Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003113}
3114
3115// A user-defined class for testing Property().
3116class AClass {
3117 public:
3118 AClass() : n_(0) {}
3119
3120 // A getter that returns a non-reference.
3121 int n() const { return n_; }
3122
3123 void set_n(int new_n) { n_ = new_n; }
3124
3125 // A getter that returns a reference to const.
3126 const string& s() const { return s_; }
3127
3128 void set_s(const string& new_s) { s_ = new_s; }
3129
3130 // A getter that returns a reference to non-const.
3131 double& x() const { return x_; }
3132 private:
3133 int n_;
3134 string s_;
3135
3136 static double x_;
3137};
3138
3139double AClass::x_ = 0.0;
3140
3141// A derived class for testing Property().
3142class DerivedClass : public AClass {
3143 private:
3144 int k_;
3145};
3146
3147// Tests that Property(&Foo::property, ...) works when property()
3148// returns a non-reference.
3149TEST(PropertyTest, WorksForNonReferenceProperty) {
3150 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3151
3152 AClass a;
3153 a.set_n(1);
3154 EXPECT_TRUE(m.Matches(a));
3155
3156 a.set_n(-1);
3157 EXPECT_FALSE(m.Matches(a));
3158}
3159
3160// Tests that Property(&Foo::property, ...) works when property()
3161// returns a reference to const.
3162TEST(PropertyTest, WorksForReferenceToConstProperty) {
3163 Matcher<const AClass&> m = Property(&AClass::s, StartsWith("hi"));
3164
3165 AClass a;
3166 a.set_s("hill");
3167 EXPECT_TRUE(m.Matches(a));
3168
3169 a.set_s("hole");
3170 EXPECT_FALSE(m.Matches(a));
3171}
3172
3173// Tests that Property(&Foo::property, ...) works when property()
3174// returns a reference to non-const.
3175TEST(PropertyTest, WorksForReferenceToNonConstProperty) {
3176 double x = 0.0;
3177 AClass a;
3178
3179 Matcher<const AClass&> m = Property(&AClass::x, Ref(x));
3180 EXPECT_FALSE(m.Matches(a));
3181
3182 m = Property(&AClass::x, Not(Ref(x)));
3183 EXPECT_TRUE(m.Matches(a));
3184}
3185
3186// Tests that Property(&Foo::property, ...) works when the argument is
3187// passed by value.
3188TEST(PropertyTest, WorksForByValueArgument) {
3189 Matcher<AClass> m = Property(&AClass::s, StartsWith("hi"));
3190
3191 AClass a;
3192 a.set_s("hill");
3193 EXPECT_TRUE(m.Matches(a));
3194
3195 a.set_s("hole");
3196 EXPECT_FALSE(m.Matches(a));
3197}
3198
3199// Tests that Property(&Foo::property, ...) works when the argument's
3200// type is a sub-type of Foo.
3201TEST(PropertyTest, WorksForArgumentOfSubType) {
3202 // The matcher expects a DerivedClass, but inside the Property() we
3203 // say AClass.
3204 Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0));
3205
3206 DerivedClass d;
3207 d.set_n(1);
3208 EXPECT_TRUE(m.Matches(d));
3209
3210 d.set_n(-1);
3211 EXPECT_FALSE(m.Matches(d));
3212}
3213
3214// Tests that Property(&Foo::property, m) works when property()'s type
3215// and m's argument type are compatible but different.
3216TEST(PropertyTest, WorksForCompatibleMatcherType) {
3217 // n() returns an int but the inner matcher expects a signed char.
3218 Matcher<const AClass&> m = Property(&AClass::n,
3219 Matcher<signed char>(Ge(0)));
3220
3221 AClass a;
3222 EXPECT_TRUE(m.Matches(a));
3223 a.set_n(-1);
3224 EXPECT_FALSE(m.Matches(a));
3225}
3226
3227// Tests that Property() can describe itself.
3228TEST(PropertyTest, CanDescribeSelf) {
3229 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3230
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003231 EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
3232 EXPECT_EQ("is an object whose given property isn't >= 0",
3233 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003234}
3235
3236// Tests that Property() can explain the match result.
3237TEST(PropertyTest, CanExplainMatchResult) {
3238 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3239
3240 AClass a;
3241 a.set_n(1);
zhanyong.wan736baa82010-09-27 17:44:16 +00003242 EXPECT_EQ("whose given property is 1" + OfType("int"), Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003243
3244 m = Property(&AClass::n, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003245 EXPECT_EQ(
3246 "whose given property is 1" + OfType("int") + ", which is 1 more than 0",
3247 Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003248}
3249
3250// Tests that Property() works when the argument is a pointer to const.
3251TEST(PropertyForPointerTest, WorksForPointerToConst) {
3252 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3253
3254 AClass a;
3255 a.set_n(1);
3256 EXPECT_TRUE(m.Matches(&a));
3257
3258 a.set_n(-1);
3259 EXPECT_FALSE(m.Matches(&a));
3260}
3261
3262// Tests that Property() works when the argument is a pointer to non-const.
3263TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
3264 Matcher<AClass*> m = Property(&AClass::s, StartsWith("hi"));
3265
3266 AClass a;
3267 a.set_s("hill");
3268 EXPECT_TRUE(m.Matches(&a));
3269
3270 a.set_s("hole");
3271 EXPECT_FALSE(m.Matches(&a));
3272}
3273
zhanyong.wan6953a722010-01-13 05:15:07 +00003274// Tests that Property() works when the argument is a reference to a
3275// const pointer.
3276TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) {
3277 Matcher<AClass* const&> m = Property(&AClass::s, StartsWith("hi"));
3278
3279 AClass a;
3280 a.set_s("hill");
3281 EXPECT_TRUE(m.Matches(&a));
3282
3283 a.set_s("hole");
3284 EXPECT_FALSE(m.Matches(&a));
3285}
3286
shiqiane35fdd92008-12-10 05:08:54 +00003287// Tests that Property() does not match the NULL pointer.
3288TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
3289 Matcher<const AClass*> m = Property(&AClass::x, _);
3290 EXPECT_FALSE(m.Matches(NULL));
3291}
3292
3293// Tests that Property(&Foo::property, ...) works when the argument's
3294// type is a sub-type of const Foo*.
3295TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
3296 // The matcher expects a DerivedClass, but inside the Property() we
3297 // say AClass.
3298 Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0));
3299
3300 DerivedClass d;
3301 d.set_n(1);
3302 EXPECT_TRUE(m.Matches(&d));
3303
3304 d.set_n(-1);
3305 EXPECT_FALSE(m.Matches(&d));
3306}
3307
3308// Tests that Property() can describe itself when used to match a pointer.
3309TEST(PropertyForPointerTest, CanDescribeSelf) {
3310 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3311
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003312 EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
3313 EXPECT_EQ("is an object whose given property isn't >= 0",
3314 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003315}
3316
3317// Tests that Property() can explain the result of matching a pointer.
3318TEST(PropertyForPointerTest, CanExplainMatchResult) {
3319 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3320
3321 AClass a;
3322 a.set_n(1);
3323 EXPECT_EQ("", Explain(m, static_cast<const AClass*>(NULL)));
zhanyong.wan736baa82010-09-27 17:44:16 +00003324 EXPECT_EQ(
3325 "which points to an object whose given property is 1" + OfType("int"),
3326 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003327
3328 m = Property(&AClass::n, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003329 EXPECT_EQ("which points to an object whose given property is 1" +
3330 OfType("int") + ", which is 1 more than 0",
3331 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003332}
3333
3334// Tests ResultOf.
3335
3336// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3337// function pointer.
3338string IntToStringFunction(int input) { return input == 1 ? "foo" : "bar"; }
3339
3340TEST(ResultOfTest, WorksForFunctionPointers) {
3341 Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(string("foo")));
3342
3343 EXPECT_TRUE(matcher.Matches(1));
3344 EXPECT_FALSE(matcher.Matches(2));
3345}
3346
3347// Tests that ResultOf() can describe itself.
3348TEST(ResultOfTest, CanDescribeItself) {
3349 Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo"));
3350
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003351 EXPECT_EQ("is mapped by the given callable to a value that "
3352 "is equal to \"foo\"", Describe(matcher));
3353 EXPECT_EQ("is mapped by the given callable to a value that "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003354 "isn't equal to \"foo\"", DescribeNegation(matcher));
shiqiane35fdd92008-12-10 05:08:54 +00003355}
3356
3357// Tests that ResultOf() can explain the match result.
3358int IntFunction(int input) { return input == 42 ? 80 : 90; }
3359
3360TEST(ResultOfTest, CanExplainMatchResult) {
3361 Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
zhanyong.wan736baa82010-09-27 17:44:16 +00003362 EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int"),
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003363 Explain(matcher, 36));
shiqiane35fdd92008-12-10 05:08:54 +00003364
3365 matcher = ResultOf(&IntFunction, GreaterThan(85));
zhanyong.wan736baa82010-09-27 17:44:16 +00003366 EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int") +
3367 ", which is 5 more than 85", Explain(matcher, 36));
shiqiane35fdd92008-12-10 05:08:54 +00003368}
3369
3370// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3371// returns a non-reference.
3372TEST(ResultOfTest, WorksForNonReferenceResults) {
3373 Matcher<int> matcher = ResultOf(&IntFunction, Eq(80));
3374
3375 EXPECT_TRUE(matcher.Matches(42));
3376 EXPECT_FALSE(matcher.Matches(36));
3377}
3378
3379// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3380// returns a reference to non-const.
zhanyong.wan736baa82010-09-27 17:44:16 +00003381double& DoubleFunction(double& input) { return input; } // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00003382
zhanyong.wan736baa82010-09-27 17:44:16 +00003383Uncopyable& RefUncopyableFunction(Uncopyable& obj) { // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00003384 return obj;
3385}
3386
3387TEST(ResultOfTest, WorksForReferenceToNonConstResults) {
3388 double x = 3.14;
3389 double x2 = x;
3390 Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x));
3391
3392 EXPECT_TRUE(matcher.Matches(x));
3393 EXPECT_FALSE(matcher.Matches(x2));
3394
3395 // Test that ResultOf works with uncopyable objects
3396 Uncopyable obj(0);
3397 Uncopyable obj2(0);
3398 Matcher<Uncopyable&> matcher2 =
3399 ResultOf(&RefUncopyableFunction, Ref(obj));
3400
3401 EXPECT_TRUE(matcher2.Matches(obj));
3402 EXPECT_FALSE(matcher2.Matches(obj2));
3403}
3404
3405// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3406// returns a reference to const.
3407const string& StringFunction(const string& input) { return input; }
3408
3409TEST(ResultOfTest, WorksForReferenceToConstResults) {
3410 string s = "foo";
3411 string s2 = s;
3412 Matcher<const string&> matcher = ResultOf(&StringFunction, Ref(s));
3413
3414 EXPECT_TRUE(matcher.Matches(s));
3415 EXPECT_FALSE(matcher.Matches(s2));
3416}
3417
3418// Tests that ResultOf(f, m) works when f(x) and m's
3419// argument types are compatible but different.
3420TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {
3421 // IntFunction() returns int but the inner matcher expects a signed char.
3422 Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85)));
3423
3424 EXPECT_TRUE(matcher.Matches(36));
3425 EXPECT_FALSE(matcher.Matches(42));
3426}
3427
shiqiane35fdd92008-12-10 05:08:54 +00003428// Tests that the program aborts when ResultOf is passed
3429// a NULL function pointer.
3430TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +00003431 EXPECT_DEATH_IF_SUPPORTED(
zhanyong.wan736baa82010-09-27 17:44:16 +00003432 ResultOf(static_cast<string(*)(int dummy)>(NULL), Eq(string("foo"))),
shiqiane35fdd92008-12-10 05:08:54 +00003433 "NULL function pointer is passed into ResultOf\\(\\)\\.");
3434}
shiqiane35fdd92008-12-10 05:08:54 +00003435
3436// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3437// function reference.
3438TEST(ResultOfTest, WorksForFunctionReferences) {
3439 Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq("foo"));
3440 EXPECT_TRUE(matcher.Matches(1));
3441 EXPECT_FALSE(matcher.Matches(2));
3442}
3443
3444// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3445// function object.
3446struct Functor : public ::std::unary_function<int, string> {
3447 result_type operator()(argument_type input) const {
3448 return IntToStringFunction(input);
3449 }
3450};
3451
3452TEST(ResultOfTest, WorksForFunctors) {
3453 Matcher<int> matcher = ResultOf(Functor(), Eq(string("foo")));
3454
3455 EXPECT_TRUE(matcher.Matches(1));
3456 EXPECT_FALSE(matcher.Matches(2));
3457}
3458
3459// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3460// functor with more then one operator() defined. ResultOf() must work
3461// for each defined operator().
3462struct PolymorphicFunctor {
3463 typedef int result_type;
3464 int operator()(int n) { return n; }
3465 int operator()(const char* s) { return static_cast<int>(strlen(s)); }
3466};
3467
3468TEST(ResultOfTest, WorksForPolymorphicFunctors) {
3469 Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5));
3470
3471 EXPECT_TRUE(matcher_int.Matches(10));
3472 EXPECT_FALSE(matcher_int.Matches(2));
3473
3474 Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5));
3475
3476 EXPECT_TRUE(matcher_string.Matches("long string"));
3477 EXPECT_FALSE(matcher_string.Matches("shrt"));
3478}
3479
3480const int* ReferencingFunction(const int& n) { return &n; }
3481
3482struct ReferencingFunctor {
3483 typedef const int* result_type;
3484 result_type operator()(const int& n) { return &n; }
3485};
3486
3487TEST(ResultOfTest, WorksForReferencingCallables) {
3488 const int n = 1;
3489 const int n2 = 1;
3490 Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n));
3491 EXPECT_TRUE(matcher2.Matches(n));
3492 EXPECT_FALSE(matcher2.Matches(n2));
3493
3494 Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n));
3495 EXPECT_TRUE(matcher3.Matches(n));
3496 EXPECT_FALSE(matcher3.Matches(n2));
3497}
3498
shiqiane35fdd92008-12-10 05:08:54 +00003499class DivisibleByImpl {
3500 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003501 explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {}
shiqiane35fdd92008-12-10 05:08:54 +00003502
zhanyong.wandb22c222010-01-28 21:52:29 +00003503 // For testing using ExplainMatchResultTo() with polymorphic matchers.
shiqiane35fdd92008-12-10 05:08:54 +00003504 template <typename T>
zhanyong.wandb22c222010-01-28 21:52:29 +00003505 bool MatchAndExplain(const T& n, MatchResultListener* listener) const {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003506 *listener << "which is " << (n % divider_) << " modulo "
zhanyong.wandb22c222010-01-28 21:52:29 +00003507 << divider_;
shiqiane35fdd92008-12-10 05:08:54 +00003508 return (n % divider_) == 0;
3509 }
3510
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003511 void DescribeTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +00003512 *os << "is divisible by " << divider_;
3513 }
3514
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003515 void DescribeNegationTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +00003516 *os << "is not divisible by " << divider_;
3517 }
3518
zhanyong.wan32de5f52009-12-23 00:13:23 +00003519 void set_divider(int a_divider) { divider_ = a_divider; }
shiqiane35fdd92008-12-10 05:08:54 +00003520 int divider() const { return divider_; }
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003521
shiqiane35fdd92008-12-10 05:08:54 +00003522 private:
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003523 int divider_;
shiqiane35fdd92008-12-10 05:08:54 +00003524};
3525
shiqiane35fdd92008-12-10 05:08:54 +00003526PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) {
3527 return MakePolymorphicMatcher(DivisibleByImpl(n));
3528}
3529
3530// Tests that when AllOf() fails, only the first failing matcher is
3531// asked to explain why.
3532TEST(ExplainMatchResultTest, AllOf_False_False) {
3533 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003534 EXPECT_EQ("which is 1 modulo 4", Explain(m, 5));
shiqiane35fdd92008-12-10 05:08:54 +00003535}
3536
3537// Tests that when AllOf() fails, only the first failing matcher is
3538// asked to explain why.
3539TEST(ExplainMatchResultTest, AllOf_False_True) {
3540 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003541 EXPECT_EQ("which is 2 modulo 4", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003542}
3543
3544// Tests that when AllOf() fails, only the first failing matcher is
3545// asked to explain why.
3546TEST(ExplainMatchResultTest, AllOf_True_False) {
3547 const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003548 EXPECT_EQ("which is 2 modulo 3", Explain(m, 5));
shiqiane35fdd92008-12-10 05:08:54 +00003549}
3550
3551// Tests that when AllOf() succeeds, all matchers are asked to explain
3552// why.
3553TEST(ExplainMatchResultTest, AllOf_True_True) {
3554 const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003555 EXPECT_EQ("which is 0 modulo 2, and which is 0 modulo 3", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003556}
3557
3558TEST(ExplainMatchResultTest, AllOf_True_True_2) {
3559 const Matcher<int> m = AllOf(Ge(2), Le(3));
3560 EXPECT_EQ("", Explain(m, 2));
3561}
3562
3563TEST(ExplainmatcherResultTest, MonomorphicMatcher) {
3564 const Matcher<int> m = GreaterThan(5);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003565 EXPECT_EQ("which is 1 more than 5", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003566}
3567
3568// The following two tests verify that values without a public copy
3569// ctor can be used as arguments to matchers like Eq(), Ge(), and etc
3570// with the help of ByRef().
3571
3572class NotCopyable {
3573 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003574 explicit NotCopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00003575
3576 int value() const { return value_; }
3577
3578 bool operator==(const NotCopyable& rhs) const {
3579 return value() == rhs.value();
3580 }
3581
3582 bool operator>=(const NotCopyable& rhs) const {
3583 return value() >= rhs.value();
3584 }
3585 private:
3586 int value_;
3587
3588 GTEST_DISALLOW_COPY_AND_ASSIGN_(NotCopyable);
3589};
3590
3591TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) {
3592 const NotCopyable const_value1(1);
3593 const Matcher<const NotCopyable&> m = Eq(ByRef(const_value1));
3594
3595 const NotCopyable n1(1), n2(2);
3596 EXPECT_TRUE(m.Matches(n1));
3597 EXPECT_FALSE(m.Matches(n2));
3598}
3599
3600TEST(ByRefTest, AllowsNotCopyableValueInMatchers) {
3601 NotCopyable value2(2);
3602 const Matcher<NotCopyable&> m = Ge(ByRef(value2));
3603
3604 NotCopyable n1(1), n2(2);
3605 EXPECT_FALSE(m.Matches(n1));
3606 EXPECT_TRUE(m.Matches(n2));
3607}
3608
zhanyong.wan320814a2013-03-01 00:20:30 +00003609TEST(IsEmptyTest, ImplementsIsEmpty) {
3610 vector<int> container;
3611 EXPECT_THAT(container, IsEmpty());
3612 container.push_back(0);
3613 EXPECT_THAT(container, Not(IsEmpty()));
3614 container.push_back(1);
3615 EXPECT_THAT(container, Not(IsEmpty()));
3616}
3617
3618TEST(IsEmptyTest, WorksWithString) {
3619 string text;
3620 EXPECT_THAT(text, IsEmpty());
3621 text = "foo";
3622 EXPECT_THAT(text, Not(IsEmpty()));
3623 text = string("\0", 1);
3624 EXPECT_THAT(text, Not(IsEmpty()));
3625}
3626
3627TEST(IsEmptyTest, CanDescribeSelf) {
3628 Matcher<vector<int> > m = IsEmpty();
3629 EXPECT_EQ("is empty", Describe(m));
3630 EXPECT_EQ("isn't empty", DescribeNegation(m));
3631}
3632
3633TEST(IsEmptyTest, ExplainsResult) {
3634 Matcher<vector<int> > m = IsEmpty();
3635 vector<int> container;
3636 EXPECT_EQ("", Explain(m, container));
3637 container.push_back(0);
3638 EXPECT_EQ("whose size is 1", Explain(m, container));
3639}
3640
zhanyong.wana31d9ce2013-03-01 01:50:17 +00003641TEST(SizeIsTest, ImplementsSizeIs) {
3642 vector<int> container;
3643 EXPECT_THAT(container, SizeIs(0));
3644 EXPECT_THAT(container, Not(SizeIs(1)));
3645 container.push_back(0);
3646 EXPECT_THAT(container, Not(SizeIs(0)));
3647 EXPECT_THAT(container, SizeIs(1));
3648 container.push_back(0);
3649 EXPECT_THAT(container, Not(SizeIs(0)));
3650 EXPECT_THAT(container, SizeIs(2));
3651}
3652
3653TEST(SizeIsTest, WorksWithMap) {
3654 map<string, int> container;
3655 EXPECT_THAT(container, SizeIs(0));
3656 EXPECT_THAT(container, Not(SizeIs(1)));
3657 container.insert(make_pair("foo", 1));
3658 EXPECT_THAT(container, Not(SizeIs(0)));
3659 EXPECT_THAT(container, SizeIs(1));
3660 container.insert(make_pair("bar", 2));
3661 EXPECT_THAT(container, Not(SizeIs(0)));
3662 EXPECT_THAT(container, SizeIs(2));
3663}
3664
3665TEST(SizeIsTest, WorksWithReferences) {
3666 vector<int> container;
3667 Matcher<const vector<int>&> m = SizeIs(1);
3668 EXPECT_THAT(container, Not(m));
3669 container.push_back(0);
3670 EXPECT_THAT(container, m);
3671}
3672
3673TEST(SizeIsTest, CanDescribeSelf) {
3674 Matcher<vector<int> > m = SizeIs(2);
3675 EXPECT_EQ("size is equal to 2", Describe(m));
3676 EXPECT_EQ("size isn't equal to 2", DescribeNegation(m));
3677}
3678
3679TEST(SizeIsTest, ExplainsResult) {
3680 Matcher<vector<int> > m1 = SizeIs(2);
3681 Matcher<vector<int> > m2 = SizeIs(Lt(2u));
3682 Matcher<vector<int> > m3 = SizeIs(AnyOf(0, 3));
3683 Matcher<vector<int> > m4 = SizeIs(GreaterThan(1));
3684 vector<int> container;
3685 EXPECT_EQ("whose size 0 doesn't match", Explain(m1, container));
3686 EXPECT_EQ("whose size 0 matches", Explain(m2, container));
3687 EXPECT_EQ("whose size 0 matches", Explain(m3, container));
3688 EXPECT_EQ("whose size 0 doesn't match, which is 1 less than 1",
3689 Explain(m4, container));
3690 container.push_back(0);
3691 container.push_back(0);
3692 EXPECT_EQ("whose size 2 matches", Explain(m1, container));
3693 EXPECT_EQ("whose size 2 doesn't match", Explain(m2, container));
3694 EXPECT_EQ("whose size 2 doesn't match", Explain(m3, container));
3695 EXPECT_EQ("whose size 2 matches, which is 1 more than 1",
3696 Explain(m4, container));
3697}
3698
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003699#if GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003700// Tests ContainerEq with different container types, and
3701// different element types.
3702
3703template <typename T>
zhanyong.wanb8243162009-06-04 05:48:20 +00003704class ContainerEqTest : public testing::Test {};
zhanyong.wan6a896b52009-01-16 01:13:50 +00003705
3706typedef testing::Types<
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003707 set<int>,
3708 vector<size_t>,
3709 multiset<size_t>,
3710 list<int> >
zhanyong.wan6a896b52009-01-16 01:13:50 +00003711 ContainerEqTestTypes;
3712
3713TYPED_TEST_CASE(ContainerEqTest, ContainerEqTestTypes);
3714
3715// Tests that the filled container is equal to itself.
3716TYPED_TEST(ContainerEqTest, EqualsSelf) {
3717 static const int vals[] = {1, 1, 2, 3, 5, 8};
3718 TypeParam my_set(vals, vals + 6);
3719 const Matcher<TypeParam> m = ContainerEq(my_set);
3720 EXPECT_TRUE(m.Matches(my_set));
3721 EXPECT_EQ("", Explain(m, my_set));
3722}
3723
3724// Tests that missing values are reported.
3725TYPED_TEST(ContainerEqTest, ValueMissing) {
3726 static const int vals[] = {1, 1, 2, 3, 5, 8};
3727 static const int test_vals[] = {2, 1, 8, 5};
3728 TypeParam my_set(vals, vals + 6);
3729 TypeParam test_set(test_vals, test_vals + 4);
3730 const Matcher<TypeParam> m = ContainerEq(my_set);
3731 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003732 EXPECT_EQ("which doesn't have these expected elements: 3",
3733 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003734}
3735
3736// Tests that added values are reported.
3737TYPED_TEST(ContainerEqTest, ValueAdded) {
3738 static const int vals[] = {1, 1, 2, 3, 5, 8};
3739 static const int test_vals[] = {1, 2, 3, 5, 8, 46};
3740 TypeParam my_set(vals, vals + 6);
3741 TypeParam test_set(test_vals, test_vals + 6);
3742 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3743 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003744 EXPECT_EQ("which has these unexpected elements: 46", Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003745}
3746
3747// Tests that added and missing values are reported together.
3748TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) {
3749 static const int vals[] = {1, 1, 2, 3, 5, 8};
3750 static const int test_vals[] = {1, 2, 3, 8, 46};
3751 TypeParam my_set(vals, vals + 6);
3752 TypeParam test_set(test_vals, test_vals + 5);
3753 const Matcher<TypeParam> m = ContainerEq(my_set);
3754 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003755 EXPECT_EQ("which has these unexpected elements: 46,\n"
3756 "and doesn't have these expected elements: 5",
3757 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003758}
3759
3760// Tests duplicated value -- expect no explanation.
3761TYPED_TEST(ContainerEqTest, DuplicateDifference) {
3762 static const int vals[] = {1, 1, 2, 3, 5, 8};
3763 static const int test_vals[] = {1, 2, 3, 5, 8};
3764 TypeParam my_set(vals, vals + 6);
3765 TypeParam test_set(test_vals, test_vals + 5);
3766 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3767 // Depending on the container, match may be true or false
3768 // But in any case there should be no explanation.
3769 EXPECT_EQ("", Explain(m, test_set));
3770}
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003771#endif // GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003772
3773// Tests that mutliple missing values are reported.
3774// Using just vector here, so order is predicatble.
3775TEST(ContainerEqExtraTest, MultipleValuesMissing) {
3776 static const int vals[] = {1, 1, 2, 3, 5, 8};
3777 static const int test_vals[] = {2, 1, 5};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003778 vector<int> my_set(vals, vals + 6);
3779 vector<int> test_set(test_vals, test_vals + 3);
3780 const Matcher<vector<int> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003781 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003782 EXPECT_EQ("which doesn't have these expected elements: 3, 8",
3783 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003784}
3785
3786// Tests that added values are reported.
3787// Using just vector here, so order is predicatble.
3788TEST(ContainerEqExtraTest, MultipleValuesAdded) {
3789 static const int vals[] = {1, 1, 2, 3, 5, 8};
3790 static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003791 list<size_t> my_set(vals, vals + 6);
3792 list<size_t> test_set(test_vals, test_vals + 7);
3793 const Matcher<const list<size_t>&> m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003794 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003795 EXPECT_EQ("which has these unexpected elements: 92, 46",
3796 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003797}
3798
3799// Tests that added and missing values are reported together.
3800TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) {
3801 static const int vals[] = {1, 1, 2, 3, 5, 8};
3802 static const int test_vals[] = {1, 2, 3, 92, 46};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003803 list<size_t> my_set(vals, vals + 6);
3804 list<size_t> test_set(test_vals, test_vals + 5);
3805 const Matcher<const list<size_t> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003806 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003807 EXPECT_EQ("which has these unexpected elements: 92, 46,\n"
3808 "and doesn't have these expected elements: 5, 8",
zhanyong.wan6a896b52009-01-16 01:13:50 +00003809 Explain(m, test_set));
3810}
3811
3812// Tests to see that duplicate elements are detected,
3813// but (as above) not reported in the explanation.
3814TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) {
3815 static const int vals[] = {1, 1, 2, 3, 5, 8};
3816 static const int test_vals[] = {1, 2, 3, 5, 8};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003817 vector<int> my_set(vals, vals + 6);
3818 vector<int> test_set(test_vals, test_vals + 5);
3819 const Matcher<vector<int> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003820 EXPECT_TRUE(m.Matches(my_set));
3821 EXPECT_FALSE(m.Matches(test_set));
3822 // There is nothing to report when both sets contain all the same values.
3823 EXPECT_EQ("", Explain(m, test_set));
3824}
3825
3826// Tests that ContainerEq works for non-trivial associative containers,
3827// like maps.
3828TEST(ContainerEqExtraTest, WorksForMaps) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003829 map<int, std::string> my_map;
zhanyong.wan6a896b52009-01-16 01:13:50 +00003830 my_map[0] = "a";
3831 my_map[1] = "b";
3832
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003833 map<int, std::string> test_map;
zhanyong.wan6a896b52009-01-16 01:13:50 +00003834 test_map[0] = "aa";
3835 test_map[1] = "b";
3836
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003837 const Matcher<const map<int, std::string>&> m = ContainerEq(my_map);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003838 EXPECT_TRUE(m.Matches(my_map));
3839 EXPECT_FALSE(m.Matches(test_map));
3840
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003841 EXPECT_EQ("which has these unexpected elements: (0, \"aa\"),\n"
3842 "and doesn't have these expected elements: (0, \"a\")",
zhanyong.wan6a896b52009-01-16 01:13:50 +00003843 Explain(m, test_map));
3844}
3845
zhanyong.wanb8243162009-06-04 05:48:20 +00003846TEST(ContainerEqExtraTest, WorksForNativeArray) {
3847 int a1[] = { 1, 2, 3 };
3848 int a2[] = { 1, 2, 3 };
3849 int b[] = { 1, 2, 4 };
3850
3851 EXPECT_THAT(a1, ContainerEq(a2));
3852 EXPECT_THAT(a1, Not(ContainerEq(b)));
3853}
3854
3855TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) {
3856 const char a1[][3] = { "hi", "lo" };
3857 const char a2[][3] = { "hi", "lo" };
3858 const char b[][3] = { "lo", "hi" };
3859
3860 // Tests using ContainerEq() in the first dimension.
3861 EXPECT_THAT(a1, ContainerEq(a2));
3862 EXPECT_THAT(a1, Not(ContainerEq(b)));
3863
3864 // Tests using ContainerEq() in the second dimension.
3865 EXPECT_THAT(a1, ElementsAre(ContainerEq(a2[0]), ContainerEq(a2[1])));
3866 EXPECT_THAT(a1, ElementsAre(Not(ContainerEq(b[0])), ContainerEq(a2[1])));
3867}
3868
3869TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) {
3870 const int a1[] = { 1, 2, 3 };
3871 const int a2[] = { 1, 2, 3 };
3872 const int b[] = { 1, 2, 3, 4 };
3873
zhanyong.wan2661c682009-06-09 05:42:12 +00003874 const int* const p1 = a1;
3875 EXPECT_THAT(make_tuple(p1, 3), ContainerEq(a2));
3876 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(b)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003877
3878 const int c[] = { 1, 3, 2 };
zhanyong.wan2661c682009-06-09 05:42:12 +00003879 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(c)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003880}
3881
3882TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) {
3883 std::string a1[][3] = {
3884 { "hi", "hello", "ciao" },
3885 { "bye", "see you", "ciao" }
3886 };
3887
3888 std::string a2[][3] = {
3889 { "hi", "hello", "ciao" },
3890 { "bye", "see you", "ciao" }
3891 };
3892
3893 const Matcher<const std::string(&)[2][3]> m = ContainerEq(a2);
3894 EXPECT_THAT(a1, m);
3895
3896 a2[0][0] = "ha";
3897 EXPECT_THAT(a1, m);
3898}
3899
zhanyong.wan898725c2011-09-16 16:45:39 +00003900TEST(WhenSortedByTest, WorksForEmptyContainer) {
3901 const vector<int> numbers;
3902 EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre()));
3903 EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1))));
3904}
3905
3906TEST(WhenSortedByTest, WorksForNonEmptyContainer) {
3907 vector<unsigned> numbers;
3908 numbers.push_back(3);
3909 numbers.push_back(1);
3910 numbers.push_back(2);
3911 numbers.push_back(2);
3912 EXPECT_THAT(numbers, WhenSortedBy(greater<unsigned>(),
3913 ElementsAre(3, 2, 2, 1)));
3914 EXPECT_THAT(numbers, Not(WhenSortedBy(greater<unsigned>(),
3915 ElementsAre(1, 2, 2, 3))));
3916}
3917
3918TEST(WhenSortedByTest, WorksForNonVectorContainer) {
3919 list<string> words;
3920 words.push_back("say");
3921 words.push_back("hello");
3922 words.push_back("world");
3923 EXPECT_THAT(words, WhenSortedBy(less<string>(),
3924 ElementsAre("hello", "say", "world")));
3925 EXPECT_THAT(words, Not(WhenSortedBy(less<string>(),
3926 ElementsAre("say", "hello", "world"))));
3927}
3928
3929TEST(WhenSortedByTest, WorksForNativeArray) {
3930 const int numbers[] = { 1, 3, 2, 4 };
3931 const int sorted_numbers[] = { 1, 2, 3, 4 };
3932 EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre(1, 2, 3, 4)));
3933 EXPECT_THAT(numbers, WhenSortedBy(less<int>(),
3934 ElementsAreArray(sorted_numbers)));
3935 EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1, 3, 2, 4))));
3936}
3937
3938TEST(WhenSortedByTest, CanDescribeSelf) {
3939 const Matcher<vector<int> > m = WhenSortedBy(less<int>(), ElementsAre(1, 2));
3940 EXPECT_EQ("(when sorted) has 2 elements where\n"
3941 "element #0 is equal to 1,\n"
3942 "element #1 is equal to 2",
3943 Describe(m));
3944 EXPECT_EQ("(when sorted) doesn't have 2 elements, or\n"
3945 "element #0 isn't equal to 1, or\n"
3946 "element #1 isn't equal to 2",
3947 DescribeNegation(m));
3948}
3949
3950TEST(WhenSortedByTest, ExplainsMatchResult) {
3951 const int a[] = { 2, 1 };
3952 EXPECT_EQ("which is { 1, 2 } when sorted, whose element #0 doesn't match",
3953 Explain(WhenSortedBy(less<int>(), ElementsAre(2, 3)), a));
3954 EXPECT_EQ("which is { 1, 2 } when sorted",
3955 Explain(WhenSortedBy(less<int>(), ElementsAre(1, 2)), a));
3956}
3957
3958// WhenSorted() is a simple wrapper on WhenSortedBy(). Hence we don't
3959// need to test it as exhaustively as we test the latter.
3960
3961TEST(WhenSortedTest, WorksForEmptyContainer) {
3962 const vector<int> numbers;
3963 EXPECT_THAT(numbers, WhenSorted(ElementsAre()));
3964 EXPECT_THAT(numbers, Not(WhenSorted(ElementsAre(1))));
3965}
3966
3967TEST(WhenSortedTest, WorksForNonEmptyContainer) {
3968 list<string> words;
3969 words.push_back("3");
3970 words.push_back("1");
3971 words.push_back("2");
3972 words.push_back("2");
3973 EXPECT_THAT(words, WhenSorted(ElementsAre("1", "2", "2", "3")));
3974 EXPECT_THAT(words, Not(WhenSorted(ElementsAre("3", "1", "2", "2"))));
3975}
3976
zhanyong.wan736baa82010-09-27 17:44:16 +00003977// Tests IsReadableTypeName().
3978
3979TEST(IsReadableTypeNameTest, ReturnsTrueForShortNames) {
3980 EXPECT_TRUE(IsReadableTypeName("int"));
3981 EXPECT_TRUE(IsReadableTypeName("const unsigned char*"));
3982 EXPECT_TRUE(IsReadableTypeName("MyMap<int, void*>"));
3983 EXPECT_TRUE(IsReadableTypeName("void (*)(int, bool)"));
3984}
3985
3986TEST(IsReadableTypeNameTest, ReturnsTrueForLongNonTemplateNonFunctionNames) {
3987 EXPECT_TRUE(IsReadableTypeName("my_long_namespace::MyClassName"));
3988 EXPECT_TRUE(IsReadableTypeName("int [5][6][7][8][9][10][11]"));
3989 EXPECT_TRUE(IsReadableTypeName("my_namespace::MyOuterClass::MyInnerClass"));
3990}
3991
3992TEST(IsReadableTypeNameTest, ReturnsFalseForLongTemplateNames) {
3993 EXPECT_FALSE(
3994 IsReadableTypeName("basic_string<char, std::char_traits<char> >"));
3995 EXPECT_FALSE(IsReadableTypeName("std::vector<int, std::alloc_traits<int> >"));
3996}
3997
3998TEST(IsReadableTypeNameTest, ReturnsFalseForLongFunctionTypeNames) {
3999 EXPECT_FALSE(IsReadableTypeName("void (&)(int, bool, char, float)"));
4000}
4001
zhanyong.wan4a5330d2009-02-19 00:36:44 +00004002// Tests JoinAsTuple().
4003
4004TEST(JoinAsTupleTest, JoinsEmptyTuple) {
4005 EXPECT_EQ("", JoinAsTuple(Strings()));
4006}
4007
4008TEST(JoinAsTupleTest, JoinsOneTuple) {
4009 const char* fields[] = { "1" };
4010 EXPECT_EQ("1", JoinAsTuple(Strings(fields, fields + 1)));
4011}
4012
4013TEST(JoinAsTupleTest, JoinsTwoTuple) {
4014 const char* fields[] = { "1", "a" };
4015 EXPECT_EQ("(1, a)", JoinAsTuple(Strings(fields, fields + 2)));
4016}
4017
4018TEST(JoinAsTupleTest, JoinsTenTuple) {
4019 const char* fields[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
4020 EXPECT_EQ("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)",
4021 JoinAsTuple(Strings(fields, fields + 10)));
4022}
4023
4024// Tests FormatMatcherDescription().
4025
4026TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) {
4027 EXPECT_EQ("is even",
zhanyong.wanb4140802010-06-08 22:53:57 +00004028 FormatMatcherDescription(false, "IsEven", Strings()));
4029 EXPECT_EQ("not (is even)",
4030 FormatMatcherDescription(true, "IsEven", Strings()));
zhanyong.wan4a5330d2009-02-19 00:36:44 +00004031
4032 const char* params[] = { "5" };
4033 EXPECT_EQ("equals 5",
zhanyong.wanb4140802010-06-08 22:53:57 +00004034 FormatMatcherDescription(false, "Equals",
zhanyong.wan4a5330d2009-02-19 00:36:44 +00004035 Strings(params, params + 1)));
4036
4037 const char* params2[] = { "5", "8" };
4038 EXPECT_EQ("is in range (5, 8)",
zhanyong.wanb4140802010-06-08 22:53:57 +00004039 FormatMatcherDescription(false, "IsInRange",
zhanyong.wan4a5330d2009-02-19 00:36:44 +00004040 Strings(params2, params2 + 2)));
4041}
4042
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00004043// Tests PolymorphicMatcher::mutable_impl().
4044TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {
4045 PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
4046 DivisibleByImpl& impl = m.mutable_impl();
4047 EXPECT_EQ(42, impl.divider());
4048
4049 impl.set_divider(0);
4050 EXPECT_EQ(0, m.mutable_impl().divider());
4051}
4052
4053// Tests PolymorphicMatcher::impl().
4054TEST(PolymorphicMatcherTest, CanAccessImpl) {
4055 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
4056 const DivisibleByImpl& impl = m.impl();
4057 EXPECT_EQ(42, impl.divider());
4058}
4059
zhanyong.wanb1c7f932010-03-24 17:35:11 +00004060TEST(MatcherTupleTest, ExplainsMatchFailure) {
4061 stringstream ss1;
4062 ExplainMatchFailureTupleTo(make_tuple(Matcher<char>(Eq('a')), GreaterThan(5)),
4063 make_tuple('a', 10), &ss1);
4064 EXPECT_EQ("", ss1.str()); // Successful match.
4065
4066 stringstream ss2;
4067 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
4068 make_tuple(2, 'b'), &ss2);
4069 EXPECT_EQ(" Expected arg #0: is > 5\n"
4070 " Actual: 2, which is 3 less than 5\n"
zhanyong.wand60c5f42010-07-21 22:21:07 +00004071 " Expected arg #1: is equal to 'a' (97, 0x61)\n"
4072 " Actual: 'b' (98, 0x62)\n",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00004073 ss2.str()); // Failed match where both arguments need explanation.
4074
4075 stringstream ss3;
4076 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
4077 make_tuple(2, 'a'), &ss3);
4078 EXPECT_EQ(" Expected arg #0: is > 5\n"
4079 " Actual: 2, which is 3 less than 5\n",
4080 ss3.str()); // Failed match where only one argument needs
4081 // explanation.
4082}
4083
zhanyong.wan33605ba2010-04-22 23:37:47 +00004084// Tests Each().
4085
4086TEST(EachTest, ExplainsMatchResultCorrectly) {
4087 set<int> a; // empty
4088
4089 Matcher<set<int> > m = Each(2);
4090 EXPECT_EQ("", Explain(m, a));
4091
zhanyong.wan736baa82010-09-27 17:44:16 +00004092 Matcher<const int(&)[1]> n = Each(1); // NOLINT
zhanyong.wan33605ba2010-04-22 23:37:47 +00004093
4094 const int b[1] = { 1 };
4095 EXPECT_EQ("", Explain(n, b));
4096
4097 n = Each(3);
4098 EXPECT_EQ("whose element #0 doesn't match", Explain(n, b));
4099
4100 a.insert(1);
4101 a.insert(2);
4102 a.insert(3);
4103 m = Each(GreaterThan(0));
4104 EXPECT_EQ("", Explain(m, a));
4105
4106 m = Each(GreaterThan(10));
4107 EXPECT_EQ("whose element #0 doesn't match, which is 9 less than 10",
4108 Explain(m, a));
4109}
4110
4111TEST(EachTest, DescribesItselfCorrectly) {
4112 Matcher<vector<int> > m = Each(1);
4113 EXPECT_EQ("only contains elements that is equal to 1", Describe(m));
4114
4115 Matcher<vector<int> > m2 = Not(m);
4116 EXPECT_EQ("contains some element that isn't equal to 1", Describe(m2));
4117}
4118
4119TEST(EachTest, MatchesVectorWhenAllElementsMatch) {
4120 vector<int> some_vector;
4121 EXPECT_THAT(some_vector, Each(1));
4122 some_vector.push_back(3);
4123 EXPECT_THAT(some_vector, Not(Each(1)));
4124 EXPECT_THAT(some_vector, Each(3));
4125 some_vector.push_back(1);
4126 some_vector.push_back(2);
4127 EXPECT_THAT(some_vector, Not(Each(3)));
4128 EXPECT_THAT(some_vector, Each(Lt(3.5)));
4129
4130 vector<string> another_vector;
4131 another_vector.push_back("fee");
4132 EXPECT_THAT(another_vector, Each(string("fee")));
4133 another_vector.push_back("fie");
4134 another_vector.push_back("foe");
4135 another_vector.push_back("fum");
4136 EXPECT_THAT(another_vector, Not(Each(string("fee"))));
4137}
4138
4139TEST(EachTest, MatchesMapWhenAllElementsMatch) {
4140 map<const char*, int> my_map;
4141 const char* bar = "a string";
4142 my_map[bar] = 2;
4143 EXPECT_THAT(my_map, Each(make_pair(bar, 2)));
4144
4145 map<string, int> another_map;
4146 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1)));
4147 another_map["fee"] = 1;
4148 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1)));
4149 another_map["fie"] = 2;
4150 another_map["foe"] = 3;
4151 another_map["fum"] = 4;
4152 EXPECT_THAT(another_map, Not(Each(make_pair(string("fee"), 1))));
4153 EXPECT_THAT(another_map, Not(Each(make_pair(string("fum"), 1))));
4154 EXPECT_THAT(another_map, Each(Pair(_, Gt(0))));
4155}
4156
4157TEST(EachTest, AcceptsMatcher) {
4158 const int a[] = { 1, 2, 3 };
4159 EXPECT_THAT(a, Each(Gt(0)));
4160 EXPECT_THAT(a, Not(Each(Gt(1))));
4161}
4162
4163TEST(EachTest, WorksForNativeArrayAsTuple) {
4164 const int a[] = { 1, 2 };
4165 const int* const pointer = a;
4166 EXPECT_THAT(make_tuple(pointer, 2), Each(Gt(0)));
4167 EXPECT_THAT(make_tuple(pointer, 2), Not(Each(Gt(1))));
4168}
4169
zhanyong.wanab5b77c2010-05-17 19:32:48 +00004170// For testing Pointwise().
4171class IsHalfOfMatcher {
4172 public:
4173 template <typename T1, typename T2>
4174 bool MatchAndExplain(const tuple<T1, T2>& a_pair,
4175 MatchResultListener* listener) const {
4176 if (get<0>(a_pair) == get<1>(a_pair)/2) {
4177 *listener << "where the second is " << get<1>(a_pair);
4178 return true;
4179 } else {
4180 *listener << "where the second/2 is " << get<1>(a_pair)/2;
4181 return false;
4182 }
4183 }
4184
4185 void DescribeTo(ostream* os) const {
4186 *os << "are a pair where the first is half of the second";
4187 }
4188
4189 void DescribeNegationTo(ostream* os) const {
4190 *os << "are a pair where the first isn't half of the second";
4191 }
4192};
4193
4194PolymorphicMatcher<IsHalfOfMatcher> IsHalfOf() {
4195 return MakePolymorphicMatcher(IsHalfOfMatcher());
4196}
4197
4198TEST(PointwiseTest, DescribesSelf) {
4199 vector<int> rhs;
4200 rhs.push_back(1);
4201 rhs.push_back(2);
4202 rhs.push_back(3);
4203 const Matcher<const vector<int>&> m = Pointwise(IsHalfOf(), rhs);
4204 EXPECT_EQ("contains 3 values, where each value and its corresponding value "
4205 "in { 1, 2, 3 } are a pair where the first is half of the second",
4206 Describe(m));
4207 EXPECT_EQ("doesn't contain exactly 3 values, or contains a value x at some "
4208 "index i where x and the i-th value of { 1, 2, 3 } are a pair "
4209 "where the first isn't half of the second",
4210 DescribeNegation(m));
4211}
4212
4213TEST(PointwiseTest, MakesCopyOfRhs) {
4214 list<signed char> rhs;
4215 rhs.push_back(2);
4216 rhs.push_back(4);
4217
4218 int lhs[] = { 1, 2 };
4219 const Matcher<const int (&)[2]> m = Pointwise(IsHalfOf(), rhs);
4220 EXPECT_THAT(lhs, m);
4221
4222 // Changing rhs now shouldn't affect m, which made a copy of rhs.
4223 rhs.push_back(6);
4224 EXPECT_THAT(lhs, m);
4225}
4226
4227TEST(PointwiseTest, WorksForLhsNativeArray) {
4228 const int lhs[] = { 1, 2, 3 };
4229 vector<int> rhs;
4230 rhs.push_back(2);
4231 rhs.push_back(4);
4232 rhs.push_back(6);
4233 EXPECT_THAT(lhs, Pointwise(Lt(), rhs));
4234 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
4235}
4236
4237TEST(PointwiseTest, WorksForRhsNativeArray) {
4238 const int rhs[] = { 1, 2, 3 };
4239 vector<int> lhs;
4240 lhs.push_back(2);
4241 lhs.push_back(4);
4242 lhs.push_back(6);
4243 EXPECT_THAT(lhs, Pointwise(Gt(), rhs));
4244 EXPECT_THAT(lhs, Not(Pointwise(Lt(), rhs)));
4245}
4246
4247TEST(PointwiseTest, RejectsWrongSize) {
4248 const double lhs[2] = { 1, 2 };
4249 const int rhs[1] = { 0 };
4250 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
4251 EXPECT_EQ("which contains 2 values",
4252 Explain(Pointwise(Gt(), rhs), lhs));
4253
4254 const int rhs2[3] = { 0, 1, 2 };
4255 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs2)));
4256}
4257
4258TEST(PointwiseTest, RejectsWrongContent) {
4259 const double lhs[3] = { 1, 2, 3 };
4260 const int rhs[3] = { 2, 6, 4 };
4261 EXPECT_THAT(lhs, Not(Pointwise(IsHalfOf(), rhs)));
4262 EXPECT_EQ("where the value pair (2, 6) at index #1 don't match, "
4263 "where the second/2 is 3",
4264 Explain(Pointwise(IsHalfOf(), rhs), lhs));
4265}
4266
4267TEST(PointwiseTest, AcceptsCorrectContent) {
4268 const double lhs[3] = { 1, 2, 3 };
4269 const int rhs[3] = { 2, 4, 6 };
4270 EXPECT_THAT(lhs, Pointwise(IsHalfOf(), rhs));
4271 EXPECT_EQ("", Explain(Pointwise(IsHalfOf(), rhs), lhs));
4272}
4273
4274TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) {
4275 const double lhs[3] = { 1, 2, 3 };
4276 const int rhs[3] = { 2, 4, 6 };
4277 const Matcher<tuple<const double&, const int&> > m1 = IsHalfOf();
4278 EXPECT_THAT(lhs, Pointwise(m1, rhs));
4279 EXPECT_EQ("", Explain(Pointwise(m1, rhs), lhs));
4280
4281 // This type works as a tuple<const double&, const int&> can be
4282 // implicitly cast to tuple<double, int>.
4283 const Matcher<tuple<double, int> > m2 = IsHalfOf();
4284 EXPECT_THAT(lhs, Pointwise(m2, rhs));
4285 EXPECT_EQ("", Explain(Pointwise(m2, rhs), lhs));
4286}
4287
shiqiane35fdd92008-12-10 05:08:54 +00004288} // namespace gmock_matchers_test
4289} // namespace testing