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