| 1 | import html |
| 2 | from pathlib import Path |
| 3 | import re |
| 4 | import runpy |
| 5 | import shutil |
| 6 | from tempfile import TemporaryDirectory |
| 7 | import unittest |
| 8 | import xml.etree.ElementTree as ET |
| 9 | |
| 10 | from native_xml import ns |
| 11 | |
| 12 | ROOT = Path(__file__).resolve().parent.parent |
| 13 | FIXTURE = ROOT / 'corpus/table-edit' |
| 14 | compare = runpy.run_path(str(ROOT / 'tools/verify-document.py'))['compare'] |
| 15 | |
| 16 | |
| 17 | def tables(read, title): |
| 18 | for path in sorted(read.glob('page-*.xml')): |
| 19 | page = ET.parse(path).getroot() |
| 20 | if page.get('name') != title: |
| 21 | continue |
| 22 | out = [] |
| 23 | for table in page.iter('{%s}Table' % ns['one']): |
| 24 | columns = [(column.get('width'), column.get('isLocked')) for column in table.find('one:Columns', ns)] |
| 25 | rows = [[[html.unescape(re.sub(r'<[^>]*>', '', text.text or '')) for text in cell.findall('.//one:T', ns)] |
| 26 | for cell in row.findall('one:Cell', ns)] for row in table.findall('one:Row', ns)] |
| 27 | out.append((table.get('bordersVisible'), columns, rows)) |
| 28 | return out |
| 29 | raise AssertionError(title) |
| 30 | |
| 31 | |
| 32 | class TableEditTest(unittest.TestCase): |
| 33 | def check(self, name): |
| 34 | with TemporaryDirectory() as temporary: |
| 35 | read = Path(temporary) / 'read' |
| 36 | shutil.copytree(FIXTURE / name / 'cold/read', read) |
| 37 | compare(FIXTURE / name / 'candidate', read) |
| 38 | |
| 39 | def test_a_rust_table_renders_natively(self): |
| 40 | self.check('created') |
| 41 | self.assertEqual(tables(FIXTURE / 'created/cold/read', 'Before the table'), [ |
| 42 | ('true', [('144.0', 'true'), ('216.0', 'true')], |
| 43 | [[['Name'], ['Value']], [['Rust 🦀'], ['Written natively']]]), |
| 44 | ]) |
| 45 | |
| 46 | def test_rows_and_columns_added_to_a_native_table_render_natively(self): |
| 47 | self.check('edited') |
| 48 | (borders, columns, rows), = tables(FIXTURE / 'edited/cold/read', 'Delete cell subtree') |
| 49 | self.assertEqual(borders, 'true') |
| 50 | self.assertEqual(columns[:2], [('240.0', 'true'), ('180.0', 'true')]) |
| 51 | self.assertEqual(len(columns), 3) |
| 52 | self.assertIsNone(columns[2][1]) |
| 53 | self.assertEqual(rows[0][2], ['Third column']) |
| 54 | self.assertEqual(rows[1], [['Second row'], ['Second row, second column'], ['Second row, third column']]) |
| 55 | |
| 56 | def test_a_table_nested_in_a_native_cell_renders_natively(self): |
| 57 | self.check('nested') |
| 58 | outer, inner = tables(FIXTURE / 'nested/cold/read', 'Delete cell subtree') |
| 59 | self.assertEqual(inner, ('true', [('72.0', 'true')], [[['Inner one']], [['Inner two']]])) |
| 60 | self.assertEqual(outer[2][0][1], ['Other cell', 'Inner one', 'Inner two']) |
| 61 | |
| 62 | |
| 63 | if __name__ == '__main__': |
| 64 | unittest.main() |