Skip to content

Commit

Permalink
README.md: Document assert_regex
Browse files Browse the repository at this point in the history
Add a copy to the implementation, too.
  • Loading branch information
rico-chet committed May 26, 2022
1 parent 12b195f commit 4f8544b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,32 @@ An error is displayed if the specified extended regular expression is invalid.
This option and partial matching (`--partial` or `-p`) are mutually exclusive.
An error is displayed when used simultaneously.

### `assert_regex`

This function is similar to `assert_equal` but uses pattern matching instead of
equality, by wrapping `[[ value =~ pattern ]]`.

Fail if the value (first parameter) does not match the pattern (second
parameter).

```bash
@test 'assert_regex()' {
assert_regex 'what' 'x$'
}
```

On failure, the value and the pattern are displayed.

```
-- values does not match regular expression --
value : what
pattern : x$
--
```

If the value is longer than one line then it is displayed in *multi-line*
format.

<!-- REFERENCES -->

[bats]: https://github.com/bats-core/bats-core
Expand Down
25 changes: 25 additions & 0 deletions src/assert_regex.bash
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
# `assert_regex`
#
# This function is similar to `assert_equal` but uses pattern matching instead
# of equality, by wrapping `[[ value =~ pattern ]]`.
#
# Fail if the value (first parameter) does not match the pattern (second
# parameter).
#
# ```bash
# @test 'assert_regex()' {
# assert_regex 'what' 'x$'
# }
# ```
#
# On failure, the value and the pattern are displayed.
#
# ```
# -- values does not match regular expression --
# value : what
# pattern : x$
# --
# ```
#
# If the value is longer than one line then it is displayed in *multi-line*
# format.
assert_regex() {
local -r value="${1}"
local -r pattern="${2}"
Expand Down

0 comments on commit 4f8544b

Please sign in to comment.