OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #library('matcherTest'); | 5 #library('matcherTest'); |
6 #import('../../../lib/unittest/unittest.dart'); | 6 #import('../../../lib/unittest/unittest.dart'); |
7 #source('test_utils.dart'); | 7 #source('test_utils.dart'); |
8 | 8 |
9 doesNotThrow() {} | 9 doesNotThrow() {} |
10 doesThrow() { throw 'X'; } | 10 doesThrow() { throw 'X'; } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 var a = new Map(); | 55 var a = new Map(); |
56 var b = new Map(); | 56 var b = new Map(); |
57 shouldPass(a, same(a)); | 57 shouldPass(a, same(a)); |
58 shouldFail(b, same(a), "Expected: same instance as <{}> but: was <{}>"); | 58 shouldFail(b, same(a), "Expected: same instance as <{}> but: was <{}>"); |
59 }); | 59 }); |
60 | 60 |
61 test('equals', () { | 61 test('equals', () { |
62 var a = new Map(); | 62 var a = new Map(); |
63 var b = new Map(); | 63 var b = new Map(); |
64 shouldPass(a, equals(a)); | 64 shouldPass(a, equals(a)); |
65 shouldFail(a, equals(b), "Expected: <{}> but: was <{}>"); | 65 shouldPass(a, equals(b)); |
66 }); | 66 }); |
67 | 67 |
68 test('anything', () { | 68 test('anything', () { |
69 var a = new Map(); | 69 var a = new Map(); |
70 shouldPass(0, anything); | 70 shouldPass(0, anything); |
71 shouldPass(null, anything); | 71 shouldPass(null, anything); |
72 shouldPass(a, anything); | 72 shouldPass(a, anything); |
73 shouldFail(a, isNot(anything), "Expected: not anything but: was <{}>"); | 73 shouldFail(a, isNot(anything), "Expected: not anything but: was <{}>"); |
74 }); | 74 }); |
75 | 75 |
76 test('throws', () { | 76 test('throws', () { |
77 shouldFail(doesNotThrow, throws, | 77 shouldFail(doesNotThrow, throws, |
78 "Expected: throws an exception but: no exception"); | 78 "Expected: throws an exception but: no exception"); |
79 shouldPass(doesThrow, throws); | 79 shouldPass(doesThrow, throws); |
80 }); | 80 }); |
81 | 81 |
| 82 test('throwsA', () { |
| 83 shouldPass(doesThrow, throwsA(equals('X'))); |
| 84 shouldFail(doesThrow, throwsA(equals('Y')), |
| 85 "Expected: throws an exception which matches 'Y' " |
| 86 "but: no exception or exception does not match 'Y'"); |
| 87 }); |
| 88 |
| 89 test('throwsBadNumberFormatException', () { |
| 90 shouldPass(() { throw new BadNumberFormatException(''); }, |
| 91 throwsBadNumberFormatException); |
| 92 shouldFail(() { throw new Exception(); }, |
| 93 throwsBadNumberFormatException, |
| 94 "Expected: throws an exception which matches BadNumberFormatException " |
| 95 "but: no exception or exception does not match " |
| 96 "BadNumberFormatException"); |
| 97 }); |
| 98 |
| 99 test('throwsIllegalArgumentException', () { |
| 100 shouldPass(() { throw new IllegalArgumentException(''); }, |
| 101 throwsIllegalArgumentException); |
| 102 shouldFail(() { throw new Exception(); }, |
| 103 throwsIllegalArgumentException, |
| 104 "Expected: throws an exception which matches IllegalArgumentException " |
| 105 "but: no exception or exception does not match " |
| 106 "IllegalArgumentException"); |
| 107 }); |
| 108 |
| 109 test('throwsIllegalJSRegExpException', () { |
| 110 shouldPass(() { throw new IllegalJSRegExpException('',''); }, |
| 111 throwsIllegalJSRegExpException); |
| 112 shouldFail(() { throw new Exception(); }, |
| 113 throwsIllegalJSRegExpException, |
| 114 "Expected: throws an exception which matches IllegalJSRegExpException " |
| 115 "but: no exception or exception does not match " |
| 116 "IllegalJSRegExpException"); |
| 117 }); |
| 118 |
| 119 test('throwsIndexOutOfRangeException', () { |
| 120 shouldPass(() { throw new IndexOutOfRangeException(''); }, |
| 121 throwsIndexOutOfRangeException); |
| 122 shouldFail(() { throw new Exception(); }, |
| 123 throwsIndexOutOfRangeException, |
| 124 "Expected: throws an exception which matches IndexOutOfRangeException " |
| 125 "but: no exception or exception does not match " |
| 126 "IndexOutOfRangeException"); |
| 127 }); |
| 128 |
| 129 test('throwsNoSuchMethodException', () { |
| 130 shouldPass(() { throw new NoSuchMethodException(null, '', null); }, |
| 131 throwsNoSuchMethodException); |
| 132 shouldFail(() { throw new Exception(); }, |
| 133 throwsNoSuchMethodException, |
| 134 "Expected: throws an exception which matches NoSuchMethodException " |
| 135 "but: no exception or exception does not match " |
| 136 "NoSuchMethodException"); |
| 137 }); |
| 138 |
| 139 test('throwsNotImplementedException', () { |
| 140 shouldPass(() { throw new NotImplementedException(''); }, |
| 141 throwsNotImplementedException); |
| 142 shouldFail(() { throw new Exception(); }, |
| 143 throwsNotImplementedException, |
| 144 "Expected: throws an exception which matches NotImplementedException " |
| 145 "but: no exception or exception does not match " |
| 146 "NotImplementedException"); |
| 147 }); |
| 148 |
| 149 test('throwsNullPointerException', () { |
| 150 shouldPass(() { throw new NullPointerException(''); }, |
| 151 throwsNullPointerException); |
| 152 shouldFail(() { throw new Exception(); }, |
| 153 throwsNullPointerException, |
| 154 "Expected: throws an exception which matches NullPointerException " |
| 155 "but: no exception or exception does not match " |
| 156 "NullPointerException"); |
| 157 }); |
| 158 |
| 159 test('throwsUnsupportedOperationException', () { |
| 160 shouldPass(() { throw new UnsupportedOperationException(''); }, |
| 161 throwsUnsupportedOperationException); |
| 162 shouldFail(() { throw new Exception(); }, |
| 163 throwsUnsupportedOperationException, |
| 164 "Expected: throws an exception which matches " |
| 165 "UnsupportedOperationException " |
| 166 "but: no exception or exception does not match " |
| 167 "UnsupportedOperationException"); |
| 168 }); |
| 169 |
82 test('returnsNormally', () { | 170 test('returnsNormally', () { |
83 shouldPass(doesNotThrow, returnsNormally); | 171 shouldPass(doesNotThrow, returnsNormally); |
84 shouldFail(doesThrow, returnsNormally, | 172 shouldFail(doesThrow, returnsNormally, |
85 "Expected: return normally but: threw exception"); | 173 "Expected: return normally but: threw exception"); |
86 }); | 174 }); |
87 | 175 |
88 test('hasLength', () { | 176 test('hasLength', () { |
89 var a = new Map(); | 177 var a = new Map(); |
90 var b = new List(); | 178 var b = new List(); |
91 shouldPass(a, hasLength(0)); | 179 shouldPass(a, hasLength(0)); |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 | 306 |
219 test('inClosedOpenRange', () { | 307 test('inClosedOpenRange', () { |
220 shouldPass(0, inClosedOpenRange(0,2)); | 308 shouldPass(0, inClosedOpenRange(0,2)); |
221 shouldPass(1, inClosedOpenRange(0,2)); | 309 shouldPass(1, inClosedOpenRange(0,2)); |
222 shouldFail(2, inClosedOpenRange(0,2), | 310 shouldFail(2, inClosedOpenRange(0,2), |
223 "Expected: be in range from 0 (inclusive) to 2 (exclusive) " | 311 "Expected: be in range from 0 (inclusive) to 2 (exclusive) " |
224 "but: was <2>"); | 312 "but: was <2>"); |
225 }); | 313 }); |
226 }); | 314 }); |
227 | 315 |
228 | |
229 group('String Matchers', () { | 316 group('String Matchers', () { |
230 | 317 |
231 test('isEmpty', () { | 318 test('isEmpty', () { |
232 shouldPass('', isEmpty); | 319 shouldPass('', isEmpty); |
233 shouldFail(null, isEmpty, | 320 shouldFail(null, isEmpty, |
234 "Expected: empty but: was <null>"); | 321 "Expected: empty but: was <null>"); |
235 shouldFail(0, isEmpty, | 322 shouldFail(0, isEmpty, |
236 "Expected: empty but: was <0>"); | 323 "Expected: empty but: was <0>"); |
237 shouldFail('a', isEmpty, "Expected: empty but: was 'a'"); | 324 shouldFail('a', isEmpty, "Expected: empty but: was 'a'"); |
238 }); | 325 }); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 shouldPass([], isEmpty); | 395 shouldPass([], isEmpty); |
309 shouldFail([1], isEmpty, "Expected: empty but: was <[1]>"); | 396 shouldFail([1], isEmpty, "Expected: empty but: was <[1]>"); |
310 }); | 397 }); |
311 | 398 |
312 test('contains', () { | 399 test('contains', () { |
313 var d = [1, 2]; | 400 var d = [1, 2]; |
314 shouldPass(d, contains(1)); | 401 shouldPass(d, contains(1)); |
315 shouldFail(d, contains(0), "Expected: contains <0> but: was <[1, 2]>"); | 402 shouldFail(d, contains(0), "Expected: contains <0> but: was <[1, 2]>"); |
316 }); | 403 }); |
317 | 404 |
| 405 test('isIn', () { |
| 406 var d = [1, 2]; |
| 407 shouldPass(1, isIn(d)); |
| 408 shouldFail(0, isIn(d), "Expected: is in <[1, 2]> but: was <0>"); |
| 409 }); |
318 | 410 |
319 test('everyElement', () { | 411 test('everyElement', () { |
320 var d = [1, 2]; | 412 var d = [1, 2]; |
321 var e = [1, 1, 1]; | 413 var e = [1, 1, 1]; |
322 shouldFail(d, everyElement(1), | 414 shouldFail(d, everyElement(1), |
323 "Expected: every element <1> but: was <[1, 2]>"); | 415 "Expected: every element <1> but: was <[1, 2]>"); |
324 shouldPass(e, everyElement(1)); | 416 shouldPass(e, everyElement(1)); |
325 }); | 417 }); |
326 | 418 |
327 test('someElement', () { | 419 test('someElement', () { |
328 var d = [1, 2]; | 420 var d = [1, 2]; |
329 var e = [1, 1, 1]; | 421 var e = [1, 1, 1]; |
330 shouldPass(d, someElement(2)); | 422 shouldPass(d, someElement(2)); |
331 shouldFail(e, someElement(2), | 423 shouldFail(e, someElement(2), |
332 "Expected: some element <2> but: was <[1, 1, 1]>"); | 424 "Expected: some element <2> but: was <[1, 1, 1]>"); |
333 }); | 425 }); |
334 | 426 |
335 test('orderedEquals', () { | 427 test('orderedEquals', () { |
| 428 shouldPass([null], orderedEquals([null])); |
336 var d = [1, 2]; | 429 var d = [1, 2]; |
337 shouldPass(d, orderedEquals([1, 2])); | 430 shouldPass(d, orderedEquals([1, 2])); |
338 shouldFail(d, orderedEquals([2, 1]), | 431 shouldFail(d, orderedEquals([2, 1]), |
339 "Expected: equals <[2, 1]> ordered " | 432 "Expected: equals <[2, 1]> ordered " |
340 "but: mismatch at position 0"); | 433 "but: was <1> mismatch at position 0"); |
341 }); | 434 }); |
342 | 435 |
343 test('unorderedEquals', () { | 436 test('unorderedEquals', () { |
344 var d = [1, 2]; | 437 var d = [1, 2]; |
345 shouldPass(d, unorderedEquals([2, 1])); | 438 shouldPass(d, unorderedEquals([2, 1])); |
346 shouldFail(d, unorderedEquals([1]), | 439 shouldFail(d, unorderedEquals([1]), |
347 "Expected: equals <[1]> unordered " | 440 "Expected: equals <[1]> unordered " |
348 "but: has too many elements (2 > 1)"); | 441 "but: has too many elements (2 > 1)"); |
349 shouldFail(d, unorderedEquals([3, 2, 1]), | 442 shouldFail(d, unorderedEquals([3, 2, 1]), |
350 "Expected: equals <[3, 2, 1]> unordered " | 443 "Expected: equals <[3, 2, 1]> unordered " |
351 "but: has too few elements (2 < 3)"); | 444 "but: has too few elements (2 < 3)"); |
352 shouldFail(d, unorderedEquals([3, 1]), | 445 shouldFail(d, unorderedEquals([3, 1]), |
353 "Expected: equals <[3, 1]> unordered " | 446 "Expected: equals <[3, 1]> unordered " |
354 "but: has no match for element 3 at position 0"); | 447 "but: has no match for element <3> at position 0"); |
355 }); | 448 }); |
356 }); | 449 }); |
357 | 450 |
358 group('Map Matchers', () { | 451 group('Map Matchers', () { |
359 | 452 |
360 test('isEmpty', () { | 453 test('isEmpty', () { |
361 var a = new Map(); | 454 var a = new Map(); |
362 shouldPass({}, isEmpty); | 455 shouldPass({}, isEmpty); |
363 shouldPass(a, isEmpty); | 456 shouldPass(a, isEmpty); |
364 a['foo'] = 'bar'; | 457 a['foo'] = 'bar'; |
(...skipping 21 matching lines...) Expand all Loading... |
386 | 479 |
387 test('containsPair', () { | 480 test('containsPair', () { |
388 var a = new Map(); | 481 var a = new Map(); |
389 a['foo'] = 'bar'; | 482 a['foo'] = 'bar'; |
390 shouldPass(a, containsPair('foo', 'bar')); | 483 shouldPass(a, containsPair('foo', 'bar')); |
391 shouldFail(a, containsPair('foo', 'ba'), | 484 shouldFail(a, containsPair('foo', 'ba'), |
392 "Expected: contains pair 'foo' => 'ba' " | 485 "Expected: contains pair 'foo' => 'ba' " |
393 "but: contains key 'foo' but with value was 'bar'"); | 486 "but: contains key 'foo' but with value was 'bar'"); |
394 shouldFail(a, containsPair('fo', 'bar'), | 487 shouldFail(a, containsPair('fo', 'bar'), |
395 "Expected: contains pair 'fo' => 'bar' " | 488 "Expected: contains pair 'fo' => 'bar' " |
396 "but: <{foo: bar}>' doesn't contain key ''fo'"); | 489 "but: <{foo: bar}> doesn't contain key 'fo'"); |
397 }); | 490 }); |
398 | 491 |
399 test('hasLength', () { | 492 test('hasLength', () { |
400 var a = new Map(); | 493 var a = new Map(); |
401 a['foo'] = 'bar'; | 494 a['foo'] = 'bar'; |
402 var b = new Map(); | 495 var b = new Map(); |
403 shouldPass(a, hasLength(1)); | 496 shouldPass(a, hasLength(1)); |
404 shouldFail(b, hasLength(1), | 497 shouldFail(b, hasLength(1), |
405 "Expected: an object with length of <1> " | 498 "Expected: an object with length of <1> " |
406 "but: was <{}> with length of <0>"); | 499 "but: was <{}> with length of <0>"); |
407 }); | 500 }); |
408 }); | 501 }); |
409 | 502 |
410 group('Operator Matchers', () { | 503 group('Operator Matchers', () { |
411 | 504 |
412 test('anyOf', () { | 505 test('anyOf', () { |
413 shouldFail(0, anyOf([equals(1), equals(2)]), | 506 shouldFail(0, anyOf([equals(1), equals(2)]), |
414 "Expected: (<1> or <2>) but: was <0>"); | 507 "Expected: (<1> or <2>) but: was <0>"); |
415 shouldPass(1, anyOf([equals(1), equals(2)])); | 508 shouldPass(1, anyOf([equals(1), equals(2)])); |
416 }); | 509 }); |
417 | 510 |
418 test('allOf', () { | 511 test('allOf', () { |
419 shouldPass(1, allOf([lessThan(10), greaterThan(0)])); | 512 shouldPass(1, allOf([lessThan(10), greaterThan(0)])); |
420 shouldFail(-1, allOf([lessThan(10), greaterThan(0)]), | 513 shouldFail(-1, allOf([lessThan(10), greaterThan(0)]), |
421 "Expected: (a value less than <10> and a value greater than <0>) " | 514 "Expected: (a value less than <10> and a value greater than <0>) " |
422 "but: a value greater than <0> was <-1>"); | 515 "but: a value greater than <0> was <-1>"); |
423 }); | 516 }); |
424 }); | 517 }); |
| 518 |
| 519 group('Predicate Matchers', () { |
| 520 test('isInstanceOf', () { |
| 521 shouldFail(0, predicate((x) => x is String, "an instance of String"), |
| 522 "Expected: an instance of String but: was <0>"); |
| 523 shouldPass('cow', predicate((x) => x is String, "an instance of String")); |
| 524 }); |
| 525 }); |
425 } | 526 } |
426 | 527 |
OLD | NEW |