2014-09-21 10:17:02 -05:00
|
|
|
"""Test the code-block directive."""
|
|
|
|
|
2023-08-10 14:48:04 -05:00
|
|
|
import os.path
|
2014-09-21 10:17:02 -05:00
|
|
|
|
2018-02-19 07:39:14 -06:00
|
|
|
import pytest
|
2019-02-03 10:50:16 -06:00
|
|
|
from docutils import nodes
|
2018-02-19 07:39:14 -06:00
|
|
|
|
2017-02-12 02:27:57 -06:00
|
|
|
from sphinx.config import Config
|
|
|
|
from sphinx.directives.code import LiteralIncludeReader
|
2017-05-07 02:46:44 -05:00
|
|
|
from sphinx.testing.util import etree_parse
|
2017-02-12 02:27:57 -06:00
|
|
|
|
2018-03-22 11:04:56 -05:00
|
|
|
DUMMY_CONFIG = Config({}, {})
|
2017-02-12 02:27:57 -06:00
|
|
|
|
|
|
|
|
2017-05-07 02:46:44 -05:00
|
|
|
@pytest.fixture(scope='module')
|
|
|
|
def testroot(rootdir):
|
|
|
|
testroot_path = rootdir / 'test-directive-code'
|
|
|
|
return testroot_path
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='module')
|
|
|
|
def literal_inc_path(testroot):
|
|
|
|
return testroot / 'literal.inc'
|
|
|
|
|
|
|
|
|
|
|
|
def test_LiteralIncludeReader(literal_inc_path):
|
2017-02-17 08:03:05 -06:00
|
|
|
options = {'lineno-match': True}
|
2017-05-07 02:46:44 -05:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-12 02:27:57 -06:00
|
|
|
content, lines = reader.read()
|
2022-04-26 21:04:19 -05:00
|
|
|
assert content == literal_inc_path.read_text(encoding='utf8')
|
2019-01-02 06:53:07 -06:00
|
|
|
assert lines == 13
|
2017-02-17 08:03:05 -06:00
|
|
|
assert reader.lineno_start == 1
|
|
|
|
|
|
|
|
|
2017-05-07 02:46:44 -05:00
|
|
|
def test_LiteralIncludeReader_lineno_start(literal_inc_path):
|
2019-01-02 06:53:07 -06:00
|
|
|
options = {'lineno-start': 4}
|
2017-05-07 02:46:44 -05:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-17 08:03:05 -06:00
|
|
|
content, lines = reader.read()
|
2022-04-26 21:04:19 -05:00
|
|
|
assert content == literal_inc_path.read_text(encoding='utf8')
|
2019-01-02 06:53:07 -06:00
|
|
|
assert lines == 13
|
|
|
|
assert reader.lineno_start == 4
|
2017-02-12 02:27:57 -06:00
|
|
|
|
|
|
|
|
2017-05-07 02:46:44 -05:00
|
|
|
def test_LiteralIncludeReader_pyobject1(literal_inc_path):
|
2017-02-17 08:03:05 -06:00
|
|
|
options = {'lineno-match': True, 'pyobject': 'Foo'}
|
2017-05-07 02:46:44 -05:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-12 02:27:57 -06:00
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == ("class Foo:\n"
|
|
|
|
" pass\n")
|
2019-01-02 06:53:07 -06:00
|
|
|
assert reader.lineno_start == 5
|
2017-02-12 02:27:57 -06:00
|
|
|
|
|
|
|
|
2017-05-07 02:46:44 -05:00
|
|
|
def test_LiteralIncludeReader_pyobject2(literal_inc_path):
|
2017-02-12 02:27:57 -06:00
|
|
|
options = {'pyobject': 'Bar'}
|
2017-05-07 02:46:44 -05:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-12 02:27:57 -06:00
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == ("class Bar:\n"
|
|
|
|
" def baz():\n"
|
|
|
|
" pass\n")
|
2017-02-17 08:03:05 -06:00
|
|
|
assert reader.lineno_start == 1 # no lineno-match
|
2017-02-12 02:27:57 -06:00
|
|
|
|
|
|
|
|
2017-05-07 02:46:44 -05:00
|
|
|
def test_LiteralIncludeReader_pyobject3(literal_inc_path):
|
2017-02-12 02:27:57 -06:00
|
|
|
options = {'pyobject': 'Bar.baz'}
|
2017-05-07 02:46:44 -05:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-12 02:27:57 -06:00
|
|
|
content, lines = reader.read()
|
2017-02-21 09:29:24 -06:00
|
|
|
assert content == (" def baz():\n"
|
|
|
|
" pass\n")
|
|
|
|
|
|
|
|
|
2017-05-07 02:46:44 -05:00
|
|
|
def test_LiteralIncludeReader_pyobject_and_lines(literal_inc_path):
|
2017-02-21 09:29:24 -06:00
|
|
|
options = {'pyobject': 'Bar', 'lines': '2-'}
|
2017-05-07 02:46:44 -05:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-21 09:29:24 -06:00
|
|
|
content, lines = reader.read()
|
2017-02-12 02:27:57 -06:00
|
|
|
assert content == (" def baz():\n"
|
|
|
|
" pass\n")
|
|
|
|
|
|
|
|
|
2017-05-07 02:46:44 -05:00
|
|
|
def test_LiteralIncludeReader_lines1(literal_inc_path):
|
2019-01-02 06:53:07 -06:00
|
|
|
options = {'lines': '1-3'}
|
2017-05-07 02:46:44 -05:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-12 02:27:57 -06:00
|
|
|
content, lines = reader.read()
|
2018-12-15 08:02:28 -06:00
|
|
|
assert content == ("# Literally included file using Python highlighting\n"
|
|
|
|
"\n"
|
|
|
|
"foo = \"Including Unicode characters: üöä\"\n")
|
2017-02-12 02:27:57 -06:00
|
|
|
|
|
|
|
|
2017-05-07 02:46:44 -05:00
|
|
|
def test_LiteralIncludeReader_lines2(literal_inc_path):
|
2019-01-02 06:53:07 -06:00
|
|
|
options = {'lines': '1,3,5'}
|
2017-05-07 02:46:44 -05:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-12 02:27:57 -06:00
|
|
|
content, lines = reader.read()
|
2018-12-15 08:02:28 -06:00
|
|
|
assert content == ("# Literally included file using Python highlighting\n"
|
|
|
|
"foo = \"Including Unicode characters: üöä\"\n"
|
|
|
|
"class Foo:\n")
|
2017-02-12 02:27:57 -06:00
|
|
|
|
|
|
|
|
2017-05-07 02:46:44 -05:00
|
|
|
def test_LiteralIncludeReader_lines_and_lineno_match1(literal_inc_path):
|
2019-01-02 06:53:07 -06:00
|
|
|
options = {'lines': '3-5', 'lineno-match': True}
|
2017-05-07 02:46:44 -05:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-17 08:03:05 -06:00
|
|
|
content, lines = reader.read()
|
2018-12-15 08:02:28 -06:00
|
|
|
assert content == ("foo = \"Including Unicode characters: üöä\"\n"
|
|
|
|
"\n"
|
|
|
|
"class Foo:\n")
|
2019-01-02 06:53:07 -06:00
|
|
|
assert reader.lineno_start == 3
|
2017-02-17 08:03:05 -06:00
|
|
|
|
|
|
|
|
2024-07-10 08:13:10 -05:00
|
|
|
@pytest.mark.sphinx # init locale for errors
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_LiteralIncludeReader_lines_and_lineno_match2(literal_inc_path, app):
|
2019-01-02 06:53:07 -06:00
|
|
|
options = {'lines': '0,3,5', 'lineno-match': True}
|
2017-05-07 02:46:44 -05:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2023-08-10 06:46:03 -05:00
|
|
|
with pytest.raises(ValueError, match='Cannot use "lineno-match" with a disjoint set of "lines"'):
|
|
|
|
reader.read()
|
2017-02-12 02:27:57 -06:00
|
|
|
|
|
|
|
|
2024-07-10 08:13:10 -05:00
|
|
|
@pytest.mark.sphinx # init locale for errors
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_LiteralIncludeReader_lines_and_lineno_match3(literal_inc_path, app):
|
2017-02-12 02:27:57 -06:00
|
|
|
options = {'lines': '100-', 'lineno-match': True}
|
2017-05-07 02:46:44 -05:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2023-08-10 06:46:03 -05:00
|
|
|
with pytest.raises(ValueError, match="Line spec '100-': no lines pulled from include file"):
|
|
|
|
reader.read()
|
2017-02-12 02:27:57 -06:00
|
|
|
|
|
|
|
|
2017-05-07 02:46:44 -05:00
|
|
|
def test_LiteralIncludeReader_start_at(literal_inc_path):
|
2017-02-17 08:03:05 -06:00
|
|
|
options = {'lineno-match': True, 'start-at': 'Foo', 'end-at': 'Bar'}
|
2017-05-07 02:46:44 -05:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-12 02:27:57 -06:00
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == ("class Foo:\n"
|
|
|
|
" pass\n"
|
|
|
|
"\n"
|
|
|
|
"class Bar:\n")
|
2019-01-02 06:53:07 -06:00
|
|
|
assert reader.lineno_start == 5
|
2017-02-12 02:27:57 -06:00
|
|
|
|
|
|
|
|
2017-05-07 02:46:44 -05:00
|
|
|
def test_LiteralIncludeReader_start_after(literal_inc_path):
|
2017-02-17 08:03:05 -06:00
|
|
|
options = {'lineno-match': True, 'start-after': 'Foo', 'end-before': 'Bar'}
|
2017-05-07 02:46:44 -05:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-12 02:27:57 -06:00
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == (" pass\n"
|
|
|
|
"\n")
|
2019-01-02 06:53:07 -06:00
|
|
|
assert reader.lineno_start == 6
|
2017-02-12 02:27:57 -06:00
|
|
|
|
|
|
|
|
2017-05-07 02:46:44 -05:00
|
|
|
def test_LiteralIncludeReader_start_after_and_lines(literal_inc_path):
|
2017-02-17 23:49:56 -06:00
|
|
|
options = {'lineno-match': True, 'lines': '6-',
|
2019-01-02 06:53:07 -06:00
|
|
|
'start-after': 'Literally', 'end-before': 'comment'}
|
2017-05-07 02:46:44 -05:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-17 23:49:56 -06:00
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == ("\n"
|
|
|
|
"class Bar:\n"
|
|
|
|
" def baz():\n"
|
|
|
|
" pass\n"
|
|
|
|
"\n")
|
2019-01-02 06:53:07 -06:00
|
|
|
assert reader.lineno_start == 7
|
2017-02-17 23:49:56 -06:00
|
|
|
|
|
|
|
|
2017-05-07 02:46:44 -05:00
|
|
|
def test_LiteralIncludeReader_start_at_and_lines(literal_inc_path):
|
2017-02-17 23:49:56 -06:00
|
|
|
options = {'lines': '2, 3, 5', 'start-at': 'foo', 'end-before': '#'}
|
2017-05-07 02:46:44 -05:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-17 23:49:56 -06:00
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == ("\n"
|
|
|
|
"class Foo:\n"
|
|
|
|
"\n")
|
|
|
|
assert reader.lineno_start == 1
|
|
|
|
|
|
|
|
|
2017-05-07 02:46:44 -05:00
|
|
|
def test_LiteralIncludeReader_missing_start_and_end(literal_inc_path):
|
2017-03-26 04:13:31 -05:00
|
|
|
options = {'start-at': 'NOTHING'}
|
2017-05-07 02:46:44 -05:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2023-08-10 06:46:03 -05:00
|
|
|
with pytest.raises(ValueError, match='start-at pattern not found: NOTHING'):
|
|
|
|
reader.read()
|
2017-03-26 04:13:31 -05:00
|
|
|
|
|
|
|
options = {'end-at': 'NOTHING'}
|
2017-05-07 02:46:44 -05:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2023-08-10 06:46:03 -05:00
|
|
|
with pytest.raises(ValueError, match='end-at pattern not found: NOTHING'):
|
|
|
|
reader.read()
|
2017-03-26 04:13:31 -05:00
|
|
|
|
|
|
|
options = {'start-after': 'NOTHING'}
|
2017-05-07 02:46:44 -05:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2023-08-10 06:46:03 -05:00
|
|
|
with pytest.raises(ValueError, match='start-after pattern not found: NOTHING'):
|
|
|
|
reader.read()
|
2017-03-26 04:13:31 -05:00
|
|
|
|
|
|
|
options = {'end-before': 'NOTHING'}
|
2017-05-07 02:46:44 -05:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2023-08-10 06:46:03 -05:00
|
|
|
with pytest.raises(ValueError, match='end-before pattern not found: NOTHING'):
|
|
|
|
reader.read()
|
2017-03-26 04:13:31 -05:00
|
|
|
|
|
|
|
|
2019-10-20 01:44:19 -05:00
|
|
|
def test_LiteralIncludeReader_end_before(literal_inc_path):
|
|
|
|
options = {'end-before': 'nclud'} # *nclud* matches first and third lines.
|
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == ("# Literally included file using Python highlighting\n"
|
|
|
|
"\n")
|
|
|
|
|
|
|
|
|
2017-05-07 02:46:44 -05:00
|
|
|
def test_LiteralIncludeReader_prepend(literal_inc_path):
|
2017-02-12 02:27:57 -06:00
|
|
|
options = {'lines': '1', 'prepend': 'Hello', 'append': 'Sphinx'}
|
2017-05-07 02:46:44 -05:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-12 02:27:57 -06:00
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == ("Hello\n"
|
|
|
|
"# Literally included file using Python highlighting\n"
|
|
|
|
"Sphinx\n")
|
|
|
|
|
|
|
|
|
2017-05-07 02:46:44 -05:00
|
|
|
def test_LiteralIncludeReader_dedent(literal_inc_path):
|
2017-02-12 02:27:57 -06:00
|
|
|
# dedent: 2
|
2019-01-02 06:53:07 -06:00
|
|
|
options = {'lines': '9-11', 'dedent': 2}
|
2017-05-07 02:46:44 -05:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-12 02:27:57 -06:00
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == (" def baz():\n"
|
|
|
|
" pass\n"
|
|
|
|
"\n")
|
|
|
|
|
|
|
|
# dedent: 4
|
2019-01-02 06:53:07 -06:00
|
|
|
options = {'lines': '9-11', 'dedent': 4}
|
2017-05-07 02:46:44 -05:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-12 02:27:57 -06:00
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == ("def baz():\n"
|
|
|
|
" pass\n"
|
|
|
|
"\n")
|
|
|
|
|
|
|
|
# dedent: 6
|
2019-01-02 06:53:07 -06:00
|
|
|
options = {'lines': '9-11', 'dedent': 6}
|
2017-05-07 02:46:44 -05:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-12 02:27:57 -06:00
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == ("f baz():\n"
|
|
|
|
" pass\n"
|
|
|
|
"\n")
|
|
|
|
|
2021-01-12 08:34:29 -06:00
|
|
|
# dedent: None
|
|
|
|
options = {'lines': '9-11', 'dedent': None}
|
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == ("def baz():\n"
|
|
|
|
" pass\n"
|
|
|
|
"\n")
|
|
|
|
|
2017-02-12 02:27:57 -06:00
|
|
|
|
2022-04-02 09:39:47 -05:00
|
|
|
def test_LiteralIncludeReader_dedent_and_append_and_prepend(literal_inc_path):
|
|
|
|
# dedent: 2
|
|
|
|
options = {'lines': '9-11', 'dedent': 2, 'prepend': 'class Foo:', 'append': '# comment'}
|
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == ("class Foo:\n"
|
|
|
|
" def baz():\n"
|
|
|
|
" pass\n"
|
|
|
|
"\n"
|
|
|
|
"# comment\n")
|
|
|
|
|
|
|
|
|
2017-05-07 02:46:44 -05:00
|
|
|
def test_LiteralIncludeReader_tabwidth(testroot):
|
2017-02-12 02:27:57 -06:00
|
|
|
# tab-width: 4
|
|
|
|
options = {'tab-width': 4, 'pyobject': 'Qux'}
|
2017-05-07 02:46:44 -05:00
|
|
|
reader = LiteralIncludeReader(testroot / 'target.py', options, DUMMY_CONFIG)
|
2017-02-12 02:27:57 -06:00
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == ("class Qux:\n"
|
|
|
|
" def quux(self):\n"
|
|
|
|
" pass\n")
|
|
|
|
|
|
|
|
# tab-width: 8
|
|
|
|
options = {'tab-width': 8, 'pyobject': 'Qux'}
|
2017-05-07 02:46:44 -05:00
|
|
|
reader = LiteralIncludeReader(testroot / 'target.py', options, DUMMY_CONFIG)
|
2017-02-12 02:27:57 -06:00
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == ("class Qux:\n"
|
|
|
|
" def quux(self):\n"
|
|
|
|
" pass\n")
|
|
|
|
|
|
|
|
|
2017-05-07 02:46:44 -05:00
|
|
|
def test_LiteralIncludeReader_tabwidth_dedent(testroot):
|
2017-02-12 02:27:57 -06:00
|
|
|
options = {'tab-width': 4, 'dedent': 4, 'pyobject': 'Qux.quux'}
|
2017-05-07 02:46:44 -05:00
|
|
|
reader = LiteralIncludeReader(testroot / 'target.py', options, DUMMY_CONFIG)
|
2017-02-12 02:27:57 -06:00
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == ("def quux(self):\n"
|
|
|
|
" pass\n")
|
|
|
|
|
|
|
|
|
2017-05-07 02:46:44 -05:00
|
|
|
def test_LiteralIncludeReader_diff(testroot, literal_inc_path):
|
|
|
|
options = {'diff': testroot / 'literal-diff.inc'}
|
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-12 02:27:57 -06:00
|
|
|
content, lines = reader.read()
|
2023-08-10 14:48:04 -05:00
|
|
|
assert content == ("--- " + os.path.join(testroot, 'literal-diff.inc') + "\n"
|
|
|
|
"+++ " + os.path.join(testroot, 'literal.inc') + "\n"
|
2019-01-02 06:53:07 -06:00
|
|
|
"@@ -6,8 +6,8 @@\n"
|
2017-02-12 02:27:57 -06:00
|
|
|
" pass\n"
|
|
|
|
" \n"
|
|
|
|
" class Bar:\n"
|
|
|
|
"- def baz(self):\n"
|
|
|
|
"+ def baz():\n"
|
|
|
|
" pass\n"
|
|
|
|
" \n"
|
|
|
|
"-# comment after Bar class\n"
|
|
|
|
"+# comment after Bar class definition\n"
|
|
|
|
" def bar(): pass\n")
|
2014-09-21 10:17:02 -05:00
|
|
|
|
2017-01-05 10:14:47 -06:00
|
|
|
|
|
|
|
@pytest.mark.sphinx('xml', testroot='directive-code')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_code_block(app):
|
2024-01-16 20:38:46 -06:00
|
|
|
app.build(filenames=[app.srcdir / 'index.rst'])
|
2016-12-14 22:12:13 -06:00
|
|
|
et = etree_parse(app.outdir / 'index.xml')
|
2014-09-21 10:17:02 -05:00
|
|
|
secs = et.findall('./section/section')
|
|
|
|
code_block = secs[0].findall('literal_block')
|
|
|
|
assert len(code_block) > 0
|
|
|
|
actual = code_block[0].text
|
|
|
|
expect = (
|
2024-08-10 18:39:35 -05:00
|
|
|
" def ruby?\n"
|
|
|
|
" false\n"
|
2014-09-21 10:17:02 -05:00
|
|
|
" end"
|
|
|
|
)
|
|
|
|
assert actual == expect
|
|
|
|
|
|
|
|
|
2019-05-29 20:44:48 -05:00
|
|
|
@pytest.mark.sphinx('html', testroot='directive-code')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_force_option(app):
|
2024-01-16 20:38:46 -06:00
|
|
|
app.build(filenames=[app.srcdir / 'force.rst'])
|
2024-07-23 09:35:55 -05:00
|
|
|
assert 'force.rst' not in app.warning.getvalue()
|
2019-05-29 20:44:48 -05:00
|
|
|
|
|
|
|
|
2017-01-05 10:14:47 -06:00
|
|
|
@pytest.mark.sphinx('html', testroot='directive-code')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_code_block_caption_html(app):
|
2024-01-16 20:38:46 -06:00
|
|
|
app.build(filenames=[app.srcdir / 'caption.rst'])
|
2022-04-26 21:04:19 -05:00
|
|
|
html = (app.outdir / 'caption.html').read_text(encoding='utf8')
|
2018-12-15 08:02:28 -06:00
|
|
|
caption = ('<div class="code-block-caption">'
|
|
|
|
'<span class="caption-number">Listing 1 </span>'
|
|
|
|
'<span class="caption-text">caption <em>test</em> rb'
|
|
|
|
'</span><a class="headerlink" href="#id1" '
|
2023-08-11 23:21:16 -05:00
|
|
|
'title="Link to this code">\xb6</a></div>')
|
2014-09-21 10:17:02 -05:00
|
|
|
assert caption in html
|
|
|
|
|
|
|
|
|
2017-01-05 10:14:47 -06:00
|
|
|
@pytest.mark.sphinx('latex', testroot='directive-code')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_code_block_caption_latex(app):
|
2024-01-16 20:38:46 -06:00
|
|
|
app.build(force_all=True)
|
2024-07-10 15:43:14 -05:00
|
|
|
latex = (app.outdir / 'projectnamenotset.tex').read_text(encoding='utf8')
|
2016-06-11 12:15:47 -05:00
|
|
|
caption = '\\sphinxSetupCaptionForVerbatim{caption \\sphinxstyleemphasis{test} rb}'
|
2016-12-08 14:43:04 -06:00
|
|
|
label = '\\def\\sphinxLiteralBlockLabel{\\label{\\detokenize{caption:id1}}}'
|
2024-08-10 18:39:35 -05:00
|
|
|
link = ('\\hyperref[\\detokenize{caption:name-test-rb}]'
|
|
|
|
'{Listing \\ref{\\detokenize{caption:name-test-rb}}}')
|
2014-09-21 10:17:02 -05:00
|
|
|
assert caption in latex
|
2016-04-10 13:42:00 -05:00
|
|
|
assert label in latex
|
|
|
|
assert link in latex
|
|
|
|
|
|
|
|
|
2017-01-05 10:14:47 -06:00
|
|
|
@pytest.mark.sphinx('latex', testroot='directive-code')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_code_block_namedlink_latex(app):
|
2024-01-16 20:38:46 -06:00
|
|
|
app.build(force_all=True)
|
2024-07-10 15:43:14 -05:00
|
|
|
latex = (app.outdir / 'projectnamenotset.tex').read_text(encoding='utf8')
|
2016-12-08 14:43:04 -06:00
|
|
|
label1 = '\\def\\sphinxLiteralBlockLabel{\\label{\\detokenize{caption:name-test-rb}}}'
|
2024-08-10 18:39:35 -05:00
|
|
|
link1 = ('\\hyperref[\\detokenize{caption:name-test-rb}]'
|
|
|
|
'{\\sphinxcrossref{\\DUrole{std,std-ref}{Ruby}}')
|
2016-12-08 14:43:04 -06:00
|
|
|
label2 = ('\\def\\sphinxLiteralBlockLabel'
|
|
|
|
'{\\label{\\detokenize{namedblocks:some-ruby-code}}}')
|
2024-08-10 18:39:35 -05:00
|
|
|
link2 = ('\\hyperref[\\detokenize{namedblocks:some-ruby-code}]'
|
|
|
|
'{\\sphinxcrossref{\\DUrole{std,std-ref}{the ruby code}}}')
|
2016-04-10 13:42:00 -05:00
|
|
|
assert label1 in latex
|
2016-06-11 10:00:52 -05:00
|
|
|
assert link1 in latex
|
2016-04-10 13:42:00 -05:00
|
|
|
assert label2 in latex
|
2016-06-11 10:00:52 -05:00
|
|
|
assert link2 in latex
|
2014-09-21 10:17:02 -05:00
|
|
|
|
|
|
|
|
2017-12-10 05:16:35 -06:00
|
|
|
@pytest.mark.sphinx('latex', testroot='directive-code')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_code_block_emphasize_latex(app):
|
2024-01-16 20:38:46 -06:00
|
|
|
app.build(filenames=[app.srcdir / 'emphasize.rst'])
|
2024-07-10 15:43:14 -05:00
|
|
|
latex = (app.outdir / 'projectnamenotset.tex').read_text(encoding='utf8').replace('\r\n', '\n')
|
2019-01-02 06:53:07 -06:00
|
|
|
includes = '\\fvset{hllines={, 5, 6, 13, 14, 15, 24, 25, 26,}}%\n'
|
2017-12-10 05:16:35 -06:00
|
|
|
assert includes in latex
|
2018-12-16 15:37:47 -06:00
|
|
|
includes = '\\end{sphinxVerbatim}\n\\sphinxresetverbatimhllines\n'
|
2018-12-16 09:34:36 -06:00
|
|
|
assert includes in latex
|
2017-12-10 05:16:35 -06:00
|
|
|
|
|
|
|
|
2017-01-05 10:14:47 -06:00
|
|
|
@pytest.mark.sphinx('xml', testroot='directive-code')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_literal_include(app):
|
2024-01-16 20:38:46 -06:00
|
|
|
app.build(filenames=[app.srcdir / 'index.rst'])
|
2016-12-14 22:12:13 -06:00
|
|
|
et = etree_parse(app.outdir / 'index.xml')
|
2014-09-21 10:17:02 -05:00
|
|
|
secs = et.findall('./section/section')
|
|
|
|
literal_include = secs[1].findall('literal_block')
|
2022-04-26 21:04:19 -05:00
|
|
|
literal_src = (app.srcdir / 'literal.inc').read_text(encoding='utf8')
|
2014-09-21 10:17:02 -05:00
|
|
|
assert len(literal_include) > 0
|
|
|
|
actual = literal_include[0].text
|
|
|
|
assert actual == literal_src
|
|
|
|
|
|
|
|
|
2017-01-05 10:14:47 -06:00
|
|
|
@pytest.mark.sphinx('xml', testroot='directive-code')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_literal_include_block_start_with_comment_or_brank(app):
|
2024-01-16 20:38:46 -06:00
|
|
|
app.build(filenames=[app.srcdir / 'python.rst'])
|
2016-12-14 22:12:13 -06:00
|
|
|
et = etree_parse(app.outdir / 'python.xml')
|
2016-01-20 07:48:16 -06:00
|
|
|
secs = et.findall('./section/section')
|
|
|
|
literal_include = secs[0].findall('literal_block')
|
|
|
|
assert len(literal_include) > 0
|
|
|
|
actual = literal_include[0].text
|
|
|
|
expect = (
|
|
|
|
'def block_start_with_comment():\n'
|
|
|
|
' # Comment\n'
|
|
|
|
' return 1\n'
|
|
|
|
)
|
|
|
|
assert actual == expect
|
|
|
|
|
|
|
|
actual = literal_include[1].text
|
|
|
|
expect = (
|
|
|
|
'def block_start_with_blank():\n'
|
|
|
|
'\n'
|
|
|
|
' return 1\n'
|
|
|
|
)
|
|
|
|
assert actual == expect
|
|
|
|
|
|
|
|
|
2017-01-05 10:14:47 -06:00
|
|
|
@pytest.mark.sphinx('html', testroot='directive-code')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_literal_include_linenos(app):
|
2024-01-16 20:38:46 -06:00
|
|
|
app.build(filenames=[app.srcdir / 'linenos.rst'])
|
2022-04-26 21:04:19 -05:00
|
|
|
html = (app.outdir / 'linenos.html').read_text(encoding='utf8')
|
2014-10-04 10:48:55 -05:00
|
|
|
|
2017-02-17 09:12:54 -06:00
|
|
|
# :linenos:
|
2020-07-11 08:30:37 -05:00
|
|
|
assert ('<span class="linenos"> 1</span><span class="c1">'
|
|
|
|
'# Literally included file using Python highlighting</span>' in html)
|
2017-02-17 09:12:54 -06:00
|
|
|
|
|
|
|
# :lineno-start:
|
2020-07-11 08:30:37 -05:00
|
|
|
assert ('<span class="linenos">200</span><span class="c1">'
|
|
|
|
'# Literally included file using Python highlighting</span>' in html)
|
|
|
|
|
|
|
|
# :lines: 5-9
|
|
|
|
assert ('<span class="linenos">5</span><span class="k">class</span> '
|
|
|
|
'<span class="nc">Foo</span><span class="p">:</span>' in html)
|
2016-08-10 09:49:00 -05:00
|
|
|
|
2014-10-04 10:48:55 -05:00
|
|
|
|
2017-01-05 10:14:47 -06:00
|
|
|
@pytest.mark.sphinx('latex', testroot='directive-code')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_literalinclude_file_whole_of_emptyline(app):
|
2024-01-16 20:38:46 -06:00
|
|
|
app.build(force_all=True)
|
2024-07-10 15:43:14 -05:00
|
|
|
latex = (app.outdir / 'projectnamenotset.tex').read_text(encoding='utf8').replace('\r\n', '\n')
|
2015-03-30 06:34:00 -05:00
|
|
|
includes = (
|
2016-07-07 08:36:44 -05:00
|
|
|
'\\begin{sphinxVerbatim}'
|
|
|
|
'[commandchars=\\\\\\{\\},numbers=left,firstnumber=1,stepnumber=1]\n'
|
2015-03-30 06:34:00 -05:00
|
|
|
'\n'
|
|
|
|
'\n'
|
|
|
|
'\n'
|
2016-06-15 11:34:00 -05:00
|
|
|
'\\end{sphinxVerbatim}\n')
|
2015-03-30 06:34:00 -05:00
|
|
|
assert includes in latex
|
|
|
|
|
|
|
|
|
2017-01-05 10:14:47 -06:00
|
|
|
@pytest.mark.sphinx('html', testroot='directive-code')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_literalinclude_caption_html(app):
|
2024-01-16 20:38:46 -06:00
|
|
|
app.build(force_all=True)
|
2022-04-26 21:04:19 -05:00
|
|
|
html = (app.outdir / 'caption.html').read_text(encoding='utf8')
|
2018-12-15 08:02:28 -06:00
|
|
|
caption = ('<div class="code-block-caption">'
|
|
|
|
'<span class="caption-number">Listing 2 </span>'
|
|
|
|
'<span class="caption-text">caption <strong>test</strong> py'
|
|
|
|
'</span><a class="headerlink" href="#id2" '
|
2023-08-11 23:21:16 -05:00
|
|
|
'title="Link to this code">\xb6</a></div>')
|
2014-09-21 10:17:02 -05:00
|
|
|
assert caption in html
|
|
|
|
|
|
|
|
|
2017-01-05 10:14:47 -06:00
|
|
|
@pytest.mark.sphinx('latex', testroot='directive-code')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_literalinclude_caption_latex(app):
|
2024-01-16 20:38:46 -06:00
|
|
|
app.build(filenames='index')
|
2024-07-10 15:43:14 -05:00
|
|
|
latex = (app.outdir / 'projectnamenotset.tex').read_text(encoding='utf8')
|
2016-06-11 12:15:47 -05:00
|
|
|
caption = '\\sphinxSetupCaptionForVerbatim{caption \\sphinxstylestrong{test} py}'
|
2016-12-08 14:43:04 -06:00
|
|
|
label = '\\def\\sphinxLiteralBlockLabel{\\label{\\detokenize{caption:id2}}}'
|
2024-08-10 18:39:35 -05:00
|
|
|
link = ('\\hyperref[\\detokenize{caption:name-test-py}]'
|
|
|
|
'{Listing \\ref{\\detokenize{caption:name-test-py}}}')
|
2014-09-21 10:17:02 -05:00
|
|
|
assert caption in latex
|
2016-04-10 13:42:00 -05:00
|
|
|
assert label in latex
|
|
|
|
assert link in latex
|
|
|
|
|
|
|
|
|
2017-01-05 10:14:47 -06:00
|
|
|
@pytest.mark.sphinx('latex', testroot='directive-code')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_literalinclude_namedlink_latex(app):
|
2024-01-16 20:38:46 -06:00
|
|
|
app.build(filenames='index')
|
2024-07-10 15:43:14 -05:00
|
|
|
latex = (app.outdir / 'projectnamenotset.tex').read_text(encoding='utf8')
|
2016-12-08 14:43:04 -06:00
|
|
|
label1 = '\\def\\sphinxLiteralBlockLabel{\\label{\\detokenize{caption:name-test-py}}}'
|
2024-08-10 18:39:35 -05:00
|
|
|
link1 = ('\\hyperref[\\detokenize{caption:name-test-py}]'
|
|
|
|
'{\\sphinxcrossref{\\DUrole{std,std-ref}{Python}}')
|
2016-12-08 14:43:04 -06:00
|
|
|
label2 = ('\\def\\sphinxLiteralBlockLabel'
|
|
|
|
'{\\label{\\detokenize{namedblocks:some-python-code}}}')
|
2024-08-10 18:39:35 -05:00
|
|
|
link2 = ('\\hyperref[\\detokenize{namedblocks:some-python-code}]'
|
|
|
|
'{\\sphinxcrossref{\\DUrole{std,std-ref}{the python code}}}')
|
2016-04-10 13:42:00 -05:00
|
|
|
assert label1 in latex
|
2016-06-11 10:00:52 -05:00
|
|
|
assert link1 in latex
|
2016-04-10 13:42:00 -05:00
|
|
|
assert label2 in latex
|
2016-06-11 10:00:52 -05:00
|
|
|
assert link2 in latex
|
2015-12-20 07:44:32 -06:00
|
|
|
|
|
|
|
|
2017-01-05 10:14:47 -06:00
|
|
|
@pytest.mark.sphinx('xml', testroot='directive-code')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_literalinclude_classes(app):
|
2024-01-16 20:38:46 -06:00
|
|
|
app.build(filenames=[app.srcdir / 'classes.rst'])
|
2016-12-14 22:12:13 -06:00
|
|
|
et = etree_parse(app.outdir / 'classes.xml')
|
2015-12-20 07:44:32 -06:00
|
|
|
secs = et.findall('./section/section')
|
|
|
|
|
|
|
|
code_block = secs[0].findall('literal_block')
|
|
|
|
assert len(code_block) > 0
|
2022-12-29 18:06:11 -06:00
|
|
|
assert code_block[0].get('classes') == 'foo bar'
|
|
|
|
assert code_block[0].get('names') == 'code_block'
|
2015-12-20 07:44:32 -06:00
|
|
|
|
|
|
|
literalinclude = secs[1].findall('literal_block')
|
|
|
|
assert len(literalinclude) > 0
|
2022-12-29 18:06:11 -06:00
|
|
|
assert literalinclude[0].get('classes') == 'bar baz'
|
|
|
|
assert literalinclude[0].get('names') == 'literal_include'
|
2017-03-11 12:46:03 -06:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.sphinx('xml', testroot='directive-code')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_literalinclude_pydecorators(app):
|
2024-01-16 20:38:46 -06:00
|
|
|
app.build(filenames=[app.srcdir / 'py-decorators.rst'])
|
2017-03-11 12:46:03 -06:00
|
|
|
et = etree_parse(app.outdir / 'py-decorators.xml')
|
|
|
|
secs = et.findall('./section/section')
|
|
|
|
|
|
|
|
literal_include = secs[0].findall('literal_block')
|
|
|
|
assert len(literal_include) == 3
|
|
|
|
|
|
|
|
actual = literal_include[0].text
|
|
|
|
expect = (
|
|
|
|
'@class_decorator\n'
|
|
|
|
'@other_decorator()\n'
|
|
|
|
'class TheClass(object):\n'
|
|
|
|
'\n'
|
|
|
|
' @method_decorator\n'
|
|
|
|
' @other_decorator()\n'
|
|
|
|
' def the_method():\n'
|
|
|
|
' pass\n'
|
|
|
|
)
|
|
|
|
assert actual == expect
|
|
|
|
|
|
|
|
actual = literal_include[1].text
|
|
|
|
expect = (
|
|
|
|
' @method_decorator\n'
|
|
|
|
' @other_decorator()\n'
|
|
|
|
' def the_method():\n'
|
|
|
|
' pass\n'
|
|
|
|
)
|
|
|
|
assert actual == expect
|
|
|
|
|
|
|
|
actual = literal_include[2].text
|
|
|
|
expect = (
|
|
|
|
'@function_decorator\n'
|
|
|
|
'@other_decorator()\n'
|
|
|
|
'def the_function():\n'
|
|
|
|
' pass\n'
|
|
|
|
)
|
|
|
|
assert actual == expect
|
2019-02-03 10:50:16 -06:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.sphinx('dummy', testroot='directive-code')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_code_block_highlighted(app):
|
2024-01-16 20:38:46 -06:00
|
|
|
app.build(filenames=[app.srcdir / 'highlight.rst'])
|
2019-02-03 10:50:16 -06:00
|
|
|
doctree = app.env.get_doctree('highlight')
|
2022-01-01 10:06:24 -06:00
|
|
|
codeblocks = list(doctree.findall(nodes.literal_block))
|
2019-02-03 10:50:16 -06:00
|
|
|
|
|
|
|
assert codeblocks[0]['language'] == 'default'
|
|
|
|
assert codeblocks[1]['language'] == 'python2'
|
|
|
|
assert codeblocks[2]['language'] == 'python3'
|
|
|
|
assert codeblocks[3]['language'] == 'python2'
|
2019-02-16 20:44:32 -06:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.sphinx('html', testroot='directive-code')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_linenothreshold(app):
|
2024-01-16 20:38:46 -06:00
|
|
|
app.build(filenames=[app.srcdir / 'linenothreshold.rst'])
|
2022-04-26 21:04:19 -05:00
|
|
|
html = (app.outdir / 'linenothreshold.html').read_text(encoding='utf8')
|
2019-02-16 20:44:32 -06:00
|
|
|
|
|
|
|
# code-block using linenothreshold
|
2020-07-11 08:30:37 -05:00
|
|
|
assert ('<span class="linenos">1</span><span class="k">class</span> '
|
|
|
|
'<span class="nc">Foo</span><span class="p">:</span>' in html)
|
|
|
|
|
|
|
|
# code-block not using linenothreshold (no line numbers)
|
|
|
|
assert '<span></span><span class="c1"># comment</span>' in html
|
2019-02-16 20:44:32 -06:00
|
|
|
|
|
|
|
# literal include using linenothreshold
|
2020-07-11 08:30:37 -05:00
|
|
|
assert ('<span class="linenos"> 1</span><span class="c1">'
|
|
|
|
'# Literally included file using Python highlighting</span>' in html)
|
|
|
|
|
|
|
|
# literal include not using linenothreshold (no line numbers)
|
|
|
|
assert ('<span></span><span class="c1"># Very small literal include '
|
|
|
|
'(linenothreshold check)</span>' in html)
|
2021-09-21 04:19:53 -05:00
|
|
|
|
2022-01-15 05:01:30 -06:00
|
|
|
|
2021-09-21 04:19:53 -05:00
|
|
|
@pytest.mark.sphinx('dummy', testroot='directive-code')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_code_block_dedent(app):
|
2024-01-16 20:38:46 -06:00
|
|
|
app.build(filenames=[app.srcdir / 'dedent.rst'])
|
2021-09-21 04:19:53 -05:00
|
|
|
doctree = app.env.get_doctree('dedent')
|
2022-04-24 11:55:05 -05:00
|
|
|
codeblocks = list(doctree.findall(nodes.literal_block))
|
2021-09-21 04:19:53 -05:00
|
|
|
# Note: comparison string should not have newlines at the beginning or end
|
|
|
|
text_0_indent = '''First line
|
|
|
|
Second line
|
|
|
|
Third line
|
|
|
|
Fourth line'''
|
|
|
|
text_2_indent = ''' First line
|
|
|
|
Second line
|
|
|
|
Third line
|
|
|
|
Fourth line'''
|
|
|
|
text_4_indent = ''' First line
|
|
|
|
Second line
|
|
|
|
Third line
|
|
|
|
Fourth line'''
|
|
|
|
|
|
|
|
assert codeblocks[0].astext() == text_0_indent
|
|
|
|
assert codeblocks[1].astext() == text_0_indent
|
|
|
|
assert codeblocks[2].astext() == text_4_indent
|
|
|
|
assert codeblocks[3].astext() == text_2_indent
|
|
|
|
assert codeblocks[4].astext() == text_4_indent
|
|
|
|
assert codeblocks[5].astext() == text_0_indent
|