blob: d00f31ed82bd152886be13fe84f0447be68aead4 [file] [log] [blame]
kate.wardf51c6162008-06-17 16:38:35 +00001# vim:et:ft=sh:sts=2:sw=2
2#
kate.wardf51c6162008-06-17 16:38:35 +00003# shFlags unit test common functions
4
5__th_skipping=0
6
kate.ward31cb24f2008-11-12 20:13:28 +00007# treat unset variables as an error
8set -u
9
kate.ward1d0ecc42008-07-11 15:33:23 +000010# set shwordsplit for zsh
11[ -n "${ZSH_VERSION:-}" ] && setopt shwordsplit
12
kate.ward2fb41e32008-07-10 18:42:46 +000013# my name
14TH_MY_NAME=`basename "$0"`
15
kate.wardf51c6162008-06-17 16:38:35 +000016# path to shFlags library. can be overridden by setting SHFLAGS_INC
17TH_SHFLAGS=${SHFLAGS_INC:-./shflags}
18
19# path to shUnit2 library. can be overridden by setting SHUNIT_INC
20TH_SHUNIT=${SHUNIT_INC:-../lib/shunit2}
21
22TH_BOOL_VALID='true t 0 false f 1'
kate.warde10dd532013-01-04 21:52:23 +000023TH_BOOL_INVALID='123 123.0 invalid'
24TH_FLOAT_VALID='-1234.0 -1.0 -.123 0.0 0. .123 1.0 1234.0'
kate.wardc66a5fc2013-01-12 23:10:15 +000025TH_FLOAT_INVALID='true false 1.2.3 -1.2.3 ""'
kate.wardf51c6162008-06-17 16:38:35 +000026TH_INT_VALID='-1234 -1 0 1 1234'
kate.wardc66a5fc2013-01-12 23:10:15 +000027TH_INT_INVALID='true false -1.0 -.123 0.0 .123 1.0 ""'
kate.wardf51c6162008-06-17 16:38:35 +000028
29#
30# test helper functions
31#
32
33# message functions
34th_trace() { echo "test:TRACE $@" >&2; }
35th_debug() { echo "test:DEBUG $@" >&2; }
36th_info() { echo "test:INFO $@" >&2; }
37th_warn() { echo "test:WARN $@" >&2; }
38th_error() { echo "test:ERROR $@" >&2; }
39th_fatal() { echo "test:FATAL $@" >&2; }
40
kate.ward1b600c52008-11-12 21:26:05 +000041th_oneTimeSetUp()
42{
43 # load shFlags
44 [ -n "${ZSH_VERSION:-}" ] && FLAGS_PARENT=$0
45 . ${TH_SHFLAGS}
46
47 # these files will be cleaned up automatically by shUnit2
kate.ward1cb79602011-06-10 11:15:49 +000048 tmpDir=${SHUNIT_TMPDIR}
kate.ward1b600c52008-11-12 21:26:05 +000049 stdoutF="${tmpDir}/stdout"
50 stderrF="${tmpDir}/stderr"
kate.warda2adce12013-01-15 00:00:39 +000051 returnF="${tmpDir}/return"
kate.ward1b600c52008-11-12 21:26:05 +000052 expectedF="${tmpDir}/expected"
53}
54
kate.wardf51c6162008-06-17 16:38:35 +000055th_showOutput()
56{
57 _th_rtrn=$1
58 _th_stdout=$2
59 _th_stderr=$3
60
61 isSkipping
62 if [ $? -eq ${SHUNIT_FALSE} -a ${_th_rtrn} != ${FLAGS_TRUE} ]; then
kate.ward271aece2008-11-13 01:12:35 +000063 if [ -n "${_th_stdout}" -a -s "${_th_stdout}" ]; then
64 echo '>>> STDOUT' >&2
kate.ward31cb24f2008-11-12 20:13:28 +000065 cat "${_th_stdout}" >&2
66 fi
kate.ward271aece2008-11-13 01:12:35 +000067 if [ -n "${_th_stderr}" -a -s "${_th_stderr}" ]; then
68 echo '>>> STDERR' >&2
kate.ward31cb24f2008-11-12 20:13:28 +000069 cat "${_th_stderr}" >&2
70 fi
kate.ward271aece2008-11-13 01:12:35 +000071 if [ -n "${_th_stdout}" -o -n "${_th_stderr}" ]; then
kate.wardda1f8562013-01-05 13:56:11 +000072 echo '<<< end output' >&2
kate.ward271aece2008-11-13 01:12:35 +000073 fi
kate.wardf51c6162008-06-17 16:38:35 +000074 fi
75
76 unset _th_rtrn _th_stdout _th_stderr
77}
78
kate.warda2adce12013-01-15 00:00:39 +000079# Some shells, zsh on Solaris in particular, return immediately from a sub-shell
80# when a non-zero return value is encountered. To properly catch these values,
81# they are either written to disk, or recognized as an error the file is empty.
82th_clearReturn() { cp /dev/null "${returnF}"; }
83th_queryReturn()
84{
85 if [ -s "${returnF}" ]; then
86 th_return=`cat "${returnF}"`
87 else
88 th_return=${SHUNIT_ERROR}
89 fi
90}
91
kate.ward20d06782008-10-21 19:57:19 +000092_th_assertMsg()
kate.wardf51c6162008-06-17 16:38:35 +000093{
kate.ward20d06782008-10-21 19:57:19 +000094 _th_alert_type_=$1
95 _th_alert_msg_=$2
96 _th_msg_=$3
97
kate.ward2f3cad92008-10-21 23:29:23 +000098 case ${_th_alert_type_} in
kate.ward31cb24f2008-11-12 20:13:28 +000099 WARN) _th_alert_str_='a warning' ;;
100 ERROR) _th_alert_str_='an error' ;;
kate.ward271aece2008-11-13 01:12:35 +0000101 FATAL) _th_alert_str_='a fatal' ;;
kate.ward2f3cad92008-10-21 23:29:23 +0000102 esac
kate.ward31cb24f2008-11-12 20:13:28 +0000103 [ -z "${_th_alert_msg_}" ] && _th_alert_msg_='.*'
104 [ -n "${_th_msg_}" ] && _th_msg_="(${_th_msg_}) "
105
106 grep -- "^flags:${_th_alert_type_} ${_th_alert_msg_}" "${stderrF}" \
kate.ward2f3cad92008-10-21 23:29:23 +0000107 >/dev/null
108 assertTrue \
kate.ward31cb24f2008-11-12 20:13:28 +0000109 "FLAGS ${_th_msg_}failure did not generate ${_th_alert_str_} message" $?
kate.ward20d06782008-10-21 19:57:19 +0000110
kate.ward2f3cad92008-10-21 23:29:23 +0000111 unset _th_alert_type_ _th_alert_msg_ _th_alert_str_ _th_msg_
kate.wardf51c6162008-06-17 16:38:35 +0000112}
kate.ward20d06782008-10-21 19:57:19 +0000113
114assertWarnMsg() { _th_assertMsg 'WARN' "${1:-}" "${2:-}"; }
115assertErrorMsg() { _th_assertMsg 'ERROR' "${1:-}" "${2:-}"; }
kate.ward271aece2008-11-13 01:12:35 +0000116assertFatalMsg() { _th_assertMsg 'FATAL' "${1:-}" "${2:-}"; }