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 #library('unittestTest'); | |
| 6 #import('dart:isolate'); | |
| 7 #import('../../../lib/unittest/unittest.dart'); | |
| 8 | |
| 9 var tests; | |
| 10 var expected; | |
| 11 var actual; | |
| 12 var _testconfig; | |
| 13 | |
| 14 _defer(void fn()) { | |
| 15 // Exploit isolate ports as a platform-independent mechanism to queue a | |
| 16 // message at the end of the event loop. Stolen from unittest.dart. | |
| 17 final port = new ReceivePort(); | |
| 18 port.receive((msg, reply) { | |
| 19 fn(); | |
| 20 port.close(); | |
| 21 }); | |
| 22 port.toSendPort().send(null, null); | |
| 23 } | |
| 24 | |
| 25 class TestConfiguration extends Configuration { | |
| 26 int x = 0; | |
| 27 String setup = ''; | |
| 28 String teardown = ''; | |
| 29 SendPort _port; | |
| 30 | |
| 31 TestConfiguration(this._port); | |
| 32 | |
| 33 void onDone(int passed, int failed, int errors, List<TestCase> results, | |
| 34 String uncaughtError) { | |
| 35 var result = '$passed:$failed:$errors:${results.length}:${x}:' | |
| 36 '${setup}:${teardown}:$uncaughtError'; | |
|
eub
2012/06/21 00:09:42
$uncaughtError unused here?
gram
2012/06/21 16:48:16
Currently, as I did not find anything that trigger
| |
| 37 for (var i = 0; i < results.length; i++) { | |
| 38 result = '$result:${results[i].description}:' | |
| 39 '${collapseWhitespace(results[i].message)}'; | |
| 40 } | |
| 41 _port.send(result); | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 callback() { | |
|
eub
2012/06/21 00:09:42
I'm missing where this is used.
gram
2012/06/21 16:48:16
I went through many iterations (before isolates an
| |
| 46 if (++_testconfig.x < 10) { | |
| 47 _defer(callback); | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 runTest() { | |
| 52 port.receive((testName, sendport) { | |
| 53 configure(_testconfig = new TestConfiguration(sendport)); | |
| 54 if (testName == 'single correct test') { | |
| 55 test(testName, () => expect(2 + 3, equals(5))); | |
| 56 } else if (testName == 'single failing test') { | |
| 57 test(testName, () => expect(2 + 2, equals(5))); | |
| 58 } else if (testName == 'exception test') { | |
| 59 test(testName, () { throw new Exception('fail'); }); | |
| 60 } else if (testName == 'group name test') { | |
| 61 group('a', () { | |
| 62 test('a', () {}); | |
| 63 group('b', () { | |
| 64 test('b', () {}); | |
| 65 }); | |
| 66 }); | |
| 67 } else if (testName == 'setup test') { | |
| 68 group('a', () { | |
| 69 setUp(() { _testconfig.setup = 'setup'; }); | |
| 70 test(testName, () {}); | |
| 71 }); | |
| 72 } else if (testName == 'teardown test') { | |
| 73 group('a', () { | |
| 74 tearDown(() { _testconfig.teardown = 'teardown'; }); | |
| 75 test(testName, () {}); | |
| 76 }); | |
| 77 } else if (testName == 'setup and teardown test') { | |
| 78 group('a', () { | |
| 79 setUp(() { _testconfig.setup = 'setup'; }); | |
| 80 tearDown(() { _testconfig.teardown = 'teardown'; }); | |
| 81 test(testName, () {}); | |
| 82 }); | |
| 83 } else if (testName == 'correct callback test') { | |
| 84 test(testName, | |
| 85 () =>_defer(expectAsync0((){ ++_testconfig.x;}))); | |
| 86 } else if (testName == 'excess callback test') { | |
| 87 test(testName, () { | |
| 88 var _callback = expectAsync0((){ ++_testconfig.x;}); | |
| 89 _defer(_callback); | |
| 90 _defer(_callback); | |
| 91 }); | |
| 92 } else if (testName == 'completion test') { | |
| 93 test(testName, () { | |
| 94 var _callback; | |
| 95 _callback = expectAsyncUntil0(() { | |
| 96 if (++_testconfig.x < 10) { | |
| 97 _defer(_callback); | |
| 98 } | |
| 99 }, | |
| 100 () => (_testconfig.x == 10)); | |
| 101 _defer(_callback); | |
| 102 }); | |
| 103 } else if (testName == 'setup and teardown test') { | |
|
eub
2012/06/21 00:09:42
Duplicate 'setup and teardown test'.
gram
2012/06/21 16:48:16
Done.
| |
| 104 group('a', () { | |
| 105 setUp(() { _testconfig.setup = 'setup'; }); | |
| 106 tearDown(() { _testconfig.teardown = 'teardown'; }); | |
| 107 test(testName, () {}); | |
| 108 }); | |
| 109 } | |
| 110 }); | |
| 111 } | |
| 112 | |
| 113 void nextTest(int testNum) { | |
| 114 SendPort sport = spawnFunction(runTest); | |
| 115 sport.call(tests[testNum]).then((msg) { | |
| 116 actual.add(msg); | |
| 117 if (actual.length == expected.length) { | |
| 118 for (var i = 0; i < tests.length; i++) { | |
| 119 test(tests[i], () => expect(actual[i], equals(expected[i]))); | |
| 120 } | |
| 121 } else { | |
| 122 nextTest(testNum+1); | |
| 123 } | |
| 124 }); | |
| 125 } | |
| 126 | |
| 127 main() { | |
| 128 tests = [ | |
| 129 'single correct test', | |
| 130 'single failing test', | |
| 131 'exception test', | |
| 132 'group name test', | |
| 133 'setup test', | |
| 134 'teardown test', | |
| 135 'setup and teardown test', | |
| 136 'correct callback test', | |
| 137 'excess callback test', | |
| 138 'completion test' | |
| 139 ]; | |
| 140 | |
| 141 expected = [ | |
| 142 '1:0:0:1:0:::null:single correct test:', | |
|
eub
2012/06/21 00:09:42
Comment w/ structure reference.
passed : failed :
| |
| 143 '0:1:0:1:0:::null:single failing test:Expected: <5> but: was <4>', | |
|
eub
2012/06/21 00:09:42
BTW, might like a helper to construct these string
| |
| 144 '0:1:0:1:0:::null:exception test:Caught Exception: fail', | |
| 145 '2:0:0:2:0:::null:a a::a b b:', | |
| 146 '1:0:0:1:0:setup::null:a setup test:', | |
| 147 '1:0:0:1:0::teardown:null:a teardown test:', | |
| 148 '1:0:0:1:0:setup:teardown:null:a setup and teardown test:', | |
| 149 '1:0:0:1:1:::null:correct callback test:', | |
| 150 '0:0:1:1:1:::null:excess callback test:' | |
| 151 'Callback called more times than expected (2 > 1)', | |
| 152 '1:0:0:1:10:::null:completion test:' | |
| 153 ]; | |
| 154 | |
| 155 actual = []; | |
| 156 | |
| 157 nextTest(0); | |
| 158 } | |
| 159 | |
| OLD | NEW |