Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 92020e4

Browse files
committed
refactor(test): convert to parametrized
1 parent 6387d0a commit 92020e4

File tree

1 file changed

+52
-30
lines changed

1 file changed

+52
-30
lines changed

tests/test_files.py

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -375,17 +375,28 @@ def assertMatches(self, matcher: TMatcher, filepath: str, matches: bool) -> None
375375
msg = f"File {filepath} should have matched as {matches}"
376376
assert matches == matcher.match(canonical), msg
377377

378-
def test_tree_matcher(self) -> None:
379-
case_folding = env.WINDOWS
380-
matches_to_try = [
381-
(self.make_file("sub/file1.py"), True),
382-
(self.make_file("sub/file2.c"), True),
383-
(self.make_file("sub2/file3.h"), False),
384-
(self.make_file("sub3/file4.py"), True),
385-
(self.make_file("sub3/file5.c"), False),
386-
(self.make_file("sub4/File5.py"), case_folding),
387-
(self.make_file("sub5/file6.py"), case_folding),
388-
]
378+
@pytest.mark.parametrize(
379+
"filepath, expected_match",
380+
[
381+
("sub/file1.py", True),
382+
("sub/file2.c", True),
383+
("sub2/file3.h", False),
384+
("sub3/file4.py", True),
385+
("sub3/file5.c", False),
386+
("sub4/File5.py", env.WINDOWS),
387+
("sub5/file6.py", env.WINDOWS),
388+
],
389+
)
390+
def test_tree_matcher(self, filepath: str, expected_match: bool) -> None:
391+
# Create all files that TreeMatcher needs
392+
self.make_file("sub/file1.py")
393+
self.make_file("sub/file2.c")
394+
self.make_file("sub2/file3.h")
395+
self.make_file("sub3/file4.py")
396+
self.make_file("sub3/file5.c")
397+
self.make_file("sub4/File5.py")
398+
self.make_file("sub5/file6.py")
399+
389400
trees = [
390401
files.canonical_filename("sub"),
391402
files.canonical_filename("sub3/file4.py"),
@@ -394,11 +405,11 @@ def test_tree_matcher(self) -> None:
394405
]
395406
tm = TreeMatcher(trees)
396407
assert tm.info() == sorted(trees)
397-
for filepath, matches in matches_to_try:
398-
self.assertMatches(tm, filepath, matches)
408+
self.assertMatches(tm, filepath, expected_match)
399409

400-
def test_module_matcher(self) -> None:
401-
matches_to_try = [
410+
@pytest.mark.parametrize(
411+
"modulename, expected_match",
412+
[
402413
("test", True),
403414
("trash", False),
404415
("testing", False),
@@ -413,26 +424,37 @@ def test_module_matcher(self) -> None:
413424
("__main__", False),
414425
("mymain", True),
415426
("yourmain", False),
416-
]
427+
],
428+
)
429+
def test_module_matcher(self, modulename: str, expected_match: bool) -> None:
417430
modules = ["test", "py.test", "mymain"]
418431
mm = ModuleMatcher(modules)
419432
assert mm.info() == modules
420-
for modulename, matches in matches_to_try:
421-
assert mm.match(modulename) == matches, modulename
422-
423-
def test_glob_matcher(self) -> None:
424-
matches_to_try = [
425-
(self.make_file("sub/file1.py"), True),
426-
(self.make_file("sub/file2.c"), False),
427-
(self.make_file("sub2/file3.h"), True),
428-
(self.make_file("sub2/sub/file3.h"), True),
429-
(self.make_file("sub3/file4.py"), True),
430-
(self.make_file("sub3/file5.c"), False),
431-
]
433+
assert mm.match(modulename) == expected_match, modulename
434+
435+
@pytest.mark.parametrize(
436+
"filepath, expected_match",
437+
[
438+
("sub/file1.py", True),
439+
("sub/file2.c", False),
440+
("sub2/file3.h", True),
441+
("sub2/sub/file3.h", True),
442+
("sub3/file4.py", True),
443+
("sub3/file5.c", False),
444+
],
445+
)
446+
def test_glob_matcher(self, filepath: str, expected_match: bool) -> None:
447+
# Create all files that GlobMatcher needs
448+
self.make_file("sub/file1.py")
449+
self.make_file("sub/file2.c")
450+
self.make_file("sub2/file3.h")
451+
self.make_file("sub2/sub/file3.h")
452+
self.make_file("sub3/file4.py")
453+
self.make_file("sub3/file5.c")
454+
432455
fnm = GlobMatcher(["*.py", "*/sub2/*"])
433456
assert fnm.info() == ["*.py", "*/sub2/*"]
434-
for filepath, matches in matches_to_try:
435-
self.assertMatches(fnm, filepath, matches)
457+
self.assertMatches(fnm, filepath, expected_match)
436458

437459
def test_glob_matcher_overload(self) -> None:
438460
fnm = GlobMatcher(["*x%03d*.txt" % i for i in range(500)])

0 commit comments

Comments
 (0)