| 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 | |
|---|
| 17 | def make_tree(tuple): |
|---|
| 18 | n = Node(tuple[0]) |
|---|
| 19 | for child in tuple[1]: |
|---|
| 20 | n.append(make_tree(child)) |
|---|
| 21 | return n |
|---|
| 22 | |
|---|
| 23 | class Node_ClassTest(TestCase): |
|---|
| 24 | """ a basic tree node, caracterised by an id""" |
|---|
| 25 | def setUp(self): |
|---|
| 26 | """ called before each test from this class """ |
|---|
| 27 | self.o = make_tree(tree) |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | def test_flatten(self): |
|---|
| 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 | |
|---|
| 35 | def test_flatten_with_outlist(self): |
|---|
| 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 | |
|---|
| 43 | def test_known_values_remove(self): |
|---|
| 44 | """ |
|---|
| 45 | remove a child node |
|---|
| 46 | """ |
|---|
| 47 | self.o.remove(self.o.get_node_by_id('child_1_1')) |
|---|
| 48 | self.assertRaises(NodeNotFound, self.o.get_node_by_id, 'child_1_1') |
|---|
| 49 | |
|---|
| 50 | def test_known_values_replace(self): |
|---|
| 51 | """ |
|---|
| 52 | replace a child node with another |
|---|
| 53 | """ |
|---|
| 54 | self.o.replace(self.o.get_node_by_id('child_1_1'), Node('hoho')) |
|---|
| 55 | self.assertRaises(NodeNotFound, self.o.get_node_by_id, 'child_1_1') |
|---|
| 56 | self.assertEqual(self.o.get_node_by_id('hoho'), self.o.children[0]) |
|---|
| 57 | |
|---|
| 58 | def test_known_values_get_sibling(self): |
|---|
| 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 | |
|---|
| 64 | def test_raise_get_sibling_NodeNotFound(self): |
|---|
| 65 | self.assertRaises(NodeNotFound, self.o.children[0].get_sibling, 'houhou') |
|---|
| 66 | |
|---|
| 67 | def test_known_values_get_node_by_id(self): |
|---|
| 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 | |
|---|
| 73 | def test_raise_get_node_by_id_NodeNotFound(self): |
|---|
| 74 | self.assertRaises(NodeNotFound, self.o.get_node_by_id, 'houhou') |
|---|
| 75 | |
|---|
| 76 | def test_known_values_get_child_by_id(self): |
|---|
| 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 | |
|---|
| 82 | def test_raise_get_child_by_id_NodeNotFound(self): |
|---|
| 83 | self.assertRaises(NodeNotFound, self.o.get_child_by_id, nid='child_2_1') |
|---|
| 84 | self.assertRaises(NodeNotFound, self.o.get_child_by_id, 'houhou') |
|---|
| 85 | |
|---|
| 86 | def test_known_values_get_child_by_path(self): |
|---|
| 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 | |
|---|
| 92 | def test_raise_get_child_by_path_NodeNotFound(self): |
|---|
| 93 | self.assertRaises(NodeNotFound, self.o.get_child_by_path, ['child_1_1', 'child_2_11']) |
|---|
| 94 | |
|---|
| 95 | def test_known_values_depth_down(self): |
|---|
| 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 | |
|---|
| 102 | def test_known_values_depth(self): |
|---|
| 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 | |
|---|
| 109 | def test_known_values_width(self): |
|---|
| 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 | |
|---|
| 116 | def test_known_values_root(self): |
|---|
| 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 | |
|---|
| 122 | def test_known_values_leaves(self): |
|---|
| 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 | |
|---|
| 130 | def test_known_values_lineage(self): |
|---|
| 131 | c31 = self.o.get_child_by_id('child_3_1',True) |
|---|
| 132 | self.assertEqual(c31.lineage(), [self.o.get_child_by_id('child_3_1',True), |
|---|
| 133 | self.o.get_child_by_id('child_2_2',True), |
|---|
| 134 | self.o.get_child_by_id('child_1_1',True), |
|---|
| 135 | self.o]) |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | class post_order_list_FunctionTest(TestCase): |
|---|
| 139 | """""" |
|---|
| 140 | def setUp(self): |
|---|
| 141 | """ called before each test from this class """ |
|---|
| 142 | self.o = make_tree(tree) |
|---|
| 143 | |
|---|
| 144 | def test_known_values_post_order_list(self): |
|---|
| 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 | |
|---|
| 153 | def test_known_values_post_order_list2(self): |
|---|
| 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 | |
|---|
| 167 | class PostfixedDepthFirstIterator_ClassTest(TestCase): |
|---|
| 168 | """""" |
|---|
| 169 | def setUp(self): |
|---|
| 170 | """ called before each test from this class """ |
|---|
| 171 | self.o = make_tree(tree) |
|---|
| 172 | |
|---|
| 173 | def test_known_values_next(self): |
|---|
| 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 | |
|---|
| 184 | class pre_order_list_FunctionTest(TestCase): |
|---|
| 185 | """""" |
|---|
| 186 | def setUp(self): |
|---|
| 187 | """ called before each test from this class """ |
|---|
| 188 | self.o = make_tree(tree) |
|---|
| 189 | |
|---|
| 190 | def test_known_values_pre_order_list(self): |
|---|
| 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 | |
|---|
| 199 | def test_known_values_pre_order_list2(self): |
|---|
| 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 | |
|---|
| 213 | class PrefixedDepthFirstIterator_ClassTest(TestCase): |
|---|
| 214 | """""" |
|---|
| 215 | def setUp(self): |
|---|
| 216 | """ called before each test from this class """ |
|---|
| 217 | self.o = make_tree(tree) |
|---|
| 218 | |
|---|
| 219 | def test_known_values_next(self): |
|---|
| 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() |
|---|