| 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('unittestTest'); | 5 #library('matcherTest'); |
| 6 | 6 #import('../../../lib/unittest/unittest.dart'); |
| 7 #import('../../lib/unittest/unittest.dart'); | 7 #source('test_utils.dart'); |
| 8 | |
| 9 | 8 |
| 10 doesNotThrow() {} | 9 doesNotThrow() {} |
| 11 doesThrow() { throw 'X'; } | 10 doesThrow() { throw 'X'; } |
| 12 | 11 |
| 13 int errorCount; | |
| 14 String errorString; | |
| 15 var testHandler; | |
| 16 | |
| 17 class MyFailureHandler extends DefaultFailureHandler { | |
| 18 void fail(String reason) { | |
| 19 ++errorCount; | |
| 20 errorString = reason; | |
| 21 } | |
| 22 } | |
| 23 | |
| 24 void shouldFail(var value, Matcher matcher, expected) { | |
| 25 errorCount = 0; | |
| 26 errorString = ''; | |
| 27 configureExpectHandler(testHandler); | |
| 28 expect(value, matcher); | |
| 29 configureExpectHandler(null); | |
| 30 expect(errorCount, equals(1)); | |
| 31 if (expected is String) | |
| 32 expect(errorString, equalsIgnoringWhitespace(expected)); | |
| 33 else | |
| 34 expect(errorString, expected); | |
| 35 } | |
| 36 | |
| 37 void shouldPass(var value, Matcher matcher) { | |
| 38 errorCount = 0; | |
| 39 errorString = ''; | |
| 40 configureExpectHandler(testHandler); | |
| 41 expect(value, matcher); | |
| 42 configureExpectHandler(null); | |
| 43 expect(errorCount, equals(0)); | |
| 44 } | |
| 45 | |
| 46 class PrefixMatcher extends BaseMatcher { | 12 class PrefixMatcher extends BaseMatcher { |
| 47 final String _prefix; | 13 final String _prefix; |
| 48 const PrefixMatcher(this._prefix); | 14 const PrefixMatcher(this._prefix); |
| 49 bool matches(item) { | 15 bool matches(item) { |
| 50 return item is String && | 16 return item is String && |
| 51 (collapseWhitespace(item)).startsWith(collapseWhitespace(_prefix)); | 17 (collapseWhitespace(item)).startsWith(collapseWhitespace(_prefix)); |
| 52 } | 18 } |
| 53 | 19 |
| 54 Description describe(Description description) => | 20 Description describe(Description description) => |
| 55 description.add('a string starting with '). | 21 description.add('a string starting with '). |
| 56 addDescriptionOf(collapseWhitespace(_prefix)). | 22 addDescriptionOf(collapseWhitespace(_prefix)). |
| 57 add(' ignoring whitespace'); | 23 add(' ignoring whitespace'); |
| 58 } | 24 } |
| 59 | 25 |
| 60 void main() { | 26 void main() { |
| 61 | 27 |
| 62 testHandler = new MyFailureHandler(); | 28 initUtils(); |
| 63 | 29 |
| 64 var a = new Map(); | 30 var a = new Map(); |
| 65 var b = new Map(); | 31 var b = new Map(); |
| 66 var c = new List(); | 32 var c = new List(); |
| 67 | 33 |
| 68 // Core matchers | 34 // Core matchers |
| 69 | 35 |
| 70 group('Core matchers', () { | 36 group('Core matchers', () { |
| 71 test('isTrue', () { | 37 test('isTrue', () { |
| 72 shouldPass(true, isTrue); | 38 shouldPass(true, isTrue); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 102 shouldPass(a, anything); | 68 shouldPass(a, anything); |
| 103 shouldFail(a, isNot(anything), "Expected: not anything but: was <{}>"); | 69 shouldFail(a, isNot(anything), "Expected: not anything but: was <{}>"); |
| 104 }); | 70 }); |
| 105 | 71 |
| 106 test('throws', () { | 72 test('throws', () { |
| 107 shouldFail(doesNotThrow, throws, | 73 shouldFail(doesNotThrow, throws, |
| 108 "Expected: throws an exception but: no exception"); | 74 "Expected: throws an exception but: no exception"); |
| 109 shouldPass(doesThrow, throws); | 75 shouldPass(doesThrow, throws); |
| 110 }); | 76 }); |
| 111 | 77 |
| 112 test('throwsA', () { | |
| 113 shouldPass(doesThrow, throwsA(equals('X'))); | |
| 114 shouldFail(doesThrow, throwsA(equals('Y')), | |
| 115 "Expected: throws an exception which matches 'Y' " | |
| 116 "but: exception does not match 'Y'"); | |
| 117 }); | |
| 118 | |
| 119 test('returnsNormally', () { | 78 test('returnsNormally', () { |
| 120 shouldPass(doesNotThrow, returnsNormally); | 79 shouldPass(doesNotThrow, returnsNormally); |
| 121 shouldFail(doesThrow, returnsNormally, | 80 shouldFail(doesThrow, returnsNormally, |
| 122 "Expected: return normally but: threw exception"); | 81 "Expected: return normally but: threw exception"); |
| 123 }); | 82 }); |
| 124 | 83 |
| 125 /* Removing these temporarily while dart2js failure is investigated */ | |
| 126 /* | |
| 127 test('isInstanceOf', () { | |
| 128 shouldFail(0, new isInstanceOf<String>('String'), | |
| 129 "Expected: an instance of String but: was <0>"); | |
| 130 shouldPass('cow', new isInstanceOf<String>('String')); | |
| 131 }); | |
| 132 */ | |
| 133 | |
| 134 test('hasLength', () { | 84 test('hasLength', () { |
| 135 shouldPass(c, hasLength(0)); | 85 shouldPass(c, hasLength(0)); |
| 136 shouldPass(a, hasLength(0)); | 86 shouldPass(a, hasLength(0)); |
| 137 shouldPass('a', hasLength(1)); | 87 shouldPass('a', hasLength(1)); |
| 138 shouldFail(0, hasLength(0), new PrefixMatcher( | 88 shouldFail(0, hasLength(0), new PrefixMatcher( |
| 139 "Expected: an object with length of <0> " | 89 "Expected: an object with length of <0> " |
| 140 "but: was <0> has no length property")); | 90 "but: was <0> has no length property")); |
| 141 | 91 |
| 142 c.add(0); | 92 c.add(0); |
| 143 shouldPass(c, hasLength(1)); | 93 shouldPass(c, hasLength(1)); |
| (...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 447 test('allOf', () { | 397 test('allOf', () { |
| 448 shouldPass(1, allOf([lessThan(10), greaterThan(0)])); | 398 shouldPass(1, allOf([lessThan(10), greaterThan(0)])); |
| 449 shouldFail(-1, allOf([lessThan(10), greaterThan(0)]), | 399 shouldFail(-1, allOf([lessThan(10), greaterThan(0)]), |
| 450 "Expected: (a value less than <10> and a value greater than <0>) " | 400 "Expected: (a value less than <10> and a value greater than <0>) " |
| 451 "but: a value greater than <0> was <-1>"); | 401 "but: a value greater than <0> was <-1>"); |
| 452 }); | 402 }); |
| 453 }); | 403 }); |
| 454 } | 404 } |
| 455 | 405 |
| 456 | 406 |
| OLD | NEW |