• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#! /bin/sh
2# vim:et:ft=sh:sts=2:sw=2
3#
4# shFlags unit test for the internal functions
5
6# load test helpers
7. ./shflags_test_helpers
8
9#------------------------------------------------------------------------------
10# suite tests
11#
12
13testColumns()
14{
15  cols=`_flags_columns`
16  value=`expr "${cols}" : '\([0-9]*\)'`
17  assertNotNull "unexpected screen width (${cols})" "${value}"
18}
19
20testGenOptStr()
21{
22  _testGenOptStr '' ''
23
24  DEFINE_boolean bool false 'boolean value' b
25  _testGenOptStr 'b' 'bool'
26
27  DEFINE_float float 0.0 'float value' f
28  _testGenOptStr 'bf:' 'bool,float:'
29
30  DEFINE_integer int 0 'integer value' i
31  _testGenOptStr 'bf:i:' 'bool,float:,int:'
32
33  DEFINE_string str 0 'string value' s
34  _testGenOptStr 'bf:i:s:' 'bool,float:,int:,str:'
35
36  DEFINE_boolean help false 'show help' h
37  _testGenOptStr 'bf:i:s:h' 'bool,float:,int:,str:,help'
38}
39
40_testGenOptStr()
41{
42  short=$1
43  long=$2
44
45  result=`_flags_genOptStr ${__FLAGS_OPTSTR_SHORT}`
46  assertTrue 'short option string generation failed' $?
47  assertEquals "${short}" "${result}"
48
49  result=`_flags_genOptStr ${__FLAGS_OPTSTR_LONG}`
50  assertTrue 'long option string generation failed' $?
51  assertEquals "${long}" "${result}"
52}
53
54testGetFlagInfo()
55{
56  __flags_blah_foobar='1234'
57
58  rslt=`_flags_getFlagInfo 'blah' 'foobar'`
59  assertTrue 'request for valid flag info failed' $?
60  assertEquals 'invalid flag info returned' "${__flags_blah_foobar}" "${rslt}"
61
62  rslt=`_flags_getFlagInfo 'blah' 'hubbabubba' >"${stdoutF}" 2>"${stderrF}"`
63  assertEquals 'invalid flag did not result in an error' ${FLAGS_ERROR} $?
64  assertErrorMsg 'missing flag info variable'
65}
66
67testItemInList()
68{
69  list='this is a test'
70
71  _flags_itemInList 'is' ${list}
72  assertTrue 'unable to find leading string (this)' $?
73
74  _flags_itemInList 'is' ${list}
75  assertTrue 'unable to find string (is)' $?
76
77  _flags_itemInList 'is' ${list}
78  assertTrue 'unable to find trailing string (test)' $?
79
80  _flags_itemInList 'abc' ${list}
81  assertFalse 'found nonexistant string (abc)' $?
82
83  _flags_itemInList '' ${list}
84  assertFalse 'empty strings should not match' $?
85
86  _flags_itemInList 'blah' ''
87  assertFalse 'empty lists should not match' $?
88}
89
90testValidBool()
91{
92  # valid values
93  for value in ${TH_BOOL_VALID}; do
94    _flags_validBool "${value}"
95    assertTrue "valid value (${value}) did not validate" $?
96  done
97
98  # invalid values
99  for value in ${TH_BOOL_INVALID}; do
100    _flags_validBool "${value}"
101    assertFalse "invalid value (${value}) validated" $?
102  done
103}
104
105_testValidFloat()
106{
107  # valid values
108  for value in ${TH_INT_VALID} ${TH_FLOAT_VALID}; do
109    _flags_validFloat "${value}"
110    assertTrue "valid value (${value}) did not validate" $?
111  done
112
113  # invalid values
114  for value in ${TH_FLOAT_INVALID}; do
115    _flags_validFloat "${value}"
116    assertFalse "invalid value (${value}) validated" $?
117  done
118}
119
120testValidFloatBuiltin()
121{
122  _flags_useBuiltin || startSkipping
123  _testValidFloat
124}
125
126testValidFloatExpr()
127{
128  (
129    _flags_useBuiltin() { return ${FLAGS_FALSE}; }
130    _testValidFloat
131  )
132}
133
134_testValidInt()
135{
136  # valid values
137  for value in ${TH_INT_VALID}; do
138    _flags_validInt "${value}"
139    assertTrue "valid value (${value}) did not validate" $?
140  done
141
142  # invalid values
143  for value in ${TH_INT_INVALID}; do
144    _flags_validInt "${value}"
145    assertFalse "invalid value (${value}) should not validate" $?
146  done
147}
148
149testValidIntBuiltin()
150{
151  _flags_useBuiltin || startSkipping
152  _testValidInt
153}
154
155testValidIntExpr()
156{
157  (
158    _flags_useBuiltin() { return ${FLAGS_FALSE}; }
159    _testValidInt
160  )
161}
162
163_testMath()
164{
165  result=`_flags_math 1`
166  assertTrue '1 failed' $?
167  assertEquals '1' 1 ${result}
168
169  result=`_flags_math '1 + 2'`
170  assertTrue '1+2 failed' $?
171  assertEquals '1+2' 3 ${result}
172
173  result=`_flags_math '1 + 2 + 3'`
174  assertTrue '1+2+3 failed' $?
175  assertEquals '1+2+3' 6 ${result}
176
177  result=`_flags_math`
178  assertFalse 'missing math succeeded' $?
179}
180
181testMathBuiltin()
182{
183  _flags_useBuiltin || startSkipping
184  _testMath
185}
186
187testMathExpr()
188{
189  (
190    _flags_useBuiltin() { return ${FLAGS_FALSE}; }
191    _testMath
192  )
193}
194
195_testStrlen()
196{
197  len=`_flags_strlen`
198  assertTrue 'missing argument failed' $?
199  assertEquals 'missing argument' 0 ${len}
200
201  len=`_flags_strlen ''`
202  assertTrue 'empty argument failed' $?
203  assertEquals 'empty argument' 0 ${len}
204
205  len=`_flags_strlen abc123`
206  assertTrue 'single-word failed' $?
207  assertEquals 'single-word' 6 ${len}
208
209  len=`_flags_strlen 'This is a test'`
210  assertTrue 'multi-word failed' $?
211  assertEquals 'multi-word' 14 ${len}
212}
213
214testStrlenBuiltin()
215{
216  _flags_useBuiltin || startSkipping
217  _testStrlen
218}
219
220testStrlenExpr()
221{
222  (
223    _flags_useBuiltin() { return ${FLAGS_FALSE}; }
224    _testStrlen
225  )
226}
227
228#------------------------------------------------------------------------------
229# suite functions
230#
231
232oneTimeSetUp()
233{
234  th_oneTimeSetUp
235
236  _flags_useBuiltin || \
237    th_warn 'Shell built-ins not supported. Some tests will be skipped.'
238}
239
240tearDown()
241{
242  flags_reset
243}
244
245# load and run shUnit2
246[ -n "${ZSH_VERSION:-}" ] && SHUNIT_PARENT=$0
247. ${TH_SHUNIT}
248