blob: c2db86ca88569b7f5fb47a61ed48a0ad6fdcf03d [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
zhanyong.wan02c15052010-06-09 19:21:30 +00001860// Helper to allow easy testing of AllOf matchers with num parameters.
1861void AllOfMatches(int num, const Matcher<int>& m) {
1862 SCOPED_TRACE(Describe(m));
1863 EXPECT_TRUE(m.Matches(0));
1864 for (int i = 1; i <= num; ++i) {
1865 EXPECT_FALSE(m.Matches(i));
1866 }
1867 EXPECT_TRUE(m.Matches(num + 1));
1868}
1869
shiqiane35fdd92008-12-10 05:08:54 +00001870// Tests that AllOf(m1, ..., mn) matches any value that matches all of
1871// the given matchers.
1872TEST(AllOfTest, MatchesWhenAllMatch) {
1873 Matcher<int> m;
1874 m = AllOf(Le(2), Ge(1));
1875 EXPECT_TRUE(m.Matches(1));
1876 EXPECT_TRUE(m.Matches(2));
1877 EXPECT_FALSE(m.Matches(0));
1878 EXPECT_FALSE(m.Matches(3));
1879
1880 m = AllOf(Gt(0), Ne(1), Ne(2));
1881 EXPECT_TRUE(m.Matches(3));
1882 EXPECT_FALSE(m.Matches(2));
1883 EXPECT_FALSE(m.Matches(1));
1884 EXPECT_FALSE(m.Matches(0));
1885
1886 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
1887 EXPECT_TRUE(m.Matches(4));
1888 EXPECT_FALSE(m.Matches(3));
1889 EXPECT_FALSE(m.Matches(2));
1890 EXPECT_FALSE(m.Matches(1));
1891 EXPECT_FALSE(m.Matches(0));
1892
1893 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
1894 EXPECT_TRUE(m.Matches(0));
1895 EXPECT_TRUE(m.Matches(1));
1896 EXPECT_FALSE(m.Matches(3));
zhanyong.wan02c15052010-06-09 19:21:30 +00001897
1898 // The following tests for varying number of sub-matchers. Due to the way
1899 // the sub-matchers are handled it is enough to test every sub-matcher once
1900 // with sub-matchers using the same matcher type. Varying matcher types are
1901 // checked for above.
1902 AllOfMatches(2, AllOf(Ne(1), Ne(2)));
1903 AllOfMatches(3, AllOf(Ne(1), Ne(2), Ne(3)));
1904 AllOfMatches(4, AllOf(Ne(1), Ne(2), Ne(3), Ne(4)));
1905 AllOfMatches(5, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5)));
1906 AllOfMatches(6, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6)));
1907 AllOfMatches(7, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7)));
1908 AllOfMatches(8, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
1909 Ne(8)));
1910 AllOfMatches(9, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
1911 Ne(8), Ne(9)));
1912 AllOfMatches(10, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8),
1913 Ne(9), Ne(10)));
shiqiane35fdd92008-12-10 05:08:54 +00001914}
1915
1916// Tests that AllOf(m1, ..., mn) describes itself properly.
1917TEST(AllOfTest, CanDescribeSelf) {
1918 Matcher<int> m;
1919 m = AllOf(Le(2), Ge(1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001920 EXPECT_EQ("(is <= 2) and (is >= 1)", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001921
1922 m = AllOf(Gt(0), Ne(1), Ne(2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001923 EXPECT_EQ("(is > 0) and "
1924 "((isn't equal to 1) and "
1925 "(isn't equal to 2))",
shiqiane35fdd92008-12-10 05:08:54 +00001926 Describe(m));
1927
1928
1929 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001930 EXPECT_EQ("(is > 0) and "
1931 "((isn't equal to 1) and "
1932 "((isn't equal to 2) and "
1933 "(isn't equal to 3)))",
shiqiane35fdd92008-12-10 05:08:54 +00001934 Describe(m));
1935
1936
1937 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001938 EXPECT_EQ("(is >= 0) and "
1939 "((is < 10) and "
1940 "((isn't equal to 3) and "
1941 "((isn't equal to 5) and "
1942 "(isn't equal to 7))))",
1943 Describe(m));
1944}
1945
1946// Tests that AllOf(m1, ..., mn) describes its negation properly.
1947TEST(AllOfTest, CanDescribeNegation) {
1948 Matcher<int> m;
1949 m = AllOf(Le(2), Ge(1));
1950 EXPECT_EQ("(isn't <= 2) or "
1951 "(isn't >= 1)",
1952 DescribeNegation(m));
1953
1954 m = AllOf(Gt(0), Ne(1), Ne(2));
1955 EXPECT_EQ("(isn't > 0) or "
1956 "((is equal to 1) or "
1957 "(is equal to 2))",
1958 DescribeNegation(m));
1959
1960
1961 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
1962 EXPECT_EQ("(isn't > 0) or "
1963 "((is equal to 1) or "
1964 "((is equal to 2) or "
1965 "(is equal to 3)))",
1966 DescribeNegation(m));
1967
1968
1969 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
1970 EXPECT_EQ("(isn't >= 0) or "
1971 "((isn't < 10) or "
1972 "((is equal to 3) or "
1973 "((is equal to 5) or "
1974 "(is equal to 7))))",
1975 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00001976}
1977
zhanyong.wan18490652009-05-11 18:54:08 +00001978// Tests that monomorphic matchers are safely cast by the AllOf matcher.
1979TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
1980 // greater_than_5 and less_than_10 are monomorphic matchers.
1981 Matcher<int> greater_than_5 = Gt(5);
1982 Matcher<int> less_than_10 = Lt(10);
1983
1984 Matcher<const int&> m = AllOf(greater_than_5, less_than_10);
1985 Matcher<int&> m2 = AllOf(greater_than_5, less_than_10);
1986 Matcher<int&> m3 = AllOf(greater_than_5, m2);
1987
1988 // Tests that BothOf works when composing itself.
1989 Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10);
1990 Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);
1991}
1992
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001993TEST(AllOfTest, ExplainsResult) {
1994 Matcher<int> m;
1995
1996 // Successful match. Both matchers need to explain. The second
1997 // matcher doesn't give an explanation, so only the first matcher's
1998 // explanation is printed.
1999 m = AllOf(GreaterThan(10), Lt(30));
2000 EXPECT_EQ("which is 15 more than 10", Explain(m, 25));
2001
2002 // Successful match. Both matchers need to explain.
2003 m = AllOf(GreaterThan(10), GreaterThan(20));
2004 EXPECT_EQ("which is 20 more than 10, and which is 10 more than 20",
2005 Explain(m, 30));
2006
2007 // Successful match. All matchers need to explain. The second
2008 // matcher doesn't given an explanation.
2009 m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20));
2010 EXPECT_EQ("which is 15 more than 10, and which is 5 more than 20",
2011 Explain(m, 25));
2012
2013 // Successful match. All matchers need to explain.
2014 m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2015 EXPECT_EQ("which is 30 more than 10, and which is 20 more than 20, "
2016 "and which is 10 more than 30",
2017 Explain(m, 40));
2018
2019 // Failed match. The first matcher, which failed, needs to
2020 // explain.
2021 m = AllOf(GreaterThan(10), GreaterThan(20));
2022 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
2023
2024 // Failed match. The second matcher, which failed, needs to
2025 // explain. Since it doesn't given an explanation, nothing is
2026 // printed.
2027 m = AllOf(GreaterThan(10), Lt(30));
2028 EXPECT_EQ("", Explain(m, 40));
2029
2030 // Failed match. The second matcher, which failed, needs to
2031 // explain.
2032 m = AllOf(GreaterThan(10), GreaterThan(20));
2033 EXPECT_EQ("which is 5 less than 20", Explain(m, 15));
2034}
2035
zhanyong.wan02c15052010-06-09 19:21:30 +00002036// Helper to allow easy testing of AnyOf matchers with num parameters.
2037void AnyOfMatches(int num, const Matcher<int>& m) {
2038 SCOPED_TRACE(Describe(m));
2039 EXPECT_FALSE(m.Matches(0));
2040 for (int i = 1; i <= num; ++i) {
2041 EXPECT_TRUE(m.Matches(i));
2042 }
2043 EXPECT_FALSE(m.Matches(num + 1));
2044}
2045
shiqiane35fdd92008-12-10 05:08:54 +00002046// Tests that AnyOf(m1, ..., mn) matches any value that matches at
2047// least one of the given matchers.
2048TEST(AnyOfTest, MatchesWhenAnyMatches) {
2049 Matcher<int> m;
2050 m = AnyOf(Le(1), Ge(3));
2051 EXPECT_TRUE(m.Matches(1));
2052 EXPECT_TRUE(m.Matches(4));
2053 EXPECT_FALSE(m.Matches(2));
2054
2055 m = AnyOf(Lt(0), Eq(1), Eq(2));
2056 EXPECT_TRUE(m.Matches(-1));
2057 EXPECT_TRUE(m.Matches(1));
2058 EXPECT_TRUE(m.Matches(2));
2059 EXPECT_FALSE(m.Matches(0));
2060
2061 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2062 EXPECT_TRUE(m.Matches(-1));
2063 EXPECT_TRUE(m.Matches(1));
2064 EXPECT_TRUE(m.Matches(2));
2065 EXPECT_TRUE(m.Matches(3));
2066 EXPECT_FALSE(m.Matches(0));
2067
2068 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2069 EXPECT_TRUE(m.Matches(0));
2070 EXPECT_TRUE(m.Matches(11));
2071 EXPECT_TRUE(m.Matches(3));
2072 EXPECT_FALSE(m.Matches(2));
zhanyong.wan02c15052010-06-09 19:21:30 +00002073
2074 // The following tests for varying number of sub-matchers. Due to the way
2075 // the sub-matchers are handled it is enough to test every sub-matcher once
2076 // with sub-matchers using the same matcher type. Varying matcher types are
2077 // checked for above.
2078 AnyOfMatches(2, AnyOf(1, 2));
2079 AnyOfMatches(3, AnyOf(1, 2, 3));
2080 AnyOfMatches(4, AnyOf(1, 2, 3, 4));
2081 AnyOfMatches(5, AnyOf(1, 2, 3, 4, 5));
2082 AnyOfMatches(6, AnyOf(1, 2, 3, 4, 5, 6));
2083 AnyOfMatches(7, AnyOf(1, 2, 3, 4, 5, 6, 7));
2084 AnyOfMatches(8, AnyOf(1, 2, 3, 4, 5, 6, 7, 8));
2085 AnyOfMatches(9, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9));
2086 AnyOfMatches(10, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
shiqiane35fdd92008-12-10 05:08:54 +00002087}
2088
2089// Tests that AnyOf(m1, ..., mn) describes itself properly.
2090TEST(AnyOfTest, CanDescribeSelf) {
2091 Matcher<int> m;
2092 m = AnyOf(Le(1), Ge(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002093 EXPECT_EQ("(is <= 1) or (is >= 3)",
shiqiane35fdd92008-12-10 05:08:54 +00002094 Describe(m));
2095
2096 m = AnyOf(Lt(0), Eq(1), Eq(2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002097 EXPECT_EQ("(is < 0) or "
shiqiane35fdd92008-12-10 05:08:54 +00002098 "((is equal to 1) or (is equal to 2))",
2099 Describe(m));
2100
2101 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002102 EXPECT_EQ("(is < 0) or "
shiqiane35fdd92008-12-10 05:08:54 +00002103 "((is equal to 1) or "
2104 "((is equal to 2) or "
2105 "(is equal to 3)))",
2106 Describe(m));
2107
2108 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002109 EXPECT_EQ("(is <= 0) or "
2110 "((is > 10) or "
shiqiane35fdd92008-12-10 05:08:54 +00002111 "((is equal to 3) or "
2112 "((is equal to 5) or "
2113 "(is equal to 7))))",
2114 Describe(m));
2115}
2116
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002117// Tests that AnyOf(m1, ..., mn) describes its negation properly.
2118TEST(AnyOfTest, CanDescribeNegation) {
2119 Matcher<int> m;
2120 m = AnyOf(Le(1), Ge(3));
2121 EXPECT_EQ("(isn't <= 1) and (isn't >= 3)",
2122 DescribeNegation(m));
2123
2124 m = AnyOf(Lt(0), Eq(1), Eq(2));
2125 EXPECT_EQ("(isn't < 0) and "
2126 "((isn't equal to 1) and (isn't equal to 2))",
2127 DescribeNegation(m));
2128
2129 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2130 EXPECT_EQ("(isn't < 0) and "
2131 "((isn't equal to 1) and "
2132 "((isn't equal to 2) and "
2133 "(isn't equal to 3)))",
2134 DescribeNegation(m));
2135
2136 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2137 EXPECT_EQ("(isn't <= 0) and "
2138 "((isn't > 10) and "
2139 "((isn't equal to 3) and "
2140 "((isn't equal to 5) and "
2141 "(isn't equal to 7))))",
2142 DescribeNegation(m));
2143}
2144
zhanyong.wan18490652009-05-11 18:54:08 +00002145// Tests that monomorphic matchers are safely cast by the AnyOf matcher.
2146TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
2147 // greater_than_5 and less_than_10 are monomorphic matchers.
2148 Matcher<int> greater_than_5 = Gt(5);
2149 Matcher<int> less_than_10 = Lt(10);
2150
2151 Matcher<const int&> m = AnyOf(greater_than_5, less_than_10);
2152 Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10);
2153 Matcher<int&> m3 = AnyOf(greater_than_5, m2);
2154
2155 // Tests that EitherOf works when composing itself.
2156 Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10);
2157 Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);
2158}
2159
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002160TEST(AnyOfTest, ExplainsResult) {
2161 Matcher<int> m;
2162
2163 // Failed match. Both matchers need to explain. The second
2164 // matcher doesn't give an explanation, so only the first matcher's
2165 // explanation is printed.
2166 m = AnyOf(GreaterThan(10), Lt(0));
2167 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
2168
2169 // Failed match. Both matchers need to explain.
2170 m = AnyOf(GreaterThan(10), GreaterThan(20));
2171 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20",
2172 Explain(m, 5));
2173
2174 // Failed match. All matchers need to explain. The second
2175 // matcher doesn't given an explanation.
2176 m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30));
2177 EXPECT_EQ("which is 5 less than 10, and which is 25 less than 30",
2178 Explain(m, 5));
2179
2180 // Failed match. All matchers need to explain.
2181 m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2182 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20, "
2183 "and which is 25 less than 30",
2184 Explain(m, 5));
2185
2186 // Successful match. The first matcher, which succeeded, needs to
2187 // explain.
2188 m = AnyOf(GreaterThan(10), GreaterThan(20));
2189 EXPECT_EQ("which is 5 more than 10", Explain(m, 15));
2190
2191 // Successful match. The second matcher, which succeeded, needs to
2192 // explain. Since it doesn't given an explanation, nothing is
2193 // printed.
2194 m = AnyOf(GreaterThan(10), Lt(30));
2195 EXPECT_EQ("", Explain(m, 0));
2196
2197 // Successful match. The second matcher, which succeeded, needs to
2198 // explain.
2199 m = AnyOf(GreaterThan(30), GreaterThan(20));
2200 EXPECT_EQ("which is 5 more than 20", Explain(m, 25));
2201}
2202
shiqiane35fdd92008-12-10 05:08:54 +00002203// The following predicate function and predicate functor are for
2204// testing the Truly(predicate) matcher.
2205
2206// Returns non-zero if the input is positive. Note that the return
2207// type of this function is not bool. It's OK as Truly() accepts any
2208// unary function or functor whose return type can be implicitly
2209// converted to bool.
2210int IsPositive(double x) {
2211 return x > 0 ? 1 : 0;
2212}
2213
2214// This functor returns true if the input is greater than the given
2215// number.
2216class IsGreaterThan {
2217 public:
2218 explicit IsGreaterThan(int threshold) : threshold_(threshold) {}
2219
2220 bool operator()(int n) const { return n > threshold_; }
zhanyong.wan32de5f52009-12-23 00:13:23 +00002221
shiqiane35fdd92008-12-10 05:08:54 +00002222 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002223 int threshold_;
shiqiane35fdd92008-12-10 05:08:54 +00002224};
2225
2226// For testing Truly().
2227const int foo = 0;
2228
2229// This predicate returns true iff the argument references foo and has
2230// a zero value.
2231bool ReferencesFooAndIsZero(const int& n) {
2232 return (&n == &foo) && (n == 0);
2233}
2234
2235// Tests that Truly(predicate) matches what satisfies the given
2236// predicate.
2237TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {
2238 Matcher<double> m = Truly(IsPositive);
2239 EXPECT_TRUE(m.Matches(2.0));
2240 EXPECT_FALSE(m.Matches(-1.5));
2241}
2242
2243// Tests that Truly(predicate_functor) works too.
2244TEST(TrulyTest, CanBeUsedWithFunctor) {
2245 Matcher<int> m = Truly(IsGreaterThan(5));
2246 EXPECT_TRUE(m.Matches(6));
2247 EXPECT_FALSE(m.Matches(4));
2248}
2249
2250// Tests that Truly(predicate) can describe itself properly.
2251TEST(TrulyTest, CanDescribeSelf) {
2252 Matcher<double> m = Truly(IsPositive);
2253 EXPECT_EQ("satisfies the given predicate",
2254 Describe(m));
2255}
2256
2257// Tests that Truly(predicate) works when the matcher takes its
2258// argument by reference.
2259TEST(TrulyTest, WorksForByRefArguments) {
2260 Matcher<const int&> m = Truly(ReferencesFooAndIsZero);
2261 EXPECT_TRUE(m.Matches(foo));
2262 int n = 0;
2263 EXPECT_FALSE(m.Matches(n));
2264}
2265
2266// Tests that Matches(m) is a predicate satisfied by whatever that
2267// matches matcher m.
2268TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {
2269 EXPECT_TRUE(Matches(Ge(0))(1));
2270 EXPECT_FALSE(Matches(Eq('a'))('b'));
2271}
2272
2273// Tests that Matches(m) works when the matcher takes its argument by
2274// reference.
2275TEST(MatchesTest, WorksOnByRefArguments) {
2276 int m = 0, n = 0;
2277 EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n));
2278 EXPECT_FALSE(Matches(Ref(m))(n));
2279}
2280
2281// Tests that a Matcher on non-reference type can be used in
2282// Matches().
2283TEST(MatchesTest, WorksWithMatcherOnNonRefType) {
2284 Matcher<int> eq5 = Eq(5);
2285 EXPECT_TRUE(Matches(eq5)(5));
2286 EXPECT_FALSE(Matches(eq5)(2));
2287}
2288
zhanyong.wanb8243162009-06-04 05:48:20 +00002289// Tests Value(value, matcher). Since Value() is a simple wrapper for
2290// Matches(), which has been tested already, we don't spend a lot of
2291// effort on testing Value().
2292TEST(ValueTest, WorksWithPolymorphicMatcher) {
2293 EXPECT_TRUE(Value("hi", StartsWith("h")));
2294 EXPECT_FALSE(Value(5, Gt(10)));
2295}
2296
2297TEST(ValueTest, WorksWithMonomorphicMatcher) {
2298 const Matcher<int> is_zero = Eq(0);
2299 EXPECT_TRUE(Value(0, is_zero));
2300 EXPECT_FALSE(Value('a', is_zero));
2301
2302 int n = 0;
2303 const Matcher<const int&> ref_n = Ref(n);
2304 EXPECT_TRUE(Value(n, ref_n));
2305 EXPECT_FALSE(Value(1, ref_n));
2306}
2307
zhanyong.wana862f1d2010-03-15 21:23:04 +00002308TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) {
zhanyong.wan34b034c2010-03-05 21:23:23 +00002309 StringMatchResultListener listener1;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002310 EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002311 EXPECT_EQ("% 2 == 0", listener1.str());
2312
2313 StringMatchResultListener listener2;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002314 EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002315 EXPECT_EQ("", listener2.str());
2316}
2317
zhanyong.wana862f1d2010-03-15 21:23:04 +00002318TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) {
zhanyong.wan34b034c2010-03-05 21:23:23 +00002319 const Matcher<int> is_even = PolymorphicIsEven();
2320 StringMatchResultListener listener1;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002321 EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002322 EXPECT_EQ("% 2 == 0", listener1.str());
2323
2324 const Matcher<const double&> is_zero = Eq(0);
2325 StringMatchResultListener listener2;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002326 EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002327 EXPECT_EQ("", listener2.str());
2328}
2329
zhanyong.wana862f1d2010-03-15 21:23:04 +00002330MATCHER_P(Really, inner_matcher, "") {
2331 return ExplainMatchResult(inner_matcher, arg, result_listener);
2332}
2333
2334TEST(ExplainMatchResultTest, WorksInsideMATCHER) {
2335 EXPECT_THAT(0, Really(Eq(0)));
2336}
2337
zhanyong.wanbf550852009-06-09 06:09:53 +00002338TEST(AllArgsTest, WorksForTuple) {
2339 EXPECT_THAT(make_tuple(1, 2L), AllArgs(Lt()));
2340 EXPECT_THAT(make_tuple(2L, 1), Not(AllArgs(Lt())));
2341}
2342
2343TEST(AllArgsTest, WorksForNonTuple) {
2344 EXPECT_THAT(42, AllArgs(Gt(0)));
2345 EXPECT_THAT('a', Not(AllArgs(Eq('b'))));
2346}
2347
2348class AllArgsHelper {
2349 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002350 AllArgsHelper() {}
2351
zhanyong.wanbf550852009-06-09 06:09:53 +00002352 MOCK_METHOD2(Helper, int(char x, int y));
zhanyong.wan32de5f52009-12-23 00:13:23 +00002353
2354 private:
2355 GTEST_DISALLOW_COPY_AND_ASSIGN_(AllArgsHelper);
zhanyong.wanbf550852009-06-09 06:09:53 +00002356};
2357
2358TEST(AllArgsTest, WorksInWithClause) {
2359 AllArgsHelper helper;
2360 ON_CALL(helper, Helper(_, _))
2361 .With(AllArgs(Lt()))
2362 .WillByDefault(Return(1));
2363 EXPECT_CALL(helper, Helper(_, _));
2364 EXPECT_CALL(helper, Helper(_, _))
2365 .With(AllArgs(Gt()))
2366 .WillOnce(Return(2));
2367
2368 EXPECT_EQ(1, helper.Helper('\1', 2));
2369 EXPECT_EQ(2, helper.Helper('a', 1));
2370}
2371
shiqiane35fdd92008-12-10 05:08:54 +00002372// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2373// matches the matcher.
2374TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {
2375 ASSERT_THAT(5, Ge(2)) << "This should succeed.";
2376 ASSERT_THAT("Foo", EndsWith("oo"));
2377 EXPECT_THAT(2, AllOf(Le(7), Ge(0))) << "This should succeed too.";
2378 EXPECT_THAT("Hello", StartsWith("Hell"));
2379}
2380
2381// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2382// doesn't match the matcher.
2383TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {
2384 // 'n' must be static as it is used in an EXPECT_FATAL_FAILURE(),
2385 // which cannot reference auto variables.
2386 static int n;
2387 n = 5;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002388
2389 // VC++ prior to version 8.0 SP1 has a bug where it will not see any
2390 // functions declared in the namespace scope from within nested classes.
2391 // EXPECT/ASSERT_(NON)FATAL_FAILURE macros use nested classes so that all
2392 // namespace-level functions invoked inside them need to be explicitly
2393 // resolved.
2394 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Gt(10)),
shiqiane35fdd92008-12-10 05:08:54 +00002395 "Value of: n\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002396 "Expected: is > 10\n"
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002397 " Actual: 5");
shiqiane35fdd92008-12-10 05:08:54 +00002398 n = 0;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002399 EXPECT_NONFATAL_FAILURE(
2400 EXPECT_THAT(n, ::testing::AllOf(::testing::Le(7), ::testing::Ge(5))),
2401 "Value of: n\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002402 "Expected: (is <= 7) and (is >= 5)\n"
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002403 " Actual: 0");
shiqiane35fdd92008-12-10 05:08:54 +00002404}
2405
2406// Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument
2407// has a reference type.
2408TEST(MatcherAssertionTest, WorksForByRefArguments) {
2409 // We use a static variable here as EXPECT_FATAL_FAILURE() cannot
2410 // reference auto variables.
2411 static int n;
2412 n = 0;
2413 EXPECT_THAT(n, AllOf(Le(7), Ref(n)));
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002414 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
shiqiane35fdd92008-12-10 05:08:54 +00002415 "Value of: n\n"
2416 "Expected: does not reference the variable @");
2417 // Tests the "Actual" part.
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002418 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002419 "Actual: 0, which is located @");
shiqiane35fdd92008-12-10 05:08:54 +00002420}
2421
zhanyong.wan95b12332009-09-25 18:55:50 +00002422#if !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002423// Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is
2424// monomorphic.
zhanyong.wan95b12332009-09-25 18:55:50 +00002425
2426// ASSERT_THAT("hello", starts_with_he) fails to compile with Nokia's
2427// Symbian compiler: it tries to compile
2428// template<T, U> class MatcherCastImpl { ...
zhanyong.wandb22c222010-01-28 21:52:29 +00002429// virtual bool MatchAndExplain(T x, ...) const {
2430// return source_matcher_.MatchAndExplain(static_cast<U>(x), ...);
zhanyong.wan95b12332009-09-25 18:55:50 +00002431// with U == string and T == const char*
2432// With ASSERT_THAT("hello"...) changed to ASSERT_THAT(string("hello") ... )
2433// the compiler silently crashes with no output.
2434// If MatcherCastImpl is changed to use U(x) instead of static_cast<U>(x)
2435// the code compiles but the converted string is bogus.
shiqiane35fdd92008-12-10 05:08:54 +00002436TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
2437 Matcher<const char*> starts_with_he = StartsWith("he");
2438 ASSERT_THAT("hello", starts_with_he);
2439
2440 Matcher<const string&> ends_with_ok = EndsWith("ok");
2441 ASSERT_THAT("book", ends_with_ok);
2442
2443 Matcher<int> is_greater_than_5 = Gt(5);
2444 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5),
2445 "Value of: 5\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002446 "Expected: is > 5\n"
shiqiane35fdd92008-12-10 05:08:54 +00002447 " Actual: 5");
2448}
zhanyong.wan95b12332009-09-25 18:55:50 +00002449#endif // !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002450
2451// Tests floating-point matchers.
2452template <typename RawType>
2453class FloatingPointTest : public testing::Test {
2454 protected:
2455 typedef typename testing::internal::FloatingPoint<RawType> Floating;
2456 typedef typename Floating::Bits Bits;
2457
2458 virtual void SetUp() {
2459 const size_t max_ulps = Floating::kMaxUlps;
2460
2461 // The bits that represent 0.0.
2462 const Bits zero_bits = Floating(0).bits();
2463
2464 // Makes some numbers close to 0.0.
2465 close_to_positive_zero_ = Floating::ReinterpretBits(zero_bits + max_ulps/2);
2466 close_to_negative_zero_ = -Floating::ReinterpretBits(
2467 zero_bits + max_ulps - max_ulps/2);
2468 further_from_negative_zero_ = -Floating::ReinterpretBits(
2469 zero_bits + max_ulps + 1 - max_ulps/2);
2470
2471 // The bits that represent 1.0.
2472 const Bits one_bits = Floating(1).bits();
2473
2474 // Makes some numbers close to 1.0.
2475 close_to_one_ = Floating::ReinterpretBits(one_bits + max_ulps);
2476 further_from_one_ = Floating::ReinterpretBits(one_bits + max_ulps + 1);
2477
2478 // +infinity.
2479 infinity_ = Floating::Infinity();
2480
2481 // The bits that represent +infinity.
2482 const Bits infinity_bits = Floating(infinity_).bits();
2483
2484 // Makes some numbers close to infinity.
2485 close_to_infinity_ = Floating::ReinterpretBits(infinity_bits - max_ulps);
2486 further_from_infinity_ = Floating::ReinterpretBits(
2487 infinity_bits - max_ulps - 1);
2488
2489 // Makes some NAN's.
2490 nan1_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 1);
2491 nan2_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 200);
2492 }
2493
2494 void TestSize() {
2495 EXPECT_EQ(sizeof(RawType), sizeof(Bits));
2496 }
2497
2498 // A battery of tests for FloatingEqMatcher::Matches.
2499 // matcher_maker is a pointer to a function which creates a FloatingEqMatcher.
2500 void TestMatches(
2501 testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) {
2502 Matcher<RawType> m1 = matcher_maker(0.0);
2503 EXPECT_TRUE(m1.Matches(-0.0));
2504 EXPECT_TRUE(m1.Matches(close_to_positive_zero_));
2505 EXPECT_TRUE(m1.Matches(close_to_negative_zero_));
2506 EXPECT_FALSE(m1.Matches(1.0));
2507
2508 Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_);
2509 EXPECT_FALSE(m2.Matches(further_from_negative_zero_));
2510
2511 Matcher<RawType> m3 = matcher_maker(1.0);
2512 EXPECT_TRUE(m3.Matches(close_to_one_));
2513 EXPECT_FALSE(m3.Matches(further_from_one_));
2514
2515 // Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above.
2516 EXPECT_FALSE(m3.Matches(0.0));
2517
2518 Matcher<RawType> m4 = matcher_maker(-infinity_);
2519 EXPECT_TRUE(m4.Matches(-close_to_infinity_));
2520
2521 Matcher<RawType> m5 = matcher_maker(infinity_);
2522 EXPECT_TRUE(m5.Matches(close_to_infinity_));
2523
2524 // This is interesting as the representations of infinity_ and nan1_
2525 // are only 1 DLP apart.
2526 EXPECT_FALSE(m5.Matches(nan1_));
2527
2528 // matcher_maker can produce a Matcher<const RawType&>, which is needed in
2529 // some cases.
2530 Matcher<const RawType&> m6 = matcher_maker(0.0);
2531 EXPECT_TRUE(m6.Matches(-0.0));
2532 EXPECT_TRUE(m6.Matches(close_to_positive_zero_));
2533 EXPECT_FALSE(m6.Matches(1.0));
2534
2535 // matcher_maker can produce a Matcher<RawType&>, which is needed in some
2536 // cases.
2537 Matcher<RawType&> m7 = matcher_maker(0.0);
2538 RawType x = 0.0;
2539 EXPECT_TRUE(m7.Matches(x));
2540 x = 0.01f;
2541 EXPECT_FALSE(m7.Matches(x));
2542 }
2543
2544 // Pre-calculated numbers to be used by the tests.
2545
2546 static RawType close_to_positive_zero_;
2547 static RawType close_to_negative_zero_;
2548 static RawType further_from_negative_zero_;
2549
2550 static RawType close_to_one_;
2551 static RawType further_from_one_;
2552
2553 static RawType infinity_;
2554 static RawType close_to_infinity_;
2555 static RawType further_from_infinity_;
2556
2557 static RawType nan1_;
2558 static RawType nan2_;
2559};
2560
2561template <typename RawType>
2562RawType FloatingPointTest<RawType>::close_to_positive_zero_;
2563
2564template <typename RawType>
2565RawType FloatingPointTest<RawType>::close_to_negative_zero_;
2566
2567template <typename RawType>
2568RawType FloatingPointTest<RawType>::further_from_negative_zero_;
2569
2570template <typename RawType>
2571RawType FloatingPointTest<RawType>::close_to_one_;
2572
2573template <typename RawType>
2574RawType FloatingPointTest<RawType>::further_from_one_;
2575
2576template <typename RawType>
2577RawType FloatingPointTest<RawType>::infinity_;
2578
2579template <typename RawType>
2580RawType FloatingPointTest<RawType>::close_to_infinity_;
2581
2582template <typename RawType>
2583RawType FloatingPointTest<RawType>::further_from_infinity_;
2584
2585template <typename RawType>
2586RawType FloatingPointTest<RawType>::nan1_;
2587
2588template <typename RawType>
2589RawType FloatingPointTest<RawType>::nan2_;
2590
2591// Instantiate FloatingPointTest for testing floats.
2592typedef FloatingPointTest<float> FloatTest;
2593
2594TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) {
2595 TestMatches(&FloatEq);
2596}
2597
2598TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
2599 TestMatches(&NanSensitiveFloatEq);
2600}
2601
2602TEST_F(FloatTest, FloatEqCannotMatchNaN) {
2603 // FloatEq never matches NaN.
2604 Matcher<float> m = FloatEq(nan1_);
2605 EXPECT_FALSE(m.Matches(nan1_));
2606 EXPECT_FALSE(m.Matches(nan2_));
2607 EXPECT_FALSE(m.Matches(1.0));
2608}
2609
2610TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {
2611 // NanSensitiveFloatEq will match NaN.
2612 Matcher<float> m = NanSensitiveFloatEq(nan1_);
2613 EXPECT_TRUE(m.Matches(nan1_));
2614 EXPECT_TRUE(m.Matches(nan2_));
2615 EXPECT_FALSE(m.Matches(1.0));
2616}
2617
2618TEST_F(FloatTest, FloatEqCanDescribeSelf) {
2619 Matcher<float> m1 = FloatEq(2.0f);
2620 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002621 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002622
2623 Matcher<float> m2 = FloatEq(0.5f);
2624 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002625 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002626
2627 Matcher<float> m3 = FloatEq(nan1_);
2628 EXPECT_EQ("never matches", Describe(m3));
2629 EXPECT_EQ("is anything", DescribeNegation(m3));
2630}
2631
2632TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {
2633 Matcher<float> m1 = NanSensitiveFloatEq(2.0f);
2634 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002635 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002636
2637 Matcher<float> m2 = NanSensitiveFloatEq(0.5f);
2638 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002639 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002640
2641 Matcher<float> m3 = NanSensitiveFloatEq(nan1_);
2642 EXPECT_EQ("is NaN", Describe(m3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002643 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
shiqiane35fdd92008-12-10 05:08:54 +00002644}
2645
2646// Instantiate FloatingPointTest for testing doubles.
2647typedef FloatingPointTest<double> DoubleTest;
2648
2649TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
2650 TestMatches(&DoubleEq);
2651}
2652
2653TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
2654 TestMatches(&NanSensitiveDoubleEq);
2655}
2656
2657TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {
2658 // DoubleEq never matches NaN.
2659 Matcher<double> m = DoubleEq(nan1_);
2660 EXPECT_FALSE(m.Matches(nan1_));
2661 EXPECT_FALSE(m.Matches(nan2_));
2662 EXPECT_FALSE(m.Matches(1.0));
2663}
2664
2665TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {
2666 // NanSensitiveDoubleEq will match NaN.
2667 Matcher<double> m = NanSensitiveDoubleEq(nan1_);
2668 EXPECT_TRUE(m.Matches(nan1_));
2669 EXPECT_TRUE(m.Matches(nan2_));
2670 EXPECT_FALSE(m.Matches(1.0));
2671}
2672
2673TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {
2674 Matcher<double> m1 = DoubleEq(2.0);
2675 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002676 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002677
2678 Matcher<double> m2 = DoubleEq(0.5);
2679 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002680 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002681
2682 Matcher<double> m3 = DoubleEq(nan1_);
2683 EXPECT_EQ("never matches", Describe(m3));
2684 EXPECT_EQ("is anything", DescribeNegation(m3));
2685}
2686
2687TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {
2688 Matcher<double> m1 = NanSensitiveDoubleEq(2.0);
2689 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002690 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002691
2692 Matcher<double> m2 = NanSensitiveDoubleEq(0.5);
2693 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002694 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002695
2696 Matcher<double> m3 = NanSensitiveDoubleEq(nan1_);
2697 EXPECT_EQ("is NaN", Describe(m3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002698 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
shiqiane35fdd92008-12-10 05:08:54 +00002699}
2700
2701TEST(PointeeTest, RawPointer) {
2702 const Matcher<int*> m = Pointee(Ge(0));
2703
2704 int n = 1;
2705 EXPECT_TRUE(m.Matches(&n));
2706 n = -1;
2707 EXPECT_FALSE(m.Matches(&n));
2708 EXPECT_FALSE(m.Matches(NULL));
2709}
2710
2711TEST(PointeeTest, RawPointerToConst) {
2712 const Matcher<const double*> m = Pointee(Ge(0));
2713
2714 double x = 1;
2715 EXPECT_TRUE(m.Matches(&x));
2716 x = -1;
2717 EXPECT_FALSE(m.Matches(&x));
2718 EXPECT_FALSE(m.Matches(NULL));
2719}
2720
2721TEST(PointeeTest, ReferenceToConstRawPointer) {
2722 const Matcher<int* const &> m = Pointee(Ge(0));
2723
2724 int n = 1;
2725 EXPECT_TRUE(m.Matches(&n));
2726 n = -1;
2727 EXPECT_FALSE(m.Matches(&n));
2728 EXPECT_FALSE(m.Matches(NULL));
2729}
2730
2731TEST(PointeeTest, ReferenceToNonConstRawPointer) {
2732 const Matcher<double* &> m = Pointee(Ge(0));
2733
2734 double x = 1.0;
2735 double* p = &x;
2736 EXPECT_TRUE(m.Matches(p));
2737 x = -1;
2738 EXPECT_FALSE(m.Matches(p));
2739 p = NULL;
2740 EXPECT_FALSE(m.Matches(p));
2741}
2742
2743TEST(PointeeTest, NeverMatchesNull) {
2744 const Matcher<const char*> m = Pointee(_);
2745 EXPECT_FALSE(m.Matches(NULL));
2746}
2747
2748// Tests that we can write Pointee(value) instead of Pointee(Eq(value)).
2749TEST(PointeeTest, MatchesAgainstAValue) {
2750 const Matcher<int*> m = Pointee(5);
2751
2752 int n = 5;
2753 EXPECT_TRUE(m.Matches(&n));
2754 n = -1;
2755 EXPECT_FALSE(m.Matches(&n));
2756 EXPECT_FALSE(m.Matches(NULL));
2757}
2758
2759TEST(PointeeTest, CanDescribeSelf) {
2760 const Matcher<int*> m = Pointee(Gt(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002761 EXPECT_EQ("points to a value that is > 3", Describe(m));
2762 EXPECT_EQ("does not point to a value that is > 3",
shiqiane35fdd92008-12-10 05:08:54 +00002763 DescribeNegation(m));
2764}
2765
shiqiane35fdd92008-12-10 05:08:54 +00002766TEST(PointeeTest, CanExplainMatchResult) {
2767 const Matcher<const string*> m = Pointee(StartsWith("Hi"));
2768
2769 EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL)));
2770
2771 const Matcher<int*> m2 = Pointee(GreaterThan(1));
2772 int n = 3;
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002773 EXPECT_EQ("which points to 3, which is 2 more than 1",
2774 Explain(m2, &n));
2775}
2776
2777TEST(PointeeTest, AlwaysExplainsPointee) {
2778 const Matcher<int*> m = Pointee(0);
2779 int n = 42;
2780 EXPECT_EQ("which points to 42", Explain(m, &n));
shiqiane35fdd92008-12-10 05:08:54 +00002781}
2782
2783// An uncopyable class.
2784class Uncopyable {
2785 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002786 explicit Uncopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00002787
2788 int value() const { return value_; }
2789 private:
2790 const int value_;
2791 GTEST_DISALLOW_COPY_AND_ASSIGN_(Uncopyable);
2792};
2793
2794// Returns true iff x.value() is positive.
2795bool ValueIsPositive(const Uncopyable& x) { return x.value() > 0; }
2796
2797// A user-defined struct for testing Field().
2798struct AStruct {
2799 AStruct() : x(0), y(1.0), z(5), p(NULL) {}
2800 AStruct(const AStruct& rhs)
2801 : x(rhs.x), y(rhs.y), z(rhs.z.value()), p(rhs.p) {}
2802
2803 int x; // A non-const field.
2804 const double y; // A const field.
2805 Uncopyable z; // An uncopyable field.
2806 const char* p; // A pointer field.
zhanyong.wan32de5f52009-12-23 00:13:23 +00002807
2808 private:
2809 GTEST_DISALLOW_ASSIGN_(AStruct);
shiqiane35fdd92008-12-10 05:08:54 +00002810};
2811
2812// A derived struct for testing Field().
2813struct DerivedStruct : public AStruct {
2814 char ch;
zhanyong.wan32de5f52009-12-23 00:13:23 +00002815
2816 private:
2817 GTEST_DISALLOW_ASSIGN_(DerivedStruct);
shiqiane35fdd92008-12-10 05:08:54 +00002818};
2819
2820// Tests that Field(&Foo::field, ...) works when field is non-const.
2821TEST(FieldTest, WorksForNonConstField) {
2822 Matcher<AStruct> m = Field(&AStruct::x, Ge(0));
2823
2824 AStruct a;
2825 EXPECT_TRUE(m.Matches(a));
2826 a.x = -1;
2827 EXPECT_FALSE(m.Matches(a));
2828}
2829
2830// Tests that Field(&Foo::field, ...) works when field is const.
2831TEST(FieldTest, WorksForConstField) {
2832 AStruct a;
2833
2834 Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0));
2835 EXPECT_TRUE(m.Matches(a));
2836 m = Field(&AStruct::y, Le(0.0));
2837 EXPECT_FALSE(m.Matches(a));
2838}
2839
2840// Tests that Field(&Foo::field, ...) works when field is not copyable.
2841TEST(FieldTest, WorksForUncopyableField) {
2842 AStruct a;
2843
2844 Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive));
2845 EXPECT_TRUE(m.Matches(a));
2846 m = Field(&AStruct::z, Not(Truly(ValueIsPositive)));
2847 EXPECT_FALSE(m.Matches(a));
2848}
2849
2850// Tests that Field(&Foo::field, ...) works when field is a pointer.
2851TEST(FieldTest, WorksForPointerField) {
2852 // Matching against NULL.
2853 Matcher<AStruct> m = Field(&AStruct::p, static_cast<const char*>(NULL));
2854 AStruct a;
2855 EXPECT_TRUE(m.Matches(a));
2856 a.p = "hi";
2857 EXPECT_FALSE(m.Matches(a));
2858
2859 // Matching a pointer that is not NULL.
2860 m = Field(&AStruct::p, StartsWith("hi"));
2861 a.p = "hill";
2862 EXPECT_TRUE(m.Matches(a));
2863 a.p = "hole";
2864 EXPECT_FALSE(m.Matches(a));
2865}
2866
2867// Tests that Field() works when the object is passed by reference.
2868TEST(FieldTest, WorksForByRefArgument) {
2869 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2870
2871 AStruct a;
2872 EXPECT_TRUE(m.Matches(a));
2873 a.x = -1;
2874 EXPECT_FALSE(m.Matches(a));
2875}
2876
2877// Tests that Field(&Foo::field, ...) works when the argument's type
2878// is a sub-type of Foo.
2879TEST(FieldTest, WorksForArgumentOfSubType) {
2880 // Note that the matcher expects DerivedStruct but we say AStruct
2881 // inside Field().
2882 Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0));
2883
2884 DerivedStruct d;
2885 EXPECT_TRUE(m.Matches(d));
2886 d.x = -1;
2887 EXPECT_FALSE(m.Matches(d));
2888}
2889
2890// Tests that Field(&Foo::field, m) works when field's type and m's
2891// argument type are compatible but not the same.
2892TEST(FieldTest, WorksForCompatibleMatcherType) {
2893 // The field is an int, but the inner matcher expects a signed char.
2894 Matcher<const AStruct&> m = Field(&AStruct::x,
2895 Matcher<signed char>(Ge(0)));
2896
2897 AStruct a;
2898 EXPECT_TRUE(m.Matches(a));
2899 a.x = -1;
2900 EXPECT_FALSE(m.Matches(a));
2901}
2902
2903// Tests that Field() can describe itself.
2904TEST(FieldTest, CanDescribeSelf) {
2905 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2906
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002907 EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
2908 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00002909}
2910
2911// Tests that Field() can explain the match result.
2912TEST(FieldTest, CanExplainMatchResult) {
2913 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2914
2915 AStruct a;
2916 a.x = 1;
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002917 EXPECT_EQ("whose given field is 1", Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00002918
2919 m = Field(&AStruct::x, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002920 EXPECT_EQ("whose given field is 1, which is 1 more than 0", Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00002921}
2922
2923// Tests that Field() works when the argument is a pointer to const.
2924TEST(FieldForPointerTest, WorksForPointerToConst) {
2925 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2926
2927 AStruct a;
2928 EXPECT_TRUE(m.Matches(&a));
2929 a.x = -1;
2930 EXPECT_FALSE(m.Matches(&a));
2931}
2932
2933// Tests that Field() works when the argument is a pointer to non-const.
2934TEST(FieldForPointerTest, WorksForPointerToNonConst) {
2935 Matcher<AStruct*> m = Field(&AStruct::x, Ge(0));
2936
2937 AStruct a;
2938 EXPECT_TRUE(m.Matches(&a));
2939 a.x = -1;
2940 EXPECT_FALSE(m.Matches(&a));
2941}
2942
zhanyong.wan6953a722010-01-13 05:15:07 +00002943// Tests that Field() works when the argument is a reference to a const pointer.
2944TEST(FieldForPointerTest, WorksForReferenceToConstPointer) {
2945 Matcher<AStruct* const&> m = Field(&AStruct::x, Ge(0));
2946
2947 AStruct a;
2948 EXPECT_TRUE(m.Matches(&a));
2949 a.x = -1;
2950 EXPECT_FALSE(m.Matches(&a));
2951}
2952
shiqiane35fdd92008-12-10 05:08:54 +00002953// Tests that Field() does not match the NULL pointer.
2954TEST(FieldForPointerTest, DoesNotMatchNull) {
2955 Matcher<const AStruct*> m = Field(&AStruct::x, _);
2956 EXPECT_FALSE(m.Matches(NULL));
2957}
2958
2959// Tests that Field(&Foo::field, ...) works when the argument's type
2960// is a sub-type of const Foo*.
2961TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
2962 // Note that the matcher expects DerivedStruct but we say AStruct
2963 // inside Field().
2964 Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0));
2965
2966 DerivedStruct d;
2967 EXPECT_TRUE(m.Matches(&d));
2968 d.x = -1;
2969 EXPECT_FALSE(m.Matches(&d));
2970}
2971
2972// Tests that Field() can describe itself when used to match a pointer.
2973TEST(FieldForPointerTest, CanDescribeSelf) {
2974 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2975
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002976 EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
2977 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00002978}
2979
2980// Tests that Field() can explain the result of matching a pointer.
2981TEST(FieldForPointerTest, CanExplainMatchResult) {
2982 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2983
2984 AStruct a;
2985 a.x = 1;
2986 EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(NULL)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002987 EXPECT_EQ("which points to an object whose given field is 1", Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00002988
2989 m = Field(&AStruct::x, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002990 EXPECT_EQ("which points to an object whose given field is 1, "
2991 "which is 1 more than 0", Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00002992}
2993
2994// A user-defined class for testing Property().
2995class AClass {
2996 public:
2997 AClass() : n_(0) {}
2998
2999 // A getter that returns a non-reference.
3000 int n() const { return n_; }
3001
3002 void set_n(int new_n) { n_ = new_n; }
3003
3004 // A getter that returns a reference to const.
3005 const string& s() const { return s_; }
3006
3007 void set_s(const string& new_s) { s_ = new_s; }
3008
3009 // A getter that returns a reference to non-const.
3010 double& x() const { return x_; }
3011 private:
3012 int n_;
3013 string s_;
3014
3015 static double x_;
3016};
3017
3018double AClass::x_ = 0.0;
3019
3020// A derived class for testing Property().
3021class DerivedClass : public AClass {
3022 private:
3023 int k_;
3024};
3025
3026// Tests that Property(&Foo::property, ...) works when property()
3027// returns a non-reference.
3028TEST(PropertyTest, WorksForNonReferenceProperty) {
3029 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3030
3031 AClass a;
3032 a.set_n(1);
3033 EXPECT_TRUE(m.Matches(a));
3034
3035 a.set_n(-1);
3036 EXPECT_FALSE(m.Matches(a));
3037}
3038
3039// Tests that Property(&Foo::property, ...) works when property()
3040// returns a reference to const.
3041TEST(PropertyTest, WorksForReferenceToConstProperty) {
3042 Matcher<const AClass&> m = Property(&AClass::s, StartsWith("hi"));
3043
3044 AClass a;
3045 a.set_s("hill");
3046 EXPECT_TRUE(m.Matches(a));
3047
3048 a.set_s("hole");
3049 EXPECT_FALSE(m.Matches(a));
3050}
3051
3052// Tests that Property(&Foo::property, ...) works when property()
3053// returns a reference to non-const.
3054TEST(PropertyTest, WorksForReferenceToNonConstProperty) {
3055 double x = 0.0;
3056 AClass a;
3057
3058 Matcher<const AClass&> m = Property(&AClass::x, Ref(x));
3059 EXPECT_FALSE(m.Matches(a));
3060
3061 m = Property(&AClass::x, Not(Ref(x)));
3062 EXPECT_TRUE(m.Matches(a));
3063}
3064
3065// Tests that Property(&Foo::property, ...) works when the argument is
3066// passed by value.
3067TEST(PropertyTest, WorksForByValueArgument) {
3068 Matcher<AClass> m = Property(&AClass::s, StartsWith("hi"));
3069
3070 AClass a;
3071 a.set_s("hill");
3072 EXPECT_TRUE(m.Matches(a));
3073
3074 a.set_s("hole");
3075 EXPECT_FALSE(m.Matches(a));
3076}
3077
3078// Tests that Property(&Foo::property, ...) works when the argument's
3079// type is a sub-type of Foo.
3080TEST(PropertyTest, WorksForArgumentOfSubType) {
3081 // The matcher expects a DerivedClass, but inside the Property() we
3082 // say AClass.
3083 Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0));
3084
3085 DerivedClass d;
3086 d.set_n(1);
3087 EXPECT_TRUE(m.Matches(d));
3088
3089 d.set_n(-1);
3090 EXPECT_FALSE(m.Matches(d));
3091}
3092
3093// Tests that Property(&Foo::property, m) works when property()'s type
3094// and m's argument type are compatible but different.
3095TEST(PropertyTest, WorksForCompatibleMatcherType) {
3096 // n() returns an int but the inner matcher expects a signed char.
3097 Matcher<const AClass&> m = Property(&AClass::n,
3098 Matcher<signed char>(Ge(0)));
3099
3100 AClass a;
3101 EXPECT_TRUE(m.Matches(a));
3102 a.set_n(-1);
3103 EXPECT_FALSE(m.Matches(a));
3104}
3105
3106// Tests that Property() can describe itself.
3107TEST(PropertyTest, CanDescribeSelf) {
3108 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3109
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003110 EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
3111 EXPECT_EQ("is an object whose given property isn't >= 0",
3112 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003113}
3114
3115// Tests that Property() can explain the match result.
3116TEST(PropertyTest, CanExplainMatchResult) {
3117 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3118
3119 AClass a;
3120 a.set_n(1);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003121 EXPECT_EQ("whose given property is 1", Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003122
3123 m = Property(&AClass::n, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003124 EXPECT_EQ("whose given property is 1, which is 1 more than 0", Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003125}
3126
3127// Tests that Property() works when the argument is a pointer to const.
3128TEST(PropertyForPointerTest, WorksForPointerToConst) {
3129 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3130
3131 AClass a;
3132 a.set_n(1);
3133 EXPECT_TRUE(m.Matches(&a));
3134
3135 a.set_n(-1);
3136 EXPECT_FALSE(m.Matches(&a));
3137}
3138
3139// Tests that Property() works when the argument is a pointer to non-const.
3140TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
3141 Matcher<AClass*> m = Property(&AClass::s, StartsWith("hi"));
3142
3143 AClass a;
3144 a.set_s("hill");
3145 EXPECT_TRUE(m.Matches(&a));
3146
3147 a.set_s("hole");
3148 EXPECT_FALSE(m.Matches(&a));
3149}
3150
zhanyong.wan6953a722010-01-13 05:15:07 +00003151// Tests that Property() works when the argument is a reference to a
3152// const pointer.
3153TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) {
3154 Matcher<AClass* const&> m = Property(&AClass::s, StartsWith("hi"));
3155
3156 AClass a;
3157 a.set_s("hill");
3158 EXPECT_TRUE(m.Matches(&a));
3159
3160 a.set_s("hole");
3161 EXPECT_FALSE(m.Matches(&a));
3162}
3163
shiqiane35fdd92008-12-10 05:08:54 +00003164// Tests that Property() does not match the NULL pointer.
3165TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
3166 Matcher<const AClass*> m = Property(&AClass::x, _);
3167 EXPECT_FALSE(m.Matches(NULL));
3168}
3169
3170// Tests that Property(&Foo::property, ...) works when the argument's
3171// type is a sub-type of const Foo*.
3172TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
3173 // The matcher expects a DerivedClass, but inside the Property() we
3174 // say AClass.
3175 Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0));
3176
3177 DerivedClass d;
3178 d.set_n(1);
3179 EXPECT_TRUE(m.Matches(&d));
3180
3181 d.set_n(-1);
3182 EXPECT_FALSE(m.Matches(&d));
3183}
3184
3185// Tests that Property() can describe itself when used to match a pointer.
3186TEST(PropertyForPointerTest, CanDescribeSelf) {
3187 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3188
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003189 EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
3190 EXPECT_EQ("is an object whose given property isn't >= 0",
3191 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003192}
3193
3194// Tests that Property() can explain the result of matching a pointer.
3195TEST(PropertyForPointerTest, CanExplainMatchResult) {
3196 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3197
3198 AClass a;
3199 a.set_n(1);
3200 EXPECT_EQ("", Explain(m, static_cast<const AClass*>(NULL)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003201 EXPECT_EQ("which points to an object whose given property is 1",
3202 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003203
3204 m = Property(&AClass::n, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003205 EXPECT_EQ("which points to an object whose given property is 1, "
3206 "which is 1 more than 0", Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003207}
3208
3209// Tests ResultOf.
3210
3211// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3212// function pointer.
3213string IntToStringFunction(int input) { return input == 1 ? "foo" : "bar"; }
3214
3215TEST(ResultOfTest, WorksForFunctionPointers) {
3216 Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(string("foo")));
3217
3218 EXPECT_TRUE(matcher.Matches(1));
3219 EXPECT_FALSE(matcher.Matches(2));
3220}
3221
3222// Tests that ResultOf() can describe itself.
3223TEST(ResultOfTest, CanDescribeItself) {
3224 Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo"));
3225
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003226 EXPECT_EQ("is mapped by the given callable to a value that "
3227 "is equal to \"foo\"", Describe(matcher));
3228 EXPECT_EQ("is mapped by the given callable to a value that "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003229 "isn't equal to \"foo\"", DescribeNegation(matcher));
shiqiane35fdd92008-12-10 05:08:54 +00003230}
3231
3232// Tests that ResultOf() can explain the match result.
3233int IntFunction(int input) { return input == 42 ? 80 : 90; }
3234
3235TEST(ResultOfTest, CanExplainMatchResult) {
3236 Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003237 EXPECT_EQ("which is mapped by the given callable to 90",
3238 Explain(matcher, 36));
shiqiane35fdd92008-12-10 05:08:54 +00003239
3240 matcher = ResultOf(&IntFunction, GreaterThan(85));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003241 EXPECT_EQ("which is mapped by the given callable to 90, "
3242 "which is 5 more than 85", Explain(matcher, 36));
shiqiane35fdd92008-12-10 05:08:54 +00003243}
3244
3245// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3246// returns a non-reference.
3247TEST(ResultOfTest, WorksForNonReferenceResults) {
3248 Matcher<int> matcher = ResultOf(&IntFunction, Eq(80));
3249
3250 EXPECT_TRUE(matcher.Matches(42));
3251 EXPECT_FALSE(matcher.Matches(36));
3252}
3253
3254// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3255// returns a reference to non-const.
3256double& DoubleFunction(double& input) { return input; }
3257
3258Uncopyable& RefUncopyableFunction(Uncopyable& obj) {
3259 return obj;
3260}
3261
3262TEST(ResultOfTest, WorksForReferenceToNonConstResults) {
3263 double x = 3.14;
3264 double x2 = x;
3265 Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x));
3266
3267 EXPECT_TRUE(matcher.Matches(x));
3268 EXPECT_FALSE(matcher.Matches(x2));
3269
3270 // Test that ResultOf works with uncopyable objects
3271 Uncopyable obj(0);
3272 Uncopyable obj2(0);
3273 Matcher<Uncopyable&> matcher2 =
3274 ResultOf(&RefUncopyableFunction, Ref(obj));
3275
3276 EXPECT_TRUE(matcher2.Matches(obj));
3277 EXPECT_FALSE(matcher2.Matches(obj2));
3278}
3279
3280// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3281// returns a reference to const.
3282const string& StringFunction(const string& input) { return input; }
3283
3284TEST(ResultOfTest, WorksForReferenceToConstResults) {
3285 string s = "foo";
3286 string s2 = s;
3287 Matcher<const string&> matcher = ResultOf(&StringFunction, Ref(s));
3288
3289 EXPECT_TRUE(matcher.Matches(s));
3290 EXPECT_FALSE(matcher.Matches(s2));
3291}
3292
3293// Tests that ResultOf(f, m) works when f(x) and m's
3294// argument types are compatible but different.
3295TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {
3296 // IntFunction() returns int but the inner matcher expects a signed char.
3297 Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85)));
3298
3299 EXPECT_TRUE(matcher.Matches(36));
3300 EXPECT_FALSE(matcher.Matches(42));
3301}
3302
shiqiane35fdd92008-12-10 05:08:54 +00003303// Tests that the program aborts when ResultOf is passed
3304// a NULL function pointer.
3305TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +00003306 EXPECT_DEATH_IF_SUPPORTED(
shiqiane35fdd92008-12-10 05:08:54 +00003307 ResultOf(static_cast<string(*)(int)>(NULL), Eq(string("foo"))),
3308 "NULL function pointer is passed into ResultOf\\(\\)\\.");
3309}
shiqiane35fdd92008-12-10 05:08:54 +00003310
3311// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3312// function reference.
3313TEST(ResultOfTest, WorksForFunctionReferences) {
3314 Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq("foo"));
3315 EXPECT_TRUE(matcher.Matches(1));
3316 EXPECT_FALSE(matcher.Matches(2));
3317}
3318
3319// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3320// function object.
3321struct Functor : public ::std::unary_function<int, string> {
3322 result_type operator()(argument_type input) const {
3323 return IntToStringFunction(input);
3324 }
3325};
3326
3327TEST(ResultOfTest, WorksForFunctors) {
3328 Matcher<int> matcher = ResultOf(Functor(), Eq(string("foo")));
3329
3330 EXPECT_TRUE(matcher.Matches(1));
3331 EXPECT_FALSE(matcher.Matches(2));
3332}
3333
3334// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3335// functor with more then one operator() defined. ResultOf() must work
3336// for each defined operator().
3337struct PolymorphicFunctor {
3338 typedef int result_type;
3339 int operator()(int n) { return n; }
3340 int operator()(const char* s) { return static_cast<int>(strlen(s)); }
3341};
3342
3343TEST(ResultOfTest, WorksForPolymorphicFunctors) {
3344 Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5));
3345
3346 EXPECT_TRUE(matcher_int.Matches(10));
3347 EXPECT_FALSE(matcher_int.Matches(2));
3348
3349 Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5));
3350
3351 EXPECT_TRUE(matcher_string.Matches("long string"));
3352 EXPECT_FALSE(matcher_string.Matches("shrt"));
3353}
3354
3355const int* ReferencingFunction(const int& n) { return &n; }
3356
3357struct ReferencingFunctor {
3358 typedef const int* result_type;
3359 result_type operator()(const int& n) { return &n; }
3360};
3361
3362TEST(ResultOfTest, WorksForReferencingCallables) {
3363 const int n = 1;
3364 const int n2 = 1;
3365 Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n));
3366 EXPECT_TRUE(matcher2.Matches(n));
3367 EXPECT_FALSE(matcher2.Matches(n2));
3368
3369 Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n));
3370 EXPECT_TRUE(matcher3.Matches(n));
3371 EXPECT_FALSE(matcher3.Matches(n2));
3372}
3373
shiqiane35fdd92008-12-10 05:08:54 +00003374class DivisibleByImpl {
3375 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003376 explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {}
shiqiane35fdd92008-12-10 05:08:54 +00003377
zhanyong.wandb22c222010-01-28 21:52:29 +00003378 // For testing using ExplainMatchResultTo() with polymorphic matchers.
shiqiane35fdd92008-12-10 05:08:54 +00003379 template <typename T>
zhanyong.wandb22c222010-01-28 21:52:29 +00003380 bool MatchAndExplain(const T& n, MatchResultListener* listener) const {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003381 *listener << "which is " << (n % divider_) << " modulo "
zhanyong.wandb22c222010-01-28 21:52:29 +00003382 << divider_;
shiqiane35fdd92008-12-10 05:08:54 +00003383 return (n % divider_) == 0;
3384 }
3385
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003386 void DescribeTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +00003387 *os << "is divisible by " << divider_;
3388 }
3389
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003390 void DescribeNegationTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +00003391 *os << "is not divisible by " << divider_;
3392 }
3393
zhanyong.wan32de5f52009-12-23 00:13:23 +00003394 void set_divider(int a_divider) { divider_ = a_divider; }
shiqiane35fdd92008-12-10 05:08:54 +00003395 int divider() const { return divider_; }
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003396
shiqiane35fdd92008-12-10 05:08:54 +00003397 private:
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003398 int divider_;
shiqiane35fdd92008-12-10 05:08:54 +00003399};
3400
shiqiane35fdd92008-12-10 05:08:54 +00003401PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) {
3402 return MakePolymorphicMatcher(DivisibleByImpl(n));
3403}
3404
3405// Tests that when AllOf() fails, only the first failing matcher is
3406// asked to explain why.
3407TEST(ExplainMatchResultTest, AllOf_False_False) {
3408 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003409 EXPECT_EQ("which is 1 modulo 4", Explain(m, 5));
shiqiane35fdd92008-12-10 05:08:54 +00003410}
3411
3412// Tests that when AllOf() fails, only the first failing matcher is
3413// asked to explain why.
3414TEST(ExplainMatchResultTest, AllOf_False_True) {
3415 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003416 EXPECT_EQ("which is 2 modulo 4", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003417}
3418
3419// Tests that when AllOf() fails, only the first failing matcher is
3420// asked to explain why.
3421TEST(ExplainMatchResultTest, AllOf_True_False) {
3422 const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003423 EXPECT_EQ("which is 2 modulo 3", Explain(m, 5));
shiqiane35fdd92008-12-10 05:08:54 +00003424}
3425
3426// Tests that when AllOf() succeeds, all matchers are asked to explain
3427// why.
3428TEST(ExplainMatchResultTest, AllOf_True_True) {
3429 const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003430 EXPECT_EQ("which is 0 modulo 2, and which is 0 modulo 3", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003431}
3432
3433TEST(ExplainMatchResultTest, AllOf_True_True_2) {
3434 const Matcher<int> m = AllOf(Ge(2), Le(3));
3435 EXPECT_EQ("", Explain(m, 2));
3436}
3437
3438TEST(ExplainmatcherResultTest, MonomorphicMatcher) {
3439 const Matcher<int> m = GreaterThan(5);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003440 EXPECT_EQ("which is 1 more than 5", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003441}
3442
3443// The following two tests verify that values without a public copy
3444// ctor can be used as arguments to matchers like Eq(), Ge(), and etc
3445// with the help of ByRef().
3446
3447class NotCopyable {
3448 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003449 explicit NotCopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00003450
3451 int value() const { return value_; }
3452
3453 bool operator==(const NotCopyable& rhs) const {
3454 return value() == rhs.value();
3455 }
3456
3457 bool operator>=(const NotCopyable& rhs) const {
3458 return value() >= rhs.value();
3459 }
3460 private:
3461 int value_;
3462
3463 GTEST_DISALLOW_COPY_AND_ASSIGN_(NotCopyable);
3464};
3465
3466TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) {
3467 const NotCopyable const_value1(1);
3468 const Matcher<const NotCopyable&> m = Eq(ByRef(const_value1));
3469
3470 const NotCopyable n1(1), n2(2);
3471 EXPECT_TRUE(m.Matches(n1));
3472 EXPECT_FALSE(m.Matches(n2));
3473}
3474
3475TEST(ByRefTest, AllowsNotCopyableValueInMatchers) {
3476 NotCopyable value2(2);
3477 const Matcher<NotCopyable&> m = Ge(ByRef(value2));
3478
3479 NotCopyable n1(1), n2(2);
3480 EXPECT_FALSE(m.Matches(n1));
3481 EXPECT_TRUE(m.Matches(n2));
3482}
3483
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003484#if GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003485// Tests ContainerEq with different container types, and
3486// different element types.
3487
3488template <typename T>
zhanyong.wanb8243162009-06-04 05:48:20 +00003489class ContainerEqTest : public testing::Test {};
zhanyong.wan6a896b52009-01-16 01:13:50 +00003490
3491typedef testing::Types<
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003492 set<int>,
3493 vector<size_t>,
3494 multiset<size_t>,
3495 list<int> >
zhanyong.wan6a896b52009-01-16 01:13:50 +00003496 ContainerEqTestTypes;
3497
3498TYPED_TEST_CASE(ContainerEqTest, ContainerEqTestTypes);
3499
3500// Tests that the filled container is equal to itself.
3501TYPED_TEST(ContainerEqTest, EqualsSelf) {
3502 static const int vals[] = {1, 1, 2, 3, 5, 8};
3503 TypeParam my_set(vals, vals + 6);
3504 const Matcher<TypeParam> m = ContainerEq(my_set);
3505 EXPECT_TRUE(m.Matches(my_set));
3506 EXPECT_EQ("", Explain(m, my_set));
3507}
3508
3509// Tests that missing values are reported.
3510TYPED_TEST(ContainerEqTest, ValueMissing) {
3511 static const int vals[] = {1, 1, 2, 3, 5, 8};
3512 static const int test_vals[] = {2, 1, 8, 5};
3513 TypeParam my_set(vals, vals + 6);
3514 TypeParam test_set(test_vals, test_vals + 4);
3515 const Matcher<TypeParam> m = ContainerEq(my_set);
3516 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003517 EXPECT_EQ("which doesn't have these expected elements: 3",
3518 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003519}
3520
3521// Tests that added values are reported.
3522TYPED_TEST(ContainerEqTest, ValueAdded) {
3523 static const int vals[] = {1, 1, 2, 3, 5, 8};
3524 static const int test_vals[] = {1, 2, 3, 5, 8, 46};
3525 TypeParam my_set(vals, vals + 6);
3526 TypeParam test_set(test_vals, test_vals + 6);
3527 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3528 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003529 EXPECT_EQ("which has these unexpected elements: 46", Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003530}
3531
3532// Tests that added and missing values are reported together.
3533TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) {
3534 static const int vals[] = {1, 1, 2, 3, 5, 8};
3535 static const int test_vals[] = {1, 2, 3, 8, 46};
3536 TypeParam my_set(vals, vals + 6);
3537 TypeParam test_set(test_vals, test_vals + 5);
3538 const Matcher<TypeParam> m = ContainerEq(my_set);
3539 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003540 EXPECT_EQ("which has these unexpected elements: 46,\n"
3541 "and doesn't have these expected elements: 5",
3542 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003543}
3544
3545// Tests duplicated value -- expect no explanation.
3546TYPED_TEST(ContainerEqTest, DuplicateDifference) {
3547 static const int vals[] = {1, 1, 2, 3, 5, 8};
3548 static const int test_vals[] = {1, 2, 3, 5, 8};
3549 TypeParam my_set(vals, vals + 6);
3550 TypeParam test_set(test_vals, test_vals + 5);
3551 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3552 // Depending on the container, match may be true or false
3553 // But in any case there should be no explanation.
3554 EXPECT_EQ("", Explain(m, test_set));
3555}
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003556#endif // GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003557
3558// Tests that mutliple missing values are reported.
3559// Using just vector here, so order is predicatble.
3560TEST(ContainerEqExtraTest, MultipleValuesMissing) {
3561 static const int vals[] = {1, 1, 2, 3, 5, 8};
3562 static const int test_vals[] = {2, 1, 5};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003563 vector<int> my_set(vals, vals + 6);
3564 vector<int> test_set(test_vals, test_vals + 3);
3565 const Matcher<vector<int> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003566 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003567 EXPECT_EQ("which doesn't have these expected elements: 3, 8",
3568 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003569}
3570
3571// Tests that added values are reported.
3572// Using just vector here, so order is predicatble.
3573TEST(ContainerEqExtraTest, MultipleValuesAdded) {
3574 static const int vals[] = {1, 1, 2, 3, 5, 8};
3575 static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003576 list<size_t> my_set(vals, vals + 6);
3577 list<size_t> test_set(test_vals, test_vals + 7);
3578 const Matcher<const list<size_t>&> m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003579 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003580 EXPECT_EQ("which has these unexpected elements: 92, 46",
3581 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003582}
3583
3584// Tests that added and missing values are reported together.
3585TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) {
3586 static const int vals[] = {1, 1, 2, 3, 5, 8};
3587 static const int test_vals[] = {1, 2, 3, 92, 46};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003588 list<size_t> my_set(vals, vals + 6);
3589 list<size_t> test_set(test_vals, test_vals + 5);
3590 const Matcher<const list<size_t> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003591 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003592 EXPECT_EQ("which has these unexpected elements: 92, 46,\n"
3593 "and doesn't have these expected elements: 5, 8",
zhanyong.wan6a896b52009-01-16 01:13:50 +00003594 Explain(m, test_set));
3595}
3596
3597// Tests to see that duplicate elements are detected,
3598// but (as above) not reported in the explanation.
3599TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) {
3600 static const int vals[] = {1, 1, 2, 3, 5, 8};
3601 static const int test_vals[] = {1, 2, 3, 5, 8};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003602 vector<int> my_set(vals, vals + 6);
3603 vector<int> test_set(test_vals, test_vals + 5);
3604 const Matcher<vector<int> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003605 EXPECT_TRUE(m.Matches(my_set));
3606 EXPECT_FALSE(m.Matches(test_set));
3607 // There is nothing to report when both sets contain all the same values.
3608 EXPECT_EQ("", Explain(m, test_set));
3609}
3610
3611// Tests that ContainerEq works for non-trivial associative containers,
3612// like maps.
3613TEST(ContainerEqExtraTest, WorksForMaps) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003614 map<int, std::string> my_map;
zhanyong.wan6a896b52009-01-16 01:13:50 +00003615 my_map[0] = "a";
3616 my_map[1] = "b";
3617
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003618 map<int, std::string> test_map;
zhanyong.wan6a896b52009-01-16 01:13:50 +00003619 test_map[0] = "aa";
3620 test_map[1] = "b";
3621
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003622 const Matcher<const map<int, std::string>&> m = ContainerEq(my_map);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003623 EXPECT_TRUE(m.Matches(my_map));
3624 EXPECT_FALSE(m.Matches(test_map));
3625
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003626 EXPECT_EQ("which has these unexpected elements: (0, \"aa\"),\n"
3627 "and doesn't have these expected elements: (0, \"a\")",
zhanyong.wan6a896b52009-01-16 01:13:50 +00003628 Explain(m, test_map));
3629}
3630
zhanyong.wanb8243162009-06-04 05:48:20 +00003631TEST(ContainerEqExtraTest, WorksForNativeArray) {
3632 int a1[] = { 1, 2, 3 };
3633 int a2[] = { 1, 2, 3 };
3634 int b[] = { 1, 2, 4 };
3635
3636 EXPECT_THAT(a1, ContainerEq(a2));
3637 EXPECT_THAT(a1, Not(ContainerEq(b)));
3638}
3639
3640TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) {
3641 const char a1[][3] = { "hi", "lo" };
3642 const char a2[][3] = { "hi", "lo" };
3643 const char b[][3] = { "lo", "hi" };
3644
3645 // Tests using ContainerEq() in the first dimension.
3646 EXPECT_THAT(a1, ContainerEq(a2));
3647 EXPECT_THAT(a1, Not(ContainerEq(b)));
3648
3649 // Tests using ContainerEq() in the second dimension.
3650 EXPECT_THAT(a1, ElementsAre(ContainerEq(a2[0]), ContainerEq(a2[1])));
3651 EXPECT_THAT(a1, ElementsAre(Not(ContainerEq(b[0])), ContainerEq(a2[1])));
3652}
3653
3654TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) {
3655 const int a1[] = { 1, 2, 3 };
3656 const int a2[] = { 1, 2, 3 };
3657 const int b[] = { 1, 2, 3, 4 };
3658
zhanyong.wan2661c682009-06-09 05:42:12 +00003659 const int* const p1 = a1;
3660 EXPECT_THAT(make_tuple(p1, 3), ContainerEq(a2));
3661 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(b)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003662
3663 const int c[] = { 1, 3, 2 };
zhanyong.wan2661c682009-06-09 05:42:12 +00003664 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(c)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003665}
3666
3667TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) {
3668 std::string a1[][3] = {
3669 { "hi", "hello", "ciao" },
3670 { "bye", "see you", "ciao" }
3671 };
3672
3673 std::string a2[][3] = {
3674 { "hi", "hello", "ciao" },
3675 { "bye", "see you", "ciao" }
3676 };
3677
3678 const Matcher<const std::string(&)[2][3]> m = ContainerEq(a2);
3679 EXPECT_THAT(a1, m);
3680
3681 a2[0][0] = "ha";
3682 EXPECT_THAT(a1, m);
3683}
3684
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003685// Tests JoinAsTuple().
3686
3687TEST(JoinAsTupleTest, JoinsEmptyTuple) {
3688 EXPECT_EQ("", JoinAsTuple(Strings()));
3689}
3690
3691TEST(JoinAsTupleTest, JoinsOneTuple) {
3692 const char* fields[] = { "1" };
3693 EXPECT_EQ("1", JoinAsTuple(Strings(fields, fields + 1)));
3694}
3695
3696TEST(JoinAsTupleTest, JoinsTwoTuple) {
3697 const char* fields[] = { "1", "a" };
3698 EXPECT_EQ("(1, a)", JoinAsTuple(Strings(fields, fields + 2)));
3699}
3700
3701TEST(JoinAsTupleTest, JoinsTenTuple) {
3702 const char* fields[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
3703 EXPECT_EQ("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)",
3704 JoinAsTuple(Strings(fields, fields + 10)));
3705}
3706
3707// Tests FormatMatcherDescription().
3708
3709TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) {
3710 EXPECT_EQ("is even",
zhanyong.wanb4140802010-06-08 22:53:57 +00003711 FormatMatcherDescription(false, "IsEven", Strings()));
3712 EXPECT_EQ("not (is even)",
3713 FormatMatcherDescription(true, "IsEven", Strings()));
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003714
3715 const char* params[] = { "5" };
3716 EXPECT_EQ("equals 5",
zhanyong.wanb4140802010-06-08 22:53:57 +00003717 FormatMatcherDescription(false, "Equals",
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003718 Strings(params, params + 1)));
3719
3720 const char* params2[] = { "5", "8" };
3721 EXPECT_EQ("is in range (5, 8)",
zhanyong.wanb4140802010-06-08 22:53:57 +00003722 FormatMatcherDescription(false, "IsInRange",
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003723 Strings(params2, params2 + 2)));
3724}
3725
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003726// Tests PolymorphicMatcher::mutable_impl().
3727TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {
3728 PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
3729 DivisibleByImpl& impl = m.mutable_impl();
3730 EXPECT_EQ(42, impl.divider());
3731
3732 impl.set_divider(0);
3733 EXPECT_EQ(0, m.mutable_impl().divider());
3734}
3735
3736// Tests PolymorphicMatcher::impl().
3737TEST(PolymorphicMatcherTest, CanAccessImpl) {
3738 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
3739 const DivisibleByImpl& impl = m.impl();
3740 EXPECT_EQ(42, impl.divider());
3741}
3742
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003743TEST(MatcherTupleTest, ExplainsMatchFailure) {
3744 stringstream ss1;
3745 ExplainMatchFailureTupleTo(make_tuple(Matcher<char>(Eq('a')), GreaterThan(5)),
3746 make_tuple('a', 10), &ss1);
3747 EXPECT_EQ("", ss1.str()); // Successful match.
3748
3749 stringstream ss2;
3750 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
3751 make_tuple(2, 'b'), &ss2);
3752 EXPECT_EQ(" Expected arg #0: is > 5\n"
3753 " Actual: 2, which is 3 less than 5\n"
zhanyong.wand60c5f42010-07-21 22:21:07 +00003754 " Expected arg #1: is equal to 'a' (97, 0x61)\n"
3755 " Actual: 'b' (98, 0x62)\n",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003756 ss2.str()); // Failed match where both arguments need explanation.
3757
3758 stringstream ss3;
3759 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
3760 make_tuple(2, 'a'), &ss3);
3761 EXPECT_EQ(" Expected arg #0: is > 5\n"
3762 " Actual: 2, which is 3 less than 5\n",
3763 ss3.str()); // Failed match where only one argument needs
3764 // explanation.
3765}
3766
zhanyong.wan33605ba2010-04-22 23:37:47 +00003767// Tests Each().
3768
3769TEST(EachTest, ExplainsMatchResultCorrectly) {
3770 set<int> a; // empty
3771
3772 Matcher<set<int> > m = Each(2);
3773 EXPECT_EQ("", Explain(m, a));
3774
3775 Matcher<const int(&)[1]> n = Each(1);
3776
3777 const int b[1] = { 1 };
3778 EXPECT_EQ("", Explain(n, b));
3779
3780 n = Each(3);
3781 EXPECT_EQ("whose element #0 doesn't match", Explain(n, b));
3782
3783 a.insert(1);
3784 a.insert(2);
3785 a.insert(3);
3786 m = Each(GreaterThan(0));
3787 EXPECT_EQ("", Explain(m, a));
3788
3789 m = Each(GreaterThan(10));
3790 EXPECT_EQ("whose element #0 doesn't match, which is 9 less than 10",
3791 Explain(m, a));
3792}
3793
3794TEST(EachTest, DescribesItselfCorrectly) {
3795 Matcher<vector<int> > m = Each(1);
3796 EXPECT_EQ("only contains elements that is equal to 1", Describe(m));
3797
3798 Matcher<vector<int> > m2 = Not(m);
3799 EXPECT_EQ("contains some element that isn't equal to 1", Describe(m2));
3800}
3801
3802TEST(EachTest, MatchesVectorWhenAllElementsMatch) {
3803 vector<int> some_vector;
3804 EXPECT_THAT(some_vector, Each(1));
3805 some_vector.push_back(3);
3806 EXPECT_THAT(some_vector, Not(Each(1)));
3807 EXPECT_THAT(some_vector, Each(3));
3808 some_vector.push_back(1);
3809 some_vector.push_back(2);
3810 EXPECT_THAT(some_vector, Not(Each(3)));
3811 EXPECT_THAT(some_vector, Each(Lt(3.5)));
3812
3813 vector<string> another_vector;
3814 another_vector.push_back("fee");
3815 EXPECT_THAT(another_vector, Each(string("fee")));
3816 another_vector.push_back("fie");
3817 another_vector.push_back("foe");
3818 another_vector.push_back("fum");
3819 EXPECT_THAT(another_vector, Not(Each(string("fee"))));
3820}
3821
3822TEST(EachTest, MatchesMapWhenAllElementsMatch) {
3823 map<const char*, int> my_map;
3824 const char* bar = "a string";
3825 my_map[bar] = 2;
3826 EXPECT_THAT(my_map, Each(make_pair(bar, 2)));
3827
3828 map<string, int> another_map;
3829 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1)));
3830 another_map["fee"] = 1;
3831 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1)));
3832 another_map["fie"] = 2;
3833 another_map["foe"] = 3;
3834 another_map["fum"] = 4;
3835 EXPECT_THAT(another_map, Not(Each(make_pair(string("fee"), 1))));
3836 EXPECT_THAT(another_map, Not(Each(make_pair(string("fum"), 1))));
3837 EXPECT_THAT(another_map, Each(Pair(_, Gt(0))));
3838}
3839
3840TEST(EachTest, AcceptsMatcher) {
3841 const int a[] = { 1, 2, 3 };
3842 EXPECT_THAT(a, Each(Gt(0)));
3843 EXPECT_THAT(a, Not(Each(Gt(1))));
3844}
3845
3846TEST(EachTest, WorksForNativeArrayAsTuple) {
3847 const int a[] = { 1, 2 };
3848 const int* const pointer = a;
3849 EXPECT_THAT(make_tuple(pointer, 2), Each(Gt(0)));
3850 EXPECT_THAT(make_tuple(pointer, 2), Not(Each(Gt(1))));
3851}
3852
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003853// For testing Pointwise().
3854class IsHalfOfMatcher {
3855 public:
3856 template <typename T1, typename T2>
3857 bool MatchAndExplain(const tuple<T1, T2>& a_pair,
3858 MatchResultListener* listener) const {
3859 if (get<0>(a_pair) == get<1>(a_pair)/2) {
3860 *listener << "where the second is " << get<1>(a_pair);
3861 return true;
3862 } else {
3863 *listener << "where the second/2 is " << get<1>(a_pair)/2;
3864 return false;
3865 }
3866 }
3867
3868 void DescribeTo(ostream* os) const {
3869 *os << "are a pair where the first is half of the second";
3870 }
3871
3872 void DescribeNegationTo(ostream* os) const {
3873 *os << "are a pair where the first isn't half of the second";
3874 }
3875};
3876
3877PolymorphicMatcher<IsHalfOfMatcher> IsHalfOf() {
3878 return MakePolymorphicMatcher(IsHalfOfMatcher());
3879}
3880
3881TEST(PointwiseTest, DescribesSelf) {
3882 vector<int> rhs;
3883 rhs.push_back(1);
3884 rhs.push_back(2);
3885 rhs.push_back(3);
3886 const Matcher<const vector<int>&> m = Pointwise(IsHalfOf(), rhs);
3887 EXPECT_EQ("contains 3 values, where each value and its corresponding value "
3888 "in { 1, 2, 3 } are a pair where the first is half of the second",
3889 Describe(m));
3890 EXPECT_EQ("doesn't contain exactly 3 values, or contains a value x at some "
3891 "index i where x and the i-th value of { 1, 2, 3 } are a pair "
3892 "where the first isn't half of the second",
3893 DescribeNegation(m));
3894}
3895
3896TEST(PointwiseTest, MakesCopyOfRhs) {
3897 list<signed char> rhs;
3898 rhs.push_back(2);
3899 rhs.push_back(4);
3900
3901 int lhs[] = { 1, 2 };
3902 const Matcher<const int (&)[2]> m = Pointwise(IsHalfOf(), rhs);
3903 EXPECT_THAT(lhs, m);
3904
3905 // Changing rhs now shouldn't affect m, which made a copy of rhs.
3906 rhs.push_back(6);
3907 EXPECT_THAT(lhs, m);
3908}
3909
3910TEST(PointwiseTest, WorksForLhsNativeArray) {
3911 const int lhs[] = { 1, 2, 3 };
3912 vector<int> rhs;
3913 rhs.push_back(2);
3914 rhs.push_back(4);
3915 rhs.push_back(6);
3916 EXPECT_THAT(lhs, Pointwise(Lt(), rhs));
3917 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
3918}
3919
3920TEST(PointwiseTest, WorksForRhsNativeArray) {
3921 const int rhs[] = { 1, 2, 3 };
3922 vector<int> lhs;
3923 lhs.push_back(2);
3924 lhs.push_back(4);
3925 lhs.push_back(6);
3926 EXPECT_THAT(lhs, Pointwise(Gt(), rhs));
3927 EXPECT_THAT(lhs, Not(Pointwise(Lt(), rhs)));
3928}
3929
3930TEST(PointwiseTest, RejectsWrongSize) {
3931 const double lhs[2] = { 1, 2 };
3932 const int rhs[1] = { 0 };
3933 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
3934 EXPECT_EQ("which contains 2 values",
3935 Explain(Pointwise(Gt(), rhs), lhs));
3936
3937 const int rhs2[3] = { 0, 1, 2 };
3938 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs2)));
3939}
3940
3941TEST(PointwiseTest, RejectsWrongContent) {
3942 const double lhs[3] = { 1, 2, 3 };
3943 const int rhs[3] = { 2, 6, 4 };
3944 EXPECT_THAT(lhs, Not(Pointwise(IsHalfOf(), rhs)));
3945 EXPECT_EQ("where the value pair (2, 6) at index #1 don't match, "
3946 "where the second/2 is 3",
3947 Explain(Pointwise(IsHalfOf(), rhs), lhs));
3948}
3949
3950TEST(PointwiseTest, AcceptsCorrectContent) {
3951 const double lhs[3] = { 1, 2, 3 };
3952 const int rhs[3] = { 2, 4, 6 };
3953 EXPECT_THAT(lhs, Pointwise(IsHalfOf(), rhs));
3954 EXPECT_EQ("", Explain(Pointwise(IsHalfOf(), rhs), lhs));
3955}
3956
3957TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) {
3958 const double lhs[3] = { 1, 2, 3 };
3959 const int rhs[3] = { 2, 4, 6 };
3960 const Matcher<tuple<const double&, const int&> > m1 = IsHalfOf();
3961 EXPECT_THAT(lhs, Pointwise(m1, rhs));
3962 EXPECT_EQ("", Explain(Pointwise(m1, rhs), lhs));
3963
3964 // This type works as a tuple<const double&, const int&> can be
3965 // implicitly cast to tuple<double, int>.
3966 const Matcher<tuple<double, int> > m2 = IsHalfOf();
3967 EXPECT_THAT(lhs, Pointwise(m2, rhs));
3968 EXPECT_EQ("", Explain(Pointwise(m2, rhs), lhs));
3969}
3970
shiqiane35fdd92008-12-10 05:08:54 +00003971} // namespace gmock_matchers_test
3972} // namespace testing