* Consolidated version numbering documentation in docs/dev/policies.txt (moved text...
[docutils.git] / docutils / test / test_nodes.py
blobda5b93e2eab9871a2c35c5c3d1f6e9d0db32f9aa
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
18 debug = False
21 class TextTests(unittest.TestCase):
23 def setUp(self):
24 self.text = nodes.Text('Line 1.\nLine 2.')
25 self.unicode_text = nodes.Text(u'Möhren')
26 self.longtext = nodes.Text('Mary had a little lamb whose '
27 'fleece was white as snow and '
28 'everwhere that Mary went the '
29 'lamb was sure to go.')
31 def test_repr(self):
32 self.assertEqual(repr(self.text), r"<#text: 'Line 1.\nLine 2.'>")
33 self.assertEqual(self.text.shortrepr(),
34 r"<#text: 'Line 1.\nLine 2.'>")
35 self.assertEqual(nodes.reprunicode('foo'), u'foo')
36 if sys.version_info < (3,):
37 self.assertEqual(repr(self.unicode_text), r"<#text: 'M\xf6hren'>")
38 else:
39 self.assertEqual(repr(self.unicode_text), u"<#text: 'Möhren'>")
41 def test_str(self):
42 self.assertEqual(str(self.text), 'Line 1.\nLine 2.')
44 def test_unicode(self):
45 self.assertEqual(unicode(self.unicode_text), u'Möhren')
46 self.assertEqual(str(self.unicode_text), 'M\xf6hren')
48 def test_astext(self):
49 self.assertTrue(isinstance(self.text.astext(), unicode))
50 self.assertEqual(self.text.astext(), u'Line 1.\nLine 2.')
51 self.assertEqual(self.unicode_text.astext(), u'Möhren')
53 def test_pformat(self):
54 self.assertTrue(isinstance(self.text.pformat(), unicode))
55 self.assertEqual(self.text.pformat(), u'Line 1.\nLine 2.\n')
57 def test_asciirestriction(self):
58 if sys.version_info < (3,):
59 self.assertRaises(UnicodeDecodeError, nodes.Text,
60 b'hol%s' % chr(224))
61 else:
62 # no bytes at all allowed
63 self.assertRaises(TypeError, nodes.Text, b'hol')
65 def test_longrepr(self):
66 self.assertEqual(repr(self.longtext), r"<#text: 'Mary had a "
67 r"little lamb whose fleece was white as snow "
68 r"and everwh ...'>")
69 self.assertEqual(self.longtext.shortrepr(),
70 r"<#text: 'Mary had a lit ...'>")
72 class ElementTests(unittest.TestCase):
74 def test_empty(self):
75 element = nodes.Element()
76 self.assertEqual(repr(element), '<Element: >')
77 self.assertEqual(str(element), '<Element/>')
78 dom = element.asdom()
79 self.assertEqual(dom.toxml(), '<Element/>')
80 dom.unlink()
81 element['attr'] = '1'
82 self.assertEqual(repr(element), '<Element: >')
83 self.assertEqual(str(element), '<Element attr="1"/>')
84 dom = element.asdom()
85 self.assertEqual(dom.toxml(), '<Element attr="1"/>')
86 dom.unlink()
87 self.assertEqual(element.pformat(), '<Element attr="1">\n')
88 del element['attr']
89 element['mark'] = u'\u2022'
90 self.assertEqual(repr(element), '<Element: >')
91 if sys.version_info < (3,):
92 self.assertEqual(str(element), '<Element mark="\\u2022"/>')
93 else:
94 self.assertEqual(str(element), u'<Element mark="\u2022"/>')
95 dom = element.asdom()
96 self.assertEqual(dom.toxml(), u'<Element mark="\u2022"/>')
97 dom.unlink()
98 element['names'] = ['nobody', u'имя', u'näs']
99 if sys.version_info < (3,):
100 self.assertEqual(repr(element),
101 '<Element "nobody; \\u0438\\u043c\\u044f; n\\xe4s": >')
102 else:
103 self.assertEqual(repr(element), u'<Element "nobody; имя; näs": >')
104 self.assertTrue(isinstance(repr(element), str))
106 def test_withtext(self):
107 element = nodes.Element('text\nmore', nodes.Text('text\nmore'))
108 uelement = nodes.Element(u'grün', nodes.Text(u'grün'))
109 self.assertEqual(repr(element), r"<Element: <#text: 'text\nmore'>>")
110 if sys.version_info < (3,):
111 self.assertEqual(repr(uelement), "<Element: <#text: 'gr\\xfcn'>>")
112 else:
113 self.assertEqual(repr(uelement), u"<Element: <#text: 'grün'>>")
114 self.assertTrue(isinstance(repr(uelement),str))
115 self.assertEqual(str(element), '<Element>text\nmore</Element>')
116 self.assertEqual(str(uelement), '<Element>gr\xfcn</Element>')
117 dom = element.asdom()
118 self.assertEqual(dom.toxml(), '<Element>text\nmore</Element>')
119 dom.unlink()
120 element['attr'] = '1'
121 self.assertEqual(repr(element), r"<Element: <#text: 'text\nmore'>>")
122 self.assertEqual(str(element),
123 '<Element attr="1">text\nmore</Element>')
124 dom = element.asdom()
125 self.assertEqual(dom.toxml(),
126 '<Element attr="1">text\nmore</Element>')
127 dom.unlink()
128 self.assertEqual(element.pformat(),
129 '<Element attr="1">\n text\n more\n')
131 def test_clear(self):
132 element = nodes.Element()
133 element += nodes.Element()
134 self.assertTrue(len(element))
135 element.clear()
136 self.assertTrue(not len(element))
138 def test_normal_attributes(self):
139 element = nodes.Element()
140 self.assertTrue('foo' not in element)
141 self.assertRaises(KeyError, element.__getitem__, 'foo')
142 element['foo'] = 'sometext'
143 self.assertEqual(element['foo'], 'sometext')
144 del element['foo']
145 self.assertRaises(KeyError, element.__getitem__, 'foo')
147 def test_default_attributes(self):
148 element = nodes.Element()
149 self.assertEqual(element['ids'], [])
150 self.assertEqual(element.non_default_attributes(), {})
151 self.assertTrue(not element.is_not_default('ids'))
152 self.assertTrue(element['ids'] is not nodes.Element()['ids'])
153 element['ids'].append('someid')
154 self.assertEqual(element['ids'], ['someid'])
155 self.assertEqual(element.non_default_attributes(),
156 {'ids': ['someid']})
157 self.assertTrue(element.is_not_default('ids'))
159 def test_update_basic_atts(self):
160 element1 = nodes.Element(ids=['foo', 'bar'], test=['test1'])
161 element2 = nodes.Element(ids=['baz', 'qux'], test=['test2'])
162 element1.update_basic_atts(element2)
163 # 'ids' are appended because 'ids' is a basic attribute.
164 self.assertEqual(element1['ids'], ['foo', 'bar', 'baz', 'qux'])
165 # 'test' is not overwritten because it is not a basic attribute.
166 self.assertEqual(element1['test'], ['test1'])
168 def test_update_all_atts(self):
169 # Note: Also tests is_not_list_attribute and is_not_known_attribute
170 # and various helpers
171 ## Test for full attribute replacement
172 element1 = nodes.Element(ids=['foo', 'bar'], parent_only='parent',
173 all_nodes='mom')
174 element2 = nodes.Element(ids=['baz', 'qux'], child_only='child',
175 all_nodes='dad', source='source')
177 # Test for when same fields are replaced as well as source...
178 element1.update_all_atts_consistantly(element2, True, True)
179 # 'ids' are appended because 'ids' is a basic attribute.
180 self.assertEquals(element1['ids'], ['foo', 'bar', 'baz', 'qux'])
181 # 'parent_only' should remain unaffected.
182 self.assertEquals(element1['parent_only'], 'parent')
183 # 'all_nodes' is overwritten due to the second parameter == True.
184 self.assertEquals(element1['all_nodes'], 'dad')
185 # 'child_only' should have been added.
186 self.assertEquals(element1['child_only'], 'child')
187 # 'source' is also overwritten due to the third parameter == True.
188 self.assertEquals(element1['source'], 'source')
190 # Test for when same fields are replaced but not source...
191 element1 = nodes.Element(ids=['foo', 'bar'], parent_only='parent',
192 all_nodes='mom')
193 element1.update_all_atts_consistantly(element2)
194 # 'ids' are appended because 'ids' is a basic attribute.
195 self.assertEquals(element1['ids'], ['foo', 'bar', 'baz', 'qux'])
196 # 'parent_only' should remain unaffected.
197 self.assertEquals(element1['parent_only'], 'parent')
198 # 'all_nodes' is overwritten due to the second parameter default of True.
199 self.assertEquals(element1['all_nodes'], 'dad')
200 # 'child_only' should have been added.
201 self.assertEquals(element1['child_only'], 'child')
202 # 'source' remains unset due to the third parameter default of False.
203 self.assertEquals(element1.get('source'), None)
205 # Test for when fields are NOT replaced but source is...
206 element1 = nodes.Element(ids=['foo', 'bar'], parent_only='parent',
207 all_nodes='mom')
208 element1.update_all_atts_consistantly(element2, False, True)
209 # 'ids' are appended because 'ids' is a basic attribute.
210 self.assertEquals(element1['ids'], ['foo', 'bar', 'baz', 'qux'])
211 # 'parent_only' should remain unaffected.
212 self.assertEquals(element1['parent_only'], 'parent')
213 # 'all_nodes' is preserved due to the second parameter == False.
214 self.assertEquals(element1['all_nodes'], 'mom')
215 # 'child_only' should have been added.
216 self.assertEquals(element1['child_only'], 'child')
217 # 'source' is added due to the third parameter == True.
218 self.assertEquals(element1['source'], 'source')
219 element1 = nodes.Element(source='destination')
220 element1.update_all_atts_consistantly(element2, False, True)
221 # 'source' remains unchanged due to the second parameter == False.
222 self.assertEquals(element1['source'], 'destination')
224 # Test for when same fields are replaced but not source...
225 element1 = nodes.Element(ids=['foo', 'bar'], parent_only='parent',
226 all_nodes='mom')
227 element1.update_all_atts_consistantly(element2, False)
228 # 'ids' are appended because 'ids' is a basic attribute.
229 self.assertEquals(element1['ids'], ['foo', 'bar', 'baz', 'qux'])
230 # 'parent_only' should remain unaffected.
231 self.assertEquals(element1['parent_only'], 'parent')
232 # 'all_nodes' is preserved due to the second parameter == False.
233 self.assertEquals(element1['all_nodes'], 'mom')
234 # 'child_only' should have been added.
235 self.assertEquals(element1['child_only'], 'child')
236 # 'source' remains unset due to the third parameter default of False.
237 self.assertEquals(element1.get('source'), None)
239 ## Test for List attribute merging
240 # Attribute Concatination
241 element1 = nodes.Element(ss='a', sl='1', ls=['I'], ll=['A'])
242 element2 = nodes.Element(ss='b', sl=['2'], ls='II', ll=['B'])
243 element1.update_all_atts_concatenating(element2)
244 # 'ss' is replaced because non-list
245 self.assertEquals(element1['ss'], 'b')
246 # 'sl' is replaced because they are both not lists
247 self.assertEquals(element1['sl'], ['2'])
248 # 'ls' is replaced because they are both not lists
249 self.assertEquals(element1['ls'], 'II')
250 # 'll' is extended because they are both lists
251 self.assertEquals(element1['ll'], ['A', 'B'])
253 # Attribute Coercion
254 element1 = nodes.Element(ss='a', sl='1', ls=['I'], ll=['A'])
255 element2 = nodes.Element(ss='b', sl=['2'], ls='II', ll=['B'])
256 element1.update_all_atts_coercion(element2)
257 # 'ss' is replaced because non-list
258 self.assertEquals(element1['ss'], 'b')
259 # 'sl' is converted to a list and appended because element2 has a list
260 self.assertEquals(element1['sl'], ['1', '2'])
261 # 'ls' has element2's value appended to the list
262 self.assertEquals(element1['ls'], ['I', 'II'])
263 # 'll' is extended because they are both lists
264 self.assertEquals(element1['ll'], ['A', 'B'])
266 # Attribute Conversion
267 element1 = nodes.Element(ss='a', sl='1', ls=['I'], ll=['A'])
268 element2 = nodes.Element(ss='b', sl=['2'], ls='II', ll=['B'])
269 element1.update_all_atts_convert(element2)
270 # 'ss' is converted to a list with the values from each element
271 self.assertEquals(element1['ss'], ['a', 'b'])
272 # 'sl' is converted to a list and appended
273 self.assertEquals(element1['sl'], ['1', '2'])
274 # 'ls' has element2's value appended to the list
275 self.assertEquals(element1['ls'], ['I', 'II'])
276 # 'll' is extended
277 self.assertEquals(element1['ll'], ['A', 'B'])
279 def test_replace_self(self):
280 parent = nodes.Element(ids=['parent'])
281 child1 = nodes.Element(ids=['child1'])
282 grandchild = nodes.Element(ids=['grandchild'])
283 child1 += grandchild
284 child2 = nodes.Element(ids=['child2'])
285 twins = [nodes.Element(ids=['twin%s' % i]) for i in (1, 2)]
286 child2 += twins
287 child3 = nodes.Element(ids=['child3'])
288 child4 = nodes.Element(ids=['child4'])
289 parent += [child1, child2, child3, child4]
290 self.assertEqual(parent.pformat(), """\
291 <Element ids="parent">
292 <Element ids="child1">
293 <Element ids="grandchild">
294 <Element ids="child2">
295 <Element ids="twin1">
296 <Element ids="twin2">
297 <Element ids="child3">
298 <Element ids="child4">
299 """)
300 # Replace child1 with the grandchild.
301 child1.replace_self(child1[0])
302 self.assertEqual(parent[0], grandchild)
303 # Assert that 'ids' have been updated.
304 self.assertEqual(grandchild['ids'], ['grandchild', 'child1'])
305 # Replace child2 with its children.
306 child2.replace_self(child2[:])
307 self.assertEqual(parent[1:3], twins)
308 # Assert that 'ids' have been propagated to first child.
309 self.assertEqual(twins[0]['ids'], ['twin1', 'child2'])
310 self.assertEqual(twins[1]['ids'], ['twin2'])
311 # Replace child3 with new child.
312 newchild = nodes.Element(ids=['newchild'])
313 child3.replace_self(newchild)
314 self.assertEqual(parent[3], newchild)
315 self.assertEqual(newchild['ids'], ['newchild', 'child3'])
316 # Crazy but possible case: Substitute child4 for itself.
317 child4.replace_self(child4)
318 # Make sure the 'child4' ID hasn't been duplicated.
319 self.assertEqual(child4['ids'], ['child4'])
320 self.assertEqual(len(parent), 5)
322 def test_unicode(self):
323 node = nodes.Element(u'Möhren', nodes.Text(u'Möhren', u'Möhren'))
324 self.assertEqual(unicode(node), u'<Element>Möhren</Element>')
327 class MiscTests(unittest.TestCase):
329 def test_reprunicode(self):
330 # return `unicode` instance
331 self.assertTrue(isinstance(nodes.reprunicode('foo'), unicode))
332 self.assertEqual(nodes.reprunicode('foo'), u'foo')
333 self.assertEqual(nodes.reprunicode(u'Möhre'), u'Möhre')
334 if sys.version_info < (3,): # strip leading "u" from representation
335 self.assertEqual(repr(nodes.reprunicode(u'Möhre')),
336 repr(u'Möhre')[1:])
337 else: # no change to `unicode` under Python 3k
338 self.assertEqual(repr(nodes.reprunicode(u'Möhre')), repr(u'Möhre'))
340 def test_ensure_str(self):
341 self.assertTrue(isinstance(nodes.ensure_str(u'über'), str))
342 self.assertEqual(nodes.ensure_str('over'), 'over')
343 if sys.version_info < (3,): # strip leading "u" from representation
344 self.assertEqual(nodes.ensure_str(u'über'), r'\xfcber')
345 else:
346 self.assertEqual(nodes.ensure_str(u'über'), r'über')
348 def test_node_class_names(self):
349 node_class_names = []
350 for x in dir(nodes):
351 c = getattr(nodes, x)
352 if isinstance(c, (type, types.ClassType)) and \
353 issubclass(c, nodes.Node) and len(c.__bases__) > 1:
354 node_class_names.append(x)
355 node_class_names.sort()
356 nodes.node_class_names.sort()
357 self.assertEqual(node_class_names, nodes.node_class_names)
359 ids = [(u'a', 'a'), ('A', 'a'), ('', ''), ('a b \n c', 'a-b-c'),
360 ('a.b.c', 'a-b-c'), (' - a - b - c - ', 'a-b-c'), (' - ', ''),
361 (u'\u2020\u2066', ''), (u'a \xa7 b \u2020 c', 'a-b-c'),
362 ('1', ''), ('1abc', 'abc'),
364 ids_unicode_all = [
365 (u'\u00f8 o with stroke', 'o-o-with-stroke'),
366 (u'\u0111 d with stroke', 'd-d-with-stroke'),
367 (u'\u0127 h with stroke', 'h-h-with-stroke'),
368 (u'\u0131 dotless i', 'i-dotless-i'),
369 (u'\u0142 l with stroke', 'l-l-with-stroke'),
370 (u'\u0167 t with stroke', 't-t-with-stroke'),
371 # From Latin Extended-B
372 (u'\u0180 b with stroke', 'b-b-with-stroke'),
373 (u'\u0183 b with topbar', 'b-b-with-topbar'),
374 (u'\u0188 c with hook', 'c-c-with-hook'),
375 (u'\u018c d with topbar', 'd-d-with-topbar'),
376 (u'\u0192 f with hook', 'f-f-with-hook'),
377 (u'\u0199 k with hook', 'k-k-with-hook'),
378 (u'\u019a l with bar', 'l-l-with-bar'),
379 (u'\u019e n with long right leg', 'n-n-with-long-right-leg'),
380 (u'\u01a5 p with hook', 'p-p-with-hook'),
381 (u'\u01ab t with palatal hook', 't-t-with-palatal-hook'),
382 (u'\u01ad t with hook', 't-t-with-hook'),
383 (u'\u01b4 y with hook', 'y-y-with-hook'),
384 (u'\u01b6 z with stroke', 'z-z-with-stroke'),
385 (u'\u01e5 g with stroke', 'g-g-with-stroke'),
386 (u'\u0225 z with hook', 'z-z-with-hook'),
387 (u'\u0234 l with curl', 'l-l-with-curl'),
388 (u'\u0235 n with curl', 'n-n-with-curl'),
389 (u'\u0236 t with curl', 't-t-with-curl'),
390 (u'\u0237 dotless j', 'j-dotless-j'),
391 (u'\u023c c with stroke', 'c-c-with-stroke'),
392 (u'\u023f s with swash tail', 's-s-with-swash-tail'),
393 (u'\u0240 z with swash tail', 'z-z-with-swash-tail'),
394 (u'\u0247 e with stroke', 'e-e-with-stroke'),
395 (u'\u0249 j with stroke', 'j-j-with-stroke'),
396 (u'\u024b q with hook tail', 'q-q-with-hook-tail'),
397 (u'\u024d r with stroke', 'r-r-with-stroke'),
398 (u'\u024f y with stroke', 'y-y-with-stroke'),
399 # From Latin-1 Supplements
400 (u'\u00e0: a with grave', 'a-a-with-grave'),
401 (u'\u00e1 a with acute', 'a-a-with-acute'),
402 (u'\u00e2 a with circumflex', 'a-a-with-circumflex'),
403 (u'\u00e3 a with tilde', 'a-a-with-tilde'),
404 (u'\u00e4 a with diaeresis', 'a-a-with-diaeresis'),
405 (u'\u00e5 a with ring above', 'a-a-with-ring-above'),
406 (u'\u00e7 c with cedilla', 'c-c-with-cedilla'),
407 (u'\u00e8 e with grave', 'e-e-with-grave'),
408 (u'\u00e9 e with acute', 'e-e-with-acute'),
409 (u'\u00ea e with circumflex', 'e-e-with-circumflex'),
410 (u'\u00eb e with diaeresis', 'e-e-with-diaeresis'),
411 (u'\u00ec i with grave', 'i-i-with-grave'),
412 (u'\u00ed i with acute', 'i-i-with-acute'),
413 (u'\u00ee i with circumflex', 'i-i-with-circumflex'),
414 (u'\u00ef i with diaeresis', 'i-i-with-diaeresis'),
415 (u'\u00f1 n with tilde', 'n-n-with-tilde'),
416 (u'\u00f2 o with grave', 'o-o-with-grave'),
417 (u'\u00f3 o with acute', 'o-o-with-acute'),
418 (u'\u00f4 o with circumflex', 'o-o-with-circumflex'),
419 (u'\u00f5 o with tilde', 'o-o-with-tilde'),
420 (u'\u00f6 o with diaeresis', 'o-o-with-diaeresis'),
421 (u'\u00f9 u with grave', 'u-u-with-grave'),
422 (u'\u00fa u with acute', 'u-u-with-acute'),
423 (u'\u00fb u with circumflex', 'u-u-with-circumflex'),
424 (u'\u00fc u with diaeresis', 'u-u-with-diaeresis'),
425 (u'\u00fd y with acute', 'y-y-with-acute'),
426 (u'\u00ff y with diaeresis', 'y-y-with-diaeresis'),
427 # From Latin Extended-A
428 (u'\u0101 a with macron', 'a-a-with-macron'),
429 (u'\u0103 a with breve', 'a-a-with-breve'),
430 (u'\u0105 a with ogonek', 'a-a-with-ogonek'),
431 (u'\u0107 c with acute', 'c-c-with-acute'),
432 (u'\u0109 c with circumflex', 'c-c-with-circumflex'),
433 (u'\u010b c with dot above', 'c-c-with-dot-above'),
434 (u'\u010d c with caron', 'c-c-with-caron'),
435 (u'\u010f d with caron', 'd-d-with-caron'),
436 (u'\u0113 e with macron', 'e-e-with-macron'),
437 (u'\u0115 e with breve', 'e-e-with-breve'),
438 (u'\u0117 e with dot above', 'e-e-with-dot-above'),
439 (u'\u0119 e with ogonek', 'e-e-with-ogonek'),
440 (u'\u011b e with caron', 'e-e-with-caron'),
441 (u'\u011d g with circumflex', 'g-g-with-circumflex'),
442 (u'\u011f g with breve', 'g-g-with-breve'),
443 (u'\u0121 g with dot above', 'g-g-with-dot-above'),
444 (u'\u0123 g with cedilla', 'g-g-with-cedilla'),
445 (u'\u0125 h with circumflex', 'h-h-with-circumflex'),
446 (u'\u0129 i with tilde', 'i-i-with-tilde'),
447 (u'\u012b i with macron', 'i-i-with-macron'),
448 (u'\u012d i with breve', 'i-i-with-breve'),
449 (u'\u012f i with ogonek', 'i-i-with-ogonek'),
450 (u'\u0133 ligature ij', 'ij-ligature-ij'),
451 (u'\u0135 j with circumflex', 'j-j-with-circumflex'),
452 (u'\u0137 k with cedilla', 'k-k-with-cedilla'),
453 (u'\u013a l with acute', 'l-l-with-acute'),
454 (u'\u013c l with cedilla', 'l-l-with-cedilla'),
455 (u'\u013e l with caron', 'l-l-with-caron'),
456 (u'\u0140 l with middle dot', 'l-l-with-middle-dot'),
457 (u'\u0144 n with acute', 'n-n-with-acute'),
458 (u'\u0146 n with cedilla', 'n-n-with-cedilla'),
459 (u'\u0148 n with caron', 'n-n-with-caron'),
460 (u'\u014d o with macron', 'o-o-with-macron'),
461 (u'\u014f o with breve', 'o-o-with-breve'),
462 (u'\u0151 o with double acute', 'o-o-with-double-acute'),
463 (u'\u0155 r with acute', 'r-r-with-acute'),
464 (u'\u0157 r with cedilla', 'r-r-with-cedilla'),
465 (u'\u0159 r with caron', 'r-r-with-caron'),
466 (u'\u015b s with acute', 's-s-with-acute'),
467 (u'\u015d s with circumflex', 's-s-with-circumflex'),
468 (u'\u015f s with cedilla', 's-s-with-cedilla'),
469 (u'\u0161 s with caron', 's-s-with-caron'),
470 (u'\u0163 t with cedilla', 't-t-with-cedilla'),
471 (u'\u0165 t with caron', 't-t-with-caron'),
472 (u'\u0169 u with tilde', 'u-u-with-tilde'),
473 (u'\u016b u with macron', 'u-u-with-macron'),
474 (u'\u016d u with breve', 'u-u-with-breve'),
475 (u'\u016f u with ring above', 'u-u-with-ring-above'),
476 (u'\u0171 u with double acute', 'u-u-with-double-acute'),
477 (u'\u0173 u with ogonek', 'u-u-with-ogonek'),
478 (u'\u0175 w with circumflex', 'w-w-with-circumflex'),
479 (u'\u0177 y with circumflex', 'y-y-with-circumflex'),
480 (u'\u017a z with acute', 'z-z-with-acute'),
481 (u'\u017c z with dot above', 'z-z-with-dot-above'),
482 (u'\u017e z with caron', 'z-z-with-caron'),
483 # From Latin Extended-B
484 (u'\u01a1 o with horn', 'o-o-with-horn'),
485 (u'\u01b0 u with horn', 'u-u-with-horn'),
486 (u'\u01c6 dz with caron', 'dz-dz-with-caron'),
487 (u'\u01c9 lj', 'lj-lj'),
488 (u'\u01cc nj', 'nj-nj'),
489 (u'\u01ce a with caron', 'a-a-with-caron'),
490 (u'\u01d0 i with caron', 'i-i-with-caron'),
491 (u'\u01d2 o with caron', 'o-o-with-caron'),
492 (u'\u01d4 u with caron', 'u-u-with-caron'),
493 (u'\u01e7 g with caron', 'g-g-with-caron'),
494 (u'\u01e9 k with caron', 'k-k-with-caron'),
495 (u'\u01eb o with ogonek', 'o-o-with-ogonek'),
496 (u'\u01ed o with ogonek and macron', 'o-o-with-ogonek-and-macron'),
497 (u'\u01f0 j with caron', 'j-j-with-caron'),
498 (u'\u01f3 dz', 'dz-dz'),
499 (u'\u01f5 g with acute', 'g-g-with-acute'),
500 (u'\u01f9 n with grave', 'n-n-with-grave'),
501 (u'\u0201 a with double grave', 'a-a-with-double-grave'),
502 (u'\u0203 a with inverted breve', 'a-a-with-inverted-breve'),
503 (u'\u0205 e with double grave', 'e-e-with-double-grave'),
504 (u'\u0207 e with inverted breve', 'e-e-with-inverted-breve'),
505 (u'\u0209 i with double grave', 'i-i-with-double-grave'),
506 (u'\u020b i with inverted breve', 'i-i-with-inverted-breve'),
507 (u'\u020d o with double grave', 'o-o-with-double-grave'),
508 (u'\u020f o with inverted breve', 'o-o-with-inverted-breve'),
509 (u'\u0211 r with double grave', 'r-r-with-double-grave'),
510 (u'\u0213 r with inverted breve', 'r-r-with-inverted-breve'),
511 (u'\u0215 u with double grave', 'u-u-with-double-grave'),
512 (u'\u0217 u with inverted breve', 'u-u-with-inverted-breve'),
513 (u'\u0219 s with comma below', 's-s-with-comma-below'),
514 (u'\u021b t with comma below', 't-t-with-comma-below'),
515 (u'\u021f h with caron', 'h-h-with-caron'),
516 (u'\u0227 a with dot above', 'a-a-with-dot-above'),
517 (u'\u0229 e with cedilla', 'e-e-with-cedilla'),
518 (u'\u022f o with dot above', 'o-o-with-dot-above'),
519 (u'\u0233 y with macron', 'y-y-with-macron'),
520 # digraphs From Latin-1 Supplements
521 (u'\u00df: ligature sz', 'sz-ligature-sz'),
522 (u'\u00e6 ae', 'ae-ae'),
523 (u'\u0153 ligature oe', 'oe-ligature-oe'),
524 (u'\u0238 db digraph', 'db-db-digraph'),
525 (u'\u0239 qp digraph', 'qp-qp-digraph'),
528 def test_make_id(self):
529 failures = []
530 tests = self.ids + self.ids_unicode_all
531 for input, expect in tests:
532 output = nodes.make_id(input)
533 if expect != output:
534 failures.append("'%s' != '%s'" % (expect, output))
535 if failures:
536 self.fail("%d failures in %d\n%s" % (len(failures), len(self.ids), "\n".join(failures)))
538 def test_traverse(self):
539 e = nodes.Element()
540 e += nodes.Element()
541 e[0] += nodes.Element()
542 e[0] += nodes.TextElement()
543 e[0][1] += nodes.Text('some text')
544 e += nodes.Element()
545 e += nodes.Element()
546 self.assertEqual(list(e.traverse()),
547 [e, e[0], e[0][0], e[0][1], e[0][1][0], e[1], e[2]])
548 self.assertEqual(list(e.traverse(include_self=False)),
549 [e[0], e[0][0], e[0][1], e[0][1][0], e[1], e[2]])
550 self.assertEqual(list(e.traverse(descend=False)),
551 [e])
552 self.assertEqual(list(e[0].traverse(descend=False, ascend=True)),
553 [e[0], e[1], e[2]])
554 self.assertEqual(list(e[0][0].traverse(descend=False, ascend=True)),
555 [e[0][0], e[0][1], e[1], e[2]])
556 self.assertEqual(list(e[0][0].traverse(descend=False, siblings=True)),
557 [e[0][0], e[0][1]])
558 self.testlist = e[0:2]
559 self.assertEqual(list(e.traverse(condition=self.not_in_testlist)),
560 [e, e[0][0], e[0][1], e[0][1][0], e[2]])
561 # Return siblings despite siblings=False because ascend is true.
562 self.assertEqual(list(e[1].traverse(ascend=True, siblings=False)),
563 [e[1], e[2]])
564 self.assertEqual(list(e[0].traverse()),
565 [e[0], e[0][0], e[0][1], e[0][1][0]])
566 self.testlist = [e[0][0], e[0][1]]
567 self.assertEqual(list(e[0].traverse(condition=self.not_in_testlist)),
568 [e[0], e[0][1][0]])
569 self.testlist.append(e[0][1][0])
570 self.assertEqual(list(e[0].traverse(condition=self.not_in_testlist)),
571 [e[0]])
572 self.assertEqual(list(e.traverse(nodes.TextElement)), [e[0][1]])
574 def test_next_node(self):
575 e = nodes.Element()
576 e += nodes.Element()
577 e[0] += nodes.Element()
578 e[0] += nodes.TextElement()
579 e[0][1] += nodes.Text('some text')
580 e += nodes.Element()
581 e += nodes.Element()
582 self.testlist = [e[0], e[0][1], e[1]]
583 compare = [(e, e[0][0]),
584 (e[0], e[0][0]),
585 (e[0][0], e[0][1][0]),
586 (e[0][1], e[0][1][0]),
587 (e[0][1][0], e[2]),
588 (e[1], e[2]),
589 (e[2], None)]
590 for node, next_node in compare:
591 self.assertEqual(node.next_node(self.not_in_testlist, ascend=True),
592 next_node)
593 self.assertEqual(e[0][0].next_node(ascend=True), e[0][1])
594 self.assertEqual(e[2].next_node(), None)
596 def not_in_testlist(self, x):
597 return x not in self.testlist
599 def test_copy(self):
600 grandchild = nodes.Text('rawsource')
601 child = nodes.emphasis('rawsource', grandchild, att='child')
602 e = nodes.Element('rawsource', child, att='e')
603 # Shallow copy:
604 e_copy = e.copy()
605 self.assertTrue(e is not e_copy)
606 # Internal attributes (like `rawsource`) are also copied.
607 self.assertEqual(e.rawsource, 'rawsource')
608 self.assertEqual(e_copy.rawsource, e.rawsource)
609 self.assertEqual(e_copy['att'], 'e')
610 # Children are not copied.
611 self.assertEqual(len(e_copy), 0)
612 # Deep copy:
613 e_deepcopy = e.deepcopy()
614 self.assertEqual(e_deepcopy.rawsource, e.rawsource)
615 self.assertEqual(e_deepcopy['att'], 'e')
616 # Children are copied recursively.
617 self.assertEqual(e_deepcopy[0][0], grandchild)
618 self.assertTrue(e_deepcopy[0][0] is not grandchild)
619 self.assertEqual(e_deepcopy[0]['att'], 'child')
622 class TreeCopyVisitorTests(unittest.TestCase):
624 def setUp(self):
625 document = utils.new_document('test data')
626 document += nodes.paragraph('', 'Paragraph 1.')
627 blist = nodes.bullet_list()
628 for i in range(1, 6):
629 item = nodes.list_item()
630 for j in range(1, 4):
631 item += nodes.paragraph('', 'Item %s, paragraph %s.' % (i, j))
632 blist += item
633 document += blist
634 self.document = document
636 def compare_trees(self, one, two):
637 self.assertEqual(one.__class__, two.__class__)
638 self.assertNotEqual(id(one), id(two))
639 self.assertEqual(len(one.children), len(two.children))
640 for i in range(len(one.children)):
641 self.compare_trees(one.children[i], two.children[i])
643 def test_copy_whole(self):
644 visitor = nodes.TreeCopyVisitor(self.document)
645 self.document.walkabout(visitor)
646 newtree = visitor.get_tree_copy()
647 self.assertEqual(self.document.pformat(), newtree.pformat())
648 self.compare_trees(self.document, newtree)
651 class MiscFunctionTests(unittest.TestCase):
653 names = [('a', 'a'), ('A', 'a'), ('A a A', 'a a a'),
654 ('A a A a', 'a a a a'),
655 (' AaA\n\r\naAa\tAaA\t\t', 'aaa aaa aaa')]
657 def test_normalize_name(self):
658 for input, output in self.names:
659 normed = nodes.fully_normalize_name(input)
660 self.assertEqual(normed, output)
662 def test_set_id_default(self):
663 # Default prefixes.
664 document = utils.new_document('test')
665 # From name.
666 element = nodes.Element(names=['test'])
667 document.set_id(element)
668 self.assertEqual(element['ids'], ['test'])
669 # Auto-generated.
670 element = nodes.Element()
671 document.set_id(element)
672 self.assertEqual(element['ids'], ['id1'])
674 def test_set_id_custom(self):
675 # Custom prefixes.
676 document = utils.new_document('test')
677 # Change settings.
678 document.settings.id_prefix = 'prefix'
679 document.settings.auto_id_prefix = 'auto'
680 # From name.
681 element = nodes.Element(names=['test'])
682 document.set_id(element)
683 self.assertEqual(element['ids'], ['prefixtest'])
684 # Auto-generated.
685 element = nodes.Element()
686 document.set_id(element)
687 self.assertEqual(element['ids'], ['prefixauto1'])
690 if __name__ == '__main__':
691 unittest.main()