Debian Codegraph @

test/test_parsers/test_rst/test_directives/test_images.py

raw symbols SCIP
1 #! /usr/bin/env python3
2
3 # $Id: test_images.py 9943 2024-09-29 09:31:39Z milde $
4 # Author: David Goodger <[email protected]>
5 # Copyright: This module has been placed in the public domain.
6
7 """
8 Tests for images.py image 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 maxDiff = None
27
28 def test_parser(self):
29 parser = Parser()
30 settings = get_default_settings(Parser)
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 try:
42 int(None)
43 except TypeError as detail:
44 int_none = detail.args[0]
45 else:
46 int_none = ''
47
48
49 try:
50 int('fifty')
51 except ValueError as detail:
52 invalid_literal = detail.args[0]
53 else:
54 invalid_literal = ''
55
56
57 totest = {}
58
59 totest['images'] = [
60 ["""\
61 .. image:: picture.png
62 """,
63 """\
64 <document source="test data">
65 <image uri="picture.png">
66 """],
67 ["""\
68 .. image::
69 """,
70 """\
71 <document source="test data">
72 <system_message level="3" line="1" source="test data" type="ERROR">
73 <paragraph>
74 Error in "image" directive:
75 1 argument(s) required, 0 supplied.
76 <literal_block xml:space="preserve">
77 .. image::
78 """],
79 ["""\
80 .. image:: one two three.png
81 """,
82 """\
83 <document source="test data">
84 <image uri="onetwothree.png">
85 """],
86 ["""\
87 .. image:: picture.png
88 :height: 100
89 :width: 200
90 :scale: 50
91 """,
92 """\
93 <document source="test data">
94 <image height="100" scale="50" uri="picture.png" width="200">
95 """],
96 ["""\
97 .. image::
98 picture.png
99 :height: 100
100 :width: 200
101 :scale: 50
102 """,
103 """\
104 <document source="test data">
105 <image height="100" scale="50" uri="picture.png" width="200">
106 """],
107 ["""\
108 .. image::
109 :height: 100
110 :width: 200
111 :scale: 50
112 """,
113 """\
114 <document source="test data">
115 <system_message level="3" line="1" source="test data" type="ERROR">
116 <paragraph>
117 Error in "image" directive:
118 1 argument(s) required, 0 supplied.
119 <literal_block xml:space="preserve">
120 .. image::
121 :height: 100
122 :width: 200
123 :scale: 50
124 """],
125 # If there are multiple lines in the link block, they are stripped of
126 # leading and trailing whitespace and joined together:
127 ["""\
128 .. image:: a/very/long/path/to/
129 picture.png
130 :height: 100
131 :width: 200
132 :scale: 50
133 """,
134 """\
135 <document source="test data">
136 <image height="100" scale="50" uri="a/very/long/path/to/picture.png" width="200">
137 """],
138 # The following two misspellings were detected in Docutils <= 0.8
139 # (the option block was started by any line starting with a colon
140 # which led to problems with named roles in other directives):
141 ["""\
142 .. image:: picture.png
143 :scale 50
144 """,
145 """\
146 <document source="test data">
147 <image uri="picture.png:scale50">
148 """],
149 ["""\
150 .. image:: picture.png
151 :: 50
152 """,
153 """\
154 <document source="test data">
155 <image uri="picture.png::50">
156 """],
157 # a missing leading colon went undetected also in Docutils <= 0.8:
158 ["""\
159 .. image:: picture.png
160 scale: 50
161 """,
162 """\
163 <document source="test data">
164 <image uri="picture.pngscale:50">
165 """],
166 ["""\
167 .. image:: picture.png
168 :width: 200px
169 :height: 100 em
170 """,
171 """\
172 <document source="test data">
173 <image height="100em" uri="picture.png" width="200px">
174 """],
175 ["""\
176 .. image:: picture.png
177 :width: 200 Q
178 :height: 100 em
179 """,
180 """\
181 <document source="test data">
182 <image height="100em" uri="picture.png" width="200Q">
183 """],
184 # TODO: support CSS3 units (cf. [feature-requests:#57]
185 ["""\
186 .. image:: picture.png
187 :width: 50%
188 :height: 10vh
189 """,
190 """\
191 <document source="test data">
192 <image height="10vh" uri="picture.png" width="50%">
193 """],
194 ["""\
195 .. image:: picture.png
196 :width: 50%
197 :height: 40%
198 """,
199 """\
200 <document source="test data">
201 <system_message level="3" line="1" source="test data" type="ERROR">
202 <paragraph>
203 Error in "image" directive:
204 invalid option value: (option: "height"; value: \'40%\')
205 not a positive number or measure of one of the following units:
206 em, ex, ch, rem, vw, vh, vmin, vmax, cm, mm, Q, in, pt, pc, px.
207 <literal_block xml:space="preserve">
208 .. image:: picture.png
209 :width: 50%
210 :height: 40%
211 """],
212 ["""\
213 .. image:: picture.png
214 :width: 20mc
215 """,
216 """\
217 <document source="test data">
218 <system_message level="3" line="1" source="test data" type="ERROR">
219 <paragraph>
220 Error in "image" directive:
221 invalid option value: (option: "width"; value: \'20mc\')
222 not a positive number or measure of one of the following units:
223 em, ex, ch, rem, vw, vh, vmin, vmax, cm, mm, Q, in, pt, pc, px, %.
224 <literal_block xml:space="preserve">
225 .. image:: picture.png
226 :width: 20mc
227 """],
228 ["""\
229 .. image:: picture.png
230 :height: 100
231 :width: 200
232 :scale: 50
233 :alt: Alternate text for the picture
234 """,
235 """\
236 <document source="test data">
237 <image alt="Alternate text for the picture" height="100" scale="50" uri="picture.png" width="200">
238 """],
239 ["""\
240 .. image:: picture.png
241 :scale: -50
242 """,
243 """\
244 <document source="test data">
245 <system_message level="3" line="1" source="test data" type="ERROR">
246 <paragraph>
247 Error in "image" directive:
248 invalid option value: (option: "scale"; value: '-50')
249 negative value; must be positive or zero.
250 <literal_block xml:space="preserve">
251 .. image:: picture.png
252 :scale: -50
253 """],
254 ["""\
255 .. image:: picture.png
256 :scale:
257 """,
258 """\
259 <document source="test data">
260 <system_message level="3" line="1" source="test data" type="ERROR">
261 <paragraph>
262 Error in "image" directive:
263 invalid option value: (option: "scale"; value: None)
264 %s.
265 <literal_block xml:space="preserve">
266 .. image:: picture.png
267 :scale:
268 """ % int_none],
269 ["""\
270 .. image:: picture.png
271 :height: 100
272 :scale 50
273 """,
274 """\
275 <document source="test data">
276 <system_message level="3" line="1" source="test data" type="ERROR">
277 <paragraph>
278 Error in "image" directive:
279 invalid option block.
280 <literal_block xml:space="preserve">
281 .. image:: picture.png
282 :height: 100
283 :scale 50
284 """],
285 ["""\
286 .. image:: picture.png
287 :sale: 50
288 """,
289 """\
290 <document source="test data">
291 <system_message level="3" line="1" source="test data" type="ERROR">
292 <paragraph>
293 Error in "image" directive:
294 unknown option: "sale".
295 <literal_block xml:space="preserve">
296 .. image:: picture.png
297 :sale: 50
298 """],
299 ["""\
300 .. image:: picture.png
301 :scale is: 50
302 """,
303 """\
304 <document source="test data">
305 <system_message level="3" line="1" source="test data" type="ERROR">
306 <paragraph>
307 Error in "image" directive:
308 invalid option data: extension option field name may not contain multiple words.
309 <literal_block xml:space="preserve">
310 .. image:: picture.png
311 :scale is: 50
312 """],
313 ["""\
314 .. image:: picture.png
315 :scale: fifty
316 """,
317 """\
318 <document source="test data">
319 <system_message level="3" line="1" source="test data" type="ERROR">
320 <paragraph>
321 Error in "image" directive:
322 invalid option value: (option: "scale"; value: 'fifty')
323 %s.
324 <literal_block xml:space="preserve">
325 .. image:: picture.png
326 :scale: fifty
327 """ % invalid_literal],
328 ["""\
329 .. image:: picture.png
330 :scale: 50
331 :scale: 50
332 """,
333 """\
334 <document source="test data">
335 <system_message level="3" line="1" source="test data" type="ERROR">
336 <paragraph>
337 Error in "image" directive:
338 invalid option data: duplicate option "scale".
339 <literal_block xml:space="preserve">
340 .. image:: picture.png
341 :scale: 50
342 :scale: 50
343 """],
344 ["""\
345 .. image:: picture.png
346 :alt:
347
348 (Empty "alt" option.)
349 """,
350 """\
351 <document source="test data">
352 <image alt="" uri="picture.png">
353 <paragraph>
354 (Empty "alt" option.)
355 """],
356 ["""\
357 .. image:: picture.png
358 :target: bigpicture.png
359 :name: fig:pix
360 """,
361 """\
362 <document source="test data">
363 <reference refuri="bigpicture.png">
364 <image ids="fig-pix" names="fig:pix" uri="picture.png">
365 """],
366 ["""\
367 .. image:: picture.png
368 :target: indirect_
369 """,
370 """\
371 <document source="test data">
372 <reference name="indirect" refname="indirect">
373 <image uri="picture.png">
374 """],
375 ["""\
376 .. image:: picture.png
377 :target: a/multi/
378 line/uri
379
380 .. image:: picture.png
381 :target: `a multi line
382 internal reference`_
383 """,
384 """\
385 <document source="test data">
386 <reference refuri="a/multi/line/uri">
387 <image uri="picture.png">
388 <reference name="a multi line internal reference" refname="a multi line internal reference">
389 <image uri="picture.png">
390 """],
391 ["""\
392 .. image:: picture.png
393 :target:
394 """,
395 """\
396 <document source="test data">
397 <system_message level="3" line="1" source="test data" type="ERROR">
398 <paragraph>
399 Error in "image" directive:
400 invalid option value: (option: "target"; value: None)
401 argument required but none supplied.
402 <literal_block xml:space="preserve">
403 .. image:: picture.png
404 :target:
405 """],
406 ["""\
407 .. image:: picture.png
408 :align: left
409 """,
410 """\
411 <document source="test data">
412 <image align="left" uri="picture.png">
413 """],
414 ["""\
415 .. image:: picture.png
416 :align: top
417 """,
418 """\
419 <document source="test data">
420 <system_message level="3" line="1" source="test data" type="ERROR">
421 <paragraph>
422 Error in "image" directive: "top" is not a valid value for the "align" option. Valid values for "align" are: "left", "center", "right".
423 <literal_block xml:space="preserve">
424 .. image:: picture.png
425 :align: top
426 """],
427 ["""\
428 .. |img| image:: picture.png
429 :align: top
430 """,
431 """\
432 <document source="test data">
433 <substitution_definition names="img">
434 <image align="top" alt="img" uri="picture.png">
435 """],
436 ["""\
437 .. |img| image:: picture.png
438 :align: left
439 """,
440 """\
441 <document source="test data">
442 <system_message level="3" line="1" source="test data" type="ERROR">
443 <paragraph>
444 Error in "image" directive: "left" is not a valid value for the "align" option within a substitution definition. Valid values for "align" are: "top", "middle", "bottom".
445 <literal_block xml:space="preserve">
446 image:: picture.png
447 :align: left
448 <system_message level="2" line="1" source="test data" type="WARNING">
449 <paragraph>
450 Substitution definition "img" empty or invalid.
451 <literal_block xml:space="preserve">
452 .. |img| image:: picture.png
453 :align: left
454 """],
455 ["""\
456 .. image:: picture.png
457 :align: \xe4
458 """,
459 """\
460 <document source="test data">
461 <system_message level="3" line="1" source="test data" type="ERROR">
462 <paragraph>
463 Error in "image" directive:
464 invalid option value: (option: "align"; value: 'รค')
465 "\xe4" unknown; choose from "top", "middle", "bottom", "left", "center", or "right".
466 <literal_block xml:space="preserve">
467 .. image:: picture.png
468 :align: \xe4
469 """],
470 ["""
471 .. image:: test.png
472 :target: Uppercase_
473
474 .. _Uppercase: https://docutils.sourceforge.io/
475 """,
476 """\
477 <document source="test data">
478 <reference name="Uppercase" refname="uppercase">
479 <image uri="test.png">
480 <target ids="uppercase" names="uppercase" refuri="https://docutils.sourceforge.io/">
481 """],
482 [r"""
483 .. image:: path\ with\ spaces/name\ with\ spaces.png
484 :target: path\ with\ spaces/
485 target\ with\ spaces\ across\ lines.html
486 """,
487 """\
488 <document source="test data">
489 <reference refuri="path with spaces/target with spaces across lines.html">
490 <image uri="path with spaces/name with spaces.png">
491 """],
492 ["""
493 .. image:: test.png
494 :loading: embed
495 .. image:: test.png
496 :loading: link
497 .. image:: test.png
498 :loading: lazy
499 """,
500 """\
501 <document source="test data">
502 <image loading="embed" uri="test.png">
503 <image loading="link" uri="test.png">
504 <image loading="lazy" uri="test.png">
505 """],
506 ]
507
508
509 if __name__ == '__main__':
510 unittest.main()