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