sphinx/tests/test_domains/test_domain_rst.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

280 lines
6.9 KiB
Python
Raw Normal View History

"""Tests the reStructuredText domain."""
import pytest
2019-04-12 06:58:10 -05:00
from sphinx import addnodes
from sphinx.addnodes import (
desc,
desc_addname,
desc_annotation,
desc_content,
desc_name,
desc_signature,
)
from sphinx.domains.rst import parse_directive
2019-04-12 06:58:10 -05:00
from sphinx.testing import restructuredtext
from sphinx.testing.util import assert_node
2016-06-11 10:00:52 -05:00
def test_parse_directive():
2018-12-15 08:02:28 -06:00
s = parse_directive(' foö ')
assert s == ('foö', '')
2018-12-15 08:02:28 -06:00
s = parse_directive(' .. foö :: ')
assert s == ('foö', '')
2018-12-15 08:02:28 -06:00
s = parse_directive('.. foö:: args1 args2')
assert s == ('foö', ' args1 args2')
s = parse_directive('.. :: bar')
assert s == ('.. :: bar', '')
2019-04-12 06:58:10 -05:00
@pytest.mark.sphinx('html', testroot='root')
2019-04-12 06:58:10 -05:00
def test_rst_directive(app):
# bare
2024-08-11 08:58:56 -05:00
text = '.. rst:directive:: toctree'
2019-04-12 06:58:10 -05:00
doctree = restructuredtext.parse(app, text)
2024-08-11 08:58:56 -05:00
assert_node(
doctree,
(
addnodes.index,
[desc, ([desc_signature, desc_name, '.. toctree::'], [desc_content, ()])],
),
)
assert_node(
doctree[0],
entries=[('single', 'toctree (directive)', 'directive-toctree', '', None)],
)
assert_node(
doctree[1],
addnodes.desc,
desctype='directive',
domain='rst',
objtype='directive',
no_index=False,
)
2019-04-12 06:58:10 -05:00
# decorated
2024-08-11 08:58:56 -05:00
text = '.. rst:directive:: .. toctree::'
2019-04-12 06:58:10 -05:00
doctree = restructuredtext.parse(app, text)
2024-08-11 08:58:56 -05:00
assert_node(
doctree,
(
addnodes.index,
[desc, ([desc_signature, desc_name, '.. toctree::'], [desc_content, ()])],
),
)
assert_node(
doctree[0],
entries=[('single', 'toctree (directive)', 'directive-toctree', '', None)],
)
assert_node(
doctree[1],
addnodes.desc,
desctype='directive',
domain='rst',
objtype='directive',
no_index=False,
)
2019-04-12 06:58:10 -05:00
@pytest.mark.sphinx('html', testroot='root')
2019-04-12 06:58:10 -05:00
def test_rst_directive_with_argument(app):
2024-08-11 08:58:56 -05:00
text = '.. rst:directive:: .. toctree:: foo bar baz'
2019-04-12 06:58:10 -05:00
doctree = restructuredtext.parse(app, text)
2024-08-11 08:58:56 -05:00
assert_node(
doctree,
(
addnodes.index,
[
desc,
(
[
desc_signature,
([desc_name, '.. toctree::'], [desc_addname, ' foo bar baz']),
],
[desc_content, ()],
),
],
),
)
assert_node(
doctree[0],
entries=[('single', 'toctree (directive)', 'directive-toctree', '', None)],
)
assert_node(
doctree[1],
addnodes.desc,
desctype='directive',
domain='rst',
objtype='directive',
no_index=False,
)
2019-04-12 06:58:10 -05:00
@pytest.mark.sphinx('html', testroot='root')
2019-04-12 21:00:16 -05:00
def test_rst_directive_option(app):
2024-08-11 08:58:56 -05:00
text = '.. rst:directive:option:: foo'
2019-04-12 21:00:16 -05:00
doctree = restructuredtext.parse(app, text)
2024-08-11 08:58:56 -05:00
assert_node(
doctree,
(
addnodes.index,
[desc, ([desc_signature, desc_name, ':foo:'], [desc_content, ()])],
),
)
assert_node(
doctree[0],
entries=[
('single', ':foo: (directive option)', 'directive-option-foo', '', 'F')
],
)
assert_node(
doctree[1],
addnodes.desc,
desctype='directive:option',
domain='rst',
objtype='directive:option',
no_index=False,
)
2019-04-12 21:00:16 -05:00
@pytest.mark.sphinx('html', testroot='root')
2019-04-12 21:00:16 -05:00
def test_rst_directive_option_with_argument(app):
2024-08-11 08:58:56 -05:00
text = '.. rst:directive:option:: foo: bar baz'
2019-04-12 21:00:16 -05:00
doctree = restructuredtext.parse(app, text)
2024-08-11 08:58:56 -05:00
assert_node(
doctree,
(
addnodes.index,
[
desc,
(
[
desc_signature,
([desc_name, ':foo:'], [desc_annotation, ' bar baz']),
],
[desc_content, ()],
),
],
),
)
assert_node(
doctree[0],
entries=[
('single', ':foo: (directive option)', 'directive-option-foo', '', 'F')
],
)
assert_node(
doctree[1],
addnodes.desc,
desctype='directive:option',
domain='rst',
objtype='directive:option',
no_index=False,
)
2019-04-12 21:00:16 -05:00
@pytest.mark.sphinx('html', testroot='root')
def test_rst_directive_option_type(app):
2024-08-11 08:58:56 -05:00
text = '.. rst:directive:option:: foo\n :type: directives.flags\n'
doctree = restructuredtext.parse(app, text)
2024-08-11 08:58:56 -05:00
assert_node(
doctree,
(
addnodes.index,
[
desc,
(
[
desc_signature,
(
[desc_name, ':foo:'],
[desc_annotation, ' (directives.flags)'],
),
],
[desc_content, ()],
),
],
),
)
assert_node(
doctree[0],
entries=[
('single', ':foo: (directive option)', 'directive-option-foo', '', 'F')
],
)
assert_node(
doctree[1],
addnodes.desc,
desctype='directive:option',
domain='rst',
objtype='directive:option',
no_index=False,
)
@pytest.mark.sphinx('html', testroot='root')
2019-04-12 21:00:16 -05:00
def test_rst_directive_and_directive_option(app):
2024-08-11 08:58:56 -05:00
text = '.. rst:directive:: foo\n\n .. rst:directive:option:: bar\n'
2019-04-12 21:00:16 -05:00
doctree = restructuredtext.parse(app, text)
2024-08-11 08:58:56 -05:00
assert_node(
doctree,
(
addnodes.index,
[
desc,
(
[desc_signature, desc_name, '.. foo::'],
[desc_content, (addnodes.index, desc)],
),
],
),
)
assert_node(
doctree[1][1][0],
entries=[
(
'pair',
'foo (directive); :bar: (directive option)',
'directive-option-foo-bar',
'',
'B',
)
],
)
assert_node(
doctree[1][1][1], ([desc_signature, desc_name, ':bar:'], [desc_content, ()])
)
assert_node(
doctree[1][1][1],
addnodes.desc,
desctype='directive:option',
domain='rst',
objtype='directive:option',
no_index=False,
)
2019-04-12 21:00:16 -05:00
@pytest.mark.sphinx('html', testroot='root')
2019-04-12 06:58:10 -05:00
def test_rst_role(app):
2024-08-11 08:58:56 -05:00
text = '.. rst:role:: ref'
2019-04-12 06:58:10 -05:00
doctree = restructuredtext.parse(app, text)
2024-08-11 08:58:56 -05:00
assert_node(
doctree,
(
addnodes.index,
[desc, ([desc_signature, desc_name, ':ref:'], [desc_content, ()])],
),
)
assert_node(doctree[0], entries=[('single', 'ref (role)', 'role-ref', '', None)])
assert_node(
doctree[1],
addnodes.desc,
desctype='role',
domain='rst',
objtype='role',
no_index=False,
)