diff options
author | Alex Thiessen <alex.thiessen.de+github@gmail.com> | 2022-05-25 22:19:45 +0300 |
---|---|---|
committer | Alex Thiessen <alex.thiessen.de+github@gmail.com> | 2022-05-29 00:24:48 +0300 |
commit | ee0e7a95fab96b138cc9310e5a2deac8e6d739a3 (patch) | |
tree | 0657617e80c783d0a98bb791eb81de73bdb47dbf | |
parent | 12b195fe36f5259272ce309e43e00cbd069a7b92 (diff) |
README.md: Document `assert_regex`
Add a copy to the implementation, too.
-rw-r--r-- | README.md | 35 | ||||
-rw-r--r-- | src/assert_regex.bash | 34 |
2 files changed, 69 insertions, 0 deletions
@@ -693,6 +693,41 @@ 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. + +An error is displayed if the specified extended regular expression is invalid. + +For description of the matching behavior, refer to the documentation of the +`=~` operator in the +[Bash manual]: https://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html. +Note that the `BASH_REMATCH` array is available immediately after the +assertion succeeds but is fragile, i.e. prone to being overwritten as a side +effect of other actions. + <!-- REFERENCES --> [bats]: https://github.com/bats-core/bats-core diff --git a/src/assert_regex.bash b/src/assert_regex.bash index b371bf3..1dd2081 100644 --- a/src/assert_regex.bash +++ b/src/assert_regex.bash @@ -1,3 +1,37 @@ +# `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. +# +# An error is displayed if the specified extended regular expression is invalid. +# +# For description of the matching behavior, refer to the documentation of the +# `=~` operator in the +# [Bash manual]: https://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html. +# Note that the `BASH_REMATCH` array is available immediately after the +# assertion succeeds but is fragile, i.e. prone to being overwritten as a side +# effect of other actions. assert_regex() { local -r value="${1}" local -r pattern="${2}" |