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 import 'dart:collection'; |
9 import 'dart:io'; | |
10 | |
11 import 'package:path/path.dart' as p; | |
12 import 'package:stack_trace/stack_trace.dart'; | |
13 | 9 |
14 import 'package:unittest/src/live_test.dart'; | 10 import 'package:unittest/src/live_test.dart'; |
15 import 'package:unittest/src/remote_exception.dart'; | 11 import 'package:unittest/src/remote_exception.dart'; |
16 import 'package:unittest/src/state.dart'; | 12 import 'package:unittest/src/state.dart'; |
17 import 'package:unittest/unittest.dart'; | 13 import 'package:unittest/unittest.dart'; |
18 | 14 |
19 // The last state change detected via [expectStates]. | 15 // The last state change detected via [expectStates]. |
20 State lastState; | 16 State lastState; |
21 | 17 |
22 final String packageDir = _computePackageDir(); | |
23 | |
24 String _computePackageDir() { | |
25 var trace = new Trace.current(); | |
26 return p.dirname(p.dirname(p.fromUri(trace.frames.first.uri))); | |
27 } | |
28 | |
29 /// Asserts that exactly [states] will be emitted via [liveTest.onStateChange]. | 18 /// Asserts that exactly [states] will be emitted via [liveTest.onStateChange]. |
30 /// | 19 /// |
31 /// The most recent emitted state is stored in [_lastState]. | 20 /// The most recent emitted state is stored in [_lastState]. |
32 void expectStates(LiveTest liveTest, Iterable<State> statesIter) { | 21 void expectStates(LiveTest liveTest, Iterable<State> statesIter) { |
33 var states = new Queue.from(statesIter); | 22 var states = new Queue.from(statesIter); |
34 liveTest.onStateChange.listen(expectAsync((state) { | 23 liveTest.onStateChange.listen(expectAsync((state) { |
35 lastState = state; | 24 lastState = state; |
36 expect(state, equals(states.removeFirst())); | 25 expect(state, equals(states.removeFirst())); |
37 }, count: states.length, max: states.length)); | 26 }, count: states.length, max: states.length)); |
38 } | 27 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 /// Returns a matcher that matches a [TestFailure] with the given [message]. | 64 /// Returns a matcher that matches a [TestFailure] with the given [message]. |
76 Matcher isTestFailure(String message) => predicate( | 65 Matcher isTestFailure(String message) => predicate( |
77 (error) => error is TestFailure && error.message == message, | 66 (error) => error is TestFailure && error.message == message, |
78 'is a TestFailure with message "$message"'); | 67 'is a TestFailure with message "$message"'); |
79 | 68 |
80 /// Returns a matcher that matches a [RemoteException] with the given [message]. | 69 /// Returns a matcher that matches a [RemoteException] with the given [message]. |
81 Matcher isRemoteException(String message) => predicate( | 70 Matcher isRemoteException(String message) => predicate( |
82 (error) => error is RemoteException && error.message == message, | 71 (error) => error is RemoteException && error.message == message, |
83 'is a RemoteException with message "$message"'); | 72 'is a RemoteException with message "$message"'); |
84 | 73 |
85 /// Returns a matcher that matches a [FileSystemException] with the given | |
86 /// [message]. | |
87 Matcher isFileSystemException(String message) => predicate( | |
88 (error) => error is FileSystemException && error.message == message, | |
89 'is a FileSystemException with message "$message"'); | |
90 | |
91 /// Returns a [Future] that completes after pumping the event queue [times] | 74 /// Returns a [Future] that completes after pumping the event queue [times] |
92 /// times. | 75 /// times. |
93 /// | 76 /// |
94 /// 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 |
95 /// 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. |
96 Future pumpEventQueue([int times=20]) { | 79 Future pumpEventQueue([int times=20]) { |
97 if (times == 0) return new Future.value(); | 80 if (times == 0) return new Future.value(); |
98 // 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 |
99 // Future.value] constructor uses scheduleMicrotask itself and would therefore | 82 // Future.value] constructor uses scheduleMicrotask itself and would therefore |
100 // not wait for microtask callbacks that are scheduled after invoking this | 83 // not wait for microtask callbacks that are scheduled after invoking this |
101 // method. | 84 // method. |
102 return new Future(() => pumpEventQueue(times - 1)); | 85 return new Future(() => pumpEventQueue(times - 1)); |
103 } | 86 } |
OLD | NEW |