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

# Source Code for Module lxml.tests.test_unicode

     1  # -*- coding: utf-8 -*-
     2  import unittest, doctest, sys, os.path
     3
     4  this_dir = os.path.dirname(__file__)
     5  if this_dir not in sys.path:
     6      sys.path.insert(0, this_dir) # needed for Py3
     7
     8  from common_imports import StringIO, etree, SillyFileLike, HelperTestCase
     9  from common_imports import _str, _bytes
    10
    11  try:
    12      unicode = __builtins__["unicode"]
    13  except (NameError, KeyError):
    14      unicode = str
    15
    16  ascii_uni = _str('a')
    17
    18  klingon = _bytes("\\uF8D2").decode("unicode_escape") # not valid for XML names
    19
    20  invalid_tag = _str("test") + klingon
    21
    22  uni = _bytes('\\xc3\\u0680\\u3120').decode("unicode_escape") # some non-ASCII characters
    23
    24  uxml = _bytes("<test><title>test \\xc3\\xa1\\u3120</title><h1>page \\xc3\\xa1\\u3120 title</h1></test>"
    25                ).decode("unicode_escape")
    26
    27 -class UnicodeTestCase(HelperTestCase):
    28 -    def test_unicode_xml(self):
    29          tree = etree.XML(_str('<p>%s</p>') % uni)
    30          self.assertEquals(uni, tree.text)
    31
    32 -    def test_unicode_xml_broken(self):
    33          uxml = _str('<?xml version="1.0" encoding="UTF-8"?>') + \
    34                 _str('<p>%s</p>') % uni
    35          self.assertRaises(ValueError, etree.XML, uxml)
    36
    37 -    def test_unicode_tag(self):
    38          el = etree.Element(uni)
    39          self.assertEquals(uni, el.tag)
    40
    41 -    def test_unicode_tag_invalid(self):
    42          # sadly, Klingon is not well-formed
    43          self.assertRaises(ValueError, etree.Element, invalid_tag)
    44
    45 -    def test_unicode_nstag(self):
    46          tag = _str("{%s}%s") % (uni, uni)
    47          el = etree.Element(tag)
    48          self.assertEquals(tag, el.tag)
    49
    50 -    def test_unicode_nstag_invalid(self):
    51          # sadly, Klingon is not well-formed
    52          tag = _str("{%s}%s") % (uni, invalid_tag)
    53          self.assertRaises(ValueError, etree.Element, tag)
    54
    55 -    def test_unicode_qname(self):
    56          qname = etree.QName(uni, uni)
    57          tag = _str("{%s}%s") % (uni, uni)
    58          self.assertEquals(qname.text, tag)
    59          self.assertEquals(unicode(qname), tag)
    60
    61 -    def test_unicode_qname_invalid(self):
    62          self.assertRaises(ValueError, etree.QName, invalid_tag)
    63
    64 -    def test_unicode_attr(self):
    65          el = etree.Element('foo', {'bar': uni})
    66          self.assertEquals(uni, el.attrib['bar'])
    67
    68 -    def test_unicode_comment(self):
    69          el = etree.Comment(uni)
    70          self.assertEquals(uni, el.text)
    71
    72 -    def test_unicode_parse_stringio(self):
    73          el = etree.parse(StringIO(_str('<p>%s</p>') % uni)).getroot()
    74          self.assertEquals(uni, el.text)
    75
    76  ##     def test_parse_fileobject_unicode(self):
    77  ##         # parse unicode from unamed file object (not support by ElementTree)
    78  ##         f = SillyFileLike(uxml)
    79  ##         root = etree.parse(f).getroot()
    80  ##         self.assertEquals(unicode(etree.tostring(root, 'UTF-8'), 'UTF-8'),
    81  ##                           uxml)
    82
    83 -def test_suite():
    84      suite = unittest.TestSuite()
    85      suite.addTests([unittest.makeSuite(UnicodeTestCase)])
    86      return suite
    87