OLD | NEW |
1 // Copyright 2008, Google Inc. | 1 // Copyright 2008, 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 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 Describe(m)); | 204 Describe(m)); |
205 } | 205 } |
206 | 206 |
207 TEST(ArgsTest, DescribesNegationCorrectly) { | 207 TEST(ArgsTest, DescribesNegationCorrectly) { |
208 const Matcher<tuple<int, char> > m = Args<1, 0>(Gt()); | 208 const Matcher<tuple<int, char> > m = Args<1, 0>(Gt()); |
209 EXPECT_EQ("are a tuple whose fields (#1, #0) are a pair (x, y) " | 209 EXPECT_EQ("are a tuple whose fields (#1, #0) are a pair (x, y) " |
210 "where x > y is false", | 210 "where x > y is false", |
211 DescribeNegation(m)); | 211 DescribeNegation(m)); |
212 } | 212 } |
213 | 213 |
| 214 TEST(ArgsTest, ExplainsMatchResultWithoutInnerExplanation) { |
| 215 const Matcher<tuple<bool, int, int> > m = Args<1, 2>(Eq()); |
| 216 EXPECT_EQ("whose fields (#1, #2) are (42, 42)", |
| 217 Explain(m, make_tuple(false, 42, 42))); |
| 218 EXPECT_EQ("whose fields (#1, #2) are (42, 43)", |
| 219 Explain(m, make_tuple(false, 42, 43))); |
| 220 } |
| 221 |
| 222 // For testing Args<>'s explanation. |
| 223 class LessThanMatcher : public MatcherInterface<tuple<char, int> > { |
| 224 public: |
| 225 virtual void DescribeTo(::std::ostream* os) const {} |
| 226 |
| 227 virtual bool MatchAndExplain(tuple<char, int> value, |
| 228 MatchResultListener* listener) const { |
| 229 const int diff = get<0>(value) - get<1>(value); |
| 230 if (diff > 0) { |
| 231 *listener << "where the first value is " << diff |
| 232 << " more than the second"; |
| 233 } |
| 234 return diff < 0; |
| 235 } |
| 236 }; |
| 237 |
| 238 Matcher<tuple<char, int> > LessThan() { |
| 239 return MakeMatcher(new LessThanMatcher); |
| 240 } |
| 241 |
| 242 TEST(ArgsTest, ExplainsMatchResultWithInnerExplanation) { |
| 243 const Matcher<tuple<char, int, int> > m = Args<0, 2>(LessThan()); |
| 244 EXPECT_EQ("whose fields (#0, #2) are ('a' (97), 42), " |
| 245 "where the first value is 55 more than the second", |
| 246 Explain(m, make_tuple('a', 42, 42))); |
| 247 EXPECT_EQ("whose fields (#0, #2) are ('\\0', 43)", |
| 248 Explain(m, make_tuple('\0', 42, 43))); |
| 249 } |
| 250 |
214 // For testing ExplainMatchResultTo(). | 251 // For testing ExplainMatchResultTo(). |
215 class GreaterThanMatcher : public MatcherInterface<int> { | 252 class GreaterThanMatcher : public MatcherInterface<int> { |
216 public: | 253 public: |
217 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {} | 254 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {} |
218 | 255 |
219 virtual void DescribeTo(::std::ostream* os) const { | 256 virtual void DescribeTo(::std::ostream* os) const { |
220 *os << "is greater than " << rhs_; | 257 *os << "is greater than " << rhs_; |
221 } | 258 } |
222 | 259 |
223 virtual bool MatchAndExplain(int lhs, | 260 virtual bool MatchAndExplain(int lhs, |
224 MatchResultListener* listener) const { | 261 MatchResultListener* listener) const { |
225 const int diff = lhs - rhs_; | 262 const int diff = lhs - rhs_; |
226 if (diff > 0) { | 263 if (diff > 0) { |
227 *listener << "is " << diff << " more than " << rhs_; | 264 *listener << "which is " << diff << " more than " << rhs_; |
228 } else if (diff == 0) { | 265 } else if (diff == 0) { |
229 *listener << "is the same as " << rhs_; | 266 *listener << "which is the same as " << rhs_; |
230 } else { | 267 } else { |
231 *listener << "is " << -diff << " less than " << rhs_; | 268 *listener << "which is " << -diff << " less than " << rhs_; |
232 } | 269 } |
233 | 270 |
234 return lhs > rhs_; | 271 return lhs > rhs_; |
235 } | 272 } |
236 | 273 |
237 private: | 274 private: |
238 int rhs_; | 275 int rhs_; |
239 }; | 276 }; |
240 | 277 |
241 Matcher<int> GreaterThan(int n) { | 278 Matcher<int> GreaterThan(int n) { |
242 return MakeMatcher(new GreaterThanMatcher(n)); | 279 return MakeMatcher(new GreaterThanMatcher(n)); |
243 } | 280 } |
244 | 281 |
245 // Tests for ElementsAre(). | 282 // Tests for ElementsAre(). |
246 | 283 |
247 // Evaluates to the number of elements in 'array'. | 284 // Evaluates to the number of elements in 'array'. |
248 #define GMOCK_ARRAY_SIZE_(array) (sizeof(array)/sizeof(array[0])) | 285 #define GMOCK_ARRAY_SIZE_(array) (sizeof(array)/sizeof(array[0])) |
249 | 286 |
250 TEST(ElementsAreTest, CanDescribeExpectingNoElement) { | 287 TEST(ElementsAreTest, CanDescribeExpectingNoElement) { |
251 Matcher<const vector<int>&> m = ElementsAre(); | 288 Matcher<const vector<int>&> m = ElementsAre(); |
252 EXPECT_EQ("is empty", Describe(m)); | 289 EXPECT_EQ("is empty", Describe(m)); |
253 } | 290 } |
254 | 291 |
255 TEST(ElementsAreTest, CanDescribeExpectingOneElement) { | 292 TEST(ElementsAreTest, CanDescribeExpectingOneElement) { |
256 Matcher<vector<int> > m = ElementsAre(Gt(5)); | 293 Matcher<vector<int> > m = ElementsAre(Gt(5)); |
257 EXPECT_EQ("has 1 element that is greater than 5", Describe(m)); | 294 EXPECT_EQ("has 1 element that is > 5", Describe(m)); |
258 } | 295 } |
259 | 296 |
260 TEST(ElementsAreTest, CanDescribeExpectingManyElements) { | 297 TEST(ElementsAreTest, CanDescribeExpectingManyElements) { |
261 Matcher<list<string> > m = ElementsAre(StrEq("one"), "two"); | 298 Matcher<list<string> > m = ElementsAre(StrEq("one"), "two"); |
262 EXPECT_EQ("has 2 elements where\n" | 299 EXPECT_EQ("has 2 elements where\n" |
263 "element 0 is equal to \"one\",\n" | 300 "element #0 is equal to \"one\",\n" |
264 "element 1 is equal to \"two\"", Describe(m)); | 301 "element #1 is equal to \"two\"", Describe(m)); |
265 } | 302 } |
266 | 303 |
267 TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) { | 304 TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) { |
268 Matcher<vector<int> > m = ElementsAre(); | 305 Matcher<vector<int> > m = ElementsAre(); |
269 EXPECT_EQ("is not empty", DescribeNegation(m)); | 306 EXPECT_EQ("isn't empty", DescribeNegation(m)); |
270 } | 307 } |
271 | 308 |
272 TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElment) { | 309 TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElment) { |
273 Matcher<const list<int>& > m = ElementsAre(Gt(5)); | 310 Matcher<const list<int>& > m = ElementsAre(Gt(5)); |
274 EXPECT_EQ("does not have 1 element, or\n" | 311 EXPECT_EQ("doesn't have 1 element, or\n" |
275 "element 0 is not greater than 5", DescribeNegation(m)); | 312 "element #0 isn't > 5", DescribeNegation(m)); |
276 } | 313 } |
277 | 314 |
278 TEST(ElementsAreTest, CanDescribeNegationOfExpectingManyElements) { | 315 TEST(ElementsAreTest, CanDescribeNegationOfExpectingManyElements) { |
279 Matcher<const list<string>& > m = ElementsAre("one", "two"); | 316 Matcher<const list<string>& > m = ElementsAre("one", "two"); |
280 EXPECT_EQ("does not have 2 elements, or\n" | 317 EXPECT_EQ("doesn't have 2 elements, or\n" |
281 "element 0 is not equal to \"one\", or\n" | 318 "element #0 isn't equal to \"one\", or\n" |
282 "element 1 is not equal to \"two\"", DescribeNegation(m)); | 319 "element #1 isn't equal to \"two\"", DescribeNegation(m)); |
283 } | 320 } |
284 | 321 |
285 TEST(ElementsAreTest, DoesNotExplainTrivialMatch) { | 322 TEST(ElementsAreTest, DoesNotExplainTrivialMatch) { |
286 Matcher<const list<int>& > m = ElementsAre(1, Ne(2)); | 323 Matcher<const list<int>& > m = ElementsAre(1, Ne(2)); |
287 | 324 |
288 list<int> test_list; | 325 list<int> test_list; |
289 test_list.push_back(1); | 326 test_list.push_back(1); |
290 test_list.push_back(3); | 327 test_list.push_back(3); |
291 EXPECT_EQ("", Explain(m, test_list)); // No need to explain anything. | 328 EXPECT_EQ("", Explain(m, test_list)); // No need to explain anything. |
292 } | 329 } |
293 | 330 |
294 TEST(ElementsAreTest, ExplainsNonTrivialMatch) { | 331 TEST(ElementsAreTest, ExplainsNonTrivialMatch) { |
295 Matcher<const vector<int>& > m = | 332 Matcher<const vector<int>& > m = |
296 ElementsAre(GreaterThan(1), 0, GreaterThan(2)); | 333 ElementsAre(GreaterThan(1), 0, GreaterThan(2)); |
297 | 334 |
298 const int a[] = { 10, 0, 100 }; | 335 const int a[] = { 10, 0, 100 }; |
299 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a)); | 336 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a)); |
300 EXPECT_EQ("element 0 is 9 more than 1,\n" | 337 EXPECT_EQ("whose element #0 matches, which is 9 more than 1,\n" |
301 "element 2 is 98 more than 2", Explain(m, test_vector)); | 338 "and whose element #2 matches, which is 98 more than 2", |
| 339 Explain(m, test_vector)); |
302 } | 340 } |
303 | 341 |
304 TEST(ElementsAreTest, CanExplainMismatchWrongSize) { | 342 TEST(ElementsAreTest, CanExplainMismatchWrongSize) { |
305 Matcher<const list<int>& > m = ElementsAre(1, 3); | 343 Matcher<const list<int>& > m = ElementsAre(1, 3); |
306 | 344 |
307 list<int> test_list; | 345 list<int> test_list; |
308 // No need to explain when the container is empty. | 346 // No need to explain when the container is empty. |
309 EXPECT_EQ("", Explain(m, test_list)); | 347 EXPECT_EQ("", Explain(m, test_list)); |
310 | 348 |
311 test_list.push_back(1); | 349 test_list.push_back(1); |
312 EXPECT_EQ("has 1 element", Explain(m, test_list)); | 350 EXPECT_EQ("which has 1 element", Explain(m, test_list)); |
313 } | 351 } |
314 | 352 |
315 TEST(ElementsAreTest, CanExplainMismatchRightSize) { | 353 TEST(ElementsAreTest, CanExplainMismatchRightSize) { |
316 Matcher<const vector<int>& > m = ElementsAre(1, GreaterThan(5)); | 354 Matcher<const vector<int>& > m = ElementsAre(1, GreaterThan(5)); |
317 | 355 |
318 vector<int> v; | 356 vector<int> v; |
319 v.push_back(2); | 357 v.push_back(2); |
320 v.push_back(1); | 358 v.push_back(1); |
321 EXPECT_EQ("element 0 doesn't match", Explain(m, v)); | 359 EXPECT_EQ("whose element #0 doesn't match", Explain(m, v)); |
322 | 360 |
323 v[0] = 1; | 361 v[0] = 1; |
324 EXPECT_EQ("element 1 doesn't match (is 4 less than 5)", Explain(m, v)); | 362 EXPECT_EQ("whose element #1 doesn't match, which is 4 less than 5", |
| 363 Explain(m, v)); |
325 } | 364 } |
326 | 365 |
327 TEST(ElementsAreTest, MatchesOneElementVector) { | 366 TEST(ElementsAreTest, MatchesOneElementVector) { |
328 vector<string> test_vector; | 367 vector<string> test_vector; |
329 test_vector.push_back("test string"); | 368 test_vector.push_back("test string"); |
330 | 369 |
331 EXPECT_THAT(test_vector, ElementsAre(StrEq("test string"))); | 370 EXPECT_THAT(test_vector, ElementsAre(StrEq("test string"))); |
332 } | 371 } |
333 | 372 |
334 TEST(ElementsAreTest, MatchesOneElementList) { | 373 TEST(ElementsAreTest, MatchesOneElementList) { |
(...skipping 688 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1023 set<int> some_set; | 1062 set<int> some_set; |
1024 some_set.insert(3); | 1063 some_set.insert(3); |
1025 some_set.insert(1); | 1064 some_set.insert(1); |
1026 EXPECT_THAT(some_set, Not(Contains(4))); | 1065 EXPECT_THAT(some_set, Not(Contains(4))); |
1027 | 1066 |
1028 set<const char*> c_string_set; | 1067 set<const char*> c_string_set; |
1029 c_string_set.insert("hello"); | 1068 c_string_set.insert("hello"); |
1030 EXPECT_THAT(c_string_set, Not(Contains(string("hello").c_str()))); | 1069 EXPECT_THAT(c_string_set, Not(Contains(string("hello").c_str()))); |
1031 } | 1070 } |
1032 | 1071 |
1033 TEST(ContainsTest, DescribesItselfCorrectly) { | 1072 TEST(ContainsTest, ExplainsMatchResultCorrectly) { |
1034 const int a[2] = { 1, 2 }; | 1073 const int a[2] = { 1, 2 }; |
1035 Matcher<const int(&)[2]> m = Contains(2); | 1074 Matcher<const int(&)[2]> m = Contains(2); |
1036 EXPECT_EQ("element 1 matches", Explain(m, a)); | 1075 EXPECT_EQ("whose element #1 matches", Explain(m, a)); |
1037 | 1076 |
1038 m = Contains(3); | 1077 m = Contains(3); |
1039 EXPECT_EQ("", Explain(m, a)); | 1078 EXPECT_EQ("", Explain(m, a)); |
| 1079 |
| 1080 m = Contains(GreaterThan(0)); |
| 1081 EXPECT_EQ("whose element #0 matches, which is 1 more than 0", Explain(m, a)); |
| 1082 |
| 1083 m = Contains(GreaterThan(10)); |
| 1084 EXPECT_EQ("", Explain(m, a)); |
1040 } | 1085 } |
1041 | 1086 |
1042 TEST(ContainsTest, ExplainsMatchResultCorrectly) { | 1087 TEST(ContainsTest, DescribesItselfCorrectly) { |
1043 Matcher<vector<int> > m = Contains(1); | 1088 Matcher<vector<int> > m = Contains(1); |
1044 EXPECT_EQ("contains at least one element that is equal to 1", Describe(m)); | 1089 EXPECT_EQ("contains at least one element that is equal to 1", Describe(m)); |
1045 | 1090 |
1046 Matcher<vector<int> > m2 = Not(m); | 1091 Matcher<vector<int> > m2 = Not(m); |
1047 EXPECT_EQ("doesn't contain any element that is equal to 1", Describe(m2)); | 1092 EXPECT_EQ("doesn't contain any element that is equal to 1", Describe(m2)); |
1048 } | 1093 } |
1049 | 1094 |
1050 TEST(ContainsTest, MapMatchesWhenElementIsInContainer) { | 1095 TEST(ContainsTest, MapMatchesWhenElementIsInContainer) { |
1051 map<const char*, int> my_map; | 1096 map<const char*, int> my_map; |
1052 const char* bar = "a string"; | 1097 const char* bar = "a string"; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1098 EXPECT_THAT(a, Contains(Contains(5))); | 1143 EXPECT_THAT(a, Contains(Contains(5))); |
1099 EXPECT_THAT(a, Not(Contains(ElementsAre(3, 4, 5)))); | 1144 EXPECT_THAT(a, Not(Contains(ElementsAre(3, 4, 5)))); |
1100 EXPECT_THAT(a, Contains(Not(Contains(5)))); | 1145 EXPECT_THAT(a, Contains(Not(Contains(5)))); |
1101 } | 1146 } |
1102 | 1147 |
1103 #ifdef _MSC_VER | 1148 #ifdef _MSC_VER |
1104 #pragma warning(pop) | 1149 #pragma warning(pop) |
1105 #endif | 1150 #endif |
1106 | 1151 |
1107 } // namespace | 1152 } // namespace |
OLD | NEW |