1#! /bin/sh
2# vim:et:ft=sh:sts=2:sw=2
3#
4# shFlags unit test for the public functions
5
6# load test helpers
7. ./shflags_test_helpers
8
9#------------------------------------------------------------------------------
10# suite tests
11#
12
13testHelp()
14{
15  _testHelp '-h'
16  flags_getoptIsEnh || return
17  _testHelp '--help'
18}
19
20_testHelp()
21{
22  flag=$1
23
24  # test default help output
25  th_clearReturn
26  (
27    FLAGS ${flag} >"${stdoutF}" 2>"${stderrF}"
28    echo $? >"${returnF}"
29  )
30  th_queryReturn
31  assertTrue \
32      'short help request should have returned a true exit code.' \
33      ${th_return}
34  grep 'show this help' "${stderrF}" >/dev/null
35  grepped=$?
36  assertTrue \
37      'short request for help should have produced some help output.' \
38      ${grepped}
39  [ ${grepped} -ne ${FLAGS_TRUE} ] && th_showOutput
40
41  # test proper output when FLAGS_HELP set
42  (
43    FLAGS_HELP='this is a test'
44    FLAGS ${flag} >"${stdoutF}" 2>"${stderrF}"
45  )
46  grep 'this is a test' "${stderrF}" >/dev/null
47  grepped=$?
48  assertTrue 'setting FLAGS_HELP did not produce expected result' ${grepped}
49  [ ${grepped} -ne ${FLAGS_TRUE} ] && th_showOutput
50
51  # test that "'" chars work in help string
52  (
53    DEFINE_boolean b false "help string containing a ' char" b
54    FLAGS ${flag} >"${stdoutF}" 2>"${stderrF}"
55  )
56  grep "help string containing a ' char" "${stderrF}" >/dev/null
57  grepped=$?
58  assertTrue "help strings containing apostrophes don't work" ${grepped}
59  [ ${grepped} -ne ${FLAGS_TRUE} ] && th_showOutput
60}
61
62mock_flags_columns()
63{
64  echo 80
65}
66
67testStandardHelpOutput()
68{
69  flags_getoptIsStd || startSkipping
70
71  DEFINE_boolean test_bool false 'test boolean' b
72  DEFINE_integer test_int 0 'test integer' i
73  DEFINE_string test_str '' 'test string' s
74  DEFINE_string long_desc 'blah' \
75      'testing of a long description to force wrap of default value' D
76  DEFINE_string long_default \
77      'this_is_a_long_default_value_to_force_alternate_indentation' \
78      'testing of long default value' F
79  help='USAGE: standard [flags] args'
80
81  cat >"${expectedF}" <<EOF
82${help}
83flags:
84  -b  test boolean (default: false)
85  -i  test integer (default: 0)
86  -s  test string (default: '')
87  -D  testing of a long description to force wrap of default value
88      (default: 'blah')
89  -F  testing of long default value
90      (default: 'this_is_a_long_default_value_to_force_alternate_indentation')
91  -h  show this help (default: false)
92EOF
93  (
94    _flags_columns() { mock_flags_columns; }
95    FLAGS_HELP=${help};
96    FLAGS -h >"${stdoutF}" 2>"${stderrF}"
97  )
98  r3turn=$?
99  assertTrue 'a call for help should not return an error' ${r3turn}
100
101  diff "${expectedF}" "${stderrF}" >/dev/null
102  r3turn=$?
103  assertTrue 'unexpected help output' ${r3turn}
104  th_showOutput ${r3turn} "${stdoutF}" "${stderrF}"
105}
106
107testEnhancedHelpOutput()
108{
109  flags_getoptIsEnh || startSkipping
110
111  DEFINE_boolean test_bool false 'test boolean' b
112  DEFINE_integer test_int 0 'test integer' i
113  DEFINE_string test_str '' 'test string' s
114  DEFINE_string long_desc 'blah' \
115      'testing of a long description to force wrap of default value' D
116  DEFINE_string long_default \
117      'this_is_a_long_default_value_to_force_alternate_indentation' \
118      'testing of long default value' F
119  help='USAGE: enhanced [flags] args'
120
121  cat >"${expectedF}" <<EOF
122${help}
123flags:
124  -b,--[no]test_bool:  test boolean (default: false)
125  -i,--test_int:  test integer (default: 0)
126  -s,--test_str:  test string (default: '')
127  -D,--long_desc:  testing of a long description to force wrap of default value
128                   (default: 'blah')
129  -F,--long_default:  testing of long default value
130    (default: 'this_is_a_long_default_value_to_force_alternate_indentation')
131  -h,--help:  show this help (default: false)
132EOF
133  (
134    _flags_columns() { mock_flags_columns; }
135    FLAGS_HELP=${help};
136    FLAGS -h >"${stdoutF}" 2>"${stderrF}"
137  )
138  r3turn=$?
139  assertTrue 'a call for help should not return an error' ${r3turn}
140
141  diff "${expectedF}" "${stderrF}" >/dev/null
142  differed=$?
143  assertTrue 'unexpected help output' ${differed}
144  th_showOutput ${differed} "${stdoutF}" "${stderrF}"
145}
146
147testNoHelp()
148{
149  flags_getoptIsEnh || startSkipping
150
151  ( FLAGS --nohelp >"${stdoutF}" 2>"${stderrF}" )
152  r3turn=$?
153  assertTrue "FLAGS returned a non-zero result (${r3turn})" ${r3turn}
154  assertFalse 'expected no output to STDOUT' "[ -s '${stdoutF}' ]"
155  assertFalse 'expected no output to STDERR' "[ -s '${stderrF}' ]"
156}
157
158#------------------------------------------------------------------------------
159# suite functions
160#
161
162oneTimeSetUp()
163{
164  th_oneTimeSetUp
165
166  if flags_getoptIsStd; then
167    th_warn 'Standard version of getopt found. Enhanced tests will be skipped.'
168  else
169    th_warn 'Enhanced version of getopt found. Standard tests will be skipped.'
170  fi
171}
172
173setUp()
174{
175  flags_reset
176}
177
178# load and run shUnit2
179[ -n "${ZSH_VERSION:-}" ] && SHUNIT_PARENT=$0
180. ${TH_SHUNIT}
181