Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012, 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 // TODO(gram): | |
| 6 // Unfortunately I can't seem to test anything that involves timeouts, e.g. | |
| 7 // insufficient callbacks, because the timeout is controlled externally | |
| 8 // (test.dart?), and we would need to use a shorter timeout for the inner tests | |
| 9 // so the outer timeout doesn't fire. So I removed all such tests. | |
| 10 // I'd like to revisit this at some point. | |
| 11 | |
| 12 #library('unittestTest'); | |
| 13 #import('dart:isolate'); | |
| 14 #import('../../../lib/unittest/unittest.dart'); | |
| 15 | |
| 16 var tests; | |
|
eub
2012/06/21 17:52:20
I forgot these - brief comments please? And I lik
| |
| 17 var expected; | |
| 18 var actual; | |
| 19 var _testconfig; | |
| 20 | |
| 21 _defer(void fn()) { | |
| 22 // Exploit isolate ports as a platform-independent mechanism to queue a | |
| 23 // message at the end of the event loop. Stolen from unittest.dart. | |
| 24 final port = new ReceivePort(); | |
| 25 port.receive((msg, reply) { | |
| 26 fn(); | |
| 27 port.close(); | |
| 28 }); | |
| 29 port.toSendPort().send(null, null); | |
| 30 } | |
| 31 | |
| 32 String buildStatusString(int passed, int failed, int errors, | |
| 33 var results, | |
| 34 [int x = 0, String setup = '', String teardown = '', | |
| 35 String uncaughtError = null, | |
| 36 String message = '']) { | |
| 37 var totalTests = 0; | |
| 38 String testDetails = ''; | |
| 39 if (results is String) { | |
| 40 totalTests = passed + failed + errors; | |
| 41 testDetails = ':$results:$message'; | |
| 42 } else { | |
| 43 totalTests = results.length; | |
| 44 for (var i = 0; i < results.length; i++) { | |
| 45 testDetails = '$testDetails:${results[i].description}:' | |
| 46 '${collapseWhitespace(results[i].message)}'; | |
| 47 } | |
| 48 } | |
| 49 var result = '$passed:$failed:$errors:$totalTests:$x:' | |
| 50 '$setup:$teardown:$uncaughtError$testDetails'; | |
| 51 return result; | |
| 52 } | |
| 53 | |
| 54 class TestConfiguration extends Configuration { | |
| 55 int x = 0; | |
|
eub
2012/06/21 17:52:20
First three members could use a comment that they'
| |
| 56 String setup = ''; | |
| 57 String teardown = ''; | |
| 58 SendPort _port; | |
| 59 | |
| 60 TestConfiguration(this._port); | |
| 61 | |
| 62 void onDone(int passed, int failed, int errors, List<TestCase> results, | |
| 63 String uncaughtError) { | |
| 64 var result = buildStatusString(passed, failed, errors, results, x, | |
| 65 setup, teardown, uncaughtError); | |
| 66 _port.send(result); | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 runTest() { | |
| 71 port.receive((testName, sendport) { | |
| 72 configure(_testconfig = new TestConfiguration(sendport)); | |
| 73 if (testName == 'single correct test') { | |
| 74 test(testName, () => expect(2 + 3, equals(5))); | |
| 75 } else if (testName == 'single failing test') { | |
| 76 test(testName, () => expect(2 + 2, equals(5))); | |
| 77 } else if (testName == 'exception test') { | |
| 78 test(testName, () { throw new Exception('fail'); }); | |
| 79 } else if (testName == 'group name test') { | |
| 80 group('a', () { | |
| 81 test('a', () {}); | |
| 82 group('b', () { | |
| 83 test('b', () {}); | |
| 84 }); | |
| 85 }); | |
| 86 } else if (testName == 'setup test') { | |
| 87 group('a', () { | |
| 88 setUp(() { _testconfig.setup = 'setup'; }); | |
| 89 test(testName, () {}); | |
| 90 }); | |
| 91 } else if (testName == 'teardown test') { | |
| 92 group('a', () { | |
| 93 tearDown(() { _testconfig.teardown = 'teardown'; }); | |
| 94 test(testName, () {}); | |
| 95 }); | |
| 96 } else if (testName == 'setup and teardown test') { | |
| 97 group('a', () { | |
| 98 setUp(() { _testconfig.setup = 'setup'; }); | |
| 99 tearDown(() { _testconfig.teardown = 'teardown'; }); | |
| 100 test(testName, () {}); | |
| 101 }); | |
| 102 } else if (testName == 'correct callback test') { | |
| 103 test(testName, | |
| 104 () =>_defer(expectAsync0((){ ++_testconfig.x;}))); | |
| 105 } else if (testName == 'excess callback test') { | |
| 106 test(testName, () { | |
| 107 var _callback = expectAsync0((){ ++_testconfig.x;}); | |
| 108 _defer(_callback); | |
| 109 _defer(_callback); | |
| 110 }); | |
| 111 } else if (testName == 'completion test') { | |
| 112 test(testName, () { | |
| 113 var _callback; | |
| 114 _callback = expectAsyncUntil0(() { | |
| 115 if (++_testconfig.x < 10) { | |
| 116 _defer(_callback); | |
| 117 } | |
| 118 }, | |
| 119 () => (_testconfig.x == 10)); | |
| 120 _defer(_callback); | |
| 121 }); | |
| 122 } | |
| 123 }); | |
| 124 } | |
| 125 | |
| 126 void nextTest(int testNum) { | |
| 127 SendPort sport = spawnFunction(runTest); | |
| 128 sport.call(tests[testNum]).then((msg) { | |
| 129 actual.add(msg); | |
| 130 if (actual.length == expected.length) { | |
| 131 for (var i = 0; i < tests.length; i++) { | |
| 132 test(tests[i], () => expect(actual[i], equals(expected[i]))); | |
| 133 } | |
| 134 } else { | |
| 135 nextTest(testNum+1); | |
| 136 } | |
| 137 }); | |
| 138 } | |
| 139 | |
| 140 main() { | |
| 141 tests = [ | |
| 142 'single correct test', | |
| 143 'single failing test', | |
| 144 'exception test', | |
| 145 'group name test', | |
| 146 'setup test', | |
| 147 'teardown test', | |
| 148 'setup and teardown test', | |
| 149 'correct callback test', | |
| 150 'excess callback test', | |
| 151 'completion test' | |
| 152 ]; | |
| 153 | |
| 154 expected = [ | |
| 155 buildStatusString(1, 0, 0, tests[0]), | |
| 156 buildStatusString(0, 1, 0, tests[1], message: 'Expected: <5> but: was <4>'), | |
| 157 buildStatusString(0, 1, 0, tests[2], message: 'Caught Exception: fail'), | |
| 158 buildStatusString(2, 0, 0, 'a a::a b b'), | |
| 159 buildStatusString(1, 0, 0, 'a ${tests[4]}', 0, 'setup'), | |
| 160 buildStatusString(1, 0, 0, 'a ${tests[5]}', 0, '', 'teardown'), | |
| 161 buildStatusString(1, 0, 0, 'a ${tests[6]}', 0, | |
| 162 'setup', 'teardown'), | |
| 163 buildStatusString(1, 0, 0, tests[7], 1), | |
| 164 buildStatusString(0, 0, 1, tests[8], 1, | |
| 165 message: 'Callback called more times than expected (2 > 1)'), | |
| 166 buildStatusString(1, 0, 0, tests[9], 10) | |
| 167 ]; | |
| 168 | |
| 169 actual = []; | |
| 170 | |
| 171 nextTest(0); | |
| 172 } | |
| 173 | |
| OLD | NEW |