| 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 /// Support for writing Dart unit tests. | 5 /// Support for writing Dart unit tests. |
| 6 /// | 6 /// |
| 7 /// For information on installing and importing this library, see the | 7 /// For information on installing and importing this library, see the |
| 8 /// [unittest package on pub.dartlang.org] | 8 /// [unittest package on pub.dartlang.org] |
| 9 /// (http://pub.dartlang.org/packages/unittest). | 9 /// (http://pub.dartlang.org/packages/unittest). |
| 10 /// | 10 /// |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 import 'src/configuration.dart'; | 147 import 'src/configuration.dart'; |
| 148 export 'src/configuration.dart'; | 148 export 'src/configuration.dart'; |
| 149 | 149 |
| 150 part 'src/simple_configuration.dart'; | 150 part 'src/simple_configuration.dart'; |
| 151 part 'src/group_context.dart'; | 151 part 'src/group_context.dart'; |
| 152 part 'src/spread_args_helper.dart'; | 152 part 'src/spread_args_helper.dart'; |
| 153 part 'src/test_case.dart'; | 153 part 'src/test_case.dart'; |
| 154 | 154 |
| 155 Configuration _config; | 155 Configuration _config; |
| 156 | 156 |
| 157 /// [Configuration] used by the unittest library. Note that if a | 157 /// [Configuration] used by the unittest library. |
| 158 /// configuration has not been set, calling this getter will create | 158 /// |
| 159 /// a default configuration. | 159 /// Note that if a configuration has not been set, calling this getter will |
| 160 /// create a default configuration. |
| 160 Configuration get unittestConfiguration { | 161 Configuration get unittestConfiguration { |
| 161 if (_config == null) { | 162 if (_config == null) { |
| 162 _config = new Configuration(); | 163 _config = new Configuration(); |
| 163 } | 164 } |
| 164 return _config; | 165 return _config; |
| 165 } | 166 } |
| 166 | 167 |
| 167 /// Sets the [Configuration] used by the unittest library. | 168 /// Sets the [Configuration] used by the unittest library. |
| 168 /// | 169 /// |
| 169 /// Throws a [StateError] if there is an existing, incompatible value. | 170 /// Throws a [StateError] if there is an existing, incompatible value. |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 } | 279 } |
| 279 ++_soloNestingLevel; | 280 ++_soloNestingLevel; |
| 280 try { | 281 try { |
| 281 test(spec, body); | 282 test(spec, body); |
| 282 } finally { | 283 } finally { |
| 283 --_soloNestingLevel; | 284 --_soloNestingLevel; |
| 284 } | 285 } |
| 285 } | 286 } |
| 286 | 287 |
| 287 /// Indicate that [callback] is expected to be called a [count] number of times | 288 /// Indicate that [callback] is expected to be called a [count] number of times |
| 288 /// (by default 1). The unittest framework will wait for the callback to run the | 289 /// (by default 1). |
| 290 /// |
| 291 /// The unittest framework will wait for the callback to run the |
| 289 /// specified [count] times before it continues with the following test. Using | 292 /// specified [count] times before it continues with the following test. Using |
| 290 /// [expectAsync] will also ensure that errors that occur within [callback] are | 293 /// [expectAsync] will also ensure that errors that occur within [callback] are |
| 291 /// tracked and reported. [callback] should take 0 positional arguments (named | 294 /// tracked and reported. [callback] should take 0 positional arguments (named |
| 292 /// arguments are not supported). [id] can be used to provide more | 295 /// arguments are not supported). [id] can be used to provide more |
| 293 /// descriptive error messages if the callback is called more often than | 296 /// descriptive error messages if the callback is called more often than |
| 294 /// expected. [max] can be used to specify an upper bound on the number of | 297 /// expected. |
| 298 /// |
| 299 /// [max] can be used to specify an upper bound on the number of |
| 295 /// calls; if this is exceeded the test will fail (or be marked as in error if | 300 /// calls; if this is exceeded the test will fail (or be marked as in error if |
| 296 /// it was already complete). A value of 0 for [max] (the default) will set | 301 /// it was already complete). A value of 0 for [max] (the default) will set |
| 297 /// the upper bound to the same value as [count]; i.e. the callback should be | 302 /// the upper bound to the same value as [count]; i.e. the callback should be |
| 298 /// called exactly [count] times. A value of -1 for [max] will mean no upper | 303 /// called exactly [count] times. A value of -1 for [max] will mean no upper |
| 299 /// bound. | 304 /// bound. |
| 300 /// | 305 /// |
| 301 /// [reason] is optional and is typically not supplied, as a reason is generated | 306 /// [reason] is optional and is typically not supplied, as a reason is generated |
| 302 /// by the unittest package; if reason is included it is appended to the | 307 /// by the unittest package; if reason is included it is appended to the |
| 303 /// generated reason. | 308 /// generated reason. |
| 304 Function expectAsync(Function callback, | 309 Function expectAsync(Function callback, |
| 305 {int count: 1, int max: 0, String id, String reason}) => | 310 {int count: 1, int max: 0, String id, String reason}) => |
| 306 new _SpreadArgsHelper(callback, count, max, id, reason).func; | 311 new _SpreadArgsHelper(callback, count, max, id, reason).func; |
| 307 | 312 |
| 308 /// Indicate that [callback] is expected to be called until [isDone] returns | 313 /// Indicate that [callback] is expected to be called until [isDone] returns |
| 309 /// true. The unittest framework check [isDone] after each callback and only | 314 /// true. |
| 315 /// |
| 316 /// The unittest framework checks [isDone] after each callback and only |
| 310 /// when it returns true will it continue with the following test. Using | 317 /// when it returns true will it continue with the following test. Using |
| 311 /// [expectAsyncUntil] will also ensure that errors that occur within | 318 /// [expectAsyncUntil] will also ensure that errors that occur within |
| 312 /// [callback] are tracked and reported. [callback] should take 0 positional | 319 /// [callback] are tracked and reported. [callback] should take 0 positional |
| 313 /// arguments (named arguments are not supported). [id] can be used to | 320 /// arguments (named arguments are not supported). [id] can be used to |
| 314 /// identify the callback in error messages (for example if it is called | 321 /// identify the callback in error messages (for example if it is called |
| 315 /// after the test case is complete). | 322 /// after the test case is complete). |
| 316 /// | 323 /// |
| 317 /// [reason] is optional and is typically not supplied, as a reason is generated | 324 /// [reason] is optional and is typically not supplied, as a reason is generated |
| 318 /// by the unittest package; if reason is included it is appended to the | 325 /// by the unittest package; if reason is included it is appended to the |
| 319 /// generated reason. | 326 /// generated reason. |
| 320 Function expectAsyncUntil(Function callback, bool isDone(), | 327 Function expectAsyncUntil(Function callback, bool isDone(), |
| 321 {String id, String reason}) => | 328 {String id, String reason}) => |
| 322 new _SpreadArgsHelper(callback, 0, -1, id, reason, isDone: isDone).func; | 329 new _SpreadArgsHelper(callback, 0, -1, id, reason, isDone: isDone).func; |
| 323 | 330 |
| 324 /// Creates a new named group of tests. Calls to group() or test() within the | 331 /// Creates a new named group of tests. |
| 325 /// body of the function passed to this will inherit this group's description. | 332 /// |
| 333 /// Calls to group() or test() within the body of the function passed to this |
| 334 /// named group will inherit this group's description. |
| 326 void group(String description, void body()) { | 335 void group(String description, void body()) { |
| 327 ensureInitialized(); | 336 ensureInitialized(); |
| 328 _requireNotRunning(); | 337 _requireNotRunning(); |
| 329 _currentContext = new _GroupContext(_currentContext, description); | 338 _currentContext = new _GroupContext(_currentContext, description); |
| 330 try { | 339 try { |
| 331 body(); | 340 body(); |
| 332 } catch (e, trace) { | 341 } catch (e, trace) { |
| 333 var stack = (trace == null) ? '' : ': ${trace.toString()}'; | 342 var stack = (trace == null) ? '' : ': ${trace.toString()}'; |
| 334 _uncaughtErrorMessage = "${e.toString()}$stack"; | 343 _uncaughtErrorMessage = "${e.toString()}$stack"; |
| 335 } finally { | 344 } finally { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 351 _testCases.clear(); | 360 _testCases.clear(); |
| 352 } | 361 } |
| 353 ++_soloNestingLevel; | 362 ++_soloNestingLevel; |
| 354 try { | 363 try { |
| 355 group(description, body); | 364 group(description, body); |
| 356 } finally { | 365 } finally { |
| 357 --_soloNestingLevel; | 366 --_soloNestingLevel; |
| 358 } | 367 } |
| 359 } | 368 } |
| 360 | 369 |
| 361 /// Register a [setUp] function for a test [group]. This function will | 370 /// Register a [setUp] function for a test [group]. |
| 362 /// be called before each test in the group is run. | 371 /// |
| 372 /// This function will be called before each test in the group is run. |
| 363 /// [setUp] and [tearDown] should be called within the [group] before any | 373 /// [setUp] and [tearDown] should be called within the [group] before any |
| 364 /// calls to [test]. The [setupTest] function can be asynchronous; in this | 374 /// calls to [test]. The [setupTest] function can be asynchronous; in this |
| 365 /// case it must return a [Future]. | 375 /// case it must return a [Future]. |
| 366 void setUp(Function setupTest) { | 376 void setUp(Function setupTest) { |
| 367 _requireNotRunning(); | 377 _requireNotRunning(); |
| 368 _currentContext.testSetup = setupTest; | 378 _currentContext.testSetup = setupTest; |
| 369 } | 379 } |
| 370 | 380 |
| 371 /// Register a [tearDown] function for a test [group]. This function will | 381 /// Register a [tearDown] function for a test [group]. |
| 372 /// be called after each test in the group is run. Note that if groups | 382 /// |
| 373 /// are nested only the most locally scoped [teardownTest] function will be run. | 383 /// This function will be called after each test in the group is run. |
| 374 /// [setUp] and [tearDown] should be called within the [group] before any | 384 /// |
| 375 /// calls to [test]. The [teardownTest] function can be asynchronous; in this | 385 /// Note that if groups are nested only the most locally scoped [teardownTest] |
| 376 /// case it must return a [Future]. | 386 /// function will be run. [setUp] and [tearDown] should be called within the |
| 387 /// [group] before any calls to [test]. The [teardownTest] function can be |
| 388 /// asynchronous; in this case it must return a [Future]. |
| 377 void tearDown(Function teardownTest) { | 389 void tearDown(Function teardownTest) { |
| 378 _requireNotRunning(); | 390 _requireNotRunning(); |
| 379 _currentContext.testTeardown = teardownTest; | 391 _currentContext.testTeardown = teardownTest; |
| 380 } | 392 } |
| 381 | 393 |
| 382 /// Advance to the next test case. | 394 /// Advance to the next test case. |
| 383 void _nextTestCase() { | 395 void _nextTestCase() { |
| 384 _currentTestCaseIndex++; | 396 _currentTestCaseIndex++; |
| 385 _runTest(); | 397 _runTest(); |
| 386 } | 398 } |
| 387 | 399 |
| 388 /// Handle errors that happen outside the tests. | 400 /// Handle errors that happen outside the tests. |
| 389 // TODO(vsm): figure out how to expose the stack trace here | 401 // TODO(vsm): figure out how to expose the stack trace here |
| 390 // Currently e.message works in dartium, but not in dartc. | 402 // Currently e.message works in dartium, but not in dartc. |
| 391 void handleExternalError(e, String message, [stack]) { | 403 void handleExternalError(e, String message, [stack]) { |
| 392 var msg = '$message\nCaught $e'; | 404 var msg = '$message\nCaught $e'; |
| 393 | 405 |
| 394 if (currentTestCase != null) { | 406 if (currentTestCase != null) { |
| 395 currentTestCase._error(msg, stack); | 407 currentTestCase._error(msg, stack); |
| 396 } else { | 408 } else { |
| 397 _uncaughtErrorMessage = "$msg: $stack"; | 409 _uncaughtErrorMessage = "$msg: $stack"; |
| 398 } | 410 } |
| 399 } | 411 } |
| 400 | 412 |
| 401 /// Filter the tests. [testFilter] can be a [RegExp], a [String] or a | 413 /// Filter the tests by [testFilter]. |
| 402 /// predicate function. This is different to enabling/disabling tests | 414 /// |
| 415 /// [testFilter] can be a [RegExp], a [String] or a |
| 416 /// predicate function. This is different from enabling or disabling tests |
| 403 /// in that it removes the tests completely. | 417 /// in that it removes the tests completely. |
| 404 void filterTests(testFilter) { | 418 void filterTests(testFilter) { |
| 405 var filterFunction; | 419 var filterFunction; |
| 406 if (testFilter is String) { | 420 if (testFilter is String) { |
| 407 RegExp re = new RegExp(testFilter); | 421 RegExp re = new RegExp(testFilter); |
| 408 filterFunction = (t) => re.hasMatch(t.description); | 422 filterFunction = (t) => re.hasMatch(t.description); |
| 409 } else if (testFilter is RegExp) { | 423 } else if (testFilter is RegExp) { |
| 410 filterFunction = (t) => testFilter.hasMatch(t.description); | 424 filterFunction = (t) => testFilter.hasMatch(t.description); |
| 411 } else if (testFilter is Function) { | 425 } else if (testFilter is Function) { |
| 412 filterFunction = testFilter; | 426 filterFunction = testFilter; |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 556 /// Signature for a test function. | 570 /// Signature for a test function. |
| 557 typedef dynamic TestFunction(); | 571 typedef dynamic TestFunction(); |
| 558 | 572 |
| 559 /// A flag that controls whether we hide unittest and core library details in | 573 /// A flag that controls whether we hide unittest and core library details in |
| 560 /// exception stacks. | 574 /// exception stacks. |
| 561 /// | 575 /// |
| 562 /// Useful to disable when debugging unittest or matcher customizations. | 576 /// Useful to disable when debugging unittest or matcher customizations. |
| 563 bool formatStacks = true; | 577 bool formatStacks = true; |
| 564 | 578 |
| 565 /// A flag that controls whether we try to filter out irrelevant frames from | 579 /// A flag that controls whether we try to filter out irrelevant frames from |
| 566 /// the stack trace. Requires formatStacks to be set. | 580 /// the stack trace. |
| 581 /// |
| 582 /// Requires [formatStacks] to be set. |
| 567 bool filterStacks = true; | 583 bool filterStacks = true; |
| 568 | 584 |
| 569 void _requireNotRunning() { | 585 void _requireNotRunning() { |
| 570 if (_currentTestCaseIndex != -1) { | 586 if (_currentTestCaseIndex != -1) { |
| 571 throw new StateError('Not allowed when tests are running.'); | 587 throw new StateError('Not allowed when tests are running.'); |
| 572 } | 588 } |
| 573 } | 589 } |
| OLD | NEW |