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.console_reporter; | 5 library unittest.console_reporter; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 | 9 |
10 import 'package:stack_trace/stack_trace.dart'; | |
kevmoo
2015/02/19 01:54:08
unused import
| |
11 | |
10 import 'engine.dart'; | 12 import 'engine.dart'; |
11 import 'io.dart'; | 13 import 'io.dart'; |
12 import 'live_test.dart'; | 14 import 'live_test.dart'; |
13 import 'state.dart'; | 15 import 'state.dart'; |
14 import 'suite.dart'; | 16 import 'suite.dart'; |
15 import 'utils.dart'; | 17 import 'utils.dart'; |
16 | 18 |
17 /// The terminal escape for green text, or the empty string if this is Windows | 19 /// The terminal escape for green text, or the empty string if this is Windows |
18 /// or not outputting to a terminal. | 20 /// or not outputting to a terminal. |
19 final _green = getSpecial('\u001b[32m'); | 21 final _green = getSpecial('\u001b[32m'); |
(...skipping 22 matching lines...) Expand all Loading... | |
42 | 44 |
43 /// A stopwatch that tracks the duration of the full run. | 45 /// A stopwatch that tracks the duration of the full run. |
44 final _stopwatch = new Stopwatch(); | 46 final _stopwatch = new Stopwatch(); |
45 | 47 |
46 /// The set of tests that have completed and been marked as passing. | 48 /// The set of tests that have completed and been marked as passing. |
47 final _passed = new Set<LiveTest>(); | 49 final _passed = new Set<LiveTest>(); |
48 | 50 |
49 /// The set of tests that have completed and been marked as failing or error. | 51 /// The set of tests that have completed and been marked as failing or error. |
50 final _failed = new Set<LiveTest>(); | 52 final _failed = new Set<LiveTest>(); |
51 | 53 |
54 /// The size of [_passed] last time a progress notification was printed. | |
55 int _lastProgressPassed; | |
56 | |
57 /// The size of [_failed] last time a progress notification was printed. | |
58 int _lastProgressFailed; | |
59 | |
60 /// The message printed for the last progress notification. | |
61 String _lastProgressMessage; | |
62 | |
52 /// Creates a [ConsoleReporter] that will run all tests in [suites]. | 63 /// Creates a [ConsoleReporter] that will run all tests in [suites]. |
53 ConsoleReporter(Iterable<Suite> suites) | 64 ConsoleReporter(Iterable<Suite> suites) |
54 : _multipleSuites = suites.length > 1, | 65 : _multipleSuites = suites.length > 1, |
55 _engine = new Engine(suites) { | 66 _engine = new Engine(suites) { |
56 | 67 |
57 _engine.onTestStarted.listen((liveTest) { | 68 _engine.onTestStarted.listen((liveTest) { |
58 _progressLine(_description(liveTest)); | 69 _progressLine(_description(liveTest)); |
59 liveTest.onStateChange.listen((state) { | 70 liveTest.onStateChange.listen((state) { |
60 if (state.status != Status.complete) return; | 71 if (state.status != Status.complete) return; |
61 if (state.result == Result.success) { | 72 if (state.result == Result.success) { |
62 _passed.add(liveTest); | 73 _passed.add(liveTest); |
63 } else { | 74 } else { |
64 _passed.remove(liveTest); | 75 _passed.remove(liveTest); |
65 _failed.add(liveTest); | 76 _failed.add(liveTest); |
66 } | 77 } |
67 _progressLine(_description(liveTest)); | 78 _progressLine(_description(liveTest)); |
68 }); | 79 }); |
69 | 80 |
70 liveTest.onError.listen((error) { | 81 liveTest.onError.listen((error) { |
71 if (liveTest.state.status != Status.complete) return; | 82 if (liveTest.state.status != Status.complete) return; |
72 | 83 |
73 // TODO(nweiz): don't re-print the progress line if a test has multiple | |
74 // errors in a row. | |
75 _progressLine(_description(liveTest)); | 84 _progressLine(_description(liveTest)); |
76 print(''); | 85 print(''); |
77 print(indent("${error.error}\n${error.stackTrace}")); | 86 print(indent(error.error.toString())); |
87 print(indent(terseChain(error.stackTrace).toString())); | |
78 }); | 88 }); |
79 }); | 89 }); |
80 } | 90 } |
81 | 91 |
82 /// Runs all tests in all provided suites. | 92 /// Runs all tests in all provided suites. |
83 /// | 93 /// |
84 /// This returns `true` if all tests succeed, and `false` otherwise. It will | 94 /// This returns `true` if all tests succeed, and `false` otherwise. It will |
85 /// only return once all tests have finished running. | 95 /// only return once all tests have finished running. |
86 Future<bool> run() { | 96 Future<bool> run() { |
87 if (_stopwatch.isRunning) { | 97 if (_stopwatch.isRunning) { |
88 throw new StateError("ConsoleReporter.run() may not be called more than " | 98 throw new StateError("ConsoleReporter.run() may not be called more than " |
89 "once."); | 99 "once."); |
90 } | 100 } |
91 | 101 |
102 if (_engine.liveTests.isEmpty) { | |
103 print("No tests ran."); | |
104 return new Future.value(true); | |
105 } | |
106 | |
92 _stopwatch.start(); | 107 _stopwatch.start(); |
93 return _engine.run().then((success) { | 108 return _engine.run().then((success) { |
94 if (_engine.liveTests.isEmpty) { | 109 if (success) { |
95 print("\nNo tests ran."); | |
96 } else if (success) { | |
97 _progressLine("All tests passed!"); | 110 _progressLine("All tests passed!"); |
98 print(''); | 111 print(''); |
99 } else { | 112 } else { |
100 _progressLine('Some tests failed.', color: _red); | 113 _progressLine('Some tests failed.', color: _red); |
101 print(''); | 114 print(''); |
102 } | 115 } |
103 | 116 |
104 return success; | 117 return success; |
105 }); | 118 }); |
106 } | 119 } |
107 | 120 |
121 /// Signals that the caller is done with any test output and the reporter | |
122 /// should release any resources it has allocated. | |
123 Future close() => _engine.close(); | |
124 | |
108 /// Prints a line representing the current state of the tests. | 125 /// Prints a line representing the current state of the tests. |
109 /// | 126 /// |
110 /// [message] goes after the progress report, and may be truncated to fit the | 127 /// [message] goes after the progress report, and may be truncated to fit the |
111 /// entire line within [_lineLength]. If [color] is passed, it's used as the | 128 /// entire line within [_lineLength]. If [color] is passed, it's used as the |
112 /// color for [message]. | 129 /// color for [message]. |
113 void _progressLine(String message, {String color}) { | 130 void _progressLine(String message, {String color}) { |
131 // Print nothing if nothing has changed since the last progress line. | |
132 if (_passed.length == _lastProgressPassed && | |
133 _failed.length == _lastProgressFailed && | |
134 message == _lastProgressMessage) { | |
135 return; | |
136 } | |
137 | |
138 _lastProgressPassed = _passed.length; | |
139 _lastProgressFailed = _failed.length; | |
140 _lastProgressMessage = message; | |
141 | |
114 if (color == null) color = ''; | 142 if (color == null) color = ''; |
115 var duration = _stopwatch.elapsed; | 143 var duration = _stopwatch.elapsed; |
116 var buffer = new StringBuffer(); | 144 var buffer = new StringBuffer(); |
117 | 145 |
118 // \r moves back to the beginning of the current line. | 146 // \r moves back to the beginning of the current line. |
119 buffer.write('\r${_timeString(duration)} '); | 147 buffer.write('\r${_timeString(duration)} '); |
120 buffer.write(_green); | 148 buffer.write(_green); |
121 buffer.write('+'); | 149 buffer.write('+'); |
122 buffer.write(_passed.length); | 150 buffer.write(_passed.length); |
123 buffer.write(_noColor); | 151 buffer.write(_noColor); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
196 | 224 |
197 /// Returns a description of [liveTest]. | 225 /// Returns a description of [liveTest]. |
198 /// | 226 /// |
199 /// This differs from the test's own description in that it may also include | 227 /// This differs from the test's own description in that it may also include |
200 /// the suite's name. | 228 /// the suite's name. |
201 String _description(LiveTest liveTest) { | 229 String _description(LiveTest liveTest) { |
202 if (_multipleSuites) return "${liveTest.suite.name}: ${liveTest.test.name}"; | 230 if (_multipleSuites) return "${liveTest.suite.name}: ${liveTest.test.name}"; |
203 return liveTest.test.name; | 231 return liveTest.test.name; |
204 } | 232 } |
205 } | 233 } |
OLD | NEW |