| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 /** | 5 /** |
| 6 * A library for writing dart unit tests. | 6 * A library for writing dart unit tests. |
| 7 * | 7 * |
| 8 * To import this library, use the pub package manager. | 8 * To import this library, use the pub package manager. |
| 9 * Create a pubspec.yaml file in your project and add | 9 * Create a pubspec.yaml file in your project and add |
| 10 * a dependency on unittest with the following lines: | 10 * a dependency on unittest with the following lines: |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 /** | 226 /** |
| 227 * A map that can be used to communicate state between a test driver | 227 * A map that can be used to communicate state between a test driver |
| 228 * or main() function and the tests, particularly when these two | 228 * or main() function and the tests, particularly when these two |
| 229 * are otherwise independent. For example, a test driver that starts | 229 * are otherwise independent. For example, a test driver that starts |
| 230 * an HTTP server and then runs tests that access that server could use | 230 * an HTTP server and then runs tests that access that server could use |
| 231 * this as a way of communicating the server port to the tests. | 231 * this as a way of communicating the server port to the tests. |
| 232 */ | 232 */ |
| 233 Map testState = {}; | 233 Map testState = {}; |
| 234 | 234 |
| 235 /** | 235 /** |
| 236 * (Deprecated) Evaluates the [function] and validates that it throws an | |
| 237 * exception. If [callback] is provided, then it will be invoked with the | |
| 238 * thrown exception. The callback may do any validation it wants. In addition, | |
| 239 * if it returns `false`, that also indicates an expectation failure. | |
| 240 */ | |
| 241 void expectThrow(function, [bool callback(exception)]) { | |
| 242 bool threw = false; | |
| 243 try { | |
| 244 function(); | |
| 245 } catch (e) { | |
| 246 threw = true; | |
| 247 | |
| 248 // Also let the callback look at it. | |
| 249 if (callback != null) { | |
| 250 var result = callback(e); | |
| 251 | |
| 252 // If the callback explicitly returned false, treat that like an | |
| 253 // expectation too. (If it returns null, though, don't.) | |
| 254 if (result == false) { | |
| 255 fail('Exception:\n$e\ndid not match expectation.'); | |
| 256 } | |
| 257 } | |
| 258 } | |
| 259 | |
| 260 if (threw != true) fail('An expected exception was not thrown.'); | |
| 261 } | |
| 262 | |
| 263 /** | |
| 264 * Creates a new test case with the given description and body. The | 236 * Creates a new test case with the given description and body. The |
| 265 * description will include the descriptions of any surrounding group() | 237 * description will include the descriptions of any surrounding group() |
| 266 * calls. | 238 * calls. |
| 267 */ | 239 */ |
| 268 void test(String spec, TestFunction body) { | 240 void test(String spec, TestFunction body) { |
| 269 ensureInitialized(); | 241 ensureInitialized(); |
| 270 _tests.add(new TestCase(_tests.length + 1, _fullSpec(spec), body, 0)); | 242 _tests.add(new TestCase(_tests.length + 1, _fullSpec(spec), body, 0)); |
| 271 } | 243 } |
| 272 | 244 |
| 273 /** | 245 /** |
| 274 * (Deprecated) Creates a new async test case with the given description | |
| 275 * and body. The description will include the descriptions of any surrounding | |
| 276 * group() calls. | |
| 277 */ | |
| 278 // TODO(sigmund): deprecate this API | |
| 279 void asyncTest(String spec, int callbacks, TestFunction body) { | |
| 280 ensureInitialized(); | |
| 281 | |
| 282 final testCase = new TestCase( | |
| 283 _tests.length + 1, _fullSpec(spec), body, callbacks); | |
| 284 _tests.add(testCase); | |
| 285 | |
| 286 if (callbacks < 1) { | |
| 287 testCase.error( | |
| 288 'Async tests must wait for at least one callback ', ''); | |
| 289 } | |
| 290 } | |
| 291 | |
| 292 /** | |
| 293 * Creates a new test case with the given description and body. The | 246 * Creates a new test case with the given description and body. The |
| 294 * description will include the descriptions of any surrounding group() | 247 * description will include the descriptions of any surrounding group() |
| 295 * calls. | 248 * calls. |
| 296 * | 249 * |
| 297 * "solo_" means that this will be the only test that is run. All other tests | 250 * "solo_" means that this will be the only test that is run. All other tests |
| 298 * will be skipped. This is a convenience function to let you quickly isolate | 251 * will be skipped. This is a convenience function to let you quickly isolate |
| 299 * a single test by adding "solo_" before it to temporarily disable all other | 252 * a single test by adding "solo_" before it to temporarily disable all other |
| 300 * tests. | 253 * tests. |
| 301 */ | 254 */ |
| 302 void solo_test(String spec, TestFunction body) { | 255 void solo_test(String spec, TestFunction body) { |
| (...skipping 620 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 923 } | 876 } |
| 924 | 877 |
| 925 /** Enable a test by ID. */ | 878 /** Enable a test by ID. */ |
| 926 void enableTest(int testId) => _setTestEnabledState(testId, true); | 879 void enableTest(int testId) => _setTestEnabledState(testId, true); |
| 927 | 880 |
| 928 /** Disable a test by ID. */ | 881 /** Disable a test by ID. */ |
| 929 void disableTest(int testId) => _setTestEnabledState(testId, false); | 882 void disableTest(int testId) => _setTestEnabledState(testId, false); |
| 930 | 883 |
| 931 /** Signature for a test function. */ | 884 /** Signature for a test function. */ |
| 932 typedef void TestFunction(); | 885 typedef void TestFunction(); |
| OLD | NEW |