| 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 final Matcher completes = const _ScheduledCompletes(null, 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 /// [description] is an optional tag that can be used to identify the completion | |
| 33 /// matcher in error messages. | |
| 34 /// | |
| 35 /// This differs from the `completion` matcher in `unittest` in that it pipes | |
| 36 /// any errors in the Future to [currentSchedule], rather than reporting them in | |
| 37 /// the [expect]'s error message. | |
| 38 Matcher completion(matcher, [String description]) => | |
| 39 new _ScheduledCompletes(wrapMatcher(matcher), description); | |
| 40 | |
| 41 class _ScheduledCompletes extends Matcher { | |
| 42 final Matcher _matcher; | |
| 43 final String _description; | |
| 44 | |
| 45 const _ScheduledCompletes(this._matcher, this._description); | |
| 46 | |
| 47 bool matches(item, Map matchState) { | |
| 48 if (item is! Future) return false; | |
| 49 | |
| 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) { | |
| 64 if (_matcher == null) return; | |
| 65 | |
| 66 // If expect throws we might want to be able to switch to the outer trace | |
| 67 // instead. | |
| 68 expect(value, _matcher); | |
| 69 }), description); | |
| 70 | |
| 71 return true; | |
| 72 } | |
| 73 | |
| 74 Description describe(Description description) { | |
| 75 if (_matcher == null) { | |
| 76 description.add('completes successfully'); | |
| 77 } else { | |
| 78 description.add('completes to a value that ').addDescriptionOf(_matcher); | |
| 79 } | |
| 80 return description; | |
| 81 } | |
| 82 } | |
| OLD | NEW |