Created
November 21, 2013 14:49
-
-
Save adamcameron/7582867 to your computer and use it in GitHub Desktop.
The initial state of my unit test code as at 2013-11-21
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
// Test.cfc | |
component extends="mxunit.framework.TestCase" { | |
public void function beforeTests(){ | |
addAssertDecorator("CustomAssertions"); | |
include "./function.cfm"; | |
} | |
public void function setup(){ | |
variables.eventObject = createEventObject(); | |
} | |
public void function testBaseline(){ | |
createEventObject(); | |
// it doesn't error. That's it | |
} | |
public void function testReturnValues(){ | |
assertIsStruct(variables.eventObject, "Returned value should be a struct"); | |
assertStructKeysCorrect( | |
"on,trigger", | |
variables.eventObject, | |
"Incorrect keys returned in eventObject" | |
); | |
assertTrue( | |
isClosure(variables.eventObject.on), | |
"The returned on() value should be a function" | |
); | |
assertTrue( | |
isClosure(variables.eventObject.trigger), | |
"The returned trigger() value should be a function" | |
); | |
} | |
public void function testOnRequiresEventArg() { | |
var failMsg = "on() should require an EVENT argument"; | |
try { | |
variables.eventObject.on(handler=function(){}); | |
fail(failMsg); | |
} catch (any e){ | |
// can't catch this coherently by exception type as CF and Railo return completely different exceptions here | |
assertTrue( | |
findNoCase("event", e.message) && findNoCase("parameter", e.message), | |
failMsg | |
); | |
} | |
} | |
public void function testOnRequiresHandlerArg() { | |
var failMsg = "on() should require an HANDLER argument"; | |
try { | |
variables.eventObject.on(event="TestEvent"); | |
fail(failMsg); | |
} catch (any e){ | |
assertTrue( | |
findNoCase("handler", e.message) && findNoCase("parameter", e.message), | |
failMsg | |
); | |
} | |
} | |
public void function testTriggerRequiresEventArg() { | |
var failMsg = "trigger() should require an EVENT argument"; | |
try { | |
variables.eventObject.trigger(); | |
fail(failMsg); | |
} catch (any e){ | |
assertTrue( | |
findNoCase("event", e.message) && findNoCase("parameter", e.message), | |
failMsg | |
); | |
} | |
} | |
} |
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
<cfscript> | |
// function.cfm | |
function createEventObject(){ | |
var eventContainer = {}; | |
return { | |
on = function(required string event, required function handler){}, | |
trigger = function(required string event){} | |
}; | |
}; | |
</cfscript> |
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
// Test.cfc | |
component extends="mxunit.framework.TestCase" { | |
public void function beforeTests(){ | |
addAssertDecorator("CustomAssertions"); | |
include "./function.cfm"; | |
} | |
public void function setup(){ | |
variables.eventObject = createEventObject(); | |
} | |
public void function testBaseline(){ | |
createEventObject(); | |
// it doesn't error. That's it | |
} | |
public void function testReturnValues(){ | |
assertIsStruct(variables.eventObject, "Returned value should be a struct"); | |
assertStructKeysCorrect( | |
"on,trigger", | |
variables.eventObject, | |
"Incorrect keys returned in eventObject" | |
); | |
assertTrue( | |
isClosure(variables.eventObject.on), | |
"The returned on() value should be a function" | |
); | |
assertTrue( | |
isClosure(variables.eventObject.trigger), | |
"The returned trigger() value should be a function" | |
); | |
} | |
public void function testOnRequiresEventArg() { | |
var failMsg = "on() should require an EVENT argument"; | |
try { | |
variables.eventObject.on(handler=function(){}); | |
fail(failMsg); | |
} catch (any e){ | |
// can't catch this coherently by exception type as CF and Railo return completely different exceptions here | |
assertTrue( | |
findNoCase("event", e.message) && findNoCase("parameter", e.message), | |
failMsg | |
); | |
} | |
} | |
public void function testOnRequiresHandlerArg() { | |
var failMsg = "on() should require an HANDLER argument"; | |
try { | |
variables.eventObject.on(event="TestEvent"); | |
fail(failMsg); | |
} catch (any e){ | |
assertTrue( | |
findNoCase("handler", e.message) && findNoCase("parameter", e.message), | |
failMsg | |
); | |
} | |
} | |
public void function testTriggerRequiresEventArg() { | |
var failMsg = "trigger() should require an EVENT argument"; | |
try { | |
variables.eventObject.trigger(); | |
fail(failMsg); | |
} catch (any e){ | |
assertTrue( | |
findNoCase("event", e.message) && findNoCase("parameter", e.message), | |
failMsg | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment