blob: 3b151dbd9bf62342bb9ae76ff1bcb42ef1090009 [file] [log] [blame]
shiqiane35fdd92008-12-10 05:08:54 +00001// Copyright 2007, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29//
30// Author: wan@google.com (Zhanyong Wan)
31
32// Google Mock - a framework for writing C++ mock classes.
33//
34// This file tests some commonly used argument matchers.
35
36#include <gmock/gmock-matchers.h>
37
38#include <string.h>
39#include <functional>
zhanyong.wana862f1d2010-03-15 21:23:04 +000040#include <iostream>
zhanyong.wan6a896b52009-01-16 01:13:50 +000041#include <list>
42#include <map>
43#include <set>
shiqiane35fdd92008-12-10 05:08:54 +000044#include <sstream>
zhanyong.wan6a896b52009-01-16 01:13:50 +000045#include <string>
zhanyong.wanf5e1ce52009-09-16 07:02:02 +000046#include <utility>
zhanyong.wan6a896b52009-01-16 01:13:50 +000047#include <vector>
shiqiane35fdd92008-12-10 05:08:54 +000048#include <gmock/gmock.h>
49#include <gtest/gtest.h>
50#include <gtest/gtest-spi.h>
51
52namespace testing {
zhanyong.wan4a5330d2009-02-19 00:36:44 +000053
54namespace internal {
55string FormatMatcherDescriptionSyntaxError(const char* description,
56 const char* error_pos);
57int GetParamIndex(const char* param_names[], const string& param_name);
58string JoinAsTuple(const Strings& fields);
59bool SkipPrefix(const char* prefix, const char** pstr);
60} // namespace internal
61
shiqiane35fdd92008-12-10 05:08:54 +000062namespace gmock_matchers_test {
63
zhanyong.wanab5b77c2010-05-17 19:32:48 +000064using std::list;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000065using std::make_pair;
zhanyong.wanb5937da2009-07-16 20:26:41 +000066using std::map;
67using std::multimap;
zhanyong.wanab5b77c2010-05-17 19:32:48 +000068using std::multiset;
69using std::ostream;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000070using std::pair;
zhanyong.wan33605ba2010-04-22 23:37:47 +000071using std::set;
shiqiane35fdd92008-12-10 05:08:54 +000072using std::stringstream;
zhanyong.wanab5b77c2010-05-17 19:32:48 +000073using std::tr1::get;
zhanyong.wanb8243162009-06-04 05:48:20 +000074using std::tr1::make_tuple;
zhanyong.wanab5b77c2010-05-17 19:32:48 +000075using std::tr1::tuple;
zhanyong.wan33605ba2010-04-22 23:37:47 +000076using std::vector;
shiqiane35fdd92008-12-10 05:08:54 +000077using testing::A;
zhanyong.wanbf550852009-06-09 06:09:53 +000078using testing::AllArgs;
shiqiane35fdd92008-12-10 05:08:54 +000079using testing::AllOf;
80using testing::An;
81using testing::AnyOf;
82using testing::ByRef;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000083using testing::ContainsRegex;
shiqiane35fdd92008-12-10 05:08:54 +000084using testing::DoubleEq;
85using testing::EndsWith;
86using testing::Eq;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000087using testing::ExplainMatchResult;
shiqiane35fdd92008-12-10 05:08:54 +000088using testing::Field;
89using testing::FloatEq;
90using testing::Ge;
91using testing::Gt;
92using testing::HasSubstr;
zhanyong.wan2d970ee2009-09-24 21:41:36 +000093using testing::IsNull;
zhanyong.wanb5937da2009-07-16 20:26:41 +000094using testing::Key;
shiqiane35fdd92008-12-10 05:08:54 +000095using testing::Le;
96using testing::Lt;
97using testing::MakeMatcher;
98using testing::MakePolymorphicMatcher;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000099using testing::MatchResultListener;
shiqiane35fdd92008-12-10 05:08:54 +0000100using testing::Matcher;
101using testing::MatcherCast;
102using testing::MatcherInterface;
103using testing::Matches;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000104using testing::MatchesRegex;
shiqiane35fdd92008-12-10 05:08:54 +0000105using testing::NanSensitiveDoubleEq;
106using testing::NanSensitiveFloatEq;
107using testing::Ne;
108using testing::Not;
109using testing::NotNull;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000110using testing::Pair;
shiqiane35fdd92008-12-10 05:08:54 +0000111using testing::Pointee;
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000112using testing::Pointwise;
shiqiane35fdd92008-12-10 05:08:54 +0000113using testing::PolymorphicMatcher;
114using testing::Property;
115using testing::Ref;
116using testing::ResultOf;
117using testing::StartsWith;
118using testing::StrCaseEq;
119using testing::StrCaseNe;
120using testing::StrEq;
121using testing::StrNe;
122using testing::Truly;
123using testing::TypedEq;
zhanyong.wanb8243162009-06-04 05:48:20 +0000124using testing::Value;
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.wan4a5330d2009-02-19 00:36:44 +0000129using testing::internal::FormatMatcherDescriptionSyntaxError;
130using testing::internal::GetParamIndex;
131using testing::internal::Interpolation;
132using testing::internal::Interpolations;
133using testing::internal::JoinAsTuple;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000134using testing::internal::RE;
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000135using testing::internal::SkipPrefix;
zhanyong.wana862f1d2010-03-15 21:23:04 +0000136using testing::internal::StreamMatchResultListener;
shiqiane35fdd92008-12-10 05:08:54 +0000137using testing::internal::String;
zhanyong.wan34b034c2010-03-05 21:23:23 +0000138using testing::internal::StringMatchResultListener;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000139using testing::internal::Strings;
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000140using testing::internal::ValidateMatcherDescription;
141using testing::internal::kInvalidInterpolation;
142using testing::internal::kPercentInterpolation;
143using testing::internal::kTupleInterpolation;
vladlosev79b83502009-11-18 00:43:37 +0000144using testing::internal::linked_ptr;
vladloseve56daa72009-11-18 01:08:08 +0000145using testing::internal::scoped_ptr;
shiqiane35fdd92008-12-10 05:08:54 +0000146using testing::internal::string;
147
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000148// For testing ExplainMatchResultTo().
149class GreaterThanMatcher : public MatcherInterface<int> {
150 public:
151 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
152
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000153 virtual void DescribeTo(ostream* os) const {
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000154 *os << "is > " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000155 }
156
zhanyong.wandb22c222010-01-28 21:52:29 +0000157 virtual bool MatchAndExplain(int lhs,
158 MatchResultListener* listener) const {
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000159 const int diff = lhs - rhs_;
160 if (diff > 0) {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000161 *listener << "which is " << diff << " more than " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000162 } else if (diff == 0) {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000163 *listener << "which is the same as " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000164 } else {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000165 *listener << "which is " << -diff << " less than " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000166 }
zhanyong.wandb22c222010-01-28 21:52:29 +0000167
168 return lhs > rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000169 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000170
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000171 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000172 int rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000173};
174
175Matcher<int> GreaterThan(int n) {
176 return MakeMatcher(new GreaterThanMatcher(n));
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
zhanyong.wan18490652009-05-11 18:54:08 +0000547class Base {};
548class Derived : public Base {};
549
550// Tests that SafeMatcherCast<T>(m) works when m is a polymorphic matcher.
551TEST(SafeMatcherCastTest, FromPolymorphicMatcher) {
552 Matcher<char> m2 = SafeMatcherCast<char>(Eq(32));
553 EXPECT_TRUE(m2.Matches(' '));
554 EXPECT_FALSE(m2.Matches('\n'));
555}
556
zhanyong.wan16cf4732009-05-14 20:55:30 +0000557// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where
558// T and U are arithmetic types and T can be losslessly converted to
559// U.
560TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) {
zhanyong.wan18490652009-05-11 18:54:08 +0000561 Matcher<double> m1 = DoubleEq(1.0);
zhanyong.wan16cf4732009-05-14 20:55:30 +0000562 Matcher<float> m2 = SafeMatcherCast<float>(m1);
563 EXPECT_TRUE(m2.Matches(1.0f));
564 EXPECT_FALSE(m2.Matches(2.0f));
565
566 Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>('a'));
567 EXPECT_TRUE(m3.Matches('a'));
568 EXPECT_FALSE(m3.Matches('b'));
zhanyong.wan18490652009-05-11 18:54:08 +0000569}
570
571// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where T and U
572// are pointers or references to a derived and a base class, correspondingly.
573TEST(SafeMatcherCastTest, FromBaseClass) {
574 Derived d, d2;
575 Matcher<Base*> m1 = Eq(&d);
576 Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1);
577 EXPECT_TRUE(m2.Matches(&d));
578 EXPECT_FALSE(m2.Matches(&d2));
579
580 Matcher<Base&> m3 = Ref(d);
581 Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3);
582 EXPECT_TRUE(m4.Matches(d));
583 EXPECT_FALSE(m4.Matches(d2));
584}
585
586// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<const T&>.
587TEST(SafeMatcherCastTest, FromConstReferenceToReference) {
588 int n = 0;
589 Matcher<const int&> m1 = Ref(n);
590 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
591 int n1 = 0;
592 EXPECT_TRUE(m2.Matches(n));
593 EXPECT_FALSE(m2.Matches(n1));
594}
595
596// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
597TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) {
598 Matcher<int> m1 = Eq(0);
599 Matcher<const int&> m2 = SafeMatcherCast<const int&>(m1);
600 EXPECT_TRUE(m2.Matches(0));
601 EXPECT_FALSE(m2.Matches(1));
602}
603
604// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<T>.
605TEST(SafeMatcherCastTest, FromNonReferenceToReference) {
606 Matcher<int> m1 = Eq(0);
607 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
608 int n = 0;
609 EXPECT_TRUE(m2.Matches(n));
610 n = 1;
611 EXPECT_FALSE(m2.Matches(n));
612}
613
614// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<T>.
615TEST(SafeMatcherCastTest, FromSameType) {
616 Matcher<int> m1 = Eq(0);
617 Matcher<int> m2 = SafeMatcherCast<int>(m1);
618 EXPECT_TRUE(m2.Matches(0));
619 EXPECT_FALSE(m2.Matches(1));
620}
621
shiqiane35fdd92008-12-10 05:08:54 +0000622// Tests that A<T>() matches any value of type T.
623TEST(ATest, MatchesAnyValue) {
624 // Tests a matcher for a value type.
625 Matcher<double> m1 = A<double>();
626 EXPECT_TRUE(m1.Matches(91.43));
627 EXPECT_TRUE(m1.Matches(-15.32));
628
629 // Tests a matcher for a reference type.
630 int a = 2;
631 int b = -6;
632 Matcher<int&> m2 = A<int&>();
633 EXPECT_TRUE(m2.Matches(a));
634 EXPECT_TRUE(m2.Matches(b));
635}
636
637// Tests that A<T>() describes itself properly.
638TEST(ATest, CanDescribeSelf) {
639 EXPECT_EQ("is anything", Describe(A<bool>()));
640}
641
642// Tests that An<T>() matches any value of type T.
643TEST(AnTest, MatchesAnyValue) {
644 // Tests a matcher for a value type.
645 Matcher<int> m1 = An<int>();
646 EXPECT_TRUE(m1.Matches(9143));
647 EXPECT_TRUE(m1.Matches(-1532));
648
649 // Tests a matcher for a reference type.
650 int a = 2;
651 int b = -6;
652 Matcher<int&> m2 = An<int&>();
653 EXPECT_TRUE(m2.Matches(a));
654 EXPECT_TRUE(m2.Matches(b));
655}
656
657// Tests that An<T>() describes itself properly.
658TEST(AnTest, CanDescribeSelf) {
659 EXPECT_EQ("is anything", Describe(An<int>()));
660}
661
662// Tests that _ can be used as a matcher for any type and matches any
663// value of that type.
664TEST(UnderscoreTest, MatchesAnyValue) {
665 // Uses _ as a matcher for a value type.
666 Matcher<int> m1 = _;
667 EXPECT_TRUE(m1.Matches(123));
668 EXPECT_TRUE(m1.Matches(-242));
669
670 // Uses _ as a matcher for a reference type.
671 bool a = false;
672 const bool b = true;
673 Matcher<const bool&> m2 = _;
674 EXPECT_TRUE(m2.Matches(a));
675 EXPECT_TRUE(m2.Matches(b));
676}
677
678// Tests that _ describes itself properly.
679TEST(UnderscoreTest, CanDescribeSelf) {
680 Matcher<int> m = _;
681 EXPECT_EQ("is anything", Describe(m));
682}
683
684// Tests that Eq(x) matches any value equal to x.
685TEST(EqTest, MatchesEqualValue) {
686 // 2 C-strings with same content but different addresses.
687 const char a1[] = "hi";
688 const char a2[] = "hi";
689
690 Matcher<const char*> m1 = Eq(a1);
691 EXPECT_TRUE(m1.Matches(a1));
692 EXPECT_FALSE(m1.Matches(a2));
693}
694
695// Tests that Eq(v) describes itself properly.
696
697class Unprintable {
698 public:
699 Unprintable() : c_('a') {}
700
zhanyong.wan32de5f52009-12-23 00:13:23 +0000701 bool operator==(const Unprintable& /* rhs */) { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000702 private:
703 char c_;
704};
705
706TEST(EqTest, CanDescribeSelf) {
707 Matcher<Unprintable> m = Eq(Unprintable());
708 EXPECT_EQ("is equal to 1-byte object <61>", Describe(m));
709}
710
711// Tests that Eq(v) can be used to match any type that supports
712// comparing with type T, where T is v's type.
713TEST(EqTest, IsPolymorphic) {
714 Matcher<int> m1 = Eq(1);
715 EXPECT_TRUE(m1.Matches(1));
716 EXPECT_FALSE(m1.Matches(2));
717
718 Matcher<char> m2 = Eq(1);
719 EXPECT_TRUE(m2.Matches('\1'));
720 EXPECT_FALSE(m2.Matches('a'));
721}
722
723// Tests that TypedEq<T>(v) matches values of type T that's equal to v.
724TEST(TypedEqTest, ChecksEqualityForGivenType) {
725 Matcher<char> m1 = TypedEq<char>('a');
726 EXPECT_TRUE(m1.Matches('a'));
727 EXPECT_FALSE(m1.Matches('b'));
728
729 Matcher<int> m2 = TypedEq<int>(6);
730 EXPECT_TRUE(m2.Matches(6));
731 EXPECT_FALSE(m2.Matches(7));
732}
733
734// Tests that TypedEq(v) describes itself properly.
735TEST(TypedEqTest, CanDescribeSelf) {
736 EXPECT_EQ("is equal to 2", Describe(TypedEq<int>(2)));
737}
738
739// Tests that TypedEq<T>(v) has type Matcher<T>.
740
741// Type<T>::IsTypeOf(v) compiles iff the type of value v is T, where T
742// is a "bare" type (i.e. not in the form of const U or U&). If v's
743// type is not T, the compiler will generate a message about
744// "undefined referece".
745template <typename T>
746struct Type {
zhanyong.wan32de5f52009-12-23 00:13:23 +0000747 static bool IsTypeOf(const T& /* v */) { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000748
749 template <typename T2>
750 static void IsTypeOf(T2 v);
751};
752
753TEST(TypedEqTest, HasSpecifiedType) {
754 // Verfies that the type of TypedEq<T>(v) is Matcher<T>.
755 Type<Matcher<int> >::IsTypeOf(TypedEq<int>(5));
756 Type<Matcher<double> >::IsTypeOf(TypedEq<double>(5));
757}
758
759// Tests that Ge(v) matches anything >= v.
760TEST(GeTest, ImplementsGreaterThanOrEqual) {
761 Matcher<int> m1 = Ge(0);
762 EXPECT_TRUE(m1.Matches(1));
763 EXPECT_TRUE(m1.Matches(0));
764 EXPECT_FALSE(m1.Matches(-1));
765}
766
767// Tests that Ge(v) describes itself properly.
768TEST(GeTest, CanDescribeSelf) {
769 Matcher<int> m = Ge(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000770 EXPECT_EQ("is >= 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000771}
772
773// Tests that Gt(v) matches anything > v.
774TEST(GtTest, ImplementsGreaterThan) {
775 Matcher<double> m1 = Gt(0);
776 EXPECT_TRUE(m1.Matches(1.0));
777 EXPECT_FALSE(m1.Matches(0.0));
778 EXPECT_FALSE(m1.Matches(-1.0));
779}
780
781// Tests that Gt(v) describes itself properly.
782TEST(GtTest, CanDescribeSelf) {
783 Matcher<int> m = Gt(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000784 EXPECT_EQ("is > 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000785}
786
787// Tests that Le(v) matches anything <= v.
788TEST(LeTest, ImplementsLessThanOrEqual) {
789 Matcher<char> m1 = Le('b');
790 EXPECT_TRUE(m1.Matches('a'));
791 EXPECT_TRUE(m1.Matches('b'));
792 EXPECT_FALSE(m1.Matches('c'));
793}
794
795// Tests that Le(v) describes itself properly.
796TEST(LeTest, CanDescribeSelf) {
797 Matcher<int> m = Le(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000798 EXPECT_EQ("is <= 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000799}
800
801// Tests that Lt(v) matches anything < v.
802TEST(LtTest, ImplementsLessThan) {
803 Matcher<const string&> m1 = Lt("Hello");
804 EXPECT_TRUE(m1.Matches("Abc"));
805 EXPECT_FALSE(m1.Matches("Hello"));
806 EXPECT_FALSE(m1.Matches("Hello, world!"));
807}
808
809// Tests that Lt(v) describes itself properly.
810TEST(LtTest, CanDescribeSelf) {
811 Matcher<int> m = Lt(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000812 EXPECT_EQ("is < 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000813}
814
815// Tests that Ne(v) matches anything != v.
816TEST(NeTest, ImplementsNotEqual) {
817 Matcher<int> m1 = Ne(0);
818 EXPECT_TRUE(m1.Matches(1));
819 EXPECT_TRUE(m1.Matches(-1));
820 EXPECT_FALSE(m1.Matches(0));
821}
822
823// Tests that Ne(v) describes itself properly.
824TEST(NeTest, CanDescribeSelf) {
825 Matcher<int> m = Ne(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000826 EXPECT_EQ("isn't equal to 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000827}
828
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000829// Tests that IsNull() matches any NULL pointer of any type.
830TEST(IsNullTest, MatchesNullPointer) {
831 Matcher<int*> m1 = IsNull();
832 int* p1 = NULL;
833 int n = 0;
834 EXPECT_TRUE(m1.Matches(p1));
835 EXPECT_FALSE(m1.Matches(&n));
836
837 Matcher<const char*> m2 = IsNull();
838 const char* p2 = NULL;
839 EXPECT_TRUE(m2.Matches(p2));
840 EXPECT_FALSE(m2.Matches("hi"));
841
zhanyong.wan95b12332009-09-25 18:55:50 +0000842#if !GTEST_OS_SYMBIAN
843 // Nokia's Symbian compiler generates:
844 // gmock-matchers.h: ambiguous access to overloaded function
845 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(void *)'
846 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(const testing::
847 // MatcherInterface<void *> *)'
848 // gmock-matchers.h: (point of instantiation: 'testing::
849 // gmock_matchers_test::IsNullTest_MatchesNullPointer_Test::TestBody()')
850 // gmock-matchers.h: (instantiating: 'testing::PolymorphicMatc
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000851 Matcher<void*> m3 = IsNull();
852 void* p3 = NULL;
853 EXPECT_TRUE(m3.Matches(p3));
854 EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef)));
zhanyong.wan95b12332009-09-25 18:55:50 +0000855#endif
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000856}
857
vladlosev79b83502009-11-18 00:43:37 +0000858TEST(IsNullTest, LinkedPtr) {
859 const Matcher<linked_ptr<int> > m = IsNull();
860 const linked_ptr<int> null_p;
861 const linked_ptr<int> non_null_p(new int);
862
863 EXPECT_TRUE(m.Matches(null_p));
864 EXPECT_FALSE(m.Matches(non_null_p));
865}
866
867TEST(IsNullTest, ReferenceToConstLinkedPtr) {
868 const Matcher<const linked_ptr<double>&> m = IsNull();
869 const linked_ptr<double> null_p;
870 const linked_ptr<double> non_null_p(new double);
871
872 EXPECT_TRUE(m.Matches(null_p));
873 EXPECT_FALSE(m.Matches(non_null_p));
874}
875
vladloseve56daa72009-11-18 01:08:08 +0000876TEST(IsNullTest, ReferenceToConstScopedPtr) {
877 const Matcher<const scoped_ptr<double>&> m = IsNull();
878 const scoped_ptr<double> null_p;
879 const scoped_ptr<double> non_null_p(new double);
880
881 EXPECT_TRUE(m.Matches(null_p));
882 EXPECT_FALSE(m.Matches(non_null_p));
883}
884
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000885// Tests that IsNull() describes itself properly.
886TEST(IsNullTest, CanDescribeSelf) {
887 Matcher<int*> m = IsNull();
888 EXPECT_EQ("is NULL", Describe(m));
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000889 EXPECT_EQ("isn't NULL", DescribeNegation(m));
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000890}
891
shiqiane35fdd92008-12-10 05:08:54 +0000892// Tests that NotNull() matches any non-NULL pointer of any type.
893TEST(NotNullTest, MatchesNonNullPointer) {
894 Matcher<int*> m1 = NotNull();
895 int* p1 = NULL;
896 int n = 0;
897 EXPECT_FALSE(m1.Matches(p1));
898 EXPECT_TRUE(m1.Matches(&n));
899
900 Matcher<const char*> m2 = NotNull();
901 const char* p2 = NULL;
902 EXPECT_FALSE(m2.Matches(p2));
903 EXPECT_TRUE(m2.Matches("hi"));
904}
905
vladlosev79b83502009-11-18 00:43:37 +0000906TEST(NotNullTest, LinkedPtr) {
907 const Matcher<linked_ptr<int> > m = NotNull();
908 const linked_ptr<int> null_p;
909 const linked_ptr<int> non_null_p(new int);
910
911 EXPECT_FALSE(m.Matches(null_p));
912 EXPECT_TRUE(m.Matches(non_null_p));
913}
914
915TEST(NotNullTest, ReferenceToConstLinkedPtr) {
916 const Matcher<const linked_ptr<double>&> m = NotNull();
917 const linked_ptr<double> null_p;
918 const linked_ptr<double> non_null_p(new double);
919
920 EXPECT_FALSE(m.Matches(null_p));
921 EXPECT_TRUE(m.Matches(non_null_p));
922}
923
vladloseve56daa72009-11-18 01:08:08 +0000924TEST(NotNullTest, ReferenceToConstScopedPtr) {
925 const Matcher<const scoped_ptr<double>&> m = NotNull();
926 const scoped_ptr<double> null_p;
927 const scoped_ptr<double> non_null_p(new double);
928
929 EXPECT_FALSE(m.Matches(null_p));
930 EXPECT_TRUE(m.Matches(non_null_p));
931}
932
shiqiane35fdd92008-12-10 05:08:54 +0000933// Tests that NotNull() describes itself properly.
934TEST(NotNullTest, CanDescribeSelf) {
935 Matcher<int*> m = NotNull();
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000936 EXPECT_EQ("isn't NULL", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000937}
938
939// Tests that Ref(variable) matches an argument that references
940// 'variable'.
941TEST(RefTest, MatchesSameVariable) {
942 int a = 0;
943 int b = 0;
944 Matcher<int&> m = Ref(a);
945 EXPECT_TRUE(m.Matches(a));
946 EXPECT_FALSE(m.Matches(b));
947}
948
949// Tests that Ref(variable) describes itself properly.
950TEST(RefTest, CanDescribeSelf) {
951 int n = 5;
952 Matcher<int&> m = Ref(n);
953 stringstream ss;
954 ss << "references the variable @" << &n << " 5";
955 EXPECT_EQ(string(ss.str()), Describe(m));
956}
957
958// Test that Ref(non_const_varialbe) can be used as a matcher for a
959// const reference.
960TEST(RefTest, CanBeUsedAsMatcherForConstReference) {
961 int a = 0;
962 int b = 0;
963 Matcher<const int&> m = Ref(a);
964 EXPECT_TRUE(m.Matches(a));
965 EXPECT_FALSE(m.Matches(b));
966}
967
968// Tests that Ref(variable) is covariant, i.e. Ref(derived) can be
969// used wherever Ref(base) can be used (Ref(derived) is a sub-type
970// of Ref(base), but not vice versa.
971
shiqiane35fdd92008-12-10 05:08:54 +0000972TEST(RefTest, IsCovariant) {
973 Base base, base2;
974 Derived derived;
975 Matcher<const Base&> m1 = Ref(base);
976 EXPECT_TRUE(m1.Matches(base));
977 EXPECT_FALSE(m1.Matches(base2));
978 EXPECT_FALSE(m1.Matches(derived));
979
980 m1 = Ref(derived);
981 EXPECT_TRUE(m1.Matches(derived));
982 EXPECT_FALSE(m1.Matches(base));
983 EXPECT_FALSE(m1.Matches(base2));
984}
985
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000986TEST(RefTest, ExplainsResult) {
987 int n = 0;
988 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), n),
989 StartsWith("which is located @"));
990
991 int m = 0;
992 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), m),
993 StartsWith("which is located @"));
994}
995
shiqiane35fdd92008-12-10 05:08:54 +0000996// Tests string comparison matchers.
997
998TEST(StrEqTest, MatchesEqualString) {
999 Matcher<const char*> m = StrEq(string("Hello"));
1000 EXPECT_TRUE(m.Matches("Hello"));
1001 EXPECT_FALSE(m.Matches("hello"));
1002 EXPECT_FALSE(m.Matches(NULL));
1003
1004 Matcher<const string&> m2 = StrEq("Hello");
1005 EXPECT_TRUE(m2.Matches("Hello"));
1006 EXPECT_FALSE(m2.Matches("Hi"));
1007}
1008
1009TEST(StrEqTest, CanDescribeSelf) {
1010 Matcher<string> m = StrEq("Hi-\'\"\?\\\a\b\f\n\r\t\v\xD3");
1011 EXPECT_EQ("is equal to \"Hi-\'\\\"\\?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",
1012 Describe(m));
1013
1014 string str("01204500800");
1015 str[3] = '\0';
1016 Matcher<string> m2 = StrEq(str);
1017 EXPECT_EQ("is equal to \"012\\04500800\"", Describe(m2));
1018 str[0] = str[6] = str[7] = str[9] = str[10] = '\0';
1019 Matcher<string> m3 = StrEq(str);
1020 EXPECT_EQ("is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3));
1021}
1022
1023TEST(StrNeTest, MatchesUnequalString) {
1024 Matcher<const char*> m = StrNe("Hello");
1025 EXPECT_TRUE(m.Matches(""));
1026 EXPECT_TRUE(m.Matches(NULL));
1027 EXPECT_FALSE(m.Matches("Hello"));
1028
1029 Matcher<string> m2 = StrNe(string("Hello"));
1030 EXPECT_TRUE(m2.Matches("hello"));
1031 EXPECT_FALSE(m2.Matches("Hello"));
1032}
1033
1034TEST(StrNeTest, CanDescribeSelf) {
1035 Matcher<const char*> m = StrNe("Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001036 EXPECT_EQ("isn't equal to \"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001037}
1038
1039TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
1040 Matcher<const char*> m = StrCaseEq(string("Hello"));
1041 EXPECT_TRUE(m.Matches("Hello"));
1042 EXPECT_TRUE(m.Matches("hello"));
1043 EXPECT_FALSE(m.Matches("Hi"));
1044 EXPECT_FALSE(m.Matches(NULL));
1045
1046 Matcher<const string&> m2 = StrCaseEq("Hello");
1047 EXPECT_TRUE(m2.Matches("hello"));
1048 EXPECT_FALSE(m2.Matches("Hi"));
1049}
1050
1051TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1052 string str1("oabocdooeoo");
1053 string str2("OABOCDOOEOO");
1054 Matcher<const string&> m0 = StrCaseEq(str1);
1055 EXPECT_FALSE(m0.Matches(str2 + string(1, '\0')));
1056
1057 str1[3] = str2[3] = '\0';
1058 Matcher<const string&> m1 = StrCaseEq(str1);
1059 EXPECT_TRUE(m1.Matches(str2));
1060
1061 str1[0] = str1[6] = str1[7] = str1[10] = '\0';
1062 str2[0] = str2[6] = str2[7] = str2[10] = '\0';
1063 Matcher<const string&> m2 = StrCaseEq(str1);
1064 str1[9] = str2[9] = '\0';
1065 EXPECT_FALSE(m2.Matches(str2));
1066
1067 Matcher<const string&> m3 = StrCaseEq(str1);
1068 EXPECT_TRUE(m3.Matches(str2));
1069
1070 EXPECT_FALSE(m3.Matches(str2 + "x"));
1071 str2.append(1, '\0');
1072 EXPECT_FALSE(m3.Matches(str2));
1073 EXPECT_FALSE(m3.Matches(string(str2, 0, 9)));
1074}
1075
1076TEST(StrCaseEqTest, CanDescribeSelf) {
1077 Matcher<string> m = StrCaseEq("Hi");
1078 EXPECT_EQ("is equal to (ignoring case) \"Hi\"", Describe(m));
1079}
1080
1081TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1082 Matcher<const char*> m = StrCaseNe("Hello");
1083 EXPECT_TRUE(m.Matches("Hi"));
1084 EXPECT_TRUE(m.Matches(NULL));
1085 EXPECT_FALSE(m.Matches("Hello"));
1086 EXPECT_FALSE(m.Matches("hello"));
1087
1088 Matcher<string> m2 = StrCaseNe(string("Hello"));
1089 EXPECT_TRUE(m2.Matches(""));
1090 EXPECT_FALSE(m2.Matches("Hello"));
1091}
1092
1093TEST(StrCaseNeTest, CanDescribeSelf) {
1094 Matcher<const char*> m = StrCaseNe("Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001095 EXPECT_EQ("isn't equal to (ignoring case) \"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001096}
1097
1098// Tests that HasSubstr() works for matching string-typed values.
1099TEST(HasSubstrTest, WorksForStringClasses) {
1100 const Matcher<string> m1 = HasSubstr("foo");
1101 EXPECT_TRUE(m1.Matches(string("I love food.")));
1102 EXPECT_FALSE(m1.Matches(string("tofo")));
1103
1104 const Matcher<const std::string&> m2 = HasSubstr("foo");
1105 EXPECT_TRUE(m2.Matches(std::string("I love food.")));
1106 EXPECT_FALSE(m2.Matches(std::string("tofo")));
1107}
1108
1109// Tests that HasSubstr() works for matching C-string-typed values.
1110TEST(HasSubstrTest, WorksForCStrings) {
1111 const Matcher<char*> m1 = HasSubstr("foo");
1112 EXPECT_TRUE(m1.Matches(const_cast<char*>("I love food.")));
1113 EXPECT_FALSE(m1.Matches(const_cast<char*>("tofo")));
1114 EXPECT_FALSE(m1.Matches(NULL));
1115
1116 const Matcher<const char*> m2 = HasSubstr("foo");
1117 EXPECT_TRUE(m2.Matches("I love food."));
1118 EXPECT_FALSE(m2.Matches("tofo"));
1119 EXPECT_FALSE(m2.Matches(NULL));
1120}
1121
1122// Tests that HasSubstr(s) describes itself properly.
1123TEST(HasSubstrTest, CanDescribeSelf) {
1124 Matcher<string> m = HasSubstr("foo\n\"");
1125 EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m));
1126}
1127
zhanyong.wanb5937da2009-07-16 20:26:41 +00001128TEST(KeyTest, CanDescribeSelf) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001129 Matcher<const pair<std::string, int>&> m = Key("foo");
zhanyong.wanb5937da2009-07-16 20:26:41 +00001130 EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001131 EXPECT_EQ("doesn't have a key that is equal to \"foo\"", DescribeNegation(m));
1132}
1133
1134TEST(KeyTest, ExplainsResult) {
1135 Matcher<pair<int, bool> > m = Key(GreaterThan(10));
1136 EXPECT_EQ("whose first field is a value which is 5 less than 10",
1137 Explain(m, make_pair(5, true)));
1138 EXPECT_EQ("whose first field is a value which is 5 more than 10",
1139 Explain(m, make_pair(15, true)));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001140}
1141
1142TEST(KeyTest, MatchesCorrectly) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001143 pair<int, std::string> p(25, "foo");
zhanyong.wanb5937da2009-07-16 20:26:41 +00001144 EXPECT_THAT(p, Key(25));
1145 EXPECT_THAT(p, Not(Key(42)));
1146 EXPECT_THAT(p, Key(Ge(20)));
1147 EXPECT_THAT(p, Not(Key(Lt(25))));
1148}
1149
1150TEST(KeyTest, SafelyCastsInnerMatcher) {
1151 Matcher<int> is_positive = Gt(0);
1152 Matcher<int> is_negative = Lt(0);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001153 pair<char, bool> p('a', true);
zhanyong.wanb5937da2009-07-16 20:26:41 +00001154 EXPECT_THAT(p, Key(is_positive));
1155 EXPECT_THAT(p, Not(Key(is_negative)));
1156}
1157
1158TEST(KeyTest, InsideContainsUsingMap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001159 map<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001160 container.insert(make_pair(1, 'a'));
1161 container.insert(make_pair(2, 'b'));
1162 container.insert(make_pair(4, 'c'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001163 EXPECT_THAT(container, Contains(Key(1)));
1164 EXPECT_THAT(container, Not(Contains(Key(3))));
1165}
1166
1167TEST(KeyTest, InsideContainsUsingMultimap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001168 multimap<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001169 container.insert(make_pair(1, 'a'));
1170 container.insert(make_pair(2, 'b'));
1171 container.insert(make_pair(4, 'c'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001172
1173 EXPECT_THAT(container, Not(Contains(Key(25))));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001174 container.insert(make_pair(25, 'd'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001175 EXPECT_THAT(container, Contains(Key(25)));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001176 container.insert(make_pair(25, 'e'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001177 EXPECT_THAT(container, Contains(Key(25)));
1178
1179 EXPECT_THAT(container, Contains(Key(1)));
1180 EXPECT_THAT(container, Not(Contains(Key(3))));
1181}
1182
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001183TEST(PairTest, Typing) {
1184 // Test verifies the following type conversions can be compiled.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001185 Matcher<const pair<const char*, int>&> m1 = Pair("foo", 42);
1186 Matcher<const pair<const char*, int> > m2 = Pair("foo", 42);
1187 Matcher<pair<const char*, int> > m3 = Pair("foo", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001188
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001189 Matcher<pair<int, const std::string> > m4 = Pair(25, "42");
1190 Matcher<pair<const std::string, int> > m5 = Pair("25", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001191}
1192
1193TEST(PairTest, CanDescribeSelf) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001194 Matcher<const pair<std::string, int>&> m1 = Pair("foo", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001195 EXPECT_EQ("has a first field that is equal to \"foo\""
1196 ", and has a second field that is equal to 42",
1197 Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001198 EXPECT_EQ("has a first field that isn't equal to \"foo\""
1199 ", or has a second field that isn't equal to 42",
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001200 DescribeNegation(m1));
1201 // Double and triple negation (1 or 2 times not and description of negation).
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001202 Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42));
1203 EXPECT_EQ("has a first field that isn't equal to 13"
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001204 ", and has a second field that is equal to 42",
1205 DescribeNegation(m2));
1206}
1207
1208TEST(PairTest, CanExplainMatchResultTo) {
zhanyong.wan82113312010-01-08 21:55:40 +00001209 // If neither field matches, Pair() should explain about the first
1210 // field.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001211 const Matcher<pair<int, int> > m = Pair(GreaterThan(0), GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001212 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001213 Explain(m, make_pair(-1, -2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001214
zhanyong.wan82113312010-01-08 21:55:40 +00001215 // If the first field matches but the second doesn't, Pair() should
1216 // explain about the second field.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001217 EXPECT_EQ("whose second field does not match, which is 2 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001218 Explain(m, make_pair(1, -2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001219
zhanyong.wan82113312010-01-08 21:55:40 +00001220 // If the first field doesn't match but the second does, Pair()
1221 // should explain about the first field.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001222 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001223 Explain(m, make_pair(-1, 2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001224
zhanyong.wan82113312010-01-08 21:55:40 +00001225 // If both fields match, Pair() should explain about them both.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001226 EXPECT_EQ("whose both fields match, where the first field is a value "
1227 "which is 1 more than 0, and the second field is a value "
1228 "which is 2 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001229 Explain(m, make_pair(1, 2)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001230
1231 // If only the first match has an explanation, only this explanation should
1232 // be printed.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001233 const Matcher<pair<int, int> > explain_first = Pair(GreaterThan(0), 0);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001234 EXPECT_EQ("whose both fields match, where the first field is a value "
1235 "which is 1 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001236 Explain(explain_first, make_pair(1, 0)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001237
1238 // If only the second match has an explanation, only this explanation should
1239 // be printed.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001240 const Matcher<pair<int, int> > explain_second = Pair(0, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001241 EXPECT_EQ("whose both fields match, where the second field is a value "
1242 "which is 1 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001243 Explain(explain_second, make_pair(0, 1)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001244}
1245
1246TEST(PairTest, MatchesCorrectly) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001247 pair<int, std::string> p(25, "foo");
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001248
1249 // Both fields match.
1250 EXPECT_THAT(p, Pair(25, "foo"));
1251 EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o")));
1252
1253 // 'first' doesnt' match, but 'second' matches.
1254 EXPECT_THAT(p, Not(Pair(42, "foo")));
1255 EXPECT_THAT(p, Not(Pair(Lt(25), "foo")));
1256
1257 // 'first' matches, but 'second' doesn't match.
1258 EXPECT_THAT(p, Not(Pair(25, "bar")));
1259 EXPECT_THAT(p, Not(Pair(25, Not("foo"))));
1260
1261 // Neither field matches.
1262 EXPECT_THAT(p, Not(Pair(13, "bar")));
1263 EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a"))));
1264}
1265
1266TEST(PairTest, SafelyCastsInnerMatchers) {
1267 Matcher<int> is_positive = Gt(0);
1268 Matcher<int> is_negative = Lt(0);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001269 pair<char, bool> p('a', true);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001270 EXPECT_THAT(p, Pair(is_positive, _));
1271 EXPECT_THAT(p, Not(Pair(is_negative, _)));
1272 EXPECT_THAT(p, Pair(_, is_positive));
1273 EXPECT_THAT(p, Not(Pair(_, is_negative)));
1274}
1275
1276TEST(PairTest, InsideContainsUsingMap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001277 map<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001278 container.insert(make_pair(1, 'a'));
1279 container.insert(make_pair(2, 'b'));
1280 container.insert(make_pair(4, 'c'));
zhanyong.wan95b12332009-09-25 18:55:50 +00001281 EXPECT_THAT(container, Contains(Pair(1, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001282 EXPECT_THAT(container, Contains(Pair(1, _)));
zhanyong.wan95b12332009-09-25 18:55:50 +00001283 EXPECT_THAT(container, Contains(Pair(_, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001284 EXPECT_THAT(container, Not(Contains(Pair(3, _))));
1285}
1286
shiqiane35fdd92008-12-10 05:08:54 +00001287// Tests StartsWith(s).
1288
1289TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
1290 const Matcher<const char*> m1 = StartsWith(string(""));
1291 EXPECT_TRUE(m1.Matches("Hi"));
1292 EXPECT_TRUE(m1.Matches(""));
1293 EXPECT_FALSE(m1.Matches(NULL));
1294
1295 const Matcher<const string&> m2 = StartsWith("Hi");
1296 EXPECT_TRUE(m2.Matches("Hi"));
1297 EXPECT_TRUE(m2.Matches("Hi Hi!"));
1298 EXPECT_TRUE(m2.Matches("High"));
1299 EXPECT_FALSE(m2.Matches("H"));
1300 EXPECT_FALSE(m2.Matches(" Hi"));
1301}
1302
1303TEST(StartsWithTest, CanDescribeSelf) {
1304 Matcher<const std::string> m = StartsWith("Hi");
1305 EXPECT_EQ("starts with \"Hi\"", Describe(m));
1306}
1307
1308// Tests EndsWith(s).
1309
1310TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
1311 const Matcher<const char*> m1 = EndsWith("");
1312 EXPECT_TRUE(m1.Matches("Hi"));
1313 EXPECT_TRUE(m1.Matches(""));
1314 EXPECT_FALSE(m1.Matches(NULL));
1315
1316 const Matcher<const string&> m2 = EndsWith(string("Hi"));
1317 EXPECT_TRUE(m2.Matches("Hi"));
1318 EXPECT_TRUE(m2.Matches("Wow Hi Hi"));
1319 EXPECT_TRUE(m2.Matches("Super Hi"));
1320 EXPECT_FALSE(m2.Matches("i"));
1321 EXPECT_FALSE(m2.Matches("Hi "));
1322}
1323
1324TEST(EndsWithTest, CanDescribeSelf) {
1325 Matcher<const std::string> m = EndsWith("Hi");
1326 EXPECT_EQ("ends with \"Hi\"", Describe(m));
1327}
1328
shiqiane35fdd92008-12-10 05:08:54 +00001329// Tests MatchesRegex().
1330
1331TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
1332 const Matcher<const char*> m1 = MatchesRegex("a.*z");
1333 EXPECT_TRUE(m1.Matches("az"));
1334 EXPECT_TRUE(m1.Matches("abcz"));
1335 EXPECT_FALSE(m1.Matches(NULL));
1336
1337 const Matcher<const string&> m2 = MatchesRegex(new RE("a.*z"));
1338 EXPECT_TRUE(m2.Matches("azbz"));
1339 EXPECT_FALSE(m2.Matches("az1"));
1340 EXPECT_FALSE(m2.Matches("1az"));
1341}
1342
1343TEST(MatchesRegexTest, CanDescribeSelf) {
1344 Matcher<const std::string> m1 = MatchesRegex(string("Hi.*"));
1345 EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1));
1346
zhanyong.wand14aaed2010-01-14 05:36:32 +00001347 Matcher<const char*> m2 = MatchesRegex(new RE("a.*"));
1348 EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +00001349}
1350
1351// Tests ContainsRegex().
1352
1353TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
1354 const Matcher<const char*> m1 = ContainsRegex(string("a.*z"));
1355 EXPECT_TRUE(m1.Matches("az"));
1356 EXPECT_TRUE(m1.Matches("0abcz1"));
1357 EXPECT_FALSE(m1.Matches(NULL));
1358
1359 const Matcher<const string&> m2 = ContainsRegex(new RE("a.*z"));
1360 EXPECT_TRUE(m2.Matches("azbz"));
1361 EXPECT_TRUE(m2.Matches("az1"));
1362 EXPECT_FALSE(m2.Matches("1a"));
1363}
1364
1365TEST(ContainsRegexTest, CanDescribeSelf) {
1366 Matcher<const std::string> m1 = ContainsRegex("Hi.*");
1367 EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1));
1368
zhanyong.wand14aaed2010-01-14 05:36:32 +00001369 Matcher<const char*> m2 = ContainsRegex(new RE("a.*"));
1370 EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +00001371}
shiqiane35fdd92008-12-10 05:08:54 +00001372
1373// Tests for wide strings.
1374#if GTEST_HAS_STD_WSTRING
1375TEST(StdWideStrEqTest, MatchesEqual) {
1376 Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello"));
1377 EXPECT_TRUE(m.Matches(L"Hello"));
1378 EXPECT_FALSE(m.Matches(L"hello"));
1379 EXPECT_FALSE(m.Matches(NULL));
1380
1381 Matcher<const ::std::wstring&> m2 = StrEq(L"Hello");
1382 EXPECT_TRUE(m2.Matches(L"Hello"));
1383 EXPECT_FALSE(m2.Matches(L"Hi"));
1384
1385 Matcher<const ::std::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1386 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1387 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1388
1389 ::std::wstring str(L"01204500800");
1390 str[3] = L'\0';
1391 Matcher<const ::std::wstring&> m4 = StrEq(str);
1392 EXPECT_TRUE(m4.Matches(str));
1393 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1394 Matcher<const ::std::wstring&> m5 = StrEq(str);
1395 EXPECT_TRUE(m5.Matches(str));
1396}
1397
1398TEST(StdWideStrEqTest, CanDescribeSelf) {
1399 Matcher< ::std::wstring> m = StrEq(L"Hi-\'\"\?\\\a\b\f\n\r\t\v");
1400 EXPECT_EQ("is equal to L\"Hi-\'\\\"\\?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
1401 Describe(m));
1402
1403 Matcher< ::std::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1404 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1405 Describe(m2));
1406
1407 ::std::wstring str(L"01204500800");
1408 str[3] = L'\0';
1409 Matcher<const ::std::wstring&> m4 = StrEq(str);
1410 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1411 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1412 Matcher<const ::std::wstring&> m5 = StrEq(str);
1413 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1414}
1415
1416TEST(StdWideStrNeTest, MatchesUnequalString) {
1417 Matcher<const wchar_t*> m = StrNe(L"Hello");
1418 EXPECT_TRUE(m.Matches(L""));
1419 EXPECT_TRUE(m.Matches(NULL));
1420 EXPECT_FALSE(m.Matches(L"Hello"));
1421
1422 Matcher< ::std::wstring> m2 = StrNe(::std::wstring(L"Hello"));
1423 EXPECT_TRUE(m2.Matches(L"hello"));
1424 EXPECT_FALSE(m2.Matches(L"Hello"));
1425}
1426
1427TEST(StdWideStrNeTest, CanDescribeSelf) {
1428 Matcher<const wchar_t*> m = StrNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001429 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001430}
1431
1432TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1433 Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello"));
1434 EXPECT_TRUE(m.Matches(L"Hello"));
1435 EXPECT_TRUE(m.Matches(L"hello"));
1436 EXPECT_FALSE(m.Matches(L"Hi"));
1437 EXPECT_FALSE(m.Matches(NULL));
1438
1439 Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello");
1440 EXPECT_TRUE(m2.Matches(L"hello"));
1441 EXPECT_FALSE(m2.Matches(L"Hi"));
1442}
1443
1444TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1445 ::std::wstring str1(L"oabocdooeoo");
1446 ::std::wstring str2(L"OABOCDOOEOO");
1447 Matcher<const ::std::wstring&> m0 = StrCaseEq(str1);
1448 EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L'\0')));
1449
1450 str1[3] = str2[3] = L'\0';
1451 Matcher<const ::std::wstring&> m1 = StrCaseEq(str1);
1452 EXPECT_TRUE(m1.Matches(str2));
1453
1454 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1455 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1456 Matcher<const ::std::wstring&> m2 = StrCaseEq(str1);
1457 str1[9] = str2[9] = L'\0';
1458 EXPECT_FALSE(m2.Matches(str2));
1459
1460 Matcher<const ::std::wstring&> m3 = StrCaseEq(str1);
1461 EXPECT_TRUE(m3.Matches(str2));
1462
1463 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1464 str2.append(1, L'\0');
1465 EXPECT_FALSE(m3.Matches(str2));
1466 EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9)));
1467}
1468
1469TEST(StdWideStrCaseEqTest, CanDescribeSelf) {
1470 Matcher< ::std::wstring> m = StrCaseEq(L"Hi");
1471 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1472}
1473
1474TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1475 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1476 EXPECT_TRUE(m.Matches(L"Hi"));
1477 EXPECT_TRUE(m.Matches(NULL));
1478 EXPECT_FALSE(m.Matches(L"Hello"));
1479 EXPECT_FALSE(m.Matches(L"hello"));
1480
1481 Matcher< ::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello"));
1482 EXPECT_TRUE(m2.Matches(L""));
1483 EXPECT_FALSE(m2.Matches(L"Hello"));
1484}
1485
1486TEST(StdWideStrCaseNeTest, CanDescribeSelf) {
1487 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001488 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001489}
1490
1491// Tests that HasSubstr() works for matching wstring-typed values.
1492TEST(StdWideHasSubstrTest, WorksForStringClasses) {
1493 const Matcher< ::std::wstring> m1 = HasSubstr(L"foo");
1494 EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food.")));
1495 EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo")));
1496
1497 const Matcher<const ::std::wstring&> m2 = HasSubstr(L"foo");
1498 EXPECT_TRUE(m2.Matches(::std::wstring(L"I love food.")));
1499 EXPECT_FALSE(m2.Matches(::std::wstring(L"tofo")));
1500}
1501
1502// Tests that HasSubstr() works for matching C-wide-string-typed values.
1503TEST(StdWideHasSubstrTest, WorksForCStrings) {
1504 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1505 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1506 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1507 EXPECT_FALSE(m1.Matches(NULL));
1508
1509 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1510 EXPECT_TRUE(m2.Matches(L"I love food."));
1511 EXPECT_FALSE(m2.Matches(L"tofo"));
1512 EXPECT_FALSE(m2.Matches(NULL));
1513}
1514
1515// Tests that HasSubstr(s) describes itself properly.
1516TEST(StdWideHasSubstrTest, CanDescribeSelf) {
1517 Matcher< ::std::wstring> m = HasSubstr(L"foo\n\"");
1518 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1519}
1520
1521// Tests StartsWith(s).
1522
1523TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) {
1524 const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L""));
1525 EXPECT_TRUE(m1.Matches(L"Hi"));
1526 EXPECT_TRUE(m1.Matches(L""));
1527 EXPECT_FALSE(m1.Matches(NULL));
1528
1529 const Matcher<const ::std::wstring&> m2 = StartsWith(L"Hi");
1530 EXPECT_TRUE(m2.Matches(L"Hi"));
1531 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1532 EXPECT_TRUE(m2.Matches(L"High"));
1533 EXPECT_FALSE(m2.Matches(L"H"));
1534 EXPECT_FALSE(m2.Matches(L" Hi"));
1535}
1536
1537TEST(StdWideStartsWithTest, CanDescribeSelf) {
1538 Matcher<const ::std::wstring> m = StartsWith(L"Hi");
1539 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1540}
1541
1542// Tests EndsWith(s).
1543
1544TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) {
1545 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1546 EXPECT_TRUE(m1.Matches(L"Hi"));
1547 EXPECT_TRUE(m1.Matches(L""));
1548 EXPECT_FALSE(m1.Matches(NULL));
1549
1550 const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L"Hi"));
1551 EXPECT_TRUE(m2.Matches(L"Hi"));
1552 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1553 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1554 EXPECT_FALSE(m2.Matches(L"i"));
1555 EXPECT_FALSE(m2.Matches(L"Hi "));
1556}
1557
1558TEST(StdWideEndsWithTest, CanDescribeSelf) {
1559 Matcher<const ::std::wstring> m = EndsWith(L"Hi");
1560 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1561}
1562
1563#endif // GTEST_HAS_STD_WSTRING
1564
1565#if GTEST_HAS_GLOBAL_WSTRING
1566TEST(GlobalWideStrEqTest, MatchesEqual) {
1567 Matcher<const wchar_t*> m = StrEq(::wstring(L"Hello"));
1568 EXPECT_TRUE(m.Matches(L"Hello"));
1569 EXPECT_FALSE(m.Matches(L"hello"));
1570 EXPECT_FALSE(m.Matches(NULL));
1571
1572 Matcher<const ::wstring&> m2 = StrEq(L"Hello");
1573 EXPECT_TRUE(m2.Matches(L"Hello"));
1574 EXPECT_FALSE(m2.Matches(L"Hi"));
1575
1576 Matcher<const ::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1577 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1578 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1579
1580 ::wstring str(L"01204500800");
1581 str[3] = L'\0';
1582 Matcher<const ::wstring&> m4 = StrEq(str);
1583 EXPECT_TRUE(m4.Matches(str));
1584 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1585 Matcher<const ::wstring&> m5 = StrEq(str);
1586 EXPECT_TRUE(m5.Matches(str));
1587}
1588
1589TEST(GlobalWideStrEqTest, CanDescribeSelf) {
1590 Matcher< ::wstring> m = StrEq(L"Hi-\'\"\?\\\a\b\f\n\r\t\v");
1591 EXPECT_EQ("is equal to L\"Hi-\'\\\"\\?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
1592 Describe(m));
1593
1594 Matcher< ::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1595 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1596 Describe(m2));
1597
1598 ::wstring str(L"01204500800");
1599 str[3] = L'\0';
1600 Matcher<const ::wstring&> m4 = StrEq(str);
1601 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1602 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1603 Matcher<const ::wstring&> m5 = StrEq(str);
1604 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1605}
1606
1607TEST(GlobalWideStrNeTest, MatchesUnequalString) {
1608 Matcher<const wchar_t*> m = StrNe(L"Hello");
1609 EXPECT_TRUE(m.Matches(L""));
1610 EXPECT_TRUE(m.Matches(NULL));
1611 EXPECT_FALSE(m.Matches(L"Hello"));
1612
1613 Matcher< ::wstring> m2 = StrNe(::wstring(L"Hello"));
1614 EXPECT_TRUE(m2.Matches(L"hello"));
1615 EXPECT_FALSE(m2.Matches(L"Hello"));
1616}
1617
1618TEST(GlobalWideStrNeTest, CanDescribeSelf) {
1619 Matcher<const wchar_t*> m = StrNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001620 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001621}
1622
1623TEST(GlobalWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1624 Matcher<const wchar_t*> m = StrCaseEq(::wstring(L"Hello"));
1625 EXPECT_TRUE(m.Matches(L"Hello"));
1626 EXPECT_TRUE(m.Matches(L"hello"));
1627 EXPECT_FALSE(m.Matches(L"Hi"));
1628 EXPECT_FALSE(m.Matches(NULL));
1629
1630 Matcher<const ::wstring&> m2 = StrCaseEq(L"Hello");
1631 EXPECT_TRUE(m2.Matches(L"hello"));
1632 EXPECT_FALSE(m2.Matches(L"Hi"));
1633}
1634
1635TEST(GlobalWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1636 ::wstring str1(L"oabocdooeoo");
1637 ::wstring str2(L"OABOCDOOEOO");
1638 Matcher<const ::wstring&> m0 = StrCaseEq(str1);
1639 EXPECT_FALSE(m0.Matches(str2 + ::wstring(1, L'\0')));
1640
1641 str1[3] = str2[3] = L'\0';
1642 Matcher<const ::wstring&> m1 = StrCaseEq(str1);
1643 EXPECT_TRUE(m1.Matches(str2));
1644
1645 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1646 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1647 Matcher<const ::wstring&> m2 = StrCaseEq(str1);
1648 str1[9] = str2[9] = L'\0';
1649 EXPECT_FALSE(m2.Matches(str2));
1650
1651 Matcher<const ::wstring&> m3 = StrCaseEq(str1);
1652 EXPECT_TRUE(m3.Matches(str2));
1653
1654 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1655 str2.append(1, L'\0');
1656 EXPECT_FALSE(m3.Matches(str2));
1657 EXPECT_FALSE(m3.Matches(::wstring(str2, 0, 9)));
1658}
1659
1660TEST(GlobalWideStrCaseEqTest, CanDescribeSelf) {
1661 Matcher< ::wstring> m = StrCaseEq(L"Hi");
1662 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1663}
1664
1665TEST(GlobalWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1666 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1667 EXPECT_TRUE(m.Matches(L"Hi"));
1668 EXPECT_TRUE(m.Matches(NULL));
1669 EXPECT_FALSE(m.Matches(L"Hello"));
1670 EXPECT_FALSE(m.Matches(L"hello"));
1671
1672 Matcher< ::wstring> m2 = StrCaseNe(::wstring(L"Hello"));
1673 EXPECT_TRUE(m2.Matches(L""));
1674 EXPECT_FALSE(m2.Matches(L"Hello"));
1675}
1676
1677TEST(GlobalWideStrCaseNeTest, CanDescribeSelf) {
1678 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001679 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001680}
1681
1682// Tests that HasSubstr() works for matching wstring-typed values.
1683TEST(GlobalWideHasSubstrTest, WorksForStringClasses) {
1684 const Matcher< ::wstring> m1 = HasSubstr(L"foo");
1685 EXPECT_TRUE(m1.Matches(::wstring(L"I love food.")));
1686 EXPECT_FALSE(m1.Matches(::wstring(L"tofo")));
1687
1688 const Matcher<const ::wstring&> m2 = HasSubstr(L"foo");
1689 EXPECT_TRUE(m2.Matches(::wstring(L"I love food.")));
1690 EXPECT_FALSE(m2.Matches(::wstring(L"tofo")));
1691}
1692
1693// Tests that HasSubstr() works for matching C-wide-string-typed values.
1694TEST(GlobalWideHasSubstrTest, WorksForCStrings) {
1695 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1696 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1697 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1698 EXPECT_FALSE(m1.Matches(NULL));
1699
1700 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1701 EXPECT_TRUE(m2.Matches(L"I love food."));
1702 EXPECT_FALSE(m2.Matches(L"tofo"));
1703 EXPECT_FALSE(m2.Matches(NULL));
1704}
1705
1706// Tests that HasSubstr(s) describes itself properly.
1707TEST(GlobalWideHasSubstrTest, CanDescribeSelf) {
1708 Matcher< ::wstring> m = HasSubstr(L"foo\n\"");
1709 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1710}
1711
1712// Tests StartsWith(s).
1713
1714TEST(GlobalWideStartsWithTest, MatchesStringWithGivenPrefix) {
1715 const Matcher<const wchar_t*> m1 = StartsWith(::wstring(L""));
1716 EXPECT_TRUE(m1.Matches(L"Hi"));
1717 EXPECT_TRUE(m1.Matches(L""));
1718 EXPECT_FALSE(m1.Matches(NULL));
1719
1720 const Matcher<const ::wstring&> m2 = StartsWith(L"Hi");
1721 EXPECT_TRUE(m2.Matches(L"Hi"));
1722 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1723 EXPECT_TRUE(m2.Matches(L"High"));
1724 EXPECT_FALSE(m2.Matches(L"H"));
1725 EXPECT_FALSE(m2.Matches(L" Hi"));
1726}
1727
1728TEST(GlobalWideStartsWithTest, CanDescribeSelf) {
1729 Matcher<const ::wstring> m = StartsWith(L"Hi");
1730 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1731}
1732
1733// Tests EndsWith(s).
1734
1735TEST(GlobalWideEndsWithTest, MatchesStringWithGivenSuffix) {
1736 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1737 EXPECT_TRUE(m1.Matches(L"Hi"));
1738 EXPECT_TRUE(m1.Matches(L""));
1739 EXPECT_FALSE(m1.Matches(NULL));
1740
1741 const Matcher<const ::wstring&> m2 = EndsWith(::wstring(L"Hi"));
1742 EXPECT_TRUE(m2.Matches(L"Hi"));
1743 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1744 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1745 EXPECT_FALSE(m2.Matches(L"i"));
1746 EXPECT_FALSE(m2.Matches(L"Hi "));
1747}
1748
1749TEST(GlobalWideEndsWithTest, CanDescribeSelf) {
1750 Matcher<const ::wstring> m = EndsWith(L"Hi");
1751 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1752}
1753
1754#endif // GTEST_HAS_GLOBAL_WSTRING
1755
1756
1757typedef ::std::tr1::tuple<long, int> Tuple2; // NOLINT
1758
1759// Tests that Eq() matches a 2-tuple where the first field == the
1760// second field.
1761TEST(Eq2Test, MatchesEqualArguments) {
1762 Matcher<const Tuple2&> m = Eq();
1763 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1764 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1765}
1766
1767// Tests that Eq() describes itself properly.
1768TEST(Eq2Test, CanDescribeSelf) {
1769 Matcher<const Tuple2&> m = Eq();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001770 EXPECT_EQ("are an equal pair", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001771}
1772
1773// Tests that Ge() matches a 2-tuple where the first field >= the
1774// second field.
1775TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) {
1776 Matcher<const Tuple2&> m = Ge();
1777 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1778 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1779 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1780}
1781
1782// Tests that Ge() describes itself properly.
1783TEST(Ge2Test, CanDescribeSelf) {
1784 Matcher<const Tuple2&> m = Ge();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001785 EXPECT_EQ("are a pair where the first >= the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001786}
1787
1788// Tests that Gt() matches a 2-tuple where the first field > the
1789// second field.
1790TEST(Gt2Test, MatchesGreaterThanArguments) {
1791 Matcher<const Tuple2&> m = Gt();
1792 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1793 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1794 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1795}
1796
1797// Tests that Gt() describes itself properly.
1798TEST(Gt2Test, CanDescribeSelf) {
1799 Matcher<const Tuple2&> m = Gt();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001800 EXPECT_EQ("are a pair where the first > the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001801}
1802
1803// Tests that Le() matches a 2-tuple where the first field <= the
1804// second field.
1805TEST(Le2Test, MatchesLessThanOrEqualArguments) {
1806 Matcher<const Tuple2&> m = Le();
1807 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1808 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1809 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1810}
1811
1812// Tests that Le() describes itself properly.
1813TEST(Le2Test, CanDescribeSelf) {
1814 Matcher<const Tuple2&> m = Le();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001815 EXPECT_EQ("are a pair where the first <= the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001816}
1817
1818// Tests that Lt() matches a 2-tuple where the first field < the
1819// second field.
1820TEST(Lt2Test, MatchesLessThanArguments) {
1821 Matcher<const Tuple2&> m = Lt();
1822 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1823 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1824 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1825}
1826
1827// Tests that Lt() describes itself properly.
1828TEST(Lt2Test, CanDescribeSelf) {
1829 Matcher<const Tuple2&> m = Lt();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001830 EXPECT_EQ("are a pair where the first < the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001831}
1832
1833// Tests that Ne() matches a 2-tuple where the first field != the
1834// second field.
1835TEST(Ne2Test, MatchesUnequalArguments) {
1836 Matcher<const Tuple2&> m = Ne();
1837 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1838 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1839 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1840}
1841
1842// Tests that Ne() describes itself properly.
1843TEST(Ne2Test, CanDescribeSelf) {
1844 Matcher<const Tuple2&> m = Ne();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001845 EXPECT_EQ("are an unequal pair", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001846}
1847
1848// Tests that Not(m) matches any value that doesn't match m.
1849TEST(NotTest, NegatesMatcher) {
1850 Matcher<int> m;
1851 m = Not(Eq(2));
1852 EXPECT_TRUE(m.Matches(3));
1853 EXPECT_FALSE(m.Matches(2));
1854}
1855
1856// Tests that Not(m) describes itself properly.
1857TEST(NotTest, CanDescribeSelf) {
1858 Matcher<int> m = Not(Eq(5));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001859 EXPECT_EQ("isn't equal to 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001860}
1861
zhanyong.wan18490652009-05-11 18:54:08 +00001862// Tests that monomorphic matchers are safely cast by the Not matcher.
1863TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {
1864 // greater_than_5 is a monomorphic matcher.
1865 Matcher<int> greater_than_5 = Gt(5);
1866
1867 Matcher<const int&> m = Not(greater_than_5);
1868 Matcher<int&> m2 = Not(greater_than_5);
1869 Matcher<int&> m3 = Not(m);
1870}
1871
shiqiane35fdd92008-12-10 05:08:54 +00001872// Tests that AllOf(m1, ..., mn) matches any value that matches all of
1873// the given matchers.
1874TEST(AllOfTest, MatchesWhenAllMatch) {
1875 Matcher<int> m;
1876 m = AllOf(Le(2), Ge(1));
1877 EXPECT_TRUE(m.Matches(1));
1878 EXPECT_TRUE(m.Matches(2));
1879 EXPECT_FALSE(m.Matches(0));
1880 EXPECT_FALSE(m.Matches(3));
1881
1882 m = AllOf(Gt(0), Ne(1), Ne(2));
1883 EXPECT_TRUE(m.Matches(3));
1884 EXPECT_FALSE(m.Matches(2));
1885 EXPECT_FALSE(m.Matches(1));
1886 EXPECT_FALSE(m.Matches(0));
1887
1888 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
1889 EXPECT_TRUE(m.Matches(4));
1890 EXPECT_FALSE(m.Matches(3));
1891 EXPECT_FALSE(m.Matches(2));
1892 EXPECT_FALSE(m.Matches(1));
1893 EXPECT_FALSE(m.Matches(0));
1894
1895 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
1896 EXPECT_TRUE(m.Matches(0));
1897 EXPECT_TRUE(m.Matches(1));
1898 EXPECT_FALSE(m.Matches(3));
1899}
1900
1901// Tests that AllOf(m1, ..., mn) describes itself properly.
1902TEST(AllOfTest, CanDescribeSelf) {
1903 Matcher<int> m;
1904 m = AllOf(Le(2), Ge(1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001905 EXPECT_EQ("(is <= 2) and (is >= 1)", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001906
1907 m = AllOf(Gt(0), Ne(1), Ne(2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001908 EXPECT_EQ("(is > 0) and "
1909 "((isn't equal to 1) and "
1910 "(isn't equal to 2))",
shiqiane35fdd92008-12-10 05:08:54 +00001911 Describe(m));
1912
1913
1914 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001915 EXPECT_EQ("(is > 0) and "
1916 "((isn't equal to 1) and "
1917 "((isn't equal to 2) and "
1918 "(isn't equal to 3)))",
shiqiane35fdd92008-12-10 05:08:54 +00001919 Describe(m));
1920
1921
1922 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001923 EXPECT_EQ("(is >= 0) and "
1924 "((is < 10) and "
1925 "((isn't equal to 3) and "
1926 "((isn't equal to 5) and "
1927 "(isn't equal to 7))))",
1928 Describe(m));
1929}
1930
1931// Tests that AllOf(m1, ..., mn) describes its negation properly.
1932TEST(AllOfTest, CanDescribeNegation) {
1933 Matcher<int> m;
1934 m = AllOf(Le(2), Ge(1));
1935 EXPECT_EQ("(isn't <= 2) or "
1936 "(isn't >= 1)",
1937 DescribeNegation(m));
1938
1939 m = AllOf(Gt(0), Ne(1), Ne(2));
1940 EXPECT_EQ("(isn't > 0) or "
1941 "((is equal to 1) or "
1942 "(is equal to 2))",
1943 DescribeNegation(m));
1944
1945
1946 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
1947 EXPECT_EQ("(isn't > 0) or "
1948 "((is equal to 1) or "
1949 "((is equal to 2) or "
1950 "(is equal to 3)))",
1951 DescribeNegation(m));
1952
1953
1954 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
1955 EXPECT_EQ("(isn't >= 0) or "
1956 "((isn't < 10) or "
1957 "((is equal to 3) or "
1958 "((is equal to 5) or "
1959 "(is equal to 7))))",
1960 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00001961}
1962
zhanyong.wan18490652009-05-11 18:54:08 +00001963// Tests that monomorphic matchers are safely cast by the AllOf matcher.
1964TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
1965 // greater_than_5 and less_than_10 are monomorphic matchers.
1966 Matcher<int> greater_than_5 = Gt(5);
1967 Matcher<int> less_than_10 = Lt(10);
1968
1969 Matcher<const int&> m = AllOf(greater_than_5, less_than_10);
1970 Matcher<int&> m2 = AllOf(greater_than_5, less_than_10);
1971 Matcher<int&> m3 = AllOf(greater_than_5, m2);
1972
1973 // Tests that BothOf works when composing itself.
1974 Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10);
1975 Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);
1976}
1977
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001978TEST(AllOfTest, ExplainsResult) {
1979 Matcher<int> m;
1980
1981 // Successful match. Both matchers need to explain. The second
1982 // matcher doesn't give an explanation, so only the first matcher's
1983 // explanation is printed.
1984 m = AllOf(GreaterThan(10), Lt(30));
1985 EXPECT_EQ("which is 15 more than 10", Explain(m, 25));
1986
1987 // Successful match. Both matchers need to explain.
1988 m = AllOf(GreaterThan(10), GreaterThan(20));
1989 EXPECT_EQ("which is 20 more than 10, and which is 10 more than 20",
1990 Explain(m, 30));
1991
1992 // Successful match. All matchers need to explain. The second
1993 // matcher doesn't given an explanation.
1994 m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20));
1995 EXPECT_EQ("which is 15 more than 10, and which is 5 more than 20",
1996 Explain(m, 25));
1997
1998 // Successful match. All matchers need to explain.
1999 m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2000 EXPECT_EQ("which is 30 more than 10, and which is 20 more than 20, "
2001 "and which is 10 more than 30",
2002 Explain(m, 40));
2003
2004 // Failed match. The first matcher, which failed, needs to
2005 // explain.
2006 m = AllOf(GreaterThan(10), GreaterThan(20));
2007 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
2008
2009 // Failed match. The second matcher, which failed, needs to
2010 // explain. Since it doesn't given an explanation, nothing is
2011 // printed.
2012 m = AllOf(GreaterThan(10), Lt(30));
2013 EXPECT_EQ("", Explain(m, 40));
2014
2015 // Failed match. The second matcher, which failed, needs to
2016 // explain.
2017 m = AllOf(GreaterThan(10), GreaterThan(20));
2018 EXPECT_EQ("which is 5 less than 20", Explain(m, 15));
2019}
2020
shiqiane35fdd92008-12-10 05:08:54 +00002021// Tests that AnyOf(m1, ..., mn) matches any value that matches at
2022// least one of the given matchers.
2023TEST(AnyOfTest, MatchesWhenAnyMatches) {
2024 Matcher<int> m;
2025 m = AnyOf(Le(1), Ge(3));
2026 EXPECT_TRUE(m.Matches(1));
2027 EXPECT_TRUE(m.Matches(4));
2028 EXPECT_FALSE(m.Matches(2));
2029
2030 m = AnyOf(Lt(0), Eq(1), Eq(2));
2031 EXPECT_TRUE(m.Matches(-1));
2032 EXPECT_TRUE(m.Matches(1));
2033 EXPECT_TRUE(m.Matches(2));
2034 EXPECT_FALSE(m.Matches(0));
2035
2036 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2037 EXPECT_TRUE(m.Matches(-1));
2038 EXPECT_TRUE(m.Matches(1));
2039 EXPECT_TRUE(m.Matches(2));
2040 EXPECT_TRUE(m.Matches(3));
2041 EXPECT_FALSE(m.Matches(0));
2042
2043 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2044 EXPECT_TRUE(m.Matches(0));
2045 EXPECT_TRUE(m.Matches(11));
2046 EXPECT_TRUE(m.Matches(3));
2047 EXPECT_FALSE(m.Matches(2));
2048}
2049
2050// Tests that AnyOf(m1, ..., mn) describes itself properly.
2051TEST(AnyOfTest, CanDescribeSelf) {
2052 Matcher<int> m;
2053 m = AnyOf(Le(1), Ge(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002054 EXPECT_EQ("(is <= 1) or (is >= 3)",
shiqiane35fdd92008-12-10 05:08:54 +00002055 Describe(m));
2056
2057 m = AnyOf(Lt(0), Eq(1), Eq(2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002058 EXPECT_EQ("(is < 0) or "
shiqiane35fdd92008-12-10 05:08:54 +00002059 "((is equal to 1) or (is equal to 2))",
2060 Describe(m));
2061
2062 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002063 EXPECT_EQ("(is < 0) or "
shiqiane35fdd92008-12-10 05:08:54 +00002064 "((is equal to 1) or "
2065 "((is equal to 2) or "
2066 "(is equal to 3)))",
2067 Describe(m));
2068
2069 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002070 EXPECT_EQ("(is <= 0) or "
2071 "((is > 10) or "
shiqiane35fdd92008-12-10 05:08:54 +00002072 "((is equal to 3) or "
2073 "((is equal to 5) or "
2074 "(is equal to 7))))",
2075 Describe(m));
2076}
2077
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002078// Tests that AnyOf(m1, ..., mn) describes its negation properly.
2079TEST(AnyOfTest, CanDescribeNegation) {
2080 Matcher<int> m;
2081 m = AnyOf(Le(1), Ge(3));
2082 EXPECT_EQ("(isn't <= 1) and (isn't >= 3)",
2083 DescribeNegation(m));
2084
2085 m = AnyOf(Lt(0), Eq(1), Eq(2));
2086 EXPECT_EQ("(isn't < 0) and "
2087 "((isn't equal to 1) and (isn't equal to 2))",
2088 DescribeNegation(m));
2089
2090 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2091 EXPECT_EQ("(isn't < 0) and "
2092 "((isn't equal to 1) and "
2093 "((isn't equal to 2) and "
2094 "(isn't equal to 3)))",
2095 DescribeNegation(m));
2096
2097 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2098 EXPECT_EQ("(isn't <= 0) and "
2099 "((isn't > 10) and "
2100 "((isn't equal to 3) and "
2101 "((isn't equal to 5) and "
2102 "(isn't equal to 7))))",
2103 DescribeNegation(m));
2104}
2105
zhanyong.wan18490652009-05-11 18:54:08 +00002106// Tests that monomorphic matchers are safely cast by the AnyOf matcher.
2107TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
2108 // greater_than_5 and less_than_10 are monomorphic matchers.
2109 Matcher<int> greater_than_5 = Gt(5);
2110 Matcher<int> less_than_10 = Lt(10);
2111
2112 Matcher<const int&> m = AnyOf(greater_than_5, less_than_10);
2113 Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10);
2114 Matcher<int&> m3 = AnyOf(greater_than_5, m2);
2115
2116 // Tests that EitherOf works when composing itself.
2117 Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10);
2118 Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);
2119}
2120
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002121TEST(AnyOfTest, ExplainsResult) {
2122 Matcher<int> m;
2123
2124 // Failed match. Both matchers need to explain. The second
2125 // matcher doesn't give an explanation, so only the first matcher's
2126 // explanation is printed.
2127 m = AnyOf(GreaterThan(10), Lt(0));
2128 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
2129
2130 // Failed match. Both matchers need to explain.
2131 m = AnyOf(GreaterThan(10), GreaterThan(20));
2132 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20",
2133 Explain(m, 5));
2134
2135 // Failed match. All matchers need to explain. The second
2136 // matcher doesn't given an explanation.
2137 m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30));
2138 EXPECT_EQ("which is 5 less than 10, and which is 25 less than 30",
2139 Explain(m, 5));
2140
2141 // Failed match. All matchers need to explain.
2142 m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2143 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20, "
2144 "and which is 25 less than 30",
2145 Explain(m, 5));
2146
2147 // Successful match. The first matcher, which succeeded, needs to
2148 // explain.
2149 m = AnyOf(GreaterThan(10), GreaterThan(20));
2150 EXPECT_EQ("which is 5 more than 10", Explain(m, 15));
2151
2152 // Successful match. The second matcher, which succeeded, needs to
2153 // explain. Since it doesn't given an explanation, nothing is
2154 // printed.
2155 m = AnyOf(GreaterThan(10), Lt(30));
2156 EXPECT_EQ("", Explain(m, 0));
2157
2158 // Successful match. The second matcher, which succeeded, needs to
2159 // explain.
2160 m = AnyOf(GreaterThan(30), GreaterThan(20));
2161 EXPECT_EQ("which is 5 more than 20", Explain(m, 25));
2162}
2163
shiqiane35fdd92008-12-10 05:08:54 +00002164// The following predicate function and predicate functor are for
2165// testing the Truly(predicate) matcher.
2166
2167// Returns non-zero if the input is positive. Note that the return
2168// type of this function is not bool. It's OK as Truly() accepts any
2169// unary function or functor whose return type can be implicitly
2170// converted to bool.
2171int IsPositive(double x) {
2172 return x > 0 ? 1 : 0;
2173}
2174
2175// This functor returns true if the input is greater than the given
2176// number.
2177class IsGreaterThan {
2178 public:
2179 explicit IsGreaterThan(int threshold) : threshold_(threshold) {}
2180
2181 bool operator()(int n) const { return n > threshold_; }
zhanyong.wan32de5f52009-12-23 00:13:23 +00002182
shiqiane35fdd92008-12-10 05:08:54 +00002183 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002184 int threshold_;
shiqiane35fdd92008-12-10 05:08:54 +00002185};
2186
2187// For testing Truly().
2188const int foo = 0;
2189
2190// This predicate returns true iff the argument references foo and has
2191// a zero value.
2192bool ReferencesFooAndIsZero(const int& n) {
2193 return (&n == &foo) && (n == 0);
2194}
2195
2196// Tests that Truly(predicate) matches what satisfies the given
2197// predicate.
2198TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {
2199 Matcher<double> m = Truly(IsPositive);
2200 EXPECT_TRUE(m.Matches(2.0));
2201 EXPECT_FALSE(m.Matches(-1.5));
2202}
2203
2204// Tests that Truly(predicate_functor) works too.
2205TEST(TrulyTest, CanBeUsedWithFunctor) {
2206 Matcher<int> m = Truly(IsGreaterThan(5));
2207 EXPECT_TRUE(m.Matches(6));
2208 EXPECT_FALSE(m.Matches(4));
2209}
2210
2211// Tests that Truly(predicate) can describe itself properly.
2212TEST(TrulyTest, CanDescribeSelf) {
2213 Matcher<double> m = Truly(IsPositive);
2214 EXPECT_EQ("satisfies the given predicate",
2215 Describe(m));
2216}
2217
2218// Tests that Truly(predicate) works when the matcher takes its
2219// argument by reference.
2220TEST(TrulyTest, WorksForByRefArguments) {
2221 Matcher<const int&> m = Truly(ReferencesFooAndIsZero);
2222 EXPECT_TRUE(m.Matches(foo));
2223 int n = 0;
2224 EXPECT_FALSE(m.Matches(n));
2225}
2226
2227// Tests that Matches(m) is a predicate satisfied by whatever that
2228// matches matcher m.
2229TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {
2230 EXPECT_TRUE(Matches(Ge(0))(1));
2231 EXPECT_FALSE(Matches(Eq('a'))('b'));
2232}
2233
2234// Tests that Matches(m) works when the matcher takes its argument by
2235// reference.
2236TEST(MatchesTest, WorksOnByRefArguments) {
2237 int m = 0, n = 0;
2238 EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n));
2239 EXPECT_FALSE(Matches(Ref(m))(n));
2240}
2241
2242// Tests that a Matcher on non-reference type can be used in
2243// Matches().
2244TEST(MatchesTest, WorksWithMatcherOnNonRefType) {
2245 Matcher<int> eq5 = Eq(5);
2246 EXPECT_TRUE(Matches(eq5)(5));
2247 EXPECT_FALSE(Matches(eq5)(2));
2248}
2249
zhanyong.wanb8243162009-06-04 05:48:20 +00002250// Tests Value(value, matcher). Since Value() is a simple wrapper for
2251// Matches(), which has been tested already, we don't spend a lot of
2252// effort on testing Value().
2253TEST(ValueTest, WorksWithPolymorphicMatcher) {
2254 EXPECT_TRUE(Value("hi", StartsWith("h")));
2255 EXPECT_FALSE(Value(5, Gt(10)));
2256}
2257
2258TEST(ValueTest, WorksWithMonomorphicMatcher) {
2259 const Matcher<int> is_zero = Eq(0);
2260 EXPECT_TRUE(Value(0, is_zero));
2261 EXPECT_FALSE(Value('a', is_zero));
2262
2263 int n = 0;
2264 const Matcher<const int&> ref_n = Ref(n);
2265 EXPECT_TRUE(Value(n, ref_n));
2266 EXPECT_FALSE(Value(1, ref_n));
2267}
2268
zhanyong.wana862f1d2010-03-15 21:23:04 +00002269TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) {
zhanyong.wan34b034c2010-03-05 21:23:23 +00002270 StringMatchResultListener listener1;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002271 EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002272 EXPECT_EQ("% 2 == 0", listener1.str());
2273
2274 StringMatchResultListener listener2;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002275 EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002276 EXPECT_EQ("", listener2.str());
2277}
2278
zhanyong.wana862f1d2010-03-15 21:23:04 +00002279TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) {
zhanyong.wan34b034c2010-03-05 21:23:23 +00002280 const Matcher<int> is_even = PolymorphicIsEven();
2281 StringMatchResultListener listener1;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002282 EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002283 EXPECT_EQ("% 2 == 0", listener1.str());
2284
2285 const Matcher<const double&> is_zero = Eq(0);
2286 StringMatchResultListener listener2;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002287 EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002288 EXPECT_EQ("", listener2.str());
2289}
2290
zhanyong.wana862f1d2010-03-15 21:23:04 +00002291MATCHER_P(Really, inner_matcher, "") {
2292 return ExplainMatchResult(inner_matcher, arg, result_listener);
2293}
2294
2295TEST(ExplainMatchResultTest, WorksInsideMATCHER) {
2296 EXPECT_THAT(0, Really(Eq(0)));
2297}
2298
zhanyong.wanbf550852009-06-09 06:09:53 +00002299TEST(AllArgsTest, WorksForTuple) {
2300 EXPECT_THAT(make_tuple(1, 2L), AllArgs(Lt()));
2301 EXPECT_THAT(make_tuple(2L, 1), Not(AllArgs(Lt())));
2302}
2303
2304TEST(AllArgsTest, WorksForNonTuple) {
2305 EXPECT_THAT(42, AllArgs(Gt(0)));
2306 EXPECT_THAT('a', Not(AllArgs(Eq('b'))));
2307}
2308
2309class AllArgsHelper {
2310 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002311 AllArgsHelper() {}
2312
zhanyong.wanbf550852009-06-09 06:09:53 +00002313 MOCK_METHOD2(Helper, int(char x, int y));
zhanyong.wan32de5f52009-12-23 00:13:23 +00002314
2315 private:
2316 GTEST_DISALLOW_COPY_AND_ASSIGN_(AllArgsHelper);
zhanyong.wanbf550852009-06-09 06:09:53 +00002317};
2318
2319TEST(AllArgsTest, WorksInWithClause) {
2320 AllArgsHelper helper;
2321 ON_CALL(helper, Helper(_, _))
2322 .With(AllArgs(Lt()))
2323 .WillByDefault(Return(1));
2324 EXPECT_CALL(helper, Helper(_, _));
2325 EXPECT_CALL(helper, Helper(_, _))
2326 .With(AllArgs(Gt()))
2327 .WillOnce(Return(2));
2328
2329 EXPECT_EQ(1, helper.Helper('\1', 2));
2330 EXPECT_EQ(2, helper.Helper('a', 1));
2331}
2332
shiqiane35fdd92008-12-10 05:08:54 +00002333// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2334// matches the matcher.
2335TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {
2336 ASSERT_THAT(5, Ge(2)) << "This should succeed.";
2337 ASSERT_THAT("Foo", EndsWith("oo"));
2338 EXPECT_THAT(2, AllOf(Le(7), Ge(0))) << "This should succeed too.";
2339 EXPECT_THAT("Hello", StartsWith("Hell"));
2340}
2341
2342// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2343// doesn't match the matcher.
2344TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {
2345 // 'n' must be static as it is used in an EXPECT_FATAL_FAILURE(),
2346 // which cannot reference auto variables.
2347 static int n;
2348 n = 5;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002349
2350 // VC++ prior to version 8.0 SP1 has a bug where it will not see any
2351 // functions declared in the namespace scope from within nested classes.
2352 // EXPECT/ASSERT_(NON)FATAL_FAILURE macros use nested classes so that all
2353 // namespace-level functions invoked inside them need to be explicitly
2354 // resolved.
2355 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Gt(10)),
shiqiane35fdd92008-12-10 05:08:54 +00002356 "Value of: n\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002357 "Expected: is > 10\n"
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002358 " Actual: 5");
shiqiane35fdd92008-12-10 05:08:54 +00002359 n = 0;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002360 EXPECT_NONFATAL_FAILURE(
2361 EXPECT_THAT(n, ::testing::AllOf(::testing::Le(7), ::testing::Ge(5))),
2362 "Value of: n\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002363 "Expected: (is <= 7) and (is >= 5)\n"
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002364 " Actual: 0");
shiqiane35fdd92008-12-10 05:08:54 +00002365}
2366
2367// Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument
2368// has a reference type.
2369TEST(MatcherAssertionTest, WorksForByRefArguments) {
2370 // We use a static variable here as EXPECT_FATAL_FAILURE() cannot
2371 // reference auto variables.
2372 static int n;
2373 n = 0;
2374 EXPECT_THAT(n, AllOf(Le(7), Ref(n)));
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002375 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
shiqiane35fdd92008-12-10 05:08:54 +00002376 "Value of: n\n"
2377 "Expected: does not reference the variable @");
2378 // Tests the "Actual" part.
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002379 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002380 "Actual: 0, which is located @");
shiqiane35fdd92008-12-10 05:08:54 +00002381}
2382
zhanyong.wan95b12332009-09-25 18:55:50 +00002383#if !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002384// Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is
2385// monomorphic.
zhanyong.wan95b12332009-09-25 18:55:50 +00002386
2387// ASSERT_THAT("hello", starts_with_he) fails to compile with Nokia's
2388// Symbian compiler: it tries to compile
2389// template<T, U> class MatcherCastImpl { ...
zhanyong.wandb22c222010-01-28 21:52:29 +00002390// virtual bool MatchAndExplain(T x, ...) const {
2391// return source_matcher_.MatchAndExplain(static_cast<U>(x), ...);
zhanyong.wan95b12332009-09-25 18:55:50 +00002392// with U == string and T == const char*
2393// With ASSERT_THAT("hello"...) changed to ASSERT_THAT(string("hello") ... )
2394// the compiler silently crashes with no output.
2395// If MatcherCastImpl is changed to use U(x) instead of static_cast<U>(x)
2396// the code compiles but the converted string is bogus.
shiqiane35fdd92008-12-10 05:08:54 +00002397TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
2398 Matcher<const char*> starts_with_he = StartsWith("he");
2399 ASSERT_THAT("hello", starts_with_he);
2400
2401 Matcher<const string&> ends_with_ok = EndsWith("ok");
2402 ASSERT_THAT("book", ends_with_ok);
2403
2404 Matcher<int> is_greater_than_5 = Gt(5);
2405 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5),
2406 "Value of: 5\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002407 "Expected: is > 5\n"
shiqiane35fdd92008-12-10 05:08:54 +00002408 " Actual: 5");
2409}
zhanyong.wan95b12332009-09-25 18:55:50 +00002410#endif // !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002411
2412// Tests floating-point matchers.
2413template <typename RawType>
2414class FloatingPointTest : public testing::Test {
2415 protected:
2416 typedef typename testing::internal::FloatingPoint<RawType> Floating;
2417 typedef typename Floating::Bits Bits;
2418
2419 virtual void SetUp() {
2420 const size_t max_ulps = Floating::kMaxUlps;
2421
2422 // The bits that represent 0.0.
2423 const Bits zero_bits = Floating(0).bits();
2424
2425 // Makes some numbers close to 0.0.
2426 close_to_positive_zero_ = Floating::ReinterpretBits(zero_bits + max_ulps/2);
2427 close_to_negative_zero_ = -Floating::ReinterpretBits(
2428 zero_bits + max_ulps - max_ulps/2);
2429 further_from_negative_zero_ = -Floating::ReinterpretBits(
2430 zero_bits + max_ulps + 1 - max_ulps/2);
2431
2432 // The bits that represent 1.0.
2433 const Bits one_bits = Floating(1).bits();
2434
2435 // Makes some numbers close to 1.0.
2436 close_to_one_ = Floating::ReinterpretBits(one_bits + max_ulps);
2437 further_from_one_ = Floating::ReinterpretBits(one_bits + max_ulps + 1);
2438
2439 // +infinity.
2440 infinity_ = Floating::Infinity();
2441
2442 // The bits that represent +infinity.
2443 const Bits infinity_bits = Floating(infinity_).bits();
2444
2445 // Makes some numbers close to infinity.
2446 close_to_infinity_ = Floating::ReinterpretBits(infinity_bits - max_ulps);
2447 further_from_infinity_ = Floating::ReinterpretBits(
2448 infinity_bits - max_ulps - 1);
2449
2450 // Makes some NAN's.
2451 nan1_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 1);
2452 nan2_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 200);
2453 }
2454
2455 void TestSize() {
2456 EXPECT_EQ(sizeof(RawType), sizeof(Bits));
2457 }
2458
2459 // A battery of tests for FloatingEqMatcher::Matches.
2460 // matcher_maker is a pointer to a function which creates a FloatingEqMatcher.
2461 void TestMatches(
2462 testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) {
2463 Matcher<RawType> m1 = matcher_maker(0.0);
2464 EXPECT_TRUE(m1.Matches(-0.0));
2465 EXPECT_TRUE(m1.Matches(close_to_positive_zero_));
2466 EXPECT_TRUE(m1.Matches(close_to_negative_zero_));
2467 EXPECT_FALSE(m1.Matches(1.0));
2468
2469 Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_);
2470 EXPECT_FALSE(m2.Matches(further_from_negative_zero_));
2471
2472 Matcher<RawType> m3 = matcher_maker(1.0);
2473 EXPECT_TRUE(m3.Matches(close_to_one_));
2474 EXPECT_FALSE(m3.Matches(further_from_one_));
2475
2476 // Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above.
2477 EXPECT_FALSE(m3.Matches(0.0));
2478
2479 Matcher<RawType> m4 = matcher_maker(-infinity_);
2480 EXPECT_TRUE(m4.Matches(-close_to_infinity_));
2481
2482 Matcher<RawType> m5 = matcher_maker(infinity_);
2483 EXPECT_TRUE(m5.Matches(close_to_infinity_));
2484
2485 // This is interesting as the representations of infinity_ and nan1_
2486 // are only 1 DLP apart.
2487 EXPECT_FALSE(m5.Matches(nan1_));
2488
2489 // matcher_maker can produce a Matcher<const RawType&>, which is needed in
2490 // some cases.
2491 Matcher<const RawType&> m6 = matcher_maker(0.0);
2492 EXPECT_TRUE(m6.Matches(-0.0));
2493 EXPECT_TRUE(m6.Matches(close_to_positive_zero_));
2494 EXPECT_FALSE(m6.Matches(1.0));
2495
2496 // matcher_maker can produce a Matcher<RawType&>, which is needed in some
2497 // cases.
2498 Matcher<RawType&> m7 = matcher_maker(0.0);
2499 RawType x = 0.0;
2500 EXPECT_TRUE(m7.Matches(x));
2501 x = 0.01f;
2502 EXPECT_FALSE(m7.Matches(x));
2503 }
2504
2505 // Pre-calculated numbers to be used by the tests.
2506
2507 static RawType close_to_positive_zero_;
2508 static RawType close_to_negative_zero_;
2509 static RawType further_from_negative_zero_;
2510
2511 static RawType close_to_one_;
2512 static RawType further_from_one_;
2513
2514 static RawType infinity_;
2515 static RawType close_to_infinity_;
2516 static RawType further_from_infinity_;
2517
2518 static RawType nan1_;
2519 static RawType nan2_;
2520};
2521
2522template <typename RawType>
2523RawType FloatingPointTest<RawType>::close_to_positive_zero_;
2524
2525template <typename RawType>
2526RawType FloatingPointTest<RawType>::close_to_negative_zero_;
2527
2528template <typename RawType>
2529RawType FloatingPointTest<RawType>::further_from_negative_zero_;
2530
2531template <typename RawType>
2532RawType FloatingPointTest<RawType>::close_to_one_;
2533
2534template <typename RawType>
2535RawType FloatingPointTest<RawType>::further_from_one_;
2536
2537template <typename RawType>
2538RawType FloatingPointTest<RawType>::infinity_;
2539
2540template <typename RawType>
2541RawType FloatingPointTest<RawType>::close_to_infinity_;
2542
2543template <typename RawType>
2544RawType FloatingPointTest<RawType>::further_from_infinity_;
2545
2546template <typename RawType>
2547RawType FloatingPointTest<RawType>::nan1_;
2548
2549template <typename RawType>
2550RawType FloatingPointTest<RawType>::nan2_;
2551
2552// Instantiate FloatingPointTest for testing floats.
2553typedef FloatingPointTest<float> FloatTest;
2554
2555TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) {
2556 TestMatches(&FloatEq);
2557}
2558
2559TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
2560 TestMatches(&NanSensitiveFloatEq);
2561}
2562
2563TEST_F(FloatTest, FloatEqCannotMatchNaN) {
2564 // FloatEq never matches NaN.
2565 Matcher<float> m = FloatEq(nan1_);
2566 EXPECT_FALSE(m.Matches(nan1_));
2567 EXPECT_FALSE(m.Matches(nan2_));
2568 EXPECT_FALSE(m.Matches(1.0));
2569}
2570
2571TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {
2572 // NanSensitiveFloatEq will match NaN.
2573 Matcher<float> m = NanSensitiveFloatEq(nan1_);
2574 EXPECT_TRUE(m.Matches(nan1_));
2575 EXPECT_TRUE(m.Matches(nan2_));
2576 EXPECT_FALSE(m.Matches(1.0));
2577}
2578
2579TEST_F(FloatTest, FloatEqCanDescribeSelf) {
2580 Matcher<float> m1 = FloatEq(2.0f);
2581 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002582 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002583
2584 Matcher<float> m2 = FloatEq(0.5f);
2585 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002586 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002587
2588 Matcher<float> m3 = FloatEq(nan1_);
2589 EXPECT_EQ("never matches", Describe(m3));
2590 EXPECT_EQ("is anything", DescribeNegation(m3));
2591}
2592
2593TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {
2594 Matcher<float> m1 = NanSensitiveFloatEq(2.0f);
2595 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002596 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002597
2598 Matcher<float> m2 = NanSensitiveFloatEq(0.5f);
2599 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002600 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002601
2602 Matcher<float> m3 = NanSensitiveFloatEq(nan1_);
2603 EXPECT_EQ("is NaN", Describe(m3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002604 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
shiqiane35fdd92008-12-10 05:08:54 +00002605}
2606
2607// Instantiate FloatingPointTest for testing doubles.
2608typedef FloatingPointTest<double> DoubleTest;
2609
2610TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
2611 TestMatches(&DoubleEq);
2612}
2613
2614TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
2615 TestMatches(&NanSensitiveDoubleEq);
2616}
2617
2618TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {
2619 // DoubleEq never matches NaN.
2620 Matcher<double> m = DoubleEq(nan1_);
2621 EXPECT_FALSE(m.Matches(nan1_));
2622 EXPECT_FALSE(m.Matches(nan2_));
2623 EXPECT_FALSE(m.Matches(1.0));
2624}
2625
2626TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {
2627 // NanSensitiveDoubleEq will match NaN.
2628 Matcher<double> m = NanSensitiveDoubleEq(nan1_);
2629 EXPECT_TRUE(m.Matches(nan1_));
2630 EXPECT_TRUE(m.Matches(nan2_));
2631 EXPECT_FALSE(m.Matches(1.0));
2632}
2633
2634TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {
2635 Matcher<double> m1 = DoubleEq(2.0);
2636 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002637 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002638
2639 Matcher<double> m2 = DoubleEq(0.5);
2640 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002641 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002642
2643 Matcher<double> m3 = DoubleEq(nan1_);
2644 EXPECT_EQ("never matches", Describe(m3));
2645 EXPECT_EQ("is anything", DescribeNegation(m3));
2646}
2647
2648TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {
2649 Matcher<double> m1 = NanSensitiveDoubleEq(2.0);
2650 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002651 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002652
2653 Matcher<double> m2 = NanSensitiveDoubleEq(0.5);
2654 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002655 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002656
2657 Matcher<double> m3 = NanSensitiveDoubleEq(nan1_);
2658 EXPECT_EQ("is NaN", Describe(m3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002659 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
shiqiane35fdd92008-12-10 05:08:54 +00002660}
2661
2662TEST(PointeeTest, RawPointer) {
2663 const Matcher<int*> m = Pointee(Ge(0));
2664
2665 int n = 1;
2666 EXPECT_TRUE(m.Matches(&n));
2667 n = -1;
2668 EXPECT_FALSE(m.Matches(&n));
2669 EXPECT_FALSE(m.Matches(NULL));
2670}
2671
2672TEST(PointeeTest, RawPointerToConst) {
2673 const Matcher<const double*> m = Pointee(Ge(0));
2674
2675 double x = 1;
2676 EXPECT_TRUE(m.Matches(&x));
2677 x = -1;
2678 EXPECT_FALSE(m.Matches(&x));
2679 EXPECT_FALSE(m.Matches(NULL));
2680}
2681
2682TEST(PointeeTest, ReferenceToConstRawPointer) {
2683 const Matcher<int* const &> m = Pointee(Ge(0));
2684
2685 int n = 1;
2686 EXPECT_TRUE(m.Matches(&n));
2687 n = -1;
2688 EXPECT_FALSE(m.Matches(&n));
2689 EXPECT_FALSE(m.Matches(NULL));
2690}
2691
2692TEST(PointeeTest, ReferenceToNonConstRawPointer) {
2693 const Matcher<double* &> m = Pointee(Ge(0));
2694
2695 double x = 1.0;
2696 double* p = &x;
2697 EXPECT_TRUE(m.Matches(p));
2698 x = -1;
2699 EXPECT_FALSE(m.Matches(p));
2700 p = NULL;
2701 EXPECT_FALSE(m.Matches(p));
2702}
2703
2704TEST(PointeeTest, NeverMatchesNull) {
2705 const Matcher<const char*> m = Pointee(_);
2706 EXPECT_FALSE(m.Matches(NULL));
2707}
2708
2709// Tests that we can write Pointee(value) instead of Pointee(Eq(value)).
2710TEST(PointeeTest, MatchesAgainstAValue) {
2711 const Matcher<int*> m = Pointee(5);
2712
2713 int n = 5;
2714 EXPECT_TRUE(m.Matches(&n));
2715 n = -1;
2716 EXPECT_FALSE(m.Matches(&n));
2717 EXPECT_FALSE(m.Matches(NULL));
2718}
2719
2720TEST(PointeeTest, CanDescribeSelf) {
2721 const Matcher<int*> m = Pointee(Gt(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002722 EXPECT_EQ("points to a value that is > 3", Describe(m));
2723 EXPECT_EQ("does not point to a value that is > 3",
shiqiane35fdd92008-12-10 05:08:54 +00002724 DescribeNegation(m));
2725}
2726
shiqiane35fdd92008-12-10 05:08:54 +00002727TEST(PointeeTest, CanExplainMatchResult) {
2728 const Matcher<const string*> m = Pointee(StartsWith("Hi"));
2729
2730 EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL)));
2731
2732 const Matcher<int*> m2 = Pointee(GreaterThan(1));
2733 int n = 3;
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002734 EXPECT_EQ("which points to 3, which is 2 more than 1",
2735 Explain(m2, &n));
2736}
2737
2738TEST(PointeeTest, AlwaysExplainsPointee) {
2739 const Matcher<int*> m = Pointee(0);
2740 int n = 42;
2741 EXPECT_EQ("which points to 42", Explain(m, &n));
shiqiane35fdd92008-12-10 05:08:54 +00002742}
2743
2744// An uncopyable class.
2745class Uncopyable {
2746 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002747 explicit Uncopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00002748
2749 int value() const { return value_; }
2750 private:
2751 const int value_;
2752 GTEST_DISALLOW_COPY_AND_ASSIGN_(Uncopyable);
2753};
2754
2755// Returns true iff x.value() is positive.
2756bool ValueIsPositive(const Uncopyable& x) { return x.value() > 0; }
2757
2758// A user-defined struct for testing Field().
2759struct AStruct {
2760 AStruct() : x(0), y(1.0), z(5), p(NULL) {}
2761 AStruct(const AStruct& rhs)
2762 : x(rhs.x), y(rhs.y), z(rhs.z.value()), p(rhs.p) {}
2763
2764 int x; // A non-const field.
2765 const double y; // A const field.
2766 Uncopyable z; // An uncopyable field.
2767 const char* p; // A pointer field.
zhanyong.wan32de5f52009-12-23 00:13:23 +00002768
2769 private:
2770 GTEST_DISALLOW_ASSIGN_(AStruct);
shiqiane35fdd92008-12-10 05:08:54 +00002771};
2772
2773// A derived struct for testing Field().
2774struct DerivedStruct : public AStruct {
2775 char ch;
zhanyong.wan32de5f52009-12-23 00:13:23 +00002776
2777 private:
2778 GTEST_DISALLOW_ASSIGN_(DerivedStruct);
shiqiane35fdd92008-12-10 05:08:54 +00002779};
2780
2781// Tests that Field(&Foo::field, ...) works when field is non-const.
2782TEST(FieldTest, WorksForNonConstField) {
2783 Matcher<AStruct> m = Field(&AStruct::x, Ge(0));
2784
2785 AStruct a;
2786 EXPECT_TRUE(m.Matches(a));
2787 a.x = -1;
2788 EXPECT_FALSE(m.Matches(a));
2789}
2790
2791// Tests that Field(&Foo::field, ...) works when field is const.
2792TEST(FieldTest, WorksForConstField) {
2793 AStruct a;
2794
2795 Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0));
2796 EXPECT_TRUE(m.Matches(a));
2797 m = Field(&AStruct::y, Le(0.0));
2798 EXPECT_FALSE(m.Matches(a));
2799}
2800
2801// Tests that Field(&Foo::field, ...) works when field is not copyable.
2802TEST(FieldTest, WorksForUncopyableField) {
2803 AStruct a;
2804
2805 Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive));
2806 EXPECT_TRUE(m.Matches(a));
2807 m = Field(&AStruct::z, Not(Truly(ValueIsPositive)));
2808 EXPECT_FALSE(m.Matches(a));
2809}
2810
2811// Tests that Field(&Foo::field, ...) works when field is a pointer.
2812TEST(FieldTest, WorksForPointerField) {
2813 // Matching against NULL.
2814 Matcher<AStruct> m = Field(&AStruct::p, static_cast<const char*>(NULL));
2815 AStruct a;
2816 EXPECT_TRUE(m.Matches(a));
2817 a.p = "hi";
2818 EXPECT_FALSE(m.Matches(a));
2819
2820 // Matching a pointer that is not NULL.
2821 m = Field(&AStruct::p, StartsWith("hi"));
2822 a.p = "hill";
2823 EXPECT_TRUE(m.Matches(a));
2824 a.p = "hole";
2825 EXPECT_FALSE(m.Matches(a));
2826}
2827
2828// Tests that Field() works when the object is passed by reference.
2829TEST(FieldTest, WorksForByRefArgument) {
2830 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2831
2832 AStruct a;
2833 EXPECT_TRUE(m.Matches(a));
2834 a.x = -1;
2835 EXPECT_FALSE(m.Matches(a));
2836}
2837
2838// Tests that Field(&Foo::field, ...) works when the argument's type
2839// is a sub-type of Foo.
2840TEST(FieldTest, WorksForArgumentOfSubType) {
2841 // Note that the matcher expects DerivedStruct but we say AStruct
2842 // inside Field().
2843 Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0));
2844
2845 DerivedStruct d;
2846 EXPECT_TRUE(m.Matches(d));
2847 d.x = -1;
2848 EXPECT_FALSE(m.Matches(d));
2849}
2850
2851// Tests that Field(&Foo::field, m) works when field's type and m's
2852// argument type are compatible but not the same.
2853TEST(FieldTest, WorksForCompatibleMatcherType) {
2854 // The field is an int, but the inner matcher expects a signed char.
2855 Matcher<const AStruct&> m = Field(&AStruct::x,
2856 Matcher<signed char>(Ge(0)));
2857
2858 AStruct a;
2859 EXPECT_TRUE(m.Matches(a));
2860 a.x = -1;
2861 EXPECT_FALSE(m.Matches(a));
2862}
2863
2864// Tests that Field() can describe itself.
2865TEST(FieldTest, CanDescribeSelf) {
2866 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2867
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002868 EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
2869 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00002870}
2871
2872// Tests that Field() can explain the match result.
2873TEST(FieldTest, CanExplainMatchResult) {
2874 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2875
2876 AStruct a;
2877 a.x = 1;
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002878 EXPECT_EQ("whose given field is 1", Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00002879
2880 m = Field(&AStruct::x, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002881 EXPECT_EQ("whose given field is 1, which is 1 more than 0", Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00002882}
2883
2884// Tests that Field() works when the argument is a pointer to const.
2885TEST(FieldForPointerTest, WorksForPointerToConst) {
2886 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2887
2888 AStruct a;
2889 EXPECT_TRUE(m.Matches(&a));
2890 a.x = -1;
2891 EXPECT_FALSE(m.Matches(&a));
2892}
2893
2894// Tests that Field() works when the argument is a pointer to non-const.
2895TEST(FieldForPointerTest, WorksForPointerToNonConst) {
2896 Matcher<AStruct*> m = Field(&AStruct::x, Ge(0));
2897
2898 AStruct a;
2899 EXPECT_TRUE(m.Matches(&a));
2900 a.x = -1;
2901 EXPECT_FALSE(m.Matches(&a));
2902}
2903
zhanyong.wan6953a722010-01-13 05:15:07 +00002904// Tests that Field() works when the argument is a reference to a const pointer.
2905TEST(FieldForPointerTest, WorksForReferenceToConstPointer) {
2906 Matcher<AStruct* const&> m = Field(&AStruct::x, Ge(0));
2907
2908 AStruct a;
2909 EXPECT_TRUE(m.Matches(&a));
2910 a.x = -1;
2911 EXPECT_FALSE(m.Matches(&a));
2912}
2913
shiqiane35fdd92008-12-10 05:08:54 +00002914// Tests that Field() does not match the NULL pointer.
2915TEST(FieldForPointerTest, DoesNotMatchNull) {
2916 Matcher<const AStruct*> m = Field(&AStruct::x, _);
2917 EXPECT_FALSE(m.Matches(NULL));
2918}
2919
2920// Tests that Field(&Foo::field, ...) works when the argument's type
2921// is a sub-type of const Foo*.
2922TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
2923 // Note that the matcher expects DerivedStruct but we say AStruct
2924 // inside Field().
2925 Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0));
2926
2927 DerivedStruct d;
2928 EXPECT_TRUE(m.Matches(&d));
2929 d.x = -1;
2930 EXPECT_FALSE(m.Matches(&d));
2931}
2932
2933// Tests that Field() can describe itself when used to match a pointer.
2934TEST(FieldForPointerTest, CanDescribeSelf) {
2935 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2936
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002937 EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
2938 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00002939}
2940
2941// Tests that Field() can explain the result of matching a pointer.
2942TEST(FieldForPointerTest, CanExplainMatchResult) {
2943 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2944
2945 AStruct a;
2946 a.x = 1;
2947 EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(NULL)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002948 EXPECT_EQ("which points to an object whose given field is 1", Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00002949
2950 m = Field(&AStruct::x, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002951 EXPECT_EQ("which points to an object whose given field is 1, "
2952 "which is 1 more than 0", Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00002953}
2954
2955// A user-defined class for testing Property().
2956class AClass {
2957 public:
2958 AClass() : n_(0) {}
2959
2960 // A getter that returns a non-reference.
2961 int n() const { return n_; }
2962
2963 void set_n(int new_n) { n_ = new_n; }
2964
2965 // A getter that returns a reference to const.
2966 const string& s() const { return s_; }
2967
2968 void set_s(const string& new_s) { s_ = new_s; }
2969
2970 // A getter that returns a reference to non-const.
2971 double& x() const { return x_; }
2972 private:
2973 int n_;
2974 string s_;
2975
2976 static double x_;
2977};
2978
2979double AClass::x_ = 0.0;
2980
2981// A derived class for testing Property().
2982class DerivedClass : public AClass {
2983 private:
2984 int k_;
2985};
2986
2987// Tests that Property(&Foo::property, ...) works when property()
2988// returns a non-reference.
2989TEST(PropertyTest, WorksForNonReferenceProperty) {
2990 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
2991
2992 AClass a;
2993 a.set_n(1);
2994 EXPECT_TRUE(m.Matches(a));
2995
2996 a.set_n(-1);
2997 EXPECT_FALSE(m.Matches(a));
2998}
2999
3000// Tests that Property(&Foo::property, ...) works when property()
3001// returns a reference to const.
3002TEST(PropertyTest, WorksForReferenceToConstProperty) {
3003 Matcher<const AClass&> m = Property(&AClass::s, StartsWith("hi"));
3004
3005 AClass a;
3006 a.set_s("hill");
3007 EXPECT_TRUE(m.Matches(a));
3008
3009 a.set_s("hole");
3010 EXPECT_FALSE(m.Matches(a));
3011}
3012
3013// Tests that Property(&Foo::property, ...) works when property()
3014// returns a reference to non-const.
3015TEST(PropertyTest, WorksForReferenceToNonConstProperty) {
3016 double x = 0.0;
3017 AClass a;
3018
3019 Matcher<const AClass&> m = Property(&AClass::x, Ref(x));
3020 EXPECT_FALSE(m.Matches(a));
3021
3022 m = Property(&AClass::x, Not(Ref(x)));
3023 EXPECT_TRUE(m.Matches(a));
3024}
3025
3026// Tests that Property(&Foo::property, ...) works when the argument is
3027// passed by value.
3028TEST(PropertyTest, WorksForByValueArgument) {
3029 Matcher<AClass> m = Property(&AClass::s, StartsWith("hi"));
3030
3031 AClass a;
3032 a.set_s("hill");
3033 EXPECT_TRUE(m.Matches(a));
3034
3035 a.set_s("hole");
3036 EXPECT_FALSE(m.Matches(a));
3037}
3038
3039// Tests that Property(&Foo::property, ...) works when the argument's
3040// type is a sub-type of Foo.
3041TEST(PropertyTest, WorksForArgumentOfSubType) {
3042 // The matcher expects a DerivedClass, but inside the Property() we
3043 // say AClass.
3044 Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0));
3045
3046 DerivedClass d;
3047 d.set_n(1);
3048 EXPECT_TRUE(m.Matches(d));
3049
3050 d.set_n(-1);
3051 EXPECT_FALSE(m.Matches(d));
3052}
3053
3054// Tests that Property(&Foo::property, m) works when property()'s type
3055// and m's argument type are compatible but different.
3056TEST(PropertyTest, WorksForCompatibleMatcherType) {
3057 // n() returns an int but the inner matcher expects a signed char.
3058 Matcher<const AClass&> m = Property(&AClass::n,
3059 Matcher<signed char>(Ge(0)));
3060
3061 AClass a;
3062 EXPECT_TRUE(m.Matches(a));
3063 a.set_n(-1);
3064 EXPECT_FALSE(m.Matches(a));
3065}
3066
3067// Tests that Property() can describe itself.
3068TEST(PropertyTest, CanDescribeSelf) {
3069 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3070
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003071 EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
3072 EXPECT_EQ("is an object whose given property isn't >= 0",
3073 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003074}
3075
3076// Tests that Property() can explain the match result.
3077TEST(PropertyTest, CanExplainMatchResult) {
3078 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3079
3080 AClass a;
3081 a.set_n(1);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003082 EXPECT_EQ("whose given property is 1", Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003083
3084 m = Property(&AClass::n, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003085 EXPECT_EQ("whose given property is 1, which is 1 more than 0", Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003086}
3087
3088// Tests that Property() works when the argument is a pointer to const.
3089TEST(PropertyForPointerTest, WorksForPointerToConst) {
3090 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3091
3092 AClass a;
3093 a.set_n(1);
3094 EXPECT_TRUE(m.Matches(&a));
3095
3096 a.set_n(-1);
3097 EXPECT_FALSE(m.Matches(&a));
3098}
3099
3100// Tests that Property() works when the argument is a pointer to non-const.
3101TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
3102 Matcher<AClass*> m = Property(&AClass::s, StartsWith("hi"));
3103
3104 AClass a;
3105 a.set_s("hill");
3106 EXPECT_TRUE(m.Matches(&a));
3107
3108 a.set_s("hole");
3109 EXPECT_FALSE(m.Matches(&a));
3110}
3111
zhanyong.wan6953a722010-01-13 05:15:07 +00003112// Tests that Property() works when the argument is a reference to a
3113// const pointer.
3114TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) {
3115 Matcher<AClass* const&> m = Property(&AClass::s, StartsWith("hi"));
3116
3117 AClass a;
3118 a.set_s("hill");
3119 EXPECT_TRUE(m.Matches(&a));
3120
3121 a.set_s("hole");
3122 EXPECT_FALSE(m.Matches(&a));
3123}
3124
shiqiane35fdd92008-12-10 05:08:54 +00003125// Tests that Property() does not match the NULL pointer.
3126TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
3127 Matcher<const AClass*> m = Property(&AClass::x, _);
3128 EXPECT_FALSE(m.Matches(NULL));
3129}
3130
3131// Tests that Property(&Foo::property, ...) works when the argument's
3132// type is a sub-type of const Foo*.
3133TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
3134 // The matcher expects a DerivedClass, but inside the Property() we
3135 // say AClass.
3136 Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0));
3137
3138 DerivedClass d;
3139 d.set_n(1);
3140 EXPECT_TRUE(m.Matches(&d));
3141
3142 d.set_n(-1);
3143 EXPECT_FALSE(m.Matches(&d));
3144}
3145
3146// Tests that Property() can describe itself when used to match a pointer.
3147TEST(PropertyForPointerTest, CanDescribeSelf) {
3148 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3149
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003150 EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
3151 EXPECT_EQ("is an object whose given property isn't >= 0",
3152 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003153}
3154
3155// Tests that Property() can explain the result of matching a pointer.
3156TEST(PropertyForPointerTest, CanExplainMatchResult) {
3157 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3158
3159 AClass a;
3160 a.set_n(1);
3161 EXPECT_EQ("", Explain(m, static_cast<const AClass*>(NULL)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003162 EXPECT_EQ("which points to an object whose given property is 1",
3163 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003164
3165 m = Property(&AClass::n, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003166 EXPECT_EQ("which points to an object whose given property is 1, "
3167 "which is 1 more than 0", Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003168}
3169
3170// Tests ResultOf.
3171
3172// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3173// function pointer.
3174string IntToStringFunction(int input) { return input == 1 ? "foo" : "bar"; }
3175
3176TEST(ResultOfTest, WorksForFunctionPointers) {
3177 Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(string("foo")));
3178
3179 EXPECT_TRUE(matcher.Matches(1));
3180 EXPECT_FALSE(matcher.Matches(2));
3181}
3182
3183// Tests that ResultOf() can describe itself.
3184TEST(ResultOfTest, CanDescribeItself) {
3185 Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo"));
3186
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003187 EXPECT_EQ("is mapped by the given callable to a value that "
3188 "is equal to \"foo\"", Describe(matcher));
3189 EXPECT_EQ("is mapped by the given callable to a value that "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003190 "isn't equal to \"foo\"", DescribeNegation(matcher));
shiqiane35fdd92008-12-10 05:08:54 +00003191}
3192
3193// Tests that ResultOf() can explain the match result.
3194int IntFunction(int input) { return input == 42 ? 80 : 90; }
3195
3196TEST(ResultOfTest, CanExplainMatchResult) {
3197 Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003198 EXPECT_EQ("which is mapped by the given callable to 90",
3199 Explain(matcher, 36));
shiqiane35fdd92008-12-10 05:08:54 +00003200
3201 matcher = ResultOf(&IntFunction, GreaterThan(85));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003202 EXPECT_EQ("which is mapped by the given callable to 90, "
3203 "which is 5 more than 85", Explain(matcher, 36));
shiqiane35fdd92008-12-10 05:08:54 +00003204}
3205
3206// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3207// returns a non-reference.
3208TEST(ResultOfTest, WorksForNonReferenceResults) {
3209 Matcher<int> matcher = ResultOf(&IntFunction, Eq(80));
3210
3211 EXPECT_TRUE(matcher.Matches(42));
3212 EXPECT_FALSE(matcher.Matches(36));
3213}
3214
3215// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3216// returns a reference to non-const.
3217double& DoubleFunction(double& input) { return input; }
3218
3219Uncopyable& RefUncopyableFunction(Uncopyable& obj) {
3220 return obj;
3221}
3222
3223TEST(ResultOfTest, WorksForReferenceToNonConstResults) {
3224 double x = 3.14;
3225 double x2 = x;
3226 Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x));
3227
3228 EXPECT_TRUE(matcher.Matches(x));
3229 EXPECT_FALSE(matcher.Matches(x2));
3230
3231 // Test that ResultOf works with uncopyable objects
3232 Uncopyable obj(0);
3233 Uncopyable obj2(0);
3234 Matcher<Uncopyable&> matcher2 =
3235 ResultOf(&RefUncopyableFunction, Ref(obj));
3236
3237 EXPECT_TRUE(matcher2.Matches(obj));
3238 EXPECT_FALSE(matcher2.Matches(obj2));
3239}
3240
3241// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3242// returns a reference to const.
3243const string& StringFunction(const string& input) { return input; }
3244
3245TEST(ResultOfTest, WorksForReferenceToConstResults) {
3246 string s = "foo";
3247 string s2 = s;
3248 Matcher<const string&> matcher = ResultOf(&StringFunction, Ref(s));
3249
3250 EXPECT_TRUE(matcher.Matches(s));
3251 EXPECT_FALSE(matcher.Matches(s2));
3252}
3253
3254// Tests that ResultOf(f, m) works when f(x) and m's
3255// argument types are compatible but different.
3256TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {
3257 // IntFunction() returns int but the inner matcher expects a signed char.
3258 Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85)));
3259
3260 EXPECT_TRUE(matcher.Matches(36));
3261 EXPECT_FALSE(matcher.Matches(42));
3262}
3263
shiqiane35fdd92008-12-10 05:08:54 +00003264// Tests that the program aborts when ResultOf is passed
3265// a NULL function pointer.
3266TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +00003267 EXPECT_DEATH_IF_SUPPORTED(
shiqiane35fdd92008-12-10 05:08:54 +00003268 ResultOf(static_cast<string(*)(int)>(NULL), Eq(string("foo"))),
3269 "NULL function pointer is passed into ResultOf\\(\\)\\.");
3270}
shiqiane35fdd92008-12-10 05:08:54 +00003271
3272// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3273// function reference.
3274TEST(ResultOfTest, WorksForFunctionReferences) {
3275 Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq("foo"));
3276 EXPECT_TRUE(matcher.Matches(1));
3277 EXPECT_FALSE(matcher.Matches(2));
3278}
3279
3280// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3281// function object.
3282struct Functor : public ::std::unary_function<int, string> {
3283 result_type operator()(argument_type input) const {
3284 return IntToStringFunction(input);
3285 }
3286};
3287
3288TEST(ResultOfTest, WorksForFunctors) {
3289 Matcher<int> matcher = ResultOf(Functor(), Eq(string("foo")));
3290
3291 EXPECT_TRUE(matcher.Matches(1));
3292 EXPECT_FALSE(matcher.Matches(2));
3293}
3294
3295// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3296// functor with more then one operator() defined. ResultOf() must work
3297// for each defined operator().
3298struct PolymorphicFunctor {
3299 typedef int result_type;
3300 int operator()(int n) { return n; }
3301 int operator()(const char* s) { return static_cast<int>(strlen(s)); }
3302};
3303
3304TEST(ResultOfTest, WorksForPolymorphicFunctors) {
3305 Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5));
3306
3307 EXPECT_TRUE(matcher_int.Matches(10));
3308 EXPECT_FALSE(matcher_int.Matches(2));
3309
3310 Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5));
3311
3312 EXPECT_TRUE(matcher_string.Matches("long string"));
3313 EXPECT_FALSE(matcher_string.Matches("shrt"));
3314}
3315
3316const int* ReferencingFunction(const int& n) { return &n; }
3317
3318struct ReferencingFunctor {
3319 typedef const int* result_type;
3320 result_type operator()(const int& n) { return &n; }
3321};
3322
3323TEST(ResultOfTest, WorksForReferencingCallables) {
3324 const int n = 1;
3325 const int n2 = 1;
3326 Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n));
3327 EXPECT_TRUE(matcher2.Matches(n));
3328 EXPECT_FALSE(matcher2.Matches(n2));
3329
3330 Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n));
3331 EXPECT_TRUE(matcher3.Matches(n));
3332 EXPECT_FALSE(matcher3.Matches(n2));
3333}
3334
shiqiane35fdd92008-12-10 05:08:54 +00003335class DivisibleByImpl {
3336 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003337 explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {}
shiqiane35fdd92008-12-10 05:08:54 +00003338
zhanyong.wandb22c222010-01-28 21:52:29 +00003339 // For testing using ExplainMatchResultTo() with polymorphic matchers.
shiqiane35fdd92008-12-10 05:08:54 +00003340 template <typename T>
zhanyong.wandb22c222010-01-28 21:52:29 +00003341 bool MatchAndExplain(const T& n, MatchResultListener* listener) const {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003342 *listener << "which is " << (n % divider_) << " modulo "
zhanyong.wandb22c222010-01-28 21:52:29 +00003343 << divider_;
shiqiane35fdd92008-12-10 05:08:54 +00003344 return (n % divider_) == 0;
3345 }
3346
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003347 void DescribeTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +00003348 *os << "is divisible by " << divider_;
3349 }
3350
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003351 void DescribeNegationTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +00003352 *os << "is not divisible by " << divider_;
3353 }
3354
zhanyong.wan32de5f52009-12-23 00:13:23 +00003355 void set_divider(int a_divider) { divider_ = a_divider; }
shiqiane35fdd92008-12-10 05:08:54 +00003356 int divider() const { return divider_; }
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003357
shiqiane35fdd92008-12-10 05:08:54 +00003358 private:
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003359 int divider_;
shiqiane35fdd92008-12-10 05:08:54 +00003360};
3361
shiqiane35fdd92008-12-10 05:08:54 +00003362PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) {
3363 return MakePolymorphicMatcher(DivisibleByImpl(n));
3364}
3365
3366// Tests that when AllOf() fails, only the first failing matcher is
3367// asked to explain why.
3368TEST(ExplainMatchResultTest, AllOf_False_False) {
3369 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003370 EXPECT_EQ("which is 1 modulo 4", Explain(m, 5));
shiqiane35fdd92008-12-10 05:08:54 +00003371}
3372
3373// Tests that when AllOf() fails, only the first failing matcher is
3374// asked to explain why.
3375TEST(ExplainMatchResultTest, AllOf_False_True) {
3376 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003377 EXPECT_EQ("which is 2 modulo 4", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003378}
3379
3380// Tests that when AllOf() fails, only the first failing matcher is
3381// asked to explain why.
3382TEST(ExplainMatchResultTest, AllOf_True_False) {
3383 const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003384 EXPECT_EQ("which is 2 modulo 3", Explain(m, 5));
shiqiane35fdd92008-12-10 05:08:54 +00003385}
3386
3387// Tests that when AllOf() succeeds, all matchers are asked to explain
3388// why.
3389TEST(ExplainMatchResultTest, AllOf_True_True) {
3390 const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003391 EXPECT_EQ("which is 0 modulo 2, and which is 0 modulo 3", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003392}
3393
3394TEST(ExplainMatchResultTest, AllOf_True_True_2) {
3395 const Matcher<int> m = AllOf(Ge(2), Le(3));
3396 EXPECT_EQ("", Explain(m, 2));
3397}
3398
3399TEST(ExplainmatcherResultTest, MonomorphicMatcher) {
3400 const Matcher<int> m = GreaterThan(5);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003401 EXPECT_EQ("which is 1 more than 5", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003402}
3403
3404// The following two tests verify that values without a public copy
3405// ctor can be used as arguments to matchers like Eq(), Ge(), and etc
3406// with the help of ByRef().
3407
3408class NotCopyable {
3409 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003410 explicit NotCopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00003411
3412 int value() const { return value_; }
3413
3414 bool operator==(const NotCopyable& rhs) const {
3415 return value() == rhs.value();
3416 }
3417
3418 bool operator>=(const NotCopyable& rhs) const {
3419 return value() >= rhs.value();
3420 }
3421 private:
3422 int value_;
3423
3424 GTEST_DISALLOW_COPY_AND_ASSIGN_(NotCopyable);
3425};
3426
3427TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) {
3428 const NotCopyable const_value1(1);
3429 const Matcher<const NotCopyable&> m = Eq(ByRef(const_value1));
3430
3431 const NotCopyable n1(1), n2(2);
3432 EXPECT_TRUE(m.Matches(n1));
3433 EXPECT_FALSE(m.Matches(n2));
3434}
3435
3436TEST(ByRefTest, AllowsNotCopyableValueInMatchers) {
3437 NotCopyable value2(2);
3438 const Matcher<NotCopyable&> m = Ge(ByRef(value2));
3439
3440 NotCopyable n1(1), n2(2);
3441 EXPECT_FALSE(m.Matches(n1));
3442 EXPECT_TRUE(m.Matches(n2));
3443}
3444
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003445#if GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003446// Tests ContainerEq with different container types, and
3447// different element types.
3448
3449template <typename T>
zhanyong.wanb8243162009-06-04 05:48:20 +00003450class ContainerEqTest : public testing::Test {};
zhanyong.wan6a896b52009-01-16 01:13:50 +00003451
3452typedef testing::Types<
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003453 set<int>,
3454 vector<size_t>,
3455 multiset<size_t>,
3456 list<int> >
zhanyong.wan6a896b52009-01-16 01:13:50 +00003457 ContainerEqTestTypes;
3458
3459TYPED_TEST_CASE(ContainerEqTest, ContainerEqTestTypes);
3460
3461// Tests that the filled container is equal to itself.
3462TYPED_TEST(ContainerEqTest, EqualsSelf) {
3463 static const int vals[] = {1, 1, 2, 3, 5, 8};
3464 TypeParam my_set(vals, vals + 6);
3465 const Matcher<TypeParam> m = ContainerEq(my_set);
3466 EXPECT_TRUE(m.Matches(my_set));
3467 EXPECT_EQ("", Explain(m, my_set));
3468}
3469
3470// Tests that missing values are reported.
3471TYPED_TEST(ContainerEqTest, ValueMissing) {
3472 static const int vals[] = {1, 1, 2, 3, 5, 8};
3473 static const int test_vals[] = {2, 1, 8, 5};
3474 TypeParam my_set(vals, vals + 6);
3475 TypeParam test_set(test_vals, test_vals + 4);
3476 const Matcher<TypeParam> m = ContainerEq(my_set);
3477 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003478 EXPECT_EQ("which doesn't have these expected elements: 3",
3479 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003480}
3481
3482// Tests that added values are reported.
3483TYPED_TEST(ContainerEqTest, ValueAdded) {
3484 static const int vals[] = {1, 1, 2, 3, 5, 8};
3485 static const int test_vals[] = {1, 2, 3, 5, 8, 46};
3486 TypeParam my_set(vals, vals + 6);
3487 TypeParam test_set(test_vals, test_vals + 6);
3488 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3489 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003490 EXPECT_EQ("which has these unexpected elements: 46", Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003491}
3492
3493// Tests that added and missing values are reported together.
3494TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) {
3495 static const int vals[] = {1, 1, 2, 3, 5, 8};
3496 static const int test_vals[] = {1, 2, 3, 8, 46};
3497 TypeParam my_set(vals, vals + 6);
3498 TypeParam test_set(test_vals, test_vals + 5);
3499 const Matcher<TypeParam> m = ContainerEq(my_set);
3500 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003501 EXPECT_EQ("which has these unexpected elements: 46,\n"
3502 "and doesn't have these expected elements: 5",
3503 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003504}
3505
3506// Tests duplicated value -- expect no explanation.
3507TYPED_TEST(ContainerEqTest, DuplicateDifference) {
3508 static const int vals[] = {1, 1, 2, 3, 5, 8};
3509 static const int test_vals[] = {1, 2, 3, 5, 8};
3510 TypeParam my_set(vals, vals + 6);
3511 TypeParam test_set(test_vals, test_vals + 5);
3512 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3513 // Depending on the container, match may be true or false
3514 // But in any case there should be no explanation.
3515 EXPECT_EQ("", Explain(m, test_set));
3516}
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003517#endif // GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003518
3519// Tests that mutliple missing values are reported.
3520// Using just vector here, so order is predicatble.
3521TEST(ContainerEqExtraTest, MultipleValuesMissing) {
3522 static const int vals[] = {1, 1, 2, 3, 5, 8};
3523 static const int test_vals[] = {2, 1, 5};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003524 vector<int> my_set(vals, vals + 6);
3525 vector<int> test_set(test_vals, test_vals + 3);
3526 const Matcher<vector<int> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003527 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003528 EXPECT_EQ("which doesn't have these expected elements: 3, 8",
3529 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003530}
3531
3532// Tests that added values are reported.
3533// Using just vector here, so order is predicatble.
3534TEST(ContainerEqExtraTest, MultipleValuesAdded) {
3535 static const int vals[] = {1, 1, 2, 3, 5, 8};
3536 static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003537 list<size_t> my_set(vals, vals + 6);
3538 list<size_t> test_set(test_vals, test_vals + 7);
3539 const Matcher<const list<size_t>&> m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003540 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003541 EXPECT_EQ("which has these unexpected elements: 92, 46",
3542 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003543}
3544
3545// Tests that added and missing values are reported together.
3546TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) {
3547 static const int vals[] = {1, 1, 2, 3, 5, 8};
3548 static const int test_vals[] = {1, 2, 3, 92, 46};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003549 list<size_t> my_set(vals, vals + 6);
3550 list<size_t> test_set(test_vals, test_vals + 5);
3551 const Matcher<const list<size_t> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003552 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003553 EXPECT_EQ("which has these unexpected elements: 92, 46,\n"
3554 "and doesn't have these expected elements: 5, 8",
zhanyong.wan6a896b52009-01-16 01:13:50 +00003555 Explain(m, test_set));
3556}
3557
3558// Tests to see that duplicate elements are detected,
3559// but (as above) not reported in the explanation.
3560TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) {
3561 static const int vals[] = {1, 1, 2, 3, 5, 8};
3562 static const int test_vals[] = {1, 2, 3, 5, 8};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003563 vector<int> my_set(vals, vals + 6);
3564 vector<int> test_set(test_vals, test_vals + 5);
3565 const Matcher<vector<int> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003566 EXPECT_TRUE(m.Matches(my_set));
3567 EXPECT_FALSE(m.Matches(test_set));
3568 // There is nothing to report when both sets contain all the same values.
3569 EXPECT_EQ("", Explain(m, test_set));
3570}
3571
3572// Tests that ContainerEq works for non-trivial associative containers,
3573// like maps.
3574TEST(ContainerEqExtraTest, WorksForMaps) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003575 map<int, std::string> my_map;
zhanyong.wan6a896b52009-01-16 01:13:50 +00003576 my_map[0] = "a";
3577 my_map[1] = "b";
3578
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003579 map<int, std::string> test_map;
zhanyong.wan6a896b52009-01-16 01:13:50 +00003580 test_map[0] = "aa";
3581 test_map[1] = "b";
3582
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003583 const Matcher<const map<int, std::string>&> m = ContainerEq(my_map);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003584 EXPECT_TRUE(m.Matches(my_map));
3585 EXPECT_FALSE(m.Matches(test_map));
3586
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003587 EXPECT_EQ("which has these unexpected elements: (0, \"aa\"),\n"
3588 "and doesn't have these expected elements: (0, \"a\")",
zhanyong.wan6a896b52009-01-16 01:13:50 +00003589 Explain(m, test_map));
3590}
3591
zhanyong.wanb8243162009-06-04 05:48:20 +00003592TEST(ContainerEqExtraTest, WorksForNativeArray) {
3593 int a1[] = { 1, 2, 3 };
3594 int a2[] = { 1, 2, 3 };
3595 int b[] = { 1, 2, 4 };
3596
3597 EXPECT_THAT(a1, ContainerEq(a2));
3598 EXPECT_THAT(a1, Not(ContainerEq(b)));
3599}
3600
3601TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) {
3602 const char a1[][3] = { "hi", "lo" };
3603 const char a2[][3] = { "hi", "lo" };
3604 const char b[][3] = { "lo", "hi" };
3605
3606 // Tests using ContainerEq() in the first dimension.
3607 EXPECT_THAT(a1, ContainerEq(a2));
3608 EXPECT_THAT(a1, Not(ContainerEq(b)));
3609
3610 // Tests using ContainerEq() in the second dimension.
3611 EXPECT_THAT(a1, ElementsAre(ContainerEq(a2[0]), ContainerEq(a2[1])));
3612 EXPECT_THAT(a1, ElementsAre(Not(ContainerEq(b[0])), ContainerEq(a2[1])));
3613}
3614
3615TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) {
3616 const int a1[] = { 1, 2, 3 };
3617 const int a2[] = { 1, 2, 3 };
3618 const int b[] = { 1, 2, 3, 4 };
3619
zhanyong.wan2661c682009-06-09 05:42:12 +00003620 const int* const p1 = a1;
3621 EXPECT_THAT(make_tuple(p1, 3), ContainerEq(a2));
3622 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(b)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003623
3624 const int c[] = { 1, 3, 2 };
zhanyong.wan2661c682009-06-09 05:42:12 +00003625 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(c)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003626}
3627
3628TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) {
3629 std::string a1[][3] = {
3630 { "hi", "hello", "ciao" },
3631 { "bye", "see you", "ciao" }
3632 };
3633
3634 std::string a2[][3] = {
3635 { "hi", "hello", "ciao" },
3636 { "bye", "see you", "ciao" }
3637 };
3638
3639 const Matcher<const std::string(&)[2][3]> m = ContainerEq(a2);
3640 EXPECT_THAT(a1, m);
3641
3642 a2[0][0] = "ha";
3643 EXPECT_THAT(a1, m);
3644}
3645
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003646// Tests GetParamIndex().
3647
3648TEST(GetParamIndexTest, WorksForEmptyParamList) {
3649 const char* params[] = { NULL };
3650 EXPECT_EQ(kTupleInterpolation, GetParamIndex(params, "*"));
3651 EXPECT_EQ(kInvalidInterpolation, GetParamIndex(params, "a"));
3652}
3653
3654TEST(GetParamIndexTest, RecognizesStar) {
3655 const char* params[] = { "a", "b", NULL };
3656 EXPECT_EQ(kTupleInterpolation, GetParamIndex(params, "*"));
3657}
3658
3659TEST(GetParamIndexTest, RecognizesKnownParam) {
3660 const char* params[] = { "foo", "bar", NULL };
3661 EXPECT_EQ(0, GetParamIndex(params, "foo"));
3662 EXPECT_EQ(1, GetParamIndex(params, "bar"));
3663}
3664
3665TEST(GetParamIndexTest, RejectsUnknownParam) {
3666 const char* params[] = { "foo", "bar", NULL };
3667 EXPECT_EQ(kInvalidInterpolation, GetParamIndex(params, "foobar"));
3668}
3669
3670// Tests SkipPrefix().
3671
3672TEST(SkipPrefixTest, SkipsWhenPrefixMatches) {
3673 const char* const str = "hello";
3674
3675 const char* p = str;
3676 EXPECT_TRUE(SkipPrefix("", &p));
3677 EXPECT_EQ(str, p);
3678
3679 p = str;
3680 EXPECT_TRUE(SkipPrefix("hell", &p));
3681 EXPECT_EQ(str + 4, p);
3682}
3683
3684TEST(SkipPrefixTest, DoesNotSkipWhenPrefixDoesNotMatch) {
3685 const char* const str = "world";
3686
3687 const char* p = str;
3688 EXPECT_FALSE(SkipPrefix("W", &p));
3689 EXPECT_EQ(str, p);
3690
3691 p = str;
3692 EXPECT_FALSE(SkipPrefix("world!", &p));
3693 EXPECT_EQ(str, p);
3694}
3695
3696// Tests FormatMatcherDescriptionSyntaxError().
3697TEST(FormatMatcherDescriptionSyntaxErrorTest, FormatsCorrectly) {
3698 const char* const description = "hello%world";
3699 EXPECT_EQ("Syntax error at index 5 in matcher description \"hello%world\": ",
3700 FormatMatcherDescriptionSyntaxError(description, description + 5));
3701}
3702
3703// Tests ValidateMatcherDescription().
3704
3705TEST(ValidateMatcherDescriptionTest, AcceptsEmptyDescription) {
3706 const char* params[] = { "foo", "bar", NULL };
3707 EXPECT_THAT(ValidateMatcherDescription(params, ""),
3708 ElementsAre());
3709}
3710
3711TEST(ValidateMatcherDescriptionTest,
3712 AcceptsNonEmptyDescriptionWithNoInterpolation) {
3713 const char* params[] = { "foo", "bar", NULL };
3714 EXPECT_THAT(ValidateMatcherDescription(params, "a simple description"),
3715 ElementsAre());
3716}
3717
zhanyong.wan82113312010-01-08 21:55:40 +00003718// The MATCHER*() macros trigger warning C4100 (unreferenced formal
3719// parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
3720// the macro definition, as the warnings are generated when the macro
3721// is expanded and macro expansion cannot contain #pragma. Therefore
3722// we suppress them here.
3723#ifdef _MSC_VER
3724#pragma warning(push)
3725#pragma warning(disable:4100)
3726#endif
3727
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003728// We use MATCHER_P3() to define a matcher for testing
3729// ValidateMatcherDescription(); otherwise we'll end up with much
3730// plumbing code. This is not circular as
3731// ValidateMatcherDescription() doesn't affect whether the matcher
3732// matches a value or not.
3733MATCHER_P3(EqInterpolation, start, end, index, "equals Interpolation%(*)s") {
3734 return arg.start_pos == start && arg.end_pos == end &&
3735 arg.param_index == index;
3736}
3737
zhanyong.wan82113312010-01-08 21:55:40 +00003738#ifdef _MSC_VER
3739#pragma warning(pop)
3740#endif
3741
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003742TEST(ValidateMatcherDescriptionTest, AcceptsPercentInterpolation) {
3743 const char* params[] = { "foo", NULL };
3744 const char* const desc = "one %%";
3745 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3746 ElementsAre(EqInterpolation(desc + 4, desc + 6,
3747 kPercentInterpolation)));
3748}
3749
3750TEST(ValidateMatcherDescriptionTest, AcceptsTupleInterpolation) {
3751 const char* params[] = { "foo", "bar", "baz", NULL };
3752 const char* const desc = "%(*)s after";
3753 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3754 ElementsAre(EqInterpolation(desc, desc + 5,
3755 kTupleInterpolation)));
3756}
3757
3758TEST(ValidateMatcherDescriptionTest, AcceptsParamInterpolation) {
3759 const char* params[] = { "foo", "bar", "baz", NULL };
3760 const char* const desc = "a %(bar)s.";
3761 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3762 ElementsAre(EqInterpolation(desc + 2, desc + 9, 1)));
3763}
3764
3765TEST(ValidateMatcherDescriptionTest, AcceptsMultiplenterpolations) {
3766 const char* params[] = { "foo", "bar", "baz", NULL };
3767 const char* const desc = "%(baz)s %(foo)s %(bar)s";
3768 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3769 ElementsAre(EqInterpolation(desc, desc + 7, 2),
3770 EqInterpolation(desc + 8, desc + 15, 0),
3771 EqInterpolation(desc + 16, desc + 23, 1)));
3772}
3773
3774TEST(ValidateMatcherDescriptionTest, AcceptsRepeatedParams) {
3775 const char* params[] = { "foo", "bar", NULL };
3776 const char* const desc = "%(foo)s and %(foo)s";
3777 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3778 ElementsAre(EqInterpolation(desc, desc + 7, 0),
3779 EqInterpolation(desc + 12, desc + 19, 0)));
3780}
3781
3782TEST(ValidateMatcherDescriptionTest, RejectsUnknownParam) {
3783 const char* params[] = { "a", "bar", NULL };
3784 EXPECT_NONFATAL_FAILURE({
3785 EXPECT_THAT(ValidateMatcherDescription(params, "%(foo)s"),
3786 ElementsAre());
3787 }, "Syntax error at index 2 in matcher description \"%(foo)s\": "
3788 "\"foo\" is an invalid parameter name.");
3789}
3790
3791TEST(ValidateMatcherDescriptionTest, RejectsUnfinishedParam) {
3792 const char* params[] = { "a", "bar", NULL };
3793 EXPECT_NONFATAL_FAILURE({
3794 EXPECT_THAT(ValidateMatcherDescription(params, "%(foo)"),
3795 ElementsAre());
3796 }, "Syntax error at index 0 in matcher description \"%(foo)\": "
3797 "an interpolation must end with \")s\", but \"%(foo)\" does not.");
3798
3799 EXPECT_NONFATAL_FAILURE({
3800 EXPECT_THAT(ValidateMatcherDescription(params, "x%(a"),
3801 ElementsAre());
3802 }, "Syntax error at index 1 in matcher description \"x%(a\": "
3803 "an interpolation must end with \")s\", but \"%(a\" does not.");
3804}
3805
3806TEST(ValidateMatcherDescriptionTest, RejectsSinglePercent) {
3807 const char* params[] = { "a", NULL };
3808 EXPECT_NONFATAL_FAILURE({
3809 EXPECT_THAT(ValidateMatcherDescription(params, "a %."),
3810 ElementsAre());
3811 }, "Syntax error at index 2 in matcher description \"a %.\": "
3812 "use \"%%\" instead of \"%\" to print \"%\".");
3813
3814}
3815
3816// Tests JoinAsTuple().
3817
3818TEST(JoinAsTupleTest, JoinsEmptyTuple) {
3819 EXPECT_EQ("", JoinAsTuple(Strings()));
3820}
3821
3822TEST(JoinAsTupleTest, JoinsOneTuple) {
3823 const char* fields[] = { "1" };
3824 EXPECT_EQ("1", JoinAsTuple(Strings(fields, fields + 1)));
3825}
3826
3827TEST(JoinAsTupleTest, JoinsTwoTuple) {
3828 const char* fields[] = { "1", "a" };
3829 EXPECT_EQ("(1, a)", JoinAsTuple(Strings(fields, fields + 2)));
3830}
3831
3832TEST(JoinAsTupleTest, JoinsTenTuple) {
3833 const char* fields[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
3834 EXPECT_EQ("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)",
3835 JoinAsTuple(Strings(fields, fields + 10)));
3836}
3837
3838// Tests FormatMatcherDescription().
3839
3840TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) {
3841 EXPECT_EQ("is even",
3842 FormatMatcherDescription("IsEven", "", Interpolations(),
3843 Strings()));
3844
3845 const char* params[] = { "5" };
3846 EXPECT_EQ("equals 5",
3847 FormatMatcherDescription("Equals", "", Interpolations(),
3848 Strings(params, params + 1)));
3849
3850 const char* params2[] = { "5", "8" };
3851 EXPECT_EQ("is in range (5, 8)",
3852 FormatMatcherDescription("IsInRange", "", Interpolations(),
3853 Strings(params2, params2 + 2)));
3854}
3855
3856TEST(FormatMatcherDescriptionTest, WorksForDescriptionWithNoInterpolation) {
3857 EXPECT_EQ("is positive",
3858 FormatMatcherDescription("Gt0", "is positive", Interpolations(),
3859 Strings()));
3860
3861 const char* params[] = { "5", "6" };
3862 EXPECT_EQ("is negative",
3863 FormatMatcherDescription("Lt0", "is negative", Interpolations(),
3864 Strings(params, params + 2)));
3865}
3866
3867TEST(FormatMatcherDescriptionTest,
3868 WorksWhenDescriptionStartsWithInterpolation) {
3869 const char* params[] = { "5" };
3870 const char* const desc = "%(num)s times bigger";
3871 const Interpolation interp[] = { Interpolation(desc, desc + 7, 0) };
3872 EXPECT_EQ("5 times bigger",
3873 FormatMatcherDescription("Foo", desc,
3874 Interpolations(interp, interp + 1),
3875 Strings(params, params + 1)));
3876}
3877
3878TEST(FormatMatcherDescriptionTest,
3879 WorksWhenDescriptionEndsWithInterpolation) {
3880 const char* params[] = { "5", "6" };
3881 const char* const desc = "is bigger than %(y)s";
3882 const Interpolation interp[] = { Interpolation(desc + 15, desc + 20, 1) };
3883 EXPECT_EQ("is bigger than 6",
3884 FormatMatcherDescription("Foo", desc,
3885 Interpolations(interp, interp + 1),
3886 Strings(params, params + 2)));
3887}
3888
3889TEST(FormatMatcherDescriptionTest,
3890 WorksWhenDescriptionStartsAndEndsWithInterpolation) {
3891 const char* params[] = { "5", "6" };
3892 const char* const desc = "%(x)s <= arg <= %(y)s";
3893 const Interpolation interp[] = {
3894 Interpolation(desc, desc + 5, 0),
3895 Interpolation(desc + 16, desc + 21, 1)
3896 };
3897 EXPECT_EQ("5 <= arg <= 6",
3898 FormatMatcherDescription("Foo", desc,
3899 Interpolations(interp, interp + 2),
3900 Strings(params, params + 2)));
3901}
3902
3903TEST(FormatMatcherDescriptionTest,
3904 WorksWhenDescriptionDoesNotStartOrEndWithInterpolation) {
3905 const char* params[] = { "5.2" };
3906 const char* const desc = "has %(x)s cents";
3907 const Interpolation interp[] = { Interpolation(desc + 4, desc + 9, 0) };
3908 EXPECT_EQ("has 5.2 cents",
3909 FormatMatcherDescription("Foo", desc,
3910 Interpolations(interp, interp + 1),
3911 Strings(params, params + 1)));
3912}
3913
3914TEST(FormatMatcherDescriptionTest,
3915 WorksWhenDescriptionContainsMultipleInterpolations) {
3916 const char* params[] = { "5", "6" };
3917 const char* const desc = "in %(*)s or [%(x)s, %(y)s]";
3918 const Interpolation interp[] = {
3919 Interpolation(desc + 3, desc + 8, kTupleInterpolation),
3920 Interpolation(desc + 13, desc + 18, 0),
3921 Interpolation(desc + 20, desc + 25, 1)
3922 };
3923 EXPECT_EQ("in (5, 6) or [5, 6]",
3924 FormatMatcherDescription("Foo", desc,
3925 Interpolations(interp, interp + 3),
3926 Strings(params, params + 2)));
3927}
3928
3929TEST(FormatMatcherDescriptionTest,
3930 WorksWhenDescriptionContainsRepeatedParams) {
3931 const char* params[] = { "9" };
3932 const char* const desc = "in [-%(x)s, %(x)s]";
3933 const Interpolation interp[] = {
3934 Interpolation(desc + 5, desc + 10, 0),
3935 Interpolation(desc + 12, desc + 17, 0)
3936 };
3937 EXPECT_EQ("in [-9, 9]",
3938 FormatMatcherDescription("Foo", desc,
3939 Interpolations(interp, interp + 2),
3940 Strings(params, params + 1)));
3941}
3942
3943TEST(FormatMatcherDescriptionTest,
3944 WorksForDescriptionWithInvalidInterpolation) {
3945 const char* params[] = { "9" };
3946 const char* const desc = "> %(x)s %(x)";
3947 const Interpolation interp[] = { Interpolation(desc + 2, desc + 7, 0) };
3948 EXPECT_EQ("> 9 %(x)",
3949 FormatMatcherDescription("Foo", desc,
3950 Interpolations(interp, interp + 1),
3951 Strings(params, params + 1)));
3952}
3953
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003954// Tests PolymorphicMatcher::mutable_impl().
3955TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {
3956 PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
3957 DivisibleByImpl& impl = m.mutable_impl();
3958 EXPECT_EQ(42, impl.divider());
3959
3960 impl.set_divider(0);
3961 EXPECT_EQ(0, m.mutable_impl().divider());
3962}
3963
3964// Tests PolymorphicMatcher::impl().
3965TEST(PolymorphicMatcherTest, CanAccessImpl) {
3966 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
3967 const DivisibleByImpl& impl = m.impl();
3968 EXPECT_EQ(42, impl.divider());
3969}
3970
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003971TEST(MatcherTupleTest, ExplainsMatchFailure) {
3972 stringstream ss1;
3973 ExplainMatchFailureTupleTo(make_tuple(Matcher<char>(Eq('a')), GreaterThan(5)),
3974 make_tuple('a', 10), &ss1);
3975 EXPECT_EQ("", ss1.str()); // Successful match.
3976
3977 stringstream ss2;
3978 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
3979 make_tuple(2, 'b'), &ss2);
3980 EXPECT_EQ(" Expected arg #0: is > 5\n"
3981 " Actual: 2, which is 3 less than 5\n"
3982 " Expected arg #1: is equal to 'a' (97)\n"
3983 " Actual: 'b' (98)\n",
3984 ss2.str()); // Failed match where both arguments need explanation.
3985
3986 stringstream ss3;
3987 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
3988 make_tuple(2, 'a'), &ss3);
3989 EXPECT_EQ(" Expected arg #0: is > 5\n"
3990 " Actual: 2, which is 3 less than 5\n",
3991 ss3.str()); // Failed match where only one argument needs
3992 // explanation.
3993}
3994
zhanyong.wan33605ba2010-04-22 23:37:47 +00003995// Tests Each().
3996
3997TEST(EachTest, ExplainsMatchResultCorrectly) {
3998 set<int> a; // empty
3999
4000 Matcher<set<int> > m = Each(2);
4001 EXPECT_EQ("", Explain(m, a));
4002
4003 Matcher<const int(&)[1]> n = Each(1);
4004
4005 const int b[1] = { 1 };
4006 EXPECT_EQ("", Explain(n, b));
4007
4008 n = Each(3);
4009 EXPECT_EQ("whose element #0 doesn't match", Explain(n, b));
4010
4011 a.insert(1);
4012 a.insert(2);
4013 a.insert(3);
4014 m = Each(GreaterThan(0));
4015 EXPECT_EQ("", Explain(m, a));
4016
4017 m = Each(GreaterThan(10));
4018 EXPECT_EQ("whose element #0 doesn't match, which is 9 less than 10",
4019 Explain(m, a));
4020}
4021
4022TEST(EachTest, DescribesItselfCorrectly) {
4023 Matcher<vector<int> > m = Each(1);
4024 EXPECT_EQ("only contains elements that is equal to 1", Describe(m));
4025
4026 Matcher<vector<int> > m2 = Not(m);
4027 EXPECT_EQ("contains some element that isn't equal to 1", Describe(m2));
4028}
4029
4030TEST(EachTest, MatchesVectorWhenAllElementsMatch) {
4031 vector<int> some_vector;
4032 EXPECT_THAT(some_vector, Each(1));
4033 some_vector.push_back(3);
4034 EXPECT_THAT(some_vector, Not(Each(1)));
4035 EXPECT_THAT(some_vector, Each(3));
4036 some_vector.push_back(1);
4037 some_vector.push_back(2);
4038 EXPECT_THAT(some_vector, Not(Each(3)));
4039 EXPECT_THAT(some_vector, Each(Lt(3.5)));
4040
4041 vector<string> another_vector;
4042 another_vector.push_back("fee");
4043 EXPECT_THAT(another_vector, Each(string("fee")));
4044 another_vector.push_back("fie");
4045 another_vector.push_back("foe");
4046 another_vector.push_back("fum");
4047 EXPECT_THAT(another_vector, Not(Each(string("fee"))));
4048}
4049
4050TEST(EachTest, MatchesMapWhenAllElementsMatch) {
4051 map<const char*, int> my_map;
4052 const char* bar = "a string";
4053 my_map[bar] = 2;
4054 EXPECT_THAT(my_map, Each(make_pair(bar, 2)));
4055
4056 map<string, int> another_map;
4057 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1)));
4058 another_map["fee"] = 1;
4059 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1)));
4060 another_map["fie"] = 2;
4061 another_map["foe"] = 3;
4062 another_map["fum"] = 4;
4063 EXPECT_THAT(another_map, Not(Each(make_pair(string("fee"), 1))));
4064 EXPECT_THAT(another_map, Not(Each(make_pair(string("fum"), 1))));
4065 EXPECT_THAT(another_map, Each(Pair(_, Gt(0))));
4066}
4067
4068TEST(EachTest, AcceptsMatcher) {
4069 const int a[] = { 1, 2, 3 };
4070 EXPECT_THAT(a, Each(Gt(0)));
4071 EXPECT_THAT(a, Not(Each(Gt(1))));
4072}
4073
4074TEST(EachTest, WorksForNativeArrayAsTuple) {
4075 const int a[] = { 1, 2 };
4076 const int* const pointer = a;
4077 EXPECT_THAT(make_tuple(pointer, 2), Each(Gt(0)));
4078 EXPECT_THAT(make_tuple(pointer, 2), Not(Each(Gt(1))));
4079}
4080
zhanyong.wanab5b77c2010-05-17 19:32:48 +00004081// For testing Pointwise().
4082class IsHalfOfMatcher {
4083 public:
4084 template <typename T1, typename T2>
4085 bool MatchAndExplain(const tuple<T1, T2>& a_pair,
4086 MatchResultListener* listener) const {
4087 if (get<0>(a_pair) == get<1>(a_pair)/2) {
4088 *listener << "where the second is " << get<1>(a_pair);
4089 return true;
4090 } else {
4091 *listener << "where the second/2 is " << get<1>(a_pair)/2;
4092 return false;
4093 }
4094 }
4095
4096 void DescribeTo(ostream* os) const {
4097 *os << "are a pair where the first is half of the second";
4098 }
4099
4100 void DescribeNegationTo(ostream* os) const {
4101 *os << "are a pair where the first isn't half of the second";
4102 }
4103};
4104
4105PolymorphicMatcher<IsHalfOfMatcher> IsHalfOf() {
4106 return MakePolymorphicMatcher(IsHalfOfMatcher());
4107}
4108
4109TEST(PointwiseTest, DescribesSelf) {
4110 vector<int> rhs;
4111 rhs.push_back(1);
4112 rhs.push_back(2);
4113 rhs.push_back(3);
4114 const Matcher<const vector<int>&> m = Pointwise(IsHalfOf(), rhs);
4115 EXPECT_EQ("contains 3 values, where each value and its corresponding value "
4116 "in { 1, 2, 3 } are a pair where the first is half of the second",
4117 Describe(m));
4118 EXPECT_EQ("doesn't contain exactly 3 values, or contains a value x at some "
4119 "index i where x and the i-th value of { 1, 2, 3 } are a pair "
4120 "where the first isn't half of the second",
4121 DescribeNegation(m));
4122}
4123
4124TEST(PointwiseTest, MakesCopyOfRhs) {
4125 list<signed char> rhs;
4126 rhs.push_back(2);
4127 rhs.push_back(4);
4128
4129 int lhs[] = { 1, 2 };
4130 const Matcher<const int (&)[2]> m = Pointwise(IsHalfOf(), rhs);
4131 EXPECT_THAT(lhs, m);
4132
4133 // Changing rhs now shouldn't affect m, which made a copy of rhs.
4134 rhs.push_back(6);
4135 EXPECT_THAT(lhs, m);
4136}
4137
4138TEST(PointwiseTest, WorksForLhsNativeArray) {
4139 const int lhs[] = { 1, 2, 3 };
4140 vector<int> rhs;
4141 rhs.push_back(2);
4142 rhs.push_back(4);
4143 rhs.push_back(6);
4144 EXPECT_THAT(lhs, Pointwise(Lt(), rhs));
4145 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
4146}
4147
4148TEST(PointwiseTest, WorksForRhsNativeArray) {
4149 const int rhs[] = { 1, 2, 3 };
4150 vector<int> lhs;
4151 lhs.push_back(2);
4152 lhs.push_back(4);
4153 lhs.push_back(6);
4154 EXPECT_THAT(lhs, Pointwise(Gt(), rhs));
4155 EXPECT_THAT(lhs, Not(Pointwise(Lt(), rhs)));
4156}
4157
4158TEST(PointwiseTest, RejectsWrongSize) {
4159 const double lhs[2] = { 1, 2 };
4160 const int rhs[1] = { 0 };
4161 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
4162 EXPECT_EQ("which contains 2 values",
4163 Explain(Pointwise(Gt(), rhs), lhs));
4164
4165 const int rhs2[3] = { 0, 1, 2 };
4166 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs2)));
4167}
4168
4169TEST(PointwiseTest, RejectsWrongContent) {
4170 const double lhs[3] = { 1, 2, 3 };
4171 const int rhs[3] = { 2, 6, 4 };
4172 EXPECT_THAT(lhs, Not(Pointwise(IsHalfOf(), rhs)));
4173 EXPECT_EQ("where the value pair (2, 6) at index #1 don't match, "
4174 "where the second/2 is 3",
4175 Explain(Pointwise(IsHalfOf(), rhs), lhs));
4176}
4177
4178TEST(PointwiseTest, AcceptsCorrectContent) {
4179 const double lhs[3] = { 1, 2, 3 };
4180 const int rhs[3] = { 2, 4, 6 };
4181 EXPECT_THAT(lhs, Pointwise(IsHalfOf(), rhs));
4182 EXPECT_EQ("", Explain(Pointwise(IsHalfOf(), rhs), lhs));
4183}
4184
4185TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) {
4186 const double lhs[3] = { 1, 2, 3 };
4187 const int rhs[3] = { 2, 4, 6 };
4188 const Matcher<tuple<const double&, const int&> > m1 = IsHalfOf();
4189 EXPECT_THAT(lhs, Pointwise(m1, rhs));
4190 EXPECT_EQ("", Explain(Pointwise(m1, rhs), lhs));
4191
4192 // This type works as a tuple<const double&, const int&> can be
4193 // implicitly cast to tuple<double, int>.
4194 const Matcher<tuple<double, int> > m2 = IsHalfOf();
4195 EXPECT_THAT(lhs, Pointwise(m2, rhs));
4196 EXPECT_EQ("", Explain(Pointwise(m2, rhs), lhs));
4197}
4198
shiqiane35fdd92008-12-10 05:08:54 +00004199} // namespace gmock_matchers_test
4200} // namespace testing