Package lxml :: Package tests :: Module test_etree

[hide private]
[frames] | no frames]

# Source Code for Module lxml.tests.test_etree

       1  # -*- coding: utf-8 -*-
       2
       3  """
       4  Tests specific to the extended etree API
       5
       6  Tests that apply to the general ElementTree API should go into
       7  test_elementtree
       8  """
       9
      10  import os.path, unittest, copy, sys, operator, tempfile, gzip
      11
      12  this_dir = os.path.dirname(__file__)
      13  if this_dir not in sys.path:
      14      sys.path.insert(0, this_dir) # needed for Py3
      15
      16  from common_imports import etree, StringIO, BytesIO, HelperTestCase, fileInTestDir
      17  from common_imports import SillyFileLike, LargeFileLikeUnicode, doctest, make_doctest
      18  from common_imports import canonicalize, sorted, _str, _bytes
      19
      20  print("")
      21  print("TESTED VERSION: %s" % etree.__version__)
      22  print("    Python:           " + repr(sys.version_info))
      23  print("    lxml.etree:       " + repr(etree.LXML_VERSION))
      24  print("    libxml used:      " + repr(etree.LIBXML_VERSION))
      25  print("    libxml compiled:  " + repr(etree.LIBXML_COMPILED_VERSION))
      26  print("    libxslt used:     " + repr(etree.LIBXSLT_VERSION))
      27  print("    libxslt compiled: " + repr(etree.LIBXSLT_COMPILED_VERSION))
      28  print("")
      29
      30  try:
      31      _unicode = unicode
      32  except NameError:
      33      # Python 3
      34      _unicode = str
      35
      36 -class ETreeOnlyTestCase(HelperTestCase):
      37      """Tests only for etree, not ElementTree"""
      38      etree = etree
      39
      40 -    def test_version(self):
      41          self.assert_(isinstance(etree.__version__, _unicode))
      42          self.assert_(isinstance(etree.LXML_VERSION, tuple))
      43          self.assertEqual(len(etree.LXML_VERSION), 4)
      44          self.assert_(isinstance(etree.LXML_VERSION[0], int))
      45          self.assert_(isinstance(etree.LXML_VERSION[1], int))
      46          self.assert_(isinstance(etree.LXML_VERSION[2], int))
      47          self.assert_(isinstance(etree.LXML_VERSION[3], int))
      48          self.assert_(etree.__version__.startswith(
      49              str(etree.LXML_VERSION[0])))
      50
      51 -    def test_c_api(self):
      52          if hasattr(self.etree, '__pyx_capi__'):
      53              # newer Pyrex compatible C-API
      54              self.assert_(isinstance(self.etree.__pyx_capi__, dict))
      55              self.assert_(len(self.etree.__pyx_capi__) > 0)
      56          else:
      57              # older C-API mechanism
      58              self.assert_(hasattr(self.etree, '_import_c_api'))
      59
      60 -    def test_element_names(self):
      61          Element = self.etree.Element
      62          el = Element('name')
      63          self.assertEquals(el.tag, 'name')
      64          el = Element('{}name')
      65          self.assertEquals(el.tag, 'name')
      66
      67 -    def test_element_name_empty(self):
      68          Element = self.etree.Element
      69          el = Element('name')
      70          self.assertRaises(ValueError, Element, '{}')
      71          self.assertRaises(ValueError, setattr, el, 'tag', '{}')
      72
      73          self.assertRaises(ValueError, Element, '{test}')
      74          self.assertRaises(ValueError, setattr, el, 'tag', '{test}')
      75
      76 -    def test_element_name_colon(self):
      77          Element = self.etree.Element
      78          self.assertRaises(ValueError, Element, 'p:name')
      79          self.assertRaises(ValueError, Element, '{test}p:name')
      80
      81          el = Element('name')
      82          self.assertRaises(ValueError, setattr, el, 'tag', 'p:name')
      83
      84 -    def test_element_name_quote(self):
      85          Element = self.etree.Element
      86          self.assertRaises(ValueError, Element, "p'name")
      87          self.assertRaises(ValueError, Element, 'p"name')
      88
      89          self.assertRaises(ValueError, Element, "{test}p'name")
      90          self.assertRaises(ValueError, Element, '{test}p"name')
      91
      92          el = Element('name')
      93          self.assertRaises(ValueError, setattr, el, 'tag', "p'name")
      94          self.assertRaises(ValueError, setattr, el, 'tag', 'p"name')
      95
      96 -    def test_element_name_space(self):
      97          Element = self.etree.Element
      98          self.assertRaises(ValueError, Element, ' name ')
      99          self.assertRaises(ValueError, Element, 'na me')
     100          self.assertRaises(ValueError, Element, '{test} name')
     101
     102          el = Element('name')
     103          self.assertRaises(ValueError, setattr, el, 'tag', ' name ')
     104
     105 -    def test_subelement_name_empty(self):
     106          Element = self.etree.Element
     107          SubElement = self.etree.SubElement
     108
     109          el = Element('name')
     110          self.assertRaises(ValueError, SubElement, el, '{}')
     111          self.assertRaises(ValueError, SubElement, el, '{test}')
     112
     113 -    def test_subelement_name_colon(self):
     114          Element = self.etree.Element
     115          SubElement = self.etree.SubElement
     116
     117          el = Element('name')
     118          self.assertRaises(ValueError, SubElement, el, 'p:name')
     119          self.assertRaises(ValueError, SubElement, el, '{test}p:name')
     120
     121 -    def test_subelement_name_quote(self):
     122          Element = self.etree.Element
     123          SubElement = self.etree.SubElement
     124
     125          el = Element('name')
     126          self.assertRaises(ValueError, SubElement, el, "p'name")
     127          self.assertRaises(ValueError, SubElement, el, "{test}p'name")
     128
     129          self.assertRaises(ValueError, SubElement, el, 'p"name')
     130          self.assertRaises(ValueError, SubElement, el, '{test}p"name')
     131
     132 -    def test_subelement_name_space(self):
     133          Element = self.etree.Element
     134          SubElement = self.etree.SubElement
     135
     136          el = Element('name')
     137          self.assertRaises(ValueError, SubElement, el, ' name ')
     138          self.assertRaises(ValueError, SubElement, el, 'na me')
     139          self.assertRaises(ValueError, SubElement, el, '{test} name')
     140
     141 -    def test_subelement_attribute_invalid(self):
     142          Element = self.etree.Element
     143          SubElement = self.etree.SubElement
     144
     145          el = Element('name')
     146          self.assertRaises(ValueError, SubElement, el, 'name', {'a b c' : 'abc'})
     147          self.assertRaises(ValueError, SubElement, el, 'name', {'a' : 'a\0\n'})
     148          self.assertEquals(0, len(el))
     149
     150 -    def test_qname_empty(self):
     151          QName = self.etree.QName
     152          self.assertRaises(ValueError, QName, '')
     153          self.assertRaises(ValueError, QName, 'test', '')
     154
     155 -    def test_qname_colon(self):
     156          QName = self.etree.QName
     157          self.assertRaises(ValueError, QName, 'p:name')
     158          self.assertRaises(ValueError, QName, 'test', 'p:name')
     159
     160 -    def test_qname_space(self):
     161          QName = self.etree.QName
     162          self.assertRaises(ValueError, QName, ' name ')
     163          self.assertRaises(ValueError, QName, 'na me')
     164          self.assertRaises(ValueError, QName, 'test', ' name')
     165
     166 -    def test_qname_namespace_localname(self):
     167          # ET doesn't have namespace/localname properties on QNames
     168          QName = self.etree.QName
     169          namespace, localname = 'http://myns', 'a'
     170          qname = QName(namespace, localname)
     171          self.assertEquals(namespace, qname.namespace)
     172          self.assertEquals(localname, qname.localname)
     173
     174 -    def test_qname_element(self):
     175          # ET doesn't have namespace/localname properties on QNames
     176          QName = self.etree.QName
     177          qname1 = QName('http://myns', 'a')
     178          a = self.etree.Element(qname1, nsmap={'p' : 'http://myns'})
     179
     180          qname2 = QName(a)
     181          self.assertEquals(a.tag, qname1.text)
     182          self.assertEquals(qname1.text, qname2.text)
     183          self.assertEquals(qname1, qname2)
     184
     185 -    def test_qname_text_resolve(self):
     186          # ET doesn't resove QNames as text values
     187          etree = self.etree
     188          qname = etree.QName('http://myns', 'a')
     189          a = etree.Element(qname, nsmap={'p' : 'http://myns'})
     190          a.text = qname
     191
     192          self.assertEquals("p:a", a.text)
     193
     194 -    def test_nsmap_prefix_invalid(self):
     195          etree = self.etree
     196          self.assertRaises(ValueError,
     197                            etree.Element, "root", nsmap={'"' : 'testns'})
     198          self.assertRaises(ValueError,
     199                            etree.Element, "root", nsmap={'&' : 'testns'})
     200          self.assertRaises(ValueError,
     201                            etree.Element, "root", nsmap={'a:b' : 'testns'})
     202
     203 -    def test_attribute_set(self):
     204          Element = self.etree.Element
     205          root = Element("root")
     206          root.set("attr", "TEST")
     207          self.assertEquals("TEST", root.get("attr"))
     208
     209 -    def test_attribute_set_invalid(self):
     210          # ElementTree accepts arbitrary attribute values
     211          # lxml.etree allows only strings
     212          Element = self.etree.Element
     213          root = Element("root")
     214          self.assertRaises(TypeError, root.set, "newattr", 5)
     215          self.assertRaises(TypeError, root.set, "newattr", None)
     216
     217 -    def test_strip_attributes(self):
     218          XML = self.etree.XML
     219          xml = _bytes('<test a="5" b="10" c="20"><x a="4" b="2"/></test>')
     220
     221          root = XML(xml)
     222          self.etree.strip_attributes(root, 'a')
     223          self.assertEquals(_bytes('<test b="10" c="20"><x b="2"></x></test>'),
     224                            self._writeElement(root))
     225
     226          root = XML(xml)
     227          self.etree.strip_attributes(root, 'b', 'c')
     228          self.assertEquals(_bytes('<test a="5"><x a="4"></x></test>'),
     229                            self._writeElement(root))
     230
     231 -    def test_strip_attributes_ns(self):
     232          XML = self.etree.XML
     233          xml = _bytes('<test xmlns:n="http://test/ns" a="6" b="10" c="20" n:a="5"><x a="4" n:b="2"/></test>')
     234
     235          root = XML(xml)
     236          self.etree.strip_attributes(root, 'a')
     237          self.assertEquals(
     238              _bytes('<test xmlns:n="http://test/ns" b="10" c="20" n:a="5"><x n:b="2"></x></test>'),
     239              self._writeElement(root))
     240
     241          root = XML(xml)
     242          self.etree.strip_attributes(root, '{http://test/ns}a', 'c')
     243          self.assertEquals(
     244              _bytes('<test xmlns:n="http://test/ns" a="6" b="10"><x a="4" n:b="2"></x></test>'),
     245              self._writeElement(root))
     246
     247          root = XML(xml)
     248          self.etree.strip_attributes(root, '{http://test/ns}*')
     249          self.assertEquals(
     250              _bytes('<test xmlns:n="http://test/ns" a="6" b="10" c="20"><x a="4"></x></test>'),
     251              self._writeElement(root))
     252
     253 -    def test_strip_elements(self):
     254          XML = self.etree.XML
     255          xml = _bytes('<test><a><b><c/></b></a><x><a><b/><c/></a></x></test>')
     256
     257          root = XML(xml)
     258          self.etree.strip_elements(root, 'a')
     259          self.assertEquals(_bytes('<test><x></x></test>'),
     260                            self._writeElement(root))
     261
     262          root = XML(xml)
     263          self.etree.strip_elements(root, 'b', 'c', 'X', 'Y', 'Z')
     264          self.assertEquals(_bytes('<test><a></a><x><a></a></x></test>'),
     265                            self._writeElement(root))
     266
     267          root = XML(xml)
     268          self.etree.strip_elements(root, 'c')
     269          self.assertEquals(_bytes('<test><a><b></b></a><x><a><b></b></a></x></test>'),
     270                            self._writeElement(root))
     271
     272 -    def test_strip_elements_ns(self):
     273          XML = self.etree.XML
     274          xml = _bytes('<test>TEST<n:a xmlns:n="urn:a">A<b>B<c xmlns="urn:c"/>C</b>BT</n:a>AT<x>X<a>A<b xmlns="urn:a"/>BT<c xmlns="urn:x"/>CT</a>AT</x>XT</test>')
     275
     276          root = XML(xml)
     277          self.etree.strip_elements(root, 'a')
     278          self.assertEquals(_bytes('<test>TEST<n:a xmlns:n="urn:a">A<b>B<c xmlns="urn:c"></c>C</b>BT</n:a>AT<x>X</x>XT</test>'),
     279                            self._writeElement(root))
     280
     281          root = XML(xml)
     282          self.etree.strip_elements(root, '{urn:a}b', 'c')
     283          self.assertEquals(_bytes('<test>TEST<n:a xmlns:n="urn:a">A<b>B<c xmlns="urn:c"></c>C</b>BT</n:a>AT<x>X<a>A<c xmlns="urn:x"></c>CT</a>AT</x>XT</test>'),
     284                            self._writeElement(root))
     285
     286          root = XML(xml)
     287          self.etree.strip_elements(root, '{urn:a}*', 'c')
     288          self.assertEquals(_bytes('<test>TEST<x>X<a>A<c xmlns="urn:x"></c>CT</a>AT</x>XT</test>'),
     289                            self._writeElement(root))
     290
     291          root = XML(xml)
     292          self.etree.strip_elements(root, '{urn:a}*', 'c', with_tail=False)
     293          self.assertEquals(_bytes('<test>TESTAT<x>X<a>ABT<c xmlns="urn:x"></c>CT</a>AT</x>XT</test>'),
     294                            self._writeElement(root))
     295
     296 -    def test_strip_tags(self):
     297          XML = self.etree.XML
     298          xml = _bytes('<test>TEST<a>A<b>B<c/>CT</b>BT</a>AT<x>X<a>A<b/>BT<c/>CT</a>AT</x>XT</test>')
     299
     300          root = XML(xml)
     301          self.etree.strip_tags(root, 'a')
     302          self.assertEquals(_bytes('<test>TESTA<b>B<c></c>CT</b>BTAT<x>XA<b></b>BT<c></c>CTAT</x>XT</test>'),
     303                            self._writeElement(root))
     304
     305          root = XML(xml)
     306          self.etree.strip_tags(root, 'b', 'c', 'X', 'Y', 'Z')
     307          self.assertEquals(_bytes('<test>TEST<a>ABCTBT</a>AT<x>X<a>ABTCT</a>AT</x>XT</test>'),
     308                            self._writeElement(root))
     309
     310          root = XML(xml)
     311          self.etree.strip_tags(root, 'c')
     312          self.assertEquals(_bytes('<test>TEST<a>A<b>BCT</b>BT</a>AT<x>X<a>A<b></b>BTCT</a>AT</x>XT</test>'),
     313                            self._writeElement(root))
     314
     315 -    def test_strip_tags_ns(self):
     316          XML = self.etree.XML
     317          xml = _bytes('<test>TEST<n:a xmlns:n="urn:a">A<b>B<c xmlns="urn:c"/>CT</b>BT</n:a>AT<x>X<a>A<b xmlns="urn:a"/>BT<c xmlns="urn:x"/>CT</a>AT</x>XT</test>')
     318
     319          root = XML(xml)
     320          self.etree.strip_tags(root, 'a')
     321          self.assertEquals(_bytes('<test>TEST<n:a xmlns:n="urn:a">A<b>B<c xmlns="urn:c"></c>CT</b>BT</n:a>AT<x>XA<b xmlns="urn:a"></b>BT<c xmlns="urn:x"></c>CTAT</x>XT</test>'),
     322                            self._writeElement(root))
     323
     324          root = XML(xml)
     325          self.etree.strip_tags(root, '{urn:a}b', 'c')
     326          self.assertEquals(_bytes('<test>TEST<n:a xmlns:n="urn:a">A<b>B<c xmlns="urn:c"></c>CT</b>BT</n:a>AT<x>X<a>ABT<c xmlns="urn:x"></c>CT</a>AT</x>XT</test>'),
     327                            self._writeElement(root))
     328
     329          root = XML(xml)
     330          self.etree.strip_tags(root, '{urn:a}*', 'c')
     331          self.assertEquals(_bytes('<test>TESTA<b>B<c xmlns="urn:c"></c>CT</b>BTAT<x>X<a>ABT<c xmlns="urn:x"></c>CT</a>AT</x>XT</test>'),
     332                            self._writeElement(root))
     333
     334 -    def test_pi(self):
     335          # lxml.etree separates target and text
     336          Element = self.etree.Element
     337          SubElement = self.etree.SubElement
     338          ProcessingInstruction = self.etree.ProcessingInstruction
     339
     340          a = Element('a')
     341          a.append(ProcessingInstruction('foo', 'some more text'))
     342          self.assertEquals(a[0].target, 'foo')
     343          self.assertEquals(a[0].text, 'some more text')
     344
     345 -    def test_pi_parse(self):
     346          XML = self.etree.XML
     347          root = XML(_bytes("<test><?mypi my test ?></test>"))
     348          self.assertEquals(root[0].target, "mypi")
     349          self.assertEquals(root[0].text, "my test ")
     350
     351 -    def test_deepcopy_pi(self):
     352          # previously caused a crash
     353          ProcessingInstruction = self.etree.ProcessingInstruction
     354
     355          a = ProcessingInstruction("PI", "ONE")
     356          b = copy.deepcopy(a)
     357          b.text = "ANOTHER"
     358
     359          self.assertEquals('ONE',     a.text)
     360          self.assertEquals('ANOTHER', b.text)
     361
     362 -    def test_deepcopy_elementtree_pi(self):
     363          XML = self.etree.XML
     364          tostring = self.etree.tostring
     365          root = XML(_bytes("<?mypi my test ?><test/><!--comment -->"))
     366          tree1 = self.etree.ElementTree(root)
     367          self.assertEquals(_bytes("<?mypi my test ?><test/><!--comment -->"),
     368                            tostring(tree1))
     369
     370          tree2 = copy.deepcopy(tree1)
     371          self.assertEquals(_bytes("<?mypi my test ?><test/><!--comment -->"),
     372                            tostring(tree2))
     373
     374          root2 = copy.deepcopy(tree1.getroot())
     375          self.assertEquals(_bytes("<test/>"),
     376                            tostring(root2))
     377
     378 -    def test_deepcopy_elementtree_dtd(self):
     379          XML = self.etree.XML
     380          tostring = self.etree.tostring
     381          xml = _bytes('<!DOCTYPE test [\n<!ENTITY entity "tasty">\n]>\n<test/>')
     382          root = XML(xml)
     383          tree1 = self.etree.ElementTree(root)
     384          self.assertEquals(xml, tostring(tree1))
     385
     386          tree2 = copy.deepcopy(tree1)
     387          self.assertEquals(xml, tostring(tree2))
     388
     389          root2 = copy.deepcopy(tree1.getroot())
     390          self.assertEquals(_bytes("<test/>"),
     391                            tostring(root2))
     392
     393 -    def test_attribute_set(self):
     394          # ElementTree accepts arbitrary attribute values
     395          # lxml.etree allows only strings
     396          Element = self.etree.Element
     397
     398          root = Element("root")
     399          root.set("attr", "TEST")
     400          self.assertEquals("TEST", root.get("attr"))
     401          self.assertRaises(TypeError, root.set, "newattr", 5)
     402
     403 -    def test_parse_remove_comments(self):
     404          fromstring = self.etree.fromstring
     405          tostring = self.etree.tostring
     406          XMLParser = self.etree.XMLParser
     407
     408          xml = _bytes('<a><!--A--><b><!-- B --><c/></b><!--C--></a>')
     409          parser = XMLParser(remove_comments=True)
     410          root = fromstring(xml, parser)
     411          self.assertEquals(
     412              _bytes('<a><b><c/></b></a>'),
     413              tostring(root))
     414
     415 -    def test_parse_remove_pis(self):
     416          parse = self.etree.parse
     417          tostring = self.etree.tostring
     418          XMLParser = self.etree.XMLParser
     419
     420          xml = _bytes('<?test?><a><?A?><b><?B?><c/></b><?C?></a><?tail?>')
     421
     422          f = BytesIO(xml)
     423          tree = parse(f)
     424          self.assertEquals(
     425              xml,
     426              tostring(tree))
     427
     428          parser = XMLParser(remove_pis=True)
     429          tree = parse(f, parser)
     430          self.assertEquals(
     431              _bytes('<a><b><c/></b></a>'),
     432              tostring(tree))
     433
     434 -    def test_parse_parser_type_error(self):
     435          # ET raises IOError only
     436          parse = self.etree.parse
     437          self.assertRaises(TypeError, parse, 'notthere.xml', object())
     438
     439 -    def test_iterparse_tree_comments(self):
     440          # ET removes comments
     441          iterparse = self.etree.iterparse
     442          tostring = self.etree.tostring
     443
     444          f = BytesIO('<a><!--A--><b><!-- B --><c/></b><!--C--></a>')
     445          events = list(iterparse(f))
     446          root = events[-1][1]
     447          self.assertEquals(3, len(events))
     448          self.assertEquals(
     449              _bytes('<a><!--A--><b><!-- B --><c/></b><!--C--></a>'),
     450              tostring(root))
     451
     452 -    def test_iterparse_comments(self):
     453          # ET removes comments
     454          iterparse = self.etree.iterparse
     455          tostring = self.etree.tostring
     456
     457          def name(event, el):
     458              if event == 'comment':
     459                  return el.text
     460              else:
     461                  return el.tag
     462
     463          f = BytesIO('<a><!--A--><b><!-- B --><c/></b><!--C--></a>')
     464          events = list(iterparse(f, events=('end', 'comment')))
     465          root = events[-1][1]
     466          self.assertEquals(6, len(events))
     467          self.assertEquals(['A', ' B ', 'c', 'b', 'C', 'a'],
     468                            [ name(*item) for item in events ])
     469          self.assertEquals(
     470              _bytes('<a><!--A--><b><!-- B --><c/></b><!--C--></a>'),
     471              tostring(root))
     472
     473 -    def test_iterparse_pis(self):
     474          # ET removes pis
     475          iterparse = self.etree.iterparse
     476          tostring = self.etree.tostring
     477          ElementTree = self.etree.ElementTree
     478
     479          def name(event, el):
     480              if event == 'pi':
     481                  return (el.target, el.text)
     482              else:
     483                  return el.tag
     484
     485          f = BytesIO('<?pia a?><a><?pib b?><b><?pic c?><c/></b><?pid d?></a><?pie e?>')
     486          events = list(iterparse(f, events=('end', 'pi')))
     487          root = events[-2][1]
     488          self.assertEquals(8, len(events))
     489          self.assertEquals([('pia','a'), ('pib','b'), ('pic','c'), 'c', 'b',
     490                             ('pid','d'), 'a', ('pie','e')],
     491                            [ name(*item) for item in events ])
     492          self.assertEquals(
     493              _bytes('<?pia a?><a><?pib b?><b><?pic c?><c/></b><?pid d?></a><?pie e?>'),
     494              tostring(ElementTree(root)))
     495
     496 -    def test_iterparse_remove_comments(self):
     497          iterparse = self.etree.iterparse
     498          tostring = self.etree.tostring
     499
     500          f = BytesIO('<a><!--A--><b><!-- B --><c/></b><!--C--></a>')
     501          events = list(iterparse(f, remove_comments=True,
     502                                  events=('end', 'comment')))
     503          root = events[-1][1]
     504          self.assertEquals(3, len(events))
     505          self.assertEquals(['c', 'b', 'a'],
     506                            [ el.tag for (event, el) in events ])
     507          self.assertEquals(
     508              _bytes('<a><b><c/></b></a>'),
     509              tostring(root))
     510
     511 -    def test_iterparse_broken(self):
     512          iterparse = self.etree.iterparse
     513          f = BytesIO('<a><b><c/></a>')
     514          # ET raises ExpatError, lxml raises XMLSyntaxError
     515          self.assertRaises(self.etree.XMLSyntaxError, list, iterparse(f))
     516
     517 -    def test_iterparse_strip(self):
     518          iterparse = self.etree.iterparse
     519          f = BytesIO("""
     520                 <a>  \n \n  <b> b test </b>  \n
     521
     522                 \n\t <c> \n </c> </a>  \n """)
     523          iterator = iterparse(f, remove_blank_text=True)
     524          text = [ (element.text, element.tail)
     525                   for event, element in iterator ]
     526          self.assertEquals(
     527              [(" b test ", None), (" \n ", None), (None, None)],
     528              text)
     529
     530 -    def test_iterparse_tag(self):
     531          iterparse = self.etree.iterparse
     532          f = BytesIO('<a><b><d/></b><c/></a>')
     533
     534          iterator = iterparse(f, tag="b", events=('start', 'end'))
     535          events = list(iterator)
     536          root = iterator.root
     537          self.assertEquals(
     538              [('start', root[0]), ('end', root[0])],
     539              events)
     540
     541 -    def test_iterparse_tag_all(self):
     542          iterparse = self.etree.iterparse
     543          f = BytesIO('<a><b><d/></b><c/></a>')
     544
     545          iterator = iterparse(f, tag="*", events=('start', 'end'))
     546          events = list(iterator)
     547          self.assertEquals(
     548              8,
     549              len(events))
     550
     551 -    def test_iterparse_encoding_error(self):
     552          text = _str('Søk på nettet')
     553          wrong_declaration = "<?xml version='1.0' encoding='UTF-8'?>"
     554          xml_latin1 = (_str('%s<a>%s</a>') % (wrong_declaration, text)
     555                        ).encode('iso-8859-1')
     556
     557          self.assertRaises(self.etree.ParseError,
     558                            list, self.etree.iterparse(BytesIO(xml_latin1)))
     559
     560 -    def test_iterparse_encoding_8bit_override(self):
     561          text = _str('Søk på nettet', encoding="UTF-8")
     562          wrong_declaration = "<?xml version='1.0' encoding='UTF-8'?>"
     563          xml_latin1 = (_str('%s<a>%s</a>') % (wrong_declaration, text)
     564                        ).encode('iso-8859-1')
     565
     566          iterator = self.etree.iterparse(BytesIO(xml_latin1),
     567                                          encoding="iso-8859-1")
     568          self.assertEquals(1, len(list(iterator)))
     569
     570          a = iterator.root
     571          self.assertEquals(a.text, text)
     572
     573 -    def test_iterparse_keep_cdata(self):
     574          tostring = self.etree.tostring
     575          f = BytesIO('<root><![CDATA[test]]></root>')
     576          context = self.etree.iterparse(f, strip_cdata=False)
     577          content = [ el.text for event,el in context ]
     578
     579          self.assertEquals(['test'], content)
     580          self.assertEquals(_bytes('<root><![CDATA[test]]></root>'),
     581                            tostring(context.root))
     582
     583 -    def test_parser_encoding_unknown(self):
     584          self.assertRaises(
     585              LookupError, self.etree.XMLParser, encoding="hopefully unknown")
     586
     587 -    def test_parser_encoding(self):
     588          self.etree.XMLParser(encoding="ascii")
     589          self.etree.XMLParser(encoding="utf-8")
     590          self.etree.XMLParser(encoding="iso-8859-1")
     591
     592 -    def test_feed_parser_recover(self):
     593          parser = self.etree.XMLParser(recover=True)
     594
     595          parser.feed('<?xml version=')
     596          parser.feed('"1.0"?><ro')
     597          parser.feed('ot><')
     598          parser.feed('a test="works"')
     599          parser.feed('><othertag/></root') # <a> not closed!
     600          parser.feed('>')
     601
     602          root = parser.close()
     603
     604          self.assertEquals(root.tag, "root")
     605          self.assertEquals(len(root), 1)
     606          self.assertEquals(root[0].tag, "a")
     607          self.assertEquals(root[0].get("test"), "works")
     608          self.assertEquals(len(root[0]), 1)
     609          self.assertEquals(root[0][0].tag, "othertag")
     610          # FIXME: would be nice to get some errors logged ...
     611          #self.assert_(len(parser.error_log) > 0, "error log is empty")
     612
     613 -    def test_elementtree_parser_target_type_error(self):
     614          assertEquals = self.assertEquals
     615          assertFalse  = self.assertFalse
     616
     617          events = []
     618          class Target(object):
     619              def start(self, tag, attrib):
     620                  events.append("start")
     621                  assertFalse(attrib)
     622                  assertEquals("TAG", tag)
     623              def end(self, tag):
     624                  events.append("end")
     625                  assertEquals("TAG", tag)
     626              def close(self):
     627                  return "DONE" # no Element!
     628
     629          parser = self.etree.XMLParser(target=Target())
     630          tree = self.etree.ElementTree()
     631
     632          self.assertRaises(TypeError,
     633                            tree.parse, BytesIO("<TAG/>"), parser=parser)
     634          self.assertEquals(["start", "end"], events)
     635
     636 -    def test_parser_target_comment(self):
     637          events = []
     638          class Target(object):
     639              def start(self, tag, attrib):
     640                  events.append("start-" + tag)
     641              def end(self, tag):
     642                  events.append("end-" + tag)
     643              def data(self, data):
     644                  events.append("data-" + data)
     645              def comment(self, text):
     646                  events.append("comment-" + text)
     647              def close(self):
     648                  return "DONE"
     649
     650          parser = self.etree.XMLParser(target=Target())
     651
     652          parser.feed(_bytes('<!--a--><root>A<!--b--><sub/><!--c-->B</root><!--d-->'))
     653          done = parser.close()
     654
     655          self.assertEquals("DONE", done)
     656          self.assertEquals(["comment-a", "start-root", "data-A", "comment-b",
     657                             "start-sub", "end-sub", "comment-c", "data-B",
     658                             "end-root", "comment-d"],
     659                            events)
     660
     661 -    def test_parser_target_pi(self):
     662          events = []
     663          class Target(object):
     664              def start(self, tag, attrib):
     665                  events.append("start-" + tag)
     666              def end(self, tag):
     667                  events.append("end-" + tag)
     668              def data(self, data):
     669                  events.append("data-" + data)
     670              def pi(self, target, data):
     671                  events.append("pi-" + target + "-" + data)
     672              def close(self):
     673                  return "DONE"
     674
     675          parser = self.etree.XMLParser(target=Target())
     676
     677          parser.feed(_bytes('<?test a?><root>A<?test b?>B</root><?test c?>'))
     678          done = parser.close()
     679
     680          self.assertEquals("DONE", done)
     681          self.assertEquals(["pi-test-a", "start-root", "data-A", "pi-test-b",
     682                             "data-B", "end-root", "pi-test-c"],
     683                            events)
     684
     685 -    def test_parser_target_cdata(self):
     686          events = []
     687          class Target(object):
     688              def start(self, tag, attrib):
     689                  events.append("start-" + tag)
     690              def end(self, tag):
     691                  events.append("end-" + tag)
     692              def data(self, data):
     693                  events.append("data-" + data)
     694              def close(self):
     695                  return "DONE"
     696
     697          parser = self.etree.XMLParser(target=Target(),
     698                                        strip_cdata=False)
     699
     700          parser.feed(_bytes('<root>A<a><![CDATA[ca]]></a>B</root>'))
     701          done = parser.close()
     702
     703          self.assertEquals("DONE", done)
     704          self.assertEquals(["start-root", "data-A", "start-a",
     705                             "data-ca", "end-a", "data-B", "end-root"],
     706                            events)
     707
     708 -    def test_parser_target_recover(self):
     709          events = []
     710          class Target(object):
     711              def start(self, tag, attrib):
     712                  events.append("start-" + tag)
     713              def end(self, tag):
     714                  events.append("end-" + tag)
     715              def data(self, data):
     716                  events.append("data-" + data)
     717              def close(self):
     718                  events.append("close")
     719                  return "DONE"
     720
     721          parser = self.etree.XMLParser(target=Target(),
     722                                        recover=True)
     723
     724          parser.feed(_bytes('<root>A<a>ca</a>B</not-root>'))
     725          done = parser.close()
     726
     727          self.assertEquals("DONE", done)
     728          self.assertEquals(["start-root", "data-A", "start-a",
     729                             "data-ca", "end-a", "data-B",
     730                             "end-root", "close"],
     731                            events)
     732
     733 -    def test_iterwalk_tag(self):
     734          iterwalk = self.etree.iterwalk
     735          root = self.etree.XML(_bytes('<a><b><d/></b><c/></a>'))
     736
     737          iterator = iterwalk(root, tag="b", events=('start', 'end'))
     738          events = list(iterator)
     739          self.assertEquals(
     740              [('start', root[0]), ('end', root[0])],
     741              events)
     742
     743 -    def test_iterwalk_tag_all(self):
     744          iterwalk = self.etree.iterwalk
     745          root = self.etree.XML(_bytes('<a><b><d/></b><c/></a>'))
     746
     747          iterator = iterwalk(root, tag="*", events=('start', 'end'))
     748          events = list(iterator)
     749          self.assertEquals(
     750              8,
     751              len(events))
     752
     753 -    def test_iterwalk(self):
     754          iterwalk = self.etree.iterwalk
     755          root = self.etree.XML(_bytes('<a><b></b><c/></a>'))
     756
     757          events = list(iterwalk(root))
     758          self.assertEquals(
     759              [('end', root[0]), ('end', root[1]), ('end', root)],
     760              events)
     761
     762 -    def test_iterwalk_start(self):
     763          iterwalk = self.etree.iterwalk
     764          root = self.etree.XML(_bytes('<a><b></b><c/></a>'))
     765
     766          iterator = iterwalk(root, events=('start',))
     767          events = list(iterator)
     768          self.assertEquals(
     769              [('start', root), ('start', root[0]), ('start', root[1])],
     770              events)
     771
     772 -    def test_iterwalk_start_end(self):
     773          iterwalk = self.etree.iterwalk
     774          root = self.etree.XML(_bytes('<a><b></b><c/></a>'))
     775
     776          iterator = iterwalk(root, events=('start','end'))
     777          events = list(iterator)
     778          self.assertEquals(
     779              [('start', root), ('start', root[0]), ('end', root[0]),
     780               ('start', root[1]), ('end', root[1]), ('end', root)],
     781              events)
     782
     783 -    def test_iterwalk_clear(self):
     784          iterwalk = self.etree.iterwalk
     785          root = self.etree.XML(_bytes('<a><b></b><c/></a>'))
     786
     787          iterator = iterwalk(root)
     788          for event, elem in iterator:
     789              elem.clear()
     790
     791          self.assertEquals(0,
     792                            len(root))
     793
     794 -    def test_iterwalk_attrib_ns(self):
     795          iterwalk = self.etree.iterwalk
     796          root = self.etree.XML(_bytes('<a xmlns="ns1"><b><c xmlns="ns2"/></b></a>'))
     797
     798          attr_name = '{testns}bla'
     799          events = []
     800          iterator = iterwalk(root, events=('start','end','start-ns','end-ns'))
     801          for event, elem in iterator:
     802              events.append(event)
     803              if event == 'start':
     804                  if elem.tag != '{ns1}a':
     805                      elem.set(attr_name, 'value')
     806
     807          self.assertEquals(
     808              ['start-ns', 'start', 'start', 'start-ns', 'start',
     809               'end', 'end-ns', 'end', 'end', 'end-ns'],
     810              events)
     811
     812          self.assertEquals(
     813              None,
     814              root.get(attr_name))
     815          self.assertEquals(
     816              'value',
     817              root[0].get(attr_name))
     818
     819 -    def test_iterwalk_getiterator(self):
     820          iterwalk = self.etree.iterwalk
     821          root = self.etree.XML(_bytes('<a><b><d/></b><c/></a>'))
     822
     823          counts = []
     824          for event, elem in iterwalk(root):
     825              counts.append(len(list(elem.getiterator())))
     826          self.assertEquals(
     827              [1,2,1,4],
     828              counts)
     829
     830 -    def test_resolve_string_dtd(self):
     831          parse = self.etree.parse
     832          parser = self.etree.XMLParser(dtd_validation=True)
     833          assertEqual = self.assertEqual
     834          test_url = _str("__nosuch.dtd")
     835
     836          class MyResolver(self.etree.Resolver):
     837              def resolve(self, url, id, context):
     838                  assertEqual(url, test_url)
     839                  return self.resolve_string(
     840                      _str('''<!ENTITY myentity "%s">
     841                          <!ELEMENT doc ANY>''') % url, context)
     842
     843          parser.resolvers.add(MyResolver())
     844
     845          xml = _str('<!DOCTYPE doc SYSTEM "%s"><doc>&myentity;</doc>') % test_url
     846          tree = parse(StringIO(xml), parser)
     847          root = tree.getroot()
     848          self.assertEquals(root.text, test_url)
     849
     850 -    def test_resolve_bytes_dtd(self):
     851          parse = self.etree.parse
     852          parser = self.etree.XMLParser(dtd_validation=True)
     853          assertEqual = self.assertEqual
     854          test_url = _str("__nosuch.dtd")
     855
     856          class MyResolver(self.etree.Resolver):
     857              def resolve(self, url, id, context):
     858                  assertEqual(url, test_url)
     859                  return self.resolve_string(
     860                      (_str('''<!ENTITY myentity "%s">
     861                               <!ELEMENT doc ANY>''') % url).encode('utf-8'),
     862                      context)
     863
     864          parser.resolvers.add(MyResolver())
     865
     866          xml = _str('<!DOCTYPE doc SYSTEM "%s"><doc>&myentity;</doc>') % test_url
     867          tree = parse(StringIO(xml), parser)
     868          root = tree.getroot()
     869          self.assertEquals(root.text, test_url)
     870
     871 -    def test_resolve_filelike_dtd(self):
     872          parse = self.etree.parse
     873          parser = self.etree.XMLParser(dtd_validation=True)
     874          assertEqual = self.assertEqual
     875          test_url = _str("__nosuch.dtd")
     876
     877          class MyResolver(self.etree.Resolver):
     878              def resolve(self, url, id, context):
     879                  assertEqual(url, test_url)
     880                  return self.resolve_file(
     881                      SillyFileLike(
     882                          _str('''<!ENTITY myentity "%s">
     883                          <!ELEMENT doc ANY>''') % url), context)
     884
     885          parser.resolvers.add(MyResolver())
     886
     887          xml = _str('<!DOCTYPE doc SYSTEM "%s"><doc>&myentity;</doc>') % test_url
     888          tree = parse(StringIO(xml), parser)
     889          root = tree.getroot()
     890          self.assertEquals(root.text, test_url)
     891
     892 -    def test_resolve_filename_dtd(self):
     893          parse = self.etree.parse
     894          parser = self.etree.XMLParser(attribute_defaults=True)
     895          assertEqual = self.assertEqual
     896          test_url = _str("__nosuch.dtd")
     897
     898          class MyResolver(self.etree.Resolver):
     899              def resolve(self, url, id, context):
     900                  assertEqual(url, test_url)
     901                  return self.resolve_filename(
     902                      fileInTestDir('test.dtd'), context)
     903
     904          parser.resolvers.add(MyResolver())
     905
     906          xml = _str('<!DOCTYPE a SYSTEM "%s"><a><b/></a>') % test_url
     907          tree = parse(StringIO(xml), parser)
     908          root = tree.getroot()
     909          self.assertEquals(
     910              root.attrib,    {'default': 'valueA'})
     911          self.assertEquals(
     912              root[0].attrib, {'default': 'valueB'})
     913
     914 -    def test_resolve_filename_dtd_relative(self):
     915          parse = self.etree.parse
     916          parser = self.etree.XMLParser(attribute_defaults=True)
     917          assertEqual = self.assertEqual
     918          test_url = _str("__nosuch.dtd")
     919
     920          class MyResolver(self.etree.Resolver):
     921              def resolve(self, url, id, context):
     922                  assertEqual(url, fileInTestDir(test_url))
     923                  return self.resolve_filename(
     924                      fileInTestDir('test.dtd'), context)
     925
     926          parser.resolvers.add(MyResolver())
     927
     928          xml = _str('<!DOCTYPE a SYSTEM "%s"><a><b/></a>') % test_url
     929          tree = parse(StringIO(xml), parser,
     930                       base_url=fileInTestDir('__test.xml'))
     931          root = tree.getroot()
     932          self.assertEquals(
     933              root.attrib,    {'default': 'valueA'})
     934          self.assertEquals(
     935              root[0].attrib, {'default': 'valueB'})
     936
     937 -    def test_resolve_file_dtd(self):
     938          parse = self.etree.parse
     939          parser = self.etree.XMLParser(attribute_defaults=True)
     940          assertEqual = self.assertEqual
     941          test_url = _str("__nosuch.dtd")
     942
     943          class MyResolver(self.etree.Resolver):
     944              def resolve(self, url, id, context):
     945                  assertEqual(url, test_url)
     946                  return self.resolve_file(
     947                      open(fileInTestDir('test.dtd'), 'rb'), context)
     948
     949          parser.resolvers.add(MyResolver())
     950
     951          xml = _str('<!DOCTYPE a SYSTEM "%s"><a><b/></a>') % test_url
     952          tree = parse(StringIO(xml), parser)
     953          root = tree.getroot()
     954          self.assertEquals(
     955              root.attrib,    {'default': 'valueA'})
     956          self.assertEquals(
     957              root[0].attrib, {'default': 'valueB'})
     958
     959 -    def test_resolve_empty(self):
     960          parse = self.etree.parse
     961          parser = self.etree.XMLParser(load_dtd=True)
     962          assertEqual = self.assertEqual
     963          test_url = _str("__nosuch.dtd")
     964
     965          class check(object):
     966              resolved = False
     967
     968          class MyResolver(self.etree.Resolver):
     969              def resolve(self, url, id, context):
     970                  assertEqual(url, test_url)
     971                  check.resolved = True
     972                  return self.resolve_empty(context)
     973
     974          parser.resolvers.add(MyResolver())
     975
     976          xml = _str('<!DOCTYPE doc SYSTEM "%s"><doc>&myentity;</doc>') % test_url
     977          self.assertRaises(etree.XMLSyntaxError, parse, StringIO(xml), parser)
     978          self.assert_(check.resolved)
     979
     980 -    def test_resolve_error(self):
     981          parse = self.etree.parse
     982          parser = self.etree.XMLParser(dtd_validation=True)
     983
     984          class _LocalException(Exception):
     985              pass
     986
     987          class MyResolver(self.etree.Resolver):
     988              def resolve(self, url, id, context):
     989                  raise _LocalException
     990
     991          parser.resolvers.add(MyResolver())
     992
     993          xml = '<!DOCTYPE doc SYSTEM "test"><doc>&myentity;</doc>'
     994          self.assertRaises(_LocalException, parse, BytesIO(xml), parser)
     995
     996      if etree.LIBXML_VERSION > (2,6,20):
     997 -        def test_entity_parse(self):
     998              parse = self.etree.parse
     999              tostring = self.etree.tostring
    1000              parser = self.etree.XMLParser(resolve_entities=False)
    1001              Entity = self.etree.Entity
    1002
    1003              xml = _bytes('<!DOCTYPE doc SYSTEM "test"><doc>&myentity;</doc>')
    1004              tree = parse(BytesIO(xml), parser)
    1005              root = tree.getroot()
    1006              self.assertEquals(root[0].tag, Entity)
    1007              self.assertEquals(root[0].text, "&myentity;")
    1008              self.assertEquals(root[0].tail, None)
    1009              self.assertEquals(root[0].name, "myentity")
    1010
    1011              self.assertEquals(_bytes('<doc>&myentity;</doc>'),
    1012                                tostring(root))
    1013
    1014 -        def test_entity_restructure(self):
    1015              xml = _bytes('''<!DOCTYPE root [ <!ENTITY nbsp "&#160;"> ]>
    1016                  <root>
    1017                    <child1/>
    1018                    <child2/>
    1019                    <child3>&nbsp;</child3>
    1020                  </root>''')
    1021
    1022              parser = self.etree.XMLParser(resolve_entities=False)
    1023              root = etree.fromstring(xml, parser)
    1024              self.assertEquals([ el.tag for el in root ],
    1025                                ['child1', 'child2', 'child3'])
    1026
    1027              root[0] = root[-1]
    1028              self.assertEquals([ el.tag for el in root ],
    1029                                ['child3', 'child2'])
    1030              self.assertEquals(root[0][0].text, '&nbsp;')
    1031              self.assertEquals(root[0][0].name, 'nbsp')
    1032
    1033 -    def test_entity_append(self):
    1034          Entity = self.etree.Entity
    1035          Element = self.etree.Element
    1036          tostring = self.etree.tostring
    1037
    1038          root = Element("root")
    1039          root.append( Entity("test") )
    1040
    1041          self.assertEquals(root[0].tag, Entity)
    1042          self.assertEquals(root[0].text, "&test;")
    1043          self.assertEquals(root[0].tail, None)
    1044          self.assertEquals(root[0].name, "test")
    1045
    1046          self.assertEquals(_bytes('<root>&test;</root>'),
    1047                            tostring(root))
    1048
    1049 -    def test_entity_values(self):
    1050          Entity = self.etree.Entity
    1051          self.assertEquals(Entity("test").text, '&test;')
    1052          self.assertEquals(Entity("#17683").text, '&#17683;')
    1053          self.assertEquals(Entity("#x1768").text, '&#x1768;')
    1054          self.assertEquals(Entity("#x98AF").text, '&#x98AF;')
    1055
    1056 -    def test_entity_error(self):
    1057          Entity = self.etree.Entity
    1058          self.assertRaises(ValueError, Entity, 'a b c')
    1059          self.assertRaises(ValueError, Entity, 'a,b')
    1060          self.assertRaises(ValueError, Entity, 'a\0b')
    1061          self.assertRaises(ValueError, Entity, '#abc')
    1062          self.assertRaises(ValueError, Entity, '#xxyz')
    1063
    1064 -    def test_cdata(self):
    1065          CDATA = self.etree.CDATA
    1066          Element = self.etree.Element
    1067          tostring = self.etree.tostring
    1068
    1069          root = Element("root")
    1070          root.text = CDATA('test')
    1071
    1072          self.assertEquals('test',
    1073                            root.text)
    1074          self.assertEquals(_bytes('<root><![CDATA[test]]></root>'),
    1075                            tostring(root))
    1076
    1077 -    def test_cdata_type(self):
    1078          CDATA = self.etree.CDATA
    1079          Element = self.etree.Element
    1080          root = Element("root")
    1081
    1082          root.text = CDATA("test")
    1083          self.assertEquals('test', root.text)
    1084
    1085          root.text = CDATA(_str("test"))
    1086          self.assertEquals('test', root.text)
    1087
    1088          self.assertRaises(TypeError, CDATA, 1)
    1089
    1090 -    def test_cdata_errors(self):
    1091          CDATA = self.etree.CDATA
    1092          Element = self.etree.Element
    1093
    1094          root = Element("root")
    1095          cdata = CDATA('test')
    1096
    1097          self.assertRaises(TypeError,
    1098                            setattr, root, 'tail', cdata)
    1099          self.assertRaises(TypeError,
    1100                            root.set, 'attr', cdata)
    1101          self.assertRaises(TypeError,
    1102                            operator.setitem, root.attrib, 'attr', cdata)
    1103
    1104 -    def test_cdata_parser(self):
    1105          tostring = self.etree.tostring
    1106          parser = self.etree.XMLParser(strip_cdata=False)
    1107          root = self.etree.XML(_bytes('<root><![CDATA[test]]></root>'), parser)
    1108
    1109          self.assertEquals('test', root.text)
    1110          self.assertEquals(_bytes('<root><![CDATA[test]]></root>'),
    1111                            tostring(root))
    1112
    1113 -    def test_cdata_xpath(self):
    1114          tostring = self.etree.tostring
    1115          parser = self.etree.XMLParser(strip_cdata=False)
    1116          root = self.etree.XML(_bytes('<root><![CDATA[test]]></root>'), parser)
    1117          self.assertEquals(_bytes('<root><![CDATA[test]]></root>'),
    1118                            tostring(root))
    1119
    1120          self.assertEquals(['test'], root.xpath('//text()'))
    1121
    1122      # TypeError in etree, AssertionError in ElementTree;
    1123 -    def test_setitem_assert(self):
    1124          Element = self.etree.Element
    1125          SubElement = self.etree.SubElement
    1126
    1127          a = Element('a')
    1128          b = SubElement(a, 'b')
    1129
    1130          self.assertRaises(TypeError,
    1131                            a.__setitem__, 0, 'foo')
    1132
    1133 -    def test_append_None(self):
    1134          # raises AssertionError in ElementTree
    1135          Element = self.etree.Element
    1136          self.assertRaises(TypeError, Element('a').append, None)
    1137
    1138 -    def test_addnext(self):
    1139          Element = self.etree.Element
    1140          SubElement = self.etree.SubElement
    1141          root = Element('root')
    1142          SubElement(root, 'a')
    1143          SubElement(root, 'b')
    1144
    1145          self.assertEquals(['a', 'b'],
    1146                            [c.tag for c in root])
    1147          root[1].addnext(root[0])
    1148          self.assertEquals(['b', 'a'],
    1149                            [c.tag for c in root])
    1150
    1151 -    def test_addprevious(self):
    1152          Element = self.etree.Element
    1153          SubElement = self.etree.SubElement
    1154          root = Element('root')
    1155          SubElement(root, 'a')
    1156          SubElement(root, 'b')
    1157
    1158          self.assertEquals(['a', 'b'],
    1159                            [c.tag for c in root])
    1160          root[0].addprevious(root[1])
    1161          self.assertEquals(['b', 'a'],
    1162                            [c.tag for c in root])
    1163
    1164 -    def test_addnext_root(self):
    1165          Element = self.etree.Element
    1166          a = Element('a')
    1167          b = Element('b')
    1168          self.assertRaises(TypeError, a.addnext, b)
    1169
    1170 -    def test_addnext_root(self):
    1171          Element = self.etree.Element
    1172          a = Element('a')
    1173          b = Element('b')
    1174          self.assertRaises(TypeError, a.addnext, b)
    1175
    1176 -    def test_addprevious_pi(self):
    1177          Element = self.etree.Element
    1178          SubElement = self.etree.SubElement
    1179          PI = self.etree.PI
    1180          root = Element('root')
    1181          SubElement(root, 'a')
    1182          pi = PI('TARGET', 'TEXT')
    1183          pi.tail = "TAIL"
    1184
    1185          self.assertEquals(_bytes('<root><a></a></root>'),
    1186                            self._writeElement(root))
    1187          root[0].addprevious(pi)
    1188          self.assertEquals(_bytes('<root><?TARGET TEXT?>TAIL<a></a></root>'),
    1189                            self._writeElement(root))
    1190
    1191 -    def test_addprevious_root_pi(self):
    1192          Element = self.etree.Element
    1193          PI = self.etree.PI
    1194          root = Element('root')
    1195          pi = PI('TARGET', 'TEXT')
    1196          pi.tail = "TAIL"
    1197
    1198          self.assertEquals(_bytes('<root></root>'),
    1199                            self._writeElement(root))
    1200          root.addprevious(pi)
    1201          self.assertEquals(_bytes('<?TARGET TEXT?>\n<root></root>'),
    1202                            self._writeElement(root))
    1203
    1204 -    def test_addnext_pi(self):
    1205          Element = self.etree.Element
    1206          SubElement = self.etree.SubElement
    1207          PI = self.etree.PI
    1208          root = Element('root')
    1209          SubElement(root, 'a')
    1210          pi = PI('TARGET', 'TEXT')
    1211          pi.tail = "TAIL"
    1212
    1213          self.assertEquals(_bytes('<root><a></a></root>'),
    1214                            self._writeElement(root))
    1215          root[0].addnext(pi)
    1216          self.assertEquals(_bytes('<root><a></a><?TARGET TEXT?>TAIL</root>'),
    1217                            self._writeElement(root))
    1218
    1219 -    def test_addnext_root_pi(self):
    1220          Element = self.etree.Element
    1221          PI = self.etree.PI
    1222          root = Element('root')
    1223          pi = PI('TARGET', 'TEXT')
    1224          pi.tail = "TAIL"
    1225
    1226          self.assertEquals(_bytes('<root></root>'),
    1227                            self._writeElement(root))
    1228          root.addnext(pi)
    1229          self.assertEquals(_bytes('<root></root>\n<?TARGET TEXT?>'),
    1230                            self._writeElement(root))
    1231
    1232 -    def test_addnext_comment(self):
    1233          Element = self.etree.Element
    1234          SubElement = self.etree.SubElement
    1235          Comment = self.etree.Comment
    1236          root = Element('root')
    1237          SubElement(root, 'a')
    1238          comment = Comment('TEXT ')
    1239          comment.tail = "TAIL"
    1240
    1241          self.assertEquals(_bytes('<root><a></a></root>'),
    1242                            self._writeElement(root))
    1243          root[0].addnext(comment)
    1244          self.assertEquals(_bytes('<root><a></a><!--TEXT -->TAIL</root>'),
    1245                            self._writeElement(root))
    1246
    1247 -    def test_addnext_root_comment(self):
    1248          Element = self.etree.Element
    1249          Comment = self.etree.Comment
    1250          root = Element('root')
    1251          comment = Comment('TEXT ')
    1252          comment.tail = "TAIL"
    1253
    1254          self.assertEquals(_bytes('<root></root>'),
    1255                            self._writeElement(root))
    1256          root.addnext(comment)
    1257          self.assertEquals(_bytes('<root></root>\n<!--TEXT -->'),
    1258                            self._writeElement(root))
    1259
    1260 -    def test_addprevious_comment(self):
    1261          Element = self.etree.Element
    1262          SubElement = self.etree.SubElement
    1263          Comment = self.etree.Comment
    1264          root = Element('root')
    1265          SubElement(root, 'a')
    1266          comment = Comment('TEXT ')
    1267          comment.tail = "TAIL"
    1268
    1269          self.assertEquals(_bytes('<root><a></a></root>'),
    1270                            self._writeElement(root))
    1271          root[0].addprevious(comment)
    1272          self.assertEquals(_bytes('<root><!--TEXT -->TAIL<a></a></root>'),
    1273                            self._writeElement(root))
    1274
    1275 -    def test_addprevious_root_comment(self):
    1276          Element = self.etree.Element
    1277          Comment = self.etree.Comment
    1278          root = Element('root')
    1279          comment = Comment('TEXT ')
    1280          comment.tail = "TAIL"
    1281
    1282          self.assertEquals(_bytes('<root></root>'),
    1283                            self._writeElement(root))
    1284          root.addprevious(comment)
    1285          self.assertEquals(_bytes('<!--TEXT -->\n<root></root>'),
    1286                            self._writeElement(root))
    1287
    1288      # ET's Elements have items() and key(), but not values()
    1289 -    def test_attribute_values(self):
    1290          XML = self.etree.XML
    1291
    1292          root = XML(_bytes('<doc alpha="Alpha" beta="Beta" gamma="Gamma"/>'))
    1293          values = root.values()
    1294          values.sort()
    1295          self.assertEquals(['Alpha', 'Beta', 'Gamma'], values)
    1296
    1297      # gives error in ElementTree
    1298 -    def test_comment_empty(self):
    1299          Element = self.etree.Element
    1300          Comment = self.etree.Comment
    1301
    1302          a = Element('a')
    1303          a.append(Comment())
    1304          self.assertEquals(
    1305              _bytes('<a><!----></a>'),
    1306              self._writeElement(a))
    1307
    1308      # ElementTree ignores comments
    1309 -    def test_comment_parse_empty(self):
    1310          ElementTree = self.etree.ElementTree
    1311          tostring = self.etree.tostring
    1312
    1313          xml = _bytes('<a><b/><!----><c/></a>')
    1314          f = BytesIO(xml)
    1315          doc = ElementTree(file=f)
    1316          a = doc.getroot()
    1317          self.assertEquals(
    1318              '',
    1319              a[1].text)
    1320          self.assertEquals(
    1321              xml,
    1322              tostring(a))
    1323
    1324      # ElementTree ignores comments
    1325 -    def test_comment_no_proxy_yet(self):
    1326          ElementTree = self.etree.ElementTree
    1327
    1328          f = BytesIO('<a><b></b><!-- hoi --><c></c></a>')
    1329          doc = ElementTree(file=f)
    1330          a = doc.getroot()
    1331          self.assertEquals(
    1332              ' hoi ',
    1333              a[1].text)
    1334
    1335      # does not raise an exception in ElementTree
    1336 -    def test_comment_immutable(self):
    1337          Element = self.etree.Element
    1338          Comment = self.etree.Comment
    1339
    1340          c = Comment()
    1341          el = Element('myel')
    1342
    1343          self.assertRaises(TypeError, c.append, el)
    1344          self.assertRaises(TypeError, c.insert, 0, el)
    1345          self.assertRaises(TypeError, c.set, "myattr", "test")
    1346
    1347      # test passing 'None' to dump
    1348 -    def test_dump_none(self):
    1349          self.assertRaises(TypeError, self.etree.dump, None)
    1350
    1351 -    def test_prefix(self):
    1352          ElementTree = self.etree.ElementTree
    1353
    1354          f = BytesIO('<a xmlns:foo="http://www.infrae.com/ns/1"><foo:b/></a>')
    1355          doc = ElementTree(file=f)
    1356          a = doc.getroot()
    1357          self.assertEquals(
    1358              None,
    1359              a.prefix)
    1360          self.assertEquals(
    1361              'foo',
    1362              a[0].prefix)
    1363
    1364 -    def test_prefix_default_ns(self):
    1365          ElementTree = self.etree.ElementTree
    1366
    1367          f = BytesIO('<a xmlns="http://www.infrae.com/ns/1"><b/></a>')
    1368          doc = ElementTree(file=f)
    1369          a = doc.getroot()
    1370          self.assertEquals(
    1371              None,
    1372              a.prefix)
    1373          self.assertEquals(
    1374              None,
    1375              a[0].prefix)
    1376
    1377 -    def test_getparent(self):
    1378          Element = self.etree.Element
    1379          SubElement = self.etree.SubElement
    1380
    1381          a = Element('a')
    1382          b = SubElement(a, 'b')
    1383          c = SubElement(a, 'c')
    1384          d = SubElement(b, 'd')
    1385          self.assertEquals(
    1386              None,
    1387              a.getparent())
    1388          self.assertEquals(
    1389              a,
    1390              b.getparent())
    1391          self.assertEquals(
    1392              b.getparent(),
    1393              c.getparent())
    1394          self.assertEquals(
    1395              b,
    1396              d.getparent())
    1397
    1398 -    def test_iterchildren(self):
    1399          XML = self.etree.XML
    1400
    1401          root = XML(_bytes('<doc><one/><two>Two</two>Hm<three/></doc>'))
    1402          result = []
    1403          for el in root.iterchildren():
    1404              result.append(el.tag)
    1405          self.assertEquals(['one', 'two', 'three'], result)
    1406
    1407 -    def test_iterchildren_reversed(self):
    1408          XML = self.etree.XML
    1409
    1410          root = XML(_bytes('<doc><one/><two>Two</two>Hm<three/></doc>'))
    1411          result = []
    1412          for el in root.iterchildren(reversed=True):
    1413              result.append(el.tag)
    1414          self.assertEquals(['three', 'two', 'one'], result)
    1415
    1416 -    def test_iterchildren_tag(self):
    1417          XML = self.etree.XML
    1418
    1419          root = XML(_bytes('<doc><one/><two>Two</two>Hm<two>Bla</two></doc>'))
    1420          result = []
    1421          for el in root.iterchildren(tag='two'):
    1422              result.append(el.text)
    1423          self.assertEquals(['Two', 'Bla'], result)
    1424
    1425 -    def test_iterchildren_tag_reversed(self):
    1426          XML = self.etree.XML
    1427
    1428          root = XML(_bytes('<doc><one/><two>Two</two>Hm<two>Bla</two></doc>'))
    1429          result = []
    1430          for el in root.iterchildren(reversed=True, tag='two'):
    1431              result.append(el.text)
    1432          self.assertEquals(['Bla', 'Two'], result)
    1433
    1434 -    def test_iterancestors(self):
    1435          Element    = self.etree.Element
    1436          SubElement = self.etree.SubElement
    1437
    1438          a = Element('a')
    1439          b = SubElement(a, 'b')
    1440          c = SubElement(a, 'c')
    1441          d = SubElement(b, 'd')
    1442          self.assertEquals(
    1443              [],
    1444              list(a.iterancestors()))
    1445          self.assertEquals(
    1446              [a],
    1447              list(b.iterancestors()))
    1448          self.assertEquals(
    1449              [a],
    1450              list(c.iterancestors()))
    1451          self.assertEquals(
    1452              [b, a],
    1453              list(d.iterancestors()))
    1454
    1455 -    def test_iterancestors_tag(self):
    1456          Element    = self.etree.Element
    1457          SubElement = self.etree.SubElement
    1458
    1459          a = Element('a')
    1460          b = SubElement(a, 'b')
    1461          c = SubElement(a, 'c')
    1462          d = SubElement(b, 'd')
    1463          self.assertEquals(
    1464              [a],
    1465              list(d.iterancestors(tag='a')))
    1466
    1467 -    def test_iterdescendants(self):
    1468          Element = self.etree.Element
    1469          SubElement = self.etree.SubElement
    1470
    1471          a = Element('a')
    1472          b = SubElement(a, 'b')
    1473          c = SubElement(a, 'c')
    1474          d = SubElement(b, 'd')
    1475          e = SubElement(c, 'e')
    1476
    1477          self.assertEquals(
    1478              [b, d, c, e],
    1479              list(a.iterdescendants()))
    1480          self.assertEquals(
    1481              [],
    1482              list(d.iterdescendants()))
    1483
    1484 -    def test_iterdescendants_tag(self):
    1485          Element = self.etree.Element
    1486          SubElement = self.etree.SubElement
    1487
    1488          a = Element('a')
    1489          b = SubElement(a, 'b')
    1490          c = SubElement(a, 'c')
    1491          d = SubElement(b, 'd')
    1492          e = SubElement(c, 'e')
    1493
    1494          self.assertEquals(
    1495              [],
    1496              list(a.iterdescendants('a')))
    1497          a2 = SubElement(e, 'a')
    1498          self.assertEquals(
    1499              [a2],
    1500              list(a.iterdescendants('a')))
    1501          self.assertEquals(
    1502              [a2],
    1503              list(c.iterdescendants('a')))
    1504
    1505 -    def test_getroottree(self):
    1506          Element = self.etree.Element
    1507          SubElement = self.etree.SubElement
    1508
    1509          a = Element('a')
    1510          b = SubElement(a, 'b')
    1511          c = SubElement(a, 'c')
    1512          d = SubElement(b, 'd')
    1513          self.assertEquals(
    1514              a,
    1515              a.getroottree().getroot())
    1516          self.assertEquals(
    1517              a,
    1518              b.getroottree().getroot())
    1519          self.assertEquals(
    1520              a,
    1521              d.getroottree().getroot())
    1522
    1523 -    def test_getnext(self):
    1524          Element    = self.etree.Element
    1525          SubElement = self.etree.SubElement
    1526
    1527          a = Element('a')
    1528          b = SubElement(a, 'b')
    1529          c = SubElement(a, 'c')
    1530          self.assertEquals(
    1531              None,
    1532              a.getnext())
    1533          self.assertEquals(
    1534              c,
    1535              b.getnext())
    1536          self.assertEquals(
    1537              None,
    1538              c.getnext())
    1539
    1540 -    def test_getprevious(self):
    1541          Element    = self.etree.Element
    1542          SubElement = self.etree.SubElement
    1543
    1544          a = Element('a')
    1545          b = SubElement(a, 'b')
    1546          c = SubElement(a, 'c')
    1547          d = SubElement(b, 'd')
    1548          self.assertEquals(
    1549              None,
    1550              a.getprevious())
    1551          self.assertEquals(
    1552              b,
    1553              c.getprevious())
    1554          self.assertEquals(
    1555              None,
    1556              b.getprevious())
    1557
    1558 -    def test_itersiblings(self):
    1559          Element    = self.etree.Element
    1560          SubElement = self.etree.SubElement
    1561
    1562          a = Element('a')
    1563          b = SubElement(a, 'b')
    1564          c = SubElement(a, 'c')
    1565          d = SubElement(b, 'd')
    1566          self.assertEquals(
    1567              [],
    1568              list(a.itersiblings()))
    1569          self.assertEquals(
    1570              [c],
    1571              list(b.itersiblings()))
    1572          self.assertEquals(
    1573              [],
    1574              list(c.itersiblings()))
    1575          self.assertEquals(
    1576              [b],
    1577              list(c.itersiblings(preceding=True)))
    1578          self.assertEquals(
    1579              [],
    1580              list(b.itersiblings(preceding=True)))
    1581
    1582 -    def test_itersiblings_tag(self):
    1583          Element    = self.etree.Element
    1584          SubElement = self.etree.SubElement
    1585
    1586          a = Element('a')
    1587          b = SubElement(a, 'b')
    1588          c = SubElement(a, 'c')
    1589          d = SubElement(b, 'd')
    1590          self.assertEquals(
    1591              [],
    1592              list(a.itersiblings(tag='XXX')))
    1593          self.assertEquals(
    1594              [c],
    1595              list(b.itersiblings(tag='c')))
    1596          self.assertEquals(
    1597              [b],
    1598              list(c.itersiblings(preceding=True, tag='b')))
    1599          self.assertEquals(
    1600              [],
    1601              list(c.itersiblings(preceding=True, tag='c')))
    1602
    1603 -    def test_parseid(self):
    1604          parseid = self.etree.parseid
    1605          XML     = self.etree.XML
    1606          xml_text = _bytes('''
    1607          <!DOCTYPE document [
    1608          <!ELEMENT document (h1,p)*>
    1609          <!ELEMENT h1 (#PCDATA)>
    1610          <!ATTLIST h1 myid ID #REQUIRED>
    1611          <!ELEMENT p  (#PCDATA)>
    1612          <!ATTLIST p  someid ID #REQUIRED>
    1613          ]>
    1614          <document>
    1615            <h1 myid="chapter1">...</h1>
    1616            <p id="note1" class="note">...</p>
    1617            <p>Regular paragraph.</p>
    1618            <p xml:id="xmlid">XML:ID paragraph.</p>
    1619            <p someid="warn1" class="warning">...</p>
    1620          </document>
    1621          ''')
    1622
    1623          tree, dic = parseid(BytesIO(xml_text))
    1624          root = tree.getroot()
    1625          root2 = XML(xml_text)
    1626          self.assertEquals(self._writeElement(root),
    1627                            self._writeElement(root2))
    1628          expected = {
    1629              "chapter1" : root[0],
    1630              "xmlid"    : root[3],
    1631              "warn1"    : root[4]
    1632              }
    1633          self.assert_("chapter1" in dic)
    1634          self.assert_("warn1" in dic)
    1635          self.assert_("xmlid" in dic)
    1636          self._checkIDDict(dic, expected)
    1637
    1638 -    def test_XMLDTDID(self):
    1639          XMLDTDID = self.etree.XMLDTDID
    1640          XML      = self.etree.XML
    1641          xml_text = _bytes('''
    1642          <!DOCTYPE document [
    1643          <!ELEMENT document (h1,p)*>
    1644          <!ELEMENT h1 (#PCDATA)>
    1645          <!ATTLIST h1 myid ID #REQUIRED>
    1646          <!ELEMENT p  (#PCDATA)>
    1647          <!ATTLIST p  someid ID #REQUIRED>
    1648          ]>
    1649          <document>
    1650            <h1 myid="chapter1">...</h1>
    1651            <p id="note1" class="note">...</p>
    1652            <p>Regular paragraph.</p>
    1653            <p xml:id="xmlid">XML:ID paragraph.</p>
    1654            <p someid="warn1" class="warning">...</p>
    1655          </document>
    1656          ''')
    1657
    1658          root, dic = XMLDTDID(xml_text)
    1659          root2 = XML(xml_text)
    1660          self.assertEquals(self._writeElement(root),
    1661                            self._writeElement(root2))
    1662          expected = {
    1663              "chapter1" : root[0],
    1664              "xmlid"    : root[3],
    1665              "warn1"    : root[4]
    1666              }
    1667          self.assert_("chapter1" in dic)
    1668          self.assert_("warn1" in dic)
    1669          self.assert_("xmlid" in dic)
    1670          self._checkIDDict(dic, expected)
    1671
    1672 -    def test_XMLDTDID_empty(self):
    1673          XMLDTDID = self.etree.XMLDTDID
    1674          XML      = self.etree.XML
    1675          xml_text = _bytes('''
    1676          <document>
    1677            <h1 myid="chapter1">...</h1>
    1678            <p id="note1" class="note">...</p>
    1679            <p>Regular paragraph.</p>
    1680            <p someid="warn1" class="warning">...</p>
    1681          </document>
    1682          ''')
    1683
    1684          root, dic = XMLDTDID(xml_text)
    1685          root2 = XML(xml_text)
    1686          self.assertEquals(self._writeElement(root),
    1687                            self._writeElement(root2))
    1688          expected = {}
    1689          self._checkIDDict(dic, expected)
    1690
    1691 -    def _checkIDDict(self, dic, expected):
    1692          self.assertEquals(len(dic),
    1693                            len(expected))
    1694          self.assertEquals(sorted(dic.items()),
    1695                            sorted(expected.items()))
    1696          if sys.version_info < (3,):
    1697              self.assertEquals(sorted(dic.iteritems()),
    1698                                sorted(expected.iteritems()))
    1699          self.assertEquals(sorted(dic.keys()),
    1700                            sorted(expected.keys()))
    1701          if sys.version_info < (3,):
    1702              self.assertEquals(sorted(dic.iterkeys()),
    1703                                sorted(expected.iterkeys()))
    1704          if sys.version_info < (3,):
    1705              self.assertEquals(sorted(dic.values()),
    1706                                sorted(expected.values()))
    1707              self.assertEquals(sorted(dic.itervalues()),
    1708                                sorted(expected.itervalues()))
    1709
    1710 -    def test_namespaces(self):
    1711          etree = self.etree
    1712
    1713          r = {'foo': 'http://ns.infrae.com/foo'}
    1714          e = etree.Element('{http://ns.infrae.com/foo}bar', nsmap=r)
    1715          self.assertEquals(
    1716              'foo',
    1717              e.prefix)
    1718          self.assertEquals(
    1719              _bytes('<foo:bar xmlns:foo="http://ns.infrae.com/foo"></foo:bar>'),
    1720              self._writeElement(e))
    1721
    1722 -    def test_namespaces_default(self):
    1723          etree = self.etree
    1724
    1725          r = {None: 'http://ns.infrae.com/foo'}
    1726          e = etree.Element('{http://ns.infrae.com/foo}bar', nsmap=r)
    1727          self.assertEquals(
    1728              None,
    1729              e.prefix)
    1730          self.assertEquals(
    1731              '{http://ns.infrae.com/foo}bar',
    1732              e.tag)
    1733          self.assertEquals(
    1734              _bytes('<bar xmlns="http://ns.infrae.com/foo"></bar>'),
    1735              self._writeElement(e))
    1736
    1737 -    def test_namespaces_default_and_attr(self):
    1738          etree = self.etree
    1739
    1740          r = {None: 'http://ns.infrae.com/foo',
    1741               'hoi': 'http://ns.infrae.com/hoi'}
    1742          e = etree.Element('{http://ns.infrae.com/foo}bar', nsmap=r)
    1743          e.set('{http://ns.infrae.com/hoi}test', 'value')
    1744          self.assertEquals(
    1745              _bytes('<bar xmlns="http://ns.infrae.com/foo" xmlns:hoi="http://ns.infrae.com/hoi" hoi:test="value"></bar>'),
    1746              self._writeElement(e))
    1747
    1748 -    def test_namespaces_elementtree(self):
    1749          etree = self.etree
    1750          r = {None: 'http://ns.infrae.com/foo',
    1751               'hoi': 'http://ns.infrae.com/hoi'}
    1752          e = etree.Element('{http://ns.infrae.com/foo}z', nsmap=r)
    1753          tree = etree.ElementTree(element=e)
    1754          etree.SubElement(e, '{http://ns.infrae.com/hoi}x')
    1755          self.assertEquals(
    1756              _bytes('<z xmlns="http://ns.infrae.com/foo" xmlns:hoi="http://ns.infrae.com/hoi"><hoi:x></hoi:x></z>'),
    1757              self._writeElement(e))
    1758
    1759 -    def test_namespaces_default_copy_element(self):
    1760          etree = self.etree
    1761
    1762          r = {None: 'http://ns.infrae.com/foo'}
    1763          e1 = etree.Element('{http://ns.infrae.com/foo}bar', nsmap=r)
    1764          e2 = etree.Element('{http://ns.infrae.com/foo}bar', nsmap=r)
    1765
    1766          e1.append(e2)
    1767
    1768          self.assertEquals(
    1769              None,
    1770              e1.prefix)
    1771          self.assertEquals(
    1772              None,
    1773              e1[0].prefix)
    1774          self.assertEquals(
    1775              '{http://ns.infrae.com/foo}bar',
    1776              e1.tag)
    1777          self.assertEquals(
    1778              '{http://ns.infrae.com/foo}bar',
    1779              e1[0].tag)
    1780
    1781 -    def test_namespaces_copy_element(self):
    1782          etree = self.etree
    1783
    1784          r = {None: 'http://ns.infrae.com/BAR'}
    1785          e1 = etree.Element('{http://ns.infrae.com/BAR}bar', nsmap=r)
    1786          e2 = etree.Element('{http://ns.infrae.com/foo}bar', nsmap=r)
    1787
    1788          e1.append(e2)
    1789
    1790          self.assertEquals(
    1791              None,
    1792              e1.prefix)
    1793          self.assertNotEquals(
    1794              None,
    1795              e2.prefix)
    1796          self.assertEquals(
    1797              '{http://ns.infrae.com/BAR}bar',
    1798              e1.tag)
    1799          self.assertEquals(
    1800              '{http://ns.infrae.com/foo}bar',
    1801              e2.tag)
    1802
    1803 -    def test_namespaces_reuse_after_move(self):
    1804          ns_href = "http://a.b.c"
    1805          one = self.etree.fromstring(
    1806              _bytes('<foo><bar xmlns:ns="%s"><ns:baz/></bar></foo>' % ns_href))
    1807          baz = one[0][0]
    1808
    1809          two = self.etree.fromstring(
    1810              _bytes('<root xmlns:ns="%s"/>' % ns_href))
    1811          two.append(baz)
    1812          del one # make sure the source document is deallocated
    1813
    1814          self.assertEquals('{%s}baz' % ns_href, baz.tag)
    1815          self.assertEquals(
    1816              _bytes('<root xmlns:ns="%s"><ns:baz/></root>' % ns_href),
    1817              self.etree.tostring(two))
    1818
    1819 -    def test_namespace_cleanup(self):
    1820          xml = _bytes('<foo xmlns="F" xmlns:x="x"><bar xmlns:ns="NS" xmlns:b="b" xmlns="B"><ns:baz/></bar></foo>')
    1821          root = self.etree.fromstring(xml)
    1822          self.assertEquals(xml,
    1823                            self.etree.tostring(root))
    1824          self.etree.cleanup_namespaces(root)
    1825          self.assertEquals(
    1826              _bytes('<foo xmlns="F"><bar xmlns:ns="NS" xmlns="B"><ns:baz/></bar></foo>'),
    1827              self.etree.tostring(root))
    1828
    1829 -    def test_element_nsmap(self):
    1830          etree = self.etree
    1831
    1832          r = {None: 'http://ns.infrae.com/foo',
    1833               'hoi': 'http://ns.infrae.com/hoi'}
    1834          e = etree.Element('{http://ns.infrae.com/foo}bar', nsmap=r)
    1835          self.assertEquals(
    1836              r,
    1837              e.nsmap)
    1838
    1839 -    def test_subelement_nsmap(self):
    1840          etree = self.etree
    1841
    1842          re = {None: 'http://ns.infrae.com/foo',
    1843               'hoi': 'http://ns.infrae.com/hoi'}
    1844          e = etree.Element('{http://ns.infrae.com/foo}bar', nsmap=re)
    1845
    1846          rs = {None: 'http://ns.infrae.com/honk',
    1847               'top': 'http://ns.infrae.com/top'}
    1848          s = etree.SubElement(e, '{http://ns.infrae.com/honk}bar', nsmap=rs)
    1849
    1850          r = re.copy()
    1851          r.update(rs)
    1852          self.assertEquals(
    1853              re,
    1854              e.nsmap)
    1855          self.assertEquals(
    1856              r,
    1857              s.nsmap)
    1858
    1859 -    def test_getiterator_filter_namespace(self):
    1860          Element = self.etree.Element
    1861          SubElement = self.etree.SubElement
    1862
    1863          a = Element('{a}a')
    1864          b = SubElement(a, '{a}b')
    1865          c = SubElement(a, '{a}c')
    1866          d = SubElement(b, '{b}d')
    1867          e = SubElement(c, '{a}e')
    1868          f = SubElement(c, '{b}f')
    1869
    1870          self.assertEquals(
    1871              [a],
    1872              list(a.getiterator('{a}a')))
    1873          self.assertEquals(
    1874              [],
    1875              list(a.getiterator('{b}a')))
    1876          self.assertEquals(
    1877              [],
    1878              list(a.getiterator('a')))
    1879          self.assertEquals(
    1880              [f],
    1881              list(c.getiterator('{b}*')))
    1882          self.assertEquals(
    1883              [d, f],
    1884              list(a.getiterator('{b}*')))
    1885
    1886 -    def test_getiterator_filter_entities(self):
    1887          Element = self.etree.Element
    1888          Entity = self.etree.Entity
    1889          SubElement = self.etree.SubElement
    1890
    1891          a = Element('a')
    1892          b = SubElement(a, 'b')
    1893          entity_b = Entity("TEST-b")
    1894          b.append(entity_b)
    1895
    1896          self.assertEquals(
    1897              [entity_b],
    1898              list(a.getiterator(Entity)))
    1899
    1900          entity_a = Entity("TEST-a")
    1901          a.append(entity_a)
    1902
    1903          self.assertEquals(
    1904              [entity_b, entity_a],
    1905              list(a.getiterator(Entity)))
    1906
    1907          self.assertEquals(
    1908              [entity_b],
    1909              list(b.getiterator(Entity)))
    1910
    1911 -    def test_getiterator_filter_element(self):
    1912          Element = self.etree.Element
    1913          Comment = self.etree.Comment
    1914          PI = self.etree.PI
    1915          SubElement = self.etree.SubElement
    1916
    1917          a = Element('a')
    1918          b = SubElement(a, 'b')
    1919          a.append(Comment("test"))
    1920          a.append(PI("pi", "content"))
    1921          c = SubElement(a, 'c')
    1922
    1923          self.assertEquals(
    1924              [a, b, c],
    1925              list(a.getiterator(Element)))
    1926
    1927 -    def test_getiterator_filter_all_comment_pi(self):
    1928          # ElementTree iterates over everything here
    1929          Element = self.etree.Element
    1930          Comment = self.etree.Comment
    1931          PI = self.etree.PI
    1932          SubElement = self.etree.SubElement
    1933
    1934          a = Element('a')
    1935          b = SubElement(a, 'b')
    1936          a.append(Comment("test"))
    1937          a.append(PI("pi", "content"))
    1938          c = SubElement(a, 'c')
    1939
    1940          self.assertEquals(
    1941              [a, b, c],
    1942              list(a.getiterator('*')))
    1943
    1944 -    def test_elementtree_find_qname(self):
    1945          XML = self.etree.XML
    1946          ElementTree = self.etree.ElementTree
    1947          QName = self.etree.QName
    1948          tree = ElementTree(XML(_bytes('<a><b><c/></b><b/><c><b/></c></a>')))
    1949          self.assertEquals(tree.find(QName("c")), tree.getroot()[2])
    1950
    1951 -    def test_elementtree_findall_qname(self):
    1952          XML = self.etree.XML
    1953          ElementTree = self.etree.ElementTree
    1954          QName = self.etree.QName
    1955          tree = ElementTree(XML(_bytes('<a><b><c/></b><b/><c><b/></c></a>')))
    1956          self.assertEquals(len(list(tree.findall(QName("c")))), 1)
    1957
    1958 -    def test_elementtree_findall_ns_qname(self):
    1959          XML = self.etree.XML
    1960          ElementTree = self.etree.ElementTree
    1961          QName = self.etree.QName
    1962          tree = ElementTree(XML(
    1963                  _bytes('<a xmlns:x="X" xmlns:y="Y"><x:b><c/></x:b><b/><c><x:b/><b/></c><b/></a>')))
    1964          self.assertEquals(len(list(tree.findall(QName("b")))), 2)
    1965          self.assertEquals(len(list(tree.findall(QName("X", "b")))), 1)
    1966
    1967 -    def test_findall_ns(self):
    1968          XML = self.etree.XML
    1969          root = XML(_bytes('<a xmlns:x="X" xmlns:y="Y"><x:b><c/></x:b><b/><c><x:b/><b/></c><b/></a>'))
    1970          self.assertEquals(len(root.findall(".//{X}b")), 2)
    1971          self.assertEquals(len(root.findall(".//{X}*")), 2)
    1972          self.assertEquals(len(root.findall(".//b")), 3)
    1973
    1974 -    def test_index(self):
    1975          etree = self.etree
    1976          e = etree.Element('foo')
    1977          for i in range(10):
    1978              etree.SubElement(e, 'a%s' % i)
    1979          for i in range(10):
    1980              self.assertEquals(
    1981                  i,
    1982                  e.index(e[i]))
    1983          self.assertEquals(
    1984              3, e.index(e[3], 3))
    1985          self.assertRaises(
    1986              ValueError, e.index, e[3], 4)
    1987          self.assertRaises(
    1988              ValueError, e.index, e[3], 0, 2)
    1989          self.assertRaises(
    1990              ValueError, e.index, e[8], 0, -3)
    1991          self.assertRaises(
    1992              ValueError, e.index, e[8], -5, -3)
    1993          self.assertEquals(
    1994              8, e.index(e[8], 0, -1))
    1995          self.assertEquals(
    1996              8, e.index(e[8], -12, -1))
    1997          self.assertEquals(
    1998              0, e.index(e[0], -12, -1))
    1999
    2000 -    def test_replace(self):
    2001          etree = self.etree
    2002          e = etree.Element('foo')
    2003          for i in range(10):
    2004              el = etree.SubElement(e, 'a%s' % i)
    2005              el.text = "text%d" % i
    2006              el.tail = "tail%d" % i
    2007
    2008          child0 = e[0]
    2009          child1 = e[1]
    2010          child2 = e[2]
    2011
    2012          e.replace(e[0], e[1])
    2013          self.assertEquals(
    2014              9, len(e))
    2015          self.assertEquals(
    2016              child1, e[0])
    2017          self.assertEquals(
    2018              child1.text, "text1")
    2019          self.assertEquals(
    2020              child1.tail, "tail1")
    2021          self.assertEquals(
    2022              child0.tail, "tail0")
    2023          self.assertEquals(
    2024              child2, e[1])
    2025
    2026          e.replace(e[-1], e[0])
    2027          self.assertEquals(
    2028              child1, e[-1])
    2029          self.assertEquals(
    2030              child1.text, "text1")
    2031          self.assertEquals(
    2032              child1.tail, "tail1")
    2033          self.assertEquals(
    2034              child2, e[0])
    2035
    2036 -    def test_replace_new(self):
    2037          etree = self.etree
    2038          e = etree.Element('foo')
    2039          for i in range(10):
    2040              etree.SubElement(e, 'a%s' % i)
    2041
    2042          new_element = etree.Element("test")
    2043          new_element.text = "TESTTEXT"
    2044          new_element.tail = "TESTTAIL"
    2045          child1 = e[1]
    2046          e.replace(e[0], new_element)
    2047          self.assertEquals(
    2048              new_element, e[0])
    2049          self.assertEquals(
    2050              "TESTTEXT",
    2051              e[0].text)
    2052          self.assertEquals(
    2053              "TESTTAIL",
    2054              e[0].tail)
    2055          self.assertEquals(
    2056              child1, e[1])
    2057
    2058 -    def test_setslice_all_empty_reversed(self):
    2059          Element = self.etree.Element
    2060          SubElement = self.etree.SubElement
    2061
    2062          a = Element('a')
    2063
    2064          e = Element('e')
    2065          f = Element('f')
    2066          g = Element('g')
    2067
    2068          s = [e, f, g]
    2069          a[::-1] = s
    2070          self.assertEquals(
    2071              [g, f, e],
    2072              list(a))
    2073
    2074 -    def test_setslice_step(self):
    2075          Element = self.etree.Element
    2076          SubElement = self.etree.SubElement
    2077
    2078          a = Element('a')
    2079          b = SubElement(a, 'b')
    2080          c = SubElement(a, 'c')
    2081          d = SubElement(a, 'd')
    2082          e = SubElement(a, 'e')
    2083
    2084          x = Element('x')
    2085          y = Element('y')
    2086
    2087          a[1::2] = [x, y]
    2088          self.assertEquals(
    2089              [b, x, d, y],
    2090              list(a))
    2091
    2092 -    def test_setslice_step_negative(self):
    2093          Element = self.etree.Element
    2094          SubElement = self.etree.SubElement
    2095
    2096          a = Element('a')
    2097          b = SubElement(a, 'b')
    2098          c = SubElement(a, 'c')
    2099          d = SubElement(a, 'd')
    2100          e = SubElement(a, 'e')
    2101
    2102          x = Element('x')
    2103          y = Element('y')
    2104
    2105          a[1::-1] = [x, y]
    2106          self.assertEquals(
    2107              [y, x, d, e],
    2108              list(a))
    2109
    2110 -    def test_setslice_step_negative2(self):
    2111          Element = self.etree.Element
    2112          SubElement = self.etree.SubElement
    2113
    2114          a = Element('a')
    2115          b = SubElement(a, 'b')
    2116          c = SubElement(a, 'c')
    2117          d = SubElement(a, 'd')
    2118          e = SubElement(a, 'e')
    2119
    2120          x = Element('x')
    2121          y = Element('y')
    2122
    2123          a[::-2] = [x, y]
    2124          self.assertEquals(
    2125              [b, y, d, x],
    2126              list(a))
    2127
    2128 -    def test_setslice_step_overrun(self):
    2129          Element = self.etree.Element
    2130          SubElement = self.etree.SubElement
    2131          try:
    2132              slice
    2133          except NameError:
    2134              print("slice() not found")
    2135              return
    2136
    2137          a = Element('a')
    2138          b = SubElement(a, 'b')
    2139          c = SubElement(a, 'c')
    2140          d = SubElement(a, 'd')
    2141          e = SubElement(a, 'e')
    2142
    2143          x = Element('x')
    2144          y = Element('y')
    2145          z = Element('z')
    2146
    2147          self.assertRaises(
    2148              ValueError,
    2149              operator.setitem, a, slice(1,None,2), [x, y, z])
    2150
    2151          self.assertEquals(
    2152              [b, c, d, e],
    2153              list(a))
    2154
    2155 -    def test_sourceline_XML(self):
    2156          XML = self.etree.XML
    2157          root = XML(_bytes('''<?xml version="1.0"?>
    2158          <root><test>
    2159
    2160          <bla/></test>
    2161          </root>
    2162          '''))
    2163
    2164          self.assertEquals(
    2165              [2, 2, 4],
    2166              [ el.sourceline for el in root.getiterator() ])
    2167
    2168 -    def test_sourceline_parse(self):
    2169          parse = self.etree.parse
    2170          tree = parse(fileInTestDir('include/test_xinclude.xml'))
    2171
    2172          self.assertEquals(
    2173              [1, 2, 3],
    2174              [ el.sourceline for el in tree.getiterator() ])
    2175
    2176 -    def test_sourceline_iterparse_end(self):
    2177          iterparse = self.etree.iterparse
    2178          lines = [ el.sourceline for (event, el) in
    2179                    iterparse(fileInTestDir('include/test_xinclude.xml')) ]
    2180
    2181          self.assertEquals(
    2182              [2, 3, 1],
    2183              lines)
    2184
    2185 -    def test_sourceline_iterparse_start(self):
    2186          iterparse = self.etree.iterparse
    2187          lines = [ el.sourceline for (event, el) in
    2188                    iterparse(fileInTestDir('include/test_xinclude.xml'),
    2189                              events=("start",)) ]
    2190
    2191          self.assertEquals(
    2192              [1, 2, 3],
    2193              lines)
    2194
    2195 -    def test_sourceline_element(self):
    2196          Element = self.etree.Element
    2197          SubElement = self.etree.SubElement
    2198          el = Element("test")
    2199          self.assertEquals(None, el.sourceline)
    2200
    2201          child = SubElement(el, "test")
    2202          self.assertEquals(None, el.sourceline)
    2203          self.assertEquals(None, child.sourceline)
    2204
    2205 -    def test_XML_base_url_docinfo(self):
    2206          etree = self.etree
    2207          root = etree.XML(_bytes("<root/>"), base_url="http://no/such/url")
    2208          docinfo = root.getroottree().docinfo
    2209          self.assertEquals(docinfo.URL, "http://no/such/url")
    2210
    2211 -    def test_XML_set_base_url_docinfo(self):
    2212          etree = self.etree
    2213          root = etree.XML(_bytes("<root/>"), base_url="http://no/such/url")
    2214          docinfo = root.getroottree().docinfo
    2215          self.assertEquals(docinfo.URL, "http://no/such/url")
    2216          docinfo.URL = "https://secret/url"
    2217          self.assertEquals(docinfo.URL, "https://secret/url")
    2218
    2219 -    def test_parse_stringio_base_url(self):
    2220          etree = self.etree
    2221          tree = etree.parse(BytesIO("<root/>"), base_url="http://no/such/url")
    2222          docinfo = tree.docinfo
    2223          self.assertEquals(docinfo.URL, "http://no/such/url")
    2224
    2225 -    def test_parse_base_url_docinfo(self):
    2226          etree = self.etree
    2227          tree = etree.parse(fileInTestDir('include/test_xinclude.xml'),
    2228                             base_url="http://no/such/url")
    2229          docinfo = tree.docinfo
    2230          self.assertEquals(docinfo.URL, "http://no/such/url")
    2231
    2232 -    def test_HTML_base_url_docinfo(self):
    2233          etree = self.etree
    2234          root = etree.HTML(_bytes("<html/>"), base_url="http://no/such/url")
    2235          docinfo = root.getroottree().docinfo
    2236          self.assertEquals(docinfo.URL, "http://no/such/url")
    2237
    2238 -    def test_docinfo_public(self):
    2239          etree = self.etree
    2240          xml_header = '<?xml version="1.0" encoding="ascii"?>'
    2241          pub_id = "-//W3C//DTD XHTML 1.0 Transitional//EN"
    2242          sys_id = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
    2243          doctype_string = '<!DOCTYPE html PUBLIC "%s" "%s">' % (pub_id, sys_id)
    2244
    2245          xml = _bytes(xml_header + doctype_string + '<html><body></body></html>')
    2246
    2247          tree = etree.parse(BytesIO(xml))
    2248          docinfo = tree.docinfo
    2249          self.assertEquals(docinfo.encoding,    "ascii")
    2250          self.assertEquals(docinfo.xml_version, "1.0")
    2251          self.assertEquals(docinfo.public_id,   pub_id)
    2252          self.assertEquals(docinfo.system_url,  sys_id)
    2253          self.assertEquals(docinfo.root_name,   'html')
    2254          self.assertEquals(docinfo.doctype, doctype_string)
    2255
    2256 -    def test_docinfo_system(self):
    2257          etree = self.etree
    2258          xml_header = '<?xml version="1.0" encoding="UTF-8"?>'
    2259          sys_id = "some.dtd"
    2260          doctype_string = '<!DOCTYPE html SYSTEM "%s">' % sys_id
    2261          xml = _bytes(xml_header + doctype_string + '<html><body></body></html>')
    2262
    2263          tree = etree.parse(BytesIO(xml))
    2264          docinfo = tree.docinfo
    2265          self.assertEquals(docinfo.encoding,    "UTF-8")
    2266          self.assertEquals(docinfo.xml_version, "1.0")
    2267          self.assertEquals(docinfo.public_id,   None)
    2268          self.assertEquals(docinfo.system_url,  sys_id)
    2269          self.assertEquals(docinfo.root_name,   'html')
    2270          self.assertEquals(docinfo.doctype, doctype_string)
    2271
    2272 -    def test_docinfo_empty(self):
    2273          etree = self.etree
    2274          xml = _bytes('<html><body></body></html>')
    2275          tree = etree.parse(BytesIO(xml))
    2276          docinfo = tree.docinfo
    2277          self.assertEquals(docinfo.encoding,    "UTF-8")
    2278          self.assertEquals(docinfo.xml_version, "1.0")
    2279          self.assertEquals(docinfo.public_id,   None)
    2280          self.assertEquals(docinfo.system_url,  None)
    2281          self.assertEquals(docinfo.root_name,   'html')
    2282          self.assertEquals(docinfo.doctype, '')
    2283
    2284 -    def test_docinfo_name_only(self):
    2285          etree = self.etree
    2286          xml = _bytes('<!DOCTYPE root><root></root>')
    2287          tree = etree.parse(BytesIO(xml))
    2288          docinfo = tree.docinfo
    2289          self.assertEquals(docinfo.encoding,    "UTF-8")
    2290          self.assertEquals(docinfo.xml_version, "1.0")
    2291          self.assertEquals(docinfo.public_id,   None)
    2292          self.assertEquals(docinfo.system_url,  None)
    2293          self.assertEquals(docinfo.root_name,   'root')
    2294          self.assertEquals(docinfo.doctype, '<!DOCTYPE root>')
    2295
    2296 -    def test_doctype_name_only_roundtrip(self):
    2297          etree = self.etree
    2298          xml = _bytes('<!DOCTYPE root>\n<root/>')
    2299          tree = etree.parse(BytesIO(xml))
    2300          self.assertEquals(xml, etree.tostring(tree))
    2301
    2302 -    def test_xml_base(self):
    2303          etree = self.etree
    2304          root = etree.XML(_bytes("<root/>"), base_url="http://no/such/url")
    2305          self.assertEquals(root.base, "http://no/such/url")
    2306          self.assertEquals(
    2307              root.get('{http://www.w3.org/XML/1998/namespace}base'), None)
    2308          root.base = "https://secret/url"
    2309          self.assertEquals(root.base, "https://secret/url")
    2310          self.assertEquals(
    2311              root.get('{http://www.w3.org/XML/1998/namespace}base'),
    2312              "https://secret/url")
    2313
    2314 -    def test_xml_base_attribute(self):
    2315          etree = self.etree
    2316          root = etree.XML(_bytes("<root/>"), base_url="http://no/such/url")
    2317          self.assertEquals(root.base, "http://no/such/url")
    2318          self.assertEquals(
    2319              root.get('{http://www.w3.org/XML/1998/namespace}base'), None)
    2320          root.set('{http://www.w3.org/XML/1998/namespace}base',
    2321                   "https://secret/url")
    2322          self.assertEquals(root.base, "https://secret/url")
    2323          self.assertEquals(
    2324              root.get('{http://www.w3.org/XML/1998/namespace}base'),
    2325              "https://secret/url")
    2326
    2327 -    def test_html_base(self):
    2328          etree = self.etree
    2329          root = etree.HTML(_bytes("<html><body></body></html>"),
    2330                            base_url="http://no/such/url")
    2331          self.assertEquals(root.base, "http://no/such/url")
    2332
    2333 -    def test_html_base_tag(self):
    2334          etree = self.etree
    2335          root = etree.HTML(_bytes('<html><head><base href="http://no/such/url"></head></html>'))
    2336          self.assertEquals(root.base, "http://no/such/url")
    2337
    2338 -    def test_parse_fileobject_unicode(self):
    2339          # parse from a file object that returns unicode strings
    2340          f = LargeFileLikeUnicode()
    2341          tree = self.etree.parse(f)
    2342          root = tree.getroot()
    2343          self.assert_(root.tag.endswith('root'))
    2344
    2345 -    def test_dtd_io(self):
    2346          # check that DTDs that go in also go back out
    2347          xml = _bytes('''\
    2348          <!DOCTYPE test SYSTEM "test.dtd" [
    2349            <!ENTITY entity "tasty">
    2350            <!ELEMENT test (a)>
    2351            <!ELEMENT a (#PCDATA)>
    2352          ]>
    2353          <test><a>test-test</a></test>\
    2354          ''')
    2355          tree = self.etree.parse(BytesIO(xml))
    2356          self.assertEqual(self.etree.tostring(tree).replace(_bytes(" "), _bytes("")),
    2357                           xml.replace(_bytes(" "), _bytes("")))
    2358
    2359 -    def test_byte_zero(self):
    2360          Element = self.etree.Element
    2361
    2362          a = Element('a')
    2363          self.assertRaises(ValueError, setattr, a, "text", 'ha\0ho')
    2364          self.assertRaises(ValueError, setattr, a, "tail", 'ha\0ho')
    2365
    2366          self.assertRaises(ValueError, Element, 'ha\0ho')
    2367
    2368 -    def test_unicode_byte_zero(self):
    2369          Element = self.etree.Element
    2370
    2371          a = Element('a')
    2372          self.assertRaises(ValueError, setattr, a, "text",
    2373                            _str('ha\0ho'))
    2374          self.assertRaises(ValueError, setattr, a, "tail",
    2375                            _str('ha\0ho'))
    2376
    2377          self.assertRaises(ValueError, Element,
    2378                            _str('ha\0ho'))
    2379
    2380 -    def test_byte_invalid(self):
    2381          Element = self.etree.Element
    2382
    2383          a = Element('a')
    2384          self.assertRaises(ValueError, setattr, a, "text", 'ha\x07ho')
    2385          self.assertRaises(ValueError, setattr, a, "text", 'ha\x02ho')
    2386
    2387          self.assertRaises(ValueError, setattr, a, "tail", 'ha\x07ho')
    2388          self.assertRaises(ValueError, setattr, a, "tail", 'ha\x02ho')
    2389
    2390          self.assertRaises(ValueError, Element, 'ha\x07ho')
    2391          self.assertRaises(ValueError, Element, 'ha\x02ho')
    2392
    2393 -    def test_unicode_byte_invalid(self):
    2394          Element = self.etree.Element
    2395
    2396          a = Element('a')
    2397          self.assertRaises(ValueError, setattr, a, "text",
    2398                            _str('ha\x07ho'))
    2399          self.assertRaises(ValueError, setattr, a, "text",
    2400                            _str('ha\x02ho'))
    2401
    2402          self.assertRaises(ValueError, setattr, a, "tail",
    2403                            _str('ha\x07ho'))
    2404          self.assertRaises(ValueError, setattr, a, "tail",
    2405                            _str('ha\x02ho'))
    2406
    2407          self.assertRaises(ValueError, Element,
    2408                            _str('ha\x07ho'))
    2409          self.assertRaises(ValueError, Element,
    2410                            _str('ha\x02ho'))
    2411
    2412 -    def test_encoding_tostring_utf16(self):
    2413          # ElementTree fails to serialize this
    2414          tostring = self.etree.tostring
    2415          Element = self.etree.Element
    2416          SubElement = self.etree.SubElement
    2417
    2418          a = Element('a')
    2419          b = SubElement(a, 'b')
    2420          c = SubElement(a, 'c')
    2421
    2422          result = tostring(a, encoding='UTF-16')
    2423          self.assertEquals(_bytes('<a><b></b><c></c></a>'),
    2424                            canonicalize(result))
    2425
    2426 -    def test_tostring_none(self):
    2427          # ElementTree raises an AssertionError here
    2428          tostring = self.etree.tostring
    2429          self.assertRaises(TypeError, self.etree.tostring, None)
    2430
    2431 -    def test_tostring_pretty(self):
    2432          tostring = self.etree.tostring
    2433          Element = self.etree.Element
    2434          SubElement = self.etree.SubElement
    2435
    2436          a = Element('a')
    2437          b = SubElement(a, 'b')
    2438          c = SubElement(a, 'c')
    2439
    2440          result = tostring(a)
    2441          self.assertEquals(result, _bytes("<a><b/><c/></a>"))
    2442
    2443          result = tostring(a, pretty_print=False)
    2444          self.assertEquals(result, _bytes("<a><b/><c/></a>"))
    2445
    2446          result = tostring(a, pretty_print=True)
    2447          self.assertEquals(result, _bytes("<a>\n  <b/>\n  <c/>\n</a>\n"))
    2448
    2449 -    def test_tostring_with_tail(self):
    2450          tostring = self.etree.tostring
    2451          Element = self.etree.Element
    2452          SubElement = self.etree.SubElement
    2453
    2454          a = Element('a')
    2455          a.tail = "aTAIL"
    2456          b = SubElement(a, 'b')
    2457          b.tail = "bTAIL"
    2458          c = SubElement(a, 'c')
    2459
    2460          result = tostring(a)
    2461          self.assertEquals(result, _bytes("<a><b/>bTAIL<c/></a>aTAIL"))
    2462
    2463          result = tostring(a, with_tail=False)
    2464          self.assertEquals(result, _bytes("<a><b/>bTAIL<c/></a>"))
    2465
    2466          result = tostring(a, with_tail=True)
    2467          self.assertEquals(result, _bytes("<a><b/>bTAIL<c/></a>aTAIL"))
    2468
    2469 -    def test_standalone(self):
    2470          tostring = self.etree.tostring
    2471          XML = self.etree.XML
    2472          ElementTree = self.etree.ElementTree
    2473          Element = self.etree.Element
    2474
    2475          tree = Element("root").getroottree()
    2476          self.assertEquals(None, tree.docinfo.standalone)
    2477
    2478          tree = XML(_bytes("<root/>")).getroottree()
    2479          self.assertEquals(None, tree.docinfo.standalone)
    2480
    2481          tree = XML(_bytes(
    2482              "<?xml version='1.0' encoding='ASCII' standalone='yes'?>\n<root/>"
    2483              )).getroottree()
    2484          self.assertEquals(True, tree.docinfo.standalone)
    2485
    2486          tree = XML(_bytes(
    2487              "<?xml version='1.0' encoding='ASCII' standalone='no'?>\n<root/>"
    2488              )).getroottree()
    2489          self.assertEquals(False, tree.docinfo.standalone)
    2490
    2491 -    def test_tostring_standalone(self):
    2492          tostring = self.etree.tostring
    2493          XML = self.etree.XML
    2494          ElementTree = self.etree.ElementTree
    2495
    2496          root = XML(_bytes("<root/>"))
    2497
    2498          tree = ElementTree(root)
    2499          self.assertEquals(None, tree.docinfo.standalone)
    2500
    2501          result = tostring(root, xml_declaration=True, encoding="ASCII")
    2502          self.assertEquals(result, _bytes(
    2503              "<?xml version='1.0' encoding='ASCII'?>\n<root/>"))
    2504
    2505          result = tostring(root, xml_declaration=True, encoding="ASCII",
    2506                            standalone=True)
    2507          self.assertEquals(result, _bytes(
    2508              "<?xml version='1.0' encoding='ASCII' standalone='yes'?>\n<root/>"))
    2509
    2510          tree = ElementTree(XML(result))
    2511          self.assertEquals(True, tree.docinfo.standalone)
    2512
    2513          result = tostring(root, xml_declaration=True, encoding="ASCII",
    2514                            standalone=False)
    2515          self.assertEquals(result, _bytes(
    2516              "<?xml version='1.0' encoding='ASCII' standalone='no'?>\n<root/>"))
    2517
    2518          tree = ElementTree(XML(result))
    2519          self.assertEquals(False, tree.docinfo.standalone)
    2520
    2521 -    def test_tostring_standalone_in_out(self):
    2522          tostring = self.etree.tostring
    2523          XML = self.etree.XML
    2524          ElementTree = self.etree.ElementTree
    2525
    2526          root = XML(_bytes(
    2527              "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>\n<root/>"))
    2528
    2529          tree = ElementTree(root)
    2530          self.assertEquals(True, tree.docinfo.standalone)
    2531
    2532          result = tostring(root, xml_declaration=True, encoding="ASCII")
    2533          self.assertEquals(result, _bytes(
    2534              "<?xml version='1.0' encoding='ASCII'?>\n<root/>"))
    2535
    2536          result = tostring(root, xml_declaration=True, encoding="ASCII",
    2537                            standalone=True)
    2538          self.assertEquals(result, _bytes(
    2539              "<?xml version='1.0' encoding='ASCII' standalone='yes'?>\n<root/>"))
    2540
    2541 -    def test_tostring_method_text_encoding(self):
    2542          tostring = self.etree.tostring
    2543          Element = self.etree.Element
    2544          SubElement = self.etree.SubElement
    2545
    2546          a = Element('a')
    2547          a.text = "A"
    2548          a.tail = "tail"
    2549          b = SubElement(a, 'b')
    2550          b.text = "B"
    2551          b.tail = _str("Søk på nettet")
    2552          c = SubElement(a, 'c')
    2553          c.text = "C"
    2554
    2555          result = tostring(a, method="text", encoding="UTF-16")
    2556
    2557          self.assertEquals(_str('ABSøk på nettetCtail').encode("UTF-16"),
    2558                            result)
    2559
    2560 -    def test_tostring_method_text_unicode(self):
    2561          tostring = self.etree.tostring
    2562          Element = self.etree.Element
    2563          SubElement = self.etree.SubElement
    2564
    2565          a = Element('a')
    2566          a.text = _str('Søk på nettetA')
    2567          a.tail = "tail"
    2568          b = SubElement(a, 'b')
    2569          b.text = "B"
    2570          b.tail = _str('Søk på nettetB')
    2571          c = SubElement(a, 'c')
    2572          c.text = "C"
    2573
    2574          self.assertRaises(UnicodeEncodeError,
    2575                            tostring, a, method="text")
    2576
    2577          self.assertEquals(
    2578              _str('Søk på nettetABSøk på nettetBCtail').encode('utf-8'),
    2579              tostring(a, encoding="UTF-8", method="text"))
    2580
    2581 -    def test_tounicode(self):
    2582          tounicode = self.etree.tounicode
    2583          Element = self.etree.Element
    2584          SubElement = self.etree.SubElement
    2585
    2586          a = Element('a')
    2587          b = SubElement(a, 'b')
    2588          c = SubElement(a, 'c')
    2589
    2590          self.assert_(isinstance(tounicode(a), _unicode))
    2591          self.assertEquals(_bytes('<a><b></b><c></c></a>'),
    2592                            canonicalize(tounicode(a)))
    2593
    2594 -    def test_tounicode_element(self):
    2595          tounicode = self.etree.tounicode
    2596          Element = self.etree.Element
    2597          SubElement = self.etree.SubElement
    2598
    2599          a = Element('a')
    2600          b = SubElement(a, 'b')
    2601          c = SubElement(a, 'c')
    2602          d = SubElement(c, 'd')
    2603          self.assert_(isinstance(tounicode(b), _unicode))
    2604          self.assert_(isinstance(tounicode(c), _unicode))
    2605          self.assertEquals(_bytes('<b></b>'),
    2606                            canonicalize(tounicode(b)))
    2607          self.assertEquals(_bytes('<c><d></d></c>'),
    2608                            canonicalize(tounicode(c)))
    2609
    2610 -    def test_tounicode_none(self):
    2611          tounicode = self.etree.tounicode
    2612          self.assertRaises(TypeError, self.etree.tounicode, None)
    2613
    2614 -    def test_tounicode_element_tail(self):
    2615          tounicode = self.etree.tounicode
    2616          Element = self.etree.Element
    2617          SubElement = self.etree.SubElement
    2618
    2619          a = Element('a')
    2620          b = SubElement(a, 'b')
    2621          c = SubElement(a, 'c')
    2622          d = SubElement(c, 'd')
    2623          b.tail = 'Foo'
    2624
    2625          self.assert_(isinstance(tounicode(b), _unicode))
    2626          self.assert_(tounicode(b) == '<b/>Foo' or
    2627                       tounicode(b) == '<b />Foo')
    2628
    2629 -    def test_tounicode_pretty(self):
    2630          tounicode = self.etree.tounicode
    2631          Element = self.etree.Element
    2632          SubElement = self.etree.SubElement
    2633
    2634          a = Element('a')
    2635          b = SubElement(a, 'b')
    2636          c = SubElement(a, 'c')
    2637
    2638          result = tounicode(a)
    2639          self.assertEquals(result, "<a><b/><c/></a>")
    2640
    2641          result = tounicode(a, pretty_print=False)
    2642          self.assertEquals(result, "<a><b/><c/></a>")
    2643
    2644          result = tounicode(a, pretty_print=True)
    2645          self.assertEquals(result, "<a>\n  <b/>\n  <c/>\n</a>\n")
    2646
    2647 -    def test_tostring_unicode(self):
    2648          tostring = self.etree.tostring
    2649          Element = self.etree.Element
    2650          SubElement = self.etree.SubElement
    2651
    2652          a = Element('a')
    2653          b = SubElement(a, 'b')
    2654          c = SubElement(a, 'c')
    2655
    2656          self.assert_(isinstance(tostring(a, encoding=_unicode), _unicode))
    2657          self.assertEquals(_bytes('<a><b></b><c></c></a>'),
    2658                            canonicalize(tostring(a, encoding=_unicode)))
    2659
    2660 -    def test_tostring_unicode_element(self):
    2661          tostring = self.etree.tostring
    2662          Element = self.etree.Element
    2663          SubElement = self.etree.SubElement
    2664
    2665          a = Element('a')
    2666          b = SubElement(a, 'b')
    2667          c = SubElement(a, 'c')
    2668          d = SubElement(c, 'd')
    2669          self.assert_(isinstance(tostring(b, encoding=_unicode), _unicode))
    2670          self.assert_(isinstance(tostring(c, encoding=_unicode), _unicode))
    2671          self.assertEquals(_bytes('<b></b>'),
    2672                            canonicalize(tostring(b, encoding=_unicode)))
    2673          self.assertEquals(_bytes('<c><d></d></c>'),
    2674                            canonicalize(tostring(c, encoding=_unicode)))
    2675
    2676 -    def test_tostring_unicode_none(self):
    2677          tostring = self.etree.tostring
    2678          self.assertRaises(TypeError, self.etree.tostring,
    2679                            None, encoding=_unicode)
    2680
    2681 -    def test_tostring_unicode_element_tail(self):
    2682          tostring = self.etree.tostring
    2683          Element = self.etree.Element
    2684          SubElement = self.etree.SubElement
    2685
    2686          a = Element('a')
    2687          b = SubElement(a, 'b')
    2688          c = SubElement(a, 'c')
    2689          d = SubElement(c, 'd')
    2690          b.tail = 'Foo'
    2691
    2692          self.assert_(isinstance(tostring(b, encoding=_unicode), _unicode))
    2693          self.assert_(tostring(b, encoding=_unicode) == '<b/>Foo' or
    2694                       tostring(b, encoding=_unicode) == '<b />Foo')
    2695
    2696 -    def test_tostring_unicode_pretty(self):
    2697          tostring = self.etree.tostring
    2698          Element = self.etree.Element
    2699          SubElement = self.etree.SubElement
    2700
    2701          a = Element('a')
    2702          b = SubElement(a, 'b')
    2703          c = SubElement(a, 'c')
    2704
    2705          result = tostring(a, encoding=_unicode)
    2706          self.assertEquals(result, "<a><b/><c/></a>")
    2707
    2708          result = tostring(a, encoding=_unicode, pretty_print=False)
    2709          self.assertEquals(result, "<a><b/><c/></a>")
    2710
    2711          result = tostring(a, encoding=_unicode, pretty_print=True)
    2712          self.assertEquals(result, "<a>\n  <b/>\n  <c/>\n</a>\n")
    2713
    2714      # helper methods
    2715
    2716 -    def _writeElement(self, element, encoding='us-ascii', compression=0):
    2717          """Write out element for comparison.
    2718          """
    2719          ElementTree = self.etree.ElementTree
    2720          f = BytesIO()
    2721          tree = ElementTree(element=element)
    2722          tree.write(f, encoding=encoding, compression=compression)
    2723          data = f.getvalue()
    2724          if compression:
    2725              data = zlib.decompress(data)
    2726          return canonicalize(data)
    2727
    2728
    2729 -class XIncludeTestCase(HelperTestCase):
    2730 -    def test_xinclude_text(self):
    2731          filename = fileInTestDir('test_broken.xml')
    2732          root = etree.XML(_bytes('''\
    2733          <doc xmlns:xi="http://www.w3.org/2001/XInclude">
    2734            <xi:include href="%s" parse="text"/>
    2735          </doc>
    2736          ''' % filename))
    2737          old_text = root.text
    2738          content = open(filename).read()
    2739          old_tail = root[0].tail
    2740
    2741          self.include( etree.ElementTree(root) )
    2742          self.assertEquals(old_text + content + old_tail,
    2743                            root.text)
    2744
    2745 -    def test_xinclude(self):
    2746          tree = etree.parse(fileInTestDir('include/test_xinclude.xml'))
    2747          self.assertNotEquals(
    2748              'a',
    2749              tree.getroot()[1].tag)
    2750          # process xincludes
    2751          self.include( tree )
    2752          # check whether we find it replaced with included data
    2753          self.assertEquals(
    2754              'a',
    2755              tree.getroot()[1].tag)
    2756
    2757 -    def test_xinclude_resolver(self):
    2758          class res(etree.Resolver):
    2759              include_text = open(fileInTestDir('test.xml')).read()
    2760              called = {}
    2761              def resolve(self, url, id, context):
    2762                  if url.endswith(".dtd"):
    2763                      self.called["dtd"] = True
    2764                      return self.resolve_filename(
    2765                          fileInTestDir('test.dtd'), context)
    2766                  elif url.endswith("test_xinclude.xml"):
    2767                      self.called["input"] = True
    2768                      return None # delegate to default resolver
    2769                  else:
    2770                      self.called["include"] = True
    2771                      return self.resolve_string(self.include_text, context)
    2772
    2773          res_instance = res()
    2774          parser = etree.XMLParser(load_dtd = True)
    2775          parser.resolvers.add(res_instance)
    2776
    2777          tree = etree.parse(fileInTestDir('include/test_xinclude.xml'),
    2778                             parser = parser)
    2779
    2780          self.include(tree)
    2781
    2782          called = list(res_instance.called.items())
    2783          called.sort()
    2784          self.assertEquals(
    2785              [("dtd", True), ("include", True), ("input", True)],
    2786              called)
    2787
    2788 -class ETreeXIncludeTestCase(XIncludeTestCase):
    2789 -    def include(self, tree):
    2790          tree.xinclude()
    2791
    2792
    2793 -class ElementIncludeTestCase(XIncludeTestCase):
    2794      from lxml import ElementInclude
    2795 -    def include(self, tree):
    2796          self.ElementInclude.include(tree.getroot())
    2797
    2798
    2799 -class ETreeC14NTestCase(HelperTestCase):
    2800 -    def test_c14n(self):
    2801          tree = self.parse(_bytes('<a><b/></a>'))
    2802          f = BytesIO()
    2803          tree.write_c14n(f)
    2804          s = f.getvalue()
    2805          self.assertEquals(_bytes('<a><b></b></a>'),
    2806                            s)
    2807
    2808 -    def test_c14n_gzip(self):
    2809          tree = self.parse(_bytes('<a>'+'<b/>'*200+'</a>'))
    2810          f = BytesIO()
    2811          tree.write_c14n(f, compression=9)
    2812          s = gzip.GzipFile(fileobj=BytesIO(f.getvalue())).read()
    2813          self.assertEquals(_bytes('<a>'+'<b></b>'*200+'</a>'),
    2814                            s)
    2815
    2816 -    def test_c14n_file(self):
    2817          tree = self.parse(_bytes('<a><b/></a>'))
    2818          handle, filename = tempfile.mkstemp()
    2819          try:
    2820              tree.write_c14n(filename)
    2821              f = open(filename, 'rb')
    2822              data = f.read()
    2823              f.close()
    2824          finally:
    2825              os.close(handle)
    2826              os.remove(filename)
    2827          self.assertEquals(_bytes('<a><b></b></a>'),
    2828                            data)
    2829
    2830 -    def test_c14n_file_gzip(self):
    2831          tree = self.parse(_bytes('<a>'+'<b/>'*200+'</a>'))
    2832          handle, filename = tempfile.mkstemp()
    2833          try:
    2834              tree.write_c14n(filename, compression=9)
    2835              f = gzip.open(filename, 'rb')
    2836              data = f.read()
    2837              f.close()
    2838          finally:
    2839              os.close(handle)
    2840              os.remove(filename)
    2841          self.assertEquals(_bytes('<a>'+'<b></b>'*200+'</a>'),
    2842                            data)
    2843
    2844 -    def test_c14n_with_comments(self):
    2845          tree = self.parse(_bytes('<!--hi--><a><!--ho--><b/></a><!--hu-->'))
    2846          f = BytesIO()
    2847          tree.write_c14n(f)
    2848          s = f.getvalue()
    2849          self.assertEquals(_bytes('<!--hi-->\n<a><!--ho--><b></b></a>\n<!--hu-->'),
    2850                            s)
    2851          f = BytesIO()
    2852          tree.write_c14n(f, with_comments=True)
    2853          s = f.getvalue()
    2854          self.assertEquals(_bytes('<!--hi-->\n<a><!--ho--><b></b></a>\n<!--hu-->'),
    2855                            s)
    2856          f = BytesIO()
    2857          tree.write_c14n(f, with_comments=False)
    2858          s = f.getvalue()
    2859          self.assertEquals(_bytes('<a><b></b></a>'),
    2860                            s)
    2861
    2862 -    def test_c14n_exclusive(self):
    2863          tree = self.parse(_bytes(
    2864                  '<a xmlns="http://abc" xmlns:y="http://bcd" xmlns:z="http://cde"><z:b/></a>'))
    2865          f = BytesIO()
    2866          tree.write_c14n(f)
    2867          s = f.getvalue()
    2868          self.assertEquals(_bytes('<a xmlns="http://abc" xmlns:y="http://bcd" xmlns:z="http://cde"><z:b></z:b></a>'),
    2869                            s)
    2870          f = BytesIO()
    2871          tree.write_c14n(f, exclusive=False)
    2872          s = f.getvalue()
    2873          self.assertEquals(_bytes('<a xmlns="http://abc" xmlns:y="http://bcd" xmlns:z="http://cde"><z:b></z:b></a>'),
    2874                            s)
    2875          f = BytesIO()
    2876          tree.write_c14n(f, exclusive=True)
    2877          s = f.getvalue()
    2878          self.assertEquals(_bytes('<a xmlns="http://abc"><z:b xmlns:z="http://cde"></z:b></a>'),
    2879                            s)
    2880
    2881
    2882 -class ETreeWriteTestCase(HelperTestCase):
    2883 -    def test_write(self):
    2884          tree = self.parse(_bytes('<a><b/></a>'))
    2885          f = BytesIO()
    2886          tree.write(f)
    2887          s = f.getvalue()
    2888          self.assertEquals(_bytes('<a><b/></a>'),
    2889                            s)
    2890
    2891 -    def test_write_gzip(self):
    2892          tree = self.parse(_bytes('<a>'+'<b/>'*200+'</a>'))
    2893          f = BytesIO()
    2894          tree.write(f, compression=9)
    2895          s = gzip.GzipFile(fileobj=BytesIO(f.getvalue())).read()
    2896          self.assertEquals(_bytes('<a>'+'<b/>'*200+'</a>'),
    2897                            s)
    2898
    2899 -    def test_write_gzip_level(self):
    2900          tree = self.parse(_bytes('<a>'+'<b/>'*200+'</a>'))
    2901          f = BytesIO()
    2902          tree.write(f, compression=0)
    2903          s0 = f.getvalue()
    2904
    2905          f = BytesIO()
    2906          tree.write(f)
    2907          self.assertEquals(f.getvalue(), s0)
    2908
    2909          f = BytesIO()
    2910          tree.write(f, compression=1)
    2911          s = f.getvalue()
    2912          self.assert_(len(s) <= len(s0))
    2913          s1 = gzip.GzipFile(fileobj=BytesIO(s)).read()
    2914
    2915          f = BytesIO()
    2916          tree.write(f, compression=9)
    2917          s = f.getvalue()
    2918          self.assert_(len(s) <= len(s0))
    2919          s9 = gzip.GzipFile(fileobj=BytesIO(s)).read()
    2920
    2921          self.assertEquals(_bytes('<a>'+'<b/>'*200+'</a>'),
    2922                            s0)
    2923          self.assertEquals(_bytes('<a>'+'<b/>'*200+'</a>'),
    2924                            s1)
    2925          self.assertEquals(_bytes('<a>'+'<b/>'*200+'</a>'),
    2926                            s9)
    2927
    2928 -    def test_write_file(self):
    2929          tree = self.parse(_bytes('<a><b/></a>'))
    2930          handle, filename = tempfile.mkstemp()
    2931          try:
    2932              tree.write(filename)
    2933              f = open(filename, 'rb')
    2934              data = f.read()
    2935              f.close()
    2936          finally:
    2937              os.close(handle)
    2938              os.remove(filename)
    2939          self.assertEquals(_bytes('<a><b/></a>'),
    2940                            data)
    2941
    2942 -    def test_write_file_gzip(self):
    2943          tree = self.parse(_bytes('<a>'+'<b/>'*200+'</a>'))
    2944          handle, filename = tempfile.mkstemp()
    2945          try:
    2946              tree.write(filename, compression=9)
    2947              f = gzip.open(filename, 'rb')
    2948              data = f.read()
    2949              f.close()
    2950          finally:
    2951              os.close(handle)
    2952              os.remove(filename)
    2953          self.assertEquals(_bytes('<a>'+'<b/>'*200+'</a>'),
    2954                            data)
    2955
    2956 -    def test_write_file_gzip_parse(self):
    2957          tree = self.parse(_bytes('<a>'+'<b/>'*200+'</a>'))
    2958          handle, filename = tempfile.mkstemp()
    2959          try:
    2960              tree.write(filename, compression=9)
    2961              data = etree.tostring(etree.parse(filename))
    2962          finally:
    2963              os.close(handle)
    2964              os.remove(filename)
    2965          self.assertEquals(_bytes('<a>'+'<b/>'*200+'</a>'),
    2966                            data)
    2967
    2968 -    def test_write_file_gzipfile_parse(self):
    2969          tree = self.parse(_bytes('<a>'+'<b/>'*200+'</a>'))
    2970          handle, filename = tempfile.mkstemp()
    2971          try:
    2972              tree.write(filename, compression=9)
    2973              data = etree.tostring(etree.parse(
    2974                  gzip.GzipFile(filename)))
    2975          finally:
    2976              os.close(handle)
    2977              os.remove(filename)
    2978          self.assertEquals(_bytes('<a>'+'<b/>'*200+'</a>'),
    2979                            data)
    2980
    2981 -class ETreeErrorLogTest(HelperTestCase):
    2982      etree = etree
    2983
    2984 -    def test_parse_error_logging(self):
    2985          parse = self.etree.parse
    2986          f = BytesIO('<a><b></c></b></a>')
    2987          self.etree.clear_error_log()
    2988          try:
    2989              parse(f)
    2990              logs = None
    2991          except SyntaxError:
    2992              e = sys.exc_info()[1]
    2993              logs = e.error_log
    2994          f.close()
    2995          self.assert_([ log for log in logs
    2996                         if 'mismatch' in log.message ])
    2997          self.assert_([ log for log in logs
    2998                         if 'PARSER'   in log.domain_name])
    2999          self.assert_([ log for log in logs
    3000                         if 'ERR_TAG_NAME_MISMATCH' in log.type_name ])
    3001          self.assert_([ log for log in logs
    3002                         if 1 == log.line ])
    3003          self.assert_([ log for log in logs
    3004                         if 15 == log.column ])
    3005
    3006 -    def _test_python_error_logging(self):
    3007          """This can't really be tested as long as there isn't a way to
    3008          reset the logging setup ...
    3009          """
    3010          parse = self.etree.parse
    3011
    3012          messages = []
    3013          class Logger(self.etree.PyErrorLog):
    3014              def log(self, entry, message, *args):
    3015                  messages.append(message)
    3016
    3017          self.etree.use_global_python_log(Logger())
    3018          f = BytesIO('<a><b></c></b></a>')
    3019          try:
    3020              parse(f)
    3021          except SyntaxError:
    3022              pass
    3023          f.close()
    3024
    3025          self.assert_([ message for message in messages
    3026                         if 'mismatch' in message ])
    3027          self.assert_([ message for message in messages
    3028                         if ':PARSER:'   in message])
    3029          self.assert_([ message for message in messages
    3030                         if ':ERR_TAG_NAME_MISMATCH:' in message ])
    3031          self.assert_([ message for message in messages
    3032                         if ':1:15:' in message ])
    3033
    3034 -def test_suite():
    3035      suite = unittest.TestSuite()
    3036      suite.addTests([unittest.makeSuite(ETreeOnlyTestCase)])
    3037      suite.addTests([unittest.makeSuite(ETreeXIncludeTestCase)])
    3038      suite.addTests([unittest.makeSuite(ElementIncludeTestCase)])
    3039      suite.addTests([unittest.makeSuite(ETreeC14NTestCase)])
    3040      suite.addTests([unittest.makeSuite(ETreeWriteTestCase)])
    3041      suite.addTests([unittest.makeSuite(ETreeErrorLogTest)])
    3042      suite.addTests(
    3043          [make_doctest('../../../doc/tutorial.txt')])
    3044      suite.addTests(
    3045          [make_doctest('../../../doc/api.txt')])
    3046      suite.addTests(
    3047          [make_doctest('../../../doc/FAQ.txt')])
    3048      suite.addTests(
    3049          [make_doctest('../../../doc/parsing.txt')])
    3050      suite.addTests(
    3051          [make_doctest('../../../doc/resolvers.txt')])
    3052      return suite
    3053
    3054  if __name__ == '__main__':
    3055      print('to test use test.py %s' % __file__)
    3056