Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(176)

Side by Side Diff: lib/src/live_test_controller.dart

Issue 877553009: Add some basic interfaces that will be used for the test runner. (Closed) Base URL: git@github.com:dart-lang/unittest@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 'live_test.dart';
11 import 'state.dart';
12 import 'suite.dart';
13 import 'test.dart';
14
15 /// An implementation of [LiveTest] that's controlled by a [LiveTestController].
16 class _LiveTest extends LiveTest {
17 final LiveTestController _controller;
18
19 Suite get suite => _controller._suite;
20
21 Test get test => _controller._test;
22
23 State get state => _controller._state;
24
25 Stream<State> get onStateChange =>
26 _controller._onStateChangeController.stream;
27
28 List<AsyncError> get errors => new UnmodifiableListView(_controller._errors);
29
30 Stream<AsyncError> get onError => _controller._onErrorController.stream;
31
32 Future get onComplete => _controller.completer.future;
33
34 Future run() => _controller._run();
35
36 _LiveTest(this._controller);
37 }
38
39 /// A controller that drives a [LiveTest].
40 ///
41 /// This is a utility class to make it easier for implementors of [Test] to
42 /// create the [LiveTest] returned by [Test.load]. The [LiveTest] is accessible
43 /// through [LiveTestController.liveTest].
44 ///
45 /// This automatically handles some of [LiveTest]'s guarantees, but for the most
46 /// part it's the caller's responsibility to make sure everything gets
47 /// dispatched in the correct order.
48 class LiveTestController {
49 /// The [LiveTest] controlled by [this].
50 LiveTest get liveTest => _liveTest;
51 LiveTest _liveTest;
52
53 /// The test suite that's running [this].
54 final Suite _suite;
55
56 /// The test that's being run.
57 final Test _test;
58
59 /// The function that will actually start the test running.
60 final Function _onRun();
61
62 /// The list of errors caught by the test.
63 final _errors = new List<AsyncError>();
64
65 /// The current state of the test.
66 var _state = const State(Status.pending, Result.success);
67
68 /// The controller for [LiveTest.onStateChange].
69 final _onStateChangeController = new StreamController<State>.broadcast();
70
71 /// The controller for [LiveTest.onError].
72 final _onErrorController = new StreamController<AsyncError>.broadcast();
73
74 /// Whether [run] has been called.
75 var _runCalled = false;
76
77 /// Creates a new controller for a [LiveTest].
78 ///
79 /// [test] is the test being run; [suite] is the suite that contains it.
80 ///
81 /// [onRun] is a function that will be called from [LiveTest.run]. It should
82 /// start the test running. The controller takes care of ensuring that
83 /// [LiveTest.run] isn't called more than once and that [LiveTest.onComplete]
84 /// is returned.
85 LiveTestController(this._suite, this._test, void onRun())
86 : _onRun = onRun {
87 _liveTest = new _LiveTest(this);
88 }
89
90 /// Adds an error to the [LiveTest].
91 ///
92 /// This both adds the error to [LiveTest.errors] and emits it via
93 /// [LiveTest.onError]. [stackTrace] is automatically converted into a [Chain]
94 /// if it's not one already.
95 void addError(error, StackTrace stackTrace) {
96 var asyncError = new AsyncError(error, new Chain.forTrace(stackTrace));
97 _errors.add(asyncError);
98 _onErrorController.add(asyncError);
99 }
100
101 /// Sets the current state of the [LiveTest] to [newState].
102 ///
103 /// If [newState] is different than the old state, this both sets
104 /// [LiveTest.state] and emits the new state via [LiveTest.onStateChanged]. If
105 /// it's not different, this does nothing.
106 void setState(State newState) {
107 if (_state == newState) return;
108 _state = newState;
109 _onStateChangeController.add(newState);
110 }
111
112 /// A wrapper for [_onRun] that ensures that it follows the guarantees for
113 /// [LiveTest.run].
114 Future _run() {
115 if (_runCalled) {
116 throw new StateError("LiveTest.run() may not be called more than once.");
117 }
118 _runCalled = true;
119
120 _controller._onRun();
121 return onComplete;
122 }
123 }
OLDNEW
« lib/src/live_test.dart ('K') | « lib/src/live_test.dart ('k') | lib/src/suite.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698