-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathassert.sh
More file actions
executable file
·567 lines (461 loc) · 14.6 KB
/
assert.sh
File metadata and controls
executable file
·567 lines (461 loc) · 14.6 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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
#!/usr/bin/env bash
function fail() {
local message="${1:-${FUNCNAME[1]}}"
local label
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
state::add_assertions_failed
console_results::print_failure_message "${label}" "$message"
}
function assert_true() {
local actual="$1"
# Check for expected literal values first
case "$actual" in
"true"|"0") state::add_assertions_passed; return ;;
"false"|"1") handle_bool_assertion_failure "true or 0" "$actual"; return ;;
esac
# Run command or eval and check the exit code
run_command_or_eval "$actual"
local exit_code=$?
if [[ $exit_code -ne 0 ]]; then
handle_bool_assertion_failure "command or function with zero exit code" "exit code: $exit_code"
else
state::add_assertions_passed
fi
}
function assert_false() {
local actual="$1"
# Check for expected literal values first
case "$actual" in
"false"|"1") state::add_assertions_passed; return ;;
"true"|"0") handle_bool_assertion_failure "false or 1" "$actual"; return ;;
esac
# Run command or eval and check the exit code
run_command_or_eval "$actual"
local exit_code=$?
if [[ $exit_code -eq 0 ]]; then
handle_bool_assertion_failure "command or function with non-zero exit code" "exit code: $exit_code"
else
state::add_assertions_passed
fi
}
function run_command_or_eval() {
local cmd="$1"
local eval_pattern='^eval'
local alias_pattern='^alias'
if [[ "$cmd" =~ $eval_pattern ]]; then
eval "${cmd#eval }" &> /dev/null
elif [[ "$(command -v "$cmd")" =~ $alias_pattern ]]; then
eval "$cmd" &> /dev/null
else
"$cmd" &> /dev/null
fi
return $?
}
function handle_bool_assertion_failure() {
local expected="$1"
local got="$2"
local label
label="$(helper::normalize_test_function_name "${FUNCNAME[2]}")"
state::add_assertions_failed
console_results::print_failed_test "$label" "$expected" "but got " "$got"
}
function assert_same() {
local expected="$1"
local actual="$2"
if [[ "$expected" != "$actual" ]]; then
local label
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
state::add_assertions_failed
console_results::print_failed_test "${label}" "${expected}" "but got " "${actual}"
return
fi
state::add_assertions_passed
}
function assert_equals() {
local expected="$1"
local actual="$2"
# Remove ANSI escape sequences (color codes)
local actual_cleaned
actual_cleaned=$(echo -e "$actual" | sed -r "s/\x1B\[[0-9;]*[mK]//g")
local expected_cleaned
expected_cleaned=$(echo -e "$expected" | sed -r "s/\x1B\[[0-9;]*[mK]//g")
# Remove all control characters and whitespace (optional, depending on your needs)
actual_cleaned=$(echo "$actual_cleaned" | tr -d '[:cntrl:]')
expected_cleaned=$(echo "$expected_cleaned" | tr -d '[:cntrl:]')
if [[ "$expected_cleaned" != "$actual_cleaned" ]]; then
local label
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
state::add_assertions_failed
console_results::print_failed_test "${label}" "${expected_cleaned}" "but got " "${actual_cleaned}"
return
fi
state::add_assertions_passed
}
function assert_not_equals() {
local expected="$1"
local actual="$2"
# Remove ANSI escape sequences (color codes)
local actual_cleaned
actual_cleaned=$(echo -e "$actual" | sed -r "s/\x1B\[[0-9;]*[mK]//g")
local expected_cleaned
expected_cleaned=$(echo -e "$expected" | sed -r "s/\x1B\[[0-9;]*[mK]//g")
# Remove all control characters and whitespace (optional, depending on your needs)
actual_cleaned=$(echo "$actual_cleaned" | tr -d '[:cntrl:]')
expected_cleaned=$(echo "$expected_cleaned" | tr -d '[:cntrl:]')
if [[ "$expected_cleaned" == "$actual_cleaned" ]]; then
local label
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
state::add_assertions_failed
console_results::print_failed_test "${label}" "${expected_cleaned}" "but got " "${actual_cleaned}"
return
fi
state::add_assertions_passed
}
function assert_empty() {
local expected="$1"
if [[ "$expected" != "" ]]; then
local label
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
state::add_assertions_failed
console_results::print_failed_test "${label}" "to be empty" "but got " "${expected}"
return
fi
state::add_assertions_passed
}
function assert_not_empty() {
local expected="$1"
if [[ "$expected" == "" ]]; then
local label
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
state::add_assertions_failed
console_results::print_failed_test "${label}" "to not be empty" "but got " "${expected}"
return
fi
state::add_assertions_passed
}
function assert_not_same() {
local expected="$1"
local actual="$2"
if [[ "$expected" == "$actual" ]]; then
local label
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
state::add_assertions_failed
console_results::print_failed_test "${label}" "${expected}" "but got " "${actual}"
return
fi
state::add_assertions_passed
}
function assert_contains() {
local expected="$1"
local actual_arr=("${@:2}")
local actual
actual=$(printf '%s\n' "${actual_arr[@]}")
if ! [[ $actual == *"$expected"* ]]; then
local label
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
state::add_assertions_failed
console_results::print_failed_test "${label}" "${actual}" "to contain" "${expected}"
return
fi
state::add_assertions_passed
}
function assert_contains_ignore_case() {
local expected="$1"
local actual="$2"
shopt -s nocasematch
if ! [[ $actual =~ $expected ]]; then
local label
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
state::add_assertions_failed
console_results::print_failed_test "${label}" "${actual}" "to contain" "${expected}"
shopt -u nocasematch
return
fi
shopt -u nocasematch
state::add_assertions_passed
}
function assert_not_contains() {
local expected="$1"
local actual_arr=("${@:2}")
local actual
actual=$(printf '%s\n' "${actual_arr[@]}")
if [[ $actual == *"$expected"* ]]; then
local label
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
state::add_assertions_failed
console_results::print_failed_test "${label}" "${actual}" "to not contain" "${expected}"
return
fi
state::add_assertions_passed
}
function assert_matches() {
local expected="$1"
local actual_arr=("${@:2}")
local actual
actual=$(printf '%s\n' "${actual_arr[@]}")
if ! [[ $actual =~ $expected ]]; then
local label
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
state::add_assertions_failed
console_results::print_failed_test "${label}" "${actual}" "to match" "${expected}"
return
fi
state::add_assertions_passed
}
function assert_not_matches() {
local expected="$1"
local actual_arr=("${@:2}")
local actual
actual=$(printf '%s\n' "${actual_arr[@]}")
if [[ $actual =~ $expected ]]; then
local label
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
state::add_assertions_failed
console_results::print_failed_test "${label}" "${actual}" "to not match" "${expected}"
return
fi
state::add_assertions_passed
}
function assert_exec() {
local cmd="$1"
shift
local expected_exit=0
local expected_stdout=""
local expected_stderr=""
local check_stdout=false
local check_stderr=false
while [[ $# -gt 0 ]]; do
case "$1" in
--exit)
expected_exit="$2"
shift 2
;;
--stdout)
expected_stdout="$2"
check_stdout=true
shift 2
;;
--stderr)
expected_stderr="$2"
check_stderr=true
shift 2
;;
*)
shift
;;
esac
done
local stdout_file stderr_file
stdout_file=$(mktemp)
stderr_file=$(mktemp)
eval "$cmd" >"$stdout_file" 2>"$stderr_file"
local exit_code=$?
local stdout
stdout=$(cat "$stdout_file")
local stderr
stderr=$(cat "$stderr_file")
rm -f "$stdout_file" "$stderr_file"
local expected_desc="exit: $expected_exit"
local actual_desc="exit: $exit_code"
local failed=0
if [[ "$exit_code" -ne "$expected_exit" ]]; then
failed=1
fi
if $check_stdout; then
expected_desc+=$'\n'"stdout: $expected_stdout"
actual_desc+=$'\n'"stdout: $stdout"
if [[ "$stdout" != "$expected_stdout" ]]; then
failed=1
fi
fi
if $check_stderr; then
expected_desc+=$'\n'"stderr: $expected_stderr"
actual_desc+=$'\n'"stderr: $stderr"
if [[ "$stderr" != "$expected_stderr" ]]; then
failed=1
fi
fi
if [[ $failed -eq 1 ]]; then
local label
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
state::add_assertions_failed
console_results::print_failed_test "$label" "$expected_desc" "but got " "$actual_desc"
return
fi
state::add_assertions_passed
}
function assert_exit_code() {
local actual_exit_code=${3-"$?"}
local expected_exit_code="$1"
if [[ "$actual_exit_code" -ne "$expected_exit_code" ]]; then
local label
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
state::add_assertions_failed
console_results::print_failed_test "${label}" "${actual_exit_code}" "to be" "${expected_exit_code}"
return
fi
state::add_assertions_passed
}
function assert_successful_code() {
local actual_exit_code=${3-"$?"}
local expected_exit_code=0
if [[ "$actual_exit_code" -ne "$expected_exit_code" ]]; then
local label
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
state::add_assertions_failed
console_results::print_failed_test "${label}" "${actual_exit_code}" "to be exactly" "${expected_exit_code}"
return
fi
state::add_assertions_passed
}
function assert_general_error() {
local actual_exit_code=${3-"$?"}
local expected_exit_code=1
if [[ "$actual_exit_code" -ne "$expected_exit_code" ]]; then
local label
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
state::add_assertions_failed
console_results::print_failed_test "${label}" "${actual_exit_code}" "to be exactly" "${expected_exit_code}"
return
fi
state::add_assertions_passed
}
function assert_command_not_found() {
local actual_exit_code=${3-"$?"}
local expected_exit_code=127
if [[ $actual_exit_code -ne "$expected_exit_code" ]]; then
local label
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
state::add_assertions_failed
console_results::print_failed_test "${label}" "${actual_exit_code}" "to be exactly" "${expected_exit_code}"
return
fi
state::add_assertions_passed
}
function assert_string_starts_with() {
local expected="$1"
local actual_arr=("${@:2}")
local actual
actual=$(printf '%s\n' "${actual_arr[@]}")
if [[ $actual != "$expected"* ]]; then
local label
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
state::add_assertions_failed
console_results::print_failed_test "${label}" "${actual}" "to start with" "${expected}"
return
fi
state::add_assertions_passed
}
function assert_string_not_starts_with() {
local expected="$1"
local actual="$2"
if [[ $actual == "$expected"* ]]; then
local label
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
state::add_assertions_failed
console_results::print_failed_test "${label}" "${actual}" "to not start with" "${expected}"
return
fi
state::add_assertions_passed
}
function assert_string_ends_with() {
local expected="$1"
local actual_arr=("${@:2}")
local actual
actual=$(printf '%s\n' "${actual_arr[@]}")
if [[ $actual != *"$expected" ]]; then
local label
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
state::add_assertions_failed
console_results::print_failed_test "${label}" "${actual}" "to end with" "${expected}"
return
fi
state::add_assertions_passed
}
function assert_string_not_ends_with() {
local expected="$1"
local actual_arr=("${@:2}")
local actual
actual=$(printf '%s\n' "${actual_arr[@]}")
if [[ $actual == *"$expected" ]]; then
local label
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
state::add_assertions_failed
console_results::print_failed_test "${label}" "${actual}" "to not end with" "${expected}"
return
fi
state::add_assertions_passed
}
function assert_less_than() {
local expected="$1"
local actual="$2"
if ! [[ "$actual" -lt "$expected" ]]; then
local label
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
state::add_assertions_failed
console_results::print_failed_test "${label}" "${actual}" "to be less than" "${expected}"
return
fi
state::add_assertions_passed
}
function assert_less_or_equal_than() {
local expected="$1"
local actual="$2"
if ! [[ "$actual" -le "$expected" ]]; then
local label
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
state::add_assertions_failed
console_results::print_failed_test "${label}" "${actual}" "to be less or equal than" "${expected}"
return
fi
state::add_assertions_passed
}
function assert_greater_than() {
local expected="$1"
local actual="$2"
if ! [[ "$actual" -gt "$expected" ]]; then
local label
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
state::add_assertions_failed
console_results::print_failed_test "${label}" "${actual}" "to be greater than" "${expected}"
return
fi
state::add_assertions_passed
}
function assert_greater_or_equal_than() {
local expected="$1"
local actual="$2"
if ! [[ "$actual" -ge "$expected" ]]; then
local label
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
state::add_assertions_failed
console_results::print_failed_test "${label}" "${actual}" "to be greater or equal than" "${expected}"
return
fi
state::add_assertions_passed
}
function assert_line_count() {
local expected="$1"
local input_arr=("${@:2}")
local input_str
input_str=$(printf '%s\n' "${input_arr[@]}")
if [ -z "$input_str" ]; then
local actual=0
else
local actual
actual=$(echo "$input_str" | wc -l | tr -d '[:blank:]')
local additional_new_lines
additional_new_lines=$(echo "$input_str" | grep -o '\\n' | wc -l | tr -d '[:blank:]')
((actual+=additional_new_lines))
fi
if [[ "$expected" != "$actual" ]]; then
local label
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
state::add_assertions_failed
console_results::print_failed_test "${label}" "${input_str}"\
"to contain number of lines equal to" "${expected}"\
"but found" "${actual}"
return
fi
state::add_assertions_passed
}