| 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.runner.console_reporter; | 5 library unittest.runner.reporter.compact; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 import '../backend/live_test.dart'; | 10 import '../../backend/live_test.dart'; |
| 11 import '../backend/state.dart'; | 11 import '../../backend/state.dart'; |
| 12 import '../backend/suite.dart'; | 12 import '../../backend/suite.dart'; |
| 13 import '../utils.dart'; | 13 import '../../utils.dart'; |
| 14 import 'engine.dart'; | 14 import '../engine.dart'; |
| 15 | 15 |
| 16 /// The maximum console line length. | 16 /// The maximum console line length. |
| 17 /// | 17 /// |
| 18 /// Lines longer than this will be cropped. | 18 /// Lines longer than this will be cropped. |
| 19 const _lineLength = 100; | 19 const _lineLength = 100; |
| 20 | 20 |
| 21 /// A reporter that prints test results to the console in a single | 21 /// A reporter that prints test results to the console in a single |
| 22 /// continuously-updating line. | 22 /// continuously-updating line. |
| 23 class ConsoleReporter { | 23 class CompactReporter { |
| 24 /// The terminal escape for green text, or the empty string if this is Windows | 24 /// The terminal escape for green text, or the empty string if this is Windows |
| 25 /// or not outputting to a terminal. | 25 /// or not outputting to a terminal. |
| 26 final String _green; | 26 final String _green; |
| 27 | 27 |
| 28 /// The terminal escape for red text, or the empty string if this is Windows | 28 /// The terminal escape for red text, or the empty string if this is Windows |
| 29 /// or not outputting to a terminal. | 29 /// or not outputting to a terminal. |
| 30 final String _red; | 30 final String _red; |
| 31 | 31 |
| 32 /// The terminal escape for removing test coloring, or the empty string if | 32 /// The terminal escape for removing test coloring, or the empty string if |
| 33 /// this is Windows or not outputting to a terminal. | 33 /// this is Windows or not outputting to a terminal. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 54 /// The size of [_failed] last time a progress notification was printed. | 54 /// The size of [_failed] last time a progress notification was printed. |
| 55 int _lastProgressFailed; | 55 int _lastProgressFailed; |
| 56 | 56 |
| 57 /// The message printed for the last progress notification. | 57 /// The message printed for the last progress notification. |
| 58 String _lastProgressMessage; | 58 String _lastProgressMessage; |
| 59 | 59 |
| 60 /// Creates a [ConsoleReporter] that will run all tests in [suites]. | 60 /// Creates a [ConsoleReporter] that will run all tests in [suites]. |
| 61 /// | 61 /// |
| 62 /// If [color] is `true`, this will use terminal colors; if it's `false`, it | 62 /// If [color] is `true`, this will use terminal colors; if it's `false`, it |
| 63 /// won't. | 63 /// won't. |
| 64 ConsoleReporter(Iterable<Suite> suites, {bool color: true}) | 64 CompactReporter(Iterable<Suite> suites, {bool color: true}) |
| 65 : _multipleSuites = suites.length > 1, | 65 : _multipleSuites = suites.length > 1, |
| 66 _engine = new Engine(suites), | 66 _engine = new Engine(suites), |
| 67 _green = color ? '\u001b[32m' : '', | 67 _green = color ? '\u001b[32m' : '', |
| 68 _red = color ? '\u001b[31m' : '', | 68 _red = color ? '\u001b[31m' : '', |
| 69 _noColor = color ? '\u001b[0m' : '' { | 69 _noColor = color ? '\u001b[0m' : '' { |
| 70 _engine.onTestStarted.listen((liveTest) { | 70 _engine.onTestStarted.listen((liveTest) { |
| 71 _progressLine(_description(liveTest)); | 71 _progressLine(_description(liveTest)); |
| 72 liveTest.onStateChange.listen((state) { | 72 liveTest.onStateChange.listen((state) { |
| 73 if (state.status != Status.complete) return; | 73 if (state.status != Status.complete) return; |
| 74 if (state.result == Result.success) { | 74 if (state.result == Result.success) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 90 }); | 90 }); |
| 91 }); | 91 }); |
| 92 } | 92 } |
| 93 | 93 |
| 94 /// Runs all tests in all provided suites. | 94 /// Runs all tests in all provided suites. |
| 95 /// | 95 /// |
| 96 /// This returns `true` if all tests succeed, and `false` otherwise. It will | 96 /// This returns `true` if all tests succeed, and `false` otherwise. It will |
| 97 /// only return once all tests have finished running. | 97 /// only return once all tests have finished running. |
| 98 Future<bool> run() { | 98 Future<bool> run() { |
| 99 if (_stopwatch.isRunning) { | 99 if (_stopwatch.isRunning) { |
| 100 throw new StateError("ConsoleReporter.run() may not be called more than " | 100 throw new StateError("CompactReporter.run() may not be called more than " |
| 101 "once."); | 101 "once."); |
| 102 } | 102 } |
| 103 | 103 |
| 104 if (_engine.liveTests.isEmpty) { | 104 if (_engine.liveTests.isEmpty) { |
| 105 print("No tests ran."); | 105 print("No tests ran."); |
| 106 return new Future.value(true); | 106 return new Future.value(true); |
| 107 } | 107 } |
| 108 | 108 |
| 109 _stopwatch.start(); | 109 _stopwatch.start(); |
| 110 return _engine.run().then((success) { | 110 return _engine.run().then((success) { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 | 161 |
| 162 buffer.write(': '); | 162 buffer.write(': '); |
| 163 buffer.write(color); | 163 buffer.write(color); |
| 164 | 164 |
| 165 // Ensure the line fits within [_lineLength]. [buffer] includes the color | 165 // Ensure the line fits within [_lineLength]. [buffer] includes the color |
| 166 // escape sequences too. Because these sequences are not visible characters, | 166 // escape sequences too. Because these sequences are not visible characters, |
| 167 // we make sure they are not counted towards the limit. | 167 // we make sure they are not counted towards the limit. |
| 168 var nonVisible = 1 + _green.length + _noColor.length + color.length + | 168 var nonVisible = 1 + _green.length + _noColor.length + color.length + |
| 169 (_failed.isEmpty ? 0 : _red.length + _noColor.length); | 169 (_failed.isEmpty ? 0 : _red.length + _noColor.length); |
| 170 var length = buffer.length - nonVisible; | 170 var length = buffer.length - nonVisible; |
| 171 buffer.write(_truncate(message, _lineLength - length)); | 171 buffer.write(truncate(message, _lineLength - length)); |
| 172 buffer.write(_noColor); | 172 buffer.write(_noColor); |
| 173 | 173 |
| 174 // Pad the rest of the line so that it looks erased. | 174 // Pad the rest of the line so that it looks erased. |
| 175 length = buffer.length - nonVisible - _noColor.length; | 175 length = buffer.length - nonVisible - _noColor.length; |
| 176 buffer.write(' ' * (_lineLength - length)); | 176 buffer.write(' ' * (_lineLength - length)); |
| 177 stdout.write(buffer.toString()); | 177 stdout.write(buffer.toString()); |
| 178 } | 178 } |
| 179 | 179 |
| 180 /// Returns a representation of [duration] as `MM:SS`. | 180 /// Returns a representation of [duration] as `MM:SS`. |
| 181 String _timeString(Duration duration) { | 181 String _timeString(Duration duration) { |
| 182 return "${duration.inMinutes.toString().padLeft(2, '0')}:" | 182 return "${duration.inMinutes.toString().padLeft(2, '0')}:" |
| 183 "${(duration.inSeconds % 60).toString().padLeft(2, '0')}"; | 183 "${(duration.inSeconds % 60).toString().padLeft(2, '0')}"; |
| 184 } | 184 } |
| 185 | 185 |
| 186 /// Truncates [text] to fit within [maxLength]. | |
| 187 /// | |
| 188 /// This will try to truncate along word boundaries and preserve words both at | |
| 189 /// the beginning and the end of [text]. | |
| 190 String _truncate(String text, int maxLength) { | |
| 191 // Return the full message if it fits. | |
| 192 if (text.length <= maxLength) return text; | |
| 193 | |
| 194 // If we can fit the first and last three words, do so. | |
| 195 var words = text.split(' '); | |
| 196 if (words.length > 1) { | |
| 197 var i = words.length; | |
| 198 var length = words.first.length + 4; | |
| 199 do { | |
| 200 i--; | |
| 201 length += 1 + words[i].length; | |
| 202 } while (length <= maxLength && i > 0); | |
| 203 if (length > maxLength || i == 0) i++; | |
| 204 if (i < words.length - 4) { | |
| 205 // Require at least 3 words at the end. | |
| 206 var buffer = new StringBuffer(); | |
| 207 buffer.write(words.first); | |
| 208 buffer.write(' ...'); | |
| 209 for ( ; i < words.length; i++) { | |
| 210 buffer.write(' '); | |
| 211 buffer.write(words[i]); | |
| 212 } | |
| 213 return buffer.toString(); | |
| 214 } | |
| 215 } | |
| 216 | |
| 217 // Otherwise truncate to return the trailing text, but attempt to start at | |
| 218 // the beginning of a word. | |
| 219 var result = text.substring(text.length - maxLength + 4); | |
| 220 var firstSpace = result.indexOf(' '); | |
| 221 if (firstSpace > 0) { | |
| 222 result = result.substring(firstSpace); | |
| 223 } | |
| 224 return '...$result'; | |
| 225 } | |
| 226 | |
| 227 /// Returns a description of [liveTest]. | 186 /// Returns a description of [liveTest]. |
| 228 /// | 187 /// |
| 229 /// This differs from the test's own description in that it may also include | 188 /// This differs from the test's own description in that it may also include |
| 230 /// the suite's name. | 189 /// the suite's name. |
| 231 String _description(LiveTest liveTest) { | 190 String _description(LiveTest liveTest) { |
| 232 if (_multipleSuites) return "${liveTest.suite.name}: ${liveTest.test.name}"; | 191 if (_multipleSuites) return "${liveTest.suite.name}: ${liveTest.test.name}"; |
| 233 return liveTest.test.name; | 192 return liveTest.test.name; |
| 234 } | 193 } |
| 235 } | 194 } |
| OLD | NEW |