bringing in testkit tools
[feisty_meow.git] / testkit / shunit / shunit2_test_asserts.sh
1 #! /bin/sh
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
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 test for assert functions
11
12 # load test helpers
13 . ./shunit2_test_helpers
14
15 #------------------------------------------------------------------------------
16 # suite tests
17 #
18
19 commonEqualsSame()
20 {
21   fn=$1
22
23   ( ${fn} 'x' 'x' >"${stdoutF}" 2>"${stderrF}" )
24   th_assertTrueWithNoOutput 'equal' $? "${stdoutF}" "${stderrF}"
25
26   ( ${fn} "${MSG}" 'x' 'x' >"${stdoutF}" 2>"${stderrF}" )
27   th_assertTrueWithNoOutput 'equal; with msg' $? "${stdoutF}" "${stderrF}"
28
29   ( ${fn} 'abc def' 'abc def' >"${stdoutF}" 2>"${stderrF}" )
30   th_assertTrueWithNoOutput 'equal with spaces' $? "${stdoutF}" "${stderrF}"
31
32   ( ${fn} 'x' 'y' >"${stdoutF}" 2>"${stderrF}" )
33   th_assertFalseWithOutput 'not equal' $? "${stdoutF}" "${stderrF}"
34
35   ( ${fn} '' '' >"${stdoutF}" 2>"${stderrF}" )
36   th_assertTrueWithNoOutput 'null values' $? "${stdoutF}" "${stderrF}"
37
38   ( ${fn} arg1 >"${stdoutF}" 2>"${stderrF}" )
39   th_assertFalseWithError 'too few arguments' $? "${stdoutF}" "${stderrF}"
40
41   ( ${fn} arg1 arg2 arg3 arg4 >"${stdoutF}" 2>"${stderrF}" )
42   th_assertFalseWithError 'too many arguments' $? "${stdoutF}" "${stderrF}"
43 }
44
45 commonNotEqualsSame()
46 {
47   fn=$1
48
49   ( ${fn} 'x' 'y' >"${stdoutF}" 2>"${stderrF}" )
50   th_assertTrueWithNoOutput 'not same' $? "${stdoutF}" "${stderrF}"
51
52   ( ${fn} "${MSG}" 'x' 'y' >"${stdoutF}" 2>"${stderrF}" )
53   th_assertTrueWithNoOutput 'not same, with msg' $? "${stdoutF}" "${stderrF}"
54
55   ( ${fn} 'x' 'x' >"${stdoutF}" 2>"${stderrF}" )
56   th_assertFalseWithOutput 'same' $? "${stdoutF}" "${stderrF}"
57
58   ( ${fn} '' '' >"${stdoutF}" 2>"${stderrF}" )
59   th_assertFalseWithOutput 'null values' $? "${stdoutF}" "${stderrF}"
60
61   ( ${fn} arg1 >"${stdoutF}" 2>"${stderrF}" )
62   th_assertFalseWithError 'too few arguments' $? "${stdoutF}" "${stderrF}"
63
64   ( ${fn} arg1 arg2 arg3 arg4 >"${stdoutF}" 2>"${stderrF}" )
65   th_assertFalseWithError 'too many arguments' $? "${stdoutF}" "${stderrF}"
66 }
67
68 testAssertEquals()
69 {
70   commonEqualsSame 'assertEquals'
71 }
72
73 testAssertNotEquals()
74 {
75   commonNotEqualsSame 'assertNotEquals'
76 }
77
78 testAssertSame()
79 {
80   commonEqualsSame 'assertSame'
81 }
82
83 testAssertNotSame()
84 {
85   commonNotEqualsSame 'assertNotSame'
86 }
87
88 testAssertNull()
89 {
90   ( assertNull '' >"${stdoutF}" 2>"${stderrF}" )
91   th_assertTrueWithNoOutput 'null' $? "${stdoutF}" "${stderrF}"
92
93   ( assertNull "${MSG}" '' >"${stdoutF}" 2>"${stderrF}" )
94   th_assertTrueWithNoOutput 'null, with msg' $? "${stdoutF}" "${stderrF}"
95
96   ( assertNull 'x' >"${stdoutF}" 2>"${stderrF}" )
97   th_assertFalseWithOutput 'not null' $? "${stdoutF}" "${stderrF}"
98
99   ( assertNull >"${stdoutF}" 2>"${stderrF}" )
100   th_assertFalseWithError 'too few arguments' $? "${stdoutF}" "${stderrF}"
101
102   ( assertNull arg1 arg2 arg3 >"${stdoutF}" 2>"${stderrF}" )
103   th_assertFalseWithError 'too many arguments' $? "${stdoutF}" "${stderrF}"
104 }
105
106 testAssertNotNull()
107 {
108   ( assertNotNull 'x' >"${stdoutF}" 2>"${stderrF}" )
109   th_assertTrueWithNoOutput 'not null' $? "${stdoutF}" "${stderrF}"
110
111   ( assertNotNull "${MSG}" 'x' >"${stdoutF}" 2>"${stderrF}" )
112   th_assertTrueWithNoOutput 'not null, with msg' $? "${stdoutF}" "${stderrF}"
113
114   ( assertNotNull 'x"b' >"${stdoutF}" 2>"${stderrF}" )
115   th_assertTrueWithNoOutput 'not null, with double-quote' $? \
116       "${stdoutF}" "${stderrF}"
117
118   ( assertNotNull "x'b" >"${stdoutF}" 2>"${stderrF}" )
119   th_assertTrueWithNoOutput 'not null, with single-quote' $? \
120       "${stdoutF}" "${stderrF}"
121
122   ( assertNotNull 'x$b' >"${stdoutF}" 2>"${stderrF}" )
123   th_assertTrueWithNoOutput 'not null, with dollar' $? \
124       "${stdoutF}" "${stderrF}"
125
126   ( assertNotNull 'x`b' >"${stdoutF}" 2>"${stderrF}" )
127   th_assertTrueWithNoOutput 'not null, with backtick' $? \
128       "${stdoutF}" "${stderrF}"
129
130   ( assertNotNull '' >"${stdoutF}" 2>"${stderrF}" )
131   th_assertFalseWithOutput 'null' $? "${stdoutF}" "${stderrF}"
132
133   # there is no test for too few arguments as $1 might actually be null
134
135   ( assertNotNull arg1 arg2 arg3 >"${stdoutF}" 2>"${stderrF}" )
136   th_assertFalseWithError 'too many arguments' $? "${stdoutF}" "${stderrF}"
137 }
138
139 testAssertTrue()
140 {
141   ( assertTrue 0 >"${stdoutF}" 2>"${stderrF}" )
142   th_assertTrueWithNoOutput 'true' $? "${stdoutF}" "${stderrF}"
143
144   ( assertTrue "${MSG}" 0 >"${stdoutF}" 2>"${stderrF}" )
145   th_assertTrueWithNoOutput 'true, with msg' $? "${stdoutF}" "${stderrF}"
146
147   ( assertTrue '[ 0 -eq 0 ]' >"${stdoutF}" 2>"${stderrF}" )
148   th_assertTrueWithNoOutput 'true condition' $? "${stdoutF}" "${stderrF}"
149
150   ( assertTrue 1 >"${stdoutF}" 2>"${stderrF}" )
151   th_assertFalseWithOutput 'false' $? "${stdoutF}" "${stderrF}"
152
153   ( assertTrue '[ 0 -eq 1 ]' >"${stdoutF}" 2>"${stderrF}" )
154   th_assertFalseWithOutput 'false condition' $? "${stdoutF}" "${stderrF}"
155
156   ( assertTrue '' >"${stdoutF}" 2>"${stderrF}" )
157   th_assertFalseWithOutput 'null' $? "${stdoutF}" "${stderrF}"
158
159   ( assertTrue >"${stdoutF}" 2>"${stderrF}" )
160   th_assertFalseWithError 'too few arguments' $? "${stdoutF}" "${stderrF}"
161
162   ( assertTrue arg1 arg2 arg3 >"${stdoutF}" 2>"${stderrF}" )
163   th_assertFalseWithError 'too many arguments' $? "${stdoutF}" "${stderrF}"
164 }
165
166 testAssertFalse()
167 {
168   ( assertFalse 1 >"${stdoutF}" 2>"${stderrF}" )
169   th_assertTrueWithNoOutput 'false' $? "${stdoutF}" "${stderrF}"
170
171   ( assertFalse "${MSG}" 1 >"${stdoutF}" 2>"${stderrF}" )
172   th_assertTrueWithNoOutput 'false, with msg' $? "${stdoutF}" "${stderrF}"
173
174   ( assertFalse '[ 0 -eq 1 ]' >"${stdoutF}" 2>"${stderrF}" )
175   th_assertTrueWithNoOutput 'false condition' $? "${stdoutF}" "${stderrF}"
176
177   ( assertFalse 0 >"${stdoutF}" 2>"${stderrF}" )
178   th_assertFalseWithOutput 'true' $? "${stdoutF}" "${stderrF}"
179
180   ( assertFalse '[ 0 -eq 0 ]' >"${stdoutF}" 2>"${stderrF}" )
181   th_assertFalseWithOutput 'true condition' $? "${stdoutF}" "${stderrF}"
182
183   ( assertFalse '' >"${stdoutF}" 2>"${stderrF}" )
184   th_assertFalseWithOutput 'true condition' $? "${stdoutF}" "${stderrF}"
185
186   ( assertFalse >"${stdoutF}" 2>"${stderrF}" )
187   th_assertFalseWithError 'too few arguments' $? "${stdoutF}" "${stderrF}"
188
189   ( assertFalse arg1 arg2 arg3 >"${stdoutF}" 2>"${stderrF}" )
190   th_assertFalseWithError 'too many arguments' $? "${stdoutF}" "${stderrF}"
191 }
192
193 #------------------------------------------------------------------------------
194 # suite functions
195 #
196
197 oneTimeSetUp()
198 {
199   tmpDir="${__shunit_tmpDir}/output"
200   mkdir "${tmpDir}"
201   stdoutF="${tmpDir}/stdout"
202   stderrF="${tmpDir}/stderr"
203
204   MSG='This is a test message'
205 }
206
207 # load and run shUnit2
208 [ -n "${ZSH_VERSION:-}" ] && SHUNIT_PARENT=$0
209 . ${TH_SHUNIT}