Beispiel #1
0
class TestText(TestCase):
    def setUp(self):
        self.node = Node()
        self.df = DocumentFragment()
        self.tnode = Text('text')

    def test_nodename(self):
        self.assertEqual(self.tnode.nodeName, '#text')

    def test_html_escape(self):
        self.assertEqual(self.tnode.html, 'text')
        self.tnode.textContent = '<'
        # not escape it has no parent
        self.assertEqual(self.tnode.html, '<')
        self.df.appendChild(self.tnode)
        # escape its parent is DocumentFragment or Element or...
        self.assertEqual(self.tnode.html, '&lt;')

        self.assertEqual(Text('<', parent=self.df).html, '&lt;')
        self.assertEqual(Text('>', parent=self.df).html, '&gt;')
        self.assertEqual(Text('&', parent=self.df).html, '&amp;')
        self.assertEqual(Text('"', parent=self.df).html, '&quot;')
        self.assertEqual(Text('\'', parent=self.df).html, '&#x27;')

    def test_appned(self):
        self.node.appendChild(self.tnode)
        self.assertTrue(self.node.hasChildNodes())
        self.assertIs(self.tnode.parentNode, self.node)

        node1 = Node()
        node2 = Node()
        self.node.appendChild(node1)
        self.assertIs(self.tnode.nextSibling, node1)
        self.assertIs(self.tnode, node1.previousSibling)

        self.node.insertBefore(node2, self.tnode)
        self.assertIs(self.tnode.previousSibling, node2)
        self.assertIs(self.tnode, node2.nextSibling)
Beispiel #2
0
class TestText(TestCase):
    def setUp(self):
        self.node = Node()
        self.df = DocumentFragment()
        self.tnode = Text('text')

    def test_nodename(self):
        self.assertEqual(self.tnode.nodeName, '#text')

    def test_html_escape(self):
        self.assertEqual(self.tnode.html, 'text')
        self.tnode.textContent = '<'
        # not escape it has no parent
        self.assertEqual(self.tnode.html, '<')
        self.df.appendChild(self.tnode)
        # escape its parent is DocumentFragment or Element or...
        self.assertEqual(self.tnode.html, '&lt;')

        self.assertEqual(Text('<', parent=self.df).html, '&lt;')
        self.assertEqual(Text('>', parent=self.df).html, '&gt;')
        self.assertEqual(Text('&', parent=self.df).html, '&amp;')
        self.assertEqual(Text('"', parent=self.df).html, '&quot;')
        self.assertEqual(Text('\'', parent=self.df).html, '&#x27;')

    def test_appned(self):
        self.node.appendChild(self.tnode)
        self.assertTrue(self.node.hasChildNodes())
        self.assertIs(self.tnode.parentNode, self.node)

        node1 = Node()
        node2 = Node()
        self.node.appendChild(node1)
        self.assertIs(self.tnode.nextSibling, node1)
        self.assertIs(self.tnode, node1.previousSibling)

        self.node.insertBefore(node2, self.tnode)
        self.assertIs(self.tnode.previousSibling, node2)
        self.assertIs(self.tnode, node2.nextSibling)
Beispiel #3
0
 def test_parent_init(self):
     node = Node(parent=self.node)
     self.assertTrue(self.node.hasChildNodes())
     self.assertIs(self.node, node.parentNode)
     self.assertFalse(node.hasChildNodes())
