mirror of
https://github.com/natelandau/shell-scripting-templates.git
synced 2025-11-12 15:03:47 -05:00
_printArray_: only print in verbose mode
This commit is contained in:
@@ -84,19 +84,31 @@ teardown() {
|
||||
|
||||
@test "_printArray_: Array" {
|
||||
testArray=(1 2 3)
|
||||
VERBOSE=true
|
||||
run _printArray_ "testArray"
|
||||
assert_success
|
||||
assert_line --index 1 "0 = 1"
|
||||
assert_line --index 2 "1 = 2"
|
||||
assert_line --index 3 "2 = 3"
|
||||
assert_line --index 1 "[ debug] 0 = 1"
|
||||
assert_line --index 2 "[ debug] 1 = 2"
|
||||
assert_line --index 3 "[ debug] 2 = 3"
|
||||
}
|
||||
|
||||
@test "_printArray_: don't print unless verbose mode" {
|
||||
testArray=(1 2 3)
|
||||
VERBOSE=false
|
||||
run _printArray_ "testArray"
|
||||
assert_success
|
||||
refute_output --partial "[ debug] 0 = 1"
|
||||
refute_output --partial "[ debug] 1 = 2"
|
||||
refute_output --partial "[ debug] 2 = 3"
|
||||
}
|
||||
|
||||
@test "_printArray_: Associative array" {
|
||||
declare -A assoc_array
|
||||
VERBOSE=true
|
||||
assoc_array=([foo]=bar [baz]=foobar)
|
||||
run _printArray_ "assoc_array"
|
||||
assert_success
|
||||
assert_line --index 1 "foo = bar"
|
||||
assert_line --index 2 "baz = foobar"
|
||||
assert_line --index 1 "[ debug] foo = bar"
|
||||
assert_line --index 2 "[ debug] baz = foobar"
|
||||
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ _printAnsi_() {
|
||||
|
||||
_printArray_() {
|
||||
# DESC:
|
||||
# Prints the content of array as key value pairs for easier debugging
|
||||
# Prints the content of array as key value pairs for easier debugging. Only prints in verbose mode.
|
||||
# ARGS:
|
||||
# $1 (Required) - String variable name of the array
|
||||
# OUTS:
|
||||
@@ -53,8 +53,10 @@ _printArray_() {
|
||||
|
||||
local _arrayName="${1}"
|
||||
declare -n _arr="${1}"
|
||||
printf "${underline}Printing contents of \${%s${reset}[@]}\n" "${_arrayName}"
|
||||
[[ ${VERBOSE} != true ]] && return 0
|
||||
debug "Printing contents of \${${_arrayName}[@]}"
|
||||
|
||||
for _k in "${!_arr[@]}"; do
|
||||
printf "%s = %s\n" "$_k" "${_arr[$_k]}"
|
||||
debug "${_k} = ${_arr[$_k]}"
|
||||
done
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user