Chromium Code Reviews| 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 Future _defer(void fn()) { | 17 Future _defer(void fn()) { |
| 18 return new Future.of(fn); | 18 return new Future.of(fn); |
| 19 } | 19 } |
| 20 | 20 |
| 21 String buildStatusString(int passed, int failed, int errors, | 21 String buildStatusString(int passed, int failed, int errors, |
| 22 var results, | 22 var results, |
| 23 {int count: 0, | 23 {int count: 0, |
| 24 String setup: '', String teardown: '', | 24 String setup: '', String teardown: '', |
| 25 String uncaughtError: null, | 25 String uncaughtError: null, |
| 26 String message: ''}) { | 26 String message: ''}) { |
| 27 var totalTests = 0; | 27 var totalTests = 0; |
| 28 String testDetails = ''; | 28 var testDetails = new StringBuffer(); |
| 29 if (results is String) { | 29 if(results == null) { |
| 30 // no op | |
| 31 assert(message == ''); | |
| 32 } else if (results is String) { | |
| 30 totalTests = passed + failed + errors; | 33 totalTests = passed + failed + errors; |
| 31 testDetails = ':$results:$message'; | 34 testDetails.write(':$results:$message'); |
| 32 } else { | 35 } else { |
| 33 totalTests = results.length; | 36 totalTests = results.length; |
| 34 for (var i = 0; i < results.length; i++) { | 37 for (var i = 0; i < results.length; i++) { |
| 35 testDetails = '$testDetails:${results[i].description}:' | 38 testDetails.write(':${results[i].description}:' |
| 36 '${collapseWhitespace(results[i].message)}'; | 39 '${collapseWhitespace(results[i].message)}'); |
| 37 } | 40 } |
| 38 } | 41 } |
| 39 var result = '$passed:$failed:$errors:$totalTests:$count:' | 42 return '$passed:$failed:$errors:$totalTests:$count:' |
| 40 '$setup:$teardown:$uncaughtError$testDetails'; | 43 '$setup:$teardown:$uncaughtError$testDetails'; |
| 41 return result; | |
| 42 } | 44 } |
| 43 | 45 |
| 44 class TestConfiguration extends Configuration { | 46 class TestConfiguration extends Configuration { |
| 45 | 47 |
| 46 // Some test state that is captured | 48 // Some test state that is captured |
| 47 int count = 0; // a count of callbacks | 49 int count = 0; // a count of callbacks |
| 48 String setup = ''; // the name of the test group setup function, if any | 50 String setup = ''; // the name of the test group setup function, if any |
| 49 String teardown = ''; // the name of the test group teardown function, if any | 51 String teardown = ''; // the name of the test group teardown function, if any |
| 50 | 52 |
| 51 // The port to communicate with the parent isolate | 53 // The port to communicate with the parent isolate |
| 52 final SendPort _port; | 54 final SendPort _port; |
| 53 String _result; | 55 String _result; |
| 54 | 56 |
| 55 TestConfiguration(this._port); | 57 TestConfiguration(this._port); |
| 56 | 58 |
| 57 void onSummary(int passed, int failed, int errors, List<TestCase> results, | 59 void onSummary(int passed, int failed, int errors, List<TestCase> results, |
| 58 String uncaughtError) { | 60 String uncaughtError) { |
| 59 _result = buildStatusString(passed, failed, errors, results, | 61 _result = buildStatusString(passed, failed, errors, results, |
| 60 count: count, setup: setup, teardown: teardown, | 62 count: count, setup: setup, teardown: teardown, |
| 61 uncaughtError: uncaughtError); | 63 uncaughtError: uncaughtError); |
| 62 } | 64 } |
| 63 | 65 |
| 64 void onDone(bool success) { | 66 void onDone(bool success) { |
| 65 _port.send(_result); | 67 _port.send(_result); |
| 66 } | 68 } |
| 67 } | 69 } |
| 68 | 70 |
| 69 runTest() { | 71 runTest() { |
| 70 port.receive((String testName, sendport) { | 72 port.receive((String testName, sendport) { |
| 71 var _testconfig= new TestConfiguration(sendport); | 73 var testConfig= new TestConfiguration(sendport); |
|
Siggi Cherem (dart-lang)
2013/04/04 00:05:24
while you are here, can you add a space before the
| |
| 72 unittestConfiguration = _testconfig; | 74 unittestConfiguration = testConfig; |
| 73 | 75 |
| 74 if (testName == 'single correct test') { | 76 if (testName == 'single correct test') { |
| 75 test(testName, () => expect(2 + 3, equals(5))); | 77 test(testName, () => expect(2 + 3, equals(5))); |
| 76 } else if (testName == 'single failing test') { | 78 } else if (testName == 'single failing test') { |
| 77 test(testName, () => expect(2 + 2, equals(5))); | 79 test(testName, () => expect(2 + 2, equals(5))); |
| 78 } else if (testName == 'exception test') { | 80 } else if (testName == 'exception test') { |
| 79 test(testName, () { throw new Exception('Fail.'); }); | 81 test(testName, () { throw new Exception('Fail.'); }); |
| 80 } else if (testName == 'group name test') { | 82 } else if (testName == 'group name test') { |
| 81 group('a', () { | 83 group('a', () { |
| 82 test('a', () {}); | 84 test('a', () {}); |
| 83 group('b', () { | 85 group('b', () { |
| 84 test('b', () {}); | 86 test('b', () {}); |
| 85 }); | 87 }); |
| 86 }); | 88 }); |
| 87 } else if (testName == 'setup test') { | 89 } else if (testName == 'setup test') { |
| 88 group('a', () { | 90 group('a', () { |
| 89 setUp(() { _testconfig.setup = 'setup'; }); | 91 setUp(() { testConfig.setup = 'setup'; }); |
| 90 test(testName, () {}); | 92 test(testName, () {}); |
| 91 }); | 93 }); |
| 92 } else if (testName == 'teardown test') { | 94 } else if (testName == 'teardown test') { |
| 93 group('a', () { | 95 group('a', () { |
| 94 tearDown(() { _testconfig.teardown = 'teardown'; }); | 96 tearDown(() { testConfig.teardown = 'teardown'; }); |
| 95 test(testName, () {}); | 97 test(testName, () {}); |
| 96 }); | 98 }); |
| 97 } else if (testName == 'setup and teardown test') { | 99 } else if (testName == 'setup and teardown test') { |
| 98 group('a', () { | 100 group('a', () { |
| 99 setUp(() { _testconfig.setup = 'setup'; }); | 101 setUp(() { testConfig.setup = 'setup'; }); |
| 100 tearDown(() { _testconfig.teardown = 'teardown'; }); | 102 tearDown(() { testConfig.teardown = 'teardown'; }); |
| 101 test(testName, () {}); | 103 test(testName, () {}); |
| 102 }); | 104 }); |
| 103 } else if (testName == 'correct callback test') { | 105 } else if (testName == 'correct callback test') { |
| 104 test(testName, | 106 test(testName, |
| 105 () =>_defer(expectAsync0((){ ++_testconfig.count;}))); | 107 () =>_defer(expectAsync0((){ ++testConfig.count;}))); |
| 106 } else if (testName == 'excess callback test') { | 108 } else if (testName == 'excess callback test') { |
| 107 test(testName, () { | 109 test(testName, () { |
| 108 var _callback0 = expectAsync0(() => ++_testconfig.count); | 110 var _callback0 = expectAsync0(() => ++testConfig.count); |
| 109 var _callback1 = expectAsync0(() => ++_testconfig.count); | 111 var _callback1 = expectAsync0(() => ++testConfig.count); |
| 110 var _callback2 = expectAsync0(() { | 112 var _callback2 = expectAsync0(() { |
| 111 _callback1(); | 113 _callback1(); |
| 112 _callback1(); | 114 _callback1(); |
| 113 _callback0(); | 115 _callback0(); |
| 114 }); | 116 }); |
| 115 _defer(_callback2); | 117 _defer(_callback2); |
| 116 }); | 118 }); |
| 117 } else if (testName == 'completion test') { | 119 } else if (testName == 'completion test') { |
| 118 test(testName, () { | 120 test(testName, () { |
| 119 var _callback; | 121 var _callback; |
| 120 _callback = expectAsyncUntil0(() { | 122 _callback = expectAsyncUntil0(() { |
| 121 if (++_testconfig.count < 10) { | 123 if (++testConfig.count < 10) { |
| 122 _defer(_callback); | 124 _defer(_callback); |
| 123 } | 125 } |
| 124 }, | 126 }, |
| 125 () => (_testconfig.count == 10)); | 127 () => (testConfig.count == 10)); |
| 126 _defer(_callback); | 128 _defer(_callback); |
| 127 }); | 129 }); |
| 128 } else if (testName == 'async exception test') { | 130 } else if (testName == 'async exception test') { |
| 129 test(testName, () { | 131 test(testName, () { |
| 130 expectAsync0(() {}); | 132 expectAsync0(() {}); |
| 131 _defer(() => guardAsync(() { throw "error!"; })); | 133 _defer(() => guardAsync(() { throw "error!"; })); |
| 132 }); | 134 }); |
| 133 } else if (testName == 'late exception test') { | 135 } else if (testName == 'late exception test') { |
| 134 var f; | 136 var f; |
| 135 test('testOne', () { | 137 test('testOne', () { |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 288 }); | 290 }); |
| 289 }); | 291 }); |
| 290 }); | 292 }); |
| 291 test('foo6', () { | 293 test('foo6', () { |
| 292 }); | 294 }); |
| 293 } else if (testName == 'testCases immutable') { | 295 } else if (testName == 'testCases immutable') { |
| 294 test(testName, () { | 296 test(testName, () { |
| 295 expect(() => testCases.clear(), throwsUnsupportedError); | 297 expect(() => testCases.clear(), throwsUnsupportedError); |
| 296 expect(() => testCases.removeLast(), throwsUnsupportedError); | 298 expect(() => testCases.removeLast(), throwsUnsupportedError); |
| 297 }); | 299 }); |
| 300 } else if (testName == 'runTests without tests') { | |
| 301 runTests(); | |
| 298 } | 302 } |
| 299 }); | 303 }); |
| 300 } | 304 } |
| 301 | 305 |
| 302 main() { | 306 main() { |
| 303 var tests = { | 307 var tests = { |
| 304 'single correct test': buildStatusString(1, 0, 0, 'single correct test'), | 308 'single correct test': buildStatusString(1, 0, 0, 'single correct test'), |
| 305 'single failing test': buildStatusString(0, 1, 0, 'single failing test', | 309 'single failing test': buildStatusString(0, 1, 0, 'single failing test', |
| 306 message: 'Expected: <5> but: was <4>.'), | 310 message: 'Expected: <5> but: was <4>.'), |
| 307 'exception test': buildStatusString(0, 1, 0, 'exception test', | 311 'exception test': buildStatusString(0, 1, 0, 'exception test', |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 346 'successful::' | 350 'successful::' |
| 347 'fail1:Expected: <false> but: was <true>.:' | 351 'fail1:Expected: <false> but: was <true>.:' |
| 348 'error1:Callback called more times than expected (1).:' | 352 'error1:Callback called more times than expected (1).:' |
| 349 'fail2:failure:' | 353 'fail2:failure:' |
| 350 'error2:Callback called more times than expected (1).:' | 354 'error2:Callback called more times than expected (1).:' |
| 351 'foo6'), | 355 'foo6'), |
| 352 'testCases immutable': | 356 'testCases immutable': |
| 353 buildStatusString(1, 0, 0, 'testCases immutable'), | 357 buildStatusString(1, 0, 0, 'testCases immutable'), |
| 354 }; | 358 }; |
| 355 | 359 |
| 360 tests['runTests without tests'] = buildStatusString(0, 0, 0, null); | |
|
Siggi Cherem (dart-lang)
2013/04/04 00:05:24
Now that you switched to use a map above, can we a
| |
| 361 | |
| 356 tests.forEach((String name, String expected) { | 362 tests.forEach((String name, String expected) { |
| 357 test(name, () => spawnFunction(runTest) | 363 test(name, () => spawnFunction(runTest) |
| 358 .call(name) | 364 .call(name) |
| 359 .then((String msg) => expect(msg.trim(), equals(expected)))); | 365 .then((String msg) => expect(msg.trim(), equals(expected)))); |
| 360 }); | 366 }); |
| 361 } | 367 } |
| 362 | 368 |
| OLD | NEW |