| 1 |
"""Tests the Python Domain""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from typing import TYPE_CHECKING |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from sphinx import addnodes |
| 10 |
from sphinx.addnodes import ( |
| 11 |
desc, |
| 12 |
desc_addname, |
| 13 |
desc_annotation, |
| 14 |
desc_content, |
| 15 |
desc_name, |
| 16 |
desc_sig_keyword, |
| 17 |
desc_sig_space, |
| 18 |
desc_signature, |
| 19 |
) |
| 20 |
from sphinx.testing import restructuredtext |
| 21 |
from sphinx.testing.util import assert_node |
| 22 |
|
| 23 |
if TYPE_CHECKING: |
| 24 |
from sphinx.testing.util import SphinxTestApp |
| 25 |
|
| 26 |
|
| 27 |
@pytest.mark.sphinx( |
| 28 |
'html', |
| 29 |
testroot='domain-py', |
| 30 |
freshenv=True, |
| 31 |
) |
| 32 |
def test_domain_py_canonical(app: SphinxTestApp) -> None: |
| 33 |
app.build(force_all=True) |
| 34 |
|
| 35 |
content = (app.outdir / 'canonical.html').read_text(encoding='utf8') |
| 36 |
assert ( |
| 37 |
'<a class="reference internal" href="#canonical.Foo" title="canonical.Foo">' |
| 38 |
'<code class="xref py py-class docutils literal notranslate">' |
| 39 |
'<span class="pre">Foo</span></code></a>' |
| 40 |
) in content |
| 41 |
assert app.warning.getvalue() == '' |
| 42 |
|
| 43 |
|
| 44 |
@pytest.mark.sphinx('html', testroot='_blank') |
| 45 |
def test_canonical(app: SphinxTestApp) -> None: |
| 46 |
text = '.. py:class:: io.StringIO\n :canonical: _io.StringIO' |
| 47 |
domain = app.env.domains.python_domain |
| 48 |
doctree = restructuredtext.parse(app, text) |
| 49 |
assert_node( |
| 50 |
doctree, |
| 51 |
( |
| 52 |
addnodes.index, |
| 53 |
[ |
| 54 |
desc, |
| 55 |
( |
| 56 |
[ |
| 57 |
desc_signature, |
| 58 |
( |
| 59 |
[ |
| 60 |
desc_annotation, |
| 61 |
([desc_sig_keyword, 'class'], desc_sig_space), |
| 62 |
], |
| 63 |
[desc_addname, 'io.'], |
| 64 |
[desc_name, 'StringIO'], |
| 65 |
), |
| 66 |
], |
| 67 |
desc_content, |
| 68 |
), |
| 69 |
], |
| 70 |
), |
| 71 |
) |
| 72 |
assert 'io.StringIO' in domain.objects |
| 73 |
assert domain.objects['io.StringIO'] == ('index', 'io.StringIO', 'class', False) |
| 74 |
assert domain.objects['_io.StringIO'] == ('index', 'io.StringIO', 'class', True) |
| 75 |
|
| 76 |
|
| 77 |
@pytest.mark.sphinx('html', testroot='_blank') |
| 78 |
def test_canonical_definition_overrides(app: SphinxTestApp) -> None: |
| 79 |
text = ( |
| 80 |
'.. py:class:: io.StringIO\n' |
| 81 |
' :canonical: _io.StringIO\n' |
| 82 |
'.. py:class:: _io.StringIO\n' |
| 83 |
) |
| 84 |
restructuredtext.parse(app, text) |
| 85 |
assert app.warning.getvalue() == '' |
| 86 |
|
| 87 |
domain = app.env.domains.python_domain |
| 88 |
assert domain.objects['_io.StringIO'] == ('index', 'id0', 'class', False) |
| 89 |
|
| 90 |
|
| 91 |
@pytest.mark.sphinx('html', testroot='_blank') |
| 92 |
def test_canonical_definition_skip(app: SphinxTestApp) -> None: |
| 93 |
text = ( |
| 94 |
'.. py:class:: _io.StringIO\n' |
| 95 |
'.. py:class:: io.StringIO\n' |
| 96 |
' :canonical: _io.StringIO\n' |
| 97 |
) |
| 98 |
|
| 99 |
restructuredtext.parse(app, text) |
| 100 |
assert app.warning.getvalue() == '' |
| 101 |
|
| 102 |
domain = app.env.domains.python_domain |
| 103 |
assert domain.objects['_io.StringIO'] == ('index', 'io.StringIO', 'class', False) |
| 104 |
|
| 105 |
|
| 106 |
@pytest.mark.sphinx('html', testroot='_blank') |
| 107 |
def test_canonical_duplicated(app: SphinxTestApp) -> None: |
| 108 |
text = ( |
| 109 |
'.. py:class:: mypackage.StringIO\n' |
| 110 |
' :canonical: _io.StringIO\n' |
| 111 |
'.. py:class:: io.StringIO\n' |
| 112 |
' :canonical: _io.StringIO\n' |
| 113 |
) |
| 114 |
|
| 115 |
restructuredtext.parse(app, text) |
| 116 |
assert app.warning.getvalue() != '' |