"""Test math extensions.""" from __future__ import annotations import re import shutil import subprocess import warnings from typing import TYPE_CHECKING import pytest from docutils import nodes from sphinx.ext.mathjax import MATHJAX_URL from sphinx.testing.util import assert_node from tests.utils import extract_node if TYPE_CHECKING: from sphinx.testing.util import SphinxTestApp FAKE_MATHJAX_URL = 'https://example.org/fake-mathjax.js' def has_binary(binary: str) -> bool: try: subprocess.check_output([binary]) except FileNotFoundError: return False except OSError: pass return True @pytest.mark.skipif( not has_binary('dvipng'), reason='Requires dvipng" binary', ) @pytest.mark.sphinx( 'html', testroot='ext-math-simple', confoverrides={'extensions': ['sphinx.ext.imgmath']}, ) def test_imgmath_png(app: SphinxTestApp) -> None: app.build(force_all=True) if "LaTeX command 'latex' cannot be run" in app.warning.getvalue(): msg = 'LaTeX command "latex" is not available' raise pytest.skip.Exception(msg) if "dvipng command 'dvipng' cannot be run" in app.warning.getvalue(): msg = 'dvipng command "dvipng" is not available' raise pytest.skip.Exception(msg) content = (app.outdir / 'index.html').read_text(encoding='utf8') shutil.rmtree(app.outdir) html = ( r'
\s*
\s*
\s*\s*
Referencing equation Eq.1 and Eq.1.
' ) assert html in content @pytest.mark.sphinx( 'latex', testroot='ext-math', confoverrides={ 'extensions': ['sphinx.ext.mathjax'], 'math_eqref_format': 'Eq.{number}', }, ) def test_math_eqref_format_latex(app: SphinxTestApp) -> None: app.build(force_all=True) content = (app.outdir / 'projectnamenotset.tex').read_text(encoding='utf8') macro = ( r'Referencing equation Eq.\\ref{equation:math:foo} and ' r'Eq.\\ref{equation:math:foo}.' ) assert re.search(macro, content, re.DOTALL) @pytest.mark.sphinx( 'html', testroot='ext-math', confoverrides={ 'extensions': ['sphinx.ext.mathjax'], 'numfig': True, 'math_numfig': True, }, ) def test_mathjax_numfig_html(app: SphinxTestApp) -> None: app.build(force_all=True) content = (app.outdir / 'math.html').read_text(encoding='utf8') html = ( 'Referencing equation (1.1) and ' '(1.1).
' ) assert html in content @pytest.mark.sphinx( 'html', testroot='ext-math', confoverrides={ 'extensions': ['sphinx.ext.mathjax'], 'numfig': True, 'math_numfig': True, 'math_numsep': '-', }, ) def test_mathjax_numsep_html(app: SphinxTestApp) -> None: app.build(force_all=True) content = (app.outdir / 'math.html').read_text(encoding='utf8') html = ( 'Referencing equation (1-1) and ' '(1-1).
' ) assert html in content @pytest.mark.usefixtures('_http_teapot') @pytest.mark.sphinx( 'html', testroot='ext-math', confoverrides={ 'extensions': ['sphinx.ext.imgmath'], 'numfig': True, 'numfig_secnum_depth': 0, 'math_numfig': True, }, ) def test_imgmath_numfig_html(app: SphinxTestApp) -> None: app.build(force_all=True) content = (app.outdir / 'page.html').read_text(encoding='utf8') html = '(3)Referencing equations (1) and ' '(3).' ) assert html in content @pytest.mark.sphinx('dummy', testroot='ext-math-compat') def test_math_compat(app: SphinxTestApp) -> None: with warnings.catch_warnings(record=True): app.build(force_all=True) doctree = app.env.get_and_resolve_doctree('index', app.builder, tags=app.tags) assert_node( doctree, [ nodes.document, nodes.section, ( nodes.title, [nodes.section, (nodes.title, nodes.paragraph)], nodes.section, ), ], ) assert_node( extract_node(doctree, 0, 1, 1), ( 'Inline: ', [nodes.math, 'E=mc^2'], '\nInline my math: ', [nodes.math, 'E = mc^2'], ), ) assert_node( extract_node(doctree, 0, 2), ( [nodes.title, 'block'], [nodes.math_block, 'a^2+b^2=c^2\n\n'], [nodes.paragraph, 'Second math'], [nodes.math_block, 'e^{i\\pi}+1=0\n\n'], [nodes.paragraph, 'Multi math equations'], [nodes.math_block, 'E = mc^2'], ), ) @pytest.mark.sphinx( 'html', testroot='ext-math', confoverrides={ 'extensions': ['sphinx.ext.mathjax'], 'mathjax4_config': {'extensions': ['tex2jax.js']}, }, ) def test_mathjax4_config(app: SphinxTestApp) -> None: app.build(force_all=True) content = (app.outdir / 'index.html').read_text(encoding='utf8') assert MATHJAX_URL in content assert '' in content @pytest.mark.sphinx( 'html', testroot='ext-math', confoverrides={ 'extensions': ['sphinx.ext.mathjax'], 'mathjax3_config': {'extensions': ['tex2jax.js']}, 'mathjax_path': FAKE_MATHJAX_URL, }, ) def test_mathjax3_config(app: SphinxTestApp) -> None: app.build(force_all=True) content = (app.outdir / 'index.html').read_text(encoding='utf8') assert FAKE_MATHJAX_URL in content assert f'' in content @pytest.mark.sphinx( 'html', testroot='ext-math', confoverrides={ 'extensions': ['sphinx.ext.mathjax'], 'mathjax_config_path': '_static/custom_mathjax_config.js', }, ) def test_mathjax_config_path_config(app: SphinxTestApp) -> None: app.build(force_all=True) content = (app.outdir / 'index.html').read_text(encoding='utf8') assert MATHJAX_URL in content assert f'' ) in content @pytest.mark.sphinx( 'html', testroot='ext-math', confoverrides={ 'extensions': ['sphinx.ext.mathjax'], 'mathjax2_config': {'extensions': ['tex2jax.js']}, 'mathjax_path': FAKE_MATHJAX_URL, }, ) def test_mathjax2_config(app: SphinxTestApp) -> None: app.build(force_all=True) content = (app.outdir / 'index.html').read_text(encoding='utf8') assert f'' ) in content @pytest.mark.sphinx( 'html', testroot='ext-math', confoverrides={ 'extensions': ['sphinx.ext.mathjax'], 'mathjax_options': {'async': 'async'}, 'mathjax4_config': {'extensions': ['tex2jax.js']}, }, ) def test_mathjax_options_async_for_mathjax4(app: SphinxTestApp) -> None: app.build(force_all=True) content = (app.outdir / 'index.html').read_text(encoding='utf8') assert MATHJAX_URL in content assert '' in content @pytest.mark.sphinx( 'html', testroot='ext-math', confoverrides={ 'extensions': ['sphinx.ext.mathjax'], 'mathjax_path': 'MathJax.js?config=scipy-mathjax', }, ) def test_mathjax_path_config(app: SphinxTestApp) -> None: app.build(force_all=True) content = (app.outdir / 'index.html').read_text(encoding='utf8') assert ( '' ) in content @pytest.mark.sphinx( 'html', testroot='ext-math', confoverrides={'extensions': ['sphinx.ext.mathjax']}, ) def test_mathjax_is_installed_only_if_document_having_math(app: SphinxTestApp) -> None: app.build(force_all=True) content = (app.outdir / 'index.html').read_text(encoding='utf8') assert MATHJAX_URL in content content = (app.outdir / 'nomath.html').read_text(encoding='utf8') assert MATHJAX_URL not in content @pytest.mark.sphinx( 'html', testroot='basic', confoverrides={'extensions': ['sphinx.ext.mathjax']}, ) def test_mathjax_is_not_installed_if_no_equations(app: SphinxTestApp) -> None: app.build(force_all=True) content = (app.outdir / 'index.html').read_text(encoding='utf8') assert 'MathJax.js' not in content assert MATHJAX_URL not in content @pytest.mark.sphinx( 'html', testroot='ext-math', confoverrides={'extensions': ['sphinx.ext.mathjax']}, ) def test_mathjax_is_installed_if_no_equations_when_forced(app: SphinxTestApp) -> None: app.set_html_assets_policy('always') app.build(force_all=True) content = (app.outdir / 'index.html').read_text(encoding='utf8') assert MATHJAX_URL in content content = (app.outdir / 'nomath.html').read_text(encoding='utf8') assert MATHJAX_URL in content @pytest.mark.sphinx( 'html', testroot='ext-math-include', confoverrides={'extensions': ['sphinx.ext.mathjax']}, ) def test_mathjax_is_installed_if_included_file_has_equations( app: SphinxTestApp, ) -> None: app.build(force_all=True) # no real equations at the rst level, but includes "included" content = (app.outdir / 'index.html').read_text(encoding='utf8') assert MATHJAX_URL in content # no real equations at the rst level, but includes "math.rst" content = (app.outdir / 'included.html').read_text(encoding='utf8') assert MATHJAX_URL in content content = (app.outdir / 'math.html').read_text(encoding='utf8') assert MATHJAX_URL in content @pytest.mark.sphinx( 'singlehtml', testroot='ext-math', confoverrides={'extensions': ['sphinx.ext.mathjax']}, ) def test_mathjax_is_installed_only_if_document_having_math_singlehtml( app: SphinxTestApp, ) -> None: app.build(force_all=True) content = (app.outdir / 'index.html').read_text(encoding='utf8') assert MATHJAX_URL in content @pytest.mark.sphinx( 'singlehtml', testroot='basic', confoverrides={'extensions': ['sphinx.ext.mathjax']}, ) def test_mathjax_is_not_installed_if_no_equations_singlehtml( app: SphinxTestApp, ) -> None: app.build(force_all=True) content = (app.outdir / 'index.html').read_text(encoding='utf8') assert 'MathJax.js' not in content assert MATHJAX_URL not in content @pytest.mark.sphinx( 'singlehtml', testroot='ext-math-include', confoverrides={'extensions': ['sphinx.ext.mathjax']}, ) def test_mathjax_is_installed_if_included_file_has_equations_singlehtml( app: SphinxTestApp, ) -> None: app.build(force_all=True) content = (app.outdir / 'index.html').read_text(encoding='utf8') assert MATHJAX_URL in content @pytest.mark.sphinx( 'html', testroot='ext-math-duplicate-label', confoverrides={'extensions': ['sphinx.ext.mathjax'], 'show_warning_types': True}, ) def test_duplicate_equation_label_warning_type(app: SphinxTestApp) -> None: """Test that duplicate equation labels emit warnings with type ref.equation.""" app.build(force_all=True) from sphinx._cli.util.errors import strip_escape_sequences warnings = strip_escape_sequences(app.warning.getvalue()) assert 'WARNING: duplicate label of equation duplicated' in warnings assert '[ref.equation]' in warnings