[Python-checkins] r54811 - in sandbox/trunk/2to3: pytree.py tests/test_pytree.py

collin.winter python-checkins at python.org
Sat Apr 14 00:05:53 CEST 2007


Author: collin.winter
Date: Sat Apr 14 00:05:51 2007
New Revision: 54811

Modified:
   sandbox/trunk/2to3/pytree.py
   sandbox/trunk/2to3/tests/test_pytree.py
Log:
Add a get_suffix() 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:05:51 2007
@@ -142,7 +142,7 @@
                     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
@@ -157,6 +157,14 @@
                     return self.parent.children[i+1]
                 except IndexError:
                     return None
+                    
+    def get_suffix(self):
+        """Return the string immediately following the invocant node. This
+        is effectively equivalent to node.get_next_sibling().get_prefix()"""
+        next_sib = self.get_next_sibling()
+        if next_sib is None:
+            return ""
+        return next_sib.get_prefix()
 
 
 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:05:51 2007
@@ -99,6 +99,22 @@
         self.assertEqual(n1.get_prefix(), " ")
         self.assertEqual(l1.get_prefix(), " ")
 
+    def testGetSuffix(self):
+        l1 = pytree.Leaf(100, "foo", prefix="a")
+        l2 = pytree.Leaf(100, "bar", prefix="b")
+        n1 = pytree.Node(1000, [l1, l2])
+        
+        self.assertEqual(l1.get_suffix(), l2.get_prefix())
+        self.assertEqual(l2.get_suffix(), "")
+        self.assertEqual(n1.get_suffix(), "")
+        
+        l3 = pytree.Leaf(100, "bar", prefix="c")
+        n2 = pytree.Node(1000, [n1, l3])
+
+        self.assertEqual(n1.get_suffix(), l3.get_prefix())
+        self.assertEqual(l3.get_suffix(), "")
+        self.assertEqual(n2.get_suffix(), "")
+
     def testNodeEq(self):
         n1 = pytree.Node(1000, ())
         n2 = pytree.Node(1000, [], context=(" ", (1, 0)))


More information about the Python-checkins mailing list