[py-svn] r38142 - py/trunk/py/doc

guido at codespeak.net guido at codespeak.net
Thu Feb 8 14:47:25 CET 2007


Author: guido
Date: Thu Feb  8 14:47:23 2007
New Revision: 38142

Modified:
   py/trunk/py/doc/conftest.py
   py/trunk/py/doc/test_conftest.py
Log:
Fixed problem with indentation in the results of a doctest.


Modified: py/trunk/py/doc/conftest.py
==============================================================================
--- py/trunk/py/doc/conftest.py	(original)
+++ py/trunk/py/doc/conftest.py	Thu Feb  8 14:47:23 2007
@@ -14,6 +14,24 @@
         )
 ) 
 
+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 @@
         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:

Modified: py/trunk/py/doc/test_conftest.py
==============================================================================
--- py/trunk/py/doc/test_conftest.py	(original)
+++ py/trunk/py/doc/test_conftest.py	Thu Feb  8 14:47:23 2007
@@ -52,6 +52,15 @@
     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 @@
     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')



More information about the pytest-commit mailing list