1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
|
#! /usr/bin/env python3
# $Id: test_substitutions.py 9425 2023-06-30 14:56:47Z milde $
# Author: David Goodger <goodger@python.org>
# Copyright: This module has been placed in the public domain.
"""
Tests for states.py.
"""
from pathlib import Path
import sys
import unittest
if __name__ == '__main__':
# prepend the "docutils root" to the Python library path
# so we import the local `docutils` package.
sys.path.insert(0, str(Path(__file__).resolve().parents[3]))
from docutils.frontend import get_default_settings
from docutils.parsers.rst import Parser
from docutils.utils import new_document
class ParserTestCase(unittest.TestCase):
def test_parser(self):
parser = Parser()
settings = get_default_settings(Parser)
settings.warning_stream = ''
for name, cases in totest.items():
for casenum, (case_input, case_expected) in enumerate(cases):
with self.subTest(id=f'totest[{name!r}][{casenum}]'):
document = new_document('test data', settings.copy())
parser.parse(case_input, document)
output = document.pformat()
self.assertEqual(case_expected, output)
totest = {}
totest['substitution_definitions'] = [
["""\
Here's an image substitution definition:
.. |symbol| image:: symbol.png
""",
"""\
<document source="test data">
<paragraph>
Here's an image substitution definition:
<substitution_definition names="symbol">
<image alt="symbol" uri="symbol.png">
"""],
["""\
Embedded directive starts on the next line:
.. |symbol|
image:: symbol.png
""",
"""\
<document source="test data">
<paragraph>
Embedded directive starts on the next line:
<substitution_definition names="symbol">
<image alt="symbol" uri="symbol.png">
"""],
["""\
Trailing spaces should not be significant:
.. |symbol| image:: \n\
symbol.png
""",
"""\
<document source="test data">
<paragraph>
Trailing spaces should not be significant:
<substitution_definition names="symbol">
<image alt="symbol" uri="symbol.png">
"""],
["""\
Here's a series of substitution definitions:
.. |symbol 1| image:: symbol1.png
.. |SYMBOL 2| image:: symbol2.png
:height: 50
:width: 100
.. |symbol 3| image:: symbol3.png
""",
"""\
<document source="test data">
<paragraph>
Here's a series of substitution definitions:
<substitution_definition names="symbol\\ 1">
<image alt="symbol 1" uri="symbol1.png">
<substitution_definition names="SYMBOL\\ 2">
<image alt="SYMBOL 2" height="50" uri="symbol2.png" width="100">
<substitution_definition names="symbol\\ 3">
<image alt="symbol 3" uri="symbol3.png">
"""],
["""\
.. |very long substitution text,
split across lines| image:: symbol.png
""",
"""\
<document source="test data">
<substitution_definition names="very\\ long\\ substitution\\ text,\\ split\\ across\\ lines">
<image alt="very long substitution text, split across lines" uri="symbol.png">
"""],
["""\
.. |symbol 1| image:: symbol.png
Followed by a block quote.
""",
"""\
<document source="test data">
<system_message level="3" line="1" source="test data" type="ERROR">
<paragraph>
Error in "image" directive:
no content permitted.
<literal_block xml:space="preserve">
image:: symbol.png
\n\
Followed by a block quote.
<system_message level="2" line="1" source="test data" type="WARNING">
<paragraph>
Substitution definition "symbol 1" empty or invalid.
<literal_block xml:space="preserve">
.. |symbol 1| image:: symbol.png
\n\
Followed by a block quote.
"""],
["""\
.. |symbol 1| image:: symbol.png
Followed by a paragraph.
.. |symbol 2| image:: symbol.png
..
Followed by a block quote.
""",
"""\
<document source="test data">
<substitution_definition names="symbol\\ 1">
<image alt="symbol 1" uri="symbol.png">
<paragraph>
Followed by a paragraph.
<substitution_definition names="symbol\\ 2">
<image alt="symbol 2" uri="symbol.png">
<comment xml:space="preserve">
<block_quote>
<paragraph>
Followed by a block quote.
"""],
["""\
Substitutions support case differences:
.. |eacute| replace:: \u00E9
.. |Eacute| replace:: \u00C9
""",
"""\
<document source="test data">
<paragraph>
Substitutions support case differences:
<substitution_definition names="eacute">
\u00E9
<substitution_definition names="Eacute">
\u00C9
"""],
["""\
Raw substitution, backslashes should be preserved:
.. |alpha| raw:: latex
$\\\\alpha$
""",
"""\
<document source="test data">
<paragraph>
Raw substitution, backslashes should be preserved:
<substitution_definition names="alpha">
<raw format="latex" xml:space="preserve">
$\\\\alpha$
"""],
["""\
Here are some duplicate substitution definitions:
.. |symbol| image:: symbol.png
.. |symbol| image:: symbol.png
""",
"""\
<document source="test data">
<paragraph>
Here are some duplicate substitution definitions:
<substitution_definition dupnames="symbol">
<image alt="symbol" uri="symbol.png">
<system_message level="3" line="4" source="test data" type="ERROR">
<paragraph>
Duplicate substitution definition name: "symbol".
<substitution_definition names="symbol">
<image alt="symbol" uri="symbol.png">
"""],
["""\
Here are some bad cases:
.. |symbol| image:: symbol.png
No blank line after.
.. |empty|
.. |unknown| directive:: symbol.png
.. |invalid 1| there's no directive here
.. |invalid 2| there's no directive here
With some block quote text, line 1.
And some more, line 2.
.. |invalid 3| there's no directive here
.. | bad name | bad data
.. |
""",
"""\
<document source="test data">
<paragraph>
Here are some bad cases:
<substitution_definition names="symbol">
<image alt="symbol" uri="symbol.png">
<system_message level="2" line="4" source="test data" type="WARNING">
<paragraph>
Explicit markup ends without a blank line; unexpected unindent.
<paragraph>
No blank line after.
<system_message level="2" line="6" source="test data" type="WARNING">
<paragraph>
Substitution definition "empty" missing contents.
<literal_block xml:space="preserve">
.. |empty|
<system_message level="1" line="8" source="test data" type="INFO">
<paragraph>
No directive entry for "directive" in module "docutils.parsers.rst.languages.en".
Trying "directive" as canonical directive name.
<system_message level="3" line="8" source="test data" type="ERROR">
<paragraph>
Unknown directive type "directive".
<literal_block xml:space="preserve">
directive:: symbol.png
<system_message level="2" line="8" source="test data" type="WARNING">
<paragraph>
Substitution definition "unknown" empty or invalid.
<literal_block xml:space="preserve">
.. |unknown| directive:: symbol.png
<system_message level="2" line="10" source="test data" type="WARNING">
<paragraph>
Substitution definition "invalid 1" empty or invalid.
<literal_block xml:space="preserve">
.. |invalid 1| there's no directive here
<system_message level="2" line="11" source="test data" type="WARNING">
<paragraph>
Substitution definition "invalid 2" empty or invalid.
<literal_block xml:space="preserve">
.. |invalid 2| there's no directive here
With some block quote text, line 1.
And some more, line 2.
<system_message level="2" line="15" source="test data" type="WARNING">
<paragraph>
Substitution definition "invalid 3" empty or invalid.
<literal_block xml:space="preserve">
.. |invalid 3| there's no directive here
<comment xml:space="preserve">
| bad name | bad data
<comment xml:space="preserve">
|
"""],
["""\
Elements that are prohibited inside of substitution definitions:
.. |target| replace:: _`target`
.. |reference| replace:: anonymous__
.. |auto-numbered footnote| replace:: [#]_
""",
"""\
<document source="test data">
<paragraph>
Elements that are prohibited inside of substitution definitions:
<system_message level="3" line="3" source="test data" type="ERROR">
<paragraph>
Substitution definition contains illegal element <target>:
<literal_block xml:space="preserve">
<target ids="target" names="target">
target
<literal_block xml:space="preserve">
.. |target| replace:: _`target`
<system_message level="3" line="4" source="test data" type="ERROR">
<paragraph>
Substitution definition contains illegal element <reference>:
<literal_block xml:space="preserve">
<reference anonymous="1" name="anonymous">
anonymous
<literal_block xml:space="preserve">
.. |reference| replace:: anonymous__
<system_message level="3" line="5" source="test data" type="ERROR">
<paragraph>
Substitution definition contains illegal element <footnote_reference>:
<literal_block xml:space="preserve">
<footnote_reference auto="1" ids="footnote-reference-1">
<literal_block xml:space="preserve">
.. |auto-numbered footnote| replace:: [#]_
"""],
]
if __name__ == '__main__':
unittest.main()
|