Skip to content

Instantly share code, notes, and snippets.

// test/unit/services/SecurityFilterService
describe("Tests for SecurityFilterService", () => {
describe("Tests for isAuthorised", () => {
it("will reject a user that is not authorised to access the resource", () => {
service = new SecurityFilterSerivce() // might need mocked dependencies
result = service.isAuthorised("juniorUser", "/email/approve-copy", "patch")
expect result.toBeFalse()
})
<cfscript>
// implementation
void function describe(required string label, required function testGroup) {
try {
writeOutput("#label#<br>")
testGroup()
} catch (any e) {
writeOutput("Error: #e.message#<br>")
}
}
@adamcameron
adamcameron / exampleTests.cfm
Created October 30, 2021 22:20
Example tests
<cfscript>
function run() {
describe("some tests", () => {
it("a passing test", () => {
expect(true).toBe(true)
})
it("a failing test", () => {
expect(true).toBe(false)
})
})
@adamcameron
adamcameron / tinyTestFramework.cfm
Last active November 6, 2023 09:13
A tiny testing framework to use with trycf.com
<style>
.tinyTest {background-color: black; color:white; font-family:monospace}
.tinyTest div {margin-left: 1em}
.tinyTest .pass {color:green;}
.tinyTest .fail {color:red;}
.tinyTest .error {background-color:red; color:black}
</style>
<cfscript>
tinyTest = {
<?php
function haystackContainsNeedle($haystack, $needle) {
$intersection = array_intersect_assoc($haystack, $needle);
return $intersection == $needle ? 'true' : 'false';
}
$haystack = [
"firstName" => "Adam",
"lastName" => "Cameron",
@adamcameron
adamcameron / results.out
Last active October 16, 2021 16:36
Some testing of Lucee's tag islands :-|
It returns true when the test function is defined as a function statement inline
Result: OK
It returns true when the test function is defined as a function expression inline
Result: OK
It returns true when the test function is defined as a tag island inline
Result: Expected: [true]; received: []
It returns true when the test function is defined as a tag island not inline
<cfscript>
function function createIdSequence(required string prefix){
var counters[prefix] = counters[prefix] ?: 1
var generators[prefix] = generators[prefix] ?: () => prefix & counters[prefix]++
return generators[prefix]
}
getNewSubscriptionID = createIdSequence("SUB")
getNewMandateID = createIdSequence("MND")
@adamcameron
adamcameron / test.cfm
Last active September 26, 2021 10:33
TDDing a micro test framework using itself to test itself
<cfscript>
runTest("it uses a function called runTest", ()=>{})
savecontent variable="testOutput" {
runTest("runTest requires and displays a passed-in message describing the test, followed by a line break", ()=>{})
}
writeOutput(testOutput)
if (testOutput != "runTest requires and displays a passed-in message describing the test, followed by a line break<br>") {
throw(type="AssertionException", message="runTest did not output the message and line break")
@adamcameron
adamcameron / test.cfm
Created September 25, 2021 18:56
Testing ?.
<cfscript>
runTest("default struct deferencing works the same as with dot operator", () => {
var st = {key="value"}
assertEqual(st.key, st?.key)
})
runTest("null left-hand operand does error with . operator", () => {
assertException(() => {
var result = st.key
})
@adamcameron
adamcameron / testFramework.cfm
Last active September 25, 2021 18:27
A tiny test framework for when I'm testing code in trycf.com
<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#")