[Python-checkins] r54810 - in sandbox/trunk/2to3: pytree.py tests/test_pytree.py
collin.winter
python-checkins at python.org
Sat Apr 14 00:03:38 CEST 2007
Author: collin.winter
Date: Sat Apr 14 00:03:36 2007
New Revision: 54810
Modified:
sandbox/trunk/2to3/pytree.py
sandbox/trunk/2to3/tests/test_pytree.py
Log:
Add a get_next_sibling() method to pytree.Base.
Modified: sandbox/trunk/2to3/pytree.py
==============================================================================
--- sandbox/trunk/2to3/pytree.py (original)
+++ sandbox/trunk/2to3/pytree.py Sat Apr 14 00:03:36 2007
@@ -142,6 +142,21 @@
del self.parent.children[i]
self.parent = None
return i
+
+ def get_next_sibling(self):
+ """Return the node immediately following the invocant in their
+ parent's children list. If the invocant does not have a next
+ sibling, return None."""
+ if self.parent is None:
+ return None
+
+ # Can't use index(); we need to test by identity
+ for i, sibling in enumerate(self.parent.children):
+ if sibling is self:
+ try:
+ return self.parent.children[i+1]
+ except IndexError:
+ return None
class Node(Base):
Modified: sandbox/trunk/2to3/tests/test_pytree.py
==============================================================================
--- sandbox/trunk/2to3/tests/test_pytree.py (original)
+++ sandbox/trunk/2to3/tests/test_pytree.py Sat Apr 14 00:03:36 2007
@@ -262,6 +262,24 @@
# I don't care what it raises, so long as it's an exception
self.assertRaises(Exception, n1.append_child, list)
+
+ def testNodeNextSibling(self):
+ n1 = pytree.Node(1000, [])
+ n2 = pytree.Node(1000, [])
+ p1 = pytree.Node(1000, [n1, n2])
+
+ self.failUnless(n1.get_next_sibling() is n2)
+ self.assertEqual(n2.get_next_sibling(), None)
+ self.assertEqual(p1.get_next_sibling(), None)
+
+ def testLeafNextSibling(self):
+ l1 = pytree.Leaf(100, "a")
+ l2 = pytree.Leaf(100, "b")
+ p1 = pytree.Node(1000, [l1, l2])
+
+ self.failUnless(l1.get_next_sibling() is l2)
+ self.assertEqual(l2.get_next_sibling(), None)
+ self.assertEqual(p1.get_next_sibling(), None)
class TestPatterns(support.TestCase):
More information about the Python-checkins
mailing list