OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 /// This library is used for testing asynchronous tests. |
| 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() |
| 8 /// finished] even if the asynchronous operations fail). |
| 9 /// Tests which can't use the unittest framework should use the helper functions |
| 10 /// in this library. |
| 11 /// This library provides two methods |
| 12 /// - asyncStart(): Needs to be called before an asynchronous operation is |
| 13 /// scheduled. |
| 14 /// - asyncEnd(): Needs to be called as soon as the asynchronous operation |
| 15 /// ended. |
| 16 /// After the last asyncStart() called was matched with a corresponding |
| 17 /// asyncEnd() call, the testing driver will be notified that the tests is done. |
| 18 |
| 19 library async_helper; |
| 20 |
| 21 |
| 22 bool _initialized = false; |
| 23 int _asyncLevel = 0; |
| 24 |
| 25 Exception _buildException(String msg) { |
| 26 return new Exception('Fatal: $msg. This is most likely a bug in your test.'); |
| 27 } |
| 28 |
| 29 void asyncStart() { |
| 30 if (_initialized && _asyncLevel == 0) { |
| 31 throw _buildException('asyncStart() was called even though we are done ' |
| 32 'with testing.'); |
| 33 } |
| 34 if (!_initialized) { |
| 35 print('unittest-suite-wait-for-done'); |
| 36 _initialized = true; |
| 37 } |
| 38 _asyncLevel++; |
| 39 } |
| 40 |
| 41 void asyncEnd() { |
| 42 if (_asyncLevel <= 0) { |
| 43 if (!_initialized) { |
| 44 throw _buildException('asyncEnd() was called before asyncStart().'); |
| 45 } else { |
| 46 throw _buildException('asyncEnd() was called more often than ' |
| 47 'asyncStart().'); |
| 48 } |
| 49 } |
| 50 _asyncLevel--; |
| 51 if (_asyncLevel == 0) { |
| 52 print('unittest-suite-done'); |
| 53 } |
| 54 } |
OLD | NEW |