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

collin.winter python-checkins at python.org
Wed Apr 11 17:57:49 CEST 2007


Author: collin.winter
Date: Wed Apr 11 17:57:45 2007
New Revision: 54752

Modified:
   sandbox/trunk/2to3/pytree.py
   sandbox/trunk/2to3/tests/test_pytree.py
Log:
Add append_child(), insert_child() and set_child() methods to pytree.Node to simplify child-manipulating code.

Modified: sandbox/trunk/2to3/pytree.py
==============================================================================
--- sandbox/trunk/2to3/pytree.py	(original)
+++ sandbox/trunk/2to3/pytree.py	Wed Apr 11 17:57:45 2007
@@ -209,6 +209,25 @@
         if not self.children:
             return ""
         return self.children[0].get_prefix()
+        
+    def set_child(self, i, child):
+        """Equivalent to 'node.children[i] = child'. This method also sets the
+        child's parent attribute appropriately."""
+        child.parent = self
+        self.children[i].parent = None
+        self.children[i] = child
+        
+    def insert_child(self, i, child):
+        """Equivalent to 'node.children.insert(i, child)'. This method also
+        sets the child's parent attribute appropriately."""
+        child.parent = self
+        self.children.insert(i, child)
+        
+    def append_child(self, child):
+        """Equivalent to 'node.children.append(child)'. This method also
+        sets the child's parent attribute appropriately."""
+        child.parent = self
+        self.children.append(child)
 
 
 class Leaf(Base):

Modified: sandbox/trunk/2to3/tests/test_pytree.py
==============================================================================
--- sandbox/trunk/2to3/tests/test_pytree.py	(original)
+++ sandbox/trunk/2to3/tests/test_pytree.py	Wed Apr 11 17:57:45 2007
@@ -209,6 +209,59 @@
         l1 = pytree.Leaf(100, "foo")
         l1.remove()
         self.assertEqual(l1.parent, None)
+        
+    def testNodeSetChild(self):
+        l1 = pytree.Leaf(100, "foo")
+        n1 = pytree.Node(1000, [l1])
+        
+        l2 = pytree.Leaf(100, "bar")
+        n1.set_child(0, l2)
+        self.assertEqual(l1.parent, None)
+        self.assertEqual(l2.parent, n1)
+        self.assertEqual(n1.children, [l2])
+        
+        n2 = pytree.Node(1000, [l1])
+        n2.set_child(0, n1)
+        self.assertEqual(l1.parent, None)
+        self.assertEqual(n1.parent, n2)
+        self.assertEqual(n2.parent, None)
+        self.assertEqual(n2.children, [n1])
+        
+        self.assertRaises(IndexError, n1.set_child, 4, l2)
+        # I don't care what it raises, so long as it's an exception
+        self.assertRaises(Exception, n1.set_child, 0, list)
+        
+    def testNodeInsertChild(self):
+        l1 = pytree.Leaf(100, "foo")
+        n1 = pytree.Node(1000, [l1])
+        
+        l2 = pytree.Leaf(100, "bar")
+        n1.insert_child(0, l2)
+        self.assertEqual(l2.parent, n1)
+        self.assertEqual(n1.children, [l2, l1])
+        
+        l3 = pytree.Leaf(100, "abc")
+        n1.insert_child(2, l3)
+        self.assertEqual(n1.children, [l2, l1, l3])
+        
+        # I don't care what it raises, so long as it's an exception
+        self.assertRaises(Exception, n1.insert_child, 0, list)
+        
+    def testNodeAppendChild(self):
+        n1 = pytree.Node(1000, [])
+        
+        l1 = pytree.Leaf(100, "foo")
+        n1.append_child(l1)
+        self.assertEqual(l1.parent, n1)
+        self.assertEqual(n1.children, [l1])
+        
+        l2 = pytree.Leaf(100, "bar")
+        n1.append_child(l2)
+        self.assertEqual(l2.parent, n1)
+        self.assertEqual(n1.children, [l1, l2])
+        
+        # I don't care what it raises, so long as it's an exception
+        self.assertRaises(Exception, n1.append_child, list)
 
 
 class TestPatterns(support.TestCase):


More information about the Python-checkins mailing list