Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(205)

Unified Diff: lib/src/frontend/expect_async.dart

Issue 2515303002: Add expectAsyncX and expectAsyncUntilX methods, and deprecate the old methods. (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « README.md ('k') | pubspec.yaml » ('j') | pubspec.yaml » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/frontend/expect_async.dart
diff --git a/lib/src/frontend/expect_async.dart b/lib/src/frontend/expect_async.dart
index aa4b8b6114efa3d6b8fe71dc61b3eebcdcf6b1a3..1f3dd1b65993276c68af961eb7c8e8ebdf3f0c09 100644
--- a/lib/src/frontend/expect_async.dart
+++ b/lib/src/frontend/expect_async.dart
@@ -29,7 +29,7 @@ typedef bool _IsDoneCallback();
///
/// The wrapper function is accessible via [func]. It supports up to six
/// optional and/or required positional arguments, but no named arguments.
-class _ExpectedFunction {
+class _ExpectedFunction<T> {
/// The wrapped callback.
final Function _callback;
@@ -83,9 +83,8 @@ class _ExpectedFunction {
{String id, String reason, bool isDone()})
: this._callback = callback,
_minExpectedCalls = minExpected,
- _maxExpectedCalls = (maxExpected == 0 && minExpected > 0)
- ? minExpected
- : maxExpected,
+ _maxExpectedCalls =
+ (maxExpected == 0 && minExpected > 0) ? minExpected : maxExpected,
this._isDone = isDone,
this._reason = reason == null ? '' : '\n$reason',
this._zone = Zone.current,
@@ -143,27 +142,41 @@ class _ExpectedFunction {
// This indirection is critical. It ensures the returned function has an
// argument count of zero.
- _max0() => _max6();
+ T _max0() => _max6();
- _max1([a0 = _PLACEHOLDER]) => _max6(a0);
+ T _max1([a0 = _PLACEHOLDER]) => _max6(a0);
- _max2([a0 = _PLACEHOLDER, a1 = _PLACEHOLDER]) => _max6(a0, a1);
+ T _max2([a0 = _PLACEHOLDER, a1 = _PLACEHOLDER]) => _max6(a0, a1);
- _max3([a0 = _PLACEHOLDER, a1 = _PLACEHOLDER, a2 = _PLACEHOLDER]) =>
+ T _max3([a0 = _PLACEHOLDER, a1 = _PLACEHOLDER, a2 = _PLACEHOLDER]) =>
_max6(a0, a1, a2);
- _max4([a0 = _PLACEHOLDER, a1 = _PLACEHOLDER, a2 = _PLACEHOLDER,
- a3 = _PLACEHOLDER]) => _max6(a0, a1, a2, a3);
-
- _max5([a0 = _PLACEHOLDER, a1 = _PLACEHOLDER, a2 = _PLACEHOLDER,
- a3 = _PLACEHOLDER, a4 = _PLACEHOLDER]) => _max6(a0, a1, a2, a3, a4);
-
- _max6([a0 = _PLACEHOLDER, a1 = _PLACEHOLDER, a2 = _PLACEHOLDER,
- a3 = _PLACEHOLDER, a4 = _PLACEHOLDER, a5 = _PLACEHOLDER]) =>
+ T _max4(
+ [a0 = _PLACEHOLDER,
+ a1 = _PLACEHOLDER,
+ a2 = _PLACEHOLDER,
+ a3 = _PLACEHOLDER]) =>
+ _max6(a0, a1, a2, a3);
+
+ T _max5(
+ [a0 = _PLACEHOLDER,
+ a1 = _PLACEHOLDER,
+ a2 = _PLACEHOLDER,
+ a3 = _PLACEHOLDER,
+ a4 = _PLACEHOLDER]) =>
+ _max6(a0, a1, a2, a3, a4);
+
+ T _max6(
+ [a0 = _PLACEHOLDER,
+ a1 = _PLACEHOLDER,
+ a2 = _PLACEHOLDER,
+ a3 = _PLACEHOLDER,
+ a4 = _PLACEHOLDER,
+ a5 = _PLACEHOLDER]) =>
_run([a0, a1, a2, a3, a4, a5].where((a) => a != _PLACEHOLDER));
/// Runs the wrapped function with [args] and returns its return value.
- _run(Iterable args) {
+ T _run(Iterable args) {
// Note that in the old test, this returned `null` if it encountered an
// error, where now it just re-throws that error because Zone machinery will
// pass it to the invoker anyway.
@@ -171,13 +184,13 @@ class _ExpectedFunction {
_actualCalls++;
if (_invoker.liveTest.state.shouldBeDone) {
throw 'Callback ${_id}called ($_actualCalls) after test case '
- '${_invoker.liveTest.test.name} had already completed.$_reason';
+ '${_invoker.liveTest.test.name} had already completed.$_reason';
} else if (_maxExpectedCalls >= 0 && _actualCalls > _maxExpectedCalls) {
throw new TestFailure('Callback ${_id}called more times than expected '
- '($_maxExpectedCalls).$_reason');
+ '($_maxExpectedCalls).$_reason');
}
- return Function.apply(_callback, args.toList());
+ return Function.apply(_callback, args.toList()) as T;
} catch (error, stackTrace) {
_zone.handleUncaughtError(error, stackTrace);
return null;
@@ -202,6 +215,10 @@ class _ExpectedFunction {
/// Indicate that [callback] is expected to be called [count] number of times
/// (by default 1).
///
+/// This function is deprecated. Use [expectAsync0], [expectAsync1],
+/// [expectAsync2], [expectAsync3], [expectAsync4], [expectAsync5], or
+/// [expectAsync6] instead.
+///
/// The test framework will wait for the callback to run the [count] times
/// before it considers the current test to be complete. [callback] may take up
/// to six optional or required positional arguments; named arguments are not
@@ -215,8 +232,9 @@ class _ExpectedFunction {
/// Both [id] and [reason] are optional and provide extra information about the
/// callback when debugging. [id] should be the name of the callback, while
/// [reason] should be the reason the callback is expected to be called.
+@deprecated
Function expectAsync(Function callback,
- {int count: 1, int max: 0, String id, String reason}) {
+ {int count: 1, int max: 0, String id, String reason}) {
if (Invoker.current == null) {
throw new StateError("expectAsync() may only be called within a test.");
}
@@ -225,9 +243,159 @@ Function expectAsync(Function callback,
.func;
}
+typedef T F0<T>();
+typedef T F1<T, P0>(P0 p0);
+typedef T F2<T, P0, P1>(P0 p0, P1 p1);
+typedef T F3<T, P0, P1, P2>(P0 p0, P1 p1, P2 p2);
+typedef T F4<T, P0, P1, P2, P3>(P0 p0, P1 p1, P2 p2, P3 p3);
+typedef T F5<T, P0, P1, P2, P3, P4>(P0 p0, P1 p1, P2 p2, P3 p3, P4 p4);
+typedef T F6<T, P0, P1, P2, P3, P4, P5>(
+ P0 p0, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5);
+
+/// Indicate that [callback] is expected to be called [count] number of times
+/// (by default 1).
+///
+/// The test framework will wait for the callback to run the [count] times
+/// before it considers the current test to be complete.
+///
+/// [max] can be used to specify an upper bound on the number of calls; if this
+/// is exceeded the test will fail. If [max] is `0` (the default), the callback
+/// is expected to be called exactly [count] times. If [max] is `-1`, the
+/// callback is allowed to be called any number of times greater than [count].
+///
+/// Both [id] and [reason] are optional and provide extra information about the
+/// callback when debugging. [id] should be the name of the callback, while
+/// [reason] should be the reason the callback is expected to be called.
+F0<dynamic/*=T*/ > expectAsync0/*<T>*/(dynamic/*=T*/ callback(),
+ {int count: 1, int max: 0, String id, String reason}) {
+ if (Invoker.current == null) {
+ throw new StateError("expectAsync() may only be called within a test.");
+ }
+
+ return new _ExpectedFunction/*<T>*/(callback, count, max,
+ id: id, reason: reason)
+ ._max0;
+}
+
+/// See [expectAsync0].
+F1<dynamic/*=T*/, dynamic/*=P0*/ > expectAsync1/*<T, P0>*/(
+ dynamic/*=T*/ callback(dynamic/*=P0*/ p0),
+ {int count: 1,
+ int max: 0,
+ String id,
+ String reason}) {
+ if (Invoker.current == null) {
+ throw new StateError("expectAsync() may only be called within a test.");
+ }
+
+ return new _ExpectedFunction/*<T>*/(callback, count, max,
+ id: id, reason: reason)
+ ._max1;
+}
+
+/// See [expectAsync0].
+F2<dynamic/*=T*/, dynamic/*=P0*/, dynamic/*=P1*/ > expectAsync2/*<T, P0, P1>*/(
+ dynamic/*=T*/ callback(dynamic/*=P0*/ p0, dynamic/*=P1*/ p1),
+ {int count: 1,
+ int max: 0,
+ String id,
+ String reason}) {
+ if (Invoker.current == null) {
+ throw new StateError("expectAsync() may only be called within a test.");
+ }
+
+ return new _ExpectedFunction/*<T>*/(callback, count, max,
+ id: id, reason: reason)
+ ._max2;
+}
+
+/// See [expectAsync0].
+F3<dynamic/*=T*/, dynamic/*=P0*/, dynamic/*=P1*/, dynamic/*=P2*/ >
+ expectAsync3/*<T, P0, P1, P2>*/(
+ dynamic/*=T*/ callback(
+ dynamic/*=P0*/ p0, dynamic/*=P1*/ p1, dynamic/*=P2*/ p2),
+ {int count: 1,
+ int max: 0,
+ String id,
+ String reason}) {
+ if (Invoker.current == null) {
+ throw new StateError("expectAsync() may only be called within a test.");
+ }
+
+ return new _ExpectedFunction/*<T>*/(callback, count, max,
+ id: id, reason: reason)
+ ._max3;
+}
+
+/// See [expectAsync0].
+F4<dynamic/*=T*/, dynamic/*=P0*/, dynamic/*=P1*/, dynamic/*=P2*/,
+ dynamic/*=P3*/ >
+ expectAsync4/*<T, P0, P1, P2, P3>*/(
+ dynamic/*=T*/ callback(dynamic/*=P0*/ p0, dynamic/*=P1*/ p1,
+ dynamic/*=P2*/ p2, dynamic/*=P3*/ p3),
+ {int count: 1,
+ int max: 0,
+ String id,
+ String reason}) {
+ if (Invoker.current == null) {
+ throw new StateError("expectAsync() may only be called within a test.");
+ }
+
+ return new _ExpectedFunction/*<T>*/(callback, count, max,
+ id: id, reason: reason)
+ ._max4;
+}
+
+/// See [expectAsync0].
+F5<dynamic/*=T*/, dynamic/*=P0*/, dynamic/*=P1*/, dynamic/*=P2*/,
+ dynamic/*=P3*/, dynamic/*=P4*/ >
+ expectAsync5/*<T, P0, P1, P2, P3, P4>*/(
+ dynamic/*=T*/ callback(dynamic/*=P0*/ p0, dynamic/*=P1*/ p1,
+ dynamic/*=P2*/ p2, dynamic/*=P3*/ p3, dynamic/*=P4*/ p4),
+ {int count: 1,
+ int max: 0,
+ String id,
+ String reason}) {
+ if (Invoker.current == null) {
+ throw new StateError("expectAsync() may only be called within a test.");
+ }
+
+ return new _ExpectedFunction/*<T>*/(callback, count, max,
+ id: id, reason: reason)
+ ._max5;
+}
+
+/// See [expectAsync0].
+F6<dynamic/*=T*/, dynamic/*=P0*/, dynamic/*=P1*/, dynamic/*=P2*/,
+ dynamic/*=P3*/, dynamic/*=P4*/, dynamic/*=P5*/ >
+ expectAsync6/*<T, P0, P1, P2, P3, P4, P5>*/(
+ dynamic/*=T*/ callback(
+ dynamic/*=P0*/ p0,
+ dynamic/*=P1*/ p1,
+ dynamic/*=P2*/ p2,
+ dynamic/*=P3*/ p3,
+ dynamic/*=P4*/ p4,
+ dynamic/*=P5*/ p5),
+ {int count: 1,
+ int max: 0,
+ String id,
+ String reason}) {
+ if (Invoker.current == null) {
+ throw new StateError("expectAsync() may only be called within a test.");
+ }
+
+ return new _ExpectedFunction/*<T>*/(callback, count, max,
+ id: id, reason: reason)
+ ._max6;
+}
+
/// Indicate that [callback] is expected to be called until [isDone] returns
/// true.
///
+/// Deprecated. Use [expectAsyncUntil0], [expectAsyncUntil1],
+/// [expectAsyncUntil2], [expectAsyncUntil3], [expectAsyncUntil4],
+/// [expectAsyncUntil5], or [expectAsyncUntil6] instead.
+///
/// [isDone] is called after each time the function is run. Only when it returns
/// true will the callback be considered complete. [callback] may take up to six
/// optional or required positional arguments; named arguments are not
@@ -236,6 +404,7 @@ Function expectAsync(Function callback,
/// Both [id] and [reason] are optional and provide extra information about the
/// callback when debugging. [id] should be the name of the callback, while
/// [reason] should be the reason the callback is expected to be called.
+@deprecated
Function expectAsyncUntil(Function callback, bool isDone(),
{String id, String reason}) {
if (Invoker.current == null) {
@@ -244,5 +413,139 @@ Function expectAsyncUntil(Function callback, bool isDone(),
}
return new _ExpectedFunction(callback, 0, -1,
- id: id, reason: reason, isDone: isDone).func;
+ id: id, reason: reason, isDone: isDone)
+ .func;
+}
+
+/// Indicate that [callback] is expected to be called until [isDone] returns
+/// true.
+///
+/// [isDone] is called after each time the function is run. Only when it returns
+/// true will the callback be considered complete.
+///
+/// Both [id] and [reason] are optional and provide extra information about the
+/// callback when debugging. [id] should be the name of the callback, while
+/// [reason] should be the reason the callback is expected to be called.
+F0<dynamic/*=T*/ > expectAsyncUntil0/*<T>*/(
+ dynamic/*=T*/ callback(), bool isDone(),
+ {String id, String reason}) {
+ if (Invoker.current == null) {
+ throw new StateError(
+ "expectAsyncUntil() may only be called within a test.");
+ }
+
+ return new _ExpectedFunction/*<T>*/(callback, 0, -1,
+ id: id, reason: reason, isDone: isDone)
+ ._max0;
+}
+
+/// See [expectAsyncUntil0].
+F1<dynamic/*=T*/, dynamic/*=P0*/ > expectAsyncUntil1/*<T, P0>*/(
+ dynamic/*=T*/ callback(dynamic/*=P0*/ p0), bool isDone(),
+ {String id, String reason}) {
+ if (Invoker.current == null) {
+ throw new StateError(
+ "expectAsyncUntil() may only be called within a test.");
+ }
+
+ return new _ExpectedFunction/*<T>*/(callback, 0, -1,
+ id: id, reason: reason, isDone: isDone)
+ ._max1;
+}
+
+/// See [expectAsyncUntil0].
+F2<dynamic/*=T*/, dynamic/*=P0*/, dynamic/*=P1*/ >
+ expectAsyncUntil2/*<T, P0, P1>*/(
+ dynamic/*=T*/ callback(dynamic/*=P0*/ p0, dynamic/*=P1*/ p1),
+ bool isDone(),
+ {String id,
+ String reason}) {
+ if (Invoker.current == null) {
+ throw new StateError(
+ "expectAsyncUntil() may only be called within a test.");
+ }
+
+ return new _ExpectedFunction/*<T>*/(callback, 0, -1,
+ id: id, reason: reason, isDone: isDone)
+ ._max2;
+}
+
+/// See [expectAsyncUntil0].
+F3<dynamic/*=T*/, dynamic/*=P0*/, dynamic/*=P1*/, dynamic/*=P2*/ >
+ expectAsyncUntil3/*<T, P0, P1, P2>*/(
+ dynamic/*=T*/ callback(
+ dynamic/*=P0*/ p0, dynamic/*=P1*/ p1, dynamic/*=P2*/ p2),
+ bool isDone(),
+ {String id,
+ String reason}) {
+ if (Invoker.current == null) {
+ throw new StateError(
+ "expectAsyncUntil() may only be called within a test.");
+ }
+
+ return new _ExpectedFunction/*<T>*/(callback, 0, -1,
+ id: id, reason: reason, isDone: isDone)
+ ._max3;
+}
+
+/// See [expectAsyncUntil0].
+F4<dynamic/*=T*/, dynamic/*=P0*/, dynamic/*=P1*/, dynamic/*=P2*/,
+ dynamic/*=P3*/ >
+ expectAsyncUntil4/*<T, P0, P1, P2, P3>*/(
+ dynamic/*=T*/ callback(dynamic/*=P0*/ p0, dynamic/*=P1*/ p1,
+ dynamic/*=P2*/ p2, dynamic/*=P3*/ p3),
+ bool isDone(),
+ {String id,
+ String reason}) {
+ if (Invoker.current == null) {
+ throw new StateError(
+ "expectAsyncUntil() may only be called within a test.");
+ }
+
+ return new _ExpectedFunction/*<T>*/(callback, 0, -1,
+ id: id, reason: reason, isDone: isDone)
+ ._max4;
+}
+
+/// See [expectAsyncUntil0].
+F5<dynamic/*=T*/, dynamic/*=P0*/, dynamic/*=P1*/, dynamic/*=P2*/,
+ dynamic/*=P3*/, dynamic/*=P4*/ >
+ expectAsyncUntil5/*<T, P0, P1, P2, P3, P4>*/(
+ dynamic/*=T*/ callback(dynamic/*=P0*/ p0, dynamic/*=P1*/ p1,
+ dynamic/*=P2*/ p2, dynamic/*=P3*/ p3, dynamic/*=P4*/ p4),
+ bool isDone(),
+ {String id,
+ String reason}) {
+ if (Invoker.current == null) {
+ throw new StateError(
+ "expectAsyncUntil() may only be called within a test.");
+ }
+
+ return new _ExpectedFunction/*<T>*/(callback, 0, -1,
+ id: id, reason: reason, isDone: isDone)
+ ._max5;
+}
+
+/// See [expectAsyncUntil0].
+F6<dynamic/*=T*/, dynamic/*=P0*/, dynamic/*=P1*/, dynamic/*=P2*/,
+ dynamic/*=P3*/, dynamic/*=P4*/, dynamic/*=P5*/ >
+ expectAsyncUntil6/*<T, P0, P1, P2, P3, P4, P5>*/(
+ dynamic/*=T*/ callback(
+ dynamic/*=P0*/ p0,
+ dynamic/*=P1*/ p1,
+ dynamic/*=P2*/ p2,
+ dynamic/*=P3*/ p3,
+ dynamic/*=P4*/ p4,
+ dynamic/*=P5*/ p5),
+ bool isDone(),
+ {String id,
+ String reason}) {
+ if (Invoker.current == null) {
+ throw new StateError(
+ "expectAsyncUntil() may only be called within a test.");
+ }
+
+ return new _ExpectedFunction/*<T>*/(callback, 0, -1,
+ id: id, reason: reason, isDone: isDone)
+ ._max6;
}
« no previous file with comments | « README.md ('k') | pubspec.yaml » ('j') | pubspec.yaml » ('J')

Powered by Google App Engine
This is Rietveld 408576698