|
From: <luk...@us...> - 2006-09-21 01:32:49
|
Revision: 115
http://svn.sourceforge.net/asunit/?rev=115&view=rev
Author: lukebayes
Date: 2006-09-20 18:32:40 -0700 (Wed, 20 Sep 2006)
Log Message:
-----------
Async test cases and suites seem to be working properly
Modified Paths:
--------------
trunk/framework/as3/AsUnitTestRunner.as
trunk/framework/as3/asunit/framework/Assert.as
trunk/framework/as3/asunit/framework/Test.as
trunk/framework/as3/asunit/framework/TestCase.as
trunk/framework/as3/asunit/framework/TestSuite.as
trunk/framework/as3/asunit/textui/TestRunner.as
Modified: trunk/framework/as3/AsUnitTestRunner.as
===================================================================
--- trunk/framework/as3/AsUnitTestRunner.as 2006-09-20 21:23:53 UTC (rev 114)
+++ trunk/framework/as3/AsUnitTestRunner.as 2006-09-21 01:32:40 UTC (rev 115)
@@ -1,10 +1,12 @@
package {
import asunit.textui.TestRunner;
+ import asunit.framework.TestCaseTest;
public class AsUnitTestRunner extends TestRunner {
public function AsUnitTestRunner() {
start(AllTests, null, TestRunner.SHOW_TRACE);
+// start(TestCaseTest, "testAsync");
}
}
}
Modified: trunk/framework/as3/asunit/framework/Assert.as
===================================================================
--- trunk/framework/as3/asunit/framework/Assert.as 2006-09-20 21:23:53 UTC (rev 114)
+++ trunk/framework/as3/asunit/framework/Assert.as 2006-09-21 01:32:40 UTC (rev 115)
@@ -1,14 +1,15 @@
package asunit.framework {
-
+ import asunit.errors.AssertionFailedError;
+
import flash.errors.IllegalOperationError;
- import asunit.errors.AssertionFailedError;
+ import flash.events.EventDispatcher;
import flash.utils.Proxy;
/**
* A set of assert methods. Messages are only displayed when an assert fails.
*/
- public class Assert {
+ public class Assert extends EventDispatcher {
/**
* Protect constructor since it is a static only class
*/
Modified: trunk/framework/as3/asunit/framework/Test.as
===================================================================
--- trunk/framework/as3/asunit/framework/Test.as 2006-09-20 21:23:53 UTC (rev 114)
+++ trunk/framework/as3/asunit/framework/Test.as 2006-09-21 01:32:40 UTC (rev 115)
@@ -1,7 +1,8 @@
package asunit.framework {
import flash.display.DisplayObjectContainer;
+ import flash.events.IEventDispatcher;
- public interface Test {
+ public interface Test extends IEventDispatcher {
function countTestCases():int;
function toString():String;
function setResult(result:TestResult):void;
Modified: trunk/framework/as3/asunit/framework/TestCase.as
===================================================================
--- trunk/framework/as3/asunit/framework/TestCase.as 2006-09-20 21:23:53 UTC (rev 114)
+++ trunk/framework/as3/asunit/framework/TestCase.as 2006-09-21 01:32:40 UTC (rev 115)
@@ -10,6 +10,7 @@
import flash.net.getClassByAlias;
import flash.utils.setTimeout;
import flash.utils.clearTimeout;
+ import flash.events.Event;
/**
* A test case defines the fixture to run multiple tests. To define a test case<br>
@@ -89,7 +90,7 @@
private var currentMethod:String;
private var runSingle:Boolean;
private var methodIterator:Iterator;
- private var methodIsAsynchronous:Boolean;
+ protected var methodIsAsynchronous:Boolean;
/**
* Constructs a test case with the given name.
@@ -99,12 +100,14 @@
var className:Object = description.@name;
var methods:XMLList = description..method.(@name.match("^test"));
if(testMethod != null) {
- runSingle = true;
testMethods = testMethod.split(", ").join(",").split(",");
} else {
setTestMethods(methods);
}
- setName(className.toString());
+ if(testMethods.length == 1) {
+ runSingle = true;
+ }
+ setName(className.toString());
}
/**
@@ -152,8 +155,7 @@
* @see TestResult
*/
public function run():void {
- var result:TestResult = getResult();
- result.run(this);
+ getResult().run(this);
}
public function setResult(result:TestResult):void {
@@ -181,12 +183,11 @@
}
}
finally {
- if(!runSingle) {
- if(!itr.hasNext() && !methodIsAsynchronous) {
- cleanUp();
- result.endTest(this);
- isComplete = true;
- }
+ if(!itr.hasNext() && !methodIsAsynchronous) {
+ cleanUp();
+ getResult().endTest(this);
+ isComplete = true;
+ dispatchEvent(new Event(Event.COMPLETE));
}
}
}
@@ -225,10 +226,10 @@
}
}
catch(e:AssertionFailedError) {
- result.addFailure(this, e);
+ getResult().addFailure(this, e);
}
catch(ioe:Error) {
- result.addError(this, ioe);
+ getResult().addError(this, ioe);
}
return true;
}
@@ -250,7 +251,7 @@
/**
* Returns a string representation of the test case
*/
- public function toString():String {
+ override public function toString():String {
return getName() + "." + getCurrentMethod() + "()";
}
/**
@@ -278,29 +279,32 @@
}
protected function addAsync(handler:Function, timeout:int=500):Function {
- methodIsAsynchronous = true;
+ this.methodIsAsynchronous = true;
var context:TestCase = this;
- asyncMethodTimeoutId = setTimeout(timeoutHandler, timeout);
+ this.asyncMethodTimeoutId = setTimeout(this.timeoutHandler, timeout, timeout);
return function(args:*):* {
+ clearTimeout(context.asyncMethodTimeoutId);
try {
handler.apply(context, arguments);
}
catch(e:AssertionFailedError) {
- context.result.addFailure(context, e);
+ context.getResult().addFailure(context, e);
}
catch(ioe:IllegalOperationError) {
- trace('foo'); // this trace needs to be here?! Why?!
- context.result.addError(context, ioe);
+ trace('illegaloperation'); // this trace needs to be here?! Why?!
+ // without that trace, the app throws an error!!!!
+ context.getResult().addError(context, ioe);
}
finally {
+ trace("ASYNC METHOD FINALLY BLOCK CALLED!");
context.methodIsAsynchronous = false;
context.runBare();
}
}
}
- protected function timeoutHandler():void {
- result.addError(this, new IllegalOperationError("TestCase.DEFAULT_TIMEOUT (" + DEFAULT_TIMEOUT + "ms) exceeded on an asynchronous test method."));
+ protected function timeoutHandler(timeout:Number):void {
+ result.addError(this, new IllegalOperationError("TestCase.timeout (" + timeout + "ms) exceeded on an asynchronous test method."));
methodIsAsynchronous = false;
runBare();
}
Modified: trunk/framework/as3/asunit/framework/TestSuite.as
===================================================================
--- trunk/framework/as3/asunit/framework/TestSuite.as 2006-09-20 21:23:53 UTC (rev 114)
+++ trunk/framework/as3/asunit/framework/TestSuite.as 2006-09-21 01:32:40 UTC (rev 115)
@@ -1,5 +1,8 @@
package asunit.framework {
import flash.display.DisplayObjectContainer;
+ import flash.events.Event;
+ import asunit.util.Iterator;
+ import asunit.util.ArrayIterator;
/**
* A <code>TestSuite</code> is a <code>Composite</code> of Tests.
@@ -15,6 +18,7 @@
*/
public class TestSuite extends TestCase implements Test {
private var fTests:Array = new Array();
+ private var testsCompleteCount:Number = 0;
public function TestSuite() {
super();
@@ -48,12 +52,22 @@
*/
public override function run():void {
var result:TestResult = getResult();
- for each(var test:TestCase in fTests) {
+ var test:Test;
+ var itr:Iterator = new ArrayIterator(fTests);
+ while(itr.hasNext()) {
+ test = Test(itr.next());
test.setResult(result);
+ test.addEventListener(Event.COMPLETE, testCompleteHandler);
test.run();
}
}
-
+
+ private function testCompleteHandler(event:Event):void {
+ if(++testsCompleteCount >= testCount()) {
+ dispatchEvent(new Event(Event.COMPLETE));
+ }
+ }
+
/**
* Returns the number of tests in this suite
*/
Modified: trunk/framework/as3/asunit/textui/TestRunner.as
===================================================================
--- trunk/framework/as3/asunit/textui/TestRunner.as 2006-09-20 21:23:53 UTC (rev 114)
+++ trunk/framework/as3/asunit/textui/TestRunner.as 2006-09-21 01:32:40 UTC (rev 115)
@@ -32,6 +32,8 @@
public static const EXCEPTION_EXIT:int = 2;
public static const SHOW_TRACE:Boolean = true;
protected var fPrinter:ResultPrinter;
+ protected var startTime:Number;
+ protected var result:TestResult;
public function TestRunner() {
configureListeners();
@@ -66,14 +68,14 @@
// fscommand("showmenu", "false");
try {
- var suite:Test;
+ var instance:Test;
if(testMethod != null) {
- suite = new testCase(testMethod);
+ instance = new testCase(testMethod);
}
else {
- suite = new testCase();
+ instance = new testCase();
}
- return doRun(suite, showTrace);
+ return doRun(instance, showTrace);
}
catch(e:Error) {
throw new Error("Could not create and run test suite: " + e.getStackTrace());
@@ -81,8 +83,8 @@
return null;
}
- public function doRun(suite:Test, showTrace:Boolean = false):TestResult {
- var result:TestResult = new TestResult();
+ public function doRun(test:Test, showTrace:Boolean = false):TestResult {
+ result = new TestResult();
if(fPrinter == null) {
setPrinter(new ResultPrinter(showTrace));
}
@@ -90,39 +92,18 @@
fPrinter.setShowTrace(showTrace);
}
result.addListener(getPrinter());
- var startTime:Number = getTimer();
- suite.setResult(result);
- suite.setContext(this);
- suite.run();
-
- // Wait for all tests to be completed before finishing
- // the output.
- // This is how we are going to support asynchronous
- // TestCases.
- var intervalObj:Object = new Object();
- intervalObj.startTime = startTime;
- intervalObj.result = result;
- intervalObj.suite = suite;
- intervalObj.runOnce = false;
- // If you have no asynchronous TestCases, this will complete
- // in one millisecond
- intervalObj.intervalId = setInterval(onTestCompleted, 1, intervalObj);
-
+ startTime = getTimer();
+ test.setResult(result);
+ test.setContext(this);
+ test.addEventListener(Event.COMPLETE, testCompleteHandler);
+ test.run();
return result;
}
- private function onTestCompleted(intervalObj:Object):void {
- if(intervalObj.suite.getIsComplete()) {
- var endTime:Number = getTimer();
- var runTime:Number = endTime - intervalObj.startTime;
- getPrinter().printResult(intervalObj.result, runTime);
- clearInterval(intervalObj.intervalId);
- } else if(!intervalObj.runOnce) {
- // If you have an asynchronous TestCase, poll at less frequent intervals
- clearInterval(intervalObj.intervalId);
- intervalObj.runOnce = true;
- intervalObj.intervalId = setInterval(onTestCompleted, 20, intervalObj);
- }
+ private function testCompleteHandler(event:Event):void {
+ var endTime:Number = getTimer();
+ var runTime:Number = endTime - startTime;
+ getPrinter().printResult(result, runTime);
}
public function setPrinter(printer:ResultPrinter):void {
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|