source: exist/trunk/python/elementtree-1.3a6-20070212-badc/unittests/test_c14n.py @ 3429

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/exist/trunk/python/elementtree-1.3a6-20070212-badc/unittests/test_c14n.py@4663
Revision 3429, 2.6 KB checked in by pjkersha, 12 years ago (diff)

Added latest ElementTree version (12/2/08) from Fredrik Lundh

Line 
1# $Id: test_c14n.py 3374 2008-02-13 08:04:46Z fredrik $
2
3from StringIO import StringIO
4
5from support import *
6from elementtree import ElementTree, ElementC14N
7
8def roundtrip(xml):
9    tree = ElementC14N.parse(StringIO(xml))
10    f = StringIO()
11    ElementC14N.write(tree, f)
12    return f.getvalue()
13
14#
15# simple roundtrip tests (from c14n.py)
16
17def simple_roundtrip(xml):
18    r"""
19    >>> roundtrip("<doc/>")
20    '<doc></doc>'
21    >>> roundtrip("<doc xmlns='uri'/>") # FIXME
22    '<doc></doc>'
23    >>> roundtrip("<prefix:doc xmlns:prefix='uri'/>")
24    '<prefix:doc xmlns:prefix="uri"></prefix:doc>'
25    >>> roundtrip("<doc xmlns:prefix='uri'><prefix:bar/></doc>")
26    '<doc xmlns:prefix="uri"><prefix:bar></prefix:bar></doc>'
27    >>> roundtrip("<doc>Hello, world!<!-- Comment 1 --></doc>")
28    '<doc>Hello, world!</doc>'
29    >>> roundtrip("<value>&#x32;</value>")
30    '<value>2</value>'
31    >>> roundtrip('<compute><![CDATA[value>"0" && value<"10" ?"valid":"error"]]></compute>')
32    '<compute>value&gt;"0" &amp;&amp; value&lt;"10" ?"valid":"error"</compute>'
33    >>> roundtrip('''<compute expr='value>"0" &amp;&amp; value&lt;"10" ?"valid":"error"'>valid</compute>''')
34    '<compute expr="value>&quot;0&quot; &amp;&amp; value&lt;&quot;10&quot; ?&quot;valid&quot;:&quot;error&quot;">valid</compute>'
35    >>> roundtrip("<norm attr=' &apos;   &#x20;&#13;&#xa;&#9;   &apos; '/>")
36    '<norm attr=" \'    &#xD;&#xA;&#x9;   \' "></norm>'
37    >>> roundtrip("<normNames attr='   A   &#x20;&#13;&#xa;&#9;   B   '/>")
38    '<normNames attr="   A    &#xD;&#xA;&#x9;   B   "></normNames>'
39    >>> roundtrip("<normId id=' &apos;   &#x20;&#13;&#xa;&#9;   &apos; '/>")
40    '<normId id=" \'    &#xD;&#xA;&#x9;   \' "></normId>'
41    """
42
43#
44# basic method=c14n tests, mainly from the c14n specification.  uses
45# test files under unittests/c14n.
46
47# note that this uses generates C14N versions of the standard ET.write
48# output, not roundtripped C14N (see above).
49
50def xml_c14n(name):
51    """
52    >>> xml_c14n("simple-1")
53    7
54    >>> xml_c14n("simple-2")
55    14
56    >>> xml_c14n("xml-c14n-1")
57    24
58    >>> xml_c14n("xml-c14n-2")
59    169
60    >>> xml_c14n("xml-c14n-3")
61    381
62    >>> # doesn't work under doctest, due to whitespace issues
63    >>> # xml_c14n("xml-c14n-4") # FIXME!
64    >>> xml_c14n("xml-c14n-5")
65    13
66    """
67    name = os.path.join("unittests", "c14n", name)
68    elem = ElementTree.parse(open(name + ".xml")).getroot()
69    text = serialize(elem, method="c14n")
70    out = open(name + ".out", "r").read()
71    if repr(text) == out.strip():
72        return len(text)
73
Note: See TracBrowser for help on using the repository browser.