| 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 library unittest; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 | |
| 9 import 'package:path/path.dart' as p; | |
| 10 | |
| 11 import 'src/backend/declarer.dart'; | |
| 12 import 'src/backend/invoker.dart'; | |
| 13 import 'src/backend/suite.dart'; | |
| 14 import 'src/backend/test_platform.dart'; | |
| 15 import 'src/deprecated/configuration.dart'; | |
| 16 import 'src/deprecated/test_case.dart'; | |
| 17 import 'src/runner/reporter/no_io_compact.dart'; | |
| 18 import 'src/utils.dart'; | |
| 19 | |
| 20 export 'package:matcher/matcher.dart'; | |
| 21 | |
| 22 export 'src/deprecated/configuration.dart'; | |
| 23 export 'src/deprecated/simple_configuration.dart'; | |
| 24 export 'src/deprecated/test_case.dart'; | |
| 25 export 'src/frontend/expect.dart'; | |
| 26 export 'src/frontend/expect_async.dart'; | |
| 27 export 'src/frontend/future_matchers.dart'; | |
| 28 export 'src/frontend/prints_matcher.dart'; | |
| 29 export 'src/frontend/test_on.dart'; | |
| 30 export 'src/frontend/throws_matcher.dart'; | |
| 31 export 'src/frontend/throws_matchers.dart'; | |
| 32 | |
| 33 /// The global declarer. | |
| 34 /// | |
| 35 /// This is used if a test file is run directly, rather than through the runner. | |
| 36 Declarer _globalDeclarer; | |
| 37 | |
| 38 /// Gets the declarer for the current scope. | |
| 39 /// | |
| 40 /// When using the runner, this returns the [Zone]-scoped declarer that's set by | |
| 41 /// [IsolateListener] or [IframeListener]. If the test file is run directly, | |
| 42 /// this returns [_globalDeclarer] (and sets it up on the first call). | |
| 43 Declarer get _declarer { | |
| 44 var declarer = Zone.current[#unittest.declarer]; | |
| 45 if (declarer != null) return declarer; | |
| 46 if (_globalDeclarer != null) return _globalDeclarer; | |
| 47 | |
| 48 // Since there's no Zone-scoped declarer, the test file is being run directly. | |
| 49 // In order to run the tests, we set up our own Declarer via | |
| 50 // [_globalDeclarer], and schedule a microtask to run the tests once they're | |
| 51 // finished being defined. | |
| 52 _globalDeclarer = new Declarer(); | |
| 53 scheduleMicrotask(() { | |
| 54 var suite = | |
| 55 new Suite(_globalDeclarer.tests, | |
| 56 path: p.prettyUri(Uri.base), | |
| 57 platform: "VM") | |
| 58 .filter(TestPlatform.vm, os: currentOSGuess); | |
| 59 // TODO(nweiz): Set the exit code on the VM when issue 6943 is fixed. | |
| 60 new NoIoCompactReporter([suite], color: true).run(); | |
| 61 }); | |
| 62 return _globalDeclarer; | |
| 63 } | |
| 64 | |
| 65 // TODO(nweiz): This and other top-level functions should throw exceptions if | |
| 66 // they're called after the declarer has finished declaring. | |
| 67 /// Creates a new test case with the given description and body. | |
| 68 /// | |
| 69 /// The description will be added to the descriptions of any surrounding | |
| 70 /// [group]s. If [testOn] is passed, it's parsed as a [platform selector][]; the | |
| 71 /// test will only be run on matching platforms. | |
| 72 /// | |
| 73 /// [platform selector]: https://github.com/dart-lang/unittest/#platform-selecto
r-syntax | |
| 74 void test(String description, body(), {String testOn}) => | |
| 75 _declarer.test(description, body, testOn: testOn); | |
| 76 | |
| 77 /// Creates a group of tests. | |
| 78 /// | |
| 79 /// A group's description is included in the descriptions of any tests or | |
| 80 /// sub-groups it contains. [setUp] and [tearDown] are also scoped to the | |
| 81 /// containing group. | |
| 82 /// | |
| 83 /// If [testOn] is passed, it's parsed as a [platform selector][]; the test will | |
| 84 /// only be run on matching platforms. | |
| 85 /// | |
| 86 /// [platform selector]: https://github.com/dart-lang/unittest/#platform-selecto
r-syntax | |
| 87 void group(String description, void body(), {String testOn}) => | |
| 88 _declarer.group(description, body, testOn: testOn); | |
| 89 | |
| 90 /// Registers a function to be run before tests. | |
| 91 /// | |
| 92 /// This function will be called before each test is run. [callback] may be | |
| 93 /// asynchronous; if so, it must return a [Future]. | |
| 94 /// | |
| 95 /// If this is called within a test group, it applies only to tests in that | |
| 96 /// group. [callback] will be run after any set-up callbacks in parent groups or | |
| 97 /// at the top level. | |
| 98 void setUp(callback()) => _declarer.setUp(callback); | |
| 99 | |
| 100 /// Registers a function to be run after tests. | |
| 101 /// | |
| 102 /// This function will be called after each test is run. [callback] may be | |
| 103 /// asynchronous; if so, it must return a [Future]. | |
| 104 /// | |
| 105 /// If this is called within a test group, it applies only to tests in that | |
| 106 /// group. [callback] will be run before any tear-down callbacks in parent | |
| 107 /// groups or at the top level. | |
| 108 void tearDown(callback()) => _declarer.tearDown(callback); | |
| 109 | |
| 110 /// Handle an error that occurs outside of any test. | |
| 111 void handleExternalError(error, String message, [stackTrace]) { | |
| 112 // TODO(nweiz): handle this better. | |
| 113 registerException(error, stackTrace); | |
| 114 } | |
| 115 | |
| 116 /// Registers an exception that was caught for the current test. | |
| 117 void registerException(error, [StackTrace stackTrace]) => | |
| 118 Invoker.current.handleError(error, stackTrace); | |
| 119 | |
| 120 // What follows are stubs for various top-level names supported by unittest | |
| 121 // 0.11.*. These are preserved for the time being for ease of migration, but | |
| 122 // should be removed before this is released as stable. | |
| 123 | |
| 124 @deprecated | |
| 125 typedef dynamic TestFunction(); | |
| 126 | |
| 127 @deprecated | |
| 128 Configuration unittestConfiguration = new Configuration(); | |
| 129 | |
| 130 @deprecated | |
| 131 bool formatStacks = true; | |
| 132 | |
| 133 @deprecated | |
| 134 bool filterStacks = true; | |
| 135 | |
| 136 @deprecated | |
| 137 String groupSep = ' '; | |
| 138 | |
| 139 @deprecated | |
| 140 void logMessage(String message) => print(message); | |
| 141 | |
| 142 @deprecated | |
| 143 final testCases = []; | |
| 144 | |
| 145 @deprecated | |
| 146 const int BREATH_INTERVAL = 200; | |
| 147 | |
| 148 @deprecated | |
| 149 TestCase get currentTestCase => null; | |
| 150 | |
| 151 @deprecated | |
| 152 const PASS = 'pass'; | |
| 153 | |
| 154 @deprecated | |
| 155 const FAIL = 'fail'; | |
| 156 | |
| 157 @deprecated | |
| 158 const ERROR = 'error'; | |
| 159 | |
| 160 @deprecated | |
| 161 void skip_test(String spec, TestFunction body) {} | |
| 162 | |
| 163 @deprecated | |
| 164 void solo_test(String spec, TestFunction body) => test(spec, body); | |
| 165 | |
| 166 @deprecated | |
| 167 void skip_group(String description, void body()) {} | |
| 168 | |
| 169 @deprecated | |
| 170 void solo_group(String description, void body()) => group(description, body); | |
| 171 | |
| 172 @deprecated | |
| 173 void filterTests(testFilter) {} | |
| 174 | |
| 175 @deprecated | |
| 176 void runTests() {} | |
| 177 | |
| 178 @deprecated | |
| 179 void ensureInitialized() {} | |
| 180 | |
| 181 @deprecated | |
| 182 void setSoloTest(int id) {} | |
| 183 | |
| 184 @deprecated | |
| 185 void enableTest(int id) {} | |
| 186 | |
| 187 @deprecated | |
| 188 void disableTest(int id) {} | |
| 189 | |
| 190 @deprecated | |
| 191 withTestEnvironment(callback()) => callback(); | |
| OLD | NEW |