Skip to content

Instantly share code, notes, and snippets.

@adamcameron
adamcameron / Adam.cfc
Created March 17, 2022 08:12
Code for request.testBox fail
component {
request.testBox.debug("in pseudo constructor")
static {
request.testBox.debug("in static constructor")
}
function init() {
request.testBox.debug("in constructor")
@adamcameron
adamcameron / lifecycle_functions_spec.rb
Created March 5, 2022 17:07
Demonstrates the order in which lifecycle event handlers run in rspec
describe "Lifecycle function tests" do
test_array = []
before(:all) do
test_array.push "main block before :all handler"
end
before(:each) do # or just before with no param
test_array.push "main block before :each handler"
end
<cfscript>
function run() {
describe("email address tests", () => {
tests = [
{label="local too long (fails on CF)", expectation=false, address="#repeatString('x', 65)#@example.com"},
{label="domain too long (fails on CF)", expectation=false, address="x@#repeatString('x', 255)#.com"},
{label="embedded % should be fine (fails on both)", expectation=false, address="first%second@example.com"},
{label="domain format, length and overall length max (both pass)", expectation=true, address="x@x23456789ABCDEF-x23456789ABCDEF-x23456789ABCDEF-x23456789ABCDEF.x23456789ABCDEF-x23456789ABCDEF-x23456789ABCDEF-x23456789ABCDEF.x23456789ABCDEF-x23456789ABCDEF-x23456789ABCDEF-x23456789ABCDEF.x23456789ABCDEF-x23456789ABCDEF-x23456789ABCDEF-x2345678.com"},
{label="domain label length bad (first one 64 chars; max is 63) (fails on both)", expectation=false, address="x@XX23456789ABCDEF-x23456789ABCDEF-x23456789ABCDEF-x23456789ABCDEF.x23456789ABCDEF-x23456789ABCD
<cfscript>
uuid = replace(createUuid(), "-", "", "ALL");
result64 = baseMToBaseN(uuid, "HEX", "BASE62");
result36 = baseMToBaseN(uuid, "HEX", "BASE36");
uuidAgain = baseMToBaseN(result64, "BASE62", "HEX");
writeDump([uuid, result64, result64.len(), uuidAgain, result36, result36.len()]);
@adamcameron
adamcameron / C.cfc
Created January 5, 2022 18:24
Demonstrating behaviour variation when getCurrentTemplatePath is called from file included in CFC vs CFM
component {
include "m.cfm";
}
// 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",