Skip to content

Instantly share code, notes, and snippets.

@adamcameron
adamcameron / C.cfc
Created Jan 5, 2022
Demonstrating behaviour variation when getCurrentTemplatePath is called from file included in CFC vs CFM
View C.cfc
component {
include "m.cfm";
}
View tests.code
// 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()
})
View testFrameworkAndItsTestsForBlog.cfm
<cfscript>
// implementation
void function describe(required string label, required function testGroup) {
try {
writeOutput("#label#<br>")
testGroup()
} catch (any e) {
writeOutput("Error: #e.message#<br>")
}
}
View exampleTests.cfm
<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
Created Oct 30, 2021
A tiny testing framework to use with trycf.com
View tinyTestFramework.cfm
<cfscript>
// implementation
void function describe(required string label, required function testGroup) {
try {
writeOutput("#label#<br>")
testGroup()
} catch (any e) {
writeOutput("Error: #e.message#<br>")
}
}
View haystackContainsNeedle.php
<?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 Oct 16, 2021
Some testing of Lucee's tag islands :-|
View results.out
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
View createIdSequence.cfm
<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 Sep 26, 2021
TDDing a micro test framework using itself to test itself
View test.cfm
<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")
View test.cfm
<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
})