blob: 68f6e19e960604ed8a9826d8a0321f136d855e8e [file] [log] [blame]
kate.ward9a183b82010-03-18 00:25:34 +00001# $Id: shunit2 296 2010-03-17 23:49:03Z kate.ward@forestent.com $
kate.wardf51c6162008-06-17 16:38:35 +00002# vim:et:ft=sh:sts=2:sw=2
kate.wardf51c6162008-06-17 16:38:35 +00003#
kate.ward9a183b82010-03-18 00:25:34 +00004# Copyright 2008 Kate Ward. All Rights Reserved.
5# Released under the LGPL (GNU Lesser General Public License)
kate.wardf51c6162008-06-17 16:38:35 +00006#
kate.ward9a183b82010-03-18 00:25:34 +00007# shUnit2 -- Unit testing framework for Unix shell scripts.
8# http://code.google.com/p/shunit2/
kate.wardf51c6162008-06-17 16:38:35 +00009#
kate.ward9a183b82010-03-18 00:25:34 +000010# Author: kate.ward@forestent.com (Kate Ward)
kate.wardf51c6162008-06-17 16:38:35 +000011#
kate.ward9a183b82010-03-18 00:25:34 +000012# shUnit2 is a xUnit based unit test framework for Bourne shell scripts. It is
13# based on the popular JUnit unit testing framework for Java.
kate.wardf51c6162008-06-17 16:38:35 +000014
kate.ward9a183b82010-03-18 00:25:34 +000015SHUNIT_VERSION='2.1.6pre'
kate.wardf51c6162008-06-17 16:38:35 +000016
17SHUNIT_TRUE=0
18SHUNIT_FALSE=1
19SHUNIT_ERROR=2
20
kate.ward38097672008-11-03 20:58:42 +000021_shunit_warn() { echo "shunit2:WARN $@" >&2; }
22_shunit_error() { echo "shunit2:ERROR $@" >&2; }
23_shunit_fatal() { echo "shunit2:FATAL $@" >&2; }
24
kate.wardf51c6162008-06-17 16:38:35 +000025# specific shell checks
26if [ -n "${ZSH_VERSION:-}" ]; then
kate.wardf32fb4f2008-07-11 15:20:38 +000027 setopt |grep "^shwordsplit$" >/dev/null
28 if [ $? -ne ${SHUNIT_TRUE} ]; then
29 _shunit_fatal 'zsh shwordsplit option is required for proper operation'
kate.wardf51c6162008-06-17 16:38:35 +000030 exit ${SHUNIT_ERROR}
31 fi
32 if [ -z "${SHUNIT_PARENT:-}" ]; then
33 _shunit_fatal "zsh does not pass \$0 through properly. please declare \
34\"SHUNIT_PARENT=\$0\" before calling shUnit2"
35 exit ${SHUNIT_ERROR}
36 fi
37fi
38
kate.wardf51c6162008-06-17 16:38:35 +000039#
40# constants
41#
42
43__SHUNIT_ASSERT_MSG_PREFIX='ASSERT:'
44__SHUNIT_PARENT=${SHUNIT_PARENT:-$0}
45
46# set the constants readonly
kate.ward38097672008-11-03 20:58:42 +000047shunit_constants_=`set |grep '^__SHUNIT_' |cut -d= -f1`
48echo "${shunit_constants_}" |grep '^Binary file' >/dev/null \
49 && shunit_constants_=`set |grep -a '^__SHUNIT_' |cut -d= -f1`
50for shunit_constant_ in ${shunit_constants_}; do
kate.wardf32fb4f2008-07-11 15:20:38 +000051 shunit_ro_opts_=''
kate.ward38097672008-11-03 20:58:42 +000052 case ${ZSH_VERSION:-} in
53 '') ;; # this isn't zsh
54 [123].*) ;; # early versions (1.x, 2.x, 3.x)
55 *) shunit_ro_opts_='-g' ;; # all later versions. declare readonly globally
56 esac
57 readonly ${shunit_ro_opts_} ${shunit_constant_}
kate.wardf51c6162008-06-17 16:38:35 +000058done
kate.ward38097672008-11-03 20:58:42 +000059unset shunit_constant_ shunit_constants_ shunit_ro_opts_
kate.wardf51c6162008-06-17 16:38:35 +000060
61# variables
62__shunit_skip=${SHUNIT_FALSE}
63__shunit_suite=''
64
kate.ward38097672008-11-03 20:58:42 +000065# counts of tests
66__shunit_testSuccess=${SHUNIT_TRUE}
67__shunit_testsTotal=0
kate.wardf51c6162008-06-17 16:38:35 +000068__shunit_testsPassed=0
69__shunit_testsFailed=0
kate.ward38097672008-11-03 20:58:42 +000070
71# counts of asserts
72__shunit_assertsTotal=0
73__shunit_assertsPassed=0
74__shunit_assertsFailed=0
75__shunit_assertsSkipped=0
76
77__shunit_lineno=''
78__shunit_reportGenerated=${SHUNIT_FALSE}
kate.wardf51c6162008-06-17 16:38:35 +000079
kate.wardf32fb4f2008-07-11 15:20:38 +000080# macros
kate.ward38097672008-11-03 20:58:42 +000081_SHUNIT_LINENO_='eval __shunit_lineno=""; if [ "${1:-}" = "--lineno" ]; then [ -n "$2" ] && __shunit_lineno="[$2] "; shift 2; fi'
kate.wardf32fb4f2008-07-11 15:20:38 +000082
kate.wardf51c6162008-06-17 16:38:35 +000083#-----------------------------------------------------------------------------
84# assert functions
85#
86
kate.ward9a183b82010-03-18 00:25:34 +000087# Assert that two values are equal to one another.
88#
89# Args:
90# message: string: failure message [optional]
91# expected: string: expected value
92# actual: string: actual value
93# Returns:
94# integer: success (TRUE/FALSE/ERROR constant)
kate.wardf51c6162008-06-17 16:38:35 +000095assertEquals()
96{
kate.wardf32fb4f2008-07-11 15:20:38 +000097 ${_SHUNIT_LINENO_}
98 if [ $# -lt 2 -o $# -gt 3 ]; then
kate.ward38097672008-11-03 20:58:42 +000099 _shunit_error "assertEquals() requires two or three arguments; $# given"
100 _shunit_error "1: ${1:+$1} 2: ${2:+$2} 3: ${3:+$3}"
kate.wardf32fb4f2008-07-11 15:20:38 +0000101 return ${SHUNIT_ERROR}
102 fi
kate.wardf51c6162008-06-17 16:38:35 +0000103 _shunit_shouldSkip && return ${SHUNIT_TRUE}
104
kate.ward38097672008-11-03 20:58:42 +0000105 shunit_message_=${__shunit_lineno}
kate.wardf32fb4f2008-07-11 15:20:38 +0000106 if [ $# -eq 3 ]; then
107 shunit_message_="${shunit_message_}$1"
kate.wardf51c6162008-06-17 16:38:35 +0000108 shift
109 fi
kate.wardf32fb4f2008-07-11 15:20:38 +0000110 shunit_expected_=$1
111 shunit_actual_=$2
kate.wardf51c6162008-06-17 16:38:35 +0000112
113 shunit_return=${SHUNIT_TRUE}
kate.wardf32fb4f2008-07-11 15:20:38 +0000114 if [ "${shunit_expected_}" = "${shunit_actual_}" ]; then
kate.ward38097672008-11-03 20:58:42 +0000115 _shunit_assertPass
kate.wardf51c6162008-06-17 16:38:35 +0000116 else
kate.wardf32fb4f2008-07-11 15:20:38 +0000117 failNotEquals "${shunit_message_}" "${shunit_expected_}" "${shunit_actual_}"
kate.wardf51c6162008-06-17 16:38:35 +0000118 shunit_return=${SHUNIT_FALSE}
119 fi
120
kate.ward38097672008-11-03 20:58:42 +0000121 unset shunit_message_ shunit_expected_ shunit_actual_
kate.wardf51c6162008-06-17 16:38:35 +0000122 return ${shunit_return}
123}
kate.wardf32fb4f2008-07-11 15:20:38 +0000124_ASSERT_EQUALS_='eval assertEquals --lineno "${LINENO:-}"'
kate.wardf51c6162008-06-17 16:38:35 +0000125
kate.ward9a183b82010-03-18 00:25:34 +0000126# Assert that two values are not equal to one another.
127#
128# Args:
129# message: string: failure message [optional]
130# expected: string: expected value
131# actual: string: actual value
132# Returns:
133# integer: success (TRUE/FALSE/ERROR constant)
kate.ward38097672008-11-03 20:58:42 +0000134assertNotEquals()
135{
136 ${_SHUNIT_LINENO_}
137 if [ $# -lt 2 -o $# -gt 3 ]; then
138 _shunit_error "assertNotEquals() requires two or three arguments; $# given"
139 return ${SHUNIT_ERROR}
140 fi
141 _shunit_shouldSkip && return ${SHUNIT_TRUE}
142
143 shunit_message_=${__shunit_lineno}
144 if [ $# -eq 3 ]; then
145 shunit_message_="${shunit_message_}$1"
146 shift
147 fi
148 shunit_unexpected_=$1
149 shunit_actual_=$2
150
151 shunit_return=${SHUNIT_TRUE}
152 if [ "${shunit_unexpected_}" != "${shunit_actual_}" ]; then
153 _shunit_assertPass
154 else
155 failSame "${shunit_message_}" "$@"
156 shunit_return=${SHUNIT_FALSE}
157 fi
158
159 unset shunit_message_ shunit_unexpected_ shunit_actual_
160 return ${shunit_return}
161}
162_ASSERT_NOT_EQUALS_='eval assertNotEquals --lineno "${LINENO:-}"'
163
kate.ward9a183b82010-03-18 00:25:34 +0000164# Assert that a value is null (i.e. an empty string)
165#
166# Args:
167# message: string: failure message [optional]
168# actual: string: actual value
169# Returns:
170# integer: success (TRUE/FALSE/ERROR constant)
kate.wardf51c6162008-06-17 16:38:35 +0000171assertNull()
172{
kate.wardf32fb4f2008-07-11 15:20:38 +0000173 ${_SHUNIT_LINENO_}
174 if [ $# -lt 1 -o $# -gt 2 ]; then
kate.ward38097672008-11-03 20:58:42 +0000175 _shunit_error "assertNull() requires one or two arguments; $# given"
kate.wardf32fb4f2008-07-11 15:20:38 +0000176 return ${SHUNIT_ERROR}
177 fi
kate.wardf51c6162008-06-17 16:38:35 +0000178 _shunit_shouldSkip && return ${SHUNIT_TRUE}
179
kate.ward38097672008-11-03 20:58:42 +0000180 shunit_message_=${__shunit_lineno}
kate.wardf32fb4f2008-07-11 15:20:38 +0000181 if [ $# -eq 2 ]; then
182 shunit_message_="${shunit_message_}$1"
183 shift
184 fi
kate.ward38097672008-11-03 20:58:42 +0000185 assertTrue "${shunit_message_}" "[ -z '$1' ]"
186 shunit_return=$?
187
188 unset shunit_message_
189 return ${shunit_return}
kate.wardf51c6162008-06-17 16:38:35 +0000190}
kate.wardf32fb4f2008-07-11 15:20:38 +0000191_ASSERT_NULL_='eval assertNull --lineno "${LINENO:-}"'
kate.wardf51c6162008-06-17 16:38:35 +0000192
kate.ward9a183b82010-03-18 00:25:34 +0000193# Assert that a value is not null (i.e. a non-empty string)
194#
195# Args:
196# message: string: failure message [optional]
197# actual: string: actual value
198# Returns:
199# integer: success (TRUE/FALSE/ERROR constant)
kate.wardf51c6162008-06-17 16:38:35 +0000200assertNotNull()
201{
kate.wardf32fb4f2008-07-11 15:20:38 +0000202 ${_SHUNIT_LINENO_}
203 if [ $# -gt 2 ]; then # allowing 0 arguments as $1 might actually be null
kate.ward38097672008-11-03 20:58:42 +0000204 _shunit_error "assertNotNull() requires one or two arguments; $# given"
kate.wardf32fb4f2008-07-11 15:20:38 +0000205 return ${SHUNIT_ERROR}
206 fi
kate.wardf51c6162008-06-17 16:38:35 +0000207 _shunit_shouldSkip && return ${SHUNIT_TRUE}
208
kate.ward38097672008-11-03 20:58:42 +0000209 shunit_message_=${__shunit_lineno}
kate.wardf51c6162008-06-17 16:38:35 +0000210 if [ $# -eq 2 ]; then
kate.ward38097672008-11-03 20:58:42 +0000211 shunit_message_="${shunit_message_}$1"
212 shift
kate.wardf51c6162008-06-17 16:38:35 +0000213 fi
kate.ward38097672008-11-03 20:58:42 +0000214 assertTrue "${shunit_message_}" "[ -n '${1:-}' ]"
215 shunit_return=$?
216
217 unset shunit_message_
218 return ${shunit_return}
kate.wardf51c6162008-06-17 16:38:35 +0000219}
kate.wardf32fb4f2008-07-11 15:20:38 +0000220_ASSERT_NOT_NULL_='eval assertNotNull --lineno "${LINENO:-}"'
kate.wardf51c6162008-06-17 16:38:35 +0000221
kate.ward9a183b82010-03-18 00:25:34 +0000222# Assert that two values are the same (i.e. equal to one another).
223#
224# Args:
225# message: string: failure message [optional]
226# expected: string: expected value
227# actual: string: actual value
228# Returns:
229# integer: success (TRUE/FALSE/ERROR constant)
kate.wardf51c6162008-06-17 16:38:35 +0000230assertSame()
231{
kate.wardf32fb4f2008-07-11 15:20:38 +0000232 ${_SHUNIT_LINENO_}
233 if [ $# -lt 2 -o $# -gt 3 ]; then
kate.ward38097672008-11-03 20:58:42 +0000234 _shunit_error "assertSame() requires one or two arguments; $# given"
kate.wardf32fb4f2008-07-11 15:20:38 +0000235 return ${SHUNIT_ERROR}
236 fi
237 _shunit_shouldSkip && return ${SHUNIT_TRUE}
238
kate.ward38097672008-11-03 20:58:42 +0000239 shunit_message_=${__shunit_lineno}
240 if [ $# -eq 3 ]; then
241 shunit_message_="${shunit_message_}$1"
242 shift
kate.wardf32fb4f2008-07-11 15:20:38 +0000243 fi
kate.ward38097672008-11-03 20:58:42 +0000244 assertEquals "${shunit_message_}" "$1" "$2"
245 shunit_return=$?
246
247 unset shunit_message_
248 return ${shunit_return}
kate.wardf51c6162008-06-17 16:38:35 +0000249}
kate.wardf32fb4f2008-07-11 15:20:38 +0000250_ASSERT_SAME_='eval assertSame --lineno "${LINENO:-}"'
kate.wardf51c6162008-06-17 16:38:35 +0000251
kate.ward9a183b82010-03-18 00:25:34 +0000252# Assert that two values are not the same (i.e. not equal to one another).
253#
254# Args:
255# message: string: failure message [optional]
256# expected: string: expected value
257# actual: string: actual value
258# Returns:
259# integer: success (TRUE/FALSE/ERROR constant)
kate.wardf51c6162008-06-17 16:38:35 +0000260assertNotSame()
261{
kate.wardf32fb4f2008-07-11 15:20:38 +0000262 ${_SHUNIT_LINENO_}
263 if [ $# -lt 2 -o $# -gt 3 ]; then
kate.ward38097672008-11-03 20:58:42 +0000264 _shunit_error "assertNotSame() requires two or three arguments; $# given"
kate.wardf32fb4f2008-07-11 15:20:38 +0000265 return ${SHUNIT_ERROR}
266 fi
kate.wardf51c6162008-06-17 16:38:35 +0000267 _shunit_shouldSkip && return ${SHUNIT_TRUE}
268
kate.ward38097672008-11-03 20:58:42 +0000269 shunit_message_=${__shunit_lineno}
kate.wardf51c6162008-06-17 16:38:35 +0000270 if [ $# -eq 3 ]; then
kate.ward38097672008-11-03 20:58:42 +0000271 shunit_message_="${shunit_message_:-}$1"
kate.wardf51c6162008-06-17 16:38:35 +0000272 shift
273 fi
kate.ward38097672008-11-03 20:58:42 +0000274 assertNotEquals "${shunit_message_}" "$1" "$2"
275 shunit_return=$?
kate.wardf51c6162008-06-17 16:38:35 +0000276
kate.ward38097672008-11-03 20:58:42 +0000277 unset shunit_message_
kate.wardf51c6162008-06-17 16:38:35 +0000278 return ${shunit_return}
279}
kate.wardf32fb4f2008-07-11 15:20:38 +0000280_ASSERT_NOT_SAME_='eval assertNotSame --lineno "${LINENO:-}"'
kate.wardf51c6162008-06-17 16:38:35 +0000281
kate.ward9a183b82010-03-18 00:25:34 +0000282# Assert that a value or shell test condition is true.
283#
284# In shell, a value of 0 is true and a non-zero value is false. Any integer
285# value passed can thereby be tested.
286#
287# Shell supports much more complicated tests though, and a means to support
288# them was needed. As such, this function tests that conditions are true or
289# false through evaluation rather than just looking for a true or false.
290#
291# The following test will succeed:
292# assertTrue 0
293# assertTrue "[ 34 -gt 23 ]"
294# The folloing test will fail with a message:
295# assertTrue 123
296# assertTrue "test failed" "[ -r '/non/existant/file' ]"
297#
298# Args:
299# message: string: failure message [optional]
300# condition: string: integer value or shell conditional statement
301# Returns:
302# integer: success (TRUE/FALSE/ERROR constant)
kate.wardf51c6162008-06-17 16:38:35 +0000303assertTrue()
304{
kate.wardf32fb4f2008-07-11 15:20:38 +0000305 ${_SHUNIT_LINENO_}
306 if [ $# -gt 2 ]; then
kate.ward38097672008-11-03 20:58:42 +0000307 _shunit_error "assertTrue() takes one two arguments; $# given"
kate.wardf32fb4f2008-07-11 15:20:38 +0000308 return ${SHUNIT_ERROR}
309 fi
kate.wardf51c6162008-06-17 16:38:35 +0000310 _shunit_shouldSkip && return ${SHUNIT_TRUE}
311
kate.ward38097672008-11-03 20:58:42 +0000312 shunit_message_=${__shunit_lineno}
kate.wardf51c6162008-06-17 16:38:35 +0000313 if [ $# -eq 2 ]; then
kate.wardf32fb4f2008-07-11 15:20:38 +0000314 shunit_message_="${shunit_message_}$1"
kate.wardf51c6162008-06-17 16:38:35 +0000315 shift
316 fi
kate.wardf32fb4f2008-07-11 15:20:38 +0000317 shunit_condition_=$1
kate.wardf51c6162008-06-17 16:38:35 +0000318
319 # see if condition is an integer, i.e. a return value
kate.wardf32fb4f2008-07-11 15:20:38 +0000320 shunit_match_=`expr "${shunit_condition_}" : '\([0-9]*\)'`
321 shunit_return=${SHUNIT_TRUE}
322 if [ -z "${shunit_condition_}" ]; then
kate.wardf51c6162008-06-17 16:38:35 +0000323 # null condition
324 shunit_return=${SHUNIT_FALSE}
kate.wardf32fb4f2008-07-11 15:20:38 +0000325 elif [ "${shunit_condition_}" = "${shunit_match_}" ]; then
kate.wardf51c6162008-06-17 16:38:35 +0000326 # possible return value. treating 0 as true, and non-zero as false.
kate.wardf32fb4f2008-07-11 15:20:38 +0000327 [ ${shunit_condition_} -ne 0 ] && shunit_return=${SHUNIT_FALSE}
kate.wardf51c6162008-06-17 16:38:35 +0000328 else
329 # (hopefully) a condition
kate.wardf32fb4f2008-07-11 15:20:38 +0000330 ( eval ${shunit_condition_} ) >/dev/null 2>&1
kate.wardf51c6162008-06-17 16:38:35 +0000331 [ $? -ne 0 ] && shunit_return=${SHUNIT_FALSE}
332 fi
333
334 # record the test
335 if [ ${shunit_return} -eq ${SHUNIT_TRUE} ]; then
kate.ward38097672008-11-03 20:58:42 +0000336 _shunit_assertPass
kate.wardf51c6162008-06-17 16:38:35 +0000337 else
kate.ward38097672008-11-03 20:58:42 +0000338 _shunit_assertFail "${shunit_message_}"
kate.wardf51c6162008-06-17 16:38:35 +0000339 fi
340
kate.wardf32fb4f2008-07-11 15:20:38 +0000341 unset shunit_message_ shunit_condition_ shunit_match_
kate.wardf51c6162008-06-17 16:38:35 +0000342 return ${shunit_return}
343}
kate.wardf32fb4f2008-07-11 15:20:38 +0000344_ASSERT_TRUE_='eval assertTrue --lineno "${LINENO:-}"'
kate.wardf51c6162008-06-17 16:38:35 +0000345
kate.ward9a183b82010-03-18 00:25:34 +0000346# Assert that a value or shell test condition is false.
347#
348# In shell, a value of 0 is true and a non-zero value is false. Any integer
349# value passed can thereby be tested.
350#
351# Shell supports much more complicated tests though, and a means to support
352# them was needed. As such, this function tests that conditions are true or
353# false through evaluation rather than just looking for a true or false.
354#
355# The following test will succeed:
356# assertFalse 1
357# assertFalse "[ 'apples' = 'oranges' ]"
358# The folloing test will fail with a message:
359# assertFalse 0
360# assertFalse "test failed" "[ 1 -eq 1 -a 2 -eq 2 ]"
361#
362# Args:
363# message: string: failure message [optional]
364# condition: string: integer value or shell conditional statement
365# Returns:
366# integer: success (TRUE/FALSE/ERROR constant)
kate.wardf51c6162008-06-17 16:38:35 +0000367assertFalse()
368{
kate.wardf32fb4f2008-07-11 15:20:38 +0000369 ${_SHUNIT_LINENO_}
370 if [ $# -lt 1 -o $# -gt 2 ]; then
kate.ward38097672008-11-03 20:58:42 +0000371 _shunit_error "assertFalse() quires one or two arguments; $# given"
kate.wardf32fb4f2008-07-11 15:20:38 +0000372 return ${SHUNIT_ERROR}
373 fi
kate.wardf51c6162008-06-17 16:38:35 +0000374 _shunit_shouldSkip && return ${SHUNIT_TRUE}
375
kate.ward38097672008-11-03 20:58:42 +0000376 shunit_message_=${__shunit_lineno}
kate.wardf51c6162008-06-17 16:38:35 +0000377 if [ $# -eq 2 ]; then
kate.wardf32fb4f2008-07-11 15:20:38 +0000378 shunit_message_="${shunit_message_}$1"
kate.wardf51c6162008-06-17 16:38:35 +0000379 shift
380 fi
kate.wardf32fb4f2008-07-11 15:20:38 +0000381 shunit_condition_=$1
kate.wardf51c6162008-06-17 16:38:35 +0000382
383 # see if condition is an integer, i.e. a return value
kate.wardf32fb4f2008-07-11 15:20:38 +0000384 shunit_match_=`expr "${shunit_condition_}" : '\([0-9]*\)'`
385 shunit_return=${SHUNIT_TRUE}
386 if [ -z "${shunit_condition_}" ]; then
kate.wardf51c6162008-06-17 16:38:35 +0000387 # null condition
388 shunit_return=${SHUNIT_FALSE}
kate.wardf32fb4f2008-07-11 15:20:38 +0000389 elif [ "${shunit_condition_}" = "${shunit_match_}" ]; then
kate.wardf51c6162008-06-17 16:38:35 +0000390 # possible return value. treating 0 as true, and non-zero as false.
kate.wardf32fb4f2008-07-11 15:20:38 +0000391 [ ${shunit_condition_} -eq 0 ] && shunit_return=${SHUNIT_FALSE}
kate.wardf51c6162008-06-17 16:38:35 +0000392 else
393 # (hopefully) a condition
kate.wardf32fb4f2008-07-11 15:20:38 +0000394 ( eval ${shunit_condition_} ) >/dev/null 2>&1
kate.wardf51c6162008-06-17 16:38:35 +0000395 [ $? -eq 0 ] && shunit_return=${SHUNIT_FALSE}
396 fi
397
398 # record the test
399 if [ ${shunit_return} -eq ${SHUNIT_TRUE} ]; then
kate.ward38097672008-11-03 20:58:42 +0000400 _shunit_assertPass
kate.wardf51c6162008-06-17 16:38:35 +0000401 else
kate.ward38097672008-11-03 20:58:42 +0000402 _shunit_assertFail "${shunit_message_}"
kate.wardf51c6162008-06-17 16:38:35 +0000403 fi
404
kate.wardf32fb4f2008-07-11 15:20:38 +0000405 unset shunit_message_ shunit_condition_ shunit_match_
kate.wardf51c6162008-06-17 16:38:35 +0000406 return ${shunit_return}
407}
kate.wardf32fb4f2008-07-11 15:20:38 +0000408_ASSERT_FALSE_='eval assertFalse --lineno "${LINENO:-}"'
kate.wardf51c6162008-06-17 16:38:35 +0000409
410#-----------------------------------------------------------------------------
411# failure functions
412#
413
kate.ward9a183b82010-03-18 00:25:34 +0000414# Records a test failure.
415#
416# Args:
417# message: string: failure message [optional]
418# Returns:
419# integer: success (TRUE/FALSE/ERROR constant)
kate.wardf51c6162008-06-17 16:38:35 +0000420fail()
421{
kate.wardf32fb4f2008-07-11 15:20:38 +0000422 ${_SHUNIT_LINENO_}
423 if [ $# -gt 1 ]; then
kate.ward9a183b82010-03-18 00:25:34 +0000424 _shunit_error "fail() requires zero or one arguments; $# given"
kate.wardf32fb4f2008-07-11 15:20:38 +0000425 return ${SHUNIT_ERROR}
426 fi
kate.wardf51c6162008-06-17 16:38:35 +0000427 _shunit_shouldSkip && return ${SHUNIT_TRUE}
428
kate.ward38097672008-11-03 20:58:42 +0000429 shunit_message_=${__shunit_lineno}
kate.wardf32fb4f2008-07-11 15:20:38 +0000430 if [ $# -eq 1 ]; then
431 shunit_message_="${shunit_message_}$1"
432 shift
433 fi
kate.wardf51c6162008-06-17 16:38:35 +0000434
kate.ward38097672008-11-03 20:58:42 +0000435 _shunit_assertFail "${shunit_message_}"
kate.wardf51c6162008-06-17 16:38:35 +0000436
kate.wardf32fb4f2008-07-11 15:20:38 +0000437 unset shunit_message_
438 return ${SHUNIT_FALSE}
kate.wardf51c6162008-06-17 16:38:35 +0000439}
kate.wardf32fb4f2008-07-11 15:20:38 +0000440_FAIL_='eval fail --lineno "${LINENO:-}"'
kate.wardf51c6162008-06-17 16:38:35 +0000441
kate.ward9a183b82010-03-18 00:25:34 +0000442# Records a test failure, stating two values were not equal.
443#
444# Args:
445# message: string: failure message [optional]
446# unexpected: string: unexpected value
447# actual: string: actual value
448# Returns:
449# integer: success (TRUE/FALSE/ERROR constant)
kate.wardf51c6162008-06-17 16:38:35 +0000450failNotEquals()
451{
kate.wardf32fb4f2008-07-11 15:20:38 +0000452 ${_SHUNIT_LINENO_}
453 if [ $# -lt 2 -o $# -gt 3 ]; then
kate.ward38097672008-11-03 20:58:42 +0000454 _shunit_error "failNotEquals() requires one or two arguments; $# given"
kate.wardf32fb4f2008-07-11 15:20:38 +0000455 return ${SHUNIT_ERROR}
456 fi
kate.wardf51c6162008-06-17 16:38:35 +0000457 _shunit_shouldSkip && return ${SHUNIT_TRUE}
458
kate.ward38097672008-11-03 20:58:42 +0000459 shunit_message_=${__shunit_lineno}
kate.wardf51c6162008-06-17 16:38:35 +0000460 if [ $# -eq 3 ]; then
kate.wardf32fb4f2008-07-11 15:20:38 +0000461 shunit_message_="${shunit_message_}$1"
kate.wardf51c6162008-06-17 16:38:35 +0000462 shift
463 fi
kate.wardf32fb4f2008-07-11 15:20:38 +0000464 shunit_unexpected_=$1
465 shunit_actual_=$2
kate.wardf51c6162008-06-17 16:38:35 +0000466
kate.ward38097672008-11-03 20:58:42 +0000467 _shunit_assertFail "${shunit_message_:+${shunit_message_} }expected:<${shunit_unexpected_}> but was:<${shunit_actual_}>"
kate.wardf51c6162008-06-17 16:38:35 +0000468
kate.wardf32fb4f2008-07-11 15:20:38 +0000469 unset shunit_message_ shunit_unexpected_ shunit_actual_
470 return ${SHUNIT_FALSE}
kate.wardf51c6162008-06-17 16:38:35 +0000471}
kate.wardf32fb4f2008-07-11 15:20:38 +0000472_FAIL_NOT_EQUALS_='eval failNotEquals --lineno "${LINENO:-}"'
kate.wardf51c6162008-06-17 16:38:35 +0000473
kate.ward9a183b82010-03-18 00:25:34 +0000474# Records a test failure, stating two values should have been the same.
475#
476# Args:
477# message: string: failure message [optional]
478# expected: string: expected value
479# actual: string: actual value
480# Returns:
481# integer: success (TRUE/FALSE/ERROR constant)
kate.wardf51c6162008-06-17 16:38:35 +0000482failSame()
483{
kate.wardf32fb4f2008-07-11 15:20:38 +0000484 ${_SHUNIT_LINENO_}
485 if [ $# -lt 2 -o $# -gt 3 ]; then
kate.ward38097672008-11-03 20:58:42 +0000486 _shunit_error "failSame() requires two or three arguments; $# given"
kate.wardf32fb4f2008-07-11 15:20:38 +0000487 return ${SHUNIT_ERROR}
488 fi
kate.wardf51c6162008-06-17 16:38:35 +0000489 _shunit_shouldSkip && return ${SHUNIT_TRUE}
490
kate.ward38097672008-11-03 20:58:42 +0000491 shunit_message_=${__shunit_lineno}
kate.wardf32fb4f2008-07-11 15:20:38 +0000492 if [ $# -eq 3 ]; then
493 shunit_message_="${shunit_message_}$1"
494 shift
495 fi
kate.wardf51c6162008-06-17 16:38:35 +0000496
kate.ward38097672008-11-03 20:58:42 +0000497 _shunit_assertFail "${shunit_message_:+${shunit_message_} }expected not same"
kate.wardf51c6162008-06-17 16:38:35 +0000498
kate.wardf32fb4f2008-07-11 15:20:38 +0000499 unset shunit_message_
500 return ${SHUNIT_FALSE}
kate.wardf51c6162008-06-17 16:38:35 +0000501}
kate.wardf32fb4f2008-07-11 15:20:38 +0000502_FAIL_SAME_='eval failSame --lineno "${LINENO:-}"'
kate.wardf51c6162008-06-17 16:38:35 +0000503
kate.ward9a183b82010-03-18 00:25:34 +0000504# Records a test failure, stating two values were not equal.
505#
506# This is functionally equivalent to calling failNotEquals().
507#
508# Args:
509# message: string: failure message [optional]
510# unexpected: string: unexpected value
511# actual: string: actual value
512# Returns:
513# integer: success (TRUE/FALSE/ERROR constant)
kate.wardf51c6162008-06-17 16:38:35 +0000514failNotSame()
515{
kate.wardf32fb4f2008-07-11 15:20:38 +0000516 ${_SHUNIT_LINENO_}
517 if [ $# -lt 2 -o $# -gt 3 ]; then
kate.ward38097672008-11-03 20:58:42 +0000518 _shunit_error "failNotEquals() requires one or two arguments; $# given"
kate.wardf32fb4f2008-07-11 15:20:38 +0000519 return ${SHUNIT_ERROR}
520 fi
521 _shunit_shouldSkip && return ${SHUNIT_TRUE}
522
kate.ward38097672008-11-03 20:58:42 +0000523 shunit_message_=${__shunit_lineno}
524 if [ $# -eq 3 ]; then
525 shunit_message_="${shunit_message_}$1"
526 shift
kate.wardf32fb4f2008-07-11 15:20:38 +0000527 fi
kate.ward38097672008-11-03 20:58:42 +0000528 failNotEquals "${shunit_message_}" "$1" "$2"
529 shunit_return=$?
530
531 unset shunit_message_
532 return ${shunit_return}
kate.wardf51c6162008-06-17 16:38:35 +0000533}
kate.wardf32fb4f2008-07-11 15:20:38 +0000534_FAIL_NOT_SAME_='eval failNotSame --lineno "${LINENO:-}"'
kate.wardf51c6162008-06-17 16:38:35 +0000535
536#-----------------------------------------------------------------------------
537# skipping functions
538#
539
kate.ward9a183b82010-03-18 00:25:34 +0000540# Force remaining assert and fail functions to be "skipped".
541#
542# This function forces the remaining assert and fail functions to be "skipped",
543# i.e. they will have no effect. Each function skipped will be recorded so that
544# the total of asserts and fails will not be altered.
545#
546# Args:
547# None
kate.wardf51c6162008-06-17 16:38:35 +0000548startSkipping()
549{
550 __shunit_skip=${SHUNIT_TRUE}
551}
552
kate.ward9a183b82010-03-18 00:25:34 +0000553# Resume the normal recording behavior of assert and fail calls.
554#
555# Args:
556# None
kate.wardf51c6162008-06-17 16:38:35 +0000557endSkipping()
558{
559 __shunit_skip=${SHUNIT_FALSE}
560}
561
kate.ward9a183b82010-03-18 00:25:34 +0000562# Returns the state of assert and fail call skipping.
563#
564# Args:
565# None
566# Returns:
567# boolean: (TRUE/FALSE constant)
kate.wardf51c6162008-06-17 16:38:35 +0000568isSkipping()
569{
570 return ${__shunit_skip}
571}
572
573#-----------------------------------------------------------------------------
574# suite functions
575#
576
kate.ward9a183b82010-03-18 00:25:34 +0000577# Stub. This function should contains all unit test calls to be made.
578#
579# DEPRECATED (as of 2.1.0)
580#
581# This function can be optionally overridden by the user in their test suite.
582#
583# If this function exists, it will be called when shunit2 is sourced. If it
584# does not exist, shunit2 will search the parent script for all functions
585# beginning with the word 'test', and they will be added dynamically to the
586# test suite.
587#
588# This function should be overridden by the user in their unit test suite.
kate.wardf51c6162008-06-17 16:38:35 +0000589# Note: see _shunit_mktempFunc() for actual implementation
kate.ward9a183b82010-03-18 00:25:34 +0000590#
591# Args:
592# None
593#suite() { :; } # DO NOT UNCOMMENT THIS FUNCTION
kate.wardf51c6162008-06-17 16:38:35 +0000594
kate.ward9a183b82010-03-18 00:25:34 +0000595# Adds a function name to the list of tests schedule for execution.
596#
597# This function should only be called from within the suite() function.
598#
599# Args:
600# function: string: name of a function to add to current unit test suite
kate.wardf51c6162008-06-17 16:38:35 +0000601suite_addTest()
602{
kate.ward38097672008-11-03 20:58:42 +0000603 shunit_func_=${1:-}
kate.wardf51c6162008-06-17 16:38:35 +0000604
kate.ward38097672008-11-03 20:58:42 +0000605 __shunit_suite="${__shunit_suite:+${__shunit_suite} }${shunit_func_}"
606 __shunit_testsTotal=`expr ${__shunit_testsTotal} + 1`
kate.wardf51c6162008-06-17 16:38:35 +0000607
kate.ward38097672008-11-03 20:58:42 +0000608 unset shunit_func_
kate.wardf51c6162008-06-17 16:38:35 +0000609}
610
kate.ward9a183b82010-03-18 00:25:34 +0000611# Stub. This function will be called once before any tests are run.
612#
613# Common one-time environment preparation tasks shared by all tests can be
614# defined here.
615#
616# This function should be overridden by the user in their unit test suite.
kate.wardf51c6162008-06-17 16:38:35 +0000617# Note: see _shunit_mktempFunc() for actual implementation
kate.ward9a183b82010-03-18 00:25:34 +0000618#
619# Args:
620# None
621#oneTimeSetUp() { :; } # DO NOT UNCOMMENT THIS FUNCTION
kate.wardf51c6162008-06-17 16:38:35 +0000622
kate.ward9a183b82010-03-18 00:25:34 +0000623# Stub. This function will be called once after all tests are finished.
624#
625# Common one-time environment cleanup tasks shared by all tests can be defined
626# here.
627#
628# This function should be overridden by the user in their unit test suite.
kate.wardf51c6162008-06-17 16:38:35 +0000629# Note: see _shunit_mktempFunc() for actual implementation
kate.ward9a183b82010-03-18 00:25:34 +0000630#
631# Args:
632# None
633#oneTimeTearDown() { :; } # DO NOT UNCOMMENT THIS FUNCTION
kate.wardf51c6162008-06-17 16:38:35 +0000634
kate.ward9a183b82010-03-18 00:25:34 +0000635# Stub. This function will be called before each test is run.
636#
637# Common environment preparation tasks shared by all tests can be defined here.
638#
639# This function should be overridden by the user in their unit test suite.
kate.wardf51c6162008-06-17 16:38:35 +0000640# Note: see _shunit_mktempFunc() for actual implementation
kate.ward9a183b82010-03-18 00:25:34 +0000641#
642# Args:
643# None
644#setUp() { :; }
kate.wardf51c6162008-06-17 16:38:35 +0000645
kate.wardf51c6162008-06-17 16:38:35 +0000646# Note: see _shunit_mktempFunc() for actual implementation
kate.ward9a183b82010-03-18 00:25:34 +0000647# Stub. This function will be called after each test is run.
648#
649# Common environment cleanup tasks shared by all tests can be defined here.
650#
651# This function should be overridden by the user in their unit test suite.
652# Note: see _shunit_mktempFunc() for actual implementation
653#
654# Args:
655# None
656#tearDown() { :; } # DO NOT UNCOMMENT THIS FUNCTION
kate.wardf51c6162008-06-17 16:38:35 +0000657
658#------------------------------------------------------------------------------
659# internal shUnit2 functions
660#
661
kate.ward9a183b82010-03-18 00:25:34 +0000662# Create a temporary directory to store various run-time files in.
663#
664# This function is a cross-platform temporary directory creation tool. Not all
kate.wardf51c6162008-06-17 16:38:35 +0000665# OSes have the mktemp function, so one is included here.
kate.ward9a183b82010-03-18 00:25:34 +0000666#
667# Args:
668# None
669# Outputs:
670# string: the temporary directory that was created
kate.wardf51c6162008-06-17 16:38:35 +0000671_shunit_mktempDir()
672{
673 # try the standard mktemp function
674 ( exec mktemp -dqt shunit.XXXXXX 2>/dev/null ) && return
675
676 # the standard mktemp didn't work. doing our own.
kate.ward9a183b82010-03-18 00:25:34 +0000677 if [ -r '/dev/urandom' -a -x '/usr/bin/od' ]; then
678 _shunit_random_=`/usr/bin/od -vAn -N4 -tx4 </dev/urandom \
679 |sed 's/^[^0-9a-f]*//'`
kate.wardf51c6162008-06-17 16:38:35 +0000680 elif [ -n "${RANDOM:-}" ]; then
681 # $RANDOM works
kate.ward38097672008-11-03 20:58:42 +0000682 _shunit_random_=${RANDOM}${RANDOM}${RANDOM}$$
kate.wardf51c6162008-06-17 16:38:35 +0000683 else
684 # $RANDOM doesn't work
kate.ward38097672008-11-03 20:58:42 +0000685 _shunit_date_=`date '+%Y%m%d%H%M%S'`
686 _shunit_random_=`expr ${_shunit_date_} / $$`
kate.wardf51c6162008-06-17 16:38:35 +0000687 fi
688
kate.ward38097672008-11-03 20:58:42 +0000689 _shunit_tmpDir_="${TMPDIR:-/tmp}/shunit.${_shunit_random_}"
690 ( umask 077 && mkdir "${_shunit_tmpDir_}" ) || {
691 _shunit_fatal 'could not create temporary directory! exiting'
kate.ward9a183b82010-03-18 00:25:34 +0000692 return ${SHUNIT_ERROR}
kate.wardf51c6162008-06-17 16:38:35 +0000693 }
694
kate.ward38097672008-11-03 20:58:42 +0000695 echo ${_shunit_tmpDir_}
696 unset _shunit_date_ _shunit_random_ _shunit_tmpDir_
kate.wardf51c6162008-06-17 16:38:35 +0000697}
698
kate.ward9a183b82010-03-18 00:25:34 +0000699# This function is here to work around issues in Cygwin
700#
701# Args:
702# None
kate.wardf51c6162008-06-17 16:38:35 +0000703_shunit_mktempFunc()
704{
kate.ward38097672008-11-03 20:58:42 +0000705 for _shunit_func_ in oneTimeSetUp oneTimeTearDown setUp tearDown suite; do
706 _shunit_file_="${__shunit_tmpDir}/${_shunit_func_}"
707 cat <<EOF >"${_shunit_file_}"
kate.wardf51c6162008-06-17 16:38:35 +0000708#! /bin/sh
kate.ward38097672008-11-03 20:58:42 +0000709exit ${SHUNIT_TRUE}
kate.wardf51c6162008-06-17 16:38:35 +0000710EOF
kate.ward38097672008-11-03 20:58:42 +0000711 chmod +x "${_shunit_file_}"
kate.wardf51c6162008-06-17 16:38:35 +0000712 done
713
kate.ward38097672008-11-03 20:58:42 +0000714 unset _shunit_file_
715}
716
kate.ward9a183b82010-03-18 00:25:34 +0000717# Final cleanup function to leave things as we found them.
718#
719# Besides removing the temporary directory, this function is in charge of the
720# final exit code of the unit test. The exit code is based on how the script
721# was ended (e.g. normal exit, or via Ctrl-C).
722#
723# Args:
724# name: string: name of the trap called (specified when trap defined)
kate.ward38097672008-11-03 20:58:42 +0000725_shunit_cleanup()
726{
727 _shunit_name_=$1
728
729 case ${_shunit_name_} in
730 EXIT) _shunit_signal_=0 ;;
731 INT) _shunit_signal_=2 ;;
732 TERM) _shunit_signal_=15 ;;
733 *)
734 _shunit_warn "unrecognized trap value (${_shunit_name_})"
735 _shunit_signal_=0
736 ;;
737 esac
738
739 # do our work
740 rm -fr "${__shunit_tmpDir}"
741
742 # exit for all non-EXIT signals
743 if [ ${_shunit_name_} != 'EXIT' ]; then
744 _shunit_warn "trapped and now handling the (${_shunit_name_}) signal"
745 # disable EXIT trap
746 trap 0
747 # add 128 to signal and exit
748 exit `expr ${_shunit_signal_} + 128`
749 elif [ ${__shunit_reportGenerated} -eq ${SHUNIT_FALSE} ] ; then
750 _shunit_assertFail 'Unknown failure encountered running a test'
751 _shunit_generateReport
752 exit ${SHUNIT_ERROR}
753 fi
754
755 unset _shunit_name_ _shunit_signal_
756}
757
758# The actual running of the tests happens here.
kate.ward9a183b82010-03-18 00:25:34 +0000759#
760# Args:
761# None
kate.ward38097672008-11-03 20:58:42 +0000762_shunit_execSuite()
763{
764 for _shunit_test_ in ${__shunit_suite}; do
765 __shunit_testSuccess=${SHUNIT_TRUE}
766
767 # disable skipping
768 endSkipping
769
770 # execute the per-test setup function
771 setUp
772
773 # execute the test
774 echo "${_shunit_test_}"
775 eval ${_shunit_test_}
776
777 # execute the per-test tear-down function
778 tearDown
779
780 # update stats
781 if [ ${__shunit_testSuccess} -eq ${SHUNIT_TRUE} ]; then
782 __shunit_testsPassed=`expr ${__shunit_testsPassed} + 1`
783 else
784 __shunit_testsFailed=`expr ${__shunit_testsFailed} + 1`
785 fi
786 done
787
788 unset _shunit_test_
789}
790
kate.ward9a183b82010-03-18 00:25:34 +0000791# Generates the user friendly report with appropriate OK/FAILED message.
792#
793# Args:
794# None
795# Output:
796# string: the report of successful and failed tests, as well as totals.
kate.ward38097672008-11-03 20:58:42 +0000797_shunit_generateReport()
798{
799 _shunit_ok_=${SHUNIT_TRUE}
800
801 # if no exit code was provided one, determine an appropriate one
802 [ ${__shunit_testsFailed} -gt 0 \
803 -o ${__shunit_testSuccess} -eq ${SHUNIT_FALSE} ] \
804 && _shunit_ok_=${SHUNIT_FALSE}
805
806 echo
807 if [ ${__shunit_testsTotal} -eq 1 ]; then
808 echo "Ran ${__shunit_testsTotal} test."
809 else
810 echo "Ran ${__shunit_testsTotal} tests."
811 fi
812
813 _shunit_failures_=''
814 _shunit_skipped_=''
815 [ ${__shunit_assertsFailed} -gt 0 ] \
816 && _shunit_failures_="failures=${__shunit_assertsFailed}"
817 [ ${__shunit_assertsSkipped} -gt 0 ] \
818 && _shunit_skipped_="skipped=${__shunit_assertsSkipped}"
819
820 if [ ${_shunit_ok_} -eq ${SHUNIT_TRUE} ]; then
821 _shunit_msg_='OK'
822 [ -n "${_shunit_skipped_}" ] \
823 && _shunit_msg_="${_shunit_msg_} (${_shunit_skipped_})"
824 else
825 _shunit_msg_="FAILED (${_shunit_failures_}"
826 [ -n "${_shunit_skipped_}" ] \
827 && _shunit_msg_="${_shunit_msg_},${_shunit_skipped_}"
828 _shunit_msg_="${_shunit_msg_})"
829 fi
830
831 echo
832 echo ${_shunit_msg_}
833 __shunit_reportGenerated=${SHUNIT_TRUE}
834
835 unset _shunit_failures_ _shunit_msg_ _shunit_ok_ _shunit_skipped_
kate.wardf51c6162008-06-17 16:38:35 +0000836}
837
kate.ward9a183b82010-03-18 00:25:34 +0000838# Test for whether a function should be skipped.
839#
840# Args:
841# None
842# Returns:
843# boolean: whether the test should be skipped (TRUE/FALSE constant)
kate.wardf51c6162008-06-17 16:38:35 +0000844_shunit_shouldSkip()
845{
846 [ ${__shunit_skip} -eq ${SHUNIT_FALSE} ] && return ${SHUNIT_FALSE}
kate.ward38097672008-11-03 20:58:42 +0000847 _shunit_assertSkip
kate.wardf51c6162008-06-17 16:38:35 +0000848}
849
kate.ward9a183b82010-03-18 00:25:34 +0000850# Records a successful test.
851#
852# Args:
853# None
kate.ward38097672008-11-03 20:58:42 +0000854_shunit_assertPass()
kate.wardf51c6162008-06-17 16:38:35 +0000855{
kate.ward38097672008-11-03 20:58:42 +0000856 __shunit_assertsPassed=`expr ${__shunit_assertsPassed} + 1`
857 __shunit_assertsTotal=`expr ${__shunit_assertsTotal} + 1`
kate.wardf51c6162008-06-17 16:38:35 +0000858}
859
kate.ward9a183b82010-03-18 00:25:34 +0000860# Records a test failure.
861#
862# Args:
863# message: string: failure message to provide user
kate.ward38097672008-11-03 20:58:42 +0000864_shunit_assertFail()
kate.wardf51c6162008-06-17 16:38:35 +0000865{
kate.ward38097672008-11-03 20:58:42 +0000866 _shunit_msg_=$1
kate.wardf51c6162008-06-17 16:38:35 +0000867
kate.ward38097672008-11-03 20:58:42 +0000868 __shunit_testSuccess=${SHUNIT_FALSE}
869 __shunit_assertsFailed=`expr ${__shunit_assertsFailed} + 1`
870 __shunit_assertsTotal=`expr ${__shunit_assertsTotal} + 1`
871 echo "${__SHUNIT_ASSERT_MSG_PREFIX}${_shunit_msg_}"
kate.wardf51c6162008-06-17 16:38:35 +0000872
kate.ward38097672008-11-03 20:58:42 +0000873 unset _shunit_msg_
kate.wardf51c6162008-06-17 16:38:35 +0000874}
875
kate.ward9a183b82010-03-18 00:25:34 +0000876# Records a skipped test.
877#
878# Args:
879# None
kate.ward38097672008-11-03 20:58:42 +0000880_shunit_assertSkip()
kate.wardf51c6162008-06-17 16:38:35 +0000881{
kate.ward38097672008-11-03 20:58:42 +0000882 __shunit_assertsSkipped=`expr ${__shunit_assertsSkipped} + 1`
883 __shunit_assertsTotal=`expr ${__shunit_assertsTotal} + 1`
kate.wardf51c6162008-06-17 16:38:35 +0000884}
885
886#------------------------------------------------------------------------------
887# main
888#
889
890# create a temporary storage location
kate.ward9a183b82010-03-18 00:25:34 +0000891__shunit_tmpDir=`_shunit_mktempDir` || exit ${SHUNIT_ERROR}
kate.wardf51c6162008-06-17 16:38:35 +0000892
kate.ward38097672008-11-03 20:58:42 +0000893# provide a public temporary directory for unit test scripts
894# TODO(kward): document this
895shunit_tmpDir="${__shunit_tmpDir}/tmp"
896mkdir "${shunit_tmpDir}"
897
kate.wardf51c6162008-06-17 16:38:35 +0000898# setup traps to clean up after ourselves
899trap '_shunit_cleanup EXIT' 0
900trap '_shunit_cleanup INT' 2
901trap '_shunit_cleanup TERM' 15
902
903# create phantom functions to work around issues with Cygwin
904_shunit_mktempFunc
905PATH="${__shunit_tmpDir}:${PATH}"
906
907# execute the oneTimeSetUp function (if it exists)
kate.wardf51c6162008-06-17 16:38:35 +0000908oneTimeSetUp
909
910# execute the suite function defined in the parent test script
911# deprecated as of 2.1.0
912suite
913
914# if no suite function was defined, dynamically build a list of functions
915if [ -z "${__shunit_suite}" ]; then
kate.wardf32fb4f2008-07-11 15:20:38 +0000916 shunit_funcs_=`grep "^[ \t]*test[A-Za-z0-9_]* *()" ${__SHUNIT_PARENT} \
kate.wardf51c6162008-06-17 16:38:35 +0000917 |sed 's/[^A-Za-z0-9_]//g'`
kate.wardf32fb4f2008-07-11 15:20:38 +0000918 for shunit_func_ in ${shunit_funcs_}; do
919 suite_addTest ${shunit_func_}
kate.wardf51c6162008-06-17 16:38:35 +0000920 done
921fi
kate.wardf32fb4f2008-07-11 15:20:38 +0000922unset shunit_func_ shunit_funcs_
kate.wardf51c6162008-06-17 16:38:35 +0000923
924# execute the tests
925_shunit_execSuite
926
927# execute the oneTimeTearDown function (if it exists)
928oneTimeTearDown
929
kate.ward38097672008-11-03 20:58:42 +0000930# generate the report
kate.wardf51c6162008-06-17 16:38:35 +0000931_shunit_generateReport
932
kate.ward38097672008-11-03 20:58:42 +0000933# that's it folks
934[ ${__shunit_testsFailed} -eq 0 ]
935exit $?