| 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 * Matches a [Future] that completes successfully with a value. Note that this | 6 * Matches a [Future] that completes successfully with a value. Note that this |
| 7 * creates an asynchronous expectation. The call to `expect()` that includes | 7 * creates an asynchronous expectation. The call to `expect()` that includes |
| 8 * this will return immediately and execution will continue. Later, when the | 8 * this will return immediately and execution will continue. Later, when the |
| 9 * future completes, the actual expectation will run. | 9 * future completes, the actual expectation will run. |
| 10 * | 10 * |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 */ | 27 */ |
| 28 Matcher completion(matcher) => new _Completes(wrapMatcher(matcher)); | 28 Matcher completion(matcher) => new _Completes(wrapMatcher(matcher)); |
| 29 | 29 |
| 30 class _Completes extends BaseMatcher { | 30 class _Completes extends BaseMatcher { |
| 31 final Matcher _matcher; | 31 final Matcher _matcher; |
| 32 | 32 |
| 33 const _Completes(this._matcher); | 33 const _Completes(this._matcher); |
| 34 | 34 |
| 35 bool matches(item, MatchState matchState) { | 35 bool matches(item, MatchState matchState) { |
| 36 if (item is! Future) return false; | 36 if (item is! Future) return false; |
| 37 var done = wrapAsync((fn) => fn()); |
| 37 | 38 |
| 38 item.then(wrapAsync((value) { | 39 item.then((value) { |
| 39 if (_matcher != null) expect(value, _matcher); | 40 done(() { if (_matcher != null) expect(value, _matcher); }); |
| 40 })); | 41 }, onError: (e) { |
| 41 | |
| 42 item.catchError((e) { | |
| 43 var reason = 'Expected future to complete successfully, but it failed ' | 42 var reason = 'Expected future to complete successfully, but it failed ' |
| 44 'with ${e.error}'; | 43 'with ${e.error}'; |
| 45 if (future.stackTrace != null) { | 44 if (e.stackTrace != null) { |
| 46 var stackTrace = e.stackTrace.toString(); | 45 var stackTrace = e.stackTrace.toString(); |
| 47 stackTrace = ' ${stackTrace.replaceAll('\n', '\n ')}'; | 46 stackTrace = ' ${stackTrace.replaceAll('\n', '\n ')}'; |
| 48 reason = '$reason\nStack trace:\n$stackTrace'; | 47 reason = '$reason\nStack trace:\n$stackTrace'; |
| 49 } | 48 } |
| 50 expect(false, isTrue, reason: reason); | 49 done(() => expect(false, isTrue, reason: reason)); |
| 51 }); | 50 }); |
| 52 | 51 |
| 53 return true; | 52 return true; |
| 54 } | 53 } |
| 55 | 54 |
| 56 Description describe(Description description) { | 55 Description describe(Description description) { |
| 57 if (_matcher == null) { | 56 if (_matcher == null) { |
| 58 description.add('completes successfully'); | 57 description.add('completes successfully'); |
| 59 } else { | 58 } else { |
| 60 description.add('completes to a value that ').addDescriptionOf(_matcher); | 59 description.add('completes to a value that ').addDescriptionOf(_matcher); |
| 61 } | 60 } |
| 62 return description; | 61 return description; |
| 63 } | 62 } |
| 64 } | 63 } |
| OLD | NEW |