| 1 | import json |
| 2 | from pathlib import Path |
| 3 | import re |
| 4 | import runpy |
| 5 | import shutil |
| 6 | from tempfile import TemporaryDirectory |
| 7 | import unittest |
| 8 | |
| 9 | ROOT = Path(__file__).resolve().parent.parent |
| 10 | FIXTURE = ROOT / 'corpus/math-edit' |
| 11 | compare = runpy.run_path(str(ROOT / 'tools/verify-document.py'))['compare'] |
| 12 | |
| 13 | |
| 14 | def exported_mathml(path): |
| 15 | text = path.read_text(encoding='utf-8-sig') |
| 16 | out = [] |
| 17 | for body in re.findall(r'<mml:math[^>]*>(.*?)</mml:math>', text, re.S): |
| 18 | out.append(re.sub(r'&#(\d+);', lambda m: chr(int(m.group(1))), body)) |
| 19 | return out |
| 20 | |
| 21 | |
| 22 | class MathEditTest(unittest.TestCase): |
| 23 | def test_equation_editor_expressions_export_as_mathml(self): |
| 24 | self.assertEqual(len(exported_mathml(FIXTURE / 'native-editor/read/page-000.xml')), 5) |
| 25 | self.assertEqual(len(exported_mathml(FIXTURE / 'native-editor-2/read/page-000.xml')), 9) |
| 26 | self.assertEqual(len(exported_mathml(FIXTURE / 'native-editor-3/read/page-000.xml')), 10) |
| 27 | self.assertEqual(len(exported_mathml(FIXTURE / 'native-editor-4/read/page-000.xml')), 26) |
| 28 | |
| 29 | def test_rust_written_equations_export_the_expected_mathml(self): |
| 30 | with TemporaryDirectory() as temporary: |
| 31 | read = Path(temporary) / 'read' |
| 32 | shutil.copytree(FIXTURE / 'written/cold/read', read) |
| 33 | compare(FIXTURE / 'written/candidate', read) |
| 34 | expected = json.loads((FIXTURE / 'written/candidate/expected-mathml.json').read_text()) |
| 35 | self.assertEqual(exported_mathml(FIXTURE / 'written/cold/read/page-000.xml'), expected) |
| 36 | self.assertTrue((FIXTURE / 'written/cold/read/page-000.png').exists()) |
| 37 | |
| 38 | |
| 39 | if __name__ == '__main__': |
| 40 | unittest.main() |