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