| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
""" |
| 8 |
Tests for docutils.transforms.references.Substitutions. |
| 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[2])) |
| 19 |
|
| 20 |
from docutils.frontend import get_default_settings |
| 21 |
from docutils.parsers.rst import Parser |
| 22 |
from docutils.transforms.references import Substitutions |
| 23 |
from docutils.transforms.universal import TestMessages |
| 24 |
from docutils.utils import new_document |
| 25 |
|
| 26 |
|
| 27 |
class TransformTestCase(unittest.TestCase): |
| 28 |
maxDiff = None |
| 29 |
|
| 30 |
def test_transforms(self): |
| 31 |
parser = Parser() |
| 32 |
settings = get_default_settings(Parser) |
| 33 |
settings.warning_stream = '' |
| 34 |
for name, (transforms, cases) in totest.items(): |
| 35 |
for casenum, (case_input, case_expected) in enumerate(cases): |
| 36 |
with self.subTest(id=f'totest[{name!r}][{casenum}]'): |
| 37 |
document = new_document('test data', settings.copy()) |
| 38 |
parser.parse(case_input, document) |
| 39 |
|
| 40 |
|
| 41 |
document.transformer.add_transforms(transforms) |
| 42 |
document.transformer.add_transform(TestMessages) |
| 43 |
document.transformer.apply_transforms() |
| 44 |
output = document.pformat() |
| 45 |
self.assertEqual(case_expected, output) |
| 46 |
|
| 47 |
|
| 48 |
totest = {} |
| 49 |
|
| 50 |
totest['substitutions'] = ((Substitutions,), [ |
| 51 |
["""\ |
| 52 |
The |biohazard| symbol is deservedly scary-looking. |
| 53 |
|
| 54 |
.. |biohazard| image:: biohazard.png |
| 55 |
""", |
| 56 |
"""\ |
| 57 |
<document source="test data"> |
| 58 |
<paragraph> |
| 59 |
The \n\ |
| 60 |
<image alt="biohazard" uri="biohazard.png"> |
| 61 |
symbol is deservedly scary-looking. |
| 62 |
<substitution_definition names="biohazard"> |
| 63 |
<image alt="biohazard" uri="biohazard.png"> |
| 64 |
"""], |
| 65 |
["""\ |
| 66 |
Here's an |unknown| substitution. |
| 67 |
""", |
| 68 |
"""\ |
| 69 |
<document source="test data"> |
| 70 |
<paragraph> |
| 71 |
Here's an \n\ |
| 72 |
<problematic ids="problematic-1" refid="system-message-1"> |
| 73 |
|unknown| |
| 74 |
substitution. |
| 75 |
<system_message backrefs="problematic-1" ids="system-message-1" level="3" line="1" source="test data" type="ERROR"> |
| 76 |
<paragraph> |
| 77 |
Undefined substitution referenced: "unknown". |
| 78 |
"""], |
| 79 |
["""\ |
| 80 |
Substitutions support case differences: |
| 81 |
|
| 82 |
.. |eacute| replace:: \u00E9 |
| 83 |
.. |Eacute| replace:: \u00C9 |
| 84 |
|
| 85 |
|Eacute|\\t\\ |eacute|, and even |EACUTE|. |
| 86 |
""", |
| 87 |
"""\ |
| 88 |
<document source="test data"> |
| 89 |
<paragraph> |
| 90 |
Substitutions support case differences: |
| 91 |
<substitution_definition names="eacute"> |
| 92 |
\u00E9 |
| 93 |
<substitution_definition names="Eacute"> |
| 94 |
\u00C9 |
| 95 |
<paragraph> |
| 96 |
\u00C9 |
| 97 |
t |
| 98 |
\u00E9 |
| 99 |
, and even \n\ |
| 100 |
\u00C9 |
| 101 |
. |
| 102 |
"""], |
| 103 |
["""\ |
| 104 |
Indirect substitution definitions with multiple references: |
| 105 |
|
| 106 |
|substitute| my coke for gin |
| 107 |
|substitute| you for my mum |
| 108 |
at least I'll get my washing done |
| 109 |
|
| 110 |
.. |substitute| replace:: |replace| |
| 111 |
.. |replace| replace:: swap |
| 112 |
""", |
| 113 |
"""\ |
| 114 |
<document source="test data"> |
| 115 |
<paragraph> |
| 116 |
Indirect substitution definitions with multiple references: |
| 117 |
<paragraph> |
| 118 |
swap |
| 119 |
my coke for gin |
| 120 |
swap |
| 121 |
you for my mum |
| 122 |
at least I'll get my washing done |
| 123 |
<substitution_definition names="substitute"> |
| 124 |
swap |
| 125 |
<substitution_definition names="replace"> |
| 126 |
swap |
| 127 |
"""], |
| 128 |
["""\ |
| 129 |
.. |l| unicode:: U+00AB .. left chevron |
| 130 |
.. |r| unicode:: U+00BB .. right chevron |
| 131 |
.. |.| replace:: |l|\\ ``.``\\ |r| |
| 132 |
|
| 133 |
.. Delete either of the following lines, and there is no error. |
| 134 |
|
| 135 |
Regular expression |.| will match any character |
| 136 |
|
| 137 |
.. Note:: Note that |.| matches *exactly* one character |
| 138 |
""", |
| 139 |
"""\ |
| 140 |
<document source="test data"> |
| 141 |
<substitution_definition names="l"> |
| 142 |
\xab |
| 143 |
<substitution_definition names="r"> |
| 144 |
\xbb |
| 145 |
<substitution_definition names="."> |
| 146 |
\xab |
| 147 |
<literal> |
| 148 |
. |
| 149 |
\xbb |
| 150 |
<comment xml:space="preserve"> |
| 151 |
Delete either of the following lines, and there is no error. |
| 152 |
<paragraph> |
| 153 |
Regular expression \n\ |
| 154 |
\xab |
| 155 |
<literal> |
| 156 |
. |
| 157 |
\xbb |
| 158 |
will match any character |
| 159 |
<note> |
| 160 |
<paragraph> |
| 161 |
Note that \n\ |
| 162 |
\xab |
| 163 |
<literal> |
| 164 |
. |
| 165 |
\xbb |
| 166 |
matches \n\ |
| 167 |
<emphasis> |
| 168 |
exactly |
| 169 |
one character |
| 170 |
"""], |
| 171 |
["""\ |
| 172 |
.. |sub| replace:: |sub| |
| 173 |
""", |
| 174 |
"""\ |
| 175 |
<document source="test data"> |
| 176 |
<system_message level="3" line="1" names="sub" source="test data" type="ERROR"> |
| 177 |
<paragraph> |
| 178 |
Circular substitution definition detected: |
| 179 |
<literal_block xml:space="preserve"> |
| 180 |
.. |sub| replace:: |sub| |
| 181 |
"""], |
| 182 |
["""\ |
| 183 |
.. |sub| replace:: |indirect1| |
| 184 |
.. |indirect1| replace:: |indirect2| |
| 185 |
.. |indirect2| replace:: |Sub| |
| 186 |
""", |
| 187 |
"""\ |
| 188 |
<document source="test data"> |
| 189 |
<system_message level="3" line="1" names="sub" source="test data" type="ERROR"> |
| 190 |
<paragraph> |
| 191 |
Circular substitution definition detected: |
| 192 |
<literal_block xml:space="preserve"> |
| 193 |
.. |sub| replace:: |indirect1| |
| 194 |
<system_message level="3" line="2" names="indirect1" source="test data" type="ERROR"> |
| 195 |
<paragraph> |
| 196 |
Circular substitution definition detected: |
| 197 |
<literal_block xml:space="preserve"> |
| 198 |
.. |indirect1| replace:: |indirect2| |
| 199 |
<system_message level="3" line="3" names="indirect2" source="test data" type="ERROR"> |
| 200 |
<paragraph> |
| 201 |
Circular substitution definition detected: |
| 202 |
<literal_block xml:space="preserve"> |
| 203 |
.. |indirect2| replace:: |Sub| |
| 204 |
"""], |
| 205 |
["""\ |
| 206 |
.. |indirect1| replace:: |indirect2| |
| 207 |
.. |indirect2| replace:: |Sub| |
| 208 |
.. |sub| replace:: |indirect1| |
| 209 |
|
| 210 |
Use |sub| and |indirect1| and |sub| again (and |sub| one more time). |
| 211 |
""", |
| 212 |
"""\ |
| 213 |
<document source="test data"> |
| 214 |
<system_message level="3" line="1" names="indirect1" source="test data" type="ERROR"> |
| 215 |
<paragraph> |
| 216 |
Circular substitution definition detected: |
| 217 |
<literal_block xml:space="preserve"> |
| 218 |
.. |indirect1| replace:: |indirect2| |
| 219 |
<system_message level="3" line="2" names="indirect2" source="test data" type="ERROR"> |
| 220 |
<paragraph> |
| 221 |
Circular substitution definition detected: |
| 222 |
<literal_block xml:space="preserve"> |
| 223 |
.. |indirect2| replace:: |Sub| |
| 224 |
<system_message level="3" line="3" names="sub" source="test data" type="ERROR"> |
| 225 |
<paragraph> |
| 226 |
Circular substitution definition detected: |
| 227 |
<literal_block xml:space="preserve"> |
| 228 |
.. |sub| replace:: |indirect1| |
| 229 |
<paragraph> |
| 230 |
Use \n\ |
| 231 |
<problematic ids="problematic-4" refid="system-message-4"> |
| 232 |
|Sub| |
| 233 |
and \n\ |
| 234 |
<problematic ids="problematic-1" refid="system-message-1"> |
| 235 |
|indirect1| |
| 236 |
and \n\ |
| 237 |
<problematic ids="problematic-2" refid="system-message-2"> |
| 238 |
|sub| |
| 239 |
again (and \n\ |
| 240 |
<problematic ids="problematic-3" refid="system-message-3"> |
| 241 |
|sub| |
| 242 |
one more time). |
| 243 |
<system_message backrefs="problematic-1" ids="system-message-1" level="3" line="5" source="test data" type="ERROR"> |
| 244 |
<paragraph> |
| 245 |
Circular substitution definition referenced: "indirect1". |
| 246 |
<system_message backrefs="problematic-2" ids="system-message-2" level="3" line="5" source="test data" type="ERROR"> |
| 247 |
<paragraph> |
| 248 |
Circular substitution definition referenced: "sub". |
| 249 |
<system_message backrefs="problematic-3" ids="system-message-3" level="3" line="5" source="test data" type="ERROR"> |
| 250 |
<paragraph> |
| 251 |
Circular substitution definition referenced: "sub". |
| 252 |
<system_message backrefs="problematic-4" ids="system-message-4" level="3" line="5" source="test data" type="ERROR"> |
| 253 |
<paragraph> |
| 254 |
Circular substitution definition referenced: "Sub". |
| 255 |
"""], |
| 256 |
["""\ |
| 257 |
Substitution reference with |reference-in-content|. |
| 258 |
|
| 259 |
.. |reference-in-content| replace:: text and hyperlink-reference_ |
| 260 |
""", |
| 261 |
"""\ |
| 262 |
<document source="test data"> |
| 263 |
<paragraph> |
| 264 |
Substitution reference with \n\ |
| 265 |
text and \n\ |
| 266 |
<reference name="hyperlink-reference" refname="hyperlink-reference"> |
| 267 |
hyperlink-reference |
| 268 |
. |
| 269 |
<substitution_definition names="reference-in-content"> |
| 270 |
text and \n\ |
| 271 |
<reference name="hyperlink-reference" refname="hyperlink-reference"> |
| 272 |
hyperlink-reference |
| 273 |
"""], |
| 274 |
]) |
| 275 |
|
| 276 |
totest['unicode'] = ((Substitutions,), [ |
| 277 |
["""\ |
| 278 |
Insert an em-dash (|mdash|), a copyright symbol (|copy|), a non-breaking |
| 279 |
space (|nbsp|), a backwards-not-equals (|bne|), and a captial omega (|Omega|). |
| 280 |
|
| 281 |
.. |mdash| unicode:: 0x02014 |
| 282 |
.. |copy| unicode:: \\u00A9 |
| 283 |
.. |nbsp| unicode::   |
| 284 |
.. |bne| unicode:: U0003D U020E5 |
| 285 |
.. |Omega| unicode:: U+003A9 |
| 286 |
""", |
| 287 |
"""\ |
| 288 |
<document source="test data"> |
| 289 |
<paragraph> |
| 290 |
Insert an em-dash ( |
| 291 |
\u2014 |
| 292 |
), a copyright symbol ( |
| 293 |
\u00a9 |
| 294 |
), a non-breaking |
| 295 |
space ( |
| 296 |
\u00a0 |
| 297 |
), a backwards-not-equals ( |
| 298 |
= |
| 299 |
\u20e5 |
| 300 |
), and a captial omega ( |
| 301 |
\u03a9 |
| 302 |
). |
| 303 |
<substitution_definition names="mdash"> |
| 304 |
\u2014 |
| 305 |
<substitution_definition names="copy"> |
| 306 |
\u00a9 |
| 307 |
<substitution_definition names="nbsp"> |
| 308 |
\u00a0 |
| 309 |
<substitution_definition names="bne"> |
| 310 |
= |
| 311 |
\u20e5 |
| 312 |
<substitution_definition names="Omega"> |
| 313 |
\u03a9 |
| 314 |
"""], |
| 315 |
[""" |
| 316 |
Testing comments and extra text. |
| 317 |
|
| 318 |
Copyright |copy| 2003, |BogusMegaCorp (TM)|. |
| 319 |
|
| 320 |
.. |copy| unicode:: 0xA9 .. copyright sign |
| 321 |
.. |BogusMegaCorp (TM)| unicode:: BogusMegaCorp U+2122 |
| 322 |
.. with trademark sign |
| 323 |
""", |
| 324 |
"""\ |
| 325 |
<document source="test data"> |
| 326 |
<paragraph> |
| 327 |
Testing comments and extra text. |
| 328 |
<paragraph> |
| 329 |
Copyright \n\ |
| 330 |
\u00a9 |
| 331 |
2003, \n\ |
| 332 |
BogusMegaCorp |
| 333 |
\u2122 |
| 334 |
. |
| 335 |
<substitution_definition names="copy"> |
| 336 |
\u00a9 |
| 337 |
<substitution_definition names="BogusMegaCorp\\ (TM)"> |
| 338 |
BogusMegaCorp |
| 339 |
\u2122 |
| 340 |
"""], |
| 341 |
["""\ |
| 342 |
Insert an em-dash |---| automatically trimming whitespace. |
| 343 |
Some substitutions |TM| only need |rarrow| trimming on one side. |
| 344 |
|
| 345 |
.. |---| unicode:: U+02014 |
| 346 |
:trim: |
| 347 |
.. |TM| unicode:: U+02122 |
| 348 |
:ltrim: |
| 349 |
.. |rarrow| unicode:: U+2192 |
| 350 |
:rtrim: |
| 351 |
""", |
| 352 |
"""\ |
| 353 |
<document source="test data"> |
| 354 |
<paragraph> |
| 355 |
Insert an em-dash |
| 356 |
\u2014 |
| 357 |
automatically trimming whitespace. |
| 358 |
Some substitutions |
| 359 |
\u2122 |
| 360 |
only need \n\ |
| 361 |
\u2192 |
| 362 |
trimming on one side. |
| 363 |
<substitution_definition ltrim="1" names="---" rtrim="1"> |
| 364 |
\u2014 |
| 365 |
<substitution_definition ltrim="1" names="TM"> |
| 366 |
\u2122 |
| 367 |
<substitution_definition names="rarrow" rtrim="1"> |
| 368 |
\u2192 |
| 369 |
"""], |
| 370 |
["""\ |
| 371 |
Substitution definition with an illegal element: |
| 372 |
|
| 373 |
.. |target| replace:: _`target` |
| 374 |
|
| 375 |
Make sure this substitution definition is not registered: |target| |
| 376 |
""", |
| 377 |
"""\ |
| 378 |
<document source="test data"> |
| 379 |
<paragraph> |
| 380 |
Substitution definition with an illegal element: |
| 381 |
<system_message level="3" line="3" source="test data" type="ERROR"> |
| 382 |
<paragraph> |
| 383 |
Targets (names and identifiers) are not supported in a substitution definition. |
| 384 |
<literal_block xml:space="preserve"> |
| 385 |
.. |target| replace:: _`target` |
| 386 |
<paragraph> |
| 387 |
Make sure this substitution definition is not registered: \n\ |
| 388 |
<problematic ids="problematic-1" refid="system-message-1"> |
| 389 |
|target| |
| 390 |
<system_message backrefs="problematic-1" ids="system-message-1" level="3" line="5" source="test data" type="ERROR"> |
| 391 |
<paragraph> |
| 392 |
Undefined substitution referenced: "target". |
| 393 |
"""], |
| 394 |
]) |
| 395 |
|
| 396 |
|
| 397 |
if __name__ == '__main__': |
| 398 |
unittest.main() |