Open
Description
Hi Robert.
Here's the assert_contains() function I wrote for regex matching.
Usage is: assert_contains "str" "regex" [0|1], where 0=expected match, 1=nomatch
assert_contains() {
# assert_contains [expected](%28 tests_ran++ %29) || :
[[ -n "$DISCOVERONLY" ]] && return || true
str=${1:-}
regex=${2:-}
expected=${3:-0}
# status = 0 match, 1 = no match
status=1
# try the match. there's 3 possible outcomes:
#
#1) it may succeed and be captured if user specified parentheses: status=0 and match="something"
#2) it may succeed and not be captured if parentheses not used: status=0 and match=""
#3) it may fail to match: status=1 and match=""
match=""
if [[ $str =~ $regex ]]; then
status=0
match=${BASH_REMATCH[1]} # maybe some kind of capture is available
fi
if [[ "$status" -eq "$expected" ]]; then
[[ -n "$DEBUG" ]] && echo -n . || true
return
fi
if [ $status -eq 0 ] && [ -z "$match" ]; then
if [[ $str =~ ($regex) ]]; then # the previous regex succeeded but no capture in $match, so try again with capturing parenthesess for debugging
match=${BASH_REMATCH[1]}
fi
fi
_assert_fail "match terminated with code $status and match '$match' instead of $expected" "$str" "$expected"
}
Metadata
Metadata
Assignees
Labels
No labels