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

Side by Side Diff: lib/unittest/unittest_html.dart

Issue 10031022: step 1 in making unittest platform independent. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 8 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
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 /** 5 /**
6 * A simple unit test library for running tests in a browser. 6 * A simple unit test library for running tests in a browser.
7 */ 7 */
8 #library("unittest"); 8 #library('unittest');
9 9
10 #import("dart:html"); 10 #import('dart:html');
11 #import('dart:isolate');
11 12
12 #source("shared.dart"); 13 #source('config.dart');
14 #source('shared.dart');
15 #source('html_print.dart');
13 16
14 // TODO(rnystrom): Get rid of this if we get canonical closures for methods. 17 /** Whether this is run within dartium layout tests. */
15 EventListener _onErrorClosure; 18 bool _isLayoutTest = false;
16 19
17 _platformInitialize() { 20 void forLayoutTests() {
18 _onErrorClosure = (e) { _onError(e); }; 21 _isLayoutTest = true;
19 } 22 }
20 23
21 _platformDefer(void callback()) { 24 void serialInvokeAsync(List closures) {
22 window.setTimeout(callback, 0); 25 final length = closures.length;
23 } 26 if (length > 0) {
24 27 int i = 0;
25 void _onError(e) { 28 void invokeNext() {
26 if (_currentTest < _tests.length) { 29 closures[i]();
27 final testCase = _tests[_currentTest]; 30 i++;
28 // TODO(vsm): figure out how to expose the stack trace here 31 if (i < length) {
29 // Currently e.message works in dartium, but not in dartc. 32 window.setTimeout(invokeNext, 0);
30 testCase.error('(DOM callback has errors) Caught ${e}', ''); 33 }
31 _state = _UNCAUGHT_ERROR;
32 if (testCase.callbacks > 0) {
33 _currentTest++;
34 _testRunner();
35 } 34 }
35 window.setTimeout(invokeNext, 0);
36 } 36 }
37 } 37 }
38 38
39 /** Runs all queued tests, one at a time. */ 39 class _DefaultConfiguration() {
Bob Nystrom 2012/04/10 01:17:15 This doesn't look like valid Dart code. Ditch the
Siggi Cherem (dart-lang) 2012/04/10 01:28:44 Doh!... fixed
40 _platformStartTests() { 40 // TODO(rnystrom): Get rid of this if we get canonical closures for methods.
41 window.postMessage('unittest-suite-wait-for-done', '*'); 41 EventListener _onErrorClosure;
42 42
43 // Listen for uncaught errors. 43 void onInit() {
44 window.on.error.add(_onErrorClosure); 44 _onErrorClosure = (e) { _onError(e); };
45 }
46
47 _platformCompleteTests(int testsPassed, int testsFailed, int testsErrors) {
48 window.on.error.remove(_onErrorClosure);
49
50 if (_isLayoutTest && testsPassed == _tests.length) {
51 document.body.innerHTML = "PASS";
52 } else {
53 var newBody = new StringBuffer();
54 newBody.add("<table class='unittest-table'><tbody>");
55 newBody.add(testsPassed == _tests.length
56 ? "<tr><td colspan='3' class='unittest-pass'>PASS</td></tr>"
57 : "<tr><td colspan='3' class='unittest-fail'>FAIL</td></tr>");
58
59 for (final test_ in _tests) {
60 newBody.add(_toHtml(test_));
61 }
62
63 if (testsPassed == _tests.length) {
64 newBody.add("""
65 <tr><td colspan='3' class='unittest-pass'>
66 All ${testsPassed} tests passed
67 </td></tr>""");
68 } else {
69 newBody.add("""
70 <tr><td colspan='3'>Total
71 <span class='unittest-pass'>${testsPassed} passed</span>,
72 <span class='unittest-fail'>${testsFailed} failed</span>
73 <span class='unittest-error'>${testsErrors} errors</span>
74 </td></tr>""");
75 }
76 newBody.add("</tbody></table>");
77 document.body.innerHTML = newBody.toString();
78 } 45 }
79 46
80 window.postMessage('unittest-suite-done', '*'); 47 void _onError(e) {
81 } 48 if (_currentTest < _tests.length) {
82 49 final testCase = _tests[_currentTest];
83 String _toHtml(TestCase test_) { 50 // TODO(vsm): figure out how to expose the stack trace here
84 if (!test_.isComplete) { 51 // Currently e.message works in dartium, but not in dartc.
85 return ''' 52 testCase.error('(DOM callback has errors) Caught ${e}', '');
86 <tr> 53 _state = _UNCAUGHT_ERROR;
87 <td>${test_.id}</td> 54 if (testCase.callbacks > 0) {
88 <td class="unittest-error">NO STATUS</td> 55 _currentTest++;
89 <td>Test did not complete</td> 56 _testRunner();
90 </tr>'''; 57 }
58 }
91 } 59 }
92 60
93 var html = ''' 61 void onStart() {
94 <tr> 62 window.postMessage('unittest-suite-wait-for-done', '*');
95 <td>${test_.id}</td> 63 // Listen for uncaught errors.
96 <td class="unittest-${test_.result}">${test_.result.toUpperCase()}</td> 64 window.on.error.add(_onErrorClosure);
97 <td>Expectation: ${test_.description}. ${_htmlEscape(test_.message)}</td >
98 </tr>''';
99
100 if (test_.stackTrace != null) {
101 html +=
102 '<tr><td></td><td colspan="2"><pre>${_htmlEscape(test_.stackTrace)}</pre ></td></tr>';
103 } 65 }
104 66
105 return html; 67 void onResult(TestCase testCase) {}
68
69 onDone(int testsPassed, int testsFailed, int testsErrors,
70 List<TestCase> results) {
71 window.on.error.remove(_onErrorClosure);
72 _showResultsInPage(
73 testsPassed, testsFailed, testsErrors, results, _isLayoutTest);
74 window.postMessage('unittest-suite-done', '*');
75 }
106 } 76 }
107
108 //TODO(pquitslund): Move to a common lib
109 String _htmlEscape(String string) {
110 return string.replaceAll('&', '&amp;')
111 .replaceAll('<','&lt;')
112 .replaceAll('>','&gt;');
113 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698