mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
- Changed output of DocTestParser.get_program() to make it easier to
visually distinguish the expected output from the comments (use "##" to mark expected outputs, and "#" to mark comments). - If the string given to DocTestParser.get_program() is indented, then strip its indentation. (In particular, find the min indentation of non-blank lines, and strip that indentation from all lines.)
This commit is contained in:
parent
8e4a34ba09
commit
a5db6009fe
2 changed files with 37 additions and 23 deletions
|
@ -595,14 +595,20 @@ class DocTestParser:
|
||||||
print x
|
print x
|
||||||
print y
|
print y
|
||||||
# Expected:
|
# Expected:
|
||||||
# 2
|
## 2
|
||||||
# 3
|
## 3
|
||||||
#
|
#
|
||||||
# Some text.
|
# Some text.
|
||||||
x+y
|
x+y
|
||||||
# Expected:
|
# Expected:
|
||||||
# 5
|
## 5
|
||||||
"""
|
"""
|
||||||
|
string = string.expandtabs()
|
||||||
|
# If all lines begin with the same indentation, then strip it.
|
||||||
|
min_indent = self._min_indent(string)
|
||||||
|
if min_indent > 0:
|
||||||
|
string = '\n'.join([l[min_indent:] for l in string.split('\n')])
|
||||||
|
|
||||||
output = []
|
output = []
|
||||||
charnum, lineno = 0, 0
|
charnum, lineno = 0, 0
|
||||||
# Find all doctest examples in the string:
|
# Find all doctest examples in the string:
|
||||||
|
@ -620,7 +626,7 @@ class DocTestParser:
|
||||||
# Display the expected output, if any
|
# Display the expected output, if any
|
||||||
if want:
|
if want:
|
||||||
output.append('# Expected:')
|
output.append('# Expected:')
|
||||||
output.extend(['# '+l for l in want.split('\n')])
|
output.extend(['## '+l for l in want.split('\n')])
|
||||||
|
|
||||||
# Update the line number & char number.
|
# Update the line number & char number.
|
||||||
lineno += string.count('\n', m.start(), m.end())
|
lineno += string.count('\n', m.start(), m.end())
|
||||||
|
@ -702,6 +708,14 @@ class DocTestParser:
|
||||||
(lineno, name, source))
|
(lineno, name, source))
|
||||||
return options
|
return options
|
||||||
|
|
||||||
|
# This regular expression finds the indentation of every non-blank
|
||||||
|
# line in a string.
|
||||||
|
_INDENT_RE = re.compile('^([ ]+)(?=\S)', re.MULTILINE)
|
||||||
|
|
||||||
|
def _min_indent(self, s):
|
||||||
|
"Return the minimum indentation of any non-blank line in `s`"
|
||||||
|
return min([len(indent) for indent in self._INDENT_RE.findall(s)])
|
||||||
|
|
||||||
def _comment_line(self, line):
|
def _comment_line(self, line):
|
||||||
"Return a commented form of the given line"
|
"Return a commented form of the given line"
|
||||||
line = line.rstrip()
|
line = line.rstrip()
|
||||||
|
@ -2185,15 +2199,15 @@ def script_from_examples(s):
|
||||||
#
|
#
|
||||||
2 + 2
|
2 + 2
|
||||||
# Expected:
|
# Expected:
|
||||||
# 5
|
## 5
|
||||||
#
|
#
|
||||||
# And very friendly error messages:
|
# And very friendly error messages:
|
||||||
#
|
#
|
||||||
1/0
|
1/0
|
||||||
# Expected:
|
# Expected:
|
||||||
# To Infinity
|
## To Infinity
|
||||||
# And
|
## And
|
||||||
# Beyond
|
## Beyond
|
||||||
#
|
#
|
||||||
# You can use logic if you want:
|
# You can use logic if you want:
|
||||||
#
|
#
|
||||||
|
|
|
@ -1057,7 +1057,7 @@ words and expected output are converted to comments:
|
||||||
#
|
#
|
||||||
print sample_func(22)
|
print sample_func(22)
|
||||||
# Expected:
|
# Expected:
|
||||||
# 44
|
## 44
|
||||||
#
|
#
|
||||||
# Yee ha!
|
# Yee ha!
|
||||||
|
|
||||||
|
@ -1065,18 +1065,18 @@ words and expected output are converted to comments:
|
||||||
>>> print doctest.testsource(test.test_doctest, name)
|
>>> print doctest.testsource(test.test_doctest, name)
|
||||||
print '1\n2\n3'
|
print '1\n2\n3'
|
||||||
# Expected:
|
# Expected:
|
||||||
# 1
|
## 1
|
||||||
# 2
|
## 2
|
||||||
# 3
|
## 3
|
||||||
|
|
||||||
>>> name = 'test.test_doctest.SampleClass.a_classmethod'
|
>>> name = 'test.test_doctest.SampleClass.a_classmethod'
|
||||||
>>> print doctest.testsource(test.test_doctest, name)
|
>>> print doctest.testsource(test.test_doctest, name)
|
||||||
print SampleClass.a_classmethod(10)
|
print SampleClass.a_classmethod(10)
|
||||||
# Expected:
|
# Expected:
|
||||||
# 12
|
## 12
|
||||||
print SampleClass(0).a_classmethod(10)
|
print SampleClass(0).a_classmethod(10)
|
||||||
# Expected:
|
# Expected:
|
||||||
# 12
|
## 12
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def test_debug(): r"""
|
def test_debug(): r"""
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue