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