Skip to main content

show where your regex match assertion failed!

Project description

Build Status Azure DevOps coverage

re-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'

Matches.assert_matches(s: str)

if you're using some other test framework, this method is useful for producing a readable traceback

>>> Matches('foo').assert_matches('food')
>>> Matches('foo').assert_matches('fork')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  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
    ^

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

re_assert-1.0.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

re_assert-1.0.0-py2.py3-none-any.whl (4.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file re_assert-1.0.0.tar.gz.

File metadata

  • Download URL: re_assert-1.0.0.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.8

File hashes

Hashes for re_assert-1.0.0.tar.gz
Algorithm Hash digest
SHA256 bc506382b1b9e77df7a6adfdd2a7d58871f7231113de0417e498dda61117c42d
MD5 9561a022964be9c25ac3e275de46c245
BLAKE2b-256 d01698d954270f34ce61604528d1a114ce90fc35712b20de76d8cc9192671e78

See more details on using hashes here.

Provenance

File details

Details for the file re_assert-1.0.0-py2.py3-none-any.whl.

File metadata

  • Download URL: re_assert-1.0.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.8

File hashes

Hashes for re_assert-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5be9d23ab05518699bf09ebdd477628c0340264190a51c75e6bf88067ea9f266
MD5 71c40fd0943bbddd373fd975c388a15a
BLAKE2b-256 1d06f1fb87623b627ec66bc23457d1c47f45ec7a213e2a03fb16bad1aedc1383

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page