Debian Codegraph @

test/test_utils/test_math/test_tex2mathml_extern.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # :Copyright: © 2024 Günter Milde.
3 # :License: Released under the terms of the `2-Clause BSD license`_, in short:
4 #
5 # Copying and distribution of this file, with or without modification,
6 # are permitted in any medium without royalty provided the copyright
7 # notice and this notice are preserved.
8 # This file is offered as-is, without any warranty.
9 #
10 # .. _2-Clause BSD license: https://opensource.org/licenses/BSD-2-Clause
11
12 """Create output samples for TeX to MathML conversion.
13
14 Allows to compare the results of the various converters and Docutils' built-in
15 convert in web browsers.
16 """
17
18 from pathlib import Path
19 import sys
20 import unittest
21
22 # Testing the conversion with external converters requires additional
23 # ressources (latexml, blahtexml, ttm, pandoc) and is SLOW
24 # (ca. 27s without testing "latexml" and 13 min with "latexml").
25 # Therefore, tests are skipped unless run as stand-alone module:
26
27 if __name__ != '__main__':
28 raise unittest.SkipTest('run stand-alone to test external math converters')
29 else:
30 # prepend the "docutils root" to the Python library path
31 # so we import the local `docutils` package.
32 sys.path.insert(0, str(Path(__file__).resolve().parents[3]))
33
34
35 from docutils.core import publish_parts, publish_file
36 from docutils.utils import relative_path
37
38 from test.test_functional import compare_output
39
40 TEST_ROOT = Path(__file__).parents[2]
41 DOCS = TEST_ROOT.parent / 'docs'
42 FUNCTIONAL = TEST_ROOT / 'functional'
43 EXPECTED = FUNCTIONAL / 'expected'
44 INPUT = FUNCTIONAL / 'input'
45 OUTPUT = FUNCTIONAL / 'output'
46
47
48 buggy_sample = r"""
49 There is ":math:`\lambda\omega\tau\varsigma`" to math.
50
51 ====================== ========== =======
52 Das ist nicht lustig. Dafür das hier
53
54 .. math:: So \isses
55 ====================== ========== =======
56
57
58 Das ist :math:`\ein` Fehler.
59
60 .. math:: \int_{-\infty} 3 \sin x} \phy \, d\gamma
61
62 .. sidebar:: nebenbemerkung
63
64 noch was :math:`\sqrt[2]`
65
66 .. math:: \sqrt[2]
67
68 2-zeilig mit align:
69
70 .. math::
71 s_i & = 3 \\
72 s_j + 3 &
73
74 """
75
76
77 math_options = [('mathml', ''),
78 ('mathml', 'ttm'),
79 ('mathml', 'blahtexml'),
80 ('mathml', 'pandoc'),
81 # ('mathml', 'latexml'), # VERY slow (up to 20 min)
82 ]
83
84
85 class MathMLConverterTestCase(unittest.TestCase):
86 """
87 Functional tests of TeX to MathML converters.
88
89 PROVISIONAL: This class tests a provisional implementation
90 which is open to change without warning.
91 """
92 settings = {'_disable_config': True,
93 'input_encoding': 'utf-8', # skip auto-detection
94 'embed_stylesheet': False,
95 'warning_stream': '',
96 'report_level': 2, # warning
97 # 'report_level': 3, # error
98 # 'report_level': 4, # severe
99 }
100
101 def test_mathematics(self):
102 """Test converting "mathematics.rst" from the documentation."""
103
104 source_path = relative_path(None, DOCS/'ref'/'rst'/'mathematics.rst')
105
106 for math_output in math_options:
107 settings = {'math_output': math_output,
108 'stylesheet_path': 'minimal.css,responsive.css',
109 **self.settings}
110 out_file = f'mathematics_{"_".join(math_output).strip("_")}.html'
111 out_path = OUTPUT / out_file
112 expected_path = EXPECTED / out_file
113 output = publish_file(source_path=str(source_path),
114 destination_path=out_path.as_posix(),
115 writer='html5',
116 settings_overrides=settings)
117 with self.subTest(converter=math_output[1] or 'latex2mathml()'):
118 compare_output(output, out_path, expected_path)
119
120 def test_math_experiments(self):
121 """Convert experimental math sample."""
122
123 source_path = relative_path(None, INPUT/'data'/'math_experiments.rst')
124
125 for math_output in math_options:
126 settings = {'math_output': math_output, **self.settings}
127 out_file = f'math_experiments_{"_".join(math_output).strip("_")}.html' # noqa: E501
128 out_path = OUTPUT / out_file
129 expected_path = EXPECTED / out_file
130 output = publish_file(source_path=str(source_path),
131 destination_path=out_path.as_posix(),
132 writer='html5',
133 settings_overrides=settings)
134 with self.subTest(converter=math_output[1] or 'latex2mathml()'):
135 compare_output(output, out_path, expected_path)
136
137 def test_buggy_math(self):
138 """Test reporting conversion failure due to TeX-syntax errors."""
139
140 for math_output in math_options:
141 settings = {'math_output': math_output, **self.settings}
142 out_file = f'buggy_{"_".join(math_output).strip("_")}.html'
143 out_path = OUTPUT / out_file
144 expected_path = EXPECTED / out_file
145 preface = f'Test "math-output: {" ".join(math_output)}".\n\n'
146 parts = publish_parts(preface + buggy_sample,
147 'buggy-maths',
148 writer='html5',
149 settings_overrides=settings)
150 Path(out_path).write_text(parts['whole'])
151 with self.subTest(converter=math_output[1] or 'latex2mathml()'):
152 compare_output(parts['whole'], out_path, expected_path)
153
154
155 if __name__ == '__main__':
156 unittest.main()