Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1297)

Side by Side Diff: third_party/twisted_8_1/twisted/words/test/test_domish.py

Issue 12261012: Remove third_party/twisted_8_1 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/build
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 # Copyright (c) 2001-2007 Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4 """
5 Tests for L{twisted.words.xish.domish}.
6 """
7
8 from twisted.trial import unittest
9 from twisted.words.xish import domish
10
11 class DomishTestCase(unittest.TestCase):
12 def testEscaping(self):
13 s = "&<>'\""
14 self.assertEquals(domish.escapeToXml(s), "&amp;&lt;&gt;'\"")
15 self.assertEquals(domish.escapeToXml(s, 1), "&amp;&lt;&gt;&apos;&quot;")
16
17 def testNamespaceObject(self):
18 ns = domish.Namespace("testns")
19 self.assertEquals(ns.foo, ("testns", "foo"))
20
21 def testElementInit(self):
22 e = domish.Element((None, "foo"))
23 self.assertEquals(e.name, "foo")
24 self.assertEquals(e.uri, None)
25 self.assertEquals(e.defaultUri, None)
26 self.assertEquals(e.parent, None)
27
28 e = domish.Element(("", "foo"))
29 self.assertEquals(e.name, "foo")
30 self.assertEquals(e.uri, "")
31 self.assertEquals(e.defaultUri, "")
32 self.assertEquals(e.parent, None)
33
34 e = domish.Element(("testns", "foo"))
35 self.assertEquals(e.name, "foo")
36 self.assertEquals(e.uri, "testns")
37 self.assertEquals(e.defaultUri, "testns")
38 self.assertEquals(e.parent, None)
39
40 e = domish.Element(("testns", "foo"), "test2ns")
41 self.assertEquals(e.name, "foo")
42 self.assertEquals(e.uri, "testns")
43 self.assertEquals(e.defaultUri, "test2ns")
44
45 def testChildOps(self):
46 e = domish.Element(("testns", "foo"))
47 e.addContent("somecontent")
48 b2 = e.addElement(("testns2", "bar2"))
49 e["attrib1"] = "value1"
50 e[("testns2", "attrib2")] = "value2"
51 e.addElement("bar")
52 e.addElement("bar")
53 e.addContent("abc")
54 e.addContent("123")
55
56 # Check content merging
57 self.assertEquals(e.children[-1], "abc123")
58
59 # Check str()/content extraction
60 self.assertEquals(str(e), "somecontent")
61
62 # Check direct child accessor
63 self.assertEquals(e.bar2, b2)
64 e.bar2.addContent("subcontent")
65 e.bar2["bar2value"] = "somevalue"
66
67 # Check child ops
68 self.assertEquals(e.children[1], e.bar2)
69 self.assertEquals(e.children[2], e.bar)
70
71 # Check attribute ops
72 self.assertEquals(e["attrib1"], "value1")
73 del e["attrib1"]
74 self.assertEquals(e.hasAttribute("attrib1"), 0)
75 self.assertEquals(e.hasAttribute("attrib2"), 0)
76 self.assertEquals(e[("testns2", "attrib2")], "value2")
77
78 class DomishStreamTests:
79 def setUp(self):
80 self.doc_started = False
81 self.doc_ended = False
82 self.root = None
83 self.elements = []
84 self.stream = self.streamClass()
85 self.stream.DocumentStartEvent = self._docStarted
86 self.stream.ElementEvent = self.elements.append
87 self.stream.DocumentEndEvent = self._docEnded
88
89 def _docStarted(self, root):
90 self.root = root
91 self.doc_started = True
92
93 def _docEnded(self):
94 self.doc_ended = True
95
96 def doTest(self, xml):
97 self.stream.parse(xml)
98
99 def testHarness(self):
100 xml = "<root><child/><child2/></root>"
101 self.stream.parse(xml)
102 self.assertEquals(self.doc_started, True)
103 self.assertEquals(self.root.name, 'root')
104 self.assertEquals(self.elements[0].name, 'child')
105 self.assertEquals(self.elements[1].name, 'child2')
106 self.assertEquals(self.doc_ended, True)
107
108 def testBasic(self):
109 xml = "<stream:stream xmlns:stream='etherx' xmlns='jabber'>\n" + \
110 " <message to='bar'>" + \
111 " <x xmlns='xdelay'>some&amp;data&gt;</x>" + \
112 " </message>" + \
113 "</stream:stream>"
114
115 self.stream.parse(xml)
116 self.assertEquals(self.root.name, 'stream')
117 self.assertEquals(self.root.uri, 'etherx')
118 self.assertEquals(self.elements[0].name, 'message')
119 self.assertEquals(self.elements[0].uri, 'jabber')
120 self.assertEquals(self.elements[0]['to'], 'bar')
121 self.assertEquals(self.elements[0].x.uri, 'xdelay')
122 self.assertEquals(unicode(self.elements[0].x), 'some&data>')
123
124 def testNoRootNS(self):
125 xml = "<stream><error xmlns='etherx'/></stream>"
126
127 self.stream.parse(xml)
128 self.assertEquals(self.root.uri, '')
129 self.assertEquals(self.elements[0].uri, 'etherx')
130
131 def testNoDefaultNS(self):
132 xml = "<stream:stream xmlns:stream='etherx'><error/></stream:stream>"""
133
134 self.stream.parse(xml)
135 self.assertEquals(self.root.uri, 'etherx')
136 self.assertEquals(self.root.defaultUri, '')
137 self.assertEquals(self.elements[0].uri, '')
138 self.assertEquals(self.elements[0].defaultUri, '')
139
140 def testChildDefaultNS(self):
141 xml = "<root xmlns='testns'><child/></root>"
142
143 self.stream.parse(xml)
144 self.assertEquals(self.root.uri, 'testns')
145 self.assertEquals(self.elements[0].uri, 'testns')
146
147 def testEmptyChildNS(self):
148 xml = "<root xmlns='testns'><child1><child2 xmlns=''/></child1></root>"
149
150 self.stream.parse(xml)
151 self.assertEquals(self.elements[0].child2.uri, '')
152
153 def testChildPrefix(self):
154 xml = "<root xmlns='testns' xmlns:foo='testns2'><foo:child/></root>"
155
156 self.stream.parse(xml)
157 self.assertEquals(self.root.localPrefixes['foo'], 'testns2')
158 self.assertEquals(self.elements[0].uri, 'testns2')
159
160 def testUnclosedElement(self):
161 self.assertRaises(domish.ParserError, self.stream.parse,
162 "<root><error></root>")
163
164 def test_namespaceReuse(self):
165 """
166 Test that reuse of namespaces does affect an element's serialization.
167
168 When one element uses a prefix for a certain namespace, this is
169 stored in the C{localPrefixes} attribute of the element. We want
170 to make sure that elements created after such use, won't have this
171 prefix end up in their C{localPrefixes} attribute, too.
172 """
173
174 xml = """<root>
175 <foo:child1 xmlns:foo='testns'/>
176 <child2 xmlns='testns'/>
177 </root>"""
178
179 self.stream.parse(xml)
180 self.assertEquals('child1', self.elements[0].name)
181 self.assertEquals('testns', self.elements[0].uri)
182 self.assertEquals('', self.elements[0].defaultUri)
183 self.assertEquals({'foo': 'testns'}, self.elements[0].localPrefixes)
184 self.assertEquals('child2', self.elements[1].name)
185 self.assertEquals('testns', self.elements[1].uri)
186 self.assertEquals('testns', self.elements[1].defaultUri)
187 self.assertEquals({}, self.elements[1].localPrefixes)
188
189 class DomishExpatStreamTestCase(unittest.TestCase, DomishStreamTests):
190 def setUp(self):
191 DomishStreamTests.setUp(self)
192
193 def setUpClass(self):
194 try:
195 import pyexpat
196 except ImportError:
197 raise unittest.SkipTest, "Skipping ExpatElementStream test, since no expat wrapper is available."
198
199 self.streamClass = domish.ExpatElementStream
200
201 class DomishSuxStreamTestCase(unittest.TestCase, DomishStreamTests):
202 def setUp(self):
203 DomishStreamTests.setUp(self)
204
205 def setUpClass(self):
206 if domish.SuxElementStream is None:
207 raise unittest.SkipTest, "Skipping SuxElementStream test, since twis ted.web is not available."
208
209 self.streamClass = domish.SuxElementStream
210
211
212
213 class SerializerTests(unittest.TestCase):
214 def testNoNamespace(self):
215 e = domish.Element((None, "foo"))
216 self.assertEquals(e.toXml(), "<foo/>")
217 self.assertEquals(e.toXml(closeElement = 0), "<foo>")
218
219 def testDefaultNamespace(self):
220 e = domish.Element(("testns", "foo"))
221 self.assertEquals(e.toXml(), "<foo xmlns='testns'/>")
222
223 def testOtherNamespace(self):
224 e = domish.Element(("testns", "foo"), "testns2")
225 self.assertEquals(e.toXml({'testns': 'bar'}),
226 "<bar:foo xmlns:bar='testns' xmlns='testns2'/>")
227
228 def testChildDefaultNamespace(self):
229 e = domish.Element(("testns", "foo"))
230 e.addElement("bar")
231 self.assertEquals(e.toXml(), "<foo xmlns='testns'><bar/></foo>")
232
233 def testChildSameNamespace(self):
234 e = domish.Element(("testns", "foo"))
235 e.addElement(("testns", "bar"))
236 self.assertEquals(e.toXml(), "<foo xmlns='testns'><bar/></foo>")
237
238 def testChildSameDefaultNamespace(self):
239 e = domish.Element(("testns", "foo"))
240 e.addElement("bar", "testns")
241 self.assertEquals(e.toXml(), "<foo xmlns='testns'><bar/></foo>")
242
243 def testChildOtherDefaultNamespace(self):
244 e = domish.Element(("testns", "foo"))
245 e.addElement(("testns2", "bar"), 'testns2')
246 self.assertEquals(e.toXml(), "<foo xmlns='testns'><bar xmlns='testns2'/> </foo>")
247
248 def testOnlyChildDefaultNamespace(self):
249 e = domish.Element((None, "foo"))
250 e.addElement(("ns2", "bar"), 'ns2')
251 self.assertEquals(e.toXml(), "<foo><bar xmlns='ns2'/></foo>")
252
253 def testOnlyChildDefaultNamespace2(self):
254 e = domish.Element((None, "foo"))
255 e.addElement("bar")
256 self.assertEquals(e.toXml(), "<foo><bar/></foo>")
257
258 def testChildInDefaultNamespace(self):
259 e = domish.Element(("testns", "foo"), "testns2")
260 e.addElement(("testns2", "bar"))
261 self.assertEquals(e.toXml(), "<xn0:foo xmlns:xn0='testns' xmlns='testns2 '><bar/></xn0:foo>")
262
263 def testQualifiedAttribute(self):
264 e = domish.Element((None, "foo"),
265 attribs = {("testns2", "bar"): "baz"})
266 self.assertEquals(e.toXml(), "<foo xmlns:xn0='testns2' xn0:bar='baz'/>")
267
268 def testQualifiedAttributeDefaultNS(self):
269 e = domish.Element(("testns", "foo"),
270 attribs = {("testns", "bar"): "baz"})
271 self.assertEquals(e.toXml(), "<foo xmlns='testns' xmlns:xn0='testns' xn0 :bar='baz'/>")
272
273 def testTwoChilds(self):
274 e = domish.Element(('', "foo"))
275 child1 = e.addElement(("testns", "bar"), "testns2")
276 child1.addElement(('testns2', 'quux'))
277 child2 = e.addElement(("testns3", "baz"), "testns4")
278 child2.addElement(('testns', 'quux'))
279 self.assertEquals(e.toXml(), "<foo><xn0:bar xmlns:xn0='testns' xmlns='te stns2'><quux/></xn0:bar><xn1:baz xmlns:xn1='testns3' xmlns='testns4'><xn0:quux x mlns:xn0='testns'/></xn1:baz></foo>")
280
281 def testXMLNamespace(self):
282 e = domish.Element((None, "foo"),
283 attribs = {("http://www.w3.org/XML/1998/namespace",
284 "lang"): "en_US"})
285 self.assertEquals(e.toXml(), "<foo xml:lang='en_US'/>")
286
287 def testQualifiedAttributeGivenListOfPrefixes(self):
288 e = domish.Element((None, "foo"),
289 attribs = {("testns2", "bar"): "baz"})
290 self.assertEquals(e.toXml({"testns2": "qux"}),
291 "<foo xmlns:qux='testns2' qux:bar='baz'/>")
292
293 def testNSPrefix(self):
294 e = domish.Element((None, "foo"),
295 attribs = {("testns2", "bar"): "baz"})
296 c = e.addElement(("testns2", "qux"))
297 c[("testns2", "bar")] = "quux"
298
299 self.assertEquals(e.toXml(), "<foo xmlns:xn0='testns2' xn0:bar='baz'><xn 0:qux xn0:bar='quux'/></foo>")
300
301 def testDefaultNSPrefix(self):
302 e = domish.Element((None, "foo"),
303 attribs = {("testns2", "bar"): "baz"})
304 c = e.addElement(("testns2", "qux"))
305 c[("testns2", "bar")] = "quux"
306 c.addElement('foo')
307
308 self.assertEquals(e.toXml(), "<foo xmlns:xn0='testns2' xn0:bar='baz'><xn 0:qux xn0:bar='quux'><xn0:foo/></xn0:qux></foo>")
309
310 def testPrefixScope(self):
311 e = domish.Element(('testns', 'foo'))
312
313 self.assertEquals(e.toXml(prefixes={'testns': 'bar'},
314 prefixesInScope=['bar']),
315 "<bar:foo/>")
316
317 def testLocalPrefixes(self):
318 e = domish.Element(('testns', 'foo'), localPrefixes={'bar': 'testns'})
319 self.assertEquals(e.toXml(), "<bar:foo xmlns:bar='testns'/>")
320
321 def testLocalPrefixesWithChild(self):
322 e = domish.Element(('testns', 'foo'), localPrefixes={'bar': 'testns'})
323 e.addElement('baz')
324 self.assertIdentical(e.baz.defaultUri, None)
325 self.assertEquals(e.toXml(), "<bar:foo xmlns:bar='testns'><baz/></bar:fo o>")
326
327 def test_prefixesReuse(self):
328 """
329 Test that prefixes passed to serialization are not modified.
330
331 This test makes sure that passing a dictionary of prefixes repeatedly
332 to C{toXml} of elements does not cause serialization errors. A
333 previous implementation changed the passed in dictionary internally,
334 causing havoc later on.
335 """
336 prefixes = {'testns': 'foo'}
337
338 # test passing of dictionary
339 s = domish.SerializerClass(prefixes=prefixes)
340 self.assertNotIdentical(prefixes, s.prefixes)
341
342 # test proper serialization on prefixes reuse
343 e = domish.Element(('testns2', 'foo'),
344 localPrefixes={'quux': 'testns2'})
345 self.assertEquals("<quux:foo xmlns:quux='testns2'/>",
346 e.toXml(prefixes=prefixes))
347 e = domish.Element(('testns2', 'foo'))
348 self.assertEquals("<foo xmlns='testns2'/>",
349 e.toXml(prefixes=prefixes))
350
351 def testRawXMLSerialization(self):
352 e = domish.Element((None, "foo"))
353 e.addRawXml("<abc123>")
354 # The testcase below should NOT generate valid XML -- that's
355 # the whole point of using the raw XML call -- it's the callers
356 # responsiblity to ensure that the data inserted is valid
357 self.assertEquals(e.toXml(), "<foo><abc123></foo>")
358
359 def testRawXMLWithUnicodeSerialization(self):
360 e = domish.Element((None, "foo"))
361 e.addRawXml(u"<degree>\u00B0</degree>")
362 self.assertEquals(e.toXml(), u"<foo><degree>\u00B0</degree></foo>")
363
364 def testUnicodeSerialization(self):
365 e = domish.Element((None, "foo"))
366 e["test"] = u"my value\u0221e"
367 e.addContent(u"A degree symbol...\u00B0")
368 self.assertEquals(e.toXml(),
369 u"<foo test='my value\u0221e'>A degree symbol...\u00B0 </foo>")
OLDNEW
« no previous file with comments | « third_party/twisted_8_1/twisted/words/test/test_basesupport.py ('k') | third_party/twisted_8_1/twisted/words/test/test_irc.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698