#! /usr/bin/env python3 # $Id: test_figures.py 10102 2025-04-23 15:54:44Z milde $ # Author: David Goodger # Copyright: This module has been placed in the public domain. """ Tests for images.py figure 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) totest = {} totest['figures'] = [ # Note: A figure with no caption nor legend is not valid according to the DTD. ["""\ .. figure:: picture.png """, """\
"""], ["""\ .. figure:: picture.png A picture with a caption. """, """\
A picture with a caption. """], ["""\ .. figure:: picture.png - A picture with an invalid caption. """, """\
Figure caption must be a paragraph or empty comment. .. figure:: picture.png \n\ - A picture with an invalid caption. """], ["""\ .. figure:: picture.png .. A picture with a legend but no caption. """, """\
A picture with a legend but no caption. """], ["""\ .. figure:: picture.png .. The comment replacing the caption must be empty. This should be a legend. """, """\
Figure caption must be a paragraph or empty comment. .. figure:: picture.png \n\ .. The comment replacing the caption must be empty. \n\ This should be a legend. """], # Passing a class value to the caption is done with a class directive # that inserts a pending node (to be removed by the ClassAttribute transform). # A hyperlink target before the caption is removed by a transform, too. ["""\ .. Figure:: picture.png :height: 100 :width: 200 :scale: 50 :loading: embed .. class:: custom .. _figure:caption: A picture with image options and a caption with class value and target. """, """\
.. internal attributes: .transform: docutils.transforms.misc.ClassAttribute .details: class: ['custom'] directive: 'class' A picture with image options and a caption with class value and target. """], ["""\ .. Figure:: picture.png :alt: alternate text :name: img:picture :height: 100 :width: 200 :scale: 50 :loading: lazy :class: image-class :figwidth: 300 :figclass: class1 class2 :figname: Fig: pix A figure with options and this caption. """, """\
alternate text A figure with options and this caption. """], ["""\ .. figure:: picture.png :align: center A figure with explicit alignment. """, """\
A figure with explicit alignment. """], ["""\ .. figure:: picture.png :align: top A figure with wrong alignment. """, """\ Error in "figure" directive: invalid option value: (option: "align"; value: 'top') "top" unknown; choose from "left", "center", or "right". .. figure:: picture.png :align: top \n\ A figure with wrong alignment. """], ["""\ .. figure:: picture.png A picture with a caption and a legend. +-----------------------+-----------------------+ | Symbol | Meaning | +=======================+=======================+ | .. image:: tent.png | Campground | +-----------------------+-----------------------+ | .. image:: waves.png | Lake | +-----------------------+-----------------------+ | .. image:: peak.png | Mountain | +-----------------------+-----------------------+ """, """\
A picture with a caption and a legend. Symbol Meaning Campground Lake Mountain """], ["""\ .. figure:: picture.png .. A picture with a legend but no caption. (The empty comment replaces the caption, which must be a single paragraph.) """, """\
A picture with a legend but no caption. (The empty comment replaces the caption, which must be a single paragraph.) """], ["""\ Testing for line-leaks: .. figure:: picture.png A picture with a caption. .. figure:: picture.png A picture with a caption. .. figure:: picture.png A picture with a caption. .. figure:: picture.png .. figure:: picture.png .. figure:: picture.png .. figure:: picture.png A picture with a caption. .. figure:: picture.png .. figure:: picture.png A picture with a caption. .. figure:: picture.png """, """\ Testing for line-leaks:
A picture with a caption.
A picture with a caption.
A picture with a caption.
A picture with a caption.
A picture with a caption.
"""], ] if __name__ == '__main__': unittest.main()