Skip to content

Commit ed32355

Browse files
committed
add test for check_dependency
1 parent 01928ea commit ed32355

File tree

2 files changed

+49
-32
lines changed

2 files changed

+49
-32
lines changed

fooof/core/modutils.py

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,38 @@ def safe_import(*args):
4545
return mod
4646

4747

48+
def check_dependency(dep, name):
49+
"""Decorator that checks if an optional dependency is available.
50+
51+
Parameters
52+
----------
53+
dep : module or False
54+
Module, if successfully imported, or boolean (False) if not.
55+
name : str
56+
Full name of the module, to be printed in message.
57+
58+
Returns
59+
-------
60+
wrap : callable
61+
The decorated function.
62+
63+
Raises
64+
------
65+
ImportError
66+
If the requested dependency is not available.
67+
"""
68+
69+
def wrap(func):
70+
@wraps(func)
71+
def wrapped_func(*args, **kwargs):
72+
if not dep:
73+
raise ImportError("Optional FOOOF dependency " + name + \
74+
" is required for this functionality.")
75+
return func(*args, **kwargs)
76+
return wrapped_func
77+
return wrap
78+
79+
4880
def docs_drop_param(docstring):
4981
"""Drop the first parameter description for a string representation of a docstring.
5082
@@ -148,35 +180,3 @@ def wrapper(func):
148180
return func
149181

150182
return wrapper
151-
152-
153-
def check_dependency(dep, name):
154-
"""Decorator that checks if an optional dependency is available.
155-
156-
Parameters
157-
----------
158-
dep : module or False
159-
Module, if successfully imported, or boolean (False) if not.
160-
name : str
161-
Full name of the module, to be printed in message.
162-
163-
Returns
164-
-------
165-
wrap : callable
166-
The decorated function.
167-
168-
Raises
169-
------
170-
ImportError
171-
If the requested dependency is not available.
172-
"""
173-
174-
def wrap(func):
175-
@wraps(func)
176-
def wrapped_func(*args, **kwargs):
177-
if not dep:
178-
raise ImportError("Optional FOOOF dependency " + name + \
179-
" is required for this functionality.")
180-
return func(*args, **kwargs)
181-
return wrapped_func
182-
return wrap

fooof/tests/core/test_modutils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
Note: decorators (that are in modutils) are currently not tested.
44
"""
55

6+
from pytest import raises
7+
68
from fooof.core.modutils import *
79

810
###################################################################################################
@@ -16,6 +18,21 @@ def test_safe_import():
1618
bad = safe_import('bad')
1719
assert not bad
1820

21+
def test_check_dependency():
22+
23+
import numpy as np
24+
@check_dependency(np, 'numpy')
25+
def subfunc_good():
26+
pass
27+
subfunc_good()
28+
29+
bad = None
30+
@check_dependency(bad, 'bad')
31+
def subfunc_bad():
32+
pass
33+
with raises(ImportError):
34+
subfunc_bad()
35+
1936
def test_docs_drop_param():
2037

2138
ds = """STUFF

0 commit comments

Comments
 (0)