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_handler_default_ns_None(self):
    155          handler = sax.ElementTreeContentHandler()
    156          handler.startDocument()
    157          handler.startPrefixMapping(None, 'blaA')
    158          handler.startElementNS((None, 'a'), 'a', {})
    159          handler.startPrefixMapping(None, 'blaB')
    160          handler.startElementNS((None, 'b'), 'b', {})
    161          handler.endElementNS(  (None, 'b'), 'b')
    162          handler.endPrefixMapping(None)
    163          handler.startElementNS((None, 'c'), 'c', {})
    164          handler.endElementNS(  (None, 'c'), 'c')
    165          handler.endElementNS(  (None, 'a'), 'a')
    166          handler.endPrefixMapping(None)
    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_redefine_ns(self):
    179          handler = sax.ElementTreeContentHandler()
    180          handler.startDocument()
    181          handler.startPrefixMapping('ns', 'blaA')
    182          handler.startElementNS(('blaA', 'a'), 'ns:a', {})
    183          handler.startPrefixMapping('ns', 'blaB')
    184          handler.startElementNS(('blaB', 'b'), 'ns:b', {})
    185          handler.endElementNS(  ('blaB', 'b'), 'ns:b')
    186          handler.endPrefixMapping('ns')
    187          handler.startElementNS(('blaA', 'c'), 'ns:c', {})
    188          handler.endElementNS(  ('blaA', 'c'), 'ns:c')
    189          handler.endElementNS(  ('blaA', 'a'), 'ns:a')
    190          handler.endPrefixMapping('ns')
    191          handler.endDocument()
    192
    193          new_tree = handler.etree
    194          root = new_tree.getroot()
    195          self.assertEqual('{blaA}a',
    196                           root.tag)
    197          self.assertEqual('{blaB}b',
    198                           root[0].tag)
    199          self.assertEqual('{blaA}c',
    200                           root[1].tag)
    201
    202 -    def test_etree_sax_no_ns(self):
    203          handler = sax.ElementTreeContentHandler()
    204          handler.startDocument()
    205          handler.startElement('a', {})
    206          handler.startElement('b', {})
    207          handler.endElement('b')
    208          handler.startElement('c') # with empty attributes
    209          handler.endElement('c')
    210          handler.endElement('a')
    211          handler.endDocument()
    212
    213          new_tree = handler.etree
    214          root = new_tree.getroot()
    215          self.assertEqual('a', root.tag)
    216          self.assertEqual('b', root[0].tag)
    217          self.assertEqual('c', root[1].tag)
    218
    219 -    def test_etree_sax_error(self):
    220          handler = sax.ElementTreeContentHandler()
    221          handler.startDocument()
    222          handler.startElement('a')
    223          self.assertRaises(sax.SaxError, handler.endElement, 'b')
    224
    225 -    def test_etree_sax_error2(self):
    226          handler = sax.ElementTreeContentHandler()
    227          handler.startDocument()
    228          handler.startElement('a')
    229          handler.startElement('b')
    230          self.assertRaises(sax.SaxError, handler.endElement, 'a')
    231
    232 -    def _saxify_unsaxify(self, saxifiable):
    233          handler = sax.ElementTreeContentHandler()
    234          sax.ElementTreeProducer(saxifiable, handler).saxify()
    235          return handler.etree
    236
    237 -    def _saxify_serialize(self, tree):
    238          new_tree = self._saxify_unsaxify(tree)
    239          f = BytesIO()
    240          new_tree.write(f)
    241          return f.getvalue().replace(_bytes('\n'), _bytes(''))
    242
    243
    244 -def test_suite():
    245      suite = unittest.TestSuite()
    246      suite.addTests([unittest.makeSuite(ETreeSaxTestCase)])
    247      suite.addTests(
    248          [make_doctest('../../../doc/sax.txt')])
    249      return suite
    250
    251  if __name__ == '__main__':
    252      print('to test use test.py %s' % __file__)
    253