OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, 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 library scheduled_future_matchers; |
| 6 |
| 7 import 'dart:async'; |
| 8 |
| 9 import '../scheduled_test.dart'; |
| 10 |
| 11 /// Matches a [Future] that completes successfully with a value. Note that this |
| 12 /// creates an asynchronous expectation. The call to `expect()` that includes |
| 13 /// this will return immediately and execution will continue. Later, when the |
| 14 /// future completes, the actual expectation will run. |
| 15 /// |
| 16 /// To test that a Future completes with an exception, you can use [throws] and |
| 17 /// [throwsA]. |
| 18 /// |
| 19 /// This differs from the `completes` matcher in `unittest` in that it pipes any |
| 20 /// errors in the Future to [currentSchedule], rather than reporting them in the |
| 21 /// [expect]'s error message. |
| 22 Matcher completes = const _ScheduledCompletes(null); |
| 23 |
| 24 /// Matches a [Future] that completes succesfully with a value that matches |
| 25 /// [matcher]. Note that this creates an asynchronous expectation. The call to |
| 26 /// `expect()` that includes this will return immediately and execution will |
| 27 /// continue. Later, when the future completes, the actual expectation will run. |
| 28 /// |
| 29 /// To test that a Future completes with an exception, you can use [throws] and |
| 30 /// [throwsA]. |
| 31 /// |
| 32 /// This differs from the `completion` matcher in `unittest` in that it pipes |
| 33 /// any errors in the Future to [currentSchedule], rather than reporting them in |
| 34 /// the [expect]'s error message. |
| 35 Matcher completion(matcher) => new _ScheduledCompletes(wrapMatcher(matcher)); |
| 36 |
| 37 class _ScheduledCompletes extends BaseMatcher { |
| 38 final Matcher _matcher; |
| 39 |
| 40 const _ScheduledCompletes(this._matcher); |
| 41 |
| 42 bool matches(item, MatchState matchState) { |
| 43 if (item is! Future) return false; |
| 44 |
| 45 wrapFuture(item.then((value) { |
| 46 if (_matcher != null) expect(value, _matcher); |
| 47 })); |
| 48 |
| 49 return true; |
| 50 } |
| 51 |
| 52 Description describe(Description description) { |
| 53 if (_matcher == null) { |
| 54 description.add('completes successfully'); |
| 55 } else { |
| 56 description.add('completes to a value that ').addDescriptionOf(_matcher); |
| 57 } |
| 58 return description; |
| 59 } |
| 60 } |
OLD | NEW |