Пример #1
0
    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(RuntimeError):
            n2 = n3.parent

        doc.destroy()
        self.assertFalse(doc.isAlive())
        with self.assertRaises(RuntimeError):
            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())
Пример #2
0
    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), [])
Пример #3
0
 def testIsAlive(self):
     doc = Document()
     n1 = Element(doc, "n1")
     self.assertTrue(doc.isAlive())
     self.assertTrue(n1.isAlive())
Пример #4
0
    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())