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 library test.frontend.future_matchers; |
| 6 |
| 7 import 'dart:async'; |
| 8 |
| 9 import 'package:matcher/matcher.dart' hide throws, throwsA, expect, fail; |
| 10 |
| 11 import '../backend/invoker.dart'; |
| 12 import 'expect.dart'; |
| 13 |
| 14 /// Matches a [Future] that completes successfully with a value. |
| 15 /// |
| 16 /// Note that this creates an asynchronous expectation. The call to `expect()` |
| 17 /// that includes this will return immediately and execution will continue. |
| 18 /// Later, when the future completes, the actual expectation will run. |
| 19 /// |
| 20 /// To test that a Future completes with an exception, you can use [throws] and |
| 21 /// [throwsA]. |
| 22 final Matcher completes = const _Completes(null); |
| 23 |
| 24 /// Matches a [Future] that completes succesfully with a value that matches |
| 25 /// [matcher]. |
| 26 /// |
| 27 /// Note that this creates an asynchronous expectation. The call to |
| 28 /// `expect()` that includes this will return immediately and execution will |
| 29 /// continue. Later, when the future completes, the actual expectation will run. |
| 30 /// |
| 31 /// To test that a Future completes with an exception, you can use [throws] and |
| 32 /// [throwsA]. |
| 33 /// |
| 34 /// The [description] parameter is deprecated and shouldn't be used. |
| 35 Matcher completion(matcher, [@deprecated String description]) => |
| 36 new _Completes(wrapMatcher(matcher)); |
| 37 |
| 38 class _Completes extends Matcher { |
| 39 final Matcher _matcher; |
| 40 |
| 41 const _Completes(this._matcher); |
| 42 |
| 43 bool matches(item, Map matchState) { |
| 44 if (item is! Future) return false; |
| 45 Invoker.current.addOutstandingCallback(); |
| 46 |
| 47 item.then((value) { |
| 48 if (_matcher != null) expect(value, _matcher); |
| 49 Invoker.current.removeOutstandingCallback(); |
| 50 }); |
| 51 |
| 52 return true; |
| 53 } |
| 54 |
| 55 Description describe(Description description) { |
| 56 if (_matcher == null) { |
| 57 description.add('completes successfully'); |
| 58 } else { |
| 59 description.add('completes to a value that ').addDescriptionOf(_matcher); |
| 60 } |
| 61 return description; |
| 62 } |
| 63 } |
OLD | NEW |