Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
self = doctests[0]
# print(self._parts[0])
assert self.lineno == 5
# print(self.format_src())
self.config['colored'] = False
assert self.format_src(offset_linenos=False).strip().startswith('1')
assert self.format_src(offset_linenos=True).strip().startswith('5')
with utils.PythonPathContext(dpath):
status = self.run(verbose=10, on_error='return')
assert not status['passed']
def _test_status(docstr):
docstr = utils.codeblock(docstr)
try:
temp = utils.util_misc.TempDoctest(docstr=docstr)
except Exception:
# pytest seems to load an older version of xdoctest for some reason
import xdoctest
import inspect
print('xdoctest.__version__ = {!r}'.format(xdoctest.__version__))
print('utils = {!r}'.format(utils))
print('utils.util_misc = {!r}'.format(utils.util_misc))
print('utils.TempDoctest = {!r}'.format(utils.TempDoctest))
print(inspect.getargspec(utils.TempDoctest))
raise
doctests = list(core.parse_doctestables(temp.modpath))
status = doctests[0].run(verbose=0, on_error='return')
return status
if command is None:
# Display help if command is not specified
_log('Not testname given. Use `all` to run everything or'
' pick from a list of valid choices:')
command = 'list'
# TODO: command should not be allowed to be the requested doctest name in
# case it conflicts with an existing command. This probably requires an API
# change to this function.
gather_all = (command == 'all' or command == 'dump')
tic = time.time()
# Parse all valid examples
with warnings.catch_warnings(record=True) as parse_warnlist:
examples = list(core.parse_doctestables(modpath, exclude=exclude,
style=style))
# Set each example mode to native to signal that we are using the
# native xdoctest runner instead of the pytest runner
for example in examples:
example.mode = 'native'
if command == 'list':
if len(examples) == 0:
_log('... no docstrings with examples found')
else:
_log(' ' + '\n '.join([example.cmdline # + ' @ ' + str(example.lineno)
for example in examples]))
run_summary = {'action': 'list'}
else:
_log('gathering tests')
enabled_examples = []
Ignore:
temp = utils.TempDir()
"""
temp = utils.TempDir()
dpath = temp.ensure()
modpath = join(dpath, 'test_collect_module_level.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
src_offset = self.format_src(offset_linenos=True).strip()
src_nooffset = self.format_src(offset_linenos=False).strip()
assert src_offset[:4].startswith('2')
assert src_nooffset[:4].startswith('1')
with utils.PythonPathContext(dpath):
status = self.run(verbose=0, on_error='return')
assert status['passed']
temp.cleanup()
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):
status = self.run(verbose=0, on_error='return')
assert status['passed']
temp.cleanup()
def collect(self):
from xdoctest import core
modpath = str(self.fspath)
style = self.config.getvalue('xdoctest_style')
self._prepare_internal_config()
try:
examples = list(core.parse_doctestables(modpath, style=style))
except SyntaxError:
if self.config.getvalue('xdoctest_ignore_syntax_errors'):
pytest.skip('unable to import module %r' % self.fspath)
else:
raise
for example in examples:
example.config.update(self._examp_conf)
name = example.unique_callname
yield XDoctestItem(name, self, example)
"""
python ~/code/xdoctest/testing/test_core.py test_oneliner
"""
with utils.TempDir() as temp:
dpath = temp.dpath
modpath = join(dpath, 'test_oneliner.py')
source = utils.codeblock(
'''
def foo():
"""
>>> assert False, 'should fail'
"""
''')
with open(modpath, 'w') as file:
file.write(source)
doctests = list(core.parse_doctestables(modpath))
assert len(doctests) == 1
print('doctests = {!r}'.format(doctests))
import pytest
with pytest.raises(AssertionError, message='should fail'):
doctests[0].run()
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