| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
""" |
| 8 |
Tests for misc.py "replace" directive. |
| 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['replace'] = [ |
| 44 |
["""\ |
| 45 |
Test the |name| directive. |
| 46 |
|
| 47 |
.. |name| replace:: "**replace**" |
| 48 |
""", |
| 49 |
"""\ |
| 50 |
<document source="test data"> |
| 51 |
<paragraph> |
| 52 |
Test the \n\ |
| 53 |
<substitution_reference refname="name"> |
| 54 |
name |
| 55 |
directive. |
| 56 |
<substitution_definition names="name"> |
| 57 |
" |
| 58 |
<strong> |
| 59 |
replace |
| 60 |
" |
| 61 |
"""], |
| 62 |
["""\ |
| 63 |
.. |name| replace:: paragraph 1 |
| 64 |
|
| 65 |
paragraph 2 |
| 66 |
""", |
| 67 |
"""\ |
| 68 |
<document source="test data"> |
| 69 |
<system_message level="3" line="1" source="test data" type="ERROR"> |
| 70 |
<paragraph> |
| 71 |
Error in "replace" directive: may contain a single paragraph only. |
| 72 |
<system_message level="2" line="1" source="test data" type="WARNING"> |
| 73 |
<paragraph> |
| 74 |
Substitution definition "name" empty or invalid. |
| 75 |
<literal_block xml:space="preserve"> |
| 76 |
.. |name| replace:: paragraph 1 |
| 77 |
\n\ |
| 78 |
paragraph 2 |
| 79 |
"""], |
| 80 |
["""\ |
| 81 |
.. |name| replace:: |
| 82 |
""", |
| 83 |
"""\ |
| 84 |
<document source="test data"> |
| 85 |
<system_message level="3" line="1" source="test data" type="ERROR"> |
| 86 |
<paragraph> |
| 87 |
Content block expected for the "replace" directive; none found. |
| 88 |
<literal_block xml:space="preserve"> |
| 89 |
replace:: |
| 90 |
<system_message level="2" line="1" source="test data" type="WARNING"> |
| 91 |
<paragraph> |
| 92 |
Substitution definition "name" empty or invalid. |
| 93 |
<literal_block xml:space="preserve"> |
| 94 |
.. |name| replace:: |
| 95 |
"""], |
| 96 |
["""\ |
| 97 |
.. |Python| replace:: Python, *the* best language around |
| 98 |
|
| 99 |
.. _Python: http://www.python.org/ |
| 100 |
|
| 101 |
I recommend you try |Python|_. |
| 102 |
""", |
| 103 |
"""\ |
| 104 |
<document source="test data"> |
| 105 |
<substitution_definition names="Python"> |
| 106 |
Python, \n\ |
| 107 |
<emphasis> |
| 108 |
the |
| 109 |
best language around |
| 110 |
<target ids="python" names="python" refuri="http://www.python.org/"> |
| 111 |
<paragraph> |
| 112 |
I recommend you try \n\ |
| 113 |
<reference refname="python"> |
| 114 |
<substitution_reference refname="Python"> |
| 115 |
Python |
| 116 |
. |
| 117 |
"""], |
| 118 |
["""\ |
| 119 |
.. |name| replace:: *error in **inline ``markup |
| 120 |
""", |
| 121 |
"""\ |
| 122 |
<document source="test data"> |
| 123 |
<system_message ids="system-message-1" level="2" line="1" source="test data" type="WARNING"> |
| 124 |
<paragraph> |
| 125 |
Inline emphasis start-string without end-string. |
| 126 |
<system_message ids="system-message-2" level="2" line="1" source="test data" type="WARNING"> |
| 127 |
<paragraph> |
| 128 |
Inline strong start-string without end-string. |
| 129 |
<system_message ids="system-message-3" level="2" line="1" source="test data" type="WARNING"> |
| 130 |
<paragraph> |
| 131 |
Inline literal start-string without end-string. |
| 132 |
<system_message level="3" line="1" source="test data" type="ERROR"> |
| 133 |
<paragraph> |
| 134 |
Problematic content in substitution definition |
| 135 |
<literal_block xml:space="preserve"> |
| 136 |
.. |name| replace:: *error in **inline ``markup |
| 137 |
<block_quote> |
| 138 |
<paragraph> |
| 139 |
<problematic ids="problematic-1" refid="system-message-1"> |
| 140 |
* |
| 141 |
error in \n\ |
| 142 |
<problematic ids="problematic-2" refid="system-message-2"> |
| 143 |
** |
| 144 |
inline \n\ |
| 145 |
<problematic ids="problematic-3" refid="system-message-3"> |
| 146 |
`` |
| 147 |
markup |
| 148 |
"""], |
| 149 |
["""\ |
| 150 |
.. replace:: not valid outside of a substitution definition |
| 151 |
""", |
| 152 |
"""\ |
| 153 |
<document source="test data"> |
| 154 |
<system_message level="3" line="1" source="test data" type="ERROR"> |
| 155 |
<paragraph> |
| 156 |
Invalid context: the "replace" directive can only be used within a substitution definition. |
| 157 |
<literal_block xml:space="preserve"> |
| 158 |
.. replace:: not valid outside of a substitution definition |
| 159 |
"""], |
| 160 |
] |
| 161 |
|
| 162 |
|
| 163 |
if __name__ == '__main__': |
| 164 |
unittest.main() |