Fix [ 3527842 ].
[docutils.git] / test / test_nodes.py
blob1fc3477c4febe48dcf766117cdf5e45addba16c5
1 #! /usr/bin/env python
2 # -*- coding: utf-8 -*-
4 # $Id$
5 # Author: David Goodger <goodger@python.org>
6 # Copyright: This module has been placed in the public domain.
8 """
9 Test module for nodes.py.
10 """
12 import sys
13 import unittest
14 import types
15 import DocutilsTestSupport # must be imported before docutils
16 from DocutilsTestSupport import nodes, utils
17 from docutils._compat import b
19 debug = False
21 # python 2.3
22 if not hasattr(unittest.TestCase, "assertTrue"):
23 # HACK? this changes TestCase, fixes the problem for tests executing afterwards.
24 # this tests break if run alone
25 unittest.TestCase.assertTrue = unittest.TestCase.failUnless
27 class TextTests(unittest.TestCase):
29 def setUp(self):
30 self.text = nodes.Text('Line 1.\nLine 2.')
31 self.unicode_text = nodes.Text(u'Möhren')
32 self.longtext = nodes.Text('Mary had a little lamb whose '
33 'fleece was white as snow and '
34 'everwhere that Mary went the '
35 'lamb was sure to go.')
37 def test_repr(self):
38 self.assertEqual(repr(self.text), r"<#text: 'Line 1.\nLine 2.'>")
39 self.assertEqual(self.text.shortrepr(),
40 r"<#text: 'Line 1.\nLine 2.'>")
42 def test_str(self):
43 self.assertEqual(str(self.text), 'Line 1.\nLine 2.')
45 def test_unicode(self):
46 self.assertEqual(unicode(self.unicode_text), u'Möhren')
47 self.assertEqual(str(self.unicode_text), 'M\xf6hren')
49 def test_astext(self):
50 self.assertEqual(self.text.astext(), 'Line 1.\nLine 2.')
52 def test_pformat(self):
53 self.assertEqual(self.text.pformat(), 'Line 1.\nLine 2.\n')
55 def test_asciirestriction(self):
56 if sys.version_info < (3,):
57 self.assertRaises(UnicodeDecodeError, nodes.Text,
58 b('hol%s' % chr(224)))
59 else:
60 # no bytes at all allowed
61 self.assertRaises(TypeError, nodes.Text, b('hol'))
63 def test_longrepr(self):
64 self.assertEqual(repr(self.longtext), r"<#text: 'Mary had a "
65 r"little lamb whose fleece was white as snow "
66 r"and everwh ...'>")
67 self.assertEqual(self.longtext.shortrepr(),
68 r"<#text: 'Mary had a lit ...'>")
70 class ElementTests(unittest.TestCase):
72 def test_empty(self):
73 element = nodes.Element()
74 self.assertEqual(repr(element), '<Element: >')
75 self.assertEqual(str(element), '<Element/>')
76 dom = element.asdom()
77 self.assertEqual(dom.toxml(), '<Element/>')
78 dom.unlink()
79 element['attr'] = '1'
80 self.assertEqual(repr(element), '<Element: >')
81 self.assertEqual(str(element), '<Element attr="1"/>')
82 dom = element.asdom()
83 self.assertEqual(dom.toxml(), '<Element attr="1"/>')
84 dom.unlink()
85 self.assertEqual(element.pformat(), '<Element attr="1">\n')
86 del element['attr']
87 element['mark'] = u'\u2022'
88 self.assertEqual(repr(element), '<Element: >')
89 if sys.version_info < (3,):
90 self.assertEqual(str(element), '<Element mark="\\u2022"/>')
91 else:
92 self.assertEqual(str(element), '<Element mark="\u2022"/>')
93 dom = element.asdom()
94 self.assertEqual(dom.toxml(), u'<Element mark="\u2022"/>')
95 dom.unlink()
97 def test_withtext(self):
98 element = nodes.Element('text\nmore', nodes.Text('text\nmore'))
99 self.assertEqual(repr(element), r"<Element: <#text: 'text\nmore'>>")
100 self.assertEqual(str(element), '<Element>text\nmore</Element>')
101 dom = element.asdom()
102 self.assertEqual(dom.toxml(), '<Element>text\nmore</Element>')
103 dom.unlink()
104 element['attr'] = '1'
105 self.assertEqual(repr(element), r"<Element: <#text: 'text\nmore'>>")
106 self.assertEqual(str(element),
107 '<Element attr="1">text\nmore</Element>')
108 dom = element.asdom()
109 self.assertEqual(dom.toxml(),
110 '<Element attr="1">text\nmore</Element>')
111 dom.unlink()
112 self.assertEqual(element.pformat(),
113 '<Element attr="1">\n text\n more\n')
115 def test_clear(self):
116 element = nodes.Element()
117 element += nodes.Element()
118 self.assertTrue(len(element))
119 element.clear()
120 self.assertTrue(not len(element))
122 def test_normal_attributes(self):
123 element = nodes.Element()
124 self.assertTrue('foo' not in element)
125 self.assertRaises(KeyError, element.__getitem__, 'foo')
126 element['foo'] = 'sometext'
127 self.assertEqual(element['foo'], 'sometext')
128 del element['foo']
129 self.assertRaises(KeyError, element.__getitem__, 'foo')
131 def test_default_attributes(self):
132 element = nodes.Element()
133 self.assertEqual(element['ids'], [])
134 self.assertEqual(element.non_default_attributes(), {})
135 self.assertTrue(not element.is_not_default('ids'))
136 self.assertTrue(element['ids'] is not nodes.Element()['ids'])
137 element['ids'].append('someid')
138 self.assertEqual(element['ids'], ['someid'])
139 self.assertEqual(element.non_default_attributes(),
140 {'ids': ['someid']})
141 self.assertTrue(element.is_not_default('ids'))
143 def test_update_basic_atts(self):
144 element1 = nodes.Element(ids=['foo', 'bar'], test=['test1'])
145 element2 = nodes.Element(ids=['baz', 'qux'], test=['test2'])
146 element1.update_basic_atts(element2)
147 # 'ids' are appended because 'ids' is a basic attribute.
148 self.assertEqual(element1['ids'], ['foo', 'bar', 'baz', 'qux'])
149 # 'test' is not overwritten because it is not a basic attribute.
150 self.assertEqual(element1['test'], ['test1'])
152 def test_replace_self(self):
153 parent = nodes.Element(ids=['parent'])
154 child1 = nodes.Element(ids=['child1'])
155 grandchild = nodes.Element(ids=['grandchild'])
156 child1 += grandchild
157 child2 = nodes.Element(ids=['child2'])
158 twins = [nodes.Element(ids=['twin%s' % i]) for i in (1, 2)]
159 child2 += twins
160 child3 = nodes.Element(ids=['child3'])
161 child4 = nodes.Element(ids=['child4'])
162 parent += [child1, child2, child3, child4]
163 self.assertEqual(parent.pformat(), """\
164 <Element ids="parent">
165 <Element ids="child1">
166 <Element ids="grandchild">
167 <Element ids="child2">
168 <Element ids="twin1">
169 <Element ids="twin2">
170 <Element ids="child3">
171 <Element ids="child4">
172 """)
173 # Replace child1 with the grandchild.
174 child1.replace_self(child1[0])
175 self.assertEqual(parent[0], grandchild)
176 # Assert that 'ids' have been updated.
177 self.assertEqual(grandchild['ids'], ['grandchild', 'child1'])
178 # Replace child2 with its children.
179 child2.replace_self(child2[:])
180 self.assertEqual(parent[1:3], twins)
181 # Assert that 'ids' have been propagated to first child.
182 self.assertEqual(twins[0]['ids'], ['twin1', 'child2'])
183 self.assertEqual(twins[1]['ids'], ['twin2'])
184 # Replace child3 with new child.
185 newchild = nodes.Element(ids=['newchild'])
186 child3.replace_self(newchild)
187 self.assertEqual(parent[3], newchild)
188 self.assertEqual(newchild['ids'], ['newchild', 'child3'])
189 # Crazy but possible case: Substitute child4 for itself.
190 child4.replace_self(child4)
191 # Make sure the 'child4' ID hasn't been duplicated.
192 self.assertEqual(child4['ids'], ['child4'])
193 self.assertEqual(len(parent), 5)
195 def test_unicode(self):
196 node = nodes.Element(u'Möhren', nodes.Text(u'Möhren', u'Möhren'))
197 self.assertEqual(unicode(node), u'<Element>Möhren</Element>')
200 class MiscTests(unittest.TestCase):
202 def test_node_class_names(self):
203 node_class_names = []
204 for x in dir(nodes):
205 c = getattr(nodes, x)
206 if isinstance(c, (type, types.ClassType)) and \
207 issubclass(c, nodes.Node) and len(c.__bases__) > 1:
208 node_class_names.append(x)
209 node_class_names.sort()
210 nodes.node_class_names.sort()
211 self.assertEqual(node_class_names, nodes.node_class_names)
213 ids = [(u'a', 'a'), ('A', 'a'), ('', ''), ('a b \n c', 'a-b-c'),
214 ('a.b.c', 'a-b-c'), (' - a - b - c - ', 'a-b-c'), (' - ', ''),
215 (u'\u2020\u2066', ''), (u'a \xa7 b \u2020 c', 'a-b-c'),
216 ('1', ''), ('1abc', 'abc'),
218 ids_unicode_all = [
219 (u'\u00f8 o with stroke', 'o-o-with-stroke'),
220 (u'\u0111 d with stroke', 'd-d-with-stroke'),
221 (u'\u0127 h with stroke', 'h-h-with-stroke'),
222 (u'\u0131 dotless i', 'i-dotless-i'),
223 (u'\u0142 l with stroke', 'l-l-with-stroke'),
224 (u'\u0167 t with stroke', 't-t-with-stroke'),
225 # From Latin Extended-B
226 (u'\u0180 b with stroke', 'b-b-with-stroke'),
227 (u'\u0183 b with topbar', 'b-b-with-topbar'),
228 (u'\u0188 c with hook', 'c-c-with-hook'),
229 (u'\u018c d with topbar', 'd-d-with-topbar'),
230 (u'\u0192 f with hook', 'f-f-with-hook'),
231 (u'\u0199 k with hook', 'k-k-with-hook'),
232 (u'\u019a l with bar', 'l-l-with-bar'),
233 (u'\u019e n with long right leg', 'n-n-with-long-right-leg'),
234 (u'\u01a5 p with hook', 'p-p-with-hook'),
235 (u'\u01ab t with palatal hook', 't-t-with-palatal-hook'),
236 (u'\u01ad t with hook', 't-t-with-hook'),
237 (u'\u01b4 y with hook', 'y-y-with-hook'),
238 (u'\u01b6 z with stroke', 'z-z-with-stroke'),
239 (u'\u01e5 g with stroke', 'g-g-with-stroke'),
240 (u'\u0225 z with hook', 'z-z-with-hook'),
241 (u'\u0234 l with curl', 'l-l-with-curl'),
242 (u'\u0235 n with curl', 'n-n-with-curl'),
243 (u'\u0236 t with curl', 't-t-with-curl'),
244 (u'\u0237 dotless j', 'j-dotless-j'),
245 (u'\u023c c with stroke', 'c-c-with-stroke'),
246 (u'\u023f s with swash tail', 's-s-with-swash-tail'),
247 (u'\u0240 z with swash tail', 'z-z-with-swash-tail'),
248 (u'\u0247 e with stroke', 'e-e-with-stroke'),
249 (u'\u0249 j with stroke', 'j-j-with-stroke'),
250 (u'\u024b q with hook tail', 'q-q-with-hook-tail'),
251 (u'\u024d r with stroke', 'r-r-with-stroke'),
252 (u'\u024f y with stroke', 'y-y-with-stroke'),
254 ids_unicode_not_2_2 = [
255 # From Latin-1 Supplements
256 (u'\u00e0: a with grave', 'a-a-with-grave'),
257 (u'\u00e1 a with acute', 'a-a-with-acute'),
258 (u'\u00e2 a with circumflex', 'a-a-with-circumflex'),
259 (u'\u00e3 a with tilde', 'a-a-with-tilde'),
260 (u'\u00e4 a with diaeresis', 'a-a-with-diaeresis'),
261 (u'\u00e5 a with ring above', 'a-a-with-ring-above'),
262 (u'\u00e7 c with cedilla', 'c-c-with-cedilla'),
263 (u'\u00e8 e with grave', 'e-e-with-grave'),
264 (u'\u00e9 e with acute', 'e-e-with-acute'),
265 (u'\u00ea e with circumflex', 'e-e-with-circumflex'),
266 (u'\u00eb e with diaeresis', 'e-e-with-diaeresis'),
267 (u'\u00ec i with grave', 'i-i-with-grave'),
268 (u'\u00ed i with acute', 'i-i-with-acute'),
269 (u'\u00ee i with circumflex', 'i-i-with-circumflex'),
270 (u'\u00ef i with diaeresis', 'i-i-with-diaeresis'),
271 (u'\u00f1 n with tilde', 'n-n-with-tilde'),
272 (u'\u00f2 o with grave', 'o-o-with-grave'),
273 (u'\u00f3 o with acute', 'o-o-with-acute'),
274 (u'\u00f4 o with circumflex', 'o-o-with-circumflex'),
275 (u'\u00f5 o with tilde', 'o-o-with-tilde'),
276 (u'\u00f6 o with diaeresis', 'o-o-with-diaeresis'),
277 (u'\u00f9 u with grave', 'u-u-with-grave'),
278 (u'\u00fa u with acute', 'u-u-with-acute'),
279 (u'\u00fb u with circumflex', 'u-u-with-circumflex'),
280 (u'\u00fc u with diaeresis', 'u-u-with-diaeresis'),
281 (u'\u00fd y with acute', 'y-y-with-acute'),
282 (u'\u00ff y with diaeresis', 'y-y-with-diaeresis'),
283 # From Latin Extended-A
284 (u'\u0101 a with macron', 'a-a-with-macron'),
285 (u'\u0103 a with breve', 'a-a-with-breve'),
286 (u'\u0105 a with ogonek', 'a-a-with-ogonek'),
287 (u'\u0107 c with acute', 'c-c-with-acute'),
288 (u'\u0109 c with circumflex', 'c-c-with-circumflex'),
289 (u'\u010b c with dot above', 'c-c-with-dot-above'),
290 (u'\u010d c with caron', 'c-c-with-caron'),
291 (u'\u010f d with caron', 'd-d-with-caron'),
292 (u'\u0113 e with macron', 'e-e-with-macron'),
293 (u'\u0115 e with breve', 'e-e-with-breve'),
294 (u'\u0117 e with dot above', 'e-e-with-dot-above'),
295 (u'\u0119 e with ogonek', 'e-e-with-ogonek'),
296 (u'\u011b e with caron', 'e-e-with-caron'),
297 (u'\u011d g with circumflex', 'g-g-with-circumflex'),
298 (u'\u011f g with breve', 'g-g-with-breve'),
299 (u'\u0121 g with dot above', 'g-g-with-dot-above'),
300 (u'\u0123 g with cedilla', 'g-g-with-cedilla'),
301 (u'\u0125 h with circumflex', 'h-h-with-circumflex'),
302 (u'\u0129 i with tilde', 'i-i-with-tilde'),
303 (u'\u012b i with macron', 'i-i-with-macron'),
304 (u'\u012d i with breve', 'i-i-with-breve'),
305 (u'\u012f i with ogonek', 'i-i-with-ogonek'),
306 (u'\u0133 ligature ij', 'ij-ligature-ij'),
307 (u'\u0135 j with circumflex', 'j-j-with-circumflex'),
308 (u'\u0137 k with cedilla', 'k-k-with-cedilla'),
309 (u'\u013a l with acute', 'l-l-with-acute'),
310 (u'\u013c l with cedilla', 'l-l-with-cedilla'),
311 (u'\u013e l with caron', 'l-l-with-caron'),
312 (u'\u0140 l with middle dot', 'l-l-with-middle-dot'),
313 (u'\u0144 n with acute', 'n-n-with-acute'),
314 (u'\u0146 n with cedilla', 'n-n-with-cedilla'),
315 (u'\u0148 n with caron', 'n-n-with-caron'),
316 (u'\u014d o with macron', 'o-o-with-macron'),
317 (u'\u014f o with breve', 'o-o-with-breve'),
318 (u'\u0151 o with double acute', 'o-o-with-double-acute'),
319 (u'\u0155 r with acute', 'r-r-with-acute'),
320 (u'\u0157 r with cedilla', 'r-r-with-cedilla'),
321 (u'\u0159 r with caron', 'r-r-with-caron'),
322 (u'\u015b s with acute', 's-s-with-acute'),
323 (u'\u015d s with circumflex', 's-s-with-circumflex'),
324 (u'\u015f s with cedilla', 's-s-with-cedilla'),
325 (u'\u0161 s with caron', 's-s-with-caron'),
326 (u'\u0163 t with cedilla', 't-t-with-cedilla'),
327 (u'\u0165 t with caron', 't-t-with-caron'),
328 (u'\u0169 u with tilde', 'u-u-with-tilde'),
329 (u'\u016b u with macron', 'u-u-with-macron'),
330 (u'\u016d u with breve', 'u-u-with-breve'),
331 (u'\u016f u with ring above', 'u-u-with-ring-above'),
332 (u'\u0171 u with double acute', 'u-u-with-double-acute'),
333 (u'\u0173 u with ogonek', 'u-u-with-ogonek'),
334 (u'\u0175 w with circumflex', 'w-w-with-circumflex'),
335 (u'\u0177 y with circumflex', 'y-y-with-circumflex'),
336 (u'\u017a z with acute', 'z-z-with-acute'),
337 (u'\u017c z with dot above', 'z-z-with-dot-above'),
338 (u'\u017e z with caron', 'z-z-with-caron'),
339 # From Latin Extended-B
340 (u'\u01a1 o with horn', 'o-o-with-horn'),
341 (u'\u01b0 u with horn', 'u-u-with-horn'),
342 (u'\u01c6 dz with caron', 'dz-dz-with-caron'),
343 (u'\u01c9 lj', 'lj-lj'),
344 (u'\u01cc nj', 'nj-nj'),
345 (u'\u01ce a with caron', 'a-a-with-caron'),
346 (u'\u01d0 i with caron', 'i-i-with-caron'),
347 (u'\u01d2 o with caron', 'o-o-with-caron'),
348 (u'\u01d4 u with caron', 'u-u-with-caron'),
349 (u'\u01e7 g with caron', 'g-g-with-caron'),
350 (u'\u01e9 k with caron', 'k-k-with-caron'),
351 (u'\u01eb o with ogonek', 'o-o-with-ogonek'),
352 (u'\u01ed o with ogonek and macron', 'o-o-with-ogonek-and-macron'),
353 (u'\u01f0 j with caron', 'j-j-with-caron'),
354 (u'\u01f3 dz', 'dz-dz'),
355 (u'\u01f5 g with acute', 'g-g-with-acute'),
356 (u'\u01f9 n with grave', 'n-n-with-grave'),
357 (u'\u0201 a with double grave', 'a-a-with-double-grave'),
358 (u'\u0203 a with inverted breve', 'a-a-with-inverted-breve'),
359 (u'\u0205 e with double grave', 'e-e-with-double-grave'),
360 (u'\u0207 e with inverted breve', 'e-e-with-inverted-breve'),
361 (u'\u0209 i with double grave', 'i-i-with-double-grave'),
362 (u'\u020b i with inverted breve', 'i-i-with-inverted-breve'),
363 (u'\u020d o with double grave', 'o-o-with-double-grave'),
364 (u'\u020f o with inverted breve', 'o-o-with-inverted-breve'),
365 (u'\u0211 r with double grave', 'r-r-with-double-grave'),
366 (u'\u0213 r with inverted breve', 'r-r-with-inverted-breve'),
367 (u'\u0215 u with double grave', 'u-u-with-double-grave'),
368 (u'\u0217 u with inverted breve', 'u-u-with-inverted-breve'),
369 (u'\u0219 s with comma below', 's-s-with-comma-below'),
370 (u'\u021b t with comma below', 't-t-with-comma-below'),
371 (u'\u021f h with caron', 'h-h-with-caron'),
372 (u'\u0227 a with dot above', 'a-a-with-dot-above'),
373 (u'\u0229 e with cedilla', 'e-e-with-cedilla'),
374 (u'\u022f o with dot above', 'o-o-with-dot-above'),
375 (u'\u0233 y with macron', 'y-y-with-macron'),
376 # digraphs From Latin-1 Supplements
377 (u'\u00df: ligature sz', 'sz-ligature-sz'),
378 (u'\u00e6 ae', 'ae-ae'),
379 (u'\u0153 ligature oe', 'oe-ligature-oe'),
380 (u'\u0238 db digraph', 'db-db-digraph'),
381 (u'\u0239 qp digraph', 'qp-qp-digraph'),
384 def test_make_id(self):
385 failures = []
386 tests = self.ids + self.ids_unicode_all
387 import sys
388 if sys.version_info[:2] != (2, 2):
389 tests += self.ids_unicode_not_2_2
390 for input, expect in tests:
391 output = nodes.make_id(input)
392 if expect != output:
393 failures.append("'%s' != '%s'" % (expect, output))
394 if failures:
395 self.fail("%d failures in %d\n%s" % (len(failures), len(self.ids), "\n".join(failures)))
397 def test_traverse(self):
398 e = nodes.Element()
399 e += nodes.Element()
400 e[0] += nodes.Element()
401 e[0] += nodes.TextElement()
402 e[0][1] += nodes.Text('some text')
403 e += nodes.Element()
404 e += nodes.Element()
405 self.assertEqual(list(e.traverse()),
406 [e, e[0], e[0][0], e[0][1], e[0][1][0], e[1], e[2]])
407 self.assertEqual(list(e.traverse(include_self=False)),
408 [e[0], e[0][0], e[0][1], e[0][1][0], e[1], e[2]])
409 self.assertEqual(list(e.traverse(descend=False)),
410 [e])
411 self.assertEqual(list(e[0].traverse(descend=False, ascend=True)),
412 [e[0], e[1], e[2]])
413 self.assertEqual(list(e[0][0].traverse(descend=False, ascend=True)),
414 [e[0][0], e[0][1], e[1], e[2]])
415 self.assertEqual(list(e[0][0].traverse(descend=False, siblings=True)),
416 [e[0][0], e[0][1]])
417 self.testlist = e[0:2]
418 self.assertEqual(list(e.traverse(condition=self.not_in_testlist)),
419 [e, e[0][0], e[0][1], e[0][1][0], e[2]])
420 # Return siblings despite siblings=False because ascend is true.
421 self.assertEqual(list(e[1].traverse(ascend=True, siblings=False)),
422 [e[1], e[2]])
423 self.assertEqual(list(e[0].traverse()),
424 [e[0], e[0][0], e[0][1], e[0][1][0]])
425 self.testlist = [e[0][0], e[0][1]]
426 self.assertEqual(list(e[0].traverse(condition=self.not_in_testlist)),
427 [e[0], e[0][1][0]])
428 self.testlist.append(e[0][1][0])
429 self.assertEqual(list(e[0].traverse(condition=self.not_in_testlist)),
430 [e[0]])
431 self.assertEqual(list(e.traverse(nodes.TextElement)), [e[0][1]])
433 def test_next_node(self):
434 e = nodes.Element()
435 e += nodes.Element()
436 e[0] += nodes.Element()
437 e[0] += nodes.TextElement()
438 e[0][1] += nodes.Text('some text')
439 e += nodes.Element()
440 e += nodes.Element()
441 self.testlist = [e[0], e[0][1], e[1]]
442 compare = [(e, e[0][0]),
443 (e[0], e[0][0]),
444 (e[0][0], e[0][1][0]),
445 (e[0][1], e[0][1][0]),
446 (e[0][1][0], e[2]),
447 (e[1], e[2]),
448 (e[2], None)]
449 for node, next_node in compare:
450 self.assertEqual(node.next_node(self.not_in_testlist, ascend=True),
451 next_node)
452 self.assertEqual(e[0][0].next_node(ascend=True), e[0][1])
453 self.assertEqual(e[2].next_node(), None)
455 def not_in_testlist(self, x):
456 return x not in self.testlist
458 def test_copy(self):
459 grandchild = nodes.Text('rawsource')
460 child = nodes.emphasis('rawsource', grandchild, att='child')
461 e = nodes.Element('rawsource', child, att='e')
462 # Shallow copy:
463 e_copy = e.copy()
464 self.assertTrue(e is not e_copy)
465 # Internal attributes (like `rawsource`) are also copied.
466 self.assertEqual(e.rawsource, 'rawsource')
467 self.assertEqual(e_copy.rawsource, e.rawsource)
468 self.assertEqual(e_copy['att'], 'e')
469 # Children are not copied.
470 self.assertEqual(len(e_copy), 0)
471 # Deep copy:
472 e_deepcopy = e.deepcopy()
473 self.assertEqual(e_deepcopy.rawsource, e.rawsource)
474 self.assertEqual(e_deepcopy['att'], 'e')
475 # Children are copied recursively.
476 self.assertEqual(e_deepcopy[0][0], grandchild)
477 self.assertTrue(e_deepcopy[0][0] is not grandchild)
478 self.assertEqual(e_deepcopy[0]['att'], 'child')
481 class TreeCopyVisitorTests(unittest.TestCase):
483 def setUp(self):
484 document = utils.new_document('test data')
485 document += nodes.paragraph('', 'Paragraph 1.')
486 blist = nodes.bullet_list()
487 for i in range(1, 6):
488 item = nodes.list_item()
489 for j in range(1, 4):
490 item += nodes.paragraph('', 'Item %s, paragraph %s.' % (i, j))
491 blist += item
492 document += blist
493 self.document = document
495 def compare_trees(self, one, two):
496 self.assertEqual(one.__class__, two.__class__)
497 self.assertNotEquals(id(one), id(two))
498 self.assertEqual(len(one.children), len(two.children))
499 for i in range(len(one.children)):
500 self.compare_trees(one.children[i], two.children[i])
502 def test_copy_whole(self):
503 visitor = nodes.TreeCopyVisitor(self.document)
504 self.document.walkabout(visitor)
505 newtree = visitor.get_tree_copy()
506 self.assertEqual(self.document.pformat(), newtree.pformat())
507 self.compare_trees(self.document, newtree)
510 class MiscFunctionTests(unittest.TestCase):
512 names = [('a', 'a'), ('A', 'a'), ('A a A', 'a a a'),
513 ('A a A a', 'a a a a'),
514 (' AaA\n\r\naAa\tAaA\t\t', 'aaa aaa aaa')]
516 def test_normalize_name(self):
517 for input, output in self.names:
518 normed = nodes.fully_normalize_name(input)
519 self.assertEqual(normed, output)
521 def test_set_id_default(self):
522 # Default prefixes.
523 document = utils.new_document('test')
524 # From name.
525 element = nodes.Element(names=['test'])
526 document.set_id(element)
527 self.assertEqual(element['ids'], ['test'])
528 # Auto-generated.
529 element = nodes.Element()
530 document.set_id(element)
531 self.assertEqual(element['ids'], ['id1'])
533 def test_set_id_custom(self):
534 # Custom prefixes.
535 document = utils.new_document('test')
536 # Change settings.
537 document.settings.id_prefix = 'prefix'
538 document.settings.auto_id_prefix = 'auto'
539 # From name.
540 element = nodes.Element(names=['test'])
541 document.set_id(element)
542 self.assertEqual(element['ids'], ['prefixtest'])
543 # Auto-generated.
544 element = nodes.Element()
545 document.set_id(element)
546 self.assertEqual(element['ids'], ['prefixauto1'])
549 if __name__ == '__main__':
550 unittest.main()