| 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 'dart:async'; |
| 15 import 'package:unittest/unittest.dart'; | 15 import 'package:unittest/unittest.dart'; |
| 16 | 16 |
| 17 var tests; // array of test names | |
| 18 var expected; // array of test expected results (from buildStatusString) | |
| 19 var actual; // actual test results (from buildStatusString in config.onDone) | |
| 20 | |
| 21 Future _defer(void fn()) { | 17 Future _defer(void fn()) { |
| 22 return new Future.of(fn); | 18 return new Future.of(fn); |
| 23 } | 19 } |
| 24 | 20 |
| 25 String buildStatusString(int passed, int failed, int errors, | 21 String buildStatusString(int passed, int failed, int errors, |
| 26 var results, | 22 var results, |
| 27 {int count: 0, | 23 {int count: 0, |
| 28 String setup: '', String teardown: '', | 24 String setup: '', String teardown: '', |
| 29 String uncaughtError: null, | 25 String uncaughtError: null, |
| 30 String message: ''}) { | 26 String message: ''}) { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 count: count, setup: setup, teardown: teardown, | 60 count: count, setup: setup, teardown: teardown, |
| 65 uncaughtError: uncaughtError); | 61 uncaughtError: uncaughtError); |
| 66 } | 62 } |
| 67 | 63 |
| 68 void onDone(bool success) { | 64 void onDone(bool success) { |
| 69 _port.send(_result); | 65 _port.send(_result); |
| 70 } | 66 } |
| 71 } | 67 } |
| 72 | 68 |
| 73 runTest() { | 69 runTest() { |
| 74 port.receive((testName, sendport) { | 70 port.receive((String testName, sendport) { |
| 75 var _testconfig= new TestConfiguration(sendport); | 71 var _testconfig= new TestConfiguration(sendport); |
| 76 unittestConfiguration = _testconfig; | 72 unittestConfiguration = _testconfig; |
| 77 | 73 |
| 78 if (testName == 'single correct test') { | 74 if (testName == 'single correct test') { |
| 79 test(testName, () => expect(2 + 3, equals(5))); | 75 test(testName, () => expect(2 + 3, equals(5))); |
| 80 } else if (testName == 'single failing test') { | 76 } else if (testName == 'single failing test') { |
| 81 test(testName, () => expect(2 + 2, equals(5))); | 77 test(testName, () => expect(2 + 2, equals(5))); |
| 82 } else if (testName == 'exception test') { | 78 } else if (testName == 'exception test') { |
| 83 test(testName, () { throw new Exception('Fail.'); }); | 79 test(testName, () { throw new Exception('Fail.'); }); |
| 84 } else if (testName == 'group name test') { | 80 } else if (testName == 'group name test') { |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 }); | 292 }); |
| 297 } else if (testName == 'testCases immutable') { | 293 } else if (testName == 'testCases immutable') { |
| 298 test(testName, () { | 294 test(testName, () { |
| 299 expect(() => testCases.clear(), throwsUnsupportedError); | 295 expect(() => testCases.clear(), throwsUnsupportedError); |
| 300 expect(() => testCases.removeLast(), throwsUnsupportedError); | 296 expect(() => testCases.removeLast(), throwsUnsupportedError); |
| 301 }); | 297 }); |
| 302 } | 298 } |
| 303 }); | 299 }); |
| 304 } | 300 } |
| 305 | 301 |
| 306 void nextTest(int testNum) { | |
| 307 SendPort sport = spawnFunction(runTest); | |
| 308 sport.call(tests[testNum]).then((msg) { | |
| 309 actual.add(msg); | |
| 310 if (actual.length == expected.length) { | |
| 311 for (var i = 0; i < tests.length; i++) { | |
| 312 test(tests[i], () => expect(actual[i].trim(), equals(expected[i]))); | |
| 313 } | |
| 314 } else { | |
| 315 nextTest(testNum+1); | |
| 316 } | |
| 317 }); | |
| 318 } | |
| 319 | |
| 320 main() { | 302 main() { |
| 321 tests = [ | 303 var tests = { |
| 322 'single correct test', | 304 'single correct test': buildStatusString(1, 0, 0, 'single correct test'), |
| 323 'single failing test', | 305 'single failing test': buildStatusString(0, 1, 0, 'single failing test', |
| 324 'exception test', | |
| 325 'group name test', | |
| 326 'setup test', | |
| 327 'teardown test', | |
| 328 'setup and teardown test', | |
| 329 'correct callback test', | |
| 330 'excess callback test', | |
| 331 'completion test', | |
| 332 'async exception test', | |
| 333 'late exception test', | |
| 334 'middle exception test', | |
| 335 'async setup/teardown test', | |
| 336 'test returning future', | |
| 337 'test returning future using Timer', | |
| 338 'testCases immutable' | |
| 339 ]; | |
| 340 | |
| 341 expected = [ | |
| 342 buildStatusString(1, 0, 0, tests[0]), | |
| 343 buildStatusString(0, 1, 0, tests[1], | |
| 344 message: 'Expected: <5> but: was <4>.'), | 306 message: 'Expected: <5> but: was <4>.'), |
| 345 buildStatusString(0, 1, 0, tests[2], message: 'Caught Exception: Fail.'), | 307 'exception test': buildStatusString(0, 1, 0, 'exception test', |
| 346 buildStatusString(2, 0, 0, 'a a::a b b'), | 308 message: 'Caught Exception: Fail.'), |
| 347 buildStatusString(1, 0, 0, 'a ${tests[4]}', count: 0, setup: 'setup'), | 309 'group name test': buildStatusString(2, 0, 0, 'a a::a b b'), |
| 348 buildStatusString(1, 0, 0, 'a ${tests[5]}', count: 0, setup: '', | 310 'setup test': buildStatusString(1, 0, 0, 'a setup test', |
| 311 count: 0, setup: 'setup'), |
| 312 'teardown test': buildStatusString(1, 0, 0, 'a teardown test', |
| 313 count: 0, setup: '', teardown: 'teardown'), |
| 314 'setup and teardown test': buildStatusString(1, 0, 0, |
| 315 'a setup and teardown test', count: 0, setup: 'setup', |
| 349 teardown: 'teardown'), | 316 teardown: 'teardown'), |
| 350 buildStatusString(1, 0, 0, 'a ${tests[6]}', count: 0, | 317 'correct callback test': buildStatusString(1, 0, 0, 'correct callback test', |
| 351 setup: 'setup', teardown: 'teardown'), | 318 count: 1), |
| 352 buildStatusString(1, 0, 0, tests[7], count: 1), | 319 'excess callback test': buildStatusString(0, 1, 0, 'excess callback test', |
| 353 buildStatusString(0, 1, 0, tests[8], count: 1, | 320 count: 1, message: 'Callback called more times than expected (1).'), |
| 354 message: 'Callback called more times than expected (1).'), | 321 'completion test': buildStatusString(1, 0, 0, 'completion test', count: 10), |
| 355 buildStatusString(1, 0, 0, tests[9], count: 10), | 322 'async exception test': buildStatusString(0, 1, 0, 'async exception test', |
| 356 buildStatusString(0, 1, 0, tests[10], message: 'Caught error!'), | 323 message: 'Caught error!'), |
| 357 buildStatusString(1, 0, 1, 'testOne', | 324 'late exception test': buildStatusString(1, 0, 1, 'testOne', |
| 358 message: 'Callback called (2) after test case testOne has already ' | 325 message: 'Callback called (2) after test case testOne has already ' |
| 359 'been marked as pass.:testTwo:'), | 326 'been marked as pass.:testTwo:'), |
| 360 buildStatusString(2, 1, 0, | 327 'middle exception test': buildStatusString(2, 1, 0, |
| 361 'testOne::testTwo:Expected: false but: was <true>.:testThree'), | 328 'testOne::testTwo:Expected: false but: was <true>.:testThree'), |
| 362 buildStatusString(2, 0, 3, | 329 'async setup/teardown test': buildStatusString(2, 0, 3, |
| 363 'good setup/good teardown foo1::' | 330 'good setup/good teardown foo1::' |
| 364 'good setup/bad teardown foo2:good setup/bad teardown ' | 331 'good setup/bad teardown foo2:good setup/bad teardown ' |
| 365 'foo2: Test teardown failed: Failed to complete tearDown:' | 332 'foo2: Test teardown failed: Failed to complete tearDown:' |
| 366 'bad setup/good teardown foo3:bad setup/good teardown ' | 333 'bad setup/good teardown foo3:bad setup/good teardown ' |
| 367 'foo3: Test setup failed: Failed to complete setUp:' | 334 'foo3: Test setup failed: Failed to complete setUp:' |
| 368 'bad setup/bad teardown foo4:bad setup/bad teardown ' | 335 'bad setup/bad teardown foo4:bad setup/bad teardown ' |
| 369 'foo4: Test teardown failed: Failed to complete tearDown:' | 336 'foo4: Test teardown failed: Failed to complete tearDown:' |
| 370 'post groups'), | 337 'post groups'), |
| 371 buildStatusString(2, 4, 0, | 338 'test returning future': buildStatusString(2, 4, 0, |
| 372 'successful::' | 339 'successful::' |
| 373 'error1:Callback called more times than expected (1).:' | 340 'error1:Callback called more times than expected (1).:' |
| 374 'fail1:Expected: <false> but: was <true>.:' | 341 'fail1:Expected: <false> but: was <true>.:' |
| 375 'error2:Callback called more times than expected (1).:' | 342 'error2:Callback called more times than expected (1).:' |
| 376 'fail2:failure:' | 343 'fail2:failure:' |
| 377 'foo5'), | 344 'foo5'), |
| 378 buildStatusString(2, 4, 0, | 345 'test returning future using Timer': buildStatusString(2, 4, 0, |
| 379 'successful::' | 346 'successful::' |
| 380 'fail1:Expected: <false> but: was <true>.:' | 347 'fail1:Expected: <false> but: was <true>.:' |
| 381 'error1:Callback called more times than expected (1).:' | 348 'error1:Callback called more times than expected (1).:' |
| 382 'fail2:failure:' | 349 'fail2:failure:' |
| 383 'error2:Callback called more times than expected (1).:' | 350 'error2:Callback called more times than expected (1).:' |
| 384 'foo6'), | 351 'foo6'), |
| 385 buildStatusString(1, 0, 0, 'testCases immutable'), | 352 'testCases immutable': |
| 386 ]; | 353 buildStatusString(1, 0, 0, 'testCases immutable'), |
| 354 }; |
| 387 | 355 |
| 388 actual = []; | 356 tests.forEach((String name, String expected) { |
| 389 | 357 test(name, () => spawnFunction(runTest) |
| 390 nextTest(0); | 358 .call(name) |
| 359 .then((String msg) => expect(msg.trim(), equals(expected)))); |
| 360 }); |
| 391 } | 361 } |
| 392 | 362 |
| OLD | NEW |