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 /** | 5 /** |
6 * Matches a [Future] that completes successfully with a value. Note that this | 6 * Matches a [Future] that completes successfully with a value. Note that this |
7 * creates an asynchronous expectation. The call to `expect()` that includes | 7 * creates an asynchronous expectation. The call to `expect()` that includes |
8 * this will return immediately and execution will continue. Later, when the | 8 * this will return immediately and execution will continue. Later, when the |
9 * future completes, the actual expectation will run. | 9 * future completes, the actual expectation will run. |
10 * | 10 * |
11 * To test that a Future completes with an exception, you can use [throws] and | 11 * To test that a Future completes with an exception, you can use [throws] and |
12 * [throwsA]. | 12 * [throwsA]. |
13 */ | 13 */ |
14 | 14 |
15 part of unittest; | 15 part of matcher; |
16 | 16 |
17 Matcher completes = const _Completes(null); | 17 Matcher completes = const _Completes(null); |
18 | 18 |
19 /** | 19 /** |
20 * Matches a [Future] that completes succesfully with a value that matches | 20 * Matches a [Future] that completes succesfully with a value that matches |
21 * [matcher]. Note that this creates an asynchronous expectation. The call to | 21 * [matcher]. Note that this creates an asynchronous expectation. The call to |
22 * `expect()` that includes this will return immediately and execution will | 22 * `expect()` that includes this will return immediately and execution will |
23 * continue. Later, when the future completes, the actual expectation will run. | 23 * continue. Later, when the future completes, the actual expectation will run. |
24 * | 24 * |
25 * To test that a Future completes with an exception, you can use [throws] and | 25 * To test that a Future completes with an exception, you can use [throws] and |
26 * [throwsA]. | 26 * [throwsA]. |
27 */ | 27 */ |
28 Matcher completion(matcher) => new _Completes(wrapMatcher(matcher)); | 28 Matcher completion(matcher) => new _Completes(wrapMatcher(matcher)); |
29 | 29 |
30 class _Completes extends BaseMatcher { | 30 class _Completes extends BaseMatcher { |
31 final Matcher _matcher; | 31 final Matcher _matcher; |
32 | 32 |
33 const _Completes(this._matcher); | 33 const _Completes(this._matcher); |
34 | 34 |
35 bool matches(item, MatchState matchState) { | 35 bool matches(item, MatchState matchState) { |
36 if (item is! Future) return false; | 36 if (item is! Future) return false; |
37 | 37 |
38 item.onComplete(expectAsync1((future) { | 38 item.onComplete(_wrapAsync((future) { |
39 var reason = 'Expected future to complete successfully, but it failed ' | 39 var reason = 'Expected future to complete successfully, but it failed ' |
40 'with ${future.exception}'; | 40 'with ${future.exception}'; |
41 if (future.stackTrace != null) { | 41 if (future.stackTrace != null) { |
42 var stackTrace = future.stackTrace.toString(); | 42 var stackTrace = future.stackTrace.toString(); |
43 stackTrace = ' ${stackTrace.replaceAll('\n', '\n ')}'; | 43 stackTrace = ' ${stackTrace.replaceAll('\n', '\n ')}'; |
44 reason = '$reason\nStack trace:\n$stackTrace'; | 44 reason = '$reason\nStack trace:\n$stackTrace'; |
45 } | 45 } |
46 | 46 |
47 expect(future.hasValue, isTrue, reason: reason); | 47 expect(future.hasValue, isTrue, reason: reason); |
48 if (_matcher != null) expect(future.value, _matcher); | 48 if (_matcher != null) expect(future.value, _matcher); |
49 })); | 49 })); |
50 | 50 |
51 return true; | 51 return true; |
52 } | 52 } |
53 | 53 |
54 Description describe(Description description) { | 54 Description describe(Description description) { |
55 if (_matcher == null) { | 55 if (_matcher == null) { |
56 description.add('completes successfully'); | 56 description.add('completes successfully'); |
57 } else { | 57 } else { |
58 description.add('completes to a value that ').addDescriptionOf(_matcher); | 58 description.add('completes to a value that ').addDescriptionOf(_matcher); |
59 } | 59 } |
60 return description; | 60 return description; |
61 } | 61 } |
62 } | 62 } |
OLD | NEW |