Skip to content

Instantly share code, notes, and snippets.

@adamcameron
Created September 25, 2021 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamcameron/feb71879d1d8903968f7a7f1f6a1e33d to your computer and use it in GitHub Desktop.
Save adamcameron/feb71879d1d8903968f7a7f1f6a1e33d to your computer and use it in GitHub Desktop.
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