Chromium Code Reviews| Index: tests/lib/unittest/unittest_test.dart |
| =================================================================== |
| --- tests/lib/unittest/unittest_test.dart (revision 0) |
| +++ tests/lib/unittest/unittest_test.dart (revision 0) |
| @@ -0,0 +1,173 @@ |
| +// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +// TODO(gram): |
| +// Unfortunately I can't seem to test anything that involves timeouts, e.g. |
| +// insufficient callbacks, because the timeout is controlled externally |
| +// (test.dart?), and we would need to use a shorter timeout for the inner tests |
| +// so the outer timeout doesn't fire. So I removed all such tests. |
| +// I'd like to revisit this at some point. |
| + |
| +#library('unittestTest'); |
| +#import('dart:isolate'); |
| +#import('../../../lib/unittest/unittest.dart'); |
| + |
| +var tests; |
|
eub
2012/06/21 17:52:20
I forgot these - brief comments please? And I lik
|
| +var expected; |
| +var actual; |
| +var _testconfig; |
| + |
| +_defer(void fn()) { |
| + // Exploit isolate ports as a platform-independent mechanism to queue a |
| + // message at the end of the event loop. Stolen from unittest.dart. |
| + final port = new ReceivePort(); |
| + port.receive((msg, reply) { |
| + fn(); |
| + port.close(); |
| + }); |
| + port.toSendPort().send(null, null); |
| +} |
| + |
| +String buildStatusString(int passed, int failed, int errors, |
| + var results, |
| + [int x = 0, String setup = '', String teardown = '', |
| + String uncaughtError = null, |
| + String message = '']) { |
| + var totalTests = 0; |
| + String testDetails = ''; |
| + if (results is String) { |
| + totalTests = passed + failed + errors; |
| + testDetails = ':$results:$message'; |
| + } else { |
| + totalTests = results.length; |
| + for (var i = 0; i < results.length; i++) { |
| + testDetails = '$testDetails:${results[i].description}:' |
| + '${collapseWhitespace(results[i].message)}'; |
| + } |
| + } |
| + var result = '$passed:$failed:$errors:$totalTests:$x:' |
| + '$setup:$teardown:$uncaughtError$testDetails'; |
| + return result; |
| +} |
| + |
| +class TestConfiguration extends Configuration { |
| + int x = 0; |
|
eub
2012/06/21 17:52:20
First three members could use a comment that they'
|
| + String setup = ''; |
| + String teardown = ''; |
| + SendPort _port; |
| + |
| + TestConfiguration(this._port); |
| + |
| + void onDone(int passed, int failed, int errors, List<TestCase> results, |
| + String uncaughtError) { |
| + var result = buildStatusString(passed, failed, errors, results, x, |
| + setup, teardown, uncaughtError); |
| + _port.send(result); |
| + } |
| +} |
| + |
| +runTest() { |
| + port.receive((testName, sendport) { |
| + configure(_testconfig = new TestConfiguration(sendport)); |
| + if (testName == 'single correct test') { |
| + test(testName, () => expect(2 + 3, equals(5))); |
| + } else if (testName == 'single failing test') { |
| + test(testName, () => expect(2 + 2, equals(5))); |
| + } else if (testName == 'exception test') { |
| + test(testName, () { throw new Exception('fail'); }); |
| + } else if (testName == 'group name test') { |
| + group('a', () { |
| + test('a', () {}); |
| + group('b', () { |
| + test('b', () {}); |
| + }); |
| + }); |
| + } else if (testName == 'setup test') { |
| + group('a', () { |
| + setUp(() { _testconfig.setup = 'setup'; }); |
| + test(testName, () {}); |
| + }); |
| + } else if (testName == 'teardown test') { |
| + group('a', () { |
| + tearDown(() { _testconfig.teardown = 'teardown'; }); |
| + test(testName, () {}); |
| + }); |
| + } else if (testName == 'setup and teardown test') { |
| + group('a', () { |
| + setUp(() { _testconfig.setup = 'setup'; }); |
| + tearDown(() { _testconfig.teardown = 'teardown'; }); |
| + test(testName, () {}); |
| + }); |
| + } else if (testName == 'correct callback test') { |
| + test(testName, |
| + () =>_defer(expectAsync0((){ ++_testconfig.x;}))); |
| + } else if (testName == 'excess callback test') { |
| + test(testName, () { |
| + var _callback = expectAsync0((){ ++_testconfig.x;}); |
| + _defer(_callback); |
| + _defer(_callback); |
| + }); |
| + } else if (testName == 'completion test') { |
| + test(testName, () { |
| + var _callback; |
| + _callback = expectAsyncUntil0(() { |
| + if (++_testconfig.x < 10) { |
| + _defer(_callback); |
| + } |
| + }, |
| + () => (_testconfig.x == 10)); |
| + _defer(_callback); |
| + }); |
| + } |
| + }); |
| +} |
| + |
| +void nextTest(int testNum) { |
| + SendPort sport = spawnFunction(runTest); |
| + sport.call(tests[testNum]).then((msg) { |
| + actual.add(msg); |
| + if (actual.length == expected.length) { |
| + for (var i = 0; i < tests.length; i++) { |
| + test(tests[i], () => expect(actual[i], equals(expected[i]))); |
| + } |
| + } else { |
| + nextTest(testNum+1); |
| + } |
| + }); |
| +} |
| + |
| +main() { |
| + tests = [ |
| + 'single correct test', |
| + 'single failing test', |
| + 'exception test', |
| + 'group name test', |
| + 'setup test', |
| + 'teardown test', |
| + 'setup and teardown test', |
| + 'correct callback test', |
| + 'excess callback test', |
| + 'completion test' |
| + ]; |
| + |
| + expected = [ |
| + buildStatusString(1, 0, 0, tests[0]), |
| + buildStatusString(0, 1, 0, tests[1], message: 'Expected: <5> but: was <4>'), |
| + buildStatusString(0, 1, 0, tests[2], message: 'Caught Exception: fail'), |
| + buildStatusString(2, 0, 0, 'a a::a b b'), |
| + buildStatusString(1, 0, 0, 'a ${tests[4]}', 0, 'setup'), |
| + buildStatusString(1, 0, 0, 'a ${tests[5]}', 0, '', 'teardown'), |
| + buildStatusString(1, 0, 0, 'a ${tests[6]}', 0, |
| + 'setup', 'teardown'), |
| + buildStatusString(1, 0, 0, tests[7], 1), |
| + buildStatusString(0, 0, 1, tests[8], 1, |
| + message: 'Callback called more times than expected (2 > 1)'), |
| + buildStatusString(1, 0, 0, tests[9], 10) |
| + ]; |
| + |
| + actual = []; |
| + |
| + nextTest(0); |
| +} |
| + |