Merge lp:~justizin/zope.copypastemove/lp_98385 into lp:zope.copypastemove

Proposed by Tres Seaver on 2010-05-20
Status: Merged
Merge reported by: Tres Seaver
Merged at revision: not available
Proposed branch: lp:~justizin/zope.copypastemove/lp_98385
Merge into: lp:zope.copypastemove
Diff against target: 143 lines (+68/-4)
3 files modified
setup.py (+1/-0)
src/zope/copypastemove/__init__.py (+8/-4)
src/zope/copypastemove/tests/test_rename.py (+59/-0)
To merge this branch: bzr merge lp:~justizin/zope.copypastemove/lp_98385
Reviewer Review Type Date Requested Status
Tres Seaver Pending
Review via email: mp+25657@code.launchpad.net

Description of the change

To post a comment you must log in.
Tres Seaver (tseaver) wrote :

I will merge with a couple of tweaks:

- We really prefer not to add any dependencies, even testing dependencies, on
  the 'zope.app' packages from outside that namespace. As it turns out, the
  'zope.container' package provides all these features anywa, which allows me
  to use the new test with a trivial tweak.

- The 'renameItem' methods should *all* return the newName: I'm updating the
  one for ordered containers to do so.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'setup.py'
2--- setup.py 2010-05-05 16:58:38 +0000
3+++ setup.py 2010-05-20 02:19:23 +0000
4@@ -47,6 +47,7 @@
5 test=['zope.principalannotation',
6 'zope.testing',
7 'zope.traversing',
8+ 'zope.app.container',
9 ]),
10 install_requires=['setuptools',
11 'zope.annotation',
12
13=== modified file 'src/zope/copypastemove/__init__.py'
14--- src/zope/copypastemove/__init__.py 2010-05-19 10:48:49 +0000
15+++ src/zope/copypastemove/__init__.py 2010-05-20 02:19:23 +0000
16@@ -445,6 +445,7 @@
17 to 'bar':
18
19 >>> renamer.renameItem('foo', 'bar')
20+ u'bar'
21 >>> container['foo'] is foo
22 Traceback (most recent call last):
23 KeyError: 'foo'
24@@ -473,6 +474,9 @@
25 self.container = container
26
27 def renameItem(self, oldName, newName):
28+ return self._renameItem(oldName, newName)
29+
30+ def _renameItem(self, oldName, newName):
31 object = self.container.get(oldName)
32 if object is None:
33 raise ItemNotFoundError(self.container, oldName)
34@@ -481,7 +485,7 @@
35 if newName in self.container:
36 raise DuplicationError("%s is already in use" % newName)
37
38- mover.moveTo(self.container, newName)
39+ return mover.moveTo(self.container, newName)
40
41
42 class OrderedContainerItemRenamer(ContainerItemRenamer):
43@@ -518,14 +522,14 @@
44 the order is preserved:
45
46 >>> container.items()
47- [('I', 'Item 1'), ('2', 'Item 2'), ('3', 'Item 3')]
48+ [(u'I', 'Item 1'), ('2', 'Item 2'), ('3', 'Item 3')]
49
50 Renaming the other two items also preserves the origina order:
51
52 >>> renamer.renameItem('2', 'II')
53 >>> renamer.renameItem('3', 'III')
54 >>> container.items()
55- [('I', 'Item 1'), ('II', 'Item 2'), ('III', 'Item 3')]
56+ [(u'I', 'Item 1'), (u'II', 'Item 2'), (u'III', 'Item 3')]
57
58 As with the standard renamer, trying to rename a non-existent item raises
59 an error:
60@@ -548,7 +552,7 @@
61
62 def renameItem(self, oldName, newName):
63 order = list(self.container.keys())
64- super(OrderedContainerItemRenamer, self).renameItem(oldName, newName)
65+ newName = self._renameItem(oldName, newName)
66 order[order.index(oldName)] = newName
67 self.container.updateOrder(order)
68
69
70=== modified file 'src/zope/copypastemove/tests/test_rename.py'
71--- src/zope/copypastemove/tests/test_rename.py 2010-05-19 10:48:49 +0000
72+++ src/zope/copypastemove/tests/test_rename.py 2010-05-20 02:19:23 +0000
73@@ -57,11 +57,70 @@
74 eventtesting.setUp()
75 container_setup.setUp()
76
77+
78+def doctest_namechooser_rename_preserve_order():
79+ """Test for OrderedContainerItemRenamer.renameItem
80+
81+ This is a regression test for
82+ http://www.zope.org/Collectors/Zope3-dev/658
83+
84+ Also: https://bugs.launchpad.net/zope.copypastemove/+bug/98385
85+
86+ >>> from zope.component import adapts, provideAdapter
87+ >>> from zope.copypastemove import ObjectMover
88+ >>> provideAdapter(ObjectMover)
89+
90+ There's an ordered container
91+
92+ >>> from zope.app.container.ordered import OrderedContainer
93+ >>> container = OrderedContainer()
94+
95+ >>> from zope.app.container.contained import Contained
96+ >>> class Obj(Contained):
97+ ... def __init__(self, title):
98+ ... self.title = title
99+ ... def __repr__(self):
100+ ... return self.title
101+ >>> container['foo'] = Obj('Foo')
102+ >>> container['bar'] = Obj('Bar')
103+ >>> container['baz'] = Obj('Baz')
104+
105+ with a custom name chooser
106+
107+ >>> from zope.interface import implements, Interface
108+ >>> from zope.app.container.interfaces import INameChooser
109+ >>> class IMyContainer(Interface): pass
110+ >>> class MyNameChooser(object):
111+ ... adapts(IMyContainer)
112+ ... implements(INameChooser)
113+ ... def __init__(self, container):
114+ ... self.container = container
115+ ... def chooseName(self, name, obj):
116+ ... return name.encode('rot-13')
117+ >>> provideAdapter(MyNameChooser)
118+
119+ >>> from zope.interface import alsoProvides
120+ >>> alsoProvides(container, IMyContainer)
121+
122+ OrderedContainerItemRenamer renames and preserves the order of items
123+
124+ >>> from zope.copypastemove import OrderedContainerItemRenamer
125+ >>> renamer = OrderedContainerItemRenamer(container)
126+ >>> renamer.renameItem('bar', 'quux')
127+
128+ >>> list(container.keys())
129+ ['foo', 'dhhk', 'baz']
130+ >>> list(container.values())
131+ [Foo, Bar, Baz]
132+
133+ """
134+
135 def test_suite():
136 return unittest.TestSuite((
137 unittest.makeSuite(RenamerTest),
138 DocTestSuite('zope.copypastemove',
139 setUp=setUp, tearDown=testing.tearDown),
140+ DocTestSuite(setUp=setUp, tearDown=testing.tearDown),
141 ))
142
143 if __name__=='__main__':

Subscribers

People subscribed via source and target branches

to all changes: