sphinx/tests/test_extensions/test_ext_autodoc_events.py

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

136 lines
3.6 KiB
Python
Raw Normal View History

"""Test the autodoc extension. This tests mainly for autodoc events"""
import pytest
from sphinx.ext.autodoc import between, cut_lines
from tests.test_extensions.autodoc_util import do_autodoc
@pytest.mark.sphinx('html', testroot='ext-autodoc')
def test_process_docstring(app):
def on_process_docstring(app, what, name, obj, options, lines):
lines.clear()
lines.append('my docstring')
app.connect('autodoc-process-docstring', on_process_docstring)
actual = do_autodoc(app, 'function', 'target.process_docstring.func')
assert list(actual) == [
'',
'.. py:function:: func()',
' :module: target.process_docstring',
'',
' my docstring',
'',
]
@pytest.mark.sphinx('html', testroot='ext-autodoc')
def test_process_docstring_for_nondatadescriptor(app):
def on_process_docstring(app, what, name, obj, options, lines):
raise
app.connect('autodoc-process-docstring', on_process_docstring)
actual = do_autodoc(app, 'attribute', 'target.AttCls.a1')
assert list(actual) == [
'',
'.. py:attribute:: AttCls.a1',
' :module: target',
' :value: hello world',
'',
]
@pytest.mark.sphinx('html', testroot='ext-autodoc')
def test_cut_lines(app):
2024-08-11 08:58:56 -05:00
app.connect('autodoc-process-docstring', cut_lines(2, 2, ['function']))
actual = do_autodoc(app, 'function', 'target.process_docstring.func')
assert list(actual) == [
'',
'.. py:function:: func()',
' :module: target.process_docstring',
'',
' second line',
'',
]
def test_cut_lines_no_objtype():
docstring_lines = [
'first line',
'---',
'second line',
'---',
'third line ',
'',
]
process = cut_lines(2)
process(None, 'function', 'func', None, {}, docstring_lines) # type: ignore[arg-type]
assert docstring_lines == [
'second line',
'---',
'third line ',
'',
]
@pytest.mark.sphinx('html', testroot='ext-autodoc')
def test_between(app):
2024-08-11 08:58:56 -05:00
app.connect('autodoc-process-docstring', between('---', ['function']))
actual = do_autodoc(app, 'function', 'target.process_docstring.func')
assert list(actual) == [
'',
'.. py:function:: func()',
' :module: target.process_docstring',
'',
' second line',
'',
]
@pytest.mark.sphinx('html', testroot='ext-autodoc')
def test_between_exclude(app):
2024-08-11 08:58:56 -05:00
app.connect('autodoc-process-docstring', between('---', ['function'], exclude=True))
actual = do_autodoc(app, 'function', 'target.process_docstring.func')
assert list(actual) == [
'',
'.. py:function:: func()',
' :module: target.process_docstring',
'',
' first line',
' third line',
'',
]
@pytest.mark.sphinx('html', testroot='ext-autodoc')
def test_skip_module_member(app):
def autodoc_skip_member(app, what, name, obj, skip, options):
2024-08-11 08:58:56 -05:00
if name == 'Class':
return True # Skip "Class" class in __all__
2024-08-11 08:58:56 -05:00
elif name == 'raises':
return False # Show "raises()" function (not in __all__)
2023-02-17 19:53:14 -06:00
return None
app.connect('autodoc-skip-member', autodoc_skip_member)
2024-08-11 08:58:56 -05:00
options = {'members': None}
actual = do_autodoc(app, 'module', 'target', options)
assert list(actual) == [
'',
'.. py:module:: target',
'',
'',
'.. py:function:: raises(exc, func, *args, **kwds)',
' :module: target',
'',
' Raise AssertionError if ``func(*args, **kwds)`` does not raise *exc*.',
'',
]