Commits

almad  committed 99ede2a

Fixed get_node_from_children for non-first match

  • Participants
  • Parent commits 2d33370

Comments (0)

Files changed (2)

File sneakylang/test/test_treebuilder.py

         self.root = DummyNode()
         self.builder.set_root(self.root)
     
-#    def testSingleChildSearch(self):
-#        n1 = DummyNode()
-#        self.root.add_child(n1)
-#        
-#        self.assertEquals(n1, self.builder.get_node_from_children(n1))
+    def testSingleChildSearch(self):
+        n1 = DummyNode()
+        self.root.add_child(n1)
         
+        self.assertEquals(n1, self.builder.get_node_from_children(n1, self.root.children))
+        
+    def testMultipleChildSearch(self):
+        n1 = DummyNode()
+        n2 = DummyNode()
+        self.root.add_child(n1)
+        self.root.add_child(n2)
+        
+        self.assertEquals(n2, self.builder.get_node_from_children(n2, self.root.children))
+
+    def testMultipleNestedChildSearch(self):
+        n1 = DummyNode()
+        n2 = DummyNode()
+        self.root.add_child(n1)
+        self.root.add_child(n2)
+        
+        n1_1 = DummyNode()
+        n2_1 = DummyNode()
+        
+        n1.add_child(n1_1)
+        n2.add_child(n2_1)
+        
+        self.assertEquals(n2_1, self.builder.get_node_from_children(n2_1, self.root.children))
+
 
 class TestBuilderCalledByMacro(TestCase):
     def setUp(self):

File sneakylang/treebuilder.py

 
     def get_node_from_children(self, node, node_list):
         if node in node_list:
-            return node
+            return node_list[node_list.index(node)]
         else:
             for n in node_list:
-                return self.get_node_from_children(node, n.children)
+                found_node = self.get_node_from_children(node, n.children)
+                if found_node:
+                    return found_node
 
     def set_actual_node(self, node):
         """ Traverse through node tree, find given node instance and set it as active node.
         If node is not found, raise ValueError """
-        if self.root is node or (self.root is not None and self.get_node_from_children(node, self.root.children) is node):
+        if self.root is node:
             self._actual_node = node
+        elif self.root is not None:
+            returned_node = self.get_node_from_children(node, self.root.children)
+            if node is returned_node:
+                self._actual_node = node
+            else:
+                raise ValueError(u"Bad node %s returned" % returned_node)
+            
         else:
             raise ValueError('Node %s not found in tree' % node)