summaryrefslogtreecommitdiff
path: root/docutils/test/test_transforms/test_class.py
blob: fc6bcd2dc7c8ed4a1f80e290b5ea2e1b0b9f1b9e (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#! /usr/bin/env python3

# $Id$
# Author: David Goodger <goodger@python.org>
# Copyright: This module has been placed in the public domain.

"""
Tests for `docutils.transforms.misc.ClassAttribute`.
"""

from pathlib import Path
import sys
import unittest

if __name__ == '__main__':
    # prepend the "docutils root" to the Python library path
    # so we import the local `docutils` package.
    sys.path.insert(0, str(Path(__file__).resolve().parents[2]))

from docutils.frontend import get_default_settings
from docutils.parsers.rst import Parser
from docutils.transforms.universal import TestMessages
from docutils.utils import new_document


class TransformTestCase(unittest.TestCase):
    def test_transforms(self):
        parser = Parser()
        settings = get_default_settings(Parser)
        settings.warning_stream = ''
        for name, (transforms, cases) in totest.items():
            for casenum, (case_input, case_expected) in enumerate(cases):
                with self.subTest(id=f'totest[{name!r}][{casenum}]'):
                    document = new_document('test data', settings.copy())
                    parser.parse(case_input, document)
                    # Don't do a ``populate_from_components()`` because that
                    # would enable the Transformer's default transforms.
                    document.transformer.add_transforms(transforms)
                    document.transformer.add_transform(TestMessages)
                    document.transformer.apply_transforms()
                    output = document.pformat()
                    self.assertEqual(output, case_expected)


totest = {}

totest['class'] = ((), [
["""\
.. class:: one

paragraph
""",
"""\
<document source="test data">
    <paragraph classes="one">
        paragraph
"""],
["""\
.. class:: two
..

    Block quote
""",
"""\
<document source="test data">
    <comment xml:space="preserve">
    <block_quote classes="two">
        <paragraph>
            Block quote
"""],
["""\
    Block quote

    .. class:: three

Paragraph
""",
"""\
<document source="test data">
    <block_quote>
        <paragraph>
            Block quote
    <paragraph classes="three">
        Paragraph
"""],
["""\
.. class:: four

Section Title
=============

Paragraph
""",
"""\
<document source="test data">
    <section classes="four" ids="section-title" names="section\\ title">
        <title>
            Section Title
        <paragraph>
            Paragraph
"""],
["""\
.. class:: multiple

   paragraph 1

   paragraph 2
""",
"""\
<document source="test data">
    <paragraph classes="multiple">
        paragraph 1
    <paragraph classes="multiple">
        paragraph 2
"""],
["""\
.. class:: multiple

   .. Just a comment.  It's silly, but possible
""",
"""\
<document source="test data">
    <comment classes="multiple" xml:space="preserve">
        Just a comment.  It's silly, but possible
"""],
["""\
.. class::

.. class:: 99
""",
"""\
<document source="test data">
    <system_message level="3" line="1" source="test data" type="ERROR">
        <paragraph>
            Error in "class" directive:
            1 argument(s) required, 0 supplied.
        <literal_block xml:space="preserve">
            .. class::
    <system_message level="3" line="3" source="test data" type="ERROR">
        <paragraph>
            Invalid class attribute value for "class" directive: "99".
        <literal_block xml:space="preserve">
            .. class:: 99
"""],
["""\
.. class:: one
.. class:: two

multiple class values may be assigned to one element
""",
"""\
<document source="test data">
    <paragraph classes="one two">
        multiple class values may be assigned to one element
"""],
["""\
.. class:: one two

multiple class values may be assigned to one element
""",
"""\
<document source="test data">
    <paragraph classes="one two">
        multiple class values may be assigned to one element
"""],
["""\
.. class:: fancy

2. List starts at 2.
3. Class should apply to list, not to system message.
""",
"""\
<document source="test data">
    <enumerated_list classes="fancy" enumtype="arabic" prefix="" start="2" suffix=".">
        <list_item>
            <paragraph>
                List starts at 2.
        <list_item>
            <paragraph>
                Class should apply to list, not to system message.
    <system_message level="1" line="3" source="test data" type="INFO">
        <paragraph>
            Enumerated list start value not ordinal-1: "2" (ordinal 2)
"""],
["""\
2. List starts at 2.
3. Class should apply to next paragraph, not to system message.

   .. class:: fancy

A paragraph.
""",
"""\
<document source="test data">
    <enumerated_list enumtype="arabic" prefix="" start="2" suffix=".">
        <list_item>
            <paragraph>
                List starts at 2.
        <list_item>
            <paragraph>
                Class should apply to next paragraph, not to system message.
    <system_message level="1" line="1" source="test data" type="INFO">
        <paragraph>
            Enumerated list start value not ordinal-1: "2" (ordinal 2)
    <paragraph classes="fancy">
        A paragraph.
"""],
])


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