| 1 |
"""Test math extensions.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import re |
| 6 |
import shutil |
| 7 |
import subprocess |
| 8 |
import warnings |
| 9 |
from typing import TYPE_CHECKING |
| 10 |
|
| 11 |
import pytest |
| 12 |
from docutils import nodes |
| 13 |
|
| 14 |
from sphinx.ext.mathjax import MATHJAX_URL |
| 15 |
from sphinx.testing.util import assert_node |
| 16 |
|
| 17 |
from tests.utils import extract_node |
| 18 |
|
| 19 |
if TYPE_CHECKING: |
| 20 |
from sphinx.testing.util import SphinxTestApp |
| 21 |
|
| 22 |
FAKE_MATHJAX_URL = 'https://example.org/fake-mathjax.js' |
| 23 |
|
| 24 |
|
| 25 |
def has_binary(binary: str) -> bool: |
| 26 |
try: |
| 27 |
subprocess.check_output([binary]) |
| 28 |
except FileNotFoundError: |
| 29 |
return False |
| 30 |
except OSError: |
| 31 |
pass |
| 32 |
return True |
| 33 |
|
| 34 |
|
| 35 |
@pytest.mark.skipif( |
| 36 |
not has_binary('dvipng'), |
| 37 |
reason='Requires dvipng" binary', |
| 38 |
) |
| 39 |
@pytest.mark.sphinx( |
| 40 |
'html', |
| 41 |
testroot='ext-math-simple', |
| 42 |
confoverrides={'extensions': ['sphinx.ext.imgmath']}, |
| 43 |
) |
| 44 |
def test_imgmath_png(app: SphinxTestApp) -> None: |
| 45 |
app.build(force_all=True) |
| 46 |
if "LaTeX command 'latex' cannot be run" in app.warning.getvalue(): |
| 47 |
msg = 'LaTeX command "latex" is not available' |
| 48 |
raise pytest.skip.Exception(msg) |
| 49 |
if "dvipng command 'dvipng' cannot be run" in app.warning.getvalue(): |
| 50 |
msg = 'dvipng command "dvipng" is not available' |
| 51 |
raise pytest.skip.Exception(msg) |
| 52 |
|
| 53 |
content = (app.outdir / 'index.html').read_text(encoding='utf8') |
| 54 |
shutil.rmtree(app.outdir) |
| 55 |
html = ( |
| 56 |
r'<div class="math">\s*<p>\s*<img src="_images/math/\w+.png"' |
| 57 |
r'\s*alt="a\^2\+b\^2=c\^2"/>\s*</p>\s*</div>' |
| 58 |
) |
| 59 |
assert re.search(html, content, re.DOTALL) |
| 60 |
|
| 61 |
|
| 62 |
@pytest.mark.skipif( |
| 63 |
not has_binary('dvisvgm'), |
| 64 |
reason='Requires dvisvgm" binary', |
| 65 |
) |
| 66 |
@pytest.mark.sphinx( |
| 67 |
'html', |
| 68 |
testroot='ext-math-simple', |
| 69 |
confoverrides={'extensions': ['sphinx.ext.imgmath'], 'imgmath_image_format': 'svg'}, |
| 70 |
) |
| 71 |
def test_imgmath_svg(app: SphinxTestApp) -> None: |
| 72 |
app.build(force_all=True) |
| 73 |
if "LaTeX command 'latex' cannot be run" in app.warning.getvalue(): |
| 74 |
msg = 'LaTeX command "latex" is not available' |
| 75 |
raise pytest.skip.Exception(msg) |
| 76 |
if "dvisvgm command 'dvisvgm' cannot be run" in app.warning.getvalue(): |
| 77 |
msg = 'dvisvgm command "dvisvgm" is not available' |
| 78 |
raise pytest.skip.Exception(msg) |
| 79 |
|
| 80 |
content = (app.outdir / 'index.html').read_text(encoding='utf8') |
| 81 |
shutil.rmtree(app.outdir) |
| 82 |
html = ( |
| 83 |
r'<div class="math">\s*<p>\s*<img src="_images/math/\w+.svg"' |
| 84 |
r'\s*alt="a\^2\+b\^2=c\^2"/>\s*</p>\s*</div>' |
| 85 |
) |
| 86 |
assert re.search(html, content, re.DOTALL) |
| 87 |
|
| 88 |
|
| 89 |
@pytest.mark.skipif( |
| 90 |
not has_binary('dvisvgm'), |
| 91 |
reason='Requires dvisvgm" binary', |
| 92 |
) |
| 93 |
@pytest.mark.sphinx( |
| 94 |
'html', |
| 95 |
testroot='ext-math-simple', |
| 96 |
confoverrides={ |
| 97 |
'extensions': ['sphinx.ext.imgmath'], |
| 98 |
'imgmath_image_format': 'svg', |
| 99 |
'imgmath_embed': True, |
| 100 |
}, |
| 101 |
) |
| 102 |
def test_imgmath_svg_embed(app: SphinxTestApp) -> None: |
| 103 |
app.build(force_all=True) |
| 104 |
if "LaTeX command 'latex' cannot be run" in app.warning.getvalue(): |
| 105 |
msg = 'LaTeX command "latex" is not available' |
| 106 |
raise pytest.skip.Exception(msg) |
| 107 |
if "dvisvgm command 'dvisvgm' cannot be run" in app.warning.getvalue(): |
| 108 |
msg = 'dvisvgm command "dvisvgm" is not available' |
| 109 |
raise pytest.skip.Exception(msg) |
| 110 |
|
| 111 |
content = (app.outdir / 'index.html').read_text(encoding='utf8') |
| 112 |
shutil.rmtree(app.outdir) |
| 113 |
html = r'<img src="data:image/svg\+xml;base64,[\w\+/=]+"' |
| 114 |
assert re.search(html, content, re.DOTALL) |
| 115 |
|
| 116 |
|
| 117 |
@pytest.mark.sphinx( |
| 118 |
'html', |
| 119 |
testroot='ext-math', |
| 120 |
confoverrides={ |
| 121 |
'extensions': ['sphinx.ext.mathjax'], |
| 122 |
'mathjax_options': {'integrity': 'sha384-0123456789'}, |
| 123 |
}, |
| 124 |
) |
| 125 |
def test_mathjax_options(app: SphinxTestApp) -> None: |
| 126 |
app.build(force_all=True) |
| 127 |
|
| 128 |
content = (app.outdir / 'index.html').read_text(encoding='utf8') |
| 129 |
shutil.rmtree(app.outdir) |
| 130 |
assert ( |
| 131 |
'<script defer="defer" integrity="sha384-0123456789" ' |
| 132 |
f'src="{MATHJAX_URL}">' |
| 133 |
'</script>' |
| 134 |
) in content |
| 135 |
|
| 136 |
|
| 137 |
@pytest.mark.sphinx( |
| 138 |
'html', |
| 139 |
testroot='ext-math', |
| 140 |
confoverrides={'extensions': ['sphinx.ext.mathjax']}, |
| 141 |
) |
| 142 |
def test_mathjax_align(app: SphinxTestApp) -> None: |
| 143 |
app.build(force_all=True) |
| 144 |
|
| 145 |
content = (app.outdir / 'index.html').read_text(encoding='utf8') |
| 146 |
shutil.rmtree(app.outdir) |
| 147 |
html = ( |
| 148 |
r'<div class="math notranslate nohighlight">\s*' |
| 149 |
r'\\\[ \\begin\{align\}\\begin\{aligned\}S \&= \\pi r\^2\\\\' |
| 150 |
r'V \&= \\frac\{4\}\{3\} \\pi r\^3\\end\{aligned\}\\end\{align\} \\\]</div>' |
| 151 |
) |
| 152 |
assert re.search(html, content, re.DOTALL) |
| 153 |
|
| 154 |
|
| 155 |
@pytest.mark.sphinx( |
| 156 |
'html', |
| 157 |
testroot='ext-math', |
| 158 |
confoverrides={'math_number_all': True, 'extensions': ['sphinx.ext.mathjax']}, |
| 159 |
) |
| 160 |
def test_math_number_all_mathjax(app: SphinxTestApp) -> None: |
| 161 |
app.build() |
| 162 |
|
| 163 |
content = (app.outdir / 'index.html').read_text(encoding='utf8') |
| 164 |
html = ( |
| 165 |
r'<div class="math notranslate nohighlight" id="equation-index-0">\s*' |
| 166 |
r'<span class="eqno">\(1\)<a .*>\xb6</a></span>\\\[a\^2\+b\^2=c\^2\\\]</div>' |
| 167 |
) |
| 168 |
assert re.search(html, content, re.DOTALL) |
| 169 |
|
| 170 |
|
| 171 |
@pytest.mark.sphinx( |
| 172 |
'latex', |
| 173 |
testroot='ext-math', |
| 174 |
confoverrides={'extensions': ['sphinx.ext.mathjax']}, |
| 175 |
) |
| 176 |
def test_math_number_all_latex(app: SphinxTestApp) -> None: |
| 177 |
app.build() |
| 178 |
|
| 179 |
content = (app.outdir / 'projectnamenotset.tex').read_text(encoding='utf8') |
| 180 |
macro = ( |
| 181 |
r'\\begin{equation\*}\s*' |
| 182 |
r'\\begin{split}a\^2\+b\^2=c\^2\\end{split}\s*' |
| 183 |
r'\\end{equation\*}' |
| 184 |
) |
| 185 |
assert re.search(macro, content, re.DOTALL) |
| 186 |
|
| 187 |
macro = r'Inline \\\(E=mc\^2\\\)' |
| 188 |
assert re.search(macro, content, re.DOTALL) |
| 189 |
|
| 190 |
macro = ( |
| 191 |
r'\\begin{equation\*}\s*' |
| 192 |
r'\\begin{split}e\^{i\\pi}\+1=0\\end{split}\s+' |
| 193 |
r'\\end{equation\*}' |
| 194 |
) |
| 195 |
assert re.search(macro, content, re.DOTALL) |
| 196 |
|
| 197 |
macro = ( |
| 198 |
r'\\begin{align\*}\\!\\begin{aligned}\s*' |
| 199 |
r'S &= \\pi r\^2\\\\\s*' |
| 200 |
r'V &= \\frac\{4}\{3} \\pi r\^3\\\\\s*' |
| 201 |
r'\\end{aligned}\\end{align\*}' |
| 202 |
) |
| 203 |
assert re.search(macro, content, re.DOTALL) |
| 204 |
|
| 205 |
macro = r'Referencing equation \\eqref{equation:math:foo}.' |
| 206 |
assert re.search(macro, content, re.DOTALL) |
| 207 |
|
| 208 |
|
| 209 |
@pytest.mark.sphinx( |
| 210 |
'html', |
| 211 |
testroot='ext-math', |
| 212 |
confoverrides={ |
| 213 |
'extensions': ['sphinx.ext.mathjax'], |
| 214 |
'math_eqref_format': 'Eq.{number}', |
| 215 |
}, |
| 216 |
) |
| 217 |
def test_math_eqref_format_html(app: SphinxTestApp) -> None: |
| 218 |
app.build(force_all=True) |
| 219 |
|
| 220 |
content = (app.outdir / 'math.html').read_text(encoding='utf8') |
| 221 |
html = ( |
| 222 |
'<p>Referencing equation <a class="reference internal" ' |
| 223 |
'href="#equation-foo">Eq.1</a> and <a class="reference internal" ' |
| 224 |
'href="#equation-foo">Eq.1</a>.</p>' |
| 225 |
) |
| 226 |
assert html in content |
| 227 |
|
| 228 |
|
| 229 |
@pytest.mark.sphinx( |
| 230 |
'latex', |
| 231 |
testroot='ext-math', |
| 232 |
confoverrides={ |
| 233 |
'extensions': ['sphinx.ext.mathjax'], |
| 234 |
'math_eqref_format': 'Eq.{number}', |
| 235 |
}, |
| 236 |
) |
| 237 |
def test_math_eqref_format_latex(app: SphinxTestApp) -> None: |
| 238 |
app.build(force_all=True) |
| 239 |
|
| 240 |
content = (app.outdir / 'projectnamenotset.tex').read_text(encoding='utf8') |
| 241 |
macro = ( |
| 242 |
r'Referencing equation Eq.\\ref{equation:math:foo} and ' |
| 243 |
r'Eq.\\ref{equation:math:foo}.' |
| 244 |
) |
| 245 |
assert re.search(macro, content, re.DOTALL) |
| 246 |
|
| 247 |
|
| 248 |
@pytest.mark.sphinx( |
| 249 |
'html', |
| 250 |
testroot='ext-math', |
| 251 |
confoverrides={ |
| 252 |
'extensions': ['sphinx.ext.mathjax'], |
| 253 |
'numfig': True, |
| 254 |
'math_numfig': True, |
| 255 |
}, |
| 256 |
) |
| 257 |
def test_mathjax_numfig_html(app: SphinxTestApp) -> None: |
| 258 |
app.build(force_all=True) |
| 259 |
|
| 260 |
content = (app.outdir / 'math.html').read_text(encoding='utf8') |
| 261 |
html = ( |
| 262 |
'<div class="math notranslate nohighlight" id="equation-math-0">\n' |
| 263 |
'<span class="eqno">(1.2)' |
| 264 |
) |
| 265 |
assert html in content |
| 266 |
html = ( |
| 267 |
'<p>Referencing equation <a class="reference internal" ' |
| 268 |
'href="#equation-foo">(1.1)</a> and ' |
| 269 |
'<a class="reference internal" href="#equation-foo">(1.1)</a>.</p>' |
| 270 |
) |
| 271 |
assert html in content |
| 272 |
|
| 273 |
|
| 274 |
@pytest.mark.sphinx( |
| 275 |
'html', |
| 276 |
testroot='ext-math', |
| 277 |
confoverrides={ |
| 278 |
'extensions': ['sphinx.ext.mathjax'], |
| 279 |
'numfig': True, |
| 280 |
'math_numfig': True, |
| 281 |
'math_numsep': '-', |
| 282 |
}, |
| 283 |
) |
| 284 |
def test_mathjax_numsep_html(app: SphinxTestApp) -> None: |
| 285 |
app.build(force_all=True) |
| 286 |
|
| 287 |
content = (app.outdir / 'math.html').read_text(encoding='utf8') |
| 288 |
html = ( |
| 289 |
'<div class="math notranslate nohighlight" id="equation-math-0">\n' |
| 290 |
'<span class="eqno">(1-2)' |
| 291 |
) |
| 292 |
assert html in content |
| 293 |
html = ( |
| 294 |
'<p>Referencing equation <a class="reference internal" ' |
| 295 |
'href="#equation-foo">(1-1)</a> and ' |
| 296 |
'<a class="reference internal" href="#equation-foo">(1-1)</a>.</p>' |
| 297 |
) |
| 298 |
assert html in content |
| 299 |
|
| 300 |
|
| 301 |
@pytest.mark.usefixtures('_http_teapot') |
| 302 |
@pytest.mark.sphinx( |
| 303 |
'html', |
| 304 |
testroot='ext-math', |
| 305 |
confoverrides={ |
| 306 |
'extensions': ['sphinx.ext.imgmath'], |
| 307 |
'numfig': True, |
| 308 |
'numfig_secnum_depth': 0, |
| 309 |
'math_numfig': True, |
| 310 |
}, |
| 311 |
) |
| 312 |
def test_imgmath_numfig_html(app: SphinxTestApp) -> None: |
| 313 |
app.build(force_all=True) |
| 314 |
|
| 315 |
content = (app.outdir / 'page.html').read_text(encoding='utf8') |
| 316 |
html = '<span class="eqno">(3)<a class="headerlink" href="#equation-bar"' |
| 317 |
assert html in content |
| 318 |
html = ( |
| 319 |
'<p>Referencing equations <a class="reference internal" ' |
| 320 |
'href="math.html#equation-foo">(1)</a> and ' |
| 321 |
'<a class="reference internal" href="#equation-bar">(3)</a>.</p>' |
| 322 |
) |
| 323 |
assert html in content |
| 324 |
|
| 325 |
|
| 326 |
@pytest.mark.sphinx('dummy', testroot='ext-math-compat') |
| 327 |
def test_math_compat(app: SphinxTestApp) -> None: |
| 328 |
with warnings.catch_warnings(record=True): |
| 329 |
app.build(force_all=True) |
| 330 |
doctree = app.env.get_and_resolve_doctree('index', app.builder, tags=app.tags) |
| 331 |
assert_node( |
| 332 |
doctree, |
| 333 |
[ |
| 334 |
nodes.document, |
| 335 |
nodes.section, |
| 336 |
( |
| 337 |
nodes.title, |
| 338 |
[nodes.section, (nodes.title, nodes.paragraph)], |
| 339 |
nodes.section, |
| 340 |
), |
| 341 |
], |
| 342 |
) |
| 343 |
assert_node( |
| 344 |
extract_node(doctree, 0, 1, 1), |
| 345 |
( |
| 346 |
'Inline: ', |
| 347 |
[nodes.math, 'E=mc^2'], |
| 348 |
'\nInline my math: ', |
| 349 |
[nodes.math, 'E = mc^2'], |
| 350 |
), |
| 351 |
) |
| 352 |
assert_node( |
| 353 |
extract_node(doctree, 0, 2), |
| 354 |
( |
| 355 |
[nodes.title, 'block'], |
| 356 |
[nodes.math_block, 'a^2+b^2=c^2\n\n'], |
| 357 |
[nodes.paragraph, 'Second math'], |
| 358 |
[nodes.math_block, 'e^{i\\pi}+1=0\n\n'], |
| 359 |
[nodes.paragraph, 'Multi math equations'], |
| 360 |
[nodes.math_block, 'E = mc^2'], |
| 361 |
), |
| 362 |
) |
| 363 |
|
| 364 |
|
| 365 |
@pytest.mark.sphinx( |
| 366 |
'html', |
| 367 |
testroot='ext-math', |
| 368 |
confoverrides={ |
| 369 |
'extensions': ['sphinx.ext.mathjax'], |
| 370 |
'mathjax4_config': {'extensions': ['tex2jax.js']}, |
| 371 |
}, |
| 372 |
) |
| 373 |
def test_mathjax4_config(app: SphinxTestApp) -> None: |
| 374 |
app.build(force_all=True) |
| 375 |
|
| 376 |
content = (app.outdir / 'index.html').read_text(encoding='utf8') |
| 377 |
assert MATHJAX_URL in content |
| 378 |
assert '<script defer="defer" src="%s">' % MATHJAX_URL in content |
| 379 |
assert '<script>window.MathJax = {"extensions": ["tex2jax.js"]}</script>' in content |
| 380 |
|
| 381 |
|
| 382 |
@pytest.mark.sphinx( |
| 383 |
'html', |
| 384 |
testroot='ext-math', |
| 385 |
confoverrides={ |
| 386 |
'extensions': ['sphinx.ext.mathjax'], |
| 387 |
'mathjax3_config': {'extensions': ['tex2jax.js']}, |
| 388 |
'mathjax_path': FAKE_MATHJAX_URL, |
| 389 |
}, |
| 390 |
) |
| 391 |
def test_mathjax3_config(app: SphinxTestApp) -> None: |
| 392 |
app.build(force_all=True) |
| 393 |
|
| 394 |
content = (app.outdir / 'index.html').read_text(encoding='utf8') |
| 395 |
assert FAKE_MATHJAX_URL in content |
| 396 |
assert f'<script defer="defer" src="{FAKE_MATHJAX_URL}">' in content |
| 397 |
assert '<script>window.MathJax = {"extensions": ["tex2jax.js"]}</script>' in content |
| 398 |
|
| 399 |
|
| 400 |
@pytest.mark.sphinx( |
| 401 |
'html', |
| 402 |
testroot='ext-math', |
| 403 |
confoverrides={ |
| 404 |
'extensions': ['sphinx.ext.mathjax'], |
| 405 |
'mathjax_config_path': '_static/custom_mathjax_config.js', |
| 406 |
}, |
| 407 |
) |
| 408 |
def test_mathjax_config_path_config(app: SphinxTestApp) -> None: |
| 409 |
app.build(force_all=True) |
| 410 |
|
| 411 |
content = (app.outdir / 'index.html').read_text(encoding='utf8') |
| 412 |
assert MATHJAX_URL in content |
| 413 |
assert f'<script defer="defer" src="{MATHJAX_URL}">' in content |
| 414 |
assert ( |
| 415 |
'<script>window.MathJax = {"extensions": ["tex2jax.js"]}\n</script>' |
| 416 |
) in content |
| 417 |
|
| 418 |
|
| 419 |
@pytest.mark.sphinx( |
| 420 |
'html', |
| 421 |
testroot='ext-math', |
| 422 |
confoverrides={ |
| 423 |
'extensions': ['sphinx.ext.mathjax'], |
| 424 |
'mathjax2_config': {'extensions': ['tex2jax.js']}, |
| 425 |
'mathjax_path': FAKE_MATHJAX_URL, |
| 426 |
}, |
| 427 |
) |
| 428 |
def test_mathjax2_config(app: SphinxTestApp) -> None: |
| 429 |
app.build(force_all=True) |
| 430 |
|
| 431 |
content = (app.outdir / 'index.html').read_text(encoding='utf8') |
| 432 |
assert f'<script async="async" src="{FAKE_MATHJAX_URL}">' in content |
| 433 |
assert ( |
| 434 |
'<script type="text/x-mathjax-config">' |
| 435 |
'MathJax.Hub.Config({"extensions": ["tex2jax.js"]})' |
| 436 |
'</script>' |
| 437 |
) in content |
| 438 |
|
| 439 |
|
| 440 |
@pytest.mark.sphinx( |
| 441 |
'html', |
| 442 |
testroot='ext-math', |
| 443 |
confoverrides={ |
| 444 |
'extensions': ['sphinx.ext.mathjax'], |
| 445 |
'mathjax_options': {'async': 'async'}, |
| 446 |
'mathjax4_config': {'extensions': ['tex2jax.js']}, |
| 447 |
}, |
| 448 |
) |
| 449 |
def test_mathjax_options_async_for_mathjax4(app: SphinxTestApp) -> None: |
| 450 |
app.build(force_all=True) |
| 451 |
|
| 452 |
content = (app.outdir / 'index.html').read_text(encoding='utf8') |
| 453 |
assert MATHJAX_URL in content |
| 454 |
assert '<script async="async" src="%s">' % MATHJAX_URL in content |
| 455 |
|
| 456 |
|
| 457 |
@pytest.mark.sphinx( |
| 458 |
'html', |
| 459 |
testroot='ext-math', |
| 460 |
confoverrides={ |
| 461 |
'extensions': ['sphinx.ext.mathjax'], |
| 462 |
'mathjax_options': {'defer': 'defer'}, |
| 463 |
'mathjax2_config': {'extensions': ['tex2jax.js']}, |
| 464 |
}, |
| 465 |
) |
| 466 |
def test_mathjax_options_defer_for_mathjax2(app: SphinxTestApp) -> None: |
| 467 |
app.build(force_all=True) |
| 468 |
|
| 469 |
content = (app.outdir / 'index.html').read_text(encoding='utf8') |
| 470 |
assert '<script defer="defer" src="%s">' % MATHJAX_URL in content |
| 471 |
|
| 472 |
|
| 473 |
@pytest.mark.sphinx( |
| 474 |
'html', |
| 475 |
testroot='ext-math', |
| 476 |
confoverrides={ |
| 477 |
'extensions': ['sphinx.ext.mathjax'], |
| 478 |
'mathjax_path': 'MathJax.js', |
| 479 |
}, |
| 480 |
) |
| 481 |
def test_mathjax_path(app: SphinxTestApp) -> None: |
| 482 |
app.build(force_all=True) |
| 483 |
|
| 484 |
content = (app.outdir / 'index.html').read_text(encoding='utf8') |
| 485 |
assert '<script defer="defer" src="_static/MathJax.js"></script>' in content |
| 486 |
|
| 487 |
|
| 488 |
@pytest.mark.sphinx( |
| 489 |
'html', |
| 490 |
testroot='ext-math', |
| 491 |
confoverrides={ |
| 492 |
'extensions': ['sphinx.ext.mathjax'], |
| 493 |
'mathjax_path': 'MathJax.js?config=scipy-mathjax', |
| 494 |
}, |
| 495 |
) |
| 496 |
def test_mathjax_path_config(app: SphinxTestApp) -> None: |
| 497 |
app.build(force_all=True) |
| 498 |
|
| 499 |
content = (app.outdir / 'index.html').read_text(encoding='utf8') |
| 500 |
assert ( |
| 501 |
'<script defer="defer" src="_static/MathJax.js?config=scipy-mathjax"></script>' |
| 502 |
) in content |
| 503 |
|
| 504 |
|
| 505 |
@pytest.mark.sphinx( |
| 506 |
'html', |
| 507 |
testroot='ext-math', |
| 508 |
confoverrides={'extensions': ['sphinx.ext.mathjax']}, |
| 509 |
) |
| 510 |
def test_mathjax_is_installed_only_if_document_having_math(app: SphinxTestApp) -> None: |
| 511 |
app.build(force_all=True) |
| 512 |
|
| 513 |
content = (app.outdir / 'index.html').read_text(encoding='utf8') |
| 514 |
assert MATHJAX_URL in content |
| 515 |
|
| 516 |
content = (app.outdir / 'nomath.html').read_text(encoding='utf8') |
| 517 |
assert MATHJAX_URL not in content |
| 518 |
|
| 519 |
|
| 520 |
@pytest.mark.sphinx( |
| 521 |
'html', |
| 522 |
testroot='basic', |
| 523 |
confoverrides={'extensions': ['sphinx.ext.mathjax']}, |
| 524 |
) |
| 525 |
def test_mathjax_is_not_installed_if_no_equations(app: SphinxTestApp) -> None: |
| 526 |
app.build(force_all=True) |
| 527 |
|
| 528 |
content = (app.outdir / 'index.html').read_text(encoding='utf8') |
| 529 |
assert 'MathJax.js' not in content |
| 530 |
assert MATHJAX_URL not in content |
| 531 |
|
| 532 |
|
| 533 |
@pytest.mark.sphinx( |
| 534 |
'html', |
| 535 |
testroot='ext-math', |
| 536 |
confoverrides={'extensions': ['sphinx.ext.mathjax']}, |
| 537 |
) |
| 538 |
def test_mathjax_is_installed_if_no_equations_when_forced(app: SphinxTestApp) -> None: |
| 539 |
app.set_html_assets_policy('always') |
| 540 |
app.build(force_all=True) |
| 541 |
|
| 542 |
content = (app.outdir / 'index.html').read_text(encoding='utf8') |
| 543 |
assert MATHJAX_URL in content |
| 544 |
|
| 545 |
content = (app.outdir / 'nomath.html').read_text(encoding='utf8') |
| 546 |
assert MATHJAX_URL in content |
| 547 |
|
| 548 |
|
| 549 |
@pytest.mark.sphinx( |
| 550 |
'html', |
| 551 |
testroot='ext-math-include', |
| 552 |
confoverrides={'extensions': ['sphinx.ext.mathjax']}, |
| 553 |
) |
| 554 |
def test_mathjax_is_installed_if_included_file_has_equations( |
| 555 |
app: SphinxTestApp, |
| 556 |
) -> None: |
| 557 |
app.build(force_all=True) |
| 558 |
|
| 559 |
# no real equations at the rst level, but includes "included" |
| 560 |
content = (app.outdir / 'index.html').read_text(encoding='utf8') |
| 561 |
assert MATHJAX_URL in content |
| 562 |
|
| 563 |
# no real equations at the rst level, but includes "math.rst" |
| 564 |
content = (app.outdir / 'included.html').read_text(encoding='utf8') |
| 565 |
assert MATHJAX_URL in content |
| 566 |
|
| 567 |
content = (app.outdir / 'math.html').read_text(encoding='utf8') |
| 568 |
assert MATHJAX_URL in content |
| 569 |
|
| 570 |
|
| 571 |
@pytest.mark.sphinx( |
| 572 |
'singlehtml', |
| 573 |
testroot='ext-math', |
| 574 |
confoverrides={'extensions': ['sphinx.ext.mathjax']}, |
| 575 |
) |
| 576 |
def test_mathjax_is_installed_only_if_document_having_math_singlehtml( |
| 577 |
app: SphinxTestApp, |
| 578 |
) -> None: |
| 579 |
app.build(force_all=True) |
| 580 |
|
| 581 |
content = (app.outdir / 'index.html').read_text(encoding='utf8') |
| 582 |
assert MATHJAX_URL in content |
| 583 |
|
| 584 |
|
| 585 |
@pytest.mark.sphinx( |
| 586 |
'singlehtml', |
| 587 |
testroot='basic', |
| 588 |
confoverrides={'extensions': ['sphinx.ext.mathjax']}, |
| 589 |
) |
| 590 |
def test_mathjax_is_not_installed_if_no_equations_singlehtml( |
| 591 |
app: SphinxTestApp, |
| 592 |
) -> None: |
| 593 |
app.build(force_all=True) |
| 594 |
|
| 595 |
content = (app.outdir / 'index.html').read_text(encoding='utf8') |
| 596 |
assert 'MathJax.js' not in content |
| 597 |
assert MATHJAX_URL not in content |
| 598 |
|
| 599 |
|
| 600 |
@pytest.mark.sphinx( |
| 601 |
'singlehtml', |
| 602 |
testroot='ext-math-include', |
| 603 |
confoverrides={'extensions': ['sphinx.ext.mathjax']}, |
| 604 |
) |
| 605 |
def test_mathjax_is_installed_if_included_file_has_equations_singlehtml( |
| 606 |
app: SphinxTestApp, |
| 607 |
) -> None: |
| 608 |
app.build(force_all=True) |
| 609 |
|
| 610 |
content = (app.outdir / 'index.html').read_text(encoding='utf8') |
| 611 |
assert MATHJAX_URL in content |
| 612 |
|
| 613 |
|
| 614 |
@pytest.mark.sphinx( |
| 615 |
'html', |
| 616 |
testroot='ext-math-duplicate-label', |
| 617 |
confoverrides={'extensions': ['sphinx.ext.mathjax'], 'show_warning_types': True}, |
| 618 |
) |
| 619 |
def test_duplicate_equation_label_warning_type(app: SphinxTestApp) -> None: |
| 620 |
"""Test that duplicate equation labels emit warnings with type ref.equation.""" |
| 621 |
app.build(force_all=True) |
| 622 |
|
| 623 |
from sphinx._cli.util.errors import strip_escape_sequences |
| 624 |
|
| 625 |
warnings = strip_escape_sequences(app.warning.getvalue()) |
| 626 |
assert 'WARNING: duplicate label of equation duplicated' in warnings |
| 627 |
assert '[ref.equation]' in warnings |