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 unittest.future_matchers; | 5 library unittest.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 '../unittest.dart'; | 11 import 'expect.dart'; |
| 12 import 'invoker.dart'; |
| 13 import 'utils.dart'; |
12 | 14 |
13 /// Matches a [Future] that completes successfully with a value. | 15 /// Matches a [Future] that completes successfully with a value. |
14 /// | 16 /// |
15 /// Note that this creates an asynchronous expectation. The call to `expect()` | 17 /// Note that this creates an asynchronous expectation. The call to `expect()` |
16 /// that includes this will return immediately and execution will continue. | 18 /// that includes this will return immediately and execution will continue. |
17 /// Later, when the future completes, the actual expectation will run. | 19 /// Later, when the future completes, the actual expectation will run. |
18 /// | 20 /// |
19 /// 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 |
20 /// [throwsA]. | 22 /// [throwsA]. |
21 final Matcher completes = const _Completes(null, ''); | 23 final Matcher completes = const _Completes(null, ''); |
(...skipping 14 matching lines...) Expand all Loading... |
36 new _Completes(wrapMatcher(matcher), description); | 38 new _Completes(wrapMatcher(matcher), description); |
37 | 39 |
38 class _Completes extends Matcher { | 40 class _Completes extends Matcher { |
39 final Matcher _matcher; | 41 final Matcher _matcher; |
40 final String _id; | 42 final String _id; |
41 | 43 |
42 const _Completes(this._matcher, this._id); | 44 const _Completes(this._matcher, this._id); |
43 | 45 |
44 bool matches(item, Map matchState) { | 46 bool matches(item, Map matchState) { |
45 if (item is! Future) return false; | 47 if (item is! Future) return false; |
46 var done = expectAsync((fn) => fn(), id: _id); | 48 Invoker.current.addOutstandingCallback(); |
47 | 49 |
48 item.then((value) { | 50 item.then((value) { |
49 done(() { | 51 if (_matcher != null) expect(value, _matcher); |
50 if (_matcher != null) expect(value, _matcher); | 52 Invoker.current.removeOutstandingCallback(); |
51 }); | |
52 }, onError: (error, trace) { | 53 }, onError: (error, trace) { |
| 54 if (error is TestFailure) { |
| 55 Invoker.current.handleError(error, trace); |
| 56 return; |
| 57 } |
| 58 |
53 var id = _id == '' ? '' : '${_id} '; | 59 var id = _id == '' ? '' : '${_id} '; |
54 var reason = 'Expected future ${id}to complete successfully, ' | 60 var reason = 'Expected future ${id}to complete successfully, ' |
55 'but it failed with ${error}'; | 61 'but it failed with ${error}'; |
56 if (trace != null) { | 62 if (trace != null) { |
57 var stackTrace = trace.toString(); | 63 var stackTrace = terseChain(trace).toString(); |
58 stackTrace = ' ${stackTrace.replaceAll('\n', '\n ')}'; | 64 stackTrace = ' ${stackTrace.replaceAll('\n', '\n ')}'; |
59 reason = '$reason\nStack trace:\n$stackTrace'; | 65 reason = '$reason\nStack trace:\n$stackTrace'; |
60 } | 66 } |
61 done(() => fail(reason)); | 67 fail(reason); |
62 }); | 68 }); |
63 | 69 |
64 return true; | 70 return true; |
65 } | 71 } |
66 | 72 |
67 Description describe(Description description) { | 73 Description describe(Description description) { |
68 if (_matcher == null) { | 74 if (_matcher == null) { |
69 description.add('completes successfully'); | 75 description.add('completes successfully'); |
70 } else { | 76 } else { |
71 description.add('completes to a value that ').addDescriptionOf(_matcher); | 77 description.add('completes to a value that ').addDescriptionOf(_matcher); |
72 } | 78 } |
73 return description; | 79 return description; |
74 } | 80 } |
75 } | 81 } |
OLD | NEW |