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 23 matching lines...) Expand all Loading... |
34 int _asyncLevel = 0; | 34 int _asyncLevel = 0; |
35 | 35 |
36 Exception _buildException(String msg) { | 36 Exception _buildException(String msg) { |
37 return new Exception('Fatal: $msg. This is most likely a bug in your test.'); | 37 return new Exception('Fatal: $msg. This is most likely a bug in your test.'); |
38 } | 38 } |
39 | 39 |
40 /// Call this method before an asynchronous test is created. | 40 /// Call this method before an asynchronous test is created. |
41 void asyncStart() { | 41 void asyncStart() { |
42 if (_initialized && _asyncLevel == 0) { | 42 if (_initialized && _asyncLevel == 0) { |
43 throw _buildException('asyncStart() was called even though we are done ' | 43 throw _buildException('asyncStart() was called even though we are done ' |
44 'with testing.'); | 44 'with testing.'); |
45 } | 45 } |
46 if (!_initialized) { | 46 if (!_initialized) { |
47 print('unittest-suite-wait-for-done'); | 47 print('unittest-suite-wait-for-done'); |
48 _initialized = true; | 48 _initialized = true; |
49 _port = new ReceivePort(); | 49 _port = new ReceivePort(); |
50 } | 50 } |
51 _asyncLevel++; | 51 _asyncLevel++; |
52 } | 52 } |
53 | 53 |
54 /// Call this after an asynchronous test has ended successfully. | 54 /// Call this after an asynchronous test has ended successfully. |
55 void asyncEnd() { | 55 void asyncEnd() { |
56 if (_asyncLevel <= 0) { | 56 if (_asyncLevel <= 0) { |
57 if (!_initialized) { | 57 if (!_initialized) { |
58 throw _buildException('asyncEnd() was called before asyncStart().'); | 58 throw _buildException('asyncEnd() was called before asyncStart().'); |
59 } else { | 59 } else { |
60 throw _buildException('asyncEnd() was called more often than ' | 60 throw _buildException('asyncEnd() was called more often than ' |
61 'asyncStart().'); | 61 'asyncStart().'); |
62 } | 62 } |
63 } | 63 } |
64 _asyncLevel--; | 64 _asyncLevel--; |
65 if (_asyncLevel == 0) { | 65 if (_asyncLevel == 0) { |
66 _port.close(); | 66 _port.close(); |
67 _port = null; | 67 _port = null; |
68 print('unittest-suite-success'); | 68 print('unittest-suite-success'); |
69 } | 69 } |
70 } | 70 } |
71 | 71 |
(...skipping 13 matching lines...) Expand all Loading... |
85 | 85 |
86 /** | 86 /** |
87 * Helper method for performing asynchronous tests involving [:Future:]. | 87 * Helper method for performing asynchronous tests involving [:Future:]. |
88 * | 88 * |
89 * [f] must return a [:Future:] for the test computation. | 89 * [f] must return a [:Future:] for the test computation. |
90 */ | 90 */ |
91 void asyncTest(f()) { | 91 void asyncTest(f()) { |
92 asyncStart(); | 92 asyncStart(); |
93 f().then(asyncSuccess); | 93 f().then(asyncSuccess); |
94 } | 94 } |
OLD | NEW |