OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, 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 scheduled_test_test; |
| 6 |
| 7 import 'dart:async'; |
| 8 |
| 9 import 'package:scheduled_test/scheduled_test.dart'; |
| 10 import 'package:scheduled_test/src/utils.dart'; |
| 11 import 'metatest.dart'; |
| 12 |
| 13 void main() { |
| 14 expectTestsPass('a scheduled test with a correct synchronous expectation ' |
| 15 'should pass', () { |
| 16 test('test', () { |
| 17 expect('foo', equals('foo')); |
| 18 }); |
| 19 }); |
| 20 |
| 21 expectTestsFail('a scheduled test with an incorrect synchronous expectation ' |
| 22 'should fail', () { |
| 23 test('test', () { |
| 24 expect('foo', equals('bar')); |
| 25 }); |
| 26 }); |
| 27 |
| 28 expectTestsPass('a scheduled test with a correct asynchronous expectation ' |
| 29 'should pass', () { |
| 30 test('test', () { |
| 31 expect(new Future.immediate('foo'), completion(equals('foo'))); |
| 32 }); |
| 33 }); |
| 34 |
| 35 expectTestsFail('a scheduled test with an incorrect asynchronous expectation ' |
| 36 'should fail', () { |
| 37 test('test', () { |
| 38 expect(new Future.immediate('foo'), completion(equals('bar'))); |
| 39 }); |
| 40 }); |
| 41 |
| 42 expectTestsPass('a passing scheduled synchronous expect should register', () { |
| 43 test('test', () { |
| 44 schedule(() => expect('foo', equals('foo'))); |
| 45 }); |
| 46 }); |
| 47 |
| 48 expectTestsFail('a failing scheduled synchronous expect should register', () { |
| 49 test('test', () { |
| 50 schedule(() => expect('foo', equals('bar'))); |
| 51 }); |
| 52 }); |
| 53 |
| 54 expectTestsPass('a passing scheduled asynchronous expect should ' |
| 55 'register', () { |
| 56 test('test', () { |
| 57 schedule(() => |
| 58 expect(new Future.immediate('foo'), completion(equals('foo')))); |
| 59 }); |
| 60 }); |
| 61 |
| 62 expectTestsFail('a failing scheduled synchronous expect should ' |
| 63 'register', () { |
| 64 test('test', () { |
| 65 schedule(() => |
| 66 expect(new Future.immediate('foo'), completion(equals('bar')))); |
| 67 }); |
| 68 }); |
| 69 |
| 70 expectTestsPass('scheduled blocks should be run in order after the ' |
| 71 'synchronous setup', () { |
| 72 test('test', () { |
| 73 var list = [1]; |
| 74 schedule(() => list.add(2)); |
| 75 list.add(3); |
| 76 schedule(() => expect(list, equals([1, 3, 4, 2]))); |
| 77 list.add(4); |
| 78 }); |
| 79 }); |
| 80 |
| 81 expectTestsPass('scheduled blocks should forward their return values as ' |
| 82 'Futures', () { |
| 83 test('synchronous value', () { |
| 84 var future = schedule(() => 'value'); |
| 85 expect(future, completion(equals('value'))); |
| 86 }); |
| 87 |
| 88 test('asynchronous value', () { |
| 89 var future = schedule(() => new Future.immediate('value')); |
| 90 expect(future, completion(equals('value'))); |
| 91 }); |
| 92 }); |
| 93 |
| 94 expectTestsPass('scheduled blocks should wait for their Future return values ' |
| 95 'to complete before proceeding', () { |
| 96 test('test', () { |
| 97 var value = 'unset'; |
| 98 schedule(() => sleep(500).then((_) { |
| 99 value = 'set'; |
| 100 })); |
| 101 schedule(() => expect(value, equals('set'))); |
| 102 }); |
| 103 }); |
| 104 |
| 105 expectTestsFail('a test failure in a chained future in a scheduled block ' |
| 106 'should be registered', () { |
| 107 test('test', () { |
| 108 schedule(() => new Future.immediate('foo') |
| 109 .then((v) => expect(v, equals('bar')))); |
| 110 }); |
| 111 }); |
| 112 |
| 113 expectTestsFail('an error in a chained future in a scheduled block should be ' |
| 114 'registered', () { |
| 115 test('test', () { |
| 116 schedule(() => new Future.immediate(null).then((_) { |
| 117 throw 'error'; |
| 118 })); |
| 119 }); |
| 120 }); |
| 121 |
| 122 expectTestsFail('an out-of-band failure in wrapAsync is handled', () { |
| 123 test('test', () { |
| 124 schedule(() { |
| 125 sleep(10).then(wrapAsync((_) => expect('foo', equals('bar')))); |
| 126 }); |
| 127 schedule(() => sleep(50)); |
| 128 }); |
| 129 }); |
| 130 |
| 131 expectTestsFail('an out-of-band failure in wrapAsync that finishes after the ' |
| 132 'schedule is handled', () { |
| 133 test('test', () { |
| 134 schedule(() { |
| 135 sleep(50).then(wrapAsync((_) => expect('foo', equals('bar')))); |
| 136 }); |
| 137 schedule(() => sleep(10)); |
| 138 }); |
| 139 }); |
| 140 |
| 141 expectTestsFail('an out-of-band error reported via signalError is ' |
| 142 'handled', () { |
| 143 test('test', () { |
| 144 schedule(() { |
| 145 sleep(10).then((_) => currentSchedule.signalError('bad')); |
| 146 }); |
| 147 schedule(() => sleep(50)); |
| 148 }); |
| 149 }); |
| 150 |
| 151 expectTestsFail('an out-of-band error reported via signalError that finished ' |
| 152 'after the schedule is handled', () { |
| 153 test('test', () { |
| 154 schedule(() { |
| 155 var done = wrapAsync((_) {}); |
| 156 sleep(50).then((_) { |
| 157 currentSchedule.signalError('bad'); |
| 158 done(null); |
| 159 }); |
| 160 }); |
| 161 schedule(() => sleep(10)); |
| 162 }); |
| 163 }); |
| 164 |
| 165 expectTestsFail('a synchronous error reported via signalError is handled', ()
{ |
| 166 test('test', () { |
| 167 currentSchedule.signalError('bad'); |
| 168 }); |
| 169 }); |
| 170 |
| 171 expectTestsPass('the onComplete queue is run if a test is successful', () { |
| 172 var onCompleteRun = false; |
| 173 test('test 1', () { |
| 174 currentSchedule.onComplete.schedule(() { |
| 175 onCompleteRun = true; |
| 176 }); |
| 177 |
| 178 schedule(() => expect('foo', equals('foo'))); |
| 179 }); |
| 180 |
| 181 test('test 2', () { |
| 182 expect(onCompleteRun, isTrue); |
| 183 }); |
| 184 }); |
| 185 |
| 186 expectTestsPass('the onComplete queue is run after an out-of-band callback', |
| 187 () { |
| 188 var outOfBandRun = false; |
| 189 test('test1', () { |
| 190 currentSchedule.onComplete.schedule(() { |
| 191 expect(outOfBandRun, isTrue); |
| 192 }); |
| 193 |
| 194 sleep(50).then(wrapAsync((_) { |
| 195 outOfBandRun = true; |
| 196 })); |
| 197 }); |
| 198 }); |
| 199 |
| 200 expectTestsFail('an out-of-band callback in the onComplete queue blocks the ' |
| 201 'test', () { |
| 202 var outOfBandRun = false; |
| 203 test('test', () { |
| 204 currentSchedule.onComplete.schedule(() { |
| 205 sleep(50).then(wrapAsync((_) => expect('foo', equals('bar')))); |
| 206 }); |
| 207 }); |
| 208 }); |
| 209 |
| 210 expectTestsPass('an out-of-band callback blocks onComplete even with an ' |
| 211 'unrelated error', () { |
| 212 var outOfBandRun = false; |
| 213 var outOfBandSetInOnComplete = false; |
| 214 test('test 1', () { |
| 215 currentSchedule.onComplete.schedule(() { |
| 216 outOfBandSetInOnComplete = outOfBandRun; |
| 217 }); |
| 218 |
| 219 sleep(50).then(wrapAsync((_) { |
| 220 outOfBandRun = true; |
| 221 })); |
| 222 |
| 223 schedule(() => expect('foo', equals('bar'))); |
| 224 }); |
| 225 |
| 226 test('test 2', () => expect(outOfBandSetInOnComplete, isTrue)); |
| 227 }, passing: ['test 2']); |
| 228 |
| 229 expectTestsPass('the onComplete queue is run after an asynchronous error', |
| 230 () { |
| 231 var onCompleteRun = false; |
| 232 test('test 1', () { |
| 233 currentSchedule.onComplete.schedule(() { |
| 234 onCompleteRun = true; |
| 235 }); |
| 236 |
| 237 schedule(() => expect('foo', equals('bar'))); |
| 238 }); |
| 239 |
| 240 test('test 2', () { |
| 241 expect(onCompleteRun, isTrue); |
| 242 }); |
| 243 }, passing: ['test 2']); |
| 244 |
| 245 expectTestsPass('the onComplete queue is run after a synchronous error', () { |
| 246 var onCompleteRun = false; |
| 247 test('test 1', () { |
| 248 currentSchedule.onComplete.schedule(() { |
| 249 onCompleteRun = true; |
| 250 }); |
| 251 |
| 252 throw 'error'; |
| 253 }); |
| 254 |
| 255 test('test 2', () { |
| 256 expect(onCompleteRun, isTrue); |
| 257 }); |
| 258 }, passing: ['test 2']); |
| 259 |
| 260 expectTestsPass('the onComplete queue is run after an out-of-band error', () { |
| 261 var onCompleteRun = false; |
| 262 test('test 1', () { |
| 263 currentSchedule.onComplete.schedule(() { |
| 264 onCompleteRun = true; |
| 265 }); |
| 266 |
| 267 sleep(50).then(wrapAsync((_) => expect('foo', equals('bar')))); |
| 268 }); |
| 269 |
| 270 test('test 2', () { |
| 271 expect(onCompleteRun, isTrue); |
| 272 }); |
| 273 }, passing: ['test 2']); |
| 274 |
| 275 expectTestsPass('currentSchedule.error contains the error in the onComplete ' |
| 276 'queue', () { |
| 277 var error; |
| 278 test('test 1', () { |
| 279 currentSchedule.onComplete.schedule(() { |
| 280 error = currentSchedule.error; |
| 281 }); |
| 282 |
| 283 throw 'error'; |
| 284 }); |
| 285 |
| 286 test('test 2', () { |
| 287 expect(error, new isInstanceOf<ScheduleError>()); |
| 288 expect(error.error, equals('error')); |
| 289 }); |
| 290 }, passing: ['test 2']); |
| 291 |
| 292 expectTestsPass('onComplete tasks can be scheduled during normal tasks', () { |
| 293 var onCompleteRun = false; |
| 294 test('test 1', () { |
| 295 schedule(() { |
| 296 currentSchedule.onComplete.schedule(() { |
| 297 onCompleteRun = true; |
| 298 }); |
| 299 }); |
| 300 }); |
| 301 |
| 302 test('test 2', () { |
| 303 expect(onCompleteRun, isTrue); |
| 304 }); |
| 305 }); |
| 306 |
| 307 expectTestsFail('failures in onComplete cause test failures', () { |
| 308 test('test', () { |
| 309 currentSchedule.onComplete.schedule(() { |
| 310 expect('foo', equals('bar')); |
| 311 }); |
| 312 }); |
| 313 }); |
| 314 |
| 315 expectTestsPass('the onException queue is not run if a test is successful', |
| 316 () { |
| 317 var onExceptionRun = false; |
| 318 test('test 1', () { |
| 319 currentSchedule.onException.schedule(() { |
| 320 onExceptionRun = true; |
| 321 }); |
| 322 |
| 323 schedule(() => expect('foo', equals('foo'))); |
| 324 }); |
| 325 |
| 326 test('test 2', () { |
| 327 expect(onExceptionRun, isFalse); |
| 328 }); |
| 329 }); |
| 330 |
| 331 expectTestsPass('the onException queue is run after an asynchronous error', |
| 332 () { |
| 333 var onExceptionRun = false; |
| 334 test('test 1', () { |
| 335 currentSchedule.onException.schedule(() { |
| 336 onExceptionRun = true; |
| 337 }); |
| 338 |
| 339 schedule(() => expect('foo', equals('bar'))); |
| 340 }); |
| 341 |
| 342 test('test 2', () { |
| 343 expect(onExceptionRun, isTrue); |
| 344 }); |
| 345 }, passing: ['test 2']); |
| 346 |
| 347 expectTestsPass('the onException queue is run after a synchronous error', () { |
| 348 var onExceptionRun = false; |
| 349 test('test 1', () { |
| 350 currentSchedule.onException.schedule(() { |
| 351 onExceptionRun = true; |
| 352 }); |
| 353 |
| 354 throw 'error'; |
| 355 }); |
| 356 |
| 357 test('test 2', () { |
| 358 expect(onExceptionRun, isTrue); |
| 359 }); |
| 360 }, passing: ['test 2']); |
| 361 |
| 362 expectTestsPass('the onException queue is run after an out-of-band error', ()
{ |
| 363 var onExceptionRun = false; |
| 364 test('test 1', () { |
| 365 currentSchedule.onException.schedule(() { |
| 366 onExceptionRun = true; |
| 367 }); |
| 368 |
| 369 sleep(50).then(wrapAsync((_) => expect('foo', equals('bar')))); |
| 370 }); |
| 371 |
| 372 test('test 2', () { |
| 373 expect(onExceptionRun, isTrue); |
| 374 }); |
| 375 }, passing: ['test 2']); |
| 376 |
| 377 expectTestsPass('currentSchedule.error contains the error in the onException ' |
| 378 'queue', () { |
| 379 var error; |
| 380 test('test 1', () { |
| 381 currentSchedule.onException.schedule(() { |
| 382 error = currentSchedule.error; |
| 383 }); |
| 384 |
| 385 throw 'error'; |
| 386 }); |
| 387 |
| 388 test('test 2', () { |
| 389 expect(error, new isInstanceOf<ScheduleError>()); |
| 390 expect(error.error, equals('error')); |
| 391 }); |
| 392 }, passing: ['test 2']); |
| 393 |
| 394 expectTestsPass('currentSchedule.error contains an error passed into ' |
| 395 'signalError synchronously', () { |
| 396 var error; |
| 397 test('test 1', () { |
| 398 currentSchedule.onException.schedule(() { |
| 399 error = currentSchedule.error; |
| 400 }); |
| 401 |
| 402 currentSchedule.signalError('error'); |
| 403 }); |
| 404 |
| 405 test('test 2', () { |
| 406 expect(error, new isInstanceOf<ScheduleError>()); |
| 407 expect(error.error, equals('error')); |
| 408 }); |
| 409 }, passing: ['test 2']); |
| 410 |
| 411 expectTestsPass('currentSchedule.error contains an error passed into ' |
| 412 'signalError asynchronously', () { |
| 413 var error; |
| 414 test('test 1', () { |
| 415 currentSchedule.onException.schedule(() { |
| 416 error = currentSchedule.error; |
| 417 }); |
| 418 |
| 419 schedule(() => currentSchedule.signalError('error')); |
| 420 }); |
| 421 |
| 422 test('test 2', () { |
| 423 expect(error, new isInstanceOf<ScheduleError>()); |
| 424 expect(error.error, equals('error')); |
| 425 }); |
| 426 }, passing: ['test 2']); |
| 427 |
| 428 expectTestsPass('currentSchedule.error contains an error passed into ' |
| 429 'signalError out-of-band', () { |
| 430 var error; |
| 431 test('test 1', () { |
| 432 currentSchedule.onException.schedule(() { |
| 433 error = currentSchedule.error; |
| 434 }); |
| 435 |
| 436 sleep(50).then(wrapAsync((_) => currentSchedule.signalError('error'))); |
| 437 }); |
| 438 |
| 439 test('test 2', () { |
| 440 expect(error, new isInstanceOf<ScheduleError>()); |
| 441 expect(error.error, equals('error')); |
| 442 }); |
| 443 }, passing: ['test 2']); |
| 444 |
| 445 expectTestsPass('currentSchedule.currentTask returns the current task while ' |
| 446 'executing a task', () { |
| 447 test('test', () { |
| 448 schedule(() => expect('foo', equals('foo')), 'task 1'); |
| 449 |
| 450 schedule(() { |
| 451 expect(currentSchedule.currentTask.description, equals('task 2')); |
| 452 }, 'task 2'); |
| 453 |
| 454 schedule(() => expect('bar', equals('bar')), 'task 3'); |
| 455 }); |
| 456 }); |
| 457 |
| 458 expectTestsPass('currentSchedule.currentTask is null before the schedule has ' |
| 459 'started', () { |
| 460 test('test', () { |
| 461 schedule(() => expect('foo', equals('foo'))); |
| 462 |
| 463 expect(currentSchedule.currentTask, isNull); |
| 464 }); |
| 465 }); |
| 466 |
| 467 expectTestsPass('currentSchedule.currentTask is null after the schedule has ' |
| 468 'completed', () { |
| 469 test('test', () { |
| 470 expect(sleep(50).then((_) { |
| 471 expect(currentSchedule.currentTask, isNull); |
| 472 }), completes); |
| 473 |
| 474 schedule(() => expect('foo', equals('foo'))); |
| 475 }); |
| 476 }); |
| 477 |
| 478 expectTestsPass('currentSchedule.currentQueue returns the current queue while
' |
| 479 'executing a task', () { |
| 480 test('test', () { |
| 481 schedule(() { |
| 482 expect(currentSchedule.currentQueue.name, equals('tasks')); |
| 483 }); |
| 484 }); |
| 485 }); |
| 486 |
| 487 expectTestsPass('currentSchedule.currentQueue is null before the schedule has
' |
| 488 'started', () { |
| 489 test('test', () { |
| 490 schedule(() => expect('foo', equals('foo'))); |
| 491 |
| 492 expect(currentSchedule.currentQueue, isNull); |
| 493 }); |
| 494 }); |
| 495 |
| 496 expectTestsPass('setUp is run before each test', () { |
| 497 var setUpRun = false; |
| 498 setUp(() { |
| 499 setUpRun = true; |
| 500 }); |
| 501 |
| 502 test('test 1', () { |
| 503 expect(setUpRun, isTrue); |
| 504 setUpRun = false; |
| 505 }); |
| 506 |
| 507 test('test 2', () { |
| 508 expect(setUpRun, isTrue); |
| 509 setUpRun = false; |
| 510 }); |
| 511 }); |
| 512 |
| 513 expectTestsPass('setUp can schedule events', () { |
| 514 var setUpRun = false; |
| 515 setUp(() { |
| 516 schedule(() { |
| 517 setUpRun = true; |
| 518 }); |
| 519 currentSchedule.onComplete.schedule(() { |
| 520 setUpRun = false; |
| 521 }); |
| 522 }); |
| 523 |
| 524 test('test 1', () { |
| 525 expect(setUpRun, isFalse); |
| 526 schedule(() => expect(setUpRun, isTrue)); |
| 527 }); |
| 528 |
| 529 test('test 2', () { |
| 530 expect(setUpRun, isFalse); |
| 531 schedule(() => expect(setUpRun, isTrue)); |
| 532 }); |
| 533 }); |
| 534 |
| 535 expectTestsFail('synchronous errors in setUp will cause tests to fail', () { |
| 536 setUp(() => expect('foo', equals('bar'))); |
| 537 test('test 1', () => expect('foo', equals('foo'))); |
| 538 test('test 2', () => expect('foo', equals('foo'))); |
| 539 }); |
| 540 |
| 541 expectTestsFail('scheduled errors in setUp will cause tests to fail', () { |
| 542 setUp(() => schedule(() => expect('foo', equals('bar')))); |
| 543 test('test 1', () => expect('foo', equals('foo'))); |
| 544 test('test 2', () => expect('foo', equals('foo'))); |
| 545 }); |
| 546 |
| 547 expectTestsPass("setUp doesn't apply to child groups", () { |
| 548 var setUpRun = false; |
| 549 setUp(() { |
| 550 setUpRun = true; |
| 551 currentSchedule.onComplete.schedule(() { |
| 552 setUpRun = false; |
| 553 }); |
| 554 }); |
| 555 |
| 556 test('outer', () { |
| 557 expect(setUpRun, isTrue); |
| 558 }); |
| 559 |
| 560 group('group', () { |
| 561 test('inner', () { |
| 562 expect(setUpRun, isFalse); |
| 563 }); |
| 564 }); |
| 565 }); |
| 566 |
| 567 expectTestsPass("setUp doesn't apply to parent groups", () { |
| 568 var setUpRun = false; |
| 569 group('group', () { |
| 570 setUp(() { |
| 571 setUpRun = true; |
| 572 currentSchedule.onComplete.schedule(() { |
| 573 setUpRun = false; |
| 574 }); |
| 575 }); |
| 576 |
| 577 test('inner', () { |
| 578 expect(setUpRun, isTrue); |
| 579 }); |
| 580 }); |
| 581 |
| 582 test('outer', () { |
| 583 expect(setUpRun, isFalse); |
| 584 }); |
| 585 }); |
| 586 |
| 587 expectTestsPass("setUp doesn't apply to sibling groups", () { |
| 588 var setUpRun = false; |
| 589 group('group 1', () { |
| 590 setUp(() { |
| 591 setUpRun = true; |
| 592 currentSchedule.onComplete.schedule(() { |
| 593 setUpRun = false; |
| 594 }); |
| 595 }); |
| 596 |
| 597 test('test 1', () { |
| 598 expect(setUpRun, isTrue); |
| 599 }); |
| 600 }); |
| 601 |
| 602 group('group 2', () { |
| 603 test('test 2', () { |
| 604 expect(setUpRun, isFalse); |
| 605 }); |
| 606 }); |
| 607 }); |
| 608 } |
OLD | NEW |