2016-02-14 00:06:28 -06:00
|
|
|
"""
|
|
|
|
test_ext_inheritance_diagram
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Test sphinx.ext.inheritance_diagram extension.
|
|
|
|
|
2019-01-02 01:00:30 -06:00
|
|
|
:copyright: Copyright 2007-2019 by the Sphinx team, see AUTHORS.
|
2016-02-14 00:06:28 -06:00
|
|
|
:license: BSD, see LICENSE for details.
|
|
|
|
"""
|
|
|
|
|
2016-11-22 22:38:20 -06:00
|
|
|
import re
|
2016-11-21 09:00:50 -06:00
|
|
|
import sys
|
2017-07-26 14:07:25 -05:00
|
|
|
|
2017-01-03 07:24:00 -06:00
|
|
|
import pytest
|
2016-02-14 00:06:28 -06:00
|
|
|
|
2017-07-26 14:07:25 -05:00
|
|
|
from sphinx.ext.inheritance_diagram import InheritanceException, import_classes
|
|
|
|
|
2016-02-14 00:06:28 -06:00
|
|
|
|
2017-01-05 10:14:47 -06:00
|
|
|
@pytest.mark.sphinx('html', testroot='ext-inheritance_diagram')
|
2017-01-03 07:24:00 -06:00
|
|
|
@pytest.mark.usefixtures('if_graphviz_found')
|
2018-04-20 23:59:27 -05:00
|
|
|
def test_inheritance_diagram_png_html(app, status, warning):
|
2016-02-14 00:06:28 -06:00
|
|
|
app.builder.build_all()
|
2016-03-10 22:20:05 -06:00
|
|
|
|
|
|
|
content = (app.outdir / 'index.html').text()
|
|
|
|
|
2019-02-04 10:02:20 -06:00
|
|
|
pattern = ('<div class="figure align-center" id="id1">\n'
|
2018-04-20 01:10:30 -05:00
|
|
|
'<div class="graphviz">'
|
2017-02-12 11:02:51 -06:00
|
|
|
'<img src="_images/inheritance-\\w+.png" alt="Inheritance diagram of test.Foo" '
|
2018-08-04 04:38:39 -05:00
|
|
|
'class="inheritance graphviz" /></div>\n<p class="caption">'
|
|
|
|
'<span class="caption-text">Test Foo!</span><a class="headerlink" href="#id1" '
|
2016-03-10 22:20:05 -06:00
|
|
|
'title="Permalink to this image">\xb6</a></p>')
|
|
|
|
assert re.search(pattern, content, re.M)
|
|
|
|
|
|
|
|
|
2018-04-20 23:59:27 -05:00
|
|
|
@pytest.mark.sphinx('html', testroot='ext-inheritance_diagram',
|
|
|
|
confoverrides={'graphviz_output_format': 'svg'})
|
|
|
|
@pytest.mark.usefixtures('if_graphviz_found')
|
|
|
|
def test_inheritance_diagram_svg_html(app, status, warning):
|
|
|
|
app.builder.build_all()
|
|
|
|
|
|
|
|
content = (app.outdir / 'index.html').text()
|
|
|
|
|
2019-02-04 10:02:20 -06:00
|
|
|
pattern = ('<div class="figure align-center" id="id1">\n'
|
2018-04-20 23:59:27 -05:00
|
|
|
'<div class="graphviz">'
|
|
|
|
'<object data="_images/inheritance-\\w+.svg" '
|
2018-08-04 04:38:39 -05:00
|
|
|
'type="image/svg\\+xml" class="inheritance graphviz">\n'
|
2018-04-20 23:59:27 -05:00
|
|
|
'<p class=\"warning\">Inheritance diagram of test.Foo</p>'
|
|
|
|
'</object></div>\n<p class="caption"><span class="caption-text">'
|
|
|
|
'Test Foo!</span><a class="headerlink" href="#id1" '
|
|
|
|
'title="Permalink to this image">\xb6</a></p>')
|
|
|
|
assert re.search(pattern, content, re.M)
|
|
|
|
|
|
|
|
|
2017-01-05 10:14:47 -06:00
|
|
|
@pytest.mark.sphinx('latex', testroot='ext-inheritance_diagram')
|
2017-01-03 07:24:00 -06:00
|
|
|
@pytest.mark.usefixtures('if_graphviz_found')
|
2016-03-10 22:20:05 -06:00
|
|
|
def test_inheritance_diagram_latex(app, status, warning):
|
|
|
|
app.builder.build_all()
|
|
|
|
|
2018-09-03 07:18:58 -05:00
|
|
|
content = (app.outdir / 'python.tex').text()
|
2016-03-10 22:20:05 -06:00
|
|
|
|
|
|
|
pattern = ('\\\\begin{figure}\\[htbp]\n\\\\centering\n\\\\capstart\n\n'
|
2018-02-11 08:32:59 -06:00
|
|
|
'\\\\sphinxincludegraphics\\[\\]{inheritance-\\w+.pdf}\n'
|
2016-12-08 15:14:48 -06:00
|
|
|
'\\\\caption{Test Foo!}\\\\label{\\\\detokenize{index:id1}}\\\\end{figure}')
|
2016-03-10 22:20:05 -06:00
|
|
|
assert re.search(pattern, content, re.M)
|
2016-11-22 22:22:38 -06:00
|
|
|
|
2016-11-21 09:00:50 -06:00
|
|
|
|
2017-07-26 14:07:25 -05:00
|
|
|
@pytest.mark.sphinx('html', testroot='ext-inheritance_diagram',
|
|
|
|
srcdir='ext-inheritance_diagram-alias')
|
|
|
|
@pytest.mark.usefixtures('if_graphviz_found')
|
|
|
|
def test_inheritance_diagram_latex_alias(app, status, warning):
|
|
|
|
app.config.inheritance_alias = {'test.Foo': 'alias.Foo'}
|
|
|
|
app.builder.build_all()
|
|
|
|
|
|
|
|
doc = app.env.get_and_resolve_doctree('index', app)
|
|
|
|
aliased_graph = doc.children[0].children[3]['graph'].class_info
|
|
|
|
assert len(aliased_graph) == 3
|
|
|
|
assert ('test.Baz', 'test.Baz', ['test.Bar'], None) in aliased_graph
|
|
|
|
assert ('test.Bar', 'test.Bar', ['alias.Foo'], None) in aliased_graph
|
|
|
|
assert ('alias.Foo', 'alias.Foo', [], None) in aliased_graph
|
|
|
|
|
|
|
|
content = (app.outdir / 'index.html').text()
|
|
|
|
|
2019-02-04 10:02:20 -06:00
|
|
|
pattern = ('<div class="figure align-center" id="id1">\n'
|
2018-04-20 01:10:30 -05:00
|
|
|
'<div class="graphviz">'
|
2017-07-26 14:07:25 -05:00
|
|
|
'<img src="_images/inheritance-\\w+.png" alt="Inheritance diagram of test.Foo" '
|
2018-08-04 04:38:39 -05:00
|
|
|
'class="inheritance graphviz" /></div>\n<p class="caption">'
|
|
|
|
'<span class="caption-text">Test Foo!</span><a class="headerlink" href="#id1" '
|
2017-07-26 14:07:25 -05:00
|
|
|
'title="Permalink to this image">\xb6</a></p>')
|
|
|
|
assert re.search(pattern, content, re.M)
|
|
|
|
|
|
|
|
|
2017-05-07 02:46:44 -05:00
|
|
|
def test_import_classes(rootdir):
|
2019-03-29 10:16:45 -05:00
|
|
|
from sphinx.parsers import Parser, RSTParser
|
2016-11-22 01:32:03 -06:00
|
|
|
from sphinx.util.i18n import CatalogInfo
|
2016-11-21 09:00:50 -06:00
|
|
|
|
|
|
|
try:
|
2017-05-07 02:46:44 -05:00
|
|
|
sys.path.append(rootdir / 'test-ext-inheritance_diagram')
|
2016-11-22 01:32:03 -06:00
|
|
|
from example.sphinx import DummyClass
|
2016-11-21 09:00:50 -06:00
|
|
|
|
|
|
|
# got exception for unknown class or module
|
2017-01-05 09:46:42 -06:00
|
|
|
with pytest.raises(InheritanceException):
|
|
|
|
import_classes('unknown', None)
|
|
|
|
with pytest.raises(InheritanceException):
|
|
|
|
import_classes('unknown.Unknown', None)
|
2016-11-21 09:00:50 -06:00
|
|
|
|
2018-02-01 13:29:18 -06:00
|
|
|
# got exception InheritanceException for wrong class or module
|
|
|
|
# not AttributeError (refs: #4019)
|
|
|
|
with pytest.raises(InheritanceException):
|
|
|
|
import_classes('unknown', '.')
|
|
|
|
with pytest.raises(InheritanceException):
|
|
|
|
import_classes('unknown.Unknown', '.')
|
|
|
|
with pytest.raises(InheritanceException):
|
|
|
|
import_classes('.', None)
|
|
|
|
|
2016-11-21 09:00:50 -06:00
|
|
|
# a module having no classes
|
|
|
|
classes = import_classes('sphinx', None)
|
|
|
|
assert classes == []
|
|
|
|
|
|
|
|
classes = import_classes('sphinx', 'foo')
|
|
|
|
assert classes == []
|
|
|
|
|
|
|
|
# all of classes in the module
|
2019-03-29 10:16:45 -05:00
|
|
|
classes = import_classes('sphinx.parsers', None)
|
|
|
|
assert set(classes) == {Parser, RSTParser}
|
2016-11-21 09:00:50 -06:00
|
|
|
|
|
|
|
# specified class in the module
|
2019-03-29 10:16:45 -05:00
|
|
|
classes = import_classes('sphinx.parsers.Parser', None)
|
|
|
|
assert classes == [Parser]
|
2016-11-21 09:00:50 -06:00
|
|
|
|
|
|
|
# specified class in current module
|
2019-03-29 10:16:45 -05:00
|
|
|
classes = import_classes('Parser', 'sphinx.parsers')
|
|
|
|
assert classes == [Parser]
|
2016-11-21 09:00:50 -06:00
|
|
|
|
2016-11-22 01:32:03 -06:00
|
|
|
# relative module name to current module
|
|
|
|
classes = import_classes('i18n.CatalogInfo', 'sphinx.util')
|
|
|
|
assert classes == [CatalogInfo]
|
2016-11-21 09:00:50 -06:00
|
|
|
|
|
|
|
# got exception for functions
|
2017-01-05 09:46:42 -06:00
|
|
|
with pytest.raises(InheritanceException):
|
|
|
|
import_classes('encode_uri', 'sphinx.util')
|
2016-11-21 09:00:50 -06:00
|
|
|
|
2016-11-22 01:32:03 -06:00
|
|
|
# import submodule on current module (refs: #3164)
|
2016-11-21 09:00:50 -06:00
|
|
|
classes = import_classes('sphinx', 'example')
|
2016-11-22 01:32:03 -06:00
|
|
|
assert classes == [DummyClass]
|
2016-11-21 09:00:50 -06:00
|
|
|
finally:
|
|
|
|
sys.path.pop()
|