4 # Author: Martin Blais <blais@furius.ca>
5 # Copyright: This module has been placed in the public domain.
8 Test module for traversals.
12 import DocutilsTestSupport
# must be imported before docutils
13 from docutils
import nodes
, core
, io
, utils
, writers
14 from docutils
.writers
.null
import Writer
as NullWriter
19 stop_traversal_input
= '''
24 Happily, happily going by train.
26 .. attention:: Attention, attention. This is a public annoucement.
27 You must get off the train now.
29 KaZoom! Train crashes.
31 - Told ya!!! Get off the train next time.
35 class AttentiveVisitor(nodes
.SparseNodeVisitor
):
37 def visit_attention(self
, node
):
38 raise nodes
.StopTraversal
40 def visit_bullet_list(self
, node
):
41 raise RuntimeError("It's too late for attention, "
42 "more discipline is needed!.")
44 class AttentiveWriter(writers
.Writer
):
47 self
.visitor
= visitor
= AttentiveVisitor(self
.document
)
49 # Test both kinds of traversals.
50 self
.document
.walkabout(visitor
)
51 self
.document
.walk(visitor
)
53 class StopTraversalTests(unittest
.TestCase
, docutils
.SettingsSpec
):
56 if not hasattr(unittest
.TestCase
, "assertTrue"):
57 assertTrue
= unittest
.TestCase
.failUnless
60 Test interrupting the visitor during traversal. In this test we stop it
61 when we reach an attention node.
63 def test_stop_traversal(self
):
64 # Load some document tree in memory.
65 doctree
= docutils
.core
.publish_doctree(
66 source
=stop_traversal_input
,
67 reader_name
='standalone',
68 parser_name
='restructuredtext',
70 self
.assertTrue(isinstance(doctree
, nodes
.document
))
72 parts
= docutils
.core
.publish_parts(
73 reader_name
='doctree', source_class
=docutils
.io
.DocTreeInput
,
74 source
=doctree
, source_path
='test',
75 writer
=AttentiveWriter())
78 if __name__
== '__main__':