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...) Expand 10 before | Expand all | Expand 10 after 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 var minArgs = _minArgs(callback); | 329 new _SpreadArgsHelper(callback, count, max, id).func; |
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 } | |
344 | 330 |
345 /** | 331 /** |
346 * *Deprecated* | 332 * *Deprecated* |
347 * | 333 * |
348 * Use [expectAsync] instead. | 334 * Use [expectAsync] instead. |
349 */ | 335 */ |
350 @deprecated | 336 @deprecated |
351 Function expectAsync0(Function callback, | 337 Function expectAsync0(Function callback, |
352 {int count: 1, int max: 0, String id}) => | 338 {int count: 1, int max: 0, String id}) => |
353 expectAsync(callback, count: count, max: max, id: id); | 339 expectAsync(callback, count: count, max: max, id: id); |
(...skipping 21 matching lines...) Expand all Loading... |
375 /** | 361 /** |
376 * Indicate that [callback] is expected to be called until [isDone] returns | 362 * Indicate that [callback] is expected to be called until [isDone] returns |
377 * true. The unittest framework check [isDone] after each callback and only | 363 * true. The unittest framework check [isDone] after each callback and only |
378 * when it returns true will it continue with the following test. Using | 364 * when it returns true will it continue with the following test. Using |
379 * [expectAsyncUntil] will also ensure that errors that occur within | 365 * [expectAsyncUntil] will also ensure that errors that occur within |
380 * [callback] are tracked and reported. [callback] should take 0 positional | 366 * [callback] are tracked and reported. [callback] should take 0 positional |
381 * arguments (named arguments are not supported). [id] can be used to | 367 * arguments (named arguments are not supported). [id] can be used to |
382 * identify the callback in error messages (for example if it is called | 368 * identify the callback in error messages (for example if it is called |
383 * after the test case is complete). | 369 * after the test case is complete). |
384 */ | 370 */ |
385 Function expectAsyncUntil(Function callback, Function isDone, {String id}) { | 371 Function expectAsyncUntil(Function callback, bool isDone(), {String id}) => |
386 var minArgs = _minArgs(callback); | 372 new _SpreadArgsHelper(callback, 0, -1, id, isDone: isDone).func; |
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 } | |
401 | 373 |
402 /** | 374 /** |
403 * *Deprecated* | 375 * *Deprecated* |
404 * | 376 * |
405 * Use [expectAsyncUntil] instead. | 377 * Use [expectAsyncUntil] instead. |
406 */ | 378 */ |
407 @deprecated | 379 @deprecated |
408 Function expectAsyncUntil0(Function callback, Function isDone, {String id}) => | 380 Function expectAsyncUntil0(Function callback, Function isDone, {String id}) => |
409 expectAsyncUntil(callback, isDone, id: id); | 381 expectAsyncUntil(callback, isDone, id: id); |
410 | 382 |
(...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
763 | 735 |
764 if (!filterStacks) return trace; | 736 if (!filterStacks) return trace; |
765 | 737 |
766 // Format the stack trace by removing everything above TestCase._runTest, | 738 // Format the stack trace by removing everything above TestCase._runTest, |
767 // which is usually going to be irrelevant. Also fold together unittest and | 739 // which is usually going to be irrelevant. Also fold together unittest and |
768 // core library calls so only the function the user called is visible. | 740 // core library calls so only the function the user called is visible. |
769 return new Trace(trace.frames.takeWhile((frame) { | 741 return new Trace(trace.frames.takeWhile((frame) { |
770 return frame.package != 'unittest' || frame.member != 'TestCase._runTest'; | 742 return frame.package != 'unittest' || frame.member != 'TestCase._runTest'; |
771 })).terse.foldFrames((frame) => frame.package == 'unittest' || frame.isCore); | 743 })).terse.foldFrames((frame) => frame.package == 'unittest' || frame.isCore); |
772 } | 744 } |
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 |