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

Side by Side Diff: lib/src/runner/console_reporter.dart

Issue 961003005: Add a flag to control the use of color in the test runner. (Closed) Base URL: git@github.com:dart-lang/unittest@master
Patch Set: Code review changes Created 5 years, 9 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
« no previous file with comments | « bin/unittest.dart ('k') | lib/src/util/io.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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.console_reporter;
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 '../util/io.dart';
14 import '../utils.dart'; 13 import '../utils.dart';
15 import 'engine.dart'; 14 import 'engine.dart';
16 15
17 /// The terminal escape for green text, or the empty string if this is Windows
18 /// or not outputting to a terminal.
19 final _green = getSpecial('\u001b[32m');
20
21 /// The terminal escape for red text, or the empty string if this is Windows or
22 /// not outputting to a terminal.
23 final _red = getSpecial('\u001b[31m');
24
25 /// The terminal escape for removing test coloring, or the empty string if this
26 /// is Windows or not outputting to a terminal.
27 final _noColor = getSpecial('\u001b[0m');
28
29 /// The maximum console line length. 16 /// The maximum console line length.
30 /// 17 ///
31 /// Lines longer than this will be cropped. 18 /// Lines longer than this will be cropped.
32 const _lineLength = 100; 19 const _lineLength = 100;
33 20
34 /// 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
35 /// continuously-updating line. 22 /// continuously-updating line.
36 class ConsoleReporter { 23 class ConsoleReporter {
24 /// The terminal escape for green text, or the empty string if this is Windows
25 /// or not outputting to a terminal.
26 final String _green;
27
28 /// The terminal escape for red text, or the empty string if this is Windows
29 /// or not outputting to a terminal.
30 final String _red;
31
32 /// The terminal escape for removing test coloring, or the empty string if
33 /// this is Windows or not outputting to a terminal.
34 final String _noColor;
35
37 /// The engine used to run the tests. 36 /// The engine used to run the tests.
38 final Engine _engine; 37 final Engine _engine;
39 38
40 /// Whether multiple test suites are being run. 39 /// Whether multiple test suites are being run.
41 final bool _multipleSuites; 40 final bool _multipleSuites;
42 41
43 /// A stopwatch that tracks the duration of the full run. 42 /// A stopwatch that tracks the duration of the full run.
44 final _stopwatch = new Stopwatch(); 43 final _stopwatch = new Stopwatch();
45 44
46 /// The set of tests that have completed and been marked as passing. 45 /// The set of tests that have completed and been marked as passing.
47 final _passed = new Set<LiveTest>(); 46 final _passed = new Set<LiveTest>();
48 47
49 /// The set of tests that have completed and been marked as failing or error. 48 /// The set of tests that have completed and been marked as failing or error.
50 final _failed = new Set<LiveTest>(); 49 final _failed = new Set<LiveTest>();
51 50
52 /// The size of [_passed] last time a progress notification was printed. 51 /// The size of [_passed] last time a progress notification was printed.
53 int _lastProgressPassed; 52 int _lastProgressPassed;
54 53
55 /// The size of [_failed] last time a progress notification was printed. 54 /// The size of [_failed] last time a progress notification was printed.
56 int _lastProgressFailed; 55 int _lastProgressFailed;
57 56
58 /// The message printed for the last progress notification. 57 /// The message printed for the last progress notification.
59 String _lastProgressMessage; 58 String _lastProgressMessage;
60 59
61 /// Creates a [ConsoleReporter] that will run all tests in [suites]. 60 /// Creates a [ConsoleReporter] that will run all tests in [suites].
62 ConsoleReporter(Iterable<Suite> suites) 61 ///
62 /// If [color] is `true`, this will use terminal colors; if it's `false`, it
63 /// won't.
64 ConsoleReporter(Iterable<Suite> suites, {bool color: true})
63 : _multipleSuites = suites.length > 1, 65 : _multipleSuites = suites.length > 1,
64 _engine = new Engine(suites) { 66 _engine = new Engine(suites),
65 67 _green = color ? '\u001b[32m' : '',
68 _red = color ? '\u001b[31m' : '',
69 _noColor = color ? '\u001b[0m' : '' {
66 _engine.onTestStarted.listen((liveTest) { 70 _engine.onTestStarted.listen((liveTest) {
67 _progressLine(_description(liveTest)); 71 _progressLine(_description(liveTest));
68 liveTest.onStateChange.listen((state) { 72 liveTest.onStateChange.listen((state) {
69 if (state.status != Status.complete) return; 73 if (state.status != Status.complete) return;
70 if (state.result == Result.success) { 74 if (state.result == Result.success) {
71 _passed.add(liveTest); 75 _passed.add(liveTest);
72 } else { 76 } else {
73 _passed.remove(liveTest); 77 _passed.remove(liveTest);
74 _failed.add(liveTest); 78 _failed.add(liveTest);
75 } 79 }
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 226
223 /// Returns a description of [liveTest]. 227 /// Returns a description of [liveTest].
224 /// 228 ///
225 /// This differs from the test's own description in that it may also include 229 /// This differs from the test's own description in that it may also include
226 /// the suite's name. 230 /// the suite's name.
227 String _description(LiveTest liveTest) { 231 String _description(LiveTest liveTest) {
228 if (_multipleSuites) return "${liveTest.suite.name}: ${liveTest.test.name}"; 232 if (_multipleSuites) return "${liveTest.suite.name}: ${liveTest.test.name}";
229 return liveTest.test.name; 233 return liveTest.test.name;
230 } 234 }
231 } 235 }
OLDNEW
« no previous file with comments | « bin/unittest.dart ('k') | lib/src/util/io.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698