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.engine; | |
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 'utils.dart'; | |
14 | |
15 /// An [Engine] manages a run that encompasses multiple test suites. | |
16 /// | |
17 /// The current status of every test is visible via [liveTests]. [onTestStarted] | |
18 /// can also be used to be notified when a test is about to be run. | |
19 /// | |
20 /// Suites will be run in the order they're provided to [new Engine]. Tests | |
21 /// within those suites will likewise be run in the order of [Suite.tests]. | |
22 class Engine { | |
23 /// Whether [run] has been called yet. | |
24 var _runCalled = false; | |
25 | |
26 /// An unmodifiable list of tests to run. | |
27 /// | |
28 /// These are [LiveTest]s, representing the in-progress state of each test. | |
29 /// Tests that have not yet begun running are marked [Status.pending]; tests | |
30 /// that have finished are marked [Status.complete]. | |
31 /// | |
32 /// [LiveTest.run] must not be called on these tests. | |
33 final List<LiveTest> liveTests; | |
34 | |
35 /// A stream that emits each [LiveTest] as it's about to start running. | |
36 /// | |
37 /// This is guaranteed to fire before [LiveTest.onStateChange] first fires. | |
38 Stream<LiveTest> get onTestStarted => _onTestStartedController.stream; | |
39 final _onTestStartedController = new StreamController<LiveTest>.broadcast(); | |
40 | |
41 /// Creates an [Engine] that will run all tests in [suites]. | |
42 Engine(Iterable<Suite> suites) | |
43 : liveTests = new UnmodifiableListView(flatten(suites.map((suite) => | |
44 suite.tests.map((test) => test.load(suite))))); | |
45 | |
46 /// Runs all tests in all suites defined by this engine. | |
47 /// | |
48 /// This returns `true` if all tests succeed, and `false` otherwise. It will | |
49 /// only return once all tests have finished running. | |
50 Future<bool> run() { | |
51 if (_runCalled) { | |
52 throw new StateError("Engine.run() may not be called more than once."); | |
53 } | |
54 _runCalled = true; | |
55 | |
56 return Future.forEach(liveTests, (liveTest) { | |
57 _onTestStartedController.add(liveTest); | |
58 | |
59 // First, schedule a microtask to ensure that [onTestStarted] fires before | |
60 // the first [LiveTest.onStateChange] event. Once the test finishes, use | |
61 // [new Future] to do a coarse-grained event loop pump to avoid starving | |
62 // the DOM or other non-microtask events. | |
63 return new Future.microtask(liveTest.run).then((_) => new Future(() {})); | |
64 }).then((_) => | |
65 liveTests.every((liveTest) => liveTest.state.result == Result.success)); | |
66 } | |
67 | |
68 /// Signals that the caller is done paying attention to test results and the | |
69 /// engine should release any resources it has allocated. | |
70 Future close() => | |
71 Future.wait(liveTests.map((liveTest) => liveTest.close())); | |
72 } | |
OLD | NEW |