comparison bindings/as3/ext/asunit/framework/TestSuite.as @ 732:3a0b9700b3d2

* Initial AS3 commit
author mas01mj
date Tue, 14 Sep 2010 16:47:10 +0000
parents
children
comparison
equal deleted inserted replaced
731:65134dd772fc 732:3a0b9700b3d2
1 package asunit.framework {
2 import asunit.util.ArrayIterator;
3 import asunit.util.Iterator;
4
5 import flash.display.DisplayObjectContainer;
6 import flash.events.Event;
7
8 /**
9 * A <code>TestSuite</code> is a <code>Composite</code> of Tests.
10 *
11 * @see Test
12 * @see TestCase
13 *
14 * @includeExample TestSuiteExample.as
15 */
16 public class TestSuite extends TestCase implements Test {
17 private var fTests:Array = new Array();
18 private var testsCompleteCount:Number = 0;
19 private var iterator:ArrayIterator;
20 private var isRunning:Boolean;
21
22 public function TestSuite() {
23 super();
24 fTests = new Array();
25 }
26
27 protected override function setTestMethods(methodNodes:XMLList):void {
28 testMethods = new Array();
29 }
30
31 /**
32 * Adds a test instance to the suite.
33 */
34 public function addTest(test:Test):void {
35 if (!test.getIsComplete())
36 fTests.push(test);
37 }
38
39 /**
40 * Counts the number of tests that will be run by this Suite.
41 */
42 public override function countTestCases():int {
43 var count:int;
44 for each(var test:TestCase in fTests) {
45 count = count + test.countTestCases();
46 }
47 return count;
48 }
49
50 /**
51 * Runs the tests and collects their result in a TestResult.
52 */
53 public override function run():void {
54 var result:TestListener = getResult();
55 var test:Test;
56 var itr:Iterator = getIterator();
57 while(itr.hasNext()) {
58 isRunning = true;
59 test = Test(itr.next());
60 test.setResult(result);
61 test.addEventListener(Event.COMPLETE, testCompleteHandler);
62 test.run();
63 if(!test.getIsComplete()) {
64 isRunning = false;
65 break;
66 }
67 }
68 }
69
70 private function getIterator():ArrayIterator {
71 if(iterator == null) {
72 iterator = new ArrayIterator(fTests);
73 }
74 return iterator;
75 }
76
77 private function testCompleteHandler(event:Event):void {
78 if(!isRunning) {
79 run();
80 }
81 if(++testsCompleteCount >= testCount()) {
82 dispatchEvent(new Event(Event.COMPLETE));
83 }
84 }
85
86 /**
87 * Returns the number of tests in this suite
88 */
89 public function testCount():int {
90 return fTests.length;
91 }
92
93 public override function toString():String {
94 return getName();
95 }
96
97 public override function getIsComplete():Boolean {
98 for each(var test:TestCase in fTests) {
99 if(!test.getIsComplete()) {
100 return false;
101 }
102 }
103 return true;
104 }
105
106 public override function setContext(context:DisplayObjectContainer):void {
107 super.setContext(context);
108 for each(var test:Test in fTests) {
109 test.setContext(context);
110 }
111 }
112 }
113 }