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))
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))
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]
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"]