Beispiel #4
0
class TestNode(TestCase):
    def setUp(self):
        super().setUp()
        self.node = Node()
        self.c1 = Node()
        self.c2 = Node()
        self.c3 = Node()

    def test_attributes(self):
        self.assertFalse(self.node.hasAttributes())

    def test_parent(self):
        self.assertIsNone(self.node.parentNode)
        self.assertIsNone(self.c1.parentNode)
        self.node.appendChild(self.c1)
        self.assertIs(self.node, self.c1.parentNode)

        self.node.removeChild(self.c1)
        self.assertIsNone(self.c1.parentNode)

    def test_parent_init(self):
        node = Node(parent=self.node)
        self.assertTrue(self.node.hasChildNodes())
        self.assertIs(self.node, node.parentNode)
        self.assertFalse(node.hasChildNodes())

    def test_addremove_child(self):
        self.assertFalse(self.node.hasChildNodes())
        self.assertEqual(len(self.node), 0)
        self.assertEqual(self.node.length, 0)

        appended_child1 = self.node.appendChild(self.c1)
        self.assertIs(appended_child1, self.c1)
        self.assertTrue(self.node.hasChildNodes())
        self.assertEqual(len(self.node), 1)
        self.assertEqual(self.node.length, 1)

        appended_child2 = self.node.appendChild(self.c2)
        self.assertIs(appended_child2, self.c2)
        self.assertEqual(len(self.node), 2)
        self.assertEqual(self.node.length, 2)

        removed_child2 = self.node.removeChild(self.c2)
        self.assertIs(removed_child2, self.c2)
        self.assertEqual(len(self.node), 1)
        self.assertEqual(self.node.length, 1)
        self.assertIsNone(self.c2.parentNode)

        removed_child1 = self.node.removeChild(self.c1)
        self.assertIs(removed_child1, self.c1)
        self.assertFalse(self.node.hasChildNodes())
        self.assertEqual(len(self.node), 0)
        self.assertEqual(self.node.length, 0)

        with self.assertRaises(ValueError):
            self.node.removeChild(self.c1)

    def test_empty(self):
        self.node.appendChild(self.c1)
        self.assertTrue(self.node.hasChildNodes())
        self.node.empty()
        self.assertFalse(self.node.hasChildNodes())

        self.node.appendChild(self.c1)
        self.node.appendChild(self.c2)
        self.c1.appendChild(self.c3)
        self.assertTrue(self.node.hasChildNodes())
        self.node.empty()
        self.assertFalse(self.node.hasChildNodes())
        self.assertTrue(self.c1.hasChildNodes())
        self.assertIsNone(self.c1.parentNode)

    def test_insert_before(self):
        self.node.appendChild(self.c1)
        self.node.appendChild(self.c2)
        inserted_node3 = self.node.insertBefore(self.c3, self.c2)

        self.assertIs(inserted_node3, self.c3)
        self.assertIs(self.c3.parentNode, self.node)
        self.assertIs(self.node.childNodes[0], self.c1)
        self.assertIs(self.node.childNodes[1], self.c3)
        self.assertIs(self.node.childNodes[2], self.c2)

    def test_insert_first(self):
        self.node.appendChild(self.c1)
        self.node.appendChild(self.c2)
        inserted_node3 = self.node.insertBefore(self.c3, self.c1)

        self.assertIs(inserted_node3, self.c3)
        self.assertIs(self.c3.parentNode, self.node)
        self.assertIs(self.node.childNodes[0], self.c3)
        self.assertIs(self.node.childNodes[1], self.c1)
        self.assertIs(self.node.childNodes[2], self.c2)

    def test_replace_child(self):
        self.node.appendChild(self.c1)
        self.assertTrue(self.c1 in self.node)
        self.assertFalse(self.c2 in self.node)

        replaced_node = self.node.replaceChild(self.c2, self.c1)
        self.assertIs(replaced_node, self.c1)
        self.assertFalse(self.c1 in self.node)
        self.assertTrue(self.c2 in self.node)
        self.assertIsNone(self.c1.parentNode)
        self.assertIs(self.c2.parentNode, self.node)

    def test_first_last_child(self):
        self.assertIsNone(self.node.firstChild)
        self.assertIsNone(self.node.lastChild)

        self.node.appendChild(self.c1)
        self.assertIs(self.node.firstChild, self.c1)
        self.assertIs(self.node.lastChild, self.c1)

        self.node.appendChild(self.c2)
        self.assertIs(self.node.firstChild, self.c1)
        self.assertIs(self.node.lastChild, self.c2)

    def test_siblings(self):
        self.assertIsNone(self.node.previousSibling)
        self.assertIsNone(self.node.nextSibling)

        self.node.appendChild(self.c1)
        self.assertIsNone(self.c1.previousSibling)
        self.assertIsNone(self.c1.nextSibling)

        self.node.appendChild(self.c2)
        self.assertIsNone(self.c1.previousSibling)
        self.assertIs(self.c1.nextSibling, self.c2)
        self.assertIs(self.c2.previousSibling, self.c1)
        self.assertIsNone(self.c2.nextSibling)

    def _test_shallow_copy(self, clone):
        self.assertTrue(self.node.hasChildNodes())
        self.assertFalse(clone.hasChildNodes())
        self.assertEqual(len(clone), 0)

        clone.appendChild(self.c2)
        self.assertTrue(clone.hasChildNodes())
        self.assertIn(self.c2, clone)
        self.assertNotIn(self.c2, self.node)

    def test_copy(self):
        from copy import copy
        self.node.appendChild(self.c1)
        clone = copy(self.node)
        self._test_shallow_copy(clone)

    def test_clone_node_sharrow(self):
        self.node.appendChild(self.c1)
        clone = self.node.cloneNode()
        self._test_shallow_copy(clone)

        clone2 = self.node.cloneNode(deep=False)
        self._test_shallow_copy(clone2)

    def _test_deep_copy(self, clone):
        self.assertTrue(clone.hasChildNodes())
        self.assertEqual(len(clone), 1)
        self.assertIn(self.c1, self.node)
        self.assertNotIn(self.c1, clone)

        clone.appendChild(self.c2)
        self.assertEqual(len(clone), 2)
        self.assertEqual(len(self.node), 1)

    def test_deepcopy(self):
        from copy import deepcopy
        self.node.appendChild(self.c1)
        clone = deepcopy(self.node)
        self._test_deep_copy(clone)

    def test_clone_node_deep(self):
        self.node.appendChild(self.c1)
        clone = self.node.cloneNode(deep=True)
        self._test_deep_copy(clone)

    def test_owner_document(self):
        self.assertIsNone(self.node.ownerDocument)

    def test_text_content(self):
        self.assertEqual(self.node.textContent, '')
        self.node.textContent = 'a'
        self.assertEqual(self.node.textContent, 'a')
        self.node.textContent = 'b'
        self.assertEqual(self.node.textContent, 'b')

        self.node.appendChild(self.c1)
        self.c1.textContent = 'c1'
        self.assertEqual(self.node.textContent, 'bc1')
        self.node.textContent = 'd'
        self.assertEqual(self.node.textContent, 'd')
        self.assertIsNone(self.c1.parentNode)
        self.assertEqual(self.c1.textContent, 'c1')

    def test_index(self):
        self.node.appendChild(self.c1)
        self.node.appendChild(self.c2)
        self.node.appendChild(self.c3)
        self.assertEqual(self.node.index(self.c1), 0)
        self.assertEqual(self.node.index(self.c2), 1)
        self.assertEqual(self.node.index(self.c3), 2)
        self.assertEqual(self.node.childNodes.index(self.c1), 0)
        self.assertEqual(self.node.childNodes.index(self.c2), 1)
        self.assertEqual(self.node.childNodes.index(self.c3), 2)
