2 # $Id: shunit2_test_asserts.sh 312 2011-03-14 22:41:29Z 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 test for assert functions
13 . ./shunit2_test_helpers
15 #------------------------------------------------------------------------------
23 ( ${fn} 'x' 'x' >"${stdoutF}" 2>"${stderrF}" )
24 th_assertTrueWithNoOutput 'equal' $? "${stdoutF}" "${stderrF}"
26 ( ${fn} "${MSG}" 'x' 'x' >"${stdoutF}" 2>"${stderrF}" )
27 th_assertTrueWithNoOutput 'equal; with msg' $? "${stdoutF}" "${stderrF}"
29 ( ${fn} 'abc def' 'abc def' >"${stdoutF}" 2>"${stderrF}" )
30 th_assertTrueWithNoOutput 'equal with spaces' $? "${stdoutF}" "${stderrF}"
32 ( ${fn} 'x' 'y' >"${stdoutF}" 2>"${stderrF}" )
33 th_assertFalseWithOutput 'not equal' $? "${stdoutF}" "${stderrF}"
35 ( ${fn} '' '' >"${stdoutF}" 2>"${stderrF}" )
36 th_assertTrueWithNoOutput 'null values' $? "${stdoutF}" "${stderrF}"
38 ( ${fn} arg1 >"${stdoutF}" 2>"${stderrF}" )
39 th_assertFalseWithError 'too few arguments' $? "${stdoutF}" "${stderrF}"
41 ( ${fn} arg1 arg2 arg3 arg4 >"${stdoutF}" 2>"${stderrF}" )
42 th_assertFalseWithError 'too many arguments' $? "${stdoutF}" "${stderrF}"
49 ( ${fn} 'x' 'y' >"${stdoutF}" 2>"${stderrF}" )
50 th_assertTrueWithNoOutput 'not same' $? "${stdoutF}" "${stderrF}"
52 ( ${fn} "${MSG}" 'x' 'y' >"${stdoutF}" 2>"${stderrF}" )
53 th_assertTrueWithNoOutput 'not same, with msg' $? "${stdoutF}" "${stderrF}"
55 ( ${fn} 'x' 'x' >"${stdoutF}" 2>"${stderrF}" )
56 th_assertFalseWithOutput 'same' $? "${stdoutF}" "${stderrF}"
58 ( ${fn} '' '' >"${stdoutF}" 2>"${stderrF}" )
59 th_assertFalseWithOutput 'null values' $? "${stdoutF}" "${stderrF}"
61 ( ${fn} arg1 >"${stdoutF}" 2>"${stderrF}" )
62 th_assertFalseWithError 'too few arguments' $? "${stdoutF}" "${stderrF}"
64 ( ${fn} arg1 arg2 arg3 arg4 >"${stdoutF}" 2>"${stderrF}" )
65 th_assertFalseWithError 'too many arguments' $? "${stdoutF}" "${stderrF}"
70 commonEqualsSame 'assertEquals'
75 commonNotEqualsSame 'assertNotEquals'
80 commonEqualsSame 'assertSame'
85 commonNotEqualsSame 'assertNotSame'
90 ( assertNull '' >"${stdoutF}" 2>"${stderrF}" )
91 th_assertTrueWithNoOutput 'null' $? "${stdoutF}" "${stderrF}"
93 ( assertNull "${MSG}" '' >"${stdoutF}" 2>"${stderrF}" )
94 th_assertTrueWithNoOutput 'null, with msg' $? "${stdoutF}" "${stderrF}"
96 ( assertNull 'x' >"${stdoutF}" 2>"${stderrF}" )
97 th_assertFalseWithOutput 'not null' $? "${stdoutF}" "${stderrF}"
99 ( assertNull >"${stdoutF}" 2>"${stderrF}" )
100 th_assertFalseWithError 'too few arguments' $? "${stdoutF}" "${stderrF}"
102 ( assertNull arg1 arg2 arg3 >"${stdoutF}" 2>"${stderrF}" )
103 th_assertFalseWithError 'too many arguments' $? "${stdoutF}" "${stderrF}"
108 ( assertNotNull 'x' >"${stdoutF}" 2>"${stderrF}" )
109 th_assertTrueWithNoOutput 'not null' $? "${stdoutF}" "${stderrF}"
111 ( assertNotNull "${MSG}" 'x' >"${stdoutF}" 2>"${stderrF}" )
112 th_assertTrueWithNoOutput 'not null, with msg' $? "${stdoutF}" "${stderrF}"
114 ( assertNotNull 'x"b' >"${stdoutF}" 2>"${stderrF}" )
115 th_assertTrueWithNoOutput 'not null, with double-quote' $? \
116 "${stdoutF}" "${stderrF}"
118 ( assertNotNull "x'b" >"${stdoutF}" 2>"${stderrF}" )
119 th_assertTrueWithNoOutput 'not null, with single-quote' $? \
120 "${stdoutF}" "${stderrF}"
122 ( assertNotNull 'x$b' >"${stdoutF}" 2>"${stderrF}" )
123 th_assertTrueWithNoOutput 'not null, with dollar' $? \
124 "${stdoutF}" "${stderrF}"
126 ( assertNotNull 'x`b' >"${stdoutF}" 2>"${stderrF}" )
127 th_assertTrueWithNoOutput 'not null, with backtick' $? \
128 "${stdoutF}" "${stderrF}"
130 ( assertNotNull '' >"${stdoutF}" 2>"${stderrF}" )
131 th_assertFalseWithOutput 'null' $? "${stdoutF}" "${stderrF}"
133 # there is no test for too few arguments as $1 might actually be null
135 ( assertNotNull arg1 arg2 arg3 >"${stdoutF}" 2>"${stderrF}" )
136 th_assertFalseWithError 'too many arguments' $? "${stdoutF}" "${stderrF}"
141 ( assertTrue 0 >"${stdoutF}" 2>"${stderrF}" )
142 th_assertTrueWithNoOutput 'true' $? "${stdoutF}" "${stderrF}"
144 ( assertTrue "${MSG}" 0 >"${stdoutF}" 2>"${stderrF}" )
145 th_assertTrueWithNoOutput 'true, with msg' $? "${stdoutF}" "${stderrF}"
147 ( assertTrue '[ 0 -eq 0 ]' >"${stdoutF}" 2>"${stderrF}" )
148 th_assertTrueWithNoOutput 'true condition' $? "${stdoutF}" "${stderrF}"
150 ( assertTrue 1 >"${stdoutF}" 2>"${stderrF}" )
151 th_assertFalseWithOutput 'false' $? "${stdoutF}" "${stderrF}"
153 ( assertTrue '[ 0 -eq 1 ]' >"${stdoutF}" 2>"${stderrF}" )
154 th_assertFalseWithOutput 'false condition' $? "${stdoutF}" "${stderrF}"
156 ( assertTrue '' >"${stdoutF}" 2>"${stderrF}" )
157 th_assertFalseWithOutput 'null' $? "${stdoutF}" "${stderrF}"
159 ( assertTrue >"${stdoutF}" 2>"${stderrF}" )
160 th_assertFalseWithError 'too few arguments' $? "${stdoutF}" "${stderrF}"
162 ( assertTrue arg1 arg2 arg3 >"${stdoutF}" 2>"${stderrF}" )
163 th_assertFalseWithError 'too many arguments' $? "${stdoutF}" "${stderrF}"
168 ( assertFalse 1 >"${stdoutF}" 2>"${stderrF}" )
169 th_assertTrueWithNoOutput 'false' $? "${stdoutF}" "${stderrF}"
171 ( assertFalse "${MSG}" 1 >"${stdoutF}" 2>"${stderrF}" )
172 th_assertTrueWithNoOutput 'false, with msg' $? "${stdoutF}" "${stderrF}"
174 ( assertFalse '[ 0 -eq 1 ]' >"${stdoutF}" 2>"${stderrF}" )
175 th_assertTrueWithNoOutput 'false condition' $? "${stdoutF}" "${stderrF}"
177 ( assertFalse 0 >"${stdoutF}" 2>"${stderrF}" )
178 th_assertFalseWithOutput 'true' $? "${stdoutF}" "${stderrF}"
180 ( assertFalse '[ 0 -eq 0 ]' >"${stdoutF}" 2>"${stderrF}" )
181 th_assertFalseWithOutput 'true condition' $? "${stdoutF}" "${stderrF}"
183 ( assertFalse '' >"${stdoutF}" 2>"${stderrF}" )
184 th_assertFalseWithOutput 'true condition' $? "${stdoutF}" "${stderrF}"
186 ( assertFalse >"${stdoutF}" 2>"${stderrF}" )
187 th_assertFalseWithError 'too few arguments' $? "${stdoutF}" "${stderrF}"
189 ( assertFalse arg1 arg2 arg3 >"${stdoutF}" 2>"${stderrF}" )
190 th_assertFalseWithError 'too many arguments' $? "${stdoutF}" "${stderrF}"
193 #------------------------------------------------------------------------------
199 tmpDir="${__shunit_tmpDir}/output"
201 stdoutF="${tmpDir}/stdout"
202 stderrF="${tmpDir}/stderr"
204 MSG='This is a test message'
207 # load and run shUnit2
208 [ -n "${ZSH_VERSION:-}" ] && SHUNIT_PARENT=$0