kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 1 | #! /bin/sh |
| 2 | # vim:et:ft=sh:sts=2:sw=2 |
| 3 | # |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 4 | # shFlags unit test for the flag definition methods |
| 5 | # |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 6 | # 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.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 16 | # TODO(kward): assert on FLAGS errors |
kate.ward | c521068 | 2009-03-30 18:54:36 +0000 | [diff] [blame] | 17 | # TODO(kward): testNonStandardIFS() |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 18 | |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 19 | # Exit immediately if a pipeline or subshell exits with a non-zero status. |
kate.ward | c66a5fc | 2013-01-12 23:10:15 +0000 | [diff] [blame] | 20 | #set -e |
| 21 | |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 22 | # Treat unset variables as an error. |
kate.ward | c66a5fc | 2013-01-12 23:10:15 +0000 | [diff] [blame] | 23 | set -u |
| 24 | |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 25 | # These variables will be overridden by the test helpers. |
| 26 | returnF="${TMPDIR:-/tmp}/return" |
| 27 | stdoutF="${TMPDIR:-/tmp}/STDOUT" |
| 28 | stderrF="${TMPDIR:-/tmp}/STDERR" |
| 29 | |
| 30 | # Load test helpers. |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 31 | . ./shflags_test_helpers |
| 32 | |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 33 | testGetoptStandard() { |
kate.ward | c521068 | 2009-03-30 18:54:36 +0000 | [diff] [blame] | 34 | _flags_getoptStandard '-b' >"${stdoutF}" 2>"${stderrF}" |
kate.ward | 619806d | 2008-11-15 19:59:57 +0000 | [diff] [blame] | 35 | rslt=$? |
| 36 | assertTrue "didn't parse valid flag 'b'" ${rslt} |
| 37 | th_showOutput ${rslt} "${stdoutF}" "${stderrF}" |
kate.ward | 9ebdd16 | 2008-11-12 20:11:57 +0000 | [diff] [blame] | 38 | |
kate.ward | c521068 | 2009-03-30 18:54:36 +0000 | [diff] [blame] | 39 | _flags_getoptStandard '-x' >"${stdoutF}" 2>"${stderrF}" |
kate.ward | 9ebdd16 | 2008-11-12 20:11:57 +0000 | [diff] [blame] | 40 | assertFalse "parsed invalid flag 'x'" $? |
| 41 | } |
| 42 | |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 43 | testGetoptEnhanced() { |
kate.ward | 20d9ef5 | 2013-01-06 15:56:46 +0000 | [diff] [blame] | 44 | flags_getoptIsEnh || return |
kate.ward | 271aece | 2008-11-13 01:12:35 +0000 | [diff] [blame] | 45 | |
kate.ward | c521068 | 2009-03-30 18:54:36 +0000 | [diff] [blame] | 46 | _flags_getoptEnhanced '-b' >"${stdoutF}" 2>"${stderrF}" |
kate.ward | 9ebdd16 | 2008-11-12 20:11:57 +0000 | [diff] [blame] | 47 | assertTrue "didn't parse valid flag 'b'" $? |
kate.ward | c521068 | 2009-03-30 18:54:36 +0000 | [diff] [blame] | 48 | _flags_getoptEnhanced '--bool' >"${stdoutF}" 2>"${stderrF}" |
kate.ward | 9ebdd16 | 2008-11-12 20:11:57 +0000 | [diff] [blame] | 49 | assertTrue "didn't parse valid flag 'bool'" $? |
| 50 | |
kate.ward | c521068 | 2009-03-30 18:54:36 +0000 | [diff] [blame] | 51 | _flags_getoptEnhanced '-x' >"${stdoutF}" 2>"${stderrF}" |
kate.ward | 9ebdd16 | 2008-11-12 20:11:57 +0000 | [diff] [blame] | 52 | assertFalse "parsed invalid flag 'x'" $? |
kate.ward | c521068 | 2009-03-30 18:54:36 +0000 | [diff] [blame] | 53 | _flags_getoptEnhanced '--xyz' >"${stdoutF}" 2>"${stderrF}" |
kate.ward | 9ebdd16 | 2008-11-12 20:11:57 +0000 | [diff] [blame] | 54 | assertFalse "parsed invalid flag 'xyz'" $? |
| 55 | } |
| 56 | |
kate.ward | e10dd53 | 2013-01-04 21:52:23 +0000 | [diff] [blame] | 57 | testValidBoolsShort() |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 58 | { |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 59 | FLAGS -b >"${stdoutF}" 2>"${stderrF}" |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 60 | r3turn=$? |
| 61 | assertTrue "-b) FLAGS returned a non-zero result (${r3turn})" ${r3turn} |
kate.ward | 9ebdd16 | 2008-11-12 20:11:57 +0000 | [diff] [blame] | 62 | value=${FLAGS_bool:-} |
kate.ward | 0a4f16f | 2013-01-07 22:10:49 +0000 | [diff] [blame] | 63 | assertTrue "-b) boolean was not true (${value})." "${value}" |
| 64 | assertFalse '-b) expected no output to STDERR' "[ -s '${stderrF}' ]" |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 65 | test ${r3turn} -eq "${FLAGS_TRUE}" -a ! -s "${stderrF}" |
kate.ward | abae05d | 2008-11-12 22:37:05 +0000 | [diff] [blame] | 66 | th_showOutput $? "${stdoutF}" "${stderrF}" |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 67 | |
kate.ward | 0a4f16f | 2013-01-07 22:10:49 +0000 | [diff] [blame] | 68 | DEFINE_boolean bool2 true '2nd boolean' B |
| 69 | FLAGS >"${stdoutF}" 2>"${stderrF}" |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 70 | r3turn=$? |
| 71 | assertTrue "-B) FLAGS returned a non-zero result (${r3turn})" ${r3turn} |
kate.ward | 0a4f16f | 2013-01-07 22:10:49 +0000 | [diff] [blame] | 72 | value=${FLAGS_bool2:-} |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 73 | assertTrue "-B) boolean was not true (${value})" "${value}" |
kate.ward | 0a4f16f | 2013-01-07 22:10:49 +0000 | [diff] [blame] | 74 | assertFalse '-B) expected no output to STDERR' "[ -s '${stderrF}' ]" |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 75 | test ${r3turn} -eq "${FLAGS_TRUE}" -a ! -s "${stderrF}" |
kate.ward | 0a4f16f | 2013-01-07 22:10:49 +0000 | [diff] [blame] | 76 | th_showOutput $? "${stdoutF}" "${stderrF}" |
| 77 | |
| 78 | FLAGS -B >"${stdoutF}" 2>"${stderrF}" |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 79 | r3turn=$? |
| 80 | assertTrue "-B) FLAGS returned a non-zero result (${r3turn})" ${r3turn} |
kate.ward | 0a4f16f | 2013-01-07 22:10:49 +0000 | [diff] [blame] | 81 | value=${FLAGS_bool2:-} |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 82 | assertFalse "-B) boolean was not false (${value})" "${value}" |
kate.ward | 0a4f16f | 2013-01-07 22:10:49 +0000 | [diff] [blame] | 83 | assertFalse '-B) expected no output to STDERR' "[ -s '${stderrF}' ]" |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 84 | test ${r3turn} -eq "${FLAGS_TRUE}" -a ! -s "${stderrF}" |
kate.ward | abae05d | 2008-11-12 22:37:05 +0000 | [diff] [blame] | 85 | th_showOutput $? "${stdoutF}" "${stderrF}" |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 86 | } |
| 87 | |
kate.ward | e10dd53 | 2013-01-04 21:52:23 +0000 | [diff] [blame] | 88 | # TODO(kate): separate into multiple functions to reflect correct usage |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 89 | testValidBoolsLong() { |
kate.ward | 20d9ef5 | 2013-01-06 15:56:46 +0000 | [diff] [blame] | 90 | flags_getoptIsEnh || return |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 91 | |
kate.ward | e10dd53 | 2013-01-04 21:52:23 +0000 | [diff] [blame] | 92 | # Note: the default value of bool is 'false'. |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 93 | |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 94 | # Leave flag false. |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 95 | FLAGS --nobool >"${stdoutF}" 2>"${stderrF}" |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 96 | r3turn=$? |
| 97 | assertTrue "FLAGS returned a non-zero result (${r3turn})" ${r3turn} |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 98 | assertFalse '--noXX flag resulted in true value.' "${FLAGS_bool:-}" |
kate.ward | e556cbf | 2008-11-13 00:41:59 +0000 | [diff] [blame] | 99 | assertFalse 'expected no output to STDERR' "[ -s '${stderrF}' ]" |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 100 | th_showOutput ${r3turn} "${stdoutF}" "${stderrF}" |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 101 | |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 102 | # Flip flag true. |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 103 | FLAGS --bool >"${stdoutF}" 2>"${stderrF}" |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 104 | r3turn=$? |
| 105 | assertTrue "FLAGS returned a non-zero result (${r3turn})" ${r3turn} |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 106 | assertTrue '--XX flag resulted in false value.' "${FLAGS_bool:-}" |
kate.ward | e556cbf | 2008-11-13 00:41:59 +0000 | [diff] [blame] | 107 | assertFalse 'expected no output to STDERR' "[ -s '${stderrF}' ]" |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 108 | th_showOutput ${r3turn} "${stdoutF}" "${stderrF}" |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 109 | |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 110 | # Flip flag back false. |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 111 | FLAGS --nobool >"${stdoutF}" 2>"${stderrF}" |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 112 | r3turn=$? |
| 113 | assertTrue "FLAGS returned a non-zero result (${r3turn})" ${r3turn} |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 114 | assertFalse '--noXX flag resulted in true value.' "${FLAGS_bool:-}" |
kate.ward | e556cbf | 2008-11-13 00:41:59 +0000 | [diff] [blame] | 115 | assertFalse 'expected no output to STDERR' "[ -s '${stderrF}' ]" |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 116 | th_showOutput ${r3turn} "${stdoutF}" "${stderrF}" |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 119 | testValidFloats() { |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 120 | _testValidFloats '-f' |
kate.ward | 20d9ef5 | 2013-01-06 15:56:46 +0000 | [diff] [blame] | 121 | flags_getoptIsEnh || return |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 122 | _testValidFloats '--float' |
| 123 | } |
| 124 | |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 125 | _testValidFloats() { |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 126 | flag=$1 |
| 127 | for value in ${TH_FLOAT_VALID}; do |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 128 | FLAGS "${flag}" "${value}" >"${stdoutF}" 2>"${stderrF}" |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 129 | r3turn=$? |
| 130 | assertTrue "FLAGS ${flag} ${value} returned non-zero result (${r3turn})" \ |
| 131 | ${r3turn} |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 132 | # shellcheck disable=SC2154 |
| 133 | assertEquals "float (${flag} ${value}) test failed." "${value}" "${FLAGS_float}" |
kate.ward | e556cbf | 2008-11-13 00:41:59 +0000 | [diff] [blame] | 134 | assertFalse 'expected no output to STDERR' "[ -s '${stderrF}' ]" |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 135 | th_showOutput ${r3turn} "${stdoutF}" "${stderrF}" |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 136 | done |
| 137 | } |
| 138 | |
| 139 | testInvalidFloats() |
| 140 | { |
| 141 | _testInvalidFloats '-f' |
kate.ward | 20d9ef5 | 2013-01-06 15:56:46 +0000 | [diff] [blame] | 142 | flags_getoptIsEnh || return |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 143 | _testInvalidFloats '--float' |
| 144 | } |
| 145 | |
| 146 | _testInvalidFloats() |
| 147 | { |
| 148 | flag=$1 |
| 149 | for value in ${TH_FLOAT_INVALID}; do |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 150 | th_clearReturn |
| 151 | ( |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 152 | FLAGS "${flag}" "${value}" >"${stdoutF}" 2>"${stderrF}" |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 153 | echo $? >"${returnF}" |
| 154 | ) |
Kate Ward | e46d632 | 2017-10-18 00:02:22 +0200 | [diff] [blame] | 155 | assertFalse "FLAGS (${value}) returned a zero result" "$(th_queryReturn)" |
kate.ward | e10dd53 | 2013-01-04 21:52:23 +0000 | [diff] [blame] | 156 | assertFalse 'expected no output to STDOUT' "[ -s '${stdoutF}' ]" |
| 157 | assertTrue 'expected output to STDERR' "[ -s '${stderrF}' ]" |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 158 | done |
| 159 | } |
| 160 | |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 161 | testValidIntegers() { |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 162 | _testValidIntegers '-i' |
kate.ward | 20d9ef5 | 2013-01-06 15:56:46 +0000 | [diff] [blame] | 163 | flags_getoptIsEnh || return |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 164 | _testValidIntegers '--int' |
| 165 | } |
| 166 | |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 167 | _testValidIntegers() { |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 168 | flag=$1 |
| 169 | for value in ${TH_INT_VALID}; do |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 170 | FLAGS "${flag}" "${value}" >"${stdoutF}" 2>"${stderrF}" |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 171 | r3turn=$? |
| 172 | assertTrue "FLAGS (${value}) returned a non-zero result (${r3turn})" ${r3turn} |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 173 | # shellcheck disable=SC2154 |
| 174 | assertEquals "integer (${value}) test failed." "${value}" "${FLAGS_int}" |
kate.ward | e556cbf | 2008-11-13 00:41:59 +0000 | [diff] [blame] | 175 | assertFalse 'expected no output to STDERR' "[ -s '${stderrF}' ]" |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 176 | th_showOutput ${r3turn} "${stdoutF}" "${stderrF}" |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 177 | done |
| 178 | } |
| 179 | |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 180 | testInvalidIntegers() { |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 181 | _testInvalidIntegers '-i' |
kate.ward | 20d9ef5 | 2013-01-06 15:56:46 +0000 | [diff] [blame] | 182 | flags_getoptIsEnh || return |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 183 | _testInvalidIntegers '--int' |
| 184 | } |
| 185 | |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 186 | _testInvalidIntegers() { |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 187 | flag=$1 |
| 188 | for value in ${TH_INT_INVALID}; do |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 189 | th_clearReturn |
| 190 | ( |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 191 | FLAGS "${flag}" "${value}" >"${stdoutF}" 2>"${stderrF}" |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 192 | echo $? >"${returnF}" |
| 193 | ) |
Kate Ward | e46d632 | 2017-10-18 00:02:22 +0200 | [diff] [blame] | 194 | assertFalse "invalid integer (${value}) test returned success." "$(th_queryReturn)" |
kate.ward | e10dd53 | 2013-01-04 21:52:23 +0000 | [diff] [blame] | 195 | assertFalse 'expected no output to STDOUT' "[ -s '${stdoutF}' ]" |
| 196 | assertTrue 'expected output to STDERR' "[ -s '${stderrF}' ]" |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 197 | done |
| 198 | } |
| 199 | |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 200 | testValidStrings() { |
kate.ward | 7a3c9c4 | 2009-04-24 00:12:35 +0000 | [diff] [blame] | 201 | _testValidStrings -s single_word |
kate.ward | 20d9ef5 | 2013-01-06 15:56:46 +0000 | [diff] [blame] | 202 | if flags_getoptIsEnh; then |
| 203 | _testValidStrings --str single_word |
| 204 | _testValidStrings --str 'string with spaces' |
| 205 | fi |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | _testValidStrings() |
| 209 | { |
| 210 | flag=$1 |
kate.ward | 7a3c9c4 | 2009-04-24 00:12:35 +0000 | [diff] [blame] | 211 | value=$2 |
| 212 | |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 213 | FLAGS "${flag}" "${value}" >"${stdoutF}" 2>"${stderrF}" |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 214 | r3turn=$? |
| 215 | assertTrue "'FLAGS ${flag} ${value}' returned a non-zero result (${r3turn})" \ |
| 216 | ${r3turn} |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 217 | # shellcheck disable=SC2154 |
kate.ward | 7a3c9c4 | 2009-04-24 00:12:35 +0000 | [diff] [blame] | 218 | assertEquals "string (${value}) test failed." "${value}" "${FLAGS_str}" |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 219 | if [ ${r3turn} -eq "${FLAGS_TRUE}" ]; then |
kate.ward | 7a3c9c4 | 2009-04-24 00:12:35 +0000 | [diff] [blame] | 220 | assertFalse 'expected no output to STDERR' "[ -s '${stderrF}' ]" |
| 221 | else |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 222 | # Validate that an error is thrown for unsupported getopt uses. |
kate.ward | 7a3c9c4 | 2009-04-24 00:12:35 +0000 | [diff] [blame] | 223 | assertFatalMsg '.* spaces in options' |
| 224 | fi |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 225 | th_showOutput ${r3turn} "${stdoutF}" "${stderrF}" |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 228 | testMultipleFlags() { |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 229 | _testMultipleFlags '-b' '-i' '-f' '-s' |
kate.ward | 20d9ef5 | 2013-01-06 15:56:46 +0000 | [diff] [blame] | 230 | flags_getoptIsEnh || return |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 231 | _testMultipleFlags '--bool' '--int' '--float' '--str' |
| 232 | } |
| 233 | |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 234 | _testMultipleFlags() { |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 235 | boolFlag=$1 |
| 236 | intFlag=$2 |
| 237 | floatFlag=$3 |
| 238 | strFlag=$4 |
| 239 | |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 240 | FLAGS \ |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 241 | "${boolFlag}" \ |
| 242 | "${intFlag}" 567 \ |
| 243 | "${floatFlag}" 123.45678 \ |
| 244 | "${strFlag}" 'some_string' \ |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 245 | >"${stdoutF}" 2>"${stderrF}" |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 246 | r3turn=$? |
Peter van der Does | ade2a17 | 2016-03-05 20:29:52 -0500 | [diff] [blame] | 247 | assertTrue "use of multiple flags returned a non-zero result" ${r3turn} |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 248 | 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.ward | e556cbf | 2008-11-13 00:41:59 +0000 | [diff] [blame] | 252 | assertFalse 'expected no output to STDERR' "[ -s '${stderrF}' ]" |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 253 | th_showOutput ${r3turn} "${stdoutF}" "${stderrF}" |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 256 | _testNonFlagArgs() { |
kate.ward | 7a3c9c4 | 2009-04-24 00:12:35 +0000 | [diff] [blame] | 257 | argc=$1 |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 258 | shift |
| 259 | |
kate.ward | 812aeae | 2009-04-01 02:49:51 +0000 | [diff] [blame] | 260 | FLAGS "$@" >"${stdoutF}" 2>"${stderrF}" |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 261 | r3turn=$? |
| 262 | assertTrue 'parse returned non-zero value.' ${r3turn} |
| 263 | th_showOutput ${r3turn} "${stdoutF}" "${stderrF}" |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 264 | |
kate.ward | c521068 | 2009-03-30 18:54:36 +0000 | [diff] [blame] | 265 | eval set -- "${FLAGS_ARGV}" |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 266 | assertEquals 'wrong count of argv arguments returned.' "${argc}" $# |
| 267 | assertEquals 'wrong count of argc arguments returned.' 0 "${FLAGS_ARGC}" |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 270 | testSingleNonFlagArg() { |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 271 | _testNonFlagArgs 1 argOne |
| 272 | } |
| 273 | |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 274 | testMultipleNonFlagArgs() { |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 275 | _testNonFlagArgs 3 argOne argTwo arg3 |
| 276 | } |
| 277 | |
| 278 | testMultipleNonFlagStringArgsWithSpaces() |
| 279 | { |
kate.ward | 20d9ef5 | 2013-01-06 15:56:46 +0000 | [diff] [blame] | 280 | flags_getoptIsEnh || return |
kate.ward | c521068 | 2009-03-30 18:54:36 +0000 | [diff] [blame] | 281 | _testNonFlagArgs 3 argOne 'arg two' arg3 |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 282 | } |
| 283 | |
kate.ward | c521068 | 2009-03-30 18:54:36 +0000 | [diff] [blame] | 284 | testFlagsWithEquals() |
| 285 | { |
kate.ward | 20d9ef5 | 2013-01-06 15:56:46 +0000 | [diff] [blame] | 286 | flags_getoptIsEnh || return |
kate.ward | 812aeae | 2009-04-01 02:49:51 +0000 | [diff] [blame] | 287 | |
kate.ward | c521068 | 2009-03-30 18:54:36 +0000 | [diff] [blame] | 288 | 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.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 291 | th_showOutput ${r3turn} "${stdoutF}" "${stderrF}" |
kate.ward | c521068 | 2009-03-30 18:54:36 +0000 | [diff] [blame] | 292 | |
| 293 | eval set -- "${FLAGS_ARGV}" |
| 294 | assertEquals 'wrong count of argv arguments returned.' 1 $# |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 295 | assertEquals 'wrong count of argc arguments returned.' 1 "${FLAGS_ARGC}" |
kate.ward | c521068 | 2009-03-30 18:54:36 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 298 | testComplicatedCommandLineStandard() { |
kate.ward | 32d97fb | 2013-01-06 16:05:26 +0000 | [diff] [blame] | 299 | 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.ward | 7a3c9c4 | 2009-04-24 00:12:35 +0000 | [diff] [blame] | 303 | FLAGS -i 1 non_flag_1 -s 'two' non_flag_2 -f 3 non_flag_3 \ |
| 304 | >"${stdoutF}" 2>"${stderrF}" |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 305 | r3turn=$? |
| 306 | assertTrue 'FLAGS returned a non-zero result' ${r3turn} |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 307 | assertEquals 'failed int test' 1 "${FLAGS_int}" |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 308 | th_showOutput ${r3turn} "${stdoutF}" "${stderrF}" |
kate.ward | c521068 | 2009-03-30 18:54:36 +0000 | [diff] [blame] | 309 | |
| 310 | eval set -- "${FLAGS_ARGV}" |
kate.ward | 32d97fb | 2013-01-06 16:05:26 +0000 | [diff] [blame] | 311 | assertEquals 'incorrect number of argv values' 7 $# |
kate.ward | 812aeae | 2009-04-01 02:49:51 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 314 | testComplicatedCommandLineEnhanced() { |
kate.ward | 20d9ef5 | 2013-01-06 15:56:46 +0000 | [diff] [blame] | 315 | flags_getoptIsEnh || return |
kate.ward | 812aeae | 2009-04-01 02:49:51 +0000 | [diff] [blame] | 316 | |
kate.ward | 7a3c9c4 | 2009-04-24 00:12:35 +0000 | [diff] [blame] | 317 | FLAGS -i 1 non_flag_1 --str='two' non_flag_2 --float 3 'non flag 3' \ |
| 318 | >"${stdoutF}" 2>"${stderrF}" |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 319 | r3turn=$? |
| 320 | assertTrue 'FLAGS returned a non-zero result' ${r3turn} |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 321 | assertEquals 'failed int test' 1 "${FLAGS_int}" |
kate.ward | 7a3c9c4 | 2009-04-24 00:12:35 +0000 | [diff] [blame] | 322 | assertEquals 'failed str test' 'two' "${FLAGS_str}" |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 323 | assertEquals 'failed float test' 3 "${FLAGS_float}" |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 324 | th_showOutput ${r3turn} "${stdoutF}" "${stderrF}" |
kate.ward | 7a3c9c4 | 2009-04-24 00:12:35 +0000 | [diff] [blame] | 325 | |
| 326 | eval set -- "${FLAGS_ARGV}" |
| 327 | assertEquals 'incorrect number of argv values' 3 $# |
kate.ward | c521068 | 2009-03-30 18:54:36 +0000 | [diff] [blame] | 328 | } |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 329 | |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 330 | oneTimeSetUp() { |
kate.ward | 1b600c5 | 2008-11-12 21:26:05 +0000 | [diff] [blame] | 331 | th_oneTimeSetUp |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 332 | |
kate.ward | 619806d | 2008-11-15 19:59:57 +0000 | [diff] [blame] | 333 | 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.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 337 | fi |
| 338 | } |
| 339 | |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 340 | setUp() { |
kate.ward | 8fe62e6 | 2010-03-28 23:07:35 +0000 | [diff] [blame] | 341 | DEFINE_boolean bool false 'boolean test' 'b' |
kate.ward | 9ebdd16 | 2008-11-12 20:11:57 +0000 | [diff] [blame] | 342 | DEFINE_float float 0.0 'float test' 'f' |
| 343 | DEFINE_integer int 0 'integer test' 'i' |
| 344 | DEFINE_string str '' 'string test' 's' |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 347 | tearDown() { |
kate.ward | e10dd53 | 2013-01-04 21:52:23 +0000 | [diff] [blame] | 348 | flags_reset |
| 349 | } |
| 350 | |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 351 | # Load and run shUnit2. |
| 352 | # shellcheck disable=SC2034 |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 353 | [ -n "${ZSH_VERSION:-}" ] && SHUNIT_PARENT=$0 |
Kate Ward | 8e37fd0 | 2017-10-18 00:12:37 +0200 | [diff] [blame] | 354 | . "${TH_SHUNIT}" |