| 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'; } |
| 11 | 11 |
| 12 class PrefixMatcher extends BaseMatcher { | 12 class PrefixMatcher extends BaseMatcher { |
| 13 final String _prefix; | 13 final String _prefix; |
| 14 const PrefixMatcher(this._prefix); | 14 const PrefixMatcher(this._prefix); |
| 15 bool matches(item) { | 15 bool matches(item, MatchState matchState) { |
| 16 return item is String && | 16 return item is String && |
| 17 (collapseWhitespace(item)).startsWith(collapseWhitespace(_prefix)); | 17 (collapseWhitespace(item)).startsWith(collapseWhitespace(_prefix)); |
| 18 } | 18 } |
| 19 | 19 |
| 20 Description describe(Description description) => | 20 Description describe(Description description) => |
| 21 description.add('a string starting with '). | 21 description.add('a string starting with '). |
| 22 addDescriptionOf(collapseWhitespace(_prefix)). | 22 addDescriptionOf(collapseWhitespace(_prefix)). |
| 23 add(' ignoring whitespace'); | 23 add(' ignoring whitespace'); |
| 24 } | 24 } |
| 25 | 25 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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', () { | 82 test('throwsA', () { |
| 83 shouldPass(doesThrow, throwsA(equals('X'))); | 83 shouldPass(doesThrow, throwsA(equals('X'))); |
| 84 shouldFail(doesThrow, throwsA(equals('Y')), | 84 shouldFail(doesThrow, throwsA(equals('Y')), |
| 85 "Expected: throws an exception which matches 'Y' " | 85 "Expected: throws an exception which matches 'Y' " |
| 86 "but: no exception or exception does not match 'Y'."); | 86 "but: exception 'X' does not match 'Y'."); |
| 87 }); | 87 }); |
| 88 | 88 |
| 89 test('throwsBadNumberFormatException', () { | 89 test('throwsBadNumberFormatException', () { |
| 90 shouldPass(() { throw new BadNumberFormatException(''); }, | 90 shouldPass(() { throw new BadNumberFormatException(''); }, |
| 91 throwsBadNumberFormatException); | 91 throwsBadNumberFormatException); |
| 92 shouldFail(() { throw new Exception(); }, | 92 shouldFail(() { throw new Exception(); }, |
| 93 throwsBadNumberFormatException, | 93 throwsBadNumberFormatException, |
| 94 "Expected: throws an exception which matches BadNumberFormatException " | 94 "Expected: throws an exception which matches BadNumberFormatException " |
| 95 "but: no exception or exception does not match " | 95 "but: exception <Exception> does not match " |
| 96 "BadNumberFormatException."); | 96 "BadNumberFormatException."); |
| 97 }); | 97 }); |
| 98 | 98 |
| 99 test('throwsIllegalArgumentException', () { | 99 test('throwsIllegalArgumentException', () { |
| 100 shouldPass(() { throw new IllegalArgumentException(''); }, | 100 shouldPass(() { throw new IllegalArgumentException(''); }, |
| 101 throwsIllegalArgumentException); | 101 throwsIllegalArgumentException); |
| 102 shouldFail(() { throw new Exception(); }, | 102 shouldFail(() { throw new Exception(); }, |
| 103 throwsIllegalArgumentException, | 103 throwsIllegalArgumentException, |
| 104 "Expected: throws an exception which matches IllegalArgumentException " | 104 "Expected: throws an exception which matches IllegalArgumentException " |
| 105 "but: no exception or exception does not match " | 105 "but: exception <Exception> does not match " |
| 106 "IllegalArgumentException."); | 106 "IllegalArgumentException."); |
| 107 }); | 107 }); |
| 108 | 108 |
| 109 test('throwsIllegalJSRegExpException', () { | 109 test('throwsIllegalJSRegExpException', () { |
| 110 shouldPass(() { throw new IllegalJSRegExpException('',''); }, | 110 shouldPass(() { throw new IllegalJSRegExpException('',''); }, |
| 111 throwsIllegalJSRegExpException); | 111 throwsIllegalJSRegExpException); |
| 112 shouldFail(() { throw new Exception(); }, | 112 shouldFail(() { throw new Exception(); }, |
| 113 throwsIllegalJSRegExpException, | 113 throwsIllegalJSRegExpException, |
| 114 "Expected: throws an exception which matches IllegalJSRegExpException " | 114 "Expected: throws an exception which matches IllegalJSRegExpException " |
| 115 "but: no exception or exception does not match " | 115 "but: exception <Exception> does not match " |
| 116 "IllegalJSRegExpException."); | 116 "IllegalJSRegExpException."); |
| 117 }); | 117 }); |
| 118 | 118 |
| 119 test('throwsIndexOutOfRangeException', () { | 119 test('throwsIndexOutOfRangeException', () { |
| 120 shouldPass(() { throw new IndexOutOfRangeException(0); }, | 120 shouldPass(() { throw new IndexOutOfRangeException(0); }, |
| 121 throwsIndexOutOfRangeException); | 121 throwsIndexOutOfRangeException); |
| 122 shouldFail(() { throw new Exception(); }, | 122 shouldFail(() { throw new Exception(); }, |
| 123 throwsIndexOutOfRangeException, | 123 throwsIndexOutOfRangeException, |
| 124 "Expected: throws an exception which matches IndexOutOfRangeException " | 124 "Expected: throws an exception which matches IndexOutOfRangeException " |
| 125 "but: no exception or exception does not match " | 125 "but: exception <Exception> does not match " |
| 126 "IndexOutOfRangeException."); | 126 "IndexOutOfRangeException."); |
| 127 }); | 127 }); |
| 128 | 128 |
| 129 test('throwsNoSuchMethodException', () { | 129 test('throwsNoSuchMethodException', () { |
| 130 shouldPass(() { throw new NoSuchMethodException(null, '', null); }, | 130 shouldPass(() { throw new NoSuchMethodException(null, '', null); }, |
| 131 throwsNoSuchMethodException); | 131 throwsNoSuchMethodException); |
| 132 shouldFail(() { throw new Exception(); }, | 132 shouldFail(() { throw new Exception(); }, |
| 133 throwsNoSuchMethodException, | 133 throwsNoSuchMethodException, |
| 134 "Expected: throws an exception which matches NoSuchMethodException " | 134 "Expected: throws an exception which matches NoSuchMethodException " |
| 135 "but: no exception or exception does not match " | 135 "but: exception <Exception> does not match " |
| 136 "NoSuchMethodException."); | 136 "NoSuchMethodException."); |
| 137 }); | 137 }); |
| 138 | 138 |
| 139 test('throwsNotImplementedException', () { | 139 test('throwsNotImplementedException', () { |
| 140 shouldPass(() { throw new NotImplementedException(''); }, | 140 shouldPass(() { throw new NotImplementedException(''); }, |
| 141 throwsNotImplementedException); | 141 throwsNotImplementedException); |
| 142 shouldFail(() { throw new Exception(); }, | 142 shouldFail(() { throw new Exception(); }, |
| 143 throwsNotImplementedException, | 143 throwsNotImplementedException, |
| 144 "Expected: throws an exception which matches NotImplementedException " | 144 "Expected: throws an exception which matches NotImplementedException " |
| 145 "but: no exception or exception does not match " | 145 "but: exception <Exception> does not match " |
| 146 "NotImplementedException."); | 146 "NotImplementedException."); |
| 147 }); | 147 }); |
| 148 | 148 |
| 149 test('throwsNullPointerException', () { | 149 test('throwsNullPointerException', () { |
| 150 shouldPass(() { throw new NullPointerException(''); }, | 150 shouldPass(() { throw new NullPointerException(''); }, |
| 151 throwsNullPointerException); | 151 throwsNullPointerException); |
| 152 shouldFail(() { throw new Exception(); }, | 152 shouldFail(() { throw new Exception(); }, |
| 153 throwsNullPointerException, | 153 throwsNullPointerException, |
| 154 "Expected: throws an exception which matches NullPointerException " | 154 "Expected: throws an exception which matches NullPointerException " |
| 155 "but: no exception or exception does not match " | 155 "but: exception <Exception> does not match " |
| 156 "NullPointerException."); | 156 "NullPointerException."); |
| 157 }); | 157 }); |
| 158 | 158 |
| 159 test('throwsUnsupportedOperationException', () { | 159 test('throwsUnsupportedOperationException', () { |
| 160 shouldPass(() { throw new UnsupportedOperationException(''); }, | 160 shouldPass(() { throw new UnsupportedOperationException(''); }, |
| 161 throwsUnsupportedOperationException); | 161 throwsUnsupportedOperationException); |
| 162 shouldFail(() { throw new Exception(); }, | 162 shouldFail(() { throw new Exception(); }, |
| 163 throwsUnsupportedOperationException, | 163 throwsUnsupportedOperationException, |
| 164 "Expected: throws an exception which matches " | 164 "Expected: throws an exception which matches " |
| 165 "UnsupportedOperationException " | 165 "UnsupportedOperationException " |
| 166 "but: no exception or exception does not match " | 166 "but: exception <Exception> does not match " |
| 167 "UnsupportedOperationException."); | 167 "UnsupportedOperationException."); |
| 168 }); | 168 }); |
| 169 | 169 |
| 170 test('returnsNormally', () { | 170 test('returnsNormally', () { |
| 171 shouldPass(doesNotThrow, returnsNormally); | 171 shouldPass(doesNotThrow, returnsNormally); |
| 172 shouldFail(doesThrow, returnsNormally, | 172 shouldFail(doesThrow, returnsNormally, |
| 173 "Expected: return normally but: threw exception."); | 173 "Expected: return normally but: threw 'X'."); |
| 174 }); | 174 }); |
| 175 | 175 |
| 176 test('hasLength', () { | 176 test('hasLength', () { |
| 177 var a = new Map(); | 177 var a = new Map(); |
| 178 var b = new List(); | 178 var b = new List(); |
| 179 shouldPass(a, hasLength(0)); | 179 shouldPass(a, hasLength(0)); |
| 180 shouldPass(b, hasLength(0)); | 180 shouldPass(b, hasLength(0)); |
| 181 shouldPass('a', hasLength(1)); | 181 shouldPass('a', hasLength(1)); |
| 182 shouldFail(0, hasLength(0), new PrefixMatcher( | 182 shouldFail(0, hasLength(0), new PrefixMatcher( |
| 183 "Expected: an object with length of <0> " | 183 "Expected: an object with length of <0> " |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 test('isIn', () { | 405 test('isIn', () { |
| 406 var d = [1, 2]; | 406 var d = [1, 2]; |
| 407 shouldPass(1, isIn(d)); | 407 shouldPass(1, isIn(d)); |
| 408 shouldFail(0, isIn(d), "Expected: is in <[1, 2]> but: was <0>."); | 408 shouldFail(0, isIn(d), "Expected: is in <[1, 2]> but: was <0>."); |
| 409 }); | 409 }); |
| 410 | 410 |
| 411 test('everyElement', () { | 411 test('everyElement', () { |
| 412 var d = [1, 2]; | 412 var d = [1, 2]; |
| 413 var e = [1, 1, 1]; | 413 var e = [1, 1, 1]; |
| 414 shouldFail(d, everyElement(1), | 414 shouldFail(d, everyElement(1), |
| 415 "Expected: every element <1> but: was <[1, 2]>."); | 415 "Expected: every element <1> but: was <2> at position 1."); |
| 416 shouldPass(e, everyElement(1)); | 416 shouldPass(e, everyElement(1)); |
| 417 }); | 417 }); |
| 418 | 418 |
| 419 test('someElement', () { | 419 test('someElement', () { |
| 420 var d = [1, 2]; | 420 var d = [1, 2]; |
| 421 var e = [1, 1, 1]; | 421 var e = [1, 1, 1]; |
| 422 shouldPass(d, someElement(2)); | 422 shouldPass(d, someElement(2)); |
| 423 shouldFail(e, someElement(2), | 423 shouldFail(e, someElement(2), |
| 424 "Expected: some element <2> but: was <[1, 1, 1]>."); | 424 "Expected: some element <2> but: was <[1, 1, 1]>."); |
| 425 }); | 425 }); |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 518 | 518 |
| 519 group('Predicate Matchers', () { | 519 group('Predicate Matchers', () { |
| 520 test('isInstanceOf', () { | 520 test('isInstanceOf', () { |
| 521 shouldFail(0, predicate((x) => x is String, "an instance of String"), | 521 shouldFail(0, predicate((x) => x is String, "an instance of String"), |
| 522 "Expected: an instance of String but: was <0>."); | 522 "Expected: an instance of String but: was <0>."); |
| 523 shouldPass('cow', predicate((x) => x is String, "an instance of String")); | 523 shouldPass('cow', predicate((x) => x is String, "an instance of String")); |
| 524 }); | 524 }); |
| 525 }); | 525 }); |
| 526 } | 526 } |
| 527 | 527 |
| OLD | NEW |