| 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 matcher.future_matchers; | 5 library matcher.future_matchers; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'expect.dart'; | 9 import 'expect.dart'; |
| 10 import 'interfaces.dart'; | 10 import 'interfaces.dart'; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 final Matcher _matcher; | 39 final Matcher _matcher; |
| 40 final String _id; | 40 final String _id; |
| 41 | 41 |
| 42 const _Completes(this._matcher, this._id); | 42 const _Completes(this._matcher, this._id); |
| 43 | 43 |
| 44 bool matches(item, Map matchState) { | 44 bool matches(item, Map matchState) { |
| 45 if (item is! Future) return false; | 45 if (item is! Future) return false; |
| 46 var done = wrapAsync((fn) => fn(), _id); | 46 var done = wrapAsync((fn) => fn(), _id); |
| 47 | 47 |
| 48 item.then((value) { | 48 item.then((value) { |
| 49 done(() { if (_matcher != null) expect(value, _matcher); }); | 49 done(() { |
| 50 if (_matcher != null) expect(value, _matcher); |
| 51 }); |
| 50 }, onError: (error, trace) { | 52 }, onError: (error, trace) { |
| 51 var id = _id == '' ? '' : '${_id} '; | 53 var id = _id == '' ? '' : '${_id} '; |
| 52 var reason = 'Expected future ${id}to complete successfully, ' | 54 var reason = 'Expected future ${id}to complete successfully, ' |
| 53 'but it failed with ${error}'; | 55 'but it failed with ${error}'; |
| 54 if (trace != null) { | 56 if (trace != null) { |
| 55 var stackTrace = trace.toString(); | 57 var stackTrace = trace.toString(); |
| 56 stackTrace = ' ${stackTrace.replaceAll('\n', '\n ')}'; | 58 stackTrace = ' ${stackTrace.replaceAll('\n', '\n ')}'; |
| 57 reason = '$reason\nStack trace:\n$stackTrace'; | 59 reason = '$reason\nStack trace:\n$stackTrace'; |
| 58 } | 60 } |
| 59 done(() => fail(reason)); | 61 done(() => fail(reason)); |
| 60 }); | 62 }); |
| 61 | 63 |
| 62 return true; | 64 return true; |
| 63 } | 65 } |
| 64 | 66 |
| 65 Description describe(Description description) { | 67 Description describe(Description description) { |
| 66 if (_matcher == null) { | 68 if (_matcher == null) { |
| 67 description.add('completes successfully'); | 69 description.add('completes successfully'); |
| 68 } else { | 70 } else { |
| 69 description.add('completes to a value that ').addDescriptionOf(_matcher); | 71 description.add('completes to a value that ').addDescriptionOf(_matcher); |
| 70 } | 72 } |
| 71 return description; | 73 return description; |
| 72 } | 74 } |
| 73 } | 75 } |
| OLD | NEW |