OLD | NEW |
---|---|
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 /// This library is used for testing asynchronous tests. | 5 /// This library is used for testing asynchronous tests. |
6 /// If a test is asynchronous, it needs to notify the testing driver | 6 /// If a test is asynchronous, it needs to notify the testing driver |
7 /// about this (otherwise tests may get reported as passing [after main() | 7 /// about this (otherwise tests may get reported as passing [after main() |
8 /// finished] even if the asynchronous operations fail). | 8 /// finished] even if the asynchronous operations fail). |
9 /// Tests which can't use the unittest framework should use the helper functions | 9 /// Tests which can't use the unittest framework should use the helper functions |
10 /// in this library. | 10 /// in this library. |
(...skipping 14 matching lines...) Expand all Loading... | |
25 import 'dart:isolate'; | 25 import 'dart:isolate'; |
26 | 26 |
27 bool _initialized = false; | 27 bool _initialized = false; |
28 ReceivePort _port = null; | 28 ReceivePort _port = null; |
29 int _asyncLevel = 0; | 29 int _asyncLevel = 0; |
30 | 30 |
31 Exception _buildException(String msg) { | 31 Exception _buildException(String msg) { |
32 return new Exception('Fatal: $msg. This is most likely a bug in your test.'); | 32 return new Exception('Fatal: $msg. This is most likely a bug in your test.'); |
33 } | 33 } |
34 | 34 |
35 /// Call this method before an asynchronous test is created. | |
35 void asyncStart() { | 36 void asyncStart() { |
36 if (_initialized && _asyncLevel == 0) { | 37 if (_initialized && _asyncLevel == 0) { |
37 throw _buildException('asyncStart() was called even though we are done ' | 38 throw _buildException('asyncStart() was called even though we are done ' |
38 'with testing.'); | 39 'with testing.'); |
39 } | 40 } |
40 if (!_initialized) { | 41 if (!_initialized) { |
41 print('unittest-suite-wait-for-done'); | 42 print('unittest-suite-wait-for-done'); |
42 _initialized = true; | 43 _initialized = true; |
43 _port = new ReceivePort(); | 44 _port = new ReceivePort(); |
44 } | 45 } |
45 _asyncLevel++; | 46 _asyncLevel++; |
46 } | 47 } |
47 | 48 |
49 /// Call this after an asynchronous test has ended successfully. | |
48 void asyncEnd() { | 50 void asyncEnd() { |
49 if (_asyncLevel <= 0) { | 51 if (_asyncLevel <= 0) { |
50 if (!_initialized) { | 52 if (!_initialized) { |
51 throw _buildException('asyncEnd() was called before asyncStart().'); | 53 throw _buildException('asyncEnd() was called before asyncStart().'); |
52 } else { | 54 } else { |
53 throw _buildException('asyncEnd() was called more often than ' | 55 throw _buildException('asyncEnd() was called more often than ' |
54 'asyncStart().'); | 56 'asyncStart().'); |
55 } | 57 } |
56 } | 58 } |
57 _asyncLevel--; | 59 _asyncLevel--; |
58 if (_asyncLevel == 0) { | 60 if (_asyncLevel == 0) { |
59 _port.close(); | 61 _port.close(); |
60 _port = null; | 62 _port = null; |
61 print('unittest-suite-success'); | 63 print('unittest-suite-success'); |
62 } | 64 } |
63 } | 65 } |
kustermann
2013/09/09 12:41:54
In general I've noticed that people misuse these t
| |
64 | 66 |
67 /** | |
68 * Call this after an asynchronous test has ended successfully. This is a helper | |
69 * for calling [asyncEnd]. | |
70 * | |
71 * This method intentionally has a signature that matches [:Future.then:] as a | |
72 * convenience for calling [asyncEnd] when a [:Future:] completes without error, | |
73 * like this: | |
74 * | |
75 * asyncStart(); | |
76 * Future result = test(); | |
77 * result.then(asyncSuccess); | |
78 */ | |
79 void asyncSuccess(_) => asyncEnd(); | |
80 | |
81 /** | |
82 * Helper method for performing asynchronous tests involving [:Future:]. | |
83 * | |
84 * [f] must return a [:Future:] for the test computation. | |
85 */ | |
65 void asyncTest(f()) { | 86 void asyncTest(f()) { |
kustermann
2013/09/09 12:41:54
Either document this on the top of the file or at
Johnni Winther
2013/09/13 07:34:59
Added a top comment as well.
| |
66 asyncStart(); | 87 asyncStart(); |
67 f().whenComplete(() => asyncEnd()); | 88 f().then(asyncSuccess); |
68 } | 89 } |
OLD | NEW |