Skip to content

Commit

Permalink
Implement a more generalized parameterization function and use it for…
Browse files Browse the repository at this point in the history
… alpharep.
  • Loading branch information
jaraco committed Oct 8, 2022
1 parent 9f26f90 commit c3b5c4e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 22 deletions.
39 changes: 39 additions & 0 deletions _test_params.py
@@ -0,0 +1,39 @@
import types
import functools

from more_itertools import always_iterable


def parameterize(names, value_groups):
"""
Decorate a test method to run it as a set of subtests.
Modeled after pytest.parametrize.
"""

def decorator(func):
@functools.wraps(func)
def wrapped(self):
for values in value_groups:
resolved = map(Invoked.eval, always_iterable(values))
params = dict(zip(always_iterable(names), resolved))
with self.subTest(**params):
func(self, **params)

return wrapped

return decorator


class Invoked(types.SimpleNamespace):
"""
Wrap a function to be invoked for each usage.
"""

@classmethod
def wrap(cls, func):
return cls(func=func)

@classmethod
def eval(cls, cand):
return cand.func() if isinstance(cand, cls) else cand
2 changes: 2 additions & 0 deletions setup.cfg
Expand Up @@ -46,6 +46,8 @@ testing =
# local
jaraco.itertools
func-timeout
jaraco.functools
more_itertools

docs =
# upstream
Expand Down
35 changes: 13 additions & 22 deletions test_zipp.py
Expand Up @@ -6,13 +6,16 @@
import tempfile
import shutil
import string
import functools

import jaraco.itertools
import func_timeout
from jaraco.functools import compose

import zipp

from _test_params import parameterize, Invoked


consume = tuple


Expand Down Expand Up @@ -71,31 +74,20 @@ def temp_dir():
shutil.rmtree(tmpdir)


def pass_alpharep(meth):
"""
Given a method, wrap it in a for loop that invokes method
with each subtest.
"""

@functools.wraps(meth)
def wrapper(self):
for alpharep in self.zipfile_alpharep():
meth(self, alpharep=alpharep)

return wrapper
pass_alpharep = parameterize(
['alpharep'],
[
Invoked.wrap(build_alpharep_fixture),
Invoked.wrap(compose(add_dirs, build_alpharep_fixture)),
],
)


class TestPath(unittest.TestCase):
def setUp(self):
self.fixtures = contextlib.ExitStack()
self.addCleanup(self.fixtures.close)

def zipfile_alpharep(self):
with self.subTest():
yield build_alpharep_fixture()
with self.subTest():
yield add_dirs(build_alpharep_fixture())

def zipfile_ondisk(self, alpharep):
tmpdir = pathlib.Path(self.fixtures.enter_context(temp_dir()))
buffer = alpharep.fp
Expand Down Expand Up @@ -413,6 +405,5 @@ def test_root_unnamed(self, alpharep):
@pass_alpharep
def test_inheritance(self, alpharep):
cls = type('PathChild', (zipp.Path,), {})
for alpharep in self.zipfile_alpharep():
file = cls(alpharep).joinpath('some dir').parent
assert isinstance(file, cls)
file = cls(alpharep).joinpath('some dir').parent
assert isinstance(file, cls)

0 comments on commit c3b5c4e

Please sign in to comment.