Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(128)

Side by Side Diff: testing/gmock/test/gmock-matchers_test.cc

Issue 1151006: Update to current gtest/gmock. (Closed)
Patch Set: rebase Created 10 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2007, Google Inc. 1 // Copyright 2007, Google Inc.
2 // All rights reserved. 2 // All rights reserved.
3 // 3 //
4 // Redistribution and use in source and binary forms, with or without 4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are 5 // modification, are permitted provided that the following conditions are
6 // met: 6 // met:
7 // 7 //
8 // * Redistributions of source code must retain the above copyright 8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer. 9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above 10 // * Redistributions in binary form must reproduce the above
(...skipping 19 matching lines...) Expand all
30 // Author: wan@google.com (Zhanyong Wan) 30 // Author: wan@google.com (Zhanyong Wan)
31 31
32 // Google Mock - a framework for writing C++ mock classes. 32 // Google Mock - a framework for writing C++ mock classes.
33 // 33 //
34 // This file tests some commonly used argument matchers. 34 // This file tests some commonly used argument matchers.
35 35
36 #include <gmock/gmock-matchers.h> 36 #include <gmock/gmock-matchers.h>
37 37
38 #include <string.h> 38 #include <string.h>
39 #include <functional> 39 #include <functional>
40 #include <iostream>
40 #include <list> 41 #include <list>
41 #include <map> 42 #include <map>
42 #include <set> 43 #include <set>
43 #include <sstream> 44 #include <sstream>
44 #include <string> 45 #include <string>
45 #include <utility> 46 #include <utility>
46 #include <vector> 47 #include <vector>
47 #include <gmock/gmock.h> 48 #include <gmock/gmock.h>
48 #include <gtest/gtest.h> 49 #include <gtest/gtest.h>
49 #include <gtest/gtest-spi.h> 50 #include <gtest/gtest-spi.h>
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 using testing::IsNull; 82 using testing::IsNull;
82 using testing::Key; 83 using testing::Key;
83 using testing::Le; 84 using testing::Le;
84 using testing::Lt; 85 using testing::Lt;
85 using testing::MakeMatcher; 86 using testing::MakeMatcher;
86 using testing::MakePolymorphicMatcher; 87 using testing::MakePolymorphicMatcher;
87 using testing::Matcher; 88 using testing::Matcher;
88 using testing::MatcherCast; 89 using testing::MatcherCast;
89 using testing::MatcherInterface; 90 using testing::MatcherInterface;
90 using testing::Matches; 91 using testing::Matches;
92 using testing::ExplainMatchResult;
93 using testing::MatchResultListener;
91 using testing::NanSensitiveDoubleEq; 94 using testing::NanSensitiveDoubleEq;
92 using testing::NanSensitiveFloatEq; 95 using testing::NanSensitiveFloatEq;
93 using testing::Ne; 96 using testing::Ne;
94 using testing::Not; 97 using testing::Not;
95 using testing::NotNull; 98 using testing::NotNull;
96 using testing::Pair; 99 using testing::Pair;
97 using testing::Pointee; 100 using testing::Pointee;
98 using testing::PolymorphicMatcher; 101 using testing::PolymorphicMatcher;
99 using testing::Property; 102 using testing::Property;
100 using testing::Ref; 103 using testing::Ref;
101 using testing::ResultOf; 104 using testing::ResultOf;
102 using testing::StartsWith; 105 using testing::StartsWith;
103 using testing::StrCaseEq; 106 using testing::StrCaseEq;
104 using testing::StrCaseNe; 107 using testing::StrCaseNe;
105 using testing::StrEq; 108 using testing::StrEq;
106 using testing::StrNe; 109 using testing::StrNe;
107 using testing::Truly; 110 using testing::Truly;
108 using testing::TypedEq; 111 using testing::TypedEq;
109 using testing::Value; 112 using testing::Value;
110 using testing::_; 113 using testing::_;
114 using testing::internal::DummyMatchResultListener;
111 using testing::internal::FloatingEqMatcher; 115 using testing::internal::FloatingEqMatcher;
112 using testing::internal::FormatMatcherDescriptionSyntaxError; 116 using testing::internal::FormatMatcherDescriptionSyntaxError;
113 using testing::internal::GetParamIndex; 117 using testing::internal::GetParamIndex;
114 using testing::internal::Interpolation; 118 using testing::internal::Interpolation;
115 using testing::internal::Interpolations; 119 using testing::internal::Interpolations;
116 using testing::internal::JoinAsTuple; 120 using testing::internal::JoinAsTuple;
117 using testing::internal::SkipPrefix; 121 using testing::internal::SkipPrefix;
122 using testing::internal::StreamMatchResultListener;
118 using testing::internal::String; 123 using testing::internal::String;
119 using testing::internal::Strings; 124 using testing::internal::Strings;
125 using testing::internal::StringMatchResultListener;
120 using testing::internal::ValidateMatcherDescription; 126 using testing::internal::ValidateMatcherDescription;
121 using testing::internal::kInvalidInterpolation; 127 using testing::internal::kInvalidInterpolation;
122 using testing::internal::kPercentInterpolation; 128 using testing::internal::kPercentInterpolation;
123 using testing::internal::kTupleInterpolation; 129 using testing::internal::kTupleInterpolation;
124 using testing::internal::linked_ptr; 130 using testing::internal::linked_ptr;
125 using testing::internal::scoped_ptr; 131 using testing::internal::scoped_ptr;
126 using testing::internal::string; 132 using testing::internal::string;
127 133
128 #ifdef GMOCK_HAS_REGEX
129 using testing::ContainsRegex; 134 using testing::ContainsRegex;
130 using testing::MatchesRegex; 135 using testing::MatchesRegex;
131 using testing::internal::RE; 136 using testing::internal::RE;
132 #endif // GMOCK_HAS_REGEX
133 137
134 // For testing ExplainMatchResultTo(). 138 // For testing ExplainMatchResultTo().
135 class GreaterThanMatcher : public MatcherInterface<int> { 139 class GreaterThanMatcher : public MatcherInterface<int> {
136 public: 140 public:
137 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {} 141 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
138 142
139 virtual bool Matches(int lhs) const { return lhs > rhs_; }
140
141 virtual void DescribeTo(::std::ostream* os) const { 143 virtual void DescribeTo(::std::ostream* os) const {
142 *os << "is greater than " << rhs_; 144 *os << "is greater than " << rhs_;
143 } 145 }
144 146
145 virtual void ExplainMatchResultTo(int lhs, ::std::ostream* os) const { 147 virtual bool MatchAndExplain(int lhs,
148 MatchResultListener* listener) const {
146 const int diff = lhs - rhs_; 149 const int diff = lhs - rhs_;
147 if (diff > 0) { 150 if (diff > 0) {
148 *os << "is " << diff << " more than " << rhs_; 151 *listener << "which is " << diff << " more than " << rhs_;
149 } else if (diff == 0) { 152 } else if (diff == 0) {
150 *os << "is the same as " << rhs_; 153 *listener << "which is the same as " << rhs_;
151 } else { 154 } else {
152 *os << "is " << -diff << " less than " << rhs_; 155 *listener << "which is " << -diff << " less than " << rhs_;
153 } 156 }
157
158 return lhs > rhs_;
154 } 159 }
155 160
156 private: 161 private:
157 int rhs_; 162 int rhs_;
158 }; 163 };
159 164
160 Matcher<int> GreaterThan(int n) { 165 Matcher<int> GreaterThan(int n) {
161 return MakeMatcher(new GreaterThanMatcher(n)); 166 return MakeMatcher(new GreaterThanMatcher(n));
162 } 167 }
163 168
(...skipping 14 matching lines...) Expand all
178 } 183 }
179 184
180 // Returns the reason why x matches, or doesn't match, m. 185 // Returns the reason why x matches, or doesn't match, m.
181 template <typename MatcherType, typename Value> 186 template <typename MatcherType, typename Value>
182 string Explain(const MatcherType& m, const Value& x) { 187 string Explain(const MatcherType& m, const Value& x) {
183 stringstream ss; 188 stringstream ss;
184 m.ExplainMatchResultTo(x, &ss); 189 m.ExplainMatchResultTo(x, &ss);
185 return ss.str(); 190 return ss.str();
186 } 191 }
187 192
193 TEST(MatchResultListenerTest, StreamingWorks) {
194 StringMatchResultListener listener;
195 listener << "hi" << 5;
196 EXPECT_EQ("hi5", listener.str());
197
198 // Streaming shouldn't crash when the underlying ostream is NULL.
199 DummyMatchResultListener dummy;
200 dummy << "hi" << 5;
201 }
202
203 TEST(MatchResultListenerTest, CanAccessUnderlyingStream) {
204 EXPECT_TRUE(DummyMatchResultListener().stream() == NULL);
205 EXPECT_TRUE(StreamMatchResultListener(NULL).stream() == NULL);
206
207 EXPECT_EQ(&std::cout, StreamMatchResultListener(&std::cout).stream());
208 }
209
210 TEST(MatchResultListenerTest, IsInterestedWorks) {
211 EXPECT_TRUE(StringMatchResultListener().IsInterested());
212 EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested());
213
214 EXPECT_FALSE(DummyMatchResultListener().IsInterested());
215 EXPECT_FALSE(StreamMatchResultListener(NULL).IsInterested());
216 }
217
188 // Makes sure that the MatcherInterface<T> interface doesn't 218 // Makes sure that the MatcherInterface<T> interface doesn't
189 // change. 219 // change.
190 class EvenMatcherImpl : public MatcherInterface<int> { 220 class EvenMatcherImpl : public MatcherInterface<int> {
191 public: 221 public:
192 virtual bool Matches(int x) const { return x % 2 == 0; } 222 virtual bool MatchAndExplain(int x,
223 MatchResultListener* /* listener */) const {
224 return x % 2 == 0;
225 }
193 226
194 virtual void DescribeTo(::std::ostream* os) const { 227 virtual void DescribeTo(::std::ostream* os) const {
195 *os << "is an even number"; 228 *os << "is an even number";
196 } 229 }
197 230
198 // We deliberately don't define DescribeNegationTo() and 231 // We deliberately don't define DescribeNegationTo() and
199 // ExplainMatchResultTo() here, to make sure the definition of these 232 // ExplainMatchResultTo() here, to make sure the definition of these
200 // two methods is optional. 233 // two methods is optional.
201 }; 234 };
202 235
203 TEST(MatcherInterfaceTest, CanBeImplemented) { 236 // Makes sure that the MatcherInterface API doesn't change.
237 TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) {
204 EvenMatcherImpl m; 238 EvenMatcherImpl m;
205 } 239 }
206 240
241 // Tests implementing a monomorphic matcher using MatchAndExplain().
242
243 class NewEvenMatcherImpl : public MatcherInterface<int> {
244 public:
245 virtual bool MatchAndExplain(int x, MatchResultListener* listener) const {
246 const bool match = x % 2 == 0;
247 // Verifies that we can stream to a listener directly.
248 *listener << "value % " << 2;
249 if (listener->stream() != NULL) {
250 // Verifies that we can stream to a listener's underlying stream
251 // too.
252 *listener->stream() << " == " << (x % 2);
253 }
254 return match;
255 }
256
257 virtual void DescribeTo(::std::ostream* os) const {
258 *os << "is an even number";
259 }
260 };
261
262 TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {
263 Matcher<int> m = MakeMatcher(new NewEvenMatcherImpl);
264 EXPECT_TRUE(m.Matches(2));
265 EXPECT_FALSE(m.Matches(3));
266 EXPECT_EQ("value % 2 == 0", Explain(m, 2));
267 EXPECT_EQ("value % 2 == 1", Explain(m, 3));
268 }
269
207 // Tests default-constructing a matcher. 270 // Tests default-constructing a matcher.
208 TEST(MatcherTest, CanBeDefaultConstructed) { 271 TEST(MatcherTest, CanBeDefaultConstructed) {
209 Matcher<double> m; 272 Matcher<double> m;
210 } 273 }
211 274
212 // Tests that Matcher<T> can be constructed from a MatcherInterface<T>*. 275 // Tests that Matcher<T> can be constructed from a MatcherInterface<T>*.
213 TEST(MatcherTest, CanBeConstructedFromMatcherInterface) { 276 TEST(MatcherTest, CanBeConstructedFromMatcherInterface) {
214 const MatcherInterface<int>* impl = new EvenMatcherImpl; 277 const MatcherInterface<int>* impl = new EvenMatcherImpl;
215 Matcher<int> m(impl); 278 Matcher<int> m(impl);
216 EXPECT_TRUE(m.Matches(4)); 279 EXPECT_TRUE(m.Matches(4));
(...skipping 28 matching lines...) Expand all
245 EXPECT_FALSE(m1.Matches(false)); 308 EXPECT_FALSE(m1.Matches(false));
246 } 309 }
247 310
248 // Tests that Matcher<T>::DescribeTo() calls 311 // Tests that Matcher<T>::DescribeTo() calls
249 // MatcherInterface<T>::DescribeTo(). 312 // MatcherInterface<T>::DescribeTo().
250 TEST(MatcherTest, CanDescribeItself) { 313 TEST(MatcherTest, CanDescribeItself) {
251 EXPECT_EQ("is an even number", 314 EXPECT_EQ("is an even number",
252 Describe(Matcher<int>(new EvenMatcherImpl))); 315 Describe(Matcher<int>(new EvenMatcherImpl)));
253 } 316 }
254 317
318 // Tests Matcher<T>::MatchAndExplain().
319 TEST(MatcherTest, MatchAndExplain) {
320 Matcher<int> m = GreaterThan(0);
321 StringMatchResultListener listener1;
322 EXPECT_TRUE(m.MatchAndExplain(42, &listener1));
323 EXPECT_EQ("which is 42 more than 0", listener1.str());
324
325 StringMatchResultListener listener2;
326 EXPECT_FALSE(m.MatchAndExplain(-9, &listener2));
327 EXPECT_EQ("which is 9 less than 0", listener2.str());
328 }
329
255 // Tests that a C-string literal can be implicitly converted to a 330 // Tests that a C-string literal can be implicitly converted to a
256 // Matcher<string> or Matcher<const string&>. 331 // Matcher<string> or Matcher<const string&>.
257 TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) { 332 TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
258 Matcher<string> m1 = "hi"; 333 Matcher<string> m1 = "hi";
259 EXPECT_TRUE(m1.Matches("hi")); 334 EXPECT_TRUE(m1.Matches("hi"));
260 EXPECT_FALSE(m1.Matches("hello")); 335 EXPECT_FALSE(m1.Matches("hello"));
261 336
262 Matcher<const string&> m2 = "hi"; 337 Matcher<const string&> m2 = "hi";
263 EXPECT_TRUE(m2.Matches("hi")); 338 EXPECT_TRUE(m2.Matches("hi"));
264 EXPECT_FALSE(m2.Matches("hello")); 339 EXPECT_FALSE(m2.Matches("hello"));
(...skipping 12 matching lines...) Expand all
277 } 352 }
278 353
279 // Tests that MakeMatcher() constructs a Matcher<T> from a 354 // Tests that MakeMatcher() constructs a Matcher<T> from a
280 // MatcherInterface* without requiring the user to explicitly 355 // MatcherInterface* without requiring the user to explicitly
281 // write the type. 356 // write the type.
282 TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) { 357 TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) {
283 const MatcherInterface<int>* dummy_impl = NULL; 358 const MatcherInterface<int>* dummy_impl = NULL;
284 Matcher<int> m = MakeMatcher(dummy_impl); 359 Matcher<int> m = MakeMatcher(dummy_impl);
285 } 360 }
286 361
287 // Tests that MakePolymorphicMatcher() constructs a polymorphic 362 // Tests that MakePolymorphicMatcher() can construct a polymorphic
288 // matcher from its implementation. 363 // matcher from its implementation using the old API.
289 const int bar = 1; 364 const int bar = 1;
290 class ReferencesBarOrIsZeroImpl { 365 class ReferencesBarOrIsZeroImpl {
291 public: 366 public:
292 template <typename T> 367 template <typename T>
293 bool Matches(const T& x) const { 368 bool MatchAndExplain(const T& x,
369 MatchResultListener* /* listener */) const {
294 const void* p = &x; 370 const void* p = &x;
295 return p == &bar || x == 0; 371 return p == &bar || x == 0;
296 } 372 }
297 373
298 void DescribeTo(::std::ostream* os) const { *os << "bar or zero"; } 374 void DescribeTo(::std::ostream* os) const { *os << "bar or zero"; }
299 375
300 void DescribeNegationTo(::std::ostream* os) const { 376 void DescribeNegationTo(::std::ostream* os) const {
301 *os << "doesn't reference bar and is not zero"; 377 *os << "doesn't reference bar and is not zero";
302 } 378 }
303 }; 379 };
304 380
305 // This function verifies that MakePolymorphicMatcher() returns a 381 // This function verifies that MakePolymorphicMatcher() returns a
306 // PolymorphicMatcher<T> where T is the argument's type. 382 // PolymorphicMatcher<T> where T is the argument's type.
307 PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() { 383 PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() {
308 return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl()); 384 return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl());
309 } 385 }
310 386
311 TEST(MakePolymorphicMatcherTest, ConstructsMatcherFromImpl) { 387 TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) {
312 // Using a polymorphic matcher to match a reference type. 388 // Using a polymorphic matcher to match a reference type.
313 Matcher<const int&> m1 = ReferencesBarOrIsZero(); 389 Matcher<const int&> m1 = ReferencesBarOrIsZero();
314 EXPECT_TRUE(m1.Matches(0)); 390 EXPECT_TRUE(m1.Matches(0));
315 // Verifies that the identity of a by-reference argument is preserved. 391 // Verifies that the identity of a by-reference argument is preserved.
316 EXPECT_TRUE(m1.Matches(bar)); 392 EXPECT_TRUE(m1.Matches(bar));
317 EXPECT_FALSE(m1.Matches(1)); 393 EXPECT_FALSE(m1.Matches(1));
318 EXPECT_EQ("bar or zero", Describe(m1)); 394 EXPECT_EQ("bar or zero", Describe(m1));
319 395
320 // Using a polymorphic matcher to match a value type. 396 // Using a polymorphic matcher to match a value type.
321 Matcher<double> m2 = ReferencesBarOrIsZero(); 397 Matcher<double> m2 = ReferencesBarOrIsZero();
322 EXPECT_TRUE(m2.Matches(0.0)); 398 EXPECT_TRUE(m2.Matches(0.0));
323 EXPECT_FALSE(m2.Matches(0.1)); 399 EXPECT_FALSE(m2.Matches(0.1));
324 EXPECT_EQ("bar or zero", Describe(m2)); 400 EXPECT_EQ("bar or zero", Describe(m2));
325 } 401 }
326 402
403 // Tests implementing a polymorphic matcher using MatchAndExplain().
404
405 class PolymorphicIsEvenImpl {
406 public:
407 void DescribeTo(::std::ostream* os) const { *os << "is even"; }
408
409 void DescribeNegationTo(::std::ostream* os) const {
410 *os << "is odd";
411 }
412
413 template <typename T>
414 bool MatchAndExplain(const T& x, MatchResultListener* listener) const {
415 // Verifies that we can stream to the listener directly.
416 *listener << "% " << 2;
417 if (listener->stream() != NULL) {
418 // Verifies that we can stream to the listener's underlying stream
419 // too.
420 *listener->stream() << " == " << (x % 2);
421 }
422 return (x % 2) == 0;
423 }
424 };
425
426 PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() {
427 return MakePolymorphicMatcher(PolymorphicIsEvenImpl());
428 }
429
430 TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {
431 // Using PolymorphicIsEven() as a Matcher<int>.
432 const Matcher<int> m1 = PolymorphicIsEven();
433 EXPECT_TRUE(m1.Matches(42));
434 EXPECT_FALSE(m1.Matches(43));
435 EXPECT_EQ("is even", Describe(m1));
436
437 const Matcher<int> not_m1 = Not(m1);
438 EXPECT_EQ("is odd", Describe(not_m1));
439
440 EXPECT_EQ("% 2 == 0", Explain(m1, 42));
441
442 // Using PolymorphicIsEven() as a Matcher<char>.
443 const Matcher<char> m2 = PolymorphicIsEven();
444 EXPECT_TRUE(m2.Matches('\x42'));
445 EXPECT_FALSE(m2.Matches('\x43'));
446 EXPECT_EQ("is even", Describe(m2));
447
448 const Matcher<char> not_m2 = Not(m2);
449 EXPECT_EQ("is odd", Describe(not_m2));
450
451 EXPECT_EQ("% 2 == 0", Explain(m2, '\x42'));
452 }
453
327 // Tests that MatcherCast<T>(m) works when m is a polymorphic matcher. 454 // Tests that MatcherCast<T>(m) works when m is a polymorphic matcher.
328 TEST(MatcherCastTest, FromPolymorphicMatcher) { 455 TEST(MatcherCastTest, FromPolymorphicMatcher) {
329 Matcher<int> m = MatcherCast<int>(Eq(5)); 456 Matcher<int> m = MatcherCast<int>(Eq(5));
330 EXPECT_TRUE(m.Matches(5)); 457 EXPECT_TRUE(m.Matches(5));
331 EXPECT_FALSE(m.Matches(6)); 458 EXPECT_FALSE(m.Matches(6));
332 } 459 }
333 460
334 // For testing casting matchers between compatible types. 461 // For testing casting matchers between compatible types.
335 class IntValue { 462 class IntValue {
336 public: 463 public:
(...skipping 706 matching lines...) Expand 10 before | Expand all | Expand 10 after
1043 ", or has a second field that is not equal to 42", 1170 ", or has a second field that is not equal to 42",
1044 DescribeNegation(m1)); 1171 DescribeNegation(m1));
1045 // Double and triple negation (1 or 2 times not and description of negation). 1172 // Double and triple negation (1 or 2 times not and description of negation).
1046 Matcher<const std::pair<int, int>&> m2 = Not(Pair(Not(13), 42)); 1173 Matcher<const std::pair<int, int>&> m2 = Not(Pair(Not(13), 42));
1047 EXPECT_EQ("has a first field that is not equal to 13" 1174 EXPECT_EQ("has a first field that is not equal to 13"
1048 ", and has a second field that is equal to 42", 1175 ", and has a second field that is equal to 42",
1049 DescribeNegation(m2)); 1176 DescribeNegation(m2));
1050 } 1177 }
1051 1178
1052 TEST(PairTest, CanExplainMatchResultTo) { 1179 TEST(PairTest, CanExplainMatchResultTo) {
1053 const Matcher<std::pair<int, int> > m0 = Pair(0, 0); 1180 // If neither field matches, Pair() should explain about the first
1054 EXPECT_EQ("", Explain(m0, std::make_pair(25, 42))); 1181 // field.
1182 const Matcher<std::pair<int, int> > m = Pair(GreaterThan(0), GreaterThan(0));
1183 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
1184 Explain(m, std::make_pair(-1, -2)));
1055 1185
1056 const Matcher<std::pair<int, int> > m1 = Pair(GreaterThan(0), 0); 1186 // If the first field matches but the second doesn't, Pair() should
1057 EXPECT_EQ("the first field is 25 more than 0", 1187 // explain about the second field.
1058 Explain(m1, std::make_pair(25, 42))); 1188 EXPECT_EQ("whose second field does not match, which is 2 less than 0",
1189 Explain(m, std::make_pair(1, -2)));
1059 1190
1060 const Matcher<std::pair<int, int> > m2 = Pair(0, GreaterThan(0)); 1191 // If the first field doesn't match but the second does, Pair()
1061 EXPECT_EQ("the second field is 42 more than 0", 1192 // should explain about the first field.
1062 Explain(m2, std::make_pair(25, 42))); 1193 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
1194 Explain(m, std::make_pair(-1, 2)));
1063 1195
1064 const Matcher<std::pair<int, int> > m3 = Pair(GreaterThan(0), GreaterThan(0)); 1196 // If both fields match, Pair() should explain about them both.
1065 EXPECT_EQ("the first field is 25 more than 0" 1197 EXPECT_EQ("whose both fields match, where the first field is a value "
1066 ", and the second field is 42 more than 0", 1198 "which is 1 more than 0, and the second field is a value "
1067 Explain(m3, std::make_pair(25, 42))); 1199 "which is 2 more than 0",
1200 Explain(m, std::make_pair(1, 2)));
1201
1202 // If only the first match has an explanation, only this explanation should
1203 // be printed.
1204 const Matcher<std::pair<int, int> > explain_first = Pair(GreaterThan(0), 0);
1205 EXPECT_EQ("whose both fields match, where the first field is a value "
1206 "which is 1 more than 0",
1207 Explain(explain_first, std::make_pair(1, 0)));
1208
1209 // If only the second match has an explanation, only this explanation should
1210 // be printed.
1211 const Matcher<std::pair<int, int> > explain_second = Pair(0, GreaterThan(0));
1212 EXPECT_EQ("whose both fields match, where the second field is a value "
1213 "which is 1 more than 0",
1214 Explain(explain_second, std::make_pair(0, 1)));
1068 } 1215 }
1069 1216
1070 TEST(PairTest, MatchesCorrectly) { 1217 TEST(PairTest, MatchesCorrectly) {
1071 std::pair<int, std::string> p(25, "foo"); 1218 std::pair<int, std::string> p(25, "foo");
1072 1219
1073 // Both fields match. 1220 // Both fields match.
1074 EXPECT_THAT(p, Pair(25, "foo")); 1221 EXPECT_THAT(p, Pair(25, "foo"));
1075 EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o"))); 1222 EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o")));
1076 1223
1077 // 'first' doesnt' match, but 'second' matches. 1224 // 'first' doesnt' match, but 'second' matches.
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
1143 EXPECT_TRUE(m2.Matches("Super Hi")); 1290 EXPECT_TRUE(m2.Matches("Super Hi"));
1144 EXPECT_FALSE(m2.Matches("i")); 1291 EXPECT_FALSE(m2.Matches("i"));
1145 EXPECT_FALSE(m2.Matches("Hi ")); 1292 EXPECT_FALSE(m2.Matches("Hi "));
1146 } 1293 }
1147 1294
1148 TEST(EndsWithTest, CanDescribeSelf) { 1295 TEST(EndsWithTest, CanDescribeSelf) {
1149 Matcher<const std::string> m = EndsWith("Hi"); 1296 Matcher<const std::string> m = EndsWith("Hi");
1150 EXPECT_EQ("ends with \"Hi\"", Describe(m)); 1297 EXPECT_EQ("ends with \"Hi\"", Describe(m));
1151 } 1298 }
1152 1299
1153 #ifdef GMOCK_HAS_REGEX
1154
1155 // Tests MatchesRegex(). 1300 // Tests MatchesRegex().
1156 1301
1157 TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) { 1302 TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
1158 const Matcher<const char*> m1 = MatchesRegex("a.*z"); 1303 const Matcher<const char*> m1 = MatchesRegex("a.*z");
1159 EXPECT_TRUE(m1.Matches("az")); 1304 EXPECT_TRUE(m1.Matches("az"));
1160 EXPECT_TRUE(m1.Matches("abcz")); 1305 EXPECT_TRUE(m1.Matches("abcz"));
1161 EXPECT_FALSE(m1.Matches(NULL)); 1306 EXPECT_FALSE(m1.Matches(NULL));
1162 1307
1163 const Matcher<const string&> m2 = MatchesRegex(new RE("a.*z")); 1308 const Matcher<const string&> m2 = MatchesRegex(new RE("a.*z"));
1164 EXPECT_TRUE(m2.Matches("azbz")); 1309 EXPECT_TRUE(m2.Matches("azbz"));
1165 EXPECT_FALSE(m2.Matches("az1")); 1310 EXPECT_FALSE(m2.Matches("az1"));
1166 EXPECT_FALSE(m2.Matches("1az")); 1311 EXPECT_FALSE(m2.Matches("1az"));
1167 } 1312 }
1168 1313
1169 TEST(MatchesRegexTest, CanDescribeSelf) { 1314 TEST(MatchesRegexTest, CanDescribeSelf) {
1170 Matcher<const std::string> m1 = MatchesRegex(string("Hi.*")); 1315 Matcher<const std::string> m1 = MatchesRegex(string("Hi.*"));
1171 EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1)); 1316 EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1));
1172 1317
1173 Matcher<const char*> m2 = MatchesRegex(new RE("[a-z].*")); 1318 Matcher<const char*> m2 = MatchesRegex(new RE("a.*"));
1174 EXPECT_EQ("matches regular expression \"[a-z].*\"", Describe(m2)); 1319 EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2));
1175 } 1320 }
1176 1321
1177 // Tests ContainsRegex(). 1322 // Tests ContainsRegex().
1178 1323
1179 TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) { 1324 TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
1180 const Matcher<const char*> m1 = ContainsRegex(string("a.*z")); 1325 const Matcher<const char*> m1 = ContainsRegex(string("a.*z"));
1181 EXPECT_TRUE(m1.Matches("az")); 1326 EXPECT_TRUE(m1.Matches("az"));
1182 EXPECT_TRUE(m1.Matches("0abcz1")); 1327 EXPECT_TRUE(m1.Matches("0abcz1"));
1183 EXPECT_FALSE(m1.Matches(NULL)); 1328 EXPECT_FALSE(m1.Matches(NULL));
1184 1329
1185 const Matcher<const string&> m2 = ContainsRegex(new RE("a.*z")); 1330 const Matcher<const string&> m2 = ContainsRegex(new RE("a.*z"));
1186 EXPECT_TRUE(m2.Matches("azbz")); 1331 EXPECT_TRUE(m2.Matches("azbz"));
1187 EXPECT_TRUE(m2.Matches("az1")); 1332 EXPECT_TRUE(m2.Matches("az1"));
1188 EXPECT_FALSE(m2.Matches("1a")); 1333 EXPECT_FALSE(m2.Matches("1a"));
1189 } 1334 }
1190 1335
1191 TEST(ContainsRegexTest, CanDescribeSelf) { 1336 TEST(ContainsRegexTest, CanDescribeSelf) {
1192 Matcher<const std::string> m1 = ContainsRegex("Hi.*"); 1337 Matcher<const std::string> m1 = ContainsRegex("Hi.*");
1193 EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1)); 1338 EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1));
1194 1339
1195 Matcher<const char*> m2 = ContainsRegex(new RE("[a-z].*")); 1340 Matcher<const char*> m2 = ContainsRegex(new RE("a.*"));
1196 EXPECT_EQ("contains regular expression \"[a-z].*\"", Describe(m2)); 1341 EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2));
1197 } 1342 }
1198 #endif // GMOCK_HAS_REGEX
1199 1343
1200 // Tests for wide strings. 1344 // Tests for wide strings.
1201 #if GTEST_HAS_STD_WSTRING 1345 #if GTEST_HAS_STD_WSTRING
1202 TEST(StdWideStrEqTest, MatchesEqual) { 1346 TEST(StdWideStrEqTest, MatchesEqual) {
1203 Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello")); 1347 Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello"));
1204 EXPECT_TRUE(m.Matches(L"Hello")); 1348 EXPECT_TRUE(m.Matches(L"Hello"));
1205 EXPECT_FALSE(m.Matches(L"hello")); 1349 EXPECT_FALSE(m.Matches(L"hello"));
1206 EXPECT_FALSE(m.Matches(NULL)); 1350 EXPECT_FALSE(m.Matches(NULL));
1207 1351
1208 Matcher<const ::std::wstring&> m2 = StrEq(L"Hello"); 1352 Matcher<const ::std::wstring&> m2 = StrEq(L"Hello");
(...skipping 733 matching lines...) Expand 10 before | Expand all | Expand 10 after
1942 const Matcher<int> is_zero = Eq(0); 2086 const Matcher<int> is_zero = Eq(0);
1943 EXPECT_TRUE(Value(0, is_zero)); 2087 EXPECT_TRUE(Value(0, is_zero));
1944 EXPECT_FALSE(Value('a', is_zero)); 2088 EXPECT_FALSE(Value('a', is_zero));
1945 2089
1946 int n = 0; 2090 int n = 0;
1947 const Matcher<const int&> ref_n = Ref(n); 2091 const Matcher<const int&> ref_n = Ref(n);
1948 EXPECT_TRUE(Value(n, ref_n)); 2092 EXPECT_TRUE(Value(n, ref_n));
1949 EXPECT_FALSE(Value(1, ref_n)); 2093 EXPECT_FALSE(Value(1, ref_n));
1950 } 2094 }
1951 2095
2096 TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) {
2097 StringMatchResultListener listener1;
2098 EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1));
2099 EXPECT_EQ("% 2 == 0", listener1.str());
2100
2101 StringMatchResultListener listener2;
2102 EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2));
2103 EXPECT_EQ("", listener2.str());
2104 }
2105
2106 TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) {
2107 const Matcher<int> is_even = PolymorphicIsEven();
2108 StringMatchResultListener listener1;
2109 EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1));
2110 EXPECT_EQ("% 2 == 0", listener1.str());
2111
2112 const Matcher<const double&> is_zero = Eq(0);
2113 StringMatchResultListener listener2;
2114 EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2));
2115 EXPECT_EQ("", listener2.str());
2116 }
2117
2118 MATCHER_P(Really, inner_matcher, "") {
2119 return ExplainMatchResult(inner_matcher, arg, result_listener);
2120 }
2121
2122 TEST(ExplainMatchResultTest, WorksInsideMATCHER) {
2123 EXPECT_THAT(0, Really(Eq(0)));
2124 }
2125
1952 TEST(AllArgsTest, WorksForTuple) { 2126 TEST(AllArgsTest, WorksForTuple) {
1953 EXPECT_THAT(make_tuple(1, 2L), AllArgs(Lt())); 2127 EXPECT_THAT(make_tuple(1, 2L), AllArgs(Lt()));
1954 EXPECT_THAT(make_tuple(2L, 1), Not(AllArgs(Lt()))); 2128 EXPECT_THAT(make_tuple(2L, 1), Not(AllArgs(Lt())));
1955 } 2129 }
1956 2130
1957 TEST(AllArgsTest, WorksForNonTuple) { 2131 TEST(AllArgsTest, WorksForNonTuple) {
1958 EXPECT_THAT(42, AllArgs(Gt(0))); 2132 EXPECT_THAT(42, AllArgs(Gt(0)));
1959 EXPECT_THAT('a', Not(AllArgs(Eq('b')))); 2133 EXPECT_THAT('a', Not(AllArgs(Eq('b'))));
1960 } 2134 }
1961 2135
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
2034 "Actual: 0 (is located @"); 2208 "Actual: 0 (is located @");
2035 } 2209 }
2036 2210
2037 #if !GTEST_OS_SYMBIAN 2211 #if !GTEST_OS_SYMBIAN
2038 // Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is 2212 // Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is
2039 // monomorphic. 2213 // monomorphic.
2040 2214
2041 // ASSERT_THAT("hello", starts_with_he) fails to compile with Nokia's 2215 // ASSERT_THAT("hello", starts_with_he) fails to compile with Nokia's
2042 // Symbian compiler: it tries to compile 2216 // Symbian compiler: it tries to compile
2043 // template<T, U> class MatcherCastImpl { ... 2217 // template<T, U> class MatcherCastImpl { ...
2044 // virtual bool Matches(T x) const { 2218 // virtual bool MatchAndExplain(T x, ...) const {
2045 // return source_matcher_.Matches(static_cast<U>(x)); 2219 // return source_matcher_.MatchAndExplain(static_cast<U>(x), ...);
2046 // with U == string and T == const char* 2220 // with U == string and T == const char*
2047 // With ASSERT_THAT("hello"...) changed to ASSERT_THAT(string("hello") ... ) 2221 // With ASSERT_THAT("hello"...) changed to ASSERT_THAT(string("hello") ... )
2048 // the compiler silently crashes with no output. 2222 // the compiler silently crashes with no output.
2049 // If MatcherCastImpl is changed to use U(x) instead of static_cast<U>(x) 2223 // If MatcherCastImpl is changed to use U(x) instead of static_cast<U>(x)
2050 // the code compiles but the converted string is bogus. 2224 // the code compiles but the converted string is bogus.
2051 TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) { 2225 TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
2052 Matcher<const char*> starts_with_he = StartsWith("he"); 2226 Matcher<const char*> starts_with_he = StartsWith("he");
2053 ASSERT_THAT("hello", starts_with_he); 2227 ASSERT_THAT("hello", starts_with_he);
2054 2228
2055 Matcher<const string&> ends_with_ok = EndsWith("ok"); 2229 Matcher<const string&> ends_with_ok = EndsWith("ok");
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after
2378 DescribeNegation(m)); 2552 DescribeNegation(m));
2379 } 2553 }
2380 2554
2381 TEST(PointeeTest, CanExplainMatchResult) { 2555 TEST(PointeeTest, CanExplainMatchResult) {
2382 const Matcher<const string*> m = Pointee(StartsWith("Hi")); 2556 const Matcher<const string*> m = Pointee(StartsWith("Hi"));
2383 2557
2384 EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL))); 2558 EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL)));
2385 2559
2386 const Matcher<int*> m2 = Pointee(GreaterThan(1)); 2560 const Matcher<int*> m2 = Pointee(GreaterThan(1));
2387 int n = 3; 2561 int n = 3;
2388 EXPECT_EQ("points to a value that is 2 more than 1", Explain(m2, &n)); 2562 EXPECT_EQ("which points to 3, which is 2 more than 1",
2563 Explain(m2, &n));
2564 }
2565
2566 TEST(PointeeTest, AlwaysExplainsPointee) {
2567 const Matcher<int*> m = Pointee(0);
2568 int n = 42;
2569 EXPECT_EQ("which points to 42", Explain(m, &n));
2389 } 2570 }
2390 2571
2391 // An uncopyable class. 2572 // An uncopyable class.
2392 class Uncopyable { 2573 class Uncopyable {
2393 public: 2574 public:
2394 explicit Uncopyable(int a_value) : value_(a_value) {} 2575 explicit Uncopyable(int a_value) : value_(a_value) {}
2395 2576
2396 int value() const { return value_; } 2577 int value() const { return value_; }
2397 private: 2578 private:
2398 const int value_; 2579 const int value_;
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
2505 AStruct a; 2686 AStruct a;
2506 EXPECT_TRUE(m.Matches(a)); 2687 EXPECT_TRUE(m.Matches(a));
2507 a.x = -1; 2688 a.x = -1;
2508 EXPECT_FALSE(m.Matches(a)); 2689 EXPECT_FALSE(m.Matches(a));
2509 } 2690 }
2510 2691
2511 // Tests that Field() can describe itself. 2692 // Tests that Field() can describe itself.
2512 TEST(FieldTest, CanDescribeSelf) { 2693 TEST(FieldTest, CanDescribeSelf) {
2513 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0)); 2694 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2514 2695
2515 EXPECT_EQ("the given field is greater than or equal to 0", Describe(m)); 2696 EXPECT_EQ("is an object whose given field is greater than or equal to 0",
2516 EXPECT_EQ("the given field is not greater than or equal to 0", 2697 Describe(m));
2698 EXPECT_EQ("is an object whose given field is not greater than or equal to 0",
2517 DescribeNegation(m)); 2699 DescribeNegation(m));
2518 } 2700 }
2519 2701
2520 // Tests that Field() can explain the match result. 2702 // Tests that Field() can explain the match result.
2521 TEST(FieldTest, CanExplainMatchResult) { 2703 TEST(FieldTest, CanExplainMatchResult) {
2522 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0)); 2704 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
2523 2705
2524 AStruct a; 2706 AStruct a;
2525 a.x = 1; 2707 a.x = 1;
2526 EXPECT_EQ("", Explain(m, a)); 2708 EXPECT_EQ("whose given field is 1", Explain(m, a));
2527 2709
2528 m = Field(&AStruct::x, GreaterThan(0)); 2710 m = Field(&AStruct::x, GreaterThan(0));
2529 EXPECT_EQ("the given field is 1 more than 0", Explain(m, a)); 2711 EXPECT_EQ("whose given field is 1, which is 1 more than 0", Explain(m, a));
2530 } 2712 }
2531 2713
2532 // Tests that Field() works when the argument is a pointer to const. 2714 // Tests that Field() works when the argument is a pointer to const.
2533 TEST(FieldForPointerTest, WorksForPointerToConst) { 2715 TEST(FieldForPointerTest, WorksForPointerToConst) {
2534 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0)); 2716 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2535 2717
2536 AStruct a; 2718 AStruct a;
2537 EXPECT_TRUE(m.Matches(&a)); 2719 EXPECT_TRUE(m.Matches(&a));
2538 a.x = -1; 2720 a.x = -1;
2539 EXPECT_FALSE(m.Matches(&a)); 2721 EXPECT_FALSE(m.Matches(&a));
2540 } 2722 }
2541 2723
2542 // Tests that Field() works when the argument is a pointer to non-const. 2724 // Tests that Field() works when the argument is a pointer to non-const.
2543 TEST(FieldForPointerTest, WorksForPointerToNonConst) { 2725 TEST(FieldForPointerTest, WorksForPointerToNonConst) {
2544 Matcher<AStruct*> m = Field(&AStruct::x, Ge(0)); 2726 Matcher<AStruct*> m = Field(&AStruct::x, Ge(0));
2545 2727
2546 AStruct a; 2728 AStruct a;
2547 EXPECT_TRUE(m.Matches(&a)); 2729 EXPECT_TRUE(m.Matches(&a));
2548 a.x = -1; 2730 a.x = -1;
2549 EXPECT_FALSE(m.Matches(&a)); 2731 EXPECT_FALSE(m.Matches(&a));
2550 } 2732 }
2551 2733
2734 // Tests that Field() works when the argument is a reference to a const pointer.
2735 TEST(FieldForPointerTest, WorksForReferenceToConstPointer) {
2736 Matcher<AStruct* const&> m = Field(&AStruct::x, Ge(0));
2737
2738 AStruct a;
2739 EXPECT_TRUE(m.Matches(&a));
2740 a.x = -1;
2741 EXPECT_FALSE(m.Matches(&a));
2742 }
2743
2552 // Tests that Field() does not match the NULL pointer. 2744 // Tests that Field() does not match the NULL pointer.
2553 TEST(FieldForPointerTest, DoesNotMatchNull) { 2745 TEST(FieldForPointerTest, DoesNotMatchNull) {
2554 Matcher<const AStruct*> m = Field(&AStruct::x, _); 2746 Matcher<const AStruct*> m = Field(&AStruct::x, _);
2555 EXPECT_FALSE(m.Matches(NULL)); 2747 EXPECT_FALSE(m.Matches(NULL));
2556 } 2748 }
2557 2749
2558 // Tests that Field(&Foo::field, ...) works when the argument's type 2750 // Tests that Field(&Foo::field, ...) works when the argument's type
2559 // is a sub-type of const Foo*. 2751 // is a sub-type of const Foo*.
2560 TEST(FieldForPointerTest, WorksForArgumentOfSubType) { 2752 TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
2561 // Note that the matcher expects DerivedStruct but we say AStruct 2753 // Note that the matcher expects DerivedStruct but we say AStruct
2562 // inside Field(). 2754 // inside Field().
2563 Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0)); 2755 Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0));
2564 2756
2565 DerivedStruct d; 2757 DerivedStruct d;
2566 EXPECT_TRUE(m.Matches(&d)); 2758 EXPECT_TRUE(m.Matches(&d));
2567 d.x = -1; 2759 d.x = -1;
2568 EXPECT_FALSE(m.Matches(&d)); 2760 EXPECT_FALSE(m.Matches(&d));
2569 } 2761 }
2570 2762
2571 // Tests that Field() can describe itself when used to match a pointer. 2763 // Tests that Field() can describe itself when used to match a pointer.
2572 TEST(FieldForPointerTest, CanDescribeSelf) { 2764 TEST(FieldForPointerTest, CanDescribeSelf) {
2573 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0)); 2765 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2574 2766
2575 EXPECT_EQ("the given field is greater than or equal to 0", Describe(m)); 2767 EXPECT_EQ("is an object whose given field is greater than or equal to 0",
2576 EXPECT_EQ("the given field is not greater than or equal to 0", 2768 Describe(m));
2769 EXPECT_EQ("is an object whose given field is not greater than or equal to 0",
2577 DescribeNegation(m)); 2770 DescribeNegation(m));
2578 } 2771 }
2579 2772
2580 // Tests that Field() can explain the result of matching a pointer. 2773 // Tests that Field() can explain the result of matching a pointer.
2581 TEST(FieldForPointerTest, CanExplainMatchResult) { 2774 TEST(FieldForPointerTest, CanExplainMatchResult) {
2582 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0)); 2775 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
2583 2776
2584 AStruct a; 2777 AStruct a;
2585 a.x = 1; 2778 a.x = 1;
2586 EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(NULL))); 2779 EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(NULL)));
2587 EXPECT_EQ("", Explain(m, &a)); 2780 EXPECT_EQ("which points to an object whose given field is 1", Explain(m, &a));
2588 2781
2589 m = Field(&AStruct::x, GreaterThan(0)); 2782 m = Field(&AStruct::x, GreaterThan(0));
2590 EXPECT_EQ("the given field is 1 more than 0", Explain(m, &a)); 2783 EXPECT_EQ("which points to an object whose given field is 1, "
2784 "which is 1 more than 0", Explain(m, &a));
2591 } 2785 }
2592 2786
2593 // A user-defined class for testing Property(). 2787 // A user-defined class for testing Property().
2594 class AClass { 2788 class AClass {
2595 public: 2789 public:
2596 AClass() : n_(0) {} 2790 AClass() : n_(0) {}
2597 2791
2598 // A getter that returns a non-reference. 2792 // A getter that returns a non-reference.
2599 int n() const { return n_; } 2793 int n() const { return n_; }
2600 2794
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
2699 AClass a; 2893 AClass a;
2700 EXPECT_TRUE(m.Matches(a)); 2894 EXPECT_TRUE(m.Matches(a));
2701 a.set_n(-1); 2895 a.set_n(-1);
2702 EXPECT_FALSE(m.Matches(a)); 2896 EXPECT_FALSE(m.Matches(a));
2703 } 2897 }
2704 2898
2705 // Tests that Property() can describe itself. 2899 // Tests that Property() can describe itself.
2706 TEST(PropertyTest, CanDescribeSelf) { 2900 TEST(PropertyTest, CanDescribeSelf) {
2707 Matcher<const AClass&> m = Property(&AClass::n, Ge(0)); 2901 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
2708 2902
2709 EXPECT_EQ("the given property is greater than or equal to 0", Describe(m)); 2903 EXPECT_EQ("is an object whose given property is greater than or equal to 0",
2710 EXPECT_EQ("the given property is not greater than or equal to 0", 2904 Describe(m));
2711 DescribeNegation(m)); 2905 EXPECT_EQ("is an object whose given property "
2906 "is not greater than or equal to 0", DescribeNegation(m));
2712 } 2907 }
2713 2908
2714 // Tests that Property() can explain the match result. 2909 // Tests that Property() can explain the match result.
2715 TEST(PropertyTest, CanExplainMatchResult) { 2910 TEST(PropertyTest, CanExplainMatchResult) {
2716 Matcher<const AClass&> m = Property(&AClass::n, Ge(0)); 2911 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
2717 2912
2718 AClass a; 2913 AClass a;
2719 a.set_n(1); 2914 a.set_n(1);
2720 EXPECT_EQ("", Explain(m, a)); 2915 EXPECT_EQ("whose given property is 1", Explain(m, a));
2721 2916
2722 m = Property(&AClass::n, GreaterThan(0)); 2917 m = Property(&AClass::n, GreaterThan(0));
2723 EXPECT_EQ("the given property is 1 more than 0", Explain(m, a)); 2918 EXPECT_EQ("whose given property is 1, which is 1 more than 0", Explain(m, a));
2724 } 2919 }
2725 2920
2726 // Tests that Property() works when the argument is a pointer to const. 2921 // Tests that Property() works when the argument is a pointer to const.
2727 TEST(PropertyForPointerTest, WorksForPointerToConst) { 2922 TEST(PropertyForPointerTest, WorksForPointerToConst) {
2728 Matcher<const AClass*> m = Property(&AClass::n, Ge(0)); 2923 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
2729 2924
2730 AClass a; 2925 AClass a;
2731 a.set_n(1); 2926 a.set_n(1);
2732 EXPECT_TRUE(m.Matches(&a)); 2927 EXPECT_TRUE(m.Matches(&a));
2733 2928
2734 a.set_n(-1); 2929 a.set_n(-1);
2735 EXPECT_FALSE(m.Matches(&a)); 2930 EXPECT_FALSE(m.Matches(&a));
2736 } 2931 }
2737 2932
2738 // Tests that Property() works when the argument is a pointer to non-const. 2933 // Tests that Property() works when the argument is a pointer to non-const.
2739 TEST(PropertyForPointerTest, WorksForPointerToNonConst) { 2934 TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
2740 Matcher<AClass*> m = Property(&AClass::s, StartsWith("hi")); 2935 Matcher<AClass*> m = Property(&AClass::s, StartsWith("hi"));
2741 2936
2742 AClass a; 2937 AClass a;
2743 a.set_s("hill"); 2938 a.set_s("hill");
2744 EXPECT_TRUE(m.Matches(&a)); 2939 EXPECT_TRUE(m.Matches(&a));
2745 2940
2746 a.set_s("hole"); 2941 a.set_s("hole");
2747 EXPECT_FALSE(m.Matches(&a)); 2942 EXPECT_FALSE(m.Matches(&a));
2748 } 2943 }
2749 2944
2945 // Tests that Property() works when the argument is a reference to a
2946 // const pointer.
2947 TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) {
2948 Matcher<AClass* const&> m = Property(&AClass::s, StartsWith("hi"));
2949
2950 AClass a;
2951 a.set_s("hill");
2952 EXPECT_TRUE(m.Matches(&a));
2953
2954 a.set_s("hole");
2955 EXPECT_FALSE(m.Matches(&a));
2956 }
2957
2750 // Tests that Property() does not match the NULL pointer. 2958 // Tests that Property() does not match the NULL pointer.
2751 TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) { 2959 TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
2752 Matcher<const AClass*> m = Property(&AClass::x, _); 2960 Matcher<const AClass*> m = Property(&AClass::x, _);
2753 EXPECT_FALSE(m.Matches(NULL)); 2961 EXPECT_FALSE(m.Matches(NULL));
2754 } 2962 }
2755 2963
2756 // Tests that Property(&Foo::property, ...) works when the argument's 2964 // Tests that Property(&Foo::property, ...) works when the argument's
2757 // type is a sub-type of const Foo*. 2965 // type is a sub-type of const Foo*.
2758 TEST(PropertyForPointerTest, WorksForArgumentOfSubType) { 2966 TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
2759 // The matcher expects a DerivedClass, but inside the Property() we 2967 // The matcher expects a DerivedClass, but inside the Property() we
2760 // say AClass. 2968 // say AClass.
2761 Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0)); 2969 Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0));
2762 2970
2763 DerivedClass d; 2971 DerivedClass d;
2764 d.set_n(1); 2972 d.set_n(1);
2765 EXPECT_TRUE(m.Matches(&d)); 2973 EXPECT_TRUE(m.Matches(&d));
2766 2974
2767 d.set_n(-1); 2975 d.set_n(-1);
2768 EXPECT_FALSE(m.Matches(&d)); 2976 EXPECT_FALSE(m.Matches(&d));
2769 } 2977 }
2770 2978
2771 // Tests that Property() can describe itself when used to match a pointer. 2979 // Tests that Property() can describe itself when used to match a pointer.
2772 TEST(PropertyForPointerTest, CanDescribeSelf) { 2980 TEST(PropertyForPointerTest, CanDescribeSelf) {
2773 Matcher<const AClass*> m = Property(&AClass::n, Ge(0)); 2981 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
2774 2982
2775 EXPECT_EQ("the given property is greater than or equal to 0", Describe(m)); 2983 EXPECT_EQ("is an object whose given property is greater than or equal to 0",
2776 EXPECT_EQ("the given property is not greater than or equal to 0", 2984 Describe(m));
2777 DescribeNegation(m)); 2985 EXPECT_EQ("is an object whose given property "
2986 "is not greater than or equal to 0", DescribeNegation(m));
2778 } 2987 }
2779 2988
2780 // Tests that Property() can explain the result of matching a pointer. 2989 // Tests that Property() can explain the result of matching a pointer.
2781 TEST(PropertyForPointerTest, CanExplainMatchResult) { 2990 TEST(PropertyForPointerTest, CanExplainMatchResult) {
2782 Matcher<const AClass*> m = Property(&AClass::n, Ge(0)); 2991 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
2783 2992
2784 AClass a; 2993 AClass a;
2785 a.set_n(1); 2994 a.set_n(1);
2786 EXPECT_EQ("", Explain(m, static_cast<const AClass*>(NULL))); 2995 EXPECT_EQ("", Explain(m, static_cast<const AClass*>(NULL)));
2787 EXPECT_EQ("", Explain(m, &a)); 2996 EXPECT_EQ("which points to an object whose given property is 1",
2997 Explain(m, &a));
2788 2998
2789 m = Property(&AClass::n, GreaterThan(0)); 2999 m = Property(&AClass::n, GreaterThan(0));
2790 EXPECT_EQ("the given property is 1 more than 0", Explain(m, &a)); 3000 EXPECT_EQ("which points to an object whose given property is 1, "
3001 "which is 1 more than 0", Explain(m, &a));
2791 } 3002 }
2792 3003
2793 // Tests ResultOf. 3004 // Tests ResultOf.
2794 3005
2795 // Tests that ResultOf(f, ...) compiles and works as expected when f is a 3006 // Tests that ResultOf(f, ...) compiles and works as expected when f is a
2796 // function pointer. 3007 // function pointer.
2797 string IntToStringFunction(int input) { return input == 1 ? "foo" : "bar"; } 3008 string IntToStringFunction(int input) { return input == 1 ? "foo" : "bar"; }
2798 3009
2799 TEST(ResultOfTest, WorksForFunctionPointers) { 3010 TEST(ResultOfTest, WorksForFunctionPointers) {
2800 Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(string("foo"))); 3011 Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(string("foo")));
2801 3012
2802 EXPECT_TRUE(matcher.Matches(1)); 3013 EXPECT_TRUE(matcher.Matches(1));
2803 EXPECT_FALSE(matcher.Matches(2)); 3014 EXPECT_FALSE(matcher.Matches(2));
2804 } 3015 }
2805 3016
2806 // Tests that ResultOf() can describe itself. 3017 // Tests that ResultOf() can describe itself.
2807 TEST(ResultOfTest, CanDescribeItself) { 3018 TEST(ResultOfTest, CanDescribeItself) {
2808 Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo")); 3019 Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo"));
2809 3020
2810 EXPECT_EQ("result of the given callable is equal to \"foo\"", 3021 EXPECT_EQ("is mapped by the given callable to a value that "
2811 Describe(matcher)); 3022 "is equal to \"foo\"", Describe(matcher));
2812 EXPECT_EQ("result of the given callable is not equal to \"foo\"", 3023 EXPECT_EQ("is mapped by the given callable to a value that "
2813 DescribeNegation(matcher)); 3024 "is not equal to \"foo\"", DescribeNegation(matcher));
2814 } 3025 }
2815 3026
2816 // Tests that ResultOf() can explain the match result. 3027 // Tests that ResultOf() can explain the match result.
2817 int IntFunction(int input) { return input == 42 ? 80 : 90; } 3028 int IntFunction(int input) { return input == 42 ? 80 : 90; }
2818 3029
2819 TEST(ResultOfTest, CanExplainMatchResult) { 3030 TEST(ResultOfTest, CanExplainMatchResult) {
2820 Matcher<int> matcher = ResultOf(&IntFunction, Ge(85)); 3031 Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
2821 EXPECT_EQ("", Explain(matcher, 36)); 3032 EXPECT_EQ("which is mapped by the given callable to 90",
3033 Explain(matcher, 36));
2822 3034
2823 matcher = ResultOf(&IntFunction, GreaterThan(85)); 3035 matcher = ResultOf(&IntFunction, GreaterThan(85));
2824 EXPECT_EQ("result of the given callable is 5 more than 85", 3036 EXPECT_EQ("which is mapped by the given callable to 90, "
2825 Explain(matcher, 36)); 3037 "which is 5 more than 85", Explain(matcher, 36));
2826 } 3038 }
2827 3039
2828 // Tests that ResultOf(f, ...) compiles and works as expected when f(x) 3040 // Tests that ResultOf(f, ...) compiles and works as expected when f(x)
2829 // returns a non-reference. 3041 // returns a non-reference.
2830 TEST(ResultOfTest, WorksForNonReferenceResults) { 3042 TEST(ResultOfTest, WorksForNonReferenceResults) {
2831 Matcher<int> matcher = ResultOf(&IntFunction, Eq(80)); 3043 Matcher<int> matcher = ResultOf(&IntFunction, Eq(80));
2832 3044
2833 EXPECT_TRUE(matcher.Matches(42)); 3045 EXPECT_TRUE(matcher.Matches(42));
2834 EXPECT_FALSE(matcher.Matches(36)); 3046 EXPECT_FALSE(matcher.Matches(36));
2835 } 3047 }
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
2951 3163
2952 Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n)); 3164 Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n));
2953 EXPECT_TRUE(matcher3.Matches(n)); 3165 EXPECT_TRUE(matcher3.Matches(n));
2954 EXPECT_FALSE(matcher3.Matches(n2)); 3166 EXPECT_FALSE(matcher3.Matches(n2));
2955 } 3167 }
2956 3168
2957 class DivisibleByImpl { 3169 class DivisibleByImpl {
2958 public: 3170 public:
2959 explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {} 3171 explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {}
2960 3172
3173 // For testing using ExplainMatchResultTo() with polymorphic matchers.
2961 template <typename T> 3174 template <typename T>
2962 bool Matches(const T& n) const { 3175 bool MatchAndExplain(const T& n, MatchResultListener* listener) const {
3176 *listener << "is " << (n % divider_) << " modulo "
3177 << divider_;
2963 return (n % divider_) == 0; 3178 return (n % divider_) == 0;
2964 } 3179 }
2965 3180
2966 void DescribeTo(::std::ostream* os) const { 3181 void DescribeTo(::std::ostream* os) const {
2967 *os << "is divisible by " << divider_; 3182 *os << "is divisible by " << divider_;
2968 } 3183 }
2969 3184
2970 void DescribeNegationTo(::std::ostream* os) const { 3185 void DescribeNegationTo(::std::ostream* os) const {
2971 *os << "is not divisible by " << divider_; 3186 *os << "is not divisible by " << divider_;
2972 } 3187 }
2973 3188
2974 void set_divider(int a_divider) { divider_ = a_divider; } 3189 void set_divider(int a_divider) { divider_ = a_divider; }
2975 int divider() const { return divider_; } 3190 int divider() const { return divider_; }
2976 3191
2977 private: 3192 private:
2978 int divider_; 3193 int divider_;
2979 }; 3194 };
2980 3195
2981 // For testing using ExplainMatchResultTo() with polymorphic matchers.
2982 template <typename T>
2983 void ExplainMatchResultTo(const DivisibleByImpl& impl, const T& n,
2984 ::std::ostream* os) {
2985 *os << "is " << (n % impl.divider()) << " modulo "
2986 << impl.divider();
2987 }
2988
2989 PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) { 3196 PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) {
2990 return MakePolymorphicMatcher(DivisibleByImpl(n)); 3197 return MakePolymorphicMatcher(DivisibleByImpl(n));
2991 } 3198 }
2992 3199
2993 // Tests that when AllOf() fails, only the first failing matcher is 3200 // Tests that when AllOf() fails, only the first failing matcher is
2994 // asked to explain why. 3201 // asked to explain why.
2995 TEST(ExplainMatchResultTest, AllOf_False_False) { 3202 TEST(ExplainMatchResultTest, AllOf_False_False) {
2996 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3)); 3203 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
2997 EXPECT_EQ("is 1 modulo 4", Explain(m, 5)); 3204 EXPECT_EQ("is 1 modulo 4", Explain(m, 5));
2998 } 3205 }
(...skipping 19 matching lines...) Expand all
3018 EXPECT_EQ("is 0 modulo 2; is 0 modulo 3", Explain(m, 6)); 3225 EXPECT_EQ("is 0 modulo 2; is 0 modulo 3", Explain(m, 6));
3019 } 3226 }
3020 3227
3021 TEST(ExplainMatchResultTest, AllOf_True_True_2) { 3228 TEST(ExplainMatchResultTest, AllOf_True_True_2) {
3022 const Matcher<int> m = AllOf(Ge(2), Le(3)); 3229 const Matcher<int> m = AllOf(Ge(2), Le(3));
3023 EXPECT_EQ("", Explain(m, 2)); 3230 EXPECT_EQ("", Explain(m, 2));
3024 } 3231 }
3025 3232
3026 TEST(ExplainmatcherResultTest, MonomorphicMatcher) { 3233 TEST(ExplainmatcherResultTest, MonomorphicMatcher) {
3027 const Matcher<int> m = GreaterThan(5); 3234 const Matcher<int> m = GreaterThan(5);
3028 EXPECT_EQ("is 1 more than 5", Explain(m, 6)); 3235 EXPECT_EQ("which is 1 more than 5", Explain(m, 6));
3029 } 3236 }
3030 3237
3031 // The following two tests verify that values without a public copy 3238 // The following two tests verify that values without a public copy
3032 // ctor can be used as arguments to matchers like Eq(), Ge(), and etc 3239 // ctor can be used as arguments to matchers like Eq(), Ge(), and etc
3033 // with the help of ByRef(). 3240 // with the help of ByRef().
3034 3241
3035 class NotCopyable { 3242 class NotCopyable {
3036 public: 3243 public:
3037 explicit NotCopyable(int a_value) : value_(a_value) {} 3244 explicit NotCopyable(int a_value) : value_(a_value) {}
3038 3245
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after
3328 ElementsAre()); 3535 ElementsAre());
3329 } 3536 }
3330 3537
3331 TEST(ValidateMatcherDescriptionTest, 3538 TEST(ValidateMatcherDescriptionTest,
3332 AcceptsNonEmptyDescriptionWithNoInterpolation) { 3539 AcceptsNonEmptyDescriptionWithNoInterpolation) {
3333 const char* params[] = { "foo", "bar", NULL }; 3540 const char* params[] = { "foo", "bar", NULL };
3334 EXPECT_THAT(ValidateMatcherDescription(params, "a simple description"), 3541 EXPECT_THAT(ValidateMatcherDescription(params, "a simple description"),
3335 ElementsAre()); 3542 ElementsAre());
3336 } 3543 }
3337 3544
3545 // The MATCHER*() macros trigger warning C4100 (unreferenced formal
3546 // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
3547 // the macro definition, as the warnings are generated when the macro
3548 // is expanded and macro expansion cannot contain #pragma. Therefore
3549 // we suppress them here.
3550 #ifdef _MSC_VER
3551 #pragma warning(push)
3552 #pragma warning(disable:4100)
3553 #endif
3554
3338 // We use MATCHER_P3() to define a matcher for testing 3555 // We use MATCHER_P3() to define a matcher for testing
3339 // ValidateMatcherDescription(); otherwise we'll end up with much 3556 // ValidateMatcherDescription(); otherwise we'll end up with much
3340 // plumbing code. This is not circular as 3557 // plumbing code. This is not circular as
3341 // ValidateMatcherDescription() doesn't affect whether the matcher 3558 // ValidateMatcherDescription() doesn't affect whether the matcher
3342 // matches a value or not. 3559 // matches a value or not.
3343 MATCHER_P3(EqInterpolation, start, end, index, "equals Interpolation%(*)s") { 3560 MATCHER_P3(EqInterpolation, start, end, index, "equals Interpolation%(*)s") {
3344 return arg.start_pos == start && arg.end_pos == end && 3561 return arg.start_pos == start && arg.end_pos == end &&
3345 arg.param_index == index; 3562 arg.param_index == index;
3346 } 3563 }
3347 3564
3565 #ifdef _MSC_VER
3566 #pragma warning(pop)
3567 #endif
3568
3348 TEST(ValidateMatcherDescriptionTest, AcceptsPercentInterpolation) { 3569 TEST(ValidateMatcherDescriptionTest, AcceptsPercentInterpolation) {
3349 const char* params[] = { "foo", NULL }; 3570 const char* params[] = { "foo", NULL };
3350 const char* const desc = "one %%"; 3571 const char* const desc = "one %%";
3351 EXPECT_THAT(ValidateMatcherDescription(params, desc), 3572 EXPECT_THAT(ValidateMatcherDescription(params, desc),
3352 ElementsAre(EqInterpolation(desc + 4, desc + 6, 3573 ElementsAre(EqInterpolation(desc + 4, desc + 6,
3353 kPercentInterpolation))); 3574 kPercentInterpolation)));
3354 } 3575 }
3355 3576
3356 TEST(ValidateMatcherDescriptionTest, AcceptsTupleInterpolation) { 3577 TEST(ValidateMatcherDescriptionTest, AcceptsTupleInterpolation) {
3357 const char* params[] = { "foo", "bar", "baz", NULL }; 3578 const char* params[] = { "foo", "bar", "baz", NULL };
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
3569 3790
3570 // Tests PolymorphicMatcher::impl(). 3791 // Tests PolymorphicMatcher::impl().
3571 TEST(PolymorphicMatcherTest, CanAccessImpl) { 3792 TEST(PolymorphicMatcherTest, CanAccessImpl) {
3572 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42)); 3793 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
3573 const DivisibleByImpl& impl = m.impl(); 3794 const DivisibleByImpl& impl = m.impl();
3574 EXPECT_EQ(42, impl.divider()); 3795 EXPECT_EQ(42, impl.divider());
3575 } 3796 }
3576 3797
3577 } // namespace gmock_matchers_test 3798 } // namespace gmock_matchers_test
3578 } // namespace testing 3799 } // namespace testing
OLDNEW
« no previous file with comments | « testing/gmock/test/gmock-internal-utils_test.cc ('k') | testing/gmock/test/gmock-nice-strict_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698