Skip to content

Commit

Permalink
{src,test}/assert_regex: Make case-sensitivity transparent
Browse files Browse the repository at this point in the history
  • Loading branch information
rico-chet committed May 28, 2022
1 parent edb2300 commit d950f28
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/assert_regex.bash
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@ assert_regex() {
| batslib_decorate 'ERROR: assert_regex' \
| fail
elif ! [[ "${value}" =~ ${pattern} ]]; then
if shopt -p nocasematch &>/dev/null; then
local case_sensitive=insensitive
else
local case_sensitive=sensitive
fi
batslib_print_kv_single_or_multi 8 \
'value' "${value}" \
'pattern' "${pattern}" \
'case' "${case_sensitive}" \
| batslib_decorate 'value does not match regular expression' \
| fail
fi
Expand Down
18 changes: 18 additions & 0 deletions test/assert_regex.bats
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ load test_helper
-- value does not match regular expression --
value : bcd
pattern : ^[a-z]b[c-z]+
case : sensitive
--
ERR_MSG
}
Expand Down Expand Up @@ -47,6 +48,23 @@ value (2 lines):
123
pattern (1 lines):
^[a-z]b[c-z]+
case (1 lines):
sensitive
--
ERR_MSG

shopt -s nocasematch
run assert_regex $'bcd\n123' '^[a-z]b[c-z]+'
assert_test_fail <<'ERR_MSG'
-- value does not match regular expression --
value (2 lines):
bcd
123
pattern (1 lines):
^[a-z]b[c-z]+
case (1 lines):
insensitive
--
ERR_MSG
}
Expand Down

0 comments on commit d950f28

Please sign in to comment.