Debian Codegraph @

test/test_traversals.py

raw symbols SCIP
1 #! /usr/bin/env python3
2
3 # $Id: test_traversals.py 9914 2024-08-21 13:45:08Z milde $
4 # Author: Martin Blais <[email protected]>
5 # Copyright: This module has been placed in the public domain.
6
7 """
8 Test module for traversals.
9 """
10
11 from pathlib import Path
12 import sys
13 import unittest
14
15 if __name__ == '__main__':
16 # prepend the "docutils root" to the Python library path
17 # so we import the local `docutils` package.
18 sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
19
20 import docutils
21 from docutils import core, nodes, writers
22
23
24 stop_traversal_input = '''
25 ==================
26 Train Travel
27 ==================
28
29 Happily, happily going by train.
30
31 .. attention:: Attention, attention. This is a public annoucement.
32 You must get off the train now.
33
34 KaZoom! Train crashes.
35
36 - Told ya!!! Get off the train next time.
37
38 '''
39
40
41 class AttentiveVisitor(nodes.SparseNodeVisitor):
42
43 def visit_attention(self, node):
44 raise nodes.StopTraversal
45
46 def visit_bullet_list(self, node):
47 raise RuntimeError("It's too late for attention, "
48 "more discipline is needed!.")
49
50
51 class AttentiveWriter(writers.Writer):
52
53 def translate(self):
54 self.visitor = visitor = AttentiveVisitor(self.document)
55
56 # Test both kinds of traversals.
57 self.document.walkabout(visitor)
58 self.document.walk(visitor)
59
60
61 class StopTraversalTests(unittest.TestCase, docutils.SettingsSpec):
62
63 """
64 Test interrupting the visitor during traversal. In this test we stop it
65 when we reach an attention node.
66 """
67 def test_stop_traversal(self):
68 # Load some document tree in memory.
69 doctree = core.publish_doctree(
70 source=stop_traversal_input,
71 settings_spec=self)
72 self.assertTrue(isinstance(doctree, nodes.document))
73
74 core.publish_parts(
75 reader='doctree', source_class=docutils.io.DocTreeInput,
76 source=doctree, source_path='test',
77 writer=AttentiveWriter())
78
79
80 if __name__ == '__main__':
81 unittest.main()