Skip to content

Commit

Permalink
! improve/cleanup unit test of parseOpts
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Feb 19, 2021
1 parent 513a869 commit ccc256e
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 88 deletions.
2 changes: 1 addition & 1 deletion docs/shell.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
...
```
Expand Down
100 changes: 100 additions & 0 deletions test-cases/my_unit_test_lib.sh
Original file line number Diff line number Diff line change
@@ -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)
120 changes: 33 additions & 87 deletions test-cases/parseOpts-test.sh → test-cases/parseOpts_test.sh
Original file line number Diff line number Diff line change
@@ -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"
# ========================================
Expand All @@ -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 -- "
Expand All @@ -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"
Expand All @@ -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"
# ========================================
Expand All @@ -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"
# ========================================
Expand All @@ -168,26 +115,25 @@ _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
_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!!!"

0 comments on commit ccc256e

Please sign in to comment.