show where your regex match assertion failed!
Low maintenance activity. Last release over 6 years ago, commits in 7 of the last 26 weeks. Weaker on recent releases and release cadence.
pip install re-assertre-assert
show where your regex match assertion failed!
installation
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.)
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.
>>> 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
>>> 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:
> 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'