aboutsummaryrefslogtreecommitdiffstats
path: root/test/test-backend.sh
blob: 324a49d1534d8f30a0ce1d28cf54e8ac2ca56659 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/bin/bash
#
# Test backend
#
# $Id$
#
# Wireshark - Network traffic analyzer
# By Gerald Combs <gerald@wireshark.org>
# Copyright 2005 Ulf Lamping
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#


# References:
# http://www.gnu.org/software/bash/manual/bashref.html "Bash Reference Manual"
# http://www.tldp.org/LDP/abs/html/ "Advanced Bash-Scripting Guide"
# http://www.tldp.org/LDP/abs/html/colorizing.html "Colorizing" Scripts"
# http://www.junit.org/junit/javadoc/3.8.1/index.htm "JUnit javadoc"

# check undefined variables
# http://www.tldp.org/LDP/abs/html/options.html
# bash -u test.sh


# coloring the output
if [ $USE_COLOR -eq 1 ] ; then
        color_reset="tput sgr0"
        color_green='\E[32;40m'
        color_red='\E[31;40m'
        color_yellow='\E[33;40m'
        color_blue='\E[36;40m'
else
        color_reset="/bin/true"
        color_green=''
        color_red=''
        color_yellow=''
        color_blue=''
fi

# runtime flags
TEST_RUN="OFF"
TEST_OUTPUT="VERBOSE"	# "OFF", "DOTTED", "VERBOSE"

# runtime vars
TEST_NESTING_LEVEL=0	# nesting level of current test
TEST_STEPS[0]=0			# number of steps of a specific nesting level

# output counters
TEST_OK=0				# global count of succeeded steps
TEST_FAILED=0			# global count of failed steps
TEST_SKIPPED=0			# global count of failed steps

TEST_STEP_PRE_CB=
TEST_STEP_POST_CB=

# level number of this test item (suite or step)
test_level() {
	LIMIT_LEVEL=100

	for ((a=0; a <= LIMIT_LEVEL ; a++))
	do
		if [ ! $a -eq 0 ]; then
			echo -n "."
		fi
		echo -n "${TEST_STEPS[a]}"
		if [ $a -eq $TEST_NESTING_LEVEL ]; then
			#echo "end"
			return
		fi
	done
}

# set output format
# $1 - "OUT", "DOTTED", "VERBOSE"
test_set_output() {
	TEST_OUTPUT=$1
}

# run a test suite
# $1 name
# $2 command
test_suite_run() {
	# header
	echo -n -e $color_blue
	echo ""
	echo "### $1 ###"
	$color_reset

	TEST_RUN="ON"

	# run the actual test suite
	$2

	# results
	if [ $TEST_RUN = "ON" ]; then
		echo ""
		if [ $TEST_FAILED -eq 0 ]; then
			echo -n -e $color_green
		else
			echo -n -e $color_red
		fi
		echo "### Test suite results ###"
		echo -n -e $color_green
		echo "Ok    : $TEST_OK"
		echo -n -e $color_red
		echo "Failed: $TEST_FAILED"
		echo -n -e $color_yellow
		echo "Skipped: $TEST_SKIPPED"
		$color_reset
	fi

	TEST_RUN="OFF"

	# exit status
	if [ $TEST_FAILED -eq 0 ]; then
		return 0
	else
		return 1
	fi
}


# show a test suite
# $1 name
# $2 command
test_suite_show() {

	# header
	echo -n -e $color_blue
	echo ""
	echo "### Test suite: $1 ###"
	echo ""
	echo "Subitems:"
	echo "---------"
	$color_reset

	# show this test suite subitems
	$2

	echo ""
}


