| 1 | import unittest |
| 2 | import xml.etree.ElementTree as ET |
| 3 | |
| 4 | from compare_page import compare |
| 5 | |
| 6 | |
| 7 | class PageComparisonTests(unittest.TestCase): |
| 8 | def test_matches_reordered_outlines_and_preserves_html_whitespace(self): |
| 9 | native = ET.fromstring('''<Page xmlns="http://schemas.microsoft.com/office/onenote/2010/onenote" name="Page"> |
| 10 | <Outline objectID="second"><Position x="70" y="30"/><Size width="90" height="14"/> |
| 11 | <OEChildren><OE><T>B</T></OE></OEChildren></Outline> |
| 12 | <Outline objectID="first"><Position x="36" y="20"/><Size width="100" height="28"/> |
| 13 | <OEChildren><OE><T>&lt;b&gt;A&amp;nbsp;&lt;/b&gt;&lt;br/&gt;B</T></OE><OE><T/></OE></OEChildren></Outline> |
| 14 | </Page>''') |
| 15 | probe = {'title': 'Page', 'margin_origin': [36.5, 14.4], 'measurement': 'Raw paragraphs', |
| 16 | 'objects': [ |
| 17 | {'kind': 'outline', 'is_title': False, 'unsupported': [], 'id': 'a', |
| 18 | 'layout': {'x': 36.5, 'y': 20, 'max_width': 100}, |
| 19 | 'paragraphs': [{'visible_text': text, 'unsupported': [], 'height': 14, |
| 20 | 'parent': None, 'lists': []} for text in ['A\u00a0\nB', '']]}, |
| 21 | {'kind': 'outline', 'is_title': False, 'unsupported': [], 'id': 'b', |
| 22 | 'layout': {'x': 70.5, 'y': 30, 'max_width': 90}, |
| 23 | 'paragraphs': [{'visible_text': 'B', 'unsupported': [], 'height': 14, |
| 24 | 'parent': None, 'lists': []}]}, |
| 25 | ]} |
| 26 | result = compare(probe, native) |
| 27 | self.assertEqual([o['native_id'] for o in result['outlines']], ['first', 'second']) |
| 28 | self.assertEqual(result['outlines'][0]['height_residual'], 0) |
| 29 | self.assertEqual(result['outlines'][0]['inferred_native_margin_origin'], [36, 14.4]) |
| 30 | probe['objects'][0]['paragraphs'][0]['visible_text'] = 'A \nB' |
| 31 | with self.assertRaises(ValueError): |
| 32 | compare(probe, native) |
| 33 | |
| 34 | def test_native_empty_nbsp_requires_explicit_normalization_and_is_reported(self): |
| 35 | native = ET.fromstring('''<Page xmlns="http://schemas.microsoft.com/office/onenote/2010/onenote" name="Page"> |
| 36 | <Outline><Position x="0" y="0"/><Size width="100" height="14"/> |
| 37 | <OEChildren><OE><T/></OE></OEChildren></Outline> |
| 38 | </Page>''') |
| 39 | probe = {'title': 'Page', 'margin_origin': [0, 0], 'measurement': 'Raw paragraphs', 'objects': [ |
| 40 | {'kind': 'outline', 'is_title': False, 'unsupported': [], 'id': 'a', |
| 41 | 'layout': {'x': 0, 'y': 0, 'max_width': 100}, |
| 42 | 'paragraphs': [{'visible_text': '\u00a0', 'unsupported': [], 'height': 14, |
| 43 | 'parent': None, 'lists': []}]}]} |
| 44 | with self.assertRaises(ValueError): |
| 45 | compare(probe, native) |
| 46 | result = compare(probe, native, allow_native_empty_nbsp=True)['outlines'][0] |
| 47 | self.assertFalse(result['text_matches_exactly']) |
| 48 | self.assertEqual(result['native_empty_nbsp_paragraphs'], [0]) |
| 49 | |
| 50 | def test_rejects_ambiguous_identical_content(self): |
| 51 | native = ET.fromstring('''<Page xmlns="http://schemas.microsoft.com/office/onenote/2010/onenote" name="Page"> |
| 52 | <Outline><OEChildren><OE><T>same</T></OE></OEChildren></Outline> |
| 53 | <Outline><OEChildren><OE><T>same</T></OE></OEChildren></Outline> |
| 54 | </Page>''') |
| 55 | probe = {'title': 'Page', 'objects': [ |
| 56 | {'kind': 'outline', 'is_title': False, 'unsupported': [], 'id': 'a', |
| 57 | 'paragraphs': [{'visible_text': 'same', 'unsupported': []}]}]} |
| 58 | with self.assertRaises(ValueError): |
| 59 | compare(probe, native) |
| 60 | |
| 61 | |
| 62 | if __name__ == '__main__': |
| 63 | unittest.main() |