This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
test.sh
executable file
·251 lines (197 loc) · 5.92 KB
/
test.sh
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
#!/usr/bin/env bash
# shellcheck source=/dev/null disable=2178,2128
#
# Tests for the Pure Bash Bible.
test_trim_string() {
result="$(trim_string " Hello, World ")"
assert_equals "$result" "Hello, World"
}
test_trim_all() {
result="$(trim_all " Hello, World ")"
assert_equals "$result" "Hello, World"
}
test_regex() {
result="$(regex "#FFFFFF" '^(#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3}))$')"
assert_equals "$result" "#FFFFFF"
}
test_lower() {
result="$(lower "HeLlO")"
assert_equals "$result" "hello"
}
test_upper() {
result="$(upper "HeLlO")"
assert_equals "$result" "HELLO"
}
test_reverse_case() {
result="$(reverse_case "HeLlO")"
assert_equals "$result" "hElLo"
}
test_trim_quotes() {
result="$(trim_quotes "\"te'st' 'str'ing\"")"
assert_equals "$result" "test string"
}
test_strip_all() {
result="$(strip_all "The Quick Brown Fox" "[aeiou]")"
assert_equals "$result" "Th Qck Brwn Fx"
}
test_strip() {
result="$(strip "The Quick Brown Fox" "[aeiou]")"
assert_equals "$result" "Th Quick Brown Fox"
}
test_lstrip() {
result="$(lstrip "!:IHello" "!:I")"
assert_equals "$result" "Hello"
}
test_rstrip() {
result="$(rstrip "Hello!:I" "!:I")"
assert_equals "$result" "Hello"
}
test_urlencode() {
result="$(urlencode "https://github.com/dylanaraps/pure-bash-bible")"
assert_equals "$result" "https%3A%2F%2Fgithub.com%2Fdylanaraps%2Fpure-bash-bible"
}
test_urldecode() {
result="$(urldecode "https%3A%2F%2Fgithub.com%2Fdylanaraps%2Fpure-bash-bible")"
assert_equals "$result" "https://github.com/dylanaraps/pure-bash-bible"
}
test_reverse_array() {
shopt -s compat44
IFS=$'\n' read -d "" -ra result < <(reverse_array 1 2 3 4 5)
assert_equals "${result[*]}" "5 4 3 2 1"
shopt -u compat44
}
test_cycle() {
# shellcheck disable=2034
arr=(a b c d)
result="$(cycle; cycle; cycle)"
assert_equals "$result" "a b c "
}
test_head() {
printf '%s\n%s\n\n\n' "hello" "world" > test_file
result="$(head 2 test_file)"
assert_equals "$result" $'hello\nworld'
}
test_tail() {
printf '\n\n\n%s\n%s\n' "hello" "world" > test_file
result="$(tail 2 test_file)"
assert_equals "$result" $'hello\nworld'
}
test_lines() {
printf '\n\n\n\n\n\n\n\n' > test_file
result="$(lines test_file)"
assert_equals "$result" "8"
}
test_lines_loop() {
printf '\n\n\n\n\n\n\n\n' > test_file
result="$(lines_loop test_file)"
assert_equals "$result" "8"
}
test_count() {
result="$(count ./{README.m,LICENSE.m,.travis.ym}*)"
assert_equals "$result" "3"
}
test_dirname() {
result="$(dirname "/home/black/Pictures/Wallpapers/1.jpg")"
assert_equals "$result" "/home/black/Pictures/Wallpapers"
result="$(dirname "/")"
assert_equals "$result" "/"
result="$(dirname "/foo")"
assert_equals "$result" "/"
result="$(dirname ".")"
assert_equals "$result" "."
result="$(dirname "/foo/foo")"
assert_equals "$result" "/foo"
result="$(dirname "something/")"
assert_equals "$result" "."
result="$(dirname "//")"
assert_equals "$result" "/"
result="$(dirname "//foo")"
assert_equals "$result" "/"
result="$(dirname "")"
assert_equals "$result" "."
result="$(dirname "something//")"
assert_equals "$result" "."
result="$(dirname "something/////////////////////")"
assert_equals "$result" "."
result="$(dirname "something/////////////////////a")"
assert_equals "$result" "something"
result="$(dirname "something//////////.///////////")"
assert_equals "$result" "something"
result="$(dirname "//////")"
assert_equals "$result" "/"
}
test_basename() {
result="$(basename "/home/black/Pictures/Wallpapers/1.jpg")"
assert_equals "$result" "1.jpg"
}
test_hex_to_rgb() {
result="$(hex_to_rgb "#FFFFFF")"
assert_equals "$result" "255 255 255"
result="$(hex_to_rgb "000000")"
assert_equals "$result" "0 0 0"
}
test_rgb_to_hex() {
result="$(rgb_to_hex 0 0 0)"
assert_equals "$result" "#000000"
}
test_date() {
result="$(date "%C")"
assert_equals "$result" "20"
}
test_read_sleep() {
result="$((SECONDS+1))"
read_sleep 1
assert_equals "$result" "$SECONDS"
}
test_bar() {
result="$(bar 50 10)"
assert_equals "${result//$'\r'}" "[----- ]"
}
test_get_functions() {
IFS=$'\n' read -d "" -ra functions < <(get_functions)
assert_equals "${functions[0]}" "assert_equals"
}
test_extract() {
printf '{\nhello, world\n}\n' > test_file
result="$(extract test_file "{" "}")"
assert_equals "$result" "hello, world"
}
test_split() {
IFS=$'\n' read -d "" -ra result < <(split "hello,world,my,name,is,john" ",")
assert_equals "${result[*]}" "hello world my name is john"
}
assert_equals() {
if [[ "$1" == "$2" ]]; then
((pass+=1))
status=$'\e[32m✔'
else
((fail+=1))
status=$'\e[31m✖'
local err="(\"$1\" != \"$2\")"
fi
printf ' %s\e[m | %s\n' "$status" "${FUNCNAME[1]/test_} $err"
}
main() {
trap 'rm readme_code test_file' EXIT
# Extract code blocks from the README.
while IFS=$'\n' read -r line; do
[[ "$code" && "$line" != \`\`\` ]] && printf '%s\n' "$line"
[[ "$line" =~ ^\`\`\`sh$ ]] && code=1
[[ "$line" =~ ^\`\`\`$ ]] && code=
done < README.md > readme_code
# Run shellcheck and source the code.
shellcheck -s bash readme_code test.sh build.sh || exit 1
. readme_code
head="-> Running tests on the Pure Bash Bible.."
printf '\n%s\n%s\n' "$head" "${head//?/-}"
# Generate the list of tests to run.
IFS=$'\n' read -d "" -ra funcs < <(declare -F)
for func in "${funcs[@]//declare -f }"; do
[[ "$func" == test_* ]] && "$func";
done
comp="Completed $((fail+pass)) tests. ${pass:-0} passed, ${fail:-0} failed."
printf '%s\n%s\n\n' "${comp//?/-}" "$comp"
# If a test failed, exit with '1'.
((fail>0)) || exit 0 && exit 1
}
main "$@"