OLD | NEW |
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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 namespace internal { | 54 namespace internal { |
55 string FormatMatcherDescriptionSyntaxError(const char* description, | 55 string FormatMatcherDescriptionSyntaxError(const char* description, |
56 const char* error_pos); | 56 const char* error_pos); |
57 int GetParamIndex(const char* param_names[], const string& param_name); | 57 int GetParamIndex(const char* param_names[], const string& param_name); |
58 string JoinAsTuple(const Strings& fields); | 58 string JoinAsTuple(const Strings& fields); |
59 bool SkipPrefix(const char* prefix, const char** pstr); | 59 bool SkipPrefix(const char* prefix, const char** pstr); |
60 } // namespace internal | 60 } // namespace internal |
61 | 61 |
62 namespace gmock_matchers_test { | 62 namespace gmock_matchers_test { |
63 | 63 |
| 64 using std::list; |
64 using std::make_pair; | 65 using std::make_pair; |
65 using std::map; | 66 using std::map; |
66 using std::multimap; | 67 using std::multimap; |
| 68 using std::multiset; |
| 69 using std::ostream; |
67 using std::pair; | 70 using std::pair; |
| 71 using std::set; |
68 using std::stringstream; | 72 using std::stringstream; |
| 73 using std::tr1::get; |
69 using std::tr1::make_tuple; | 74 using std::tr1::make_tuple; |
| 75 using std::tr1::tuple; |
| 76 using std::vector; |
70 using testing::A; | 77 using testing::A; |
71 using testing::AllArgs; | 78 using testing::AllArgs; |
72 using testing::AllOf; | 79 using testing::AllOf; |
73 using testing::An; | 80 using testing::An; |
74 using testing::AnyOf; | 81 using testing::AnyOf; |
75 using testing::ByRef; | 82 using testing::ByRef; |
76 using testing::ContainsRegex; | 83 using testing::ContainsRegex; |
77 using testing::DoubleEq; | 84 using testing::DoubleEq; |
78 using testing::EndsWith; | 85 using testing::EndsWith; |
79 using testing::Eq; | 86 using testing::Eq; |
(...skipping 15 matching lines...) Expand all Loading... |
95 using testing::MatcherInterface; | 102 using testing::MatcherInterface; |
96 using testing::Matches; | 103 using testing::Matches; |
97 using testing::MatchesRegex; | 104 using testing::MatchesRegex; |
98 using testing::NanSensitiveDoubleEq; | 105 using testing::NanSensitiveDoubleEq; |
99 using testing::NanSensitiveFloatEq; | 106 using testing::NanSensitiveFloatEq; |
100 using testing::Ne; | 107 using testing::Ne; |
101 using testing::Not; | 108 using testing::Not; |
102 using testing::NotNull; | 109 using testing::NotNull; |
103 using testing::Pair; | 110 using testing::Pair; |
104 using testing::Pointee; | 111 using testing::Pointee; |
| 112 using testing::Pointwise; |
105 using testing::PolymorphicMatcher; | 113 using testing::PolymorphicMatcher; |
106 using testing::Property; | 114 using testing::Property; |
107 using testing::Ref; | 115 using testing::Ref; |
108 using testing::ResultOf; | 116 using testing::ResultOf; |
109 using testing::StartsWith; | 117 using testing::StartsWith; |
110 using testing::StrCaseEq; | 118 using testing::StrCaseEq; |
111 using testing::StrCaseNe; | 119 using testing::StrCaseNe; |
112 using testing::StrEq; | 120 using testing::StrEq; |
113 using testing::StrNe; | 121 using testing::StrNe; |
114 using testing::Truly; | 122 using testing::Truly; |
(...skipping 20 matching lines...) Expand all Loading... |
135 using testing::internal::kTupleInterpolation; | 143 using testing::internal::kTupleInterpolation; |
136 using testing::internal::linked_ptr; | 144 using testing::internal::linked_ptr; |
137 using testing::internal::scoped_ptr; | 145 using testing::internal::scoped_ptr; |
138 using testing::internal::string; | 146 using testing::internal::string; |
139 | 147 |
140 // For testing ExplainMatchResultTo(). | 148 // For testing ExplainMatchResultTo(). |
141 class GreaterThanMatcher : public MatcherInterface<int> { | 149 class GreaterThanMatcher : public MatcherInterface<int> { |
142 public: | 150 public: |
143 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {} | 151 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {} |
144 | 152 |
145 virtual void DescribeTo(::std::ostream* os) const { | 153 virtual void DescribeTo(ostream* os) const { |
146 *os << "is > " << rhs_; | 154 *os << "is > " << rhs_; |
147 } | 155 } |
148 | 156 |
149 virtual bool MatchAndExplain(int lhs, | 157 virtual bool MatchAndExplain(int lhs, |
150 MatchResultListener* listener) const { | 158 MatchResultListener* listener) const { |
151 const int diff = lhs - rhs_; | 159 const int diff = lhs - rhs_; |
152 if (diff > 0) { | 160 if (diff > 0) { |
153 *listener << "which is " << diff << " more than " << rhs_; | 161 *listener << "which is " << diff << " more than " << rhs_; |
154 } else if (diff == 0) { | 162 } else if (diff == 0) { |
155 *listener << "which is the same as " << rhs_; | 163 *listener << "which is the same as " << rhs_; |
(...skipping 24 matching lines...) Expand all Loading... |
180 template <typename T> | 188 template <typename T> |
181 string DescribeNegation(const Matcher<T>& m) { | 189 string DescribeNegation(const Matcher<T>& m) { |
182 stringstream ss; | 190 stringstream ss; |
183 m.DescribeNegationTo(&ss); | 191 m.DescribeNegationTo(&ss); |
184 return ss.str(); | 192 return ss.str(); |
185 } | 193 } |
186 | 194 |
187 // Returns the reason why x matches, or doesn't match, m. | 195 // Returns the reason why x matches, or doesn't match, m. |
188 template <typename MatcherType, typename Value> | 196 template <typename MatcherType, typename Value> |
189 string Explain(const MatcherType& m, const Value& x) { | 197 string Explain(const MatcherType& m, const Value& x) { |
190 stringstream ss; | 198 StringMatchResultListener listener; |
191 m.ExplainMatchResultTo(x, &ss); | 199 ExplainMatchResult(m, x, &listener); |
192 return ss.str(); | 200 return listener.str(); |
193 } | 201 } |
194 | 202 |
195 TEST(MatchResultListenerTest, StreamingWorks) { | 203 TEST(MatchResultListenerTest, StreamingWorks) { |
196 StringMatchResultListener listener; | 204 StringMatchResultListener listener; |
197 listener << "hi" << 5; | 205 listener << "hi" << 5; |
198 EXPECT_EQ("hi5", listener.str()); | 206 EXPECT_EQ("hi5", listener.str()); |
199 | 207 |
200 // Streaming shouldn't crash when the underlying ostream is NULL. | 208 // Streaming shouldn't crash when the underlying ostream is NULL. |
201 DummyMatchResultListener dummy; | 209 DummyMatchResultListener dummy; |
202 dummy << "hi" << 5; | 210 dummy << "hi" << 5; |
(...skipping 16 matching lines...) Expand all Loading... |
219 | 227 |
220 // Makes sure that the MatcherInterface<T> interface doesn't | 228 // Makes sure that the MatcherInterface<T> interface doesn't |
221 // change. | 229 // change. |
222 class EvenMatcherImpl : public MatcherInterface<int> { | 230 class EvenMatcherImpl : public MatcherInterface<int> { |
223 public: | 231 public: |
224 virtual bool MatchAndExplain(int x, | 232 virtual bool MatchAndExplain(int x, |
225 MatchResultListener* /* listener */) const { | 233 MatchResultListener* /* listener */) const { |
226 return x % 2 == 0; | 234 return x % 2 == 0; |
227 } | 235 } |
228 | 236 |
229 virtual void DescribeTo(::std::ostream* os) const { | 237 virtual void DescribeTo(ostream* os) const { |
230 *os << "is an even number"; | 238 *os << "is an even number"; |
231 } | 239 } |
232 | 240 |
233 // We deliberately don't define DescribeNegationTo() and | 241 // We deliberately don't define DescribeNegationTo() and |
234 // ExplainMatchResultTo() here, to make sure the definition of these | 242 // ExplainMatchResultTo() here, to make sure the definition of these |
235 // two methods is optional. | 243 // two methods is optional. |
236 }; | 244 }; |
237 | 245 |
238 // Makes sure that the MatcherInterface API doesn't change. | 246 // Makes sure that the MatcherInterface API doesn't change. |
239 TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) { | 247 TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) { |
240 EvenMatcherImpl m; | 248 EvenMatcherImpl m; |
241 } | 249 } |
242 | 250 |
243 // Tests implementing a monomorphic matcher using MatchAndExplain(). | 251 // Tests implementing a monomorphic matcher using MatchAndExplain(). |
244 | 252 |
245 class NewEvenMatcherImpl : public MatcherInterface<int> { | 253 class NewEvenMatcherImpl : public MatcherInterface<int> { |
246 public: | 254 public: |
247 virtual bool MatchAndExplain(int x, MatchResultListener* listener) const { | 255 virtual bool MatchAndExplain(int x, MatchResultListener* listener) const { |
248 const bool match = x % 2 == 0; | 256 const bool match = x % 2 == 0; |
249 // Verifies that we can stream to a listener directly. | 257 // Verifies that we can stream to a listener directly. |
250 *listener << "value % " << 2; | 258 *listener << "value % " << 2; |
251 if (listener->stream() != NULL) { | 259 if (listener->stream() != NULL) { |
252 // Verifies that we can stream to a listener's underlying stream | 260 // Verifies that we can stream to a listener's underlying stream |
253 // too. | 261 // too. |
254 *listener->stream() << " == " << (x % 2); | 262 *listener->stream() << " == " << (x % 2); |
255 } | 263 } |
256 return match; | 264 return match; |
257 } | 265 } |
258 | 266 |
259 virtual void DescribeTo(::std::ostream* os) const { | 267 virtual void DescribeTo(ostream* os) const { |
260 *os << "is an even number"; | 268 *os << "is an even number"; |
261 } | 269 } |
262 }; | 270 }; |
263 | 271 |
264 TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) { | 272 TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) { |
265 Matcher<int> m = MakeMatcher(new NewEvenMatcherImpl); | 273 Matcher<int> m = MakeMatcher(new NewEvenMatcherImpl); |
266 EXPECT_TRUE(m.Matches(2)); | 274 EXPECT_TRUE(m.Matches(2)); |
267 EXPECT_FALSE(m.Matches(3)); | 275 EXPECT_FALSE(m.Matches(3)); |
268 EXPECT_EQ("value % 2 == 0", Explain(m, 2)); | 276 EXPECT_EQ("value % 2 == 0", Explain(m, 2)); |
269 EXPECT_EQ("value % 2 == 1", Explain(m, 3)); | 277 EXPECT_EQ("value % 2 == 1", Explain(m, 3)); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 // Tests that MakeMatcher() constructs a Matcher<T> from a | 364 // Tests that MakeMatcher() constructs a Matcher<T> from a |
357 // MatcherInterface* without requiring the user to explicitly | 365 // MatcherInterface* without requiring the user to explicitly |
358 // write the type. | 366 // write the type. |
359 TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) { | 367 TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) { |
360 const MatcherInterface<int>* dummy_impl = NULL; | 368 const MatcherInterface<int>* dummy_impl = NULL; |
361 Matcher<int> m = MakeMatcher(dummy_impl); | 369 Matcher<int> m = MakeMatcher(dummy_impl); |
362 } | 370 } |
363 | 371 |
364 // Tests that MakePolymorphicMatcher() can construct a polymorphic | 372 // Tests that MakePolymorphicMatcher() can construct a polymorphic |
365 // matcher from its implementation using the old API. | 373 // matcher from its implementation using the old API. |
366 const int bar = 1; | 374 const int g_bar = 1; |
367 class ReferencesBarOrIsZeroImpl { | 375 class ReferencesBarOrIsZeroImpl { |
368 public: | 376 public: |
369 template <typename T> | 377 template <typename T> |
370 bool MatchAndExplain(const T& x, | 378 bool MatchAndExplain(const T& x, |
371 MatchResultListener* /* listener */) const { | 379 MatchResultListener* /* listener */) const { |
372 const void* p = &x; | 380 const void* p = &x; |
373 return p == &bar || x == 0; | 381 return p == &g_bar || x == 0; |
374 } | 382 } |
375 | 383 |
376 void DescribeTo(::std::ostream* os) const { *os << "bar or zero"; } | 384 void DescribeTo(ostream* os) const { *os << "g_bar or zero"; } |
377 | 385 |
378 void DescribeNegationTo(::std::ostream* os) const { | 386 void DescribeNegationTo(ostream* os) const { |
379 *os << "doesn't reference bar and is not zero"; | 387 *os << "doesn't reference g_bar and is not zero"; |
380 } | 388 } |
381 }; | 389 }; |
382 | 390 |
383 // This function verifies that MakePolymorphicMatcher() returns a | 391 // This function verifies that MakePolymorphicMatcher() returns a |
384 // PolymorphicMatcher<T> where T is the argument's type. | 392 // PolymorphicMatcher<T> where T is the argument's type. |
385 PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() { | 393 PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() { |
386 return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl()); | 394 return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl()); |
387 } | 395 } |
388 | 396 |
389 TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) { | 397 TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) { |
390 // Using a polymorphic matcher to match a reference type. | 398 // Using a polymorphic matcher to match a reference type. |
391 Matcher<const int&> m1 = ReferencesBarOrIsZero(); | 399 Matcher<const int&> m1 = ReferencesBarOrIsZero(); |
392 EXPECT_TRUE(m1.Matches(0)); | 400 EXPECT_TRUE(m1.Matches(0)); |
393 // Verifies that the identity of a by-reference argument is preserved. | 401 // Verifies that the identity of a by-reference argument is preserved. |
394 EXPECT_TRUE(m1.Matches(bar)); | 402 EXPECT_TRUE(m1.Matches(g_bar)); |
395 EXPECT_FALSE(m1.Matches(1)); | 403 EXPECT_FALSE(m1.Matches(1)); |
396 EXPECT_EQ("bar or zero", Describe(m1)); | 404 EXPECT_EQ("g_bar or zero", Describe(m1)); |
397 | 405 |
398 // Using a polymorphic matcher to match a value type. | 406 // Using a polymorphic matcher to match a value type. |
399 Matcher<double> m2 = ReferencesBarOrIsZero(); | 407 Matcher<double> m2 = ReferencesBarOrIsZero(); |
400 EXPECT_TRUE(m2.Matches(0.0)); | 408 EXPECT_TRUE(m2.Matches(0.0)); |
401 EXPECT_FALSE(m2.Matches(0.1)); | 409 EXPECT_FALSE(m2.Matches(0.1)); |
402 EXPECT_EQ("bar or zero", Describe(m2)); | 410 EXPECT_EQ("g_bar or zero", Describe(m2)); |
403 } | 411 } |
404 | 412 |
405 // Tests implementing a polymorphic matcher using MatchAndExplain(). | 413 // Tests implementing a polymorphic matcher using MatchAndExplain(). |
406 | 414 |
407 class PolymorphicIsEvenImpl { | 415 class PolymorphicIsEvenImpl { |
408 public: | 416 public: |
409 void DescribeTo(::std::ostream* os) const { *os << "is even"; } | 417 void DescribeTo(ostream* os) const { *os << "is even"; } |
410 | 418 |
411 void DescribeNegationTo(::std::ostream* os) const { | 419 void DescribeNegationTo(ostream* os) const { |
412 *os << "is odd"; | 420 *os << "is odd"; |
413 } | 421 } |
414 | 422 |
415 template <typename T> | 423 template <typename T> |
416 bool MatchAndExplain(const T& x, MatchResultListener* listener) const { | 424 bool MatchAndExplain(const T& x, MatchResultListener* listener) const { |
417 // Verifies that we can stream to the listener directly. | 425 // Verifies that we can stream to the listener directly. |
418 *listener << "% " << 2; | 426 *listener << "% " << 2; |
419 if (listener->stream() != NULL) { | 427 if (listener->stream() != NULL) { |
420 // Verifies that we can stream to the listener's underlying stream | 428 // Verifies that we can stream to the listener's underlying stream |
421 // too. | 429 // too. |
(...skipping 719 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1141 | 1149 |
1142 TEST(KeyTest, SafelyCastsInnerMatcher) { | 1150 TEST(KeyTest, SafelyCastsInnerMatcher) { |
1143 Matcher<int> is_positive = Gt(0); | 1151 Matcher<int> is_positive = Gt(0); |
1144 Matcher<int> is_negative = Lt(0); | 1152 Matcher<int> is_negative = Lt(0); |
1145 pair<char, bool> p('a', true); | 1153 pair<char, bool> p('a', true); |
1146 EXPECT_THAT(p, Key(is_positive)); | 1154 EXPECT_THAT(p, Key(is_positive)); |
1147 EXPECT_THAT(p, Not(Key(is_negative))); | 1155 EXPECT_THAT(p, Not(Key(is_negative))); |
1148 } | 1156 } |
1149 | 1157 |
1150 TEST(KeyTest, InsideContainsUsingMap) { | 1158 TEST(KeyTest, InsideContainsUsingMap) { |
1151 std::map<int, char> container; | 1159 map<int, char> container; |
1152 container.insert(make_pair(1, 'a')); | 1160 container.insert(make_pair(1, 'a')); |
1153 container.insert(make_pair(2, 'b')); | 1161 container.insert(make_pair(2, 'b')); |
1154 container.insert(make_pair(4, 'c')); | 1162 container.insert(make_pair(4, 'c')); |
1155 EXPECT_THAT(container, Contains(Key(1))); | 1163 EXPECT_THAT(container, Contains(Key(1))); |
1156 EXPECT_THAT(container, Not(Contains(Key(3)))); | 1164 EXPECT_THAT(container, Not(Contains(Key(3)))); |
1157 } | 1165 } |
1158 | 1166 |
1159 TEST(KeyTest, InsideContainsUsingMultimap) { | 1167 TEST(KeyTest, InsideContainsUsingMultimap) { |
1160 std::multimap<int, char> container; | 1168 multimap<int, char> container; |
1161 container.insert(make_pair(1, 'a')); | 1169 container.insert(make_pair(1, 'a')); |
1162 container.insert(make_pair(2, 'b')); | 1170 container.insert(make_pair(2, 'b')); |
1163 container.insert(make_pair(4, 'c')); | 1171 container.insert(make_pair(4, 'c')); |
1164 | 1172 |
1165 EXPECT_THAT(container, Not(Contains(Key(25)))); | 1173 EXPECT_THAT(container, Not(Contains(Key(25)))); |
1166 container.insert(make_pair(25, 'd')); | 1174 container.insert(make_pair(25, 'd')); |
1167 EXPECT_THAT(container, Contains(Key(25))); | 1175 EXPECT_THAT(container, Contains(Key(25))); |
1168 container.insert(make_pair(25, 'e')); | 1176 container.insert(make_pair(25, 'e')); |
1169 EXPECT_THAT(container, Contains(Key(25))); | 1177 EXPECT_THAT(container, Contains(Key(25))); |
1170 | 1178 |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1259 Matcher<int> is_positive = Gt(0); | 1267 Matcher<int> is_positive = Gt(0); |
1260 Matcher<int> is_negative = Lt(0); | 1268 Matcher<int> is_negative = Lt(0); |
1261 pair<char, bool> p('a', true); | 1269 pair<char, bool> p('a', true); |
1262 EXPECT_THAT(p, Pair(is_positive, _)); | 1270 EXPECT_THAT(p, Pair(is_positive, _)); |
1263 EXPECT_THAT(p, Not(Pair(is_negative, _))); | 1271 EXPECT_THAT(p, Not(Pair(is_negative, _))); |
1264 EXPECT_THAT(p, Pair(_, is_positive)); | 1272 EXPECT_THAT(p, Pair(_, is_positive)); |
1265 EXPECT_THAT(p, Not(Pair(_, is_negative))); | 1273 EXPECT_THAT(p, Not(Pair(_, is_negative))); |
1266 } | 1274 } |
1267 | 1275 |
1268 TEST(PairTest, InsideContainsUsingMap) { | 1276 TEST(PairTest, InsideContainsUsingMap) { |
1269 std::map<int, char> container; | 1277 map<int, char> container; |
1270 container.insert(make_pair(1, 'a')); | 1278 container.insert(make_pair(1, 'a')); |
1271 container.insert(make_pair(2, 'b')); | 1279 container.insert(make_pair(2, 'b')); |
1272 container.insert(make_pair(4, 'c')); | 1280 container.insert(make_pair(4, 'c')); |
1273 EXPECT_THAT(container, Contains(Pair(1, 'a'))); | 1281 EXPECT_THAT(container, Contains(Pair(1, 'a'))); |
1274 EXPECT_THAT(container, Contains(Pair(1, _))); | 1282 EXPECT_THAT(container, Contains(Pair(1, _))); |
1275 EXPECT_THAT(container, Contains(Pair(_, 'a'))); | 1283 EXPECT_THAT(container, Contains(Pair(_, 'a'))); |
1276 EXPECT_THAT(container, Not(Contains(Pair(3, _)))); | 1284 EXPECT_THAT(container, Not(Contains(Pair(3, _)))); |
1277 } | 1285 } |
1278 | 1286 |
1279 // Tests StartsWith(s). | 1287 // Tests StartsWith(s). |
(...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1752 // second field. | 1760 // second field. |
1753 TEST(Eq2Test, MatchesEqualArguments) { | 1761 TEST(Eq2Test, MatchesEqualArguments) { |
1754 Matcher<const Tuple2&> m = Eq(); | 1762 Matcher<const Tuple2&> m = Eq(); |
1755 EXPECT_TRUE(m.Matches(Tuple2(5L, 5))); | 1763 EXPECT_TRUE(m.Matches(Tuple2(5L, 5))); |
1756 EXPECT_FALSE(m.Matches(Tuple2(5L, 6))); | 1764 EXPECT_FALSE(m.Matches(Tuple2(5L, 6))); |
1757 } | 1765 } |
1758 | 1766 |
1759 // Tests that Eq() describes itself properly. | 1767 // Tests that Eq() describes itself properly. |
1760 TEST(Eq2Test, CanDescribeSelf) { | 1768 TEST(Eq2Test, CanDescribeSelf) { |
1761 Matcher<const Tuple2&> m = Eq(); | 1769 Matcher<const Tuple2&> m = Eq(); |
1762 EXPECT_EQ("are a pair (x, y) where x == y", Describe(m)); | 1770 EXPECT_EQ("are an equal pair", Describe(m)); |
1763 } | 1771 } |
1764 | 1772 |
1765 // Tests that Ge() matches a 2-tuple where the first field >= the | 1773 // Tests that Ge() matches a 2-tuple where the first field >= the |
1766 // second field. | 1774 // second field. |
1767 TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) { | 1775 TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) { |
1768 Matcher<const Tuple2&> m = Ge(); | 1776 Matcher<const Tuple2&> m = Ge(); |
1769 EXPECT_TRUE(m.Matches(Tuple2(5L, 4))); | 1777 EXPECT_TRUE(m.Matches(Tuple2(5L, 4))); |
1770 EXPECT_TRUE(m.Matches(Tuple2(5L, 5))); | 1778 EXPECT_TRUE(m.Matches(Tuple2(5L, 5))); |
1771 EXPECT_FALSE(m.Matches(Tuple2(5L, 6))); | 1779 EXPECT_FALSE(m.Matches(Tuple2(5L, 6))); |
1772 } | 1780 } |
1773 | 1781 |
1774 // Tests that Ge() describes itself properly. | 1782 // Tests that Ge() describes itself properly. |
1775 TEST(Ge2Test, CanDescribeSelf) { | 1783 TEST(Ge2Test, CanDescribeSelf) { |
1776 Matcher<const Tuple2&> m = Ge(); | 1784 Matcher<const Tuple2&> m = Ge(); |
1777 EXPECT_EQ("are a pair (x, y) where x >= y", Describe(m)); | 1785 EXPECT_EQ("are a pair where the first >= the second", Describe(m)); |
1778 } | 1786 } |
1779 | 1787 |
1780 // Tests that Gt() matches a 2-tuple where the first field > the | 1788 // Tests that Gt() matches a 2-tuple where the first field > the |
1781 // second field. | 1789 // second field. |
1782 TEST(Gt2Test, MatchesGreaterThanArguments) { | 1790 TEST(Gt2Test, MatchesGreaterThanArguments) { |
1783 Matcher<const Tuple2&> m = Gt(); | 1791 Matcher<const Tuple2&> m = Gt(); |
1784 EXPECT_TRUE(m.Matches(Tuple2(5L, 4))); | 1792 EXPECT_TRUE(m.Matches(Tuple2(5L, 4))); |
1785 EXPECT_FALSE(m.Matches(Tuple2(5L, 5))); | 1793 EXPECT_FALSE(m.Matches(Tuple2(5L, 5))); |
1786 EXPECT_FALSE(m.Matches(Tuple2(5L, 6))); | 1794 EXPECT_FALSE(m.Matches(Tuple2(5L, 6))); |
1787 } | 1795 } |
1788 | 1796 |
1789 // Tests that Gt() describes itself properly. | 1797 // Tests that Gt() describes itself properly. |
1790 TEST(Gt2Test, CanDescribeSelf) { | 1798 TEST(Gt2Test, CanDescribeSelf) { |
1791 Matcher<const Tuple2&> m = Gt(); | 1799 Matcher<const Tuple2&> m = Gt(); |
1792 EXPECT_EQ("are a pair (x, y) where x > y", Describe(m)); | 1800 EXPECT_EQ("are a pair where the first > the second", Describe(m)); |
1793 } | 1801 } |
1794 | 1802 |
1795 // Tests that Le() matches a 2-tuple where the first field <= the | 1803 // Tests that Le() matches a 2-tuple where the first field <= the |
1796 // second field. | 1804 // second field. |
1797 TEST(Le2Test, MatchesLessThanOrEqualArguments) { | 1805 TEST(Le2Test, MatchesLessThanOrEqualArguments) { |
1798 Matcher<const Tuple2&> m = Le(); | 1806 Matcher<const Tuple2&> m = Le(); |
1799 EXPECT_TRUE(m.Matches(Tuple2(5L, 6))); | 1807 EXPECT_TRUE(m.Matches(Tuple2(5L, 6))); |
1800 EXPECT_TRUE(m.Matches(Tuple2(5L, 5))); | 1808 EXPECT_TRUE(m.Matches(Tuple2(5L, 5))); |
1801 EXPECT_FALSE(m.Matches(Tuple2(5L, 4))); | 1809 EXPECT_FALSE(m.Matches(Tuple2(5L, 4))); |
1802 } | 1810 } |
1803 | 1811 |
1804 // Tests that Le() describes itself properly. | 1812 // Tests that Le() describes itself properly. |
1805 TEST(Le2Test, CanDescribeSelf) { | 1813 TEST(Le2Test, CanDescribeSelf) { |
1806 Matcher<const Tuple2&> m = Le(); | 1814 Matcher<const Tuple2&> m = Le(); |
1807 EXPECT_EQ("are a pair (x, y) where x <= y", Describe(m)); | 1815 EXPECT_EQ("are a pair where the first <= the second", Describe(m)); |
1808 } | 1816 } |
1809 | 1817 |
1810 // Tests that Lt() matches a 2-tuple where the first field < the | 1818 // Tests that Lt() matches a 2-tuple where the first field < the |
1811 // second field. | 1819 // second field. |
1812 TEST(Lt2Test, MatchesLessThanArguments) { | 1820 TEST(Lt2Test, MatchesLessThanArguments) { |
1813 Matcher<const Tuple2&> m = Lt(); | 1821 Matcher<const Tuple2&> m = Lt(); |
1814 EXPECT_TRUE(m.Matches(Tuple2(5L, 6))); | 1822 EXPECT_TRUE(m.Matches(Tuple2(5L, 6))); |
1815 EXPECT_FALSE(m.Matches(Tuple2(5L, 5))); | 1823 EXPECT_FALSE(m.Matches(Tuple2(5L, 5))); |
1816 EXPECT_FALSE(m.Matches(Tuple2(5L, 4))); | 1824 EXPECT_FALSE(m.Matches(Tuple2(5L, 4))); |
1817 } | 1825 } |
1818 | 1826 |
1819 // Tests that Lt() describes itself properly. | 1827 // Tests that Lt() describes itself properly. |
1820 TEST(Lt2Test, CanDescribeSelf) { | 1828 TEST(Lt2Test, CanDescribeSelf) { |
1821 Matcher<const Tuple2&> m = Lt(); | 1829 Matcher<const Tuple2&> m = Lt(); |
1822 EXPECT_EQ("are a pair (x, y) where x < y", Describe(m)); | 1830 EXPECT_EQ("are a pair where the first < the second", Describe(m)); |
1823 } | 1831 } |
1824 | 1832 |
1825 // Tests that Ne() matches a 2-tuple where the first field != the | 1833 // Tests that Ne() matches a 2-tuple where the first field != the |
1826 // second field. | 1834 // second field. |
1827 TEST(Ne2Test, MatchesUnequalArguments) { | 1835 TEST(Ne2Test, MatchesUnequalArguments) { |
1828 Matcher<const Tuple2&> m = Ne(); | 1836 Matcher<const Tuple2&> m = Ne(); |
1829 EXPECT_TRUE(m.Matches(Tuple2(5L, 6))); | 1837 EXPECT_TRUE(m.Matches(Tuple2(5L, 6))); |
1830 EXPECT_TRUE(m.Matches(Tuple2(5L, 4))); | 1838 EXPECT_TRUE(m.Matches(Tuple2(5L, 4))); |
1831 EXPECT_FALSE(m.Matches(Tuple2(5L, 5))); | 1839 EXPECT_FALSE(m.Matches(Tuple2(5L, 5))); |
1832 } | 1840 } |
1833 | 1841 |
1834 // Tests that Ne() describes itself properly. | 1842 // Tests that Ne() describes itself properly. |
1835 TEST(Ne2Test, CanDescribeSelf) { | 1843 TEST(Ne2Test, CanDescribeSelf) { |
1836 Matcher<const Tuple2&> m = Ne(); | 1844 Matcher<const Tuple2&> m = Ne(); |
1837 EXPECT_EQ("are a pair (x, y) where x != y", Describe(m)); | 1845 EXPECT_EQ("are an unequal pair", Describe(m)); |
1838 } | 1846 } |
1839 | 1847 |
1840 // Tests that Not(m) matches any value that doesn't match m. | 1848 // Tests that Not(m) matches any value that doesn't match m. |
1841 TEST(NotTest, NegatesMatcher) { | 1849 TEST(NotTest, NegatesMatcher) { |
1842 Matcher<int> m; | 1850 Matcher<int> m; |
1843 m = Not(Eq(2)); | 1851 m = Not(Eq(2)); |
1844 EXPECT_TRUE(m.Matches(3)); | 1852 EXPECT_TRUE(m.Matches(3)); |
1845 EXPECT_FALSE(m.Matches(2)); | 1853 EXPECT_FALSE(m.Matches(2)); |
1846 } | 1854 } |
1847 | 1855 |
(...skipping 1481 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3329 explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {} | 3337 explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {} |
3330 | 3338 |
3331 // For testing using ExplainMatchResultTo() with polymorphic matchers. | 3339 // For testing using ExplainMatchResultTo() with polymorphic matchers. |
3332 template <typename T> | 3340 template <typename T> |
3333 bool MatchAndExplain(const T& n, MatchResultListener* listener) const { | 3341 bool MatchAndExplain(const T& n, MatchResultListener* listener) const { |
3334 *listener << "which is " << (n % divider_) << " modulo " | 3342 *listener << "which is " << (n % divider_) << " modulo " |
3335 << divider_; | 3343 << divider_; |
3336 return (n % divider_) == 0; | 3344 return (n % divider_) == 0; |
3337 } | 3345 } |
3338 | 3346 |
3339 void DescribeTo(::std::ostream* os) const { | 3347 void DescribeTo(ostream* os) const { |
3340 *os << "is divisible by " << divider_; | 3348 *os << "is divisible by " << divider_; |
3341 } | 3349 } |
3342 | 3350 |
3343 void DescribeNegationTo(::std::ostream* os) const { | 3351 void DescribeNegationTo(ostream* os) const { |
3344 *os << "is not divisible by " << divider_; | 3352 *os << "is not divisible by " << divider_; |
3345 } | 3353 } |
3346 | 3354 |
3347 void set_divider(int a_divider) { divider_ = a_divider; } | 3355 void set_divider(int a_divider) { divider_ = a_divider; } |
3348 int divider() const { return divider_; } | 3356 int divider() const { return divider_; } |
3349 | 3357 |
3350 private: | 3358 private: |
3351 int divider_; | 3359 int divider_; |
3352 }; | 3360 }; |
3353 | 3361 |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3435 } | 3443 } |
3436 | 3444 |
3437 #if GTEST_HAS_TYPED_TEST | 3445 #if GTEST_HAS_TYPED_TEST |
3438 // Tests ContainerEq with different container types, and | 3446 // Tests ContainerEq with different container types, and |
3439 // different element types. | 3447 // different element types. |
3440 | 3448 |
3441 template <typename T> | 3449 template <typename T> |
3442 class ContainerEqTest : public testing::Test {}; | 3450 class ContainerEqTest : public testing::Test {}; |
3443 | 3451 |
3444 typedef testing::Types< | 3452 typedef testing::Types< |
3445 std::set<int>, | 3453 set<int>, |
3446 std::vector<size_t>, | 3454 vector<size_t>, |
3447 std::multiset<size_t>, | 3455 multiset<size_t>, |
3448 std::list<int> > | 3456 list<int> > |
3449 ContainerEqTestTypes; | 3457 ContainerEqTestTypes; |
3450 | 3458 |
3451 TYPED_TEST_CASE(ContainerEqTest, ContainerEqTestTypes); | 3459 TYPED_TEST_CASE(ContainerEqTest, ContainerEqTestTypes); |
3452 | 3460 |
3453 // Tests that the filled container is equal to itself. | 3461 // Tests that the filled container is equal to itself. |
3454 TYPED_TEST(ContainerEqTest, EqualsSelf) { | 3462 TYPED_TEST(ContainerEqTest, EqualsSelf) { |
3455 static const int vals[] = {1, 1, 2, 3, 5, 8}; | 3463 static const int vals[] = {1, 1, 2, 3, 5, 8}; |
3456 TypeParam my_set(vals, vals + 6); | 3464 TypeParam my_set(vals, vals + 6); |
3457 const Matcher<TypeParam> m = ContainerEq(my_set); | 3465 const Matcher<TypeParam> m = ContainerEq(my_set); |
3458 EXPECT_TRUE(m.Matches(my_set)); | 3466 EXPECT_TRUE(m.Matches(my_set)); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3506 // But in any case there should be no explanation. | 3514 // But in any case there should be no explanation. |
3507 EXPECT_EQ("", Explain(m, test_set)); | 3515 EXPECT_EQ("", Explain(m, test_set)); |
3508 } | 3516 } |
3509 #endif // GTEST_HAS_TYPED_TEST | 3517 #endif // GTEST_HAS_TYPED_TEST |
3510 | 3518 |
3511 // Tests that mutliple missing values are reported. | 3519 // Tests that mutliple missing values are reported. |
3512 // Using just vector here, so order is predicatble. | 3520 // Using just vector here, so order is predicatble. |
3513 TEST(ContainerEqExtraTest, MultipleValuesMissing) { | 3521 TEST(ContainerEqExtraTest, MultipleValuesMissing) { |
3514 static const int vals[] = {1, 1, 2, 3, 5, 8}; | 3522 static const int vals[] = {1, 1, 2, 3, 5, 8}; |
3515 static const int test_vals[] = {2, 1, 5}; | 3523 static const int test_vals[] = {2, 1, 5}; |
3516 std::vector<int> my_set(vals, vals + 6); | 3524 vector<int> my_set(vals, vals + 6); |
3517 std::vector<int> test_set(test_vals, test_vals + 3); | 3525 vector<int> test_set(test_vals, test_vals + 3); |
3518 const Matcher<std::vector<int> > m = ContainerEq(my_set); | 3526 const Matcher<vector<int> > m = ContainerEq(my_set); |
3519 EXPECT_FALSE(m.Matches(test_set)); | 3527 EXPECT_FALSE(m.Matches(test_set)); |
3520 EXPECT_EQ("which doesn't have these expected elements: 3, 8", | 3528 EXPECT_EQ("which doesn't have these expected elements: 3, 8", |
3521 Explain(m, test_set)); | 3529 Explain(m, test_set)); |
3522 } | 3530 } |
3523 | 3531 |
3524 // Tests that added values are reported. | 3532 // Tests that added values are reported. |
3525 // Using just vector here, so order is predicatble. | 3533 // Using just vector here, so order is predicatble. |
3526 TEST(ContainerEqExtraTest, MultipleValuesAdded) { | 3534 TEST(ContainerEqExtraTest, MultipleValuesAdded) { |
3527 static const int vals[] = {1, 1, 2, 3, 5, 8}; | 3535 static const int vals[] = {1, 1, 2, 3, 5, 8}; |
3528 static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46}; | 3536 static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46}; |
3529 std::list<size_t> my_set(vals, vals + 6); | 3537 list<size_t> my_set(vals, vals + 6); |
3530 std::list<size_t> test_set(test_vals, test_vals + 7); | 3538 list<size_t> test_set(test_vals, test_vals + 7); |
3531 const Matcher<const std::list<size_t>&> m = ContainerEq(my_set); | 3539 const Matcher<const list<size_t>&> m = ContainerEq(my_set); |
3532 EXPECT_FALSE(m.Matches(test_set)); | 3540 EXPECT_FALSE(m.Matches(test_set)); |
3533 EXPECT_EQ("which has these unexpected elements: 92, 46", | 3541 EXPECT_EQ("which has these unexpected elements: 92, 46", |
3534 Explain(m, test_set)); | 3542 Explain(m, test_set)); |
3535 } | 3543 } |
3536 | 3544 |
3537 // Tests that added and missing values are reported together. | 3545 // Tests that added and missing values are reported together. |
3538 TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) { | 3546 TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) { |
3539 static const int vals[] = {1, 1, 2, 3, 5, 8}; | 3547 static const int vals[] = {1, 1, 2, 3, 5, 8}; |
3540 static const int test_vals[] = {1, 2, 3, 92, 46}; | 3548 static const int test_vals[] = {1, 2, 3, 92, 46}; |
3541 std::list<size_t> my_set(vals, vals + 6); | 3549 list<size_t> my_set(vals, vals + 6); |
3542 std::list<size_t> test_set(test_vals, test_vals + 5); | 3550 list<size_t> test_set(test_vals, test_vals + 5); |
3543 const Matcher<const std::list<size_t> > m = ContainerEq(my_set); | 3551 const Matcher<const list<size_t> > m = ContainerEq(my_set); |
3544 EXPECT_FALSE(m.Matches(test_set)); | 3552 EXPECT_FALSE(m.Matches(test_set)); |
3545 EXPECT_EQ("which has these unexpected elements: 92, 46,\n" | 3553 EXPECT_EQ("which has these unexpected elements: 92, 46,\n" |
3546 "and doesn't have these expected elements: 5, 8", | 3554 "and doesn't have these expected elements: 5, 8", |
3547 Explain(m, test_set)); | 3555 Explain(m, test_set)); |
3548 } | 3556 } |
3549 | 3557 |
3550 // Tests to see that duplicate elements are detected, | 3558 // Tests to see that duplicate elements are detected, |
3551 // but (as above) not reported in the explanation. | 3559 // but (as above) not reported in the explanation. |
3552 TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) { | 3560 TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) { |
3553 static const int vals[] = {1, 1, 2, 3, 5, 8}; | 3561 static const int vals[] = {1, 1, 2, 3, 5, 8}; |
3554 static const int test_vals[] = {1, 2, 3, 5, 8}; | 3562 static const int test_vals[] = {1, 2, 3, 5, 8}; |
3555 std::vector<int> my_set(vals, vals + 6); | 3563 vector<int> my_set(vals, vals + 6); |
3556 std::vector<int> test_set(test_vals, test_vals + 5); | 3564 vector<int> test_set(test_vals, test_vals + 5); |
3557 const Matcher<std::vector<int> > m = ContainerEq(my_set); | 3565 const Matcher<vector<int> > m = ContainerEq(my_set); |
3558 EXPECT_TRUE(m.Matches(my_set)); | 3566 EXPECT_TRUE(m.Matches(my_set)); |
3559 EXPECT_FALSE(m.Matches(test_set)); | 3567 EXPECT_FALSE(m.Matches(test_set)); |
3560 // There is nothing to report when both sets contain all the same values. | 3568 // There is nothing to report when both sets contain all the same values. |
3561 EXPECT_EQ("", Explain(m, test_set)); | 3569 EXPECT_EQ("", Explain(m, test_set)); |
3562 } | 3570 } |
3563 | 3571 |
3564 // Tests that ContainerEq works for non-trivial associative containers, | 3572 // Tests that ContainerEq works for non-trivial associative containers, |
3565 // like maps. | 3573 // like maps. |
3566 TEST(ContainerEqExtraTest, WorksForMaps) { | 3574 TEST(ContainerEqExtraTest, WorksForMaps) { |
3567 std::map<int, std::string> my_map; | 3575 map<int, std::string> my_map; |
3568 my_map[0] = "a"; | 3576 my_map[0] = "a"; |
3569 my_map[1] = "b"; | 3577 my_map[1] = "b"; |
3570 | 3578 |
3571 std::map<int, std::string> test_map; | 3579 map<int, std::string> test_map; |
3572 test_map[0] = "aa"; | 3580 test_map[0] = "aa"; |
3573 test_map[1] = "b"; | 3581 test_map[1] = "b"; |
3574 | 3582 |
3575 const Matcher<const std::map<int, std::string>&> m = ContainerEq(my_map); | 3583 const Matcher<const map<int, std::string>&> m = ContainerEq(my_map); |
3576 EXPECT_TRUE(m.Matches(my_map)); | 3584 EXPECT_TRUE(m.Matches(my_map)); |
3577 EXPECT_FALSE(m.Matches(test_map)); | 3585 EXPECT_FALSE(m.Matches(test_map)); |
3578 | 3586 |
3579 EXPECT_EQ("which has these unexpected elements: (0, \"aa\"),\n" | 3587 EXPECT_EQ("which has these unexpected elements: (0, \"aa\"),\n" |
3580 "and doesn't have these expected elements: (0, \"a\")", | 3588 "and doesn't have these expected elements: (0, \"a\")", |
3581 Explain(m, test_map)); | 3589 Explain(m, test_map)); |
3582 } | 3590 } |
3583 | 3591 |
3584 TEST(ContainerEqExtraTest, WorksForNativeArray) { | 3592 TEST(ContainerEqExtraTest, WorksForNativeArray) { |
3585 int a1[] = { 1, 2, 3 }; | 3593 int a1[] = { 1, 2, 3 }; |
(...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3977 | 3985 |
3978 stringstream ss3; | 3986 stringstream ss3; |
3979 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))), | 3987 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))), |
3980 make_tuple(2, 'a'), &ss3); | 3988 make_tuple(2, 'a'), &ss3); |
3981 EXPECT_EQ(" Expected arg #0: is > 5\n" | 3989 EXPECT_EQ(" Expected arg #0: is > 5\n" |
3982 " Actual: 2, which is 3 less than 5\n", | 3990 " Actual: 2, which is 3 less than 5\n", |
3983 ss3.str()); // Failed match where only one argument needs | 3991 ss3.str()); // Failed match where only one argument needs |
3984 // explanation. | 3992 // explanation. |
3985 } | 3993 } |
3986 | 3994 |
| 3995 // Tests Each(). |
| 3996 |
| 3997 TEST(EachTest, ExplainsMatchResultCorrectly) { |
| 3998 set<int> a; // empty |
| 3999 |
| 4000 Matcher<set<int> > m = Each(2); |
| 4001 EXPECT_EQ("", Explain(m, a)); |
| 4002 |
| 4003 Matcher<const int(&)[1]> n = Each(1); |
| 4004 |
| 4005 const int b[1] = { 1 }; |
| 4006 EXPECT_EQ("", Explain(n, b)); |
| 4007 |
| 4008 n = Each(3); |
| 4009 EXPECT_EQ("whose element #0 doesn't match", Explain(n, b)); |
| 4010 |
| 4011 a.insert(1); |
| 4012 a.insert(2); |
| 4013 a.insert(3); |
| 4014 m = Each(GreaterThan(0)); |
| 4015 EXPECT_EQ("", Explain(m, a)); |
| 4016 |
| 4017 m = Each(GreaterThan(10)); |
| 4018 EXPECT_EQ("whose element #0 doesn't match, which is 9 less than 10", |
| 4019 Explain(m, a)); |
| 4020 } |
| 4021 |
| 4022 TEST(EachTest, DescribesItselfCorrectly) { |
| 4023 Matcher<vector<int> > m = Each(1); |
| 4024 EXPECT_EQ("only contains elements that is equal to 1", Describe(m)); |
| 4025 |
| 4026 Matcher<vector<int> > m2 = Not(m); |
| 4027 EXPECT_EQ("contains some element that isn't equal to 1", Describe(m2)); |
| 4028 } |
| 4029 |
| 4030 TEST(EachTest, MatchesVectorWhenAllElementsMatch) { |
| 4031 vector<int> some_vector; |
| 4032 EXPECT_THAT(some_vector, Each(1)); |
| 4033 some_vector.push_back(3); |
| 4034 EXPECT_THAT(some_vector, Not(Each(1))); |
| 4035 EXPECT_THAT(some_vector, Each(3)); |
| 4036 some_vector.push_back(1); |
| 4037 some_vector.push_back(2); |
| 4038 EXPECT_THAT(some_vector, Not(Each(3))); |
| 4039 EXPECT_THAT(some_vector, Each(Lt(3.5))); |
| 4040 |
| 4041 vector<string> another_vector; |
| 4042 another_vector.push_back("fee"); |
| 4043 EXPECT_THAT(another_vector, Each(string("fee"))); |
| 4044 another_vector.push_back("fie"); |
| 4045 another_vector.push_back("foe"); |
| 4046 another_vector.push_back("fum"); |
| 4047 EXPECT_THAT(another_vector, Not(Each(string("fee")))); |
| 4048 } |
| 4049 |
| 4050 TEST(EachTest, MatchesMapWhenAllElementsMatch) { |
| 4051 map<const char*, int> my_map; |
| 4052 const char* bar = "a string"; |
| 4053 my_map[bar] = 2; |
| 4054 EXPECT_THAT(my_map, Each(make_pair(bar, 2))); |
| 4055 |
| 4056 map<string, int> another_map; |
| 4057 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1))); |
| 4058 another_map["fee"] = 1; |
| 4059 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1))); |
| 4060 another_map["fie"] = 2; |
| 4061 another_map["foe"] = 3; |
| 4062 another_map["fum"] = 4; |
| 4063 EXPECT_THAT(another_map, Not(Each(make_pair(string("fee"), 1)))); |
| 4064 EXPECT_THAT(another_map, Not(Each(make_pair(string("fum"), 1)))); |
| 4065 EXPECT_THAT(another_map, Each(Pair(_, Gt(0)))); |
| 4066 } |
| 4067 |
| 4068 TEST(EachTest, AcceptsMatcher) { |
| 4069 const int a[] = { 1, 2, 3 }; |
| 4070 EXPECT_THAT(a, Each(Gt(0))); |
| 4071 EXPECT_THAT(a, Not(Each(Gt(1)))); |
| 4072 } |
| 4073 |
| 4074 TEST(EachTest, WorksForNativeArrayAsTuple) { |
| 4075 const int a[] = { 1, 2 }; |
| 4076 const int* const pointer = a; |
| 4077 EXPECT_THAT(make_tuple(pointer, 2), Each(Gt(0))); |
| 4078 EXPECT_THAT(make_tuple(pointer, 2), Not(Each(Gt(1)))); |
| 4079 } |
| 4080 |
| 4081 // For testing Pointwise(). |
| 4082 class IsHalfOfMatcher { |
| 4083 public: |
| 4084 template <typename T1, typename T2> |
| 4085 bool MatchAndExplain(const tuple<T1, T2>& a_pair, |
| 4086 MatchResultListener* listener) const { |
| 4087 if (get<0>(a_pair) == get<1>(a_pair)/2) { |
| 4088 *listener << "where the second is " << get<1>(a_pair); |
| 4089 return true; |
| 4090 } else { |
| 4091 *listener << "where the second/2 is " << get<1>(a_pair)/2; |
| 4092 return false; |
| 4093 } |
| 4094 } |
| 4095 |
| 4096 void DescribeTo(ostream* os) const { |
| 4097 *os << "are a pair where the first is half of the second"; |
| 4098 } |
| 4099 |
| 4100 void DescribeNegationTo(ostream* os) const { |
| 4101 *os << "are a pair where the first isn't half of the second"; |
| 4102 } |
| 4103 }; |
| 4104 |
| 4105 PolymorphicMatcher<IsHalfOfMatcher> IsHalfOf() { |
| 4106 return MakePolymorphicMatcher(IsHalfOfMatcher()); |
| 4107 } |
| 4108 |
| 4109 TEST(PointwiseTest, DescribesSelf) { |
| 4110 vector<int> rhs; |
| 4111 rhs.push_back(1); |
| 4112 rhs.push_back(2); |
| 4113 rhs.push_back(3); |
| 4114 const Matcher<const vector<int>&> m = Pointwise(IsHalfOf(), rhs); |
| 4115 EXPECT_EQ("contains 3 values, where each value and its corresponding value " |
| 4116 "in { 1, 2, 3 } are a pair where the first is half of the second", |
| 4117 Describe(m)); |
| 4118 EXPECT_EQ("doesn't contain exactly 3 values, or contains a value x at some " |
| 4119 "index i where x and the i-th value of { 1, 2, 3 } are a pair " |
| 4120 "where the first isn't half of the second", |
| 4121 DescribeNegation(m)); |
| 4122 } |
| 4123 |
| 4124 TEST(PointwiseTest, MakesCopyOfRhs) { |
| 4125 list<signed char> rhs; |
| 4126 rhs.push_back(2); |
| 4127 rhs.push_back(4); |
| 4128 |
| 4129 int lhs[] = { 1, 2 }; |
| 4130 const Matcher<const int (&)[2]> m = Pointwise(IsHalfOf(), rhs); |
| 4131 EXPECT_THAT(lhs, m); |
| 4132 |
| 4133 // Changing rhs now shouldn't affect m, which made a copy of rhs. |
| 4134 rhs.push_back(6); |
| 4135 EXPECT_THAT(lhs, m); |
| 4136 } |
| 4137 |
| 4138 TEST(PointwiseTest, WorksForLhsNativeArray) { |
| 4139 const int lhs[] = { 1, 2, 3 }; |
| 4140 vector<int> rhs; |
| 4141 rhs.push_back(2); |
| 4142 rhs.push_back(4); |
| 4143 rhs.push_back(6); |
| 4144 EXPECT_THAT(lhs, Pointwise(Lt(), rhs)); |
| 4145 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs))); |
| 4146 } |
| 4147 |
| 4148 TEST(PointwiseTest, WorksForRhsNativeArray) { |
| 4149 const int rhs[] = { 1, 2, 3 }; |
| 4150 vector<int> lhs; |
| 4151 lhs.push_back(2); |
| 4152 lhs.push_back(4); |
| 4153 lhs.push_back(6); |
| 4154 EXPECT_THAT(lhs, Pointwise(Gt(), rhs)); |
| 4155 EXPECT_THAT(lhs, Not(Pointwise(Lt(), rhs))); |
| 4156 } |
| 4157 |
| 4158 TEST(PointwiseTest, RejectsWrongSize) { |
| 4159 const double lhs[2] = { 1, 2 }; |
| 4160 const int rhs[1] = { 0 }; |
| 4161 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs))); |
| 4162 EXPECT_EQ("which contains 2 values", |
| 4163 Explain(Pointwise(Gt(), rhs), lhs)); |
| 4164 |
| 4165 const int rhs2[3] = { 0, 1, 2 }; |
| 4166 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs2))); |
| 4167 } |
| 4168 |
| 4169 TEST(PointwiseTest, RejectsWrongContent) { |
| 4170 const double lhs[3] = { 1, 2, 3 }; |
| 4171 const int rhs[3] = { 2, 6, 4 }; |
| 4172 EXPECT_THAT(lhs, Not(Pointwise(IsHalfOf(), rhs))); |
| 4173 EXPECT_EQ("where the value pair (2, 6) at index #1 don't match, " |
| 4174 "where the second/2 is 3", |
| 4175 Explain(Pointwise(IsHalfOf(), rhs), lhs)); |
| 4176 } |
| 4177 |
| 4178 TEST(PointwiseTest, AcceptsCorrectContent) { |
| 4179 const double lhs[3] = { 1, 2, 3 }; |
| 4180 const int rhs[3] = { 2, 4, 6 }; |
| 4181 EXPECT_THAT(lhs, Pointwise(IsHalfOf(), rhs)); |
| 4182 EXPECT_EQ("", Explain(Pointwise(IsHalfOf(), rhs), lhs)); |
| 4183 } |
| 4184 |
| 4185 TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) { |
| 4186 const double lhs[3] = { 1, 2, 3 }; |
| 4187 const int rhs[3] = { 2, 4, 6 }; |
| 4188 const Matcher<tuple<const double&, const int&> > m1 = IsHalfOf(); |
| 4189 EXPECT_THAT(lhs, Pointwise(m1, rhs)); |
| 4190 EXPECT_EQ("", Explain(Pointwise(m1, rhs), lhs)); |
| 4191 |
| 4192 // This type works as a tuple<const double&, const int&> can be |
| 4193 // implicitly cast to tuple<double, int>. |
| 4194 const Matcher<tuple<double, int> > m2 = IsHalfOf(); |
| 4195 EXPECT_THAT(lhs, Pointwise(m2, rhs)); |
| 4196 EXPECT_EQ("", Explain(Pointwise(m2, rhs), lhs)); |
| 4197 } |
| 4198 |
3987 } // namespace gmock_matchers_test | 4199 } // namespace gmock_matchers_test |
3988 } // namespace testing | 4200 } // namespace testing |
OLD | NEW |