package tests.com.ericfeminella
{
import flash.utils.describeType;
import flash.utils.getQualifiedClassName;
import flexunit.framework.TestSuite;
/**
*
* All static utility class which provides a mechanism for adding
* a <code>TestCase</code> to a <code>TestSuite</code> for each
* method defined by a specific class
*
* @example The following example demonstrates how a class which
* extends TestCase can utilize <code>TestSuiteBuilder</code> to
* automate the creation of the associated <code>TestSuite</code>
*
* <listing version="3.0">
*
* package
* {
* public class ExampleTest extends TestCase
* {
* public function testX() : void
* {
* // test implementation...
* }
*
* public function testY() : void
* {
* // test implementation...
* }
*
* public function testZ() : void
* {
* // test implementation...
* }
* }
* }
*
* // TestRunner.mxml on creationComplete Event
* private function onCreationComplete() : void
* {
* testRunner.test = TestSuiteHelper.createSuite( ExampleTest );
* testRunner.startTest();
* }
*
* // creates a new TestSuite and adds each method defined by ExampleTest
* // to the suite
*
* </listing>
*
* @see flexunit.framework.TestSuite
* @see flexunit.framework.TestCase
*
*/
public final class TestSuiteHelper
{
/**
*
* @private
*
* Defines the setUp test fixtures which is not to be included in
* the tests
*
*/
private static const SET_UP_FIXTURE:String = "setUp";
/**
*
* @private
*
* Defines the setUp test fixtures which is not to be included in
* the tests
*
*/
private static const TEAR_DOWN_FIXTURE:String = "tearDown";
/**
*
* Creates a <code>TestSuite</code> and, through introspection,
* determines all methods defined by a specific class which
* extends <code>TestCase</code> and adds each method to the
* <code>TestSuite</code>
*
* @param the class in which to locate all tests
* @return a <code>TestSuite</code> containing all of the tests
*
*/
public static function createSuite(Type:Class) : TestSuite
{
var tests:XMLList = getTests( Type );
var testSuite:TestSuite = new TestSuite();
for each (var methodName:String in tests.@name)
{
if ( methodName != SET_UP_FIXTURE && methodName != TEAR_DOWN_FIXTURE )
{
testSuite.addTest( new Type( methodName ) );
}
}
return testSuite;
}
/**
*
* Determines if the specified test has been defined for
* a specific class
*
* @param the class or type in which to locate tests
* @param the name of the test which is to be located
* @return true if the test exists, otherwise false
*
*/
public static function hasTest(Type:Class, testName:String) : Boolean
{
var result:Boolean = false;
var tests:XMLList = getTests( Type );
for each (var methodName:String in tests.@name)
{
if (methodName == testName)
{
result = true;
break;
}
}
return result;
}
/**
*
* @private
*
* Retrieves all tests which have been defined for a specific
* <code>TestCase</code> sub class
*
* @param the class or type in which to locate tests
* @return an <code>XMLList</code> containing all test names
*
*/
private static function getTests(Type:Class) : XMLList
{
var type:String = getQualifiedClassName( Type );
var definedMethods:XMLList = describeType(Type)..method.(@declaredBy == type).(@name != "suite");
return definedMethods;
}
}
}