1
16
17 import org.as2lib.core.BasicClass;
18 import org.as2lib.data.holder.array.ArrayIterator;
19 import org.as2lib.data.holder.array.TypedArray;
20 import org.as2lib.data.holder.Iterator;
21 import org.as2lib.test.unit.TestResult;
22 import org.as2lib.test.unit.TestCase;
23 import org.as2lib.test.unit.TestCaseMethodInfo;
24 import org.as2lib.env.reflect.ClassInfo;
25 import org.as2lib.env.reflect.MethodInfo;
26 import org.as2lib.util.StringUtil;
27 import org.as2lib.data.type.Time;
28
29
40 class org.as2lib.test.unit.TestCaseResult extends BasicClass implements TestResult {
41
42
43 private var testCase:TestCase;
44
45
46 private var testCaseMethodInfos:TypedArray;
47
48
49 private var finished:Boolean;
50
51
52 private var started:Boolean;
53
54
59 public function TestCaseResult(testCase:TestCase) {
60 this.testCase = testCase;
61 this.started = false;
62 this.finished = false;
63 }
64
65
74 public function getMethodInfos(Void):TypedArray {
75
76
77
78 if(!testCaseMethodInfos){
79 testCaseMethodInfos = fetchTestCaseMethodInfos();
80 }
81 return testCaseMethodInfos;
82 }
83
84
89 private function fetchTestCaseMethodInfos(Void):TypedArray {
90 var result:TypedArray = new TypedArray(TestCaseMethodInfo);
91 var methods:Array = ClassInfo.forInstance(testCase).getMethods();
92 if(methods) {
93 for (var i:Number = methods.length-1; i >= 0; i--) {
94 var method:MethodInfo = methods[i];
95 if (StringUtil.startsWith(method.getName(), "test")) {
96 result.push(new TestCaseMethodInfo(method));
97 }
98 }
99 }
100 return result;
101 }
102
103
108 public function getTestCase(Void):TestCase {
109 return testCase;
110 }
111
112
117 public function getName(Void):String {
118 return ClassInfo.forInstance(getTestCase()).getFullName();
119 }
120
121
126 public function getTestResults(Void):TypedArray {
127 var result:TypedArray = new TypedArray(TestResult);
128 result.push(this);
129 return result;
130 }
131
132
138 public function getTestCaseResults(Void):TypedArray {
139 var result:TypedArray = new TypedArray(TestCaseResult);
140 result.push(this);
141 return result;
142 }
143
144
149 public function getPercentage(Void):Number {
150 var finished:Number = 0;
151
152 var a:Array = getMethodInfos();
153 var total:Number = a.length;
154 var i:Number = a.length;
155
156 while(--i-(-1)) {
157 if(a[i].hasFinished()) {
158 finished ++;
159 }
160 }
161
162 return (100/total*finished);
163 }
164
165
170 public function hasFinished(Void):Boolean {
171 if (finished) return true;
172 var methodIterator:Iterator = new ArrayIterator(getMethodInfos());
173 while (methodIterator.hasNext()) {
174 if(!methodIterator.next().hasFinished()) {
175 return false;
176 }
177 }
178 return (finished=true);
179 }
180
181
186 public function hasStarted(Void):Boolean {
187 if (started) return true;
188 var methodIterator:Iterator = new ArrayIterator(getMethodInfos());
189 while (methodIterator.hasNext()) {
190 if (methodIterator.next().hasFinished()) {
191 return (started=true);
192 }
193 }
194 return false;
195 }
196
197
202 public function getOperationTime(Void):Time {
203 var result:Number = 0;
204 var methodIterator:Iterator = new ArrayIterator(getMethodInfos());
205 while (methodIterator.hasNext()) {
206 result += methodIterator.next().getStopWatch().getTimeInMilliSeconds();
207 }
208 return (new Time(result));
209 }
210
211
216 public function hasErrors(Void):Boolean {
217 var methodIterator:Iterator = new ArrayIterator(getMethodInfos());
218 while (methodIterator.hasNext()) {
219 if (methodIterator.next().hasErrors()) {
220 return true;
221 }
222 }
223 return false;
224 }
225
226
231 public function toString():String {
232 var result:String;
233 var methodResult:String = "";
234 var ms:Number = 0;
235 var errors:Number = 0;
236 var methodInfos:Array = getMethodInfos();
237 var iter:Iterator = new ArrayIterator(methodInfos);
238 while (iter.hasNext()) {
239 var method:TestCaseMethodInfo = iter.next();
240 ms += method.getStopWatch().getTimeInMilliSeconds();
241 if(method.hasErrors()) {
242 errors += method.getErrors().length;
243 methodResult += "\n"+StringUtil.addSpaceIndent(method.toString(), 3);
244 }
245 }
246
247 result = getName()+" run "+methodInfos.length+" methods in ["+ms+"ms]. ";
248
249 result += (errors>0) ? errors + ((errors > 1) ? " errors" : " error") + " occured" + methodResult : "no error occured";
250
251 return result;
252 }
253 }