| Index: test/utils.dart
|
| diff --git a/test/utils.dart b/test/utils.dart
|
| index 17caceade76d789046cb438d4b71d0700521b302..f9b4075d70df066393024421491889b9a0561c3b 100644
|
| --- a/test/utils.dart
|
| +++ b/test/utils.dart
|
| @@ -5,14 +5,72 @@
|
| library unittest.test.utils;
|
|
|
| import 'dart:async';
|
| +import 'dart:collection';
|
|
|
| +import 'package:unittest/src/live_test.dart';
|
| +import 'package:unittest/src/remote_exception.dart';
|
| +import 'package:unittest/src/state.dart';
|
| import 'package:unittest/unittest.dart';
|
|
|
| +// The last state change detected via [expectStates].
|
| +State lastState;
|
| +
|
| +/// Asserts that exactly [states] will be emitted via [liveTest.onStateChange].
|
| +///
|
| +/// The most recent emitted state is stored in [_lastState].
|
| +void expectStates(LiveTest liveTest, Iterable<State> statesIter) {
|
| + var states = new Queue.from(statesIter);
|
| + liveTest.onStateChange.listen(expectAsync((state) {
|
| + lastState = state;
|
| + expect(state, equals(states.removeFirst()));
|
| + }, count: states.length, max: states.length));
|
| +}
|
| +
|
| +/// Asserts that errors will be emitted via [liveTest.onError] that match
|
| +/// [validators], in order.
|
| +void expectErrors(LiveTest liveTest, Iterable<Function> validatorsIter) {
|
| + var validators = new Queue.from(validatorsIter);
|
| + liveTest.onError.listen(expectAsync((error) {
|
| + validators.removeFirst()(error.error);
|
| + }, count: validators.length, max: validators.length));
|
| +}
|
| +
|
| +/// Asserts that [liveTest] will have a single failure with message `"oh no"`.
|
| +void expectSingleFailure(LiveTest liveTest) {
|
| + expectStates(liveTest, [
|
| + const State(Status.running, Result.success),
|
| + const State(Status.complete, Result.failure)
|
| + ]);
|
| +
|
| + expectErrors(liveTest, [(error) {
|
| + expect(lastState.status, equals(Status.complete));
|
| + expect(error, isTestFailure("oh no"));
|
| + }]);
|
| +}
|
| +
|
| +/// Asserts that [liveTest] will have a single error, the string `"oh no"`.
|
| +void expectSingleError(LiveTest liveTest) {
|
| + expectStates(liveTest, [
|
| + const State(Status.running, Result.success),
|
| + const State(Status.complete, Result.error)
|
| + ]);
|
| +
|
| + expectErrors(liveTest, [(error) {
|
| + expect(lastState.status, equals(Status.complete));
|
| + expect(error, equals("oh no"));
|
| + }]);
|
| +}
|
| +
|
| /// Returns a matcher that matches a [TestFailure] with the given [message].
|
| Matcher isTestFailure(String message) => predicate(
|
| (error) => error is TestFailure && error.message == message,
|
| 'is a TestFailure with message "$message"');
|
|
|
| +/// Returns a matcher that matches a [RemoteException] with the given [message].
|
| +Matcher isRemoteException(String message) => predicate(
|
| + (error) => error is RemoteException && error.message == message,
|
| + 'is a RemoteException with message "$message"');
|
| +
|
| /// Returns a [Future] that completes after pumping the event queue [times]
|
| /// times.
|
| ///
|
|
|