예제 #1
0
    def test_add(self):
        "Nodes can be added as children to another node"

        style = Style()
        node = Node(style=style, children=[])

        child = Node(style=style)
        node.add(child)

        self.assertTrue(child in node.children)
        self.assertEqual(child.parent, node)
        self.assertEqual(child.root, node.root)
예제 #2
0
    def setUp(self):
        self.maxDiff = None

        self.grandchild1_1 = Node(style=Style())
        self.grandchild1_1.layout.content_width = 10
        self.grandchild1_1.layout.content_height = 16
        self.grandchild1_2 = Node(style=Style())

        self.child1 = Node(style=Style(), children=[self.grandchild1_1, self.grandchild1_2])
        self.child1.layout.content_width = 10
        self.child1.layout.content_height = 16
        self.child2 = Node(style=Style(), children=[])

        self.node = Node(style=Style(), children=[self.child1, self.child2])
        self.node.layout.content_width = 10
        self.node.layout.content_height = 16
예제 #3
0
    def test_create_leaf(self):
        "A leaf can be created"
        style = Style()
        leaf = Node(style=style)

        self.assertIsNone(leaf._children)
        self.assertEqual(leaf.children, [])
        self.assertFalse(leaf.can_have_children)

        # An unattached leaf is a root
        self.assertIsNone(leaf.parent)
        self.assertEqual(leaf.root, leaf)

        # A leaf can't have children
        child = Node(style=style)

        with self.assertRaises(ValueError):
            leaf.add(child)
예제 #4
0
    def test_create_node(self):
        "A node can be created with children"
        style = Style()

        child1 = Node(style=style)
        child2 = Node(style=style)
        child3 = Node(style=style)

        node = Node(style=style, children=[child1, child2, child3])

        self.assertEqual(node.children, [child1, child2, child3])
        self.assertTrue(node.can_have_children)

        # The node is the root as well.
        self.assertIsNone(node.parent)
        self.assertEqual(node.root, node)

        # The children all point at the node.
        self.assertEqual(child1.parent, node)
        self.assertEqual(child1.root, node)

        self.assertEqual(child2.parent, node)
        self.assertEqual(child2.root, node)

        self.assertEqual(child3.parent, node)
        self.assertEqual(child3.root, node)

        # Create another node
        new_node = Node(style=style, children=[])

        self.assertEqual(new_node.children, [])
        self.assertTrue(new_node.can_have_children)

        # Add the old node as a child of the new one.
        new_node.add(node)

        # The new node is the root
        self.assertIsNone(new_node.parent)
        self.assertEqual(new_node.root, new_node)

        # The node is the root as well.
        self.assertEqual(node.parent, new_node)
        self.assertEqual(node.root, new_node)

        # The children all point at the node.
        self.assertEqual(child1.parent, node)
        self.assertEqual(child1.root, new_node)

        self.assertEqual(child2.parent, node)
        self.assertEqual(child2.root, new_node)

        self.assertEqual(child3.parent, node)
        self.assertEqual(child3.root, new_node)
예제 #5
0
    def test_remove(self):
        "Children can be removed from node"

        style = Style()
        child1 = Node(style=style)
        child2 = Node(style=style)
        child3 = Node(style=style)
        node = Node(style=style, children=[child1, child2, child3])

        node.remove(child1)

        self.assertFalse(child1 in node.children)
        self.assertEqual(child1.parent, None)
        self.assertEqual(child1.root, child1)
예제 #6
0
    def test_insert(self):
        "Node can be inserted at a specific position as a child"

        style = Style()
        child1 = Node(style=style)
        child2 = Node(style=style)
        child3 = Node(style=style)
        node = Node(style=style, children=[child1, child2, child3])

        child4 = Node(style=style)

        index = 2
        node.insert(index, child4)

        self.assertTrue(child4 in node.children)
        self.assertEqual(child4.parent, node)
        self.assertEqual(child4.root, node.root)

        self.assertEqual(node.children.index(child4), index)
예제 #7
0
    def test_clear(self):
        "Node can be inserted at a specific position as a child"
        style = Style()
        children = [Node(style=style), Node(style=style), Node(style=style)]
        node = Node(style=style, children=children)

        for child in children:
            self.assertTrue(child in node.children)
            self.assertEqual(child.parent, node)
            self.assertEqual(child.root, node)
        self.assertEqual(node.children, children)

        node.clear()

        for child in children:
            self.assertFalse(child in node.children)
            self.assertEqual(child.parent, None)
            self.assertEqual(child.root, child)

        self.assertEqual(node.children, [])