@@ -313,7 +313,7 @@ void solo_test(String spec, TestFunction body) {
313
313
* Indicate that [callback] is expected to be called a [count] number of times
314
314
* (by default 1). The unittest framework will wait for the callback to run the
315
315
* specified [count] times before it continues with the following test. Using
316
- * [expectAsync0 ] will also ensure that errors that occur within [callback] are
316
+ * [expectAsync ] will also ensure that errors that occur within [callback] are
317
317
* tracked and reported. [callback] should take 0 positional arguments (named
318
318
* arguments are not supported). [id] can be used to provide more
319
319
* descriptive error messages if the callback is called more often than
@@ -324,56 +324,107 @@ void solo_test(String spec, TestFunction body) {
324
324
* called exactly [count] times. A value of -1 for [max] will mean no upper
325
325
* bound.
326
326
*/
327
- // TODO(sigmund): deprecate this API when issue 2706 is fixed.
328
- Function expectAsync0 (Function callback,
327
+ Function expectAsync (Function callback,
329
328
{int count: 1 , int max: 0 , String id}) {
330
- return new _SpreadArgsHelper (callback, count, max, null , id).invoke0;
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
+ }
331
343
}
332
344
333
- /** Like [expectAsync0] but [callback] should take 1 positional argument. */
334
- // TODO(sigmund): deprecate this API when issue 2706 is fixed.
345
+ /**
346
+ * *Deprecated*
347
+ *
348
+ * Use [expectAsync] instead.
349
+ */
350
+ @deprecated
351
+ Function expectAsync0 (Function callback,
352
+ {int count: 1 , int max: 0 , String id}) =>
353
+ expectAsync (callback, count: count, max: max, id: id);
354
+
355
+ /**
356
+ * *Deprecated*
357
+ *
358
+ * Use [expectAsync] instead.
359
+ */
360
+ @deprecated
335
361
Function expectAsync1 (Function callback,
336
- {int count: 1 , int max: 0 , String id}) {
337
- return new _SpreadArgsHelper (callback, count, max, null , id).invoke1;
338
- }
362
+ {int count: 1 , int max: 0 , String id}) =>
363
+ expectAsync (callback, count: count, max: max, id: id);
339
364
340
- /** Like [expectAsync0] but [callback] should take 2 positional arguments. */
341
- // TODO(sigmund): deprecate this API when issue 2706 is fixed.
365
+ /**
366
+ * *Deprecated*
367
+ *
368
+ * Use [expectAsync] instead.
369
+ */
370
+ @deprecated
342
371
Function expectAsync2 (Function callback,
343
- {int count: 1 , int max: 0 , String id}) {
344
- return new _SpreadArgsHelper (callback, count, max, null , id).invoke2;
345
- }
372
+ {int count: 1 , int max: 0 , String id}) =>
373
+ expectAsync (callback, count: count, max: max, id: id);
346
374
347
375
/**
348
376
* Indicate that [callback] is expected to be called until [isDone] returns
349
377
* true. The unittest framework check [isDone] after each callback and only
350
378
* when it returns true will it continue with the following test. Using
351
- * [expectAsyncUntil0 ] will also ensure that errors that occur within
379
+ * [expectAsyncUntil ] will also ensure that errors that occur within
352
380
* [callback] are tracked and reported. [callback] should take 0 positional
353
381
* arguments (named arguments are not supported). [id] can be used to
354
382
* identify the callback in error messages (for example if it is called
355
383
* after the test case is complete).
356
384
*/
357
- // TODO(sigmund): deprecate this API when issue 2706 is fixed.
358
- Function expectAsyncUntil0 (Function callback, Function isDone, {String id}) {
359
- return new _SpreadArgsHelper (callback, 0 , - 1 , isDone, id).invoke0;
385
+ Function expectAsyncUntil (Function callback, Function isDone, {String id}) {
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
+ }
360
400
}
361
401
362
402
/**
363
- * Like [expectAsyncUntil0] but [callback] should take 1 positional argument.
403
+ * *Deprecated*
404
+ *
405
+ * Use [expectAsyncUntil] instead.
364
406
*/
365
- // TODO(sigmund): deprecate this API when issue 2706 is fixed.
366
- Function expectAsyncUntil1 (Function callback, Function isDone, {String id}) {
367
- return new _SpreadArgsHelper (callback, 0 , - 1 , isDone, id).invoke1;
368
- }
407
+ @deprecated
408
+ Function expectAsyncUntil0 (Function callback, Function isDone, {String id}) =>
409
+ expectAsyncUntil (callback, isDone, id: id);
369
410
370
411
/**
371
- * Like [expectAsyncUntil0] but [callback] should take 2 positional arguments.
412
+ * *Deprecated*
413
+ *
414
+ * Use [expectAsyncUntil] instead.
372
415
*/
373
- // TODO(sigmund): deprecate this API when issue 2706 is fixed.
374
- Function expectAsyncUntil2 (Function callback, Function isDone, {String id}) {
375
- return new _SpreadArgsHelper (callback, 0 , - 1 , isDone, id).invoke2;
376
- }
416
+ @deprecated
417
+ Function expectAsyncUntil1 (Function callback, Function isDone, {String id}) =>
418
+ expectAsyncUntil (callback, isDone, id: id);
419
+
420
+ /**
421
+ * *Deprecated*
422
+ *
423
+ * Use [expectAsyncUntil] instead.
424
+ */
425
+ @deprecated
426
+ Function expectAsyncUntil2 (Function callback, Function isDone, {String id}) =>
427
+ expectAsyncUntil (callback, isDone, id: id);
377
428
378
429
/**
379
430
* *Deprecated*
@@ -634,7 +685,7 @@ void _ensureInitialized(bool configAutoStart) {
634
685
}
635
686
_initialized = true ;
636
687
// Hook our async guard into the matcher library.
637
- wrapAsync = (f, [id]) => expectAsync1 (f, id: id);
688
+ wrapAsync = (f, [id]) => expectAsync (f, id: id);
638
689
639
690
_uncaughtErrorMessage = null ;
640
691
@@ -719,3 +770,18 @@ Trace _getTrace(stack) {
719
770
return frame.package != 'unittest' || frame.member != 'TestCase._runTest' ;
720
771
})).terse.foldFrames ((frame) => frame.package == 'unittest' || frame.isCore);
721
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
+ }
0 commit comments