Skip to content

Commit 2ebb920

Browse files
authored
Merge pull request #84 from poWer4aiX/issue-80
enh: added assert_matches and assert_not_matches, fixes Issue 80
2 parents 5576160 + 975829a commit 2ebb920

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

README.adoc

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,14 @@ Running tests in tests/test_core.sh
9393
Running test_assert_fails ... SUCCESS
9494
Running test_assert_fails_fails ... SUCCESS
9595
Running test_assert_fails_succeeds ... SUCCESS
96+
Running test_assert_matches_fails_when_not_matching ... SUCCESS
97+
Running test_assert_matches_succeed_when_matching ... SUCCESS
9698
Running test_assert_no_diff_fails_when_diff ... SUCCESS
9799
Running test_assert_no_diff_succeeds_when_no_diff ... SUCCESS
98100
Running test_assert_not_equals_fails_when_equal ... SUCCESS
99101
Running test_assert_not_equals_succeeds_when_not_equal ... SUCCESS
102+
Running test_assert_not_matches_fails_when_matching ... SUCCESS
103+
Running test_assert_not_matches_succeed_when_not_matching ... SUCCESS
100104
Running test_assert_shows_stderr_on_failure ... SUCCESS
101105
Running test_assert_shows_stdout_on_failure ... SUCCESS
102106
Running test_assert_status_code_fails ... SUCCESS
@@ -129,10 +133,14 @@ Running tests in tests/test_core.sh
129133
Running test_assert_fails ... SUCCESS
130134
Running test_assert_fails_fails ... SUCCESS
131135
Running test_assert_fails_succeeds ... SUCCESS
136+
Running test_assert_matches_fails_when_not_matching ... SUCCESS
137+
Running test_assert_matches_succeed_when_matching ... SUCCESS
132138
Running test_assert_no_diff_fails_when_diff ... SUCCESS
133139
Running test_assert_no_diff_succeeds_when_no_diff ... SUCCESS
134140
Running test_assert_not_equals_fails_when_equal ... SUCCESS
135141
Running test_assert_not_equals_succeeds_when_not_equal ... SUCCESS
142+
Running test_assert_not_matches_fails_when_matching ... SUCCESS
143+
Running test_assert_not_matches_succeed_when_not_matching ... SUCCESS
136144
Running test_assert_shows_stderr_on_failure ... SUCCESS
137145
Running test_assert_shows_stdout_on_failure ... SUCCESS
138146
Running test_assert_status_code_fails ... SUCCESS
@@ -156,10 +164,14 @@ ok - test_assert_equals_succeed_when_equal
156164
ok - test_assert_fails
157165
ok - test_assert_fails_fails
158166
ok - test_assert_fails_succeeds
167+
ok - test_assert_matches_fails_when_not_matching
168+
ok - test_assert_matches_succeed_when_matching
159169
ok - test_assert_no_diff_fails_when_diff
160170
ok - test_assert_no_diff_succeeds_when_no_diff
161171
ok - test_assert_not_equals_fails_when_equal
162172
ok - test_assert_not_equals_succeeds_when_not_equal
173+
ok - test_assert_not_matches_fails_when_matching
174+
ok - test_assert_not_matches_succeed_when_not_matching
163175
ok - test_assert_shows_stderr_on_failure
164176
ok - test_assert_shows_stdout_on_failure
165177
ok - test_assert_status_code_fails
@@ -411,6 +423,56 @@ doc:2:test_obvious_equality_with_assert_not_equals()
411423
Running test_obvious_inequality_with_assert_not_equals ... SUCCESS
412424
```
413425

426+
##############################
427+
=== *assert_matches*
428+
429+
assert_matches <expected-regex> <actual> [message]
430+
431+
Asserts that the string _actual_ matches the regex pattern _expected-regex_.
432+
433+
```test
434+
test_obvious_notmatching_with_assert_matches(){
435+
assert_matches "a str.*" "another string" "'another string' should not match 'a str.*'"
436+
}
437+
test_obvious_matching_with_assert_matches(){
438+
assert_matches "a[nN].t{0,1}.*r str.*" "another string"
439+
}
440+
441+
```
442+
443+
```output
444+
Running test_obvious_matching_with_assert_matches ... SUCCESS
445+
Running test_obvious_notmatching_with_assert_matches ... FAILURE
446+
'another string' should not match 'a str.*'
447+
expected regex [a str.*] to match [another string]
448+
doc:2:test_obvious_notmatching_with_assert_matches()
449+
```
450+
451+
=== *assert_not_matches*
452+
453+
assert_not_matches <unexpected-regex> <actual> [message]
454+
455+
Asserts that the string _actual_ does not match the regex pattern _unexpected-regex_.
456+
457+
```test
458+
test_obvious_matching_with_assert_not_matches(){
459+
assert_not_matches "a str.*" "a string" "'a string' should not match 'a str.*'"
460+
}
461+
test_obvious_notmatching_with_assert_not_matches(){
462+
assert_not_matches "a str.*" "another string"
463+
}
464+
465+
```
466+
467+
```output
468+
Running test_obvious_matching_with_assert_not_matches ... FAILURE
469+
'a string' should not match 'a str.*'
470+
expected regex [a str.*] should not match but matched [a string]
471+
doc:2:test_obvious_matching_with_assert_not_matches()
472+
Running test_obvious_notmatching_with_assert_not_matches ... SUCCESS
473+
```
474+
475+
##############################
414476
== *fake* function
415477

416478
fake <command> [replacement code]

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 unexpected=$1
137+
local actual=$2
138+
local message=${3:-}
139+
[[ -z $message ]] || message="$message\n"
140+
141+
if [[ "${actual}" =~ ${unexpected} ]]; then
142+
fail "$message expected regex [$unexpected] 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)