blob: a7c217d496d98dd71e4d57b9acff6bb4fdbf87d2 [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.wanb1c7f932010-03-24 17:35:11 +000064using std::make_pair;
zhanyong.wanb5937da2009-07-16 20:26:41 +000065using std::map;
66using std::multimap;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000067using std::pair;
zhanyong.wan33605ba2010-04-22 23:37:47 +000068using std::set;
shiqiane35fdd92008-12-10 05:08:54 +000069using std::stringstream;
zhanyong.wanb8243162009-06-04 05:48:20 +000070using std::tr1::make_tuple;
zhanyong.wan33605ba2010-04-22 23:37:47 +000071using std::vector;
shiqiane35fdd92008-12-10 05:08:54 +000072using testing::A;
zhanyong.wanbf550852009-06-09 06:09:53 +000073using testing::AllArgs;
shiqiane35fdd92008-12-10 05:08:54 +000074using testing::AllOf;
75using testing::An;
76using testing::AnyOf;
77using testing::ByRef;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000078using testing::ContainsRegex;
shiqiane35fdd92008-12-10 05:08:54 +000079using testing::DoubleEq;
80using testing::EndsWith;
81using testing::Eq;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000082using testing::ExplainMatchResult;
shiqiane35fdd92008-12-10 05:08:54 +000083using testing::Field;
84using testing::FloatEq;
85using testing::Ge;
86using testing::Gt;
87using testing::HasSubstr;
zhanyong.wan2d970ee2009-09-24 21:41:36 +000088using testing::IsNull;
zhanyong.wanb5937da2009-07-16 20:26:41 +000089using testing::Key;
shiqiane35fdd92008-12-10 05:08:54 +000090using testing::Le;
91using testing::Lt;
92using testing::MakeMatcher;
93using testing::MakePolymorphicMatcher;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000094using testing::MatchResultListener;
shiqiane35fdd92008-12-10 05:08:54 +000095using testing::Matcher;
96using testing::MatcherCast;
97using testing::MatcherInterface;
98using testing::Matches;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000099using testing::MatchesRegex;
shiqiane35fdd92008-12-10 05:08:54 +0000100using testing::NanSensitiveDoubleEq;
101using testing::NanSensitiveFloatEq;
102using testing::Ne;
103using testing::Not;
104using testing::NotNull;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000105using testing::Pair;
shiqiane35fdd92008-12-10 05:08:54 +0000106using testing::Pointee;
107using testing::PolymorphicMatcher;
108using testing::Property;
109using testing::Ref;
110using testing::ResultOf;
111using testing::StartsWith;
112using testing::StrCaseEq;
113using testing::StrCaseNe;
114using testing::StrEq;
115using testing::StrNe;
116using testing::Truly;
117using testing::TypedEq;
zhanyong.wanb8243162009-06-04 05:48:20 +0000118using testing::Value;
shiqiane35fdd92008-12-10 05:08:54 +0000119using testing::_;
zhanyong.wana862f1d2010-03-15 21:23:04 +0000120using testing::internal::DummyMatchResultListener;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000121using testing::internal::ExplainMatchFailureTupleTo;
shiqiane35fdd92008-12-10 05:08:54 +0000122using testing::internal::FloatingEqMatcher;
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000123using testing::internal::FormatMatcherDescriptionSyntaxError;
124using testing::internal::GetParamIndex;
125using testing::internal::Interpolation;
126using testing::internal::Interpolations;
127using testing::internal::JoinAsTuple;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000128using testing::internal::RE;
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000129using testing::internal::SkipPrefix;
zhanyong.wana862f1d2010-03-15 21:23:04 +0000130using testing::internal::StreamMatchResultListener;
shiqiane35fdd92008-12-10 05:08:54 +0000131using testing::internal::String;
zhanyong.wan34b034c2010-03-05 21:23:23 +0000132using testing::internal::StringMatchResultListener;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000133using testing::internal::Strings;
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000134using testing::internal::ValidateMatcherDescription;
135using testing::internal::kInvalidInterpolation;
136using testing::internal::kPercentInterpolation;
137using testing::internal::kTupleInterpolation;
vladlosev79b83502009-11-18 00:43:37 +0000138using testing::internal::linked_ptr;
vladloseve56daa72009-11-18 01:08:08 +0000139using testing::internal::scoped_ptr;
shiqiane35fdd92008-12-10 05:08:54 +0000140using testing::internal::string;
141
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000142// For testing ExplainMatchResultTo().
143class GreaterThanMatcher : public MatcherInterface<int> {
144 public:
145 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
146
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000147 virtual void DescribeTo(::std::ostream* os) const {
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000148 *os << "is > " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000149 }
150
zhanyong.wandb22c222010-01-28 21:52:29 +0000151 virtual bool MatchAndExplain(int lhs,
152 MatchResultListener* listener) const {
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000153 const int diff = lhs - rhs_;
154 if (diff > 0) {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000155 *listener << "which is " << diff << " more than " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000156 } else if (diff == 0) {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000157 *listener << "which is the same as " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000158 } else {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000159 *listener << "which is " << -diff << " less than " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000160 }
zhanyong.wandb22c222010-01-28 21:52:29 +0000161
162 return lhs > rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000163 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000164
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000165 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000166 int rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000167};
168
169Matcher<int> GreaterThan(int n) {
170 return MakeMatcher(new GreaterThanMatcher(n));
171}
172
shiqiane35fdd92008-12-10 05:08:54 +0000173// Returns the description of the given matcher.
174template <typename T>
175string Describe(const Matcher<T>& m) {
176 stringstream ss;
177 m.DescribeTo(&ss);
178 return ss.str();
179}
180
181// Returns the description of the negation of the given matcher.
182template <typename T>
183string DescribeNegation(const Matcher<T>& m) {
184 stringstream ss;
185 m.DescribeNegationTo(&ss);
186 return ss.str();
187}
188
189// Returns the reason why x matches, or doesn't match, m.
190template <typename MatcherType, typename Value>
191string Explain(const MatcherType& m, const Value& x) {
192 stringstream ss;
193 m.ExplainMatchResultTo(x, &ss);
194 return ss.str();
195}
196
zhanyong.wana862f1d2010-03-15 21:23:04 +0000197TEST(MatchResultListenerTest, StreamingWorks) {
198 StringMatchResultListener listener;
199 listener << "hi" << 5;
200 EXPECT_EQ("hi5", listener.str());
201
202 // Streaming shouldn't crash when the underlying ostream is NULL.
203 DummyMatchResultListener dummy;
204 dummy << "hi" << 5;
205}
206
207TEST(MatchResultListenerTest, CanAccessUnderlyingStream) {
208 EXPECT_TRUE(DummyMatchResultListener().stream() == NULL);
209 EXPECT_TRUE(StreamMatchResultListener(NULL).stream() == NULL);
210
211 EXPECT_EQ(&std::cout, StreamMatchResultListener(&std::cout).stream());
212}
213
214TEST(MatchResultListenerTest, IsInterestedWorks) {
215 EXPECT_TRUE(StringMatchResultListener().IsInterested());
216 EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested());
217
218 EXPECT_FALSE(DummyMatchResultListener().IsInterested());
219 EXPECT_FALSE(StreamMatchResultListener(NULL).IsInterested());
220}
221
shiqiane35fdd92008-12-10 05:08:54 +0000222// Makes sure that the MatcherInterface<T> interface doesn't
223// change.
224class EvenMatcherImpl : public MatcherInterface<int> {
225 public:
zhanyong.wandb22c222010-01-28 21:52:29 +0000226 virtual bool MatchAndExplain(int x,
227 MatchResultListener* /* listener */) const {
228 return x % 2 == 0;
229 }
shiqiane35fdd92008-12-10 05:08:54 +0000230
231 virtual void DescribeTo(::std::ostream* os) const {
232 *os << "is an even number";
233 }
234
235 // We deliberately don't define DescribeNegationTo() and
236 // ExplainMatchResultTo() here, to make sure the definition of these
237 // two methods is optional.
238};
239
zhanyong.wana862f1d2010-03-15 21:23:04 +0000240// Makes sure that the MatcherInterface API doesn't change.
241TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) {
shiqiane35fdd92008-12-10 05:08:54 +0000242 EvenMatcherImpl m;
243}
244
zhanyong.wan82113312010-01-08 21:55:40 +0000245// Tests implementing a monomorphic matcher using MatchAndExplain().
246
247class NewEvenMatcherImpl : public MatcherInterface<int> {
248 public:
249 virtual bool MatchAndExplain(int x, MatchResultListener* listener) const {
250 const bool match = x % 2 == 0;
251 // Verifies that we can stream to a listener directly.
252 *listener << "value % " << 2;
253 if (listener->stream() != NULL) {
254 // Verifies that we can stream to a listener's underlying stream
255 // too.
256 *listener->stream() << " == " << (x % 2);
257 }
258 return match;
259 }
260
261 virtual void DescribeTo(::std::ostream* os) const {
262 *os << "is an even number";
263 }
264};
265
266TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {
267 Matcher<int> m = MakeMatcher(new NewEvenMatcherImpl);
268 EXPECT_TRUE(m.Matches(2));
269 EXPECT_FALSE(m.Matches(3));
270 EXPECT_EQ("value % 2 == 0", Explain(m, 2));
271 EXPECT_EQ("value % 2 == 1", Explain(m, 3));
272}
273
shiqiane35fdd92008-12-10 05:08:54 +0000274// Tests default-constructing a matcher.
275TEST(MatcherTest, CanBeDefaultConstructed) {
276 Matcher<double> m;
277}
278
279// Tests that Matcher<T> can be constructed from a MatcherInterface<T>*.
280TEST(MatcherTest, CanBeConstructedFromMatcherInterface) {
281 const MatcherInterface<int>* impl = new EvenMatcherImpl;
282 Matcher<int> m(impl);
283 EXPECT_TRUE(m.Matches(4));
284 EXPECT_FALSE(m.Matches(5));
285}
286
287// Tests that value can be used in place of Eq(value).
288TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) {
289 Matcher<int> m1 = 5;
290 EXPECT_TRUE(m1.Matches(5));
291 EXPECT_FALSE(m1.Matches(6));
292}
293
294// Tests that NULL can be used in place of Eq(NULL).
295TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) {
296 Matcher<int*> m1 = NULL;
297 EXPECT_TRUE(m1.Matches(NULL));
298 int n = 0;
299 EXPECT_FALSE(m1.Matches(&n));
300}
301
302// Tests that matchers are copyable.
303TEST(MatcherTest, IsCopyable) {
304 // Tests the copy constructor.
305 Matcher<bool> m1 = Eq(false);
306 EXPECT_TRUE(m1.Matches(false));
307 EXPECT_FALSE(m1.Matches(true));
308
309 // Tests the assignment operator.
310 m1 = Eq(true);
311 EXPECT_TRUE(m1.Matches(true));
312 EXPECT_FALSE(m1.Matches(false));
313}
314
315// Tests that Matcher<T>::DescribeTo() calls
316// MatcherInterface<T>::DescribeTo().
317TEST(MatcherTest, CanDescribeItself) {
318 EXPECT_EQ("is an even number",
319 Describe(Matcher<int>(new EvenMatcherImpl)));
320}
321
zhanyong.wan82113312010-01-08 21:55:40 +0000322// Tests Matcher<T>::MatchAndExplain().
323TEST(MatcherTest, MatchAndExplain) {
324 Matcher<int> m = GreaterThan(0);
zhanyong.wan34b034c2010-03-05 21:23:23 +0000325 StringMatchResultListener listener1;
zhanyong.wan82113312010-01-08 21:55:40 +0000326 EXPECT_TRUE(m.MatchAndExplain(42, &listener1));
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000327 EXPECT_EQ("which is 42 more than 0", listener1.str());
zhanyong.wan82113312010-01-08 21:55:40 +0000328
zhanyong.wan34b034c2010-03-05 21:23:23 +0000329 StringMatchResultListener listener2;
zhanyong.wan82113312010-01-08 21:55:40 +0000330 EXPECT_FALSE(m.MatchAndExplain(-9, &listener2));
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000331 EXPECT_EQ("which is 9 less than 0", listener2.str());
zhanyong.wan82113312010-01-08 21:55:40 +0000332}
333
shiqiane35fdd92008-12-10 05:08:54 +0000334// Tests that a C-string literal can be implicitly converted to a
335// Matcher<string> or Matcher<const string&>.
336TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
337 Matcher<string> m1 = "hi";
338 EXPECT_TRUE(m1.Matches("hi"));
339 EXPECT_FALSE(m1.Matches("hello"));
340
341 Matcher<const string&> m2 = "hi";
342 EXPECT_TRUE(m2.Matches("hi"));
343 EXPECT_FALSE(m2.Matches("hello"));
344}
345
346// Tests that a string object can be implicitly converted to a
347// Matcher<string> or Matcher<const string&>.
348TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {
349 Matcher<string> m1 = string("hi");
350 EXPECT_TRUE(m1.Matches("hi"));
351 EXPECT_FALSE(m1.Matches("hello"));
352
353 Matcher<const string&> m2 = string("hi");
354 EXPECT_TRUE(m2.Matches("hi"));
355 EXPECT_FALSE(m2.Matches("hello"));
356}
357
358// Tests that MakeMatcher() constructs a Matcher<T> from a
359// MatcherInterface* without requiring the user to explicitly
360// write the type.
361TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) {
362 const MatcherInterface<int>* dummy_impl = NULL;
363 Matcher<int> m = MakeMatcher(dummy_impl);
364}
365
zhanyong.wan82113312010-01-08 21:55:40 +0000366// Tests that MakePolymorphicMatcher() can construct a polymorphic
367// matcher from its implementation using the old API.
zhanyong.wan33605ba2010-04-22 23:37:47 +0000368const int g_bar = 1;
shiqiane35fdd92008-12-10 05:08:54 +0000369class ReferencesBarOrIsZeroImpl {
370 public:
371 template <typename T>
zhanyong.wandb22c222010-01-28 21:52:29 +0000372 bool MatchAndExplain(const T& x,
373 MatchResultListener* /* listener */) const {
shiqiane35fdd92008-12-10 05:08:54 +0000374 const void* p = &x;
zhanyong.wan33605ba2010-04-22 23:37:47 +0000375 return p == &g_bar || x == 0;
shiqiane35fdd92008-12-10 05:08:54 +0000376 }
377
zhanyong.wan33605ba2010-04-22 23:37:47 +0000378 void DescribeTo(::std::ostream* os) const { *os << "g_bar or zero"; }
shiqiane35fdd92008-12-10 05:08:54 +0000379
380 void DescribeNegationTo(::std::ostream* os) const {
zhanyong.wan33605ba2010-04-22 23:37:47 +0000381 *os << "doesn't reference g_bar and is not zero";
shiqiane35fdd92008-12-10 05:08:54 +0000382 }
383};
384
385// This function verifies that MakePolymorphicMatcher() returns a
386// PolymorphicMatcher<T> where T is the argument's type.
387PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() {
388 return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl());
389}
390
zhanyong.wan82113312010-01-08 21:55:40 +0000391TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) {
shiqiane35fdd92008-12-10 05:08:54 +0000392 // Using a polymorphic matcher to match a reference type.
393 Matcher<const int&> m1 = ReferencesBarOrIsZero();
394 EXPECT_TRUE(m1.Matches(0));
395 // Verifies that the identity of a by-reference argument is preserved.
zhanyong.wan33605ba2010-04-22 23:37:47 +0000396 EXPECT_TRUE(m1.Matches(g_bar));
shiqiane35fdd92008-12-10 05:08:54 +0000397 EXPECT_FALSE(m1.Matches(1));
zhanyong.wan33605ba2010-04-22 23:37:47 +0000398 EXPECT_EQ("g_bar or zero", Describe(m1));
shiqiane35fdd92008-12-10 05:08:54 +0000399
400 // Using a polymorphic matcher to match a value type.
401 Matcher<double> m2 = ReferencesBarOrIsZero();
402 EXPECT_TRUE(m2.Matches(0.0));
403 EXPECT_FALSE(m2.Matches(0.1));
zhanyong.wan33605ba2010-04-22 23:37:47 +0000404 EXPECT_EQ("g_bar or zero", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +0000405}
406
zhanyong.wan82113312010-01-08 21:55:40 +0000407// Tests implementing a polymorphic matcher using MatchAndExplain().
408
409class PolymorphicIsEvenImpl {
410 public:
411 void DescribeTo(::std::ostream* os) const { *os << "is even"; }
412
413 void DescribeNegationTo(::std::ostream* os) const {
414 *os << "is odd";
415 }
zhanyong.wan82113312010-01-08 21:55:40 +0000416
zhanyong.wandb22c222010-01-28 21:52:29 +0000417 template <typename T>
418 bool MatchAndExplain(const T& x, MatchResultListener* listener) const {
419 // Verifies that we can stream to the listener directly.
420 *listener << "% " << 2;
421 if (listener->stream() != NULL) {
422 // Verifies that we can stream to the listener's underlying stream
423 // too.
424 *listener->stream() << " == " << (x % 2);
425 }
426 return (x % 2) == 0;
zhanyong.wan82113312010-01-08 21:55:40 +0000427 }
zhanyong.wandb22c222010-01-28 21:52:29 +0000428};
zhanyong.wan82113312010-01-08 21:55:40 +0000429
430PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() {
431 return MakePolymorphicMatcher(PolymorphicIsEvenImpl());
432}
433
434TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {
435 // Using PolymorphicIsEven() as a Matcher<int>.
436 const Matcher<int> m1 = PolymorphicIsEven();
437 EXPECT_TRUE(m1.Matches(42));
438 EXPECT_FALSE(m1.Matches(43));
439 EXPECT_EQ("is even", Describe(m1));
440
441 const Matcher<int> not_m1 = Not(m1);
442 EXPECT_EQ("is odd", Describe(not_m1));
443
444 EXPECT_EQ("% 2 == 0", Explain(m1, 42));
445
446 // Using PolymorphicIsEven() as a Matcher<char>.
447 const Matcher<char> m2 = PolymorphicIsEven();
448 EXPECT_TRUE(m2.Matches('\x42'));
449 EXPECT_FALSE(m2.Matches('\x43'));
450 EXPECT_EQ("is even", Describe(m2));
451
452 const Matcher<char> not_m2 = Not(m2);
453 EXPECT_EQ("is odd", Describe(not_m2));
454
455 EXPECT_EQ("% 2 == 0", Explain(m2, '\x42'));
456}
457
shiqiane35fdd92008-12-10 05:08:54 +0000458// Tests that MatcherCast<T>(m) works when m is a polymorphic matcher.
459TEST(MatcherCastTest, FromPolymorphicMatcher) {
460 Matcher<int> m = MatcherCast<int>(Eq(5));
461 EXPECT_TRUE(m.Matches(5));
462 EXPECT_FALSE(m.Matches(6));
463}
464
465// For testing casting matchers between compatible types.
466class IntValue {
467 public:
468 // An int can be statically (although not implicitly) cast to a
469 // IntValue.
zhanyong.wan32de5f52009-12-23 00:13:23 +0000470 explicit IntValue(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +0000471
472 int value() const { return value_; }
473 private:
474 int value_;
475};
476
477// For testing casting matchers between compatible types.
478bool IsPositiveIntValue(const IntValue& foo) {
479 return foo.value() > 0;
480}
481
482// Tests that MatcherCast<T>(m) works when m is a Matcher<U> where T
483// can be statically converted to U.
484TEST(MatcherCastTest, FromCompatibleType) {
485 Matcher<double> m1 = Eq(2.0);
486 Matcher<int> m2 = MatcherCast<int>(m1);
487 EXPECT_TRUE(m2.Matches(2));
488 EXPECT_FALSE(m2.Matches(3));
489
490 Matcher<IntValue> m3 = Truly(IsPositiveIntValue);
491 Matcher<int> m4 = MatcherCast<int>(m3);
492 // In the following, the arguments 1 and 0 are statically converted
493 // to IntValue objects, and then tested by the IsPositiveIntValue()
494 // predicate.
495 EXPECT_TRUE(m4.Matches(1));
496 EXPECT_FALSE(m4.Matches(0));
497}
498
499// Tests that MatcherCast<T>(m) works when m is a Matcher<const T&>.
500TEST(MatcherCastTest, FromConstReferenceToNonReference) {
501 Matcher<const int&> m1 = Eq(0);
502 Matcher<int> m2 = MatcherCast<int>(m1);
503 EXPECT_TRUE(m2.Matches(0));
504 EXPECT_FALSE(m2.Matches(1));
505}
506
507// Tests that MatcherCast<T>(m) works when m is a Matcher<T&>.
508TEST(MatcherCastTest, FromReferenceToNonReference) {
509 Matcher<int&> m1 = Eq(0);
510 Matcher<int> m2 = MatcherCast<int>(m1);
511 EXPECT_TRUE(m2.Matches(0));
512 EXPECT_FALSE(m2.Matches(1));
513}
514
515// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
516TEST(MatcherCastTest, FromNonReferenceToConstReference) {
517 Matcher<int> m1 = Eq(0);
518 Matcher<const int&> m2 = MatcherCast<const int&>(m1);
519 EXPECT_TRUE(m2.Matches(0));
520 EXPECT_FALSE(m2.Matches(1));
521}
522
523// Tests that MatcherCast<T&>(m) works when m is a Matcher<T>.
524TEST(MatcherCastTest, FromNonReferenceToReference) {
525 Matcher<int> m1 = Eq(0);
526 Matcher<int&> m2 = MatcherCast<int&>(m1);
527 int n = 0;
528 EXPECT_TRUE(m2.Matches(n));
529 n = 1;
530 EXPECT_FALSE(m2.Matches(n));
531}
532
533// Tests that MatcherCast<T>(m) works when m is a Matcher<T>.
534TEST(MatcherCastTest, FromSameType) {
535 Matcher<int> m1 = Eq(0);
536 Matcher<int> m2 = MatcherCast<int>(m1);
537 EXPECT_TRUE(m2.Matches(0));
538 EXPECT_FALSE(m2.Matches(1));
539}
540
zhanyong.wan18490652009-05-11 18:54:08 +0000541class Base {};
542class Derived : public Base {};
543
544// Tests that SafeMatcherCast<T>(m) works when m is a polymorphic matcher.
545TEST(SafeMatcherCastTest, FromPolymorphicMatcher) {
546 Matcher<char> m2 = SafeMatcherCast<char>(Eq(32));
547 EXPECT_TRUE(m2.Matches(' '));
548 EXPECT_FALSE(m2.Matches('\n'));
549}
550
zhanyong.wan16cf4732009-05-14 20:55:30 +0000551// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where
552// T and U are arithmetic types and T can be losslessly converted to
553// U.
554TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) {
zhanyong.wan18490652009-05-11 18:54:08 +0000555 Matcher<double> m1 = DoubleEq(1.0);
zhanyong.wan16cf4732009-05-14 20:55:30 +0000556 Matcher<float> m2 = SafeMatcherCast<float>(m1);
557 EXPECT_TRUE(m2.Matches(1.0f));
558 EXPECT_FALSE(m2.Matches(2.0f));
559
560 Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>('a'));
561 EXPECT_TRUE(m3.Matches('a'));
562 EXPECT_FALSE(m3.Matches('b'));
zhanyong.wan18490652009-05-11 18:54:08 +0000563}
564
565// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where T and U
566// are pointers or references to a derived and a base class, correspondingly.
567TEST(SafeMatcherCastTest, FromBaseClass) {
568 Derived d, d2;
569 Matcher<Base*> m1 = Eq(&d);
570 Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1);
571 EXPECT_TRUE(m2.Matches(&d));
572 EXPECT_FALSE(m2.Matches(&d2));
573
574 Matcher<Base&> m3 = Ref(d);
575 Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3);
576 EXPECT_TRUE(m4.Matches(d));
577 EXPECT_FALSE(m4.Matches(d2));
578}
579
580// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<const T&>.
581TEST(SafeMatcherCastTest, FromConstReferenceToReference) {
582 int n = 0;
583 Matcher<const int&> m1 = Ref(n);
584 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
585 int n1 = 0;
586 EXPECT_TRUE(m2.Matches(n));
587 EXPECT_FALSE(m2.Matches(n1));
588}
589
590// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
591TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) {
592 Matcher<int> m1 = Eq(0);
593 Matcher<const int&> m2 = SafeMatcherCast<const int&>(m1);
594 EXPECT_TRUE(m2.Matches(0));
595 EXPECT_FALSE(m2.Matches(1));
596}
597
598// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<T>.
599TEST(SafeMatcherCastTest, FromNonReferenceToReference) {
600 Matcher<int> m1 = Eq(0);
601 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
602 int n = 0;
603 EXPECT_TRUE(m2.Matches(n));
604 n = 1;
605 EXPECT_FALSE(m2.Matches(n));
606}
607
608// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<T>.
609TEST(SafeMatcherCastTest, FromSameType) {
610 Matcher<int> m1 = Eq(0);
611 Matcher<int> m2 = SafeMatcherCast<int>(m1);
612 EXPECT_TRUE(m2.Matches(0));
613 EXPECT_FALSE(m2.Matches(1));
614}
615
shiqiane35fdd92008-12-10 05:08:54 +0000616// Tests that A<T>() matches any value of type T.
617TEST(ATest, MatchesAnyValue) {
618 // Tests a matcher for a value type.
619 Matcher<double> m1 = A<double>();
620 EXPECT_TRUE(m1.Matches(91.43));
621 EXPECT_TRUE(m1.Matches(-15.32));
622
623 // Tests a matcher for a reference type.
624 int a = 2;
625 int b = -6;
626 Matcher<int&> m2 = A<int&>();
627 EXPECT_TRUE(m2.Matches(a));
628 EXPECT_TRUE(m2.Matches(b));
629}
630
631// Tests that A<T>() describes itself properly.
632TEST(ATest, CanDescribeSelf) {
633 EXPECT_EQ("is anything", Describe(A<bool>()));
634}
635
636// Tests that An<T>() matches any value of type T.
637TEST(AnTest, MatchesAnyValue) {
638 // Tests a matcher for a value type.
639 Matcher<int> m1 = An<int>();
640 EXPECT_TRUE(m1.Matches(9143));
641 EXPECT_TRUE(m1.Matches(-1532));
642
643 // Tests a matcher for a reference type.
644 int a = 2;
645 int b = -6;
646 Matcher<int&> m2 = An<int&>();
647 EXPECT_TRUE(m2.Matches(a));
648 EXPECT_TRUE(m2.Matches(b));
649}
650
651// Tests that An<T>() describes itself properly.
652TEST(AnTest, CanDescribeSelf) {
653 EXPECT_EQ("is anything", Describe(An<int>()));
654}
655
656// Tests that _ can be used as a matcher for any type and matches any
657// value of that type.
658TEST(UnderscoreTest, MatchesAnyValue) {
659 // Uses _ as a matcher for a value type.
660 Matcher<int> m1 = _;
661 EXPECT_TRUE(m1.Matches(123));
662 EXPECT_TRUE(m1.Matches(-242));
663
664 // Uses _ as a matcher for a reference type.
665 bool a = false;
666 const bool b = true;
667 Matcher<const bool&> m2 = _;
668 EXPECT_TRUE(m2.Matches(a));
669 EXPECT_TRUE(m2.Matches(b));
670}
671
672// Tests that _ describes itself properly.
673TEST(UnderscoreTest, CanDescribeSelf) {
674 Matcher<int> m = _;
675 EXPECT_EQ("is anything", Describe(m));
676}
677
678// Tests that Eq(x) matches any value equal to x.
679TEST(EqTest, MatchesEqualValue) {
680 // 2 C-strings with same content but different addresses.
681 const char a1[] = "hi";
682 const char a2[] = "hi";
683
684 Matcher<const char*> m1 = Eq(a1);
685 EXPECT_TRUE(m1.Matches(a1));
686 EXPECT_FALSE(m1.Matches(a2));
687}
688
689// Tests that Eq(v) describes itself properly.
690
691class Unprintable {
692 public:
693 Unprintable() : c_('a') {}
694
zhanyong.wan32de5f52009-12-23 00:13:23 +0000695 bool operator==(const Unprintable& /* rhs */) { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000696 private:
697 char c_;
698};
699
700TEST(EqTest, CanDescribeSelf) {
701 Matcher<Unprintable> m = Eq(Unprintable());
702 EXPECT_EQ("is equal to 1-byte object <61>", Describe(m));
703}
704
705// Tests that Eq(v) can be used to match any type that supports
706// comparing with type T, where T is v's type.
707TEST(EqTest, IsPolymorphic) {
708 Matcher<int> m1 = Eq(1);
709 EXPECT_TRUE(m1.Matches(1));
710 EXPECT_FALSE(m1.Matches(2));
711
712 Matcher<char> m2 = Eq(1);
713 EXPECT_TRUE(m2.Matches('\1'));
714 EXPECT_FALSE(m2.Matches('a'));
715}
716
717// Tests that TypedEq<T>(v) matches values of type T that's equal to v.
718TEST(TypedEqTest, ChecksEqualityForGivenType) {
719 Matcher<char> m1 = TypedEq<char>('a');
720 EXPECT_TRUE(m1.Matches('a'));
721 EXPECT_FALSE(m1.Matches('b'));
722
723 Matcher<int> m2 = TypedEq<int>(6);
724 EXPECT_TRUE(m2.Matches(6));
725 EXPECT_FALSE(m2.Matches(7));
726}
727
728// Tests that TypedEq(v) describes itself properly.
729TEST(TypedEqTest, CanDescribeSelf) {
730 EXPECT_EQ("is equal to 2", Describe(TypedEq<int>(2)));
731}
732
733// Tests that TypedEq<T>(v) has type Matcher<T>.
734
735// Type<T>::IsTypeOf(v) compiles iff the type of value v is T, where T
736// is a "bare" type (i.e. not in the form of const U or U&). If v's
737// type is not T, the compiler will generate a message about
738// "undefined referece".
739template <typename T>
740struct Type {
zhanyong.wan32de5f52009-12-23 00:13:23 +0000741 static bool IsTypeOf(const T& /* v */) { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000742
743 template <typename T2>
744 static void IsTypeOf(T2 v);
745};
746
747TEST(TypedEqTest, HasSpecifiedType) {
748 // Verfies that the type of TypedEq<T>(v) is Matcher<T>.
749 Type<Matcher<int> >::IsTypeOf(TypedEq<int>(5));
750 Type<Matcher<double> >::IsTypeOf(TypedEq<double>(5));
751}
752
753// Tests that Ge(v) matches anything >= v.
754TEST(GeTest, ImplementsGreaterThanOrEqual) {
755 Matcher<int> m1 = Ge(0);
756 EXPECT_TRUE(m1.Matches(1));
757 EXPECT_TRUE(m1.Matches(0));
758 EXPECT_FALSE(m1.Matches(-1));
759}
760
761// Tests that Ge(v) describes itself properly.
762TEST(GeTest, CanDescribeSelf) {
763 Matcher<int> m = Ge(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000764 EXPECT_EQ("is >= 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000765}
766
767// Tests that Gt(v) matches anything > v.
768TEST(GtTest, ImplementsGreaterThan) {
769 Matcher<double> m1 = Gt(0);
770 EXPECT_TRUE(m1.Matches(1.0));
771 EXPECT_FALSE(m1.Matches(0.0));
772 EXPECT_FALSE(m1.Matches(-1.0));
773}
774
775// Tests that Gt(v) describes itself properly.
776TEST(GtTest, CanDescribeSelf) {
777 Matcher<int> m = Gt(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000778 EXPECT_EQ("is > 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000779}
780
781// Tests that Le(v) matches anything <= v.
782TEST(LeTest, ImplementsLessThanOrEqual) {
783 Matcher<char> m1 = Le('b');
784 EXPECT_TRUE(m1.Matches('a'));
785 EXPECT_TRUE(m1.Matches('b'));
786 EXPECT_FALSE(m1.Matches('c'));
787}
788
789// Tests that Le(v) describes itself properly.
790TEST(LeTest, CanDescribeSelf) {
791 Matcher<int> m = Le(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000792 EXPECT_EQ("is <= 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000793}
794
795// Tests that Lt(v) matches anything < v.
796TEST(LtTest, ImplementsLessThan) {
797 Matcher<const string&> m1 = Lt("Hello");
798 EXPECT_TRUE(m1.Matches("Abc"));
799 EXPECT_FALSE(m1.Matches("Hello"));
800 EXPECT_FALSE(m1.Matches("Hello, world!"));
801}
802
803// Tests that Lt(v) describes itself properly.
804TEST(LtTest, CanDescribeSelf) {
805 Matcher<int> m = Lt(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000806 EXPECT_EQ("is < 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000807}
808
809// Tests that Ne(v) matches anything != v.
810TEST(NeTest, ImplementsNotEqual) {
811 Matcher<int> m1 = Ne(0);
812 EXPECT_TRUE(m1.Matches(1));
813 EXPECT_TRUE(m1.Matches(-1));
814 EXPECT_FALSE(m1.Matches(0));
815}
816
817// Tests that Ne(v) describes itself properly.
818TEST(NeTest, CanDescribeSelf) {
819 Matcher<int> m = Ne(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000820 EXPECT_EQ("isn't equal to 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000821}
822
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000823// Tests that IsNull() matches any NULL pointer of any type.
824TEST(IsNullTest, MatchesNullPointer) {
825 Matcher<int*> m1 = IsNull();
826 int* p1 = NULL;
827 int n = 0;
828 EXPECT_TRUE(m1.Matches(p1));
829 EXPECT_FALSE(m1.Matches(&n));
830
831 Matcher<const char*> m2 = IsNull();
832 const char* p2 = NULL;
833 EXPECT_TRUE(m2.Matches(p2));
834 EXPECT_FALSE(m2.Matches("hi"));
835
zhanyong.wan95b12332009-09-25 18:55:50 +0000836#if !GTEST_OS_SYMBIAN
837 // Nokia's Symbian compiler generates:
838 // gmock-matchers.h: ambiguous access to overloaded function
839 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(void *)'
840 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(const testing::
841 // MatcherInterface<void *> *)'
842 // gmock-matchers.h: (point of instantiation: 'testing::
843 // gmock_matchers_test::IsNullTest_MatchesNullPointer_Test::TestBody()')
844 // gmock-matchers.h: (instantiating: 'testing::PolymorphicMatc
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000845 Matcher<void*> m3 = IsNull();
846 void* p3 = NULL;
847 EXPECT_TRUE(m3.Matches(p3));
848 EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef)));
zhanyong.wan95b12332009-09-25 18:55:50 +0000849#endif
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000850}
851
vladlosev79b83502009-11-18 00:43:37 +0000852TEST(IsNullTest, LinkedPtr) {
853 const Matcher<linked_ptr<int> > m = IsNull();
854 const linked_ptr<int> null_p;
855 const linked_ptr<int> non_null_p(new int);
856
857 EXPECT_TRUE(m.Matches(null_p));
858 EXPECT_FALSE(m.Matches(non_null_p));
859}
860
861TEST(IsNullTest, ReferenceToConstLinkedPtr) {
862 const Matcher<const linked_ptr<double>&> m = IsNull();
863 const linked_ptr<double> null_p;
864 const linked_ptr<double> non_null_p(new double);
865
866 EXPECT_TRUE(m.Matches(null_p));
867 EXPECT_FALSE(m.Matches(non_null_p));
868}
869
vladloseve56daa72009-11-18 01:08:08 +0000870TEST(IsNullTest, ReferenceToConstScopedPtr) {
871 const Matcher<const scoped_ptr<double>&> m = IsNull();
872 const scoped_ptr<double> null_p;
873 const scoped_ptr<double> non_null_p(new double);
874
875 EXPECT_TRUE(m.Matches(null_p));
876 EXPECT_FALSE(m.Matches(non_null_p));
877}
878
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000879// Tests that IsNull() describes itself properly.
880TEST(IsNullTest, CanDescribeSelf) {
881 Matcher<int*> m = IsNull();
882 EXPECT_EQ("is NULL", Describe(m));
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000883 EXPECT_EQ("isn't NULL", DescribeNegation(m));
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000884}
885
shiqiane35fdd92008-12-10 05:08:54 +0000886// Tests that NotNull() matches any non-NULL pointer of any type.
887TEST(NotNullTest, MatchesNonNullPointer) {
888 Matcher<int*> m1 = NotNull();
889 int* p1 = NULL;
890 int n = 0;
891 EXPECT_FALSE(m1.Matches(p1));
892 EXPECT_TRUE(m1.Matches(&n));
893
894 Matcher<const char*> m2 = NotNull();
895 const char* p2 = NULL;
896 EXPECT_FALSE(m2.Matches(p2));
897 EXPECT_TRUE(m2.Matches("hi"));
898}
899
vladlosev79b83502009-11-18 00:43:37 +0000900TEST(NotNullTest, LinkedPtr) {
901 const Matcher<linked_ptr<int> > m = NotNull();
902 const linked_ptr<int> null_p;
903 const linked_ptr<int> non_null_p(new int);
904
905 EXPECT_FALSE(m.Matches(null_p));
906 EXPECT_TRUE(m.Matches(non_null_p));
907}
908
909TEST(NotNullTest, ReferenceToConstLinkedPtr) {
910 const Matcher<const linked_ptr<double>&> m = NotNull();
911 const linked_ptr<double> null_p;
912 const linked_ptr<double> non_null_p(new double);
913
914 EXPECT_FALSE(m.Matches(null_p));
915 EXPECT_TRUE(m.Matches(non_null_p));
916}
917
vladloseve56daa72009-11-18 01:08:08 +0000918TEST(NotNullTest, ReferenceToConstScopedPtr) {
919 const Matcher<const scoped_ptr<double>&> m = NotNull();
920 const scoped_ptr<double> null_p;
921 const scoped_ptr<double> non_null_p(new double);
922
923 EXPECT_FALSE(m.Matches(null_p));
924 EXPECT_TRUE(m.Matches(non_null_p));
925}
926
shiqiane35fdd92008-12-10 05:08:54 +0000927// Tests that NotNull() describes itself properly.
928TEST(NotNullTest, CanDescribeSelf) {
929 Matcher<int*> m = NotNull();
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000930 EXPECT_EQ("isn't NULL", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000931}
932
933// Tests that Ref(variable) matches an argument that references
934// 'variable'.
935TEST(RefTest, MatchesSameVariable) {
936 int a = 0;
937 int b = 0;
938 Matcher<int&> m = Ref(a);
939 EXPECT_TRUE(m.Matches(a));
940 EXPECT_FALSE(m.Matches(b));
941}
942
943// Tests that Ref(variable) describes itself properly.
944TEST(RefTest, CanDescribeSelf) {
945 int n = 5;
946 Matcher<int&> m = Ref(n);
947 stringstream ss;
948 ss << "references the variable @" << &n << " 5";
949 EXPECT_EQ(string(ss.str()), Describe(m));
950}
951
952// Test that Ref(non_const_varialbe) can be used as a matcher for a
953// const reference.
954TEST(RefTest, CanBeUsedAsMatcherForConstReference) {
955 int a = 0;
956 int b = 0;
957 Matcher<const int&> m = Ref(a);
958 EXPECT_TRUE(m.Matches(a));
959 EXPECT_FALSE(m.Matches(b));
960}
961
962// Tests that Ref(variable) is covariant, i.e. Ref(derived) can be
963// used wherever Ref(base) can be used (Ref(derived) is a sub-type
964// of Ref(base), but not vice versa.
965
shiqiane35fdd92008-12-10 05:08:54 +0000966TEST(RefTest, IsCovariant) {
967 Base base, base2;
968 Derived derived;
969 Matcher<const Base&> m1 = Ref(base);
970 EXPECT_TRUE(m1.Matches(base));
971 EXPECT_FALSE(m1.Matches(base2));
972 EXPECT_FALSE(m1.Matches(derived));
973
974 m1 = Ref(derived);
975 EXPECT_TRUE(m1.Matches(derived));
976 EXPECT_FALSE(m1.Matches(base));
977 EXPECT_FALSE(m1.Matches(base2));
978}
979
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000980TEST(RefTest, ExplainsResult) {
981 int n = 0;
982 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), n),
983 StartsWith("which is located @"));
984
985 int m = 0;
986 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), m),
987 StartsWith("which is located @"));
988}
989
shiqiane35fdd92008-12-10 05:08:54 +0000990// Tests string comparison matchers.
991
992TEST(StrEqTest, MatchesEqualString) {
993 Matcher<const char*> m = StrEq(string("Hello"));
994 EXPECT_TRUE(m.Matches("Hello"));
995 EXPECT_FALSE(m.Matches("hello"));
996 EXPECT_FALSE(m.Matches(NULL));
997
998 Matcher<const string&> m2 = StrEq("Hello");
999 EXPECT_TRUE(m2.Matches("Hello"));
1000 EXPECT_FALSE(m2.Matches("Hi"));
1001}
1002
1003TEST(StrEqTest, CanDescribeSelf) {
1004 Matcher<string> m = StrEq("Hi-\'\"\?\\\a\b\f\n\r\t\v\xD3");
1005 EXPECT_EQ("is equal to \"Hi-\'\\\"\\?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",
1006 Describe(m));
1007
1008 string str("01204500800");
1009 str[3] = '\0';
1010 Matcher<string> m2 = StrEq(str);
1011 EXPECT_EQ("is equal to \"012\\04500800\"", Describe(m2));
1012 str[0] = str[6] = str[7] = str[9] = str[10] = '\0';
1013 Matcher<string> m3 = StrEq(str);
1014 EXPECT_EQ("is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3));
1015}
1016
1017TEST(StrNeTest, MatchesUnequalString) {
1018 Matcher<const char*> m = StrNe("Hello");
1019 EXPECT_TRUE(m.Matches(""));
1020 EXPECT_TRUE(m.Matches(NULL));
1021 EXPECT_FALSE(m.Matches("Hello"));
1022
1023 Matcher<string> m2 = StrNe(string("Hello"));
1024 EXPECT_TRUE(m2.Matches("hello"));
1025 EXPECT_FALSE(m2.Matches("Hello"));
1026}
1027
1028TEST(StrNeTest, CanDescribeSelf) {
1029 Matcher<const char*> m = StrNe("Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001030 EXPECT_EQ("isn't equal to \"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001031}
1032
1033TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
1034 Matcher<const char*> m = StrCaseEq(string("Hello"));
1035 EXPECT_TRUE(m.Matches("Hello"));
1036 EXPECT_TRUE(m.Matches("hello"));
1037 EXPECT_FALSE(m.Matches("Hi"));
1038 EXPECT_FALSE(m.Matches(NULL));
1039
1040 Matcher<const string&> m2 = StrCaseEq("Hello");
1041 EXPECT_TRUE(m2.Matches("hello"));
1042 EXPECT_FALSE(m2.Matches("Hi"));
1043}
1044
1045TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1046 string str1("oabocdooeoo");
1047 string str2("OABOCDOOEOO");
1048 Matcher<const string&> m0 = StrCaseEq(str1);
1049 EXPECT_FALSE(m0.Matches(str2 + string(1, '\0')));
1050
1051 str1[3] = str2[3] = '\0';
1052 Matcher<const string&> m1 = StrCaseEq(str1);
1053 EXPECT_TRUE(m1.Matches(str2));
1054
1055 str1[0] = str1[6] = str1[7] = str1[10] = '\0';
1056 str2[0] = str2[6] = str2[7] = str2[10] = '\0';
1057 Matcher<const string&> m2 = StrCaseEq(str1);
1058 str1[9] = str2[9] = '\0';
1059 EXPECT_FALSE(m2.Matches(str2));
1060
1061 Matcher<const string&> m3 = StrCaseEq(str1);
1062 EXPECT_TRUE(m3.Matches(str2));
1063
1064 EXPECT_FALSE(m3.Matches(str2 + "x"));
1065 str2.append(1, '\0');
1066 EXPECT_FALSE(m3.Matches(str2));
1067 EXPECT_FALSE(m3.Matches(string(str2, 0, 9)));
1068}
1069
1070TEST(StrCaseEqTest, CanDescribeSelf) {
1071 Matcher<string> m = StrCaseEq("Hi");
1072 EXPECT_EQ("is equal to (ignoring case) \"Hi\"", Describe(m));
1073}
1074
1075TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1076 Matcher<const char*> m = StrCaseNe("Hello");
1077 EXPECT_TRUE(m.Matches("Hi"));
1078 EXPECT_TRUE(m.Matches(NULL));
1079 EXPECT_FALSE(m.Matches("Hello"));
1080 EXPECT_FALSE(m.Matches("hello"));
1081
1082 Matcher<string> m2 = StrCaseNe(string("Hello"));
1083 EXPECT_TRUE(m2.Matches(""));
1084 EXPECT_FALSE(m2.Matches("Hello"));
1085}
1086
1087TEST(StrCaseNeTest, CanDescribeSelf) {
1088 Matcher<const char*> m = StrCaseNe("Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001089 EXPECT_EQ("isn't equal to (ignoring case) \"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001090}
1091
1092// Tests that HasSubstr() works for matching string-typed values.
1093TEST(HasSubstrTest, WorksForStringClasses) {
1094 const Matcher<string> m1 = HasSubstr("foo");
1095 EXPECT_TRUE(m1.Matches(string("I love food.")));
1096 EXPECT_FALSE(m1.Matches(string("tofo")));
1097
1098 const Matcher<const std::string&> m2 = HasSubstr("foo");
1099 EXPECT_TRUE(m2.Matches(std::string("I love food.")));
1100 EXPECT_FALSE(m2.Matches(std::string("tofo")));
1101}
1102
1103// Tests that HasSubstr() works for matching C-string-typed values.
1104TEST(HasSubstrTest, WorksForCStrings) {
1105 const Matcher<char*> m1 = HasSubstr("foo");
1106 EXPECT_TRUE(m1.Matches(const_cast<char*>("I love food.")));
1107 EXPECT_FALSE(m1.Matches(const_cast<char*>("tofo")));
1108 EXPECT_FALSE(m1.Matches(NULL));
1109
1110 const Matcher<const char*> m2 = HasSubstr("foo");
1111 EXPECT_TRUE(m2.Matches("I love food."));
1112 EXPECT_FALSE(m2.Matches("tofo"));
1113 EXPECT_FALSE(m2.Matches(NULL));
1114}
1115
1116// Tests that HasSubstr(s) describes itself properly.
1117TEST(HasSubstrTest, CanDescribeSelf) {
1118 Matcher<string> m = HasSubstr("foo\n\"");
1119 EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m));
1120}
1121
zhanyong.wanb5937da2009-07-16 20:26:41 +00001122TEST(KeyTest, CanDescribeSelf) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001123 Matcher<const pair<std::string, int>&> m = Key("foo");
zhanyong.wanb5937da2009-07-16 20:26:41 +00001124 EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001125 EXPECT_EQ("doesn't have a key that is equal to \"foo\"", DescribeNegation(m));
1126}
1127
1128TEST(KeyTest, ExplainsResult) {
1129 Matcher<pair<int, bool> > m = Key(GreaterThan(10));
1130 EXPECT_EQ("whose first field is a value which is 5 less than 10",
1131 Explain(m, make_pair(5, true)));
1132 EXPECT_EQ("whose first field is a value which is 5 more than 10",
1133 Explain(m, make_pair(15, true)));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001134}
1135
1136TEST(KeyTest, MatchesCorrectly) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001137 pair<int, std::string> p(25, "foo");
zhanyong.wanb5937da2009-07-16 20:26:41 +00001138 EXPECT_THAT(p, Key(25));
1139 EXPECT_THAT(p, Not(Key(42)));
1140 EXPECT_THAT(p, Key(Ge(20)));
1141 EXPECT_THAT(p, Not(Key(Lt(25))));
1142}
1143
1144TEST(KeyTest, SafelyCastsInnerMatcher) {
1145 Matcher<int> is_positive = Gt(0);
1146 Matcher<int> is_negative = Lt(0);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001147 pair<char, bool> p('a', true);
zhanyong.wanb5937da2009-07-16 20:26:41 +00001148 EXPECT_THAT(p, Key(is_positive));
1149 EXPECT_THAT(p, Not(Key(is_negative)));
1150}
1151
1152TEST(KeyTest, InsideContainsUsingMap) {
zhanyong.wan95b12332009-09-25 18:55:50 +00001153 std::map<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001154 container.insert(make_pair(1, 'a'));
1155 container.insert(make_pair(2, 'b'));
1156 container.insert(make_pair(4, 'c'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001157 EXPECT_THAT(container, Contains(Key(1)));
1158 EXPECT_THAT(container, Not(Contains(Key(3))));
1159}
1160
1161TEST(KeyTest, InsideContainsUsingMultimap) {
zhanyong.wan95b12332009-09-25 18:55:50 +00001162 std::multimap<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001163 container.insert(make_pair(1, 'a'));
1164 container.insert(make_pair(2, 'b'));
1165 container.insert(make_pair(4, 'c'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001166
1167 EXPECT_THAT(container, Not(Contains(Key(25))));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001168 container.insert(make_pair(25, 'd'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001169 EXPECT_THAT(container, Contains(Key(25)));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001170 container.insert(make_pair(25, 'e'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001171 EXPECT_THAT(container, Contains(Key(25)));
1172
1173 EXPECT_THAT(container, Contains(Key(1)));
1174 EXPECT_THAT(container, Not(Contains(Key(3))));
1175}
1176
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001177TEST(PairTest, Typing) {
1178 // Test verifies the following type conversions can be compiled.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001179 Matcher<const pair<const char*, int>&> m1 = Pair("foo", 42);
1180 Matcher<const pair<const char*, int> > m2 = Pair("foo", 42);
1181 Matcher<pair<const char*, int> > m3 = Pair("foo", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001182
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001183 Matcher<pair<int, const std::string> > m4 = Pair(25, "42");
1184 Matcher<pair<const std::string, int> > m5 = Pair("25", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001185}
1186
1187TEST(PairTest, CanDescribeSelf) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001188 Matcher<const pair<std::string, int>&> m1 = Pair("foo", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001189 EXPECT_EQ("has a first field that is equal to \"foo\""
1190 ", and has a second field that is equal to 42",
1191 Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001192 EXPECT_EQ("has a first field that isn't equal to \"foo\""
1193 ", or has a second field that isn't equal to 42",
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001194 DescribeNegation(m1));
1195 // Double and triple negation (1 or 2 times not and description of negation).
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001196 Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42));
1197 EXPECT_EQ("has a first field that isn't equal to 13"
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001198 ", and has a second field that is equal to 42",
1199 DescribeNegation(m2));
1200}
1201
1202TEST(PairTest, CanExplainMatchResultTo) {
zhanyong.wan82113312010-01-08 21:55:40 +00001203 // If neither field matches, Pair() should explain about the first
1204 // field.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001205 const Matcher<pair<int, int> > m = Pair(GreaterThan(0), GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001206 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001207 Explain(m, make_pair(-1, -2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001208
zhanyong.wan82113312010-01-08 21:55:40 +00001209 // If the first field matches but the second doesn't, Pair() should
1210 // explain about the second field.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001211 EXPECT_EQ("whose second field does not match, which is 2 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001212 Explain(m, make_pair(1, -2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001213
zhanyong.wan82113312010-01-08 21:55:40 +00001214 // If the first field doesn't match but the second does, Pair()
1215 // should explain about the first field.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001216 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001217 Explain(m, make_pair(-1, 2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001218
zhanyong.wan82113312010-01-08 21:55:40 +00001219 // If both fields match, Pair() should explain about them both.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001220 EXPECT_EQ("whose both fields match, where the first field is a value "
1221 "which is 1 more than 0, and the second field is a value "
1222 "which is 2 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001223 Explain(m, make_pair(1, 2)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001224
1225 // If only the first match has an explanation, only this explanation should
1226 // be printed.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001227 const Matcher<pair<int, int> > explain_first = Pair(GreaterThan(0), 0);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001228 EXPECT_EQ("whose both fields match, where the first field is a value "
1229 "which is 1 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001230 Explain(explain_first, make_pair(1, 0)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001231
1232 // If only the second match has an explanation, only this explanation should
1233 // be printed.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001234 const Matcher<pair<int, int> > explain_second = Pair(0, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001235 EXPECT_EQ("whose both fields match, where the second field is a value "
1236 "which is 1 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001237 Explain(explain_second, make_pair(0, 1)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001238}
1239
1240TEST(PairTest, MatchesCorrectly) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001241 pair<int, std::string> p(25, "foo");
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001242
1243 // Both fields match.
1244 EXPECT_THAT(p, Pair(25, "foo"));
1245 EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o")));
1246
1247 // 'first' doesnt' match, but 'second' matches.
1248 EXPECT_THAT(p, Not(Pair(42, "foo")));
1249 EXPECT_THAT(p, Not(Pair(Lt(25), "foo")));
1250
1251 // 'first' matches, but 'second' doesn't match.
1252 EXPECT_THAT(p, Not(Pair(25, "bar")));
1253 EXPECT_THAT(p, Not(Pair(25, Not("foo"))));
1254
1255 // Neither field matches.
1256 EXPECT_THAT(p, Not(Pair(13, "bar")));
1257 EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a"))));
1258}
1259
1260TEST(PairTest, SafelyCastsInnerMatchers) {
1261 Matcher<int> is_positive = Gt(0);
1262 Matcher<int> is_negative = Lt(0);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001263 pair<char, bool> p('a', true);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001264 EXPECT_THAT(p, Pair(is_positive, _));
1265 EXPECT_THAT(p, Not(Pair(is_negative, _)));
1266 EXPECT_THAT(p, Pair(_, is_positive));
1267 EXPECT_THAT(p, Not(Pair(_, is_negative)));
1268}
1269
1270TEST(PairTest, InsideContainsUsingMap) {
zhanyong.wan95b12332009-09-25 18:55:50 +00001271 std::map<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001272 container.insert(make_pair(1, 'a'));
1273 container.insert(make_pair(2, 'b'));
1274 container.insert(make_pair(4, 'c'));
zhanyong.wan95b12332009-09-25 18:55:50 +00001275 EXPECT_THAT(container, Contains(Pair(1, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001276 EXPECT_THAT(container, Contains(Pair(1, _)));
zhanyong.wan95b12332009-09-25 18:55:50 +00001277 EXPECT_THAT(container, Contains(Pair(_, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001278 EXPECT_THAT(container, Not(Contains(Pair(3, _))));
1279}
1280
shiqiane35fdd92008-12-10 05:08:54 +00001281// Tests StartsWith(s).
1282
1283TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
1284 const Matcher<const char*> m1 = StartsWith(string(""));
1285 EXPECT_TRUE(m1.Matches("Hi"));
1286 EXPECT_TRUE(m1.Matches(""));
1287 EXPECT_FALSE(m1.Matches(NULL));
1288
1289 const Matcher<const string&> m2 = StartsWith("Hi");
1290 EXPECT_TRUE(m2.Matches("Hi"));
1291 EXPECT_TRUE(m2.Matches("Hi Hi!"));
1292 EXPECT_TRUE(m2.Matches("High"));
1293 EXPECT_FALSE(m2.Matches("H"));
1294 EXPECT_FALSE(m2.Matches(" Hi"));
1295}
1296
1297TEST(StartsWithTest, CanDescribeSelf) {
1298 Matcher<const std::string> m = StartsWith("Hi");
1299 EXPECT_EQ("starts with \"Hi\"", Describe(m));
1300}
1301
1302// Tests EndsWith(s).
1303
1304TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
1305 const Matcher<const char*> m1 = EndsWith("");
1306 EXPECT_TRUE(m1.Matches("Hi"));
1307 EXPECT_TRUE(m1.Matches(""));
1308 EXPECT_FALSE(m1.Matches(NULL));
1309
1310 const Matcher<const string&> m2 = EndsWith(string("Hi"));
1311 EXPECT_TRUE(m2.Matches("Hi"));
1312 EXPECT_TRUE(m2.Matches("Wow Hi Hi"));
1313 EXPECT_TRUE(m2.Matches("Super Hi"));
1314 EXPECT_FALSE(m2.Matches("i"));
1315 EXPECT_FALSE(m2.Matches("Hi "));
1316}
1317
1318TEST(EndsWithTest, CanDescribeSelf) {
1319 Matcher<const std::string> m = EndsWith("Hi");
1320 EXPECT_EQ("ends with \"Hi\"", Describe(m));
1321}
1322
shiqiane35fdd92008-12-10 05:08:54 +00001323// Tests MatchesRegex().
1324
1325TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
1326 const Matcher<const char*> m1 = MatchesRegex("a.*z");
1327 EXPECT_TRUE(m1.Matches("az"));
1328 EXPECT_TRUE(m1.Matches("abcz"));
1329 EXPECT_FALSE(m1.Matches(NULL));
1330
1331 const Matcher<const string&> m2 = MatchesRegex(new RE("a.*z"));
1332 EXPECT_TRUE(m2.Matches("azbz"));
1333 EXPECT_FALSE(m2.Matches("az1"));
1334 EXPECT_FALSE(m2.Matches("1az"));
1335}
1336
1337TEST(MatchesRegexTest, CanDescribeSelf) {
1338 Matcher<const std::string> m1 = MatchesRegex(string("Hi.*"));
1339 EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1));
1340
zhanyong.wand14aaed2010-01-14 05:36:32 +00001341 Matcher<const char*> m2 = MatchesRegex(new RE("a.*"));
1342 EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +00001343}
1344
1345// Tests ContainsRegex().
1346
1347TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
1348 const Matcher<const char*> m1 = ContainsRegex(string("a.*z"));
1349 EXPECT_TRUE(m1.Matches("az"));
1350 EXPECT_TRUE(m1.Matches("0abcz1"));
1351 EXPECT_FALSE(m1.Matches(NULL));
1352
1353 const Matcher<const string&> m2 = ContainsRegex(new RE("a.*z"));
1354 EXPECT_TRUE(m2.Matches("azbz"));
1355 EXPECT_TRUE(m2.Matches("az1"));
1356 EXPECT_FALSE(m2.Matches("1a"));
1357}
1358
1359TEST(ContainsRegexTest, CanDescribeSelf) {
1360 Matcher<const std::string> m1 = ContainsRegex("Hi.*");
1361 EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1));
1362
zhanyong.wand14aaed2010-01-14 05:36:32 +00001363 Matcher<const char*> m2 = ContainsRegex(new RE("a.*"));
1364 EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +00001365}
shiqiane35fdd92008-12-10 05:08:54 +00001366
1367// Tests for wide strings.
1368#if GTEST_HAS_STD_WSTRING
1369TEST(StdWideStrEqTest, MatchesEqual) {
1370 Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello"));
1371 EXPECT_TRUE(m.Matches(L"Hello"));
1372 EXPECT_FALSE(m.Matches(L"hello"));
1373 EXPECT_FALSE(m.Matches(NULL));
1374
1375 Matcher<const ::std::wstring&> m2 = StrEq(L"Hello");
1376 EXPECT_TRUE(m2.Matches(L"Hello"));
1377 EXPECT_FALSE(m2.Matches(L"Hi"));
1378
1379 Matcher<const ::std::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1380 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1381 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1382
1383 ::std::wstring str(L"01204500800");
1384 str[3] = L'\0';
1385 Matcher<const ::std::wstring&> m4 = StrEq(str);
1386 EXPECT_TRUE(m4.Matches(str));
1387 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1388 Matcher<const ::std::wstring&> m5 = StrEq(str);
1389 EXPECT_TRUE(m5.Matches(str));
1390}
1391
1392TEST(StdWideStrEqTest, CanDescribeSelf) {
1393 Matcher< ::std::wstring> m = StrEq(L"Hi-\'\"\?\\\a\b\f\n\r\t\v");
1394 EXPECT_EQ("is equal to L\"Hi-\'\\\"\\?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
1395 Describe(m));
1396
1397 Matcher< ::std::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1398 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1399 Describe(m2));
1400
1401 ::std::wstring str(L"01204500800");
1402 str[3] = L'\0';
1403 Matcher<const ::std::wstring&> m4 = StrEq(str);
1404 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1405 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1406 Matcher<const ::std::wstring&> m5 = StrEq(str);
1407 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1408}
1409
1410TEST(StdWideStrNeTest, MatchesUnequalString) {
1411 Matcher<const wchar_t*> m = StrNe(L"Hello");
1412 EXPECT_TRUE(m.Matches(L""));
1413 EXPECT_TRUE(m.Matches(NULL));
1414 EXPECT_FALSE(m.Matches(L"Hello"));
1415
1416 Matcher< ::std::wstring> m2 = StrNe(::std::wstring(L"Hello"));
1417 EXPECT_TRUE(m2.Matches(L"hello"));
1418 EXPECT_FALSE(m2.Matches(L"Hello"));
1419}
1420
1421TEST(StdWideStrNeTest, CanDescribeSelf) {
1422 Matcher<const wchar_t*> m = StrNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001423 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001424}
1425
1426TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1427 Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello"));
1428 EXPECT_TRUE(m.Matches(L"Hello"));
1429 EXPECT_TRUE(m.Matches(L"hello"));
1430 EXPECT_FALSE(m.Matches(L"Hi"));
1431 EXPECT_FALSE(m.Matches(NULL));
1432
1433 Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello");
1434 EXPECT_TRUE(m2.Matches(L"hello"));
1435 EXPECT_FALSE(m2.Matches(L"Hi"));
1436}
1437
1438TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1439 ::std::wstring str1(L"oabocdooeoo");
1440 ::std::wstring str2(L"OABOCDOOEOO");
1441 Matcher<const ::std::wstring&> m0 = StrCaseEq(str1);
1442 EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L'\0')));
1443
1444 str1[3] = str2[3] = L'\0';
1445 Matcher<const ::std::wstring&> m1 = StrCaseEq(str1);
1446 EXPECT_TRUE(m1.Matches(str2));
1447
1448 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1449 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1450 Matcher<const ::std::wstring&> m2 = StrCaseEq(str1);
1451 str1[9] = str2[9] = L'\0';
1452 EXPECT_FALSE(m2.Matches(str2));
1453
1454 Matcher<const ::std::wstring&> m3 = StrCaseEq(str1);
1455 EXPECT_TRUE(m3.Matches(str2));
1456
1457 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1458 str2.append(1, L'\0');
1459 EXPECT_FALSE(m3.Matches(str2));
1460 EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9)));
1461}
1462
1463TEST(StdWideStrCaseEqTest, CanDescribeSelf) {
1464 Matcher< ::std::wstring> m = StrCaseEq(L"Hi");
1465 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1466}
1467
1468TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1469 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1470 EXPECT_TRUE(m.Matches(L"Hi"));
1471 EXPECT_TRUE(m.Matches(NULL));
1472 EXPECT_FALSE(m.Matches(L"Hello"));
1473 EXPECT_FALSE(m.Matches(L"hello"));
1474
1475 Matcher< ::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello"));
1476 EXPECT_TRUE(m2.Matches(L""));
1477 EXPECT_FALSE(m2.Matches(L"Hello"));
1478}
1479
1480TEST(StdWideStrCaseNeTest, CanDescribeSelf) {
1481 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001482 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001483}
1484
1485// Tests that HasSubstr() works for matching wstring-typed values.
1486TEST(StdWideHasSubstrTest, WorksForStringClasses) {
1487 const Matcher< ::std::wstring> m1 = HasSubstr(L"foo");
1488 EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food.")));
1489 EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo")));
1490
1491 const Matcher<const ::std::wstring&> m2 = HasSubstr(L"foo");
1492 EXPECT_TRUE(m2.Matches(::std::wstring(L"I love food.")));
1493 EXPECT_FALSE(m2.Matches(::std::wstring(L"tofo")));
1494}
1495
1496// Tests that HasSubstr() works for matching C-wide-string-typed values.
1497TEST(StdWideHasSubstrTest, WorksForCStrings) {
1498 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1499 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1500 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1501 EXPECT_FALSE(m1.Matches(NULL));
1502
1503 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1504 EXPECT_TRUE(m2.Matches(L"I love food."));
1505 EXPECT_FALSE(m2.Matches(L"tofo"));
1506 EXPECT_FALSE(m2.Matches(NULL));
1507}
1508
1509// Tests that HasSubstr(s) describes itself properly.
1510TEST(StdWideHasSubstrTest, CanDescribeSelf) {
1511 Matcher< ::std::wstring> m = HasSubstr(L"foo\n\"");
1512 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1513}
1514
1515// Tests StartsWith(s).
1516
1517TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) {
1518 const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L""));
1519 EXPECT_TRUE(m1.Matches(L"Hi"));
1520 EXPECT_TRUE(m1.Matches(L""));
1521 EXPECT_FALSE(m1.Matches(NULL));
1522
1523 const Matcher<const ::std::wstring&> m2 = StartsWith(L"Hi");
1524 EXPECT_TRUE(m2.Matches(L"Hi"));
1525 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1526 EXPECT_TRUE(m2.Matches(L"High"));
1527 EXPECT_FALSE(m2.Matches(L"H"));
1528 EXPECT_FALSE(m2.Matches(L" Hi"));
1529}
1530
1531TEST(StdWideStartsWithTest, CanDescribeSelf) {
1532 Matcher<const ::std::wstring> m = StartsWith(L"Hi");
1533 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1534}
1535
1536// Tests EndsWith(s).
1537
1538TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) {
1539 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1540 EXPECT_TRUE(m1.Matches(L"Hi"));
1541 EXPECT_TRUE(m1.Matches(L""));
1542 EXPECT_FALSE(m1.Matches(NULL));
1543
1544 const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L"Hi"));
1545 EXPECT_TRUE(m2.Matches(L"Hi"));
1546 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1547 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1548 EXPECT_FALSE(m2.Matches(L"i"));
1549 EXPECT_FALSE(m2.Matches(L"Hi "));
1550}
1551
1552TEST(StdWideEndsWithTest, CanDescribeSelf) {
1553 Matcher<const ::std::wstring> m = EndsWith(L"Hi");
1554 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1555}
1556
1557#endif // GTEST_HAS_STD_WSTRING
1558
1559#if GTEST_HAS_GLOBAL_WSTRING
1560TEST(GlobalWideStrEqTest, MatchesEqual) {
1561 Matcher<const wchar_t*> m = StrEq(::wstring(L"Hello"));
1562 EXPECT_TRUE(m.Matches(L"Hello"));
1563 EXPECT_FALSE(m.Matches(L"hello"));
1564 EXPECT_FALSE(m.Matches(NULL));
1565
1566 Matcher<const ::wstring&> m2 = StrEq(L"Hello");
1567 EXPECT_TRUE(m2.Matches(L"Hello"));
1568 EXPECT_FALSE(m2.Matches(L"Hi"));
1569
1570 Matcher<const ::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1571 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1572 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1573
1574 ::wstring str(L"01204500800");
1575 str[3] = L'\0';
1576 Matcher<const ::wstring&> m4 = StrEq(str);
1577 EXPECT_TRUE(m4.Matches(str));
1578 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1579 Matcher<const ::wstring&> m5 = StrEq(str);
1580 EXPECT_TRUE(m5.Matches(str));
1581}
1582
1583TEST(GlobalWideStrEqTest, CanDescribeSelf) {
1584 Matcher< ::wstring> m = StrEq(L"Hi-\'\"\?\\\a\b\f\n\r\t\v");
1585 EXPECT_EQ("is equal to L\"Hi-\'\\\"\\?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
1586 Describe(m));
1587
1588 Matcher< ::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1589 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1590 Describe(m2));
1591
1592 ::wstring str(L"01204500800");
1593 str[3] = L'\0';
1594 Matcher<const ::wstring&> m4 = StrEq(str);
1595 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1596 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1597 Matcher<const ::wstring&> m5 = StrEq(str);
1598 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1599}
1600
1601TEST(GlobalWideStrNeTest, MatchesUnequalString) {
1602 Matcher<const wchar_t*> m = StrNe(L"Hello");
1603 EXPECT_TRUE(m.Matches(L""));
1604 EXPECT_TRUE(m.Matches(NULL));
1605 EXPECT_FALSE(m.Matches(L"Hello"));
1606
1607 Matcher< ::wstring> m2 = StrNe(::wstring(L"Hello"));
1608 EXPECT_TRUE(m2.Matches(L"hello"));
1609 EXPECT_FALSE(m2.Matches(L"Hello"));
1610}
1611
1612TEST(GlobalWideStrNeTest, CanDescribeSelf) {
1613 Matcher<const wchar_t*> m = StrNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001614 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001615}
1616
1617TEST(GlobalWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1618 Matcher<const wchar_t*> m = StrCaseEq(::wstring(L"Hello"));
1619 EXPECT_TRUE(m.Matches(L"Hello"));
1620 EXPECT_TRUE(m.Matches(L"hello"));
1621 EXPECT_FALSE(m.Matches(L"Hi"));
1622 EXPECT_FALSE(m.Matches(NULL));
1623
1624 Matcher<const ::wstring&> m2 = StrCaseEq(L"Hello");
1625 EXPECT_TRUE(m2.Matches(L"hello"));
1626 EXPECT_FALSE(m2.Matches(L"Hi"));
1627}
1628
1629TEST(GlobalWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1630 ::wstring str1(L"oabocdooeoo");
1631 ::wstring str2(L"OABOCDOOEOO");
1632 Matcher<const ::wstring&> m0 = StrCaseEq(str1);
1633 EXPECT_FALSE(m0.Matches(str2 + ::wstring(1, L'\0')));
1634
1635 str1[3] = str2[3] = L'\0';
1636 Matcher<const ::wstring&> m1 = StrCaseEq(str1);
1637 EXPECT_TRUE(m1.Matches(str2));
1638
1639 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1640 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1641 Matcher<const ::wstring&> m2 = StrCaseEq(str1);
1642 str1[9] = str2[9] = L'\0';
1643 EXPECT_FALSE(m2.Matches(str2));
1644
1645 Matcher<const ::wstring&> m3 = StrCaseEq(str1);
1646 EXPECT_TRUE(m3.Matches(str2));
1647
1648 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1649 str2.append(1, L'\0');
1650 EXPECT_FALSE(m3.Matches(str2));
1651 EXPECT_FALSE(m3.Matches(::wstring(str2, 0, 9)));
1652}
1653
1654TEST(GlobalWideStrCaseEqTest, CanDescribeSelf) {
1655 Matcher< ::wstring> m = StrCaseEq(L"Hi");
1656 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1657}
1658
1659TEST(GlobalWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1660 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1661 EXPECT_TRUE(m.Matches(L"Hi"));
1662 EXPECT_TRUE(m.Matches(NULL));
1663 EXPECT_FALSE(m.Matches(L"Hello"));
1664 EXPECT_FALSE(m.Matches(L"hello"));
1665
1666 Matcher< ::wstring> m2 = StrCaseNe(::wstring(L"Hello"));
1667 EXPECT_TRUE(m2.Matches(L""));
1668 EXPECT_FALSE(m2.Matches(L"Hello"));
1669}
1670
1671TEST(GlobalWideStrCaseNeTest, CanDescribeSelf) {
1672 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001673 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001674}
1675
1676// Tests that HasSubstr() works for matching wstring-typed values.
1677TEST(GlobalWideHasSubstrTest, WorksForStringClasses) {
1678 const Matcher< ::wstring> m1 = HasSubstr(L"foo");
1679 EXPECT_TRUE(m1.Matches(::wstring(L"I love food.")));
1680 EXPECT_FALSE(m1.Matches(::wstring(L"tofo")));
1681
1682 const Matcher<const ::wstring&> m2 = HasSubstr(L"foo");
1683 EXPECT_TRUE(m2.Matches(::wstring(L"I love food.")));
1684 EXPECT_FALSE(m2.Matches(::wstring(L"tofo")));
1685}
1686
1687// Tests that HasSubstr() works for matching C-wide-string-typed values.
1688TEST(GlobalWideHasSubstrTest, WorksForCStrings) {
1689 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1690 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1691 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1692 EXPECT_FALSE(m1.Matches(NULL));
1693
1694 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1695 EXPECT_TRUE(m2.Matches(L"I love food."));
1696 EXPECT_FALSE(m2.Matches(L"tofo"));
1697 EXPECT_FALSE(m2.Matches(NULL));
1698}
1699
1700// Tests that HasSubstr(s) describes itself properly.
1701TEST(GlobalWideHasSubstrTest, CanDescribeSelf) {
1702 Matcher< ::wstring> m = HasSubstr(L"foo\n\"");
1703 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1704}
1705
1706// Tests StartsWith(s).
1707
1708TEST(GlobalWideStartsWithTest, MatchesStringWithGivenPrefix) {
1709 const Matcher<const wchar_t*> m1 = StartsWith(::wstring(L""));
1710 EXPECT_TRUE(m1.Matches(L"Hi"));
1711 EXPECT_TRUE(m1.Matches(L""));
1712 EXPECT_FALSE(m1.Matches(NULL));
1713
1714 const Matcher<const ::wstring&> m2 = StartsWith(L"Hi");
1715 EXPECT_TRUE(m2.Matches(L"Hi"));
1716 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1717 EXPECT_TRUE(m2.Matches(L"High"));
1718 EXPECT_FALSE(m2.Matches(L"H"));
1719 EXPECT_FALSE(m2.Matches(L" Hi"));
1720}
1721
1722TEST(GlobalWideStartsWithTest, CanDescribeSelf) {
1723 Matcher<const ::wstring> m = StartsWith(L"Hi");
1724 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1725}
1726
1727// Tests EndsWith(s).
1728
1729TEST(GlobalWideEndsWithTest, MatchesStringWithGivenSuffix) {
1730 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1731 EXPECT_TRUE(m1.Matches(L"Hi"));
1732 EXPECT_TRUE(m1.Matches(L""));
1733 EXPECT_FALSE(m1.Matches(NULL));
1734
1735 const Matcher<const ::wstring&> m2 = EndsWith(::wstring(L"Hi"));
1736 EXPECT_TRUE(m2.Matches(L"Hi"));
1737 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1738 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1739 EXPECT_FALSE(m2.Matches(L"i"));
1740 EXPECT_FALSE(m2.Matches(L"Hi "));
1741}
1742
1743TEST(GlobalWideEndsWithTest, CanDescribeSelf) {
1744 Matcher<const ::wstring> m = EndsWith(L"Hi");
1745 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1746}
1747
1748#endif // GTEST_HAS_GLOBAL_WSTRING
1749
1750
1751typedef ::std::tr1::tuple<long, int> Tuple2; // NOLINT
1752
1753// Tests that Eq() matches a 2-tuple where the first field == the
1754// second field.
1755TEST(Eq2Test, MatchesEqualArguments) {
1756 Matcher<const Tuple2&> m = Eq();
1757 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1758 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1759}
1760
1761// Tests that Eq() describes itself properly.
1762TEST(Eq2Test, CanDescribeSelf) {
1763 Matcher<const Tuple2&> m = Eq();
zhanyong.wan2661c682009-06-09 05:42:12 +00001764 EXPECT_EQ("are a pair (x, y) where x == y", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001765}
1766
1767// Tests that Ge() matches a 2-tuple where the first field >= the
1768// second field.
1769TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) {
1770 Matcher<const Tuple2&> m = Ge();
1771 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1772 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1773 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1774}
1775
1776// Tests that Ge() describes itself properly.
1777TEST(Ge2Test, CanDescribeSelf) {
1778 Matcher<const Tuple2&> m = Ge();
zhanyong.wan2661c682009-06-09 05:42:12 +00001779 EXPECT_EQ("are a pair (x, y) where x >= y", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001780}
1781
1782// Tests that Gt() matches a 2-tuple where the first field > the
1783// second field.
1784TEST(Gt2Test, MatchesGreaterThanArguments) {
1785 Matcher<const Tuple2&> m = Gt();
1786 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1787 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1788 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1789}
1790
1791// Tests that Gt() describes itself properly.
1792TEST(Gt2Test, CanDescribeSelf) {
1793 Matcher<const Tuple2&> m = Gt();
zhanyong.wan2661c682009-06-09 05:42:12 +00001794 EXPECT_EQ("are a pair (x, y) where x > y", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001795}
1796
1797// Tests that Le() matches a 2-tuple where the first field <= the
1798// second field.
1799TEST(Le2Test, MatchesLessThanOrEqualArguments) {
1800 Matcher<const Tuple2&> m = Le();
1801 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1802 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1803 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1804}
1805
1806// Tests that Le() describes itself properly.
1807TEST(Le2Test, CanDescribeSelf) {
1808 Matcher<const Tuple2&> m = Le();
zhanyong.wan2661c682009-06-09 05:42:12 +00001809 EXPECT_EQ("are a pair (x, y) where x <= y", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001810}
1811
1812// Tests that Lt() matches a 2-tuple where the first field < the
1813// second field.
1814TEST(Lt2Test, MatchesLessThanArguments) {
1815 Matcher<const Tuple2&> m = Lt();
1816 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1817 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1818 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1819}
1820
1821// Tests that Lt() describes itself properly.
1822TEST(Lt2Test, CanDescribeSelf) {
1823 Matcher<const Tuple2&> m = Lt();
zhanyong.wan2661c682009-06-09 05:42:12 +00001824 EXPECT_EQ("are a pair (x, y) where x < y", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001825}
1826
1827// Tests that Ne() matches a 2-tuple where the first field != the
1828// second field.
1829TEST(Ne2Test, MatchesUnequalArguments) {
1830 Matcher<const Tuple2&> m = Ne();
1831 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1832 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1833 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1834}
1835
1836// Tests that Ne() describes itself properly.
1837TEST(Ne2Test, CanDescribeSelf) {
1838 Matcher<const Tuple2&> m = Ne();
zhanyong.wan2661c682009-06-09 05:42:12 +00001839 EXPECT_EQ("are a pair (x, y) where x != y", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001840}
1841
1842// Tests that Not(m) matches any value that doesn't match m.
1843TEST(NotTest, NegatesMatcher) {
1844 Matcher<int> m;
1845 m = Not(Eq(2));
1846 EXPECT_TRUE(m.Matches(3));
1847 EXPECT_FALSE(m.Matches(2));
1848}
1849
1850// Tests that Not(m) describes itself properly.
1851TEST(NotTest, CanDescribeSelf) {
1852 Matcher<int> m = Not(Eq(5));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001853 EXPECT_EQ("isn't equal to 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001854}
1855
zhanyong.wan18490652009-05-11 18:54:08 +00001856// Tests that monomorphic matchers are safely cast by the Not matcher.
1857TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {
1858 // greater_than_5 is a monomorphic matcher.
1859 Matcher<int> greater_than_5 = Gt(5);
1860
1861 Matcher<const int&> m = Not(greater_than_5);
1862 Matcher<int&> m2 = Not(greater_than_5);
1863 Matcher<int&> m3 = Not(m);
1864}
1865
shiqiane35fdd92008-12-10 05:08:54 +00001866// Tests that AllOf(m1, ..., mn) matches any value that matches all of
1867// the given matchers.
1868TEST(AllOfTest, MatchesWhenAllMatch) {
1869 Matcher<int> m;
1870 m = AllOf(Le(2), Ge(1));
1871 EXPECT_TRUE(m.Matches(1));
1872 EXPECT_TRUE(m.Matches(2));
1873 EXPECT_FALSE(m.Matches(0));
1874 EXPECT_FALSE(m.Matches(3));
1875
1876 m = AllOf(Gt(0), Ne(1), Ne(2));
1877 EXPECT_TRUE(m.Matches(3));
1878 EXPECT_FALSE(m.Matches(2));
1879 EXPECT_FALSE(m.Matches(1));
1880 EXPECT_FALSE(m.Matches(0));
1881
1882 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
1883 EXPECT_TRUE(m.Matches(4));
1884 EXPECT_FALSE(m.Matches(3));
1885 EXPECT_FALSE(m.Matches(2));
1886 EXPECT_FALSE(m.Matches(1));
1887 EXPECT_FALSE(m.Matches(0));
1888
1889 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
1890 EXPECT_TRUE(m.Matches(0));
1891 EXPECT_TRUE(m.Matches(1));
1892 EXPECT_FALSE(m.Matches(3));
1893}
1894
1895// Tests that AllOf(m1, ..., mn) describes itself properly.
1896TEST(AllOfTest, CanDescribeSelf) {
1897 Matcher<int> m;
1898 m = AllOf(Le(2), Ge(1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001899 EXPECT_EQ("(is <= 2) and (is >= 1)", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001900
1901 m = AllOf(Gt(0), Ne(1), Ne(2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001902 EXPECT_EQ("(is > 0) and "
1903 "((isn't equal to 1) and "
1904 "(isn't equal to 2))",
shiqiane35fdd92008-12-10 05:08:54 +00001905 Describe(m));
1906
1907
1908 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001909 EXPECT_EQ("(is > 0) and "
1910 "((isn't equal to 1) and "
1911 "((isn't equal to 2) and "
1912 "(isn't equal to 3)))",
shiqiane35fdd92008-12-10 05:08:54 +00001913 Describe(m));
1914
1915
1916 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001917 EXPECT_EQ("(is >= 0) and "
1918 "((is < 10) and "
1919 "((isn't equal to 3) and "
1920 "((isn't equal to 5) and "
1921 "(isn't equal to 7))))",
1922 Describe(m));
1923}
1924
1925// Tests that AllOf(m1, ..., mn) describes its negation properly.
1926TEST(AllOfTest, CanDescribeNegation) {
1927 Matcher<int> m;
1928 m = AllOf(Le(2), Ge(1));
1929 EXPECT_EQ("(isn't <= 2) or "
1930 "(isn't >= 1)",
1931 DescribeNegation(m));
1932
1933 m = AllOf(Gt(0), Ne(1), Ne(2));
1934 EXPECT_EQ("(isn't > 0) or "
1935 "((is equal to 1) or "
1936 "(is equal to 2))",
1937 DescribeNegation(m));
1938
1939
1940 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
1941 EXPECT_EQ("(isn't > 0) or "
1942 "((is equal to 1) or "
1943 "((is equal to 2) or "
1944 "(is equal to 3)))",
1945 DescribeNegation(m));
1946
1947
1948 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
1949 EXPECT_EQ("(isn't >= 0) or "
1950 "((isn't < 10) or "
1951 "((is equal to 3) or "
1952 "((is equal to 5) or "
1953 "(is equal to 7))))",
1954 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00001955}
1956
zhanyong.wan18490652009-05-11 18:54:08 +00001957// Tests that monomorphic matchers are safely cast by the AllOf matcher.
1958TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
1959 // greater_than_5 and less_than_10 are monomorphic matchers.
1960 Matcher<int> greater_than_5 = Gt(5);
1961 Matcher<int> less_than_10 = Lt(10);
1962
1963 Matcher<const int&> m = AllOf(greater_than_5, less_than_10);
1964 Matcher<int&> m2 = AllOf(greater_than_5, less_than_10);
1965 Matcher<int&> m3 = AllOf(greater_than_5, m2);
1966
1967 // Tests that BothOf works when composing itself.
1968 Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10);
1969 Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);
1970}
1971
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001972TEST(AllOfTest, ExplainsResult) {
1973 Matcher<int> m;
1974
1975 // Successful match. Both matchers need to explain. The second
1976 // matcher doesn't give an explanation, so only the first matcher's
1977 // explanation is printed.
1978 m = AllOf(GreaterThan(10), Lt(30));
1979 EXPECT_EQ("which is 15 more than 10", Explain(m, 25));
1980
1981 // Successful match. Both matchers need to explain.
1982 m = AllOf(GreaterThan(10), GreaterThan(20));
1983 EXPECT_EQ("which is 20 more than 10, and which is 10 more than 20",
1984 Explain(m, 30));
1985
1986 // Successful match. All matchers need to explain. The second
1987 // matcher doesn't given an explanation.
1988 m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20));
1989 EXPECT_EQ("which is 15 more than 10, and which is 5 more than 20",
1990 Explain(m, 25));
1991
1992 // Successful match. All matchers need to explain.
1993 m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
1994 EXPECT_EQ("which is 30 more than 10, and which is 20 more than 20, "
1995 "and which is 10 more than 30",
1996 Explain(m, 40));
1997
1998 // Failed match. The first matcher, which failed, needs to
1999 // explain.
2000 m = AllOf(GreaterThan(10), GreaterThan(20));
2001 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
2002
2003 // Failed match. The second matcher, which failed, needs to
2004 // explain. Since it doesn't given an explanation, nothing is
2005 // printed.
2006 m = AllOf(GreaterThan(10), Lt(30));
2007 EXPECT_EQ("", Explain(m, 40));
2008
2009 // Failed match. The second matcher, which failed, needs to
2010 // explain.
2011 m = AllOf(GreaterThan(10), GreaterThan(20));
2012 EXPECT_EQ("which is 5 less than 20", Explain(m, 15));
2013}
2014
shiqiane35fdd92008-12-10 05:08:54 +00002015// Tests that AnyOf(m1, ..., mn) matches any value that matches at
2016// least one of the given matchers.
2017TEST(AnyOfTest, MatchesWhenAnyMatches) {
2018 Matcher<int> m;
2019 m = AnyOf(Le(1), Ge(3));
2020 EXPECT_TRUE(m.Matches(1));
2021 EXPECT_TRUE(m.Matches(4));
2022 EXPECT_FALSE(m.Matches(2));
2023
2024 m = AnyOf(Lt(0), Eq(1), Eq(2));
2025 EXPECT_TRUE(m.Matches(-1));
2026 EXPECT_TRUE(m.Matches(1));
2027 EXPECT_TRUE(m.Matches(2));
2028 EXPECT_FALSE(m.Matches(0));
2029
2030 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2031 EXPECT_TRUE(m.Matches(-1));
2032 EXPECT_TRUE(m.Matches(1));
2033 EXPECT_TRUE(m.Matches(2));
2034 EXPECT_TRUE(m.Matches(3));
2035 EXPECT_FALSE(m.Matches(0));
2036
2037 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2038 EXPECT_TRUE(m.Matches(0));
2039 EXPECT_TRUE(m.Matches(11));
2040 EXPECT_TRUE(m.Matches(3));
2041 EXPECT_FALSE(m.Matches(2));
2042}
2043
2044// Tests that AnyOf(m1, ..., mn) describes itself properly.
2045TEST(AnyOfTest, CanDescribeSelf) {
2046 Matcher<int> m;
2047 m = AnyOf(Le(1), Ge(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002048 EXPECT_EQ("(is <= 1) or (is >= 3)",
shiqiane35fdd92008-12-10 05:08:54 +00002049 Describe(m));
2050
2051 m = AnyOf(Lt(0), Eq(1), Eq(2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002052 EXPECT_EQ("(is < 0) or "
shiqiane35fdd92008-12-10 05:08:54 +00002053 "((is equal to 1) or (is equal to 2))",
2054 Describe(m));
2055
2056 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002057 EXPECT_EQ("(is < 0) or "
shiqiane35fdd92008-12-10 05:08:54 +00002058 "((is equal to 1) or "
2059 "((is equal to 2) or "
2060 "(is equal to 3)))",
2061 Describe(m));
2062
2063 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002064 EXPECT_EQ("(is <= 0) or "
2065 "((is > 10) or "
shiqiane35fdd92008-12-10 05:08:54 +00002066 "((is equal to 3) or "
2067 "((is equal to 5) or "
2068 "(is equal to 7))))",
2069 Describe(m));
2070}
2071
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002072// Tests that AnyOf(m1, ..., mn) describes its negation properly.
2073TEST(AnyOfTest, CanDescribeNegation) {
2074 Matcher<int> m;
2075 m = AnyOf(Le(1), Ge(3));
2076 EXPECT_EQ("(isn't <= 1) and (isn't >= 3)",
2077 DescribeNegation(m));
2078
2079 m = AnyOf(Lt(0), Eq(1), Eq(2));
2080 EXPECT_EQ("(isn't < 0) and "
2081 "((isn't equal to 1) and (isn't equal to 2))",
2082 DescribeNegation(m));
2083
2084 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2085 EXPECT_EQ("(isn't < 0) and "
2086 "((isn't equal to 1) and "
2087 "((isn't equal to 2) and "
2088 "(isn't equal to 3)))",
2089 DescribeNegation(m));
2090
2091 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2092 EXPECT_EQ("(isn't <= 0) and "
2093 "((isn't > 10) and "
2094 "((isn't equal to 3) and "
2095 "((isn't equal to 5) and "
2096 "(isn't equal to 7))))",
2097 DescribeNegation(m));
2098}
2099
zhanyong.wan18490652009-05-11 18:54:08 +00002100// Tests that monomorphic matchers are safely cast by the AnyOf matcher.
2101TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
2102 // greater_than_5 and less_than_10 are monomorphic matchers.
2103 Matcher<int> greater_than_5 = Gt(5);
2104 Matcher<int> less_than_10 = Lt(10);
2105
2106 Matcher<const int&> m = AnyOf(greater_than_5, less_than_10);
2107 Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10);
2108 Matcher<int&> m3 = AnyOf(greater_than_5, m2);
2109
2110 // Tests that EitherOf works when composing itself.
2111 Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10);
2112 Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);
2113}
2114
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002115TEST(AnyOfTest, ExplainsResult) {
2116 Matcher<int> m;
2117
2118 // Failed match. Both matchers need to explain. The second
2119 // matcher doesn't give an explanation, so only the first matcher's
2120 // explanation is printed.
2121 m = AnyOf(GreaterThan(10), Lt(0));
2122 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
2123
2124 // Failed match. Both matchers need to explain.
2125 m = AnyOf(GreaterThan(10), GreaterThan(20));
2126 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20",
2127 Explain(m, 5));
2128
2129 // Failed match. All matchers need to explain. The second
2130 // matcher doesn't given an explanation.
2131 m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30));
2132 EXPECT_EQ("which is 5 less than 10, and which is 25 less than 30",
2133 Explain(m, 5));
2134
2135 // Failed match. All matchers need to explain.
2136 m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2137 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20, "
2138 "and which is 25 less than 30",
2139 Explain(m, 5));
2140
2141 // Successful match. The first matcher, which succeeded, needs to
2142 // explain.
2143 m = AnyOf(GreaterThan(10), GreaterThan(20));
2144 EXPECT_EQ("which is 5 more than 10", Explain(m, 15));
2145
2146 // Successful match. The second matcher, which succeeded, needs to
2147 // explain. Since it doesn't given an explanation, nothing is
2148 // printed.
2149 m = AnyOf(GreaterThan(10), Lt(30));
2150 EXPECT_EQ("", Explain(m, 0));
2151
2152 // Successful match. The second matcher, which succeeded, needs to
2153 // explain.
2154 m = AnyOf(GreaterThan(30), GreaterThan(20));
2155 EXPECT_EQ("which is 5 more than 20", Explain(m, 25));
2156}
2157
shiqiane35fdd92008-12-10 05:08:54 +00002158// The following predicate function and predicate functor are for
2159// testing the Truly(predicate) matcher.
2160
2161// Returns non-zero if the input is positive. Note that the return
2162// type of this function is not bool. It's OK as Truly() accepts any
2163// unary function or functor whose return type can be implicitly
2164// converted to bool.
2165int IsPositive(double x) {
2166 return x > 0 ? 1 : 0;
2167}
2168
2169// This functor returns true if the input is greater than the given
2170// number.
2171class IsGreaterThan {
2172 public:
2173 explicit IsGreaterThan(int threshold) : threshold_(threshold) {}
2174
2175 bool operator()(int n) const { return n > threshold_; }
zhanyong.wan32de5f52009-12-23 00:13:23 +00002176
shiqiane35fdd92008-12-10 05:08:54 +00002177 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002178 int threshold_;
shiqiane35fdd92008-12-10 05:08:54 +00002179};
2180
2181// For testing Truly().
2182const int foo = 0;
2183
2184// This predicate returns true iff the argument references foo and has
2185// a zero value.
2186bool ReferencesFooAndIsZero(const int& n) {
2187 return (&n == &foo) && (n == 0);
2188}
2189
2190// Tests that Truly(predicate) matches what satisfies the given
2191// predicate.
2192TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {
2193 Matcher<double> m = Truly(IsPositive);
2194 EXPECT_TRUE(m.Matches(2.0));
2195 EXPECT_FALSE(m.Matches(-1.5));
2196}
2197
2198// Tests that Truly(predicate_functor) works too.
2199TEST(TrulyTest, CanBeUsedWithFunctor) {
2200 Matcher<int> m = Truly(IsGreaterThan(5));
2201 EXPECT_TRUE(m.Matches(6));
2202 EXPECT_FALSE(m.Matches(4));
2203}
2204
2205// Tests that Truly(predicate) can describe itself properly.
2206TEST(TrulyTest, CanDescribeSelf) {
2207 Matcher<double> m = Truly(IsPositive);
2208 EXPECT_EQ("satisfies the given predicate",
2209 Describe(m));
2210}
2211
2212// Tests that Truly(predicate) works when the matcher takes its
2213// argument by reference.
2214TEST(TrulyTest, WorksForByRefArguments) {
2215 Matcher<const int&> m = Truly(ReferencesFooAndIsZero);
2216 EXPECT_TRUE(m.Matches(foo));
2217 int n = 0;
2218 EXPECT_FALSE(m.Matches(n));
2219}
2220
2221// Tests that Matches(m) is a predicate satisfied by whatever that
2222// matches matcher m.
2223TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {
2224 EXPECT_TRUE(Matches(Ge(0))(1));
2225 EXPECT_FALSE(Matches(Eq('a'))('b'));
2226}
2227
2228// Tests that Matches(m) works when the matcher takes its argument by
2229// reference.
2230TEST(MatchesTest, WorksOnByRefArguments) {
2231 int m = 0, n = 0;
2232 EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n));
2233 EXPECT_FALSE(Matches(Ref(m))(n));
2234}
2235
2236// Tests that a Matcher on non-reference type can be used in
2237// Matches().
2238TEST(MatchesTest, WorksWithMatcherOnNonRefType) {
2239 Matcher<int> eq5 = Eq(5);
2240 EXPECT_TRUE(Matches(eq5)(5));
2241 EXPECT_FALSE(Matches(eq5)(2));
2242}
2243
zhanyong.wanb8243162009-06-04 05:48:20 +00002244// Tests Value(value, matcher). Since Value() is a simple wrapper for
2245// Matches(), which has been tested already, we don't spend a lot of
2246// effort on testing Value().
2247TEST(ValueTest, WorksWithPolymorphicMatcher) {
2248 EXPECT_TRUE(Value("hi", StartsWith("h")));
2249 EXPECT_FALSE(Value(5, Gt(10)));
2250}
2251
2252TEST(ValueTest, WorksWithMonomorphicMatcher) {
2253 const Matcher<int> is_zero = Eq(0);
2254 EXPECT_TRUE(Value(0, is_zero));
2255 EXPECT_FALSE(Value('a', is_zero));
2256
2257 int n = 0;
2258 const Matcher<const int&> ref_n = Ref(n);
2259 EXPECT_TRUE(Value(n, ref_n));
2260 EXPECT_FALSE(Value(1, ref_n));
2261}
2262
zhanyong.wana862f1d2010-03-15 21:23:04 +00002263TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) {
zhanyong.wan34b034c2010-03-05 21:23:23 +00002264 StringMatchResultListener listener1;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002265 EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002266 EXPECT_EQ("% 2 == 0", listener1.str());
2267
2268 StringMatchResultListener listener2;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002269 EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002270 EXPECT_EQ("", listener2.str());
2271}
2272
zhanyong.wana862f1d2010-03-15 21:23:04 +00002273TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) {
zhanyong.wan34b034c2010-03-05 21:23:23 +00002274 const Matcher<int> is_even = PolymorphicIsEven();
2275 StringMatchResultListener listener1;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002276 EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002277 EXPECT_EQ("% 2 == 0", listener1.str());
2278
2279 const Matcher<const double&> is_zero = Eq(0);
2280 StringMatchResultListener listener2;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002281 EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002282 EXPECT_EQ("", listener2.str());
2283}
2284
zhanyong.wana862f1d2010-03-15 21:23:04 +00002285MATCHER_P(Really, inner_matcher, "") {
2286 return ExplainMatchResult(inner_matcher, arg, result_listener);
2287}
2288
2289TEST(ExplainMatchResultTest, WorksInsideMATCHER) {
2290 EXPECT_THAT(0, Really(Eq(0)));
2291}
2292
zhanyong.wanbf550852009-06-09 06:09:53 +00002293TEST(AllArgsTest, WorksForTuple) {
2294 EXPECT_THAT(make_tuple(1, 2L), AllArgs(Lt()));
2295 EXPECT_THAT(make_tuple(2L, 1), Not(AllArgs(Lt())));
2296}
2297
2298TEST(AllArgsTest, WorksForNonTuple) {
2299 EXPECT_THAT(42, AllArgs(Gt(0)));
2300 EXPECT_THAT('a', Not(AllArgs(Eq('b'))));
2301}
2302
2303class AllArgsHelper {
2304 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002305 AllArgsHelper() {}
2306
zhanyong.wanbf550852009-06-09 06:09:53 +00002307 MOCK_METHOD2(Helper, int(char x, int y));
zhanyong.wan32de5f52009-12-23 00:13:23 +00002308
2309 private:
2310 GTEST_DISALLOW_COPY_AND_ASSIGN_(AllArgsHelper);
zhanyong.wanbf550852009-06-09 06:09:53 +00002311};
2312
2313TEST(AllArgsTest, WorksInWithClause) {
2314 AllArgsHelper helper;
2315 ON_CALL(helper, Helper(_, _))
2316 .With(AllArgs(Lt()))
2317 .WillByDefault(Return(1));
2318 EXPECT_CALL(helper, Helper(_, _));
2319 EXPECT_CALL(helper, Helper(_, _))
2320 .With(AllArgs(Gt()))
2321 .WillOnce(Return(2));
2322
2323 EXPECT_EQ(1, helper.Helper('\1', 2));
2324 EXPECT_EQ(2, helper.Helper('a', 1));
2325}
2326
shiqiane35fdd92008-12-10 05:08:54 +00002327// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2328// matches the matcher.
2329TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {
2330 ASSERT_THAT(5, Ge(2)) << "This should succeed.";
2331 ASSERT_THAT("Foo", EndsWith("oo"));
2332 EXPECT_THAT(2, AllOf(Le(7), Ge(0))) << "This should succeed too.";
2333 EXPECT_THAT("Hello", StartsWith("Hell"));
2334}
2335
2336// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2337// doesn't match the matcher.
2338TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {
2339 // 'n' must be static as it is used in an EXPECT_FATAL_FAILURE(),
2340 // which cannot reference auto variables.
2341 static int n;
2342 n = 5;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002343
2344 // VC++ prior to version 8.0 SP1 has a bug where it will not see any
2345 // functions declared in the namespace scope from within nested classes.
2346 // EXPECT/ASSERT_(NON)FATAL_FAILURE macros use nested classes so that all
2347 // namespace-level functions invoked inside them need to be explicitly
2348 // resolved.
2349 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Gt(10)),
shiqiane35fdd92008-12-10 05:08:54 +00002350 "Value of: n\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002351 "Expected: is > 10\n"
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002352 " Actual: 5");
shiqiane35fdd92008-12-10 05:08:54 +00002353 n = 0;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002354 EXPECT_NONFATAL_FAILURE(
2355 EXPECT_THAT(n, ::testing::AllOf(::testing::Le(7), ::testing::Ge(5))),
2356 "Value of: n\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002357 "Expected: (is <= 7) and (is >= 5)\n"
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002358 " Actual: 0");
shiqiane35fdd92008-12-10 05:08:54 +00002359}
2360
2361// Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument
2362// has a reference type.
2363TEST(MatcherAssertionTest, WorksForByRefArguments) {
2364 // We use a static variable here as EXPECT_FATAL_FAILURE() cannot
2365 // reference auto variables.
2366 static int n;
2367 n = 0;
2368 EXPECT_THAT(n, AllOf(Le(7), Ref(n)));
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002369 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
shiqiane35fdd92008-12-10 05:08:54 +00002370 "Value of: n\n"
2371 "Expected: does not reference the variable @");
2372 // Tests the "Actual" part.
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002373 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002374 "Actual: 0, which is located @");
shiqiane35fdd92008-12-10 05:08:54 +00002375}
2376
zhanyong.wan95b12332009-09-25 18:55:50 +00002377#if !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002378// Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is
2379// monomorphic.
zhanyong.wan95b12332009-09-25 18:55:50 +00002380
2381// ASSERT_THAT("hello", starts_with_he) fails to compile with Nokia's
2382// Symbian compiler: it tries to compile
2383// template<T, U> class MatcherCastImpl { ...
zhanyong.wandb22c222010-01-28 21:52:29 +00002384// virtual bool MatchAndExplain(T x, ...) const {
2385// return source_matcher_.MatchAndExplain(static_cast<U>(x), ...);
zhanyong.wan95b12332009-09-25 18:55:50 +00002386// with U == string and T == const char*
2387// With ASSERT_THAT("hello"...) changed to ASSERT_THAT(string("hello") ... )
2388// the compiler silently crashes with no output.
2389// If MatcherCastImpl is changed to use U(x) instead of static_cast<U>(x)
2390// the code compiles but the converted string is bogus.
shiqiane35fdd92008-12-10 05:08:54 +00002391TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
2392 Matcher<const char*> starts_with_he = StartsWith("he");
2393 ASSERT_THAT("hello", starts_with_he);
2394
2395 Matcher<const string&> ends_with_ok = EndsWith("ok");
2396 ASSERT_THAT("book", ends_with_ok);
2397
2398 Matcher<int> is_greater_than_5 = Gt(5);
2399 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5),
2400 "Value of: 5\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002401 "Expected: is > 5\n"
shiqiane35fdd92008-12-10 05:08:54 +00002402 " Actual: 5");
2403}
zhanyong.wan95b12332009-09-25 18:55:50 +00002404#endif // !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002405
2406// Tests floating-point matchers.
2407template <typename RawType>
2408class FloatingPointTest : public testing::Test {
2409 protected:
2410 typedef typename testing::internal::FloatingPoint<RawType> Floating;
2411 typedef typename Floating::Bits Bits;
2412
2413 virtual void SetUp() {
2414 const size_t max_ulps = Floating::kMaxUlps;
2415
2416 // The bits that represent 0.0.
2417 const Bits zero_bits = Floating(0).bits();
2418
2419 // Makes some numbers close to 0.0.
2420 close_to_positive_zero_ = Floating::ReinterpretBits(zero_bits + max_ulps/2);
2421 close_to_negative_zero_ = -Floating::ReinterpretBits(
2422 zero_bits + max_ulps - max_ulps/2);
2423 further_from_negative_zero_ = -Floating::ReinterpretBits(
2424 zero_bits + max_ulps + 1 - max_ulps/2);
2425
2426 // The bits that represent 1.0.
2427 const Bits one_bits = Floating(1).bits();
2428
2429 // Makes some numbers close to 1.0.
2430 close_to_one_ = Floating::ReinterpretBits(one_bits + max_ulps);
2431 further_from_one_ = Floating::ReinterpretBits(one_bits + max_ulps + 1);
2432
2433 // +infinity.
2434 infinity_ = Floating::Infinity();
2435
2436 // The bits that represent +infinity.
2437 const Bits infinity_bits = Floating(infinity_).bits();
2438
2439 // Makes some numbers close to infinity.
2440 close_to_infinity_ = Floating::ReinterpretBits(infinity_bits - max_ulps);
2441 further_from_infinity_ = Floating::ReinterpretBits(
2442 infinity_bits - max_ulps - 1);
2443
2444 // Makes some NAN's.
2445 nan1_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 1);
2446 nan2_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 200);
2447 }
2448
2449 void TestSize() {
2450 EXPECT_EQ(sizeof(RawType), sizeof(Bits));
2451 }
2452
2453 // A battery of tests for FloatingEqMatcher::Matches.
2454 // matcher_maker is a pointer to a function which creates a FloatingEqMatcher.
2455 void TestMatches(
2456 testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) {
2457 Matcher<RawType> m1 = matcher_maker(0.0);
2458 EXPECT_TRUE(m1.Matches(-0.0));
2459 EXPECT_TRUE(m1.Matches(close_to_positive_zero_));
2460 EXPECT_TRUE(m1.Matches(close_to_negative_zero_));
2461 EXPECT_FALSE(m1.Matches(1.0));
2462
2463 Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_);
2464 EXPECT_FALSE(m2.Matches(further_from_negative_zero_));
2465
2466 Matcher<RawType> m3 = matcher_maker(1.0);
2467 EXPECT_TRUE(m3.Matches(close_to_one_));
2468 EXPECT_FALSE(m3.Matches(further_from_one_));
2469
2470 // Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above.
2471 EXPECT_FALSE(m3.Matches(0.0));
2472
2473 Matcher<RawType> m4 = matcher_maker(-infinity_);
2474 EXPECT_TRUE(m4.Matches(-close_to_infinity_));
2475
2476 Matcher<RawType> m5 = matcher_maker(infinity_);
2477 EXPECT_TRUE(m5.Matches(close_to_infinity_));
2478
2479 // This is interesting as the representations of infinity_ and nan1_
2480 // are only 1 DLP apart.
2481 EXPECT_FALSE(m5.Matches(nan1_));
2482
2483 // matcher_maker can produce a Matcher<const RawType&>, which is needed in
2484 // some cases.
2485 Matcher<const RawType&> m6 = matcher_maker(0.0);
2486 EXPECT_TRUE(m6.Matches(-0.0));
2487 EXPECT_TRUE(m6.Matches(close_to_positive_zero_));
2488 EXPECT_FALSE(m6.Matches(1.0));
2489
2490 // matcher_maker can produce a Matcher<RawType&>, which is needed in some
2491 // cases.
2492 Matcher<RawType&> m7 = matcher_maker(0.0);
2493 RawType x = 0.0;
2494 EXPECT_TRUE(m7.Matches(x));
2495 x = 0.01f;
2496 EXPECT_FALSE(m7.Matches(x));
2497 }
2498
2499 // Pre-calculated numbers to be used by the tests.
2500
2501 static RawType close_to_positive_zero_;
2502 static RawType close_to_negative_zero_;
2503 static RawType further_from_negative_zero_;
2504
2505 static RawType close_to_one_;
2506 static RawType further_from_one_;
2507
2508 static RawType infinity_;
2509 static RawType close_to_infinity_;
2510 static RawType further_from_infinity_;
2511
2512 static RawType nan1_;
2513 static RawType nan2_;
2514};
2515
2516template <typename RawType>
2517RawType FloatingPointTest<RawType>::close_to_positive_zero_;
2518
2519template <typename RawType>
2520RawType FloatingPointTest<RawType>::close_to_negative_zero_;
2521
2522template <typename RawType>
2523RawType FloatingPointTest<RawType>::further_from_negative_zero_;
2524
2525template <typename RawType>
2526RawType FloatingPointTest<RawType>::close_to_one_;
2527
2528template <typename RawType>
2529RawType FloatingPointTest<RawType>::further_from_one_;
2530
2531template <typename RawType>
2532RawType FloatingPointTest<RawType>::infinity_;
2533
2534template <typename RawType>
2535RawType FloatingPointTest<RawType>::close_to_infinity_;
2536
2537template <typename RawType>
2538RawType FloatingPointTest<RawType>::further_from_infinity_;
2539
2540template <typename RawType>
2541RawType FloatingPointTest<RawType>::nan1_;
2542
2543template <typename RawType>
2544RawType FloatingPointTest<RawType>::nan2_;
2545
2546// Instantiate FloatingPointTest for testing floats.
2547typedef FloatingPointTest<float> FloatTest;
2548
2549TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) {
2550 TestMatches(&FloatEq);
2551}
2552
2553TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
2554 TestMatches(&NanSensitiveFloatEq);
2555}
2556
2557TEST_F(FloatTest, FloatEqCannotMatchNaN) {
2558 // FloatEq never matches NaN.
2559 Matcher<float> m = FloatEq(nan1_);
2560 EXPECT_FALSE(m.Matches(nan1_));
2561 EXPECT_FALSE(m.Matches(nan2_));
2562 EXPECT_FALSE(m.Matches(1.0));
2563}
2564
2565TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {
2566 // NanSensitiveFloatEq will match NaN.
2567 Matcher<float> m = NanSensitiveFloatEq(nan1_);
2568 EXPECT_TRUE(m.Matches(nan1_));
2569 EXPECT_TRUE(m.Matches(nan2_));
2570 EXPECT_FALSE(m.Matches(1.0));
2571}
2572
2573TEST_F(FloatTest, FloatEqCanDescribeSelf) {
2574 Matcher<float> m1 = FloatEq(2.0f);
2575 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002576 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002577
2578 Matcher<float> m2 = FloatEq(0.5f);
2579 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002580 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002581
2582 Matcher<float> m3 = FloatEq(nan1_);
2583 EXPECT_EQ("never matches", Describe(m3));
2584 EXPECT_EQ("is anything", DescribeNegation(m3));
2585}
2586
2587TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {
2588 Matcher<float> m1 = NanSensitiveFloatEq(2.0f);
2589 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002590 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002591
2592 Matcher<float> m2 = NanSensitiveFloatEq(0.5f);
2593 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002594 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002595
2596 Matcher<float> m3 = NanSensitiveFloatEq(nan1_);
2597 EXPECT_EQ("is NaN", Describe(m3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002598 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
shiqiane35fdd92008-12-10 05:08:54 +00002599}
2600
2601// Instantiate FloatingPointTest for testing doubles.
2602typedef FloatingPointTest<double> DoubleTest;
2603
2604TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
2605 TestMatches(&DoubleEq);
2606}
2607
2608TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
2609 TestMatches(&NanSensitiveDoubleEq);
2610}
2611
2612TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {
2613 // DoubleEq never matches NaN.
2614 Matcher<double> m = DoubleEq(nan1_);
2615 EXPECT_FALSE(m.Matches(nan1_));
2616 EXPECT_FALSE(m.Matches(nan2_));
2617 EXPECT_FALSE(m.Matches(1.0));
2618}
2619
2620TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {
2621 // NanSensitiveDoubleEq will match NaN.
2622 Matcher<double> m = NanSensitiveDoubleEq(nan1_);
2623 EXPECT_TRUE(m.Matches(nan1_));
2624 EXPECT_TRUE(m.Matches(nan2_));
2625 EXPECT_FALSE(m.Matches(1.0));
2626}
2627
2628TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {
2629 Matcher<double> m1 = DoubleEq(2.0);
2630 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002631 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002632
2633 Matcher<double> m2 = DoubleEq(0.5);
2634 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002635 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002636
2637 Matcher<double> m3 = DoubleEq(nan1_);
2638 EXPECT_EQ("never matches", Describe(m3));
2639 EXPECT_EQ("is anything", DescribeNegation(m3));
2640}
2641
2642TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {
2643 Matcher<double> m1 = NanSensitiveDoubleEq(2.0);
2644 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002645 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002646
2647 Matcher<double> m2 = NanSensitiveDoubleEq(0.5);
2648 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002649 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002650
2651 Matcher<double> m3 = NanSensitiveDoubleEq(nan1_);
2652 EXPECT_EQ("is NaN", Describe(m3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002653 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
shiqiane35fdd92008-12-10 05:08:54 +00002654}
2655
2656TEST(PointeeTest, RawPointer) {
2657 const Matcher<int*> m = Pointee(Ge(0));
2658
2659 int n = 1;
2660 EXPECT_TRUE(m.Matches(&n));
2661 n = -1;
2662 EXPECT_FALSE(m.Matches(&n));
2663 EXPECT_FALSE(m.Matches(NULL));
2664}
2665
2666TEST(PointeeTest, RawPointerToConst) {
2667 const Matcher<const double*> m = Pointee(Ge(0));
2668
2669 double x = 1;
2670 EXPECT_TRUE(m.Matches(&x));
2671 x = -1;
2672 EXPECT_FALSE(m.Matches(&x));
2673 EXPECT_FALSE(m.Matches(NULL));
2674}
2675
2676TEST(PointeeTest, ReferenceToConstRawPointer) {
2677 const Matcher<int* const &> m = Pointee(Ge(0));
2678
2679 int n = 1;
2680 EXPECT_TRUE(m.Matches(&n));
2681 n = -1;
2682 EXPECT_FALSE(m.Matches(&n));
2683 EXPECT_FALSE(m.Matches(NULL));
2684}
2685
2686TEST(PointeeTest, ReferenceToNonConstRawPointer) {
2687 const Matcher<double* &> m = Pointee(Ge(0));
2688
2689 double x = 1.0;
2690 double* p = &x;
2691 EXPECT_TRUE(m.Matches(p));
2692 x = -1;
2693 EXPECT_FALSE(m.Matches(p));
2694 p = NULL;
2695 EXPECT_FALSE(m.Matches(p));
2696}
2697
2698TEST(PointeeTest, NeverMatchesNull) {
2699 const Matcher<const char*> m = Pointee(_);
2700 EXPECT_FALSE(m.Matches(NULL));
2701}
2702
2703// Tests that we can write Pointee(value) instead of Pointee(Eq(value)).
2704TEST(PointeeTest, MatchesAgainstAValue) {
2705 const Matcher<int*> m = Pointee(5);
2706
2707 int n = 5;
2708 EXPECT_TRUE(m.Matches(&n));
2709 n = -1;
2710 EXPECT_FALSE(m.Matches(&n));
2711 EXPECT_FALSE(m.Matches(NULL));
2712}
2713
2714TEST(PointeeTest, CanDescribeSelf) {
2715 const Matcher<int*> m = Pointee(Gt(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002716 EXPECT_EQ("points to a value that is > 3", Describe(m));
2717 EXPECT_EQ("does not point to a value that is > 3",
shiqiane35fdd92008-12-10 05:08:54 +00002718 DescribeNegation(m));
2719}
2720
shiqiane35fdd92008-12-10 05:08:54 +00002721TEST(PointeeTest, CanExplainMatchResult) {
2722 const Matcher<const string*> m = Pointee(StartsWith("Hi"));
2723
2724 EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL)));
2725
2726 const Matcher<int*> m2 = Pointee(GreaterThan(1));
2727 int n = 3;
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002728 EXPECT_EQ("which points to 3, which is 2 more than 1",
2729 Explain(m2, &n));
2730}
2731
2732TEST(PointeeTest, AlwaysExplainsPointee) {
2733 const Matcher<int*> m = Pointee(0);
2734 int n = 42;
2735 EXPECT_EQ("which points to 42", Explain(m, &n));
shiqiane35fdd92008-12-10 05:08:54 +00002736}
2737
2738// An uncopyable class.
2739class Uncopyable {
2740 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002741 explicit Uncopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00002742
2743 int value() const { return value_; }
2744 private:
2745 const int value_;
2746 GTEST_DISALLOW_COPY_AND_ASSIGN_(Uncopyable);
2747};
2748
2749// Returns true iff x.value() is positive.
2750bool ValueIsPositive(const Uncopyable& x) { return x.value() > 0; }
2751
2752// A user-defined struct for testing Field().
2753struct AStruct {
2754 AStruct() : x(0), y(1.0), z(5), p(NULL) {}
2755 AStruct(const AStruct& rhs)
2756 : x(rhs.x), y(rhs.y), z(rhs.z.value()), p(rhs.p) {}
2757
2758 int x; // A non-const field.
2759 const double y; // A const field.
2760 Uncopyable z; // An uncopyable field.
2761 const char* p; // A pointer field.
zhanyong.wan32de5f52009-12-23 00:13:23 +00002762
2763 private:
2764 GTEST_DISALLOW_ASSIGN_(AStruct);
shiqiane35fdd92008-12-10 05:08:54 +00002765};
2766
2767// A derived struct for testing Field().
2768struct DerivedStruct : public AStruct {
2769 char ch;
zhanyong.wan32de5f52009-12-23 00:13:23 +00002770
2771 private:
2772 GTEST_DISALLOW_ASSIGN_(DerivedStruct);
shiqiane35fdd92008-12-10 05:08:54 +00002773};
2774
2775// Tests that Field(&Foo::field, ...) works when field is non-const.
2776TEST(FieldTest, WorksForNonConstField) {
2777 Matcher<AStruct> m = Field(&AStruct::x, Ge(0));
2778
2779 AStruct a;
2780 EXPECT_TRUE(m.Matches(a));
2781 a.x = -1;
2782 EXPECT_FALSE(m.Matches(a));
2783}
2784
2785// Tests that Field(&Foo::field, ...) works when field is const.
2786TEST(FieldTest, WorksForConstField) {
2787 AStruct a;
2788
2789 Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0));
2790 EXPECT_TRUE(m.Matches(a));
2791 m = Field(&AStruct::y, Le(0.0));
2792 EXPECT_FALSE(m.Matches(a));
2793}
2794
2795// Tests that Field(&Foo::field, ...) works when field is not copyable.
2796TEST(FieldTest, WorksForUncopyableField) {
2797 AStruct a;
2798
2799 Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive));
2800 EXPECT_TRUE(m.Matches(a));
2801 m = Field(&AStruct::z, Not(Truly(ValueIsPositive)));
2802 EXPECT_FALSE(m.Matches(a));
2803}
2804
2805// Tests that Field(&Foo::field, ...) works when field is a pointer.
2806TEST(FieldTest, WorksForPointerField) {
2807 // Matching against NULL.
2808 Matcher<AStruct> m = Field(&AStruct::p, static_cast<const char*>(NULL));
2809 AStruct a;
2810 EXPECT_TRUE(m.Matches(a));
2811 a.p = "hi";
2812 EXPECT_FALSE(m.Matches(a));
2813
2814 // Matching a pointer that is not NULL.
2815 m = Field(&AStruct::p, StartsWith("hi"));
2816 a.p = "hill";
2817 EXPECT_TRUE(m.Matches(a));
2818 a.p = "hole";
2819 EXPECT_FALSE(m.Matches(a));
2820}
2821
2822// Tests that Field() works when the object is passed by reference.
2823TEST(FieldTest, WorksForByRefArgument) {
2824 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2825
2826 AStruct a;
2827 EXPECT_TRUE(m.Matches(a));
2828 a.x = -1;
2829 EXPECT_FALSE(m.Matches(a));
2830}
2831
2832// Tests that Field(&Foo::field, ...) works when the argument's type
2833// is a sub-type of Foo.
2834TEST(FieldTest, WorksForArgumentOfSubType) {
2835 // Note that the matcher expects DerivedStruct but we say AStruct
2836 // inside Field().
2837 Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0));
2838
2839 DerivedStruct d;
2840 EXPECT_TRUE(m.Matches(d));
2841 d.x = -1;
2842 EXPECT_FALSE(m.Matches(d));
2843}
2844
2845// Tests that Field(&Foo::field, m) works when field's type and m's
2846// argument type are compatible but not the same.
2847TEST(FieldTest, WorksForCompatibleMatcherType) {
2848 // The field is an int, but the inner matcher expects a signed char.
2849 Matcher<const AStruct&> m = Field(&AStruct::x,
2850 Matcher<signed char>(Ge(0)));
2851
2852 AStruct a;
2853 EXPECT_TRUE(m.Matches(a));
2854 a.x = -1;
2855 EXPECT_FALSE(m.Matches(a));
2856}
2857
2858// Tests that Field() can describe itself.
2859TEST(FieldTest, CanDescribeSelf) {
2860 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2861
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002862 EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
2863 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00002864}
2865
2866// Tests that Field() can explain the match result.
2867TEST(FieldTest, CanExplainMatchResult) {
2868 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2869
2870 AStruct a;
2871 a.x = 1;
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002872 EXPECT_EQ("whose given field is 1", Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00002873
2874 m = Field(&AStruct::x, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002875 EXPECT_EQ("whose given field is 1, which is 1 more than 0", Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00002876}
2877
2878// Tests that Field() works when the argument is a pointer to const.
2879TEST(FieldForPointerTest, WorksForPointerToConst) {
2880 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2881
2882 AStruct a;
2883 EXPECT_TRUE(m.Matches(&a));
2884 a.x = -1;
2885 EXPECT_FALSE(m.Matches(&a));
2886}
2887
2888// Tests that Field() works when the argument is a pointer to non-const.
2889TEST(FieldForPointerTest, WorksForPointerToNonConst) {
2890 Matcher<AStruct*> m = Field(&AStruct::x, Ge(0));
2891
2892 AStruct a;
2893 EXPECT_TRUE(m.Matches(&a));
2894 a.x = -1;
2895 EXPECT_FALSE(m.Matches(&a));
2896}
2897
zhanyong.wan6953a722010-01-13 05:15:07 +00002898// Tests that Field() works when the argument is a reference to a const pointer.
2899TEST(FieldForPointerTest, WorksForReferenceToConstPointer) {
2900 Matcher<AStruct* const&> m = Field(&AStruct::x, Ge(0));
2901
2902 AStruct a;
2903 EXPECT_TRUE(m.Matches(&a));
2904 a.x = -1;
2905 EXPECT_FALSE(m.Matches(&a));
2906}
2907
shiqiane35fdd92008-12-10 05:08:54 +00002908// Tests that Field() does not match the NULL pointer.
2909TEST(FieldForPointerTest, DoesNotMatchNull) {
2910 Matcher<const AStruct*> m = Field(&AStruct::x, _);
2911 EXPECT_FALSE(m.Matches(NULL));
2912}
2913
2914// Tests that Field(&Foo::field, ...) works when the argument's type
2915// is a sub-type of const Foo*.
2916TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
2917 // Note that the matcher expects DerivedStruct but we say AStruct
2918 // inside Field().
2919 Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0));
2920
2921 DerivedStruct d;
2922 EXPECT_TRUE(m.Matches(&d));
2923 d.x = -1;
2924 EXPECT_FALSE(m.Matches(&d));
2925}
2926
2927// Tests that Field() can describe itself when used to match a pointer.
2928TEST(FieldForPointerTest, CanDescribeSelf) {
2929 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2930
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002931 EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
2932 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00002933}
2934
2935// Tests that Field() can explain the result of matching a pointer.
2936TEST(FieldForPointerTest, CanExplainMatchResult) {
2937 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2938
2939 AStruct a;
2940 a.x = 1;
2941 EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(NULL)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002942 EXPECT_EQ("which points to an object whose given field is 1", Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00002943
2944 m = Field(&AStruct::x, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002945 EXPECT_EQ("which points to an object whose given field is 1, "
2946 "which is 1 more than 0", Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00002947}
2948
2949// A user-defined class for testing Property().
2950class AClass {
2951 public:
2952 AClass() : n_(0) {}
2953
2954 // A getter that returns a non-reference.
2955 int n() const { return n_; }
2956
2957 void set_n(int new_n) { n_ = new_n; }
2958
2959 // A getter that returns a reference to const.
2960 const string& s() const { return s_; }
2961
2962 void set_s(const string& new_s) { s_ = new_s; }
2963
2964 // A getter that returns a reference to non-const.
2965 double& x() const { return x_; }
2966 private:
2967 int n_;
2968 string s_;
2969
2970 static double x_;
2971};
2972
2973double AClass::x_ = 0.0;
2974
2975// A derived class for testing Property().
2976class DerivedClass : public AClass {
2977 private:
2978 int k_;
2979};
2980
2981// Tests that Property(&Foo::property, ...) works when property()
2982// returns a non-reference.
2983TEST(PropertyTest, WorksForNonReferenceProperty) {
2984 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
2985
2986 AClass a;
2987 a.set_n(1);
2988 EXPECT_TRUE(m.Matches(a));
2989
2990 a.set_n(-1);
2991 EXPECT_FALSE(m.Matches(a));
2992}
2993
2994// Tests that Property(&Foo::property, ...) works when property()
2995// returns a reference to const.
2996TEST(PropertyTest, WorksForReferenceToConstProperty) {
2997 Matcher<const AClass&> m = Property(&AClass::s, StartsWith("hi"));
2998
2999 AClass a;
3000 a.set_s("hill");
3001 EXPECT_TRUE(m.Matches(a));
3002
3003 a.set_s("hole");
3004 EXPECT_FALSE(m.Matches(a));
3005}
3006
3007// Tests that Property(&Foo::property, ...) works when property()
3008// returns a reference to non-const.
3009TEST(PropertyTest, WorksForReferenceToNonConstProperty) {
3010 double x = 0.0;
3011 AClass a;
3012
3013 Matcher<const AClass&> m = Property(&AClass::x, Ref(x));
3014 EXPECT_FALSE(m.Matches(a));
3015
3016 m = Property(&AClass::x, Not(Ref(x)));
3017 EXPECT_TRUE(m.Matches(a));
3018}
3019
3020// Tests that Property(&Foo::property, ...) works when the argument is
3021// passed by value.
3022TEST(PropertyTest, WorksForByValueArgument) {
3023 Matcher<AClass> m = Property(&AClass::s, StartsWith("hi"));
3024
3025 AClass a;
3026 a.set_s("hill");
3027 EXPECT_TRUE(m.Matches(a));
3028
3029 a.set_s("hole");
3030 EXPECT_FALSE(m.Matches(a));
3031}
3032
3033// Tests that Property(&Foo::property, ...) works when the argument's
3034// type is a sub-type of Foo.
3035TEST(PropertyTest, WorksForArgumentOfSubType) {
3036 // The matcher expects a DerivedClass, but inside the Property() we
3037 // say AClass.
3038 Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0));
3039
3040 DerivedClass d;
3041 d.set_n(1);
3042 EXPECT_TRUE(m.Matches(d));
3043
3044 d.set_n(-1);
3045 EXPECT_FALSE(m.Matches(d));
3046}
3047
3048// Tests that Property(&Foo::property, m) works when property()'s type
3049// and m's argument type are compatible but different.
3050TEST(PropertyTest, WorksForCompatibleMatcherType) {
3051 // n() returns an int but the inner matcher expects a signed char.
3052 Matcher<const AClass&> m = Property(&AClass::n,
3053 Matcher<signed char>(Ge(0)));
3054
3055 AClass a;
3056 EXPECT_TRUE(m.Matches(a));
3057 a.set_n(-1);
3058 EXPECT_FALSE(m.Matches(a));
3059}
3060
3061// Tests that Property() can describe itself.
3062TEST(PropertyTest, CanDescribeSelf) {
3063 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3064
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003065 EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
3066 EXPECT_EQ("is an object whose given property isn't >= 0",
3067 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003068}
3069
3070// Tests that Property() can explain the match result.
3071TEST(PropertyTest, CanExplainMatchResult) {
3072 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3073
3074 AClass a;
3075 a.set_n(1);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003076 EXPECT_EQ("whose given property is 1", Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003077
3078 m = Property(&AClass::n, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003079 EXPECT_EQ("whose given property is 1, which is 1 more than 0", Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003080}
3081
3082// Tests that Property() works when the argument is a pointer to const.
3083TEST(PropertyForPointerTest, WorksForPointerToConst) {
3084 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3085
3086 AClass a;
3087 a.set_n(1);
3088 EXPECT_TRUE(m.Matches(&a));
3089
3090 a.set_n(-1);
3091 EXPECT_FALSE(m.Matches(&a));
3092}
3093
3094// Tests that Property() works when the argument is a pointer to non-const.
3095TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
3096 Matcher<AClass*> m = Property(&AClass::s, StartsWith("hi"));
3097
3098 AClass a;
3099 a.set_s("hill");
3100 EXPECT_TRUE(m.Matches(&a));
3101
3102 a.set_s("hole");
3103 EXPECT_FALSE(m.Matches(&a));
3104}
3105
zhanyong.wan6953a722010-01-13 05:15:07 +00003106// Tests that Property() works when the argument is a reference to a
3107// const pointer.
3108TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) {
3109 Matcher<AClass* const&> m = Property(&AClass::s, StartsWith("hi"));
3110
3111 AClass a;
3112 a.set_s("hill");
3113 EXPECT_TRUE(m.Matches(&a));
3114
3115 a.set_s("hole");
3116 EXPECT_FALSE(m.Matches(&a));
3117}
3118
shiqiane35fdd92008-12-10 05:08:54 +00003119// Tests that Property() does not match the NULL pointer.
3120TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
3121 Matcher<const AClass*> m = Property(&AClass::x, _);
3122 EXPECT_FALSE(m.Matches(NULL));
3123}
3124
3125// Tests that Property(&Foo::property, ...) works when the argument's
3126// type is a sub-type of const Foo*.
3127TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
3128 // The matcher expects a DerivedClass, but inside the Property() we
3129 // say AClass.
3130 Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0));
3131
3132 DerivedClass d;
3133 d.set_n(1);
3134 EXPECT_TRUE(m.Matches(&d));
3135
3136 d.set_n(-1);
3137 EXPECT_FALSE(m.Matches(&d));
3138}
3139
3140// Tests that Property() can describe itself when used to match a pointer.
3141TEST(PropertyForPointerTest, CanDescribeSelf) {
3142 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3143
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003144 EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
3145 EXPECT_EQ("is an object whose given property isn't >= 0",
3146 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003147}
3148
3149// Tests that Property() can explain the result of matching a pointer.
3150TEST(PropertyForPointerTest, CanExplainMatchResult) {
3151 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3152
3153 AClass a;
3154 a.set_n(1);
3155 EXPECT_EQ("", Explain(m, static_cast<const AClass*>(NULL)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003156 EXPECT_EQ("which points to an object whose given property is 1",
3157 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003158
3159 m = Property(&AClass::n, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003160 EXPECT_EQ("which points to an object whose given property is 1, "
3161 "which is 1 more than 0", Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003162}
3163
3164// Tests ResultOf.
3165
3166// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3167// function pointer.
3168string IntToStringFunction(int input) { return input == 1 ? "foo" : "bar"; }
3169
3170TEST(ResultOfTest, WorksForFunctionPointers) {
3171 Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(string("foo")));
3172
3173 EXPECT_TRUE(matcher.Matches(1));
3174 EXPECT_FALSE(matcher.Matches(2));
3175}
3176
3177// Tests that ResultOf() can describe itself.
3178TEST(ResultOfTest, CanDescribeItself) {
3179 Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo"));
3180
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003181 EXPECT_EQ("is mapped by the given callable to a value that "
3182 "is equal to \"foo\"", Describe(matcher));
3183 EXPECT_EQ("is mapped by the given callable to a value that "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003184 "isn't equal to \"foo\"", DescribeNegation(matcher));
shiqiane35fdd92008-12-10 05:08:54 +00003185}
3186
3187// Tests that ResultOf() can explain the match result.
3188int IntFunction(int input) { return input == 42 ? 80 : 90; }
3189
3190TEST(ResultOfTest, CanExplainMatchResult) {
3191 Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003192 EXPECT_EQ("which is mapped by the given callable to 90",
3193 Explain(matcher, 36));
shiqiane35fdd92008-12-10 05:08:54 +00003194
3195 matcher = ResultOf(&IntFunction, GreaterThan(85));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003196 EXPECT_EQ("which is mapped by the given callable to 90, "
3197 "which is 5 more than 85", Explain(matcher, 36));
shiqiane35fdd92008-12-10 05:08:54 +00003198}
3199
3200// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3201// returns a non-reference.
3202TEST(ResultOfTest, WorksForNonReferenceResults) {
3203 Matcher<int> matcher = ResultOf(&IntFunction, Eq(80));
3204
3205 EXPECT_TRUE(matcher.Matches(42));
3206 EXPECT_FALSE(matcher.Matches(36));
3207}
3208
3209// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3210// returns a reference to non-const.
3211double& DoubleFunction(double& input) { return input; }
3212
3213Uncopyable& RefUncopyableFunction(Uncopyable& obj) {
3214 return obj;
3215}
3216
3217TEST(ResultOfTest, WorksForReferenceToNonConstResults) {
3218 double x = 3.14;
3219 double x2 = x;
3220 Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x));
3221
3222 EXPECT_TRUE(matcher.Matches(x));
3223 EXPECT_FALSE(matcher.Matches(x2));
3224
3225 // Test that ResultOf works with uncopyable objects
3226 Uncopyable obj(0);
3227 Uncopyable obj2(0);
3228 Matcher<Uncopyable&> matcher2 =
3229 ResultOf(&RefUncopyableFunction, Ref(obj));
3230
3231 EXPECT_TRUE(matcher2.Matches(obj));
3232 EXPECT_FALSE(matcher2.Matches(obj2));
3233}
3234
3235// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3236// returns a reference to const.
3237const string& StringFunction(const string& input) { return input; }
3238
3239TEST(ResultOfTest, WorksForReferenceToConstResults) {
3240 string s = "foo";
3241 string s2 = s;
3242 Matcher<const string&> matcher = ResultOf(&StringFunction, Ref(s));
3243
3244 EXPECT_TRUE(matcher.Matches(s));
3245 EXPECT_FALSE(matcher.Matches(s2));
3246}
3247
3248// Tests that ResultOf(f, m) works when f(x) and m's
3249// argument types are compatible but different.
3250TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {
3251 // IntFunction() returns int but the inner matcher expects a signed char.
3252 Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85)));
3253
3254 EXPECT_TRUE(matcher.Matches(36));
3255 EXPECT_FALSE(matcher.Matches(42));
3256}
3257
shiqiane35fdd92008-12-10 05:08:54 +00003258// Tests that the program aborts when ResultOf is passed
3259// a NULL function pointer.
3260TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +00003261 EXPECT_DEATH_IF_SUPPORTED(
shiqiane35fdd92008-12-10 05:08:54 +00003262 ResultOf(static_cast<string(*)(int)>(NULL), Eq(string("foo"))),
3263 "NULL function pointer is passed into ResultOf\\(\\)\\.");
3264}
shiqiane35fdd92008-12-10 05:08:54 +00003265
3266// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3267// function reference.
3268TEST(ResultOfTest, WorksForFunctionReferences) {
3269 Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq("foo"));
3270 EXPECT_TRUE(matcher.Matches(1));
3271 EXPECT_FALSE(matcher.Matches(2));
3272}
3273
3274// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3275// function object.
3276struct Functor : public ::std::unary_function<int, string> {
3277 result_type operator()(argument_type input) const {
3278 return IntToStringFunction(input);
3279 }
3280};
3281
3282TEST(ResultOfTest, WorksForFunctors) {
3283 Matcher<int> matcher = ResultOf(Functor(), Eq(string("foo")));
3284
3285 EXPECT_TRUE(matcher.Matches(1));
3286 EXPECT_FALSE(matcher.Matches(2));
3287}
3288
3289// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3290// functor with more then one operator() defined. ResultOf() must work
3291// for each defined operator().
3292struct PolymorphicFunctor {
3293 typedef int result_type;
3294 int operator()(int n) { return n; }
3295 int operator()(const char* s) { return static_cast<int>(strlen(s)); }
3296};
3297
3298TEST(ResultOfTest, WorksForPolymorphicFunctors) {
3299 Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5));
3300
3301 EXPECT_TRUE(matcher_int.Matches(10));
3302 EXPECT_FALSE(matcher_int.Matches(2));
3303
3304 Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5));
3305
3306 EXPECT_TRUE(matcher_string.Matches("long string"));
3307 EXPECT_FALSE(matcher_string.Matches("shrt"));
3308}
3309
3310const int* ReferencingFunction(const int& n) { return &n; }
3311
3312struct ReferencingFunctor {
3313 typedef const int* result_type;
3314 result_type operator()(const int& n) { return &n; }
3315};
3316
3317TEST(ResultOfTest, WorksForReferencingCallables) {
3318 const int n = 1;
3319 const int n2 = 1;
3320 Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n));
3321 EXPECT_TRUE(matcher2.Matches(n));
3322 EXPECT_FALSE(matcher2.Matches(n2));
3323
3324 Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n));
3325 EXPECT_TRUE(matcher3.Matches(n));
3326 EXPECT_FALSE(matcher3.Matches(n2));
3327}
3328
shiqiane35fdd92008-12-10 05:08:54 +00003329class DivisibleByImpl {
3330 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003331 explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {}
shiqiane35fdd92008-12-10 05:08:54 +00003332
zhanyong.wandb22c222010-01-28 21:52:29 +00003333 // For testing using ExplainMatchResultTo() with polymorphic matchers.
shiqiane35fdd92008-12-10 05:08:54 +00003334 template <typename T>
zhanyong.wandb22c222010-01-28 21:52:29 +00003335 bool MatchAndExplain(const T& n, MatchResultListener* listener) const {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003336 *listener << "which is " << (n % divider_) << " modulo "
zhanyong.wandb22c222010-01-28 21:52:29 +00003337 << divider_;
shiqiane35fdd92008-12-10 05:08:54 +00003338 return (n % divider_) == 0;
3339 }
3340
3341 void DescribeTo(::std::ostream* os) const {
3342 *os << "is divisible by " << divider_;
3343 }
3344
3345 void DescribeNegationTo(::std::ostream* os) const {
3346 *os << "is not divisible by " << divider_;
3347 }
3348
zhanyong.wan32de5f52009-12-23 00:13:23 +00003349 void set_divider(int a_divider) { divider_ = a_divider; }
shiqiane35fdd92008-12-10 05:08:54 +00003350 int divider() const { return divider_; }
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003351
shiqiane35fdd92008-12-10 05:08:54 +00003352 private:
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003353 int divider_;
shiqiane35fdd92008-12-10 05:08:54 +00003354};
3355
shiqiane35fdd92008-12-10 05:08:54 +00003356PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) {
3357 return MakePolymorphicMatcher(DivisibleByImpl(n));
3358}
3359
3360// Tests that when AllOf() fails, only the first failing matcher is
3361// asked to explain why.
3362TEST(ExplainMatchResultTest, AllOf_False_False) {
3363 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003364 EXPECT_EQ("which is 1 modulo 4", Explain(m, 5));
shiqiane35fdd92008-12-10 05:08:54 +00003365}
3366
3367// Tests that when AllOf() fails, only the first failing matcher is
3368// asked to explain why.
3369TEST(ExplainMatchResultTest, AllOf_False_True) {
3370 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003371 EXPECT_EQ("which is 2 modulo 4", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003372}
3373
3374// Tests that when AllOf() fails, only the first failing matcher is
3375// asked to explain why.
3376TEST(ExplainMatchResultTest, AllOf_True_False) {
3377 const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003378 EXPECT_EQ("which is 2 modulo 3", Explain(m, 5));
shiqiane35fdd92008-12-10 05:08:54 +00003379}
3380
3381// Tests that when AllOf() succeeds, all matchers are asked to explain
3382// why.
3383TEST(ExplainMatchResultTest, AllOf_True_True) {
3384 const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003385 EXPECT_EQ("which is 0 modulo 2, and which is 0 modulo 3", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003386}
3387
3388TEST(ExplainMatchResultTest, AllOf_True_True_2) {
3389 const Matcher<int> m = AllOf(Ge(2), Le(3));
3390 EXPECT_EQ("", Explain(m, 2));
3391}
3392
3393TEST(ExplainmatcherResultTest, MonomorphicMatcher) {
3394 const Matcher<int> m = GreaterThan(5);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003395 EXPECT_EQ("which is 1 more than 5", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003396}
3397
3398// The following two tests verify that values without a public copy
3399// ctor can be used as arguments to matchers like Eq(), Ge(), and etc
3400// with the help of ByRef().
3401
3402class NotCopyable {
3403 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003404 explicit NotCopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00003405
3406 int value() const { return value_; }
3407
3408 bool operator==(const NotCopyable& rhs) const {
3409 return value() == rhs.value();
3410 }
3411
3412 bool operator>=(const NotCopyable& rhs) const {
3413 return value() >= rhs.value();
3414 }
3415 private:
3416 int value_;
3417
3418 GTEST_DISALLOW_COPY_AND_ASSIGN_(NotCopyable);
3419};
3420
3421TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) {
3422 const NotCopyable const_value1(1);
3423 const Matcher<const NotCopyable&> m = Eq(ByRef(const_value1));
3424
3425 const NotCopyable n1(1), n2(2);
3426 EXPECT_TRUE(m.Matches(n1));
3427 EXPECT_FALSE(m.Matches(n2));
3428}
3429
3430TEST(ByRefTest, AllowsNotCopyableValueInMatchers) {
3431 NotCopyable value2(2);
3432 const Matcher<NotCopyable&> m = Ge(ByRef(value2));
3433
3434 NotCopyable n1(1), n2(2);
3435 EXPECT_FALSE(m.Matches(n1));
3436 EXPECT_TRUE(m.Matches(n2));
3437}
3438
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003439#if GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003440// Tests ContainerEq with different container types, and
3441// different element types.
3442
3443template <typename T>
zhanyong.wanb8243162009-06-04 05:48:20 +00003444class ContainerEqTest : public testing::Test {};
zhanyong.wan6a896b52009-01-16 01:13:50 +00003445
3446typedef testing::Types<
3447 std::set<int>,
3448 std::vector<size_t>,
3449 std::multiset<size_t>,
3450 std::list<int> >
3451 ContainerEqTestTypes;
3452
3453TYPED_TEST_CASE(ContainerEqTest, ContainerEqTestTypes);
3454
3455// Tests that the filled container is equal to itself.
3456TYPED_TEST(ContainerEqTest, EqualsSelf) {
3457 static const int vals[] = {1, 1, 2, 3, 5, 8};
3458 TypeParam my_set(vals, vals + 6);
3459 const Matcher<TypeParam> m = ContainerEq(my_set);
3460 EXPECT_TRUE(m.Matches(my_set));
3461 EXPECT_EQ("", Explain(m, my_set));
3462}
3463
3464// Tests that missing values are reported.
3465TYPED_TEST(ContainerEqTest, ValueMissing) {
3466 static const int vals[] = {1, 1, 2, 3, 5, 8};
3467 static const int test_vals[] = {2, 1, 8, 5};
3468 TypeParam my_set(vals, vals + 6);
3469 TypeParam test_set(test_vals, test_vals + 4);
3470 const Matcher<TypeParam> m = ContainerEq(my_set);
3471 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003472 EXPECT_EQ("which doesn't have these expected elements: 3",
3473 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003474}
3475
3476// Tests that added values are reported.
3477TYPED_TEST(ContainerEqTest, ValueAdded) {
3478 static const int vals[] = {1, 1, 2, 3, 5, 8};
3479 static const int test_vals[] = {1, 2, 3, 5, 8, 46};
3480 TypeParam my_set(vals, vals + 6);
3481 TypeParam test_set(test_vals, test_vals + 6);
3482 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3483 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003484 EXPECT_EQ("which has these unexpected elements: 46", Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003485}
3486
3487// Tests that added and missing values are reported together.
3488TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) {
3489 static const int vals[] = {1, 1, 2, 3, 5, 8};
3490 static const int test_vals[] = {1, 2, 3, 8, 46};
3491 TypeParam my_set(vals, vals + 6);
3492 TypeParam test_set(test_vals, test_vals + 5);
3493 const Matcher<TypeParam> m = ContainerEq(my_set);
3494 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003495 EXPECT_EQ("which has these unexpected elements: 46,\n"
3496 "and doesn't have these expected elements: 5",
3497 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003498}
3499
3500// Tests duplicated value -- expect no explanation.
3501TYPED_TEST(ContainerEqTest, DuplicateDifference) {
3502 static const int vals[] = {1, 1, 2, 3, 5, 8};
3503 static const int test_vals[] = {1, 2, 3, 5, 8};
3504 TypeParam my_set(vals, vals + 6);
3505 TypeParam test_set(test_vals, test_vals + 5);
3506 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3507 // Depending on the container, match may be true or false
3508 // But in any case there should be no explanation.
3509 EXPECT_EQ("", Explain(m, test_set));
3510}
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003511#endif // GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003512
3513// Tests that mutliple missing values are reported.
3514// Using just vector here, so order is predicatble.
3515TEST(ContainerEqExtraTest, MultipleValuesMissing) {
3516 static const int vals[] = {1, 1, 2, 3, 5, 8};
3517 static const int test_vals[] = {2, 1, 5};
3518 std::vector<int> my_set(vals, vals + 6);
3519 std::vector<int> test_set(test_vals, test_vals + 3);
3520 const Matcher<std::vector<int> > m = ContainerEq(my_set);
3521 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003522 EXPECT_EQ("which doesn't have these expected elements: 3, 8",
3523 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003524}
3525
3526// Tests that added values are reported.
3527// Using just vector here, so order is predicatble.
3528TEST(ContainerEqExtraTest, MultipleValuesAdded) {
3529 static const int vals[] = {1, 1, 2, 3, 5, 8};
3530 static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46};
3531 std::list<size_t> my_set(vals, vals + 6);
3532 std::list<size_t> test_set(test_vals, test_vals + 7);
3533 const Matcher<const std::list<size_t>&> m = ContainerEq(my_set);
3534 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003535 EXPECT_EQ("which has these unexpected elements: 92, 46",
3536 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003537}
3538
3539// Tests that added and missing values are reported together.
3540TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) {
3541 static const int vals[] = {1, 1, 2, 3, 5, 8};
3542 static const int test_vals[] = {1, 2, 3, 92, 46};
3543 std::list<size_t> my_set(vals, vals + 6);
3544 std::list<size_t> test_set(test_vals, test_vals + 5);
3545 const Matcher<const std::list<size_t> > m = ContainerEq(my_set);
3546 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003547 EXPECT_EQ("which has these unexpected elements: 92, 46,\n"
3548 "and doesn't have these expected elements: 5, 8",
zhanyong.wan6a896b52009-01-16 01:13:50 +00003549 Explain(m, test_set));
3550}
3551
3552// Tests to see that duplicate elements are detected,
3553// but (as above) not reported in the explanation.
3554TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) {
3555 static const int vals[] = {1, 1, 2, 3, 5, 8};
3556 static const int test_vals[] = {1, 2, 3, 5, 8};
3557 std::vector<int> my_set(vals, vals + 6);
3558 std::vector<int> test_set(test_vals, test_vals + 5);
3559 const Matcher<std::vector<int> > m = ContainerEq(my_set);
3560 EXPECT_TRUE(m.Matches(my_set));
3561 EXPECT_FALSE(m.Matches(test_set));
3562 // There is nothing to report when both sets contain all the same values.
3563 EXPECT_EQ("", Explain(m, test_set));
3564}
3565
3566// Tests that ContainerEq works for non-trivial associative containers,
3567// like maps.
3568TEST(ContainerEqExtraTest, WorksForMaps) {
3569 std::map<int, std::string> my_map;
3570 my_map[0] = "a";
3571 my_map[1] = "b";
3572
3573 std::map<int, std::string> test_map;
3574 test_map[0] = "aa";
3575 test_map[1] = "b";
3576
3577 const Matcher<const std::map<int, std::string>&> m = ContainerEq(my_map);
3578 EXPECT_TRUE(m.Matches(my_map));
3579 EXPECT_FALSE(m.Matches(test_map));
3580
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003581 EXPECT_EQ("which has these unexpected elements: (0, \"aa\"),\n"
3582 "and doesn't have these expected elements: (0, \"a\")",
zhanyong.wan6a896b52009-01-16 01:13:50 +00003583 Explain(m, test_map));
3584}
3585
zhanyong.wanb8243162009-06-04 05:48:20 +00003586TEST(ContainerEqExtraTest, WorksForNativeArray) {
3587 int a1[] = { 1, 2, 3 };
3588 int a2[] = { 1, 2, 3 };
3589 int b[] = { 1, 2, 4 };
3590
3591 EXPECT_THAT(a1, ContainerEq(a2));
3592 EXPECT_THAT(a1, Not(ContainerEq(b)));
3593}
3594
3595TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) {
3596 const char a1[][3] = { "hi", "lo" };
3597 const char a2[][3] = { "hi", "lo" };
3598 const char b[][3] = { "lo", "hi" };
3599
3600 // Tests using ContainerEq() in the first dimension.
3601 EXPECT_THAT(a1, ContainerEq(a2));
3602 EXPECT_THAT(a1, Not(ContainerEq(b)));
3603
3604 // Tests using ContainerEq() in the second dimension.
3605 EXPECT_THAT(a1, ElementsAre(ContainerEq(a2[0]), ContainerEq(a2[1])));
3606 EXPECT_THAT(a1, ElementsAre(Not(ContainerEq(b[0])), ContainerEq(a2[1])));
3607}
3608
3609TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) {
3610 const int a1[] = { 1, 2, 3 };
3611 const int a2[] = { 1, 2, 3 };
3612 const int b[] = { 1, 2, 3, 4 };
3613
zhanyong.wan2661c682009-06-09 05:42:12 +00003614 const int* const p1 = a1;
3615 EXPECT_THAT(make_tuple(p1, 3), ContainerEq(a2));
3616 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(b)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003617
3618 const int c[] = { 1, 3, 2 };
zhanyong.wan2661c682009-06-09 05:42:12 +00003619 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(c)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003620}
3621
3622TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) {
3623 std::string a1[][3] = {
3624 { "hi", "hello", "ciao" },
3625 { "bye", "see you", "ciao" }
3626 };
3627
3628 std::string a2[][3] = {
3629 { "hi", "hello", "ciao" },
3630 { "bye", "see you", "ciao" }
3631 };
3632
3633 const Matcher<const std::string(&)[2][3]> m = ContainerEq(a2);
3634 EXPECT_THAT(a1, m);
3635
3636 a2[0][0] = "ha";
3637 EXPECT_THAT(a1, m);
3638}
3639
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003640// Tests GetParamIndex().
3641
3642TEST(GetParamIndexTest, WorksForEmptyParamList) {
3643 const char* params[] = { NULL };
3644 EXPECT_EQ(kTupleInterpolation, GetParamIndex(params, "*"));
3645 EXPECT_EQ(kInvalidInterpolation, GetParamIndex(params, "a"));
3646}
3647
3648TEST(GetParamIndexTest, RecognizesStar) {
3649 const char* params[] = { "a", "b", NULL };
3650 EXPECT_EQ(kTupleInterpolation, GetParamIndex(params, "*"));
3651}
3652
3653TEST(GetParamIndexTest, RecognizesKnownParam) {
3654 const char* params[] = { "foo", "bar", NULL };
3655 EXPECT_EQ(0, GetParamIndex(params, "foo"));
3656 EXPECT_EQ(1, GetParamIndex(params, "bar"));
3657}
3658
3659TEST(GetParamIndexTest, RejectsUnknownParam) {
3660 const char* params[] = { "foo", "bar", NULL };
3661 EXPECT_EQ(kInvalidInterpolation, GetParamIndex(params, "foobar"));
3662}
3663
3664// Tests SkipPrefix().
3665
3666TEST(SkipPrefixTest, SkipsWhenPrefixMatches) {
3667 const char* const str = "hello";
3668
3669 const char* p = str;
3670 EXPECT_TRUE(SkipPrefix("", &p));
3671 EXPECT_EQ(str, p);
3672
3673 p = str;
3674 EXPECT_TRUE(SkipPrefix("hell", &p));
3675 EXPECT_EQ(str + 4, p);
3676}
3677
3678TEST(SkipPrefixTest, DoesNotSkipWhenPrefixDoesNotMatch) {
3679 const char* const str = "world";
3680
3681 const char* p = str;
3682 EXPECT_FALSE(SkipPrefix("W", &p));
3683 EXPECT_EQ(str, p);
3684
3685 p = str;
3686 EXPECT_FALSE(SkipPrefix("world!", &p));
3687 EXPECT_EQ(str, p);
3688}
3689
3690// Tests FormatMatcherDescriptionSyntaxError().
3691TEST(FormatMatcherDescriptionSyntaxErrorTest, FormatsCorrectly) {
3692 const char* const description = "hello%world";
3693 EXPECT_EQ("Syntax error at index 5 in matcher description \"hello%world\": ",
3694 FormatMatcherDescriptionSyntaxError(description, description + 5));
3695}
3696
3697// Tests ValidateMatcherDescription().
3698
3699TEST(ValidateMatcherDescriptionTest, AcceptsEmptyDescription) {
3700 const char* params[] = { "foo", "bar", NULL };
3701 EXPECT_THAT(ValidateMatcherDescription(params, ""),
3702 ElementsAre());
3703}
3704
3705TEST(ValidateMatcherDescriptionTest,
3706 AcceptsNonEmptyDescriptionWithNoInterpolation) {
3707 const char* params[] = { "foo", "bar", NULL };
3708 EXPECT_THAT(ValidateMatcherDescription(params, "a simple description"),
3709 ElementsAre());
3710}
3711
zhanyong.wan82113312010-01-08 21:55:40 +00003712// The MATCHER*() macros trigger warning C4100 (unreferenced formal
3713// parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
3714// the macro definition, as the warnings are generated when the macro
3715// is expanded and macro expansion cannot contain #pragma. Therefore
3716// we suppress them here.
3717#ifdef _MSC_VER
3718#pragma warning(push)
3719#pragma warning(disable:4100)
3720#endif
3721
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003722// We use MATCHER_P3() to define a matcher for testing
3723// ValidateMatcherDescription(); otherwise we'll end up with much
3724// plumbing code. This is not circular as
3725// ValidateMatcherDescription() doesn't affect whether the matcher
3726// matches a value or not.
3727MATCHER_P3(EqInterpolation, start, end, index, "equals Interpolation%(*)s") {
3728 return arg.start_pos == start && arg.end_pos == end &&
3729 arg.param_index == index;
3730}
3731
zhanyong.wan82113312010-01-08 21:55:40 +00003732#ifdef _MSC_VER
3733#pragma warning(pop)
3734#endif
3735
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003736TEST(ValidateMatcherDescriptionTest, AcceptsPercentInterpolation) {
3737 const char* params[] = { "foo", NULL };
3738 const char* const desc = "one %%";
3739 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3740 ElementsAre(EqInterpolation(desc + 4, desc + 6,
3741 kPercentInterpolation)));
3742}
3743
3744TEST(ValidateMatcherDescriptionTest, AcceptsTupleInterpolation) {
3745 const char* params[] = { "foo", "bar", "baz", NULL };
3746 const char* const desc = "%(*)s after";
3747 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3748 ElementsAre(EqInterpolation(desc, desc + 5,
3749 kTupleInterpolation)));
3750}
3751
3752TEST(ValidateMatcherDescriptionTest, AcceptsParamInterpolation) {
3753 const char* params[] = { "foo", "bar", "baz", NULL };
3754 const char* const desc = "a %(bar)s.";
3755 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3756 ElementsAre(EqInterpolation(desc + 2, desc + 9, 1)));
3757}
3758
3759TEST(ValidateMatcherDescriptionTest, AcceptsMultiplenterpolations) {
3760 const char* params[] = { "foo", "bar", "baz", NULL };
3761 const char* const desc = "%(baz)s %(foo)s %(bar)s";
3762 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3763 ElementsAre(EqInterpolation(desc, desc + 7, 2),
3764 EqInterpolation(desc + 8, desc + 15, 0),
3765 EqInterpolation(desc + 16, desc + 23, 1)));
3766}
3767
3768TEST(ValidateMatcherDescriptionTest, AcceptsRepeatedParams) {
3769 const char* params[] = { "foo", "bar", NULL };
3770 const char* const desc = "%(foo)s and %(foo)s";
3771 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3772 ElementsAre(EqInterpolation(desc, desc + 7, 0),
3773 EqInterpolation(desc + 12, desc + 19, 0)));
3774}
3775
3776TEST(ValidateMatcherDescriptionTest, RejectsUnknownParam) {
3777 const char* params[] = { "a", "bar", NULL };
3778 EXPECT_NONFATAL_FAILURE({
3779 EXPECT_THAT(ValidateMatcherDescription(params, "%(foo)s"),
3780 ElementsAre());
3781 }, "Syntax error at index 2 in matcher description \"%(foo)s\": "
3782 "\"foo\" is an invalid parameter name.");
3783}
3784
3785TEST(ValidateMatcherDescriptionTest, RejectsUnfinishedParam) {
3786 const char* params[] = { "a", "bar", NULL };
3787 EXPECT_NONFATAL_FAILURE({
3788 EXPECT_THAT(ValidateMatcherDescription(params, "%(foo)"),
3789 ElementsAre());
3790 }, "Syntax error at index 0 in matcher description \"%(foo)\": "
3791 "an interpolation must end with \")s\", but \"%(foo)\" does not.");
3792
3793 EXPECT_NONFATAL_FAILURE({
3794 EXPECT_THAT(ValidateMatcherDescription(params, "x%(a"),
3795 ElementsAre());
3796 }, "Syntax error at index 1 in matcher description \"x%(a\": "
3797 "an interpolation must end with \")s\", but \"%(a\" does not.");
3798}
3799
3800TEST(ValidateMatcherDescriptionTest, RejectsSinglePercent) {
3801 const char* params[] = { "a", NULL };
3802 EXPECT_NONFATAL_FAILURE({
3803 EXPECT_THAT(ValidateMatcherDescription(params, "a %."),
3804 ElementsAre());
3805 }, "Syntax error at index 2 in matcher description \"a %.\": "
3806 "use \"%%\" instead of \"%\" to print \"%\".");
3807
3808}
3809
3810// Tests JoinAsTuple().
3811
3812TEST(JoinAsTupleTest, JoinsEmptyTuple) {
3813 EXPECT_EQ("", JoinAsTuple(Strings()));
3814}
3815
3816TEST(JoinAsTupleTest, JoinsOneTuple) {
3817 const char* fields[] = { "1" };
3818 EXPECT_EQ("1", JoinAsTuple(Strings(fields, fields + 1)));
3819}
3820
3821TEST(JoinAsTupleTest, JoinsTwoTuple) {
3822 const char* fields[] = { "1", "a" };
3823 EXPECT_EQ("(1, a)", JoinAsTuple(Strings(fields, fields + 2)));
3824}
3825
3826TEST(JoinAsTupleTest, JoinsTenTuple) {
3827 const char* fields[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
3828 EXPECT_EQ("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)",
3829 JoinAsTuple(Strings(fields, fields + 10)));
3830}
3831
3832// Tests FormatMatcherDescription().
3833
3834TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) {
3835 EXPECT_EQ("is even",
3836 FormatMatcherDescription("IsEven", "", Interpolations(),
3837 Strings()));
3838
3839 const char* params[] = { "5" };
3840 EXPECT_EQ("equals 5",
3841 FormatMatcherDescription("Equals", "", Interpolations(),
3842 Strings(params, params + 1)));
3843
3844 const char* params2[] = { "5", "8" };
3845 EXPECT_EQ("is in range (5, 8)",
3846 FormatMatcherDescription("IsInRange", "", Interpolations(),
3847 Strings(params2, params2 + 2)));
3848}
3849
3850TEST(FormatMatcherDescriptionTest, WorksForDescriptionWithNoInterpolation) {
3851 EXPECT_EQ("is positive",
3852 FormatMatcherDescription("Gt0", "is positive", Interpolations(),
3853 Strings()));
3854
3855 const char* params[] = { "5", "6" };
3856 EXPECT_EQ("is negative",
3857 FormatMatcherDescription("Lt0", "is negative", Interpolations(),
3858 Strings(params, params + 2)));
3859}
3860
3861TEST(FormatMatcherDescriptionTest,
3862 WorksWhenDescriptionStartsWithInterpolation) {
3863 const char* params[] = { "5" };
3864 const char* const desc = "%(num)s times bigger";
3865 const Interpolation interp[] = { Interpolation(desc, desc + 7, 0) };
3866 EXPECT_EQ("5 times bigger",
3867 FormatMatcherDescription("Foo", desc,
3868 Interpolations(interp, interp + 1),
3869 Strings(params, params + 1)));
3870}
3871
3872TEST(FormatMatcherDescriptionTest,
3873 WorksWhenDescriptionEndsWithInterpolation) {
3874 const char* params[] = { "5", "6" };
3875 const char* const desc = "is bigger than %(y)s";
3876 const Interpolation interp[] = { Interpolation(desc + 15, desc + 20, 1) };
3877 EXPECT_EQ("is bigger than 6",
3878 FormatMatcherDescription("Foo", desc,
3879 Interpolations(interp, interp + 1),
3880 Strings(params, params + 2)));
3881}
3882
3883TEST(FormatMatcherDescriptionTest,
3884 WorksWhenDescriptionStartsAndEndsWithInterpolation) {
3885 const char* params[] = { "5", "6" };
3886 const char* const desc = "%(x)s <= arg <= %(y)s";
3887 const Interpolation interp[] = {
3888 Interpolation(desc, desc + 5, 0),
3889 Interpolation(desc + 16, desc + 21, 1)
3890 };
3891 EXPECT_EQ("5 <= arg <= 6",
3892 FormatMatcherDescription("Foo", desc,
3893 Interpolations(interp, interp + 2),
3894 Strings(params, params + 2)));
3895}
3896
3897TEST(FormatMatcherDescriptionTest,
3898 WorksWhenDescriptionDoesNotStartOrEndWithInterpolation) {
3899 const char* params[] = { "5.2" };
3900 const char* const desc = "has %(x)s cents";
3901 const Interpolation interp[] = { Interpolation(desc + 4, desc + 9, 0) };
3902 EXPECT_EQ("has 5.2 cents",
3903 FormatMatcherDescription("Foo", desc,
3904 Interpolations(interp, interp + 1),
3905 Strings(params, params + 1)));
3906}
3907
3908TEST(FormatMatcherDescriptionTest,
3909 WorksWhenDescriptionContainsMultipleInterpolations) {
3910 const char* params[] = { "5", "6" };
3911 const char* const desc = "in %(*)s or [%(x)s, %(y)s]";
3912 const Interpolation interp[] = {
3913 Interpolation(desc + 3, desc + 8, kTupleInterpolation),
3914 Interpolation(desc + 13, desc + 18, 0),
3915 Interpolation(desc + 20, desc + 25, 1)
3916 };
3917 EXPECT_EQ("in (5, 6) or [5, 6]",
3918 FormatMatcherDescription("Foo", desc,
3919 Interpolations(interp, interp + 3),
3920 Strings(params, params + 2)));
3921}
3922
3923TEST(FormatMatcherDescriptionTest,
3924 WorksWhenDescriptionContainsRepeatedParams) {
3925 const char* params[] = { "9" };
3926 const char* const desc = "in [-%(x)s, %(x)s]";
3927 const Interpolation interp[] = {
3928 Interpolation(desc + 5, desc + 10, 0),
3929 Interpolation(desc + 12, desc + 17, 0)
3930 };
3931 EXPECT_EQ("in [-9, 9]",
3932 FormatMatcherDescription("Foo", desc,
3933 Interpolations(interp, interp + 2),
3934 Strings(params, params + 1)));
3935}
3936
3937TEST(FormatMatcherDescriptionTest,
3938 WorksForDescriptionWithInvalidInterpolation) {
3939 const char* params[] = { "9" };
3940 const char* const desc = "> %(x)s %(x)";
3941 const Interpolation interp[] = { Interpolation(desc + 2, desc + 7, 0) };
3942 EXPECT_EQ("> 9 %(x)",
3943 FormatMatcherDescription("Foo", desc,
3944 Interpolations(interp, interp + 1),
3945 Strings(params, params + 1)));
3946}
3947
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003948// Tests PolymorphicMatcher::mutable_impl().
3949TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {
3950 PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
3951 DivisibleByImpl& impl = m.mutable_impl();
3952 EXPECT_EQ(42, impl.divider());
3953
3954 impl.set_divider(0);
3955 EXPECT_EQ(0, m.mutable_impl().divider());
3956}
3957
3958// Tests PolymorphicMatcher::impl().
3959TEST(PolymorphicMatcherTest, CanAccessImpl) {
3960 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
3961 const DivisibleByImpl& impl = m.impl();
3962 EXPECT_EQ(42, impl.divider());
3963}
3964
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003965TEST(MatcherTupleTest, ExplainsMatchFailure) {
3966 stringstream ss1;
3967 ExplainMatchFailureTupleTo(make_tuple(Matcher<char>(Eq('a')), GreaterThan(5)),
3968 make_tuple('a', 10), &ss1);
3969 EXPECT_EQ("", ss1.str()); // Successful match.
3970
3971 stringstream ss2;
3972 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
3973 make_tuple(2, 'b'), &ss2);
3974 EXPECT_EQ(" Expected arg #0: is > 5\n"
3975 " Actual: 2, which is 3 less than 5\n"
3976 " Expected arg #1: is equal to 'a' (97)\n"
3977 " Actual: 'b' (98)\n",
3978 ss2.str()); // Failed match where both arguments need explanation.
3979
3980 stringstream ss3;
3981 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
3982 make_tuple(2, 'a'), &ss3);
3983 EXPECT_EQ(" Expected arg #0: is > 5\n"
3984 " Actual: 2, which is 3 less than 5\n",
3985 ss3.str()); // Failed match where only one argument needs
3986 // explanation.
3987}
3988
zhanyong.wan33605ba2010-04-22 23:37:47 +00003989// Tests Each().
3990
3991TEST(EachTest, ExplainsMatchResultCorrectly) {
3992 set<int> a; // empty
3993
3994 Matcher<set<int> > m = Each(2);
3995 EXPECT_EQ("", Explain(m, a));
3996
3997 Matcher<const int(&)[1]> n = Each(1);
3998
3999 const int b[1] = { 1 };
4000 EXPECT_EQ("", Explain(n, b));
4001
4002 n = Each(3);
4003 EXPECT_EQ("whose element #0 doesn't match", Explain(n, b));
4004
4005 a.insert(1);
4006 a.insert(2);
4007 a.insert(3);
4008 m = Each(GreaterThan(0));
4009 EXPECT_EQ("", Explain(m, a));
4010
4011 m = Each(GreaterThan(10));
4012 EXPECT_EQ("whose element #0 doesn't match, which is 9 less than 10",
4013 Explain(m, a));
4014}
4015
4016TEST(EachTest, DescribesItselfCorrectly) {
4017 Matcher<vector<int> > m = Each(1);
4018 EXPECT_EQ("only contains elements that is equal to 1", Describe(m));
4019
4020 Matcher<vector<int> > m2 = Not(m);
4021 EXPECT_EQ("contains some element that isn't equal to 1", Describe(m2));
4022}
4023
4024TEST(EachTest, MatchesVectorWhenAllElementsMatch) {
4025 vector<int> some_vector;
4026 EXPECT_THAT(some_vector, Each(1));
4027 some_vector.push_back(3);
4028 EXPECT_THAT(some_vector, Not(Each(1)));
4029 EXPECT_THAT(some_vector, Each(3));
4030 some_vector.push_back(1);
4031 some_vector.push_back(2);
4032 EXPECT_THAT(some_vector, Not(Each(3)));
4033 EXPECT_THAT(some_vector, Each(Lt(3.5)));
4034
4035 vector<string> another_vector;
4036 another_vector.push_back("fee");
4037 EXPECT_THAT(another_vector, Each(string("fee")));
4038 another_vector.push_back("fie");
4039 another_vector.push_back("foe");
4040 another_vector.push_back("fum");
4041 EXPECT_THAT(another_vector, Not(Each(string("fee"))));
4042}
4043
4044TEST(EachTest, MatchesMapWhenAllElementsMatch) {
4045 map<const char*, int> my_map;
4046 const char* bar = "a string";
4047 my_map[bar] = 2;
4048 EXPECT_THAT(my_map, Each(make_pair(bar, 2)));
4049
4050 map<string, int> another_map;
4051 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1)));
4052 another_map["fee"] = 1;
4053 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1)));
4054 another_map["fie"] = 2;
4055 another_map["foe"] = 3;
4056 another_map["fum"] = 4;
4057 EXPECT_THAT(another_map, Not(Each(make_pair(string("fee"), 1))));
4058 EXPECT_THAT(another_map, Not(Each(make_pair(string("fum"), 1))));
4059 EXPECT_THAT(another_map, Each(Pair(_, Gt(0))));
4060}
4061
4062TEST(EachTest, AcceptsMatcher) {
4063 const int a[] = { 1, 2, 3 };
4064 EXPECT_THAT(a, Each(Gt(0)));
4065 EXPECT_THAT(a, Not(Each(Gt(1))));
4066}
4067
4068TEST(EachTest, WorksForNativeArrayAsTuple) {
4069 const int a[] = { 1, 2 };
4070 const int* const pointer = a;
4071 EXPECT_THAT(make_tuple(pointer, 2), Each(Gt(0)));
4072 EXPECT_THAT(make_tuple(pointer, 2), Not(Each(Gt(1))));
4073}
4074
shiqiane35fdd92008-12-10 05:08:54 +00004075} // namespace gmock_matchers_test
4076} // namespace testing