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 var tests; // array of test names | 17 var tests; // array of test names |
| 18 var expected; // array of test expected results (from buildStatusString) | 18 var expected; // array of test expected results (from buildStatusString) |
| 19 var actual; // actual test results (from buildStatusString in config.onDone) | 19 var actual; // actual test results (from buildStatusString in config.onDone) |
| 20 var _testconfig; // test configuration to capture onDone | 20 var _testconfig; // test configuration to capture onDone |
| 21 | 21 |
| 22 _defer(void fn()) { | 22 Future _defer(void fn()) { |
| 23 return (new Future.immediate(null)).then((_) => guardAsync(fn)); | 23 return new Future.of(fn); |
| 24 } | 24 } |
| 25 | 25 |
| 26 String buildStatusString(int passed, int failed, int errors, | 26 String buildStatusString(int passed, int failed, int errors, |
| 27 var results, | 27 var results, |
| 28 {int count: 0, | 28 {int count: 0, |
| 29 String setup: '', String teardown: '', | 29 String setup: '', String teardown: '', |
| 30 String uncaughtError: null, | 30 String uncaughtError: null, |
| 31 String message: ''}) { | 31 String message: ''}) { |
| 32 var totalTests = 0; | 32 var totalTests = 0; |
| 33 String testDetails = ''; | 33 String testDetails = ''; |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 143 _defer(expectAsync0(() { f(); })); | 143 _defer(expectAsync0(() { f(); })); |
| 144 }); | 144 }); |
| 145 } else if (testName == 'middle exception test') { | 145 } else if (testName == 'middle exception test') { |
| 146 test('testOne', () { expect(true, isTrue); }); | 146 test('testOne', () { expect(true, isTrue); }); |
| 147 test('testTwo', () { expect(true, isFalse); }); | 147 test('testTwo', () { expect(true, isFalse); }); |
| 148 test('testThree', () { | 148 test('testThree', () { |
| 149 var done = expectAsync0((){}); | 149 var done = expectAsync0((){}); |
| 150 _defer(() { | 150 _defer(() { |
| 151 expect(true, isTrue); | 151 expect(true, isTrue); |
| 152 done(); | 152 done(); |
| 153 }); | 153 }); |
|
gram
2013/03/21 17:55:24
Can you explain why you added the defer here? And
kevmoo-old
2013/03/21 18:19:57
It ends up being a nice little verification of exp
| |
| 154 }); | 154 }); |
| 155 } else if (testName == 'async setup/teardown test') { | 155 } else if (testName == 'async setup/teardown test') { |
| 156 group('good setup/good teardown', () { | 156 group('good setup/good teardown', () { |
| 157 setUp(() { | 157 setUp(() { |
| 158 var completer = new Completer(); | 158 return new Future.immediate(0); |
| 159 _defer(() { | |
| 160 completer.complete(0); | |
| 161 }); | |
| 162 return completer.future; | |
| 163 }); | 159 }); |
| 164 tearDown(() { | 160 tearDown(() { |
| 165 var completer = new Completer(); | 161 return new Future.immediate(0); |
| 166 _defer(() { | |
| 167 completer.complete(0); | |
| 168 }); | |
| 169 return completer.future; | |
| 170 }); | 162 }); |
| 171 test('foo1', (){}); | 163 test('foo1', (){}); |
| 172 }); | 164 }); |
| 173 group('good setup/bad teardown', () { | 165 group('good setup/bad teardown', () { |
| 174 setUp(() { | 166 setUp(() { |
| 175 var completer = new Completer(); | 167 return new Future.immediate(0); |
| 176 _defer(() { | |
| 177 completer.complete(0); | |
| 178 }); | |
| 179 return completer.future; | |
| 180 }); | 168 }); |
| 181 tearDown(() { | 169 tearDown(() { |
| 182 var completer = new Completer(); | 170 return new Future.immediateError("Failed to complete tearDown"); |
| 183 _defer(() { | |
| 184 //throw "Failed to complete tearDown"; | |
| 185 completer.completeError( | |
| 186 new AsyncError("Failed to complete tearDown")); | |
| 187 }); | |
| 188 return completer.future; | |
| 189 }); | 171 }); |
| 190 test('foo2', (){}); | 172 test('foo2', (){}); |
| 191 }); | 173 }); |
| 192 group('bad setup/good teardown', () { | 174 group('bad setup/good teardown', () { |
| 193 setUp(() { | 175 setUp(() { |
| 194 var completer = new Completer(); | 176 return new Future.immediateError("Failed to complete setUp"); |
| 195 _defer(() { | |
| 196 //throw "Failed to complete setUp"; | |
| 197 completer.completeError(new AsyncError("Failed to complete setUp")); | |
| 198 }); | |
| 199 return completer.future; | |
| 200 }); | 177 }); |
| 201 tearDown(() { | 178 tearDown(() { |
| 202 var completer = new Completer(); | 179 return new Future.immediate(0); |
| 203 _defer(() { | |
| 204 completer.complete(0); | |
| 205 }); | |
| 206 return completer.future; | |
| 207 }); | 180 }); |
| 208 test('foo3', (){}); | 181 test('foo3', (){}); |
| 209 }); | 182 }); |
| 210 group('bad setup/bad teardown', () { | 183 group('bad setup/bad teardown', () { |
| 211 setUp(() { | 184 setUp(() { |
| 212 var completer = new Completer(); | 185 return new Future.immediateError("Failed to complete setUp"); |
| 213 _defer(() { | |
| 214 //throw "Failed to complete setUp"; | |
| 215 completer.completeError(new AsyncError("Failed to complete setUp")); | |
| 216 }); | |
| 217 return completer.future; | |
| 218 }); | 186 }); |
| 219 tearDown(() { | 187 tearDown(() { |
| 220 var completer = new Completer(); | 188 return new Future.immediateError("Failed to complete tearDown"); |
| 221 _defer(() { | |
| 222 //throw "Failed to complete tearDown"; | |
| 223 completer.completeError( | |
| 224 new AsyncError("Failed to complete tearDown")); | |
| 225 }); | |
| 226 return completer.future; | |
| 227 }); | 189 }); |
| 228 test('foo4', (){}); | 190 test('foo4', (){}); |
| 229 }); | 191 }); |
| 230 // The next test is just to make sure we make steady progress | 192 // The next test is just to make sure we make steady progress |
| 231 // through the tests. | 193 // through the tests. |
| 232 test('post groups', () {}); | 194 test('post groups', () {}); |
| 233 } else if (testName == 'test returning future') { | 195 } else if (testName == 'test returning future') { |
| 234 test("successful", () { | 196 test("successful", () { |
| 235 return _defer(() { | 197 return _defer(() { |
| 236 expect(true, true); | 198 expect(true, true); |
| 237 }); | 199 }); |
| 238 }); | 200 }); |
| 239 // We repeat the fail and error tests, because during development | 201 // We repeat the fail and error tests, because during development |
| 240 // I had a situation where either worked fine on their own, and | 202 // I had a situation where either worked fine on their own, and |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 383 buildStatusString(1, 0, 0, tests[7], count: 1), | 345 buildStatusString(1, 0, 0, tests[7], count: 1), |
| 384 buildStatusString(0, 1, 0, tests[8], count: 1, | 346 buildStatusString(0, 1, 0, tests[8], count: 1, |
| 385 message: 'Callback called more times than expected (1).'), | 347 message: 'Callback called more times than expected (1).'), |
| 386 buildStatusString(1, 0, 0, tests[9], count: 10), | 348 buildStatusString(1, 0, 0, tests[9], count: 10), |
| 387 buildStatusString(0, 1, 0, tests[10], message: 'Caught error!'), | 349 buildStatusString(0, 1, 0, tests[10], message: 'Caught error!'), |
| 388 buildStatusString(1, 0, 1, 'testOne', | 350 buildStatusString(1, 0, 1, 'testOne', |
| 389 message: 'Callback called (2) after test case testOne has already ' | 351 message: 'Callback called (2) after test case testOne has already ' |
| 390 'been marked as pass.:testTwo:'), | 352 'been marked as pass.:testTwo:'), |
| 391 buildStatusString(2, 1, 0, | 353 buildStatusString(2, 1, 0, |
| 392 'testOne::testTwo:Expected: false but: was <true>.:testThree'), | 354 'testOne::testTwo:Expected: false but: was <true>.:testThree'), |
| 393 buildStatusString(2, 0, 3, | 355 buildStatusString(2, 0, 3, |
| 394 'good setup/good teardown foo1::' | 356 'good setup/good teardown foo1::' |
| 395 'good setup/bad teardown foo2:good setup/bad teardown ' | 357 'good setup/bad teardown foo2:good setup/bad teardown ' |
| 396 'foo2: Test teardown failed: Failed to complete tearDown:' | 358 'foo2: Test teardown failed: Failed to complete tearDown:' |
| 397 'bad setup/good teardown foo3:bad setup/good teardown ' | 359 'bad setup/good teardown foo3:bad setup/good teardown ' |
| 398 'foo3: Test setup failed: Failed to complete setUp:' | 360 'foo3: Test setup failed: Failed to complete setUp:' |
| 399 'bad setup/bad teardown foo4:bad setup/bad teardown ' | 361 'bad setup/bad teardown foo4:bad setup/bad teardown ' |
| 400 'foo4: Test teardown failed: Failed to complete tearDown:' | 362 'foo4: Test teardown failed: Failed to complete tearDown:' |
| 401 'post groups'), | 363 'post groups'), |
| 402 buildStatusString(2, 4, 0, | 364 buildStatusString(2, 4, 0, |
| 403 'successful::' | 365 'successful::' |
| 404 'error1:Callback called more times than expected (1).:' | 366 'error1:Callback called more times than expected (1).:' |
| 405 'fail1:Expected: <false> but: was <true>.:' | 367 'fail1:Expected: <false> but: was <true>.:' |
| 406 'error2:Callback called more times than expected (1).:' | 368 'error2:Callback called more times than expected (1).:' |
| 407 'fail2:failure:' | 369 'fail2:failure:' |
| 408 'foo5'), | 370 'foo5'), |
| 409 buildStatusString(2, 4, 0, | 371 buildStatusString(2, 4, 0, |
| 410 'successful::' | 372 'successful::' |
| 411 'fail1:Expected: <false> but: was <true>.:' | 373 'fail1:Expected: <false> but: was <true>.:' |
| 412 'error1:Callback called more times than expected (1).:' | 374 'error1:Callback called more times than expected (1).:' |
| 413 'fail2:failure:' | 375 'fail2:failure:' |
| 414 'error2:Callback called more times than expected (1).:' | 376 'error2:Callback called more times than expected (1).:' |
| 415 'foo6'), | 377 'foo6'), |
| 416 ]; | 378 ]; |
| 417 | 379 |
| 418 actual = []; | 380 actual = []; |
| 419 | 381 |
| 420 nextTest(0); | 382 nextTest(0); |
| 421 } | 383 } |
| 422 | 384 |
| OLD | NEW |