Skip to content

Commit e28f439

Browse files
authored
Merge pull request #451 from TypedDevs/fix/suffix-assertion-checks
Improve suffix assertion checks
2 parents 37a6aac + 51cd5c0 commit e28f439

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Fix prevents writing in src dir during tests
66
- Fix negative widths with rpad
77
- Fix internal assert_line_count and call_test_functions
8+
- Improve suffix assertion checks to use shell pattern matching
89
- Include calling function name in dev log output for easier debugging
910
- Add more internal dev log messages and prefix them with [INTERNAL]
1011
- Toggle internal log messages with `BASHUNIT_INTERNAL_LOG=true|false`

src/assert.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ function assert_string_starts_with() {
333333
local actual
334334
actual=$(printf '%s\n' "${actual_arr[@]}")
335335

336-
if ! [[ $actual =~ ^"$expected"* ]]; then
336+
if [[ $actual != "$expected"* ]]; then
337337
local label
338338
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
339339
state::add_assertions_failed
@@ -348,7 +348,7 @@ function assert_string_not_starts_with() {
348348
local expected="$1"
349349
local actual="$2"
350350

351-
if [[ $actual =~ ^"$expected"* ]]; then
351+
if [[ $actual == "$expected"* ]]; then
352352
local label
353353
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
354354
state::add_assertions_failed
@@ -365,7 +365,7 @@ function assert_string_ends_with() {
365365
local actual
366366
actual=$(printf '%s\n' "${actual_arr[@]}")
367367

368-
if ! [[ $actual =~ .*"$expected"$ ]]; then
368+
if [[ $actual != *"$expected" ]]; then
369369
local label
370370
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
371371
state::add_assertions_failed
@@ -382,7 +382,7 @@ function assert_string_not_ends_with() {
382382
local actual
383383
actual=$(printf '%s\n' "${actual_arr[@]}")
384384

385-
if [[ $actual =~ .*"$expected"$ ]]; then
385+
if [[ $actual == *"$expected" ]]; then
386386
local label
387387
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
388388
state::add_assertions_failed

tests/unit/assert_test.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,23 @@ function test_unsuccessful_assert_string_not_ends_with() {
354354
"$(assert_string_not_ends_with "bar" "foobar")"
355355
}
356356

357+
function test_assert_string_start_end_with_special_chars() {
358+
assert_empty "$(assert_string_starts_with "foo." "foo.bar")"
359+
assert_empty "$(assert_string_ends_with ".bar" "foo.bar")"
360+
}
361+
362+
function test_assert_string_start_end_with_special_chars_fail() {
363+
assert_same\
364+
"$(console_results::print_failed_test\
365+
"Assert string start end with special chars fail" "fooX" "to start with" "foo.")"\
366+
"$(assert_string_starts_with "foo." "fooX")"
367+
368+
assert_same\
369+
"$(console_results::print_failed_test\
370+
"Assert string start end with special chars fail" "fooX" "to end with" ".bar")"\
371+
"$(assert_string_ends_with ".bar" "fooX")"
372+
}
373+
357374
function test_successful_assert_less_than() {
358375
assert_empty "$(assert_less_than "3" "1")"
359376
}

0 commit comments

Comments
 (0)