blob: ce7d1515418c60a2112da752a8dd2fdede1b8831 [file] [log] [blame]
kate.wardf51c6162008-06-17 16:38:35 +00001#! /bin/sh
2# vim:et:ft=sh:sts=2:sw=2
3#
kate.wardf51c6162008-06-17 16:38:35 +00004# shFlags unit test for the flag definition methods
5#
Kate Ward8e37fd02017-10-18 00:12:37 +02006# Copyright 2008-2017 Kate Ward. All Rights Reserved.
7# Released under the Apache 2.0 license.
8#
9# Author: kate.ward@forestent.com (Kate Ward)
10# https://github.com/kward/shflags
11#
12### ShellCheck (http://www.shellcheck.net/)
13# Disable source following.
14# shellcheck disable=SC1090,SC1091
15
kate.wardf51c6162008-06-17 16:38:35 +000016# TODO(kward): assert on FLAGS errors
kate.wardc5210682009-03-30 18:54:36 +000017# TODO(kward): testNonStandardIFS()
kate.wardf51c6162008-06-17 16:38:35 +000018
Kate Ward8e37fd02017-10-18 00:12:37 +020019# Exit immediately if a pipeline or subshell exits with a non-zero status.
kate.wardc66a5fc2013-01-12 23:10:15 +000020#set -e
21
Kate Ward8e37fd02017-10-18 00:12:37 +020022# Treat unset variables as an error.
kate.wardc66a5fc2013-01-12 23:10:15 +000023set -u
24
Kate Ward8e37fd02017-10-18 00:12:37 +020025# These variables will be overridden by the test helpers.
26returnF="${TMPDIR:-/tmp}/return"
27stdoutF="${TMPDIR:-/tmp}/STDOUT"
28stderrF="${TMPDIR:-/tmp}/STDERR"
29
30# Load test helpers.
kate.wardf51c6162008-06-17 16:38:35 +000031. ./shflags_test_helpers
32
Kate Ward8e37fd02017-10-18 00:12:37 +020033testGetoptStandard() {
kate.wardc5210682009-03-30 18:54:36 +000034 _flags_getoptStandard '-b' >"${stdoutF}" 2>"${stderrF}"
kate.ward619806d2008-11-15 19:59:57 +000035 rslt=$?
36 assertTrue "didn't parse valid flag 'b'" ${rslt}
37 th_showOutput ${rslt} "${stdoutF}" "${stderrF}"
kate.ward9ebdd162008-11-12 20:11:57 +000038
kate.wardc5210682009-03-30 18:54:36 +000039 _flags_getoptStandard '-x' >"${stdoutF}" 2>"${stderrF}"
kate.ward9ebdd162008-11-12 20:11:57 +000040 assertFalse "parsed invalid flag 'x'" $?
41}
42
Kate Ward8e37fd02017-10-18 00:12:37 +020043testGetoptEnhanced() {
kate.ward20d9ef52013-01-06 15:56:46 +000044 flags_getoptIsEnh || return
kate.ward271aece2008-11-13 01:12:35 +000045
kate.wardc5210682009-03-30 18:54:36 +000046 _flags_getoptEnhanced '-b' >"${stdoutF}" 2>"${stderrF}"
kate.ward9ebdd162008-11-12 20:11:57 +000047 assertTrue "didn't parse valid flag 'b'" $?
kate.wardc5210682009-03-30 18:54:36 +000048 _flags_getoptEnhanced '--bool' >"${stdoutF}" 2>"${stderrF}"
kate.ward9ebdd162008-11-12 20:11:57 +000049 assertTrue "didn't parse valid flag 'bool'" $?
50
kate.wardc5210682009-03-30 18:54:36 +000051 _flags_getoptEnhanced '-x' >"${stdoutF}" 2>"${stderrF}"
kate.ward9ebdd162008-11-12 20:11:57 +000052 assertFalse "parsed invalid flag 'x'" $?
kate.wardc5210682009-03-30 18:54:36 +000053 _flags_getoptEnhanced '--xyz' >"${stdoutF}" 2>"${stderrF}"
kate.ward9ebdd162008-11-12 20:11:57 +000054 assertFalse "parsed invalid flag 'xyz'" $?
55}
56
kate.warde10dd532013-01-04 21:52:23 +000057testValidBoolsShort()
kate.wardf51c6162008-06-17 16:38:35 +000058{
kate.wardf51c6162008-06-17 16:38:35 +000059 FLAGS -b >"${stdoutF}" 2>"${stderrF}"
kate.warda2adce12013-01-15 00:00:39 +000060 r3turn=$?
61 assertTrue "-b) FLAGS returned a non-zero result (${r3turn})" ${r3turn}
kate.ward9ebdd162008-11-12 20:11:57 +000062 value=${FLAGS_bool:-}
kate.ward0a4f16f2013-01-07 22:10:49 +000063 assertTrue "-b) boolean was not true (${value})." "${value}"
64 assertFalse '-b) expected no output to STDERR' "[ -s '${stderrF}' ]"
Kate Ward8e37fd02017-10-18 00:12:37 +020065 test ${r3turn} -eq "${FLAGS_TRUE}" -a ! -s "${stderrF}"
kate.wardabae05d2008-11-12 22:37:05 +000066 th_showOutput $? "${stdoutF}" "${stderrF}"
kate.wardf51c6162008-06-17 16:38:35 +000067
kate.ward0a4f16f2013-01-07 22:10:49 +000068 DEFINE_boolean bool2 true '2nd boolean' B
69 FLAGS >"${stdoutF}" 2>"${stderrF}"
kate.warda2adce12013-01-15 00:00:39 +000070 r3turn=$?
71 assertTrue "-B) FLAGS returned a non-zero result (${r3turn})" ${r3turn}
kate.ward0a4f16f2013-01-07 22:10:49 +000072 value=${FLAGS_bool2:-}
Kate Ward8e37fd02017-10-18 00:12:37 +020073 assertTrue "-B) boolean was not true (${value})" "${value}"
kate.ward0a4f16f2013-01-07 22:10:49 +000074 assertFalse '-B) expected no output to STDERR' "[ -s '${stderrF}' ]"
Kate Ward8e37fd02017-10-18 00:12:37 +020075 test ${r3turn} -eq "${FLAGS_TRUE}" -a ! -s "${stderrF}"
kate.ward0a4f16f2013-01-07 22:10:49 +000076 th_showOutput $? "${stdoutF}" "${stderrF}"
77
78 FLAGS -B >"${stdoutF}" 2>"${stderrF}"
kate.warda2adce12013-01-15 00:00:39 +000079 r3turn=$?
80 assertTrue "-B) FLAGS returned a non-zero result (${r3turn})" ${r3turn}
kate.ward0a4f16f2013-01-07 22:10:49 +000081 value=${FLAGS_bool2:-}
Kate Ward8e37fd02017-10-18 00:12:37 +020082 assertFalse "-B) boolean was not false (${value})" "${value}"
kate.ward0a4f16f2013-01-07 22:10:49 +000083 assertFalse '-B) expected no output to STDERR' "[ -s '${stderrF}' ]"
Kate Ward8e37fd02017-10-18 00:12:37 +020084 test ${r3turn} -eq "${FLAGS_TRUE}" -a ! -s "${stderrF}"
kate.wardabae05d2008-11-12 22:37:05 +000085 th_showOutput $? "${stdoutF}" "${stderrF}"
kate.wardf51c6162008-06-17 16:38:35 +000086}
87
kate.warde10dd532013-01-04 21:52:23 +000088# TODO(kate): separate into multiple functions to reflect correct usage
Kate Ward8e37fd02017-10-18 00:12:37 +020089testValidBoolsLong() {
kate.ward20d9ef52013-01-06 15:56:46 +000090 flags_getoptIsEnh || return
kate.wardf51c6162008-06-17 16:38:35 +000091
kate.warde10dd532013-01-04 21:52:23 +000092 # Note: the default value of bool is 'false'.
kate.wardf51c6162008-06-17 16:38:35 +000093
Kate Ward8e37fd02017-10-18 00:12:37 +020094 # Leave flag false.
kate.wardf51c6162008-06-17 16:38:35 +000095 FLAGS --nobool >"${stdoutF}" 2>"${stderrF}"
kate.warda2adce12013-01-15 00:00:39 +000096 r3turn=$?
97 assertTrue "FLAGS returned a non-zero result (${r3turn})" ${r3turn}
Kate Ward8e37fd02017-10-18 00:12:37 +020098 assertFalse '--noXX flag resulted in true value.' "${FLAGS_bool:-}"
kate.warde556cbf2008-11-13 00:41:59 +000099 assertFalse 'expected no output to STDERR' "[ -s '${stderrF}' ]"
kate.warda2adce12013-01-15 00:00:39 +0000100 th_showOutput ${r3turn} "${stdoutF}" "${stderrF}"
kate.wardf51c6162008-06-17 16:38:35 +0000101
Kate Ward8e37fd02017-10-18 00:12:37 +0200102 # Flip flag true.
kate.wardf51c6162008-06-17 16:38:35 +0000103 FLAGS --bool >"${stdoutF}" 2>"${stderrF}"
kate.warda2adce12013-01-15 00:00:39 +0000104 r3turn=$?
105 assertTrue "FLAGS returned a non-zero result (${r3turn})" ${r3turn}
Kate Ward8e37fd02017-10-18 00:12:37 +0200106 assertTrue '--XX flag resulted in false value.' "${FLAGS_bool:-}"
kate.warde556cbf2008-11-13 00:41:59 +0000107 assertFalse 'expected no output to STDERR' "[ -s '${stderrF}' ]"
kate.warda2adce12013-01-15 00:00:39 +0000108 th_showOutput ${r3turn} "${stdoutF}" "${stderrF}"
kate.wardf51c6162008-06-17 16:38:35 +0000109
Kate Ward8e37fd02017-10-18 00:12:37 +0200110 # Flip flag back false.
kate.wardf51c6162008-06-17 16:38:35 +0000111 FLAGS --nobool >"${stdoutF}" 2>"${stderrF}"
kate.warda2adce12013-01-15 00:00:39 +0000112 r3turn=$?
113 assertTrue "FLAGS returned a non-zero result (${r3turn})" ${r3turn}
Kate Ward8e37fd02017-10-18 00:12:37 +0200114 assertFalse '--noXX flag resulted in true value.' "${FLAGS_bool:-}"
kate.warde556cbf2008-11-13 00:41:59 +0000115 assertFalse 'expected no output to STDERR' "[ -s '${stderrF}' ]"
kate.warda2adce12013-01-15 00:00:39 +0000116 th_showOutput ${r3turn} "${stdoutF}" "${stderrF}"
kate.wardf51c6162008-06-17 16:38:35 +0000117}
118
Kate Ward8e37fd02017-10-18 00:12:37 +0200119testValidFloats() {
kate.wardf51c6162008-06-17 16:38:35 +0000120 _testValidFloats '-f'
kate.ward20d9ef52013-01-06 15:56:46 +0000121 flags_getoptIsEnh || return
kate.wardf51c6162008-06-17 16:38:35 +0000122 _testValidFloats '--float'
123}
124
Kate Ward8e37fd02017-10-18 00:12:37 +0200125_testValidFloats() {
kate.wardf51c6162008-06-17 16:38:35 +0000126 flag=$1
127 for value in ${TH_FLOAT_VALID}; do
Kate Ward8e37fd02017-10-18 00:12:37 +0200128 FLAGS "${flag}" "${value}" >"${stdoutF}" 2>"${stderrF}"
kate.warda2adce12013-01-15 00:00:39 +0000129 r3turn=$?
130 assertTrue "FLAGS ${flag} ${value} returned non-zero result (${r3turn})" \
131 ${r3turn}
Kate Ward8e37fd02017-10-18 00:12:37 +0200132 # shellcheck disable=SC2154
133 assertEquals "float (${flag} ${value}) test failed." "${value}" "${FLAGS_float}"
kate.warde556cbf2008-11-13 00:41:59 +0000134 assertFalse 'expected no output to STDERR' "[ -s '${stderrF}' ]"
kate.warda2adce12013-01-15 00:00:39 +0000135 th_showOutput ${r3turn} "${stdoutF}" "${stderrF}"
kate.wardf51c6162008-06-17 16:38:35 +0000136 done
137}
138
139testInvalidFloats()
140{
141 _testInvalidFloats '-f'
kate.ward20d9ef52013-01-06 15:56:46 +0000142 flags_getoptIsEnh || return
kate.wardf51c6162008-06-17 16:38:35 +0000143 _testInvalidFloats '--float'
144}
145
146_testInvalidFloats()
147{
148 flag=$1
149 for value in ${TH_FLOAT_INVALID}; do
kate.warda2adce12013-01-15 00:00:39 +0000150 th_clearReturn
151 (
Kate Ward8e37fd02017-10-18 00:12:37 +0200152 FLAGS "${flag}" "${value}" >"${stdoutF}" 2>"${stderrF}"
kate.warda2adce12013-01-15 00:00:39 +0000153 echo $? >"${returnF}"
154 )
Kate Warde46d6322017-10-18 00:02:22 +0200155 assertFalse "FLAGS (${value}) returned a zero result" "$(th_queryReturn)"
kate.warde10dd532013-01-04 21:52:23 +0000156 assertFalse 'expected no output to STDOUT' "[ -s '${stdoutF}' ]"
157 assertTrue 'expected output to STDERR' "[ -s '${stderrF}' ]"
kate.wardf51c6162008-06-17 16:38:35 +0000158 done
159}
160
Kate Ward8e37fd02017-10-18 00:12:37 +0200161testValidIntegers() {
kate.wardf51c6162008-06-17 16:38:35 +0000162 _testValidIntegers '-i'
kate.ward20d9ef52013-01-06 15:56:46 +0000163 flags_getoptIsEnh || return
kate.wardf51c6162008-06-17 16:38:35 +0000164 _testValidIntegers '--int'
165}
166
Kate Ward8e37fd02017-10-18 00:12:37 +0200167_testValidIntegers() {
kate.wardf51c6162008-06-17 16:38:35 +0000168 flag=$1
169 for value in ${TH_INT_VALID}; do
Kate Ward8e37fd02017-10-18 00:12:37 +0200170 FLAGS "${flag}" "${value}" >"${stdoutF}" 2>"${stderrF}"
kate.warda2adce12013-01-15 00:00:39 +0000171 r3turn=$?
172 assertTrue "FLAGS (${value}) returned a non-zero result (${r3turn})" ${r3turn}
Kate Ward8e37fd02017-10-18 00:12:37 +0200173 # shellcheck disable=SC2154
174 assertEquals "integer (${value}) test failed." "${value}" "${FLAGS_int}"
kate.warde556cbf2008-11-13 00:41:59 +0000175 assertFalse 'expected no output to STDERR' "[ -s '${stderrF}' ]"
kate.warda2adce12013-01-15 00:00:39 +0000176 th_showOutput ${r3turn} "${stdoutF}" "${stderrF}"
kate.wardf51c6162008-06-17 16:38:35 +0000177 done
178}
179
Kate Ward8e37fd02017-10-18 00:12:37 +0200180testInvalidIntegers() {
kate.wardf51c6162008-06-17 16:38:35 +0000181 _testInvalidIntegers '-i'
kate.ward20d9ef52013-01-06 15:56:46 +0000182 flags_getoptIsEnh || return
kate.wardf51c6162008-06-17 16:38:35 +0000183 _testInvalidIntegers '--int'
184}
185
Kate Ward8e37fd02017-10-18 00:12:37 +0200186_testInvalidIntegers() {
kate.wardf51c6162008-06-17 16:38:35 +0000187 flag=$1
188 for value in ${TH_INT_INVALID}; do
kate.warda2adce12013-01-15 00:00:39 +0000189 th_clearReturn
190 (
Kate Ward8e37fd02017-10-18 00:12:37 +0200191 FLAGS "${flag}" "${value}" >"${stdoutF}" 2>"${stderrF}"
kate.warda2adce12013-01-15 00:00:39 +0000192 echo $? >"${returnF}"
193 )
Kate Warde46d6322017-10-18 00:02:22 +0200194 assertFalse "invalid integer (${value}) test returned success." "$(th_queryReturn)"
kate.warde10dd532013-01-04 21:52:23 +0000195 assertFalse 'expected no output to STDOUT' "[ -s '${stdoutF}' ]"
196 assertTrue 'expected output to STDERR' "[ -s '${stderrF}' ]"
kate.wardf51c6162008-06-17 16:38:35 +0000197 done
198}
199
Kate Ward8e37fd02017-10-18 00:12:37 +0200200testValidStrings() {
kate.ward7a3c9c42009-04-24 00:12:35 +0000201 _testValidStrings -s single_word
kate.ward20d9ef52013-01-06 15:56:46 +0000202 if flags_getoptIsEnh; then
203 _testValidStrings --str single_word
204 _testValidStrings --str 'string with spaces'
205 fi
kate.wardf51c6162008-06-17 16:38:35 +0000206}
207
208_testValidStrings()
209{
210 flag=$1
kate.ward7a3c9c42009-04-24 00:12:35 +0000211 value=$2
212
Kate Ward8e37fd02017-10-18 00:12:37 +0200213 FLAGS "${flag}" "${value}" >"${stdoutF}" 2>"${stderrF}"
kate.warda2adce12013-01-15 00:00:39 +0000214 r3turn=$?
215 assertTrue "'FLAGS ${flag} ${value}' returned a non-zero result (${r3turn})" \
216 ${r3turn}
Kate Ward8e37fd02017-10-18 00:12:37 +0200217 # shellcheck disable=SC2154
kate.ward7a3c9c42009-04-24 00:12:35 +0000218 assertEquals "string (${value}) test failed." "${value}" "${FLAGS_str}"
Kate Ward8e37fd02017-10-18 00:12:37 +0200219 if [ ${r3turn} -eq "${FLAGS_TRUE}" ]; then
kate.ward7a3c9c42009-04-24 00:12:35 +0000220 assertFalse 'expected no output to STDERR' "[ -s '${stderrF}' ]"
221 else
Kate Ward8e37fd02017-10-18 00:12:37 +0200222 # Validate that an error is thrown for unsupported getopt uses.
kate.ward7a3c9c42009-04-24 00:12:35 +0000223 assertFatalMsg '.* spaces in options'
224 fi
kate.warda2adce12013-01-15 00:00:39 +0000225 th_showOutput ${r3turn} "${stdoutF}" "${stderrF}"
kate.wardf51c6162008-06-17 16:38:35 +0000226}
227
Kate Ward8e37fd02017-10-18 00:12:37 +0200228testMultipleFlags() {
kate.wardf51c6162008-06-17 16:38:35 +0000229 _testMultipleFlags '-b' '-i' '-f' '-s'
kate.ward20d9ef52013-01-06 15:56:46 +0000230 flags_getoptIsEnh || return
kate.wardf51c6162008-06-17 16:38:35 +0000231 _testMultipleFlags '--bool' '--int' '--float' '--str'
232}
233
Kate Ward8e37fd02017-10-18 00:12:37 +0200234_testMultipleFlags() {
kate.wardf51c6162008-06-17 16:38:35 +0000235 boolFlag=$1
236 intFlag=$2
237 floatFlag=$3
238 strFlag=$4
239
kate.wardf51c6162008-06-17 16:38:35 +0000240 FLAGS \
Kate Ward8e37fd02017-10-18 00:12:37 +0200241 "${boolFlag}" \
242 "${intFlag}" 567 \
243 "${floatFlag}" 123.45678 \
244 "${strFlag}" 'some_string' \
kate.wardf51c6162008-06-17 16:38:35 +0000245 >"${stdoutF}" 2>"${stderrF}"
kate.warda2adce12013-01-15 00:00:39 +0000246 r3turn=$?
Peter van der Doesade2a172016-03-05 20:29:52 -0500247 assertTrue "use of multiple flags returned a non-zero result" ${r3turn}
Kate Ward8e37fd02017-10-18 00:12:37 +0200248 assertTrue 'boolean test failed.' "${FLAGS_bool}"
249 assertNotSame 'float test failed.' 0 "${FLAGS_float}"
250 assertNotSame 'integer test failed.' 0 "${FLAGS_int}"
251 assertNotSame 'string test failed.' '' "${FLAGS_str}"
kate.warde556cbf2008-11-13 00:41:59 +0000252 assertFalse 'expected no output to STDERR' "[ -s '${stderrF}' ]"
kate.warda2adce12013-01-15 00:00:39 +0000253 th_showOutput ${r3turn} "${stdoutF}" "${stderrF}"
kate.wardf51c6162008-06-17 16:38:35 +0000254}
255
Kate Ward8e37fd02017-10-18 00:12:37 +0200256_testNonFlagArgs() {
kate.ward7a3c9c42009-04-24 00:12:35 +0000257 argc=$1
kate.wardf51c6162008-06-17 16:38:35 +0000258 shift
259
kate.ward812aeae2009-04-01 02:49:51 +0000260 FLAGS "$@" >"${stdoutF}" 2>"${stderrF}"
kate.warda2adce12013-01-15 00:00:39 +0000261 r3turn=$?
262 assertTrue 'parse returned non-zero value.' ${r3turn}
263 th_showOutput ${r3turn} "${stdoutF}" "${stderrF}"
kate.wardf51c6162008-06-17 16:38:35 +0000264
kate.wardc5210682009-03-30 18:54:36 +0000265 eval set -- "${FLAGS_ARGV}"
Kate Ward8e37fd02017-10-18 00:12:37 +0200266 assertEquals 'wrong count of argv arguments returned.' "${argc}" $#
267 assertEquals 'wrong count of argc arguments returned.' 0 "${FLAGS_ARGC}"
kate.wardf51c6162008-06-17 16:38:35 +0000268}
269
Kate Ward8e37fd02017-10-18 00:12:37 +0200270testSingleNonFlagArg() {
kate.wardf51c6162008-06-17 16:38:35 +0000271 _testNonFlagArgs 1 argOne
272}
273
Kate Ward8e37fd02017-10-18 00:12:37 +0200274testMultipleNonFlagArgs() {
kate.wardf51c6162008-06-17 16:38:35 +0000275 _testNonFlagArgs 3 argOne argTwo arg3
276}
277
278testMultipleNonFlagStringArgsWithSpaces()
279{
kate.ward20d9ef52013-01-06 15:56:46 +0000280 flags_getoptIsEnh || return
kate.wardc5210682009-03-30 18:54:36 +0000281 _testNonFlagArgs 3 argOne 'arg two' arg3
kate.wardf51c6162008-06-17 16:38:35 +0000282}
283
kate.wardc5210682009-03-30 18:54:36 +0000284testFlagsWithEquals()
285{
kate.ward20d9ef52013-01-06 15:56:46 +0000286 flags_getoptIsEnh || return
kate.ward812aeae2009-04-01 02:49:51 +0000287
kate.wardc5210682009-03-30 18:54:36 +0000288 FLAGS --str='str_flag' 'non_flag' >"${stdoutF}" 2>"${stderrF}"
289 assertTrue 'FLAGS returned a non-zero result' $?
290 assertEquals 'string flag not set properly' 'str_flag' "${FLAGS_str}"
kate.warda2adce12013-01-15 00:00:39 +0000291 th_showOutput ${r3turn} "${stdoutF}" "${stderrF}"
kate.wardc5210682009-03-30 18:54:36 +0000292
293 eval set -- "${FLAGS_ARGV}"
294 assertEquals 'wrong count of argv arguments returned.' 1 $#
Kate Ward8e37fd02017-10-18 00:12:37 +0200295 assertEquals 'wrong count of argc arguments returned.' 1 "${FLAGS_ARGC}"
kate.wardc5210682009-03-30 18:54:36 +0000296}
297
Kate Ward8e37fd02017-10-18 00:12:37 +0200298testComplicatedCommandLineStandard() {
kate.ward32d97fb2013-01-06 16:05:26 +0000299 flags_getoptIsEnh && return
300
301 # Note: standard getopt stops parsing after first non-flag argument, which
302 # results in the remaining flags being treated as arguments instead.
kate.ward7a3c9c42009-04-24 00:12:35 +0000303 FLAGS -i 1 non_flag_1 -s 'two' non_flag_2 -f 3 non_flag_3 \
304 >"${stdoutF}" 2>"${stderrF}"
kate.warda2adce12013-01-15 00:00:39 +0000305 r3turn=$?
306 assertTrue 'FLAGS returned a non-zero result' ${r3turn}
Kate Ward8e37fd02017-10-18 00:12:37 +0200307 assertEquals 'failed int test' 1 "${FLAGS_int}"
kate.warda2adce12013-01-15 00:00:39 +0000308 th_showOutput ${r3turn} "${stdoutF}" "${stderrF}"
kate.wardc5210682009-03-30 18:54:36 +0000309
310 eval set -- "${FLAGS_ARGV}"
kate.ward32d97fb2013-01-06 16:05:26 +0000311 assertEquals 'incorrect number of argv values' 7 $#
kate.ward812aeae2009-04-01 02:49:51 +0000312}
313
Kate Ward8e37fd02017-10-18 00:12:37 +0200314testComplicatedCommandLineEnhanced() {
kate.ward20d9ef52013-01-06 15:56:46 +0000315 flags_getoptIsEnh || return
kate.ward812aeae2009-04-01 02:49:51 +0000316
kate.ward7a3c9c42009-04-24 00:12:35 +0000317 FLAGS -i 1 non_flag_1 --str='two' non_flag_2 --float 3 'non flag 3' \
318 >"${stdoutF}" 2>"${stderrF}"
kate.warda2adce12013-01-15 00:00:39 +0000319 r3turn=$?
320 assertTrue 'FLAGS returned a non-zero result' ${r3turn}
Kate Ward8e37fd02017-10-18 00:12:37 +0200321 assertEquals 'failed int test' 1 "${FLAGS_int}"
kate.ward7a3c9c42009-04-24 00:12:35 +0000322 assertEquals 'failed str test' 'two' "${FLAGS_str}"
Kate Ward8e37fd02017-10-18 00:12:37 +0200323 assertEquals 'failed float test' 3 "${FLAGS_float}"
kate.warda2adce12013-01-15 00:00:39 +0000324 th_showOutput ${r3turn} "${stdoutF}" "${stderrF}"
kate.ward7a3c9c42009-04-24 00:12:35 +0000325
326 eval set -- "${FLAGS_ARGV}"
327 assertEquals 'incorrect number of argv values' 3 $#
kate.wardc5210682009-03-30 18:54:36 +0000328}
kate.wardf51c6162008-06-17 16:38:35 +0000329
Kate Ward8e37fd02017-10-18 00:12:37 +0200330oneTimeSetUp() {
kate.ward1b600c52008-11-12 21:26:05 +0000331 th_oneTimeSetUp
kate.wardf51c6162008-06-17 16:38:35 +0000332
kate.ward619806d2008-11-15 19:59:57 +0000333 if flags_getoptIsStd; then
334 th_warn 'Standard version of getopt found. Enhanced tests will be skipped.'
335 else
336 th_warn 'Enhanced version of getopt found. Standard tests will be skipped.'
kate.wardf51c6162008-06-17 16:38:35 +0000337 fi
338}
339
Kate Ward8e37fd02017-10-18 00:12:37 +0200340setUp() {
kate.ward8fe62e62010-03-28 23:07:35 +0000341 DEFINE_boolean bool false 'boolean test' 'b'
kate.ward9ebdd162008-11-12 20:11:57 +0000342 DEFINE_float float 0.0 'float test' 'f'
343 DEFINE_integer int 0 'integer test' 'i'
344 DEFINE_string str '' 'string test' 's'
kate.wardf51c6162008-06-17 16:38:35 +0000345}
346
Kate Ward8e37fd02017-10-18 00:12:37 +0200347tearDown() {
kate.warde10dd532013-01-04 21:52:23 +0000348 flags_reset
349}
350
Kate Ward8e37fd02017-10-18 00:12:37 +0200351# Load and run shUnit2.
352# shellcheck disable=SC2034
kate.wardf51c6162008-06-17 16:38:35 +0000353[ -n "${ZSH_VERSION:-}" ] && SHUNIT_PARENT=$0
Kate Ward8e37fd02017-10-18 00:12:37 +0200354. "${TH_SHUNIT}"