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