Created
September 25, 2021 18:56
-
-
Save adamcameron/feb71879d1d8903968f7a7f1f6a1e33d to your computer and use it in GitHub Desktop.
Testing ?.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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