Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_mod_globals():
with utils.TempDir() as temp:
dpath = temp.dpath
modpath = join(dpath, 'test_mod_globals.py')
source = utils.codeblock(
'''
X = 10
def test(self):
"""
>>> X
10
"""
''')
with open(modpath, 'w') as file:
file.write(source)
from xdoctest import core
doctests = list(core.parse_doctestables(modpath, style='freeform'))
assert len(doctests) == 1
def test_mod_lineno():
with utils.TempDir() as temp:
dpath = temp.dpath
modpath = join(dpath, 'test_mod_lineno.py')
source = utils.codeblock(
'''
class Fun(object): #1
@property
def test(self):
""" # 4
>>> a = 1
>>> 1 / 0
"""
''')
with open(modpath, 'w') as file:
file.write(source)
doctests = list(core.parse_doctestables(modpath, style='freeform'))
assert len(doctests) == 1
pass
def non_zero_args3(a, b, *args):
pass
def non_zero_args4(a, b, **kwargs):
pass
def non_zero_args5(a, b=1, **kwargs):
pass
''')
with utils.TempDir() as temp:
dpath = temp.dpath
modpath = join(dpath, 'test_zero_args.py')
with open(modpath, 'w') as file:
file.write(source)
zero_func_names = {
example.callname
for example in runner._gather_zero_arg_examples(modpath)
}
assert zero_func_names == set(['zero_args1', 'zero_args2',
'zero_args3', 'zero_args4'])
def test_collect_module_level_singleline():
"""
pytest testing/test_core.py::test_collect_module_level
Ignore:
temp = utils.TempDir()
"""
temp = utils.TempDir()
dpath = temp.ensure()
modpath = join(dpath, 'test_collect_module_level_singleline.py')
source = utils.codeblock(
'''">>> pass"''')
with open(modpath, 'w') as file:
file.write(source)
from xdoctest import core
doctests = list(core.parse_doctestables(modpath, style='freeform'))
assert len(doctests) == 1
self = doctests[0]
assert self.callname == '__doc__'
self.config['colored'] = False
assert self.format_src(offset_linenos=True).strip().startswith('1')
assert self.format_src(offset_linenos=False).strip().startswith('1')
with utils.PythonPathContext(dpath):
def __init__(self, modname, docstr):
self.modname = modname
self.docstr = docstr
self.temp = TempDir()
self.dpath = self.temp.ensure()
self.modpath = join(self.dpath, self.modname + '.py')
with open(self.modpath, 'w') as file:
file.write("'''\n%s'''" % self.docstr)
def fake_test1(a=1):
pass
def real_test2():
"""
Example:
>>> pass
"""
pass
def fake_test2():
pass
''')
with utils.TempDir() as temp:
dpath = temp.dpath
modpath = join(dpath, 'test_list.py')
with open(modpath, 'w') as file:
file.write(source)
with utils.CaptureStdout() as cap:
runner.doctest_module(modpath, 'list', argv=[''])
assert 'real_test1' in cap.text
assert 'real_test2' in cap.text
assert 'fake_test1' not in cap.text
assert 'fake_test2' not in cap.text
# test command=None
with utils.CaptureStdout() as cap:
def test_modname_to_modpath_namespace():
"""
Ignore:
import sys
sys.path.append('/home/joncrall/code/xdoctest/testing')
from test_static import *
temp = utils.TempDir()
temp.__enter__()
sys.path.append(temp.dpath)
temp.__exit__(None, None, None)
%timeit _syspath_modname_to_modpath('xdoctest.static_analysis')
%timeit _pkgutil_modname_to_modpath('xdoctest.static_analysis')
"""
with utils.TempDir() as temp:
dpath = temp.dpath
# Some "bad" non-module directories
tmpbad = utils.ensuredir((dpath, '_tmpbad'))
# Make a submodule of a bad directory, look good.
sub_bad = utils.ensuredir((tmpbad, 'sub_bad'))
touch((tmpbad, '_inbad.py'))
subbad = touch((sub_bad, '__init__.py')) # NOQA
b0 = touch((sub_bad, 'b0.py')) # NOQA
with utils.PythonPathContext(dpath):
assert _static_modname_to_modpath('_tmpbad') is None
# Tricky case, these modules look good outside of _tmpbad WOW, you
# can actually import this and it works, but pkgloader still
def test_no_docstr():
"""
CommandLine:
python -m test_core test_no_docstr
"""
with utils.TempDir() as temp:
dpath = temp.dpath
modpath = join(dpath, 'test_no_docstr.py')
source = utils.codeblock(
'''
def get_scales(kpts):
""" Gets average scale (does not take into account elliptical shape """
_scales = np.sqrt(get_sqrd_scales(kpts))
return _scales
''')
with open(modpath, 'w') as file:
file.write(source)
from xdoctest import core
doctests = list(core.parse_doctestables(modpath, style='freeform'))
assert len(doctests) == 0
def test_modname_to_modpath_package():
"""
CommandLine:
pytest testing/test_static.py::test_modname_to_modpath_package
Ignore:
import sys
sys.path.append('/home/joncrall/code/xdoctest/testing')
from test_static import *
temp = utils.TempDir()
temp.__enter__()
sys.path.append(temp.dpath)
temp.__exit__(None, None, None)
"""
with utils.TempDir() as temp:
dpath = temp.dpath
# Create a dummy package heirachy
root = utils.ensuredir((dpath, '_tmproot'))
sub1 = utils.ensuredir((root, 'sub1'))
sub2 = utils.ensuredir((sub1, 'sub2'))
root_init = touch((root, '__init__.py'))
sub1_init = touch((sub1, '__init__.py'))
sub2_init = touch((sub2, '__init__.py'))
mod0 = touch((root, 'mod0.py'))
mod1 = touch((sub1, 'mod1.py'))
mod2 = touch((sub2, 'mod2.py'))
root_main = touch((root, '__main__.py'))
'\n ======== '
'\n', COLOR)
cprint('CASE SOURCE:')
cprint('------------')
print(utils.indent(
utils.add_line_numbers(utils.highlight_code(source, 'python'))))
print('')
import hashlib
hasher = hashlib.sha1()
hasher.update(source.encode('utf8'))
hashid = hasher.hexdigest()[0:8]
with utils.TempDir() as temp:
dpath = temp.dpath
modpath = join(dpath, 'test_linenos_' + hashid + '.py')
with open(modpath, 'w') as file:
file.write(source)
with utils.CaptureStdout(supress=False) as cap:
runner.doctest_module(modpath, 'all', argv=[''], style=style)
cprint('\n\n --- --- \n\n', COLOR)
return cap.text