ReachableCEO-Profile-FullTi.../tests/function-args-read
Charles N Wyble fda3d3ea40 Squashed 'vendor/git.knownelement.com/ExternalVendorCode/mo/' content from commit 7e86c1a
git-subtree-dir: vendor/git.knownelement.com/ExternalVendorCode/mo
git-subtree-split: 7e86c1a5f525f352983077d743c2ce2f5d75f4fa
2024-12-10 17:22:52 -06:00

43 lines
1.0 KiB
Bash
Executable File

#!/usr/bin/env bash
cd "${0%/*}" || exit 1
. ../run-tests
testArgs() {
local args
# shellcheck disable=SC2031
args=$(declare -p MO_FUNCTION_ARGS)
# The output from declare -p could look like these
# declare -a MO_FUNCTION_ARGS=([0]="one")
# declare -ax MO_FUNCTION_ARGS='([0]="one")'
# Trim leading declare statement and variable name
args="${args#*=}"
# If there are any quotes, remove them. The function arguments will always
# be an array.
if [[ "${args:0:1}" == "'" ]]; then
args=${args#\'}
args=${args%\'}
fi
echo -n "$args"
}
template() {
cat <<EOF
No args: {{testArgs}} - done
One arg: {{testArgs 'one'}} - done
Multiple arguments: {{testArgs 'aa' 'bb' 'cc' 'x' "" '!' '{[_.|' }} - done
Evil: {{testArgs bla; cat /etc/issue}} - done
EOF
}
expected() {
cat <<EOF
No args: () - done
One arg: ([0]="one") - done
Multiple arguments: ([0]="aa" [1]="bb" [2]="cc" [3]="x" [4]="" [5]="!" [6]="{[_.|") - done
Evil: ([0]="" [1]="" [2]="") - done
EOF
}
runTest