| 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/list-edit' |
| 13 | compare = runpy.run_path(str(ROOT / 'tools/verify-document.py'))['compare'] |
| 14 | |
| 15 | |
| 16 | def items(read): |
| 17 | page, = (ET.parse(path).getroot() for path in sorted(read.glob('page-*.xml'))) |
| 18 | outline, = page.findall('one:Outline', ns) |
| 19 | |
| 20 | def walk(element, depth): |
| 21 | marker = element.find('one:List', ns) |
| 22 | kind = None |
| 23 | if marker is not None: |
| 24 | child, = list(marker) |
| 25 | kind = (child.tag.split('}')[1], child.get('text')) |
| 26 | text = re.sub(r'<[^>]*>', '', element.find('one:T', ns).text or '') |
| 27 | yield depth, kind, text |
| 28 | for nested in element.findall('one:OEChildren/one:OE', ns): |
| 29 | yield from walk(nested, depth + 1) |
| 30 | |
| 31 | return [item for element in outline.findall('one:OEChildren/one:OE', ns) for item in walk(element, 0)] |
| 32 | |
| 33 | |
| 34 | class ListEditTest(unittest.TestCase): |
| 35 | def test_rust_bullets_numbering_and_restarts_render_natively(self): |
| 36 | with TemporaryDirectory() as temporary: |
| 37 | read = Path(temporary) / 'read' |
| 38 | shutil.copytree(FIXTURE / 'cold/read', read) |
| 39 | compare(FIXTURE / 'candidate', read) |
| 40 | self.assertEqual(items(FIXTURE / 'cold/read'), [ |
| 41 | (0, ('Bullet', None), 'Bullet item'), |
| 42 | (0, ('Number', 'I.'), 'First numbered'), |
| 43 | (0, ('Number', 'II.'), 'Second numbered'), |
| 44 | (1, ('Number', 'I.'), 'Nested numbered'), |
| 45 | (0, ('Number', 'III.'), 'Restarted at three'), |
| 46 | (1, None, 'Nested plain'), |
| 47 | (0, None, 'Plain again'), |
| 48 | ]) |
| 49 | |
| 50 | |
| 51 | if __name__ == '__main__': |
| 52 | unittest.main() |