PageRenderTime 60ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/asunit3/asunit/textui/ResultPrinter.as

http://github.com/Stray/RobotEyes
ActionScript | 298 lines | 213 code | 42 blank | 43 comment | 13 complexity | 4c206860fd59fc5c6af449b149ce260c MD5 | raw file
  1. package asunit.textui {
  2. import asunit.errors.AssertionFailedError;
  3. import asunit.framework.Test;
  4. import asunit.framework.TestFailure;
  5. import asunit.framework.TestListener;
  6. import asunit.framework.TestResult;
  7. import asunit.runner.BaseTestRunner;
  8. import asunit.runner.Version;
  9. import flash.display.Sprite;
  10. import flash.events.*;
  11. import flash.system.Capabilities;
  12. import flash.text.TextField;
  13. import flash.text.TextFormat;
  14. import flash.utils.getTimer;
  15. import flash.utils.setInterval;
  16. import flash.utils.setTimeout;
  17. /**
  18. * This is the base class for collecting test output and formatting for different
  19. * displays.
  20. *
  21. * This class simply presents test results as if they were being shown on a terminal.
  22. *
  23. * The <code>XMLResultPrinter</code> provides a good example of how this class can
  24. * be subclassed and used to emit different/additional output.
  25. *
  26. * @see XMLResultPrinter
  27. **/
  28. public class ResultPrinter extends Sprite implements TestListener {
  29. private var fColumn:int = 0;
  30. private var textArea:TextField;
  31. private var gutter:uint = 0;
  32. private var backgroundColor:uint = 0x333333;
  33. private var bar:SuccessBar;
  34. private var barHeight:Number = 3;
  35. private var showTrace:Boolean;
  36. protected var startTime:Number;
  37. protected var testTimes:Array;
  38. public function ResultPrinter(showTrace:Boolean = false) {
  39. this.showTrace = showTrace;
  40. testTimes = new Array();
  41. configureAssets();
  42. println();
  43. // Create a loop so that the FDBTask
  44. // can halt execution properly:
  45. setInterval(function():void {
  46. }, 500);
  47. }
  48. private function configureAssets():void {
  49. textArea = new TextField();
  50. textArea.background = true;
  51. textArea.backgroundColor = backgroundColor;
  52. textArea.border = true;
  53. textArea.wordWrap = true;
  54. var format:TextFormat = new TextFormat();
  55. format.font = "Verdana";
  56. format.size = 10;
  57. format.color = 0xFFFFFF;
  58. textArea.defaultTextFormat = format;
  59. addChild(textArea);
  60. println("AsUnit " + Version.id() + " by Luke Bayes and Ali Mills");
  61. println("");
  62. println("Flash Player version: " + Capabilities.version);
  63. bar = new SuccessBar();
  64. addChild(bar);
  65. }
  66. public function setShowTrace(showTrace:Boolean):void {
  67. this.showTrace = showTrace;
  68. }
  69. public override function set width(w:Number):void {
  70. textArea.x = gutter;
  71. textArea.width = w - gutter*2;
  72. bar.x = gutter;
  73. bar.width = textArea.width;
  74. }
  75. public override function set height(h:Number):void {
  76. textArea.height = h - ((gutter*2) + barHeight);
  77. textArea.y = gutter;
  78. bar.y = h - (gutter + barHeight);
  79. bar.height = barHeight;
  80. }
  81. public function println(...args:Array):void {
  82. textArea.appendText(args.toString() + "\n");
  83. }
  84. public function print(...args:Array):void {
  85. textArea.appendText(args.toString());
  86. }
  87. /**
  88. * API for use by textui.TestRunner
  89. */
  90. public function run(test:Test):void {
  91. }
  92. public function printResult(result:TestResult, runTime:Number):void {
  93. printHeader(runTime);
  94. printErrors(result);
  95. printFailures(result);
  96. printFooter(result);
  97. bar.setSuccess(result.wasSuccessful());
  98. if(showTrace) {
  99. trace(textArea.text.split("\r").join("\n"));
  100. }
  101. }
  102. /* Internal methods
  103. */
  104. protected function printHeader(runTime:Number):void {
  105. println();
  106. println();
  107. println("Time: " + elapsedTimeAsString(runTime));
  108. }
  109. protected function printErrors(result:TestResult):void {
  110. printDefects(result.errors(), result.errorCount(), "error");
  111. }
  112. protected function printFailures(result:TestResult):void {
  113. printDefects(result.failures(), result.failureCount(), "failure");
  114. }
  115. protected function printDefects(booBoos:Object, count:int, type:String):void {
  116. if (count == 0) {
  117. return;
  118. }
  119. if (count == 1) {
  120. println("There was " + count + " " + type + ":");
  121. }
  122. else {
  123. println("There were " + count + " " + type + "s:");
  124. }
  125. var i:uint;
  126. for each (var item:TestFailure in booBoos) {
  127. printDefect(TestFailure(item), i);
  128. i++;
  129. }
  130. }
  131. public function printDefect(booBoo:TestFailure, count:int ):void { // only public for testing purposes
  132. printDefectHeader(booBoo, count);
  133. printDefectTrace(booBoo);
  134. }
  135. protected function printDefectHeader(booBoo:TestFailure, count:int):void {
  136. // I feel like making this a println, then adding a line giving the throwable a chance to print something
  137. // before we get to the stack trace.
  138. var startIndex:uint = textArea.text.length;
  139. println(count + ") " + booBoo.failedFeature());
  140. var endIndex:uint = textArea.text.length;
  141. var format:TextFormat = textArea.getTextFormat();
  142. format.bold = true;
  143. // GROSS HACK because of bug in flash player - TextField isn't accepting formats...
  144. setTimeout(onFormatTimeout, 1, format, startIndex, endIndex);
  145. }
  146. public function onFormatTimeout(format:TextFormat, startIndex:uint, endIndex:uint):void {
  147. textArea.setTextFormat(format, startIndex, endIndex);
  148. }
  149. protected function printDefectTrace(booBoo:TestFailure):void {
  150. println(BaseTestRunner.getFilteredTrace(booBoo.thrownException().getStackTrace()));
  151. }
  152. protected function printFooter(result:TestResult):void {
  153. println();
  154. if (result.wasSuccessful()) {
  155. print("OK");
  156. println (" (" + result.runCount() + " test" + (result.runCount() == 1 ? "": "s") + ")");
  157. } else {
  158. println("FAILURES!!!");
  159. println("Tests run: " + result.runCount()+
  160. ", Failures: "+result.failureCount()+
  161. ", Errors: "+result.errorCount());
  162. }
  163. printTimeSummary();
  164. println();
  165. }
  166. protected function printTimeSummary():void {
  167. testTimes.sortOn('duration', Array.NUMERIC | Array.DESCENDING);
  168. println();
  169. println();
  170. println('Time Summary:');
  171. println();
  172. var len:Number = testTimes.length;
  173. for(var i:Number = 0; i < len; i++) {
  174. println(testTimes[i].toString());
  175. }
  176. }
  177. /**
  178. * Returns the formatted string of the elapsed time.
  179. * Duplicated from BaseTestRunner. Fix it.
  180. */
  181. protected function elapsedTimeAsString(runTime:Number):String {
  182. return Number(runTime/1000).toString();
  183. }
  184. /**
  185. * @see asunit.framework.TestListener#addError(Test, Throwable)
  186. */
  187. public function addError(test:Test, t:Error):void {
  188. print("E");
  189. }
  190. /**
  191. * @see asunit.framework.TestListener#addFailure(Test, AssertionFailedError)
  192. */
  193. public function addFailure(test:Test, t:AssertionFailedError):void {
  194. print("F");
  195. }
  196. /**
  197. * @see asunit.framework.TestListener#endTestMethod(test, testMethod);
  198. */
  199. public function startTestMethod(test:Test, methodName:String):void {
  200. }
  201. /**
  202. * @see asunit.framework.TestListener#endTestMethod(test, testMethod);
  203. */
  204. public function endTestMethod(test:Test, methodName:String):void {
  205. }
  206. /**
  207. * @see asunit.framework.TestListener#startTest(Test)
  208. */
  209. public function startTest(test:Test):void {
  210. startTime = getTimer();
  211. var count:uint = test.countTestCases();
  212. for(var i:uint; i < count; i++) {
  213. print(".");
  214. if (fColumn++ >= 80) {
  215. println();
  216. fColumn = 0;
  217. }
  218. }
  219. }
  220. /**
  221. * @see asunit.framework.TestListener#endTest(Test)
  222. */
  223. public function endTest(test:Test):void {
  224. var duration:Number = getTimer() - startTime;
  225. testTimes.push(TestTime.create(test, duration));
  226. }
  227. }
  228. }
  229. import flash.display.Sprite;
  230. class SuccessBar extends Sprite {
  231. private var myWidth:uint;
  232. private var myHeight:uint;
  233. private var bgColor:uint;
  234. private var passingColor:uint = 0x00FF00;
  235. private var failingColor:uint = 0xFD0000;
  236. public function SuccessBar() {
  237. }
  238. public function setSuccess(success:Boolean):void {
  239. bgColor = (success) ? passingColor : failingColor;
  240. draw();
  241. }
  242. public override function set width(num:Number):void {
  243. myWidth = num;
  244. draw();
  245. }
  246. public override function set height(num:Number):void {
  247. myHeight = num;
  248. draw();
  249. }
  250. private function draw():void {
  251. graphics.clear();
  252. graphics.beginFill(bgColor);
  253. graphics.drawRect(0, 0, myWidth, myHeight);
  254. graphics.endFill();
  255. }
  256. }