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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 } | 43 } |
44 | 44 |
45 /// Implementation method called from language_tests.js. | 45 /// Implementation method called from language_tests.js. |
46 /// Returns true if an asyncTest was started. | 46 /// Returns true if an asyncTest was started. |
47 bool get asyncTestStarted => _initialized; | 47 bool get asyncTestStarted => _initialized; |
48 | 48 |
49 /// Call this method before an asynchronous test is created. | 49 /// Call this method before an asynchronous test is created. |
50 void asyncStart() { | 50 void asyncStart() { |
51 if (_initialized && _asyncLevel == 0) { | 51 if (_initialized && _asyncLevel == 0) { |
52 throw _buildException('asyncStart() was called even though we are done ' | 52 throw _buildException('asyncStart() was called even though we are done ' |
53 'with testing.'); | 53 'with testing.'); |
54 } | 54 } |
55 if (!_initialized) { | 55 if (!_initialized) { |
56 if (_onAsyncEnd == null) { | 56 if (_onAsyncEnd == null) { |
57 throw _buildException( | 57 throw _buildException( |
58 'asyncStart() was called before asyncTestInitialize()'); | 58 'asyncStart() was called before asyncTestInitialize()'); |
59 } | 59 } |
60 | 60 |
61 print('unittest-suite-wait-for-done'); | 61 print('unittest-suite-wait-for-done'); |
62 _initialized = true; | 62 _initialized = true; |
63 | |
64 } | 63 } |
65 _asyncLevel++; | 64 _asyncLevel++; |
66 } | 65 } |
67 | 66 |
68 /// Call this after an asynchronous test has ended successfully. | 67 /// Call this after an asynchronous test has ended successfully. |
69 void asyncEnd() { | 68 void asyncEnd() { |
70 if (_asyncLevel <= 0) { | 69 if (_asyncLevel <= 0) { |
71 if (!_initialized) { | 70 if (!_initialized) { |
72 throw _buildException('asyncEnd() was called before asyncStart().'); | 71 throw _buildException('asyncEnd() was called before asyncStart().'); |
73 } else { | 72 } else { |
74 throw _buildException('asyncEnd() was called more often than ' | 73 throw _buildException('asyncEnd() was called more often than ' |
75 'asyncStart().'); | 74 'asyncStart().'); |
76 } | 75 } |
77 } | 76 } |
78 _asyncLevel--; | 77 _asyncLevel--; |
79 if (_asyncLevel == 0) { | 78 if (_asyncLevel == 0) { |
80 var callback = _onAsyncEnd; | 79 var callback = _onAsyncEnd; |
81 _onAsyncEnd = null; | 80 _onAsyncEnd = null; |
82 callback(); | 81 callback(); |
83 print('unittest-suite-success'); | 82 print('unittest-suite-success'); |
84 } | 83 } |
85 } | 84 } |
(...skipping 14 matching lines...) Expand all Loading... |
100 | 99 |
101 /** | 100 /** |
102 * Helper method for performing asynchronous tests involving [:Future:]. | 101 * Helper method for performing asynchronous tests involving [:Future:]. |
103 * | 102 * |
104 * [f] must return a [:Future:] for the test computation. | 103 * [f] must return a [:Future:] for the test computation. |
105 */ | 104 */ |
106 void asyncTest(f()) { | 105 void asyncTest(f()) { |
107 asyncStart(); | 106 asyncStart(); |
108 f().then(asyncSuccess); | 107 f().then(asyncSuccess); |
109 } | 108 } |
OLD | NEW |