| 1 |
from pytest import fixture |
| 2 |
from test2ref import assert_refdata |
| 3 |
|
| 4 |
from anytree import Node |
| 5 |
from anytree.exporter import MermaidExporter |
| 6 |
|
| 7 |
|
| 8 |
@fixture |
| 9 |
def root(): |
| 10 |
root = Node("root") |
| 11 |
s0 = Node("sub0", parent=root, edge=2) |
| 12 |
Node("sub0B", parent=s0, foo=4, edge=109) |
| 13 |
Node("sub0A", parent=s0, edge="") |
| 14 |
s1 = Node("sub1", parent=root, edge="") |
| 15 |
Node("sub1A", parent=s1, edge=7) |
| 16 |
Node('sub1"B', parent=s1, edge=8) |
| 17 |
s1c = Node("su\\b1C", parent=s1, edge=22) |
| 18 |
Node("sub1Ca", parent=s1c, edge=42) |
| 19 |
yield root |
| 20 |
|
| 21 |
|
| 22 |
def test_tree(tmp_path, root): |
| 23 |
"""Tree.""" |
| 24 |
MermaidExporter(root).to_file(tmp_path / "tree.md") |
| 25 |
assert_refdata(test_tree, tmp_path) |
| 26 |
|
| 27 |
|
| 28 |
def test_tree_custom(tmp_path, root): |
| 29 |
"""Tree Custom.""" |
| 30 |
|
| 31 |
def nodefunc(node): |
| 32 |
return f'("{node.name}")' |
| 33 |
|
| 34 |
def edgefunc(node, child): |
| 35 |
return f"--{child.edge}-->" |
| 36 |
|
| 37 |
options = [ |
| 38 |
"%% just an example comment", |
| 39 |
"%% could be an option too", |
| 40 |
] |
| 41 |
|
| 42 |
MermaidExporter( |
| 43 |
root, |
| 44 |
options=options, |
| 45 |
nodefunc=nodefunc, |
| 46 |
edgefunc=edgefunc, |
| 47 |
).to_file(tmp_path / "tree_custom.md") |
| 48 |
assert_refdata(test_tree_custom, tmp_path) |
| 49 |
|
| 50 |
|
| 51 |
def test_tree_filter(tmp_path, root): |
| 52 |
"""Tree with Filter.""" |
| 53 |
MermaidExporter(root, filter_=lambda node: node.name.startswith("sub")).to_file(tmp_path / "tree_filter.md") |
| 54 |
assert_refdata(test_tree_filter, tmp_path) |
| 55 |
|
| 56 |
|
| 57 |
def test_tree_stop(tmp_path, root): |
| 58 |
"""Tree with stop.""" |
| 59 |
MermaidExporter(root, stop=lambda node: node.name == "sub1").to_file(tmp_path / "tree_stop.md") |
| 60 |
assert_refdata(test_tree_stop, tmp_path) |
| 61 |
|
| 62 |
|
| 63 |
def test_tree_maxlevel(tmp_path, root): |
| 64 |
"""Tree with maxlevel.""" |
| 65 |
MermaidExporter(root, maxlevel=2).to_file(tmp_path / "tree_maxlevel.md") |
| 66 |
assert_refdata(test_tree_maxlevel, tmp_path) |