| 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
13 except NameError:
14 unicode = str
15
16 ascii_uni = _bytes('a').decode('utf8')
17
18 klingon = _bytes("\\uF8D2").decode("unicode_escape") # not valid for XML names
19
20 invalid_tag = _bytes("test").decode('utf8') + 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 if sys.maxunicode < 1114111:
34 return # skip test
35 tree = etree.XML(_bytes('<p>\\U00026007</p>').decode('unicode_escape'))
36 self.assertEqual(1, len(tree.text))
37 self.assertEqual(_bytes('\\U00026007').decode('unicode_escape'),
38 tree.text)
39
41 uxml = ('<?xml version="1.0" encoding="UTF-8"?>' +
42 '<p>%s</p>' % uni)
43 self.assertRaises(ValueError, etree.XML, uxml)
44
48
52
57
59 # namespace URIs must conform to RFC 3986
60 tag = "{http://%s/}abc" % uni
61 self.assertRaises(ValueError, etree.Element, tag)
62
64 # sadly, Klingon is not well-formed
65 tag = "{http://abc/}%s" % invalid_tag
66 self.assertRaises(ValueError, etree.Element, tag)
67
69 qname = etree.QName(uni, uni)
70 tag = "{%s}%s" % (uni, uni)
71 self.assertEqual(qname.text, tag)
72 self.assertEqual(unicode(qname), tag)
73
76
80
84
88
89 ## def test_parse_fileobject_unicode(self):
90 ## # parse unicode from unamed file object (not support by ElementTree)
91 ## f = SillyFileLike(uxml)
92 ## root = etree.parse(f).getroot()
93 ## self.assertEqual(unicode(etree.tostring(root, 'UTF-8'), 'UTF-8'),
94 ## uxml)
95
97 suite = unittest.TestSuite()
98 suite.addTests([unittest.makeSuite(UnicodeTestCase)])
99 return suite
100
| Home | Trees | Indices | Help |
|
|---|
| Generated by Epydoc 3.0.1 on Tue Mar 4 19:19:02 2014 | http://epydoc.sourceforge.net |