Custom Assertions

Version 4 by bill shelton
on Jan 15, 2010 16:29.

compared with
Current by bill shelton
on Dec 02, 2010 11:59.

(show comment)
Key
This line was removed.
This word was removed. This word was added.
This line was added.

Changes (30)

View page history
The code for this might look like:
{code}
<cffunctionname="testValidEmailAddress">
<cffunctionname="testValidEmailAddress">
<cfscript>
varemailRegEx="^\[A-Za-z0-9\]((\[_\.\-\]?\[a-zA-Z0-9\]+)*)@(\[A-Za-z0-9\]+)((\[\.\-\]?\[a-zA-Z0-9\]+)*)\.(\[A-Za-z\]{2,})$";
assertTrue(refind(emailRegEx,emailAddress)lt1,"Invalidemailaddressformat");
</cfscript>
</cffunction><cffunction name="testValidEmailAddress">
<cfscript>
var emailRegEx = "^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$";
var emailAddress = "[email protected]";
assertTrue(refind(emailRegEx,emailAddress) lt 1,"Invalid email address format");
</cfscript>
</cffunction>
</cffunction>
{code}
This is fine, but you could save yourself some typing and some Ctrl+C/Ctrl+V errors. How about something that looks like this instead?

{code}
<cffunctionname="testValidEmailAddress">
<cffunctionname="testValidEmailAddress">
<cfscript>
varemailAddress="[email protected]";
assertIsValidEmail(emailAddress);
</cfscript>
</cffunction><cffunction name="testValidEmailAddress">
<cfscript>
var emailAddress = "[email protected]";
assertIsValidEmail(emailAddress);
</cfscript>
</cffunction>
</cffunction>
{code}
{code}
<cfscript>
vargoodEmailAddress="[email protected]";
varbadEmailAddress="so~~/\/\/\/\/\[email protected]";
addAssertDecorator("mxunit.framework.ext.ValidEmailAssertion");
assertIsValidEmail(goodEmailAddress);
try{
//expectfailure
assertIsValidEmail(badEmailAddress);
}
catch(mxunit.exception.AssertionFailedErrore){}
</cfscript>
</cffunction>
 
</cfcomponent>
<cfcomponent name="ValidEmailAssertionTest" extends="mxunit.framework.TestCase" >
 
<cffunction name="testAssertIsValidEmail">
<cfscript>

{code}
<cfcomponentname="ValidEmailAssertion">
 
<cffunctionname="assertIsValidEmail"returntype="boolean">
<cfargumentname="email"type="string"/>
<cfargumentname="message"type="string"required="false"default="Emailaddressdoesnotappearvalid."/>
<cfcomponent name="ValidEmailAssertion" >
 
<cfscript>
arguments=normalizeArguments(arguments);
emailRegEx="^\[A-Za-z0-9\]((\[_\.\-\]?\[a-zA-Z0-9\]+)*)@(\[A-Za-z0-9\]+)((\[\.\-\]?\[a-zA-Z0-9\]+)*)\.(\[A-Za-z\]{2,})$";
actual=refind(emailRegEx,arguments.email);
</cfscript>

<cfifactuallt1>
<cfthrowtype="mxunit.exception.AssertionFailedError"message="arguments.message"/>
</cfif>
<cfreturntrue/>
</cffunction>

</cfcomponent><cfcomponent name="ValidEmailAssertion" >
<cffunction name="assertIsValidEmail" returntype="boolean" >
<cfargument name="email" type="string" />

</cfcomponent>
 
{code}

{code}
<cfcomponentname="ValidEmailAssertion">
 
<cffunctionname="assertIsValidEmail"returntype="boolean">
<cfargumentname="email"type="string"/>
<cfargumentname="message"type="string"required="false"default="Emailaddressdoesnotappearvalid."/>
<cfcomponent name="ValidEmailAssertion" >
 
<cfscript>
arguments=normalizeArguments(arguments);
emailRegEx="^\[A-Za-z0-9\]((\[_\.\-\]?\[a-zA-Z0-9\]+)*)@(\[A-Za-z0-9\]+)((\[\.\-\]?\[a-zA-Z0-9\]+)*)\.(\[A-Za-z\]{2,})$";
actual=refind(emailRegEx,arguments.email);
assertTrue(refind(emailRegEx,arguments.email)lt1,arguments.message);
</cfscript>

<cfreturntrue/>
</cffunction>

</cfcomponent><cfcomponent name="ValidEmailAssertion" >
<cffunction name="assertIsValidEmail" returntype="boolean" >
<cfargument name="email" type="string" />
emailRegEx = "^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$";
actual = refind(emailRegEx,arguments.email);
//leverage existing MXUnit assertions
assertTrue(refind(emailRegEx,arguments.email) lt 1, arguments.message);
</cfscript>

{code}
<cfcomponentname="ValidEmailAssertionTest"extends="mxunit.framework.TestCase">
...
<cffunctionname="setUp">
<cfscript>
addAssertDecorator("mxunit.framework.ext.ValidEmailAssertion");
</cfscript>
</cffunction>
 
</cfcomponent><cfcomponent <cfcomponent name="ValidEmailAssertionTest" extends="mxunit.framework.TestCase" >
...
<cffunction name="setUp">

{code}
<?xmlversion="1.0"encoding="UTF-8"?>
<?xml version="1.0"encoding="UTF-8"?>
<mxunit-config>
...
<config-elementtype="assertionExtension"path="mxunit.framework.ext.ValidEmailAssertion"autoload="true"override="false"/>
<config-element type="assertionExtension" path="mxunit.framework.ext.ValidEmailAssertion" autoload="true" override="false"/>
...
</mxunit-config>
{code}

This tells MXUnit to automatically load the custom assertion mxunit.framework.ext.ValidEmailAssertion when it starts and makes assertValidEmail(...) method available to all tests. Note that you can put as many public methods in a custom assertion component as you wish.