Package lxml :: Package tests :: Module test_nsclasses |
[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
      9
     10  from common_imports import etree, HelperTestCase, doctest
     11
     12 -class ETreeNamespaceClassesTestCase(HelperTestCase):
     13
     14 -    class default_class(etree.ElementBase):
     15          pass
     16 -    class maeh_class(etree.ElementBase):
     17 -        def maeh(self):
     18              return u'maeh'
     19 -    class bluff_class(etree.ElementBase):
     20 -        def bluff(self):
     21              return u'bluff'
     22
     23 -    def setUp(self):
     24          super(ETreeNamespaceClassesTestCase, self).setUp()
     25          lookup = etree.ElementNamespaceClassLookup()
     26          self.Namespace = lookup.get_namespace
     27          parser = etree.XMLParser()
     28          parser.setElementClassLookup(lookup)
     29          etree.setDefaultParser(parser)
     30
     31 -    def tearDown(self):
     32          etree.setDefaultParser()
     33          del self.Namespace
     34          super(ETreeNamespaceClassesTestCase, self).tearDown()
     35
     36 -    def test_registry(self):
     37          ns = self.Namespace(u'ns01')
     38          ns[u'maeh'] = self.maeh_class
     39
     40          self.Namespace(u'ns01').clear()
     41
     42          self.Namespace(u'ns02').update({u'maeh'  : self.maeh_class})
     43          self.Namespace(u'ns03').update({u'bluff' : self.bluff_class}.items())
     44          self.Namespace(u'ns02').clear()
     45          self.Namespace(u'ns03').clear()
     46
     47 -    def test_ns_classes(self):
     48          bluff_dict = {u'bluff' : self.bluff_class}
     49          maeh_dict  = {u'maeh'  : self.maeh_class}
     50
     51          self.Namespace(u'ns10').update(bluff_dict)
     52
     53          tree = self.parse(u'<bluff xmlns="ns10"><ns11:maeh xmlns:ns11="ns11"/></bluff>')
     54
     55          el = tree.getroot()
     56          self.assert_(isinstance(el, etree.ElementBase))
     57          self.assert_(hasattr(el, 'bluff'))
     58          self.assertFalse(hasattr(el[0], 'maeh'))
     59          self.assertFalse(hasattr(el[0], 'bluff'))
     60          self.assertEquals(el.bluff(), u'bluff')
     61          del el
     62
     63          self.Namespace(u'ns11').update(maeh_dict)
     64          el = tree.getroot()
     65          self.assert_(hasattr(el, 'bluff'))
     66          self.assert_(hasattr(el[0], 'maeh'))
     67          self.assertEquals(el.bluff(), u'bluff')
     68          self.assertEquals(el[0].maeh(), u'maeh')
     69          del el
     70
     71          self.Namespace(u'ns10').clear()
     72
     73          tree = self.parse(u'<bluff xmlns="ns10"><ns11:maeh xmlns:ns11="ns11"/></bluff>')
     74          el = tree.getroot()
     75          self.assertFalse(hasattr(el, 'bluff'))
     76          self.assertFalse(hasattr(el, 'maeh'))
     77          self.assertFalse(hasattr(el[0], 'bluff'))
     78          self.assert_(hasattr(el[0], 'maeh'))
     79
     80          self.Namespace(u'ns11').clear()
     81
     82 -    def test_default_tagname(self):
     83          bluff_dict = {
     84              None   : self.bluff_class,
     85              'maeh' : self.maeh_class
     86              }
     87
     88          ns = self.Namespace("uri:nsDefClass")
     89          ns.update(bluff_dict)
     90
     91          tree = self.parse(u'''
     92              <test xmlns="bla" xmlns:ns1="uri:nsDefClass" xmlns:ns2="uri:nsDefClass">
     93                <ns2:el1/><ns1:el2/><ns1:maeh/><ns2:maeh/><maeh/>
     94              </test>
     95              ''')
     96
     97          el = tree.getroot()
     98          self.assertFalse(isinstance(el, etree.ElementBase))
     99          for child in el[:-1]:
    100              self.assert_(isinstance(child, etree.ElementBase), child.tag)
    101          self.assertFalse(isinstance(el[-1], etree.ElementBase))
    102
    103          self.assert_(hasattr(el[0], 'bluff'))
    104          self.assert_(hasattr(el[1], 'bluff'))
    105          self.assert_(hasattr(el[2], 'maeh'))
    106          self.assert_(hasattr(el[3], 'maeh'))
    107          self.assertFalse(hasattr(el[4], 'maeh'))
    108          del el
    109
    110          ns.clear()
    111
    112 -    def test_create_element(self):
    113          bluff_dict = {u'bluff' : self.bluff_class}
    114          self.Namespace(u'ns20').update(bluff_dict)
    115
    116          maeh_dict  = {u'maeh'  : self.maeh_class}
    117          self.Namespace(u'ns21').update(maeh_dict)
    118
    119          el = etree.Element("{ns20}bluff")
    120          self.assert_(hasattr(el, 'bluff'))
    121
    122          child = etree.SubElement(el, "{ns21}maeh")
    123          self.assert_(hasattr(child, 'maeh'))
    124          child = etree.SubElement(el, "{ns20}bluff")
    125          self.assert_(hasattr(child, 'bluff'))
    126          child = etree.SubElement(el, "{ns21}bluff")
    127          self.assertFalse(hasattr(child, 'bluff'))
    128          self.assertFalse(hasattr(child, 'maeh'))
    129
    130          self.assert_(hasattr(el[0], 'maeh'))
    131          self.assert_(hasattr(el[1], 'bluff'))
    132          self.assertFalse(hasattr(el[2], 'bluff'))
    133          self.assertFalse(hasattr(el[2], 'maeh'))
    134
    135          self.assertEquals(el.bluff(), u'bluff')
    136          self.assertEquals(el[0].maeh(), u'maeh')
    137          self.assertEquals(el[1].bluff(), u'bluff')
    138
    139          self.Namespace(u'ns20').clear()
    140          self.Namespace(u'ns21').clear()
    141
    142 -    def test_create_element_default(self):
    143          bluff_dict = {None : self.bluff_class}
    144          self.Namespace(u'ns30').update(bluff_dict)
    145
    146          maeh_dict  = {u'maeh'  : self.maeh_class}
    147          self.Namespace(None).update(maeh_dict)
    148
    149          el = etree.Element("{ns30}bluff")
    150          etree.SubElement(el, "maeh")
    151          self.assert_(hasattr(el, 'bluff'))
    152          self.assert_(hasattr(el[0], 'maeh'))
    153          self.assertEquals(el.bluff(), u'bluff')
    154          self.assertEquals(el[0].maeh(), u'maeh')
    155
    156          self.Namespace(None).clear()
    157          self.Namespace(u'ns30').clear()
    158
    159 -def test_suite():
    160      suite = unittest.TestSuite()
    161      suite.addTests([unittest.makeSuite(ETreeNamespaceClassesTestCase)])
    162      optionflags = doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS
    163      suite.addTests(
    164          [doctest.DocFileSuite('../../../doc/element_classes.txt',
    165                                optionflags=optionflags)],
    166          )
    167      return suite
    168
    169  if __name__ == '__main__':
    170      print 'to test use test.py %s' % __file__
    171