OLD | NEW |
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_html_config'); | 8 library unittest_html_config; |
9 | 9 |
10 #import('dart:html'); | 10 import 'dart:html'; |
11 #import('unittest.dart'); | 11 import 'unittest.dart'; |
12 | 12 |
13 #source('html_print.dart'); | 13 /** Creates a table showing tests results in HTML. */ |
| 14 void _showResultsInPage(int passed, int failed, int errors, |
| 15 List<TestCase> results, bool isLayoutTest, String uncaughtError) { |
| 16 if (isLayoutTest && (passed == results.length) && uncaughtError == null) { |
| 17 document.body.innerHTML = "PASS"; |
| 18 } else { |
| 19 var newBody = new StringBuffer(); |
| 20 newBody.add("<table class='unittest-table'><tbody>"); |
| 21 newBody.add(passed == results.length && uncaughtError == null |
| 22 ? "<tr><td colspan='3' class='unittest-pass'>PASS</td></tr>" |
| 23 : "<tr><td colspan='3' class='unittest-fail'>FAIL</td></tr>"); |
| 24 |
| 25 for (final test_ in results) { |
| 26 newBody.add(_toHtml(test_)); |
| 27 } |
| 28 |
| 29 if (uncaughtError != null) { |
| 30 newBody.add('''<tr> |
| 31 <td>--</td> |
| 32 <td class="unittest-error">ERROR</td> |
| 33 <td>Uncaught error: $uncaughtError</td> |
| 34 </tr>'''); |
| 35 } |
| 36 |
| 37 if (passed == results.length && uncaughtError == null) { |
| 38 newBody.add(""" |
| 39 <tr><td colspan='3' class='unittest-pass'> |
| 40 All ${passed} tests passed |
| 41 </td></tr>"""); |
| 42 } else { |
| 43 newBody.add(""" |
| 44 <tr><td colspan='3'>Total |
| 45 <span class='unittest-pass'>${passed} passed</span>, |
| 46 <span class='unittest-fail'>${failed} failed</span> |
| 47 <span class='unittest-error'> |
| 48 ${errors + (uncaughtError == null ? 0 : 1)} errors</span> |
| 49 </td></tr>"""); |
| 50 } |
| 51 newBody.add("</tbody></table>"); |
| 52 document.body.innerHTML = newBody.toString(); |
| 53 } |
| 54 } |
| 55 |
| 56 String _toHtml(TestCase test_) { |
| 57 if (!test_.isComplete) { |
| 58 return ''' |
| 59 <tr> |
| 60 <td>${test_.id}</td> |
| 61 <td class="unittest-error">NO STATUS</td> |
| 62 <td>Test did not complete</td> |
| 63 </tr>'''; |
| 64 } |
| 65 |
| 66 var html = ''' |
| 67 <tr> |
| 68 <td>${test_.id}</td> |
| 69 <td class="unittest-${test_.result}">${test_.result.toUpperCase()}</td> |
| 70 <td>Expectation: ${test_.description}. ${_htmlEscape(test_.message)}</td
> |
| 71 </tr>'''; |
| 72 |
| 73 if (test_.stackTrace != null) { |
| 74 html = '$html<tr><td></td><td colspan="2"><pre>${_htmlEscape(test_.stackTrac
e)}</pre></td></tr>'; |
| 75 } |
| 76 |
| 77 return html; |
| 78 } |
| 79 |
| 80 //TODO(pquitslund): Move to a common lib |
| 81 String _htmlEscape(String string) { |
| 82 return string.replaceAll('&', '&') |
| 83 .replaceAll('<','<') |
| 84 .replaceAll('>','>'); |
| 85 } |
14 | 86 |
15 class HtmlConfiguration extends Configuration { | 87 class HtmlConfiguration extends Configuration { |
16 /** Whether this is run within dartium layout tests. */ | 88 /** Whether this is run within dartium layout tests. */ |
17 final bool _isLayoutTest; | 89 final bool _isLayoutTest; |
18 HtmlConfiguration(this._isLayoutTest); | 90 HtmlConfiguration(this._isLayoutTest); |
19 | 91 |
20 // TODO(rnystrom): Get rid of this if we get canonical closures for methods. | 92 // TODO(rnystrom): Get rid of this if we get canonical closures for methods. |
21 EventListener _onErrorClosure; | 93 EventListener _onErrorClosure; |
22 EventListener _onMessageClosure; | 94 EventListener _onMessageClosure; |
23 | 95 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 _uninstallHandlers(); | 139 _uninstallHandlers(); |
68 _showResultsInPage(passed, failed, errors, results, _isLayoutTest, | 140 _showResultsInPage(passed, failed, errors, results, _isLayoutTest, |
69 uncaughtError); | 141 uncaughtError); |
70 window.postMessage('unittest-suite-done', '*'); | 142 window.postMessage('unittest-suite-done', '*'); |
71 } | 143 } |
72 } | 144 } |
73 | 145 |
74 void useHtmlConfiguration([bool isLayoutTest = false]) { | 146 void useHtmlConfiguration([bool isLayoutTest = false]) { |
75 configure(new HtmlConfiguration(isLayoutTest)); | 147 configure(new HtmlConfiguration(isLayoutTest)); |
76 } | 148 } |
OLD | NEW |