blob: de3f8d73e54d16a3e3132c93a71c8fa574814023 [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;
zhanyong.wan34b034c2010-03-05 21:23:23 +0000134using testing::internal::StringMatchResultListener;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000135using testing::internal::Strings;
vladlosev79b83502009-11-18 00:43:37 +0000136using testing::internal::linked_ptr;
vladloseve56daa72009-11-18 01:08:08 +0000137using testing::internal::scoped_ptr;
shiqiane35fdd92008-12-10 05:08:54 +0000138using testing::internal::string;
139
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000140// For testing ExplainMatchResultTo().
141class GreaterThanMatcher : public MatcherInterface<int> {
142 public:
143 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
144
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000145 virtual void DescribeTo(ostream* os) const {
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000146 *os << "is > " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000147 }
148
zhanyong.wandb22c222010-01-28 21:52:29 +0000149 virtual bool MatchAndExplain(int lhs,
150 MatchResultListener* listener) const {
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000151 const int diff = lhs - rhs_;
152 if (diff > 0) {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000153 *listener << "which is " << diff << " more than " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000154 } else if (diff == 0) {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000155 *listener << "which is the same as " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000156 } else {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000157 *listener << "which is " << -diff << " less than " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000158 }
zhanyong.wandb22c222010-01-28 21:52:29 +0000159
160 return lhs > rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000161 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000162
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000163 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000164 int rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000165};
166
167Matcher<int> GreaterThan(int n) {
168 return MakeMatcher(new GreaterThanMatcher(n));
169}
170
zhanyong.wan736baa82010-09-27 17:44:16 +0000171string OfType(const string& type_name) {
172#if GTEST_HAS_RTTI
173 return " (of type " + type_name + ")";
174#else
175 return "";
176#endif
177}
178
shiqiane35fdd92008-12-10 05:08:54 +0000179// Returns the description of the given matcher.
180template <typename T>
181string Describe(const Matcher<T>& m) {
182 stringstream ss;
183 m.DescribeTo(&ss);
184 return ss.str();
185}
186
187// Returns the description of the negation of the given matcher.
188template <typename T>
189string DescribeNegation(const Matcher<T>& m) {
190 stringstream ss;
191 m.DescribeNegationTo(&ss);
192 return ss.str();
193}
194
195// Returns the reason why x matches, or doesn't match, m.
196template <typename MatcherType, typename Value>
197string Explain(const MatcherType& m, const Value& x) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000198 StringMatchResultListener listener;
199 ExplainMatchResult(m, x, &listener);
200 return listener.str();
shiqiane35fdd92008-12-10 05:08:54 +0000201}
202
zhanyong.wana862f1d2010-03-15 21:23:04 +0000203TEST(MatchResultListenerTest, StreamingWorks) {
204 StringMatchResultListener listener;
205 listener << "hi" << 5;
206 EXPECT_EQ("hi5", listener.str());
207
208 // Streaming shouldn't crash when the underlying ostream is NULL.
209 DummyMatchResultListener dummy;
210 dummy << "hi" << 5;
211}
212
213TEST(MatchResultListenerTest, CanAccessUnderlyingStream) {
214 EXPECT_TRUE(DummyMatchResultListener().stream() == NULL);
215 EXPECT_TRUE(StreamMatchResultListener(NULL).stream() == NULL);
216
217 EXPECT_EQ(&std::cout, StreamMatchResultListener(&std::cout).stream());
218}
219
220TEST(MatchResultListenerTest, IsInterestedWorks) {
221 EXPECT_TRUE(StringMatchResultListener().IsInterested());
222 EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested());
223
224 EXPECT_FALSE(DummyMatchResultListener().IsInterested());
225 EXPECT_FALSE(StreamMatchResultListener(NULL).IsInterested());
226}
227
shiqiane35fdd92008-12-10 05:08:54 +0000228// Makes sure that the MatcherInterface<T> interface doesn't
229// change.
230class EvenMatcherImpl : public MatcherInterface<int> {
231 public:
zhanyong.wandb22c222010-01-28 21:52:29 +0000232 virtual bool MatchAndExplain(int x,
233 MatchResultListener* /* listener */) const {
234 return x % 2 == 0;
235 }
shiqiane35fdd92008-12-10 05:08:54 +0000236
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000237 virtual void DescribeTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +0000238 *os << "is an even number";
239 }
240
241 // We deliberately don't define DescribeNegationTo() and
242 // ExplainMatchResultTo() here, to make sure the definition of these
243 // two methods is optional.
244};
245
zhanyong.wana862f1d2010-03-15 21:23:04 +0000246// Makes sure that the MatcherInterface API doesn't change.
247TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) {
shiqiane35fdd92008-12-10 05:08:54 +0000248 EvenMatcherImpl m;
249}
250
zhanyong.wan82113312010-01-08 21:55:40 +0000251// Tests implementing a monomorphic matcher using MatchAndExplain().
252
253class NewEvenMatcherImpl : public MatcherInterface<int> {
254 public:
255 virtual bool MatchAndExplain(int x, MatchResultListener* listener) const {
256 const bool match = x % 2 == 0;
257 // Verifies that we can stream to a listener directly.
258 *listener << "value % " << 2;
259 if (listener->stream() != NULL) {
260 // Verifies that we can stream to a listener's underlying stream
261 // too.
262 *listener->stream() << " == " << (x % 2);
263 }
264 return match;
265 }
266
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000267 virtual void DescribeTo(ostream* os) const {
zhanyong.wan82113312010-01-08 21:55:40 +0000268 *os << "is an even number";
269 }
270};
271
272TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {
273 Matcher<int> m = MakeMatcher(new NewEvenMatcherImpl);
274 EXPECT_TRUE(m.Matches(2));
275 EXPECT_FALSE(m.Matches(3));
276 EXPECT_EQ("value % 2 == 0", Explain(m, 2));
277 EXPECT_EQ("value % 2 == 1", Explain(m, 3));
278}
279
shiqiane35fdd92008-12-10 05:08:54 +0000280// Tests default-constructing a matcher.
281TEST(MatcherTest, CanBeDefaultConstructed) {
282 Matcher<double> m;
283}
284
285// Tests that Matcher<T> can be constructed from a MatcherInterface<T>*.
286TEST(MatcherTest, CanBeConstructedFromMatcherInterface) {
287 const MatcherInterface<int>* impl = new EvenMatcherImpl;
288 Matcher<int> m(impl);
289 EXPECT_TRUE(m.Matches(4));
290 EXPECT_FALSE(m.Matches(5));
291}
292
293// Tests that value can be used in place of Eq(value).
294TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) {
295 Matcher<int> m1 = 5;
296 EXPECT_TRUE(m1.Matches(5));
297 EXPECT_FALSE(m1.Matches(6));
298}
299
300// Tests that NULL can be used in place of Eq(NULL).
301TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) {
302 Matcher<int*> m1 = NULL;
303 EXPECT_TRUE(m1.Matches(NULL));
304 int n = 0;
305 EXPECT_FALSE(m1.Matches(&n));
306}
307
308// Tests that matchers are copyable.
309TEST(MatcherTest, IsCopyable) {
310 // Tests the copy constructor.
311 Matcher<bool> m1 = Eq(false);
312 EXPECT_TRUE(m1.Matches(false));
313 EXPECT_FALSE(m1.Matches(true));
314
315 // Tests the assignment operator.
316 m1 = Eq(true);
317 EXPECT_TRUE(m1.Matches(true));
318 EXPECT_FALSE(m1.Matches(false));
319}
320
321// Tests that Matcher<T>::DescribeTo() calls
322// MatcherInterface<T>::DescribeTo().
323TEST(MatcherTest, CanDescribeItself) {
324 EXPECT_EQ("is an even number",
325 Describe(Matcher<int>(new EvenMatcherImpl)));
326}
327
zhanyong.wan82113312010-01-08 21:55:40 +0000328// Tests Matcher<T>::MatchAndExplain().
329TEST(MatcherTest, MatchAndExplain) {
330 Matcher<int> m = GreaterThan(0);
zhanyong.wan34b034c2010-03-05 21:23:23 +0000331 StringMatchResultListener listener1;
zhanyong.wan82113312010-01-08 21:55:40 +0000332 EXPECT_TRUE(m.MatchAndExplain(42, &listener1));
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000333 EXPECT_EQ("which is 42 more than 0", listener1.str());
zhanyong.wan82113312010-01-08 21:55:40 +0000334
zhanyong.wan34b034c2010-03-05 21:23:23 +0000335 StringMatchResultListener listener2;
zhanyong.wan82113312010-01-08 21:55:40 +0000336 EXPECT_FALSE(m.MatchAndExplain(-9, &listener2));
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000337 EXPECT_EQ("which is 9 less than 0", listener2.str());
zhanyong.wan82113312010-01-08 21:55:40 +0000338}
339
shiqiane35fdd92008-12-10 05:08:54 +0000340// Tests that a C-string literal can be implicitly converted to a
341// Matcher<string> or Matcher<const string&>.
342TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
343 Matcher<string> m1 = "hi";
344 EXPECT_TRUE(m1.Matches("hi"));
345 EXPECT_FALSE(m1.Matches("hello"));
346
347 Matcher<const string&> m2 = "hi";
348 EXPECT_TRUE(m2.Matches("hi"));
349 EXPECT_FALSE(m2.Matches("hello"));
350}
351
352// Tests that a string object can be implicitly converted to a
353// Matcher<string> or Matcher<const string&>.
354TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {
355 Matcher<string> m1 = string("hi");
356 EXPECT_TRUE(m1.Matches("hi"));
357 EXPECT_FALSE(m1.Matches("hello"));
358
359 Matcher<const string&> m2 = string("hi");
360 EXPECT_TRUE(m2.Matches("hi"));
361 EXPECT_FALSE(m2.Matches("hello"));
362}
363
364// Tests that MakeMatcher() constructs a Matcher<T> from a
365// MatcherInterface* without requiring the user to explicitly
366// write the type.
367TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) {
368 const MatcherInterface<int>* dummy_impl = NULL;
369 Matcher<int> m = MakeMatcher(dummy_impl);
370}
371
zhanyong.wan82113312010-01-08 21:55:40 +0000372// Tests that MakePolymorphicMatcher() can construct a polymorphic
373// matcher from its implementation using the old API.
zhanyong.wan33605ba2010-04-22 23:37:47 +0000374const int g_bar = 1;
shiqiane35fdd92008-12-10 05:08:54 +0000375class ReferencesBarOrIsZeroImpl {
376 public:
377 template <typename T>
zhanyong.wandb22c222010-01-28 21:52:29 +0000378 bool MatchAndExplain(const T& x,
379 MatchResultListener* /* listener */) const {
shiqiane35fdd92008-12-10 05:08:54 +0000380 const void* p = &x;
zhanyong.wan33605ba2010-04-22 23:37:47 +0000381 return p == &g_bar || x == 0;
shiqiane35fdd92008-12-10 05:08:54 +0000382 }
383
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000384 void DescribeTo(ostream* os) const { *os << "g_bar or zero"; }
shiqiane35fdd92008-12-10 05:08:54 +0000385
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000386 void DescribeNegationTo(ostream* os) const {
zhanyong.wan33605ba2010-04-22 23:37:47 +0000387 *os << "doesn't reference g_bar and is not zero";
shiqiane35fdd92008-12-10 05:08:54 +0000388 }
389};
390
391// This function verifies that MakePolymorphicMatcher() returns a
392// PolymorphicMatcher<T> where T is the argument's type.
393PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() {
394 return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl());
395}
396
zhanyong.wan82113312010-01-08 21:55:40 +0000397TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) {
shiqiane35fdd92008-12-10 05:08:54 +0000398 // Using a polymorphic matcher to match a reference type.
399 Matcher<const int&> m1 = ReferencesBarOrIsZero();
400 EXPECT_TRUE(m1.Matches(0));
401 // Verifies that the identity of a by-reference argument is preserved.
zhanyong.wan33605ba2010-04-22 23:37:47 +0000402 EXPECT_TRUE(m1.Matches(g_bar));
shiqiane35fdd92008-12-10 05:08:54 +0000403 EXPECT_FALSE(m1.Matches(1));
zhanyong.wan33605ba2010-04-22 23:37:47 +0000404 EXPECT_EQ("g_bar or zero", Describe(m1));
shiqiane35fdd92008-12-10 05:08:54 +0000405
406 // Using a polymorphic matcher to match a value type.
407 Matcher<double> m2 = ReferencesBarOrIsZero();
408 EXPECT_TRUE(m2.Matches(0.0));
409 EXPECT_FALSE(m2.Matches(0.1));
zhanyong.wan33605ba2010-04-22 23:37:47 +0000410 EXPECT_EQ("g_bar or zero", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +0000411}
412
zhanyong.wan82113312010-01-08 21:55:40 +0000413// Tests implementing a polymorphic matcher using MatchAndExplain().
414
415class PolymorphicIsEvenImpl {
416 public:
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000417 void DescribeTo(ostream* os) const { *os << "is even"; }
zhanyong.wan82113312010-01-08 21:55:40 +0000418
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000419 void DescribeNegationTo(ostream* os) const {
zhanyong.wan82113312010-01-08 21:55:40 +0000420 *os << "is odd";
421 }
zhanyong.wan82113312010-01-08 21:55:40 +0000422
zhanyong.wandb22c222010-01-28 21:52:29 +0000423 template <typename T>
424 bool MatchAndExplain(const T& x, MatchResultListener* listener) const {
425 // Verifies that we can stream to the listener directly.
426 *listener << "% " << 2;
427 if (listener->stream() != NULL) {
428 // Verifies that we can stream to the listener's underlying stream
429 // too.
430 *listener->stream() << " == " << (x % 2);
431 }
432 return (x % 2) == 0;
zhanyong.wan82113312010-01-08 21:55:40 +0000433 }
zhanyong.wandb22c222010-01-28 21:52:29 +0000434};
zhanyong.wan82113312010-01-08 21:55:40 +0000435
436PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() {
437 return MakePolymorphicMatcher(PolymorphicIsEvenImpl());
438}
439
440TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {
441 // Using PolymorphicIsEven() as a Matcher<int>.
442 const Matcher<int> m1 = PolymorphicIsEven();
443 EXPECT_TRUE(m1.Matches(42));
444 EXPECT_FALSE(m1.Matches(43));
445 EXPECT_EQ("is even", Describe(m1));
446
447 const Matcher<int> not_m1 = Not(m1);
448 EXPECT_EQ("is odd", Describe(not_m1));
449
450 EXPECT_EQ("% 2 == 0", Explain(m1, 42));
451
452 // Using PolymorphicIsEven() as a Matcher<char>.
453 const Matcher<char> m2 = PolymorphicIsEven();
454 EXPECT_TRUE(m2.Matches('\x42'));
455 EXPECT_FALSE(m2.Matches('\x43'));
456 EXPECT_EQ("is even", Describe(m2));
457
458 const Matcher<char> not_m2 = Not(m2);
459 EXPECT_EQ("is odd", Describe(not_m2));
460
461 EXPECT_EQ("% 2 == 0", Explain(m2, '\x42'));
462}
463
shiqiane35fdd92008-12-10 05:08:54 +0000464// Tests that MatcherCast<T>(m) works when m is a polymorphic matcher.
465TEST(MatcherCastTest, FromPolymorphicMatcher) {
466 Matcher<int> m = MatcherCast<int>(Eq(5));
467 EXPECT_TRUE(m.Matches(5));
468 EXPECT_FALSE(m.Matches(6));
469}
470
471// For testing casting matchers between compatible types.
472class IntValue {
473 public:
474 // An int can be statically (although not implicitly) cast to a
475 // IntValue.
zhanyong.wan32de5f52009-12-23 00:13:23 +0000476 explicit IntValue(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +0000477
478 int value() const { return value_; }
479 private:
480 int value_;
481};
482
483// For testing casting matchers between compatible types.
484bool IsPositiveIntValue(const IntValue& foo) {
485 return foo.value() > 0;
486}
487
488// Tests that MatcherCast<T>(m) works when m is a Matcher<U> where T
489// can be statically converted to U.
490TEST(MatcherCastTest, FromCompatibleType) {
491 Matcher<double> m1 = Eq(2.0);
492 Matcher<int> m2 = MatcherCast<int>(m1);
493 EXPECT_TRUE(m2.Matches(2));
494 EXPECT_FALSE(m2.Matches(3));
495
496 Matcher<IntValue> m3 = Truly(IsPositiveIntValue);
497 Matcher<int> m4 = MatcherCast<int>(m3);
498 // In the following, the arguments 1 and 0 are statically converted
499 // to IntValue objects, and then tested by the IsPositiveIntValue()
500 // predicate.
501 EXPECT_TRUE(m4.Matches(1));
502 EXPECT_FALSE(m4.Matches(0));
503}
504
505// Tests that MatcherCast<T>(m) works when m is a Matcher<const T&>.
506TEST(MatcherCastTest, FromConstReferenceToNonReference) {
507 Matcher<const int&> m1 = Eq(0);
508 Matcher<int> m2 = MatcherCast<int>(m1);
509 EXPECT_TRUE(m2.Matches(0));
510 EXPECT_FALSE(m2.Matches(1));
511}
512
513// Tests that MatcherCast<T>(m) works when m is a Matcher<T&>.
514TEST(MatcherCastTest, FromReferenceToNonReference) {
515 Matcher<int&> m1 = Eq(0);
516 Matcher<int> m2 = MatcherCast<int>(m1);
517 EXPECT_TRUE(m2.Matches(0));
518 EXPECT_FALSE(m2.Matches(1));
519}
520
521// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
522TEST(MatcherCastTest, FromNonReferenceToConstReference) {
523 Matcher<int> m1 = Eq(0);
524 Matcher<const int&> m2 = MatcherCast<const int&>(m1);
525 EXPECT_TRUE(m2.Matches(0));
526 EXPECT_FALSE(m2.Matches(1));
527}
528
529// Tests that MatcherCast<T&>(m) works when m is a Matcher<T>.
530TEST(MatcherCastTest, FromNonReferenceToReference) {
531 Matcher<int> m1 = Eq(0);
532 Matcher<int&> m2 = MatcherCast<int&>(m1);
533 int n = 0;
534 EXPECT_TRUE(m2.Matches(n));
535 n = 1;
536 EXPECT_FALSE(m2.Matches(n));
537}
538
539// Tests that MatcherCast<T>(m) works when m is a Matcher<T>.
540TEST(MatcherCastTest, FromSameType) {
541 Matcher<int> m1 = Eq(0);
542 Matcher<int> m2 = MatcherCast<int>(m1);
543 EXPECT_TRUE(m2.Matches(0));
544 EXPECT_FALSE(m2.Matches(1));
545}
546
jgm79a367e2012-04-10 16:02:11 +0000547// Implicitly convertible form any type.
548struct ConvertibleFromAny {
549 ConvertibleFromAny(int a_value) : value(a_value) {}
550 template <typename T>
551 ConvertibleFromAny(const T& a_value) : value(-1) {
552 ADD_FAILURE() << "Conversion constructor called";
553 }
554 int value;
555};
556
557bool operator==(const ConvertibleFromAny& a, const ConvertibleFromAny& b) {
558 return a.value == b.value;
559}
560
561ostream& operator<<(ostream& os, const ConvertibleFromAny& a) {
562 return os << a.value;
563}
564
565TEST(MatcherCastTest, ConversionConstructorIsUsed) {
566 Matcher<ConvertibleFromAny> m = MatcherCast<ConvertibleFromAny>(1);
567 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
568 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
569}
570
571TEST(MatcherCastTest, FromConvertibleFromAny) {
572 Matcher<ConvertibleFromAny> m =
573 MatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
574 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
575 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
576}
577
zhanyong.wan18490652009-05-11 18:54:08 +0000578class Base {};
579class Derived : public Base {};
580
581// Tests that SafeMatcherCast<T>(m) works when m is a polymorphic matcher.
582TEST(SafeMatcherCastTest, FromPolymorphicMatcher) {
583 Matcher<char> m2 = SafeMatcherCast<char>(Eq(32));
584 EXPECT_TRUE(m2.Matches(' '));
585 EXPECT_FALSE(m2.Matches('\n'));
586}
587
zhanyong.wan16cf4732009-05-14 20:55:30 +0000588// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where
589// T and U are arithmetic types and T can be losslessly converted to
590// U.
591TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) {
zhanyong.wan18490652009-05-11 18:54:08 +0000592 Matcher<double> m1 = DoubleEq(1.0);
zhanyong.wan16cf4732009-05-14 20:55:30 +0000593 Matcher<float> m2 = SafeMatcherCast<float>(m1);
594 EXPECT_TRUE(m2.Matches(1.0f));
595 EXPECT_FALSE(m2.Matches(2.0f));
596
597 Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>('a'));
598 EXPECT_TRUE(m3.Matches('a'));
599 EXPECT_FALSE(m3.Matches('b'));
zhanyong.wan18490652009-05-11 18:54:08 +0000600}
601
602// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where T and U
603// are pointers or references to a derived and a base class, correspondingly.
604TEST(SafeMatcherCastTest, FromBaseClass) {
605 Derived d, d2;
606 Matcher<Base*> m1 = Eq(&d);
607 Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1);
608 EXPECT_TRUE(m2.Matches(&d));
609 EXPECT_FALSE(m2.Matches(&d2));
610
611 Matcher<Base&> m3 = Ref(d);
612 Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3);
613 EXPECT_TRUE(m4.Matches(d));
614 EXPECT_FALSE(m4.Matches(d2));
615}
616
617// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<const T&>.
618TEST(SafeMatcherCastTest, FromConstReferenceToReference) {
619 int n = 0;
620 Matcher<const int&> m1 = Ref(n);
621 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
622 int n1 = 0;
623 EXPECT_TRUE(m2.Matches(n));
624 EXPECT_FALSE(m2.Matches(n1));
625}
626
627// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
628TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) {
629 Matcher<int> m1 = Eq(0);
630 Matcher<const int&> m2 = SafeMatcherCast<const int&>(m1);
631 EXPECT_TRUE(m2.Matches(0));
632 EXPECT_FALSE(m2.Matches(1));
633}
634
635// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<T>.
636TEST(SafeMatcherCastTest, FromNonReferenceToReference) {
637 Matcher<int> m1 = Eq(0);
638 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
639 int n = 0;
640 EXPECT_TRUE(m2.Matches(n));
641 n = 1;
642 EXPECT_FALSE(m2.Matches(n));
643}
644
645// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<T>.
646TEST(SafeMatcherCastTest, FromSameType) {
647 Matcher<int> m1 = Eq(0);
648 Matcher<int> m2 = SafeMatcherCast<int>(m1);
649 EXPECT_TRUE(m2.Matches(0));
650 EXPECT_FALSE(m2.Matches(1));
651}
652
jgm79a367e2012-04-10 16:02:11 +0000653TEST(SafeMatcherCastTest, ConversionConstructorIsUsed) {
654 Matcher<ConvertibleFromAny> m = SafeMatcherCast<ConvertibleFromAny>(1);
655 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
656 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
657}
658
659TEST(SafeMatcherCastTest, FromConvertibleFromAny) {
660 Matcher<ConvertibleFromAny> m =
661 SafeMatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
662 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
663 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
664}
665
shiqiane35fdd92008-12-10 05:08:54 +0000666// Tests that A<T>() matches any value of type T.
667TEST(ATest, MatchesAnyValue) {
668 // Tests a matcher for a value type.
669 Matcher<double> m1 = A<double>();
670 EXPECT_TRUE(m1.Matches(91.43));
671 EXPECT_TRUE(m1.Matches(-15.32));
672
673 // Tests a matcher for a reference type.
674 int a = 2;
675 int b = -6;
676 Matcher<int&> m2 = A<int&>();
677 EXPECT_TRUE(m2.Matches(a));
678 EXPECT_TRUE(m2.Matches(b));
679}
680
681// Tests that A<T>() describes itself properly.
682TEST(ATest, CanDescribeSelf) {
683 EXPECT_EQ("is anything", Describe(A<bool>()));
684}
685
686// Tests that An<T>() matches any value of type T.
687TEST(AnTest, MatchesAnyValue) {
688 // Tests a matcher for a value type.
689 Matcher<int> m1 = An<int>();
690 EXPECT_TRUE(m1.Matches(9143));
691 EXPECT_TRUE(m1.Matches(-1532));
692
693 // Tests a matcher for a reference type.
694 int a = 2;
695 int b = -6;
696 Matcher<int&> m2 = An<int&>();
697 EXPECT_TRUE(m2.Matches(a));
698 EXPECT_TRUE(m2.Matches(b));
699}
700
701// Tests that An<T>() describes itself properly.
702TEST(AnTest, CanDescribeSelf) {
703 EXPECT_EQ("is anything", Describe(An<int>()));
704}
705
706// Tests that _ can be used as a matcher for any type and matches any
707// value of that type.
708TEST(UnderscoreTest, MatchesAnyValue) {
709 // Uses _ as a matcher for a value type.
710 Matcher<int> m1 = _;
711 EXPECT_TRUE(m1.Matches(123));
712 EXPECT_TRUE(m1.Matches(-242));
713
714 // Uses _ as a matcher for a reference type.
715 bool a = false;
716 const bool b = true;
717 Matcher<const bool&> m2 = _;
718 EXPECT_TRUE(m2.Matches(a));
719 EXPECT_TRUE(m2.Matches(b));
720}
721
722// Tests that _ describes itself properly.
723TEST(UnderscoreTest, CanDescribeSelf) {
724 Matcher<int> m = _;
725 EXPECT_EQ("is anything", Describe(m));
726}
727
728// Tests that Eq(x) matches any value equal to x.
729TEST(EqTest, MatchesEqualValue) {
730 // 2 C-strings with same content but different addresses.
731 const char a1[] = "hi";
732 const char a2[] = "hi";
733
734 Matcher<const char*> m1 = Eq(a1);
735 EXPECT_TRUE(m1.Matches(a1));
736 EXPECT_FALSE(m1.Matches(a2));
737}
738
739// Tests that Eq(v) describes itself properly.
740
741class Unprintable {
742 public:
743 Unprintable() : c_('a') {}
744
zhanyong.wan32de5f52009-12-23 00:13:23 +0000745 bool operator==(const Unprintable& /* rhs */) { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000746 private:
747 char c_;
748};
749
750TEST(EqTest, CanDescribeSelf) {
751 Matcher<Unprintable> m = Eq(Unprintable());
752 EXPECT_EQ("is equal to 1-byte object <61>", Describe(m));
753}
754
755// Tests that Eq(v) can be used to match any type that supports
756// comparing with type T, where T is v's type.
757TEST(EqTest, IsPolymorphic) {
758 Matcher<int> m1 = Eq(1);
759 EXPECT_TRUE(m1.Matches(1));
760 EXPECT_FALSE(m1.Matches(2));
761
762 Matcher<char> m2 = Eq(1);
763 EXPECT_TRUE(m2.Matches('\1'));
764 EXPECT_FALSE(m2.Matches('a'));
765}
766
767// Tests that TypedEq<T>(v) matches values of type T that's equal to v.
768TEST(TypedEqTest, ChecksEqualityForGivenType) {
769 Matcher<char> m1 = TypedEq<char>('a');
770 EXPECT_TRUE(m1.Matches('a'));
771 EXPECT_FALSE(m1.Matches('b'));
772
773 Matcher<int> m2 = TypedEq<int>(6);
774 EXPECT_TRUE(m2.Matches(6));
775 EXPECT_FALSE(m2.Matches(7));
776}
777
778// Tests that TypedEq(v) describes itself properly.
779TEST(TypedEqTest, CanDescribeSelf) {
780 EXPECT_EQ("is equal to 2", Describe(TypedEq<int>(2)));
781}
782
783// Tests that TypedEq<T>(v) has type Matcher<T>.
784
785// Type<T>::IsTypeOf(v) compiles iff the type of value v is T, where T
786// is a "bare" type (i.e. not in the form of const U or U&). If v's
787// type is not T, the compiler will generate a message about
788// "undefined referece".
789template <typename T>
790struct Type {
zhanyong.wan32de5f52009-12-23 00:13:23 +0000791 static bool IsTypeOf(const T& /* v */) { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000792
793 template <typename T2>
794 static void IsTypeOf(T2 v);
795};
796
797TEST(TypedEqTest, HasSpecifiedType) {
798 // Verfies that the type of TypedEq<T>(v) is Matcher<T>.
799 Type<Matcher<int> >::IsTypeOf(TypedEq<int>(5));
800 Type<Matcher<double> >::IsTypeOf(TypedEq<double>(5));
801}
802
803// Tests that Ge(v) matches anything >= v.
804TEST(GeTest, ImplementsGreaterThanOrEqual) {
805 Matcher<int> m1 = Ge(0);
806 EXPECT_TRUE(m1.Matches(1));
807 EXPECT_TRUE(m1.Matches(0));
808 EXPECT_FALSE(m1.Matches(-1));
809}
810
811// Tests that Ge(v) describes itself properly.
812TEST(GeTest, CanDescribeSelf) {
813 Matcher<int> m = Ge(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000814 EXPECT_EQ("is >= 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000815}
816
817// Tests that Gt(v) matches anything > v.
818TEST(GtTest, ImplementsGreaterThan) {
819 Matcher<double> m1 = Gt(0);
820 EXPECT_TRUE(m1.Matches(1.0));
821 EXPECT_FALSE(m1.Matches(0.0));
822 EXPECT_FALSE(m1.Matches(-1.0));
823}
824
825// Tests that Gt(v) describes itself properly.
826TEST(GtTest, CanDescribeSelf) {
827 Matcher<int> m = Gt(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000828 EXPECT_EQ("is > 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000829}
830
831// Tests that Le(v) matches anything <= v.
832TEST(LeTest, ImplementsLessThanOrEqual) {
833 Matcher<char> m1 = Le('b');
834 EXPECT_TRUE(m1.Matches('a'));
835 EXPECT_TRUE(m1.Matches('b'));
836 EXPECT_FALSE(m1.Matches('c'));
837}
838
839// Tests that Le(v) describes itself properly.
840TEST(LeTest, CanDescribeSelf) {
841 Matcher<int> m = Le(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000842 EXPECT_EQ("is <= 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000843}
844
845// Tests that Lt(v) matches anything < v.
846TEST(LtTest, ImplementsLessThan) {
847 Matcher<const string&> m1 = Lt("Hello");
848 EXPECT_TRUE(m1.Matches("Abc"));
849 EXPECT_FALSE(m1.Matches("Hello"));
850 EXPECT_FALSE(m1.Matches("Hello, world!"));
851}
852
853// Tests that Lt(v) describes itself properly.
854TEST(LtTest, CanDescribeSelf) {
855 Matcher<int> m = Lt(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000856 EXPECT_EQ("is < 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000857}
858
859// Tests that Ne(v) matches anything != v.
860TEST(NeTest, ImplementsNotEqual) {
861 Matcher<int> m1 = Ne(0);
862 EXPECT_TRUE(m1.Matches(1));
863 EXPECT_TRUE(m1.Matches(-1));
864 EXPECT_FALSE(m1.Matches(0));
865}
866
867// Tests that Ne(v) describes itself properly.
868TEST(NeTest, CanDescribeSelf) {
869 Matcher<int> m = Ne(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000870 EXPECT_EQ("isn't equal to 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000871}
872
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000873// Tests that IsNull() matches any NULL pointer of any type.
874TEST(IsNullTest, MatchesNullPointer) {
875 Matcher<int*> m1 = IsNull();
876 int* p1 = NULL;
877 int n = 0;
878 EXPECT_TRUE(m1.Matches(p1));
879 EXPECT_FALSE(m1.Matches(&n));
880
881 Matcher<const char*> m2 = IsNull();
882 const char* p2 = NULL;
883 EXPECT_TRUE(m2.Matches(p2));
884 EXPECT_FALSE(m2.Matches("hi"));
885
zhanyong.wan95b12332009-09-25 18:55:50 +0000886#if !GTEST_OS_SYMBIAN
887 // Nokia's Symbian compiler generates:
888 // gmock-matchers.h: ambiguous access to overloaded function
889 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(void *)'
890 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(const testing::
891 // MatcherInterface<void *> *)'
892 // gmock-matchers.h: (point of instantiation: 'testing::
893 // gmock_matchers_test::IsNullTest_MatchesNullPointer_Test::TestBody()')
894 // gmock-matchers.h: (instantiating: 'testing::PolymorphicMatc
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000895 Matcher<void*> m3 = IsNull();
896 void* p3 = NULL;
897 EXPECT_TRUE(m3.Matches(p3));
898 EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef)));
zhanyong.wan95b12332009-09-25 18:55:50 +0000899#endif
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000900}
901
vladlosev79b83502009-11-18 00:43:37 +0000902TEST(IsNullTest, LinkedPtr) {
903 const Matcher<linked_ptr<int> > m = IsNull();
904 const linked_ptr<int> null_p;
905 const linked_ptr<int> non_null_p(new int);
906
907 EXPECT_TRUE(m.Matches(null_p));
908 EXPECT_FALSE(m.Matches(non_null_p));
909}
910
911TEST(IsNullTest, ReferenceToConstLinkedPtr) {
912 const Matcher<const linked_ptr<double>&> m = IsNull();
913 const linked_ptr<double> null_p;
914 const linked_ptr<double> non_null_p(new double);
915
916 EXPECT_TRUE(m.Matches(null_p));
917 EXPECT_FALSE(m.Matches(non_null_p));
918}
919
vladloseve56daa72009-11-18 01:08:08 +0000920TEST(IsNullTest, ReferenceToConstScopedPtr) {
921 const Matcher<const scoped_ptr<double>&> m = IsNull();
922 const scoped_ptr<double> null_p;
923 const scoped_ptr<double> non_null_p(new double);
924
925 EXPECT_TRUE(m.Matches(null_p));
926 EXPECT_FALSE(m.Matches(non_null_p));
927}
928
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000929// Tests that IsNull() describes itself properly.
930TEST(IsNullTest, CanDescribeSelf) {
931 Matcher<int*> m = IsNull();
932 EXPECT_EQ("is NULL", Describe(m));
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000933 EXPECT_EQ("isn't NULL", DescribeNegation(m));
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000934}
935
shiqiane35fdd92008-12-10 05:08:54 +0000936// Tests that NotNull() matches any non-NULL pointer of any type.
937TEST(NotNullTest, MatchesNonNullPointer) {
938 Matcher<int*> m1 = NotNull();
939 int* p1 = NULL;
940 int n = 0;
941 EXPECT_FALSE(m1.Matches(p1));
942 EXPECT_TRUE(m1.Matches(&n));
943
944 Matcher<const char*> m2 = NotNull();
945 const char* p2 = NULL;
946 EXPECT_FALSE(m2.Matches(p2));
947 EXPECT_TRUE(m2.Matches("hi"));
948}
949
vladlosev79b83502009-11-18 00:43:37 +0000950TEST(NotNullTest, LinkedPtr) {
951 const Matcher<linked_ptr<int> > m = NotNull();
952 const linked_ptr<int> null_p;
953 const linked_ptr<int> non_null_p(new int);
954
955 EXPECT_FALSE(m.Matches(null_p));
956 EXPECT_TRUE(m.Matches(non_null_p));
957}
958
959TEST(NotNullTest, ReferenceToConstLinkedPtr) {
960 const Matcher<const linked_ptr<double>&> m = NotNull();
961 const linked_ptr<double> null_p;
962 const linked_ptr<double> non_null_p(new double);
963
964 EXPECT_FALSE(m.Matches(null_p));
965 EXPECT_TRUE(m.Matches(non_null_p));
966}
967
vladloseve56daa72009-11-18 01:08:08 +0000968TEST(NotNullTest, ReferenceToConstScopedPtr) {
969 const Matcher<const scoped_ptr<double>&> m = NotNull();
970 const scoped_ptr<double> null_p;
971 const scoped_ptr<double> non_null_p(new double);
972
973 EXPECT_FALSE(m.Matches(null_p));
974 EXPECT_TRUE(m.Matches(non_null_p));
975}
976
shiqiane35fdd92008-12-10 05:08:54 +0000977// Tests that NotNull() describes itself properly.
978TEST(NotNullTest, CanDescribeSelf) {
979 Matcher<int*> m = NotNull();
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000980 EXPECT_EQ("isn't NULL", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000981}
982
983// Tests that Ref(variable) matches an argument that references
984// 'variable'.
985TEST(RefTest, MatchesSameVariable) {
986 int a = 0;
987 int b = 0;
988 Matcher<int&> m = Ref(a);
989 EXPECT_TRUE(m.Matches(a));
990 EXPECT_FALSE(m.Matches(b));
991}
992
993// Tests that Ref(variable) describes itself properly.
994TEST(RefTest, CanDescribeSelf) {
995 int n = 5;
996 Matcher<int&> m = Ref(n);
997 stringstream ss;
998 ss << "references the variable @" << &n << " 5";
999 EXPECT_EQ(string(ss.str()), Describe(m));
1000}
1001
1002// Test that Ref(non_const_varialbe) can be used as a matcher for a
1003// const reference.
1004TEST(RefTest, CanBeUsedAsMatcherForConstReference) {
1005 int a = 0;
1006 int b = 0;
1007 Matcher<const int&> m = Ref(a);
1008 EXPECT_TRUE(m.Matches(a));
1009 EXPECT_FALSE(m.Matches(b));
1010}
1011
1012// Tests that Ref(variable) is covariant, i.e. Ref(derived) can be
1013// used wherever Ref(base) can be used (Ref(derived) is a sub-type
1014// of Ref(base), but not vice versa.
1015
shiqiane35fdd92008-12-10 05:08:54 +00001016TEST(RefTest, IsCovariant) {
1017 Base base, base2;
1018 Derived derived;
1019 Matcher<const Base&> m1 = Ref(base);
1020 EXPECT_TRUE(m1.Matches(base));
1021 EXPECT_FALSE(m1.Matches(base2));
1022 EXPECT_FALSE(m1.Matches(derived));
1023
1024 m1 = Ref(derived);
1025 EXPECT_TRUE(m1.Matches(derived));
1026 EXPECT_FALSE(m1.Matches(base));
1027 EXPECT_FALSE(m1.Matches(base2));
1028}
1029
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001030TEST(RefTest, ExplainsResult) {
1031 int n = 0;
1032 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), n),
1033 StartsWith("which is located @"));
1034
1035 int m = 0;
1036 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), m),
1037 StartsWith("which is located @"));
1038}
1039
shiqiane35fdd92008-12-10 05:08:54 +00001040// Tests string comparison matchers.
1041
1042TEST(StrEqTest, MatchesEqualString) {
1043 Matcher<const char*> m = StrEq(string("Hello"));
1044 EXPECT_TRUE(m.Matches("Hello"));
1045 EXPECT_FALSE(m.Matches("hello"));
1046 EXPECT_FALSE(m.Matches(NULL));
1047
1048 Matcher<const string&> m2 = StrEq("Hello");
1049 EXPECT_TRUE(m2.Matches("Hello"));
1050 EXPECT_FALSE(m2.Matches("Hi"));
1051}
1052
1053TEST(StrEqTest, CanDescribeSelf) {
vladlosevaa432202011-04-01 21:58:42 +00001054 Matcher<string> m = StrEq("Hi-\'\"?\\\a\b\f\n\r\t\v\xD3");
1055 EXPECT_EQ("is equal to \"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",
shiqiane35fdd92008-12-10 05:08:54 +00001056 Describe(m));
1057
1058 string str("01204500800");
1059 str[3] = '\0';
1060 Matcher<string> m2 = StrEq(str);
1061 EXPECT_EQ("is equal to \"012\\04500800\"", Describe(m2));
1062 str[0] = str[6] = str[7] = str[9] = str[10] = '\0';
1063 Matcher<string> m3 = StrEq(str);
1064 EXPECT_EQ("is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3));
1065}
1066
1067TEST(StrNeTest, MatchesUnequalString) {
1068 Matcher<const char*> m = StrNe("Hello");
1069 EXPECT_TRUE(m.Matches(""));
1070 EXPECT_TRUE(m.Matches(NULL));
1071 EXPECT_FALSE(m.Matches("Hello"));
1072
1073 Matcher<string> m2 = StrNe(string("Hello"));
1074 EXPECT_TRUE(m2.Matches("hello"));
1075 EXPECT_FALSE(m2.Matches("Hello"));
1076}
1077
1078TEST(StrNeTest, CanDescribeSelf) {
1079 Matcher<const char*> m = StrNe("Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001080 EXPECT_EQ("isn't equal to \"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001081}
1082
1083TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
1084 Matcher<const char*> m = StrCaseEq(string("Hello"));
1085 EXPECT_TRUE(m.Matches("Hello"));
1086 EXPECT_TRUE(m.Matches("hello"));
1087 EXPECT_FALSE(m.Matches("Hi"));
1088 EXPECT_FALSE(m.Matches(NULL));
1089
1090 Matcher<const string&> m2 = StrCaseEq("Hello");
1091 EXPECT_TRUE(m2.Matches("hello"));
1092 EXPECT_FALSE(m2.Matches("Hi"));
1093}
1094
1095TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1096 string str1("oabocdooeoo");
1097 string str2("OABOCDOOEOO");
1098 Matcher<const string&> m0 = StrCaseEq(str1);
1099 EXPECT_FALSE(m0.Matches(str2 + string(1, '\0')));
1100
1101 str1[3] = str2[3] = '\0';
1102 Matcher<const string&> m1 = StrCaseEq(str1);
1103 EXPECT_TRUE(m1.Matches(str2));
1104
1105 str1[0] = str1[6] = str1[7] = str1[10] = '\0';
1106 str2[0] = str2[6] = str2[7] = str2[10] = '\0';
1107 Matcher<const string&> m2 = StrCaseEq(str1);
1108 str1[9] = str2[9] = '\0';
1109 EXPECT_FALSE(m2.Matches(str2));
1110
1111 Matcher<const string&> m3 = StrCaseEq(str1);
1112 EXPECT_TRUE(m3.Matches(str2));
1113
1114 EXPECT_FALSE(m3.Matches(str2 + "x"));
1115 str2.append(1, '\0');
1116 EXPECT_FALSE(m3.Matches(str2));
1117 EXPECT_FALSE(m3.Matches(string(str2, 0, 9)));
1118}
1119
1120TEST(StrCaseEqTest, CanDescribeSelf) {
1121 Matcher<string> m = StrCaseEq("Hi");
1122 EXPECT_EQ("is equal to (ignoring case) \"Hi\"", Describe(m));
1123}
1124
1125TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1126 Matcher<const char*> m = StrCaseNe("Hello");
1127 EXPECT_TRUE(m.Matches("Hi"));
1128 EXPECT_TRUE(m.Matches(NULL));
1129 EXPECT_FALSE(m.Matches("Hello"));
1130 EXPECT_FALSE(m.Matches("hello"));
1131
1132 Matcher<string> m2 = StrCaseNe(string("Hello"));
1133 EXPECT_TRUE(m2.Matches(""));
1134 EXPECT_FALSE(m2.Matches("Hello"));
1135}
1136
1137TEST(StrCaseNeTest, CanDescribeSelf) {
1138 Matcher<const char*> m = StrCaseNe("Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001139 EXPECT_EQ("isn't equal to (ignoring case) \"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001140}
1141
1142// Tests that HasSubstr() works for matching string-typed values.
1143TEST(HasSubstrTest, WorksForStringClasses) {
1144 const Matcher<string> m1 = HasSubstr("foo");
1145 EXPECT_TRUE(m1.Matches(string("I love food.")));
1146 EXPECT_FALSE(m1.Matches(string("tofo")));
1147
1148 const Matcher<const std::string&> m2 = HasSubstr("foo");
1149 EXPECT_TRUE(m2.Matches(std::string("I love food.")));
1150 EXPECT_FALSE(m2.Matches(std::string("tofo")));
1151}
1152
1153// Tests that HasSubstr() works for matching C-string-typed values.
1154TEST(HasSubstrTest, WorksForCStrings) {
1155 const Matcher<char*> m1 = HasSubstr("foo");
1156 EXPECT_TRUE(m1.Matches(const_cast<char*>("I love food.")));
1157 EXPECT_FALSE(m1.Matches(const_cast<char*>("tofo")));
1158 EXPECT_FALSE(m1.Matches(NULL));
1159
1160 const Matcher<const char*> m2 = HasSubstr("foo");
1161 EXPECT_TRUE(m2.Matches("I love food."));
1162 EXPECT_FALSE(m2.Matches("tofo"));
1163 EXPECT_FALSE(m2.Matches(NULL));
1164}
1165
1166// Tests that HasSubstr(s) describes itself properly.
1167TEST(HasSubstrTest, CanDescribeSelf) {
1168 Matcher<string> m = HasSubstr("foo\n\"");
1169 EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m));
1170}
1171
zhanyong.wanb5937da2009-07-16 20:26:41 +00001172TEST(KeyTest, CanDescribeSelf) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001173 Matcher<const pair<std::string, int>&> m = Key("foo");
zhanyong.wanb5937da2009-07-16 20:26:41 +00001174 EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001175 EXPECT_EQ("doesn't have a key that is equal to \"foo\"", DescribeNegation(m));
1176}
1177
1178TEST(KeyTest, ExplainsResult) {
1179 Matcher<pair<int, bool> > m = Key(GreaterThan(10));
1180 EXPECT_EQ("whose first field is a value which is 5 less than 10",
1181 Explain(m, make_pair(5, true)));
1182 EXPECT_EQ("whose first field is a value which is 5 more than 10",
1183 Explain(m, make_pair(15, true)));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001184}
1185
1186TEST(KeyTest, MatchesCorrectly) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001187 pair<int, std::string> p(25, "foo");
zhanyong.wanb5937da2009-07-16 20:26:41 +00001188 EXPECT_THAT(p, Key(25));
1189 EXPECT_THAT(p, Not(Key(42)));
1190 EXPECT_THAT(p, Key(Ge(20)));
1191 EXPECT_THAT(p, Not(Key(Lt(25))));
1192}
1193
1194TEST(KeyTest, SafelyCastsInnerMatcher) {
1195 Matcher<int> is_positive = Gt(0);
1196 Matcher<int> is_negative = Lt(0);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001197 pair<char, bool> p('a', true);
zhanyong.wanb5937da2009-07-16 20:26:41 +00001198 EXPECT_THAT(p, Key(is_positive));
1199 EXPECT_THAT(p, Not(Key(is_negative)));
1200}
1201
1202TEST(KeyTest, InsideContainsUsingMap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001203 map<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001204 container.insert(make_pair(1, 'a'));
1205 container.insert(make_pair(2, 'b'));
1206 container.insert(make_pair(4, 'c'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001207 EXPECT_THAT(container, Contains(Key(1)));
1208 EXPECT_THAT(container, Not(Contains(Key(3))));
1209}
1210
1211TEST(KeyTest, InsideContainsUsingMultimap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001212 multimap<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001213 container.insert(make_pair(1, 'a'));
1214 container.insert(make_pair(2, 'b'));
1215 container.insert(make_pair(4, 'c'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001216
1217 EXPECT_THAT(container, Not(Contains(Key(25))));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001218 container.insert(make_pair(25, 'd'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001219 EXPECT_THAT(container, Contains(Key(25)));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001220 container.insert(make_pair(25, 'e'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001221 EXPECT_THAT(container, Contains(Key(25)));
1222
1223 EXPECT_THAT(container, Contains(Key(1)));
1224 EXPECT_THAT(container, Not(Contains(Key(3))));
1225}
1226
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001227TEST(PairTest, Typing) {
1228 // Test verifies the following type conversions can be compiled.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001229 Matcher<const pair<const char*, int>&> m1 = Pair("foo", 42);
1230 Matcher<const pair<const char*, int> > m2 = Pair("foo", 42);
1231 Matcher<pair<const char*, int> > m3 = Pair("foo", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001232
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001233 Matcher<pair<int, const std::string> > m4 = Pair(25, "42");
1234 Matcher<pair<const std::string, int> > m5 = Pair("25", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001235}
1236
1237TEST(PairTest, CanDescribeSelf) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001238 Matcher<const pair<std::string, int>&> m1 = Pair("foo", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001239 EXPECT_EQ("has a first field that is equal to \"foo\""
1240 ", and has a second field that is equal to 42",
1241 Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001242 EXPECT_EQ("has a first field that isn't equal to \"foo\""
1243 ", or has a second field that isn't equal to 42",
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001244 DescribeNegation(m1));
1245 // Double and triple negation (1 or 2 times not and description of negation).
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001246 Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42));
1247 EXPECT_EQ("has a first field that isn't equal to 13"
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001248 ", and has a second field that is equal to 42",
1249 DescribeNegation(m2));
1250}
1251
1252TEST(PairTest, CanExplainMatchResultTo) {
zhanyong.wan82113312010-01-08 21:55:40 +00001253 // If neither field matches, Pair() should explain about the first
1254 // field.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001255 const Matcher<pair<int, int> > m = Pair(GreaterThan(0), GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001256 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001257 Explain(m, make_pair(-1, -2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001258
zhanyong.wan82113312010-01-08 21:55:40 +00001259 // If the first field matches but the second doesn't, Pair() should
1260 // explain about the second field.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001261 EXPECT_EQ("whose second field does not match, which is 2 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001262 Explain(m, make_pair(1, -2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001263
zhanyong.wan82113312010-01-08 21:55:40 +00001264 // If the first field doesn't match but the second does, Pair()
1265 // should explain about the first field.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001266 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001267 Explain(m, make_pair(-1, 2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001268
zhanyong.wan82113312010-01-08 21:55:40 +00001269 // If both fields match, Pair() should explain about them both.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001270 EXPECT_EQ("whose both fields match, where the first field is a value "
1271 "which is 1 more than 0, and the second field is a value "
1272 "which is 2 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001273 Explain(m, make_pair(1, 2)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001274
1275 // If only the first match has an explanation, only this explanation should
1276 // be printed.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001277 const Matcher<pair<int, int> > explain_first = Pair(GreaterThan(0), 0);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001278 EXPECT_EQ("whose both fields match, where the first field is a value "
1279 "which is 1 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001280 Explain(explain_first, make_pair(1, 0)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001281
1282 // If only the second match has an explanation, only this explanation should
1283 // be printed.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001284 const Matcher<pair<int, int> > explain_second = Pair(0, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001285 EXPECT_EQ("whose both fields match, where the second field is a value "
1286 "which is 1 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001287 Explain(explain_second, make_pair(0, 1)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001288}
1289
1290TEST(PairTest, MatchesCorrectly) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001291 pair<int, std::string> p(25, "foo");
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001292
1293 // Both fields match.
1294 EXPECT_THAT(p, Pair(25, "foo"));
1295 EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o")));
1296
1297 // 'first' doesnt' match, but 'second' matches.
1298 EXPECT_THAT(p, Not(Pair(42, "foo")));
1299 EXPECT_THAT(p, Not(Pair(Lt(25), "foo")));
1300
1301 // 'first' matches, but 'second' doesn't match.
1302 EXPECT_THAT(p, Not(Pair(25, "bar")));
1303 EXPECT_THAT(p, Not(Pair(25, Not("foo"))));
1304
1305 // Neither field matches.
1306 EXPECT_THAT(p, Not(Pair(13, "bar")));
1307 EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a"))));
1308}
1309
1310TEST(PairTest, SafelyCastsInnerMatchers) {
1311 Matcher<int> is_positive = Gt(0);
1312 Matcher<int> is_negative = Lt(0);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001313 pair<char, bool> p('a', true);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001314 EXPECT_THAT(p, Pair(is_positive, _));
1315 EXPECT_THAT(p, Not(Pair(is_negative, _)));
1316 EXPECT_THAT(p, Pair(_, is_positive));
1317 EXPECT_THAT(p, Not(Pair(_, is_negative)));
1318}
1319
1320TEST(PairTest, InsideContainsUsingMap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001321 map<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001322 container.insert(make_pair(1, 'a'));
1323 container.insert(make_pair(2, 'b'));
1324 container.insert(make_pair(4, 'c'));
zhanyong.wan95b12332009-09-25 18:55:50 +00001325 EXPECT_THAT(container, Contains(Pair(1, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001326 EXPECT_THAT(container, Contains(Pair(1, _)));
zhanyong.wan95b12332009-09-25 18:55:50 +00001327 EXPECT_THAT(container, Contains(Pair(_, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001328 EXPECT_THAT(container, Not(Contains(Pair(3, _))));
1329}
1330
shiqiane35fdd92008-12-10 05:08:54 +00001331// Tests StartsWith(s).
1332
1333TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
1334 const Matcher<const char*> m1 = StartsWith(string(""));
1335 EXPECT_TRUE(m1.Matches("Hi"));
1336 EXPECT_TRUE(m1.Matches(""));
1337 EXPECT_FALSE(m1.Matches(NULL));
1338
1339 const Matcher<const string&> m2 = StartsWith("Hi");
1340 EXPECT_TRUE(m2.Matches("Hi"));
1341 EXPECT_TRUE(m2.Matches("Hi Hi!"));
1342 EXPECT_TRUE(m2.Matches("High"));
1343 EXPECT_FALSE(m2.Matches("H"));
1344 EXPECT_FALSE(m2.Matches(" Hi"));
1345}
1346
1347TEST(StartsWithTest, CanDescribeSelf) {
1348 Matcher<const std::string> m = StartsWith("Hi");
1349 EXPECT_EQ("starts with \"Hi\"", Describe(m));
1350}
1351
1352// Tests EndsWith(s).
1353
1354TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
1355 const Matcher<const char*> m1 = EndsWith("");
1356 EXPECT_TRUE(m1.Matches("Hi"));
1357 EXPECT_TRUE(m1.Matches(""));
1358 EXPECT_FALSE(m1.Matches(NULL));
1359
1360 const Matcher<const string&> m2 = EndsWith(string("Hi"));
1361 EXPECT_TRUE(m2.Matches("Hi"));
1362 EXPECT_TRUE(m2.Matches("Wow Hi Hi"));
1363 EXPECT_TRUE(m2.Matches("Super Hi"));
1364 EXPECT_FALSE(m2.Matches("i"));
1365 EXPECT_FALSE(m2.Matches("Hi "));
1366}
1367
1368TEST(EndsWithTest, CanDescribeSelf) {
1369 Matcher<const std::string> m = EndsWith("Hi");
1370 EXPECT_EQ("ends with \"Hi\"", Describe(m));
1371}
1372
shiqiane35fdd92008-12-10 05:08:54 +00001373// Tests MatchesRegex().
1374
1375TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
1376 const Matcher<const char*> m1 = MatchesRegex("a.*z");
1377 EXPECT_TRUE(m1.Matches("az"));
1378 EXPECT_TRUE(m1.Matches("abcz"));
1379 EXPECT_FALSE(m1.Matches(NULL));
1380
1381 const Matcher<const string&> m2 = MatchesRegex(new RE("a.*z"));
1382 EXPECT_TRUE(m2.Matches("azbz"));
1383 EXPECT_FALSE(m2.Matches("az1"));
1384 EXPECT_FALSE(m2.Matches("1az"));
1385}
1386
1387TEST(MatchesRegexTest, CanDescribeSelf) {
1388 Matcher<const std::string> m1 = MatchesRegex(string("Hi.*"));
1389 EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1));
1390
zhanyong.wand14aaed2010-01-14 05:36:32 +00001391 Matcher<const char*> m2 = MatchesRegex(new RE("a.*"));
1392 EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +00001393}
1394
1395// Tests ContainsRegex().
1396
1397TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
1398 const Matcher<const char*> m1 = ContainsRegex(string("a.*z"));
1399 EXPECT_TRUE(m1.Matches("az"));
1400 EXPECT_TRUE(m1.Matches("0abcz1"));
1401 EXPECT_FALSE(m1.Matches(NULL));
1402
1403 const Matcher<const string&> m2 = ContainsRegex(new RE("a.*z"));
1404 EXPECT_TRUE(m2.Matches("azbz"));
1405 EXPECT_TRUE(m2.Matches("az1"));
1406 EXPECT_FALSE(m2.Matches("1a"));
1407}
1408
1409TEST(ContainsRegexTest, CanDescribeSelf) {
1410 Matcher<const std::string> m1 = ContainsRegex("Hi.*");
1411 EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1));
1412
zhanyong.wand14aaed2010-01-14 05:36:32 +00001413 Matcher<const char*> m2 = ContainsRegex(new RE("a.*"));
1414 EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +00001415}
shiqiane35fdd92008-12-10 05:08:54 +00001416
1417// Tests for wide strings.
1418#if GTEST_HAS_STD_WSTRING
1419TEST(StdWideStrEqTest, MatchesEqual) {
1420 Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello"));
1421 EXPECT_TRUE(m.Matches(L"Hello"));
1422 EXPECT_FALSE(m.Matches(L"hello"));
1423 EXPECT_FALSE(m.Matches(NULL));
1424
1425 Matcher<const ::std::wstring&> m2 = StrEq(L"Hello");
1426 EXPECT_TRUE(m2.Matches(L"Hello"));
1427 EXPECT_FALSE(m2.Matches(L"Hi"));
1428
1429 Matcher<const ::std::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1430 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1431 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1432
1433 ::std::wstring str(L"01204500800");
1434 str[3] = L'\0';
1435 Matcher<const ::std::wstring&> m4 = StrEq(str);
1436 EXPECT_TRUE(m4.Matches(str));
1437 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1438 Matcher<const ::std::wstring&> m5 = StrEq(str);
1439 EXPECT_TRUE(m5.Matches(str));
1440}
1441
1442TEST(StdWideStrEqTest, CanDescribeSelf) {
vladlosevaa432202011-04-01 21:58:42 +00001443 Matcher< ::std::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v");
1444 EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
shiqiane35fdd92008-12-10 05:08:54 +00001445 Describe(m));
1446
1447 Matcher< ::std::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1448 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1449 Describe(m2));
1450
1451 ::std::wstring str(L"01204500800");
1452 str[3] = L'\0';
1453 Matcher<const ::std::wstring&> m4 = StrEq(str);
1454 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1455 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1456 Matcher<const ::std::wstring&> m5 = StrEq(str);
1457 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1458}
1459
1460TEST(StdWideStrNeTest, MatchesUnequalString) {
1461 Matcher<const wchar_t*> m = StrNe(L"Hello");
1462 EXPECT_TRUE(m.Matches(L""));
1463 EXPECT_TRUE(m.Matches(NULL));
1464 EXPECT_FALSE(m.Matches(L"Hello"));
1465
1466 Matcher< ::std::wstring> m2 = StrNe(::std::wstring(L"Hello"));
1467 EXPECT_TRUE(m2.Matches(L"hello"));
1468 EXPECT_FALSE(m2.Matches(L"Hello"));
1469}
1470
1471TEST(StdWideStrNeTest, CanDescribeSelf) {
1472 Matcher<const wchar_t*> m = StrNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001473 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001474}
1475
1476TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1477 Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello"));
1478 EXPECT_TRUE(m.Matches(L"Hello"));
1479 EXPECT_TRUE(m.Matches(L"hello"));
1480 EXPECT_FALSE(m.Matches(L"Hi"));
1481 EXPECT_FALSE(m.Matches(NULL));
1482
1483 Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello");
1484 EXPECT_TRUE(m2.Matches(L"hello"));
1485 EXPECT_FALSE(m2.Matches(L"Hi"));
1486}
1487
1488TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1489 ::std::wstring str1(L"oabocdooeoo");
1490 ::std::wstring str2(L"OABOCDOOEOO");
1491 Matcher<const ::std::wstring&> m0 = StrCaseEq(str1);
1492 EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L'\0')));
1493
1494 str1[3] = str2[3] = L'\0';
1495 Matcher<const ::std::wstring&> m1 = StrCaseEq(str1);
1496 EXPECT_TRUE(m1.Matches(str2));
1497
1498 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1499 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1500 Matcher<const ::std::wstring&> m2 = StrCaseEq(str1);
1501 str1[9] = str2[9] = L'\0';
1502 EXPECT_FALSE(m2.Matches(str2));
1503
1504 Matcher<const ::std::wstring&> m3 = StrCaseEq(str1);
1505 EXPECT_TRUE(m3.Matches(str2));
1506
1507 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1508 str2.append(1, L'\0');
1509 EXPECT_FALSE(m3.Matches(str2));
1510 EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9)));
1511}
1512
1513TEST(StdWideStrCaseEqTest, CanDescribeSelf) {
1514 Matcher< ::std::wstring> m = StrCaseEq(L"Hi");
1515 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1516}
1517
1518TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1519 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1520 EXPECT_TRUE(m.Matches(L"Hi"));
1521 EXPECT_TRUE(m.Matches(NULL));
1522 EXPECT_FALSE(m.Matches(L"Hello"));
1523 EXPECT_FALSE(m.Matches(L"hello"));
1524
1525 Matcher< ::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello"));
1526 EXPECT_TRUE(m2.Matches(L""));
1527 EXPECT_FALSE(m2.Matches(L"Hello"));
1528}
1529
1530TEST(StdWideStrCaseNeTest, CanDescribeSelf) {
1531 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001532 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001533}
1534
1535// Tests that HasSubstr() works for matching wstring-typed values.
1536TEST(StdWideHasSubstrTest, WorksForStringClasses) {
1537 const Matcher< ::std::wstring> m1 = HasSubstr(L"foo");
1538 EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food.")));
1539 EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo")));
1540
1541 const Matcher<const ::std::wstring&> m2 = HasSubstr(L"foo");
1542 EXPECT_TRUE(m2.Matches(::std::wstring(L"I love food.")));
1543 EXPECT_FALSE(m2.Matches(::std::wstring(L"tofo")));
1544}
1545
1546// Tests that HasSubstr() works for matching C-wide-string-typed values.
1547TEST(StdWideHasSubstrTest, WorksForCStrings) {
1548 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1549 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1550 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1551 EXPECT_FALSE(m1.Matches(NULL));
1552
1553 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1554 EXPECT_TRUE(m2.Matches(L"I love food."));
1555 EXPECT_FALSE(m2.Matches(L"tofo"));
1556 EXPECT_FALSE(m2.Matches(NULL));
1557}
1558
1559// Tests that HasSubstr(s) describes itself properly.
1560TEST(StdWideHasSubstrTest, CanDescribeSelf) {
1561 Matcher< ::std::wstring> m = HasSubstr(L"foo\n\"");
1562 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1563}
1564
1565// Tests StartsWith(s).
1566
1567TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) {
1568 const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L""));
1569 EXPECT_TRUE(m1.Matches(L"Hi"));
1570 EXPECT_TRUE(m1.Matches(L""));
1571 EXPECT_FALSE(m1.Matches(NULL));
1572
1573 const Matcher<const ::std::wstring&> m2 = StartsWith(L"Hi");
1574 EXPECT_TRUE(m2.Matches(L"Hi"));
1575 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1576 EXPECT_TRUE(m2.Matches(L"High"));
1577 EXPECT_FALSE(m2.Matches(L"H"));
1578 EXPECT_FALSE(m2.Matches(L" Hi"));
1579}
1580
1581TEST(StdWideStartsWithTest, CanDescribeSelf) {
1582 Matcher<const ::std::wstring> m = StartsWith(L"Hi");
1583 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1584}
1585
1586// Tests EndsWith(s).
1587
1588TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) {
1589 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1590 EXPECT_TRUE(m1.Matches(L"Hi"));
1591 EXPECT_TRUE(m1.Matches(L""));
1592 EXPECT_FALSE(m1.Matches(NULL));
1593
1594 const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L"Hi"));
1595 EXPECT_TRUE(m2.Matches(L"Hi"));
1596 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1597 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1598 EXPECT_FALSE(m2.Matches(L"i"));
1599 EXPECT_FALSE(m2.Matches(L"Hi "));
1600}
1601
1602TEST(StdWideEndsWithTest, CanDescribeSelf) {
1603 Matcher<const ::std::wstring> m = EndsWith(L"Hi");
1604 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1605}
1606
1607#endif // GTEST_HAS_STD_WSTRING
1608
1609#if GTEST_HAS_GLOBAL_WSTRING
1610TEST(GlobalWideStrEqTest, MatchesEqual) {
1611 Matcher<const wchar_t*> m = StrEq(::wstring(L"Hello"));
1612 EXPECT_TRUE(m.Matches(L"Hello"));
1613 EXPECT_FALSE(m.Matches(L"hello"));
1614 EXPECT_FALSE(m.Matches(NULL));
1615
1616 Matcher<const ::wstring&> m2 = StrEq(L"Hello");
1617 EXPECT_TRUE(m2.Matches(L"Hello"));
1618 EXPECT_FALSE(m2.Matches(L"Hi"));
1619
1620 Matcher<const ::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1621 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1622 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1623
1624 ::wstring str(L"01204500800");
1625 str[3] = L'\0';
1626 Matcher<const ::wstring&> m4 = StrEq(str);
1627 EXPECT_TRUE(m4.Matches(str));
1628 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1629 Matcher<const ::wstring&> m5 = StrEq(str);
1630 EXPECT_TRUE(m5.Matches(str));
1631}
1632
1633TEST(GlobalWideStrEqTest, CanDescribeSelf) {
vladlosevaa432202011-04-01 21:58:42 +00001634 Matcher< ::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v");
1635 EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
shiqiane35fdd92008-12-10 05:08:54 +00001636 Describe(m));
1637
1638 Matcher< ::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1639 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1640 Describe(m2));
1641
1642 ::wstring str(L"01204500800");
1643 str[3] = L'\0';
1644 Matcher<const ::wstring&> m4 = StrEq(str);
1645 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1646 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1647 Matcher<const ::wstring&> m5 = StrEq(str);
1648 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1649}
1650
1651TEST(GlobalWideStrNeTest, MatchesUnequalString) {
1652 Matcher<const wchar_t*> m = StrNe(L"Hello");
1653 EXPECT_TRUE(m.Matches(L""));
1654 EXPECT_TRUE(m.Matches(NULL));
1655 EXPECT_FALSE(m.Matches(L"Hello"));
1656
1657 Matcher< ::wstring> m2 = StrNe(::wstring(L"Hello"));
1658 EXPECT_TRUE(m2.Matches(L"hello"));
1659 EXPECT_FALSE(m2.Matches(L"Hello"));
1660}
1661
1662TEST(GlobalWideStrNeTest, CanDescribeSelf) {
1663 Matcher<const wchar_t*> m = StrNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001664 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001665}
1666
1667TEST(GlobalWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1668 Matcher<const wchar_t*> m = StrCaseEq(::wstring(L"Hello"));
1669 EXPECT_TRUE(m.Matches(L"Hello"));
1670 EXPECT_TRUE(m.Matches(L"hello"));
1671 EXPECT_FALSE(m.Matches(L"Hi"));
1672 EXPECT_FALSE(m.Matches(NULL));
1673
1674 Matcher<const ::wstring&> m2 = StrCaseEq(L"Hello");
1675 EXPECT_TRUE(m2.Matches(L"hello"));
1676 EXPECT_FALSE(m2.Matches(L"Hi"));
1677}
1678
1679TEST(GlobalWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1680 ::wstring str1(L"oabocdooeoo");
1681 ::wstring str2(L"OABOCDOOEOO");
1682 Matcher<const ::wstring&> m0 = StrCaseEq(str1);
1683 EXPECT_FALSE(m0.Matches(str2 + ::wstring(1, L'\0')));
1684
1685 str1[3] = str2[3] = L'\0';
1686 Matcher<const ::wstring&> m1 = StrCaseEq(str1);
1687 EXPECT_TRUE(m1.Matches(str2));
1688
1689 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1690 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1691 Matcher<const ::wstring&> m2 = StrCaseEq(str1);
1692 str1[9] = str2[9] = L'\0';
1693 EXPECT_FALSE(m2.Matches(str2));
1694
1695 Matcher<const ::wstring&> m3 = StrCaseEq(str1);
1696 EXPECT_TRUE(m3.Matches(str2));
1697
1698 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1699 str2.append(1, L'\0');
1700 EXPECT_FALSE(m3.Matches(str2));
1701 EXPECT_FALSE(m3.Matches(::wstring(str2, 0, 9)));
1702}
1703
1704TEST(GlobalWideStrCaseEqTest, CanDescribeSelf) {
1705 Matcher< ::wstring> m = StrCaseEq(L"Hi");
1706 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1707}
1708
1709TEST(GlobalWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1710 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1711 EXPECT_TRUE(m.Matches(L"Hi"));
1712 EXPECT_TRUE(m.Matches(NULL));
1713 EXPECT_FALSE(m.Matches(L"Hello"));
1714 EXPECT_FALSE(m.Matches(L"hello"));
1715
1716 Matcher< ::wstring> m2 = StrCaseNe(::wstring(L"Hello"));
1717 EXPECT_TRUE(m2.Matches(L""));
1718 EXPECT_FALSE(m2.Matches(L"Hello"));
1719}
1720
1721TEST(GlobalWideStrCaseNeTest, CanDescribeSelf) {
1722 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001723 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001724}
1725
1726// Tests that HasSubstr() works for matching wstring-typed values.
1727TEST(GlobalWideHasSubstrTest, WorksForStringClasses) {
1728 const Matcher< ::wstring> m1 = HasSubstr(L"foo");
1729 EXPECT_TRUE(m1.Matches(::wstring(L"I love food.")));
1730 EXPECT_FALSE(m1.Matches(::wstring(L"tofo")));
1731
1732 const Matcher<const ::wstring&> m2 = HasSubstr(L"foo");
1733 EXPECT_TRUE(m2.Matches(::wstring(L"I love food.")));
1734 EXPECT_FALSE(m2.Matches(::wstring(L"tofo")));
1735}
1736
1737// Tests that HasSubstr() works for matching C-wide-string-typed values.
1738TEST(GlobalWideHasSubstrTest, WorksForCStrings) {
1739 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1740 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1741 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1742 EXPECT_FALSE(m1.Matches(NULL));
1743
1744 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1745 EXPECT_TRUE(m2.Matches(L"I love food."));
1746 EXPECT_FALSE(m2.Matches(L"tofo"));
1747 EXPECT_FALSE(m2.Matches(NULL));
1748}
1749
1750// Tests that HasSubstr(s) describes itself properly.
1751TEST(GlobalWideHasSubstrTest, CanDescribeSelf) {
1752 Matcher< ::wstring> m = HasSubstr(L"foo\n\"");
1753 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1754}
1755
1756// Tests StartsWith(s).
1757
1758TEST(GlobalWideStartsWithTest, MatchesStringWithGivenPrefix) {
1759 const Matcher<const wchar_t*> m1 = StartsWith(::wstring(L""));
1760 EXPECT_TRUE(m1.Matches(L"Hi"));
1761 EXPECT_TRUE(m1.Matches(L""));
1762 EXPECT_FALSE(m1.Matches(NULL));
1763
1764 const Matcher<const ::wstring&> m2 = StartsWith(L"Hi");
1765 EXPECT_TRUE(m2.Matches(L"Hi"));
1766 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1767 EXPECT_TRUE(m2.Matches(L"High"));
1768 EXPECT_FALSE(m2.Matches(L"H"));
1769 EXPECT_FALSE(m2.Matches(L" Hi"));
1770}
1771
1772TEST(GlobalWideStartsWithTest, CanDescribeSelf) {
1773 Matcher<const ::wstring> m = StartsWith(L"Hi");
1774 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1775}
1776
1777// Tests EndsWith(s).
1778
1779TEST(GlobalWideEndsWithTest, MatchesStringWithGivenSuffix) {
1780 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1781 EXPECT_TRUE(m1.Matches(L"Hi"));
1782 EXPECT_TRUE(m1.Matches(L""));
1783 EXPECT_FALSE(m1.Matches(NULL));
1784
1785 const Matcher<const ::wstring&> m2 = EndsWith(::wstring(L"Hi"));
1786 EXPECT_TRUE(m2.Matches(L"Hi"));
1787 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1788 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1789 EXPECT_FALSE(m2.Matches(L"i"));
1790 EXPECT_FALSE(m2.Matches(L"Hi "));
1791}
1792
1793TEST(GlobalWideEndsWithTest, CanDescribeSelf) {
1794 Matcher<const ::wstring> m = EndsWith(L"Hi");
1795 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1796}
1797
1798#endif // GTEST_HAS_GLOBAL_WSTRING
1799
1800
1801typedef ::std::tr1::tuple<long, int> Tuple2; // NOLINT
1802
1803// Tests that Eq() matches a 2-tuple where the first field == the
1804// second field.
1805TEST(Eq2Test, MatchesEqualArguments) {
1806 Matcher<const Tuple2&> m = Eq();
1807 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1808 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1809}
1810
1811// Tests that Eq() describes itself properly.
1812TEST(Eq2Test, CanDescribeSelf) {
1813 Matcher<const Tuple2&> m = Eq();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001814 EXPECT_EQ("are an equal pair", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001815}
1816
1817// Tests that Ge() matches a 2-tuple where the first field >= the
1818// second field.
1819TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) {
1820 Matcher<const Tuple2&> m = Ge();
1821 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1822 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1823 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1824}
1825
1826// Tests that Ge() describes itself properly.
1827TEST(Ge2Test, CanDescribeSelf) {
1828 Matcher<const Tuple2&> m = Ge();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001829 EXPECT_EQ("are a pair where the first >= the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001830}
1831
1832// Tests that Gt() matches a 2-tuple where the first field > the
1833// second field.
1834TEST(Gt2Test, MatchesGreaterThanArguments) {
1835 Matcher<const Tuple2&> m = Gt();
1836 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1837 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1838 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1839}
1840
1841// Tests that Gt() describes itself properly.
1842TEST(Gt2Test, CanDescribeSelf) {
1843 Matcher<const Tuple2&> m = Gt();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001844 EXPECT_EQ("are a pair where the first > the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001845}
1846
1847// Tests that Le() matches a 2-tuple where the first field <= the
1848// second field.
1849TEST(Le2Test, MatchesLessThanOrEqualArguments) {
1850 Matcher<const Tuple2&> m = Le();
1851 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1852 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1853 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1854}
1855
1856// Tests that Le() describes itself properly.
1857TEST(Le2Test, CanDescribeSelf) {
1858 Matcher<const Tuple2&> m = Le();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001859 EXPECT_EQ("are a pair where the first <= the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001860}
1861
1862// Tests that Lt() matches a 2-tuple where the first field < the
1863// second field.
1864TEST(Lt2Test, MatchesLessThanArguments) {
1865 Matcher<const Tuple2&> m = Lt();
1866 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1867 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1868 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1869}
1870
1871// Tests that Lt() describes itself properly.
1872TEST(Lt2Test, CanDescribeSelf) {
1873 Matcher<const Tuple2&> m = Lt();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001874 EXPECT_EQ("are a pair where the first < the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001875}
1876
1877// Tests that Ne() matches a 2-tuple where the first field != the
1878// second field.
1879TEST(Ne2Test, MatchesUnequalArguments) {
1880 Matcher<const Tuple2&> m = Ne();
1881 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1882 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1883 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1884}
1885
1886// Tests that Ne() describes itself properly.
1887TEST(Ne2Test, CanDescribeSelf) {
1888 Matcher<const Tuple2&> m = Ne();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001889 EXPECT_EQ("are an unequal pair", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001890}
1891
1892// Tests that Not(m) matches any value that doesn't match m.
1893TEST(NotTest, NegatesMatcher) {
1894 Matcher<int> m;
1895 m = Not(Eq(2));
1896 EXPECT_TRUE(m.Matches(3));
1897 EXPECT_FALSE(m.Matches(2));
1898}
1899
1900// Tests that Not(m) describes itself properly.
1901TEST(NotTest, CanDescribeSelf) {
1902 Matcher<int> m = Not(Eq(5));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001903 EXPECT_EQ("isn't equal to 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001904}
1905
zhanyong.wan18490652009-05-11 18:54:08 +00001906// Tests that monomorphic matchers are safely cast by the Not matcher.
1907TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {
1908 // greater_than_5 is a monomorphic matcher.
1909 Matcher<int> greater_than_5 = Gt(5);
1910
1911 Matcher<const int&> m = Not(greater_than_5);
1912 Matcher<int&> m2 = Not(greater_than_5);
1913 Matcher<int&> m3 = Not(m);
1914}
1915
zhanyong.wan02c15052010-06-09 19:21:30 +00001916// Helper to allow easy testing of AllOf matchers with num parameters.
1917void AllOfMatches(int num, const Matcher<int>& m) {
1918 SCOPED_TRACE(Describe(m));
1919 EXPECT_TRUE(m.Matches(0));
1920 for (int i = 1; i <= num; ++i) {
1921 EXPECT_FALSE(m.Matches(i));
1922 }
1923 EXPECT_TRUE(m.Matches(num + 1));
1924}
1925
shiqiane35fdd92008-12-10 05:08:54 +00001926// Tests that AllOf(m1, ..., mn) matches any value that matches all of
1927// the given matchers.
1928TEST(AllOfTest, MatchesWhenAllMatch) {
1929 Matcher<int> m;
1930 m = AllOf(Le(2), Ge(1));
1931 EXPECT_TRUE(m.Matches(1));
1932 EXPECT_TRUE(m.Matches(2));
1933 EXPECT_FALSE(m.Matches(0));
1934 EXPECT_FALSE(m.Matches(3));
1935
1936 m = AllOf(Gt(0), Ne(1), Ne(2));
1937 EXPECT_TRUE(m.Matches(3));
1938 EXPECT_FALSE(m.Matches(2));
1939 EXPECT_FALSE(m.Matches(1));
1940 EXPECT_FALSE(m.Matches(0));
1941
1942 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
1943 EXPECT_TRUE(m.Matches(4));
1944 EXPECT_FALSE(m.Matches(3));
1945 EXPECT_FALSE(m.Matches(2));
1946 EXPECT_FALSE(m.Matches(1));
1947 EXPECT_FALSE(m.Matches(0));
1948
1949 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
1950 EXPECT_TRUE(m.Matches(0));
1951 EXPECT_TRUE(m.Matches(1));
1952 EXPECT_FALSE(m.Matches(3));
zhanyong.wan02c15052010-06-09 19:21:30 +00001953
1954 // The following tests for varying number of sub-matchers. Due to the way
1955 // the sub-matchers are handled it is enough to test every sub-matcher once
1956 // with sub-matchers using the same matcher type. Varying matcher types are
1957 // checked for above.
1958 AllOfMatches(2, AllOf(Ne(1), Ne(2)));
1959 AllOfMatches(3, AllOf(Ne(1), Ne(2), Ne(3)));
1960 AllOfMatches(4, AllOf(Ne(1), Ne(2), Ne(3), Ne(4)));
1961 AllOfMatches(5, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5)));
1962 AllOfMatches(6, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6)));
1963 AllOfMatches(7, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7)));
1964 AllOfMatches(8, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
1965 Ne(8)));
1966 AllOfMatches(9, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
1967 Ne(8), Ne(9)));
1968 AllOfMatches(10, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8),
1969 Ne(9), Ne(10)));
shiqiane35fdd92008-12-10 05:08:54 +00001970}
1971
1972// Tests that AllOf(m1, ..., mn) describes itself properly.
1973TEST(AllOfTest, CanDescribeSelf) {
1974 Matcher<int> m;
1975 m = AllOf(Le(2), Ge(1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001976 EXPECT_EQ("(is <= 2) and (is >= 1)", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001977
1978 m = AllOf(Gt(0), Ne(1), Ne(2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001979 EXPECT_EQ("(is > 0) and "
1980 "((isn't equal to 1) and "
1981 "(isn't equal to 2))",
shiqiane35fdd92008-12-10 05:08:54 +00001982 Describe(m));
1983
1984
1985 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
jgm79a367e2012-04-10 16:02:11 +00001986 EXPECT_EQ("((is > 0) and "
1987 "(isn't equal to 1)) and "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001988 "((isn't equal to 2) and "
jgm79a367e2012-04-10 16:02:11 +00001989 "(isn't equal to 3))",
shiqiane35fdd92008-12-10 05:08:54 +00001990 Describe(m));
1991
1992
1993 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
jgm79a367e2012-04-10 16:02:11 +00001994 EXPECT_EQ("((is >= 0) and "
1995 "(is < 10)) and "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001996 "((isn't equal to 3) and "
1997 "((isn't equal to 5) and "
jgm79a367e2012-04-10 16:02:11 +00001998 "(isn't equal to 7)))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001999 Describe(m));
2000}
2001
2002// Tests that AllOf(m1, ..., mn) describes its negation properly.
2003TEST(AllOfTest, CanDescribeNegation) {
2004 Matcher<int> m;
2005 m = AllOf(Le(2), Ge(1));
2006 EXPECT_EQ("(isn't <= 2) or "
2007 "(isn't >= 1)",
2008 DescribeNegation(m));
2009
2010 m = AllOf(Gt(0), Ne(1), Ne(2));
2011 EXPECT_EQ("(isn't > 0) or "
2012 "((is equal to 1) or "
2013 "(is equal to 2))",
2014 DescribeNegation(m));
2015
2016
2017 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
jgm79a367e2012-04-10 16:02:11 +00002018 EXPECT_EQ("((isn't > 0) or "
2019 "(is equal to 1)) or "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002020 "((is equal to 2) or "
jgm79a367e2012-04-10 16:02:11 +00002021 "(is equal to 3))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002022 DescribeNegation(m));
2023
2024
2025 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
jgm79a367e2012-04-10 16:02:11 +00002026 EXPECT_EQ("((isn't >= 0) or "
2027 "(isn't < 10)) or "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002028 "((is equal to 3) or "
2029 "((is equal to 5) or "
jgm79a367e2012-04-10 16:02:11 +00002030 "(is equal to 7)))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002031 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00002032}
2033
zhanyong.wan18490652009-05-11 18:54:08 +00002034// Tests that monomorphic matchers are safely cast by the AllOf matcher.
2035TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
2036 // greater_than_5 and less_than_10 are monomorphic matchers.
2037 Matcher<int> greater_than_5 = Gt(5);
2038 Matcher<int> less_than_10 = Lt(10);
2039
2040 Matcher<const int&> m = AllOf(greater_than_5, less_than_10);
2041 Matcher<int&> m2 = AllOf(greater_than_5, less_than_10);
2042 Matcher<int&> m3 = AllOf(greater_than_5, m2);
2043
2044 // Tests that BothOf works when composing itself.
2045 Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10);
2046 Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);
2047}
2048
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002049TEST(AllOfTest, ExplainsResult) {
2050 Matcher<int> m;
2051
2052 // Successful match. Both matchers need to explain. The second
2053 // matcher doesn't give an explanation, so only the first matcher's
2054 // explanation is printed.
2055 m = AllOf(GreaterThan(10), Lt(30));
2056 EXPECT_EQ("which is 15 more than 10", Explain(m, 25));
2057
2058 // Successful match. Both matchers need to explain.
2059 m = AllOf(GreaterThan(10), GreaterThan(20));
2060 EXPECT_EQ("which is 20 more than 10, and which is 10 more than 20",
2061 Explain(m, 30));
2062
2063 // Successful match. All matchers need to explain. The second
2064 // matcher doesn't given an explanation.
2065 m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20));
2066 EXPECT_EQ("which is 15 more than 10, and which is 5 more than 20",
2067 Explain(m, 25));
2068
2069 // Successful match. All matchers need to explain.
2070 m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2071 EXPECT_EQ("which is 30 more than 10, and which is 20 more than 20, "
2072 "and which is 10 more than 30",
2073 Explain(m, 40));
2074
2075 // Failed match. The first matcher, which failed, needs to
2076 // explain.
2077 m = AllOf(GreaterThan(10), GreaterThan(20));
2078 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
2079
2080 // Failed match. The second matcher, which failed, needs to
2081 // explain. Since it doesn't given an explanation, nothing is
2082 // printed.
2083 m = AllOf(GreaterThan(10), Lt(30));
2084 EXPECT_EQ("", Explain(m, 40));
2085
2086 // Failed match. The second matcher, which failed, needs to
2087 // explain.
2088 m = AllOf(GreaterThan(10), GreaterThan(20));
2089 EXPECT_EQ("which is 5 less than 20", Explain(m, 15));
2090}
2091
zhanyong.wan02c15052010-06-09 19:21:30 +00002092// Helper to allow easy testing of AnyOf matchers with num parameters.
2093void AnyOfMatches(int num, const Matcher<int>& m) {
2094 SCOPED_TRACE(Describe(m));
2095 EXPECT_FALSE(m.Matches(0));
2096 for (int i = 1; i <= num; ++i) {
2097 EXPECT_TRUE(m.Matches(i));
2098 }
2099 EXPECT_FALSE(m.Matches(num + 1));
2100}
2101
shiqiane35fdd92008-12-10 05:08:54 +00002102// Tests that AnyOf(m1, ..., mn) matches any value that matches at
2103// least one of the given matchers.
2104TEST(AnyOfTest, MatchesWhenAnyMatches) {
2105 Matcher<int> m;
2106 m = AnyOf(Le(1), Ge(3));
2107 EXPECT_TRUE(m.Matches(1));
2108 EXPECT_TRUE(m.Matches(4));
2109 EXPECT_FALSE(m.Matches(2));
2110
2111 m = AnyOf(Lt(0), Eq(1), Eq(2));
2112 EXPECT_TRUE(m.Matches(-1));
2113 EXPECT_TRUE(m.Matches(1));
2114 EXPECT_TRUE(m.Matches(2));
2115 EXPECT_FALSE(m.Matches(0));
2116
2117 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2118 EXPECT_TRUE(m.Matches(-1));
2119 EXPECT_TRUE(m.Matches(1));
2120 EXPECT_TRUE(m.Matches(2));
2121 EXPECT_TRUE(m.Matches(3));
2122 EXPECT_FALSE(m.Matches(0));
2123
2124 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2125 EXPECT_TRUE(m.Matches(0));
2126 EXPECT_TRUE(m.Matches(11));
2127 EXPECT_TRUE(m.Matches(3));
2128 EXPECT_FALSE(m.Matches(2));
zhanyong.wan02c15052010-06-09 19:21:30 +00002129
2130 // The following tests for varying number of sub-matchers. Due to the way
2131 // the sub-matchers are handled it is enough to test every sub-matcher once
2132 // with sub-matchers using the same matcher type. Varying matcher types are
2133 // checked for above.
2134 AnyOfMatches(2, AnyOf(1, 2));
2135 AnyOfMatches(3, AnyOf(1, 2, 3));
2136 AnyOfMatches(4, AnyOf(1, 2, 3, 4));
2137 AnyOfMatches(5, AnyOf(1, 2, 3, 4, 5));
2138 AnyOfMatches(6, AnyOf(1, 2, 3, 4, 5, 6));
2139 AnyOfMatches(7, AnyOf(1, 2, 3, 4, 5, 6, 7));
2140 AnyOfMatches(8, AnyOf(1, 2, 3, 4, 5, 6, 7, 8));
2141 AnyOfMatches(9, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9));
2142 AnyOfMatches(10, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
shiqiane35fdd92008-12-10 05:08:54 +00002143}
2144
2145// Tests that AnyOf(m1, ..., mn) describes itself properly.
2146TEST(AnyOfTest, CanDescribeSelf) {
2147 Matcher<int> m;
2148 m = AnyOf(Le(1), Ge(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002149 EXPECT_EQ("(is <= 1) or (is >= 3)",
shiqiane35fdd92008-12-10 05:08:54 +00002150 Describe(m));
2151
2152 m = AnyOf(Lt(0), Eq(1), Eq(2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002153 EXPECT_EQ("(is < 0) or "
shiqiane35fdd92008-12-10 05:08:54 +00002154 "((is equal to 1) or (is equal to 2))",
2155 Describe(m));
2156
2157 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
jgm79a367e2012-04-10 16:02:11 +00002158 EXPECT_EQ("((is < 0) or "
2159 "(is equal to 1)) or "
shiqiane35fdd92008-12-10 05:08:54 +00002160 "((is equal to 2) or "
jgm79a367e2012-04-10 16:02:11 +00002161 "(is equal to 3))",
shiqiane35fdd92008-12-10 05:08:54 +00002162 Describe(m));
2163
2164 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
jgm79a367e2012-04-10 16:02:11 +00002165 EXPECT_EQ("((is <= 0) or "
2166 "(is > 10)) or "
shiqiane35fdd92008-12-10 05:08:54 +00002167 "((is equal to 3) or "
2168 "((is equal to 5) or "
jgm79a367e2012-04-10 16:02:11 +00002169 "(is equal to 7)))",
shiqiane35fdd92008-12-10 05:08:54 +00002170 Describe(m));
2171}
2172
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002173// Tests that AnyOf(m1, ..., mn) describes its negation properly.
2174TEST(AnyOfTest, CanDescribeNegation) {
2175 Matcher<int> m;
2176 m = AnyOf(Le(1), Ge(3));
2177 EXPECT_EQ("(isn't <= 1) and (isn't >= 3)",
2178 DescribeNegation(m));
2179
2180 m = AnyOf(Lt(0), Eq(1), Eq(2));
2181 EXPECT_EQ("(isn't < 0) and "
2182 "((isn't equal to 1) and (isn't equal to 2))",
2183 DescribeNegation(m));
2184
2185 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
jgm79a367e2012-04-10 16:02:11 +00002186 EXPECT_EQ("((isn't < 0) and "
2187 "(isn't equal to 1)) and "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002188 "((isn't equal to 2) and "
jgm79a367e2012-04-10 16:02:11 +00002189 "(isn't equal to 3))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002190 DescribeNegation(m));
2191
2192 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
jgm79a367e2012-04-10 16:02:11 +00002193 EXPECT_EQ("((isn't <= 0) and "
2194 "(isn't > 10)) and "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002195 "((isn't equal to 3) and "
2196 "((isn't equal to 5) and "
jgm79a367e2012-04-10 16:02:11 +00002197 "(isn't equal to 7)))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002198 DescribeNegation(m));
2199}
2200
zhanyong.wan18490652009-05-11 18:54:08 +00002201// Tests that monomorphic matchers are safely cast by the AnyOf matcher.
2202TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
2203 // greater_than_5 and less_than_10 are monomorphic matchers.
2204 Matcher<int> greater_than_5 = Gt(5);
2205 Matcher<int> less_than_10 = Lt(10);
2206
2207 Matcher<const int&> m = AnyOf(greater_than_5, less_than_10);
2208 Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10);
2209 Matcher<int&> m3 = AnyOf(greater_than_5, m2);
2210
2211 // Tests that EitherOf works when composing itself.
2212 Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10);
2213 Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);
2214}
2215
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002216TEST(AnyOfTest, ExplainsResult) {
2217 Matcher<int> m;
2218
2219 // Failed match. Both matchers need to explain. The second
2220 // matcher doesn't give an explanation, so only the first matcher's
2221 // explanation is printed.
2222 m = AnyOf(GreaterThan(10), Lt(0));
2223 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
2224
2225 // Failed match. Both matchers need to explain.
2226 m = AnyOf(GreaterThan(10), GreaterThan(20));
2227 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20",
2228 Explain(m, 5));
2229
2230 // Failed match. All matchers need to explain. The second
2231 // matcher doesn't given an explanation.
2232 m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30));
2233 EXPECT_EQ("which is 5 less than 10, and which is 25 less than 30",
2234 Explain(m, 5));
2235
2236 // Failed match. All matchers need to explain.
2237 m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2238 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20, "
2239 "and which is 25 less than 30",
2240 Explain(m, 5));
2241
2242 // Successful match. The first matcher, which succeeded, needs to
2243 // explain.
2244 m = AnyOf(GreaterThan(10), GreaterThan(20));
2245 EXPECT_EQ("which is 5 more than 10", Explain(m, 15));
2246
2247 // Successful match. The second matcher, which succeeded, needs to
2248 // explain. Since it doesn't given an explanation, nothing is
2249 // printed.
2250 m = AnyOf(GreaterThan(10), Lt(30));
2251 EXPECT_EQ("", Explain(m, 0));
2252
2253 // Successful match. The second matcher, which succeeded, needs to
2254 // explain.
2255 m = AnyOf(GreaterThan(30), GreaterThan(20));
2256 EXPECT_EQ("which is 5 more than 20", Explain(m, 25));
2257}
2258
shiqiane35fdd92008-12-10 05:08:54 +00002259// The following predicate function and predicate functor are for
2260// testing the Truly(predicate) matcher.
2261
2262// Returns non-zero if the input is positive. Note that the return
2263// type of this function is not bool. It's OK as Truly() accepts any
2264// unary function or functor whose return type can be implicitly
2265// converted to bool.
2266int IsPositive(double x) {
2267 return x > 0 ? 1 : 0;
2268}
2269
2270// This functor returns true if the input is greater than the given
2271// number.
2272class IsGreaterThan {
2273 public:
2274 explicit IsGreaterThan(int threshold) : threshold_(threshold) {}
2275
2276 bool operator()(int n) const { return n > threshold_; }
zhanyong.wan32de5f52009-12-23 00:13:23 +00002277
shiqiane35fdd92008-12-10 05:08:54 +00002278 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002279 int threshold_;
shiqiane35fdd92008-12-10 05:08:54 +00002280};
2281
2282// For testing Truly().
2283const int foo = 0;
2284
2285// This predicate returns true iff the argument references foo and has
2286// a zero value.
2287bool ReferencesFooAndIsZero(const int& n) {
2288 return (&n == &foo) && (n == 0);
2289}
2290
2291// Tests that Truly(predicate) matches what satisfies the given
2292// predicate.
2293TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {
2294 Matcher<double> m = Truly(IsPositive);
2295 EXPECT_TRUE(m.Matches(2.0));
2296 EXPECT_FALSE(m.Matches(-1.5));
2297}
2298
2299// Tests that Truly(predicate_functor) works too.
2300TEST(TrulyTest, CanBeUsedWithFunctor) {
2301 Matcher<int> m = Truly(IsGreaterThan(5));
2302 EXPECT_TRUE(m.Matches(6));
2303 EXPECT_FALSE(m.Matches(4));
2304}
2305
zhanyong.wan8d3dc0c2011-04-14 19:37:06 +00002306// A class that can be implicitly converted to bool.
2307class ConvertibleToBool {
2308 public:
2309 explicit ConvertibleToBool(int number) : number_(number) {}
2310 operator bool() const { return number_ != 0; }
2311
2312 private:
2313 int number_;
2314};
2315
2316ConvertibleToBool IsNotZero(int number) {
2317 return ConvertibleToBool(number);
2318}
2319
2320// Tests that the predicate used in Truly() may return a class that's
2321// implicitly convertible to bool, even when the class has no
2322// operator!().
2323TEST(TrulyTest, PredicateCanReturnAClassConvertibleToBool) {
2324 Matcher<int> m = Truly(IsNotZero);
2325 EXPECT_TRUE(m.Matches(1));
2326 EXPECT_FALSE(m.Matches(0));
2327}
2328
shiqiane35fdd92008-12-10 05:08:54 +00002329// Tests that Truly(predicate) can describe itself properly.
2330TEST(TrulyTest, CanDescribeSelf) {
2331 Matcher<double> m = Truly(IsPositive);
2332 EXPECT_EQ("satisfies the given predicate",
2333 Describe(m));
2334}
2335
2336// Tests that Truly(predicate) works when the matcher takes its
2337// argument by reference.
2338TEST(TrulyTest, WorksForByRefArguments) {
2339 Matcher<const int&> m = Truly(ReferencesFooAndIsZero);
2340 EXPECT_TRUE(m.Matches(foo));
2341 int n = 0;
2342 EXPECT_FALSE(m.Matches(n));
2343}
2344
2345// Tests that Matches(m) is a predicate satisfied by whatever that
2346// matches matcher m.
2347TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {
2348 EXPECT_TRUE(Matches(Ge(0))(1));
2349 EXPECT_FALSE(Matches(Eq('a'))('b'));
2350}
2351
2352// Tests that Matches(m) works when the matcher takes its argument by
2353// reference.
2354TEST(MatchesTest, WorksOnByRefArguments) {
2355 int m = 0, n = 0;
2356 EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n));
2357 EXPECT_FALSE(Matches(Ref(m))(n));
2358}
2359
2360// Tests that a Matcher on non-reference type can be used in
2361// Matches().
2362TEST(MatchesTest, WorksWithMatcherOnNonRefType) {
2363 Matcher<int> eq5 = Eq(5);
2364 EXPECT_TRUE(Matches(eq5)(5));
2365 EXPECT_FALSE(Matches(eq5)(2));
2366}
2367
zhanyong.wanb8243162009-06-04 05:48:20 +00002368// Tests Value(value, matcher). Since Value() is a simple wrapper for
2369// Matches(), which has been tested already, we don't spend a lot of
2370// effort on testing Value().
2371TEST(ValueTest, WorksWithPolymorphicMatcher) {
2372 EXPECT_TRUE(Value("hi", StartsWith("h")));
2373 EXPECT_FALSE(Value(5, Gt(10)));
2374}
2375
2376TEST(ValueTest, WorksWithMonomorphicMatcher) {
2377 const Matcher<int> is_zero = Eq(0);
2378 EXPECT_TRUE(Value(0, is_zero));
2379 EXPECT_FALSE(Value('a', is_zero));
2380
2381 int n = 0;
2382 const Matcher<const int&> ref_n = Ref(n);
2383 EXPECT_TRUE(Value(n, ref_n));
2384 EXPECT_FALSE(Value(1, ref_n));
2385}
2386
zhanyong.wana862f1d2010-03-15 21:23:04 +00002387TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) {
zhanyong.wan34b034c2010-03-05 21:23:23 +00002388 StringMatchResultListener listener1;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002389 EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002390 EXPECT_EQ("% 2 == 0", listener1.str());
2391
2392 StringMatchResultListener listener2;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002393 EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002394 EXPECT_EQ("", listener2.str());
2395}
2396
zhanyong.wana862f1d2010-03-15 21:23:04 +00002397TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) {
zhanyong.wan34b034c2010-03-05 21:23:23 +00002398 const Matcher<int> is_even = PolymorphicIsEven();
2399 StringMatchResultListener listener1;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002400 EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002401 EXPECT_EQ("% 2 == 0", listener1.str());
2402
2403 const Matcher<const double&> is_zero = Eq(0);
2404 StringMatchResultListener listener2;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002405 EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002406 EXPECT_EQ("", listener2.str());
2407}
2408
zhanyong.wana862f1d2010-03-15 21:23:04 +00002409MATCHER_P(Really, inner_matcher, "") {
2410 return ExplainMatchResult(inner_matcher, arg, result_listener);
2411}
2412
2413TEST(ExplainMatchResultTest, WorksInsideMATCHER) {
2414 EXPECT_THAT(0, Really(Eq(0)));
2415}
2416
zhanyong.wanbf550852009-06-09 06:09:53 +00002417TEST(AllArgsTest, WorksForTuple) {
2418 EXPECT_THAT(make_tuple(1, 2L), AllArgs(Lt()));
2419 EXPECT_THAT(make_tuple(2L, 1), Not(AllArgs(Lt())));
2420}
2421
2422TEST(AllArgsTest, WorksForNonTuple) {
2423 EXPECT_THAT(42, AllArgs(Gt(0)));
2424 EXPECT_THAT('a', Not(AllArgs(Eq('b'))));
2425}
2426
2427class AllArgsHelper {
2428 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002429 AllArgsHelper() {}
2430
zhanyong.wanbf550852009-06-09 06:09:53 +00002431 MOCK_METHOD2(Helper, int(char x, int y));
zhanyong.wan32de5f52009-12-23 00:13:23 +00002432
2433 private:
2434 GTEST_DISALLOW_COPY_AND_ASSIGN_(AllArgsHelper);
zhanyong.wanbf550852009-06-09 06:09:53 +00002435};
2436
2437TEST(AllArgsTest, WorksInWithClause) {
2438 AllArgsHelper helper;
2439 ON_CALL(helper, Helper(_, _))
2440 .With(AllArgs(Lt()))
2441 .WillByDefault(Return(1));
2442 EXPECT_CALL(helper, Helper(_, _));
2443 EXPECT_CALL(helper, Helper(_, _))
2444 .With(AllArgs(Gt()))
2445 .WillOnce(Return(2));
2446
2447 EXPECT_EQ(1, helper.Helper('\1', 2));
2448 EXPECT_EQ(2, helper.Helper('a', 1));
2449}
2450
shiqiane35fdd92008-12-10 05:08:54 +00002451// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2452// matches the matcher.
2453TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {
2454 ASSERT_THAT(5, Ge(2)) << "This should succeed.";
2455 ASSERT_THAT("Foo", EndsWith("oo"));
2456 EXPECT_THAT(2, AllOf(Le(7), Ge(0))) << "This should succeed too.";
2457 EXPECT_THAT("Hello", StartsWith("Hell"));
2458}
2459
2460// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2461// doesn't match the matcher.
2462TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {
2463 // 'n' must be static as it is used in an EXPECT_FATAL_FAILURE(),
2464 // which cannot reference auto variables.
zhanyong.wan736baa82010-09-27 17:44:16 +00002465 static unsigned short n; // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00002466 n = 5;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002467
2468 // VC++ prior to version 8.0 SP1 has a bug where it will not see any
2469 // functions declared in the namespace scope from within nested classes.
2470 // EXPECT/ASSERT_(NON)FATAL_FAILURE macros use nested classes so that all
2471 // namespace-level functions invoked inside them need to be explicitly
2472 // resolved.
2473 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Gt(10)),
shiqiane35fdd92008-12-10 05:08:54 +00002474 "Value of: n\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002475 "Expected: is > 10\n"
zhanyong.wan736baa82010-09-27 17:44:16 +00002476 " Actual: 5" + OfType("unsigned short"));
shiqiane35fdd92008-12-10 05:08:54 +00002477 n = 0;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002478 EXPECT_NONFATAL_FAILURE(
2479 EXPECT_THAT(n, ::testing::AllOf(::testing::Le(7), ::testing::Ge(5))),
2480 "Value of: n\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002481 "Expected: (is <= 7) and (is >= 5)\n"
zhanyong.wan736baa82010-09-27 17:44:16 +00002482 " Actual: 0" + OfType("unsigned short"));
shiqiane35fdd92008-12-10 05:08:54 +00002483}
2484
2485// Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument
2486// has a reference type.
2487TEST(MatcherAssertionTest, WorksForByRefArguments) {
2488 // We use a static variable here as EXPECT_FATAL_FAILURE() cannot
2489 // reference auto variables.
2490 static int n;
2491 n = 0;
2492 EXPECT_THAT(n, AllOf(Le(7), Ref(n)));
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002493 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
shiqiane35fdd92008-12-10 05:08:54 +00002494 "Value of: n\n"
2495 "Expected: does not reference the variable @");
2496 // Tests the "Actual" part.
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002497 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
zhanyong.wan736baa82010-09-27 17:44:16 +00002498 "Actual: 0" + OfType("int") + ", which is located @");
shiqiane35fdd92008-12-10 05:08:54 +00002499}
2500
zhanyong.wan95b12332009-09-25 18:55:50 +00002501#if !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002502// Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is
2503// monomorphic.
zhanyong.wan95b12332009-09-25 18:55:50 +00002504
2505// ASSERT_THAT("hello", starts_with_he) fails to compile with Nokia's
2506// Symbian compiler: it tries to compile
2507// template<T, U> class MatcherCastImpl { ...
zhanyong.wandb22c222010-01-28 21:52:29 +00002508// virtual bool MatchAndExplain(T x, ...) const {
2509// return source_matcher_.MatchAndExplain(static_cast<U>(x), ...);
zhanyong.wan95b12332009-09-25 18:55:50 +00002510// with U == string and T == const char*
2511// With ASSERT_THAT("hello"...) changed to ASSERT_THAT(string("hello") ... )
2512// the compiler silently crashes with no output.
2513// If MatcherCastImpl is changed to use U(x) instead of static_cast<U>(x)
2514// the code compiles but the converted string is bogus.
shiqiane35fdd92008-12-10 05:08:54 +00002515TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
2516 Matcher<const char*> starts_with_he = StartsWith("he");
2517 ASSERT_THAT("hello", starts_with_he);
2518
2519 Matcher<const string&> ends_with_ok = EndsWith("ok");
2520 ASSERT_THAT("book", ends_with_ok);
zhanyong.wan736baa82010-09-27 17:44:16 +00002521 const string bad = "bad";
2522 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(bad, ends_with_ok),
2523 "Value of: bad\n"
2524 "Expected: ends with \"ok\"\n"
2525 " Actual: \"bad\"");
shiqiane35fdd92008-12-10 05:08:54 +00002526 Matcher<int> is_greater_than_5 = Gt(5);
2527 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5),
2528 "Value of: 5\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002529 "Expected: is > 5\n"
zhanyong.wan736baa82010-09-27 17:44:16 +00002530 " Actual: 5" + OfType("int"));
shiqiane35fdd92008-12-10 05:08:54 +00002531}
zhanyong.wan95b12332009-09-25 18:55:50 +00002532#endif // !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002533
2534// Tests floating-point matchers.
2535template <typename RawType>
2536class FloatingPointTest : public testing::Test {
2537 protected:
2538 typedef typename testing::internal::FloatingPoint<RawType> Floating;
2539 typedef typename Floating::Bits Bits;
2540
2541 virtual void SetUp() {
2542 const size_t max_ulps = Floating::kMaxUlps;
2543
2544 // The bits that represent 0.0.
2545 const Bits zero_bits = Floating(0).bits();
2546
2547 // Makes some numbers close to 0.0.
2548 close_to_positive_zero_ = Floating::ReinterpretBits(zero_bits + max_ulps/2);
2549 close_to_negative_zero_ = -Floating::ReinterpretBits(
2550 zero_bits + max_ulps - max_ulps/2);
2551 further_from_negative_zero_ = -Floating::ReinterpretBits(
2552 zero_bits + max_ulps + 1 - max_ulps/2);
2553
2554 // The bits that represent 1.0.
2555 const Bits one_bits = Floating(1).bits();
2556
2557 // Makes some numbers close to 1.0.
2558 close_to_one_ = Floating::ReinterpretBits(one_bits + max_ulps);
2559 further_from_one_ = Floating::ReinterpretBits(one_bits + max_ulps + 1);
2560
2561 // +infinity.
2562 infinity_ = Floating::Infinity();
2563
2564 // The bits that represent +infinity.
2565 const Bits infinity_bits = Floating(infinity_).bits();
2566
2567 // Makes some numbers close to infinity.
2568 close_to_infinity_ = Floating::ReinterpretBits(infinity_bits - max_ulps);
2569 further_from_infinity_ = Floating::ReinterpretBits(
2570 infinity_bits - max_ulps - 1);
2571
2572 // Makes some NAN's.
2573 nan1_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 1);
2574 nan2_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 200);
2575 }
2576
2577 void TestSize() {
2578 EXPECT_EQ(sizeof(RawType), sizeof(Bits));
2579 }
2580
2581 // A battery of tests for FloatingEqMatcher::Matches.
2582 // matcher_maker is a pointer to a function which creates a FloatingEqMatcher.
2583 void TestMatches(
2584 testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) {
2585 Matcher<RawType> m1 = matcher_maker(0.0);
2586 EXPECT_TRUE(m1.Matches(-0.0));
2587 EXPECT_TRUE(m1.Matches(close_to_positive_zero_));
2588 EXPECT_TRUE(m1.Matches(close_to_negative_zero_));
2589 EXPECT_FALSE(m1.Matches(1.0));
2590
2591 Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_);
2592 EXPECT_FALSE(m2.Matches(further_from_negative_zero_));
2593
2594 Matcher<RawType> m3 = matcher_maker(1.0);
2595 EXPECT_TRUE(m3.Matches(close_to_one_));
2596 EXPECT_FALSE(m3.Matches(further_from_one_));
2597
2598 // Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above.
2599 EXPECT_FALSE(m3.Matches(0.0));
2600
2601 Matcher<RawType> m4 = matcher_maker(-infinity_);
2602 EXPECT_TRUE(m4.Matches(-close_to_infinity_));
2603
2604 Matcher<RawType> m5 = matcher_maker(infinity_);
2605 EXPECT_TRUE(m5.Matches(close_to_infinity_));
2606
2607 // This is interesting as the representations of infinity_ and nan1_
2608 // are only 1 DLP apart.
2609 EXPECT_FALSE(m5.Matches(nan1_));
2610
2611 // matcher_maker can produce a Matcher<const RawType&>, which is needed in
2612 // some cases.
2613 Matcher<const RawType&> m6 = matcher_maker(0.0);
2614 EXPECT_TRUE(m6.Matches(-0.0));
2615 EXPECT_TRUE(m6.Matches(close_to_positive_zero_));
2616 EXPECT_FALSE(m6.Matches(1.0));
2617
2618 // matcher_maker can produce a Matcher<RawType&>, which is needed in some
2619 // cases.
2620 Matcher<RawType&> m7 = matcher_maker(0.0);
2621 RawType x = 0.0;
2622 EXPECT_TRUE(m7.Matches(x));
2623 x = 0.01f;
2624 EXPECT_FALSE(m7.Matches(x));
2625 }
2626
2627 // Pre-calculated numbers to be used by the tests.
2628
2629 static RawType close_to_positive_zero_;
2630 static RawType close_to_negative_zero_;
2631 static RawType further_from_negative_zero_;
2632
2633 static RawType close_to_one_;
2634 static RawType further_from_one_;
2635
2636 static RawType infinity_;
2637 static RawType close_to_infinity_;
2638 static RawType further_from_infinity_;
2639
2640 static RawType nan1_;
2641 static RawType nan2_;
2642};
2643
2644template <typename RawType>
2645RawType FloatingPointTest<RawType>::close_to_positive_zero_;
2646
2647template <typename RawType>
2648RawType FloatingPointTest<RawType>::close_to_negative_zero_;
2649
2650template <typename RawType>
2651RawType FloatingPointTest<RawType>::further_from_negative_zero_;
2652
2653template <typename RawType>
2654RawType FloatingPointTest<RawType>::close_to_one_;
2655
2656template <typename RawType>
2657RawType FloatingPointTest<RawType>::further_from_one_;
2658
2659template <typename RawType>
2660RawType FloatingPointTest<RawType>::infinity_;
2661
2662template <typename RawType>
2663RawType FloatingPointTest<RawType>::close_to_infinity_;
2664
2665template <typename RawType>
2666RawType FloatingPointTest<RawType>::further_from_infinity_;
2667
2668template <typename RawType>
2669RawType FloatingPointTest<RawType>::nan1_;
2670
2671template <typename RawType>
2672RawType FloatingPointTest<RawType>::nan2_;
2673
2674// Instantiate FloatingPointTest for testing floats.
2675typedef FloatingPointTest<float> FloatTest;
2676
2677TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) {
2678 TestMatches(&FloatEq);
2679}
2680
2681TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
2682 TestMatches(&NanSensitiveFloatEq);
2683}
2684
2685TEST_F(FloatTest, FloatEqCannotMatchNaN) {
2686 // FloatEq never matches NaN.
2687 Matcher<float> m = FloatEq(nan1_);
2688 EXPECT_FALSE(m.Matches(nan1_));
2689 EXPECT_FALSE(m.Matches(nan2_));
2690 EXPECT_FALSE(m.Matches(1.0));
2691}
2692
2693TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {
2694 // NanSensitiveFloatEq will match NaN.
2695 Matcher<float> m = NanSensitiveFloatEq(nan1_);
2696 EXPECT_TRUE(m.Matches(nan1_));
2697 EXPECT_TRUE(m.Matches(nan2_));
2698 EXPECT_FALSE(m.Matches(1.0));
2699}
2700
2701TEST_F(FloatTest, FloatEqCanDescribeSelf) {
2702 Matcher<float> m1 = FloatEq(2.0f);
2703 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002704 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002705
2706 Matcher<float> m2 = FloatEq(0.5f);
2707 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002708 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002709
2710 Matcher<float> m3 = FloatEq(nan1_);
2711 EXPECT_EQ("never matches", Describe(m3));
2712 EXPECT_EQ("is anything", DescribeNegation(m3));
2713}
2714
2715TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {
2716 Matcher<float> m1 = NanSensitiveFloatEq(2.0f);
2717 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002718 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002719
2720 Matcher<float> m2 = NanSensitiveFloatEq(0.5f);
2721 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002722 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002723
2724 Matcher<float> m3 = NanSensitiveFloatEq(nan1_);
2725 EXPECT_EQ("is NaN", Describe(m3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002726 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
shiqiane35fdd92008-12-10 05:08:54 +00002727}
2728
2729// Instantiate FloatingPointTest for testing doubles.
2730typedef FloatingPointTest<double> DoubleTest;
2731
2732TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
2733 TestMatches(&DoubleEq);
2734}
2735
2736TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
2737 TestMatches(&NanSensitiveDoubleEq);
2738}
2739
2740TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {
2741 // DoubleEq never matches NaN.
2742 Matcher<double> m = DoubleEq(nan1_);
2743 EXPECT_FALSE(m.Matches(nan1_));
2744 EXPECT_FALSE(m.Matches(nan2_));
2745 EXPECT_FALSE(m.Matches(1.0));
2746}
2747
2748TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {
2749 // NanSensitiveDoubleEq will match NaN.
2750 Matcher<double> m = NanSensitiveDoubleEq(nan1_);
2751 EXPECT_TRUE(m.Matches(nan1_));
2752 EXPECT_TRUE(m.Matches(nan2_));
2753 EXPECT_FALSE(m.Matches(1.0));
2754}
2755
2756TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {
2757 Matcher<double> m1 = DoubleEq(2.0);
2758 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002759 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002760
2761 Matcher<double> m2 = DoubleEq(0.5);
2762 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002763 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002764
2765 Matcher<double> m3 = DoubleEq(nan1_);
2766 EXPECT_EQ("never matches", Describe(m3));
2767 EXPECT_EQ("is anything", DescribeNegation(m3));
2768}
2769
2770TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {
2771 Matcher<double> m1 = NanSensitiveDoubleEq(2.0);
2772 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002773 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002774
2775 Matcher<double> m2 = NanSensitiveDoubleEq(0.5);
2776 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002777 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002778
2779 Matcher<double> m3 = NanSensitiveDoubleEq(nan1_);
2780 EXPECT_EQ("is NaN", Describe(m3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002781 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
shiqiane35fdd92008-12-10 05:08:54 +00002782}
2783
2784TEST(PointeeTest, RawPointer) {
2785 const Matcher<int*> m = Pointee(Ge(0));
2786
2787 int n = 1;
2788 EXPECT_TRUE(m.Matches(&n));
2789 n = -1;
2790 EXPECT_FALSE(m.Matches(&n));
2791 EXPECT_FALSE(m.Matches(NULL));
2792}
2793
2794TEST(PointeeTest, RawPointerToConst) {
2795 const Matcher<const double*> m = Pointee(Ge(0));
2796
2797 double x = 1;
2798 EXPECT_TRUE(m.Matches(&x));
2799 x = -1;
2800 EXPECT_FALSE(m.Matches(&x));
2801 EXPECT_FALSE(m.Matches(NULL));
2802}
2803
2804TEST(PointeeTest, ReferenceToConstRawPointer) {
2805 const Matcher<int* const &> m = Pointee(Ge(0));
2806
2807 int n = 1;
2808 EXPECT_TRUE(m.Matches(&n));
2809 n = -1;
2810 EXPECT_FALSE(m.Matches(&n));
2811 EXPECT_FALSE(m.Matches(NULL));
2812}
2813
2814TEST(PointeeTest, ReferenceToNonConstRawPointer) {
2815 const Matcher<double* &> m = Pointee(Ge(0));
2816
2817 double x = 1.0;
2818 double* p = &x;
2819 EXPECT_TRUE(m.Matches(p));
2820 x = -1;
2821 EXPECT_FALSE(m.Matches(p));
2822 p = NULL;
2823 EXPECT_FALSE(m.Matches(p));
2824}
2825
vladlosevada23472012-08-14 15:38:49 +00002826// Minimal const-propagating pointer.
2827template <typename T>
2828class ConstPropagatingPtr {
2829 public:
2830 typedef T element_type;
2831
2832 ConstPropagatingPtr() : val_() {}
2833 explicit ConstPropagatingPtr(T* t) : val_(t) {}
2834 ConstPropagatingPtr(const ConstPropagatingPtr& other) : val_(other.val_) {}
2835
2836 T* get() { return val_; }
2837 T& operator*() { return *val_; }
2838 // Most smart pointers return non-const T* and T& from the next methods.
2839 const T* get() const { return val_; }
2840 const T& operator*() const { return *val_; }
2841
2842 private:
2843 T* val_;
2844};
2845
2846TEST(PointeeTest, WorksWithConstPropagatingPointers) {
2847 const Matcher< ConstPropagatingPtr<int> > m = Pointee(Lt(5));
2848 int three = 3;
2849 const ConstPropagatingPtr<int> co(&three);
2850 ConstPropagatingPtr<int> o(&three);
2851 EXPECT_TRUE(m.Matches(o));
2852 EXPECT_TRUE(m.Matches(co));
2853 *o = 6;
2854 EXPECT_FALSE(m.Matches(o));
2855 EXPECT_FALSE(m.Matches(ConstPropagatingPtr<int>()));
2856}
2857
shiqiane35fdd92008-12-10 05:08:54 +00002858TEST(PointeeTest, NeverMatchesNull) {
2859 const Matcher<const char*> m = Pointee(_);
2860 EXPECT_FALSE(m.Matches(NULL));
2861}
2862
2863// Tests that we can write Pointee(value) instead of Pointee(Eq(value)).
2864TEST(PointeeTest, MatchesAgainstAValue) {
2865 const Matcher<int*> m = Pointee(5);
2866
2867 int n = 5;
2868 EXPECT_TRUE(m.Matches(&n));
2869 n = -1;
2870 EXPECT_FALSE(m.Matches(&n));
2871 EXPECT_FALSE(m.Matches(NULL));
2872}
2873
2874TEST(PointeeTest, CanDescribeSelf) {
2875 const Matcher<int*> m = Pointee(Gt(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002876 EXPECT_EQ("points to a value that is > 3", Describe(m));
2877 EXPECT_EQ("does not point to a value that is > 3",
shiqiane35fdd92008-12-10 05:08:54 +00002878 DescribeNegation(m));
2879}
2880
shiqiane35fdd92008-12-10 05:08:54 +00002881TEST(PointeeTest, CanExplainMatchResult) {
2882 const Matcher<const string*> m = Pointee(StartsWith("Hi"));
2883
2884 EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL)));
2885
zhanyong.wan736baa82010-09-27 17:44:16 +00002886 const Matcher<long*> m2 = Pointee(GreaterThan(1)); // NOLINT
2887 long n = 3; // NOLINT
2888 EXPECT_EQ("which points to 3" + OfType("long") + ", which is 2 more than 1",
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002889 Explain(m2, &n));
2890}
2891
2892TEST(PointeeTest, AlwaysExplainsPointee) {
2893 const Matcher<int*> m = Pointee(0);
2894 int n = 42;
zhanyong.wan736baa82010-09-27 17:44:16 +00002895 EXPECT_EQ("which points to 42" + OfType("int"), Explain(m, &n));
shiqiane35fdd92008-12-10 05:08:54 +00002896}
2897
2898// An uncopyable class.
2899class Uncopyable {
2900 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002901 explicit Uncopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00002902
2903 int value() const { return value_; }
2904 private:
2905 const int value_;
2906 GTEST_DISALLOW_COPY_AND_ASSIGN_(Uncopyable);
2907};
2908
2909// Returns true iff x.value() is positive.
2910bool ValueIsPositive(const Uncopyable& x) { return x.value() > 0; }
2911
2912// A user-defined struct for testing Field().
2913struct AStruct {
2914 AStruct() : x(0), y(1.0), z(5), p(NULL) {}
2915 AStruct(const AStruct& rhs)
2916 : x(rhs.x), y(rhs.y), z(rhs.z.value()), p(rhs.p) {}
2917
2918 int x; // A non-const field.
2919 const double y; // A const field.
2920 Uncopyable z; // An uncopyable field.
2921 const char* p; // A pointer field.
zhanyong.wan32de5f52009-12-23 00:13:23 +00002922
2923 private:
2924 GTEST_DISALLOW_ASSIGN_(AStruct);
shiqiane35fdd92008-12-10 05:08:54 +00002925};
2926
2927// A derived struct for testing Field().
2928struct DerivedStruct : public AStruct {
2929 char ch;
zhanyong.wan32de5f52009-12-23 00:13:23 +00002930
2931 private:
2932 GTEST_DISALLOW_ASSIGN_(DerivedStruct);
shiqiane35fdd92008-12-10 05:08:54 +00002933};
2934
2935// Tests that Field(&Foo::field, ...) works when field is non-const.
2936TEST(FieldTest, WorksForNonConstField) {
2937 Matcher<AStruct> m = Field(&AStruct::x, Ge(0));
2938
2939 AStruct a;
2940 EXPECT_TRUE(m.Matches(a));
2941 a.x = -1;
2942 EXPECT_FALSE(m.Matches(a));
2943}
2944
2945// Tests that Field(&Foo::field, ...) works when field is const.
2946TEST(FieldTest, WorksForConstField) {
2947 AStruct a;
2948
2949 Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0));
2950 EXPECT_TRUE(m.Matches(a));
2951 m = Field(&AStruct::y, Le(0.0));
2952 EXPECT_FALSE(m.Matches(a));
2953}
2954
2955// Tests that Field(&Foo::field, ...) works when field is not copyable.
2956TEST(FieldTest, WorksForUncopyableField) {
2957 AStruct a;
2958
2959 Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive));
2960 EXPECT_TRUE(m.Matches(a));
2961 m = Field(&AStruct::z, Not(Truly(ValueIsPositive)));
2962 EXPECT_FALSE(m.Matches(a));
2963}
2964
2965// Tests that Field(&Foo::field, ...) works when field is a pointer.
2966TEST(FieldTest, WorksForPointerField) {
2967 // Matching against NULL.
2968 Matcher<AStruct> m = Field(&AStruct::p, static_cast<const char*>(NULL));
2969 AStruct a;
2970 EXPECT_TRUE(m.Matches(a));
2971 a.p = "hi";
2972 EXPECT_FALSE(m.Matches(a));
2973
2974 // Matching a pointer that is not NULL.
2975 m = Field(&AStruct::p, StartsWith("hi"));
2976 a.p = "hill";
2977 EXPECT_TRUE(m.Matches(a));
2978 a.p = "hole";
2979 EXPECT_FALSE(m.Matches(a));
2980}
2981
2982// Tests that Field() works when the object is passed by reference.
2983TEST(FieldTest, WorksForByRefArgument) {
2984 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2985
2986 AStruct a;
2987 EXPECT_TRUE(m.Matches(a));
2988 a.x = -1;
2989 EXPECT_FALSE(m.Matches(a));
2990}
2991
2992// Tests that Field(&Foo::field, ...) works when the argument's type
2993// is a sub-type of Foo.
2994TEST(FieldTest, WorksForArgumentOfSubType) {
2995 // Note that the matcher expects DerivedStruct but we say AStruct
2996 // inside Field().
2997 Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0));
2998
2999 DerivedStruct d;
3000 EXPECT_TRUE(m.Matches(d));
3001 d.x = -1;
3002 EXPECT_FALSE(m.Matches(d));
3003}
3004
3005// Tests that Field(&Foo::field, m) works when field's type and m's
3006// argument type are compatible but not the same.
3007TEST(FieldTest, WorksForCompatibleMatcherType) {
3008 // The field is an int, but the inner matcher expects a signed char.
3009 Matcher<const AStruct&> m = Field(&AStruct::x,
3010 Matcher<signed char>(Ge(0)));
3011
3012 AStruct a;
3013 EXPECT_TRUE(m.Matches(a));
3014 a.x = -1;
3015 EXPECT_FALSE(m.Matches(a));
3016}
3017
3018// Tests that Field() can describe itself.
3019TEST(FieldTest, CanDescribeSelf) {
3020 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
3021
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003022 EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
3023 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003024}
3025
3026// Tests that Field() can explain the match result.
3027TEST(FieldTest, CanExplainMatchResult) {
3028 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
3029
3030 AStruct a;
3031 a.x = 1;
zhanyong.wan736baa82010-09-27 17:44:16 +00003032 EXPECT_EQ("whose given field is 1" + OfType("int"), Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003033
3034 m = Field(&AStruct::x, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003035 EXPECT_EQ(
3036 "whose given field is 1" + OfType("int") + ", which is 1 more than 0",
3037 Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003038}
3039
3040// Tests that Field() works when the argument is a pointer to const.
3041TEST(FieldForPointerTest, WorksForPointerToConst) {
3042 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
3043
3044 AStruct a;
3045 EXPECT_TRUE(m.Matches(&a));
3046 a.x = -1;
3047 EXPECT_FALSE(m.Matches(&a));
3048}
3049
3050// Tests that Field() works when the argument is a pointer to non-const.
3051TEST(FieldForPointerTest, WorksForPointerToNonConst) {
3052 Matcher<AStruct*> m = Field(&AStruct::x, Ge(0));
3053
3054 AStruct a;
3055 EXPECT_TRUE(m.Matches(&a));
3056 a.x = -1;
3057 EXPECT_FALSE(m.Matches(&a));
3058}
3059
zhanyong.wan6953a722010-01-13 05:15:07 +00003060// Tests that Field() works when the argument is a reference to a const pointer.
3061TEST(FieldForPointerTest, WorksForReferenceToConstPointer) {
3062 Matcher<AStruct* const&> m = Field(&AStruct::x, Ge(0));
3063
3064 AStruct a;
3065 EXPECT_TRUE(m.Matches(&a));
3066 a.x = -1;
3067 EXPECT_FALSE(m.Matches(&a));
3068}
3069
shiqiane35fdd92008-12-10 05:08:54 +00003070// Tests that Field() does not match the NULL pointer.
3071TEST(FieldForPointerTest, DoesNotMatchNull) {
3072 Matcher<const AStruct*> m = Field(&AStruct::x, _);
3073 EXPECT_FALSE(m.Matches(NULL));
3074}
3075
3076// Tests that Field(&Foo::field, ...) works when the argument's type
3077// is a sub-type of const Foo*.
3078TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
3079 // Note that the matcher expects DerivedStruct but we say AStruct
3080 // inside Field().
3081 Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0));
3082
3083 DerivedStruct d;
3084 EXPECT_TRUE(m.Matches(&d));
3085 d.x = -1;
3086 EXPECT_FALSE(m.Matches(&d));
3087}
3088
3089// Tests that Field() can describe itself when used to match a pointer.
3090TEST(FieldForPointerTest, CanDescribeSelf) {
3091 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
3092
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003093 EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
3094 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003095}
3096
3097// Tests that Field() can explain the result of matching a pointer.
3098TEST(FieldForPointerTest, CanExplainMatchResult) {
3099 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
3100
3101 AStruct a;
3102 a.x = 1;
3103 EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(NULL)));
zhanyong.wan736baa82010-09-27 17:44:16 +00003104 EXPECT_EQ("which points to an object whose given field is 1" + OfType("int"),
3105 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003106
3107 m = Field(&AStruct::x, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003108 EXPECT_EQ("which points to an object whose given field is 1" + OfType("int") +
3109 ", which is 1 more than 0", Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003110}
3111
3112// A user-defined class for testing Property().
3113class AClass {
3114 public:
3115 AClass() : n_(0) {}
3116
3117 // A getter that returns a non-reference.
3118 int n() const { return n_; }
3119
3120 void set_n(int new_n) { n_ = new_n; }
3121
3122 // A getter that returns a reference to const.
3123 const string& s() const { return s_; }
3124
3125 void set_s(const string& new_s) { s_ = new_s; }
3126
3127 // A getter that returns a reference to non-const.
3128 double& x() const { return x_; }
3129 private:
3130 int n_;
3131 string s_;
3132
3133 static double x_;
3134};
3135
3136double AClass::x_ = 0.0;
3137
3138// A derived class for testing Property().
3139class DerivedClass : public AClass {
3140 private:
3141 int k_;
3142};
3143
3144// Tests that Property(&Foo::property, ...) works when property()
3145// returns a non-reference.
3146TEST(PropertyTest, WorksForNonReferenceProperty) {
3147 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3148
3149 AClass a;
3150 a.set_n(1);
3151 EXPECT_TRUE(m.Matches(a));
3152
3153 a.set_n(-1);
3154 EXPECT_FALSE(m.Matches(a));
3155}
3156
3157// Tests that Property(&Foo::property, ...) works when property()
3158// returns a reference to const.
3159TEST(PropertyTest, WorksForReferenceToConstProperty) {
3160 Matcher<const AClass&> m = Property(&AClass::s, StartsWith("hi"));
3161
3162 AClass a;
3163 a.set_s("hill");
3164 EXPECT_TRUE(m.Matches(a));
3165
3166 a.set_s("hole");
3167 EXPECT_FALSE(m.Matches(a));
3168}
3169
3170// Tests that Property(&Foo::property, ...) works when property()
3171// returns a reference to non-const.
3172TEST(PropertyTest, WorksForReferenceToNonConstProperty) {
3173 double x = 0.0;
3174 AClass a;
3175
3176 Matcher<const AClass&> m = Property(&AClass::x, Ref(x));
3177 EXPECT_FALSE(m.Matches(a));
3178
3179 m = Property(&AClass::x, Not(Ref(x)));
3180 EXPECT_TRUE(m.Matches(a));
3181}
3182
3183// Tests that Property(&Foo::property, ...) works when the argument is
3184// passed by value.
3185TEST(PropertyTest, WorksForByValueArgument) {
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
3196// Tests that Property(&Foo::property, ...) works when the argument's
3197// type is a sub-type of Foo.
3198TEST(PropertyTest, WorksForArgumentOfSubType) {
3199 // The matcher expects a DerivedClass, but inside the Property() we
3200 // say AClass.
3201 Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0));
3202
3203 DerivedClass d;
3204 d.set_n(1);
3205 EXPECT_TRUE(m.Matches(d));
3206
3207 d.set_n(-1);
3208 EXPECT_FALSE(m.Matches(d));
3209}
3210
3211// Tests that Property(&Foo::property, m) works when property()'s type
3212// and m's argument type are compatible but different.
3213TEST(PropertyTest, WorksForCompatibleMatcherType) {
3214 // n() returns an int but the inner matcher expects a signed char.
3215 Matcher<const AClass&> m = Property(&AClass::n,
3216 Matcher<signed char>(Ge(0)));
3217
3218 AClass a;
3219 EXPECT_TRUE(m.Matches(a));
3220 a.set_n(-1);
3221 EXPECT_FALSE(m.Matches(a));
3222}
3223
3224// Tests that Property() can describe itself.
3225TEST(PropertyTest, CanDescribeSelf) {
3226 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3227
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003228 EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
3229 EXPECT_EQ("is an object whose given property isn't >= 0",
3230 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003231}
3232
3233// Tests that Property() can explain the match result.
3234TEST(PropertyTest, CanExplainMatchResult) {
3235 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3236
3237 AClass a;
3238 a.set_n(1);
zhanyong.wan736baa82010-09-27 17:44:16 +00003239 EXPECT_EQ("whose given property is 1" + OfType("int"), Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003240
3241 m = Property(&AClass::n, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003242 EXPECT_EQ(
3243 "whose given property is 1" + OfType("int") + ", which is 1 more than 0",
3244 Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003245}
3246
3247// Tests that Property() works when the argument is a pointer to const.
3248TEST(PropertyForPointerTest, WorksForPointerToConst) {
3249 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3250
3251 AClass a;
3252 a.set_n(1);
3253 EXPECT_TRUE(m.Matches(&a));
3254
3255 a.set_n(-1);
3256 EXPECT_FALSE(m.Matches(&a));
3257}
3258
3259// Tests that Property() works when the argument is a pointer to non-const.
3260TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
3261 Matcher<AClass*> m = Property(&AClass::s, StartsWith("hi"));
3262
3263 AClass a;
3264 a.set_s("hill");
3265 EXPECT_TRUE(m.Matches(&a));
3266
3267 a.set_s("hole");
3268 EXPECT_FALSE(m.Matches(&a));
3269}
3270
zhanyong.wan6953a722010-01-13 05:15:07 +00003271// Tests that Property() works when the argument is a reference to a
3272// const pointer.
3273TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) {
3274 Matcher<AClass* const&> m = Property(&AClass::s, StartsWith("hi"));
3275
3276 AClass a;
3277 a.set_s("hill");
3278 EXPECT_TRUE(m.Matches(&a));
3279
3280 a.set_s("hole");
3281 EXPECT_FALSE(m.Matches(&a));
3282}
3283
shiqiane35fdd92008-12-10 05:08:54 +00003284// Tests that Property() does not match the NULL pointer.
3285TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
3286 Matcher<const AClass*> m = Property(&AClass::x, _);
3287 EXPECT_FALSE(m.Matches(NULL));
3288}
3289
3290// Tests that Property(&Foo::property, ...) works when the argument's
3291// type is a sub-type of const Foo*.
3292TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
3293 // The matcher expects a DerivedClass, but inside the Property() we
3294 // say AClass.
3295 Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0));
3296
3297 DerivedClass d;
3298 d.set_n(1);
3299 EXPECT_TRUE(m.Matches(&d));
3300
3301 d.set_n(-1);
3302 EXPECT_FALSE(m.Matches(&d));
3303}
3304
3305// Tests that Property() can describe itself when used to match a pointer.
3306TEST(PropertyForPointerTest, CanDescribeSelf) {
3307 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3308
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003309 EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
3310 EXPECT_EQ("is an object whose given property isn't >= 0",
3311 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003312}
3313
3314// Tests that Property() can explain the result of matching a pointer.
3315TEST(PropertyForPointerTest, CanExplainMatchResult) {
3316 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3317
3318 AClass a;
3319 a.set_n(1);
3320 EXPECT_EQ("", Explain(m, static_cast<const AClass*>(NULL)));
zhanyong.wan736baa82010-09-27 17:44:16 +00003321 EXPECT_EQ(
3322 "which points to an object whose given property is 1" + OfType("int"),
3323 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003324
3325 m = Property(&AClass::n, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003326 EXPECT_EQ("which points to an object whose given property is 1" +
3327 OfType("int") + ", which is 1 more than 0",
3328 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003329}
3330
3331// Tests ResultOf.
3332
3333// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3334// function pointer.
3335string IntToStringFunction(int input) { return input == 1 ? "foo" : "bar"; }
3336
3337TEST(ResultOfTest, WorksForFunctionPointers) {
3338 Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(string("foo")));
3339
3340 EXPECT_TRUE(matcher.Matches(1));
3341 EXPECT_FALSE(matcher.Matches(2));
3342}
3343
3344// Tests that ResultOf() can describe itself.
3345TEST(ResultOfTest, CanDescribeItself) {
3346 Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo"));
3347
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003348 EXPECT_EQ("is mapped by the given callable to a value that "
3349 "is equal to \"foo\"", Describe(matcher));
3350 EXPECT_EQ("is mapped by the given callable to a value that "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003351 "isn't equal to \"foo\"", DescribeNegation(matcher));
shiqiane35fdd92008-12-10 05:08:54 +00003352}
3353
3354// Tests that ResultOf() can explain the match result.
3355int IntFunction(int input) { return input == 42 ? 80 : 90; }
3356
3357TEST(ResultOfTest, CanExplainMatchResult) {
3358 Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
zhanyong.wan736baa82010-09-27 17:44:16 +00003359 EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int"),
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003360 Explain(matcher, 36));
shiqiane35fdd92008-12-10 05:08:54 +00003361
3362 matcher = ResultOf(&IntFunction, GreaterThan(85));
zhanyong.wan736baa82010-09-27 17:44:16 +00003363 EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int") +
3364 ", which is 5 more than 85", Explain(matcher, 36));
shiqiane35fdd92008-12-10 05:08:54 +00003365}
3366
3367// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3368// returns a non-reference.
3369TEST(ResultOfTest, WorksForNonReferenceResults) {
3370 Matcher<int> matcher = ResultOf(&IntFunction, Eq(80));
3371
3372 EXPECT_TRUE(matcher.Matches(42));
3373 EXPECT_FALSE(matcher.Matches(36));
3374}
3375
3376// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3377// returns a reference to non-const.
zhanyong.wan736baa82010-09-27 17:44:16 +00003378double& DoubleFunction(double& input) { return input; } // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00003379
zhanyong.wan736baa82010-09-27 17:44:16 +00003380Uncopyable& RefUncopyableFunction(Uncopyable& obj) { // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00003381 return obj;
3382}
3383
3384TEST(ResultOfTest, WorksForReferenceToNonConstResults) {
3385 double x = 3.14;
3386 double x2 = x;
3387 Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x));
3388
3389 EXPECT_TRUE(matcher.Matches(x));
3390 EXPECT_FALSE(matcher.Matches(x2));
3391
3392 // Test that ResultOf works with uncopyable objects
3393 Uncopyable obj(0);
3394 Uncopyable obj2(0);
3395 Matcher<Uncopyable&> matcher2 =
3396 ResultOf(&RefUncopyableFunction, Ref(obj));
3397
3398 EXPECT_TRUE(matcher2.Matches(obj));
3399 EXPECT_FALSE(matcher2.Matches(obj2));
3400}
3401
3402// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3403// returns a reference to const.
3404const string& StringFunction(const string& input) { return input; }
3405
3406TEST(ResultOfTest, WorksForReferenceToConstResults) {
3407 string s = "foo";
3408 string s2 = s;
3409 Matcher<const string&> matcher = ResultOf(&StringFunction, Ref(s));
3410
3411 EXPECT_TRUE(matcher.Matches(s));
3412 EXPECT_FALSE(matcher.Matches(s2));
3413}
3414
3415// Tests that ResultOf(f, m) works when f(x) and m's
3416// argument types are compatible but different.
3417TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {
3418 // IntFunction() returns int but the inner matcher expects a signed char.
3419 Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85)));
3420
3421 EXPECT_TRUE(matcher.Matches(36));
3422 EXPECT_FALSE(matcher.Matches(42));
3423}
3424
shiqiane35fdd92008-12-10 05:08:54 +00003425// Tests that the program aborts when ResultOf is passed
3426// a NULL function pointer.
3427TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +00003428 EXPECT_DEATH_IF_SUPPORTED(
zhanyong.wan736baa82010-09-27 17:44:16 +00003429 ResultOf(static_cast<string(*)(int dummy)>(NULL), Eq(string("foo"))),
shiqiane35fdd92008-12-10 05:08:54 +00003430 "NULL function pointer is passed into ResultOf\\(\\)\\.");
3431}
shiqiane35fdd92008-12-10 05:08:54 +00003432
3433// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3434// function reference.
3435TEST(ResultOfTest, WorksForFunctionReferences) {
3436 Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq("foo"));
3437 EXPECT_TRUE(matcher.Matches(1));
3438 EXPECT_FALSE(matcher.Matches(2));
3439}
3440
3441// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3442// function object.
3443struct Functor : public ::std::unary_function<int, string> {
3444 result_type operator()(argument_type input) const {
3445 return IntToStringFunction(input);
3446 }
3447};
3448
3449TEST(ResultOfTest, WorksForFunctors) {
3450 Matcher<int> matcher = ResultOf(Functor(), Eq(string("foo")));
3451
3452 EXPECT_TRUE(matcher.Matches(1));
3453 EXPECT_FALSE(matcher.Matches(2));
3454}
3455
3456// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3457// functor with more then one operator() defined. ResultOf() must work
3458// for each defined operator().
3459struct PolymorphicFunctor {
3460 typedef int result_type;
3461 int operator()(int n) { return n; }
3462 int operator()(const char* s) { return static_cast<int>(strlen(s)); }
3463};
3464
3465TEST(ResultOfTest, WorksForPolymorphicFunctors) {
3466 Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5));
3467
3468 EXPECT_TRUE(matcher_int.Matches(10));
3469 EXPECT_FALSE(matcher_int.Matches(2));
3470
3471 Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5));
3472
3473 EXPECT_TRUE(matcher_string.Matches("long string"));
3474 EXPECT_FALSE(matcher_string.Matches("shrt"));
3475}
3476
3477const int* ReferencingFunction(const int& n) { return &n; }
3478
3479struct ReferencingFunctor {
3480 typedef const int* result_type;
3481 result_type operator()(const int& n) { return &n; }
3482};
3483
3484TEST(ResultOfTest, WorksForReferencingCallables) {
3485 const int n = 1;
3486 const int n2 = 1;
3487 Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n));
3488 EXPECT_TRUE(matcher2.Matches(n));
3489 EXPECT_FALSE(matcher2.Matches(n2));
3490
3491 Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n));
3492 EXPECT_TRUE(matcher3.Matches(n));
3493 EXPECT_FALSE(matcher3.Matches(n2));
3494}
3495
shiqiane35fdd92008-12-10 05:08:54 +00003496class DivisibleByImpl {
3497 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003498 explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {}
shiqiane35fdd92008-12-10 05:08:54 +00003499
zhanyong.wandb22c222010-01-28 21:52:29 +00003500 // For testing using ExplainMatchResultTo() with polymorphic matchers.
shiqiane35fdd92008-12-10 05:08:54 +00003501 template <typename T>
zhanyong.wandb22c222010-01-28 21:52:29 +00003502 bool MatchAndExplain(const T& n, MatchResultListener* listener) const {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003503 *listener << "which is " << (n % divider_) << " modulo "
zhanyong.wandb22c222010-01-28 21:52:29 +00003504 << divider_;
shiqiane35fdd92008-12-10 05:08:54 +00003505 return (n % divider_) == 0;
3506 }
3507
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003508 void DescribeTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +00003509 *os << "is divisible by " << divider_;
3510 }
3511
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003512 void DescribeNegationTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +00003513 *os << "is not divisible by " << divider_;
3514 }
3515
zhanyong.wan32de5f52009-12-23 00:13:23 +00003516 void set_divider(int a_divider) { divider_ = a_divider; }
shiqiane35fdd92008-12-10 05:08:54 +00003517 int divider() const { return divider_; }
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003518
shiqiane35fdd92008-12-10 05:08:54 +00003519 private:
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003520 int divider_;
shiqiane35fdd92008-12-10 05:08:54 +00003521};
3522
shiqiane35fdd92008-12-10 05:08:54 +00003523PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) {
3524 return MakePolymorphicMatcher(DivisibleByImpl(n));
3525}
3526
3527// Tests that when AllOf() fails, only the first failing matcher is
3528// asked to explain why.
3529TEST(ExplainMatchResultTest, AllOf_False_False) {
3530 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003531 EXPECT_EQ("which is 1 modulo 4", Explain(m, 5));
shiqiane35fdd92008-12-10 05:08:54 +00003532}
3533
3534// Tests that when AllOf() fails, only the first failing matcher is
3535// asked to explain why.
3536TEST(ExplainMatchResultTest, AllOf_False_True) {
3537 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003538 EXPECT_EQ("which is 2 modulo 4", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003539}
3540
3541// Tests that when AllOf() fails, only the first failing matcher is
3542// asked to explain why.
3543TEST(ExplainMatchResultTest, AllOf_True_False) {
3544 const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003545 EXPECT_EQ("which is 2 modulo 3", Explain(m, 5));
shiqiane35fdd92008-12-10 05:08:54 +00003546}
3547
3548// Tests that when AllOf() succeeds, all matchers are asked to explain
3549// why.
3550TEST(ExplainMatchResultTest, AllOf_True_True) {
3551 const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003552 EXPECT_EQ("which is 0 modulo 2, and which is 0 modulo 3", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003553}
3554
3555TEST(ExplainMatchResultTest, AllOf_True_True_2) {
3556 const Matcher<int> m = AllOf(Ge(2), Le(3));
3557 EXPECT_EQ("", Explain(m, 2));
3558}
3559
3560TEST(ExplainmatcherResultTest, MonomorphicMatcher) {
3561 const Matcher<int> m = GreaterThan(5);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003562 EXPECT_EQ("which is 1 more than 5", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003563}
3564
3565// The following two tests verify that values without a public copy
3566// ctor can be used as arguments to matchers like Eq(), Ge(), and etc
3567// with the help of ByRef().
3568
3569class NotCopyable {
3570 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003571 explicit NotCopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00003572
3573 int value() const { return value_; }
3574
3575 bool operator==(const NotCopyable& rhs) const {
3576 return value() == rhs.value();
3577 }
3578
3579 bool operator>=(const NotCopyable& rhs) const {
3580 return value() >= rhs.value();
3581 }
3582 private:
3583 int value_;
3584
3585 GTEST_DISALLOW_COPY_AND_ASSIGN_(NotCopyable);
3586};
3587
3588TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) {
3589 const NotCopyable const_value1(1);
3590 const Matcher<const NotCopyable&> m = Eq(ByRef(const_value1));
3591
3592 const NotCopyable n1(1), n2(2);
3593 EXPECT_TRUE(m.Matches(n1));
3594 EXPECT_FALSE(m.Matches(n2));
3595}
3596
3597TEST(ByRefTest, AllowsNotCopyableValueInMatchers) {
3598 NotCopyable value2(2);
3599 const Matcher<NotCopyable&> m = Ge(ByRef(value2));
3600
3601 NotCopyable n1(1), n2(2);
3602 EXPECT_FALSE(m.Matches(n1));
3603 EXPECT_TRUE(m.Matches(n2));
3604}
3605
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003606#if GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003607// Tests ContainerEq with different container types, and
3608// different element types.
3609
3610template <typename T>
zhanyong.wanb8243162009-06-04 05:48:20 +00003611class ContainerEqTest : public testing::Test {};
zhanyong.wan6a896b52009-01-16 01:13:50 +00003612
3613typedef testing::Types<
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003614 set<int>,
3615 vector<size_t>,
3616 multiset<size_t>,
3617 list<int> >
zhanyong.wan6a896b52009-01-16 01:13:50 +00003618 ContainerEqTestTypes;
3619
3620TYPED_TEST_CASE(ContainerEqTest, ContainerEqTestTypes);
3621
3622// Tests that the filled container is equal to itself.
3623TYPED_TEST(ContainerEqTest, EqualsSelf) {
3624 static const int vals[] = {1, 1, 2, 3, 5, 8};
3625 TypeParam my_set(vals, vals + 6);
3626 const Matcher<TypeParam> m = ContainerEq(my_set);
3627 EXPECT_TRUE(m.Matches(my_set));
3628 EXPECT_EQ("", Explain(m, my_set));
3629}
3630
3631// Tests that missing values are reported.
3632TYPED_TEST(ContainerEqTest, ValueMissing) {
3633 static const int vals[] = {1, 1, 2, 3, 5, 8};
3634 static const int test_vals[] = {2, 1, 8, 5};
3635 TypeParam my_set(vals, vals + 6);
3636 TypeParam test_set(test_vals, test_vals + 4);
3637 const Matcher<TypeParam> m = ContainerEq(my_set);
3638 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003639 EXPECT_EQ("which doesn't have these expected elements: 3",
3640 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003641}
3642
3643// Tests that added values are reported.
3644TYPED_TEST(ContainerEqTest, ValueAdded) {
3645 static const int vals[] = {1, 1, 2, 3, 5, 8};
3646 static const int test_vals[] = {1, 2, 3, 5, 8, 46};
3647 TypeParam my_set(vals, vals + 6);
3648 TypeParam test_set(test_vals, test_vals + 6);
3649 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3650 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003651 EXPECT_EQ("which has these unexpected elements: 46", Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003652}
3653
3654// Tests that added and missing values are reported together.
3655TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) {
3656 static const int vals[] = {1, 1, 2, 3, 5, 8};
3657 static const int test_vals[] = {1, 2, 3, 8, 46};
3658 TypeParam my_set(vals, vals + 6);
3659 TypeParam test_set(test_vals, test_vals + 5);
3660 const Matcher<TypeParam> m = ContainerEq(my_set);
3661 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003662 EXPECT_EQ("which has these unexpected elements: 46,\n"
3663 "and doesn't have these expected elements: 5",
3664 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003665}
3666
3667// Tests duplicated value -- expect no explanation.
3668TYPED_TEST(ContainerEqTest, DuplicateDifference) {
3669 static const int vals[] = {1, 1, 2, 3, 5, 8};
3670 static const int test_vals[] = {1, 2, 3, 5, 8};
3671 TypeParam my_set(vals, vals + 6);
3672 TypeParam test_set(test_vals, test_vals + 5);
3673 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3674 // Depending on the container, match may be true or false
3675 // But in any case there should be no explanation.
3676 EXPECT_EQ("", Explain(m, test_set));
3677}
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003678#endif // GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003679
3680// Tests that mutliple missing values are reported.
3681// Using just vector here, so order is predicatble.
3682TEST(ContainerEqExtraTest, MultipleValuesMissing) {
3683 static const int vals[] = {1, 1, 2, 3, 5, 8};
3684 static const int test_vals[] = {2, 1, 5};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003685 vector<int> my_set(vals, vals + 6);
3686 vector<int> test_set(test_vals, test_vals + 3);
3687 const Matcher<vector<int> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003688 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003689 EXPECT_EQ("which doesn't have these expected elements: 3, 8",
3690 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003691}
3692
3693// Tests that added values are reported.
3694// Using just vector here, so order is predicatble.
3695TEST(ContainerEqExtraTest, MultipleValuesAdded) {
3696 static const int vals[] = {1, 1, 2, 3, 5, 8};
3697 static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003698 list<size_t> my_set(vals, vals + 6);
3699 list<size_t> test_set(test_vals, test_vals + 7);
3700 const Matcher<const list<size_t>&> m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003701 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003702 EXPECT_EQ("which has these unexpected elements: 92, 46",
3703 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003704}
3705
3706// Tests that added and missing values are reported together.
3707TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) {
3708 static const int vals[] = {1, 1, 2, 3, 5, 8};
3709 static const int test_vals[] = {1, 2, 3, 92, 46};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003710 list<size_t> my_set(vals, vals + 6);
3711 list<size_t> test_set(test_vals, test_vals + 5);
3712 const Matcher<const list<size_t> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003713 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003714 EXPECT_EQ("which has these unexpected elements: 92, 46,\n"
3715 "and doesn't have these expected elements: 5, 8",
zhanyong.wan6a896b52009-01-16 01:13:50 +00003716 Explain(m, test_set));
3717}
3718
3719// Tests to see that duplicate elements are detected,
3720// but (as above) not reported in the explanation.
3721TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) {
3722 static const int vals[] = {1, 1, 2, 3, 5, 8};
3723 static const int test_vals[] = {1, 2, 3, 5, 8};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003724 vector<int> my_set(vals, vals + 6);
3725 vector<int> test_set(test_vals, test_vals + 5);
3726 const Matcher<vector<int> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003727 EXPECT_TRUE(m.Matches(my_set));
3728 EXPECT_FALSE(m.Matches(test_set));
3729 // There is nothing to report when both sets contain all the same values.
3730 EXPECT_EQ("", Explain(m, test_set));
3731}
3732
3733// Tests that ContainerEq works for non-trivial associative containers,
3734// like maps.
3735TEST(ContainerEqExtraTest, WorksForMaps) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003736 map<int, std::string> my_map;
zhanyong.wan6a896b52009-01-16 01:13:50 +00003737 my_map[0] = "a";
3738 my_map[1] = "b";
3739
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003740 map<int, std::string> test_map;
zhanyong.wan6a896b52009-01-16 01:13:50 +00003741 test_map[0] = "aa";
3742 test_map[1] = "b";
3743
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003744 const Matcher<const map<int, std::string>&> m = ContainerEq(my_map);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003745 EXPECT_TRUE(m.Matches(my_map));
3746 EXPECT_FALSE(m.Matches(test_map));
3747
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003748 EXPECT_EQ("which has these unexpected elements: (0, \"aa\"),\n"
3749 "and doesn't have these expected elements: (0, \"a\")",
zhanyong.wan6a896b52009-01-16 01:13:50 +00003750 Explain(m, test_map));
3751}
3752
zhanyong.wanb8243162009-06-04 05:48:20 +00003753TEST(ContainerEqExtraTest, WorksForNativeArray) {
3754 int a1[] = { 1, 2, 3 };
3755 int a2[] = { 1, 2, 3 };
3756 int b[] = { 1, 2, 4 };
3757
3758 EXPECT_THAT(a1, ContainerEq(a2));
3759 EXPECT_THAT(a1, Not(ContainerEq(b)));
3760}
3761
3762TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) {
3763 const char a1[][3] = { "hi", "lo" };
3764 const char a2[][3] = { "hi", "lo" };
3765 const char b[][3] = { "lo", "hi" };
3766
3767 // Tests using ContainerEq() in the first dimension.
3768 EXPECT_THAT(a1, ContainerEq(a2));
3769 EXPECT_THAT(a1, Not(ContainerEq(b)));
3770
3771 // Tests using ContainerEq() in the second dimension.
3772 EXPECT_THAT(a1, ElementsAre(ContainerEq(a2[0]), ContainerEq(a2[1])));
3773 EXPECT_THAT(a1, ElementsAre(Not(ContainerEq(b[0])), ContainerEq(a2[1])));
3774}
3775
3776TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) {
3777 const int a1[] = { 1, 2, 3 };
3778 const int a2[] = { 1, 2, 3 };
3779 const int b[] = { 1, 2, 3, 4 };
3780
zhanyong.wan2661c682009-06-09 05:42:12 +00003781 const int* const p1 = a1;
3782 EXPECT_THAT(make_tuple(p1, 3), ContainerEq(a2));
3783 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(b)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003784
3785 const int c[] = { 1, 3, 2 };
zhanyong.wan2661c682009-06-09 05:42:12 +00003786 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(c)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003787}
3788
3789TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) {
3790 std::string a1[][3] = {
3791 { "hi", "hello", "ciao" },
3792 { "bye", "see you", "ciao" }
3793 };
3794
3795 std::string a2[][3] = {
3796 { "hi", "hello", "ciao" },
3797 { "bye", "see you", "ciao" }
3798 };
3799
3800 const Matcher<const std::string(&)[2][3]> m = ContainerEq(a2);
3801 EXPECT_THAT(a1, m);
3802
3803 a2[0][0] = "ha";
3804 EXPECT_THAT(a1, m);
3805}
3806
zhanyong.wan898725c2011-09-16 16:45:39 +00003807TEST(WhenSortedByTest, WorksForEmptyContainer) {
3808 const vector<int> numbers;
3809 EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre()));
3810 EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1))));
3811}
3812
3813TEST(WhenSortedByTest, WorksForNonEmptyContainer) {
3814 vector<unsigned> numbers;
3815 numbers.push_back(3);
3816 numbers.push_back(1);
3817 numbers.push_back(2);
3818 numbers.push_back(2);
3819 EXPECT_THAT(numbers, WhenSortedBy(greater<unsigned>(),
3820 ElementsAre(3, 2, 2, 1)));
3821 EXPECT_THAT(numbers, Not(WhenSortedBy(greater<unsigned>(),
3822 ElementsAre(1, 2, 2, 3))));
3823}
3824
3825TEST(WhenSortedByTest, WorksForNonVectorContainer) {
3826 list<string> words;
3827 words.push_back("say");
3828 words.push_back("hello");
3829 words.push_back("world");
3830 EXPECT_THAT(words, WhenSortedBy(less<string>(),
3831 ElementsAre("hello", "say", "world")));
3832 EXPECT_THAT(words, Not(WhenSortedBy(less<string>(),
3833 ElementsAre("say", "hello", "world"))));
3834}
3835
3836TEST(WhenSortedByTest, WorksForNativeArray) {
3837 const int numbers[] = { 1, 3, 2, 4 };
3838 const int sorted_numbers[] = { 1, 2, 3, 4 };
3839 EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre(1, 2, 3, 4)));
3840 EXPECT_THAT(numbers, WhenSortedBy(less<int>(),
3841 ElementsAreArray(sorted_numbers)));
3842 EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1, 3, 2, 4))));
3843}
3844
3845TEST(WhenSortedByTest, CanDescribeSelf) {
3846 const Matcher<vector<int> > m = WhenSortedBy(less<int>(), ElementsAre(1, 2));
3847 EXPECT_EQ("(when sorted) has 2 elements where\n"
3848 "element #0 is equal to 1,\n"
3849 "element #1 is equal to 2",
3850 Describe(m));
3851 EXPECT_EQ("(when sorted) doesn't have 2 elements, or\n"
3852 "element #0 isn't equal to 1, or\n"
3853 "element #1 isn't equal to 2",
3854 DescribeNegation(m));
3855}
3856
3857TEST(WhenSortedByTest, ExplainsMatchResult) {
3858 const int a[] = { 2, 1 };
3859 EXPECT_EQ("which is { 1, 2 } when sorted, whose element #0 doesn't match",
3860 Explain(WhenSortedBy(less<int>(), ElementsAre(2, 3)), a));
3861 EXPECT_EQ("which is { 1, 2 } when sorted",
3862 Explain(WhenSortedBy(less<int>(), ElementsAre(1, 2)), a));
3863}
3864
3865// WhenSorted() is a simple wrapper on WhenSortedBy(). Hence we don't
3866// need to test it as exhaustively as we test the latter.
3867
3868TEST(WhenSortedTest, WorksForEmptyContainer) {
3869 const vector<int> numbers;
3870 EXPECT_THAT(numbers, WhenSorted(ElementsAre()));
3871 EXPECT_THAT(numbers, Not(WhenSorted(ElementsAre(1))));
3872}
3873
3874TEST(WhenSortedTest, WorksForNonEmptyContainer) {
3875 list<string> words;
3876 words.push_back("3");
3877 words.push_back("1");
3878 words.push_back("2");
3879 words.push_back("2");
3880 EXPECT_THAT(words, WhenSorted(ElementsAre("1", "2", "2", "3")));
3881 EXPECT_THAT(words, Not(WhenSorted(ElementsAre("3", "1", "2", "2"))));
3882}
3883
zhanyong.wan736baa82010-09-27 17:44:16 +00003884// Tests IsReadableTypeName().
3885
3886TEST(IsReadableTypeNameTest, ReturnsTrueForShortNames) {
3887 EXPECT_TRUE(IsReadableTypeName("int"));
3888 EXPECT_TRUE(IsReadableTypeName("const unsigned char*"));
3889 EXPECT_TRUE(IsReadableTypeName("MyMap<int, void*>"));
3890 EXPECT_TRUE(IsReadableTypeName("void (*)(int, bool)"));
3891}
3892
3893TEST(IsReadableTypeNameTest, ReturnsTrueForLongNonTemplateNonFunctionNames) {
3894 EXPECT_TRUE(IsReadableTypeName("my_long_namespace::MyClassName"));
3895 EXPECT_TRUE(IsReadableTypeName("int [5][6][7][8][9][10][11]"));
3896 EXPECT_TRUE(IsReadableTypeName("my_namespace::MyOuterClass::MyInnerClass"));
3897}
3898
3899TEST(IsReadableTypeNameTest, ReturnsFalseForLongTemplateNames) {
3900 EXPECT_FALSE(
3901 IsReadableTypeName("basic_string<char, std::char_traits<char> >"));
3902 EXPECT_FALSE(IsReadableTypeName("std::vector<int, std::alloc_traits<int> >"));
3903}
3904
3905TEST(IsReadableTypeNameTest, ReturnsFalseForLongFunctionTypeNames) {
3906 EXPECT_FALSE(IsReadableTypeName("void (&)(int, bool, char, float)"));
3907}
3908
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003909// Tests JoinAsTuple().
3910
3911TEST(JoinAsTupleTest, JoinsEmptyTuple) {
3912 EXPECT_EQ("", JoinAsTuple(Strings()));
3913}
3914
3915TEST(JoinAsTupleTest, JoinsOneTuple) {
3916 const char* fields[] = { "1" };
3917 EXPECT_EQ("1", JoinAsTuple(Strings(fields, fields + 1)));
3918}
3919
3920TEST(JoinAsTupleTest, JoinsTwoTuple) {
3921 const char* fields[] = { "1", "a" };
3922 EXPECT_EQ("(1, a)", JoinAsTuple(Strings(fields, fields + 2)));
3923}
3924
3925TEST(JoinAsTupleTest, JoinsTenTuple) {
3926 const char* fields[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
3927 EXPECT_EQ("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)",
3928 JoinAsTuple(Strings(fields, fields + 10)));
3929}
3930
3931// Tests FormatMatcherDescription().
3932
3933TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) {
3934 EXPECT_EQ("is even",
zhanyong.wanb4140802010-06-08 22:53:57 +00003935 FormatMatcherDescription(false, "IsEven", Strings()));
3936 EXPECT_EQ("not (is even)",
3937 FormatMatcherDescription(true, "IsEven", Strings()));
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003938
3939 const char* params[] = { "5" };
3940 EXPECT_EQ("equals 5",
zhanyong.wanb4140802010-06-08 22:53:57 +00003941 FormatMatcherDescription(false, "Equals",
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003942 Strings(params, params + 1)));
3943
3944 const char* params2[] = { "5", "8" };
3945 EXPECT_EQ("is in range (5, 8)",
zhanyong.wanb4140802010-06-08 22:53:57 +00003946 FormatMatcherDescription(false, "IsInRange",
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003947 Strings(params2, params2 + 2)));
3948}
3949
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003950// Tests PolymorphicMatcher::mutable_impl().
3951TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {
3952 PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
3953 DivisibleByImpl& impl = m.mutable_impl();
3954 EXPECT_EQ(42, impl.divider());
3955
3956 impl.set_divider(0);
3957 EXPECT_EQ(0, m.mutable_impl().divider());
3958}
3959
3960// Tests PolymorphicMatcher::impl().
3961TEST(PolymorphicMatcherTest, CanAccessImpl) {
3962 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
3963 const DivisibleByImpl& impl = m.impl();
3964 EXPECT_EQ(42, impl.divider());
3965}
3966
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003967TEST(MatcherTupleTest, ExplainsMatchFailure) {
3968 stringstream ss1;
3969 ExplainMatchFailureTupleTo(make_tuple(Matcher<char>(Eq('a')), GreaterThan(5)),
3970 make_tuple('a', 10), &ss1);
3971 EXPECT_EQ("", ss1.str()); // Successful match.
3972
3973 stringstream ss2;
3974 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
3975 make_tuple(2, 'b'), &ss2);
3976 EXPECT_EQ(" Expected arg #0: is > 5\n"
3977 " Actual: 2, which is 3 less than 5\n"
zhanyong.wand60c5f42010-07-21 22:21:07 +00003978 " Expected arg #1: is equal to 'a' (97, 0x61)\n"
3979 " Actual: 'b' (98, 0x62)\n",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003980 ss2.str()); // Failed match where both arguments need explanation.
3981
3982 stringstream ss3;
3983 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
3984 make_tuple(2, 'a'), &ss3);
3985 EXPECT_EQ(" Expected arg #0: is > 5\n"
3986 " Actual: 2, which is 3 less than 5\n",
3987 ss3.str()); // Failed match where only one argument needs
3988 // explanation.
3989}
3990
zhanyong.wan33605ba2010-04-22 23:37:47 +00003991// Tests Each().
3992
3993TEST(EachTest, ExplainsMatchResultCorrectly) {
3994 set<int> a; // empty
3995
3996 Matcher<set<int> > m = Each(2);
3997 EXPECT_EQ("", Explain(m, a));
3998
zhanyong.wan736baa82010-09-27 17:44:16 +00003999 Matcher<const int(&)[1]> n = Each(1); // NOLINT
zhanyong.wan33605ba2010-04-22 23:37:47 +00004000
4001 const int b[1] = { 1 };
4002 EXPECT_EQ("", Explain(n, b));
4003
4004 n = Each(3);
4005 EXPECT_EQ("whose element #0 doesn't match", Explain(n, b));
4006
4007 a.insert(1);
4008 a.insert(2);
4009 a.insert(3);
4010 m = Each(GreaterThan(0));
4011 EXPECT_EQ("", Explain(m, a));
4012
4013 m = Each(GreaterThan(10));
4014 EXPECT_EQ("whose element #0 doesn't match, which is 9 less than 10",
4015 Explain(m, a));
4016}
4017
4018TEST(EachTest, DescribesItselfCorrectly) {
4019 Matcher<vector<int> > m = Each(1);
4020 EXPECT_EQ("only contains elements that is equal to 1", Describe(m));
4021
4022 Matcher<vector<int> > m2 = Not(m);
4023 EXPECT_EQ("contains some element that isn't equal to 1", Describe(m2));
4024}
4025
4026TEST(EachTest, MatchesVectorWhenAllElementsMatch) {
4027 vector<int> some_vector;
4028 EXPECT_THAT(some_vector, Each(1));
4029 some_vector.push_back(3);
4030 EXPECT_THAT(some_vector, Not(Each(1)));
4031 EXPECT_THAT(some_vector, Each(3));
4032 some_vector.push_back(1);
4033 some_vector.push_back(2);
4034 EXPECT_THAT(some_vector, Not(Each(3)));
4035 EXPECT_THAT(some_vector, Each(Lt(3.5)));
4036
4037 vector<string> another_vector;
4038 another_vector.push_back("fee");
4039 EXPECT_THAT(another_vector, Each(string("fee")));
4040 another_vector.push_back("fie");
4041 another_vector.push_back("foe");
4042 another_vector.push_back("fum");
4043 EXPECT_THAT(another_vector, Not(Each(string("fee"))));
4044}
4045
4046TEST(EachTest, MatchesMapWhenAllElementsMatch) {
4047 map<const char*, int> my_map;
4048 const char* bar = "a string";
4049 my_map[bar] = 2;
4050 EXPECT_THAT(my_map, Each(make_pair(bar, 2)));
4051
4052 map<string, int> another_map;
4053 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1)));
4054 another_map["fee"] = 1;
4055 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1)));
4056 another_map["fie"] = 2;
4057 another_map["foe"] = 3;
4058 another_map["fum"] = 4;
4059 EXPECT_THAT(another_map, Not(Each(make_pair(string("fee"), 1))));
4060 EXPECT_THAT(another_map, Not(Each(make_pair(string("fum"), 1))));
4061 EXPECT_THAT(another_map, Each(Pair(_, Gt(0))));
4062}
4063
4064TEST(EachTest, AcceptsMatcher) {
4065 const int a[] = { 1, 2, 3 };
4066 EXPECT_THAT(a, Each(Gt(0)));
4067 EXPECT_THAT(a, Not(Each(Gt(1))));
4068}
4069
4070TEST(EachTest, WorksForNativeArrayAsTuple) {
4071 const int a[] = { 1, 2 };
4072 const int* const pointer = a;
4073 EXPECT_THAT(make_tuple(pointer, 2), Each(Gt(0)));
4074 EXPECT_THAT(make_tuple(pointer, 2), Not(Each(Gt(1))));
4075}
4076
zhanyong.wanab5b77c2010-05-17 19:32:48 +00004077// For testing Pointwise().
4078class IsHalfOfMatcher {
4079 public:
4080 template <typename T1, typename T2>
4081 bool MatchAndExplain(const tuple<T1, T2>& a_pair,
4082 MatchResultListener* listener) const {
4083 if (get<0>(a_pair) == get<1>(a_pair)/2) {
4084 *listener << "where the second is " << get<1>(a_pair);
4085 return true;
4086 } else {
4087 *listener << "where the second/2 is " << get<1>(a_pair)/2;
4088 return false;
4089 }
4090 }
4091
4092 void DescribeTo(ostream* os) const {
4093 *os << "are a pair where the first is half of the second";
4094 }
4095
4096 void DescribeNegationTo(ostream* os) const {
4097 *os << "are a pair where the first isn't half of the second";
4098 }
4099};
4100
4101PolymorphicMatcher<IsHalfOfMatcher> IsHalfOf() {
4102 return MakePolymorphicMatcher(IsHalfOfMatcher());
4103}
4104
4105TEST(PointwiseTest, DescribesSelf) {
4106 vector<int> rhs;
4107 rhs.push_back(1);
4108 rhs.push_back(2);
4109 rhs.push_back(3);
4110 const Matcher<const vector<int>&> m = Pointwise(IsHalfOf(), rhs);
4111 EXPECT_EQ("contains 3 values, where each value and its corresponding value "
4112 "in { 1, 2, 3 } are a pair where the first is half of the second",
4113 Describe(m));
4114 EXPECT_EQ("doesn't contain exactly 3 values, or contains a value x at some "
4115 "index i where x and the i-th value of { 1, 2, 3 } are a pair "
4116 "where the first isn't half of the second",
4117 DescribeNegation(m));
4118}
4119
4120TEST(PointwiseTest, MakesCopyOfRhs) {
4121 list<signed char> rhs;
4122 rhs.push_back(2);
4123 rhs.push_back(4);
4124
4125 int lhs[] = { 1, 2 };
4126 const Matcher<const int (&)[2]> m = Pointwise(IsHalfOf(), rhs);
4127 EXPECT_THAT(lhs, m);
4128
4129 // Changing rhs now shouldn't affect m, which made a copy of rhs.
4130 rhs.push_back(6);
4131 EXPECT_THAT(lhs, m);
4132}
4133
4134TEST(PointwiseTest, WorksForLhsNativeArray) {
4135 const int lhs[] = { 1, 2, 3 };
4136 vector<int> rhs;
4137 rhs.push_back(2);
4138 rhs.push_back(4);
4139 rhs.push_back(6);
4140 EXPECT_THAT(lhs, Pointwise(Lt(), rhs));
4141 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
4142}
4143
4144TEST(PointwiseTest, WorksForRhsNativeArray) {
4145 const int rhs[] = { 1, 2, 3 };
4146 vector<int> lhs;
4147 lhs.push_back(2);
4148 lhs.push_back(4);
4149 lhs.push_back(6);
4150 EXPECT_THAT(lhs, Pointwise(Gt(), rhs));
4151 EXPECT_THAT(lhs, Not(Pointwise(Lt(), rhs)));
4152}
4153
4154TEST(PointwiseTest, RejectsWrongSize) {
4155 const double lhs[2] = { 1, 2 };
4156 const int rhs[1] = { 0 };
4157 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
4158 EXPECT_EQ("which contains 2 values",
4159 Explain(Pointwise(Gt(), rhs), lhs));
4160
4161 const int rhs2[3] = { 0, 1, 2 };
4162 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs2)));
4163}
4164
4165TEST(PointwiseTest, RejectsWrongContent) {
4166 const double lhs[3] = { 1, 2, 3 };
4167 const int rhs[3] = { 2, 6, 4 };
4168 EXPECT_THAT(lhs, Not(Pointwise(IsHalfOf(), rhs)));
4169 EXPECT_EQ("where the value pair (2, 6) at index #1 don't match, "
4170 "where the second/2 is 3",
4171 Explain(Pointwise(IsHalfOf(), rhs), lhs));
4172}
4173
4174TEST(PointwiseTest, AcceptsCorrectContent) {
4175 const double lhs[3] = { 1, 2, 3 };
4176 const int rhs[3] = { 2, 4, 6 };
4177 EXPECT_THAT(lhs, Pointwise(IsHalfOf(), rhs));
4178 EXPECT_EQ("", Explain(Pointwise(IsHalfOf(), rhs), lhs));
4179}
4180
4181TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) {
4182 const double lhs[3] = { 1, 2, 3 };
4183 const int rhs[3] = { 2, 4, 6 };
4184 const Matcher<tuple<const double&, const int&> > m1 = IsHalfOf();
4185 EXPECT_THAT(lhs, Pointwise(m1, rhs));
4186 EXPECT_EQ("", Explain(Pointwise(m1, rhs), lhs));
4187
4188 // This type works as a tuple<const double&, const int&> can be
4189 // implicitly cast to tuple<double, int>.
4190 const Matcher<tuple<double, int> > m2 = IsHalfOf();
4191 EXPECT_THAT(lhs, Pointwise(m2, rhs));
4192 EXPECT_EQ("", Explain(Pointwise(m2, rhs), lhs));
4193}
4194
shiqiane35fdd92008-12-10 05:08:54 +00004195} // namespace gmock_matchers_test
4196} // namespace testing