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

Side by Side Diff: utils/testrunner/dart_wrap_task.dart

Issue 10897016: Testrunner for 3rd parties. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 3 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012, 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 class DartWrapTask extends PipelineTask {
6 String sourceFileTemplate;
7 String tempDartFileTemplate;
8
9 DartWrapTask(this.sourceFileTemplate, this.tempDartFileTemplate);
10
11 void execute(Path testfile, List stdout, List stderr, bool verboseLogging,
12 Function exitHandler) {
13 // Get the source test file and canonicalize the path.
14 var sourceName = makePathAbsolute(concretize(sourceFileTemplate, testfile));
15 // Get the destination file.
16 var destFile = concretize(tempDartFileTemplate, testfile);
17
18 StringBuffer sbuf = new StringBuffer();
19
20 // Add the common header stuff.
21 var p = new Path(sourceName);
22 var u = new Path(configuration['unittest']);
23 sbuf.add(directives(p.filenameWithoutExtension, u.toString(), sourceName));
24
25 // Add the test configuration and the action function.
26 var action;
27 if (configuration['list-tests']) {
28 action = 'listTests';
29 sbuf.add(barebonesConfig());
30 sbuf.add(listTests(sourceName));
31 sbuf.add(formatListMessage(configuration['list-format']));
32 } else if (configuration['list-groups']) {
33 sbuf.add(barebonesConfig());
34 sbuf.add(listGroups(sourceName));
35 sbuf.add(formatListMessage(configuration['list-format']));
36 action = 'listGroups';
37 } else {
38 var runIsolated = configuration['isolate'];
39 var runInBrowser =
40 configuration['runtime'] != 'vm' &&
41 configuration['runtime'] != 'd8';
42 sbuf.add(testPrint(runInBrowser));
43 sbuf.add(testConfig(sourceName, runInBrowser, runIsolated,
44 configuration['time'],
45 configuration['summary']));
46 // Add the isolate stuff, if applicable. This overrides
47 if (runIsolated) {
48 action = 'runIsolateTests';
49 sbuf.add(isolateTests());
50 } else {
51 action = 'runTests';
52 sbuf.add("runTests() { unittest.runTests(); }\n");
53 }
54 sbuf.add(formatMessage(configuration['pass-format'],
55 configuration['fail-format'],
56 configuration['error-format']));
57 }
58
59 // Add the filter, if applicable.
60 var include = configuration['include'];
61 var filter = false;
62 if (include.length > 0) {
63 sbuf.add(filterTests(include, 'true'));
64 filter = true;
65 } else {
66 var exclude = configuration['exclude'];
67 if (exclude.length > 0) {
68 sbuf.add(filterTests(exclude, 'false'));
69 filter = true;
70 }
71 }
72
73 // Add the common trailer stuff.
74 sbuf.add(dartMain(action, filter));
75
76 // Save the Dart file.
77 createFile(destFile, sbuf.toString());
78 exitHandler(0);
79 }
80
81 String directives(String library, String unittest, String sourceName) {
82 return """
83 #library('$library');
84 #import('dart:isolate');
85 #import('$unittest', prefix:'unittest');
86 #import('$sourceName', prefix: 'test');
87 """;
88 }
89
90 String testPrint(bool runInBrowser) {
91 if (runInBrowser) {
92 return """
93 #import('dart:html');
94 void tprint(msg) {
95 var pre = query('#console');
96 pre.elements.add(new Text('###\$msg\\n'));
97 }
98 """;
99 } else {
100 return """
101 void tprint(msg) {
102 print('###\$msg');
103 }
104 """;
105 }
106 }
107
108 String config(String body) {
109 return """
110 class TestRunnerConfiguration extends unittest.Configuration {
111 get name => 'Test runner configuration';
112 get autoStart => false;
113 $body
114 }
115 """;
116 }
117
118 String barebonesConfig() {
119 return config('');
120 }
121
122 String testConfig(String sourceName, bool runInBrowser, bool runIsolated,
123 bool showTime, bool summary) {
124 StringBuffer sbuf = new StringBuffer();
125 if (runIsolated) {
126 sbuf.add("var port;\nTestRunnerConfiguration(this.port);\n");
127 }
128
129 sbuf.add("""
130 void dumpTestResult(TestCase t) {
131 var groupName = '', testName = '';
132 var idx = t.description.lastIndexOf('###');
133 if (idx >= 0) {
134 groupName = t.description.substring(0, idx).replaceAll('###', ' ');
135 testName = t.description.substring(idx+3);
136 } else {
137 testName = t.description;
138 }
139 var stack = (t.stackTrace == null) ? '' : '\${t.stackTrace} ';
140 var message = (t.message.length > 0) ? '\$t.message ' : '';
141 """);
142 if (showTime) {
143 sbuf.add("""
144 double duration = t.runningTime.inMilliseconds;
145 duration /= 1000;
146 var elapsed = '\${duration.toStringAsFixed(3)}s ';
147 """);
148 } else {
149 sbuf.add("var elapsed = '';\n");
150 }
151 sbuf.add("""
152 tprint(formatMessage('${sourceName} ', '\$groupName ', '\$testName ',
153 elapsed, t.result, message, stack));
154 }
155 """);
156
157 if (configuration['immediate']) {
158 sbuf.add("""
159 void onTestResult(TestCase testCase) {
160 dumpTestResult(testCase);
161 }
162 """);
163 }
164 sbuf.add("""
165 void onTestStart(TestCase testCase) {}
166 void onDone(int passed, int failed, int errors, List<TestCase> results,
167 String uncaughtError) {
168 """);
169 if (!configuration['immediate']) {
170 sbuf.add("""
171 // Print each result.
172 for (final testCase in results) {
173 dumpTestResult(testCase);
174 }
175 """);
176 }
177 if (summary) {
178 sbuf.add("""
179 tprint('');
180 var success = false;
181 if (passed == 0 && failed == 0 && errors == 0) {
182 tprint('$sourceName: No tests found.');
183 // This is considered a failure too.
184 } else if (failed == 0 && errors == 0 && uncaughtError == null) {
185 tprint('$sourceName: All \$passed tests passed.');
186 success = true;
187 } else {
188 if (uncaughtError != null) {
189 tprint('$sourceName: Top-level uncaught error: \$uncaughtError');
190 }
191 tprint('$sourceName: \$passed PASSED, \$failed FAILED, \$errors ERRORS');
192 }
193 """);
194 }
195 if (runIsolated) {
196 sbuf.add("var success = (passed > 0 && failed == 0 && errors == 0 "
197 "&& uncaughtError == null);\n");
198 sbuf.add("port.send(success);\n}\n");
199 } else if (runInBrowser) {
200 sbuf.add("window.postMessage('done', '*');\n}\n");
201 } else {
202 sbuf.add("}\n");
203 }
204 return config(sbuf.toString());
205 }
206
207 String formatListMessage(String format) {
208 return """
209 String formatMessage(filename, groupname, [ testname = '']) {
210 return '${format}'.
211 replaceAll('${Meta.testfile}', filename).
212 replaceAll('${Meta.testGroup}', groupname).
213 replaceAll('${Meta.testDescription}', testname);
214 }
215 """;
216 }
217
218 String formatMessage(
219 String passFormat, String failFormat, String errorFormat) {
220 return """
221 String formatMessage(filename, groupname,
222 [ testname = '', testTime = '', result = '',
223 message = '', stack = '' ]) {
224 var format = '$errorFormat';
225 if (result == 'pass') format = '$passFormat';
226 else if (result == 'fail') format = '$failFormat';
227 return format.
228 replaceAll('${Meta.testTime}', testTime).
229 replaceAll('${Meta.testfile}', filename).
230 replaceAll('${Meta.testGroup}', groupname).
231 replaceAll('${Meta.testDescription}', testname).
232 replaceAll('${Meta.testMessage}', message).
233 replaceAll('${Meta.testStacktrace}', stack);
234 }
235 """;
236 }
237
238 String listGroups(String sourceName) {
239 return """
240 listGroups() {
241 List tests = unittest.testCases;
242 Map groups = {};
243 for (var t in tests) {
244 var groupName, testName = '';
245 var idx = t.description.lastIndexOf('###');
246 if (idx >= 0) {
247 groupName = t.description.substring(0, idx).replaceAll('###', ' ');
248 if (!groups.containsKey(groupName)) {
249 groups[groupName] = '';
250 }
251 }
252 }
253 for (var g in groups.getKeys()) {
254 var msg = formatMessage('${sourceName} ', '\$g ');
255 print('###\$msg');
256 }
257 }
258 """;
259 }
260
261 String listTests(String sourceName) {
262 return """
263 listTests() {
264 List tests = unittest.testCases;
265 for (var t in tests) {
266 var groupName, testName = '';
267 var idx = t.description.lastIndexOf('###');
268 if (idx >= 0) {
269 groupName = t.description.substring(0, idx).replaceAll('###', ' ');
270 testName = t.description.substring(idx+3);
271 } else {
272 groupName = '';
273 testName = t.description;
274 }
275 var msg = formatMessage('${sourceName} ', '\$groupName ',
276 '\$testName ');
277 print('###\$msg');
278 }
279 }
280 """;
281 }
282
283 String filterTests(List filters, String filterReturnValue) {
284 StringBuffer sbuf = new StringBuffer();
285 sbuf.add('filterTest(t) {\n');
286 if (filters != null) {
287 sbuf.add(' var name = t.description.replaceAll("###", " ");\n');
288 for (var f in filters) {
289 sbuf.add(' if (name.indexOf("$f")>=0) return $filterReturnValue;\n');
290 }
291 sbuf.add(' return !$filterReturnValue;\n');
292 } else {
293 // TODO - is this right? Should it be filterReturnValue?
294 sbuf.add(' return true;\n');
295 }
296 sbuf.add('}\n');
297 return sbuf.toString();
298 }
299
300 String isolateTests() {
301 return """
302 runSlaveTest() {
303 port.receive((testName, sendport) {
304 unittest.configure(new TestRunnerConfiguration(sendport));
305 unittest.group('', test.main);
306 unittest.filterTests(testName);
307 unittest.runTests();
308 });
309 }
310
311 var testNum;
312 var failed;
313
314 runMasterTest() {
315 var tests = unittest.testCases;
316 SendPort sport = spawnFunction(runSlaveTest);
317 sport.call(tests[testNum].description).then((result) {
318 if (!result) failed = true;
319 ++testNum;
320 if (testNum < tests.length) {
321 runMasterTest();
322 } else if (failed) {
323 throw new Exception("Some tests failed.");
324 }
325 });
326 }
327
328 runIsolateTests() {
329 testNum = 0;
330 failed = false;
331 runMasterTest();
332 }
333 """;
334 }
335
336 String dartMain(String action, bool filter) {
337 return """
338 main() {
339 unittest.groupSep = '###';
340 unittest.configure(new TestRunnerConfiguration());
341 unittest.group('', test.main);
342 ${filter?'unittest.filterTests(filterTest);':''}
343 $action();
344 }
345 """;
346 }
347
348 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698