Debian Codegraph @

test/test_parsers/test_rst/test_directives/test_admonitions.py

raw symbols SCIP
1 #! /usr/bin/env python3
2
3 # $Id: test_admonitions.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 admonitions.py directives.
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[4]))
19
20 from docutils.frontend import get_default_settings
21 from docutils.parsers.rst import Parser
22 from docutils.utils import new_document
23
24
25 class ParserTestCase(unittest.TestCase):
26 def test_parser(self):
27 parser = Parser()
28 settings = get_default_settings(Parser)
29 settings.warning_stream = ''
30 for name, cases in totest.items():
31 for casenum, (case_input, case_expected) in enumerate(cases):
32 with self.subTest(id=f'totest[{name!r}][{casenum}]'):
33 document = new_document('test data', settings.copy())
34 parser.parse(case_input, document)
35 output = document.pformat()
36 self.assertEqual(case_expected, output)
37
38
39 totest = {}
40
41 totest['admonitions'] = [
42 ["""\
43 .. Attention:: Directives at large.
44
45 .. Note:: :name: mynote
46 :class: testnote
47
48 Admonitions support the generic "name" and "class" options.
49
50 .. Tip:: 15% if the
51 service is good.
52
53 .. Hint:: It's bigger than a bread box.
54
55 - .. WARNING:: Strong prose may provoke extreme mental exertion.
56 Reader discretion is strongly advised.
57 - .. Error:: Does not compute.
58
59 .. Caution::
60
61 Don't take any wooden nickels.
62
63 .. DANGER:: Mad scientist at work!
64
65 .. Important::
66 - Wash behind your ears.
67 - Clean up your room.
68 - Call your mother.
69 - Back up your data.
70 """,
71 """\
72 <document source="test data">
73 <attention>
74 <paragraph>
75 Directives at large.
76 <note classes="testnote" ids="mynote" names="mynote">
77 <paragraph>
78 Admonitions support the generic "name" and "class" options.
79 <tip>
80 <paragraph>
81 15% if the
82 service is good.
83 <hint>
84 <paragraph>
85 It's bigger than a bread box.
86 <bullet_list bullet="-">
87 <list_item>
88 <warning>
89 <paragraph>
90 Strong prose may provoke extreme mental exertion.
91 Reader discretion is strongly advised.
92 <list_item>
93 <error>
94 <paragraph>
95 Does not compute.
96 <caution>
97 <paragraph>
98 Don't take any wooden nickels.
99 <danger>
100 <paragraph>
101 Mad scientist at work!
102 <important>
103 <bullet_list bullet="-">
104 <list_item>
105 <paragraph>
106 Wash behind your ears.
107 <list_item>
108 <paragraph>
109 Clean up your room.
110 <list_item>
111 <paragraph>
112 Call your mother.
113 <list_item>
114 <paragraph>
115 Back up your data.
116 """],
117 ["""\
118 .. note:: One-line notes.
119 .. note:: One after the other.
120 .. note:: No blank lines in-between.
121 """,
122 """\
123 <document source="test data">
124 <note>
125 <paragraph>
126 One-line notes.
127 <note>
128 <paragraph>
129 One after the other.
130 <note>
131 <paragraph>
132 No blank lines in-between.
133 """],
134 ["""\
135 .. note:: Content before options
136 is possible too.
137 :class: mynote
138
139 .. note:: :strong:`a role is not an option`.
140 :name: role not option
141
142 .. note:: a role is
143 :strong:`not an option`, even if its starts a line.
144 """,
145 """\
146 <document source="test data">
147 <note classes="mynote">
148 <paragraph>
149 Content before options
150 is possible too.
151 <note ids="role-not-option" names="role\\ not\\ option">
152 <paragraph>
153 <strong>
154 a role is not an option
155 .
156 <note>
157 <paragraph>
158 a role is
159 <strong>
160 not an option
161 , even if its starts a line.
162 """],
163 ["""\
164 .. note::
165 """,
166 """\
167 <document source="test data">
168 <system_message level="3" line="1" source="test data" type="ERROR">
169 <paragraph>
170 Content block expected for the "note" directive; none found.
171 <literal_block xml:space="preserve">
172 .. note::
173 """],
174 ["""\
175 .. admonition:: Admonition
176
177 This is a generic admonition.
178 """,
179 """\
180 <document source="test data">
181 <admonition classes="admonition-admonition">
182 <title>
183 Admonition
184 <paragraph>
185 This is a generic admonition.
186 """],
187 ["""\
188 .. admonition:: And, by the way...
189
190 You can make up your own admonition too.
191 """,
192 """\
193 <document source="test data">
194 <admonition classes="admonition-and-by-the-way">
195 <title>
196 And, by the way...
197 <paragraph>
198 You can make up your own admonition too.
199 """],
200 ["""\
201 .. admonition:: Admonition
202 :class: emergency
203 :name: reference name
204
205 Test the "class" override.
206 """,
207 """\
208 <document source="test data">
209 <admonition classes="emergency" ids="reference-name" names="reference\\ name">
210 <title>
211 Admonition
212 <paragraph>
213 Test the "class" override.
214 """],
215 ["""\
216 .. admonition::
217
218 Generic admonitions require a title.
219 """,
220 """\
221 <document source="test data">
222 <system_message level="3" line="1" source="test data" type="ERROR">
223 <paragraph>
224 Error in "admonition" directive:
225 1 argument(s) required, 0 supplied.
226 <literal_block xml:space="preserve">
227 .. admonition::
228 \n\
229 Generic admonitions require a title.
230 """],
231 ]
232
233
234 if __name__ == '__main__':
235 unittest.main()