Small fixes.
[docutils.git] / docutils / test / test_parsers / test_rst / test_footnotes.py
blob7d28757a955d6d36383021057b4729ad4a2e8d14
1 #! /usr/bin/env python3
3 # $Id$
4 # Author: David Goodger <goodger@python.org>
5 # Copyright: This module has been placed in the public domain.
7 """
8 Tests for states.py.
9 """
11 from pathlib import Path
12 import sys
13 import unittest
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):
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)
39 totest = {}
41 totest['footnotes'] = [
42 ["""\
43 .. [1] This is a footnote.
44 """,
45 """\
46 <document source="test data">
47 <footnote ids="footnote-1" names="1">
48 <label>
50 <paragraph>
51 This is a footnote.
52 """],
53 ["""\
54 .. [1] This is a footnote
55 on multiple lines.
56 """,
57 """\
58 <document source="test data">
59 <footnote ids="footnote-1" names="1">
60 <label>
62 <paragraph>
63 This is a footnote
64 on multiple lines.
65 """],
66 ["""\
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.
72 """,
73 """\
74 <document source="test data">
75 <footnote ids="footnote-1" names="1">
76 <label>
78 <paragraph>
79 This is a footnote
80 on multiple lines with more space.
81 <footnote ids="footnote-2" names="2">
82 <label>
84 <paragraph>
85 This is a footnote
86 on multiple lines with less space.
87 """],
88 ["""\
89 .. [1]
90 This is a footnote on multiple lines
91 whose block starts on line 2.
92 """,
93 """\
94 <document source="test data">
95 <footnote ids="footnote-1" names="1">
96 <label>
98 <paragraph>
99 This is a footnote on multiple lines
100 whose block starts on line 2.
101 """],
102 ["""\
103 An empty footnote raises a warning:
105 .. [1]
107 """,
108 """\
109 <document source="test data">
110 <paragraph>
111 An empty footnote raises a warning:
112 <footnote ids="footnote-1" names="1">
113 <label>
115 <system_message level="2" line="4" source="test data" type="WARNING">
116 <paragraph>
117 Footnote content expected.
118 """],
119 ["""\
120 .. [1] spamalot
121 No blank line.
122 """,
123 """\
124 <document source="test data">
125 <footnote ids="footnote-1" names="1">
126 <label>
128 <paragraph>
129 spamalot
130 <system_message level="2" line="2" source="test data" type="WARNING">
131 <paragraph>
132 Explicit markup ends without a blank line; unexpected unindent.
133 <paragraph>
134 No blank line.
135 """],
138 totest['auto_numbered_footnotes'] = [
139 ["""\
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.
148 """,
149 """\
150 <document source="test data">
151 <paragraph>
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">
157 <paragraph>
158 Auto-numbered footnote 1.
159 <footnote auto="1" ids="footnote-2">
160 <paragraph>
161 Auto-numbered footnote 2.
162 <footnote auto="1" ids="footnote-3">
163 <paragraph>
164 Auto-numbered footnote 3.
165 <paragraph>
166 <footnote_reference auto="1" ids="footnote-reference-3">
167 is the third auto-numbered footnote reference.
168 """],
169 ["""\
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_.
182 """,
183 """\
184 <document source="test data">
185 <paragraph>
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">
189 <paragraph>
190 First auto-numbered footnote.
191 <footnote auto="1" ids="second" names="second">
192 <paragraph>
193 Second auto-numbered footnote.
194 <footnote auto="1" ids="third" names="third">
195 <paragraph>
196 Third auto-numbered footnote.
197 <paragraph>
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.
204 <paragraph>
205 Here are some internal cross-references to the targets generated by
206 the footnotes: \n\
207 <reference name="first" refname="first">
208 first
209 , \n\
210 <reference name="second" refname="second">
211 second
212 , \n\
213 <reference name="third" refname="third">
214 third
216 """],
217 ["""\
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).
230 """,
231 """\
232 <document source="test data">
233 <paragraph>
234 Mixed anonymous and labelled auto-numbered footnotes:
235 <paragraph>
236 <footnote_reference auto="1" ids="footnote-reference-1" refname="four">
237 should be 4, \n\
238 <footnote_reference auto="1" ids="footnote-reference-2">
239 should be 1,
240 <footnote_reference auto="1" ids="footnote-reference-3">
241 should be 3, \n\
242 <footnote_reference auto="1" ids="footnote-reference-4">
243 is one too many,
244 <footnote_reference auto="1" ids="footnote-reference-5" refname="two">
245 should be 2, and \n\
246 <footnote_reference auto="1" ids="footnote-reference-6" refname="six">
247 doesn't exist.
248 <footnote auto="1" ids="footnote-1">
249 <paragraph>
250 Auto-numbered footnote 1.
251 <footnote auto="1" ids="two" names="two">
252 <paragraph>
253 Auto-numbered footnote 2.
254 <footnote auto="1" ids="footnote-2">
255 <paragraph>
256 Auto-numbered footnote 3.
257 <footnote auto="1" ids="four" names="four">
258 <paragraph>
259 Auto-numbered footnote 4.
260 <footnote auto="1" dupnames="five" ids="five">
261 <paragraph>
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">
265 <paragraph>
266 Duplicate explicit target name: "five".
267 <paragraph>
268 Auto-numbered footnote 5 again (duplicate).
269 """],
270 ["""\
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.
286 """,
287 """\
288 <document source="test data">
289 <paragraph>
290 Mixed manually-numbered, anonymous auto-numbered,
291 and labelled auto-numbered footnotes:
292 <paragraph>
293 <footnote_reference auto="1" ids="footnote-reference-1" refname="four">
294 should be 4, \n\
295 <footnote_reference auto="1" ids="footnote-reference-2">
296 should be 2,
297 <footnote_reference ids="footnote-reference-3" refname="1">
299 is 1, \n\
300 <footnote_reference ids="footnote-reference-4" refname="3">
302 is 3,
303 <footnote_reference auto="1" ids="footnote-reference-5">
304 should be 6, \n\
305 <footnote_reference auto="1" ids="footnote-reference-6">
306 is one too many,
307 <footnote_reference auto="1" ids="footnote-reference-7" refname="five">
308 should be 5, and \n\
309 <footnote_reference auto="1" ids="footnote-reference-8" refname="six">
310 doesn't exist.
311 <footnote ids="footnote-1" names="1">
312 <label>
314 <paragraph>
315 Manually-numbered footnote 1.
316 <footnote auto="1" ids="footnote-2">
317 <paragraph>
318 Auto-numbered footnote 2.
319 <footnote auto="1" ids="four" names="four">
320 <paragraph>
321 Auto-numbered footnote 4.
322 <footnote ids="footnote-3" names="3">
323 <label>
325 <paragraph>
326 Manually-numbered footnote 3
327 <footnote auto="1" dupnames="five" ids="five">
328 <paragraph>
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">
332 <paragraph>
333 Duplicate explicit target name: "five".
334 <paragraph>
335 Auto-numbered footnote 5 again (duplicate).
336 <footnote auto="1" ids="footnote-4">
337 <paragraph>
338 Auto-numbered footnote 6.
339 """],
342 totest['auto_symbol_footnotes'] = [
343 ["""\
344 .. [*] This is an auto-symbol footnote.
345 """,
346 """\
347 <document source="test data">
348 <footnote auto="*" ids="footnote-1">
349 <paragraph>
350 This is an auto-symbol footnote.
351 """],
355 if __name__ == '__main__':
356 unittest.main()