2014-09-21 10:17:02 -05:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
test_directive_code
|
|
|
|
~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Test the code-block directive.
|
|
|
|
|
2016-01-14 15:54:04 -06:00
|
|
|
:copyright: Copyright 2007-2016 by the Sphinx team, see AUTHORS.
|
2014-09-21 10:17:02 -05:00
|
|
|
:license: BSD, see LICENSE for details.
|
|
|
|
"""
|
|
|
|
|
2017-01-05 10:14:47 -06:00
|
|
|
import pytest
|
2014-09-21 10:17:02 -05:00
|
|
|
|
2017-02-12 02:27:57 -06:00
|
|
|
from sphinx.config import Config
|
|
|
|
from sphinx.directives.code import LiteralIncludeReader
|
|
|
|
from util import etree_parse, rootdir
|
|
|
|
|
|
|
|
TESTROOT_PATH = rootdir / 'roots' / 'test-directive-code'
|
|
|
|
LITERAL_INC_PATH = TESTROOT_PATH / 'literal.inc'
|
|
|
|
DUMMY_CONFIG = Config(None, None, {}, '')
|
|
|
|
|
|
|
|
|
|
|
|
def test_LiteralIncludeReader():
|
2017-02-17 08:03:05 -06:00
|
|
|
options = {'lineno-match': True}
|
2017-02-12 02:27:57 -06:00
|
|
|
reader = LiteralIncludeReader(LITERAL_INC_PATH, options, DUMMY_CONFIG)
|
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == LITERAL_INC_PATH.text()
|
|
|
|
assert lines == 14
|
2017-02-17 08:03:05 -06:00
|
|
|
assert reader.lineno_start == 1
|
|
|
|
|
|
|
|
|
|
|
|
def test_LiteralIncludeReader_lineno_start():
|
|
|
|
options = {'lineno-start': 5}
|
|
|
|
reader = LiteralIncludeReader(LITERAL_INC_PATH, options, DUMMY_CONFIG)
|
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == LITERAL_INC_PATH.text()
|
|
|
|
assert lines == 14
|
|
|
|
assert reader.lineno_start == 5
|
2017-02-12 02:27:57 -06:00
|
|
|
|
|
|
|
|
|
|
|
def test_LiteralIncludeReader_pyobject1():
|
2017-02-17 08:03:05 -06:00
|
|
|
options = {'lineno-match': True, 'pyobject': 'Foo'}
|
2017-02-12 02:27:57 -06:00
|
|
|
reader = LiteralIncludeReader(LITERAL_INC_PATH, options, DUMMY_CONFIG)
|
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == ("class Foo:\n"
|
|
|
|
" pass\n")
|
2017-02-17 08:03:05 -06:00
|
|
|
assert reader.lineno_start == 6
|
2017-02-12 02:27:57 -06:00
|
|
|
|
|
|
|
|
|
|
|
def test_LiteralIncludeReader_pyobject2():
|
|
|
|
options = {'pyobject': 'Bar'}
|
|
|
|
reader = LiteralIncludeReader(LITERAL_INC_PATH, options, DUMMY_CONFIG)
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
def test_LiteralIncludeReader_pyobject3():
|
|
|
|
options = {'pyobject': 'Bar.baz'}
|
|
|
|
reader = LiteralIncludeReader(LITERAL_INC_PATH, options, DUMMY_CONFIG)
|
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == (" def baz():\n"
|
|
|
|
" pass\n")
|
|
|
|
|
|
|
|
|
|
|
|
def test_LiteralIncludeReader_lines1():
|
|
|
|
options = {'lines': '1-4'}
|
|
|
|
reader = LiteralIncludeReader(LITERAL_INC_PATH, options, DUMMY_CONFIG)
|
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == (u"# Literally included file using Python highlighting\n"
|
|
|
|
u"# -*- coding: utf-8 -*-\n"
|
|
|
|
u"\n"
|
|
|
|
u"foo = \"Including Unicode characters: üöä\"\n")
|
|
|
|
|
|
|
|
|
|
|
|
def test_LiteralIncludeReader_lines2():
|
|
|
|
options = {'lines': '1,4,6'}
|
|
|
|
reader = LiteralIncludeReader(LITERAL_INC_PATH, options, DUMMY_CONFIG)
|
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == (u"# Literally included file using Python highlighting\n"
|
|
|
|
u"foo = \"Including Unicode characters: üöä\"\n"
|
|
|
|
u"class Foo:\n")
|
|
|
|
|
|
|
|
|
2017-02-17 08:03:05 -06:00
|
|
|
def test_LiteralIncludeReader_lines_and_lineno_match1():
|
|
|
|
options = {'lines': '4-6', 'lineno-match': True}
|
|
|
|
reader = LiteralIncludeReader(LITERAL_INC_PATH, options, DUMMY_CONFIG)
|
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == (u"foo = \"Including Unicode characters: üöä\"\n"
|
|
|
|
u"\n"
|
|
|
|
u"class Foo:\n")
|
|
|
|
assert reader.lineno_start == 4
|
|
|
|
|
|
|
|
|
2017-02-12 02:27:57 -06:00
|
|
|
@pytest.mark.sphinx() # init locale for errors
|
2017-02-17 08:03:05 -06:00
|
|
|
def test_LiteralIncludeReader_lines_and_lineno_match2(app, status, warning):
|
2017-02-12 02:27:57 -06:00
|
|
|
options = {'lines': '1,4,6', 'lineno-match': True}
|
|
|
|
reader = LiteralIncludeReader(LITERAL_INC_PATH, options, DUMMY_CONFIG)
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
content, lines = reader.read()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.sphinx() # init locale for errors
|
2017-02-17 08:03:05 -06:00
|
|
|
def test_LiteralIncludeReader_lines_and_lineno_match3(app, status, warning):
|
2017-02-12 02:27:57 -06:00
|
|
|
options = {'lines': '100-', 'lineno-match': True}
|
|
|
|
reader = LiteralIncludeReader(LITERAL_INC_PATH, options, DUMMY_CONFIG)
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
content, lines = reader.read()
|
|
|
|
|
|
|
|
|
|
|
|
def test_LiteralIncludeReader_start_at():
|
2017-02-17 08:03:05 -06:00
|
|
|
options = {'lineno-match': True, 'start-at': 'Foo', 'end-at': 'Bar'}
|
2017-02-12 02:27:57 -06:00
|
|
|
reader = LiteralIncludeReader(LITERAL_INC_PATH, options, DUMMY_CONFIG)
|
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == ("class Foo:\n"
|
|
|
|
" pass\n"
|
|
|
|
"\n"
|
|
|
|
"class Bar:\n")
|
2017-02-17 08:03:05 -06:00
|
|
|
assert reader.lineno_start == 6
|
2017-02-12 02:27:57 -06:00
|
|
|
|
|
|
|
|
|
|
|
def test_LiteralIncludeReader_start_after():
|
2017-02-17 08:03:05 -06:00
|
|
|
options = {'lineno-match': True, 'start-after': 'Foo', 'end-before': 'Bar'}
|
2017-02-12 02:27:57 -06:00
|
|
|
reader = LiteralIncludeReader(LITERAL_INC_PATH, options, DUMMY_CONFIG)
|
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == (" pass\n"
|
|
|
|
"\n")
|
2017-02-17 08:03:05 -06:00
|
|
|
assert reader.lineno_start == 7
|
2017-02-12 02:27:57 -06:00
|
|
|
|
|
|
|
|
2017-02-17 23:49:56 -06:00
|
|
|
def test_LiteralIncludeReader_start_after_and_lines():
|
|
|
|
options = {'lineno-match': True, 'lines': '6-',
|
|
|
|
'start-after': 'coding', 'end-before': 'comment'}
|
|
|
|
reader = LiteralIncludeReader(LITERAL_INC_PATH, options, DUMMY_CONFIG)
|
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == ("\n"
|
|
|
|
"class Bar:\n"
|
|
|
|
" def baz():\n"
|
|
|
|
" pass\n"
|
|
|
|
"\n")
|
|
|
|
assert reader.lineno_start == 8
|
|
|
|
|
|
|
|
|
|
|
|
def test_LiteralIncludeReader_start_at_and_lines2():
|
|
|
|
options = {'lines': '2, 3, 5', 'start-at': 'foo', 'end-before': '#'}
|
|
|
|
reader = LiteralIncludeReader(LITERAL_INC_PATH, options, DUMMY_CONFIG)
|
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == ("\n"
|
|
|
|
"class Foo:\n"
|
|
|
|
"\n")
|
|
|
|
assert reader.lineno_start == 1
|
|
|
|
|
|
|
|
|
2017-02-12 02:27:57 -06:00
|
|
|
def test_LiteralIncludeReader_prepend():
|
|
|
|
options = {'lines': '1', 'prepend': 'Hello', 'append': 'Sphinx'}
|
|
|
|
reader = LiteralIncludeReader(LITERAL_INC_PATH, options, DUMMY_CONFIG)
|
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == ("Hello\n"
|
|
|
|
"# Literally included file using Python highlighting\n"
|
|
|
|
"Sphinx\n")
|
|
|
|
|
|
|
|
|
|
|
|
def test_LiteralIncludeReader_dedent():
|
|
|
|
# dedent: 2
|
|
|
|
options = {'lines': '10-12', 'dedent': 2}
|
|
|
|
reader = LiteralIncludeReader(LITERAL_INC_PATH, options, DUMMY_CONFIG)
|
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == (" def baz():\n"
|
|
|
|
" pass\n"
|
|
|
|
"\n")
|
|
|
|
|
|
|
|
# dedent: 4
|
|
|
|
options = {'lines': '10-12', 'dedent': 4}
|
|
|
|
reader = LiteralIncludeReader(LITERAL_INC_PATH, options, DUMMY_CONFIG)
|
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == ("def baz():\n"
|
|
|
|
" pass\n"
|
|
|
|
"\n")
|
|
|
|
|
|
|
|
# dedent: 6
|
|
|
|
options = {'lines': '10-12', 'dedent': 6}
|
|
|
|
reader = LiteralIncludeReader(LITERAL_INC_PATH, options, DUMMY_CONFIG)
|
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == ("f baz():\n"
|
|
|
|
" pass\n"
|
|
|
|
"\n")
|
|
|
|
|
|
|
|
|
|
|
|
def test_LiteralIncludeReader_tabwidth():
|
|
|
|
# tab-width: 4
|
|
|
|
options = {'tab-width': 4, 'pyobject': 'Qux'}
|
|
|
|
reader = LiteralIncludeReader(TESTROOT_PATH / 'target.py', options, DUMMY_CONFIG)
|
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == ("class Qux:\n"
|
|
|
|
" def quux(self):\n"
|
|
|
|
" pass\n")
|
|
|
|
|
|
|
|
# tab-width: 8
|
|
|
|
options = {'tab-width': 8, 'pyobject': 'Qux'}
|
|
|
|
reader = LiteralIncludeReader(TESTROOT_PATH / 'target.py', options, DUMMY_CONFIG)
|
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == ("class Qux:\n"
|
|
|
|
" def quux(self):\n"
|
|
|
|
" pass\n")
|
|
|
|
|
|
|
|
|
|
|
|
def test_LiteralIncludeReader_tabwidth_dedent():
|
|
|
|
options = {'tab-width': 4, 'dedent': 4, 'pyobject': 'Qux.quux'}
|
|
|
|
reader = LiteralIncludeReader(TESTROOT_PATH / 'target.py', options, DUMMY_CONFIG)
|
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == ("def quux(self):\n"
|
|
|
|
" pass\n")
|
|
|
|
|
|
|
|
|
|
|
|
def test_LiteralIncludeReader_diff():
|
|
|
|
options = {'diff': TESTROOT_PATH / 'literal-diff.inc'}
|
|
|
|
reader = LiteralIncludeReader(LITERAL_INC_PATH, options, DUMMY_CONFIG)
|
|
|
|
content, lines = reader.read()
|
|
|
|
assert content == ("--- " + TESTROOT_PATH + "/literal-diff.inc\n"
|
|
|
|
"+++ " + TESTROOT_PATH + "/literal.inc\n"
|
|
|
|
"@@ -7,8 +7,8 @@\n"
|
|
|
|
" 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')
|
2014-09-21 10:17:02 -05:00
|
|
|
def test_code_block(app, status, warning):
|
|
|
|
app.builder.build('index')
|
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 = (
|
|
|
|
" def ruby?\n" +
|
|
|
|
" false\n" +
|
|
|
|
" end"
|
|
|
|
)
|
|
|
|
assert actual == expect
|
|
|
|
|
|
|
|
|
2017-01-05 10:14:47 -06:00
|
|
|
@pytest.mark.sphinx('html', testroot='directive-code')
|
2014-09-21 10:17:02 -05:00
|
|
|
def test_code_block_caption_html(app, status, warning):
|
|
|
|
app.builder.build(['caption'])
|
2014-10-05 02:27:38 -05:00
|
|
|
html = (app.outdir / 'caption.html').text(encoding='utf-8')
|
|
|
|
caption = (u'<div class="code-block-caption">'
|
2016-04-10 13:42:00 -05:00
|
|
|
u'<span class="caption-number">Listing 1 </span>'
|
2014-10-05 06:55:46 -05:00
|
|
|
u'<span class="caption-text">caption <em>test</em> rb'
|
2016-09-17 04:13:17 -05:00
|
|
|
u'</span><a class="headerlink" href="#id1" '
|
2014-10-05 02:27:38 -05:00
|
|
|
u'title="Permalink 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')
|
2014-09-21 10:17:02 -05:00
|
|
|
def test_code_block_caption_latex(app, status, warning):
|
|
|
|
app.builder.build_all()
|
2015-07-25 12:02:47 -05:00
|
|
|
latex = (app.outdir / 'Python.tex').text(encoding='utf-8')
|
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}}}'
|
2017-02-12 11:02:51 -06:00
|
|
|
link = '\\hyperref[\\detokenize{caption:name-test-rb}]' \
|
2017-01-25 10:13:17 -06:00
|
|
|
'{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')
|
2016-04-10 13:42:00 -05:00
|
|
|
def test_code_block_namedlink_latex(app, status, warning):
|
|
|
|
app.builder.build_all()
|
|
|
|
latex = (app.outdir / 'Python.tex').text(encoding='utf-8')
|
2016-12-08 14:43:04 -06:00
|
|
|
label1 = '\\def\\sphinxLiteralBlockLabel{\\label{\\detokenize{caption:name-test-rb}}}'
|
2017-01-25 10:13:17 -06: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}}}')
|
2017-01-25 10:13:17 -06: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-01-05 10:14:47 -06:00
|
|
|
@pytest.mark.sphinx('xml', testroot='directive-code')
|
2014-09-21 10:17:02 -05:00
|
|
|
def test_literal_include(app, status, warning):
|
|
|
|
app.builder.build(['index'])
|
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')
|
|
|
|
literal_src = (app.srcdir / 'literal.inc').text(encoding='utf-8')
|
|
|
|
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')
|
2016-01-20 07:48:16 -06:00
|
|
|
def test_literal_include_block_start_with_comment_or_brank(app, status, warning):
|
|
|
|
app.builder.build(['python'])
|
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')
|
2014-10-04 10:48:55 -05:00
|
|
|
def test_literal_include_linenos(app, status, warning):
|
|
|
|
app.builder.build(['linenos'])
|
2015-07-25 12:02:47 -05:00
|
|
|
html = (app.outdir / 'linenos.html').text(encoding='utf-8')
|
2014-10-04 10:48:55 -05:00
|
|
|
|
2017-02-17 09:12:54 -06: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'
|
|
|
|
'13\n'
|
|
|
|
'14</pre></div></td>' in html)
|
|
|
|
|
|
|
|
# :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'
|
|
|
|
'212\n'
|
|
|
|
'213</pre></div></td>' in html)
|
|
|
|
|
|
|
|
# :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 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')
|
2015-03-30 06:34:00 -05:00
|
|
|
def test_literalinclude_file_whole_of_emptyline(app, status, warning):
|
|
|
|
app.builder.build_all()
|
2015-07-25 12:02:47 -05:00
|
|
|
latex = (app.outdir / 'Python.tex').text(encoding='utf-8').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')
|
2014-09-21 10:17:02 -05:00
|
|
|
def test_literalinclude_caption_html(app, status, warning):
|
|
|
|
app.builder.build('index')
|
2014-10-05 02:27:38 -05:00
|
|
|
html = (app.outdir / 'caption.html').text(encoding='utf-8')
|
|
|
|
caption = (u'<div class="code-block-caption">'
|
2016-04-10 13:42:00 -05:00
|
|
|
u'<span class="caption-number">Listing 2 </span>'
|
2014-10-05 06:55:46 -05:00
|
|
|
u'<span class="caption-text">caption <strong>test</strong> py'
|
2016-09-17 04:13:17 -05:00
|
|
|
u'</span><a class="headerlink" href="#id2" '
|
2014-10-05 02:27:38 -05:00
|
|
|
u'title="Permalink 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')
|
2014-09-21 10:17:02 -05:00
|
|
|
def test_literalinclude_caption_latex(app, status, warning):
|
|
|
|
app.builder.build('index')
|
2015-07-25 12:02:47 -05:00
|
|
|
latex = (app.outdir / 'Python.tex').text(encoding='utf-8')
|
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}}}'
|
2017-02-12 11:02:51 -06:00
|
|
|
link = '\\hyperref[\\detokenize{caption:name-test-py}]' \
|
2017-01-25 10:13:17 -06:00
|
|
|
'{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')
|
2016-04-10 13:42:00 -05:00
|
|
|
def test_literalinclude_namedlink_latex(app, status, warning):
|
|
|
|
app.builder.build('index')
|
|
|
|
latex = (app.outdir / 'Python.tex').text(encoding='utf-8')
|
2016-12-08 14:43:04 -06:00
|
|
|
label1 = '\\def\\sphinxLiteralBlockLabel{\\label{\\detokenize{caption:name-test-py}}}'
|
2017-01-25 10:13:17 -06: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}}}')
|
2017-01-25 10:13:17 -06: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')
|
2015-12-20 07:44:32 -06:00
|
|
|
def test_literalinclude_classes(app, status, warning):
|
|
|
|
app.builder.build(['classes'])
|
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
|
|
|
|
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')
|