# add a test suite
# $1 name
# $2 function
test_suite_add() {
	# increase step counter of this nesting level
	let "TEST_STEPS[$TEST_NESTING_LEVEL] += 1"

	if [ $TEST_RUN = "ON" ]; then
		echo ""
	fi

	# title output if we'll list the subitems
	if [[ $TEST_RUN = "ON" ]]; then
		echo -n -e $color_blue
		test_level
		echo "  Suite: $1"
		$color_reset
	fi

	if [[ $TEST_NESTING_LEVEL -eq 0 ]]; then
		pos=${TEST_STEPS[$TEST_NESTING_LEVEL]}
		#echo "pos " $pos
		test_title[$pos]=$1
		test_function[$pos]=$2
		#echo ${test_title[1]}

	fi

	# reset test step counter back to zero
	TEST_STEP=0

	# call the suites function
	let "TEST_NESTING_LEVEL += 1"
	TEST_STEPS[$TEST_NESTING_LEVEL]=0
	$2
	let "TEST_NESTING_LEVEL -= 1"

	# title output (with subitem counter) if we don't listed the subitems
	if [[ ! $TEST_RUN = "ON" && $TEST_NESTING_LEVEL -eq 0 ]]; then
		echo -n -e $color_blue
		test_level
		echo "  Suite: $1 (${TEST_STEPS[TEST_NESTING_LEVEL+1]} subitems)"
		$color_reset
	fi

}


# add a test step
# $1 name
# $2 function
test_step_add() {

	let "TEST_STEPS[$TEST_NESTING_LEVEL] += 1"

	if [[ ($TEST_RUN = "ON" && $TEST_OUTPUT = "DOTTED") && $TEST_NESTING_LEVEL -eq 0 ]]; then
		echo ""
	fi

	if [[ ( $TEST_RUN = "ON" && $TEST_OUTPUT = "VERBOSE" ) || $TEST_NESTING_LEVEL -eq 0 ]]; then
		echo -n -e $color_blue
		test_level
		echo -n " Step:" $1
		$color_reset
	fi

	if [ $TEST_RUN = "ON" ]; then
		# preprecessing step
		$TEST_STEP_PRE_CB
		#echo "command: "$2" opt1: "$3" opt2: "$4" opt3: "$5" opt4: "$6" opt5: "$7
		TEST_STEP_NAME=$1
		# actually run the command to test now
		$2
		#"$3" "$4" "$5" "$6" "$7"
		# post precessing step
		$TEST_STEP_POST_CB
	else
		if [[ $TEST_NESTING_LEVEL -eq 0 ]]; then
			echo ""
		fi
	fi
}


# set the preprocessing function
# $1 remark
test_step_set_pre() {
	TEST_STEP_PRE_CB=$1
}

# set the post processing function
# $1 remark
test_step_set_post() {
	TEST_STEP_POST_CB=$1
}

# add a test remark
# $1 remark
test_remark_add() {

	# test is running or toplevel item? -> show remark
	if [[ $TEST_RUN = "ON" || $TEST_NESTING_LEVEL -eq 0 ]]; then
		# test is running and output is dotted -> newline first
		if [[ $TEST_RUN = "ON" && $TEST_OUTPUT = "DOTTED" ]]; then
			echo ""
		fi

		# remark
		echo -n -e $color_blue
		echo "  Remark: $1"
		$color_reset
	fi
}


# the test step succeeded
test_step_ok() {
	# count appearance
	let "TEST_OK += 1"

	# output in green
	echo -n -e $color_green

	if [ $TEST_OUTPUT = "VERBOSE" ]; then
		echo " Ok"
	else
		echo -n .
	fi

	$color_reset
}

# the test step failed
# $1 output text
test_step_failed() {
	let "TEST_FAILED += 1"

	# output in red
	echo -n -e "$color_red"

	echo ""
	echo "\"$TEST_STEP_NAME\" Failed!"
	echo $1

	$color_reset

	exit 1

	# XXX - add a mechanism to optionally stop here
}

# the test step succeeded
test_step_skipped() {
	# count appearance
	let "TEST_SKIPPED += 1"

	# output in green
	echo -n -e $color_yellow

	if [ $TEST_OUTPUT = "VERBOSE" ]; then
		echo " Skipped"
	else
		echo -n .
	fi

	$color_reset
}

test_step_output_print() {
	wait
	printf "\n"
	for f in "$@"; do
		if [[ -f "$f" ]]; then
			printf " --> $f\n"
			cat "$f"
			printf " <--\n"
		else
			printf " --> $f: doesn't exist (or isn't a file)\n"
		fi
	done
}

## Emacs
## Local Variables:
## tab-width: 8
## indent-tabs-mode: t
## sh-basic-offset: 8
## End: