| 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 /// A test library for testing test libraries? We must go deeper. | 5 /// A test library for testing test libraries? We must go deeper. |
| 6 /// | 6 /// |
| 7 /// Since unit testing code tends to use a lot of global state, it can be tough | 7 /// Since unit testing code tends to use a lot of global state, it can be tough |
| 8 /// to test. This library manages it by running each test case in a child | 8 /// to test. This library manages it by running each test case in a child |
| 9 /// isolate, then reporting the results back to the parent isolate. | 9 /// isolate, then reporting the results back to the parent isolate. |
| 10 library metatest; | 10 library metatest; |
| 11 | 11 |
| 12 import 'dart:io'; | 12 import 'dart:io'; |
| 13 import 'dart:async'; | 13 import 'dart:async'; |
| 14 import 'dart:isolate'; | 14 import 'dart:isolate'; |
| 15 | 15 |
| 16 import 'package:pathos/path.dart' as path; | 16 import 'package:pathos/path.dart' as path; |
| 17 import 'package:unittest/unittest.dart'; | 17 import 'package:unittest/unittest.dart'; |
| 18 import 'package:scheduled_test/scheduled_test.dart' as scheduled_test; |
| 18 | 19 |
| 19 import 'utils.dart'; | 20 import 'utils.dart'; |
| 20 | 21 |
| 21 // TODO(nweiz): get rid of this once issue 8863 is fixed. | 22 // TODO(nweiz): get rid of this once issue 8863 is fixed. |
| 22 /// The path to the Dart executable. This is only set in a child isolate. | 23 /// The path to the Dart executable. This is only set in a child isolate. |
| 23 String get dartExecutable => _executable; | 24 String get dartExecutable => _executable; |
| 24 String _executable; | 25 String _executable; |
| 25 | 26 |
| 26 /// Declares a test with the given [description] and [body]. [body] corresponds | 27 /// Declares a test with the given [description] and [body]. [body] corresponds |
| 27 /// to the `main` method of a test file, and will be run in an isolate. By | 28 /// to the `main` method of a test file, and will be run in an isolate. By |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 if (_hasError(results)) { | 65 if (_hasError(results)) { |
| 65 throw 'Expected all tests to fail, but got error(s):\n' | 66 throw 'Expected all tests to fail, but got error(s):\n' |
| 66 '${_summarizeTests(results)}'; | 67 '${_summarizeTests(results)}'; |
| 67 } else if (results['passed'] != 0) { | 68 } else if (results['passed'] != 0) { |
| 68 throw 'Expected all tests to fail, but some passed:\n' | 69 throw 'Expected all tests to fail, but some passed:\n' |
| 69 '${_summarizeTests(results)}'; | 70 '${_summarizeTests(results)}'; |
| 70 } | 71 } |
| 71 }); | 72 }); |
| 72 } | 73 } |
| 73 | 74 |
| 75 /// Runs [setUpFn] before every metatest. Note that [setUpFn] will be |
| 76 /// overwritten if the test itself calls [setUp]. |
| 77 void metaSetUp(void setUpFn()) { |
| 78 _inChildIsolate.then((inIsolate) { |
| 79 if (inIsolate) scheduled_test.setUp(setUpFn); |
| 80 }); |
| 81 } |
| 82 |
| 74 /// Sets up a test with the given [description] and [body]. After the test runs, | 83 /// Sets up a test with the given [description] and [body]. After the test runs, |
| 75 /// calls [validate] with the result map. | 84 /// calls [validate] with the result map. |
| 76 void _setUpTest(String description, void body(), void validate(Map)) { | 85 void _setUpTest(String description, void body(), void validate(Map)) { |
| 77 _inChildIsolate.then((inIsolate) { | 86 _inChildIsolate.then((inIsolate) { |
| 78 if (inIsolate) { | 87 if (inIsolate) { |
| 79 _ensureInitialized(); | 88 _ensureInitialized(); |
| 80 if (_testToRun == description) body(); | 89 if (_testToRun == description) body(); |
| 81 } else { | 90 } else { |
| 82 test(description, () { | 91 test(description, () { |
| 83 expect(_runInIsolate(description).then(validate), completes); | 92 expect(_runInIsolate(description).then(validate), completes); |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 "message": testCase.message, | 217 "message": testCase.message, |
| 209 "result": testCase.result, | 218 "result": testCase.result, |
| 210 "stackTrace": testCase.stackTrace | 219 "stackTrace": testCase.stackTrace |
| 211 }).toList() | 220 }).toList() |
| 212 }); | 221 }); |
| 213 } | 222 } |
| 214 | 223 |
| 215 void onInit() {} | 224 void onInit() {} |
| 216 void onDone(bool success) {} | 225 void onDone(bool success) {} |
| 217 } | 226 } |
| OLD | NEW |