| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
""" |
| 8 |
Tests for docutils.transforms.universal.Messages. |
| 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 Messages, 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 |
for name, (transforms, 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 |
|
| 38 |
|
| 39 |
document.transformer.add_transforms(transforms) |
| 40 |
document.transformer.add_transform(TestMessages) |
| 41 |
document.transformer.apply_transforms() |
| 42 |
output = document.pformat() |
| 43 |
self.assertEqual(case_expected, output) |
| 44 |
|
| 45 |
|
| 46 |
totest = {} |
| 47 |
|
| 48 |
totest['system_message_sections'] = ((Substitutions, Messages), [ |
| 49 |
["""\ |
| 50 |
This |unknown substitution| will generate a system message, thanks to |
| 51 |
the ``Substitutions`` transform. The ``Messages`` transform will |
| 52 |
generate a "System Messages" section. |
| 53 |
|
| 54 |
(A second copy of the system message is tacked on to the end of the |
| 55 |
document by the test framework.) |
| 56 |
""", |
| 57 |
"""\ |
| 58 |
<document source="test data"> |
| 59 |
<paragraph> |
| 60 |
This \n\ |
| 61 |
<problematic ids="problematic-1" refid="system-message-1"> |
| 62 |
|unknown substitution| |
| 63 |
will generate a system message, thanks to |
| 64 |
the \n\ |
| 65 |
<literal> |
| 66 |
Substitutions |
| 67 |
transform. The \n\ |
| 68 |
<literal> |
| 69 |
Messages |
| 70 |
transform will |
| 71 |
generate a "System Messages" section. |
| 72 |
<paragraph> |
| 73 |
(A second copy of the system message is tacked on to the end of the |
| 74 |
document by the test framework.) |
| 75 |
<section classes="system-messages"> |
| 76 |
<title> |
| 77 |
Docutils System Messages |
| 78 |
<system_message backrefs="problematic-1" ids="system-message-1" level="3" line="1" source="test data" type="ERROR"> |
| 79 |
<paragraph> |
| 80 |
Undefined substitution referenced: "unknown substitution". |
| 81 |
"""], |
| 82 |
]) |
| 83 |
|
| 84 |
|
| 85 |
if __name__ == '__main__': |
| 86 |
unittest.main() |