| 1 | from pathlib import Path |
| 2 | import runpy |
| 3 | import shutil |
| 4 | from tempfile import TemporaryDirectory |
| 5 | import unittest |
| 6 | import xml.etree.ElementTree as ET |
| 7 | |
| 8 | from native_format import native_characters |
| 9 | from native_xml import ns |
| 10 | |
| 11 | ROOT = Path(__file__).resolve().parent.parent |
| 12 | FIXTURE = ROOT / 'corpus/paragraph-format' |
| 13 | compare = runpy.run_path(str(ROOT / 'tools/verify-document.py'))['compare'] |
| 14 | IGNORED = {'objectID', 'lastModifiedTime', 'creationTime'} |
| 15 | |
| 16 | |
| 17 | def paragraphs(read): |
| 18 | for path in sorted(read.glob('page-*.xml')): |
| 19 | root = ET.parse(path).getroot() |
| 20 | if root.get('name') == 'Paragraph formatting': |
| 21 | outline, = root.findall('one:Outline', ns) |
| 22 | return [({k: v for k, v in oe.attrib.items() if k not in IGNORED}, oe) |
| 23 | for oe in outline.findall('one:OEChildren/one:OE', ns)], root |
| 24 | raise AssertionError('The paragraph formatting page is missing') |
| 25 | |
| 26 | |
| 27 | class ParagraphFormatTest(unittest.TestCase): |
| 28 | def test_native_controls_and_rust_paragraph_properties_reopen_natively(self): |
| 29 | with TemporaryDirectory() as temporary: |
| 30 | for notebook, read in [(FIXTURE / 'before', FIXTURE / 'before/read'), |
| 31 | (FIXTURE / 'candidate', FIXTURE / 'cold/read')]: |
| 32 | folder = Path(temporary) / notebook.name |
| 33 | shutil.copytree(read, folder / 'read') |
| 34 | compare(notebook, folder / 'read') |
| 35 | before, before_page = paragraphs(FIXTURE / 'before/read') |
| 36 | after, after_page = paragraphs(FIXTURE / 'cold/read') |
| 37 | self.assertEqual(len(before), 6) |
| 38 | self.assertEqual([attributes for attributes, _ in before], [ |
| 39 | {'alignment': 'left', 'quickStyleIndex': '0'}, |
| 40 | {'alignment': 'left', 'quickStyleIndex': '0'}, |
| 41 | {'alignment': 'center', 'spaceBefore': '12.0', 'spaceAfter': '6.0', 'spaceBetween': '20.0', 'quickStyleIndex': '0'}, |
| 42 | {'RTL': 'true', 'alignment': 'right', 'spaceBefore': '3.0', 'spaceAfter': '9.0', 'spaceBetween': '18.0', 'quickStyleIndex': '0'}, |
| 43 | {'alignment': 'left', 'spaceBefore': '288.0', 'spaceAfter': '144.0', 'quickStyleIndex': '1'}, |
| 44 | {'alignment': 'center', 'spaceBefore': '288.0', 'spaceAfter': '2.0', 'spaceBetween': '16.0', 'quickStyleIndex': '1'}, |
| 45 | ]) |
| 46 | for index, ((original, _), (attributes, _)) in enumerate(zip(before, after, strict=True)): |
| 47 | expected = {'alignment': 'center', 'spaceAfter': '2.0', 'spaceBetween': '16.0', |
| 48 | 'quickStyleIndex': original['quickStyleIndex']} |
| 49 | if original.get('RTL') == 'true': |
| 50 | expected['RTL'] = 'true' |
| 51 | self.assertEqual(attributes, expected, index) |
| 52 | self.assertEqual(native_characters(after_page, after_page.findall('one:Outline', ns)), |
| 53 | native_characters(before_page, before_page.findall('one:Outline', ns))) |
| 54 | |
| 55 | |
| 56 | if __name__ == '__main__': |
| 57 | unittest.main() |