1 """
2 unit tests for module logilab.common.tree
3 squeleton generated by /home/syt/bin/py2tests on Jan 20 at 10:43:25
4 """
5 __revision__ = "$Id: unittest_tree.py,v 1.9 2005-09-07 23:44:02 nico Exp $"
6
7 from logilab.common.testlib import TestCase, unittest_main
8 from logilab.common.tree import *
9
10 tree = ('root', (
11 ('child_1_1', (
12 ('child_2_1', ()), ('child_2_2', (
13 ('child_3_1', ()),
14 )))),
15 ('child_1_2', (('child_2_3', ()),))))
16
22
24 """ a basic tree node, caracterised by an id"""
26 """ called before each test from this class """
27 self.o = make_tree(tree)
28
29
31 result = [r.id for r in self.o.flatten()]
32 expected = ['root', 'child_1_1', 'child_2_1', 'child_2_2', 'child_3_1', 'child_1_2', 'child_2_3']
33 self.assertListEqual(result, expected)
34
36 resultnodes = []
37 self.o.flatten(resultnodes)
38 result = [r.id for r in resultnodes]
39 expected = ['root', 'child_1_1', 'child_2_1', 'child_2_2', 'child_3_1', 'child_1_2', 'child_2_3']
40 self.assertListEqual(result, expected)
41
42
49
57
59 """
60 return the sibling node that has given id
61 """
62 self.assertEqual(self.o.children[0].get_sibling('child_1_2'), self.o.children[1], None)
63
66
68 """
69 return node in whole hierarchy that has given id
70 """
71 self.assertEqual(self.o.get_node_by_id('child_1_1'), self.o.children[0])
72
75
77 """
78 return child of given id
79 """
80 self.assertEqual(self.o.get_child_by_id('child_2_1', recurse=1), self.o.children[0].children[0])
81
85
87 """
88 return child of given path (path is a list of ids)
89 """
90 self.assertEqual(self.o.get_child_by_path(['root', 'child_1_1', 'child_2_1']), self.o.children[0].children[0])
91
94
96 """
97 return depth of this node in the tree
98 """
99 self.assertEqual(self.o.depth_down(), 4)
100 self.assertEqual(self.o.get_child_by_id('child_2_1',True).depth_down(), 1)
101
103 """
104 return depth of this node in the tree
105 """
106 self.assertEqual(self.o.depth(), 0)
107 self.assertEqual(self.o.get_child_by_id('child_2_1',True).depth(), 2)
108
110 """
111 return depth of this node in the tree
112 """
113 self.assertEqual(self.o.width(), 3)
114 self.assertEqual(self.o.get_child_by_id('child_2_1',True).width(), 1)
115
117 """
118 return the root node of the tree
119 """
120 self.assertEqual(self.o.get_child_by_id('child_2_1', True).root(), self.o)
121
123 """
124 return a list with all the leaf nodes descendant from this task
125 """
126 self.assertEqual(self.o.leaves(), [self.o.get_child_by_id('child_2_1',True),
127 self.o.get_child_by_id('child_3_1',True),
128 self.o.get_child_by_id('child_2_3',True)])
129
136
137
139 """"""
141 """ called before each test from this class """
142 self.o = make_tree(tree)
143
145 """
146 create a list with tree nodes for which the <filter> function returned true
147 in a post order foashion
148 """
149 L = ['child_2_1', 'child_3_1', 'child_2_2', 'child_1_1', 'child_2_3', 'child_1_2', 'root']
150 l = [n.id for n in post_order_list(self.o)]
151 self.assertEqual(l, L, l)
152
154 """
155 create a list with tree nodes for which the <filter> function returned true
156 in a post order foashion
157 """
158 def filter(node):
159 if node.id == 'child_2_2':
160 return 0
161 return 1
162 L = ['child_2_1', 'child_1_1', 'child_2_3', 'child_1_2', 'root']
163 l = [n.id for n in post_order_list(self.o, filter)]
164 self.assertEqual(l, L, l)
165
166
168 """"""
170 """ called before each test from this class """
171 self.o = make_tree(tree)
172
174 L = ['child_2_1', 'child_3_1', 'child_2_2', 'child_1_1', 'child_2_3', 'child_1_2', 'root']
175 iter = PostfixedDepthFirstIterator(self.o)
176 o = iter.next()
177 i = 0
178 while o:
179 self.assertEqual(o.id, L[i])
180 o = iter.next()
181 i += 1
182
183
185 """"""
187 """ called before each test from this class """
188 self.o = make_tree(tree)
189
191 """
192 create a list with tree nodes for which the <filter> function returned true
193 in a pre order fashion
194 """
195 L = ['root', 'child_1_1', 'child_2_1', 'child_2_2', 'child_3_1', 'child_1_2', 'child_2_3']
196 l = [n.id for n in pre_order_list(self.o)]
197 self.assertEqual(l, L, l)
198
200 """
201 create a list with tree nodes for which the <filter> function returned true
202 in a pre order fashion
203 """
204 def filter(node):
205 if node.id == 'child_2_2':
206 return 0
207 return 1
208 L = ['root', 'child_1_1', 'child_2_1', 'child_1_2', 'child_2_3']
209 l = [n.id for n in pre_order_list(self.o, filter)]
210 self.assertEqual(l, L, l)
211
212
214 """"""
216 """ called before each test from this class """
217 self.o = make_tree(tree)
218
220 L = ['root', 'child_1_1', 'child_2_1', 'child_2_2', 'child_3_1', 'child_1_2', 'child_2_3']
221 iter = PrefixedDepthFirstIterator(self.o)
222 o = iter.next()
223 i = 0
224 while o:
225 self.assertEqual(o.id, L[i])
226 o = iter.next()
227 i += 1
228
229
230 if __name__ == '__main__':
231 unittest_main()
232