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

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

Issue 869053002: unittest: refactor ahead of matcher changes (Closed) Base URL: https://github.com/dart-lang/unittest.git@master
Patch Set: nits 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
« no previous file with comments | « lib/src/prints_matcher.dart ('k') | lib/src/throws_matcher.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) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 part of unittest; 5 library unittest.simple_configuration;
6 6
7 // A custom failure handler for [expect] that routes expect failures 7 import 'dart:isolate';
8 // to the config.
9 class _ExpectFailureHandler extends DefaultFailureHandler {
10 final SimpleConfiguration _config;
11 8
12 _ExpectFailureHandler(this._config); 9 import '../unittest.dart';
13 10 import 'configuration.dart';
14 void fail(String reason) { 11 import 'utils.dart';
15 _config.onExpectFailure(reason);
16 }
17 }
18 12
19 /// Hooks to configure the unittest library for different platforms. This class 13 /// Hooks to configure the unittest library for different platforms. This class
20 /// implements the API in a platform-independent way. Tests that want to take 14 /// implements the API in a platform-independent way. Tests that want to take
21 /// advantage of the platform can create a subclass and override methods from 15 /// advantage of the platform can create a subclass and override methods from
22 /// this class. 16 /// this class.
23 class SimpleConfiguration extends Configuration { 17 class SimpleConfiguration extends Configuration {
24 // The VM won't shut down if a receive port is open. Use this to make sure 18 // The VM won't shut down if a receive port is open. Use this to make sure
25 // we correctly wait for asynchronous tests. 19 // we correctly wait for asynchronous tests.
26 ReceivePort _receivePort; 20 ReceivePort _receivePort;
27 21
28 /// Subclasses can override this with something useful for diagnostics.
29 /// Particularly useful in cases where we have parent/child configurations
30 /// such as layout tests.
31 String get name => 'Configuration';
32
33 bool get autoStart => true;
34
35 /// If true (the default), throw an exception at the end if any tests failed. 22 /// If true (the default), throw an exception at the end if any tests failed.
36 bool throwOnTestFailures = true; 23 bool throwOnTestFailures = true;
37 24
38 /// If true (the default), then tests will stop after the first failed
39 /// [expect]. If false, failed [expect]s will not cause the test
40 /// to stop (other exceptions will still terminate the test).
41 bool stopTestOnExpectFailure = true;
42
43 // If stopTestOnExpectFailure is false, we need to capture failures, which
44 // we do with this List.
45 final _testLogBuffer = <Pair<String, StackTrace>>[];
46
47 /// The constructor sets up a failure handler for [expect] that redirects 25 /// The constructor sets up a failure handler for [expect] that redirects
48 /// [expect] failures to [onExpectFailure]. 26 /// [expect] failures to [onExpectFailure].
49 SimpleConfiguration() : super.blank() { 27 SimpleConfiguration() : super.blank();
50 configureExpectFailureHandler(new _ExpectFailureHandler(this));
51 }
52 28
53 void onInit() { 29 void onInit() {
54 // For Dart internal tests, we don't want stack frame filtering. 30 // For Dart internal tests, we don't want stack frame filtering.
55 // We turn it off here in the default config, but by default turn 31 // We turn it off here in the default config, but by default turn
56 // it back on in the vm and html configs. 32 // it back on in the vm and html configs.
57 filterStacks = false; 33 filterStacks = false;
58 _receivePort = new ReceivePort(); 34 _receivePort = new ReceivePort();
59 _postMessage('unittest-suite-wait-for-done'); 35 _postMessage('unittest-suite-wait-for-done');
60 } 36 }
61 37
62 /// Called when each test starts. Useful to show intermediate progress on 38 /// Called when each test starts. Useful to show intermediate progress on
63 /// a test suite. Derived classes should call this first before their own 39 /// a test suite. Derived classes should call this first before their own
64 /// override code. 40 /// override code.
65 void onTestStart(TestCase testCase) { 41 void onTestStart(TestCase testCase) {
66 assert(testCase != null); 42 assert(testCase != null);
67 _testLogBuffer.clear();
68 }
69
70 /// Called when each test is first completed. Useful to show intermediate
71 /// progress on a test suite. Derived classes should call this first
72 /// before their own override code.
73 void onTestResult(TestCase testCase) {
74 assert(testCase != null);
75 if (!stopTestOnExpectFailure && _testLogBuffer.length > 0) {
76 // Write the message/stack pairs up to the last pairs.
77 var reason = new StringBuffer();
78 for (var reasonAndTrace
79 in _testLogBuffer.take(_testLogBuffer.length - 1)) {
80 reason.write(reasonAndTrace.first);
81 reason.write('\n');
82 reason.write(reasonAndTrace.last);
83 reason.write('\n');
84 }
85 var lastReasonAndTrace = _testLogBuffer.last;
86 // Write the last message.
87 reason.write(lastReasonAndTrace.first);
88 if (testCase.result == PASS) {
89 testCase._result = FAIL;
90 testCase._message = reason.toString();
91 // Use the last stack as the overall failure stack.
92 testCase._stackTrace = lastReasonAndTrace.last;
93 } else {
94 // Add the last stack to the message; we have a further stack
95 // caused by some other failure.
96 reason.write(lastReasonAndTrace.last);
97 reason.write('\n');
98 // Add the existing reason to the end of the expect log to
99 // create the final message.
100 testCase._message = '${reason.toString()}\n${testCase._message}';
101 }
102 }
103 } 43 }
104 44
105 void onTestResultChanged(TestCase testCase) { 45 void onTestResultChanged(TestCase testCase) {
106 assert(testCase != null); 46 assert(testCase != null);
107 } 47 }
108 48
109 /// Handles the logging of messages by a test case. The default in 49 /// Handles the logging of messages by a test case. The default in
110 /// this base configuration is to call print(); 50 /// this base configuration is to call print();
111 void onLogMessage(TestCase testCase, String message) { 51 void onLogMessage(TestCase testCase, String message) {
112 print(message); 52 print(message);
113 } 53 }
114 54
115 /// Handles failures from expect(). The default in
116 /// this base configuration is to throw an exception;
117 void onExpectFailure(String reason) {
118 if (stopTestOnExpectFailure) {
119 throw new TestFailure(reason);
120 } else {
121 try {
122 throw '';
123 } catch (_, stack) {
124 var trace = getTrace(stack, formatStacks, filterStacks);
125 if (trace == null) trace = stack;
126 _testLogBuffer.add(new Pair<String, StackTrace>(reason, trace));
127 }
128 }
129 }
130
131 /// Format a test result. 55 /// Format a test result.
132 String formatResult(TestCase testCase) { 56 String formatResult(TestCase testCase) {
133 var result = new StringBuffer(); 57 var result = new StringBuffer();
134 result.write(testCase.result.toUpperCase()); 58 result.write(testCase.result.toUpperCase());
135 result.write(": "); 59 result.write(": ");
136 result.write(testCase.description); 60 result.write(testCase.description);
137 result.write("\n"); 61 result.write("\n");
138 62
139 if (testCase.message != '') { 63 if (testCase.message != '') {
140 result.write(indent(testCase.message)); 64 result.write(indent(testCase.message));
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 } 114 }
191 } 115 }
192 } 116 }
193 117
194 void _postMessage(String message) { 118 void _postMessage(String message) {
195 // In dart2js browser tests, the JavaScript-based test controller 119 // In dart2js browser tests, the JavaScript-based test controller
196 // intercepts calls to print and listens for "secret" messages. 120 // intercepts calls to print and listens for "secret" messages.
197 print(message); 121 print(message);
198 } 122 }
199 } 123 }
OLDNEW
« no previous file with comments | « lib/src/prints_matcher.dart ('k') | lib/src/throws_matcher.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698