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