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 /** | 5 /** |
6 * Support for writing Dart unit tests. | 6 * Support for writing Dart unit tests. |
7 * | 7 * |
8 * For information on installing and importing this library, see the | 8 * For information on installing and importing this library, see the |
9 * [unittest package on pub.dartlang.org] | 9 * [unittest package on pub.dartlang.org] |
10 * (http://pub.dartlang.org/packages/unittest). | 10 * (http://pub.dartlang.org/packages/unittest). |
(...skipping 307 matching lines...) Loading... |
318 * arguments are not supported). [id] can be used to provide more | 318 * arguments are not supported). [id] can be used to provide more |
319 * descriptive error messages if the callback is called more often than | 319 * descriptive error messages if the callback is called more often than |
320 * expected. [max] can be used to specify an upper bound on the number of | 320 * expected. [max] can be used to specify an upper bound on the number of |
321 * calls; if this is exceeded the test will fail (or be marked as in error if | 321 * calls; if this is exceeded the test will fail (or be marked as in error if |
322 * it was already complete). A value of 0 for [max] (the default) will set | 322 * it was already complete). A value of 0 for [max] (the default) will set |
323 * the upper bound to the same value as [count]; i.e. the callback should be | 323 * the upper bound to the same value as [count]; i.e. the callback should be |
324 * called exactly [count] times. A value of -1 for [max] will mean no upper | 324 * called exactly [count] times. A value of -1 for [max] will mean no upper |
325 * bound. | 325 * bound. |
326 */ | 326 */ |
327 Function expectAsync(Function callback, | 327 Function expectAsync(Function callback, |
328 {int count: 1, int max: 0, String id}) => | 328 {int count: 1, int max: 0, String id}) { |
329 new _SpreadArgsHelper(callback, count, max, id); | 329 var minArgs = _minArgs(callback); |
| 330 |
| 331 switch(minArgs) { |
| 332 case 0: |
| 333 return new _SpreadArgsHelper(callback, count, max, null, id).invoke0; |
| 334 case 1: |
| 335 return new _SpreadArgsHelper(callback, count, max, null, id).invoke1; |
| 336 case 2: |
| 337 return new _SpreadArgsHelper(callback, count, max, null, id).invoke2; |
| 338 default: |
| 339 // _minArgs throws an argument exception if the arg count is > 2. |
| 340 // this is just for paranoia |
| 341 throw new StateError('Should never get here'); |
| 342 } |
| 343 } |
330 | 344 |
331 /** | 345 /** |
332 * *Deprecated* | 346 * *Deprecated* |
333 * | 347 * |
334 * Use [expectAsync] instead. | 348 * Use [expectAsync] instead. |
335 */ | 349 */ |
336 @deprecated | 350 @deprecated |
337 Function expectAsync0(Function callback, | 351 Function expectAsync0(Function callback, |
338 {int count: 1, int max: 0, String id}) => | 352 {int count: 1, int max: 0, String id}) => |
339 expectAsync(callback, count: count, max: max, id: id); | 353 expectAsync(callback, count: count, max: max, id: id); |
(...skipping 21 matching lines...) Loading... |
361 /** | 375 /** |
362 * Indicate that [callback] is expected to be called until [isDone] returns | 376 * Indicate that [callback] is expected to be called until [isDone] returns |
363 * true. The unittest framework check [isDone] after each callback and only | 377 * true. The unittest framework check [isDone] after each callback and only |
364 * when it returns true will it continue with the following test. Using | 378 * when it returns true will it continue with the following test. Using |
365 * [expectAsyncUntil] will also ensure that errors that occur within | 379 * [expectAsyncUntil] will also ensure that errors that occur within |
366 * [callback] are tracked and reported. [callback] should take 0 positional | 380 * [callback] are tracked and reported. [callback] should take 0 positional |
367 * arguments (named arguments are not supported). [id] can be used to | 381 * arguments (named arguments are not supported). [id] can be used to |
368 * identify the callback in error messages (for example if it is called | 382 * identify the callback in error messages (for example if it is called |
369 * after the test case is complete). | 383 * after the test case is complete). |
370 */ | 384 */ |
371 Function expectAsyncUntil(Function callback, bool isDone(), {String id}) => | 385 Function expectAsyncUntil(Function callback, Function isDone, {String id}) { |
372 new _SpreadArgsHelper(callback, 0, -1, id, isDone: isDone); | 386 var minArgs = _minArgs(callback); |
| 387 |
| 388 switch(minArgs) { |
| 389 case 0: |
| 390 return new _SpreadArgsHelper(callback, 0, -1, isDone, id).invoke0; |
| 391 case 1: |
| 392 return new _SpreadArgsHelper(callback, 0, -1, isDone, id).invoke1; |
| 393 case 2: |
| 394 return new _SpreadArgsHelper(callback, 0, -1, isDone, id).invoke2; |
| 395 default: |
| 396 // _minArgs throws an argument exception if the arg count is > 2. |
| 397 // this is just for paranoia |
| 398 throw new StateError('Should never get here'); |
| 399 } |
| 400 } |
373 | 401 |
374 /** | 402 /** |
375 * *Deprecated* | 403 * *Deprecated* |
376 * | 404 * |
377 * Use [expectAsyncUntil] instead. | 405 * Use [expectAsyncUntil] instead. |
378 */ | 406 */ |
379 @deprecated | 407 @deprecated |
380 Function expectAsyncUntil0(Function callback, Function isDone, {String id}) => | 408 Function expectAsyncUntil0(Function callback, Function isDone, {String id}) => |
381 expectAsyncUntil(callback, isDone, id: id); | 409 expectAsyncUntil(callback, isDone, id: id); |
382 | 410 |
(...skipping 352 matching lines...) Loading... |
735 | 763 |
736 if (!filterStacks) return trace; | 764 if (!filterStacks) return trace; |
737 | 765 |
738 // Format the stack trace by removing everything above TestCase._runTest, | 766 // Format the stack trace by removing everything above TestCase._runTest, |
739 // which is usually going to be irrelevant. Also fold together unittest and | 767 // which is usually going to be irrelevant. Also fold together unittest and |
740 // core library calls so only the function the user called is visible. | 768 // core library calls so only the function the user called is visible. |
741 return new Trace(trace.frames.takeWhile((frame) { | 769 return new Trace(trace.frames.takeWhile((frame) { |
742 return frame.package != 'unittest' || frame.member != 'TestCase._runTest'; | 770 return frame.package != 'unittest' || frame.member != 'TestCase._runTest'; |
743 })).terse.foldFrames((frame) => frame.package == 'unittest' || frame.isCore); | 771 })).terse.foldFrames((frame) => frame.package == 'unittest' || frame.isCore); |
744 } | 772 } |
| 773 |
| 774 typedef _Func0(); |
| 775 typedef _Func1(a); |
| 776 typedef _Func2(a, b); |
| 777 |
| 778 /** |
| 779 * Throws an [ArgumentError] if the callback has more than 2 required arguments. |
| 780 */ |
| 781 int _minArgs(Function callback) { |
| 782 if (callback is _Func0) return 0; |
| 783 if (callback is _Func1) return 1; |
| 784 if (callback is _Func2) return 2; |
| 785 throw new ArgumentError( |
| 786 'The callback argument has more than 2 required arguments.'); |
| 787 } |
OLD | NEW |