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

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

Latest releases from Fredrik Lundh. 10 March release has exclusive C14N support with namespace prefixes.

Line 
1# $Id: test_c14n.py 3392 2008-03-10 23:32:45Z fredrik $
2
3from StringIO import StringIO
4
5from support import *
6from elementtree import ElementTree, ElementC14N
7
8def roundtrip(xml, subset=None, **options):
9    tree = ElementC14N.parse(StringIO(xml))
10    if subset:
11        subset = tree.find(subset)
12    f = StringIO()
13    ElementC14N.write(tree, f, subset, **options)
14    return f.getvalue()
15
16def parents(xml):
17    r"""
18    Broken parent structure.
19    >>> parents("<e0/>")
20    >>> parents("<e0><e1><e2/></e1></e0>")
21    >>> parents("<e0><e1/><e2><e3/></e2></e0>")
22    """
23    tree = ElementC14N.parse(StringIO(xml))
24    p1 = tree._parent
25    p2 = dict([(c, p) for p in tree.getiterator() for c in p])
26    for elem in tree.getiterator():
27        if p1.get(elem) != p2.get(elem):
28            print "%s's parent is %s, expected %s" % (
29                elem.tag, p1[elem].tag, p2[elem].tag
30                )
31
32#
33# simple roundtrip tests (from c14n.py)
34
35def simple_roundtrip(xml):
36    r"""
37
38    Basics
39    >>> roundtrip("<doc/>")
40    '<doc></doc>'
41    >>> roundtrip("<doc xmlns='uri'/>") # FIXME
42    '<doc></doc>'
43    >>> roundtrip("<prefix:doc xmlns:prefix='uri'/>")
44    '<prefix:doc xmlns:prefix="uri"></prefix:doc>'
45    >>> roundtrip("<doc xmlns:prefix='uri'><prefix:bar/></doc>")
46    '<doc xmlns:prefix="uri"><prefix:bar></prefix:bar></doc>'
47    >>> roundtrip("<elem xmlns:wsu='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd' xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' />")
48    '<elem xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"></elem>'
49
50    C14N spec
51    >>> roundtrip("<doc>Hello, world!<!-- Comment 1 --></doc>")
52    '<doc>Hello, world!</doc>'
53    >>> roundtrip("<value>&#x32;</value>")
54    '<value>2</value>'
55    >>> roundtrip('<compute><![CDATA[value>"0" && value<"10" ?"valid":"error"]]></compute>')
56    '<compute>value&gt;"0" &amp;&amp; value&lt;"10" ?"valid":"error"</compute>'
57    >>> roundtrip('''<compute expr='value>"0" &amp;&amp; value&lt;"10" ?"valid":"error"'>valid</compute>''')
58    '<compute expr="value>&quot;0&quot; &amp;&amp; value&lt;&quot;10&quot; ?&quot;valid&quot;:&quot;error&quot;">valid</compute>'
59    >>> roundtrip("<norm attr=' &apos;   &#x20;&#13;&#xa;&#9;   &apos; '/>")
60    '<norm attr=" \'    &#xD;&#xA;&#x9;   \' "></norm>'
61    >>> roundtrip("<normNames attr='   A   &#x20;&#13;&#xa;&#9;   B   '/>")
62    '<normNames attr="   A    &#xD;&#xA;&#x9;   B   "></normNames>'
63    >>> roundtrip("<normId id=' &apos;   &#x20;&#13;&#xa;&#9;   &apos; '/>")
64    '<normId id=" \'    &#xD;&#xA;&#x9;   \' "></normId>'
65
66    fragments from PJ's tests
67    >>> roundtrip("<doc xmlns:x='http://example.com/x' xmlns='http://example.com/default'><b y:a1='1' xmlns='http://example.com/default' a3='3' xmlns:y='http://example.com/y' y:a2='2'/></doc>")
68    '<doc xmlns:x="http://example.com/x"><b xmlns:y="http://example.com/y" a3="3" y:a1="1" y:a2="2"></b></doc>'
69
70    """
71
72def exclusive_roundtrip(elem):
73    """
74    >>> XML = "<root xmlns:ns='NS' xmlns:sn='SN'><ns:subset><ns:elem/></ns:subset></root>"
75    >>> roundtrip(XML) # <-- broken?
76    '<root xmlns:ns="NS" xmlns:sn="SN"><ns:subset><ns:elem></ns:elem></ns:subset></root>'
77    >>> roundtrip(XML, ".", exclusive=True)
78    '<root><ns:subset xmlns:ns="NS"><ns:elem></ns:elem></ns:subset></root>'
79    >>> roundtrip(XML, ".", exclusive=True, inclusive_namespaces=["sn"])
80    '<root xmlns:sn="SN"><ns:subset xmlns:ns="NS"><ns:elem></ns:elem></ns:subset></root>'
81    >>> roundtrip(XML, "{NS}subset", exclusive=True)
82    '<ns:subset xmlns:ns="NS"><ns:elem></ns:elem></ns:subset>'
83    >>> roundtrip(XML, "{NS}subset/{NS}elem", exclusive=True)
84    '<ns:elem xmlns:ns="NS"></ns:elem>'
85    >>> XML = "<root xmlns:ns='NS'><ns:subset xmlns:ns='SN'><ns:elem/></ns:subset></root>"
86    >>> roundtrip(XML) # <-- broken?
87    '<root xmlns:ns="NS"><ns:subset xmlns:ns="SN"><ns:elem></ns:elem></ns:subset></root>'
88    >>> roundtrip(XML, ".", exclusive=True)
89    '<root><ns:subset xmlns:ns="SN"><ns:elem></ns:elem></ns:subset></root>'
90    """
91
92#
93# basic method=c14n tests, mainly from the c14n specification.  uses
94# test files under unittests/c14n.
95
96# note that this uses generates C14N versions of the standard ET.write
97# output, not roundtripped C14N (see above).
98
99def xml_c14n(name):
100    """
101    >>> xml_c14n("simple-1")
102    7
103    >>> xml_c14n("simple-2")
104    14
105    >>> xml_c14n("xml-c14n-1")
106    24
107    >>> xml_c14n("xml-c14n-2")
108    169
109    >>> xml_c14n("xml-c14n-3")
110    381
111    >>> # doesn't work under doctest, due to whitespace issues
112    >>> # xml_c14n("xml-c14n-4") # FIXME!
113    >>> xml_c14n("xml-c14n-5")
114    13
115    """
116    name = os.path.join("unittests", "c14n", name)
117    elem = ElementTree.parse(open(name + ".xml")).getroot()
118    text = serialize(elem, method="c14n")
119    out = open(name + ".out", "r").read()
120    if repr(text) == out.strip():
121        return len(text)
Note: See TracBrowser for help on using the repository browser.