• 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 flag definition methods
5
6# load test helpers
7. ./shflags_test_helpers
8
9#------------------------------------------------------------------------------
10# suite tests
11#
12
13testFlagsDefine()
14{
15  # no arguments
16  _flags_define >"${stdoutF}" 2>"${stderrF}"
17  assertFalse '_flags_define() with no arguments should have failed.' $?
18  assertErrorMsg '' 'no arguments'
19
20  # one argument
21  _flags_define arg1 >"${stdoutF}" 2>"${stderrF}"
22  assertFalse '_flags_define() call with one argument should fail' $?
23  assertErrorMsg '' 'one argument'
24
25  # two arguments
26  _flags_define arg1 arg2 >"${stdoutF}" 2>"${stderrF}"
27  assertFalse '_flags_define() call with two arguments should fail' $?
28  assertErrorMsg '' 'two arguments'
29
30  # three arguments
31  _flags_define arg1 arg2 arg3 >"${stdoutF}" 2>"${stderrF}"
32  assertFalse '_flags_define() call with three arguments should fail' $?
33  assertErrorMsg '' 'three arguments'
34
35  # multiple definition -- assumes working boolean definition (tested elsewhere)
36  _flags_define ${__FLAGS_TYPE_BOOLEAN} multiDefBool true 'multi def #1' m
37  _flags_define ${__FLAGS_TYPE_BOOLEAN} multiDefBool false 'multi def #2' m \
38      >"${stdoutF}" 2>"${stderrF}"
39  assertFalse '_flags_define() with existing flag name should fail' $?
40  assertTrue \
41      '_flags_define() should not overwrite previously defined default.' \
42      "${FLAGS_multiDefBool:-}"
43  assertWarnMsg '' 'existing flag'
44
45  # duplicate dashed and underscored definition
46  _flags_define ${__FLAGS_TYPE_STRING} long-name 'foo' 'dashed name' l
47  _flags_define ${__FLAGS_TYPE_STRING} long_name 'bar' 'underscored name' l \
48      >"${stdoutF}" 2>"${stderrF}"
49  assertFalse '_flags_define() with existing flag name should fail' $?
50  assertEquals \
51      '_flags_define() should not overwrite previously defined default.' \
52      "${FLAGS_long_name}" 'foo'
53  assertWarnMsg '' 'already exists'
54
55  # TODO(kward): test requirement of enhanced getopt
56
57  # invalid type
58  _flags_define invalid arg2 arg3 arg4 i >"${stdoutF}" 2>"${stderrF}"
59  assertFalse '_flags_define() with "invalid" type should have failed.' $?
60  assertErrorMsg 'unrecognized flag type' 'invalid type'
61}
62
63testBoolean()
64{
65  # test true defaults
66  for default in 'true' 't' 0; do
67    flags_reset
68    DEFINE_boolean boolVal "${default}" 'my boolean' b
69    rtrn=$?
70    assertTrue \
71        "DEFINE_boolean() call with default of '${default}' failed." \
72        "${FLAGS_boolVal:-}"
73    assertTrue \
74        "DEFINE_boolean() call with default of '${default}' returned faliure." \
75        ${rtrn}
76  done
77
78  # test false defaults
79  for default in 'false' 'f' 1; do
80    flags_reset
81    DEFINE_boolean boolVal "${default}" 'my boolean' b
82    rtrn=$?
83    assertFalse \
84        "DEFINE_boolean() call with default of '${default}' failed." \
85        "${FLAGS_boolVal:-}"
86    assertTrue \
87        "DEFINE_boolean() call with default of '${default}' returned faliure." \
88        ${rtrn}
89  done
90
91  # test invalid default
92  flags_reset
93  DEFINE_boolean boolVal 'invalid' 'my boolean' b >"${stdoutF}" 2>"${stderrF}"
94  assertFalse 'DEFINE_boolean() call with invalid default did not fail.' $?
95  assertErrorMsg
96}
97
98testFloat()
99{
100  # test valid defaults
101  for default in ${TH_FLOAT_VALID}; do
102    flags_reset
103    DEFINE_float floatVal ${default} "float: ${default}" f
104    rtrn=$?
105    assertSame "DEFINE_float() call with valid default failed." \
106        ${default} "${FLAGS_floatVal:-}"
107    assertTrue \
108        "DEFINE_float() call with valid default of '${default}' returned faliure." \
109        ${rtrn}
110  done
111
112  # test invalid defaults
113  flags_reset
114  DEFINE_float floatVal 'invalid' 'invalid float: string' f \
115      >"${stdoutF}" 2>"${stderrF}"
116  assertFalse 'DEFINE_float() call with string value default did not fail.' $?
117  assertErrorMsg
118}
119
120testInteger()
121{
122  # test valid defaults
123  for default in ${TH_INT_VALID}; do
124    flags_reset
125    DEFINE_integer intVal ${default} "integer: ${default}" i
126    rtrn=$?
127    assertSame \
128        "DEFINE_integer() call with valid default failed." \
129        ${default} "${FLAGS_intVal:-}"
130    assertTrue \
131        "DEFINE_integer() call with valid default of '${default}' returned failure." \
132        ${rtrn}
133  done
134
135  # test invalid defaults
136  flags_reset
137  DEFINE_integer intVal 1.234 'invalid integer: float' i \
138      >"${stdoutF}" 2>"${stderrF}"
139  assertFalse 'DEFINE_integer() call with float value default did not fail.' $?
140  assertErrorMsg 'invalid default' 'float default'
141
142  DEFINE_integer intVal -1.234 'invalid integer: negative float' i \
143      >"${stdoutF}" 2>"${stderrF}"
144  assertFalse \
145      'DEFINE_integer() call with negative float value default did not fail.' \
146      $?
147  assertErrorMsg 'invalid default' 'negative float default'
148
149  DEFINE_integer intVal 'invalid' 'invalid integer: string' i \
150      >"${stdoutF}" 2>"${stderrF}"
151  assertFalse \
152      'DEFINE_integer() call with string value default did not fail.' \
153      $?
154  assertErrorMsg 'invalid default' 'string default'
155}
156
157testString()
158{
159  # test valid defaults
160  for default in \
161      ${TH_BOOL_VALID} \
162      ${TH_FLOAT_VALID} \
163      ${TH_INT_VALID} \
164      'also valid'
165  do
166    flags_reset
167    DEFINE_string strVal "${default}" "string: ${default}" s
168    rtrn=$?
169    assertSame \
170        "DEFINE_string() call with valid default failed." \
171        "${default}" "${FLAGS_strVal:-}"
172    assertTrue \
173        "DEFINE_string() call with valid default of '${default}' returned faliure." \
174        ${rtrn}
175  done
176
177  # test "empty" strings
178  flags_reset
179  DEFINE_string str '' "string: empty single quotes" s
180  rtrn=$?
181  assertSame \
182      "DEFINE_string() call with valid default failed." \
183      '' "${FLAGS_str:-}"
184}
185
186testShortNameLength()
187{
188  # make sure short names are no longer than a single character
189  :
190}
191
192testFlagNameIsReserved()
193{
194  ( DEFINE_string TRUE '' 'true is a reserved flag name' t \
195      >"${stdoutF}" 2>"${stderrF}" )
196  rtrn=$?
197  assertEquals ${FLAGS_ERROR} ${rtrn}
198  assertErrorMsg 'flag name (TRUE) is reserved'
199}
200
201#------------------------------------------------------------------------------
202# suite functions
203#
204
205oneTimeSetUp()
206{
207  th_oneTimeSetUp
208}
209
210tearDown()
211{
212  flags_reset
213}
214
215# load and run shUnit2
216[ -n "${ZSH_VERSION:-}" ] && SHUNIT_PARENT=$0
217. ${TH_SHUNIT}
218