Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/asottile/re-assert
show where your regex match assertion failed!
https://github.com/asottile/re-assert
Last synced: about 13 hours ago
JSON representation
show where your regex match assertion failed!
- Host: GitHub
- URL: https://github.com/asottile/re-assert
- Owner: asottile
- License: mit
- Created: 2019-09-21T19:57:18.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2024-10-29T01:22:00.000Z (15 days ago)
- Last Synced: 2024-10-30T04:49:37.205Z (14 days ago)
- Language: Python
- Homepage:
- Size: 197 KB
- Stars: 81
- Watchers: 4
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![build status](https://github.com/asottile/re-assert/actions/workflows/main.yml/badge.svg)](https://github.com/asottile/re-assert/actions/workflows/main.yml)
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/asottile/re-assert/main.svg)](https://results.pre-commit.ci/latest/github/asottile/re-assert/main)re-assert
=========show where your regex match assertion failed!
## installation
```bash
pip install re-assert
```## usage
`re-assert` provides a helper class to make assertions of regexes simpler.
### `re_assert.Matches(pattern: str, *args, **kwargs)`
construct a `Matches` object.
_note_: under the hood, `re-assert` uses the [`regex`] library for matching,
any `*args` / `**kwargs` that `regex.compile` supports will work. in general,
the `regex` library is 100% compatible with the `re` library (and will even
accept its flags, etc.)[`regex`]: https://pypi.org/project/regex/
### `re_assert.Matches.from_pattern(pattern: Pattern[str]) -> Matches`
construct a `Matches` object from an already-compiled regex.
this is useful (for instance) if you're testing an existing compiled regex.
```pycon
>>> import re
>>> reg = re.compile('foo')
>>> Matches.from_pattern(reg) == 'fork'
False
>>> Matches.from_pattern(reg) == 'food'
True
```### `Matches.__eq__(other)` (`==`)
the equality operator is overridden for use with assertion frameworks such
as pytest```pycon
>>> pat = Matches('foo')
>>> pat == 'bar'
False
>>> pat == 'food'
True
```### `Matches.__repr__()` (`repr(...)`)
a side-effect of an equality failure changes the `repr(...)` of a `Matches`
object. this allows for useful pytest assertion messages:```pytest
> assert Matches('foo') == 'fork'
E AssertionError: assert Matches('foo'...ork\n # ^ == 'fork'
E -Matches('foo')\n
E - # regex failed to match at:\n
E - #\n
E - #> fork\n
E - # ^
E +'fork'
```### `Matches.assert_matches(s: str)`
if you're using some other test framework, this method is useful for producing
a readable traceback```pycon
>>> Matches('foo').assert_matches('food')
>>> Matches('foo').assert_matches('fork')
Traceback (most recent call last):
File "", line 1, in
File "/home/asottile/workspace/re-assert/re_assert.py", line 63, in assert_matches
assert self == s, self._fail
AssertionError: regex failed to match at:> fork
^
```