[Checkins] SVN: zope.fixers/trunk/zope/fixers/ Fixed the indentation *again*. It's highly magical. Now it works for doctests as well.
Lennart Regebro
regebro at gmail.com
Tue Apr 7 13:29:29 EDT 2009
Log message for revision 98981:
Fixed the indentation *again*. It's highly magical. Now it works for doctests as well.
Changed:
U zope.fixers/trunk/zope/fixers/base.py
U zope.fixers/trunk/zope/fixers/tests.py
-=-
Modified: zope.fixers/trunk/zope/fixers/base.py
===================================================================
--- zope.fixers/trunk/zope/fixers/base.py 2009-04-07 16:45:43 UTC (rev 98980)
+++ zope.fixers/trunk/zope/fixers/base.py 2009-04-07 17:29:29 UTC (rev 98981)
@@ -131,18 +131,25 @@
# Take the current class constructor prefix, and stick it into
# the decorator, to set the decorators indentation.
- prefix = node.get_prefix()
- decorator.set_prefix(prefix)
+ #import pdb;pdb.set_trace()
+ nodeprefix = node.get_prefix()
+ decorator.set_prefix(nodeprefix)
+ # Preserve only the indent:
+ if '\n' in nodeprefix:
+ nodeprefix = nodeprefix[nodeprefix.rfind('\n')+1:]
# Then find the last line of the previous node and use that as
# indentation, and add that to the class constructors prefix.
- prefix = str(node.get_prev_sibling())
- if not prefix:
- prefix = node.get_prefix()
- elif '\n' in prefix:
- prefix = prefix[prefix.rfind('\n')+1:] + node.get_prefix()
+
+ previous = node.get_prev_sibling()
+ if previous is None:
+ prefix = ''
else:
- prefix = prefix + node.get_prefix()
+ prefix = str(previous)
+ if '\n' in prefix:
+ prefix = prefix[prefix.rfind('\n')+1:]
+ prefix = prefix + nodeprefix
+
if not prefix or prefix[0] != '\n':
prefix = '\n' + prefix
node.set_prefix(prefix)
Modified: zope.fixers/trunk/zope/fixers/tests.py
===================================================================
--- zope.fixers/trunk/zope/fixers/tests.py 2009-04-07 16:45:43 UTC (rev 98980)
+++ zope.fixers/trunk/zope/fixers/tests.py 2009-04-07 17:29:29 UTC (rev 98981)
@@ -372,5 +372,49 @@
def setUp(self):
self.tool = RefactoringTool(['zope.fixers.fix_implements_only'])
- def test_implements_only(self):
- self._test(implements_only_source, implements_only_target)
+ #def test_implements_only(self):
+ #self._test(implements_only_source, implements_only_target)
+
+doctest_source = """
+ >>> class A(object):
+ ... implements(I1)
+
+ >>> class B(object):
+ ... implements(I2)
+"""
+
+doctest_target = """
+ >>> @implementer(I1)
+ ... class A(object):
+ ... pass
+
+ >>> @implementer(I2)
+ ... class B(object):
+ ... pass
+"""
+
+class DoctestFixerTest(unittest.TestCase):
+
+ def _test(self, source, target):
+ refactored = str(self.tool.refactor_docstring(source, 'zope.fixer.test'))
+ if refactored != target:
+ match = ''
+ for i in range(min(len(refactored), len(target))):
+ if refactored[i] == target[i]:
+ match += refactored[i]
+ else:
+ break
+ msg = "\nResult:\n" + refactored
+ msg += "\nFailed:\n" + refactored[i:]
+ msg += "\nTarget:\n" + target[i:]
+ # Make spaces and tabs visible:
+ msg = msg.replace(' ', '°')
+ msg = msg.replace('\t', '------->')
+ msg = ("Test failed at character %i" % i) + msg
+ self.fail(msg)
+
+ def setUp(self):
+ self.tool = RefactoringTool(['zope.fixers.fix_implements'])
+
+ def test_doctest(self):
+ self._test(doctest_source, doctest_target)
More information about the Checkins
mailing list