mas01mj@732: package asunit.framework { mas01mj@732: import asunit.errors.AssertionFailedError; mas01mj@732: import asunit.errors.InstanceNotFoundError; mas01mj@732: mas01mj@732: /** mas01mj@732: * A TestResult collects the results of executing mas01mj@732: * a test case. It is an instance of the Collecting Parameter pattern. mas01mj@732: * The test framework distinguishes between failures and errors. mas01mj@732: * A failure is anticipated and checked for with assertions. Errors are mas01mj@732: * unanticipated problems like an ArrayIndexOutOfBoundsException. mas01mj@732: * mas01mj@732: * @see Test mas01mj@732: */ mas01mj@732: public class TestResult implements TestListener { mas01mj@732: protected var fFailures:Array; mas01mj@732: protected var fErrors:Array; mas01mj@732: protected var fListeners:Array; mas01mj@732: protected var fRunTests:int; mas01mj@732: private var fStop:Boolean; mas01mj@732: mas01mj@732: public function TestResult() { mas01mj@732: fFailures = new Array(); mas01mj@732: fErrors = new Array(); mas01mj@732: fListeners = new Array(); mas01mj@732: fRunTests = 0; mas01mj@732: fStop = false; mas01mj@732: } mas01mj@732: /** mas01mj@732: * Adds an error to the list of errors. The passed in exception mas01mj@732: * caused the error. mas01mj@732: */ mas01mj@732: public function addError(test:Test, t:Error):void { mas01mj@732: fErrors.push(new TestFailure(test, t)); mas01mj@732: var len:uint = fListeners.length; mas01mj@732: var item:TestListener; mas01mj@732: for(var i:uint; i < len; i++) { mas01mj@732: item = TestListener(fListeners[i]); mas01mj@732: item.addError(test, t); mas01mj@732: } mas01mj@732: } mas01mj@732: /** mas01mj@732: * Adds a failure to the list of failures. The passed in exception mas01mj@732: * caused the failure. mas01mj@732: */ mas01mj@732: public function addFailure(test:Test, t:AssertionFailedError):void { mas01mj@732: fFailures.push(new TestFailure(test, t)); mas01mj@732: var len:uint = fListeners.length; mas01mj@732: var item:TestListener; mas01mj@732: for(var i:uint; i < len; i++) { mas01mj@732: item = TestListener(fListeners[i]); mas01mj@732: item.addFailure(test, t); mas01mj@732: } mas01mj@732: } mas01mj@732: /** mas01mj@732: * Registers a TestListener mas01mj@732: */ mas01mj@732: public function addListener(listener:TestListener):void { mas01mj@732: fListeners.push(listener); mas01mj@732: } mas01mj@732: /** mas01mj@732: * Unregisters a TestListener mas01mj@732: */ mas01mj@732: public function removeListener(listener:TestListener):void { mas01mj@732: var len:uint = fListeners.length; mas01mj@732: for(var i:uint; i < len; i++) { mas01mj@732: if(fListeners[i] == listener) { mas01mj@732: fListeners.splice(i, 1); mas01mj@732: return; mas01mj@732: } mas01mj@732: } mas01mj@732: throw new InstanceNotFoundError("removeListener called without listener in list"); mas01mj@732: } mas01mj@732: /** mas01mj@732: * Gets the number of detected errors. mas01mj@732: */ mas01mj@732: public function errorCount():int { mas01mj@732: return fErrors.length; mas01mj@732: } mas01mj@732: /** mas01mj@732: * Returns an Enumeration for the errors mas01mj@732: */ mas01mj@732: public function errors():Array { mas01mj@732: return fErrors; mas01mj@732: } mas01mj@732: /** mas01mj@732: * Gets the number of detected failures. mas01mj@732: */ mas01mj@732: public function failureCount():int { mas01mj@732: return fFailures.length; mas01mj@732: } mas01mj@732: /** mas01mj@732: * Returns an Enumeration for the failures mas01mj@732: */ mas01mj@732: public function failures():Array { mas01mj@732: return fFailures; mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Runs a TestCase. mas01mj@732: */ mas01mj@732: public function run(test:Test):void { mas01mj@732: startTest(test); mas01mj@732: test.runBare(); mas01mj@732: } mas01mj@732: /** mas01mj@732: * Gets the number of run tests. mas01mj@732: */ mas01mj@732: public function runCount():int { mas01mj@732: return fRunTests; mas01mj@732: } mas01mj@732: /** mas01mj@732: * Checks whether the test run should stop mas01mj@732: */ mas01mj@732: public function shouldStop():Boolean { mas01mj@732: return fStop; mas01mj@732: } mas01mj@732: /** mas01mj@732: * Informs the result that a test will be started. mas01mj@732: */ mas01mj@732: public function startTest(test:Test):void { mas01mj@732: var count:int = test.countTestCases(); mas01mj@732: fRunTests += count; mas01mj@732: mas01mj@732: var len:uint = fListeners.length; mas01mj@732: var item:TestListener; mas01mj@732: for(var i:uint; i < len; i++) { mas01mj@732: item = TestListener(fListeners[i]); mas01mj@732: item.startTest(test); mas01mj@732: } mas01mj@732: } mas01mj@732: mas01mj@732: public function startTestMethod(test:Test, method:String):void { mas01mj@732: var len:uint = fListeners.length; mas01mj@732: var item:TestListener; mas01mj@732: for(var i:uint; i < len; i++) { mas01mj@732: item = TestListener(fListeners[i]); mas01mj@732: item.startTestMethod(test, method); mas01mj@732: } mas01mj@732: } mas01mj@732: mas01mj@732: public function endTestMethod(test:Test, method:String):void { mas01mj@732: var len:uint = fListeners.length; mas01mj@732: var item:TestListener; mas01mj@732: for(var i:uint; i < len; i++) { mas01mj@732: item = TestListener(fListeners[i]); mas01mj@732: item.endTestMethod(test, method); mas01mj@732: } mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Informs the result that a test was completed. mas01mj@732: */ mas01mj@732: public function endTest(test:Test):void { mas01mj@732: var len:uint = fListeners.length; mas01mj@732: var item:TestListener; mas01mj@732: for(var i:uint; i < len; i++) { mas01mj@732: item = TestListener(fListeners[i]); mas01mj@732: item.endTest(test); mas01mj@732: } mas01mj@732: } mas01mj@732: /** mas01mj@732: * Marks that the test run should stop. mas01mj@732: */ mas01mj@732: public function stop():void { mas01mj@732: fStop = true; mas01mj@732: } mas01mj@732: /** mas01mj@732: * Returns whether the entire test was successful or not. mas01mj@732: */ mas01mj@732: public function wasSuccessful():Boolean { mas01mj@732: return failureCount() == 0 && errorCount() == 0; mas01mj@732: } mas01mj@732: } mas01mj@732: }