Last active
September 25, 2021 18:27
-
-
Save adamcameron/2a5a52165f9068e38e9f2229b2f6b57e to your computer and use it in GitHub Desktop.
A tiny test framework for when I'm testing code in trycf.com
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 runTest(required string label, required function testCase) { | |
try { | |
writeOutput("#label#: ") | |
testCase() | |
writeOutput("OK") | |
} catch("AssertionException" e) { | |
writeOutput("Failure: #e.message#") | |
} catch(any e) { | |
writeOutput("Error: #e.message#") | |
}finally{ | |
writeOutput("<hr>") | |
} | |
} | |
function assertEqual(required any expected, required any actual) { | |
try { | |
var result = expected == actual | |
} catch(any e) { | |
var result = false | |
} | |
if (!result) { | |
throw(type="AssertionException", message="expected: [#serializeJson(expected)#]; actual: [#serializeJson(actual)#]") | |
} | |
} | |
function assertNull(any actual) { | |
if (arguments.keyExists("actual") || !isNull(actual)) { | |
throw(type="AssertionException", message="expected: [null]; actual: [#serializeJson(actual)#]") | |
} | |
} | |
function assertTrue(any actual) { | |
assertEqual(true, isBoolean(actual)) | |
assertEqual(true, !!actual) | |
} | |
function assertCallback(required any actual, required function callback) { | |
callback(actual) | |
} | |
function assertException(required function callback) { | |
var result = false | |
try { | |
callback() | |
} catch(any e) { | |
result = true | |
} | |
if (!result) { | |
throw( | |
type="AssertionException", | |
message="expected: [an exception to be thrown]; actual: [no exception was thrown]" | |
); | |
} | |
} | |
</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
<cfscript> | |
runTest("assertEqual passes for two equal values", () => { | |
assertEqual(1,1) | |
}) | |
runTest("assertEqual fails for two unequal values", () => { | |
assertEqual(1,2) | |
}) | |
runTest("assertEqual can handle non-comparable operands", () => { | |
assertEqual(1,[]) | |
}) | |
runTest("assertEqual will handle user exceptions gracefully", () => { | |
throw "oops"; | |
}) | |
runTest("assetEqual will handle system exceptions gracefully", () => { | |
1 / 0 | |
}) | |
runTest("assertNull fails for not-null values", () => { | |
assertNull("NOT NULL") | |
}) | |
runTest("assertNull passes for absent values", () => { | |
assertNull() | |
}) | |
runTest("assertNull passes for null values", () => { | |
var nuller = () => {} | |
assertNull(nuller()) | |
}) | |
runTest("assertTrue passes for true values", () => { | |
assertTrue(true) | |
assertTrue(1) | |
}) | |
runTest("assertTrue fails for false values", () => { | |
assertTrue(false) | |
assertTrue(0) | |
}) | |
runTest("assertCallback passes when no assertion fails", () => { | |
assertCallback("match this one", (actual) => { | |
assertTrue(listContains("not this one,or this one, match this one", actual)) | |
}) | |
}) | |
runTest("assertCallback gracefully handles exceptions", () => { | |
assertCallback("not tested", (actual) => { | |
throw "oops"; | |
}) | |
}) | |
runTest("assertException passes when there is an exception", () => { | |
assertException(() => { | |
throw "an exception"; | |
}) | |
}) | |
runTest("assertException fails when there is no exception", () => { | |
assertException(() => { | |
return "all good" | |
}) | |
}) | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment