Package lxml :: Package tests :: Module test_unicode

[frames] | no frames]

# Source Code for Module lxml.tests.test_unicode

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