blob: 8f96efc20851ff652ab5c0964b03c2df7a48cb32 [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"
shiqiane35fdd92008-12-10 05:08:54 +000037
38#include <string.h>
39#include <functional>
zhanyong.wana862f1d2010-03-15 21:23:04 +000040#include <iostream>
zhanyong.wan6a896b52009-01-16 01:13:50 +000041#include <list>
42#include <map>
43#include <set>
shiqiane35fdd92008-12-10 05:08:54 +000044#include <sstream>
zhanyong.wan6a896b52009-01-16 01:13:50 +000045#include <string>
zhanyong.wanf5e1ce52009-09-16 07:02:02 +000046#include <utility>
zhanyong.wan6a896b52009-01-16 01:13:50 +000047#include <vector>
zhanyong.wan53e08c42010-09-14 05:38:21 +000048#include "gmock/gmock.h"
49#include "gtest/gtest.h"
50#include "gtest/gtest-spi.h"
shiqiane35fdd92008-12-10 05:08:54 +000051
52namespace testing {
zhanyong.wan4a5330d2009-02-19 00:36:44 +000053
54namespace internal {
vladlosev587c1b32011-05-20 00:42:22 +000055GTEST_API_ string JoinAsTuple(const Strings& fields);
zhanyong.wan4a5330d2009-02-19 00:36:44 +000056} // namespace internal
57
shiqiane35fdd92008-12-10 05:08:54 +000058namespace gmock_matchers_test {
59
zhanyong.wanab5b77c2010-05-17 19:32:48 +000060using std::list;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000061using std::make_pair;
zhanyong.wanb5937da2009-07-16 20:26:41 +000062using std::map;
63using std::multimap;
zhanyong.wanab5b77c2010-05-17 19:32:48 +000064using std::multiset;
65using std::ostream;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000066using std::pair;
zhanyong.wan33605ba2010-04-22 23:37:47 +000067using std::set;
shiqiane35fdd92008-12-10 05:08:54 +000068using std::stringstream;
zhanyong.wanab5b77c2010-05-17 19:32:48 +000069using std::tr1::get;
zhanyong.wanb8243162009-06-04 05:48:20 +000070using std::tr1::make_tuple;
zhanyong.wanab5b77c2010-05-17 19:32:48 +000071using std::tr1::tuple;
zhanyong.wan33605ba2010-04-22 23:37:47 +000072using std::vector;
shiqiane35fdd92008-12-10 05:08:54 +000073using testing::A;
zhanyong.wanbf550852009-06-09 06:09:53 +000074using testing::AllArgs;
shiqiane35fdd92008-12-10 05:08:54 +000075using testing::AllOf;
76using testing::An;
77using testing::AnyOf;
78using testing::ByRef;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000079using testing::ContainsRegex;
shiqiane35fdd92008-12-10 05:08:54 +000080using testing::DoubleEq;
81using testing::EndsWith;
82using testing::Eq;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000083using testing::ExplainMatchResult;
shiqiane35fdd92008-12-10 05:08:54 +000084using testing::Field;
85using testing::FloatEq;
86using testing::Ge;
87using testing::Gt;
88using testing::HasSubstr;
zhanyong.wan2d970ee2009-09-24 21:41:36 +000089using testing::IsNull;
zhanyong.wanb5937da2009-07-16 20:26:41 +000090using testing::Key;
shiqiane35fdd92008-12-10 05:08:54 +000091using testing::Le;
92using testing::Lt;
93using testing::MakeMatcher;
94using testing::MakePolymorphicMatcher;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000095using testing::MatchResultListener;
shiqiane35fdd92008-12-10 05:08:54 +000096using testing::Matcher;
97using testing::MatcherCast;
98using testing::MatcherInterface;
99using testing::Matches;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000100using testing::MatchesRegex;
shiqiane35fdd92008-12-10 05:08:54 +0000101using testing::NanSensitiveDoubleEq;
102using testing::NanSensitiveFloatEq;
103using testing::Ne;
104using testing::Not;
105using testing::NotNull;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000106using testing::Pair;
shiqiane35fdd92008-12-10 05:08:54 +0000107using testing::Pointee;
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000108using testing::Pointwise;
shiqiane35fdd92008-12-10 05:08:54 +0000109using testing::PolymorphicMatcher;
110using testing::Property;
111using testing::Ref;
112using testing::ResultOf;
113using testing::StartsWith;
114using testing::StrCaseEq;
115using testing::StrCaseNe;
116using testing::StrEq;
117using testing::StrNe;
118using testing::Truly;
119using testing::TypedEq;
zhanyong.wanb8243162009-06-04 05:48:20 +0000120using testing::Value;
shiqiane35fdd92008-12-10 05:08:54 +0000121using testing::_;
zhanyong.wana862f1d2010-03-15 21:23:04 +0000122using testing::internal::DummyMatchResultListener;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000123using testing::internal::ExplainMatchFailureTupleTo;
shiqiane35fdd92008-12-10 05:08:54 +0000124using testing::internal::FloatingEqMatcher;
zhanyong.wanb4140802010-06-08 22:53:57 +0000125using testing::internal::FormatMatcherDescription;
zhanyong.wan736baa82010-09-27 17:44:16 +0000126using testing::internal::IsReadableTypeName;
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000127using testing::internal::JoinAsTuple;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000128using testing::internal::RE;
zhanyong.wana862f1d2010-03-15 21:23:04 +0000129using testing::internal::StreamMatchResultListener;
shiqiane35fdd92008-12-10 05:08:54 +0000130using testing::internal::String;
zhanyong.wan34b034c2010-03-05 21:23:23 +0000131using testing::internal::StringMatchResultListener;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000132using testing::internal::Strings;
vladlosev79b83502009-11-18 00:43:37 +0000133using testing::internal::linked_ptr;
vladloseve56daa72009-11-18 01:08:08 +0000134using testing::internal::scoped_ptr;
shiqiane35fdd92008-12-10 05:08:54 +0000135using testing::internal::string;
136
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000137// For testing ExplainMatchResultTo().
138class GreaterThanMatcher : public MatcherInterface<int> {
139 public:
140 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
141
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000142 virtual void DescribeTo(ostream* os) const {
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000143 *os << "is > " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000144 }
145
zhanyong.wandb22c222010-01-28 21:52:29 +0000146 virtual bool MatchAndExplain(int lhs,
147 MatchResultListener* listener) const {
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000148 const int diff = lhs - rhs_;
149 if (diff > 0) {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000150 *listener << "which is " << diff << " more than " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000151 } else if (diff == 0) {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000152 *listener << "which is the same as " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000153 } else {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000154 *listener << "which is " << -diff << " less than " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000155 }
zhanyong.wandb22c222010-01-28 21:52:29 +0000156
157 return lhs > rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000158 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000159
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000160 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000161 int rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000162};
163
164Matcher<int> GreaterThan(int n) {
165 return MakeMatcher(new GreaterThanMatcher(n));
166}
167
zhanyong.wan736baa82010-09-27 17:44:16 +0000168string OfType(const string& type_name) {
169#if GTEST_HAS_RTTI
170 return " (of type " + type_name + ")";
171#else
172 return "";
173#endif
174}
175
shiqiane35fdd92008-12-10 05:08:54 +0000176// Returns the description of the given matcher.
177template <typename T>
178string Describe(const Matcher<T>& m) {
179 stringstream ss;
180 m.DescribeTo(&ss);
181 return ss.str();
182}
183
184// Returns the description of the negation of the given matcher.
185template <typename T>
186string DescribeNegation(const Matcher<T>& m) {
187 stringstream ss;
188 m.DescribeNegationTo(&ss);
189 return ss.str();
190}
191
192// Returns the reason why x matches, or doesn't match, m.
193template <typename MatcherType, typename Value>
194string Explain(const MatcherType& m, const Value& x) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000195 StringMatchResultListener listener;
196 ExplainMatchResult(m, x, &listener);
197 return listener.str();
shiqiane35fdd92008-12-10 05:08:54 +0000198}
199
zhanyong.wana862f1d2010-03-15 21:23:04 +0000200TEST(MatchResultListenerTest, StreamingWorks) {
201 StringMatchResultListener listener;
202 listener << "hi" << 5;
203 EXPECT_EQ("hi5", listener.str());
204
205 // Streaming shouldn't crash when the underlying ostream is NULL.
206 DummyMatchResultListener dummy;
207 dummy << "hi" << 5;
208}
209
210TEST(MatchResultListenerTest, CanAccessUnderlyingStream) {
211 EXPECT_TRUE(DummyMatchResultListener().stream() == NULL);
212 EXPECT_TRUE(StreamMatchResultListener(NULL).stream() == NULL);
213
214 EXPECT_EQ(&std::cout, StreamMatchResultListener(&std::cout).stream());
215}
216
217TEST(MatchResultListenerTest, IsInterestedWorks) {
218 EXPECT_TRUE(StringMatchResultListener().IsInterested());
219 EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested());
220
221 EXPECT_FALSE(DummyMatchResultListener().IsInterested());
222 EXPECT_FALSE(StreamMatchResultListener(NULL).IsInterested());
223}
224
shiqiane35fdd92008-12-10 05:08:54 +0000225// Makes sure that the MatcherInterface<T> interface doesn't
226// change.
227class EvenMatcherImpl : public MatcherInterface<int> {
228 public:
zhanyong.wandb22c222010-01-28 21:52:29 +0000229 virtual bool MatchAndExplain(int x,
230 MatchResultListener* /* listener */) const {
231 return x % 2 == 0;
232 }
shiqiane35fdd92008-12-10 05:08:54 +0000233
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000234 virtual void DescribeTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +0000235 *os << "is an even number";
236 }
237
238 // We deliberately don't define DescribeNegationTo() and
239 // ExplainMatchResultTo() here, to make sure the definition of these
240 // two methods is optional.
241};
242
zhanyong.wana862f1d2010-03-15 21:23:04 +0000243// Makes sure that the MatcherInterface API doesn't change.
244TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) {
shiqiane35fdd92008-12-10 05:08:54 +0000245 EvenMatcherImpl m;
246}
247
zhanyong.wan82113312010-01-08 21:55:40 +0000248// Tests implementing a monomorphic matcher using MatchAndExplain().
249
250class NewEvenMatcherImpl : public MatcherInterface<int> {
251 public:
252 virtual bool MatchAndExplain(int x, MatchResultListener* listener) const {
253 const bool match = x % 2 == 0;
254 // Verifies that we can stream to a listener directly.
255 *listener << "value % " << 2;
256 if (listener->stream() != NULL) {
257 // Verifies that we can stream to a listener's underlying stream
258 // too.
259 *listener->stream() << " == " << (x % 2);
260 }
261 return match;
262 }
263
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000264 virtual void DescribeTo(ostream* os) const {
zhanyong.wan82113312010-01-08 21:55:40 +0000265 *os << "is an even number";
266 }
267};
268
269TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {
270 Matcher<int> m = MakeMatcher(new NewEvenMatcherImpl);
271 EXPECT_TRUE(m.Matches(2));
272 EXPECT_FALSE(m.Matches(3));
273 EXPECT_EQ("value % 2 == 0", Explain(m, 2));
274 EXPECT_EQ("value % 2 == 1", Explain(m, 3));
275}
276
shiqiane35fdd92008-12-10 05:08:54 +0000277// Tests default-constructing a matcher.
278TEST(MatcherTest, CanBeDefaultConstructed) {
279 Matcher<double> m;
280}
281
282// Tests that Matcher<T> can be constructed from a MatcherInterface<T>*.
283TEST(MatcherTest, CanBeConstructedFromMatcherInterface) {
284 const MatcherInterface<int>* impl = new EvenMatcherImpl;
285 Matcher<int> m(impl);
286 EXPECT_TRUE(m.Matches(4));
287 EXPECT_FALSE(m.Matches(5));
288}
289
290// Tests that value can be used in place of Eq(value).
291TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) {
292 Matcher<int> m1 = 5;
293 EXPECT_TRUE(m1.Matches(5));
294 EXPECT_FALSE(m1.Matches(6));
295}
296
297// Tests that NULL can be used in place of Eq(NULL).
298TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) {
299 Matcher<int*> m1 = NULL;
300 EXPECT_TRUE(m1.Matches(NULL));
301 int n = 0;
302 EXPECT_FALSE(m1.Matches(&n));
303}
304
305// Tests that matchers are copyable.
306TEST(MatcherTest, IsCopyable) {
307 // Tests the copy constructor.
308 Matcher<bool> m1 = Eq(false);
309 EXPECT_TRUE(m1.Matches(false));
310 EXPECT_FALSE(m1.Matches(true));
311
312 // Tests the assignment operator.
313 m1 = Eq(true);
314 EXPECT_TRUE(m1.Matches(true));
315 EXPECT_FALSE(m1.Matches(false));
316}
317
318// Tests that Matcher<T>::DescribeTo() calls
319// MatcherInterface<T>::DescribeTo().
320TEST(MatcherTest, CanDescribeItself) {
321 EXPECT_EQ("is an even number",
322 Describe(Matcher<int>(new EvenMatcherImpl)));
323}
324
zhanyong.wan82113312010-01-08 21:55:40 +0000325// Tests Matcher<T>::MatchAndExplain().
326TEST(MatcherTest, MatchAndExplain) {
327 Matcher<int> m = GreaterThan(0);
zhanyong.wan34b034c2010-03-05 21:23:23 +0000328 StringMatchResultListener listener1;
zhanyong.wan82113312010-01-08 21:55:40 +0000329 EXPECT_TRUE(m.MatchAndExplain(42, &listener1));
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000330 EXPECT_EQ("which is 42 more than 0", listener1.str());
zhanyong.wan82113312010-01-08 21:55:40 +0000331
zhanyong.wan34b034c2010-03-05 21:23:23 +0000332 StringMatchResultListener listener2;
zhanyong.wan82113312010-01-08 21:55:40 +0000333 EXPECT_FALSE(m.MatchAndExplain(-9, &listener2));
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000334 EXPECT_EQ("which is 9 less than 0", listener2.str());
zhanyong.wan82113312010-01-08 21:55:40 +0000335}
336
shiqiane35fdd92008-12-10 05:08:54 +0000337// Tests that a C-string literal can be implicitly converted to a
338// Matcher<string> or Matcher<const string&>.
339TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
340 Matcher<string> m1 = "hi";
341 EXPECT_TRUE(m1.Matches("hi"));
342 EXPECT_FALSE(m1.Matches("hello"));
343
344 Matcher<const string&> m2 = "hi";
345 EXPECT_TRUE(m2.Matches("hi"));
346 EXPECT_FALSE(m2.Matches("hello"));
347}
348
349// Tests that a string object can be implicitly converted to a
350// Matcher<string> or Matcher<const string&>.
351TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {
352 Matcher<string> m1 = string("hi");
353 EXPECT_TRUE(m1.Matches("hi"));
354 EXPECT_FALSE(m1.Matches("hello"));
355
356 Matcher<const string&> m2 = string("hi");
357 EXPECT_TRUE(m2.Matches("hi"));
358 EXPECT_FALSE(m2.Matches("hello"));
359}
360
361// Tests that MakeMatcher() constructs a Matcher<T> from a
362// MatcherInterface* without requiring the user to explicitly
363// write the type.
364TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) {
365 const MatcherInterface<int>* dummy_impl = NULL;
366 Matcher<int> m = MakeMatcher(dummy_impl);
367}
368
zhanyong.wan82113312010-01-08 21:55:40 +0000369// Tests that MakePolymorphicMatcher() can construct a polymorphic
370// matcher from its implementation using the old API.
zhanyong.wan33605ba2010-04-22 23:37:47 +0000371const int g_bar = 1;
shiqiane35fdd92008-12-10 05:08:54 +0000372class ReferencesBarOrIsZeroImpl {
373 public:
374 template <typename T>
zhanyong.wandb22c222010-01-28 21:52:29 +0000375 bool MatchAndExplain(const T& x,
376 MatchResultListener* /* listener */) const {
shiqiane35fdd92008-12-10 05:08:54 +0000377 const void* p = &x;
zhanyong.wan33605ba2010-04-22 23:37:47 +0000378 return p == &g_bar || x == 0;
shiqiane35fdd92008-12-10 05:08:54 +0000379 }
380
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000381 void DescribeTo(ostream* os) const { *os << "g_bar or zero"; }
shiqiane35fdd92008-12-10 05:08:54 +0000382
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000383 void DescribeNegationTo(ostream* os) const {
zhanyong.wan33605ba2010-04-22 23:37:47 +0000384 *os << "doesn't reference g_bar and is not zero";
shiqiane35fdd92008-12-10 05:08:54 +0000385 }
386};
387
388// This function verifies that MakePolymorphicMatcher() returns a
389// PolymorphicMatcher<T> where T is the argument's type.
390PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() {
391 return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl());
392}
393
zhanyong.wan82113312010-01-08 21:55:40 +0000394TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) {
shiqiane35fdd92008-12-10 05:08:54 +0000395 // Using a polymorphic matcher to match a reference type.
396 Matcher<const int&> m1 = ReferencesBarOrIsZero();
397 EXPECT_TRUE(m1.Matches(0));
398 // Verifies that the identity of a by-reference argument is preserved.
zhanyong.wan33605ba2010-04-22 23:37:47 +0000399 EXPECT_TRUE(m1.Matches(g_bar));
shiqiane35fdd92008-12-10 05:08:54 +0000400 EXPECT_FALSE(m1.Matches(1));
zhanyong.wan33605ba2010-04-22 23:37:47 +0000401 EXPECT_EQ("g_bar or zero", Describe(m1));
shiqiane35fdd92008-12-10 05:08:54 +0000402
403 // Using a polymorphic matcher to match a value type.
404 Matcher<double> m2 = ReferencesBarOrIsZero();
405 EXPECT_TRUE(m2.Matches(0.0));
406 EXPECT_FALSE(m2.Matches(0.1));
zhanyong.wan33605ba2010-04-22 23:37:47 +0000407 EXPECT_EQ("g_bar or zero", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +0000408}
409
zhanyong.wan82113312010-01-08 21:55:40 +0000410// Tests implementing a polymorphic matcher using MatchAndExplain().
411
412class PolymorphicIsEvenImpl {
413 public:
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000414 void DescribeTo(ostream* os) const { *os << "is even"; }
zhanyong.wan82113312010-01-08 21:55:40 +0000415
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000416 void DescribeNegationTo(ostream* os) const {
zhanyong.wan82113312010-01-08 21:55:40 +0000417 *os << "is odd";
418 }
zhanyong.wan82113312010-01-08 21:55:40 +0000419
zhanyong.wandb22c222010-01-28 21:52:29 +0000420 template <typename T>
421 bool MatchAndExplain(const T& x, MatchResultListener* listener) const {
422 // Verifies that we can stream to the listener directly.
423 *listener << "% " << 2;
424 if (listener->stream() != NULL) {
425 // Verifies that we can stream to the listener's underlying stream
426 // too.
427 *listener->stream() << " == " << (x % 2);
428 }
429 return (x % 2) == 0;
zhanyong.wan82113312010-01-08 21:55:40 +0000430 }
zhanyong.wandb22c222010-01-28 21:52:29 +0000431};
zhanyong.wan82113312010-01-08 21:55:40 +0000432
433PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() {
434 return MakePolymorphicMatcher(PolymorphicIsEvenImpl());
435}
436
437TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {
438 // Using PolymorphicIsEven() as a Matcher<int>.
439 const Matcher<int> m1 = PolymorphicIsEven();
440 EXPECT_TRUE(m1.Matches(42));
441 EXPECT_FALSE(m1.Matches(43));
442 EXPECT_EQ("is even", Describe(m1));
443
444 const Matcher<int> not_m1 = Not(m1);
445 EXPECT_EQ("is odd", Describe(not_m1));
446
447 EXPECT_EQ("% 2 == 0", Explain(m1, 42));
448
449 // Using PolymorphicIsEven() as a Matcher<char>.
450 const Matcher<char> m2 = PolymorphicIsEven();
451 EXPECT_TRUE(m2.Matches('\x42'));
452 EXPECT_FALSE(m2.Matches('\x43'));
453 EXPECT_EQ("is even", Describe(m2));
454
455 const Matcher<char> not_m2 = Not(m2);
456 EXPECT_EQ("is odd", Describe(not_m2));
457
458 EXPECT_EQ("% 2 == 0", Explain(m2, '\x42'));
459}
460
shiqiane35fdd92008-12-10 05:08:54 +0000461// Tests that MatcherCast<T>(m) works when m is a polymorphic matcher.
462TEST(MatcherCastTest, FromPolymorphicMatcher) {
463 Matcher<int> m = MatcherCast<int>(Eq(5));
464 EXPECT_TRUE(m.Matches(5));
465 EXPECT_FALSE(m.Matches(6));
466}
467
468// For testing casting matchers between compatible types.
469class IntValue {
470 public:
471 // An int can be statically (although not implicitly) cast to a
472 // IntValue.
zhanyong.wan32de5f52009-12-23 00:13:23 +0000473 explicit IntValue(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +0000474
475 int value() const { return value_; }
476 private:
477 int value_;
478};
479
480// For testing casting matchers between compatible types.
481bool IsPositiveIntValue(const IntValue& foo) {
482 return foo.value() > 0;
483}
484
485// Tests that MatcherCast<T>(m) works when m is a Matcher<U> where T
486// can be statically converted to U.
487TEST(MatcherCastTest, FromCompatibleType) {
488 Matcher<double> m1 = Eq(2.0);
489 Matcher<int> m2 = MatcherCast<int>(m1);
490 EXPECT_TRUE(m2.Matches(2));
491 EXPECT_FALSE(m2.Matches(3));
492
493 Matcher<IntValue> m3 = Truly(IsPositiveIntValue);
494 Matcher<int> m4 = MatcherCast<int>(m3);
495 // In the following, the arguments 1 and 0 are statically converted
496 // to IntValue objects, and then tested by the IsPositiveIntValue()
497 // predicate.
498 EXPECT_TRUE(m4.Matches(1));
499 EXPECT_FALSE(m4.Matches(0));
500}
501
502// Tests that MatcherCast<T>(m) works when m is a Matcher<const T&>.
503TEST(MatcherCastTest, FromConstReferenceToNonReference) {
504 Matcher<const int&> m1 = Eq(0);
505 Matcher<int> m2 = MatcherCast<int>(m1);
506 EXPECT_TRUE(m2.Matches(0));
507 EXPECT_FALSE(m2.Matches(1));
508}
509
510// Tests that MatcherCast<T>(m) works when m is a Matcher<T&>.
511TEST(MatcherCastTest, FromReferenceToNonReference) {
512 Matcher<int&> m1 = Eq(0);
513 Matcher<int> m2 = MatcherCast<int>(m1);
514 EXPECT_TRUE(m2.Matches(0));
515 EXPECT_FALSE(m2.Matches(1));
516}
517
518// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
519TEST(MatcherCastTest, FromNonReferenceToConstReference) {
520 Matcher<int> m1 = Eq(0);
521 Matcher<const int&> m2 = MatcherCast<const int&>(m1);
522 EXPECT_TRUE(m2.Matches(0));
523 EXPECT_FALSE(m2.Matches(1));
524}
525
526// Tests that MatcherCast<T&>(m) works when m is a Matcher<T>.
527TEST(MatcherCastTest, FromNonReferenceToReference) {
528 Matcher<int> m1 = Eq(0);
529 Matcher<int&> m2 = MatcherCast<int&>(m1);
530 int n = 0;
531 EXPECT_TRUE(m2.Matches(n));
532 n = 1;
533 EXPECT_FALSE(m2.Matches(n));
534}
535
536// Tests that MatcherCast<T>(m) works when m is a Matcher<T>.
537TEST(MatcherCastTest, FromSameType) {
538 Matcher<int> m1 = Eq(0);
539 Matcher<int> m2 = MatcherCast<int>(m1);
540 EXPECT_TRUE(m2.Matches(0));
541 EXPECT_FALSE(m2.Matches(1));
542}
543
zhanyong.wan18490652009-05-11 18:54:08 +0000544class Base {};
545class Derived : public Base {};
546
547// Tests that SafeMatcherCast<T>(m) works when m is a polymorphic matcher.
548TEST(SafeMatcherCastTest, FromPolymorphicMatcher) {
549 Matcher<char> m2 = SafeMatcherCast<char>(Eq(32));
550 EXPECT_TRUE(m2.Matches(' '));
551 EXPECT_FALSE(m2.Matches('\n'));
552}
553
zhanyong.wan16cf4732009-05-14 20:55:30 +0000554// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where
555// T and U are arithmetic types and T can be losslessly converted to
556// U.
557TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) {
zhanyong.wan18490652009-05-11 18:54:08 +0000558 Matcher<double> m1 = DoubleEq(1.0);
zhanyong.wan16cf4732009-05-14 20:55:30 +0000559 Matcher<float> m2 = SafeMatcherCast<float>(m1);
560 EXPECT_TRUE(m2.Matches(1.0f));
561 EXPECT_FALSE(m2.Matches(2.0f));
562
563 Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>('a'));
564 EXPECT_TRUE(m3.Matches('a'));
565 EXPECT_FALSE(m3.Matches('b'));
zhanyong.wan18490652009-05-11 18:54:08 +0000566}
567
568// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where T and U
569// are pointers or references to a derived and a base class, correspondingly.
570TEST(SafeMatcherCastTest, FromBaseClass) {
571 Derived d, d2;
572 Matcher<Base*> m1 = Eq(&d);
573 Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1);
574 EXPECT_TRUE(m2.Matches(&d));
575 EXPECT_FALSE(m2.Matches(&d2));
576
577 Matcher<Base&> m3 = Ref(d);
578 Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3);
579 EXPECT_TRUE(m4.Matches(d));
580 EXPECT_FALSE(m4.Matches(d2));
581}
582
583// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<const T&>.
584TEST(SafeMatcherCastTest, FromConstReferenceToReference) {
585 int n = 0;
586 Matcher<const int&> m1 = Ref(n);
587 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
588 int n1 = 0;
589 EXPECT_TRUE(m2.Matches(n));
590 EXPECT_FALSE(m2.Matches(n1));
591}
592
593// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
594TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) {
595 Matcher<int> m1 = Eq(0);
596 Matcher<const int&> m2 = SafeMatcherCast<const int&>(m1);
597 EXPECT_TRUE(m2.Matches(0));
598 EXPECT_FALSE(m2.Matches(1));
599}
600
601// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<T>.
602TEST(SafeMatcherCastTest, FromNonReferenceToReference) {
603 Matcher<int> m1 = Eq(0);
604 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
605 int n = 0;
606 EXPECT_TRUE(m2.Matches(n));
607 n = 1;
608 EXPECT_FALSE(m2.Matches(n));
609}
610
611// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<T>.
612TEST(SafeMatcherCastTest, FromSameType) {
613 Matcher<int> m1 = Eq(0);
614 Matcher<int> m2 = SafeMatcherCast<int>(m1);
615 EXPECT_TRUE(m2.Matches(0));
616 EXPECT_FALSE(m2.Matches(1));
617}
618
shiqiane35fdd92008-12-10 05:08:54 +0000619// Tests that A<T>() matches any value of type T.
620TEST(ATest, MatchesAnyValue) {
621 // Tests a matcher for a value type.
622 Matcher<double> m1 = A<double>();
623 EXPECT_TRUE(m1.Matches(91.43));
624 EXPECT_TRUE(m1.Matches(-15.32));
625
626 // Tests a matcher for a reference type.
627 int a = 2;
628 int b = -6;
629 Matcher<int&> m2 = A<int&>();
630 EXPECT_TRUE(m2.Matches(a));
631 EXPECT_TRUE(m2.Matches(b));
632}
633
634// Tests that A<T>() describes itself properly.
635TEST(ATest, CanDescribeSelf) {
636 EXPECT_EQ("is anything", Describe(A<bool>()));
637}
638
639// Tests that An<T>() matches any value of type T.
640TEST(AnTest, MatchesAnyValue) {
641 // Tests a matcher for a value type.
642 Matcher<int> m1 = An<int>();
643 EXPECT_TRUE(m1.Matches(9143));
644 EXPECT_TRUE(m1.Matches(-1532));
645
646 // Tests a matcher for a reference type.
647 int a = 2;
648 int b = -6;
649 Matcher<int&> m2 = An<int&>();
650 EXPECT_TRUE(m2.Matches(a));
651 EXPECT_TRUE(m2.Matches(b));
652}
653
654// Tests that An<T>() describes itself properly.
655TEST(AnTest, CanDescribeSelf) {
656 EXPECT_EQ("is anything", Describe(An<int>()));
657}
658
659// Tests that _ can be used as a matcher for any type and matches any
660// value of that type.
661TEST(UnderscoreTest, MatchesAnyValue) {
662 // Uses _ as a matcher for a value type.
663 Matcher<int> m1 = _;
664 EXPECT_TRUE(m1.Matches(123));
665 EXPECT_TRUE(m1.Matches(-242));
666
667 // Uses _ as a matcher for a reference type.
668 bool a = false;
669 const bool b = true;
670 Matcher<const bool&> m2 = _;
671 EXPECT_TRUE(m2.Matches(a));
672 EXPECT_TRUE(m2.Matches(b));
673}
674
675// Tests that _ describes itself properly.
676TEST(UnderscoreTest, CanDescribeSelf) {
677 Matcher<int> m = _;
678 EXPECT_EQ("is anything", Describe(m));
679}
680
681// Tests that Eq(x) matches any value equal to x.
682TEST(EqTest, MatchesEqualValue) {
683 // 2 C-strings with same content but different addresses.
684 const char a1[] = "hi";
685 const char a2[] = "hi";
686
687 Matcher<const char*> m1 = Eq(a1);
688 EXPECT_TRUE(m1.Matches(a1));
689 EXPECT_FALSE(m1.Matches(a2));
690}
691
692// Tests that Eq(v) describes itself properly.
693
694class Unprintable {
695 public:
696 Unprintable() : c_('a') {}
697
zhanyong.wan32de5f52009-12-23 00:13:23 +0000698 bool operator==(const Unprintable& /* rhs */) { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000699 private:
700 char c_;
701};
702
703TEST(EqTest, CanDescribeSelf) {
704 Matcher<Unprintable> m = Eq(Unprintable());
705 EXPECT_EQ("is equal to 1-byte object <61>", Describe(m));
706}
707
708// Tests that Eq(v) can be used to match any type that supports
709// comparing with type T, where T is v's type.
710TEST(EqTest, IsPolymorphic) {
711 Matcher<int> m1 = Eq(1);
712 EXPECT_TRUE(m1.Matches(1));
713 EXPECT_FALSE(m1.Matches(2));
714
715 Matcher<char> m2 = Eq(1);
716 EXPECT_TRUE(m2.Matches('\1'));
717 EXPECT_FALSE(m2.Matches('a'));
718}
719
720// Tests that TypedEq<T>(v) matches values of type T that's equal to v.
721TEST(TypedEqTest, ChecksEqualityForGivenType) {
722 Matcher<char> m1 = TypedEq<char>('a');
723 EXPECT_TRUE(m1.Matches('a'));
724 EXPECT_FALSE(m1.Matches('b'));
725
726 Matcher<int> m2 = TypedEq<int>(6);
727 EXPECT_TRUE(m2.Matches(6));
728 EXPECT_FALSE(m2.Matches(7));
729}
730
731// Tests that TypedEq(v) describes itself properly.
732TEST(TypedEqTest, CanDescribeSelf) {
733 EXPECT_EQ("is equal to 2", Describe(TypedEq<int>(2)));
734}
735
736// Tests that TypedEq<T>(v) has type Matcher<T>.
737
738// Type<T>::IsTypeOf(v) compiles iff the type of value v is T, where T
739// is a "bare" type (i.e. not in the form of const U or U&). If v's
740// type is not T, the compiler will generate a message about
741// "undefined referece".
742template <typename T>
743struct Type {
zhanyong.wan32de5f52009-12-23 00:13:23 +0000744 static bool IsTypeOf(const T& /* v */) { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000745
746 template <typename T2>
747 static void IsTypeOf(T2 v);
748};
749
750TEST(TypedEqTest, HasSpecifiedType) {
751 // Verfies that the type of TypedEq<T>(v) is Matcher<T>.
752 Type<Matcher<int> >::IsTypeOf(TypedEq<int>(5));
753 Type<Matcher<double> >::IsTypeOf(TypedEq<double>(5));
754}
755
756// Tests that Ge(v) matches anything >= v.
757TEST(GeTest, ImplementsGreaterThanOrEqual) {
758 Matcher<int> m1 = Ge(0);
759 EXPECT_TRUE(m1.Matches(1));
760 EXPECT_TRUE(m1.Matches(0));
761 EXPECT_FALSE(m1.Matches(-1));
762}
763
764// Tests that Ge(v) describes itself properly.
765TEST(GeTest, CanDescribeSelf) {
766 Matcher<int> m = Ge(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000767 EXPECT_EQ("is >= 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000768}
769
770// Tests that Gt(v) matches anything > v.
771TEST(GtTest, ImplementsGreaterThan) {
772 Matcher<double> m1 = Gt(0);
773 EXPECT_TRUE(m1.Matches(1.0));
774 EXPECT_FALSE(m1.Matches(0.0));
775 EXPECT_FALSE(m1.Matches(-1.0));
776}
777
778// Tests that Gt(v) describes itself properly.
779TEST(GtTest, CanDescribeSelf) {
780 Matcher<int> m = Gt(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000781 EXPECT_EQ("is > 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000782}
783
784// Tests that Le(v) matches anything <= v.
785TEST(LeTest, ImplementsLessThanOrEqual) {
786 Matcher<char> m1 = Le('b');
787 EXPECT_TRUE(m1.Matches('a'));
788 EXPECT_TRUE(m1.Matches('b'));
789 EXPECT_FALSE(m1.Matches('c'));
790}
791
792// Tests that Le(v) describes itself properly.
793TEST(LeTest, CanDescribeSelf) {
794 Matcher<int> m = Le(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000795 EXPECT_EQ("is <= 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000796}
797
798// Tests that Lt(v) matches anything < v.
799TEST(LtTest, ImplementsLessThan) {
800 Matcher<const string&> m1 = Lt("Hello");
801 EXPECT_TRUE(m1.Matches("Abc"));
802 EXPECT_FALSE(m1.Matches("Hello"));
803 EXPECT_FALSE(m1.Matches("Hello, world!"));
804}
805
806// Tests that Lt(v) describes itself properly.
807TEST(LtTest, CanDescribeSelf) {
808 Matcher<int> m = Lt(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000809 EXPECT_EQ("is < 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000810}
811
812// Tests that Ne(v) matches anything != v.
813TEST(NeTest, ImplementsNotEqual) {
814 Matcher<int> m1 = Ne(0);
815 EXPECT_TRUE(m1.Matches(1));
816 EXPECT_TRUE(m1.Matches(-1));
817 EXPECT_FALSE(m1.Matches(0));
818}
819
820// Tests that Ne(v) describes itself properly.
821TEST(NeTest, CanDescribeSelf) {
822 Matcher<int> m = Ne(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000823 EXPECT_EQ("isn't equal to 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000824}
825
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000826// Tests that IsNull() matches any NULL pointer of any type.
827TEST(IsNullTest, MatchesNullPointer) {
828 Matcher<int*> m1 = IsNull();
829 int* p1 = NULL;
830 int n = 0;
831 EXPECT_TRUE(m1.Matches(p1));
832 EXPECT_FALSE(m1.Matches(&n));
833
834 Matcher<const char*> m2 = IsNull();
835 const char* p2 = NULL;
836 EXPECT_TRUE(m2.Matches(p2));
837 EXPECT_FALSE(m2.Matches("hi"));
838
zhanyong.wan95b12332009-09-25 18:55:50 +0000839#if !GTEST_OS_SYMBIAN
840 // Nokia's Symbian compiler generates:
841 // gmock-matchers.h: ambiguous access to overloaded function
842 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(void *)'
843 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(const testing::
844 // MatcherInterface<void *> *)'
845 // gmock-matchers.h: (point of instantiation: 'testing::
846 // gmock_matchers_test::IsNullTest_MatchesNullPointer_Test::TestBody()')
847 // gmock-matchers.h: (instantiating: 'testing::PolymorphicMatc
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000848 Matcher<void*> m3 = IsNull();
849 void* p3 = NULL;
850 EXPECT_TRUE(m3.Matches(p3));
851 EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef)));
zhanyong.wan95b12332009-09-25 18:55:50 +0000852#endif
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000853}
854
vladlosev79b83502009-11-18 00:43:37 +0000855TEST(IsNullTest, LinkedPtr) {
856 const Matcher<linked_ptr<int> > m = IsNull();
857 const linked_ptr<int> null_p;
858 const linked_ptr<int> non_null_p(new int);
859
860 EXPECT_TRUE(m.Matches(null_p));
861 EXPECT_FALSE(m.Matches(non_null_p));
862}
863
864TEST(IsNullTest, ReferenceToConstLinkedPtr) {
865 const Matcher<const linked_ptr<double>&> m = IsNull();
866 const linked_ptr<double> null_p;
867 const linked_ptr<double> non_null_p(new double);
868
869 EXPECT_TRUE(m.Matches(null_p));
870 EXPECT_FALSE(m.Matches(non_null_p));
871}
872
vladloseve56daa72009-11-18 01:08:08 +0000873TEST(IsNullTest, ReferenceToConstScopedPtr) {
874 const Matcher<const scoped_ptr<double>&> m = IsNull();
875 const scoped_ptr<double> null_p;
876 const scoped_ptr<double> non_null_p(new double);
877
878 EXPECT_TRUE(m.Matches(null_p));
879 EXPECT_FALSE(m.Matches(non_null_p));
880}
881
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000882// Tests that IsNull() describes itself properly.
883TEST(IsNullTest, CanDescribeSelf) {
884 Matcher<int*> m = IsNull();
885 EXPECT_EQ("is NULL", Describe(m));
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000886 EXPECT_EQ("isn't NULL", DescribeNegation(m));
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000887}
888
shiqiane35fdd92008-12-10 05:08:54 +0000889// Tests that NotNull() matches any non-NULL pointer of any type.
890TEST(NotNullTest, MatchesNonNullPointer) {
891 Matcher<int*> m1 = NotNull();
892 int* p1 = NULL;
893 int n = 0;
894 EXPECT_FALSE(m1.Matches(p1));
895 EXPECT_TRUE(m1.Matches(&n));
896
897 Matcher<const char*> m2 = NotNull();
898 const char* p2 = NULL;
899 EXPECT_FALSE(m2.Matches(p2));
900 EXPECT_TRUE(m2.Matches("hi"));
901}
902
vladlosev79b83502009-11-18 00:43:37 +0000903TEST(NotNullTest, LinkedPtr) {
904 const Matcher<linked_ptr<int> > m = NotNull();
905 const linked_ptr<int> null_p;
906 const linked_ptr<int> non_null_p(new int);
907
908 EXPECT_FALSE(m.Matches(null_p));
909 EXPECT_TRUE(m.Matches(non_null_p));
910}
911
912TEST(NotNullTest, ReferenceToConstLinkedPtr) {
913 const Matcher<const linked_ptr<double>&> m = NotNull();
914 const linked_ptr<double> null_p;
915 const linked_ptr<double> non_null_p(new double);
916
917 EXPECT_FALSE(m.Matches(null_p));
918 EXPECT_TRUE(m.Matches(non_null_p));
919}
920
vladloseve56daa72009-11-18 01:08:08 +0000921TEST(NotNullTest, ReferenceToConstScopedPtr) {
922 const Matcher<const scoped_ptr<double>&> m = NotNull();
923 const scoped_ptr<double> null_p;
924 const scoped_ptr<double> non_null_p(new double);
925
926 EXPECT_FALSE(m.Matches(null_p));
927 EXPECT_TRUE(m.Matches(non_null_p));
928}
929
shiqiane35fdd92008-12-10 05:08:54 +0000930// Tests that NotNull() describes itself properly.
931TEST(NotNullTest, CanDescribeSelf) {
932 Matcher<int*> m = NotNull();
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000933 EXPECT_EQ("isn't NULL", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000934}
935
936// Tests that Ref(variable) matches an argument that references
937// 'variable'.
938TEST(RefTest, MatchesSameVariable) {
939 int a = 0;
940 int b = 0;
941 Matcher<int&> m = Ref(a);
942 EXPECT_TRUE(m.Matches(a));
943 EXPECT_FALSE(m.Matches(b));
944}
945
946// Tests that Ref(variable) describes itself properly.
947TEST(RefTest, CanDescribeSelf) {
948 int n = 5;
949 Matcher<int&> m = Ref(n);
950 stringstream ss;
951 ss << "references the variable @" << &n << " 5";
952 EXPECT_EQ(string(ss.str()), Describe(m));
953}
954
955// Test that Ref(non_const_varialbe) can be used as a matcher for a
956// const reference.
957TEST(RefTest, CanBeUsedAsMatcherForConstReference) {
958 int a = 0;
959 int b = 0;
960 Matcher<const int&> m = Ref(a);
961 EXPECT_TRUE(m.Matches(a));
962 EXPECT_FALSE(m.Matches(b));
963}
964
965// Tests that Ref(variable) is covariant, i.e. Ref(derived) can be
966// used wherever Ref(base) can be used (Ref(derived) is a sub-type
967// of Ref(base), but not vice versa.
968
shiqiane35fdd92008-12-10 05:08:54 +0000969TEST(RefTest, IsCovariant) {
970 Base base, base2;
971 Derived derived;
972 Matcher<const Base&> m1 = Ref(base);
973 EXPECT_TRUE(m1.Matches(base));
974 EXPECT_FALSE(m1.Matches(base2));
975 EXPECT_FALSE(m1.Matches(derived));
976
977 m1 = Ref(derived);
978 EXPECT_TRUE(m1.Matches(derived));
979 EXPECT_FALSE(m1.Matches(base));
980 EXPECT_FALSE(m1.Matches(base2));
981}
982
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000983TEST(RefTest, ExplainsResult) {
984 int n = 0;
985 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), n),
986 StartsWith("which is located @"));
987
988 int m = 0;
989 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), m),
990 StartsWith("which is located @"));
991}
992
shiqiane35fdd92008-12-10 05:08:54 +0000993// Tests string comparison matchers.
994
995TEST(StrEqTest, MatchesEqualString) {
996 Matcher<const char*> m = StrEq(string("Hello"));
997 EXPECT_TRUE(m.Matches("Hello"));
998 EXPECT_FALSE(m.Matches("hello"));
999 EXPECT_FALSE(m.Matches(NULL));
1000
1001 Matcher<const string&> m2 = StrEq("Hello");
1002 EXPECT_TRUE(m2.Matches("Hello"));
1003 EXPECT_FALSE(m2.Matches("Hi"));
1004}
1005
1006TEST(StrEqTest, CanDescribeSelf) {
vladlosevaa432202011-04-01 21:58:42 +00001007 Matcher<string> m = StrEq("Hi-\'\"?\\\a\b\f\n\r\t\v\xD3");
1008 EXPECT_EQ("is equal to \"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",
shiqiane35fdd92008-12-10 05:08:54 +00001009 Describe(m));
1010
1011 string str("01204500800");
1012 str[3] = '\0';
1013 Matcher<string> m2 = StrEq(str);
1014 EXPECT_EQ("is equal to \"012\\04500800\"", Describe(m2));
1015 str[0] = str[6] = str[7] = str[9] = str[10] = '\0';
1016 Matcher<string> m3 = StrEq(str);
1017 EXPECT_EQ("is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3));
1018}
1019
1020TEST(StrNeTest, MatchesUnequalString) {
1021 Matcher<const char*> m = StrNe("Hello");
1022 EXPECT_TRUE(m.Matches(""));
1023 EXPECT_TRUE(m.Matches(NULL));
1024 EXPECT_FALSE(m.Matches("Hello"));
1025
1026 Matcher<string> m2 = StrNe(string("Hello"));
1027 EXPECT_TRUE(m2.Matches("hello"));
1028 EXPECT_FALSE(m2.Matches("Hello"));
1029}
1030
1031TEST(StrNeTest, CanDescribeSelf) {
1032 Matcher<const char*> m = StrNe("Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001033 EXPECT_EQ("isn't equal to \"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001034}
1035
1036TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
1037 Matcher<const char*> m = StrCaseEq(string("Hello"));
1038 EXPECT_TRUE(m.Matches("Hello"));
1039 EXPECT_TRUE(m.Matches("hello"));
1040 EXPECT_FALSE(m.Matches("Hi"));
1041 EXPECT_FALSE(m.Matches(NULL));
1042
1043 Matcher<const string&> m2 = StrCaseEq("Hello");
1044 EXPECT_TRUE(m2.Matches("hello"));
1045 EXPECT_FALSE(m2.Matches("Hi"));
1046}
1047
1048TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1049 string str1("oabocdooeoo");
1050 string str2("OABOCDOOEOO");
1051 Matcher<const string&> m0 = StrCaseEq(str1);
1052 EXPECT_FALSE(m0.Matches(str2 + string(1, '\0')));
1053
1054 str1[3] = str2[3] = '\0';
1055 Matcher<const string&> m1 = StrCaseEq(str1);
1056 EXPECT_TRUE(m1.Matches(str2));
1057
1058 str1[0] = str1[6] = str1[7] = str1[10] = '\0';
1059 str2[0] = str2[6] = str2[7] = str2[10] = '\0';
1060 Matcher<const string&> m2 = StrCaseEq(str1);
1061 str1[9] = str2[9] = '\0';
1062 EXPECT_FALSE(m2.Matches(str2));
1063
1064 Matcher<const string&> m3 = StrCaseEq(str1);
1065 EXPECT_TRUE(m3.Matches(str2));
1066
1067 EXPECT_FALSE(m3.Matches(str2 + "x"));
1068 str2.append(1, '\0');
1069 EXPECT_FALSE(m3.Matches(str2));
1070 EXPECT_FALSE(m3.Matches(string(str2, 0, 9)));
1071}
1072
1073TEST(StrCaseEqTest, CanDescribeSelf) {
1074 Matcher<string> m = StrCaseEq("Hi");
1075 EXPECT_EQ("is equal to (ignoring case) \"Hi\"", Describe(m));
1076}
1077
1078TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1079 Matcher<const char*> m = StrCaseNe("Hello");
1080 EXPECT_TRUE(m.Matches("Hi"));
1081 EXPECT_TRUE(m.Matches(NULL));
1082 EXPECT_FALSE(m.Matches("Hello"));
1083 EXPECT_FALSE(m.Matches("hello"));
1084
1085 Matcher<string> m2 = StrCaseNe(string("Hello"));
1086 EXPECT_TRUE(m2.Matches(""));
1087 EXPECT_FALSE(m2.Matches("Hello"));
1088}
1089
1090TEST(StrCaseNeTest, CanDescribeSelf) {
1091 Matcher<const char*> m = StrCaseNe("Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001092 EXPECT_EQ("isn't equal to (ignoring case) \"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001093}
1094
1095// Tests that HasSubstr() works for matching string-typed values.
1096TEST(HasSubstrTest, WorksForStringClasses) {
1097 const Matcher<string> m1 = HasSubstr("foo");
1098 EXPECT_TRUE(m1.Matches(string("I love food.")));
1099 EXPECT_FALSE(m1.Matches(string("tofo")));
1100
1101 const Matcher<const std::string&> m2 = HasSubstr("foo");
1102 EXPECT_TRUE(m2.Matches(std::string("I love food.")));
1103 EXPECT_FALSE(m2.Matches(std::string("tofo")));
1104}
1105
1106// Tests that HasSubstr() works for matching C-string-typed values.
1107TEST(HasSubstrTest, WorksForCStrings) {
1108 const Matcher<char*> m1 = HasSubstr("foo");
1109 EXPECT_TRUE(m1.Matches(const_cast<char*>("I love food.")));
1110 EXPECT_FALSE(m1.Matches(const_cast<char*>("tofo")));
1111 EXPECT_FALSE(m1.Matches(NULL));
1112
1113 const Matcher<const char*> m2 = HasSubstr("foo");
1114 EXPECT_TRUE(m2.Matches("I love food."));
1115 EXPECT_FALSE(m2.Matches("tofo"));
1116 EXPECT_FALSE(m2.Matches(NULL));
1117}
1118
1119// Tests that HasSubstr(s) describes itself properly.
1120TEST(HasSubstrTest, CanDescribeSelf) {
1121 Matcher<string> m = HasSubstr("foo\n\"");
1122 EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m));
1123}
1124
zhanyong.wanb5937da2009-07-16 20:26:41 +00001125TEST(KeyTest, CanDescribeSelf) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001126 Matcher<const pair<std::string, int>&> m = Key("foo");
zhanyong.wanb5937da2009-07-16 20:26:41 +00001127 EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001128 EXPECT_EQ("doesn't have a key that is equal to \"foo\"", DescribeNegation(m));
1129}
1130
1131TEST(KeyTest, ExplainsResult) {
1132 Matcher<pair<int, bool> > m = Key(GreaterThan(10));
1133 EXPECT_EQ("whose first field is a value which is 5 less than 10",
1134 Explain(m, make_pair(5, true)));
1135 EXPECT_EQ("whose first field is a value which is 5 more than 10",
1136 Explain(m, make_pair(15, true)));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001137}
1138
1139TEST(KeyTest, MatchesCorrectly) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001140 pair<int, std::string> p(25, "foo");
zhanyong.wanb5937da2009-07-16 20:26:41 +00001141 EXPECT_THAT(p, Key(25));
1142 EXPECT_THAT(p, Not(Key(42)));
1143 EXPECT_THAT(p, Key(Ge(20)));
1144 EXPECT_THAT(p, Not(Key(Lt(25))));
1145}
1146
1147TEST(KeyTest, SafelyCastsInnerMatcher) {
1148 Matcher<int> is_positive = Gt(0);
1149 Matcher<int> is_negative = Lt(0);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001150 pair<char, bool> p('a', true);
zhanyong.wanb5937da2009-07-16 20:26:41 +00001151 EXPECT_THAT(p, Key(is_positive));
1152 EXPECT_THAT(p, Not(Key(is_negative)));
1153}
1154
1155TEST(KeyTest, InsideContainsUsingMap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001156 map<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001157 container.insert(make_pair(1, 'a'));
1158 container.insert(make_pair(2, 'b'));
1159 container.insert(make_pair(4, 'c'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001160 EXPECT_THAT(container, Contains(Key(1)));
1161 EXPECT_THAT(container, Not(Contains(Key(3))));
1162}
1163
1164TEST(KeyTest, InsideContainsUsingMultimap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001165 multimap<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001166 container.insert(make_pair(1, 'a'));
1167 container.insert(make_pair(2, 'b'));
1168 container.insert(make_pair(4, 'c'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001169
1170 EXPECT_THAT(container, Not(Contains(Key(25))));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001171 container.insert(make_pair(25, 'd'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001172 EXPECT_THAT(container, Contains(Key(25)));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001173 container.insert(make_pair(25, 'e'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001174 EXPECT_THAT(container, Contains(Key(25)));
1175
1176 EXPECT_THAT(container, Contains(Key(1)));
1177 EXPECT_THAT(container, Not(Contains(Key(3))));
1178}
1179
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001180TEST(PairTest, Typing) {
1181 // Test verifies the following type conversions can be compiled.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001182 Matcher<const pair<const char*, int>&> m1 = Pair("foo", 42);
1183 Matcher<const pair<const char*, int> > m2 = Pair("foo", 42);
1184 Matcher<pair<const char*, int> > m3 = Pair("foo", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001185
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001186 Matcher<pair<int, const std::string> > m4 = Pair(25, "42");
1187 Matcher<pair<const std::string, int> > m5 = Pair("25", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001188}
1189
1190TEST(PairTest, CanDescribeSelf) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001191 Matcher<const pair<std::string, int>&> m1 = Pair("foo", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001192 EXPECT_EQ("has a first field that is equal to \"foo\""
1193 ", and has a second field that is equal to 42",
1194 Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001195 EXPECT_EQ("has a first field that isn't equal to \"foo\""
1196 ", or has a second field that isn't equal to 42",
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001197 DescribeNegation(m1));
1198 // Double and triple negation (1 or 2 times not and description of negation).
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001199 Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42));
1200 EXPECT_EQ("has a first field that isn't equal to 13"
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001201 ", and has a second field that is equal to 42",
1202 DescribeNegation(m2));
1203}
1204
1205TEST(PairTest, CanExplainMatchResultTo) {
zhanyong.wan82113312010-01-08 21:55:40 +00001206 // If neither field matches, Pair() should explain about the first
1207 // field.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001208 const Matcher<pair<int, int> > m = Pair(GreaterThan(0), GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001209 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001210 Explain(m, make_pair(-1, -2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001211
zhanyong.wan82113312010-01-08 21:55:40 +00001212 // If the first field matches but the second doesn't, Pair() should
1213 // explain about the second field.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001214 EXPECT_EQ("whose second field does not match, which is 2 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001215 Explain(m, make_pair(1, -2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001216
zhanyong.wan82113312010-01-08 21:55:40 +00001217 // If the first field doesn't match but the second does, Pair()
1218 // should explain about the first field.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001219 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001220 Explain(m, make_pair(-1, 2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001221
zhanyong.wan82113312010-01-08 21:55:40 +00001222 // If both fields match, Pair() should explain about them both.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001223 EXPECT_EQ("whose both fields match, where the first field is a value "
1224 "which is 1 more than 0, and the second field is a value "
1225 "which is 2 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001226 Explain(m, make_pair(1, 2)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001227
1228 // If only the first match has an explanation, only this explanation should
1229 // be printed.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001230 const Matcher<pair<int, int> > explain_first = Pair(GreaterThan(0), 0);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001231 EXPECT_EQ("whose both fields match, where the first field is a value "
1232 "which is 1 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001233 Explain(explain_first, make_pair(1, 0)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001234
1235 // If only the second match has an explanation, only this explanation should
1236 // be printed.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001237 const Matcher<pair<int, int> > explain_second = Pair(0, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001238 EXPECT_EQ("whose both fields match, where the second field is a value "
1239 "which is 1 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001240 Explain(explain_second, make_pair(0, 1)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001241}
1242
1243TEST(PairTest, MatchesCorrectly) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001244 pair<int, std::string> p(25, "foo");
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001245
1246 // Both fields match.
1247 EXPECT_THAT(p, Pair(25, "foo"));
1248 EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o")));
1249
1250 // 'first' doesnt' match, but 'second' matches.
1251 EXPECT_THAT(p, Not(Pair(42, "foo")));
1252 EXPECT_THAT(p, Not(Pair(Lt(25), "foo")));
1253
1254 // 'first' matches, but 'second' doesn't match.
1255 EXPECT_THAT(p, Not(Pair(25, "bar")));
1256 EXPECT_THAT(p, Not(Pair(25, Not("foo"))));
1257
1258 // Neither field matches.
1259 EXPECT_THAT(p, Not(Pair(13, "bar")));
1260 EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a"))));
1261}
1262
1263TEST(PairTest, SafelyCastsInnerMatchers) {
1264 Matcher<int> is_positive = Gt(0);
1265 Matcher<int> is_negative = Lt(0);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001266 pair<char, bool> p('a', true);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001267 EXPECT_THAT(p, Pair(is_positive, _));
1268 EXPECT_THAT(p, Not(Pair(is_negative, _)));
1269 EXPECT_THAT(p, Pair(_, is_positive));
1270 EXPECT_THAT(p, Not(Pair(_, is_negative)));
1271}
1272
1273TEST(PairTest, InsideContainsUsingMap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001274 map<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001275 container.insert(make_pair(1, 'a'));
1276 container.insert(make_pair(2, 'b'));
1277 container.insert(make_pair(4, 'c'));
zhanyong.wan95b12332009-09-25 18:55:50 +00001278 EXPECT_THAT(container, Contains(Pair(1, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001279 EXPECT_THAT(container, Contains(Pair(1, _)));
zhanyong.wan95b12332009-09-25 18:55:50 +00001280 EXPECT_THAT(container, Contains(Pair(_, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001281 EXPECT_THAT(container, Not(Contains(Pair(3, _))));
1282}
1283
shiqiane35fdd92008-12-10 05:08:54 +00001284// Tests StartsWith(s).
1285
1286TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
1287 const Matcher<const char*> m1 = StartsWith(string(""));
1288 EXPECT_TRUE(m1.Matches("Hi"));
1289 EXPECT_TRUE(m1.Matches(""));
1290 EXPECT_FALSE(m1.Matches(NULL));
1291
1292 const Matcher<const string&> m2 = StartsWith("Hi");
1293 EXPECT_TRUE(m2.Matches("Hi"));
1294 EXPECT_TRUE(m2.Matches("Hi Hi!"));
1295 EXPECT_TRUE(m2.Matches("High"));
1296 EXPECT_FALSE(m2.Matches("H"));
1297 EXPECT_FALSE(m2.Matches(" Hi"));
1298}
1299
1300TEST(StartsWithTest, CanDescribeSelf) {
1301 Matcher<const std::string> m = StartsWith("Hi");
1302 EXPECT_EQ("starts with \"Hi\"", Describe(m));
1303}
1304
1305// Tests EndsWith(s).
1306
1307TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
1308 const Matcher<const char*> m1 = EndsWith("");
1309 EXPECT_TRUE(m1.Matches("Hi"));
1310 EXPECT_TRUE(m1.Matches(""));
1311 EXPECT_FALSE(m1.Matches(NULL));
1312
1313 const Matcher<const string&> m2 = EndsWith(string("Hi"));
1314 EXPECT_TRUE(m2.Matches("Hi"));
1315 EXPECT_TRUE(m2.Matches("Wow Hi Hi"));
1316 EXPECT_TRUE(m2.Matches("Super Hi"));
1317 EXPECT_FALSE(m2.Matches("i"));
1318 EXPECT_FALSE(m2.Matches("Hi "));
1319}
1320
1321TEST(EndsWithTest, CanDescribeSelf) {
1322 Matcher<const std::string> m = EndsWith("Hi");
1323 EXPECT_EQ("ends with \"Hi\"", Describe(m));
1324}
1325
shiqiane35fdd92008-12-10 05:08:54 +00001326// Tests MatchesRegex().
1327
1328TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
1329 const Matcher<const char*> m1 = MatchesRegex("a.*z");
1330 EXPECT_TRUE(m1.Matches("az"));
1331 EXPECT_TRUE(m1.Matches("abcz"));
1332 EXPECT_FALSE(m1.Matches(NULL));
1333
1334 const Matcher<const string&> m2 = MatchesRegex(new RE("a.*z"));
1335 EXPECT_TRUE(m2.Matches("azbz"));
1336 EXPECT_FALSE(m2.Matches("az1"));
1337 EXPECT_FALSE(m2.Matches("1az"));
1338}
1339
1340TEST(MatchesRegexTest, CanDescribeSelf) {
1341 Matcher<const std::string> m1 = MatchesRegex(string("Hi.*"));
1342 EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1));
1343
zhanyong.wand14aaed2010-01-14 05:36:32 +00001344 Matcher<const char*> m2 = MatchesRegex(new RE("a.*"));
1345 EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +00001346}
1347
1348// Tests ContainsRegex().
1349
1350TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
1351 const Matcher<const char*> m1 = ContainsRegex(string("a.*z"));
1352 EXPECT_TRUE(m1.Matches("az"));
1353 EXPECT_TRUE(m1.Matches("0abcz1"));
1354 EXPECT_FALSE(m1.Matches(NULL));
1355
1356 const Matcher<const string&> m2 = ContainsRegex(new RE("a.*z"));
1357 EXPECT_TRUE(m2.Matches("azbz"));
1358 EXPECT_TRUE(m2.Matches("az1"));
1359 EXPECT_FALSE(m2.Matches("1a"));
1360}
1361
1362TEST(ContainsRegexTest, CanDescribeSelf) {
1363 Matcher<const std::string> m1 = ContainsRegex("Hi.*");
1364 EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1));
1365
zhanyong.wand14aaed2010-01-14 05:36:32 +00001366 Matcher<const char*> m2 = ContainsRegex(new RE("a.*"));
1367 EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +00001368}
shiqiane35fdd92008-12-10 05:08:54 +00001369
1370// Tests for wide strings.
1371#if GTEST_HAS_STD_WSTRING
1372TEST(StdWideStrEqTest, MatchesEqual) {
1373 Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello"));
1374 EXPECT_TRUE(m.Matches(L"Hello"));
1375 EXPECT_FALSE(m.Matches(L"hello"));
1376 EXPECT_FALSE(m.Matches(NULL));
1377
1378 Matcher<const ::std::wstring&> m2 = StrEq(L"Hello");
1379 EXPECT_TRUE(m2.Matches(L"Hello"));
1380 EXPECT_FALSE(m2.Matches(L"Hi"));
1381
1382 Matcher<const ::std::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1383 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1384 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1385
1386 ::std::wstring str(L"01204500800");
1387 str[3] = L'\0';
1388 Matcher<const ::std::wstring&> m4 = StrEq(str);
1389 EXPECT_TRUE(m4.Matches(str));
1390 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1391 Matcher<const ::std::wstring&> m5 = StrEq(str);
1392 EXPECT_TRUE(m5.Matches(str));
1393}
1394
1395TEST(StdWideStrEqTest, CanDescribeSelf) {
vladlosevaa432202011-04-01 21:58:42 +00001396 Matcher< ::std::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v");
1397 EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
shiqiane35fdd92008-12-10 05:08:54 +00001398 Describe(m));
1399
1400 Matcher< ::std::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1401 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1402 Describe(m2));
1403
1404 ::std::wstring str(L"01204500800");
1405 str[3] = L'\0';
1406 Matcher<const ::std::wstring&> m4 = StrEq(str);
1407 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1408 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1409 Matcher<const ::std::wstring&> m5 = StrEq(str);
1410 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1411}
1412
1413TEST(StdWideStrNeTest, MatchesUnequalString) {
1414 Matcher<const wchar_t*> m = StrNe(L"Hello");
1415 EXPECT_TRUE(m.Matches(L""));
1416 EXPECT_TRUE(m.Matches(NULL));
1417 EXPECT_FALSE(m.Matches(L"Hello"));
1418
1419 Matcher< ::std::wstring> m2 = StrNe(::std::wstring(L"Hello"));
1420 EXPECT_TRUE(m2.Matches(L"hello"));
1421 EXPECT_FALSE(m2.Matches(L"Hello"));
1422}
1423
1424TEST(StdWideStrNeTest, CanDescribeSelf) {
1425 Matcher<const wchar_t*> m = StrNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001426 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001427}
1428
1429TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1430 Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello"));
1431 EXPECT_TRUE(m.Matches(L"Hello"));
1432 EXPECT_TRUE(m.Matches(L"hello"));
1433 EXPECT_FALSE(m.Matches(L"Hi"));
1434 EXPECT_FALSE(m.Matches(NULL));
1435
1436 Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello");
1437 EXPECT_TRUE(m2.Matches(L"hello"));
1438 EXPECT_FALSE(m2.Matches(L"Hi"));
1439}
1440
1441TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1442 ::std::wstring str1(L"oabocdooeoo");
1443 ::std::wstring str2(L"OABOCDOOEOO");
1444 Matcher<const ::std::wstring&> m0 = StrCaseEq(str1);
1445 EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L'\0')));
1446
1447 str1[3] = str2[3] = L'\0';
1448 Matcher<const ::std::wstring&> m1 = StrCaseEq(str1);
1449 EXPECT_TRUE(m1.Matches(str2));
1450
1451 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1452 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1453 Matcher<const ::std::wstring&> m2 = StrCaseEq(str1);
1454 str1[9] = str2[9] = L'\0';
1455 EXPECT_FALSE(m2.Matches(str2));
1456
1457 Matcher<const ::std::wstring&> m3 = StrCaseEq(str1);
1458 EXPECT_TRUE(m3.Matches(str2));
1459
1460 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1461 str2.append(1, L'\0');
1462 EXPECT_FALSE(m3.Matches(str2));
1463 EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9)));
1464}
1465
1466TEST(StdWideStrCaseEqTest, CanDescribeSelf) {
1467 Matcher< ::std::wstring> m = StrCaseEq(L"Hi");
1468 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1469}
1470
1471TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1472 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1473 EXPECT_TRUE(m.Matches(L"Hi"));
1474 EXPECT_TRUE(m.Matches(NULL));
1475 EXPECT_FALSE(m.Matches(L"Hello"));
1476 EXPECT_FALSE(m.Matches(L"hello"));
1477
1478 Matcher< ::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello"));
1479 EXPECT_TRUE(m2.Matches(L""));
1480 EXPECT_FALSE(m2.Matches(L"Hello"));
1481}
1482
1483TEST(StdWideStrCaseNeTest, CanDescribeSelf) {
1484 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001485 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001486}
1487
1488// Tests that HasSubstr() works for matching wstring-typed values.
1489TEST(StdWideHasSubstrTest, WorksForStringClasses) {
1490 const Matcher< ::std::wstring> m1 = HasSubstr(L"foo");
1491 EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food.")));
1492 EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo")));
1493
1494 const Matcher<const ::std::wstring&> m2 = HasSubstr(L"foo");
1495 EXPECT_TRUE(m2.Matches(::std::wstring(L"I love food.")));
1496 EXPECT_FALSE(m2.Matches(::std::wstring(L"tofo")));
1497}
1498
1499// Tests that HasSubstr() works for matching C-wide-string-typed values.
1500TEST(StdWideHasSubstrTest, WorksForCStrings) {
1501 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1502 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1503 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1504 EXPECT_FALSE(m1.Matches(NULL));
1505
1506 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1507 EXPECT_TRUE(m2.Matches(L"I love food."));
1508 EXPECT_FALSE(m2.Matches(L"tofo"));
1509 EXPECT_FALSE(m2.Matches(NULL));
1510}
1511
1512// Tests that HasSubstr(s) describes itself properly.
1513TEST(StdWideHasSubstrTest, CanDescribeSelf) {
1514 Matcher< ::std::wstring> m = HasSubstr(L"foo\n\"");
1515 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1516}
1517
1518// Tests StartsWith(s).
1519
1520TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) {
1521 const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L""));
1522 EXPECT_TRUE(m1.Matches(L"Hi"));
1523 EXPECT_TRUE(m1.Matches(L""));
1524 EXPECT_FALSE(m1.Matches(NULL));
1525
1526 const Matcher<const ::std::wstring&> m2 = StartsWith(L"Hi");
1527 EXPECT_TRUE(m2.Matches(L"Hi"));
1528 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1529 EXPECT_TRUE(m2.Matches(L"High"));
1530 EXPECT_FALSE(m2.Matches(L"H"));
1531 EXPECT_FALSE(m2.Matches(L" Hi"));
1532}
1533
1534TEST(StdWideStartsWithTest, CanDescribeSelf) {
1535 Matcher<const ::std::wstring> m = StartsWith(L"Hi");
1536 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1537}
1538
1539// Tests EndsWith(s).
1540
1541TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) {
1542 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1543 EXPECT_TRUE(m1.Matches(L"Hi"));
1544 EXPECT_TRUE(m1.Matches(L""));
1545 EXPECT_FALSE(m1.Matches(NULL));
1546
1547 const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L"Hi"));
1548 EXPECT_TRUE(m2.Matches(L"Hi"));
1549 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1550 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1551 EXPECT_FALSE(m2.Matches(L"i"));
1552 EXPECT_FALSE(m2.Matches(L"Hi "));
1553}
1554
1555TEST(StdWideEndsWithTest, CanDescribeSelf) {
1556 Matcher<const ::std::wstring> m = EndsWith(L"Hi");
1557 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1558}
1559
1560#endif // GTEST_HAS_STD_WSTRING
1561
1562#if GTEST_HAS_GLOBAL_WSTRING
1563TEST(GlobalWideStrEqTest, MatchesEqual) {
1564 Matcher<const wchar_t*> m = StrEq(::wstring(L"Hello"));
1565 EXPECT_TRUE(m.Matches(L"Hello"));
1566 EXPECT_FALSE(m.Matches(L"hello"));
1567 EXPECT_FALSE(m.Matches(NULL));
1568
1569 Matcher<const ::wstring&> m2 = StrEq(L"Hello");
1570 EXPECT_TRUE(m2.Matches(L"Hello"));
1571 EXPECT_FALSE(m2.Matches(L"Hi"));
1572
1573 Matcher<const ::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1574 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1575 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1576
1577 ::wstring str(L"01204500800");
1578 str[3] = L'\0';
1579 Matcher<const ::wstring&> m4 = StrEq(str);
1580 EXPECT_TRUE(m4.Matches(str));
1581 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1582 Matcher<const ::wstring&> m5 = StrEq(str);
1583 EXPECT_TRUE(m5.Matches(str));
1584}
1585
1586TEST(GlobalWideStrEqTest, CanDescribeSelf) {
vladlosevaa432202011-04-01 21:58:42 +00001587 Matcher< ::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v");
1588 EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
shiqiane35fdd92008-12-10 05:08:54 +00001589 Describe(m));
1590
1591 Matcher< ::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1592 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1593 Describe(m2));
1594
1595 ::wstring str(L"01204500800");
1596 str[3] = L'\0';
1597 Matcher<const ::wstring&> m4 = StrEq(str);
1598 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1599 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1600 Matcher<const ::wstring&> m5 = StrEq(str);
1601 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1602}
1603
1604TEST(GlobalWideStrNeTest, MatchesUnequalString) {
1605 Matcher<const wchar_t*> m = StrNe(L"Hello");
1606 EXPECT_TRUE(m.Matches(L""));
1607 EXPECT_TRUE(m.Matches(NULL));
1608 EXPECT_FALSE(m.Matches(L"Hello"));
1609
1610 Matcher< ::wstring> m2 = StrNe(::wstring(L"Hello"));
1611 EXPECT_TRUE(m2.Matches(L"hello"));
1612 EXPECT_FALSE(m2.Matches(L"Hello"));
1613}
1614
1615TEST(GlobalWideStrNeTest, CanDescribeSelf) {
1616 Matcher<const wchar_t*> m = StrNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001617 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001618}
1619
1620TEST(GlobalWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1621 Matcher<const wchar_t*> m = StrCaseEq(::wstring(L"Hello"));
1622 EXPECT_TRUE(m.Matches(L"Hello"));
1623 EXPECT_TRUE(m.Matches(L"hello"));
1624 EXPECT_FALSE(m.Matches(L"Hi"));
1625 EXPECT_FALSE(m.Matches(NULL));
1626
1627 Matcher<const ::wstring&> m2 = StrCaseEq(L"Hello");
1628 EXPECT_TRUE(m2.Matches(L"hello"));
1629 EXPECT_FALSE(m2.Matches(L"Hi"));
1630}
1631
1632TEST(GlobalWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1633 ::wstring str1(L"oabocdooeoo");
1634 ::wstring str2(L"OABOCDOOEOO");
1635 Matcher<const ::wstring&> m0 = StrCaseEq(str1);
1636 EXPECT_FALSE(m0.Matches(str2 + ::wstring(1, L'\0')));
1637
1638 str1[3] = str2[3] = L'\0';
1639 Matcher<const ::wstring&> m1 = StrCaseEq(str1);
1640 EXPECT_TRUE(m1.Matches(str2));
1641
1642 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1643 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1644 Matcher<const ::wstring&> m2 = StrCaseEq(str1);
1645 str1[9] = str2[9] = L'\0';
1646 EXPECT_FALSE(m2.Matches(str2));
1647
1648 Matcher<const ::wstring&> m3 = StrCaseEq(str1);
1649 EXPECT_TRUE(m3.Matches(str2));
1650
1651 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1652 str2.append(1, L'\0');
1653 EXPECT_FALSE(m3.Matches(str2));
1654 EXPECT_FALSE(m3.Matches(::wstring(str2, 0, 9)));
1655}
1656
1657TEST(GlobalWideStrCaseEqTest, CanDescribeSelf) {
1658 Matcher< ::wstring> m = StrCaseEq(L"Hi");
1659 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1660}
1661
1662TEST(GlobalWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1663 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1664 EXPECT_TRUE(m.Matches(L"Hi"));
1665 EXPECT_TRUE(m.Matches(NULL));
1666 EXPECT_FALSE(m.Matches(L"Hello"));
1667 EXPECT_FALSE(m.Matches(L"hello"));
1668
1669 Matcher< ::wstring> m2 = StrCaseNe(::wstring(L"Hello"));
1670 EXPECT_TRUE(m2.Matches(L""));
1671 EXPECT_FALSE(m2.Matches(L"Hello"));
1672}
1673
1674TEST(GlobalWideStrCaseNeTest, CanDescribeSelf) {
1675 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001676 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001677}
1678
1679// Tests that HasSubstr() works for matching wstring-typed values.
1680TEST(GlobalWideHasSubstrTest, WorksForStringClasses) {
1681 const Matcher< ::wstring> m1 = HasSubstr(L"foo");
1682 EXPECT_TRUE(m1.Matches(::wstring(L"I love food.")));
1683 EXPECT_FALSE(m1.Matches(::wstring(L"tofo")));
1684
1685 const Matcher<const ::wstring&> m2 = HasSubstr(L"foo");
1686 EXPECT_TRUE(m2.Matches(::wstring(L"I love food.")));
1687 EXPECT_FALSE(m2.Matches(::wstring(L"tofo")));
1688}
1689
1690// Tests that HasSubstr() works for matching C-wide-string-typed values.
1691TEST(GlobalWideHasSubstrTest, WorksForCStrings) {
1692 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1693 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1694 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1695 EXPECT_FALSE(m1.Matches(NULL));
1696
1697 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1698 EXPECT_TRUE(m2.Matches(L"I love food."));
1699 EXPECT_FALSE(m2.Matches(L"tofo"));
1700 EXPECT_FALSE(m2.Matches(NULL));
1701}
1702
1703// Tests that HasSubstr(s) describes itself properly.
1704TEST(GlobalWideHasSubstrTest, CanDescribeSelf) {
1705 Matcher< ::wstring> m = HasSubstr(L"foo\n\"");
1706 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1707}
1708
1709// Tests StartsWith(s).
1710
1711TEST(GlobalWideStartsWithTest, MatchesStringWithGivenPrefix) {
1712 const Matcher<const wchar_t*> m1 = StartsWith(::wstring(L""));
1713 EXPECT_TRUE(m1.Matches(L"Hi"));
1714 EXPECT_TRUE(m1.Matches(L""));
1715 EXPECT_FALSE(m1.Matches(NULL));
1716
1717 const Matcher<const ::wstring&> m2 = StartsWith(L"Hi");
1718 EXPECT_TRUE(m2.Matches(L"Hi"));
1719 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1720 EXPECT_TRUE(m2.Matches(L"High"));
1721 EXPECT_FALSE(m2.Matches(L"H"));
1722 EXPECT_FALSE(m2.Matches(L" Hi"));
1723}
1724
1725TEST(GlobalWideStartsWithTest, CanDescribeSelf) {
1726 Matcher<const ::wstring> m = StartsWith(L"Hi");
1727 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1728}
1729
1730// Tests EndsWith(s).
1731
1732TEST(GlobalWideEndsWithTest, MatchesStringWithGivenSuffix) {
1733 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1734 EXPECT_TRUE(m1.Matches(L"Hi"));
1735 EXPECT_TRUE(m1.Matches(L""));
1736 EXPECT_FALSE(m1.Matches(NULL));
1737
1738 const Matcher<const ::wstring&> m2 = EndsWith(::wstring(L"Hi"));
1739 EXPECT_TRUE(m2.Matches(L"Hi"));
1740 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1741 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1742 EXPECT_FALSE(m2.Matches(L"i"));
1743 EXPECT_FALSE(m2.Matches(L"Hi "));
1744}
1745
1746TEST(GlobalWideEndsWithTest, CanDescribeSelf) {
1747 Matcher<const ::wstring> m = EndsWith(L"Hi");
1748 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1749}
1750
1751#endif // GTEST_HAS_GLOBAL_WSTRING
1752
1753
1754typedef ::std::tr1::tuple<long, int> Tuple2; // NOLINT
1755
1756// Tests that Eq() matches a 2-tuple where the first field == the
1757// second field.
1758TEST(Eq2Test, MatchesEqualArguments) {
1759 Matcher<const Tuple2&> m = Eq();
1760 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1761 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1762}
1763
1764// Tests that Eq() describes itself properly.
1765TEST(Eq2Test, CanDescribeSelf) {
1766 Matcher<const Tuple2&> m = Eq();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001767 EXPECT_EQ("are an equal pair", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001768}
1769
1770// Tests that Ge() matches a 2-tuple where the first field >= the
1771// second field.
1772TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) {
1773 Matcher<const Tuple2&> m = Ge();
1774 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1775 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1776 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1777}
1778
1779// Tests that Ge() describes itself properly.
1780TEST(Ge2Test, CanDescribeSelf) {
1781 Matcher<const Tuple2&> m = Ge();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001782 EXPECT_EQ("are a pair where the first >= the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001783}
1784
1785// Tests that Gt() matches a 2-tuple where the first field > the
1786// second field.
1787TEST(Gt2Test, MatchesGreaterThanArguments) {
1788 Matcher<const Tuple2&> m = Gt();
1789 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1790 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1791 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1792}
1793
1794// Tests that Gt() describes itself properly.
1795TEST(Gt2Test, CanDescribeSelf) {
1796 Matcher<const Tuple2&> m = Gt();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001797 EXPECT_EQ("are a pair where the first > the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001798}
1799
1800// Tests that Le() matches a 2-tuple where the first field <= the
1801// second field.
1802TEST(Le2Test, MatchesLessThanOrEqualArguments) {
1803 Matcher<const Tuple2&> m = Le();
1804 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1805 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1806 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1807}
1808
1809// Tests that Le() describes itself properly.
1810TEST(Le2Test, CanDescribeSelf) {
1811 Matcher<const Tuple2&> m = Le();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001812 EXPECT_EQ("are a pair where the first <= the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001813}
1814
1815// Tests that Lt() matches a 2-tuple where the first field < the
1816// second field.
1817TEST(Lt2Test, MatchesLessThanArguments) {
1818 Matcher<const Tuple2&> m = Lt();
1819 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1820 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1821 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1822}
1823
1824// Tests that Lt() describes itself properly.
1825TEST(Lt2Test, CanDescribeSelf) {
1826 Matcher<const Tuple2&> m = Lt();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001827 EXPECT_EQ("are a pair where the first < the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001828}
1829
1830// Tests that Ne() matches a 2-tuple where the first field != the
1831// second field.
1832TEST(Ne2Test, MatchesUnequalArguments) {
1833 Matcher<const Tuple2&> m = Ne();
1834 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1835 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1836 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1837}
1838
1839// Tests that Ne() describes itself properly.
1840TEST(Ne2Test, CanDescribeSelf) {
1841 Matcher<const Tuple2&> m = Ne();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001842 EXPECT_EQ("are an unequal pair", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001843}
1844
1845// Tests that Not(m) matches any value that doesn't match m.
1846TEST(NotTest, NegatesMatcher) {
1847 Matcher<int> m;
1848 m = Not(Eq(2));
1849 EXPECT_TRUE(m.Matches(3));
1850 EXPECT_FALSE(m.Matches(2));
1851}
1852
1853// Tests that Not(m) describes itself properly.
1854TEST(NotTest, CanDescribeSelf) {
1855 Matcher<int> m = Not(Eq(5));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001856 EXPECT_EQ("isn't equal to 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001857}
1858
zhanyong.wan18490652009-05-11 18:54:08 +00001859// Tests that monomorphic matchers are safely cast by the Not matcher.
1860TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {
1861 // greater_than_5 is a monomorphic matcher.
1862 Matcher<int> greater_than_5 = Gt(5);
1863
1864 Matcher<const int&> m = Not(greater_than_5);
1865 Matcher<int&> m2 = Not(greater_than_5);
1866 Matcher<int&> m3 = Not(m);
1867}
1868
zhanyong.wan02c15052010-06-09 19:21:30 +00001869// Helper to allow easy testing of AllOf matchers with num parameters.
1870void AllOfMatches(int num, const Matcher<int>& m) {
1871 SCOPED_TRACE(Describe(m));
1872 EXPECT_TRUE(m.Matches(0));
1873 for (int i = 1; i <= num; ++i) {
1874 EXPECT_FALSE(m.Matches(i));
1875 }
1876 EXPECT_TRUE(m.Matches(num + 1));
1877}
1878
shiqiane35fdd92008-12-10 05:08:54 +00001879// Tests that AllOf(m1, ..., mn) matches any value that matches all of
1880// the given matchers.
1881TEST(AllOfTest, MatchesWhenAllMatch) {
1882 Matcher<int> m;
1883 m = AllOf(Le(2), Ge(1));
1884 EXPECT_TRUE(m.Matches(1));
1885 EXPECT_TRUE(m.Matches(2));
1886 EXPECT_FALSE(m.Matches(0));
1887 EXPECT_FALSE(m.Matches(3));
1888
1889 m = AllOf(Gt(0), Ne(1), Ne(2));
1890 EXPECT_TRUE(m.Matches(3));
1891 EXPECT_FALSE(m.Matches(2));
1892 EXPECT_FALSE(m.Matches(1));
1893 EXPECT_FALSE(m.Matches(0));
1894
1895 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
1896 EXPECT_TRUE(m.Matches(4));
1897 EXPECT_FALSE(m.Matches(3));
1898 EXPECT_FALSE(m.Matches(2));
1899 EXPECT_FALSE(m.Matches(1));
1900 EXPECT_FALSE(m.Matches(0));
1901
1902 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
1903 EXPECT_TRUE(m.Matches(0));
1904 EXPECT_TRUE(m.Matches(1));
1905 EXPECT_FALSE(m.Matches(3));
zhanyong.wan02c15052010-06-09 19:21:30 +00001906
1907 // The following tests for varying number of sub-matchers. Due to the way
1908 // the sub-matchers are handled it is enough to test every sub-matcher once
1909 // with sub-matchers using the same matcher type. Varying matcher types are
1910 // checked for above.
1911 AllOfMatches(2, AllOf(Ne(1), Ne(2)));
1912 AllOfMatches(3, AllOf(Ne(1), Ne(2), Ne(3)));
1913 AllOfMatches(4, AllOf(Ne(1), Ne(2), Ne(3), Ne(4)));
1914 AllOfMatches(5, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5)));
1915 AllOfMatches(6, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6)));
1916 AllOfMatches(7, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7)));
1917 AllOfMatches(8, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
1918 Ne(8)));
1919 AllOfMatches(9, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
1920 Ne(8), Ne(9)));
1921 AllOfMatches(10, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8),
1922 Ne(9), Ne(10)));
shiqiane35fdd92008-12-10 05:08:54 +00001923}
1924
1925// Tests that AllOf(m1, ..., mn) describes itself properly.
1926TEST(AllOfTest, CanDescribeSelf) {
1927 Matcher<int> m;
1928 m = AllOf(Le(2), Ge(1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001929 EXPECT_EQ("(is <= 2) and (is >= 1)", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001930
1931 m = AllOf(Gt(0), Ne(1), Ne(2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001932 EXPECT_EQ("(is > 0) and "
1933 "((isn't equal to 1) and "
1934 "(isn't equal to 2))",
shiqiane35fdd92008-12-10 05:08:54 +00001935 Describe(m));
1936
1937
1938 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001939 EXPECT_EQ("(is > 0) and "
1940 "((isn't equal to 1) and "
1941 "((isn't equal to 2) and "
1942 "(isn't equal to 3)))",
shiqiane35fdd92008-12-10 05:08:54 +00001943 Describe(m));
1944
1945
1946 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001947 EXPECT_EQ("(is >= 0) and "
1948 "((is < 10) and "
1949 "((isn't equal to 3) and "
1950 "((isn't equal to 5) and "
1951 "(isn't equal to 7))))",
1952 Describe(m));
1953}
1954
1955// Tests that AllOf(m1, ..., mn) describes its negation properly.
1956TEST(AllOfTest, CanDescribeNegation) {
1957 Matcher<int> m;
1958 m = AllOf(Le(2), Ge(1));
1959 EXPECT_EQ("(isn't <= 2) or "
1960 "(isn't >= 1)",
1961 DescribeNegation(m));
1962
1963 m = AllOf(Gt(0), Ne(1), Ne(2));
1964 EXPECT_EQ("(isn't > 0) or "
1965 "((is equal to 1) or "
1966 "(is equal to 2))",
1967 DescribeNegation(m));
1968
1969
1970 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
1971 EXPECT_EQ("(isn't > 0) or "
1972 "((is equal to 1) or "
1973 "((is equal to 2) or "
1974 "(is equal to 3)))",
1975 DescribeNegation(m));
1976
1977
1978 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
1979 EXPECT_EQ("(isn't >= 0) or "
1980 "((isn't < 10) or "
1981 "((is equal to 3) or "
1982 "((is equal to 5) or "
1983 "(is equal to 7))))",
1984 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00001985}
1986
zhanyong.wan18490652009-05-11 18:54:08 +00001987// Tests that monomorphic matchers are safely cast by the AllOf matcher.
1988TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
1989 // greater_than_5 and less_than_10 are monomorphic matchers.
1990 Matcher<int> greater_than_5 = Gt(5);
1991 Matcher<int> less_than_10 = Lt(10);
1992
1993 Matcher<const int&> m = AllOf(greater_than_5, less_than_10);
1994 Matcher<int&> m2 = AllOf(greater_than_5, less_than_10);
1995 Matcher<int&> m3 = AllOf(greater_than_5, m2);
1996
1997 // Tests that BothOf works when composing itself.
1998 Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10);
1999 Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);
2000}
2001
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002002TEST(AllOfTest, ExplainsResult) {
2003 Matcher<int> m;
2004
2005 // Successful match. Both matchers need to explain. The second
2006 // matcher doesn't give an explanation, so only the first matcher's
2007 // explanation is printed.
2008 m = AllOf(GreaterThan(10), Lt(30));
2009 EXPECT_EQ("which is 15 more than 10", Explain(m, 25));
2010
2011 // Successful match. Both matchers need to explain.
2012 m = AllOf(GreaterThan(10), GreaterThan(20));
2013 EXPECT_EQ("which is 20 more than 10, and which is 10 more than 20",
2014 Explain(m, 30));
2015
2016 // Successful match. All matchers need to explain. The second
2017 // matcher doesn't given an explanation.
2018 m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20));
2019 EXPECT_EQ("which is 15 more than 10, and which is 5 more than 20",
2020 Explain(m, 25));
2021
2022 // Successful match. All matchers need to explain.
2023 m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2024 EXPECT_EQ("which is 30 more than 10, and which is 20 more than 20, "
2025 "and which is 10 more than 30",
2026 Explain(m, 40));
2027
2028 // Failed match. The first matcher, which failed, needs to
2029 // explain.
2030 m = AllOf(GreaterThan(10), GreaterThan(20));
2031 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
2032
2033 // Failed match. The second matcher, which failed, needs to
2034 // explain. Since it doesn't given an explanation, nothing is
2035 // printed.
2036 m = AllOf(GreaterThan(10), Lt(30));
2037 EXPECT_EQ("", Explain(m, 40));
2038
2039 // Failed match. The second matcher, which failed, needs to
2040 // explain.
2041 m = AllOf(GreaterThan(10), GreaterThan(20));
2042 EXPECT_EQ("which is 5 less than 20", Explain(m, 15));
2043}
2044
zhanyong.wan02c15052010-06-09 19:21:30 +00002045// Helper to allow easy testing of AnyOf matchers with num parameters.
2046void AnyOfMatches(int num, const Matcher<int>& m) {
2047 SCOPED_TRACE(Describe(m));
2048 EXPECT_FALSE(m.Matches(0));
2049 for (int i = 1; i <= num; ++i) {
2050 EXPECT_TRUE(m.Matches(i));
2051 }
2052 EXPECT_FALSE(m.Matches(num + 1));
2053}
2054
shiqiane35fdd92008-12-10 05:08:54 +00002055// Tests that AnyOf(m1, ..., mn) matches any value that matches at
2056// least one of the given matchers.
2057TEST(AnyOfTest, MatchesWhenAnyMatches) {
2058 Matcher<int> m;
2059 m = AnyOf(Le(1), Ge(3));
2060 EXPECT_TRUE(m.Matches(1));
2061 EXPECT_TRUE(m.Matches(4));
2062 EXPECT_FALSE(m.Matches(2));
2063
2064 m = AnyOf(Lt(0), Eq(1), Eq(2));
2065 EXPECT_TRUE(m.Matches(-1));
2066 EXPECT_TRUE(m.Matches(1));
2067 EXPECT_TRUE(m.Matches(2));
2068 EXPECT_FALSE(m.Matches(0));
2069
2070 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2071 EXPECT_TRUE(m.Matches(-1));
2072 EXPECT_TRUE(m.Matches(1));
2073 EXPECT_TRUE(m.Matches(2));
2074 EXPECT_TRUE(m.Matches(3));
2075 EXPECT_FALSE(m.Matches(0));
2076
2077 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2078 EXPECT_TRUE(m.Matches(0));
2079 EXPECT_TRUE(m.Matches(11));
2080 EXPECT_TRUE(m.Matches(3));
2081 EXPECT_FALSE(m.Matches(2));
zhanyong.wan02c15052010-06-09 19:21:30 +00002082
2083 // The following tests for varying number of sub-matchers. Due to the way
2084 // the sub-matchers are handled it is enough to test every sub-matcher once
2085 // with sub-matchers using the same matcher type. Varying matcher types are
2086 // checked for above.
2087 AnyOfMatches(2, AnyOf(1, 2));
2088 AnyOfMatches(3, AnyOf(1, 2, 3));
2089 AnyOfMatches(4, AnyOf(1, 2, 3, 4));
2090 AnyOfMatches(5, AnyOf(1, 2, 3, 4, 5));
2091 AnyOfMatches(6, AnyOf(1, 2, 3, 4, 5, 6));
2092 AnyOfMatches(7, AnyOf(1, 2, 3, 4, 5, 6, 7));
2093 AnyOfMatches(8, AnyOf(1, 2, 3, 4, 5, 6, 7, 8));
2094 AnyOfMatches(9, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9));
2095 AnyOfMatches(10, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
shiqiane35fdd92008-12-10 05:08:54 +00002096}
2097
2098// Tests that AnyOf(m1, ..., mn) describes itself properly.
2099TEST(AnyOfTest, CanDescribeSelf) {
2100 Matcher<int> m;
2101 m = AnyOf(Le(1), Ge(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002102 EXPECT_EQ("(is <= 1) or (is >= 3)",
shiqiane35fdd92008-12-10 05:08:54 +00002103 Describe(m));
2104
2105 m = AnyOf(Lt(0), Eq(1), Eq(2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002106 EXPECT_EQ("(is < 0) or "
shiqiane35fdd92008-12-10 05:08:54 +00002107 "((is equal to 1) or (is equal to 2))",
2108 Describe(m));
2109
2110 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002111 EXPECT_EQ("(is < 0) or "
shiqiane35fdd92008-12-10 05:08:54 +00002112 "((is equal to 1) or "
2113 "((is equal to 2) or "
2114 "(is equal to 3)))",
2115 Describe(m));
2116
2117 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002118 EXPECT_EQ("(is <= 0) or "
2119 "((is > 10) or "
shiqiane35fdd92008-12-10 05:08:54 +00002120 "((is equal to 3) or "
2121 "((is equal to 5) or "
2122 "(is equal to 7))))",
2123 Describe(m));
2124}
2125
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002126// Tests that AnyOf(m1, ..., mn) describes its negation properly.
2127TEST(AnyOfTest, CanDescribeNegation) {
2128 Matcher<int> m;
2129 m = AnyOf(Le(1), Ge(3));
2130 EXPECT_EQ("(isn't <= 1) and (isn't >= 3)",
2131 DescribeNegation(m));
2132
2133 m = AnyOf(Lt(0), Eq(1), Eq(2));
2134 EXPECT_EQ("(isn't < 0) and "
2135 "((isn't equal to 1) and (isn't equal to 2))",
2136 DescribeNegation(m));
2137
2138 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2139 EXPECT_EQ("(isn't < 0) and "
2140 "((isn't equal to 1) and "
2141 "((isn't equal to 2) and "
2142 "(isn't equal to 3)))",
2143 DescribeNegation(m));
2144
2145 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2146 EXPECT_EQ("(isn't <= 0) and "
2147 "((isn't > 10) and "
2148 "((isn't equal to 3) and "
2149 "((isn't equal to 5) and "
2150 "(isn't equal to 7))))",
2151 DescribeNegation(m));
2152}
2153
zhanyong.wan18490652009-05-11 18:54:08 +00002154// Tests that monomorphic matchers are safely cast by the AnyOf matcher.
2155TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
2156 // greater_than_5 and less_than_10 are monomorphic matchers.
2157 Matcher<int> greater_than_5 = Gt(5);
2158 Matcher<int> less_than_10 = Lt(10);
2159
2160 Matcher<const int&> m = AnyOf(greater_than_5, less_than_10);
2161 Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10);
2162 Matcher<int&> m3 = AnyOf(greater_than_5, m2);
2163
2164 // Tests that EitherOf works when composing itself.
2165 Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10);
2166 Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);
2167}
2168
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002169TEST(AnyOfTest, ExplainsResult) {
2170 Matcher<int> m;
2171
2172 // Failed match. Both matchers need to explain. The second
2173 // matcher doesn't give an explanation, so only the first matcher's
2174 // explanation is printed.
2175 m = AnyOf(GreaterThan(10), Lt(0));
2176 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
2177
2178 // Failed match. Both matchers need to explain.
2179 m = AnyOf(GreaterThan(10), GreaterThan(20));
2180 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20",
2181 Explain(m, 5));
2182
2183 // Failed match. All matchers need to explain. The second
2184 // matcher doesn't given an explanation.
2185 m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30));
2186 EXPECT_EQ("which is 5 less than 10, and which is 25 less than 30",
2187 Explain(m, 5));
2188
2189 // Failed match. All matchers need to explain.
2190 m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2191 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20, "
2192 "and which is 25 less than 30",
2193 Explain(m, 5));
2194
2195 // Successful match. The first matcher, which succeeded, needs to
2196 // explain.
2197 m = AnyOf(GreaterThan(10), GreaterThan(20));
2198 EXPECT_EQ("which is 5 more than 10", Explain(m, 15));
2199
2200 // Successful match. The second matcher, which succeeded, needs to
2201 // explain. Since it doesn't given an explanation, nothing is
2202 // printed.
2203 m = AnyOf(GreaterThan(10), Lt(30));
2204 EXPECT_EQ("", Explain(m, 0));
2205
2206 // Successful match. The second matcher, which succeeded, needs to
2207 // explain.
2208 m = AnyOf(GreaterThan(30), GreaterThan(20));
2209 EXPECT_EQ("which is 5 more than 20", Explain(m, 25));
2210}
2211
shiqiane35fdd92008-12-10 05:08:54 +00002212// The following predicate function and predicate functor are for
2213// testing the Truly(predicate) matcher.
2214
2215// Returns non-zero if the input is positive. Note that the return
2216// type of this function is not bool. It's OK as Truly() accepts any
2217// unary function or functor whose return type can be implicitly
2218// converted to bool.
2219int IsPositive(double x) {
2220 return x > 0 ? 1 : 0;
2221}
2222
2223// This functor returns true if the input is greater than the given
2224// number.
2225class IsGreaterThan {
2226 public:
2227 explicit IsGreaterThan(int threshold) : threshold_(threshold) {}
2228
2229 bool operator()(int n) const { return n > threshold_; }
zhanyong.wan32de5f52009-12-23 00:13:23 +00002230
shiqiane35fdd92008-12-10 05:08:54 +00002231 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002232 int threshold_;
shiqiane35fdd92008-12-10 05:08:54 +00002233};
2234
2235// For testing Truly().
2236const int foo = 0;
2237
2238// This predicate returns true iff the argument references foo and has
2239// a zero value.
2240bool ReferencesFooAndIsZero(const int& n) {
2241 return (&n == &foo) && (n == 0);
2242}
2243
2244// Tests that Truly(predicate) matches what satisfies the given
2245// predicate.
2246TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {
2247 Matcher<double> m = Truly(IsPositive);
2248 EXPECT_TRUE(m.Matches(2.0));
2249 EXPECT_FALSE(m.Matches(-1.5));
2250}
2251
2252// Tests that Truly(predicate_functor) works too.
2253TEST(TrulyTest, CanBeUsedWithFunctor) {
2254 Matcher<int> m = Truly(IsGreaterThan(5));
2255 EXPECT_TRUE(m.Matches(6));
2256 EXPECT_FALSE(m.Matches(4));
2257}
2258
zhanyong.wan8d3dc0c2011-04-14 19:37:06 +00002259// A class that can be implicitly converted to bool.
2260class ConvertibleToBool {
2261 public:
2262 explicit ConvertibleToBool(int number) : number_(number) {}
2263 operator bool() const { return number_ != 0; }
2264
2265 private:
2266 int number_;
2267};
2268
2269ConvertibleToBool IsNotZero(int number) {
2270 return ConvertibleToBool(number);
2271}
2272
2273// Tests that the predicate used in Truly() may return a class that's
2274// implicitly convertible to bool, even when the class has no
2275// operator!().
2276TEST(TrulyTest, PredicateCanReturnAClassConvertibleToBool) {
2277 Matcher<int> m = Truly(IsNotZero);
2278 EXPECT_TRUE(m.Matches(1));
2279 EXPECT_FALSE(m.Matches(0));
2280}
2281
shiqiane35fdd92008-12-10 05:08:54 +00002282// Tests that Truly(predicate) can describe itself properly.
2283TEST(TrulyTest, CanDescribeSelf) {
2284 Matcher<double> m = Truly(IsPositive);
2285 EXPECT_EQ("satisfies the given predicate",
2286 Describe(m));
2287}
2288
2289// Tests that Truly(predicate) works when the matcher takes its
2290// argument by reference.
2291TEST(TrulyTest, WorksForByRefArguments) {
2292 Matcher<const int&> m = Truly(ReferencesFooAndIsZero);
2293 EXPECT_TRUE(m.Matches(foo));
2294 int n = 0;
2295 EXPECT_FALSE(m.Matches(n));
2296}
2297
2298// Tests that Matches(m) is a predicate satisfied by whatever that
2299// matches matcher m.
2300TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {
2301 EXPECT_TRUE(Matches(Ge(0))(1));
2302 EXPECT_FALSE(Matches(Eq('a'))('b'));
2303}
2304
2305// Tests that Matches(m) works when the matcher takes its argument by
2306// reference.
2307TEST(MatchesTest, WorksOnByRefArguments) {
2308 int m = 0, n = 0;
2309 EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n));
2310 EXPECT_FALSE(Matches(Ref(m))(n));
2311}
2312
2313// Tests that a Matcher on non-reference type can be used in
2314// Matches().
2315TEST(MatchesTest, WorksWithMatcherOnNonRefType) {
2316 Matcher<int> eq5 = Eq(5);
2317 EXPECT_TRUE(Matches(eq5)(5));
2318 EXPECT_FALSE(Matches(eq5)(2));
2319}
2320
zhanyong.wanb8243162009-06-04 05:48:20 +00002321// Tests Value(value, matcher). Since Value() is a simple wrapper for
2322// Matches(), which has been tested already, we don't spend a lot of
2323// effort on testing Value().
2324TEST(ValueTest, WorksWithPolymorphicMatcher) {
2325 EXPECT_TRUE(Value("hi", StartsWith("h")));
2326 EXPECT_FALSE(Value(5, Gt(10)));
2327}
2328
2329TEST(ValueTest, WorksWithMonomorphicMatcher) {
2330 const Matcher<int> is_zero = Eq(0);
2331 EXPECT_TRUE(Value(0, is_zero));
2332 EXPECT_FALSE(Value('a', is_zero));
2333
2334 int n = 0;
2335 const Matcher<const int&> ref_n = Ref(n);
2336 EXPECT_TRUE(Value(n, ref_n));
2337 EXPECT_FALSE(Value(1, ref_n));
2338}
2339
zhanyong.wana862f1d2010-03-15 21:23:04 +00002340TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) {
zhanyong.wan34b034c2010-03-05 21:23:23 +00002341 StringMatchResultListener listener1;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002342 EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002343 EXPECT_EQ("% 2 == 0", listener1.str());
2344
2345 StringMatchResultListener listener2;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002346 EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002347 EXPECT_EQ("", listener2.str());
2348}
2349
zhanyong.wana862f1d2010-03-15 21:23:04 +00002350TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) {
zhanyong.wan34b034c2010-03-05 21:23:23 +00002351 const Matcher<int> is_even = PolymorphicIsEven();
2352 StringMatchResultListener listener1;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002353 EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002354 EXPECT_EQ("% 2 == 0", listener1.str());
2355
2356 const Matcher<const double&> is_zero = Eq(0);
2357 StringMatchResultListener listener2;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002358 EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002359 EXPECT_EQ("", listener2.str());
2360}
2361
zhanyong.wana862f1d2010-03-15 21:23:04 +00002362MATCHER_P(Really, inner_matcher, "") {
2363 return ExplainMatchResult(inner_matcher, arg, result_listener);
2364}
2365
2366TEST(ExplainMatchResultTest, WorksInsideMATCHER) {
2367 EXPECT_THAT(0, Really(Eq(0)));
2368}
2369
zhanyong.wanbf550852009-06-09 06:09:53 +00002370TEST(AllArgsTest, WorksForTuple) {
2371 EXPECT_THAT(make_tuple(1, 2L), AllArgs(Lt()));
2372 EXPECT_THAT(make_tuple(2L, 1), Not(AllArgs(Lt())));
2373}
2374
2375TEST(AllArgsTest, WorksForNonTuple) {
2376 EXPECT_THAT(42, AllArgs(Gt(0)));
2377 EXPECT_THAT('a', Not(AllArgs(Eq('b'))));
2378}
2379
2380class AllArgsHelper {
2381 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002382 AllArgsHelper() {}
2383
zhanyong.wanbf550852009-06-09 06:09:53 +00002384 MOCK_METHOD2(Helper, int(char x, int y));
zhanyong.wan32de5f52009-12-23 00:13:23 +00002385
2386 private:
2387 GTEST_DISALLOW_COPY_AND_ASSIGN_(AllArgsHelper);
zhanyong.wanbf550852009-06-09 06:09:53 +00002388};
2389
2390TEST(AllArgsTest, WorksInWithClause) {
2391 AllArgsHelper helper;
2392 ON_CALL(helper, Helper(_, _))
2393 .With(AllArgs(Lt()))
2394 .WillByDefault(Return(1));
2395 EXPECT_CALL(helper, Helper(_, _));
2396 EXPECT_CALL(helper, Helper(_, _))
2397 .With(AllArgs(Gt()))
2398 .WillOnce(Return(2));
2399
2400 EXPECT_EQ(1, helper.Helper('\1', 2));
2401 EXPECT_EQ(2, helper.Helper('a', 1));
2402}
2403
shiqiane35fdd92008-12-10 05:08:54 +00002404// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2405// matches the matcher.
2406TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {
2407 ASSERT_THAT(5, Ge(2)) << "This should succeed.";
2408 ASSERT_THAT("Foo", EndsWith("oo"));
2409 EXPECT_THAT(2, AllOf(Le(7), Ge(0))) << "This should succeed too.";
2410 EXPECT_THAT("Hello", StartsWith("Hell"));
2411}
2412
2413// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2414// doesn't match the matcher.
2415TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {
2416 // 'n' must be static as it is used in an EXPECT_FATAL_FAILURE(),
2417 // which cannot reference auto variables.
zhanyong.wan736baa82010-09-27 17:44:16 +00002418 static unsigned short n; // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00002419 n = 5;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002420
2421 // VC++ prior to version 8.0 SP1 has a bug where it will not see any
2422 // functions declared in the namespace scope from within nested classes.
2423 // EXPECT/ASSERT_(NON)FATAL_FAILURE macros use nested classes so that all
2424 // namespace-level functions invoked inside them need to be explicitly
2425 // resolved.
2426 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Gt(10)),
shiqiane35fdd92008-12-10 05:08:54 +00002427 "Value of: n\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002428 "Expected: is > 10\n"
zhanyong.wan736baa82010-09-27 17:44:16 +00002429 " Actual: 5" + OfType("unsigned short"));
shiqiane35fdd92008-12-10 05:08:54 +00002430 n = 0;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002431 EXPECT_NONFATAL_FAILURE(
2432 EXPECT_THAT(n, ::testing::AllOf(::testing::Le(7), ::testing::Ge(5))),
2433 "Value of: n\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002434 "Expected: (is <= 7) and (is >= 5)\n"
zhanyong.wan736baa82010-09-27 17:44:16 +00002435 " Actual: 0" + OfType("unsigned short"));
shiqiane35fdd92008-12-10 05:08:54 +00002436}
2437
2438// Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument
2439// has a reference type.
2440TEST(MatcherAssertionTest, WorksForByRefArguments) {
2441 // We use a static variable here as EXPECT_FATAL_FAILURE() cannot
2442 // reference auto variables.
2443 static int n;
2444 n = 0;
2445 EXPECT_THAT(n, AllOf(Le(7), Ref(n)));
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002446 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
shiqiane35fdd92008-12-10 05:08:54 +00002447 "Value of: n\n"
2448 "Expected: does not reference the variable @");
2449 // Tests the "Actual" part.
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002450 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
zhanyong.wan736baa82010-09-27 17:44:16 +00002451 "Actual: 0" + OfType("int") + ", which is located @");
shiqiane35fdd92008-12-10 05:08:54 +00002452}
2453
zhanyong.wan95b12332009-09-25 18:55:50 +00002454#if !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002455// Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is
2456// monomorphic.
zhanyong.wan95b12332009-09-25 18:55:50 +00002457
2458// ASSERT_THAT("hello", starts_with_he) fails to compile with Nokia's
2459// Symbian compiler: it tries to compile
2460// template<T, U> class MatcherCastImpl { ...
zhanyong.wandb22c222010-01-28 21:52:29 +00002461// virtual bool MatchAndExplain(T x, ...) const {
2462// return source_matcher_.MatchAndExplain(static_cast<U>(x), ...);
zhanyong.wan95b12332009-09-25 18:55:50 +00002463// with U == string and T == const char*
2464// With ASSERT_THAT("hello"...) changed to ASSERT_THAT(string("hello") ... )
2465// the compiler silently crashes with no output.
2466// If MatcherCastImpl is changed to use U(x) instead of static_cast<U>(x)
2467// the code compiles but the converted string is bogus.
shiqiane35fdd92008-12-10 05:08:54 +00002468TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
2469 Matcher<const char*> starts_with_he = StartsWith("he");
2470 ASSERT_THAT("hello", starts_with_he);
2471
2472 Matcher<const string&> ends_with_ok = EndsWith("ok");
2473 ASSERT_THAT("book", ends_with_ok);
zhanyong.wan736baa82010-09-27 17:44:16 +00002474 const string bad = "bad";
2475 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(bad, ends_with_ok),
2476 "Value of: bad\n"
2477 "Expected: ends with \"ok\"\n"
2478 " Actual: \"bad\"");
shiqiane35fdd92008-12-10 05:08:54 +00002479 Matcher<int> is_greater_than_5 = Gt(5);
2480 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5),
2481 "Value of: 5\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002482 "Expected: is > 5\n"
zhanyong.wan736baa82010-09-27 17:44:16 +00002483 " Actual: 5" + OfType("int"));
shiqiane35fdd92008-12-10 05:08:54 +00002484}
zhanyong.wan95b12332009-09-25 18:55:50 +00002485#endif // !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002486
2487// Tests floating-point matchers.
2488template <typename RawType>
2489class FloatingPointTest : public testing::Test {
2490 protected:
2491 typedef typename testing::internal::FloatingPoint<RawType> Floating;
2492 typedef typename Floating::Bits Bits;
2493
2494 virtual void SetUp() {
2495 const size_t max_ulps = Floating::kMaxUlps;
2496
2497 // The bits that represent 0.0.
2498 const Bits zero_bits = Floating(0).bits();
2499
2500 // Makes some numbers close to 0.0.
2501 close_to_positive_zero_ = Floating::ReinterpretBits(zero_bits + max_ulps/2);
2502 close_to_negative_zero_ = -Floating::ReinterpretBits(
2503 zero_bits + max_ulps - max_ulps/2);
2504 further_from_negative_zero_ = -Floating::ReinterpretBits(
2505 zero_bits + max_ulps + 1 - max_ulps/2);
2506
2507 // The bits that represent 1.0.
2508 const Bits one_bits = Floating(1).bits();
2509
2510 // Makes some numbers close to 1.0.
2511 close_to_one_ = Floating::ReinterpretBits(one_bits + max_ulps);
2512 further_from_one_ = Floating::ReinterpretBits(one_bits + max_ulps + 1);
2513
2514 // +infinity.
2515 infinity_ = Floating::Infinity();
2516
2517 // The bits that represent +infinity.
2518 const Bits infinity_bits = Floating(infinity_).bits();
2519
2520 // Makes some numbers close to infinity.
2521 close_to_infinity_ = Floating::ReinterpretBits(infinity_bits - max_ulps);
2522 further_from_infinity_ = Floating::ReinterpretBits(
2523 infinity_bits - max_ulps - 1);
2524
2525 // Makes some NAN's.
2526 nan1_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 1);
2527 nan2_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 200);
2528 }
2529
2530 void TestSize() {
2531 EXPECT_EQ(sizeof(RawType), sizeof(Bits));
2532 }
2533
2534 // A battery of tests for FloatingEqMatcher::Matches.
2535 // matcher_maker is a pointer to a function which creates a FloatingEqMatcher.
2536 void TestMatches(
2537 testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) {
2538 Matcher<RawType> m1 = matcher_maker(0.0);
2539 EXPECT_TRUE(m1.Matches(-0.0));
2540 EXPECT_TRUE(m1.Matches(close_to_positive_zero_));
2541 EXPECT_TRUE(m1.Matches(close_to_negative_zero_));
2542 EXPECT_FALSE(m1.Matches(1.0));
2543
2544 Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_);
2545 EXPECT_FALSE(m2.Matches(further_from_negative_zero_));
2546
2547 Matcher<RawType> m3 = matcher_maker(1.0);
2548 EXPECT_TRUE(m3.Matches(close_to_one_));
2549 EXPECT_FALSE(m3.Matches(further_from_one_));
2550
2551 // Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above.
2552 EXPECT_FALSE(m3.Matches(0.0));
2553
2554 Matcher<RawType> m4 = matcher_maker(-infinity_);
2555 EXPECT_TRUE(m4.Matches(-close_to_infinity_));
2556
2557 Matcher<RawType> m5 = matcher_maker(infinity_);
2558 EXPECT_TRUE(m5.Matches(close_to_infinity_));
2559
2560 // This is interesting as the representations of infinity_ and nan1_
2561 // are only 1 DLP apart.
2562 EXPECT_FALSE(m5.Matches(nan1_));
2563
2564 // matcher_maker can produce a Matcher<const RawType&>, which is needed in
2565 // some cases.
2566 Matcher<const RawType&> m6 = matcher_maker(0.0);
2567 EXPECT_TRUE(m6.Matches(-0.0));
2568 EXPECT_TRUE(m6.Matches(close_to_positive_zero_));
2569 EXPECT_FALSE(m6.Matches(1.0));
2570
2571 // matcher_maker can produce a Matcher<RawType&>, which is needed in some
2572 // cases.
2573 Matcher<RawType&> m7 = matcher_maker(0.0);
2574 RawType x = 0.0;
2575 EXPECT_TRUE(m7.Matches(x));
2576 x = 0.01f;
2577 EXPECT_FALSE(m7.Matches(x));
2578 }
2579
2580 // Pre-calculated numbers to be used by the tests.
2581
2582 static RawType close_to_positive_zero_;
2583 static RawType close_to_negative_zero_;
2584 static RawType further_from_negative_zero_;
2585
2586 static RawType close_to_one_;
2587 static RawType further_from_one_;
2588
2589 static RawType infinity_;
2590 static RawType close_to_infinity_;
2591 static RawType further_from_infinity_;
2592
2593 static RawType nan1_;
2594 static RawType nan2_;
2595};
2596
2597template <typename RawType>
2598RawType FloatingPointTest<RawType>::close_to_positive_zero_;
2599
2600template <typename RawType>
2601RawType FloatingPointTest<RawType>::close_to_negative_zero_;
2602
2603template <typename RawType>
2604RawType FloatingPointTest<RawType>::further_from_negative_zero_;
2605
2606template <typename RawType>
2607RawType FloatingPointTest<RawType>::close_to_one_;
2608
2609template <typename RawType>
2610RawType FloatingPointTest<RawType>::further_from_one_;
2611
2612template <typename RawType>
2613RawType FloatingPointTest<RawType>::infinity_;
2614
2615template <typename RawType>
2616RawType FloatingPointTest<RawType>::close_to_infinity_;
2617
2618template <typename RawType>
2619RawType FloatingPointTest<RawType>::further_from_infinity_;
2620
2621template <typename RawType>
2622RawType FloatingPointTest<RawType>::nan1_;
2623
2624template <typename RawType>
2625RawType FloatingPointTest<RawType>::nan2_;
2626
2627// Instantiate FloatingPointTest for testing floats.
2628typedef FloatingPointTest<float> FloatTest;
2629
2630TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) {
2631 TestMatches(&FloatEq);
2632}
2633
2634TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
2635 TestMatches(&NanSensitiveFloatEq);
2636}
2637
2638TEST_F(FloatTest, FloatEqCannotMatchNaN) {
2639 // FloatEq never matches NaN.
2640 Matcher<float> m = FloatEq(nan1_);
2641 EXPECT_FALSE(m.Matches(nan1_));
2642 EXPECT_FALSE(m.Matches(nan2_));
2643 EXPECT_FALSE(m.Matches(1.0));
2644}
2645
2646TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {
2647 // NanSensitiveFloatEq will match NaN.
2648 Matcher<float> m = NanSensitiveFloatEq(nan1_);
2649 EXPECT_TRUE(m.Matches(nan1_));
2650 EXPECT_TRUE(m.Matches(nan2_));
2651 EXPECT_FALSE(m.Matches(1.0));
2652}
2653
2654TEST_F(FloatTest, FloatEqCanDescribeSelf) {
2655 Matcher<float> m1 = FloatEq(2.0f);
2656 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002657 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002658
2659 Matcher<float> m2 = FloatEq(0.5f);
2660 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002661 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002662
2663 Matcher<float> m3 = FloatEq(nan1_);
2664 EXPECT_EQ("never matches", Describe(m3));
2665 EXPECT_EQ("is anything", DescribeNegation(m3));
2666}
2667
2668TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {
2669 Matcher<float> m1 = NanSensitiveFloatEq(2.0f);
2670 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002671 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002672
2673 Matcher<float> m2 = NanSensitiveFloatEq(0.5f);
2674 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002675 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002676
2677 Matcher<float> m3 = NanSensitiveFloatEq(nan1_);
2678 EXPECT_EQ("is NaN", Describe(m3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002679 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
shiqiane35fdd92008-12-10 05:08:54 +00002680}
2681
2682// Instantiate FloatingPointTest for testing doubles.
2683typedef FloatingPointTest<double> DoubleTest;
2684
2685TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
2686 TestMatches(&DoubleEq);
2687}
2688
2689TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
2690 TestMatches(&NanSensitiveDoubleEq);
2691}
2692
2693TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {
2694 // DoubleEq never matches NaN.
2695 Matcher<double> m = DoubleEq(nan1_);
2696 EXPECT_FALSE(m.Matches(nan1_));
2697 EXPECT_FALSE(m.Matches(nan2_));
2698 EXPECT_FALSE(m.Matches(1.0));
2699}
2700
2701TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {
2702 // NanSensitiveDoubleEq will match NaN.
2703 Matcher<double> m = NanSensitiveDoubleEq(nan1_);
2704 EXPECT_TRUE(m.Matches(nan1_));
2705 EXPECT_TRUE(m.Matches(nan2_));
2706 EXPECT_FALSE(m.Matches(1.0));
2707}
2708
2709TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {
2710 Matcher<double> m1 = DoubleEq(2.0);
2711 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002712 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002713
2714 Matcher<double> m2 = DoubleEq(0.5);
2715 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002716 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002717
2718 Matcher<double> m3 = DoubleEq(nan1_);
2719 EXPECT_EQ("never matches", Describe(m3));
2720 EXPECT_EQ("is anything", DescribeNegation(m3));
2721}
2722
2723TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {
2724 Matcher<double> m1 = NanSensitiveDoubleEq(2.0);
2725 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002726 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002727
2728 Matcher<double> m2 = NanSensitiveDoubleEq(0.5);
2729 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002730 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002731
2732 Matcher<double> m3 = NanSensitiveDoubleEq(nan1_);
2733 EXPECT_EQ("is NaN", Describe(m3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002734 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
shiqiane35fdd92008-12-10 05:08:54 +00002735}
2736
2737TEST(PointeeTest, RawPointer) {
2738 const Matcher<int*> m = Pointee(Ge(0));
2739
2740 int n = 1;
2741 EXPECT_TRUE(m.Matches(&n));
2742 n = -1;
2743 EXPECT_FALSE(m.Matches(&n));
2744 EXPECT_FALSE(m.Matches(NULL));
2745}
2746
2747TEST(PointeeTest, RawPointerToConst) {
2748 const Matcher<const double*> m = Pointee(Ge(0));
2749
2750 double x = 1;
2751 EXPECT_TRUE(m.Matches(&x));
2752 x = -1;
2753 EXPECT_FALSE(m.Matches(&x));
2754 EXPECT_FALSE(m.Matches(NULL));
2755}
2756
2757TEST(PointeeTest, ReferenceToConstRawPointer) {
2758 const Matcher<int* const &> m = Pointee(Ge(0));
2759
2760 int n = 1;
2761 EXPECT_TRUE(m.Matches(&n));
2762 n = -1;
2763 EXPECT_FALSE(m.Matches(&n));
2764 EXPECT_FALSE(m.Matches(NULL));
2765}
2766
2767TEST(PointeeTest, ReferenceToNonConstRawPointer) {
2768 const Matcher<double* &> m = Pointee(Ge(0));
2769
2770 double x = 1.0;
2771 double* p = &x;
2772 EXPECT_TRUE(m.Matches(p));
2773 x = -1;
2774 EXPECT_FALSE(m.Matches(p));
2775 p = NULL;
2776 EXPECT_FALSE(m.Matches(p));
2777}
2778
2779TEST(PointeeTest, NeverMatchesNull) {
2780 const Matcher<const char*> m = Pointee(_);
2781 EXPECT_FALSE(m.Matches(NULL));
2782}
2783
2784// Tests that we can write Pointee(value) instead of Pointee(Eq(value)).
2785TEST(PointeeTest, MatchesAgainstAValue) {
2786 const Matcher<int*> m = Pointee(5);
2787
2788 int n = 5;
2789 EXPECT_TRUE(m.Matches(&n));
2790 n = -1;
2791 EXPECT_FALSE(m.Matches(&n));
2792 EXPECT_FALSE(m.Matches(NULL));
2793}
2794
2795TEST(PointeeTest, CanDescribeSelf) {
2796 const Matcher<int*> m = Pointee(Gt(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002797 EXPECT_EQ("points to a value that is > 3", Describe(m));
2798 EXPECT_EQ("does not point to a value that is > 3",
shiqiane35fdd92008-12-10 05:08:54 +00002799 DescribeNegation(m));
2800}
2801
shiqiane35fdd92008-12-10 05:08:54 +00002802TEST(PointeeTest, CanExplainMatchResult) {
2803 const Matcher<const string*> m = Pointee(StartsWith("Hi"));
2804
2805 EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL)));
2806
zhanyong.wan736baa82010-09-27 17:44:16 +00002807 const Matcher<long*> m2 = Pointee(GreaterThan(1)); // NOLINT
2808 long n = 3; // NOLINT
2809 EXPECT_EQ("which points to 3" + OfType("long") + ", which is 2 more than 1",
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002810 Explain(m2, &n));
2811}
2812
2813TEST(PointeeTest, AlwaysExplainsPointee) {
2814 const Matcher<int*> m = Pointee(0);
2815 int n = 42;
zhanyong.wan736baa82010-09-27 17:44:16 +00002816 EXPECT_EQ("which points to 42" + OfType("int"), Explain(m, &n));
shiqiane35fdd92008-12-10 05:08:54 +00002817}
2818
2819// An uncopyable class.
2820class Uncopyable {
2821 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002822 explicit Uncopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00002823
2824 int value() const { return value_; }
2825 private:
2826 const int value_;
2827 GTEST_DISALLOW_COPY_AND_ASSIGN_(Uncopyable);
2828};
2829
2830// Returns true iff x.value() is positive.
2831bool ValueIsPositive(const Uncopyable& x) { return x.value() > 0; }
2832
2833// A user-defined struct for testing Field().
2834struct AStruct {
2835 AStruct() : x(0), y(1.0), z(5), p(NULL) {}
2836 AStruct(const AStruct& rhs)
2837 : x(rhs.x), y(rhs.y), z(rhs.z.value()), p(rhs.p) {}
2838
2839 int x; // A non-const field.
2840 const double y; // A const field.
2841 Uncopyable z; // An uncopyable field.
2842 const char* p; // A pointer field.
zhanyong.wan32de5f52009-12-23 00:13:23 +00002843
2844 private:
2845 GTEST_DISALLOW_ASSIGN_(AStruct);
shiqiane35fdd92008-12-10 05:08:54 +00002846};
2847
2848// A derived struct for testing Field().
2849struct DerivedStruct : public AStruct {
2850 char ch;
zhanyong.wan32de5f52009-12-23 00:13:23 +00002851
2852 private:
2853 GTEST_DISALLOW_ASSIGN_(DerivedStruct);
shiqiane35fdd92008-12-10 05:08:54 +00002854};
2855
2856// Tests that Field(&Foo::field, ...) works when field is non-const.
2857TEST(FieldTest, WorksForNonConstField) {
2858 Matcher<AStruct> m = Field(&AStruct::x, Ge(0));
2859
2860 AStruct a;
2861 EXPECT_TRUE(m.Matches(a));
2862 a.x = -1;
2863 EXPECT_FALSE(m.Matches(a));
2864}
2865
2866// Tests that Field(&Foo::field, ...) works when field is const.
2867TEST(FieldTest, WorksForConstField) {
2868 AStruct a;
2869
2870 Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0));
2871 EXPECT_TRUE(m.Matches(a));
2872 m = Field(&AStruct::y, Le(0.0));
2873 EXPECT_FALSE(m.Matches(a));
2874}
2875
2876// Tests that Field(&Foo::field, ...) works when field is not copyable.
2877TEST(FieldTest, WorksForUncopyableField) {
2878 AStruct a;
2879
2880 Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive));
2881 EXPECT_TRUE(m.Matches(a));
2882 m = Field(&AStruct::z, Not(Truly(ValueIsPositive)));
2883 EXPECT_FALSE(m.Matches(a));
2884}
2885
2886// Tests that Field(&Foo::field, ...) works when field is a pointer.
2887TEST(FieldTest, WorksForPointerField) {
2888 // Matching against NULL.
2889 Matcher<AStruct> m = Field(&AStruct::p, static_cast<const char*>(NULL));
2890 AStruct a;
2891 EXPECT_TRUE(m.Matches(a));
2892 a.p = "hi";
2893 EXPECT_FALSE(m.Matches(a));
2894
2895 // Matching a pointer that is not NULL.
2896 m = Field(&AStruct::p, StartsWith("hi"));
2897 a.p = "hill";
2898 EXPECT_TRUE(m.Matches(a));
2899 a.p = "hole";
2900 EXPECT_FALSE(m.Matches(a));
2901}
2902
2903// Tests that Field() works when the object is passed by reference.
2904TEST(FieldTest, WorksForByRefArgument) {
2905 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2906
2907 AStruct a;
2908 EXPECT_TRUE(m.Matches(a));
2909 a.x = -1;
2910 EXPECT_FALSE(m.Matches(a));
2911}
2912
2913// Tests that Field(&Foo::field, ...) works when the argument's type
2914// is a sub-type of Foo.
2915TEST(FieldTest, WorksForArgumentOfSubType) {
2916 // Note that the matcher expects DerivedStruct but we say AStruct
2917 // inside Field().
2918 Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0));
2919
2920 DerivedStruct d;
2921 EXPECT_TRUE(m.Matches(d));
2922 d.x = -1;
2923 EXPECT_FALSE(m.Matches(d));
2924}
2925
2926// Tests that Field(&Foo::field, m) works when field's type and m's
2927// argument type are compatible but not the same.
2928TEST(FieldTest, WorksForCompatibleMatcherType) {
2929 // The field is an int, but the inner matcher expects a signed char.
2930 Matcher<const AStruct&> m = Field(&AStruct::x,
2931 Matcher<signed char>(Ge(0)));
2932
2933 AStruct a;
2934 EXPECT_TRUE(m.Matches(a));
2935 a.x = -1;
2936 EXPECT_FALSE(m.Matches(a));
2937}
2938
2939// Tests that Field() can describe itself.
2940TEST(FieldTest, CanDescribeSelf) {
2941 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2942
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002943 EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
2944 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00002945}
2946
2947// Tests that Field() can explain the match result.
2948TEST(FieldTest, CanExplainMatchResult) {
2949 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2950
2951 AStruct a;
2952 a.x = 1;
zhanyong.wan736baa82010-09-27 17:44:16 +00002953 EXPECT_EQ("whose given field is 1" + OfType("int"), Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00002954
2955 m = Field(&AStruct::x, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00002956 EXPECT_EQ(
2957 "whose given field is 1" + OfType("int") + ", which is 1 more than 0",
2958 Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00002959}
2960
2961// Tests that Field() works when the argument is a pointer to const.
2962TEST(FieldForPointerTest, WorksForPointerToConst) {
2963 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2964
2965 AStruct a;
2966 EXPECT_TRUE(m.Matches(&a));
2967 a.x = -1;
2968 EXPECT_FALSE(m.Matches(&a));
2969}
2970
2971// Tests that Field() works when the argument is a pointer to non-const.
2972TEST(FieldForPointerTest, WorksForPointerToNonConst) {
2973 Matcher<AStruct*> m = Field(&AStruct::x, Ge(0));
2974
2975 AStruct a;
2976 EXPECT_TRUE(m.Matches(&a));
2977 a.x = -1;
2978 EXPECT_FALSE(m.Matches(&a));
2979}
2980
zhanyong.wan6953a722010-01-13 05:15:07 +00002981// Tests that Field() works when the argument is a reference to a const pointer.
2982TEST(FieldForPointerTest, WorksForReferenceToConstPointer) {
2983 Matcher<AStruct* const&> m = Field(&AStruct::x, Ge(0));
2984
2985 AStruct a;
2986 EXPECT_TRUE(m.Matches(&a));
2987 a.x = -1;
2988 EXPECT_FALSE(m.Matches(&a));
2989}
2990
shiqiane35fdd92008-12-10 05:08:54 +00002991// Tests that Field() does not match the NULL pointer.
2992TEST(FieldForPointerTest, DoesNotMatchNull) {
2993 Matcher<const AStruct*> m = Field(&AStruct::x, _);
2994 EXPECT_FALSE(m.Matches(NULL));
2995}
2996
2997// Tests that Field(&Foo::field, ...) works when the argument's type
2998// is a sub-type of const Foo*.
2999TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
3000 // Note that the matcher expects DerivedStruct but we say AStruct
3001 // inside Field().
3002 Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0));
3003
3004 DerivedStruct d;
3005 EXPECT_TRUE(m.Matches(&d));
3006 d.x = -1;
3007 EXPECT_FALSE(m.Matches(&d));
3008}
3009
3010// Tests that Field() can describe itself when used to match a pointer.
3011TEST(FieldForPointerTest, CanDescribeSelf) {
3012 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
3013
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003014 EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
3015 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003016}
3017
3018// Tests that Field() can explain the result of matching a pointer.
3019TEST(FieldForPointerTest, CanExplainMatchResult) {
3020 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
3021
3022 AStruct a;
3023 a.x = 1;
3024 EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(NULL)));
zhanyong.wan736baa82010-09-27 17:44:16 +00003025 EXPECT_EQ("which points to an object whose given field is 1" + OfType("int"),
3026 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003027
3028 m = Field(&AStruct::x, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003029 EXPECT_EQ("which points to an object whose given field is 1" + OfType("int") +
3030 ", which is 1 more than 0", Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003031}
3032
3033// A user-defined class for testing Property().
3034class AClass {
3035 public:
3036 AClass() : n_(0) {}
3037
3038 // A getter that returns a non-reference.
3039 int n() const { return n_; }
3040
3041 void set_n(int new_n) { n_ = new_n; }
3042
3043 // A getter that returns a reference to const.
3044 const string& s() const { return s_; }
3045
3046 void set_s(const string& new_s) { s_ = new_s; }
3047
3048 // A getter that returns a reference to non-const.
3049 double& x() const { return x_; }
3050 private:
3051 int n_;
3052 string s_;
3053
3054 static double x_;
3055};
3056
3057double AClass::x_ = 0.0;
3058
3059// A derived class for testing Property().
3060class DerivedClass : public AClass {
3061 private:
3062 int k_;
3063};
3064
3065// Tests that Property(&Foo::property, ...) works when property()
3066// returns a non-reference.
3067TEST(PropertyTest, WorksForNonReferenceProperty) {
3068 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3069
3070 AClass a;
3071 a.set_n(1);
3072 EXPECT_TRUE(m.Matches(a));
3073
3074 a.set_n(-1);
3075 EXPECT_FALSE(m.Matches(a));
3076}
3077
3078// Tests that Property(&Foo::property, ...) works when property()
3079// returns a reference to const.
3080TEST(PropertyTest, WorksForReferenceToConstProperty) {
3081 Matcher<const AClass&> m = Property(&AClass::s, StartsWith("hi"));
3082
3083 AClass a;
3084 a.set_s("hill");
3085 EXPECT_TRUE(m.Matches(a));
3086
3087 a.set_s("hole");
3088 EXPECT_FALSE(m.Matches(a));
3089}
3090
3091// Tests that Property(&Foo::property, ...) works when property()
3092// returns a reference to non-const.
3093TEST(PropertyTest, WorksForReferenceToNonConstProperty) {
3094 double x = 0.0;
3095 AClass a;
3096
3097 Matcher<const AClass&> m = Property(&AClass::x, Ref(x));
3098 EXPECT_FALSE(m.Matches(a));
3099
3100 m = Property(&AClass::x, Not(Ref(x)));
3101 EXPECT_TRUE(m.Matches(a));
3102}
3103
3104// Tests that Property(&Foo::property, ...) works when the argument is
3105// passed by value.
3106TEST(PropertyTest, WorksForByValueArgument) {
3107 Matcher<AClass> m = Property(&AClass::s, StartsWith("hi"));
3108
3109 AClass a;
3110 a.set_s("hill");
3111 EXPECT_TRUE(m.Matches(a));
3112
3113 a.set_s("hole");
3114 EXPECT_FALSE(m.Matches(a));
3115}
3116
3117// Tests that Property(&Foo::property, ...) works when the argument's
3118// type is a sub-type of Foo.
3119TEST(PropertyTest, WorksForArgumentOfSubType) {
3120 // The matcher expects a DerivedClass, but inside the Property() we
3121 // say AClass.
3122 Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0));
3123
3124 DerivedClass d;
3125 d.set_n(1);
3126 EXPECT_TRUE(m.Matches(d));
3127
3128 d.set_n(-1);
3129 EXPECT_FALSE(m.Matches(d));
3130}
3131
3132// Tests that Property(&Foo::property, m) works when property()'s type
3133// and m's argument type are compatible but different.
3134TEST(PropertyTest, WorksForCompatibleMatcherType) {
3135 // n() returns an int but the inner matcher expects a signed char.
3136 Matcher<const AClass&> m = Property(&AClass::n,
3137 Matcher<signed char>(Ge(0)));
3138
3139 AClass a;
3140 EXPECT_TRUE(m.Matches(a));
3141 a.set_n(-1);
3142 EXPECT_FALSE(m.Matches(a));
3143}
3144
3145// Tests that Property() can describe itself.
3146TEST(PropertyTest, CanDescribeSelf) {
3147 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3148
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003149 EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
3150 EXPECT_EQ("is an object whose given property isn't >= 0",
3151 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003152}
3153
3154// Tests that Property() can explain the match result.
3155TEST(PropertyTest, CanExplainMatchResult) {
3156 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3157
3158 AClass a;
3159 a.set_n(1);
zhanyong.wan736baa82010-09-27 17:44:16 +00003160 EXPECT_EQ("whose given property is 1" + OfType("int"), Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003161
3162 m = Property(&AClass::n, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003163 EXPECT_EQ(
3164 "whose given property is 1" + OfType("int") + ", which is 1 more than 0",
3165 Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003166}
3167
3168// Tests that Property() works when the argument is a pointer to const.
3169TEST(PropertyForPointerTest, WorksForPointerToConst) {
3170 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3171
3172 AClass a;
3173 a.set_n(1);
3174 EXPECT_TRUE(m.Matches(&a));
3175
3176 a.set_n(-1);
3177 EXPECT_FALSE(m.Matches(&a));
3178}
3179
3180// Tests that Property() works when the argument is a pointer to non-const.
3181TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
3182 Matcher<AClass*> m = Property(&AClass::s, StartsWith("hi"));
3183
3184 AClass a;
3185 a.set_s("hill");
3186 EXPECT_TRUE(m.Matches(&a));
3187
3188 a.set_s("hole");
3189 EXPECT_FALSE(m.Matches(&a));
3190}
3191
zhanyong.wan6953a722010-01-13 05:15:07 +00003192// Tests that Property() works when the argument is a reference to a
3193// const pointer.
3194TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) {
3195 Matcher<AClass* const&> m = Property(&AClass::s, StartsWith("hi"));
3196
3197 AClass a;
3198 a.set_s("hill");
3199 EXPECT_TRUE(m.Matches(&a));
3200
3201 a.set_s("hole");
3202 EXPECT_FALSE(m.Matches(&a));
3203}
3204
shiqiane35fdd92008-12-10 05:08:54 +00003205// Tests that Property() does not match the NULL pointer.
3206TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
3207 Matcher<const AClass*> m = Property(&AClass::x, _);
3208 EXPECT_FALSE(m.Matches(NULL));
3209}
3210
3211// Tests that Property(&Foo::property, ...) works when the argument's
3212// type is a sub-type of const Foo*.
3213TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
3214 // The matcher expects a DerivedClass, but inside the Property() we
3215 // say AClass.
3216 Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0));
3217
3218 DerivedClass d;
3219 d.set_n(1);
3220 EXPECT_TRUE(m.Matches(&d));
3221
3222 d.set_n(-1);
3223 EXPECT_FALSE(m.Matches(&d));
3224}
3225
3226// Tests that Property() can describe itself when used to match a pointer.
3227TEST(PropertyForPointerTest, 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 result of matching a pointer.
3236TEST(PropertyForPointerTest, CanExplainMatchResult) {
3237 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3238
3239 AClass a;
3240 a.set_n(1);
3241 EXPECT_EQ("", Explain(m, static_cast<const AClass*>(NULL)));
zhanyong.wan736baa82010-09-27 17:44:16 +00003242 EXPECT_EQ(
3243 "which points to an object whose given property is 1" + OfType("int"),
3244 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003245
3246 m = Property(&AClass::n, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003247 EXPECT_EQ("which points to an object whose given property is 1" +
3248 OfType("int") + ", which is 1 more than 0",
3249 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003250}
3251
3252// Tests ResultOf.
3253
3254// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3255// function pointer.
3256string IntToStringFunction(int input) { return input == 1 ? "foo" : "bar"; }
3257
3258TEST(ResultOfTest, WorksForFunctionPointers) {
3259 Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(string("foo")));
3260
3261 EXPECT_TRUE(matcher.Matches(1));
3262 EXPECT_FALSE(matcher.Matches(2));
3263}
3264
3265// Tests that ResultOf() can describe itself.
3266TEST(ResultOfTest, CanDescribeItself) {
3267 Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo"));
3268
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003269 EXPECT_EQ("is mapped by the given callable to a value that "
3270 "is equal to \"foo\"", Describe(matcher));
3271 EXPECT_EQ("is mapped by the given callable to a value that "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003272 "isn't equal to \"foo\"", DescribeNegation(matcher));
shiqiane35fdd92008-12-10 05:08:54 +00003273}
3274
3275// Tests that ResultOf() can explain the match result.
3276int IntFunction(int input) { return input == 42 ? 80 : 90; }
3277
3278TEST(ResultOfTest, CanExplainMatchResult) {
3279 Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
zhanyong.wan736baa82010-09-27 17:44:16 +00003280 EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int"),
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003281 Explain(matcher, 36));
shiqiane35fdd92008-12-10 05:08:54 +00003282
3283 matcher = ResultOf(&IntFunction, GreaterThan(85));
zhanyong.wan736baa82010-09-27 17:44:16 +00003284 EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int") +
3285 ", which is 5 more than 85", Explain(matcher, 36));
shiqiane35fdd92008-12-10 05:08:54 +00003286}
3287
3288// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3289// returns a non-reference.
3290TEST(ResultOfTest, WorksForNonReferenceResults) {
3291 Matcher<int> matcher = ResultOf(&IntFunction, Eq(80));
3292
3293 EXPECT_TRUE(matcher.Matches(42));
3294 EXPECT_FALSE(matcher.Matches(36));
3295}
3296
3297// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3298// returns a reference to non-const.
zhanyong.wan736baa82010-09-27 17:44:16 +00003299double& DoubleFunction(double& input) { return input; } // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00003300
zhanyong.wan736baa82010-09-27 17:44:16 +00003301Uncopyable& RefUncopyableFunction(Uncopyable& obj) { // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00003302 return obj;
3303}
3304
3305TEST(ResultOfTest, WorksForReferenceToNonConstResults) {
3306 double x = 3.14;
3307 double x2 = x;
3308 Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x));
3309
3310 EXPECT_TRUE(matcher.Matches(x));
3311 EXPECT_FALSE(matcher.Matches(x2));
3312
3313 // Test that ResultOf works with uncopyable objects
3314 Uncopyable obj(0);
3315 Uncopyable obj2(0);
3316 Matcher<Uncopyable&> matcher2 =
3317 ResultOf(&RefUncopyableFunction, Ref(obj));
3318
3319 EXPECT_TRUE(matcher2.Matches(obj));
3320 EXPECT_FALSE(matcher2.Matches(obj2));
3321}
3322
3323// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3324// returns a reference to const.
3325const string& StringFunction(const string& input) { return input; }
3326
3327TEST(ResultOfTest, WorksForReferenceToConstResults) {
3328 string s = "foo";
3329 string s2 = s;
3330 Matcher<const string&> matcher = ResultOf(&StringFunction, Ref(s));
3331
3332 EXPECT_TRUE(matcher.Matches(s));
3333 EXPECT_FALSE(matcher.Matches(s2));
3334}
3335
3336// Tests that ResultOf(f, m) works when f(x) and m's
3337// argument types are compatible but different.
3338TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {
3339 // IntFunction() returns int but the inner matcher expects a signed char.
3340 Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85)));
3341
3342 EXPECT_TRUE(matcher.Matches(36));
3343 EXPECT_FALSE(matcher.Matches(42));
3344}
3345
shiqiane35fdd92008-12-10 05:08:54 +00003346// Tests that the program aborts when ResultOf is passed
3347// a NULL function pointer.
3348TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +00003349 EXPECT_DEATH_IF_SUPPORTED(
zhanyong.wan736baa82010-09-27 17:44:16 +00003350 ResultOf(static_cast<string(*)(int dummy)>(NULL), Eq(string("foo"))),
shiqiane35fdd92008-12-10 05:08:54 +00003351 "NULL function pointer is passed into ResultOf\\(\\)\\.");
3352}
shiqiane35fdd92008-12-10 05:08:54 +00003353
3354// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3355// function reference.
3356TEST(ResultOfTest, WorksForFunctionReferences) {
3357 Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq("foo"));
3358 EXPECT_TRUE(matcher.Matches(1));
3359 EXPECT_FALSE(matcher.Matches(2));
3360}
3361
3362// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3363// function object.
3364struct Functor : public ::std::unary_function<int, string> {
3365 result_type operator()(argument_type input) const {
3366 return IntToStringFunction(input);
3367 }
3368};
3369
3370TEST(ResultOfTest, WorksForFunctors) {
3371 Matcher<int> matcher = ResultOf(Functor(), Eq(string("foo")));
3372
3373 EXPECT_TRUE(matcher.Matches(1));
3374 EXPECT_FALSE(matcher.Matches(2));
3375}
3376
3377// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3378// functor with more then one operator() defined. ResultOf() must work
3379// for each defined operator().
3380struct PolymorphicFunctor {
3381 typedef int result_type;
3382 int operator()(int n) { return n; }
3383 int operator()(const char* s) { return static_cast<int>(strlen(s)); }
3384};
3385
3386TEST(ResultOfTest, WorksForPolymorphicFunctors) {
3387 Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5));
3388
3389 EXPECT_TRUE(matcher_int.Matches(10));
3390 EXPECT_FALSE(matcher_int.Matches(2));
3391
3392 Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5));
3393
3394 EXPECT_TRUE(matcher_string.Matches("long string"));
3395 EXPECT_FALSE(matcher_string.Matches("shrt"));
3396}
3397
3398const int* ReferencingFunction(const int& n) { return &n; }
3399
3400struct ReferencingFunctor {
3401 typedef const int* result_type;
3402 result_type operator()(const int& n) { return &n; }
3403};
3404
3405TEST(ResultOfTest, WorksForReferencingCallables) {
3406 const int n = 1;
3407 const int n2 = 1;
3408 Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n));
3409 EXPECT_TRUE(matcher2.Matches(n));
3410 EXPECT_FALSE(matcher2.Matches(n2));
3411
3412 Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n));
3413 EXPECT_TRUE(matcher3.Matches(n));
3414 EXPECT_FALSE(matcher3.Matches(n2));
3415}
3416
shiqiane35fdd92008-12-10 05:08:54 +00003417class DivisibleByImpl {
3418 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003419 explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {}
shiqiane35fdd92008-12-10 05:08:54 +00003420
zhanyong.wandb22c222010-01-28 21:52:29 +00003421 // For testing using ExplainMatchResultTo() with polymorphic matchers.
shiqiane35fdd92008-12-10 05:08:54 +00003422 template <typename T>
zhanyong.wandb22c222010-01-28 21:52:29 +00003423 bool MatchAndExplain(const T& n, MatchResultListener* listener) const {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003424 *listener << "which is " << (n % divider_) << " modulo "
zhanyong.wandb22c222010-01-28 21:52:29 +00003425 << divider_;
shiqiane35fdd92008-12-10 05:08:54 +00003426 return (n % divider_) == 0;
3427 }
3428
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003429 void DescribeTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +00003430 *os << "is divisible by " << divider_;
3431 }
3432
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003433 void DescribeNegationTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +00003434 *os << "is not divisible by " << divider_;
3435 }
3436
zhanyong.wan32de5f52009-12-23 00:13:23 +00003437 void set_divider(int a_divider) { divider_ = a_divider; }
shiqiane35fdd92008-12-10 05:08:54 +00003438 int divider() const { return divider_; }
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003439
shiqiane35fdd92008-12-10 05:08:54 +00003440 private:
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003441 int divider_;
shiqiane35fdd92008-12-10 05:08:54 +00003442};
3443
shiqiane35fdd92008-12-10 05:08:54 +00003444PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) {
3445 return MakePolymorphicMatcher(DivisibleByImpl(n));
3446}
3447
3448// Tests that when AllOf() fails, only the first failing matcher is
3449// asked to explain why.
3450TEST(ExplainMatchResultTest, AllOf_False_False) {
3451 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003452 EXPECT_EQ("which is 1 modulo 4", Explain(m, 5));
shiqiane35fdd92008-12-10 05:08:54 +00003453}
3454
3455// Tests that when AllOf() fails, only the first failing matcher is
3456// asked to explain why.
3457TEST(ExplainMatchResultTest, AllOf_False_True) {
3458 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003459 EXPECT_EQ("which is 2 modulo 4", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003460}
3461
3462// Tests that when AllOf() fails, only the first failing matcher is
3463// asked to explain why.
3464TEST(ExplainMatchResultTest, AllOf_True_False) {
3465 const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003466 EXPECT_EQ("which is 2 modulo 3", Explain(m, 5));
shiqiane35fdd92008-12-10 05:08:54 +00003467}
3468
3469// Tests that when AllOf() succeeds, all matchers are asked to explain
3470// why.
3471TEST(ExplainMatchResultTest, AllOf_True_True) {
3472 const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003473 EXPECT_EQ("which is 0 modulo 2, and which is 0 modulo 3", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003474}
3475
3476TEST(ExplainMatchResultTest, AllOf_True_True_2) {
3477 const Matcher<int> m = AllOf(Ge(2), Le(3));
3478 EXPECT_EQ("", Explain(m, 2));
3479}
3480
3481TEST(ExplainmatcherResultTest, MonomorphicMatcher) {
3482 const Matcher<int> m = GreaterThan(5);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003483 EXPECT_EQ("which is 1 more than 5", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003484}
3485
3486// The following two tests verify that values without a public copy
3487// ctor can be used as arguments to matchers like Eq(), Ge(), and etc
3488// with the help of ByRef().
3489
3490class NotCopyable {
3491 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003492 explicit NotCopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00003493
3494 int value() const { return value_; }
3495
3496 bool operator==(const NotCopyable& rhs) const {
3497 return value() == rhs.value();
3498 }
3499
3500 bool operator>=(const NotCopyable& rhs) const {
3501 return value() >= rhs.value();
3502 }
3503 private:
3504 int value_;
3505
3506 GTEST_DISALLOW_COPY_AND_ASSIGN_(NotCopyable);
3507};
3508
3509TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) {
3510 const NotCopyable const_value1(1);
3511 const Matcher<const NotCopyable&> m = Eq(ByRef(const_value1));
3512
3513 const NotCopyable n1(1), n2(2);
3514 EXPECT_TRUE(m.Matches(n1));
3515 EXPECT_FALSE(m.Matches(n2));
3516}
3517
3518TEST(ByRefTest, AllowsNotCopyableValueInMatchers) {
3519 NotCopyable value2(2);
3520 const Matcher<NotCopyable&> m = Ge(ByRef(value2));
3521
3522 NotCopyable n1(1), n2(2);
3523 EXPECT_FALSE(m.Matches(n1));
3524 EXPECT_TRUE(m.Matches(n2));
3525}
3526
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003527#if GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003528// Tests ContainerEq with different container types, and
3529// different element types.
3530
3531template <typename T>
zhanyong.wanb8243162009-06-04 05:48:20 +00003532class ContainerEqTest : public testing::Test {};
zhanyong.wan6a896b52009-01-16 01:13:50 +00003533
3534typedef testing::Types<
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003535 set<int>,
3536 vector<size_t>,
3537 multiset<size_t>,
3538 list<int> >
zhanyong.wan6a896b52009-01-16 01:13:50 +00003539 ContainerEqTestTypes;
3540
3541TYPED_TEST_CASE(ContainerEqTest, ContainerEqTestTypes);
3542
3543// Tests that the filled container is equal to itself.
3544TYPED_TEST(ContainerEqTest, EqualsSelf) {
3545 static const int vals[] = {1, 1, 2, 3, 5, 8};
3546 TypeParam my_set(vals, vals + 6);
3547 const Matcher<TypeParam> m = ContainerEq(my_set);
3548 EXPECT_TRUE(m.Matches(my_set));
3549 EXPECT_EQ("", Explain(m, my_set));
3550}
3551
3552// Tests that missing values are reported.
3553TYPED_TEST(ContainerEqTest, ValueMissing) {
3554 static const int vals[] = {1, 1, 2, 3, 5, 8};
3555 static const int test_vals[] = {2, 1, 8, 5};
3556 TypeParam my_set(vals, vals + 6);
3557 TypeParam test_set(test_vals, test_vals + 4);
3558 const Matcher<TypeParam> m = ContainerEq(my_set);
3559 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003560 EXPECT_EQ("which doesn't have these expected elements: 3",
3561 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003562}
3563
3564// Tests that added values are reported.
3565TYPED_TEST(ContainerEqTest, ValueAdded) {
3566 static const int vals[] = {1, 1, 2, 3, 5, 8};
3567 static const int test_vals[] = {1, 2, 3, 5, 8, 46};
3568 TypeParam my_set(vals, vals + 6);
3569 TypeParam test_set(test_vals, test_vals + 6);
3570 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3571 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003572 EXPECT_EQ("which has these unexpected elements: 46", Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003573}
3574
3575// Tests that added and missing values are reported together.
3576TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) {
3577 static const int vals[] = {1, 1, 2, 3, 5, 8};
3578 static const int test_vals[] = {1, 2, 3, 8, 46};
3579 TypeParam my_set(vals, vals + 6);
3580 TypeParam test_set(test_vals, test_vals + 5);
3581 const Matcher<TypeParam> m = ContainerEq(my_set);
3582 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003583 EXPECT_EQ("which has these unexpected elements: 46,\n"
3584 "and doesn't have these expected elements: 5",
3585 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003586}
3587
3588// Tests duplicated value -- expect no explanation.
3589TYPED_TEST(ContainerEqTest, DuplicateDifference) {
3590 static const int vals[] = {1, 1, 2, 3, 5, 8};
3591 static const int test_vals[] = {1, 2, 3, 5, 8};
3592 TypeParam my_set(vals, vals + 6);
3593 TypeParam test_set(test_vals, test_vals + 5);
3594 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3595 // Depending on the container, match may be true or false
3596 // But in any case there should be no explanation.
3597 EXPECT_EQ("", Explain(m, test_set));
3598}
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003599#endif // GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003600
3601// Tests that mutliple missing values are reported.
3602// Using just vector here, so order is predicatble.
3603TEST(ContainerEqExtraTest, MultipleValuesMissing) {
3604 static const int vals[] = {1, 1, 2, 3, 5, 8};
3605 static const int test_vals[] = {2, 1, 5};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003606 vector<int> my_set(vals, vals + 6);
3607 vector<int> test_set(test_vals, test_vals + 3);
3608 const Matcher<vector<int> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003609 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003610 EXPECT_EQ("which doesn't have these expected elements: 3, 8",
3611 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003612}
3613
3614// Tests that added values are reported.
3615// Using just vector here, so order is predicatble.
3616TEST(ContainerEqExtraTest, MultipleValuesAdded) {
3617 static const int vals[] = {1, 1, 2, 3, 5, 8};
3618 static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003619 list<size_t> my_set(vals, vals + 6);
3620 list<size_t> test_set(test_vals, test_vals + 7);
3621 const Matcher<const list<size_t>&> m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003622 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003623 EXPECT_EQ("which has these unexpected elements: 92, 46",
3624 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003625}
3626
3627// Tests that added and missing values are reported together.
3628TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) {
3629 static const int vals[] = {1, 1, 2, 3, 5, 8};
3630 static const int test_vals[] = {1, 2, 3, 92, 46};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003631 list<size_t> my_set(vals, vals + 6);
3632 list<size_t> test_set(test_vals, test_vals + 5);
3633 const Matcher<const list<size_t> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003634 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003635 EXPECT_EQ("which has these unexpected elements: 92, 46,\n"
3636 "and doesn't have these expected elements: 5, 8",
zhanyong.wan6a896b52009-01-16 01:13:50 +00003637 Explain(m, test_set));
3638}
3639
3640// Tests to see that duplicate elements are detected,
3641// but (as above) not reported in the explanation.
3642TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) {
3643 static const int vals[] = {1, 1, 2, 3, 5, 8};
3644 static const int test_vals[] = {1, 2, 3, 5, 8};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003645 vector<int> my_set(vals, vals + 6);
3646 vector<int> test_set(test_vals, test_vals + 5);
3647 const Matcher<vector<int> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003648 EXPECT_TRUE(m.Matches(my_set));
3649 EXPECT_FALSE(m.Matches(test_set));
3650 // There is nothing to report when both sets contain all the same values.
3651 EXPECT_EQ("", Explain(m, test_set));
3652}
3653
3654// Tests that ContainerEq works for non-trivial associative containers,
3655// like maps.
3656TEST(ContainerEqExtraTest, WorksForMaps) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003657 map<int, std::string> my_map;
zhanyong.wan6a896b52009-01-16 01:13:50 +00003658 my_map[0] = "a";
3659 my_map[1] = "b";
3660
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003661 map<int, std::string> test_map;
zhanyong.wan6a896b52009-01-16 01:13:50 +00003662 test_map[0] = "aa";
3663 test_map[1] = "b";
3664
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003665 const Matcher<const map<int, std::string>&> m = ContainerEq(my_map);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003666 EXPECT_TRUE(m.Matches(my_map));
3667 EXPECT_FALSE(m.Matches(test_map));
3668
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003669 EXPECT_EQ("which has these unexpected elements: (0, \"aa\"),\n"
3670 "and doesn't have these expected elements: (0, \"a\")",
zhanyong.wan6a896b52009-01-16 01:13:50 +00003671 Explain(m, test_map));
3672}
3673
zhanyong.wanb8243162009-06-04 05:48:20 +00003674TEST(ContainerEqExtraTest, WorksForNativeArray) {
3675 int a1[] = { 1, 2, 3 };
3676 int a2[] = { 1, 2, 3 };
3677 int b[] = { 1, 2, 4 };
3678
3679 EXPECT_THAT(a1, ContainerEq(a2));
3680 EXPECT_THAT(a1, Not(ContainerEq(b)));
3681}
3682
3683TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) {
3684 const char a1[][3] = { "hi", "lo" };
3685 const char a2[][3] = { "hi", "lo" };
3686 const char b[][3] = { "lo", "hi" };
3687
3688 // Tests using ContainerEq() in the first dimension.
3689 EXPECT_THAT(a1, ContainerEq(a2));
3690 EXPECT_THAT(a1, Not(ContainerEq(b)));
3691
3692 // Tests using ContainerEq() in the second dimension.
3693 EXPECT_THAT(a1, ElementsAre(ContainerEq(a2[0]), ContainerEq(a2[1])));
3694 EXPECT_THAT(a1, ElementsAre(Not(ContainerEq(b[0])), ContainerEq(a2[1])));
3695}
3696
3697TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) {
3698 const int a1[] = { 1, 2, 3 };
3699 const int a2[] = { 1, 2, 3 };
3700 const int b[] = { 1, 2, 3, 4 };
3701
zhanyong.wan2661c682009-06-09 05:42:12 +00003702 const int* const p1 = a1;
3703 EXPECT_THAT(make_tuple(p1, 3), ContainerEq(a2));
3704 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(b)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003705
3706 const int c[] = { 1, 3, 2 };
zhanyong.wan2661c682009-06-09 05:42:12 +00003707 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(c)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003708}
3709
3710TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) {
3711 std::string a1[][3] = {
3712 { "hi", "hello", "ciao" },
3713 { "bye", "see you", "ciao" }
3714 };
3715
3716 std::string a2[][3] = {
3717 { "hi", "hello", "ciao" },
3718 { "bye", "see you", "ciao" }
3719 };
3720
3721 const Matcher<const std::string(&)[2][3]> m = ContainerEq(a2);
3722 EXPECT_THAT(a1, m);
3723
3724 a2[0][0] = "ha";
3725 EXPECT_THAT(a1, m);
3726}
3727
zhanyong.wan736baa82010-09-27 17:44:16 +00003728// Tests IsReadableTypeName().
3729
3730TEST(IsReadableTypeNameTest, ReturnsTrueForShortNames) {
3731 EXPECT_TRUE(IsReadableTypeName("int"));
3732 EXPECT_TRUE(IsReadableTypeName("const unsigned char*"));
3733 EXPECT_TRUE(IsReadableTypeName("MyMap<int, void*>"));
3734 EXPECT_TRUE(IsReadableTypeName("void (*)(int, bool)"));
3735}
3736
3737TEST(IsReadableTypeNameTest, ReturnsTrueForLongNonTemplateNonFunctionNames) {
3738 EXPECT_TRUE(IsReadableTypeName("my_long_namespace::MyClassName"));
3739 EXPECT_TRUE(IsReadableTypeName("int [5][6][7][8][9][10][11]"));
3740 EXPECT_TRUE(IsReadableTypeName("my_namespace::MyOuterClass::MyInnerClass"));
3741}
3742
3743TEST(IsReadableTypeNameTest, ReturnsFalseForLongTemplateNames) {
3744 EXPECT_FALSE(
3745 IsReadableTypeName("basic_string<char, std::char_traits<char> >"));
3746 EXPECT_FALSE(IsReadableTypeName("std::vector<int, std::alloc_traits<int> >"));
3747}
3748
3749TEST(IsReadableTypeNameTest, ReturnsFalseForLongFunctionTypeNames) {
3750 EXPECT_FALSE(IsReadableTypeName("void (&)(int, bool, char, float)"));
3751}
3752
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003753// Tests JoinAsTuple().
3754
3755TEST(JoinAsTupleTest, JoinsEmptyTuple) {
3756 EXPECT_EQ("", JoinAsTuple(Strings()));
3757}
3758
3759TEST(JoinAsTupleTest, JoinsOneTuple) {
3760 const char* fields[] = { "1" };
3761 EXPECT_EQ("1", JoinAsTuple(Strings(fields, fields + 1)));
3762}
3763
3764TEST(JoinAsTupleTest, JoinsTwoTuple) {
3765 const char* fields[] = { "1", "a" };
3766 EXPECT_EQ("(1, a)", JoinAsTuple(Strings(fields, fields + 2)));
3767}
3768
3769TEST(JoinAsTupleTest, JoinsTenTuple) {
3770 const char* fields[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
3771 EXPECT_EQ("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)",
3772 JoinAsTuple(Strings(fields, fields + 10)));
3773}
3774
3775// Tests FormatMatcherDescription().
3776
3777TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) {
3778 EXPECT_EQ("is even",
zhanyong.wanb4140802010-06-08 22:53:57 +00003779 FormatMatcherDescription(false, "IsEven", Strings()));
3780 EXPECT_EQ("not (is even)",
3781 FormatMatcherDescription(true, "IsEven", Strings()));
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003782
3783 const char* params[] = { "5" };
3784 EXPECT_EQ("equals 5",
zhanyong.wanb4140802010-06-08 22:53:57 +00003785 FormatMatcherDescription(false, "Equals",
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003786 Strings(params, params + 1)));
3787
3788 const char* params2[] = { "5", "8" };
3789 EXPECT_EQ("is in range (5, 8)",
zhanyong.wanb4140802010-06-08 22:53:57 +00003790 FormatMatcherDescription(false, "IsInRange",
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003791 Strings(params2, params2 + 2)));
3792}
3793
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003794// Tests PolymorphicMatcher::mutable_impl().
3795TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {
3796 PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
3797 DivisibleByImpl& impl = m.mutable_impl();
3798 EXPECT_EQ(42, impl.divider());
3799
3800 impl.set_divider(0);
3801 EXPECT_EQ(0, m.mutable_impl().divider());
3802}
3803
3804// Tests PolymorphicMatcher::impl().
3805TEST(PolymorphicMatcherTest, CanAccessImpl) {
3806 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
3807 const DivisibleByImpl& impl = m.impl();
3808 EXPECT_EQ(42, impl.divider());
3809}
3810
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003811TEST(MatcherTupleTest, ExplainsMatchFailure) {
3812 stringstream ss1;
3813 ExplainMatchFailureTupleTo(make_tuple(Matcher<char>(Eq('a')), GreaterThan(5)),
3814 make_tuple('a', 10), &ss1);
3815 EXPECT_EQ("", ss1.str()); // Successful match.
3816
3817 stringstream ss2;
3818 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
3819 make_tuple(2, 'b'), &ss2);
3820 EXPECT_EQ(" Expected arg #0: is > 5\n"
3821 " Actual: 2, which is 3 less than 5\n"
zhanyong.wand60c5f42010-07-21 22:21:07 +00003822 " Expected arg #1: is equal to 'a' (97, 0x61)\n"
3823 " Actual: 'b' (98, 0x62)\n",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003824 ss2.str()); // Failed match where both arguments need explanation.
3825
3826 stringstream ss3;
3827 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
3828 make_tuple(2, 'a'), &ss3);
3829 EXPECT_EQ(" Expected arg #0: is > 5\n"
3830 " Actual: 2, which is 3 less than 5\n",
3831 ss3.str()); // Failed match where only one argument needs
3832 // explanation.
3833}
3834
zhanyong.wan33605ba2010-04-22 23:37:47 +00003835// Tests Each().
3836
3837TEST(EachTest, ExplainsMatchResultCorrectly) {
3838 set<int> a; // empty
3839
3840 Matcher<set<int> > m = Each(2);
3841 EXPECT_EQ("", Explain(m, a));
3842
zhanyong.wan736baa82010-09-27 17:44:16 +00003843 Matcher<const int(&)[1]> n = Each(1); // NOLINT
zhanyong.wan33605ba2010-04-22 23:37:47 +00003844
3845 const int b[1] = { 1 };
3846 EXPECT_EQ("", Explain(n, b));
3847
3848 n = Each(3);
3849 EXPECT_EQ("whose element #0 doesn't match", Explain(n, b));
3850
3851 a.insert(1);
3852 a.insert(2);
3853 a.insert(3);
3854 m = Each(GreaterThan(0));
3855 EXPECT_EQ("", Explain(m, a));
3856
3857 m = Each(GreaterThan(10));
3858 EXPECT_EQ("whose element #0 doesn't match, which is 9 less than 10",
3859 Explain(m, a));
3860}
3861
3862TEST(EachTest, DescribesItselfCorrectly) {
3863 Matcher<vector<int> > m = Each(1);
3864 EXPECT_EQ("only contains elements that is equal to 1", Describe(m));
3865
3866 Matcher<vector<int> > m2 = Not(m);
3867 EXPECT_EQ("contains some element that isn't equal to 1", Describe(m2));
3868}
3869
3870TEST(EachTest, MatchesVectorWhenAllElementsMatch) {
3871 vector<int> some_vector;
3872 EXPECT_THAT(some_vector, Each(1));
3873 some_vector.push_back(3);
3874 EXPECT_THAT(some_vector, Not(Each(1)));
3875 EXPECT_THAT(some_vector, Each(3));
3876 some_vector.push_back(1);
3877 some_vector.push_back(2);
3878 EXPECT_THAT(some_vector, Not(Each(3)));
3879 EXPECT_THAT(some_vector, Each(Lt(3.5)));
3880
3881 vector<string> another_vector;
3882 another_vector.push_back("fee");
3883 EXPECT_THAT(another_vector, Each(string("fee")));
3884 another_vector.push_back("fie");
3885 another_vector.push_back("foe");
3886 another_vector.push_back("fum");
3887 EXPECT_THAT(another_vector, Not(Each(string("fee"))));
3888}
3889
3890TEST(EachTest, MatchesMapWhenAllElementsMatch) {
3891 map<const char*, int> my_map;
3892 const char* bar = "a string";
3893 my_map[bar] = 2;
3894 EXPECT_THAT(my_map, Each(make_pair(bar, 2)));
3895
3896 map<string, int> another_map;
3897 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1)));
3898 another_map["fee"] = 1;
3899 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1)));
3900 another_map["fie"] = 2;
3901 another_map["foe"] = 3;
3902 another_map["fum"] = 4;
3903 EXPECT_THAT(another_map, Not(Each(make_pair(string("fee"), 1))));
3904 EXPECT_THAT(another_map, Not(Each(make_pair(string("fum"), 1))));
3905 EXPECT_THAT(another_map, Each(Pair(_, Gt(0))));
3906}
3907
3908TEST(EachTest, AcceptsMatcher) {
3909 const int a[] = { 1, 2, 3 };
3910 EXPECT_THAT(a, Each(Gt(0)));
3911 EXPECT_THAT(a, Not(Each(Gt(1))));
3912}
3913
3914TEST(EachTest, WorksForNativeArrayAsTuple) {
3915 const int a[] = { 1, 2 };
3916 const int* const pointer = a;
3917 EXPECT_THAT(make_tuple(pointer, 2), Each(Gt(0)));
3918 EXPECT_THAT(make_tuple(pointer, 2), Not(Each(Gt(1))));
3919}
3920
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003921// For testing Pointwise().
3922class IsHalfOfMatcher {
3923 public:
3924 template <typename T1, typename T2>
3925 bool MatchAndExplain(const tuple<T1, T2>& a_pair,
3926 MatchResultListener* listener) const {
3927 if (get<0>(a_pair) == get<1>(a_pair)/2) {
3928 *listener << "where the second is " << get<1>(a_pair);
3929 return true;
3930 } else {
3931 *listener << "where the second/2 is " << get<1>(a_pair)/2;
3932 return false;
3933 }
3934 }
3935
3936 void DescribeTo(ostream* os) const {
3937 *os << "are a pair where the first is half of the second";
3938 }
3939
3940 void DescribeNegationTo(ostream* os) const {
3941 *os << "are a pair where the first isn't half of the second";
3942 }
3943};
3944
3945PolymorphicMatcher<IsHalfOfMatcher> IsHalfOf() {
3946 return MakePolymorphicMatcher(IsHalfOfMatcher());
3947}
3948
3949TEST(PointwiseTest, DescribesSelf) {
3950 vector<int> rhs;
3951 rhs.push_back(1);
3952 rhs.push_back(2);
3953 rhs.push_back(3);
3954 const Matcher<const vector<int>&> m = Pointwise(IsHalfOf(), rhs);
3955 EXPECT_EQ("contains 3 values, where each value and its corresponding value "
3956 "in { 1, 2, 3 } are a pair where the first is half of the second",
3957 Describe(m));
3958 EXPECT_EQ("doesn't contain exactly 3 values, or contains a value x at some "
3959 "index i where x and the i-th value of { 1, 2, 3 } are a pair "
3960 "where the first isn't half of the second",
3961 DescribeNegation(m));
3962}
3963
3964TEST(PointwiseTest, MakesCopyOfRhs) {
3965 list<signed char> rhs;
3966 rhs.push_back(2);
3967 rhs.push_back(4);
3968
3969 int lhs[] = { 1, 2 };
3970 const Matcher<const int (&)[2]> m = Pointwise(IsHalfOf(), rhs);
3971 EXPECT_THAT(lhs, m);
3972
3973 // Changing rhs now shouldn't affect m, which made a copy of rhs.
3974 rhs.push_back(6);
3975 EXPECT_THAT(lhs, m);
3976}
3977
3978TEST(PointwiseTest, WorksForLhsNativeArray) {
3979 const int lhs[] = { 1, 2, 3 };
3980 vector<int> rhs;
3981 rhs.push_back(2);
3982 rhs.push_back(4);
3983 rhs.push_back(6);
3984 EXPECT_THAT(lhs, Pointwise(Lt(), rhs));
3985 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
3986}
3987
3988TEST(PointwiseTest, WorksForRhsNativeArray) {
3989 const int rhs[] = { 1, 2, 3 };
3990 vector<int> lhs;
3991 lhs.push_back(2);
3992 lhs.push_back(4);
3993 lhs.push_back(6);
3994 EXPECT_THAT(lhs, Pointwise(Gt(), rhs));
3995 EXPECT_THAT(lhs, Not(Pointwise(Lt(), rhs)));
3996}
3997
3998TEST(PointwiseTest, RejectsWrongSize) {
3999 const double lhs[2] = { 1, 2 };
4000 const int rhs[1] = { 0 };
4001 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
4002 EXPECT_EQ("which contains 2 values",
4003 Explain(Pointwise(Gt(), rhs), lhs));
4004
4005 const int rhs2[3] = { 0, 1, 2 };
4006 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs2)));
4007}
4008
4009TEST(PointwiseTest, RejectsWrongContent) {
4010 const double lhs[3] = { 1, 2, 3 };
4011 const int rhs[3] = { 2, 6, 4 };
4012 EXPECT_THAT(lhs, Not(Pointwise(IsHalfOf(), rhs)));
4013 EXPECT_EQ("where the value pair (2, 6) at index #1 don't match, "
4014 "where the second/2 is 3",
4015 Explain(Pointwise(IsHalfOf(), rhs), lhs));
4016}
4017
4018TEST(PointwiseTest, AcceptsCorrectContent) {
4019 const double lhs[3] = { 1, 2, 3 };
4020 const int rhs[3] = { 2, 4, 6 };
4021 EXPECT_THAT(lhs, Pointwise(IsHalfOf(), rhs));
4022 EXPECT_EQ("", Explain(Pointwise(IsHalfOf(), rhs), lhs));
4023}
4024
4025TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) {
4026 const double lhs[3] = { 1, 2, 3 };
4027 const int rhs[3] = { 2, 4, 6 };
4028 const Matcher<tuple<const double&, const int&> > m1 = IsHalfOf();
4029 EXPECT_THAT(lhs, Pointwise(m1, rhs));
4030 EXPECT_EQ("", Explain(Pointwise(m1, rhs), lhs));
4031
4032 // This type works as a tuple<const double&, const int&> can be
4033 // implicitly cast to tuple<double, int>.
4034 const Matcher<tuple<double, int> > m2 = IsHalfOf();
4035 EXPECT_THAT(lhs, Pointwise(m2, rhs));
4036 EXPECT_EQ("", Explain(Pointwise(m2, rhs), lhs));
4037}
4038
shiqiane35fdd92008-12-10 05:08:54 +00004039} // namespace gmock_matchers_test
4040} // namespace testing