aboutsummaryrefslogtreecommitdiffstats
path: root/mtest.sh
blob: 41c36627be6f6cdda051f50104e4bb7a0583b022 (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
#!/bin/sh

## ==========================================================================
#   Licensed under BSD 2clause license See LICENSE file for more information
#   Author: Michał Łyszczek <michal.lyszczek@bofc.pl>
#  ==========================================================================
#          __________________________________________________________
#         /                      mtest v9999                         \
#         |                  https://mtest.bofc.pl                   |
#         |                                                          |
#         |    Simple test framework that uses TAP output format     |
#         \                 http://testanything.org                  /
#          ----------------------------------------------------------
#                \
#                 \
#                  \        .
#                   .---.  //
#                  Y|o o|Y//
#                 /_(i=i)K/
#                 ~()~*~()~
#                  (_)-(_)
#
#              Darth Vader Koala
## ==========================================================================
#          __             __                     __   _
#     ____/ /___   _____ / /____ _ _____ ____ _ / /_ (_)____   ____   _____
#    / __  // _ \ / ___// // __ `// ___// __ `// __// // __ \ / __ \ / ___/
#   / /_/ //  __// /__ / // /_/ // /   / /_/ // /_ / // /_/ // / / /(__  )
#   \__,_/ \___/ \___//_/ \__,_//_/    \__,_/ \__//_/ \____//_/ /_//____/
#
## ==========================================================================


mt_test_status=0
mt_total_tests=0
mt_total_failed=0
mt_total_checks=0
mt_checks_failed=0
mt_current_test="none"


## ==========================================================================
#                       __     __ _          ____
#        ____   __  __ / /_   / /(_)_____   / __/__  __ ____   _____ _____
#       / __ \ / / / // __ \ / // // ___/  / /_ / / / // __ \ / ___// ___/
#      / /_/ // /_/ // /_/ // // // /__   / __// /_/ // / / // /__ (__  )
#     / .___/ \__,_//_.___//_//_/ \___/  /_/   \__,_//_/ /_/ \___//____/
#    /_/
## ==========================================================================


## ==========================================================================
#   Runs specified test with optional custom parameters. Check info about
#   mt_run_named() to learn more.
#
#   $1 - function name as a string - will be passed to eval
#   $@ - unspecified number of parameters to pass to function $1
## ==========================================================================


mt_run()
{
	function_name=$1
	shift
	mt_run_named $function_name $function_name $@
}


## ==========================================================================
#   run specified test with custom name to be printed during report and
#   also pass optional arguments to test function.
#
#   $1 - function name as a string - will be passed to eval
#   $2 - test name, will be used instead of $1 in report
#   $@ - unspecified number of parameters to pass to function $1
#
#   Consider:
#
#   foo()
#   {
#       echo param1: $1 param2: $2 param3: $3
#   }
#
#   mt_run_named_param foo test-name 42 "'string with space'" string
#
#   will print
#       param1: 42 param2: string with space param3: string
#
#   NOTE: due to the fact that function is called by `eval', when you
#   want to pass string with space, you need to use double quotation:
#   like this "''".
## ==========================================================================


mt_run_named()
{
	function_name="$1"
	mt_current_test="$2"
	mt_test_status=0
	mt_total_tests=$((mt_total_tests + 1))

	if type mt_prepare_test > /dev/null 2>&1; then
		mt_prepare_test
	fi

	shift
	shift
	eval $function_name $@

	if type mt_cleanup_test > /dev/null 2>&1; then
		mt_cleanup_test
	fi

	if [ $mt_test_status -ne 0 ]; then
		echo "not ok $mt_total_tests - $mt_current_test"
		mt_total_failed=$((mt_total_failed + 1))
	else
		echo "ok $mt_total_tests - $mt_current_test"
	fi
}


## ==========================================================================
#   performs check on given command, if command returns error,  current test
#   will be marked as failed. If second argument is provided, this string
#   will be used as error message instead of "$1".
#
#   $1 - code to evaluate, simply passed to eval
#   $2 - error message to print
## ==========================================================================


mt_fail()
{
	err_msg="$1"
	if [ $# -eq 2 ]; then
		err_msg="$2"
	fi
	mt_total_checks=$(( mt_total_checks + 1 ))
	if ! eval $1 >&2; then
		echo "# assert $mt_current_test, '$err_msg'" >&2
		mt_test_status=1
		mt_checks_failed=$(( mt_checks_failed + 1 ))
	fi
}


## ==========================================================================
#   same as mt_fail() but does not use eval and arguments are directly
#   passed to if
#
#   $@ - code to run
## ==========================================================================


mt_dfail()
{
	mt_total_checks=$(( mt_total_checks + 1 ))
	if ! $@ >&2; then
		echo "# assert $mt_current_test, '$@'" >&2
		mt_test_status=1
		mt_checks_failed=$(( mt_checks_failed + 1 ))
	fi
}


## ==========================================================================
#   prints test plant in  format 1..<number_of_test_run>.  If all tests have
#   passed,  macro will exit script with code 0,  else  it returns number of
#   failed tests.  If number of failed tests  exceeds 254,  then 254 will be
#   returned.
#
#   This function should be called when all tests have been run
## ==========================================================================


mt_return()
{
	echo "1..$mt_total_tests"

	mt_passed_tests=$((mt_total_tests - mt_total_failed))
	mt_passed_checks=$((mt_total_checks - mt_checks_failed))

	printf "# total tests.......: %4d\n" ${mt_total_tests}
	printf "# passed tests......: %4d\n" ${mt_passed_tests}
	printf "# failed tests......: %4d\n" ${mt_total_failed}
	printf "# total checks......: %4d\n" ${mt_total_checks}
	printf "# passed checks.....: %4d\n" ${mt_passed_checks}
	printf "# failed checks.....: %4d\n" ${mt_checks_failed}

	if [ $mt_total_failed -gt 254 ]; then
		exit 254
	else
		exit $mt_total_failed
	fi
}