| 1 |
from enum import IntEnum |
| 2 |
|
| 3 |
from anytree import CountError, Node, find, find_by_attr, findall, findall_by_attr |
| 4 |
|
| 5 |
from .helper import assert_raises, eq_ |
| 6 |
|
| 7 |
|
| 8 |
def test_findall(): |
| 9 |
f = Node("f") |
| 10 |
b = Node("b", parent=f) |
| 11 |
a = Node("a", parent=b) |
| 12 |
d = Node("d", parent=b) |
| 13 |
c = Node("c", parent=d) |
| 14 |
e = Node("e", parent=d) |
| 15 |
|
| 16 |
eq_(findall(f, filter_=lambda node: node.name in ("a", "b")), (b, a)) |
| 17 |
eq_(findall(f, filter_=lambda node: d in node.path), (d, c, e)) |
| 18 |
with assert_raises( |
| 19 |
CountError, |
| 20 |
("Expecting at least 4 elements, but found 3. (Node('/f/b/d'), Node('/f/b/d/c'), Node('/f/b/d/e'))"), |
| 21 |
): |
| 22 |
findall(f, filter_=lambda node: d in node.path, mincount=4) |
| 23 |
with assert_raises( |
| 24 |
CountError, |
| 25 |
("Expecting 2 elements at maximum, but found 3. (Node('/f/b/d'), Node('/f/b/d/c'), Node('/f/b/d/e'))"), |
| 26 |
): |
| 27 |
findall(f, filter_=lambda node: d in node.path, maxcount=2) |
| 28 |
|
| 29 |
|
| 30 |
def test_findall_by_attr(): |
| 31 |
f = Node("f") |
| 32 |
b = Node("b", parent=f) |
| 33 |
Node("a", parent=b) |
| 34 |
d = Node("d", parent=b) |
| 35 |
Node("c", parent=d) |
| 36 |
Node("e", parent=d) |
| 37 |
|
| 38 |
eq_(findall_by_attr(f, "d"), (d,)) |
| 39 |
with assert_raises(CountError, ("Expecting at least 1 elements, but found 0.")): |
| 40 |
findall_by_attr(f, "z", mincount=1) |
| 41 |
|
| 42 |
|
| 43 |
def test_find(): |
| 44 |
f = Node("f") |
| 45 |
b = Node("b", parent=f) |
| 46 |
Node("a", parent=b) |
| 47 |
d = Node("d", parent=b) |
| 48 |
Node("c", parent=d) |
| 49 |
Node("e", parent=d) |
| 50 |
g = Node("g", parent=f) |
| 51 |
i = Node("i", parent=g) |
| 52 |
Node("h", parent=i) |
| 53 |
|
| 54 |
eq_(find(f, lambda n: n.name == "d"), d) |
| 55 |
eq_(find(f, lambda n: n.name == "z"), None) |
| 56 |
with assert_raises( |
| 57 |
CountError, |
| 58 |
( |
| 59 |
"Expecting 1 elements at maximum, but found 5. " |
| 60 |
"(Node('/f/b'), Node('/f/b/a'), Node('/f/b/d'), Node('/f/b/d/c'), Node('/f/b/d/e'))" |
| 61 |
), |
| 62 |
): |
| 63 |
find(f, lambda n: b in n.path) |
| 64 |
|
| 65 |
|
| 66 |
def test_find_by_attr(): |
| 67 |
f = Node("f") |
| 68 |
b = Node("b", parent=f) |
| 69 |
Node("a", parent=b) |
| 70 |
d = Node("d", parent=b) |
| 71 |
c = Node("c", parent=d, foo=4) |
| 72 |
Node("e", parent=d) |
| 73 |
g = Node("g", parent=f) |
| 74 |
i = Node("i", parent=g) |
| 75 |
Node("h", parent=i) |
| 76 |
|
| 77 |
eq_(find_by_attr(f, "d"), d) |
| 78 |
eq_(find_by_attr(f, name="foo", value=4), c) |
| 79 |
eq_(find_by_attr(f, name="foo", value=8), None) |
| 80 |
|
| 81 |
|
| 82 |
def test_enum(): |
| 83 |
class Animals(IntEnum): |
| 84 |
Mammal = 1 |
| 85 |
Cat = 2 |
| 86 |
Dog = 3 |
| 87 |
|
| 88 |
root = Node("ANIMAL") |
| 89 |
mammal = Node(Animals.Mammal, parent=root) |
| 90 |
cat = Node(Animals.Cat, parent=mammal) |
| 91 |
dog = Node(Animals.Dog, parent=mammal) |
| 92 |
|
| 93 |
eq_(findall(root), (root, mammal, cat, dog)) |
| 94 |
eq_(findall_by_attr(root, Animals.Cat), (cat,)) |