1 | #! /usr/bin/env/python |
---|
2 | # -*- coding: iso-8859-1 -*- |
---|
3 | |
---|
4 | ## Copyright 1999-2005 by LivingLogic AG, Bayreuth/Germany. |
---|
5 | ## Copyright 1999-2005 by Walter Dörwald |
---|
6 | ## |
---|
7 | ## All Rights Reserved |
---|
8 | ## |
---|
9 | ## See xist/__init__.py for the license |
---|
10 | |
---|
11 | |
---|
12 | import py.test |
---|
13 | |
---|
14 | from ll.xist import xsc |
---|
15 | from ll.xist.ns import html, xml, chars, abbr, ihtml, wml, specials, htmlspecials, form, meta, svg, fo, docbook, jsp, struts_html, struts_config, tld |
---|
16 | |
---|
17 | |
---|
18 | def test_mixedattrnames(): |
---|
19 | class __ns__(xsc.Namespace): |
---|
20 | xmlname = "test" |
---|
21 | xmlurl = "test" |
---|
22 | |
---|
23 | class Attrs(xsc.Namespace.Attrs): |
---|
24 | class a(xsc.TextAttr): xmlname = "A" |
---|
25 | class A(xsc.TextAttr): xmlname = "a" |
---|
26 | class Test(xsc.Element): |
---|
27 | class Attrs(xsc.Element.Attrs): |
---|
28 | class a(xsc.TextAttr): xmlname = "A" |
---|
29 | class A(xsc.TextAttr): xmlname = "a" |
---|
30 | |
---|
31 | node = __ns__.Test( |
---|
32 | { |
---|
33 | (__ns__, "a"): "a2", |
---|
34 | (__ns__, "A"): "A2", |
---|
35 | }, |
---|
36 | a="a", |
---|
37 | A="A" |
---|
38 | ) |
---|
39 | |
---|
40 | def check(name, value): |
---|
41 | assert unicode(node[name]) == value |
---|
42 | assert unicode(node.attrs[name]) == value |
---|
43 | if not isinstance(name, tuple): |
---|
44 | assert unicode(getattr(node.attrs, name)) == value |
---|
45 | assert unicode(node.attrs.get(name, xml=False)) == value |
---|
46 | if isinstance(name, tuple): |
---|
47 | name = (name[0], name[1].swapcase()) |
---|
48 | else: |
---|
49 | name = name.swapcase() |
---|
50 | assert unicode(node.attrs.get(name, xml=True)) == value |
---|
51 | |
---|
52 | tests = [ |
---|
53 | ("a", "a"), |
---|
54 | ("A", "A"), |
---|
55 | ((__ns__, "a"), "a2"), |
---|
56 | ((__ns__, "A"), "A2") |
---|
57 | ] |
---|
58 | for (name, value) in tests: |
---|
59 | yield check, name, value |
---|
60 | |
---|
61 | |
---|
62 | def test_variousnamespaces(): |
---|
63 | def check(ns, *skip): |
---|
64 | for obj in ns.iterelementvalues(): |
---|
65 | if not issubclass(obj, skip): |
---|
66 | node = obj() |
---|
67 | for (attrname, attrvalue) in node.attrs.alloweditems(): |
---|
68 | if attrvalue.required: |
---|
69 | if attrvalue.values: |
---|
70 | node[attrname] = attrvalue.values[0] |
---|
71 | else: |
---|
72 | node[attrname] = "foo" |
---|
73 | node.conv().asBytes() |
---|
74 | for obj in ns.iterentityvalues(): |
---|
75 | node = obj() |
---|
76 | node.conv().asBytes() |
---|
77 | for obj in ns.iterprocinstvalues(): |
---|
78 | node = obj() |
---|
79 | node.conv().asBytes() |
---|
80 | |
---|
81 | yield check, html |
---|
82 | yield check, ihtml |
---|
83 | yield check, wml |
---|
84 | yield check, specials, specials.include, specials.filetime, specials.filesize |
---|
85 | yield check, form |
---|
86 | yield check, meta |
---|
87 | yield check, htmlspecials, htmlspecials.autoimg, htmlspecials.autopixel |
---|
88 | yield check, svg |
---|
89 | yield check, fo |
---|
90 | yield check, docbook |
---|
91 | yield check, jsp |
---|
92 | yield check, struts_html |
---|
93 | yield check, struts_config |
---|
94 | yield check, tld |
---|
95 | |
---|
96 | |
---|
97 | def test_nsupdate(): |
---|
98 | def createns(): |
---|
99 | class __ns__(xsc.Namespace): |
---|
100 | xmlname = "gurk" |
---|
101 | xmlurl = "http://www.gurk.com/" |
---|
102 | class foo(xsc.Element): pass |
---|
103 | class bar(xsc.Element): pass |
---|
104 | return __ns__ |
---|
105 | |
---|
106 | class ns1: |
---|
107 | class foo(xsc.Element): pass |
---|
108 | class bar(xsc.Element): pass |
---|
109 | class foo2(xsc.Element): pass |
---|
110 | class bar2(xsc.Element): pass |
---|
111 | class ns2: |
---|
112 | class foo(xsc.Element): pass |
---|
113 | class bar(xsc.Element): pass |
---|
114 | class foo2(xsc.Element): pass |
---|
115 | class bar2(xsc.Element): pass |
---|
116 | a = [ {"foo": ns.foo, "bar": ns.bar, "foo2": ns.foo2, "bar2": ns.bar2} for ns in (ns1, ns2) ] |
---|
117 | |
---|
118 | ns = createns() |
---|
119 | ns.update(*a) |
---|
120 | assert ns.element("foo") is ns2.foo |
---|
121 | assert ns.element("bar") is ns2.bar |
---|
122 | assert ns.element("foo2") is ns2.foo2 |
---|
123 | assert ns.element("bar2") is ns2.bar2 |
---|
124 | |
---|
125 | ns = createns() |
---|
126 | ns.updatenew(*a) |
---|
127 | assert ns.element("foo") is ns.foo |
---|
128 | assert ns.element("bar") is ns.bar |
---|
129 | assert ns.element("foo2") is ns2.foo2 |
---|
130 | assert ns.element("bar2") is ns2.bar2 |
---|
131 | |
---|
132 | ns = createns() |
---|
133 | ns.updateexisting(*a) |
---|
134 | assert ns.element("foo") == ns2.foo |
---|
135 | assert ns.element("bar") == ns2.bar |
---|
136 | py.test.raises(xsc.IllegalElementError, ns.element, "foo2") |
---|
137 | py.test.raises(xsc.IllegalElementError, ns.element, "bar2") |
---|
138 | |
---|
139 | |
---|
140 | def test_attributeexamples(): |
---|
141 | assert xsc.amp.__name__ == "amp" |
---|
142 | assert xsc.amp.xmlname == u"amp" |
---|
143 | assert xsc.amp.__ns__ is None |
---|
144 | assert xsc.amp.xmlprefix() is None |
---|
145 | |
---|
146 | assert chars.uuml.__name__ == "uuml" |
---|
147 | assert chars.uuml.xmlname == u"uuml" |
---|
148 | assert chars.uuml.__ns__ is chars |
---|
149 | assert chars.uuml.xmlprefix() == "chars" |
---|
150 | |
---|
151 | assert html.a.Attrs.class_.__name__ == "class_" |
---|
152 | assert html.a.Attrs.class_.xmlname == u"class" |
---|
153 | assert html.a.Attrs.class_.__ns__ is None |
---|
154 | |
---|
155 | assert xml.Attrs.lang.__name__ == "lang" |
---|
156 | assert xml.Attrs.lang.xmlname == u"lang" |
---|
157 | assert xml.Attrs.lang.__ns__ is xml |
---|
158 | assert xml.Attrs.lang.xmlprefix() == "xml" |
---|
159 | |
---|
160 | |
---|
161 | def test_autoinherit(): |
---|
162 | class NS1(xsc.Namespace): |
---|
163 | xmlname = "test" |
---|
164 | xmlurl = "test" |
---|
165 | class foo(xsc.Element): |
---|
166 | model = False |
---|
167 | def convert(self, converter): |
---|
168 | e = self.__ns__.bar() |
---|
169 | return e.convert(converter) |
---|
170 | class bar(xsc.Entity): |
---|
171 | def convert(self, converter): |
---|
172 | return xsc.Text(17) |
---|
173 | |
---|
174 | class NS2(NS1): |
---|
175 | xmlname = "test" |
---|
176 | class bar(xsc.Entity): |
---|
177 | def convert(self, converter): |
---|
178 | return xsc.Text(23) |
---|
179 | |
---|
180 | assert unicode(NS1.foo().conv()) == u"17" |
---|
181 | assert unicode(NS2.foo().conv()) == u"23" |
---|
182 | |
---|
183 | |
---|
184 | def check_nskeysvaluesitems(ns, method, resname, resclass): |
---|
185 | assert getattr(ns, method + "keys")(xml=False) == [resname] |
---|
186 | assert getattr(ns, method + "keys")(xml=True) == [resname[:-1]] |
---|
187 | |
---|
188 | assert getattr(ns, method + "values")() == [resclass] |
---|
189 | |
---|
190 | assert getattr(ns, method + "items")(xml=False) == [(resname, resclass)] |
---|
191 | assert getattr(ns, method + "items")(xml=True) == [(resname[:-1], resclass)] |
---|
192 | |
---|
193 | |
---|
194 | def test_nskeysvaluesitems(): |
---|
195 | class NS(xsc.Namespace): |
---|
196 | xmlname = "test" |
---|
197 | class el_(xsc.Element): |
---|
198 | xmlname = "el" |
---|
199 | class en_(xsc.Entity): |
---|
200 | xmlname = "en" |
---|
201 | class pi_(xsc.ProcInst): |
---|
202 | xmlname = "pi" |
---|
203 | class cr_(xsc.CharRef): |
---|
204 | xmlname = "cr" |
---|
205 | codepoint = 0x4242 |
---|
206 | |
---|
207 | check_nskeysvaluesitems(NS, "element", "el_", NS.el_) |
---|
208 | |
---|
209 | keys = NS.entitykeys(xml=False) |
---|
210 | assert len(keys) == 2 |
---|
211 | assert "en_" in keys |
---|
212 | assert "cr_" in keys |
---|
213 | keys = NS.entitykeys(xml=True) |
---|
214 | assert len(keys) == 2 |
---|
215 | assert "en" in keys |
---|
216 | assert "cr" in keys |
---|
217 | |
---|
218 | values = NS.entityvalues() |
---|
219 | assert len(values) == 2 |
---|
220 | assert NS.en_ in values |
---|
221 | assert NS.cr_ in values |
---|
222 | |
---|
223 | items = NS.entityitems(xml=False) |
---|
224 | assert len(items) == 2 |
---|
225 | assert ("en_", NS.en_) in items |
---|
226 | assert ("cr_", NS.cr_) in items |
---|
227 | items = NS.entityitems(xml=True) |
---|
228 | assert len(items) == 2 |
---|
229 | assert ("en", NS.en_) in items |
---|
230 | assert ("cr", NS.cr_) in items |
---|
231 | |
---|
232 | check_nskeysvaluesitems(NS, "procinst", "pi_", NS.pi_) |
---|
233 | |
---|
234 | check_nskeysvaluesitems(NS, "charref", "cr_", NS.cr_) |
---|
235 | |
---|
236 | |
---|
237 | def test_prefixsubclasses(): |
---|
238 | class NS1(xsc.Namespace): |
---|
239 | xmlname = "ns" |
---|
240 | xmlurl = "http://xmlns.ns.info/" |
---|
241 | |
---|
242 | class gurk(xsc.Element): |
---|
243 | model = False |
---|
244 | |
---|
245 | class NS2(NS1): |
---|
246 | xmlname = "ns" |
---|
247 | |
---|
248 | p = xsc.Prefixes(ns=NS1) |
---|
249 | |
---|
250 | assert \ |
---|
251 | NS1.gurk().asBytes(xhtml=2, prefixmode=2, prefixes=p) == \ |
---|
252 | '<ns:gurk xmlns:ns="http://xmlns.ns.info/"/>' |
---|
253 | |
---|
254 | # The sub namespace should pick up the prefix defined for the first one |
---|
255 | assert \ |
---|
256 | NS2.gurk().asBytes(xhtml=2, prefixmode=2, prefixes=p) == \ |
---|
257 | '<ns:gurk xmlns:ns="http://xmlns.ns.info/"/>' |
---|