1 | n/a | import io |
---|
2 | n/a | import unittest |
---|
3 | n/a | import xml.sax |
---|
4 | n/a | |
---|
5 | n/a | from xml.sax.xmlreader import AttributesImpl |
---|
6 | n/a | from xml.dom import pulldom |
---|
7 | n/a | |
---|
8 | n/a | from test.support import findfile |
---|
9 | n/a | |
---|
10 | n/a | |
---|
11 | n/a | tstfile = findfile("test.xml", subdir="xmltestdata") |
---|
12 | n/a | |
---|
13 | n/a | # A handy XML snippet, containing attributes, a namespace prefix, and a |
---|
14 | n/a | # self-closing tag: |
---|
15 | n/a | SMALL_SAMPLE = """<?xml version="1.0"?> |
---|
16 | n/a | <html xmlns="http://www.w3.org/1999/xhtml" xmlns:xdc="http://www.xml.com/books"> |
---|
17 | n/a | <!-- A comment --> |
---|
18 | n/a | <title>Introduction to XSL</title> |
---|
19 | n/a | <hr/> |
---|
20 | n/a | <p><xdc:author xdc:attrib="prefixed attribute" attrib="other attrib">A. Namespace</xdc:author></p> |
---|
21 | n/a | </html>""" |
---|
22 | n/a | |
---|
23 | n/a | |
---|
24 | n/a | class PullDOMTestCase(unittest.TestCase): |
---|
25 | n/a | |
---|
26 | n/a | def test_parse(self): |
---|
27 | n/a | """Minimal test of DOMEventStream.parse()""" |
---|
28 | n/a | |
---|
29 | n/a | # This just tests that parsing from a stream works. Actual parser |
---|
30 | n/a | # semantics are tested using parseString with a more focused XML |
---|
31 | n/a | # fragment. |
---|
32 | n/a | |
---|
33 | n/a | # Test with a filename: |
---|
34 | n/a | handler = pulldom.parse(tstfile) |
---|
35 | n/a | self.addCleanup(handler.stream.close) |
---|
36 | n/a | list(handler) |
---|
37 | n/a | |
---|
38 | n/a | # Test with a file object: |
---|
39 | n/a | with open(tstfile, "rb") as fin: |
---|
40 | n/a | list(pulldom.parse(fin)) |
---|
41 | n/a | |
---|
42 | n/a | def test_parse_semantics(self): |
---|
43 | n/a | """Test DOMEventStream parsing semantics.""" |
---|
44 | n/a | |
---|
45 | n/a | items = pulldom.parseString(SMALL_SAMPLE) |
---|
46 | n/a | evt, node = next(items) |
---|
47 | n/a | # Just check the node is a Document: |
---|
48 | n/a | self.assertTrue(hasattr(node, "createElement")) |
---|
49 | n/a | self.assertEqual(pulldom.START_DOCUMENT, evt) |
---|
50 | n/a | evt, node = next(items) |
---|
51 | n/a | self.assertEqual(pulldom.START_ELEMENT, evt) |
---|
52 | n/a | self.assertEqual("html", node.tagName) |
---|
53 | n/a | self.assertEqual(2, len(node.attributes)) |
---|
54 | n/a | self.assertEqual(node.attributes.getNamedItem("xmlns:xdc").value, |
---|
55 | n/a | "http://www.xml.com/books") |
---|
56 | n/a | evt, node = next(items) |
---|
57 | n/a | self.assertEqual(pulldom.CHARACTERS, evt) # Line break |
---|
58 | n/a | evt, node = next(items) |
---|
59 | n/a | # XXX - A comment should be reported here! |
---|
60 | n/a | # self.assertEqual(pulldom.COMMENT, evt) |
---|
61 | n/a | # Line break after swallowed comment: |
---|
62 | n/a | self.assertEqual(pulldom.CHARACTERS, evt) |
---|
63 | n/a | evt, node = next(items) |
---|
64 | n/a | self.assertEqual("title", node.tagName) |
---|
65 | n/a | title_node = node |
---|
66 | n/a | evt, node = next(items) |
---|
67 | n/a | self.assertEqual(pulldom.CHARACTERS, evt) |
---|
68 | n/a | self.assertEqual("Introduction to XSL", node.data) |
---|
69 | n/a | evt, node = next(items) |
---|
70 | n/a | self.assertEqual(pulldom.END_ELEMENT, evt) |
---|
71 | n/a | self.assertEqual("title", node.tagName) |
---|
72 | n/a | self.assertTrue(title_node is node) |
---|
73 | n/a | evt, node = next(items) |
---|
74 | n/a | self.assertEqual(pulldom.CHARACTERS, evt) |
---|
75 | n/a | evt, node = next(items) |
---|
76 | n/a | self.assertEqual(pulldom.START_ELEMENT, evt) |
---|
77 | n/a | self.assertEqual("hr", node.tagName) |
---|
78 | n/a | evt, node = next(items) |
---|
79 | n/a | self.assertEqual(pulldom.END_ELEMENT, evt) |
---|
80 | n/a | self.assertEqual("hr", node.tagName) |
---|
81 | n/a | evt, node = next(items) |
---|
82 | n/a | self.assertEqual(pulldom.CHARACTERS, evt) |
---|
83 | n/a | evt, node = next(items) |
---|
84 | n/a | self.assertEqual(pulldom.START_ELEMENT, evt) |
---|
85 | n/a | self.assertEqual("p", node.tagName) |
---|
86 | n/a | evt, node = next(items) |
---|
87 | n/a | self.assertEqual(pulldom.START_ELEMENT, evt) |
---|
88 | n/a | self.assertEqual("xdc:author", node.tagName) |
---|
89 | n/a | evt, node = next(items) |
---|
90 | n/a | self.assertEqual(pulldom.CHARACTERS, evt) |
---|
91 | n/a | evt, node = next(items) |
---|
92 | n/a | self.assertEqual(pulldom.END_ELEMENT, evt) |
---|
93 | n/a | self.assertEqual("xdc:author", node.tagName) |
---|
94 | n/a | evt, node = next(items) |
---|
95 | n/a | self.assertEqual(pulldom.END_ELEMENT, evt) |
---|
96 | n/a | evt, node = next(items) |
---|
97 | n/a | self.assertEqual(pulldom.CHARACTERS, evt) |
---|
98 | n/a | evt, node = next(items) |
---|
99 | n/a | self.assertEqual(pulldom.END_ELEMENT, evt) |
---|
100 | n/a | # XXX No END_DOCUMENT item is ever obtained: |
---|
101 | n/a | #evt, node = next(items) |
---|
102 | n/a | #self.assertEqual(pulldom.END_DOCUMENT, evt) |
---|
103 | n/a | |
---|
104 | n/a | def test_expandItem(self): |
---|
105 | n/a | """Ensure expandItem works as expected.""" |
---|
106 | n/a | items = pulldom.parseString(SMALL_SAMPLE) |
---|
107 | n/a | # Loop through the nodes until we get to a "title" start tag: |
---|
108 | n/a | for evt, item in items: |
---|
109 | n/a | if evt == pulldom.START_ELEMENT and item.tagName == "title": |
---|
110 | n/a | items.expandNode(item) |
---|
111 | n/a | self.assertEqual(1, len(item.childNodes)) |
---|
112 | n/a | break |
---|
113 | n/a | else: |
---|
114 | n/a | self.fail("No \"title\" element detected in SMALL_SAMPLE!") |
---|
115 | n/a | # Loop until we get to the next start-element: |
---|
116 | n/a | for evt, node in items: |
---|
117 | n/a | if evt == pulldom.START_ELEMENT: |
---|
118 | n/a | break |
---|
119 | n/a | self.assertEqual("hr", node.tagName, |
---|
120 | n/a | "expandNode did not leave DOMEventStream in the correct state.") |
---|
121 | n/a | # Attempt to expand a standalone element: |
---|
122 | n/a | items.expandNode(node) |
---|
123 | n/a | self.assertEqual(next(items)[0], pulldom.CHARACTERS) |
---|
124 | n/a | evt, node = next(items) |
---|
125 | n/a | self.assertEqual(node.tagName, "p") |
---|
126 | n/a | items.expandNode(node) |
---|
127 | n/a | next(items) # Skip character data |
---|
128 | n/a | evt, node = next(items) |
---|
129 | n/a | self.assertEqual(node.tagName, "html") |
---|
130 | n/a | with self.assertRaises(StopIteration): |
---|
131 | n/a | next(items) |
---|
132 | n/a | items.clear() |
---|
133 | n/a | self.assertIsNone(items.parser) |
---|
134 | n/a | self.assertIsNone(items.stream) |
---|
135 | n/a | |
---|
136 | n/a | @unittest.expectedFailure |
---|
137 | n/a | def test_comment(self): |
---|
138 | n/a | """PullDOM does not receive "comment" events.""" |
---|
139 | n/a | items = pulldom.parseString(SMALL_SAMPLE) |
---|
140 | n/a | for evt, _ in items: |
---|
141 | n/a | if evt == pulldom.COMMENT: |
---|
142 | n/a | break |
---|
143 | n/a | else: |
---|
144 | n/a | self.fail("No comment was encountered") |
---|
145 | n/a | |
---|
146 | n/a | @unittest.expectedFailure |
---|
147 | n/a | def test_end_document(self): |
---|
148 | n/a | """PullDOM does not receive "end-document" events.""" |
---|
149 | n/a | items = pulldom.parseString(SMALL_SAMPLE) |
---|
150 | n/a | # Read all of the nodes up to and including </html>: |
---|
151 | n/a | for evt, node in items: |
---|
152 | n/a | if evt == pulldom.END_ELEMENT and node.tagName == "html": |
---|
153 | n/a | break |
---|
154 | n/a | try: |
---|
155 | n/a | # Assert that the next node is END_DOCUMENT: |
---|
156 | n/a | evt, node = next(items) |
---|
157 | n/a | self.assertEqual(pulldom.END_DOCUMENT, evt) |
---|
158 | n/a | except StopIteration: |
---|
159 | n/a | self.fail( |
---|
160 | n/a | "Ran out of events, but should have received END_DOCUMENT") |
---|
161 | n/a | |
---|
162 | n/a | |
---|
163 | n/a | class ThoroughTestCase(unittest.TestCase): |
---|
164 | n/a | """Test the hard-to-reach parts of pulldom.""" |
---|
165 | n/a | |
---|
166 | n/a | def test_thorough_parse(self): |
---|
167 | n/a | """Test some of the hard-to-reach parts of PullDOM.""" |
---|
168 | n/a | self._test_thorough(pulldom.parse(None, parser=SAXExerciser())) |
---|
169 | n/a | |
---|
170 | n/a | @unittest.expectedFailure |
---|
171 | n/a | def test_sax2dom_fail(self): |
---|
172 | n/a | """SAX2DOM can"t handle a PI before the root element.""" |
---|
173 | n/a | pd = SAX2DOMTestHelper(None, SAXExerciser(), 12) |
---|
174 | n/a | self._test_thorough(pd) |
---|
175 | n/a | |
---|
176 | n/a | def test_thorough_sax2dom(self): |
---|
177 | n/a | """Test some of the hard-to-reach parts of SAX2DOM.""" |
---|
178 | n/a | pd = SAX2DOMTestHelper(None, SAX2DOMExerciser(), 12) |
---|
179 | n/a | self._test_thorough(pd, False) |
---|
180 | n/a | |
---|
181 | n/a | def _test_thorough(self, pd, before_root=True): |
---|
182 | n/a | """Test some of the hard-to-reach parts of the parser, using a mock |
---|
183 | n/a | parser.""" |
---|
184 | n/a | |
---|
185 | n/a | evt, node = next(pd) |
---|
186 | n/a | self.assertEqual(pulldom.START_DOCUMENT, evt) |
---|
187 | n/a | # Just check the node is a Document: |
---|
188 | n/a | self.assertTrue(hasattr(node, "createElement")) |
---|
189 | n/a | |
---|
190 | n/a | if before_root: |
---|
191 | n/a | evt, node = next(pd) |
---|
192 | n/a | self.assertEqual(pulldom.COMMENT, evt) |
---|
193 | n/a | self.assertEqual("a comment", node.data) |
---|
194 | n/a | evt, node = next(pd) |
---|
195 | n/a | self.assertEqual(pulldom.PROCESSING_INSTRUCTION, evt) |
---|
196 | n/a | self.assertEqual("target", node.target) |
---|
197 | n/a | self.assertEqual("data", node.data) |
---|
198 | n/a | |
---|
199 | n/a | evt, node = next(pd) |
---|
200 | n/a | self.assertEqual(pulldom.START_ELEMENT, evt) |
---|
201 | n/a | self.assertEqual("html", node.tagName) |
---|
202 | n/a | |
---|
203 | n/a | evt, node = next(pd) |
---|
204 | n/a | self.assertEqual(pulldom.COMMENT, evt) |
---|
205 | n/a | self.assertEqual("a comment", node.data) |
---|
206 | n/a | evt, node = next(pd) |
---|
207 | n/a | self.assertEqual(pulldom.PROCESSING_INSTRUCTION, evt) |
---|
208 | n/a | self.assertEqual("target", node.target) |
---|
209 | n/a | self.assertEqual("data", node.data) |
---|
210 | n/a | |
---|
211 | n/a | evt, node = next(pd) |
---|
212 | n/a | self.assertEqual(pulldom.START_ELEMENT, evt) |
---|
213 | n/a | self.assertEqual("p", node.tagName) |
---|
214 | n/a | |
---|
215 | n/a | evt, node = next(pd) |
---|
216 | n/a | self.assertEqual(pulldom.CHARACTERS, evt) |
---|
217 | n/a | self.assertEqual("text", node.data) |
---|
218 | n/a | evt, node = next(pd) |
---|
219 | n/a | self.assertEqual(pulldom.END_ELEMENT, evt) |
---|
220 | n/a | self.assertEqual("p", node.tagName) |
---|
221 | n/a | evt, node = next(pd) |
---|
222 | n/a | self.assertEqual(pulldom.END_ELEMENT, evt) |
---|
223 | n/a | self.assertEqual("html", node.tagName) |
---|
224 | n/a | evt, node = next(pd) |
---|
225 | n/a | self.assertEqual(pulldom.END_DOCUMENT, evt) |
---|
226 | n/a | |
---|
227 | n/a | |
---|
228 | n/a | class SAXExerciser(object): |
---|
229 | n/a | """A fake sax parser that calls some of the harder-to-reach sax methods to |
---|
230 | n/a | ensure it emits the correct events""" |
---|
231 | n/a | |
---|
232 | n/a | def setContentHandler(self, handler): |
---|
233 | n/a | self._handler = handler |
---|
234 | n/a | |
---|
235 | n/a | def parse(self, _): |
---|
236 | n/a | h = self._handler |
---|
237 | n/a | h.startDocument() |
---|
238 | n/a | |
---|
239 | n/a | # The next two items ensure that items preceding the first |
---|
240 | n/a | # start_element are properly stored and emitted: |
---|
241 | n/a | h.comment("a comment") |
---|
242 | n/a | h.processingInstruction("target", "data") |
---|
243 | n/a | |
---|
244 | n/a | h.startElement("html", AttributesImpl({})) |
---|
245 | n/a | |
---|
246 | n/a | h.comment("a comment") |
---|
247 | n/a | h.processingInstruction("target", "data") |
---|
248 | n/a | |
---|
249 | n/a | h.startElement("p", AttributesImpl({"class": "paraclass"})) |
---|
250 | n/a | h.characters("text") |
---|
251 | n/a | h.endElement("p") |
---|
252 | n/a | h.endElement("html") |
---|
253 | n/a | h.endDocument() |
---|
254 | n/a | |
---|
255 | n/a | def stub(self, *args, **kwargs): |
---|
256 | n/a | """Stub method. Does nothing.""" |
---|
257 | n/a | pass |
---|
258 | n/a | setProperty = stub |
---|
259 | n/a | setFeature = stub |
---|
260 | n/a | |
---|
261 | n/a | |
---|
262 | n/a | class SAX2DOMExerciser(SAXExerciser): |
---|
263 | n/a | """The same as SAXExerciser, but without the processing instruction and |
---|
264 | n/a | comment before the root element, because S2D can"t handle it""" |
---|
265 | n/a | |
---|
266 | n/a | def parse(self, _): |
---|
267 | n/a | h = self._handler |
---|
268 | n/a | h.startDocument() |
---|
269 | n/a | h.startElement("html", AttributesImpl({})) |
---|
270 | n/a | h.comment("a comment") |
---|
271 | n/a | h.processingInstruction("target", "data") |
---|
272 | n/a | h.startElement("p", AttributesImpl({"class": "paraclass"})) |
---|
273 | n/a | h.characters("text") |
---|
274 | n/a | h.endElement("p") |
---|
275 | n/a | h.endElement("html") |
---|
276 | n/a | h.endDocument() |
---|
277 | n/a | |
---|
278 | n/a | |
---|
279 | n/a | class SAX2DOMTestHelper(pulldom.DOMEventStream): |
---|
280 | n/a | """Allows us to drive SAX2DOM from a DOMEventStream.""" |
---|
281 | n/a | |
---|
282 | n/a | def reset(self): |
---|
283 | n/a | self.pulldom = pulldom.SAX2DOM() |
---|
284 | n/a | # This content handler relies on namespace support |
---|
285 | n/a | self.parser.setFeature(xml.sax.handler.feature_namespaces, 1) |
---|
286 | n/a | self.parser.setContentHandler(self.pulldom) |
---|
287 | n/a | |
---|
288 | n/a | |
---|
289 | n/a | class SAX2DOMTestCase(unittest.TestCase): |
---|
290 | n/a | |
---|
291 | n/a | def confirm(self, test, testname="Test"): |
---|
292 | n/a | self.assertTrue(test, testname) |
---|
293 | n/a | |
---|
294 | n/a | def test_basic(self): |
---|
295 | n/a | """Ensure SAX2DOM can parse from a stream.""" |
---|
296 | n/a | with io.StringIO(SMALL_SAMPLE) as fin: |
---|
297 | n/a | sd = SAX2DOMTestHelper(fin, xml.sax.make_parser(), |
---|
298 | n/a | len(SMALL_SAMPLE)) |
---|
299 | n/a | for evt, node in sd: |
---|
300 | n/a | if evt == pulldom.START_ELEMENT and node.tagName == "html": |
---|
301 | n/a | break |
---|
302 | n/a | # Because the buffer is the same length as the XML, all the |
---|
303 | n/a | # nodes should have been parsed and added: |
---|
304 | n/a | self.assertGreater(len(node.childNodes), 0) |
---|
305 | n/a | |
---|
306 | n/a | def testSAX2DOM(self): |
---|
307 | n/a | """Ensure SAX2DOM expands nodes as expected.""" |
---|
308 | n/a | sax2dom = pulldom.SAX2DOM() |
---|
309 | n/a | sax2dom.startDocument() |
---|
310 | n/a | sax2dom.startElement("doc", {}) |
---|
311 | n/a | sax2dom.characters("text") |
---|
312 | n/a | sax2dom.startElement("subelm", {}) |
---|
313 | n/a | sax2dom.characters("text") |
---|
314 | n/a | sax2dom.endElement("subelm") |
---|
315 | n/a | sax2dom.characters("text") |
---|
316 | n/a | sax2dom.endElement("doc") |
---|
317 | n/a | sax2dom.endDocument() |
---|
318 | n/a | |
---|
319 | n/a | doc = sax2dom.document |
---|
320 | n/a | root = doc.documentElement |
---|
321 | n/a | (text1, elm1, text2) = root.childNodes |
---|
322 | n/a | text3 = elm1.childNodes[0] |
---|
323 | n/a | |
---|
324 | n/a | self.assertIsNone(text1.previousSibling) |
---|
325 | n/a | self.assertIs(text1.nextSibling, elm1) |
---|
326 | n/a | self.assertIs(elm1.previousSibling, text1) |
---|
327 | n/a | self.assertIs(elm1.nextSibling, text2) |
---|
328 | n/a | self.assertIs(text2.previousSibling, elm1) |
---|
329 | n/a | self.assertIsNone(text2.nextSibling) |
---|
330 | n/a | self.assertIsNone(text3.previousSibling) |
---|
331 | n/a | self.assertIsNone(text3.nextSibling) |
---|
332 | n/a | |
---|
333 | n/a | self.assertIs(root.parentNode, doc) |
---|
334 | n/a | self.assertIs(text1.parentNode, root) |
---|
335 | n/a | self.assertIs(elm1.parentNode, root) |
---|
336 | n/a | self.assertIs(text2.parentNode, root) |
---|
337 | n/a | self.assertIs(text3.parentNode, elm1) |
---|
338 | n/a | doc.unlink() |
---|
339 | n/a | |
---|
340 | n/a | |
---|
341 | n/a | if __name__ == "__main__": |
---|
342 | n/a | unittest.main() |
---|