|
27 | 27 |
|
28 | 28 | # NEVER import these objects from sphinx.ext.autodoc directly |
29 | 29 | from sphinx.ext.autodoc.directive import DocumenterBridge |
| 30 | +from sphinx.ext.autodoc.importer import _parse_name |
30 | 31 |
|
31 | 32 | from tests.test_extensions.autodoc_util import do_autodoc |
32 | 33 |
|
@@ -80,58 +81,54 @@ def make_directive_bridge(env: BuildEnvironment) -> DocumenterBridge: |
80 | 81 |
|
81 | 82 | @pytest.mark.sphinx('html', testroot='root') |
82 | 83 | def test_parse_name(app): |
83 | | - def verify(objtype, name, result): |
84 | | - inst = app.registry.documenters[objtype](directive, name) |
85 | | - assert inst.parse_name() |
86 | | - assert (inst.modname, inst.objpath, inst.args, inst.retann) == result |
| 84 | + env = app.env |
| 85 | + current_document = env.current_document |
87 | 86 |
|
88 | | - directive = make_directive_bridge(app.env) |
| 87 | + def parse(objtype, name): |
| 88 | + parsed = _parse_name( |
| 89 | + name=name, objtype=objtype, current_document=current_document, env=env |
| 90 | + ) |
| 91 | + if parsed is None: |
| 92 | + return None |
| 93 | + module_name, parts, args, retann = parsed |
| 94 | + return module_name, list(parts), args, retann |
89 | 95 |
|
90 | 96 | # for modules |
91 | | - verify('module', 'test_ext_autodoc', ('test_ext_autodoc', [], None, None)) |
92 | | - verify('module', 'test.test_ext_autodoc', ('test.test_ext_autodoc', [], None, None)) |
93 | | - verify('module', 'test(arg)', ('test', [], 'arg', None)) |
| 97 | + parsed = parse('module', 'test_ext_autodoc') |
| 98 | + assert parsed == ('test_ext_autodoc', [], None, None) |
| 99 | + parsed = parse('module', 'test.test_ext_autodoc') |
| 100 | + assert parsed == ('test.test_ext_autodoc', [], None, None) |
| 101 | + parsed = parse('module', 'test(arg)') |
| 102 | + assert parsed is None |
94 | 103 | assert 'signature arguments' in app.warning.getvalue() |
95 | 104 |
|
96 | 105 | # for functions/classes |
97 | | - verify( |
98 | | - 'function', |
99 | | - 'test_ext_autodoc.raises', |
100 | | - ('test_ext_autodoc', ['raises'], None, None), |
101 | | - ) |
102 | | - verify( |
103 | | - 'function', |
104 | | - 'test_ext_autodoc.raises(exc) -> None', |
105 | | - ('test_ext_autodoc', ['raises'], 'exc', 'None'), |
106 | | - ) |
107 | | - directive.env.current_document.autodoc_module = 'test_ext_autodoc' |
108 | | - verify('function', 'raises', ('test_ext_autodoc', ['raises'], None, None)) |
109 | | - directive.env.current_document.autodoc_module = '' |
110 | | - |
111 | | - directive.env.ref_context['py:module'] = 'test_ext_autodoc' |
112 | | - verify('function', 'raises', ('test_ext_autodoc', ['raises'], None, None)) |
113 | | - verify('class', 'Base', ('test_ext_autodoc', ['Base'], None, None)) |
| 106 | + parsed = parse('function', 'test_ext_autodoc.raises') |
| 107 | + assert parsed == ('test_ext_autodoc', ['raises'], None, None) |
| 108 | + parsed = parse('function', 'test_ext_autodoc.raises(exc) -> None') |
| 109 | + assert parsed == ('test_ext_autodoc', ['raises'], 'exc', 'None') |
| 110 | + current_document.autodoc_module = 'test_ext_autodoc' |
| 111 | + parsed = parse('function', 'raises') |
| 112 | + assert parsed == ('test_ext_autodoc', ['raises'], None, None) |
| 113 | + current_document.autodoc_module = '' |
| 114 | + |
| 115 | + env.ref_context['py:module'] = 'test_ext_autodoc' |
| 116 | + parsed = parse('function', 'raises') |
| 117 | + assert parsed == ('test_ext_autodoc', ['raises'], None, None) |
| 118 | + parsed = parse('class', 'Base') |
| 119 | + assert parsed == ('test_ext_autodoc', ['Base'], None, None) |
114 | 120 |
|
115 | 121 | # for members |
116 | | - directive.env.ref_context['py:module'] = 'sphinx.testing.util' |
117 | | - verify( |
118 | | - 'method', |
119 | | - 'SphinxTestApp.cleanup', |
120 | | - ('sphinx.testing.util', ['SphinxTestApp', 'cleanup'], None, None), |
121 | | - ) |
122 | | - directive.env.ref_context['py:module'] = 'sphinx.testing.util' |
123 | | - directive.env.ref_context['py:class'] = 'Foo' |
124 | | - directive.env.current_document.autodoc_class = 'SphinxTestApp' |
125 | | - verify( |
126 | | - 'method', |
127 | | - 'cleanup', |
128 | | - ('sphinx.testing.util', ['SphinxTestApp', 'cleanup'], None, None), |
129 | | - ) |
130 | | - verify( |
131 | | - 'method', |
132 | | - 'SphinxTestApp.cleanup', |
133 | | - ('sphinx.testing.util', ['SphinxTestApp', 'cleanup'], None, None), |
134 | | - ) |
| 122 | + env.ref_context['py:module'] = 'sphinx.testing.util' |
| 123 | + parsed = parse('method', 'SphinxTestApp.cleanup') |
| 124 | + assert parsed == ('sphinx.testing.util', ['SphinxTestApp', 'cleanup'], None, None) |
| 125 | + env.ref_context['py:module'] = 'sphinx.testing.util' |
| 126 | + env.ref_context['py:class'] = 'Foo' |
| 127 | + current_document.autodoc_class = 'SphinxTestApp' |
| 128 | + parsed = parse('method', 'cleanup') |
| 129 | + assert parsed == ('sphinx.testing.util', ['SphinxTestApp', 'cleanup'], None, None) |
| 130 | + parsed = parse('method', 'SphinxTestApp.cleanup') |
| 131 | + assert parsed == ('sphinx.testing.util', ['SphinxTestApp', 'cleanup'], None, None) |
135 | 132 |
|
136 | 133 |
|
137 | 134 | @pytest.mark.sphinx('html', testroot='root') |
|
0 commit comments