summaryrefslogtreecommitdiff
path: root/creole/tests/test_rest2html.py
blob: dee787c2a4bc60af40f9f3bce5f5610a278258bd (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python
# coding: utf-8

"""
    rest2html unittest
    ~~~~~~~~~~~~~~~~~~

    Unittests for rest2html, see: creole/rest2html/clean_writer.py

    :copyleft: 2011-2012 by python-creole team, see AUTHORS for more details.
    :license: GNU GPL v3 or above, see LICENSE for more details.
"""


import tempfile
import unittest

from creole.tests.utils.base_unittest import BaseCreoleTest


class ReSt2HtmlTests(BaseCreoleTest):
    def test_clean_link_table(self):
        self.assert_rest2html("""
            :homepage:
              http://code.google.com/p/python-creole/

            :sourcecode:
              http://github.com/jedie/python-creole
        """, """
            <table>
            <tr><th>homepage:</th><td><a href="http://code.google.com/p/python-creole/">http://code.google.com/p/python-creole/</a></td>
            </tr>
            <tr><th>sourcecode:</th><td><a href="http://github.com/jedie/python-creole">http://github.com/jedie/python-creole</a></td>
            </tr>
            </table>
        """)

    def test_clean_table(self):
        self.assert_rest2html("""
            +------------+------------+
            | Headline 1 | Headline 2 |
            +============+============+
            | cell one   | cell two   |
            +------------+------------+
        """, """
            <table>
            <colgroup>
            <col width="50%" />
            <col width="50%" />
            </colgroup>
            <tr><th>Headline 1</th>
            <th>Headline 2</th>
            </tr>
            <tr><td>cell one</td>
            <td>cell two</td>
            </tr>
            </table>
        """)

    def test_clean_list(self):
        self.assert_rest2html("""
            * item 1

                * item 1.1

                * item 1.2

            * item 2

            numbered list:

            #. item A

            #. item B
        """, """
            <ul>
            <li><p>item 1</p>
            <ul>
            <li>item 1.1</li>
            <li>item 1.2</li>
            </ul>
            </li>
            <li><p>item 2</p>
            </li>
            </ul>
            <p>numbered list:</p>
            <ol>
            <li>item A</li>
            <li>item B</li>
            </ol>
        """)

    def test_clean_headline(self):
        self.assert_rest2html("""
            ======
            head 1
            ======

            ------
            head 2
            ------
        """, """
            <h1>head 1</h1>
            <h2>head 2</h2>
        """)

    def test_include_disabled_by_default(self):
        self.assert_rest2html("""
            Include should be disabled by default.

            .. include:: doesntexist.txt
        """, """
            <p>Include should be disabled by default.</p>
        """, report_level=3)  # Set log level to "error" to suppress the waring output

    def test_include_enabled(self):
        test_content = "Content from include file."
        test_content = test_content.encode("utf-8")
        with tempfile.NamedTemporaryFile() as temp:
            temp.write(test_content)
            temp.flush()
            self.assert_rest2html("""
                Enable include and test it.

                .. include:: %s
            """ % temp.name, """
                <p>Enable include and test it.</p>
                <p>Content from include file.</p>
            """, file_insertion_enabled=True, input_encoding="utf-8")

    def test_raw_disabled_by_default(self):
        self.assert_rest2html("""
            Raw directive should be disabled by default.

            .. raw:: html

               <hr width=50 size=10>
        """, """
            <p>Raw directive should be disabled by default.</p>
        """, report_level=3)  # Set log level to "error" to suppress the waring output

    def test_raw_enabled(self):
        self.assert_rest2html("""
            Now RAW is enabled.

            .. raw:: html

               <hr width=50 size=10>
        """, """
            <p>Now RAW is enabled.</p>
            <hr width=50 size=10>
        """, raw_enabled=True)

    def test_preserve_image_alignment(self):
        self.assert_rest2html("""
            Image alignment should be preserved.

            .. image:: foo.png
               :align: right
        """, """
            <p>Image alignment should be preserved.</p>
            <img alt="foo.png" src="foo.png" align="right" />
        """)

    def test_preserve_figure_alignment(self):
        self.assert_rest2html("""
            Image alignment should be preserved.

            .. figure:: bar.png
               :align: right
        """, """
            <p>Image alignment should be preserved.</p>
            <img alt="bar.png" src="bar.png" align="right" />
        """)


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