| Home | Trees | Indices | Help |
|
|---|
|
|
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
31
33 uxml = _str('<?xml version="1.0" encoding="UTF-8"?>') + \
34 _str('<p>%s</p>') % uni
35 self.assertRaises(ValueError, etree.XML, uxml)
36
40
44
49
51 # namespace URIs must conform to RFC 3986
52 tag = _str("{http://%s/}abc") % uni
53 self.assertRaises(ValueError, etree.Element, tag)
54
56 # sadly, Klingon is not well-formed
57 tag = _str("{http://abc/}%s") % invalid_tag
58 self.assertRaises(ValueError, etree.Element, tag)
59
61 qname = etree.QName(uni, uni)
62 tag = _str("{%s}%s") % (uni, uni)
63 self.assertEquals(qname.text, tag)
64 self.assertEquals(unicode(qname), tag)
65
68
72
76
80
81 ## def test_parse_fileobject_unicode(self):
82 ## # parse unicode from unamed file object (not support by ElementTree)
83 ## f = SillyFileLike(uxml)
84 ## root = etree.parse(f).getroot()
85 ## self.assertEquals(unicode(etree.tostring(root, 'UTF-8'), 'UTF-8'),
86 ## uxml)
87
89 suite = unittest.TestSuite()
90 suite.addTests([unittest.makeSuite(UnicodeTestCase)])
91 return suite
92
| Home | Trees | Indices | Help |
|
|---|
| Generated by Epydoc 3.0.1 on Fri Nov 11 18:38:29 2011 | http://epydoc.sourceforge.net |