| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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 test.frontend.future_matchers; | 5 library test.frontend.future_matchers; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:matcher/matcher.dart' hide throws, throwsA, expect, fail; | 9 import 'package:matcher/matcher.dart' hide throws, throwsA, expect, fail; |
| 10 | 10 |
| 11 import '../backend/invoker.dart'; | 11 import '../backend/invoker.dart'; |
| 12 import '../utils.dart'; | 12 import '../utils.dart'; |
| 13 import 'expect.dart'; | 13 import 'expect.dart'; |
| 14 | 14 |
| 15 /// Matches a [Future] that completes successfully with a value. | 15 /// Matches a [Future] that completes successfully with a value. |
| 16 /// | 16 /// |
| 17 /// Note that this creates an asynchronous expectation. The call to `expect()` | 17 /// Note that this creates an asynchronous expectation. The call to `expect()` |
| 18 /// that includes this will return immediately and execution will continue. | 18 /// that includes this will return immediately and execution will continue. |
| 19 /// Later, when the future completes, the actual expectation will run. | 19 /// Later, when the future completes, the actual expectation will run. |
| 20 /// | 20 /// |
| 21 /// To test that a Future completes with an exception, you can use [throws] and | 21 /// To test that a Future completes with an exception, you can use [throws] and |
| 22 /// [throwsA]. | 22 /// [throwsA]. |
| 23 final Matcher completes = const _Completes(null, ''); | 23 final Matcher completes = const _Completes(null); |
| 24 | 24 |
| 25 /// Matches a [Future] that completes succesfully with a value that matches | 25 /// Matches a [Future] that completes succesfully with a value that matches |
| 26 /// [matcher]. | 26 /// [matcher]. |
| 27 /// | 27 /// |
| 28 /// Note that this creates an asynchronous expectation. The call to | 28 /// Note that this creates an asynchronous expectation. The call to |
| 29 /// `expect()` that includes this will return immediately and execution will | 29 /// `expect()` that includes this will return immediately and execution will |
| 30 /// continue. Later, when the future completes, the actual expectation will run. | 30 /// continue. Later, when the future completes, the actual expectation will run. |
| 31 /// | 31 /// |
| 32 /// To test that a Future completes with an exception, you can use [throws] and | 32 /// To test that a Future completes with an exception, you can use [throws] and |
| 33 /// [throwsA]. | 33 /// [throwsA]. |
| 34 /// | 34 /// |
| 35 /// [description] is an optional tag that can be used to identify the completion | 35 /// The [description] parameter is deprecated and shouldn't be used. |
| 36 /// matcher in error messages. | 36 Matcher completion(matcher, [@deprecated String description]) => |
| 37 Matcher completion(matcher, [String description = '']) => | 37 new _Completes(wrapMatcher(matcher)); |
| 38 new _Completes(wrapMatcher(matcher), description); | |
| 39 | 38 |
| 40 class _Completes extends Matcher { | 39 class _Completes extends Matcher { |
| 41 final Matcher _matcher; | 40 final Matcher _matcher; |
| 42 final String _id; | |
| 43 | 41 |
| 44 const _Completes(this._matcher, this._id); | 42 const _Completes(this._matcher); |
| 45 | 43 |
| 46 bool matches(item, Map matchState) { | 44 bool matches(item, Map matchState) { |
| 47 if (item is! Future) return false; | 45 if (item is! Future) return false; |
| 48 Invoker.current.addOutstandingCallback(); | 46 Invoker.current.addOutstandingCallback(); |
| 49 | 47 |
| 50 item.then((value) { | 48 item.then((value) { |
| 51 if (_matcher != null) expect(value, _matcher); | 49 if (_matcher != null) expect(value, _matcher); |
| 52 Invoker.current.removeOutstandingCallback(); | 50 Invoker.current.removeOutstandingCallback(); |
| 53 }, onError: (error, trace) { | |
| 54 if (error is TestFailure) throw error; | |
| 55 | |
| 56 var id = _id == '' ? '' : '${_id} '; | |
| 57 var reason = 'Expected future ${id}to complete successfully, ' | |
| 58 'but it failed with ${error}'; | |
| 59 if (trace != null) { | |
| 60 var stackTrace = terseChain(trace, | |
| 61 verbose: Invoker.current.metadata.verboseTrace); | |
| 62 stackTrace = ' ${stackTrace.toString().replaceAll('\n', '\n ')}'; | |
| 63 reason = '$reason\nStack trace:\n$stackTrace'; | |
| 64 } | |
| 65 fail(reason); | |
| 66 }); | 51 }); |
| 67 | 52 |
| 68 return true; | 53 return true; |
| 69 } | 54 } |
| 70 | 55 |
| 71 Description describe(Description description) { | 56 Description describe(Description description) { |
| 72 if (_matcher == null) { | 57 if (_matcher == null) { |
| 73 description.add('completes successfully'); | 58 description.add('completes successfully'); |
| 74 } else { | 59 } else { |
| 75 description.add('completes to a value that ').addDescriptionOf(_matcher); | 60 description.add('completes to a value that ').addDescriptionOf(_matcher); |
| 76 } | 61 } |
| 77 return description; | 62 return description; |
| 78 } | 63 } |
| 79 } | 64 } |
| OLD | NEW |