summaryrefslogtreecommitdiff
path: root/sandbox/tibs/pysource2/test_reader.py
blob: e48f207497e9b2f9e6612449fd3a4ec268e65087 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#! /usr/bin/env python
"""test_reader.py

Unit tests for the Python source Reader

Initially, this is a standalone test, but ultimately it may be merged into the
mechanisms used for the Docutils self-tests.

:Author:    Tibs
:Contact:   tibs@tibsnjoan.co.uk
:Revision:  $Revision$
:Date:      $Date$
:Copyright: This module has been placed in the public domain.
"""

__docformat__ = 'reStructuredText'

import unittest

from package import parse_package
from transform import make_document
from reader import Reader
from docutils.core import publish_string
from docutils.readers.python.moduleparser import parse_module

class PackageTest(unittest.TestCase):

    def testReader(self):
        """Test the reader works as expected
        """
        reader = Reader()

        source="# A Python comment"
        source_path="test.py"

        # Hmm - extra debugging info...
        publish_string = publish_string_with_traceback

        actual_result = publish_string(reader=reader,reader_name="python",
                                       parser_name="restructuredtext",
                                       writer_name="pseudoxml",
                                       source=source, source_path=source_path)

        wanted_result = """\
<document source="Module test">
    <section class="module" id="module-test" name="module test">
        <title>
            Module test\n"""


        if wanted_result != actual_result:
            print "+++++++++++++++++++++++++ WANT"
            print wanted_result
            print "+++++++++++++++++++++++++ GOT"
            print actual_result
            print "+++++++++++++++++++++++++"

        self.assertEqual(actual_result,wanted_result)

    def testTool(self):
        """Trying to think what to do for packages"""
        # The Reader interface is designed to work with single test entities,
        # either a string or the content of a text file (i.e., a single thing
        # that can be accessed via some sort of "read" method).
        # This doesn't work for packages, where one has multiple files.
        # Thus I suspect that the Reader interface is not appropriate for
        # what I want to do (at least, not without doing it unnecessary
        # violence and making it a lot more complicated).
        # So I need to do things "by hand"...

        source="# A Python comment"
        source_path="test.py"

        # Since a body of text is a Module, not a Package, we'll go straight
        # to it
        nodes = parse_module(source,source_path)

        # That then needs converting to a docutils tree
        document = make_document(nodes)

        # And *that* wants converting to the appropriate output format

        from docutils.writers.pseudoxml import Writer
        writer = Writer()
        writer.document = document
        writer.translate()
        actual_result = writer.output

        wanted_result = """\
<document source="Module test">
    <section class="module" id="module-test" name="module test">
        <title>
            Module test\n"""


        if wanted_result != actual_result:
            print "+++++++++++++++++++++++++ WANT"
            print wanted_result
            print "+++++++++++++++++++++++++ GOT"
            print actual_result
            print "+++++++++++++++++++++++++"

        self.assertEqual(actual_result,wanted_result)


def publish_string_with_traceback(reader=None,reader_name=None,
                                  parser_name=None,writer_name=None,
                                  source=None,source_path=None):
    """A modified version of publish_string, so I can request traceback.
    """
    from docutils.core import Publisher
    from docutils import io
    pub = Publisher(reader=reader,
                    source_class=io.StringInput,
                    destination_class=io.StringOutput)
    pub.set_components(reader_name="python",
                       parser_name="restructuredtext",
                       writer_name="pseudoxml")

    pub.process_command_line(argv=["--traceback"])

    pub.set_source(source=source, source_path=source_path)
    return pub.publish(enable_exit=False)


if __name__ == "__main__":
    unittest.main()