#! /usr/bin/env python3 # $Id: test_images.py 9943 2024-09-29 09:31:39Z milde $ # Author: David Goodger # Copyright: This module has been placed in the public domain. """ Tests for images.py image directives. """ from pathlib import Path import sys import unittest if __name__ == '__main__': # prepend the "docutils root" to the Python library path # so we import the local `docutils` package. sys.path.insert(0, str(Path(__file__).resolve().parents[4])) from docutils.frontend import get_default_settings from docutils.parsers.rst import Parser from docutils.utils import new_document class ParserTestCase(unittest.TestCase): maxDiff = None def test_parser(self): parser = Parser() settings = get_default_settings(Parser) settings.warning_stream = '' for name, cases in totest.items(): for casenum, (case_input, case_expected) in enumerate(cases): with self.subTest(id=f'totest[{name!r}][{casenum}]'): document = new_document('test data', settings.copy()) parser.parse(case_input, document) output = document.pformat() self.assertEqual(case_expected, output) try: int(None) except TypeError as detail: int_none = detail.args[0] else: int_none = '' try: int('fifty') except ValueError as detail: invalid_literal = detail.args[0] else: invalid_literal = '' totest = {} totest['images'] = [ ["""\ .. image:: picture.png """, """\ """], ["""\ .. image:: """, """\ Error in "image" directive: 1 argument(s) required, 0 supplied. .. image:: """], ["""\ .. image:: one two three.png """, """\ """], ["""\ .. image:: picture.png :height: 100 :width: 200 :scale: 50 """, """\ """], ["""\ .. image:: picture.png :height: 100 :width: 200 :scale: 50 """, """\ """], ["""\ .. image:: :height: 100 :width: 200 :scale: 50 """, """\ Error in "image" directive: 1 argument(s) required, 0 supplied. .. image:: :height: 100 :width: 200 :scale: 50 """], # If there are multiple lines in the link block, they are stripped of # leading and trailing whitespace and joined together: ["""\ .. image:: a/very/long/path/to/ picture.png :height: 100 :width: 200 :scale: 50 """, """\ """], # The following two misspellings were detected in Docutils <= 0.8 # (the option block was started by any line starting with a colon # which led to problems with named roles in other directives): ["""\ .. image:: picture.png :scale 50 """, """\ """], ["""\ .. image:: picture.png :: 50 """, """\ """], # a missing leading colon went undetected also in Docutils <= 0.8: ["""\ .. image:: picture.png scale: 50 """, """\ """], ["""\ .. image:: picture.png :width: 200px :height: 100 em """, """\ """], ["""\ .. image:: picture.png :width: 200 Q :height: 100 em """, """\ """], # TODO: support CSS3 units (cf. [feature-requests:#57] ["""\ .. image:: picture.png :width: 50% :height: 10vh """, """\ """], ["""\ .. image:: picture.png :width: 50% :height: 40% """, """\ Error in "image" directive: invalid option value: (option: "height"; value: \'40%\') not a positive number or measure of one of the following units: em, ex, ch, rem, vw, vh, vmin, vmax, cm, mm, Q, in, pt, pc, px. .. image:: picture.png :width: 50% :height: 40% """], ["""\ .. image:: picture.png :width: 20mc """, """\ Error in "image" directive: invalid option value: (option: "width"; value: \'20mc\') not a positive number or measure of one of the following units: em, ex, ch, rem, vw, vh, vmin, vmax, cm, mm, Q, in, pt, pc, px, %. .. image:: picture.png :width: 20mc """], ["""\ .. image:: picture.png :height: 100 :width: 200 :scale: 50 :alt: Alternate text for the picture """, """\ Alternate text for the picture """], ["""\ .. image:: picture.png :scale: -50 """, """\ Error in "image" directive: invalid option value: (option: "scale"; value: '-50') negative value; must be positive or zero. .. image:: picture.png :scale: -50 """], ["""\ .. image:: picture.png :scale: """, """\ Error in "image" directive: invalid option value: (option: "scale"; value: None) %s. .. image:: picture.png :scale: """ % int_none], ["""\ .. image:: picture.png :height: 100 :scale 50 """, """\ Error in "image" directive: invalid option block. .. image:: picture.png :height: 100 :scale 50 """], ["""\ .. image:: picture.png :sale: 50 """, """\ Error in "image" directive: unknown option: "sale". .. image:: picture.png :sale: 50 """], ["""\ .. image:: picture.png :scale is: 50 """, """\ Error in "image" directive: invalid option data: extension option field name may not contain multiple words. .. image:: picture.png :scale is: 50 """], ["""\ .. image:: picture.png :scale: fifty """, """\ Error in "image" directive: invalid option value: (option: "scale"; value: 'fifty') %s. .. image:: picture.png :scale: fifty """ % invalid_literal], ["""\ .. image:: picture.png :scale: 50 :scale: 50 """, """\ Error in "image" directive: invalid option data: duplicate option "scale". .. image:: picture.png :scale: 50 :scale: 50 """], ["""\ .. image:: picture.png :alt: (Empty "alt" option.) """, """\ (Empty "alt" option.) """], ["""\ .. image:: picture.png :target: bigpicture.png :name: fig:pix """, """\ """], ["""\ .. image:: picture.png :target: indirect_ """, """\ """], ["""\ .. image:: picture.png :target: a/multi/ line/uri .. image:: picture.png :target: `a multi line internal reference`_ """, """\ """], ["""\ .. image:: picture.png :target: """, """\ Error in "image" directive: invalid option value: (option: "target"; value: None) argument required but none supplied. .. image:: picture.png :target: """], ["""\ .. image:: picture.png :align: left """, """\ """], ["""\ .. image:: picture.png :align: top """, """\ Error in "image" directive: "top" is not a valid value for the "align" option. Valid values for "align" are: "left", "center", "right". .. image:: picture.png :align: top """], ["""\ .. |img| image:: picture.png :align: top """, """\ img """], ["""\ .. |img| image:: picture.png :align: left """, """\ 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". image:: picture.png :align: left Substitution definition "img" empty or invalid. .. |img| image:: picture.png :align: left """], ["""\ .. image:: picture.png :align: \xe4 """, """\ Error in "image" directive: invalid option value: (option: "align"; value: 'รค') "\xe4" unknown; choose from "top", "middle", "bottom", "left", "center", or "right". .. image:: picture.png :align: \xe4 """], [""" .. image:: test.png :target: Uppercase_ .. _Uppercase: https://docutils.sourceforge.io/ """, """\ """], [r""" .. image:: path\ with\ spaces/name\ with\ spaces.png :target: path\ with\ spaces/ target\ with\ spaces\ across\ lines.html """, """\ """], [""" .. image:: test.png :loading: embed .. image:: test.png :loading: link .. image:: test.png :loading: lazy """, """\ """], ] if __name__ == '__main__': unittest.main()