Skip to content

Commit

Permalink
Make expect* throw useful errors outside tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
nex3 committed Jun 1, 2015
1 parent 028c4e1 commit cebcb3d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
* Add a heartbeat to reset a test's timeout whenever the test interacts with the
test infrastructure.

* `expect()`, `expectAsync()`, and `expectAsyncUntil()` throw more useful errors
if called outside a test body.

## 0.12.2

* Convert JavaScript stack traces into Dart stack traces using source maps. This
Expand Down
4 changes: 4 additions & 0 deletions lib/src/frontend/expect.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ typedef String ErrorFormatter(
/// [verbose] should be specified as `true`.
void expect(actual, matcher,
{String reason, bool verbose: false, ErrorFormatter formatter}) {
if (Invoker.current == null) {
throw new StateError("extend() may only be called within a test.");
}

if (Invoker.current.closed) throw new ClosedException();

matcher = wrapMatcher(matcher);
Expand Down
21 changes: 17 additions & 4 deletions lib/src/frontend/expect_async.dart
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,14 @@ class _ExpectedFunction {
/// callback when debugging. [id] should be the name of the callback, while
/// [reason] should be the reason the callback is expected to be called.
Function expectAsync(Function callback,
{int count: 1, int max: 0, String id, String reason}) =>
new _ExpectedFunction(callback, count, max, id: id, reason: reason).func;
{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(callback, count, max, id: id, reason: reason)
.func;
}

/// Indicate that [callback] is expected to be called until [isDone] returns
/// true.
Expand All @@ -235,5 +241,12 @@ Function expectAsync(Function callback,
/// callback when debugging. [id] should be the name of the callback, while
/// [reason] should be the reason the callback is expected to be called.
Function expectAsyncUntil(Function callback, bool isDone(),
{String id, String reason}) => new _ExpectedFunction(callback, 0, -1,
id: id, reason: reason, isDone: isDone).func;
{String id, String reason}) {
if (Invoker.current == null) {
throw new StateError(
"expectAsyncUntil() may only be called within a test.");
}

return new _ExpectedFunction(callback, 0, -1,
id: id, reason: reason, isDone: isDone).func;
}

0 comments on commit cebcb3d

Please sign in to comment.