aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKate Ward <kate.ward@forestent.com>2020-04-12 00:25:04 +0200
committerKate Ward <kate.ward@forestent.com>2020-04-12 00:25:04 +0200
commit1c69f4a11767d4d4e794bbfba24e6bf5591bbad8 (patch)
tree8dfa3bffc259aa3659ebeb678100cf16eb620e19
parente5e39ae9452d923bb40ae655e5c3215c805614de (diff)
downloadshflags-1c69f4a11767d4d4e794bbfba24e6bf5591bbad8.tar.gz
Fixed more set -e errors.
-rwxr-xr-xshflags_private_test.sh145
-rw-r--r--shflags_test_helpers24
2 files changed, 108 insertions, 61 deletions
diff --git a/shflags_private_test.sh b/shflags_private_test.sh
index 6490a20..6435925 100755
--- a/shflags_private_test.sh
+++ b/shflags_private_test.sh
@@ -3,7 +3,7 @@
#
# shFlags unit tests for the internal functions.
#
-# Copyright 2008-2018 Kate Ward. All Rights Reserved.
+# Copyright 2008-2020 Kate Ward. All Rights Reserved.
# Released under the Apache 2.0 license.
#
# Author: kate.ward@forestent.com (Kate Ward)
@@ -86,35 +86,49 @@ _testGenOptStr() {
testGetFlagInfo() {
__flags_blah_foobar='1234'
- rslt=`_flags_getFlagInfo 'blah' 'foobar'`
- assertTrue 'request for valid flag info failed' $?
- assertEquals 'invalid flag info returned' "${__flags_blah_foobar}" "${rslt}"
-
- rslt=`_flags_getFlagInfo 'blah' 'hubbabubba' >"${stdoutF}" 2>"${stderrF}"`
- assertEquals 'invalid flag did not result in an error' "${FLAGS_ERROR}" $?
- assertErrorMsg 'missing flag info variable'
+ desc='valid_flag'
+ if rslt="`_flags_getFlagInfo 'blah' 'foobar'`"; then
+ assertEquals "${desc}: invalid flag result" "${__flags_blah_foobar}" "${rslt}"
+ else
+ fail "${desc}: request for valid flag info failed"
+ fi
+
+ desc='invalid_flag'
+ if rslt="`_flags_getFlagInfo 'blah' 'hubbabubba' >"${stdoutF}" 2>"${stderrF}"`"; then
+ fail "${desc}: expected invalid flag request to fail"
+ th_showOutput
+ else
+ assertEquals "${desc}: expected an error" "${FLAGS_ERROR}" $?
+ assertErrorMsg "missing flag info variable"
+ fi
}
testItemInList() {
list='this is a test'
# shellcheck disable=SC2162
while read desc item want; do
- _flags_itemInList "${item}" "${list}"
- got=$?
- assertEquals "${desc}: itemInList(${item})" "${got}" "${want}"
+ if [ ${want} -eq ${FLAGS_TRUE} ]; then
+ continue
+ fi
+ got=${FLAGS_TRUE}
+ if ! _flags_itemInList "${item}" "${list}"; then
+ got=${FLAGS_FALSE}
+ fi
+ assertEquals "${desc}: itemInList(${item})" "${want}" "${got}"
done <<EOF
-lead_item this ${FLAGS_TRUE}
-middle_item is ${FLAGS_TRUE}
-last_item test ${FLAGS_TRUE}
-missing_item asdf ${FLAGS_FALSE}
-test_partial_te te ${FLAGS_FALSE}
-test_partial_es es ${FLAGS_FALSE}
-test_partial_st st ${FLAGS_FALSE}
-empty_item '' ${FLAGS_FALSE}
+lead_item this ${FLAGS_TRUE}
+middle_item is ${FLAGS_TRUE}
+last_item test ${FLAGS_TRUE}
+missing_item asdf ${FLAGS_FALSE}
+test_partial_te te ${FLAGS_FALSE}
+test_partial_es es ${FLAGS_FALSE}
+test_partial_st st ${FLAGS_FALSE}
+empty_item '' ${FLAGS_FALSE}
EOF
- _flags_itemInList 'item' ''
- assertFalse 'empty lists should not match' $?
+ if _flags_itemInList 'item' ''; then
+ fail 'empty lists should not match'
+ fi
}
testUnderscoreName() {
@@ -130,36 +144,50 @@ empty "" ""
EOF
}
-testValidBool() {
+testBool() {
# Valid values.
for value in ${TH_BOOL_VALID}; do
- _flags_validBool "${value}"
- assertTrue "valid value (${value}) did not validate" $?
+ got=${FLAGS_TRUE}
+ if ! _flags_validBool "${value}"; then
+ got=${FLAGS_FALSE}
+ fi
+ assertTrue "valid value (${value}) did not validate" "${got}"
done
# Invalid values.
for value in ${TH_BOOL_INVALID}; do
- _flags_validBool "${value}"
- assertFalse "invalid value (${value}) validated" $?
+ got=${FLAGS_FALSE}
+ if _flags_validBool "${value}"; then
+ got=${FLAGS_TRUE}
+ fi
+ assertFalse "invalid value (${value}) validated" "${got}"
done
}
_testValidFloat() {
# Valid values.
for value in ${TH_INT_VALID} ${TH_FLOAT_VALID}; do
- _flags_validFloat "${value}"
- assertTrue "valid value (${value}) did not validate" $?
+ got=${FLAGS_TRUE}
+ if ! _flags_validFloat "${value}"; then
+ got=${FLAGS_FALSE}
+ fi
+ assertTrue "valid value (${value}) did not validate" "${got}"
done
# Invalid values.
for value in ${TH_FLOAT_INVALID}; do
- _flags_validFloat "${value}"
- assertFalse "invalid value (${value}) validated" $?
+ got=${FLAGS_FALSE}
+ if _flags_validFloat "${value}"; then
+ got=${FLAGS_TRUE}
+ fi
+ assertFalse "invalid value (${value}) validated" "${got}"
done
}
testValidFloatBuiltin() {
- _flags_useBuiltin || startSkipping
+ if ! _flags_useBuiltin; then
+ startSkipping
+ fi
_testValidFloat
}
@@ -173,19 +201,27 @@ testValidFloatExpr() {
_testValidInt() {
# Valid values.
for value in ${TH_INT_VALID}; do
- _flags_validInt "${value}"
- assertTrue "valid value (${value}) did not validate" $?
+ got=${FLAGS_TRUE}
+ if ! _flags_validInt "${value}"; then
+ got=${FLAGS_FALSE}
+ fi
+ assertTrue "valid value (${value}) did not validate" "${got}"
done
# Invalid values.
for value in ${TH_INT_INVALID}; do
- _flags_validInt "${value}"
- assertFalse "invalid value (${value}) should not validate" $?
+ got=${FLAGS_FALSE}
+ if _flags_validInt "${value}"; then
+ got=${FLAGS_TRUE}
+ fi
+ assertFalse "invalid value (${value}) should not validate" "${got}"
done
}
testValidIntBuiltin() {
- _flags_useBuiltin || startSkipping
+ if ! _flags_useBuiltin; then
+ startSkipping
+ fi
_testValidInt
}
@@ -197,20 +233,29 @@ testValidIntExpr() {
}
_testMath() {
- result=`_flags_math 1`
- assertTrue '1 failed' $?
- assertEquals '1' 1 "${result}"
-
- result=`_flags_math '1 + 2'`
- assertTrue '1+2 failed' $?
- assertEquals '1+2' 3 "${result}"
-
- result=`_flags_math '1 + 2 + 3'`
- assertTrue '1+2+3 failed' $?
- assertEquals '1+2+3' 6 "${result}"
-
- result=`_flags_math`
- assertFalse 'missing math succeeded' $?
+ if result=`_flags_math 1`; then
+ assertEquals '1' 1 "${result}"
+ else
+ fail '1 failed'
+ fi
+
+ if result=`_flags_math '1 + 2'`; then
+ assertEquals '1+2' 3 "${result}"
+ else
+ fail '1+2 failed'
+ fi
+
+ if result=`_flags_math '1 + 2 + 3'`; then
+ assertEquals '1+2+3' 6 "${result}"
+ else
+ fail '1+2+3 failed'
+ fi
+
+ got=${FLAGS_TRUE}
+ if ! _flags_math >/dev/null 2>&1; then
+ got=${FLAGS_FALSE}
+ fi
+ assertFalse 'missing math succeeded' "${got}"
}
testMathBuiltin() {
diff --git a/shflags_test_helpers b/shflags_test_helpers
index 9b8e41e..f5d2191 100644
--- a/shflags_test_helpers
+++ b/shflags_test_helpers
@@ -109,27 +109,29 @@ th_queryReturn() {
return "${SHUNIT_ERROR}"
}
+assertWarnMsg() { _th_assertMsg 'WARN' "${1:-}" "${2:-}"; }
+assertErrorMsg() { _th_assertMsg 'ERROR' "${1:-}" "${2:-}"; }
+assertFatalMsg() { _th_assertMsg 'FATAL' "${1:-}" "${2:-}"; }
+
_th_assertMsg() {
_th_alert_type_=$1
_th_alert_msg_=$2
_th_msg_=$3
case ${_th_alert_type_} in
- WARN) _th_alert_str_='a warning' ;;
+ WARN) _th_alert_str_='a warning' ;;
ERROR) _th_alert_str_='an error' ;;
FATAL) _th_alert_str_='a fatal' ;;
esac
- [ -z "${_th_alert_msg_}" ] && _th_alert_msg_='.*'
- [ -n "${_th_msg_}" ] && _th_msg_="(${_th_msg_}) "
+ if [ -z "${_th_alert_msg_}" ]; then
+ _th_alert_msg_='.*'
+ fi
+ if [ -n "${_th_msg_}" ]; then
+ _th_msg_="(${_th_msg_}) "
+ fi
- grep -- "^flags:${_th_alert_type_} ${_th_alert_msg_}" "${stderrF}" \
- >/dev/null
- assertTrue \
- "FLAGS ${_th_msg_}failure did not generate ${_th_alert_str_} message" $?
+ (grep -- "^flags:${_th_alert_type_} ${_th_alert_msg_}" "${stderrF}" >/dev/null)
+ assertEquals "FLAGS ${_th_msg_}failure did not generate ${_th_alert_str_} message" "${FLAGS_TRUE}" $?
unset _th_alert_type_ _th_alert_msg_ _th_alert_str_ _th_msg_
}
-
-assertWarnMsg() { _th_assertMsg 'WARN' "${1:-}" "${2:-}"; }
-assertErrorMsg() { _th_assertMsg 'ERROR' "${1:-}" "${2:-}"; }
-assertFatalMsg() { _th_assertMsg 'FATAL' "${1:-}" "${2:-}"; }