Skip to content

Commit cebcb3d

Browse files
committed
Make expect* throw useful errors outside tests.
Closes #132 [email protected] Review URL: https://codereview.chromium.org//1148723005
1 parent 028c4e1 commit cebcb3d

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
* Add a heartbeat to reset a test's timeout whenever the test interacts with the
1212
test infrastructure.
1313

14+
* `expect()`, `expectAsync()`, and `expectAsyncUntil()` throw more useful errors
15+
if called outside a test body.
16+
1417
## 0.12.2
1518

1619
* Convert JavaScript stack traces into Dart stack traces using source maps. This

lib/src/frontend/expect.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ typedef String ErrorFormatter(
3939
/// [verbose] should be specified as `true`.
4040
void expect(actual, matcher,
4141
{String reason, bool verbose: false, ErrorFormatter formatter}) {
42+
if (Invoker.current == null) {
43+
throw new StateError("extend() may only be called within a test.");
44+
}
45+
4246
if (Invoker.current.closed) throw new ClosedException();
4347

4448
matcher = wrapMatcher(matcher);

lib/src/frontend/expect_async.dart

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,14 @@ class _ExpectedFunction {
220220
/// callback when debugging. [id] should be the name of the callback, while
221221
/// [reason] should be the reason the callback is expected to be called.
222222
Function expectAsync(Function callback,
223-
{int count: 1, int max: 0, String id, String reason}) =>
224-
new _ExpectedFunction(callback, count, max, id: id, reason: reason).func;
223+
{int count: 1, int max: 0, String id, String reason}) {
224+
if (Invoker.current == null) {
225+
throw new StateError("expectAsync() may only be called within a test.");
226+
}
227+
228+
return new _ExpectedFunction(callback, count, max, id: id, reason: reason)
229+
.func;
230+
}
225231

226232
/// Indicate that [callback] is expected to be called until [isDone] returns
227233
/// true.
@@ -235,5 +241,12 @@ Function expectAsync(Function callback,
235241
/// callback when debugging. [id] should be the name of the callback, while
236242
/// [reason] should be the reason the callback is expected to be called.
237243
Function expectAsyncUntil(Function callback, bool isDone(),
238-
{String id, String reason}) => new _ExpectedFunction(callback, 0, -1,
239-
id: id, reason: reason, isDone: isDone).func;
244+
{String id, String reason}) {
245+
if (Invoker.current == null) {
246+
throw new StateError(
247+
"expectAsyncUntil() may only be called within a test.");
248+
}
249+
250+
return new _ExpectedFunction(callback, 0, -1,
251+
id: id, reason: reason, isDone: isDone).func;
252+
}

0 commit comments

Comments
 (0)