| 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-select' |
| 10 | compare = runpy.run_path(str(ROOT / 'tools/verify-document.py'))['compare'] |
| 11 | |
| 12 | |
| 13 | def mathml(path): |
| 14 | """Each equation's MathML in a COM read, trailing spaces dropped.""" |
| 15 | xml = path.read_text(encoding='utf-8-sig') |
| 16 | return [re.sub(r'(<mml:mo>&nbsp;</mml:mo>)+</mml:math>', '</mml:math>', m).replace(' display="block"', '') |
| 17 | for m in re.findall(r'<mml:math[^!]*?</mml:math>', xml)] |
| 18 | |
| 19 | |
| 20 | class EquationSelectTest(unittest.TestCase): |
| 21 | def test_edits_into_equation_objects_reopen_as_onenote_made_them(self): |
| 22 | with TemporaryDirectory() as temporary: |
| 23 | read = Path(temporary) / 'read' |
| 24 | shutil.copytree(FIXTURE / 'cold/read', read) |
| 25 | compare(FIXTURE / 'candidate', read) |
| 26 | onenote = mathml(FIXTURE / 'lab/after.xml') |
| 27 | cold = mathml(FIXTURE / 'cold/read/page-000.xml') |
| 28 | # The lab page ends with "x" typed over a placeholder, which the candidate has no |
| 29 | # placeholder left for. |
| 30 | self.assertEqual(onenote[-1], '<mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML"><mml:mi>x</mml:mi></mml:math>') |
| 31 | self.assertEqual(cold, onenote[:-1]) |
| 32 | before = mathml(FIXTURE / 'lab/before.xml') |
| 33 | self.assertIn('Type&nbsp;equation&nbsp;here.', ''.join(before)) |
| 34 | self.assertTrue(any('<mml:msqrt>' in m for m in before)) |
| 35 | self.assertFalse(any('<mml:msqrt>' in m or 'Type&nbsp;equation' in m for m in cold)) |
| 36 | self.assertTrue(any('<mml:mtable>' in m and 'lim' in m for m in cold)) |
| 37 | |
| 38 | |
| 39 | if __name__ == '__main__': |
| 40 | unittest.main() |