Last active
August 29, 2015 14:20
-
-
Save jamiejackson/38e06abf6e3d8bc20d04 to your computer and use it in GitHub Desktop.
Attempting to Create a Test Decorator for Running Dynamically-Generated Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @mxunit:decorators DynamicFunctionsTestDecorator | |
**/ | |
component extends="mxunit.framework.TestCase" { | |
init(); | |
private function init() { | |
dynamicFunctions = {}; | |
for (var i=1; i <= 10; i++) { | |
var functionName = "fooTest_#i#"; | |
dynamicFunctions[functionName] = { | |
input = i, | |
expectedResult = i+10 | |
}; | |
} | |
for (var i=1; i <= 10; i++) { | |
var functionName = "barTest_#i#"; | |
dynamicFunctions[functionName] = { | |
input = i, | |
expectedResult = i-5 | |
}; | |
} | |
} | |
private function fooTest( | |
required numeric input, | |
required numeric expectedResult | |
) { | |
var actualResult = input + 10; | |
assert( | |
actualResult == expectedResult, | |
"Expected #expectedResult#, but got #actualResult#" | |
); | |
} | |
private function barTest( | |
required numeric input, | |
required numeric expectedResult | |
) { | |
var actualResult = input + 5; | |
assert( | |
actualResult == expectedResult, | |
"Expected #expectedResult#, but got #actualResult#" | |
); | |
} | |
array function getDynamicTestFunctionNames() { | |
// writeDump(var=listToArray(structKeyList(dynamicFunctions)), abort=true); | |
return listToArray(structKeyList(dynamicFunctions)); | |
} | |
function onMissingMethod( | |
required string missingMethodName, | |
required struct missingMethodArguments | |
) { | |
if ( reFindNoCase("test\d*", missingMethodName) ) { | |
fooTest( | |
input = | |
dynamicFunctions[missingMethodName].input, | |
expectedResult = | |
dynamicFunctions[missingMethodName].expectedResult | |
); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfcomponent extends="mxunit.framework.TestDecorator" output="false" hint="Orders tests alphabetically"> | |
<cffunction name="getRunnableMethods" output="false" access="public" returntype="any" hint=""> | |
<cfset var methods = getTarget().getRunnableMethods() /> | |
<!--- remove the methods that are unique to this test case type ---> | |
<cfset arrayDelete(methods, "getDynamicTestFunctionNames") /> | |
<cfset arrayDelete(methods, "onMissingMethod") /> | |
<!--- add the dynamic functions ---> | |
<cfset methods.addAll( getTarget().getDynamicTestFunctionNames() ) /> | |
<cfset arraySort(methods, "text", "asc" )> | |
<cfreturn methods> | |
</cffunction> | |
<cffunction name="getAnnotation" access="public" returntype="Any" hint="Returns the value for an annotation, allowing for an mxunit namespace or not"> | |
<cfargument name="methodName" type="Any" required="false" hint="The name of the test method. An empty string means a testCase annotation" default="" /> | |
<cfargument name="annotationName" type="Any" required="true" hint="The name of the annotation" /> | |
<cfargument name="defaultValue" type="Any" required="false" default="" hint="The value to return if no annotation is found" /> | |
<!--- | |
<cftry> | |
<cfreturn super.getAnnotation(argumentCollection=arguments) /> | |
<cfcatch> | |
<cfreturn arguments.defaultValue /> | |
</cfcatch> | |
</cftry> | |
---> | |
<cfset var returnVal = arguments.defaultValue /> | |
<cfset var metadataTarget = this /> | |
<cfset var metadata = "" /> | |
<cfif len(arguments.methodName) gt 0> | |
<cfif not structKeyExists(this,arguments.methodName)> | |
<!--- JAJ Hack! don't throw exception, otherwise i get | |
"An annotation of expectedException was requested for the ... method, which does not exist." | |
<cfthrow type="mxunit.exception.methodNotFound" | |
message="An annotation of #arguments.annotationName# was requested for the #arguments.methodName# method, which does not exist." | |
detail="Check the name of the method." /> | |
---> | |
<cfelse> | |
<cfset metadataTarget = this[arguments.methodName] /> | |
</cfif> | |
</cfif> | |
<cfset metadata = getMetadata(metadataTarget) /> | |
<!--- go lookup up inheritence tree, and move the metadata if neccessary ---> | |
<cfscript> | |
if(NOT Len(arguments.methodName)) | |
{ | |
while(StructKeyExists(metadata, "extends")) | |
{ | |
if(StructKeyExists(metadata,"mxunit:" & arguments.annotationName) OR | |
StructKeyExists(metadata,arguments.annotationName) | |
) | |
{ | |
break; | |
} | |
metadata = metadata.extends; | |
} | |
} | |
</cfscript> | |
<cfif StructKeyExists(metadata,"mxunit:" & arguments.annotationName)> | |
<cfset returnVal = metadata["mxunit:" & arguments.annotationName] /> | |
<cfelseif StructKeyExists(metadata,arguments.annotationName)> | |
<cfset returnVal = metadata[arguments.annotationName] /> | |
</cfif> | |
<cfreturn returnVal /> | |
</cffunction> | |
</cfcomponent> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment