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 /** | |
| 6 * A simple unit test library for running tests in a browser. | |
| 7 */ | |
| 8 #library("unittest"); | |
| 9 | 5 |
| 10 #import("dart:html"); | 6 /** Creates a table showing tests results in HTML. */ |
| 11 | 7 void _showResultsInPage(int testsPassed, int testsFailed, int testsErrors, |
| 12 #source("shared.dart"); | 8 List<TestCase> results, isLayoutTest) { |
| 13 | 9 if (isLayoutTest && testsPassed == results.length) { |
|
Bob Nystrom
2012/04/10 00:30:31
I would parenthesize the == expression.
Siggi Cherem (dart-lang)
2012/04/10 01:05:28
Done.
| |
| 14 // TODO(rnystrom): Get rid of this if we get canonical closures for methods. | |
| 15 EventListener _onErrorClosure; | |
| 16 | |
| 17 _platformInitialize() { | |
| 18 _onErrorClosure = (e) { _onError(e); }; | |
| 19 } | |
| 20 | |
| 21 _platformDefer(void callback()) { | |
| 22 window.setTimeout(callback, 0); | |
| 23 } | |
| 24 | |
| 25 void _onError(e) { | |
| 26 if (_currentTest < _tests.length) { | |
| 27 final testCase = _tests[_currentTest]; | |
| 28 // TODO(vsm): figure out how to expose the stack trace here | |
| 29 // Currently e.message works in dartium, but not in dartc. | |
| 30 testCase.error('(DOM callback has errors) Caught ${e}', ''); | |
| 31 _state = _UNCAUGHT_ERROR; | |
| 32 if (testCase.callbacks > 0) { | |
| 33 _currentTest++; | |
| 34 _testRunner(); | |
| 35 } | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 /** Runs all queued tests, one at a time. */ | |
| 40 _platformStartTests() { | |
| 41 window.postMessage('unittest-suite-wait-for-done', '*'); | |
| 42 | |
| 43 // Listen for uncaught errors. | |
| 44 window.on.error.add(_onErrorClosure); | |
| 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"; | 10 document.body.innerHTML = "PASS"; |
| 52 } else { | 11 } else { |
| 53 var newBody = new StringBuffer(); | 12 var newBody = new StringBuffer(); |
| 54 newBody.add("<table class='unittest-table'><tbody>"); | 13 newBody.add("<table class='unittest-table'><tbody>"); |
| 55 newBody.add(testsPassed == _tests.length | 14 newBody.add(testsPassed == results.length |
| 56 ? "<tr><td colspan='3' class='unittest-pass'>PASS</td></tr>" | 15 ? "<tr><td colspan='3' class='unittest-pass'>PASS</td></tr>" |
| 57 : "<tr><td colspan='3' class='unittest-fail'>FAIL</td></tr>"); | 16 : "<tr><td colspan='3' class='unittest-fail'>FAIL</td></tr>"); |
| 58 | 17 |
| 59 for (final test_ in _tests) { | 18 for (final test_ in results) { |
| 60 newBody.add(_toHtml(test_)); | 19 newBody.add(_toHtml(test_)); |
| 61 } | 20 } |
| 62 | 21 |
| 63 if (testsPassed == _tests.length) { | 22 if (testsPassed == results.length) { |
| 64 newBody.add(""" | 23 newBody.add(""" |
| 65 <tr><td colspan='3' class='unittest-pass'> | 24 <tr><td colspan='3' class='unittest-pass'> |
| 66 All ${testsPassed} tests passed | 25 All ${testsPassed} tests passed |
| 67 </td></tr>"""); | 26 </td></tr>"""); |
| 68 } else { | 27 } else { |
| 69 newBody.add(""" | 28 newBody.add(""" |
| 70 <tr><td colspan='3'>Total | 29 <tr><td colspan='3'>Total |
| 71 <span class='unittest-pass'>${testsPassed} passed</span>, | 30 <span class='unittest-pass'>${testsPassed} passed</span>, |
| 72 <span class='unittest-fail'>${testsFailed} failed</span> | 31 <span class='unittest-fail'>${testsFailed} failed</span> |
| 73 <span class='unittest-error'>${testsErrors} errors</span> | 32 <span class='unittest-error'>${testsErrors} errors</span> |
| 74 </td></tr>"""); | 33 </td></tr>"""); |
| 75 } | 34 } |
| 76 newBody.add("</tbody></table>"); | 35 newBody.add("</tbody></table>"); |
| 77 document.body.innerHTML = newBody.toString(); | 36 document.body.innerHTML = newBody.toString(); |
| 78 } | 37 } |
| 79 | |
| 80 window.postMessage('unittest-suite-done', '*'); | |
| 81 } | 38 } |
| 82 | 39 |
| 83 String _toHtml(TestCase test_) { | 40 String _toHtml(TestCase test_) { |
| 84 if (!test_.isComplete) { | 41 if (!test_.isComplete) { |
| 85 return ''' | 42 return ''' |
| 86 <tr> | 43 <tr> |
| 87 <td>${test_.id}</td> | 44 <td>${test_.id}</td> |
| 88 <td class="unittest-error">NO STATUS</td> | 45 <td class="unittest-error">NO STATUS</td> |
| 89 <td>Test did not complete</td> | 46 <td>Test did not complete</td> |
| 90 </tr>'''; | 47 </tr>'''; |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 103 } | 60 } |
| 104 | 61 |
| 105 return html; | 62 return html; |
| 106 } | 63 } |
| 107 | 64 |
| 108 //TODO(pquitslund): Move to a common lib | 65 //TODO(pquitslund): Move to a common lib |
| 109 String _htmlEscape(String string) { | 66 String _htmlEscape(String string) { |
| 110 return string.replaceAll('&', '&') | 67 return string.replaceAll('&', '&') |
| 111 .replaceAll('<','<') | 68 .replaceAll('<','<') |
| 112 .replaceAll('>','>'); | 69 .replaceAll('>','>'); |
| 113 } | 70 } |
| OLD | NEW |