Debian Codegraph @

test/test_readers/test_pep/test_rfc2822.py

raw symbols SCIP
1 #! /usr/bin/env python3
2
3 # $Id: test_rfc2822.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 RFC-2822 headers in PEPs (readers/pep.py).
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[3]))
19
20 from docutils.frontend import get_default_settings
21 from docutils.parsers.rst import Parser
22 from docutils.parsers.rst.states import Inliner
23 from docutils.readers.pep import Reader
24 from docutils.utils import new_document
25
26
27 class PEPParserTestCase(unittest.TestCase):
28 def test_parser(self):
29 parser = Parser(rfc2822=True, inliner=Inliner())
30 settings = get_default_settings(Parser, Reader)
31 settings.warning_stream = ''
32 for name, cases in totest.items():
33 for casenum, (case_input, case_expected) in enumerate(cases):
34 with self.subTest(id=f'totest[{name!r}][{casenum}]'):
35 document = new_document('test data', settings.copy())
36 parser.parse(case_input, document)
37 output = document.pformat()
38 self.assertEqual(case_expected, output)
39
40
41 totest = {}
42
43 totest['rfc2822'] = [
44 ["""\
45 Author: Me
46 Version: 1
47 Date: 2002-04-23
48 """,
49 """\
50 <document source="test data">
51 <field_list classes="rfc2822">
52 <field>
53 <field_name>
54 Author
55 <field_body>
56 <paragraph>
57 Me
58 <field>
59 <field_name>
60 Version
61 <field_body>
62 <paragraph>
63 1
64 <field>
65 <field_name>
66 Date
67 <field_body>
68 <paragraph>
69 2002-04-23
70 """],
71 ["""\
72
73
74 Author: Me
75 Version: 1
76 Date: 2002-04-23
77
78 .. Leading blank lines don't affect RFC-2822 header parsing.
79 """,
80 """\
81 <document source="test data">
82 <field_list classes="rfc2822">
83 <field>
84 <field_name>
85 Author
86 <field_body>
87 <paragraph>
88 Me
89 <field>
90 <field_name>
91 Version
92 <field_body>
93 <paragraph>
94 1
95 <field>
96 <field_name>
97 Date
98 <field_body>
99 <paragraph>
100 2002-04-23
101 <comment xml:space="preserve">
102 Leading blank lines don't affect RFC-2822 header parsing.
103 """],
104 ["""\
105 .. A comment should prevent RFC-2822 header parsing.
106
107 Author: Me
108 Version: 1
109 Date: 2002-04-23
110 """,
111 """\
112 <document source="test data">
113 <comment xml:space="preserve">
114 A comment should prevent RFC-2822 header parsing.
115 <paragraph>
116 Author: Me
117 Version: 1
118 Date: 2002-04-23
119 """],
120 ["""\
121 Author: Me
122
123 Version: 1
124 Date: 2002-04-23
125 """,
126 """\
127 <document source="test data">
128 <field_list classes="rfc2822">
129 <field>
130 <field_name>
131 Author
132 <field_body>
133 <paragraph>
134 Me
135 <paragraph>
136 Version: 1
137 Date: 2002-04-23
138 """],
139 ["""\
140 field:
141 empty item above, no blank line
142 """,
143 """\
144 <document source="test data">
145 <field_list classes="rfc2822">
146 <field>
147 <field_name>
148 field
149 <field_body>
150 <system_message level="2" line="2" source="test data" type="WARNING">
151 <paragraph>
152 RFC2822-style field list ends without a blank line; unexpected unindent.
153 <paragraph>
154 empty item above, no blank line
155 """],
156 ["""\
157 Author:
158 Me
159 Version:
160 1
161 Date:
162 2002-04-23
163 """,
164 """\
165 <document source="test data">
166 <field_list classes="rfc2822">
167 <field>
168 <field_name>
169 Author
170 <field_body>
171 <paragraph>
172 Me
173 <field>
174 <field_name>
175 Version
176 <field_body>
177 <paragraph>
178 1
179 <field>
180 <field_name>
181 Date
182 <field_body>
183 <paragraph>
184 2002-04-23
185 """],
186 ["""\
187 Authors: Me,
188 Myself,
189 and I
190 Version: 1
191 or so
192 Date: 2002-04-23
193 (Tuesday)
194 """,
195 """\
196 <document source="test data">
197 <field_list classes="rfc2822">
198 <field>
199 <field_name>
200 Authors
201 <field_body>
202 <paragraph>
203 Me,
204 Myself,
205 and I
206 <field>
207 <field_name>
208 Version
209 <field_body>
210 <paragraph>
211 1
212 or so
213 <field>
214 <field_name>
215 Date
216 <field_body>
217 <paragraph>
218 2002-04-23
219 (Tuesday)
220 """],
221 ["""\
222 Authors: Me,
223 Myself,
224 and I
225 Version: 1
226 or so
227 Date: 2002-04-23
228 (Tuesday)
229 """,
230 """\
231 <document source="test data">
232 <field_list classes="rfc2822">
233 <field>
234 <field_name>
235 Authors
236 <field_body>
237 <paragraph>
238 Me,
239 Myself,
240 and I
241 <field>
242 <field_name>
243 Version
244 <field_body>
245 <paragraph>
246 1
247 or so
248 <field>
249 <field_name>
250 Date
251 <field_body>
252 <paragraph>
253 2002-04-23
254 (Tuesday)
255 """],
256 ["""\
257 Authors: - Me
258 - Myself
259 - I
260 Version:
261 """,
262 """\
263 <document source="test data">
264 <field_list classes="rfc2822">
265 <field>
266 <field_name>
267 Authors
268 <field_body>
269 <bullet_list bullet="-">
270 <list_item>
271 <paragraph>
272 Me
273 <list_item>
274 <paragraph>
275 Myself
276 <list_item>
277 <paragraph>
278 I
279 <field>
280 <field_name>
281 Version
282 <field_body>
283 """],
284 ["""\
285 Authors: Me
286
287 Myself and I
288 Version:
289 """,
290 """\
291 <document source="test data">
292 <field_list classes="rfc2822">
293 <field>
294 <field_name>
295 Authors
296 <field_body>
297 <paragraph>
298 Me
299 <block_quote>
300 <paragraph>
301 Myself and I
302 <system_message level="2" line="4" source="test data" type="WARNING">
303 <paragraph>
304 Block quote ends without a blank line; unexpected unindent.
305 <paragraph>
306 Version:
307 """],
308 ]
309
310 if __name__ == '__main__':
311 unittest.main()