Debian Codegraph @

tests/test_extensions/test_ext_graphviz.py

raw
1 """Test sphinx.ext.graphviz extension."""
2
3 from __future__ import annotations
4
5 import re
6 import sys
7 from typing import TYPE_CHECKING
8
9 import pytest
10
11 from sphinx.ext.graphviz import ClickableMapDefinition
12
13 if TYPE_CHECKING:
14 from sphinx.testing.util import SphinxTestApp
15
16
17 @pytest.mark.sphinx('html', testroot='ext-graphviz')
18 @pytest.mark.usefixtures('if_graphviz_found')
19 def test_graphviz_png_html(app: SphinxTestApp) -> None:
20 app.build(force_all=True)
21
22 content = (app.outdir / 'index.html').read_text(encoding='utf8')
23 html = (
24 r'<figure class="align-default" .*?>\s*'
25 r'<div class="graphviz"><img .*?/></div>\s*<figcaption>\s*'
26 r'<p><span class="caption-text">caption of graph</span>.*</p>\s*'
27 r'</figcaption>\s*</figure>'
28 )
29 assert re.search(html, content, re.DOTALL)
30
31 html = 'Hello <div class="graphviz"><img .*?/></div>\n graphviz world'
32 assert re.search(html, content, re.DOTALL)
33
34 html = (
35 '<img src=".*?" alt="digraph foo {\nbaz -&gt; qux\n}" '
36 'class="graphviz neato-graph" />'
37 )
38 assert re.search(html, content, re.DOTALL)
39
40 html = (
41 r'<figure class="align-right" .*?>\s*'
42 r'<div class="graphviz"><img .*?/></div>\s*<figcaption>\s*'
43 r'<p><span class="caption-text">on <em>right</em></span>.*</p>\s*'
44 r'</figcaption>\s*</figure>'
45 )
46 assert re.search(html, content, re.DOTALL)
47
48 html = (
49 r'<div align=\"center\" class=\"align-center\">'
50 r'<div class="graphviz"><img src=\".*\.png\" alt=\"digraph foo {\n'
51 r'centered\n'
52 r'}\" class="graphviz" /></div>\n</div>'
53 )
54 assert re.search(html, content, re.DOTALL)
55
56
57 @pytest.mark.sphinx(
58 'html',
59 testroot='ext-graphviz',
60 confoverrides={'graphviz_output_format': 'svg'},
61 )
62 @pytest.mark.usefixtures('if_graphviz_found')
63 def test_graphviz_svg_html(app: SphinxTestApp) -> None:
64 app.build(force_all=True)
65
66 content = (app.outdir / 'index.html').read_text(encoding='utf8')
67
68 html = (
69 r'<figure class=\"align-default\" .*?>\n'
70 r'<div class="graphviz"><object data=\".*\.svg\".*>\n'
71 r'\s*<p class=\"warning\">digraph foo {\n'
72 r'bar -&gt; baz\n'
73 r'}</p></object></div>\n'
74 r'<figcaption>\n'
75 r'<p><span class=\"caption-text\">caption of graph</span>.*</p>\n'
76 r'</figcaption>\n'
77 r'</figure>'
78 )
79 assert re.search(html, content, re.DOTALL)
80
81 html = (
82 r'Hello <div class="graphviz"><object.*>\n'
83 r'\s*<p class=\"warning\">graph</p></object></div>\n'
84 r' graphviz world'
85 )
86 assert re.search(html, content, re.DOTALL)
87
88 html = (
89 r'<figure class=\"align-right\" .*\>\n'
90 r'<div class="graphviz"><object data=\".*\.svg\".*>\n'
91 r'\s*<p class=\"warning\">digraph bar {\n'
92 r'foo -&gt; bar\n'
93 r'}</p></object></div>\n'
94 r'<figcaption>\n'
95 r'<p><span class=\"caption-text\">on <em>right</em></span>.*</p>\n'
96 r'</figcaption>\n'
97 r'</figure>'
98 )
99 assert re.search(html, content, re.DOTALL)
100
101 html = (
102 r'<div align=\"center\" class=\"align-center\">'
103 r'<div class="graphviz"><object data=\".*\.svg\".*>\n'
104 r'\s*<p class=\"warning\">digraph foo {\n'
105 r'centered\n'
106 r'}</p></object></div>\n'
107 r'</div>'
108 )
109 assert re.search(html, content, re.DOTALL)
110
111 image_re = r'.*data="([^"]+)".*?digraph test'
112 image_path_match = re.search(image_re, content, re.DOTALL)
113 assert image_path_match
114
115 image_path = image_path_match.group(1)
116 image_content = (app.outdir / image_path).read_text(encoding='utf8')
117 if sys.platform == 'win32':
118 assert '".\\_static\\' not in image_content
119 assert r'<ns0:image ns1:href="..\_static\images\test.svg"' in image_content
120 assert r'<ns0:a ns1:href="..\_static\images\test.svg"' in image_content
121 else:
122 assert '"./_static/' not in image_content
123 assert '<ns0:image ns1:href="../_static/images/test.svg"' in image_content
124 assert '<ns0:a ns1:href="../_static/images/test.svg"' in image_content
125 assert '<ns0:a ns1:href="..#graphviz"' in image_content
126
127
128 @pytest.mark.sphinx('latex', testroot='ext-graphviz')
129 @pytest.mark.usefixtures('if_graphviz_found')
130 def test_graphviz_latex(app: SphinxTestApp) -> None:
131 app.build(force_all=True)
132
133 content = (app.outdir / 'projectnamenotset.tex').read_text(encoding='utf8')
134 macro = (
135 '\\\\begin{figure}\\[htbp\\]\n\\\\centering\n\\\\capstart\n\n'
136 '\\\\sphinxincludegraphics\\[\\]{graphviz-\\w+.pdf}\n'
137 '\\\\caption{caption of graph}\\\\label{.*}\\\\end{figure}'
138 )
139 assert re.search(macro, content, re.DOTALL)
140
141 macro = 'Hello \\\\sphinxincludegraphics\\[\\]{graphviz-\\w+.pdf} graphviz world'
142 assert re.search(macro, content, re.DOTALL)
143
144 macro = (
145 '\\\\begin{wrapfigure}{r}{0pt}\n\\\\centering\n'
146 '\\\\sphinxincludegraphics\\[\\]{graphviz-\\w+.pdf}\n'
147 '\\\\caption{on \\\\sphinxstyleemphasis{right}}'
148 '\\\\label{.*}\\\\end{wrapfigure}'
149 )
150 assert re.search(macro, content, re.DOTALL)
151
152 macro = r'\{\\hfill\\sphinxincludegraphics\[\]{graphviz-.*}\\hspace\*{\\fill}}'
153 assert re.search(macro, content, re.DOTALL)
154
155
156 @pytest.mark.sphinx('html', testroot='ext-graphviz', confoverrides={'language': 'xx'})
157 @pytest.mark.usefixtures('if_graphviz_found')
158 def test_graphviz_i18n(app: SphinxTestApp) -> None:
159 app.build(force_all=True)
160
161 content = (app.outdir / 'index.html').read_text(encoding='utf8')
162 html = '<img src=".*?" alt="digraph {\n BAR -&gt; BAZ\n}" class="graphviz" />'
163 assert re.search(html, content, re.MULTILINE)
164
165
166 def test_graphviz_parse_mapfile() -> None:
167 # empty graph
168 code = '# digraph {\n# }\n'
169 content = '<map id="%3" name="%3">\n</map>'
170 cmap = ClickableMapDefinition('dummy.map', content, code)
171 assert cmap.filename == 'dummy.map'
172 assert cmap.id == 'grapvizb08107169e'
173 assert len(cmap.clickable) == 0
174 assert cmap.generate_clickable_map() == ''
175
176 # normal graph
177 code = 'digraph {\n foo [href="https://www.google.com/"];\n foo -> bar;\n}\n'
178 content = (
179 '<map id="%3" name="%3">\n'
180 '<area shape="poly" id="node1" href="https://www.google.com/" title="foo" alt=""'
181 ' coords="77,29,76,22,70,15,62,10,52,7,41,5,30,7,20,10,12,15,7,22,5,29,7,37,12,'
182 '43,20,49,30,52,41,53,52,52,62,49,70,43,76,37"/>\n'
183 '</map>'
184 )
185 cmap = ClickableMapDefinition('dummy.map', content, code)
186 assert cmap.filename == 'dummy.map'
187 assert cmap.id == 'grapvizff087ab863'
188 assert len(cmap.clickable) == 1
189 assert cmap.generate_clickable_map() == content.replace('%3', cmap.id)
190
191 # inheritance-diagram:: sphinx.builders.html
192 content = (
193 '<map id="inheritance66ff5471b9" name="inheritance66ff5471b9">\n'
194 '<area shape="rect" id="node1" title="Builds target formats from the reST sources."'
195 ' alt="" coords="26,95,125,110"/>\n'
196 '<area shape="rect" id="node5" title="Builds standalone HTML docs."'
197 ' alt="" coords="179,95,362,110"/>\n'
198 '<area shape="rect" id="node2" title="buildinfo file manipulator." '
199 ' alt="" coords="14,64,138,80"/>\n'
200 '<area shape="rect" id="node3" title="The container of stylesheets."'
201 ' alt="" coords="3,34,148,49"/>\n'
202 '<area shape="rect" id="node4" title="A StandaloneHTMLBuilder that creates all HTML'
203 ' pages as &quot;index.html&quot; in" alt="" coords="395,64,569,80"/>\n'
204 '<area shape="rect" id="node7" title="An abstract builder that serializes'
205 ' the generated HTML." alt="" coords="392,95,571,110"/>\n'
206 '<area shape="rect" id="node9" title="A StandaloneHTMLBuilder subclass that puts'
207 ' the whole document tree on one" alt="" coords="393,125,570,141"/>\n'
208 '<area shape="rect" id="node6" title="A builder that dumps the generated HTML'
209 ' into JSON files." alt="" coords="602,80,765,95"/>\n'
210 '<area shape="rect" id="node8" title="A Builder that dumps the generated HTML'
211 ' into pickle files." alt="" coords="602,110,765,125"/>\n'
212 '<area shape="rect" id="node10" title="The metadata of stylesheet."'
213 ' alt="" coords="11,3,141,19"/>\n'
214 '</map>'
215 )
216 cmap = ClickableMapDefinition('dummy.map', content, 'dummy_code')
217 assert cmap.filename == 'dummy.map'
218 assert cmap.id == 'inheritance66ff5471b9'
219 assert len(cmap.clickable) == 0
220 assert cmap.generate_clickable_map() == ''