| 1 | from pathlib import Path |
| 2 | import re |
| 3 | import runpy |
| 4 | import shutil |
| 5 | from tempfile import TemporaryDirectory |
| 6 | import unittest |
| 7 | |
| 8 | ROOT = Path(__file__).resolve().parent.parent |
| 9 | FIXTURE = ROOT / 'corpus/equation-join' |
| 10 | compare = runpy.run_path(str(ROOT / 'tools/verify-document.py'))['compare'] |
| 11 | |
| 12 | |
| 13 | def mathml(xml): |
| 14 | """Each equation's MathML in a COM read, trailing spaces dropped.""" |
| 15 | return [re.sub(r'(<mml:mo>&nbsp;</mml:mo>)+</mml:math>', '</mml:math>', m) |
| 16 | for m in re.findall(r'<mml:math[^!]*?</mml:math>', xml)] |
| 17 | |
| 18 | |
| 19 | class EquationJoinTest(unittest.TestCase): |
| 20 | def test_joined_equations_reopen_as_onenote_joined_them(self): |
| 21 | with TemporaryDirectory() as temporary: |
| 22 | read = Path(temporary) / 'read' |
| 23 | shutil.copytree(FIXTURE / 'cold/read', read) |
| 24 | compare(FIXTURE / 'candidate', read) |
| 25 | onenote = mathml((FIXTURE / 'lab/equation-seam.xml').read_text()) |
| 26 | cold = mathml(''.join(path.read_text(encoding='utf-8-sig') for path in sorted((FIXTURE / 'cold/read').glob('page-*.xml')))) |
| 27 | self.assertEqual(len(onenote), 1) |
| 28 | self.assertEqual([m.replace(' display="block"', '') for m in cold], |
| 29 | [m.replace(' display="block"', '') for m in onenote]) |
| 30 | |
| 31 | |
| 32 | if __name__ == '__main__': |
| 33 | unittest.main() |