Skip to content

Commit f746f0c

Browse files
committed
enh: added assert_matches (issue #80 from stuertz) and assert_not_matches
1 parent 5576160 commit f746f0c

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

bash_unit

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,28 @@ assert_not_equals() {
121121
fail "$message expected different value than [$unexpected] but was the same"
122122
}
123123

124+
assert_matches() {
125+
local expected=$1
126+
local actual=$2
127+
local message=${3:-}
128+
[[ -z $message ]] || message="$message\n"
129+
130+
if [[ ! "${actual}" =~ ${expected} ]]; then
131+
fail "$message expected regex [$expected] to match [$actual]"
132+
fi
133+
}
134+
135+
assert_not_matches() {
136+
local expected=$1
137+
local actual=$2
138+
local message=${3:-}
139+
[[ -z $message ]] || message="$message\n"
140+
141+
if [[ "${actual}" =~ ${expected} ]]; then
142+
fail "$message expected regex [$expected] should not match but matched [$actual]"
143+
fi
144+
}
145+
124146
assert_no_diff() {
125147
local expected=$1
126148
local actual=$2

tests/test_core.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,30 @@ test_assert_equals_succeed_when_equal() {
4545

4646
#assert_equals can now be used in the following tests
4747

48+
test_assert_matches_fails_when_not_matching() {
49+
assert_fails \
50+
"with_bash_unit_muted assert_matches to.*to tutu" \
51+
"assert_matches should fail"
52+
}
53+
54+
test_assert_matches_succeed_when_matching() {
55+
assert \
56+
"assert_matches 't.to{0,1} t[Aa].*ta$' 'toto tata'"\
57+
'assert_matches should succeed'
58+
}
59+
60+
test_assert_not_matches_fails_when_matching() {
61+
assert_fails \
62+
"with_bash_unit_muted assert_not_matches 't.to{0,1} t[Aa].*ta$' 'toto tata'" \
63+
"assert_not_matches should fail"
64+
}
65+
66+
test_assert_not_matches_succeed_when_not_matching() {
67+
assert \
68+
"assert_not_matches 'toto' 'tata'"\
69+
'assert_not_matches should succeed'
70+
}
71+
4872
test_assert_not_equals_fails_when_equal() {
4973
assert_fails \
5074
"with_bash_unit_muted assert_not_equals toto toto" \

0 commit comments

Comments
 (0)