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_parse_eval_single_want():
string = ub.codeblock(
'''
>>> a = 1
>>> 1 / 0
We have a want
''')
self = doctest_parser.DoctestParser()
parts = self.parse(string)
raw_source_lines = string.split('\n')[:-1]
ps1_linenos, eval_final = self._locate_ps1_linenos(raw_source_lines)
assert ps1_linenos == [0, 1]
assert eval_final
# Only one part because there is no want
assert len(parts) == 2
... a=b)
... dsrc
>>> dsrc():
... a
... b = """
multiline
"""
want
text
... still text
>>> "now its a doctest"
text
'''
string = ub.codeblock(string)
self = doctest_parser.DoctestParser()
self._label_docsrc_lines(string)
self.parse(string)
string = ub.codeblock(
'''
.. doctest::
>>> print(
... "Hi\\n\\nByé")
Hi
...
Byé
>>> 1/0 # Byé
1
def test_label_lines():
string = ub.codeblock(
r'''
>>> i = 0
>>> 0 / i
2
''')
self = doctest_parser.DoctestParser()
labeled = self._label_docsrc_lines(string)
assert labeled == [
('dsrc', '>>> i = 0'),
('dsrc', '>>> 0 / i'),
('want', '2')
]
def __repr__(self):
return ub.codeblock(
'''
import numpy as np
except ImportError:
import pytest
pytest.skip('numpy is optional')
data = ub.odict(zip(
['one', 'two', 'three', 'four'],
[
np.zeros((3, 3), dtype=np.int32),
np.zeros((3, 10), dtype=np.int32),
np.zeros((3, 20), dtype=np.int32),
np.zeros((3, 30), dtype=np.int32),
]))
text = ub.repr2(data, nl=2)
print(text)
assert text == ub.codeblock(
'''
{
text-line-3
text-line-4
text-line-5
text-line-6
text-line-7
text-line-8
text-line-9
text-line-10
text-line-11
>>> 1 + 1
3
text-line-after
''')
string = ub.codeblock(
"""
>>> '''
multiline strings are now kosher
'''.strip()
'multiline strings are now kosher'
>>> '''
double multiline string
'''.strip()
...
>>> '''
double multiline string
'''.strip()
'double multiline string'
""")
r'''
for i in `seq 0 29`;
do
sleep .001
>&1 echo "O$i"
if [ "$(($i % 5))" = "0" ]; then
>&2 echo "!E$i"
fi
done
''').lstrip()
result = ub.cmd(sh_script, shell=True, verbose=0)
assert result['out'] == 'O0\nO1\nO2\nO3\nO4\nO5\nO6\nO7\nO8\nO9\nO10\nO11\nO12\nO13\nO14\nO15\nO16\nO17\nO18\nO19\nO20\nO21\nO22\nO23\nO24\nO25\nO26\nO27\nO28\nO29\n'
assert result['err'] == '!E0\n!E5\n!E10\n!E15\n!E20\n!E25\n'
else:
sh_script = ub.codeblock(
r'''
for i in `seq 0 15`;
do
sleep .000001
>&1 echo "O$i"
if [ "$(($i % 5))" = "0" ]; then
>&2 echo "!E$i"
fi
done
''').lstrip()
result = ub.cmd(sh_script, shell=True, verbose=0)
assert result['out'] == 'O0\nO1\nO2\nO3\nO4\nO5\nO6\nO7\nO8\nO9\nO10\nO11\nO12\nO13\nO14\nO15\n'
assert result['err'] == '!E0\n!E5\n!E10\n!E15\n'
def generate():
content = ub.readfrom('base_diff.py') + '\n\n'
xdoc_version = content + ub.codeblock(
'''
if __name__ == '__main__':
import xdoctest
xdoctest.doctest_module(__file__)
''') + '\n'
doc_version = content + ub.codeblock(
'''
if __name__ == '__main__':
import doctest
doctest.testmod()
''') + '\n'
ub.writeto('_doc_version.py', doc_version)
ub.writeto('_xdoc_version.py', xdoc_version)