OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library unittest.live_test_controller; | |
6 | |
7 import 'dart:async'; | |
8 import 'dart:collection'; | |
9 | |
10 import 'package:stack_trace/stack_trace.dart'; | |
11 | |
12 import 'live_test.dart'; | |
13 import 'state.dart'; | |
14 import 'suite.dart'; | |
15 import 'test.dart'; | |
16 import 'utils.dart'; | |
17 | |
18 /// An implementation of [LiveTest] that's controlled by a [LiveTestController]. | |
19 class _LiveTest extends LiveTest { | |
20 final LiveTestController _controller; | |
21 | |
22 Suite get suite => _controller._suite; | |
23 | |
24 Test get test => _controller._test; | |
25 | |
26 State get state => _controller._state; | |
27 | |
28 Stream<State> get onStateChange => | |
29 _controller._onStateChangeController.stream; | |
30 | |
31 List<AsyncError> get errors => new UnmodifiableListView(_controller._errors); | |
32 | |
33 Stream<AsyncError> get onError => _controller._onErrorController.stream; | |
34 | |
35 Future get onComplete => _controller.completer.future; | |
36 | |
37 Future run() => _controller._run(); | |
38 | |
39 Future close() => _controller._close(); | |
40 | |
41 _LiveTest(this._controller); | |
42 } | |
43 | |
44 /// A controller that drives a [LiveTest]. | |
45 /// | |
46 /// This is a utility class to make it easier for implementors of [Test] to | |
47 /// create the [LiveTest] returned by [Test.load]. The [LiveTest] is accessible | |
48 /// through [LiveTestController.liveTest]. | |
49 /// | |
50 /// This automatically handles some of [LiveTest]'s guarantees, but for the most | |
51 /// part it's the caller's responsibility to make sure everything gets | |
52 /// dispatched in the correct order. | |
53 class LiveTestController { | |
54 /// The [LiveTest] controlled by [this]. | |
55 LiveTest get liveTest => _liveTest; | |
56 LiveTest _liveTest; | |
57 | |
58 /// The test suite that's running [this]. | |
59 final Suite _suite; | |
60 | |
61 /// The test that's being run. | |
62 final Test _test; | |
63 | |
64 /// The function that will actually start the test running. | |
65 final Function _onRun; | |
66 | |
67 /// A function to run when the test is closed. | |
68 /// | |
69 /// This may be `null`. | |
70 final AsyncFunction _onClose; | |
71 | |
72 /// The list of errors caught by the test. | |
73 final _errors = new List<AsyncError>(); | |
74 | |
75 /// The current state of the test. | |
76 var _state = const State(Status.pending, Result.success); | |
77 | |
78 /// The controller for [LiveTest.onStateChange]. | |
79 final _onStateChangeController = new StreamController<State>.broadcast(); | |
80 | |
81 /// The controller for [LiveTest.onError]. | |
82 final _onErrorController = new StreamController<AsyncError>.broadcast(); | |
83 | |
84 /// The completer for [LiveTest.onComplete]; | |
85 final completer = new Completer(); | |
86 | |
87 /// Whether [run] has been called. | |
88 var _runCalled = false; | |
89 | |
90 /// Whether [close] has been called. | |
91 bool get _isClosed => _onErrorController.isClosed; | |
92 | |
93 /// Creates a new controller for a [LiveTest]. | |
94 /// | |
95 /// [test] is the test being run; [suite] is the suite that contains it. | |
96 /// | |
97 /// [onRun] is a function that will be called from [LiveTest.run]. It should | |
98 /// start the test running. The controller takes care of ensuring that | |
99 /// [LiveTest.run] isn't called more than once and that [LiveTest.onComplete] | |
100 /// is returned. | |
101 /// | |
102 /// If [onClose] is passed, it's called the first [LiveTest.close] is called. | |
103 /// It should clean up any resources that have been allocated for the test. It | |
104 /// may return a [Future]. | |
105 LiveTestController(this._suite, this._test, void onRun(), {onClose()}) | |
106 : _onRun = onRun, | |
107 _onClose = onClose { | |
108 _liveTest = new _LiveTest(this); | |
109 } | |
110 | |
111 /// Adds an error to the [LiveTest]. | |
112 /// | |
113 /// This both adds the error to [LiveTest.errors] and emits it via | |
114 /// [LiveTest.onError]. [stackTrace] is automatically converted into a [Chain] | |
115 /// if it's not one already. | |
116 void addError(error, StackTrace stackTrace) { | |
117 var asyncError = new AsyncError(error, new Chain.forTrace(stackTrace)); | |
118 _errors.add(asyncError); | |
119 _onErrorController.add(asyncError); | |
120 } | |
121 | |
122 /// Sets the current state of the [LiveTest] to [newState]. | |
123 /// | |
124 /// If [newState] is different than the old state, this both sets | |
125 /// [LiveTest.state] and emits the new state via [LiveTest.onStateChanged]. If | |
126 /// it's not different, this does nothing. | |
127 void setState(State newState) { | |
128 if (_state == newState) return; | |
129 _state = newState; | |
130 _onStateChangeController.add(newState); | |
131 } | |
132 | |
133 /// A wrapper for [_onRun] that ensures that it follows the guarantees for | |
134 /// [LiveTest.run]. | |
135 Future _run() { | |
136 if (_runCalled) { | |
137 throw new StateError("LiveTest.run() may not be called more than once."); | |
138 } | |
139 _runCalled = true; | |
140 | |
141 _onRun(); | |
142 return liveTest.onComplete; | |
143 } | |
144 | |
145 /// A wrapper for [_onClose] that ensures that all controllers are closed. | |
146 Future _close() { | |
147 if (_isClosed) return new Future.value(); | |
148 _onStateChangeController.close(); | |
149 _onErrorController.close(); | |
150 if (!completer.isCompleted) completer.complete(); | |
151 | |
152 if (_onClose != null) return new Future.sync(_onClose); | |
153 return new Future.value(); | |
154 } | |
155 } | |
OLD | NEW |