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 part of matcher; | 5 part of matcher; |
6 | 6 |
7 /** | 7 /** |
8 * Matches a [Future] that completes successfully with a value. Note that this | 8 * Matches a [Future] that completes successfully with a value. Note that this |
9 * creates an asynchronous expectation. The call to `expect()` that includes | 9 * creates an asynchronous expectation. The call to `expect()` that includes |
10 * this will return immediately and execution will continue. Later, when the | 10 * this will return immediately and execution will continue. Later, when the |
11 * future completes, the actual expectation will run. | 11 * future completes, the actual expectation will run. |
12 * | 12 * |
13 * To test that a Future completes with an exception, you can use [throws] and | 13 * To test that a Future completes with an exception, you can use [throws] and |
14 * [throwsA]. | 14 * [throwsA]. |
15 */ | 15 */ |
16 Matcher completes = const _Completes(null); | 16 Matcher completes = const _Completes(null, ''); |
17 | 17 |
18 /** | 18 /** |
19 * Matches a [Future] that completes succesfully with a value that matches | 19 * Matches a [Future] that completes succesfully with a value that matches |
20 * [matcher]. Note that this creates an asynchronous expectation. The call to | 20 * [matcher]. Note that this creates an asynchronous expectation. The call to |
21 * `expect()` that includes this will return immediately and execution will | 21 * `expect()` that includes this will return immediately and execution will |
22 * continue. Later, when the future completes, the actual expectation will run. | 22 * continue. Later, when the future completes, the actual expectation will run. |
23 * | 23 * |
24 * To test that a Future completes with an exception, you can use [throws] and | 24 * To test that a Future completes with an exception, you can use [throws] and |
25 * [throwsA]. | 25 * [throwsA]. |
26 * | |
27 * [id] is an optional tag that can be used to identify the completion matcher | |
28 * in error messages. | |
26 */ | 29 */ |
27 Matcher completion(matcher) => new _Completes(wrapMatcher(matcher)); | 30 Matcher completion(matcher, [String id = '']) => |
31 new _Completes(wrapMatcher(matcher), id); | |
28 | 32 |
29 class _Completes extends BaseMatcher { | 33 class _Completes extends BaseMatcher { |
30 final Matcher _matcher; | 34 final Matcher _matcher; |
35 final String _id; | |
31 | 36 |
32 const _Completes(this._matcher); | 37 const _Completes(this._matcher, String id) : |
Siggi Cherem (dart-lang)
2013/03/01 21:11:20
style nit: move the ':' to the next line:
cons
gram
2013/03/04 19:53:40
Done.
| |
38 this._id = (id == '') ? '' : '$id '; | |
33 | 39 |
34 bool matches(item, MatchState matchState) { | 40 bool matches(item, MatchState matchState) { |
35 if (item is! Future) return false; | 41 if (item is! Future) return false; |
36 var done = wrapAsync((fn) => fn()); | 42 var done = wrapAsync((fn) => fn(), _id); |
37 | 43 |
38 item.then((value) { | 44 item.then((value) { |
39 done(() { if (_matcher != null) expect(value, _matcher); }); | 45 done(() { if (_matcher != null) expect(value, _matcher); }); |
40 }, onError: (e) { | 46 }, onError: (e) { |
41 var reason = 'Expected future to complete successfully, but it failed ' | 47 var reason = 'Expected future ${_id}to complete successfully, ' |
42 'with ${e.error}'; | 48 'but it failed with ${e.error}'; |
43 if (e.stackTrace != null) { | 49 if (e.stackTrace != null) { |
44 var stackTrace = e.stackTrace.toString(); | 50 var stackTrace = e.stackTrace.toString(); |
45 stackTrace = ' ${stackTrace.replaceAll('\n', '\n ')}'; | 51 stackTrace = ' ${stackTrace.replaceAll('\n', '\n ')}'; |
46 reason = '$reason\nStack trace:\n$stackTrace'; | 52 reason = '$reason\nStack trace:\n$stackTrace'; |
47 } | 53 } |
48 done(() => fail(reason)); | 54 done(() => fail(reason)); |
49 }); | 55 }); |
50 | 56 |
51 return true; | 57 return true; |
52 } | 58 } |
53 | 59 |
54 Description describe(Description description) { | 60 Description describe(Description description) { |
55 if (_matcher == null) { | 61 if (_matcher == null) { |
56 description.add('completes successfully'); | 62 description.add('completes successfully'); |
57 } else { | 63 } else { |
58 description.add('completes to a value that ').addDescriptionOf(_matcher); | 64 description.add('completes to a value that ').addDescriptionOf(_matcher); |
59 } | 65 } |
60 return description; | 66 return description; |
61 } | 67 } |
62 } | 68 } |
OLD | NEW |