comparison bindings/as3/ext/asunit/framework/TestResult.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.errors.AssertionFailedError;
3 import asunit.errors.InstanceNotFoundError;
4
5 /**
6 * A <code>TestResult</code> collects the results of executing
7 * a test case. It is an instance of the Collecting Parameter pattern.
8 * The test framework distinguishes between <i>failures</i> and <i>errors</i>.
9 * A failure is anticipated and checked for with assertions. Errors are
10 * unanticipated problems like an <code>ArrayIndexOutOfBoundsException</code>.
11 *
12 * @see Test
13 */
14 public class TestResult implements TestListener {
15 protected var fFailures:Array;
16 protected var fErrors:Array;
17 protected var fListeners:Array;
18 protected var fRunTests:int;
19 private var fStop:Boolean;
20
21 public function TestResult() {
22 fFailures = new Array();
23 fErrors = new Array();
24 fListeners = new Array();
25 fRunTests = 0;
26 fStop = false;
27 }
28 /**
29 * Adds an error to the list of errors. The passed in exception
30 * caused the error.
31 */
32 public function addError(test:Test, t:Error):void {
33 fErrors.push(new TestFailure(test, t));
34 var len:uint = fListeners.length;
35 var item:TestListener;
36 for(var i:uint; i < len; i++) {
37 item = TestListener(fListeners[i]);
38 item.addError(test, t);
39 }
40 }
41 /**
42 * Adds a failure to the list of failures. The passed in exception
43 * caused the failure.
44 */
45 public function addFailure(test:Test, t:AssertionFailedError):void {
46 fFailures.push(new TestFailure(test, t));
47 var len:uint = fListeners.length;
48 var item:TestListener;
49 for(var i:uint; i < len; i++) {
50 item = TestListener(fListeners[i]);
51 item.addFailure(test, t);
52 }
53 }
54 /**
55 * Registers a TestListener
56 */
57 public function addListener(listener:TestListener):void {
58 fListeners.push(listener);
59 }
60 /**
61 * Unregisters a TestListener
62 */
63 public function removeListener(listener:TestListener):void {
64 var len:uint = fListeners.length;
65 for(var i:uint; i < len; i++) {
66 if(fListeners[i] == listener) {
67 fListeners.splice(i, 1);
68 return;
69 }
70 }
71 throw new InstanceNotFoundError("removeListener called without listener in list");
72 }
73 /**
74 * Gets the number of detected errors.
75 */
76 public function errorCount():int {
77 return fErrors.length;
78 }
79 /**
80 * Returns an Enumeration for the errors
81 */
82 public function errors():Array {
83 return fErrors;
84 }
85 /**
86 * Gets the number of detected failures.
87 */
88 public function failureCount():int {
89 return fFailures.length;
90 }
91 /**
92 * Returns an Enumeration for the failures
93 */
94 public function failures():Array {
95 return fFailures;
96 }
97
98 /**
99 * Runs a TestCase.
100 */
101 public function run(test:Test):void {
102 startTest(test);
103 test.runBare();
104 }
105 /**
106 * Gets the number of run tests.
107 */
108 public function runCount():int {
109 return fRunTests;
110 }
111 /**
112 * Checks whether the test run should stop
113 */
114 public function shouldStop():Boolean {
115 return fStop;
116 }
117 /**
118 * Informs the result that a test will be started.
119 */
120 public function startTest(test:Test):void {
121 var count:int = test.countTestCases();
122 fRunTests += count;
123
124 var len:uint = fListeners.length;
125 var item:TestListener;
126 for(var i:uint; i < len; i++) {
127 item = TestListener(fListeners[i]);
128 item.startTest(test);
129 }
130 }
131
132 public function startTestMethod(test:Test, method:String):void {
133 var len:uint = fListeners.length;
134 var item:TestListener;
135 for(var i:uint; i < len; i++) {
136 item = TestListener(fListeners[i]);
137 item.startTestMethod(test, method);
138 }
139 }
140
141 public function endTestMethod(test:Test, method:String):void {
142 var len:uint = fListeners.length;
143 var item:TestListener;
144 for(var i:uint; i < len; i++) {
145 item = TestListener(fListeners[i]);
146 item.endTestMethod(test, method);
147 }
148 }
149
150 /**
151 * Informs the result that a test was completed.
152 */
153 public function endTest(test:Test):void {
154 var len:uint = fListeners.length;
155 var item:TestListener;
156 for(var i:uint; i < len; i++) {
157 item = TestListener(fListeners[i]);
158 item.endTest(test);
159 }
160 }
161 /**
162 * Marks that the test run should stop.
163 */
164 public function stop():void {
165 fStop = true;
166 }
167 /**
168 * Returns whether the entire test was successful or not.
169 */
170 public function wasSuccessful():Boolean {
171 return failureCount() == 0 && errorCount() == 0;
172 }
173 }
174 }