| 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 | 5 |
| 6 /** | 6 /** |
| 7 * Returns a matcher that matches empty strings, maps or collections. | 7 * Returns a matcher that matches empty strings, maps or collections. |
| 8 */ | 8 */ |
| 9 final Matcher isEmpty = const _Empty(); | 9 final Matcher isEmpty = const _Empty(); |
| 10 | 10 |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 * | 111 * |
| 112 * expect(bar, new isInstanceOf<Foo>()); | 112 * expect(bar, new isInstanceOf<Foo>()); |
| 113 * | 113 * |
| 114 * To get better error message, supply a name when creating the | 114 * To get better error message, supply a name when creating the |
| 115 * Type wrapper; e.g.: | 115 * Type wrapper; e.g.: |
| 116 * | 116 * |
| 117 * expect(bar, new isInstanceOf<Foo>('Foo')); | 117 * expect(bar, new isInstanceOf<Foo>('Foo')); |
| 118 */ | 118 */ |
| 119 class isInstanceOf<T> extends BaseMatcher { | 119 class isInstanceOf<T> extends BaseMatcher { |
| 120 final String _name; | 120 final String _name; |
| 121 const isInstanceOf([this._name = 'specified type']); | 121 const isInstanceOf([name = 'specified type']) : this._name = name; |
| 122 bool matches(obj) => obj is T; | 122 bool matches(obj) => obj is T; |
| 123 // The description here is lame :-( | 123 // The description here is lame :-( |
| 124 Description describe(Description description) => | 124 Description describe(Description description) => |
| 125 description.add('an instance of ${_name}'); | 125 description.add('an instance of ${_name}'); |
| 126 } | 126 } |
| 127 | 127 |
| 128 /** | 128 /** |
| 129 * A matcher that matches functions that throw exceptions when called. | 129 * A matcher that matches functions that throw exceptions when called. |
| 130 * The value passed to expect() should be a reference to the function. | 130 * The value passed to expect() should be a reference to the function. |
| 131 * Note that the function cannot take arguments; to handle this | 131 * Note that the function cannot take arguments; to handle this |
| (...skipping 19 matching lines...) Expand all Loading... |
| 151 * The function will be called once. Any exceptions will be silently swallowed. | 151 * The function will be called once. Any exceptions will be silently swallowed. |
| 152 * The value passed to expect() should be a reference to the function. | 152 * The value passed to expect() should be a reference to the function. |
| 153 * Note that the function cannot take arguments; to handle this | 153 * Note that the function cannot take arguments; to handle this |
| 154 * a wrapper will have to be created. | 154 * a wrapper will have to be created. |
| 155 */ | 155 */ |
| 156 final Matcher returnsNormally = const _ReturnsNormally(); | 156 final Matcher returnsNormally = const _ReturnsNormally(); |
| 157 | 157 |
| 158 class _Throws extends BaseMatcher { | 158 class _Throws extends BaseMatcher { |
| 159 final Matcher _matcher; | 159 final Matcher _matcher; |
| 160 | 160 |
| 161 const _Throws([Matcher this._matcher = null]); | 161 const _Throws([Matcher matcher = null]) : this._matcher = matcher; |
| 162 | 162 |
| 163 bool matches(item) { | 163 bool matches(item) { |
| 164 try { | 164 try { |
| 165 item(); | 165 item(); |
| 166 return false; | 166 return false; |
| 167 } catch (final e) { | 167 } catch (final e) { |
| 168 return _matcher == null || _matcher.matches(e); | 168 return _matcher == null || _matcher.matches(e); |
| 169 } | 169 } |
| 170 } | 170 } |
| 171 | 171 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 | 248 |
| 249 /** | 249 /** |
| 250 * Returns a matcher that matches if an object has a length property | 250 * Returns a matcher that matches if an object has a length property |
| 251 * that matches [matcher]. | 251 * that matches [matcher]. |
| 252 */ | 252 */ |
| 253 Matcher hasLength(matcher) => | 253 Matcher hasLength(matcher) => |
| 254 new _HasLength(wrapMatcher(matcher)); | 254 new _HasLength(wrapMatcher(matcher)); |
| 255 | 255 |
| 256 class _HasLength extends BaseMatcher { | 256 class _HasLength extends BaseMatcher { |
| 257 final Matcher _matcher; | 257 final Matcher _matcher; |
| 258 const _HasLength([Matcher this._matcher = null]); | 258 const _HasLength([Matcher matcher = null]) : this._matcher = matcher; |
| 259 | 259 |
| 260 bool matches(item) { | 260 bool matches(item) { |
| 261 return _matcher.matches(item.length); | 261 return _matcher.matches(item.length); |
| 262 } | 262 } |
| 263 | 263 |
| 264 Description describe(Description description) => | 264 Description describe(Description description) => |
| 265 description.add('an object with length of '). | 265 description.add('an object with length of '). |
| 266 addDescriptionOf(_matcher); | 266 addDescriptionOf(_matcher); |
| 267 | 267 |
| 268 Description describeMismatch(item, Description mismatchDescription) { | 268 Description describeMismatch(item, Description mismatchDescription) { |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 } | 318 } |
| 319 } | 319 } |
| 320 return reason; | 320 return reason; |
| 321 } | 321 } |
| 322 | 322 |
| 323 class _DeepMatcher extends BaseMatcher { | 323 class _DeepMatcher extends BaseMatcher { |
| 324 final _expected; | 324 final _expected; |
| 325 final int _limit; | 325 final int _limit; |
| 326 var count; | 326 var count; |
| 327 | 327 |
| 328 _DeepMatcher(this._expected, [this._limit = 1000]); | 328 _DeepMatcher(this._expected, [limit = 1000]) : this._limit = limit; |
| 329 | 329 |
| 330 String _recursiveMatch(expected, actual, String location) { | 330 String _recursiveMatch(expected, actual, String location) { |
| 331 String reason = null; | 331 String reason = null; |
| 332 if (++count >= _limit) { | 332 if (++count >= _limit) { |
| 333 reason = 'item comparison limit exceeded'; | 333 reason = 'item comparison limit exceeded'; |
| 334 } else if (expected is Iterable) { | 334 } else if (expected is Iterable) { |
| 335 reason = _compareIterables(expected, actual, _recursiveMatch); | 335 reason = _compareIterables(expected, actual, _recursiveMatch); |
| 336 } else if (expected is Map) { | 336 } else if (expected is Map) { |
| 337 if (actual is !Map) { | 337 if (actual is !Map) { |
| 338 reason = 'expected a map'; | 338 reason = 'expected a map'; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 401 } | 401 } |
| 402 } else if (item is Map) { | 402 } else if (item is Map) { |
| 403 return item.containsKey(_expected); | 403 return item.containsKey(_expected); |
| 404 } | 404 } |
| 405 return false; | 405 return false; |
| 406 } | 406 } |
| 407 | 407 |
| 408 Description describe(Description description) => | 408 Description describe(Description description) => |
| 409 description.add('contains ').addDescriptionOf(_expected); | 409 description.add('contains ').addDescriptionOf(_expected); |
| 410 } | 410 } |
| OLD | NEW |