-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathassert.sh
More file actions
executable file
·841 lines (691 loc) · 22.4 KB
/
assert.sh
File metadata and controls
executable file
·841 lines (691 loc) · 22.4 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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
#!/usr/bin/env bash
# Helper to mark assertion as failed and set the guard flag
function bashunit::assert::mark_failed() {
bashunit::state::add_assertions_failed
bashunit::state::mark_assertion_failed_in_test
}
# Guard clause to skip assertion if one already failed in test (when stop-on-assertion is enabled)
function bashunit::assert::should_skip() {
bashunit::env::is_stop_on_assertion_failure_enabled && ((_BASHUNIT_ASSERTION_FAILED_IN_TEST))
}
function bashunit::fail() {
bashunit::assert::should_skip && return 0
local message="${1:-${FUNCNAME[1]}}"
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failure_message "${label}" "$message"
}
function assert_true() {
bashunit::assert::should_skip && return 0
local actual="$1"
# Check for expected literal values first
case "$actual" in
"true" | "0")
bashunit::state::add_assertions_passed
return
;;
"false" | "1")
bashunit::handle_bool_assertion_failure "true or 0" "$actual"
return
;;
esac
# Run command or eval and check the exit code
bashunit::run_command_or_eval "$actual"
local exit_code=$?
if [ "$exit_code" -ne 0 ]; then
bashunit::handle_bool_assertion_failure "command or function with zero exit code" "exit code: $exit_code"
else
bashunit::state::add_assertions_passed
fi
}
function assert_false() {
bashunit::assert::should_skip && return 0
local actual="$1"
# Check for expected literal values first
case "$actual" in
"false" | "1")
bashunit::state::add_assertions_passed
return
;;
"true" | "0")
bashunit::handle_bool_assertion_failure "false or 1" "$actual"
return
;;
esac
# Run command or eval and check the exit code
bashunit::run_command_or_eval "$actual"
local exit_code=$?
if [ "$exit_code" -eq 0 ]; then
bashunit::handle_bool_assertion_failure "command or function with non-zero exit code" "exit code: $exit_code"
else
bashunit::state::add_assertions_passed
fi
}
function bashunit::run_command_or_eval() {
local cmd="$1"
case "$cmd" in
eval\ * | eval)
eval "${cmd#eval }" &>/dev/null
;;
*)
if [ "$(command -v "$cmd" | "$GREP" -cE '^alias' || true)" -gt 0 ]; then
eval "$cmd" &>/dev/null
else
"$cmd" &>/dev/null
fi
;;
esac
return $?
}
function bashunit::handle_bool_assertion_failure() {
local expected="$1"
local got="$2"
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "$label" "$expected" "but got " "$got"
}
function assert_same() {
bashunit::assert::should_skip && return 0
local expected="$1"
local actual="$2"
if [ "$expected" != "$actual" ]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${expected}" "but got " "${actual}"
return
fi
bashunit::state::add_assertions_passed
}
function assert_equals() {
bashunit::assert::should_skip && return 0
local expected="$1"
local actual="$2"
local actual_cleaned
actual_cleaned=$(bashunit::str::strip_ansi "$actual")
local expected_cleaned
expected_cleaned=$(bashunit::str::strip_ansi "$expected")
if [ "$expected_cleaned" != "$actual_cleaned" ]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${expected_cleaned}" "but got " "${actual_cleaned}"
return
fi
bashunit::state::add_assertions_passed
}
function assert_not_equals() {
bashunit::assert::should_skip && return 0
local expected="$1"
local actual="$2"
local actual_cleaned
actual_cleaned=$(bashunit::str::strip_ansi "$actual")
local expected_cleaned
expected_cleaned=$(bashunit::str::strip_ansi "$expected")
if [ "$expected_cleaned" = "$actual_cleaned" ]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${expected_cleaned}" "to not be" "${actual_cleaned}"
return
fi
bashunit::state::add_assertions_passed
}
function assert_empty() {
bashunit::assert::should_skip && return 0
local expected="$1"
if [ "$expected" != "" ]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "to be empty" "but got " "${expected}"
return
fi
bashunit::state::add_assertions_passed
}
function assert_not_empty() {
bashunit::assert::should_skip && return 0
local expected="$1"
if [ "$expected" = "" ]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "to not be empty" "but got " "${expected}"
return
fi
bashunit::state::add_assertions_passed
}
function assert_not_same() {
bashunit::assert::should_skip && return 0
local expected="$1"
local actual="$2"
if [ "$expected" = "$actual" ]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${expected}" "to not be" "${actual}"
return
fi
bashunit::state::add_assertions_passed
}
function assert_contains() {
bashunit::assert::should_skip && return 0
local IFS=$' \t\n'
local expected="$1"
local -a actual_arr
actual_arr=("${@:2}")
local actual
actual=$(printf '%s\n' "${actual_arr[@]}")
case "$actual" in
*"$expected"*) ;;
*)
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${actual}" "to contain" "${expected}"
return
;;
esac
bashunit::state::add_assertions_passed
}
function assert_contains_ignore_case() {
bashunit::assert::should_skip && return 0
local expected="$1"
local actual="$2"
# Bash 3.0 compatible: use tr for case-insensitive comparison
# (shopt nocasematch was introduced in Bash 3.1)
local expected_lower
local actual_lower
expected_lower=$(printf '%s' "$expected" | tr '[:upper:]' '[:lower:]')
actual_lower=$(printf '%s' "$actual" | tr '[:upper:]' '[:lower:]')
case "$actual_lower" in
*"$expected_lower"*) ;;
*)
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${actual}" "to contain" "${expected}"
return
;;
esac
bashunit::state::add_assertions_passed
}
function assert_not_contains() {
bashunit::assert::should_skip && return 0
local IFS=$' \t\n'
local expected="$1"
local -a actual_arr
actual_arr=("${@:2}")
local actual
actual=$(printf '%s\n' "${actual_arr[@]}")
case "$actual" in
*"$expected"*)
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${actual}" "to not contain" "${expected}"
return
;;
esac
bashunit::state::add_assertions_passed
}
function assert_matches() {
bashunit::assert::should_skip && return 0
local IFS=$' \t\n'
local expected="$1"
local -a actual_arr
actual_arr=("${@:2}")
local actual
actual=$(printf '%s\n' "${actual_arr[@]}")
if [ "$(printf '%s' "$actual" | "$GREP" -cE "$expected" || true)" -eq 0 ]; then
# Retry with newlines collapsed for cross-line patterns
if [ "$(printf '%s' "$actual" | tr '\n' ' ' | "$GREP" -cE "$expected" || true)" -eq 0 ]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${actual}" "to match" "${expected}"
return
fi
fi
bashunit::state::add_assertions_passed
}
function assert_not_matches() {
bashunit::assert::should_skip && return 0
local IFS=$' \t\n'
local expected="$1"
local -a actual_arr
actual_arr=("${@:2}")
local actual
actual=$(printf '%s\n' "${actual_arr[@]}")
# Check both line-by-line and with newlines collapsed for cross-line patterns
if [ "$(printf '%s' "$actual" | "$GREP" -cE "$expected" || true)" -gt 0 ] ||
[ "$(printf '%s' "$actual" | tr '\n' ' ' | "$GREP" -cE "$expected" || true)" -gt 0 ]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${actual}" "to not match" "${expected}"
return
fi
bashunit::state::add_assertions_passed
}
function assert_exec() {
bashunit::assert::should_skip && return 0
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="$expected_desc"$'\n'"stdout: $expected_stdout"
actual_desc="$actual_desc"$'\n'"stdout: $stdout"
if [ "$stdout" != "$expected_stdout" ]; then
failed=1
fi
fi
if $check_stderr; then
expected_desc="$expected_desc"$'\n'"stderr: $expected_stderr"
actual_desc="$actual_desc"$'\n'"stderr: $stderr"
if [ "$stderr" != "$expected_stderr" ]; then
failed=1
fi
fi
if [ "$failed" -eq 1 ]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "$label" "$expected_desc" "but got " "$actual_desc"
return
fi
bashunit::state::add_assertions_passed
}
function assert_exit_code() {
local actual_exit_code=${3-"$?"} # Capture $? before guard check
bashunit::assert::should_skip && return 0
local expected_exit_code="$1"
if [ "$actual_exit_code" -ne "$expected_exit_code" ]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${actual_exit_code}" "to be" "${expected_exit_code}"
return
fi
bashunit::state::add_assertions_passed
}
function assert_successful_code() {
local actual_exit_code=${3-"$?"} # Capture $? before guard check
bashunit::assert::should_skip && return 0
local expected_exit_code=0
if [ "$actual_exit_code" -ne "$expected_exit_code" ]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test \
"${label}" "${actual_exit_code}" "to be exactly" "${expected_exit_code}"
return
fi
bashunit::state::add_assertions_passed
}
function assert_unsuccessful_code() {
local actual_exit_code=${3-"$?"} # Capture $? before guard check
bashunit::assert::should_skip && return 0
if [ "$actual_exit_code" -eq 0 ]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${actual_exit_code}" "to be non-zero" "but was 0"
return
fi
bashunit::state::add_assertions_passed
}
function assert_general_error() {
local actual_exit_code=${3-"$?"} # Capture $? before guard check
bashunit::assert::should_skip && return 0
local expected_exit_code=1
if [ "$actual_exit_code" -ne "$expected_exit_code" ]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test \
"${label}" "${actual_exit_code}" "to be exactly" "${expected_exit_code}"
return
fi
bashunit::state::add_assertions_passed
}
function assert_command_not_found() {
local actual_exit_code=${3-"$?"} # Capture $? before guard check
bashunit::assert::should_skip && return 0
local expected_exit_code=127
if [ "$actual_exit_code" -ne "$expected_exit_code" ]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test \
"${label}" "${actual_exit_code}" "to be exactly" "${expected_exit_code}"
return
fi
bashunit::state::add_assertions_passed
}
function assert_string_starts_with() {
bashunit::assert::should_skip && return 0
local IFS=$' \t\n'
local expected="$1"
local -a actual_arr
actual_arr=("${@:2}")
local actual
actual=$(printf '%s\n' "${actual_arr[@]}")
case "$actual" in
"$expected"*) ;;
*)
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${actual}" "to start with" "${expected}"
return
;;
esac
bashunit::state::add_assertions_passed
}
function assert_string_not_starts_with() {
bashunit::assert::should_skip && return 0
local expected="$1"
local actual="$2"
case "$actual" in
"$expected"*)
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${actual}" "to not start with" "${expected}"
return
;;
esac
bashunit::state::add_assertions_passed
}
function assert_string_ends_with() {
bashunit::assert::should_skip && return 0
local IFS=$' \t\n'
local expected="$1"
local -a actual_arr
actual_arr=("${@:2}")
local actual
actual=$(printf '%s\n' "${actual_arr[@]}")
case "$actual" in
*"$expected") ;;
*)
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${actual}" "to end with" "${expected}"
return
;;
esac
bashunit::state::add_assertions_passed
}
function assert_string_not_ends_with() {
bashunit::assert::should_skip && return 0
local IFS=$' \t\n'
local expected="$1"
local -a actual_arr
actual_arr=("${@:2}")
local actual
actual=$(printf '%s\n' "${actual_arr[@]}")
case "$actual" in
*"$expected")
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${actual}" "to not end with" "${expected}"
return
;;
esac
bashunit::state::add_assertions_passed
}
function assert_less_than() {
bashunit::assert::should_skip && return 0
local expected="$1"
local actual="$2"
if ! [ "$actual" -lt "$expected" ]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${actual}" "to be less than" "${expected}"
return
fi
bashunit::state::add_assertions_passed
}
function assert_less_or_equal_than() {
bashunit::assert::should_skip && return 0
local expected="$1"
local actual="$2"
if ! [ "$actual" -le "$expected" ]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${actual}" "to be less or equal than" "${expected}"
return
fi
bashunit::state::add_assertions_passed
}
function assert_greater_than() {
bashunit::assert::should_skip && return 0
local expected="$1"
local actual="$2"
if ! [ "$actual" -gt "$expected" ]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${actual}" "to be greater than" "${expected}"
return
fi
bashunit::state::add_assertions_passed
}
function assert_greater_or_equal_than() {
bashunit::assert::should_skip && return 0
local expected="$1"
local actual="$2"
if ! [ "$actual" -ge "$expected" ]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${actual}" "to be greater or equal than" "${expected}"
return
fi
bashunit::state::add_assertions_passed
}
function assert_line_count() {
bashunit::assert::should_skip && return 0
local IFS=$' \t\n'
local expected="$1"
local -a input_arr
input_arr=("${@:2}")
local input_str
input_str=$(printf '%s\n' ${input_arr+"${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=$(grep -o '\\n' <<<"$input_str" | wc -l | tr -d '[:blank:]')
actual=$((actual + additional_new_lines))
fi
if [ "$expected" != "$actual" ]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${input_str}" \
"to contain number of lines equal to" "${expected}" \
"but found" "${actual}"
return
fi
bashunit::state::add_assertions_passed
}
function bashunit::format_to_regex() {
local format="$1"
local regex=""
local i=0
local len=${#format}
while [ $i -lt "$len" ]; do
local char="${format:$i:1}"
if [ "$char" = "%" ] && [ $((i + 1)) -lt "$len" ]; then
local next="${format:$((i + 1)):1}"
case "$next" in
d) regex="${regex}[0-9]+" ;;
i) regex="${regex}[+-]?[0-9]+" ;;
f) regex="${regex}[+-]?[0-9]*\\.?[0-9]+" ;;
s) regex="${regex}[^ ]+" ;;
x) regex="${regex}[0-9a-fA-F]+" ;;
e) regex="${regex}[+-]?[0-9]*\\.?[0-9]+[eE][+-]?[0-9]+" ;;
%) regex="${regex}%" ;;
*)
regex="${regex}%${next}"
;;
esac
i=$((i + 2))
else
case "$char" in
. | '*' | '+' | '?' | '(' | ')' | '[' | ']' | '{' | '}' | '|' | '^' | '$')
regex="${regex}\\${char}"
;;
\\)
regex="${regex}\\\\"
;;
*)
regex="${regex}${char}"
;;
esac
i=$((i + 1))
fi
done
printf '%s' "^${regex}$"
}
function assert_string_matches_format() {
bashunit::assert::should_skip && return 0
local format="$1"
local actual="$2"
local regex
regex="$(bashunit::format_to_regex "$format")"
if [ "$(printf '%s' "$actual" | "$GREP" -cE "$regex" || true)" -eq 0 ]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${actual}" "to match format" "${format}"
return
fi
bashunit::state::add_assertions_passed
}
function assert_string_not_matches_format() {
bashunit::assert::should_skip && return 0
local format="$1"
local actual="$2"
local regex
regex="$(bashunit::format_to_regex "$format")"
if [ "$(printf '%s' "$actual" | "$GREP" -cE "$regex" || true)" -gt 0 ]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${actual}" "to not match format" "${format}"
return
fi
bashunit::state::add_assertions_passed
}