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. | 6 * A simple unit test library. |
| 7 */ | 7 */ |
| 8 #library("unittest"); | 8 #library("unittest"); |
| 9 | 9 |
| 10 #import("dart:dom"); | 10 #import("dart:dom"); |
| 11 | 11 |
| 12 #source("unittestsuite.dart"); | 12 #source("shared.dart"); |
| 13 | |
| 14 // TODO(rnystrom): Get rid of this if we get canonical closures for methods. | |
| 15 EventListener _onErrorClosure; | |
| 16 | |
| 17 _initializePlatform() { | |
|
Siggi Cherem (dart-lang)
2011/10/28 23:35:13
suggestion - what if we create an abstraction for
Bob Nystrom
2011/10/28 23:57:48
When would that field get initialized? Would the P
Siggi Cherem (dart-lang)
2011/10/29 00:13:19
Another idea ---
if you'd like to avoid adding cl
Siggi Cherem (dart-lang)
2011/10/29 00:13:19
Basically yes, it would be a const class - here is
| |
| 18 _onErrorClosure = (e) { _onError(e); }; | |
| 19 } | |
| 20 | |
| 21 _doLaterPlatform(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.recordError('(DOM callback has errors) Caught ${e}', ''); | |
| 31 _state = _UNCAUGHT_ERROR; | |
| 32 if (testCase.callbacks > 0) { | |
| 33 _currentTest++; | |
| 34 _nextBatch(); | |
| 35 } | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 /** Runs all queued tests, one at a time. */ | |
| 40 _runTestsPlatform() { | |
| 41 window.postMessage('unittest-suite-start', '*'); | |
| 42 | |
| 43 // Listen for uncaught errors. | |
| 44 window.onerror = _onErrorClosure; | |
| 45 } | |
| 46 | |
| 47 _completeTestsPlatform(int testsPassed, int testsFailed, int testsErrors) { | |
| 48 window.onerror = null; | |
| 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("<tr><td colspan='3' class='unittest-pass'>All " | |
| 65 + testsPassed + " tests passed</td></tr>"); | |
| 66 } else { | |
| 67 newBody.add(""" | |
| 68 <tr><td colspan='3'>Total | |
| 69 <span class='unittest-pass'>${testsPassed} passed</span>, | |
| 70 <span class='unittest-fail'>${testsFailed} failed</span> | |
| 71 <span class='unittest-error'>${testsErrors} errors</span> | |
| 72 </td></tr>"""); | |
| 73 } | |
| 74 newBody.add("</tbody></table>"); | |
| 75 document.body.innerHTML = newBody.toString(); | |
| 76 } | |
| 77 | |
| 78 window.postMessage('unittest-suite-done', '*'); | |
| 79 } | |
| 80 | |
| 81 String _toHtml(TestCase test) { | |
| 82 if (!test.isComplete) { | |
| 83 return ''' | |
| 84 <tr> | |
| 85 <td>${test.id}</td> | |
| 86 <td class="unittest-error">NO STATUS</td> | |
| 87 <td>Test did not complete</td> | |
| 88 </tr>'''; | |
| 89 } | |
| 90 | |
| 91 var html = ''' | |
| 92 <tr> | |
| 93 <td>${test.id}</td> | |
| 94 <td class="unittest-${test.result}">${test.result.toUpperCase()}</td> | |
| 95 <td>Expectation: ${test.description}. ${test.message}</td> | |
| 96 </tr>'''; | |
| 97 | |
| 98 if (test.stackTrace != null) { | |
| 99 html += | |
| 100 '<tr><td></td><td colspan="2"><pre>${test.stackTrace}</pre></td></tr>'; | |
| 101 } | |
| 102 | |
| 103 return html; | |
| 104 } | |
| OLD | NEW |