1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
|
import re
import sys
import pytest
import sphinx
import textwrap
from hoverxref import __version__
from .utils import srcdir, prefixdocumentsrcdir, customobjectsrcdir, pythondomainsrcdir, intersphinxsrc, bibtexdomainsrcdir
@pytest.mark.sphinx(
srcdir=srcdir,
)
def test_default_settings(app, status, warning):
"""The extension should not change the output if not configured."""
app.build()
path = app.outdir / 'index.html'
assert path.exists() is True
content = open(path).read()
chunks = [
'<a class="reference internal" href="chapter-i.html#chapter-i"><span class="std std-ref">This a :ref: to Chapter I</span></a>',
'<a class="hxr-hoverxref hxr-tooltip reference internal" href="chapter-i.html#section-i"><span class="std std-ref">This a :hoverxref: to Chapter I, Section I</span></a>',
]
for chunk in chunks:
assert chunk in content
@pytest.mark.sphinx(
srcdir=srcdir,
)
def test_js_render(app, status, warning):
app.build()
path = app.outdir / '_static' / 'js' / 'hoverxref.js'
assert path.exists() is True
content = open(path).read()
chunks = [
"theme: ['tooltipster-shadow', 'tooltipster-shadow-custom']",
"interactive: true",
"maxWidth: 450",
"animation: 'fade'",
"animationDuration: 0",
"contentAsHTML: true",
"content: 'Loading...'",
"var url = '/_' + '/api/v3/embed/?' + $.param(params);",
textwrap.indent(
textwrap.dedent("""
var params = {{
'doctool': 'sphinx',
'doctoolversion': '{}',
'url': url,
}}""".format(sphinx.__version__)),
' ',
).strip(),
"var sphinxtabs = false",
"var mathjax = false",
"var url = getEmbedURL(href);",
f"headers: {{'X-HoverXRef-Version': '{__version__}'}}",
]
for chunk in chunks:
assert chunk in content
@pytest.mark.sphinx(
srcdir=prefixdocumentsrcdir,
)
def test_autosectionlabel_project_version_settings(app, status, warning):
app.build()
path = app.outdir / 'index.html'
assert path.exists() is True
content = open(path).read()
chunks = [
'<a class="reference internal" href="chapter-i.html#chapter-i"><span class="std std-ref">This a :ref: to Chapter I</span></a>.',
'<a class="hxr-hoverxref hxr-tooltip reference internal" href="chapter-i.html#chapter-i"><span class="std std-ref">This a :hoverxref: to Chapter I</span></a>',
]
for chunk in chunks:
assert chunk in content
@pytest.mark.sphinx(
srcdir=customobjectsrcdir,
confoverrides={},
)
def test_custom_object(app, status, warning):
app.build()
path = app.outdir / 'index.html'
assert path.exists() is True
content = open(path).read()
chunks = [
'<a class="hxr-hoverxref hxr-tooltip reference internal" href="configuration.html#confval-conf-title"><code class="xref std std-confval docutils literal notranslate"><span class="pre">This</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">:confval:</span> <span class="pre">to</span> <span class="pre">conf-title</span></code></a>',
'<a class="hxr-hoverxref hxr-tooltip reference internal" href="configuration.html#configuration"><span class="std std-ref">This is a :hoverxref: to Configuration document</span></a>',
'<a class="hxr-hoverxref hxr-tooltip reference internal" href="code.html#python-code-block"><span class="std std-numref">This is a :numref: to a Python code block (PyExample)</span></a>'
]
for chunk in chunks:
assert chunk in content
@pytest.mark.sphinx(
srcdir=pythondomainsrcdir,
confoverrides={
'hoverxref_domains': ['py'],
},
)
def test_python_domain(app, status, warning):
app.build()
path = app.outdir / 'index.html'
assert path.exists() is True
content = open(path).read()
chunks = [
'<a class="hxr-hoverxref hxr-tooltip reference internal" href="api.html#hoverxref.extension.HoverXRefStandardDomainMixin" title="hoverxref.extension.HoverXRefStandardDomainMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">This</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">:py:class:</span> <span class="pre">role</span> <span class="pre">to</span> <span class="pre">a</span> <span class="pre">Python</span> <span class="pre">object</span></code></a>',
'<a class="hxr-hoverxref hxr-tooltip reference internal" href="api.html#module-hoverxref.extension" title="hoverxref.extension"><code class="xref py py-mod docutils literal notranslate"><span class="pre">hoverxref.extension</span></code></a>',
'<a class="hxr-hoverxref hxr-tooltip reference internal" href="api.html#hoverxref.extension.setup" title="hoverxref.extension.setup"><code class="xref py py-func docutils literal notranslate"><span class="pre">hoverxref.extension.setup()</span></code></a>',
'<code class="xref py py-const docutils literal notranslate"><span class="pre">Constant</span></code>',
]
for chunk in chunks:
assert chunk in content
@pytest.mark.sphinx(
srcdir=pythondomainsrcdir,
confoverrides={
'hoverxref_domains': ['py'],
'hoverxref_intersphinx': ['python'],
'hoverxref_auto_ref': True,
'extensions': [
'sphinx.ext.autodoc',
'sphinx.ext.autosectionlabel',
'sphinx.ext.intersphinx',
'hoverxref.extension',
],
},
)
def test_python_domain_intersphinx(app, status, warning):
app.build()
path = app.outdir / 'index.html'
assert path.exists() is True
content = open(path).read()
chunks = [
'<a class="hxr-hoverxref hxr-tooltip reference internal" href="api.html#hoverxref.extension.HoverXRefStandardDomainMixin" title="hoverxref.extension.HoverXRefStandardDomainMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">This</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">:py:class:</span> <span class="pre">role</span> <span class="pre">to</span> <span class="pre">a</span> <span class="pre">Python</span> <span class="pre">object</span></code></a>',
'<a class="hxr-hoverxref hxr-tooltip reference internal" href="api.html#module-hoverxref.extension" title="hoverxref.extension"><code class="xref py py-mod docutils literal notranslate"><span class="pre">hoverxref.extension</span></code></a>',
'<a class="hxr-hoverxref hxr-tooltip reference internal" href="api.html#hoverxref.extension.setup" title="hoverxref.extension.setup"><code class="xref py py-func docutils literal notranslate"><span class="pre">hoverxref.extension.setup()</span></code></a>',
'<code class="xref py py-const docutils literal notranslate"><span class="pre">Constant</span></code>',
]
for chunk in chunks:
assert chunk in content
@pytest.mark.skipif(sys.version_info >= (3, 12), reason="sphinxcontrib.bibtex is not compatible with Python 3.12")
@pytest.mark.sphinx(
srcdir=bibtexdomainsrcdir,
confoverrides={
'hoverxref_domains': ['cite'],
},
)
def test_bibtex_domain(app, status, warning):
app.build()
path = app.outdir / 'index.html'
assert path.exists() is True
content = open(path).read()
chunks = [
'<p>See <span id="id1">Nelson [<a class="hxr-hoverxref hxr-tooltip reference internal" href="#id4" title="Edward Nelson. Radically Elementary Probability Theory. Princeton University Press, 1987.">Nel87</a>]</span> for an introduction to non-standard analysis.\nNon-standard analysis is fun <span id="id2">[<a class="hxr-hoverxref hxr-tooltip reference internal" href="#id4" title="Edward Nelson. Radically Elementary Probability Theory. Princeton University Press, 1987.">Nel87</a>]</span>.</p>',
]
for chunk in chunks:
assert chunk in content
@pytest.mark.sphinx(
srcdir=srcdir,
confoverrides={
'hoverxref_roles': ['term'],
},
)
def test_glossary_term_domain(app, status, warning):
app.build()
path = app.outdir / 'glossary.html'
assert path.exists() is True
content = open(path).read()
chunks = [
'<p>See definition <a class="hxr-hoverxref hxr-tooltip reference internal" href="#term-builder"><span class="xref std std-term">builder</span></a> for more information.</p>',
]
for chunk in chunks:
assert chunk in content
@pytest.mark.sphinx(
srcdir=srcdir,
confoverrides={
'hoverxref_default_type': 'modal',
},
)
def test_default_type(app, status, warning):
app.build()
path = app.outdir / 'index.html'
assert path.exists() is True
content = open(path).read()
chunks = [
'<a class="reference internal" href="chapter-i.html#chapter-i"><span class="std std-ref">This a :ref: to Chapter I</span></a>',
'<a class="hxr-hoverxref hxr-modal reference internal" href="chapter-i.html#section-i"><span class="std std-ref">This a :hoverxref: to Chapter I, Section I</span></a>',
]
for chunk in chunks:
assert chunk in content
@pytest.mark.sphinx(
srcdir=srcdir,
confoverrides={
'hoverxref_ignore_refs': [
'section i',
],
},
)
def test_ignore_refs(app, status, warning):
app.build()
path = app.outdir / 'index.html'
assert path.exists() is True
content = open(path).read()
chunks = [
'<a class="reference internal" href="chapter-i.html#chapter-i"><span class="std std-ref">This a :ref: to Chapter I</span></a>',
'<a class="reference internal" href="chapter-i.html#section-i"><span class="std std-ref">This a :hoverxref: to Chapter I, Section I</span></a>',
]
for chunk in chunks:
assert chunk in content
ignored_chunks = [
'<a class="hxr-hoverxref reference internal" href="chapter-i.html#section-i"><span class="std std-ref">This a :hoverxref: to Chapter I, Section I</span></a>',
]
for chunk in ignored_chunks:
assert chunk not in content
@pytest.mark.sphinx(
srcdir=intersphinxsrc,
)
def test_intersphinx_default_configs(app, status, warning):
app.build()
path = app.outdir / 'index.html'
assert path.exists() is True
content = open(path).read()
chunks_regex = [
r'<a class="reference external" href="https://docs.python.org/3/tutorial/index.html#tutorial-index" title="\(in Python v3.\d\d?\)"><span class="xref std std-ref">This a :ref: to The Python Tutorial using intersphinx</span></a>',
r'<a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime-datetime" title="\(in Python v3.\d\d?\)"><span class="xref std std-ref">This a :ref: to datetime.datetime Python’s function using intersphinx</span></a>',
r'<a class="reference external" href="https://docs.readthedocs.io/en/stable/config-file/v2.html#python" title="\(in Read the Docs user documentation v\d\d?.\d\d?.\d\d?\)"><span class="xref std std-ref">This a :ref: to Config File v2 Read the Docs’ page using intersphinx</span></a>',
r'<a class="reference external" href="https://docs.python.org/3/library/functions.html#float" title="\(in Python v3.\d\d?\)"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a>',
]
chunks = [
'<a class="reference internal" href="#hoverxref.extension.setup" title="hoverxref.extension.setup"><code class="xref py py-func docutils literal notranslate"><span class="pre">hoverxref.extension.setup()</span></code></a>',
]
chunks.extend([
'<dt class="sig sig-object py" id="hoverxref.extension.setup">',
])
for chunk in chunks:
assert chunk in content
for chunk in chunks_regex:
assert re.search(chunk, content)
@pytest.mark.sphinx(
srcdir=intersphinxsrc,
confoverrides={
'hoverxref_intersphinx': [
'python',
],
},
)
def test_intersphinx_python_mapping(app, status, warning):
app.build()
path = app.outdir / 'index.html'
assert path.exists() is True
content = open(path).read()
chunks_regex = [
# Python's links do have hoverxref enabled
r'<a class="hxr-hoverxref hxr-tooltip reference external" href="https://docs.python.org/3/tutorial/index.html#tutorial-index" title="\(in Python v3.\d\d?\)"><span class="xref std std-ref">This a :ref: to The Python Tutorial using intersphinx</span></a>',
r'<a class="hxr-hoverxref hxr-tooltip reference external" href="https://docs.python.org/3/library/datetime.html#datetime-datetime" title="\(in Python v3.\d\d?\)"><span class="xref std std-ref">This a :ref: to datetime.datetime Python’s function using intersphinx</span></a>',
r'<a class="hxr-hoverxref hxr-tooltip reference external" href="https://docs.python.org/3/library/functions.html#float" title="\(in Python v3.\d\d?\)"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a>',
# Read the Docs' link does not have hoverxref enabled
r'<a class="reference external" href="https://docs.readthedocs.io/en/stable/config-file/v2.html#python" title="\(in Read the Docs user documentation v\d\d?.\d\d?.\d\d?\)"><span class="xref std std-ref">This a :ref: to Config File v2 Read the Docs’ page using intersphinx</span></a>',
]
chunks = [
# Python's domain does not have hoverxref enabled
'<a class="reference internal" href="#hoverxref.extension.setup" title="hoverxref.extension.setup"><code class="xref py py-func docutils literal notranslate"><span class="pre">hoverxref.extension.setup()</span></code></a>',
]
for chunk in chunks:
assert chunk in content
for chunk in chunks_regex:
assert re.search(chunk, content)
@pytest.mark.sphinx(
srcdir=intersphinxsrc,
confoverrides={
'hoverxref_intersphinx': [
'readthedocs',
'python',
],
'hoverxref_intersphinx_types': {
'readthedocs': 'modal',
'python': {
'class': 'modal',
}
},
'hoverxref_domains': ['py'],
'default_role': 'obj',
},
)
def test_intersphinx_all_mappings(app, status, warning):
app.build()
path = app.outdir / 'index.html'
assert path.exists() is True
content = open(path).read()
chunks_regex = [
# Python's links do have hoverxref enabled
r'<a class="hxr-hoverxref hxr-tooltip reference external" href="https://docs.python.org/3/tutorial/index.html#tutorial-index" title="\(in Python v3.\d\d?\)"><span class="xref std std-ref">This a :ref: to The Python Tutorial using intersphinx</span></a>',
r'<a class="hxr-hoverxref hxr-tooltip reference external" href="https://docs.python.org/3/library/datetime.html#datetime-datetime" title="\(in Python v3.\d\d?\)"><span class="xref std std-ref">This a :ref: to datetime.datetime Python’s function using intersphinx</span></a>',
r'<a class="hxr-hoverxref hxr-tooltip reference external" href="https://docs.python.org/3/library/datetime.html#datetime.time.minute" title="\(in Python v3.\d\d?\)"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Python\'s</span> <span class="pre">datetime.time.minute</span></code></a>',
r'<a class="hxr-hoverxref hxr-modal reference external" href="https://docs.python.org/3/library/functions.html#float" title="\(in Python v3.\d\d?\)"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a>',
# Read the Docs' link does have hoverxref enabled
r'<a class="hxr-hoverxref hxr-modal reference external" href="https://docs.readthedocs.io/en/stable/config-file/v2.html#python" title="\(in Read the Docs user documentation v\d\d?.\d\d?.\d+\)"><span class="xref std std-ref">This a :ref: to Config File v2 Read the Docs’ page using intersphinx</span></a>',
# Using `default_role = 'obj'`
# Note the difference with the same `float` line previouly. Here it uses `py-obj` instead of `py-class`.
r'<a class="hxr-hoverxref hxr-tooltip reference external" href="https://docs.python.org/3/library/functions.html#float" title="\(in Python v3.\d\d?\)"><code class="xref py py-obj docutils literal notranslate"><span class="pre">float</span></code></a>'
]
chunks = [
# Python domain's link does have hoverxref enabled
'<a class="hxr-hoverxref hxr-tooltip reference internal" href="#hoverxref.extension.setup" title="hoverxref.extension.setup"><code class="xref py py-func docutils literal notranslate"><span class="pre">hoverxref.extension.setup()</span></code></a>',
]
for chunk in chunks:
assert chunk in content
for chunk in chunks_regex:
assert re.search(chunk, content)
|