1#! /bin/sh 2# vim:et:ft=sh:sts=2:sw=2 3# 4# shFlags unit tests for the internal functions. 5# 6# Copyright 2008-2020 Kate Ward. All Rights Reserved. 7# Released under the Apache 2.0 license. 8# 9# Author: [email protected] (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# expr may be antiquated, but it is the only solution in some cases. 16# shellcheck disable=SC2003 17# $() are not fully portable (POSIX != portable). 18# shellcheck disable=SC2006 19 20# These variables will be overridden by the test helpers. 21stdoutF="${TMPDIR:-/tmp}/STDOUT" 22stderrF="${TMPDIR:-/tmp}/STDERR" 23 24# Load test helpers. 25. ./shflags_test_helpers 26 27testColumns() { 28 cols=`_flags_columns` 29 value=`expr "${cols}" : '\([0-9]*\)'` 30 assertNotNull "unexpected screen width (${cols})" "${value}" 31} 32 33testGetoptVers() { 34 # shellcheck disable=SC2162 35 while read desc mock want; do 36 assertEquals "${desc}" "$(_flags_getopt_vers "${mock}")" "${want}" 37 done <<EOF 38standard mock_getopt_std ${__FLAGS_GETOPT_VERS_STD} 39enhanced mock_getopt_enh ${__FLAGS_GETOPT_VERS_ENH} 40EOF 41} 42 43### The mock_getopt_* commands behave like "getopt -lfoo '' --foo" was called. 44# macOS 10.13.0. 45mock_getopt_std() { echo ' -- --foo'; return 0; } 46# Ubuntu 16.04.3 47mock_getopt_enh() { echo ' --foo --'; return 0; } 48 49testGenOptStr() { 50 _testGenOptStr '' '' 51 52 # shellcheck disable=SC2034 53 DEFINE_boolean bool false 'boolean value' b 54 _testGenOptStr 'b' 'bool' 55 56 # shellcheck disable=SC2034 57 DEFINE_float float 0.0 'float value' f 58 _testGenOptStr 'bf:' 'bool,float:' 59 60 # shellcheck disable=SC2034 61 DEFINE_integer int 0 'integer value' i 62 _testGenOptStr 'bf:i:' 'bool,float:,int:' 63 64 # shellcheck disable=SC2034 65 DEFINE_string str 0 'string value' s 66 _testGenOptStr 'bf:i:s:' 'bool,float:,int:,str:' 67 68 # shellcheck disable=SC2034 69 DEFINE_boolean help false 'show help' h 70 _testGenOptStr 'bf:i:s:h' 'bool,float:,int:,str:,help' 71} 72 73_testGenOptStr() { 74 short=$1 75 long=$2 76 77 result=`_flags_genOptStr "${__FLAGS_OPTSTR_SHORT}"` 78 assertTrue 'short option string generation failed' $? 79 assertEquals "${short}" "${result}" 80 81 result=`_flags_genOptStr "${__FLAGS_OPTSTR_LONG}"` 82 assertTrue 'long option string generation failed' $? 83 assertEquals "${long}" "${result}" 84} 85 86testGetFlagInfo() { 87 __flags_blah_foobar='1234' 88 89 desc='valid_flag' 90 if rslt="`_flags_getFlagInfo 'blah' 'foobar'`"; then 91 assertEquals "${desc}: invalid flag result" "${__flags_blah_foobar}" "${rslt}" 92 else 93 fail "${desc}: request for valid flag info failed" 94 fi 95 96 desc='invalid_flag' 97 if rslt="`_flags_getFlagInfo 'blah' 'hubbabubba' >"${stdoutF}" 2>"${stderrF}"`"; then 98 fail "${desc}: expected invalid flag request to fail" 99 th_showOutput 100 else 101 assertEquals "${desc}: expected an error" "${FLAGS_ERROR}" $? 102 assertErrorMsg "missing flag info variable" 103 fi 104} 105 106testItemInList() { 107 list='this is a test' 108 # shellcheck disable=SC2162 109 while read desc item want; do 110 if [ "${want}" -eq "${FLAGS_TRUE}" ]; then 111 continue 112 fi 113 got=${FLAGS_TRUE} 114 if ! _flags_itemInList "${item}" "${list}"; then 115 got=${FLAGS_FALSE} 116 fi 117 assertEquals "${desc}: itemInList(${item})" "${want}" "${got}" 118 done <<EOF 119lead_item this ${FLAGS_TRUE} 120middle_item is ${FLAGS_TRUE} 121last_item test ${FLAGS_TRUE} 122missing_item asdf ${FLAGS_FALSE} 123test_partial_te te ${FLAGS_FALSE} 124test_partial_es es ${FLAGS_FALSE} 125test_partial_st st ${FLAGS_FALSE} 126empty_item '' ${FLAGS_FALSE} 127EOF 128 129 if _flags_itemInList 'item' ''; then 130 fail 'empty lists should not match' 131 fi 132} 133 134testUnderscoreName() { 135 # shellcheck disable=SC2162 136 while read desc name want; do 137 got=`_flags_underscoreName "${name}"` 138 assertEquals "${desc}: underscoreName(${name})" "${got}" "${want}" 139 done <<EOF 140with_dashes name-with-dashes name_with_dashes 141with_underscores name_with_underscores name_with_underscores 142just_alpha_numeric abc123 abc123 143empty "" "" 144EOF 145} 146 147testBool() { 148 # Valid values. 149 for value in ${TH_BOOL_VALID}; do 150 got=${FLAGS_TRUE} 151 if ! _flags_validBool "${value}"; then 152 got=${FLAGS_FALSE} 153 fi 154 assertTrue "valid value (${value}) did not validate" "${got}" 155 done 156 157 # Invalid values. 158 for value in ${TH_BOOL_INVALID}; do 159 got=${FLAGS_FALSE} 160 if _flags_validBool "${value}"; then 161 got=${FLAGS_TRUE} 162 fi 163 assertFalse "invalid value (${value}) validated" "${got}" 164 done 165} 166 167_testValidFloat() { 168 # Valid values. 169 for value in ${TH_INT_VALID} ${TH_FLOAT_VALID}; do 170 got=${FLAGS_TRUE} 171 if ! _flags_validFloat "${value}"; then 172 got=${FLAGS_FALSE} 173 fi 174 assertTrue "valid value (${value}) did not validate" "${got}" 175 done 176 177 # Invalid values. 178 for value in ${TH_FLOAT_INVALID}; do 179 got=${FLAGS_FALSE} 180 if _flags_validFloat "${value}"; then 181 got=${FLAGS_TRUE} 182 fi 183 assertFalse "invalid value (${value}) validated" "${got}" 184 done 185} 186 187testValidFloatBuiltin() { 188 if ! _flags_useBuiltin; then 189 startSkipping 190 fi 191 _testValidFloat 192} 193 194testValidFloatExpr() { 195 ( 196 _flags_useBuiltin() { return "${FLAGS_FALSE}"; } 197 _testValidFloat 198 ) 199} 200 201_testValidInt() { 202 # Valid values. 203 for value in ${TH_INT_VALID}; do 204 got=${FLAGS_TRUE} 205 if ! _flags_validInt "${value}"; then 206 got=${FLAGS_FALSE} 207 fi 208 assertTrue "valid value (${value}) did not validate" "${got}" 209 done 210 211 # Invalid values. 212 for value in ${TH_INT_INVALID}; do 213 got=${FLAGS_FALSE} 214 if _flags_validInt "${value}"; then 215 got=${FLAGS_TRUE} 216 fi 217 assertFalse "invalid value (${value}) should not validate" "${got}" 218 done 219} 220 221testValidIntBuiltin() { 222 if ! _flags_useBuiltin; then 223 startSkipping 224 fi 225 _testValidInt 226} 227 228testValidIntExpr() { 229 ( 230 _flags_useBuiltin() { return "${FLAGS_FALSE}"; } 231 _testValidInt 232 ) 233} 234 235_testMath() { 236 if result=`_flags_math 1`; then 237 assertEquals '1' 1 "${result}" 238 else 239 fail '1 failed' 240 fi 241 242 if result=`_flags_math '1 + 2'`; then 243 assertEquals '1+2' 3 "${result}" 244 else 245 fail '1+2 failed' 246 fi 247 248 if result=`_flags_math '1 + 2 + 3'`; then 249 assertEquals '1+2+3' 6 "${result}" 250 else 251 fail '1+2+3 failed' 252 fi 253 254 got=${FLAGS_TRUE} 255 if ! _flags_math >/dev/null 2>&1; then 256 got=${FLAGS_FALSE} 257 fi 258 assertFalse 'missing math succeeded' "${got}" 259} 260 261testMathBuiltin() { 262 _flags_useBuiltin || startSkipping 263 _testMath 264} 265 266testMathExpr() { 267 ( 268 _flags_useBuiltin() { return "${FLAGS_FALSE}"; } 269 _testMath 270 ) 271} 272 273_testStrlen() { 274 len=`_flags_strlen` 275 assertTrue 'missing argument failed' $? 276 assertEquals 'missing argument' 0 "${len}" 277 278 len=`_flags_strlen ''` 279 assertTrue 'empty argument failed' $? 280 assertEquals 'empty argument' 0 "${len}" 281 282 len=`_flags_strlen abc123` 283 assertTrue 'single-word failed' $? 284 assertEquals 'single-word' 6 "${len}" 285 286 len=`_flags_strlen 'This is a test'` 287 assertTrue 'multi-word failed' $? 288 assertEquals 'multi-word' 14 "${len}" 289} 290 291testStrlenBuiltin() { 292 _flags_useBuiltin || startSkipping 293 _testStrlen 294} 295 296testStrlenExpr() { 297 ( 298 _flags_useBuiltin() { return "${FLAGS_FALSE}"; } 299 _testStrlen 300 ) 301} 302 303oneTimeSetUp() { 304 th_oneTimeSetUp 305 306 _flags_useBuiltin || \ 307 th_warn 'Shell built-ins not supported. Some tests will be skipped.' 308} 309 310tearDown() { 311 flags_reset 312} 313 314# Load and run shUnit2. 315# shellcheck disable=SC2034 316[ -n "${ZSH_VERSION:-}" ] && SHUNIT_PARENT=$0 317. "${TH_SHUNIT}" 318