source: exist/branches/proglue_production_rev4605_xquery4884/python/elementtree-1.3a6-20070310-badc/unittests/test_write.py @ 4886

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/exist/branches/proglue_production_rev4605_xquery4884/python/elementtree-1.3a6-20070310-badc/unittests/test_write.py@4886
Revision 4886, 9.3 KB checked in by sdonegan, 10 years ago (diff)

Creating operational branch for ndgUtils as used on proglue

Line 
1# $Id: test_write.py 3306 2007-09-16 19:57:36Z fredrik $
2# -*- coding: iso-8859-1 -*-
3
4from support import *
5
6def writefile():
7    """
8    >>> elem = ET.Element("tag")
9    >>> elem.text = "text"
10    >>> serialize(elem)
11    '<tag>text</tag>'
12    >>> ET.SubElement(elem, "subtag").text = "subtext"
13    >>> serialize(elem)
14    '<tag>text<subtag>subtext</subtag></tag>'
15
16    Test tag suppression
17    >>> elem.tag = None
18    >>> serialize(elem)
19    'text<subtag>subtext</subtag>'
20    """
21
22def writestring():
23    """
24    >>> elem = ET.XML("<html><body>text</body></html>")
25    >>> ET.tostring(elem)
26    '<html><body>text</body></html>'
27    >>> elem = ET.fromstring("<html><body>text</body></html>")
28    >>> ET.tostring(elem)
29    '<html><body>text</body></html>'
30    """
31
32def encoding():
33    r"""
34    Test encoding issues.
35
36    >>> elem = ET.Element("tag")
37    >>> elem.text = u"abc"
38    >>> serialize(elem)
39    '<tag>abc</tag>'
40    >>> serialize(elem, encoding="utf-8")
41    '<tag>abc</tag>'
42    >>> serialize(elem, encoding="us-ascii")
43    '<tag>abc</tag>'
44    >>> serialize(elem, encoding="iso-8859-1")
45    "<?xml version='1.0' encoding='iso-8859-1'?>\n<tag>abc</tag>"
46
47    >>> elem.text = "<&\"\'>"
48    >>> serialize(elem)
49    '<tag>&lt;&amp;"\'&gt;</tag>'
50    >>> serialize(elem, encoding="utf-8")
51    '<tag>&lt;&amp;"\'&gt;</tag>'
52    >>> serialize(elem, encoding="us-ascii") # cdata characters
53    '<tag>&lt;&amp;"\'&gt;</tag>'
54    >>> serialize(elem, encoding="iso-8859-1")
55    '<?xml version=\'1.0\' encoding=\'iso-8859-1\'?>\n<tag>&lt;&amp;"\'&gt;</tag>'
56
57    >>> elem.attrib["key"] = "<&\"\'>"
58    >>> elem.text = None
59    >>> serialize(elem)
60    '<tag key="&lt;&amp;&quot;\'&gt;" />'
61    >>> serialize(elem, encoding="utf-8")
62    '<tag key="&lt;&amp;&quot;\'&gt;" />'
63    >>> serialize(elem, encoding="us-ascii")
64    '<tag key="&lt;&amp;&quot;\'&gt;" />'
65    >>> serialize(elem, encoding="iso-8859-1")
66    '<?xml version=\'1.0\' encoding=\'iso-8859-1\'?>\n<tag key="&lt;&amp;&quot;\'&gt;" />'
67
68    >>> elem.text = u'\xe5\xf6\xf6<>'
69    >>> elem.attrib.clear()
70    >>> serialize(elem)
71    '<tag>&#229;&#246;&#246;&lt;&gt;</tag>'
72    >>> serialize(elem, encoding="utf-8")
73    '<tag>\xc3\xa5\xc3\xb6\xc3\xb6&lt;&gt;</tag>'
74    >>> serialize(elem, encoding="us-ascii")
75    '<tag>&#229;&#246;&#246;&lt;&gt;</tag>'
76    >>> serialize(elem, encoding="iso-8859-1")
77    "<?xml version='1.0' encoding='iso-8859-1'?>\n<tag>\xe5\xf6\xf6&lt;&gt;</tag>"
78
79    >>> elem.attrib["key"] = u'\xe5\xf6\xf6<>'
80    >>> elem.text = None
81    >>> serialize(elem)
82    '<tag key="&#229;&#246;&#246;&lt;&gt;" />'
83    >>> serialize(elem, encoding="utf-8")
84    '<tag key="\xc3\xa5\xc3\xb6\xc3\xb6&lt;&gt;" />'
85    >>> serialize(elem, encoding="us-ascii")
86    '<tag key="&#229;&#246;&#246;&lt;&gt;" />'
87    >>> serialize(elem, encoding="iso-8859-1")
88    '<?xml version=\'1.0\' encoding=\'iso-8859-1\'?>\n<tag key="\xe5\xf6\xf6&lt;&gt;" />'
89
90    """
91
92def methods():
93    r"""
94    Test serialization methods.
95
96    >>> e = ET.XML("<html><link/><script>1 &lt; 2</script></html>")
97    >>> e.tail = "\n"
98
99    >>> serialize(e)
100    '<html><link /><script>1 &lt; 2</script></html>\n'
101    >>> serialize(e, method=None)
102    '<html><link /><script>1 &lt; 2</script></html>\n'
103    >>> serialize(e, method="xml")
104    '<html><link /><script>1 &lt; 2</script></html>\n'
105    >>> serialize(e, method="xml", encoding="utf-8")
106    '<html><link /><script>1 &lt; 2</script></html>\n'
107    >>> serialize(e, method="xml", encoding="us-ascii")
108    '<html><link /><script>1 &lt; 2</script></html>\n'
109    >>> serialize(e, method="xml", encoding="iso-8859-1")
110    "<?xml version='1.0' encoding='iso-8859-1'?>\n<html><link /><script>1 &lt; 2</script></html>\n"
111
112    >>> serialize(e, method="html")
113    '<html><link><script>1 < 2</script></html>\n'
114    >>> serialize(e, method="html", encoding="iso-8859-1")
115    '<html><link><script>1 < 2</script></html>\n'
116
117    >>> serialize(e, method="text")
118    '1 < 2\n'
119    >>> serialize(e, method="text", encoding="iso-8859-1")
120    '1 < 2\n'
121
122    """
123
124def namespace():
125    """
126    Test namespace issues.
127
128    1) xml namespace
129
130    >>> elem = ET.XML("<tag xml:lang='en' />")
131    >>> serialize(elem) # 1.1
132    '<tag xml:lang="en" />'
133
134    2) other "well-known" namespaces
135
136    >>> elem = ET.XML("<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#' />")
137    >>> serialize(elem) # 2.1
138    '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" />'
139
140    >>> elem = ET.XML("<html:html xmlns:html='http://www.w3.org/1999/xhtml' />")
141    >>> serialize(elem) # 2.2
142    '<html:html xmlns:html="http://www.w3.org/1999/xhtml" />'
143
144    >>> elem = ET.XML("<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope' />")
145    >>> serialize(elem) # 2.3
146    '<ns0:Envelope xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope" />'
147
148    3) unknown namespaces
149
150    """
151
152def qname():
153    """
154    Test QName handling.
155
156    1) decorated tags
157
158    >>> elem = ET.Element("{uri}tag")
159    >>> serialize(elem) # 1.1
160    '<ns0:tag xmlns:ns0="uri" />'
161    >>> elem = ET.Element(ET.QName("{uri}tag"))
162    >>> serialize(elem) # 1.2
163    '<ns0:tag xmlns:ns0="uri" />'
164    >>> elem = ET.Element(ET.QName("uri", "tag"))
165    >>> serialize(elem) # 1.3
166    '<ns0:tag xmlns:ns0="uri" />'
167
168    2) decorated attributes
169
170    >>> elem.clear()
171    >>> elem.attrib["{uri}key"] = "value"
172    >>> serialize(elem) # 2.1
173    '<ns0:tag xmlns:ns0="uri" ns0:key="value" />'
174
175    >>> elem.clear()
176    >>> elem.attrib[ET.QName("{uri}key")] = "value"
177    >>> serialize(elem) # 2.2
178    '<ns0:tag xmlns:ns0="uri" ns0:key="value" />'
179
180    3) decorated values are not converted by default, but the
181       QName wrapper can be used for values
182
183    >>> elem.clear()
184    >>> elem.attrib["{uri}key"] = "{uri}value"
185    >>> serialize(elem) # 3.1
186    '<ns0:tag xmlns:ns0="uri" ns0:key="{uri}value" />'
187
188    >>> elem.clear()
189    >>> elem.attrib["{uri}key"] = ET.QName("{uri}value")
190    >>> serialize(elem) # 3.2
191    '<ns0:tag xmlns:ns0="uri" ns0:key="ns0:value" />'
192
193    >>> elem.clear()
194    >>> subelem = ET.Element("tag")
195    >>> subelem.attrib["{uri1}key"] = ET.QName("{uri2}value")
196    >>> elem.append(subelem)
197    >>> elem.append(subelem)
198    >>> serialize(elem) # 3.3
199    '<ns0:tag xmlns:ns0="uri" xmlns:ns1="uri1" xmlns:ns2="uri2"><tag ns1:key="ns2:value" /><tag ns1:key="ns2:value" /></ns0:tag>'
200
201    """
202
203# --------------------------------------------------------------------
204
205def bug_xmltoolkit21():
206    """
207    marshaller gives obscure errors for non-string values
208
209    >>> elem = ET.Element(123)
210    >>> serialize(elem) # tag
211    Traceback (most recent call last):
212    TypeError: cannot serialize 123 (type int)
213    >>> elem = ET.Element("elem")
214    >>> elem.text = 123
215    >>> serialize(elem) # text
216    Traceback (most recent call last):
217    TypeError: cannot serialize 123 (type int)
218    >>> elem = ET.Element("elem")
219    >>> elem.tail = 123
220    >>> serialize(elem) # tail
221    Traceback (most recent call last):
222    TypeError: cannot serialize 123 (type int)
223    >>> elem = ET.Element("elem")
224    >>> elem.set(123, "123")
225    >>> serialize(elem) # attribute key
226    Traceback (most recent call last):
227    TypeError: cannot serialize 123 (type int)
228    >>> elem = ET.Element("elem")
229    >>> elem.set("123", 123)
230    >>> serialize(elem) # attribute value
231    Traceback (most recent call last):
232    TypeError: cannot serialize 123 (type int)
233
234    """
235
236def bug_xmltoolkit39():
237    """
238    non-ascii element and attribute names doesn't work
239
240    >>> tree = ET.XML("<?xml version='1.0' encoding='iso-8859-1'?><täg />")
241    >>> ET.tostring(tree, "utf-8")
242    '<t\\xc3\\xa4g />'
243
244    >>> tree = ET.XML("<?xml version='1.0' encoding='iso-8859-1'?><tag ättr='v&#228;lue' />")
245    >>> tree.attrib
246    {u'\\xe4ttr': u'v\\xe4lue'}
247    >>> ET.tostring(tree, "utf-8")
248    '<tag \\xc3\\xa4ttr="v\\xc3\\xa4lue" />'
249
250    >>> tree = ET.XML("<?xml version='1.0' encoding='iso-8859-1'?><täg>text</täg>")
251    >>> ET.tostring(tree, "utf-8")
252    '<t\\xc3\\xa4g>text</t\\xc3\\xa4g>'
253
254    >>> tree = ET.Element(u"täg")
255    >>> ET.tostring(tree, "utf-8")
256    '<t\\xc3\\xa4g />'
257
258    >>> tree = ET.Element("tag")
259    >>> tree.set(u"ättr", u"välue")
260    >>> ET.tostring(tree, "utf-8")
261    '<tag \\xc3\\xa4ttr="v\\xc3\\xa4lue" />'
262
263    """
264
265def bug_200708_newline():
266    r"""
267
268    Preserve newlines in attributes.
269
270    >>> e = ET.Element('SomeTag', text="def _f():\n  return 3\n")
271    >>> ET.tostring(e)
272    '<SomeTag text="def _f():&#10;  return 3&#10;" />'
273    >>> ET.XML(ET.tostring(e)).get("text")
274    'def _f():\n  return 3\n'
275    >>> ET.tostring(ET.XML(ET.tostring(e)))
276    '<SomeTag text="def _f():&#10;  return 3&#10;" />'
277    """
278
279def bug_200709_default_namespace():
280    """
281
282    >>> e = ET.Element("{default}elem")
283    >>> s = ET.SubElement(e, "{default}elem")
284    >>> serialize(e, default_namespace="default") # 1
285    '<elem xmlns="default"><elem /></elem>'
286
287    >>> e = ET.Element("{default}elem")
288    >>> s = ET.SubElement(e, "{default}elem")
289    >>> s = ET.SubElement(e, "{not-default}elem")
290    >>> serialize(e, default_namespace="default") # 2
291    '<elem xmlns="default" xmlns:ns1="not-default"><elem /><ns1:elem /></elem>'
292
293    >>> e = ET.Element("{default}elem")
294    >>> s = ET.SubElement(e, "{default}elem")
295    >>> s = ET.SubElement(e, "elem") # unprefixed name
296    >>> serialize(e, default_namespace="default") # 3
297    Traceback (most recent call last):
298    ValueError: cannot use non-qualified names with default_namespace option
299
300    """
Note: See TracBrowser for help on using the repository browser.