-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbunit.shl
executable file
·300 lines (255 loc) · 6.84 KB
/
bunit.shl
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
#!/bin/bash
passed_tests=0
failed_tests=0
total_tests=0
VERBOSE_MODE="false"
NO_SCREEN_CLEAR="false"
COLOR_RED='\033[0;31m'
COLOR_GREEN='\033[0;32m'
NO_COLOR='\033[0m'
showHelp () {
echo "Usage:"
echo " unitTestSuite.ut [options]"
echo ""
echo "Options"
echo " -v shows the output of tests"
echo " -c does not clear the screen before tests"
echo " -e run with 'set -e' enabled. This will cause execution to halt if a command failed."
echo " -u run with 'set -u' enabled. This will cause execution to halt if a variable is unbound (declared but not used). Can cause instability."
echo " -x run with 'set -x' enabled. This will print all debug/trace messages. Generates a lot of output."
echo " -h show this help"
echo " -V show version"
echo ""
}
showVersion () {
echo "bunit: Version 1.2.1"
}
setFailFast() {
set -e
}
setTrace() {
set -x
}
setUnboundVariables() {
set -u
}
# Parse arguments
while getopts "h?veuxcV" opt; do
case "$opt" in
h|\?)
showHelp
exit 0
;;
v) VERBOSE_MODE="true"
;;
e) setFailFast
;;
u) setUnboundVariables
;;
x) setTrace
;;
c) NO_SCREEN_CLEAR="true"
;;
V) showVersion
;;
esac
done
# Assert Functions
assertEquals () {
local args=(
"${1}"
"${2}"
)
__assert 2 __equalsCallback args[@] "$#" "Expected \"$1\", but got \"$2\"."
}
assertNotEquals () {
local args=(
"${1}"
"${2}"
)
__assert 2 __notEqualsCallback args[@] "$#" "Expected not \"$1\", but got \"$2\"."
}
assertNull () {
local args=(
"${1}"
)
__assert 1 __nullCallback args[@] "$#" "Expected empty string, but got \"$1\"."
}
assertNotNull () {
local args=(
"${1}"
)
__assert 1 __notNullCallback args[@] "$#" "Expected not empty string, but got \"$1\"."
}
assertTrue () {
local args=(
"${1}"
)
__assert 1 __trueCallback args[@] "$#" "Boolean Expression [ $1 ] expected to evaluate as true, evaluated as false."
}
assertFalse () {
local args=(
"${1}"
)
__assert 1 __falseCallback args[@] "$#" "Boolean Expression [ $1 ] expected to evaluate as false, evaluated as true."
}
assertContains () {
# shellcheck disable=SC2034
local args=(
"${1}"
"${2}"
)
__assert 2 __containsCallback args[@] "$#" "Expected \"$2\" to contain \"$1\"."
}
__logError () {
echo -e "$COLOR_RED$1$NO_COLOR"
}
__logSuccess () {
echo -e "$COLOR_GREEN$1$NO_COLOR"
}
# Main assert function, DONT CALL THIS DIRECTLY!
__assert () {
local correct_num_of_args=${1}
local callback_function=${2}
local callback_args=${3}
local actual_num_of_args=${4}
local failure_msg=${5}
local test_case_calling_assert=${FUNCNAME[2]}
local assert_caller_func_name=${FUNCNAME[1]}
local line=$(echo $(caller 1) | awk '{print $1}')
if [ "$(correctNumberOfArgs "${actual_num_of_args}" "${correct_num_of_args}")" = "true" ]; then
result=$(${callback_function} "${callback_args}")
if [ "${result}" = "true" ]; then
[[ "$VERBOSE_MODE" = "true" ]] && __logSuccess "Line ${line}: Passed - ${test_case_calling_assert}"
incrementPassedTests
else
__logError "Line ${line}: ${test_case_calling_assert}: ${assert_caller_func_name}() failed. $failure_msg"
incrementFailedTests
fi
else
__logError "Line $line: $test_case_calling_assert: ${assert_caller_func_name}() failed. Expected ${correct_num_of_args} argument(s)."
incrementFailedTests
fi
}
# Callback functions - DONT CALL THESE DIRECTLY!
__equalsCallback() {
declare -a callback_args=("${!1}")
if [ "${callback_args[0]}" = "${callback_args[1]}" ] || [ "$(numericCompare "${callback_args[0]}" "${callback_args[1]}")" = "true" ]; then
echo "true"
else
echo "false"
fi
}
__notEqualsCallback() {
declare -a callback_args=("${!1}")
if [ "${callback_args[0]}" != "${callback_args[1]}" ]; then
echo "true"
else
echo "false"
fi
}
__nullCallback() {
declare -a callback_args=("${!1}")
if [ "${callback_args[0]}" = "" ]; then
echo "true"
else
echo "false"
fi
}
__notNullCallback() {
declare -a callback_args=("${!1}")
if [ "${callback_args[0]}" != "" ]; then
echo "true"
else
echo "false"
fi
}
__trueCallback() {
declare -a callback_args=("${!1}")
eval [ "${callback_args[0]}" ]
testResult=$?
if [ "$testResult" = "0" ]; then
echo "true"
else
echo "false"
fi
}
__falseCallback() {
declare -a callback_args=("${!1}")
eval [ "${callback_args[0]}" ]
testResult=$?
if [ "$testResult" = "1" ]; then
echo "true"
else
echo "false"
fi
}
__containsCallback() {
declare -a callback_args=("${!1}")
if [[ "${callback_args[1]}" == *"${callback_args[0]}"* ]]; then
echo "true"
else
echo "false"
fi
}
# Utility functions
runUnitTests () {
START_TIME=$(date +%s)
# Explanation of the following command:
# grep -E - Use extended regexp
# ^(function )? - Lines may start with the keyword 'function' followed by space
# test.*?\( - Match any line with 'test[anything] (', doesn't need 'function'
# sed 's/^function //' - Remove the 'function ' prefix from any string, if it occurs
# tr -d ' (){' - Remove the trailing ' (){' characters
test_names=$(grep -E '^(function )?test.*?\(' "$0" | sed 's/^function //' | tr -d ' (){')
test_names_array=($test_names)
beginUnitTests
for test_case in "${test_names_array[@]}"
do
:
$test_case
done
endUnitTests
if [ "$failed_tests" -gt "0" ]; then
echo "Unit Tests failed. Exiting Status Code 1"
exit 1
fi
}
beginUnitTests () {
if [ $VERBOSE_MODE = "true" ] && [ $NO_SCREEN_CLEAR = "false" ]; then
clear
echo ""
echo "$0:"
echo ""
fi
}
endUnitTests () {
if [ $failed_tests -gt "0" ] || [ $VERBOSE_MODE = "true" ]; then
END_TIME=$(date +%s)
echo ""
echo "RESULTS: $passed_tests tests passed. $failed_tests tests failed. $total_tests tests total."
echo "Execution completed in $((END_TIME - START_TIME)) second(s)..."
echo ""
fi
}
incrementPassedTests () {
passed_tests=$((passed_tests+1))
incrementTotalTests
}
incrementFailedTests () {
failed_tests=$((failed_tests+1))
incrementTotalTests
}
incrementTotalTests () {
total_tests=$((total_tests+1))
}
correctNumberOfArgs () {
if [ "${1}" = "${2}" ]; then
echo "true"
else
echo "false"
fi
}
numericCompare () {
awk -v n1="${1}" -v n2="${2}" \ 'BEGIN { print (n1 == n2) ? "true" : "false" }' 2>/dev/null
}