Ticket #3421: microdom.assert2.diff

File microdom.assert2.diff, 4.4 KB (added by jparise, 8 years ago)

Replaces asserts with exceptions

  • twisted/web/test/test_xml.py

     
    350350        self.assertEquals(d.firstChild(), t)
    351351        self.assert_(d.isEqualToNode(d2))
    352352
     353        self.assertRaises(ValueError, d.replaceChild, t, d1)
     354
    353355    def testSearch(self):
    354356        s = "<foo><bar id='me' /><baz><foo /></baz></foo>"
    355357        s2 = "<fOo><bAr id='me' /><bAz><fOO /></bAz></fOo>"
     
    788790        another.firstChild().appendChild(microdom.Node(object()))
    789791        self.assertTrue(node.isEqualToNode(another))
    790792
     793    def test_validChildInstance(self):
     794        """
     795        Children of L{Node} instances must also be L{Node} instances.
     796        """
     797        node = microdom.Node(object())
     798        child = microdom.Node(object())
     799        # Node.appendChild() only accepts Node instances.
     800        node.appendChild(child)
     801        self.assertRaises(TypeError, node.appendChild, None)
     802        # Node.insertBefore() only accepts Node instances.
     803        self.assertRaises(TypeError, node.insertBefore, child, None)
     804        self.assertRaises(TypeError, node.insertBefore, None, child)
     805        self.assertRaises(TypeError, node.insertBefore, None, None)
     806        # Node.removeChild() only accepts Node instances.
     807        node.removeChild(child)
     808        self.assertRaises(TypeError, node.removeChild, None)
     809        # Node.replaceChild() only accepts Node instances.
     810        self.assertRaises(TypeError, node.replaceChild, child, None)
     811        self.assertRaises(TypeError, node.replaceChild, None, child)
     812        self.assertRaises(TypeError, node.replaceChild, None, None)
    791813
    792814
    793815class DocumentTests(TestCase):
  • twisted/web/microdom.py

     
    158158            return 0
    159159
    160160    def appendChild(self, child):
    161         assert isinstance(child, Node)
     161        """
     162        @raise TypeError: If C{child} is not a C{Node} instance.
     163        """
     164        if not isinstance(child, Node):
     165            raise TypeError("expected Node instance")
    162166        self.childNodes.append(child)
    163167        child.parentNode = self
    164168
    165169    def insertBefore(self, new, ref):
     170        """
     171        @raise TypeError: If C{new} or C{ref} is not a C{Node} instance.
     172        """
     173        if not isinstance(new, Node) or not isinstance(ref, Node):
     174            raise TypeError("expected Node instance")
    166175        i = self.childNodes.index(ref)
    167176        new.parentNode = self
    168177        self.childNodes.insert(i, new)
    169178        return new
    170179
    171180    def removeChild(self, child):
     181        """
     182        @raise TypeError: If C{child} is not a C{Node} instance.
     183        """
     184        if not isinstance(child, Node):
     185            raise TypeError("expected Node instance")
    172186        if child in self.childNodes:
    173187            self.childNodes.remove(child)
    174188            child.parentNode = None
    175189        return child
    176190
    177191    def replaceChild(self, newChild, oldChild):
    178         assert isinstance(newChild, Node)
     192        """
     193        @raise TypeError: If C{newChild} or C{oldChild} is not a C{Node}
     194            instance.
     195
     196        @raise ValueError: If C{oldChild} is not a child of this C{Node}.
     197        """
     198        if not isinstance(newChild, Node) or not isinstance(oldChild, Node):
     199            raise TypeError("expected Node instance")
    179200        #if newChild.parentNode:
    180201        #    newChild.parentNode.removeChild(newChild)
    181         assert (oldChild.parentNode is self,
    182                 ('oldChild (%s): oldChild.parentNode (%s) != self (%s)'
    183                  % (oldChild, oldChild.parentNode, self)))
     202        if not oldChild.parentNode is self:
     203            raise ValueError("oldChild is not a child of this node")
    184204        self.childNodes[self.childNodes.index(oldChild)] = newChild
    185205        oldChild.parentNode = None
    186206        newChild.parentNode = self
     
    232252        return self.childNodes[0]
    233253    documentElement=property(get_documentElement)
    234254
    235     def appendChild(self, c):
     255    def appendChild(self, child):
    236256        assert not self.childNodes, "Only one element per document."
    237         Node.appendChild(self, c)
     257        Node.appendChild(self, child)
    238258
    239259    def writexml(self, stream, indent='', addindent='', newl='', strip=0,
    240260                 nsprefixes={}, namespace=''):