diff --git a/test/debug.bats b/test/debug.bats index fcc069c..c223a9f 100755 --- a/test/debug.bats +++ b/test/debug.bats @@ -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" } diff --git a/utilities/debug.bash b/utilities/debug.bash index e2f6f14..64b45b7 100644 --- a/utilities/debug.bash +++ b/utilities/debug.bash @@ -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 }