Skip to content

Commit 8bcc876

Browse files
committed
pkg/unittest: deprecate expectAsyncN and expectAsyncUntilN
add top-level expectAsync and expectAsyncUntil methods which take care of counting arguments [email protected] Review URL: https://codereview.chromium.org//137223007 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@32449 260f80e4-7a28-3924-810f-c04153c831b5
1 parent deaed72 commit 8bcc876

File tree

3 files changed

+101
-31
lines changed

3 files changed

+101
-31
lines changed

pkg/unittest/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ occur is async operations are reported back to the source test case.
77
* **DEPRECATED** `guardAsync`, `protectAsync0`, `protectAsync1`,
88
and `protectAsync2`
99
* Running each test in a `Zone` addresses the need for these methods.
10+
* **NEW!** `expectAsync` replaces the now deprecated `expectAsync0`,
11+
`expectAsync1` and `expectAsync2`
12+
* **NEW!** `expectAsyncUntil` replaces the now deprecated `expectAsyncUntil0`,
13+
`expectAsyncUntil1` and `expectAsyncUntil2`
1014
* `TestCase`:
1115
* Removed properties: `setUp`, `tearDown`, `testFunction`
1216
* `enabled` is now get-only

pkg/unittest/lib/unittest.dart

Lines changed: 95 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ void solo_test(String spec, TestFunction body) {
313313
* Indicate that [callback] is expected to be called a [count] number of times
314314
* (by default 1). The unittest framework will wait for the callback to run the
315315
* 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
317317
* tracked and reported. [callback] should take 0 positional arguments (named
318318
* arguments are not supported). [id] can be used to provide more
319319
* descriptive error messages if the callback is called more often than
@@ -324,56 +324,107 @@ void solo_test(String spec, TestFunction body) {
324324
* called exactly [count] times. A value of -1 for [max] will mean no upper
325325
* bound.
326326
*/
327-
// TODO(sigmund): deprecate this API when issue 2706 is fixed.
328-
Function expectAsync0(Function callback,
327+
Function expectAsync(Function callback,
329328
{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+
}
331343
}
332344

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
335361
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);
339364

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
342371
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);
346374

347375
/**
348376
* Indicate that [callback] is expected to be called until [isDone] returns
349377
* true. The unittest framework check [isDone] after each callback and only
350378
* 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
352380
* [callback] are tracked and reported. [callback] should take 0 positional
353381
* arguments (named arguments are not supported). [id] can be used to
354382
* identify the callback in error messages (for example if it is called
355383
* after the test case is complete).
356384
*/
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+
}
360400
}
361401

362402
/**
363-
* Like [expectAsyncUntil0] but [callback] should take 1 positional argument.
403+
* *Deprecated*
404+
*
405+
* Use [expectAsyncUntil] instead.
364406
*/
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);
369410

370411
/**
371-
* Like [expectAsyncUntil0] but [callback] should take 2 positional arguments.
412+
* *Deprecated*
413+
*
414+
* Use [expectAsyncUntil] instead.
372415
*/
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);
377428

378429
/**
379430
* *Deprecated*
@@ -634,7 +685,7 @@ void _ensureInitialized(bool configAutoStart) {
634685
}
635686
_initialized = true;
636687
// Hook our async guard into the matcher library.
637-
wrapAsync = (f, [id]) => expectAsync1(f, id: id);
688+
wrapAsync = (f, [id]) => expectAsync(f, id: id);
638689

639690
_uncaughtErrorMessage = null;
640691

@@ -719,3 +770,18 @@ Trace _getTrace(stack) {
719770
return frame.package != 'unittest' || frame.member != 'TestCase._runTest';
720771
})).terse.foldFrames((frame) => frame.package == 'unittest' || frame.isCore);
721772
}
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+
}

pkg/unittest/test/test_utils.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void shouldFail(value, Matcher matcher, expected, {bool isAsync: false}) {
4141
}
4242

4343
if (isAsync) {
44-
Timer.run(expectAsync0(afterTest));
44+
Timer.run(expectAsync(afterTest));
4545
} else {
4646
afterTest();
4747
}
@@ -57,7 +57,7 @@ void shouldPass(value, Matcher matcher, {bool isAsync: false}) {
5757
expect(errorCount, equals(0));
5858
}
5959
if (isAsync) {
60-
Timer.run(expectAsync0(afterTest));
60+
Timer.run(expectAsync(afterTest));
6161
} else {
6262
afterTest();
6363
}

0 commit comments

Comments
 (0)