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 |
| 17 /// An implementation of [LiveTest] that's controlled by a [LiveTestController]. |
| 18 class _LiveTest extends LiveTest { |
| 19 final LiveTestController _controller; |
| 20 |
| 21 Suite get suite => _controller._suite; |
| 22 |
| 23 Test get test => _controller._test; |
| 24 |
| 25 State get state => _controller._state; |
| 26 |
| 27 Stream<State> get onStateChange => |
| 28 _controller._onStateChangeController.stream; |
| 29 |
| 30 List<AsyncError> get errors => new UnmodifiableListView(_controller._errors); |
| 31 |
| 32 Stream<AsyncError> get onError => _controller._onErrorController.stream; |
| 33 |
| 34 Future get onComplete => _controller.completer.future; |
| 35 |
| 36 Future run() => _controller._run(); |
| 37 |
| 38 _LiveTest(this._controller); |
| 39 } |
| 40 |
| 41 /// A controller that drives a [LiveTest]. |
| 42 /// |
| 43 /// This is a utility class to make it easier for implementors of [Test] to |
| 44 /// create the [LiveTest] returned by [Test.load]. The [LiveTest] is accessible |
| 45 /// through [LiveTestController.liveTest]. |
| 46 /// |
| 47 /// This automatically handles some of [LiveTest]'s guarantees, but for the most |
| 48 /// part it's the caller's responsibility to make sure everything gets |
| 49 /// dispatched in the correct order. |
| 50 class LiveTestController { |
| 51 /// The [LiveTest] controlled by [this]. |
| 52 LiveTest get liveTest => _liveTest; |
| 53 LiveTest _liveTest; |
| 54 |
| 55 /// The test suite that's running [this]. |
| 56 final Suite _suite; |
| 57 |
| 58 /// The test that's being run. |
| 59 final Test _test; |
| 60 |
| 61 /// The function that will actually start the test running. |
| 62 final Function _onRun; |
| 63 |
| 64 /// The list of errors caught by the test. |
| 65 final _errors = new List<AsyncError>(); |
| 66 |
| 67 /// The current state of the test. |
| 68 var _state = const State(Status.pending, Result.success); |
| 69 |
| 70 /// The controller for [LiveTest.onStateChange]. |
| 71 final _onStateChangeController = new StreamController<State>.broadcast(); |
| 72 |
| 73 /// The controller for [LiveTest.onError]. |
| 74 final _onErrorController = new StreamController<AsyncError>.broadcast(); |
| 75 |
| 76 /// The completer for [LiveTest.onComplete]; |
| 77 final completer = new Completer(); |
| 78 |
| 79 /// Whether [run] has been called. |
| 80 var _runCalled = false; |
| 81 |
| 82 /// Creates a new controller for a [LiveTest]. |
| 83 /// |
| 84 /// [test] is the test being run; [suite] is the suite that contains it. |
| 85 /// |
| 86 /// [onRun] is a function that will be called from [LiveTest.run]. It should |
| 87 /// start the test running. The controller takes care of ensuring that |
| 88 /// [LiveTest.run] isn't called more than once and that [LiveTest.onComplete] |
| 89 /// is returned. |
| 90 LiveTestController(this._suite, this._test, void onRun()) |
| 91 : _onRun = onRun { |
| 92 _liveTest = new _LiveTest(this); |
| 93 } |
| 94 |
| 95 /// Adds an error to the [LiveTest]. |
| 96 /// |
| 97 /// This both adds the error to [LiveTest.errors] and emits it via |
| 98 /// [LiveTest.onError]. [stackTrace] is automatically converted into a [Chain] |
| 99 /// if it's not one already. |
| 100 void addError(error, StackTrace stackTrace) { |
| 101 var asyncError = new AsyncError(error, new Chain.forTrace(stackTrace)); |
| 102 _errors.add(asyncError); |
| 103 _onErrorController.add(asyncError); |
| 104 } |
| 105 |
| 106 /// Sets the current state of the [LiveTest] to [newState]. |
| 107 /// |
| 108 /// If [newState] is different than the old state, this both sets |
| 109 /// [LiveTest.state] and emits the new state via [LiveTest.onStateChanged]. If |
| 110 /// it's not different, this does nothing. |
| 111 void setState(State newState) { |
| 112 if (_state == newState) return; |
| 113 _state = newState; |
| 114 _onStateChangeController.add(newState); |
| 115 } |
| 116 |
| 117 /// A wrapper for [_onRun] that ensures that it follows the guarantees for |
| 118 /// [LiveTest.run]. |
| 119 Future _run() { |
| 120 if (_runCalled) { |
| 121 throw new StateError("LiveTest.run() may not be called more than once."); |
| 122 } |
| 123 _runCalled = true; |
| 124 |
| 125 _onRun(); |
| 126 return liveTest.onComplete; |
| 127 } |
| 128 } |
OLD | NEW |