| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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 scheduled_future_matchers; | 5 library scheduled_future_matchers; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import '../scheduled_test.dart'; | 9 import '../scheduled_test.dart'; |
| 10 | 10 |
| 11 /// Matches a [Future] that completes successfully with a value. Note that this | 11 /// Matches a [Future] that completes successfully with a value. Note that this |
| 12 /// creates an asynchronous expectation. The call to `expect()` that includes | 12 /// creates an asynchronous expectation. The call to `expect()` that includes |
| 13 /// this will return immediately and execution will continue. Later, when the | 13 /// this will return immediately and execution will continue. Later, when the |
| 14 /// future completes, the actual expectation will run. | 14 /// future completes, the actual expectation will run. |
| 15 /// | 15 /// |
| 16 /// To test that a Future completes with an exception, you can use [throws] and | 16 /// To test that a Future completes with an exception, you can use [throws] and |
| 17 /// [throwsA]. | 17 /// [throwsA]. |
| 18 /// | 18 /// |
| 19 /// This differs from the `completes` matcher in `unittest` in that it pipes any | 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 | 20 /// errors in the Future to [currentSchedule], rather than reporting them in the |
| 21 /// [expect]'s error message. | 21 /// [expect]'s error message. |
| 22 Matcher completes = const _ScheduledCompletes(null); | 22 Matcher completes = const _ScheduledCompletes(null, null); |
| 23 | 23 |
| 24 /// Matches a [Future] that completes succesfully with a value that matches | 24 /// Matches a [Future] that completes succesfully with a value that matches |
| 25 /// [matcher]. Note that this creates an asynchronous expectation. The call to | 25 /// [matcher]. Note that this creates an asynchronous expectation. The call to |
| 26 /// `expect()` that includes this will return immediately and execution will | 26 /// `expect()` that includes this will return immediately and execution will |
| 27 /// continue. Later, when the future completes, the actual expectation will run. | 27 /// continue. Later, when the future completes, the actual expectation will run. |
| 28 /// | 28 /// |
| 29 /// To test that a Future completes with an exception, you can use [throws] and | 29 /// To test that a Future completes with an exception, you can use [throws] and |
| 30 /// [throwsA]. | 30 /// [throwsA]. |
| 31 /// | 31 /// |
| 32 /// [description] is an optional tag that can be used to identify the completion |
| 33 /// matcher in error messages. |
| 34 /// |
| 32 /// This differs from the `completion` matcher in `unittest` in that it pipes | 35 /// 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 | 36 /// any errors in the Future to [currentSchedule], rather than reporting them in |
| 34 /// the [expect]'s error message. | 37 /// the [expect]'s error message. |
| 35 Matcher completion(matcher) => new _ScheduledCompletes(wrapMatcher(matcher)); | 38 Matcher completion(matcher, [String description]) => |
| 39 new _ScheduledCompletes(wrapMatcher(matcher), description); |
| 36 | 40 |
| 37 class _ScheduledCompletes extends BaseMatcher { | 41 class _ScheduledCompletes extends BaseMatcher { |
| 38 final Matcher _matcher; | 42 final Matcher _matcher; |
| 43 final String _description; |
| 39 | 44 |
| 40 const _ScheduledCompletes(this._matcher); | 45 const _ScheduledCompletes(this._matcher, this._description); |
| 41 | 46 |
| 42 bool matches(item, MatchState matchState) { | 47 bool matches(item, MatchState matchState) { |
| 43 if (item is! Future) return false; | 48 if (item is! Future) return false; |
| 44 | 49 |
| 45 wrapFuture(item.then((value) { | 50 // TODO(nweiz): parse the stack, figure out on what line these were called, |
| 51 // and include that in their descriptions |
| 52 var description = _description; |
| 53 if (description == null) { |
| 54 if (_matcher == null) { |
| 55 description = 'expect(..., completes)'; |
| 56 } else { |
| 57 var matcherDescription = new StringDescription(); |
| 58 _matcher.describe(matcherDescription); |
| 59 description = 'expect(..., completion($matcherDescription))'; |
| 60 } |
| 61 } |
| 62 |
| 63 currentSchedule.wrapFuture(item.then((value) { |
| 46 if (_matcher != null) expect(value, _matcher); | 64 if (_matcher != null) expect(value, _matcher); |
| 47 })); | 65 }), description); |
| 48 | 66 |
| 49 return true; | 67 return true; |
| 50 } | 68 } |
| 51 | 69 |
| 52 Description describe(Description description) { | 70 Description describe(Description description) { |
| 53 if (_matcher == null) { | 71 if (_matcher == null) { |
| 54 description.add('completes successfully'); | 72 description.add('completes successfully'); |
| 55 } else { | 73 } else { |
| 56 description.add('completes to a value that ').addDescriptionOf(_matcher); | 74 description.add('completes to a value that ').addDescriptionOf(_matcher); |
| 57 } | 75 } |
| 58 return description; | 76 return description; |
| 59 } | 77 } |
| 60 } | 78 } |
| OLD | NEW |