| 1 |
"""Test object description directives.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from typing import TYPE_CHECKING |
| 6 |
|
| 7 |
import docutils.utils |
| 8 |
import pytest |
| 9 |
from docutils import nodes |
| 10 |
|
| 11 |
from sphinx import addnodes |
| 12 |
from sphinx.testing import restructuredtext |
| 13 |
from sphinx.util.docutils import _parse_str_to_doctree |
| 14 |
|
| 15 |
from tests.utils import extract_node |
| 16 |
|
| 17 |
if TYPE_CHECKING: |
| 18 |
from sphinx.application import Sphinx |
| 19 |
from sphinx.environment import BuildEnvironment |
| 20 |
from sphinx.testing.util import SphinxTestApp |
| 21 |
|
| 22 |
|
| 23 |
def _doctree_for_test( |
| 24 |
app: Sphinx, env: BuildEnvironment, docname: str |
| 25 |
) -> nodes.document: |
| 26 |
config = app.config |
| 27 |
registry = app.registry |
| 28 |
|
| 29 |
filename = env.doc2path(docname) |
| 30 |
content = filename.read_text(encoding='utf-8') |
| 31 |
|
| 32 |
env.prepare_settings(docname) |
| 33 |
parser = registry.create_source_parser('restructuredtext', config=config, env=env) |
| 34 |
return _parse_str_to_doctree( |
| 35 |
content, |
| 36 |
filename=filename, |
| 37 |
default_settings={'env': env}, |
| 38 |
env=env, |
| 39 |
parser=parser, |
| 40 |
transforms=registry.get_transforms(), |
| 41 |
) |
| 42 |
|
| 43 |
|
| 44 |
@pytest.mark.sphinx('text', testroot='object-description-sections') |
| 45 |
def test_object_description_sections(app: SphinxTestApp) -> None: |
| 46 |
doctree = _doctree_for_test(app, app.env, 'index') |
| 47 |
# <document> |
| 48 |
# <index> |
| 49 |
# <desc> |
| 50 |
# <desc_signature> |
| 51 |
# <desc_name> |
| 52 |
# func |
| 53 |
# <desc_parameterlist> |
| 54 |
# <desc_content> |
| 55 |
# <section> |
| 56 |
# <title> |
| 57 |
# Overview |
| 58 |
# <paragraph> |
| 59 |
# Lorem ipsum dolar sit amet |
| 60 |
|
| 61 |
assert isinstance(doctree[0], addnodes.index) |
| 62 |
assert isinstance(doctree[1], addnodes.desc) |
| 63 |
assert isinstance(extract_node(doctree, 1, 0), addnodes.desc_signature) |
| 64 |
assert isinstance(extract_node(doctree, 1, 1), addnodes.desc_content) |
| 65 |
assert isinstance(extract_node(doctree, 1, 1, 0), nodes.section) |
| 66 |
assert isinstance(extract_node(doctree, 1, 1, 0, 0), nodes.title) |
| 67 |
assert extract_node(doctree, 1, 1, 0, 0, 0) == 'Overview' |
| 68 |
assert isinstance(extract_node(doctree, 1, 1, 0, 1), nodes.paragraph) |
| 69 |
assert extract_node(doctree, 1, 1, 0, 1, 0) == 'Lorem ipsum dolar sit amet' |
| 70 |
|
| 71 |
|
| 72 |
@pytest.mark.sphinx('html', testroot='_blank') |
| 73 |
def test_object_description_content_line_number(app: SphinxTestApp) -> None: |
| 74 |
text = '.. py:function:: foo(bar)\n\n Some link here: :ref:`abc`\n' |
| 75 |
doc = restructuredtext.parse(app, text) |
| 76 |
xrefs = list(doc.findall(condition=addnodes.pending_xref)) |
| 77 |
assert len(xrefs) == 1 |
| 78 |
source, line = docutils.utils.get_source_line(xrefs[0]) |
| 79 |
assert 'index.rst' in source |
| 80 |
assert line == 3 |