显示您的正则表达式匹配断言失败的位置!
项目描述
re-assert
显示您的正则表达式匹配断言失败的位置!
安装
pip安装re-assert
使用
re-assert
提供了一个辅助类,使正则表达式的断言更加简单。
re_assert.Matches(pattern: str, *args, **kwargs)
构建一个Matches
对象。
注意:在底层,re-assert
使用regex
库进行匹配,任何*args
/ **kwargs
都支持regex.compile
,通常,regex
库与re
库100%兼容(甚至可以接受其标志等)
re_assert.Matches.from_pattern(pattern: Pattern[str]) -> Matches
从一个已编译的正则表达式构建一个Matches
对象。
这在测试现有的已编译正则表达式时很有用。
>>> import re
>>> reg = re.compile('foo')
>>> Matches.from_pattern(reg) == 'fork'
False
>>> Matches.from_pattern(reg) == 'food'
True
Matches.__eq__(other)
(==
)
相等操作符被覆盖,以便与pytest等断言框架一起使用
>>> pat = Matches('foo')
>>> pat == 'bar'
False
>>> pat == 'food'
True
Matches.__repr__()
(repr(...)
)
相等失败的一个副作用是改变repr(...)
的Matches
对象。这允许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)
如果您使用的是其他测试框架,此方法对于生成可读的回溯很有用
>>> 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
^
项目详情
关闭
re_assert-1.1.0.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 5172dfbd2047a15dff2347735dea7e495479cc7e58841199a4a4973256b20464 |
|
MD5 | c948ac9a95285ab2b8048408fb70c978 |
|
BLAKE2b-256 | 72895801de335fa7a9cd9f402f66680b14be076d2084dd8244e3a3136a743262 |
关闭
re_assert-1.1.0-py2.py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 6b2902d5beac2a0e715eb0312fcb226ab4da21d1ea6953441734734b5b68cd48 |
|
MD5 | 8b7babcb12d5654cebedb551f3a3885d |
|
BLAKE2b-256 | 95b0e70a69ece48c3b6862a5ae6ca87e1e76ff2941447885550073eb280235fb |