forked from kward/shflags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shflags_private_test.sh
executable file
·267 lines (221 loc) · 6.38 KB
/
shflags_private_test.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#! /bin/sh
# vim:et:ft=sh:sts=2:sw=2
#
# shFlags unit tests for the internal functions.
#
# Copyright 2008-2018 Kate Ward. All Rights Reserved.
# Released under the Apache 2.0 license.
#
# Author: [email protected] (Kate Ward)
# https://github.com/kward/shflags
#
### ShellCheck (http://www.shellcheck.net/)
# Disable source following.
# shellcheck disable=SC1090,SC1091
# expr may be antiquated, but it is the only solution in some cases.
# shellcheck disable=SC2003
# $() are not fully portable (POSIX != portable).
# shellcheck disable=SC2006
# These variables will be overridden by the test helpers.
stdoutF="${TMPDIR:-/tmp}/STDOUT"
stderrF="${TMPDIR:-/tmp}/STDERR"
# Load test helpers.
. ./shflags_test_helpers
testColumns() {
cols=`_flags_columns`
value=`expr "${cols}" : '\([0-9]*\)'`
assertNotNull "unexpected screen width (${cols})" "${value}"
}
testGetoptVers() {
# shellcheck disable=SC2162
while read desc mock want; do
assertEquals "${desc}" "$(_flags_getopt_vers "${mock}")" "${want}"
done <<EOF
standard mock_getopt_std ${__FLAGS_GETOPT_VERS_STD}
enhanced mock_getopt_enh ${__FLAGS_GETOPT_VERS_ENH}
EOF
}
### The mock_getopt_* commands behave like "getopt -lfoo '' --foo" was called.
# macOS 10.13.0.
mock_getopt_std() { echo ' -- --foo'; return 0; }
# Ubuntu 16.04.3
mock_getopt_enh() { echo ' --foo --'; return 0; }
testGenOptStr() {
_testGenOptStr '' ''
DEFINE_boolean bool false 'boolean value' b
_testGenOptStr 'b' 'bool'
DEFINE_float float 0.0 'float value' f
_testGenOptStr 'bf:' 'bool,float:'
DEFINE_integer int 0 'integer value' i
_testGenOptStr 'bf:i:' 'bool,float:,int:'
DEFINE_string str 0 'string value' s
_testGenOptStr 'bf:i:s:' 'bool,float:,int:,str:'
DEFINE_boolean help false 'show help' h
_testGenOptStr 'bf:i:s:h' 'bool,float:,int:,str:,help'
}
_testGenOptStr() {
short=$1
long=$2
result=`_flags_genOptStr "${__FLAGS_OPTSTR_SHORT}"`
assertTrue 'short option string generation failed' $?
assertEquals "${short}" "${result}"
result=`_flags_genOptStr "${__FLAGS_OPTSTR_LONG}"`
assertTrue 'long option string generation failed' $?
assertEquals "${long}" "${result}"
}
testGetFlagInfo() {
__flags_blah_foobar='1234'
rslt=`_flags_getFlagInfo 'blah' 'foobar'`
assertTrue 'request for valid flag info failed' $?
assertEquals 'invalid flag info returned' "${__flags_blah_foobar}" "${rslt}"
rslt=`_flags_getFlagInfo 'blah' 'hubbabubba' >"${stdoutF}" 2>"${stderrF}"`
assertEquals 'invalid flag did not result in an error' "${FLAGS_ERROR}" $?
assertErrorMsg 'missing flag info variable'
}
testItemInList() {
list='this is a test'
# shellcheck disable=SC2162
while read desc item want; do
_flags_itemInList "${item}" "${list}"
got=$?
assertEquals "${desc}: itemInList(${item})" "${got}" "${want}"
done <<EOF
lead_item this ${FLAGS_TRUE}
middle_item is ${FLAGS_TRUE}
last_item test ${FLAGS_TRUE}
missing_item asdf ${FLAGS_FALSE}
test_partial_te te ${FLAGS_FALSE}
test_partial_es es ${FLAGS_FALSE}
test_partial_st st ${FLAGS_FALSE}
empty_item '' ${FLAGS_FALSE}
EOF
_flags_itemInList 'item' ''
assertFalse 'empty lists should not match' $?
}
testUnderscoreName() {
# shellcheck disable=SC2162
while read desc name want; do
got=`_flags_underscoreName "${name}"`
assertEquals "${desc}: underscoreName(${name})" "${got}" "${want}"
done <<EOF
with_dashes name-with-dashes name_with_dashes
with_underscores name_with_underscores name_with_underscores
just_alpha_numeric abc123 abc123
empty "" ""
EOF
}
testValidBool() {
# Valid values.
for value in ${TH_BOOL_VALID}; do
_flags_validBool "${value}"
assertTrue "valid value (${value}) did not validate" $?
done
# Invalid values.
for value in ${TH_BOOL_INVALID}; do
_flags_validBool "${value}"
assertFalse "invalid value (${value}) validated" $?
done
}
_testValidFloat() {
# Valid values.
for value in ${TH_INT_VALID} ${TH_FLOAT_VALID}; do
_flags_validFloat "${value}"
assertTrue "valid value (${value}) did not validate" $?
done
# Invalid values.
for value in ${TH_FLOAT_INVALID}; do
_flags_validFloat "${value}"
assertFalse "invalid value (${value}) validated" $?
done
}
testValidFloatBuiltin() {
_flags_useBuiltin || startSkipping
_testValidFloat
}
testValidFloatExpr() {
(
_flags_useBuiltin() { return "${FLAGS_FALSE}"; }
_testValidFloat
)
}
_testValidInt() {
# Valid values.
for value in ${TH_INT_VALID}; do
_flags_validInt "${value}"
assertTrue "valid value (${value}) did not validate" $?
done
# Invalid values.
for value in ${TH_INT_INVALID}; do
_flags_validInt "${value}"
assertFalse "invalid value (${value}) should not validate" $?
done
}
testValidIntBuiltin() {
_flags_useBuiltin || startSkipping
_testValidInt
}
testValidIntExpr() {
(
_flags_useBuiltin() { return "${FLAGS_FALSE}"; }
_testValidInt
)
}
_testMath() {
result=`_flags_math 1`
assertTrue '1 failed' $?
assertEquals '1' 1 "${result}"
result=`_flags_math '1 + 2'`
assertTrue '1+2 failed' $?
assertEquals '1+2' 3 "${result}"
result=`_flags_math '1 + 2 + 3'`
assertTrue '1+2+3 failed' $?
assertEquals '1+2+3' 6 "${result}"
result=`_flags_math`
assertFalse 'missing math succeeded' $?
}
testMathBuiltin() {
_flags_useBuiltin || startSkipping
_testMath
}
testMathExpr() {
(
_flags_useBuiltin() { return "${FLAGS_FALSE}"; }
_testMath
)
}
_testStrlen() {
len=`_flags_strlen`
assertTrue 'missing argument failed' $?
assertEquals 'missing argument' 0 "${len}"
len=`_flags_strlen ''`
assertTrue 'empty argument failed' $?
assertEquals 'empty argument' 0 "${len}"
len=`_flags_strlen abc123`
assertTrue 'single-word failed' $?
assertEquals 'single-word' 6 "${len}"
len=`_flags_strlen 'This is a test'`
assertTrue 'multi-word failed' $?
assertEquals 'multi-word' 14 "${len}"
}
testStrlenBuiltin() {
_flags_useBuiltin || startSkipping
_testStrlen
}
testStrlenExpr() {
(
_flags_useBuiltin() { return "${FLAGS_FALSE}"; }
_testStrlen
)
}
oneTimeSetUp() {
th_oneTimeSetUp
_flags_useBuiltin || \
th_warn 'Shell built-ins not supported. Some tests will be skipped.'
}
tearDown() {
flags_reset
}
# Load and run shUnit2.
# shellcheck disable=SC2034
[ -n "${ZSH_VERSION:-}" ] && SHUNIT_PARENT=$0
. "${TH_SHUNIT}"