2014-09-21 17:17:02 +02:00
|
|
|
"""
|
|
|
|
|
test_directive_code
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
Test the code-block directive.
|
|
|
|
|
|
2021-01-01 02:00:29 +09:00
|
|
|
:copyright: Copyright 2007-2021 by the Sphinx team, see AUTHORS.
|
2014-09-21 17:17:02 +02:00
|
|
|
:license: BSD, see LICENSE for details.
|
|
|
|
|
"""
|
|
|
|
|
|
2017-06-14 13:26:15 -05:00
|
|
|
import os
|
2014-09-21 17:17:02 +02:00
|
|
|
|
2018-02-19 22:39:14 +09:00
|
|
|
import pytest
|
2019-02-04 01:50:16 +09:00
|
|
|
from docutils import nodes
|
2018-02-19 22:39:14 +09:00
|
|
|
|
2017-02-12 17:27:57 +09:00
|
|
|
from sphinx.config import Config
|
|
|
|
|
from sphinx.directives.code import LiteralIncludeReader
|
2017-05-07 16:46:44 +09:00
|
|
|
from sphinx.testing.util import etree_parse
|
2017-02-12 17:27:57 +09:00
|
|
|
|
2018-03-23 01:04:56 +09:00
|
|
|
DUMMY_CONFIG = Config({}, {})
|
2017-02-12 17:27:57 +09:00
|
|
|
|
|
|
|
|
|
2017-05-07 16:46:44 +09: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'
|
|
|
|
|
|
|
|
|
|
|
2017-06-14 13:26:15 -05:00
|
|
|
@pytest.mark.xfail(os.name != 'posix', reason="Not working on windows")
|
2017-05-07 16:46:44 +09:00
|
|
|
def test_LiteralIncludeReader(literal_inc_path):
|
2017-02-17 23:03:05 +09:00
|
|
|
options = {'lineno-match': True}
|
2017-05-07 16:46:44 +09:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-12 17:27:57 +09:00
|
|
|
content, lines = reader.read()
|
2020-02-01 11:58:51 +09:00
|
|
|
assert content == literal_inc_path.read_text()
|
2019-01-02 21:53:07 +09:00
|
|
|
assert lines == 13
|
2017-02-17 23:03:05 +09:00
|
|
|
assert reader.lineno_start == 1
|
|
|
|
|
|
|
|
|
|
|
2017-06-14 13:26:15 -05:00
|
|
|
@pytest.mark.xfail(os.name != 'posix', reason="Not working on windows")
|
2017-05-07 16:46:44 +09:00
|
|
|
def test_LiteralIncludeReader_lineno_start(literal_inc_path):
|
2019-01-02 21:53:07 +09:00
|
|
|
options = {'lineno-start': 4}
|
2017-05-07 16:46:44 +09:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-17 23:03:05 +09:00
|
|
|
content, lines = reader.read()
|
2020-02-01 11:58:51 +09:00
|
|
|
assert content == literal_inc_path.read_text()
|
2019-01-02 21:53:07 +09:00
|
|
|
assert lines == 13
|
|
|
|
|
assert reader.lineno_start == 4
|
2017-02-12 17:27:57 +09:00
|
|
|
|
|
|
|
|
|
2017-06-14 13:26:15 -05:00
|
|
|
@pytest.mark.xfail(os.name != 'posix', reason="Not working on windows")
|
2017-05-07 16:46:44 +09:00
|
|
|
def test_LiteralIncludeReader_pyobject1(literal_inc_path):
|
2017-02-17 23:03:05 +09:00
|
|
|
options = {'lineno-match': True, 'pyobject': 'Foo'}
|
2017-05-07 16:46:44 +09:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-12 17:27:57 +09:00
|
|
|
content, lines = reader.read()
|
|
|
|
|
assert content == ("class Foo:\n"
|
|
|
|
|
" pass\n")
|
2019-01-02 21:53:07 +09:00
|
|
|
assert reader.lineno_start == 5
|
2017-02-12 17:27:57 +09:00
|
|
|
|
|
|
|
|
|
2017-06-14 13:26:15 -05:00
|
|
|
@pytest.mark.xfail(os.name != 'posix', reason="Not working on windows")
|
2017-05-07 16:46:44 +09:00
|
|
|
def test_LiteralIncludeReader_pyobject2(literal_inc_path):
|
2017-02-12 17:27:57 +09:00
|
|
|
options = {'pyobject': 'Bar'}
|
2017-05-07 16:46:44 +09:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-12 17:27:57 +09:00
|
|
|
content, lines = reader.read()
|
|
|
|
|
assert content == ("class Bar:\n"
|
|
|
|
|
" def baz():\n"
|
|
|
|
|
" pass\n")
|
2017-02-17 23:03:05 +09:00
|
|
|
assert reader.lineno_start == 1 # no lineno-match
|
2017-02-12 17:27:57 +09:00
|
|
|
|
|
|
|
|
|
2017-06-14 13:26:15 -05:00
|
|
|
@pytest.mark.xfail(os.name != 'posix', reason="Not working on windows")
|
2017-05-07 16:46:44 +09:00
|
|
|
def test_LiteralIncludeReader_pyobject3(literal_inc_path):
|
2017-02-12 17:27:57 +09:00
|
|
|
options = {'pyobject': 'Bar.baz'}
|
2017-05-07 16:46:44 +09:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-12 17:27:57 +09:00
|
|
|
content, lines = reader.read()
|
2017-02-22 00:29:24 +09:00
|
|
|
assert content == (" def baz():\n"
|
|
|
|
|
" pass\n")
|
|
|
|
|
|
|
|
|
|
|
2017-06-14 13:26:15 -05:00
|
|
|
@pytest.mark.xfail(os.name != 'posix', reason="Not working on windows")
|
2017-05-07 16:46:44 +09:00
|
|
|
def test_LiteralIncludeReader_pyobject_and_lines(literal_inc_path):
|
2017-02-22 00:29:24 +09:00
|
|
|
options = {'pyobject': 'Bar', 'lines': '2-'}
|
2017-05-07 16:46:44 +09:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-22 00:29:24 +09:00
|
|
|
content, lines = reader.read()
|
2017-02-12 17:27:57 +09:00
|
|
|
assert content == (" def baz():\n"
|
|
|
|
|
" pass\n")
|
|
|
|
|
|
|
|
|
|
|
2017-06-14 13:26:15 -05:00
|
|
|
@pytest.mark.xfail(os.name != 'posix', reason="Not working on windows")
|
2017-05-07 16:46:44 +09:00
|
|
|
def test_LiteralIncludeReader_lines1(literal_inc_path):
|
2019-01-02 21:53:07 +09:00
|
|
|
options = {'lines': '1-3'}
|
2017-05-07 16:46:44 +09:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-12 17:27:57 +09:00
|
|
|
content, lines = reader.read()
|
2018-12-15 23:02:28 +09:00
|
|
|
assert content == ("# Literally included file using Python highlighting\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"foo = \"Including Unicode characters: üöä\"\n")
|
2017-02-12 17:27:57 +09:00
|
|
|
|
|
|
|
|
|
2017-06-14 13:26:15 -05:00
|
|
|
@pytest.mark.xfail(os.name != 'posix', reason="Not working on windows")
|
2017-05-07 16:46:44 +09:00
|
|
|
def test_LiteralIncludeReader_lines2(literal_inc_path):
|
2019-01-02 21:53:07 +09:00
|
|
|
options = {'lines': '1,3,5'}
|
2017-05-07 16:46:44 +09:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-12 17:27:57 +09:00
|
|
|
content, lines = reader.read()
|
2018-12-15 23:02:28 +09:00
|
|
|
assert content == ("# Literally included file using Python highlighting\n"
|
|
|
|
|
"foo = \"Including Unicode characters: üöä\"\n"
|
|
|
|
|
"class Foo:\n")
|
2017-02-12 17:27:57 +09:00
|
|
|
|
|
|
|
|
|
2017-06-14 13:26:15 -05:00
|
|
|
@pytest.mark.xfail(os.name != 'posix', reason="Not working on windows")
|
2017-05-07 16:46:44 +09:00
|
|
|
def test_LiteralIncludeReader_lines_and_lineno_match1(literal_inc_path):
|
2019-01-02 21:53:07 +09:00
|
|
|
options = {'lines': '3-5', 'lineno-match': True}
|
2017-05-07 16:46:44 +09:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-17 23:03:05 +09:00
|
|
|
content, lines = reader.read()
|
2018-12-15 23:02:28 +09:00
|
|
|
assert content == ("foo = \"Including Unicode characters: üöä\"\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"class Foo:\n")
|
2019-01-02 21:53:07 +09:00
|
|
|
assert reader.lineno_start == 3
|
2017-02-17 23:03:05 +09:00
|
|
|
|
|
|
|
|
|
2017-02-12 17:27:57 +09:00
|
|
|
@pytest.mark.sphinx() # init locale for errors
|
2017-05-07 16:46:44 +09:00
|
|
|
def test_LiteralIncludeReader_lines_and_lineno_match2(literal_inc_path, app, status, warning):
|
2019-01-02 21:53:07 +09:00
|
|
|
options = {'lines': '0,3,5', 'lineno-match': True}
|
2017-05-07 16:46:44 +09:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-12 17:27:57 +09:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
content, lines = reader.read()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.sphinx() # init locale for errors
|
2017-05-07 16:46:44 +09:00
|
|
|
def test_LiteralIncludeReader_lines_and_lineno_match3(literal_inc_path, app, status, warning):
|
2017-02-12 17:27:57 +09:00
|
|
|
options = {'lines': '100-', 'lineno-match': True}
|
2017-05-07 16:46:44 +09:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-12 17:27:57 +09:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
content, lines = reader.read()
|
|
|
|
|
|
|
|
|
|
|
2017-06-14 13:26:15 -05:00
|
|
|
@pytest.mark.xfail(os.name != 'posix', reason="Not working on windows")
|
2017-05-07 16:46:44 +09:00
|
|
|
def test_LiteralIncludeReader_start_at(literal_inc_path):
|
2017-02-17 23:03:05 +09:00
|
|
|
options = {'lineno-match': True, 'start-at': 'Foo', 'end-at': 'Bar'}
|
2017-05-07 16:46:44 +09:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-12 17:27:57 +09:00
|
|
|
content, lines = reader.read()
|
|
|
|
|
assert content == ("class Foo:\n"
|
|
|
|
|
" pass\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"class Bar:\n")
|
2019-01-02 21:53:07 +09:00
|
|
|
assert reader.lineno_start == 5
|
2017-02-12 17:27:57 +09:00
|
|
|
|
|
|
|
|
|
2017-06-14 13:26:15 -05:00
|
|
|
@pytest.mark.xfail(os.name != 'posix', reason="Not working on windows")
|
2017-05-07 16:46:44 +09:00
|
|
|
def test_LiteralIncludeReader_start_after(literal_inc_path):
|
2017-02-17 23:03:05 +09:00
|
|
|
options = {'lineno-match': True, 'start-after': 'Foo', 'end-before': 'Bar'}
|
2017-05-07 16:46:44 +09:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-12 17:27:57 +09:00
|
|
|
content, lines = reader.read()
|
|
|
|
|
assert content == (" pass\n"
|
|
|
|
|
"\n")
|
2019-01-02 21:53:07 +09:00
|
|
|
assert reader.lineno_start == 6
|
2017-02-12 17:27:57 +09:00
|
|
|
|
|
|
|
|
|
2017-06-14 13:26:15 -05:00
|
|
|
@pytest.mark.xfail(os.name != 'posix', reason="Not working on windows")
|
2017-05-07 16:46:44 +09:00
|
|
|
def test_LiteralIncludeReader_start_after_and_lines(literal_inc_path):
|
2017-02-18 14:49:56 +09:00
|
|
|
options = {'lineno-match': True, 'lines': '6-',
|
2019-01-02 21:53:07 +09:00
|
|
|
'start-after': 'Literally', 'end-before': 'comment'}
|
2017-05-07 16:46:44 +09:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-18 14:49:56 +09:00
|
|
|
content, lines = reader.read()
|
|
|
|
|
assert content == ("\n"
|
|
|
|
|
"class Bar:\n"
|
|
|
|
|
" def baz():\n"
|
|
|
|
|
" pass\n"
|
|
|
|
|
"\n")
|
2019-01-02 21:53:07 +09:00
|
|
|
assert reader.lineno_start == 7
|
2017-02-18 14:49:56 +09:00
|
|
|
|
|
|
|
|
|
2017-06-14 13:26:15 -05:00
|
|
|
@pytest.mark.xfail(os.name != 'posix', reason="Not working on windows")
|
2017-05-07 16:46:44 +09:00
|
|
|
def test_LiteralIncludeReader_start_at_and_lines(literal_inc_path):
|
2017-02-18 14:49:56 +09:00
|
|
|
options = {'lines': '2, 3, 5', 'start-at': 'foo', 'end-before': '#'}
|
2017-05-07 16:46:44 +09:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-18 14:49:56 +09:00
|
|
|
content, lines = reader.read()
|
|
|
|
|
assert content == ("\n"
|
|
|
|
|
"class Foo:\n"
|
|
|
|
|
"\n")
|
|
|
|
|
assert reader.lineno_start == 1
|
|
|
|
|
|
|
|
|
|
|
2017-05-07 16:46:44 +09:00
|
|
|
def test_LiteralIncludeReader_missing_start_and_end(literal_inc_path):
|
2017-03-26 18:13:31 +09:00
|
|
|
options = {'start-at': 'NOTHING'}
|
2017-05-07 16:46:44 +09:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-03-26 18:13:31 +09:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
content, lines = reader.read()
|
|
|
|
|
|
|
|
|
|
options = {'end-at': 'NOTHING'}
|
2017-05-07 16:46:44 +09:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-03-26 18:13:31 +09:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
content, lines = reader.read()
|
|
|
|
|
|
|
|
|
|
options = {'start-after': 'NOTHING'}
|
2017-05-07 16:46:44 +09:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-03-26 18:13:31 +09:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
content, lines = reader.read()
|
|
|
|
|
|
|
|
|
|
options = {'end-before': 'NOTHING'}
|
2017-05-07 16:46:44 +09:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-03-26 18:13:31 +09:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
content, lines = reader.read()
|
|
|
|
|
|
|
|
|
|
|
2019-10-20 15:44:19 +09: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-06-14 13:26:15 -05:00
|
|
|
@pytest.mark.xfail(os.name != 'posix', reason="Not working on windows")
|
2017-05-07 16:46:44 +09:00
|
|
|
def test_LiteralIncludeReader_prepend(literal_inc_path):
|
2017-02-12 17:27:57 +09:00
|
|
|
options = {'lines': '1', 'prepend': 'Hello', 'append': 'Sphinx'}
|
2017-05-07 16:46:44 +09:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-12 17:27:57 +09:00
|
|
|
content, lines = reader.read()
|
|
|
|
|
assert content == ("Hello\n"
|
|
|
|
|
"# Literally included file using Python highlighting\n"
|
|
|
|
|
"Sphinx\n")
|
|
|
|
|
|
|
|
|
|
|
2017-06-14 13:26:15 -05:00
|
|
|
@pytest.mark.xfail(os.name != 'posix', reason="Not working on windows")
|
2017-05-07 16:46:44 +09:00
|
|
|
def test_LiteralIncludeReader_dedent(literal_inc_path):
|
2017-02-12 17:27:57 +09:00
|
|
|
# dedent: 2
|
2019-01-02 21:53:07 +09:00
|
|
|
options = {'lines': '9-11', 'dedent': 2}
|
2017-05-07 16:46:44 +09:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-12 17:27:57 +09:00
|
|
|
content, lines = reader.read()
|
|
|
|
|
assert content == (" def baz():\n"
|
|
|
|
|
" pass\n"
|
|
|
|
|
"\n")
|
|
|
|
|
|
|
|
|
|
# dedent: 4
|
2019-01-02 21:53:07 +09:00
|
|
|
options = {'lines': '9-11', 'dedent': 4}
|
2017-05-07 16:46:44 +09:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-12 17:27:57 +09:00
|
|
|
content, lines = reader.read()
|
|
|
|
|
assert content == ("def baz():\n"
|
|
|
|
|
" pass\n"
|
|
|
|
|
"\n")
|
|
|
|
|
|
|
|
|
|
# dedent: 6
|
2019-01-02 21:53:07 +09:00
|
|
|
options = {'lines': '9-11', 'dedent': 6}
|
2017-05-07 16:46:44 +09:00
|
|
|
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
|
2017-02-12 17:27:57 +09:00
|
|
|
content, lines = reader.read()
|
|
|
|
|
assert content == ("f baz():\n"
|
|
|
|
|
" pass\n"
|
|
|
|
|
"\n")
|
|
|
|
|
|
2021-01-12 23:34:29 +09: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 17:27:57 +09:00
|
|
|
|
2017-06-14 13:26:15 -05:00
|
|
|
@pytest.mark.xfail(os.name != 'posix', reason="Not working on windows")
|
2017-05-07 16:46:44 +09:00
|
|
|
def test_LiteralIncludeReader_tabwidth(testroot):
|
2017-02-12 17:27:57 +09:00
|
|
|
# tab-width: 4
|
|
|
|
|
options = {'tab-width': 4, 'pyobject': 'Qux'}
|
2017-05-07 16:46:44 +09:00
|
|
|
reader = LiteralIncludeReader(testroot / 'target.py', options, DUMMY_CONFIG)
|
2017-02-12 17:27:57 +09: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 16:46:44 +09:00
|
|
|
reader = LiteralIncludeReader(testroot / 'target.py', options, DUMMY_CONFIG)
|
2017-02-12 17:27:57 +09:00
|
|
|
content, lines = reader.read()
|
|
|
|
|
assert content == ("class Qux:\n"
|
|
|
|
|
" def quux(self):\n"
|
|
|
|
|
" pass\n")
|
|
|
|
|
|
|
|
|
|
|
2017-06-14 13:26:15 -05:00
|
|
|
@pytest.mark.xfail(os.name != 'posix', reason="Not working on windows")
|
2017-05-07 16:46:44 +09:00
|
|
|
def test_LiteralIncludeReader_tabwidth_dedent(testroot):
|
2017-02-12 17:27:57 +09:00
|
|
|
options = {'tab-width': 4, 'dedent': 4, 'pyobject': 'Qux.quux'}
|
2017-05-07 16:46:44 +09:00
|
|
|
reader = LiteralIncludeReader(testroot / 'target.py', options, DUMMY_CONFIG)
|
2017-02-12 17:27:57 +09:00
|
|
|
content, lines = reader.read()
|
|
|
|
|
assert content == ("def quux(self):\n"
|
|
|
|
|
" pass\n")
|
|
|
|
|
|
|
|
|
|
|
2017-06-14 13:26:15 -05:00
|
|
|
@pytest.mark.xfail(os.name != 'posix', reason="Not working on windows")
|
2017-05-07 16:46:44 +09: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 17:27:57 +09:00
|
|
|
content, lines = reader.read()
|
2017-05-07 16:46:44 +09:00
|
|
|
assert content == ("--- " + testroot + "/literal-diff.inc\n"
|
|
|
|
|
"+++ " + testroot + "/literal.inc\n"
|
2019-01-02 21:53:07 +09:00
|
|
|
"@@ -6,8 +6,8 @@\n"
|
2017-02-12 17:27:57 +09: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 17:17:02 +02:00
|
|
|
|
2017-01-06 01:14:47 +09:00
|
|
|
|
|
|
|
|
@pytest.mark.sphinx('xml', testroot='directive-code')
|
2014-09-21 17:17:02 +02:00
|
|
|
def test_code_block(app, status, warning):
|
|
|
|
|
app.builder.build('index')
|
2016-12-15 13:12:13 +09:00
|
|
|
et = etree_parse(app.outdir / 'index.xml')
|
2014-09-21 17:17:02 +02:00
|
|
|
secs = et.findall('./section/section')
|
|
|
|
|
code_block = secs[0].findall('literal_block')
|
|
|
|
|
assert len(code_block) > 0
|
|
|
|
|
actual = code_block[0].text
|
|
|
|
|
expect = (
|
|
|
|
|
" def ruby?\n" +
|
|
|
|
|
" false\n" +
|
|
|
|
|
" end"
|
|
|
|
|
)
|
|
|
|
|
assert actual == expect
|
|
|
|
|
|
|
|
|
|
|
2019-05-30 10:44:48 +09:00
|
|
|
@pytest.mark.sphinx('html', testroot='directive-code')
|
|
|
|
|
def test_force_option(app, status, warning):
|
|
|
|
|
app.builder.build(['force'])
|
|
|
|
|
assert 'force.rst' not in warning.getvalue()
|
|
|
|
|
|
|
|
|
|
|
2017-01-06 01:14:47 +09:00
|
|
|
@pytest.mark.sphinx('html', testroot='directive-code')
|
2014-09-21 17:17:02 +02:00
|
|
|
def test_code_block_caption_html(app, status, warning):
|
|
|
|
|
app.builder.build(['caption'])
|
2020-02-01 11:58:51 +09:00
|
|
|
html = (app.outdir / 'caption.html').read_text()
|
2018-12-15 23:02:28 +09: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" '
|
|
|
|
|
'title="Permalink to this code">\xb6</a></div>')
|
2014-09-21 17:17:02 +02:00
|
|
|
assert caption in html
|
|
|
|
|
|
|
|
|
|
|
2017-01-06 01:14:47 +09:00
|
|
|
@pytest.mark.sphinx('latex', testroot='directive-code')
|
2014-09-21 17:17:02 +02:00
|
|
|
def test_code_block_caption_latex(app, status, warning):
|
|
|
|
|
app.builder.build_all()
|
2020-02-01 11:58:51 +09:00
|
|
|
latex = (app.outdir / 'python.tex').read_text()
|
2016-06-11 19:15:47 +02:00
|
|
|
caption = '\\sphinxSetupCaptionForVerbatim{caption \\sphinxstyleemphasis{test} rb}'
|
2016-12-08 21:43:04 +01:00
|
|
|
label = '\\def\\sphinxLiteralBlockLabel{\\label{\\detokenize{caption:id1}}}'
|
2017-02-13 02:02:51 +09:00
|
|
|
link = '\\hyperref[\\detokenize{caption:name-test-rb}]' \
|
2017-01-26 01:13:17 +09:00
|
|
|
'{Listing \\ref{\\detokenize{caption:name-test-rb}}}'
|
2014-09-21 17:17:02 +02:00
|
|
|
assert caption in latex
|
2016-04-10 20:42:00 +02:00
|
|
|
assert label in latex
|
|
|
|
|
assert link in latex
|
|
|
|
|
|
|
|
|
|
|
2017-01-06 01:14:47 +09:00
|
|
|
@pytest.mark.sphinx('latex', testroot='directive-code')
|
2016-04-10 20:42:00 +02:00
|
|
|
def test_code_block_namedlink_latex(app, status, warning):
|
|
|
|
|
app.builder.build_all()
|
2020-02-01 11:58:51 +09:00
|
|
|
latex = (app.outdir / 'python.tex').read_text()
|
2016-12-08 21:43:04 +01:00
|
|
|
label1 = '\\def\\sphinxLiteralBlockLabel{\\label{\\detokenize{caption:name-test-rb}}}'
|
2017-01-26 01:13:17 +09:00
|
|
|
link1 = '\\hyperref[\\detokenize{caption:name-test-rb}]'\
|
|
|
|
|
'{\\sphinxcrossref{\\DUrole{std,std-ref}{Ruby}}'
|
2016-12-08 21:43:04 +01:00
|
|
|
label2 = ('\\def\\sphinxLiteralBlockLabel'
|
|
|
|
|
'{\\label{\\detokenize{namedblocks:some-ruby-code}}}')
|
2017-01-26 01:13:17 +09:00
|
|
|
link2 = '\\hyperref[\\detokenize{namedblocks:some-ruby-code}]'\
|
|
|
|
|
'{\\sphinxcrossref{\\DUrole{std,std-ref}{the ruby code}}}'
|
2016-04-10 20:42:00 +02:00
|
|
|
assert label1 in latex
|
2016-06-12 00:00:52 +09:00
|
|
|
assert link1 in latex
|
2016-04-10 20:42:00 +02:00
|
|
|
assert label2 in latex
|
2016-06-12 00:00:52 +09:00
|
|
|
assert link2 in latex
|
2014-09-21 17:17:02 +02:00
|
|
|
|
|
|
|
|
|
2017-12-10 12:16:35 +01:00
|
|
|
@pytest.mark.sphinx('latex', testroot='directive-code')
|
|
|
|
|
def test_code_block_emphasize_latex(app, status, warning):
|
|
|
|
|
app.builder.build(['emphasize'])
|
2020-02-01 11:58:51 +09:00
|
|
|
latex = (app.outdir / 'python.tex').read_text().replace('\r\n', '\n')
|
2019-01-02 21:53:07 +09:00
|
|
|
includes = '\\fvset{hllines={, 5, 6, 13, 14, 15, 24, 25, 26,}}%\n'
|
2017-12-10 12:16:35 +01:00
|
|
|
assert includes in latex
|
2018-12-16 13:37:47 -08:00
|
|
|
includes = '\\end{sphinxVerbatim}\n\\sphinxresetverbatimhllines\n'
|
2018-12-16 16:34:36 +01:00
|
|
|
assert includes in latex
|
2017-12-10 12:16:35 +01:00
|
|
|
|
|
|
|
|
|
2017-01-06 01:14:47 +09:00
|
|
|
@pytest.mark.sphinx('xml', testroot='directive-code')
|
2014-09-21 17:17:02 +02:00
|
|
|
def test_literal_include(app, status, warning):
|
|
|
|
|
app.builder.build(['index'])
|
2016-12-15 13:12:13 +09:00
|
|
|
et = etree_parse(app.outdir / 'index.xml')
|
2014-09-21 17:17:02 +02:00
|
|
|
secs = et.findall('./section/section')
|
|
|
|
|
literal_include = secs[1].findall('literal_block')
|
2020-02-01 11:58:51 +09:00
|
|
|
literal_src = (app.srcdir / 'literal.inc').read_text()
|
2014-09-21 17:17:02 +02:00
|
|
|
assert len(literal_include) > 0
|
|
|
|
|
actual = literal_include[0].text
|
|
|
|
|
assert actual == literal_src
|
|
|
|
|
|
|
|
|
|
|
2017-01-06 01:14:47 +09:00
|
|
|
@pytest.mark.sphinx('xml', testroot='directive-code')
|
2016-01-20 22:48:16 +09:00
|
|
|
def test_literal_include_block_start_with_comment_or_brank(app, status, warning):
|
|
|
|
|
app.builder.build(['python'])
|
2016-12-15 13:12:13 +09:00
|
|
|
et = etree_parse(app.outdir / 'python.xml')
|
2016-01-20 22:48:16 +09: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-06 01:14:47 +09:00
|
|
|
@pytest.mark.sphinx('html', testroot='directive-code')
|
2014-10-04 17:48:55 +02:00
|
|
|
def test_literal_include_linenos(app, status, warning):
|
|
|
|
|
app.builder.build(['linenos'])
|
2020-02-01 11:58:51 +09:00
|
|
|
html = (app.outdir / 'linenos.html').read_text()
|
2014-10-04 17:48:55 +02:00
|
|
|
|
2017-02-18 00:12:54 +09:00
|
|
|
# :linenos:
|
|
|
|
|
assert ('<td class="linenos"><div class="linenodiv"><pre>'
|
|
|
|
|
' 1\n'
|
|
|
|
|
' 2\n'
|
|
|
|
|
' 3\n'
|
|
|
|
|
' 4\n'
|
|
|
|
|
' 5\n'
|
|
|
|
|
' 6\n'
|
|
|
|
|
' 7\n'
|
|
|
|
|
' 8\n'
|
|
|
|
|
' 9\n'
|
|
|
|
|
'10\n'
|
|
|
|
|
'11\n'
|
|
|
|
|
'12\n'
|
2019-01-02 21:53:07 +09:00
|
|
|
'13</pre></div></td>' in html)
|
2017-02-18 00:12:54 +09:00
|
|
|
|
|
|
|
|
# :lineno-start:
|
|
|
|
|
assert ('<td class="linenos"><div class="linenodiv"><pre>'
|
|
|
|
|
'200\n'
|
|
|
|
|
'201\n'
|
|
|
|
|
'202\n'
|
|
|
|
|
'203\n'
|
|
|
|
|
'204\n'
|
|
|
|
|
'205\n'
|
|
|
|
|
'206\n'
|
|
|
|
|
'207\n'
|
|
|
|
|
'208\n'
|
|
|
|
|
'209\n'
|
|
|
|
|
'210\n'
|
|
|
|
|
'211\n'
|
2019-01-02 21:53:07 +09:00
|
|
|
'212</pre></div></td>' in html)
|
2017-02-18 00:12:54 +09:00
|
|
|
|
|
|
|
|
# :lineno-match:
|
|
|
|
|
assert ('<td class="linenos"><div class="linenodiv"><pre>'
|
|
|
|
|
'5\n'
|
|
|
|
|
'6\n'
|
|
|
|
|
'7\n'
|
|
|
|
|
'8\n'
|
|
|
|
|
'9</pre></div></td>' in html)
|
2016-08-10 10:49:00 -04:00
|
|
|
|
2014-10-04 17:48:55 +02:00
|
|
|
|
2017-01-06 01:14:47 +09:00
|
|
|
@pytest.mark.sphinx('latex', testroot='directive-code')
|
2015-03-30 20:34:00 +09:00
|
|
|
def test_literalinclude_file_whole_of_emptyline(app, status, warning):
|
|
|
|
|
app.builder.build_all()
|
2020-02-01 11:58:51 +09:00
|
|
|
latex = (app.outdir / 'python.tex').read_text().replace('\r\n', '\n')
|
2015-03-30 20:34:00 +09:00
|
|
|
includes = (
|
2016-07-07 16:36:44 +03:00
|
|
|
'\\begin{sphinxVerbatim}'
|
|
|
|
|
'[commandchars=\\\\\\{\\},numbers=left,firstnumber=1,stepnumber=1]\n'
|
2015-03-30 20:34:00 +09:00
|
|
|
'\n'
|
|
|
|
|
'\n'
|
|
|
|
|
'\n'
|
2016-06-15 18:34:00 +02:00
|
|
|
'\\end{sphinxVerbatim}\n')
|
2015-03-30 20:34:00 +09:00
|
|
|
assert includes in latex
|
|
|
|
|
|
|
|
|
|
|
2017-01-06 01:14:47 +09:00
|
|
|
@pytest.mark.sphinx('html', testroot='directive-code')
|
2014-09-21 17:17:02 +02:00
|
|
|
def test_literalinclude_caption_html(app, status, warning):
|
|
|
|
|
app.builder.build('index')
|
2020-02-01 11:58:51 +09:00
|
|
|
html = (app.outdir / 'caption.html').read_text()
|
2018-12-15 23:02:28 +09: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" '
|
|
|
|
|
'title="Permalink to this code">\xb6</a></div>')
|
2014-09-21 17:17:02 +02:00
|
|
|
assert caption in html
|
|
|
|
|
|
|
|
|
|
|
2017-01-06 01:14:47 +09:00
|
|
|
@pytest.mark.sphinx('latex', testroot='directive-code')
|
2014-09-21 17:17:02 +02:00
|
|
|
def test_literalinclude_caption_latex(app, status, warning):
|
|
|
|
|
app.builder.build('index')
|
2020-02-01 11:58:51 +09:00
|
|
|
latex = (app.outdir / 'python.tex').read_text()
|
2016-06-11 19:15:47 +02:00
|
|
|
caption = '\\sphinxSetupCaptionForVerbatim{caption \\sphinxstylestrong{test} py}'
|
2016-12-08 21:43:04 +01:00
|
|
|
label = '\\def\\sphinxLiteralBlockLabel{\\label{\\detokenize{caption:id2}}}'
|
2017-02-13 02:02:51 +09:00
|
|
|
link = '\\hyperref[\\detokenize{caption:name-test-py}]' \
|
2017-01-26 01:13:17 +09:00
|
|
|
'{Listing \\ref{\\detokenize{caption:name-test-py}}}'
|
2014-09-21 17:17:02 +02:00
|
|
|
assert caption in latex
|
2016-04-10 20:42:00 +02:00
|
|
|
assert label in latex
|
|
|
|
|
assert link in latex
|
|
|
|
|
|
|
|
|
|
|
2017-01-06 01:14:47 +09:00
|
|
|
@pytest.mark.sphinx('latex', testroot='directive-code')
|
2016-04-10 20:42:00 +02:00
|
|
|
def test_literalinclude_namedlink_latex(app, status, warning):
|
|
|
|
|
app.builder.build('index')
|
2020-02-01 11:58:51 +09:00
|
|
|
latex = (app.outdir / 'python.tex').read_text()
|
2016-12-08 21:43:04 +01:00
|
|
|
label1 = '\\def\\sphinxLiteralBlockLabel{\\label{\\detokenize{caption:name-test-py}}}'
|
2017-01-26 01:13:17 +09:00
|
|
|
link1 = '\\hyperref[\\detokenize{caption:name-test-py}]'\
|
|
|
|
|
'{\\sphinxcrossref{\\DUrole{std,std-ref}{Python}}'
|
2016-12-08 21:43:04 +01:00
|
|
|
label2 = ('\\def\\sphinxLiteralBlockLabel'
|
|
|
|
|
'{\\label{\\detokenize{namedblocks:some-python-code}}}')
|
2017-01-26 01:13:17 +09:00
|
|
|
link2 = '\\hyperref[\\detokenize{namedblocks:some-python-code}]'\
|
|
|
|
|
'{\\sphinxcrossref{\\DUrole{std,std-ref}{the python code}}}'
|
2016-04-10 20:42:00 +02:00
|
|
|
assert label1 in latex
|
2016-06-12 00:00:52 +09:00
|
|
|
assert link1 in latex
|
2016-04-10 20:42:00 +02:00
|
|
|
assert label2 in latex
|
2016-06-12 00:00:52 +09:00
|
|
|
assert link2 in latex
|
2015-12-20 22:44:32 +09:00
|
|
|
|
|
|
|
|
|
2017-01-06 01:14:47 +09:00
|
|
|
@pytest.mark.sphinx('xml', testroot='directive-code')
|
2015-12-20 22:44:32 +09:00
|
|
|
def test_literalinclude_classes(app, status, warning):
|
|
|
|
|
app.builder.build(['classes'])
|
2016-12-15 13:12:13 +09:00
|
|
|
et = etree_parse(app.outdir / 'classes.xml')
|
2015-12-20 22:44:32 +09:00
|
|
|
secs = et.findall('./section/section')
|
|
|
|
|
|
|
|
|
|
code_block = secs[0].findall('literal_block')
|
|
|
|
|
assert len(code_block) > 0
|
|
|
|
|
assert 'foo bar' == code_block[0].get('classes')
|
|
|
|
|
assert 'code_block' == code_block[0].get('names')
|
|
|
|
|
|
|
|
|
|
literalinclude = secs[1].findall('literal_block')
|
|
|
|
|
assert len(literalinclude) > 0
|
|
|
|
|
assert 'bar baz' == literalinclude[0].get('classes')
|
|
|
|
|
assert 'literal_include' == literalinclude[0].get('names')
|
2017-03-11 11:46:03 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.sphinx('xml', testroot='directive-code')
|
|
|
|
|
def test_literalinclude_pydecorators(app, status, warning):
|
|
|
|
|
app.builder.build(['py-decorators'])
|
|
|
|
|
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-04 01:50:16 +09:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.sphinx('dummy', testroot='directive-code')
|
|
|
|
|
def test_code_block_highlighted(app, status, warning):
|
|
|
|
|
app.builder.build(['highlight'])
|
|
|
|
|
doctree = app.env.get_doctree('highlight')
|
2019-12-14 11:40:50 +09:00
|
|
|
codeblocks = list(doctree.traverse(nodes.literal_block))
|
2019-02-04 01:50:16 +09:00
|
|
|
|
|
|
|
|
assert codeblocks[0]['language'] == 'default'
|
|
|
|
|
assert codeblocks[1]['language'] == 'python2'
|
|
|
|
|
assert codeblocks[2]['language'] == 'python3'
|
|
|
|
|
assert codeblocks[3]['language'] == 'python2'
|
2019-02-16 21:44:32 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.sphinx('html', testroot='directive-code')
|
|
|
|
|
def test_linenothreshold(app, status, warning):
|
|
|
|
|
app.builder.build(['linenothreshold'])
|
2020-02-01 11:58:51 +09:00
|
|
|
html = (app.outdir / 'linenothreshold.html').read_text()
|
2019-02-16 21:44:32 -05:00
|
|
|
|
|
|
|
|
lineos_head = '<td class="linenos"><div class="linenodiv"><pre>'
|
|
|
|
|
lineos_tail = '</pre></div></td>'
|
|
|
|
|
|
|
|
|
|
# code-block using linenothreshold
|
|
|
|
|
_, matched, html = html.partition(lineos_head +
|
|
|
|
|
'1\n'
|
|
|
|
|
'2\n'
|
|
|
|
|
'3\n'
|
|
|
|
|
'4\n'
|
|
|
|
|
'5\n'
|
|
|
|
|
'6' + lineos_tail)
|
|
|
|
|
assert matched
|
|
|
|
|
|
|
|
|
|
# code-block not using linenothreshold
|
|
|
|
|
html, matched, _ = html.partition(lineos_head +
|
|
|
|
|
'1\n'
|
|
|
|
|
'2' + lineos_tail)
|
|
|
|
|
assert not matched
|
|
|
|
|
|
|
|
|
|
# literal include using linenothreshold
|
|
|
|
|
_, matched, html = html.partition(lineos_head +
|
|
|
|
|
' 1\n'
|
|
|
|
|
' 2\n'
|
|
|
|
|
' 3\n'
|
|
|
|
|
' 4\n'
|
|
|
|
|
' 5\n'
|
|
|
|
|
' 6\n'
|
|
|
|
|
' 7\n'
|
|
|
|
|
' 8\n'
|
|
|
|
|
' 9\n'
|
|
|
|
|
'10\n'
|
|
|
|
|
'11\n'
|
|
|
|
|
'12\n'
|
|
|
|
|
'13' + lineos_tail)
|
|
|
|
|
assert matched
|
|
|
|
|
|
|
|
|
|
# literal include not using linenothreshold
|
|
|
|
|
html, matched, _ = html.partition(lineos_head +
|
|
|
|
|
'1\n'
|
2019-06-30 14:55:22 +09:00
|
|
|
'2\n'
|
2019-02-16 21:44:32 -05:00
|
|
|
'3' + lineos_tail)
|
|
|
|
|
assert not matched
|