1
1
#! /bin/bash
2
2
3
- BASE=` dirname $0 `
3
+ BASE=" $( dirname " ${BASH_SOURCE[0]} " ) "
4
4
5
- . $BASE /../lib/parseOpts.sh
5
+ source " $BASE /../lib/parseOpts.sh"
6
6
7
-
8
- # ################################################
9
- # Util Functions
10
- # ################################################
11
-
12
- # NOTE: $'foo' is the escape sequence syntax of bash
13
- readonly ec=$' \033 ' # escape char
14
- readonly eend=$' \033 [0m' # escape end
15
-
16
- colorEcho () {
17
- local color=$1
18
- shift
19
- # if stdout is console, turn on color output.
20
- [ -t 1 ] && echo " ${ec} [1;${color} m$* $eend " || echo " $* "
21
- }
22
-
23
- redEcho () {
24
- colorEcho 31 " $@ "
25
- }
26
-
27
- greenEcho () {
28
- colorEcho 32 " $@ "
29
- }
30
-
31
- yellowEcho () {
32
- colorEcho 33 " $@ "
33
- }
34
-
35
- blueEcho () {
36
- colorEcho 34 " $@ "
37
- }
38
-
39
- arrayEquals () {
40
- local a1PlaceHolder=" $1 [@]"
41
- local a2PlaceHolder=" $2 [@]"
42
- local a1=(" ${! a1PlaceHolder} " )
43
- local a2=(" ${! a2PlaceHolder} " )
44
-
45
- [ ${# a1[@]} -eq ${# a2[@]} ] || return 1
46
-
47
- local i
48
- for(( i= 0 ; i< ${# a1[@]} ; i++ )) ; do
49
- [ " ${a1[$i]} " = " ${a2[$i]} " ] || return 1
50
- done
51
- }
52
-
53
- compareAllVars () {
54
- local test_afterVars=` declare`
55
- diff <( echo " $test_beforeVars " | grep -v ' ^BASH_\|^_=' ) <( echo " $test_afterVars " | grep -v ' ^BASH_\|^_=\|^FUNCNAME=\|^test_' )
56
- }
57
-
58
- compareAllVarsExcludeOptVars () {
59
- local test_afterVars=` declare`
60
- diff <( echo " $test_beforeVars " | grep -v ' ^BASH_\|^_=' ) <( echo " $test_afterVars " | grep -v ' ^BASH_\|^_=\|^FUNCNAME=\|^test_\|^_OPT_\|^_opts_index_name_' )
61
- }
62
-
63
- fail () {
64
- redEcho " TEST FAIL: $* "
65
- exit 1
66
- }
7
+ source " $BASE /my_unit_test_lib.sh"
67
8
68
9
# ################################################
69
10
# Test
70
11
# ################################################
71
12
72
- test_beforeVars=` declare`
73
-
74
13
# ========================================
75
14
blueEcho " Test case: success parse"
76
15
# ========================================
@@ -82,18 +21,25 @@ _opts_showOptValueInfoList
82
21
83
22
[ $test_exitCode -eq 0 ] || fail " Wrong exit code!"
84
23
[ ${# _OPT_INFO_LIST_INDEX[@]} -eq 4 ] || fail " Wrong _OPT_INFO_LIST_INDEX!"
85
- [ $_OPT_VALUE_a = " true" ] && [ $_OPT_VALUE_a_long = " true" ] || fail " Wrong option value of a!"
24
+
25
+ [ $_OPT_VALUE_a = " true" ] && [ $_OPT_VALUE_a_long = " true" ] || fail " Wrong option value of a!"
86
26
[ $_OPT_VALUE_b = " bb" ] && [ $_OPT_VALUE_b_long = " bb" ] || fail " Wrong option value of b!"
27
+
87
28
test_cArray=(c.sh -p pv -q qv cc)
88
- arrayEquals test_cArray _OPT_VALUE_c && arrayEquals test_cArray _OPT_VALUE_c_long || fail " Wrong option value of c!"
89
- test_dArray=(d.sh -x xv d1 d2 d3 )
90
- arrayEquals test_dArray _OPT_VALUE_d && arrayEquals test_dArray _OPT_VALUE_d_long || fail " Wrong option value of d!"
29
+ assertArrayEquals " Wrong option value of c!" test_cArray _OPT_VALUE_c
30
+ assertArrayEquals " Wrong option value of c!" test_cArray _OPT_VALUE_c_long
31
+
32
+ test_dArray=(d.sh -x xv d1 d2 d3)
33
+ assertArrayEquals " Wrong option value of d!" test_dArray _OPT_VALUE_d
34
+ assertArrayEquals " Wrong option value of d!" test_dArray _OPT_VALUE_d_long
35
+
91
36
test_argArray=(aa bb cc dd ee)
92
- arrayEquals test_argArray _OPT_ARGS || fail " Wrong args!"
37
+ assertArrayEquals " Wrong args!" test_argArray _OPT_ARGS
38
+
39
+ assertAllVarsExcludeOptVarsSame
93
40
94
- compareAllVarsExcludeOptVars || fail " Unpected extra glable vars!"
95
41
_opts_cleanOptValueInfoList
96
- compareAllVars || fail " Unpected extra glable vars! "
42
+ assertAllVarsSame
97
43
98
44
# ========================================
99
45
blueEcho " Test case: success parse with -- "
@@ -106,18 +52,23 @@ _opts_showOptValueInfoList
106
52
107
53
[ $test_exitCode -eq 0 ] || fail " Wrong exit code!"
108
54
[ ${# _OPT_INFO_LIST_INDEX[@]} -eq 4 ] || fail " Wrong _OPT_INFO_LIST_INDEX!"
109
- [ $_OPT_VALUE_a = " true" ] && [ $_OPT_VALUE_a_long = " true" ] || fail " Wrong option value of a!"
55
+
56
+ [ $_OPT_VALUE_a = " true" ] && [ $_OPT_VALUE_a_long = " true" ] || fail " Wrong option value of a!"
110
57
[ $_OPT_VALUE_b = " bb" ] && [ $_OPT_VALUE_b_long = " bb" ] || fail " Wrong option value of b!"
58
+
111
59
test_cArray=(c.sh -p pv -q qv cc)
112
- arrayEquals test_cArray _OPT_VALUE_c && arrayEquals test_cArray _OPT_VALUE_c_long || fail " Wrong option value of c!"
60
+ assertArrayEquals " Wrong option value of c!" test_cArray _OPT_VALUE_c
61
+ assertArrayEquals " Wrong option value of c!" test_cArray _OPT_VALUE_c_long
62
+
113
63
[ " $_OPT_VALUE_d " = " " ] && [ " $_OPT_VALUE_d_long " = " " ] || fail " Wrong option value of d!"
64
+
114
65
test_argArray=(aa bb --d-long d.sh -x xv d1 d2 d3 \; cc dd ee)
115
- arrayEquals test_argArray _OPT_ARGS || fail " Wrong args!"
66
+ assertArrayEquals " Wrong args!" test_argArray _OPT_ARGS
116
67
117
- compareAllVarsExcludeOptVars || fail " Unpected extra glable vars!"
118
- _opts_cleanOptValueInfoList
119
- compareAllVars || fail " Unpected extra glable vars!"
68
+ assertAllVarsExcludeOptVarsSame
120
69
70
+ _opts_cleanOptValueInfoList
71
+ assertAllVarsSame
121
72
122
73
# ========================================
123
74
blueEcho " Test case: illegal option x"
@@ -130,14 +81,12 @@ _opts_showOptValueInfoList
130
81
131
82
[ $test_exitCode -eq 232 ] || fail " Wrong exit code!"
132
83
[ ${# _OPT_INFO_LIST_INDEX[@]} -eq 0 ] || fail " Wrong _OPT_INFO_LIST_INDEX!"
133
- [ " $_OPT_VALUE_a " = " " ] && [ " $_OPT_VALUE_a_long " = " " ] || fail " Wrong option value of a!"
84
+ [ " $_OPT_VALUE_a " = " " ] && [ " $_OPT_VALUE_a_long " = " " ] || fail " Wrong option value of a!"
134
85
[ " $_OPT_VALUE_b " = " " ] && [ " $_OPT_VALUE_b_long " = " " ] || fail " Wrong option value of b!"
135
86
[ " $_OPT_VALUE_c " = " " ] && [ " $_OPT_VALUE_c_long " = " " ] || fail " Wrong option value of c!"
136
87
[ " $_OPT_VALUE_d " = " " ] && [ " $_OPT_VALUE_d_long " = " " ] || fail " Wrong option value of d!"
137
88
[ " $_OPT_ARGS " = " " ] || fail " Wrong args!"
138
89
139
-
140
-
141
90
# ========================================
142
91
blueEcho " Test case: empty options"
143
92
# ========================================
@@ -149,14 +98,12 @@ _opts_showOptValueInfoList
149
98
150
99
[ $test_exitCode -eq 0 ] || fail " Wrong exit code!"
151
100
[ ${# _OPT_INFO_LIST_INDEX[@]} -eq 4 ] || fail " Wrong _OPT_INFO_LIST_INDEX!"
152
- [ " $_OPT_VALUE_a " = " " ] && [ " $_OPT_VALUE_a_long " = " " ] || fail " Wrong option value of a!"
101
+ [ " $_OPT_VALUE_a " = " " ] && [ " $_OPT_VALUE_a_long " = " " ] || fail " Wrong option value of a!"
153
102
[ " $_OPT_VALUE_b " = " " ] && [ " $_OPT_VALUE_b_long " = " " ] || fail " Wrong option value of b!"
154
103
[ " $_OPT_VALUE_c " = " " ] && [ " $_OPT_VALUE_c_long " = " " ] || fail " Wrong option value of c!"
155
104
[ " $_OPT_VALUE_d " = " " ] && [ " $_OPT_VALUE_d_long " = " " ] || fail " Wrong option value of d!"
156
105
[ " $_OPT_ARGS " = " " ] || fail " Wrong args!"
157
106
158
-
159
-
160
107
# ========================================
161
108
blueEcho " Test case: illegal option name"
162
109
# ========================================
@@ -168,26 +115,25 @@ _opts_showOptValueInfoList
168
115
169
116
[ $test_exitCode -eq 221 ] || fail " Wrong exit code!"
170
117
[ ${# _OPT_INFO_LIST_INDEX[@]} -eq 0 ] || fail " Wrong _OPT_INFO_LIST_INDEX!"
171
- [ " $_OPT_VALUE_a " = " " ] && [ " $_OPT_VALUE_a_long " = " " ] || fail " Wrong option value of a!"
118
+ [ " $_OPT_VALUE_a " = " " ] && [ " $_OPT_VALUE_a_long " = " " ] || fail " Wrong option value of a!"
172
119
[ " $_OPT_VALUE_b " = " " ] && [ " $_OPT_VALUE_b_long " = " " ] || fail " Wrong option value of b!"
173
120
[ " $_OPT_VALUE_c " = " " ] && [ " $_OPT_VALUE_c_long " = " " ] || fail " Wrong option value of c!"
174
121
[ " $_OPT_VALUE_d " = " " ] && [ " $_OPT_VALUE_d_long " = " " ] || fail " Wrong option value of d!"
175
122
[ " $_OPT_ARGS " = " " ] || fail " Wrong args!"
176
123
177
-
178
124
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
179
125
test_exitCode=$?
180
126
_opts_showOptDescInfoList
181
127
_opts_showOptValueInfoList
182
128
183
129
[ $test_exitCode -eq 222 ] || fail " Wrong exit code!"
184
130
[ ${# _OPT_INFO_LIST_INDEX[@]} -eq 0 ] || fail " Wrong _OPT_INFO_LIST_INDEX!"
185
- [ " $_OPT_VALUE_a " = " " ] && [ " $_OPT_VALUE_a_long " = " " ] || fail " Wrong option value of a!"
131
+ [ " $_OPT_VALUE_a " = " " ] && [ " $_OPT_VALUE_a_long " = " " ] || fail " Wrong option value of a!"
186
132
[ " $_OPT_VALUE_b " = " " ] && [ " $_OPT_VALUE_b_long " = " " ] || fail " Wrong option value of b!"
187
133
[ " $_OPT_VALUE_c " = " " ] && [ " $_OPT_VALUE_c_long " = " " ] || fail " Wrong option value of c!"
188
134
[ " $_OPT_VALUE_d " = " " ] && [ " $_OPT_VALUE_d_long " = " " ] || fail " Wrong option value of d!"
189
135
[ " $_OPT_ARGS " = " " ] || fail " Wrong args!"
190
136
191
- compareAllVars || fail " Unpected extra glable vars! "
137
+ assertAllVarsSame
192
138
193
139
greenEcho " TEST SUCCESS!!!"
0 commit comments