def testAppendChildDocument(self): doc1 = Document() n1 = Element(doc1, "foo") doc2 = Document() self.assertFalse(doc1.canAppendChild(doc2)) self.assertFalse(n1.canAppendChild(doc1)) self.assertFalse(n1.canAppendChild(doc2))
def testChildren(self): doc = Document() n1 = Element(doc, "foo") n2 = Element(n1, "bar1") n3 = Element(n1, "bar2") n4 = Element(n1, "bar3") self.assertEqual(getChildNames(n1), ["bar1", "bar2", "bar3"])
def testDocument(self): doc = Document() self.assertEqual(doc.document, doc) n1 = Element(doc, "foo") n2 = Element(n1, "bar") self.assertEqual(n1.document, doc) self.assertEqual(n2.document, doc)
def testAppendChildRootElement(self): document = Document() element1 = Element("foo") element2 = Element("bar") document.appendChild(element1) self.assertEqual(document.rootElement, element1) self.assertFalse(document.canAppendChild(element2)) self.assertIsNone(document.appendChild(element2))
def testReparentCycle(self): doc = Document() n1 = Element(doc, "foo") n2 = Element(n1, "bar") self.assertFalse(n2.canReparent(n2)) with self.assertRaises(ChildCycleError): n2.reparent(n2) self.assertFalse(n1.canReparent(n2)) with self.assertRaises(ChildCycleError): n1.reparent(n2)
def testSaveAndOpen(self): filePath = "testSave.vgc" doc = Document() root = Element(doc, "vgc") path = Element(root, "path") doc.save(filePath) doc = None doc = Document.open(filePath) self.assertEqual(doc.rootElement.name, "vgc") self.assertEqual(doc.rootElement.firstChild.name, "path")
def testChildren(self): n1 = Element("foo") n2 = Element("bar1") n3 = Element("bar2") n4 = Element("bar3") n1.appendChild(n2) n1.appendChild(n3) n1.appendChild(n4) self.assertEqual(getChildNames(n1), ["bar1", "bar2", "bar3"])
def testCheckAlive(self): doc = Document() n1 = Element(doc, "n1") n1.checkAlive() n1.destroy() with self.assertRaises(NotAliveError): n1.checkAlive()
def testDocument(self): doc = Document() self.assertEqual(doc.document, doc) n1 = Element("foo") self.assertIsNone(n1.document) n2 = Element("bar") n1.appendChild(n2) self.assertIsNone(n1.document) self.assertIsNone(n2.document) doc.appendChild(n1) self.assertEqual(n1.document, doc) self.assertEqual(n2.document, doc)
def testConstructor(self): doc = Document() n1 = Element(doc, "n1") self.assertEqual(n1.nodeType, NodeType.Element) self.assertEqual(n1.parent, doc) self.assertEqual(n1.name, "n1") n2 = Element(n1, "n2") self.assertEqual(n2.nodeType, NodeType.Element) self.assertEqual(n2.parent, n1) self.assertEqual(n2.name, "n2") n3 = Element(n1, "n3") self.assertEqual(n3.nodeType, NodeType.Element) self.assertEqual(n3.parent, n1) self.assertEqual(n3.name, "n3") with self.assertRaises(SecondRootElementError): n4 = Element(doc, "n4")
def testRootElement(self): doc = Document() self.assertIsNone(doc.rootElement) n1 = Element(doc, "n1") self.assertEqual(doc.rootElement, n1) n2 = Element(n1, "n2") n2.replace(doc.rootElement) self.assertEqual(doc.rootElement, n2) self.assertFalse(n1.isAlive()) self.assertTrue(n2.isAlive())
def testWrongDocumentError(self): doc1 = Document() n1 = Element(doc1, "n1") doc2 = Document() n2 = Element(doc2, "n2") self.assertFalse(n2.canReparent(n1)) with self.assertRaises(WrongDocumentError): n2.reparent(n1)
def testReparentRootElement(self): doc = Document() n1 = Element(doc, "foo") n2 = Element(n1, "bar") self.assertEqual(doc.rootElement, n1) self.assertFalse(n2.canReparent(doc)) with self.assertRaises(SecondRootElementError): n2.reparent(doc)
def testParentChildRelationships(self): doc = Document() n1 = Element(doc, "foo") self.assertEqual(n1.parent, doc) self.assertEqual(n1.firstChild, None) self.assertEqual(n1.lastChild, None) self.assertEqual(n1.previousSibling, None) self.assertEqual(n1.nextSibling, None) n2 = Element(n1, "bar") n3 = Element(n1, "bar") n4 = Element(n1, "bar") self.assertEqual(n1.parent, doc) self.assertEqual(n1.firstChild, n2) self.assertEqual(n1.lastChild, n4) self.assertEqual(n1.previousSibling, None) self.assertEqual(n1.nextSibling, None) self.assertEqual(n2.parent, n1) self.assertEqual(n2.firstChild, None) self.assertEqual(n2.lastChild, None) self.assertEqual(n2.previousSibling, None) self.assertEqual(n2.nextSibling, n3) self.assertEqual(n3.parent, n1) self.assertEqual(n3.firstChild, None) self.assertEqual(n3.lastChild, None) self.assertEqual(n3.previousSibling, n2) self.assertEqual(n3.nextSibling, n4) self.assertEqual(n4.parent, n1) self.assertEqual(n4.firstChild, None) self.assertEqual(n4.lastChild, None) self.assertEqual(n4.previousSibling, n3) self.assertEqual(n4.nextSibling, None)
def testParentChildRelationships(self): n1 = Element("foo") self.assertIsNone(n1.parent) self.assertIsNone(n1.firstChild) self.assertIsNone(n1.lastChild) self.assertIsNone(n1.previousSibling) self.assertIsNone(n1.nextSibling) n2 = Element("bar") n3 = Element("bar") n4 = Element("bar") n1.appendChild(n2) n1.appendChild(n3) n1.appendChild(n4) self.assertEqual(n1.parent, None) self.assertEqual(n1.firstChild, n2) self.assertEqual(n1.lastChild, n4) self.assertEqual(n1.previousSibling, None) self.assertEqual(n1.nextSibling, None) self.assertEqual(n2.parent, n1) self.assertEqual(n2.firstChild, None) self.assertEqual(n2.lastChild, None) self.assertEqual(n2.previousSibling, None) self.assertEqual(n2.nextSibling, n3) self.assertEqual(n3.parent, n1) self.assertEqual(n3.firstChild, None) self.assertEqual(n3.lastChild, None) self.assertEqual(n3.previousSibling, n2) self.assertEqual(n3.nextSibling, n4) self.assertEqual(n4.parent, n1) self.assertEqual(n4.firstChild, None) self.assertEqual(n4.lastChild, None) self.assertEqual(n4.previousSibling, n3) self.assertEqual(n4.nextSibling, None)
def testReparentDocument(self): doc1 = Document() n1 = Element(doc1, "foo") doc2 = Document() self.assertFalse(doc2.canReparent(doc1)) with self.assertRaises(WrongDocumentError ): # takes precedence over WrongChildTypeError doc2.reparent(doc1) self.assertFalse(doc1.canReparent(n1)) with self.assertRaises( WrongChildTypeError): # takes precedence over ChildCycleError doc1.reparent(n1) self.assertFalse(doc2.canReparent(n1)) with self.assertRaises(WrongDocumentError ): # takes precedence over WrongChildTypeError doc2.reparent(n1)
def testAppendChild(self): doc = Document() n1 = Element(doc, "n1") n2 = Element(n1, "n2") n3 = Element(n2, "n3") self.assertEqual(getChildNames(doc), ["n1"]) self.assertEqual(getChildNames(n1), ["n2"]) self.assertEqual(getChildNames(n2), ["n3"]) self.assertEqual(getChildNames(n3), []) n1.appendChild(n3) self.assertEqual(getChildNames(doc), ["n1"]) self.assertEqual(getChildNames(n1), ["n2", "n3"]) self.assertEqual(getChildNames(n2), []) self.assertEqual(getChildNames(n3), [])
def testConstructor(self): doc = Document() foo = Element(doc, "foo") self.assertEqual(foo.nodeType, NodeType.Element) self.assertEqual(foo.name, "foo")
def testSave(self): doc = Document() root = Element(doc, "vgc") path = Element(root, "path") doc.save("testSave.vgc")
def testReplace(self): doc = Document() root = Element(doc, "root") n1 = Element(root, "n1") n2 = Element(root, "n2") n3 = Element(root, "n3") n4 = Element(root, "n4") n21 = Element(n2, "n21") n22 = Element(n2, "n22") n23 = Element(n2, "n23") n31 = Element(n3, "n31") n211 = Element(n21, "n211") n212 = Element(n21, "n212") self.assertTrue(n21.canReplace(n3)) n21.replace(n3) self.assertEqual(getChildNames(root), ["n1", "n2", "n21", "n4"]) self.assertEqual(getChildNames(n2), ["n22", "n23"]) self.assertTrue(n21.isAlive()) self.assertFalse(n3.isAlive()) self.assertFalse(n31.isAlive()) self.assertTrue(n2.canReplace(n2)) n2.replace(n2) self.assertEqual(getChildNames(root), ["n1", "n2", "n21", "n4"]) self.assertEqual(getChildNames(n2), ["n22", "n23"]) self.assertTrue(n22.canReplace(n2)) n22.replace(n2) self.assertEqual(getChildNames(root), ["n1", "n22", "n21", "n4"]) self.assertTrue(n22.isAlive()) self.assertFalse(n2.isAlive()) self.assertFalse(n23.isAlive()) self.assertTrue(n21.canReplace(n1)) n21.replace(n1) self.assertEqual(getChildNames(root), ["n21", "n22", "n4"]) self.assertTrue(n21.isAlive()) self.assertFalse(n1.isAlive()) self.assertTrue(n22.canReplace(n4)) n22.replace(n4) self.assertEqual(getChildNames(root), ["n21", "n22"]) self.assertTrue(n22.isAlive()) self.assertFalse(n4.isAlive()) self.assertTrue(root.canReplace(root)) root.replace(root) self.assertEqual(getChildNames(doc), ["root"]) self.assertEqual(getChildNames(root), ["n21", "n22"]) self.assertTrue(root.isAlive()) self.assertTrue(n21.canReplace(root)) n21.replace(root) self.assertEqual(getChildNames(doc), ["n21"]) self.assertEqual(getChildNames(n21), ["n211", "n212"]) self.assertTrue(n21.isAlive()) self.assertFalse(root.isAlive()) self.assertFalse(n22.isAlive()) self.assertTrue(doc.canReplace(doc)) doc.replace(doc) self.assertTrue(doc.isAlive()) self.assertEqual(getChildNames(doc), ["n21"]) self.assertTrue(n21.isAlive())
def testReparent(self): doc = Document() n1 = Element(doc, "n1") n2 = Element(n1, "n2") n3 = Element(n2, "n3") self.assertEqual(getChildNames(doc), ["n1"]) self.assertEqual(getChildNames(n1), ["n2"]) self.assertEqual(getChildNames(n2), ["n3"]) self.assertEqual(getChildNames(n3), []) self.assertTrue(n3.canReparent(n1)) n3.reparent(n1) self.assertEqual(getChildNames(doc), ["n1"]) self.assertEqual(getChildNames(n1), ["n2", "n3"]) self.assertEqual(getChildNames(n2), []) self.assertEqual(getChildNames(n3), []) # Special case 1: move last self.assertTrue(n2.canReparent(n1)) n2.reparent(n1) self.assertEqual(getChildNames(n1), ["n3", "n2"]) # Special case 2: append root element again (= do nothing, or move last in case of comments) self.assertTrue(n1.canReparent(doc)) n1.reparent(doc) self.assertEqual(getChildNames(doc), ["n1"])
def testIsDescendant(self): doc = Document() root = Element(doc, "root") n1 = Element(root, "n1") n2 = Element(root, "n2") n3 = Element(root, "n3") n4 = Element(root, "n4") n21 = Element(n2, "n21") n22 = Element(n2, "n22") n23 = Element(n2, "n23") n31 = Element(n3, "n31") n211 = Element(n21, "n211") n212 = Element(n21, "n212") # Test all pairs: 12*12=144 tests for n in [doc, root, n1, n2, n3, n4, n21, n22, n23, n31, n211, n212]: self.assertTrue(n.isDescendant(n)) for n in [root, n1, n2, n3, n4, n21, n22, n23, n31, n211, n212]: self.assertTrue(n.isDescendant(doc)) self.assertFalse(doc.isDescendant(n)) for n in [n1, n2, n3, n4, n21, n22, n23, n31, n211, n212]: self.assertTrue(n.isDescendant(root)) self.assertFalse(root.isDescendant(n)) for n in [n21, n22, n23, n211, n212]: self.assertTrue(n.isDescendant(n2)) self.assertFalse(n2.isDescendant(n)) for n in [n31]: self.assertTrue(n.isDescendant(n3)) self.assertFalse(n3.isDescendant(n)) for n in [n211, n212]: self.assertTrue(n.isDescendant(n21)) self.assertFalse(n21.isDescendant(n)) for n in [n2, n3, n4, n21, n22, n23, n31, n211, n212]: self.assertFalse(n.isDescendant(n1)) self.assertFalse(n1.isDescendant(n)) for n in [n3, n4, n31]: self.assertFalse(n.isDescendant(n2)) self.assertFalse(n2.isDescendant(n)) for n in [n4, n21, n22, n23, n211, n212]: self.assertFalse(n.isDescendant(n3)) self.assertFalse(n3.isDescendant(n)) for n in [n21, n22, n23, n31, n211, n212]: self.assertFalse(n.isDescendant(n4)) self.assertFalse(n4.isDescendant(n)) for n in [n22, n23, n31]: self.assertFalse(n.isDescendant(n21)) self.assertFalse(n21.isDescendant(n)) for n in [n23, n31, n211, n212]: self.assertFalse(n.isDescendant(n22)) self.assertFalse(n22.isDescendant(n)) for n in [n31, n211, n212]: self.assertFalse(n.isDescendant(n23)) self.assertFalse(n23.isDescendant(n)) for n in [n211, n212]: self.assertFalse(n.isDescendant(n31)) self.assertFalse(n31.isDescendant(n)) for n in [n212]: self.assertFalse(n.isDescendant(n211)) self.assertFalse(n211.isDescendant(n))
def testReplaceExceptions(self): doc = Document() root = Element(doc, "root") n1 = Element(root, "n1") doc2 = Document() root2 = Element(doc2, "root2") self.assertFalse(n1.canReplace(doc)) with self.assertRaises(ReplaceDocumentError): n1.replace(doc) self.assertFalse(doc.canReplace(doc2)) with self.assertRaises(ReplaceDocumentError): doc.replace(doc2) self.assertFalse(root.canReplace(root2)) with self.assertRaises(WrongDocumentError): root.replace(root2) self.assertFalse(doc.canReplace(n1)) with self.assertRaises(WrongChildTypeError): doc.replace(n1) # XXX Once NodeType::Comment is implemented, uncomment the following test. # until then, replace() can never raise SecondRootElementError # c1 = Comment(doc, "comment") # self.assertFalse(n1.canReplace(c1)) # with self.assertRaises(SecondRootElementError): # n1.replace(c1) self.assertFalse(root.canReplace(n1)) with self.assertRaises(ChildCycleError): root.replace(n1)
def testIsAlive(self): doc = Document() n1 = Element(doc, "n1") self.assertTrue(doc.isAlive()) self.assertTrue(n1.isAlive())
def testNodeType(self): doc = Document() element = Element(doc, "foo") self.assertEqual(doc.nodeType, NodeType.Document) self.assertEqual(element.nodeType, NodeType.Element)
def testNodeType(self): node = Element("foo") self.assertEqual(node.nodeType, NodeType.Element)
def testRemoveChild(self): n = Element("foo") n1 = Element("n1") n2 = Element("n2") n3 = Element("n3") n4 = Element("n4") n.appendChild(n1) n.appendChild(n2) n.appendChild(n3) n.appendChild(n4) self.assertEqual(getChildNames(n), ["n1", "n2", "n3", "n4"]) n.removeChild(n3) self.assertEqual(getChildNames(n), ["n1", "n2", "n4"]) n.removeChild(n4) self.assertEqual(getChildNames(n), ["n1", "n2"]) self.assertIsNone(n.removeChild(n4)) self.assertEqual(getChildNames(n), ["n1", "n2"]) n5 = Element("n5") self.assertIsNone(n.removeChild(n5)) self.assertEqual(getChildNames(n), ["n1", "n2"]) n.removeChild(n1) self.assertEqual(getChildNames(n), ["n2"]) n.removeChild(n2) self.assertEqual(getChildNames(n), [])
def testDestroy(self): doc = Document() n1 = Element(doc, "n1") n3 = Element(n1, "n2") # not a typo n3 = Element(n3, "n3") self.assertTrue(doc.isAlive()) self.assertTrue(n1.isAlive()) self.assertTrue(n3.parent.isAlive()) self.assertTrue(n3.isAlive()) self.assertTrue(doc.rootElement, n1) n1.destroy() self.assertTrue(doc.isAlive()) self.assertFalse(n1.isAlive()) self.assertFalse(n3.isAlive()) self.assertTrue(doc.rootElement == None) with self.assertRaises(NotAliveError): n2 = n3.parent doc.destroy() self.assertFalse(doc.isAlive()) with self.assertRaises(NotAliveError): n1 = doc.rootElement n1 = None with self.assertRaises(AttributeError): doc = n1.parent del n3 with self.assertRaises(UnboundLocalError): n2 = n3.parent doc = Document() n1 = Element(doc, "n1") n2 = Element(n1, "n2") n3 = Element(n1, "n3") self.assertTrue(doc.isAlive()) self.assertTrue(n1.isAlive()) self.assertTrue(n2.isAlive()) self.assertTrue(n3.isAlive()) self.assertTrue(doc.rootElement == n1) self.assertTrue(n1.firstChild == n2) self.assertTrue(n1.lastChild == n3) del n2 self.assertTrue(n1.firstChild.isAlive()) n1 = None self.assertTrue(doc.rootElement.isAlive()) self.assertTrue(doc.rootElement.firstChild.isAlive()) self.assertTrue(n3.isAlive()) doc = None self.assertFalse(n3.isAlive()) doc = Document() n1 = Element(doc, "n1") del doc self.assertFalse(n1.isAlive()) doc = Document() root = Element(doc, "root") n1 = Element(root, "n1") n2 = Element(root, "n2") n3 = Element(root, "n3") n4 = Element(root, "n4") self.assertEqual(getChildNames(root), ["n1", "n2", "n3", "n4"]) n3.destroy() self.assertEqual(getChildNames(root), ["n1", "n2", "n4"]) n4.destroy() self.assertEqual(getChildNames(root), ["n1", "n2"]) n1.destroy() self.assertEqual(getChildNames(root), ["n2"]) n2.destroy() self.assertEqual(getChildNames(root), [])
def testAppendChildDocument(self): element = Element("foo") document = Document() self.assertFalse(element.canAppendChild(document)) self.assertIsNone(element.appendChild(document))
def testAppendChild(self): node1 = Element("foo") node2 = Element("bar") self.assertTrue(node1.canAppendChild(node2)) self.assertEqual(node1.appendChild(node2), node2)