Mercurial > hg > audiodb
comparison bindings/as3/ext/asunit/textui/XMLResultPrinter.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.textui { | |
2 | |
3 import asunit.errors.AssertionFailedError; | |
4 import asunit.framework.Test; | |
5 import asunit.framework.TestListener; | |
6 import asunit.framework.TestResult; | |
7 import flash.utils.setTimeout; | |
8 import flash.utils.Dictionary; | |
9 | |
10 /** | |
11 * The <code>XMLResultPrinter</code> is used to transform AsUnit test results | |
12 * to JUnit-compatible XML content. | |
13 * | |
14 * This printer will send JUnit-compatible XML content to trace output. The XML content | |
15 * will be enclosed by '<XMLResultPrinter/>' tags. | |
16 * | |
17 * @includeExample XMLResultPrinterExample.as | |
18 * @includeExample XMLResultPrinterExample.xml | |
19 **/ | |
20 public class XMLResultPrinter extends ResultPrinter { | |
21 | |
22 protected var results:Dictionary; | |
23 | |
24 public function XMLResultPrinter() { | |
25 results = new Dictionary(); | |
26 } | |
27 | |
28 override public function startTest(test:Test):void { | |
29 super.startTest(test); | |
30 var result:TestListener = new XMLTestResult(test); | |
31 results[test.getName()] = result; | |
32 result.startTest(test); | |
33 } | |
34 | |
35 override public function endTest(test:Test):void { | |
36 super.endTest(test); | |
37 results[test.getName()].endTest(test); | |
38 } | |
39 | |
40 override public function startTestMethod(test:Test, methodName:String):void { | |
41 super.startTestMethod(test, methodName); | |
42 results[test.getName()].startTestMethod(test, methodName); | |
43 } | |
44 | |
45 override public function endTestMethod(test:Test, methodName:String):void { | |
46 super.endTestMethod(test, methodName); | |
47 results[test.getName()].endTestMethod(test, methodName); | |
48 } | |
49 | |
50 override public function addFailure(test:Test, t:AssertionFailedError):void { | |
51 super.addFailure(test, t); | |
52 results[test.getName()].addFailure(test, t); | |
53 } | |
54 | |
55 override public function addError(test:Test, t:Error):void { | |
56 super.addError(test, t); | |
57 results[test.getName()].addError(test, t); | |
58 } | |
59 | |
60 override public function printResult(result:TestResult, runTime:Number):void { | |
61 super.printResult(result, runTime); | |
62 trace("<XMLResultPrinter>"); | |
63 trace("<?xml version='1.0' encoding='UTF-8'?>"); | |
64 trace("<testsuites>"); | |
65 trace("<testsuite name='AllTests' errors='" + result.errorCount() + "' failures='" + result.failureCount() + "' tests='" + result.runCount() + "' time='" + elapsedTimeAsString(runTime) + "'>"); | |
66 var xmlTestResult:XMLTestResult; | |
67 for each(xmlTestResult in results) { | |
68 trace(xmlTestResult.toString()); | |
69 } | |
70 trace("</testsuite>"); | |
71 trace("</testsuites>"); | |
72 trace("</XMLResultPrinter>"); | |
73 } | |
74 } | |
75 } | |
76 | |
77 import asunit.framework.Test; | |
78 import asunit.framework.TestFailure; | |
79 import flash.utils.getQualifiedClassName; | |
80 import flash.utils.getTimer; | |
81 import asunit.framework.TestListener; | |
82 import asunit.errors.AssertionFailedError; | |
83 import asunit.framework.TestMethod; | |
84 import flash.utils.Dictionary; | |
85 | |
86 class XMLTestResult implements TestListener { | |
87 | |
88 private var _duration:Number; | |
89 private var start:Number; | |
90 private var test:Test; | |
91 private var testName:String; | |
92 private var failureHash:Dictionary; | |
93 private var failures:Array; | |
94 private var errorHash:Dictionary; | |
95 private var errors:Array; | |
96 private var methodHash:Dictionary; | |
97 private var methods:Array; | |
98 | |
99 public function XMLTestResult(test:Test) { | |
100 this.test = test; | |
101 testName = test.getName().split("::").join("."); | |
102 failures = new Array(); | |
103 errors = new Array(); | |
104 methods = new Array(); | |
105 | |
106 failureHash = new Dictionary(); | |
107 errorHash = new Dictionary(); | |
108 methodHash = new Dictionary(); | |
109 } | |
110 | |
111 public function startTest(test:Test):void { | |
112 start = getTimer(); | |
113 } | |
114 | |
115 public function run(test:Test):void { | |
116 } | |
117 | |
118 public function addError(test:Test, t:Error):void { | |
119 var failure:TestFailure = new TestFailure(test, t); | |
120 errors.push(failure); | |
121 errorHash[failure.failedMethod()] = failure; | |
122 } | |
123 | |
124 public function addFailure(test:Test, t:AssertionFailedError):void { | |
125 var failure:TestFailure = new TestFailure(test, t); | |
126 failures.push(failure); | |
127 failureHash[failure.failedMethod()] = failure; | |
128 } | |
129 | |
130 public function startTestMethod(test:Test, methodName:String):void { | |
131 var method:TestMethod = new TestMethod(test, methodName); | |
132 methods.push(method); | |
133 methodHash[method.getName()] = method; | |
134 } | |
135 | |
136 public function endTestMethod(test:Test, methodName:String):void { | |
137 methodHash[methodName].endTest(test); | |
138 } | |
139 | |
140 public function endTest(test:Test):void { | |
141 _duration = (getTimer() - start) * .001; | |
142 } | |
143 | |
144 private function errorCount():int { | |
145 return errors.length; | |
146 } | |
147 | |
148 private function failureCount():int { | |
149 return failures.length; | |
150 } | |
151 | |
152 private function duration():Number { | |
153 return _duration; | |
154 } | |
155 | |
156 private function renderSuiteOpener():String { | |
157 return "<testsuite name='" + testName + "' errors='" + errorCount() + "' failures='" + failureCount() + "' tests='" + methods.length + "' time='" + duration() + "'>\n"; | |
158 } | |
159 | |
160 private function renderTestOpener(methodName:String):String { | |
161 return "<testcase classname='" + testName + "' name='" + methodName + "' time='" + methodHash[methodName].duration() + "'>\n"; | |
162 } | |
163 | |
164 private function renderTestBody(method:String):String { | |
165 if(errorHash[method]) { | |
166 return renderError(errorHash[method]); | |
167 } | |
168 else if(failureHash[method]) { | |
169 return renderFailure(failureHash[method]); | |
170 } | |
171 else { | |
172 return ""; | |
173 } | |
174 } | |
175 | |
176 private function renderError(failure:TestFailure):String { | |
177 return "<error type='" + getQualifiedClassName(failure.thrownException()).split("::").join(".") + "'><![CDATA[\n" + failure.thrownException().getStackTrace() + "\n]]></error>\n"; | |
178 } | |
179 | |
180 private function renderFailure(failure:TestFailure):String { | |
181 return "<failure type='" + getQualifiedClassName(failure.thrownException()).split("::").join(".") + "'><![CDATA[\n" + failure.thrownException().getStackTrace() + "\n]]></failure>\n"; | |
182 } | |
183 | |
184 private function renderTestCloser():String { | |
185 return '</testcase>\n'; | |
186 } | |
187 | |
188 private function renderSuiteCloser():String { | |
189 return '</testsuite>\n'; | |
190 } | |
191 | |
192 public function toString():String { | |
193 var str:String = ''; | |
194 str += renderSuiteOpener(); | |
195 for(var name:String in methodHash) { | |
196 str += renderTestOpener(name); | |
197 str += renderTestBody(name); | |
198 str += renderTestCloser(); | |
199 } | |
200 str += renderSuiteCloser(); | |
201 return str; | |
202 } | |
203 } | |
204 |