OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 import 'dart:async'; | 5 import 'dart:async'; |
6 | 6 |
7 import 'package:test/src/backend/group.dart'; | 7 import 'package:test/src/backend/group.dart'; |
8 import 'package:test/src/backend/invoker.dart'; | 8 import 'package:test/src/backend/invoker.dart'; |
9 import 'package:test/src/backend/suite.dart'; | 9 import 'package:test/src/backend/suite.dart'; |
10 import 'package:test/src/backend/test.dart'; | 10 import 'package:test/src/backend/test.dart'; |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 | 272 |
273 group("in a group,", () { | 273 group("in a group,", () { |
274 test("tests inherit the group's description", () { | 274 test("tests inherit the group's description", () { |
275 var entries = declare(() { | 275 var entries = declare(() { |
276 group("group", () { | 276 group("group", () { |
277 test("description", () {}); | 277 test("description", () {}); |
278 }); | 278 }); |
279 }); | 279 }); |
280 | 280 |
281 expect(entries, hasLength(1)); | 281 expect(entries, hasLength(1)); |
282 expect(entries.single, new isInstanceOf<Group>()); | 282 var testGroup = entries.single as Group; |
283 expect(entries.single.name, equals("group")); | 283 expect(testGroup.name, equals("group")); |
284 expect(entries.single.entries, hasLength(1)); | 284 expect(testGroup.entries, hasLength(1)); |
285 expect(entries.single.entries.single, new isInstanceOf<Test>()); | 285 expect(testGroup.entries.single, new isInstanceOf<Test>()); |
286 expect(entries.single.entries.single.name, "group description"); | 286 expect(testGroup.entries.single.name, "group description"); |
287 }); | 287 }); |
288 | 288 |
289 test("tests inherit the group's description when it's not a string", () { | 289 test("tests inherit the group's description when it's not a string", () { |
290 var entries = declare(() { | 290 var entries = declare(() { |
291 group(Object, () { | 291 group(Object, () { |
292 test("description", () {}); | 292 test("description", () {}); |
293 }); | 293 }); |
294 }); | 294 }); |
295 | 295 |
296 expect(entries, hasLength(1)); | 296 expect(entries, hasLength(1)); |
297 expect(entries.single, new isInstanceOf<Group>()); | 297 var testGroup = entries.single as Group; |
298 expect(entries.single.name, equals("Object")); | 298 expect(testGroup.name, equals("Object")); |
299 expect(entries.single.entries, hasLength(1)); | 299 expect(testGroup.entries, hasLength(1)); |
300 expect(entries.single.entries.single, new isInstanceOf<Test>()); | 300 expect(testGroup.entries.single, new isInstanceOf<Test>()); |
301 expect(entries.single.entries.single.name, "Object description"); | 301 expect(testGroup.entries.single.name, "Object description"); |
302 }); | 302 }); |
303 | 303 |
304 test("a test's timeout factor is applied to the group's", () { | 304 test("a test's timeout factor is applied to the group's", () { |
305 var entries = declare(() { | 305 var entries = declare(() { |
306 group("group", () { | 306 group("group", () { |
307 test("test", () {}, | 307 test("test", () {}, |
308 timeout: new Timeout.factor(3)); | 308 timeout: new Timeout.factor(3)); |
309 }, timeout: new Timeout.factor(2)); | 309 }, timeout: new Timeout.factor(2)); |
310 }); | 310 }); |
311 | 311 |
312 expect(entries, hasLength(1)); | 312 expect(entries, hasLength(1)); |
313 expect(entries.single, new isInstanceOf<Group>()); | 313 var testGroup = entries.single as Group; |
314 expect(entries.single.metadata.timeout.scaleFactor, equals(2)); | 314 expect(testGroup.metadata.timeout.scaleFactor, equals(2)); |
315 expect(entries.single.entries, hasLength(1)); | 315 expect(testGroup.entries, hasLength(1)); |
316 expect(entries.single.entries.single, new isInstanceOf<Test>()); | 316 expect(testGroup.entries.single, new isInstanceOf<Test>()); |
317 expect(entries.single.entries.single.metadata.timeout.scaleFactor, | 317 expect(testGroup.entries.single.metadata.timeout.scaleFactor, |
318 equals(6)); | 318 equals(6)); |
319 }); | 319 }); |
320 | 320 |
321 test("a test's timeout factor is applied to the group's duration", () { | 321 test("a test's timeout factor is applied to the group's duration", () { |
322 var entries = declare(() { | 322 var entries = declare(() { |
323 group("group", () { | 323 group("group", () { |
324 test("test", () {}, | 324 test("test", () {}, |
325 timeout: new Timeout.factor(2)); | 325 timeout: new Timeout.factor(2)); |
326 }, timeout: new Timeout(new Duration(seconds: 10))); | 326 }, timeout: new Timeout(new Duration(seconds: 10))); |
327 }); | 327 }); |
328 | 328 |
329 expect(entries, hasLength(1)); | 329 expect(entries, hasLength(1)); |
330 expect(entries.single, new isInstanceOf<Group>()); | 330 var testGroup = entries.single as Group; |
331 expect(entries.single.metadata.timeout.duration, | 331 expect(testGroup.metadata.timeout.duration, |
332 equals(new Duration(seconds: 10))); | 332 equals(new Duration(seconds: 10))); |
333 expect(entries.single.entries, hasLength(1)); | 333 expect(testGroup.entries, hasLength(1)); |
334 expect(entries.single.entries.single, new isInstanceOf<Test>()); | 334 expect(testGroup.entries.single, new isInstanceOf<Test>()); |
335 expect(entries.single.entries.single.metadata.timeout.duration, | 335 expect(testGroup.entries.single.metadata.timeout.duration, |
336 equals(new Duration(seconds: 20))); | 336 equals(new Duration(seconds: 20))); |
337 }); | 337 }); |
338 | 338 |
339 test("a test's timeout duration is applied over the group's", () { | 339 test("a test's timeout duration is applied over the group's", () { |
340 var entries = declare(() { | 340 var entries = declare(() { |
341 group("group", () { | 341 group("group", () { |
342 test("test", () {}, | 342 test("test", () {}, |
343 timeout: new Timeout(new Duration(seconds: 15))); | 343 timeout: new Timeout(new Duration(seconds: 15))); |
344 }, timeout: new Timeout(new Duration(seconds: 10))); | 344 }, timeout: new Timeout(new Duration(seconds: 10))); |
345 }); | 345 }); |
346 | 346 |
347 expect(entries, hasLength(1)); | 347 expect(entries, hasLength(1)); |
348 expect(entries.single, new isInstanceOf<Group>()); | 348 var testGroup = entries.single as Group; |
349 expect(entries.single.metadata.timeout.duration, | 349 expect(testGroup.metadata.timeout.duration, |
350 equals(new Duration(seconds: 10))); | 350 equals(new Duration(seconds: 10))); |
351 expect(entries.single.entries, hasLength(1)); | 351 expect(testGroup.entries, hasLength(1)); |
352 expect(entries.single.entries.single, new isInstanceOf<Test>()); | 352 expect(testGroup.entries.single, new isInstanceOf<Test>()); |
353 expect(entries.single.entries.single.metadata.timeout.duration, | 353 expect(testGroup.entries.single.metadata.timeout.duration, |
354 equals(new Duration(seconds: 15))); | 354 equals(new Duration(seconds: 15))); |
355 }); | 355 }); |
356 | 356 |
357 group(".setUp()", () { | 357 group(".setUp()", () { |
358 test("is scoped to the group", () async { | 358 test("is scoped to the group", () async { |
359 var setUpRun = false; | 359 var setUpRun = false; |
360 var entries = declare(() { | 360 var entries = declare(() { |
361 group("group", () { | 361 group("group", () { |
362 setUp(() => setUpRun = true); | 362 setUp(() => setUpRun = true); |
363 | 363 |
364 test("description 1", expectAsync(() { | 364 test("description 1", expectAsync(() { |
365 expect(setUpRun, isTrue); | 365 expect(setUpRun, isTrue); |
366 setUpRun = false; | 366 setUpRun = false; |
367 }, max: 1)); | 367 }, max: 1)); |
368 }); | 368 }); |
369 | 369 |
370 test("description 2", expectAsync(() { | 370 test("description 2", expectAsync(() { |
371 expect(setUpRun, isFalse); | 371 expect(setUpRun, isFalse); |
372 setUpRun = false; | 372 setUpRun = false; |
373 }, max: 1)); | 373 }, max: 1)); |
374 }); | 374 }); |
375 | 375 |
376 await _runTest(entries[0].entries.single); | 376 await _runTest((entries[0] as Group).entries.single); |
377 await _runTest(entries[1]); | 377 await _runTest(entries[1]); |
378 }); | 378 }); |
379 | 379 |
380 test("runs from the outside in", () { | 380 test("runs from the outside in", () { |
381 var outerSetUpRun = false; | 381 var outerSetUpRun = false; |
382 var middleSetUpRun = false; | 382 var middleSetUpRun = false; |
383 var innerSetUpRun = false; | 383 var innerSetUpRun = false; |
384 var entries = declare(() { | 384 var entries = declare(() { |
385 setUp(expectAsync(() { | 385 setUp(expectAsync(() { |
386 expect(middleSetUpRun, isFalse); | 386 expect(middleSetUpRun, isFalse); |
(...skipping 17 matching lines...) Expand all Loading... |
404 | 404 |
405 test("description", expectAsync(() { | 405 test("description", expectAsync(() { |
406 expect(outerSetUpRun, isTrue); | 406 expect(outerSetUpRun, isTrue); |
407 expect(middleSetUpRun, isTrue); | 407 expect(middleSetUpRun, isTrue); |
408 expect(innerSetUpRun, isTrue); | 408 expect(innerSetUpRun, isTrue); |
409 }, max: 1)); | 409 }, max: 1)); |
410 }); | 410 }); |
411 }); | 411 }); |
412 }); | 412 }); |
413 | 413 |
414 return _runTest(entries.single.entries.single.entries.single); | 414 var middleGroup = entries.single as Group; |
| 415 var innerGroup = middleGroup.entries.single as Group; |
| 416 return _runTest(innerGroup.entries.single); |
415 }); | 417 }); |
416 | 418 |
417 test("handles Futures when chained", () { | 419 test("handles Futures when chained", () { |
418 var outerSetUpRun = false; | 420 var outerSetUpRun = false; |
419 var innerSetUpRun = false; | 421 var innerSetUpRun = false; |
420 var entries = declare(() { | 422 var entries = declare(() { |
421 setUp(expectAsync(() { | 423 setUp(expectAsync(() { |
422 expect(innerSetUpRun, isFalse); | 424 expect(innerSetUpRun, isFalse); |
423 return new Future(() => outerSetUpRun = true); | 425 return new Future(() => outerSetUpRun = true); |
424 }, max: 1)); | 426 }, max: 1)); |
425 | 427 |
426 group("inner", () { | 428 group("inner", () { |
427 setUp(expectAsync(() { | 429 setUp(expectAsync(() { |
428 expect(outerSetUpRun, isTrue); | 430 expect(outerSetUpRun, isTrue); |
429 return new Future(() => innerSetUpRun = true); | 431 return new Future(() => innerSetUpRun = true); |
430 }, max: 1)); | 432 }, max: 1)); |
431 | 433 |
432 test("description", expectAsync(() { | 434 test("description", expectAsync(() { |
433 expect(outerSetUpRun, isTrue); | 435 expect(outerSetUpRun, isTrue); |
434 expect(innerSetUpRun, isTrue); | 436 expect(innerSetUpRun, isTrue); |
435 }, max: 1)); | 437 }, max: 1)); |
436 }); | 438 }); |
437 }); | 439 }); |
438 | 440 |
439 return _runTest(entries.single.entries.single); | 441 var innerGroup = entries.single as Group; |
| 442 return _runTest(innerGroup.entries.single); |
440 }); | 443 }); |
441 | 444 |
442 test("inherits group's tags", () { | 445 test("inherits group's tags", () { |
443 var tests = declare(() { | 446 var tests = declare(() { |
444 group("outer", () { | 447 group("outer", () { |
445 group("inner", () { | 448 group("inner", () { |
446 test("with tags", () {}, tags: "d"); | 449 test("with tags", () {}, tags: "d"); |
447 }, tags: ["b", "c"]); | 450 }, tags: ["b", "c"]); |
448 }, tags: "a"); | 451 }, tags: "a"); |
449 }); | 452 }); |
450 | 453 |
451 var outerGroup = tests.single; | 454 var outerGroup = tests.single as Group; |
452 var innerGroup = outerGroup.entries.single; | 455 var innerGroup = outerGroup.entries.single as Group; |
453 var testWithTags = innerGroup.entries.single; | 456 var testWithTags = innerGroup.entries.single; |
454 expect(outerGroup.metadata.tags, unorderedEquals(["a"])); | 457 expect(outerGroup.metadata.tags, unorderedEquals(["a"])); |
455 expect(innerGroup.metadata.tags, unorderedEquals(["a", "b", "c"])); | 458 expect(innerGroup.metadata.tags, unorderedEquals(["a", "b", "c"])); |
456 expect(testWithTags.metadata.tags, | 459 expect(testWithTags.metadata.tags, |
457 unorderedEquals(["a", "b", "c", "d"])); | 460 unorderedEquals(["a", "b", "c", "d"])); |
458 }); | 461 }); |
459 | 462 |
460 test("throws on invalid tags", () { | 463 test("throws on invalid tags", () { |
461 expect(() { | 464 expect(() { |
462 declare(() { | 465 declare(() { |
(...skipping 15 matching lines...) Expand all Loading... |
478 test("description 1", expectAsync(() { | 481 test("description 1", expectAsync(() { |
479 expect(tearDownRun, isFalse); | 482 expect(tearDownRun, isFalse); |
480 }, max: 1)); | 483 }, max: 1)); |
481 }); | 484 }); |
482 | 485 |
483 test("description 2", expectAsync(() { | 486 test("description 2", expectAsync(() { |
484 expect(tearDownRun, isFalse); | 487 expect(tearDownRun, isFalse); |
485 }, max: 1)); | 488 }, max: 1)); |
486 }); | 489 }); |
487 | 490 |
488 await _runTest(entries[0].entries.single); | 491 var testGroup = entries[0] as Group; |
| 492 await _runTest(testGroup.entries.single); |
489 expect(tearDownRun, isTrue); | 493 expect(tearDownRun, isTrue); |
490 await _runTest(entries[1]); | 494 await _runTest(entries[1]); |
491 expect(tearDownRun, isFalse); | 495 expect(tearDownRun, isFalse); |
492 }); | 496 }); |
493 | 497 |
494 test("runs from the inside out", () async { | 498 test("runs from the inside out", () async { |
495 var innerTearDownRun = false; | 499 var innerTearDownRun = false; |
496 var middleTearDownRun = false; | 500 var middleTearDownRun = false; |
497 var outerTearDownRun = false; | 501 var outerTearDownRun = false; |
498 var entries = declare(() { | 502 var entries = declare(() { |
(...skipping 19 matching lines...) Expand all Loading... |
518 | 522 |
519 test("description", expectAsync(() { | 523 test("description", expectAsync(() { |
520 expect(outerTearDownRun, isFalse); | 524 expect(outerTearDownRun, isFalse); |
521 expect(middleTearDownRun, isFalse); | 525 expect(middleTearDownRun, isFalse); |
522 expect(innerTearDownRun, isFalse); | 526 expect(innerTearDownRun, isFalse); |
523 }, max: 1)); | 527 }, max: 1)); |
524 }); | 528 }); |
525 }); | 529 }); |
526 }); | 530 }); |
527 | 531 |
528 await _runTest(entries.single.entries.single.entries.single); | 532 var middleGroup = entries.single as Group; |
| 533 var innerGroup = middleGroup.entries.single as Group; |
| 534 await _runTest(innerGroup.entries.single); |
529 expect(innerTearDownRun, isTrue); | 535 expect(innerTearDownRun, isTrue); |
530 expect(middleTearDownRun, isTrue); | 536 expect(middleTearDownRun, isTrue); |
531 expect(outerTearDownRun, isTrue); | 537 expect(outerTearDownRun, isTrue); |
532 }); | 538 }); |
533 | 539 |
534 test("handles Futures when chained", () async { | 540 test("handles Futures when chained", () async { |
535 var outerTearDownRun = false; | 541 var outerTearDownRun = false; |
536 var innerTearDownRun = false; | 542 var innerTearDownRun = false; |
537 var entries = declare(() { | 543 var entries = declare(() { |
538 tearDown(expectAsync(() { | 544 tearDown(expectAsync(() { |
539 expect(innerTearDownRun, isTrue); | 545 expect(innerTearDownRun, isTrue); |
540 return new Future(() => outerTearDownRun = true); | 546 return new Future(() => outerTearDownRun = true); |
541 }, max: 1)); | 547 }, max: 1)); |
542 | 548 |
543 group("inner", () { | 549 group("inner", () { |
544 tearDown(expectAsync(() { | 550 tearDown(expectAsync(() { |
545 expect(outerTearDownRun, isFalse); | 551 expect(outerTearDownRun, isFalse); |
546 return new Future(() => innerTearDownRun = true); | 552 return new Future(() => innerTearDownRun = true); |
547 }, max: 1)); | 553 }, max: 1)); |
548 | 554 |
549 test("description", expectAsync(() { | 555 test("description", expectAsync(() { |
550 expect(outerTearDownRun, isFalse); | 556 expect(outerTearDownRun, isFalse); |
551 expect(innerTearDownRun, isFalse); | 557 expect(innerTearDownRun, isFalse); |
552 }, max: 1)); | 558 }, max: 1)); |
553 }); | 559 }); |
554 }); | 560 }); |
555 | 561 |
556 await _runTest(entries.single.entries.single); | 562 var innerGroup = entries.single as Group; |
| 563 await _runTest(innerGroup.entries.single); |
557 expect(innerTearDownRun, isTrue); | 564 expect(innerTearDownRun, isTrue); |
558 expect(outerTearDownRun, isTrue); | 565 expect(outerTearDownRun, isTrue); |
559 }); | 566 }); |
560 | 567 |
561 test("runs outer callbacks even when inner ones fail", () async { | 568 test("runs outer callbacks even when inner ones fail", () async { |
562 var outerTearDownRun = false; | 569 var outerTearDownRun = false; |
563 var entries = declare(() { | 570 var entries = declare(() { |
564 tearDown(() { | 571 tearDown(() { |
565 return new Future(() => outerTearDownRun = true); | 572 return new Future(() => outerTearDownRun = true); |
566 }); | 573 }); |
567 | 574 |
568 group("inner", () { | 575 group("inner", () { |
569 tearDown(() { | 576 tearDown(() { |
570 throw 'inner error'; | 577 throw 'inner error'; |
571 }); | 578 }); |
572 | 579 |
573 test("description", expectAsync(() { | 580 test("description", expectAsync(() { |
574 expect(outerTearDownRun, isFalse); | 581 expect(outerTearDownRun, isFalse); |
575 }, max: 1)); | 582 }, max: 1)); |
576 }); | 583 }); |
577 }); | 584 }); |
578 | 585 |
579 await _runTest(entries.single.entries.single, shouldFail: true); | 586 var innerGroup = entries.single as Group; |
| 587 await _runTest(innerGroup.entries.single, shouldFail: true); |
580 expect(outerTearDownRun, isTrue); | 588 expect(outerTearDownRun, isTrue); |
581 }); | 589 }); |
582 }); | 590 }); |
583 }); | 591 }); |
584 } | 592 } |
585 | 593 |
586 /// Runs [test]. | 594 /// Runs [test]. |
587 /// | 595 /// |
588 /// This automatically sets up an `onError` listener to ensure that the test | 596 /// This automatically sets up an `onError` listener to ensure that the test |
589 /// doesn't throw any invisible exceptions. | 597 /// doesn't throw any invisible exceptions. |
590 Future _runTest(Test test, {bool shouldFail: false}) { | 598 Future _runTest(Test test, {bool shouldFail: false}) { |
591 var liveTest = test.load(_suite); | 599 var liveTest = test.load(_suite); |
592 | 600 |
593 liveTest.onError.listen(shouldFail | 601 liveTest.onError.listen(shouldFail |
594 ? expectAsync((_) {}) | 602 ? expectAsync((_) {}) |
595 : (error) => registerException(error.error, error.stackTrace)); | 603 : (error) => registerException(error.error, error.stackTrace)); |
596 | 604 |
597 return liveTest.run(); | 605 return liveTest.run(); |
598 } | 606 } |
OLD | NEW |