diff options
Diffstat (limited to 'tests/test_domains/test_domain_py_pyobject.py')
-rw-r--r-- | tests/test_domains/test_domain_py_pyobject.py | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/tests/test_domains/test_domain_py_pyobject.py b/tests/test_domains/test_domain_py_pyobject.py index 04f9341..adc0453 100644 --- a/tests/test_domains/test_domain_py_pyobject.py +++ b/tests/test_domains/test_domain_py_pyobject.py @@ -2,6 +2,7 @@ from __future__ import annotations +import pytest from docutils import nodes from sphinx import addnodes @@ -362,6 +363,76 @@ def test_pyproperty(app): assert domain.objects['Class.prop2'] == ('index', 'Class.prop2', 'property', False) +def test_py_type_alias(app): + text = (".. py:module:: example\n" + ".. py:type:: Alias1\n" + " :canonical: list[str | int]\n" + "\n" + ".. py:class:: Class\n" + "\n" + " .. py:type:: Alias2\n" + " :canonical: int\n") + domain = app.env.get_domain('py') + doctree = restructuredtext.parse(app, text) + assert_node(doctree, (addnodes.index, + addnodes.index, + nodes.target, + [desc, ([desc_signature, ([desc_annotation, ('type', desc_sig_space)], + [desc_addname, 'example.'], + [desc_name, 'Alias1'], + [desc_annotation, (desc_sig_space, + [desc_sig_punctuation, '='], + desc_sig_space, + [pending_xref, 'list'], + [desc_sig_punctuation, '['], + [pending_xref, 'str'], + desc_sig_space, + [desc_sig_punctuation, '|'], + desc_sig_space, + [pending_xref, 'int'], + [desc_sig_punctuation, ']'], + )])], + [desc_content, ()])], + addnodes.index, + [desc, ([desc_signature, ([desc_annotation, ('class', desc_sig_space)], + [desc_addname, 'example.'], + [desc_name, 'Class'])], + [desc_content, (addnodes.index, + desc)])])) + assert_node(doctree[5][1][0], addnodes.index, + entries=[('single', 'Alias2 (type alias in example.Class)', 'example.Class.Alias2', '', None)]) + assert_node(doctree[5][1][1], ([desc_signature, ([desc_annotation, ('type', desc_sig_space)], + [desc_name, 'Alias2'], + [desc_annotation, (desc_sig_space, + [desc_sig_punctuation, '='], + desc_sig_space, + [pending_xref, 'int'])])], + [desc_content, ()])) + assert 'example.Alias1' in domain.objects + assert domain.objects['example.Alias1'] == ('index', 'example.Alias1', 'type', False) + assert 'example.Class.Alias2' in domain.objects + assert domain.objects['example.Class.Alias2'] == ('index', 'example.Class.Alias2', 'type', False) + + +@pytest.mark.sphinx('html', testroot='domain-py', freshenv=True) +def test_domain_py_type_alias(app, status, warning): + app.build(force_all=True) + + content = (app.outdir / 'type_alias.html').read_text(encoding='utf8') + assert ('<em class="property"><span class="pre">type</span><span class="w"> </span></em>' + '<span class="sig-prename descclassname"><span class="pre">module_one.</span></span>' + '<span class="sig-name descname"><span class="pre">MyAlias</span></span>' + '<em class="property"><span class="w"> </span><span class="p"><span class="pre">=</span></span>' + '<span class="w"> </span><span class="pre">list</span>' + '<span class="p"><span class="pre">[</span></span>' + '<span class="pre">int</span><span class="w"> </span>' + '<span class="p"><span class="pre">|</span></span><span class="w"> </span>' + '<a class="reference internal" href="#module_two.SomeClass" title="module_two.SomeClass">' + '<span class="pre">module_two.SomeClass</span></a>' + '<span class="p"><span class="pre">]</span></span></em>' in content) + assert warning.getvalue() == '' + + def test_pydecorator_signature(app): text = ".. py:decorator:: deco" domain = app.env.get_domain('py') |