kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 1 | # $Id: shunit2 296 2010-03-17 23:49:03Z kate.ward@forestent.com $ |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 2 | # vim:et:ft=sh:sts=2:sw=2 |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 3 | # |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 4 | # Copyright 2008 Kate Ward. All Rights Reserved. |
| 5 | # Released under the LGPL (GNU Lesser General Public License) |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 6 | # |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 7 | # shUnit2 -- Unit testing framework for Unix shell scripts. |
| 8 | # http://code.google.com/p/shunit2/ |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 9 | # |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 10 | # Author: kate.ward@forestent.com (Kate Ward) |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 11 | # |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 12 | # 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.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 14 | |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 15 | SHUNIT_VERSION='2.1.6pre' |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 16 | |
| 17 | SHUNIT_TRUE=0 |
| 18 | SHUNIT_FALSE=1 |
| 19 | SHUNIT_ERROR=2 |
| 20 | |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 21 | _shunit_warn() { echo "shunit2:WARN $@" >&2; } |
| 22 | _shunit_error() { echo "shunit2:ERROR $@" >&2; } |
| 23 | _shunit_fatal() { echo "shunit2:FATAL $@" >&2; } |
| 24 | |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 25 | # specific shell checks |
| 26 | if [ -n "${ZSH_VERSION:-}" ]; then |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 27 | setopt |grep "^shwordsplit$" >/dev/null |
| 28 | if [ $? -ne ${SHUNIT_TRUE} ]; then |
| 29 | _shunit_fatal 'zsh shwordsplit option is required for proper operation' |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 30 | 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 |
| 37 | fi |
| 38 | |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 39 | # |
| 40 | # constants |
| 41 | # |
| 42 | |
| 43 | __SHUNIT_ASSERT_MSG_PREFIX='ASSERT:' |
| 44 | __SHUNIT_PARENT=${SHUNIT_PARENT:-$0} |
| 45 | |
| 46 | # set the constants readonly |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 47 | shunit_constants_=`set |grep '^__SHUNIT_' |cut -d= -f1` |
| 48 | echo "${shunit_constants_}" |grep '^Binary file' >/dev/null \ |
| 49 | && shunit_constants_=`set |grep -a '^__SHUNIT_' |cut -d= -f1` |
| 50 | for shunit_constant_ in ${shunit_constants_}; do |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 51 | shunit_ro_opts_='' |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 52 | 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.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 58 | done |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 59 | unset shunit_constant_ shunit_constants_ shunit_ro_opts_ |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 60 | |
| 61 | # variables |
| 62 | __shunit_skip=${SHUNIT_FALSE} |
| 63 | __shunit_suite='' |
| 64 | |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 65 | # counts of tests |
| 66 | __shunit_testSuccess=${SHUNIT_TRUE} |
| 67 | __shunit_testsTotal=0 |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 68 | __shunit_testsPassed=0 |
| 69 | __shunit_testsFailed=0 |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 70 | |
| 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.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 79 | |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 80 | # macros |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 81 | _SHUNIT_LINENO_='eval __shunit_lineno=""; if [ "${1:-}" = "--lineno" ]; then [ -n "$2" ] && __shunit_lineno="[$2] "; shift 2; fi' |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 82 | |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 83 | #----------------------------------------------------------------------------- |
| 84 | # assert functions |
| 85 | # |
| 86 | |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 87 | # 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.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 95 | assertEquals() |
| 96 | { |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 97 | ${_SHUNIT_LINENO_} |
| 98 | if [ $# -lt 2 -o $# -gt 3 ]; then |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 99 | _shunit_error "assertEquals() requires two or three arguments; $# given" |
| 100 | _shunit_error "1: ${1:+$1} 2: ${2:+$2} 3: ${3:+$3}" |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 101 | return ${SHUNIT_ERROR} |
| 102 | fi |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 103 | _shunit_shouldSkip && return ${SHUNIT_TRUE} |
| 104 | |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 105 | shunit_message_=${__shunit_lineno} |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 106 | if [ $# -eq 3 ]; then |
| 107 | shunit_message_="${shunit_message_}$1" |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 108 | shift |
| 109 | fi |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 110 | shunit_expected_=$1 |
| 111 | shunit_actual_=$2 |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 112 | |
| 113 | shunit_return=${SHUNIT_TRUE} |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 114 | if [ "${shunit_expected_}" = "${shunit_actual_}" ]; then |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 115 | _shunit_assertPass |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 116 | else |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 117 | failNotEquals "${shunit_message_}" "${shunit_expected_}" "${shunit_actual_}" |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 118 | shunit_return=${SHUNIT_FALSE} |
| 119 | fi |
| 120 | |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 121 | unset shunit_message_ shunit_expected_ shunit_actual_ |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 122 | return ${shunit_return} |
| 123 | } |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 124 | _ASSERT_EQUALS_='eval assertEquals --lineno "${LINENO:-}"' |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 125 | |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 126 | # 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.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 134 | assertNotEquals() |
| 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.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 164 | # 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.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 171 | assertNull() |
| 172 | { |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 173 | ${_SHUNIT_LINENO_} |
| 174 | if [ $# -lt 1 -o $# -gt 2 ]; then |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 175 | _shunit_error "assertNull() requires one or two arguments; $# given" |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 176 | return ${SHUNIT_ERROR} |
| 177 | fi |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 178 | _shunit_shouldSkip && return ${SHUNIT_TRUE} |
| 179 | |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 180 | shunit_message_=${__shunit_lineno} |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 181 | if [ $# -eq 2 ]; then |
| 182 | shunit_message_="${shunit_message_}$1" |
| 183 | shift |
| 184 | fi |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 185 | assertTrue "${shunit_message_}" "[ -z '$1' ]" |
| 186 | shunit_return=$? |
| 187 | |
| 188 | unset shunit_message_ |
| 189 | return ${shunit_return} |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 190 | } |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 191 | _ASSERT_NULL_='eval assertNull --lineno "${LINENO:-}"' |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 192 | |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 193 | # 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.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 200 | assertNotNull() |
| 201 | { |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 202 | ${_SHUNIT_LINENO_} |
| 203 | if [ $# -gt 2 ]; then # allowing 0 arguments as $1 might actually be null |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 204 | _shunit_error "assertNotNull() requires one or two arguments; $# given" |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 205 | return ${SHUNIT_ERROR} |
| 206 | fi |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 207 | _shunit_shouldSkip && return ${SHUNIT_TRUE} |
| 208 | |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 209 | shunit_message_=${__shunit_lineno} |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 210 | if [ $# -eq 2 ]; then |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 211 | shunit_message_="${shunit_message_}$1" |
| 212 | shift |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 213 | fi |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 214 | assertTrue "${shunit_message_}" "[ -n '${1:-}' ]" |
| 215 | shunit_return=$? |
| 216 | |
| 217 | unset shunit_message_ |
| 218 | return ${shunit_return} |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 219 | } |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 220 | _ASSERT_NOT_NULL_='eval assertNotNull --lineno "${LINENO:-}"' |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 221 | |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 222 | # 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.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 230 | assertSame() |
| 231 | { |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 232 | ${_SHUNIT_LINENO_} |
| 233 | if [ $# -lt 2 -o $# -gt 3 ]; then |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 234 | _shunit_error "assertSame() requires one or two arguments; $# given" |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 235 | return ${SHUNIT_ERROR} |
| 236 | fi |
| 237 | _shunit_shouldSkip && return ${SHUNIT_TRUE} |
| 238 | |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 239 | shunit_message_=${__shunit_lineno} |
| 240 | if [ $# -eq 3 ]; then |
| 241 | shunit_message_="${shunit_message_}$1" |
| 242 | shift |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 243 | fi |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 244 | assertEquals "${shunit_message_}" "$1" "$2" |
| 245 | shunit_return=$? |
| 246 | |
| 247 | unset shunit_message_ |
| 248 | return ${shunit_return} |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 249 | } |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 250 | _ASSERT_SAME_='eval assertSame --lineno "${LINENO:-}"' |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 251 | |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 252 | # 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.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 260 | assertNotSame() |
| 261 | { |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 262 | ${_SHUNIT_LINENO_} |
| 263 | if [ $# -lt 2 -o $# -gt 3 ]; then |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 264 | _shunit_error "assertNotSame() requires two or three arguments; $# given" |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 265 | return ${SHUNIT_ERROR} |
| 266 | fi |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 267 | _shunit_shouldSkip && return ${SHUNIT_TRUE} |
| 268 | |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 269 | shunit_message_=${__shunit_lineno} |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 270 | if [ $# -eq 3 ]; then |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 271 | shunit_message_="${shunit_message_:-}$1" |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 272 | shift |
| 273 | fi |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 274 | assertNotEquals "${shunit_message_}" "$1" "$2" |
| 275 | shunit_return=$? |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 276 | |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 277 | unset shunit_message_ |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 278 | return ${shunit_return} |
| 279 | } |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 280 | _ASSERT_NOT_SAME_='eval assertNotSame --lineno "${LINENO:-}"' |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 281 | |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 282 | # 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.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 303 | assertTrue() |
| 304 | { |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 305 | ${_SHUNIT_LINENO_} |
| 306 | if [ $# -gt 2 ]; then |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 307 | _shunit_error "assertTrue() takes one two arguments; $# given" |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 308 | return ${SHUNIT_ERROR} |
| 309 | fi |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 310 | _shunit_shouldSkip && return ${SHUNIT_TRUE} |
| 311 | |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 312 | shunit_message_=${__shunit_lineno} |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 313 | if [ $# -eq 2 ]; then |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 314 | shunit_message_="${shunit_message_}$1" |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 315 | shift |
| 316 | fi |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 317 | shunit_condition_=$1 |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 318 | |
| 319 | # see if condition is an integer, i.e. a return value |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 320 | shunit_match_=`expr "${shunit_condition_}" : '\([0-9]*\)'` |
| 321 | shunit_return=${SHUNIT_TRUE} |
| 322 | if [ -z "${shunit_condition_}" ]; then |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 323 | # null condition |
| 324 | shunit_return=${SHUNIT_FALSE} |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 325 | elif [ "${shunit_condition_}" = "${shunit_match_}" ]; then |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 326 | # possible return value. treating 0 as true, and non-zero as false. |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 327 | [ ${shunit_condition_} -ne 0 ] && shunit_return=${SHUNIT_FALSE} |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 328 | else |
| 329 | # (hopefully) a condition |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 330 | ( eval ${shunit_condition_} ) >/dev/null 2>&1 |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 331 | [ $? -ne 0 ] && shunit_return=${SHUNIT_FALSE} |
| 332 | fi |
| 333 | |
| 334 | # record the test |
| 335 | if [ ${shunit_return} -eq ${SHUNIT_TRUE} ]; then |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 336 | _shunit_assertPass |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 337 | else |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 338 | _shunit_assertFail "${shunit_message_}" |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 339 | fi |
| 340 | |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 341 | unset shunit_message_ shunit_condition_ shunit_match_ |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 342 | return ${shunit_return} |
| 343 | } |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 344 | _ASSERT_TRUE_='eval assertTrue --lineno "${LINENO:-}"' |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 345 | |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 346 | # 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.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 367 | assertFalse() |
| 368 | { |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 369 | ${_SHUNIT_LINENO_} |
| 370 | if [ $# -lt 1 -o $# -gt 2 ]; then |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 371 | _shunit_error "assertFalse() quires one or two arguments; $# given" |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 372 | return ${SHUNIT_ERROR} |
| 373 | fi |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 374 | _shunit_shouldSkip && return ${SHUNIT_TRUE} |
| 375 | |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 376 | shunit_message_=${__shunit_lineno} |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 377 | if [ $# -eq 2 ]; then |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 378 | shunit_message_="${shunit_message_}$1" |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 379 | shift |
| 380 | fi |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 381 | shunit_condition_=$1 |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 382 | |
| 383 | # see if condition is an integer, i.e. a return value |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 384 | shunit_match_=`expr "${shunit_condition_}" : '\([0-9]*\)'` |
| 385 | shunit_return=${SHUNIT_TRUE} |
| 386 | if [ -z "${shunit_condition_}" ]; then |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 387 | # null condition |
| 388 | shunit_return=${SHUNIT_FALSE} |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 389 | elif [ "${shunit_condition_}" = "${shunit_match_}" ]; then |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 390 | # possible return value. treating 0 as true, and non-zero as false. |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 391 | [ ${shunit_condition_} -eq 0 ] && shunit_return=${SHUNIT_FALSE} |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 392 | else |
| 393 | # (hopefully) a condition |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 394 | ( eval ${shunit_condition_} ) >/dev/null 2>&1 |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 395 | [ $? -eq 0 ] && shunit_return=${SHUNIT_FALSE} |
| 396 | fi |
| 397 | |
| 398 | # record the test |
| 399 | if [ ${shunit_return} -eq ${SHUNIT_TRUE} ]; then |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 400 | _shunit_assertPass |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 401 | else |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 402 | _shunit_assertFail "${shunit_message_}" |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 403 | fi |
| 404 | |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 405 | unset shunit_message_ shunit_condition_ shunit_match_ |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 406 | return ${shunit_return} |
| 407 | } |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 408 | _ASSERT_FALSE_='eval assertFalse --lineno "${LINENO:-}"' |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 409 | |
| 410 | #----------------------------------------------------------------------------- |
| 411 | # failure functions |
| 412 | # |
| 413 | |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 414 | # Records a test failure. |
| 415 | # |
| 416 | # Args: |
| 417 | # message: string: failure message [optional] |
| 418 | # Returns: |
| 419 | # integer: success (TRUE/FALSE/ERROR constant) |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 420 | fail() |
| 421 | { |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 422 | ${_SHUNIT_LINENO_} |
| 423 | if [ $# -gt 1 ]; then |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 424 | _shunit_error "fail() requires zero or one arguments; $# given" |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 425 | return ${SHUNIT_ERROR} |
| 426 | fi |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 427 | _shunit_shouldSkip && return ${SHUNIT_TRUE} |
| 428 | |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 429 | shunit_message_=${__shunit_lineno} |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 430 | if [ $# -eq 1 ]; then |
| 431 | shunit_message_="${shunit_message_}$1" |
| 432 | shift |
| 433 | fi |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 434 | |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 435 | _shunit_assertFail "${shunit_message_}" |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 436 | |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 437 | unset shunit_message_ |
| 438 | return ${SHUNIT_FALSE} |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 439 | } |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 440 | _FAIL_='eval fail --lineno "${LINENO:-}"' |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 441 | |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 442 | # 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.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 450 | failNotEquals() |
| 451 | { |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 452 | ${_SHUNIT_LINENO_} |
| 453 | if [ $# -lt 2 -o $# -gt 3 ]; then |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 454 | _shunit_error "failNotEquals() requires one or two arguments; $# given" |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 455 | return ${SHUNIT_ERROR} |
| 456 | fi |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 457 | _shunit_shouldSkip && return ${SHUNIT_TRUE} |
| 458 | |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 459 | shunit_message_=${__shunit_lineno} |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 460 | if [ $# -eq 3 ]; then |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 461 | shunit_message_="${shunit_message_}$1" |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 462 | shift |
| 463 | fi |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 464 | shunit_unexpected_=$1 |
| 465 | shunit_actual_=$2 |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 466 | |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 467 | _shunit_assertFail "${shunit_message_:+${shunit_message_} }expected:<${shunit_unexpected_}> but was:<${shunit_actual_}>" |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 468 | |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 469 | unset shunit_message_ shunit_unexpected_ shunit_actual_ |
| 470 | return ${SHUNIT_FALSE} |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 471 | } |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 472 | _FAIL_NOT_EQUALS_='eval failNotEquals --lineno "${LINENO:-}"' |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 473 | |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 474 | # 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.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 482 | failSame() |
| 483 | { |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 484 | ${_SHUNIT_LINENO_} |
| 485 | if [ $# -lt 2 -o $# -gt 3 ]; then |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 486 | _shunit_error "failSame() requires two or three arguments; $# given" |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 487 | return ${SHUNIT_ERROR} |
| 488 | fi |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 489 | _shunit_shouldSkip && return ${SHUNIT_TRUE} |
| 490 | |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 491 | shunit_message_=${__shunit_lineno} |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 492 | if [ $# -eq 3 ]; then |
| 493 | shunit_message_="${shunit_message_}$1" |
| 494 | shift |
| 495 | fi |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 496 | |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 497 | _shunit_assertFail "${shunit_message_:+${shunit_message_} }expected not same" |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 498 | |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 499 | unset shunit_message_ |
| 500 | return ${SHUNIT_FALSE} |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 501 | } |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 502 | _FAIL_SAME_='eval failSame --lineno "${LINENO:-}"' |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 503 | |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 504 | # 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.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 514 | failNotSame() |
| 515 | { |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 516 | ${_SHUNIT_LINENO_} |
| 517 | if [ $# -lt 2 -o $# -gt 3 ]; then |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 518 | _shunit_error "failNotEquals() requires one or two arguments; $# given" |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 519 | return ${SHUNIT_ERROR} |
| 520 | fi |
| 521 | _shunit_shouldSkip && return ${SHUNIT_TRUE} |
| 522 | |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 523 | shunit_message_=${__shunit_lineno} |
| 524 | if [ $# -eq 3 ]; then |
| 525 | shunit_message_="${shunit_message_}$1" |
| 526 | shift |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 527 | fi |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 528 | failNotEquals "${shunit_message_}" "$1" "$2" |
| 529 | shunit_return=$? |
| 530 | |
| 531 | unset shunit_message_ |
| 532 | return ${shunit_return} |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 533 | } |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 534 | _FAIL_NOT_SAME_='eval failNotSame --lineno "${LINENO:-}"' |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 535 | |
| 536 | #----------------------------------------------------------------------------- |
| 537 | # skipping functions |
| 538 | # |
| 539 | |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 540 | # 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.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 548 | startSkipping() |
| 549 | { |
| 550 | __shunit_skip=${SHUNIT_TRUE} |
| 551 | } |
| 552 | |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 553 | # Resume the normal recording behavior of assert and fail calls. |
| 554 | # |
| 555 | # Args: |
| 556 | # None |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 557 | endSkipping() |
| 558 | { |
| 559 | __shunit_skip=${SHUNIT_FALSE} |
| 560 | } |
| 561 | |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 562 | # Returns the state of assert and fail call skipping. |
| 563 | # |
| 564 | # Args: |
| 565 | # None |
| 566 | # Returns: |
| 567 | # boolean: (TRUE/FALSE constant) |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 568 | isSkipping() |
| 569 | { |
| 570 | return ${__shunit_skip} |
| 571 | } |
| 572 | |
| 573 | #----------------------------------------------------------------------------- |
| 574 | # suite functions |
| 575 | # |
| 576 | |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 577 | # 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.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 589 | # Note: see _shunit_mktempFunc() for actual implementation |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 590 | # |
| 591 | # Args: |
| 592 | # None |
| 593 | #suite() { :; } # DO NOT UNCOMMENT THIS FUNCTION |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 594 | |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 595 | # 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.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 601 | suite_addTest() |
| 602 | { |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 603 | shunit_func_=${1:-} |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 604 | |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 605 | __shunit_suite="${__shunit_suite:+${__shunit_suite} }${shunit_func_}" |
| 606 | __shunit_testsTotal=`expr ${__shunit_testsTotal} + 1` |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 607 | |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 608 | unset shunit_func_ |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 609 | } |
| 610 | |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 611 | # 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.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 617 | # Note: see _shunit_mktempFunc() for actual implementation |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 618 | # |
| 619 | # Args: |
| 620 | # None |
| 621 | #oneTimeSetUp() { :; } # DO NOT UNCOMMENT THIS FUNCTION |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 622 | |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 623 | # 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.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 629 | # Note: see _shunit_mktempFunc() for actual implementation |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 630 | # |
| 631 | # Args: |
| 632 | # None |
| 633 | #oneTimeTearDown() { :; } # DO NOT UNCOMMENT THIS FUNCTION |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 634 | |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 635 | # 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.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 640 | # Note: see _shunit_mktempFunc() for actual implementation |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 641 | # |
| 642 | # Args: |
| 643 | # None |
| 644 | #setUp() { :; } |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 645 | |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 646 | # Note: see _shunit_mktempFunc() for actual implementation |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 647 | # 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.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 657 | |
| 658 | #------------------------------------------------------------------------------ |
| 659 | # internal shUnit2 functions |
| 660 | # |
| 661 | |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 662 | # 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.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 665 | # OSes have the mktemp function, so one is included here. |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 666 | # |
| 667 | # Args: |
| 668 | # None |
| 669 | # Outputs: |
| 670 | # string: the temporary directory that was created |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 671 | _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.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 677 | 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.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 680 | elif [ -n "${RANDOM:-}" ]; then |
| 681 | # $RANDOM works |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 682 | _shunit_random_=${RANDOM}${RANDOM}${RANDOM}$$ |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 683 | else |
| 684 | # $RANDOM doesn't work |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 685 | _shunit_date_=`date '+%Y%m%d%H%M%S'` |
| 686 | _shunit_random_=`expr ${_shunit_date_} / $$` |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 687 | fi |
| 688 | |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 689 | _shunit_tmpDir_="${TMPDIR:-/tmp}/shunit.${_shunit_random_}" |
| 690 | ( umask 077 && mkdir "${_shunit_tmpDir_}" ) || { |
| 691 | _shunit_fatal 'could not create temporary directory! exiting' |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 692 | return ${SHUNIT_ERROR} |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 693 | } |
| 694 | |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 695 | echo ${_shunit_tmpDir_} |
| 696 | unset _shunit_date_ _shunit_random_ _shunit_tmpDir_ |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 697 | } |
| 698 | |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 699 | # This function is here to work around issues in Cygwin |
| 700 | # |
| 701 | # Args: |
| 702 | # None |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 703 | _shunit_mktempFunc() |
| 704 | { |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 705 | for _shunit_func_ in oneTimeSetUp oneTimeTearDown setUp tearDown suite; do |
| 706 | _shunit_file_="${__shunit_tmpDir}/${_shunit_func_}" |
| 707 | cat <<EOF >"${_shunit_file_}" |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 708 | #! /bin/sh |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 709 | exit ${SHUNIT_TRUE} |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 710 | EOF |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 711 | chmod +x "${_shunit_file_}" |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 712 | done |
| 713 | |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 714 | unset _shunit_file_ |
| 715 | } |
| 716 | |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 717 | # 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.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 725 | _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.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 759 | # |
| 760 | # Args: |
| 761 | # None |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 762 | _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.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 791 | # 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.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 797 | _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.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 836 | } |
| 837 | |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 838 | # 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.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 844 | _shunit_shouldSkip() |
| 845 | { |
| 846 | [ ${__shunit_skip} -eq ${SHUNIT_FALSE} ] && return ${SHUNIT_FALSE} |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 847 | _shunit_assertSkip |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 848 | } |
| 849 | |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 850 | # Records a successful test. |
| 851 | # |
| 852 | # Args: |
| 853 | # None |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 854 | _shunit_assertPass() |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 855 | { |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 856 | __shunit_assertsPassed=`expr ${__shunit_assertsPassed} + 1` |
| 857 | __shunit_assertsTotal=`expr ${__shunit_assertsTotal} + 1` |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 858 | } |
| 859 | |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 860 | # Records a test failure. |
| 861 | # |
| 862 | # Args: |
| 863 | # message: string: failure message to provide user |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 864 | _shunit_assertFail() |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 865 | { |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 866 | _shunit_msg_=$1 |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 867 | |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 868 | __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.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 872 | |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 873 | unset _shunit_msg_ |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 874 | } |
| 875 | |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 876 | # Records a skipped test. |
| 877 | # |
| 878 | # Args: |
| 879 | # None |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 880 | _shunit_assertSkip() |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 881 | { |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 882 | __shunit_assertsSkipped=`expr ${__shunit_assertsSkipped} + 1` |
| 883 | __shunit_assertsTotal=`expr ${__shunit_assertsTotal} + 1` |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | #------------------------------------------------------------------------------ |
| 887 | # main |
| 888 | # |
| 889 | |
| 890 | # create a temporary storage location |
kate.ward | 9a183b8 | 2010-03-18 00:25:34 +0000 | [diff] [blame^] | 891 | __shunit_tmpDir=`_shunit_mktempDir` || exit ${SHUNIT_ERROR} |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 892 | |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 893 | # provide a public temporary directory for unit test scripts |
| 894 | # TODO(kward): document this |
| 895 | shunit_tmpDir="${__shunit_tmpDir}/tmp" |
| 896 | mkdir "${shunit_tmpDir}" |
| 897 | |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 898 | # setup traps to clean up after ourselves |
| 899 | trap '_shunit_cleanup EXIT' 0 |
| 900 | trap '_shunit_cleanup INT' 2 |
| 901 | trap '_shunit_cleanup TERM' 15 |
| 902 | |
| 903 | # create phantom functions to work around issues with Cygwin |
| 904 | _shunit_mktempFunc |
| 905 | PATH="${__shunit_tmpDir}:${PATH}" |
| 906 | |
| 907 | # execute the oneTimeSetUp function (if it exists) |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 908 | oneTimeSetUp |
| 909 | |
| 910 | # execute the suite function defined in the parent test script |
| 911 | # deprecated as of 2.1.0 |
| 912 | suite |
| 913 | |
| 914 | # if no suite function was defined, dynamically build a list of functions |
| 915 | if [ -z "${__shunit_suite}" ]; then |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 916 | shunit_funcs_=`grep "^[ \t]*test[A-Za-z0-9_]* *()" ${__SHUNIT_PARENT} \ |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 917 | |sed 's/[^A-Za-z0-9_]//g'` |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 918 | for shunit_func_ in ${shunit_funcs_}; do |
| 919 | suite_addTest ${shunit_func_} |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 920 | done |
| 921 | fi |
kate.ward | f32fb4f | 2008-07-11 15:20:38 +0000 | [diff] [blame] | 922 | unset shunit_func_ shunit_funcs_ |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 923 | |
| 924 | # execute the tests |
| 925 | _shunit_execSuite |
| 926 | |
| 927 | # execute the oneTimeTearDown function (if it exists) |
| 928 | oneTimeTearDown |
| 929 | |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 930 | # generate the report |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 931 | _shunit_generateReport |
| 932 | |
kate.ward | 3809767 | 2008-11-03 20:58:42 +0000 | [diff] [blame] | 933 | # that's it folks |
| 934 | [ ${__shunit_testsFailed} -eq 0 ] |
| 935 | exit $? |