1 # $Id: shunit2_test_helpers 286 2008-11-24 21:42:34Z kate.ward@forestent.com $
2 # vim:et:ft=sh:sts=2:sw=2
4 # Copyright 2008 Kate Ward. All Rights Reserved.
5 # Released under the LGPL (GNU Lesser General Public License)
7 # Author: kate.ward@forestent.com (Kate Ward)
9 # shUnit2 unit test common functions
11 # treat unset variables as an error when performing parameter expansion
14 # set shwordsplit for zsh
15 [ -n "${ZSH_VERSION:-}" ] && setopt shwordsplit
21 # path to shUnit2 library. can be overridden by setting SHUNIT_INC
22 TH_SHUNIT=${SHUNIT_INC:-./shunit2}
24 # configure debugging. set the DEBUG environment variable to any
25 # non-empty value to enable debug output, or TRACE to enable trace
27 TRACE=${TRACE:+'th_trace '}
28 [ -n "${TRACE}" ] && DEBUG=1
29 [ -z "${TRACE}" ] && TRACE=':'
31 DEBUG=${DEBUG:+'th_debug '}
32 [ -z "${DEBUG}" ] && DEBUG=':'
45 th_trace() { echo "${MY_NAME}:TRACE $@" >&2; }
46 th_debug() { echo "${MY_NAME}:DEBUG $@" >&2; }
47 th_info() { echo "${MY_NAME}:INFO $@" >&2; }
48 th_warn() { echo "${MY_NAME}:WARN $@" >&2; }
49 th_error() { echo "${MY_NAME}:ERROR $@" >&2; }
50 th_fatal() { echo "${MY_NAME}:FATAL $@" >&2; }
53 th_subtest() { echo " $@" >&2; }
55 # generate a random number
58 tfgr_random=${th_RANDOM}
60 while [ "${tfgr_random}" = "${th_RANDOM}" ]; do
61 if [ -n "${RANDOM:-}" ]; then
63 tfgr_random=${RANDOM}${RANDOM}${RANDOM}$$
64 elif [ -r '/dev/urandom' ]; then
65 tfgr_random=`od -vAn -N4 -tu4 </dev/urandom |sed 's/^[^0-9]*//'`
67 tfgr_date=`date '+%H%M%S'`
68 tfgr_random=`expr ${tfgr_date} \* $$`
71 [ "${tfgr_random}" = "${th_RANDOM}" ] && sleep 1
74 th_RANDOM=${tfgr_random}
78 # this section returns the data section from the specified section of a file. a
79 # datasection is defined by a [header], one or more lines of data, and then a
83 th_sgrep "\\[$1\\]" "$2" |sed '1d'
86 # this function greps a section from a file. a section is defined as a group of
87 # lines preceeded and followed by blank lines.
93 sed -e '/./{H;$!d;}' -e "x;/${th_pattern_}/"'!d;' $@ |sed '1d'
98 # Custom assert that checks for true return value (0), and no output to STDOUT
99 # or STDERR. If a non-zero return value is encountered, the output of STDERR
103 # th_test_: string: name of the subtest
104 # th_rtrn_: integer: the return value of the subtest performed
105 # th_stdout_: string: filename where stdout was redirected to
106 # th_stderr_: string: filename where stderr was redirected to
107 th_assertTrueWithNoOutput()
114 assertTrue "${th_test_}; expected return value of zero" ${th_rtrn_}
115 [ ${th_rtrn_} -ne ${SHUNIT_TRUE} ] && cat "${th_stderr_}"
116 assertFalse "${th_test_}; expected no output to STDOUT" \
117 "[ -s '${th_stdout_}' ]"
118 assertFalse "${th_test_}; expected no output to STDERR" \
119 "[ -s '${th_stderr_}' ]"
121 unset th_test_ th_rtrn_ th_stdout_ th_stderr_
124 # Custom assert that checks for non-zero return value, output to STDOUT, but no
128 # th_test_: string: name of the subtest
129 # th_rtrn_: integer: the return value of the subtest performed
130 # th_stdout_: string: filename where stdout was redirected to
131 # th_stderr_: string: filename where stderr was redirected to
132 th_assertFalseWithOutput()
139 assertFalse "${th_test_}; expected non-zero return value" ${th_rtrn_}
140 assertTrue "${th_test_}; expected output to STDOUT" \
141 "[ -s '${th_stdout_}' ]"
142 assertFalse "${th_test_}; expected no output to STDERR" \
143 "[ -s '${th_stderr_}' ]"
145 unset th_test_ th_rtrn_ th_stdout_ th_stderr_
148 # Custom assert that checks for non-zero return value, no output to STDOUT, but
152 # th_test_: string: name of the subtest
153 # th_rtrn_: integer: the return value of the subtest performed
154 # th_stdout_: string: filename where stdout was redirected to
155 # th_stderr_: string: filename where stderr was redirected to
156 th_assertFalseWithError()
163 assertFalse "${th_test_}; expected non-zero return value" ${th_rtrn_}
164 assertFalse "${th_test_}; expected no output to STDOUT" \
165 "[ -s '${th_stdout_}' ]"
166 assertTrue "${th_test_}; expected output to STDERR" \
167 "[ -s '${th_stderr_}' ]"
169 unset th_test_ th_rtrn_ th_stdout_ th_stderr_
176 ${TRACE} 'trace output enabled'
177 ${DEBUG} 'debug output enabled'