| 1 | from pathlib import Path |
| 2 | import re |
| 3 | import runpy |
| 4 | import shutil |
| 5 | from tempfile import TemporaryDirectory |
| 6 | import unittest |
| 7 | import xml.etree.ElementTree as ET |
| 8 | |
| 9 | from native_xml import ns |
| 10 | |
| 11 | ROOT = Path(__file__).resolve().parent.parent |
| 12 | FIXTURE = ROOT / 'corpus/cross-container' |
| 13 | compare = runpy.run_path(str(ROOT / 'tools/verify-document.py'))['compare'] |
| 14 | |
| 15 | |
| 16 | def blocks(read): |
| 17 | """Each page's body by title: paragraph texts, and tables as rows of cell paragraph texts.""" |
| 18 | text = lambda node: re.sub(r'<[^>]*>', '', node.text or '') |
| 19 | result = {} |
| 20 | for path in sorted(read.glob('page-*.xml')): |
| 21 | page = ET.parse(path).getroot() |
| 22 | title = page.find('one:Title/one:OE/one:T', ns) |
| 23 | body = page.find('one:Outline/one:OEChildren', ns) |
| 24 | if title is None or body is None: |
| 25 | continue |
| 26 | result[title.text] = [ |
| 27 | [[[text(t) for t in cell.findall('one:OEChildren/one:OE/one:T', ns)] |
| 28 | for cell in row.findall('one:Cell', ns)] |
| 29 | for row in oe.find('one:Table', ns).findall('one:Row', ns)] |
| 30 | if oe.find('one:Table', ns) is not None else text(oe.find('one:T', ns)) |
| 31 | for oe in body.findall('one:OE', ns)] |
| 32 | return result |
| 33 | |
| 34 | |
| 35 | class CrossContainerTest(unittest.TestCase): |
| 36 | def test_deletions_across_a_tables_edge_reopen_as_onenote_made_them(self): |
| 37 | with TemporaryDirectory() as temporary: |
| 38 | read = Path(temporary) / 'read' |
| 39 | shutil.copytree(FIXTURE / 'cold/read', read) |
| 40 | compare(FIXTURE / 'candidate', read) |
| 41 | onenote = blocks(FIXTURE / 'keys/read') |
| 42 | cold = blocks(FIXTURE / 'cold/read') |
| 43 | self.assertEqual(len(cold), 4) |
| 44 | for title, body in cold.items(): |
| 45 | self.assertEqual(body, onenote[title], title) |
| 46 | |
| 47 | |
| 48 | if __name__ == '__main__': |
| 49 | unittest.main() |