blob: dd2534f02d7f07a7cd06878c02693db938a0e681 [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
36#include <gmock/gmock-matchers.h>
37
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>
shiqiane35fdd92008-12-10 05:08:54 +000048#include <gmock/gmock.h>
49#include <gtest/gtest.h>
50#include <gtest/gtest-spi.h>
51
52namespace testing {
zhanyong.wan4a5330d2009-02-19 00:36:44 +000053
54namespace internal {
zhanyong.wan4a5330d2009-02-19 00:36:44 +000055string 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.wan4a5330d2009-02-19 00:36:44 +0000126using testing::internal::JoinAsTuple;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000127using testing::internal::RE;
zhanyong.wana862f1d2010-03-15 21:23:04 +0000128using testing::internal::StreamMatchResultListener;
shiqiane35fdd92008-12-10 05:08:54 +0000129using testing::internal::String;
zhanyong.wan34b034c2010-03-05 21:23:23 +0000130using testing::internal::StringMatchResultListener;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000131using testing::internal::Strings;
vladlosev79b83502009-11-18 00:43:37 +0000132using testing::internal::linked_ptr;
vladloseve56daa72009-11-18 01:08:08 +0000133using testing::internal::scoped_ptr;
shiqiane35fdd92008-12-10 05:08:54 +0000134using testing::internal::string;
135
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000136// For testing ExplainMatchResultTo().
137class GreaterThanMatcher : public MatcherInterface<int> {
138 public:
139 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
140
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000141 virtual void DescribeTo(ostream* os) const {
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000142 *os << "is > " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000143 }
144
zhanyong.wandb22c222010-01-28 21:52:29 +0000145 virtual bool MatchAndExplain(int lhs,
146 MatchResultListener* listener) const {
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000147 const int diff = lhs - rhs_;
148 if (diff > 0) {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000149 *listener << "which is " << diff << " more than " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000150 } else if (diff == 0) {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000151 *listener << "which is the same as " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000152 } else {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000153 *listener << "which is " << -diff << " less than " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000154 }
zhanyong.wandb22c222010-01-28 21:52:29 +0000155
156 return lhs > rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000157 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000158
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000159 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000160 int rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000161};
162
163Matcher<int> GreaterThan(int n) {
164 return MakeMatcher(new GreaterThanMatcher(n));
165}
166
shiqiane35fdd92008-12-10 05:08:54 +0000167// Returns the description of the given matcher.
168template <typename T>
169string Describe(const Matcher<T>& m) {
170 stringstream ss;
171 m.DescribeTo(&ss);
172 return ss.str();
173}
174
175// Returns the description of the negation of the given matcher.
176template <typename T>
177string DescribeNegation(const Matcher<T>& m) {
178 stringstream ss;
179 m.DescribeNegationTo(&ss);
180 return ss.str();
181}
182
183// Returns the reason why x matches, or doesn't match, m.
184template <typename MatcherType, typename Value>
185string Explain(const MatcherType& m, const Value& x) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000186 StringMatchResultListener listener;
187 ExplainMatchResult(m, x, &listener);
188 return listener.str();
shiqiane35fdd92008-12-10 05:08:54 +0000189}
190
zhanyong.wana862f1d2010-03-15 21:23:04 +0000191TEST(MatchResultListenerTest, StreamingWorks) {
192 StringMatchResultListener listener;
193 listener << "hi" << 5;
194 EXPECT_EQ("hi5", listener.str());
195
196 // Streaming shouldn't crash when the underlying ostream is NULL.
197 DummyMatchResultListener dummy;
198 dummy << "hi" << 5;
199}
200
201TEST(MatchResultListenerTest, CanAccessUnderlyingStream) {
202 EXPECT_TRUE(DummyMatchResultListener().stream() == NULL);
203 EXPECT_TRUE(StreamMatchResultListener(NULL).stream() == NULL);
204
205 EXPECT_EQ(&std::cout, StreamMatchResultListener(&std::cout).stream());
206}
207
208TEST(MatchResultListenerTest, IsInterestedWorks) {
209 EXPECT_TRUE(StringMatchResultListener().IsInterested());
210 EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested());
211
212 EXPECT_FALSE(DummyMatchResultListener().IsInterested());
213 EXPECT_FALSE(StreamMatchResultListener(NULL).IsInterested());
214}
215
shiqiane35fdd92008-12-10 05:08:54 +0000216// Makes sure that the MatcherInterface<T> interface doesn't
217// change.
218class EvenMatcherImpl : public MatcherInterface<int> {
219 public:
zhanyong.wandb22c222010-01-28 21:52:29 +0000220 virtual bool MatchAndExplain(int x,
221 MatchResultListener* /* listener */) const {
222 return x % 2 == 0;
223 }
shiqiane35fdd92008-12-10 05:08:54 +0000224
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000225 virtual void DescribeTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +0000226 *os << "is an even number";
227 }
228
229 // We deliberately don't define DescribeNegationTo() and
230 // ExplainMatchResultTo() here, to make sure the definition of these
231 // two methods is optional.
232};
233
zhanyong.wana862f1d2010-03-15 21:23:04 +0000234// Makes sure that the MatcherInterface API doesn't change.
235TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) {
shiqiane35fdd92008-12-10 05:08:54 +0000236 EvenMatcherImpl m;
237}
238
zhanyong.wan82113312010-01-08 21:55:40 +0000239// Tests implementing a monomorphic matcher using MatchAndExplain().
240
241class NewEvenMatcherImpl : public MatcherInterface<int> {
242 public:
243 virtual bool MatchAndExplain(int x, MatchResultListener* listener) const {
244 const bool match = x % 2 == 0;
245 // Verifies that we can stream to a listener directly.
246 *listener << "value % " << 2;
247 if (listener->stream() != NULL) {
248 // Verifies that we can stream to a listener's underlying stream
249 // too.
250 *listener->stream() << " == " << (x % 2);
251 }
252 return match;
253 }
254
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000255 virtual void DescribeTo(ostream* os) const {
zhanyong.wan82113312010-01-08 21:55:40 +0000256 *os << "is an even number";
257 }
258};
259
260TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {
261 Matcher<int> m = MakeMatcher(new NewEvenMatcherImpl);
262 EXPECT_TRUE(m.Matches(2));
263 EXPECT_FALSE(m.Matches(3));
264 EXPECT_EQ("value % 2 == 0", Explain(m, 2));
265 EXPECT_EQ("value % 2 == 1", Explain(m, 3));
266}
267
shiqiane35fdd92008-12-10 05:08:54 +0000268// Tests default-constructing a matcher.
269TEST(MatcherTest, CanBeDefaultConstructed) {
270 Matcher<double> m;
271}
272
273// Tests that Matcher<T> can be constructed from a MatcherInterface<T>*.
274TEST(MatcherTest, CanBeConstructedFromMatcherInterface) {
275 const MatcherInterface<int>* impl = new EvenMatcherImpl;
276 Matcher<int> m(impl);
277 EXPECT_TRUE(m.Matches(4));
278 EXPECT_FALSE(m.Matches(5));
279}
280
281// Tests that value can be used in place of Eq(value).
282TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) {
283 Matcher<int> m1 = 5;
284 EXPECT_TRUE(m1.Matches(5));
285 EXPECT_FALSE(m1.Matches(6));
286}
287
288// Tests that NULL can be used in place of Eq(NULL).
289TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) {
290 Matcher<int*> m1 = NULL;
291 EXPECT_TRUE(m1.Matches(NULL));
292 int n = 0;
293 EXPECT_FALSE(m1.Matches(&n));
294}
295
296// Tests that matchers are copyable.
297TEST(MatcherTest, IsCopyable) {
298 // Tests the copy constructor.
299 Matcher<bool> m1 = Eq(false);
300 EXPECT_TRUE(m1.Matches(false));
301 EXPECT_FALSE(m1.Matches(true));
302
303 // Tests the assignment operator.
304 m1 = Eq(true);
305 EXPECT_TRUE(m1.Matches(true));
306 EXPECT_FALSE(m1.Matches(false));
307}
308
309// Tests that Matcher<T>::DescribeTo() calls
310// MatcherInterface<T>::DescribeTo().
311TEST(MatcherTest, CanDescribeItself) {
312 EXPECT_EQ("is an even number",
313 Describe(Matcher<int>(new EvenMatcherImpl)));
314}
315
zhanyong.wan82113312010-01-08 21:55:40 +0000316// Tests Matcher<T>::MatchAndExplain().
317TEST(MatcherTest, MatchAndExplain) {
318 Matcher<int> m = GreaterThan(0);
zhanyong.wan34b034c2010-03-05 21:23:23 +0000319 StringMatchResultListener listener1;
zhanyong.wan82113312010-01-08 21:55:40 +0000320 EXPECT_TRUE(m.MatchAndExplain(42, &listener1));
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000321 EXPECT_EQ("which is 42 more than 0", listener1.str());
zhanyong.wan82113312010-01-08 21:55:40 +0000322
zhanyong.wan34b034c2010-03-05 21:23:23 +0000323 StringMatchResultListener listener2;
zhanyong.wan82113312010-01-08 21:55:40 +0000324 EXPECT_FALSE(m.MatchAndExplain(-9, &listener2));
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000325 EXPECT_EQ("which is 9 less than 0", listener2.str());
zhanyong.wan82113312010-01-08 21:55:40 +0000326}
327
shiqiane35fdd92008-12-10 05:08:54 +0000328// Tests that a C-string literal can be implicitly converted to a
329// Matcher<string> or Matcher<const string&>.
330TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
331 Matcher<string> m1 = "hi";
332 EXPECT_TRUE(m1.Matches("hi"));
333 EXPECT_FALSE(m1.Matches("hello"));
334
335 Matcher<const string&> m2 = "hi";
336 EXPECT_TRUE(m2.Matches("hi"));
337 EXPECT_FALSE(m2.Matches("hello"));
338}
339
340// Tests that a string object can be implicitly converted to a
341// Matcher<string> or Matcher<const string&>.
342TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {
343 Matcher<string> m1 = string("hi");
344 EXPECT_TRUE(m1.Matches("hi"));
345 EXPECT_FALSE(m1.Matches("hello"));
346
347 Matcher<const string&> m2 = string("hi");
348 EXPECT_TRUE(m2.Matches("hi"));
349 EXPECT_FALSE(m2.Matches("hello"));
350}
351
352// Tests that MakeMatcher() constructs a Matcher<T> from a
353// MatcherInterface* without requiring the user to explicitly
354// write the type.
355TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) {
356 const MatcherInterface<int>* dummy_impl = NULL;
357 Matcher<int> m = MakeMatcher(dummy_impl);
358}
359
zhanyong.wan82113312010-01-08 21:55:40 +0000360// Tests that MakePolymorphicMatcher() can construct a polymorphic
361// matcher from its implementation using the old API.
zhanyong.wan33605ba2010-04-22 23:37:47 +0000362const int g_bar = 1;
shiqiane35fdd92008-12-10 05:08:54 +0000363class ReferencesBarOrIsZeroImpl {
364 public:
365 template <typename T>
zhanyong.wandb22c222010-01-28 21:52:29 +0000366 bool MatchAndExplain(const T& x,
367 MatchResultListener* /* listener */) const {
shiqiane35fdd92008-12-10 05:08:54 +0000368 const void* p = &x;
zhanyong.wan33605ba2010-04-22 23:37:47 +0000369 return p == &g_bar || x == 0;
shiqiane35fdd92008-12-10 05:08:54 +0000370 }
371
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000372 void DescribeTo(ostream* os) const { *os << "g_bar or zero"; }
shiqiane35fdd92008-12-10 05:08:54 +0000373
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000374 void DescribeNegationTo(ostream* os) const {
zhanyong.wan33605ba2010-04-22 23:37:47 +0000375 *os << "doesn't reference g_bar and is not zero";
shiqiane35fdd92008-12-10 05:08:54 +0000376 }
377};
378
379// This function verifies that MakePolymorphicMatcher() returns a
380// PolymorphicMatcher<T> where T is the argument's type.
381PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() {
382 return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl());
383}
384
zhanyong.wan82113312010-01-08 21:55:40 +0000385TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) {
shiqiane35fdd92008-12-10 05:08:54 +0000386 // Using a polymorphic matcher to match a reference type.
387 Matcher<const int&> m1 = ReferencesBarOrIsZero();
388 EXPECT_TRUE(m1.Matches(0));
389 // Verifies that the identity of a by-reference argument is preserved.
zhanyong.wan33605ba2010-04-22 23:37:47 +0000390 EXPECT_TRUE(m1.Matches(g_bar));
shiqiane35fdd92008-12-10 05:08:54 +0000391 EXPECT_FALSE(m1.Matches(1));
zhanyong.wan33605ba2010-04-22 23:37:47 +0000392 EXPECT_EQ("g_bar or zero", Describe(m1));
shiqiane35fdd92008-12-10 05:08:54 +0000393
394 // Using a polymorphic matcher to match a value type.
395 Matcher<double> m2 = ReferencesBarOrIsZero();
396 EXPECT_TRUE(m2.Matches(0.0));
397 EXPECT_FALSE(m2.Matches(0.1));
zhanyong.wan33605ba2010-04-22 23:37:47 +0000398 EXPECT_EQ("g_bar or zero", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +0000399}
400
zhanyong.wan82113312010-01-08 21:55:40 +0000401// Tests implementing a polymorphic matcher using MatchAndExplain().
402
403class PolymorphicIsEvenImpl {
404 public:
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000405 void DescribeTo(ostream* os) const { *os << "is even"; }
zhanyong.wan82113312010-01-08 21:55:40 +0000406
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000407 void DescribeNegationTo(ostream* os) const {
zhanyong.wan82113312010-01-08 21:55:40 +0000408 *os << "is odd";
409 }
zhanyong.wan82113312010-01-08 21:55:40 +0000410
zhanyong.wandb22c222010-01-28 21:52:29 +0000411 template <typename T>
412 bool MatchAndExplain(const T& x, MatchResultListener* listener) const {
413 // Verifies that we can stream to the listener directly.
414 *listener << "% " << 2;
415 if (listener->stream() != NULL) {
416 // Verifies that we can stream to the listener's underlying stream
417 // too.
418 *listener->stream() << " == " << (x % 2);
419 }
420 return (x % 2) == 0;
zhanyong.wan82113312010-01-08 21:55:40 +0000421 }
zhanyong.wandb22c222010-01-28 21:52:29 +0000422};
zhanyong.wan82113312010-01-08 21:55:40 +0000423
424PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() {
425 return MakePolymorphicMatcher(PolymorphicIsEvenImpl());
426}
427
428TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {
429 // Using PolymorphicIsEven() as a Matcher<int>.
430 const Matcher<int> m1 = PolymorphicIsEven();
431 EXPECT_TRUE(m1.Matches(42));
432 EXPECT_FALSE(m1.Matches(43));
433 EXPECT_EQ("is even", Describe(m1));
434
435 const Matcher<int> not_m1 = Not(m1);
436 EXPECT_EQ("is odd", Describe(not_m1));
437
438 EXPECT_EQ("% 2 == 0", Explain(m1, 42));
439
440 // Using PolymorphicIsEven() as a Matcher<char>.
441 const Matcher<char> m2 = PolymorphicIsEven();
442 EXPECT_TRUE(m2.Matches('\x42'));
443 EXPECT_FALSE(m2.Matches('\x43'));
444 EXPECT_EQ("is even", Describe(m2));
445
446 const Matcher<char> not_m2 = Not(m2);
447 EXPECT_EQ("is odd", Describe(not_m2));
448
449 EXPECT_EQ("% 2 == 0", Explain(m2, '\x42'));
450}
451
shiqiane35fdd92008-12-10 05:08:54 +0000452// Tests that MatcherCast<T>(m) works when m is a polymorphic matcher.
453TEST(MatcherCastTest, FromPolymorphicMatcher) {
454 Matcher<int> m = MatcherCast<int>(Eq(5));
455 EXPECT_TRUE(m.Matches(5));
456 EXPECT_FALSE(m.Matches(6));
457}
458
459// For testing casting matchers between compatible types.
460class IntValue {
461 public:
462 // An int can be statically (although not implicitly) cast to a
463 // IntValue.
zhanyong.wan32de5f52009-12-23 00:13:23 +0000464 explicit IntValue(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +0000465
466 int value() const { return value_; }
467 private:
468 int value_;
469};
470
471// For testing casting matchers between compatible types.
472bool IsPositiveIntValue(const IntValue& foo) {
473 return foo.value() > 0;
474}
475
476// Tests that MatcherCast<T>(m) works when m is a Matcher<U> where T
477// can be statically converted to U.
478TEST(MatcherCastTest, FromCompatibleType) {
479 Matcher<double> m1 = Eq(2.0);
480 Matcher<int> m2 = MatcherCast<int>(m1);
481 EXPECT_TRUE(m2.Matches(2));
482 EXPECT_FALSE(m2.Matches(3));
483
484 Matcher<IntValue> m3 = Truly(IsPositiveIntValue);
485 Matcher<int> m4 = MatcherCast<int>(m3);
486 // In the following, the arguments 1 and 0 are statically converted
487 // to IntValue objects, and then tested by the IsPositiveIntValue()
488 // predicate.
489 EXPECT_TRUE(m4.Matches(1));
490 EXPECT_FALSE(m4.Matches(0));
491}
492
493// Tests that MatcherCast<T>(m) works when m is a Matcher<const T&>.
494TEST(MatcherCastTest, FromConstReferenceToNonReference) {
495 Matcher<const int&> m1 = Eq(0);
496 Matcher<int> m2 = MatcherCast<int>(m1);
497 EXPECT_TRUE(m2.Matches(0));
498 EXPECT_FALSE(m2.Matches(1));
499}
500
501// Tests that MatcherCast<T>(m) works when m is a Matcher<T&>.
502TEST(MatcherCastTest, FromReferenceToNonReference) {
503 Matcher<int&> m1 = Eq(0);
504 Matcher<int> m2 = MatcherCast<int>(m1);
505 EXPECT_TRUE(m2.Matches(0));
506 EXPECT_FALSE(m2.Matches(1));
507}
508
509// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
510TEST(MatcherCastTest, FromNonReferenceToConstReference) {
511 Matcher<int> m1 = Eq(0);
512 Matcher<const int&> m2 = MatcherCast<const int&>(m1);
513 EXPECT_TRUE(m2.Matches(0));
514 EXPECT_FALSE(m2.Matches(1));
515}
516
517// Tests that MatcherCast<T&>(m) works when m is a Matcher<T>.
518TEST(MatcherCastTest, FromNonReferenceToReference) {
519 Matcher<int> m1 = Eq(0);
520 Matcher<int&> m2 = MatcherCast<int&>(m1);
521 int n = 0;
522 EXPECT_TRUE(m2.Matches(n));
523 n = 1;
524 EXPECT_FALSE(m2.Matches(n));
525}
526
527// Tests that MatcherCast<T>(m) works when m is a Matcher<T>.
528TEST(MatcherCastTest, FromSameType) {
529 Matcher<int> m1 = Eq(0);
530 Matcher<int> m2 = MatcherCast<int>(m1);
531 EXPECT_TRUE(m2.Matches(0));
532 EXPECT_FALSE(m2.Matches(1));
533}
534
zhanyong.wan18490652009-05-11 18:54:08 +0000535class Base {};
536class Derived : public Base {};
537
538// Tests that SafeMatcherCast<T>(m) works when m is a polymorphic matcher.
539TEST(SafeMatcherCastTest, FromPolymorphicMatcher) {
540 Matcher<char> m2 = SafeMatcherCast<char>(Eq(32));
541 EXPECT_TRUE(m2.Matches(' '));
542 EXPECT_FALSE(m2.Matches('\n'));
543}
544
zhanyong.wan16cf4732009-05-14 20:55:30 +0000545// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where
546// T and U are arithmetic types and T can be losslessly converted to
547// U.
548TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) {
zhanyong.wan18490652009-05-11 18:54:08 +0000549 Matcher<double> m1 = DoubleEq(1.0);
zhanyong.wan16cf4732009-05-14 20:55:30 +0000550 Matcher<float> m2 = SafeMatcherCast<float>(m1);
551 EXPECT_TRUE(m2.Matches(1.0f));
552 EXPECT_FALSE(m2.Matches(2.0f));
553
554 Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>('a'));
555 EXPECT_TRUE(m3.Matches('a'));
556 EXPECT_FALSE(m3.Matches('b'));
zhanyong.wan18490652009-05-11 18:54:08 +0000557}
558
559// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where T and U
560// are pointers or references to a derived and a base class, correspondingly.
561TEST(SafeMatcherCastTest, FromBaseClass) {
562 Derived d, d2;
563 Matcher<Base*> m1 = Eq(&d);
564 Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1);
565 EXPECT_TRUE(m2.Matches(&d));
566 EXPECT_FALSE(m2.Matches(&d2));
567
568 Matcher<Base&> m3 = Ref(d);
569 Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3);
570 EXPECT_TRUE(m4.Matches(d));
571 EXPECT_FALSE(m4.Matches(d2));
572}
573
574// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<const T&>.
575TEST(SafeMatcherCastTest, FromConstReferenceToReference) {
576 int n = 0;
577 Matcher<const int&> m1 = Ref(n);
578 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
579 int n1 = 0;
580 EXPECT_TRUE(m2.Matches(n));
581 EXPECT_FALSE(m2.Matches(n1));
582}
583
584// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
585TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) {
586 Matcher<int> m1 = Eq(0);
587 Matcher<const int&> m2 = SafeMatcherCast<const int&>(m1);
588 EXPECT_TRUE(m2.Matches(0));
589 EXPECT_FALSE(m2.Matches(1));
590}
591
592// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<T>.
593TEST(SafeMatcherCastTest, FromNonReferenceToReference) {
594 Matcher<int> m1 = Eq(0);
595 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
596 int n = 0;
597 EXPECT_TRUE(m2.Matches(n));
598 n = 1;
599 EXPECT_FALSE(m2.Matches(n));
600}
601
602// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<T>.
603TEST(SafeMatcherCastTest, FromSameType) {
604 Matcher<int> m1 = Eq(0);
605 Matcher<int> m2 = SafeMatcherCast<int>(m1);
606 EXPECT_TRUE(m2.Matches(0));
607 EXPECT_FALSE(m2.Matches(1));
608}
609
shiqiane35fdd92008-12-10 05:08:54 +0000610// Tests that A<T>() matches any value of type T.
611TEST(ATest, MatchesAnyValue) {
612 // Tests a matcher for a value type.
613 Matcher<double> m1 = A<double>();
614 EXPECT_TRUE(m1.Matches(91.43));
615 EXPECT_TRUE(m1.Matches(-15.32));
616
617 // Tests a matcher for a reference type.
618 int a = 2;
619 int b = -6;
620 Matcher<int&> m2 = A<int&>();
621 EXPECT_TRUE(m2.Matches(a));
622 EXPECT_TRUE(m2.Matches(b));
623}
624
625// Tests that A<T>() describes itself properly.
626TEST(ATest, CanDescribeSelf) {
627 EXPECT_EQ("is anything", Describe(A<bool>()));
628}
629
630// Tests that An<T>() matches any value of type T.
631TEST(AnTest, MatchesAnyValue) {
632 // Tests a matcher for a value type.
633 Matcher<int> m1 = An<int>();
634 EXPECT_TRUE(m1.Matches(9143));
635 EXPECT_TRUE(m1.Matches(-1532));
636
637 // Tests a matcher for a reference type.
638 int a = 2;
639 int b = -6;
640 Matcher<int&> m2 = An<int&>();
641 EXPECT_TRUE(m2.Matches(a));
642 EXPECT_TRUE(m2.Matches(b));
643}
644
645// Tests that An<T>() describes itself properly.
646TEST(AnTest, CanDescribeSelf) {
647 EXPECT_EQ("is anything", Describe(An<int>()));
648}
649
650// Tests that _ can be used as a matcher for any type and matches any
651// value of that type.
652TEST(UnderscoreTest, MatchesAnyValue) {
653 // Uses _ as a matcher for a value type.
654 Matcher<int> m1 = _;
655 EXPECT_TRUE(m1.Matches(123));
656 EXPECT_TRUE(m1.Matches(-242));
657
658 // Uses _ as a matcher for a reference type.
659 bool a = false;
660 const bool b = true;
661 Matcher<const bool&> m2 = _;
662 EXPECT_TRUE(m2.Matches(a));
663 EXPECT_TRUE(m2.Matches(b));
664}
665
666// Tests that _ describes itself properly.
667TEST(UnderscoreTest, CanDescribeSelf) {
668 Matcher<int> m = _;
669 EXPECT_EQ("is anything", Describe(m));
670}
671
672// Tests that Eq(x) matches any value equal to x.
673TEST(EqTest, MatchesEqualValue) {
674 // 2 C-strings with same content but different addresses.
675 const char a1[] = "hi";
676 const char a2[] = "hi";
677
678 Matcher<const char*> m1 = Eq(a1);
679 EXPECT_TRUE(m1.Matches(a1));
680 EXPECT_FALSE(m1.Matches(a2));
681}
682
683// Tests that Eq(v) describes itself properly.
684
685class Unprintable {
686 public:
687 Unprintable() : c_('a') {}
688
zhanyong.wan32de5f52009-12-23 00:13:23 +0000689 bool operator==(const Unprintable& /* rhs */) { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000690 private:
691 char c_;
692};
693
694TEST(EqTest, CanDescribeSelf) {
695 Matcher<Unprintable> m = Eq(Unprintable());
696 EXPECT_EQ("is equal to 1-byte object <61>", Describe(m));
697}
698
699// Tests that Eq(v) can be used to match any type that supports
700// comparing with type T, where T is v's type.
701TEST(EqTest, IsPolymorphic) {
702 Matcher<int> m1 = Eq(1);
703 EXPECT_TRUE(m1.Matches(1));
704 EXPECT_FALSE(m1.Matches(2));
705
706 Matcher<char> m2 = Eq(1);
707 EXPECT_TRUE(m2.Matches('\1'));
708 EXPECT_FALSE(m2.Matches('a'));
709}
710
711// Tests that TypedEq<T>(v) matches values of type T that's equal to v.
712TEST(TypedEqTest, ChecksEqualityForGivenType) {
713 Matcher<char> m1 = TypedEq<char>('a');
714 EXPECT_TRUE(m1.Matches('a'));
715 EXPECT_FALSE(m1.Matches('b'));
716
717 Matcher<int> m2 = TypedEq<int>(6);
718 EXPECT_TRUE(m2.Matches(6));
719 EXPECT_FALSE(m2.Matches(7));
720}
721
722// Tests that TypedEq(v) describes itself properly.
723TEST(TypedEqTest, CanDescribeSelf) {
724 EXPECT_EQ("is equal to 2", Describe(TypedEq<int>(2)));
725}
726
727// Tests that TypedEq<T>(v) has type Matcher<T>.
728
729// Type<T>::IsTypeOf(v) compiles iff the type of value v is T, where T
730// is a "bare" type (i.e. not in the form of const U or U&). If v's
731// type is not T, the compiler will generate a message about
732// "undefined referece".
733template <typename T>
734struct Type {
zhanyong.wan32de5f52009-12-23 00:13:23 +0000735 static bool IsTypeOf(const T& /* v */) { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000736
737 template <typename T2>
738 static void IsTypeOf(T2 v);
739};
740
741TEST(TypedEqTest, HasSpecifiedType) {
742 // Verfies that the type of TypedEq<T>(v) is Matcher<T>.
743 Type<Matcher<int> >::IsTypeOf(TypedEq<int>(5));
744 Type<Matcher<double> >::IsTypeOf(TypedEq<double>(5));
745}
746
747// Tests that Ge(v) matches anything >= v.
748TEST(GeTest, ImplementsGreaterThanOrEqual) {
749 Matcher<int> m1 = Ge(0);
750 EXPECT_TRUE(m1.Matches(1));
751 EXPECT_TRUE(m1.Matches(0));
752 EXPECT_FALSE(m1.Matches(-1));
753}
754
755// Tests that Ge(v) describes itself properly.
756TEST(GeTest, CanDescribeSelf) {
757 Matcher<int> m = Ge(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000758 EXPECT_EQ("is >= 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000759}
760
761// Tests that Gt(v) matches anything > v.
762TEST(GtTest, ImplementsGreaterThan) {
763 Matcher<double> m1 = Gt(0);
764 EXPECT_TRUE(m1.Matches(1.0));
765 EXPECT_FALSE(m1.Matches(0.0));
766 EXPECT_FALSE(m1.Matches(-1.0));
767}
768
769// Tests that Gt(v) describes itself properly.
770TEST(GtTest, CanDescribeSelf) {
771 Matcher<int> m = Gt(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000772 EXPECT_EQ("is > 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000773}
774
775// Tests that Le(v) matches anything <= v.
776TEST(LeTest, ImplementsLessThanOrEqual) {
777 Matcher<char> m1 = Le('b');
778 EXPECT_TRUE(m1.Matches('a'));
779 EXPECT_TRUE(m1.Matches('b'));
780 EXPECT_FALSE(m1.Matches('c'));
781}
782
783// Tests that Le(v) describes itself properly.
784TEST(LeTest, CanDescribeSelf) {
785 Matcher<int> m = Le(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000786 EXPECT_EQ("is <= 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000787}
788
789// Tests that Lt(v) matches anything < v.
790TEST(LtTest, ImplementsLessThan) {
791 Matcher<const string&> m1 = Lt("Hello");
792 EXPECT_TRUE(m1.Matches("Abc"));
793 EXPECT_FALSE(m1.Matches("Hello"));
794 EXPECT_FALSE(m1.Matches("Hello, world!"));
795}
796
797// Tests that Lt(v) describes itself properly.
798TEST(LtTest, CanDescribeSelf) {
799 Matcher<int> m = Lt(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000800 EXPECT_EQ("is < 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000801}
802
803// Tests that Ne(v) matches anything != v.
804TEST(NeTest, ImplementsNotEqual) {
805 Matcher<int> m1 = Ne(0);
806 EXPECT_TRUE(m1.Matches(1));
807 EXPECT_TRUE(m1.Matches(-1));
808 EXPECT_FALSE(m1.Matches(0));
809}
810
811// Tests that Ne(v) describes itself properly.
812TEST(NeTest, CanDescribeSelf) {
813 Matcher<int> m = Ne(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000814 EXPECT_EQ("isn't equal to 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000815}
816
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000817// Tests that IsNull() matches any NULL pointer of any type.
818TEST(IsNullTest, MatchesNullPointer) {
819 Matcher<int*> m1 = IsNull();
820 int* p1 = NULL;
821 int n = 0;
822 EXPECT_TRUE(m1.Matches(p1));
823 EXPECT_FALSE(m1.Matches(&n));
824
825 Matcher<const char*> m2 = IsNull();
826 const char* p2 = NULL;
827 EXPECT_TRUE(m2.Matches(p2));
828 EXPECT_FALSE(m2.Matches("hi"));
829
zhanyong.wan95b12332009-09-25 18:55:50 +0000830#if !GTEST_OS_SYMBIAN
831 // Nokia's Symbian compiler generates:
832 // gmock-matchers.h: ambiguous access to overloaded function
833 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(void *)'
834 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(const testing::
835 // MatcherInterface<void *> *)'
836 // gmock-matchers.h: (point of instantiation: 'testing::
837 // gmock_matchers_test::IsNullTest_MatchesNullPointer_Test::TestBody()')
838 // gmock-matchers.h: (instantiating: 'testing::PolymorphicMatc
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000839 Matcher<void*> m3 = IsNull();
840 void* p3 = NULL;
841 EXPECT_TRUE(m3.Matches(p3));
842 EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef)));
zhanyong.wan95b12332009-09-25 18:55:50 +0000843#endif
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000844}
845
vladlosev79b83502009-11-18 00:43:37 +0000846TEST(IsNullTest, LinkedPtr) {
847 const Matcher<linked_ptr<int> > m = IsNull();
848 const linked_ptr<int> null_p;
849 const linked_ptr<int> non_null_p(new int);
850
851 EXPECT_TRUE(m.Matches(null_p));
852 EXPECT_FALSE(m.Matches(non_null_p));
853}
854
855TEST(IsNullTest, ReferenceToConstLinkedPtr) {
856 const Matcher<const linked_ptr<double>&> m = IsNull();
857 const linked_ptr<double> null_p;
858 const linked_ptr<double> non_null_p(new double);
859
860 EXPECT_TRUE(m.Matches(null_p));
861 EXPECT_FALSE(m.Matches(non_null_p));
862}
863
vladloseve56daa72009-11-18 01:08:08 +0000864TEST(IsNullTest, ReferenceToConstScopedPtr) {
865 const Matcher<const scoped_ptr<double>&> m = IsNull();
866 const scoped_ptr<double> null_p;
867 const scoped_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
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000873// Tests that IsNull() describes itself properly.
874TEST(IsNullTest, CanDescribeSelf) {
875 Matcher<int*> m = IsNull();
876 EXPECT_EQ("is NULL", Describe(m));
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000877 EXPECT_EQ("isn't NULL", DescribeNegation(m));
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000878}
879
shiqiane35fdd92008-12-10 05:08:54 +0000880// Tests that NotNull() matches any non-NULL pointer of any type.
881TEST(NotNullTest, MatchesNonNullPointer) {
882 Matcher<int*> m1 = NotNull();
883 int* p1 = NULL;
884 int n = 0;
885 EXPECT_FALSE(m1.Matches(p1));
886 EXPECT_TRUE(m1.Matches(&n));
887
888 Matcher<const char*> m2 = NotNull();
889 const char* p2 = NULL;
890 EXPECT_FALSE(m2.Matches(p2));
891 EXPECT_TRUE(m2.Matches("hi"));
892}
893
vladlosev79b83502009-11-18 00:43:37 +0000894TEST(NotNullTest, LinkedPtr) {
895 const Matcher<linked_ptr<int> > m = NotNull();
896 const linked_ptr<int> null_p;
897 const linked_ptr<int> non_null_p(new int);
898
899 EXPECT_FALSE(m.Matches(null_p));
900 EXPECT_TRUE(m.Matches(non_null_p));
901}
902
903TEST(NotNullTest, ReferenceToConstLinkedPtr) {
904 const Matcher<const linked_ptr<double>&> m = NotNull();
905 const linked_ptr<double> null_p;
906 const linked_ptr<double> non_null_p(new double);
907
908 EXPECT_FALSE(m.Matches(null_p));
909 EXPECT_TRUE(m.Matches(non_null_p));
910}
911
vladloseve56daa72009-11-18 01:08:08 +0000912TEST(NotNullTest, ReferenceToConstScopedPtr) {
913 const Matcher<const scoped_ptr<double>&> m = NotNull();
914 const scoped_ptr<double> null_p;
915 const scoped_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
shiqiane35fdd92008-12-10 05:08:54 +0000921// Tests that NotNull() describes itself properly.
922TEST(NotNullTest, CanDescribeSelf) {
923 Matcher<int*> m = NotNull();
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000924 EXPECT_EQ("isn't NULL", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000925}
926
927// Tests that Ref(variable) matches an argument that references
928// 'variable'.
929TEST(RefTest, MatchesSameVariable) {
930 int a = 0;
931 int b = 0;
932 Matcher<int&> m = Ref(a);
933 EXPECT_TRUE(m.Matches(a));
934 EXPECT_FALSE(m.Matches(b));
935}
936
937// Tests that Ref(variable) describes itself properly.
938TEST(RefTest, CanDescribeSelf) {
939 int n = 5;
940 Matcher<int&> m = Ref(n);
941 stringstream ss;
942 ss << "references the variable @" << &n << " 5";
943 EXPECT_EQ(string(ss.str()), Describe(m));
944}
945
946// Test that Ref(non_const_varialbe) can be used as a matcher for a
947// const reference.
948TEST(RefTest, CanBeUsedAsMatcherForConstReference) {
949 int a = 0;
950 int b = 0;
951 Matcher<const int&> m = Ref(a);
952 EXPECT_TRUE(m.Matches(a));
953 EXPECT_FALSE(m.Matches(b));
954}
955
956// Tests that Ref(variable) is covariant, i.e. Ref(derived) can be
957// used wherever Ref(base) can be used (Ref(derived) is a sub-type
958// of Ref(base), but not vice versa.
959
shiqiane35fdd92008-12-10 05:08:54 +0000960TEST(RefTest, IsCovariant) {
961 Base base, base2;
962 Derived derived;
963 Matcher<const Base&> m1 = Ref(base);
964 EXPECT_TRUE(m1.Matches(base));
965 EXPECT_FALSE(m1.Matches(base2));
966 EXPECT_FALSE(m1.Matches(derived));
967
968 m1 = Ref(derived);
969 EXPECT_TRUE(m1.Matches(derived));
970 EXPECT_FALSE(m1.Matches(base));
971 EXPECT_FALSE(m1.Matches(base2));
972}
973
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000974TEST(RefTest, ExplainsResult) {
975 int n = 0;
976 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), n),
977 StartsWith("which is located @"));
978
979 int m = 0;
980 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), m),
981 StartsWith("which is located @"));
982}
983
shiqiane35fdd92008-12-10 05:08:54 +0000984// Tests string comparison matchers.
985
986TEST(StrEqTest, MatchesEqualString) {
987 Matcher<const char*> m = StrEq(string("Hello"));
988 EXPECT_TRUE(m.Matches("Hello"));
989 EXPECT_FALSE(m.Matches("hello"));
990 EXPECT_FALSE(m.Matches(NULL));
991
992 Matcher<const string&> m2 = StrEq("Hello");
993 EXPECT_TRUE(m2.Matches("Hello"));
994 EXPECT_FALSE(m2.Matches("Hi"));
995}
996
997TEST(StrEqTest, CanDescribeSelf) {
998 Matcher<string> m = StrEq("Hi-\'\"\?\\\a\b\f\n\r\t\v\xD3");
999 EXPECT_EQ("is equal to \"Hi-\'\\\"\\?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",
1000 Describe(m));
1001
1002 string str("01204500800");
1003 str[3] = '\0';
1004 Matcher<string> m2 = StrEq(str);
1005 EXPECT_EQ("is equal to \"012\\04500800\"", Describe(m2));
1006 str[0] = str[6] = str[7] = str[9] = str[10] = '\0';
1007 Matcher<string> m3 = StrEq(str);
1008 EXPECT_EQ("is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3));
1009}
1010
1011TEST(StrNeTest, MatchesUnequalString) {
1012 Matcher<const char*> m = StrNe("Hello");
1013 EXPECT_TRUE(m.Matches(""));
1014 EXPECT_TRUE(m.Matches(NULL));
1015 EXPECT_FALSE(m.Matches("Hello"));
1016
1017 Matcher<string> m2 = StrNe(string("Hello"));
1018 EXPECT_TRUE(m2.Matches("hello"));
1019 EXPECT_FALSE(m2.Matches("Hello"));
1020}
1021
1022TEST(StrNeTest, CanDescribeSelf) {
1023 Matcher<const char*> m = StrNe("Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001024 EXPECT_EQ("isn't equal to \"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001025}
1026
1027TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
1028 Matcher<const char*> m = StrCaseEq(string("Hello"));
1029 EXPECT_TRUE(m.Matches("Hello"));
1030 EXPECT_TRUE(m.Matches("hello"));
1031 EXPECT_FALSE(m.Matches("Hi"));
1032 EXPECT_FALSE(m.Matches(NULL));
1033
1034 Matcher<const string&> m2 = StrCaseEq("Hello");
1035 EXPECT_TRUE(m2.Matches("hello"));
1036 EXPECT_FALSE(m2.Matches("Hi"));
1037}
1038
1039TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1040 string str1("oabocdooeoo");
1041 string str2("OABOCDOOEOO");
1042 Matcher<const string&> m0 = StrCaseEq(str1);
1043 EXPECT_FALSE(m0.Matches(str2 + string(1, '\0')));
1044
1045 str1[3] = str2[3] = '\0';
1046 Matcher<const string&> m1 = StrCaseEq(str1);
1047 EXPECT_TRUE(m1.Matches(str2));
1048
1049 str1[0] = str1[6] = str1[7] = str1[10] = '\0';
1050 str2[0] = str2[6] = str2[7] = str2[10] = '\0';
1051 Matcher<const string&> m2 = StrCaseEq(str1);
1052 str1[9] = str2[9] = '\0';
1053 EXPECT_FALSE(m2.Matches(str2));
1054
1055 Matcher<const string&> m3 = StrCaseEq(str1);
1056 EXPECT_TRUE(m3.Matches(str2));
1057
1058 EXPECT_FALSE(m3.Matches(str2 + "x"));
1059 str2.append(1, '\0');
1060 EXPECT_FALSE(m3.Matches(str2));
1061 EXPECT_FALSE(m3.Matches(string(str2, 0, 9)));
1062}
1063
1064TEST(StrCaseEqTest, CanDescribeSelf) {
1065 Matcher<string> m = StrCaseEq("Hi");
1066 EXPECT_EQ("is equal to (ignoring case) \"Hi\"", Describe(m));
1067}
1068
1069TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1070 Matcher<const char*> m = StrCaseNe("Hello");
1071 EXPECT_TRUE(m.Matches("Hi"));
1072 EXPECT_TRUE(m.Matches(NULL));
1073 EXPECT_FALSE(m.Matches("Hello"));
1074 EXPECT_FALSE(m.Matches("hello"));
1075
1076 Matcher<string> m2 = StrCaseNe(string("Hello"));
1077 EXPECT_TRUE(m2.Matches(""));
1078 EXPECT_FALSE(m2.Matches("Hello"));
1079}
1080
1081TEST(StrCaseNeTest, CanDescribeSelf) {
1082 Matcher<const char*> m = StrCaseNe("Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001083 EXPECT_EQ("isn't equal to (ignoring case) \"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001084}
1085
1086// Tests that HasSubstr() works for matching string-typed values.
1087TEST(HasSubstrTest, WorksForStringClasses) {
1088 const Matcher<string> m1 = HasSubstr("foo");
1089 EXPECT_TRUE(m1.Matches(string("I love food.")));
1090 EXPECT_FALSE(m1.Matches(string("tofo")));
1091
1092 const Matcher<const std::string&> m2 = HasSubstr("foo");
1093 EXPECT_TRUE(m2.Matches(std::string("I love food.")));
1094 EXPECT_FALSE(m2.Matches(std::string("tofo")));
1095}
1096
1097// Tests that HasSubstr() works for matching C-string-typed values.
1098TEST(HasSubstrTest, WorksForCStrings) {
1099 const Matcher<char*> m1 = HasSubstr("foo");
1100 EXPECT_TRUE(m1.Matches(const_cast<char*>("I love food.")));
1101 EXPECT_FALSE(m1.Matches(const_cast<char*>("tofo")));
1102 EXPECT_FALSE(m1.Matches(NULL));
1103
1104 const Matcher<const char*> m2 = HasSubstr("foo");
1105 EXPECT_TRUE(m2.Matches("I love food."));
1106 EXPECT_FALSE(m2.Matches("tofo"));
1107 EXPECT_FALSE(m2.Matches(NULL));
1108}
1109
1110// Tests that HasSubstr(s) describes itself properly.
1111TEST(HasSubstrTest, CanDescribeSelf) {
1112 Matcher<string> m = HasSubstr("foo\n\"");
1113 EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m));
1114}
1115
zhanyong.wanb5937da2009-07-16 20:26:41 +00001116TEST(KeyTest, CanDescribeSelf) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001117 Matcher<const pair<std::string, int>&> m = Key("foo");
zhanyong.wanb5937da2009-07-16 20:26:41 +00001118 EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001119 EXPECT_EQ("doesn't have a key that is equal to \"foo\"", DescribeNegation(m));
1120}
1121
1122TEST(KeyTest, ExplainsResult) {
1123 Matcher<pair<int, bool> > m = Key(GreaterThan(10));
1124 EXPECT_EQ("whose first field is a value which is 5 less than 10",
1125 Explain(m, make_pair(5, true)));
1126 EXPECT_EQ("whose first field is a value which is 5 more than 10",
1127 Explain(m, make_pair(15, true)));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001128}
1129
1130TEST(KeyTest, MatchesCorrectly) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001131 pair<int, std::string> p(25, "foo");
zhanyong.wanb5937da2009-07-16 20:26:41 +00001132 EXPECT_THAT(p, Key(25));
1133 EXPECT_THAT(p, Not(Key(42)));
1134 EXPECT_THAT(p, Key(Ge(20)));
1135 EXPECT_THAT(p, Not(Key(Lt(25))));
1136}
1137
1138TEST(KeyTest, SafelyCastsInnerMatcher) {
1139 Matcher<int> is_positive = Gt(0);
1140 Matcher<int> is_negative = Lt(0);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001141 pair<char, bool> p('a', true);
zhanyong.wanb5937da2009-07-16 20:26:41 +00001142 EXPECT_THAT(p, Key(is_positive));
1143 EXPECT_THAT(p, Not(Key(is_negative)));
1144}
1145
1146TEST(KeyTest, InsideContainsUsingMap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001147 map<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001148 container.insert(make_pair(1, 'a'));
1149 container.insert(make_pair(2, 'b'));
1150 container.insert(make_pair(4, 'c'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001151 EXPECT_THAT(container, Contains(Key(1)));
1152 EXPECT_THAT(container, Not(Contains(Key(3))));
1153}
1154
1155TEST(KeyTest, InsideContainsUsingMultimap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001156 multimap<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
1161 EXPECT_THAT(container, Not(Contains(Key(25))));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001162 container.insert(make_pair(25, 'd'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001163 EXPECT_THAT(container, Contains(Key(25)));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001164 container.insert(make_pair(25, 'e'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001165 EXPECT_THAT(container, Contains(Key(25)));
1166
1167 EXPECT_THAT(container, Contains(Key(1)));
1168 EXPECT_THAT(container, Not(Contains(Key(3))));
1169}
1170
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001171TEST(PairTest, Typing) {
1172 // Test verifies the following type conversions can be compiled.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001173 Matcher<const pair<const char*, int>&> m1 = Pair("foo", 42);
1174 Matcher<const pair<const char*, int> > m2 = Pair("foo", 42);
1175 Matcher<pair<const char*, int> > m3 = Pair("foo", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001176
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001177 Matcher<pair<int, const std::string> > m4 = Pair(25, "42");
1178 Matcher<pair<const std::string, int> > m5 = Pair("25", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001179}
1180
1181TEST(PairTest, CanDescribeSelf) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001182 Matcher<const pair<std::string, int>&> m1 = Pair("foo", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001183 EXPECT_EQ("has a first field that is equal to \"foo\""
1184 ", and has a second field that is equal to 42",
1185 Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001186 EXPECT_EQ("has a first field that isn't equal to \"foo\""
1187 ", or has a second field that isn't equal to 42",
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001188 DescribeNegation(m1));
1189 // Double and triple negation (1 or 2 times not and description of negation).
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001190 Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42));
1191 EXPECT_EQ("has a first field that isn't equal to 13"
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001192 ", and has a second field that is equal to 42",
1193 DescribeNegation(m2));
1194}
1195
1196TEST(PairTest, CanExplainMatchResultTo) {
zhanyong.wan82113312010-01-08 21:55:40 +00001197 // If neither field matches, Pair() should explain about the first
1198 // field.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001199 const Matcher<pair<int, int> > m = Pair(GreaterThan(0), GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001200 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001201 Explain(m, make_pair(-1, -2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001202
zhanyong.wan82113312010-01-08 21:55:40 +00001203 // If the first field matches but the second doesn't, Pair() should
1204 // explain about the second field.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001205 EXPECT_EQ("whose second field does not match, which is 2 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001206 Explain(m, make_pair(1, -2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001207
zhanyong.wan82113312010-01-08 21:55:40 +00001208 // If the first field doesn't match but the second does, Pair()
1209 // should explain about the first field.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001210 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001211 Explain(m, make_pair(-1, 2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001212
zhanyong.wan82113312010-01-08 21:55:40 +00001213 // If both fields match, Pair() should explain about them both.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001214 EXPECT_EQ("whose both fields match, where the first field is a value "
1215 "which is 1 more than 0, and the second field is a value "
1216 "which is 2 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001217 Explain(m, make_pair(1, 2)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001218
1219 // If only the first match has an explanation, only this explanation should
1220 // be printed.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001221 const Matcher<pair<int, int> > explain_first = Pair(GreaterThan(0), 0);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001222 EXPECT_EQ("whose both fields match, where the first field is a value "
1223 "which is 1 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001224 Explain(explain_first, make_pair(1, 0)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001225
1226 // If only the second match has an explanation, only this explanation should
1227 // be printed.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001228 const Matcher<pair<int, int> > explain_second = Pair(0, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001229 EXPECT_EQ("whose both fields match, where the second field is a value "
1230 "which is 1 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001231 Explain(explain_second, make_pair(0, 1)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001232}
1233
1234TEST(PairTest, MatchesCorrectly) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001235 pair<int, std::string> p(25, "foo");
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001236
1237 // Both fields match.
1238 EXPECT_THAT(p, Pair(25, "foo"));
1239 EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o")));
1240
1241 // 'first' doesnt' match, but 'second' matches.
1242 EXPECT_THAT(p, Not(Pair(42, "foo")));
1243 EXPECT_THAT(p, Not(Pair(Lt(25), "foo")));
1244
1245 // 'first' matches, but 'second' doesn't match.
1246 EXPECT_THAT(p, Not(Pair(25, "bar")));
1247 EXPECT_THAT(p, Not(Pair(25, Not("foo"))));
1248
1249 // Neither field matches.
1250 EXPECT_THAT(p, Not(Pair(13, "bar")));
1251 EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a"))));
1252}
1253
1254TEST(PairTest, SafelyCastsInnerMatchers) {
1255 Matcher<int> is_positive = Gt(0);
1256 Matcher<int> is_negative = Lt(0);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001257 pair<char, bool> p('a', true);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001258 EXPECT_THAT(p, Pair(is_positive, _));
1259 EXPECT_THAT(p, Not(Pair(is_negative, _)));
1260 EXPECT_THAT(p, Pair(_, is_positive));
1261 EXPECT_THAT(p, Not(Pair(_, is_negative)));
1262}
1263
1264TEST(PairTest, InsideContainsUsingMap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001265 map<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001266 container.insert(make_pair(1, 'a'));
1267 container.insert(make_pair(2, 'b'));
1268 container.insert(make_pair(4, 'c'));
zhanyong.wan95b12332009-09-25 18:55:50 +00001269 EXPECT_THAT(container, Contains(Pair(1, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001270 EXPECT_THAT(container, Contains(Pair(1, _)));
zhanyong.wan95b12332009-09-25 18:55:50 +00001271 EXPECT_THAT(container, Contains(Pair(_, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001272 EXPECT_THAT(container, Not(Contains(Pair(3, _))));
1273}
1274
shiqiane35fdd92008-12-10 05:08:54 +00001275// Tests StartsWith(s).
1276
1277TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
1278 const Matcher<const char*> m1 = StartsWith(string(""));
1279 EXPECT_TRUE(m1.Matches("Hi"));
1280 EXPECT_TRUE(m1.Matches(""));
1281 EXPECT_FALSE(m1.Matches(NULL));
1282
1283 const Matcher<const string&> m2 = StartsWith("Hi");
1284 EXPECT_TRUE(m2.Matches("Hi"));
1285 EXPECT_TRUE(m2.Matches("Hi Hi!"));
1286 EXPECT_TRUE(m2.Matches("High"));
1287 EXPECT_FALSE(m2.Matches("H"));
1288 EXPECT_FALSE(m2.Matches(" Hi"));
1289}
1290
1291TEST(StartsWithTest, CanDescribeSelf) {
1292 Matcher<const std::string> m = StartsWith("Hi");
1293 EXPECT_EQ("starts with \"Hi\"", Describe(m));
1294}
1295
1296// Tests EndsWith(s).
1297
1298TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
1299 const Matcher<const char*> m1 = EndsWith("");
1300 EXPECT_TRUE(m1.Matches("Hi"));
1301 EXPECT_TRUE(m1.Matches(""));
1302 EXPECT_FALSE(m1.Matches(NULL));
1303
1304 const Matcher<const string&> m2 = EndsWith(string("Hi"));
1305 EXPECT_TRUE(m2.Matches("Hi"));
1306 EXPECT_TRUE(m2.Matches("Wow Hi Hi"));
1307 EXPECT_TRUE(m2.Matches("Super Hi"));
1308 EXPECT_FALSE(m2.Matches("i"));
1309 EXPECT_FALSE(m2.Matches("Hi "));
1310}
1311
1312TEST(EndsWithTest, CanDescribeSelf) {
1313 Matcher<const std::string> m = EndsWith("Hi");
1314 EXPECT_EQ("ends with \"Hi\"", Describe(m));
1315}
1316
shiqiane35fdd92008-12-10 05:08:54 +00001317// Tests MatchesRegex().
1318
1319TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
1320 const Matcher<const char*> m1 = MatchesRegex("a.*z");
1321 EXPECT_TRUE(m1.Matches("az"));
1322 EXPECT_TRUE(m1.Matches("abcz"));
1323 EXPECT_FALSE(m1.Matches(NULL));
1324
1325 const Matcher<const string&> m2 = MatchesRegex(new RE("a.*z"));
1326 EXPECT_TRUE(m2.Matches("azbz"));
1327 EXPECT_FALSE(m2.Matches("az1"));
1328 EXPECT_FALSE(m2.Matches("1az"));
1329}
1330
1331TEST(MatchesRegexTest, CanDescribeSelf) {
1332 Matcher<const std::string> m1 = MatchesRegex(string("Hi.*"));
1333 EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1));
1334
zhanyong.wand14aaed2010-01-14 05:36:32 +00001335 Matcher<const char*> m2 = MatchesRegex(new RE("a.*"));
1336 EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +00001337}
1338
1339// Tests ContainsRegex().
1340
1341TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
1342 const Matcher<const char*> m1 = ContainsRegex(string("a.*z"));
1343 EXPECT_TRUE(m1.Matches("az"));
1344 EXPECT_TRUE(m1.Matches("0abcz1"));
1345 EXPECT_FALSE(m1.Matches(NULL));
1346
1347 const Matcher<const string&> m2 = ContainsRegex(new RE("a.*z"));
1348 EXPECT_TRUE(m2.Matches("azbz"));
1349 EXPECT_TRUE(m2.Matches("az1"));
1350 EXPECT_FALSE(m2.Matches("1a"));
1351}
1352
1353TEST(ContainsRegexTest, CanDescribeSelf) {
1354 Matcher<const std::string> m1 = ContainsRegex("Hi.*");
1355 EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1));
1356
zhanyong.wand14aaed2010-01-14 05:36:32 +00001357 Matcher<const char*> m2 = ContainsRegex(new RE("a.*"));
1358 EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +00001359}
shiqiane35fdd92008-12-10 05:08:54 +00001360
1361// Tests for wide strings.
1362#if GTEST_HAS_STD_WSTRING
1363TEST(StdWideStrEqTest, MatchesEqual) {
1364 Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello"));
1365 EXPECT_TRUE(m.Matches(L"Hello"));
1366 EXPECT_FALSE(m.Matches(L"hello"));
1367 EXPECT_FALSE(m.Matches(NULL));
1368
1369 Matcher<const ::std::wstring&> m2 = StrEq(L"Hello");
1370 EXPECT_TRUE(m2.Matches(L"Hello"));
1371 EXPECT_FALSE(m2.Matches(L"Hi"));
1372
1373 Matcher<const ::std::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1374 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1375 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1376
1377 ::std::wstring str(L"01204500800");
1378 str[3] = L'\0';
1379 Matcher<const ::std::wstring&> m4 = StrEq(str);
1380 EXPECT_TRUE(m4.Matches(str));
1381 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1382 Matcher<const ::std::wstring&> m5 = StrEq(str);
1383 EXPECT_TRUE(m5.Matches(str));
1384}
1385
1386TEST(StdWideStrEqTest, CanDescribeSelf) {
1387 Matcher< ::std::wstring> m = StrEq(L"Hi-\'\"\?\\\a\b\f\n\r\t\v");
1388 EXPECT_EQ("is equal to L\"Hi-\'\\\"\\?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
1389 Describe(m));
1390
1391 Matcher< ::std::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1392 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1393 Describe(m2));
1394
1395 ::std::wstring str(L"01204500800");
1396 str[3] = L'\0';
1397 Matcher<const ::std::wstring&> m4 = StrEq(str);
1398 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1399 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1400 Matcher<const ::std::wstring&> m5 = StrEq(str);
1401 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1402}
1403
1404TEST(StdWideStrNeTest, MatchesUnequalString) {
1405 Matcher<const wchar_t*> m = StrNe(L"Hello");
1406 EXPECT_TRUE(m.Matches(L""));
1407 EXPECT_TRUE(m.Matches(NULL));
1408 EXPECT_FALSE(m.Matches(L"Hello"));
1409
1410 Matcher< ::std::wstring> m2 = StrNe(::std::wstring(L"Hello"));
1411 EXPECT_TRUE(m2.Matches(L"hello"));
1412 EXPECT_FALSE(m2.Matches(L"Hello"));
1413}
1414
1415TEST(StdWideStrNeTest, CanDescribeSelf) {
1416 Matcher<const wchar_t*> m = StrNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001417 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001418}
1419
1420TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1421 Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello"));
1422 EXPECT_TRUE(m.Matches(L"Hello"));
1423 EXPECT_TRUE(m.Matches(L"hello"));
1424 EXPECT_FALSE(m.Matches(L"Hi"));
1425 EXPECT_FALSE(m.Matches(NULL));
1426
1427 Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello");
1428 EXPECT_TRUE(m2.Matches(L"hello"));
1429 EXPECT_FALSE(m2.Matches(L"Hi"));
1430}
1431
1432TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1433 ::std::wstring str1(L"oabocdooeoo");
1434 ::std::wstring str2(L"OABOCDOOEOO");
1435 Matcher<const ::std::wstring&> m0 = StrCaseEq(str1);
1436 EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L'\0')));
1437
1438 str1[3] = str2[3] = L'\0';
1439 Matcher<const ::std::wstring&> m1 = StrCaseEq(str1);
1440 EXPECT_TRUE(m1.Matches(str2));
1441
1442 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1443 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1444 Matcher<const ::std::wstring&> m2 = StrCaseEq(str1);
1445 str1[9] = str2[9] = L'\0';
1446 EXPECT_FALSE(m2.Matches(str2));
1447
1448 Matcher<const ::std::wstring&> m3 = StrCaseEq(str1);
1449 EXPECT_TRUE(m3.Matches(str2));
1450
1451 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1452 str2.append(1, L'\0');
1453 EXPECT_FALSE(m3.Matches(str2));
1454 EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9)));
1455}
1456
1457TEST(StdWideStrCaseEqTest, CanDescribeSelf) {
1458 Matcher< ::std::wstring> m = StrCaseEq(L"Hi");
1459 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1460}
1461
1462TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1463 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1464 EXPECT_TRUE(m.Matches(L"Hi"));
1465 EXPECT_TRUE(m.Matches(NULL));
1466 EXPECT_FALSE(m.Matches(L"Hello"));
1467 EXPECT_FALSE(m.Matches(L"hello"));
1468
1469 Matcher< ::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello"));
1470 EXPECT_TRUE(m2.Matches(L""));
1471 EXPECT_FALSE(m2.Matches(L"Hello"));
1472}
1473
1474TEST(StdWideStrCaseNeTest, CanDescribeSelf) {
1475 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001476 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001477}
1478
1479// Tests that HasSubstr() works for matching wstring-typed values.
1480TEST(StdWideHasSubstrTest, WorksForStringClasses) {
1481 const Matcher< ::std::wstring> m1 = HasSubstr(L"foo");
1482 EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food.")));
1483 EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo")));
1484
1485 const Matcher<const ::std::wstring&> m2 = HasSubstr(L"foo");
1486 EXPECT_TRUE(m2.Matches(::std::wstring(L"I love food.")));
1487 EXPECT_FALSE(m2.Matches(::std::wstring(L"tofo")));
1488}
1489
1490// Tests that HasSubstr() works for matching C-wide-string-typed values.
1491TEST(StdWideHasSubstrTest, WorksForCStrings) {
1492 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1493 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1494 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1495 EXPECT_FALSE(m1.Matches(NULL));
1496
1497 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1498 EXPECT_TRUE(m2.Matches(L"I love food."));
1499 EXPECT_FALSE(m2.Matches(L"tofo"));
1500 EXPECT_FALSE(m2.Matches(NULL));
1501}
1502
1503// Tests that HasSubstr(s) describes itself properly.
1504TEST(StdWideHasSubstrTest, CanDescribeSelf) {
1505 Matcher< ::std::wstring> m = HasSubstr(L"foo\n\"");
1506 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1507}
1508
1509// Tests StartsWith(s).
1510
1511TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) {
1512 const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L""));
1513 EXPECT_TRUE(m1.Matches(L"Hi"));
1514 EXPECT_TRUE(m1.Matches(L""));
1515 EXPECT_FALSE(m1.Matches(NULL));
1516
1517 const Matcher<const ::std::wstring&> m2 = StartsWith(L"Hi");
1518 EXPECT_TRUE(m2.Matches(L"Hi"));
1519 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1520 EXPECT_TRUE(m2.Matches(L"High"));
1521 EXPECT_FALSE(m2.Matches(L"H"));
1522 EXPECT_FALSE(m2.Matches(L" Hi"));
1523}
1524
1525TEST(StdWideStartsWithTest, CanDescribeSelf) {
1526 Matcher<const ::std::wstring> m = StartsWith(L"Hi");
1527 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1528}
1529
1530// Tests EndsWith(s).
1531
1532TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) {
1533 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1534 EXPECT_TRUE(m1.Matches(L"Hi"));
1535 EXPECT_TRUE(m1.Matches(L""));
1536 EXPECT_FALSE(m1.Matches(NULL));
1537
1538 const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L"Hi"));
1539 EXPECT_TRUE(m2.Matches(L"Hi"));
1540 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1541 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1542 EXPECT_FALSE(m2.Matches(L"i"));
1543 EXPECT_FALSE(m2.Matches(L"Hi "));
1544}
1545
1546TEST(StdWideEndsWithTest, CanDescribeSelf) {
1547 Matcher<const ::std::wstring> m = EndsWith(L"Hi");
1548 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1549}
1550
1551#endif // GTEST_HAS_STD_WSTRING
1552
1553#if GTEST_HAS_GLOBAL_WSTRING
1554TEST(GlobalWideStrEqTest, MatchesEqual) {
1555 Matcher<const wchar_t*> m = StrEq(::wstring(L"Hello"));
1556 EXPECT_TRUE(m.Matches(L"Hello"));
1557 EXPECT_FALSE(m.Matches(L"hello"));
1558 EXPECT_FALSE(m.Matches(NULL));
1559
1560 Matcher<const ::wstring&> m2 = StrEq(L"Hello");
1561 EXPECT_TRUE(m2.Matches(L"Hello"));
1562 EXPECT_FALSE(m2.Matches(L"Hi"));
1563
1564 Matcher<const ::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1565 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1566 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1567
1568 ::wstring str(L"01204500800");
1569 str[3] = L'\0';
1570 Matcher<const ::wstring&> m4 = StrEq(str);
1571 EXPECT_TRUE(m4.Matches(str));
1572 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1573 Matcher<const ::wstring&> m5 = StrEq(str);
1574 EXPECT_TRUE(m5.Matches(str));
1575}
1576
1577TEST(GlobalWideStrEqTest, CanDescribeSelf) {
1578 Matcher< ::wstring> m = StrEq(L"Hi-\'\"\?\\\a\b\f\n\r\t\v");
1579 EXPECT_EQ("is equal to L\"Hi-\'\\\"\\?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
1580 Describe(m));
1581
1582 Matcher< ::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1583 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1584 Describe(m2));
1585
1586 ::wstring str(L"01204500800");
1587 str[3] = L'\0';
1588 Matcher<const ::wstring&> m4 = StrEq(str);
1589 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1590 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1591 Matcher<const ::wstring&> m5 = StrEq(str);
1592 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1593}
1594
1595TEST(GlobalWideStrNeTest, MatchesUnequalString) {
1596 Matcher<const wchar_t*> m = StrNe(L"Hello");
1597 EXPECT_TRUE(m.Matches(L""));
1598 EXPECT_TRUE(m.Matches(NULL));
1599 EXPECT_FALSE(m.Matches(L"Hello"));
1600
1601 Matcher< ::wstring> m2 = StrNe(::wstring(L"Hello"));
1602 EXPECT_TRUE(m2.Matches(L"hello"));
1603 EXPECT_FALSE(m2.Matches(L"Hello"));
1604}
1605
1606TEST(GlobalWideStrNeTest, CanDescribeSelf) {
1607 Matcher<const wchar_t*> m = StrNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001608 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001609}
1610
1611TEST(GlobalWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1612 Matcher<const wchar_t*> m = StrCaseEq(::wstring(L"Hello"));
1613 EXPECT_TRUE(m.Matches(L"Hello"));
1614 EXPECT_TRUE(m.Matches(L"hello"));
1615 EXPECT_FALSE(m.Matches(L"Hi"));
1616 EXPECT_FALSE(m.Matches(NULL));
1617
1618 Matcher<const ::wstring&> m2 = StrCaseEq(L"Hello");
1619 EXPECT_TRUE(m2.Matches(L"hello"));
1620 EXPECT_FALSE(m2.Matches(L"Hi"));
1621}
1622
1623TEST(GlobalWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1624 ::wstring str1(L"oabocdooeoo");
1625 ::wstring str2(L"OABOCDOOEOO");
1626 Matcher<const ::wstring&> m0 = StrCaseEq(str1);
1627 EXPECT_FALSE(m0.Matches(str2 + ::wstring(1, L'\0')));
1628
1629 str1[3] = str2[3] = L'\0';
1630 Matcher<const ::wstring&> m1 = StrCaseEq(str1);
1631 EXPECT_TRUE(m1.Matches(str2));
1632
1633 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1634 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1635 Matcher<const ::wstring&> m2 = StrCaseEq(str1);
1636 str1[9] = str2[9] = L'\0';
1637 EXPECT_FALSE(m2.Matches(str2));
1638
1639 Matcher<const ::wstring&> m3 = StrCaseEq(str1);
1640 EXPECT_TRUE(m3.Matches(str2));
1641
1642 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1643 str2.append(1, L'\0');
1644 EXPECT_FALSE(m3.Matches(str2));
1645 EXPECT_FALSE(m3.Matches(::wstring(str2, 0, 9)));
1646}
1647
1648TEST(GlobalWideStrCaseEqTest, CanDescribeSelf) {
1649 Matcher< ::wstring> m = StrCaseEq(L"Hi");
1650 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1651}
1652
1653TEST(GlobalWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1654 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1655 EXPECT_TRUE(m.Matches(L"Hi"));
1656 EXPECT_TRUE(m.Matches(NULL));
1657 EXPECT_FALSE(m.Matches(L"Hello"));
1658 EXPECT_FALSE(m.Matches(L"hello"));
1659
1660 Matcher< ::wstring> m2 = StrCaseNe(::wstring(L"Hello"));
1661 EXPECT_TRUE(m2.Matches(L""));
1662 EXPECT_FALSE(m2.Matches(L"Hello"));
1663}
1664
1665TEST(GlobalWideStrCaseNeTest, CanDescribeSelf) {
1666 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001667 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001668}
1669
1670// Tests that HasSubstr() works for matching wstring-typed values.
1671TEST(GlobalWideHasSubstrTest, WorksForStringClasses) {
1672 const Matcher< ::wstring> m1 = HasSubstr(L"foo");
1673 EXPECT_TRUE(m1.Matches(::wstring(L"I love food.")));
1674 EXPECT_FALSE(m1.Matches(::wstring(L"tofo")));
1675
1676 const Matcher<const ::wstring&> m2 = HasSubstr(L"foo");
1677 EXPECT_TRUE(m2.Matches(::wstring(L"I love food.")));
1678 EXPECT_FALSE(m2.Matches(::wstring(L"tofo")));
1679}
1680
1681// Tests that HasSubstr() works for matching C-wide-string-typed values.
1682TEST(GlobalWideHasSubstrTest, WorksForCStrings) {
1683 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1684 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1685 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1686 EXPECT_FALSE(m1.Matches(NULL));
1687
1688 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1689 EXPECT_TRUE(m2.Matches(L"I love food."));
1690 EXPECT_FALSE(m2.Matches(L"tofo"));
1691 EXPECT_FALSE(m2.Matches(NULL));
1692}
1693
1694// Tests that HasSubstr(s) describes itself properly.
1695TEST(GlobalWideHasSubstrTest, CanDescribeSelf) {
1696 Matcher< ::wstring> m = HasSubstr(L"foo\n\"");
1697 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1698}
1699
1700// Tests StartsWith(s).
1701
1702TEST(GlobalWideStartsWithTest, MatchesStringWithGivenPrefix) {
1703 const Matcher<const wchar_t*> m1 = StartsWith(::wstring(L""));
1704 EXPECT_TRUE(m1.Matches(L"Hi"));
1705 EXPECT_TRUE(m1.Matches(L""));
1706 EXPECT_FALSE(m1.Matches(NULL));
1707
1708 const Matcher<const ::wstring&> m2 = StartsWith(L"Hi");
1709 EXPECT_TRUE(m2.Matches(L"Hi"));
1710 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1711 EXPECT_TRUE(m2.Matches(L"High"));
1712 EXPECT_FALSE(m2.Matches(L"H"));
1713 EXPECT_FALSE(m2.Matches(L" Hi"));
1714}
1715
1716TEST(GlobalWideStartsWithTest, CanDescribeSelf) {
1717 Matcher<const ::wstring> m = StartsWith(L"Hi");
1718 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1719}
1720
1721// Tests EndsWith(s).
1722
1723TEST(GlobalWideEndsWithTest, MatchesStringWithGivenSuffix) {
1724 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1725 EXPECT_TRUE(m1.Matches(L"Hi"));
1726 EXPECT_TRUE(m1.Matches(L""));
1727 EXPECT_FALSE(m1.Matches(NULL));
1728
1729 const Matcher<const ::wstring&> m2 = EndsWith(::wstring(L"Hi"));
1730 EXPECT_TRUE(m2.Matches(L"Hi"));
1731 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1732 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1733 EXPECT_FALSE(m2.Matches(L"i"));
1734 EXPECT_FALSE(m2.Matches(L"Hi "));
1735}
1736
1737TEST(GlobalWideEndsWithTest, CanDescribeSelf) {
1738 Matcher<const ::wstring> m = EndsWith(L"Hi");
1739 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1740}
1741
1742#endif // GTEST_HAS_GLOBAL_WSTRING
1743
1744
1745typedef ::std::tr1::tuple<long, int> Tuple2; // NOLINT
1746
1747// Tests that Eq() matches a 2-tuple where the first field == the
1748// second field.
1749TEST(Eq2Test, MatchesEqualArguments) {
1750 Matcher<const Tuple2&> m = Eq();
1751 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1752 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1753}
1754
1755// Tests that Eq() describes itself properly.
1756TEST(Eq2Test, CanDescribeSelf) {
1757 Matcher<const Tuple2&> m = Eq();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001758 EXPECT_EQ("are an equal pair", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001759}
1760
1761// Tests that Ge() matches a 2-tuple where the first field >= the
1762// second field.
1763TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) {
1764 Matcher<const Tuple2&> m = Ge();
1765 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1766 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1767 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1768}
1769
1770// Tests that Ge() describes itself properly.
1771TEST(Ge2Test, CanDescribeSelf) {
1772 Matcher<const Tuple2&> m = Ge();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001773 EXPECT_EQ("are a pair where the first >= the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001774}
1775
1776// Tests that Gt() matches a 2-tuple where the first field > the
1777// second field.
1778TEST(Gt2Test, MatchesGreaterThanArguments) {
1779 Matcher<const Tuple2&> m = Gt();
1780 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1781 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1782 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1783}
1784
1785// Tests that Gt() describes itself properly.
1786TEST(Gt2Test, CanDescribeSelf) {
1787 Matcher<const Tuple2&> m = Gt();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001788 EXPECT_EQ("are a pair where the first > the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001789}
1790
1791// Tests that Le() matches a 2-tuple where the first field <= the
1792// second field.
1793TEST(Le2Test, MatchesLessThanOrEqualArguments) {
1794 Matcher<const Tuple2&> m = Le();
1795 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1796 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1797 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1798}
1799
1800// Tests that Le() describes itself properly.
1801TEST(Le2Test, CanDescribeSelf) {
1802 Matcher<const Tuple2&> m = Le();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001803 EXPECT_EQ("are a pair where the first <= the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001804}
1805
1806// Tests that Lt() matches a 2-tuple where the first field < the
1807// second field.
1808TEST(Lt2Test, MatchesLessThanArguments) {
1809 Matcher<const Tuple2&> m = Lt();
1810 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1811 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1812 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1813}
1814
1815// Tests that Lt() describes itself properly.
1816TEST(Lt2Test, CanDescribeSelf) {
1817 Matcher<const Tuple2&> m = Lt();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001818 EXPECT_EQ("are a pair where the first < the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001819}
1820
1821// Tests that Ne() matches a 2-tuple where the first field != the
1822// second field.
1823TEST(Ne2Test, MatchesUnequalArguments) {
1824 Matcher<const Tuple2&> m = Ne();
1825 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1826 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1827 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1828}
1829
1830// Tests that Ne() describes itself properly.
1831TEST(Ne2Test, CanDescribeSelf) {
1832 Matcher<const Tuple2&> m = Ne();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001833 EXPECT_EQ("are an unequal pair", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001834}
1835
1836// Tests that Not(m) matches any value that doesn't match m.
1837TEST(NotTest, NegatesMatcher) {
1838 Matcher<int> m;
1839 m = Not(Eq(2));
1840 EXPECT_TRUE(m.Matches(3));
1841 EXPECT_FALSE(m.Matches(2));
1842}
1843
1844// Tests that Not(m) describes itself properly.
1845TEST(NotTest, CanDescribeSelf) {
1846 Matcher<int> m = Not(Eq(5));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001847 EXPECT_EQ("isn't equal to 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001848}
1849
zhanyong.wan18490652009-05-11 18:54:08 +00001850// Tests that monomorphic matchers are safely cast by the Not matcher.
1851TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {
1852 // greater_than_5 is a monomorphic matcher.
1853 Matcher<int> greater_than_5 = Gt(5);
1854
1855 Matcher<const int&> m = Not(greater_than_5);
1856 Matcher<int&> m2 = Not(greater_than_5);
1857 Matcher<int&> m3 = Not(m);
1858}
1859
shiqiane35fdd92008-12-10 05:08:54 +00001860// Tests that AllOf(m1, ..., mn) matches any value that matches all of
1861// the given matchers.
1862TEST(AllOfTest, MatchesWhenAllMatch) {
1863 Matcher<int> m;
1864 m = AllOf(Le(2), Ge(1));
1865 EXPECT_TRUE(m.Matches(1));
1866 EXPECT_TRUE(m.Matches(2));
1867 EXPECT_FALSE(m.Matches(0));
1868 EXPECT_FALSE(m.Matches(3));
1869
1870 m = AllOf(Gt(0), Ne(1), Ne(2));
1871 EXPECT_TRUE(m.Matches(3));
1872 EXPECT_FALSE(m.Matches(2));
1873 EXPECT_FALSE(m.Matches(1));
1874 EXPECT_FALSE(m.Matches(0));
1875
1876 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
1877 EXPECT_TRUE(m.Matches(4));
1878 EXPECT_FALSE(m.Matches(3));
1879 EXPECT_FALSE(m.Matches(2));
1880 EXPECT_FALSE(m.Matches(1));
1881 EXPECT_FALSE(m.Matches(0));
1882
1883 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
1884 EXPECT_TRUE(m.Matches(0));
1885 EXPECT_TRUE(m.Matches(1));
1886 EXPECT_FALSE(m.Matches(3));
1887}
1888
1889// Tests that AllOf(m1, ..., mn) describes itself properly.
1890TEST(AllOfTest, CanDescribeSelf) {
1891 Matcher<int> m;
1892 m = AllOf(Le(2), Ge(1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001893 EXPECT_EQ("(is <= 2) and (is >= 1)", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001894
1895 m = AllOf(Gt(0), Ne(1), Ne(2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001896 EXPECT_EQ("(is > 0) and "
1897 "((isn't equal to 1) and "
1898 "(isn't equal to 2))",
shiqiane35fdd92008-12-10 05:08:54 +00001899 Describe(m));
1900
1901
1902 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001903 EXPECT_EQ("(is > 0) and "
1904 "((isn't equal to 1) and "
1905 "((isn't equal to 2) and "
1906 "(isn't equal to 3)))",
shiqiane35fdd92008-12-10 05:08:54 +00001907 Describe(m));
1908
1909
1910 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001911 EXPECT_EQ("(is >= 0) and "
1912 "((is < 10) and "
1913 "((isn't equal to 3) and "
1914 "((isn't equal to 5) and "
1915 "(isn't equal to 7))))",
1916 Describe(m));
1917}
1918
1919// Tests that AllOf(m1, ..., mn) describes its negation properly.
1920TEST(AllOfTest, CanDescribeNegation) {
1921 Matcher<int> m;
1922 m = AllOf(Le(2), Ge(1));
1923 EXPECT_EQ("(isn't <= 2) or "
1924 "(isn't >= 1)",
1925 DescribeNegation(m));
1926
1927 m = AllOf(Gt(0), Ne(1), Ne(2));
1928 EXPECT_EQ("(isn't > 0) or "
1929 "((is equal to 1) or "
1930 "(is equal to 2))",
1931 DescribeNegation(m));
1932
1933
1934 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
1935 EXPECT_EQ("(isn't > 0) or "
1936 "((is equal to 1) or "
1937 "((is equal to 2) or "
1938 "(is equal to 3)))",
1939 DescribeNegation(m));
1940
1941
1942 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
1943 EXPECT_EQ("(isn't >= 0) or "
1944 "((isn't < 10) or "
1945 "((is equal to 3) or "
1946 "((is equal to 5) or "
1947 "(is equal to 7))))",
1948 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00001949}
1950
zhanyong.wan18490652009-05-11 18:54:08 +00001951// Tests that monomorphic matchers are safely cast by the AllOf matcher.
1952TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
1953 // greater_than_5 and less_than_10 are monomorphic matchers.
1954 Matcher<int> greater_than_5 = Gt(5);
1955 Matcher<int> less_than_10 = Lt(10);
1956
1957 Matcher<const int&> m = AllOf(greater_than_5, less_than_10);
1958 Matcher<int&> m2 = AllOf(greater_than_5, less_than_10);
1959 Matcher<int&> m3 = AllOf(greater_than_5, m2);
1960
1961 // Tests that BothOf works when composing itself.
1962 Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10);
1963 Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);
1964}
1965
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001966TEST(AllOfTest, ExplainsResult) {
1967 Matcher<int> m;
1968
1969 // Successful match. Both matchers need to explain. The second
1970 // matcher doesn't give an explanation, so only the first matcher's
1971 // explanation is printed.
1972 m = AllOf(GreaterThan(10), Lt(30));
1973 EXPECT_EQ("which is 15 more than 10", Explain(m, 25));
1974
1975 // Successful match. Both matchers need to explain.
1976 m = AllOf(GreaterThan(10), GreaterThan(20));
1977 EXPECT_EQ("which is 20 more than 10, and which is 10 more than 20",
1978 Explain(m, 30));
1979
1980 // Successful match. All matchers need to explain. The second
1981 // matcher doesn't given an explanation.
1982 m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20));
1983 EXPECT_EQ("which is 15 more than 10, and which is 5 more than 20",
1984 Explain(m, 25));
1985
1986 // Successful match. All matchers need to explain.
1987 m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
1988 EXPECT_EQ("which is 30 more than 10, and which is 20 more than 20, "
1989 "and which is 10 more than 30",
1990 Explain(m, 40));
1991
1992 // Failed match. The first matcher, which failed, needs to
1993 // explain.
1994 m = AllOf(GreaterThan(10), GreaterThan(20));
1995 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
1996
1997 // Failed match. The second matcher, which failed, needs to
1998 // explain. Since it doesn't given an explanation, nothing is
1999 // printed.
2000 m = AllOf(GreaterThan(10), Lt(30));
2001 EXPECT_EQ("", Explain(m, 40));
2002
2003 // Failed match. The second matcher, which failed, needs to
2004 // explain.
2005 m = AllOf(GreaterThan(10), GreaterThan(20));
2006 EXPECT_EQ("which is 5 less than 20", Explain(m, 15));
2007}
2008
shiqiane35fdd92008-12-10 05:08:54 +00002009// Tests that AnyOf(m1, ..., mn) matches any value that matches at
2010// least one of the given matchers.
2011TEST(AnyOfTest, MatchesWhenAnyMatches) {
2012 Matcher<int> m;
2013 m = AnyOf(Le(1), Ge(3));
2014 EXPECT_TRUE(m.Matches(1));
2015 EXPECT_TRUE(m.Matches(4));
2016 EXPECT_FALSE(m.Matches(2));
2017
2018 m = AnyOf(Lt(0), Eq(1), Eq(2));
2019 EXPECT_TRUE(m.Matches(-1));
2020 EXPECT_TRUE(m.Matches(1));
2021 EXPECT_TRUE(m.Matches(2));
2022 EXPECT_FALSE(m.Matches(0));
2023
2024 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2025 EXPECT_TRUE(m.Matches(-1));
2026 EXPECT_TRUE(m.Matches(1));
2027 EXPECT_TRUE(m.Matches(2));
2028 EXPECT_TRUE(m.Matches(3));
2029 EXPECT_FALSE(m.Matches(0));
2030
2031 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2032 EXPECT_TRUE(m.Matches(0));
2033 EXPECT_TRUE(m.Matches(11));
2034 EXPECT_TRUE(m.Matches(3));
2035 EXPECT_FALSE(m.Matches(2));
2036}
2037
2038// Tests that AnyOf(m1, ..., mn) describes itself properly.
2039TEST(AnyOfTest, CanDescribeSelf) {
2040 Matcher<int> m;
2041 m = AnyOf(Le(1), Ge(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002042 EXPECT_EQ("(is <= 1) or (is >= 3)",
shiqiane35fdd92008-12-10 05:08:54 +00002043 Describe(m));
2044
2045 m = AnyOf(Lt(0), Eq(1), Eq(2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002046 EXPECT_EQ("(is < 0) or "
shiqiane35fdd92008-12-10 05:08:54 +00002047 "((is equal to 1) or (is equal to 2))",
2048 Describe(m));
2049
2050 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002051 EXPECT_EQ("(is < 0) or "
shiqiane35fdd92008-12-10 05:08:54 +00002052 "((is equal to 1) or "
2053 "((is equal to 2) or "
2054 "(is equal to 3)))",
2055 Describe(m));
2056
2057 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002058 EXPECT_EQ("(is <= 0) or "
2059 "((is > 10) or "
shiqiane35fdd92008-12-10 05:08:54 +00002060 "((is equal to 3) or "
2061 "((is equal to 5) or "
2062 "(is equal to 7))))",
2063 Describe(m));
2064}
2065
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002066// Tests that AnyOf(m1, ..., mn) describes its negation properly.
2067TEST(AnyOfTest, CanDescribeNegation) {
2068 Matcher<int> m;
2069 m = AnyOf(Le(1), Ge(3));
2070 EXPECT_EQ("(isn't <= 1) and (isn't >= 3)",
2071 DescribeNegation(m));
2072
2073 m = AnyOf(Lt(0), Eq(1), Eq(2));
2074 EXPECT_EQ("(isn't < 0) and "
2075 "((isn't equal to 1) and (isn't equal to 2))",
2076 DescribeNegation(m));
2077
2078 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2079 EXPECT_EQ("(isn't < 0) and "
2080 "((isn't equal to 1) and "
2081 "((isn't equal to 2) and "
2082 "(isn't equal to 3)))",
2083 DescribeNegation(m));
2084
2085 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2086 EXPECT_EQ("(isn't <= 0) and "
2087 "((isn't > 10) and "
2088 "((isn't equal to 3) and "
2089 "((isn't equal to 5) and "
2090 "(isn't equal to 7))))",
2091 DescribeNegation(m));
2092}
2093
zhanyong.wan18490652009-05-11 18:54:08 +00002094// Tests that monomorphic matchers are safely cast by the AnyOf matcher.
2095TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
2096 // greater_than_5 and less_than_10 are monomorphic matchers.
2097 Matcher<int> greater_than_5 = Gt(5);
2098 Matcher<int> less_than_10 = Lt(10);
2099
2100 Matcher<const int&> m = AnyOf(greater_than_5, less_than_10);
2101 Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10);
2102 Matcher<int&> m3 = AnyOf(greater_than_5, m2);
2103
2104 // Tests that EitherOf works when composing itself.
2105 Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10);
2106 Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);
2107}
2108
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002109TEST(AnyOfTest, ExplainsResult) {
2110 Matcher<int> m;
2111
2112 // Failed match. Both matchers need to explain. The second
2113 // matcher doesn't give an explanation, so only the first matcher's
2114 // explanation is printed.
2115 m = AnyOf(GreaterThan(10), Lt(0));
2116 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
2117
2118 // Failed match. Both matchers need to explain.
2119 m = AnyOf(GreaterThan(10), GreaterThan(20));
2120 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20",
2121 Explain(m, 5));
2122
2123 // Failed match. All matchers need to explain. The second
2124 // matcher doesn't given an explanation.
2125 m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30));
2126 EXPECT_EQ("which is 5 less than 10, and which is 25 less than 30",
2127 Explain(m, 5));
2128
2129 // Failed match. All matchers need to explain.
2130 m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2131 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20, "
2132 "and which is 25 less than 30",
2133 Explain(m, 5));
2134
2135 // Successful match. The first matcher, which succeeded, needs to
2136 // explain.
2137 m = AnyOf(GreaterThan(10), GreaterThan(20));
2138 EXPECT_EQ("which is 5 more than 10", Explain(m, 15));
2139
2140 // Successful match. The second matcher, which succeeded, needs to
2141 // explain. Since it doesn't given an explanation, nothing is
2142 // printed.
2143 m = AnyOf(GreaterThan(10), Lt(30));
2144 EXPECT_EQ("", Explain(m, 0));
2145
2146 // Successful match. The second matcher, which succeeded, needs to
2147 // explain.
2148 m = AnyOf(GreaterThan(30), GreaterThan(20));
2149 EXPECT_EQ("which is 5 more than 20", Explain(m, 25));
2150}
2151
shiqiane35fdd92008-12-10 05:08:54 +00002152// The following predicate function and predicate functor are for
2153// testing the Truly(predicate) matcher.
2154
2155// Returns non-zero if the input is positive. Note that the return
2156// type of this function is not bool. It's OK as Truly() accepts any
2157// unary function or functor whose return type can be implicitly
2158// converted to bool.
2159int IsPositive(double x) {
2160 return x > 0 ? 1 : 0;
2161}
2162
2163// This functor returns true if the input is greater than the given
2164// number.
2165class IsGreaterThan {
2166 public:
2167 explicit IsGreaterThan(int threshold) : threshold_(threshold) {}
2168
2169 bool operator()(int n) const { return n > threshold_; }
zhanyong.wan32de5f52009-12-23 00:13:23 +00002170
shiqiane35fdd92008-12-10 05:08:54 +00002171 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002172 int threshold_;
shiqiane35fdd92008-12-10 05:08:54 +00002173};
2174
2175// For testing Truly().
2176const int foo = 0;
2177
2178// This predicate returns true iff the argument references foo and has
2179// a zero value.
2180bool ReferencesFooAndIsZero(const int& n) {
2181 return (&n == &foo) && (n == 0);
2182}
2183
2184// Tests that Truly(predicate) matches what satisfies the given
2185// predicate.
2186TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {
2187 Matcher<double> m = Truly(IsPositive);
2188 EXPECT_TRUE(m.Matches(2.0));
2189 EXPECT_FALSE(m.Matches(-1.5));
2190}
2191
2192// Tests that Truly(predicate_functor) works too.
2193TEST(TrulyTest, CanBeUsedWithFunctor) {
2194 Matcher<int> m = Truly(IsGreaterThan(5));
2195 EXPECT_TRUE(m.Matches(6));
2196 EXPECT_FALSE(m.Matches(4));
2197}
2198
2199// Tests that Truly(predicate) can describe itself properly.
2200TEST(TrulyTest, CanDescribeSelf) {
2201 Matcher<double> m = Truly(IsPositive);
2202 EXPECT_EQ("satisfies the given predicate",
2203 Describe(m));
2204}
2205
2206// Tests that Truly(predicate) works when the matcher takes its
2207// argument by reference.
2208TEST(TrulyTest, WorksForByRefArguments) {
2209 Matcher<const int&> m = Truly(ReferencesFooAndIsZero);
2210 EXPECT_TRUE(m.Matches(foo));
2211 int n = 0;
2212 EXPECT_FALSE(m.Matches(n));
2213}
2214
2215// Tests that Matches(m) is a predicate satisfied by whatever that
2216// matches matcher m.
2217TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {
2218 EXPECT_TRUE(Matches(Ge(0))(1));
2219 EXPECT_FALSE(Matches(Eq('a'))('b'));
2220}
2221
2222// Tests that Matches(m) works when the matcher takes its argument by
2223// reference.
2224TEST(MatchesTest, WorksOnByRefArguments) {
2225 int m = 0, n = 0;
2226 EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n));
2227 EXPECT_FALSE(Matches(Ref(m))(n));
2228}
2229
2230// Tests that a Matcher on non-reference type can be used in
2231// Matches().
2232TEST(MatchesTest, WorksWithMatcherOnNonRefType) {
2233 Matcher<int> eq5 = Eq(5);
2234 EXPECT_TRUE(Matches(eq5)(5));
2235 EXPECT_FALSE(Matches(eq5)(2));
2236}
2237
zhanyong.wanb8243162009-06-04 05:48:20 +00002238// Tests Value(value, matcher). Since Value() is a simple wrapper for
2239// Matches(), which has been tested already, we don't spend a lot of
2240// effort on testing Value().
2241TEST(ValueTest, WorksWithPolymorphicMatcher) {
2242 EXPECT_TRUE(Value("hi", StartsWith("h")));
2243 EXPECT_FALSE(Value(5, Gt(10)));
2244}
2245
2246TEST(ValueTest, WorksWithMonomorphicMatcher) {
2247 const Matcher<int> is_zero = Eq(0);
2248 EXPECT_TRUE(Value(0, is_zero));
2249 EXPECT_FALSE(Value('a', is_zero));
2250
2251 int n = 0;
2252 const Matcher<const int&> ref_n = Ref(n);
2253 EXPECT_TRUE(Value(n, ref_n));
2254 EXPECT_FALSE(Value(1, ref_n));
2255}
2256
zhanyong.wana862f1d2010-03-15 21:23:04 +00002257TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) {
zhanyong.wan34b034c2010-03-05 21:23:23 +00002258 StringMatchResultListener listener1;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002259 EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002260 EXPECT_EQ("% 2 == 0", listener1.str());
2261
2262 StringMatchResultListener listener2;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002263 EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002264 EXPECT_EQ("", listener2.str());
2265}
2266
zhanyong.wana862f1d2010-03-15 21:23:04 +00002267TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) {
zhanyong.wan34b034c2010-03-05 21:23:23 +00002268 const Matcher<int> is_even = PolymorphicIsEven();
2269 StringMatchResultListener listener1;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002270 EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002271 EXPECT_EQ("% 2 == 0", listener1.str());
2272
2273 const Matcher<const double&> is_zero = Eq(0);
2274 StringMatchResultListener listener2;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002275 EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002276 EXPECT_EQ("", listener2.str());
2277}
2278
zhanyong.wana862f1d2010-03-15 21:23:04 +00002279MATCHER_P(Really, inner_matcher, "") {
2280 return ExplainMatchResult(inner_matcher, arg, result_listener);
2281}
2282
2283TEST(ExplainMatchResultTest, WorksInsideMATCHER) {
2284 EXPECT_THAT(0, Really(Eq(0)));
2285}
2286
zhanyong.wanbf550852009-06-09 06:09:53 +00002287TEST(AllArgsTest, WorksForTuple) {
2288 EXPECT_THAT(make_tuple(1, 2L), AllArgs(Lt()));
2289 EXPECT_THAT(make_tuple(2L, 1), Not(AllArgs(Lt())));
2290}
2291
2292TEST(AllArgsTest, WorksForNonTuple) {
2293 EXPECT_THAT(42, AllArgs(Gt(0)));
2294 EXPECT_THAT('a', Not(AllArgs(Eq('b'))));
2295}
2296
2297class AllArgsHelper {
2298 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002299 AllArgsHelper() {}
2300
zhanyong.wanbf550852009-06-09 06:09:53 +00002301 MOCK_METHOD2(Helper, int(char x, int y));
zhanyong.wan32de5f52009-12-23 00:13:23 +00002302
2303 private:
2304 GTEST_DISALLOW_COPY_AND_ASSIGN_(AllArgsHelper);
zhanyong.wanbf550852009-06-09 06:09:53 +00002305};
2306
2307TEST(AllArgsTest, WorksInWithClause) {
2308 AllArgsHelper helper;
2309 ON_CALL(helper, Helper(_, _))
2310 .With(AllArgs(Lt()))
2311 .WillByDefault(Return(1));
2312 EXPECT_CALL(helper, Helper(_, _));
2313 EXPECT_CALL(helper, Helper(_, _))
2314 .With(AllArgs(Gt()))
2315 .WillOnce(Return(2));
2316
2317 EXPECT_EQ(1, helper.Helper('\1', 2));
2318 EXPECT_EQ(2, helper.Helper('a', 1));
2319}
2320
shiqiane35fdd92008-12-10 05:08:54 +00002321// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2322// matches the matcher.
2323TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {
2324 ASSERT_THAT(5, Ge(2)) << "This should succeed.";
2325 ASSERT_THAT("Foo", EndsWith("oo"));
2326 EXPECT_THAT(2, AllOf(Le(7), Ge(0))) << "This should succeed too.";
2327 EXPECT_THAT("Hello", StartsWith("Hell"));
2328}
2329
2330// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2331// doesn't match the matcher.
2332TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {
2333 // 'n' must be static as it is used in an EXPECT_FATAL_FAILURE(),
2334 // which cannot reference auto variables.
2335 static int n;
2336 n = 5;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002337
2338 // VC++ prior to version 8.0 SP1 has a bug where it will not see any
2339 // functions declared in the namespace scope from within nested classes.
2340 // EXPECT/ASSERT_(NON)FATAL_FAILURE macros use nested classes so that all
2341 // namespace-level functions invoked inside them need to be explicitly
2342 // resolved.
2343 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Gt(10)),
shiqiane35fdd92008-12-10 05:08:54 +00002344 "Value of: n\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002345 "Expected: is > 10\n"
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002346 " Actual: 5");
shiqiane35fdd92008-12-10 05:08:54 +00002347 n = 0;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002348 EXPECT_NONFATAL_FAILURE(
2349 EXPECT_THAT(n, ::testing::AllOf(::testing::Le(7), ::testing::Ge(5))),
2350 "Value of: n\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002351 "Expected: (is <= 7) and (is >= 5)\n"
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002352 " Actual: 0");
shiqiane35fdd92008-12-10 05:08:54 +00002353}
2354
2355// Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument
2356// has a reference type.
2357TEST(MatcherAssertionTest, WorksForByRefArguments) {
2358 // We use a static variable here as EXPECT_FATAL_FAILURE() cannot
2359 // reference auto variables.
2360 static int n;
2361 n = 0;
2362 EXPECT_THAT(n, AllOf(Le(7), Ref(n)));
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002363 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
shiqiane35fdd92008-12-10 05:08:54 +00002364 "Value of: n\n"
2365 "Expected: does not reference the variable @");
2366 // Tests the "Actual" part.
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002367 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002368 "Actual: 0, which is located @");
shiqiane35fdd92008-12-10 05:08:54 +00002369}
2370
zhanyong.wan95b12332009-09-25 18:55:50 +00002371#if !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002372// Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is
2373// monomorphic.
zhanyong.wan95b12332009-09-25 18:55:50 +00002374
2375// ASSERT_THAT("hello", starts_with_he) fails to compile with Nokia's
2376// Symbian compiler: it tries to compile
2377// template<T, U> class MatcherCastImpl { ...
zhanyong.wandb22c222010-01-28 21:52:29 +00002378// virtual bool MatchAndExplain(T x, ...) const {
2379// return source_matcher_.MatchAndExplain(static_cast<U>(x), ...);
zhanyong.wan95b12332009-09-25 18:55:50 +00002380// with U == string and T == const char*
2381// With ASSERT_THAT("hello"...) changed to ASSERT_THAT(string("hello") ... )
2382// the compiler silently crashes with no output.
2383// If MatcherCastImpl is changed to use U(x) instead of static_cast<U>(x)
2384// the code compiles but the converted string is bogus.
shiqiane35fdd92008-12-10 05:08:54 +00002385TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
2386 Matcher<const char*> starts_with_he = StartsWith("he");
2387 ASSERT_THAT("hello", starts_with_he);
2388
2389 Matcher<const string&> ends_with_ok = EndsWith("ok");
2390 ASSERT_THAT("book", ends_with_ok);
2391
2392 Matcher<int> is_greater_than_5 = Gt(5);
2393 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5),
2394 "Value of: 5\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002395 "Expected: is > 5\n"
shiqiane35fdd92008-12-10 05:08:54 +00002396 " Actual: 5");
2397}
zhanyong.wan95b12332009-09-25 18:55:50 +00002398#endif // !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002399
2400// Tests floating-point matchers.
2401template <typename RawType>
2402class FloatingPointTest : public testing::Test {
2403 protected:
2404 typedef typename testing::internal::FloatingPoint<RawType> Floating;
2405 typedef typename Floating::Bits Bits;
2406
2407 virtual void SetUp() {
2408 const size_t max_ulps = Floating::kMaxUlps;
2409
2410 // The bits that represent 0.0.
2411 const Bits zero_bits = Floating(0).bits();
2412
2413 // Makes some numbers close to 0.0.
2414 close_to_positive_zero_ = Floating::ReinterpretBits(zero_bits + max_ulps/2);
2415 close_to_negative_zero_ = -Floating::ReinterpretBits(
2416 zero_bits + max_ulps - max_ulps/2);
2417 further_from_negative_zero_ = -Floating::ReinterpretBits(
2418 zero_bits + max_ulps + 1 - max_ulps/2);
2419
2420 // The bits that represent 1.0.
2421 const Bits one_bits = Floating(1).bits();
2422
2423 // Makes some numbers close to 1.0.
2424 close_to_one_ = Floating::ReinterpretBits(one_bits + max_ulps);
2425 further_from_one_ = Floating::ReinterpretBits(one_bits + max_ulps + 1);
2426
2427 // +infinity.
2428 infinity_ = Floating::Infinity();
2429
2430 // The bits that represent +infinity.
2431 const Bits infinity_bits = Floating(infinity_).bits();
2432
2433 // Makes some numbers close to infinity.
2434 close_to_infinity_ = Floating::ReinterpretBits(infinity_bits - max_ulps);
2435 further_from_infinity_ = Floating::ReinterpretBits(
2436 infinity_bits - max_ulps - 1);
2437
2438 // Makes some NAN's.
2439 nan1_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 1);
2440 nan2_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 200);
2441 }
2442
2443 void TestSize() {
2444 EXPECT_EQ(sizeof(RawType), sizeof(Bits));
2445 }
2446
2447 // A battery of tests for FloatingEqMatcher::Matches.
2448 // matcher_maker is a pointer to a function which creates a FloatingEqMatcher.
2449 void TestMatches(
2450 testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) {
2451 Matcher<RawType> m1 = matcher_maker(0.0);
2452 EXPECT_TRUE(m1.Matches(-0.0));
2453 EXPECT_TRUE(m1.Matches(close_to_positive_zero_));
2454 EXPECT_TRUE(m1.Matches(close_to_negative_zero_));
2455 EXPECT_FALSE(m1.Matches(1.0));
2456
2457 Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_);
2458 EXPECT_FALSE(m2.Matches(further_from_negative_zero_));
2459
2460 Matcher<RawType> m3 = matcher_maker(1.0);
2461 EXPECT_TRUE(m3.Matches(close_to_one_));
2462 EXPECT_FALSE(m3.Matches(further_from_one_));
2463
2464 // Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above.
2465 EXPECT_FALSE(m3.Matches(0.0));
2466
2467 Matcher<RawType> m4 = matcher_maker(-infinity_);
2468 EXPECT_TRUE(m4.Matches(-close_to_infinity_));
2469
2470 Matcher<RawType> m5 = matcher_maker(infinity_);
2471 EXPECT_TRUE(m5.Matches(close_to_infinity_));
2472
2473 // This is interesting as the representations of infinity_ and nan1_
2474 // are only 1 DLP apart.
2475 EXPECT_FALSE(m5.Matches(nan1_));
2476
2477 // matcher_maker can produce a Matcher<const RawType&>, which is needed in
2478 // some cases.
2479 Matcher<const RawType&> m6 = matcher_maker(0.0);
2480 EXPECT_TRUE(m6.Matches(-0.0));
2481 EXPECT_TRUE(m6.Matches(close_to_positive_zero_));
2482 EXPECT_FALSE(m6.Matches(1.0));
2483
2484 // matcher_maker can produce a Matcher<RawType&>, which is needed in some
2485 // cases.
2486 Matcher<RawType&> m7 = matcher_maker(0.0);
2487 RawType x = 0.0;
2488 EXPECT_TRUE(m7.Matches(x));
2489 x = 0.01f;
2490 EXPECT_FALSE(m7.Matches(x));
2491 }
2492
2493 // Pre-calculated numbers to be used by the tests.
2494
2495 static RawType close_to_positive_zero_;
2496 static RawType close_to_negative_zero_;
2497 static RawType further_from_negative_zero_;
2498
2499 static RawType close_to_one_;
2500 static RawType further_from_one_;
2501
2502 static RawType infinity_;
2503 static RawType close_to_infinity_;
2504 static RawType further_from_infinity_;
2505
2506 static RawType nan1_;
2507 static RawType nan2_;
2508};
2509
2510template <typename RawType>
2511RawType FloatingPointTest<RawType>::close_to_positive_zero_;
2512
2513template <typename RawType>
2514RawType FloatingPointTest<RawType>::close_to_negative_zero_;
2515
2516template <typename RawType>
2517RawType FloatingPointTest<RawType>::further_from_negative_zero_;
2518
2519template <typename RawType>
2520RawType FloatingPointTest<RawType>::close_to_one_;
2521
2522template <typename RawType>
2523RawType FloatingPointTest<RawType>::further_from_one_;
2524
2525template <typename RawType>
2526RawType FloatingPointTest<RawType>::infinity_;
2527
2528template <typename RawType>
2529RawType FloatingPointTest<RawType>::close_to_infinity_;
2530
2531template <typename RawType>
2532RawType FloatingPointTest<RawType>::further_from_infinity_;
2533
2534template <typename RawType>
2535RawType FloatingPointTest<RawType>::nan1_;
2536
2537template <typename RawType>
2538RawType FloatingPointTest<RawType>::nan2_;
2539
2540// Instantiate FloatingPointTest for testing floats.
2541typedef FloatingPointTest<float> FloatTest;
2542
2543TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) {
2544 TestMatches(&FloatEq);
2545}
2546
2547TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
2548 TestMatches(&NanSensitiveFloatEq);
2549}
2550
2551TEST_F(FloatTest, FloatEqCannotMatchNaN) {
2552 // FloatEq never matches NaN.
2553 Matcher<float> m = FloatEq(nan1_);
2554 EXPECT_FALSE(m.Matches(nan1_));
2555 EXPECT_FALSE(m.Matches(nan2_));
2556 EXPECT_FALSE(m.Matches(1.0));
2557}
2558
2559TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {
2560 // NanSensitiveFloatEq will match NaN.
2561 Matcher<float> m = NanSensitiveFloatEq(nan1_);
2562 EXPECT_TRUE(m.Matches(nan1_));
2563 EXPECT_TRUE(m.Matches(nan2_));
2564 EXPECT_FALSE(m.Matches(1.0));
2565}
2566
2567TEST_F(FloatTest, FloatEqCanDescribeSelf) {
2568 Matcher<float> m1 = FloatEq(2.0f);
2569 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002570 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002571
2572 Matcher<float> m2 = FloatEq(0.5f);
2573 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002574 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002575
2576 Matcher<float> m3 = FloatEq(nan1_);
2577 EXPECT_EQ("never matches", Describe(m3));
2578 EXPECT_EQ("is anything", DescribeNegation(m3));
2579}
2580
2581TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {
2582 Matcher<float> m1 = NanSensitiveFloatEq(2.0f);
2583 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002584 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002585
2586 Matcher<float> m2 = NanSensitiveFloatEq(0.5f);
2587 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002588 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002589
2590 Matcher<float> m3 = NanSensitiveFloatEq(nan1_);
2591 EXPECT_EQ("is NaN", Describe(m3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002592 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
shiqiane35fdd92008-12-10 05:08:54 +00002593}
2594
2595// Instantiate FloatingPointTest for testing doubles.
2596typedef FloatingPointTest<double> DoubleTest;
2597
2598TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
2599 TestMatches(&DoubleEq);
2600}
2601
2602TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
2603 TestMatches(&NanSensitiveDoubleEq);
2604}
2605
2606TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {
2607 // DoubleEq never matches NaN.
2608 Matcher<double> m = DoubleEq(nan1_);
2609 EXPECT_FALSE(m.Matches(nan1_));
2610 EXPECT_FALSE(m.Matches(nan2_));
2611 EXPECT_FALSE(m.Matches(1.0));
2612}
2613
2614TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {
2615 // NanSensitiveDoubleEq will match NaN.
2616 Matcher<double> m = NanSensitiveDoubleEq(nan1_);
2617 EXPECT_TRUE(m.Matches(nan1_));
2618 EXPECT_TRUE(m.Matches(nan2_));
2619 EXPECT_FALSE(m.Matches(1.0));
2620}
2621
2622TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {
2623 Matcher<double> m1 = DoubleEq(2.0);
2624 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002625 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002626
2627 Matcher<double> m2 = DoubleEq(0.5);
2628 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002629 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002630
2631 Matcher<double> m3 = DoubleEq(nan1_);
2632 EXPECT_EQ("never matches", Describe(m3));
2633 EXPECT_EQ("is anything", DescribeNegation(m3));
2634}
2635
2636TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {
2637 Matcher<double> m1 = NanSensitiveDoubleEq(2.0);
2638 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002639 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002640
2641 Matcher<double> m2 = NanSensitiveDoubleEq(0.5);
2642 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002643 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002644
2645 Matcher<double> m3 = NanSensitiveDoubleEq(nan1_);
2646 EXPECT_EQ("is NaN", Describe(m3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002647 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
shiqiane35fdd92008-12-10 05:08:54 +00002648}
2649
2650TEST(PointeeTest, RawPointer) {
2651 const Matcher<int*> m = Pointee(Ge(0));
2652
2653 int n = 1;
2654 EXPECT_TRUE(m.Matches(&n));
2655 n = -1;
2656 EXPECT_FALSE(m.Matches(&n));
2657 EXPECT_FALSE(m.Matches(NULL));
2658}
2659
2660TEST(PointeeTest, RawPointerToConst) {
2661 const Matcher<const double*> m = Pointee(Ge(0));
2662
2663 double x = 1;
2664 EXPECT_TRUE(m.Matches(&x));
2665 x = -1;
2666 EXPECT_FALSE(m.Matches(&x));
2667 EXPECT_FALSE(m.Matches(NULL));
2668}
2669
2670TEST(PointeeTest, ReferenceToConstRawPointer) {
2671 const Matcher<int* const &> m = Pointee(Ge(0));
2672
2673 int n = 1;
2674 EXPECT_TRUE(m.Matches(&n));
2675 n = -1;
2676 EXPECT_FALSE(m.Matches(&n));
2677 EXPECT_FALSE(m.Matches(NULL));
2678}
2679
2680TEST(PointeeTest, ReferenceToNonConstRawPointer) {
2681 const Matcher<double* &> m = Pointee(Ge(0));
2682
2683 double x = 1.0;
2684 double* p = &x;
2685 EXPECT_TRUE(m.Matches(p));
2686 x = -1;
2687 EXPECT_FALSE(m.Matches(p));
2688 p = NULL;
2689 EXPECT_FALSE(m.Matches(p));
2690}
2691
2692TEST(PointeeTest, NeverMatchesNull) {
2693 const Matcher<const char*> m = Pointee(_);
2694 EXPECT_FALSE(m.Matches(NULL));
2695}
2696
2697// Tests that we can write Pointee(value) instead of Pointee(Eq(value)).
2698TEST(PointeeTest, MatchesAgainstAValue) {
2699 const Matcher<int*> m = Pointee(5);
2700
2701 int n = 5;
2702 EXPECT_TRUE(m.Matches(&n));
2703 n = -1;
2704 EXPECT_FALSE(m.Matches(&n));
2705 EXPECT_FALSE(m.Matches(NULL));
2706}
2707
2708TEST(PointeeTest, CanDescribeSelf) {
2709 const Matcher<int*> m = Pointee(Gt(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002710 EXPECT_EQ("points to a value that is > 3", Describe(m));
2711 EXPECT_EQ("does not point to a value that is > 3",
shiqiane35fdd92008-12-10 05:08:54 +00002712 DescribeNegation(m));
2713}
2714
shiqiane35fdd92008-12-10 05:08:54 +00002715TEST(PointeeTest, CanExplainMatchResult) {
2716 const Matcher<const string*> m = Pointee(StartsWith("Hi"));
2717
2718 EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL)));
2719
2720 const Matcher<int*> m2 = Pointee(GreaterThan(1));
2721 int n = 3;
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002722 EXPECT_EQ("which points to 3, which is 2 more than 1",
2723 Explain(m2, &n));
2724}
2725
2726TEST(PointeeTest, AlwaysExplainsPointee) {
2727 const Matcher<int*> m = Pointee(0);
2728 int n = 42;
2729 EXPECT_EQ("which points to 42", Explain(m, &n));
shiqiane35fdd92008-12-10 05:08:54 +00002730}
2731
2732// An uncopyable class.
2733class Uncopyable {
2734 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002735 explicit Uncopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00002736
2737 int value() const { return value_; }
2738 private:
2739 const int value_;
2740 GTEST_DISALLOW_COPY_AND_ASSIGN_(Uncopyable);
2741};
2742
2743// Returns true iff x.value() is positive.
2744bool ValueIsPositive(const Uncopyable& x) { return x.value() > 0; }
2745
2746// A user-defined struct for testing Field().
2747struct AStruct {
2748 AStruct() : x(0), y(1.0), z(5), p(NULL) {}
2749 AStruct(const AStruct& rhs)
2750 : x(rhs.x), y(rhs.y), z(rhs.z.value()), p(rhs.p) {}
2751
2752 int x; // A non-const field.
2753 const double y; // A const field.
2754 Uncopyable z; // An uncopyable field.
2755 const char* p; // A pointer field.
zhanyong.wan32de5f52009-12-23 00:13:23 +00002756
2757 private:
2758 GTEST_DISALLOW_ASSIGN_(AStruct);
shiqiane35fdd92008-12-10 05:08:54 +00002759};
2760
2761// A derived struct for testing Field().
2762struct DerivedStruct : public AStruct {
2763 char ch;
zhanyong.wan32de5f52009-12-23 00:13:23 +00002764
2765 private:
2766 GTEST_DISALLOW_ASSIGN_(DerivedStruct);
shiqiane35fdd92008-12-10 05:08:54 +00002767};
2768
2769// Tests that Field(&Foo::field, ...) works when field is non-const.
2770TEST(FieldTest, WorksForNonConstField) {
2771 Matcher<AStruct> m = Field(&AStruct::x, Ge(0));
2772
2773 AStruct a;
2774 EXPECT_TRUE(m.Matches(a));
2775 a.x = -1;
2776 EXPECT_FALSE(m.Matches(a));
2777}
2778
2779// Tests that Field(&Foo::field, ...) works when field is const.
2780TEST(FieldTest, WorksForConstField) {
2781 AStruct a;
2782
2783 Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0));
2784 EXPECT_TRUE(m.Matches(a));
2785 m = Field(&AStruct::y, Le(0.0));
2786 EXPECT_FALSE(m.Matches(a));
2787}
2788
2789// Tests that Field(&Foo::field, ...) works when field is not copyable.
2790TEST(FieldTest, WorksForUncopyableField) {
2791 AStruct a;
2792
2793 Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive));
2794 EXPECT_TRUE(m.Matches(a));
2795 m = Field(&AStruct::z, Not(Truly(ValueIsPositive)));
2796 EXPECT_FALSE(m.Matches(a));
2797}
2798
2799// Tests that Field(&Foo::field, ...) works when field is a pointer.
2800TEST(FieldTest, WorksForPointerField) {
2801 // Matching against NULL.
2802 Matcher<AStruct> m = Field(&AStruct::p, static_cast<const char*>(NULL));
2803 AStruct a;
2804 EXPECT_TRUE(m.Matches(a));
2805 a.p = "hi";
2806 EXPECT_FALSE(m.Matches(a));
2807
2808 // Matching a pointer that is not NULL.
2809 m = Field(&AStruct::p, StartsWith("hi"));
2810 a.p = "hill";
2811 EXPECT_TRUE(m.Matches(a));
2812 a.p = "hole";
2813 EXPECT_FALSE(m.Matches(a));
2814}
2815
2816// Tests that Field() works when the object is passed by reference.
2817TEST(FieldTest, WorksForByRefArgument) {
2818 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2819
2820 AStruct a;
2821 EXPECT_TRUE(m.Matches(a));
2822 a.x = -1;
2823 EXPECT_FALSE(m.Matches(a));
2824}
2825
2826// Tests that Field(&Foo::field, ...) works when the argument's type
2827// is a sub-type of Foo.
2828TEST(FieldTest, WorksForArgumentOfSubType) {
2829 // Note that the matcher expects DerivedStruct but we say AStruct
2830 // inside Field().
2831 Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0));
2832
2833 DerivedStruct d;
2834 EXPECT_TRUE(m.Matches(d));
2835 d.x = -1;
2836 EXPECT_FALSE(m.Matches(d));
2837}
2838
2839// Tests that Field(&Foo::field, m) works when field's type and m's
2840// argument type are compatible but not the same.
2841TEST(FieldTest, WorksForCompatibleMatcherType) {
2842 // The field is an int, but the inner matcher expects a signed char.
2843 Matcher<const AStruct&> m = Field(&AStruct::x,
2844 Matcher<signed char>(Ge(0)));
2845
2846 AStruct a;
2847 EXPECT_TRUE(m.Matches(a));
2848 a.x = -1;
2849 EXPECT_FALSE(m.Matches(a));
2850}
2851
2852// Tests that Field() can describe itself.
2853TEST(FieldTest, CanDescribeSelf) {
2854 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2855
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002856 EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
2857 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00002858}
2859
2860// Tests that Field() can explain the match result.
2861TEST(FieldTest, CanExplainMatchResult) {
2862 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2863
2864 AStruct a;
2865 a.x = 1;
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002866 EXPECT_EQ("whose given field is 1", Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00002867
2868 m = Field(&AStruct::x, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002869 EXPECT_EQ("whose given field is 1, which is 1 more than 0", Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00002870}
2871
2872// Tests that Field() works when the argument is a pointer to const.
2873TEST(FieldForPointerTest, WorksForPointerToConst) {
2874 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2875
2876 AStruct a;
2877 EXPECT_TRUE(m.Matches(&a));
2878 a.x = -1;
2879 EXPECT_FALSE(m.Matches(&a));
2880}
2881
2882// Tests that Field() works when the argument is a pointer to non-const.
2883TEST(FieldForPointerTest, WorksForPointerToNonConst) {
2884 Matcher<AStruct*> m = Field(&AStruct::x, Ge(0));
2885
2886 AStruct a;
2887 EXPECT_TRUE(m.Matches(&a));
2888 a.x = -1;
2889 EXPECT_FALSE(m.Matches(&a));
2890}
2891
zhanyong.wan6953a722010-01-13 05:15:07 +00002892// Tests that Field() works when the argument is a reference to a const pointer.
2893TEST(FieldForPointerTest, WorksForReferenceToConstPointer) {
2894 Matcher<AStruct* const&> m = Field(&AStruct::x, Ge(0));
2895
2896 AStruct a;
2897 EXPECT_TRUE(m.Matches(&a));
2898 a.x = -1;
2899 EXPECT_FALSE(m.Matches(&a));
2900}
2901
shiqiane35fdd92008-12-10 05:08:54 +00002902// Tests that Field() does not match the NULL pointer.
2903TEST(FieldForPointerTest, DoesNotMatchNull) {
2904 Matcher<const AStruct*> m = Field(&AStruct::x, _);
2905 EXPECT_FALSE(m.Matches(NULL));
2906}
2907
2908// Tests that Field(&Foo::field, ...) works when the argument's type
2909// is a sub-type of const Foo*.
2910TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
2911 // Note that the matcher expects DerivedStruct but we say AStruct
2912 // inside Field().
2913 Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0));
2914
2915 DerivedStruct d;
2916 EXPECT_TRUE(m.Matches(&d));
2917 d.x = -1;
2918 EXPECT_FALSE(m.Matches(&d));
2919}
2920
2921// Tests that Field() can describe itself when used to match a pointer.
2922TEST(FieldForPointerTest, CanDescribeSelf) {
2923 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2924
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002925 EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
2926 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00002927}
2928
2929// Tests that Field() can explain the result of matching a pointer.
2930TEST(FieldForPointerTest, CanExplainMatchResult) {
2931 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2932
2933 AStruct a;
2934 a.x = 1;
2935 EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(NULL)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002936 EXPECT_EQ("which points to an object whose given field is 1", Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00002937
2938 m = Field(&AStruct::x, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002939 EXPECT_EQ("which points to an object whose given field is 1, "
2940 "which is 1 more than 0", Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00002941}
2942
2943// A user-defined class for testing Property().
2944class AClass {
2945 public:
2946 AClass() : n_(0) {}
2947
2948 // A getter that returns a non-reference.
2949 int n() const { return n_; }
2950
2951 void set_n(int new_n) { n_ = new_n; }
2952
2953 // A getter that returns a reference to const.
2954 const string& s() const { return s_; }
2955
2956 void set_s(const string& new_s) { s_ = new_s; }
2957
2958 // A getter that returns a reference to non-const.
2959 double& x() const { return x_; }
2960 private:
2961 int n_;
2962 string s_;
2963
2964 static double x_;
2965};
2966
2967double AClass::x_ = 0.0;
2968
2969// A derived class for testing Property().
2970class DerivedClass : public AClass {
2971 private:
2972 int k_;
2973};
2974
2975// Tests that Property(&Foo::property, ...) works when property()
2976// returns a non-reference.
2977TEST(PropertyTest, WorksForNonReferenceProperty) {
2978 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
2979
2980 AClass a;
2981 a.set_n(1);
2982 EXPECT_TRUE(m.Matches(a));
2983
2984 a.set_n(-1);
2985 EXPECT_FALSE(m.Matches(a));
2986}
2987
2988// Tests that Property(&Foo::property, ...) works when property()
2989// returns a reference to const.
2990TEST(PropertyTest, WorksForReferenceToConstProperty) {
2991 Matcher<const AClass&> m = Property(&AClass::s, StartsWith("hi"));
2992
2993 AClass a;
2994 a.set_s("hill");
2995 EXPECT_TRUE(m.Matches(a));
2996
2997 a.set_s("hole");
2998 EXPECT_FALSE(m.Matches(a));
2999}
3000
3001// Tests that Property(&Foo::property, ...) works when property()
3002// returns a reference to non-const.
3003TEST(PropertyTest, WorksForReferenceToNonConstProperty) {
3004 double x = 0.0;
3005 AClass a;
3006
3007 Matcher<const AClass&> m = Property(&AClass::x, Ref(x));
3008 EXPECT_FALSE(m.Matches(a));
3009
3010 m = Property(&AClass::x, Not(Ref(x)));
3011 EXPECT_TRUE(m.Matches(a));
3012}
3013
3014// Tests that Property(&Foo::property, ...) works when the argument is
3015// passed by value.
3016TEST(PropertyTest, WorksForByValueArgument) {
3017 Matcher<AClass> m = Property(&AClass::s, StartsWith("hi"));
3018
3019 AClass a;
3020 a.set_s("hill");
3021 EXPECT_TRUE(m.Matches(a));
3022
3023 a.set_s("hole");
3024 EXPECT_FALSE(m.Matches(a));
3025}
3026
3027// Tests that Property(&Foo::property, ...) works when the argument's
3028// type is a sub-type of Foo.
3029TEST(PropertyTest, WorksForArgumentOfSubType) {
3030 // The matcher expects a DerivedClass, but inside the Property() we
3031 // say AClass.
3032 Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0));
3033
3034 DerivedClass d;
3035 d.set_n(1);
3036 EXPECT_TRUE(m.Matches(d));
3037
3038 d.set_n(-1);
3039 EXPECT_FALSE(m.Matches(d));
3040}
3041
3042// Tests that Property(&Foo::property, m) works when property()'s type
3043// and m's argument type are compatible but different.
3044TEST(PropertyTest, WorksForCompatibleMatcherType) {
3045 // n() returns an int but the inner matcher expects a signed char.
3046 Matcher<const AClass&> m = Property(&AClass::n,
3047 Matcher<signed char>(Ge(0)));
3048
3049 AClass a;
3050 EXPECT_TRUE(m.Matches(a));
3051 a.set_n(-1);
3052 EXPECT_FALSE(m.Matches(a));
3053}
3054
3055// Tests that Property() can describe itself.
3056TEST(PropertyTest, CanDescribeSelf) {
3057 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3058
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003059 EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
3060 EXPECT_EQ("is an object whose given property isn't >= 0",
3061 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003062}
3063
3064// Tests that Property() can explain the match result.
3065TEST(PropertyTest, CanExplainMatchResult) {
3066 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3067
3068 AClass a;
3069 a.set_n(1);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003070 EXPECT_EQ("whose given property is 1", Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003071
3072 m = Property(&AClass::n, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003073 EXPECT_EQ("whose given property is 1, which is 1 more than 0", Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003074}
3075
3076// Tests that Property() works when the argument is a pointer to const.
3077TEST(PropertyForPointerTest, WorksForPointerToConst) {
3078 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3079
3080 AClass a;
3081 a.set_n(1);
3082 EXPECT_TRUE(m.Matches(&a));
3083
3084 a.set_n(-1);
3085 EXPECT_FALSE(m.Matches(&a));
3086}
3087
3088// Tests that Property() works when the argument is a pointer to non-const.
3089TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
3090 Matcher<AClass*> m = Property(&AClass::s, StartsWith("hi"));
3091
3092 AClass a;
3093 a.set_s("hill");
3094 EXPECT_TRUE(m.Matches(&a));
3095
3096 a.set_s("hole");
3097 EXPECT_FALSE(m.Matches(&a));
3098}
3099
zhanyong.wan6953a722010-01-13 05:15:07 +00003100// Tests that Property() works when the argument is a reference to a
3101// const pointer.
3102TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) {
3103 Matcher<AClass* const&> m = Property(&AClass::s, StartsWith("hi"));
3104
3105 AClass a;
3106 a.set_s("hill");
3107 EXPECT_TRUE(m.Matches(&a));
3108
3109 a.set_s("hole");
3110 EXPECT_FALSE(m.Matches(&a));
3111}
3112
shiqiane35fdd92008-12-10 05:08:54 +00003113// Tests that Property() does not match the NULL pointer.
3114TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
3115 Matcher<const AClass*> m = Property(&AClass::x, _);
3116 EXPECT_FALSE(m.Matches(NULL));
3117}
3118
3119// Tests that Property(&Foo::property, ...) works when the argument's
3120// type is a sub-type of const Foo*.
3121TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
3122 // The matcher expects a DerivedClass, but inside the Property() we
3123 // say AClass.
3124 Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0));
3125
3126 DerivedClass d;
3127 d.set_n(1);
3128 EXPECT_TRUE(m.Matches(&d));
3129
3130 d.set_n(-1);
3131 EXPECT_FALSE(m.Matches(&d));
3132}
3133
3134// Tests that Property() can describe itself when used to match a pointer.
3135TEST(PropertyForPointerTest, CanDescribeSelf) {
3136 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3137
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003138 EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
3139 EXPECT_EQ("is an object whose given property isn't >= 0",
3140 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003141}
3142
3143// Tests that Property() can explain the result of matching a pointer.
3144TEST(PropertyForPointerTest, CanExplainMatchResult) {
3145 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3146
3147 AClass a;
3148 a.set_n(1);
3149 EXPECT_EQ("", Explain(m, static_cast<const AClass*>(NULL)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003150 EXPECT_EQ("which points to an object whose given property is 1",
3151 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003152
3153 m = Property(&AClass::n, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003154 EXPECT_EQ("which points to an object whose given property is 1, "
3155 "which is 1 more than 0", Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003156}
3157
3158// Tests ResultOf.
3159
3160// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3161// function pointer.
3162string IntToStringFunction(int input) { return input == 1 ? "foo" : "bar"; }
3163
3164TEST(ResultOfTest, WorksForFunctionPointers) {
3165 Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(string("foo")));
3166
3167 EXPECT_TRUE(matcher.Matches(1));
3168 EXPECT_FALSE(matcher.Matches(2));
3169}
3170
3171// Tests that ResultOf() can describe itself.
3172TEST(ResultOfTest, CanDescribeItself) {
3173 Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo"));
3174
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003175 EXPECT_EQ("is mapped by the given callable to a value that "
3176 "is equal to \"foo\"", Describe(matcher));
3177 EXPECT_EQ("is mapped by the given callable to a value that "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003178 "isn't equal to \"foo\"", DescribeNegation(matcher));
shiqiane35fdd92008-12-10 05:08:54 +00003179}
3180
3181// Tests that ResultOf() can explain the match result.
3182int IntFunction(int input) { return input == 42 ? 80 : 90; }
3183
3184TEST(ResultOfTest, CanExplainMatchResult) {
3185 Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003186 EXPECT_EQ("which is mapped by the given callable to 90",
3187 Explain(matcher, 36));
shiqiane35fdd92008-12-10 05:08:54 +00003188
3189 matcher = ResultOf(&IntFunction, GreaterThan(85));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003190 EXPECT_EQ("which is mapped by the given callable to 90, "
3191 "which is 5 more than 85", Explain(matcher, 36));
shiqiane35fdd92008-12-10 05:08:54 +00003192}
3193
3194// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3195// returns a non-reference.
3196TEST(ResultOfTest, WorksForNonReferenceResults) {
3197 Matcher<int> matcher = ResultOf(&IntFunction, Eq(80));
3198
3199 EXPECT_TRUE(matcher.Matches(42));
3200 EXPECT_FALSE(matcher.Matches(36));
3201}
3202
3203// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3204// returns a reference to non-const.
3205double& DoubleFunction(double& input) { return input; }
3206
3207Uncopyable& RefUncopyableFunction(Uncopyable& obj) {
3208 return obj;
3209}
3210
3211TEST(ResultOfTest, WorksForReferenceToNonConstResults) {
3212 double x = 3.14;
3213 double x2 = x;
3214 Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x));
3215
3216 EXPECT_TRUE(matcher.Matches(x));
3217 EXPECT_FALSE(matcher.Matches(x2));
3218
3219 // Test that ResultOf works with uncopyable objects
3220 Uncopyable obj(0);
3221 Uncopyable obj2(0);
3222 Matcher<Uncopyable&> matcher2 =
3223 ResultOf(&RefUncopyableFunction, Ref(obj));
3224
3225 EXPECT_TRUE(matcher2.Matches(obj));
3226 EXPECT_FALSE(matcher2.Matches(obj2));
3227}
3228
3229// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3230// returns a reference to const.
3231const string& StringFunction(const string& input) { return input; }
3232
3233TEST(ResultOfTest, WorksForReferenceToConstResults) {
3234 string s = "foo";
3235 string s2 = s;
3236 Matcher<const string&> matcher = ResultOf(&StringFunction, Ref(s));
3237
3238 EXPECT_TRUE(matcher.Matches(s));
3239 EXPECT_FALSE(matcher.Matches(s2));
3240}
3241
3242// Tests that ResultOf(f, m) works when f(x) and m's
3243// argument types are compatible but different.
3244TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {
3245 // IntFunction() returns int but the inner matcher expects a signed char.
3246 Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85)));
3247
3248 EXPECT_TRUE(matcher.Matches(36));
3249 EXPECT_FALSE(matcher.Matches(42));
3250}
3251
shiqiane35fdd92008-12-10 05:08:54 +00003252// Tests that the program aborts when ResultOf is passed
3253// a NULL function pointer.
3254TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +00003255 EXPECT_DEATH_IF_SUPPORTED(
shiqiane35fdd92008-12-10 05:08:54 +00003256 ResultOf(static_cast<string(*)(int)>(NULL), Eq(string("foo"))),
3257 "NULL function pointer is passed into ResultOf\\(\\)\\.");
3258}
shiqiane35fdd92008-12-10 05:08:54 +00003259
3260// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3261// function reference.
3262TEST(ResultOfTest, WorksForFunctionReferences) {
3263 Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq("foo"));
3264 EXPECT_TRUE(matcher.Matches(1));
3265 EXPECT_FALSE(matcher.Matches(2));
3266}
3267
3268// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3269// function object.
3270struct Functor : public ::std::unary_function<int, string> {
3271 result_type operator()(argument_type input) const {
3272 return IntToStringFunction(input);
3273 }
3274};
3275
3276TEST(ResultOfTest, WorksForFunctors) {
3277 Matcher<int> matcher = ResultOf(Functor(), Eq(string("foo")));
3278
3279 EXPECT_TRUE(matcher.Matches(1));
3280 EXPECT_FALSE(matcher.Matches(2));
3281}
3282
3283// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3284// functor with more then one operator() defined. ResultOf() must work
3285// for each defined operator().
3286struct PolymorphicFunctor {
3287 typedef int result_type;
3288 int operator()(int n) { return n; }
3289 int operator()(const char* s) { return static_cast<int>(strlen(s)); }
3290};
3291
3292TEST(ResultOfTest, WorksForPolymorphicFunctors) {
3293 Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5));
3294
3295 EXPECT_TRUE(matcher_int.Matches(10));
3296 EXPECT_FALSE(matcher_int.Matches(2));
3297
3298 Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5));
3299
3300 EXPECT_TRUE(matcher_string.Matches("long string"));
3301 EXPECT_FALSE(matcher_string.Matches("shrt"));
3302}
3303
3304const int* ReferencingFunction(const int& n) { return &n; }
3305
3306struct ReferencingFunctor {
3307 typedef const int* result_type;
3308 result_type operator()(const int& n) { return &n; }
3309};
3310
3311TEST(ResultOfTest, WorksForReferencingCallables) {
3312 const int n = 1;
3313 const int n2 = 1;
3314 Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n));
3315 EXPECT_TRUE(matcher2.Matches(n));
3316 EXPECT_FALSE(matcher2.Matches(n2));
3317
3318 Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n));
3319 EXPECT_TRUE(matcher3.Matches(n));
3320 EXPECT_FALSE(matcher3.Matches(n2));
3321}
3322
shiqiane35fdd92008-12-10 05:08:54 +00003323class DivisibleByImpl {
3324 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003325 explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {}
shiqiane35fdd92008-12-10 05:08:54 +00003326
zhanyong.wandb22c222010-01-28 21:52:29 +00003327 // For testing using ExplainMatchResultTo() with polymorphic matchers.
shiqiane35fdd92008-12-10 05:08:54 +00003328 template <typename T>
zhanyong.wandb22c222010-01-28 21:52:29 +00003329 bool MatchAndExplain(const T& n, MatchResultListener* listener) const {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003330 *listener << "which is " << (n % divider_) << " modulo "
zhanyong.wandb22c222010-01-28 21:52:29 +00003331 << divider_;
shiqiane35fdd92008-12-10 05:08:54 +00003332 return (n % divider_) == 0;
3333 }
3334
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003335 void DescribeTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +00003336 *os << "is divisible by " << divider_;
3337 }
3338
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003339 void DescribeNegationTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +00003340 *os << "is not divisible by " << divider_;
3341 }
3342
zhanyong.wan32de5f52009-12-23 00:13:23 +00003343 void set_divider(int a_divider) { divider_ = a_divider; }
shiqiane35fdd92008-12-10 05:08:54 +00003344 int divider() const { return divider_; }
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003345
shiqiane35fdd92008-12-10 05:08:54 +00003346 private:
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003347 int divider_;
shiqiane35fdd92008-12-10 05:08:54 +00003348};
3349
shiqiane35fdd92008-12-10 05:08:54 +00003350PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) {
3351 return MakePolymorphicMatcher(DivisibleByImpl(n));
3352}
3353
3354// Tests that when AllOf() fails, only the first failing matcher is
3355// asked to explain why.
3356TEST(ExplainMatchResultTest, AllOf_False_False) {
3357 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003358 EXPECT_EQ("which is 1 modulo 4", Explain(m, 5));
shiqiane35fdd92008-12-10 05:08:54 +00003359}
3360
3361// Tests that when AllOf() fails, only the first failing matcher is
3362// asked to explain why.
3363TEST(ExplainMatchResultTest, AllOf_False_True) {
3364 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003365 EXPECT_EQ("which is 2 modulo 4", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003366}
3367
3368// Tests that when AllOf() fails, only the first failing matcher is
3369// asked to explain why.
3370TEST(ExplainMatchResultTest, AllOf_True_False) {
3371 const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003372 EXPECT_EQ("which is 2 modulo 3", Explain(m, 5));
shiqiane35fdd92008-12-10 05:08:54 +00003373}
3374
3375// Tests that when AllOf() succeeds, all matchers are asked to explain
3376// why.
3377TEST(ExplainMatchResultTest, AllOf_True_True) {
3378 const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003379 EXPECT_EQ("which is 0 modulo 2, and which is 0 modulo 3", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003380}
3381
3382TEST(ExplainMatchResultTest, AllOf_True_True_2) {
3383 const Matcher<int> m = AllOf(Ge(2), Le(3));
3384 EXPECT_EQ("", Explain(m, 2));
3385}
3386
3387TEST(ExplainmatcherResultTest, MonomorphicMatcher) {
3388 const Matcher<int> m = GreaterThan(5);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003389 EXPECT_EQ("which is 1 more than 5", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003390}
3391
3392// The following two tests verify that values without a public copy
3393// ctor can be used as arguments to matchers like Eq(), Ge(), and etc
3394// with the help of ByRef().
3395
3396class NotCopyable {
3397 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003398 explicit NotCopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00003399
3400 int value() const { return value_; }
3401
3402 bool operator==(const NotCopyable& rhs) const {
3403 return value() == rhs.value();
3404 }
3405
3406 bool operator>=(const NotCopyable& rhs) const {
3407 return value() >= rhs.value();
3408 }
3409 private:
3410 int value_;
3411
3412 GTEST_DISALLOW_COPY_AND_ASSIGN_(NotCopyable);
3413};
3414
3415TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) {
3416 const NotCopyable const_value1(1);
3417 const Matcher<const NotCopyable&> m = Eq(ByRef(const_value1));
3418
3419 const NotCopyable n1(1), n2(2);
3420 EXPECT_TRUE(m.Matches(n1));
3421 EXPECT_FALSE(m.Matches(n2));
3422}
3423
3424TEST(ByRefTest, AllowsNotCopyableValueInMatchers) {
3425 NotCopyable value2(2);
3426 const Matcher<NotCopyable&> m = Ge(ByRef(value2));
3427
3428 NotCopyable n1(1), n2(2);
3429 EXPECT_FALSE(m.Matches(n1));
3430 EXPECT_TRUE(m.Matches(n2));
3431}
3432
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003433#if GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003434// Tests ContainerEq with different container types, and
3435// different element types.
3436
3437template <typename T>
zhanyong.wanb8243162009-06-04 05:48:20 +00003438class ContainerEqTest : public testing::Test {};
zhanyong.wan6a896b52009-01-16 01:13:50 +00003439
3440typedef testing::Types<
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003441 set<int>,
3442 vector<size_t>,
3443 multiset<size_t>,
3444 list<int> >
zhanyong.wan6a896b52009-01-16 01:13:50 +00003445 ContainerEqTestTypes;
3446
3447TYPED_TEST_CASE(ContainerEqTest, ContainerEqTestTypes);
3448
3449// Tests that the filled container is equal to itself.
3450TYPED_TEST(ContainerEqTest, EqualsSelf) {
3451 static const int vals[] = {1, 1, 2, 3, 5, 8};
3452 TypeParam my_set(vals, vals + 6);
3453 const Matcher<TypeParam> m = ContainerEq(my_set);
3454 EXPECT_TRUE(m.Matches(my_set));
3455 EXPECT_EQ("", Explain(m, my_set));
3456}
3457
3458// Tests that missing values are reported.
3459TYPED_TEST(ContainerEqTest, ValueMissing) {
3460 static const int vals[] = {1, 1, 2, 3, 5, 8};
3461 static const int test_vals[] = {2, 1, 8, 5};
3462 TypeParam my_set(vals, vals + 6);
3463 TypeParam test_set(test_vals, test_vals + 4);
3464 const Matcher<TypeParam> m = ContainerEq(my_set);
3465 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003466 EXPECT_EQ("which doesn't have these expected elements: 3",
3467 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003468}
3469
3470// Tests that added values are reported.
3471TYPED_TEST(ContainerEqTest, ValueAdded) {
3472 static const int vals[] = {1, 1, 2, 3, 5, 8};
3473 static const int test_vals[] = {1, 2, 3, 5, 8, 46};
3474 TypeParam my_set(vals, vals + 6);
3475 TypeParam test_set(test_vals, test_vals + 6);
3476 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3477 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003478 EXPECT_EQ("which has these unexpected elements: 46", Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003479}
3480
3481// Tests that added and missing values are reported together.
3482TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) {
3483 static const int vals[] = {1, 1, 2, 3, 5, 8};
3484 static const int test_vals[] = {1, 2, 3, 8, 46};
3485 TypeParam my_set(vals, vals + 6);
3486 TypeParam test_set(test_vals, test_vals + 5);
3487 const Matcher<TypeParam> m = ContainerEq(my_set);
3488 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003489 EXPECT_EQ("which has these unexpected elements: 46,\n"
3490 "and doesn't have these expected elements: 5",
3491 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003492}
3493
3494// Tests duplicated value -- expect no explanation.
3495TYPED_TEST(ContainerEqTest, DuplicateDifference) {
3496 static const int vals[] = {1, 1, 2, 3, 5, 8};
3497 static const int test_vals[] = {1, 2, 3, 5, 8};
3498 TypeParam my_set(vals, vals + 6);
3499 TypeParam test_set(test_vals, test_vals + 5);
3500 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3501 // Depending on the container, match may be true or false
3502 // But in any case there should be no explanation.
3503 EXPECT_EQ("", Explain(m, test_set));
3504}
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003505#endif // GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003506
3507// Tests that mutliple missing values are reported.
3508// Using just vector here, so order is predicatble.
3509TEST(ContainerEqExtraTest, MultipleValuesMissing) {
3510 static const int vals[] = {1, 1, 2, 3, 5, 8};
3511 static const int test_vals[] = {2, 1, 5};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003512 vector<int> my_set(vals, vals + 6);
3513 vector<int> test_set(test_vals, test_vals + 3);
3514 const Matcher<vector<int> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003515 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003516 EXPECT_EQ("which doesn't have these expected elements: 3, 8",
3517 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003518}
3519
3520// Tests that added values are reported.
3521// Using just vector here, so order is predicatble.
3522TEST(ContainerEqExtraTest, MultipleValuesAdded) {
3523 static const int vals[] = {1, 1, 2, 3, 5, 8};
3524 static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003525 list<size_t> my_set(vals, vals + 6);
3526 list<size_t> test_set(test_vals, test_vals + 7);
3527 const Matcher<const list<size_t>&> m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003528 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003529 EXPECT_EQ("which has these unexpected elements: 92, 46",
3530 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003531}
3532
3533// Tests that added and missing values are reported together.
3534TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) {
3535 static const int vals[] = {1, 1, 2, 3, 5, 8};
3536 static const int test_vals[] = {1, 2, 3, 92, 46};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003537 list<size_t> my_set(vals, vals + 6);
3538 list<size_t> test_set(test_vals, test_vals + 5);
3539 const Matcher<const list<size_t> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003540 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003541 EXPECT_EQ("which has these unexpected elements: 92, 46,\n"
3542 "and doesn't have these expected elements: 5, 8",
zhanyong.wan6a896b52009-01-16 01:13:50 +00003543 Explain(m, test_set));
3544}
3545
3546// Tests to see that duplicate elements are detected,
3547// but (as above) not reported in the explanation.
3548TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) {
3549 static const int vals[] = {1, 1, 2, 3, 5, 8};
3550 static const int test_vals[] = {1, 2, 3, 5, 8};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003551 vector<int> my_set(vals, vals + 6);
3552 vector<int> test_set(test_vals, test_vals + 5);
3553 const Matcher<vector<int> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003554 EXPECT_TRUE(m.Matches(my_set));
3555 EXPECT_FALSE(m.Matches(test_set));
3556 // There is nothing to report when both sets contain all the same values.
3557 EXPECT_EQ("", Explain(m, test_set));
3558}
3559
3560// Tests that ContainerEq works for non-trivial associative containers,
3561// like maps.
3562TEST(ContainerEqExtraTest, WorksForMaps) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003563 map<int, std::string> my_map;
zhanyong.wan6a896b52009-01-16 01:13:50 +00003564 my_map[0] = "a";
3565 my_map[1] = "b";
3566
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003567 map<int, std::string> test_map;
zhanyong.wan6a896b52009-01-16 01:13:50 +00003568 test_map[0] = "aa";
3569 test_map[1] = "b";
3570
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003571 const Matcher<const map<int, std::string>&> m = ContainerEq(my_map);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003572 EXPECT_TRUE(m.Matches(my_map));
3573 EXPECT_FALSE(m.Matches(test_map));
3574
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003575 EXPECT_EQ("which has these unexpected elements: (0, \"aa\"),\n"
3576 "and doesn't have these expected elements: (0, \"a\")",
zhanyong.wan6a896b52009-01-16 01:13:50 +00003577 Explain(m, test_map));
3578}
3579
zhanyong.wanb8243162009-06-04 05:48:20 +00003580TEST(ContainerEqExtraTest, WorksForNativeArray) {
3581 int a1[] = { 1, 2, 3 };
3582 int a2[] = { 1, 2, 3 };
3583 int b[] = { 1, 2, 4 };
3584
3585 EXPECT_THAT(a1, ContainerEq(a2));
3586 EXPECT_THAT(a1, Not(ContainerEq(b)));
3587}
3588
3589TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) {
3590 const char a1[][3] = { "hi", "lo" };
3591 const char a2[][3] = { "hi", "lo" };
3592 const char b[][3] = { "lo", "hi" };
3593
3594 // Tests using ContainerEq() in the first dimension.
3595 EXPECT_THAT(a1, ContainerEq(a2));
3596 EXPECT_THAT(a1, Not(ContainerEq(b)));
3597
3598 // Tests using ContainerEq() in the second dimension.
3599 EXPECT_THAT(a1, ElementsAre(ContainerEq(a2[0]), ContainerEq(a2[1])));
3600 EXPECT_THAT(a1, ElementsAre(Not(ContainerEq(b[0])), ContainerEq(a2[1])));
3601}
3602
3603TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) {
3604 const int a1[] = { 1, 2, 3 };
3605 const int a2[] = { 1, 2, 3 };
3606 const int b[] = { 1, 2, 3, 4 };
3607
zhanyong.wan2661c682009-06-09 05:42:12 +00003608 const int* const p1 = a1;
3609 EXPECT_THAT(make_tuple(p1, 3), ContainerEq(a2));
3610 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(b)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003611
3612 const int c[] = { 1, 3, 2 };
zhanyong.wan2661c682009-06-09 05:42:12 +00003613 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(c)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003614}
3615
3616TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) {
3617 std::string a1[][3] = {
3618 { "hi", "hello", "ciao" },
3619 { "bye", "see you", "ciao" }
3620 };
3621
3622 std::string a2[][3] = {
3623 { "hi", "hello", "ciao" },
3624 { "bye", "see you", "ciao" }
3625 };
3626
3627 const Matcher<const std::string(&)[2][3]> m = ContainerEq(a2);
3628 EXPECT_THAT(a1, m);
3629
3630 a2[0][0] = "ha";
3631 EXPECT_THAT(a1, m);
3632}
3633
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003634// Tests JoinAsTuple().
3635
3636TEST(JoinAsTupleTest, JoinsEmptyTuple) {
3637 EXPECT_EQ("", JoinAsTuple(Strings()));
3638}
3639
3640TEST(JoinAsTupleTest, JoinsOneTuple) {
3641 const char* fields[] = { "1" };
3642 EXPECT_EQ("1", JoinAsTuple(Strings(fields, fields + 1)));
3643}
3644
3645TEST(JoinAsTupleTest, JoinsTwoTuple) {
3646 const char* fields[] = { "1", "a" };
3647 EXPECT_EQ("(1, a)", JoinAsTuple(Strings(fields, fields + 2)));
3648}
3649
3650TEST(JoinAsTupleTest, JoinsTenTuple) {
3651 const char* fields[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
3652 EXPECT_EQ("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)",
3653 JoinAsTuple(Strings(fields, fields + 10)));
3654}
3655
3656// Tests FormatMatcherDescription().
3657
3658TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) {
3659 EXPECT_EQ("is even",
zhanyong.wanb4140802010-06-08 22:53:57 +00003660 FormatMatcherDescription(false, "IsEven", Strings()));
3661 EXPECT_EQ("not (is even)",
3662 FormatMatcherDescription(true, "IsEven", Strings()));
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003663
3664 const char* params[] = { "5" };
3665 EXPECT_EQ("equals 5",
zhanyong.wanb4140802010-06-08 22:53:57 +00003666 FormatMatcherDescription(false, "Equals",
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003667 Strings(params, params + 1)));
3668
3669 const char* params2[] = { "5", "8" };
3670 EXPECT_EQ("is in range (5, 8)",
zhanyong.wanb4140802010-06-08 22:53:57 +00003671 FormatMatcherDescription(false, "IsInRange",
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003672 Strings(params2, params2 + 2)));
3673}
3674
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003675// Tests PolymorphicMatcher::mutable_impl().
3676TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {
3677 PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
3678 DivisibleByImpl& impl = m.mutable_impl();
3679 EXPECT_EQ(42, impl.divider());
3680
3681 impl.set_divider(0);
3682 EXPECT_EQ(0, m.mutable_impl().divider());
3683}
3684
3685// Tests PolymorphicMatcher::impl().
3686TEST(PolymorphicMatcherTest, CanAccessImpl) {
3687 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
3688 const DivisibleByImpl& impl = m.impl();
3689 EXPECT_EQ(42, impl.divider());
3690}
3691
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003692TEST(MatcherTupleTest, ExplainsMatchFailure) {
3693 stringstream ss1;
3694 ExplainMatchFailureTupleTo(make_tuple(Matcher<char>(Eq('a')), GreaterThan(5)),
3695 make_tuple('a', 10), &ss1);
3696 EXPECT_EQ("", ss1.str()); // Successful match.
3697
3698 stringstream ss2;
3699 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
3700 make_tuple(2, 'b'), &ss2);
3701 EXPECT_EQ(" Expected arg #0: is > 5\n"
3702 " Actual: 2, which is 3 less than 5\n"
3703 " Expected arg #1: is equal to 'a' (97)\n"
3704 " Actual: 'b' (98)\n",
3705 ss2.str()); // Failed match where both arguments need explanation.
3706
3707 stringstream ss3;
3708 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
3709 make_tuple(2, 'a'), &ss3);
3710 EXPECT_EQ(" Expected arg #0: is > 5\n"
3711 " Actual: 2, which is 3 less than 5\n",
3712 ss3.str()); // Failed match where only one argument needs
3713 // explanation.
3714}
3715
zhanyong.wan33605ba2010-04-22 23:37:47 +00003716// Tests Each().
3717
3718TEST(EachTest, ExplainsMatchResultCorrectly) {
3719 set<int> a; // empty
3720
3721 Matcher<set<int> > m = Each(2);
3722 EXPECT_EQ("", Explain(m, a));
3723
3724 Matcher<const int(&)[1]> n = Each(1);
3725
3726 const int b[1] = { 1 };
3727 EXPECT_EQ("", Explain(n, b));
3728
3729 n = Each(3);
3730 EXPECT_EQ("whose element #0 doesn't match", Explain(n, b));
3731
3732 a.insert(1);
3733 a.insert(2);
3734 a.insert(3);
3735 m = Each(GreaterThan(0));
3736 EXPECT_EQ("", Explain(m, a));
3737
3738 m = Each(GreaterThan(10));
3739 EXPECT_EQ("whose element #0 doesn't match, which is 9 less than 10",
3740 Explain(m, a));
3741}
3742
3743TEST(EachTest, DescribesItselfCorrectly) {
3744 Matcher<vector<int> > m = Each(1);
3745 EXPECT_EQ("only contains elements that is equal to 1", Describe(m));
3746
3747 Matcher<vector<int> > m2 = Not(m);
3748 EXPECT_EQ("contains some element that isn't equal to 1", Describe(m2));
3749}
3750
3751TEST(EachTest, MatchesVectorWhenAllElementsMatch) {
3752 vector<int> some_vector;
3753 EXPECT_THAT(some_vector, Each(1));
3754 some_vector.push_back(3);
3755 EXPECT_THAT(some_vector, Not(Each(1)));
3756 EXPECT_THAT(some_vector, Each(3));
3757 some_vector.push_back(1);
3758 some_vector.push_back(2);
3759 EXPECT_THAT(some_vector, Not(Each(3)));
3760 EXPECT_THAT(some_vector, Each(Lt(3.5)));
3761
3762 vector<string> another_vector;
3763 another_vector.push_back("fee");
3764 EXPECT_THAT(another_vector, Each(string("fee")));
3765 another_vector.push_back("fie");
3766 another_vector.push_back("foe");
3767 another_vector.push_back("fum");
3768 EXPECT_THAT(another_vector, Not(Each(string("fee"))));
3769}
3770
3771TEST(EachTest, MatchesMapWhenAllElementsMatch) {
3772 map<const char*, int> my_map;
3773 const char* bar = "a string";
3774 my_map[bar] = 2;
3775 EXPECT_THAT(my_map, Each(make_pair(bar, 2)));
3776
3777 map<string, int> another_map;
3778 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1)));
3779 another_map["fee"] = 1;
3780 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1)));
3781 another_map["fie"] = 2;
3782 another_map["foe"] = 3;
3783 another_map["fum"] = 4;
3784 EXPECT_THAT(another_map, Not(Each(make_pair(string("fee"), 1))));
3785 EXPECT_THAT(another_map, Not(Each(make_pair(string("fum"), 1))));
3786 EXPECT_THAT(another_map, Each(Pair(_, Gt(0))));
3787}
3788
3789TEST(EachTest, AcceptsMatcher) {
3790 const int a[] = { 1, 2, 3 };
3791 EXPECT_THAT(a, Each(Gt(0)));
3792 EXPECT_THAT(a, Not(Each(Gt(1))));
3793}
3794
3795TEST(EachTest, WorksForNativeArrayAsTuple) {
3796 const int a[] = { 1, 2 };
3797 const int* const pointer = a;
3798 EXPECT_THAT(make_tuple(pointer, 2), Each(Gt(0)));
3799 EXPECT_THAT(make_tuple(pointer, 2), Not(Each(Gt(1))));
3800}
3801
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003802// For testing Pointwise().
3803class IsHalfOfMatcher {
3804 public:
3805 template <typename T1, typename T2>
3806 bool MatchAndExplain(const tuple<T1, T2>& a_pair,
3807 MatchResultListener* listener) const {
3808 if (get<0>(a_pair) == get<1>(a_pair)/2) {
3809 *listener << "where the second is " << get<1>(a_pair);
3810 return true;
3811 } else {
3812 *listener << "where the second/2 is " << get<1>(a_pair)/2;
3813 return false;
3814 }
3815 }
3816
3817 void DescribeTo(ostream* os) const {
3818 *os << "are a pair where the first is half of the second";
3819 }
3820
3821 void DescribeNegationTo(ostream* os) const {
3822 *os << "are a pair where the first isn't half of the second";
3823 }
3824};
3825
3826PolymorphicMatcher<IsHalfOfMatcher> IsHalfOf() {
3827 return MakePolymorphicMatcher(IsHalfOfMatcher());
3828}
3829
3830TEST(PointwiseTest, DescribesSelf) {
3831 vector<int> rhs;
3832 rhs.push_back(1);
3833 rhs.push_back(2);
3834 rhs.push_back(3);
3835 const Matcher<const vector<int>&> m = Pointwise(IsHalfOf(), rhs);
3836 EXPECT_EQ("contains 3 values, where each value and its corresponding value "
3837 "in { 1, 2, 3 } are a pair where the first is half of the second",
3838 Describe(m));
3839 EXPECT_EQ("doesn't contain exactly 3 values, or contains a value x at some "
3840 "index i where x and the i-th value of { 1, 2, 3 } are a pair "
3841 "where the first isn't half of the second",
3842 DescribeNegation(m));
3843}
3844
3845TEST(PointwiseTest, MakesCopyOfRhs) {
3846 list<signed char> rhs;
3847 rhs.push_back(2);
3848 rhs.push_back(4);
3849
3850 int lhs[] = { 1, 2 };
3851 const Matcher<const int (&)[2]> m = Pointwise(IsHalfOf(), rhs);
3852 EXPECT_THAT(lhs, m);
3853
3854 // Changing rhs now shouldn't affect m, which made a copy of rhs.
3855 rhs.push_back(6);
3856 EXPECT_THAT(lhs, m);
3857}
3858
3859TEST(PointwiseTest, WorksForLhsNativeArray) {
3860 const int lhs[] = { 1, 2, 3 };
3861 vector<int> rhs;
3862 rhs.push_back(2);
3863 rhs.push_back(4);
3864 rhs.push_back(6);
3865 EXPECT_THAT(lhs, Pointwise(Lt(), rhs));
3866 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
3867}
3868
3869TEST(PointwiseTest, WorksForRhsNativeArray) {
3870 const int rhs[] = { 1, 2, 3 };
3871 vector<int> lhs;
3872 lhs.push_back(2);
3873 lhs.push_back(4);
3874 lhs.push_back(6);
3875 EXPECT_THAT(lhs, Pointwise(Gt(), rhs));
3876 EXPECT_THAT(lhs, Not(Pointwise(Lt(), rhs)));
3877}
3878
3879TEST(PointwiseTest, RejectsWrongSize) {
3880 const double lhs[2] = { 1, 2 };
3881 const int rhs[1] = { 0 };
3882 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
3883 EXPECT_EQ("which contains 2 values",
3884 Explain(Pointwise(Gt(), rhs), lhs));
3885
3886 const int rhs2[3] = { 0, 1, 2 };
3887 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs2)));
3888}
3889
3890TEST(PointwiseTest, RejectsWrongContent) {
3891 const double lhs[3] = { 1, 2, 3 };
3892 const int rhs[3] = { 2, 6, 4 };
3893 EXPECT_THAT(lhs, Not(Pointwise(IsHalfOf(), rhs)));
3894 EXPECT_EQ("where the value pair (2, 6) at index #1 don't match, "
3895 "where the second/2 is 3",
3896 Explain(Pointwise(IsHalfOf(), rhs), lhs));
3897}
3898
3899TEST(PointwiseTest, AcceptsCorrectContent) {
3900 const double lhs[3] = { 1, 2, 3 };
3901 const int rhs[3] = { 2, 4, 6 };
3902 EXPECT_THAT(lhs, Pointwise(IsHalfOf(), rhs));
3903 EXPECT_EQ("", Explain(Pointwise(IsHalfOf(), rhs), lhs));
3904}
3905
3906TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) {
3907 const double lhs[3] = { 1, 2, 3 };
3908 const int rhs[3] = { 2, 4, 6 };
3909 const Matcher<tuple<const double&, const int&> > m1 = IsHalfOf();
3910 EXPECT_THAT(lhs, Pointwise(m1, rhs));
3911 EXPECT_EQ("", Explain(Pointwise(m1, rhs), lhs));
3912
3913 // This type works as a tuple<const double&, const int&> can be
3914 // implicitly cast to tuple<double, int>.
3915 const Matcher<tuple<double, int> > m2 = IsHalfOf();
3916 EXPECT_THAT(lhs, Pointwise(m2, rhs));
3917 EXPECT_EQ("", Explain(Pointwise(m2, rhs), lhs));
3918}
3919
shiqiane35fdd92008-12-10 05:08:54 +00003920} // namespace gmock_matchers_test
3921} // namespace testing