| Home | Trees | Indices | Help |
|
|---|
|
|
1 # -*- coding: utf-8 -*-
2 import unittest
3 import sys
4 import os.path
5
6 this_dir = os.path.dirname(__file__)
7 if this_dir not in sys.path:
8 sys.path.insert(0, this_dir) # needed for Py3
9
10 from common_imports import StringIO, etree, SillyFileLike, HelperTestCase
11 from common_imports import _str, _bytes
12
13 try:
14 unicode
15 except NameError:
16 unicode = str
17
18 ascii_uni = _bytes('a').decode('utf8')
19
20 klingon = _bytes("\\uF8D2").decode("unicode_escape") # not valid for XML names
21
22 invalid_tag = _bytes("test").decode('utf8') + klingon
23
24 uni = _bytes('\\xc3\\u0680\\u3120').decode("unicode_escape") # some non-ASCII characters
25
26 uxml = _bytes("<test><title>test \\xc3\\xa1\\u3120</title><h1>page \\xc3\\xa1\\u3120 title</h1></test>"
27 ).decode("unicode_escape")
28
29
34
36 if sys.maxunicode < 1114111:
37 return # skip test
38 tree = etree.XML(_bytes('<p>\\U00026007</p>').decode('unicode_escape'))
39 self.assertEqual(1, len(tree.text))
40 self.assertEqual(_bytes('\\U00026007').decode('unicode_escape'),
41 tree.text)
42
44 uxml = ('<?xml version="1.0" encoding="UTF-8"?>' +
45 '<p>%s</p>' % uni)
46 self.assertRaises(ValueError, etree.XML, uxml)
47
51
55
60
62 # namespace URIs must conform to RFC 3986
63 tag = "{http://%s/}abc" % uni
64 self.assertRaises(ValueError, etree.Element, tag)
65
67 # sadly, Klingon is not well-formed
68 tag = "{http://abc/}%s" % invalid_tag
69 self.assertRaises(ValueError, etree.Element, tag)
70
72 qname = etree.QName(uni, uni)
73 tag = "{%s}%s" % (uni, uni)
74 self.assertEqual(qname.text, tag)
75 self.assertEqual(unicode(qname), tag)
76
79
83
87
91
92 ## def test_parse_fileobject_unicode(self):
93 ## # parse unicode from unamed file object (not support by ElementTree)
94 ## f = SillyFileLike(uxml)
95 ## root = etree.parse(f).getroot()
96 ## self.assertEqual(unicode(etree.tostring(root, 'UTF-8'), 'UTF-8'),
97 ## uxml)
98
99
102 data = _bytes('<test>\x80\x80\x80</test>', encoding='iso8859-1')
103 self.assertRaises(etree.XMLSyntaxError, etree.fromstring, data)
104
109
110
112 suite = unittest.TestSuite()
113 suite.addTests([unittest.makeSuite(UnicodeTestCase)])
114 suite.addTests([unittest.makeSuite(EncodingsTestCase)])
115 return suite
116
| Home | Trees | Indices | Help |
|
|---|
| Generated by Epydoc 3.0.1 on Sat Apr 25 16:24:40 2015 | http://epydoc.sourceforge.net |