kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 1 | # $Id: shunit2 151 2008-05-10 22:07:29Z sfsetse $ |
| 2 | # 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> |
| 9 | # shUnit 2.1.3 |
| 10 | # Shell Unit Test Framework |
| 11 | # |
| 12 | # http://shunit2.sourceforge.net/ |
| 13 | # |
| 14 | # written by Kate Ward <kate.ward@forestent.com> |
| 15 | # released under the LGPL |
| 16 | # |
| 17 | # this module implements a xUnit based unit test framework similar to JUnit |
| 18 | # </s:header> |
| 19 | #*/ |
| 20 | |
| 21 | __SHUNIT_VERSION='2.1.3' |
| 22 | |
| 23 | _shunit_warn() { echo "shunit2:WARN $@" >&2; } |
| 24 | _shunit_error() { echo "shunit2:ERROR $@" >&2; } |
| 25 | _shunit_fatal() { echo "shunit2:FATAL $@" >&2; } |
| 26 | |
| 27 | SHUNIT_TRUE=0 |
| 28 | SHUNIT_FALSE=1 |
| 29 | SHUNIT_ERROR=2 |
| 30 | |
| 31 | # specific shell checks |
| 32 | if [ -n "${ZSH_VERSION:-}" ]; then |
| 33 | if [ "`set -o |grep "^shwordsplit" |awk '{print $2}'`" = 'off' ]; then |
| 34 | _shunit_fatal 'shwordsplit option required for proper zsh operation' |
| 35 | exit ${SHUNIT_ERROR} |
| 36 | fi |
| 37 | if [ -z "${SHUNIT_PARENT:-}" ]; then |
| 38 | _shunit_fatal "zsh does not pass \$0 through properly. please declare \ |
| 39 | \"SHUNIT_PARENT=\$0\" before calling shUnit2" |
| 40 | exit ${SHUNIT_ERROR} |
| 41 | fi |
| 42 | fi |
| 43 | |
| 44 | # shell flags for shunit2: |
| 45 | # u - treat unset variables as an error when performing parameter expansion |
| 46 | __SHUNIT_SHELL_FLAGS='u' |
| 47 | |
| 48 | # save the current set of shell flags, and then set some for ourself |
| 49 | _shunit_shellFlags="$-" |
| 50 | for _shunit_shellFlag in `echo "${__SHUNIT_SHELL_FLAGS}" |sed 's/\(.\)/\1 /g'` |
| 51 | do |
| 52 | set -${_shunit_shellFlag} |
| 53 | done |
| 54 | |
| 55 | # |
| 56 | # constants |
| 57 | # |
| 58 | |
| 59 | __SHUNIT_ASSERT_MSG_PREFIX='ASSERT:' |
| 60 | __SHUNIT_PARENT=${SHUNIT_PARENT:-$0} |
| 61 | |
| 62 | # set the constants readonly |
| 63 | _shunit_constants=`set |grep "^__SHUNIT_" |cut -d= -f1` |
| 64 | echo "${_shunit_constants}" |grep "^Binary file" >/dev/null |
| 65 | if [ $? -eq 0 ]; then |
| 66 | # deal with binary junk in 'set' output |
| 67 | _shunit_constants=`set |grep -a "^__SHUNIT_" |cut -d= -f1` |
| 68 | fi |
| 69 | for _shunit_const in ${_shunit_constants}; do |
| 70 | if [ -n "${ZSH_VERSION:-}" ]; then |
| 71 | readonly -g ${_shunit_const} # declare readonly constants globally |
| 72 | else |
| 73 | readonly ${_shunit_const} |
| 74 | fi |
| 75 | done |
| 76 | unset _shunit_const _shunit_constants |
| 77 | |
| 78 | # variables |
| 79 | __shunit_skip=${SHUNIT_FALSE} |
| 80 | __shunit_suite='' |
| 81 | |
| 82 | __shunit_testsPassed=0 |
| 83 | __shunit_testsFailed=0 |
| 84 | __shunit_testsSkipped=0 |
| 85 | __shunit_testsTotal=0 |
| 86 | |
| 87 | #----------------------------------------------------------------------------- |
| 88 | # assert functions |
| 89 | # |
| 90 | |
| 91 | #/** |
| 92 | # <s:function group="asserts"> |
| 93 | # <entry align="right"> |
| 94 | # <emphasis>void</emphasis> |
| 95 | # </entry> |
| 96 | # <entry> |
| 97 | # <funcsynopsis> |
| 98 | # <funcprototype> |
| 99 | # <funcdef><function>assertEquals</function></funcdef> |
| 100 | # <paramdef>string <parameter>[message]</parameter></paramdef> |
| 101 | # <paramdef>string <parameter>expected</parameter></paramdef> |
| 102 | # <paramdef>string <parameter>actual</parameter></paramdef> |
| 103 | # </funcprototype> |
| 104 | # </funcsynopsis> |
| 105 | # <para>Asserts that <emphasis>expected</emphasis> and |
| 106 | # <emphasis>actual</emphasis> are equal to one another. The message is |
| 107 | # optional.</para> |
| 108 | # </entry> |
| 109 | # </s:function> |
| 110 | #*/ |
| 111 | assertEquals() |
| 112 | { |
| 113 | _shunit_shouldSkip && return ${SHUNIT_TRUE} |
| 114 | |
| 115 | _su_message='' |
| 116 | if [ $# -lt 2 ]; then |
| 117 | _shunit_error 'assertEquals()/assertSame() require at least two arguments' |
| 118 | return ${SHUNIT_ERROR} |
| 119 | elif [ $# -eq 3 ]; then |
| 120 | _su_message=$1 |
| 121 | shift |
| 122 | fi |
| 123 | _su_expected=${1:-} |
| 124 | _su_actual=${2:-} |
| 125 | |
| 126 | shunit_return=${SHUNIT_TRUE} |
| 127 | if [ "${_su_expected}" = "${_su_actual}" ]; then |
| 128 | _shunit_testPassed |
| 129 | else |
| 130 | failNotEquals "${_su_message}" "${_su_expected}" "${_su_actual}" |
| 131 | shunit_return=${SHUNIT_FALSE} |
| 132 | fi |
| 133 | |
| 134 | unset _su_message _su_expected _su_actual |
| 135 | return ${shunit_return} |
| 136 | } |
| 137 | |
| 138 | #/** |
| 139 | # <s:function group="asserts"> |
| 140 | # <entry align="right"> |
| 141 | # <emphasis>void</emphasis> |
| 142 | # </entry> |
| 143 | # <entry> |
| 144 | # <funcsynopsis> |
| 145 | # <funcprototype> |
| 146 | # <funcdef><function>assertNull</function></funcdef> |
| 147 | # <paramdef>string <parameter>[message]</parameter></paramdef> |
| 148 | # <paramdef>string <parameter>value</parameter></paramdef> |
| 149 | # </funcprototype> |
| 150 | # </funcsynopsis> |
| 151 | # <para>Asserts that <emphasis>value</emphasis> is <literal>null</literal>, |
| 152 | # or in shell terms a zero-length string. The message is optional.</para> |
| 153 | # </entry> |
| 154 | # </s:function> |
| 155 | #*/ |
| 156 | assertNull() |
| 157 | { |
| 158 | _shunit_shouldSkip && return ${SHUNIT_TRUE} |
| 159 | |
| 160 | if [ $# -lt 1 ]; then |
| 161 | _shunit_error 'assertNull() requires at least one arguments' |
| 162 | return ${SHUNIT_ERROR} |
| 163 | elif [ $# -eq 2 ]; then |
| 164 | assertTrue "$1" "[ -z '$2' ]" |
| 165 | else |
| 166 | assertTrue "[ -z '${1:-}' ]" |
| 167 | fi |
| 168 | } |
| 169 | |
| 170 | #/** |
| 171 | # <s:function group="asserts"> |
| 172 | # <entry align="right"> |
| 173 | # <emphasis>void</emphasis> |
| 174 | # </entry> |
| 175 | # <entry> |
| 176 | # <funcsynopsis> |
| 177 | # <funcprototype> |
| 178 | # <funcdef><function>assertNotNull</function></funcdef> |
| 179 | # <paramdef>string <parameter>[message]</parameter></paramdef> |
| 180 | # <paramdef>string <parameter>value</parameter></paramdef> |
| 181 | # </funcprototype> |
| 182 | # </funcsynopsis> |
| 183 | # <para>Asserts that <emphasis>value</emphasis> is <emphasis |
| 184 | # role="strong">not</emphasis> <literal>null</literal>, or in shell terms not |
| 185 | # a zero-length string. The message is optional.</para> |
| 186 | # </entry> |
| 187 | # </s:function> |
| 188 | #*/ |
| 189 | assertNotNull() |
| 190 | { |
| 191 | _shunit_shouldSkip && return ${SHUNIT_TRUE} |
| 192 | |
| 193 | if [ $# -eq 2 ]; then |
| 194 | assertTrue "$1" "[ -n '$2' ]" |
| 195 | else |
| 196 | assertTrue "[ -n '${1:-}' ]" |
| 197 | fi |
| 198 | } |
| 199 | |
| 200 | #/** |
| 201 | # <s:function group="asserts"> |
| 202 | # <entry align="right"> |
| 203 | # <emphasis>void</emphasis> |
| 204 | # </entry> |
| 205 | # <entry> |
| 206 | # <funcsynopsis> |
| 207 | # <funcprototype> |
| 208 | # <funcdef><function>assertSame</function></funcdef> |
| 209 | # <paramdef>string <parameter>[message]</parameter></paramdef> |
| 210 | # <paramdef>string <parameter>expected</parameter></paramdef> |
| 211 | # <paramdef>string <parameter>actual</parameter></paramdef> |
| 212 | # </funcprototype> |
| 213 | # </funcsynopsis> |
| 214 | # <para>This function is functionally equivalent to |
| 215 | # <function>assertEquals</function>.</para> |
| 216 | # </entry> |
| 217 | # </s:function> |
| 218 | #*/ |
| 219 | assertSame() |
| 220 | { |
| 221 | assertEquals "${@:-}" |
| 222 | } |
| 223 | |
| 224 | #/** |
| 225 | # <s:function group="asserts"> |
| 226 | # <entry align="right"> |
| 227 | # <emphasis>void</emphasis> |
| 228 | # </entry> |
| 229 | # <entry> |
| 230 | # <funcsynopsis> |
| 231 | # <funcprototype> |
| 232 | # <funcdef><function>assertNotSame</function></funcdef> |
| 233 | # <paramdef>string <parameter>[message]</parameter></paramdef> |
| 234 | # <paramdef>string <parameter>unexpected</parameter></paramdef> |
| 235 | # <paramdef>string <parameter>actual</parameter></paramdef> |
| 236 | # </funcprototype> |
| 237 | # </funcsynopsis> |
| 238 | # <para>Asserts that <emphasis>unexpected</emphasis> and |
| 239 | # <emphasis>actual</emphasis> are <emphasis role="strong">not</emphasis> |
| 240 | # equal to one another. The message is optional.</para> |
| 241 | # </entry> |
| 242 | # </s:function> |
| 243 | #*/ |
| 244 | assertNotSame() |
| 245 | { |
| 246 | _shunit_shouldSkip && return ${SHUNIT_TRUE} |
| 247 | |
| 248 | _su_message='' |
| 249 | if [ $# -eq 3 ]; then |
| 250 | _su_message=$1 |
| 251 | shift |
| 252 | fi |
| 253 | _su_unexpected=${1:-} |
| 254 | _su_actual=${2:-} |
| 255 | |
| 256 | shunit_return=${SHUNIT_TRUE} |
| 257 | if [ "${_su_unexpected}" != "${_su_actual}" ]; then |
| 258 | _shunit_testPassed |
| 259 | else |
| 260 | failSame "${_su_message}" |
| 261 | shunit_return=${SHUNIT_FALSE} |
| 262 | fi |
| 263 | |
| 264 | unset _su_message _su_unexpected _su_actual |
| 265 | return ${shunit_return} |
| 266 | } |
| 267 | |
| 268 | #/** |
| 269 | # <s:function group="asserts"> |
| 270 | # <entry align="right"> |
| 271 | # <emphasis>void</emphasis> |
| 272 | # </entry> |
| 273 | # <entry> |
| 274 | # <funcsynopsis> |
| 275 | # <funcprototype> |
| 276 | # <funcdef><function>assertTrue</function></funcdef> |
| 277 | # <paramdef>string <parameter>[message]</parameter></paramdef> |
| 278 | # <paramdef>string <parameter>condition</parameter></paramdef> |
| 279 | # </funcprototype> |
| 280 | # </funcsynopsis> |
| 281 | # <para>Asserts that a given shell test condition is true. The message is |
| 282 | # optional.</para> |
| 283 | # <para>Testing whether something is true or false is easy enough by using |
| 284 | # the assertEquals/assertNotSame functions. Shell supports much more |
| 285 | # complicated tests though, and a means to support them was needed. As such, |
| 286 | # this function tests that conditions are true or false through evaluation |
| 287 | # rather than just looking for a true or false.</para> |
| 288 | # <funcsynopsis> |
| 289 | # The following test will succeed: <funcsynopsisinfo>assertTrue "[ 34 -gt 23 ]"</funcsynopsisinfo> |
| 290 | # The folloing test will fail with a message: <funcsynopsisinfo>assertTrue "test failed" "[ -r '/non/existant/file' ]"</funcsynopsisinfo> |
| 291 | # </funcsynopsis> |
| 292 | # </entry> |
| 293 | # </s:function> |
| 294 | #*/ |
| 295 | assertTrue() |
| 296 | { |
| 297 | _shunit_shouldSkip && return ${SHUNIT_TRUE} |
| 298 | |
| 299 | _su_message='' |
| 300 | if [ $# -eq 2 ]; then |
| 301 | _su_message=$1 |
| 302 | shift |
| 303 | fi |
| 304 | _su_condition=${1:-} |
| 305 | |
| 306 | shunit_return=${SHUNIT_TRUE} |
| 307 | |
| 308 | # see if condition is an integer, i.e. a return value |
| 309 | _su_match=`expr "${_su_condition}" : '\([0-9]*\)'` |
| 310 | if [ -z "${_su_condition}" ]; then |
| 311 | # null condition |
| 312 | shunit_return=${SHUNIT_FALSE} |
| 313 | elif [ "${_su_condition}" = "${_su_match}" ]; then |
| 314 | # possible return value. treating 0 as true, and non-zero as false. |
| 315 | [ ${_su_condition} -ne 0 ] && shunit_return=${SHUNIT_FALSE} |
| 316 | else |
| 317 | # (hopefully) a condition |
| 318 | ( eval ${_su_condition} ) >/dev/null 2>&1 |
| 319 | [ $? -ne 0 ] && shunit_return=${SHUNIT_FALSE} |
| 320 | fi |
| 321 | |
| 322 | # record the test |
| 323 | if [ ${shunit_return} -eq ${SHUNIT_TRUE} ]; then |
| 324 | _shunit_testPassed |
| 325 | else |
| 326 | _shunit_testFailed "${_su_message}" |
| 327 | fi |
| 328 | |
| 329 | unset _su_message _su_condition _su_match |
| 330 | return ${shunit_return} |
| 331 | } |
| 332 | |
| 333 | #/** |
| 334 | # <s:function group="asserts"> |
| 335 | # <entry align="right"> |
| 336 | # <emphasis>void</emphasis> |
| 337 | # </entry> |
| 338 | # <entry> |
| 339 | # <funcsynopsis> |
| 340 | # <funcprototype> |
| 341 | # <funcdef><function>assertFalse</function></funcdef> |
| 342 | # <paramdef>string <parameter>[message]</parameter></paramdef> |
| 343 | # <paramdef>string <parameter>condition</parameter></paramdef> |
| 344 | # </funcprototype> |
| 345 | # </funcsynopsis> |
| 346 | # <para>Asserts that a given shell test condition is false. The message is |
| 347 | # optional.</para> |
| 348 | # <para>Testing whether something is true or false is easy enough by using |
| 349 | # the assertEquals/assertNotSame functions. Shell supports much more |
| 350 | # complicated tests though, and a means to support them was needed. As such, |
| 351 | # this function tests that conditions are true or false through evaluation |
| 352 | # rather than just looking for a true or false.</para> |
| 353 | # <funcsynopsis> |
| 354 | # The following test will succeed: <funcsynopsisinfo>assertFalse "[ 'apples' = 'oranges' ]"</funcsynopsisinfo> |
| 355 | # The folloing test will fail with a message: <funcsynopsisinfo>assertFalse "test failed" "[ 1 -eq 1 -a 2 -eq 2 ]"</funcsynopsisinfo> |
| 356 | # </funcsynopsis> |
| 357 | # </entry> |
| 358 | # </s:function> |
| 359 | #*/ |
| 360 | assertFalse() |
| 361 | { |
| 362 | _shunit_shouldSkip && return ${SHUNIT_TRUE} |
| 363 | |
| 364 | _su_message='' |
| 365 | if [ $# -eq 2 ]; then |
| 366 | _su_message=$1 |
| 367 | shift |
| 368 | fi |
| 369 | _su_condition=${1:-} |
| 370 | |
| 371 | shunit_return=${SHUNIT_TRUE} |
| 372 | |
| 373 | # see if condition is an integer, i.e. a return value |
| 374 | _su_match=`expr "${_su_condition}" : '\([0-9]*\)'` |
| 375 | if [ -z "${_su_condition}" ]; then |
| 376 | # null condition |
| 377 | shunit_return=${SHUNIT_FALSE} |
| 378 | elif [ "${_su_condition}" = "${_su_match}" ]; then |
| 379 | # possible return value. treating 0 as true, and non-zero as false. |
| 380 | [ ${_su_condition} -eq 0 ] && shunit_return=${SHUNIT_FALSE} |
| 381 | else |
| 382 | # (hopefully) a condition |
| 383 | ( eval ${_su_condition} ) >/dev/null 2>&1 |
| 384 | [ $? -eq 0 ] && shunit_return=${SHUNIT_FALSE} |
| 385 | fi |
| 386 | |
| 387 | # record the test |
| 388 | if [ ${shunit_return} -eq ${SHUNIT_TRUE} ]; then |
| 389 | _shunit_testPassed |
| 390 | else |
| 391 | _shunit_testFailed "${_su_message}" |
| 392 | fi |
| 393 | |
| 394 | unset _su_message _su_condition _su_match |
| 395 | return ${shunit_return} |
| 396 | } |
| 397 | |
| 398 | #----------------------------------------------------------------------------- |
| 399 | # failure functions |
| 400 | # |
| 401 | |
| 402 | #/** |
| 403 | # <s:function group="failures"> |
| 404 | # <entry align="right"> |
| 405 | # <emphasis>void</emphasis> |
| 406 | # </entry> |
| 407 | # <entry> |
| 408 | # <funcsynopsis> |
| 409 | # <funcprototype> |
| 410 | # <funcdef><function>fail</function></funcdef> |
| 411 | # <paramdef>string <parameter>[message]</parameter></paramdef> |
| 412 | # </funcprototype> |
| 413 | # </funcsynopsis> |
| 414 | # <para>Fails the test immediately, with the optional message.</para> |
| 415 | # </entry> |
| 416 | # </s:function> |
| 417 | #*/ |
| 418 | fail() |
| 419 | { |
| 420 | _shunit_shouldSkip && return ${SHUNIT_TRUE} |
| 421 | |
| 422 | _su_message=${1:-} |
| 423 | |
| 424 | _shunit_testFailed "${_su_message}" |
| 425 | |
| 426 | unset _su_message |
| 427 | } |
| 428 | |
| 429 | #/** |
| 430 | # <s:function group="failures"> |
| 431 | # <entry align="right"> |
| 432 | # <emphasis>void</emphasis> |
| 433 | # </entry> |
| 434 | # <entry> |
| 435 | # <funcsynopsis> |
| 436 | # <funcprototype> |
| 437 | # <funcdef><function>failNotEquals</function></funcdef> |
| 438 | # <paramdef>string <parameter>[message]</parameter></paramdef> |
| 439 | # <paramdef>string <parameter>unexpected</parameter></paramdef> |
| 440 | # <paramdef>string <parameter>actual</parameter></paramdef> |
| 441 | # </funcprototype> |
| 442 | # </funcsynopsis> |
| 443 | # <para>Fails the test if <emphasis>unexpected</emphasis> and |
| 444 | # <emphasis>actual</emphasis> are <emphasis role="strong">not</emphasis> |
| 445 | # equal to one another. The message is optional.</para> |
| 446 | # </entry> |
| 447 | # </s:function> |
| 448 | #*/ |
| 449 | failNotEquals() |
| 450 | { |
| 451 | _shunit_shouldSkip && return ${SHUNIT_TRUE} |
| 452 | |
| 453 | _su_message='' |
| 454 | if [ $# -eq 3 ]; then |
| 455 | _su_message=$1 |
| 456 | shift |
| 457 | fi |
| 458 | _su_unexpected=${1:-} |
| 459 | _su_actual=${2:-} |
| 460 | |
| 461 | _shunit_testFailed "${_su_message:+${_su_message} }expected:<${_su_unexpected}> but was:<${_su_actual}>" |
| 462 | |
| 463 | unset _su_message _su_unexpected _su_actual |
| 464 | } |
| 465 | |
| 466 | #/** |
| 467 | # <s:function group="failures"> |
| 468 | # <entry align="right"> |
| 469 | # <emphasis>void</emphasis> |
| 470 | # </entry> |
| 471 | # <entry> |
| 472 | # <funcsynopsis> |
| 473 | # <funcprototype> |
| 474 | # <funcdef><function>failSame</function></funcdef> |
| 475 | # <paramdef>string <parameter>[message]</parameter></paramdef> |
| 476 | # </funcprototype> |
| 477 | # </funcsynopsis> |
| 478 | # <para>Indicate test failure because arguments were not the same. The |
| 479 | # message is optional.</para> |
| 480 | # </entry> |
| 481 | # </s:function> |
| 482 | #*/ |
| 483 | failSame() |
| 484 | { |
| 485 | _shunit_shouldSkip && return ${SHUNIT_TRUE} |
| 486 | |
| 487 | _su_message=${1:-} |
| 488 | |
| 489 | _shunit_testFailed "${_su_message:+${_su_message} }expected not same" |
| 490 | |
| 491 | unset _su_message |
| 492 | } |
| 493 | |
| 494 | #/** |
| 495 | # <s:function group="failures"> |
| 496 | # <entry align="right"> |
| 497 | # <emphasis>void</emphasis> |
| 498 | # </entry> |
| 499 | # <entry> |
| 500 | # <funcsynopsis> |
| 501 | # <funcprototype> |
| 502 | # <funcdef><function>failNotSame</function></funcdef> |
| 503 | # <paramdef>string <parameter>[message]</parameter></paramdef> |
| 504 | # <paramdef>string <parameter>expected</parameter></paramdef> |
| 505 | # <paramdef>string <parameter>actual</parameter></paramdef> |
| 506 | # </funcprototype> |
| 507 | # </funcsynopsis> |
| 508 | # <para>Fails the test if <emphasis>expected</emphasis> and |
| 509 | # <emphasis>actual</emphasis> are equal to one another. The message is |
| 510 | # optional.</para> |
| 511 | # </entry> |
| 512 | # </s:function> |
| 513 | #*/ |
| 514 | failNotSame() |
| 515 | { |
| 516 | failNotEquals "${@:-}" |
| 517 | } |
| 518 | |
| 519 | #----------------------------------------------------------------------------- |
| 520 | # skipping functions |
| 521 | # |
| 522 | |
| 523 | #/** |
| 524 | # <s:function group="skipping"> |
| 525 | # <entry align="right"> |
| 526 | # <emphasis>void</emphasis> |
| 527 | # </entry> |
| 528 | # <entry> |
| 529 | # <funcsynopsis> |
| 530 | # <funcprototype> |
| 531 | # <funcdef><function>startSkipping</function></funcdef> |
| 532 | # <paramdef /> |
| 533 | # </funcprototype> |
| 534 | # </funcsynopsis> |
| 535 | # <para>This function forces the remaining assert and fail functions to be |
| 536 | # "skipped", i.e. they will have no effect. Each function skipped will be |
| 537 | # recorded so that the total of asserts and fails will not be altered.</para> |
| 538 | # </entry> |
| 539 | # </s:function> |
| 540 | #*/ |
| 541 | startSkipping() |
| 542 | { |
| 543 | __shunit_skip=${SHUNIT_TRUE} |
| 544 | } |
| 545 | |
| 546 | #/** |
| 547 | # <s:function group="skipping"> |
| 548 | # <entry align="right"> |
| 549 | # <emphasis>void</emphasis> |
| 550 | # </entry> |
| 551 | # <entry> |
| 552 | # <funcsynopsis> |
| 553 | # <funcprototype> |
| 554 | # <funcdef><function>endSkipping</function></funcdef> |
| 555 | # <paramdef /> |
| 556 | # </funcprototype> |
| 557 | # </funcsynopsis> |
| 558 | # <para>This function returns calls to the assert and fail functions to their |
| 559 | # default behavior, i.e. they will be called.</para> |
| 560 | # </entry> |
| 561 | # </s:function> |
| 562 | #*/ |
| 563 | endSkipping() |
| 564 | { |
| 565 | __shunit_skip=${SHUNIT_FALSE} |
| 566 | } |
| 567 | |
| 568 | #/** |
| 569 | # <s:function group="skipping"> |
| 570 | # <entry align="right"> |
| 571 | # <emphasis>boolean</emphasis> |
| 572 | # </entry> |
| 573 | # <entry> |
| 574 | # <funcsynopsis> |
| 575 | # <funcprototype> |
| 576 | # <funcdef><function>isSkipping</function></funcdef> |
| 577 | # <paramdef /> |
| 578 | # </funcprototype> |
| 579 | # </funcsynopsis> |
| 580 | # <para>This function returns the state of skipping.</para> |
| 581 | # </entry> |
| 582 | # </s:function> |
| 583 | #*/ |
| 584 | isSkipping() |
| 585 | { |
| 586 | return ${__shunit_skip} |
| 587 | } |
| 588 | |
| 589 | #----------------------------------------------------------------------------- |
| 590 | # suite functions |
| 591 | # |
| 592 | |
| 593 | #/** |
| 594 | # <s:function group="suites"> |
| 595 | # <entry align="right"> |
| 596 | # <emphasis>void</emphasis> |
| 597 | # </entry> |
| 598 | # <entry> |
| 599 | # <funcsynopsis> |
| 600 | # <funcprototype> |
| 601 | # <funcdef><function>suite</function></funcdef> |
| 602 | # <paramdef /> |
| 603 | # </funcprototype> |
| 604 | # </funcsynopsis> |
| 605 | # <para>This function can be optionally overridden by the user in their test |
| 606 | # suite.</para> |
| 607 | # <para>If this function exists, it will be called when |
| 608 | # <command>shunit2</command> is sourced. If it does not exist, shUnit2 will |
| 609 | # search the parent script for all functions beginning with the word |
| 610 | # <literal>test</literal>, and they will be added dynamically to the test |
| 611 | # suite.</para> |
| 612 | # </entry> |
| 613 | # </s:function> |
| 614 | #*/ |
| 615 | # Note: see _shunit_mktempFunc() for actual implementation |
| 616 | # suite() { :; } |
| 617 | |
| 618 | #/** |
| 619 | # <s:function group="suites"> |
| 620 | # <entry align="right"> |
| 621 | # <emphasis>void</emphasis> |
| 622 | # </entry> |
| 623 | # <entry> |
| 624 | # <funcsynopsis> |
| 625 | # <funcprototype> |
| 626 | # <funcdef><function>suite_addTest</function></funcdef> |
| 627 | # <paramdef>string <parameter>function</parameter></paramdef> |
| 628 | # </funcprototype> |
| 629 | # </funcsynopsis> |
| 630 | # <para>This function adds a function name to the list of tests scheduled for |
| 631 | # execution as part of this test suite. This function should only be called |
| 632 | # from within the <function>suite()</function> function.</para> |
| 633 | # </entry> |
| 634 | # </s:function> |
| 635 | #*/ |
| 636 | suite_addTest() |
| 637 | { |
| 638 | _su_func=${1:-} |
| 639 | |
| 640 | __shunit_suite="${__shunit_suite:+${__shunit_suite} }${_su_func}" |
| 641 | |
| 642 | unset _su_func |
| 643 | } |
| 644 | |
| 645 | #/** |
| 646 | # <s:function group="suites"> |
| 647 | # <entry align="right"> |
| 648 | # <emphasis>void</emphasis> |
| 649 | # </entry> |
| 650 | # <entry> |
| 651 | # <funcsynopsis> |
| 652 | # <funcprototype> |
| 653 | # <funcdef><function>oneTimeSetUp</function></funcdef> |
| 654 | # <paramdef /> |
| 655 | # </funcprototype> |
| 656 | # </funcsynopsis> |
| 657 | # <para>This function can be be optionally overridden by the user in their |
| 658 | # test suite.</para> |
| 659 | # <para>If this function exists, it will be called once before any tests are |
| 660 | # run. It is useful to prepare a common environment for all tests.</para> |
| 661 | # </entry> |
| 662 | # </s:function> |
| 663 | #*/ |
| 664 | # Note: see _shunit_mktempFunc() for actual implementation |
| 665 | # oneTimeSetUp() { :; } |
| 666 | |
| 667 | #/** |
| 668 | # <s:function group="suites"> |
| 669 | # <entry align="right"> |
| 670 | # <emphasis>void</emphasis> |
| 671 | # </entry> |
| 672 | # <entry> |
| 673 | # <funcsynopsis> |
| 674 | # <funcprototype> |
| 675 | # <funcdef><function>oneTimeTearDown</function></funcdef> |
| 676 | # <paramdef /> |
| 677 | # </funcprototype> |
| 678 | # </funcsynopsis> |
| 679 | # <para>This function can be be optionally overridden by the user in their |
| 680 | # test suite.</para> |
| 681 | # <para>If this function exists, it will be called once after all tests are |
| 682 | # completed. It is useful to clean up the environment after all tests.</para> |
| 683 | # </entry> |
| 684 | # </s:function> |
| 685 | #*/ |
| 686 | # Note: see _shunit_mktempFunc() for actual implementation |
| 687 | # oneTimeTearDown() { :; } |
| 688 | |
| 689 | #/** |
| 690 | # <s:function group="suites"> |
| 691 | # <entry align="right"> |
| 692 | # <emphasis>void</emphasis> |
| 693 | # </entry> |
| 694 | # <entry> |
| 695 | # <funcsynopsis> |
| 696 | # <funcprototype> |
| 697 | # <funcdef><function>setUp</function></funcdef> |
| 698 | # <paramdef /> |
| 699 | # </funcprototype> |
| 700 | # </funcsynopsis> |
| 701 | # <para>This function can be be optionally overridden by the user in their |
| 702 | # test suite.</para> |
| 703 | # <para>If this function exists, it will be called before each test is run. |
| 704 | # It is useful to reset the environment before each test.</para> |
| 705 | # </entry> |
| 706 | # </s:function> |
| 707 | #*/ |
| 708 | # Note: see _shunit_mktempFunc() for actual implementation |
| 709 | # setUp() { :; } |
| 710 | |
| 711 | #/** |
| 712 | # <s:function group="suites"> |
| 713 | # <entry align="right"> |
| 714 | # <emphasis>void</emphasis> |
| 715 | # </entry> |
| 716 | # <entry> |
| 717 | # <funcsynopsis> |
| 718 | # <funcprototype> |
| 719 | # <funcdef><function>tearDown</function></funcdef> |
| 720 | # <paramdef /> |
| 721 | # </funcprototype> |
| 722 | # </funcsynopsis> |
| 723 | # <para>This function can be be optionally overridden by the user in their |
| 724 | # test suite.</para> |
| 725 | # <para>If this function exists, it will be called after each test completes. |
| 726 | # It is useful to clean up the environment after each test.</para> |
| 727 | # </entry> |
| 728 | # </s:function> |
| 729 | #*/ |
| 730 | # Note: see _shunit_mktempFunc() for actual implementation |
| 731 | # tearDown() { :; } |
| 732 | |
| 733 | #------------------------------------------------------------------------------ |
| 734 | # internal shUnit2 functions |
| 735 | # |
| 736 | |
| 737 | _shunit_cleanup() |
| 738 | { |
| 739 | name=$1 |
| 740 | |
| 741 | case ${name} in |
| 742 | EXIT) signal=0 ;; |
| 743 | INT) signal=2 ;; |
| 744 | TERM) signal=15 ;; |
| 745 | *) |
| 746 | _shunit_warn "unrecognized trap value (${name})" |
| 747 | signal=0 |
| 748 | ;; |
| 749 | esac |
| 750 | |
| 751 | # do our work |
| 752 | rm -fr "${__shunit_tmpDir}" |
| 753 | |
| 754 | # exit for all non-EXIT signals |
| 755 | if [ ${name} != 'EXIT' ]; then |
| 756 | _shunit_warn "trapped and now handling the (${name}) signal" |
| 757 | _shunit_generateReport |
| 758 | # disable EXIT trap |
| 759 | trap 0 |
| 760 | # add 127 to signal and exit |
| 761 | signal=`expr ${signal} + 127` |
| 762 | exit ${signal} |
| 763 | fi |
| 764 | } |
| 765 | |
| 766 | _shunit_execSuite() |
| 767 | { |
| 768 | echo '#' |
| 769 | echo '# Performing tests' |
| 770 | echo '#' |
| 771 | for _su_func in ${__shunit_suite}; do |
| 772 | # disable skipping |
| 773 | endSkipping |
| 774 | |
| 775 | # execute the per-test setup function |
| 776 | setUp |
| 777 | |
| 778 | # execute the test |
| 779 | echo "${_su_func}" |
| 780 | eval ${_su_func} |
| 781 | |
| 782 | # execute the per-test tear-down function |
| 783 | tearDown |
| 784 | done |
| 785 | |
| 786 | unset _su_func |
| 787 | } |
| 788 | |
| 789 | _shunit_functionExists() |
| 790 | { |
| 791 | _su__func=$1 |
| 792 | type ${_su__func} 2>/dev/null |grep "is a function$" >/dev/null |
| 793 | _su__return=$? |
| 794 | unset _su__func |
| 795 | return ${_su__return} |
| 796 | } |
| 797 | |
| 798 | _shunit_generateReport() |
| 799 | { |
| 800 | _su__awkPercent='{printf("%4d %3.0f%%", $1, $1*100/$2)}' |
| 801 | if [ ${__shunit_testsTotal:-0} -gt 0 ]; then |
| 802 | _su__passed=`echo ${__shunit_testsPassed} ${__shunit_testsTotal} |\ |
| 803 | awk "${_su__awkPercent}"` |
| 804 | _su__failed=`echo ${__shunit_testsFailed} ${__shunit_testsTotal} |\ |
| 805 | awk "${_su__awkPercent}"` |
| 806 | _su__skipped=`echo ${__shunit_testsSkipped} ${__shunit_testsTotal} |\ |
| 807 | awk "${_su__awkPercent}"` |
| 808 | _su__total=`echo ${__shunit_testsTotal} 100 |\ |
| 809 | awk '{printf("%4d %3d%%", $1, $2)}'` |
| 810 | else |
| 811 | _su__passed=`echo 0 0 |awk '{printf("%4d %3d%%", $1, $2)}'` |
| 812 | _su__failed=${_su__passed} |
| 813 | _su__skipped=${_su__passed} |
| 814 | _su__total=${_su__passed} |
| 815 | fi |
| 816 | |
| 817 | cat <<EOF |
| 818 | |
| 819 | # |
| 820 | # Test report |
| 821 | # |
| 822 | tests passed: ${_su__passed} |
| 823 | tests failed: ${_su__failed} |
| 824 | tests skipped: ${_su__skipped} |
| 825 | tests total: ${_su__total} |
| 826 | EOF |
| 827 | |
| 828 | unset _su__awkPercent _su__passed _su__failed _su__skipped _su__total |
| 829 | } |
| 830 | |
| 831 | # this function is a cross-platform temporary directory creation tool. not all |
| 832 | # OSes have the mktemp function, so one is included here. |
| 833 | _shunit_mktempDir() |
| 834 | { |
| 835 | # try the standard mktemp function |
| 836 | ( exec mktemp -dqt shunit.XXXXXX 2>/dev/null ) && return |
| 837 | |
| 838 | # the standard mktemp didn't work. doing our own. |
| 839 | if [ -r '/dev/urandom' ]; then |
| 840 | _su__random=`od -vAn -N4 -tx4 </dev/urandom |sed 's/^[^0-9a-f]*//'` |
| 841 | elif [ -n "${RANDOM:-}" ]; then |
| 842 | # $RANDOM works |
| 843 | _su__random=${RANDOM}${RANDOM}${RANDOM}$$ |
| 844 | else |
| 845 | # $RANDOM doesn't work |
| 846 | _su__date=`date '+%Y%m%d%H%M%S'` |
| 847 | _su__random=`expr ${_su__date} / $$` |
| 848 | fi |
| 849 | |
| 850 | _su__tmpDir="${TMPDIR-/tmp}/shunit.${_su__random}" |
| 851 | ( umask 077 && mkdir "${_su__tmpDir}" ) || { |
| 852 | echo 'shUnit:FATAL could not create temporary directory! exiting' >&2 |
| 853 | exit 1 |
| 854 | } |
| 855 | |
| 856 | echo ${_su__tmpDir} |
| 857 | unset _su__date _su__random _su__tmpDir |
| 858 | } |
| 859 | |
| 860 | # this function is here to work around issues in Cygwin |
| 861 | _shunit_mktempFunc() |
| 862 | { |
| 863 | for _su__func in oneTimeSetUp oneTimeTearDown setUp tearDown suite; do |
| 864 | _su__file="${__shunit_tmpDir}/${_su__func}" |
| 865 | cat <<EOF >"${_su__file}" |
| 866 | #! /bin/sh |
| 867 | exit 0 |
| 868 | EOF |
| 869 | chmod +x "${_su__file}" |
| 870 | done |
| 871 | |
| 872 | unset _su__file |
| 873 | } |
| 874 | |
| 875 | _shunit_shouldSkip() |
| 876 | { |
| 877 | [ ${__shunit_skip} -eq ${SHUNIT_FALSE} ] && return ${SHUNIT_FALSE} |
| 878 | _shunit_testSkipped |
| 879 | } |
| 880 | |
| 881 | _shunit_testPassed() |
| 882 | { |
| 883 | __shunit_testsPassed=`expr ${__shunit_testsPassed} + 1` |
| 884 | __shunit_testsTotal=`expr ${__shunit_testsTotal} + 1` |
| 885 | } |
| 886 | |
| 887 | _shunit_testFailed() |
| 888 | { |
| 889 | _su__msg=$1 |
| 890 | |
| 891 | __shunit_testsFailed=`expr ${__shunit_testsFailed} + 1` |
| 892 | __shunit_testsTotal=`expr ${__shunit_testsTotal} + 1` |
| 893 | echo "${__SHUNIT_ASSERT_MSG_PREFIX}${_su__msg}" >&2 |
| 894 | |
| 895 | unset _su__msg |
| 896 | } |
| 897 | |
| 898 | _shunit_testSkipped() |
| 899 | { |
| 900 | __shunit_testsSkipped=`expr ${__shunit_testsSkipped} + 1` |
| 901 | __shunit_testsTotal=`expr ${__shunit_testsTotal} + 1` |
| 902 | } |
| 903 | |
| 904 | #------------------------------------------------------------------------------ |
| 905 | # main |
| 906 | # |
| 907 | |
| 908 | # create a temporary storage location |
| 909 | __shunit_tmpDir=`_shunit_mktempDir` |
| 910 | |
| 911 | # setup traps to clean up after ourselves |
| 912 | trap '_shunit_cleanup EXIT' 0 |
| 913 | trap '_shunit_cleanup INT' 2 |
| 914 | trap '_shunit_cleanup TERM' 15 |
| 915 | |
| 916 | # create phantom functions to work around issues with Cygwin |
| 917 | _shunit_mktempFunc |
| 918 | PATH="${__shunit_tmpDir}:${PATH}" |
| 919 | |
| 920 | # execute the oneTimeSetUp function (if it exists) |
| 921 | #_shunit_functionExists oneTimeSetUp && oneTimeSetUp |
| 922 | oneTimeSetUp |
| 923 | |
| 924 | # execute the suite function defined in the parent test script |
| 925 | # deprecated as of 2.1.0 |
| 926 | suite |
| 927 | |
| 928 | # if no suite function was defined, dynamically build a list of functions |
| 929 | if [ -z "${__shunit_suite}" ]; then |
| 930 | funcs=`grep "^[ \t]*test[A-Za-z0-9_]* *()" ${__SHUNIT_PARENT} \ |
| 931 | |sed 's/[^A-Za-z0-9_]//g'` |
| 932 | for func in ${funcs}; do |
| 933 | suite_addTest ${func} |
| 934 | done |
| 935 | fi |
| 936 | |
| 937 | # execute the tests |
| 938 | _shunit_execSuite |
| 939 | |
| 940 | # execute the oneTimeTearDown function (if it exists) |
| 941 | oneTimeTearDown |
| 942 | |
| 943 | # generate report |
| 944 | _shunit_generateReport |
| 945 | |
| 946 | # restore the previous set of shell flags |
| 947 | for _shunit_shellFlag in ${__SHUNIT_SHELL_FLAGS}; do |
| 948 | echo ${_shunit_shellFlags} |grep ${_shunit_shellFlag} >/dev/null \ |
| 949 | || set +${_shunit_shellFlag} |
| 950 | done |
| 951 | unset _shunit_shellFlag _shunit_shellFlags |
| 952 | |
| 953 | [ ${__shunit_testsFailed} -eq 0 ] || exit 1 |
| 954 | |
| 955 | #/** |
| 956 | # </s:shelldoc> |
| 957 | #*/ |