Chromium Code Reviews| 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"); | 8 #library('unittest'); |
| 9 | 9 |
| 10 #import("dart:dom"); | 10 #import('dart:dom'); |
| 11 #import('dart:isolate'); | |
| 11 | 12 |
| 12 #source("shared.dart"); | 13 #source('config.dart'); |
| 14 #source('shared.dart'); | |
| 15 #source('html_print.dart'); | |
| 16 | |
| 17 /** Whether this is run within dartium layout tests. */ | |
| 18 bool _isLayoutTest = false; | |
| 19 | |
| 20 void forLayoutTests() { | |
| 21 _isLayoutTest = true; | |
| 22 } | |
| 13 | 23 |
| 14 // TODO(rnystrom): Get rid of this if we get canonical closures for methods. | 24 // TODO(rnystrom): Get rid of this if we get canonical closures for methods. |
| 15 EventListener _onErrorClosure; | 25 EventListener _onErrorClosure; |
| 16 | 26 |
| 17 _platformInitialize() { | 27 _platformInitialize() { |
| 18 _onErrorClosure = (e) { _onError(e); }; | 28 _onErrorClosure = (e) { _onError(e); }; |
| 19 } | 29 } |
| 20 | 30 |
| 21 _platformDefer(void callback()) { | |
| 22 window.setTimeout(callback, 0); | |
| 23 } | |
| 24 | |
| 25 void _onError(e) { | 31 void _onError(e) { |
| 26 if (_currentTest < _tests.length) { | 32 if (_currentTest < _tests.length) { |
| 27 final testCase = _tests[_currentTest]; | 33 final testCase = _tests[_currentTest]; |
| 28 // TODO(vsm): figure out how to expose the stack trace here | 34 // TODO(vsm): figure out how to expose the stack trace here |
| 29 // Currently e.message works in dartium, but not in dartc. | 35 // Currently e.message works in dartium, but not in dartc. |
| 30 testCase.error('(DOM callback has errors) Caught ${e}', ''); | 36 testCase.error('(DOM callback has errors) Caught ${e}', ''); |
| 31 _state = _UNCAUGHT_ERROR; | 37 _state = _UNCAUGHT_ERROR; |
| 32 if (testCase.callbacks > 0) { | 38 if (testCase.callbacks > 0) { |
| 33 _currentTest++; | 39 _currentTest++; |
| 34 _testRunner(); | 40 _testRunner(); |
| 35 } | 41 } |
| 36 } | 42 } |
| 37 } | 43 } |
| 38 | 44 |
| 39 /** Runs all queued tests, one at a time. */ | 45 /** Runs all queued tests, one at a time. */ |
| 40 _platformStartTests() { | 46 _platformStartTests() { |
| 41 window.postMessage('unittest-suite-wait-for-done', '*'); | 47 window.postMessage('unittest-suite-wait-for-done', '*'); |
| 42 | 48 |
| 43 // Listen for uncaught errors. | 49 // Listen for uncaught errors. |
| 44 window.addEventListener('error', _onErrorClosure, true); | 50 window.addEventListener('error', _onErrorClosure, true); |
| 45 } | 51 } |
| 46 | 52 |
| 47 _platformCompleteTests(int testsPassed, int testsFailed, int testsErrors) { | 53 _platformCompleteTests(int testsPassed, int testsFailed, int testsErrors, |
| 54 List<TestCase> results) { | |
| 48 window.removeEventListener('error', _onErrorClosure); | 55 window.removeEventListener('error', _onErrorClosure); |
| 49 | 56 _showResultsInPage( |
| 50 if (_isLayoutTest && testsPassed == _tests.length) { | 57 testsPassed, testsFailed, testsErrors, results, _isLayoutTest); |
| 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 } | |
| 79 | |
| 80 window.postMessage('unittest-suite-done', '*'); | 58 window.postMessage('unittest-suite-done', '*'); |
| 81 } | 59 } |
| 82 | 60 |
| 83 String _toHtml(TestCase test_) { | 61 Configuration _defaultConfig() { |
| 84 if (!test_.isComplete) { | 62 return new Configuration( |
| 85 return ''' | 63 _platformInitialize, |
|
Bob Nystrom
2012/04/10 00:30:31
Instead of closures here, I would just subclass Co
Siggi Cherem (dart-lang)
2012/04/10 01:05:28
Done.
| |
| 86 <tr> | 64 _platformStartTests, |
| 87 <td>${test_.id}</td> | 65 (testCase) {}, |
| 88 <td class="unittest-error">NO STATUS</td> | 66 _platformCompleteTests); |
| 89 <td>Test did not complete</td> | |
| 90 </tr>'''; | |
| 91 } | |
| 92 | |
| 93 var html = ''' | |
| 94 <tr> | |
| 95 <td>${test_.id}</td> | |
| 96 <td class="unittest-${test_.result}">${test_.result.toUpperCase()}</td> | |
| 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 } | |
| 104 | |
| 105 return html; | |
| 106 } | 67 } |
| 107 | |
| 108 //TODO(pquitslund): Move to a common lib | |
| 109 String _htmlEscape(String string) { | |
| 110 return string.replaceAll('&', '&') | |
| 111 .replaceAll('<','<') | |
| 112 .replaceAll('>','>'); | |
| 113 } | |
| OLD | NEW |