blob: e75b06e090bb1cfdaaac0114bb017180ca557ce2 [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;
117using testing::StartsWith;
118using testing::StrCaseEq;
119using testing::StrCaseNe;
120using testing::StrEq;
121using testing::StrNe;
122using testing::Truly;
123using testing::TypedEq;
zhanyong.wanb8243162009-06-04 05:48:20 +0000124using testing::Value;
zhanyong.wan898725c2011-09-16 16:45:39 +0000125using testing::WhenSorted;
126using testing::WhenSortedBy;
shiqiane35fdd92008-12-10 05:08:54 +0000127using testing::_;
zhanyong.wana862f1d2010-03-15 21:23:04 +0000128using testing::internal::DummyMatchResultListener;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000129using testing::internal::ExplainMatchFailureTupleTo;
shiqiane35fdd92008-12-10 05:08:54 +0000130using testing::internal::FloatingEqMatcher;
zhanyong.wanb4140802010-06-08 22:53:57 +0000131using testing::internal::FormatMatcherDescription;
zhanyong.wan736baa82010-09-27 17:44:16 +0000132using testing::internal::IsReadableTypeName;
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000133using testing::internal::JoinAsTuple;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000134using testing::internal::RE;
zhanyong.wana862f1d2010-03-15 21:23:04 +0000135using testing::internal::StreamMatchResultListener;
zhanyong.wan34b034c2010-03-05 21:23:23 +0000136using testing::internal::StringMatchResultListener;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000137using testing::internal::Strings;
vladlosev79b83502009-11-18 00:43:37 +0000138using testing::internal::linked_ptr;
vladloseve56daa72009-11-18 01:08:08 +0000139using testing::internal::scoped_ptr;
shiqiane35fdd92008-12-10 05:08:54 +0000140using testing::internal::string;
141
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000142// For testing ExplainMatchResultTo().
143class GreaterThanMatcher : public MatcherInterface<int> {
144 public:
145 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
146
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000147 virtual void DescribeTo(ostream* os) const {
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000148 *os << "is > " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000149 }
150
zhanyong.wandb22c222010-01-28 21:52:29 +0000151 virtual bool MatchAndExplain(int lhs,
152 MatchResultListener* listener) const {
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000153 const int diff = lhs - rhs_;
154 if (diff > 0) {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000155 *listener << "which is " << diff << " more than " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000156 } else if (diff == 0) {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000157 *listener << "which is the same as " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000158 } else {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000159 *listener << "which is " << -diff << " less than " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000160 }
zhanyong.wandb22c222010-01-28 21:52:29 +0000161
162 return lhs > rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000163 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000164
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000165 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000166 int rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000167};
168
169Matcher<int> GreaterThan(int n) {
170 return MakeMatcher(new GreaterThanMatcher(n));
171}
172
zhanyong.wan736baa82010-09-27 17:44:16 +0000173string OfType(const string& type_name) {
174#if GTEST_HAS_RTTI
175 return " (of type " + type_name + ")";
176#else
177 return "";
178#endif
179}
180
shiqiane35fdd92008-12-10 05:08:54 +0000181// Returns the description of the given matcher.
182template <typename T>
183string Describe(const Matcher<T>& m) {
184 stringstream ss;
185 m.DescribeTo(&ss);
186 return ss.str();
187}
188
189// Returns the description of the negation of the given matcher.
190template <typename T>
191string DescribeNegation(const Matcher<T>& m) {
192 stringstream ss;
193 m.DescribeNegationTo(&ss);
194 return ss.str();
195}
196
197// Returns the reason why x matches, or doesn't match, m.
198template <typename MatcherType, typename Value>
199string Explain(const MatcherType& m, const Value& x) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000200 StringMatchResultListener listener;
201 ExplainMatchResult(m, x, &listener);
202 return listener.str();
shiqiane35fdd92008-12-10 05:08:54 +0000203}
204
zhanyong.wana862f1d2010-03-15 21:23:04 +0000205TEST(MatchResultListenerTest, StreamingWorks) {
206 StringMatchResultListener listener;
207 listener << "hi" << 5;
208 EXPECT_EQ("hi5", listener.str());
209
210 // Streaming shouldn't crash when the underlying ostream is NULL.
211 DummyMatchResultListener dummy;
212 dummy << "hi" << 5;
213}
214
215TEST(MatchResultListenerTest, CanAccessUnderlyingStream) {
216 EXPECT_TRUE(DummyMatchResultListener().stream() == NULL);
217 EXPECT_TRUE(StreamMatchResultListener(NULL).stream() == NULL);
218
219 EXPECT_EQ(&std::cout, StreamMatchResultListener(&std::cout).stream());
220}
221
222TEST(MatchResultListenerTest, IsInterestedWorks) {
223 EXPECT_TRUE(StringMatchResultListener().IsInterested());
224 EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested());
225
226 EXPECT_FALSE(DummyMatchResultListener().IsInterested());
227 EXPECT_FALSE(StreamMatchResultListener(NULL).IsInterested());
228}
229
shiqiane35fdd92008-12-10 05:08:54 +0000230// Makes sure that the MatcherInterface<T> interface doesn't
231// change.
232class EvenMatcherImpl : public MatcherInterface<int> {
233 public:
zhanyong.wandb22c222010-01-28 21:52:29 +0000234 virtual bool MatchAndExplain(int x,
235 MatchResultListener* /* listener */) const {
236 return x % 2 == 0;
237 }
shiqiane35fdd92008-12-10 05:08:54 +0000238
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000239 virtual void DescribeTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +0000240 *os << "is an even number";
241 }
242
243 // We deliberately don't define DescribeNegationTo() and
244 // ExplainMatchResultTo() here, to make sure the definition of these
245 // two methods is optional.
246};
247
zhanyong.wana862f1d2010-03-15 21:23:04 +0000248// Makes sure that the MatcherInterface API doesn't change.
249TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) {
shiqiane35fdd92008-12-10 05:08:54 +0000250 EvenMatcherImpl m;
251}
252
zhanyong.wan82113312010-01-08 21:55:40 +0000253// Tests implementing a monomorphic matcher using MatchAndExplain().
254
255class NewEvenMatcherImpl : public MatcherInterface<int> {
256 public:
257 virtual bool MatchAndExplain(int x, MatchResultListener* listener) const {
258 const bool match = x % 2 == 0;
259 // Verifies that we can stream to a listener directly.
260 *listener << "value % " << 2;
261 if (listener->stream() != NULL) {
262 // Verifies that we can stream to a listener's underlying stream
263 // too.
264 *listener->stream() << " == " << (x % 2);
265 }
266 return match;
267 }
268
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000269 virtual void DescribeTo(ostream* os) const {
zhanyong.wan82113312010-01-08 21:55:40 +0000270 *os << "is an even number";
271 }
272};
273
274TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {
275 Matcher<int> m = MakeMatcher(new NewEvenMatcherImpl);
276 EXPECT_TRUE(m.Matches(2));
277 EXPECT_FALSE(m.Matches(3));
278 EXPECT_EQ("value % 2 == 0", Explain(m, 2));
279 EXPECT_EQ("value % 2 == 1", Explain(m, 3));
280}
281
shiqiane35fdd92008-12-10 05:08:54 +0000282// Tests default-constructing a matcher.
283TEST(MatcherTest, CanBeDefaultConstructed) {
284 Matcher<double> m;
285}
286
287// Tests that Matcher<T> can be constructed from a MatcherInterface<T>*.
288TEST(MatcherTest, CanBeConstructedFromMatcherInterface) {
289 const MatcherInterface<int>* impl = new EvenMatcherImpl;
290 Matcher<int> m(impl);
291 EXPECT_TRUE(m.Matches(4));
292 EXPECT_FALSE(m.Matches(5));
293}
294
295// Tests that value can be used in place of Eq(value).
296TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) {
297 Matcher<int> m1 = 5;
298 EXPECT_TRUE(m1.Matches(5));
299 EXPECT_FALSE(m1.Matches(6));
300}
301
302// Tests that NULL can be used in place of Eq(NULL).
303TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) {
304 Matcher<int*> m1 = NULL;
305 EXPECT_TRUE(m1.Matches(NULL));
306 int n = 0;
307 EXPECT_FALSE(m1.Matches(&n));
308}
309
310// Tests that matchers are copyable.
311TEST(MatcherTest, IsCopyable) {
312 // Tests the copy constructor.
313 Matcher<bool> m1 = Eq(false);
314 EXPECT_TRUE(m1.Matches(false));
315 EXPECT_FALSE(m1.Matches(true));
316
317 // Tests the assignment operator.
318 m1 = Eq(true);
319 EXPECT_TRUE(m1.Matches(true));
320 EXPECT_FALSE(m1.Matches(false));
321}
322
323// Tests that Matcher<T>::DescribeTo() calls
324// MatcherInterface<T>::DescribeTo().
325TEST(MatcherTest, CanDescribeItself) {
326 EXPECT_EQ("is an even number",
327 Describe(Matcher<int>(new EvenMatcherImpl)));
328}
329
zhanyong.wan82113312010-01-08 21:55:40 +0000330// Tests Matcher<T>::MatchAndExplain().
331TEST(MatcherTest, MatchAndExplain) {
332 Matcher<int> m = GreaterThan(0);
zhanyong.wan34b034c2010-03-05 21:23:23 +0000333 StringMatchResultListener listener1;
zhanyong.wan82113312010-01-08 21:55:40 +0000334 EXPECT_TRUE(m.MatchAndExplain(42, &listener1));
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000335 EXPECT_EQ("which is 42 more than 0", listener1.str());
zhanyong.wan82113312010-01-08 21:55:40 +0000336
zhanyong.wan34b034c2010-03-05 21:23:23 +0000337 StringMatchResultListener listener2;
zhanyong.wan82113312010-01-08 21:55:40 +0000338 EXPECT_FALSE(m.MatchAndExplain(-9, &listener2));
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000339 EXPECT_EQ("which is 9 less than 0", listener2.str());
zhanyong.wan82113312010-01-08 21:55:40 +0000340}
341
shiqiane35fdd92008-12-10 05:08:54 +0000342// Tests that a C-string literal can be implicitly converted to a
343// Matcher<string> or Matcher<const string&>.
344TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
345 Matcher<string> m1 = "hi";
346 EXPECT_TRUE(m1.Matches("hi"));
347 EXPECT_FALSE(m1.Matches("hello"));
348
349 Matcher<const string&> m2 = "hi";
350 EXPECT_TRUE(m2.Matches("hi"));
351 EXPECT_FALSE(m2.Matches("hello"));
352}
353
354// Tests that a string object can be implicitly converted to a
355// Matcher<string> or Matcher<const string&>.
356TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {
357 Matcher<string> m1 = string("hi");
358 EXPECT_TRUE(m1.Matches("hi"));
359 EXPECT_FALSE(m1.Matches("hello"));
360
361 Matcher<const string&> m2 = string("hi");
362 EXPECT_TRUE(m2.Matches("hi"));
363 EXPECT_FALSE(m2.Matches("hello"));
364}
365
366// Tests that MakeMatcher() constructs a Matcher<T> from a
367// MatcherInterface* without requiring the user to explicitly
368// write the type.
369TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) {
370 const MatcherInterface<int>* dummy_impl = NULL;
371 Matcher<int> m = MakeMatcher(dummy_impl);
372}
373
zhanyong.wan82113312010-01-08 21:55:40 +0000374// Tests that MakePolymorphicMatcher() can construct a polymorphic
375// matcher from its implementation using the old API.
zhanyong.wan33605ba2010-04-22 23:37:47 +0000376const int g_bar = 1;
shiqiane35fdd92008-12-10 05:08:54 +0000377class ReferencesBarOrIsZeroImpl {
378 public:
379 template <typename T>
zhanyong.wandb22c222010-01-28 21:52:29 +0000380 bool MatchAndExplain(const T& x,
381 MatchResultListener* /* listener */) const {
shiqiane35fdd92008-12-10 05:08:54 +0000382 const void* p = &x;
zhanyong.wan33605ba2010-04-22 23:37:47 +0000383 return p == &g_bar || x == 0;
shiqiane35fdd92008-12-10 05:08:54 +0000384 }
385
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000386 void DescribeTo(ostream* os) const { *os << "g_bar or zero"; }
shiqiane35fdd92008-12-10 05:08:54 +0000387
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000388 void DescribeNegationTo(ostream* os) const {
zhanyong.wan33605ba2010-04-22 23:37:47 +0000389 *os << "doesn't reference g_bar and is not zero";
shiqiane35fdd92008-12-10 05:08:54 +0000390 }
391};
392
393// This function verifies that MakePolymorphicMatcher() returns a
394// PolymorphicMatcher<T> where T is the argument's type.
395PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() {
396 return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl());
397}
398
zhanyong.wan82113312010-01-08 21:55:40 +0000399TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) {
shiqiane35fdd92008-12-10 05:08:54 +0000400 // Using a polymorphic matcher to match a reference type.
401 Matcher<const int&> m1 = ReferencesBarOrIsZero();
402 EXPECT_TRUE(m1.Matches(0));
403 // Verifies that the identity of a by-reference argument is preserved.
zhanyong.wan33605ba2010-04-22 23:37:47 +0000404 EXPECT_TRUE(m1.Matches(g_bar));
shiqiane35fdd92008-12-10 05:08:54 +0000405 EXPECT_FALSE(m1.Matches(1));
zhanyong.wan33605ba2010-04-22 23:37:47 +0000406 EXPECT_EQ("g_bar or zero", Describe(m1));
shiqiane35fdd92008-12-10 05:08:54 +0000407
408 // Using a polymorphic matcher to match a value type.
409 Matcher<double> m2 = ReferencesBarOrIsZero();
410 EXPECT_TRUE(m2.Matches(0.0));
411 EXPECT_FALSE(m2.Matches(0.1));
zhanyong.wan33605ba2010-04-22 23:37:47 +0000412 EXPECT_EQ("g_bar or zero", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +0000413}
414
zhanyong.wan82113312010-01-08 21:55:40 +0000415// Tests implementing a polymorphic matcher using MatchAndExplain().
416
417class PolymorphicIsEvenImpl {
418 public:
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000419 void DescribeTo(ostream* os) const { *os << "is even"; }
zhanyong.wan82113312010-01-08 21:55:40 +0000420
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000421 void DescribeNegationTo(ostream* os) const {
zhanyong.wan82113312010-01-08 21:55:40 +0000422 *os << "is odd";
423 }
zhanyong.wan82113312010-01-08 21:55:40 +0000424
zhanyong.wandb22c222010-01-28 21:52:29 +0000425 template <typename T>
426 bool MatchAndExplain(const T& x, MatchResultListener* listener) const {
427 // Verifies that we can stream to the listener directly.
428 *listener << "% " << 2;
429 if (listener->stream() != NULL) {
430 // Verifies that we can stream to the listener's underlying stream
431 // too.
432 *listener->stream() << " == " << (x % 2);
433 }
434 return (x % 2) == 0;
zhanyong.wan82113312010-01-08 21:55:40 +0000435 }
zhanyong.wandb22c222010-01-28 21:52:29 +0000436};
zhanyong.wan82113312010-01-08 21:55:40 +0000437
438PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() {
439 return MakePolymorphicMatcher(PolymorphicIsEvenImpl());
440}
441
442TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {
443 // Using PolymorphicIsEven() as a Matcher<int>.
444 const Matcher<int> m1 = PolymorphicIsEven();
445 EXPECT_TRUE(m1.Matches(42));
446 EXPECT_FALSE(m1.Matches(43));
447 EXPECT_EQ("is even", Describe(m1));
448
449 const Matcher<int> not_m1 = Not(m1);
450 EXPECT_EQ("is odd", Describe(not_m1));
451
452 EXPECT_EQ("% 2 == 0", Explain(m1, 42));
453
454 // Using PolymorphicIsEven() as a Matcher<char>.
455 const Matcher<char> m2 = PolymorphicIsEven();
456 EXPECT_TRUE(m2.Matches('\x42'));
457 EXPECT_FALSE(m2.Matches('\x43'));
458 EXPECT_EQ("is even", Describe(m2));
459
460 const Matcher<char> not_m2 = Not(m2);
461 EXPECT_EQ("is odd", Describe(not_m2));
462
463 EXPECT_EQ("% 2 == 0", Explain(m2, '\x42'));
464}
465
shiqiane35fdd92008-12-10 05:08:54 +0000466// Tests that MatcherCast<T>(m) works when m is a polymorphic matcher.
467TEST(MatcherCastTest, FromPolymorphicMatcher) {
468 Matcher<int> m = MatcherCast<int>(Eq(5));
469 EXPECT_TRUE(m.Matches(5));
470 EXPECT_FALSE(m.Matches(6));
471}
472
473// For testing casting matchers between compatible types.
474class IntValue {
475 public:
476 // An int can be statically (although not implicitly) cast to a
477 // IntValue.
zhanyong.wan32de5f52009-12-23 00:13:23 +0000478 explicit IntValue(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +0000479
480 int value() const { return value_; }
481 private:
482 int value_;
483};
484
485// For testing casting matchers between compatible types.
486bool IsPositiveIntValue(const IntValue& foo) {
487 return foo.value() > 0;
488}
489
490// Tests that MatcherCast<T>(m) works when m is a Matcher<U> where T
491// can be statically converted to U.
492TEST(MatcherCastTest, FromCompatibleType) {
493 Matcher<double> m1 = Eq(2.0);
494 Matcher<int> m2 = MatcherCast<int>(m1);
495 EXPECT_TRUE(m2.Matches(2));
496 EXPECT_FALSE(m2.Matches(3));
497
498 Matcher<IntValue> m3 = Truly(IsPositiveIntValue);
499 Matcher<int> m4 = MatcherCast<int>(m3);
500 // In the following, the arguments 1 and 0 are statically converted
501 // to IntValue objects, and then tested by the IsPositiveIntValue()
502 // predicate.
503 EXPECT_TRUE(m4.Matches(1));
504 EXPECT_FALSE(m4.Matches(0));
505}
506
507// Tests that MatcherCast<T>(m) works when m is a Matcher<const T&>.
508TEST(MatcherCastTest, FromConstReferenceToNonReference) {
509 Matcher<const int&> m1 = Eq(0);
510 Matcher<int> m2 = MatcherCast<int>(m1);
511 EXPECT_TRUE(m2.Matches(0));
512 EXPECT_FALSE(m2.Matches(1));
513}
514
515// Tests that MatcherCast<T>(m) works when m is a Matcher<T&>.
516TEST(MatcherCastTest, FromReferenceToNonReference) {
517 Matcher<int&> m1 = Eq(0);
518 Matcher<int> m2 = MatcherCast<int>(m1);
519 EXPECT_TRUE(m2.Matches(0));
520 EXPECT_FALSE(m2.Matches(1));
521}
522
523// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
524TEST(MatcherCastTest, FromNonReferenceToConstReference) {
525 Matcher<int> m1 = Eq(0);
526 Matcher<const int&> m2 = MatcherCast<const int&>(m1);
527 EXPECT_TRUE(m2.Matches(0));
528 EXPECT_FALSE(m2.Matches(1));
529}
530
531// Tests that MatcherCast<T&>(m) works when m is a Matcher<T>.
532TEST(MatcherCastTest, FromNonReferenceToReference) {
533 Matcher<int> m1 = Eq(0);
534 Matcher<int&> m2 = MatcherCast<int&>(m1);
535 int n = 0;
536 EXPECT_TRUE(m2.Matches(n));
537 n = 1;
538 EXPECT_FALSE(m2.Matches(n));
539}
540
541// Tests that MatcherCast<T>(m) works when m is a Matcher<T>.
542TEST(MatcherCastTest, FromSameType) {
543 Matcher<int> m1 = Eq(0);
544 Matcher<int> m2 = MatcherCast<int>(m1);
545 EXPECT_TRUE(m2.Matches(0));
546 EXPECT_FALSE(m2.Matches(1));
547}
548
jgm79a367e2012-04-10 16:02:11 +0000549// Implicitly convertible form any type.
550struct ConvertibleFromAny {
551 ConvertibleFromAny(int a_value) : value(a_value) {}
552 template <typename T>
553 ConvertibleFromAny(const T& a_value) : value(-1) {
554 ADD_FAILURE() << "Conversion constructor called";
555 }
556 int value;
557};
558
559bool operator==(const ConvertibleFromAny& a, const ConvertibleFromAny& b) {
560 return a.value == b.value;
561}
562
563ostream& operator<<(ostream& os, const ConvertibleFromAny& a) {
564 return os << a.value;
565}
566
567TEST(MatcherCastTest, ConversionConstructorIsUsed) {
568 Matcher<ConvertibleFromAny> m = MatcherCast<ConvertibleFromAny>(1);
569 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
570 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
571}
572
573TEST(MatcherCastTest, FromConvertibleFromAny) {
574 Matcher<ConvertibleFromAny> m =
575 MatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
576 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
577 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
578}
579
zhanyong.wan18490652009-05-11 18:54:08 +0000580class Base {};
581class Derived : public Base {};
582
583// Tests that SafeMatcherCast<T>(m) works when m is a polymorphic matcher.
584TEST(SafeMatcherCastTest, FromPolymorphicMatcher) {
585 Matcher<char> m2 = SafeMatcherCast<char>(Eq(32));
586 EXPECT_TRUE(m2.Matches(' '));
587 EXPECT_FALSE(m2.Matches('\n'));
588}
589
zhanyong.wan16cf4732009-05-14 20:55:30 +0000590// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where
591// T and U are arithmetic types and T can be losslessly converted to
592// U.
593TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) {
zhanyong.wan18490652009-05-11 18:54:08 +0000594 Matcher<double> m1 = DoubleEq(1.0);
zhanyong.wan16cf4732009-05-14 20:55:30 +0000595 Matcher<float> m2 = SafeMatcherCast<float>(m1);
596 EXPECT_TRUE(m2.Matches(1.0f));
597 EXPECT_FALSE(m2.Matches(2.0f));
598
599 Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>('a'));
600 EXPECT_TRUE(m3.Matches('a'));
601 EXPECT_FALSE(m3.Matches('b'));
zhanyong.wan18490652009-05-11 18:54:08 +0000602}
603
604// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where T and U
605// are pointers or references to a derived and a base class, correspondingly.
606TEST(SafeMatcherCastTest, FromBaseClass) {
607 Derived d, d2;
608 Matcher<Base*> m1 = Eq(&d);
609 Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1);
610 EXPECT_TRUE(m2.Matches(&d));
611 EXPECT_FALSE(m2.Matches(&d2));
612
613 Matcher<Base&> m3 = Ref(d);
614 Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3);
615 EXPECT_TRUE(m4.Matches(d));
616 EXPECT_FALSE(m4.Matches(d2));
617}
618
619// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<const T&>.
620TEST(SafeMatcherCastTest, FromConstReferenceToReference) {
621 int n = 0;
622 Matcher<const int&> m1 = Ref(n);
623 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
624 int n1 = 0;
625 EXPECT_TRUE(m2.Matches(n));
626 EXPECT_FALSE(m2.Matches(n1));
627}
628
629// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
630TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) {
631 Matcher<int> m1 = Eq(0);
632 Matcher<const int&> m2 = SafeMatcherCast<const int&>(m1);
633 EXPECT_TRUE(m2.Matches(0));
634 EXPECT_FALSE(m2.Matches(1));
635}
636
637// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<T>.
638TEST(SafeMatcherCastTest, FromNonReferenceToReference) {
639 Matcher<int> m1 = Eq(0);
640 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
641 int n = 0;
642 EXPECT_TRUE(m2.Matches(n));
643 n = 1;
644 EXPECT_FALSE(m2.Matches(n));
645}
646
647// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<T>.
648TEST(SafeMatcherCastTest, FromSameType) {
649 Matcher<int> m1 = Eq(0);
650 Matcher<int> m2 = SafeMatcherCast<int>(m1);
651 EXPECT_TRUE(m2.Matches(0));
652 EXPECT_FALSE(m2.Matches(1));
653}
654
jgm79a367e2012-04-10 16:02:11 +0000655TEST(SafeMatcherCastTest, ConversionConstructorIsUsed) {
656 Matcher<ConvertibleFromAny> m = SafeMatcherCast<ConvertibleFromAny>(1);
657 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
658 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
659}
660
661TEST(SafeMatcherCastTest, FromConvertibleFromAny) {
662 Matcher<ConvertibleFromAny> m =
663 SafeMatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
664 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
665 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
666}
667
shiqiane35fdd92008-12-10 05:08:54 +0000668// Tests that A<T>() matches any value of type T.
669TEST(ATest, MatchesAnyValue) {
670 // Tests a matcher for a value type.
671 Matcher<double> m1 = A<double>();
672 EXPECT_TRUE(m1.Matches(91.43));
673 EXPECT_TRUE(m1.Matches(-15.32));
674
675 // Tests a matcher for a reference type.
676 int a = 2;
677 int b = -6;
678 Matcher<int&> m2 = A<int&>();
679 EXPECT_TRUE(m2.Matches(a));
680 EXPECT_TRUE(m2.Matches(b));
681}
682
683// Tests that A<T>() describes itself properly.
684TEST(ATest, CanDescribeSelf) {
685 EXPECT_EQ("is anything", Describe(A<bool>()));
686}
687
688// Tests that An<T>() matches any value of type T.
689TEST(AnTest, MatchesAnyValue) {
690 // Tests a matcher for a value type.
691 Matcher<int> m1 = An<int>();
692 EXPECT_TRUE(m1.Matches(9143));
693 EXPECT_TRUE(m1.Matches(-1532));
694
695 // Tests a matcher for a reference type.
696 int a = 2;
697 int b = -6;
698 Matcher<int&> m2 = An<int&>();
699 EXPECT_TRUE(m2.Matches(a));
700 EXPECT_TRUE(m2.Matches(b));
701}
702
703// Tests that An<T>() describes itself properly.
704TEST(AnTest, CanDescribeSelf) {
705 EXPECT_EQ("is anything", Describe(An<int>()));
706}
707
708// Tests that _ can be used as a matcher for any type and matches any
709// value of that type.
710TEST(UnderscoreTest, MatchesAnyValue) {
711 // Uses _ as a matcher for a value type.
712 Matcher<int> m1 = _;
713 EXPECT_TRUE(m1.Matches(123));
714 EXPECT_TRUE(m1.Matches(-242));
715
716 // Uses _ as a matcher for a reference type.
717 bool a = false;
718 const bool b = true;
719 Matcher<const bool&> m2 = _;
720 EXPECT_TRUE(m2.Matches(a));
721 EXPECT_TRUE(m2.Matches(b));
722}
723
724// Tests that _ describes itself properly.
725TEST(UnderscoreTest, CanDescribeSelf) {
726 Matcher<int> m = _;
727 EXPECT_EQ("is anything", Describe(m));
728}
729
730// Tests that Eq(x) matches any value equal to x.
731TEST(EqTest, MatchesEqualValue) {
732 // 2 C-strings with same content but different addresses.
733 const char a1[] = "hi";
734 const char a2[] = "hi";
735
736 Matcher<const char*> m1 = Eq(a1);
737 EXPECT_TRUE(m1.Matches(a1));
738 EXPECT_FALSE(m1.Matches(a2));
739}
740
741// Tests that Eq(v) describes itself properly.
742
743class Unprintable {
744 public:
745 Unprintable() : c_('a') {}
746
zhanyong.wan32de5f52009-12-23 00:13:23 +0000747 bool operator==(const Unprintable& /* rhs */) { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000748 private:
749 char c_;
750};
751
752TEST(EqTest, CanDescribeSelf) {
753 Matcher<Unprintable> m = Eq(Unprintable());
754 EXPECT_EQ("is equal to 1-byte object <61>", Describe(m));
755}
756
757// Tests that Eq(v) can be used to match any type that supports
758// comparing with type T, where T is v's type.
759TEST(EqTest, IsPolymorphic) {
760 Matcher<int> m1 = Eq(1);
761 EXPECT_TRUE(m1.Matches(1));
762 EXPECT_FALSE(m1.Matches(2));
763
764 Matcher<char> m2 = Eq(1);
765 EXPECT_TRUE(m2.Matches('\1'));
766 EXPECT_FALSE(m2.Matches('a'));
767}
768
769// Tests that TypedEq<T>(v) matches values of type T that's equal to v.
770TEST(TypedEqTest, ChecksEqualityForGivenType) {
771 Matcher<char> m1 = TypedEq<char>('a');
772 EXPECT_TRUE(m1.Matches('a'));
773 EXPECT_FALSE(m1.Matches('b'));
774
775 Matcher<int> m2 = TypedEq<int>(6);
776 EXPECT_TRUE(m2.Matches(6));
777 EXPECT_FALSE(m2.Matches(7));
778}
779
780// Tests that TypedEq(v) describes itself properly.
781TEST(TypedEqTest, CanDescribeSelf) {
782 EXPECT_EQ("is equal to 2", Describe(TypedEq<int>(2)));
783}
784
785// Tests that TypedEq<T>(v) has type Matcher<T>.
786
787// Type<T>::IsTypeOf(v) compiles iff the type of value v is T, where T
788// is a "bare" type (i.e. not in the form of const U or U&). If v's
789// type is not T, the compiler will generate a message about
790// "undefined referece".
791template <typename T>
792struct Type {
zhanyong.wan32de5f52009-12-23 00:13:23 +0000793 static bool IsTypeOf(const T& /* v */) { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000794
795 template <typename T2>
796 static void IsTypeOf(T2 v);
797};
798
799TEST(TypedEqTest, HasSpecifiedType) {
800 // Verfies that the type of TypedEq<T>(v) is Matcher<T>.
801 Type<Matcher<int> >::IsTypeOf(TypedEq<int>(5));
802 Type<Matcher<double> >::IsTypeOf(TypedEq<double>(5));
803}
804
805// Tests that Ge(v) matches anything >= v.
806TEST(GeTest, ImplementsGreaterThanOrEqual) {
807 Matcher<int> m1 = Ge(0);
808 EXPECT_TRUE(m1.Matches(1));
809 EXPECT_TRUE(m1.Matches(0));
810 EXPECT_FALSE(m1.Matches(-1));
811}
812
813// Tests that Ge(v) describes itself properly.
814TEST(GeTest, CanDescribeSelf) {
815 Matcher<int> m = Ge(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000816 EXPECT_EQ("is >= 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000817}
818
819// Tests that Gt(v) matches anything > v.
820TEST(GtTest, ImplementsGreaterThan) {
821 Matcher<double> m1 = Gt(0);
822 EXPECT_TRUE(m1.Matches(1.0));
823 EXPECT_FALSE(m1.Matches(0.0));
824 EXPECT_FALSE(m1.Matches(-1.0));
825}
826
827// Tests that Gt(v) describes itself properly.
828TEST(GtTest, CanDescribeSelf) {
829 Matcher<int> m = Gt(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000830 EXPECT_EQ("is > 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000831}
832
833// Tests that Le(v) matches anything <= v.
834TEST(LeTest, ImplementsLessThanOrEqual) {
835 Matcher<char> m1 = Le('b');
836 EXPECT_TRUE(m1.Matches('a'));
837 EXPECT_TRUE(m1.Matches('b'));
838 EXPECT_FALSE(m1.Matches('c'));
839}
840
841// Tests that Le(v) describes itself properly.
842TEST(LeTest, CanDescribeSelf) {
843 Matcher<int> m = Le(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000844 EXPECT_EQ("is <= 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000845}
846
847// Tests that Lt(v) matches anything < v.
848TEST(LtTest, ImplementsLessThan) {
849 Matcher<const string&> m1 = Lt("Hello");
850 EXPECT_TRUE(m1.Matches("Abc"));
851 EXPECT_FALSE(m1.Matches("Hello"));
852 EXPECT_FALSE(m1.Matches("Hello, world!"));
853}
854
855// Tests that Lt(v) describes itself properly.
856TEST(LtTest, CanDescribeSelf) {
857 Matcher<int> m = Lt(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000858 EXPECT_EQ("is < 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000859}
860
861// Tests that Ne(v) matches anything != v.
862TEST(NeTest, ImplementsNotEqual) {
863 Matcher<int> m1 = Ne(0);
864 EXPECT_TRUE(m1.Matches(1));
865 EXPECT_TRUE(m1.Matches(-1));
866 EXPECT_FALSE(m1.Matches(0));
867}
868
869// Tests that Ne(v) describes itself properly.
870TEST(NeTest, CanDescribeSelf) {
871 Matcher<int> m = Ne(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000872 EXPECT_EQ("isn't equal to 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000873}
874
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000875// Tests that IsNull() matches any NULL pointer of any type.
876TEST(IsNullTest, MatchesNullPointer) {
877 Matcher<int*> m1 = IsNull();
878 int* p1 = NULL;
879 int n = 0;
880 EXPECT_TRUE(m1.Matches(p1));
881 EXPECT_FALSE(m1.Matches(&n));
882
883 Matcher<const char*> m2 = IsNull();
884 const char* p2 = NULL;
885 EXPECT_TRUE(m2.Matches(p2));
886 EXPECT_FALSE(m2.Matches("hi"));
887
zhanyong.wan95b12332009-09-25 18:55:50 +0000888#if !GTEST_OS_SYMBIAN
889 // Nokia's Symbian compiler generates:
890 // gmock-matchers.h: ambiguous access to overloaded function
891 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(void *)'
892 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(const testing::
893 // MatcherInterface<void *> *)'
894 // gmock-matchers.h: (point of instantiation: 'testing::
895 // gmock_matchers_test::IsNullTest_MatchesNullPointer_Test::TestBody()')
896 // gmock-matchers.h: (instantiating: 'testing::PolymorphicMatc
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000897 Matcher<void*> m3 = IsNull();
898 void* p3 = NULL;
899 EXPECT_TRUE(m3.Matches(p3));
900 EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef)));
zhanyong.wan95b12332009-09-25 18:55:50 +0000901#endif
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000902}
903
vladlosev79b83502009-11-18 00:43:37 +0000904TEST(IsNullTest, LinkedPtr) {
905 const Matcher<linked_ptr<int> > m = IsNull();
906 const linked_ptr<int> null_p;
907 const linked_ptr<int> non_null_p(new int);
908
909 EXPECT_TRUE(m.Matches(null_p));
910 EXPECT_FALSE(m.Matches(non_null_p));
911}
912
913TEST(IsNullTest, ReferenceToConstLinkedPtr) {
914 const Matcher<const linked_ptr<double>&> m = IsNull();
915 const linked_ptr<double> null_p;
916 const linked_ptr<double> non_null_p(new double);
917
918 EXPECT_TRUE(m.Matches(null_p));
919 EXPECT_FALSE(m.Matches(non_null_p));
920}
921
vladloseve56daa72009-11-18 01:08:08 +0000922TEST(IsNullTest, ReferenceToConstScopedPtr) {
923 const Matcher<const scoped_ptr<double>&> m = IsNull();
924 const scoped_ptr<double> null_p;
925 const scoped_ptr<double> non_null_p(new double);
926
927 EXPECT_TRUE(m.Matches(null_p));
928 EXPECT_FALSE(m.Matches(non_null_p));
929}
930
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000931// Tests that IsNull() describes itself properly.
932TEST(IsNullTest, CanDescribeSelf) {
933 Matcher<int*> m = IsNull();
934 EXPECT_EQ("is NULL", Describe(m));
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000935 EXPECT_EQ("isn't NULL", DescribeNegation(m));
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000936}
937
shiqiane35fdd92008-12-10 05:08:54 +0000938// Tests that NotNull() matches any non-NULL pointer of any type.
939TEST(NotNullTest, MatchesNonNullPointer) {
940 Matcher<int*> m1 = NotNull();
941 int* p1 = NULL;
942 int n = 0;
943 EXPECT_FALSE(m1.Matches(p1));
944 EXPECT_TRUE(m1.Matches(&n));
945
946 Matcher<const char*> m2 = NotNull();
947 const char* p2 = NULL;
948 EXPECT_FALSE(m2.Matches(p2));
949 EXPECT_TRUE(m2.Matches("hi"));
950}
951
vladlosev79b83502009-11-18 00:43:37 +0000952TEST(NotNullTest, LinkedPtr) {
953 const Matcher<linked_ptr<int> > m = NotNull();
954 const linked_ptr<int> null_p;
955 const linked_ptr<int> non_null_p(new int);
956
957 EXPECT_FALSE(m.Matches(null_p));
958 EXPECT_TRUE(m.Matches(non_null_p));
959}
960
961TEST(NotNullTest, ReferenceToConstLinkedPtr) {
962 const Matcher<const linked_ptr<double>&> m = NotNull();
963 const linked_ptr<double> null_p;
964 const linked_ptr<double> non_null_p(new double);
965
966 EXPECT_FALSE(m.Matches(null_p));
967 EXPECT_TRUE(m.Matches(non_null_p));
968}
969
vladloseve56daa72009-11-18 01:08:08 +0000970TEST(NotNullTest, ReferenceToConstScopedPtr) {
971 const Matcher<const scoped_ptr<double>&> m = NotNull();
972 const scoped_ptr<double> null_p;
973 const scoped_ptr<double> non_null_p(new double);
974
975 EXPECT_FALSE(m.Matches(null_p));
976 EXPECT_TRUE(m.Matches(non_null_p));
977}
978
shiqiane35fdd92008-12-10 05:08:54 +0000979// Tests that NotNull() describes itself properly.
980TEST(NotNullTest, CanDescribeSelf) {
981 Matcher<int*> m = NotNull();
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000982 EXPECT_EQ("isn't NULL", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000983}
984
985// Tests that Ref(variable) matches an argument that references
986// 'variable'.
987TEST(RefTest, MatchesSameVariable) {
988 int a = 0;
989 int b = 0;
990 Matcher<int&> m = Ref(a);
991 EXPECT_TRUE(m.Matches(a));
992 EXPECT_FALSE(m.Matches(b));
993}
994
995// Tests that Ref(variable) describes itself properly.
996TEST(RefTest, CanDescribeSelf) {
997 int n = 5;
998 Matcher<int&> m = Ref(n);
999 stringstream ss;
1000 ss << "references the variable @" << &n << " 5";
1001 EXPECT_EQ(string(ss.str()), Describe(m));
1002}
1003
1004// Test that Ref(non_const_varialbe) can be used as a matcher for a
1005// const reference.
1006TEST(RefTest, CanBeUsedAsMatcherForConstReference) {
1007 int a = 0;
1008 int b = 0;
1009 Matcher<const int&> m = Ref(a);
1010 EXPECT_TRUE(m.Matches(a));
1011 EXPECT_FALSE(m.Matches(b));
1012}
1013
1014// Tests that Ref(variable) is covariant, i.e. Ref(derived) can be
1015// used wherever Ref(base) can be used (Ref(derived) is a sub-type
1016// of Ref(base), but not vice versa.
1017
shiqiane35fdd92008-12-10 05:08:54 +00001018TEST(RefTest, IsCovariant) {
1019 Base base, base2;
1020 Derived derived;
1021 Matcher<const Base&> m1 = Ref(base);
1022 EXPECT_TRUE(m1.Matches(base));
1023 EXPECT_FALSE(m1.Matches(base2));
1024 EXPECT_FALSE(m1.Matches(derived));
1025
1026 m1 = Ref(derived);
1027 EXPECT_TRUE(m1.Matches(derived));
1028 EXPECT_FALSE(m1.Matches(base));
1029 EXPECT_FALSE(m1.Matches(base2));
1030}
1031
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001032TEST(RefTest, ExplainsResult) {
1033 int n = 0;
1034 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), n),
1035 StartsWith("which is located @"));
1036
1037 int m = 0;
1038 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), m),
1039 StartsWith("which is located @"));
1040}
1041
shiqiane35fdd92008-12-10 05:08:54 +00001042// Tests string comparison matchers.
1043
1044TEST(StrEqTest, MatchesEqualString) {
1045 Matcher<const char*> m = StrEq(string("Hello"));
1046 EXPECT_TRUE(m.Matches("Hello"));
1047 EXPECT_FALSE(m.Matches("hello"));
1048 EXPECT_FALSE(m.Matches(NULL));
1049
1050 Matcher<const string&> m2 = StrEq("Hello");
1051 EXPECT_TRUE(m2.Matches("Hello"));
1052 EXPECT_FALSE(m2.Matches("Hi"));
1053}
1054
1055TEST(StrEqTest, CanDescribeSelf) {
vladlosevaa432202011-04-01 21:58:42 +00001056 Matcher<string> m = StrEq("Hi-\'\"?\\\a\b\f\n\r\t\v\xD3");
1057 EXPECT_EQ("is equal to \"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",
shiqiane35fdd92008-12-10 05:08:54 +00001058 Describe(m));
1059
1060 string str("01204500800");
1061 str[3] = '\0';
1062 Matcher<string> m2 = StrEq(str);
1063 EXPECT_EQ("is equal to \"012\\04500800\"", Describe(m2));
1064 str[0] = str[6] = str[7] = str[9] = str[10] = '\0';
1065 Matcher<string> m3 = StrEq(str);
1066 EXPECT_EQ("is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3));
1067}
1068
1069TEST(StrNeTest, MatchesUnequalString) {
1070 Matcher<const char*> m = StrNe("Hello");
1071 EXPECT_TRUE(m.Matches(""));
1072 EXPECT_TRUE(m.Matches(NULL));
1073 EXPECT_FALSE(m.Matches("Hello"));
1074
1075 Matcher<string> m2 = StrNe(string("Hello"));
1076 EXPECT_TRUE(m2.Matches("hello"));
1077 EXPECT_FALSE(m2.Matches("Hello"));
1078}
1079
1080TEST(StrNeTest, CanDescribeSelf) {
1081 Matcher<const char*> m = StrNe("Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001082 EXPECT_EQ("isn't equal to \"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001083}
1084
1085TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
1086 Matcher<const char*> m = StrCaseEq(string("Hello"));
1087 EXPECT_TRUE(m.Matches("Hello"));
1088 EXPECT_TRUE(m.Matches("hello"));
1089 EXPECT_FALSE(m.Matches("Hi"));
1090 EXPECT_FALSE(m.Matches(NULL));
1091
1092 Matcher<const string&> m2 = StrCaseEq("Hello");
1093 EXPECT_TRUE(m2.Matches("hello"));
1094 EXPECT_FALSE(m2.Matches("Hi"));
1095}
1096
1097TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1098 string str1("oabocdooeoo");
1099 string str2("OABOCDOOEOO");
1100 Matcher<const string&> m0 = StrCaseEq(str1);
1101 EXPECT_FALSE(m0.Matches(str2 + string(1, '\0')));
1102
1103 str1[3] = str2[3] = '\0';
1104 Matcher<const string&> m1 = StrCaseEq(str1);
1105 EXPECT_TRUE(m1.Matches(str2));
1106
1107 str1[0] = str1[6] = str1[7] = str1[10] = '\0';
1108 str2[0] = str2[6] = str2[7] = str2[10] = '\0';
1109 Matcher<const string&> m2 = StrCaseEq(str1);
1110 str1[9] = str2[9] = '\0';
1111 EXPECT_FALSE(m2.Matches(str2));
1112
1113 Matcher<const string&> m3 = StrCaseEq(str1);
1114 EXPECT_TRUE(m3.Matches(str2));
1115
1116 EXPECT_FALSE(m3.Matches(str2 + "x"));
1117 str2.append(1, '\0');
1118 EXPECT_FALSE(m3.Matches(str2));
1119 EXPECT_FALSE(m3.Matches(string(str2, 0, 9)));
1120}
1121
1122TEST(StrCaseEqTest, CanDescribeSelf) {
1123 Matcher<string> m = StrCaseEq("Hi");
1124 EXPECT_EQ("is equal to (ignoring case) \"Hi\"", Describe(m));
1125}
1126
1127TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1128 Matcher<const char*> m = StrCaseNe("Hello");
1129 EXPECT_TRUE(m.Matches("Hi"));
1130 EXPECT_TRUE(m.Matches(NULL));
1131 EXPECT_FALSE(m.Matches("Hello"));
1132 EXPECT_FALSE(m.Matches("hello"));
1133
1134 Matcher<string> m2 = StrCaseNe(string("Hello"));
1135 EXPECT_TRUE(m2.Matches(""));
1136 EXPECT_FALSE(m2.Matches("Hello"));
1137}
1138
1139TEST(StrCaseNeTest, CanDescribeSelf) {
1140 Matcher<const char*> m = StrCaseNe("Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001141 EXPECT_EQ("isn't equal to (ignoring case) \"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001142}
1143
1144// Tests that HasSubstr() works for matching string-typed values.
1145TEST(HasSubstrTest, WorksForStringClasses) {
1146 const Matcher<string> m1 = HasSubstr("foo");
1147 EXPECT_TRUE(m1.Matches(string("I love food.")));
1148 EXPECT_FALSE(m1.Matches(string("tofo")));
1149
1150 const Matcher<const std::string&> m2 = HasSubstr("foo");
1151 EXPECT_TRUE(m2.Matches(std::string("I love food.")));
1152 EXPECT_FALSE(m2.Matches(std::string("tofo")));
1153}
1154
1155// Tests that HasSubstr() works for matching C-string-typed values.
1156TEST(HasSubstrTest, WorksForCStrings) {
1157 const Matcher<char*> m1 = HasSubstr("foo");
1158 EXPECT_TRUE(m1.Matches(const_cast<char*>("I love food.")));
1159 EXPECT_FALSE(m1.Matches(const_cast<char*>("tofo")));
1160 EXPECT_FALSE(m1.Matches(NULL));
1161
1162 const Matcher<const char*> m2 = HasSubstr("foo");
1163 EXPECT_TRUE(m2.Matches("I love food."));
1164 EXPECT_FALSE(m2.Matches("tofo"));
1165 EXPECT_FALSE(m2.Matches(NULL));
1166}
1167
1168// Tests that HasSubstr(s) describes itself properly.
1169TEST(HasSubstrTest, CanDescribeSelf) {
1170 Matcher<string> m = HasSubstr("foo\n\"");
1171 EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m));
1172}
1173
zhanyong.wanb5937da2009-07-16 20:26:41 +00001174TEST(KeyTest, CanDescribeSelf) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001175 Matcher<const pair<std::string, int>&> m = Key("foo");
zhanyong.wanb5937da2009-07-16 20:26:41 +00001176 EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001177 EXPECT_EQ("doesn't have a key that is equal to \"foo\"", DescribeNegation(m));
1178}
1179
1180TEST(KeyTest, ExplainsResult) {
1181 Matcher<pair<int, bool> > m = Key(GreaterThan(10));
1182 EXPECT_EQ("whose first field is a value which is 5 less than 10",
1183 Explain(m, make_pair(5, true)));
1184 EXPECT_EQ("whose first field is a value which is 5 more than 10",
1185 Explain(m, make_pair(15, true)));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001186}
1187
1188TEST(KeyTest, MatchesCorrectly) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001189 pair<int, std::string> p(25, "foo");
zhanyong.wanb5937da2009-07-16 20:26:41 +00001190 EXPECT_THAT(p, Key(25));
1191 EXPECT_THAT(p, Not(Key(42)));
1192 EXPECT_THAT(p, Key(Ge(20)));
1193 EXPECT_THAT(p, Not(Key(Lt(25))));
1194}
1195
1196TEST(KeyTest, SafelyCastsInnerMatcher) {
1197 Matcher<int> is_positive = Gt(0);
1198 Matcher<int> is_negative = Lt(0);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001199 pair<char, bool> p('a', true);
zhanyong.wanb5937da2009-07-16 20:26:41 +00001200 EXPECT_THAT(p, Key(is_positive));
1201 EXPECT_THAT(p, Not(Key(is_negative)));
1202}
1203
1204TEST(KeyTest, InsideContainsUsingMap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001205 map<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001206 container.insert(make_pair(1, 'a'));
1207 container.insert(make_pair(2, 'b'));
1208 container.insert(make_pair(4, 'c'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001209 EXPECT_THAT(container, Contains(Key(1)));
1210 EXPECT_THAT(container, Not(Contains(Key(3))));
1211}
1212
1213TEST(KeyTest, InsideContainsUsingMultimap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001214 multimap<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001215 container.insert(make_pair(1, 'a'));
1216 container.insert(make_pair(2, 'b'));
1217 container.insert(make_pair(4, 'c'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001218
1219 EXPECT_THAT(container, Not(Contains(Key(25))));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001220 container.insert(make_pair(25, 'd'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001221 EXPECT_THAT(container, Contains(Key(25)));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001222 container.insert(make_pair(25, 'e'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001223 EXPECT_THAT(container, Contains(Key(25)));
1224
1225 EXPECT_THAT(container, Contains(Key(1)));
1226 EXPECT_THAT(container, Not(Contains(Key(3))));
1227}
1228
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001229TEST(PairTest, Typing) {
1230 // Test verifies the following type conversions can be compiled.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001231 Matcher<const pair<const char*, int>&> m1 = Pair("foo", 42);
1232 Matcher<const pair<const char*, int> > m2 = Pair("foo", 42);
1233 Matcher<pair<const char*, int> > m3 = Pair("foo", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001234
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001235 Matcher<pair<int, const std::string> > m4 = Pair(25, "42");
1236 Matcher<pair<const std::string, int> > m5 = Pair("25", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001237}
1238
1239TEST(PairTest, CanDescribeSelf) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001240 Matcher<const pair<std::string, int>&> m1 = Pair("foo", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001241 EXPECT_EQ("has a first field that is equal to \"foo\""
1242 ", and has a second field that is equal to 42",
1243 Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001244 EXPECT_EQ("has a first field that isn't equal to \"foo\""
1245 ", or has a second field that isn't equal to 42",
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001246 DescribeNegation(m1));
1247 // Double and triple negation (1 or 2 times not and description of negation).
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001248 Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42));
1249 EXPECT_EQ("has a first field that isn't equal to 13"
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001250 ", and has a second field that is equal to 42",
1251 DescribeNegation(m2));
1252}
1253
1254TEST(PairTest, CanExplainMatchResultTo) {
zhanyong.wan82113312010-01-08 21:55:40 +00001255 // If neither field matches, Pair() should explain about the first
1256 // field.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001257 const Matcher<pair<int, int> > m = Pair(GreaterThan(0), GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001258 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001259 Explain(m, make_pair(-1, -2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001260
zhanyong.wan82113312010-01-08 21:55:40 +00001261 // If the first field matches but the second doesn't, Pair() should
1262 // explain about the second field.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001263 EXPECT_EQ("whose second field does not match, which is 2 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001264 Explain(m, make_pair(1, -2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001265
zhanyong.wan82113312010-01-08 21:55:40 +00001266 // If the first field doesn't match but the second does, Pair()
1267 // should explain about the first field.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001268 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001269 Explain(m, make_pair(-1, 2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001270
zhanyong.wan82113312010-01-08 21:55:40 +00001271 // If both fields match, Pair() should explain about them both.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001272 EXPECT_EQ("whose both fields match, where the first field is a value "
1273 "which is 1 more than 0, and the second field is a value "
1274 "which is 2 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001275 Explain(m, make_pair(1, 2)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001276
1277 // If only the first match has an explanation, only this explanation should
1278 // be printed.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001279 const Matcher<pair<int, int> > explain_first = Pair(GreaterThan(0), 0);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001280 EXPECT_EQ("whose both fields match, where the first field is a value "
1281 "which is 1 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001282 Explain(explain_first, make_pair(1, 0)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001283
1284 // If only the second match has an explanation, only this explanation should
1285 // be printed.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001286 const Matcher<pair<int, int> > explain_second = Pair(0, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001287 EXPECT_EQ("whose both fields match, where the second field is a value "
1288 "which is 1 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001289 Explain(explain_second, make_pair(0, 1)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001290}
1291
1292TEST(PairTest, MatchesCorrectly) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001293 pair<int, std::string> p(25, "foo");
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001294
1295 // Both fields match.
1296 EXPECT_THAT(p, Pair(25, "foo"));
1297 EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o")));
1298
1299 // 'first' doesnt' match, but 'second' matches.
1300 EXPECT_THAT(p, Not(Pair(42, "foo")));
1301 EXPECT_THAT(p, Not(Pair(Lt(25), "foo")));
1302
1303 // 'first' matches, but 'second' doesn't match.
1304 EXPECT_THAT(p, Not(Pair(25, "bar")));
1305 EXPECT_THAT(p, Not(Pair(25, Not("foo"))));
1306
1307 // Neither field matches.
1308 EXPECT_THAT(p, Not(Pair(13, "bar")));
1309 EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a"))));
1310}
1311
1312TEST(PairTest, SafelyCastsInnerMatchers) {
1313 Matcher<int> is_positive = Gt(0);
1314 Matcher<int> is_negative = Lt(0);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001315 pair<char, bool> p('a', true);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001316 EXPECT_THAT(p, Pair(is_positive, _));
1317 EXPECT_THAT(p, Not(Pair(is_negative, _)));
1318 EXPECT_THAT(p, Pair(_, is_positive));
1319 EXPECT_THAT(p, Not(Pair(_, is_negative)));
1320}
1321
1322TEST(PairTest, InsideContainsUsingMap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001323 map<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001324 container.insert(make_pair(1, 'a'));
1325 container.insert(make_pair(2, 'b'));
1326 container.insert(make_pair(4, 'c'));
zhanyong.wan95b12332009-09-25 18:55:50 +00001327 EXPECT_THAT(container, Contains(Pair(1, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001328 EXPECT_THAT(container, Contains(Pair(1, _)));
zhanyong.wan95b12332009-09-25 18:55:50 +00001329 EXPECT_THAT(container, Contains(Pair(_, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001330 EXPECT_THAT(container, Not(Contains(Pair(3, _))));
1331}
1332
shiqiane35fdd92008-12-10 05:08:54 +00001333// Tests StartsWith(s).
1334
1335TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
1336 const Matcher<const char*> m1 = StartsWith(string(""));
1337 EXPECT_TRUE(m1.Matches("Hi"));
1338 EXPECT_TRUE(m1.Matches(""));
1339 EXPECT_FALSE(m1.Matches(NULL));
1340
1341 const Matcher<const string&> m2 = StartsWith("Hi");
1342 EXPECT_TRUE(m2.Matches("Hi"));
1343 EXPECT_TRUE(m2.Matches("Hi Hi!"));
1344 EXPECT_TRUE(m2.Matches("High"));
1345 EXPECT_FALSE(m2.Matches("H"));
1346 EXPECT_FALSE(m2.Matches(" Hi"));
1347}
1348
1349TEST(StartsWithTest, CanDescribeSelf) {
1350 Matcher<const std::string> m = StartsWith("Hi");
1351 EXPECT_EQ("starts with \"Hi\"", Describe(m));
1352}
1353
1354// Tests EndsWith(s).
1355
1356TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
1357 const Matcher<const char*> m1 = EndsWith("");
1358 EXPECT_TRUE(m1.Matches("Hi"));
1359 EXPECT_TRUE(m1.Matches(""));
1360 EXPECT_FALSE(m1.Matches(NULL));
1361
1362 const Matcher<const string&> m2 = EndsWith(string("Hi"));
1363 EXPECT_TRUE(m2.Matches("Hi"));
1364 EXPECT_TRUE(m2.Matches("Wow Hi Hi"));
1365 EXPECT_TRUE(m2.Matches("Super Hi"));
1366 EXPECT_FALSE(m2.Matches("i"));
1367 EXPECT_FALSE(m2.Matches("Hi "));
1368}
1369
1370TEST(EndsWithTest, CanDescribeSelf) {
1371 Matcher<const std::string> m = EndsWith("Hi");
1372 EXPECT_EQ("ends with \"Hi\"", Describe(m));
1373}
1374
shiqiane35fdd92008-12-10 05:08:54 +00001375// Tests MatchesRegex().
1376
1377TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
1378 const Matcher<const char*> m1 = MatchesRegex("a.*z");
1379 EXPECT_TRUE(m1.Matches("az"));
1380 EXPECT_TRUE(m1.Matches("abcz"));
1381 EXPECT_FALSE(m1.Matches(NULL));
1382
1383 const Matcher<const string&> m2 = MatchesRegex(new RE("a.*z"));
1384 EXPECT_TRUE(m2.Matches("azbz"));
1385 EXPECT_FALSE(m2.Matches("az1"));
1386 EXPECT_FALSE(m2.Matches("1az"));
1387}
1388
1389TEST(MatchesRegexTest, CanDescribeSelf) {
1390 Matcher<const std::string> m1 = MatchesRegex(string("Hi.*"));
1391 EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1));
1392
zhanyong.wand14aaed2010-01-14 05:36:32 +00001393 Matcher<const char*> m2 = MatchesRegex(new RE("a.*"));
1394 EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +00001395}
1396
1397// Tests ContainsRegex().
1398
1399TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
1400 const Matcher<const char*> m1 = ContainsRegex(string("a.*z"));
1401 EXPECT_TRUE(m1.Matches("az"));
1402 EXPECT_TRUE(m1.Matches("0abcz1"));
1403 EXPECT_FALSE(m1.Matches(NULL));
1404
1405 const Matcher<const string&> m2 = ContainsRegex(new RE("a.*z"));
1406 EXPECT_TRUE(m2.Matches("azbz"));
1407 EXPECT_TRUE(m2.Matches("az1"));
1408 EXPECT_FALSE(m2.Matches("1a"));
1409}
1410
1411TEST(ContainsRegexTest, CanDescribeSelf) {
1412 Matcher<const std::string> m1 = ContainsRegex("Hi.*");
1413 EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1));
1414
zhanyong.wand14aaed2010-01-14 05:36:32 +00001415 Matcher<const char*> m2 = ContainsRegex(new RE("a.*"));
1416 EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +00001417}
shiqiane35fdd92008-12-10 05:08:54 +00001418
1419// Tests for wide strings.
1420#if GTEST_HAS_STD_WSTRING
1421TEST(StdWideStrEqTest, MatchesEqual) {
1422 Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello"));
1423 EXPECT_TRUE(m.Matches(L"Hello"));
1424 EXPECT_FALSE(m.Matches(L"hello"));
1425 EXPECT_FALSE(m.Matches(NULL));
1426
1427 Matcher<const ::std::wstring&> m2 = StrEq(L"Hello");
1428 EXPECT_TRUE(m2.Matches(L"Hello"));
1429 EXPECT_FALSE(m2.Matches(L"Hi"));
1430
1431 Matcher<const ::std::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1432 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1433 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1434
1435 ::std::wstring str(L"01204500800");
1436 str[3] = L'\0';
1437 Matcher<const ::std::wstring&> m4 = StrEq(str);
1438 EXPECT_TRUE(m4.Matches(str));
1439 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1440 Matcher<const ::std::wstring&> m5 = StrEq(str);
1441 EXPECT_TRUE(m5.Matches(str));
1442}
1443
1444TEST(StdWideStrEqTest, CanDescribeSelf) {
vladlosevaa432202011-04-01 21:58:42 +00001445 Matcher< ::std::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v");
1446 EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
shiqiane35fdd92008-12-10 05:08:54 +00001447 Describe(m));
1448
1449 Matcher< ::std::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1450 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1451 Describe(m2));
1452
1453 ::std::wstring str(L"01204500800");
1454 str[3] = L'\0';
1455 Matcher<const ::std::wstring&> m4 = StrEq(str);
1456 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1457 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1458 Matcher<const ::std::wstring&> m5 = StrEq(str);
1459 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1460}
1461
1462TEST(StdWideStrNeTest, MatchesUnequalString) {
1463 Matcher<const wchar_t*> m = StrNe(L"Hello");
1464 EXPECT_TRUE(m.Matches(L""));
1465 EXPECT_TRUE(m.Matches(NULL));
1466 EXPECT_FALSE(m.Matches(L"Hello"));
1467
1468 Matcher< ::std::wstring> m2 = StrNe(::std::wstring(L"Hello"));
1469 EXPECT_TRUE(m2.Matches(L"hello"));
1470 EXPECT_FALSE(m2.Matches(L"Hello"));
1471}
1472
1473TEST(StdWideStrNeTest, CanDescribeSelf) {
1474 Matcher<const wchar_t*> m = StrNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001475 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001476}
1477
1478TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1479 Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello"));
1480 EXPECT_TRUE(m.Matches(L"Hello"));
1481 EXPECT_TRUE(m.Matches(L"hello"));
1482 EXPECT_FALSE(m.Matches(L"Hi"));
1483 EXPECT_FALSE(m.Matches(NULL));
1484
1485 Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello");
1486 EXPECT_TRUE(m2.Matches(L"hello"));
1487 EXPECT_FALSE(m2.Matches(L"Hi"));
1488}
1489
1490TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1491 ::std::wstring str1(L"oabocdooeoo");
1492 ::std::wstring str2(L"OABOCDOOEOO");
1493 Matcher<const ::std::wstring&> m0 = StrCaseEq(str1);
1494 EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L'\0')));
1495
1496 str1[3] = str2[3] = L'\0';
1497 Matcher<const ::std::wstring&> m1 = StrCaseEq(str1);
1498 EXPECT_TRUE(m1.Matches(str2));
1499
1500 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1501 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1502 Matcher<const ::std::wstring&> m2 = StrCaseEq(str1);
1503 str1[9] = str2[9] = L'\0';
1504 EXPECT_FALSE(m2.Matches(str2));
1505
1506 Matcher<const ::std::wstring&> m3 = StrCaseEq(str1);
1507 EXPECT_TRUE(m3.Matches(str2));
1508
1509 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1510 str2.append(1, L'\0');
1511 EXPECT_FALSE(m3.Matches(str2));
1512 EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9)));
1513}
1514
1515TEST(StdWideStrCaseEqTest, CanDescribeSelf) {
1516 Matcher< ::std::wstring> m = StrCaseEq(L"Hi");
1517 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1518}
1519
1520TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1521 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1522 EXPECT_TRUE(m.Matches(L"Hi"));
1523 EXPECT_TRUE(m.Matches(NULL));
1524 EXPECT_FALSE(m.Matches(L"Hello"));
1525 EXPECT_FALSE(m.Matches(L"hello"));
1526
1527 Matcher< ::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello"));
1528 EXPECT_TRUE(m2.Matches(L""));
1529 EXPECT_FALSE(m2.Matches(L"Hello"));
1530}
1531
1532TEST(StdWideStrCaseNeTest, CanDescribeSelf) {
1533 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001534 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001535}
1536
1537// Tests that HasSubstr() works for matching wstring-typed values.
1538TEST(StdWideHasSubstrTest, WorksForStringClasses) {
1539 const Matcher< ::std::wstring> m1 = HasSubstr(L"foo");
1540 EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food.")));
1541 EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo")));
1542
1543 const Matcher<const ::std::wstring&> m2 = HasSubstr(L"foo");
1544 EXPECT_TRUE(m2.Matches(::std::wstring(L"I love food.")));
1545 EXPECT_FALSE(m2.Matches(::std::wstring(L"tofo")));
1546}
1547
1548// Tests that HasSubstr() works for matching C-wide-string-typed values.
1549TEST(StdWideHasSubstrTest, WorksForCStrings) {
1550 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1551 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1552 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1553 EXPECT_FALSE(m1.Matches(NULL));
1554
1555 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1556 EXPECT_TRUE(m2.Matches(L"I love food."));
1557 EXPECT_FALSE(m2.Matches(L"tofo"));
1558 EXPECT_FALSE(m2.Matches(NULL));
1559}
1560
1561// Tests that HasSubstr(s) describes itself properly.
1562TEST(StdWideHasSubstrTest, CanDescribeSelf) {
1563 Matcher< ::std::wstring> m = HasSubstr(L"foo\n\"");
1564 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1565}
1566
1567// Tests StartsWith(s).
1568
1569TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) {
1570 const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L""));
1571 EXPECT_TRUE(m1.Matches(L"Hi"));
1572 EXPECT_TRUE(m1.Matches(L""));
1573 EXPECT_FALSE(m1.Matches(NULL));
1574
1575 const Matcher<const ::std::wstring&> m2 = StartsWith(L"Hi");
1576 EXPECT_TRUE(m2.Matches(L"Hi"));
1577 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1578 EXPECT_TRUE(m2.Matches(L"High"));
1579 EXPECT_FALSE(m2.Matches(L"H"));
1580 EXPECT_FALSE(m2.Matches(L" Hi"));
1581}
1582
1583TEST(StdWideStartsWithTest, CanDescribeSelf) {
1584 Matcher<const ::std::wstring> m = StartsWith(L"Hi");
1585 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1586}
1587
1588// Tests EndsWith(s).
1589
1590TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) {
1591 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1592 EXPECT_TRUE(m1.Matches(L"Hi"));
1593 EXPECT_TRUE(m1.Matches(L""));
1594 EXPECT_FALSE(m1.Matches(NULL));
1595
1596 const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L"Hi"));
1597 EXPECT_TRUE(m2.Matches(L"Hi"));
1598 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1599 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1600 EXPECT_FALSE(m2.Matches(L"i"));
1601 EXPECT_FALSE(m2.Matches(L"Hi "));
1602}
1603
1604TEST(StdWideEndsWithTest, CanDescribeSelf) {
1605 Matcher<const ::std::wstring> m = EndsWith(L"Hi");
1606 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1607}
1608
1609#endif // GTEST_HAS_STD_WSTRING
1610
1611#if GTEST_HAS_GLOBAL_WSTRING
1612TEST(GlobalWideStrEqTest, MatchesEqual) {
1613 Matcher<const wchar_t*> m = StrEq(::wstring(L"Hello"));
1614 EXPECT_TRUE(m.Matches(L"Hello"));
1615 EXPECT_FALSE(m.Matches(L"hello"));
1616 EXPECT_FALSE(m.Matches(NULL));
1617
1618 Matcher<const ::wstring&> m2 = StrEq(L"Hello");
1619 EXPECT_TRUE(m2.Matches(L"Hello"));
1620 EXPECT_FALSE(m2.Matches(L"Hi"));
1621
1622 Matcher<const ::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1623 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1624 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1625
1626 ::wstring str(L"01204500800");
1627 str[3] = L'\0';
1628 Matcher<const ::wstring&> m4 = StrEq(str);
1629 EXPECT_TRUE(m4.Matches(str));
1630 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1631 Matcher<const ::wstring&> m5 = StrEq(str);
1632 EXPECT_TRUE(m5.Matches(str));
1633}
1634
1635TEST(GlobalWideStrEqTest, CanDescribeSelf) {
vladlosevaa432202011-04-01 21:58:42 +00001636 Matcher< ::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v");
1637 EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
shiqiane35fdd92008-12-10 05:08:54 +00001638 Describe(m));
1639
1640 Matcher< ::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1641 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1642 Describe(m2));
1643
1644 ::wstring str(L"01204500800");
1645 str[3] = L'\0';
1646 Matcher<const ::wstring&> m4 = StrEq(str);
1647 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1648 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1649 Matcher<const ::wstring&> m5 = StrEq(str);
1650 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1651}
1652
1653TEST(GlobalWideStrNeTest, MatchesUnequalString) {
1654 Matcher<const wchar_t*> m = StrNe(L"Hello");
1655 EXPECT_TRUE(m.Matches(L""));
1656 EXPECT_TRUE(m.Matches(NULL));
1657 EXPECT_FALSE(m.Matches(L"Hello"));
1658
1659 Matcher< ::wstring> m2 = StrNe(::wstring(L"Hello"));
1660 EXPECT_TRUE(m2.Matches(L"hello"));
1661 EXPECT_FALSE(m2.Matches(L"Hello"));
1662}
1663
1664TEST(GlobalWideStrNeTest, CanDescribeSelf) {
1665 Matcher<const wchar_t*> m = StrNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001666 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001667}
1668
1669TEST(GlobalWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1670 Matcher<const wchar_t*> m = StrCaseEq(::wstring(L"Hello"));
1671 EXPECT_TRUE(m.Matches(L"Hello"));
1672 EXPECT_TRUE(m.Matches(L"hello"));
1673 EXPECT_FALSE(m.Matches(L"Hi"));
1674 EXPECT_FALSE(m.Matches(NULL));
1675
1676 Matcher<const ::wstring&> m2 = StrCaseEq(L"Hello");
1677 EXPECT_TRUE(m2.Matches(L"hello"));
1678 EXPECT_FALSE(m2.Matches(L"Hi"));
1679}
1680
1681TEST(GlobalWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1682 ::wstring str1(L"oabocdooeoo");
1683 ::wstring str2(L"OABOCDOOEOO");
1684 Matcher<const ::wstring&> m0 = StrCaseEq(str1);
1685 EXPECT_FALSE(m0.Matches(str2 + ::wstring(1, L'\0')));
1686
1687 str1[3] = str2[3] = L'\0';
1688 Matcher<const ::wstring&> m1 = StrCaseEq(str1);
1689 EXPECT_TRUE(m1.Matches(str2));
1690
1691 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1692 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1693 Matcher<const ::wstring&> m2 = StrCaseEq(str1);
1694 str1[9] = str2[9] = L'\0';
1695 EXPECT_FALSE(m2.Matches(str2));
1696
1697 Matcher<const ::wstring&> m3 = StrCaseEq(str1);
1698 EXPECT_TRUE(m3.Matches(str2));
1699
1700 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1701 str2.append(1, L'\0');
1702 EXPECT_FALSE(m3.Matches(str2));
1703 EXPECT_FALSE(m3.Matches(::wstring(str2, 0, 9)));
1704}
1705
1706TEST(GlobalWideStrCaseEqTest, CanDescribeSelf) {
1707 Matcher< ::wstring> m = StrCaseEq(L"Hi");
1708 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1709}
1710
1711TEST(GlobalWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1712 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1713 EXPECT_TRUE(m.Matches(L"Hi"));
1714 EXPECT_TRUE(m.Matches(NULL));
1715 EXPECT_FALSE(m.Matches(L"Hello"));
1716 EXPECT_FALSE(m.Matches(L"hello"));
1717
1718 Matcher< ::wstring> m2 = StrCaseNe(::wstring(L"Hello"));
1719 EXPECT_TRUE(m2.Matches(L""));
1720 EXPECT_FALSE(m2.Matches(L"Hello"));
1721}
1722
1723TEST(GlobalWideStrCaseNeTest, CanDescribeSelf) {
1724 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001725 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001726}
1727
1728// Tests that HasSubstr() works for matching wstring-typed values.
1729TEST(GlobalWideHasSubstrTest, WorksForStringClasses) {
1730 const Matcher< ::wstring> m1 = HasSubstr(L"foo");
1731 EXPECT_TRUE(m1.Matches(::wstring(L"I love food.")));
1732 EXPECT_FALSE(m1.Matches(::wstring(L"tofo")));
1733
1734 const Matcher<const ::wstring&> m2 = HasSubstr(L"foo");
1735 EXPECT_TRUE(m2.Matches(::wstring(L"I love food.")));
1736 EXPECT_FALSE(m2.Matches(::wstring(L"tofo")));
1737}
1738
1739// Tests that HasSubstr() works for matching C-wide-string-typed values.
1740TEST(GlobalWideHasSubstrTest, WorksForCStrings) {
1741 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1742 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1743 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1744 EXPECT_FALSE(m1.Matches(NULL));
1745
1746 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1747 EXPECT_TRUE(m2.Matches(L"I love food."));
1748 EXPECT_FALSE(m2.Matches(L"tofo"));
1749 EXPECT_FALSE(m2.Matches(NULL));
1750}
1751
1752// Tests that HasSubstr(s) describes itself properly.
1753TEST(GlobalWideHasSubstrTest, CanDescribeSelf) {
1754 Matcher< ::wstring> m = HasSubstr(L"foo\n\"");
1755 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1756}
1757
1758// Tests StartsWith(s).
1759
1760TEST(GlobalWideStartsWithTest, MatchesStringWithGivenPrefix) {
1761 const Matcher<const wchar_t*> m1 = StartsWith(::wstring(L""));
1762 EXPECT_TRUE(m1.Matches(L"Hi"));
1763 EXPECT_TRUE(m1.Matches(L""));
1764 EXPECT_FALSE(m1.Matches(NULL));
1765
1766 const Matcher<const ::wstring&> m2 = StartsWith(L"Hi");
1767 EXPECT_TRUE(m2.Matches(L"Hi"));
1768 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1769 EXPECT_TRUE(m2.Matches(L"High"));
1770 EXPECT_FALSE(m2.Matches(L"H"));
1771 EXPECT_FALSE(m2.Matches(L" Hi"));
1772}
1773
1774TEST(GlobalWideStartsWithTest, CanDescribeSelf) {
1775 Matcher<const ::wstring> m = StartsWith(L"Hi");
1776 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1777}
1778
1779// Tests EndsWith(s).
1780
1781TEST(GlobalWideEndsWithTest, MatchesStringWithGivenSuffix) {
1782 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1783 EXPECT_TRUE(m1.Matches(L"Hi"));
1784 EXPECT_TRUE(m1.Matches(L""));
1785 EXPECT_FALSE(m1.Matches(NULL));
1786
1787 const Matcher<const ::wstring&> m2 = EndsWith(::wstring(L"Hi"));
1788 EXPECT_TRUE(m2.Matches(L"Hi"));
1789 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1790 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1791 EXPECT_FALSE(m2.Matches(L"i"));
1792 EXPECT_FALSE(m2.Matches(L"Hi "));
1793}
1794
1795TEST(GlobalWideEndsWithTest, CanDescribeSelf) {
1796 Matcher<const ::wstring> m = EndsWith(L"Hi");
1797 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1798}
1799
1800#endif // GTEST_HAS_GLOBAL_WSTRING
1801
1802
1803typedef ::std::tr1::tuple<long, int> Tuple2; // NOLINT
1804
1805// Tests that Eq() matches a 2-tuple where the first field == the
1806// second field.
1807TEST(Eq2Test, MatchesEqualArguments) {
1808 Matcher<const Tuple2&> m = Eq();
1809 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1810 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1811}
1812
1813// Tests that Eq() describes itself properly.
1814TEST(Eq2Test, CanDescribeSelf) {
1815 Matcher<const Tuple2&> m = Eq();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001816 EXPECT_EQ("are an equal pair", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001817}
1818
1819// Tests that Ge() matches a 2-tuple where the first field >= the
1820// second field.
1821TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) {
1822 Matcher<const Tuple2&> m = Ge();
1823 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1824 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1825 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1826}
1827
1828// Tests that Ge() describes itself properly.
1829TEST(Ge2Test, CanDescribeSelf) {
1830 Matcher<const Tuple2&> m = Ge();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001831 EXPECT_EQ("are a pair where the first >= the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001832}
1833
1834// Tests that Gt() matches a 2-tuple where the first field > the
1835// second field.
1836TEST(Gt2Test, MatchesGreaterThanArguments) {
1837 Matcher<const Tuple2&> m = Gt();
1838 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1839 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1840 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1841}
1842
1843// Tests that Gt() describes itself properly.
1844TEST(Gt2Test, CanDescribeSelf) {
1845 Matcher<const Tuple2&> m = Gt();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001846 EXPECT_EQ("are a pair where the first > the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001847}
1848
1849// Tests that Le() matches a 2-tuple where the first field <= the
1850// second field.
1851TEST(Le2Test, MatchesLessThanOrEqualArguments) {
1852 Matcher<const Tuple2&> m = Le();
1853 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1854 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1855 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1856}
1857
1858// Tests that Le() describes itself properly.
1859TEST(Le2Test, CanDescribeSelf) {
1860 Matcher<const Tuple2&> m = Le();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001861 EXPECT_EQ("are a pair where the first <= the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001862}
1863
1864// Tests that Lt() matches a 2-tuple where the first field < the
1865// second field.
1866TEST(Lt2Test, MatchesLessThanArguments) {
1867 Matcher<const Tuple2&> m = Lt();
1868 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1869 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1870 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1871}
1872
1873// Tests that Lt() describes itself properly.
1874TEST(Lt2Test, CanDescribeSelf) {
1875 Matcher<const Tuple2&> m = Lt();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001876 EXPECT_EQ("are a pair where the first < the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001877}
1878
1879// Tests that Ne() matches a 2-tuple where the first field != the
1880// second field.
1881TEST(Ne2Test, MatchesUnequalArguments) {
1882 Matcher<const Tuple2&> m = Ne();
1883 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1884 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1885 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1886}
1887
1888// Tests that Ne() describes itself properly.
1889TEST(Ne2Test, CanDescribeSelf) {
1890 Matcher<const Tuple2&> m = Ne();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001891 EXPECT_EQ("are an unequal pair", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001892}
1893
1894// Tests that Not(m) matches any value that doesn't match m.
1895TEST(NotTest, NegatesMatcher) {
1896 Matcher<int> m;
1897 m = Not(Eq(2));
1898 EXPECT_TRUE(m.Matches(3));
1899 EXPECT_FALSE(m.Matches(2));
1900}
1901
1902// Tests that Not(m) describes itself properly.
1903TEST(NotTest, CanDescribeSelf) {
1904 Matcher<int> m = Not(Eq(5));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001905 EXPECT_EQ("isn't equal to 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001906}
1907
zhanyong.wan18490652009-05-11 18:54:08 +00001908// Tests that monomorphic matchers are safely cast by the Not matcher.
1909TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {
1910 // greater_than_5 is a monomorphic matcher.
1911 Matcher<int> greater_than_5 = Gt(5);
1912
1913 Matcher<const int&> m = Not(greater_than_5);
1914 Matcher<int&> m2 = Not(greater_than_5);
1915 Matcher<int&> m3 = Not(m);
1916}
1917
zhanyong.wan02c15052010-06-09 19:21:30 +00001918// Helper to allow easy testing of AllOf matchers with num parameters.
1919void AllOfMatches(int num, const Matcher<int>& m) {
1920 SCOPED_TRACE(Describe(m));
1921 EXPECT_TRUE(m.Matches(0));
1922 for (int i = 1; i <= num; ++i) {
1923 EXPECT_FALSE(m.Matches(i));
1924 }
1925 EXPECT_TRUE(m.Matches(num + 1));
1926}
1927
shiqiane35fdd92008-12-10 05:08:54 +00001928// Tests that AllOf(m1, ..., mn) matches any value that matches all of
1929// the given matchers.
1930TEST(AllOfTest, MatchesWhenAllMatch) {
1931 Matcher<int> m;
1932 m = AllOf(Le(2), Ge(1));
1933 EXPECT_TRUE(m.Matches(1));
1934 EXPECT_TRUE(m.Matches(2));
1935 EXPECT_FALSE(m.Matches(0));
1936 EXPECT_FALSE(m.Matches(3));
1937
1938 m = AllOf(Gt(0), Ne(1), Ne(2));
1939 EXPECT_TRUE(m.Matches(3));
1940 EXPECT_FALSE(m.Matches(2));
1941 EXPECT_FALSE(m.Matches(1));
1942 EXPECT_FALSE(m.Matches(0));
1943
1944 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
1945 EXPECT_TRUE(m.Matches(4));
1946 EXPECT_FALSE(m.Matches(3));
1947 EXPECT_FALSE(m.Matches(2));
1948 EXPECT_FALSE(m.Matches(1));
1949 EXPECT_FALSE(m.Matches(0));
1950
1951 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
1952 EXPECT_TRUE(m.Matches(0));
1953 EXPECT_TRUE(m.Matches(1));
1954 EXPECT_FALSE(m.Matches(3));
zhanyong.wan02c15052010-06-09 19:21:30 +00001955
1956 // The following tests for varying number of sub-matchers. Due to the way
1957 // the sub-matchers are handled it is enough to test every sub-matcher once
1958 // with sub-matchers using the same matcher type. Varying matcher types are
1959 // checked for above.
1960 AllOfMatches(2, AllOf(Ne(1), Ne(2)));
1961 AllOfMatches(3, AllOf(Ne(1), Ne(2), Ne(3)));
1962 AllOfMatches(4, AllOf(Ne(1), Ne(2), Ne(3), Ne(4)));
1963 AllOfMatches(5, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5)));
1964 AllOfMatches(6, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6)));
1965 AllOfMatches(7, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7)));
1966 AllOfMatches(8, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
1967 Ne(8)));
1968 AllOfMatches(9, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
1969 Ne(8), Ne(9)));
1970 AllOfMatches(10, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8),
1971 Ne(9), Ne(10)));
shiqiane35fdd92008-12-10 05:08:54 +00001972}
1973
1974// Tests that AllOf(m1, ..., mn) describes itself properly.
1975TEST(AllOfTest, CanDescribeSelf) {
1976 Matcher<int> m;
1977 m = AllOf(Le(2), Ge(1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001978 EXPECT_EQ("(is <= 2) and (is >= 1)", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001979
1980 m = AllOf(Gt(0), Ne(1), Ne(2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001981 EXPECT_EQ("(is > 0) and "
1982 "((isn't equal to 1) and "
1983 "(isn't equal to 2))",
shiqiane35fdd92008-12-10 05:08:54 +00001984 Describe(m));
1985
1986
1987 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
jgm79a367e2012-04-10 16:02:11 +00001988 EXPECT_EQ("((is > 0) and "
1989 "(isn't equal to 1)) and "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001990 "((isn't equal to 2) and "
jgm79a367e2012-04-10 16:02:11 +00001991 "(isn't equal to 3))",
shiqiane35fdd92008-12-10 05:08:54 +00001992 Describe(m));
1993
1994
1995 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
jgm79a367e2012-04-10 16:02:11 +00001996 EXPECT_EQ("((is >= 0) and "
1997 "(is < 10)) and "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001998 "((isn't equal to 3) and "
1999 "((isn't equal to 5) and "
jgm79a367e2012-04-10 16:02:11 +00002000 "(isn't equal to 7)))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002001 Describe(m));
2002}
2003
2004// Tests that AllOf(m1, ..., mn) describes its negation properly.
2005TEST(AllOfTest, CanDescribeNegation) {
2006 Matcher<int> m;
2007 m = AllOf(Le(2), Ge(1));
2008 EXPECT_EQ("(isn't <= 2) or "
2009 "(isn't >= 1)",
2010 DescribeNegation(m));
2011
2012 m = AllOf(Gt(0), Ne(1), Ne(2));
2013 EXPECT_EQ("(isn't > 0) or "
2014 "((is equal to 1) or "
2015 "(is equal to 2))",
2016 DescribeNegation(m));
2017
2018
2019 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
jgm79a367e2012-04-10 16:02:11 +00002020 EXPECT_EQ("((isn't > 0) or "
2021 "(is equal to 1)) or "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002022 "((is equal to 2) or "
jgm79a367e2012-04-10 16:02:11 +00002023 "(is equal to 3))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002024 DescribeNegation(m));
2025
2026
2027 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
jgm79a367e2012-04-10 16:02:11 +00002028 EXPECT_EQ("((isn't >= 0) or "
2029 "(isn't < 10)) or "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002030 "((is equal to 3) or "
2031 "((is equal to 5) or "
jgm79a367e2012-04-10 16:02:11 +00002032 "(is equal to 7)))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002033 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00002034}
2035
zhanyong.wan18490652009-05-11 18:54:08 +00002036// Tests that monomorphic matchers are safely cast by the AllOf matcher.
2037TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
2038 // greater_than_5 and less_than_10 are monomorphic matchers.
2039 Matcher<int> greater_than_5 = Gt(5);
2040 Matcher<int> less_than_10 = Lt(10);
2041
2042 Matcher<const int&> m = AllOf(greater_than_5, less_than_10);
2043 Matcher<int&> m2 = AllOf(greater_than_5, less_than_10);
2044 Matcher<int&> m3 = AllOf(greater_than_5, m2);
2045
2046 // Tests that BothOf works when composing itself.
2047 Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10);
2048 Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);
2049}
2050
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002051TEST(AllOfTest, ExplainsResult) {
2052 Matcher<int> m;
2053
2054 // Successful match. Both matchers need to explain. The second
2055 // matcher doesn't give an explanation, so only the first matcher's
2056 // explanation is printed.
2057 m = AllOf(GreaterThan(10), Lt(30));
2058 EXPECT_EQ("which is 15 more than 10", Explain(m, 25));
2059
2060 // Successful match. Both matchers need to explain.
2061 m = AllOf(GreaterThan(10), GreaterThan(20));
2062 EXPECT_EQ("which is 20 more than 10, and which is 10 more than 20",
2063 Explain(m, 30));
2064
2065 // Successful match. All matchers need to explain. The second
2066 // matcher doesn't given an explanation.
2067 m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20));
2068 EXPECT_EQ("which is 15 more than 10, and which is 5 more than 20",
2069 Explain(m, 25));
2070
2071 // Successful match. All matchers need to explain.
2072 m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2073 EXPECT_EQ("which is 30 more than 10, and which is 20 more than 20, "
2074 "and which is 10 more than 30",
2075 Explain(m, 40));
2076
2077 // Failed match. The first matcher, which failed, needs to
2078 // explain.
2079 m = AllOf(GreaterThan(10), GreaterThan(20));
2080 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
2081
2082 // Failed match. The second matcher, which failed, needs to
2083 // explain. Since it doesn't given an explanation, nothing is
2084 // printed.
2085 m = AllOf(GreaterThan(10), Lt(30));
2086 EXPECT_EQ("", Explain(m, 40));
2087
2088 // Failed match. The second matcher, which failed, needs to
2089 // explain.
2090 m = AllOf(GreaterThan(10), GreaterThan(20));
2091 EXPECT_EQ("which is 5 less than 20", Explain(m, 15));
2092}
2093
zhanyong.wan02c15052010-06-09 19:21:30 +00002094// Helper to allow easy testing of AnyOf matchers with num parameters.
2095void AnyOfMatches(int num, const Matcher<int>& m) {
2096 SCOPED_TRACE(Describe(m));
2097 EXPECT_FALSE(m.Matches(0));
2098 for (int i = 1; i <= num; ++i) {
2099 EXPECT_TRUE(m.Matches(i));
2100 }
2101 EXPECT_FALSE(m.Matches(num + 1));
2102}
2103
shiqiane35fdd92008-12-10 05:08:54 +00002104// Tests that AnyOf(m1, ..., mn) matches any value that matches at
2105// least one of the given matchers.
2106TEST(AnyOfTest, MatchesWhenAnyMatches) {
2107 Matcher<int> m;
2108 m = AnyOf(Le(1), Ge(3));
2109 EXPECT_TRUE(m.Matches(1));
2110 EXPECT_TRUE(m.Matches(4));
2111 EXPECT_FALSE(m.Matches(2));
2112
2113 m = AnyOf(Lt(0), Eq(1), Eq(2));
2114 EXPECT_TRUE(m.Matches(-1));
2115 EXPECT_TRUE(m.Matches(1));
2116 EXPECT_TRUE(m.Matches(2));
2117 EXPECT_FALSE(m.Matches(0));
2118
2119 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2120 EXPECT_TRUE(m.Matches(-1));
2121 EXPECT_TRUE(m.Matches(1));
2122 EXPECT_TRUE(m.Matches(2));
2123 EXPECT_TRUE(m.Matches(3));
2124 EXPECT_FALSE(m.Matches(0));
2125
2126 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2127 EXPECT_TRUE(m.Matches(0));
2128 EXPECT_TRUE(m.Matches(11));
2129 EXPECT_TRUE(m.Matches(3));
2130 EXPECT_FALSE(m.Matches(2));
zhanyong.wan02c15052010-06-09 19:21:30 +00002131
2132 // The following tests for varying number of sub-matchers. Due to the way
2133 // the sub-matchers are handled it is enough to test every sub-matcher once
2134 // with sub-matchers using the same matcher type. Varying matcher types are
2135 // checked for above.
2136 AnyOfMatches(2, AnyOf(1, 2));
2137 AnyOfMatches(3, AnyOf(1, 2, 3));
2138 AnyOfMatches(4, AnyOf(1, 2, 3, 4));
2139 AnyOfMatches(5, AnyOf(1, 2, 3, 4, 5));
2140 AnyOfMatches(6, AnyOf(1, 2, 3, 4, 5, 6));
2141 AnyOfMatches(7, AnyOf(1, 2, 3, 4, 5, 6, 7));
2142 AnyOfMatches(8, AnyOf(1, 2, 3, 4, 5, 6, 7, 8));
2143 AnyOfMatches(9, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9));
2144 AnyOfMatches(10, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
shiqiane35fdd92008-12-10 05:08:54 +00002145}
2146
2147// Tests that AnyOf(m1, ..., mn) describes itself properly.
2148TEST(AnyOfTest, CanDescribeSelf) {
2149 Matcher<int> m;
2150 m = AnyOf(Le(1), Ge(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002151 EXPECT_EQ("(is <= 1) or (is >= 3)",
shiqiane35fdd92008-12-10 05:08:54 +00002152 Describe(m));
2153
2154 m = AnyOf(Lt(0), Eq(1), Eq(2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002155 EXPECT_EQ("(is < 0) or "
shiqiane35fdd92008-12-10 05:08:54 +00002156 "((is equal to 1) or (is equal to 2))",
2157 Describe(m));
2158
2159 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
jgm79a367e2012-04-10 16:02:11 +00002160 EXPECT_EQ("((is < 0) or "
2161 "(is equal to 1)) or "
shiqiane35fdd92008-12-10 05:08:54 +00002162 "((is equal to 2) or "
jgm79a367e2012-04-10 16:02:11 +00002163 "(is equal to 3))",
shiqiane35fdd92008-12-10 05:08:54 +00002164 Describe(m));
2165
2166 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
jgm79a367e2012-04-10 16:02:11 +00002167 EXPECT_EQ("((is <= 0) or "
2168 "(is > 10)) or "
shiqiane35fdd92008-12-10 05:08:54 +00002169 "((is equal to 3) or "
2170 "((is equal to 5) or "
jgm79a367e2012-04-10 16:02:11 +00002171 "(is equal to 7)))",
shiqiane35fdd92008-12-10 05:08:54 +00002172 Describe(m));
2173}
2174
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002175// Tests that AnyOf(m1, ..., mn) describes its negation properly.
2176TEST(AnyOfTest, CanDescribeNegation) {
2177 Matcher<int> m;
2178 m = AnyOf(Le(1), Ge(3));
2179 EXPECT_EQ("(isn't <= 1) and (isn't >= 3)",
2180 DescribeNegation(m));
2181
2182 m = AnyOf(Lt(0), Eq(1), Eq(2));
2183 EXPECT_EQ("(isn't < 0) and "
2184 "((isn't equal to 1) and (isn't equal to 2))",
2185 DescribeNegation(m));
2186
2187 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
jgm79a367e2012-04-10 16:02:11 +00002188 EXPECT_EQ("((isn't < 0) and "
2189 "(isn't equal to 1)) and "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002190 "((isn't equal to 2) and "
jgm79a367e2012-04-10 16:02:11 +00002191 "(isn't equal to 3))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002192 DescribeNegation(m));
2193
2194 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
jgm79a367e2012-04-10 16:02:11 +00002195 EXPECT_EQ("((isn't <= 0) and "
2196 "(isn't > 10)) and "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002197 "((isn't equal to 3) and "
2198 "((isn't equal to 5) and "
jgm79a367e2012-04-10 16:02:11 +00002199 "(isn't equal to 7)))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002200 DescribeNegation(m));
2201}
2202
zhanyong.wan18490652009-05-11 18:54:08 +00002203// Tests that monomorphic matchers are safely cast by the AnyOf matcher.
2204TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
2205 // greater_than_5 and less_than_10 are monomorphic matchers.
2206 Matcher<int> greater_than_5 = Gt(5);
2207 Matcher<int> less_than_10 = Lt(10);
2208
2209 Matcher<const int&> m = AnyOf(greater_than_5, less_than_10);
2210 Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10);
2211 Matcher<int&> m3 = AnyOf(greater_than_5, m2);
2212
2213 // Tests that EitherOf works when composing itself.
2214 Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10);
2215 Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);
2216}
2217
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002218TEST(AnyOfTest, ExplainsResult) {
2219 Matcher<int> m;
2220
2221 // Failed match. Both matchers need to explain. The second
2222 // matcher doesn't give an explanation, so only the first matcher's
2223 // explanation is printed.
2224 m = AnyOf(GreaterThan(10), Lt(0));
2225 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
2226
2227 // Failed match. Both matchers need to explain.
2228 m = AnyOf(GreaterThan(10), GreaterThan(20));
2229 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20",
2230 Explain(m, 5));
2231
2232 // Failed match. All matchers need to explain. The second
2233 // matcher doesn't given an explanation.
2234 m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30));
2235 EXPECT_EQ("which is 5 less than 10, and which is 25 less than 30",
2236 Explain(m, 5));
2237
2238 // Failed match. All matchers need to explain.
2239 m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2240 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20, "
2241 "and which is 25 less than 30",
2242 Explain(m, 5));
2243
2244 // Successful match. The first matcher, which succeeded, needs to
2245 // explain.
2246 m = AnyOf(GreaterThan(10), GreaterThan(20));
2247 EXPECT_EQ("which is 5 more than 10", Explain(m, 15));
2248
2249 // Successful match. The second matcher, which succeeded, needs to
2250 // explain. Since it doesn't given an explanation, nothing is
2251 // printed.
2252 m = AnyOf(GreaterThan(10), Lt(30));
2253 EXPECT_EQ("", Explain(m, 0));
2254
2255 // Successful match. The second matcher, which succeeded, needs to
2256 // explain.
2257 m = AnyOf(GreaterThan(30), GreaterThan(20));
2258 EXPECT_EQ("which is 5 more than 20", Explain(m, 25));
2259}
2260
shiqiane35fdd92008-12-10 05:08:54 +00002261// The following predicate function and predicate functor are for
2262// testing the Truly(predicate) matcher.
2263
2264// Returns non-zero if the input is positive. Note that the return
2265// type of this function is not bool. It's OK as Truly() accepts any
2266// unary function or functor whose return type can be implicitly
2267// converted to bool.
2268int IsPositive(double x) {
2269 return x > 0 ? 1 : 0;
2270}
2271
2272// This functor returns true if the input is greater than the given
2273// number.
2274class IsGreaterThan {
2275 public:
2276 explicit IsGreaterThan(int threshold) : threshold_(threshold) {}
2277
2278 bool operator()(int n) const { return n > threshold_; }
zhanyong.wan32de5f52009-12-23 00:13:23 +00002279
shiqiane35fdd92008-12-10 05:08:54 +00002280 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002281 int threshold_;
shiqiane35fdd92008-12-10 05:08:54 +00002282};
2283
2284// For testing Truly().
2285const int foo = 0;
2286
2287// This predicate returns true iff the argument references foo and has
2288// a zero value.
2289bool ReferencesFooAndIsZero(const int& n) {
2290 return (&n == &foo) && (n == 0);
2291}
2292
2293// Tests that Truly(predicate) matches what satisfies the given
2294// predicate.
2295TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {
2296 Matcher<double> m = Truly(IsPositive);
2297 EXPECT_TRUE(m.Matches(2.0));
2298 EXPECT_FALSE(m.Matches(-1.5));
2299}
2300
2301// Tests that Truly(predicate_functor) works too.
2302TEST(TrulyTest, CanBeUsedWithFunctor) {
2303 Matcher<int> m = Truly(IsGreaterThan(5));
2304 EXPECT_TRUE(m.Matches(6));
2305 EXPECT_FALSE(m.Matches(4));
2306}
2307
zhanyong.wan8d3dc0c2011-04-14 19:37:06 +00002308// A class that can be implicitly converted to bool.
2309class ConvertibleToBool {
2310 public:
2311 explicit ConvertibleToBool(int number) : number_(number) {}
2312 operator bool() const { return number_ != 0; }
2313
2314 private:
2315 int number_;
2316};
2317
2318ConvertibleToBool IsNotZero(int number) {
2319 return ConvertibleToBool(number);
2320}
2321
2322// Tests that the predicate used in Truly() may return a class that's
2323// implicitly convertible to bool, even when the class has no
2324// operator!().
2325TEST(TrulyTest, PredicateCanReturnAClassConvertibleToBool) {
2326 Matcher<int> m = Truly(IsNotZero);
2327 EXPECT_TRUE(m.Matches(1));
2328 EXPECT_FALSE(m.Matches(0));
2329}
2330
shiqiane35fdd92008-12-10 05:08:54 +00002331// Tests that Truly(predicate) can describe itself properly.
2332TEST(TrulyTest, CanDescribeSelf) {
2333 Matcher<double> m = Truly(IsPositive);
2334 EXPECT_EQ("satisfies the given predicate",
2335 Describe(m));
2336}
2337
2338// Tests that Truly(predicate) works when the matcher takes its
2339// argument by reference.
2340TEST(TrulyTest, WorksForByRefArguments) {
2341 Matcher<const int&> m = Truly(ReferencesFooAndIsZero);
2342 EXPECT_TRUE(m.Matches(foo));
2343 int n = 0;
2344 EXPECT_FALSE(m.Matches(n));
2345}
2346
2347// Tests that Matches(m) is a predicate satisfied by whatever that
2348// matches matcher m.
2349TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {
2350 EXPECT_TRUE(Matches(Ge(0))(1));
2351 EXPECT_FALSE(Matches(Eq('a'))('b'));
2352}
2353
2354// Tests that Matches(m) works when the matcher takes its argument by
2355// reference.
2356TEST(MatchesTest, WorksOnByRefArguments) {
2357 int m = 0, n = 0;
2358 EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n));
2359 EXPECT_FALSE(Matches(Ref(m))(n));
2360}
2361
2362// Tests that a Matcher on non-reference type can be used in
2363// Matches().
2364TEST(MatchesTest, WorksWithMatcherOnNonRefType) {
2365 Matcher<int> eq5 = Eq(5);
2366 EXPECT_TRUE(Matches(eq5)(5));
2367 EXPECT_FALSE(Matches(eq5)(2));
2368}
2369
zhanyong.wanb8243162009-06-04 05:48:20 +00002370// Tests Value(value, matcher). Since Value() is a simple wrapper for
2371// Matches(), which has been tested already, we don't spend a lot of
2372// effort on testing Value().
2373TEST(ValueTest, WorksWithPolymorphicMatcher) {
2374 EXPECT_TRUE(Value("hi", StartsWith("h")));
2375 EXPECT_FALSE(Value(5, Gt(10)));
2376}
2377
2378TEST(ValueTest, WorksWithMonomorphicMatcher) {
2379 const Matcher<int> is_zero = Eq(0);
2380 EXPECT_TRUE(Value(0, is_zero));
2381 EXPECT_FALSE(Value('a', is_zero));
2382
2383 int n = 0;
2384 const Matcher<const int&> ref_n = Ref(n);
2385 EXPECT_TRUE(Value(n, ref_n));
2386 EXPECT_FALSE(Value(1, ref_n));
2387}
2388
zhanyong.wana862f1d2010-03-15 21:23:04 +00002389TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) {
zhanyong.wan34b034c2010-03-05 21:23:23 +00002390 StringMatchResultListener listener1;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002391 EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002392 EXPECT_EQ("% 2 == 0", listener1.str());
2393
2394 StringMatchResultListener listener2;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002395 EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002396 EXPECT_EQ("", listener2.str());
2397}
2398
zhanyong.wana862f1d2010-03-15 21:23:04 +00002399TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) {
zhanyong.wan34b034c2010-03-05 21:23:23 +00002400 const Matcher<int> is_even = PolymorphicIsEven();
2401 StringMatchResultListener listener1;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002402 EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002403 EXPECT_EQ("% 2 == 0", listener1.str());
2404
2405 const Matcher<const double&> is_zero = Eq(0);
2406 StringMatchResultListener listener2;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002407 EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002408 EXPECT_EQ("", listener2.str());
2409}
2410
zhanyong.wana862f1d2010-03-15 21:23:04 +00002411MATCHER_P(Really, inner_matcher, "") {
2412 return ExplainMatchResult(inner_matcher, arg, result_listener);
2413}
2414
2415TEST(ExplainMatchResultTest, WorksInsideMATCHER) {
2416 EXPECT_THAT(0, Really(Eq(0)));
2417}
2418
zhanyong.wanbf550852009-06-09 06:09:53 +00002419TEST(AllArgsTest, WorksForTuple) {
2420 EXPECT_THAT(make_tuple(1, 2L), AllArgs(Lt()));
2421 EXPECT_THAT(make_tuple(2L, 1), Not(AllArgs(Lt())));
2422}
2423
2424TEST(AllArgsTest, WorksForNonTuple) {
2425 EXPECT_THAT(42, AllArgs(Gt(0)));
2426 EXPECT_THAT('a', Not(AllArgs(Eq('b'))));
2427}
2428
2429class AllArgsHelper {
2430 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002431 AllArgsHelper() {}
2432
zhanyong.wanbf550852009-06-09 06:09:53 +00002433 MOCK_METHOD2(Helper, int(char x, int y));
zhanyong.wan32de5f52009-12-23 00:13:23 +00002434
2435 private:
2436 GTEST_DISALLOW_COPY_AND_ASSIGN_(AllArgsHelper);
zhanyong.wanbf550852009-06-09 06:09:53 +00002437};
2438
2439TEST(AllArgsTest, WorksInWithClause) {
2440 AllArgsHelper helper;
2441 ON_CALL(helper, Helper(_, _))
2442 .With(AllArgs(Lt()))
2443 .WillByDefault(Return(1));
2444 EXPECT_CALL(helper, Helper(_, _));
2445 EXPECT_CALL(helper, Helper(_, _))
2446 .With(AllArgs(Gt()))
2447 .WillOnce(Return(2));
2448
2449 EXPECT_EQ(1, helper.Helper('\1', 2));
2450 EXPECT_EQ(2, helper.Helper('a', 1));
2451}
2452
shiqiane35fdd92008-12-10 05:08:54 +00002453// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2454// matches the matcher.
2455TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {
2456 ASSERT_THAT(5, Ge(2)) << "This should succeed.";
2457 ASSERT_THAT("Foo", EndsWith("oo"));
2458 EXPECT_THAT(2, AllOf(Le(7), Ge(0))) << "This should succeed too.";
2459 EXPECT_THAT("Hello", StartsWith("Hell"));
2460}
2461
2462// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2463// doesn't match the matcher.
2464TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {
2465 // 'n' must be static as it is used in an EXPECT_FATAL_FAILURE(),
2466 // which cannot reference auto variables.
zhanyong.wan736baa82010-09-27 17:44:16 +00002467 static unsigned short n; // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00002468 n = 5;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002469
2470 // VC++ prior to version 8.0 SP1 has a bug where it will not see any
2471 // functions declared in the namespace scope from within nested classes.
2472 // EXPECT/ASSERT_(NON)FATAL_FAILURE macros use nested classes so that all
2473 // namespace-level functions invoked inside them need to be explicitly
2474 // resolved.
2475 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Gt(10)),
shiqiane35fdd92008-12-10 05:08:54 +00002476 "Value of: n\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002477 "Expected: is > 10\n"
zhanyong.wan736baa82010-09-27 17:44:16 +00002478 " Actual: 5" + OfType("unsigned short"));
shiqiane35fdd92008-12-10 05:08:54 +00002479 n = 0;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002480 EXPECT_NONFATAL_FAILURE(
2481 EXPECT_THAT(n, ::testing::AllOf(::testing::Le(7), ::testing::Ge(5))),
2482 "Value of: n\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002483 "Expected: (is <= 7) and (is >= 5)\n"
zhanyong.wan736baa82010-09-27 17:44:16 +00002484 " Actual: 0" + OfType("unsigned short"));
shiqiane35fdd92008-12-10 05:08:54 +00002485}
2486
2487// Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument
2488// has a reference type.
2489TEST(MatcherAssertionTest, WorksForByRefArguments) {
2490 // We use a static variable here as EXPECT_FATAL_FAILURE() cannot
2491 // reference auto variables.
2492 static int n;
2493 n = 0;
2494 EXPECT_THAT(n, AllOf(Le(7), Ref(n)));
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002495 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
shiqiane35fdd92008-12-10 05:08:54 +00002496 "Value of: n\n"
2497 "Expected: does not reference the variable @");
2498 // Tests the "Actual" part.
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002499 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
zhanyong.wan736baa82010-09-27 17:44:16 +00002500 "Actual: 0" + OfType("int") + ", which is located @");
shiqiane35fdd92008-12-10 05:08:54 +00002501}
2502
zhanyong.wan95b12332009-09-25 18:55:50 +00002503#if !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002504// Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is
2505// monomorphic.
zhanyong.wan95b12332009-09-25 18:55:50 +00002506
2507// ASSERT_THAT("hello", starts_with_he) fails to compile with Nokia's
2508// Symbian compiler: it tries to compile
2509// template<T, U> class MatcherCastImpl { ...
zhanyong.wandb22c222010-01-28 21:52:29 +00002510// virtual bool MatchAndExplain(T x, ...) const {
2511// return source_matcher_.MatchAndExplain(static_cast<U>(x), ...);
zhanyong.wan95b12332009-09-25 18:55:50 +00002512// with U == string and T == const char*
2513// With ASSERT_THAT("hello"...) changed to ASSERT_THAT(string("hello") ... )
2514// the compiler silently crashes with no output.
2515// If MatcherCastImpl is changed to use U(x) instead of static_cast<U>(x)
2516// the code compiles but the converted string is bogus.
shiqiane35fdd92008-12-10 05:08:54 +00002517TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
2518 Matcher<const char*> starts_with_he = StartsWith("he");
2519 ASSERT_THAT("hello", starts_with_he);
2520
2521 Matcher<const string&> ends_with_ok = EndsWith("ok");
2522 ASSERT_THAT("book", ends_with_ok);
zhanyong.wan736baa82010-09-27 17:44:16 +00002523 const string bad = "bad";
2524 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(bad, ends_with_ok),
2525 "Value of: bad\n"
2526 "Expected: ends with \"ok\"\n"
2527 " Actual: \"bad\"");
shiqiane35fdd92008-12-10 05:08:54 +00002528 Matcher<int> is_greater_than_5 = Gt(5);
2529 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5),
2530 "Value of: 5\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002531 "Expected: is > 5\n"
zhanyong.wan736baa82010-09-27 17:44:16 +00002532 " Actual: 5" + OfType("int"));
shiqiane35fdd92008-12-10 05:08:54 +00002533}
zhanyong.wan95b12332009-09-25 18:55:50 +00002534#endif // !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002535
2536// Tests floating-point matchers.
2537template <typename RawType>
2538class FloatingPointTest : public testing::Test {
2539 protected:
2540 typedef typename testing::internal::FloatingPoint<RawType> Floating;
2541 typedef typename Floating::Bits Bits;
2542
2543 virtual void SetUp() {
2544 const size_t max_ulps = Floating::kMaxUlps;
2545
2546 // The bits that represent 0.0.
2547 const Bits zero_bits = Floating(0).bits();
2548
2549 // Makes some numbers close to 0.0.
2550 close_to_positive_zero_ = Floating::ReinterpretBits(zero_bits + max_ulps/2);
2551 close_to_negative_zero_ = -Floating::ReinterpretBits(
2552 zero_bits + max_ulps - max_ulps/2);
2553 further_from_negative_zero_ = -Floating::ReinterpretBits(
2554 zero_bits + max_ulps + 1 - max_ulps/2);
2555
2556 // The bits that represent 1.0.
2557 const Bits one_bits = Floating(1).bits();
2558
2559 // Makes some numbers close to 1.0.
2560 close_to_one_ = Floating::ReinterpretBits(one_bits + max_ulps);
2561 further_from_one_ = Floating::ReinterpretBits(one_bits + max_ulps + 1);
2562
2563 // +infinity.
2564 infinity_ = Floating::Infinity();
2565
2566 // The bits that represent +infinity.
2567 const Bits infinity_bits = Floating(infinity_).bits();
2568
2569 // Makes some numbers close to infinity.
2570 close_to_infinity_ = Floating::ReinterpretBits(infinity_bits - max_ulps);
2571 further_from_infinity_ = Floating::ReinterpretBits(
2572 infinity_bits - max_ulps - 1);
2573
2574 // Makes some NAN's.
2575 nan1_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 1);
2576 nan2_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 200);
2577 }
2578
2579 void TestSize() {
2580 EXPECT_EQ(sizeof(RawType), sizeof(Bits));
2581 }
2582
2583 // A battery of tests for FloatingEqMatcher::Matches.
2584 // matcher_maker is a pointer to a function which creates a FloatingEqMatcher.
2585 void TestMatches(
2586 testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) {
2587 Matcher<RawType> m1 = matcher_maker(0.0);
2588 EXPECT_TRUE(m1.Matches(-0.0));
2589 EXPECT_TRUE(m1.Matches(close_to_positive_zero_));
2590 EXPECT_TRUE(m1.Matches(close_to_negative_zero_));
2591 EXPECT_FALSE(m1.Matches(1.0));
2592
2593 Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_);
2594 EXPECT_FALSE(m2.Matches(further_from_negative_zero_));
2595
2596 Matcher<RawType> m3 = matcher_maker(1.0);
2597 EXPECT_TRUE(m3.Matches(close_to_one_));
2598 EXPECT_FALSE(m3.Matches(further_from_one_));
2599
2600 // Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above.
2601 EXPECT_FALSE(m3.Matches(0.0));
2602
2603 Matcher<RawType> m4 = matcher_maker(-infinity_);
2604 EXPECT_TRUE(m4.Matches(-close_to_infinity_));
2605
2606 Matcher<RawType> m5 = matcher_maker(infinity_);
2607 EXPECT_TRUE(m5.Matches(close_to_infinity_));
2608
2609 // This is interesting as the representations of infinity_ and nan1_
2610 // are only 1 DLP apart.
2611 EXPECT_FALSE(m5.Matches(nan1_));
2612
2613 // matcher_maker can produce a Matcher<const RawType&>, which is needed in
2614 // some cases.
2615 Matcher<const RawType&> m6 = matcher_maker(0.0);
2616 EXPECT_TRUE(m6.Matches(-0.0));
2617 EXPECT_TRUE(m6.Matches(close_to_positive_zero_));
2618 EXPECT_FALSE(m6.Matches(1.0));
2619
2620 // matcher_maker can produce a Matcher<RawType&>, which is needed in some
2621 // cases.
2622 Matcher<RawType&> m7 = matcher_maker(0.0);
2623 RawType x = 0.0;
2624 EXPECT_TRUE(m7.Matches(x));
2625 x = 0.01f;
2626 EXPECT_FALSE(m7.Matches(x));
2627 }
2628
2629 // Pre-calculated numbers to be used by the tests.
2630
2631 static RawType close_to_positive_zero_;
2632 static RawType close_to_negative_zero_;
2633 static RawType further_from_negative_zero_;
2634
2635 static RawType close_to_one_;
2636 static RawType further_from_one_;
2637
2638 static RawType infinity_;
2639 static RawType close_to_infinity_;
2640 static RawType further_from_infinity_;
2641
2642 static RawType nan1_;
2643 static RawType nan2_;
2644};
2645
2646template <typename RawType>
2647RawType FloatingPointTest<RawType>::close_to_positive_zero_;
2648
2649template <typename RawType>
2650RawType FloatingPointTest<RawType>::close_to_negative_zero_;
2651
2652template <typename RawType>
2653RawType FloatingPointTest<RawType>::further_from_negative_zero_;
2654
2655template <typename RawType>
2656RawType FloatingPointTest<RawType>::close_to_one_;
2657
2658template <typename RawType>
2659RawType FloatingPointTest<RawType>::further_from_one_;
2660
2661template <typename RawType>
2662RawType FloatingPointTest<RawType>::infinity_;
2663
2664template <typename RawType>
2665RawType FloatingPointTest<RawType>::close_to_infinity_;
2666
2667template <typename RawType>
2668RawType FloatingPointTest<RawType>::further_from_infinity_;
2669
2670template <typename RawType>
2671RawType FloatingPointTest<RawType>::nan1_;
2672
2673template <typename RawType>
2674RawType FloatingPointTest<RawType>::nan2_;
2675
2676// Instantiate FloatingPointTest for testing floats.
2677typedef FloatingPointTest<float> FloatTest;
2678
2679TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) {
2680 TestMatches(&FloatEq);
2681}
2682
2683TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
2684 TestMatches(&NanSensitiveFloatEq);
2685}
2686
2687TEST_F(FloatTest, FloatEqCannotMatchNaN) {
2688 // FloatEq never matches NaN.
2689 Matcher<float> m = FloatEq(nan1_);
2690 EXPECT_FALSE(m.Matches(nan1_));
2691 EXPECT_FALSE(m.Matches(nan2_));
2692 EXPECT_FALSE(m.Matches(1.0));
2693}
2694
2695TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {
2696 // NanSensitiveFloatEq will match NaN.
2697 Matcher<float> m = NanSensitiveFloatEq(nan1_);
2698 EXPECT_TRUE(m.Matches(nan1_));
2699 EXPECT_TRUE(m.Matches(nan2_));
2700 EXPECT_FALSE(m.Matches(1.0));
2701}
2702
2703TEST_F(FloatTest, FloatEqCanDescribeSelf) {
2704 Matcher<float> m1 = FloatEq(2.0f);
2705 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002706 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002707
2708 Matcher<float> m2 = FloatEq(0.5f);
2709 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002710 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002711
2712 Matcher<float> m3 = FloatEq(nan1_);
2713 EXPECT_EQ("never matches", Describe(m3));
2714 EXPECT_EQ("is anything", DescribeNegation(m3));
2715}
2716
2717TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {
2718 Matcher<float> m1 = NanSensitiveFloatEq(2.0f);
2719 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002720 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002721
2722 Matcher<float> m2 = NanSensitiveFloatEq(0.5f);
2723 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002724 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002725
2726 Matcher<float> m3 = NanSensitiveFloatEq(nan1_);
2727 EXPECT_EQ("is NaN", Describe(m3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002728 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
shiqiane35fdd92008-12-10 05:08:54 +00002729}
2730
2731// Instantiate FloatingPointTest for testing doubles.
2732typedef FloatingPointTest<double> DoubleTest;
2733
2734TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
2735 TestMatches(&DoubleEq);
2736}
2737
2738TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
2739 TestMatches(&NanSensitiveDoubleEq);
2740}
2741
2742TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {
2743 // DoubleEq never matches NaN.
2744 Matcher<double> m = DoubleEq(nan1_);
2745 EXPECT_FALSE(m.Matches(nan1_));
2746 EXPECT_FALSE(m.Matches(nan2_));
2747 EXPECT_FALSE(m.Matches(1.0));
2748}
2749
2750TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {
2751 // NanSensitiveDoubleEq will match NaN.
2752 Matcher<double> m = NanSensitiveDoubleEq(nan1_);
2753 EXPECT_TRUE(m.Matches(nan1_));
2754 EXPECT_TRUE(m.Matches(nan2_));
2755 EXPECT_FALSE(m.Matches(1.0));
2756}
2757
2758TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {
2759 Matcher<double> m1 = DoubleEq(2.0);
2760 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002761 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002762
2763 Matcher<double> m2 = DoubleEq(0.5);
2764 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002765 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002766
2767 Matcher<double> m3 = DoubleEq(nan1_);
2768 EXPECT_EQ("never matches", Describe(m3));
2769 EXPECT_EQ("is anything", DescribeNegation(m3));
2770}
2771
2772TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {
2773 Matcher<double> m1 = NanSensitiveDoubleEq(2.0);
2774 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002775 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002776
2777 Matcher<double> m2 = NanSensitiveDoubleEq(0.5);
2778 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002779 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002780
2781 Matcher<double> m3 = NanSensitiveDoubleEq(nan1_);
2782 EXPECT_EQ("is NaN", Describe(m3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002783 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
shiqiane35fdd92008-12-10 05:08:54 +00002784}
2785
2786TEST(PointeeTest, RawPointer) {
2787 const Matcher<int*> m = Pointee(Ge(0));
2788
2789 int n = 1;
2790 EXPECT_TRUE(m.Matches(&n));
2791 n = -1;
2792 EXPECT_FALSE(m.Matches(&n));
2793 EXPECT_FALSE(m.Matches(NULL));
2794}
2795
2796TEST(PointeeTest, RawPointerToConst) {
2797 const Matcher<const double*> m = Pointee(Ge(0));
2798
2799 double x = 1;
2800 EXPECT_TRUE(m.Matches(&x));
2801 x = -1;
2802 EXPECT_FALSE(m.Matches(&x));
2803 EXPECT_FALSE(m.Matches(NULL));
2804}
2805
2806TEST(PointeeTest, ReferenceToConstRawPointer) {
2807 const Matcher<int* const &> m = Pointee(Ge(0));
2808
2809 int n = 1;
2810 EXPECT_TRUE(m.Matches(&n));
2811 n = -1;
2812 EXPECT_FALSE(m.Matches(&n));
2813 EXPECT_FALSE(m.Matches(NULL));
2814}
2815
2816TEST(PointeeTest, ReferenceToNonConstRawPointer) {
2817 const Matcher<double* &> m = Pointee(Ge(0));
2818
2819 double x = 1.0;
2820 double* p = &x;
2821 EXPECT_TRUE(m.Matches(p));
2822 x = -1;
2823 EXPECT_FALSE(m.Matches(p));
2824 p = NULL;
2825 EXPECT_FALSE(m.Matches(p));
2826}
2827
vladlosevada23472012-08-14 15:38:49 +00002828// Minimal const-propagating pointer.
2829template <typename T>
2830class ConstPropagatingPtr {
2831 public:
2832 typedef T element_type;
2833
2834 ConstPropagatingPtr() : val_() {}
2835 explicit ConstPropagatingPtr(T* t) : val_(t) {}
2836 ConstPropagatingPtr(const ConstPropagatingPtr& other) : val_(other.val_) {}
2837
2838 T* get() { return val_; }
2839 T& operator*() { return *val_; }
2840 // Most smart pointers return non-const T* and T& from the next methods.
2841 const T* get() const { return val_; }
2842 const T& operator*() const { return *val_; }
2843
2844 private:
2845 T* val_;
2846};
2847
2848TEST(PointeeTest, WorksWithConstPropagatingPointers) {
2849 const Matcher< ConstPropagatingPtr<int> > m = Pointee(Lt(5));
2850 int three = 3;
2851 const ConstPropagatingPtr<int> co(&three);
2852 ConstPropagatingPtr<int> o(&three);
2853 EXPECT_TRUE(m.Matches(o));
2854 EXPECT_TRUE(m.Matches(co));
2855 *o = 6;
2856 EXPECT_FALSE(m.Matches(o));
2857 EXPECT_FALSE(m.Matches(ConstPropagatingPtr<int>()));
2858}
2859
shiqiane35fdd92008-12-10 05:08:54 +00002860TEST(PointeeTest, NeverMatchesNull) {
2861 const Matcher<const char*> m = Pointee(_);
2862 EXPECT_FALSE(m.Matches(NULL));
2863}
2864
2865// Tests that we can write Pointee(value) instead of Pointee(Eq(value)).
2866TEST(PointeeTest, MatchesAgainstAValue) {
2867 const Matcher<int*> m = Pointee(5);
2868
2869 int n = 5;
2870 EXPECT_TRUE(m.Matches(&n));
2871 n = -1;
2872 EXPECT_FALSE(m.Matches(&n));
2873 EXPECT_FALSE(m.Matches(NULL));
2874}
2875
2876TEST(PointeeTest, CanDescribeSelf) {
2877 const Matcher<int*> m = Pointee(Gt(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002878 EXPECT_EQ("points to a value that is > 3", Describe(m));
2879 EXPECT_EQ("does not point to a value that is > 3",
shiqiane35fdd92008-12-10 05:08:54 +00002880 DescribeNegation(m));
2881}
2882
shiqiane35fdd92008-12-10 05:08:54 +00002883TEST(PointeeTest, CanExplainMatchResult) {
2884 const Matcher<const string*> m = Pointee(StartsWith("Hi"));
2885
2886 EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL)));
2887
zhanyong.wan736baa82010-09-27 17:44:16 +00002888 const Matcher<long*> m2 = Pointee(GreaterThan(1)); // NOLINT
2889 long n = 3; // NOLINT
2890 EXPECT_EQ("which points to 3" + OfType("long") + ", which is 2 more than 1",
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002891 Explain(m2, &n));
2892}
2893
2894TEST(PointeeTest, AlwaysExplainsPointee) {
2895 const Matcher<int*> m = Pointee(0);
2896 int n = 42;
zhanyong.wan736baa82010-09-27 17:44:16 +00002897 EXPECT_EQ("which points to 42" + OfType("int"), Explain(m, &n));
shiqiane35fdd92008-12-10 05:08:54 +00002898}
2899
2900// An uncopyable class.
2901class Uncopyable {
2902 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002903 explicit Uncopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00002904
2905 int value() const { return value_; }
2906 private:
2907 const int value_;
2908 GTEST_DISALLOW_COPY_AND_ASSIGN_(Uncopyable);
2909};
2910
2911// Returns true iff x.value() is positive.
2912bool ValueIsPositive(const Uncopyable& x) { return x.value() > 0; }
2913
2914// A user-defined struct for testing Field().
2915struct AStruct {
2916 AStruct() : x(0), y(1.0), z(5), p(NULL) {}
2917 AStruct(const AStruct& rhs)
2918 : x(rhs.x), y(rhs.y), z(rhs.z.value()), p(rhs.p) {}
2919
2920 int x; // A non-const field.
2921 const double y; // A const field.
2922 Uncopyable z; // An uncopyable field.
2923 const char* p; // A pointer field.
zhanyong.wan32de5f52009-12-23 00:13:23 +00002924
2925 private:
2926 GTEST_DISALLOW_ASSIGN_(AStruct);
shiqiane35fdd92008-12-10 05:08:54 +00002927};
2928
2929// A derived struct for testing Field().
2930struct DerivedStruct : public AStruct {
2931 char ch;
zhanyong.wan32de5f52009-12-23 00:13:23 +00002932
2933 private:
2934 GTEST_DISALLOW_ASSIGN_(DerivedStruct);
shiqiane35fdd92008-12-10 05:08:54 +00002935};
2936
2937// Tests that Field(&Foo::field, ...) works when field is non-const.
2938TEST(FieldTest, WorksForNonConstField) {
2939 Matcher<AStruct> m = Field(&AStruct::x, Ge(0));
2940
2941 AStruct a;
2942 EXPECT_TRUE(m.Matches(a));
2943 a.x = -1;
2944 EXPECT_FALSE(m.Matches(a));
2945}
2946
2947// Tests that Field(&Foo::field, ...) works when field is const.
2948TEST(FieldTest, WorksForConstField) {
2949 AStruct a;
2950
2951 Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0));
2952 EXPECT_TRUE(m.Matches(a));
2953 m = Field(&AStruct::y, Le(0.0));
2954 EXPECT_FALSE(m.Matches(a));
2955}
2956
2957// Tests that Field(&Foo::field, ...) works when field is not copyable.
2958TEST(FieldTest, WorksForUncopyableField) {
2959 AStruct a;
2960
2961 Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive));
2962 EXPECT_TRUE(m.Matches(a));
2963 m = Field(&AStruct::z, Not(Truly(ValueIsPositive)));
2964 EXPECT_FALSE(m.Matches(a));
2965}
2966
2967// Tests that Field(&Foo::field, ...) works when field is a pointer.
2968TEST(FieldTest, WorksForPointerField) {
2969 // Matching against NULL.
2970 Matcher<AStruct> m = Field(&AStruct::p, static_cast<const char*>(NULL));
2971 AStruct a;
2972 EXPECT_TRUE(m.Matches(a));
2973 a.p = "hi";
2974 EXPECT_FALSE(m.Matches(a));
2975
2976 // Matching a pointer that is not NULL.
2977 m = Field(&AStruct::p, StartsWith("hi"));
2978 a.p = "hill";
2979 EXPECT_TRUE(m.Matches(a));
2980 a.p = "hole";
2981 EXPECT_FALSE(m.Matches(a));
2982}
2983
2984// Tests that Field() works when the object is passed by reference.
2985TEST(FieldTest, WorksForByRefArgument) {
2986 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2987
2988 AStruct a;
2989 EXPECT_TRUE(m.Matches(a));
2990 a.x = -1;
2991 EXPECT_FALSE(m.Matches(a));
2992}
2993
2994// Tests that Field(&Foo::field, ...) works when the argument's type
2995// is a sub-type of Foo.
2996TEST(FieldTest, WorksForArgumentOfSubType) {
2997 // Note that the matcher expects DerivedStruct but we say AStruct
2998 // inside Field().
2999 Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0));
3000
3001 DerivedStruct d;
3002 EXPECT_TRUE(m.Matches(d));
3003 d.x = -1;
3004 EXPECT_FALSE(m.Matches(d));
3005}
3006
3007// Tests that Field(&Foo::field, m) works when field's type and m's
3008// argument type are compatible but not the same.
3009TEST(FieldTest, WorksForCompatibleMatcherType) {
3010 // The field is an int, but the inner matcher expects a signed char.
3011 Matcher<const AStruct&> m = Field(&AStruct::x,
3012 Matcher<signed char>(Ge(0)));
3013
3014 AStruct a;
3015 EXPECT_TRUE(m.Matches(a));
3016 a.x = -1;
3017 EXPECT_FALSE(m.Matches(a));
3018}
3019
3020// Tests that Field() can describe itself.
3021TEST(FieldTest, CanDescribeSelf) {
3022 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
3023
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003024 EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
3025 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003026}
3027
3028// Tests that Field() can explain the match result.
3029TEST(FieldTest, CanExplainMatchResult) {
3030 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
3031
3032 AStruct a;
3033 a.x = 1;
zhanyong.wan736baa82010-09-27 17:44:16 +00003034 EXPECT_EQ("whose given field is 1" + OfType("int"), Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003035
3036 m = Field(&AStruct::x, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003037 EXPECT_EQ(
3038 "whose given field is 1" + OfType("int") + ", which is 1 more than 0",
3039 Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003040}
3041
3042// Tests that Field() works when the argument is a pointer to const.
3043TEST(FieldForPointerTest, WorksForPointerToConst) {
3044 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
3045
3046 AStruct a;
3047 EXPECT_TRUE(m.Matches(&a));
3048 a.x = -1;
3049 EXPECT_FALSE(m.Matches(&a));
3050}
3051
3052// Tests that Field() works when the argument is a pointer to non-const.
3053TEST(FieldForPointerTest, WorksForPointerToNonConst) {
3054 Matcher<AStruct*> m = Field(&AStruct::x, Ge(0));
3055
3056 AStruct a;
3057 EXPECT_TRUE(m.Matches(&a));
3058 a.x = -1;
3059 EXPECT_FALSE(m.Matches(&a));
3060}
3061
zhanyong.wan6953a722010-01-13 05:15:07 +00003062// Tests that Field() works when the argument is a reference to a const pointer.
3063TEST(FieldForPointerTest, WorksForReferenceToConstPointer) {
3064 Matcher<AStruct* const&> m = Field(&AStruct::x, Ge(0));
3065
3066 AStruct a;
3067 EXPECT_TRUE(m.Matches(&a));
3068 a.x = -1;
3069 EXPECT_FALSE(m.Matches(&a));
3070}
3071
shiqiane35fdd92008-12-10 05:08:54 +00003072// Tests that Field() does not match the NULL pointer.
3073TEST(FieldForPointerTest, DoesNotMatchNull) {
3074 Matcher<const AStruct*> m = Field(&AStruct::x, _);
3075 EXPECT_FALSE(m.Matches(NULL));
3076}
3077
3078// Tests that Field(&Foo::field, ...) works when the argument's type
3079// is a sub-type of const Foo*.
3080TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
3081 // Note that the matcher expects DerivedStruct but we say AStruct
3082 // inside Field().
3083 Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0));
3084
3085 DerivedStruct d;
3086 EXPECT_TRUE(m.Matches(&d));
3087 d.x = -1;
3088 EXPECT_FALSE(m.Matches(&d));
3089}
3090
3091// Tests that Field() can describe itself when used to match a pointer.
3092TEST(FieldForPointerTest, CanDescribeSelf) {
3093 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
3094
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003095 EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
3096 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003097}
3098
3099// Tests that Field() can explain the result of matching a pointer.
3100TEST(FieldForPointerTest, CanExplainMatchResult) {
3101 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
3102
3103 AStruct a;
3104 a.x = 1;
3105 EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(NULL)));
zhanyong.wan736baa82010-09-27 17:44:16 +00003106 EXPECT_EQ("which points to an object whose given field is 1" + OfType("int"),
3107 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003108
3109 m = Field(&AStruct::x, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003110 EXPECT_EQ("which points to an object whose given field is 1" + OfType("int") +
3111 ", which is 1 more than 0", Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003112}
3113
3114// A user-defined class for testing Property().
3115class AClass {
3116 public:
3117 AClass() : n_(0) {}
3118
3119 // A getter that returns a non-reference.
3120 int n() const { return n_; }
3121
3122 void set_n(int new_n) { n_ = new_n; }
3123
3124 // A getter that returns a reference to const.
3125 const string& s() const { return s_; }
3126
3127 void set_s(const string& new_s) { s_ = new_s; }
3128
3129 // A getter that returns a reference to non-const.
3130 double& x() const { return x_; }
3131 private:
3132 int n_;
3133 string s_;
3134
3135 static double x_;
3136};
3137
3138double AClass::x_ = 0.0;
3139
3140// A derived class for testing Property().
3141class DerivedClass : public AClass {
3142 private:
3143 int k_;
3144};
3145
3146// Tests that Property(&Foo::property, ...) works when property()
3147// returns a non-reference.
3148TEST(PropertyTest, WorksForNonReferenceProperty) {
3149 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3150
3151 AClass a;
3152 a.set_n(1);
3153 EXPECT_TRUE(m.Matches(a));
3154
3155 a.set_n(-1);
3156 EXPECT_FALSE(m.Matches(a));
3157}
3158
3159// Tests that Property(&Foo::property, ...) works when property()
3160// returns a reference to const.
3161TEST(PropertyTest, WorksForReferenceToConstProperty) {
3162 Matcher<const AClass&> m = Property(&AClass::s, StartsWith("hi"));
3163
3164 AClass a;
3165 a.set_s("hill");
3166 EXPECT_TRUE(m.Matches(a));
3167
3168 a.set_s("hole");
3169 EXPECT_FALSE(m.Matches(a));
3170}
3171
3172// Tests that Property(&Foo::property, ...) works when property()
3173// returns a reference to non-const.
3174TEST(PropertyTest, WorksForReferenceToNonConstProperty) {
3175 double x = 0.0;
3176 AClass a;
3177
3178 Matcher<const AClass&> m = Property(&AClass::x, Ref(x));
3179 EXPECT_FALSE(m.Matches(a));
3180
3181 m = Property(&AClass::x, Not(Ref(x)));
3182 EXPECT_TRUE(m.Matches(a));
3183}
3184
3185// Tests that Property(&Foo::property, ...) works when the argument is
3186// passed by value.
3187TEST(PropertyTest, WorksForByValueArgument) {
3188 Matcher<AClass> m = Property(&AClass::s, StartsWith("hi"));
3189
3190 AClass a;
3191 a.set_s("hill");
3192 EXPECT_TRUE(m.Matches(a));
3193
3194 a.set_s("hole");
3195 EXPECT_FALSE(m.Matches(a));
3196}
3197
3198// Tests that Property(&Foo::property, ...) works when the argument's
3199// type is a sub-type of Foo.
3200TEST(PropertyTest, WorksForArgumentOfSubType) {
3201 // The matcher expects a DerivedClass, but inside the Property() we
3202 // say AClass.
3203 Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0));
3204
3205 DerivedClass d;
3206 d.set_n(1);
3207 EXPECT_TRUE(m.Matches(d));
3208
3209 d.set_n(-1);
3210 EXPECT_FALSE(m.Matches(d));
3211}
3212
3213// Tests that Property(&Foo::property, m) works when property()'s type
3214// and m's argument type are compatible but different.
3215TEST(PropertyTest, WorksForCompatibleMatcherType) {
3216 // n() returns an int but the inner matcher expects a signed char.
3217 Matcher<const AClass&> m = Property(&AClass::n,
3218 Matcher<signed char>(Ge(0)));
3219
3220 AClass a;
3221 EXPECT_TRUE(m.Matches(a));
3222 a.set_n(-1);
3223 EXPECT_FALSE(m.Matches(a));
3224}
3225
3226// Tests that Property() can describe itself.
3227TEST(PropertyTest, CanDescribeSelf) {
3228 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3229
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003230 EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
3231 EXPECT_EQ("is an object whose given property isn't >= 0",
3232 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003233}
3234
3235// Tests that Property() can explain the match result.
3236TEST(PropertyTest, CanExplainMatchResult) {
3237 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3238
3239 AClass a;
3240 a.set_n(1);
zhanyong.wan736baa82010-09-27 17:44:16 +00003241 EXPECT_EQ("whose given property is 1" + OfType("int"), Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003242
3243 m = Property(&AClass::n, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003244 EXPECT_EQ(
3245 "whose given property is 1" + OfType("int") + ", which is 1 more than 0",
3246 Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003247}
3248
3249// Tests that Property() works when the argument is a pointer to const.
3250TEST(PropertyForPointerTest, WorksForPointerToConst) {
3251 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3252
3253 AClass a;
3254 a.set_n(1);
3255 EXPECT_TRUE(m.Matches(&a));
3256
3257 a.set_n(-1);
3258 EXPECT_FALSE(m.Matches(&a));
3259}
3260
3261// Tests that Property() works when the argument is a pointer to non-const.
3262TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
3263 Matcher<AClass*> m = Property(&AClass::s, StartsWith("hi"));
3264
3265 AClass a;
3266 a.set_s("hill");
3267 EXPECT_TRUE(m.Matches(&a));
3268
3269 a.set_s("hole");
3270 EXPECT_FALSE(m.Matches(&a));
3271}
3272
zhanyong.wan6953a722010-01-13 05:15:07 +00003273// Tests that Property() works when the argument is a reference to a
3274// const pointer.
3275TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) {
3276 Matcher<AClass* const&> m = Property(&AClass::s, StartsWith("hi"));
3277
3278 AClass a;
3279 a.set_s("hill");
3280 EXPECT_TRUE(m.Matches(&a));
3281
3282 a.set_s("hole");
3283 EXPECT_FALSE(m.Matches(&a));
3284}
3285
shiqiane35fdd92008-12-10 05:08:54 +00003286// Tests that Property() does not match the NULL pointer.
3287TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
3288 Matcher<const AClass*> m = Property(&AClass::x, _);
3289 EXPECT_FALSE(m.Matches(NULL));
3290}
3291
3292// Tests that Property(&Foo::property, ...) works when the argument's
3293// type is a sub-type of const Foo*.
3294TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
3295 // The matcher expects a DerivedClass, but inside the Property() we
3296 // say AClass.
3297 Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0));
3298
3299 DerivedClass d;
3300 d.set_n(1);
3301 EXPECT_TRUE(m.Matches(&d));
3302
3303 d.set_n(-1);
3304 EXPECT_FALSE(m.Matches(&d));
3305}
3306
3307// Tests that Property() can describe itself when used to match a pointer.
3308TEST(PropertyForPointerTest, CanDescribeSelf) {
3309 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3310
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003311 EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
3312 EXPECT_EQ("is an object whose given property isn't >= 0",
3313 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003314}
3315
3316// Tests that Property() can explain the result of matching a pointer.
3317TEST(PropertyForPointerTest, CanExplainMatchResult) {
3318 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3319
3320 AClass a;
3321 a.set_n(1);
3322 EXPECT_EQ("", Explain(m, static_cast<const AClass*>(NULL)));
zhanyong.wan736baa82010-09-27 17:44:16 +00003323 EXPECT_EQ(
3324 "which points to an object whose given property is 1" + OfType("int"),
3325 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003326
3327 m = Property(&AClass::n, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003328 EXPECT_EQ("which points to an object whose given property is 1" +
3329 OfType("int") + ", which is 1 more than 0",
3330 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003331}
3332
3333// Tests ResultOf.
3334
3335// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3336// function pointer.
3337string IntToStringFunction(int input) { return input == 1 ? "foo" : "bar"; }
3338
3339TEST(ResultOfTest, WorksForFunctionPointers) {
3340 Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(string("foo")));
3341
3342 EXPECT_TRUE(matcher.Matches(1));
3343 EXPECT_FALSE(matcher.Matches(2));
3344}
3345
3346// Tests that ResultOf() can describe itself.
3347TEST(ResultOfTest, CanDescribeItself) {
3348 Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo"));
3349
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003350 EXPECT_EQ("is mapped by the given callable to a value that "
3351 "is equal to \"foo\"", Describe(matcher));
3352 EXPECT_EQ("is mapped by the given callable to a value that "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003353 "isn't equal to \"foo\"", DescribeNegation(matcher));
shiqiane35fdd92008-12-10 05:08:54 +00003354}
3355
3356// Tests that ResultOf() can explain the match result.
3357int IntFunction(int input) { return input == 42 ? 80 : 90; }
3358
3359TEST(ResultOfTest, CanExplainMatchResult) {
3360 Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
zhanyong.wan736baa82010-09-27 17:44:16 +00003361 EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int"),
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003362 Explain(matcher, 36));
shiqiane35fdd92008-12-10 05:08:54 +00003363
3364 matcher = ResultOf(&IntFunction, GreaterThan(85));
zhanyong.wan736baa82010-09-27 17:44:16 +00003365 EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int") +
3366 ", which is 5 more than 85", Explain(matcher, 36));
shiqiane35fdd92008-12-10 05:08:54 +00003367}
3368
3369// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3370// returns a non-reference.
3371TEST(ResultOfTest, WorksForNonReferenceResults) {
3372 Matcher<int> matcher = ResultOf(&IntFunction, Eq(80));
3373
3374 EXPECT_TRUE(matcher.Matches(42));
3375 EXPECT_FALSE(matcher.Matches(36));
3376}
3377
3378// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3379// returns a reference to non-const.
zhanyong.wan736baa82010-09-27 17:44:16 +00003380double& DoubleFunction(double& input) { return input; } // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00003381
zhanyong.wan736baa82010-09-27 17:44:16 +00003382Uncopyable& RefUncopyableFunction(Uncopyable& obj) { // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00003383 return obj;
3384}
3385
3386TEST(ResultOfTest, WorksForReferenceToNonConstResults) {
3387 double x = 3.14;
3388 double x2 = x;
3389 Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x));
3390
3391 EXPECT_TRUE(matcher.Matches(x));
3392 EXPECT_FALSE(matcher.Matches(x2));
3393
3394 // Test that ResultOf works with uncopyable objects
3395 Uncopyable obj(0);
3396 Uncopyable obj2(0);
3397 Matcher<Uncopyable&> matcher2 =
3398 ResultOf(&RefUncopyableFunction, Ref(obj));
3399
3400 EXPECT_TRUE(matcher2.Matches(obj));
3401 EXPECT_FALSE(matcher2.Matches(obj2));
3402}
3403
3404// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3405// returns a reference to const.
3406const string& StringFunction(const string& input) { return input; }
3407
3408TEST(ResultOfTest, WorksForReferenceToConstResults) {
3409 string s = "foo";
3410 string s2 = s;
3411 Matcher<const string&> matcher = ResultOf(&StringFunction, Ref(s));
3412
3413 EXPECT_TRUE(matcher.Matches(s));
3414 EXPECT_FALSE(matcher.Matches(s2));
3415}
3416
3417// Tests that ResultOf(f, m) works when f(x) and m's
3418// argument types are compatible but different.
3419TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {
3420 // IntFunction() returns int but the inner matcher expects a signed char.
3421 Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85)));
3422
3423 EXPECT_TRUE(matcher.Matches(36));
3424 EXPECT_FALSE(matcher.Matches(42));
3425}
3426
shiqiane35fdd92008-12-10 05:08:54 +00003427// Tests that the program aborts when ResultOf is passed
3428// a NULL function pointer.
3429TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +00003430 EXPECT_DEATH_IF_SUPPORTED(
zhanyong.wan736baa82010-09-27 17:44:16 +00003431 ResultOf(static_cast<string(*)(int dummy)>(NULL), Eq(string("foo"))),
shiqiane35fdd92008-12-10 05:08:54 +00003432 "NULL function pointer is passed into ResultOf\\(\\)\\.");
3433}
shiqiane35fdd92008-12-10 05:08:54 +00003434
3435// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3436// function reference.
3437TEST(ResultOfTest, WorksForFunctionReferences) {
3438 Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq("foo"));
3439 EXPECT_TRUE(matcher.Matches(1));
3440 EXPECT_FALSE(matcher.Matches(2));
3441}
3442
3443// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3444// function object.
3445struct Functor : public ::std::unary_function<int, string> {
3446 result_type operator()(argument_type input) const {
3447 return IntToStringFunction(input);
3448 }
3449};
3450
3451TEST(ResultOfTest, WorksForFunctors) {
3452 Matcher<int> matcher = ResultOf(Functor(), Eq(string("foo")));
3453
3454 EXPECT_TRUE(matcher.Matches(1));
3455 EXPECT_FALSE(matcher.Matches(2));
3456}
3457
3458// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3459// functor with more then one operator() defined. ResultOf() must work
3460// for each defined operator().
3461struct PolymorphicFunctor {
3462 typedef int result_type;
3463 int operator()(int n) { return n; }
3464 int operator()(const char* s) { return static_cast<int>(strlen(s)); }
3465};
3466
3467TEST(ResultOfTest, WorksForPolymorphicFunctors) {
3468 Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5));
3469
3470 EXPECT_TRUE(matcher_int.Matches(10));
3471 EXPECT_FALSE(matcher_int.Matches(2));
3472
3473 Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5));
3474
3475 EXPECT_TRUE(matcher_string.Matches("long string"));
3476 EXPECT_FALSE(matcher_string.Matches("shrt"));
3477}
3478
3479const int* ReferencingFunction(const int& n) { return &n; }
3480
3481struct ReferencingFunctor {
3482 typedef const int* result_type;
3483 result_type operator()(const int& n) { return &n; }
3484};
3485
3486TEST(ResultOfTest, WorksForReferencingCallables) {
3487 const int n = 1;
3488 const int n2 = 1;
3489 Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n));
3490 EXPECT_TRUE(matcher2.Matches(n));
3491 EXPECT_FALSE(matcher2.Matches(n2));
3492
3493 Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n));
3494 EXPECT_TRUE(matcher3.Matches(n));
3495 EXPECT_FALSE(matcher3.Matches(n2));
3496}
3497
shiqiane35fdd92008-12-10 05:08:54 +00003498class DivisibleByImpl {
3499 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003500 explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {}
shiqiane35fdd92008-12-10 05:08:54 +00003501
zhanyong.wandb22c222010-01-28 21:52:29 +00003502 // For testing using ExplainMatchResultTo() with polymorphic matchers.
shiqiane35fdd92008-12-10 05:08:54 +00003503 template <typename T>
zhanyong.wandb22c222010-01-28 21:52:29 +00003504 bool MatchAndExplain(const T& n, MatchResultListener* listener) const {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003505 *listener << "which is " << (n % divider_) << " modulo "
zhanyong.wandb22c222010-01-28 21:52:29 +00003506 << divider_;
shiqiane35fdd92008-12-10 05:08:54 +00003507 return (n % divider_) == 0;
3508 }
3509
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003510 void DescribeTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +00003511 *os << "is divisible by " << divider_;
3512 }
3513
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003514 void DescribeNegationTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +00003515 *os << "is not divisible by " << divider_;
3516 }
3517
zhanyong.wan32de5f52009-12-23 00:13:23 +00003518 void set_divider(int a_divider) { divider_ = a_divider; }
shiqiane35fdd92008-12-10 05:08:54 +00003519 int divider() const { return divider_; }
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003520
shiqiane35fdd92008-12-10 05:08:54 +00003521 private:
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003522 int divider_;
shiqiane35fdd92008-12-10 05:08:54 +00003523};
3524
shiqiane35fdd92008-12-10 05:08:54 +00003525PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) {
3526 return MakePolymorphicMatcher(DivisibleByImpl(n));
3527}
3528
3529// Tests that when AllOf() fails, only the first failing matcher is
3530// asked to explain why.
3531TEST(ExplainMatchResultTest, AllOf_False_False) {
3532 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003533 EXPECT_EQ("which is 1 modulo 4", Explain(m, 5));
shiqiane35fdd92008-12-10 05:08:54 +00003534}
3535
3536// Tests that when AllOf() fails, only the first failing matcher is
3537// asked to explain why.
3538TEST(ExplainMatchResultTest, AllOf_False_True) {
3539 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003540 EXPECT_EQ("which is 2 modulo 4", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003541}
3542
3543// Tests that when AllOf() fails, only the first failing matcher is
3544// asked to explain why.
3545TEST(ExplainMatchResultTest, AllOf_True_False) {
3546 const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003547 EXPECT_EQ("which is 2 modulo 3", Explain(m, 5));
shiqiane35fdd92008-12-10 05:08:54 +00003548}
3549
3550// Tests that when AllOf() succeeds, all matchers are asked to explain
3551// why.
3552TEST(ExplainMatchResultTest, AllOf_True_True) {
3553 const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003554 EXPECT_EQ("which is 0 modulo 2, and which is 0 modulo 3", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003555}
3556
3557TEST(ExplainMatchResultTest, AllOf_True_True_2) {
3558 const Matcher<int> m = AllOf(Ge(2), Le(3));
3559 EXPECT_EQ("", Explain(m, 2));
3560}
3561
3562TEST(ExplainmatcherResultTest, MonomorphicMatcher) {
3563 const Matcher<int> m = GreaterThan(5);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003564 EXPECT_EQ("which is 1 more than 5", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003565}
3566
3567// The following two tests verify that values without a public copy
3568// ctor can be used as arguments to matchers like Eq(), Ge(), and etc
3569// with the help of ByRef().
3570
3571class NotCopyable {
3572 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003573 explicit NotCopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00003574
3575 int value() const { return value_; }
3576
3577 bool operator==(const NotCopyable& rhs) const {
3578 return value() == rhs.value();
3579 }
3580
3581 bool operator>=(const NotCopyable& rhs) const {
3582 return value() >= rhs.value();
3583 }
3584 private:
3585 int value_;
3586
3587 GTEST_DISALLOW_COPY_AND_ASSIGN_(NotCopyable);
3588};
3589
3590TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) {
3591 const NotCopyable const_value1(1);
3592 const Matcher<const NotCopyable&> m = Eq(ByRef(const_value1));
3593
3594 const NotCopyable n1(1), n2(2);
3595 EXPECT_TRUE(m.Matches(n1));
3596 EXPECT_FALSE(m.Matches(n2));
3597}
3598
3599TEST(ByRefTest, AllowsNotCopyableValueInMatchers) {
3600 NotCopyable value2(2);
3601 const Matcher<NotCopyable&> m = Ge(ByRef(value2));
3602
3603 NotCopyable n1(1), n2(2);
3604 EXPECT_FALSE(m.Matches(n1));
3605 EXPECT_TRUE(m.Matches(n2));
3606}
3607
zhanyong.wan320814a2013-03-01 00:20:30 +00003608TEST(IsEmptyTest, ImplementsIsEmpty) {
3609 vector<int> container;
3610 EXPECT_THAT(container, IsEmpty());
3611 container.push_back(0);
3612 EXPECT_THAT(container, Not(IsEmpty()));
3613 container.push_back(1);
3614 EXPECT_THAT(container, Not(IsEmpty()));
3615}
3616
3617TEST(IsEmptyTest, WorksWithString) {
3618 string text;
3619 EXPECT_THAT(text, IsEmpty());
3620 text = "foo";
3621 EXPECT_THAT(text, Not(IsEmpty()));
3622 text = string("\0", 1);
3623 EXPECT_THAT(text, Not(IsEmpty()));
3624}
3625
3626TEST(IsEmptyTest, CanDescribeSelf) {
3627 Matcher<vector<int> > m = IsEmpty();
3628 EXPECT_EQ("is empty", Describe(m));
3629 EXPECT_EQ("isn't empty", DescribeNegation(m));
3630}
3631
3632TEST(IsEmptyTest, ExplainsResult) {
3633 Matcher<vector<int> > m = IsEmpty();
3634 vector<int> container;
3635 EXPECT_EQ("", Explain(m, container));
3636 container.push_back(0);
3637 EXPECT_EQ("whose size is 1", Explain(m, container));
3638}
3639
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003640#if GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003641// Tests ContainerEq with different container types, and
3642// different element types.
3643
3644template <typename T>
zhanyong.wanb8243162009-06-04 05:48:20 +00003645class ContainerEqTest : public testing::Test {};
zhanyong.wan6a896b52009-01-16 01:13:50 +00003646
3647typedef testing::Types<
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003648 set<int>,
3649 vector<size_t>,
3650 multiset<size_t>,
3651 list<int> >
zhanyong.wan6a896b52009-01-16 01:13:50 +00003652 ContainerEqTestTypes;
3653
3654TYPED_TEST_CASE(ContainerEqTest, ContainerEqTestTypes);
3655
3656// Tests that the filled container is equal to itself.
3657TYPED_TEST(ContainerEqTest, EqualsSelf) {
3658 static const int vals[] = {1, 1, 2, 3, 5, 8};
3659 TypeParam my_set(vals, vals + 6);
3660 const Matcher<TypeParam> m = ContainerEq(my_set);
3661 EXPECT_TRUE(m.Matches(my_set));
3662 EXPECT_EQ("", Explain(m, my_set));
3663}
3664
3665// Tests that missing values are reported.
3666TYPED_TEST(ContainerEqTest, ValueMissing) {
3667 static const int vals[] = {1, 1, 2, 3, 5, 8};
3668 static const int test_vals[] = {2, 1, 8, 5};
3669 TypeParam my_set(vals, vals + 6);
3670 TypeParam test_set(test_vals, test_vals + 4);
3671 const Matcher<TypeParam> m = ContainerEq(my_set);
3672 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003673 EXPECT_EQ("which doesn't have these expected elements: 3",
3674 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003675}
3676
3677// Tests that added values are reported.
3678TYPED_TEST(ContainerEqTest, ValueAdded) {
3679 static const int vals[] = {1, 1, 2, 3, 5, 8};
3680 static const int test_vals[] = {1, 2, 3, 5, 8, 46};
3681 TypeParam my_set(vals, vals + 6);
3682 TypeParam test_set(test_vals, test_vals + 6);
3683 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3684 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003685 EXPECT_EQ("which has these unexpected elements: 46", Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003686}
3687
3688// Tests that added and missing values are reported together.
3689TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) {
3690 static const int vals[] = {1, 1, 2, 3, 5, 8};
3691 static const int test_vals[] = {1, 2, 3, 8, 46};
3692 TypeParam my_set(vals, vals + 6);
3693 TypeParam test_set(test_vals, test_vals + 5);
3694 const Matcher<TypeParam> m = ContainerEq(my_set);
3695 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003696 EXPECT_EQ("which has these unexpected elements: 46,\n"
3697 "and doesn't have these expected elements: 5",
3698 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003699}
3700
3701// Tests duplicated value -- expect no explanation.
3702TYPED_TEST(ContainerEqTest, DuplicateDifference) {
3703 static const int vals[] = {1, 1, 2, 3, 5, 8};
3704 static const int test_vals[] = {1, 2, 3, 5, 8};
3705 TypeParam my_set(vals, vals + 6);
3706 TypeParam test_set(test_vals, test_vals + 5);
3707 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3708 // Depending on the container, match may be true or false
3709 // But in any case there should be no explanation.
3710 EXPECT_EQ("", Explain(m, test_set));
3711}
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003712#endif // GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003713
3714// Tests that mutliple missing values are reported.
3715// Using just vector here, so order is predicatble.
3716TEST(ContainerEqExtraTest, MultipleValuesMissing) {
3717 static const int vals[] = {1, 1, 2, 3, 5, 8};
3718 static const int test_vals[] = {2, 1, 5};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003719 vector<int> my_set(vals, vals + 6);
3720 vector<int> test_set(test_vals, test_vals + 3);
3721 const Matcher<vector<int> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003722 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003723 EXPECT_EQ("which doesn't have these expected elements: 3, 8",
3724 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003725}
3726
3727// Tests that added values are reported.
3728// Using just vector here, so order is predicatble.
3729TEST(ContainerEqExtraTest, MultipleValuesAdded) {
3730 static const int vals[] = {1, 1, 2, 3, 5, 8};
3731 static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003732 list<size_t> my_set(vals, vals + 6);
3733 list<size_t> test_set(test_vals, test_vals + 7);
3734 const Matcher<const list<size_t>&> m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003735 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003736 EXPECT_EQ("which has these unexpected elements: 92, 46",
3737 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003738}
3739
3740// Tests that added and missing values are reported together.
3741TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) {
3742 static const int vals[] = {1, 1, 2, 3, 5, 8};
3743 static const int test_vals[] = {1, 2, 3, 92, 46};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003744 list<size_t> my_set(vals, vals + 6);
3745 list<size_t> test_set(test_vals, test_vals + 5);
3746 const Matcher<const list<size_t> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003747 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003748 EXPECT_EQ("which has these unexpected elements: 92, 46,\n"
3749 "and doesn't have these expected elements: 5, 8",
zhanyong.wan6a896b52009-01-16 01:13:50 +00003750 Explain(m, test_set));
3751}
3752
3753// Tests to see that duplicate elements are detected,
3754// but (as above) not reported in the explanation.
3755TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) {
3756 static const int vals[] = {1, 1, 2, 3, 5, 8};
3757 static const int test_vals[] = {1, 2, 3, 5, 8};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003758 vector<int> my_set(vals, vals + 6);
3759 vector<int> test_set(test_vals, test_vals + 5);
3760 const Matcher<vector<int> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003761 EXPECT_TRUE(m.Matches(my_set));
3762 EXPECT_FALSE(m.Matches(test_set));
3763 // There is nothing to report when both sets contain all the same values.
3764 EXPECT_EQ("", Explain(m, test_set));
3765}
3766
3767// Tests that ContainerEq works for non-trivial associative containers,
3768// like maps.
3769TEST(ContainerEqExtraTest, WorksForMaps) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003770 map<int, std::string> my_map;
zhanyong.wan6a896b52009-01-16 01:13:50 +00003771 my_map[0] = "a";
3772 my_map[1] = "b";
3773
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003774 map<int, std::string> test_map;
zhanyong.wan6a896b52009-01-16 01:13:50 +00003775 test_map[0] = "aa";
3776 test_map[1] = "b";
3777
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003778 const Matcher<const map<int, std::string>&> m = ContainerEq(my_map);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003779 EXPECT_TRUE(m.Matches(my_map));
3780 EXPECT_FALSE(m.Matches(test_map));
3781
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003782 EXPECT_EQ("which has these unexpected elements: (0, \"aa\"),\n"
3783 "and doesn't have these expected elements: (0, \"a\")",
zhanyong.wan6a896b52009-01-16 01:13:50 +00003784 Explain(m, test_map));
3785}
3786
zhanyong.wanb8243162009-06-04 05:48:20 +00003787TEST(ContainerEqExtraTest, WorksForNativeArray) {
3788 int a1[] = { 1, 2, 3 };
3789 int a2[] = { 1, 2, 3 };
3790 int b[] = { 1, 2, 4 };
3791
3792 EXPECT_THAT(a1, ContainerEq(a2));
3793 EXPECT_THAT(a1, Not(ContainerEq(b)));
3794}
3795
3796TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) {
3797 const char a1[][3] = { "hi", "lo" };
3798 const char a2[][3] = { "hi", "lo" };
3799 const char b[][3] = { "lo", "hi" };
3800
3801 // Tests using ContainerEq() in the first dimension.
3802 EXPECT_THAT(a1, ContainerEq(a2));
3803 EXPECT_THAT(a1, Not(ContainerEq(b)));
3804
3805 // Tests using ContainerEq() in the second dimension.
3806 EXPECT_THAT(a1, ElementsAre(ContainerEq(a2[0]), ContainerEq(a2[1])));
3807 EXPECT_THAT(a1, ElementsAre(Not(ContainerEq(b[0])), ContainerEq(a2[1])));
3808}
3809
3810TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) {
3811 const int a1[] = { 1, 2, 3 };
3812 const int a2[] = { 1, 2, 3 };
3813 const int b[] = { 1, 2, 3, 4 };
3814
zhanyong.wan2661c682009-06-09 05:42:12 +00003815 const int* const p1 = a1;
3816 EXPECT_THAT(make_tuple(p1, 3), ContainerEq(a2));
3817 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(b)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003818
3819 const int c[] = { 1, 3, 2 };
zhanyong.wan2661c682009-06-09 05:42:12 +00003820 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(c)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003821}
3822
3823TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) {
3824 std::string a1[][3] = {
3825 { "hi", "hello", "ciao" },
3826 { "bye", "see you", "ciao" }
3827 };
3828
3829 std::string a2[][3] = {
3830 { "hi", "hello", "ciao" },
3831 { "bye", "see you", "ciao" }
3832 };
3833
3834 const Matcher<const std::string(&)[2][3]> m = ContainerEq(a2);
3835 EXPECT_THAT(a1, m);
3836
3837 a2[0][0] = "ha";
3838 EXPECT_THAT(a1, m);
3839}
3840
zhanyong.wan898725c2011-09-16 16:45:39 +00003841TEST(WhenSortedByTest, WorksForEmptyContainer) {
3842 const vector<int> numbers;
3843 EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre()));
3844 EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1))));
3845}
3846
3847TEST(WhenSortedByTest, WorksForNonEmptyContainer) {
3848 vector<unsigned> numbers;
3849 numbers.push_back(3);
3850 numbers.push_back(1);
3851 numbers.push_back(2);
3852 numbers.push_back(2);
3853 EXPECT_THAT(numbers, WhenSortedBy(greater<unsigned>(),
3854 ElementsAre(3, 2, 2, 1)));
3855 EXPECT_THAT(numbers, Not(WhenSortedBy(greater<unsigned>(),
3856 ElementsAre(1, 2, 2, 3))));
3857}
3858
3859TEST(WhenSortedByTest, WorksForNonVectorContainer) {
3860 list<string> words;
3861 words.push_back("say");
3862 words.push_back("hello");
3863 words.push_back("world");
3864 EXPECT_THAT(words, WhenSortedBy(less<string>(),
3865 ElementsAre("hello", "say", "world")));
3866 EXPECT_THAT(words, Not(WhenSortedBy(less<string>(),
3867 ElementsAre("say", "hello", "world"))));
3868}
3869
3870TEST(WhenSortedByTest, WorksForNativeArray) {
3871 const int numbers[] = { 1, 3, 2, 4 };
3872 const int sorted_numbers[] = { 1, 2, 3, 4 };
3873 EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre(1, 2, 3, 4)));
3874 EXPECT_THAT(numbers, WhenSortedBy(less<int>(),
3875 ElementsAreArray(sorted_numbers)));
3876 EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1, 3, 2, 4))));
3877}
3878
3879TEST(WhenSortedByTest, CanDescribeSelf) {
3880 const Matcher<vector<int> > m = WhenSortedBy(less<int>(), ElementsAre(1, 2));
3881 EXPECT_EQ("(when sorted) has 2 elements where\n"
3882 "element #0 is equal to 1,\n"
3883 "element #1 is equal to 2",
3884 Describe(m));
3885 EXPECT_EQ("(when sorted) doesn't have 2 elements, or\n"
3886 "element #0 isn't equal to 1, or\n"
3887 "element #1 isn't equal to 2",
3888 DescribeNegation(m));
3889}
3890
3891TEST(WhenSortedByTest, ExplainsMatchResult) {
3892 const int a[] = { 2, 1 };
3893 EXPECT_EQ("which is { 1, 2 } when sorted, whose element #0 doesn't match",
3894 Explain(WhenSortedBy(less<int>(), ElementsAre(2, 3)), a));
3895 EXPECT_EQ("which is { 1, 2 } when sorted",
3896 Explain(WhenSortedBy(less<int>(), ElementsAre(1, 2)), a));
3897}
3898
3899// WhenSorted() is a simple wrapper on WhenSortedBy(). Hence we don't
3900// need to test it as exhaustively as we test the latter.
3901
3902TEST(WhenSortedTest, WorksForEmptyContainer) {
3903 const vector<int> numbers;
3904 EXPECT_THAT(numbers, WhenSorted(ElementsAre()));
3905 EXPECT_THAT(numbers, Not(WhenSorted(ElementsAre(1))));
3906}
3907
3908TEST(WhenSortedTest, WorksForNonEmptyContainer) {
3909 list<string> words;
3910 words.push_back("3");
3911 words.push_back("1");
3912 words.push_back("2");
3913 words.push_back("2");
3914 EXPECT_THAT(words, WhenSorted(ElementsAre("1", "2", "2", "3")));
3915 EXPECT_THAT(words, Not(WhenSorted(ElementsAre("3", "1", "2", "2"))));
3916}
3917
zhanyong.wan736baa82010-09-27 17:44:16 +00003918// Tests IsReadableTypeName().
3919
3920TEST(IsReadableTypeNameTest, ReturnsTrueForShortNames) {
3921 EXPECT_TRUE(IsReadableTypeName("int"));
3922 EXPECT_TRUE(IsReadableTypeName("const unsigned char*"));
3923 EXPECT_TRUE(IsReadableTypeName("MyMap<int, void*>"));
3924 EXPECT_TRUE(IsReadableTypeName("void (*)(int, bool)"));
3925}
3926
3927TEST(IsReadableTypeNameTest, ReturnsTrueForLongNonTemplateNonFunctionNames) {
3928 EXPECT_TRUE(IsReadableTypeName("my_long_namespace::MyClassName"));
3929 EXPECT_TRUE(IsReadableTypeName("int [5][6][7][8][9][10][11]"));
3930 EXPECT_TRUE(IsReadableTypeName("my_namespace::MyOuterClass::MyInnerClass"));
3931}
3932
3933TEST(IsReadableTypeNameTest, ReturnsFalseForLongTemplateNames) {
3934 EXPECT_FALSE(
3935 IsReadableTypeName("basic_string<char, std::char_traits<char> >"));
3936 EXPECT_FALSE(IsReadableTypeName("std::vector<int, std::alloc_traits<int> >"));
3937}
3938
3939TEST(IsReadableTypeNameTest, ReturnsFalseForLongFunctionTypeNames) {
3940 EXPECT_FALSE(IsReadableTypeName("void (&)(int, bool, char, float)"));
3941}
3942
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003943// Tests JoinAsTuple().
3944
3945TEST(JoinAsTupleTest, JoinsEmptyTuple) {
3946 EXPECT_EQ("", JoinAsTuple(Strings()));
3947}
3948
3949TEST(JoinAsTupleTest, JoinsOneTuple) {
3950 const char* fields[] = { "1" };
3951 EXPECT_EQ("1", JoinAsTuple(Strings(fields, fields + 1)));
3952}
3953
3954TEST(JoinAsTupleTest, JoinsTwoTuple) {
3955 const char* fields[] = { "1", "a" };
3956 EXPECT_EQ("(1, a)", JoinAsTuple(Strings(fields, fields + 2)));
3957}
3958
3959TEST(JoinAsTupleTest, JoinsTenTuple) {
3960 const char* fields[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
3961 EXPECT_EQ("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)",
3962 JoinAsTuple(Strings(fields, fields + 10)));
3963}
3964
3965// Tests FormatMatcherDescription().
3966
3967TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) {
3968 EXPECT_EQ("is even",
zhanyong.wanb4140802010-06-08 22:53:57 +00003969 FormatMatcherDescription(false, "IsEven", Strings()));
3970 EXPECT_EQ("not (is even)",
3971 FormatMatcherDescription(true, "IsEven", Strings()));
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003972
3973 const char* params[] = { "5" };
3974 EXPECT_EQ("equals 5",
zhanyong.wanb4140802010-06-08 22:53:57 +00003975 FormatMatcherDescription(false, "Equals",
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003976 Strings(params, params + 1)));
3977
3978 const char* params2[] = { "5", "8" };
3979 EXPECT_EQ("is in range (5, 8)",
zhanyong.wanb4140802010-06-08 22:53:57 +00003980 FormatMatcherDescription(false, "IsInRange",
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003981 Strings(params2, params2 + 2)));
3982}
3983
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003984// Tests PolymorphicMatcher::mutable_impl().
3985TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {
3986 PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
3987 DivisibleByImpl& impl = m.mutable_impl();
3988 EXPECT_EQ(42, impl.divider());
3989
3990 impl.set_divider(0);
3991 EXPECT_EQ(0, m.mutable_impl().divider());
3992}
3993
3994// Tests PolymorphicMatcher::impl().
3995TEST(PolymorphicMatcherTest, CanAccessImpl) {
3996 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
3997 const DivisibleByImpl& impl = m.impl();
3998 EXPECT_EQ(42, impl.divider());
3999}
4000
zhanyong.wanb1c7f932010-03-24 17:35:11 +00004001TEST(MatcherTupleTest, ExplainsMatchFailure) {
4002 stringstream ss1;
4003 ExplainMatchFailureTupleTo(make_tuple(Matcher<char>(Eq('a')), GreaterThan(5)),
4004 make_tuple('a', 10), &ss1);
4005 EXPECT_EQ("", ss1.str()); // Successful match.
4006
4007 stringstream ss2;
4008 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
4009 make_tuple(2, 'b'), &ss2);
4010 EXPECT_EQ(" Expected arg #0: is > 5\n"
4011 " Actual: 2, which is 3 less than 5\n"
zhanyong.wand60c5f42010-07-21 22:21:07 +00004012 " Expected arg #1: is equal to 'a' (97, 0x61)\n"
4013 " Actual: 'b' (98, 0x62)\n",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00004014 ss2.str()); // Failed match where both arguments need explanation.
4015
4016 stringstream ss3;
4017 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
4018 make_tuple(2, 'a'), &ss3);
4019 EXPECT_EQ(" Expected arg #0: is > 5\n"
4020 " Actual: 2, which is 3 less than 5\n",
4021 ss3.str()); // Failed match where only one argument needs
4022 // explanation.
4023}
4024
zhanyong.wan33605ba2010-04-22 23:37:47 +00004025// Tests Each().
4026
4027TEST(EachTest, ExplainsMatchResultCorrectly) {
4028 set<int> a; // empty
4029
4030 Matcher<set<int> > m = Each(2);
4031 EXPECT_EQ("", Explain(m, a));
4032
zhanyong.wan736baa82010-09-27 17:44:16 +00004033 Matcher<const int(&)[1]> n = Each(1); // NOLINT
zhanyong.wan33605ba2010-04-22 23:37:47 +00004034
4035 const int b[1] = { 1 };
4036 EXPECT_EQ("", Explain(n, b));
4037
4038 n = Each(3);
4039 EXPECT_EQ("whose element #0 doesn't match", Explain(n, b));
4040
4041 a.insert(1);
4042 a.insert(2);
4043 a.insert(3);
4044 m = Each(GreaterThan(0));
4045 EXPECT_EQ("", Explain(m, a));
4046
4047 m = Each(GreaterThan(10));
4048 EXPECT_EQ("whose element #0 doesn't match, which is 9 less than 10",
4049 Explain(m, a));
4050}
4051
4052TEST(EachTest, DescribesItselfCorrectly) {
4053 Matcher<vector<int> > m = Each(1);
4054 EXPECT_EQ("only contains elements that is equal to 1", Describe(m));
4055
4056 Matcher<vector<int> > m2 = Not(m);
4057 EXPECT_EQ("contains some element that isn't equal to 1", Describe(m2));
4058}
4059
4060TEST(EachTest, MatchesVectorWhenAllElementsMatch) {
4061 vector<int> some_vector;
4062 EXPECT_THAT(some_vector, Each(1));
4063 some_vector.push_back(3);
4064 EXPECT_THAT(some_vector, Not(Each(1)));
4065 EXPECT_THAT(some_vector, Each(3));
4066 some_vector.push_back(1);
4067 some_vector.push_back(2);
4068 EXPECT_THAT(some_vector, Not(Each(3)));
4069 EXPECT_THAT(some_vector, Each(Lt(3.5)));
4070
4071 vector<string> another_vector;
4072 another_vector.push_back("fee");
4073 EXPECT_THAT(another_vector, Each(string("fee")));
4074 another_vector.push_back("fie");
4075 another_vector.push_back("foe");
4076 another_vector.push_back("fum");
4077 EXPECT_THAT(another_vector, Not(Each(string("fee"))));
4078}
4079
4080TEST(EachTest, MatchesMapWhenAllElementsMatch) {
4081 map<const char*, int> my_map;
4082 const char* bar = "a string";
4083 my_map[bar] = 2;
4084 EXPECT_THAT(my_map, Each(make_pair(bar, 2)));
4085
4086 map<string, int> another_map;
4087 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1)));
4088 another_map["fee"] = 1;
4089 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1)));
4090 another_map["fie"] = 2;
4091 another_map["foe"] = 3;
4092 another_map["fum"] = 4;
4093 EXPECT_THAT(another_map, Not(Each(make_pair(string("fee"), 1))));
4094 EXPECT_THAT(another_map, Not(Each(make_pair(string("fum"), 1))));
4095 EXPECT_THAT(another_map, Each(Pair(_, Gt(0))));
4096}
4097
4098TEST(EachTest, AcceptsMatcher) {
4099 const int a[] = { 1, 2, 3 };
4100 EXPECT_THAT(a, Each(Gt(0)));
4101 EXPECT_THAT(a, Not(Each(Gt(1))));
4102}
4103
4104TEST(EachTest, WorksForNativeArrayAsTuple) {
4105 const int a[] = { 1, 2 };
4106 const int* const pointer = a;
4107 EXPECT_THAT(make_tuple(pointer, 2), Each(Gt(0)));
4108 EXPECT_THAT(make_tuple(pointer, 2), Not(Each(Gt(1))));
4109}
4110
zhanyong.wanab5b77c2010-05-17 19:32:48 +00004111// For testing Pointwise().
4112class IsHalfOfMatcher {
4113 public:
4114 template <typename T1, typename T2>
4115 bool MatchAndExplain(const tuple<T1, T2>& a_pair,
4116 MatchResultListener* listener) const {
4117 if (get<0>(a_pair) == get<1>(a_pair)/2) {
4118 *listener << "where the second is " << get<1>(a_pair);
4119 return true;
4120 } else {
4121 *listener << "where the second/2 is " << get<1>(a_pair)/2;
4122 return false;
4123 }
4124 }
4125
4126 void DescribeTo(ostream* os) const {
4127 *os << "are a pair where the first is half of the second";
4128 }
4129
4130 void DescribeNegationTo(ostream* os) const {
4131 *os << "are a pair where the first isn't half of the second";
4132 }
4133};
4134
4135PolymorphicMatcher<IsHalfOfMatcher> IsHalfOf() {
4136 return MakePolymorphicMatcher(IsHalfOfMatcher());
4137}
4138
4139TEST(PointwiseTest, DescribesSelf) {
4140 vector<int> rhs;
4141 rhs.push_back(1);
4142 rhs.push_back(2);
4143 rhs.push_back(3);
4144 const Matcher<const vector<int>&> m = Pointwise(IsHalfOf(), rhs);
4145 EXPECT_EQ("contains 3 values, where each value and its corresponding value "
4146 "in { 1, 2, 3 } are a pair where the first is half of the second",
4147 Describe(m));
4148 EXPECT_EQ("doesn't contain exactly 3 values, or contains a value x at some "
4149 "index i where x and the i-th value of { 1, 2, 3 } are a pair "
4150 "where the first isn't half of the second",
4151 DescribeNegation(m));
4152}
4153
4154TEST(PointwiseTest, MakesCopyOfRhs) {
4155 list<signed char> rhs;
4156 rhs.push_back(2);
4157 rhs.push_back(4);
4158
4159 int lhs[] = { 1, 2 };
4160 const Matcher<const int (&)[2]> m = Pointwise(IsHalfOf(), rhs);
4161 EXPECT_THAT(lhs, m);
4162
4163 // Changing rhs now shouldn't affect m, which made a copy of rhs.
4164 rhs.push_back(6);
4165 EXPECT_THAT(lhs, m);
4166}
4167
4168TEST(PointwiseTest, WorksForLhsNativeArray) {
4169 const int lhs[] = { 1, 2, 3 };
4170 vector<int> rhs;
4171 rhs.push_back(2);
4172 rhs.push_back(4);
4173 rhs.push_back(6);
4174 EXPECT_THAT(lhs, Pointwise(Lt(), rhs));
4175 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
4176}
4177
4178TEST(PointwiseTest, WorksForRhsNativeArray) {
4179 const int rhs[] = { 1, 2, 3 };
4180 vector<int> lhs;
4181 lhs.push_back(2);
4182 lhs.push_back(4);
4183 lhs.push_back(6);
4184 EXPECT_THAT(lhs, Pointwise(Gt(), rhs));
4185 EXPECT_THAT(lhs, Not(Pointwise(Lt(), rhs)));
4186}
4187
4188TEST(PointwiseTest, RejectsWrongSize) {
4189 const double lhs[2] = { 1, 2 };
4190 const int rhs[1] = { 0 };
4191 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
4192 EXPECT_EQ("which contains 2 values",
4193 Explain(Pointwise(Gt(), rhs), lhs));
4194
4195 const int rhs2[3] = { 0, 1, 2 };
4196 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs2)));
4197}
4198
4199TEST(PointwiseTest, RejectsWrongContent) {
4200 const double lhs[3] = { 1, 2, 3 };
4201 const int rhs[3] = { 2, 6, 4 };
4202 EXPECT_THAT(lhs, Not(Pointwise(IsHalfOf(), rhs)));
4203 EXPECT_EQ("where the value pair (2, 6) at index #1 don't match, "
4204 "where the second/2 is 3",
4205 Explain(Pointwise(IsHalfOf(), rhs), lhs));
4206}
4207
4208TEST(PointwiseTest, AcceptsCorrectContent) {
4209 const double lhs[3] = { 1, 2, 3 };
4210 const int rhs[3] = { 2, 4, 6 };
4211 EXPECT_THAT(lhs, Pointwise(IsHalfOf(), rhs));
4212 EXPECT_EQ("", Explain(Pointwise(IsHalfOf(), rhs), lhs));
4213}
4214
4215TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) {
4216 const double lhs[3] = { 1, 2, 3 };
4217 const int rhs[3] = { 2, 4, 6 };
4218 const Matcher<tuple<const double&, const int&> > m1 = IsHalfOf();
4219 EXPECT_THAT(lhs, Pointwise(m1, rhs));
4220 EXPECT_EQ("", Explain(Pointwise(m1, rhs), lhs));
4221
4222 // This type works as a tuple<const double&, const int&> can be
4223 // implicitly cast to tuple<double, int>.
4224 const Matcher<tuple<double, int> > m2 = IsHalfOf();
4225 EXPECT_THAT(lhs, Pointwise(m2, rhs));
4226 EXPECT_EQ("", Explain(Pointwise(m2, rhs), lhs));
4227}
4228
shiqiane35fdd92008-12-10 05:08:54 +00004229} // namespace gmock_matchers_test
4230} // namespace testing