Package lxml :: Package tests :: Module test_sax

[hide private]
[frames] | no frames]

# Source Code for Module lxml.tests.test_sax

      1  # -*- coding: utf-8 -*-
      2
      3  """
      4  Test cases related to SAX I/O
      5  """
      6
      7  import unittest, sys, os.path
      8
      9  this_dir = os.path.dirname(__file__)
     10  if this_dir not in sys.path:
     11      sys.path.insert(0, this_dir) # needed for Py3
     12
     13  from common_imports import HelperTestCase, doctest, make_doctest, BytesIO, _bytes
     14  from lxml import sax
     15  from xml.dom import pulldom
     16
     17 -class ETreeSaxTestCase(HelperTestCase):
     18
     19 -    def test_etree_sax_simple(self):
     20          tree = self.parse('<a>ab<b/>ba</a>')
     21          xml_out = self._saxify_serialize(tree)
     22          self.assertEquals(_bytes('<a>ab<b/>ba</a>'),
     23                            xml_out)
     24
     25 -    def test_etree_sax_double(self):
     26          tree = self.parse('<a>ab<b>bb</b>ba</a>')
     27          xml_out = self._saxify_serialize(tree)
     28          self.assertEquals(_bytes('<a>ab<b>bb</b>ba</a>'),
     29                            xml_out)
     30
     31 -    def test_etree_sax_comment(self):
     32          tree = self.parse('<a>ab<!-- TEST -->ba</a>')
     33          xml_out = self._saxify_serialize(tree)
     34          self.assertEquals(_bytes('<a>abba</a>'),
     35                            xml_out)
     36
     37 -    def test_etree_sax_pi(self):
     38          tree = self.parse('<a>ab<?this and that?>ba</a>')
     39          xml_out = self._saxify_serialize(tree)
     40          self.assertEquals(_bytes('<a>ab<?this and that?>ba</a>'),
     41                            xml_out)
     42
     43 -    def test_etree_sax_comment_root(self):
     44          tree = self.parse('<!-- TEST --><a>ab</a>')
     45          xml_out = self._saxify_serialize(tree)
     46          self.assertEquals(_bytes('<a>ab</a>'),
     47                            xml_out)
     48
     49 -    def test_etree_sax_pi_root(self):
     50          tree = self.parse('<?this and that?><a>ab</a>')
     51          xml_out = self._saxify_serialize(tree)
     52          self.assertEquals(_bytes('<?this and that?><a>ab</a>'),
     53                            xml_out)
     54
     55 -    def test_etree_sax_attributes(self):
     56          tree = self.parse('<a aa="5">ab<b b="5"/>ba</a>')
     57          xml_out = self._saxify_serialize(tree)
     58          self.assertEquals(_bytes('<a aa="5">ab<b b="5"/>ba</a>'),
     59                            xml_out)
     60
     61 -    def test_etree_sax_ns1(self):
     62          tree = self.parse('<a xmlns="bla">ab<b>bb</b>ba</a>')
     63          new_tree = self._saxify_unsaxify(tree)
     64          root = new_tree.getroot()
     65          self.assertEqual('{bla}a',
     66                           root.tag)
     67          self.assertEqual('{bla}b',
     68                           root[0].tag)
     69
     70 -    def test_etree_sax_ns2(self):
     71          tree = self.parse('<a xmlns="blaA">ab<b:b xmlns:b="blaB">bb</b:b>ba</a>')
     72          new_tree = self._saxify_unsaxify(tree)
     73          root = new_tree.getroot()
     74          self.assertEqual('{blaA}a',
     75                           root.tag)
     76          self.assertEqual('{blaB}b',
     77                           root[0].tag)
     78
     79 -    def test_sax_to_pulldom(self):
     80          tree = self.parse('<a xmlns="blaA">ab<b:b xmlns:b="blaB">bb</b:b>ba</a>')
     81          handler = pulldom.SAX2DOM()
     82          sax.saxify(tree, handler)
     83          dom = handler.document
     84
     85          self.assertEqual('a',
     86                           dom.firstChild.localName)
     87          self.assertEqual('blaA',
     88                           dom.firstChild.namespaceURI)
     89
     90          children = dom.firstChild.childNodes
     91          self.assertEqual('ab',
     92                           children[0].nodeValue)
     93          self.assertEqual('blaB',
     94                           children[1].namespaceURI)
     95          self.assertEqual('ba',
     96                           children[2].nodeValue)
     97
     98 -    def test_element_sax(self):
     99          tree = self.parse('<a><b/></a>')
    100          a = tree.getroot()
    101          b = a[0]
    102
    103          xml_out = self._saxify_serialize(a)
    104          self.assertEquals(_bytes('<a><b/></a>'),
    105                            xml_out)
    106
    107          xml_out = self._saxify_serialize(b)
    108          self.assertEquals(_bytes('<b/>'),
    109                            xml_out)
    110
    111 -    def test_element_sax_ns(self):
    112          tree = self.parse('<a:a xmlns:a="blaA"><b/></a:a>')
    113          a = tree.getroot()
    114          b = a[0]
    115
    116          new_tree = self._saxify_unsaxify(a)
    117          root = new_tree.getroot()
    118          self.assertEqual('{blaA}a',
    119                           root.tag)
    120          self.assertEqual('b',
    121                           root[0].tag)
    122
    123          new_tree = self._saxify_unsaxify(b)
    124          root = new_tree.getroot()
    125          self.assertEqual('b',
    126                           root.tag)
    127          self.assertEqual(0,
    128                           len(root))
    129
    130 -    def test_etree_sax_handler_default_ns(self):
    131          handler = sax.ElementTreeContentHandler()
    132          handler.startDocument()
    133          handler.startPrefixMapping(None, 'blaA')
    134          handler.startElementNS(('blaA', 'a'), 'a', {})
    135          handler.startPrefixMapping(None, 'blaB')
    136          handler.startElementNS(('blaB', 'b'), 'b', {})
    137          handler.endElementNS(  ('blaB', 'b'), 'b')
    138          handler.endPrefixMapping(None)
    139          handler.startElementNS(('blaA', 'c'), 'c', {})
    140          handler.endElementNS(  ('blaA', 'c'), 'c')
    141          handler.endElementNS(  ('blaA', 'a'), 'a')
    142          handler.endPrefixMapping(None)
    143          handler.endDocument()
    144
    145          new_tree = handler.etree
    146          root = new_tree.getroot()
    147          self.assertEqual('{blaA}a',
    148                           root.tag)
    149          self.assertEqual('{blaB}b',
    150                           root[0].tag)
    151          self.assertEqual('{blaA}c',
    152                           root[1].tag)
    153
    154 -    def test_etree_sax_redefine_ns(self):
    155          handler = sax.ElementTreeContentHandler()
    156          handler.startDocument()
    157          handler.startPrefixMapping('ns', 'blaA')
    158          handler.startElementNS(('blaA', 'a'), 'ns:a', {})
    159          handler.startPrefixMapping('ns', 'blaB')
    160          handler.startElementNS(('blaB', 'b'), 'ns:b', {})
    161          handler.endElementNS(  ('blaB', 'b'), 'ns:b')
    162          handler.endPrefixMapping('ns')
    163          handler.startElementNS(('blaA', 'c'), 'ns:c', {})
    164          handler.endElementNS(  ('blaA', 'c'), 'ns:c')
    165          handler.endElementNS(  ('blaA', 'a'), 'ns:a')
    166          handler.endPrefixMapping('ns')
    167          handler.endDocument()
    168
    169          new_tree = handler.etree
    170          root = new_tree.getroot()
    171          self.assertEqual('{blaA}a',
    172                           root.tag)
    173          self.assertEqual('{blaB}b',
    174                           root[0].tag)
    175          self.assertEqual('{blaA}c',
    176                           root[1].tag)
    177
    178 -    def test_etree_sax_no_ns(self):
    179          handler = sax.ElementTreeContentHandler()
    180          handler.startDocument()
    181          handler.startElement('a', {})
    182          handler.startElement('b', {})
    183          handler.endElement('b')
    184          handler.startElement('c') # with empty attributes
    185          handler.endElement('c')
    186          handler.endElement('a')
    187          handler.endDocument()
    188
    189          new_tree = handler.etree
    190          root = new_tree.getroot()
    191          self.assertEqual('a', root.tag)
    192          self.assertEqual('b', root[0].tag)
    193          self.assertEqual('c', root[1].tag)
    194
    195 -    def test_etree_sax_error(self):
    196          handler = sax.ElementTreeContentHandler()
    197          handler.startDocument()
    198          handler.startElement('a')
    199          self.assertRaises(sax.SaxError, handler.endElement, 'b')
    200
    201 -    def test_etree_sax_error2(self):
    202          handler = sax.ElementTreeContentHandler()
    203          handler.startDocument()
    204          handler.startElement('a')
    205          handler.startElement('b')
    206          self.assertRaises(sax.SaxError, handler.endElement, 'a')
    207
    208 -    def _saxify_unsaxify(self, saxifiable):
    209          handler = sax.ElementTreeContentHandler()
    210          sax.ElementTreeProducer(saxifiable, handler).saxify()
    211          return handler.etree
    212
    213 -    def _saxify_serialize(self, tree):
    214          new_tree = self._saxify_unsaxify(tree)
    215          f = BytesIO()
    216          new_tree.write(f)
    217          return f.getvalue().replace(_bytes('\n'), _bytes(''))
    218
    219
    220 -def test_suite():
    221      suite = unittest.TestSuite()
    222      suite.addTests([unittest.makeSuite(ETreeSaxTestCase)])
    223      suite.addTests(
    224          [make_doctest('../../../doc/sax.txt')])
    225      return suite
    226
    227  if __name__ == '__main__':
    228      print('to test use test.py %s' % __file__)
    229