| 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 |
def test_transforms(self): |
| 29 |
parser = Parser() |
| 30 |
settings = get_default_settings(Parser) |
| 31 |
settings.warning_stream = '' |
| 32 |
settings.line_length_limit = 80 |
| 33 |
for name, (transforms, 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 |
document = new_document('test data', settings.copy()) |
| 37 |
parser.parse(case_input, document) |
| 38 |
|
| 39 |
|
| 40 |
document.transformer.add_transforms(transforms) |
| 41 |
document.transformer.add_transform(TestMessages) |
| 42 |
document.transformer.apply_transforms() |
| 43 |
output = document.pformat() |
| 44 |
self.assertEqual(case_expected, output) |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
a = ' lol' |
| 49 |
b = ' 10^1 \n' + '\n \n'.join(10 * [a]) |
| 50 |
c = ' 10^2 \n' + '\n \n'.join(10 * [b]) |
| 51 |
|
| 52 |
totest = {} |
| 53 |
|
| 54 |
totest['substitutions'] = ((Substitutions,), [ |
| 55 |
["""\ |
| 56 |
The billion laughs attack for ReStructuredText: |
| 57 |
|
| 58 |
.. |a| replace:: lol |
| 59 |
.. |b| replace:: 10^1 |a| |a| |a| |a| |a| |a| |a| |a| |a| |a| |
| 60 |
.. |c| replace:: 10^2 |b| |b| |b| |b| |b| |b| |b| |b| |b| |b| |
| 61 |
.. ... |
| 62 |
|
| 63 |
|a| |c| continuation text |
| 64 |
""", |
| 65 |
f"""\ |
| 66 |
<document source="test data"> |
| 67 |
<paragraph> |
| 68 |
The billion laughs attack for ReStructuredText: |
| 69 |
<substitution_definition names="a"> |
| 70 |
lol |
| 71 |
<substitution_definition names="b"> |
| 72 |
{b} |
| 73 |
<substitution_definition names="c"> |
| 74 |
{c} |
| 75 |
<comment xml:space="preserve"> |
| 76 |
... |
| 77 |
<paragraph> |
| 78 |
lol |
| 79 |
\n\ |
| 80 |
<problematic ids="problematic-1" refid="system-message-1"> |
| 81 |
|c| |
| 82 |
continuation text |
| 83 |
<system_message backrefs="problematic-1" ids="system-message-1" level="3" line="9" source="test data" type="ERROR"> |
| 84 |
<paragraph> |
| 85 |
Substitution definition "c" exceeds the line-length-limit. |
| 86 |
"""], |
| 87 |
]) |
| 88 |
|
| 89 |
|
| 90 |
if __name__ == '__main__': |
| 91 |
unittest.main() |