コード例 #1
0
ファイル: test_node.py プロジェクト: HartleyTW/anytree
def test_children_setter():

    root = Node("root")
    s0 = Node("sub0")
    s1 = Node("sub0A")
    s0a = Node("sub0B")

    root.children = [s0, s1]
    s0.children = [s0a]
    eq_(root.descendants, (s0, s0a, s1))

    with assert_raises(LoopError, "Cannot set parent. Node('/root/sub0') cannot be parent of itself."):
        s0.children = [s0]

    # test whether tree is unchanged after LoopError
    eq_(root.descendants, (s0, s0a, s1))

    with assert_raises(LoopError, "Cannot set parent. Node('/root/sub0') is parent of Node('/root/sub0/sub0B')."):
        s0a.children = [s0]

    # test whether tree is unchanged after LoopError
    eq_(root.descendants, (s0, s0a, s1))

    root.children = [s0, s1]
    s0.children = [s0a]
    s0a.children = [s1]
    eq_(root.descendants, (s0, s0a, s1))
コード例 #2
0
ファイル: test_node.py プロジェクト: HartleyTW/anytree
def test_children_setter_large():

    root = Node("root")
    s0 = Node("sub0")
    s0b = Node("sub0B")
    s0a = Node("sub0A")
    s1 = Node("sub1")
    s1a = Node("sub1A")
    s1b = Node("sub1B")
    s1c = Node("sub1C")
    s1ca = Node("sub1Ca")

    root.children = [s0, s1]
    eq_(root.descendants, (s0, s1))
    s0.children = [s0a, s0b]
    eq_(root.descendants, (s0, s0a, s0b, s1))
    s1.children = [s1a, s1b, s1c]
    eq_(root.descendants, (s0, s0a, s0b, s1, s1a, s1b, s1c))
    with assert_raises(TypeError, "'Node' object is not iterable"):
        s1.children = s1ca
    eq_(root.descendants, (s0, s0a, s0b, s1, s1a, s1b, s1c))
コード例 #3
0
ファイル: test_node.py プロジェクト: HartleyTW/anytree
def test_node_children_multiple():

    root = Node("root")
    sub = Node("sub")
    with assert_raises(TreeError, "Cannot add node Node('/sub') multiple times as child."):
        root.children = [sub, sub]
コード例 #4
0
ファイル: test_node.py プロジェクト: HartleyTW/anytree
def test_node_children_type():

    root = Node("root")
    with assert_raises(TreeError, "Cannot add non-node object 'string'. It is not a subclass of 'NodeMixin'."):
        root.children = ["string"]