| 1 |
"""Test the code-block directive.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from pathlib import Path |
| 6 |
from typing import TYPE_CHECKING |
| 7 |
|
| 8 |
import pygments |
| 9 |
import pytest |
| 10 |
from docutils import nodes |
| 11 |
|
| 12 |
from sphinx.config import Config |
| 13 |
from sphinx.directives.code import LiteralIncludeReader |
| 14 |
from sphinx.testing.util import etree_parse |
| 15 |
|
| 16 |
if TYPE_CHECKING: |
| 17 |
from sphinx.testing.util import SphinxTestApp |
| 18 |
|
| 19 |
DUMMY_CONFIG = Config({}, {}) |
| 20 |
|
| 21 |
|
| 22 |
@pytest.fixture(scope='module') |
| 23 |
def testroot(rootdir: Path) -> Path: |
| 24 |
testroot_path = rootdir / 'test-directive-code' |
| 25 |
return testroot_path |
| 26 |
|
| 27 |
|
| 28 |
@pytest.fixture(scope='module') |
| 29 |
def literal_inc_path(testroot: Path) -> Path: |
| 30 |
return testroot / 'literal.inc' |
| 31 |
|
| 32 |
|
| 33 |
def test_LiteralIncludeReader(literal_inc_path: Path) -> None: |
| 34 |
options = {'lineno-match': True} |
| 35 |
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) |
| 36 |
content, lines = reader.read() |
| 37 |
assert content == literal_inc_path.read_text(encoding='utf8') |
| 38 |
assert lines == 13 |
| 39 |
assert reader.lineno_start == 1 |
| 40 |
|
| 41 |
|
| 42 |
def test_LiteralIncludeReader_lineno_start(literal_inc_path: Path) -> None: |
| 43 |
options = {'lineno-start': 4} |
| 44 |
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) |
| 45 |
content, lines = reader.read() |
| 46 |
assert content == literal_inc_path.read_text(encoding='utf8') |
| 47 |
assert lines == 13 |
| 48 |
assert reader.lineno_start == 4 |
| 49 |
|
| 50 |
|
| 51 |
def test_LiteralIncludeReader_pyobject1(literal_inc_path: Path) -> None: |
| 52 |
options = {'lineno-match': True, 'pyobject': 'Foo'} |
| 53 |
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) |
| 54 |
content, _lines = reader.read() |
| 55 |
assert content == 'class Foo:\n pass\n' |
| 56 |
assert reader.lineno_start == 5 |
| 57 |
|
| 58 |
|
| 59 |
def test_LiteralIncludeReader_pyobject2(literal_inc_path: Path) -> None: |
| 60 |
options = {'pyobject': 'Bar'} |
| 61 |
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) |
| 62 |
content, _lines = reader.read() |
| 63 |
assert content == 'class Bar:\n def baz():\n pass\n' |
| 64 |
assert reader.lineno_start == 1 # no lineno-match |
| 65 |
|
| 66 |
|
| 67 |
def test_LiteralIncludeReader_pyobject3(literal_inc_path: Path) -> None: |
| 68 |
options = {'pyobject': 'Bar.baz'} |
| 69 |
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) |
| 70 |
content, _lines = reader.read() |
| 71 |
assert content == ' def baz():\n pass\n' |
| 72 |
|
| 73 |
|
| 74 |
def test_LiteralIncludeReader_pyobject_and_lines(literal_inc_path: Path) -> None: |
| 75 |
options = {'pyobject': 'Bar', 'lines': '2-'} |
| 76 |
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) |
| 77 |
content, _lines = reader.read() |
| 78 |
assert content == ' def baz():\n pass\n' |
| 79 |
|
| 80 |
|
| 81 |
def test_LiteralIncludeReader_lines1(literal_inc_path: Path) -> None: |
| 82 |
options = {'lines': '1-3'} |
| 83 |
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) |
| 84 |
content, _lines = reader.read() |
| 85 |
assert content == ( |
| 86 |
'# Literally included file using Python highlighting\n' |
| 87 |
'\n' |
| 88 |
'foo = "Including Unicode characters: üöä"\n' |
| 89 |
) |
| 90 |
|
| 91 |
|
| 92 |
def test_LiteralIncludeReader_lines2(literal_inc_path: Path) -> None: |
| 93 |
options = {'lines': '1,3,5'} |
| 94 |
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) |
| 95 |
content, _lines = reader.read() |
| 96 |
assert content == ( |
| 97 |
'# Literally included file using Python highlighting\n' |
| 98 |
'foo = "Including Unicode characters: üöä"\n' |
| 99 |
'class Foo:\n' |
| 100 |
) |
| 101 |
|
| 102 |
|
| 103 |
def test_LiteralIncludeReader_lines_and_lineno_match1(literal_inc_path: Path) -> None: |
| 104 |
options = {'lines': '3-5', 'lineno-match': True} |
| 105 |
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) |
| 106 |
content, _lines = reader.read() |
| 107 |
assert content == 'foo = "Including Unicode characters: üöä"\n\nclass Foo:\n' |
| 108 |
assert reader.lineno_start == 3 |
| 109 |
|
| 110 |
|
| 111 |
@pytest.mark.sphinx('html', testroot='root') # init locale for errors |
| 112 |
def test_LiteralIncludeReader_lines_and_lineno_match2( |
| 113 |
literal_inc_path: Path, app: SphinxTestApp |
| 114 |
) -> None: |
| 115 |
options = {'lines': '0,3,5', 'lineno-match': True} |
| 116 |
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) |
| 117 |
with pytest.raises( |
| 118 |
ValueError, |
| 119 |
match='Cannot use "lineno-match" with a disjoint set of "lines"', |
| 120 |
): |
| 121 |
reader.read() |
| 122 |
|
| 123 |
|
| 124 |
@pytest.mark.sphinx('html', testroot='root') # init locale for errors |
| 125 |
def test_LiteralIncludeReader_lines_and_lineno_match3( |
| 126 |
literal_inc_path: Path, app: SphinxTestApp |
| 127 |
) -> None: |
| 128 |
options = {'lines': '100-', 'lineno-match': True} |
| 129 |
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) |
| 130 |
with pytest.raises( |
| 131 |
ValueError, |
| 132 |
match="Line spec '100-': no lines pulled from include file", |
| 133 |
): |
| 134 |
reader.read() |
| 135 |
|
| 136 |
|
| 137 |
def test_LiteralIncludeReader_start_at(literal_inc_path: Path) -> None: |
| 138 |
options = {'lineno-match': True, 'start-at': 'Foo', 'end-at': 'Bar'} |
| 139 |
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) |
| 140 |
content, _lines = reader.read() |
| 141 |
assert content == 'class Foo:\n pass\n\nclass Bar:\n' |
| 142 |
assert reader.lineno_start == 5 |
| 143 |
|
| 144 |
|
| 145 |
def test_LiteralIncludeReader_start_after(literal_inc_path: Path) -> None: |
| 146 |
options = {'lineno-match': True, 'start-after': 'Foo', 'end-before': 'Bar'} |
| 147 |
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) |
| 148 |
content, _lines = reader.read() |
| 149 |
assert content == ' pass\n\n' |
| 150 |
assert reader.lineno_start == 6 |
| 151 |
|
| 152 |
|
| 153 |
def test_LiteralIncludeReader_start_after_and_lines(literal_inc_path: Path) -> None: |
| 154 |
options = { |
| 155 |
'lineno-match': True, |
| 156 |
'lines': '6-', |
| 157 |
'start-after': 'Literally', |
| 158 |
'end-before': 'comment', |
| 159 |
} |
| 160 |
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) |
| 161 |
content, _lines = reader.read() |
| 162 |
assert content == '\nclass Bar:\n def baz():\n pass\n\n' |
| 163 |
assert reader.lineno_start == 7 |
| 164 |
|
| 165 |
|
| 166 |
def test_LiteralIncludeReader_start_at_and_lines(literal_inc_path: Path) -> None: |
| 167 |
options = {'lines': '2, 3, 5', 'start-at': 'foo', 'end-before': '#'} |
| 168 |
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) |
| 169 |
content, _lines = reader.read() |
| 170 |
assert content == '\nclass Foo:\n\n' |
| 171 |
assert reader.lineno_start == 1 |
| 172 |
|
| 173 |
|
| 174 |
def test_LiteralIncludeReader_missing_start_and_end(literal_inc_path: Path) -> None: |
| 175 |
options = {'start-at': 'NOTHING'} |
| 176 |
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) |
| 177 |
with pytest.raises(ValueError, match='start-at pattern not found: NOTHING'): |
| 178 |
reader.read() |
| 179 |
|
| 180 |
options = {'end-at': 'NOTHING'} |
| 181 |
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) |
| 182 |
with pytest.raises(ValueError, match='end-at pattern not found: NOTHING'): |
| 183 |
reader.read() |
| 184 |
|
| 185 |
options = {'start-after': 'NOTHING'} |
| 186 |
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) |
| 187 |
with pytest.raises(ValueError, match='start-after pattern not found: NOTHING'): |
| 188 |
reader.read() |
| 189 |
|
| 190 |
options = {'end-before': 'NOTHING'} |
| 191 |
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) |
| 192 |
with pytest.raises(ValueError, match='end-before pattern not found: NOTHING'): |
| 193 |
reader.read() |
| 194 |
|
| 195 |
|
| 196 |
def test_LiteralIncludeReader_end_before(literal_inc_path: Path) -> None: |
| 197 |
options = {'end-before': 'nclud'} # *nclud* matches first and third lines. |
| 198 |
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) |
| 199 |
content, _lines = reader.read() |
| 200 |
assert content == '# Literally included file using Python highlighting\n\n' |
| 201 |
|
| 202 |
|
| 203 |
def test_LiteralIncludeReader_prepend(literal_inc_path: Path) -> None: |
| 204 |
options = {'lines': '1', 'prepend': 'Hello', 'append': 'Sphinx'} |
| 205 |
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) |
| 206 |
content, _lines = reader.read() |
| 207 |
assert content == ( |
| 208 |
'Hello\n# Literally included file using Python highlighting\nSphinx\n' |
| 209 |
) |
| 210 |
|
| 211 |
|
| 212 |
def test_LiteralIncludeReader_dedent(literal_inc_path: Path) -> None: |
| 213 |
# dedent: 2 |
| 214 |
options = {'lines': '9-11', 'dedent': 2} |
| 215 |
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) |
| 216 |
content, _lines = reader.read() |
| 217 |
assert content == ' def baz():\n pass\n\n' |
| 218 |
|
| 219 |
# dedent: 4 |
| 220 |
options = {'lines': '9-11', 'dedent': 4} |
| 221 |
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) |
| 222 |
content, _lines = reader.read() |
| 223 |
assert content == 'def baz():\n pass\n\n' |
| 224 |
|
| 225 |
# dedent: 6 |
| 226 |
options = {'lines': '9-11', 'dedent': 6} |
| 227 |
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) |
| 228 |
content, _lines = reader.read() |
| 229 |
assert content == 'f baz():\n pass\n\n' |
| 230 |
|
| 231 |
# dedent: None |
| 232 |
options = {'lines': '9-11', 'dedent': None} |
| 233 |
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) |
| 234 |
content, _lines = reader.read() |
| 235 |
assert content == 'def baz():\n pass\n\n' |
| 236 |
|
| 237 |
|
| 238 |
def test_LiteralIncludeReader_dedent_and_append_and_prepend( |
| 239 |
literal_inc_path: Path, |
| 240 |
) -> None: |
| 241 |
# dedent: 2 |
| 242 |
options = { |
| 243 |
'lines': '9-11', |
| 244 |
'dedent': 2, |
| 245 |
'prepend': 'class Foo:', |
| 246 |
'append': '# comment', |
| 247 |
} |
| 248 |
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) |
| 249 |
content, _lines = reader.read() |
| 250 |
assert content == 'class Foo:\n def baz():\n pass\n\n# comment\n' |
| 251 |
|
| 252 |
|
| 253 |
def test_LiteralIncludeReader_tabwidth(testroot: Path) -> None: |
| 254 |
# tab-width: 4 |
| 255 |
options = {'tab-width': 4, 'pyobject': 'Qux'} |
| 256 |
reader = LiteralIncludeReader(testroot / 'target.py', options, DUMMY_CONFIG) |
| 257 |
content, _lines = reader.read() |
| 258 |
assert content == 'class Qux:\n def quux(self):\n pass\n' |
| 259 |
|
| 260 |
# tab-width: 8 |
| 261 |
options = {'tab-width': 8, 'pyobject': 'Qux'} |
| 262 |
reader = LiteralIncludeReader(testroot / 'target.py', options, DUMMY_CONFIG) |
| 263 |
content, _lines = reader.read() |
| 264 |
assert content == 'class Qux:\n def quux(self):\n pass\n' |
| 265 |
|
| 266 |
|
| 267 |
def test_LiteralIncludeReader_tabwidth_dedent(testroot: Path) -> None: |
| 268 |
options = {'tab-width': 4, 'dedent': 4, 'pyobject': 'Qux.quux'} |
| 269 |
reader = LiteralIncludeReader(testroot / 'target.py', options, DUMMY_CONFIG) |
| 270 |
content, _lines = reader.read() |
| 271 |
assert content == 'def quux(self):\n pass\n' |
| 272 |
|
| 273 |
|
| 274 |
def test_LiteralIncludeReader_diff(testroot: Path, literal_inc_path: Path) -> None: |
| 275 |
literal_diff_path = testroot / 'literal-diff.inc' |
| 276 |
options = {'diff': literal_diff_path} |
| 277 |
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) |
| 278 |
content, _lines = reader.read() |
| 279 |
assert content == ( |
| 280 |
f'--- {literal_diff_path}\n' |
| 281 |
f'+++ {literal_inc_path}\n' |
| 282 |
'@@ -6,8 +6,8 @@\n' |
| 283 |
' pass\n' |
| 284 |
' \n' |
| 285 |
' class Bar:\n' |
| 286 |
'- def baz(self):\n' |
| 287 |
'+ def baz():\n' |
| 288 |
' pass\n' |
| 289 |
' \n' |
| 290 |
'-# comment after Bar class\n' |
| 291 |
'+# comment after Bar class definition\n' |
| 292 |
' def bar(): pass\n' |
| 293 |
) |
| 294 |
|
| 295 |
|
| 296 |
@pytest.mark.sphinx('xml', testroot='directive-code') |
| 297 |
def test_code_block(app: SphinxTestApp) -> None: |
| 298 |
app.build(filenames=[app.srcdir / 'index.rst']) |
| 299 |
et = etree_parse(app.outdir / 'index.xml') |
| 300 |
secs = et.findall('./section/section') |
| 301 |
code_block = secs[0].findall('literal_block') |
| 302 |
assert len(code_block) > 0 |
| 303 |
actual = code_block[0].text |
| 304 |
expect = ' def ruby?\n false\n end' |
| 305 |
assert actual == expect |
| 306 |
|
| 307 |
|
| 308 |
@pytest.mark.sphinx('html', testroot='directive-code') |
| 309 |
def test_force_option(app: SphinxTestApp) -> None: |
| 310 |
app.build(filenames=[app.srcdir / 'force.rst']) |
| 311 |
assert 'force.rst' not in app.warning.getvalue() |
| 312 |
|
| 313 |
|
| 314 |
@pytest.mark.sphinx('html', testroot='directive-code') |
| 315 |
def test_code_block_caption_html(app: SphinxTestApp) -> None: |
| 316 |
app.build(filenames=[app.srcdir / 'caption.rst']) |
| 317 |
html = (app.outdir / 'caption.html').read_text(encoding='utf8') |
| 318 |
caption = ( |
| 319 |
'<div class="code-block-caption">' |
| 320 |
'<span class="caption-number">Listing 1 </span>' |
| 321 |
'<span class="caption-text">caption <em>test</em> rb' |
| 322 |
'</span><a class="headerlink" href="#id1" ' |
| 323 |
'title="Link to this code">\xb6</a></div>' |
| 324 |
) |
| 325 |
assert caption in html |
| 326 |
|
| 327 |
|
| 328 |
@pytest.mark.sphinx('latex', testroot='directive-code') |
| 329 |
def test_code_block_caption_latex(app: SphinxTestApp) -> None: |
| 330 |
app.build(force_all=True) |
| 331 |
latex = (app.outdir / 'projectnamenotset.tex').read_text(encoding='utf8') |
| 332 |
caption = '\\sphinxSetupCaptionForVerbatim{caption \\sphinxstyleemphasis{test} rb}' |
| 333 |
label = '\\def\\sphinxLiteralBlockLabel{\\label{\\detokenize{caption:id1}}}' |
| 334 |
link = ( |
| 335 |
'\\hyperref[\\detokenize{caption:name-test-rb}]' |
| 336 |
'{Listing \\ref{\\detokenize{caption:name-test-rb}}}' |
| 337 |
) |
| 338 |
assert caption in latex |
| 339 |
assert label in latex |
| 340 |
assert link in latex |
| 341 |
|
| 342 |
|
| 343 |
@pytest.mark.sphinx('latex', testroot='directive-code') |
| 344 |
def test_code_block_namedlink_latex(app: SphinxTestApp) -> None: |
| 345 |
app.build(force_all=True) |
| 346 |
latex = (app.outdir / 'projectnamenotset.tex').read_text(encoding='utf8') |
| 347 |
label1 = ( |
| 348 |
'\\def\\sphinxLiteralBlockLabel{\\label{\\detokenize{caption:name-test-rb}}}' |
| 349 |
) |
| 350 |
link1 = ( |
| 351 |
'\\hyperref[\\detokenize{caption:name-test-rb}]' |
| 352 |
'{\\sphinxcrossref{\\DUrole{std}{\\DUrole{std-ref}{Ruby}}}}' |
| 353 |
) |
| 354 |
label2 = ( |
| 355 |
'\\def\\sphinxLiteralBlockLabel' |
| 356 |
'{\\label{\\detokenize{namedblocks:some-ruby-code}}}' |
| 357 |
) |
| 358 |
link2 = ( |
| 359 |
'\\hyperref[\\detokenize{namedblocks:some-ruby-code}]' |
| 360 |
'{\\sphinxcrossref{\\DUrole{std}{\\DUrole{std-ref}{the ruby code}}}}' |
| 361 |
) |
| 362 |
assert label1 in latex |
| 363 |
assert link1 in latex |
| 364 |
assert label2 in latex |
| 365 |
assert link2 in latex |
| 366 |
|
| 367 |
|
| 368 |
@pytest.mark.sphinx('latex', testroot='directive-code') |
| 369 |
def test_code_block_emphasize_latex(app: SphinxTestApp) -> None: |
| 370 |
app.build(filenames=[app.srcdir / 'emphasize.rst']) |
| 371 |
latex = ( |
| 372 |
(app.outdir / 'projectnamenotset.tex') |
| 373 |
.read_text(encoding='utf8') |
| 374 |
.replace('\r\n', '\n') |
| 375 |
) |
| 376 |
includes = '\\fvset{hllines={, 6, 7, 16, 17, 18, 19, 29, 30, 31,}}%\n' |
| 377 |
assert includes in latex |
| 378 |
includes = '\\end{sphinxVerbatim}\n\\sphinxresetverbatimhllines\n' |
| 379 |
assert includes in latex |
| 380 |
|
| 381 |
|
| 382 |
@pytest.mark.sphinx('xml', testroot='directive-code') |
| 383 |
def test_literal_include(app: SphinxTestApp) -> None: |
| 384 |
app.build(filenames=[app.srcdir / 'index.rst']) |
| 385 |
et = etree_parse(app.outdir / 'index.xml') |
| 386 |
secs = et.findall('./section/section') |
| 387 |
literal_include = secs[1].findall('literal_block') |
| 388 |
literal_src = (app.srcdir / 'literal.inc').read_text(encoding='utf8') |
| 389 |
assert len(literal_include) > 0 |
| 390 |
actual = literal_include[0].text |
| 391 |
assert actual == literal_src |
| 392 |
|
| 393 |
|
| 394 |
@pytest.mark.sphinx('xml', testroot='directive-code') |
| 395 |
def test_literal_include_block_start_with_comment_or_brank(app: SphinxTestApp) -> None: |
| 396 |
app.build(filenames=[app.srcdir / 'python.rst']) |
| 397 |
et = etree_parse(app.outdir / 'python.xml') |
| 398 |
secs = et.findall('./section/section') |
| 399 |
literal_include = secs[0].findall('literal_block') |
| 400 |
assert len(literal_include) > 0 |
| 401 |
actual = literal_include[0].text |
| 402 |
expect = 'def block_start_with_comment():\n # Comment\n return 1\n' |
| 403 |
assert actual == expect |
| 404 |
|
| 405 |
actual = literal_include[1].text |
| 406 |
expect = 'def block_start_with_blank():\n\n return 1\n' |
| 407 |
assert actual == expect |
| 408 |
|
| 409 |
|
| 410 |
@pytest.mark.sphinx('html', testroot='directive-code') |
| 411 |
def test_literal_include_linenos(app: SphinxTestApp) -> None: |
| 412 |
if tuple(map(int, pygments.__version__.split('.')[:2])) >= (2, 19): |
| 413 |
sp = '<span class="w"> </span>' |
| 414 |
else: |
| 415 |
sp = ' ' |
| 416 |
|
| 417 |
app.build(filenames=[app.srcdir / 'linenos.rst']) |
| 418 |
html = (app.outdir / 'linenos.html').read_text(encoding='utf8') |
| 419 |
|
| 420 |
# :linenos: |
| 421 |
assert ( |
| 422 |
'<span class="linenos"> 1</span><span class="c1">' |
| 423 |
'# Literally included file using Python highlighting</span>' |
| 424 |
) in html |
| 425 |
|
| 426 |
# :lineno-start: |
| 427 |
assert ( |
| 428 |
'<span class="linenos">200</span><span class="c1">' |
| 429 |
'# Literally included file using Python highlighting</span>' |
| 430 |
) in html |
| 431 |
|
| 432 |
# :lines: 5-9 |
| 433 |
assert ( |
| 434 |
f'<span class="linenos">5</span><span class="k">class</span>{sp}' |
| 435 |
'<span class="nc">Foo</span><span class="p">:</span>' |
| 436 |
) in html |
| 437 |
|
| 438 |
|
| 439 |
@pytest.mark.sphinx('latex', testroot='directive-code') |
| 440 |
def test_literalinclude_file_whole_of_emptyline(app: SphinxTestApp) -> None: |
| 441 |
app.build(force_all=True) |
| 442 |
latex = ( |
| 443 |
(app.outdir / 'projectnamenotset.tex') |
| 444 |
.read_text(encoding='utf8') |
| 445 |
.replace('\r\n', '\n') |
| 446 |
) |
| 447 |
includes = ( |
| 448 |
'\\begin{sphinxVerbatim}' |
| 449 |
'[commandchars=\\\\\\{\\},numbers=left,firstnumber=1,stepnumber=1]\n' |
| 450 |
'\n' |
| 451 |
'\n' |
| 452 |
'\n' |
| 453 |
'\\end{sphinxVerbatim}\n' |
| 454 |
) |
| 455 |
assert includes in latex |
| 456 |
|
| 457 |
|
| 458 |
@pytest.mark.sphinx('html', testroot='directive-code') |
| 459 |
def test_literalinclude_caption_html(app: SphinxTestApp) -> None: |
| 460 |
app.build(force_all=True) |
| 461 |
html = (app.outdir / 'caption.html').read_text(encoding='utf8') |
| 462 |
caption = ( |
| 463 |
'<div class="code-block-caption">' |
| 464 |
'<span class="caption-number">Listing 2 </span>' |
| 465 |
'<span class="caption-text">caption <strong>test</strong> py' |
| 466 |
'</span><a class="headerlink" href="#id2" ' |
| 467 |
'title="Link to this code">\xb6</a></div>' |
| 468 |
) |
| 469 |
assert caption in html |
| 470 |
|
| 471 |
|
| 472 |
@pytest.mark.sphinx('latex', testroot='directive-code') |
| 473 |
def test_literalinclude_caption_latex(app: SphinxTestApp) -> None: |
| 474 |
app.build(filenames=(Path('index'),)) |
| 475 |
latex = (app.outdir / 'projectnamenotset.tex').read_text(encoding='utf8') |
| 476 |
caption = '\\sphinxSetupCaptionForVerbatim{caption \\sphinxstylestrong{test} py}' |
| 477 |
label = '\\def\\sphinxLiteralBlockLabel{\\label{\\detokenize{caption:id2}}}' |
| 478 |
link = ( |
| 479 |
'\\hyperref[\\detokenize{caption:name-test-py}]' |
| 480 |
'{Listing \\ref{\\detokenize{caption:name-test-py}}}' |
| 481 |
) |
| 482 |
assert caption in latex |
| 483 |
assert label in latex |
| 484 |
assert link in latex |
| 485 |
|
| 486 |
|
| 487 |
@pytest.mark.sphinx('latex', testroot='directive-code') |
| 488 |
def test_literalinclude_namedlink_latex(app: SphinxTestApp) -> None: |
| 489 |
app.build(filenames=(Path('index'),)) |
| 490 |
latex = (app.outdir / 'projectnamenotset.tex').read_text(encoding='utf8') |
| 491 |
label1 = ( |
| 492 |
'\\def\\sphinxLiteralBlockLabel{\\label{\\detokenize{caption:name-test-py}}}' |
| 493 |
) |
| 494 |
link1 = ( |
| 495 |
'\\hyperref[\\detokenize{caption:name-test-py}]' |
| 496 |
'{\\sphinxcrossref{\\DUrole{std}{\\DUrole{std-ref}{Python}}}}' |
| 497 |
) |
| 498 |
label2 = ( |
| 499 |
'\\def\\sphinxLiteralBlockLabel' |
| 500 |
'{\\label{\\detokenize{namedblocks:some-python-code}}}' |
| 501 |
) |
| 502 |
link2 = ( |
| 503 |
'\\hyperref[\\detokenize{namedblocks:some-python-code}]' |
| 504 |
'{\\sphinxcrossref{\\DUrole{std}{\\DUrole{std-ref}{the python code}}}}' |
| 505 |
) |
| 506 |
assert label1 in latex |
| 507 |
assert link1 in latex |
| 508 |
assert label2 in latex |
| 509 |
assert link2 in latex |
| 510 |
|
| 511 |
|
| 512 |
@pytest.mark.sphinx('xml', testroot='directive-code') |
| 513 |
def test_literalinclude_classes(app: SphinxTestApp) -> None: |
| 514 |
app.build(filenames=[app.srcdir / 'classes.rst']) |
| 515 |
et = etree_parse(app.outdir / 'classes.xml') |
| 516 |
secs = et.findall('./section/section') |
| 517 |
|
| 518 |
code_block = secs[0].findall('literal_block') |
| 519 |
assert len(code_block) > 0 |
| 520 |
assert code_block[0].get('classes') == 'foo bar' |
| 521 |
assert code_block[0].get('names') == 'code_block' |
| 522 |
|
| 523 |
literalinclude = secs[1].findall('literal_block') |
| 524 |
assert len(literalinclude) > 0 |
| 525 |
assert literalinclude[0].get('classes') == 'bar baz' |
| 526 |
assert literalinclude[0].get('names') == 'literal_include' |
| 527 |
|
| 528 |
|
| 529 |
@pytest.mark.sphinx('xml', testroot='directive-code') |
| 530 |
def test_literalinclude_pydecorators(app: SphinxTestApp) -> None: |
| 531 |
app.build(filenames=[app.srcdir / 'py-decorators.rst']) |
| 532 |
et = etree_parse(app.outdir / 'py-decorators.xml') |
| 533 |
secs = et.findall('./section/section') |
| 534 |
|
| 535 |
literal_include = secs[0].findall('literal_block') |
| 536 |
assert len(literal_include) == 3 |
| 537 |
|
| 538 |
actual = literal_include[0].text |
| 539 |
expect = ( |
| 540 |
'@class_decorator\n' |
| 541 |
'@other_decorator()\n' |
| 542 |
'class TheClass(object):\n' |
| 543 |
'\n' |
| 544 |
' @method_decorator\n' |
| 545 |
' @other_decorator()\n' |
| 546 |
' def the_method():\n' |
| 547 |
' pass\n' |
| 548 |
) |
| 549 |
assert actual == expect |
| 550 |
|
| 551 |
actual = literal_include[1].text |
| 552 |
expect = ( |
| 553 |
' @method_decorator\n' |
| 554 |
' @other_decorator()\n' |
| 555 |
' def the_method():\n' |
| 556 |
' pass\n' |
| 557 |
) |
| 558 |
assert actual == expect |
| 559 |
|
| 560 |
actual = literal_include[2].text |
| 561 |
expect = '@function_decorator\n@other_decorator()\ndef the_function():\n pass\n' |
| 562 |
assert actual == expect |
| 563 |
|
| 564 |
|
| 565 |
@pytest.mark.sphinx('dummy', testroot='directive-code') |
| 566 |
def test_code_block_highlighted(app: SphinxTestApp) -> None: |
| 567 |
app.build(filenames=[app.srcdir / 'highlight.rst']) |
| 568 |
doctree = app.env.get_doctree('highlight') |
| 569 |
codeblocks = list(doctree.findall(nodes.literal_block)) |
| 570 |
|
| 571 |
assert codeblocks[0]['language'] == 'default' |
| 572 |
assert codeblocks[1]['language'] == 'python2' |
| 573 |
assert codeblocks[2]['language'] == 'python3' |
| 574 |
assert codeblocks[3]['language'] == 'python2' |
| 575 |
|
| 576 |
|
| 577 |
@pytest.mark.sphinx('html', testroot='directive-code') |
| 578 |
def test_linenothreshold(app: SphinxTestApp) -> None: |
| 579 |
if tuple(map(int, pygments.__version__.split('.')[:2])) >= (2, 19): |
| 580 |
sp = '<span class="w"> </span>' |
| 581 |
else: |
| 582 |
sp = ' ' |
| 583 |
|
| 584 |
app.build(filenames=[app.srcdir / 'linenothreshold.rst']) |
| 585 |
html = (app.outdir / 'linenothreshold.html').read_text(encoding='utf8') |
| 586 |
|
| 587 |
# code-block using linenothreshold |
| 588 |
assert ( |
| 589 |
f'<span class="linenos">1</span><span class="k">class</span>{sp}' |
| 590 |
'<span class="nc">Foo</span><span class="p">:</span>' |
| 591 |
) in html |
| 592 |
|
| 593 |
# code-block not using linenothreshold (no line numbers) |
| 594 |
assert '<span></span><span class="c1"># comment</span>' in html |
| 595 |
|
| 596 |
# literal include using linenothreshold |
| 597 |
assert ( |
| 598 |
'<span class="linenos"> 1</span><span class="c1">' |
| 599 |
'# Literally included file using Python highlighting</span>' |
| 600 |
) in html |
| 601 |
|
| 602 |
# literal include not using linenothreshold (no line numbers) |
| 603 |
assert ( |
| 604 |
'<span></span><span class="c1"># Very small literal include ' |
| 605 |
'(linenothreshold check)</span>' |
| 606 |
) in html |
| 607 |
|
| 608 |
|
| 609 |
@pytest.mark.sphinx('dummy', testroot='directive-code') |
| 610 |
def test_code_block_dedent(app: SphinxTestApp) -> None: |
| 611 |
app.build(filenames=[app.srcdir / 'dedent.rst']) |
| 612 |
doctree = app.env.get_doctree('dedent') |
| 613 |
codeblocks = list(doctree.findall(nodes.literal_block)) |
| 614 |
# Note: comparison string should not have newlines at the beginning or end |
| 615 |
text_0_indent = """First line |
| 616 |
Second line |
| 617 |
Third line |
| 618 |
Fourth line""" |
| 619 |
text_2_indent = """ First line |
| 620 |
Second line |
| 621 |
Third line |
| 622 |
Fourth line""" |
| 623 |
text_4_indent = """ First line |
| 624 |
Second line |
| 625 |
Third line |
| 626 |
Fourth line""" |
| 627 |
|
| 628 |
assert codeblocks[0].astext() == text_0_indent |
| 629 |
assert codeblocks[1].astext() == text_0_indent |
| 630 |
assert codeblocks[2].astext() == text_4_indent |
| 631 |
assert codeblocks[3].astext() == text_2_indent |
| 632 |
assert codeblocks[4].astext() == text_4_indent |
| 633 |
assert codeblocks[5].astext() == text_0_indent |