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 
 3   
 4  from common_imports import StringIO, etree, SillyFileLike 
 5   
 6  ascii_uni = u'a' 
 7   
 8  uni = u'á\uF8D2' # klingon etc. 
 9   
10  uxml = u"<test><title>test á\uF8D2</title><h1>page á\uF8D2 title</h1></test>" 
11   
12 -class UnicodeTestCase(unittest.TestCase):
13 - def test_unicode_xml(self):
14 tree = etree.XML(u'<p>%s</p>' % uni) 15 self.assertEquals(uni, tree.text)
16
17 - def test_unicode_xml_broken(self):
18 uxml = u'<?xml version="1.0" encoding="UTF-8"?>' + \ 19 u'<p>%s</p>' % uni 20 self.assertRaises(ValueError, etree.XML, uxml)
21
22 - def test_unicode_tag(self):
23 el = etree.Element(uni) 24 self.assertEquals(uni, el.tag)
25
26 - def test_unicode_nstag(self):
27 tag = u"{%s}%s" % (uni, uni) 28 el = etree.Element(tag) 29 self.assertEquals(tag, el.tag)
30
31 - def test_unicode_qname(self):
32 qname = etree.QName(uni, uni) 33 tag = u"{%s}%s" % (uni, uni) 34 self.assertEquals(qname.text, tag) 35 self.assertEquals(unicode(qname), tag)
36
37 - def test_unicode_attr(self):
38 el = etree.Element('foo', {'bar': uni}) 39 self.assertEquals(uni, el.attrib['bar'])
40
41 - def test_unicode_comment(self):
42 el = etree.Comment(uni) 43 self.assertEquals(uni, el.text)
44
46 el = etree.parse(StringIO(u'<p>%s</p>' % uni)).getroot() 47 self.assertEquals(uni, el.text)
48 49 ## def test_parse_fileobject_unicode(self): 50 ## # parse unicode from unamed file object (not support by ElementTree) 51 ## f = SillyFileLike(uxml) 52 ## root = etree.parse(f).getroot() 53 ## self.assertEquals(unicode(etree.tostring(root, 'UTF-8'), 'UTF-8'), 54 ## uxml) 55
56 -def test_suite():
57 suite = unittest.TestSuite() 58 suite.addTests([unittest.makeSuite(UnicodeTestCase)]) 59 return suite
60