| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 part of unittest.matcher; | |
| 6 | |
| 7 /** | |
| 8 * Matches a [Future] that completes successfully with a value. Note that this | |
| 9 * creates an asynchronous expectation. The call to `expect()` that includes | |
| 10 * this will return immediately and execution will continue. Later, when the | |
| 11 * future completes, the actual expectation will run. | |
| 12 * | |
| 13 * To test that a Future completes with an exception, you can use [throws] and | |
| 14 * [throwsA]. | |
| 15 */ | |
| 16 final Matcher completes = const _Completes(null, ''); | |
| 17 | |
| 18 /** | |
| 19 * Matches a [Future] that completes succesfully with a value that matches | |
| 20 * [matcher]. Note that this creates an asynchronous expectation. The call to | |
| 21 * `expect()` that includes this will return immediately and execution will | |
| 22 * continue. Later, when the future completes, the actual expectation will run. | |
| 23 * | |
| 24 * To test that a Future completes with an exception, you can use [throws] and | |
| 25 * [throwsA]. | |
| 26 * | |
| 27 * [id] is an optional tag that can be used to identify the completion matcher | |
| 28 * in error messages. | |
| 29 */ | |
| 30 Matcher completion(matcher, [String id = '']) => | |
| 31 new _Completes(wrapMatcher(matcher), id); | |
| 32 | |
| 33 class _Completes extends Matcher { | |
| 34 final Matcher _matcher; | |
| 35 final String _id; | |
| 36 | |
| 37 const _Completes(this._matcher, this._id); | |
| 38 | |
| 39 bool matches(item, Map matchState) { | |
| 40 if (item is! Future) return false; | |
| 41 var done = wrapAsync((fn) => fn(), _id); | |
| 42 | |
| 43 item.then((value) { | |
| 44 done(() { if (_matcher != null) expect(value, _matcher); }); | |
| 45 }, onError: (error, trace) { | |
| 46 var id = _id == '' ? '' : '${_id} '; | |
| 47 var reason = 'Expected future ${id}to complete successfully, ' | |
| 48 'but it failed with ${error}'; | |
| 49 if (trace != null) { | |
| 50 var stackTrace = trace.toString(); | |
| 51 stackTrace = ' ${stackTrace.replaceAll('\n', '\n ')}'; | |
| 52 reason = '$reason\nStack trace:\n$stackTrace'; | |
| 53 } | |
| 54 done(() => fail(reason)); | |
| 55 }); | |
| 56 | |
| 57 return true; | |
| 58 } | |
| 59 | |
| 60 Description describe(Description description) { | |
| 61 if (_matcher == null) { | |
| 62 description.add('completes successfully'); | |
| 63 } else { | |
| 64 description.add('completes to a value that ').addDescriptionOf(_matcher); | |
| 65 } | |
| 66 return description; | |
| 67 } | |
| 68 } | |
| OLD | NEW |