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_inline_directive():
"""
python ~/code/xdoctest/testing/test_parser.py test_inline_directive
"""
string = utils.codeblock(
'''
>>> # doctest: +SKIP
>>> func1(*
>>> [i for i in range(10)])
>>> # not a directive
>>> func2( # not a directive
>>> a=b
>>> )
>>> func3() # xdoctest: +SKIP
>>> func4()
want1
>>> func5() # xdoctest: +SKIP
want1
>>> # xdoctest: +SKIP
>>> func6()
>>> func7(a=b,
def test_block_skip_directive():
"""
pytest testing/test_directive.py::test_block_skip_directive
"""
string = utils.codeblock(
'''
>>> x = 0
>>> # doctest: +SKIP
>>> assert False, 'should be skipped'
''')
self = doctest_example.DocTest(docsrc=string)
result = self.run(on_error='raise')
assert result['passed']
def test_repl_twoline():
string = utils.codeblock(
'''
>>> x = 1
>>> x = 2
''')
self = parser.DoctestParser(simulate_repl=True)
parts = self.parse(string)
assert [p.source for p in parts] == ['x = 1', 'x = 2']
''')
self = doctest_example.DocTest(docsrc=docsrc)
self._parse()
p1, p2 = self._parts
# test_globals = {}
# code1 = compile(p1.source, '', 'exec')
# exec(code1, test_globals)
# code2 = compile(p2.source, '', 'eval')
# result = eval(code2, test_globals)
try:
self.run()
except Exception as ex:
assert hasattr(ex, 'output_difference')
msg = ex.output_difference(colored=False)
assert msg == utils.codeblock(
'''
Expected:
def test_lineno_failcase_doctest_code():
"""
python ~/code/xdoctest/testing/test_linenos.py test_lineno_failcase_doctest_code
"""
text = _run_case(utils.codeblock(
r'''
def bar():
pass
def func(a):
"""
Example:
>>> # Perform some passing tests before we call failing code
>>> func(0)
0
>>> # call the failing code
>>> assert 1 == 2
>>> # Do stuff that wont be executed
>>> func(0)
0
>>> func(1)
def test_want_error_msg():
"""
python testing/test_doctest_example.py test_want_error_msg
pytest testing/test_doctest_example.py::test_want_error_msg
"""
string = utils.codeblock(
'''
>>> raise Exception('everything is fine')
Traceback (most recent call last):
Exception: everything is fine
''')
self = doctest_example.DocTest(docsrc=string)
result = self.run(on_error='raise')
assert result['passed']
def test_format_src():
"""
python testing/test_doctest_example.py test_format_src
pytest testing/test_doctest_example.py::test_format_src -s -v
"""
string = utils.codeblock(
'''
>>> i = 0
>>> 0 / i
2
''')
string_with_lineno = utils.codeblock(
'''
1 >>> i = 0
2 >>> 0 / i
2
''').replace('!', ' ')
self = doctest_example.DocTest(docsrc=string)
self._parse()
assert self.format_src(colored=0, linenos=1) == string_with_lineno
assert self.format_src(colored=0, linenos=0) == string
self._unmatched_stdout = []
self._skipped_parts = []
self.exc_info = None
self._suppressed_stdout = verbose <= 1
# Initialize a new runtime state
default_state = self.config['default_runtime_state']
runstate = self._runstate = directive.RuntimeState(default_state)
# setup reporting choice
runstate.set_report_style(self.config['reportchoice'].lower())
global_exec = self.config.getvalue('global_exec')
if global_exec:
# Hack to make it easier to specify multi-line input on the CLI
global_source = utils.codeblock(global_exec.replace('\\n', '\n'))
global_code = compile(
global_source, mode='exec',
filename='',
flags=compileflags, dont_inherit=True
)
exec(global_code, test_globals)
# Can't do this because we can't force execution of SCRIPTS
# if self.is_disabled():
# runstate['SKIP'] = True
# Use the same capture object for all parts in the test
cap = utils.CaptureStdout(supress=self._suppressed_stdout)
with warnings.catch_warnings(record=True) as self.warn_list:
for partx, part in enumerate(self._parts):
# Extract directives and and update runtime state