| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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 // TODO(gram): | 5 // TODO(gram): |
| 6 // Unfortunately I can't seem to test anything that involves timeouts, e.g. | 6 // Unfortunately I can't seem to test anything that involves timeouts, e.g. |
| 7 // insufficient callbacks, because the timeout is controlled externally | 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 | 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. | 9 // so the outer timeout doesn't fire. So I removed all such tests. |
| 10 // I'd like to revisit this at some point. | 10 // I'd like to revisit this at some point. |
| 11 | 11 |
| 12 library unittestTest; | 12 library unittestTest; |
| 13 import 'dart:isolate'; | 13 import 'dart:isolate'; |
| 14 import 'dart:async'; |
| 14 import 'package:unittest/unittest.dart'; | 15 import 'package:unittest/unittest.dart'; |
| 15 | 16 |
| 16 var tests; // array of test names | 17 var tests; // array of test names |
| 17 var expected; // array of test expected results (from buildStatusString) | 18 var expected; // array of test expected results (from buildStatusString) |
| 18 var actual; // actual test results (from buildStatusString in config.onDone) | 19 var actual; // actual test results (from buildStatusString in config.onDone) |
| 19 var _testconfig; // test configuration to capture onDone | 20 var _testconfig; // test configuration to capture onDone |
| 20 | 21 |
| 21 _defer(void fn()) { | 22 _defer(void fn()) { |
| 22 // Exploit isolate ports as a platform-independent mechanism to queue a | 23 // 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 // message at the end of the event loop. Stolen from unittest.dart. |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 } else if (testName == 'middle exception test') { | 148 } else if (testName == 'middle exception test') { |
| 148 test('testOne', () { expect(true, isTrue); }); | 149 test('testOne', () { expect(true, isTrue); }); |
| 149 test('testTwo', () { expect(true, isFalse); }); | 150 test('testTwo', () { expect(true, isFalse); }); |
| 150 test('testThree', () { | 151 test('testThree', () { |
| 151 var done = expectAsync0((){}); | 152 var done = expectAsync0((){}); |
| 152 _defer(() { | 153 _defer(() { |
| 153 expect(true, isTrue); | 154 expect(true, isTrue); |
| 154 done(); | 155 done(); |
| 155 }); | 156 }); |
| 156 }); | 157 }); |
| 158 } else if (testName == 'async setup/teardown test') { |
| 159 group('good setup/good teardown', () { |
| 160 setUp(() { |
| 161 var completer = new Completer(); |
| 162 _defer(() { |
| 163 completer.complete(0); |
| 164 }); |
| 165 return completer.future; |
| 166 }); |
| 167 tearDown(() { |
| 168 var completer = new Completer(); |
| 169 _defer(() { |
| 170 completer.complete(0); |
| 171 }); |
| 172 return completer.future; |
| 173 }); |
| 174 test('foo1', (){}); |
| 175 }); |
| 176 group('good setup/bad teardown', () { |
| 177 setUp(() { |
| 178 var completer = new Completer(); |
| 179 _defer(() { |
| 180 completer.complete(0); |
| 181 }); |
| 182 return completer.future; |
| 183 }); |
| 184 tearDown(() { |
| 185 var completer = new Completer(); |
| 186 _defer(() { |
| 187 //throw "Failed to complete tearDown"; |
| 188 completer.completeError( |
| 189 new AsyncError("Failed to complete tearDown")); |
| 190 }); |
| 191 return completer.future; |
| 192 }); |
| 193 test('foo2', (){}); |
| 194 }); |
| 195 group('bad setup/good teardown', () { |
| 196 setUp(() { |
| 197 var completer = new Completer(); |
| 198 _defer(() { |
| 199 //throw "Failed to complete setUp"; |
| 200 completer.completeError(new AsyncError("Failed to complete setUp")); |
| 201 }); |
| 202 return completer.future; |
| 203 }); |
| 204 tearDown(() { |
| 205 var completer = new Completer(); |
| 206 _defer(() { |
| 207 completer.complete(0); |
| 208 }); |
| 209 return completer.future; |
| 210 }); |
| 211 test('foo3', (){}); |
| 212 }); |
| 213 group('bad setup/bad teardown', () { |
| 214 setUp(() { |
| 215 var completer = new Completer(); |
| 216 _defer(() { |
| 217 //throw "Failed to complete setUp"; |
| 218 completer.completeError(new AsyncError("Failed to complete setUp")); |
| 219 }); |
| 220 return completer.future; |
| 221 }); |
| 222 tearDown(() { |
| 223 var completer = new Completer(); |
| 224 _defer(() { |
| 225 //throw "Failed to complete tearDown"; |
| 226 completer.completeError( |
| 227 new AsyncError("Failed to complete tearDown")); |
| 228 }); |
| 229 return completer.future; |
| 230 }); |
| 231 test('foo4', (){}); |
| 232 }); |
| 233 // The next test is just to make sure we make steady progress |
| 234 // through the tests. |
| 235 test('post groups', () {}); |
| 157 } | 236 } |
| 158 }); | 237 }); |
| 159 } | 238 } |
| 160 | 239 |
| 161 void nextTest(int testNum) { | 240 void nextTest(int testNum) { |
| 162 SendPort sport = spawnFunction(runTest); | 241 SendPort sport = spawnFunction(runTest); |
| 163 sport.call(tests[testNum]).then((msg) { | 242 sport.call(tests[testNum]).then((msg) { |
| 164 actual.add(msg); | 243 actual.add(msg); |
| 165 if (actual.length == expected.length) { | 244 if (actual.length == expected.length) { |
| 166 for (var i = 0; i < tests.length; i++) { | 245 for (var i = 0; i < tests.length; i++) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 179 'exception test', | 258 'exception test', |
| 180 'group name test', | 259 'group name test', |
| 181 'setup test', | 260 'setup test', |
| 182 'teardown test', | 261 'teardown test', |
| 183 'setup and teardown test', | 262 'setup and teardown test', |
| 184 'correct callback test', | 263 'correct callback test', |
| 185 'excess callback test', | 264 'excess callback test', |
| 186 'completion test', | 265 'completion test', |
| 187 'async exception test', | 266 'async exception test', |
| 188 'late exception test', | 267 'late exception test', |
| 189 'middle exception test' | 268 'middle exception test', |
| 269 'async setup/teardown test' |
| 190 ]; | 270 ]; |
| 191 | 271 |
| 192 expected = [ | 272 expected = [ |
| 193 buildStatusString(1, 0, 0, tests[0]), | 273 buildStatusString(1, 0, 0, tests[0]), |
| 194 buildStatusString(0, 1, 0, tests[1], | 274 buildStatusString(0, 1, 0, tests[1], |
| 195 message: 'Expected: <5> but: was <4>.'), | 275 message: 'Expected: <5> but: was <4>.'), |
| 196 buildStatusString(0, 1, 0, tests[2], message: 'Caught Exception: Fail.'), | 276 buildStatusString(0, 1, 0, tests[2], message: 'Caught Exception: Fail.'), |
| 197 buildStatusString(2, 0, 0, 'a a::a b b'), | 277 buildStatusString(2, 0, 0, 'a a::a b b'), |
| 198 buildStatusString(1, 0, 0, 'a ${tests[4]}', count: 0, setup: 'setup'), | 278 buildStatusString(1, 0, 0, 'a ${tests[4]}', count: 0, setup: 'setup'), |
| 199 buildStatusString(1, 0, 0, 'a ${tests[5]}', count: 0, setup: '', | 279 buildStatusString(1, 0, 0, 'a ${tests[5]}', count: 0, setup: '', |
| 200 teardown: 'teardown'), | 280 teardown: 'teardown'), |
| 201 buildStatusString(1, 0, 0, 'a ${tests[6]}', count: 0, | 281 buildStatusString(1, 0, 0, 'a ${tests[6]}', count: 0, |
| 202 setup: 'setup', teardown: 'teardown'), | 282 setup: 'setup', teardown: 'teardown'), |
| 203 buildStatusString(1, 0, 0, tests[7], count: 1), | 283 buildStatusString(1, 0, 0, tests[7], count: 1), |
| 204 buildStatusString(0, 0, 1, tests[8], count: 1, | 284 buildStatusString(0, 0, 1, tests[8], count: 1, |
| 205 message: 'Callback called more times than expected (2 > 1).'), | 285 message: 'Callback called more times than expected (2 > 1).'), |
| 206 buildStatusString(1, 0, 0, tests[9], count: 10), | 286 buildStatusString(1, 0, 0, tests[9], count: 10), |
| 207 buildStatusString(0, 1, 0, tests[10], message: 'Caught error!'), | 287 buildStatusString(0, 1, 0, tests[10], message: 'Caught error!'), |
| 208 buildStatusString(1, 0, 1, 'testOne', message: 'Callback called after alread
y being marked as done (1).:testTwo:'), | 288 buildStatusString(1, 0, 1, 'testOne', |
| 209 buildStatusString(2, 1, 0, 'testOne::testTwo:Expected: false but: was <true>
.:testThree') | 289 message: 'Callback called after already being marked as done ' |
| 290 '(1).:testTwo:'), |
| 291 buildStatusString(2, 1, 0, |
| 292 'testOne::testTwo:Expected: false but: was <true>.:testThree'), |
| 293 buildStatusString(2, 0, 3, |
| 294 'good setup/good teardown foo1::' |
| 295 'good setup/bad teardown foo2:good setup/bad teardown ' |
| 296 'foo2: Test teardown failed: Failed to complete tearDown:' |
| 297 'bad setup/good teardown foo3:bad setup/good teardown ' |
| 298 'foo3: Test setup failed: Failed to complete setUp:' |
| 299 'bad setup/bad teardown foo4:bad setup/bad teardown ' |
| 300 'foo4: Test teardown failed: Failed to complete tearDown:' |
| 301 'post groups') |
| 210 ]; | 302 ]; |
| 211 | 303 |
| 212 actual = []; | 304 actual = []; |
| 213 | 305 |
| 214 nextTest(0); | 306 nextTest(0); |
| 215 } | 307 } |
| 216 | 308 |
| OLD | NEW |