diff --git a/docs/shell.md b/docs/shell.md index 5c18424a..48673d8c 100644 --- a/docs/shell.md +++ b/docs/shell.md @@ -197,7 +197,7 @@ B.java # 把参数按行输出方便查看 或是 grep $ a2l **/*.sh lib/console-text-color-themes.sh -test-cases/parseOpts-test.sh +test-cases/parseOpts_test.sh test-cases/self-installer.sh ... ``` diff --git a/test-cases/my_unit_test_lib.sh b/test-cases/my_unit_test_lib.sh new file mode 100644 index 00000000..63f5fb8d --- /dev/null +++ b/test-cases/my_unit_test_lib.sh @@ -0,0 +1,100 @@ +#!/bin/bash +# unit test lib + +################################################# +# commons functions +################################################# + +# NOTE: $'foo' is the escape sequence syntax of bash +readonly __ut_ec=$'\033' # escape char +readonly __ut_eend=$'\033[0m' # escape end + +__ut_colorEcho() { + local color=$1 + shift + # if stdout is console, turn on color output. + [ -t 1 ] && echo "${__ut_ec}[1;${color}m$*$__ut_eend" || echo "$*" +} + +redEcho() { + __ut_colorEcho 31 "$@" +} + +greenEcho() { + __ut_colorEcho 32 "$@" +} + +yellowEcho() { + __ut_colorEcho 33 "$@" +} + +blueEcho() { + __ut_colorEcho 34 "$@" +} + +fail() { + redEcho "TEST FAIL: $*" + exit 1 +} + +die() { + redEcho "Error: $*" 1>&2 + exit 1 +} + +################################################# +# assertion functions +################################################# + +assertArrayEquals() { + (($# == 2 || $# == 3)) || die "assertArrayEquals must 2 or 3 arguments!" + local failMsg="" + (($# == 3)) && { + failMsg=$1 + shift + } + + local a1PlaceHolder="$1[@]" + local a2PlaceHolder="$2[@]" + local a1=("${!a1PlaceHolder}") + local a2=("${!a2PlaceHolder}") + + [ ${#a1[@]} -eq ${#a2[@]} ] || fail "assertArrayEquals array length [${#a1[@]}] != [${#a2[@]}]${failMsg:+: $failMsg}" + + local i + for ((i = 0; i < ${#a1[@]}; i++)); do + [ "${a1[$i]}" = "${a2[$i]}" ] || fail "assertArrayEquals fail element $i: [${a1[$i]}] != [${a2[$i]}]${failMsg:+: $failMsg}" + done +} + +assertEquals() { + (($# == 2 || $# == 3)) || die "assertEqual must 2 or 3 arguments!" + local failMsg="" + (($# == 3)) && { + failMsg=$1 + shift + } + [ "$1" = "$2" ] || fail "assertEqual fail [$1] != [$2]${failMsg:+: $failMsg}" +} + +assertAllVarsSame() { + local test_afterVars + test_afterVars=$(declare) + + diff \ + <(echo "$test_beforeVars" | grep -v '^BASH_\|^_=') \ + <(echo "$test_afterVars" | grep -v '^BASH_\|^_=\|^FUNCNAME=\|^test_') || + fail "assertAllVarsSame: Unexpected extra global vars!" +} + +assertAllVarsExcludeOptVarsSame() { + local test_afterVars + test_afterVars=$(declare) + + diff \ + <(echo "$test_beforeVars" | grep -v '^BASH_\|^_=') \ + <(echo "$test_afterVars" | grep -v '^BASH_\|^_=\|^FUNCNAME=\|^test_\|^_OPT_\|^_opts_index_name_') || + fail "assertAllVarsExcludeOptVarsSame: Unexpected extra global vars!" +} + +test_beforeVars=$(declare) diff --git a/test-cases/parseOpts-test.sh b/test-cases/parseOpts_test.sh similarity index 62% rename from test-cases/parseOpts-test.sh rename to test-cases/parseOpts_test.sh index aca6bdfb..d3aff579 100755 --- a/test-cases/parseOpts-test.sh +++ b/test-cases/parseOpts_test.sh @@ -1,76 +1,15 @@ #!/bin/bash -BASE=`dirname $0` +BASE="$(dirname "${BASH_SOURCE[0]}")" -. $BASE/../lib/parseOpts.sh +source "$BASE/../lib/parseOpts.sh" - -################################################# -# Util Functions -################################################# - -# NOTE: $'foo' is the escape sequence syntax of bash -readonly ec=$'\033' # escape char -readonly eend=$'\033[0m' # escape end - -colorEcho() { - local color=$1 - shift - # if stdout is console, turn on color output. - [ -t 1 ] && echo "${ec}[1;${color}m$*$eend" || echo "$*" -} - -redEcho() { - colorEcho 31 "$@" -} - -greenEcho() { - colorEcho 32 "$@" -} - -yellowEcho() { - colorEcho 33 "$@" -} - -blueEcho() { - colorEcho 34 "$@" -} - -arrayEquals() { - local a1PlaceHolder="$1[@]" - local a2PlaceHolder="$2[@]" - local a1=("${!a1PlaceHolder}") - local a2=("${!a2PlaceHolder}") - - [ ${#a1[@]} -eq ${#a2[@]} ] || return 1 - - local i - for((i=0; i<${#a1[@]}; i++)); do - [ "${a1[$i]}" = "${a2[$i]}" ] || return 1 - done -} - -compareAllVars() { - local test_afterVars=`declare` - diff <(echo "$test_beforeVars" | grep -v '^BASH_\|^_=') <(echo "$test_afterVars" | grep -v '^BASH_\|^_=\|^FUNCNAME=\|^test_') -} - -compareAllVarsExcludeOptVars() { - local test_afterVars=`declare` - diff <(echo "$test_beforeVars" | grep -v '^BASH_\|^_=') <(echo "$test_afterVars" | grep -v '^BASH_\|^_=\|^FUNCNAME=\|^test_\|^_OPT_\|^_opts_index_name_') -} - -fail() { - redEcho "TEST FAIL: $*" - exit 1 -} +source "$BASE/my_unit_test_lib.sh" ################################################# # Test ################################################# -test_beforeVars=`declare` - # ======================================== blueEcho "Test case: success parse" # ======================================== @@ -82,18 +21,25 @@ _opts_showOptValueInfoList [ $test_exitCode -eq 0 ] || fail "Wrong exit code!" [ ${#_OPT_INFO_LIST_INDEX[@]} -eq 4 ] || fail "Wrong _OPT_INFO_LIST_INDEX!" -[ $_OPT_VALUE_a = "true" ] && [ $_OPT_VALUE_a_long = "true" ] || fail "Wrong option value of a!" + +[ $_OPT_VALUE_a = "true" ] && [ $_OPT_VALUE_a_long = "true" ] || fail "Wrong option value of a!" [ $_OPT_VALUE_b = "bb" ] && [ $_OPT_VALUE_b_long = "bb" ] || fail "Wrong option value of b!" + test_cArray=(c.sh -p pv -q qv cc) -arrayEquals test_cArray _OPT_VALUE_c && arrayEquals test_cArray _OPT_VALUE_c_long || fail "Wrong option value of c!" -test_dArray=(d.sh -x xv d1 d2 d3 ) -arrayEquals test_dArray _OPT_VALUE_d && arrayEquals test_dArray _OPT_VALUE_d_long || fail "Wrong option value of d!" +assertArrayEquals "Wrong option value of c!" test_cArray _OPT_VALUE_c +assertArrayEquals "Wrong option value of c!" test_cArray _OPT_VALUE_c_long + +test_dArray=(d.sh -x xv d1 d2 d3) +assertArrayEquals "Wrong option value of d!" test_dArray _OPT_VALUE_d +assertArrayEquals "Wrong option value of d!" test_dArray _OPT_VALUE_d_long + test_argArray=(aa bb cc dd ee) -arrayEquals test_argArray _OPT_ARGS || fail "Wrong args!" +assertArrayEquals "Wrong args!" test_argArray _OPT_ARGS + +assertAllVarsExcludeOptVarsSame -compareAllVarsExcludeOptVars || fail "Unpected extra glable vars!" _opts_cleanOptValueInfoList -compareAllVars || fail "Unpected extra glable vars!" +assertAllVarsSame # ======================================== blueEcho "Test case: success parse with -- " @@ -106,18 +52,23 @@ _opts_showOptValueInfoList [ $test_exitCode -eq 0 ] || fail "Wrong exit code!" [ ${#_OPT_INFO_LIST_INDEX[@]} -eq 4 ] || fail "Wrong _OPT_INFO_LIST_INDEX!" -[ $_OPT_VALUE_a = "true" ] && [ $_OPT_VALUE_a_long = "true" ] || fail "Wrong option value of a!" + +[ $_OPT_VALUE_a = "true" ] && [ $_OPT_VALUE_a_long = "true" ] || fail "Wrong option value of a!" [ $_OPT_VALUE_b = "bb" ] && [ $_OPT_VALUE_b_long = "bb" ] || fail "Wrong option value of b!" + test_cArray=(c.sh -p pv -q qv cc) -arrayEquals test_cArray _OPT_VALUE_c && arrayEquals test_cArray _OPT_VALUE_c_long || fail "Wrong option value of c!" +assertArrayEquals "Wrong option value of c!" test_cArray _OPT_VALUE_c +assertArrayEquals "Wrong option value of c!" test_cArray _OPT_VALUE_c_long + [ "$_OPT_VALUE_d" = "" ] && [ "$_OPT_VALUE_d_long" = "" ] || fail "Wrong option value of d!" + test_argArray=(aa bb --d-long d.sh -x xv d1 d2 d3 \; cc dd ee) -arrayEquals test_argArray _OPT_ARGS || fail "Wrong args!" +assertArrayEquals "Wrong args!" test_argArray _OPT_ARGS -compareAllVarsExcludeOptVars || fail "Unpected extra glable vars!" -_opts_cleanOptValueInfoList -compareAllVars || fail "Unpected extra glable vars!" +assertAllVarsExcludeOptVarsSame +_opts_cleanOptValueInfoList +assertAllVarsSame # ======================================== blueEcho "Test case: illegal option x" @@ -130,14 +81,12 @@ _opts_showOptValueInfoList [ $test_exitCode -eq 232 ] || fail "Wrong exit code!" [ ${#_OPT_INFO_LIST_INDEX[@]} -eq 0 ] || fail "Wrong _OPT_INFO_LIST_INDEX!" -[ "$_OPT_VALUE_a" = "" ] && [ "$_OPT_VALUE_a_long" = "" ] || fail "Wrong option value of a!" +[ "$_OPT_VALUE_a" = "" ] && [ "$_OPT_VALUE_a_long" = "" ] || fail "Wrong option value of a!" [ "$_OPT_VALUE_b" = "" ] && [ "$_OPT_VALUE_b_long" = "" ] || fail "Wrong option value of b!" [ "$_OPT_VALUE_c" = "" ] && [ "$_OPT_VALUE_c_long" = "" ] || fail "Wrong option value of c!" [ "$_OPT_VALUE_d" = "" ] && [ "$_OPT_VALUE_d_long" = "" ] || fail "Wrong option value of d!" [ "$_OPT_ARGS" = "" ] || fail "Wrong args!" - - # ======================================== blueEcho "Test case: empty options" # ======================================== @@ -149,14 +98,12 @@ _opts_showOptValueInfoList [ $test_exitCode -eq 0 ] || fail "Wrong exit code!" [ ${#_OPT_INFO_LIST_INDEX[@]} -eq 4 ] || fail "Wrong _OPT_INFO_LIST_INDEX!" -[ "$_OPT_VALUE_a" = "" ] && [ "$_OPT_VALUE_a_long" = "" ] || fail "Wrong option value of a!" +[ "$_OPT_VALUE_a" = "" ] && [ "$_OPT_VALUE_a_long" = "" ] || fail "Wrong option value of a!" [ "$_OPT_VALUE_b" = "" ] && [ "$_OPT_VALUE_b_long" = "" ] || fail "Wrong option value of b!" [ "$_OPT_VALUE_c" = "" ] && [ "$_OPT_VALUE_c_long" = "" ] || fail "Wrong option value of c!" [ "$_OPT_VALUE_d" = "" ] && [ "$_OPT_VALUE_d_long" = "" ] || fail "Wrong option value of d!" [ "$_OPT_ARGS" = "" ] || fail "Wrong args!" - - # ======================================== blueEcho "Test case: illegal option name" # ======================================== @@ -168,13 +115,12 @@ _opts_showOptValueInfoList [ $test_exitCode -eq 221 ] || fail "Wrong exit code!" [ ${#_OPT_INFO_LIST_INDEX[@]} -eq 0 ] || fail "Wrong _OPT_INFO_LIST_INDEX!" -[ "$_OPT_VALUE_a" = "" ] && [ "$_OPT_VALUE_a_long" = "" ] || fail "Wrong option value of a!" +[ "$_OPT_VALUE_a" = "" ] && [ "$_OPT_VALUE_a_long" = "" ] || fail "Wrong option value of a!" [ "$_OPT_VALUE_b" = "" ] && [ "$_OPT_VALUE_b_long" = "" ] || fail "Wrong option value of b!" [ "$_OPT_VALUE_c" = "" ] && [ "$_OPT_VALUE_c_long" = "" ] || fail "Wrong option value of c!" [ "$_OPT_VALUE_d" = "" ] && [ "$_OPT_VALUE_d_long" = "" ] || fail "Wrong option value of d!" [ "$_OPT_ARGS" = "" ] || fail "Wrong args!" - parseOpts "a,a-long|b,b-long:|c,c-long+|d,d-long+|z,z-#long" aa -a -b bb -x -c c.sh -p pv -q qv cc \; bb -d d.sh -x xv d1 d2 d3 \; cc -- dd ee test_exitCode=$? _opts_showOptDescInfoList @@ -182,12 +128,12 @@ _opts_showOptValueInfoList [ $test_exitCode -eq 222 ] || fail "Wrong exit code!" [ ${#_OPT_INFO_LIST_INDEX[@]} -eq 0 ] || fail "Wrong _OPT_INFO_LIST_INDEX!" -[ "$_OPT_VALUE_a" = "" ] && [ "$_OPT_VALUE_a_long" = "" ] || fail "Wrong option value of a!" +[ "$_OPT_VALUE_a" = "" ] && [ "$_OPT_VALUE_a_long" = "" ] || fail "Wrong option value of a!" [ "$_OPT_VALUE_b" = "" ] && [ "$_OPT_VALUE_b_long" = "" ] || fail "Wrong option value of b!" [ "$_OPT_VALUE_c" = "" ] && [ "$_OPT_VALUE_c_long" = "" ] || fail "Wrong option value of c!" [ "$_OPT_VALUE_d" = "" ] && [ "$_OPT_VALUE_d_long" = "" ] || fail "Wrong option value of d!" [ "$_OPT_ARGS" = "" ] || fail "Wrong args!" -compareAllVars || fail "Unpected extra glable vars!" +assertAllVarsSame greenEcho "TEST SUCCESS!!!"