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