| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
""" |
| 8 |
Test the 'code' directive in parsers/rst/directives/body.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[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 |
from docutils.utils.code_analyzer import with_pygments |
| 24 |
|
| 25 |
if with_pygments: |
| 26 |
import pygments |
| 27 |
|
| 28 |
pygments_version = tuple(map(int, pygments.__version__.split('.')[:2])) |
| 29 |
else: |
| 30 |
pygments_version = (0, 0) |
| 31 |
|
| 32 |
PYGMENTS_2_14_PLUS = pygments_version >= (2, 14) |
| 33 |
if pygments_version >= (2, 19): |
| 34 |
def_ws = '<inline classes="whitespace">\n ' |
| 35 |
else: |
| 36 |
def_ws = ' ' |
| 37 |
|
| 38 |
|
| 39 |
class ParserTestCase(unittest.TestCase): |
| 40 |
def test_parser(self): |
| 41 |
parser = Parser() |
| 42 |
settings = get_default_settings(Parser) |
| 43 |
settings.warning_stream = '' |
| 44 |
settings.report_level = 5 |
| 45 |
for name, cases in totest.items(): |
| 46 |
for casenum, (case_input, case_expected) in enumerate(cases): |
| 47 |
with self.subTest(id=f'totest[{name!r}][{casenum}]'): |
| 48 |
if name == 'code_parsing' and not with_pygments: |
| 49 |
self.skipTest('syntax highlight requires pygments') |
| 50 |
document = new_document('test data', settings.copy()) |
| 51 |
parser.parse(case_input, document) |
| 52 |
output = document.pformat() |
| 53 |
self.assertEqual(case_expected, output) |
| 54 |
|
| 55 |
|
| 56 |
totest = {} |
| 57 |
|
| 58 |
totest['code'] = [ |
| 59 |
["""\ |
| 60 |
.. code:: |
| 61 |
|
| 62 |
This is a code block. |
| 63 |
""", |
| 64 |
"""\ |
| 65 |
<document source="test data"> |
| 66 |
<literal_block classes="code" xml:space="preserve"> |
| 67 |
This is a code block. |
| 68 |
"""], |
| 69 |
["""\ |
| 70 |
.. code:: |
| 71 |
:class: testclass |
| 72 |
:name: without argument |
| 73 |
|
| 74 |
This is a code block with generic options. |
| 75 |
""", |
| 76 |
"""\ |
| 77 |
<document source="test data"> |
| 78 |
<literal_block classes="code testclass" ids="without-argument" names="without\\ argument" xml:space="preserve"> |
| 79 |
This is a code block with generic options. |
| 80 |
"""], |
| 81 |
["""\ |
| 82 |
.. code:: text |
| 83 |
:class: testclass |
| 84 |
|
| 85 |
This is a code block with text. |
| 86 |
""", |
| 87 |
"""\ |
| 88 |
<document source="test data"> |
| 89 |
<literal_block classes="code text testclass" xml:space="preserve"> |
| 90 |
This is a code block with text. |
| 91 |
"""], |
| 92 |
["""\ |
| 93 |
.. code:: |
| 94 |
:number-lines: |
| 95 |
|
| 96 |
This is a code block with text. |
| 97 |
""", |
| 98 |
"""\ |
| 99 |
<document source="test data"> |
| 100 |
<literal_block classes="code" xml:space="preserve"> |
| 101 |
<inline classes="ln"> |
| 102 |
1 \n\ |
| 103 |
This is a code block with text. |
| 104 |
"""], |
| 105 |
["""\ |
| 106 |
.. code:: |
| 107 |
:number-lines: 30 |
| 108 |
|
| 109 |
This is a code block with text. |
| 110 |
""", |
| 111 |
"""\ |
| 112 |
<document source="test data"> |
| 113 |
<literal_block classes="code" xml:space="preserve"> |
| 114 |
<inline classes="ln"> |
| 115 |
30 \n\ |
| 116 |
This is a code block with text. |
| 117 |
"""], |
| 118 |
["""\ |
| 119 |
.. code:: |
| 120 |
""", |
| 121 |
"""\ |
| 122 |
<document source="test data"> |
| 123 |
<system_message level="3" line="1" source="test data" type="ERROR"> |
| 124 |
<paragraph> |
| 125 |
Content block expected for the "code" directive; none found. |
| 126 |
<literal_block xml:space="preserve"> |
| 127 |
.. code:: |
| 128 |
"""], |
| 129 |
] |
| 130 |
|
| 131 |
totest['code_parsing'] = [ |
| 132 |
["""\ |
| 133 |
.. code:: python3 |
| 134 |
:class: testclass |
| 135 |
|
| 136 |
print('hello world') # to stdout |
| 137 |
""", |
| 138 |
"""\ |
| 139 |
<document source="test data"> |
| 140 |
<literal_block classes="code python3 testclass" xml:space="preserve"> |
| 141 |
\n\ |
| 142 |
<inline classes="name builtin"> |
| 143 |
print |
| 144 |
<inline classes="punctuation"> |
| 145 |
( |
| 146 |
<inline classes="literal string single"> |
| 147 |
'hello world' |
| 148 |
<inline classes="punctuation"> |
| 149 |
) |
| 150 |
\n\ |
| 151 |
<inline classes="comment single"> |
| 152 |
# to stdout |
| 153 |
"""], |
| 154 |
["""\ |
| 155 |
.. code:: python3 |
| 156 |
:class: testclass |
| 157 |
:name: my_function |
| 158 |
:number-lines: 7 |
| 159 |
|
| 160 |
def my_function(): |
| 161 |
'''Test the lexer. |
| 162 |
''' |
| 163 |
|
| 164 |
# and now for something completely different |
| 165 |
print(8/2) |
| 166 |
""", |
| 167 |
f"""\ |
| 168 |
<document source="test data"> |
| 169 |
<literal_block classes="code python3 testclass" ids="my-function" names="my_function" xml:space="preserve"> |
| 170 |
<inline classes="ln"> |
| 171 |
7 \n\ |
| 172 |
<inline classes="keyword"> |
| 173 |
def |
| 174 |
{def_ws} |
| 175 |
<inline classes="name function"> |
| 176 |
my_function |
| 177 |
<inline classes="punctuation"> |
| 178 |
(): |
| 179 |
<inline classes="whitespace"> |
| 180 |
\n\ |
| 181 |
<inline classes="ln"> |
| 182 |
8 \n\ |
| 183 |
<inline classes="whitespace"> |
| 184 |
\n\ |
| 185 |
<inline classes="literal string doc"> |
| 186 |
\'\'\'Test the lexer. |
| 187 |
<inline classes="ln"> |
| 188 |
9 \n\ |
| 189 |
<inline classes="literal string doc"> |
| 190 |
\'\'\' |
| 191 |
<inline classes="whitespace"> |
| 192 |
\n\ |
| 193 |
<inline classes="ln"> |
| 194 |
10 \n\ |
| 195 |
<inline classes="whitespace"> |
| 196 |
\n\ |
| 197 |
<inline classes="ln"> |
| 198 |
11 \n\ |
| 199 |
<inline classes="whitespace"> |
| 200 |
\n\ |
| 201 |
<inline classes="comment single"> |
| 202 |
# and now for something completely different |
| 203 |
<inline classes="whitespace"> |
| 204 |
\n\ |
| 205 |
<inline classes="ln"> |
| 206 |
12 \n\ |
| 207 |
<inline classes="whitespace"> |
| 208 |
\n\ |
| 209 |
<inline classes="name builtin"> |
| 210 |
print |
| 211 |
<inline classes="punctuation"> |
| 212 |
( |
| 213 |
<inline classes="literal number integer"> |
| 214 |
8 |
| 215 |
<inline classes="operator"> |
| 216 |
/ |
| 217 |
<inline classes="literal number integer"> |
| 218 |
2 |
| 219 |
<inline classes="punctuation"> |
| 220 |
) |
| 221 |
""" if PYGMENTS_2_14_PLUS else """\ |
| 222 |
<document source="test data"> |
| 223 |
<literal_block classes="code python3 testclass" ids="my-function" names="my_function" xml:space="preserve"> |
| 224 |
<inline classes="ln"> |
| 225 |
7 \n\ |
| 226 |
<inline classes="keyword"> |
| 227 |
def |
| 228 |
\n\ |
| 229 |
<inline classes="name function"> |
| 230 |
my_function |
| 231 |
<inline classes="punctuation"> |
| 232 |
(): |
| 233 |
\n\ |
| 234 |
<inline classes="ln"> |
| 235 |
8 \n\ |
| 236 |
\n\ |
| 237 |
<inline classes="literal string doc"> |
| 238 |
\'\'\'Test the lexer. |
| 239 |
<inline classes="ln"> |
| 240 |
9 \n\ |
| 241 |
<inline classes="literal string doc"> |
| 242 |
\'\'\' |
| 243 |
\n\ |
| 244 |
<inline classes="ln"> |
| 245 |
10 \n\ |
| 246 |
\n\ |
| 247 |
<inline classes="ln"> |
| 248 |
11 \n\ |
| 249 |
\n\ |
| 250 |
<inline classes="comment single"> |
| 251 |
# and now for something completely different |
| 252 |
\n\ |
| 253 |
<inline classes="ln"> |
| 254 |
12 \n\ |
| 255 |
\n\ |
| 256 |
<inline classes="name builtin"> |
| 257 |
print |
| 258 |
<inline classes="punctuation"> |
| 259 |
( |
| 260 |
<inline classes="literal number integer"> |
| 261 |
8 |
| 262 |
<inline classes="operator"> |
| 263 |
/ |
| 264 |
<inline classes="literal number integer"> |
| 265 |
2 |
| 266 |
<inline classes="punctuation"> |
| 267 |
) |
| 268 |
"""], |
| 269 |
["""\ |
| 270 |
.. code:: latex |
| 271 |
:class: testclass |
| 272 |
|
| 273 |
hello \\emph{world} % emphasize |
| 274 |
""", |
| 275 |
"""\ |
| 276 |
<document source="test data"> |
| 277 |
<literal_block classes="code latex testclass" xml:space="preserve"> |
| 278 |
hello \n\ |
| 279 |
<inline classes="keyword"> |
| 280 |
\\emph |
| 281 |
<inline classes="name builtin"> |
| 282 |
{ |
| 283 |
world |
| 284 |
<inline classes="name builtin"> |
| 285 |
} |
| 286 |
\n\ |
| 287 |
<inline classes="comment"> |
| 288 |
% emphasize |
| 289 |
"""], |
| 290 |
["""\ |
| 291 |
.. code:: rst |
| 292 |
:number-lines: |
| 293 |
|
| 294 |
This is a code block with text. |
| 295 |
""", |
| 296 |
"""\ |
| 297 |
<document source="test data"> |
| 298 |
<literal_block classes="code rst" xml:space="preserve"> |
| 299 |
<inline classes="ln"> |
| 300 |
1 \n\ |
| 301 |
This is a code block with text. |
| 302 |
"""], |
| 303 |
["""\ |
| 304 |
Code not parsed but warning silenced in ParserTestCase. |
| 305 |
|
| 306 |
.. code:: s-lang |
| 307 |
|
| 308 |
% abc.sl |
| 309 |
autoload("abc_mode", "abc"); |
| 310 |
""", |
| 311 |
"""\ |
| 312 |
<document source="test data"> |
| 313 |
<paragraph> |
| 314 |
Code not parsed but warning silenced in ParserTestCase. |
| 315 |
<literal_block classes="code s-lang" xml:space="preserve"> |
| 316 |
% abc.sl |
| 317 |
autoload("abc_mode", "abc"); |
| 318 |
"""], |
| 319 |
["""\ |
| 320 |
Place the language name in a class argument to avoid the no-lexer warning: |
| 321 |
|
| 322 |
.. code:: |
| 323 |
:class: s-lang |
| 324 |
|
| 325 |
% abc.sl |
| 326 |
autoload("abc_mode", "abc"); |
| 327 |
""", |
| 328 |
"""\ |
| 329 |
<document source="test data"> |
| 330 |
<paragraph> |
| 331 |
Place the language name in a class argument to avoid the no-lexer warning: |
| 332 |
<literal_block classes="code s-lang" xml:space="preserve"> |
| 333 |
% abc.sl |
| 334 |
autoload("abc_mode", "abc"); |
| 335 |
"""], |
| 336 |
] |
| 337 |
|
| 338 |
|
| 339 |
if __name__ == '__main__': |
| 340 |
unittest.main() |