Debian Codegraph @

test/test_transforms/test_class.py

raw symbols SCIP
1 #! /usr/bin/env python3
2
3 # $Id: test_class.py 9425 2023-06-30 14:56:47Z milde $
4 # Author: David Goodger <[email protected]>
5 # Copyright: This module has been placed in the public domain.
6
7 """
8 Tests for `docutils.transforms.misc.ClassAttribute`.
9 """
10
11 from pathlib import Path
12 import sys
13 import unittest
14
15 if __name__ == '__main__':
16 # prepend the "docutils root" to the Python library path
17 # so we import the local `docutils` package.
18 sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
19
20 from docutils.frontend import get_default_settings
21 from docutils.parsers.rst import Parser
22 from docutils.transforms.universal import TestMessages
23 from docutils.utils import new_document
24
25
26 class TransformTestCase(unittest.TestCase):
27 def test_transforms(self):
28 parser = Parser()
29 settings = get_default_settings(Parser)
30 settings.warning_stream = ''
31 for name, (transforms, cases) in totest.items():
32 for casenum, (case_input, case_expected) in enumerate(cases):
33 with self.subTest(id=f'totest[{name!r}][{casenum}]'):
34 document = new_document('test data', settings.copy())
35 parser.parse(case_input, document)
36 # Don't do a ``populate_from_components()`` because that
37 # would enable the Transformer's default transforms.
38 document.transformer.add_transforms(transforms)
39 document.transformer.add_transform(TestMessages)
40 document.transformer.apply_transforms()
41 output = document.pformat()
42 self.assertEqual(case_expected, output)
43
44
45 totest = {}
46
47 totest['class'] = ((), [
48 ["""\
49 .. class:: one
50
51 paragraph
52 """,
53 """\
54 <document source="test data">
55 <paragraph classes="one">
56 paragraph
57 """],
58 ["""\
59 .. class:: two
60 ..
61
62 Block quote
63 """,
64 """\
65 <document source="test data">
66 <comment xml:space="preserve">
67 <block_quote classes="two">
68 <paragraph>
69 Block quote
70 """],
71 ["""\
72 Block quote
73
74 .. class:: three
75
76 Paragraph
77 """,
78 """\
79 <document source="test data">
80 <block_quote>
81 <paragraph>
82 Block quote
83 <paragraph classes="three">
84 Paragraph
85 """],
86 ["""\
87 .. class:: four
88
89 Section Title
90 =============
91
92 Paragraph
93 """,
94 """\
95 <document source="test data">
96 <section classes="four" ids="section-title" names="section\\ title">
97 <title>
98 Section Title
99 <paragraph>
100 Paragraph
101 """],
102 ["""\
103 .. class:: multiple
104
105 paragraph 1
106
107 paragraph 2
108 """,
109 """\
110 <document source="test data">
111 <paragraph classes="multiple">
112 paragraph 1
113 <paragraph classes="multiple">
114 paragraph 2
115 """],
116 ["""\
117 .. class:: multiple
118
119 .. Just a comment. It's silly, but possible
120 """,
121 """\
122 <document source="test data">
123 <comment classes="multiple" xml:space="preserve">
124 Just a comment. It's silly, but possible
125 """],
126 ["""\
127 .. class::
128
129 .. class:: 99
130 """,
131 """\
132 <document source="test data">
133 <system_message level="3" line="1" source="test data" type="ERROR">
134 <paragraph>
135 Error in "class" directive:
136 1 argument(s) required, 0 supplied.
137 <literal_block xml:space="preserve">
138 .. class::
139 <system_message level="3" line="3" source="test data" type="ERROR">
140 <paragraph>
141 Invalid class attribute value for "class" directive: "99".
142 <literal_block xml:space="preserve">
143 .. class:: 99
144 """],
145 ["""\
146 .. class:: one
147 .. class:: two
148
149 multiple class values may be assigned to one element
150 """,
151 """\
152 <document source="test data">
153 <paragraph classes="one two">
154 multiple class values may be assigned to one element
155 """],
156 ["""\
157 .. class:: one two
158
159 multiple class values may be assigned to one element
160 """,
161 """\
162 <document source="test data">
163 <paragraph classes="one two">
164 multiple class values may be assigned to one element
165 """],
166 ["""\
167 .. class:: fancy
168
169 2. List starts at 2.
170 3. Class should apply to list, not to system message.
171 """,
172 """\
173 <document source="test data">
174 <enumerated_list classes="fancy" enumtype="arabic" prefix="" start="2" suffix=".">
175 <list_item>
176 <paragraph>
177 List starts at 2.
178 <list_item>
179 <paragraph>
180 Class should apply to list, not to system message.
181 <system_message level="1" line="3" source="test data" type="INFO">
182 <paragraph>
183 Enumerated list start value not ordinal-1: "2" (ordinal 2)
184 """],
185 ["""\
186 2. List starts at 2.
187 3. Class should apply to next paragraph, not to system message.
188
189 .. class:: fancy
190
191 A paragraph.
192 """,
193 """\
194 <document source="test data">
195 <enumerated_list enumtype="arabic" prefix="" start="2" suffix=".">
196 <list_item>
197 <paragraph>
198 List starts at 2.
199 <list_item>
200 <paragraph>
201 Class should apply to next paragraph, not to system message.
202 <system_message level="1" line="1" source="test data" type="INFO">
203 <paragraph>
204 Enumerated list start value not ordinal-1: "2" (ordinal 2)
205 <paragraph classes="fancy">
206 A paragraph.
207 """],
208 ])
209
210
211 if __name__ == '__main__':
212 unittest.main()