File: test_treeutil.py

package info (click to toggle)
python-asdf 2.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,248 kB
  • sloc: python: 13,104; makefile: 125
file content (50 lines) | stat: -rw-r--r-- 1,513 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from asdf import treeutil


def test_get_children():
    parent = ["foo", "bar"]
    assert treeutil.get_children(parent) == [(0, "foo"), (1, "bar")]

    parent = ("foo", "bar")
    assert treeutil.get_children(parent) == [(0, "foo"), (1, "bar")]

    parent = {"foo": "bar", "ding": "dong"}
    assert sorted(treeutil.get_children(parent)) == sorted([("foo", "bar"), ("ding", "dong")])

    parent = "foo"
    assert treeutil.get_children(parent) == []

    parent = None
    assert treeutil.get_children(parent) == []


def test_is_container():
    for value in [[], {}, tuple()]:
        assert treeutil.is_container(value) is True

    for value in ["foo", 12, 13.9827]:
        assert treeutil.is_container(value) is False


def test_walk_and_modify_shared_references():
    target = {"foo": "bar"}
    nested_in_dict = {"target": target}
    nested_in_list = [target]
    tree = {"target": target, "nested_in_dict": nested_in_dict, "nested_in_list": nested_in_list}

    assert tree["target"] is tree["nested_in_dict"]["target"]
    assert tree["target"] is tree["nested_in_list"][0]

    def _callback(node):
        if "foo" in node:
            return {"foo": "baz"}
        else:
            return node

    result = treeutil.walk_and_modify(tree, _callback)

    assert result is not tree
    assert result["target"] is not target
    assert result["target"]["foo"] == "baz"
    assert result["target"] is result["nested_in_dict"]["target"]
    assert result["target"] is result["nested_in_list"][0]