Beispiel #5
0
 def test_parent_init(self):
     node = Node(parent=self.node)
     self.assertTrue(self.node.hasChildNodes())
     self.assertIs(self.node, node.parentNode)
     self.assertFalse(node.hasChildNodes())
Beispiel #6
0
class TestNode(TestCase):
    def setUp(self):
        self.node = Node()
        self.c1 = Node()
        self.c2 = Node()
        self.c3 = Node()

    def test_attributes(self):
        self.assertFalse(self.node.hasAttributes())

    def test_parent(self):
        self.assertIsNone(self.node.parentNode)
        self.assertIsNone(self.c1.parentNode)
        self.node.appendChild(self.c1)
        self.assertIs(self.node, self.c1.parentNode)

        self.node.removeChild(self.c1)
        self.assertIsNone(self.c1.parentNode)

    def test_parent_init(self):
        node = Node(parent=self.node)
        self.assertTrue(self.node.hasChildNodes())
        self.assertIs(self.node, node.parentNode)
        self.assertFalse(node.hasChildNodes())

    def test_addremove_child(self):
        self.assertFalse(self.node.hasChildNodes())
        self.assertEqual(len(self.node), 0)
        self.assertEqual(self.node.length, 0)

        appended_child1 = self.node.appendChild(self.c1)
        self.assertIs(appended_child1, self.c1)
        self.assertTrue(self.node.hasChildNodes())
        self.assertEqual(len(self.node), 1)
        self.assertEqual(self.node.length, 1)

        appended_child2 = self.node.appendChild(self.c2)
        self.assertIs(appended_child2, self.c2)
        self.assertEqual(len(self.node), 2)
        self.assertEqual(self.node.length, 2)

        removed_child2 = self.node.removeChild(self.c2)
        self.assertIs(removed_child2, self.c2)
        self.assertEqual(len(self.node), 1)
        self.assertEqual(self.node.length, 1)
        self.assertIsNone(self.c2.parentNode)

        removed_child1 = self.node.removeChild(self.c1)
        self.assertIs(removed_child1, self.c1)
        self.assertFalse(self.node.hasChildNodes())
        self.assertEqual(len(self.node), 0)
        self.assertEqual(self.node.length, 0)

        with self.assertRaises(ValueError):
            self.node.removeChild(self.c1)

    def test_empty(self):
        self.node.appendChild(self.c1)
        self.assertTrue(self.node.hasChildNodes())
        self.node.empty()
        self.assertFalse(self.node.hasChildNodes())

        self.node.appendChild(self.c1)
        self.node.appendChild(self.c2)
        self.c1.appendChild(self.c3)
        self.assertTrue(self.node.hasChildNodes())
        self.node.empty()
        self.assertFalse(self.node.hasChildNodes())
        self.assertTrue(self.c1.hasChildNodes())
        self.assertIsNone(self.c1.parentNode)

    def test_insert_before(self):
        self.node.appendChild(self.c1)
        self.node.appendChild(self.c2)
        inserted_node3 = self.node.insertBefore(self.c3, self.c2)

        self.assertIs(inserted_node3, self.c3)
        self.assertIs(self.c3.parentNode, self.node)
        self.assertIs(self.node.childNodes[0], self.c1)
        self.assertIs(self.node.childNodes[1], self.c3)
        self.assertIs(self.node.childNodes[2], self.c2)

    def test_insert_first(self):
        self.node.appendChild(self.c1)
        self.node.appendChild(self.c2)
        inserted_node3 = self.node.insertBefore(self.c3, self.c1)

        self.assertIs(inserted_node3, self.c3)
        self.assertIs(self.c3.parentNode, self.node)
        self.assertIs(self.node.childNodes[0], self.c3)
        self.assertIs(self.node.childNodes[1], self.c1)
        self.assertIs(self.node.childNodes[2], self.c2)

    def test_replace_child(self):
        self.node.appendChild(self.c1)
        self.assertTrue(self.c1 in self.node)
        self.assertFalse(self.c2 in self.node)

        replaced_node = self.node.replaceChild(self.c2, self.c1)
        self.assertIs(replaced_node, self.c1)
        self.assertFalse(self.c1 in self.node)
        self.assertTrue(self.c2 in self.node)
        self.assertIsNone(self.c1.parentNode)
        self.assertIs(self.c2.parentNode, self.node)

    def test_first_last_child(self):
        self.assertIsNone(self.node.firstChild)
        self.assertIsNone(self.node.lastChild)

        self.node.appendChild(self.c1)
        self.assertIs(self.node.firstChild, self.c1)
        self.assertIs(self.node.lastChild, self.c1)

        self.node.appendChild(self.c2)
        self.assertIs(self.node.firstChild, self.c1)
        self.assertIs(self.node.lastChild, self.c2)

    def test_siblings(self):
        self.assertIsNone(self.node.previousSibling)
        self.assertIsNone(self.node.nextSibling)

        self.node.appendChild(self.c1)
        self.assertIsNone(self.c1.previousSibling)
        self.assertIsNone(self.c1.nextSibling)

        self.node.appendChild(self.c2)
        self.assertIsNone(self.c1.previousSibling)
        self.assertIs(self.c1.nextSibling, self.c2)
        self.assertIs(self.c2.previousSibling, self.c1)
        self.assertIsNone(self.c2.nextSibling)

    def _test_shallow_copy(self, clone):
        self.assertTrue(self.node.hasChildNodes())
        self.assertFalse(clone.hasChildNodes())
        self.assertEqual(len(clone), 0)

        clone.appendChild(self.c2)
        self.assertTrue(clone.hasChildNodes())
        self.assertIn(self.c2, clone)
        self.assertNotIn(self.c2, self.node)

    def test_copy(self):
        from copy import copy
        self.node.appendChild(self.c1)
        clone = copy(self.node)
        self._test_shallow_copy(clone)

    def test_clone_node_sharrow(self):
        self.node.appendChild(self.c1)
        clone = self.node.cloneNode()
        self._test_shallow_copy(clone)

        clone2 = self.node.cloneNode(deep=False)
        self._test_shallow_copy(clone2)

    def _test_deep_copy(self, clone):
        self.assertTrue(clone.hasChildNodes())
        self.assertEqual(len(clone), 1)
        self.assertIn(self.c1, self.node)
        self.assertNotIn(self.c1, clone)

        clone.appendChild(self.c2)
        self.assertEqual(len(clone), 2)
        self.assertEqual(len(self.node), 1)

    def test_deepcopy(self):
        from copy import deepcopy
        self.node.appendChild(self.c1)
        clone = deepcopy(self.node)
        self._test_deep_copy(clone)

    def test_clone_node_deep(self):
        self.node.appendChild(self.c1)
        clone = self.node.cloneNode(deep=True)
        self._test_deep_copy(clone)

    def test_owner_document(self):
        self.assertIsNone(self.node.ownerDocument)

    def test_text_content(self):
        self.assertEqual(self.node.textContent, '')
        self.node.textContent = 'a'
        self.assertEqual(self.node.textContent, 'a')
        self.node.textContent = 'b'
        self.assertEqual(self.node.textContent, 'b')

        self.node.appendChild(self.c1)
        self.c1.textContent = 'c1'
        self.assertEqual(self.node.textContent, 'bc1')
        self.node.textContent = 'd'
        self.assertEqual(self.node.textContent, 'd')
        self.assertIsNone(self.c1.parentNode)
        self.assertEqual(self.c1.textContent, 'c1')

    def test_index(self):
        self.node.appendChild(self.c1)
        self.node.appendChild(self.c2)
        self.node.appendChild(self.c3)
        self.assertEqual(self.node.index(self.c1), 0)
        self.assertEqual(self.node.index(self.c2), 1)
        self.assertEqual(self.node.index(self.c3), 2)
        self.assertEqual(self.node.childNodes.index(self.c1), 0)
        self.assertEqual(self.node.childNodes.index(self.c2), 1)
        self.assertEqual(self.node.childNodes.index(self.c3), 2)