Package lxml :: Package tests :: Module test_nsclasses |
[hide private]
[frames] | no frames]

# Source Code for Module lxml.tests.test_nsclasses

      1  # -*- coding: utf-8 -*-
      2
      3  """
      4  Test cases related to namespace implementation classes and the
      5  namespace registry mechanism
      6  """
      7
      8  import unittest, sys, os.path
      9
     10  this_dir = os.path.dirname(__file__)
     11  if this_dir not in sys.path:
     12      sys.path.insert(0, this_dir) # needed for Py3
     13
     14  from common_imports import etree, HelperTestCase, _bytes
     15  from common_imports import doctest, make_doctest
     16
     17 -class ETreeNamespaceClassesTestCase(HelperTestCase):
     18
     19 -    class default_class(etree.ElementBase):
     20          pass
     21 -    class maeh_class(etree.ElementBase):
     22 -        def maeh(self):
     23              return 'maeh'
     24 -    class bluff_class(etree.ElementBase):
     25 -        def bluff(self):
     26              return 'bluff'
     27
     28 -    def setUp(self):
     29          super(ETreeNamespaceClassesTestCase, self).setUp()
     30          lookup = etree.ElementNamespaceClassLookup()
     31          self.Namespace = lookup.get_namespace
     32          parser = etree.XMLParser()
     33          parser.set_element_class_lookup(lookup)
     34          etree.set_default_parser(parser)
     35
     36 -    def tearDown(self):
     37          etree.set_default_parser()
     38          del self.Namespace
     39          super(ETreeNamespaceClassesTestCase, self).tearDown()
     40
     41 -    def test_registry(self):
     42          ns = self.Namespace('ns01')
     43          ns['maeh'] = self.maeh_class
     44
     45          self.Namespace('ns01').clear()
     46
     47          self.Namespace('ns02').update({'maeh'  : self.maeh_class})
     48          self.Namespace('ns03').update({'bluff' : self.bluff_class}.items())
     49          self.Namespace('ns02').clear()
     50          self.Namespace('ns03').clear()
     51
     52 -    def test_ns_classes(self):
     53          bluff_dict = {'bluff' : self.bluff_class}
     54          maeh_dict  = {'maeh'  : self.maeh_class}
     55
     56          self.Namespace('ns10').update(bluff_dict)
     57
     58          tree = self.parse(_bytes('<bluff xmlns="ns10"><ns11:maeh xmlns:ns11="ns11"/></bluff>'))
     59
     60          el = tree.getroot()
     61          self.assert_(isinstance(el, etree.ElementBase))
     62          self.assert_(hasattr(el, 'bluff'))
     63          self.assertFalse(hasattr(el[0], 'maeh'))
     64          self.assertFalse(hasattr(el[0], 'bluff'))
     65          self.assertEquals(el.bluff(), 'bluff')
     66          del el
     67
     68          self.Namespace('ns11').update(maeh_dict)
     69          el = tree.getroot()
     70          self.assert_(hasattr(el, 'bluff'))
     71          self.assert_(hasattr(el[0], 'maeh'))
     72          self.assertEquals(el.bluff(), 'bluff')
     73          self.assertEquals(el[0].maeh(), 'maeh')
     74          del el
     75
     76          self.Namespace('ns10').clear()
     77
     78          tree = self.parse(_bytes('<bluff xmlns="ns10"><ns11:maeh xmlns:ns11="ns11"/></bluff>'))
     79          el = tree.getroot()
     80          self.assertFalse(hasattr(el, 'bluff'))
     81          self.assertFalse(hasattr(el, 'maeh'))
     82          self.assertFalse(hasattr(el[0], 'bluff'))
     83          self.assert_(hasattr(el[0], 'maeh'))
     84
     85          self.Namespace('ns11').clear()
     86
     87 -    def test_default_tagname(self):
     88          bluff_dict = {
     89              None   : self.bluff_class,
     90              'maeh' : self.maeh_class
     91              }
     92
     93          ns = self.Namespace("uri:nsDefClass")
     94          ns.update(bluff_dict)
     95
     96          tree = self.parse(_bytes('''
     97              <test xmlns="bla" xmlns:ns1="uri:nsDefClass" xmlns:ns2="uri:nsDefClass">
     98                <ns2:el1/><ns1:el2/><ns1:maeh/><ns2:maeh/><maeh/>
     99              </test>
    100              '''))
    101
    102          el = tree.getroot()
    103          self.assertFalse(isinstance(el, etree.ElementBase))
    104          for child in el[:-1]:
    105              self.assert_(isinstance(child, etree.ElementBase), child.tag)
    106          self.assertFalse(isinstance(el[-1], etree.ElementBase))
    107
    108          self.assert_(hasattr(el[0], 'bluff'))
    109          self.assert_(hasattr(el[1], 'bluff'))
    110          self.assert_(hasattr(el[2], 'maeh'))
    111          self.assert_(hasattr(el[3], 'maeh'))
    112          self.assertFalse(hasattr(el[4], 'maeh'))
    113          del el
    114
    115          ns.clear()
    116
    117 -    def test_create_element(self):
    118          bluff_dict = {'bluff' : self.bluff_class}
    119          self.Namespace('ns20').update(bluff_dict)
    120
    121          maeh_dict  = {'maeh'  : self.maeh_class}
    122          self.Namespace('ns21').update(maeh_dict)
    123
    124          el = etree.Element("{ns20}bluff")
    125          self.assert_(hasattr(el, 'bluff'))
    126
    127          child = etree.SubElement(el, "{ns21}maeh")
    128          self.assert_(hasattr(child, 'maeh'))
    129          child = etree.SubElement(el, "{ns20}bluff")
    130          self.assert_(hasattr(child, 'bluff'))
    131          child = etree.SubElement(el, "{ns21}bluff")
    132          self.assertFalse(hasattr(child, 'bluff'))
    133          self.assertFalse(hasattr(child, 'maeh'))
    134
    135          self.assert_(hasattr(el[0], 'maeh'))
    136          self.assert_(hasattr(el[1], 'bluff'))
    137          self.assertFalse(hasattr(el[2], 'bluff'))
    138          self.assertFalse(hasattr(el[2], 'maeh'))
    139
    140          self.assertEquals(el.bluff(), 'bluff')
    141          self.assertEquals(el[0].maeh(), 'maeh')
    142          self.assertEquals(el[1].bluff(), 'bluff')
    143
    144          self.Namespace('ns20').clear()
    145          self.Namespace('ns21').clear()
    146
    147 -    def test_create_element_default(self):
    148          bluff_dict = {None : self.bluff_class}
    149          self.Namespace('ns30').update(bluff_dict)
    150
    151          maeh_dict  = {'maeh'  : self.maeh_class}
    152          self.Namespace(None).update(maeh_dict)
    153
    154          el = etree.Element("{ns30}bluff")
    155          etree.SubElement(el, "maeh")
    156          self.assert_(hasattr(el, 'bluff'))
    157          self.assert_(hasattr(el[0], 'maeh'))
    158          self.assertEquals(el.bluff(), 'bluff')
    159          self.assertEquals(el[0].maeh(), 'maeh')
    160
    161          self.Namespace(None).clear()
    162          self.Namespace('ns30').clear()
    163
    164 -    def test_element_creation(self):
    165          default, bluff, maeh = (
    166              self.default_class, self.bluff_class, self.maeh_class)
    167
    168          class honk(etree.ElementBase):
    169              TAG = 'HONK'
    170              NAMESPACE = 'http://a.b/c'
    171
    172          el = default(
    173              "test",
    174              "text",
    175              bluff(honk, "TaIL", maeh),
    176              maeh("TeXT", bluff, honk(), "TAiL"),
    177              "Tail")
    178
    179          self.assertEquals('default_class', el.tag)
    180          self.assertEquals('testtext', el.text)
    181          self.assertEquals(None, el.tail)
    182          self.assertEquals(2, len(el))
    183          self.assertEquals(7, len(list(el.iter())))
    184
    185          self.assertEquals('bluff_class', el[0].tag)
    186          self.assertEquals('TaIL', el[0][0].tail)
    187          self.assertEquals('TaIL', ''.join(el[0].itertext()))
    188          self.assertEquals('{http://a.b/c}HONK',
    189                            el[0][0].tag)
    190          self.assertEquals('maeh_class',
    191                            el[0][1].tag)
    192
    193          self.assertEquals('maeh_class', el[1].tag)
    194          self.assertEquals('TeXT', el[1].text)
    195          self.assertEquals('bluff_class', el[1][0].tag)
    196          self.assertEquals('{http://a.b/c}HONK', el[1][1].tag)
    197          self.assertEquals('TAiL', el[1][1].tail)
    198
    199          self.assertEquals('TeXTTAiL',
    200                            ''.join(el[1].itertext()))
    201          self.assertEquals('Tail', el[1].tail)
    202          self.assertEquals('TAiL', el[1][1].tail)
    203          self.assertEquals('bluff_class', el[1][0].tag)
    204          self.assertEquals('{http://a.b/c}HONK', el[1][1].tag)
    205
    206
    207 -def test_suite():
    208      suite = unittest.TestSuite()
    209      suite.addTests([unittest.makeSuite(ETreeNamespaceClassesTestCase)])
    210      suite.addTests(
    211          [make_doctest('../../../doc/element_classes.txt')])
    212      return suite
    213
    214  if __name__ == '__main__':
    215      print('to test use test.py %s' % __file__)
    216