bringing in testkit tools
[feisty_meow.git] / testkit / shunit / shunit2_test_helpers
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
3 #
4 # Copyright 2008 Kate Ward. All Rights Reserved.
5 # Released under the LGPL (GNU Lesser General Public License)
6 #
7 # Author: kate.ward@forestent.com (Kate Ward)
8 #
9 # shUnit2 unit test common functions
10
11 # treat unset variables as an error when performing parameter expansion
12 set -u
13
14 # set shwordsplit for zsh
15 [ -n "${ZSH_VERSION:-}" ] && setopt shwordsplit
16
17 #
18 # constants
19 #
20
21 # path to shUnit2 library. can be overridden by setting SHUNIT_INC
22 TH_SHUNIT=${SHUNIT_INC:-./shunit2}
23
24 # configure debugging. set the DEBUG environment variable to any
25 # non-empty value to enable debug output, or TRACE to enable trace
26 # output.
27 TRACE=${TRACE:+'th_trace '}
28 [ -n "${TRACE}" ] && DEBUG=1
29 [ -z "${TRACE}" ] && TRACE=':'
30
31 DEBUG=${DEBUG:+'th_debug '}
32 [ -z "${DEBUG}" ] && DEBUG=':'
33
34 #
35 # variables
36 #
37
38 th_RANDOM=0
39
40 #
41 # functions
42 #
43
44 # message functions
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; }
51
52 # output subtest name
53 th_subtest() { echo " $@" >&2; }
54
55 # generate a random number
56 th_generateRandom()
57 {
58   tfgr_random=${th_RANDOM}
59
60   while [ "${tfgr_random}" = "${th_RANDOM}" ]; do
61     if [ -n "${RANDOM:-}" ]; then
62       # $RANDOM works
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]*//'`
66     else
67       tfgr_date=`date '+%H%M%S'`
68       tfgr_random=`expr ${tfgr_date} \* $$`
69       unset tfgr_date
70     fi
71     [ "${tfgr_random}" = "${th_RANDOM}" ] && sleep 1
72   done
73
74   th_RANDOM=${tfgr_random}
75   unset tfgr_random
76 }
77
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
80 # blank line.
81 th_getDataSect()
82 {
83   th_sgrep "\\[$1\\]" "$2" |sed '1d'
84 }
85
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.
88 th_sgrep()
89 {
90   th_pattern_=$1
91   shift
92
93   sed -e '/./{H;$!d;}' -e "x;/${th_pattern_}/"'!d;' $@ |sed '1d'
94
95   unset th_pattern_
96 }
97
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
100 # will be output.
101 #
102 # Args:
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()
108 {
109   th_test_=$1
110   th_rtrn_=$2
111   th_stdout_=$3
112   th_stderr_=$4
113
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_}' ]"
120
121   unset th_test_ th_rtrn_ th_stdout_ th_stderr_
122 }
123
124 # Custom assert that checks for non-zero return value, output to STDOUT, but no
125 # output to STDERR.
126 #
127 # Args:
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()
133 {
134   th_test_=$1
135   th_rtrn_=$2
136   th_stdout_=$3
137   th_stderr_=$4
138
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_}' ]"
144
145   unset th_test_ th_rtrn_ th_stdout_ th_stderr_
146 }
147
148 # Custom assert that checks for non-zero return value, no output to STDOUT, but
149 # output to STDERR.
150 #
151 # Args:
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()
157 {
158   th_test_=$1
159   th_rtrn_=$2
160   th_stdout_=$3
161   th_stderr_=$4
162
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_}' ]"
168
169   unset th_test_ th_rtrn_ th_stdout_ th_stderr_
170 }
171
172 #
173 # main
174 #
175
176 ${TRACE} 'trace output enabled'
177 ${DEBUG} 'debug output enabled'