2009-04-04 14:05:32 -05:00
|
|
|
"""
|
|
|
|
test_autosummary
|
|
|
|
~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Test the autosummary extension.
|
|
|
|
|
2019-01-02 01:00:30 -06:00
|
|
|
:copyright: Copyright 2007-2019 by the Sphinx team, see AUTHORS.
|
2009-04-04 14:05:32 -05:00
|
|
|
:license: BSD, see LICENSE for details.
|
|
|
|
"""
|
2009-04-13 03:40:56 -05:00
|
|
|
|
2019-02-11 04:22:57 -06:00
|
|
|
import sys
|
2018-12-15 05:03:39 -06:00
|
|
|
from io import StringIO
|
|
|
|
|
2018-02-17 10:51:57 -06:00
|
|
|
import pytest
|
2014-04-29 07:20:56 -05:00
|
|
|
|
2018-02-17 10:51:57 -06:00
|
|
|
from sphinx.ext.autosummary import mangle_signature, import_by_name, extract_summary
|
2017-05-07 02:46:44 -05:00
|
|
|
from sphinx.testing.util import etree_parse
|
2017-03-25 16:16:28 -05:00
|
|
|
|
2014-06-21 03:05:31 -05:00
|
|
|
html_warnfile = StringIO()
|
|
|
|
|
2014-04-11 10:24:53 -05:00
|
|
|
|
2014-09-21 10:17:02 -05:00
|
|
|
default_kw = {
|
|
|
|
'testroot': 'autosummary',
|
|
|
|
'confoverrides': {
|
|
|
|
'extensions': ['sphinx.ext.autosummary'],
|
|
|
|
'autosummary_generate': True,
|
|
|
|
'source_suffix': '.rst'
|
2014-04-11 10:24:53 -05:00
|
|
|
}
|
2014-09-21 10:17:02 -05:00
|
|
|
}
|
2014-04-11 10:24:53 -05:00
|
|
|
|
2009-04-04 14:05:32 -05:00
|
|
|
|
|
|
|
def test_mangle_signature():
|
|
|
|
TEST = """
|
|
|
|
() :: ()
|
|
|
|
(a, b, c, d, e) :: (a, b, c, d, e)
|
|
|
|
(a, b, c=1, d=2, e=3) :: (a, b[, c, d, e])
|
2009-04-13 03:40:56 -05:00
|
|
|
(a, b, aaa=1, bbb=1, ccc=1, eee=1, fff=1, ggg=1, hhh=1, iii=1, jjj=1)\
|
2009-05-31 12:50:29 -05:00
|
|
|
:: (a, b[, aaa, bbb, ccc, ...])
|
2009-04-04 14:05:32 -05:00
|
|
|
(a, b, c=(), d=<foo>) :: (a, b[, c, d])
|
|
|
|
(a, b, c='foobar()', d=123) :: (a, b[, c, d])
|
2011-06-29 14:26:43 -05:00
|
|
|
(a, b[, c]) :: (a, b[, c])
|
|
|
|
(a, b[, cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx]) :: (a, b[, ...)
|
|
|
|
(a, b='c=d, e=f, g=h', c=3) :: (a[, b, c])
|
|
|
|
(a, b='c=d, \\'e=f,\\' g=h', c=3) :: (a[, b, c])
|
|
|
|
(a, b='c=d, ', e='\\\\' g=h, c=3) :: (a[, b, e, c])
|
|
|
|
(a, b={'c=d, ': 3, '\\\\': 3}) :: (a[, b])
|
|
|
|
(a=1, b=2, c=3) :: ([a, b, c])
|
|
|
|
(a=1, b=<SomeClass: a, b, c>, c=3) :: ([a, b, c])
|
2018-02-03 01:53:19 -06:00
|
|
|
(a: int, b: int) -> str :: (a, b)
|
2009-04-04 14:05:32 -05:00
|
|
|
"""
|
|
|
|
|
2014-04-30 09:25:44 -05:00
|
|
|
TEST = [[y.strip() for y in x.split("::")] for x in TEST.split("\n")
|
2009-04-04 14:05:32 -05:00
|
|
|
if '::' in x]
|
|
|
|
for inp, outp in TEST:
|
2018-12-15 08:02:28 -06:00
|
|
|
res = mangle_signature(inp).strip().replace("\u00a0", " ")
|
|
|
|
assert res == outp, ("'%s' -> '%s' != '%s'" % (inp, res, outp))
|
2014-02-03 21:35:04 -06:00
|
|
|
|
|
|
|
|
2018-03-01 07:21:43 -06:00
|
|
|
def test_extract_summary(capsys):
|
2018-02-17 21:09:24 -06:00
|
|
|
from sphinx.util.docutils import new_document
|
|
|
|
from mock import Mock
|
|
|
|
settings = Mock(language_code='',
|
|
|
|
id_prefix='',
|
|
|
|
auto_id_prefix='',
|
|
|
|
pep_reference=False,
|
|
|
|
rfc_reference=False)
|
|
|
|
document = new_document('', settings)
|
|
|
|
|
|
|
|
# normal case
|
2018-02-17 10:51:57 -06:00
|
|
|
doc = ['',
|
|
|
|
'This is a first sentence. And second one.',
|
|
|
|
'',
|
|
|
|
'Second block is here']
|
2018-02-17 21:09:24 -06:00
|
|
|
assert extract_summary(doc, document) == 'This is a first sentence.'
|
|
|
|
|
|
|
|
# inliner case
|
|
|
|
doc = ['This sentence contains *emphasis text having dots.*,',
|
|
|
|
'it does not break sentence.']
|
|
|
|
assert extract_summary(doc, document) == ' '.join(doc)
|
2018-02-17 10:51:57 -06:00
|
|
|
|
2018-03-01 02:37:16 -06:00
|
|
|
# abbreviations
|
|
|
|
doc = ['Blabla, i.e. bla.']
|
|
|
|
assert extract_summary(doc, document) == 'Blabla, i.e.'
|
|
|
|
|
2018-07-08 07:14:01 -05:00
|
|
|
# literal
|
|
|
|
doc = ['blah blah::']
|
|
|
|
assert extract_summary(doc, document) == 'blah blah.'
|
|
|
|
|
2018-07-16 04:44:28 -05:00
|
|
|
# heading
|
|
|
|
doc = ['blah blah',
|
|
|
|
'=========']
|
|
|
|
assert extract_summary(doc, document) == 'blah blah'
|
|
|
|
|
2018-03-01 07:21:43 -06:00
|
|
|
_, err = capsys.readouterr()
|
|
|
|
assert err == ''
|
|
|
|
|
2018-02-17 10:51:57 -06:00
|
|
|
|
2017-01-05 10:14:47 -06:00
|
|
|
@pytest.mark.sphinx('dummy', **default_kw)
|
2017-06-17 20:25:34 -05:00
|
|
|
def test_get_items_summary(make_app, app_params):
|
|
|
|
import sphinx.ext.autosummary
|
|
|
|
import sphinx.ext.autosummary.generate
|
|
|
|
args, kwargs = app_params
|
|
|
|
app = make_app(*args, **kwargs)
|
2018-01-06 09:05:48 -06:00
|
|
|
sphinx.ext.autosummary.generate.setup_documenters(app)
|
2014-02-03 21:35:04 -06:00
|
|
|
# monkey-patch Autosummary.get_items so we can easily get access to it's
|
|
|
|
# results..
|
|
|
|
orig_get_items = sphinx.ext.autosummary.Autosummary.get_items
|
|
|
|
|
|
|
|
autosummary_items = {}
|
|
|
|
|
|
|
|
def new_get_items(self, names, *args, **kwargs):
|
|
|
|
results = orig_get_items(self, names, *args, **kwargs)
|
|
|
|
for name, result in zip(names, results):
|
|
|
|
autosummary_items[name] = result
|
|
|
|
return results
|
|
|
|
|
2014-09-21 10:17:02 -05:00
|
|
|
def handler(app, what, name, obj, options, lines):
|
|
|
|
assert isinstance(lines, list)
|
2018-01-07 20:09:29 -06:00
|
|
|
|
|
|
|
# ensure no docstring is processed twice:
|
|
|
|
assert 'THIS HAS BEEN HANDLED' not in lines
|
|
|
|
lines.append('THIS HAS BEEN HANDLED')
|
2014-09-21 10:17:02 -05:00
|
|
|
app.connect('autodoc-process-docstring', handler)
|
|
|
|
|
2014-02-03 21:35:04 -06:00
|
|
|
sphinx.ext.autosummary.Autosummary.get_items = new_get_items
|
|
|
|
try:
|
2014-04-11 10:24:53 -05:00
|
|
|
app.builder.build_all()
|
2014-02-03 21:35:04 -06:00
|
|
|
finally:
|
|
|
|
sphinx.ext.autosummary.Autosummary.get_items = orig_get_items
|
|
|
|
|
2017-06-17 20:25:34 -05:00
|
|
|
html_warnings = app._warning.getvalue()
|
2014-06-21 03:05:31 -05:00
|
|
|
assert html_warnings == ''
|
|
|
|
|
2014-02-03 21:35:04 -06:00
|
|
|
expected_values = {
|
|
|
|
'withSentence': 'I have a sentence which spans multiple lines.',
|
2018-02-17 21:09:24 -06:00
|
|
|
'noSentence': "this doesn't start with a capital.",
|
2014-02-03 21:35:04 -06:00
|
|
|
'emptyLine': "This is the real summary",
|
2014-04-11 09:38:37 -05:00
|
|
|
'module_attr': 'This is a module attribute',
|
|
|
|
'C.class_attr': 'This is a class attribute',
|
|
|
|
'C.prop_attr1': 'This is a function docstring',
|
|
|
|
'C.prop_attr2': 'This is a attribute docstring',
|
2014-06-21 03:05:31 -05:00
|
|
|
'C.C2': 'This is a nested inner class docstring',
|
2014-02-03 21:35:04 -06:00
|
|
|
}
|
2018-09-11 07:50:55 -05:00
|
|
|
for key, expected in expected_values.items():
|
2014-02-03 21:35:04 -06:00
|
|
|
assert autosummary_items[key][2] == expected, 'Summary for %s was %r -'\
|
|
|
|
' expected %r' % (key, autosummary_items[key], expected)
|
2017-01-21 00:50:41 -06:00
|
|
|
|
|
|
|
# check an item in detail
|
|
|
|
assert 'func' in autosummary_items
|
|
|
|
func_attrs = ('func',
|
|
|
|
'(arg_, *args, **kwargs)',
|
|
|
|
'Test function take an argument ended with underscore.',
|
|
|
|
'dummy_module.func')
|
|
|
|
assert autosummary_items['func'] == func_attrs
|
2017-03-25 16:16:28 -05:00
|
|
|
|
2017-05-09 07:57:36 -05:00
|
|
|
|
2017-03-25 16:16:28 -05:00
|
|
|
def str_content(elem):
|
|
|
|
if elem.text is not None:
|
|
|
|
return elem.text
|
|
|
|
else:
|
|
|
|
return ''.join(str_content(e) for e in elem)
|
|
|
|
|
2017-05-09 07:57:36 -05:00
|
|
|
|
2017-03-25 16:16:28 -05:00
|
|
|
@pytest.mark.sphinx('xml', **default_kw)
|
|
|
|
def test_escaping(app, status, warning):
|
|
|
|
app.builder.build_all()
|
|
|
|
|
|
|
|
outdir = app.builder.outdir
|
|
|
|
|
|
|
|
docpage = outdir / 'underscore_module_.xml'
|
|
|
|
assert docpage.exists()
|
|
|
|
|
|
|
|
title = etree_parse(docpage).find('section/title')
|
|
|
|
|
|
|
|
assert str_content(title) == 'underscore_module_'
|
2017-07-02 04:26:41 -05:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.sphinx('dummy', testroot='ext-autosummary')
|
|
|
|
def test_autosummary_generate(app, status, warning):
|
|
|
|
app.builder.build_all()
|
|
|
|
|
2017-07-02 04:41:03 -05:00
|
|
|
module = (app.srcdir / 'generated' / 'autosummary_dummy_module.rst').text()
|
2017-07-02 04:26:41 -05:00
|
|
|
assert (' .. autosummary::\n'
|
|
|
|
' \n'
|
|
|
|
' Foo\n'
|
|
|
|
' \n' in module)
|
|
|
|
|
2017-07-02 04:41:03 -05:00
|
|
|
Foo = (app.srcdir / 'generated' / 'autosummary_dummy_module.Foo.rst').text()
|
2017-07-02 04:26:41 -05:00
|
|
|
assert '.. automethod:: __init__' in Foo
|
|
|
|
assert (' .. autosummary::\n'
|
|
|
|
' \n'
|
|
|
|
' ~Foo.__init__\n'
|
|
|
|
' ~Foo.bar\n'
|
|
|
|
' \n' in Foo)
|
2017-11-17 02:45:01 -06:00
|
|
|
assert (' .. autosummary::\n'
|
|
|
|
' \n'
|
|
|
|
' ~Foo.baz\n'
|
|
|
|
' \n' in Foo)
|
2017-11-11 07:26:12 -06:00
|
|
|
|
|
|
|
|
2018-03-28 03:03:18 -05:00
|
|
|
@pytest.mark.sphinx('latex', **default_kw)
|
|
|
|
def test_autosummary_latex_table_colspec(app, status, warning):
|
|
|
|
app.builder.build_all()
|
2018-09-03 07:18:58 -05:00
|
|
|
result = (app.outdir / 'python.tex').text(encoding='utf8')
|
2018-03-28 03:03:18 -05:00
|
|
|
print(status.getvalue())
|
|
|
|
print(warning.getvalue())
|
2019-02-04 10:02:20 -06:00
|
|
|
assert r'\begin{longtable}[c]{\X{1}{2}\X{1}{2}}' in result
|
2018-03-28 03:03:18 -05:00
|
|
|
assert r'p{0.5\linewidth}' not in result
|
|
|
|
|
|
|
|
|
2017-11-11 07:26:12 -06:00
|
|
|
def test_import_by_name():
|
|
|
|
import sphinx
|
|
|
|
import sphinx.ext.autosummary
|
|
|
|
|
|
|
|
prefixed_name, obj, parent, modname = import_by_name('sphinx')
|
|
|
|
assert prefixed_name == 'sphinx'
|
|
|
|
assert obj is sphinx
|
|
|
|
assert parent is None
|
|
|
|
assert modname == 'sphinx'
|
|
|
|
|
|
|
|
prefixed_name, obj, parent, modname = import_by_name('sphinx.ext.autosummary.__name__')
|
|
|
|
assert prefixed_name == 'sphinx.ext.autosummary.__name__'
|
|
|
|
assert obj is sphinx.ext.autosummary.__name__
|
|
|
|
assert parent is sphinx.ext.autosummary
|
|
|
|
assert modname == 'sphinx.ext.autosummary'
|
|
|
|
|
2017-12-23 06:20:32 -06:00
|
|
|
prefixed_name, obj, parent, modname = \
|
|
|
|
import_by_name('sphinx.ext.autosummary.Autosummary.get_items')
|
2017-11-11 07:26:12 -06:00
|
|
|
assert prefixed_name == 'sphinx.ext.autosummary.Autosummary.get_items'
|
|
|
|
assert obj == sphinx.ext.autosummary.Autosummary.get_items
|
|
|
|
assert parent is sphinx.ext.autosummary.Autosummary
|
|
|
|
assert modname == 'sphinx.ext.autosummary'
|
2019-02-11 04:22:57 -06:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.sphinx('dummy', testroot='ext-autosummary-mock_imports')
|
|
|
|
def test_autosummary_mock_imports(app, status, warning):
|
|
|
|
try:
|
|
|
|
app.build()
|
|
|
|
assert warning.getvalue() == ''
|
|
|
|
|
|
|
|
# generated/foo is generated successfully
|
|
|
|
assert app.env.get_doctree('generated/foo')
|
|
|
|
finally:
|
|
|
|
sys.modules.pop('foo', None) # unload foo module
|