blob: 35d59fa8ee223688412799b894c8fd2ea36137c3 [file] [log] [blame]
shiqiane35fdd92008-12-10 05:08:54 +00001// Copyright 2007, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29//
30// Author: wan@google.com (Zhanyong Wan)
31
32// Google Mock - a framework for writing C++ mock classes.
33//
34// This file tests some commonly used argument matchers.
35
zhanyong.wan53e08c42010-09-14 05:38:21 +000036#include "gmock/gmock-matchers.h"
shiqiane35fdd92008-12-10 05:08:54 +000037
38#include <string.h>
39#include <functional>
zhanyong.wana862f1d2010-03-15 21:23:04 +000040#include <iostream>
zhanyong.wan6a896b52009-01-16 01:13:50 +000041#include <list>
42#include <map>
43#include <set>
shiqiane35fdd92008-12-10 05:08:54 +000044#include <sstream>
zhanyong.wan6a896b52009-01-16 01:13:50 +000045#include <string>
zhanyong.wanf5e1ce52009-09-16 07:02:02 +000046#include <utility>
zhanyong.wan6a896b52009-01-16 01:13:50 +000047#include <vector>
zhanyong.wan53e08c42010-09-14 05:38:21 +000048#include "gmock/gmock.h"
49#include "gtest/gtest.h"
50#include "gtest/gtest-spi.h"
shiqiane35fdd92008-12-10 05:08:54 +000051
52namespace testing {
zhanyong.wan4a5330d2009-02-19 00:36:44 +000053
54namespace internal {
vladlosev587c1b32011-05-20 00:42:22 +000055GTEST_API_ string JoinAsTuple(const Strings& fields);
zhanyong.wan4a5330d2009-02-19 00:36:44 +000056} // namespace internal
57
shiqiane35fdd92008-12-10 05:08:54 +000058namespace gmock_matchers_test {
59
zhanyong.wan898725c2011-09-16 16:45:39 +000060using std::greater;
61using std::less;
zhanyong.wanab5b77c2010-05-17 19:32:48 +000062using std::list;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000063using std::make_pair;
zhanyong.wanb5937da2009-07-16 20:26:41 +000064using std::map;
65using std::multimap;
zhanyong.wanab5b77c2010-05-17 19:32:48 +000066using std::multiset;
67using std::ostream;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000068using std::pair;
zhanyong.wan33605ba2010-04-22 23:37:47 +000069using std::set;
shiqiane35fdd92008-12-10 05:08:54 +000070using std::stringstream;
zhanyong.wanab5b77c2010-05-17 19:32:48 +000071using std::tr1::get;
zhanyong.wanb8243162009-06-04 05:48:20 +000072using std::tr1::make_tuple;
zhanyong.wanab5b77c2010-05-17 19:32:48 +000073using std::tr1::tuple;
zhanyong.wan33605ba2010-04-22 23:37:47 +000074using std::vector;
shiqiane35fdd92008-12-10 05:08:54 +000075using testing::A;
zhanyong.wanbf550852009-06-09 06:09:53 +000076using testing::AllArgs;
shiqiane35fdd92008-12-10 05:08:54 +000077using testing::AllOf;
78using testing::An;
79using testing::AnyOf;
80using testing::ByRef;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000081using testing::ContainsRegex;
shiqiane35fdd92008-12-10 05:08:54 +000082using testing::DoubleEq;
83using testing::EndsWith;
84using testing::Eq;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000085using testing::ExplainMatchResult;
shiqiane35fdd92008-12-10 05:08:54 +000086using testing::Field;
87using testing::FloatEq;
88using testing::Ge;
89using testing::Gt;
90using testing::HasSubstr;
zhanyong.wan2d970ee2009-09-24 21:41:36 +000091using testing::IsNull;
zhanyong.wanb5937da2009-07-16 20:26:41 +000092using testing::Key;
shiqiane35fdd92008-12-10 05:08:54 +000093using testing::Le;
94using testing::Lt;
95using testing::MakeMatcher;
96using testing::MakePolymorphicMatcher;
zhanyong.wanb1c7f932010-03-24 17:35:11 +000097using testing::MatchResultListener;
shiqiane35fdd92008-12-10 05:08:54 +000098using testing::Matcher;
99using testing::MatcherCast;
100using testing::MatcherInterface;
101using testing::Matches;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000102using testing::MatchesRegex;
shiqiane35fdd92008-12-10 05:08:54 +0000103using testing::NanSensitiveDoubleEq;
104using testing::NanSensitiveFloatEq;
105using testing::Ne;
106using testing::Not;
107using testing::NotNull;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000108using testing::Pair;
shiqiane35fdd92008-12-10 05:08:54 +0000109using testing::Pointee;
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000110using testing::Pointwise;
shiqiane35fdd92008-12-10 05:08:54 +0000111using testing::PolymorphicMatcher;
112using testing::Property;
113using testing::Ref;
114using testing::ResultOf;
115using testing::StartsWith;
116using testing::StrCaseEq;
117using testing::StrCaseNe;
118using testing::StrEq;
119using testing::StrNe;
120using testing::Truly;
121using testing::TypedEq;
zhanyong.wanb8243162009-06-04 05:48:20 +0000122using testing::Value;
zhanyong.wan898725c2011-09-16 16:45:39 +0000123using testing::WhenSorted;
124using testing::WhenSortedBy;
shiqiane35fdd92008-12-10 05:08:54 +0000125using testing::_;
zhanyong.wana862f1d2010-03-15 21:23:04 +0000126using testing::internal::DummyMatchResultListener;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000127using testing::internal::ExplainMatchFailureTupleTo;
shiqiane35fdd92008-12-10 05:08:54 +0000128using testing::internal::FloatingEqMatcher;
zhanyong.wanb4140802010-06-08 22:53:57 +0000129using testing::internal::FormatMatcherDescription;
zhanyong.wan736baa82010-09-27 17:44:16 +0000130using testing::internal::IsReadableTypeName;
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000131using testing::internal::JoinAsTuple;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000132using testing::internal::RE;
zhanyong.wana862f1d2010-03-15 21:23:04 +0000133using testing::internal::StreamMatchResultListener;
shiqiane35fdd92008-12-10 05:08:54 +0000134using testing::internal::String;
zhanyong.wan34b034c2010-03-05 21:23:23 +0000135using testing::internal::StringMatchResultListener;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000136using testing::internal::Strings;
vladlosev79b83502009-11-18 00:43:37 +0000137using testing::internal::linked_ptr;
vladloseve56daa72009-11-18 01:08:08 +0000138using testing::internal::scoped_ptr;
shiqiane35fdd92008-12-10 05:08:54 +0000139using testing::internal::string;
140
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000141// For testing ExplainMatchResultTo().
142class GreaterThanMatcher : public MatcherInterface<int> {
143 public:
144 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
145
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000146 virtual void DescribeTo(ostream* os) const {
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000147 *os << "is > " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000148 }
149
zhanyong.wandb22c222010-01-28 21:52:29 +0000150 virtual bool MatchAndExplain(int lhs,
151 MatchResultListener* listener) const {
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000152 const int diff = lhs - rhs_;
153 if (diff > 0) {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000154 *listener << "which is " << diff << " more than " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000155 } else if (diff == 0) {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000156 *listener << "which is the same as " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000157 } else {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000158 *listener << "which is " << -diff << " less than " << rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000159 }
zhanyong.wandb22c222010-01-28 21:52:29 +0000160
161 return lhs > rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000162 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000163
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000164 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000165 int rhs_;
zhanyong.wanf5e1ce52009-09-16 07:02:02 +0000166};
167
168Matcher<int> GreaterThan(int n) {
169 return MakeMatcher(new GreaterThanMatcher(n));
170}
171
zhanyong.wan736baa82010-09-27 17:44:16 +0000172string OfType(const string& type_name) {
173#if GTEST_HAS_RTTI
174 return " (of type " + type_name + ")";
175#else
176 return "";
177#endif
178}
179
shiqiane35fdd92008-12-10 05:08:54 +0000180// Returns the description of the given matcher.
181template <typename T>
182string Describe(const Matcher<T>& m) {
183 stringstream ss;
184 m.DescribeTo(&ss);
185 return ss.str();
186}
187
188// Returns the description of the negation of the given matcher.
189template <typename T>
190string DescribeNegation(const Matcher<T>& m) {
191 stringstream ss;
192 m.DescribeNegationTo(&ss);
193 return ss.str();
194}
195
196// Returns the reason why x matches, or doesn't match, m.
197template <typename MatcherType, typename Value>
198string Explain(const MatcherType& m, const Value& x) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000199 StringMatchResultListener listener;
200 ExplainMatchResult(m, x, &listener);
201 return listener.str();
shiqiane35fdd92008-12-10 05:08:54 +0000202}
203
zhanyong.wana862f1d2010-03-15 21:23:04 +0000204TEST(MatchResultListenerTest, StreamingWorks) {
205 StringMatchResultListener listener;
206 listener << "hi" << 5;
207 EXPECT_EQ("hi5", listener.str());
208
209 // Streaming shouldn't crash when the underlying ostream is NULL.
210 DummyMatchResultListener dummy;
211 dummy << "hi" << 5;
212}
213
214TEST(MatchResultListenerTest, CanAccessUnderlyingStream) {
215 EXPECT_TRUE(DummyMatchResultListener().stream() == NULL);
216 EXPECT_TRUE(StreamMatchResultListener(NULL).stream() == NULL);
217
218 EXPECT_EQ(&std::cout, StreamMatchResultListener(&std::cout).stream());
219}
220
221TEST(MatchResultListenerTest, IsInterestedWorks) {
222 EXPECT_TRUE(StringMatchResultListener().IsInterested());
223 EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested());
224
225 EXPECT_FALSE(DummyMatchResultListener().IsInterested());
226 EXPECT_FALSE(StreamMatchResultListener(NULL).IsInterested());
227}
228
shiqiane35fdd92008-12-10 05:08:54 +0000229// Makes sure that the MatcherInterface<T> interface doesn't
230// change.
231class EvenMatcherImpl : public MatcherInterface<int> {
232 public:
zhanyong.wandb22c222010-01-28 21:52:29 +0000233 virtual bool MatchAndExplain(int x,
234 MatchResultListener* /* listener */) const {
235 return x % 2 == 0;
236 }
shiqiane35fdd92008-12-10 05:08:54 +0000237
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000238 virtual void DescribeTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +0000239 *os << "is an even number";
240 }
241
242 // We deliberately don't define DescribeNegationTo() and
243 // ExplainMatchResultTo() here, to make sure the definition of these
244 // two methods is optional.
245};
246
zhanyong.wana862f1d2010-03-15 21:23:04 +0000247// Makes sure that the MatcherInterface API doesn't change.
248TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) {
shiqiane35fdd92008-12-10 05:08:54 +0000249 EvenMatcherImpl m;
250}
251
zhanyong.wan82113312010-01-08 21:55:40 +0000252// Tests implementing a monomorphic matcher using MatchAndExplain().
253
254class NewEvenMatcherImpl : public MatcherInterface<int> {
255 public:
256 virtual bool MatchAndExplain(int x, MatchResultListener* listener) const {
257 const bool match = x % 2 == 0;
258 // Verifies that we can stream to a listener directly.
259 *listener << "value % " << 2;
260 if (listener->stream() != NULL) {
261 // Verifies that we can stream to a listener's underlying stream
262 // too.
263 *listener->stream() << " == " << (x % 2);
264 }
265 return match;
266 }
267
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000268 virtual void DescribeTo(ostream* os) const {
zhanyong.wan82113312010-01-08 21:55:40 +0000269 *os << "is an even number";
270 }
271};
272
273TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {
274 Matcher<int> m = MakeMatcher(new NewEvenMatcherImpl);
275 EXPECT_TRUE(m.Matches(2));
276 EXPECT_FALSE(m.Matches(3));
277 EXPECT_EQ("value % 2 == 0", Explain(m, 2));
278 EXPECT_EQ("value % 2 == 1", Explain(m, 3));
279}
280
shiqiane35fdd92008-12-10 05:08:54 +0000281// Tests default-constructing a matcher.
282TEST(MatcherTest, CanBeDefaultConstructed) {
283 Matcher<double> m;
284}
285
286// Tests that Matcher<T> can be constructed from a MatcherInterface<T>*.
287TEST(MatcherTest, CanBeConstructedFromMatcherInterface) {
288 const MatcherInterface<int>* impl = new EvenMatcherImpl;
289 Matcher<int> m(impl);
290 EXPECT_TRUE(m.Matches(4));
291 EXPECT_FALSE(m.Matches(5));
292}
293
294// Tests that value can be used in place of Eq(value).
295TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) {
296 Matcher<int> m1 = 5;
297 EXPECT_TRUE(m1.Matches(5));
298 EXPECT_FALSE(m1.Matches(6));
299}
300
301// Tests that NULL can be used in place of Eq(NULL).
302TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) {
303 Matcher<int*> m1 = NULL;
304 EXPECT_TRUE(m1.Matches(NULL));
305 int n = 0;
306 EXPECT_FALSE(m1.Matches(&n));
307}
308
309// Tests that matchers are copyable.
310TEST(MatcherTest, IsCopyable) {
311 // Tests the copy constructor.
312 Matcher<bool> m1 = Eq(false);
313 EXPECT_TRUE(m1.Matches(false));
314 EXPECT_FALSE(m1.Matches(true));
315
316 // Tests the assignment operator.
317 m1 = Eq(true);
318 EXPECT_TRUE(m1.Matches(true));
319 EXPECT_FALSE(m1.Matches(false));
320}
321
322// Tests that Matcher<T>::DescribeTo() calls
323// MatcherInterface<T>::DescribeTo().
324TEST(MatcherTest, CanDescribeItself) {
325 EXPECT_EQ("is an even number",
326 Describe(Matcher<int>(new EvenMatcherImpl)));
327}
328
zhanyong.wan82113312010-01-08 21:55:40 +0000329// Tests Matcher<T>::MatchAndExplain().
330TEST(MatcherTest, MatchAndExplain) {
331 Matcher<int> m = GreaterThan(0);
zhanyong.wan34b034c2010-03-05 21:23:23 +0000332 StringMatchResultListener listener1;
zhanyong.wan82113312010-01-08 21:55:40 +0000333 EXPECT_TRUE(m.MatchAndExplain(42, &listener1));
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000334 EXPECT_EQ("which is 42 more than 0", listener1.str());
zhanyong.wan82113312010-01-08 21:55:40 +0000335
zhanyong.wan34b034c2010-03-05 21:23:23 +0000336 StringMatchResultListener listener2;
zhanyong.wan82113312010-01-08 21:55:40 +0000337 EXPECT_FALSE(m.MatchAndExplain(-9, &listener2));
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000338 EXPECT_EQ("which is 9 less than 0", listener2.str());
zhanyong.wan82113312010-01-08 21:55:40 +0000339}
340
shiqiane35fdd92008-12-10 05:08:54 +0000341// Tests that a C-string literal can be implicitly converted to a
342// Matcher<string> or Matcher<const string&>.
343TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
344 Matcher<string> m1 = "hi";
345 EXPECT_TRUE(m1.Matches("hi"));
346 EXPECT_FALSE(m1.Matches("hello"));
347
348 Matcher<const string&> m2 = "hi";
349 EXPECT_TRUE(m2.Matches("hi"));
350 EXPECT_FALSE(m2.Matches("hello"));
351}
352
353// Tests that a string object can be implicitly converted to a
354// Matcher<string> or Matcher<const string&>.
355TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {
356 Matcher<string> m1 = string("hi");
357 EXPECT_TRUE(m1.Matches("hi"));
358 EXPECT_FALSE(m1.Matches("hello"));
359
360 Matcher<const string&> m2 = string("hi");
361 EXPECT_TRUE(m2.Matches("hi"));
362 EXPECT_FALSE(m2.Matches("hello"));
363}
364
365// Tests that MakeMatcher() constructs a Matcher<T> from a
366// MatcherInterface* without requiring the user to explicitly
367// write the type.
368TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) {
369 const MatcherInterface<int>* dummy_impl = NULL;
370 Matcher<int> m = MakeMatcher(dummy_impl);
371}
372
zhanyong.wan82113312010-01-08 21:55:40 +0000373// Tests that MakePolymorphicMatcher() can construct a polymorphic
374// matcher from its implementation using the old API.
zhanyong.wan33605ba2010-04-22 23:37:47 +0000375const int g_bar = 1;
shiqiane35fdd92008-12-10 05:08:54 +0000376class ReferencesBarOrIsZeroImpl {
377 public:
378 template <typename T>
zhanyong.wandb22c222010-01-28 21:52:29 +0000379 bool MatchAndExplain(const T& x,
380 MatchResultListener* /* listener */) const {
shiqiane35fdd92008-12-10 05:08:54 +0000381 const void* p = &x;
zhanyong.wan33605ba2010-04-22 23:37:47 +0000382 return p == &g_bar || x == 0;
shiqiane35fdd92008-12-10 05:08:54 +0000383 }
384
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000385 void DescribeTo(ostream* os) const { *os << "g_bar or zero"; }
shiqiane35fdd92008-12-10 05:08:54 +0000386
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000387 void DescribeNegationTo(ostream* os) const {
zhanyong.wan33605ba2010-04-22 23:37:47 +0000388 *os << "doesn't reference g_bar and is not zero";
shiqiane35fdd92008-12-10 05:08:54 +0000389 }
390};
391
392// This function verifies that MakePolymorphicMatcher() returns a
393// PolymorphicMatcher<T> where T is the argument's type.
394PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() {
395 return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl());
396}
397
zhanyong.wan82113312010-01-08 21:55:40 +0000398TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) {
shiqiane35fdd92008-12-10 05:08:54 +0000399 // Using a polymorphic matcher to match a reference type.
400 Matcher<const int&> m1 = ReferencesBarOrIsZero();
401 EXPECT_TRUE(m1.Matches(0));
402 // Verifies that the identity of a by-reference argument is preserved.
zhanyong.wan33605ba2010-04-22 23:37:47 +0000403 EXPECT_TRUE(m1.Matches(g_bar));
shiqiane35fdd92008-12-10 05:08:54 +0000404 EXPECT_FALSE(m1.Matches(1));
zhanyong.wan33605ba2010-04-22 23:37:47 +0000405 EXPECT_EQ("g_bar or zero", Describe(m1));
shiqiane35fdd92008-12-10 05:08:54 +0000406
407 // Using a polymorphic matcher to match a value type.
408 Matcher<double> m2 = ReferencesBarOrIsZero();
409 EXPECT_TRUE(m2.Matches(0.0));
410 EXPECT_FALSE(m2.Matches(0.1));
zhanyong.wan33605ba2010-04-22 23:37:47 +0000411 EXPECT_EQ("g_bar or zero", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +0000412}
413
zhanyong.wan82113312010-01-08 21:55:40 +0000414// Tests implementing a polymorphic matcher using MatchAndExplain().
415
416class PolymorphicIsEvenImpl {
417 public:
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000418 void DescribeTo(ostream* os) const { *os << "is even"; }
zhanyong.wan82113312010-01-08 21:55:40 +0000419
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000420 void DescribeNegationTo(ostream* os) const {
zhanyong.wan82113312010-01-08 21:55:40 +0000421 *os << "is odd";
422 }
zhanyong.wan82113312010-01-08 21:55:40 +0000423
zhanyong.wandb22c222010-01-28 21:52:29 +0000424 template <typename T>
425 bool MatchAndExplain(const T& x, MatchResultListener* listener) const {
426 // Verifies that we can stream to the listener directly.
427 *listener << "% " << 2;
428 if (listener->stream() != NULL) {
429 // Verifies that we can stream to the listener's underlying stream
430 // too.
431 *listener->stream() << " == " << (x % 2);
432 }
433 return (x % 2) == 0;
zhanyong.wan82113312010-01-08 21:55:40 +0000434 }
zhanyong.wandb22c222010-01-28 21:52:29 +0000435};
zhanyong.wan82113312010-01-08 21:55:40 +0000436
437PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() {
438 return MakePolymorphicMatcher(PolymorphicIsEvenImpl());
439}
440
441TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {
442 // Using PolymorphicIsEven() as a Matcher<int>.
443 const Matcher<int> m1 = PolymorphicIsEven();
444 EXPECT_TRUE(m1.Matches(42));
445 EXPECT_FALSE(m1.Matches(43));
446 EXPECT_EQ("is even", Describe(m1));
447
448 const Matcher<int> not_m1 = Not(m1);
449 EXPECT_EQ("is odd", Describe(not_m1));
450
451 EXPECT_EQ("% 2 == 0", Explain(m1, 42));
452
453 // Using PolymorphicIsEven() as a Matcher<char>.
454 const Matcher<char> m2 = PolymorphicIsEven();
455 EXPECT_TRUE(m2.Matches('\x42'));
456 EXPECT_FALSE(m2.Matches('\x43'));
457 EXPECT_EQ("is even", Describe(m2));
458
459 const Matcher<char> not_m2 = Not(m2);
460 EXPECT_EQ("is odd", Describe(not_m2));
461
462 EXPECT_EQ("% 2 == 0", Explain(m2, '\x42'));
463}
464
shiqiane35fdd92008-12-10 05:08:54 +0000465// Tests that MatcherCast<T>(m) works when m is a polymorphic matcher.
466TEST(MatcherCastTest, FromPolymorphicMatcher) {
467 Matcher<int> m = MatcherCast<int>(Eq(5));
468 EXPECT_TRUE(m.Matches(5));
469 EXPECT_FALSE(m.Matches(6));
470}
471
472// For testing casting matchers between compatible types.
473class IntValue {
474 public:
475 // An int can be statically (although not implicitly) cast to a
476 // IntValue.
zhanyong.wan32de5f52009-12-23 00:13:23 +0000477 explicit IntValue(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +0000478
479 int value() const { return value_; }
480 private:
481 int value_;
482};
483
484// For testing casting matchers between compatible types.
485bool IsPositiveIntValue(const IntValue& foo) {
486 return foo.value() > 0;
487}
488
489// Tests that MatcherCast<T>(m) works when m is a Matcher<U> where T
490// can be statically converted to U.
491TEST(MatcherCastTest, FromCompatibleType) {
492 Matcher<double> m1 = Eq(2.0);
493 Matcher<int> m2 = MatcherCast<int>(m1);
494 EXPECT_TRUE(m2.Matches(2));
495 EXPECT_FALSE(m2.Matches(3));
496
497 Matcher<IntValue> m3 = Truly(IsPositiveIntValue);
498 Matcher<int> m4 = MatcherCast<int>(m3);
499 // In the following, the arguments 1 and 0 are statically converted
500 // to IntValue objects, and then tested by the IsPositiveIntValue()
501 // predicate.
502 EXPECT_TRUE(m4.Matches(1));
503 EXPECT_FALSE(m4.Matches(0));
504}
505
506// Tests that MatcherCast<T>(m) works when m is a Matcher<const T&>.
507TEST(MatcherCastTest, FromConstReferenceToNonReference) {
508 Matcher<const int&> m1 = Eq(0);
509 Matcher<int> m2 = MatcherCast<int>(m1);
510 EXPECT_TRUE(m2.Matches(0));
511 EXPECT_FALSE(m2.Matches(1));
512}
513
514// Tests that MatcherCast<T>(m) works when m is a Matcher<T&>.
515TEST(MatcherCastTest, FromReferenceToNonReference) {
516 Matcher<int&> m1 = Eq(0);
517 Matcher<int> m2 = MatcherCast<int>(m1);
518 EXPECT_TRUE(m2.Matches(0));
519 EXPECT_FALSE(m2.Matches(1));
520}
521
522// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
523TEST(MatcherCastTest, FromNonReferenceToConstReference) {
524 Matcher<int> m1 = Eq(0);
525 Matcher<const int&> m2 = MatcherCast<const int&>(m1);
526 EXPECT_TRUE(m2.Matches(0));
527 EXPECT_FALSE(m2.Matches(1));
528}
529
530// Tests that MatcherCast<T&>(m) works when m is a Matcher<T>.
531TEST(MatcherCastTest, FromNonReferenceToReference) {
532 Matcher<int> m1 = Eq(0);
533 Matcher<int&> m2 = MatcherCast<int&>(m1);
534 int n = 0;
535 EXPECT_TRUE(m2.Matches(n));
536 n = 1;
537 EXPECT_FALSE(m2.Matches(n));
538}
539
540// Tests that MatcherCast<T>(m) works when m is a Matcher<T>.
541TEST(MatcherCastTest, FromSameType) {
542 Matcher<int> m1 = Eq(0);
543 Matcher<int> m2 = MatcherCast<int>(m1);
544 EXPECT_TRUE(m2.Matches(0));
545 EXPECT_FALSE(m2.Matches(1));
546}
547
jgm79a367e2012-04-10 16:02:11 +0000548// Implicitly convertible form any type.
549struct ConvertibleFromAny {
550 ConvertibleFromAny(int a_value) : value(a_value) {}
551 template <typename T>
552 ConvertibleFromAny(const T& a_value) : value(-1) {
553 ADD_FAILURE() << "Conversion constructor called";
554 }
555 int value;
556};
557
558bool operator==(const ConvertibleFromAny& a, const ConvertibleFromAny& b) {
559 return a.value == b.value;
560}
561
562ostream& operator<<(ostream& os, const ConvertibleFromAny& a) {
563 return os << a.value;
564}
565
566TEST(MatcherCastTest, ConversionConstructorIsUsed) {
567 Matcher<ConvertibleFromAny> m = MatcherCast<ConvertibleFromAny>(1);
568 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
569 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
570}
571
572TEST(MatcherCastTest, FromConvertibleFromAny) {
573 Matcher<ConvertibleFromAny> m =
574 MatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
575 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
576 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
577}
578
zhanyong.wan18490652009-05-11 18:54:08 +0000579class Base {};
580class Derived : public Base {};
581
582// Tests that SafeMatcherCast<T>(m) works when m is a polymorphic matcher.
583TEST(SafeMatcherCastTest, FromPolymorphicMatcher) {
584 Matcher<char> m2 = SafeMatcherCast<char>(Eq(32));
585 EXPECT_TRUE(m2.Matches(' '));
586 EXPECT_FALSE(m2.Matches('\n'));
587}
588
zhanyong.wan16cf4732009-05-14 20:55:30 +0000589// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where
590// T and U are arithmetic types and T can be losslessly converted to
591// U.
592TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) {
zhanyong.wan18490652009-05-11 18:54:08 +0000593 Matcher<double> m1 = DoubleEq(1.0);
zhanyong.wan16cf4732009-05-14 20:55:30 +0000594 Matcher<float> m2 = SafeMatcherCast<float>(m1);
595 EXPECT_TRUE(m2.Matches(1.0f));
596 EXPECT_FALSE(m2.Matches(2.0f));
597
598 Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>('a'));
599 EXPECT_TRUE(m3.Matches('a'));
600 EXPECT_FALSE(m3.Matches('b'));
zhanyong.wan18490652009-05-11 18:54:08 +0000601}
602
603// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where T and U
604// are pointers or references to a derived and a base class, correspondingly.
605TEST(SafeMatcherCastTest, FromBaseClass) {
606 Derived d, d2;
607 Matcher<Base*> m1 = Eq(&d);
608 Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1);
609 EXPECT_TRUE(m2.Matches(&d));
610 EXPECT_FALSE(m2.Matches(&d2));
611
612 Matcher<Base&> m3 = Ref(d);
613 Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3);
614 EXPECT_TRUE(m4.Matches(d));
615 EXPECT_FALSE(m4.Matches(d2));
616}
617
618// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<const T&>.
619TEST(SafeMatcherCastTest, FromConstReferenceToReference) {
620 int n = 0;
621 Matcher<const int&> m1 = Ref(n);
622 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
623 int n1 = 0;
624 EXPECT_TRUE(m2.Matches(n));
625 EXPECT_FALSE(m2.Matches(n1));
626}
627
628// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
629TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) {
630 Matcher<int> m1 = Eq(0);
631 Matcher<const int&> m2 = SafeMatcherCast<const int&>(m1);
632 EXPECT_TRUE(m2.Matches(0));
633 EXPECT_FALSE(m2.Matches(1));
634}
635
636// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<T>.
637TEST(SafeMatcherCastTest, FromNonReferenceToReference) {
638 Matcher<int> m1 = Eq(0);
639 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
640 int n = 0;
641 EXPECT_TRUE(m2.Matches(n));
642 n = 1;
643 EXPECT_FALSE(m2.Matches(n));
644}
645
646// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<T>.
647TEST(SafeMatcherCastTest, FromSameType) {
648 Matcher<int> m1 = Eq(0);
649 Matcher<int> m2 = SafeMatcherCast<int>(m1);
650 EXPECT_TRUE(m2.Matches(0));
651 EXPECT_FALSE(m2.Matches(1));
652}
653
jgm79a367e2012-04-10 16:02:11 +0000654TEST(SafeMatcherCastTest, ConversionConstructorIsUsed) {
655 Matcher<ConvertibleFromAny> m = SafeMatcherCast<ConvertibleFromAny>(1);
656 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
657 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
658}
659
660TEST(SafeMatcherCastTest, FromConvertibleFromAny) {
661 Matcher<ConvertibleFromAny> m =
662 SafeMatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
663 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
664 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
665}
666
shiqiane35fdd92008-12-10 05:08:54 +0000667// Tests that A<T>() matches any value of type T.
668TEST(ATest, MatchesAnyValue) {
669 // Tests a matcher for a value type.
670 Matcher<double> m1 = A<double>();
671 EXPECT_TRUE(m1.Matches(91.43));
672 EXPECT_TRUE(m1.Matches(-15.32));
673
674 // Tests a matcher for a reference type.
675 int a = 2;
676 int b = -6;
677 Matcher<int&> m2 = A<int&>();
678 EXPECT_TRUE(m2.Matches(a));
679 EXPECT_TRUE(m2.Matches(b));
680}
681
682// Tests that A<T>() describes itself properly.
683TEST(ATest, CanDescribeSelf) {
684 EXPECT_EQ("is anything", Describe(A<bool>()));
685}
686
687// Tests that An<T>() matches any value of type T.
688TEST(AnTest, MatchesAnyValue) {
689 // Tests a matcher for a value type.
690 Matcher<int> m1 = An<int>();
691 EXPECT_TRUE(m1.Matches(9143));
692 EXPECT_TRUE(m1.Matches(-1532));
693
694 // Tests a matcher for a reference type.
695 int a = 2;
696 int b = -6;
697 Matcher<int&> m2 = An<int&>();
698 EXPECT_TRUE(m2.Matches(a));
699 EXPECT_TRUE(m2.Matches(b));
700}
701
702// Tests that An<T>() describes itself properly.
703TEST(AnTest, CanDescribeSelf) {
704 EXPECT_EQ("is anything", Describe(An<int>()));
705}
706
707// Tests that _ can be used as a matcher for any type and matches any
708// value of that type.
709TEST(UnderscoreTest, MatchesAnyValue) {
710 // Uses _ as a matcher for a value type.
711 Matcher<int> m1 = _;
712 EXPECT_TRUE(m1.Matches(123));
713 EXPECT_TRUE(m1.Matches(-242));
714
715 // Uses _ as a matcher for a reference type.
716 bool a = false;
717 const bool b = true;
718 Matcher<const bool&> m2 = _;
719 EXPECT_TRUE(m2.Matches(a));
720 EXPECT_TRUE(m2.Matches(b));
721}
722
723// Tests that _ describes itself properly.
724TEST(UnderscoreTest, CanDescribeSelf) {
725 Matcher<int> m = _;
726 EXPECT_EQ("is anything", Describe(m));
727}
728
729// Tests that Eq(x) matches any value equal to x.
730TEST(EqTest, MatchesEqualValue) {
731 // 2 C-strings with same content but different addresses.
732 const char a1[] = "hi";
733 const char a2[] = "hi";
734
735 Matcher<const char*> m1 = Eq(a1);
736 EXPECT_TRUE(m1.Matches(a1));
737 EXPECT_FALSE(m1.Matches(a2));
738}
739
740// Tests that Eq(v) describes itself properly.
741
742class Unprintable {
743 public:
744 Unprintable() : c_('a') {}
745
zhanyong.wan32de5f52009-12-23 00:13:23 +0000746 bool operator==(const Unprintable& /* rhs */) { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000747 private:
748 char c_;
749};
750
751TEST(EqTest, CanDescribeSelf) {
752 Matcher<Unprintable> m = Eq(Unprintable());
753 EXPECT_EQ("is equal to 1-byte object <61>", Describe(m));
754}
755
756// Tests that Eq(v) can be used to match any type that supports
757// comparing with type T, where T is v's type.
758TEST(EqTest, IsPolymorphic) {
759 Matcher<int> m1 = Eq(1);
760 EXPECT_TRUE(m1.Matches(1));
761 EXPECT_FALSE(m1.Matches(2));
762
763 Matcher<char> m2 = Eq(1);
764 EXPECT_TRUE(m2.Matches('\1'));
765 EXPECT_FALSE(m2.Matches('a'));
766}
767
768// Tests that TypedEq<T>(v) matches values of type T that's equal to v.
769TEST(TypedEqTest, ChecksEqualityForGivenType) {
770 Matcher<char> m1 = TypedEq<char>('a');
771 EXPECT_TRUE(m1.Matches('a'));
772 EXPECT_FALSE(m1.Matches('b'));
773
774 Matcher<int> m2 = TypedEq<int>(6);
775 EXPECT_TRUE(m2.Matches(6));
776 EXPECT_FALSE(m2.Matches(7));
777}
778
779// Tests that TypedEq(v) describes itself properly.
780TEST(TypedEqTest, CanDescribeSelf) {
781 EXPECT_EQ("is equal to 2", Describe(TypedEq<int>(2)));
782}
783
784// Tests that TypedEq<T>(v) has type Matcher<T>.
785
786// Type<T>::IsTypeOf(v) compiles iff the type of value v is T, where T
787// is a "bare" type (i.e. not in the form of const U or U&). If v's
788// type is not T, the compiler will generate a message about
789// "undefined referece".
790template <typename T>
791struct Type {
zhanyong.wan32de5f52009-12-23 00:13:23 +0000792 static bool IsTypeOf(const T& /* v */) { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000793
794 template <typename T2>
795 static void IsTypeOf(T2 v);
796};
797
798TEST(TypedEqTest, HasSpecifiedType) {
799 // Verfies that the type of TypedEq<T>(v) is Matcher<T>.
800 Type<Matcher<int> >::IsTypeOf(TypedEq<int>(5));
801 Type<Matcher<double> >::IsTypeOf(TypedEq<double>(5));
802}
803
804// Tests that Ge(v) matches anything >= v.
805TEST(GeTest, ImplementsGreaterThanOrEqual) {
806 Matcher<int> m1 = Ge(0);
807 EXPECT_TRUE(m1.Matches(1));
808 EXPECT_TRUE(m1.Matches(0));
809 EXPECT_FALSE(m1.Matches(-1));
810}
811
812// Tests that Ge(v) describes itself properly.
813TEST(GeTest, CanDescribeSelf) {
814 Matcher<int> m = Ge(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000815 EXPECT_EQ("is >= 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000816}
817
818// Tests that Gt(v) matches anything > v.
819TEST(GtTest, ImplementsGreaterThan) {
820 Matcher<double> m1 = Gt(0);
821 EXPECT_TRUE(m1.Matches(1.0));
822 EXPECT_FALSE(m1.Matches(0.0));
823 EXPECT_FALSE(m1.Matches(-1.0));
824}
825
826// Tests that Gt(v) describes itself properly.
827TEST(GtTest, CanDescribeSelf) {
828 Matcher<int> m = Gt(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000829 EXPECT_EQ("is > 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000830}
831
832// Tests that Le(v) matches anything <= v.
833TEST(LeTest, ImplementsLessThanOrEqual) {
834 Matcher<char> m1 = Le('b');
835 EXPECT_TRUE(m1.Matches('a'));
836 EXPECT_TRUE(m1.Matches('b'));
837 EXPECT_FALSE(m1.Matches('c'));
838}
839
840// Tests that Le(v) describes itself properly.
841TEST(LeTest, CanDescribeSelf) {
842 Matcher<int> m = Le(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000843 EXPECT_EQ("is <= 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000844}
845
846// Tests that Lt(v) matches anything < v.
847TEST(LtTest, ImplementsLessThan) {
848 Matcher<const string&> m1 = Lt("Hello");
849 EXPECT_TRUE(m1.Matches("Abc"));
850 EXPECT_FALSE(m1.Matches("Hello"));
851 EXPECT_FALSE(m1.Matches("Hello, world!"));
852}
853
854// Tests that Lt(v) describes itself properly.
855TEST(LtTest, CanDescribeSelf) {
856 Matcher<int> m = Lt(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000857 EXPECT_EQ("is < 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000858}
859
860// Tests that Ne(v) matches anything != v.
861TEST(NeTest, ImplementsNotEqual) {
862 Matcher<int> m1 = Ne(0);
863 EXPECT_TRUE(m1.Matches(1));
864 EXPECT_TRUE(m1.Matches(-1));
865 EXPECT_FALSE(m1.Matches(0));
866}
867
868// Tests that Ne(v) describes itself properly.
869TEST(NeTest, CanDescribeSelf) {
870 Matcher<int> m = Ne(5);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000871 EXPECT_EQ("isn't equal to 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000872}
873
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000874// Tests that IsNull() matches any NULL pointer of any type.
875TEST(IsNullTest, MatchesNullPointer) {
876 Matcher<int*> m1 = IsNull();
877 int* p1 = NULL;
878 int n = 0;
879 EXPECT_TRUE(m1.Matches(p1));
880 EXPECT_FALSE(m1.Matches(&n));
881
882 Matcher<const char*> m2 = IsNull();
883 const char* p2 = NULL;
884 EXPECT_TRUE(m2.Matches(p2));
885 EXPECT_FALSE(m2.Matches("hi"));
886
zhanyong.wan95b12332009-09-25 18:55:50 +0000887#if !GTEST_OS_SYMBIAN
888 // Nokia's Symbian compiler generates:
889 // gmock-matchers.h: ambiguous access to overloaded function
890 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(void *)'
891 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(const testing::
892 // MatcherInterface<void *> *)'
893 // gmock-matchers.h: (point of instantiation: 'testing::
894 // gmock_matchers_test::IsNullTest_MatchesNullPointer_Test::TestBody()')
895 // gmock-matchers.h: (instantiating: 'testing::PolymorphicMatc
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000896 Matcher<void*> m3 = IsNull();
897 void* p3 = NULL;
898 EXPECT_TRUE(m3.Matches(p3));
899 EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef)));
zhanyong.wan95b12332009-09-25 18:55:50 +0000900#endif
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000901}
902
vladlosev79b83502009-11-18 00:43:37 +0000903TEST(IsNullTest, LinkedPtr) {
904 const Matcher<linked_ptr<int> > m = IsNull();
905 const linked_ptr<int> null_p;
906 const linked_ptr<int> non_null_p(new int);
907
908 EXPECT_TRUE(m.Matches(null_p));
909 EXPECT_FALSE(m.Matches(non_null_p));
910}
911
912TEST(IsNullTest, ReferenceToConstLinkedPtr) {
913 const Matcher<const linked_ptr<double>&> m = IsNull();
914 const linked_ptr<double> null_p;
915 const linked_ptr<double> non_null_p(new double);
916
917 EXPECT_TRUE(m.Matches(null_p));
918 EXPECT_FALSE(m.Matches(non_null_p));
919}
920
vladloseve56daa72009-11-18 01:08:08 +0000921TEST(IsNullTest, ReferenceToConstScopedPtr) {
922 const Matcher<const scoped_ptr<double>&> m = IsNull();
923 const scoped_ptr<double> null_p;
924 const scoped_ptr<double> non_null_p(new double);
925
926 EXPECT_TRUE(m.Matches(null_p));
927 EXPECT_FALSE(m.Matches(non_null_p));
928}
929
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000930// Tests that IsNull() describes itself properly.
931TEST(IsNullTest, CanDescribeSelf) {
932 Matcher<int*> m = IsNull();
933 EXPECT_EQ("is NULL", Describe(m));
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000934 EXPECT_EQ("isn't NULL", DescribeNegation(m));
zhanyong.wan2d970ee2009-09-24 21:41:36 +0000935}
936
shiqiane35fdd92008-12-10 05:08:54 +0000937// Tests that NotNull() matches any non-NULL pointer of any type.
938TEST(NotNullTest, MatchesNonNullPointer) {
939 Matcher<int*> m1 = NotNull();
940 int* p1 = NULL;
941 int n = 0;
942 EXPECT_FALSE(m1.Matches(p1));
943 EXPECT_TRUE(m1.Matches(&n));
944
945 Matcher<const char*> m2 = NotNull();
946 const char* p2 = NULL;
947 EXPECT_FALSE(m2.Matches(p2));
948 EXPECT_TRUE(m2.Matches("hi"));
949}
950
vladlosev79b83502009-11-18 00:43:37 +0000951TEST(NotNullTest, LinkedPtr) {
952 const Matcher<linked_ptr<int> > m = NotNull();
953 const linked_ptr<int> null_p;
954 const linked_ptr<int> non_null_p(new int);
955
956 EXPECT_FALSE(m.Matches(null_p));
957 EXPECT_TRUE(m.Matches(non_null_p));
958}
959
960TEST(NotNullTest, ReferenceToConstLinkedPtr) {
961 const Matcher<const linked_ptr<double>&> m = NotNull();
962 const linked_ptr<double> null_p;
963 const linked_ptr<double> non_null_p(new double);
964
965 EXPECT_FALSE(m.Matches(null_p));
966 EXPECT_TRUE(m.Matches(non_null_p));
967}
968
vladloseve56daa72009-11-18 01:08:08 +0000969TEST(NotNullTest, ReferenceToConstScopedPtr) {
970 const Matcher<const scoped_ptr<double>&> m = NotNull();
971 const scoped_ptr<double> null_p;
972 const scoped_ptr<double> non_null_p(new double);
973
974 EXPECT_FALSE(m.Matches(null_p));
975 EXPECT_TRUE(m.Matches(non_null_p));
976}
977
shiqiane35fdd92008-12-10 05:08:54 +0000978// Tests that NotNull() describes itself properly.
979TEST(NotNullTest, CanDescribeSelf) {
980 Matcher<int*> m = NotNull();
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000981 EXPECT_EQ("isn't NULL", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000982}
983
984// Tests that Ref(variable) matches an argument that references
985// 'variable'.
986TEST(RefTest, MatchesSameVariable) {
987 int a = 0;
988 int b = 0;
989 Matcher<int&> m = Ref(a);
990 EXPECT_TRUE(m.Matches(a));
991 EXPECT_FALSE(m.Matches(b));
992}
993
994// Tests that Ref(variable) describes itself properly.
995TEST(RefTest, CanDescribeSelf) {
996 int n = 5;
997 Matcher<int&> m = Ref(n);
998 stringstream ss;
999 ss << "references the variable @" << &n << " 5";
1000 EXPECT_EQ(string(ss.str()), Describe(m));
1001}
1002
1003// Test that Ref(non_const_varialbe) can be used as a matcher for a
1004// const reference.
1005TEST(RefTest, CanBeUsedAsMatcherForConstReference) {
1006 int a = 0;
1007 int b = 0;
1008 Matcher<const int&> m = Ref(a);
1009 EXPECT_TRUE(m.Matches(a));
1010 EXPECT_FALSE(m.Matches(b));
1011}
1012
1013// Tests that Ref(variable) is covariant, i.e. Ref(derived) can be
1014// used wherever Ref(base) can be used (Ref(derived) is a sub-type
1015// of Ref(base), but not vice versa.
1016
shiqiane35fdd92008-12-10 05:08:54 +00001017TEST(RefTest, IsCovariant) {
1018 Base base, base2;
1019 Derived derived;
1020 Matcher<const Base&> m1 = Ref(base);
1021 EXPECT_TRUE(m1.Matches(base));
1022 EXPECT_FALSE(m1.Matches(base2));
1023 EXPECT_FALSE(m1.Matches(derived));
1024
1025 m1 = Ref(derived);
1026 EXPECT_TRUE(m1.Matches(derived));
1027 EXPECT_FALSE(m1.Matches(base));
1028 EXPECT_FALSE(m1.Matches(base2));
1029}
1030
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001031TEST(RefTest, ExplainsResult) {
1032 int n = 0;
1033 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), n),
1034 StartsWith("which is located @"));
1035
1036 int m = 0;
1037 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), m),
1038 StartsWith("which is located @"));
1039}
1040
shiqiane35fdd92008-12-10 05:08:54 +00001041// Tests string comparison matchers.
1042
1043TEST(StrEqTest, MatchesEqualString) {
1044 Matcher<const char*> m = StrEq(string("Hello"));
1045 EXPECT_TRUE(m.Matches("Hello"));
1046 EXPECT_FALSE(m.Matches("hello"));
1047 EXPECT_FALSE(m.Matches(NULL));
1048
1049 Matcher<const string&> m2 = StrEq("Hello");
1050 EXPECT_TRUE(m2.Matches("Hello"));
1051 EXPECT_FALSE(m2.Matches("Hi"));
1052}
1053
1054TEST(StrEqTest, CanDescribeSelf) {
vladlosevaa432202011-04-01 21:58:42 +00001055 Matcher<string> m = StrEq("Hi-\'\"?\\\a\b\f\n\r\t\v\xD3");
1056 EXPECT_EQ("is equal to \"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",
shiqiane35fdd92008-12-10 05:08:54 +00001057 Describe(m));
1058
1059 string str("01204500800");
1060 str[3] = '\0';
1061 Matcher<string> m2 = StrEq(str);
1062 EXPECT_EQ("is equal to \"012\\04500800\"", Describe(m2));
1063 str[0] = str[6] = str[7] = str[9] = str[10] = '\0';
1064 Matcher<string> m3 = StrEq(str);
1065 EXPECT_EQ("is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3));
1066}
1067
1068TEST(StrNeTest, MatchesUnequalString) {
1069 Matcher<const char*> m = StrNe("Hello");
1070 EXPECT_TRUE(m.Matches(""));
1071 EXPECT_TRUE(m.Matches(NULL));
1072 EXPECT_FALSE(m.Matches("Hello"));
1073
1074 Matcher<string> m2 = StrNe(string("Hello"));
1075 EXPECT_TRUE(m2.Matches("hello"));
1076 EXPECT_FALSE(m2.Matches("Hello"));
1077}
1078
1079TEST(StrNeTest, CanDescribeSelf) {
1080 Matcher<const char*> m = StrNe("Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001081 EXPECT_EQ("isn't equal to \"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001082}
1083
1084TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
1085 Matcher<const char*> m = StrCaseEq(string("Hello"));
1086 EXPECT_TRUE(m.Matches("Hello"));
1087 EXPECT_TRUE(m.Matches("hello"));
1088 EXPECT_FALSE(m.Matches("Hi"));
1089 EXPECT_FALSE(m.Matches(NULL));
1090
1091 Matcher<const string&> m2 = StrCaseEq("Hello");
1092 EXPECT_TRUE(m2.Matches("hello"));
1093 EXPECT_FALSE(m2.Matches("Hi"));
1094}
1095
1096TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1097 string str1("oabocdooeoo");
1098 string str2("OABOCDOOEOO");
1099 Matcher<const string&> m0 = StrCaseEq(str1);
1100 EXPECT_FALSE(m0.Matches(str2 + string(1, '\0')));
1101
1102 str1[3] = str2[3] = '\0';
1103 Matcher<const string&> m1 = StrCaseEq(str1);
1104 EXPECT_TRUE(m1.Matches(str2));
1105
1106 str1[0] = str1[6] = str1[7] = str1[10] = '\0';
1107 str2[0] = str2[6] = str2[7] = str2[10] = '\0';
1108 Matcher<const string&> m2 = StrCaseEq(str1);
1109 str1[9] = str2[9] = '\0';
1110 EXPECT_FALSE(m2.Matches(str2));
1111
1112 Matcher<const string&> m3 = StrCaseEq(str1);
1113 EXPECT_TRUE(m3.Matches(str2));
1114
1115 EXPECT_FALSE(m3.Matches(str2 + "x"));
1116 str2.append(1, '\0');
1117 EXPECT_FALSE(m3.Matches(str2));
1118 EXPECT_FALSE(m3.Matches(string(str2, 0, 9)));
1119}
1120
1121TEST(StrCaseEqTest, CanDescribeSelf) {
1122 Matcher<string> m = StrCaseEq("Hi");
1123 EXPECT_EQ("is equal to (ignoring case) \"Hi\"", Describe(m));
1124}
1125
1126TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1127 Matcher<const char*> m = StrCaseNe("Hello");
1128 EXPECT_TRUE(m.Matches("Hi"));
1129 EXPECT_TRUE(m.Matches(NULL));
1130 EXPECT_FALSE(m.Matches("Hello"));
1131 EXPECT_FALSE(m.Matches("hello"));
1132
1133 Matcher<string> m2 = StrCaseNe(string("Hello"));
1134 EXPECT_TRUE(m2.Matches(""));
1135 EXPECT_FALSE(m2.Matches("Hello"));
1136}
1137
1138TEST(StrCaseNeTest, CanDescribeSelf) {
1139 Matcher<const char*> m = StrCaseNe("Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001140 EXPECT_EQ("isn't equal to (ignoring case) \"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001141}
1142
1143// Tests that HasSubstr() works for matching string-typed values.
1144TEST(HasSubstrTest, WorksForStringClasses) {
1145 const Matcher<string> m1 = HasSubstr("foo");
1146 EXPECT_TRUE(m1.Matches(string("I love food.")));
1147 EXPECT_FALSE(m1.Matches(string("tofo")));
1148
1149 const Matcher<const std::string&> m2 = HasSubstr("foo");
1150 EXPECT_TRUE(m2.Matches(std::string("I love food.")));
1151 EXPECT_FALSE(m2.Matches(std::string("tofo")));
1152}
1153
1154// Tests that HasSubstr() works for matching C-string-typed values.
1155TEST(HasSubstrTest, WorksForCStrings) {
1156 const Matcher<char*> m1 = HasSubstr("foo");
1157 EXPECT_TRUE(m1.Matches(const_cast<char*>("I love food.")));
1158 EXPECT_FALSE(m1.Matches(const_cast<char*>("tofo")));
1159 EXPECT_FALSE(m1.Matches(NULL));
1160
1161 const Matcher<const char*> m2 = HasSubstr("foo");
1162 EXPECT_TRUE(m2.Matches("I love food."));
1163 EXPECT_FALSE(m2.Matches("tofo"));
1164 EXPECT_FALSE(m2.Matches(NULL));
1165}
1166
1167// Tests that HasSubstr(s) describes itself properly.
1168TEST(HasSubstrTest, CanDescribeSelf) {
1169 Matcher<string> m = HasSubstr("foo\n\"");
1170 EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m));
1171}
1172
zhanyong.wanb5937da2009-07-16 20:26:41 +00001173TEST(KeyTest, CanDescribeSelf) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001174 Matcher<const pair<std::string, int>&> m = Key("foo");
zhanyong.wanb5937da2009-07-16 20:26:41 +00001175 EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001176 EXPECT_EQ("doesn't have a key that is equal to \"foo\"", DescribeNegation(m));
1177}
1178
1179TEST(KeyTest, ExplainsResult) {
1180 Matcher<pair<int, bool> > m = Key(GreaterThan(10));
1181 EXPECT_EQ("whose first field is a value which is 5 less than 10",
1182 Explain(m, make_pair(5, true)));
1183 EXPECT_EQ("whose first field is a value which is 5 more than 10",
1184 Explain(m, make_pair(15, true)));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001185}
1186
1187TEST(KeyTest, MatchesCorrectly) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001188 pair<int, std::string> p(25, "foo");
zhanyong.wanb5937da2009-07-16 20:26:41 +00001189 EXPECT_THAT(p, Key(25));
1190 EXPECT_THAT(p, Not(Key(42)));
1191 EXPECT_THAT(p, Key(Ge(20)));
1192 EXPECT_THAT(p, Not(Key(Lt(25))));
1193}
1194
1195TEST(KeyTest, SafelyCastsInnerMatcher) {
1196 Matcher<int> is_positive = Gt(0);
1197 Matcher<int> is_negative = Lt(0);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001198 pair<char, bool> p('a', true);
zhanyong.wanb5937da2009-07-16 20:26:41 +00001199 EXPECT_THAT(p, Key(is_positive));
1200 EXPECT_THAT(p, Not(Key(is_negative)));
1201}
1202
1203TEST(KeyTest, InsideContainsUsingMap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001204 map<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001205 container.insert(make_pair(1, 'a'));
1206 container.insert(make_pair(2, 'b'));
1207 container.insert(make_pair(4, 'c'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001208 EXPECT_THAT(container, Contains(Key(1)));
1209 EXPECT_THAT(container, Not(Contains(Key(3))));
1210}
1211
1212TEST(KeyTest, InsideContainsUsingMultimap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001213 multimap<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001214 container.insert(make_pair(1, 'a'));
1215 container.insert(make_pair(2, 'b'));
1216 container.insert(make_pair(4, 'c'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001217
1218 EXPECT_THAT(container, Not(Contains(Key(25))));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001219 container.insert(make_pair(25, 'd'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001220 EXPECT_THAT(container, Contains(Key(25)));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001221 container.insert(make_pair(25, 'e'));
zhanyong.wanb5937da2009-07-16 20:26:41 +00001222 EXPECT_THAT(container, Contains(Key(25)));
1223
1224 EXPECT_THAT(container, Contains(Key(1)));
1225 EXPECT_THAT(container, Not(Contains(Key(3))));
1226}
1227
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001228TEST(PairTest, Typing) {
1229 // Test verifies the following type conversions can be compiled.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001230 Matcher<const pair<const char*, int>&> m1 = Pair("foo", 42);
1231 Matcher<const pair<const char*, int> > m2 = Pair("foo", 42);
1232 Matcher<pair<const char*, int> > m3 = Pair("foo", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001233
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001234 Matcher<pair<int, const std::string> > m4 = Pair(25, "42");
1235 Matcher<pair<const std::string, int> > m5 = Pair("25", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001236}
1237
1238TEST(PairTest, CanDescribeSelf) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001239 Matcher<const pair<std::string, int>&> m1 = Pair("foo", 42);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001240 EXPECT_EQ("has a first field that is equal to \"foo\""
1241 ", and has a second field that is equal to 42",
1242 Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001243 EXPECT_EQ("has a first field that isn't equal to \"foo\""
1244 ", or has a second field that isn't equal to 42",
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001245 DescribeNegation(m1));
1246 // Double and triple negation (1 or 2 times not and description of negation).
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001247 Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42));
1248 EXPECT_EQ("has a first field that isn't equal to 13"
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001249 ", and has a second field that is equal to 42",
1250 DescribeNegation(m2));
1251}
1252
1253TEST(PairTest, CanExplainMatchResultTo) {
zhanyong.wan82113312010-01-08 21:55:40 +00001254 // If neither field matches, Pair() should explain about the first
1255 // field.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001256 const Matcher<pair<int, int> > m = Pair(GreaterThan(0), GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001257 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001258 Explain(m, make_pair(-1, -2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001259
zhanyong.wan82113312010-01-08 21:55:40 +00001260 // If the first field matches but the second doesn't, Pair() should
1261 // explain about the second field.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001262 EXPECT_EQ("whose second field does not match, which is 2 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001263 Explain(m, make_pair(1, -2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001264
zhanyong.wan82113312010-01-08 21:55:40 +00001265 // If the first field doesn't match but the second does, Pair()
1266 // should explain about the first field.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001267 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001268 Explain(m, make_pair(-1, 2)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001269
zhanyong.wan82113312010-01-08 21:55:40 +00001270 // If both fields match, Pair() should explain about them both.
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001271 EXPECT_EQ("whose both fields match, where the first field is a value "
1272 "which is 1 more than 0, and the second field is a value "
1273 "which is 2 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001274 Explain(m, make_pair(1, 2)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001275
1276 // If only the first match has an explanation, only this explanation should
1277 // be printed.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001278 const Matcher<pair<int, int> > explain_first = Pair(GreaterThan(0), 0);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001279 EXPECT_EQ("whose both fields match, where the first field is a value "
1280 "which is 1 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001281 Explain(explain_first, make_pair(1, 0)));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001282
1283 // If only the second match has an explanation, only this explanation should
1284 // be printed.
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001285 const Matcher<pair<int, int> > explain_second = Pair(0, GreaterThan(0));
zhanyong.wan676e8cc2010-03-16 20:01:51 +00001286 EXPECT_EQ("whose both fields match, where the second field is a value "
1287 "which is 1 more than 0",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001288 Explain(explain_second, make_pair(0, 1)));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001289}
1290
1291TEST(PairTest, MatchesCorrectly) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001292 pair<int, std::string> p(25, "foo");
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001293
1294 // Both fields match.
1295 EXPECT_THAT(p, Pair(25, "foo"));
1296 EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o")));
1297
1298 // 'first' doesnt' match, but 'second' matches.
1299 EXPECT_THAT(p, Not(Pair(42, "foo")));
1300 EXPECT_THAT(p, Not(Pair(Lt(25), "foo")));
1301
1302 // 'first' matches, but 'second' doesn't match.
1303 EXPECT_THAT(p, Not(Pair(25, "bar")));
1304 EXPECT_THAT(p, Not(Pair(25, Not("foo"))));
1305
1306 // Neither field matches.
1307 EXPECT_THAT(p, Not(Pair(13, "bar")));
1308 EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a"))));
1309}
1310
1311TEST(PairTest, SafelyCastsInnerMatchers) {
1312 Matcher<int> is_positive = Gt(0);
1313 Matcher<int> is_negative = Lt(0);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001314 pair<char, bool> p('a', true);
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001315 EXPECT_THAT(p, Pair(is_positive, _));
1316 EXPECT_THAT(p, Not(Pair(is_negative, _)));
1317 EXPECT_THAT(p, Pair(_, is_positive));
1318 EXPECT_THAT(p, Not(Pair(_, is_negative)));
1319}
1320
1321TEST(PairTest, InsideContainsUsingMap) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001322 map<int, char> container;
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001323 container.insert(make_pair(1, 'a'));
1324 container.insert(make_pair(2, 'b'));
1325 container.insert(make_pair(4, 'c'));
zhanyong.wan95b12332009-09-25 18:55:50 +00001326 EXPECT_THAT(container, Contains(Pair(1, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001327 EXPECT_THAT(container, Contains(Pair(1, _)));
zhanyong.wan95b12332009-09-25 18:55:50 +00001328 EXPECT_THAT(container, Contains(Pair(_, 'a')));
zhanyong.wanf5e1ce52009-09-16 07:02:02 +00001329 EXPECT_THAT(container, Not(Contains(Pair(3, _))));
1330}
1331
shiqiane35fdd92008-12-10 05:08:54 +00001332// Tests StartsWith(s).
1333
1334TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
1335 const Matcher<const char*> m1 = StartsWith(string(""));
1336 EXPECT_TRUE(m1.Matches("Hi"));
1337 EXPECT_TRUE(m1.Matches(""));
1338 EXPECT_FALSE(m1.Matches(NULL));
1339
1340 const Matcher<const string&> m2 = StartsWith("Hi");
1341 EXPECT_TRUE(m2.Matches("Hi"));
1342 EXPECT_TRUE(m2.Matches("Hi Hi!"));
1343 EXPECT_TRUE(m2.Matches("High"));
1344 EXPECT_FALSE(m2.Matches("H"));
1345 EXPECT_FALSE(m2.Matches(" Hi"));
1346}
1347
1348TEST(StartsWithTest, CanDescribeSelf) {
1349 Matcher<const std::string> m = StartsWith("Hi");
1350 EXPECT_EQ("starts with \"Hi\"", Describe(m));
1351}
1352
1353// Tests EndsWith(s).
1354
1355TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
1356 const Matcher<const char*> m1 = EndsWith("");
1357 EXPECT_TRUE(m1.Matches("Hi"));
1358 EXPECT_TRUE(m1.Matches(""));
1359 EXPECT_FALSE(m1.Matches(NULL));
1360
1361 const Matcher<const string&> m2 = EndsWith(string("Hi"));
1362 EXPECT_TRUE(m2.Matches("Hi"));
1363 EXPECT_TRUE(m2.Matches("Wow Hi Hi"));
1364 EXPECT_TRUE(m2.Matches("Super Hi"));
1365 EXPECT_FALSE(m2.Matches("i"));
1366 EXPECT_FALSE(m2.Matches("Hi "));
1367}
1368
1369TEST(EndsWithTest, CanDescribeSelf) {
1370 Matcher<const std::string> m = EndsWith("Hi");
1371 EXPECT_EQ("ends with \"Hi\"", Describe(m));
1372}
1373
shiqiane35fdd92008-12-10 05:08:54 +00001374// Tests MatchesRegex().
1375
1376TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
1377 const Matcher<const char*> m1 = MatchesRegex("a.*z");
1378 EXPECT_TRUE(m1.Matches("az"));
1379 EXPECT_TRUE(m1.Matches("abcz"));
1380 EXPECT_FALSE(m1.Matches(NULL));
1381
1382 const Matcher<const string&> m2 = MatchesRegex(new RE("a.*z"));
1383 EXPECT_TRUE(m2.Matches("azbz"));
1384 EXPECT_FALSE(m2.Matches("az1"));
1385 EXPECT_FALSE(m2.Matches("1az"));
1386}
1387
1388TEST(MatchesRegexTest, CanDescribeSelf) {
1389 Matcher<const std::string> m1 = MatchesRegex(string("Hi.*"));
1390 EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1));
1391
zhanyong.wand14aaed2010-01-14 05:36:32 +00001392 Matcher<const char*> m2 = MatchesRegex(new RE("a.*"));
1393 EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +00001394}
1395
1396// Tests ContainsRegex().
1397
1398TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
1399 const Matcher<const char*> m1 = ContainsRegex(string("a.*z"));
1400 EXPECT_TRUE(m1.Matches("az"));
1401 EXPECT_TRUE(m1.Matches("0abcz1"));
1402 EXPECT_FALSE(m1.Matches(NULL));
1403
1404 const Matcher<const string&> m2 = ContainsRegex(new RE("a.*z"));
1405 EXPECT_TRUE(m2.Matches("azbz"));
1406 EXPECT_TRUE(m2.Matches("az1"));
1407 EXPECT_FALSE(m2.Matches("1a"));
1408}
1409
1410TEST(ContainsRegexTest, CanDescribeSelf) {
1411 Matcher<const std::string> m1 = ContainsRegex("Hi.*");
1412 EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1));
1413
zhanyong.wand14aaed2010-01-14 05:36:32 +00001414 Matcher<const char*> m2 = ContainsRegex(new RE("a.*"));
1415 EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2));
shiqiane35fdd92008-12-10 05:08:54 +00001416}
shiqiane35fdd92008-12-10 05:08:54 +00001417
1418// Tests for wide strings.
1419#if GTEST_HAS_STD_WSTRING
1420TEST(StdWideStrEqTest, MatchesEqual) {
1421 Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello"));
1422 EXPECT_TRUE(m.Matches(L"Hello"));
1423 EXPECT_FALSE(m.Matches(L"hello"));
1424 EXPECT_FALSE(m.Matches(NULL));
1425
1426 Matcher<const ::std::wstring&> m2 = StrEq(L"Hello");
1427 EXPECT_TRUE(m2.Matches(L"Hello"));
1428 EXPECT_FALSE(m2.Matches(L"Hi"));
1429
1430 Matcher<const ::std::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1431 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1432 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1433
1434 ::std::wstring str(L"01204500800");
1435 str[3] = L'\0';
1436 Matcher<const ::std::wstring&> m4 = StrEq(str);
1437 EXPECT_TRUE(m4.Matches(str));
1438 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1439 Matcher<const ::std::wstring&> m5 = StrEq(str);
1440 EXPECT_TRUE(m5.Matches(str));
1441}
1442
1443TEST(StdWideStrEqTest, CanDescribeSelf) {
vladlosevaa432202011-04-01 21:58:42 +00001444 Matcher< ::std::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v");
1445 EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
shiqiane35fdd92008-12-10 05:08:54 +00001446 Describe(m));
1447
1448 Matcher< ::std::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1449 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1450 Describe(m2));
1451
1452 ::std::wstring str(L"01204500800");
1453 str[3] = L'\0';
1454 Matcher<const ::std::wstring&> m4 = StrEq(str);
1455 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1456 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1457 Matcher<const ::std::wstring&> m5 = StrEq(str);
1458 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1459}
1460
1461TEST(StdWideStrNeTest, MatchesUnequalString) {
1462 Matcher<const wchar_t*> m = StrNe(L"Hello");
1463 EXPECT_TRUE(m.Matches(L""));
1464 EXPECT_TRUE(m.Matches(NULL));
1465 EXPECT_FALSE(m.Matches(L"Hello"));
1466
1467 Matcher< ::std::wstring> m2 = StrNe(::std::wstring(L"Hello"));
1468 EXPECT_TRUE(m2.Matches(L"hello"));
1469 EXPECT_FALSE(m2.Matches(L"Hello"));
1470}
1471
1472TEST(StdWideStrNeTest, CanDescribeSelf) {
1473 Matcher<const wchar_t*> m = StrNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001474 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001475}
1476
1477TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1478 Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello"));
1479 EXPECT_TRUE(m.Matches(L"Hello"));
1480 EXPECT_TRUE(m.Matches(L"hello"));
1481 EXPECT_FALSE(m.Matches(L"Hi"));
1482 EXPECT_FALSE(m.Matches(NULL));
1483
1484 Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello");
1485 EXPECT_TRUE(m2.Matches(L"hello"));
1486 EXPECT_FALSE(m2.Matches(L"Hi"));
1487}
1488
1489TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1490 ::std::wstring str1(L"oabocdooeoo");
1491 ::std::wstring str2(L"OABOCDOOEOO");
1492 Matcher<const ::std::wstring&> m0 = StrCaseEq(str1);
1493 EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L'\0')));
1494
1495 str1[3] = str2[3] = L'\0';
1496 Matcher<const ::std::wstring&> m1 = StrCaseEq(str1);
1497 EXPECT_TRUE(m1.Matches(str2));
1498
1499 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1500 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1501 Matcher<const ::std::wstring&> m2 = StrCaseEq(str1);
1502 str1[9] = str2[9] = L'\0';
1503 EXPECT_FALSE(m2.Matches(str2));
1504
1505 Matcher<const ::std::wstring&> m3 = StrCaseEq(str1);
1506 EXPECT_TRUE(m3.Matches(str2));
1507
1508 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1509 str2.append(1, L'\0');
1510 EXPECT_FALSE(m3.Matches(str2));
1511 EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9)));
1512}
1513
1514TEST(StdWideStrCaseEqTest, CanDescribeSelf) {
1515 Matcher< ::std::wstring> m = StrCaseEq(L"Hi");
1516 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1517}
1518
1519TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1520 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1521 EXPECT_TRUE(m.Matches(L"Hi"));
1522 EXPECT_TRUE(m.Matches(NULL));
1523 EXPECT_FALSE(m.Matches(L"Hello"));
1524 EXPECT_FALSE(m.Matches(L"hello"));
1525
1526 Matcher< ::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello"));
1527 EXPECT_TRUE(m2.Matches(L""));
1528 EXPECT_FALSE(m2.Matches(L"Hello"));
1529}
1530
1531TEST(StdWideStrCaseNeTest, CanDescribeSelf) {
1532 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001533 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001534}
1535
1536// Tests that HasSubstr() works for matching wstring-typed values.
1537TEST(StdWideHasSubstrTest, WorksForStringClasses) {
1538 const Matcher< ::std::wstring> m1 = HasSubstr(L"foo");
1539 EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food.")));
1540 EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo")));
1541
1542 const Matcher<const ::std::wstring&> m2 = HasSubstr(L"foo");
1543 EXPECT_TRUE(m2.Matches(::std::wstring(L"I love food.")));
1544 EXPECT_FALSE(m2.Matches(::std::wstring(L"tofo")));
1545}
1546
1547// Tests that HasSubstr() works for matching C-wide-string-typed values.
1548TEST(StdWideHasSubstrTest, WorksForCStrings) {
1549 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1550 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1551 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1552 EXPECT_FALSE(m1.Matches(NULL));
1553
1554 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1555 EXPECT_TRUE(m2.Matches(L"I love food."));
1556 EXPECT_FALSE(m2.Matches(L"tofo"));
1557 EXPECT_FALSE(m2.Matches(NULL));
1558}
1559
1560// Tests that HasSubstr(s) describes itself properly.
1561TEST(StdWideHasSubstrTest, CanDescribeSelf) {
1562 Matcher< ::std::wstring> m = HasSubstr(L"foo\n\"");
1563 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1564}
1565
1566// Tests StartsWith(s).
1567
1568TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) {
1569 const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L""));
1570 EXPECT_TRUE(m1.Matches(L"Hi"));
1571 EXPECT_TRUE(m1.Matches(L""));
1572 EXPECT_FALSE(m1.Matches(NULL));
1573
1574 const Matcher<const ::std::wstring&> m2 = StartsWith(L"Hi");
1575 EXPECT_TRUE(m2.Matches(L"Hi"));
1576 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1577 EXPECT_TRUE(m2.Matches(L"High"));
1578 EXPECT_FALSE(m2.Matches(L"H"));
1579 EXPECT_FALSE(m2.Matches(L" Hi"));
1580}
1581
1582TEST(StdWideStartsWithTest, CanDescribeSelf) {
1583 Matcher<const ::std::wstring> m = StartsWith(L"Hi");
1584 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1585}
1586
1587// Tests EndsWith(s).
1588
1589TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) {
1590 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1591 EXPECT_TRUE(m1.Matches(L"Hi"));
1592 EXPECT_TRUE(m1.Matches(L""));
1593 EXPECT_FALSE(m1.Matches(NULL));
1594
1595 const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L"Hi"));
1596 EXPECT_TRUE(m2.Matches(L"Hi"));
1597 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1598 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1599 EXPECT_FALSE(m2.Matches(L"i"));
1600 EXPECT_FALSE(m2.Matches(L"Hi "));
1601}
1602
1603TEST(StdWideEndsWithTest, CanDescribeSelf) {
1604 Matcher<const ::std::wstring> m = EndsWith(L"Hi");
1605 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1606}
1607
1608#endif // GTEST_HAS_STD_WSTRING
1609
1610#if GTEST_HAS_GLOBAL_WSTRING
1611TEST(GlobalWideStrEqTest, MatchesEqual) {
1612 Matcher<const wchar_t*> m = StrEq(::wstring(L"Hello"));
1613 EXPECT_TRUE(m.Matches(L"Hello"));
1614 EXPECT_FALSE(m.Matches(L"hello"));
1615 EXPECT_FALSE(m.Matches(NULL));
1616
1617 Matcher<const ::wstring&> m2 = StrEq(L"Hello");
1618 EXPECT_TRUE(m2.Matches(L"Hello"));
1619 EXPECT_FALSE(m2.Matches(L"Hi"));
1620
1621 Matcher<const ::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1622 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1623 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1624
1625 ::wstring str(L"01204500800");
1626 str[3] = L'\0';
1627 Matcher<const ::wstring&> m4 = StrEq(str);
1628 EXPECT_TRUE(m4.Matches(str));
1629 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1630 Matcher<const ::wstring&> m5 = StrEq(str);
1631 EXPECT_TRUE(m5.Matches(str));
1632}
1633
1634TEST(GlobalWideStrEqTest, CanDescribeSelf) {
vladlosevaa432202011-04-01 21:58:42 +00001635 Matcher< ::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v");
1636 EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
shiqiane35fdd92008-12-10 05:08:54 +00001637 Describe(m));
1638
1639 Matcher< ::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1640 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1641 Describe(m2));
1642
1643 ::wstring str(L"01204500800");
1644 str[3] = L'\0';
1645 Matcher<const ::wstring&> m4 = StrEq(str);
1646 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1647 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1648 Matcher<const ::wstring&> m5 = StrEq(str);
1649 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1650}
1651
1652TEST(GlobalWideStrNeTest, MatchesUnequalString) {
1653 Matcher<const wchar_t*> m = StrNe(L"Hello");
1654 EXPECT_TRUE(m.Matches(L""));
1655 EXPECT_TRUE(m.Matches(NULL));
1656 EXPECT_FALSE(m.Matches(L"Hello"));
1657
1658 Matcher< ::wstring> m2 = StrNe(::wstring(L"Hello"));
1659 EXPECT_TRUE(m2.Matches(L"hello"));
1660 EXPECT_FALSE(m2.Matches(L"Hello"));
1661}
1662
1663TEST(GlobalWideStrNeTest, CanDescribeSelf) {
1664 Matcher<const wchar_t*> m = StrNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001665 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001666}
1667
1668TEST(GlobalWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1669 Matcher<const wchar_t*> m = StrCaseEq(::wstring(L"Hello"));
1670 EXPECT_TRUE(m.Matches(L"Hello"));
1671 EXPECT_TRUE(m.Matches(L"hello"));
1672 EXPECT_FALSE(m.Matches(L"Hi"));
1673 EXPECT_FALSE(m.Matches(NULL));
1674
1675 Matcher<const ::wstring&> m2 = StrCaseEq(L"Hello");
1676 EXPECT_TRUE(m2.Matches(L"hello"));
1677 EXPECT_FALSE(m2.Matches(L"Hi"));
1678}
1679
1680TEST(GlobalWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1681 ::wstring str1(L"oabocdooeoo");
1682 ::wstring str2(L"OABOCDOOEOO");
1683 Matcher<const ::wstring&> m0 = StrCaseEq(str1);
1684 EXPECT_FALSE(m0.Matches(str2 + ::wstring(1, L'\0')));
1685
1686 str1[3] = str2[3] = L'\0';
1687 Matcher<const ::wstring&> m1 = StrCaseEq(str1);
1688 EXPECT_TRUE(m1.Matches(str2));
1689
1690 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1691 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1692 Matcher<const ::wstring&> m2 = StrCaseEq(str1);
1693 str1[9] = str2[9] = L'\0';
1694 EXPECT_FALSE(m2.Matches(str2));
1695
1696 Matcher<const ::wstring&> m3 = StrCaseEq(str1);
1697 EXPECT_TRUE(m3.Matches(str2));
1698
1699 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1700 str2.append(1, L'\0');
1701 EXPECT_FALSE(m3.Matches(str2));
1702 EXPECT_FALSE(m3.Matches(::wstring(str2, 0, 9)));
1703}
1704
1705TEST(GlobalWideStrCaseEqTest, CanDescribeSelf) {
1706 Matcher< ::wstring> m = StrCaseEq(L"Hi");
1707 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1708}
1709
1710TEST(GlobalWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1711 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1712 EXPECT_TRUE(m.Matches(L"Hi"));
1713 EXPECT_TRUE(m.Matches(NULL));
1714 EXPECT_FALSE(m.Matches(L"Hello"));
1715 EXPECT_FALSE(m.Matches(L"hello"));
1716
1717 Matcher< ::wstring> m2 = StrCaseNe(::wstring(L"Hello"));
1718 EXPECT_TRUE(m2.Matches(L""));
1719 EXPECT_FALSE(m2.Matches(L"Hello"));
1720}
1721
1722TEST(GlobalWideStrCaseNeTest, CanDescribeSelf) {
1723 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001724 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001725}
1726
1727// Tests that HasSubstr() works for matching wstring-typed values.
1728TEST(GlobalWideHasSubstrTest, WorksForStringClasses) {
1729 const Matcher< ::wstring> m1 = HasSubstr(L"foo");
1730 EXPECT_TRUE(m1.Matches(::wstring(L"I love food.")));
1731 EXPECT_FALSE(m1.Matches(::wstring(L"tofo")));
1732
1733 const Matcher<const ::wstring&> m2 = HasSubstr(L"foo");
1734 EXPECT_TRUE(m2.Matches(::wstring(L"I love food.")));
1735 EXPECT_FALSE(m2.Matches(::wstring(L"tofo")));
1736}
1737
1738// Tests that HasSubstr() works for matching C-wide-string-typed values.
1739TEST(GlobalWideHasSubstrTest, WorksForCStrings) {
1740 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1741 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1742 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1743 EXPECT_FALSE(m1.Matches(NULL));
1744
1745 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
1746 EXPECT_TRUE(m2.Matches(L"I love food."));
1747 EXPECT_FALSE(m2.Matches(L"tofo"));
1748 EXPECT_FALSE(m2.Matches(NULL));
1749}
1750
1751// Tests that HasSubstr(s) describes itself properly.
1752TEST(GlobalWideHasSubstrTest, CanDescribeSelf) {
1753 Matcher< ::wstring> m = HasSubstr(L"foo\n\"");
1754 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
1755}
1756
1757// Tests StartsWith(s).
1758
1759TEST(GlobalWideStartsWithTest, MatchesStringWithGivenPrefix) {
1760 const Matcher<const wchar_t*> m1 = StartsWith(::wstring(L""));
1761 EXPECT_TRUE(m1.Matches(L"Hi"));
1762 EXPECT_TRUE(m1.Matches(L""));
1763 EXPECT_FALSE(m1.Matches(NULL));
1764
1765 const Matcher<const ::wstring&> m2 = StartsWith(L"Hi");
1766 EXPECT_TRUE(m2.Matches(L"Hi"));
1767 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
1768 EXPECT_TRUE(m2.Matches(L"High"));
1769 EXPECT_FALSE(m2.Matches(L"H"));
1770 EXPECT_FALSE(m2.Matches(L" Hi"));
1771}
1772
1773TEST(GlobalWideStartsWithTest, CanDescribeSelf) {
1774 Matcher<const ::wstring> m = StartsWith(L"Hi");
1775 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
1776}
1777
1778// Tests EndsWith(s).
1779
1780TEST(GlobalWideEndsWithTest, MatchesStringWithGivenSuffix) {
1781 const Matcher<const wchar_t*> m1 = EndsWith(L"");
1782 EXPECT_TRUE(m1.Matches(L"Hi"));
1783 EXPECT_TRUE(m1.Matches(L""));
1784 EXPECT_FALSE(m1.Matches(NULL));
1785
1786 const Matcher<const ::wstring&> m2 = EndsWith(::wstring(L"Hi"));
1787 EXPECT_TRUE(m2.Matches(L"Hi"));
1788 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
1789 EXPECT_TRUE(m2.Matches(L"Super Hi"));
1790 EXPECT_FALSE(m2.Matches(L"i"));
1791 EXPECT_FALSE(m2.Matches(L"Hi "));
1792}
1793
1794TEST(GlobalWideEndsWithTest, CanDescribeSelf) {
1795 Matcher<const ::wstring> m = EndsWith(L"Hi");
1796 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
1797}
1798
1799#endif // GTEST_HAS_GLOBAL_WSTRING
1800
1801
1802typedef ::std::tr1::tuple<long, int> Tuple2; // NOLINT
1803
1804// Tests that Eq() matches a 2-tuple where the first field == the
1805// second field.
1806TEST(Eq2Test, MatchesEqualArguments) {
1807 Matcher<const Tuple2&> m = Eq();
1808 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1809 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1810}
1811
1812// Tests that Eq() describes itself properly.
1813TEST(Eq2Test, CanDescribeSelf) {
1814 Matcher<const Tuple2&> m = Eq();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001815 EXPECT_EQ("are an equal pair", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001816}
1817
1818// Tests that Ge() matches a 2-tuple where the first field >= the
1819// second field.
1820TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) {
1821 Matcher<const Tuple2&> m = Ge();
1822 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1823 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1824 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1825}
1826
1827// Tests that Ge() describes itself properly.
1828TEST(Ge2Test, CanDescribeSelf) {
1829 Matcher<const Tuple2&> m = Ge();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001830 EXPECT_EQ("are a pair where the first >= the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001831}
1832
1833// Tests that Gt() matches a 2-tuple where the first field > the
1834// second field.
1835TEST(Gt2Test, MatchesGreaterThanArguments) {
1836 Matcher<const Tuple2&> m = Gt();
1837 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1838 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1839 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
1840}
1841
1842// Tests that Gt() describes itself properly.
1843TEST(Gt2Test, CanDescribeSelf) {
1844 Matcher<const Tuple2&> m = Gt();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001845 EXPECT_EQ("are a pair where the first > the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001846}
1847
1848// Tests that Le() matches a 2-tuple where the first field <= the
1849// second field.
1850TEST(Le2Test, MatchesLessThanOrEqualArguments) {
1851 Matcher<const Tuple2&> m = Le();
1852 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1853 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
1854 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1855}
1856
1857// Tests that Le() describes itself properly.
1858TEST(Le2Test, CanDescribeSelf) {
1859 Matcher<const Tuple2&> m = Le();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001860 EXPECT_EQ("are a pair where the first <= the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001861}
1862
1863// Tests that Lt() matches a 2-tuple where the first field < the
1864// second field.
1865TEST(Lt2Test, MatchesLessThanArguments) {
1866 Matcher<const Tuple2&> m = Lt();
1867 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1868 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1869 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
1870}
1871
1872// Tests that Lt() describes itself properly.
1873TEST(Lt2Test, CanDescribeSelf) {
1874 Matcher<const Tuple2&> m = Lt();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001875 EXPECT_EQ("are a pair where the first < the second", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001876}
1877
1878// Tests that Ne() matches a 2-tuple where the first field != the
1879// second field.
1880TEST(Ne2Test, MatchesUnequalArguments) {
1881 Matcher<const Tuple2&> m = Ne();
1882 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
1883 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
1884 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
1885}
1886
1887// Tests that Ne() describes itself properly.
1888TEST(Ne2Test, CanDescribeSelf) {
1889 Matcher<const Tuple2&> m = Ne();
zhanyong.wanab5b77c2010-05-17 19:32:48 +00001890 EXPECT_EQ("are an unequal pair", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001891}
1892
1893// Tests that Not(m) matches any value that doesn't match m.
1894TEST(NotTest, NegatesMatcher) {
1895 Matcher<int> m;
1896 m = Not(Eq(2));
1897 EXPECT_TRUE(m.Matches(3));
1898 EXPECT_FALSE(m.Matches(2));
1899}
1900
1901// Tests that Not(m) describes itself properly.
1902TEST(NotTest, CanDescribeSelf) {
1903 Matcher<int> m = Not(Eq(5));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001904 EXPECT_EQ("isn't equal to 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001905}
1906
zhanyong.wan18490652009-05-11 18:54:08 +00001907// Tests that monomorphic matchers are safely cast by the Not matcher.
1908TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {
1909 // greater_than_5 is a monomorphic matcher.
1910 Matcher<int> greater_than_5 = Gt(5);
1911
1912 Matcher<const int&> m = Not(greater_than_5);
1913 Matcher<int&> m2 = Not(greater_than_5);
1914 Matcher<int&> m3 = Not(m);
1915}
1916
zhanyong.wan02c15052010-06-09 19:21:30 +00001917// Helper to allow easy testing of AllOf matchers with num parameters.
1918void AllOfMatches(int num, const Matcher<int>& m) {
1919 SCOPED_TRACE(Describe(m));
1920 EXPECT_TRUE(m.Matches(0));
1921 for (int i = 1; i <= num; ++i) {
1922 EXPECT_FALSE(m.Matches(i));
1923 }
1924 EXPECT_TRUE(m.Matches(num + 1));
1925}
1926
shiqiane35fdd92008-12-10 05:08:54 +00001927// Tests that AllOf(m1, ..., mn) matches any value that matches all of
1928// the given matchers.
1929TEST(AllOfTest, MatchesWhenAllMatch) {
1930 Matcher<int> m;
1931 m = AllOf(Le(2), Ge(1));
1932 EXPECT_TRUE(m.Matches(1));
1933 EXPECT_TRUE(m.Matches(2));
1934 EXPECT_FALSE(m.Matches(0));
1935 EXPECT_FALSE(m.Matches(3));
1936
1937 m = AllOf(Gt(0), Ne(1), Ne(2));
1938 EXPECT_TRUE(m.Matches(3));
1939 EXPECT_FALSE(m.Matches(2));
1940 EXPECT_FALSE(m.Matches(1));
1941 EXPECT_FALSE(m.Matches(0));
1942
1943 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
1944 EXPECT_TRUE(m.Matches(4));
1945 EXPECT_FALSE(m.Matches(3));
1946 EXPECT_FALSE(m.Matches(2));
1947 EXPECT_FALSE(m.Matches(1));
1948 EXPECT_FALSE(m.Matches(0));
1949
1950 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
1951 EXPECT_TRUE(m.Matches(0));
1952 EXPECT_TRUE(m.Matches(1));
1953 EXPECT_FALSE(m.Matches(3));
zhanyong.wan02c15052010-06-09 19:21:30 +00001954
1955 // The following tests for varying number of sub-matchers. Due to the way
1956 // the sub-matchers are handled it is enough to test every sub-matcher once
1957 // with sub-matchers using the same matcher type. Varying matcher types are
1958 // checked for above.
1959 AllOfMatches(2, AllOf(Ne(1), Ne(2)));
1960 AllOfMatches(3, AllOf(Ne(1), Ne(2), Ne(3)));
1961 AllOfMatches(4, AllOf(Ne(1), Ne(2), Ne(3), Ne(4)));
1962 AllOfMatches(5, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5)));
1963 AllOfMatches(6, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6)));
1964 AllOfMatches(7, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7)));
1965 AllOfMatches(8, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
1966 Ne(8)));
1967 AllOfMatches(9, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
1968 Ne(8), Ne(9)));
1969 AllOfMatches(10, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8),
1970 Ne(9), Ne(10)));
shiqiane35fdd92008-12-10 05:08:54 +00001971}
1972
1973// Tests that AllOf(m1, ..., mn) describes itself properly.
1974TEST(AllOfTest, CanDescribeSelf) {
1975 Matcher<int> m;
1976 m = AllOf(Le(2), Ge(1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001977 EXPECT_EQ("(is <= 2) and (is >= 1)", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +00001978
1979 m = AllOf(Gt(0), Ne(1), Ne(2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001980 EXPECT_EQ("(is > 0) and "
1981 "((isn't equal to 1) and "
1982 "(isn't equal to 2))",
shiqiane35fdd92008-12-10 05:08:54 +00001983 Describe(m));
1984
1985
1986 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
jgm79a367e2012-04-10 16:02:11 +00001987 EXPECT_EQ("((is > 0) and "
1988 "(isn't equal to 1)) and "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001989 "((isn't equal to 2) and "
jgm79a367e2012-04-10 16:02:11 +00001990 "(isn't equal to 3))",
shiqiane35fdd92008-12-10 05:08:54 +00001991 Describe(m));
1992
1993
1994 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
jgm79a367e2012-04-10 16:02:11 +00001995 EXPECT_EQ("((is >= 0) and "
1996 "(is < 10)) and "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001997 "((isn't equal to 3) and "
1998 "((isn't equal to 5) and "
jgm79a367e2012-04-10 16:02:11 +00001999 "(isn't equal to 7)))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002000 Describe(m));
2001}
2002
2003// Tests that AllOf(m1, ..., mn) describes its negation properly.
2004TEST(AllOfTest, CanDescribeNegation) {
2005 Matcher<int> m;
2006 m = AllOf(Le(2), Ge(1));
2007 EXPECT_EQ("(isn't <= 2) or "
2008 "(isn't >= 1)",
2009 DescribeNegation(m));
2010
2011 m = AllOf(Gt(0), Ne(1), Ne(2));
2012 EXPECT_EQ("(isn't > 0) or "
2013 "((is equal to 1) or "
2014 "(is equal to 2))",
2015 DescribeNegation(m));
2016
2017
2018 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
jgm79a367e2012-04-10 16:02:11 +00002019 EXPECT_EQ("((isn't > 0) or "
2020 "(is equal to 1)) or "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002021 "((is equal to 2) or "
jgm79a367e2012-04-10 16:02:11 +00002022 "(is equal to 3))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002023 DescribeNegation(m));
2024
2025
2026 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
jgm79a367e2012-04-10 16:02:11 +00002027 EXPECT_EQ("((isn't >= 0) or "
2028 "(isn't < 10)) or "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002029 "((is equal to 3) or "
2030 "((is equal to 5) or "
jgm79a367e2012-04-10 16:02:11 +00002031 "(is equal to 7)))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002032 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00002033}
2034
zhanyong.wan18490652009-05-11 18:54:08 +00002035// Tests that monomorphic matchers are safely cast by the AllOf matcher.
2036TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
2037 // greater_than_5 and less_than_10 are monomorphic matchers.
2038 Matcher<int> greater_than_5 = Gt(5);
2039 Matcher<int> less_than_10 = Lt(10);
2040
2041 Matcher<const int&> m = AllOf(greater_than_5, less_than_10);
2042 Matcher<int&> m2 = AllOf(greater_than_5, less_than_10);
2043 Matcher<int&> m3 = AllOf(greater_than_5, m2);
2044
2045 // Tests that BothOf works when composing itself.
2046 Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10);
2047 Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);
2048}
2049
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002050TEST(AllOfTest, ExplainsResult) {
2051 Matcher<int> m;
2052
2053 // Successful match. Both matchers need to explain. The second
2054 // matcher doesn't give an explanation, so only the first matcher's
2055 // explanation is printed.
2056 m = AllOf(GreaterThan(10), Lt(30));
2057 EXPECT_EQ("which is 15 more than 10", Explain(m, 25));
2058
2059 // Successful match. Both matchers need to explain.
2060 m = AllOf(GreaterThan(10), GreaterThan(20));
2061 EXPECT_EQ("which is 20 more than 10, and which is 10 more than 20",
2062 Explain(m, 30));
2063
2064 // Successful match. All matchers need to explain. The second
2065 // matcher doesn't given an explanation.
2066 m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20));
2067 EXPECT_EQ("which is 15 more than 10, and which is 5 more than 20",
2068 Explain(m, 25));
2069
2070 // Successful match. All matchers need to explain.
2071 m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2072 EXPECT_EQ("which is 30 more than 10, and which is 20 more than 20, "
2073 "and which is 10 more than 30",
2074 Explain(m, 40));
2075
2076 // Failed match. The first matcher, which failed, needs to
2077 // explain.
2078 m = AllOf(GreaterThan(10), GreaterThan(20));
2079 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
2080
2081 // Failed match. The second matcher, which failed, needs to
2082 // explain. Since it doesn't given an explanation, nothing is
2083 // printed.
2084 m = AllOf(GreaterThan(10), Lt(30));
2085 EXPECT_EQ("", Explain(m, 40));
2086
2087 // Failed match. The second matcher, which failed, needs to
2088 // explain.
2089 m = AllOf(GreaterThan(10), GreaterThan(20));
2090 EXPECT_EQ("which is 5 less than 20", Explain(m, 15));
2091}
2092
zhanyong.wan02c15052010-06-09 19:21:30 +00002093// Helper to allow easy testing of AnyOf matchers with num parameters.
2094void AnyOfMatches(int num, const Matcher<int>& m) {
2095 SCOPED_TRACE(Describe(m));
2096 EXPECT_FALSE(m.Matches(0));
2097 for (int i = 1; i <= num; ++i) {
2098 EXPECT_TRUE(m.Matches(i));
2099 }
2100 EXPECT_FALSE(m.Matches(num + 1));
2101}
2102
shiqiane35fdd92008-12-10 05:08:54 +00002103// Tests that AnyOf(m1, ..., mn) matches any value that matches at
2104// least one of the given matchers.
2105TEST(AnyOfTest, MatchesWhenAnyMatches) {
2106 Matcher<int> m;
2107 m = AnyOf(Le(1), Ge(3));
2108 EXPECT_TRUE(m.Matches(1));
2109 EXPECT_TRUE(m.Matches(4));
2110 EXPECT_FALSE(m.Matches(2));
2111
2112 m = AnyOf(Lt(0), Eq(1), Eq(2));
2113 EXPECT_TRUE(m.Matches(-1));
2114 EXPECT_TRUE(m.Matches(1));
2115 EXPECT_TRUE(m.Matches(2));
2116 EXPECT_FALSE(m.Matches(0));
2117
2118 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2119 EXPECT_TRUE(m.Matches(-1));
2120 EXPECT_TRUE(m.Matches(1));
2121 EXPECT_TRUE(m.Matches(2));
2122 EXPECT_TRUE(m.Matches(3));
2123 EXPECT_FALSE(m.Matches(0));
2124
2125 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2126 EXPECT_TRUE(m.Matches(0));
2127 EXPECT_TRUE(m.Matches(11));
2128 EXPECT_TRUE(m.Matches(3));
2129 EXPECT_FALSE(m.Matches(2));
zhanyong.wan02c15052010-06-09 19:21:30 +00002130
2131 // The following tests for varying number of sub-matchers. Due to the way
2132 // the sub-matchers are handled it is enough to test every sub-matcher once
2133 // with sub-matchers using the same matcher type. Varying matcher types are
2134 // checked for above.
2135 AnyOfMatches(2, AnyOf(1, 2));
2136 AnyOfMatches(3, AnyOf(1, 2, 3));
2137 AnyOfMatches(4, AnyOf(1, 2, 3, 4));
2138 AnyOfMatches(5, AnyOf(1, 2, 3, 4, 5));
2139 AnyOfMatches(6, AnyOf(1, 2, 3, 4, 5, 6));
2140 AnyOfMatches(7, AnyOf(1, 2, 3, 4, 5, 6, 7));
2141 AnyOfMatches(8, AnyOf(1, 2, 3, 4, 5, 6, 7, 8));
2142 AnyOfMatches(9, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9));
2143 AnyOfMatches(10, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
shiqiane35fdd92008-12-10 05:08:54 +00002144}
2145
2146// Tests that AnyOf(m1, ..., mn) describes itself properly.
2147TEST(AnyOfTest, CanDescribeSelf) {
2148 Matcher<int> m;
2149 m = AnyOf(Le(1), Ge(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002150 EXPECT_EQ("(is <= 1) or (is >= 3)",
shiqiane35fdd92008-12-10 05:08:54 +00002151 Describe(m));
2152
2153 m = AnyOf(Lt(0), Eq(1), Eq(2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002154 EXPECT_EQ("(is < 0) or "
shiqiane35fdd92008-12-10 05:08:54 +00002155 "((is equal to 1) or (is equal to 2))",
2156 Describe(m));
2157
2158 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
jgm79a367e2012-04-10 16:02:11 +00002159 EXPECT_EQ("((is < 0) or "
2160 "(is equal to 1)) or "
shiqiane35fdd92008-12-10 05:08:54 +00002161 "((is equal to 2) or "
jgm79a367e2012-04-10 16:02:11 +00002162 "(is equal to 3))",
shiqiane35fdd92008-12-10 05:08:54 +00002163 Describe(m));
2164
2165 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
jgm79a367e2012-04-10 16:02:11 +00002166 EXPECT_EQ("((is <= 0) or "
2167 "(is > 10)) or "
shiqiane35fdd92008-12-10 05:08:54 +00002168 "((is equal to 3) or "
2169 "((is equal to 5) or "
jgm79a367e2012-04-10 16:02:11 +00002170 "(is equal to 7)))",
shiqiane35fdd92008-12-10 05:08:54 +00002171 Describe(m));
2172}
2173
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002174// Tests that AnyOf(m1, ..., mn) describes its negation properly.
2175TEST(AnyOfTest, CanDescribeNegation) {
2176 Matcher<int> m;
2177 m = AnyOf(Le(1), Ge(3));
2178 EXPECT_EQ("(isn't <= 1) and (isn't >= 3)",
2179 DescribeNegation(m));
2180
2181 m = AnyOf(Lt(0), Eq(1), Eq(2));
2182 EXPECT_EQ("(isn't < 0) and "
2183 "((isn't equal to 1) and (isn't equal to 2))",
2184 DescribeNegation(m));
2185
2186 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
jgm79a367e2012-04-10 16:02:11 +00002187 EXPECT_EQ("((isn't < 0) and "
2188 "(isn't equal to 1)) and "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002189 "((isn't equal to 2) and "
jgm79a367e2012-04-10 16:02:11 +00002190 "(isn't equal to 3))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002191 DescribeNegation(m));
2192
2193 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
jgm79a367e2012-04-10 16:02:11 +00002194 EXPECT_EQ("((isn't <= 0) and "
2195 "(isn't > 10)) and "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002196 "((isn't equal to 3) and "
2197 "((isn't equal to 5) and "
jgm79a367e2012-04-10 16:02:11 +00002198 "(isn't equal to 7)))",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002199 DescribeNegation(m));
2200}
2201
zhanyong.wan18490652009-05-11 18:54:08 +00002202// Tests that monomorphic matchers are safely cast by the AnyOf matcher.
2203TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
2204 // greater_than_5 and less_than_10 are monomorphic matchers.
2205 Matcher<int> greater_than_5 = Gt(5);
2206 Matcher<int> less_than_10 = Lt(10);
2207
2208 Matcher<const int&> m = AnyOf(greater_than_5, less_than_10);
2209 Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10);
2210 Matcher<int&> m3 = AnyOf(greater_than_5, m2);
2211
2212 // Tests that EitherOf works when composing itself.
2213 Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10);
2214 Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);
2215}
2216
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002217TEST(AnyOfTest, ExplainsResult) {
2218 Matcher<int> m;
2219
2220 // Failed match. Both matchers need to explain. The second
2221 // matcher doesn't give an explanation, so only the first matcher's
2222 // explanation is printed.
2223 m = AnyOf(GreaterThan(10), Lt(0));
2224 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
2225
2226 // Failed match. Both matchers need to explain.
2227 m = AnyOf(GreaterThan(10), GreaterThan(20));
2228 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20",
2229 Explain(m, 5));
2230
2231 // Failed match. All matchers need to explain. The second
2232 // matcher doesn't given an explanation.
2233 m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30));
2234 EXPECT_EQ("which is 5 less than 10, and which is 25 less than 30",
2235 Explain(m, 5));
2236
2237 // Failed match. All matchers need to explain.
2238 m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2239 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20, "
2240 "and which is 25 less than 30",
2241 Explain(m, 5));
2242
2243 // Successful match. The first matcher, which succeeded, needs to
2244 // explain.
2245 m = AnyOf(GreaterThan(10), GreaterThan(20));
2246 EXPECT_EQ("which is 5 more than 10", Explain(m, 15));
2247
2248 // Successful match. The second matcher, which succeeded, needs to
2249 // explain. Since it doesn't given an explanation, nothing is
2250 // printed.
2251 m = AnyOf(GreaterThan(10), Lt(30));
2252 EXPECT_EQ("", Explain(m, 0));
2253
2254 // Successful match. The second matcher, which succeeded, needs to
2255 // explain.
2256 m = AnyOf(GreaterThan(30), GreaterThan(20));
2257 EXPECT_EQ("which is 5 more than 20", Explain(m, 25));
2258}
2259
shiqiane35fdd92008-12-10 05:08:54 +00002260// The following predicate function and predicate functor are for
2261// testing the Truly(predicate) matcher.
2262
2263// Returns non-zero if the input is positive. Note that the return
2264// type of this function is not bool. It's OK as Truly() accepts any
2265// unary function or functor whose return type can be implicitly
2266// converted to bool.
2267int IsPositive(double x) {
2268 return x > 0 ? 1 : 0;
2269}
2270
2271// This functor returns true if the input is greater than the given
2272// number.
2273class IsGreaterThan {
2274 public:
2275 explicit IsGreaterThan(int threshold) : threshold_(threshold) {}
2276
2277 bool operator()(int n) const { return n > threshold_; }
zhanyong.wan32de5f52009-12-23 00:13:23 +00002278
shiqiane35fdd92008-12-10 05:08:54 +00002279 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002280 int threshold_;
shiqiane35fdd92008-12-10 05:08:54 +00002281};
2282
2283// For testing Truly().
2284const int foo = 0;
2285
2286// This predicate returns true iff the argument references foo and has
2287// a zero value.
2288bool ReferencesFooAndIsZero(const int& n) {
2289 return (&n == &foo) && (n == 0);
2290}
2291
2292// Tests that Truly(predicate) matches what satisfies the given
2293// predicate.
2294TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {
2295 Matcher<double> m = Truly(IsPositive);
2296 EXPECT_TRUE(m.Matches(2.0));
2297 EXPECT_FALSE(m.Matches(-1.5));
2298}
2299
2300// Tests that Truly(predicate_functor) works too.
2301TEST(TrulyTest, CanBeUsedWithFunctor) {
2302 Matcher<int> m = Truly(IsGreaterThan(5));
2303 EXPECT_TRUE(m.Matches(6));
2304 EXPECT_FALSE(m.Matches(4));
2305}
2306
zhanyong.wan8d3dc0c2011-04-14 19:37:06 +00002307// A class that can be implicitly converted to bool.
2308class ConvertibleToBool {
2309 public:
2310 explicit ConvertibleToBool(int number) : number_(number) {}
2311 operator bool() const { return number_ != 0; }
2312
2313 private:
2314 int number_;
2315};
2316
2317ConvertibleToBool IsNotZero(int number) {
2318 return ConvertibleToBool(number);
2319}
2320
2321// Tests that the predicate used in Truly() may return a class that's
2322// implicitly convertible to bool, even when the class has no
2323// operator!().
2324TEST(TrulyTest, PredicateCanReturnAClassConvertibleToBool) {
2325 Matcher<int> m = Truly(IsNotZero);
2326 EXPECT_TRUE(m.Matches(1));
2327 EXPECT_FALSE(m.Matches(0));
2328}
2329
shiqiane35fdd92008-12-10 05:08:54 +00002330// Tests that Truly(predicate) can describe itself properly.
2331TEST(TrulyTest, CanDescribeSelf) {
2332 Matcher<double> m = Truly(IsPositive);
2333 EXPECT_EQ("satisfies the given predicate",
2334 Describe(m));
2335}
2336
2337// Tests that Truly(predicate) works when the matcher takes its
2338// argument by reference.
2339TEST(TrulyTest, WorksForByRefArguments) {
2340 Matcher<const int&> m = Truly(ReferencesFooAndIsZero);
2341 EXPECT_TRUE(m.Matches(foo));
2342 int n = 0;
2343 EXPECT_FALSE(m.Matches(n));
2344}
2345
2346// Tests that Matches(m) is a predicate satisfied by whatever that
2347// matches matcher m.
2348TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {
2349 EXPECT_TRUE(Matches(Ge(0))(1));
2350 EXPECT_FALSE(Matches(Eq('a'))('b'));
2351}
2352
2353// Tests that Matches(m) works when the matcher takes its argument by
2354// reference.
2355TEST(MatchesTest, WorksOnByRefArguments) {
2356 int m = 0, n = 0;
2357 EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n));
2358 EXPECT_FALSE(Matches(Ref(m))(n));
2359}
2360
2361// Tests that a Matcher on non-reference type can be used in
2362// Matches().
2363TEST(MatchesTest, WorksWithMatcherOnNonRefType) {
2364 Matcher<int> eq5 = Eq(5);
2365 EXPECT_TRUE(Matches(eq5)(5));
2366 EXPECT_FALSE(Matches(eq5)(2));
2367}
2368
zhanyong.wanb8243162009-06-04 05:48:20 +00002369// Tests Value(value, matcher). Since Value() is a simple wrapper for
2370// Matches(), which has been tested already, we don't spend a lot of
2371// effort on testing Value().
2372TEST(ValueTest, WorksWithPolymorphicMatcher) {
2373 EXPECT_TRUE(Value("hi", StartsWith("h")));
2374 EXPECT_FALSE(Value(5, Gt(10)));
2375}
2376
2377TEST(ValueTest, WorksWithMonomorphicMatcher) {
2378 const Matcher<int> is_zero = Eq(0);
2379 EXPECT_TRUE(Value(0, is_zero));
2380 EXPECT_FALSE(Value('a', is_zero));
2381
2382 int n = 0;
2383 const Matcher<const int&> ref_n = Ref(n);
2384 EXPECT_TRUE(Value(n, ref_n));
2385 EXPECT_FALSE(Value(1, ref_n));
2386}
2387
zhanyong.wana862f1d2010-03-15 21:23:04 +00002388TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) {
zhanyong.wan34b034c2010-03-05 21:23:23 +00002389 StringMatchResultListener listener1;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002390 EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002391 EXPECT_EQ("% 2 == 0", listener1.str());
2392
2393 StringMatchResultListener listener2;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002394 EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002395 EXPECT_EQ("", listener2.str());
2396}
2397
zhanyong.wana862f1d2010-03-15 21:23:04 +00002398TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) {
zhanyong.wan34b034c2010-03-05 21:23:23 +00002399 const Matcher<int> is_even = PolymorphicIsEven();
2400 StringMatchResultListener listener1;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002401 EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002402 EXPECT_EQ("% 2 == 0", listener1.str());
2403
2404 const Matcher<const double&> is_zero = Eq(0);
2405 StringMatchResultListener listener2;
zhanyong.wana862f1d2010-03-15 21:23:04 +00002406 EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2));
zhanyong.wan34b034c2010-03-05 21:23:23 +00002407 EXPECT_EQ("", listener2.str());
2408}
2409
zhanyong.wana862f1d2010-03-15 21:23:04 +00002410MATCHER_P(Really, inner_matcher, "") {
2411 return ExplainMatchResult(inner_matcher, arg, result_listener);
2412}
2413
2414TEST(ExplainMatchResultTest, WorksInsideMATCHER) {
2415 EXPECT_THAT(0, Really(Eq(0)));
2416}
2417
zhanyong.wanbf550852009-06-09 06:09:53 +00002418TEST(AllArgsTest, WorksForTuple) {
2419 EXPECT_THAT(make_tuple(1, 2L), AllArgs(Lt()));
2420 EXPECT_THAT(make_tuple(2L, 1), Not(AllArgs(Lt())));
2421}
2422
2423TEST(AllArgsTest, WorksForNonTuple) {
2424 EXPECT_THAT(42, AllArgs(Gt(0)));
2425 EXPECT_THAT('a', Not(AllArgs(Eq('b'))));
2426}
2427
2428class AllArgsHelper {
2429 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002430 AllArgsHelper() {}
2431
zhanyong.wanbf550852009-06-09 06:09:53 +00002432 MOCK_METHOD2(Helper, int(char x, int y));
zhanyong.wan32de5f52009-12-23 00:13:23 +00002433
2434 private:
2435 GTEST_DISALLOW_COPY_AND_ASSIGN_(AllArgsHelper);
zhanyong.wanbf550852009-06-09 06:09:53 +00002436};
2437
2438TEST(AllArgsTest, WorksInWithClause) {
2439 AllArgsHelper helper;
2440 ON_CALL(helper, Helper(_, _))
2441 .With(AllArgs(Lt()))
2442 .WillByDefault(Return(1));
2443 EXPECT_CALL(helper, Helper(_, _));
2444 EXPECT_CALL(helper, Helper(_, _))
2445 .With(AllArgs(Gt()))
2446 .WillOnce(Return(2));
2447
2448 EXPECT_EQ(1, helper.Helper('\1', 2));
2449 EXPECT_EQ(2, helper.Helper('a', 1));
2450}
2451
shiqiane35fdd92008-12-10 05:08:54 +00002452// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2453// matches the matcher.
2454TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {
2455 ASSERT_THAT(5, Ge(2)) << "This should succeed.";
2456 ASSERT_THAT("Foo", EndsWith("oo"));
2457 EXPECT_THAT(2, AllOf(Le(7), Ge(0))) << "This should succeed too.";
2458 EXPECT_THAT("Hello", StartsWith("Hell"));
2459}
2460
2461// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
2462// doesn't match the matcher.
2463TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {
2464 // 'n' must be static as it is used in an EXPECT_FATAL_FAILURE(),
2465 // which cannot reference auto variables.
zhanyong.wan736baa82010-09-27 17:44:16 +00002466 static unsigned short n; // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00002467 n = 5;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002468
2469 // VC++ prior to version 8.0 SP1 has a bug where it will not see any
2470 // functions declared in the namespace scope from within nested classes.
2471 // EXPECT/ASSERT_(NON)FATAL_FAILURE macros use nested classes so that all
2472 // namespace-level functions invoked inside them need to be explicitly
2473 // resolved.
2474 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Gt(10)),
shiqiane35fdd92008-12-10 05:08:54 +00002475 "Value of: n\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002476 "Expected: is > 10\n"
zhanyong.wan736baa82010-09-27 17:44:16 +00002477 " Actual: 5" + OfType("unsigned short"));
shiqiane35fdd92008-12-10 05:08:54 +00002478 n = 0;
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002479 EXPECT_NONFATAL_FAILURE(
2480 EXPECT_THAT(n, ::testing::AllOf(::testing::Le(7), ::testing::Ge(5))),
2481 "Value of: n\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002482 "Expected: (is <= 7) and (is >= 5)\n"
zhanyong.wan736baa82010-09-27 17:44:16 +00002483 " Actual: 0" + OfType("unsigned short"));
shiqiane35fdd92008-12-10 05:08:54 +00002484}
2485
2486// Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument
2487// has a reference type.
2488TEST(MatcherAssertionTest, WorksForByRefArguments) {
2489 // We use a static variable here as EXPECT_FATAL_FAILURE() cannot
2490 // reference auto variables.
2491 static int n;
2492 n = 0;
2493 EXPECT_THAT(n, AllOf(Le(7), Ref(n)));
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002494 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
shiqiane35fdd92008-12-10 05:08:54 +00002495 "Value of: n\n"
2496 "Expected: does not reference the variable @");
2497 // Tests the "Actual" part.
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00002498 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
zhanyong.wan736baa82010-09-27 17:44:16 +00002499 "Actual: 0" + OfType("int") + ", which is located @");
shiqiane35fdd92008-12-10 05:08:54 +00002500}
2501
zhanyong.wan95b12332009-09-25 18:55:50 +00002502#if !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002503// Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is
2504// monomorphic.
zhanyong.wan95b12332009-09-25 18:55:50 +00002505
2506// ASSERT_THAT("hello", starts_with_he) fails to compile with Nokia's
2507// Symbian compiler: it tries to compile
2508// template<T, U> class MatcherCastImpl { ...
zhanyong.wandb22c222010-01-28 21:52:29 +00002509// virtual bool MatchAndExplain(T x, ...) const {
2510// return source_matcher_.MatchAndExplain(static_cast<U>(x), ...);
zhanyong.wan95b12332009-09-25 18:55:50 +00002511// with U == string and T == const char*
2512// With ASSERT_THAT("hello"...) changed to ASSERT_THAT(string("hello") ... )
2513// the compiler silently crashes with no output.
2514// If MatcherCastImpl is changed to use U(x) instead of static_cast<U>(x)
2515// the code compiles but the converted string is bogus.
shiqiane35fdd92008-12-10 05:08:54 +00002516TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
2517 Matcher<const char*> starts_with_he = StartsWith("he");
2518 ASSERT_THAT("hello", starts_with_he);
2519
2520 Matcher<const string&> ends_with_ok = EndsWith("ok");
2521 ASSERT_THAT("book", ends_with_ok);
zhanyong.wan736baa82010-09-27 17:44:16 +00002522 const string bad = "bad";
2523 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(bad, ends_with_ok),
2524 "Value of: bad\n"
2525 "Expected: ends with \"ok\"\n"
2526 " Actual: \"bad\"");
shiqiane35fdd92008-12-10 05:08:54 +00002527 Matcher<int> is_greater_than_5 = Gt(5);
2528 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5),
2529 "Value of: 5\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002530 "Expected: is > 5\n"
zhanyong.wan736baa82010-09-27 17:44:16 +00002531 " Actual: 5" + OfType("int"));
shiqiane35fdd92008-12-10 05:08:54 +00002532}
zhanyong.wan95b12332009-09-25 18:55:50 +00002533#endif // !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +00002534
2535// Tests floating-point matchers.
2536template <typename RawType>
2537class FloatingPointTest : public testing::Test {
2538 protected:
2539 typedef typename testing::internal::FloatingPoint<RawType> Floating;
2540 typedef typename Floating::Bits Bits;
2541
2542 virtual void SetUp() {
2543 const size_t max_ulps = Floating::kMaxUlps;
2544
2545 // The bits that represent 0.0.
2546 const Bits zero_bits = Floating(0).bits();
2547
2548 // Makes some numbers close to 0.0.
2549 close_to_positive_zero_ = Floating::ReinterpretBits(zero_bits + max_ulps/2);
2550 close_to_negative_zero_ = -Floating::ReinterpretBits(
2551 zero_bits + max_ulps - max_ulps/2);
2552 further_from_negative_zero_ = -Floating::ReinterpretBits(
2553 zero_bits + max_ulps + 1 - max_ulps/2);
2554
2555 // The bits that represent 1.0.
2556 const Bits one_bits = Floating(1).bits();
2557
2558 // Makes some numbers close to 1.0.
2559 close_to_one_ = Floating::ReinterpretBits(one_bits + max_ulps);
2560 further_from_one_ = Floating::ReinterpretBits(one_bits + max_ulps + 1);
2561
2562 // +infinity.
2563 infinity_ = Floating::Infinity();
2564
2565 // The bits that represent +infinity.
2566 const Bits infinity_bits = Floating(infinity_).bits();
2567
2568 // Makes some numbers close to infinity.
2569 close_to_infinity_ = Floating::ReinterpretBits(infinity_bits - max_ulps);
2570 further_from_infinity_ = Floating::ReinterpretBits(
2571 infinity_bits - max_ulps - 1);
2572
2573 // Makes some NAN's.
2574 nan1_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 1);
2575 nan2_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 200);
2576 }
2577
2578 void TestSize() {
2579 EXPECT_EQ(sizeof(RawType), sizeof(Bits));
2580 }
2581
2582 // A battery of tests for FloatingEqMatcher::Matches.
2583 // matcher_maker is a pointer to a function which creates a FloatingEqMatcher.
2584 void TestMatches(
2585 testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) {
2586 Matcher<RawType> m1 = matcher_maker(0.0);
2587 EXPECT_TRUE(m1.Matches(-0.0));
2588 EXPECT_TRUE(m1.Matches(close_to_positive_zero_));
2589 EXPECT_TRUE(m1.Matches(close_to_negative_zero_));
2590 EXPECT_FALSE(m1.Matches(1.0));
2591
2592 Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_);
2593 EXPECT_FALSE(m2.Matches(further_from_negative_zero_));
2594
2595 Matcher<RawType> m3 = matcher_maker(1.0);
2596 EXPECT_TRUE(m3.Matches(close_to_one_));
2597 EXPECT_FALSE(m3.Matches(further_from_one_));
2598
2599 // Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above.
2600 EXPECT_FALSE(m3.Matches(0.0));
2601
2602 Matcher<RawType> m4 = matcher_maker(-infinity_);
2603 EXPECT_TRUE(m4.Matches(-close_to_infinity_));
2604
2605 Matcher<RawType> m5 = matcher_maker(infinity_);
2606 EXPECT_TRUE(m5.Matches(close_to_infinity_));
2607
2608 // This is interesting as the representations of infinity_ and nan1_
2609 // are only 1 DLP apart.
2610 EXPECT_FALSE(m5.Matches(nan1_));
2611
2612 // matcher_maker can produce a Matcher<const RawType&>, which is needed in
2613 // some cases.
2614 Matcher<const RawType&> m6 = matcher_maker(0.0);
2615 EXPECT_TRUE(m6.Matches(-0.0));
2616 EXPECT_TRUE(m6.Matches(close_to_positive_zero_));
2617 EXPECT_FALSE(m6.Matches(1.0));
2618
2619 // matcher_maker can produce a Matcher<RawType&>, which is needed in some
2620 // cases.
2621 Matcher<RawType&> m7 = matcher_maker(0.0);
2622 RawType x = 0.0;
2623 EXPECT_TRUE(m7.Matches(x));
2624 x = 0.01f;
2625 EXPECT_FALSE(m7.Matches(x));
2626 }
2627
2628 // Pre-calculated numbers to be used by the tests.
2629
2630 static RawType close_to_positive_zero_;
2631 static RawType close_to_negative_zero_;
2632 static RawType further_from_negative_zero_;
2633
2634 static RawType close_to_one_;
2635 static RawType further_from_one_;
2636
2637 static RawType infinity_;
2638 static RawType close_to_infinity_;
2639 static RawType further_from_infinity_;
2640
2641 static RawType nan1_;
2642 static RawType nan2_;
2643};
2644
2645template <typename RawType>
2646RawType FloatingPointTest<RawType>::close_to_positive_zero_;
2647
2648template <typename RawType>
2649RawType FloatingPointTest<RawType>::close_to_negative_zero_;
2650
2651template <typename RawType>
2652RawType FloatingPointTest<RawType>::further_from_negative_zero_;
2653
2654template <typename RawType>
2655RawType FloatingPointTest<RawType>::close_to_one_;
2656
2657template <typename RawType>
2658RawType FloatingPointTest<RawType>::further_from_one_;
2659
2660template <typename RawType>
2661RawType FloatingPointTest<RawType>::infinity_;
2662
2663template <typename RawType>
2664RawType FloatingPointTest<RawType>::close_to_infinity_;
2665
2666template <typename RawType>
2667RawType FloatingPointTest<RawType>::further_from_infinity_;
2668
2669template <typename RawType>
2670RawType FloatingPointTest<RawType>::nan1_;
2671
2672template <typename RawType>
2673RawType FloatingPointTest<RawType>::nan2_;
2674
2675// Instantiate FloatingPointTest for testing floats.
2676typedef FloatingPointTest<float> FloatTest;
2677
2678TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) {
2679 TestMatches(&FloatEq);
2680}
2681
2682TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
2683 TestMatches(&NanSensitiveFloatEq);
2684}
2685
2686TEST_F(FloatTest, FloatEqCannotMatchNaN) {
2687 // FloatEq never matches NaN.
2688 Matcher<float> m = FloatEq(nan1_);
2689 EXPECT_FALSE(m.Matches(nan1_));
2690 EXPECT_FALSE(m.Matches(nan2_));
2691 EXPECT_FALSE(m.Matches(1.0));
2692}
2693
2694TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {
2695 // NanSensitiveFloatEq will match NaN.
2696 Matcher<float> m = NanSensitiveFloatEq(nan1_);
2697 EXPECT_TRUE(m.Matches(nan1_));
2698 EXPECT_TRUE(m.Matches(nan2_));
2699 EXPECT_FALSE(m.Matches(1.0));
2700}
2701
2702TEST_F(FloatTest, FloatEqCanDescribeSelf) {
2703 Matcher<float> m1 = FloatEq(2.0f);
2704 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002705 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002706
2707 Matcher<float> m2 = FloatEq(0.5f);
2708 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002709 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002710
2711 Matcher<float> m3 = FloatEq(nan1_);
2712 EXPECT_EQ("never matches", Describe(m3));
2713 EXPECT_EQ("is anything", DescribeNegation(m3));
2714}
2715
2716TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {
2717 Matcher<float> m1 = NanSensitiveFloatEq(2.0f);
2718 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002719 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002720
2721 Matcher<float> m2 = NanSensitiveFloatEq(0.5f);
2722 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002723 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002724
2725 Matcher<float> m3 = NanSensitiveFloatEq(nan1_);
2726 EXPECT_EQ("is NaN", Describe(m3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002727 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
shiqiane35fdd92008-12-10 05:08:54 +00002728}
2729
2730// Instantiate FloatingPointTest for testing doubles.
2731typedef FloatingPointTest<double> DoubleTest;
2732
2733TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
2734 TestMatches(&DoubleEq);
2735}
2736
2737TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
2738 TestMatches(&NanSensitiveDoubleEq);
2739}
2740
2741TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {
2742 // DoubleEq never matches NaN.
2743 Matcher<double> m = DoubleEq(nan1_);
2744 EXPECT_FALSE(m.Matches(nan1_));
2745 EXPECT_FALSE(m.Matches(nan2_));
2746 EXPECT_FALSE(m.Matches(1.0));
2747}
2748
2749TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {
2750 // NanSensitiveDoubleEq will match NaN.
2751 Matcher<double> m = NanSensitiveDoubleEq(nan1_);
2752 EXPECT_TRUE(m.Matches(nan1_));
2753 EXPECT_TRUE(m.Matches(nan2_));
2754 EXPECT_FALSE(m.Matches(1.0));
2755}
2756
2757TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {
2758 Matcher<double> m1 = DoubleEq(2.0);
2759 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002760 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002761
2762 Matcher<double> m2 = DoubleEq(0.5);
2763 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002764 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002765
2766 Matcher<double> m3 = DoubleEq(nan1_);
2767 EXPECT_EQ("never matches", Describe(m3));
2768 EXPECT_EQ("is anything", DescribeNegation(m3));
2769}
2770
2771TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {
2772 Matcher<double> m1 = NanSensitiveDoubleEq(2.0);
2773 EXPECT_EQ("is approximately 2", Describe(m1));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002774 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
shiqiane35fdd92008-12-10 05:08:54 +00002775
2776 Matcher<double> m2 = NanSensitiveDoubleEq(0.5);
2777 EXPECT_EQ("is approximately 0.5", Describe(m2));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002778 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
shiqiane35fdd92008-12-10 05:08:54 +00002779
2780 Matcher<double> m3 = NanSensitiveDoubleEq(nan1_);
2781 EXPECT_EQ("is NaN", Describe(m3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002782 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
shiqiane35fdd92008-12-10 05:08:54 +00002783}
2784
2785TEST(PointeeTest, RawPointer) {
2786 const Matcher<int*> m = Pointee(Ge(0));
2787
2788 int n = 1;
2789 EXPECT_TRUE(m.Matches(&n));
2790 n = -1;
2791 EXPECT_FALSE(m.Matches(&n));
2792 EXPECT_FALSE(m.Matches(NULL));
2793}
2794
2795TEST(PointeeTest, RawPointerToConst) {
2796 const Matcher<const double*> m = Pointee(Ge(0));
2797
2798 double x = 1;
2799 EXPECT_TRUE(m.Matches(&x));
2800 x = -1;
2801 EXPECT_FALSE(m.Matches(&x));
2802 EXPECT_FALSE(m.Matches(NULL));
2803}
2804
2805TEST(PointeeTest, ReferenceToConstRawPointer) {
2806 const Matcher<int* const &> m = Pointee(Ge(0));
2807
2808 int n = 1;
2809 EXPECT_TRUE(m.Matches(&n));
2810 n = -1;
2811 EXPECT_FALSE(m.Matches(&n));
2812 EXPECT_FALSE(m.Matches(NULL));
2813}
2814
2815TEST(PointeeTest, ReferenceToNonConstRawPointer) {
2816 const Matcher<double* &> m = Pointee(Ge(0));
2817
2818 double x = 1.0;
2819 double* p = &x;
2820 EXPECT_TRUE(m.Matches(p));
2821 x = -1;
2822 EXPECT_FALSE(m.Matches(p));
2823 p = NULL;
2824 EXPECT_FALSE(m.Matches(p));
2825}
2826
2827TEST(PointeeTest, NeverMatchesNull) {
2828 const Matcher<const char*> m = Pointee(_);
2829 EXPECT_FALSE(m.Matches(NULL));
2830}
2831
2832// Tests that we can write Pointee(value) instead of Pointee(Eq(value)).
2833TEST(PointeeTest, MatchesAgainstAValue) {
2834 const Matcher<int*> m = Pointee(5);
2835
2836 int n = 5;
2837 EXPECT_TRUE(m.Matches(&n));
2838 n = -1;
2839 EXPECT_FALSE(m.Matches(&n));
2840 EXPECT_FALSE(m.Matches(NULL));
2841}
2842
2843TEST(PointeeTest, CanDescribeSelf) {
2844 const Matcher<int*> m = Pointee(Gt(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002845 EXPECT_EQ("points to a value that is > 3", Describe(m));
2846 EXPECT_EQ("does not point to a value that is > 3",
shiqiane35fdd92008-12-10 05:08:54 +00002847 DescribeNegation(m));
2848}
2849
shiqiane35fdd92008-12-10 05:08:54 +00002850TEST(PointeeTest, CanExplainMatchResult) {
2851 const Matcher<const string*> m = Pointee(StartsWith("Hi"));
2852
2853 EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL)));
2854
zhanyong.wan736baa82010-09-27 17:44:16 +00002855 const Matcher<long*> m2 = Pointee(GreaterThan(1)); // NOLINT
2856 long n = 3; // NOLINT
2857 EXPECT_EQ("which points to 3" + OfType("long") + ", which is 2 more than 1",
zhanyong.wan676e8cc2010-03-16 20:01:51 +00002858 Explain(m2, &n));
2859}
2860
2861TEST(PointeeTest, AlwaysExplainsPointee) {
2862 const Matcher<int*> m = Pointee(0);
2863 int n = 42;
zhanyong.wan736baa82010-09-27 17:44:16 +00002864 EXPECT_EQ("which points to 42" + OfType("int"), Explain(m, &n));
shiqiane35fdd92008-12-10 05:08:54 +00002865}
2866
2867// An uncopyable class.
2868class Uncopyable {
2869 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002870 explicit Uncopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00002871
2872 int value() const { return value_; }
2873 private:
2874 const int value_;
2875 GTEST_DISALLOW_COPY_AND_ASSIGN_(Uncopyable);
2876};
2877
2878// Returns true iff x.value() is positive.
2879bool ValueIsPositive(const Uncopyable& x) { return x.value() > 0; }
2880
2881// A user-defined struct for testing Field().
2882struct AStruct {
2883 AStruct() : x(0), y(1.0), z(5), p(NULL) {}
2884 AStruct(const AStruct& rhs)
2885 : x(rhs.x), y(rhs.y), z(rhs.z.value()), p(rhs.p) {}
2886
2887 int x; // A non-const field.
2888 const double y; // A const field.
2889 Uncopyable z; // An uncopyable field.
2890 const char* p; // A pointer field.
zhanyong.wan32de5f52009-12-23 00:13:23 +00002891
2892 private:
2893 GTEST_DISALLOW_ASSIGN_(AStruct);
shiqiane35fdd92008-12-10 05:08:54 +00002894};
2895
2896// A derived struct for testing Field().
2897struct DerivedStruct : public AStruct {
2898 char ch;
zhanyong.wan32de5f52009-12-23 00:13:23 +00002899
2900 private:
2901 GTEST_DISALLOW_ASSIGN_(DerivedStruct);
shiqiane35fdd92008-12-10 05:08:54 +00002902};
2903
2904// Tests that Field(&Foo::field, ...) works when field is non-const.
2905TEST(FieldTest, WorksForNonConstField) {
2906 Matcher<AStruct> m = Field(&AStruct::x, Ge(0));
2907
2908 AStruct a;
2909 EXPECT_TRUE(m.Matches(a));
2910 a.x = -1;
2911 EXPECT_FALSE(m.Matches(a));
2912}
2913
2914// Tests that Field(&Foo::field, ...) works when field is const.
2915TEST(FieldTest, WorksForConstField) {
2916 AStruct a;
2917
2918 Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0));
2919 EXPECT_TRUE(m.Matches(a));
2920 m = Field(&AStruct::y, Le(0.0));
2921 EXPECT_FALSE(m.Matches(a));
2922}
2923
2924// Tests that Field(&Foo::field, ...) works when field is not copyable.
2925TEST(FieldTest, WorksForUncopyableField) {
2926 AStruct a;
2927
2928 Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive));
2929 EXPECT_TRUE(m.Matches(a));
2930 m = Field(&AStruct::z, Not(Truly(ValueIsPositive)));
2931 EXPECT_FALSE(m.Matches(a));
2932}
2933
2934// Tests that Field(&Foo::field, ...) works when field is a pointer.
2935TEST(FieldTest, WorksForPointerField) {
2936 // Matching against NULL.
2937 Matcher<AStruct> m = Field(&AStruct::p, static_cast<const char*>(NULL));
2938 AStruct a;
2939 EXPECT_TRUE(m.Matches(a));
2940 a.p = "hi";
2941 EXPECT_FALSE(m.Matches(a));
2942
2943 // Matching a pointer that is not NULL.
2944 m = Field(&AStruct::p, StartsWith("hi"));
2945 a.p = "hill";
2946 EXPECT_TRUE(m.Matches(a));
2947 a.p = "hole";
2948 EXPECT_FALSE(m.Matches(a));
2949}
2950
2951// Tests that Field() works when the object is passed by reference.
2952TEST(FieldTest, WorksForByRefArgument) {
2953 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2954
2955 AStruct a;
2956 EXPECT_TRUE(m.Matches(a));
2957 a.x = -1;
2958 EXPECT_FALSE(m.Matches(a));
2959}
2960
2961// Tests that Field(&Foo::field, ...) works when the argument's type
2962// is a sub-type of Foo.
2963TEST(FieldTest, WorksForArgumentOfSubType) {
2964 // Note that the matcher expects DerivedStruct but we say AStruct
2965 // inside Field().
2966 Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0));
2967
2968 DerivedStruct d;
2969 EXPECT_TRUE(m.Matches(d));
2970 d.x = -1;
2971 EXPECT_FALSE(m.Matches(d));
2972}
2973
2974// Tests that Field(&Foo::field, m) works when field's type and m's
2975// argument type are compatible but not the same.
2976TEST(FieldTest, WorksForCompatibleMatcherType) {
2977 // The field is an int, but the inner matcher expects a signed char.
2978 Matcher<const AStruct&> m = Field(&AStruct::x,
2979 Matcher<signed char>(Ge(0)));
2980
2981 AStruct a;
2982 EXPECT_TRUE(m.Matches(a));
2983 a.x = -1;
2984 EXPECT_FALSE(m.Matches(a));
2985}
2986
2987// Tests that Field() can describe itself.
2988TEST(FieldTest, CanDescribeSelf) {
2989 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2990
zhanyong.wanb1c7f932010-03-24 17:35:11 +00002991 EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
2992 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00002993}
2994
2995// Tests that Field() can explain the match result.
2996TEST(FieldTest, CanExplainMatchResult) {
2997 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2998
2999 AStruct a;
3000 a.x = 1;
zhanyong.wan736baa82010-09-27 17:44:16 +00003001 EXPECT_EQ("whose given field is 1" + OfType("int"), Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003002
3003 m = Field(&AStruct::x, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003004 EXPECT_EQ(
3005 "whose given field is 1" + OfType("int") + ", which is 1 more than 0",
3006 Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003007}
3008
3009// Tests that Field() works when the argument is a pointer to const.
3010TEST(FieldForPointerTest, WorksForPointerToConst) {
3011 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
3012
3013 AStruct a;
3014 EXPECT_TRUE(m.Matches(&a));
3015 a.x = -1;
3016 EXPECT_FALSE(m.Matches(&a));
3017}
3018
3019// Tests that Field() works when the argument is a pointer to non-const.
3020TEST(FieldForPointerTest, WorksForPointerToNonConst) {
3021 Matcher<AStruct*> m = Field(&AStruct::x, Ge(0));
3022
3023 AStruct a;
3024 EXPECT_TRUE(m.Matches(&a));
3025 a.x = -1;
3026 EXPECT_FALSE(m.Matches(&a));
3027}
3028
zhanyong.wan6953a722010-01-13 05:15:07 +00003029// Tests that Field() works when the argument is a reference to a const pointer.
3030TEST(FieldForPointerTest, WorksForReferenceToConstPointer) {
3031 Matcher<AStruct* const&> m = Field(&AStruct::x, Ge(0));
3032
3033 AStruct a;
3034 EXPECT_TRUE(m.Matches(&a));
3035 a.x = -1;
3036 EXPECT_FALSE(m.Matches(&a));
3037}
3038
shiqiane35fdd92008-12-10 05:08:54 +00003039// Tests that Field() does not match the NULL pointer.
3040TEST(FieldForPointerTest, DoesNotMatchNull) {
3041 Matcher<const AStruct*> m = Field(&AStruct::x, _);
3042 EXPECT_FALSE(m.Matches(NULL));
3043}
3044
3045// Tests that Field(&Foo::field, ...) works when the argument's type
3046// is a sub-type of const Foo*.
3047TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
3048 // Note that the matcher expects DerivedStruct but we say AStruct
3049 // inside Field().
3050 Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0));
3051
3052 DerivedStruct d;
3053 EXPECT_TRUE(m.Matches(&d));
3054 d.x = -1;
3055 EXPECT_FALSE(m.Matches(&d));
3056}
3057
3058// Tests that Field() can describe itself when used to match a pointer.
3059TEST(FieldForPointerTest, CanDescribeSelf) {
3060 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
3061
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003062 EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
3063 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003064}
3065
3066// Tests that Field() can explain the result of matching a pointer.
3067TEST(FieldForPointerTest, CanExplainMatchResult) {
3068 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
3069
3070 AStruct a;
3071 a.x = 1;
3072 EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(NULL)));
zhanyong.wan736baa82010-09-27 17:44:16 +00003073 EXPECT_EQ("which points to an object whose given field is 1" + OfType("int"),
3074 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003075
3076 m = Field(&AStruct::x, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003077 EXPECT_EQ("which points to an object whose given field is 1" + OfType("int") +
3078 ", which is 1 more than 0", Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003079}
3080
3081// A user-defined class for testing Property().
3082class AClass {
3083 public:
3084 AClass() : n_(0) {}
3085
3086 // A getter that returns a non-reference.
3087 int n() const { return n_; }
3088
3089 void set_n(int new_n) { n_ = new_n; }
3090
3091 // A getter that returns a reference to const.
3092 const string& s() const { return s_; }
3093
3094 void set_s(const string& new_s) { s_ = new_s; }
3095
3096 // A getter that returns a reference to non-const.
3097 double& x() const { return x_; }
3098 private:
3099 int n_;
3100 string s_;
3101
3102 static double x_;
3103};
3104
3105double AClass::x_ = 0.0;
3106
3107// A derived class for testing Property().
3108class DerivedClass : public AClass {
3109 private:
3110 int k_;
3111};
3112
3113// Tests that Property(&Foo::property, ...) works when property()
3114// returns a non-reference.
3115TEST(PropertyTest, WorksForNonReferenceProperty) {
3116 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3117
3118 AClass a;
3119 a.set_n(1);
3120 EXPECT_TRUE(m.Matches(a));
3121
3122 a.set_n(-1);
3123 EXPECT_FALSE(m.Matches(a));
3124}
3125
3126// Tests that Property(&Foo::property, ...) works when property()
3127// returns a reference to const.
3128TEST(PropertyTest, WorksForReferenceToConstProperty) {
3129 Matcher<const AClass&> m = Property(&AClass::s, StartsWith("hi"));
3130
3131 AClass a;
3132 a.set_s("hill");
3133 EXPECT_TRUE(m.Matches(a));
3134
3135 a.set_s("hole");
3136 EXPECT_FALSE(m.Matches(a));
3137}
3138
3139// Tests that Property(&Foo::property, ...) works when property()
3140// returns a reference to non-const.
3141TEST(PropertyTest, WorksForReferenceToNonConstProperty) {
3142 double x = 0.0;
3143 AClass a;
3144
3145 Matcher<const AClass&> m = Property(&AClass::x, Ref(x));
3146 EXPECT_FALSE(m.Matches(a));
3147
3148 m = Property(&AClass::x, Not(Ref(x)));
3149 EXPECT_TRUE(m.Matches(a));
3150}
3151
3152// Tests that Property(&Foo::property, ...) works when the argument is
3153// passed by value.
3154TEST(PropertyTest, WorksForByValueArgument) {
3155 Matcher<AClass> m = Property(&AClass::s, StartsWith("hi"));
3156
3157 AClass a;
3158 a.set_s("hill");
3159 EXPECT_TRUE(m.Matches(a));
3160
3161 a.set_s("hole");
3162 EXPECT_FALSE(m.Matches(a));
3163}
3164
3165// Tests that Property(&Foo::property, ...) works when the argument's
3166// type is a sub-type of Foo.
3167TEST(PropertyTest, WorksForArgumentOfSubType) {
3168 // The matcher expects a DerivedClass, but inside the Property() we
3169 // say AClass.
3170 Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0));
3171
3172 DerivedClass d;
3173 d.set_n(1);
3174 EXPECT_TRUE(m.Matches(d));
3175
3176 d.set_n(-1);
3177 EXPECT_FALSE(m.Matches(d));
3178}
3179
3180// Tests that Property(&Foo::property, m) works when property()'s type
3181// and m's argument type are compatible but different.
3182TEST(PropertyTest, WorksForCompatibleMatcherType) {
3183 // n() returns an int but the inner matcher expects a signed char.
3184 Matcher<const AClass&> m = Property(&AClass::n,
3185 Matcher<signed char>(Ge(0)));
3186
3187 AClass a;
3188 EXPECT_TRUE(m.Matches(a));
3189 a.set_n(-1);
3190 EXPECT_FALSE(m.Matches(a));
3191}
3192
3193// Tests that Property() can describe itself.
3194TEST(PropertyTest, CanDescribeSelf) {
3195 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3196
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003197 EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
3198 EXPECT_EQ("is an object whose given property isn't >= 0",
3199 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003200}
3201
3202// Tests that Property() can explain the match result.
3203TEST(PropertyTest, CanExplainMatchResult) {
3204 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
3205
3206 AClass a;
3207 a.set_n(1);
zhanyong.wan736baa82010-09-27 17:44:16 +00003208 EXPECT_EQ("whose given property is 1" + OfType("int"), Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003209
3210 m = Property(&AClass::n, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003211 EXPECT_EQ(
3212 "whose given property is 1" + OfType("int") + ", which is 1 more than 0",
3213 Explain(m, a));
shiqiane35fdd92008-12-10 05:08:54 +00003214}
3215
3216// Tests that Property() works when the argument is a pointer to const.
3217TEST(PropertyForPointerTest, WorksForPointerToConst) {
3218 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3219
3220 AClass a;
3221 a.set_n(1);
3222 EXPECT_TRUE(m.Matches(&a));
3223
3224 a.set_n(-1);
3225 EXPECT_FALSE(m.Matches(&a));
3226}
3227
3228// Tests that Property() works when the argument is a pointer to non-const.
3229TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
3230 Matcher<AClass*> m = Property(&AClass::s, StartsWith("hi"));
3231
3232 AClass a;
3233 a.set_s("hill");
3234 EXPECT_TRUE(m.Matches(&a));
3235
3236 a.set_s("hole");
3237 EXPECT_FALSE(m.Matches(&a));
3238}
3239
zhanyong.wan6953a722010-01-13 05:15:07 +00003240// Tests that Property() works when the argument is a reference to a
3241// const pointer.
3242TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) {
3243 Matcher<AClass* const&> m = Property(&AClass::s, StartsWith("hi"));
3244
3245 AClass a;
3246 a.set_s("hill");
3247 EXPECT_TRUE(m.Matches(&a));
3248
3249 a.set_s("hole");
3250 EXPECT_FALSE(m.Matches(&a));
3251}
3252
shiqiane35fdd92008-12-10 05:08:54 +00003253// Tests that Property() does not match the NULL pointer.
3254TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
3255 Matcher<const AClass*> m = Property(&AClass::x, _);
3256 EXPECT_FALSE(m.Matches(NULL));
3257}
3258
3259// Tests that Property(&Foo::property, ...) works when the argument's
3260// type is a sub-type of const Foo*.
3261TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
3262 // The matcher expects a DerivedClass, but inside the Property() we
3263 // say AClass.
3264 Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0));
3265
3266 DerivedClass d;
3267 d.set_n(1);
3268 EXPECT_TRUE(m.Matches(&d));
3269
3270 d.set_n(-1);
3271 EXPECT_FALSE(m.Matches(&d));
3272}
3273
3274// Tests that Property() can describe itself when used to match a pointer.
3275TEST(PropertyForPointerTest, CanDescribeSelf) {
3276 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3277
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003278 EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
3279 EXPECT_EQ("is an object whose given property isn't >= 0",
3280 DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +00003281}
3282
3283// Tests that Property() can explain the result of matching a pointer.
3284TEST(PropertyForPointerTest, CanExplainMatchResult) {
3285 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
3286
3287 AClass a;
3288 a.set_n(1);
3289 EXPECT_EQ("", Explain(m, static_cast<const AClass*>(NULL)));
zhanyong.wan736baa82010-09-27 17:44:16 +00003290 EXPECT_EQ(
3291 "which points to an object whose given property is 1" + OfType("int"),
3292 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003293
3294 m = Property(&AClass::n, GreaterThan(0));
zhanyong.wan736baa82010-09-27 17:44:16 +00003295 EXPECT_EQ("which points to an object whose given property is 1" +
3296 OfType("int") + ", which is 1 more than 0",
3297 Explain(m, &a));
shiqiane35fdd92008-12-10 05:08:54 +00003298}
3299
3300// Tests ResultOf.
3301
3302// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3303// function pointer.
3304string IntToStringFunction(int input) { return input == 1 ? "foo" : "bar"; }
3305
3306TEST(ResultOfTest, WorksForFunctionPointers) {
3307 Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(string("foo")));
3308
3309 EXPECT_TRUE(matcher.Matches(1));
3310 EXPECT_FALSE(matcher.Matches(2));
3311}
3312
3313// Tests that ResultOf() can describe itself.
3314TEST(ResultOfTest, CanDescribeItself) {
3315 Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo"));
3316
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003317 EXPECT_EQ("is mapped by the given callable to a value that "
3318 "is equal to \"foo\"", Describe(matcher));
3319 EXPECT_EQ("is mapped by the given callable to a value that "
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003320 "isn't equal to \"foo\"", DescribeNegation(matcher));
shiqiane35fdd92008-12-10 05:08:54 +00003321}
3322
3323// Tests that ResultOf() can explain the match result.
3324int IntFunction(int input) { return input == 42 ? 80 : 90; }
3325
3326TEST(ResultOfTest, CanExplainMatchResult) {
3327 Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
zhanyong.wan736baa82010-09-27 17:44:16 +00003328 EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int"),
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003329 Explain(matcher, 36));
shiqiane35fdd92008-12-10 05:08:54 +00003330
3331 matcher = ResultOf(&IntFunction, GreaterThan(85));
zhanyong.wan736baa82010-09-27 17:44:16 +00003332 EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int") +
3333 ", which is 5 more than 85", Explain(matcher, 36));
shiqiane35fdd92008-12-10 05:08:54 +00003334}
3335
3336// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3337// returns a non-reference.
3338TEST(ResultOfTest, WorksForNonReferenceResults) {
3339 Matcher<int> matcher = ResultOf(&IntFunction, Eq(80));
3340
3341 EXPECT_TRUE(matcher.Matches(42));
3342 EXPECT_FALSE(matcher.Matches(36));
3343}
3344
3345// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3346// returns a reference to non-const.
zhanyong.wan736baa82010-09-27 17:44:16 +00003347double& DoubleFunction(double& input) { return input; } // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00003348
zhanyong.wan736baa82010-09-27 17:44:16 +00003349Uncopyable& RefUncopyableFunction(Uncopyable& obj) { // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00003350 return obj;
3351}
3352
3353TEST(ResultOfTest, WorksForReferenceToNonConstResults) {
3354 double x = 3.14;
3355 double x2 = x;
3356 Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x));
3357
3358 EXPECT_TRUE(matcher.Matches(x));
3359 EXPECT_FALSE(matcher.Matches(x2));
3360
3361 // Test that ResultOf works with uncopyable objects
3362 Uncopyable obj(0);
3363 Uncopyable obj2(0);
3364 Matcher<Uncopyable&> matcher2 =
3365 ResultOf(&RefUncopyableFunction, Ref(obj));
3366
3367 EXPECT_TRUE(matcher2.Matches(obj));
3368 EXPECT_FALSE(matcher2.Matches(obj2));
3369}
3370
3371// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
3372// returns a reference to const.
3373const string& StringFunction(const string& input) { return input; }
3374
3375TEST(ResultOfTest, WorksForReferenceToConstResults) {
3376 string s = "foo";
3377 string s2 = s;
3378 Matcher<const string&> matcher = ResultOf(&StringFunction, Ref(s));
3379
3380 EXPECT_TRUE(matcher.Matches(s));
3381 EXPECT_FALSE(matcher.Matches(s2));
3382}
3383
3384// Tests that ResultOf(f, m) works when f(x) and m's
3385// argument types are compatible but different.
3386TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {
3387 // IntFunction() returns int but the inner matcher expects a signed char.
3388 Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85)));
3389
3390 EXPECT_TRUE(matcher.Matches(36));
3391 EXPECT_FALSE(matcher.Matches(42));
3392}
3393
shiqiane35fdd92008-12-10 05:08:54 +00003394// Tests that the program aborts when ResultOf is passed
3395// a NULL function pointer.
3396TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +00003397 EXPECT_DEATH_IF_SUPPORTED(
zhanyong.wan736baa82010-09-27 17:44:16 +00003398 ResultOf(static_cast<string(*)(int dummy)>(NULL), Eq(string("foo"))),
shiqiane35fdd92008-12-10 05:08:54 +00003399 "NULL function pointer is passed into ResultOf\\(\\)\\.");
3400}
shiqiane35fdd92008-12-10 05:08:54 +00003401
3402// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3403// function reference.
3404TEST(ResultOfTest, WorksForFunctionReferences) {
3405 Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq("foo"));
3406 EXPECT_TRUE(matcher.Matches(1));
3407 EXPECT_FALSE(matcher.Matches(2));
3408}
3409
3410// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3411// function object.
3412struct Functor : public ::std::unary_function<int, string> {
3413 result_type operator()(argument_type input) const {
3414 return IntToStringFunction(input);
3415 }
3416};
3417
3418TEST(ResultOfTest, WorksForFunctors) {
3419 Matcher<int> matcher = ResultOf(Functor(), Eq(string("foo")));
3420
3421 EXPECT_TRUE(matcher.Matches(1));
3422 EXPECT_FALSE(matcher.Matches(2));
3423}
3424
3425// Tests that ResultOf(f, ...) compiles and works as expected when f is a
3426// functor with more then one operator() defined. ResultOf() must work
3427// for each defined operator().
3428struct PolymorphicFunctor {
3429 typedef int result_type;
3430 int operator()(int n) { return n; }
3431 int operator()(const char* s) { return static_cast<int>(strlen(s)); }
3432};
3433
3434TEST(ResultOfTest, WorksForPolymorphicFunctors) {
3435 Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5));
3436
3437 EXPECT_TRUE(matcher_int.Matches(10));
3438 EXPECT_FALSE(matcher_int.Matches(2));
3439
3440 Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5));
3441
3442 EXPECT_TRUE(matcher_string.Matches("long string"));
3443 EXPECT_FALSE(matcher_string.Matches("shrt"));
3444}
3445
3446const int* ReferencingFunction(const int& n) { return &n; }
3447
3448struct ReferencingFunctor {
3449 typedef const int* result_type;
3450 result_type operator()(const int& n) { return &n; }
3451};
3452
3453TEST(ResultOfTest, WorksForReferencingCallables) {
3454 const int n = 1;
3455 const int n2 = 1;
3456 Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n));
3457 EXPECT_TRUE(matcher2.Matches(n));
3458 EXPECT_FALSE(matcher2.Matches(n2));
3459
3460 Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n));
3461 EXPECT_TRUE(matcher3.Matches(n));
3462 EXPECT_FALSE(matcher3.Matches(n2));
3463}
3464
shiqiane35fdd92008-12-10 05:08:54 +00003465class DivisibleByImpl {
3466 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003467 explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {}
shiqiane35fdd92008-12-10 05:08:54 +00003468
zhanyong.wandb22c222010-01-28 21:52:29 +00003469 // For testing using ExplainMatchResultTo() with polymorphic matchers.
shiqiane35fdd92008-12-10 05:08:54 +00003470 template <typename T>
zhanyong.wandb22c222010-01-28 21:52:29 +00003471 bool MatchAndExplain(const T& n, MatchResultListener* listener) const {
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003472 *listener << "which is " << (n % divider_) << " modulo "
zhanyong.wandb22c222010-01-28 21:52:29 +00003473 << divider_;
shiqiane35fdd92008-12-10 05:08:54 +00003474 return (n % divider_) == 0;
3475 }
3476
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003477 void DescribeTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +00003478 *os << "is divisible by " << divider_;
3479 }
3480
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003481 void DescribeNegationTo(ostream* os) const {
shiqiane35fdd92008-12-10 05:08:54 +00003482 *os << "is not divisible by " << divider_;
3483 }
3484
zhanyong.wan32de5f52009-12-23 00:13:23 +00003485 void set_divider(int a_divider) { divider_ = a_divider; }
shiqiane35fdd92008-12-10 05:08:54 +00003486 int divider() const { return divider_; }
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003487
shiqiane35fdd92008-12-10 05:08:54 +00003488 private:
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003489 int divider_;
shiqiane35fdd92008-12-10 05:08:54 +00003490};
3491
shiqiane35fdd92008-12-10 05:08:54 +00003492PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) {
3493 return MakePolymorphicMatcher(DivisibleByImpl(n));
3494}
3495
3496// Tests that when AllOf() fails, only the first failing matcher is
3497// asked to explain why.
3498TEST(ExplainMatchResultTest, AllOf_False_False) {
3499 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003500 EXPECT_EQ("which is 1 modulo 4", Explain(m, 5));
shiqiane35fdd92008-12-10 05:08:54 +00003501}
3502
3503// Tests that when AllOf() fails, only the first failing matcher is
3504// asked to explain why.
3505TEST(ExplainMatchResultTest, AllOf_False_True) {
3506 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003507 EXPECT_EQ("which is 2 modulo 4", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003508}
3509
3510// Tests that when AllOf() fails, only the first failing matcher is
3511// asked to explain why.
3512TEST(ExplainMatchResultTest, AllOf_True_False) {
3513 const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003514 EXPECT_EQ("which is 2 modulo 3", Explain(m, 5));
shiqiane35fdd92008-12-10 05:08:54 +00003515}
3516
3517// Tests that when AllOf() succeeds, all matchers are asked to explain
3518// why.
3519TEST(ExplainMatchResultTest, AllOf_True_True) {
3520 const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003521 EXPECT_EQ("which is 0 modulo 2, and which is 0 modulo 3", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003522}
3523
3524TEST(ExplainMatchResultTest, AllOf_True_True_2) {
3525 const Matcher<int> m = AllOf(Ge(2), Le(3));
3526 EXPECT_EQ("", Explain(m, 2));
3527}
3528
3529TEST(ExplainmatcherResultTest, MonomorphicMatcher) {
3530 const Matcher<int> m = GreaterThan(5);
zhanyong.wan676e8cc2010-03-16 20:01:51 +00003531 EXPECT_EQ("which is 1 more than 5", Explain(m, 6));
shiqiane35fdd92008-12-10 05:08:54 +00003532}
3533
3534// The following two tests verify that values without a public copy
3535// ctor can be used as arguments to matchers like Eq(), Ge(), and etc
3536// with the help of ByRef().
3537
3538class NotCopyable {
3539 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00003540 explicit NotCopyable(int a_value) : value_(a_value) {}
shiqiane35fdd92008-12-10 05:08:54 +00003541
3542 int value() const { return value_; }
3543
3544 bool operator==(const NotCopyable& rhs) const {
3545 return value() == rhs.value();
3546 }
3547
3548 bool operator>=(const NotCopyable& rhs) const {
3549 return value() >= rhs.value();
3550 }
3551 private:
3552 int value_;
3553
3554 GTEST_DISALLOW_COPY_AND_ASSIGN_(NotCopyable);
3555};
3556
3557TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) {
3558 const NotCopyable const_value1(1);
3559 const Matcher<const NotCopyable&> m = Eq(ByRef(const_value1));
3560
3561 const NotCopyable n1(1), n2(2);
3562 EXPECT_TRUE(m.Matches(n1));
3563 EXPECT_FALSE(m.Matches(n2));
3564}
3565
3566TEST(ByRefTest, AllowsNotCopyableValueInMatchers) {
3567 NotCopyable value2(2);
3568 const Matcher<NotCopyable&> m = Ge(ByRef(value2));
3569
3570 NotCopyable n1(1), n2(2);
3571 EXPECT_FALSE(m.Matches(n1));
3572 EXPECT_TRUE(m.Matches(n2));
3573}
3574
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003575#if GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003576// Tests ContainerEq with different container types, and
3577// different element types.
3578
3579template <typename T>
zhanyong.wanb8243162009-06-04 05:48:20 +00003580class ContainerEqTest : public testing::Test {};
zhanyong.wan6a896b52009-01-16 01:13:50 +00003581
3582typedef testing::Types<
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003583 set<int>,
3584 vector<size_t>,
3585 multiset<size_t>,
3586 list<int> >
zhanyong.wan6a896b52009-01-16 01:13:50 +00003587 ContainerEqTestTypes;
3588
3589TYPED_TEST_CASE(ContainerEqTest, ContainerEqTestTypes);
3590
3591// Tests that the filled container is equal to itself.
3592TYPED_TEST(ContainerEqTest, EqualsSelf) {
3593 static const int vals[] = {1, 1, 2, 3, 5, 8};
3594 TypeParam my_set(vals, vals + 6);
3595 const Matcher<TypeParam> m = ContainerEq(my_set);
3596 EXPECT_TRUE(m.Matches(my_set));
3597 EXPECT_EQ("", Explain(m, my_set));
3598}
3599
3600// Tests that missing values are reported.
3601TYPED_TEST(ContainerEqTest, ValueMissing) {
3602 static const int vals[] = {1, 1, 2, 3, 5, 8};
3603 static const int test_vals[] = {2, 1, 8, 5};
3604 TypeParam my_set(vals, vals + 6);
3605 TypeParam test_set(test_vals, test_vals + 4);
3606 const Matcher<TypeParam> m = ContainerEq(my_set);
3607 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003608 EXPECT_EQ("which doesn't have these expected elements: 3",
3609 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003610}
3611
3612// Tests that added values are reported.
3613TYPED_TEST(ContainerEqTest, ValueAdded) {
3614 static const int vals[] = {1, 1, 2, 3, 5, 8};
3615 static const int test_vals[] = {1, 2, 3, 5, 8, 46};
3616 TypeParam my_set(vals, vals + 6);
3617 TypeParam test_set(test_vals, test_vals + 6);
3618 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3619 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003620 EXPECT_EQ("which has these unexpected elements: 46", Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003621}
3622
3623// Tests that added and missing values are reported together.
3624TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) {
3625 static const int vals[] = {1, 1, 2, 3, 5, 8};
3626 static const int test_vals[] = {1, 2, 3, 8, 46};
3627 TypeParam my_set(vals, vals + 6);
3628 TypeParam test_set(test_vals, test_vals + 5);
3629 const Matcher<TypeParam> m = ContainerEq(my_set);
3630 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003631 EXPECT_EQ("which has these unexpected elements: 46,\n"
3632 "and doesn't have these expected elements: 5",
3633 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003634}
3635
3636// Tests duplicated value -- expect no explanation.
3637TYPED_TEST(ContainerEqTest, DuplicateDifference) {
3638 static const int vals[] = {1, 1, 2, 3, 5, 8};
3639 static const int test_vals[] = {1, 2, 3, 5, 8};
3640 TypeParam my_set(vals, vals + 6);
3641 TypeParam test_set(test_vals, test_vals + 5);
3642 const Matcher<const TypeParam&> m = ContainerEq(my_set);
3643 // Depending on the container, match may be true or false
3644 // But in any case there should be no explanation.
3645 EXPECT_EQ("", Explain(m, test_set));
3646}
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003647#endif // GTEST_HAS_TYPED_TEST
zhanyong.wan6a896b52009-01-16 01:13:50 +00003648
3649// Tests that mutliple missing values are reported.
3650// Using just vector here, so order is predicatble.
3651TEST(ContainerEqExtraTest, MultipleValuesMissing) {
3652 static const int vals[] = {1, 1, 2, 3, 5, 8};
3653 static const int test_vals[] = {2, 1, 5};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003654 vector<int> my_set(vals, vals + 6);
3655 vector<int> test_set(test_vals, test_vals + 3);
3656 const Matcher<vector<int> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003657 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003658 EXPECT_EQ("which doesn't have these expected elements: 3, 8",
3659 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003660}
3661
3662// Tests that added values are reported.
3663// Using just vector here, so order is predicatble.
3664TEST(ContainerEqExtraTest, MultipleValuesAdded) {
3665 static const int vals[] = {1, 1, 2, 3, 5, 8};
3666 static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003667 list<size_t> my_set(vals, vals + 6);
3668 list<size_t> test_set(test_vals, test_vals + 7);
3669 const Matcher<const list<size_t>&> m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003670 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003671 EXPECT_EQ("which has these unexpected elements: 92, 46",
3672 Explain(m, test_set));
zhanyong.wan6a896b52009-01-16 01:13:50 +00003673}
3674
3675// Tests that added and missing values are reported together.
3676TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) {
3677 static const int vals[] = {1, 1, 2, 3, 5, 8};
3678 static const int test_vals[] = {1, 2, 3, 92, 46};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003679 list<size_t> my_set(vals, vals + 6);
3680 list<size_t> test_set(test_vals, test_vals + 5);
3681 const Matcher<const list<size_t> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003682 EXPECT_FALSE(m.Matches(test_set));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003683 EXPECT_EQ("which has these unexpected elements: 92, 46,\n"
3684 "and doesn't have these expected elements: 5, 8",
zhanyong.wan6a896b52009-01-16 01:13:50 +00003685 Explain(m, test_set));
3686}
3687
3688// Tests to see that duplicate elements are detected,
3689// but (as above) not reported in the explanation.
3690TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) {
3691 static const int vals[] = {1, 1, 2, 3, 5, 8};
3692 static const int test_vals[] = {1, 2, 3, 5, 8};
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003693 vector<int> my_set(vals, vals + 6);
3694 vector<int> test_set(test_vals, test_vals + 5);
3695 const Matcher<vector<int> > m = ContainerEq(my_set);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003696 EXPECT_TRUE(m.Matches(my_set));
3697 EXPECT_FALSE(m.Matches(test_set));
3698 // There is nothing to report when both sets contain all the same values.
3699 EXPECT_EQ("", Explain(m, test_set));
3700}
3701
3702// Tests that ContainerEq works for non-trivial associative containers,
3703// like maps.
3704TEST(ContainerEqExtraTest, WorksForMaps) {
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003705 map<int, std::string> my_map;
zhanyong.wan6a896b52009-01-16 01:13:50 +00003706 my_map[0] = "a";
3707 my_map[1] = "b";
3708
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003709 map<int, std::string> test_map;
zhanyong.wan6a896b52009-01-16 01:13:50 +00003710 test_map[0] = "aa";
3711 test_map[1] = "b";
3712
zhanyong.wanab5b77c2010-05-17 19:32:48 +00003713 const Matcher<const map<int, std::string>&> m = ContainerEq(my_map);
zhanyong.wan6a896b52009-01-16 01:13:50 +00003714 EXPECT_TRUE(m.Matches(my_map));
3715 EXPECT_FALSE(m.Matches(test_map));
3716
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003717 EXPECT_EQ("which has these unexpected elements: (0, \"aa\"),\n"
3718 "and doesn't have these expected elements: (0, \"a\")",
zhanyong.wan6a896b52009-01-16 01:13:50 +00003719 Explain(m, test_map));
3720}
3721
zhanyong.wanb8243162009-06-04 05:48:20 +00003722TEST(ContainerEqExtraTest, WorksForNativeArray) {
3723 int a1[] = { 1, 2, 3 };
3724 int a2[] = { 1, 2, 3 };
3725 int b[] = { 1, 2, 4 };
3726
3727 EXPECT_THAT(a1, ContainerEq(a2));
3728 EXPECT_THAT(a1, Not(ContainerEq(b)));
3729}
3730
3731TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) {
3732 const char a1[][3] = { "hi", "lo" };
3733 const char a2[][3] = { "hi", "lo" };
3734 const char b[][3] = { "lo", "hi" };
3735
3736 // Tests using ContainerEq() in the first dimension.
3737 EXPECT_THAT(a1, ContainerEq(a2));
3738 EXPECT_THAT(a1, Not(ContainerEq(b)));
3739
3740 // Tests using ContainerEq() in the second dimension.
3741 EXPECT_THAT(a1, ElementsAre(ContainerEq(a2[0]), ContainerEq(a2[1])));
3742 EXPECT_THAT(a1, ElementsAre(Not(ContainerEq(b[0])), ContainerEq(a2[1])));
3743}
3744
3745TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) {
3746 const int a1[] = { 1, 2, 3 };
3747 const int a2[] = { 1, 2, 3 };
3748 const int b[] = { 1, 2, 3, 4 };
3749
zhanyong.wan2661c682009-06-09 05:42:12 +00003750 const int* const p1 = a1;
3751 EXPECT_THAT(make_tuple(p1, 3), ContainerEq(a2));
3752 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(b)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003753
3754 const int c[] = { 1, 3, 2 };
zhanyong.wan2661c682009-06-09 05:42:12 +00003755 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(c)));
zhanyong.wanb8243162009-06-04 05:48:20 +00003756}
3757
3758TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) {
3759 std::string a1[][3] = {
3760 { "hi", "hello", "ciao" },
3761 { "bye", "see you", "ciao" }
3762 };
3763
3764 std::string a2[][3] = {
3765 { "hi", "hello", "ciao" },
3766 { "bye", "see you", "ciao" }
3767 };
3768
3769 const Matcher<const std::string(&)[2][3]> m = ContainerEq(a2);
3770 EXPECT_THAT(a1, m);
3771
3772 a2[0][0] = "ha";
3773 EXPECT_THAT(a1, m);
3774}
3775
zhanyong.wan898725c2011-09-16 16:45:39 +00003776TEST(WhenSortedByTest, WorksForEmptyContainer) {
3777 const vector<int> numbers;
3778 EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre()));
3779 EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1))));
3780}
3781
3782TEST(WhenSortedByTest, WorksForNonEmptyContainer) {
3783 vector<unsigned> numbers;
3784 numbers.push_back(3);
3785 numbers.push_back(1);
3786 numbers.push_back(2);
3787 numbers.push_back(2);
3788 EXPECT_THAT(numbers, WhenSortedBy(greater<unsigned>(),
3789 ElementsAre(3, 2, 2, 1)));
3790 EXPECT_THAT(numbers, Not(WhenSortedBy(greater<unsigned>(),
3791 ElementsAre(1, 2, 2, 3))));
3792}
3793
3794TEST(WhenSortedByTest, WorksForNonVectorContainer) {
3795 list<string> words;
3796 words.push_back("say");
3797 words.push_back("hello");
3798 words.push_back("world");
3799 EXPECT_THAT(words, WhenSortedBy(less<string>(),
3800 ElementsAre("hello", "say", "world")));
3801 EXPECT_THAT(words, Not(WhenSortedBy(less<string>(),
3802 ElementsAre("say", "hello", "world"))));
3803}
3804
3805TEST(WhenSortedByTest, WorksForNativeArray) {
3806 const int numbers[] = { 1, 3, 2, 4 };
3807 const int sorted_numbers[] = { 1, 2, 3, 4 };
3808 EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre(1, 2, 3, 4)));
3809 EXPECT_THAT(numbers, WhenSortedBy(less<int>(),
3810 ElementsAreArray(sorted_numbers)));
3811 EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1, 3, 2, 4))));
3812}
3813
3814TEST(WhenSortedByTest, CanDescribeSelf) {
3815 const Matcher<vector<int> > m = WhenSortedBy(less<int>(), ElementsAre(1, 2));
3816 EXPECT_EQ("(when sorted) has 2 elements where\n"
3817 "element #0 is equal to 1,\n"
3818 "element #1 is equal to 2",
3819 Describe(m));
3820 EXPECT_EQ("(when sorted) doesn't have 2 elements, or\n"
3821 "element #0 isn't equal to 1, or\n"
3822 "element #1 isn't equal to 2",
3823 DescribeNegation(m));
3824}
3825
3826TEST(WhenSortedByTest, ExplainsMatchResult) {
3827 const int a[] = { 2, 1 };
3828 EXPECT_EQ("which is { 1, 2 } when sorted, whose element #0 doesn't match",
3829 Explain(WhenSortedBy(less<int>(), ElementsAre(2, 3)), a));
3830 EXPECT_EQ("which is { 1, 2 } when sorted",
3831 Explain(WhenSortedBy(less<int>(), ElementsAre(1, 2)), a));
3832}
3833
3834// WhenSorted() is a simple wrapper on WhenSortedBy(). Hence we don't
3835// need to test it as exhaustively as we test the latter.
3836
3837TEST(WhenSortedTest, WorksForEmptyContainer) {
3838 const vector<int> numbers;
3839 EXPECT_THAT(numbers, WhenSorted(ElementsAre()));
3840 EXPECT_THAT(numbers, Not(WhenSorted(ElementsAre(1))));
3841}
3842
3843TEST(WhenSortedTest, WorksForNonEmptyContainer) {
3844 list<string> words;
3845 words.push_back("3");
3846 words.push_back("1");
3847 words.push_back("2");
3848 words.push_back("2");
3849 EXPECT_THAT(words, WhenSorted(ElementsAre("1", "2", "2", "3")));
3850 EXPECT_THAT(words, Not(WhenSorted(ElementsAre("3", "1", "2", "2"))));
3851}
3852
zhanyong.wan736baa82010-09-27 17:44:16 +00003853// Tests IsReadableTypeName().
3854
3855TEST(IsReadableTypeNameTest, ReturnsTrueForShortNames) {
3856 EXPECT_TRUE(IsReadableTypeName("int"));
3857 EXPECT_TRUE(IsReadableTypeName("const unsigned char*"));
3858 EXPECT_TRUE(IsReadableTypeName("MyMap<int, void*>"));
3859 EXPECT_TRUE(IsReadableTypeName("void (*)(int, bool)"));
3860}
3861
3862TEST(IsReadableTypeNameTest, ReturnsTrueForLongNonTemplateNonFunctionNames) {
3863 EXPECT_TRUE(IsReadableTypeName("my_long_namespace::MyClassName"));
3864 EXPECT_TRUE(IsReadableTypeName("int [5][6][7][8][9][10][11]"));
3865 EXPECT_TRUE(IsReadableTypeName("my_namespace::MyOuterClass::MyInnerClass"));
3866}
3867
3868TEST(IsReadableTypeNameTest, ReturnsFalseForLongTemplateNames) {
3869 EXPECT_FALSE(
3870 IsReadableTypeName("basic_string<char, std::char_traits<char> >"));
3871 EXPECT_FALSE(IsReadableTypeName("std::vector<int, std::alloc_traits<int> >"));
3872}
3873
3874TEST(IsReadableTypeNameTest, ReturnsFalseForLongFunctionTypeNames) {
3875 EXPECT_FALSE(IsReadableTypeName("void (&)(int, bool, char, float)"));
3876}
3877
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003878// Tests JoinAsTuple().
3879
3880TEST(JoinAsTupleTest, JoinsEmptyTuple) {
3881 EXPECT_EQ("", JoinAsTuple(Strings()));
3882}
3883
3884TEST(JoinAsTupleTest, JoinsOneTuple) {
3885 const char* fields[] = { "1" };
3886 EXPECT_EQ("1", JoinAsTuple(Strings(fields, fields + 1)));
3887}
3888
3889TEST(JoinAsTupleTest, JoinsTwoTuple) {
3890 const char* fields[] = { "1", "a" };
3891 EXPECT_EQ("(1, a)", JoinAsTuple(Strings(fields, fields + 2)));
3892}
3893
3894TEST(JoinAsTupleTest, JoinsTenTuple) {
3895 const char* fields[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
3896 EXPECT_EQ("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)",
3897 JoinAsTuple(Strings(fields, fields + 10)));
3898}
3899
3900// Tests FormatMatcherDescription().
3901
3902TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) {
3903 EXPECT_EQ("is even",
zhanyong.wanb4140802010-06-08 22:53:57 +00003904 FormatMatcherDescription(false, "IsEven", Strings()));
3905 EXPECT_EQ("not (is even)",
3906 FormatMatcherDescription(true, "IsEven", Strings()));
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003907
3908 const char* params[] = { "5" };
3909 EXPECT_EQ("equals 5",
zhanyong.wanb4140802010-06-08 22:53:57 +00003910 FormatMatcherDescription(false, "Equals",
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003911 Strings(params, params + 1)));
3912
3913 const char* params2[] = { "5", "8" };
3914 EXPECT_EQ("is in range (5, 8)",
zhanyong.wanb4140802010-06-08 22:53:57 +00003915 FormatMatcherDescription(false, "IsInRange",
zhanyong.wan4a5330d2009-02-19 00:36:44 +00003916 Strings(params2, params2 + 2)));
3917}
3918
zhanyong.wan2b43a9e2009-08-31 23:51:23 +00003919// Tests PolymorphicMatcher::mutable_impl().
3920TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {
3921 PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
3922 DivisibleByImpl& impl = m.mutable_impl();
3923 EXPECT_EQ(42, impl.divider());
3924
3925 impl.set_divider(0);
3926 EXPECT_EQ(0, m.mutable_impl().divider());
3927}
3928
3929// Tests PolymorphicMatcher::impl().
3930TEST(PolymorphicMatcherTest, CanAccessImpl) {
3931 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
3932 const DivisibleByImpl& impl = m.impl();
3933 EXPECT_EQ(42, impl.divider());
3934}
3935
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003936TEST(MatcherTupleTest, ExplainsMatchFailure) {
3937 stringstream ss1;
3938 ExplainMatchFailureTupleTo(make_tuple(Matcher<char>(Eq('a')), GreaterThan(5)),
3939 make_tuple('a', 10), &ss1);
3940 EXPECT_EQ("", ss1.str()); // Successful match.
3941
3942 stringstream ss2;
3943 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
3944 make_tuple(2, 'b'), &ss2);
3945 EXPECT_EQ(" Expected arg #0: is > 5\n"
3946 " Actual: 2, which is 3 less than 5\n"
zhanyong.wand60c5f42010-07-21 22:21:07 +00003947 " Expected arg #1: is equal to 'a' (97, 0x61)\n"
3948 " Actual: 'b' (98, 0x62)\n",
zhanyong.wanb1c7f932010-03-24 17:35:11 +00003949 ss2.str()); // Failed match where both arguments need explanation.
3950
3951 stringstream ss3;
3952 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
3953 make_tuple(2, 'a'), &ss3);
3954 EXPECT_EQ(" Expected arg #0: is > 5\n"
3955 " Actual: 2, which is 3 less than 5\n",
3956 ss3.str()); // Failed match where only one argument needs
3957 // explanation.
3958}
3959
zhanyong.wan33605ba2010-04-22 23:37:47 +00003960// Tests Each().
3961
3962TEST(EachTest, ExplainsMatchResultCorrectly) {
3963 set<int> a; // empty
3964
3965 Matcher<set<int> > m = Each(2);
3966 EXPECT_EQ("", Explain(m, a));
3967
zhanyong.wan736baa82010-09-27 17:44:16 +00003968 Matcher<const int(&)[1]> n = Each(1); // NOLINT
zhanyong.wan33605ba2010-04-22 23:37:47 +00003969
3970 const int b[1] = { 1 };
3971 EXPECT_EQ("", Explain(n, b));
3972
3973 n = Each(3);
3974 EXPECT_EQ("whose element #0 doesn't match", Explain(n, b));
3975
3976 a.insert(1);
3977 a.insert(2);
3978 a.insert(3);
3979 m = Each(GreaterThan(0));
3980 EXPECT_EQ("", Explain(m, a));
3981
3982 m = Each(GreaterThan(10));
3983 EXPECT_EQ("whose element #0 doesn't match, which is 9 less than 10",
3984 Explain(m, a));
3985}
3986
3987TEST(EachTest, DescribesItselfCorrectly) {
3988 Matcher<vector<int> > m = Each(1);
3989 EXPECT_EQ("only contains elements that is equal to 1", Describe(m));
3990
3991 Matcher<vector<int> > m2 = Not(m);
3992 EXPECT_EQ("contains some element that isn't equal to 1", Describe(m2));
3993}
3994
3995TEST(EachTest, MatchesVectorWhenAllElementsMatch) {
3996 vector<int> some_vector;
3997 EXPECT_THAT(some_vector, Each(1));
3998 some_vector.push_back(3);
3999 EXPECT_THAT(some_vector, Not(Each(1)));
4000 EXPECT_THAT(some_vector, Each(3));
4001 some_vector.push_back(1);
4002 some_vector.push_back(2);
4003 EXPECT_THAT(some_vector, Not(Each(3)));
4004 EXPECT_THAT(some_vector, Each(Lt(3.5)));
4005
4006 vector<string> another_vector;
4007 another_vector.push_back("fee");
4008 EXPECT_THAT(another_vector, Each(string("fee")));
4009 another_vector.push_back("fie");
4010 another_vector.push_back("foe");
4011 another_vector.push_back("fum");
4012 EXPECT_THAT(another_vector, Not(Each(string("fee"))));
4013}
4014
4015TEST(EachTest, MatchesMapWhenAllElementsMatch) {
4016 map<const char*, int> my_map;
4017 const char* bar = "a string";
4018 my_map[bar] = 2;
4019 EXPECT_THAT(my_map, Each(make_pair(bar, 2)));
4020
4021 map<string, int> another_map;
4022 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1)));
4023 another_map["fee"] = 1;
4024 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1)));
4025 another_map["fie"] = 2;
4026 another_map["foe"] = 3;
4027 another_map["fum"] = 4;
4028 EXPECT_THAT(another_map, Not(Each(make_pair(string("fee"), 1))));
4029 EXPECT_THAT(another_map, Not(Each(make_pair(string("fum"), 1))));
4030 EXPECT_THAT(another_map, Each(Pair(_, Gt(0))));
4031}
4032
4033TEST(EachTest, AcceptsMatcher) {
4034 const int a[] = { 1, 2, 3 };
4035 EXPECT_THAT(a, Each(Gt(0)));
4036 EXPECT_THAT(a, Not(Each(Gt(1))));
4037}
4038
4039TEST(EachTest, WorksForNativeArrayAsTuple) {
4040 const int a[] = { 1, 2 };
4041 const int* const pointer = a;
4042 EXPECT_THAT(make_tuple(pointer, 2), Each(Gt(0)));
4043 EXPECT_THAT(make_tuple(pointer, 2), Not(Each(Gt(1))));
4044}
4045
zhanyong.wanab5b77c2010-05-17 19:32:48 +00004046// For testing Pointwise().
4047class IsHalfOfMatcher {
4048 public:
4049 template <typename T1, typename T2>
4050 bool MatchAndExplain(const tuple<T1, T2>& a_pair,
4051 MatchResultListener* listener) const {
4052 if (get<0>(a_pair) == get<1>(a_pair)/2) {
4053 *listener << "where the second is " << get<1>(a_pair);
4054 return true;
4055 } else {
4056 *listener << "where the second/2 is " << get<1>(a_pair)/2;
4057 return false;
4058 }
4059 }
4060
4061 void DescribeTo(ostream* os) const {
4062 *os << "are a pair where the first is half of the second";
4063 }
4064
4065 void DescribeNegationTo(ostream* os) const {
4066 *os << "are a pair where the first isn't half of the second";
4067 }
4068};
4069
4070PolymorphicMatcher<IsHalfOfMatcher> IsHalfOf() {
4071 return MakePolymorphicMatcher(IsHalfOfMatcher());
4072}
4073
4074TEST(PointwiseTest, DescribesSelf) {
4075 vector<int> rhs;
4076 rhs.push_back(1);
4077 rhs.push_back(2);
4078 rhs.push_back(3);
4079 const Matcher<const vector<int>&> m = Pointwise(IsHalfOf(), rhs);
4080 EXPECT_EQ("contains 3 values, where each value and its corresponding value "
4081 "in { 1, 2, 3 } are a pair where the first is half of the second",
4082 Describe(m));
4083 EXPECT_EQ("doesn't contain exactly 3 values, or contains a value x at some "
4084 "index i where x and the i-th value of { 1, 2, 3 } are a pair "
4085 "where the first isn't half of the second",
4086 DescribeNegation(m));
4087}
4088
4089TEST(PointwiseTest, MakesCopyOfRhs) {
4090 list<signed char> rhs;
4091 rhs.push_back(2);
4092 rhs.push_back(4);
4093
4094 int lhs[] = { 1, 2 };
4095 const Matcher<const int (&)[2]> m = Pointwise(IsHalfOf(), rhs);
4096 EXPECT_THAT(lhs, m);
4097
4098 // Changing rhs now shouldn't affect m, which made a copy of rhs.
4099 rhs.push_back(6);
4100 EXPECT_THAT(lhs, m);
4101}
4102
4103TEST(PointwiseTest, WorksForLhsNativeArray) {
4104 const int lhs[] = { 1, 2, 3 };
4105 vector<int> rhs;
4106 rhs.push_back(2);
4107 rhs.push_back(4);
4108 rhs.push_back(6);
4109 EXPECT_THAT(lhs, Pointwise(Lt(), rhs));
4110 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
4111}
4112
4113TEST(PointwiseTest, WorksForRhsNativeArray) {
4114 const int rhs[] = { 1, 2, 3 };
4115 vector<int> lhs;
4116 lhs.push_back(2);
4117 lhs.push_back(4);
4118 lhs.push_back(6);
4119 EXPECT_THAT(lhs, Pointwise(Gt(), rhs));
4120 EXPECT_THAT(lhs, Not(Pointwise(Lt(), rhs)));
4121}
4122
4123TEST(PointwiseTest, RejectsWrongSize) {
4124 const double lhs[2] = { 1, 2 };
4125 const int rhs[1] = { 0 };
4126 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
4127 EXPECT_EQ("which contains 2 values",
4128 Explain(Pointwise(Gt(), rhs), lhs));
4129
4130 const int rhs2[3] = { 0, 1, 2 };
4131 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs2)));
4132}
4133
4134TEST(PointwiseTest, RejectsWrongContent) {
4135 const double lhs[3] = { 1, 2, 3 };
4136 const int rhs[3] = { 2, 6, 4 };
4137 EXPECT_THAT(lhs, Not(Pointwise(IsHalfOf(), rhs)));
4138 EXPECT_EQ("where the value pair (2, 6) at index #1 don't match, "
4139 "where the second/2 is 3",
4140 Explain(Pointwise(IsHalfOf(), rhs), lhs));
4141}
4142
4143TEST(PointwiseTest, AcceptsCorrectContent) {
4144 const double lhs[3] = { 1, 2, 3 };
4145 const int rhs[3] = { 2, 4, 6 };
4146 EXPECT_THAT(lhs, Pointwise(IsHalfOf(), rhs));
4147 EXPECT_EQ("", Explain(Pointwise(IsHalfOf(), rhs), lhs));
4148}
4149
4150TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) {
4151 const double lhs[3] = { 1, 2, 3 };
4152 const int rhs[3] = { 2, 4, 6 };
4153 const Matcher<tuple<const double&, const int&> > m1 = IsHalfOf();
4154 EXPECT_THAT(lhs, Pointwise(m1, rhs));
4155 EXPECT_EQ("", Explain(Pointwise(m1, rhs), lhs));
4156
4157 // This type works as a tuple<const double&, const int&> can be
4158 // implicitly cast to tuple<double, int>.
4159 const Matcher<tuple<double, int> > m2 = IsHalfOf();
4160 EXPECT_THAT(lhs, Pointwise(m2, rhs));
4161 EXPECT_EQ("", Explain(Pointwise(m2, rhs), lhs));
4162}
4163
shiqiane35fdd92008-12-10 05:08:54 +00004164} // namespace gmock_matchers_test
4165} // namespace testing