handling no parms better
[feisty_meow.git] / scripts / shunit / shunit2_test_misc.sh
1 #! /bin/sh
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
4 #
5 # Copyright 2008 Kate Ward. All Rights Reserved.
6 # Released under the LGPL (GNU Lesser General Public License)
7 #
8 # Author: kate.ward@forestent.com (Kate Ward)
9 #
10 # shUnit2 unit tests of miscellaneous things
11
12 # load test helpers
13 . ./shunit2_test_helpers
14
15 #------------------------------------------------------------------------------
16 # suite tests
17 #
18
19 # Note: the test script is prefixed with '#' chars so that shUnit2 does not
20 # incorrectly interpret the embedded functions as real functions.
21 testUnboundVariable()
22 {
23   sed 's/^#//' >"${unittestF}" <<EOF
24 ## treat unset variables as an error when performing parameter expansion
25 #set -u
26 #
27 #boom() { x=\$1; }  # this function goes boom if no parameters are passed!
28 #test_boom()
29 #{
30 #   assertEquals 1 1
31 #   boom  # No parameter given
32 #   assertEquals 0 \$?
33 #}
34 #. ${TH_SHUNIT}
35 EOF
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' $?
44 }
45
46 testIssue7()
47 {
48   ( assertEquals 'Some message.' 1 2 >"${stdoutF}" 2>"${stderrF}" )
49   diff "${stdoutF}" - >/dev/null <<EOF
50 ASSERT:Some message. expected:<1> but was:<2>
51 EOF
52   rtrn=$?
53   assertEquals ${SHUNIT_TRUE} ${rtrn}
54   [ ${rtrn} -ne ${SHUNIT_TRUE} ] && cat "${stderrF}" >&2
55 }
56
57 testPrepForSourcing()
58 {
59   assertEquals '/abc' `_shunit_prepForSourcing '/abc'`
60   assertEquals './abc' `_shunit_prepForSourcing './abc'`
61   assertEquals './abc' `_shunit_prepForSourcing 'abc'`
62 }
63
64 testEscapeCharInStr()
65 {
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'`
71
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'`
77
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'`
83
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"`
89
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).
92 #  backtick='`'
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'`
101 }
102
103 testEscapeCharInStr_specialChars()
104 {
105   # make sure our forward slash doesn't upset sed
106   assertEquals '/' `_shunit_escapeCharInStr '\' '/'`
107
108   # some shells escape these differently
109   #assertEquals '\\a' `_shunit_escapeCharInStr '\' '\a'`
110   #assertEquals '\\b' `_shunit_escapeCharInStr '\' '\b'`
111 }
112
113 # Test the various ways of declaring functions.
114 #
115 # Prefixing (then stripping) with comment symbol so these functions aren't
116 # treated as real functions by shUnit2.
117 testExtractTestFunctions()
118 {
119   f="${tmpD}/extract_test_functions"
120   sed 's/^#//' <<EOF >"${f}"
121 #testABC() { echo 'ABC'; }
122 #test_def() {
123 #  echo 'def'
124 #}
125 #testG3 ()
126 #{
127 #  echo 'G3'
128 #}
129 #function test4() { echo '4'; }
130 #       test5() { echo '5'; }
131 #some_test_function() { echo 'some func'; }
132 #func_with_test_vars() {
133 #  testVariable=1234
134 #}
135 EOF
136
137   actual=`_shunit_extractTestFunctions "${f}"`
138   assertEquals 'testABC test_def testG3 test4 test5' "${actual}"
139 }
140
141 #------------------------------------------------------------------------------
142 # suite functions
143 #
144
145 setUp()
146 {
147   for f in ${expectedF} ${stdoutF} ${stderrF}; do
148     cp /dev/null ${f}
149   done
150   rm -fr "${tmpD}"
151   mkdir "${tmpD}"
152 }
153
154 oneTimeSetUp()
155 {
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"
161 }
162
163 # load and run shUnit2
164 [ -n "${ZSH_VERSION:-}" ] && SHUNIT_PARENT=$0
165 . ${TH_SHUNIT}