blob: d900a7014aba4a29765e9e57a003ebeeb87f3736 [file] [log] [blame]
kate.ward38097672008-11-03 20:58:42 +00001# $Id: shunit2 277 2008-10-29 21:20:22Z kate.ward@forestent.com $
kate.wardf51c6162008-06-17 16:38:35 +00002# vim:et:ft=sh:sts=2:sw=2
3# vim:foldmethod=marker:foldmarker=/**,*/
4#
5#/**
6# <?xml version="1.0" encoding="UTF-8"?>
7# <s:shelldoc xmlns:s="http://www.forestent.com/projects/shelldoc/xsl/2005.0">
8# <s:header>
kate.ward38097672008-11-03 20:58:42 +00009# shUnit 2.1.5
kate.wardf51c6162008-06-17 16:38:35 +000010# Shell Unit Test Framework
11#
kate.ward38097672008-11-03 20:58:42 +000012# http://shunit2.googlecode.com/
kate.wardf51c6162008-06-17 16:38:35 +000013#
14# written by Kate Ward &lt;kate.ward@forestent.com&gt;
15# released under the LGPL
16#
kate.ward38097672008-11-03 20:58:42 +000017# This module implements a xUnit based unit test framework similar to JUnit.
kate.wardf51c6162008-06-17 16:38:35 +000018# </s:header>
19#*/
20
kate.ward38097672008-11-03 20:58:42 +000021SHUNIT_VERSION='2.1.5'
kate.wardf51c6162008-06-17 16:38:35 +000022
23SHUNIT_TRUE=0
24SHUNIT_FALSE=1
25SHUNIT_ERROR=2
26
kate.ward38097672008-11-03 20:58:42 +000027_shunit_warn() { echo "shunit2:WARN $@" >&2; }
28_shunit_error() { echo "shunit2:ERROR $@" >&2; }
29_shunit_fatal() { echo "shunit2:FATAL $@" >&2; }
30
kate.wardf51c6162008-06-17 16:38:35 +000031# specific shell checks
32if [ -n "${ZSH_VERSION:-}" ]; then
kate.wardf32fb4f2008-07-11 15:20:38 +000033 setopt |grep "^shwordsplit$" >/dev/null
34 if [ $? -ne ${SHUNIT_TRUE} ]; then
35 _shunit_fatal 'zsh shwordsplit option is required for proper operation'
kate.wardf51c6162008-06-17 16:38:35 +000036 exit ${SHUNIT_ERROR}
37 fi
38 if [ -z "${SHUNIT_PARENT:-}" ]; then
39 _shunit_fatal "zsh does not pass \$0 through properly. please declare \
40\"SHUNIT_PARENT=\$0\" before calling shUnit2"
41 exit ${SHUNIT_ERROR}
42 fi
43fi
44
kate.wardf51c6162008-06-17 16:38:35 +000045#
46# constants
47#
48
49__SHUNIT_ASSERT_MSG_PREFIX='ASSERT:'
50__SHUNIT_PARENT=${SHUNIT_PARENT:-$0}
51
52# set the constants readonly
kate.ward38097672008-11-03 20:58:42 +000053shunit_constants_=`set |grep '^__SHUNIT_' |cut -d= -f1`
54echo "${shunit_constants_}" |grep '^Binary file' >/dev/null \
55 && shunit_constants_=`set |grep -a '^__SHUNIT_' |cut -d= -f1`
56for shunit_constant_ in ${shunit_constants_}; do
kate.wardf32fb4f2008-07-11 15:20:38 +000057 shunit_ro_opts_=''
kate.ward38097672008-11-03 20:58:42 +000058 case ${ZSH_VERSION:-} in
59 '') ;; # this isn't zsh
60 [123].*) ;; # early versions (1.x, 2.x, 3.x)
61 *) shunit_ro_opts_='-g' ;; # all later versions. declare readonly globally
62 esac
63 readonly ${shunit_ro_opts_} ${shunit_constant_}
kate.wardf51c6162008-06-17 16:38:35 +000064done
kate.ward38097672008-11-03 20:58:42 +000065unset shunit_constant_ shunit_constants_ shunit_ro_opts_
kate.wardf51c6162008-06-17 16:38:35 +000066
67# variables
68__shunit_skip=${SHUNIT_FALSE}
69__shunit_suite=''
70
kate.ward38097672008-11-03 20:58:42 +000071# counts of tests
72__shunit_testSuccess=${SHUNIT_TRUE}
73__shunit_testsTotal=0
kate.wardf51c6162008-06-17 16:38:35 +000074__shunit_testsPassed=0
75__shunit_testsFailed=0
kate.ward38097672008-11-03 20:58:42 +000076
77# counts of asserts
78__shunit_assertsTotal=0
79__shunit_assertsPassed=0
80__shunit_assertsFailed=0
81__shunit_assertsSkipped=0
82
83__shunit_lineno=''
84__shunit_reportGenerated=${SHUNIT_FALSE}
kate.wardf51c6162008-06-17 16:38:35 +000085
kate.wardf32fb4f2008-07-11 15:20:38 +000086# macros
kate.ward38097672008-11-03 20:58:42 +000087_SHUNIT_LINENO_='eval __shunit_lineno=""; if [ "${1:-}" = "--lineno" ]; then [ -n "$2" ] && __shunit_lineno="[$2] "; shift 2; fi'
kate.wardf32fb4f2008-07-11 15:20:38 +000088
kate.wardf51c6162008-06-17 16:38:35 +000089#-----------------------------------------------------------------------------
90# assert functions
91#
92
93#/**
94# <s:function group="asserts">
95# <entry align="right">
96# <emphasis>void</emphasis>
97# </entry>
98# <entry>
99# <funcsynopsis>
100# <funcprototype>
101# <funcdef><function>assertEquals</function></funcdef>
102# <paramdef>string <parameter>[message]</parameter></paramdef>
103# <paramdef>string <parameter>expected</parameter></paramdef>
104# <paramdef>string <parameter>actual</parameter></paramdef>
105# </funcprototype>
106# </funcsynopsis>
107# <para>Asserts that <emphasis>expected</emphasis> and
108# <emphasis>actual</emphasis> are equal to one another. The message is
109# optional.</para>
110# </entry>
111# </s:function>
112#*/
113assertEquals()
114{
kate.wardf32fb4f2008-07-11 15:20:38 +0000115 ${_SHUNIT_LINENO_}
116 if [ $# -lt 2 -o $# -gt 3 ]; then
kate.ward38097672008-11-03 20:58:42 +0000117 _shunit_error "assertEquals() requires two or three arguments; $# given"
118 _shunit_error "1: ${1:+$1} 2: ${2:+$2} 3: ${3:+$3}"
kate.wardf32fb4f2008-07-11 15:20:38 +0000119 return ${SHUNIT_ERROR}
120 fi
kate.wardf51c6162008-06-17 16:38:35 +0000121 _shunit_shouldSkip && return ${SHUNIT_TRUE}
122
kate.ward38097672008-11-03 20:58:42 +0000123 shunit_message_=${__shunit_lineno}
kate.wardf32fb4f2008-07-11 15:20:38 +0000124 if [ $# -eq 3 ]; then
125 shunit_message_="${shunit_message_}$1"
kate.wardf51c6162008-06-17 16:38:35 +0000126 shift
127 fi
kate.wardf32fb4f2008-07-11 15:20:38 +0000128 shunit_expected_=$1
129 shunit_actual_=$2
kate.wardf51c6162008-06-17 16:38:35 +0000130
131 shunit_return=${SHUNIT_TRUE}
kate.wardf32fb4f2008-07-11 15:20:38 +0000132 if [ "${shunit_expected_}" = "${shunit_actual_}" ]; then
kate.ward38097672008-11-03 20:58:42 +0000133 _shunit_assertPass
kate.wardf51c6162008-06-17 16:38:35 +0000134 else
kate.wardf32fb4f2008-07-11 15:20:38 +0000135 failNotEquals "${shunit_message_}" "${shunit_expected_}" "${shunit_actual_}"
kate.wardf51c6162008-06-17 16:38:35 +0000136 shunit_return=${SHUNIT_FALSE}
137 fi
138
kate.ward38097672008-11-03 20:58:42 +0000139 unset shunit_message_ shunit_expected_ shunit_actual_
kate.wardf51c6162008-06-17 16:38:35 +0000140 return ${shunit_return}
141}
kate.wardf32fb4f2008-07-11 15:20:38 +0000142_ASSERT_EQUALS_='eval assertEquals --lineno "${LINENO:-}"'
kate.wardf51c6162008-06-17 16:38:35 +0000143
144#/**
145# <s:function group="asserts">
146# <entry align="right">
147# <emphasis>void</emphasis>
148# </entry>
149# <entry>
150# <funcsynopsis>
151# <funcprototype>
kate.ward38097672008-11-03 20:58:42 +0000152# <funcdef><function>assertNotEquals</function></funcdef>
153# <paramdef>string <parameter>[message]</parameter></paramdef>
154# <paramdef>string <parameter>unexpected</parameter></paramdef>
155# <paramdef>string <parameter>actual</parameter></paramdef>
156# </funcprototype>
157# </funcsynopsis>
158# <para>Asserts that <emphasis>unexpected</emphasis> and
159# <emphasis>actual</emphasis> are <emphasis role="strong">not</emphasis>
160# equal to one another. The message is optional.</para>
161# </entry>
162# </s:function>
163#*/
164assertNotEquals()
165{
166 ${_SHUNIT_LINENO_}
167 if [ $# -lt 2 -o $# -gt 3 ]; then
168 _shunit_error "assertNotEquals() requires two or three arguments; $# given"
169 return ${SHUNIT_ERROR}
170 fi
171 _shunit_shouldSkip && return ${SHUNIT_TRUE}
172
173 shunit_message_=${__shunit_lineno}
174 if [ $# -eq 3 ]; then
175 shunit_message_="${shunit_message_}$1"
176 shift
177 fi
178 shunit_unexpected_=$1
179 shunit_actual_=$2
180
181 shunit_return=${SHUNIT_TRUE}
182 if [ "${shunit_unexpected_}" != "${shunit_actual_}" ]; then
183 _shunit_assertPass
184 else
185 failSame "${shunit_message_}" "$@"
186 shunit_return=${SHUNIT_FALSE}
187 fi
188
189 unset shunit_message_ shunit_unexpected_ shunit_actual_
190 return ${shunit_return}
191}
192_ASSERT_NOT_EQUALS_='eval assertNotEquals --lineno "${LINENO:-}"'
193
194#/**
195# <s:function group="asserts">
196# <entry align="right">
197# <emphasis>void</emphasis>
198# </entry>
199# <entry>
200# <funcsynopsis>
201# <funcprototype>
kate.wardf51c6162008-06-17 16:38:35 +0000202# <funcdef><function>assertNull</function></funcdef>
203# <paramdef>string <parameter>[message]</parameter></paramdef>
204# <paramdef>string <parameter>value</parameter></paramdef>
205# </funcprototype>
206# </funcsynopsis>
207# <para>Asserts that <emphasis>value</emphasis> is <literal>null</literal>,
208# or in shell terms a zero-length string. The message is optional.</para>
209# </entry>
210# </s:function>
211#*/
212assertNull()
213{
kate.wardf32fb4f2008-07-11 15:20:38 +0000214 ${_SHUNIT_LINENO_}
215 if [ $# -lt 1 -o $# -gt 2 ]; then
kate.ward38097672008-11-03 20:58:42 +0000216 _shunit_error "assertNull() requires one or two arguments; $# given"
kate.wardf32fb4f2008-07-11 15:20:38 +0000217 return ${SHUNIT_ERROR}
218 fi
kate.wardf51c6162008-06-17 16:38:35 +0000219 _shunit_shouldSkip && return ${SHUNIT_TRUE}
220
kate.ward38097672008-11-03 20:58:42 +0000221 shunit_message_=${__shunit_lineno}
kate.wardf32fb4f2008-07-11 15:20:38 +0000222 if [ $# -eq 2 ]; then
223 shunit_message_="${shunit_message_}$1"
224 shift
225 fi
kate.ward38097672008-11-03 20:58:42 +0000226 assertTrue "${shunit_message_}" "[ -z '$1' ]"
227 shunit_return=$?
228
229 unset shunit_message_
230 return ${shunit_return}
kate.wardf51c6162008-06-17 16:38:35 +0000231}
kate.wardf32fb4f2008-07-11 15:20:38 +0000232_ASSERT_NULL_='eval assertNull --lineno "${LINENO:-}"'
kate.wardf51c6162008-06-17 16:38:35 +0000233
234#/**
235# <s:function group="asserts">
236# <entry align="right">
237# <emphasis>void</emphasis>
238# </entry>
239# <entry>
240# <funcsynopsis>
241# <funcprototype>
242# <funcdef><function>assertNotNull</function></funcdef>
243# <paramdef>string <parameter>[message]</parameter></paramdef>
244# <paramdef>string <parameter>value</parameter></paramdef>
245# </funcprototype>
246# </funcsynopsis>
247# <para>Asserts that <emphasis>value</emphasis> is <emphasis
248# role="strong">not</emphasis> <literal>null</literal>, or in shell terms not
249# a zero-length string. The message is optional.</para>
250# </entry>
251# </s:function>
252#*/
253assertNotNull()
254{
kate.wardf32fb4f2008-07-11 15:20:38 +0000255 ${_SHUNIT_LINENO_}
256 if [ $# -gt 2 ]; then # allowing 0 arguments as $1 might actually be null
kate.ward38097672008-11-03 20:58:42 +0000257 _shunit_error "assertNotNull() requires one or two arguments; $# given"
kate.wardf32fb4f2008-07-11 15:20:38 +0000258 return ${SHUNIT_ERROR}
259 fi
kate.wardf51c6162008-06-17 16:38:35 +0000260 _shunit_shouldSkip && return ${SHUNIT_TRUE}
261
kate.ward38097672008-11-03 20:58:42 +0000262 shunit_message_=${__shunit_lineno}
kate.wardf51c6162008-06-17 16:38:35 +0000263 if [ $# -eq 2 ]; then
kate.ward38097672008-11-03 20:58:42 +0000264 shunit_message_="${shunit_message_}$1"
265 shift
kate.wardf51c6162008-06-17 16:38:35 +0000266 fi
kate.ward38097672008-11-03 20:58:42 +0000267 assertTrue "${shunit_message_}" "[ -n '${1:-}' ]"
268 shunit_return=$?
269
270 unset shunit_message_
271 return ${shunit_return}
kate.wardf51c6162008-06-17 16:38:35 +0000272}
kate.wardf32fb4f2008-07-11 15:20:38 +0000273_ASSERT_NOT_NULL_='eval assertNotNull --lineno "${LINENO:-}"'
kate.wardf51c6162008-06-17 16:38:35 +0000274
275#/**
276# <s:function group="asserts">
277# <entry align="right">
278# <emphasis>void</emphasis>
279# </entry>
280# <entry>
281# <funcsynopsis>
282# <funcprototype>
283# <funcdef><function>assertSame</function></funcdef>
284# <paramdef>string <parameter>[message]</parameter></paramdef>
285# <paramdef>string <parameter>expected</parameter></paramdef>
286# <paramdef>string <parameter>actual</parameter></paramdef>
287# </funcprototype>
288# </funcsynopsis>
289# <para>This function is functionally equivalent to
290# <function>assertEquals</function>.</para>
291# </entry>
292# </s:function>
293#*/
294assertSame()
295{
kate.wardf32fb4f2008-07-11 15:20:38 +0000296 ${_SHUNIT_LINENO_}
297 if [ $# -lt 2 -o $# -gt 3 ]; then
kate.ward38097672008-11-03 20:58:42 +0000298 _shunit_error "assertSame() requires one or two arguments; $# given"
kate.wardf32fb4f2008-07-11 15:20:38 +0000299 return ${SHUNIT_ERROR}
300 fi
301 _shunit_shouldSkip && return ${SHUNIT_TRUE}
302
kate.ward38097672008-11-03 20:58:42 +0000303 shunit_message_=${__shunit_lineno}
304 if [ $# -eq 3 ]; then
305 shunit_message_="${shunit_message_}$1"
306 shift
kate.wardf32fb4f2008-07-11 15:20:38 +0000307 fi
kate.ward38097672008-11-03 20:58:42 +0000308 assertEquals "${shunit_message_}" "$1" "$2"
309 shunit_return=$?
310
311 unset shunit_message_
312 return ${shunit_return}
kate.wardf51c6162008-06-17 16:38:35 +0000313}
kate.wardf32fb4f2008-07-11 15:20:38 +0000314_ASSERT_SAME_='eval assertSame --lineno "${LINENO:-}"'
kate.wardf51c6162008-06-17 16:38:35 +0000315
316#/**
317# <s:function group="asserts">
318# <entry align="right">
319# <emphasis>void</emphasis>
320# </entry>
321# <entry>
322# <funcsynopsis>
323# <funcprototype>
324# <funcdef><function>assertNotSame</function></funcdef>
325# <paramdef>string <parameter>[message]</parameter></paramdef>
326# <paramdef>string <parameter>unexpected</parameter></paramdef>
327# <paramdef>string <parameter>actual</parameter></paramdef>
328# </funcprototype>
329# </funcsynopsis>
330# <para>Asserts that <emphasis>unexpected</emphasis> and
331# <emphasis>actual</emphasis> are <emphasis role="strong">not</emphasis>
332# equal to one another. The message is optional.</para>
333# </entry>
334# </s:function>
335#*/
336assertNotSame()
337{
kate.wardf32fb4f2008-07-11 15:20:38 +0000338 ${_SHUNIT_LINENO_}
339 if [ $# -lt 2 -o $# -gt 3 ]; then
kate.ward38097672008-11-03 20:58:42 +0000340 _shunit_error "assertNotSame() requires two or three arguments; $# given"
kate.wardf32fb4f2008-07-11 15:20:38 +0000341 return ${SHUNIT_ERROR}
342 fi
kate.wardf51c6162008-06-17 16:38:35 +0000343 _shunit_shouldSkip && return ${SHUNIT_TRUE}
344
kate.ward38097672008-11-03 20:58:42 +0000345 shunit_message_=${__shunit_lineno}
kate.wardf51c6162008-06-17 16:38:35 +0000346 if [ $# -eq 3 ]; then
kate.ward38097672008-11-03 20:58:42 +0000347 shunit_message_="${shunit_message_:-}$1"
kate.wardf51c6162008-06-17 16:38:35 +0000348 shift
349 fi
kate.ward38097672008-11-03 20:58:42 +0000350 assertNotEquals "${shunit_message_}" "$1" "$2"
351 shunit_return=$?
kate.wardf51c6162008-06-17 16:38:35 +0000352
kate.ward38097672008-11-03 20:58:42 +0000353 unset shunit_message_
kate.wardf51c6162008-06-17 16:38:35 +0000354 return ${shunit_return}
355}
kate.wardf32fb4f2008-07-11 15:20:38 +0000356_ASSERT_NOT_SAME_='eval assertNotSame --lineno "${LINENO:-}"'
kate.wardf51c6162008-06-17 16:38:35 +0000357
358#/**
359# <s:function group="asserts">
360# <entry align="right">
361# <emphasis>void</emphasis>
362# </entry>
363# <entry>
364# <funcsynopsis>
365# <funcprototype>
366# <funcdef><function>assertTrue</function></funcdef>
367# <paramdef>string <parameter>[message]</parameter></paramdef>
368# <paramdef>string <parameter>condition</parameter></paramdef>
369# </funcprototype>
370# </funcsynopsis>
371# <para>Asserts that a given shell test condition is true. The message is
372# optional.</para>
373# <para>Testing whether something is true or false is easy enough by using
374# the assertEquals/assertNotSame functions. Shell supports much more
375# complicated tests though, and a means to support them was needed. As such,
376# this function tests that conditions are true or false through evaluation
377# rather than just looking for a true or false.</para>
378# <funcsynopsis>
379# The following test will succeed: <funcsynopsisinfo>assertTrue "[ 34 -gt 23 ]"</funcsynopsisinfo>
380# The folloing test will fail with a message: <funcsynopsisinfo>assertTrue "test failed" "[ -r '/non/existant/file' ]"</funcsynopsisinfo>
381# </funcsynopsis>
382# </entry>
383# </s:function>
384#*/
385assertTrue()
386{
kate.wardf32fb4f2008-07-11 15:20:38 +0000387 ${_SHUNIT_LINENO_}
388 if [ $# -gt 2 ]; then
kate.ward38097672008-11-03 20:58:42 +0000389 _shunit_error "assertTrue() takes one two arguments; $# given"
kate.wardf32fb4f2008-07-11 15:20:38 +0000390 return ${SHUNIT_ERROR}
391 fi
kate.wardf51c6162008-06-17 16:38:35 +0000392 _shunit_shouldSkip && return ${SHUNIT_TRUE}
393
kate.ward38097672008-11-03 20:58:42 +0000394 shunit_message_=${__shunit_lineno}
kate.wardf51c6162008-06-17 16:38:35 +0000395 if [ $# -eq 2 ]; then
kate.wardf32fb4f2008-07-11 15:20:38 +0000396 shunit_message_="${shunit_message_}$1"
kate.wardf51c6162008-06-17 16:38:35 +0000397 shift
398 fi
kate.wardf32fb4f2008-07-11 15:20:38 +0000399 shunit_condition_=$1
kate.wardf51c6162008-06-17 16:38:35 +0000400
401 # see if condition is an integer, i.e. a return value
kate.wardf32fb4f2008-07-11 15:20:38 +0000402 shunit_match_=`expr "${shunit_condition_}" : '\([0-9]*\)'`
403 shunit_return=${SHUNIT_TRUE}
404 if [ -z "${shunit_condition_}" ]; then
kate.wardf51c6162008-06-17 16:38:35 +0000405 # null condition
406 shunit_return=${SHUNIT_FALSE}
kate.wardf32fb4f2008-07-11 15:20:38 +0000407 elif [ "${shunit_condition_}" = "${shunit_match_}" ]; then
kate.wardf51c6162008-06-17 16:38:35 +0000408 # possible return value. treating 0 as true, and non-zero as false.
kate.wardf32fb4f2008-07-11 15:20:38 +0000409 [ ${shunit_condition_} -ne 0 ] && shunit_return=${SHUNIT_FALSE}
kate.wardf51c6162008-06-17 16:38:35 +0000410 else
411 # (hopefully) a condition
kate.wardf32fb4f2008-07-11 15:20:38 +0000412 ( eval ${shunit_condition_} ) >/dev/null 2>&1
kate.wardf51c6162008-06-17 16:38:35 +0000413 [ $? -ne 0 ] && shunit_return=${SHUNIT_FALSE}
414 fi
415
416 # record the test
417 if [ ${shunit_return} -eq ${SHUNIT_TRUE} ]; then
kate.ward38097672008-11-03 20:58:42 +0000418 _shunit_assertPass
kate.wardf51c6162008-06-17 16:38:35 +0000419 else
kate.ward38097672008-11-03 20:58:42 +0000420 _shunit_assertFail "${shunit_message_}"
kate.wardf51c6162008-06-17 16:38:35 +0000421 fi
422
kate.wardf32fb4f2008-07-11 15:20:38 +0000423 unset shunit_message_ shunit_condition_ shunit_match_
kate.wardf51c6162008-06-17 16:38:35 +0000424 return ${shunit_return}
425}
kate.wardf32fb4f2008-07-11 15:20:38 +0000426_ASSERT_TRUE_='eval assertTrue --lineno "${LINENO:-}"'
kate.wardf51c6162008-06-17 16:38:35 +0000427
428#/**
429# <s:function group="asserts">
430# <entry align="right">
431# <emphasis>void</emphasis>
432# </entry>
433# <entry>
434# <funcsynopsis>
435# <funcprototype>
436# <funcdef><function>assertFalse</function></funcdef>
437# <paramdef>string <parameter>[message]</parameter></paramdef>
438# <paramdef>string <parameter>condition</parameter></paramdef>
439# </funcprototype>
440# </funcsynopsis>
441# <para>Asserts that a given shell test condition is false. The message is
442# optional.</para>
443# <para>Testing whether something is true or false is easy enough by using
444# the assertEquals/assertNotSame functions. Shell supports much more
445# complicated tests though, and a means to support them was needed. As such,
446# this function tests that conditions are true or false through evaluation
447# rather than just looking for a true or false.</para>
448# <funcsynopsis>
449# The following test will succeed: <funcsynopsisinfo>assertFalse "[ 'apples' = 'oranges' ]"</funcsynopsisinfo>
450# The folloing test will fail with a message: <funcsynopsisinfo>assertFalse "test failed" "[ 1 -eq 1 -a 2 -eq 2 ]"</funcsynopsisinfo>
451# </funcsynopsis>
452# </entry>
453# </s:function>
454#*/
455assertFalse()
456{
kate.wardf32fb4f2008-07-11 15:20:38 +0000457 ${_SHUNIT_LINENO_}
458 if [ $# -lt 1 -o $# -gt 2 ]; then
kate.ward38097672008-11-03 20:58:42 +0000459 _shunit_error "assertFalse() quires one or two arguments; $# given"
kate.wardf32fb4f2008-07-11 15:20:38 +0000460 return ${SHUNIT_ERROR}
461 fi
kate.wardf51c6162008-06-17 16:38:35 +0000462 _shunit_shouldSkip && return ${SHUNIT_TRUE}
463
kate.ward38097672008-11-03 20:58:42 +0000464 shunit_message_=${__shunit_lineno}
kate.wardf51c6162008-06-17 16:38:35 +0000465 if [ $# -eq 2 ]; then
kate.wardf32fb4f2008-07-11 15:20:38 +0000466 shunit_message_="${shunit_message_}$1"
kate.wardf51c6162008-06-17 16:38:35 +0000467 shift
468 fi
kate.wardf32fb4f2008-07-11 15:20:38 +0000469 shunit_condition_=$1
kate.wardf51c6162008-06-17 16:38:35 +0000470
471 # see if condition is an integer, i.e. a return value
kate.wardf32fb4f2008-07-11 15:20:38 +0000472 shunit_match_=`expr "${shunit_condition_}" : '\([0-9]*\)'`
473 shunit_return=${SHUNIT_TRUE}
474 if [ -z "${shunit_condition_}" ]; then
kate.wardf51c6162008-06-17 16:38:35 +0000475 # null condition
476 shunit_return=${SHUNIT_FALSE}
kate.wardf32fb4f2008-07-11 15:20:38 +0000477 elif [ "${shunit_condition_}" = "${shunit_match_}" ]; then
kate.wardf51c6162008-06-17 16:38:35 +0000478 # possible return value. treating 0 as true, and non-zero as false.
kate.wardf32fb4f2008-07-11 15:20:38 +0000479 [ ${shunit_condition_} -eq 0 ] && shunit_return=${SHUNIT_FALSE}
kate.wardf51c6162008-06-17 16:38:35 +0000480 else
481 # (hopefully) a condition
kate.wardf32fb4f2008-07-11 15:20:38 +0000482 ( eval ${shunit_condition_} ) >/dev/null 2>&1
kate.wardf51c6162008-06-17 16:38:35 +0000483 [ $? -eq 0 ] && shunit_return=${SHUNIT_FALSE}
484 fi
485
486 # record the test
487 if [ ${shunit_return} -eq ${SHUNIT_TRUE} ]; then
kate.ward38097672008-11-03 20:58:42 +0000488 _shunit_assertPass
kate.wardf51c6162008-06-17 16:38:35 +0000489 else
kate.ward38097672008-11-03 20:58:42 +0000490 _shunit_assertFail "${shunit_message_}"
kate.wardf51c6162008-06-17 16:38:35 +0000491 fi
492
kate.wardf32fb4f2008-07-11 15:20:38 +0000493 unset shunit_message_ shunit_condition_ shunit_match_
kate.wardf51c6162008-06-17 16:38:35 +0000494 return ${shunit_return}
495}
kate.wardf32fb4f2008-07-11 15:20:38 +0000496_ASSERT_FALSE_='eval assertFalse --lineno "${LINENO:-}"'
kate.wardf51c6162008-06-17 16:38:35 +0000497
498#-----------------------------------------------------------------------------
499# failure functions
500#
501
502#/**
503# <s:function group="failures">
504# <entry align="right">
505# <emphasis>void</emphasis>
506# </entry>
507# <entry>
508# <funcsynopsis>
509# <funcprototype>
510# <funcdef><function>fail</function></funcdef>
511# <paramdef>string <parameter>[message]</parameter></paramdef>
512# </funcprototype>
513# </funcsynopsis>
514# <para>Fails the test immediately, with the optional message.</para>
515# </entry>
516# </s:function>
517#*/
518fail()
519{
kate.wardf32fb4f2008-07-11 15:20:38 +0000520 ${_SHUNIT_LINENO_}
521 if [ $# -gt 1 ]; then
kate.ward38097672008-11-03 20:58:42 +0000522 _shunit_error "fail() requires one or two arguments; $# given"
kate.wardf32fb4f2008-07-11 15:20:38 +0000523 return ${SHUNIT_ERROR}
524 fi
kate.wardf51c6162008-06-17 16:38:35 +0000525 _shunit_shouldSkip && return ${SHUNIT_TRUE}
526
kate.ward38097672008-11-03 20:58:42 +0000527 shunit_message_=${__shunit_lineno}
kate.wardf32fb4f2008-07-11 15:20:38 +0000528 if [ $# -eq 1 ]; then
529 shunit_message_="${shunit_message_}$1"
530 shift
531 fi
kate.wardf51c6162008-06-17 16:38:35 +0000532
kate.ward38097672008-11-03 20:58:42 +0000533 _shunit_assertFail "${shunit_message_}"
kate.wardf51c6162008-06-17 16:38:35 +0000534
kate.wardf32fb4f2008-07-11 15:20:38 +0000535 unset shunit_message_
536 return ${SHUNIT_FALSE}
kate.wardf51c6162008-06-17 16:38:35 +0000537}
kate.wardf32fb4f2008-07-11 15:20:38 +0000538_FAIL_='eval fail --lineno "${LINENO:-}"'
kate.wardf51c6162008-06-17 16:38:35 +0000539
540#/**
541# <s:function group="failures">
542# <entry align="right">
543# <emphasis>void</emphasis>
544# </entry>
545# <entry>
546# <funcsynopsis>
547# <funcprototype>
548# <funcdef><function>failNotEquals</function></funcdef>
549# <paramdef>string <parameter>[message]</parameter></paramdef>
550# <paramdef>string <parameter>unexpected</parameter></paramdef>
551# <paramdef>string <parameter>actual</parameter></paramdef>
552# </funcprototype>
553# </funcsynopsis>
554# <para>Fails the test if <emphasis>unexpected</emphasis> and
555# <emphasis>actual</emphasis> are <emphasis role="strong">not</emphasis>
556# equal to one another. The message is optional.</para>
557# </entry>
558# </s:function>
559#*/
560failNotEquals()
561{
kate.wardf32fb4f2008-07-11 15:20:38 +0000562 ${_SHUNIT_LINENO_}
563 if [ $# -lt 2 -o $# -gt 3 ]; then
kate.ward38097672008-11-03 20:58:42 +0000564 _shunit_error "failNotEquals() requires one or two arguments; $# given"
kate.wardf32fb4f2008-07-11 15:20:38 +0000565 return ${SHUNIT_ERROR}
566 fi
kate.wardf51c6162008-06-17 16:38:35 +0000567 _shunit_shouldSkip && return ${SHUNIT_TRUE}
568
kate.ward38097672008-11-03 20:58:42 +0000569 shunit_message_=${__shunit_lineno}
kate.wardf51c6162008-06-17 16:38:35 +0000570 if [ $# -eq 3 ]; then
kate.wardf32fb4f2008-07-11 15:20:38 +0000571 shunit_message_="${shunit_message_}$1"
kate.wardf51c6162008-06-17 16:38:35 +0000572 shift
573 fi
kate.wardf32fb4f2008-07-11 15:20:38 +0000574 shunit_unexpected_=$1
575 shunit_actual_=$2
kate.wardf51c6162008-06-17 16:38:35 +0000576
kate.ward38097672008-11-03 20:58:42 +0000577 _shunit_assertFail "${shunit_message_:+${shunit_message_} }expected:<${shunit_unexpected_}> but was:<${shunit_actual_}>"
kate.wardf51c6162008-06-17 16:38:35 +0000578
kate.wardf32fb4f2008-07-11 15:20:38 +0000579 unset shunit_message_ shunit_unexpected_ shunit_actual_
580 return ${SHUNIT_FALSE}
kate.wardf51c6162008-06-17 16:38:35 +0000581}
kate.wardf32fb4f2008-07-11 15:20:38 +0000582_FAIL_NOT_EQUALS_='eval failNotEquals --lineno "${LINENO:-}"'
kate.wardf51c6162008-06-17 16:38:35 +0000583
584#/**
585# <s:function group="failures">
586# <entry align="right">
587# <emphasis>void</emphasis>
588# </entry>
589# <entry>
590# <funcsynopsis>
591# <funcprototype>
592# <funcdef><function>failSame</function></funcdef>
593# <paramdef>string <parameter>[message]</parameter></paramdef>
594# </funcprototype>
595# </funcsynopsis>
kate.ward38097672008-11-03 20:58:42 +0000596# <para>Indicate test failure because arguments were the same. The message is
597# optional.</para>
kate.wardf51c6162008-06-17 16:38:35 +0000598# </entry>
599# </s:function>
600#*/
601failSame()
602{
kate.wardf32fb4f2008-07-11 15:20:38 +0000603 ${_SHUNIT_LINENO_}
604 if [ $# -lt 2 -o $# -gt 3 ]; then
kate.ward38097672008-11-03 20:58:42 +0000605 _shunit_error "failSame() requires two or three arguments; $# given"
kate.wardf32fb4f2008-07-11 15:20:38 +0000606 return ${SHUNIT_ERROR}
607 fi
kate.wardf51c6162008-06-17 16:38:35 +0000608 _shunit_shouldSkip && return ${SHUNIT_TRUE}
609
kate.ward38097672008-11-03 20:58:42 +0000610 shunit_message_=${__shunit_lineno}
kate.wardf32fb4f2008-07-11 15:20:38 +0000611 if [ $# -eq 3 ]; then
612 shunit_message_="${shunit_message_}$1"
613 shift
614 fi
kate.wardf51c6162008-06-17 16:38:35 +0000615
kate.ward38097672008-11-03 20:58:42 +0000616 _shunit_assertFail "${shunit_message_:+${shunit_message_} }expected not same"
kate.wardf51c6162008-06-17 16:38:35 +0000617
kate.wardf32fb4f2008-07-11 15:20:38 +0000618 unset shunit_message_
619 return ${SHUNIT_FALSE}
kate.wardf51c6162008-06-17 16:38:35 +0000620}
kate.wardf32fb4f2008-07-11 15:20:38 +0000621_FAIL_SAME_='eval failSame --lineno "${LINENO:-}"'
kate.wardf51c6162008-06-17 16:38:35 +0000622
623#/**
624# <s:function group="failures">
625# <entry align="right">
626# <emphasis>void</emphasis>
627# </entry>
628# <entry>
629# <funcsynopsis>
630# <funcprototype>
631# <funcdef><function>failNotSame</function></funcdef>
632# <paramdef>string <parameter>[message]</parameter></paramdef>
633# <paramdef>string <parameter>expected</parameter></paramdef>
634# <paramdef>string <parameter>actual</parameter></paramdef>
635# </funcprototype>
636# </funcsynopsis>
kate.ward38097672008-11-03 20:58:42 +0000637# <para>Indicate test failure because arguments were not the same. The
638# message is optional.</para>
kate.wardf51c6162008-06-17 16:38:35 +0000639# </entry>
640# </s:function>
641#*/
642failNotSame()
643{
kate.wardf32fb4f2008-07-11 15:20:38 +0000644 ${_SHUNIT_LINENO_}
645 if [ $# -lt 2 -o $# -gt 3 ]; then
kate.ward38097672008-11-03 20:58:42 +0000646 _shunit_error "failNotEquals() requires one or two arguments; $# given"
kate.wardf32fb4f2008-07-11 15:20:38 +0000647 return ${SHUNIT_ERROR}
648 fi
649 _shunit_shouldSkip && return ${SHUNIT_TRUE}
650
kate.ward38097672008-11-03 20:58:42 +0000651 shunit_message_=${__shunit_lineno}
652 if [ $# -eq 3 ]; then
653 shunit_message_="${shunit_message_}$1"
654 shift
kate.wardf32fb4f2008-07-11 15:20:38 +0000655 fi
kate.ward38097672008-11-03 20:58:42 +0000656 failNotEquals "${shunit_message_}" "$1" "$2"
657 shunit_return=$?
658
659 unset shunit_message_
660 return ${shunit_return}
kate.wardf51c6162008-06-17 16:38:35 +0000661}
kate.wardf32fb4f2008-07-11 15:20:38 +0000662_FAIL_NOT_SAME_='eval failNotSame --lineno "${LINENO:-}"'
kate.wardf51c6162008-06-17 16:38:35 +0000663
664#-----------------------------------------------------------------------------
665# skipping functions
666#
667
668#/**
669# <s:function group="skipping">
670# <entry align="right">
671# <emphasis>void</emphasis>
672# </entry>
673# <entry>
674# <funcsynopsis>
675# <funcprototype>
676# <funcdef><function>startSkipping</function></funcdef>
677# <paramdef />
678# </funcprototype>
679# </funcsynopsis>
680# <para>This function forces the remaining assert and fail functions to be
681# "skipped", i.e. they will have no effect. Each function skipped will be
682# recorded so that the total of asserts and fails will not be altered.</para>
683# </entry>
684# </s:function>
685#*/
686startSkipping()
687{
688 __shunit_skip=${SHUNIT_TRUE}
689}
690
691#/**
692# <s:function group="skipping">
693# <entry align="right">
694# <emphasis>void</emphasis>
695# </entry>
696# <entry>
697# <funcsynopsis>
698# <funcprototype>
699# <funcdef><function>endSkipping</function></funcdef>
700# <paramdef />
701# </funcprototype>
702# </funcsynopsis>
703# <para>This function returns calls to the assert and fail functions to their
704# default behavior, i.e. they will be called.</para>
705# </entry>
706# </s:function>
707#*/
708endSkipping()
709{
710 __shunit_skip=${SHUNIT_FALSE}
711}
712
713#/**
714# <s:function group="skipping">
715# <entry align="right">
716# <emphasis>boolean</emphasis>
717# </entry>
718# <entry>
719# <funcsynopsis>
720# <funcprototype>
721# <funcdef><function>isSkipping</function></funcdef>
722# <paramdef />
723# </funcprototype>
724# </funcsynopsis>
725# <para>This function returns the state of skipping.</para>
726# </entry>
727# </s:function>
728#*/
729isSkipping()
730{
731 return ${__shunit_skip}
732}
733
734#-----------------------------------------------------------------------------
735# suite functions
736#
737
738#/**
739# <s:function group="suites">
740# <entry align="right">
741# <emphasis>void</emphasis>
742# </entry>
743# <entry>
744# <funcsynopsis>
745# <funcprototype>
746# <funcdef><function>suite</function></funcdef>
747# <paramdef />
748# </funcprototype>
749# </funcsynopsis>
750# <para>This function can be optionally overridden by the user in their test
751# suite.</para>
752# <para>If this function exists, it will be called when
753# <command>shunit2</command> is sourced. If it does not exist, shUnit2 will
754# search the parent script for all functions beginning with the word
755# <literal>test</literal>, and they will be added dynamically to the test
756# suite.</para>
757# </entry>
758# </s:function>
759#*/
760# Note: see _shunit_mktempFunc() for actual implementation
761# suite() { :; }
762
763#/**
764# <s:function group="suites">
765# <entry align="right">
766# <emphasis>void</emphasis>
767# </entry>
768# <entry>
769# <funcsynopsis>
770# <funcprototype>
771# <funcdef><function>suite_addTest</function></funcdef>
772# <paramdef>string <parameter>function</parameter></paramdef>
773# </funcprototype>
774# </funcsynopsis>
775# <para>This function adds a function name to the list of tests scheduled for
776# execution as part of this test suite. This function should only be called
777# from within the <function>suite()</function> function.</para>
778# </entry>
779# </s:function>
780#*/
781suite_addTest()
782{
kate.ward38097672008-11-03 20:58:42 +0000783 shunit_func_=${1:-}
kate.wardf51c6162008-06-17 16:38:35 +0000784
kate.ward38097672008-11-03 20:58:42 +0000785 __shunit_suite="${__shunit_suite:+${__shunit_suite} }${shunit_func_}"
786 __shunit_testsTotal=`expr ${__shunit_testsTotal} + 1`
kate.wardf51c6162008-06-17 16:38:35 +0000787
kate.ward38097672008-11-03 20:58:42 +0000788 unset shunit_func_
kate.wardf51c6162008-06-17 16:38:35 +0000789}
790
791#/**
792# <s:function group="suites">
793# <entry align="right">
794# <emphasis>void</emphasis>
795# </entry>
796# <entry>
797# <funcsynopsis>
798# <funcprototype>
799# <funcdef><function>oneTimeSetUp</function></funcdef>
800# <paramdef />
801# </funcprototype>
802# </funcsynopsis>
803# <para>This function can be be optionally overridden by the user in their
804# test suite.</para>
805# <para>If this function exists, it will be called once before any tests are
806# run. It is useful to prepare a common environment for all tests.</para>
807# </entry>
808# </s:function>
809#*/
810# Note: see _shunit_mktempFunc() for actual implementation
811# oneTimeSetUp() { :; }
812
813#/**
814# <s:function group="suites">
815# <entry align="right">
816# <emphasis>void</emphasis>
817# </entry>
818# <entry>
819# <funcsynopsis>
820# <funcprototype>
821# <funcdef><function>oneTimeTearDown</function></funcdef>
822# <paramdef />
823# </funcprototype>
824# </funcsynopsis>
825# <para>This function can be be optionally overridden by the user in their
826# test suite.</para>
827# <para>If this function exists, it will be called once after all tests are
828# completed. It is useful to clean up the environment after all tests.</para>
829# </entry>
830# </s:function>
831#*/
832# Note: see _shunit_mktempFunc() for actual implementation
833# oneTimeTearDown() { :; }
834
835#/**
836# <s:function group="suites">
837# <entry align="right">
838# <emphasis>void</emphasis>
839# </entry>
840# <entry>
841# <funcsynopsis>
842# <funcprototype>
843# <funcdef><function>setUp</function></funcdef>
844# <paramdef />
845# </funcprototype>
846# </funcsynopsis>
847# <para>This function can be be optionally overridden by the user in their
848# test suite.</para>
849# <para>If this function exists, it will be called before each test is run.
850# It is useful to reset the environment before each test.</para>
851# </entry>
852# </s:function>
853#*/
854# Note: see _shunit_mktempFunc() for actual implementation
855# setUp() { :; }
856
857#/**
858# <s:function group="suites">
859# <entry align="right">
860# <emphasis>void</emphasis>
861# </entry>
862# <entry>
863# <funcsynopsis>
864# <funcprototype>
865# <funcdef><function>tearDown</function></funcdef>
866# <paramdef />
867# </funcprototype>
868# </funcsynopsis>
869# <para>This function can be be optionally overridden by the user in their
870# test suite.</para>
871# <para>If this function exists, it will be called after each test completes.
872# It is useful to clean up the environment after each test.</para>
873# </entry>
874# </s:function>
875#*/
876# Note: see _shunit_mktempFunc() for actual implementation
877# tearDown() { :; }
878
879#------------------------------------------------------------------------------
880# internal shUnit2 functions
881#
882
kate.wardf51c6162008-06-17 16:38:35 +0000883# this function is a cross-platform temporary directory creation tool. not all
884# OSes have the mktemp function, so one is included here.
885_shunit_mktempDir()
886{
887 # try the standard mktemp function
888 ( exec mktemp -dqt shunit.XXXXXX 2>/dev/null ) && return
889
890 # the standard mktemp didn't work. doing our own.
891 if [ -r '/dev/urandom' ]; then
kate.ward38097672008-11-03 20:58:42 +0000892 _shunit_random_=`od -vAn -N4 -tx4 </dev/urandom |sed 's/^[^0-9a-f]*//'`
kate.wardf51c6162008-06-17 16:38:35 +0000893 elif [ -n "${RANDOM:-}" ]; then
894 # $RANDOM works
kate.ward38097672008-11-03 20:58:42 +0000895 _shunit_random_=${RANDOM}${RANDOM}${RANDOM}$$
kate.wardf51c6162008-06-17 16:38:35 +0000896 else
897 # $RANDOM doesn't work
kate.ward38097672008-11-03 20:58:42 +0000898 _shunit_date_=`date '+%Y%m%d%H%M%S'`
899 _shunit_random_=`expr ${_shunit_date_} / $$`
kate.wardf51c6162008-06-17 16:38:35 +0000900 fi
901
kate.ward38097672008-11-03 20:58:42 +0000902 _shunit_tmpDir_="${TMPDIR:-/tmp}/shunit.${_shunit_random_}"
903 ( umask 077 && mkdir "${_shunit_tmpDir_}" ) || {
904 _shunit_fatal 'could not create temporary directory! exiting'
905 exit ${SHUNIT_FALSE}
kate.wardf51c6162008-06-17 16:38:35 +0000906 }
907
kate.ward38097672008-11-03 20:58:42 +0000908 echo ${_shunit_tmpDir_}
909 unset _shunit_date_ _shunit_random_ _shunit_tmpDir_
kate.wardf51c6162008-06-17 16:38:35 +0000910}
911
912# this function is here to work around issues in Cygwin
913_shunit_mktempFunc()
914{
kate.ward38097672008-11-03 20:58:42 +0000915 for _shunit_func_ in oneTimeSetUp oneTimeTearDown setUp tearDown suite; do
916 _shunit_file_="${__shunit_tmpDir}/${_shunit_func_}"
917 cat <<EOF >"${_shunit_file_}"
kate.wardf51c6162008-06-17 16:38:35 +0000918#! /bin/sh
kate.ward38097672008-11-03 20:58:42 +0000919exit ${SHUNIT_TRUE}
kate.wardf51c6162008-06-17 16:38:35 +0000920EOF
kate.ward38097672008-11-03 20:58:42 +0000921 chmod +x "${_shunit_file_}"
kate.wardf51c6162008-06-17 16:38:35 +0000922 done
923
kate.ward38097672008-11-03 20:58:42 +0000924 unset _shunit_file_
925}
926
927_shunit_cleanup()
928{
929 _shunit_name_=$1
930
931 case ${_shunit_name_} in
932 EXIT) _shunit_signal_=0 ;;
933 INT) _shunit_signal_=2 ;;
934 TERM) _shunit_signal_=15 ;;
935 *)
936 _shunit_warn "unrecognized trap value (${_shunit_name_})"
937 _shunit_signal_=0
938 ;;
939 esac
940
941 # do our work
942 rm -fr "${__shunit_tmpDir}"
943
944 # exit for all non-EXIT signals
945 if [ ${_shunit_name_} != 'EXIT' ]; then
946 _shunit_warn "trapped and now handling the (${_shunit_name_}) signal"
947 # disable EXIT trap
948 trap 0
949 # add 128 to signal and exit
950 exit `expr ${_shunit_signal_} + 128`
951 elif [ ${__shunit_reportGenerated} -eq ${SHUNIT_FALSE} ] ; then
952 _shunit_assertFail 'Unknown failure encountered running a test'
953 _shunit_generateReport
954 exit ${SHUNIT_ERROR}
955 fi
956
957 unset _shunit_name_ _shunit_signal_
958}
959
960# The actual running of the tests happens here.
961_shunit_execSuite()
962{
963 for _shunit_test_ in ${__shunit_suite}; do
964 __shunit_testSuccess=${SHUNIT_TRUE}
965
966 # disable skipping
967 endSkipping
968
969 # execute the per-test setup function
970 setUp
971
972 # execute the test
973 echo "${_shunit_test_}"
974 eval ${_shunit_test_}
975
976 # execute the per-test tear-down function
977 tearDown
978
979 # update stats
980 if [ ${__shunit_testSuccess} -eq ${SHUNIT_TRUE} ]; then
981 __shunit_testsPassed=`expr ${__shunit_testsPassed} + 1`
982 else
983 __shunit_testsFailed=`expr ${__shunit_testsFailed} + 1`
984 fi
985 done
986
987 unset _shunit_test_
988}
989
990# This function exits shUnit2 with the appropriate error code and OK/FAILED
991# message.
992_shunit_generateReport()
993{
994 _shunit_ok_=${SHUNIT_TRUE}
995
996 # if no exit code was provided one, determine an appropriate one
997 [ ${__shunit_testsFailed} -gt 0 \
998 -o ${__shunit_testSuccess} -eq ${SHUNIT_FALSE} ] \
999 && _shunit_ok_=${SHUNIT_FALSE}
1000
1001 echo
1002 if [ ${__shunit_testsTotal} -eq 1 ]; then
1003 echo "Ran ${__shunit_testsTotal} test."
1004 else
1005 echo "Ran ${__shunit_testsTotal} tests."
1006 fi
1007
1008 _shunit_failures_=''
1009 _shunit_skipped_=''
1010 [ ${__shunit_assertsFailed} -gt 0 ] \
1011 && _shunit_failures_="failures=${__shunit_assertsFailed}"
1012 [ ${__shunit_assertsSkipped} -gt 0 ] \
1013 && _shunit_skipped_="skipped=${__shunit_assertsSkipped}"
1014
1015 if [ ${_shunit_ok_} -eq ${SHUNIT_TRUE} ]; then
1016 _shunit_msg_='OK'
1017 [ -n "${_shunit_skipped_}" ] \
1018 && _shunit_msg_="${_shunit_msg_} (${_shunit_skipped_})"
1019 else
1020 _shunit_msg_="FAILED (${_shunit_failures_}"
1021 [ -n "${_shunit_skipped_}" ] \
1022 && _shunit_msg_="${_shunit_msg_},${_shunit_skipped_}"
1023 _shunit_msg_="${_shunit_msg_})"
1024 fi
1025
1026 echo
1027 echo ${_shunit_msg_}
1028 __shunit_reportGenerated=${SHUNIT_TRUE}
1029
1030 unset _shunit_failures_ _shunit_msg_ _shunit_ok_ _shunit_skipped_
kate.wardf51c6162008-06-17 16:38:35 +00001031}
1032
1033_shunit_shouldSkip()
1034{
1035 [ ${__shunit_skip} -eq ${SHUNIT_FALSE} ] && return ${SHUNIT_FALSE}
kate.ward38097672008-11-03 20:58:42 +00001036 _shunit_assertSkip
kate.wardf51c6162008-06-17 16:38:35 +00001037}
1038
kate.ward38097672008-11-03 20:58:42 +00001039_shunit_assertPass()
kate.wardf51c6162008-06-17 16:38:35 +00001040{
kate.ward38097672008-11-03 20:58:42 +00001041 __shunit_assertsPassed=`expr ${__shunit_assertsPassed} + 1`
1042 __shunit_assertsTotal=`expr ${__shunit_assertsTotal} + 1`
kate.wardf51c6162008-06-17 16:38:35 +00001043}
1044
kate.ward38097672008-11-03 20:58:42 +00001045_shunit_assertFail()
kate.wardf51c6162008-06-17 16:38:35 +00001046{
kate.ward38097672008-11-03 20:58:42 +00001047 _shunit_msg_=$1
kate.wardf51c6162008-06-17 16:38:35 +00001048
kate.ward38097672008-11-03 20:58:42 +00001049 __shunit_testSuccess=${SHUNIT_FALSE}
1050 __shunit_assertsFailed=`expr ${__shunit_assertsFailed} + 1`
1051 __shunit_assertsTotal=`expr ${__shunit_assertsTotal} + 1`
1052 echo "${__SHUNIT_ASSERT_MSG_PREFIX}${_shunit_msg_}"
kate.wardf51c6162008-06-17 16:38:35 +00001053
kate.ward38097672008-11-03 20:58:42 +00001054 unset _shunit_msg_
kate.wardf51c6162008-06-17 16:38:35 +00001055}
1056
kate.ward38097672008-11-03 20:58:42 +00001057_shunit_assertSkip()
kate.wardf51c6162008-06-17 16:38:35 +00001058{
kate.ward38097672008-11-03 20:58:42 +00001059 __shunit_assertsSkipped=`expr ${__shunit_assertsSkipped} + 1`
1060 __shunit_assertsTotal=`expr ${__shunit_assertsTotal} + 1`
kate.wardf51c6162008-06-17 16:38:35 +00001061}
1062
1063#------------------------------------------------------------------------------
1064# main
1065#
1066
1067# create a temporary storage location
1068__shunit_tmpDir=`_shunit_mktempDir`
1069
kate.ward38097672008-11-03 20:58:42 +00001070# provide a public temporary directory for unit test scripts
1071# TODO(kward): document this
1072shunit_tmpDir="${__shunit_tmpDir}/tmp"
1073mkdir "${shunit_tmpDir}"
1074
kate.wardf51c6162008-06-17 16:38:35 +00001075# setup traps to clean up after ourselves
1076trap '_shunit_cleanup EXIT' 0
1077trap '_shunit_cleanup INT' 2
1078trap '_shunit_cleanup TERM' 15
1079
1080# create phantom functions to work around issues with Cygwin
1081_shunit_mktempFunc
1082PATH="${__shunit_tmpDir}:${PATH}"
1083
1084# execute the oneTimeSetUp function (if it exists)
kate.wardf51c6162008-06-17 16:38:35 +00001085oneTimeSetUp
1086
1087# execute the suite function defined in the parent test script
1088# deprecated as of 2.1.0
1089suite
1090
1091# if no suite function was defined, dynamically build a list of functions
1092if [ -z "${__shunit_suite}" ]; then
kate.wardf32fb4f2008-07-11 15:20:38 +00001093 shunit_funcs_=`grep "^[ \t]*test[A-Za-z0-9_]* *()" ${__SHUNIT_PARENT} \
kate.wardf51c6162008-06-17 16:38:35 +00001094 |sed 's/[^A-Za-z0-9_]//g'`
kate.wardf32fb4f2008-07-11 15:20:38 +00001095 for shunit_func_ in ${shunit_funcs_}; do
1096 suite_addTest ${shunit_func_}
kate.wardf51c6162008-06-17 16:38:35 +00001097 done
1098fi
kate.wardf32fb4f2008-07-11 15:20:38 +00001099unset shunit_func_ shunit_funcs_
kate.wardf51c6162008-06-17 16:38:35 +00001100
1101# execute the tests
1102_shunit_execSuite
1103
1104# execute the oneTimeTearDown function (if it exists)
1105oneTimeTearDown
1106
kate.ward38097672008-11-03 20:58:42 +00001107# generate the report
kate.wardf51c6162008-06-17 16:38:35 +00001108_shunit_generateReport
1109
kate.ward38097672008-11-03 20:58:42 +00001110# that's it folks
1111[ ${__shunit_testsFailed} -eq 0 ]
1112exit $?
kate.wardf51c6162008-06-17 16:38:35 +00001113
1114#/**
1115# </s:shelldoc>
1116#*/