Ticket #3421: microdom.assert2.diff
| File microdom.assert2.diff, 4.4 KB (added by jparise, 4 years ago) |
|---|
-
twisted/web/test/test_xml.py
350 350 self.assertEquals(d.firstChild(), t) 351 351 self.assert_(d.isEqualToNode(d2)) 352 352 353 self.assertRaises(ValueError, d.replaceChild, t, d1) 354 353 355 def testSearch(self): 354 356 s = "<foo><bar id='me' /><baz><foo /></baz></foo>" 355 357 s2 = "<fOo><bAr id='me' /><bAz><fOO /></bAz></fOo>" … … 788 790 another.firstChild().appendChild(microdom.Node(object())) 789 791 self.assertTrue(node.isEqualToNode(another)) 790 792 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) 791 813 792 814 793 815 class DocumentTests(TestCase): -
twisted/web/microdom.py
158 158 return 0 159 159 160 160 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") 162 166 self.childNodes.append(child) 163 167 child.parentNode = self 164 168 165 169 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") 166 175 i = self.childNodes.index(ref) 167 176 new.parentNode = self 168 177 self.childNodes.insert(i, new) 169 178 return new 170 179 171 180 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") 172 186 if child in self.childNodes: 173 187 self.childNodes.remove(child) 174 188 child.parentNode = None 175 189 return child 176 190 177 191 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") 179 200 #if newChild.parentNode: 180 201 # 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") 184 204 self.childNodes[self.childNodes.index(oldChild)] = newChild 185 205 oldChild.parentNode = None 186 206 newChild.parentNode = self … … 232 252 return self.childNodes[0] 233 253 documentElement=property(get_documentElement) 234 254 235 def appendChild(self, c ):255 def appendChild(self, child): 236 256 assert not self.childNodes, "Only one element per document." 237 Node.appendChild(self, c )257 Node.appendChild(self, child) 238 258 239 259 def writexml(self, stream, indent='', addindent='', newl='', strip=0, 240 260 nsprefixes={}, namespace=''):
