Skip to content

Commit baffb32

Browse files
committed
README.md: Document refute_regex
Add a copy to the implementation, too.
1 parent 81d7724 commit baffb32

2 files changed

Lines changed: 87 additions & 0 deletions

File tree

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,50 @@ Note that the `BASH_REMATCH` array is available immediately after the
728728
assertion succeeds but is fragile, i.e. prone to being overwritten as a side
729729
effect of other actions.
730730

731+
### `refute_regex`
732+
733+
This function is similar to `refute_equal` but uses pattern matching instead of
734+
equality, by wrapping `! [[ value =~ pattern ]]`.
735+
736+
Fail if the value (first parameter) matches the pattern (second parameter).
737+
738+
```bash
739+
@test 'refute_regex()' {
740+
refute_regex 'WhatsApp' 'Threema'
741+
}
742+
```
743+
744+
On failure, the value, the pattern and the match are displayed.
745+
746+
```
747+
@test 'refute_regex()' {
748+
refute_regex 'WhatsApp' 'What.'
749+
}
750+
751+
-- value matches regular expression --
752+
value : WhatsApp
753+
pattern : What.
754+
match : Whats
755+
case : sensitive
756+
--
757+
```
758+
759+
If the value or pattern is longer than one line then it is displayed in
760+
*multi-line* format.
761+
762+
An error is displayed if the specified extended regular expression is invalid.
763+
764+
For description of the matching behavior, refer to the documentation of the
765+
`=~` operator in the
766+
[Bash manual]: https://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html.
767+
768+
Note that the `BASH_REMATCH` array is available immediately after the assertion
769+
fails but is fragile, i.e. prone to being overwritten as a side effect of other
770+
actions like calling `run`. Thus, it's good practice to avoid using
771+
`BASH_REMATCH` in conjunction with `refute_regex()`. The valuable information
772+
the array contains is the matching part of the value which is printed in the
773+
failing test log, as mentioned above.
774+
731775
<!-- REFERENCES -->
732776

733777
[bats]: https://github.com/bats-core/bats-core

src/refute_regex.bash

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,46 @@
1+
# `refute_regex`
2+
#
3+
# This function is similar to `refute_equal` but uses pattern matching instead
4+
# of equality, by wrapping `! [[ value =~ pattern ]]`.
5+
#
6+
# Fail if the value (first parameter) matches the pattern (second parameter).
7+
#
8+
# ```bash
9+
# @test 'refute_regex()' {
10+
# refute_regex 'WhatsApp' 'Threema'
11+
# }
12+
# ```
13+
#
14+
# On failure, the value, the pattern and the match are displayed.
15+
#
16+
# ```
17+
# @test 'refute_regex()' {
18+
# refute_regex 'WhatsApp' 'What.'
19+
# }
20+
#
21+
# -- value matches regular expression --
22+
# value : WhatsApp
23+
# pattern : What.
24+
# match : Whats
25+
# case : sensitive
26+
# --
27+
# ```
28+
#
29+
# If the value or pattern is longer than one line then it is displayed in
30+
# *multi-line* format.
31+
#
32+
# An error is displayed if the specified extended regular expression is invalid.
33+
#
34+
# For description of the matching behavior, refer to the documentation of the
35+
# `=~` operator in the
36+
# [Bash manual]: https://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html.
37+
#
38+
# Note that the `BASH_REMATCH` array is available immediately after the
39+
# assertion fails but is fragile, i.e. prone to being overwritten as a side
40+
# effect of other actions like calling `run`. Thus, it's good practice to avoid
41+
# using `BASH_REMATCH` in conjunction with `refute_regex()`. The valuable
42+
# information the array contains is the matching part of the value which is
43+
# printed in the failing test log, as mentioned above.
144
refute_regex() {
245
local -r value="${1}"
346
local -r pattern="${2}"

0 commit comments

Comments
 (0)