| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
""" |
| 8 |
Tests for images.py figure directives. |
| 9 |
""" |
| 10 |
|
| 11 |
from pathlib import Path |
| 12 |
import sys |
| 13 |
import unittest |
| 14 |
|
| 15 |
if __name__ == '__main__': |
| 16 |
|
| 17 |
|
| 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 |
totest = {} |
| 42 |
|
| 43 |
totest['figures'] = [ |
| 44 |
|
| 45 |
["""\ |
| 46 |
.. figure:: picture.png |
| 47 |
""", |
| 48 |
"""\ |
| 49 |
<document source="test data"> |
| 50 |
<figure> |
| 51 |
<image uri="picture.png"> |
| 52 |
"""], |
| 53 |
["""\ |
| 54 |
.. figure:: picture.png |
| 55 |
|
| 56 |
A picture with a caption. |
| 57 |
""", |
| 58 |
"""\ |
| 59 |
<document source="test data"> |
| 60 |
<figure> |
| 61 |
<image uri="picture.png"> |
| 62 |
<caption> |
| 63 |
A picture with a caption. |
| 64 |
"""], |
| 65 |
["""\ |
| 66 |
.. figure:: picture.png |
| 67 |
|
| 68 |
- A picture with an invalid caption. |
| 69 |
""", |
| 70 |
"""\ |
| 71 |
<document source="test data"> |
| 72 |
<figure> |
| 73 |
<image uri="picture.png"> |
| 74 |
<system_message level="3" line="1" source="test data" type="ERROR"> |
| 75 |
<paragraph> |
| 76 |
Figure caption must be a paragraph or empty comment. |
| 77 |
<literal_block xml:space="preserve"> |
| 78 |
.. figure:: picture.png |
| 79 |
\n\ |
| 80 |
- A picture with an invalid caption. |
| 81 |
"""], |
| 82 |
["""\ |
| 83 |
.. figure:: picture.png |
| 84 |
|
| 85 |
.. |
| 86 |
|
| 87 |
A picture with a legend but no caption. |
| 88 |
""", |
| 89 |
"""\ |
| 90 |
<document source="test data"> |
| 91 |
<figure> |
| 92 |
<image uri="picture.png"> |
| 93 |
<legend> |
| 94 |
<paragraph> |
| 95 |
A picture with a legend but no caption. |
| 96 |
"""], |
| 97 |
["""\ |
| 98 |
.. figure:: picture.png |
| 99 |
|
| 100 |
.. The comment replacing the caption must be empty. |
| 101 |
|
| 102 |
This should be a legend. |
| 103 |
""", |
| 104 |
"""\ |
| 105 |
<document source="test data"> |
| 106 |
<figure> |
| 107 |
<image uri="picture.png"> |
| 108 |
<system_message level="3" line="1" source="test data" type="ERROR"> |
| 109 |
<paragraph> |
| 110 |
Figure caption must be a paragraph or empty comment. |
| 111 |
<literal_block xml:space="preserve"> |
| 112 |
.. figure:: picture.png |
| 113 |
\n\ |
| 114 |
.. The comment replacing the caption must be empty. |
| 115 |
\n\ |
| 116 |
This should be a legend. |
| 117 |
"""], |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
["""\ |
| 122 |
.. Figure:: picture.png |
| 123 |
:height: 100 |
| 124 |
:width: 200 |
| 125 |
:scale: 50 |
| 126 |
:loading: embed |
| 127 |
|
| 128 |
.. class:: custom |
| 129 |
.. _figure:caption: |
| 130 |
|
| 131 |
A picture with image options and a caption with class value and target. |
| 132 |
""", |
| 133 |
"""\ |
| 134 |
<document source="test data"> |
| 135 |
<figure> |
| 136 |
<image height="100" loading="embed" scale="50" uri="picture.png" width="200"> |
| 137 |
<pending> |
| 138 |
.. internal attributes: |
| 139 |
.transform: docutils.transforms.misc.ClassAttribute |
| 140 |
.details: |
| 141 |
class: ['custom'] |
| 142 |
directive: 'class' |
| 143 |
<target ids="figure-caption" names="figure:caption"> |
| 144 |
<caption> |
| 145 |
A picture with image options and a caption with class value and target. |
| 146 |
"""], |
| 147 |
["""\ |
| 148 |
.. Figure:: picture.png |
| 149 |
:alt: alternate text |
| 150 |
:name: img:picture |
| 151 |
:height: 100 |
| 152 |
:width: 200 |
| 153 |
:scale: 50 |
| 154 |
:loading: lazy |
| 155 |
:class: image-class |
| 156 |
:figwidth: 300 |
| 157 |
:figclass: class1 class2 |
| 158 |
:figname: Fig: pix |
| 159 |
|
| 160 |
A figure with options and this caption. |
| 161 |
""", |
| 162 |
"""\ |
| 163 |
<document source="test data"> |
| 164 |
<figure classes="class1 class2" ids="fig-pix" names="fig:\\ pix" width="300px"> |
| 165 |
<image alt="alternate text" classes="image-class" height="100" ids="img-picture" loading="lazy" names="img:picture" scale="50" uri="picture.png" width="200"> |
| 166 |
<caption> |
| 167 |
A figure with options and this caption. |
| 168 |
"""], |
| 169 |
["""\ |
| 170 |
.. figure:: picture.png |
| 171 |
:align: center |
| 172 |
|
| 173 |
A figure with explicit alignment. |
| 174 |
""", |
| 175 |
"""\ |
| 176 |
<document source="test data"> |
| 177 |
<figure align="center"> |
| 178 |
<image uri="picture.png"> |
| 179 |
<caption> |
| 180 |
A figure with explicit alignment. |
| 181 |
"""], |
| 182 |
["""\ |
| 183 |
.. figure:: picture.png |
| 184 |
:align: top |
| 185 |
|
| 186 |
A figure with wrong alignment. |
| 187 |
""", |
| 188 |
"""\ |
| 189 |
<document source="test data"> |
| 190 |
<system_message level="3" line="1" source="test data" type="ERROR"> |
| 191 |
<paragraph> |
| 192 |
Error in "figure" directive: |
| 193 |
invalid option value: (option: "align"; value: 'top') |
| 194 |
"top" unknown; choose from "left", "center", or "right". |
| 195 |
<literal_block xml:space="preserve"> |
| 196 |
.. figure:: picture.png |
| 197 |
:align: top |
| 198 |
\n\ |
| 199 |
A figure with wrong alignment. |
| 200 |
"""], |
| 201 |
["""\ |
| 202 |
.. figure:: picture.png |
| 203 |
|
| 204 |
A picture with a caption and a legend. |
| 205 |
|
| 206 |
+-----------------------+-----------------------+ |
| 207 |
| Symbol | Meaning | |
| 208 |
+=======================+=======================+ |
| 209 |
| .. image:: tent.png | Campground | |
| 210 |
+-----------------------+-----------------------+ |
| 211 |
| .. image:: waves.png | Lake | |
| 212 |
+-----------------------+-----------------------+ |
| 213 |
| .. image:: peak.png | Mountain | |
| 214 |
+-----------------------+-----------------------+ |
| 215 |
""", |
| 216 |
"""\ |
| 217 |
<document source="test data"> |
| 218 |
<figure> |
| 219 |
<image uri="picture.png"> |
| 220 |
<caption> |
| 221 |
A picture with a caption and a legend. |
| 222 |
<legend> |
| 223 |
<table> |
| 224 |
<tgroup cols="2"> |
| 225 |
<colspec colwidth="23"> |
| 226 |
<colspec colwidth="23"> |
| 227 |
<thead> |
| 228 |
<row> |
| 229 |
<entry> |
| 230 |
<paragraph> |
| 231 |
Symbol |
| 232 |
<entry> |
| 233 |
<paragraph> |
| 234 |
Meaning |
| 235 |
<tbody> |
| 236 |
<row> |
| 237 |
<entry> |
| 238 |
<image uri="tent.png"> |
| 239 |
<entry> |
| 240 |
<paragraph> |
| 241 |
Campground |
| 242 |
<row> |
| 243 |
<entry> |
| 244 |
<image uri="waves.png"> |
| 245 |
<entry> |
| 246 |
<paragraph> |
| 247 |
Lake |
| 248 |
<row> |
| 249 |
<entry> |
| 250 |
<image uri="peak.png"> |
| 251 |
<entry> |
| 252 |
<paragraph> |
| 253 |
Mountain |
| 254 |
"""], |
| 255 |
["""\ |
| 256 |
.. figure:: picture.png |
| 257 |
|
| 258 |
.. |
| 259 |
|
| 260 |
A picture with a legend but no caption. |
| 261 |
(The empty comment replaces the caption, which must |
| 262 |
be a single paragraph.) |
| 263 |
""", |
| 264 |
"""\ |
| 265 |
<document source="test data"> |
| 266 |
<figure> |
| 267 |
<image uri="picture.png"> |
| 268 |
<legend> |
| 269 |
<paragraph> |
| 270 |
A picture with a legend but no caption. |
| 271 |
(The empty comment replaces the caption, which must |
| 272 |
be a single paragraph.) |
| 273 |
"""], |
| 274 |
["""\ |
| 275 |
Testing for line-leaks: |
| 276 |
|
| 277 |
.. figure:: picture.png |
| 278 |
|
| 279 |
A picture with a caption. |
| 280 |
.. figure:: picture.png |
| 281 |
|
| 282 |
A picture with a caption. |
| 283 |
.. figure:: picture.png |
| 284 |
|
| 285 |
A picture with a caption. |
| 286 |
.. figure:: picture.png |
| 287 |
.. figure:: picture.png |
| 288 |
.. figure:: picture.png |
| 289 |
.. figure:: picture.png |
| 290 |
|
| 291 |
A picture with a caption. |
| 292 |
|
| 293 |
.. figure:: picture.png |
| 294 |
|
| 295 |
.. figure:: picture.png |
| 296 |
|
| 297 |
A picture with a caption. |
| 298 |
|
| 299 |
.. figure:: picture.png |
| 300 |
""", |
| 301 |
"""\ |
| 302 |
<document source="test data"> |
| 303 |
<paragraph> |
| 304 |
Testing for line-leaks: |
| 305 |
<figure> |
| 306 |
<image uri="picture.png"> |
| 307 |
<caption> |
| 308 |
A picture with a caption. |
| 309 |
<figure> |
| 310 |
<image uri="picture.png"> |
| 311 |
<caption> |
| 312 |
A picture with a caption. |
| 313 |
<figure> |
| 314 |
<image uri="picture.png"> |
| 315 |
<caption> |
| 316 |
A picture with a caption. |
| 317 |
<figure> |
| 318 |
<image uri="picture.png"> |
| 319 |
<figure> |
| 320 |
<image uri="picture.png"> |
| 321 |
<figure> |
| 322 |
<image uri="picture.png"> |
| 323 |
<figure> |
| 324 |
<image uri="picture.png"> |
| 325 |
<caption> |
| 326 |
A picture with a caption. |
| 327 |
<figure> |
| 328 |
<image uri="picture.png"> |
| 329 |
<figure> |
| 330 |
<image uri="picture.png"> |
| 331 |
<caption> |
| 332 |
A picture with a caption. |
| 333 |
<figure> |
| 334 |
<image uri="picture.png"> |
| 335 |
"""], |
| 336 |
] |
| 337 |
|
| 338 |
|
| 339 |
if __name__ == '__main__': |
| 340 |
unittest.main() |