1 #! /usr/bin/env python3
4 # Author: David Goodger <goodger@python.org>
5 # Copyright: This module has been placed in the public domain.
11 from pathlib
import Path
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]))
20 from docutils
.frontend
import get_default_settings
21 from docutils
.parsers
.rst
import Parser
22 from docutils
.utils
import new_document
25 class ParserTestCase(unittest
.TestCase
):
26 def test_parser(self
):
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
)
41 totest
['footnotes'] = [
43 .. [1] This is a footnote.
46 <document source="test data">
47 <footnote ids="footnote-1" names="1">
54 .. [1] This is a footnote
58 <document source="test data">
59 <footnote ids="footnote-1" names="1">
67 .. [1] This is a footnote
68 on multiple lines with more space.
70 .. [2] This is a footnote
71 on multiple lines with less space.
74 <document source="test data">
75 <footnote ids="footnote-1" names="1">
80 on multiple lines with more space.
81 <footnote ids="footnote-2" names="2">
86 on multiple lines with less space.
90 This is a footnote on multiple lines
91 whose block starts on line 2.
94 <document source="test data">
95 <footnote ids="footnote-1" names="1">
99 This is a footnote on multiple lines
100 whose block starts on line 2.
103 An empty footnote raises a warning:
109 <document source="test data">
111 An empty footnote raises a warning:
112 <footnote ids="footnote-1" names="1">
115 <system_message level="2" line="4" source="test data" type="WARNING">
117 Footnote content expected.
124 <document source="test data">
125 <footnote ids="footnote-1" names="1">
130 <system_message level="2" line="2" source="test data" type="WARNING">
132 Explicit markup ends without a blank line; unexpected unindent.
138 totest
['auto_numbered_footnotes'] = [
140 [#]_ is the first auto-numbered footnote reference.
141 [#]_ is the second auto-numbered footnote reference.
143 .. [#] Auto-numbered footnote 1.
144 .. [#] Auto-numbered footnote 2.
145 .. [#] Auto-numbered footnote 3.
147 [#]_ is the third auto-numbered footnote reference.
150 <document source="test data">
152 <footnote_reference auto="1" ids="footnote-reference-1">
153 is the first auto-numbered footnote reference.
154 <footnote_reference auto="1" ids="footnote-reference-2">
155 is the second auto-numbered footnote reference.
156 <footnote auto="1" ids="footnote-1">
158 Auto-numbered footnote 1.
159 <footnote auto="1" ids="footnote-2">
161 Auto-numbered footnote 2.
162 <footnote auto="1" ids="footnote-3">
164 Auto-numbered footnote 3.
166 <footnote_reference auto="1" ids="footnote-reference-3">
167 is the third auto-numbered footnote reference.
170 [#third]_ is a reference to the third auto-numbered footnote.
172 .. [#first] First auto-numbered footnote.
173 .. [#second] Second auto-numbered footnote.
174 .. [#third] Third auto-numbered footnote.
176 [#second]_ is a reference to the second auto-numbered footnote.
177 [#first]_ is a reference to the first auto-numbered footnote.
178 [#third]_ is another reference to the third auto-numbered footnote.
180 Here are some internal cross-references to the targets generated by
181 the footnotes: first_, second_, third_.
184 <document source="test data">
186 <footnote_reference auto="1" ids="footnote-reference-1" refname="third">
187 is a reference to the third auto-numbered footnote.
188 <footnote auto="1" ids="first" names="first">
190 First auto-numbered footnote.
191 <footnote auto="1" ids="second" names="second">
193 Second auto-numbered footnote.
194 <footnote auto="1" ids="third" names="third">
196 Third auto-numbered footnote.
198 <footnote_reference auto="1" ids="footnote-reference-2" refname="second">
199 is a reference to the second auto-numbered footnote.
200 <footnote_reference auto="1" ids="footnote-reference-3" refname="first">
201 is a reference to the first auto-numbered footnote.
202 <footnote_reference auto="1" ids="footnote-reference-4" refname="third">
203 is another reference to the third auto-numbered footnote.
205 Here are some internal cross-references to the targets generated by
207 <reference name="first" refname="first">
210 <reference name="second" refname="second">
213 <reference name="third" refname="third">
218 Mixed anonymous and labelled auto-numbered footnotes:
220 [#four]_ should be 4, [#]_ should be 1,
221 [#]_ should be 3, [#]_ is one too many,
222 [#two]_ should be 2, and [#six]_ doesn't exist.
224 .. [#] Auto-numbered footnote 1.
225 .. [#two] Auto-numbered footnote 2.
226 .. [#] Auto-numbered footnote 3.
227 .. [#four] Auto-numbered footnote 4.
228 .. [#five] Auto-numbered footnote 5.
229 .. [#five] Auto-numbered footnote 5 again (duplicate).
232 <document source="test data">
234 Mixed anonymous and labelled auto-numbered footnotes:
236 <footnote_reference auto="1" ids="footnote-reference-1" refname="four">
238 <footnote_reference auto="1" ids="footnote-reference-2">
240 <footnote_reference auto="1" ids="footnote-reference-3">
242 <footnote_reference auto="1" ids="footnote-reference-4">
244 <footnote_reference auto="1" ids="footnote-reference-5" refname="two">
246 <footnote_reference auto="1" ids="footnote-reference-6" refname="six">
248 <footnote auto="1" ids="footnote-1">
250 Auto-numbered footnote 1.
251 <footnote auto="1" ids="two" names="two">
253 Auto-numbered footnote 2.
254 <footnote auto="1" ids="footnote-2">
256 Auto-numbered footnote 3.
257 <footnote auto="1" ids="four" names="four">
259 Auto-numbered footnote 4.
260 <footnote auto="1" dupnames="five" ids="five">
262 Auto-numbered footnote 5.
263 <footnote auto="1" dupnames="five" ids="five-1">
264 <system_message backrefs="five-1" level="2" line="12" source="test data" type="WARNING">
266 Duplicate explicit target name: "five".
268 Auto-numbered footnote 5 again (duplicate).
271 Mixed manually-numbered, anonymous auto-numbered,
272 and labelled auto-numbered footnotes:
274 [#four]_ should be 4, [#]_ should be 2,
275 [1]_ is 1, [3]_ is 3,
276 [#]_ should be 6, [#]_ is one too many,
277 [#five]_ should be 5, and [#six]_ doesn't exist.
279 .. [1] Manually-numbered footnote 1.
280 .. [#] Auto-numbered footnote 2.
281 .. [#four] Auto-numbered footnote 4.
282 .. [3] Manually-numbered footnote 3
283 .. [#five] Auto-numbered footnote 5.
284 .. [#five] Auto-numbered footnote 5 again (duplicate).
285 .. [#] Auto-numbered footnote 6.
288 <document source="test data">
290 Mixed manually-numbered, anonymous auto-numbered,
291 and labelled auto-numbered footnotes:
293 <footnote_reference auto="1" ids="footnote-reference-1" refname="four">
295 <footnote_reference auto="1" ids="footnote-reference-2">
297 <footnote_reference ids="footnote-reference-3" refname="1">
300 <footnote_reference ids="footnote-reference-4" refname="3">
303 <footnote_reference auto="1" ids="footnote-reference-5">
305 <footnote_reference auto="1" ids="footnote-reference-6">
307 <footnote_reference auto="1" ids="footnote-reference-7" refname="five">
309 <footnote_reference auto="1" ids="footnote-reference-8" refname="six">
311 <footnote ids="footnote-1" names="1">
315 Manually-numbered footnote 1.
316 <footnote auto="1" ids="footnote-2">
318 Auto-numbered footnote 2.
319 <footnote auto="1" ids="four" names="four">
321 Auto-numbered footnote 4.
322 <footnote ids="footnote-3" names="3">
326 Manually-numbered footnote 3
327 <footnote auto="1" dupnames="five" ids="five">
329 Auto-numbered footnote 5.
330 <footnote auto="1" dupnames="five" ids="five-1">
331 <system_message backrefs="five-1" level="2" line="14" source="test data" type="WARNING">
333 Duplicate explicit target name: "five".
335 Auto-numbered footnote 5 again (duplicate).
336 <footnote auto="1" ids="footnote-4">
338 Auto-numbered footnote 6.
342 totest
['auto_symbol_footnotes'] = [
344 .. [*] This is an auto-symbol footnote.
347 <document source="test data">
348 <footnote auto="*" ids="footnote-1">
350 This is an auto-symbol footnote.
355 if __name__
== '__main__':