def test_simple_dict(self): n = Node.build({"foo": 123, "bar": 456, "baz": 789}) assert list(n.dfwalk()) == [ n, Node(".foo", int, 123, key="foo", parent=n), Node(".bar", int, 456, key="bar", parent=n), Node(".baz", int, 789, key="baz", parent=n), ]
def test_simple_list(self): n = Node.build(["foo", 123, True]) assert list(n.dfwalk()) == [ n, Node("[0]", str, "foo", parent=n), Node("[1]", int, 123, parent=n), Node("[2]", bool, True, parent=n), ]
def test_nested_dict(self): n = Node.build({"foo": {"a": 1, "b": 2}, "bar": [3, 4], "baz": {5, 6}}) foo = Node( ".foo", dict, {"a": 1, "b": 2}, parent=n, key="foo", children=[] ) foo_a = Node(".foo.a", int, 1, parent=foo, key="a") foo_b = Node(".foo.b", int, 2, parent=foo, key="b") foo.children.extend([foo_a, foo_b]) bar = Node(".bar", list, [3, 4], parent=n, key="bar", children=[]) bar_0 = Node(".bar[0]", int, 3, parent=bar) bar_1 = Node(".bar[1]", int, 4, parent=bar) bar.children.extend([bar_0, bar_1]) baz = Node(".baz", set, {5, 6}, parent=n, key="baz", children=[]) baz_0 = Node(".baz[0]", int, 5, parent=baz) baz_1 = Node(".baz[1]", int, 6, parent=baz) baz.children.extend([baz_0, baz_1]) assert list(n.dfwalk()) == [ n, foo, foo_a, foo_b, bar, bar_0, bar_1, baz, baz_0, baz_1, ]
def verify_collection(self, n, expect_kind, expect_value, expect_name=""): assert n.name == expect_name assert n.kind is expect_kind assert n.value == expect_value assert n.kind in {list, tuple, set, dict} assert not n.is_leaf assert len(n.children) == len(expect_value) if n.kind is dict: expect_children = [ Node(f"{expect_name}.{k}", type(v), v, parent=n, key=k) for k, v in expect_value.items() ] else: expect_children = [ Node(f"{expect_name}[{i}]", type(c), c, parent=n) for i, c in enumerate(expect_value) ] assert n.children == expect_children
def test_leaf_node(self): n = Node.build("foo") assert list(n.dfwalk()) == [Node("", str, "foo")]