2 # $Id: shunit2_test_misc.sh 322 2011-04-24 00:09:45Z kate.ward@forestent.com $
3 # vim:et:ft=sh:sts=2:sw=2
5 # Copyright 2008 Kate Ward. All Rights Reserved.
6 # Released under the LGPL (GNU Lesser General Public License)
8 # Author: kate.ward@forestent.com (Kate Ward)
10 # shUnit2 unit tests of miscellaneous things
13 . ./shunit2_test_helpers
15 #------------------------------------------------------------------------------
19 # Note: the test script is prefixed with '#' chars so that shUnit2 does not
20 # incorrectly interpret the embedded functions as real functions.
23 sed 's/^#//' >"${unittestF}" <<EOF
24 ## treat unset variables as an error when performing parameter expansion
27 #boom() { x=\$1; } # this function goes boom if no parameters are passed!
31 # boom # No parameter given
36 ( exec ${SHUNIT_SHELL:-sh} "${unittestF}" >"${stdoutF}" 2>"${stderrF}" )
37 assertFalse 'expected a non-zero exit value' $?
38 grep '^ASSERT:Unknown failure' "${stdoutF}" >/dev/null
39 assertTrue 'assert message was not generated' $?
40 grep '^Ran [0-9]* test' "${stdoutF}" >/dev/null
41 assertTrue 'test count message was not generated' $?
42 grep '^FAILED' "${stdoutF}" >/dev/null
43 assertTrue 'failure message was not generated' $?
48 ( assertEquals 'Some message.' 1 2 >"${stdoutF}" 2>"${stderrF}" )
49 diff "${stdoutF}" - >/dev/null <<EOF
50 ASSERT:Some message. expected:<1> but was:<2>
53 assertEquals ${SHUNIT_TRUE} ${rtrn}
54 [ ${rtrn} -ne ${SHUNIT_TRUE} ] && cat "${stderrF}" >&2
59 assertEquals '/abc' `_shunit_prepForSourcing '/abc'`
60 assertEquals './abc' `_shunit_prepForSourcing './abc'`
61 assertEquals './abc' `_shunit_prepForSourcing 'abc'`
66 actual=`_shunit_escapeCharInStr '\' ''`
67 assertEquals '' "${actual}"
68 assertEquals 'abc\\' `_shunit_escapeCharInStr '\' 'abc\'`
69 assertEquals 'abc\\def' `_shunit_escapeCharInStr '\' 'abc\def'`
70 assertEquals '\\def' `_shunit_escapeCharInStr '\' '\def'`
72 actual=`_shunit_escapeCharInStr '"' ''`
73 assertEquals '' "${actual}"
74 assertEquals 'abc\"' `_shunit_escapeCharInStr '"' 'abc"'`
75 assertEquals 'abc\"def' `_shunit_escapeCharInStr '"' 'abc"def'`
76 assertEquals '\"def' `_shunit_escapeCharInStr '"' '"def'`
78 actual=`_shunit_escapeCharInStr '$' ''`
79 assertEquals '' "${actual}"
80 assertEquals 'abc\$' `_shunit_escapeCharInStr '$' 'abc$'`
81 assertEquals 'abc\$def' `_shunit_escapeCharInStr '$' 'abc$def'`
82 assertEquals '\$def' `_shunit_escapeCharInStr '$' '$def'`
84 # actual=`_shunit_escapeCharInStr "'" ''`
85 # assertEquals '' "${actual}"
86 # assertEquals "abc\\'" `_shunit_escapeCharInStr "'" "abc'"`
87 # assertEquals "abc\\'def" `_shunit_escapeCharInStr "'" "abc'def"`
88 # assertEquals "\\'def" `_shunit_escapeCharInStr "'" "'def"`
90 # # must put the backtick in a variable so the shell doesn't misinterpret it
91 # # while inside a backticked sequence (e.g. `echo '`'` would fail).
93 # actual=`_shunit_escapeCharInStr ${backtick} ''`
94 # assertEquals '' "${actual}"
95 # assertEquals '\`abc' \
96 # `_shunit_escapeCharInStr "${backtick}" ${backtick}'abc'`
97 # assertEquals 'abc\`' \
98 # `_shunit_escapeCharInStr "${backtick}" 'abc'${backtick}`
99 # assertEquals 'abc\`def' \
100 # `_shunit_escapeCharInStr "${backtick}" 'abc'${backtick}'def'`
103 testEscapeCharInStr_specialChars()
105 # make sure our forward slash doesn't upset sed
106 assertEquals '/' `_shunit_escapeCharInStr '\' '/'`
108 # some shells escape these differently
109 #assertEquals '\\a' `_shunit_escapeCharInStr '\' '\a'`
110 #assertEquals '\\b' `_shunit_escapeCharInStr '\' '\b'`
113 # Test the various ways of declaring functions.
115 # Prefixing (then stripping) with comment symbol so these functions aren't
116 # treated as real functions by shUnit2.
117 testExtractTestFunctions()
119 f="${tmpD}/extract_test_functions"
120 sed 's/^#//' <<EOF >"${f}"
121 #testABC() { echo 'ABC'; }
129 #function test4() { echo '4'; }
130 # test5() { echo '5'; }
131 #some_test_function() { echo 'some func'; }
132 #func_with_test_vars() {
137 actual=`_shunit_extractTestFunctions "${f}"`
138 assertEquals 'testABC test_def testG3 test4 test5' "${actual}"
141 #------------------------------------------------------------------------------
147 for f in ${expectedF} ${stdoutF} ${stderrF}; do
156 tmpD="${SHUNIT_TMPDIR}/tmp"
157 expectedF="${SHUNIT_TMPDIR}/expected"
158 stdoutF="${SHUNIT_TMPDIR}/stdout"
159 stderrF="${SHUNIT_TMPDIR}/stderr"
160 unittestF="${SHUNIT_TMPDIR}/unittest"
163 # load and run shUnit2
164 [ -n "${ZSH_VERSION:-}" ] && SHUNIT_PARENT=$0