lxml.tests.test_elementpath

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

# Source Code for Module lxml.tests.test_elementpath

     1  # -*- coding: utf-8 -*-
     2
     3  """
     4  Tests for the ElementPath implementation.
     5  """
     6
     7  from __future__ import absolute_import
     8
     9  import unittest
    10  from .common_imports import etree, HelperTestCase
    11
    12
    13 -class EtreeElementPathTestCase(HelperTestCase):
    14      etree = etree
    15      from lxml import _elementpath
    16
    17 -    def test_cache(self):
    18          self._elementpath._cache.clear()
    19          el = self.etree.XML(b'<a><b><c/><c/></b></a>')
    20          self.assertFalse(self._elementpath._cache)
    21
    22          self.assertTrue(el.findall('b/c'))
    23          self.assertEqual(1, len(self._elementpath._cache))
    24          self.assertTrue(el.findall('b/c'))
    25          self.assertEqual(1, len(self._elementpath._cache))
    26          self.assertFalse(el.findall('xxx'))
    27          self.assertEqual(2, len(self._elementpath._cache))
    28          self.assertFalse(el.findall('xxx'))
    29          self.assertEqual(2, len(self._elementpath._cache))
    30          self.assertTrue(el.findall('b/c'))
    31          self.assertEqual(2, len(self._elementpath._cache))
    32
    33 -    def test_tokenizer(self):
    34          def assert_tokens(tokens, path, namespaces=None):
    35              self.assertEqual(tokens, list(self._elementpath.xpath_tokenizer(path, namespaces)))
    36
    37          assert_tokens(
    38              [('/', '')],
    39              '/',
    40          )
    41          assert_tokens(
    42              [('.', ''), ('/', ''), ('', 'a'), ('/', ''), ('', 'b'), ('/', ''), ('', 'c')],
    43              './a/b/c',
    44          )
    45          assert_tokens(
    46              [('/', ''), ('', 'a'), ('/', ''), ('', 'b'), ('/', ''), ('', 'c')],
    47              '/a/b/c',
    48          )
    49          assert_tokens(
    50              [('/', ''), ('', '{nsx}a'), ('/', ''), ('', '{nsy}b'), ('/', ''), ('', 'c')],
    51              '/x:a/y:b/c',
    52              {'x': 'nsx', 'y': 'nsy'},
    53          )
    54          assert_tokens(
    55              [('/', ''), ('', '{nsx}a'), ('/', ''), ('', '{nsy}b'), ('/', ''), ('', '{nsnone}c')],
    56              '/x:a/y:b/c',
    57              {'x': 'nsx', 'y': 'nsy', None: 'nsnone'},
    58          )
    59
    60
    61  #class ElementTreeElementPathTestCase(EtreeElementPathTestCase):
    62  #    import xml.etree.ElementTree as etree
    63  #    import xml.etree.ElementPath as _elementpath
    64
    65
    66 -def test_suite():
    67      suite = unittest.TestSuite()
    68      suite.addTests([unittest.makeSuite(EtreeElementPathTestCase)])
    69      #suite.addTests([unittest.makeSuite(ElementTreeElementPathTestCase)])
    70      return suite
    71
    72
    73  if __name__ == '__main__':
    74      print('to test use test.py %s' % __file__)
    75