| 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_xml import ns |
| 9 | |
| 10 | ROOT = Path(__file__).resolve().parent.parent |
| 11 | FIXTURE = ROOT / 'corpus/rule-lines' |
| 12 | compare = runpy.run_path(str(ROOT / 'tools/verify-document.py'))['compare'] |
| 13 | |
| 14 | BLUE = '#CAEBFD' |
| 15 | MARGIN = ('Margin', '#FF5050', None) |
| 16 | |
| 17 | |
| 18 | def rule_lines(capture, notebook): |
| 19 | """Each page's rule lines as OneNote's page XML reports them, by title, once the Rust |
| 20 | reading of `notebook` matches the capture.""" |
| 21 | with TemporaryDirectory() as temporary: |
| 22 | read = Path(temporary) / 'read' |
| 23 | shutil.copytree(capture / 'read', read) |
| 24 | compare(notebook, read) |
| 25 | pages = {} |
| 26 | for path in sorted((capture / 'read').glob('page-*.xml')): |
| 27 | page = ET.parse(path).getroot() |
| 28 | rules = page.find('one:PageSettings/one:RuleLines', ns) |
| 29 | lines = [(line.tag.rsplit('}', 1)[-1], line.get('color'), |
| 30 | None if line.get('spacing') is None else round(float(line.get('spacing')), 3)) |
| 31 | for line in rules] |
| 32 | pages[page.get('name')] = (rules.get('visible'), lines) |
| 33 | return pages |
| 34 | |
| 35 | |
| 36 | def ruled(spacing, color=BLUE): |
| 37 | return ('true', [('Horizontal', color, spacing), MARGIN]) |
| 38 | |
| 39 | |
| 40 | def grid(spacing, color=BLUE): |
| 41 | return ('true', [('Horizontal', color, spacing), ('Vertical', color, spacing)]) |
| 42 | |
| 43 | |
| 44 | NONE = ('false', []) |
| 45 | |
| 46 | |
| 47 | class RuleLinesTest(unittest.TestCase): |
| 48 | def test_onenote_rules_pages_as_its_menu_names_them(self): |
| 49 | pages = rule_lines(FIXTURE / 'native', FIXTURE / 'native/notebook') |
| 50 | self.assertEqual(pages, { |
| 51 | 'None': NONE, |
| 52 | 'Narrow': NONE, |
| 53 | 'College': ruled(23.76), |
| 54 | 'Standard': ruled(33.12), |
| 55 | 'Wide': ruled(46.8), |
| 56 | 'SmallGrid': grid(12.0), |
| 57 | 'MediumGrid': grid(28.346, '#E0C9FF'), |
| 58 | 'LargeGrid': grid(42.52), |
| 59 | 'VeryLargeGrid': grid(56.693), |
| 60 | 'ColorRed': ruled(33.12, '#FFD4D6'), |
| 61 | 'Hidden': ruled(33.12, '#FFFFFF'), |
| 62 | }) |
| 63 | |
| 64 | def test_rust_rule_lines_read_back_as_written(self): |
| 65 | pages = rule_lines(FIXTURE / 'cold', FIXTURE / 'candidate') |
| 66 | self.assertEqual(pages, { |
| 67 | 'None': ruled(46.8), |
| 68 | 'Narrow': NONE, |
| 69 | 'College': NONE, |
| 70 | 'Standard': grid(12.0), |
| 71 | 'Wide': ruled(46.8), |
| 72 | 'SmallGrid': grid(12.0), |
| 73 | 'MediumGrid': ruled(13.428, '#FFD4D6'), |
| 74 | 'LargeGrid': grid(42.52), |
| 75 | 'VeryLargeGrid': grid(56.693), |
| 76 | 'ColorRed': ruled(33.12, '#FFD4D6'), |
| 77 | 'Hidden': ruled(33.12, '#FFFFFF'), |
| 78 | 'Created': grid(56.693), |
| 79 | }) |
| 80 | |
| 81 | |
| 82 | if __name__ == '__main__': |
| 83 | unittest.main() |