| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
""" |
| 8 |
Tests for interpreted text in docutils/parsers/rst/states.py. |
| 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[3])) |
| 19 |
|
| 20 |
from docutils.frontend import get_default_settings |
| 21 |
from docutils.parsers.rst import Parser |
| 22 |
from docutils.utils import new_document |
| 23 |
from docutils.utils.code_analyzer import with_pygments |
| 24 |
|
| 25 |
|
| 26 |
class ParserTestCase(unittest.TestCase): |
| 27 |
maxDiff = None |
| 28 |
|
| 29 |
def test_parser(self): |
| 30 |
parser = Parser() |
| 31 |
settings = get_default_settings(Parser) |
| 32 |
settings.warning_stream = '' |
| 33 |
for name, cases in totest.items(): |
| 34 |
for casenum, (case_input, case_expected) in enumerate(cases): |
| 35 |
with self.subTest(id=f'totest[{name!r}][{casenum}]'): |
| 36 |
if name == 'code_parsing' and not with_pygments: |
| 37 |
self.skipTest('syntax highlight requires pygments') |
| 38 |
document = new_document('test data', settings.copy()) |
| 39 |
parser.parse(case_input, document) |
| 40 |
output = document.pformat() |
| 41 |
self.assertEqual(case_expected, output) |
| 42 |
|
| 43 |
|
| 44 |
totest = {} |
| 45 |
|
| 46 |
totest['basics'] = [ |
| 47 |
["""\ |
| 48 |
`interpreted` |
| 49 |
""", |
| 50 |
"""\ |
| 51 |
<document source="test data"> |
| 52 |
<paragraph> |
| 53 |
<title_reference> |
| 54 |
interpreted |
| 55 |
"""], |
| 56 |
["""\ |
| 57 |
:title:`interpreted` |
| 58 |
""", |
| 59 |
"""\ |
| 60 |
<document source="test data"> |
| 61 |
<paragraph> |
| 62 |
<title_reference> |
| 63 |
interpreted |
| 64 |
"""], |
| 65 |
["""\ |
| 66 |
`interpreted`:title: |
| 67 |
""", |
| 68 |
"""\ |
| 69 |
<document source="test data"> |
| 70 |
<paragraph> |
| 71 |
<title_reference> |
| 72 |
interpreted |
| 73 |
"""], |
| 74 |
[r""" |
| 75 |
`interpreted \`title`` |
| 76 |
""", |
| 77 |
"""\ |
| 78 |
<document source="test data"> |
| 79 |
<paragraph> |
| 80 |
<title_reference> |
| 81 |
interpreted `title` |
| 82 |
"""], |
| 83 |
["""\ |
| 84 |
:title:`:not-role: interpreted` |
| 85 |
""", |
| 86 |
"""\ |
| 87 |
<document source="test data"> |
| 88 |
<paragraph> |
| 89 |
<title_reference> |
| 90 |
:not-role: interpreted |
| 91 |
"""], |
| 92 |
[r""" |
| 93 |
`interpreted` but not \`interpreted` [`] or ({[`] or [`]}) or ` |
| 94 |
""", |
| 95 |
"""\ |
| 96 |
<document source="test data"> |
| 97 |
<paragraph> |
| 98 |
<title_reference> |
| 99 |
interpreted |
| 100 |
but not `interpreted` [`] or ({[`] or [`]}) or ` |
| 101 |
"""], |
| 102 |
["""\ |
| 103 |
`interpreted`-text `interpreted`: text `interpreted`:text `text`'s interpreted |
| 104 |
""", |
| 105 |
"""\ |
| 106 |
<document source="test data"> |
| 107 |
<paragraph> |
| 108 |
<title_reference> |
| 109 |
interpreted |
| 110 |
-text \n\ |
| 111 |
<title_reference> |
| 112 |
interpreted |
| 113 |
: text \n\ |
| 114 |
<title_reference> |
| 115 |
interpreted |
| 116 |
:text \n\ |
| 117 |
<title_reference> |
| 118 |
text |
| 119 |
's interpreted |
| 120 |
"""], |
| 121 |
["""\ |
| 122 |
`interpreted without closing backquote |
| 123 |
""", |
| 124 |
"""\ |
| 125 |
<document source="test data"> |
| 126 |
<paragraph> |
| 127 |
<problematic ids="problematic-1" refid="system-message-1"> |
| 128 |
` |
| 129 |
interpreted without closing backquote |
| 130 |
<system_message backrefs="problematic-1" ids="system-message-1" level="2" line="1" source="test data" type="WARNING"> |
| 131 |
<paragraph> |
| 132 |
Inline interpreted text or phrase reference start-string without end-string. |
| 133 |
"""], |
| 134 |
["""\ |
| 135 |
`interpreted`:not a role if it contains whitespace: |
| 136 |
""", |
| 137 |
"""\ |
| 138 |
<document source="test data"> |
| 139 |
<paragraph> |
| 140 |
<title_reference> |
| 141 |
interpreted |
| 142 |
:not a role if it contains whitespace: |
| 143 |
"""], |
| 144 |
["""\ |
| 145 |
:title:`` (empty interpreted text not recognized) |
| 146 |
""", |
| 147 |
"""\ |
| 148 |
<document source="test data"> |
| 149 |
<paragraph> |
| 150 |
:title:`` (empty interpreted text not recognized) |
| 151 |
"""], |
| 152 |
[r""" |
| 153 |
:title:`\ ` (interpreted text containing empty string) |
| 154 |
""", |
| 155 |
"""\ |
| 156 |
<document source="test data"> |
| 157 |
<paragraph> |
| 158 |
<title_reference> |
| 159 |
(interpreted text containing empty string) |
| 160 |
"""], |
| 161 |
[r""" |
| 162 |
`\ `:title: (interpreted text containing empty string (postfix)) |
| 163 |
""", |
| 164 |
"""\ |
| 165 |
<document source="test data"> |
| 166 |
<paragraph> |
| 167 |
<title_reference> |
| 168 |
(interpreted text containing empty string (postfix)) |
| 169 |
"""], |
| 170 |
[r""" |
| 171 |
:title:`\ non-empty` |
| 172 |
""", |
| 173 |
"""\ |
| 174 |
<document source="test data"> |
| 175 |
<paragraph> |
| 176 |
<title_reference> |
| 177 |
non-empty |
| 178 |
"""], |
| 179 |
[r""" |
| 180 |
:title:`\ ` (trailing unquoted space) |
| 181 |
""", |
| 182 |
"""\ |
| 183 |
<document source="test data"> |
| 184 |
<paragraph> |
| 185 |
:title: |
| 186 |
<problematic ids="problematic-1" refid="system-message-1"> |
| 187 |
` |
| 188 |
` (trailing unquoted space) |
| 189 |
<system_message backrefs="problematic-1" ids="system-message-1" level="2" line="2" source="test data" type="WARNING"> |
| 190 |
<paragraph> |
| 191 |
Inline interpreted text or phrase reference start-string without end-string. |
| 192 |
"""], |
| 193 |
["""\ |
| 194 |
Explicit roles for standard inline markup: |
| 195 |
:emphasis:`emphasis`, |
| 196 |
:strong:`strong`, |
| 197 |
:literal:`inline literal text`. |
| 198 |
""", |
| 199 |
"""\ |
| 200 |
<document source="test data"> |
| 201 |
<paragraph> |
| 202 |
Explicit roles for standard inline markup: |
| 203 |
<emphasis> |
| 204 |
emphasis |
| 205 |
, |
| 206 |
<strong> |
| 207 |
strong |
| 208 |
, |
| 209 |
<literal> |
| 210 |
inline literal text |
| 211 |
. |
| 212 |
"""], |
| 213 |
["""\ |
| 214 |
Simple explicit roles: |
| 215 |
:ab:`abbreviation`, |
| 216 |
:ac:`acronym`, |
| 217 |
:sup:`superscript`, |
| 218 |
:sub:`subscript`, |
| 219 |
:title:`title reference`. |
| 220 |
""", |
| 221 |
"""\ |
| 222 |
<document source="test data"> |
| 223 |
<paragraph> |
| 224 |
Simple explicit roles: |
| 225 |
<abbreviation> |
| 226 |
abbreviation |
| 227 |
, |
| 228 |
<acronym> |
| 229 |
acronym |
| 230 |
, |
| 231 |
<superscript> |
| 232 |
superscript |
| 233 |
, |
| 234 |
<subscript> |
| 235 |
subscript |
| 236 |
, |
| 237 |
<title_reference> |
| 238 |
title reference |
| 239 |
. |
| 240 |
"""], |
| 241 |
] |
| 242 |
|
| 243 |
totest['code'] = [ |
| 244 |
[r""" |
| 245 |
Code role for inline code snippets: |
| 246 |
:code:`$\alpha = \int_0^\infty f(x) dx$`. |
| 247 |
""", |
| 248 |
"""\ |
| 249 |
<document source="test data"> |
| 250 |
<paragraph> |
| 251 |
Code role for inline code snippets: |
| 252 |
<literal classes="code"> |
| 253 |
$\\alpha = \\int_0^\\infty f(x) dx$ |
| 254 |
. |
| 255 |
"""], |
| 256 |
] |
| 257 |
|
| 258 |
totest['code_parsing'] = [ |
| 259 |
["""\ |
| 260 |
.. role:: tex(code) |
| 261 |
:language: latex |
| 262 |
|
| 263 |
Custom role based on code role: |
| 264 |
:tex:`$\alpha = f(x)$`. |
| 265 |
""", |
| 266 |
"""\ |
| 267 |
<document source="test data"> |
| 268 |
<paragraph> |
| 269 |
Custom role based on code role: |
| 270 |
<literal classes="code tex latex"> |
| 271 |
<inline classes="literal string"> |
| 272 |
$ |
| 273 |
<inline classes="name builtin"> |
| 274 |
\x07lpha \n\ |
| 275 |
<inline classes="operator"> |
| 276 |
= |
| 277 |
<inline classes="name builtin"> |
| 278 |
f |
| 279 |
<inline classes="operator"> |
| 280 |
( |
| 281 |
<inline classes="name builtin"> |
| 282 |
x |
| 283 |
<inline classes="operator"> |
| 284 |
) |
| 285 |
<inline classes="literal string"> |
| 286 |
$ |
| 287 |
. |
| 288 |
"""], |
| 289 |
["""\ |
| 290 |
Custom role based on code role: |
| 291 |
|
| 292 |
.. role:: python(code) |
| 293 |
:class: testclass |
| 294 |
|
| 295 |
Python code :python:`print("The end")`. |
| 296 |
""", |
| 297 |
"""\ |
| 298 |
<document source="test data"> |
| 299 |
<paragraph> |
| 300 |
Custom role based on code role: |
| 301 |
<paragraph> |
| 302 |
Python code \n\ |
| 303 |
<literal classes="code testclass python"> |
| 304 |
<inline classes="name builtin"> |
| 305 |
print |
| 306 |
<inline classes="punctuation"> |
| 307 |
( |
| 308 |
<inline classes="literal string double"> |
| 309 |
"The end" |
| 310 |
<inline classes="punctuation"> |
| 311 |
) |
| 312 |
. |
| 313 |
"""], |
| 314 |
["""\ |
| 315 |
Turn off syntax highlight with language "none". |
| 316 |
|
| 317 |
.. role:: python(code) |
| 318 |
:language: none |
| 319 |
|
| 320 |
Python code :python:`print("The end")`. |
| 321 |
""", |
| 322 |
"""\ |
| 323 |
<document source="test data"> |
| 324 |
<paragraph> |
| 325 |
Turn off syntax highlight with language "none". |
| 326 |
<paragraph> |
| 327 |
Python code \n\ |
| 328 |
<literal classes="code python"> |
| 329 |
print("The end") |
| 330 |
. |
| 331 |
"""], |
| 332 |
] |
| 333 |
|
| 334 |
totest['references'] = [ |
| 335 |
["""\ |
| 336 |
:PEP:`0` |
| 337 |
""", |
| 338 |
"""\ |
| 339 |
<document source="test data"> |
| 340 |
<paragraph> |
| 341 |
<reference refuri="https://peps.python.org/pep-0000"> |
| 342 |
PEP 0 |
| 343 |
"""], |
| 344 |
["""\ |
| 345 |
:PEP:`-1` |
| 346 |
""", |
| 347 |
"""\ |
| 348 |
<document source="test data"> |
| 349 |
<paragraph> |
| 350 |
<problematic ids="problematic-1" refid="system-message-1"> |
| 351 |
:PEP:`-1` |
| 352 |
<system_message backrefs="problematic-1" ids="system-message-1" level="3" line="1" source="test data" type="ERROR"> |
| 353 |
<paragraph> |
| 354 |
PEP number must be a number from 0 to 9999; "-1" is invalid. |
| 355 |
"""], |
| 356 |
["""\ |
| 357 |
:RFC:`2822` |
| 358 |
""", |
| 359 |
"""\ |
| 360 |
<document source="test data"> |
| 361 |
<paragraph> |
| 362 |
<reference refuri="https://tools.ietf.org/html/rfc2822.html"> |
| 363 |
RFC 2822 |
| 364 |
"""], |
| 365 |
["""\ |
| 366 |
:RFC:`0` |
| 367 |
""", |
| 368 |
"""\ |
| 369 |
<document source="test data"> |
| 370 |
<paragraph> |
| 371 |
<problematic ids="problematic-1" refid="system-message-1"> |
| 372 |
:RFC:`0` |
| 373 |
<system_message backrefs="problematic-1" ids="system-message-1" level="3" line="1" source="test data" type="ERROR"> |
| 374 |
<paragraph> |
| 375 |
RFC number must be a number greater than or equal to 1; "0" is invalid. |
| 376 |
"""], |
| 377 |
["""\ |
| 378 |
:RFC:`2822#section1` |
| 379 |
""", |
| 380 |
"""\ |
| 381 |
<document source="test data"> |
| 382 |
<paragraph> |
| 383 |
<reference refuri="https://tools.ietf.org/html/rfc2822.html#section1"> |
| 384 |
RFC 2822 |
| 385 |
"""], |
| 386 |
] |
| 387 |
|
| 388 |
totest['unknown_roles'] = [ |
| 389 |
["""\ |
| 390 |
:role:`interpreted` |
| 391 |
""", |
| 392 |
"""\ |
| 393 |
<document source="test data"> |
| 394 |
<paragraph> |
| 395 |
<problematic ids="problematic-1" refid="system-message-1"> |
| 396 |
:role:`interpreted` |
| 397 |
<system_message level="1" line="1" source="test data" type="INFO"> |
| 398 |
<paragraph> |
| 399 |
No role entry for "role" in module "docutils.parsers.rst.languages.en". |
| 400 |
Trying "role" as canonical role name. |
| 401 |
<system_message backrefs="problematic-1" ids="system-message-1" level="3" line="1" source="test data" type="ERROR"> |
| 402 |
<paragraph> |
| 403 |
Unknown interpreted text role "role". |
| 404 |
"""], |
| 405 |
["""\ |
| 406 |
`interpreted`:role: |
| 407 |
""", |
| 408 |
"""\ |
| 409 |
<document source="test data"> |
| 410 |
<paragraph> |
| 411 |
<problematic ids="problematic-1" refid="system-message-1"> |
| 412 |
`interpreted`:role: |
| 413 |
<system_message level="1" line="1" source="test data" type="INFO"> |
| 414 |
<paragraph> |
| 415 |
No role entry for "role" in module "docutils.parsers.rst.languages.en". |
| 416 |
Trying "role" as canonical role name. |
| 417 |
<system_message backrefs="problematic-1" ids="system-message-1" level="3" line="1" source="test data" type="ERROR"> |
| 418 |
<paragraph> |
| 419 |
Unknown interpreted text role "role". |
| 420 |
"""], |
| 421 |
["""\ |
| 422 |
:role:`interpreted`:role: |
| 423 |
""", |
| 424 |
"""\ |
| 425 |
<document source="test data"> |
| 426 |
<paragraph> |
| 427 |
<problematic ids="problematic-1" refid="system-message-1"> |
| 428 |
:role:`interpreted`:role: |
| 429 |
<system_message backrefs="problematic-1" ids="system-message-1" level="2" line="1" source="test data" type="WARNING"> |
| 430 |
<paragraph> |
| 431 |
Multiple roles in interpreted text (both prefix and suffix present; only one allowed). |
| 432 |
"""], |
| 433 |
["""\ |
| 434 |
:very.long-role_name:`interpreted` |
| 435 |
""", |
| 436 |
"""\ |
| 437 |
<document source="test data"> |
| 438 |
<paragraph> |
| 439 |
<problematic ids="problematic-1" refid="system-message-1"> |
| 440 |
:very.long-role_name:`interpreted` |
| 441 |
<system_message level="1" line="1" source="test data" type="INFO"> |
| 442 |
<paragraph> |
| 443 |
No role entry for "very.long-role_name" in module "docutils.parsers.rst.languages.en". |
| 444 |
Trying "very.long-role_name" as canonical role name. |
| 445 |
<system_message backrefs="problematic-1" ids="system-message-1" level="3" line="1" source="test data" type="ERROR"> |
| 446 |
<paragraph> |
| 447 |
Unknown interpreted text role "very.long-role_name". |
| 448 |
"""], |
| 449 |
["""\ |
| 450 |
:restructuredtext-unimplemented-role:`interpreted` |
| 451 |
""", |
| 452 |
"""\ |
| 453 |
<document source="test data"> |
| 454 |
<paragraph> |
| 455 |
<problematic ids="problematic-1" refid="system-message-1"> |
| 456 |
:restructuredtext-unimplemented-role:`interpreted` |
| 457 |
<system_message level="1" line="1" source="test data" type="INFO"> |
| 458 |
<paragraph> |
| 459 |
No role entry for "restructuredtext-unimplemented-role" in module "docutils.parsers.rst.languages.en". |
| 460 |
Trying "restructuredtext-unimplemented-role" as canonical role name. |
| 461 |
<system_message backrefs="problematic-1" ids="system-message-1" level="3" line="1" source="test data" type="ERROR"> |
| 462 |
<paragraph> |
| 463 |
Interpreted text role "restructuredtext-unimplemented-role" not implemented. |
| 464 |
"""], |
| 465 |
] |
| 466 |
|
| 467 |
|
| 468 |
if __name__ == '__main__': |
| 469 |
unittest.main() |