Skip to content

Commit

Permalink
wip: polish the globs_to_regex parametrization
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Oct 29, 2022
1 parent 87d2009 commit 62c656b
Showing 1 changed file with 96 additions and 81 deletions.
177 changes: 96 additions & 81 deletions tests/test_files.py
Expand Up @@ -106,7 +106,18 @@ def test_flat_rootname(original, flat):
assert flat_rootname(original) == flat


def gen_params(patterns, case_insensitive=False, partial=False, matches=(), nomatches=()):
def globs_to_regex_params(
patterns, case_insensitive=False, partial=False, matches=(), nomatches=(),
):
"""Generate parameters for `test_globs_to_regex`.
`patterns`, `case_insensitive`, and `partial` are arguments for
`globs_to_regex`. `matches` is a list of strings that should match, and
`nomatches` is a list of strings that should not match.
Everything is yielded so that `test_globs_to_regex` can call
`globs_to_regex` once and check one result.
"""
pat_id = "|".join(patterns)
for text in matches:
yield pytest.param(
Expand All @@ -121,86 +132,90 @@ def gen_params(patterns, case_insensitive=False, partial=False, matches=(), noma

@pytest.mark.parametrize(
"patterns, case_insensitive, partial, text, result",
list(itertools.chain.from_iterable([
gen_params(
["abc", "xyz"],
matches=["abc", "xyz", "sub/mod/abc"],
nomatches=["ABC", "xYz", "abcx", "xabc", "axyz", "xyza", "sub/mod/abcd", "sub/abc/more"],
),
gen_params(
["abc", "xyz"], case_insensitive=True,
matches=["abc", "xyz", "Abc", "XYZ", "AbC"],
nomatches=["abcx", "xabc", "axyz", "xyza"],
),
gen_params(
["a*c", "x*z"],
matches=["abc", "xyz", "xYz", "azc", "xaz", "axyzc"],
nomatches=["ABC", "abcx", "xabc", "axyz", "xyza", "a/c"],
),
gen_params(
["a?c", "x?z"],
matches=["abc", "xyz", "xYz", "azc", "xaz"],
nomatches=["ABC", "abcx", "xabc", "axyz", "xyza", "a/c"],
),
gen_params(
["a??d"],
matches=["abcd", "azcd", "a12d"],
nomatches=["ABCD", "abcx", "axyz", "abcde"],
),
gen_params(
["abc/hi.py"], case_insensitive=True,
matches=["abc/hi.py", "ABC/hi.py", r"ABC\hi.py"],
nomatches=["abc_hi.py", "abc/hi.pyc"],
),
gen_params(
[r"abc\hi.py"], case_insensitive=True,
matches=[r"abc\hi.py", r"ABC\hi.py", "abc/hi.py", "ABC/hi.py"],
nomatches=["abc_hi.py", "abc/hi.pyc"],
),
gen_params(
["abc/*/hi.py"], case_insensitive=True,
matches=["abc/foo/hi.py", r"ABC\foo/hi.py"],
nomatches=["abc/hi.py", "abc/hi.pyc", "ABC/foo/bar/hi.py", r"ABC\foo/bar/hi.py"],
),
gen_params(
["abc/**/hi.py"], case_insensitive=True,
matches=[
"abc/foo/hi.py", r"ABC\foo/hi.py", "abc/hi.py", "ABC/foo/bar/hi.py",
r"ABC\foo/bar/hi.py",
],
nomatches=["abc/hi.pyc"],
),
gen_params(
["abc/[a-f]*/hi.py"], case_insensitive=True,
matches=["abc/foo/hi.py", r"ABC\boo/hi.py"],
nomatches=[
"abc/zoo/hi.py", "abc/hi.py", "abc/hi.pyc", "abc/foo/bar/hi.py", r"abc\foo/bar/hi.py",
],
),
gen_params(
["abc/[a-f]/hi.py"], case_insensitive=True,
matches=["abc/f/hi.py", r"ABC\b/hi.py"],
nomatches=[
"abc/foo/hi.py", "abc/zoo/hi.py", "abc/hi.py", "abc/hi.pyc", "abc/foo/bar/hi.py",
r"abc\foo/bar/hi.py",
],
),
gen_params(
["abc/"], case_insensitive=True, partial=True,
matches=["abc/foo/hi.py", "ABC/foo/bar/hi.py", r"ABC\foo/bar/hi.py"],
nomatches=["abcd/foo.py", "xabc/hi.py"],
),
gen_params(
["*/foo"], case_insensitive=False, partial=True,
matches=["abc/foo/hi.py", "foo/hi.py"],
nomatches=["abc/xfoo/hi.py"],
),
gen_params(
["**/foo"],
matches=["foo", "hello/foo", "hi/there/foo"],
nomatches=["foob", "hello/foob", "hello/Foo"],
),
])))
list(itertools.chain.from_iterable([
globs_to_regex_params(
["abc", "xyz"],
matches=["abc", "xyz", "sub/mod/abc"],
nomatches=[
"ABC", "xYz", "abcx", "xabc", "axyz", "xyza", "sub/mod/abcd", "sub/abc/more",
],
),
globs_to_regex_params(
["abc", "xyz"], case_insensitive=True,
matches=["abc", "xyz", "Abc", "XYZ", "AbC"],
nomatches=["abcx", "xabc", "axyz", "xyza"],
),
globs_to_regex_params(
["a*c", "x*z"],
matches=["abc", "xyz", "xYz", "azc", "xaz", "axyzc"],
nomatches=["ABC", "abcx", "xabc", "axyz", "xyza", "a/c"],
),
globs_to_regex_params(
["a?c", "x?z"],
matches=["abc", "xyz", "xYz", "azc", "xaz"],
nomatches=["ABC", "abcx", "xabc", "axyz", "xyza", "a/c"],
),
globs_to_regex_params(
["a??d"],
matches=["abcd", "azcd", "a12d"],
nomatches=["ABCD", "abcx", "axyz", "abcde"],
),
globs_to_regex_params(
["abc/hi.py"], case_insensitive=True,
matches=["abc/hi.py", "ABC/hi.py", r"ABC\hi.py"],
nomatches=["abc_hi.py", "abc/hi.pyc"],
),
globs_to_regex_params(
[r"abc\hi.py"], case_insensitive=True,
matches=[r"abc\hi.py", r"ABC\hi.py", "abc/hi.py", "ABC/hi.py"],
nomatches=["abc_hi.py", "abc/hi.pyc"],
),
globs_to_regex_params(
["abc/*/hi.py"], case_insensitive=True,
matches=["abc/foo/hi.py", r"ABC\foo/hi.py"],
nomatches=["abc/hi.py", "abc/hi.pyc", "ABC/foo/bar/hi.py", r"ABC\foo/bar/hi.py"],
),
globs_to_regex_params(
["abc/**/hi.py"], case_insensitive=True,
matches=[
"abc/foo/hi.py", r"ABC\foo/hi.py", "abc/hi.py", "ABC/foo/bar/hi.py",
r"ABC\foo/bar/hi.py",
],
nomatches=["abc/hi.pyc"],
),
globs_to_regex_params(
["abc/[a-f]*/hi.py"], case_insensitive=True,
matches=["abc/foo/hi.py", r"ABC\boo/hi.py"],
nomatches=[
"abc/zoo/hi.py", "abc/hi.py", "abc/hi.pyc", "abc/foo/bar/hi.py",
r"abc\foo/bar/hi.py",
],
),
globs_to_regex_params(
["abc/[a-f]/hi.py"], case_insensitive=True,
matches=["abc/f/hi.py", r"ABC\b/hi.py"],
nomatches=[
"abc/foo/hi.py", "abc/zoo/hi.py", "abc/hi.py", "abc/hi.pyc", "abc/foo/bar/hi.py",
r"abc\foo/bar/hi.py",
],
),
globs_to_regex_params(
["abc/"], case_insensitive=True, partial=True,
matches=["abc/foo/hi.py", "ABC/foo/bar/hi.py", r"ABC\foo/bar/hi.py"],
nomatches=["abcd/foo.py", "xabc/hi.py"],
),
globs_to_regex_params(
["*/foo"], case_insensitive=False, partial=True,
matches=["abc/foo/hi.py", "foo/hi.py"],
nomatches=["abc/xfoo/hi.py"],
),
globs_to_regex_params(
["**/foo"],
matches=["foo", "hello/foo", "hi/there/foo"],
nomatches=["foob", "hello/foob", "hello/Foo"],
),
]))
)
def test_globs_to_regex(patterns, case_insensitive, partial, text, result):
regex = globs_to_regex(patterns, case_insensitive=case_insensitive, partial=partial)
assert bool(regex.match(text)) == result
Expand Down

0 comments on commit 62c656b

Please sign in to comment.