[svn r38142] Fixed problem with indentation in the results of a doctest.

--HG--
branch : trunk
This commit is contained in:
guido 2007-02-08 14:47:23 +01:00
parent fd0cbdb95a
commit 5ea9a43dce
2 changed files with 46 additions and 4 deletions

View File

@ -14,6 +14,24 @@ option = py.test.config.addoptions("documentation check options",
)
)
def deindent(s, sep='\n'):
leastspaces = -1
lines = s.split(sep)
for line in lines:
if not line.strip():
continue
spaces = len(line) - len(line.lstrip())
if leastspaces == -1 or spaces < leastspaces:
leastspaces = spaces
if leastspaces == -1:
return s
for i, line in py.builtin.enumerate(lines):
if not line.strip():
lines[i] = ''
else:
lines[i] = line[leastspaces:]
return sep.join(lines)
_initialized = False
def checkdocutils():
global _initialized
@ -73,10 +91,10 @@ class DoctestText(py.test.Item):
l = []
prefix = '.. >>> '
mod = py.std.types.ModuleType(self.fspath.purebasename)
for line in s.split('\n'):
line = line.strip()
if line.startswith(prefix):
exec py.code.Source(line[len(prefix):]).compile() in \
for line in deindent(s).split('\n'):
stripped = line.strip()
if stripped.startswith(prefix):
exec py.code.Source(stripped[len(prefix):]).compile() in \
mod.__dict__
line = ""
else:

View File

@ -52,6 +52,15 @@ def test_doctest_basic():
l2 = session.getitemoutcomepairs(Skipped)
assert len(l+l2) == 2
def test_deindent():
from py.__.doc.conftest import deindent
assert deindent('foo') == 'foo'
assert deindent('foo\n bar') == 'foo\n bar'
assert deindent(' foo\n bar\n') == 'foo\nbar\n'
assert deindent(' foo\n\n bar\n') == 'foo\n\nbar\n'
assert deindent(' foo\n bar\n') == 'foo\n bar\n'
assert deindent(' foo\n bar\n') == ' foo\nbar\n'
def test_doctest_eol():
# XXX get rid of the next line:
py.magic.autopath().dirpath('conftest.py').copy(tmpdir.join('conftest.py'))
@ -67,6 +76,21 @@ def test_doctest_eol():
l2 = session.getitemoutcomepairs(Skipped)
assert len(l+l2) == 2
def test_doctest_indentation():
# XXX get rid of the next line:
py.magic.autopath().dirpath('conftest.py').copy(tmpdir.join('conftest.py'))
txt = tmpdir.join('foo.txt')
txt.write('..\n >>> print "foo\\n bar"\n foo\n bar\n')
config = py.test.config._reparse([txt])
session = config.initsession()
session.main()
l = session.getitemoutcomepairs(Failed)
assert len(l) == 0
l = session.getitemoutcomepairs(Passed)
l2 = session.getitemoutcomepairs(Skipped)
assert len(l+l2) == 2
def test_js_ignore():
py.magic.autopath().dirpath('conftest.py').copy(tmpdir.join('conftest.py'))
tmpdir.ensure('__init__.py')