OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * A simple unit test library for running tests inside the DARTest In-App test |
| 7 * runner along side the application under test in a browser. |
| 8 */ |
| 9 #library("unittest"); |
| 10 |
| 11 #import("dart:dom"); |
| 12 |
| 13 #source("shared.dart"); |
| 14 |
| 15 /** Getter so that the DARTest UI can access tests. */ |
| 16 List<TestCase> get tests() => _tests; |
| 17 |
| 18 int testsRun = 0; |
| 19 int testsFailed = 0; |
| 20 int testsErrors = 0; |
| 21 |
| 22 bool previousAsyncTest = false; |
| 23 |
| 24 Function updateUI = null; |
| 25 |
| 26 Function dartestLogger = null; |
| 27 |
| 28 _platformDefer(void callback()) { |
| 29 _testRunner = runDartests; |
| 30 // DARTest ignores the callback. Tests are launched from UI. |
| 31 } |
| 32 |
| 33 // Update test results |
| 34 updateTestStats(TestCase test) { |
| 35 assert(test.result != null); |
| 36 if(test.startTime != null) { |
| 37 test.runningTime = (new Date.now()).difference(test.startTime); |
| 38 } |
| 39 testsRun++; |
| 40 switch (test.result) { |
| 41 case 'fail': testsFailed++; break; |
| 42 case 'error': testsErrors++; break; |
| 43 } |
| 44 updateUI(test); |
| 45 } |
| 46 |
| 47 // Run tests sequentially |
| 48 runDartests() { |
| 49 if(previousAsyncTest) { |
| 50 updateTestStats(_tests[_currentTest - 1]); |
| 51 previousAsyncTest = false; |
| 52 } |
| 53 if(_currentTest < _tests.length) { |
| 54 final testCase = _tests[_currentTest]; |
| 55 dartestLogger('Running test:' + testCase.description); |
| 56 testCase.startTime = new Date.now(); |
| 57 _runTest(testCase); |
| 58 if (!testCase.isComplete && testCase.callbacks > 0) { |
| 59 previousAsyncTest = true; |
| 60 return; |
| 61 } |
| 62 updateTestStats(testCase); |
| 63 _currentTest++; |
| 64 window.setTimeout(runDartests, 0); |
| 65 } |
| 66 } |
| 67 |
| 68 _platformStartTests() { |
| 69 // TODO(shauvik): Support for VM and command line coming soon! |
| 70 window.console.log("Warning: Running DARTest from VM or Command-line."); |
| 71 } |
| 72 |
| 73 _platformInitialize() { |
| 74 // Do nothing |
| 75 } |
| 76 |
| 77 _platformCompleteTests(int testsPassed, int testsFailed, int testsErrors) { |
| 78 // Do nothing |
| 79 } |
| 80 |
| 81 String getTestResultsCsv() { |
| 82 StringBuffer out = new StringBuffer(); |
| 83 _tests.forEach((final test) { |
| 84 String result = 'none'; |
| 85 if(test.result != null) { |
| 86 result = test.result.toUpperCase(); |
| 87 } |
| 88 out.add('${test.id}, "${test.description}", $result\n'); |
| 89 }); |
| 90 return out.toString(); |
| 91 } |
OLD | NEW |