| OLD | NEW |
| (Empty) | |
| 1 #!/usr/bin/env dart |
| 2 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 3 // for details. All rights reserved. Use of this source code is governed by a |
| 4 // BSD-style license that can be found in the LICENSE file. |
| 5 |
| 6 #library("DUnit"); |
| 7 |
| 8 typedef void Test(); |
| 9 typedef TestResult SynchTest(); |
| 10 typedef Future<TestResult> AsynchTest(); |
| 11 |
| 12 class TestSuite { |
| 13 TestSuite() : _tests = <SynchTest>[]; |
| 14 |
| 15 void registerTestClass(TestClass tests) { |
| 16 tests.registerTests(this); |
| 17 } |
| 18 |
| 19 void _registerTest(SynchTest test) { |
| 20 _tests.add(test); |
| 21 } |
| 22 |
| 23 void run() { |
| 24 reportResults(runTests()); |
| 25 } |
| 26 |
| 27 List<TestResult> runTests() { |
| 28 List<TestResult> results = <TestResult>[]; |
| 29 for(Function test in _tests) { |
| 30 results.add(test()); |
| 31 } |
| 32 return results; |
| 33 } |
| 34 |
| 35 void reportResults(List<TestResult> results) { |
| 36 if(results.every(bool _(TestResult r) => r is PassedTest)) { |
| 37 print("OK -- ALL TESTS PASS (${results.length} run)"); |
| 38 } else { |
| 39 for(TestResult r in |
| 40 results.filter(bool _(TestResult r) => !(r is PassedTest))) { |
| 41 print(r); |
| 42 } |
| 43 int passedTests = |
| 44 results.filter(bool _(TestResult r) => r is PassedTest).length; |
| 45 int failures = |
| 46 results.filter(bool _(TestResult r) => r is FailedTest).length; |
| 47 int errors = |
| 48 results.filter(bool _(TestResult r) => r is TestError).length; |
| 49 print("FAIL -- TESTS RUN: ${results.length}"); |
| 50 print(" PASSED: ${passedTests}"); |
| 51 print(" FAILED: ${failures}"); |
| 52 print(" ERRORS: ${errors}"); |
| 53 } |
| 54 } |
| 55 |
| 56 List<SynchTest> _tests; |
| 57 } |
| 58 |
| 59 interface TestResult { |
| 60 String get testDescription(); |
| 61 } |
| 62 |
| 63 class PassedTest implements TestResult { |
| 64 const PassedTest(String this._testDescription); |
| 65 String get testDescription() => _testDescription; |
| 66 final String _testDescription; |
| 67 String toString() => _testDescription; |
| 68 } |
| 69 |
| 70 class _ExceptionResult { |
| 71 const _ExceptionResult(String this._testDescription, var this._exception); |
| 72 |
| 73 String get testDescription() => _testDescription; |
| 74 final String _testDescription; |
| 75 |
| 76 Object get exception() => _exception; |
| 77 final _exception; |
| 78 } |
| 79 |
| 80 class FailedTest extends _ExceptionResult implements TestResult { |
| 81 FailedTest(String testDescription, var exception) : |
| 82 super(testDescription, exception); |
| 83 |
| 84 String toString() => ">>> Test failure in ${_testDescription} " + |
| 85 "with:\n${exception}\n"; |
| 86 } |
| 87 |
| 88 class TestError extends _ExceptionResult implements TestResult { |
| 89 TestError(String testDescription, var exception) : |
| 90 super(testDescription, exception); |
| 91 |
| 92 String toString() => ">>> Test error caught in " + |
| 93 "${_testDescription} with:\n${exception}\n"; |
| 94 } |
| 95 |
| 96 class TestClass { |
| 97 void register(String description, Function test, TestSuite suite) { |
| 98 suite._registerTest(TestResult _() { |
| 99 setUp(); |
| 100 try { |
| 101 test(); |
| 102 tearDown(); |
| 103 return new PassedTest(description); |
| 104 } catch (ExpectException x) { |
| 105 tearDown(); |
| 106 return new FailedTest(description, x); |
| 107 } catch (var x) { |
| 108 tearDown(); |
| 109 return new TestError(description, x); |
| 110 } |
| 111 }); |
| 112 } |
| 113 |
| 114 abstract void registerTests(TestSuite suite); |
| 115 void setUp() {} |
| 116 void tearDown() {} |
| 117 } |
| OLD | NEW |