-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_runner
More file actions
executable file
·224 lines (217 loc) · 5.97 KB
/
test_runner
File metadata and controls
executable file
·224 lines (217 loc) · 5.97 KB
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
#!/usr/bin/env bash
readonly MY_PATH=$(realpath "$0")
# echo "$MY_PATH"
MY_DIR=""
if [[ "$MY_PATH" =~ ^(.*)/[^/]+$ ]]; then
readonly MY_DIR="${BASH_REMATCH[1]}"
else
MY_DIR="Failed to get dir"
fi
# echo "$MY_DIR"
declare -r DEFAULT_TEST_CASES_DIRECTORY="$MY_DIR/test/"
declare -r DEFAULT_TEST_CASES_SUFFIX="_test_cases"
declare -r DEFAULT_TEST_CASES_EXTENSION=".txt"
show_help() {
# or: $0 [-c COARSENESS] [-d SEPARATOR] NUMERATOR DENOMINATOR
# -c, --coarseness COARSENESS: precision. Defaults to $DEFAULT_COARSENESS.
cat << __EOF__
Run tests for simple scripts
Usage: $0 COMMAND [PATH [NAME]]
or: $0 --help
or: $0 --version
Options:
-h, --help: Shows this help text.
-v, --version: Shows version info.
__EOF__
}
show_version() {
cat << __EOF__
$0 2.0.0
Copyright (C) 2025 Justin Morris
__EOF__
}
declare testPath="$DEFAULT_TEST_CASES_DIRECTORY"
declare testFile=''
declare testCommand=''
declare delimiter='>'
declare -n opt1=testCommand opt2=testPath opt3=testFile
while [[ -n "${1+x}" ]]; do
case "$1" in
(-h | --help)
show_help
exit
;;
(-v | --version)
show_version
exit
;;
# (-c | --coarseness)
# shift
# coarseInput="$1"
# shift
# ;;
(*)
if [[ -n "$opt1" ]]; then
if [[ -n "$opt2" ]]; then
if [[ -n "$opt3" ]]; then
exit 1
else
opt3="$1"
fi
else
opt2="$1"
fi
else
opt1="$1"
fi
shift
;;
esac
done
# echo "testCommand: $testCommand testFile: $testFile testPath: $testPath"
if (( ${#testFile} == 0 )); then
declare -r testFile="$testCommand"
fi
testPath+="$testFile"
if [ -f "$testPath" ]; then
declare -r testPath
elif [ -f "$testPath$DEFAULT_TEST_CASES_SUFFIX" ]; then
declare -r testPath="$testPath$DEFAULT_TEST_CASES_SUFFIX"
elif [ -f "$testPath$DEFAULT_TEST_CASES_SUFFIX$DEFAULT_TEST_CASES_EXTENSION" ]; then
declare -r testPath="$testPath$DEFAULT_TEST_CASES_SUFFIX$DEFAULT_TEST_CASES_EXTENSION"
elif [ -f "$DEFAULT_TEST_CASES_DIRECTORY$testPath" ]; then
declare -r testPath="$DEFAULT_TEST_CASES_DIRECTORY$testPath"
elif [ -f "$DEFAULT_TEST_CASES_DIRECTORY$testPath$DEFAULT_TEST_CASES_SUFFIX" ]; then
declare -r testPath="$DEFAULT_TEST_CASES_DIRECTORY$testPath$DEFAULT_TEST_CASES_SUFFIX"
elif [ -f "$DEFAULT_TEST_CASES_DIRECTORY$testPath$DEFAULT_TEST_CASES_SUFFIX$DEFAULT_TEST_CASES_EXTENSION" ]; then
declare -r testPath="$DEFAULT_TEST_CASES_DIRECTORY$testPath$DEFAULT_TEST_CASES_SUFFIX$DEFAULT_TEST_CASES_EXTENSION"
else
echo "Can't find test file '$testPath'" 1>&2
exit 1
fi
# echo "testCommand: $testCommand testFile: $testFile testPath: $testPath"
declare -r ORIG_IFS="$IFS"
declare -a TEST_RESULTS=()
declare -a TEST_ASSERTIONS=()
declare -a TEST_ORDER=()
declare -a TEST_LABELS=()
shopt -s extglob globasciiranges nocasematch
do_read() {
local params=''
local results=''
local overflow=''
local assertion=''
local label=''
while read -s -t 0; do
read -sr params results overflow
if (( ${#params} <= 0 && ${#results} <= 0 )); then
break
# elif (( ${#params} <= 0 || ${#results} <= 0 )); then
# label+="$params"
# label+=$'\n'
# continue
elif (( ${#results} <= 0 )); then
if [[ "$params" =~ ^[[:blank:]]*assert(_?not[[:blank:]])?(.*)$ ]]; then
if ((${#BASH_REMATCH[1]} > 0)); then
assertion="assertNot"
else
assertion="assert"
fi
params="${BASH_REMATCH[3]}"
else
label+="$params"
label+=$'\n'
continue
fi
elif [ "${params:0:1}" = '#' ]; then
label+="$params$delimiter$results"
[ -n "$overflow" ] && label+="$delimiter$overflow"
label+=$'\n'
continue
elif [ -n "$overflow" ]; then
assertion="$params"
params="$results"
results="$overflow"
elif [[ "$params" =~ ^[[:blank:]]*(assert[^[:blank:]]*)(.*)$ ]]; then
assertion="${BASH_REMATCH[1]}"
params="${BASH_REMATCH[2]}"
else
assertion="assertEquals"
fi
# local -i index="${#TEST_ORDER[@]}"
TEST_ORDER+=("$params")
TEST_ASSERTIONS+=("$assertion")
TEST_RESULTS+=("$results")
TEST_LABELS+=("$label")
label=''
done
# echo $?
return
}
IFS="$delimiter"
do_read < "$testPath"
IFS="$ORIG_IFS"
succeeded='true'
echo -n $'\n\n# Running:\n\n'
# for params in "${!TESTS[@]}"; do
# for params in "${TEST_ORDER[@]}"; do
declare -r successfulTestOutput="$(tput setaf 2).$(tput setaf 7)"
declare -r failingTestOutput="$(tput setaf 1)F$(tput setaf 7)"
for ((i = 0; i < ${#TEST_ORDER[@]}; i++)); do
params="${TEST_ORDER[$i]}"
results="${TEST_RESULTS[$i]}"
case "${TEST_ASSERTIONS[$i]}" in
(^assert$)
if eval "$testCommand $params" &>"/dev/null"; then
echo -n "$successfulTestOutput"
else
echo "$failingTestOutput"
echo "${TEST_LABELS[$i]}$testCommand $params: Expected true, was false" 1>&2
succeeded='false'
fi
;;
(^assert_?not$)
if ! eval "$testCommand $params" &>"/dev/null"; then
echo -n "$successfulTestOutput"
else
echo "$failingTestOutput"
echo "${TEST_LABELS[$i]}$testCommand $params: Expected false, was true" 1>&2
succeeded='false'
fi
;;
(^assert_?not_?equals?$)
# shellcheck disable=SC2086
output="$($testCommand $params)"
if [ "$output" = "$results" ]; then
echo "$failingTestOutput"
echo "${TEST_LABELS[$i]}$testCommand $params: Expected anything but ($results)" 1>&2
succeeded='false'
else
echo -n "$successfulTestOutput"
fi
;;
(^assert_?equals?$|*)
# shellcheck disable=SC2086
output="$($testCommand $params)"
if [ "$output" != "$results" ]; then
echo "$failingTestOutput"
echo "${TEST_LABELS[$i]}$testCommand $params: Result ($output) != Expected ($results)" 1>&2
succeeded='false'
else
echo -n "$successfulTestOutput"
fi
esac
# # shellcheck disable=SC2086
# output="$($testCommand $params)"
# if [ "$output" != "${TEST_RESULTS[$params]}" ]; then
# echo "$(tput setaf 1)F$(tput setaf 7)"
# echo "${TEST_LABELS[$params]}$testCommand $params: Result ($output) != Expected (${TEST_RESULTS[$params]})" 1>&2
# succeeded='false'
# else
# echo -n "$(tput setaf 2).$(tput setaf 7)"
# fi
done
echo -n $'\n\n'
echo "Succeeded: $succeeded"
eval $succeeded
exit $?