[Checkins] SVN: zope.fixers/trunk/ Supported various cases that exist in zope.interfaces tests (and probably nowhere else).
Lennart Regebro
regebro at gmail.com
Mon Apr 6 15:19:44 EDT 2009
Log message for revision 98952:
Supported various cases that exist in zope.interfaces tests (and probably nowhere else).
Changed:
U zope.fixers/trunk/setup.py
U zope.fixers/trunk/zope/fixers/fix_implements.py
U zope.fixers/trunk/zope/fixers/tests.py
-=-
Modified: zope.fixers/trunk/setup.py
===================================================================
--- zope.fixers/trunk/setup.py 2009-04-06 18:34:44 UTC (rev 98951)
+++ zope.fixers/trunk/setup.py 2009-04-06 19:19:43 UTC (rev 98952)
@@ -39,7 +39,7 @@
packages=find_packages(exclude=['ez_setup']),
namespace_packages=['zope'],
include_package_data=True,
- zip_safe=True,
+ zip_safe=False,
install_requires=[
'setuptools',
],
Modified: zope.fixers/trunk/zope/fixers/fix_implements.py
===================================================================
--- zope.fixers/trunk/zope/fixers/fix_implements.py 2009-04-06 18:34:44 UTC (rev 98951)
+++ zope.fixers/trunk/zope/fixers/fix_implements.py 2009-04-06 19:19:43 UTC (rev 98952)
@@ -106,8 +106,6 @@
# and put it before the class definition.
statement = results['statement']
- interface = results['interface'].value
-
if not isinstance(statement, list):
statement = [statement]
# Make a copy for insertion before the class:
@@ -122,10 +120,39 @@
if implements.value == 'implements':
implements.value = 'implementor'
+ interface = results['interface']
+ if not isinstance(interface, list):
+ interface = [interface]
+ interface = [x.clone() for x in interface]
+
+ # Find the current indent, and strip any previous whitespace:
+ indent = []
+ previous = []
+ if node.parent.type == syms.suite:
+ remove = []
+ for child in node.parent.children:
+ if str(child).strip(): # Not whitespace
+ break
+ # Whitespace
+ indent += [child.clone()]
+ previous += [child.clone()]
+ if child.value == '\n':
+ # The decorator should go at the nearest line before
+ # the class statement:
+ indent = []
+ # We should remove these, but not until after we have
+ # stopped looping over the node, or everything gets
+ # confused.
+ remove.append(child)
+ # We are finished looping: Remove the whitespace:
+ for child in remove:
+ child.remove()
+
# Create the decorator:
- decorator = Node(syms.decorator, [Leaf(50, '@'), ] + statement + [
- Leaf(7, '('), Leaf(1, interface),
- Leaf(8, ')'), Leaf(4, '\n')])
+ decorator = Node(syms.decorator, previous + [Leaf(50, '@'),] +
+ statement + [Leaf(7, '(')] + interface +
+ [ Leaf(8, ')'), Leaf(4, '\n') ] + indent)
+
# And stick it in before the class defintion:
prefix = node.get_prefix()
node.set_prefix('')
@@ -138,4 +165,10 @@
def finish_tree(self, tree, filename):
for node in self.fixups:
+ parent = node.parent
node.remove()
+ if not str(parent).strip():
+ # This is an empty class. Stick in a pass
+ parent.insert_child(2, Leaf(0, 'pass'))
+ parent.insert_child(3, Leaf(0, '\n'))
+
\ No newline at end of file
Modified: zope.fixers/trunk/zope/fixers/tests.py
===================================================================
--- zope.fixers/trunk/zope/fixers/tests.py 2009-04-06 18:34:44 UTC (rev 98951)
+++ zope.fixers/trunk/zope/fixers/tests.py 2009-04-06 19:19:43 UTC (rev 98952)
@@ -44,6 +44,36 @@
"An IFoo class"
"""
+# Multiple interfaces:
+multi_source = """
+from zope.interface import implements
+
+class IFoo(Interface):
+ pass
+
+class IBar(Interface):
+ pass
+
+class Foo:
+ "An IFoo class"
+
+ implements(IFoo, IBar)
+"""
+
+multi_target = """
+from zope.interface import implementor
+
+class IFoo(Interface):
+ pass
+
+class IBar(Interface):
+ pass
+
+ at implementor(IFoo, IBar)
+class Foo:
+ "An IFoo class"
+"""
+
# Make sure it works even if implements gets renamed.
renamed_source = """
from zope.interface import implements as renamed
@@ -93,44 +123,102 @@
"""
# Interface can get renamed. It's unusual, but should be supported.
-module_renamed_source= """
+module_renamed_source = """
from zope import interface as zopeinterface
+class IFoo(Interface):
+ pass
+
class Foo:
"An IFoo class"
zopeinterface.implements(IFoo)
"""
-module_renamed_target= """
+module_renamed_target = """
from zope import interface as zopeinterface
+class IFoo(Interface):
+ pass
+
@zopeinterface.implementor(IFoo)
class Foo:
"An IFoo class"
"""
-# And lastly, many always uses the full module name.
-full_import_source= """
+# Many always uses the full module name.
+full_import_source = """
import zope.interface
+class IFoo(Interface):
+ pass
+
class Foo:
"An IFoo class"
zope.interface.implements(IFoo)
"""
-full_import_target= """
+full_import_target = """
import zope.interface
+class IFoo(Interface):
+ pass
+
@zope.interface.implementor(IFoo)
class Foo:
"An IFoo class"
"""
-TESTS = [
-]
+# Empty classes:
+empty_class_source = """
+import zope.interface
+class IFoo(Interface):
+ pass
+
+class Foo:
+ zope.interface.implements(IFoo)
+
+"""
+
+empty_class_target = """
+import zope.interface
+
+class IFoo(Interface):
+ pass
+
+ at zope.interface.implementor(IFoo)
+class Foo:
+ pass
+
+"""
+
+# Classes with indentation:
+indented_class_source = """
+import zope.interface
+
+class IFoo(Interface):
+ pass
+
+def forceindent():
+ class Foo:
+ zope.interface.implements(IFoo)
+
+"""
+
+indented_class_target = """
+import zope.interface
+
+class IFoo(Interface):
+ pass
+
+def forceindent():
+ @zope.interface.implementor(IFoo)
+ class Foo:
+ pass
+
+"""
class FixerTest(unittest.TestCase):
def setUp(self):
@@ -145,10 +233,13 @@
match += refactored[i]
else:
break
- msg = "Test failed at character %i" % i
- msg += "\nResult:\n" + refactored
+ 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 test_imports(self):
@@ -156,6 +247,9 @@
def test_simple(self):
self._test(simple_source, simple_target)
+
+ def test_multi(self):
+ self._test(multi_source, multi_target)
def test_renamed(self):
self._test(renamed_source, renamed_target)
@@ -168,4 +262,10 @@
def test_full_import(self):
self._test(full_import_source, full_import_target)
-
\ No newline at end of file
+
+ def test_empty_class(self):
+ self._test(empty_class_source, empty_class_target)
+
+ def test_indented_class(self):
+ self._test(indented_class_source, indented_class_target)
+
\ No newline at end of file
More information about the Checkins
mailing list