Skip to content

Commit f45a92f

Browse files
committed
Add custom bats assert_between
1 parent ccc55d4 commit f45a92f

2 files changed

Lines changed: 25 additions & 9 deletions

File tree

exercises/practice/baffling-birthdays/baffling_birthdays.bats

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,21 +158,21 @@ load bats-extra
158158
# The expected probability is 11.694818
159159
run bash baffling_birthdays.sh estimated_probability_of_share_birthday 10
160160
assert_success
161-
(( output >= 10 && output <= 13 ))
161+
assert_between 10 13
162162
}
163163

164164
@test "estimated probability of at least one shared birthday -> among twenty-three people" {
165165
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
166166
# The expected probability is 50.729723
167167
run bash baffling_birthdays.sh estimated_probability_of_share_birthday 23
168168
assert_success
169-
(( output >= 48 && output <= 53 ))
169+
assert_between 48 53
170170
}
171171

172172
@test "estimated probability of at least one shared birthday -> among seventy people" {
173173
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
174174
# The expected probability is 99.915958
175175
run bash baffling_birthdays.sh estimated_probability_of_share_birthday 70
176176
assert_success
177-
(( output >= 98 && output <= 100 ))
177+
assert_between 98 100
178178
}

exercises/practice/baffling-birthdays/bats-extra.bash

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,9 @@ assert_line() {
251251
if (( is_mode_regexp )); then
252252
if ! [[ ${lines[$idx]} =~ $expected ]]; then
253253
batslib_print_kv_single 6 \
254-
'index' "$idx" \
254+
'index' "$idx" \
255255
'regexp' "$expected" \
256-
'line' "${lines[$idx]}" \
256+
'line' "${lines[$idx]}" \
257257
| batslib_decorate 'regular expression does not match line' \
258258
| fail
259259
fi
@@ -373,7 +373,7 @@ assert_output() {
373373
elif ! [[ $output =~ $expected ]]; then
374374
batslib_print_kv_single_or_multi 6 \
375375
'regexp' "$expected" \
376-
'output' "$output" \
376+
'output' "$output" \
377377
| batslib_decorate 'regular expression does not match output' \
378378
| fail
379379
fi
@@ -468,9 +468,9 @@ refute_line() {
468468
if (( is_mode_regexp )); then
469469
if [[ ${lines[$idx]} =~ $unexpected ]]; then
470470
batslib_print_kv_single 6 \
471-
'index' "$idx" \
471+
'index' "$idx" \
472472
'regexp' "$unexpected" \
473-
'line' "${lines[$idx]}" \
473+
'line' "${lines[$idx]}" \
474474
| batslib_decorate 'regular expression should not match line' \
475475
| fail
476476
fi
@@ -613,7 +613,7 @@ refute_output() {
613613
elif (( is_mode_regexp )); then
614614
if [[ $output =~ $unexpected ]]; then
615615
batslib_print_kv_single_or_multi 6 \
616-
'regexp' "$unexpected" \
616+
'regexp' "$unexpected" \
617617
'output' "$output" \
618618
| batslib_decorate 'regular expression should not match output' \
619619
| fail
@@ -635,3 +635,19 @@ refute_output() {
635635
fi
636636
fi
637637
}
638+
639+
assert_between() {
640+
local -r start="${1}"
641+
local -r end="${2}"
642+
643+
if (( start > end )); then
644+
return 1
645+
elif ! (( output >= "$1" && output <= "$2" )); then
646+
batslib_print_kv_single_or_multi 6 \
647+
'start' "${start}" \
648+
'end' "${end}" \
649+
'actual' "${output}" \
650+
| batslib_decorate 'output falls outside the inclusive range defined by start and end' \
651+
| fail
652+
fi
653+
}

0 commit comments

Comments
 (0)