OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library unittest.test.utils; | 5 library unittest.test.utils; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:collection'; |
8 | 9 |
| 10 import 'package:unittest/src/live_test.dart'; |
| 11 import 'package:unittest/src/remote_exception.dart'; |
| 12 import 'package:unittest/src/state.dart'; |
9 import 'package:unittest/unittest.dart'; | 13 import 'package:unittest/unittest.dart'; |
10 | 14 |
| 15 // The last state change detected via [expectStates]. |
| 16 State lastState; |
| 17 |
| 18 /// Asserts that exactly [states] will be emitted via [liveTest.onStateChange]. |
| 19 /// |
| 20 /// The most recent emitted state is stored in [_lastState]. |
| 21 void expectStates(LiveTest liveTest, Iterable<State> statesIter) { |
| 22 var states = new Queue.from(statesIter); |
| 23 liveTest.onStateChange.listen(expectAsync((state) { |
| 24 lastState = state; |
| 25 expect(state, equals(states.removeFirst())); |
| 26 }, count: states.length, max: states.length)); |
| 27 } |
| 28 |
| 29 /// Asserts that errors will be emitted via [liveTest.onError] that match |
| 30 /// [validators], in order. |
| 31 void expectErrors(LiveTest liveTest, Iterable<Function> validatorsIter) { |
| 32 var validators = new Queue.from(validatorsIter); |
| 33 liveTest.onError.listen(expectAsync((error) { |
| 34 validators.removeFirst()(error.error); |
| 35 }, count: validators.length, max: validators.length)); |
| 36 } |
| 37 |
| 38 /// Asserts that [liveTest] will have a single failure with message `"oh no"`. |
| 39 void expectSingleFailure(LiveTest liveTest) { |
| 40 expectStates(liveTest, [ |
| 41 const State(Status.running, Result.success), |
| 42 const State(Status.complete, Result.failure) |
| 43 ]); |
| 44 |
| 45 expectErrors(liveTest, [(error) { |
| 46 expect(lastState.status, equals(Status.complete)); |
| 47 expect(error, isTestFailure("oh no")); |
| 48 }]); |
| 49 } |
| 50 |
| 51 /// Asserts that [liveTest] will have a single error, the string `"oh no"`. |
| 52 void expectSingleError(LiveTest liveTest) { |
| 53 expectStates(liveTest, [ |
| 54 const State(Status.running, Result.success), |
| 55 const State(Status.complete, Result.error) |
| 56 ]); |
| 57 |
| 58 expectErrors(liveTest, [(error) { |
| 59 expect(lastState.status, equals(Status.complete)); |
| 60 expect(error, equals("oh no")); |
| 61 }]); |
| 62 } |
| 63 |
11 /// Returns a matcher that matches a [TestFailure] with the given [message]. | 64 /// Returns a matcher that matches a [TestFailure] with the given [message]. |
12 Matcher isTestFailure(String message) => predicate( | 65 Matcher isTestFailure(String message) => predicate( |
13 (error) => error is TestFailure && error.message == message, | 66 (error) => error is TestFailure && error.message == message, |
14 'is a TestFailure with message "$message"'); | 67 'is a TestFailure with message "$message"'); |
15 | 68 |
| 69 /// Returns a matcher that matches a [RemoteException] with the given [message]. |
| 70 Matcher isRemoteException(String message) => predicate( |
| 71 (error) => error is RemoteException && error.message == message, |
| 72 'is a RemoteException with message "$message"'); |
| 73 |
16 /// Returns a [Future] that completes after pumping the event queue [times] | 74 /// Returns a [Future] that completes after pumping the event queue [times] |
17 /// times. | 75 /// times. |
18 /// | 76 /// |
19 /// By default, this should pump the event queue enough times to allow any code | 77 /// By default, this should pump the event queue enough times to allow any code |
20 /// to run, as long as it's not waiting on some external event. | 78 /// to run, as long as it's not waiting on some external event. |
21 Future pumpEventQueue([int times=20]) { | 79 Future pumpEventQueue([int times=20]) { |
22 if (times == 0) return new Future.value(); | 80 if (times == 0) return new Future.value(); |
23 // Use [new Future] future to allow microtask events to finish. The [new | 81 // Use [new Future] future to allow microtask events to finish. The [new |
24 // Future.value] constructor uses scheduleMicrotask itself and would therefore | 82 // Future.value] constructor uses scheduleMicrotask itself and would therefore |
25 // not wait for microtask callbacks that are scheduled after invoking this | 83 // not wait for microtask callbacks that are scheduled after invoking this |
26 // method. | 84 // method. |
27 return new Future(() => pumpEventQueue(times - 1)); | 85 return new Future(() => pumpEventQueue(times - 1)); |
28 } | 86 } |
OLD | NEW |