Skip to content

Instantly share code, notes, and snippets.

@adamcameron
Created Sep 25, 2021
Embed
What would you like to do?
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
})
})
runTest("null left-hand operand does not error with ?. operator", () => {
assertNull(st?.key)
})
runTest("null right-hand operand does error with . operator", () => {
assertException(() => {
var st = {}
var result = st.key
})
})
runTest("null right-hand operand does not error with ?. operator", () => {
var st = {}
assertNull(st?.key)
})
runTest("subsequent keys referenced after a safely-missing key do not cause an exception", () => {
var st = {}
assertNull(st.doesNotExist?.norDoesThis)
})
runTest("?. only works on the highest level of missing elements", () => {
assertException(() => {
var st = {}
assertNull(st.does.not?.exist)
})
})
runTest("using ?. on a struct element that is not a struct throws an exception", () => {
assertException(() => {
var st = {exists="not a struct"}
var result = st.exists?.doesNotExist
})
})
runTest("using ?. on a variable that is not a struct throws an exception", () => {
assertException(() => {
var st = "not a struct"
var result = st.exists?.doesNotExist
})
})
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment