Permalink
Browse files

Fixed get_node_from_children for non-first match

--HG--
branch : sneakylang
  • Loading branch information...
1 parent cf64808 commit 568067fc01461dfe5d43f62a253c6a1aa2181f1f almad committed Mar 24, 2009
Showing with 39 additions and 8 deletions.
  1. +27 −5 sneakylang/test/test_treebuilder.py
  2. +12 −3 sneakylang/treebuilder.py
@@ -176,12 +176,34 @@ def setUp(self):
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):
View
@@ -74,16 +74,25 @@ def get_actual_node(self):
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)

0 comments on commit 568067f

Please sign in to comment.