| 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/object-outline' |
| 13 | compare = runpy.run_path(str(ROOT / 'tools/verify-document.py'))['compare'] |
| 14 | |
| 15 | |
| 16 | def bodies(read): |
| 17 | """Each page's body by title: each paragraph's content kind, with its text.""" |
| 18 | result = {} |
| 19 | for path in sorted(read.glob('page-*.xml')): |
| 20 | page = ET.parse(path).getroot() |
| 21 | title = page.find('one:Title/one:OE/one:T', ns) |
| 22 | body = page.find('one:Outline/one:OEChildren', ns) |
| 23 | if title is None or body is None: |
| 24 | continue |
| 25 | result[title.text] = [ |
| 26 | next(child.tag.rsplit('}', 1)[1] for child in oe |
| 27 | if child.tag.rsplit('}', 1)[1] in ('T', 'Image', 'InsertedFile')) |
| 28 | + (':' + re.sub(r'<[^>]*>', '', oe.find('one:T', ns).text or '') if oe.find('one:T', ns) is not None else '') |
| 29 | for oe in body.findall('one:OE', ns)] |
| 30 | return result |
| 31 | |
| 32 | |
| 33 | class ObjectOutlineTest(unittest.TestCase): |
| 34 | def test_typing_beside_an_outlines_only_object_reopens_as_onenote_made_it(self): |
| 35 | with TemporaryDirectory() as temporary: |
| 36 | read = Path(temporary) / 'read' |
| 37 | shutil.copytree(FIXTURE / 'cold/read', read) |
| 38 | compare(FIXTURE / 'candidate', read) |
| 39 | self.assertEqual(bodies(FIXTURE / 'native/read'), |
| 40 | {'File alone': ['InsertedFile'], 'Picture alone': ['Image']}) |
| 41 | onenote = bodies(FIXTURE / 'keys/read') |
| 42 | self.assertEqual(onenote, {'File alone': ['InsertedFile', 'T:abc'], 'Picture alone': ['Image', 'T:abc']}) |
| 43 | self.assertEqual(bodies(FIXTURE / 'cold/read'), onenote) |
| 44 | |
| 45 | |
| 46 | if __name__ == '__main__': |
| 47 | unittest.main() |