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 = utils.codeblock(
'''
>>> a = 1
>>> 1 / 0
We have a want
''')
self = 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
def test_retain_source():
"""
"""
source = utils.codeblock(
'''
>>> x = 2
>>> print("foo")
foo
''')
source_lines = source.split('\n')[:-1]
self = parser.DoctestParser()
linenos, eval_final = self._locate_ps1_linenos(source_lines)
assert eval_final
assert linenos == [0, 1]
p1, p2 = self.parse(source)
assert p1.source == 'x = 2'
assert p2.source == 'print("foo")'
... a=b)
... dsrc
>>> dsrc():
... a
... b = """
multiline
"""
want
text
... still text
>>> "now its a doctest"
text
'''
self = parser.DoctestParser()
labeled = self._label_docsrc_lines(string)
expected = [
('text', ''),
('text', ' text'),
('dsrc', ' >>> dsrc()'),
('want', ' want'),
('text', ''),
('dsrc', ' >>> dsrc()'),
('dsrc', ' >>> cont('),
('dsrc', ' ... a=b)'),
('dsrc', ' ... dsrc'),
('dsrc', ' >>> dsrc():'),
('dsrc', ' ... a'),
('dsrc', ' ... b = """'),
('dsrc', ' multiline'),
('dsrc', ' """'),
string = utils.codeblock(
'''
>>> x = 2
>>> x + 1
1
''')
self = parser.DoctestParser()
parts = self.parse(string)
assert [p.use_eval for p in parts] == [False, True]
string = utils.codeblock(
'''
>>> x = 2
>>> x + 1
''')
self = parser.DoctestParser()
parts = self.parse(string)
assert [p.use_eval for p in parts] == [False]
string = utils.codeblock(
r'''
>>> x = 2
>>> x += 3
>>> """
... foobar
... """
'\nfoobar\n'
''')
self = parser.DoctestParser()
parts = self.parse(string)
assert [p.use_eval for p in parts] == [False, True]
'Sympy:',
]
else:
special_skip_patterns = [] # nocover
special_skip_patterns_ = tuple([
p.lower() for p in special_skip_patterns
])
def _start_ignoring(prev):
return (special_skip_patterns_ and
isinstance(prev, six.string_types) and
prev.strip().lower().endswith(special_skip_patterns_))
# parse into doctest and plaintext parts
info = dict(callname=callname, modpath=modpath, lineno=lineno, fpath=fpath)
all_parts = list(parser.DoctestParser().parse(docstr, info))
curr_parts = []
curr_offset = 0
num = 0
prev_part = None
ignoring = False
for part in all_parts:
if isinstance(part, six.string_types):
# Part is a plaintext
if asone:
# Lump all doctest parts into one example
if not curr_parts:
curr_offset += part.count('\n') + 1
else: # nocover
if curr_parts:
def test_text_after_want():
string = utils.codeblock('''
Example:
>>> dsrc()
want
just some test
''')
self = parser.DoctestParser()
labeled = self._label_docsrc_lines(string)
expected = [
('text', 'Example:'),
('dsrc', ' >>> dsrc()'),
('want', ' want'),
('text', 'just some test'),
]
assert labeled == expected
def test_block_directive_want1_eval():
"""
python ~/code/xdoctest/testing/test_parser.py test_block_directive_want1
"""
string = utils.codeblock(
'''
>>> # doctest: +SKIP
>>> func1()
>>> func2() # eval this line so it is broken off
want
''')
source_lines = string.splitlines()
self = parser.DoctestParser()
ps1_linenos = self._locate_ps1_linenos(source_lines)[0]
parts = self.parse(string)
assert len(parts) == 2
>>> _, o = 0, 1
>>> A = B = C = D = 1
>>> cc_mask = [ # Y
>>> [ _, _, _, o, _, _, ], # 0
>>> [ _, _, o, o, o, _, ], # 1
>>> [ _, o, o, o, o, o, ], # 2
>>> [ o, o, o, o, o, _, ], # 3
>>> [ _, o, o, o, _, _, ], # 4
>>> [ _, _, o, o, _, _, ], # 5
>>> # X: 0 1 2 3 4 5 6
>>> ]
>>> # a regular comment
>>> print(cc_mask)
''')
source_lines = string.splitlines()
self = parser.DoctestParser()
ps1_linenos = self._locate_ps1_linenos(source_lines)[0]
assert ps1_linenos == [0, 1, 2, 11, 12]
parts = self.parse(string)
assert len(parts) == 1
>>> docstr = DocTest._parse.__doc__
>>> blocks = docscrape_google.split_google_docblocks(docstr)
>>> doclineno = DocTest._parse.__code__.co_firstlineno
>>> key, (docsrc, offset) = blocks[-2]
>>> lineno = doclineno + offset
>>> self = DocTest(docsrc, doctest_example.__file__, '_parse', 0,
>>> lineno)
>>> self._parse()
>>> assert len(self._parts) >= 3
>>> #p1, p2, p3 = self._parts
>>> self.run()
"""
if not self._parts:
info = dict(callname=self.callname, modpath=self.modpath,
lineno=self.lineno, fpath=self.fpath)
self._parts = parser.DoctestParser().parse(self.docsrc, info)
self._parts = [p for p in self._parts
if not isinstance(p, six.string_types)]
# Ensure part numbers are given
for partno, part in enumerate(self._parts):
part.partno = partno