blob: c4ed96ba7de0a33b23ead6736a7451a5266cfba6 [file] [log] [blame]
shiqiane35fdd92008-12-10 05:08:54 +00001// Copyright 2007, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29//
30// Author: wan@google.com (Zhanyong Wan)
31
32// Google Mock - a framework for writing C++ mock classes.
33//
34// This file tests some commonly used argument matchers.
35
zhanyong.wan53e08c42010-09-14 05:38:21 +000036#include "gmock/gmock-matchers.h"
shiqiane35fdd92008-12-10 05:08:54 +000037
38#include <string.h>
39#include <functional>
zhanyong.wana862f1d2010-03-15 21:23:04 +000040#include <iostream>
zhanyong.wan6a896b52009-01-16 01:13:50 +000041#include <list>
42#include <map>
43#include <set>
shiqiane35fdd92008-12-10 05:08:54 +000044#include <sstream>
zhanyong.wan6a896b52009-01-16 01:13:50 +000045#include <string>
zhanyong.wanf5e1ce52009-09-16 07:02:02 +000046#include <utility>
zhanyong.wan6a896b52009-01-16 01:13:50 +000047#include <vector>
zhanyong.wan53e08c42010-09-14 05:38:21 +000048#include "gmock/gmock.h"
49#include "gtest/gtest.h"
50#include "gtest/gtest-spi.h"
shiqiane35fdd92008-12-10 05:08:54 +000051
52namespace testing {
zhanyong.wan4a5330d2009-02-19 00:36:44 +000053
54namespace internal {
vladlosev587c1b32011-05-20 00:42:22 +000055GTEST_API_ string JoinAsTuple(const Strings& fields);
zhanyong.wan4a5330d2009-02-19 00:36:44 +000056} // namespace internal
57
shiqiane35fdd92008-12-10 05:08:54 +000058namespace gmock_matchers_test {
59
zhanyong.wan898725c2011-09-16 16:45:39 +000060using std::greater;
61using std::less;
zhanyong.wanab5b77c2010-05-17 19:32:48 +000062using std::list;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000063using std::make_pair;
zhanyong.wanb5937da2009-07-16 20:26:41 +000064using std::map;
65using std::multimap;
zhanyong.wanab5b77c2010-05-17 19:32:48 +000066using std::multiset;
67using std::ostream;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000068using std::pair;
zhanyong.wan33605ba2010-04-22 23:37:47 +000069using std::set;
shiqiane35fdd92008-12-10 05:08:54 +000070using std::stringstream;
zhanyong.wanab5b77c2010-05-17 19:32:48 +000071using std::tr1::get;
zhanyong.wanb8243162009-06-04 05:48:20 +000072using std::tr1::make_tuple;
zhanyong.wanab5b77c2010-05-17 19:32:48 +000073using std::tr1::tuple;
zhanyong.wan33605ba2010-04-22 23:37:47 +000074using std::vector;
shiqiane35fdd92008-12-10 05:08:54 +000075using testing::A;
zhanyong.wanbf550852009-06-09 06:09:53 +000076using testing::AllArgs;
shiqiane35fdd92008-12-10 05:08:54 +000077using testing::AllOf;
78using testing::An;
79using testing::AnyOf;
80using testing::ByRef;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000081using testing::ContainsRegex;
shiqiane35fdd92008-12-10 05:08:54 +000082using testing::DoubleEq;
83using testing::EndsWith;
84using testing::Eq;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000085using testing::ExplainMatchResult;
shiqiane35fdd92008-12-10 05:08:54 +000086using testing::Field;
87using testing::FloatEq;
88using testing::Ge;
89using testing::Gt;
90using testing::HasSubstr;
zhanyong.wan2d970ee2009-09-24 21:41:36 +000091using testing::IsNull;
zhanyong.wanb5937da2009-07-16 20:26:41 +000092using testing::Key;
shiqiane35fdd92008-12-10 05:08:54 +000093using testing::Le;
94using testing::Lt;
95using testing::MakeMatcher;
96using testing::MakePolymorphicMatcher;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000097using testing::MatchResultListener;
shiqiane35fdd92008-12-10 05:08:54 +000098using testing::Matcher;
99using testing::MatcherCast;
100using testing::MatcherInterface;
101using testing::Matches;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000102using testing::MatchesRegex;
shiqiane35fdd92008-12-10 05:08:54 +0000103using testing::NanSensitiveDoubleEq;
104using testing::NanSensitiveFloatEq;
105using testing::Ne;
106using testing::Not;
107using testing::NotNull;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000108using testing::Pair;
shiqiane35fdd92008-12-10 05:08:54 +0000109using testing::Pointee;
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000110using testing::Pointwise;
shiqiane35fdd92008-12-10 05:08:54 +0000111using testing::PolymorphicMatcher;
112using testing::Property;
113using testing::Ref;
114using testing::ResultOf;
115using testing::StartsWith;
116using testing::StrCaseEq;
117using testing::StrCaseNe;
118using testing::StrEq;
119using testing::StrNe;
120using testing::Truly;
121using testing::TypedEq;
zhanyong.wanb8243162009-06-04 05:48:20 +0000122using testing::Value;
zhanyong.wan898725c2011-09-16 16:45:39 +0000123using testing::WhenSorted;
124using testing::WhenSortedBy;
shiqiane35fdd92008-12-10 05:08:54 +0000125using testing::_;
zhanyong.wana862f1d2010-03-15 21:23:04 +0000126using testing::internal::DummyMatchResultListener;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000127using testing::internal::ExplainMatchFailureTupleTo;
shiqiane35fdd92008-12-10 05:08:54 +0000128using testing::internal::FloatingEqMatcher;
zhanyong.wanb4140802010-06-08 22:53:57 +0000129using testing::internal::FormatMatcherDescription;
zhanyong.wan736baa82010-09-27 17:44:16 +0000130using testing::internal::IsReadableTypeName;
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000131using testing::internal::JoinAsTuple;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000132using testing::internal::RE;
zhanyong.wana862f1d2010-03-15 21:23:04 +0000133using testing::internal::StreamMatchResultListener;
shiqiane35fdd92008-12-10 05:08:54 +0000134using testing::internal::String;
zhanyong.wan34b034c2010-03-05 21:23:23 +0000135using testing::internal::StringMatchResultListener;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000136using testing::internal::Strings;
vladlosev79b83502009-11-18 00:43:37 +0000137using testing::internal::linked_ptr;
vladloseve56daa72009-11-18 01:08:08 +0000138using testing::internal::scoped_ptr;
shiqiane35fdd92008-12-10 05:08:54 +0000139using testing::internal::string;
140
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000141// For testing ExplainMatchResultTo().
142class GreaterThanMatcher : public MatcherInterface<int> {
143 public:
144 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
145
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000146 virtual void DescribeTo(ostream* os) const {
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000147 *os << "is > " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000148 }
149
zhanyong.wandb22c222010-01-28 21:52:29 +0000150 virtual bool MatchAndExplain(int lhs,
151 MatchResultListener* listener) const {
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000152 const int diff = lhs - rhs_;
153 if (diff > 0) {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000154 *listener << "which is " << diff << " more than " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000155 } else if (diff == 0) {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000156 *listener << "which is the same as " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000157 } else {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000158 *listener << "which is " << -diff << " less than " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000159 }
zhanyong.wandb22c222010-01-28 21:52:29 +0000160
161 return lhs > rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000162 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000163
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000164 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000165 int rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000166};
167
168Matcher<int> GreaterThan(int n) {
169 return MakeMatcher(new GreaterThanMatcher(n));
170}
171
zhanyong.wan736baa82010-09-27 17:44:16 +0000172string OfType(const string& type_name) {
173#if GTEST_HAS_RTTI
174 return " (of type " + type_name + ")";
175#else
176 return "";
177#endif
178}
179
shiqiane35fdd92008-12-10 05:08:54 +0000180// Returns the description of the given matcher.
181template <typename T>
182string Describe(const Matcher<T>& m) {
183 stringstream ss;
184 m.DescribeTo(&ss);
185 return ss.str();
186}
187
188// Returns the description of the negation of the given matcher.
189template <typename T>
190string DescribeNegation(const Matcher<T>& m) {
191 stringstream ss;
192 m.DescribeNegationTo(&ss);
193 return ss.str();
194}
195
196// Returns the reason why x matches, or doesn't match, m.
197template <typename MatcherType, typename Value>
198string Explain(const MatcherType& m, const Value& x) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000199 StringMatchResultListener listener;
200 ExplainMatchResult(m, x, &listener);
201 return listener.str();
shiqiane35fdd92008-12-10 05:08:54 +0000202}
203
zhanyong.wana862f1d2010-03-15 21:23:04 +0000204TEST(MatchResultListenerTest, StreamingWorks) {
205 StringMatchResultListener listener;
206 listener << "hi" << 5;
207 EXPECT_EQ("hi5", listener.str());
208
209 // Streaming shouldn't crash when the underlying ostream is NULL.
210 DummyMatchResultListener dummy;
211 dummy << "hi" << 5;
212}
213
214TEST(MatchResultListenerTest, CanAccessUnderlyingStream) {
215 EXPECT_TRUE(DummyMatchResultListener().stream() == NULL);
216 EXPECT_TRUE(StreamMatchResultListener(NULL).stream() == NULL);
217
218 EXPECT_EQ(&std::cout, StreamMatchResultListener(&std::cout).stream());
219}
220
221TEST(MatchResultListenerTest, IsInterestedWorks) {
222 EXPECT_TRUE(StringMatchResultListener().IsInterested());
223 EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested());
224
225 EXPECT_FALSE(DummyMatchResultListener().IsInterested());
226 EXPECT_FALSE(StreamMatchResultListener(NULL).IsInterested());
227}
228
shiqiane35fdd92008-12-10 05:08:54 +0000229// Makes sure that the MatcherInterface<T> interface doesn't
230// change.
231class EvenMatcherImpl : public MatcherInterface<int> {
232 public:
zhanyong.wandb22c222010-01-28 21:52:29 +0000233 virtual bool MatchAndExplain(int x,
234 MatchResultListener* /* listener */) const {
235 return x % 2 == 0;
236 }
shiqiane35fdd92008-12-10 05:08:54 +0000237
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000238 virtual void DescribeTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +0000239 *os << "is an even number";
240 }
241
242 // We deliberately don't define DescribeNegationTo() and
243 // ExplainMatchResultTo() here, to make sure the definition of these
244 // two methods is optional.
245};
246
zhanyong.wana862f1d2010-03-15 21:23:04 +0000247// Makes sure that the MatcherInterface API doesn't change.
248TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) {
shiqiane35fdd92008-12-10 05:08:54 +0000249 EvenMatcherImpl m;
250}
251
zhanyong.wan82113312010-01-08 21:55:40 +0000252// Tests implementing a monomorphic matcher using MatchAndExplain().
253
254class NewEvenMatcherImpl : public MatcherInterface<int> {
255 public:
256 virtual bool MatchAndExplain(int x, MatchResultListener* listener) const {
257 const bool match = x % 2 == 0;
258 // Verifies that we can stream to a listener directly.
259 *listener << "value % " << 2;
260 if (listener->stream() != NULL) {
261 // Verifies that we can stream to a listener's underlying stream
262 // too.
263 *listener->stream() << " == " << (x % 2);
264 }
265 return match;
266 }
267
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000268 virtual void DescribeTo(ostream* os) const {
zhanyong.wan82113312010-01-08 21:55:40 +0000269 *os << "is an even number";
270 }
271};
272
273TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {
274 Matcher<int> m = MakeMatcher(new NewEvenMatcherImpl);
275 EXPECT_TRUE(m.Matches(2));
276 EXPECT_FALSE(m.Matches(3));
277 EXPECT_EQ("value % 2 == 0", Explain(m, 2));
278 EXPECT_EQ("value % 2 == 1", Explain(m, 3));
279}
280
shiqiane35fdd92008-12-10 05:08:54 +0000281// Tests default-constructing a matcher.
282TEST(MatcherTest, CanBeDefaultConstructed) {
283 Matcher<double> m;
284}
285
286// Tests that Matcher<T> can be constructed from a MatcherInterface<T>*.
287TEST(MatcherTest, CanBeConstructedFromMatcherInterface) {
288 const MatcherInterface<int>* impl = new EvenMatcherImpl;
289 Matcher<int> m(impl);
290 EXPECT_TRUE(m.Matches(4));
291 EXPECT_FALSE(m.Matches(5));
292}
293
294// Tests that value can be used in place of Eq(value).
295TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) {
296 Matcher<int> m1 = 5;
297 EXPECT_TRUE(m1.Matches(5));
298 EXPECT_FALSE(m1.Matches(6));
299}
300
301// Tests that NULL can be used in place of Eq(NULL).
302TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) {
303 Matcher<int*> m1 = NULL;
304 EXPECT_TRUE(m1.Matches(NULL));
305 int n = 0;
306 EXPECT_FALSE(m1.Matches(&n));
307}
308
309// Tests that matchers are copyable.
310TEST(MatcherTest, IsCopyable) {
311 // Tests the copy constructor.
312 Matcher<bool> m1 = Eq(false);
313 EXPECT_TRUE(m1.Matches(false));
314 EXPECT_FALSE(m1.Matches(true));
315
316 // Tests the assignment operator.
317 m1 = Eq(true);
318 EXPECT_TRUE(m1.Matches(true));
319 EXPECT_FALSE(m1.Matches(false));
320}
321
322// Tests that Matcher<T>::DescribeTo() calls
323// MatcherInterface<T>::DescribeTo().
324TEST(MatcherTest, CanDescribeItself) {
325 EXPECT_EQ("is an even number",
326 Describe(Matcher<int>(new EvenMatcherImpl)));
327}
328
zhanyong.wan82113312010-01-08 21:55:40 +0000329// Tests Matcher<T>::MatchAndExplain().
330TEST(MatcherTest, MatchAndExplain) {
331 Matcher<int> m = GreaterThan(0);
zhanyong.wan34b034c2010-03-05 21:23:23 +0000332 StringMatchResultListener listener1;
zhanyong.wan82113312010-01-08 21:55:40 +0000333 EXPECT_TRUE(m.MatchAndExplain(42, &listener1));
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000334 EXPECT_EQ("which is 42 more than 0", listener1.str());
zhanyong.wan82113312010-01-08 21:55:40 +0000335
zhanyong.wan34b034c2010-03-05 21:23:23 +0000336 StringMatchResultListener listener2;
zhanyong.wan82113312010-01-08 21:55:40 +0000337 EXPECT_FALSE(m.MatchAndExplain(-9, &listener2));
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000338 EXPECT_EQ("which is 9 less than 0", listener2.str());
zhanyong.wan82113312010-01-08 21:55:40 +0000339}
340
shiqiane35fdd92008-12-10 05:08:54 +0000341// Tests that a C-string literal can be implicitly converted to a
342// Matcher<string> or Matcher<const string&>.
343TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
344 Matcher<string> m1 = "hi";
345 EXPECT_TRUE(m1.Matches("hi"));
346 EXPECT_FALSE(m1.Matches("hello"));
347
348 Matcher<const string&> m2 = "hi";
349 EXPECT_TRUE(m2.Matches("hi"));
350 EXPECT_FALSE(m2.Matches("hello"));
351}
352
353// Tests that a string object can be implicitly converted to a
354// Matcher<string> or Matcher<const string&>.
355TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {
356 Matcher<string> m1 = string("hi");
357 EXPECT_TRUE(m1.Matches("hi"));
358 EXPECT_FALSE(m1.Matches("hello"));
359
360 Matcher<const string&> m2 = string("hi");
361 EXPECT_TRUE(m2.Matches("hi"));
362 EXPECT_FALSE(m2.Matches("hello"));
363}
364
365// Tests that MakeMatcher() constructs a Matcher<T> from a
366// MatcherInterface* without requiring the user to explicitly
367// write the type.
368TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) {
369 const MatcherInterface<int>* dummy_impl = NULL;
370 Matcher<int> m = MakeMatcher(dummy_impl);
371}
372
zhanyong.wan82113312010-01-08 21:55:40 +0000373// Tests that MakePolymorphicMatcher() can construct a polymorphic
374// matcher from its implementation using the old API.
zhanyong.wan33605ba2010-04-22 23:37:47 +0000375const int g_bar = 1;
shiqiane35fdd92008-12-10 05:08:54 +0000376class ReferencesBarOrIsZeroImpl {
377 public:
378 template <typename T>
zhanyong.wandb22c222010-01-28 21:52:29 +0000379 bool MatchAndExplain(const T& x,
380 MatchResultListener* /* listener */) const {
shiqiane35fdd92008-12-10 05:08:54 +0000381 const void* p = &x;
zhanyong.wan33605ba2010-04-22 23:37:47 +0000382 return p == &g_bar || x == 0;
shiqiane35fdd92008-12-10 05:08:54 +0000383 }
384
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000385 void DescribeTo(ostream* os) const { *os << "g_bar or zero"; }
shiqiane35fdd92008-12-10 05:08:54 +0000386
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000387 void DescribeNegationTo(ostream* os) const {
zhanyong.wan33605ba2010-04-22 23:37:47 +0000388 *os << "doesn't reference g_bar and is not zero";
shiqiane35fdd92008-12-10 05:08:54 +0000389 }
390};
391
392// This function verifies that MakePolymorphicMatcher() returns a
393// PolymorphicMatcher<T> where T is the argument's type.
394PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() {
395 return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl());
396}
397
zhanyong.wan82113312010-01-08 21:55:40 +0000398TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) {
shiqiane35fdd92008-12-10 05:08:54 +0000399 // Using a polymorphic matcher to match a reference type.
400 Matcher<const int&> m1 = ReferencesBarOrIsZero();
401 EXPECT_TRUE(m1.Matches(0));
402 // Verifies that the identity of a by-reference argument is preserved.
zhanyong.wan33605ba2010-04-22 23:37:47 +0000403 EXPECT_TRUE(m1.Matches(g_bar));
shiqiane35fdd92008-12-10 05:08:54 +0000404 EXPECT_FALSE(m1.Matches(1));
zhanyong.wan33605ba2010-04-22 23:37:47 +0000405 EXPECT_EQ("g_bar or zero", Describe(m1));
shiqiane35fdd92008-12-10 05:08:54 +0000406
407 // Using a polymorphic matcher to match a value type.
408 Matcher<double> m2 = ReferencesBarOrIsZero();
409 EXPECT_TRUE(m2.Matches(0.0));
410 EXPECT_FALSE(m2.Matches(0.1));
zhanyong.wan33605ba2010-04-22 23:37:47 +0000411 EXPECT_EQ("g_bar or zero", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +0000412}
413
zhanyong.wan82113312010-01-08 21:55:40 +0000414// Tests implementing a polymorphic matcher using MatchAndExplain().
415
416class PolymorphicIsEvenImpl {
417 public:
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000418 void DescribeTo(ostream* os) const { *os << "is even"; }
zhanyong.wan82113312010-01-08 21:55:40 +0000419
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000420 void DescribeNegationTo(ostream* os) const {
zhanyong.wan82113312010-01-08 21:55:40 +0000421 *os << "is odd";
422 }
zhanyong.wan82113312010-01-08 21:55:40 +0000423
zhanyong.wandb22c222010-01-28 21:52:29 +0000424 template <typename T>
425 bool MatchAndExplain(const T& x, MatchResultListener* listener) const {
426 // Verifies that we can stream to the listener directly.
427 *listener << "% " << 2;
428 if (listener->stream() != NULL) {
429 // Verifies that we can stream to the listener's underlying stream
430 // too.
431 *listener->stream() << " == " << (x % 2);
432 }
433 return (x % 2) == 0;
zhanyong.wan82113312010-01-08 21:55:40 +0000434 }
zhanyong.wandb22c222010-01-28 21:52:29 +0000435};
zhanyong.wan82113312010-01-08 21:55:40 +0000436
437PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() {
438 return MakePolymorphicMatcher(PolymorphicIsEvenImpl());
439}
440
441TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {
442 // Using PolymorphicIsEven() as a Matcher<int>.
443 const Matcher<int> m1 = PolymorphicIsEven();
444 EXPECT_TRUE(m1.Matches(42));
445 EXPECT_FALSE(m1.Matches(43));
446 EXPECT_EQ("is even", Describe(m1));
447
448 const Matcher<int> not_m1 = Not(m1);
449 EXPECT_EQ("is odd", Describe(not_m1));
450
451 EXPECT_EQ("% 2 == 0", Explain(m1, 42));
452
453 // Using PolymorphicIsEven() as a Matcher<char>.
454 const Matcher<char> m2 = PolymorphicIsEven();
455 EXPECT_TRUE(m2.Matches('\x42'));
456 EXPECT_FALSE(m2.Matches('\x43'));
457 EXPECT_EQ("is even", Describe(m2));
458
459 const Matcher<char> not_m2 = Not(m2);
460 EXPECT_EQ("is odd", Describe(not_m2));
461
462 EXPECT_EQ("% 2 == 0", Explain(m2, '\x42'));
463}
464
shiqiane35fdd92008-12-10 05:08:54 +0000465// Tests that MatcherCast<T>(m) works when m is a polymorphic matcher.
466TEST(MatcherCastTest, FromPolymorphicMatcher) {
467 Matcher<int> m = MatcherCast<int>(Eq(5));
468 EXPECT_TRUE(m.Matches(5));
469 EXPECT_FALSE(m.Matches(6));
470}
471
472// For testing casting matchers between compatible types.
473class IntValue {
474 public:
475 // An int can be statically (although not implicitly) cast to a
476 // IntValue.
zhanyong.wan32de5f52009-12-23 00:13:23 +0000477 explicit IntValue(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +0000478
479 int value() const { return value_; }
480 private:
481 int value_;
482};
483
484// For testing casting matchers between compatible types.
485bool IsPositiveIntValue(const IntValue& foo) {
486 return foo.value() > 0;
487}
488
489// Tests that MatcherCast<T>(m) works when m is a Matcher<U> where T
490// can be statically converted to U.
491TEST(MatcherCastTest, FromCompatibleType) {
492 Matcher<double> m1 = Eq(2.0);
493 Matcher<int> m2 = MatcherCast<int>(m1);
494 EXPECT_TRUE(m2.Matches(2));
495 EXPECT_FALSE(m2.Matches(3));
496
497 Matcher<IntValue> m3 = Truly(IsPositiveIntValue);
498 Matcher<int> m4 = MatcherCast<int>(m3);
499 // In the following, the arguments 1 and 0 are statically converted
500 // to IntValue objects, and then tested by the IsPositiveIntValue()
501 // predicate.
502 EXPECT_TRUE(m4.Matches(1));
503 EXPECT_FALSE(m4.Matches(0));
504}
505
506// Tests that MatcherCast<T>(m) works when m is a Matcher<const T&>.
507TEST(MatcherCastTest, FromConstReferenceToNonReference) {
508 Matcher<const int&> m1 = Eq(0);
509 Matcher<int> m2 = MatcherCast<int>(m1);
510 EXPECT_TRUE(m2.Matches(0));
511 EXPECT_FALSE(m2.Matches(1));
512}
513
514// Tests that MatcherCast<T>(m) works when m is a Matcher<T&>.
515TEST(MatcherCastTest, FromReferenceToNonReference) {
516 Matcher<int&> m1 = Eq(0);
517 Matcher<int> m2 = MatcherCast<int>(m1);
518 EXPECT_TRUE(m2.Matches(0));
519 EXPECT_FALSE(m2.Matches(1));
520}
521
522// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
523TEST(MatcherCastTest, FromNonReferenceToConstReference) {
524 Matcher<int> m1 = Eq(0);
525 Matcher<const int&> m2 = MatcherCast<const int&>(m1);
526 EXPECT_TRUE(m2.Matches(0));
527 EXPECT_FALSE(m2.Matches(1));
528}
529
530// Tests that MatcherCast<T&>(m) works when m is a Matcher<T>.
531TEST(MatcherCastTest, FromNonReferenceToReference) {
532 Matcher<int> m1 = Eq(0);
533 Matcher<int&> m2 = MatcherCast<int&>(m1);
534 int n = 0;
535 EXPECT_TRUE(m2.Matches(n));
536 n = 1;
537 EXPECT_FALSE(m2.Matches(n));
538}
539
540// Tests that MatcherCast<T>(m) works when m is a Matcher<T>.
541TEST(MatcherCastTest, FromSameType) {
542 Matcher<int> m1 = Eq(0);
543 Matcher<int> m2 = MatcherCast<int>(m1);
544 EXPECT_TRUE(m2.Matches(0));
545 EXPECT_FALSE(m2.Matches(1));
546}
547
zhanyong.wan18490652009-05-11 18:54:08 +0000548class Base {};
549class Derived : public Base {};
550
551// Tests that SafeMatcherCast<T>(m) works when m is a polymorphic matcher.
552TEST(SafeMatcherCastTest, FromPolymorphicMatcher) {
553 Matcher<char> m2 = SafeMatcherCast<char>(Eq(32));
554 EXPECT_TRUE(m2.Matches(' '));
555 EXPECT_FALSE(m2.Matches('\n'));
556}
557
zhanyong.wan16cf4732009-05-14 20:55:30 +0000558// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where
559// T and U are arithmetic types and T can be losslessly converted to
560// U.
561TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) {
zhanyong.wan18490652009-05-11 18:54:08 +0000562 Matcher<double> m1 = DoubleEq(1.0);
zhanyong.wan16cf4732009-05-14 20:55:30 +0000563 Matcher<float> m2 = SafeMatcherCast<float>(m1);
564 EXPECT_TRUE(m2.Matches(1.0f));
565 EXPECT_FALSE(m2.Matches(2.0f));
566
567 Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>('a'));
568 EXPECT_TRUE(m3.Matches('a'));
569 EXPECT_FALSE(m3.Matches('b'));
zhanyong.wan18490652009-05-11 18:54:08 +0000570}
571
572// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where T and U
573// are pointers or references to a derived and a base class, correspondingly.
574TEST(SafeMatcherCastTest, FromBaseClass) {
575 Derived d, d2;
576 Matcher<Base*> m1 = Eq(&d);
577 Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1);
578 EXPECT_TRUE(m2.Matches(&d));
579 EXPECT_FALSE(m2.Matches(&d2));
580
581 Matcher<Base&> m3 = Ref(d);
582 Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3);
583 EXPECT_TRUE(m4.Matches(d));
584 EXPECT_FALSE(m4.Matches(d2));
585}
586
587// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<const T&>.
588TEST(SafeMatcherCastTest, FromConstReferenceToReference) {
589 int n = 0;
590 Matcher<const int&> m1 = Ref(n);
591 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
592 int n1 = 0;
593 EXPECT_TRUE(m2.Matches(n));
594 EXPECT_FALSE(m2.Matches(n1));
595}
596
597// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
598TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) {
599 Matcher<int> m1 = Eq(0);
600 Matcher<const int&> m2 = SafeMatcherCast<const int&>(m1);
601 EXPECT_TRUE(m2.Matches(0));
602 EXPECT_FALSE(m2.Matches(1));
603}
604
605// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<T>.
606TEST(SafeMatcherCastTest, FromNonReferenceToReference) {
607 Matcher<int> m1 = Eq(0);
608 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
609 int n = 0;
610 EXPECT_TRUE(m2.Matches(n));
611 n = 1;
612 EXPECT_FALSE(m2.Matches(n));
613}
614
615// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<T>.
616TEST(SafeMatcherCastTest, FromSameType) {
617 Matcher<int> m1 = Eq(0);
618 Matcher<int> m2 = SafeMatcherCast<int>(m1);
619 EXPECT_TRUE(m2.Matches(0));
620 EXPECT_FALSE(m2.Matches(1));
621}
622
shiqiane35fdd92008-12-10 05:08:54 +0000623// Tests that A<T>() matches any value of type T.
624TEST(ATest, MatchesAnyValue) {
625 // Tests a matcher for a value type.
626 Matcher<double> m1 = A<double>();
627 EXPECT_TRUE(m1.Matches(91.43));
628 EXPECT_TRUE(m1.Matches(-15.32));
629
630 // Tests a matcher for a reference type.
631 int a = 2;
632 int b = -6;
633 Matcher<int&> m2 = A<int&>();
634 EXPECT_TRUE(m2.Matches(a));
635 EXPECT_TRUE(m2.Matches(b));
636}
637
638// Tests that A<T>() describes itself properly.
639TEST(ATest, CanDescribeSelf) {
640 EXPECT_EQ("is anything", Describe(A<bool>()));
641}
642
643// Tests that An<T>() matches any value of type T.
644TEST(AnTest, MatchesAnyValue) {
645 // Tests a matcher for a value type.
646 Matcher<int> m1 = An<int>();
647 EXPECT_TRUE(m1.Matches(9143));
648 EXPECT_TRUE(m1.Matches(-1532));
649
650 // Tests a matcher for a reference type.
651 int a = 2;
652 int b = -6;
653 Matcher<int&> m2 = An<int&>();
654 EXPECT_TRUE(m2.Matches(a));
655 EXPECT_TRUE(m2.Matches(b));
656}
657
658// Tests that An<T>() describes itself properly.
659TEST(AnTest, CanDescribeSelf) {
660 EXPECT_EQ("is anything", Describe(An<int>()));
661}
662
663// Tests that _ can be used as a matcher for any type and matches any
664// value of that type.
665TEST(UnderscoreTest, MatchesAnyValue) {
666 // Uses _ as a matcher for a value type.
667 Matcher<int> m1 = _;
668 EXPECT_TRUE(m1.Matches(123));
669 EXPECT_TRUE(m1.Matches(-242));
670
671 // Uses _ as a matcher for a reference type.
672 bool a = false;
673 const bool b = true;
674 Matcher<const bool&> m2 = _;
675 EXPECT_TRUE(m2.Matches(a));
676 EXPECT_TRUE(m2.Matches(b));
677}
678
679// Tests that _ describes itself properly.
680TEST(UnderscoreTest, CanDescribeSelf) {
681 Matcher<int> m = _;
682 EXPECT_EQ("is anything", Describe(m));
683}
684
685// Tests that Eq(x) matches any value equal to x.
686TEST(EqTest, MatchesEqualValue) {
687 // 2 C-strings with same content but different addresses.
688 const char a1[] = "hi";
689 const char a2[] = "hi";
690
691 Matcher<const char*> m1 = Eq(a1);
692 EXPECT_TRUE(m1.Matches(a1));
693 EXPECT_FALSE(m1.Matches(a2));
694}
695
696// Tests that Eq(v) describes itself properly.
697
698class Unprintable {
699 public:
700 Unprintable() : c_('a') {}
701
zhanyong.wan32de5f52009-12-23 00:13:23 +0000702 bool operator==(const Unprintable& /* rhs */) { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000703 private:
704 char c_;
705};
706
707TEST(EqTest, CanDescribeSelf) {
708 Matcher<Unprintable> m = Eq(Unprintable());
709 EXPECT_EQ("is equal to 1-byte object <61>", Describe(m));
710}
711
712// Tests that Eq(v) can be used to match any type that supports
713// comparing with type T, where T is v's type.
714TEST(EqTest, IsPolymorphic) {
715 Matcher<int> m1 = Eq(1);
716 EXPECT_TRUE(m1.Matches(1));
717 EXPECT_FALSE(m1.Matches(2));
718
719 Matcher<char> m2 = Eq(1);
720 EXPECT_TRUE(m2.Matches('\1'));
721 EXPECT_FALSE(m2.Matches('a'));
722}
723
724// Tests that TypedEq<T>(v) matches values of type T that's equal to v.
725TEST(TypedEqTest, ChecksEqualityForGivenType) {
726 Matcher<char> m1 = TypedEq<char>('a');
727 EXPECT_TRUE(m1.Matches('a'));
728 EXPECT_FALSE(m1.Matches('b'));
729
730 Matcher<int> m2 = TypedEq<int>(6);
731 EXPECT_TRUE(m2.Matches(6));
732 EXPECT_FALSE(m2.Matches(7));
733}
734
735// Tests that TypedEq(v) describes itself properly.
736TEST(TypedEqTest, CanDescribeSelf) {
737 EXPECT_EQ("is equal to 2", Describe(TypedEq<int>(2)));
738}
739
740// Tests that TypedEq<T>(v) has type Matcher<T>.
741
742// Type<T>::IsTypeOf(v) compiles iff the type of value v is T, where T
743// is a "bare" type (i.e. not in the form of const U or U&). If v's
744// type is not T, the compiler will generate a message about
745// "undefined referece".
746template <typename T>
747struct Type {
zhanyong.wan32de5f52009-12-23 00:13:23 +0000748 static bool IsTypeOf(const T& /* v */) { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000749
750 template <typename T2>
751 static void IsTypeOf(T2 v);
752};
753
754TEST(TypedEqTest, HasSpecifiedType) {
755 // Verfies that the type of TypedEq<T>(v) is Matcher<T>.
756 Type<Matcher<int> >::IsTypeOf(TypedEq<int>(5));
757 Type<Matcher<double> >::IsTypeOf(TypedEq<double>(5));
758}
759
760// Tests that Ge(v) matches anything >= v.
761TEST(GeTest, ImplementsGreaterThanOrEqual) {
762 Matcher<int> m1 = Ge(0);
763 EXPECT_TRUE(m1.Matches(1));
764 EXPECT_TRUE(m1.Matches(0));
765 EXPECT_FALSE(m1.Matches(-1));
766}
767
768// Tests that Ge(v) describes itself properly.
769TEST(GeTest, CanDescribeSelf) {
770 Matcher<int> m = Ge(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000771 EXPECT_EQ("is >= 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000772}
773
774// Tests that Gt(v) matches anything > v.
775TEST(GtTest, ImplementsGreaterThan) {
776 Matcher<double> m1 = Gt(0);
777 EXPECT_TRUE(m1.Matches(1.0));
778 EXPECT_FALSE(m1.Matches(0.0));
779 EXPECT_FALSE(m1.Matches(-1.0));
780}
781
782// Tests that Gt(v) describes itself properly.
783TEST(GtTest, CanDescribeSelf) {
784 Matcher<int> m = Gt(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000785 EXPECT_EQ("is > 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000786}
787
788// Tests that Le(v) matches anything <= v.
789TEST(LeTest, ImplementsLessThanOrEqual) {
790 Matcher<char> m1 = Le('b');
791 EXPECT_TRUE(m1.Matches('a'));
792 EXPECT_TRUE(m1.Matches('b'));
793 EXPECT_FALSE(m1.Matches('c'));
794}
795
796// Tests that Le(v) describes itself properly.
797TEST(LeTest, CanDescribeSelf) {
798 Matcher<int> m = Le(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000799 EXPECT_EQ("is <= 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000800}
801
802// Tests that Lt(v) matches anything < v.
803TEST(LtTest, ImplementsLessThan) {
804 Matcher<const string&> m1 = Lt("Hello");
805 EXPECT_TRUE(m1.Matches("Abc"));
806 EXPECT_FALSE(m1.Matches("Hello"));
807 EXPECT_FALSE(m1.Matches("Hello, world!"));
808}
809
810// Tests that Lt(v) describes itself properly.
811TEST(LtTest, CanDescribeSelf) {
812 Matcher<int> m = Lt(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000813 EXPECT_EQ("is < 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000814}
815
816// Tests that Ne(v) matches anything != v.
817TEST(NeTest, ImplementsNotEqual) {
818 Matcher<int> m1 = Ne(0);
819 EXPECT_TRUE(m1.Matches(1));
820 EXPECT_TRUE(m1.Matches(-1));
821 EXPECT_FALSE(m1.Matches(0));
822}
823
824// Tests that Ne(v) describes itself properly.
825TEST(NeTest, CanDescribeSelf) {
826 Matcher<int> m = Ne(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000827 EXPECT_EQ("isn't equal to 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000828}
829
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000830// Tests that IsNull() matches any NULL pointer of any type.
831TEST(IsNullTest, MatchesNullPointer) {
832 Matcher<int*> m1 = IsNull();
833 int* p1 = NULL;
834 int n = 0;
835 EXPECT_TRUE(m1.Matches(p1));
836 EXPECT_FALSE(m1.Matches(&n));
837
838 Matcher<const char*> m2 = IsNull();
839 const char* p2 = NULL;
840 EXPECT_TRUE(m2.Matches(p2));
841 EXPECT_FALSE(m2.Matches("hi"));
842
zhanyong.wan95b12332009-09-25 18:55:50 +0000843#if !GTEST_OS_SYMBIAN
844 // Nokia's Symbian compiler generates:
845 // gmock-matchers.h: ambiguous access to overloaded function
846 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(void *)'
847 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(const testing::
848 // MatcherInterface<void *> *)'
849 // gmock-matchers.h: (point of instantiation: 'testing::
850 // gmock_matchers_test::IsNullTest_MatchesNullPointer_Test::TestBody()')
851 // gmock-matchers.h: (instantiating: 'testing::PolymorphicMatc
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000852 Matcher<void*> m3 = IsNull();
853 void* p3 = NULL;
854 EXPECT_TRUE(m3.Matches(p3));
855 EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef)));
zhanyong.wan95b12332009-09-25 18:55:50 +0000856#endif
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000857}
858
vladlosev79b83502009-11-18 00:43:37 +0000859TEST(IsNullTest, LinkedPtr) {
860 const Matcher<linked_ptr<int> > m = IsNull();
861 const linked_ptr<int> null_p;
862 const linked_ptr<int> non_null_p(new int);
863
864 EXPECT_TRUE(m.Matches(null_p));
865 EXPECT_FALSE(m.Matches(non_null_p));
866}
867
868TEST(IsNullTest, ReferenceToConstLinkedPtr) {
869 const Matcher<const linked_ptr<double>&> m = IsNull();
870 const linked_ptr<double> null_p;
871 const linked_ptr<double> non_null_p(new double);
872
873 EXPECT_TRUE(m.Matches(null_p));
874 EXPECT_FALSE(m.Matches(non_null_p));
875}
876
vladloseve56daa72009-11-18 01:08:08 +0000877TEST(IsNullTest, ReferenceToConstScopedPtr) {
878 const Matcher<const scoped_ptr<double>&> m = IsNull();
879 const scoped_ptr<double> null_p;
880 const scoped_ptr<double> non_null_p(new double);
881
882 EXPECT_TRUE(m.Matches(null_p));
883 EXPECT_FALSE(m.Matches(non_null_p));
884}
885
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000886// Tests that IsNull() describes itself properly.
887TEST(IsNullTest, CanDescribeSelf) {
888 Matcher<int*> m = IsNull();
889 EXPECT_EQ("is NULL", Describe(m));
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000890 EXPECT_EQ("isn't NULL", DescribeNegation(m));
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000891}
892
shiqiane35fdd92008-12-10 05:08:54 +0000893// Tests that NotNull() matches any non-NULL pointer of any type.
894TEST(NotNullTest, MatchesNonNullPointer) {
895 Matcher<int*> m1 = NotNull();
896 int* p1 = NULL;
897 int n = 0;
898 EXPECT_FALSE(m1.Matches(p1));
899 EXPECT_TRUE(m1.Matches(&n));
900
901 Matcher<const char*> m2 = NotNull();
902 const char* p2 = NULL;
903 EXPECT_FALSE(m2.Matches(p2));
904 EXPECT_TRUE(m2.Matches("hi"));
905}
906
vladlosev79b83502009-11-18 00:43:37 +0000907TEST(NotNullTest, LinkedPtr) {
908 const Matcher<linked_ptr<int> > m = NotNull();
909 const linked_ptr<int> null_p;
910 const linked_ptr<int> non_null_p(new int);
911
912 EXPECT_FALSE(m.Matches(null_p));
913 EXPECT_TRUE(m.Matches(non_null_p));
914}
915
916TEST(NotNullTest, ReferenceToConstLinkedPtr) {
917 const Matcher<const linked_ptr<double>&> m = NotNull();
918 const linked_ptr<double> null_p;
919 const linked_ptr<double> non_null_p(new double);
920
921 EXPECT_FALSE(m.Matches(null_p));
922 EXPECT_TRUE(m.Matches(non_null_p));
923}
924
vladloseve56daa72009-11-18 01:08:08 +0000925TEST(NotNullTest, ReferenceToConstScopedPtr) {
926 const Matcher<const scoped_ptr<double>&> m = NotNull();
927 const scoped_ptr<double> null_p;
928 const scoped_ptr<double> non_null_p(new double);
929
930 EXPECT_FALSE(m.Matches(null_p));
931 EXPECT_TRUE(m.Matches(non_null_p));
932}
933
shiqiane35fdd92008-12-10 05:08:54 +0000934// Tests that NotNull() describes itself properly.
935TEST(NotNullTest, CanDescribeSelf) {
936 Matcher<int*> m = NotNull();
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000937 EXPECT_EQ("isn't NULL", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000938}
939
940// Tests that Ref(variable) matches an argument that references
941// 'variable'.
942TEST(RefTest, MatchesSameVariable) {
943 int a = 0;
944 int b = 0;
945 Matcher<int&> m = Ref(a);
946 EXPECT_TRUE(m.Matches(a));
947 EXPECT_FALSE(m.Matches(b));
948}
949
950// Tests that Ref(variable) describes itself properly.
951TEST(RefTest, CanDescribeSelf) {
952 int n = 5;
953 Matcher<int&> m = Ref(n);
954 stringstream ss;
955 ss << "references the variable @" << &n << " 5";
956 EXPECT_EQ(string(ss.str()), Describe(m));
957}
958
959// Test that Ref(non_const_varialbe) can be used as a matcher for a
960// const reference.
961TEST(RefTest, CanBeUsedAsMatcherForConstReference) {
962 int a = 0;
963 int b = 0;
964 Matcher<const int&> m = Ref(a);
965 EXPECT_TRUE(m.Matches(a));
966 EXPECT_FALSE(m.Matches(b));
967}
968
969// Tests that Ref(variable) is covariant, i.e. Ref(derived) can be
970// used wherever Ref(base) can be used (Ref(derived) is a sub-type
971// of Ref(base), but not vice versa.
972
shiqiane35fdd92008-12-10 05:08:54 +0000973TEST(RefTest, IsCovariant) {
974 Base base, base2;
975 Derived derived;
976 Matcher<const Base&> m1 = Ref(base);
977 EXPECT_TRUE(m1.Matches(base));
978 EXPECT_FALSE(m1.Matches(base2));
979 EXPECT_FALSE(m1.Matches(derived));
980
981 m1 = Ref(derived);
982 EXPECT_TRUE(m1.Matches(derived));
983 EXPECT_FALSE(m1.Matches(base));
984 EXPECT_FALSE(m1.Matches(base2));
985}
986
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000987TEST(RefTest, ExplainsResult) {
988 int n = 0;
989 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), n),
990 StartsWith("which is located @"));
991
992 int m = 0;
993 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), m),
994 StartsWith("which is located @"));
995}
996
shiqiane35fdd92008-12-10 05:08:54 +0000997// Tests string comparison matchers.
998
999TEST(StrEqTest, MatchesEqualString) {
1000 Matcher<const char*> m = StrEq(string("Hello"));
1001 EXPECT_TRUE(m.Matches("Hello"));
1002 EXPECT_FALSE(m.Matches("hello"));
1003 EXPECT_FALSE(m.Matches(NULL));
1004
1005 Matcher<const string&> m2 = StrEq("Hello");
1006 EXPECT_TRUE(m2.Matches("Hello"));
1007 EXPECT_FALSE(m2.Matches("Hi"));
1008}
1009
1010TEST(StrEqTest, CanDescribeSelf) {
vladlosevaa432202011-04-01 21:58:42 +00001011 Matcher<string> m = StrEq("Hi-\'\"?\\\a\b\f\n\r\t\v\xD3");
1012 EXPECT_EQ("is equal to \"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",
shiqiane35fdd92008-12-10 05:08:54 +00001013 Describe(m));
1014
1015 string str("01204500800");
1016 str[3] = '\0';
1017 Matcher<string> m2 = StrEq(str);
1018 EXPECT_EQ("is equal to \"012\\04500800\"", Describe(m2));
1019 str[0] = str[6] = str[7] = str[9] = str[10] = '\0';
1020 Matcher<string> m3 = StrEq(str);
1021 EXPECT_EQ("is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3));
1022}
1023
1024TEST(StrNeTest, MatchesUnequalString) {
1025 Matcher<const char*> m = StrNe("Hello");
1026 EXPECT_TRUE(m.Matches(""));
1027 EXPECT_TRUE(m.Matches(NULL));
1028 EXPECT_FALSE(m.Matches("Hello"));
1029
1030 Matcher<string> m2 = StrNe(string("Hello"));
1031 EXPECT_TRUE(m2.Matches("hello"));
1032 EXPECT_FALSE(m2.Matches("Hello"));
1033}
1034
1035TEST(StrNeTest, CanDescribeSelf) {
1036 Matcher<const char*> m = StrNe("Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001037 EXPECT_EQ("isn't equal to \"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001038}
1039
1040TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
1041 Matcher<const char*> m = StrCaseEq(string("Hello"));
1042 EXPECT_TRUE(m.Matches("Hello"));
1043 EXPECT_TRUE(m.Matches("hello"));
1044 EXPECT_FALSE(m.Matches("Hi"));
1045 EXPECT_FALSE(m.Matches(NULL));
1046
1047 Matcher<const string&> m2 = StrCaseEq("Hello");
1048 EXPECT_TRUE(m2.Matches("hello"));
1049 EXPECT_FALSE(m2.Matches("Hi"));
1050}
1051
1052TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1053 string str1("oabocdooeoo");
1054 string str2("OABOCDOOEOO");
1055 Matcher<const string&> m0 = StrCaseEq(str1);
1056 EXPECT_FALSE(m0.Matches(str2 + string(1, '\0')));
1057
1058 str1[3] = str2[3] = '\0';
1059 Matcher<const string&> m1 = StrCaseEq(str1);
1060 EXPECT_TRUE(m1.Matches(str2));
1061
1062 str1[0] = str1[6] = str1[7] = str1[10] = '\0';
1063 str2[0] = str2[6] = str2[7] = str2[10] = '\0';
1064 Matcher<const string&> m2 = StrCaseEq(str1);
1065 str1[9] = str2[9] = '\0';
1066 EXPECT_FALSE(m2.Matches(str2));
1067
1068 Matcher<const string&> m3 = StrCaseEq(str1);
1069 EXPECT_TRUE(m3.Matches(str2));
1070
1071 EXPECT_FALSE(m3.Matches(str2 + "x"));
1072 str2.append(1, '\0');
1073 EXPECT_FALSE(m3.Matches(str2));
1074 EXPECT_FALSE(m3.Matches(string(str2, 0, 9)));
1075}
1076
1077TEST(StrCaseEqTest, CanDescribeSelf) {
1078 Matcher<string> m = StrCaseEq("Hi");
1079 EXPECT_EQ("is equal to (ignoring case) \"Hi\"", Describe(m));
1080}
1081
1082TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1083 Matcher<const char*> m = StrCaseNe("Hello");
1084 EXPECT_TRUE(m.Matches("Hi"));
1085 EXPECT_TRUE(m.Matches(NULL));
1086 EXPECT_FALSE(m.Matches("Hello"));
1087 EXPECT_FALSE(m.Matches("hello"));
1088
1089 Matcher<string> m2 = StrCaseNe(string("Hello"));
1090 EXPECT_TRUE(m2.Matches(""));
1091 EXPECT_FALSE(m2.Matches("Hello"));
1092}
1093
1094TEST(StrCaseNeTest, CanDescribeSelf) {
1095 Matcher<const char*> m = StrCaseNe("Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001096 EXPECT_EQ("isn't equal to (ignoring case) \"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001097}
1098
1099// Tests that HasSubstr() works for matching string-typed values.
1100TEST(HasSubstrTest, WorksForStringClasses) {
1101 const Matcher<string> m1 = HasSubstr("foo");
1102 EXPECT_TRUE(m1.Matches(string("I love food.")));
1103 EXPECT_FALSE(m1.Matches(string("tofo")));
1104
1105 const Matcher<const std::string&> m2 = HasSubstr("foo");
1106 EXPECT_TRUE(m2.Matches(std::string("I love food.")));
1107 EXPECT_FALSE(m2.Matches(std::string("tofo")));
1108}
1109
1110// Tests that HasSubstr() works for matching C-string-typed values.
1111TEST(HasSubstrTest, WorksForCStrings) {
1112 const Matcher<char*> m1 = HasSubstr("foo");
1113 EXPECT_TRUE(m1.Matches(const_cast<char*>("I love food.")));
1114 EXPECT_FALSE(m1.Matches(const_cast<char*>("tofo")));
1115 EXPECT_FALSE(m1.Matches(NULL));
1116
1117 const Matcher<const char*> m2 = HasSubstr("foo");
1118 EXPECT_TRUE(m2.Matches("I love food."));
1119 EXPECT_FALSE(m2.Matches("tofo"));
1120 EXPECT_FALSE(m2.Matches(NULL));
1121}
1122
1123// Tests that HasSubstr(s) describes itself properly.
1124TEST(HasSubstrTest, CanDescribeSelf) {
1125 Matcher<string> m = HasSubstr("foo\n\"");
1126 EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m));
1127}
1128
zhanyong.wanb5937da2009-07-16 20:26:41 +00001129TEST(KeyTest, CanDescribeSelf) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001130 Matcher<const pair<std::string, int>&> m = Key("foo");
zhanyong.wanb5937da2009-07-16 20:26:41 +00001131 EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001132 EXPECT_EQ("doesn't have a key that is equal to \"foo\"", DescribeNegation(m));
1133}
1134
1135TEST(KeyTest, ExplainsResult) {
1136 Matcher<pair<int, bool> > m = Key(GreaterThan(10));
1137 EXPECT_EQ("whose first field is a value which is 5 less than 10",
1138 Explain(m, make_pair(5, true)));
1139 EXPECT_EQ("whose first field is a value which is 5 more than 10",
1140 Explain(m, make_pair(15, true)));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001141}
1142
1143TEST(KeyTest, MatchesCorrectly) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001144 pair<int, std::string> p(25, "foo");
zhanyong.wanb5937da2009-07-16 20:26:41 +00001145 EXPECT_THAT(p, Key(25));
1146 EXPECT_THAT(p, Not(Key(42)));
1147 EXPECT_THAT(p, Key(Ge(20)));
1148 EXPECT_THAT(p, Not(Key(Lt(25))));
1149}
1150
1151TEST(KeyTest, SafelyCastsInnerMatcher) {
1152 Matcher<int> is_positive = Gt(0);
1153 Matcher<int> is_negative = Lt(0);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001154 pair<char, bool> p('a', true);
zhanyong.wanb5937da2009-07-16 20:26:41 +00001155 EXPECT_THAT(p, Key(is_positive));
1156 EXPECT_THAT(p, Not(Key(is_negative)));
1157}
1158
1159TEST(KeyTest, InsideContainsUsingMap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001160 map<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001161 container.insert(make_pair(1, 'a'));
1162 container.insert(make_pair(2, 'b'));
1163 container.insert(make_pair(4, 'c'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001164 EXPECT_THAT(container, Contains(Key(1)));
1165 EXPECT_THAT(container, Not(Contains(Key(3))));
1166}
1167
1168TEST(KeyTest, InsideContainsUsingMultimap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001169 multimap<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001170 container.insert(make_pair(1, 'a'));
1171 container.insert(make_pair(2, 'b'));
1172 container.insert(make_pair(4, 'c'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001173
1174 EXPECT_THAT(container, Not(Contains(Key(25))));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001175 container.insert(make_pair(25, 'd'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001176 EXPECT_THAT(container, Contains(Key(25)));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001177 container.insert(make_pair(25, 'e'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001178 EXPECT_THAT(container, Contains(Key(25)));
1179
1180 EXPECT_THAT(container, Contains(Key(1)));
1181 EXPECT_THAT(container, Not(Contains(Key(3))));
1182}
1183
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001184TEST(PairTest, Typing) {
1185 // Test verifies the following type conversions can be compiled.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001186 Matcher<const pair<const char*, int>&> m1 = Pair("foo", 42);
1187 Matcher<const pair<const char*, int> > m2 = Pair("foo", 42);
1188 Matcher<pair<const char*, int> > m3 = Pair("foo", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001189
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001190 Matcher<pair<int, const std::string> > m4 = Pair(25, "42");
1191 Matcher<pair<const std::string, int> > m5 = Pair("25", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001192}
1193
1194TEST(PairTest, CanDescribeSelf) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001195 Matcher<const pair<std::string, int>&> m1 = Pair("foo", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001196 EXPECT_EQ("has a first field that is equal to \"foo\""
1197 ", and has a second field that is equal to 42",
1198 Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001199 EXPECT_EQ("has a first field that isn't equal to \"foo\""
1200 ", or has a second field that isn't equal to 42",
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001201 DescribeNegation(m1));
1202 // Double and triple negation (1 or 2 times not and description of negation).
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001203 Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42));
1204 EXPECT_EQ("has a first field that isn't equal to 13"
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001205 ", and has a second field that is equal to 42",
1206 DescribeNegation(m2));
1207}
1208
1209TEST(PairTest, CanExplainMatchResultTo) {
zhanyong.wan82113312010-01-08 21:55:40 +00001210 // If neither field matches, Pair() should explain about the first
1211 // field.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001212 const Matcher<pair<int, int> > m = Pair(GreaterThan(0), GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001213 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001214 Explain(m, make_pair(-1, -2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001215
zhanyong.wan82113312010-01-08 21:55:40 +00001216 // If the first field matches but the second doesn't, Pair() should
1217 // explain about the second field.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001218 EXPECT_EQ("whose second field does not match, which is 2 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001219 Explain(m, make_pair(1, -2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001220
zhanyong.wan82113312010-01-08 21:55:40 +00001221 // If the first field doesn't match but the second does, Pair()
1222 // should explain about the first field.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001223 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001224 Explain(m, make_pair(-1, 2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001225
zhanyong.wan82113312010-01-08 21:55:40 +00001226 // If both fields match, Pair() should explain about them both.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001227 EXPECT_EQ("whose both fields match, where the first field is a value "
1228 "which is 1 more than 0, and the second field is a value "
1229 "which is 2 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001230 Explain(m, make_pair(1, 2)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001231
1232 // If only the first match has an explanation, only this explanation should
1233 // be printed.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001234 const Matcher<pair<int, int> > explain_first = Pair(GreaterThan(0), 0);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001235 EXPECT_EQ("whose both fields match, where the first field is a value "
1236 "which is 1 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001237 Explain(explain_first, make_pair(1, 0)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001238
1239 // If only the second match has an explanation, only this explanation should
1240 // be printed.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001241 const Matcher<pair<int, int> > explain_second = Pair(0, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001242 EXPECT_EQ("whose both fields match, where the second field is a value "
1243 "which is 1 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001244 Explain(explain_second, make_pair(0, 1)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001245}
1246
1247TEST(PairTest, MatchesCorrectly) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001248 pair<int, std::string> p(25, "foo");
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001249
1250 // Both fields match.
1251 EXPECT_THAT(p, Pair(25, "foo"));
1252 EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o")));
1253
1254 // 'first' doesnt' match, but 'second' matches.
1255 EXPECT_THAT(p, Not(Pair(42, "foo")));
1256 EXPECT_THAT(p, Not(Pair(Lt(25), "foo")));
1257
1258 // 'first' matches, but 'second' doesn't match.
1259 EXPECT_THAT(p, Not(Pair(25, "bar")));
1260 EXPECT_THAT(p, Not(Pair(25, Not("foo"))));
1261
1262 // Neither field matches.
1263 EXPECT_THAT(p, Not(Pair(13, "bar")));
1264 EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a"))));
1265}
1266
1267TEST(PairTest, SafelyCastsInnerMatchers) {
1268 Matcher<int> is_positive = Gt(0);
1269 Matcher<int> is_negative = Lt(0);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001270 pair<char, bool> p('a', true);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001271 EXPECT_THAT(p, Pair(is_positive, _));
1272 EXPECT_THAT(p, Not(Pair(is_negative, _)));
1273 EXPECT_THAT(p, Pair(_, is_positive));
1274 EXPECT_THAT(p, Not(Pair(_, is_negative)));
1275}
1276
1277TEST(PairTest, InsideContainsUsingMap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001278 map<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001279 container.insert(make_pair(1, 'a'));
1280 container.insert(make_pair(2, 'b'));
1281 container.insert(make_pair(4, 'c'));
zhanyong.wan95b12332009-09-25 18:55:50 +00001282 EXPECT_THAT(container, Contains(Pair(1, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001283 EXPECT_THAT(container, Contains(Pair(1, _)));
zhanyong.wan95b12332009-09-25 18:55:50 +00001284 EXPECT_THAT(container, Contains(Pair(_, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001285 EXPECT_THAT(container, Not(Contains(Pair(3, _))));
1286}
1287
shiqiane35fdd92008-12-10 05:08:54 +00001288// Tests StartsWith(s).
1289
1290TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
1291 const Matcher<const char*> m1 = StartsWith(string(""));
1292 EXPECT_TRUE(m1.Matches("Hi"));
1293 EXPECT_TRUE(m1.Matches(""));
1294 EXPECT_FALSE(m1.Matches(NULL));
1295
1296 const Matcher<const string&> m2 = StartsWith("Hi");
1297 EXPECT_TRUE(m2.Matches("Hi"));
1298 EXPECT_TRUE(m2.Matches("Hi Hi!"));
1299 EXPECT_TRUE(m2.Matches("High"));
1300 EXPECT_FALSE(m2.Matches("H"));
1301 EXPECT_FALSE(m2.Matches(" Hi"));
1302}
1303
1304TEST(StartsWithTest, CanDescribeSelf) {
1305 Matcher<const std::string> m = StartsWith("Hi");
1306 EXPECT_EQ("starts with \"Hi\"", Describe(m));
1307}
1308
1309// Tests EndsWith(s).
1310
1311TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
1312 const Matcher<const char*> m1 = EndsWith("");
1313 EXPECT_TRUE(m1.Matches("Hi"));
1314 EXPECT_TRUE(m1.Matches(""));
1315 EXPECT_FALSE(m1.Matches(NULL));
1316
1317 const Matcher<const string&> m2 = EndsWith(string("Hi"));
1318 EXPECT_TRUE(m2.Matches("Hi"));
1319 EXPECT_TRUE(m2.Matches("Wow Hi Hi"));
1320 EXPECT_TRUE(m2.Matches("Super Hi"));
1321 EXPECT_FALSE(m2.Matches("i"));
1322 EXPECT_FALSE(m2.Matches("Hi "));
1323}
1324
1325TEST(EndsWithTest, CanDescribeSelf) {
1326 Matcher<const std::string> m = EndsWith("Hi");
1327 EXPECT_EQ("ends with \"Hi\"", Describe(m));
1328}
1329
shiqiane35fdd92008-12-10 05:08:54 +00001330// Tests MatchesRegex().
1331
1332TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
1333 const Matcher<const char*> m1 = MatchesRegex("a.*z");
1334 EXPECT_TRUE(m1.Matches("az"));
1335 EXPECT_TRUE(m1.Matches("abcz"));
1336 EXPECT_FALSE(m1.Matches(NULL));
1337
1338 const Matcher<const string&> m2 = MatchesRegex(new RE("a.*z"));
1339 EXPECT_TRUE(m2.Matches("azbz"));
1340 EXPECT_FALSE(m2.Matches("az1"));
1341 EXPECT_FALSE(m2.Matches("1az"));
1342}
1343
1344TEST(MatchesRegexTest, CanDescribeSelf) {
1345 Matcher<const std::string> m1 = MatchesRegex(string("Hi.*"));
1346 EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1));
1347
zhanyong.wand14aaed2010-01-14 05:36:32 +00001348 Matcher<const char*> m2 = MatchesRegex(new RE("a.*"));
1349 EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +00001350}
1351
1352// Tests ContainsRegex().
1353
1354TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
1355 const Matcher<const char*> m1 = ContainsRegex(string("a.*z"));
1356 EXPECT_TRUE(m1.Matches("az"));
1357 EXPECT_TRUE(m1.Matches("0abcz1"));
1358 EXPECT_FALSE(m1.Matches(NULL));
1359
1360 const Matcher<const string&> m2 = ContainsRegex(new RE("a.*z"));
1361 EXPECT_TRUE(m2.Matches("azbz"));
1362 EXPECT_TRUE(m2.Matches("az1"));
1363 EXPECT_FALSE(m2.Matches("1a"));
1364}
1365
1366TEST(ContainsRegexTest, CanDescribeSelf) {
1367 Matcher<const std::string> m1 = ContainsRegex("Hi.*");
1368 EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1));
1369
zhanyong.wand14aaed2010-01-14 05:36:32 +00001370 Matcher<const char*> m2 = ContainsRegex(new RE("a.*"));
1371 EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +00001372}
shiqiane35fdd92008-12-10 05:08:54 +00001373
1374// Tests for wide strings.
1375#if GTEST_HAS_STD_WSTRING
1376TEST(StdWideStrEqTest, MatchesEqual) {
1377 Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello"));
1378 EXPECT_TRUE(m.Matches(L"Hello"));
1379 EXPECT_FALSE(m.Matches(L"hello"));
1380 EXPECT_FALSE(m.Matches(NULL));
1381
1382 Matcher<const ::std::wstring&> m2 = StrEq(L"Hello");
1383 EXPECT_TRUE(m2.Matches(L"Hello"));
1384 EXPECT_FALSE(m2.Matches(L"Hi"));
1385
1386 Matcher<const ::std::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1387 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1388 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1389
1390 ::std::wstring str(L"01204500800");
1391 str[3] = L'\0';
1392 Matcher<const ::std::wstring&> m4 = StrEq(str);
1393 EXPECT_TRUE(m4.Matches(str));
1394 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1395 Matcher<const ::std::wstring&> m5 = StrEq(str);
1396 EXPECT_TRUE(m5.Matches(str));
1397}
1398
1399TEST(StdWideStrEqTest, CanDescribeSelf) {
vladlosevaa432202011-04-01 21:58:42 +00001400 Matcher< ::std::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v");
1401 EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
shiqiane35fdd92008-12-10 05:08:54 +00001402 Describe(m));
1403
1404 Matcher< ::std::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1405 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1406 Describe(m2));
1407
1408 ::std::wstring str(L"01204500800");
1409 str[3] = L'\0';
1410 Matcher<const ::std::wstring&> m4 = StrEq(str);
1411 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1412 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1413 Matcher<const ::std::wstring&> m5 = StrEq(str);
1414 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1415}
1416
1417TEST(StdWideStrNeTest, MatchesUnequalString) {
1418 Matcher<const wchar_t*> m = StrNe(L"Hello");
1419 EXPECT_TRUE(m.Matches(L""));
1420 EXPECT_TRUE(m.Matches(NULL));
1421 EXPECT_FALSE(m.Matches(L"Hello"));
1422
1423 Matcher< ::std::wstring> m2 = StrNe(::std::wstring(L"Hello"));
1424 EXPECT_TRUE(m2.Matches(L"hello"));
1425 EXPECT_FALSE(m2.Matches(L"Hello"));
1426}
1427
1428TEST(StdWideStrNeTest, CanDescribeSelf) {
1429 Matcher<const wchar_t*> m = StrNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001430 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001431}
1432
1433TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1434 Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello"));
1435 EXPECT_TRUE(m.Matches(L"Hello"));
1436 EXPECT_TRUE(m.Matches(L"hello"));
1437 EXPECT_FALSE(m.Matches(L"Hi"));
1438 EXPECT_FALSE(m.Matches(NULL));
1439
1440 Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello");
1441 EXPECT_TRUE(m2.Matches(L"hello"));
1442 EXPECT_FALSE(m2.Matches(L"Hi"));
1443}
1444
1445TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1446 ::std::wstring str1(L"oabocdooeoo");
1447 ::std::wstring str2(L"OABOCDOOEOO");
1448 Matcher<const ::std::wstring&> m0 = StrCaseEq(str1);
1449 EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L'\0')));
1450
1451 str1[3] = str2[3] = L'\0';
1452 Matcher<const ::std::wstring&> m1 = StrCaseEq(str1);
1453 EXPECT_TRUE(m1.Matches(str2));
1454
1455 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1456 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1457 Matcher<const ::std::wstring&> m2 = StrCaseEq(str1);
1458 str1[9] = str2[9] = L'\0';
1459 EXPECT_FALSE(m2.Matches(str2));
1460
1461 Matcher<const ::std::wstring&> m3 = StrCaseEq(str1);
1462 EXPECT_TRUE(m3.Matches(str2));
1463
1464 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1465 str2.append(1, L'\0');
1466 EXPECT_FALSE(m3.Matches(str2));
1467 EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9)));
1468}
1469
1470TEST(StdWideStrCaseEqTest, CanDescribeSelf) {
1471 Matcher< ::std::wstring> m = StrCaseEq(L"Hi");
1472 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1473}
1474
1475TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1476 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1477 EXPECT_TRUE(m.Matches(L"Hi"));
1478 EXPECT_TRUE(m.Matches(NULL));
1479 EXPECT_FALSE(m.Matches(L"Hello"));
1480 EXPECT_FALSE(m.Matches(L"hello"));
1481
1482 Matcher< ::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello"));
1483 EXPECT_TRUE(m2.Matches(L""));
1484 EXPECT_FALSE(m2.Matches(L"Hello"));
1485}
1486
1487TEST(StdWideStrCaseNeTest, CanDescribeSelf) {
1488 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001489 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001490}
1491
1492// Tests that HasSubstr() works for matching wstring-typed values.
1493TEST(StdWideHasSubstrTest, WorksForStringClasses) {
1494 const Matcher< ::std::wstring> m1 = HasSubstr(L"foo");
1495 EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food.")));
1496 EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo")));
1497
1498 const Matcher<const ::std::wstring&> m2 = HasSubstr(L"foo");
1499 EXPECT_TRUE(m2.Matches(::std::wstring(L"I love food.")));
1500 EXPECT_FALSE(m2.Matches(::std::wstring(L"tofo")));
1501}
1502
1503// Tests that HasSubstr() works for matching C-wide-string-typed values.
1504TEST(StdWideHasSubstrTest, WorksForCStrings) {
1505 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1506 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1507 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1508 EXPECT_FALSE(m1.Matches(NULL));
1509
1510 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1511 EXPECT_TRUE(m2.Matches(L"I love food."));
1512 EXPECT_FALSE(m2.Matches(L"tofo"));
1513 EXPECT_FALSE(m2.Matches(NULL));
1514}
1515
1516// Tests that HasSubstr(s) describes itself properly.
1517TEST(StdWideHasSubstrTest, CanDescribeSelf) {
1518 Matcher< ::std::wstring> m = HasSubstr(L"foo\n\"");
1519 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1520}
1521
1522// Tests StartsWith(s).
1523
1524TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) {
1525 const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L""));
1526 EXPECT_TRUE(m1.Matches(L"Hi"));
1527 EXPECT_TRUE(m1.Matches(L""));
1528 EXPECT_FALSE(m1.Matches(NULL));
1529
1530 const Matcher<const ::std::wstring&> m2 = StartsWith(L"Hi");
1531 EXPECT_TRUE(m2.Matches(L"Hi"));
1532 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1533 EXPECT_TRUE(m2.Matches(L"High"));
1534 EXPECT_FALSE(m2.Matches(L"H"));
1535 EXPECT_FALSE(m2.Matches(L" Hi"));
1536}
1537
1538TEST(StdWideStartsWithTest, CanDescribeSelf) {
1539 Matcher<const ::std::wstring> m = StartsWith(L"Hi");
1540 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1541}
1542
1543// Tests EndsWith(s).
1544
1545TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) {
1546 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1547 EXPECT_TRUE(m1.Matches(L"Hi"));
1548 EXPECT_TRUE(m1.Matches(L""));
1549 EXPECT_FALSE(m1.Matches(NULL));
1550
1551 const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L"Hi"));
1552 EXPECT_TRUE(m2.Matches(L"Hi"));
1553 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1554 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1555 EXPECT_FALSE(m2.Matches(L"i"));
1556 EXPECT_FALSE(m2.Matches(L"Hi "));
1557}
1558
1559TEST(StdWideEndsWithTest, CanDescribeSelf) {
1560 Matcher<const ::std::wstring> m = EndsWith(L"Hi");
1561 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1562}
1563
1564#endif // GTEST_HAS_STD_WSTRING
1565
1566#if GTEST_HAS_GLOBAL_WSTRING
1567TEST(GlobalWideStrEqTest, MatchesEqual) {
1568 Matcher<const wchar_t*> m = StrEq(::wstring(L"Hello"));
1569 EXPECT_TRUE(m.Matches(L"Hello"));
1570 EXPECT_FALSE(m.Matches(L"hello"));
1571 EXPECT_FALSE(m.Matches(NULL));
1572
1573 Matcher<const ::wstring&> m2 = StrEq(L"Hello");
1574 EXPECT_TRUE(m2.Matches(L"Hello"));
1575 EXPECT_FALSE(m2.Matches(L"Hi"));
1576
1577 Matcher<const ::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1578 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1579 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1580
1581 ::wstring str(L"01204500800");
1582 str[3] = L'\0';
1583 Matcher<const ::wstring&> m4 = StrEq(str);
1584 EXPECT_TRUE(m4.Matches(str));
1585 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1586 Matcher<const ::wstring&> m5 = StrEq(str);
1587 EXPECT_TRUE(m5.Matches(str));
1588}
1589
1590TEST(GlobalWideStrEqTest, CanDescribeSelf) {
vladlosevaa432202011-04-01 21:58:42 +00001591 Matcher< ::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v");
1592 EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
shiqiane35fdd92008-12-10 05:08:54 +00001593 Describe(m));
1594
1595 Matcher< ::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1596 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1597 Describe(m2));
1598
1599 ::wstring str(L"01204500800");
1600 str[3] = L'\0';
1601 Matcher<const ::wstring&> m4 = StrEq(str);
1602 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1603 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1604 Matcher<const ::wstring&> m5 = StrEq(str);
1605 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1606}
1607
1608TEST(GlobalWideStrNeTest, MatchesUnequalString) {
1609 Matcher<const wchar_t*> m = StrNe(L"Hello");
1610 EXPECT_TRUE(m.Matches(L""));
1611 EXPECT_TRUE(m.Matches(NULL));
1612 EXPECT_FALSE(m.Matches(L"Hello"));
1613
1614 Matcher< ::wstring> m2 = StrNe(::wstring(L"Hello"));
1615 EXPECT_TRUE(m2.Matches(L"hello"));
1616 EXPECT_FALSE(m2.Matches(L"Hello"));
1617}
1618
1619TEST(GlobalWideStrNeTest, CanDescribeSelf) {
1620 Matcher<const wchar_t*> m = StrNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001621 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001622}
1623
1624TEST(GlobalWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1625 Matcher<const wchar_t*> m = StrCaseEq(::wstring(L"Hello"));
1626 EXPECT_TRUE(m.Matches(L"Hello"));
1627 EXPECT_TRUE(m.Matches(L"hello"));
1628 EXPECT_FALSE(m.Matches(L"Hi"));
1629 EXPECT_FALSE(m.Matches(NULL));
1630
1631 Matcher<const ::wstring&> m2 = StrCaseEq(L"Hello");
1632 EXPECT_TRUE(m2.Matches(L"hello"));
1633 EXPECT_FALSE(m2.Matches(L"Hi"));
1634}
1635
1636TEST(GlobalWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1637 ::wstring str1(L"oabocdooeoo");
1638 ::wstring str2(L"OABOCDOOEOO");
1639 Matcher<const ::wstring&> m0 = StrCaseEq(str1);
1640 EXPECT_FALSE(m0.Matches(str2 + ::wstring(1, L'\0')));
1641
1642 str1[3] = str2[3] = L'\0';
1643 Matcher<const ::wstring&> m1 = StrCaseEq(str1);
1644 EXPECT_TRUE(m1.Matches(str2));
1645
1646 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1647 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1648 Matcher<const ::wstring&> m2 = StrCaseEq(str1);
1649 str1[9] = str2[9] = L'\0';
1650 EXPECT_FALSE(m2.Matches(str2));
1651
1652 Matcher<const ::wstring&> m3 = StrCaseEq(str1);
1653 EXPECT_TRUE(m3.Matches(str2));
1654
1655 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1656 str2.append(1, L'\0');
1657 EXPECT_FALSE(m3.Matches(str2));
1658 EXPECT_FALSE(m3.Matches(::wstring(str2, 0, 9)));
1659}
1660
1661TEST(GlobalWideStrCaseEqTest, CanDescribeSelf) {
1662 Matcher< ::wstring> m = StrCaseEq(L"Hi");
1663 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1664}
1665
1666TEST(GlobalWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1667 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1668 EXPECT_TRUE(m.Matches(L"Hi"));
1669 EXPECT_TRUE(m.Matches(NULL));
1670 EXPECT_FALSE(m.Matches(L"Hello"));
1671 EXPECT_FALSE(m.Matches(L"hello"));
1672
1673 Matcher< ::wstring> m2 = StrCaseNe(::wstring(L"Hello"));
1674 EXPECT_TRUE(m2.Matches(L""));
1675 EXPECT_FALSE(m2.Matches(L"Hello"));
1676}
1677
1678TEST(GlobalWideStrCaseNeTest, CanDescribeSelf) {
1679 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001680 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001681}
1682
1683// Tests that HasSubstr() works for matching wstring-typed values.
1684TEST(GlobalWideHasSubstrTest, WorksForStringClasses) {
1685 const Matcher< ::wstring> m1 = HasSubstr(L"foo");
1686 EXPECT_TRUE(m1.Matches(::wstring(L"I love food.")));
1687 EXPECT_FALSE(m1.Matches(::wstring(L"tofo")));
1688
1689 const Matcher<const ::wstring&> m2 = HasSubstr(L"foo");
1690 EXPECT_TRUE(m2.Matches(::wstring(L"I love food.")));
1691 EXPECT_FALSE(m2.Matches(::wstring(L"tofo")));
1692}
1693
1694// Tests that HasSubstr() works for matching C-wide-string-typed values.
1695TEST(GlobalWideHasSubstrTest, WorksForCStrings) {
1696 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1697 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1698 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1699 EXPECT_FALSE(m1.Matches(NULL));
1700
1701 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1702 EXPECT_TRUE(m2.Matches(L"I love food."));
1703 EXPECT_FALSE(m2.Matches(L"tofo"));
1704 EXPECT_FALSE(m2.Matches(NULL));
1705}
1706
1707// Tests that HasSubstr(s) describes itself properly.
1708TEST(GlobalWideHasSubstrTest, CanDescribeSelf) {
1709 Matcher< ::wstring> m = HasSubstr(L"foo\n\"");
1710 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1711}
1712
1713// Tests StartsWith(s).
1714
1715TEST(GlobalWideStartsWithTest, MatchesStringWithGivenPrefix) {
1716 const Matcher<const wchar_t*> m1 = StartsWith(::wstring(L""));
1717 EXPECT_TRUE(m1.Matches(L"Hi"));
1718 EXPECT_TRUE(m1.Matches(L""));
1719 EXPECT_FALSE(m1.Matches(NULL));
1720
1721 const Matcher<const ::wstring&> m2 = StartsWith(L"Hi");
1722 EXPECT_TRUE(m2.Matches(L"Hi"));
1723 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1724 EXPECT_TRUE(m2.Matches(L"High"));
1725 EXPECT_FALSE(m2.Matches(L"H"));
1726 EXPECT_FALSE(m2.Matches(L" Hi"));
1727}
1728
1729TEST(GlobalWideStartsWithTest, CanDescribeSelf) {
1730 Matcher<const ::wstring> m = StartsWith(L"Hi");
1731 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1732}
1733
1734// Tests EndsWith(s).
1735
1736TEST(GlobalWideEndsWithTest, MatchesStringWithGivenSuffix) {
1737 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1738 EXPECT_TRUE(m1.Matches(L"Hi"));
1739 EXPECT_TRUE(m1.Matches(L""));
1740 EXPECT_FALSE(m1.Matches(NULL));
1741
1742 const Matcher<const ::wstring&> m2 = EndsWith(::wstring(L"Hi"));
1743 EXPECT_TRUE(m2.Matches(L"Hi"));
1744 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1745 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1746 EXPECT_FALSE(m2.Matches(L"i"));
1747 EXPECT_FALSE(m2.Matches(L"Hi "));
1748}
1749
1750TEST(GlobalWideEndsWithTest, CanDescribeSelf) {
1751 Matcher<const ::wstring> m = EndsWith(L"Hi");
1752 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1753}
1754
1755#endif // GTEST_HAS_GLOBAL_WSTRING
1756
1757
1758typedef ::std::tr1::tuple<long, int> Tuple2; // NOLINT
1759
1760// Tests that Eq() matches a 2-tuple where the first field == the
1761// second field.
1762TEST(Eq2Test, MatchesEqualArguments) {
1763 Matcher<const Tuple2&> m = Eq();
1764 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1765 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1766}
1767
1768// Tests that Eq() describes itself properly.
1769TEST(Eq2Test, CanDescribeSelf) {
1770 Matcher<const Tuple2&> m = Eq();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001771 EXPECT_EQ("are an equal pair", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001772}
1773
1774// Tests that Ge() matches a 2-tuple where the first field >= the
1775// second field.
1776TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) {
1777 Matcher<const Tuple2&> m = Ge();
1778 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1779 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1780 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1781}
1782
1783// Tests that Ge() describes itself properly.
1784TEST(Ge2Test, CanDescribeSelf) {
1785 Matcher<const Tuple2&> m = Ge();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001786 EXPECT_EQ("are a pair where the first >= the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001787}
1788
1789// Tests that Gt() matches a 2-tuple where the first field > the
1790// second field.
1791TEST(Gt2Test, MatchesGreaterThanArguments) {
1792 Matcher<const Tuple2&> m = Gt();
1793 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1794 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1795 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1796}
1797
1798// Tests that Gt() describes itself properly.
1799TEST(Gt2Test, CanDescribeSelf) {
1800 Matcher<const Tuple2&> m = Gt();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001801 EXPECT_EQ("are a pair where the first > the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001802}
1803
1804// Tests that Le() matches a 2-tuple where the first field <= the
1805// second field.
1806TEST(Le2Test, MatchesLessThanOrEqualArguments) {
1807 Matcher<const Tuple2&> m = Le();
1808 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1809 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1810 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1811}
1812
1813// Tests that Le() describes itself properly.
1814TEST(Le2Test, CanDescribeSelf) {
1815 Matcher<const Tuple2&> m = Le();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001816 EXPECT_EQ("are a pair where the first <= the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001817}
1818
1819// Tests that Lt() matches a 2-tuple where the first field < the
1820// second field.
1821TEST(Lt2Test, MatchesLessThanArguments) {
1822 Matcher<const Tuple2&> m = Lt();
1823 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1824 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1825 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1826}
1827
1828// Tests that Lt() describes itself properly.
1829TEST(Lt2Test, CanDescribeSelf) {
1830 Matcher<const Tuple2&> m = Lt();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001831 EXPECT_EQ("are a pair where the first < the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001832}
1833
1834// Tests that Ne() matches a 2-tuple where the first field != the
1835// second field.
1836TEST(Ne2Test, MatchesUnequalArguments) {
1837 Matcher<const Tuple2&> m = Ne();
1838 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1839 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1840 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1841}
1842
1843// Tests that Ne() describes itself properly.
1844TEST(Ne2Test, CanDescribeSelf) {
1845 Matcher<const Tuple2&> m = Ne();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001846 EXPECT_EQ("are an unequal pair", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001847}
1848
1849// Tests that Not(m) matches any value that doesn't match m.
1850TEST(NotTest, NegatesMatcher) {
1851 Matcher<int> m;
1852 m = Not(Eq(2));
1853 EXPECT_TRUE(m.Matches(3));
1854 EXPECT_FALSE(m.Matches(2));
1855}
1856
1857// Tests that Not(m) describes itself properly.
1858TEST(NotTest, CanDescribeSelf) {
1859 Matcher<int> m = Not(Eq(5));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001860 EXPECT_EQ("isn't equal to 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001861}
1862
zhanyong.wan18490652009-05-11 18:54:08 +00001863// Tests that monomorphic matchers are safely cast by the Not matcher.
1864TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {
1865 // greater_than_5 is a monomorphic matcher.
1866 Matcher<int> greater_than_5 = Gt(5);
1867
1868 Matcher<const int&> m = Not(greater_than_5);
1869 Matcher<int&> m2 = Not(greater_than_5);
1870 Matcher<int&> m3 = Not(m);
1871}
1872
zhanyong.wan02c15052010-06-09 19:21:30 +00001873// Helper to allow easy testing of AllOf matchers with num parameters.
1874void AllOfMatches(int num, const Matcher<int>& m) {
1875 SCOPED_TRACE(Describe(m));
1876 EXPECT_TRUE(m.Matches(0));
1877 for (int i = 1; i <= num; ++i) {
1878 EXPECT_FALSE(m.Matches(i));
1879 }
1880 EXPECT_TRUE(m.Matches(num + 1));
1881}
1882
shiqiane35fdd92008-12-10 05:08:54 +00001883// Tests that AllOf(m1, ..., mn) matches any value that matches all of
1884// the given matchers.
1885TEST(AllOfTest, MatchesWhenAllMatch) {
1886 Matcher<int> m;
1887 m = AllOf(Le(2), Ge(1));
1888 EXPECT_TRUE(m.Matches(1));
1889 EXPECT_TRUE(m.Matches(2));
1890 EXPECT_FALSE(m.Matches(0));
1891 EXPECT_FALSE(m.Matches(3));
1892
1893 m = AllOf(Gt(0), Ne(1), Ne(2));
1894 EXPECT_TRUE(m.Matches(3));
1895 EXPECT_FALSE(m.Matches(2));
1896 EXPECT_FALSE(m.Matches(1));
1897 EXPECT_FALSE(m.Matches(0));
1898
1899 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
1900 EXPECT_TRUE(m.Matches(4));
1901 EXPECT_FALSE(m.Matches(3));
1902 EXPECT_FALSE(m.Matches(2));
1903 EXPECT_FALSE(m.Matches(1));
1904 EXPECT_FALSE(m.Matches(0));
1905
1906 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
1907 EXPECT_TRUE(m.Matches(0));
1908 EXPECT_TRUE(m.Matches(1));
1909 EXPECT_FALSE(m.Matches(3));
zhanyong.wan02c15052010-06-09 19:21:30 +00001910
1911 // The following tests for varying number of sub-matchers. Due to the way
1912 // the sub-matchers are handled it is enough to test every sub-matcher once
1913 // with sub-matchers using the same matcher type. Varying matcher types are
1914 // checked for above.
1915 AllOfMatches(2, AllOf(Ne(1), Ne(2)));
1916 AllOfMatches(3, AllOf(Ne(1), Ne(2), Ne(3)));
1917 AllOfMatches(4, AllOf(Ne(1), Ne(2), Ne(3), Ne(4)));
1918 AllOfMatches(5, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5)));
1919 AllOfMatches(6, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6)));
1920 AllOfMatches(7, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7)));
1921 AllOfMatches(8, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
1922 Ne(8)));
1923 AllOfMatches(9, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
1924 Ne(8), Ne(9)));
1925 AllOfMatches(10, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8),
1926 Ne(9), Ne(10)));
shiqiane35fdd92008-12-10 05:08:54 +00001927}
1928
1929// Tests that AllOf(m1, ..., mn) describes itself properly.
1930TEST(AllOfTest, CanDescribeSelf) {
1931 Matcher<int> m;
1932 m = AllOf(Le(2), Ge(1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001933 EXPECT_EQ("(is <= 2) and (is >= 1)", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001934
1935 m = AllOf(Gt(0), Ne(1), Ne(2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001936 EXPECT_EQ("(is > 0) and "
1937 "((isn't equal to 1) and "
1938 "(isn't equal to 2))",
shiqiane35fdd92008-12-10 05:08:54 +00001939 Describe(m));
1940
1941
1942 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001943 EXPECT_EQ("(is > 0) and "
1944 "((isn't equal to 1) and "
1945 "((isn't equal to 2) and "
1946 "(isn't equal to 3)))",
shiqiane35fdd92008-12-10 05:08:54 +00001947 Describe(m));
1948
1949
1950 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001951 EXPECT_EQ("(is >= 0) and "
1952 "((is < 10) and "
1953 "((isn't equal to 3) and "
1954 "((isn't equal to 5) and "
1955 "(isn't equal to 7))))",
1956 Describe(m));
1957}
1958
1959// Tests that AllOf(m1, ..., mn) describes its negation properly.
1960TEST(AllOfTest, CanDescribeNegation) {
1961 Matcher<int> m;
1962 m = AllOf(Le(2), Ge(1));
1963 EXPECT_EQ("(isn't <= 2) or "
1964 "(isn't >= 1)",
1965 DescribeNegation(m));
1966
1967 m = AllOf(Gt(0), Ne(1), Ne(2));
1968 EXPECT_EQ("(isn't > 0) or "
1969 "((is equal to 1) or "
1970 "(is equal to 2))",
1971 DescribeNegation(m));
1972
1973
1974 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
1975 EXPECT_EQ("(isn't > 0) or "
1976 "((is equal to 1) or "
1977 "((is equal to 2) or "
1978 "(is equal to 3)))",
1979 DescribeNegation(m));
1980
1981
1982 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
1983 EXPECT_EQ("(isn't >= 0) or "
1984 "((isn't < 10) or "
1985 "((is equal to 3) or "
1986 "((is equal to 5) or "
1987 "(is equal to 7))))",
1988 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00001989}
1990
zhanyong.wan18490652009-05-11 18:54:08 +00001991// Tests that monomorphic matchers are safely cast by the AllOf matcher.
1992TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
1993 // greater_than_5 and less_than_10 are monomorphic matchers.
1994 Matcher<int> greater_than_5 = Gt(5);
1995 Matcher<int> less_than_10 = Lt(10);
1996
1997 Matcher<const int&> m = AllOf(greater_than_5, less_than_10);
1998 Matcher<int&> m2 = AllOf(greater_than_5, less_than_10);
1999 Matcher<int&> m3 = AllOf(greater_than_5, m2);
2000
2001 // Tests that BothOf works when composing itself.
2002 Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10);
2003 Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);
2004}
2005
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002006TEST(AllOfTest, ExplainsResult) {
2007 Matcher<int> m;
2008
2009 // Successful match. Both matchers need to explain. The second
2010 // matcher doesn't give an explanation, so only the first matcher's
2011 // explanation is printed.
2012 m = AllOf(GreaterThan(10), Lt(30));
2013 EXPECT_EQ("which is 15 more than 10", Explain(m, 25));
2014
2015 // Successful match. Both matchers need to explain.
2016 m = AllOf(GreaterThan(10), GreaterThan(20));
2017 EXPECT_EQ("which is 20 more than 10, and which is 10 more than 20",
2018 Explain(m, 30));
2019
2020 // Successful match. All matchers need to explain. The second
2021 // matcher doesn't given an explanation.
2022 m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20));
2023 EXPECT_EQ("which is 15 more than 10, and which is 5 more than 20",
2024 Explain(m, 25));
2025
2026 // Successful match. All matchers need to explain.
2027 m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2028 EXPECT_EQ("which is 30 more than 10, and which is 20 more than 20, "
2029 "and which is 10 more than 30",
2030 Explain(m, 40));
2031
2032 // Failed match. The first matcher, which failed, needs to
2033 // explain.
2034 m = AllOf(GreaterThan(10), GreaterThan(20));
2035 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
2036
2037 // Failed match. The second matcher, which failed, needs to
2038 // explain. Since it doesn't given an explanation, nothing is
2039 // printed.
2040 m = AllOf(GreaterThan(10), Lt(30));
2041 EXPECT_EQ("", Explain(m, 40));
2042
2043 // Failed match. The second matcher, which failed, needs to
2044 // explain.
2045 m = AllOf(GreaterThan(10), GreaterThan(20));
2046 EXPECT_EQ("which is 5 less than 20", Explain(m, 15));
2047}
2048
zhanyong.wan02c15052010-06-09 19:21:30 +00002049// Helper to allow easy testing of AnyOf matchers with num parameters.
2050void AnyOfMatches(int num, const Matcher<int>& m) {
2051 SCOPED_TRACE(Describe(m));
2052 EXPECT_FALSE(m.Matches(0));
2053 for (int i = 1; i <= num; ++i) {
2054 EXPECT_TRUE(m.Matches(i));
2055 }
2056 EXPECT_FALSE(m.Matches(num + 1));
2057}
2058
shiqiane35fdd92008-12-10 05:08:54 +00002059// Tests that AnyOf(m1, ..., mn) matches any value that matches at
2060// least one of the given matchers.
2061TEST(AnyOfTest, MatchesWhenAnyMatches) {
2062 Matcher<int> m;
2063 m = AnyOf(Le(1), Ge(3));
2064 EXPECT_TRUE(m.Matches(1));
2065 EXPECT_TRUE(m.Matches(4));
2066 EXPECT_FALSE(m.Matches(2));
2067
2068 m = AnyOf(Lt(0), Eq(1), Eq(2));
2069 EXPECT_TRUE(m.Matches(-1));
2070 EXPECT_TRUE(m.Matches(1));
2071 EXPECT_TRUE(m.Matches(2));
2072 EXPECT_FALSE(m.Matches(0));
2073
2074 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2075 EXPECT_TRUE(m.Matches(-1));
2076 EXPECT_TRUE(m.Matches(1));
2077 EXPECT_TRUE(m.Matches(2));
2078 EXPECT_TRUE(m.Matches(3));
2079 EXPECT_FALSE(m.Matches(0));
2080
2081 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2082 EXPECT_TRUE(m.Matches(0));
2083 EXPECT_TRUE(m.Matches(11));
2084 EXPECT_TRUE(m.Matches(3));
2085 EXPECT_FALSE(m.Matches(2));
zhanyong.wan02c15052010-06-09 19:21:30 +00002086
2087 // The following tests for varying number of sub-matchers. Due to the way
2088 // the sub-matchers are handled it is enough to test every sub-matcher once
2089 // with sub-matchers using the same matcher type. Varying matcher types are
2090 // checked for above.
2091 AnyOfMatches(2, AnyOf(1, 2));
2092 AnyOfMatches(3, AnyOf(1, 2, 3));
2093 AnyOfMatches(4, AnyOf(1, 2, 3, 4));
2094 AnyOfMatches(5, AnyOf(1, 2, 3, 4, 5));
2095 AnyOfMatches(6, AnyOf(1, 2, 3, 4, 5, 6));
2096 AnyOfMatches(7, AnyOf(1, 2, 3, 4, 5, 6, 7));
2097 AnyOfMatches(8, AnyOf(1, 2, 3, 4, 5, 6, 7, 8));
2098 AnyOfMatches(9, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9));
2099 AnyOfMatches(10, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
shiqiane35fdd92008-12-10 05:08:54 +00002100}
2101
2102// Tests that AnyOf(m1, ..., mn) describes itself properly.
2103TEST(AnyOfTest, CanDescribeSelf) {
2104 Matcher<int> m;
2105 m = AnyOf(Le(1), Ge(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002106 EXPECT_EQ("(is <= 1) or (is >= 3)",
shiqiane35fdd92008-12-10 05:08:54 +00002107 Describe(m));
2108
2109 m = AnyOf(Lt(0), Eq(1), Eq(2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002110 EXPECT_EQ("(is < 0) or "
shiqiane35fdd92008-12-10 05:08:54 +00002111 "((is equal to 1) or (is equal to 2))",
2112 Describe(m));
2113
2114 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002115 EXPECT_EQ("(is < 0) or "
shiqiane35fdd92008-12-10 05:08:54 +00002116 "((is equal to 1) or "
2117 "((is equal to 2) or "
2118 "(is equal to 3)))",
2119 Describe(m));
2120
2121 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002122 EXPECT_EQ("(is <= 0) or "
2123 "((is > 10) or "
shiqiane35fdd92008-12-10 05:08:54 +00002124 "((is equal to 3) or "
2125 "((is equal to 5) or "
2126 "(is equal to 7))))",
2127 Describe(m));
2128}
2129
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002130// Tests that AnyOf(m1, ..., mn) describes its negation properly.
2131TEST(AnyOfTest, CanDescribeNegation) {
2132 Matcher<int> m;
2133 m = AnyOf(Le(1), Ge(3));
2134 EXPECT_EQ("(isn't <= 1) and (isn't >= 3)",
2135 DescribeNegation(m));
2136
2137 m = AnyOf(Lt(0), Eq(1), Eq(2));
2138 EXPECT_EQ("(isn't < 0) and "
2139 "((isn't equal to 1) and (isn't equal to 2))",
2140 DescribeNegation(m));
2141
2142 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2143 EXPECT_EQ("(isn't < 0) and "
2144 "((isn't equal to 1) and "
2145 "((isn't equal to 2) and "
2146 "(isn't equal to 3)))",
2147 DescribeNegation(m));
2148
2149 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2150 EXPECT_EQ("(isn't <= 0) and "
2151 "((isn't > 10) and "
2152 "((isn't equal to 3) and "
2153 "((isn't equal to 5) and "
2154 "(isn't equal to 7))))",
2155 DescribeNegation(m));
2156}
2157
zhanyong.wan18490652009-05-11 18:54:08 +00002158// Tests that monomorphic matchers are safely cast by the AnyOf matcher.
2159TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
2160 // greater_than_5 and less_than_10 are monomorphic matchers.
2161 Matcher<int> greater_than_5 = Gt(5);
2162 Matcher<int> less_than_10 = Lt(10);
2163
2164 Matcher<const int&> m = AnyOf(greater_than_5, less_than_10);
2165 Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10);
2166 Matcher<int&> m3 = AnyOf(greater_than_5, m2);
2167
2168 // Tests that EitherOf works when composing itself.
2169 Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10);
2170 Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);
2171}
2172
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002173TEST(AnyOfTest, ExplainsResult) {
2174 Matcher<int> m;
2175
2176 // Failed match. Both matchers need to explain. The second
2177 // matcher doesn't give an explanation, so only the first matcher's
2178 // explanation is printed.
2179 m = AnyOf(GreaterThan(10), Lt(0));
2180 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
2181
2182 // Failed match. Both matchers need to explain.
2183 m = AnyOf(GreaterThan(10), GreaterThan(20));
2184 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20",
2185 Explain(m, 5));
2186
2187 // Failed match. All matchers need to explain. The second
2188 // matcher doesn't given an explanation.
2189 m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30));
2190 EXPECT_EQ("which is 5 less than 10, and which is 25 less than 30",
2191 Explain(m, 5));
2192
2193 // Failed match. All matchers need to explain.
2194 m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2195 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20, "
2196 "and which is 25 less than 30",
2197 Explain(m, 5));
2198
2199 // Successful match. The first matcher, which succeeded, needs to
2200 // explain.
2201 m = AnyOf(GreaterThan(10), GreaterThan(20));
2202 EXPECT_EQ("which is 5 more than 10", Explain(m, 15));
2203
2204 // Successful match. The second matcher, which succeeded, needs to
2205 // explain. Since it doesn't given an explanation, nothing is
2206 // printed.
2207 m = AnyOf(GreaterThan(10), Lt(30));
2208 EXPECT_EQ("", Explain(m, 0));
2209
2210 // Successful match. The second matcher, which succeeded, needs to
2211 // explain.
2212 m = AnyOf(GreaterThan(30), GreaterThan(20));
2213 EXPECT_EQ("which is 5 more than 20", Explain(m, 25));
2214}
2215
shiqiane35fdd92008-12-10 05:08:54 +00002216// The following predicate function and predicate functor are for
2217// testing the Truly(predicate) matcher.
2218
2219// Returns non-zero if the input is positive. Note that the return
2220// type of this function is not bool. It's OK as Truly() accepts any
2221// unary function or functor whose return type can be implicitly
2222// converted to bool.
2223int IsPositive(double x) {
2224 return x > 0 ? 1 : 0;
2225}
2226
2227// This functor returns true if the input is greater than the given
2228// number.
2229class IsGreaterThan {
2230 public:
2231 explicit IsGreaterThan(int threshold) : threshold_(threshold) {}
2232
2233 bool operator()(int n) const { return n > threshold_; }
zhanyong.wan32de5f52009-12-23 00:13:23 +00002234
shiqiane35fdd92008-12-10 05:08:54 +00002235 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002236 int threshold_;
shiqiane35fdd92008-12-10 05:08:54 +00002237};
2238
2239// For testing Truly().
2240const int foo = 0;
2241
2242// This predicate returns true iff the argument references foo and has
2243// a zero value.
2244bool ReferencesFooAndIsZero(const int& n) {
2245 return (&n == &foo) && (n == 0);
2246}
2247
2248// Tests that Truly(predicate) matches what satisfies the given
2249// predicate.
2250TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {
2251 Matcher<double> m = Truly(IsPositive);
2252 EXPECT_TRUE(m.Matches(2.0));
2253 EXPECT_FALSE(m.Matches(-1.5));
2254}
2255
2256// Tests that Truly(predicate_functor) works too.
2257TEST(TrulyTest, CanBeUsedWithFunctor) {
2258 Matcher<int> m = Truly(IsGreaterThan(5));
2259 EXPECT_TRUE(m.Matches(6));
2260 EXPECT_FALSE(m.Matches(4));
2261}
2262
zhanyong.wan8d3dc0c2011-04-14 19:37:06 +00002263// A class that can be implicitly converted to bool.
2264class ConvertibleToBool {
2265 public:
2266 explicit ConvertibleToBool(int number) : number_(number) {}
2267 operator bool() const { return number_ != 0; }
2268
2269 private:
2270 int number_;
2271};
2272
2273ConvertibleToBool IsNotZero(int number) {
2274 return ConvertibleToBool(number);
2275}
2276
2277// Tests that the predicate used in Truly() may return a class that's
2278// implicitly convertible to bool, even when the class has no
2279// operator!().
2280TEST(TrulyTest, PredicateCanReturnAClassConvertibleToBool) {
2281 Matcher<int> m = Truly(IsNotZero);
2282 EXPECT_TRUE(m.Matches(1));
2283 EXPECT_FALSE(m.Matches(0));
2284}
2285
shiqiane35fdd92008-12-10 05:08:54 +00002286// Tests that Truly(predicate) can describe itself properly.
2287TEST(TrulyTest, CanDescribeSelf) {
2288 Matcher<double> m = Truly(IsPositive);
2289 EXPECT_EQ("satisfies the given predicate",
2290 Describe(m));
2291}
2292
2293// Tests that Truly(predicate) works when the matcher takes its
2294// argument by reference.
2295TEST(TrulyTest, WorksForByRefArguments) {
2296 Matcher<const int&> m = Truly(ReferencesFooAndIsZero);
2297 EXPECT_TRUE(m.Matches(foo));
2298 int n = 0;
2299 EXPECT_FALSE(m.Matches(n));
2300}
2301
2302// Tests that Matches(m) is a predicate satisfied by whatever that
2303// matches matcher m.
2304TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {
2305 EXPECT_TRUE(Matches(Ge(0))(1));
2306 EXPECT_FALSE(Matches(Eq('a'))('b'));
2307}
2308
2309// Tests that Matches(m) works when the matcher takes its argument by
2310// reference.
2311TEST(MatchesTest, WorksOnByRefArguments) {
2312 int m = 0, n = 0;
2313 EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n));
2314 EXPECT_FALSE(Matches(Ref(m))(n));
2315}
2316
2317// Tests that a Matcher on non-reference type can be used in
2318// Matches().
2319TEST(MatchesTest, WorksWithMatcherOnNonRefType) {
2320 Matcher<int> eq5 = Eq(5);
2321 EXPECT_TRUE(Matches(eq5)(5));
2322 EXPECT_FALSE(Matches(eq5)(2));
2323}
2324
zhanyong.wanb8243162009-06-04 05:48:20 +00002325// Tests Value(value, matcher). Since Value() is a simple wrapper for
2326// Matches(), which has been tested already, we don't spend a lot of
2327// effort on testing Value().
2328TEST(ValueTest, WorksWithPolymorphicMatcher) {
2329 EXPECT_TRUE(Value("hi", StartsWith("h")));
2330 EXPECT_FALSE(Value(5, Gt(10)));
2331}
2332
2333TEST(ValueTest, WorksWithMonomorphicMatcher) {
2334 const Matcher<int> is_zero = Eq(0);
2335 EXPECT_TRUE(Value(0, is_zero));
2336 EXPECT_FALSE(Value('a', is_zero));
2337
2338 int n = 0;
2339 const Matcher<const int&> ref_n = Ref(n);
2340 EXPECT_TRUE(Value(n, ref_n));
2341 EXPECT_FALSE(Value(1, ref_n));
2342}
2343
zhanyong.wana862f1d2010-03-15 21:23:04 +00002344TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) {
zhanyong.wan34b034c2010-03-05 21:23:23 +00002345 StringMatchResultListener listener1;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002346 EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002347 EXPECT_EQ("% 2 == 0", listener1.str());
2348
2349 StringMatchResultListener listener2;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002350 EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002351 EXPECT_EQ("", listener2.str());
2352}
2353
zhanyong.wana862f1d2010-03-15 21:23:04 +00002354TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) {
zhanyong.wan34b034c2010-03-05 21:23:23 +00002355 const Matcher<int> is_even = PolymorphicIsEven();
2356 StringMatchResultListener listener1;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002357 EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002358 EXPECT_EQ("% 2 == 0", listener1.str());
2359
2360 const Matcher<const double&> is_zero = Eq(0);
2361 StringMatchResultListener listener2;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002362 EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002363 EXPECT_EQ("", listener2.str());
2364}
2365
zhanyong.wana862f1d2010-03-15 21:23:04 +00002366MATCHER_P(Really, inner_matcher, "") {
2367 return ExplainMatchResult(inner_matcher, arg, result_listener);
2368}
2369
2370TEST(ExplainMatchResultTest, WorksInsideMATCHER) {
2371 EXPECT_THAT(0, Really(Eq(0)));
2372}
2373
zhanyong.wanbf550852009-06-09 06:09:53 +00002374TEST(AllArgsTest, WorksForTuple) {
2375 EXPECT_THAT(make_tuple(1, 2L), AllArgs(Lt()));
2376 EXPECT_THAT(make_tuple(2L, 1), Not(AllArgs(Lt())));
2377}
2378
2379TEST(AllArgsTest, WorksForNonTuple) {
2380 EXPECT_THAT(42, AllArgs(Gt(0)));
2381 EXPECT_THAT('a', Not(AllArgs(Eq('b'))));
2382}
2383
2384class AllArgsHelper {
2385 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002386 AllArgsHelper() {}
2387
zhanyong.wanbf550852009-06-09 06:09:53 +00002388 MOCK_METHOD2(Helper, int(char x, int y));
zhanyong.wan32de5f52009-12-23 00:13:23 +00002389
2390 private:
2391 GTEST_DISALLOW_COPY_AND_ASSIGN_(AllArgsHelper);
zhanyong.wanbf550852009-06-09 06:09:53 +00002392};
2393
2394TEST(AllArgsTest, WorksInWithClause) {
2395 AllArgsHelper helper;
2396 ON_CALL(helper, Helper(_, _))
2397 .With(AllArgs(Lt()))
2398 .WillByDefault(Return(1));
2399 EXPECT_CALL(helper, Helper(_, _));
2400 EXPECT_CALL(helper, Helper(_, _))
2401 .With(AllArgs(Gt()))
2402 .WillOnce(Return(2));
2403
2404 EXPECT_EQ(1, helper.Helper('\1', 2));
2405 EXPECT_EQ(2, helper.Helper('a', 1));
2406}
2407
shiqiane35fdd92008-12-10 05:08:54 +00002408// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2409// matches the matcher.
2410TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {
2411 ASSERT_THAT(5, Ge(2)) << "This should succeed.";
2412 ASSERT_THAT("Foo", EndsWith("oo"));
2413 EXPECT_THAT(2, AllOf(Le(7), Ge(0))) << "This should succeed too.";
2414 EXPECT_THAT("Hello", StartsWith("Hell"));
2415}
2416
2417// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2418// doesn't match the matcher.
2419TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {
2420 // 'n' must be static as it is used in an EXPECT_FATAL_FAILURE(),
2421 // which cannot reference auto variables.
zhanyong.wan736baa82010-09-27 17:44:16 +00002422 static unsigned short n; // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00002423 n = 5;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002424
2425 // VC++ prior to version 8.0 SP1 has a bug where it will not see any
2426 // functions declared in the namespace scope from within nested classes.
2427 // EXPECT/ASSERT_(NON)FATAL_FAILURE macros use nested classes so that all
2428 // namespace-level functions invoked inside them need to be explicitly
2429 // resolved.
2430 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Gt(10)),
shiqiane35fdd92008-12-10 05:08:54 +00002431 "Value of: n\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002432 "Expected: is > 10\n"
zhanyong.wan736baa82010-09-27 17:44:16 +00002433 " Actual: 5" + OfType("unsigned short"));
shiqiane35fdd92008-12-10 05:08:54 +00002434 n = 0;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002435 EXPECT_NONFATAL_FAILURE(
2436 EXPECT_THAT(n, ::testing::AllOf(::testing::Le(7), ::testing::Ge(5))),
2437 "Value of: n\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002438 "Expected: (is <= 7) and (is >= 5)\n"
zhanyong.wan736baa82010-09-27 17:44:16 +00002439 " Actual: 0" + OfType("unsigned short"));
shiqiane35fdd92008-12-10 05:08:54 +00002440}
2441
2442// Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument
2443// has a reference type.
2444TEST(MatcherAssertionTest, WorksForByRefArguments) {
2445 // We use a static variable here as EXPECT_FATAL_FAILURE() cannot
2446 // reference auto variables.
2447 static int n;
2448 n = 0;
2449 EXPECT_THAT(n, AllOf(Le(7), Ref(n)));
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002450 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
shiqiane35fdd92008-12-10 05:08:54 +00002451 "Value of: n\n"
2452 "Expected: does not reference the variable @");
2453 // Tests the "Actual" part.
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002454 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
zhanyong.wan736baa82010-09-27 17:44:16 +00002455 "Actual: 0" + OfType("int") + ", which is located @");
shiqiane35fdd92008-12-10 05:08:54 +00002456}
2457
zhanyong.wan95b12332009-09-25 18:55:50 +00002458#if !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002459// Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is
2460// monomorphic.
zhanyong.wan95b12332009-09-25 18:55:50 +00002461
2462// ASSERT_THAT("hello", starts_with_he) fails to compile with Nokia's
2463// Symbian compiler: it tries to compile
2464// template<T, U> class MatcherCastImpl { ...
zhanyong.wandb22c222010-01-28 21:52:29 +00002465// virtual bool MatchAndExplain(T x, ...) const {
2466// return source_matcher_.MatchAndExplain(static_cast<U>(x), ...);
zhanyong.wan95b12332009-09-25 18:55:50 +00002467// with U == string and T == const char*
2468// With ASSERT_THAT("hello"...) changed to ASSERT_THAT(string("hello") ... )
2469// the compiler silently crashes with no output.
2470// If MatcherCastImpl is changed to use U(x) instead of static_cast<U>(x)
2471// the code compiles but the converted string is bogus.
shiqiane35fdd92008-12-10 05:08:54 +00002472TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
2473 Matcher<const char*> starts_with_he = StartsWith("he");
2474 ASSERT_THAT("hello", starts_with_he);
2475
2476 Matcher<const string&> ends_with_ok = EndsWith("ok");
2477 ASSERT_THAT("book", ends_with_ok);
zhanyong.wan736baa82010-09-27 17:44:16 +00002478 const string bad = "bad";
2479 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(bad, ends_with_ok),
2480 "Value of: bad\n"
2481 "Expected: ends with \"ok\"\n"
2482 " Actual: \"bad\"");
shiqiane35fdd92008-12-10 05:08:54 +00002483 Matcher<int> is_greater_than_5 = Gt(5);
2484 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5),
2485 "Value of: 5\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002486 "Expected: is > 5\n"
zhanyong.wan736baa82010-09-27 17:44:16 +00002487 " Actual: 5" + OfType("int"));
shiqiane35fdd92008-12-10 05:08:54 +00002488}
zhanyong.wan95b12332009-09-25 18:55:50 +00002489#endif // !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002490
2491// Tests floating-point matchers.
2492template <typename RawType>
2493class FloatingPointTest : public testing::Test {
2494 protected:
2495 typedef typename testing::internal::FloatingPoint<RawType> Floating;
2496 typedef typename Floating::Bits Bits;
2497
2498 virtual void SetUp() {
2499 const size_t max_ulps = Floating::kMaxUlps;
2500
2501 // The bits that represent 0.0.
2502 const Bits zero_bits = Floating(0).bits();
2503
2504 // Makes some numbers close to 0.0.
2505 close_to_positive_zero_ = Floating::ReinterpretBits(zero_bits + max_ulps/2);
2506 close_to_negative_zero_ = -Floating::ReinterpretBits(
2507 zero_bits + max_ulps - max_ulps/2);
2508 further_from_negative_zero_ = -Floating::ReinterpretBits(
2509 zero_bits + max_ulps + 1 - max_ulps/2);
2510
2511 // The bits that represent 1.0.
2512 const Bits one_bits = Floating(1).bits();
2513
2514 // Makes some numbers close to 1.0.
2515 close_to_one_ = Floating::ReinterpretBits(one_bits + max_ulps);
2516 further_from_one_ = Floating::ReinterpretBits(one_bits + max_ulps + 1);
2517
2518 // +infinity.
2519 infinity_ = Floating::Infinity();
2520
2521 // The bits that represent +infinity.
2522 const Bits infinity_bits = Floating(infinity_).bits();
2523
2524 // Makes some numbers close to infinity.
2525 close_to_infinity_ = Floating::ReinterpretBits(infinity_bits - max_ulps);
2526 further_from_infinity_ = Floating::ReinterpretBits(
2527 infinity_bits - max_ulps - 1);
2528
2529 // Makes some NAN's.
2530 nan1_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 1);
2531 nan2_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 200);
2532 }
2533
2534 void TestSize() {
2535 EXPECT_EQ(sizeof(RawType), sizeof(Bits));
2536 }
2537
2538 // A battery of tests for FloatingEqMatcher::Matches.
2539 // matcher_maker is a pointer to a function which creates a FloatingEqMatcher.
2540 void TestMatches(
2541 testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) {
2542 Matcher<RawType> m1 = matcher_maker(0.0);
2543 EXPECT_TRUE(m1.Matches(-0.0));
2544 EXPECT_TRUE(m1.Matches(close_to_positive_zero_));
2545 EXPECT_TRUE(m1.Matches(close_to_negative_zero_));
2546 EXPECT_FALSE(m1.Matches(1.0));
2547
2548 Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_);
2549 EXPECT_FALSE(m2.Matches(further_from_negative_zero_));
2550
2551 Matcher<RawType> m3 = matcher_maker(1.0);
2552 EXPECT_TRUE(m3.Matches(close_to_one_));
2553 EXPECT_FALSE(m3.Matches(further_from_one_));
2554
2555 // Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above.
2556 EXPECT_FALSE(m3.Matches(0.0));
2557
2558 Matcher<RawType> m4 = matcher_maker(-infinity_);
2559 EXPECT_TRUE(m4.Matches(-close_to_infinity_));
2560
2561 Matcher<RawType> m5 = matcher_maker(infinity_);
2562 EXPECT_TRUE(m5.Matches(close_to_infinity_));
2563
2564 // This is interesting as the representations of infinity_ and nan1_
2565 // are only 1 DLP apart.
2566 EXPECT_FALSE(m5.Matches(nan1_));
2567
2568 // matcher_maker can produce a Matcher<const RawType&>, which is needed in
2569 // some cases.
2570 Matcher<const RawType&> m6 = matcher_maker(0.0);
2571 EXPECT_TRUE(m6.Matches(-0.0));
2572 EXPECT_TRUE(m6.Matches(close_to_positive_zero_));
2573 EXPECT_FALSE(m6.Matches(1.0));
2574
2575 // matcher_maker can produce a Matcher<RawType&>, which is needed in some
2576 // cases.
2577 Matcher<RawType&> m7 = matcher_maker(0.0);
2578 RawType x = 0.0;
2579 EXPECT_TRUE(m7.Matches(x));
2580 x = 0.01f;
2581 EXPECT_FALSE(m7.Matches(x));
2582 }
2583
2584 // Pre-calculated numbers to be used by the tests.
2585
2586 static RawType close_to_positive_zero_;
2587 static RawType close_to_negative_zero_;
2588 static RawType further_from_negative_zero_;
2589
2590 static RawType close_to_one_;
2591 static RawType further_from_one_;
2592
2593 static RawType infinity_;
2594 static RawType close_to_infinity_;
2595 static RawType further_from_infinity_;
2596
2597 static RawType nan1_;
2598 static RawType nan2_;
2599};
2600
2601template <typename RawType>
2602RawType FloatingPointTest<RawType>::close_to_positive_zero_;
2603
2604template <typename RawType>
2605RawType FloatingPointTest<RawType>::close_to_negative_zero_;
2606
2607template <typename RawType>
2608RawType FloatingPointTest<RawType>::further_from_negative_zero_;
2609
2610template <typename RawType>
2611RawType FloatingPointTest<RawType>::close_to_one_;
2612
2613template <typename RawType>
2614RawType FloatingPointTest<RawType>::further_from_one_;
2615
2616template <typename RawType>
2617RawType FloatingPointTest<RawType>::infinity_;
2618
2619template <typename RawType>
2620RawType FloatingPointTest<RawType>::close_to_infinity_;
2621
2622template <typename RawType>
2623RawType FloatingPointTest<RawType>::further_from_infinity_;
2624
2625template <typename RawType>
2626RawType FloatingPointTest<RawType>::nan1_;
2627
2628template <typename RawType>
2629RawType FloatingPointTest<RawType>::nan2_;
2630
2631// Instantiate FloatingPointTest for testing floats.
2632typedef FloatingPointTest<float> FloatTest;
2633
2634TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) {
2635 TestMatches(&FloatEq);
2636}
2637
2638TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
2639 TestMatches(&NanSensitiveFloatEq);
2640}
2641
2642TEST_F(FloatTest, FloatEqCannotMatchNaN) {
2643 // FloatEq never matches NaN.
2644 Matcher<float> m = FloatEq(nan1_);
2645 EXPECT_FALSE(m.Matches(nan1_));
2646 EXPECT_FALSE(m.Matches(nan2_));
2647 EXPECT_FALSE(m.Matches(1.0));
2648}
2649
2650TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {
2651 // NanSensitiveFloatEq will match NaN.
2652 Matcher<float> m = NanSensitiveFloatEq(nan1_);
2653 EXPECT_TRUE(m.Matches(nan1_));
2654 EXPECT_TRUE(m.Matches(nan2_));
2655 EXPECT_FALSE(m.Matches(1.0));
2656}
2657
2658TEST_F(FloatTest, FloatEqCanDescribeSelf) {
2659 Matcher<float> m1 = FloatEq(2.0f);
2660 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002661 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002662
2663 Matcher<float> m2 = FloatEq(0.5f);
2664 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002665 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002666
2667 Matcher<float> m3 = FloatEq(nan1_);
2668 EXPECT_EQ("never matches", Describe(m3));
2669 EXPECT_EQ("is anything", DescribeNegation(m3));
2670}
2671
2672TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {
2673 Matcher<float> m1 = NanSensitiveFloatEq(2.0f);
2674 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002675 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002676
2677 Matcher<float> m2 = NanSensitiveFloatEq(0.5f);
2678 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002679 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002680
2681 Matcher<float> m3 = NanSensitiveFloatEq(nan1_);
2682 EXPECT_EQ("is NaN", Describe(m3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002683 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
shiqiane35fdd92008-12-10 05:08:54 +00002684}
2685
2686// Instantiate FloatingPointTest for testing doubles.
2687typedef FloatingPointTest<double> DoubleTest;
2688
2689TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
2690 TestMatches(&DoubleEq);
2691}
2692
2693TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
2694 TestMatches(&NanSensitiveDoubleEq);
2695}
2696
2697TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {
2698 // DoubleEq never matches NaN.
2699 Matcher<double> m = DoubleEq(nan1_);
2700 EXPECT_FALSE(m.Matches(nan1_));
2701 EXPECT_FALSE(m.Matches(nan2_));
2702 EXPECT_FALSE(m.Matches(1.0));
2703}
2704
2705TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {
2706 // NanSensitiveDoubleEq will match NaN.
2707 Matcher<double> m = NanSensitiveDoubleEq(nan1_);
2708 EXPECT_TRUE(m.Matches(nan1_));
2709 EXPECT_TRUE(m.Matches(nan2_));
2710 EXPECT_FALSE(m.Matches(1.0));
2711}
2712
2713TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {
2714 Matcher<double> m1 = DoubleEq(2.0);
2715 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002716 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002717
2718 Matcher<double> m2 = DoubleEq(0.5);
2719 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002720 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002721
2722 Matcher<double> m3 = DoubleEq(nan1_);
2723 EXPECT_EQ("never matches", Describe(m3));
2724 EXPECT_EQ("is anything", DescribeNegation(m3));
2725}
2726
2727TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {
2728 Matcher<double> m1 = NanSensitiveDoubleEq(2.0);
2729 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002730 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002731
2732 Matcher<double> m2 = NanSensitiveDoubleEq(0.5);
2733 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002734 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002735
2736 Matcher<double> m3 = NanSensitiveDoubleEq(nan1_);
2737 EXPECT_EQ("is NaN", Describe(m3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002738 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
shiqiane35fdd92008-12-10 05:08:54 +00002739}
2740
2741TEST(PointeeTest, RawPointer) {
2742 const Matcher<int*> m = Pointee(Ge(0));
2743
2744 int n = 1;
2745 EXPECT_TRUE(m.Matches(&n));
2746 n = -1;
2747 EXPECT_FALSE(m.Matches(&n));
2748 EXPECT_FALSE(m.Matches(NULL));
2749}
2750
2751TEST(PointeeTest, RawPointerToConst) {
2752 const Matcher<const double*> m = Pointee(Ge(0));
2753
2754 double x = 1;
2755 EXPECT_TRUE(m.Matches(&x));
2756 x = -1;
2757 EXPECT_FALSE(m.Matches(&x));
2758 EXPECT_FALSE(m.Matches(NULL));
2759}
2760
2761TEST(PointeeTest, ReferenceToConstRawPointer) {
2762 const Matcher<int* const &> m = Pointee(Ge(0));
2763
2764 int n = 1;
2765 EXPECT_TRUE(m.Matches(&n));
2766 n = -1;
2767 EXPECT_FALSE(m.Matches(&n));
2768 EXPECT_FALSE(m.Matches(NULL));
2769}
2770
2771TEST(PointeeTest, ReferenceToNonConstRawPointer) {
2772 const Matcher<double* &> m = Pointee(Ge(0));
2773
2774 double x = 1.0;
2775 double* p = &x;
2776 EXPECT_TRUE(m.Matches(p));
2777 x = -1;
2778 EXPECT_FALSE(m.Matches(p));
2779 p = NULL;
2780 EXPECT_FALSE(m.Matches(p));
2781}
2782
2783TEST(PointeeTest, NeverMatchesNull) {
2784 const Matcher<const char*> m = Pointee(_);
2785 EXPECT_FALSE(m.Matches(NULL));
2786}
2787
2788// Tests that we can write Pointee(value) instead of Pointee(Eq(value)).
2789TEST(PointeeTest, MatchesAgainstAValue) {
2790 const Matcher<int*> m = Pointee(5);
2791
2792 int n = 5;
2793 EXPECT_TRUE(m.Matches(&n));
2794 n = -1;
2795 EXPECT_FALSE(m.Matches(&n));
2796 EXPECT_FALSE(m.Matches(NULL));
2797}
2798
2799TEST(PointeeTest, CanDescribeSelf) {
2800 const Matcher<int*> m = Pointee(Gt(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002801 EXPECT_EQ("points to a value that is > 3", Describe(m));
2802 EXPECT_EQ("does not point to a value that is > 3",
shiqiane35fdd92008-12-10 05:08:54 +00002803 DescribeNegation(m));
2804}
2805
shiqiane35fdd92008-12-10 05:08:54 +00002806TEST(PointeeTest, CanExplainMatchResult) {
2807 const Matcher<const string*> m = Pointee(StartsWith("Hi"));
2808
2809 EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL)));
2810
zhanyong.wan736baa82010-09-27 17:44:16 +00002811 const Matcher<long*> m2 = Pointee(GreaterThan(1)); // NOLINT
2812 long n = 3; // NOLINT
2813 EXPECT_EQ("which points to 3" + OfType("long") + ", which is 2 more than 1",
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002814 Explain(m2, &n));
2815}
2816
2817TEST(PointeeTest, AlwaysExplainsPointee) {
2818 const Matcher<int*> m = Pointee(0);
2819 int n = 42;
zhanyong.wan736baa82010-09-27 17:44:16 +00002820 EXPECT_EQ("which points to 42" + OfType("int"), Explain(m, &n));
shiqiane35fdd92008-12-10 05:08:54 +00002821}
2822
2823// An uncopyable class.
2824class Uncopyable {
2825 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002826 explicit Uncopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00002827
2828 int value() const { return value_; }
2829 private:
2830 const int value_;
2831 GTEST_DISALLOW_COPY_AND_ASSIGN_(Uncopyable);
2832};
2833
2834// Returns true iff x.value() is positive.
2835bool ValueIsPositive(const Uncopyable& x) { return x.value() > 0; }
2836
2837// A user-defined struct for testing Field().
2838struct AStruct {
2839 AStruct() : x(0), y(1.0), z(5), p(NULL) {}
2840 AStruct(const AStruct& rhs)
2841 : x(rhs.x), y(rhs.y), z(rhs.z.value()), p(rhs.p) {}
2842
2843 int x; // A non-const field.
2844 const double y; // A const field.
2845 Uncopyable z; // An uncopyable field.
2846 const char* p; // A pointer field.
zhanyong.wan32de5f52009-12-23 00:13:23 +00002847
2848 private:
2849 GTEST_DISALLOW_ASSIGN_(AStruct);
shiqiane35fdd92008-12-10 05:08:54 +00002850};
2851
2852// A derived struct for testing Field().
2853struct DerivedStruct : public AStruct {
2854 char ch;
zhanyong.wan32de5f52009-12-23 00:13:23 +00002855
2856 private:
2857 GTEST_DISALLOW_ASSIGN_(DerivedStruct);
shiqiane35fdd92008-12-10 05:08:54 +00002858};
2859
2860// Tests that Field(&Foo::field, ...) works when field is non-const.
2861TEST(FieldTest, WorksForNonConstField) {
2862 Matcher<AStruct> m = Field(&AStruct::x, Ge(0));
2863
2864 AStruct a;
2865 EXPECT_TRUE(m.Matches(a));
2866 a.x = -1;
2867 EXPECT_FALSE(m.Matches(a));
2868}
2869
2870// Tests that Field(&Foo::field, ...) works when field is const.
2871TEST(FieldTest, WorksForConstField) {
2872 AStruct a;
2873
2874 Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0));
2875 EXPECT_TRUE(m.Matches(a));
2876 m = Field(&AStruct::y, Le(0.0));
2877 EXPECT_FALSE(m.Matches(a));
2878}
2879
2880// Tests that Field(&Foo::field, ...) works when field is not copyable.
2881TEST(FieldTest, WorksForUncopyableField) {
2882 AStruct a;
2883
2884 Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive));
2885 EXPECT_TRUE(m.Matches(a));
2886 m = Field(&AStruct::z, Not(Truly(ValueIsPositive)));
2887 EXPECT_FALSE(m.Matches(a));
2888}
2889
2890// Tests that Field(&Foo::field, ...) works when field is a pointer.
2891TEST(FieldTest, WorksForPointerField) {
2892 // Matching against NULL.
2893 Matcher<AStruct> m = Field(&AStruct::p, static_cast<const char*>(NULL));
2894 AStruct a;
2895 EXPECT_TRUE(m.Matches(a));
2896 a.p = "hi";
2897 EXPECT_FALSE(m.Matches(a));
2898
2899 // Matching a pointer that is not NULL.
2900 m = Field(&AStruct::p, StartsWith("hi"));
2901 a.p = "hill";
2902 EXPECT_TRUE(m.Matches(a));
2903 a.p = "hole";
2904 EXPECT_FALSE(m.Matches(a));
2905}
2906
2907// Tests that Field() works when the object is passed by reference.
2908TEST(FieldTest, WorksForByRefArgument) {
2909 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2910
2911 AStruct a;
2912 EXPECT_TRUE(m.Matches(a));
2913 a.x = -1;
2914 EXPECT_FALSE(m.Matches(a));
2915}
2916
2917// Tests that Field(&Foo::field, ...) works when the argument's type
2918// is a sub-type of Foo.
2919TEST(FieldTest, WorksForArgumentOfSubType) {
2920 // Note that the matcher expects DerivedStruct but we say AStruct
2921 // inside Field().
2922 Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0));
2923
2924 DerivedStruct d;
2925 EXPECT_TRUE(m.Matches(d));
2926 d.x = -1;
2927 EXPECT_FALSE(m.Matches(d));
2928}
2929
2930// Tests that Field(&Foo::field, m) works when field's type and m's
2931// argument type are compatible but not the same.
2932TEST(FieldTest, WorksForCompatibleMatcherType) {
2933 // The field is an int, but the inner matcher expects a signed char.
2934 Matcher<const AStruct&> m = Field(&AStruct::x,
2935 Matcher<signed char>(Ge(0)));
2936
2937 AStruct a;
2938 EXPECT_TRUE(m.Matches(a));
2939 a.x = -1;
2940 EXPECT_FALSE(m.Matches(a));
2941}
2942
2943// Tests that Field() can describe itself.
2944TEST(FieldTest, CanDescribeSelf) {
2945 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2946
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002947 EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
2948 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00002949}
2950
2951// Tests that Field() can explain the match result.
2952TEST(FieldTest, CanExplainMatchResult) {
2953 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2954
2955 AStruct a;
2956 a.x = 1;
zhanyong.wan736baa82010-09-27 17:44:16 +00002957 EXPECT_EQ("whose given field is 1" + OfType("int"), Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00002958
2959 m = Field(&AStruct::x, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00002960 EXPECT_EQ(
2961 "whose given field is 1" + OfType("int") + ", which is 1 more than 0",
2962 Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00002963}
2964
2965// Tests that Field() works when the argument is a pointer to const.
2966TEST(FieldForPointerTest, WorksForPointerToConst) {
2967 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2968
2969 AStruct a;
2970 EXPECT_TRUE(m.Matches(&a));
2971 a.x = -1;
2972 EXPECT_FALSE(m.Matches(&a));
2973}
2974
2975// Tests that Field() works when the argument is a pointer to non-const.
2976TEST(FieldForPointerTest, WorksForPointerToNonConst) {
2977 Matcher<AStruct*> m = Field(&AStruct::x, Ge(0));
2978
2979 AStruct a;
2980 EXPECT_TRUE(m.Matches(&a));
2981 a.x = -1;
2982 EXPECT_FALSE(m.Matches(&a));
2983}
2984
zhanyong.wan6953a722010-01-13 05:15:07 +00002985// Tests that Field() works when the argument is a reference to a const pointer.
2986TEST(FieldForPointerTest, WorksForReferenceToConstPointer) {
2987 Matcher<AStruct* const&> m = Field(&AStruct::x, Ge(0));
2988
2989 AStruct a;
2990 EXPECT_TRUE(m.Matches(&a));
2991 a.x = -1;
2992 EXPECT_FALSE(m.Matches(&a));
2993}
2994
shiqiane35fdd92008-12-10 05:08:54 +00002995// Tests that Field() does not match the NULL pointer.
2996TEST(FieldForPointerTest, DoesNotMatchNull) {
2997 Matcher<const AStruct*> m = Field(&AStruct::x, _);
2998 EXPECT_FALSE(m.Matches(NULL));
2999}
3000
3001// Tests that Field(&Foo::field, ...) works when the argument's type
3002// is a sub-type of const Foo*.
3003TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
3004 // Note that the matcher expects DerivedStruct but we say AStruct
3005 // inside Field().
3006 Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0));
3007
3008 DerivedStruct d;
3009 EXPECT_TRUE(m.Matches(&d));
3010 d.x = -1;
3011 EXPECT_FALSE(m.Matches(&d));
3012}
3013
3014// Tests that Field() can describe itself when used to match a pointer.
3015TEST(FieldForPointerTest, CanDescribeSelf) {
3016 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
3017
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003018 EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
3019 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003020}
3021
3022// Tests that Field() can explain the result of matching a pointer.
3023TEST(FieldForPointerTest, CanExplainMatchResult) {
3024 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
3025
3026 AStruct a;
3027 a.x = 1;
3028 EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(NULL)));
zhanyong.wan736baa82010-09-27 17:44:16 +00003029 EXPECT_EQ("which points to an object whose given field is 1" + OfType("int"),
3030 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003031
3032 m = Field(&AStruct::x, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003033 EXPECT_EQ("which points to an object whose given field is 1" + OfType("int") +
3034 ", which is 1 more than 0", Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003035}
3036
3037// A user-defined class for testing Property().
3038class AClass {
3039 public:
3040 AClass() : n_(0) {}
3041
3042 // A getter that returns a non-reference.
3043 int n() const { return n_; }
3044
3045 void set_n(int new_n) { n_ = new_n; }
3046
3047 // A getter that returns a reference to const.
3048 const string& s() const { return s_; }
3049
3050 void set_s(const string& new_s) { s_ = new_s; }
3051
3052 // A getter that returns a reference to non-const.
3053 double& x() const { return x_; }
3054 private:
3055 int n_;
3056 string s_;
3057
3058 static double x_;
3059};
3060
3061double AClass::x_ = 0.0;
3062
3063// A derived class for testing Property().
3064class DerivedClass : public AClass {
3065 private:
3066 int k_;
3067};
3068
3069// Tests that Property(&Foo::property, ...) works when property()
3070// returns a non-reference.
3071TEST(PropertyTest, WorksForNonReferenceProperty) {
3072 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3073
3074 AClass a;
3075 a.set_n(1);
3076 EXPECT_TRUE(m.Matches(a));
3077
3078 a.set_n(-1);
3079 EXPECT_FALSE(m.Matches(a));
3080}
3081
3082// Tests that Property(&Foo::property, ...) works when property()
3083// returns a reference to const.
3084TEST(PropertyTest, WorksForReferenceToConstProperty) {
3085 Matcher<const AClass&> m = Property(&AClass::s, StartsWith("hi"));
3086
3087 AClass a;
3088 a.set_s("hill");
3089 EXPECT_TRUE(m.Matches(a));
3090
3091 a.set_s("hole");
3092 EXPECT_FALSE(m.Matches(a));
3093}
3094
3095// Tests that Property(&Foo::property, ...) works when property()
3096// returns a reference to non-const.
3097TEST(PropertyTest, WorksForReferenceToNonConstProperty) {
3098 double x = 0.0;
3099 AClass a;
3100
3101 Matcher<const AClass&> m = Property(&AClass::x, Ref(x));
3102 EXPECT_FALSE(m.Matches(a));
3103
3104 m = Property(&AClass::x, Not(Ref(x)));
3105 EXPECT_TRUE(m.Matches(a));
3106}
3107
3108// Tests that Property(&Foo::property, ...) works when the argument is
3109// passed by value.
3110TEST(PropertyTest, WorksForByValueArgument) {
3111 Matcher<AClass> m = Property(&AClass::s, StartsWith("hi"));
3112
3113 AClass a;
3114 a.set_s("hill");
3115 EXPECT_TRUE(m.Matches(a));
3116
3117 a.set_s("hole");
3118 EXPECT_FALSE(m.Matches(a));
3119}
3120
3121// Tests that Property(&Foo::property, ...) works when the argument's
3122// type is a sub-type of Foo.
3123TEST(PropertyTest, WorksForArgumentOfSubType) {
3124 // The matcher expects a DerivedClass, but inside the Property() we
3125 // say AClass.
3126 Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0));
3127
3128 DerivedClass d;
3129 d.set_n(1);
3130 EXPECT_TRUE(m.Matches(d));
3131
3132 d.set_n(-1);
3133 EXPECT_FALSE(m.Matches(d));
3134}
3135
3136// Tests that Property(&Foo::property, m) works when property()'s type
3137// and m's argument type are compatible but different.
3138TEST(PropertyTest, WorksForCompatibleMatcherType) {
3139 // n() returns an int but the inner matcher expects a signed char.
3140 Matcher<const AClass&> m = Property(&AClass::n,
3141 Matcher<signed char>(Ge(0)));
3142
3143 AClass a;
3144 EXPECT_TRUE(m.Matches(a));
3145 a.set_n(-1);
3146 EXPECT_FALSE(m.Matches(a));
3147}
3148
3149// Tests that Property() can describe itself.
3150TEST(PropertyTest, CanDescribeSelf) {
3151 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3152
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003153 EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
3154 EXPECT_EQ("is an object whose given property isn't >= 0",
3155 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003156}
3157
3158// Tests that Property() can explain the match result.
3159TEST(PropertyTest, CanExplainMatchResult) {
3160 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3161
3162 AClass a;
3163 a.set_n(1);
zhanyong.wan736baa82010-09-27 17:44:16 +00003164 EXPECT_EQ("whose given property is 1" + OfType("int"), Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003165
3166 m = Property(&AClass::n, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003167 EXPECT_EQ(
3168 "whose given property is 1" + OfType("int") + ", which is 1 more than 0",
3169 Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003170}
3171
3172// Tests that Property() works when the argument is a pointer to const.
3173TEST(PropertyForPointerTest, WorksForPointerToConst) {
3174 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3175
3176 AClass a;
3177 a.set_n(1);
3178 EXPECT_TRUE(m.Matches(&a));
3179
3180 a.set_n(-1);
3181 EXPECT_FALSE(m.Matches(&a));
3182}
3183
3184// Tests that Property() works when the argument is a pointer to non-const.
3185TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
3186 Matcher<AClass*> m = Property(&AClass::s, StartsWith("hi"));
3187
3188 AClass a;
3189 a.set_s("hill");
3190 EXPECT_TRUE(m.Matches(&a));
3191
3192 a.set_s("hole");
3193 EXPECT_FALSE(m.Matches(&a));
3194}
3195
zhanyong.wan6953a722010-01-13 05:15:07 +00003196// Tests that Property() works when the argument is a reference to a
3197// const pointer.
3198TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) {
3199 Matcher<AClass* const&> m = Property(&AClass::s, StartsWith("hi"));
3200
3201 AClass a;
3202 a.set_s("hill");
3203 EXPECT_TRUE(m.Matches(&a));
3204
3205 a.set_s("hole");
3206 EXPECT_FALSE(m.Matches(&a));
3207}
3208
shiqiane35fdd92008-12-10 05:08:54 +00003209// Tests that Property() does not match the NULL pointer.
3210TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
3211 Matcher<const AClass*> m = Property(&AClass::x, _);
3212 EXPECT_FALSE(m.Matches(NULL));
3213}
3214
3215// Tests that Property(&Foo::property, ...) works when the argument's
3216// type is a sub-type of const Foo*.
3217TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
3218 // The matcher expects a DerivedClass, but inside the Property() we
3219 // say AClass.
3220 Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0));
3221
3222 DerivedClass d;
3223 d.set_n(1);
3224 EXPECT_TRUE(m.Matches(&d));
3225
3226 d.set_n(-1);
3227 EXPECT_FALSE(m.Matches(&d));
3228}
3229
3230// Tests that Property() can describe itself when used to match a pointer.
3231TEST(PropertyForPointerTest, CanDescribeSelf) {
3232 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3233
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003234 EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
3235 EXPECT_EQ("is an object whose given property isn't >= 0",
3236 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003237}
3238
3239// Tests that Property() can explain the result of matching a pointer.
3240TEST(PropertyForPointerTest, CanExplainMatchResult) {
3241 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3242
3243 AClass a;
3244 a.set_n(1);
3245 EXPECT_EQ("", Explain(m, static_cast<const AClass*>(NULL)));
zhanyong.wan736baa82010-09-27 17:44:16 +00003246 EXPECT_EQ(
3247 "which points to an object whose given property is 1" + OfType("int"),
3248 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003249
3250 m = Property(&AClass::n, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003251 EXPECT_EQ("which points to an object whose given property is 1" +
3252 OfType("int") + ", which is 1 more than 0",
3253 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003254}
3255
3256// Tests ResultOf.
3257
3258// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3259// function pointer.
3260string IntToStringFunction(int input) { return input == 1 ? "foo" : "bar"; }
3261
3262TEST(ResultOfTest, WorksForFunctionPointers) {
3263 Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(string("foo")));
3264
3265 EXPECT_TRUE(matcher.Matches(1));
3266 EXPECT_FALSE(matcher.Matches(2));
3267}
3268
3269// Tests that ResultOf() can describe itself.
3270TEST(ResultOfTest, CanDescribeItself) {
3271 Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo"));
3272
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003273 EXPECT_EQ("is mapped by the given callable to a value that "
3274 "is equal to \"foo\"", Describe(matcher));
3275 EXPECT_EQ("is mapped by the given callable to a value that "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003276 "isn't equal to \"foo\"", DescribeNegation(matcher));
shiqiane35fdd92008-12-10 05:08:54 +00003277}
3278
3279// Tests that ResultOf() can explain the match result.
3280int IntFunction(int input) { return input == 42 ? 80 : 90; }
3281
3282TEST(ResultOfTest, CanExplainMatchResult) {
3283 Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
zhanyong.wan736baa82010-09-27 17:44:16 +00003284 EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int"),
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003285 Explain(matcher, 36));
shiqiane35fdd92008-12-10 05:08:54 +00003286
3287 matcher = ResultOf(&IntFunction, GreaterThan(85));
zhanyong.wan736baa82010-09-27 17:44:16 +00003288 EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int") +
3289 ", which is 5 more than 85", Explain(matcher, 36));
shiqiane35fdd92008-12-10 05:08:54 +00003290}
3291
3292// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3293// returns a non-reference.
3294TEST(ResultOfTest, WorksForNonReferenceResults) {
3295 Matcher<int> matcher = ResultOf(&IntFunction, Eq(80));
3296
3297 EXPECT_TRUE(matcher.Matches(42));
3298 EXPECT_FALSE(matcher.Matches(36));
3299}
3300
3301// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3302// returns a reference to non-const.
zhanyong.wan736baa82010-09-27 17:44:16 +00003303double& DoubleFunction(double& input) { return input; } // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00003304
zhanyong.wan736baa82010-09-27 17:44:16 +00003305Uncopyable& RefUncopyableFunction(Uncopyable& obj) { // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00003306 return obj;
3307}
3308
3309TEST(ResultOfTest, WorksForReferenceToNonConstResults) {
3310 double x = 3.14;
3311 double x2 = x;
3312 Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x));
3313
3314 EXPECT_TRUE(matcher.Matches(x));
3315 EXPECT_FALSE(matcher.Matches(x2));
3316
3317 // Test that ResultOf works with uncopyable objects
3318 Uncopyable obj(0);
3319 Uncopyable obj2(0);
3320 Matcher<Uncopyable&> matcher2 =
3321 ResultOf(&RefUncopyableFunction, Ref(obj));
3322
3323 EXPECT_TRUE(matcher2.Matches(obj));
3324 EXPECT_FALSE(matcher2.Matches(obj2));
3325}
3326
3327// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3328// returns a reference to const.
3329const string& StringFunction(const string& input) { return input; }
3330
3331TEST(ResultOfTest, WorksForReferenceToConstResults) {
3332 string s = "foo";
3333 string s2 = s;
3334 Matcher<const string&> matcher = ResultOf(&StringFunction, Ref(s));
3335
3336 EXPECT_TRUE(matcher.Matches(s));
3337 EXPECT_FALSE(matcher.Matches(s2));
3338}
3339
3340// Tests that ResultOf(f, m) works when f(x) and m's
3341// argument types are compatible but different.
3342TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {
3343 // IntFunction() returns int but the inner matcher expects a signed char.
3344 Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85)));
3345
3346 EXPECT_TRUE(matcher.Matches(36));
3347 EXPECT_FALSE(matcher.Matches(42));
3348}
3349
shiqiane35fdd92008-12-10 05:08:54 +00003350// Tests that the program aborts when ResultOf is passed
3351// a NULL function pointer.
3352TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +00003353 EXPECT_DEATH_IF_SUPPORTED(
zhanyong.wan736baa82010-09-27 17:44:16 +00003354 ResultOf(static_cast<string(*)(int dummy)>(NULL), Eq(string("foo"))),
shiqiane35fdd92008-12-10 05:08:54 +00003355 "NULL function pointer is passed into ResultOf\\(\\)\\.");
3356}
shiqiane35fdd92008-12-10 05:08:54 +00003357
3358// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3359// function reference.
3360TEST(ResultOfTest, WorksForFunctionReferences) {
3361 Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq("foo"));
3362 EXPECT_TRUE(matcher.Matches(1));
3363 EXPECT_FALSE(matcher.Matches(2));
3364}
3365
3366// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3367// function object.
3368struct Functor : public ::std::unary_function<int, string> {
3369 result_type operator()(argument_type input) const {
3370 return IntToStringFunction(input);
3371 }
3372};
3373
3374TEST(ResultOfTest, WorksForFunctors) {
3375 Matcher<int> matcher = ResultOf(Functor(), Eq(string("foo")));
3376
3377 EXPECT_TRUE(matcher.Matches(1));
3378 EXPECT_FALSE(matcher.Matches(2));
3379}
3380
3381// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3382// functor with more then one operator() defined. ResultOf() must work
3383// for each defined operator().
3384struct PolymorphicFunctor {
3385 typedef int result_type;
3386 int operator()(int n) { return n; }
3387 int operator()(const char* s) { return static_cast<int>(strlen(s)); }
3388};
3389
3390TEST(ResultOfTest, WorksForPolymorphicFunctors) {
3391 Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5));
3392
3393 EXPECT_TRUE(matcher_int.Matches(10));
3394 EXPECT_FALSE(matcher_int.Matches(2));
3395
3396 Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5));
3397
3398 EXPECT_TRUE(matcher_string.Matches("long string"));
3399 EXPECT_FALSE(matcher_string.Matches("shrt"));
3400}
3401
3402const int* ReferencingFunction(const int& n) { return &n; }
3403
3404struct ReferencingFunctor {
3405 typedef const int* result_type;
3406 result_type operator()(const int& n) { return &n; }
3407};
3408
3409TEST(ResultOfTest, WorksForReferencingCallables) {
3410 const int n = 1;
3411 const int n2 = 1;
3412 Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n));
3413 EXPECT_TRUE(matcher2.Matches(n));
3414 EXPECT_FALSE(matcher2.Matches(n2));
3415
3416 Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n));
3417 EXPECT_TRUE(matcher3.Matches(n));
3418 EXPECT_FALSE(matcher3.Matches(n2));
3419}
3420
shiqiane35fdd92008-12-10 05:08:54 +00003421class DivisibleByImpl {
3422 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003423 explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {}
shiqiane35fdd92008-12-10 05:08:54 +00003424
zhanyong.wandb22c222010-01-28 21:52:29 +00003425 // For testing using ExplainMatchResultTo() with polymorphic matchers.
shiqiane35fdd92008-12-10 05:08:54 +00003426 template <typename T>
zhanyong.wandb22c222010-01-28 21:52:29 +00003427 bool MatchAndExplain(const T& n, MatchResultListener* listener) const {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003428 *listener << "which is " << (n % divider_) << " modulo "
zhanyong.wandb22c222010-01-28 21:52:29 +00003429 << divider_;
shiqiane35fdd92008-12-10 05:08:54 +00003430 return (n % divider_) == 0;
3431 }
3432
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003433 void DescribeTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +00003434 *os << "is divisible by " << divider_;
3435 }
3436
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003437 void DescribeNegationTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +00003438 *os << "is not divisible by " << divider_;
3439 }
3440
zhanyong.wan32de5f52009-12-23 00:13:23 +00003441 void set_divider(int a_divider) { divider_ = a_divider; }
shiqiane35fdd92008-12-10 05:08:54 +00003442 int divider() const { return divider_; }
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003443
shiqiane35fdd92008-12-10 05:08:54 +00003444 private:
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003445 int divider_;
shiqiane35fdd92008-12-10 05:08:54 +00003446};
3447
shiqiane35fdd92008-12-10 05:08:54 +00003448PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) {
3449 return MakePolymorphicMatcher(DivisibleByImpl(n));
3450}
3451
3452// Tests that when AllOf() fails, only the first failing matcher is
3453// asked to explain why.
3454TEST(ExplainMatchResultTest, AllOf_False_False) {
3455 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003456 EXPECT_EQ("which is 1 modulo 4", Explain(m, 5));
shiqiane35fdd92008-12-10 05:08:54 +00003457}
3458
3459// Tests that when AllOf() fails, only the first failing matcher is
3460// asked to explain why.
3461TEST(ExplainMatchResultTest, AllOf_False_True) {
3462 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003463 EXPECT_EQ("which is 2 modulo 4", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003464}
3465
3466// Tests that when AllOf() fails, only the first failing matcher is
3467// asked to explain why.
3468TEST(ExplainMatchResultTest, AllOf_True_False) {
3469 const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003470 EXPECT_EQ("which is 2 modulo 3", Explain(m, 5));
shiqiane35fdd92008-12-10 05:08:54 +00003471}
3472
3473// Tests that when AllOf() succeeds, all matchers are asked to explain
3474// why.
3475TEST(ExplainMatchResultTest, AllOf_True_True) {
3476 const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003477 EXPECT_EQ("which is 0 modulo 2, and which is 0 modulo 3", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003478}
3479
3480TEST(ExplainMatchResultTest, AllOf_True_True_2) {
3481 const Matcher<int> m = AllOf(Ge(2), Le(3));
3482 EXPECT_EQ("", Explain(m, 2));
3483}
3484
3485TEST(ExplainmatcherResultTest, MonomorphicMatcher) {
3486 const Matcher<int> m = GreaterThan(5);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003487 EXPECT_EQ("which is 1 more than 5", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003488}
3489
3490// The following two tests verify that values without a public copy
3491// ctor can be used as arguments to matchers like Eq(), Ge(), and etc
3492// with the help of ByRef().
3493
3494class NotCopyable {
3495 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003496 explicit NotCopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00003497
3498 int value() const { return value_; }
3499
3500 bool operator==(const NotCopyable& rhs) const {
3501 return value() == rhs.value();
3502 }
3503
3504 bool operator>=(const NotCopyable& rhs) const {
3505 return value() >= rhs.value();
3506 }
3507 private:
3508 int value_;
3509
3510 GTEST_DISALLOW_COPY_AND_ASSIGN_(NotCopyable);
3511};
3512
3513TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) {
3514 const NotCopyable const_value1(1);
3515 const Matcher<const NotCopyable&> m = Eq(ByRef(const_value1));
3516
3517 const NotCopyable n1(1), n2(2);
3518 EXPECT_TRUE(m.Matches(n1));
3519 EXPECT_FALSE(m.Matches(n2));
3520}
3521
3522TEST(ByRefTest, AllowsNotCopyableValueInMatchers) {
3523 NotCopyable value2(2);
3524 const Matcher<NotCopyable&> m = Ge(ByRef(value2));
3525
3526 NotCopyable n1(1), n2(2);
3527 EXPECT_FALSE(m.Matches(n1));
3528 EXPECT_TRUE(m.Matches(n2));
3529}
3530
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003531#if GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003532// Tests ContainerEq with different container types, and
3533// different element types.
3534
3535template <typename T>
zhanyong.wanb8243162009-06-04 05:48:20 +00003536class ContainerEqTest : public testing::Test {};
zhanyong.wan6a896b52009-01-16 01:13:50 +00003537
3538typedef testing::Types<
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003539 set<int>,
3540 vector<size_t>,
3541 multiset<size_t>,
3542 list<int> >
zhanyong.wan6a896b52009-01-16 01:13:50 +00003543 ContainerEqTestTypes;
3544
3545TYPED_TEST_CASE(ContainerEqTest, ContainerEqTestTypes);
3546
3547// Tests that the filled container is equal to itself.
3548TYPED_TEST(ContainerEqTest, EqualsSelf) {
3549 static const int vals[] = {1, 1, 2, 3, 5, 8};
3550 TypeParam my_set(vals, vals + 6);
3551 const Matcher<TypeParam> m = ContainerEq(my_set);
3552 EXPECT_TRUE(m.Matches(my_set));
3553 EXPECT_EQ("", Explain(m, my_set));
3554}
3555
3556// Tests that missing values are reported.
3557TYPED_TEST(ContainerEqTest, ValueMissing) {
3558 static const int vals[] = {1, 1, 2, 3, 5, 8};
3559 static const int test_vals[] = {2, 1, 8, 5};
3560 TypeParam my_set(vals, vals + 6);
3561 TypeParam test_set(test_vals, test_vals + 4);
3562 const Matcher<TypeParam> m = ContainerEq(my_set);
3563 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003564 EXPECT_EQ("which doesn't have these expected elements: 3",
3565 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003566}
3567
3568// Tests that added values are reported.
3569TYPED_TEST(ContainerEqTest, ValueAdded) {
3570 static const int vals[] = {1, 1, 2, 3, 5, 8};
3571 static const int test_vals[] = {1, 2, 3, 5, 8, 46};
3572 TypeParam my_set(vals, vals + 6);
3573 TypeParam test_set(test_vals, test_vals + 6);
3574 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3575 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003576 EXPECT_EQ("which has these unexpected elements: 46", Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003577}
3578
3579// Tests that added and missing values are reported together.
3580TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) {
3581 static const int vals[] = {1, 1, 2, 3, 5, 8};
3582 static const int test_vals[] = {1, 2, 3, 8, 46};
3583 TypeParam my_set(vals, vals + 6);
3584 TypeParam test_set(test_vals, test_vals + 5);
3585 const Matcher<TypeParam> m = ContainerEq(my_set);
3586 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003587 EXPECT_EQ("which has these unexpected elements: 46,\n"
3588 "and doesn't have these expected elements: 5",
3589 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003590}
3591
3592// Tests duplicated value -- expect no explanation.
3593TYPED_TEST(ContainerEqTest, DuplicateDifference) {
3594 static const int vals[] = {1, 1, 2, 3, 5, 8};
3595 static const int test_vals[] = {1, 2, 3, 5, 8};
3596 TypeParam my_set(vals, vals + 6);
3597 TypeParam test_set(test_vals, test_vals + 5);
3598 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3599 // Depending on the container, match may be true or false
3600 // But in any case there should be no explanation.
3601 EXPECT_EQ("", Explain(m, test_set));
3602}
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003603#endif // GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003604
3605// Tests that mutliple missing values are reported.
3606// Using just vector here, so order is predicatble.
3607TEST(ContainerEqExtraTest, MultipleValuesMissing) {
3608 static const int vals[] = {1, 1, 2, 3, 5, 8};
3609 static const int test_vals[] = {2, 1, 5};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003610 vector<int> my_set(vals, vals + 6);
3611 vector<int> test_set(test_vals, test_vals + 3);
3612 const Matcher<vector<int> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003613 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003614 EXPECT_EQ("which doesn't have these expected elements: 3, 8",
3615 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003616}
3617
3618// Tests that added values are reported.
3619// Using just vector here, so order is predicatble.
3620TEST(ContainerEqExtraTest, MultipleValuesAdded) {
3621 static const int vals[] = {1, 1, 2, 3, 5, 8};
3622 static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003623 list<size_t> my_set(vals, vals + 6);
3624 list<size_t> test_set(test_vals, test_vals + 7);
3625 const Matcher<const list<size_t>&> m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003626 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003627 EXPECT_EQ("which has these unexpected elements: 92, 46",
3628 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003629}
3630
3631// Tests that added and missing values are reported together.
3632TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) {
3633 static const int vals[] = {1, 1, 2, 3, 5, 8};
3634 static const int test_vals[] = {1, 2, 3, 92, 46};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003635 list<size_t> my_set(vals, vals + 6);
3636 list<size_t> test_set(test_vals, test_vals + 5);
3637 const Matcher<const list<size_t> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003638 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003639 EXPECT_EQ("which has these unexpected elements: 92, 46,\n"
3640 "and doesn't have these expected elements: 5, 8",
zhanyong.wan6a896b52009-01-16 01:13:50 +00003641 Explain(m, test_set));
3642}
3643
3644// Tests to see that duplicate elements are detected,
3645// but (as above) not reported in the explanation.
3646TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) {
3647 static const int vals[] = {1, 1, 2, 3, 5, 8};
3648 static const int test_vals[] = {1, 2, 3, 5, 8};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003649 vector<int> my_set(vals, vals + 6);
3650 vector<int> test_set(test_vals, test_vals + 5);
3651 const Matcher<vector<int> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003652 EXPECT_TRUE(m.Matches(my_set));
3653 EXPECT_FALSE(m.Matches(test_set));
3654 // There is nothing to report when both sets contain all the same values.
3655 EXPECT_EQ("", Explain(m, test_set));
3656}
3657
3658// Tests that ContainerEq works for non-trivial associative containers,
3659// like maps.
3660TEST(ContainerEqExtraTest, WorksForMaps) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003661 map<int, std::string> my_map;
zhanyong.wan6a896b52009-01-16 01:13:50 +00003662 my_map[0] = "a";
3663 my_map[1] = "b";
3664
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003665 map<int, std::string> test_map;
zhanyong.wan6a896b52009-01-16 01:13:50 +00003666 test_map[0] = "aa";
3667 test_map[1] = "b";
3668
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003669 const Matcher<const map<int, std::string>&> m = ContainerEq(my_map);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003670 EXPECT_TRUE(m.Matches(my_map));
3671 EXPECT_FALSE(m.Matches(test_map));
3672
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003673 EXPECT_EQ("which has these unexpected elements: (0, \"aa\"),\n"
3674 "and doesn't have these expected elements: (0, \"a\")",
zhanyong.wan6a896b52009-01-16 01:13:50 +00003675 Explain(m, test_map));
3676}
3677
zhanyong.wanb8243162009-06-04 05:48:20 +00003678TEST(ContainerEqExtraTest, WorksForNativeArray) {
3679 int a1[] = { 1, 2, 3 };
3680 int a2[] = { 1, 2, 3 };
3681 int b[] = { 1, 2, 4 };
3682
3683 EXPECT_THAT(a1, ContainerEq(a2));
3684 EXPECT_THAT(a1, Not(ContainerEq(b)));
3685}
3686
3687TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) {
3688 const char a1[][3] = { "hi", "lo" };
3689 const char a2[][3] = { "hi", "lo" };
3690 const char b[][3] = { "lo", "hi" };
3691
3692 // Tests using ContainerEq() in the first dimension.
3693 EXPECT_THAT(a1, ContainerEq(a2));
3694 EXPECT_THAT(a1, Not(ContainerEq(b)));
3695
3696 // Tests using ContainerEq() in the second dimension.
3697 EXPECT_THAT(a1, ElementsAre(ContainerEq(a2[0]), ContainerEq(a2[1])));
3698 EXPECT_THAT(a1, ElementsAre(Not(ContainerEq(b[0])), ContainerEq(a2[1])));
3699}
3700
3701TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) {
3702 const int a1[] = { 1, 2, 3 };
3703 const int a2[] = { 1, 2, 3 };
3704 const int b[] = { 1, 2, 3, 4 };
3705
zhanyong.wan2661c682009-06-09 05:42:12 +00003706 const int* const p1 = a1;
3707 EXPECT_THAT(make_tuple(p1, 3), ContainerEq(a2));
3708 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(b)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003709
3710 const int c[] = { 1, 3, 2 };
zhanyong.wan2661c682009-06-09 05:42:12 +00003711 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(c)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003712}
3713
3714TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) {
3715 std::string a1[][3] = {
3716 { "hi", "hello", "ciao" },
3717 { "bye", "see you", "ciao" }
3718 };
3719
3720 std::string a2[][3] = {
3721 { "hi", "hello", "ciao" },
3722 { "bye", "see you", "ciao" }
3723 };
3724
3725 const Matcher<const std::string(&)[2][3]> m = ContainerEq(a2);
3726 EXPECT_THAT(a1, m);
3727
3728 a2[0][0] = "ha";
3729 EXPECT_THAT(a1, m);
3730}
3731
zhanyong.wan898725c2011-09-16 16:45:39 +00003732TEST(WhenSortedByTest, WorksForEmptyContainer) {
3733 const vector<int> numbers;
3734 EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre()));
3735 EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1))));
3736}
3737
3738TEST(WhenSortedByTest, WorksForNonEmptyContainer) {
3739 vector<unsigned> numbers;
3740 numbers.push_back(3);
3741 numbers.push_back(1);
3742 numbers.push_back(2);
3743 numbers.push_back(2);
3744 EXPECT_THAT(numbers, WhenSortedBy(greater<unsigned>(),
3745 ElementsAre(3, 2, 2, 1)));
3746 EXPECT_THAT(numbers, Not(WhenSortedBy(greater<unsigned>(),
3747 ElementsAre(1, 2, 2, 3))));
3748}
3749
3750TEST(WhenSortedByTest, WorksForNonVectorContainer) {
3751 list<string> words;
3752 words.push_back("say");
3753 words.push_back("hello");
3754 words.push_back("world");
3755 EXPECT_THAT(words, WhenSortedBy(less<string>(),
3756 ElementsAre("hello", "say", "world")));
3757 EXPECT_THAT(words, Not(WhenSortedBy(less<string>(),
3758 ElementsAre("say", "hello", "world"))));
3759}
3760
3761TEST(WhenSortedByTest, WorksForNativeArray) {
3762 const int numbers[] = { 1, 3, 2, 4 };
3763 const int sorted_numbers[] = { 1, 2, 3, 4 };
3764 EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre(1, 2, 3, 4)));
3765 EXPECT_THAT(numbers, WhenSortedBy(less<int>(),
3766 ElementsAreArray(sorted_numbers)));
3767 EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1, 3, 2, 4))));
3768}
3769
3770TEST(WhenSortedByTest, CanDescribeSelf) {
3771 const Matcher<vector<int> > m = WhenSortedBy(less<int>(), ElementsAre(1, 2));
3772 EXPECT_EQ("(when sorted) has 2 elements where\n"
3773 "element #0 is equal to 1,\n"
3774 "element #1 is equal to 2",
3775 Describe(m));
3776 EXPECT_EQ("(when sorted) doesn't have 2 elements, or\n"
3777 "element #0 isn't equal to 1, or\n"
3778 "element #1 isn't equal to 2",
3779 DescribeNegation(m));
3780}
3781
3782TEST(WhenSortedByTest, ExplainsMatchResult) {
3783 const int a[] = { 2, 1 };
3784 EXPECT_EQ("which is { 1, 2 } when sorted, whose element #0 doesn't match",
3785 Explain(WhenSortedBy(less<int>(), ElementsAre(2, 3)), a));
3786 EXPECT_EQ("which is { 1, 2 } when sorted",
3787 Explain(WhenSortedBy(less<int>(), ElementsAre(1, 2)), a));
3788}
3789
3790// WhenSorted() is a simple wrapper on WhenSortedBy(). Hence we don't
3791// need to test it as exhaustively as we test the latter.
3792
3793TEST(WhenSortedTest, WorksForEmptyContainer) {
3794 const vector<int> numbers;
3795 EXPECT_THAT(numbers, WhenSorted(ElementsAre()));
3796 EXPECT_THAT(numbers, Not(WhenSorted(ElementsAre(1))));
3797}
3798
3799TEST(WhenSortedTest, WorksForNonEmptyContainer) {
3800 list<string> words;
3801 words.push_back("3");
3802 words.push_back("1");
3803 words.push_back("2");
3804 words.push_back("2");
3805 EXPECT_THAT(words, WhenSorted(ElementsAre("1", "2", "2", "3")));
3806 EXPECT_THAT(words, Not(WhenSorted(ElementsAre("3", "1", "2", "2"))));
3807}
3808
zhanyong.wan736baa82010-09-27 17:44:16 +00003809// Tests IsReadableTypeName().
3810
3811TEST(IsReadableTypeNameTest, ReturnsTrueForShortNames) {
3812 EXPECT_TRUE(IsReadableTypeName("int"));
3813 EXPECT_TRUE(IsReadableTypeName("const unsigned char*"));
3814 EXPECT_TRUE(IsReadableTypeName("MyMap<int, void*>"));
3815 EXPECT_TRUE(IsReadableTypeName("void (*)(int, bool)"));
3816}
3817
3818TEST(IsReadableTypeNameTest, ReturnsTrueForLongNonTemplateNonFunctionNames) {
3819 EXPECT_TRUE(IsReadableTypeName("my_long_namespace::MyClassName"));
3820 EXPECT_TRUE(IsReadableTypeName("int [5][6][7][8][9][10][11]"));
3821 EXPECT_TRUE(IsReadableTypeName("my_namespace::MyOuterClass::MyInnerClass"));
3822}
3823
3824TEST(IsReadableTypeNameTest, ReturnsFalseForLongTemplateNames) {
3825 EXPECT_FALSE(
3826 IsReadableTypeName("basic_string<char, std::char_traits<char> >"));
3827 EXPECT_FALSE(IsReadableTypeName("std::vector<int, std::alloc_traits<int> >"));
3828}
3829
3830TEST(IsReadableTypeNameTest, ReturnsFalseForLongFunctionTypeNames) {
3831 EXPECT_FALSE(IsReadableTypeName("void (&)(int, bool, char, float)"));
3832}
3833
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003834// Tests JoinAsTuple().
3835
3836TEST(JoinAsTupleTest, JoinsEmptyTuple) {
3837 EXPECT_EQ("", JoinAsTuple(Strings()));
3838}
3839
3840TEST(JoinAsTupleTest, JoinsOneTuple) {
3841 const char* fields[] = { "1" };
3842 EXPECT_EQ("1", JoinAsTuple(Strings(fields, fields + 1)));
3843}
3844
3845TEST(JoinAsTupleTest, JoinsTwoTuple) {
3846 const char* fields[] = { "1", "a" };
3847 EXPECT_EQ("(1, a)", JoinAsTuple(Strings(fields, fields + 2)));
3848}
3849
3850TEST(JoinAsTupleTest, JoinsTenTuple) {
3851 const char* fields[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
3852 EXPECT_EQ("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)",
3853 JoinAsTuple(Strings(fields, fields + 10)));
3854}
3855
3856// Tests FormatMatcherDescription().
3857
3858TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) {
3859 EXPECT_EQ("is even",
zhanyong.wanb4140802010-06-08 22:53:57 +00003860 FormatMatcherDescription(false, "IsEven", Strings()));
3861 EXPECT_EQ("not (is even)",
3862 FormatMatcherDescription(true, "IsEven", Strings()));
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003863
3864 const char* params[] = { "5" };
3865 EXPECT_EQ("equals 5",
zhanyong.wanb4140802010-06-08 22:53:57 +00003866 FormatMatcherDescription(false, "Equals",
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003867 Strings(params, params + 1)));
3868
3869 const char* params2[] = { "5", "8" };
3870 EXPECT_EQ("is in range (5, 8)",
zhanyong.wanb4140802010-06-08 22:53:57 +00003871 FormatMatcherDescription(false, "IsInRange",
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003872 Strings(params2, params2 + 2)));
3873}
3874
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003875// Tests PolymorphicMatcher::mutable_impl().
3876TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {
3877 PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
3878 DivisibleByImpl& impl = m.mutable_impl();
3879 EXPECT_EQ(42, impl.divider());
3880
3881 impl.set_divider(0);
3882 EXPECT_EQ(0, m.mutable_impl().divider());
3883}
3884
3885// Tests PolymorphicMatcher::impl().
3886TEST(PolymorphicMatcherTest, CanAccessImpl) {
3887 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
3888 const DivisibleByImpl& impl = m.impl();
3889 EXPECT_EQ(42, impl.divider());
3890}
3891
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003892TEST(MatcherTupleTest, ExplainsMatchFailure) {
3893 stringstream ss1;
3894 ExplainMatchFailureTupleTo(make_tuple(Matcher<char>(Eq('a')), GreaterThan(5)),
3895 make_tuple('a', 10), &ss1);
3896 EXPECT_EQ("", ss1.str()); // Successful match.
3897
3898 stringstream ss2;
3899 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
3900 make_tuple(2, 'b'), &ss2);
3901 EXPECT_EQ(" Expected arg #0: is > 5\n"
3902 " Actual: 2, which is 3 less than 5\n"
zhanyong.wand60c5f42010-07-21 22:21:07 +00003903 " Expected arg #1: is equal to 'a' (97, 0x61)\n"
3904 " Actual: 'b' (98, 0x62)\n",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003905 ss2.str()); // Failed match where both arguments need explanation.
3906
3907 stringstream ss3;
3908 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
3909 make_tuple(2, 'a'), &ss3);
3910 EXPECT_EQ(" Expected arg #0: is > 5\n"
3911 " Actual: 2, which is 3 less than 5\n",
3912 ss3.str()); // Failed match where only one argument needs
3913 // explanation.
3914}
3915
zhanyong.wan33605ba2010-04-22 23:37:47 +00003916// Tests Each().
3917
3918TEST(EachTest, ExplainsMatchResultCorrectly) {
3919 set<int> a; // empty
3920
3921 Matcher<set<int> > m = Each(2);
3922 EXPECT_EQ("", Explain(m, a));
3923
zhanyong.wan736baa82010-09-27 17:44:16 +00003924 Matcher<const int(&)[1]> n = Each(1); // NOLINT
zhanyong.wan33605ba2010-04-22 23:37:47 +00003925
3926 const int b[1] = { 1 };
3927 EXPECT_EQ("", Explain(n, b));
3928
3929 n = Each(3);
3930 EXPECT_EQ("whose element #0 doesn't match", Explain(n, b));
3931
3932 a.insert(1);
3933 a.insert(2);
3934 a.insert(3);
3935 m = Each(GreaterThan(0));
3936 EXPECT_EQ("", Explain(m, a));
3937
3938 m = Each(GreaterThan(10));
3939 EXPECT_EQ("whose element #0 doesn't match, which is 9 less than 10",
3940 Explain(m, a));
3941}
3942
3943TEST(EachTest, DescribesItselfCorrectly) {
3944 Matcher<vector<int> > m = Each(1);
3945 EXPECT_EQ("only contains elements that is equal to 1", Describe(m));
3946
3947 Matcher<vector<int> > m2 = Not(m);
3948 EXPECT_EQ("contains some element that isn't equal to 1", Describe(m2));
3949}
3950
3951TEST(EachTest, MatchesVectorWhenAllElementsMatch) {
3952 vector<int> some_vector;
3953 EXPECT_THAT(some_vector, Each(1));
3954 some_vector.push_back(3);
3955 EXPECT_THAT(some_vector, Not(Each(1)));
3956 EXPECT_THAT(some_vector, Each(3));
3957 some_vector.push_back(1);
3958 some_vector.push_back(2);
3959 EXPECT_THAT(some_vector, Not(Each(3)));
3960 EXPECT_THAT(some_vector, Each(Lt(3.5)));
3961
3962 vector<string> another_vector;
3963 another_vector.push_back("fee");
3964 EXPECT_THAT(another_vector, Each(string("fee")));
3965 another_vector.push_back("fie");
3966 another_vector.push_back("foe");
3967 another_vector.push_back("fum");
3968 EXPECT_THAT(another_vector, Not(Each(string("fee"))));
3969}
3970
3971TEST(EachTest, MatchesMapWhenAllElementsMatch) {
3972 map<const char*, int> my_map;
3973 const char* bar = "a string";
3974 my_map[bar] = 2;
3975 EXPECT_THAT(my_map, Each(make_pair(bar, 2)));
3976
3977 map<string, int> another_map;
3978 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1)));
3979 another_map["fee"] = 1;
3980 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1)));
3981 another_map["fie"] = 2;
3982 another_map["foe"] = 3;
3983 another_map["fum"] = 4;
3984 EXPECT_THAT(another_map, Not(Each(make_pair(string("fee"), 1))));
3985 EXPECT_THAT(another_map, Not(Each(make_pair(string("fum"), 1))));
3986 EXPECT_THAT(another_map, Each(Pair(_, Gt(0))));
3987}
3988
3989TEST(EachTest, AcceptsMatcher) {
3990 const int a[] = { 1, 2, 3 };
3991 EXPECT_THAT(a, Each(Gt(0)));
3992 EXPECT_THAT(a, Not(Each(Gt(1))));
3993}
3994
3995TEST(EachTest, WorksForNativeArrayAsTuple) {
3996 const int a[] = { 1, 2 };
3997 const int* const pointer = a;
3998 EXPECT_THAT(make_tuple(pointer, 2), Each(Gt(0)));
3999 EXPECT_THAT(make_tuple(pointer, 2), Not(Each(Gt(1))));
4000}
4001
zhanyong.wanab5b77c2010-05-17 19:32:48 +00004002// For testing Pointwise().
4003class IsHalfOfMatcher {
4004 public:
4005 template <typename T1, typename T2>
4006 bool MatchAndExplain(const tuple<T1, T2>& a_pair,
4007 MatchResultListener* listener) const {
4008 if (get<0>(a_pair) == get<1>(a_pair)/2) {
4009 *listener << "where the second is " << get<1>(a_pair);
4010 return true;
4011 } else {
4012 *listener << "where the second/2 is " << get<1>(a_pair)/2;
4013 return false;
4014 }
4015 }
4016
4017 void DescribeTo(ostream* os) const {
4018 *os << "are a pair where the first is half of the second";
4019 }
4020
4021 void DescribeNegationTo(ostream* os) const {
4022 *os << "are a pair where the first isn't half of the second";
4023 }
4024};
4025
4026PolymorphicMatcher<IsHalfOfMatcher> IsHalfOf() {
4027 return MakePolymorphicMatcher(IsHalfOfMatcher());
4028}
4029
4030TEST(PointwiseTest, DescribesSelf) {
4031 vector<int> rhs;
4032 rhs.push_back(1);
4033 rhs.push_back(2);
4034 rhs.push_back(3);
4035 const Matcher<const vector<int>&> m = Pointwise(IsHalfOf(), rhs);
4036 EXPECT_EQ("contains 3 values, where each value and its corresponding value "
4037 "in { 1, 2, 3 } are a pair where the first is half of the second",
4038 Describe(m));
4039 EXPECT_EQ("doesn't contain exactly 3 values, or contains a value x at some "
4040 "index i where x and the i-th value of { 1, 2, 3 } are a pair "
4041 "where the first isn't half of the second",
4042 DescribeNegation(m));
4043}
4044
4045TEST(PointwiseTest, MakesCopyOfRhs) {
4046 list<signed char> rhs;
4047 rhs.push_back(2);
4048 rhs.push_back(4);
4049
4050 int lhs[] = { 1, 2 };
4051 const Matcher<const int (&)[2]> m = Pointwise(IsHalfOf(), rhs);
4052 EXPECT_THAT(lhs, m);
4053
4054 // Changing rhs now shouldn't affect m, which made a copy of rhs.
4055 rhs.push_back(6);
4056 EXPECT_THAT(lhs, m);
4057}
4058
4059TEST(PointwiseTest, WorksForLhsNativeArray) {
4060 const int lhs[] = { 1, 2, 3 };
4061 vector<int> rhs;
4062 rhs.push_back(2);
4063 rhs.push_back(4);
4064 rhs.push_back(6);
4065 EXPECT_THAT(lhs, Pointwise(Lt(), rhs));
4066 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
4067}
4068
4069TEST(PointwiseTest, WorksForRhsNativeArray) {
4070 const int rhs[] = { 1, 2, 3 };
4071 vector<int> lhs;
4072 lhs.push_back(2);
4073 lhs.push_back(4);
4074 lhs.push_back(6);
4075 EXPECT_THAT(lhs, Pointwise(Gt(), rhs));
4076 EXPECT_THAT(lhs, Not(Pointwise(Lt(), rhs)));
4077}
4078
4079TEST(PointwiseTest, RejectsWrongSize) {
4080 const double lhs[2] = { 1, 2 };
4081 const int rhs[1] = { 0 };
4082 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
4083 EXPECT_EQ("which contains 2 values",
4084 Explain(Pointwise(Gt(), rhs), lhs));
4085
4086 const int rhs2[3] = { 0, 1, 2 };
4087 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs2)));
4088}
4089
4090TEST(PointwiseTest, RejectsWrongContent) {
4091 const double lhs[3] = { 1, 2, 3 };
4092 const int rhs[3] = { 2, 6, 4 };
4093 EXPECT_THAT(lhs, Not(Pointwise(IsHalfOf(), rhs)));
4094 EXPECT_EQ("where the value pair (2, 6) at index #1 don't match, "
4095 "where the second/2 is 3",
4096 Explain(Pointwise(IsHalfOf(), rhs), lhs));
4097}
4098
4099TEST(PointwiseTest, AcceptsCorrectContent) {
4100 const double lhs[3] = { 1, 2, 3 };
4101 const int rhs[3] = { 2, 4, 6 };
4102 EXPECT_THAT(lhs, Pointwise(IsHalfOf(), rhs));
4103 EXPECT_EQ("", Explain(Pointwise(IsHalfOf(), rhs), lhs));
4104}
4105
4106TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) {
4107 const double lhs[3] = { 1, 2, 3 };
4108 const int rhs[3] = { 2, 4, 6 };
4109 const Matcher<tuple<const double&, const int&> > m1 = IsHalfOf();
4110 EXPECT_THAT(lhs, Pointwise(m1, rhs));
4111 EXPECT_EQ("", Explain(Pointwise(m1, rhs), lhs));
4112
4113 // This type works as a tuple<const double&, const int&> can be
4114 // implicitly cast to tuple<double, int>.
4115 const Matcher<tuple<double, int> > m2 = IsHalfOf();
4116 EXPECT_THAT(lhs, Pointwise(m2, rhs));
4117 EXPECT_EQ("", Explain(Pointwise(m2, rhs), lhs));
4118}
4119
shiqiane35fdd92008-12-10 05:08:54 +00004120} // namespace gmock_matchers_test
4121} // namespace testing