Debian Codegraph @

tests/test_node.py

raw
1 from anytree import AnyNode, LoopError, Node, NodeMixin, PostOrderIter, PreOrderIter, TreeError
2
3 from .helper import assert_raises
4
5
6 def test_node_parent_error():
7 """Node Parent Error."""
8 with assert_raises(TreeError, "Parent node 'parent' is not of type 'NodeMixin'."):
9 Node("root", "parent")
10
11
12 def test_parent_child():
13 """A tree parent and child attributes."""
14 root = Node("root")
15 s0 = Node("sub0", parent=root)
16 s0b = Node("sub0B", parent=s0)
17 s0a = Node("sub0A", parent=s0)
18 s1 = Node("sub1", parent=root)
19 s1a = Node("sub1A", parent=s1)
20 s1b = Node("sub1B", parent=s1)
21 s1c = Node("sub1C", parent=s1)
22 s1ca = Node("sub1Ca", parent=s1c)
23
24 assert root.parent is None
25 assert root.children == (s0, s1)
26 assert s0.parent == root
27 assert s0.children == (s0b, s0a)
28 assert s0b.parent == s0
29 assert s0b.children == ()
30 assert s0a.parent == s0
31 assert s0a.children == ()
32 assert s1.parent == root
33 assert s1.children == (s1a, s1b, s1c)
34 assert s1a.parent == s1
35 assert s1a.children == ()
36 assert s1b.parent == s1
37 assert s1b.children == ()
38 assert s1c.parent == s1
39 assert s1c.children == (s1ca,)
40 assert s1ca.parent == s1c
41 assert s1ca.children == ()
42
43 # change parent
44 s1ca.parent = s0
45
46 assert root.parent is None
47 assert root.children == (s0, s1)
48 assert s0.parent == root
49 assert s0.children == (s0b, s0a, s1ca)
50 assert s0b.parent == s0
51 assert s0b.children == ()
52 assert s0a.parent == s0
53 assert s0a.children == ()
54 assert s1.parent == root
55 assert s1.children == (s1a, s1b, s1c)
56 assert s1a.parent == s1
57 assert s1a.children == ()
58 assert s1b.parent == s1
59 assert s1b.children == ()
60 assert s1c.parent == s1
61 assert s1c.children == ()
62 assert s1ca.parent == s0
63 assert s1ca.children == ()
64
65 # break tree into two
66 s1.parent = None
67
68 assert root.parent is None
69 assert root.children == (s0,)
70 assert s0.parent == root
71 assert s0.children == (s0b, s0a, s1ca)
72 assert s0b.parent == s0
73 assert s0b.children == ()
74 assert s0a.parent == s0
75 assert s0a.children == ()
76 assert s1.parent is None
77 assert s1.children == (s1a, s1b, s1c)
78 assert s1a.parent == s1
79 assert s1a.children == ()
80 assert s1b.parent == s1
81 assert s1b.children == ()
82 assert s1c.parent == s1
83 assert s1c.children == ()
84 assert s1ca.parent == s0
85 assert s1ca.children == ()
86
87 # set to the same
88 s1b.parent = s1
89
90 assert root.parent is None
91 assert root.children == (s0,)
92 assert s0.parent == root
93 assert s0.children == (s0b, s0a, s1ca)
94 assert s0b.parent == s0
95 assert s0b.children == ()
96 assert s0a.parent == s0
97 assert s0a.children == ()
98 assert s1.parent is None
99 assert s1.children == (s1a, s1b, s1c)
100 assert s1a.parent == s1
101 assert s1a.children == ()
102 assert s1b.parent == s1
103 assert s1b.children == ()
104 assert s1c.parent == s1
105 assert s1c.children == ()
106 assert s1ca.parent == s0
107 assert s1ca.children == ()
108
109
110 def test_detach_children():
111 root = Node("root")
112 s0 = Node("sub0", parent=root)
113 s0b = Node("sub0B", parent=s0)
114 s0a = Node("sub0A", parent=s0)
115 s1 = Node("sub1", parent=root)
116 s1a = Node("sub1A", parent=s1)
117 s1b = Node("sub1B", parent=s1)
118 s1c = Node("sub1C", parent=s1)
119 s1ca = Node("sub1Ca", parent=s1c)
120
121 assert root.descendants == (s0, s0b, s0a, s1, s1a, s1b, s1c, s1ca)
122 del s0.children
123 assert root.descendants == (s0, s1, s1a, s1b, s1c, s1ca)
124 del s1.children
125 assert root.descendants == (s0, s1)
126
127
128 def test_children_setter():
129 root = Node("root")
130 s0 = Node("sub0")
131 s1 = Node("sub0A")
132 s0a = Node("sub0B")
133
134 root.children = [s0, s1]
135 s0.children = [s0a]
136 assert root.descendants == (s0, s0a, s1)
137
138 with assert_raises(LoopError, "Cannot set parent. Node('/root/sub0') cannot be parent of itself."):
139 s0.children = [s0]
140
141 # test whether tree is unchanged after LoopError
142 assert root.descendants == (s0, s0a, s1)
143
144 with assert_raises(LoopError, "Cannot set parent. Node('/root/sub0') is parent of Node('/root/sub0/sub0B')."):
145 s0a.children = [s0]
146
147 # test whether tree is unchanged after LoopError
148 assert root.descendants == (s0, s0a, s1)
149
150 root.children = [s0, s1]
151 s0.children = [s0a]
152 s0a.children = [s1]
153 assert root.descendants == (s0, s0a, s1)
154
155
156 def test_children_setter_large():
157 root = Node("root")
158 s0 = Node("sub0")
159 s0b = Node("sub0B")
160 s0a = Node("sub0A")
161 s1 = Node("sub1")
162 s1a = Node("sub1A")
163 s1b = Node("sub1B")
164 s1c = Node("sub1C")
165 s1ca = Node("sub1Ca")
166
167 root.children = [s0, s1]
168 assert root.descendants == (s0, s1)
169 s0.children = [s0a, s0b]
170 assert root.descendants == (s0, s0a, s0b, s1)
171 s1.children = [s1a, s1b, s1c]
172 assert root.descendants == (s0, s0a, s0b, s1, s1a, s1b, s1c)
173 with assert_raises(TypeError, "'Node' object is not iterable"):
174 s1.children = s1ca
175 assert root.descendants == (s0, s0a, s0b, s1, s1a, s1b, s1c)
176
177
178 def test_node_children_type():
179 root = Node("root")
180 with assert_raises(TreeError, "Cannot add non-node object 'string'. It is not a subclass of 'NodeMixin'."):
181 root.children = ["string"]
182
183
184 def test_node_children_multiple():
185 root = Node("root")
186 sub = Node("sub")
187 with assert_raises(TreeError, "Cannot add node Node('/sub') multiple times as child."):
188 root.children = [sub, sub]
189
190
191 def test_recursion_detection():
192 """Recursion detection."""
193 root = Node("root")
194 s0 = Node("sub0", parent=root)
195 Node("sub0B", parent=s0)
196 s0a = Node("sub0A", parent=s0)
197
198 # try recursion
199 assert root.parent is None
200 try:
201 root.parent = root
202 except LoopError as exc:
203 assert str(exc) == "Cannot set parent. Node('/root') cannot be parent of itself."
204 assert root.parent is None
205 else:
206 raise AssertionError
207
208 assert root.parent is None
209 try:
210 root.parent = s0a
211 except LoopError as exc:
212 assert str(exc) == ("Cannot set parent. Node('/root') is parent of Node('/root/sub0/sub0A').")
213 assert root.parent is None
214 else:
215 raise AssertionError
216
217 assert s0.parent is root
218 try:
219 s0.parent = s0a
220 except LoopError as exc:
221 assert str(exc) == ("Cannot set parent. Node('/root/sub0') is parent of Node('/root/sub0/sub0A').")
222 assert s0.parent is root
223 else:
224 raise AssertionError
225
226
227 def test_repr():
228 """Node representation."""
229 root = Node("root")
230 s0 = Node("sub0", parent=root)
231 s1 = Node("sub1", parent=root, foo=42, bar="c0fe")
232
233 assert repr(root) == "Node('/root')"
234 assert repr(s0) == "Node('/root/sub0')"
235 assert repr(s1) == "Node('/root/sub1', bar='c0fe', foo=42)"
236
237
238 def test_ancestors():
239 """Node.ancestors."""
240 root = Node("root")
241 s0 = Node("sub0", parent=root)
242 s0b = Node("sub0B", parent=s0)
243 s0a = Node("sub0A", parent=s0)
244 s1 = Node("sub1", parent=root)
245 s1c = Node("sub1C", parent=s1)
246 s1ca = Node("sub1Ca", parent=s1c)
247
248 assert root.ancestors == ()
249 assert s0.ancestors == (root,)
250 assert s0b.ancestors == (root, s0)
251 assert s0a.ancestors == (root, s0)
252 assert s1ca.ancestors == (root, s1, s1c)
253 # deprecated typo
254 assert s1ca.ancestors == (root, s1, s1c)
255
256
257 def test_node_children_init():
258 """Node With Children Attribute."""
259 root = Node("root", children=[Node("a", children=[Node("aa")]), Node("b")])
260 assert repr(root.descendants) == "(Node('/root/a'), Node('/root/a/aa'), Node('/root/b'))"
261
262
263 def test_anynode_children_init():
264 """Anynode With Children Attribute."""
265 root = AnyNode(foo="root", children=[AnyNode(foo="a", children=[AnyNode(foo="aa")]), AnyNode(foo="b")])
266 assert repr(root.descendants) == "(AnyNode(foo='a'), AnyNode(foo='aa'), AnyNode(foo='b'))"
267
268
269 def test_descendants():
270 """Node.descendants."""
271 root = Node("root")
272 s0 = Node("sub0", parent=root)
273 s0b = Node("sub0B", parent=s0)
274 s0a = Node("sub0A", parent=s0)
275 s1 = Node("sub1", parent=root)
276 s1c = Node("sub1C", parent=s1)
277 s1ca = Node("sub1Ca", parent=s1c)
278
279 assert root.descendants == (s0, s0b, s0a, s1, s1c, s1ca)
280 assert s1.descendants == (s1c, s1ca)
281 assert s1c.descendants == (s1ca,)
282 assert s1ca.descendants == ()
283
284
285 def test_root():
286 """Node.root."""
287 root = Node("root")
288 s0 = Node("sub0", parent=root)
289 s0b = Node("sub0B", parent=s0)
290 s0a = Node("sub0A", parent=s0)
291 s1 = Node("sub1", parent=root)
292 s1c = Node("sub1C", parent=s1)
293 s1ca = Node("sub1Ca", parent=s1c)
294
295 assert root.root == root
296 assert s0.root == root
297 assert s0b.root == root
298 assert s0a.root == root
299 assert s1.root == root
300 assert s1c.root == root
301 assert s1ca.root == root
302
303
304 def test_siblings():
305 """Node.siblings."""
306 root = Node("root")
307 s0 = Node("sub0", parent=root)
308 s0b = Node("sub0B", parent=s0)
309 s0a = Node("sub0A", parent=s0)
310 s1 = Node("sub1", parent=root)
311 s1c = Node("sub1C", parent=s1)
312 s1ca = Node("sub1Ca", parent=s1c)
313
314 assert root.siblings == ()
315 assert s0.siblings == (s1,)
316 assert s0b.siblings == (s0a,)
317 assert s0a.siblings == (s0b,)
318 assert s1.siblings == (s0,)
319 assert s1c.siblings == ()
320 assert s1ca.siblings == ()
321
322
323 def test_is_leaf():
324 """Node.is_leaf."""
325 root = Node("root")
326 s0 = Node("sub0", parent=root)
327 s0b = Node("sub0B", parent=s0)
328 s0a = Node("sub0A", parent=s0)
329 s1 = Node("sub1", parent=root)
330 s1c = Node("sub1C", parent=s1)
331 s1ca = Node("sub1Ca", parent=s1c)
332
333 assert root.is_leaf is False
334 assert s0.is_leaf is False
335 assert s0b.is_leaf is True
336 assert s0a.is_leaf is True
337 assert s1.is_leaf is False
338 assert s1c.is_leaf is False
339 assert s1ca.is_leaf is True
340
341
342 def test_leaves():
343 """Node.leaves."""
344 root = Node("root")
345 s0 = Node("sub0", parent=root)
346 s0b = Node("sub0B", parent=s0)
347 s0a = Node("sub0A", parent=s0)
348 s1 = Node("sub1", parent=root)
349 s1c = Node("sub1C", parent=s1)
350 s1ca = Node("sub1Ca", parent=s1c)
351
352 assert root.leaves == (s0b, s0a, s1ca)
353 assert s0.leaves == (s0b, s0a)
354 assert s0b.leaves == (s0b,)
355 assert s0a.leaves == (s0a,)
356 assert s1.leaves == (s1ca,)
357 assert s1c.leaves == (s1ca,)
358 assert s1ca.leaves == (s1ca,)
359
360
361 def test_is_root():
362 """Node.is_root."""
363 root = Node("root")
364 s0 = Node("sub0", parent=root)
365 s0b = Node("sub0B", parent=s0)
366 s0a = Node("sub0A", parent=s0)
367 s1 = Node("sub1", parent=root)
368 s1c = Node("sub1C", parent=s1)
369 s1ca = Node("sub1Ca", parent=s1c)
370
371 assert root.is_root is True
372 assert s0.is_root is False
373 assert s0b.is_root is False
374 assert s0a.is_root is False
375 assert s1.is_root is False
376 assert s1c.is_root is False
377 assert s1ca.is_root is False
378
379
380 def test_height():
381 """Node.height."""
382 root = Node("root")
383 s0 = Node("sub0", parent=root)
384 s0b = Node("sub0B", parent=s0)
385 s0a = Node("sub0A", parent=s0)
386 s1 = Node("sub1", parent=root)
387 s1c = Node("sub1C", parent=s1)
388 s1ca = Node("sub1Ca", parent=s1c)
389
390 assert root.height == 3
391 assert s0.height == 1
392 assert s0b.height == 0
393 assert s0a.height == 0
394 assert s1.height == 2
395 assert s1c.height == 1
396 assert s1ca.height == 0
397
398
399 def test_size():
400 """Node.size."""
401 root = Node("root")
402 s0 = Node("sub0", parent=root)
403 s0b = Node("sub0B", parent=s0)
404 s0a = Node("sub0A", parent=s0)
405 s1 = Node("sub1", parent=root)
406 s1c = Node("sub1C", parent=s1)
407 s1ca = Node("sub1Ca", parent=s1c)
408
409 assert root.size == 7
410 assert s0.size == 3
411 assert s0b.size == 1
412 assert s0a.size == 1
413 assert s1.size == 3
414 assert s1c.size == 2
415 assert s1ca.size == 1
416
417
418 def test_depth():
419 """Node.depth."""
420 root = Node("root")
421 s0 = Node("sub0", parent=root)
422 s0b = Node("sub0B", parent=s0)
423 s0a = Node("sub0A", parent=s0)
424 s1 = Node("sub1", parent=root)
425 s1c = Node("sub1C", parent=s1)
426 s1ca = Node("sub1Ca", parent=s1c)
427
428 assert root.depth == 0
429 assert s0.depth == 1
430 assert s0b.depth == 2
431 assert s0a.depth == 2
432 assert s1.depth == 1
433 assert s1c.depth == 2
434 assert s1ca.depth == 3
435
436
437 def test_parent():
438 """Parent attribute."""
439 foo = NodeMixin()
440 assert foo.parent is None
441
442
443 def test_pre_order_iter():
444 """Pre-Order Iterator."""
445 f = Node("f")
446 b = Node("b", parent=f)
447 a = Node("a", parent=b)
448 d = Node("d", parent=b)
449 c = Node("c", parent=d)
450 e = Node("e", parent=d)
451 g = Node("g", parent=f)
452 i = Node("i", parent=g)
453 h = Node("h", parent=i)
454
455 assert [node.name for node in PreOrderIter(f)] == ["f", "b", "a", "d", "c", "e", "g", "i", "h"]
456
457
458 def test_post_order_iter():
459 """Post-Order Iterator."""
460 f = Node("f")
461 b = Node("b", parent=f)
462 a = Node("a", parent=b)
463 d = Node("d", parent=b)
464 c = Node("c", parent=d)
465 e = Node("e", parent=d)
466 g = Node("g", parent=f)
467 i = Node("i", parent=g)
468 h = Node("h", parent=i)
469
470 assert [node.name for node in PostOrderIter(f)] == ["a", "c", "e", "d", "b", "h", "i", "g", "f"]
471
472
473 def test_anyname():
474 """Support any type as name."""
475 myroot = Node([1, 2, 3])
476 Node("/foo", parent=myroot)
477 assert str(myroot) == "Node('/[1, 2, 3]')"
478
479
480 def test_node_kwargs():
481 """Ticket #24."""
482
483 class MyNode(Node):
484 def __init__(self, name, parent=None, **kwargs):
485 super().__init__(name, parent, **kwargs)
486
487 def _post_attach(self, parent):
488 print(self.my_attribute)
489
490 node_a = MyNode("A")
491 node_b = MyNode("B", node_a, my_attribute=True)
492 assert repr(node_b) == "MyNode('/A/B', my_attribute=True)"
493
494
495 def test_hookups():
496 """Hookup attributes #29."""
497
498 class MyNode(Node):
499 def _pre_attach(self, parent):
500 assert str(self.parent) == "None"
501 assert self.children == ()
502 assert str(self.path) == "(MyNode('/B'),)"
503
504 def _post_attach(self, parent):
505 assert str(self.parent) == "MyNode('/A')"
506 assert self.children == ()
507 assert str(self.path) == "(MyNode('/A'), MyNode('/A/B'))"
508
509 def _pre_detach(self, parent):
510 assert str(self.parent) == "MyNode('/A')"
511 assert self.children == ()
512 assert str(self.path) == "(MyNode('/A'), MyNode('/A/B'))"
513
514 def _post_detach(self, parent):
515 assert str(self.parent) == "None"
516 assert self.children == ()
517 assert str(self.path) == "(MyNode('/B'),)"
518
519 node_a = MyNode("A")
520 node_b = MyNode("B", node_a) # attach B on A
521 node_b.parent = None # detach B from A
522
523
524 def test_any_node_parent_error():
525 """Any Node Parent Error."""
526 with assert_raises(TreeError, "Parent node 'r' is not of type 'NodeMixin'."):
527 AnyNode("r")
528
529
530 def test_any_node():
531 """Any Node."""
532 r = AnyNode()
533 a = AnyNode()
534 b = AnyNode(foo=4)
535 assert r.parent is None
536 assert a.parent is None
537 assert b.parent is None
538 a.parent = r
539 b.parent = r
540 assert r.children == (a, b)
541 assert repr(r) == "AnyNode()"
542 assert repr(a) == "AnyNode()"
543 assert repr(b) == "AnyNode(foo=4)"
544
545
546 def test_eq_overwrite():
547 """Node with overwritten __eq__."""
548
549 class EqOverwrittingNode(NodeMixin):
550 def __init__(self, a, b, parent=None):
551 super().__init__()
552 self.a = a
553 self.b = b
554 self.parent = parent
555
556 def __eq__(self, other):
557 if isinstance(other, EqOverwrittingNode):
558 return self.a == other.a and self.b == other.b
559 return NotImplemented
560
561 r = EqOverwrittingNode(0, 0)
562 a = EqOverwrittingNode(1, 0, parent=r)
563 b = EqOverwrittingNode(1, 0, parent=r)
564 assert a.parent is r
565 assert b.parent is r
566 assert a.a == 1
567 assert a.b == 0
568 assert b.a == 1
569 assert b.b == 0
570
571
572 def test_tuple():
573 """Tuple as parent."""
574 with assert_raises(TreeError, "Parent node (1, 0, 3) is not of type 'NodeMixin'."):
575 Node((0, 1, 2), parent=(1, 0, 3))
576
577
578 def test_tuple_as_children():
579 """Tuple as children."""
580 n = Node("foo")
581 with assert_raises(TreeError, "Cannot add non-node object (0, 1, 2). It is not a subclass of 'NodeMixin'."):
582 n.children = [(0, 1, 2)]