~ubuntu-branches/ubuntu/jaunty/python-docutils/jaunty

« back to all changes in this revision

Viewing changes to test/test_traversals.py

  • Committer: Bazaar Package Importer
  • Author(s): Simon McVittie
  • Date: 2008-07-24 10:39:53 UTC
  • mfrom: (1.1.4 upstream) (3.1.7 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080724103953-8gh4uezg17g9ysgy
Tags: 0.5-2
* Upload docutils 0.5 to unstable
* Update rst.el to upstream Subversion r5596, which apparently fixes
  all its performance problems (17_speed_up_rst_el.dpatch, closes: #474941)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/env python
 
2
 
 
3
# $Id: test_traversals.py 4641 2006-06-28 16:27:55Z blais $
 
4
# Author: Martin Blais <blais@furius.ca>
 
5
# Copyright: This module has been placed in the public domain.
 
6
 
 
7
"""
 
8
Test module for traversals.
 
9
"""
 
10
 
 
11
import unittest
 
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
 
15
import docutils
 
16
 
 
17
 
 
18
 
 
19
stop_traversal_input = '''
 
20
==================
 
21
   Train Travel
 
22
==================
 
23
 
 
24
Happily, happily going by train.
 
25
 
 
26
.. attention:: Attention, attention.  This is a public annoucement.  
 
27
               You must get off the train now.
 
28
 
 
29
KaZoom! Train crashes.
 
30
 
 
31
- Told ya!!!  Get off the train next time.
 
32
 
 
33
'''
 
34
 
 
35
class AttentiveVisitor(nodes.SparseNodeVisitor):
 
36
 
 
37
    def visit_attention(self, node):
 
38
        raise nodes.StopTraversal
 
39
 
 
40
    def visit_bullet_list(self, node):
 
41
        raise RuntimeError("It's too late for attention, "
 
42
                           "more discipline is needed!.")
 
43
 
 
44
class AttentiveWriter(writers.Writer):
 
45
 
 
46
    def translate(self):
 
47
        self.visitor = visitor = AttentiveVisitor(self.document)
 
48
 
 
49
        # Test both kinds of traversals.
 
50
        self.document.walkabout(visitor)
 
51
        self.document.walk(visitor)
 
52
 
 
53
class StopTraversalTests(unittest.TestCase, docutils.SettingsSpec):
 
54
    """
 
55
    Test interrupting the visitor during traversal.  In this test we stop it
 
56
    when we reach an attention node.
 
57
    """
 
58
    def test_stop_traversal(self):
 
59
        # Load some document tree in memory.
 
60
        doctree = docutils.core.publish_doctree(
 
61
            source=stop_traversal_input,
 
62
            reader_name='standalone',
 
63
            parser_name='restructuredtext',
 
64
            settings_spec=self)
 
65
        self.assert_(isinstance(doctree, nodes.document))
 
66
 
 
67
        parts = docutils.core.publish_parts(
 
68
           reader_name='doctree', source_class=docutils.io.DocTreeInput,
 
69
           source=doctree, source_path='test',
 
70
           writer=AttentiveWriter())
 
71
 
 
72
 
 
73
if __name__ == '__main__':
 
74
    unittest.main()
 
75