Split up re_matches tests

This commit is contained in:
Hynek Schlawack 2019-09-09 11:56:44 +02:00
parent 78cb7d4903
commit d153daa4ef
2 changed files with 27 additions and 14 deletions

View File

@ -95,16 +95,17 @@ class _MatchesReValidator(object):
def matches_re(regex, flags=0, func=None):
"""
r"""
A validator that raises :exc:`ValueError` if the initializer is called
with a string that doesn't match *regex*.
:param str regex: a regex string to match against
:param int flags: flags that will be passed to the underlying re function
(default 0)
:param callable func: which underlying re function to call (options are
:func:`re.fullmatch`, :func:`re.search`, :func:`re.match`, default
:func:`re.fullmatch`)
:param callable func: which underlying :mod:`re` function to call (options
are :func:`re.fullmatch`, :func:`re.search`, :func:`re.match`, default
:func:`re.fullmatch`). They won't be used directly but on a
pre-:func:`re.compile`\ ed pattern.
.. versionadded:: 19.2.0
"""
@ -113,7 +114,11 @@ def matches_re(regex, flags=0, func=None):
if func not in valid_funcs:
raise ValueError(
"'func' must be one of {}".format(
", ".join([repr(e) for e in set(valid_funcs)])
", ".join(
sorted(
e and e.__name__ or "None" for e in set(valid_funcs)
)
)
)
)

View File

@ -131,18 +131,26 @@ class TestMatchesRe(object):
SearchTester("bab") # re.search will match
def test_bad_args_and_repr(self):
def test_catches_invalid_func(self):
"""
miscellaneous behaviors: repr and
Invalid match functions are caught.
"""
with pytest.raises(TypeError):
matches_re(0)
with pytest.raises(TypeError):
matches_re("a", "a")
with pytest.raises(ValueError):
with pytest.raises(ValueError) as ei:
matches_re("a", 0, lambda: None)
for m in (None, getattr(re, "fullmatch", None), re.match, re.search):
matches_re("a", 0, m)
assert (
"'func' must be one of None, fullmatch, match, search"
== ei.value.args[0]
)
@pytest.mark.parametrize(
"func", [None, getattr(re, "fullmatch", None), re.match, re.search]
)
def test_accepts_all_valid_func(self, func):
"""
Every valid match function is accepted.
"""
matches_re("a", func=func)
def test_repr(self):
"""