1from importlib.util import find_spec
2from pathlib import Path
3import re
4import runpy
5import shutil
6import subprocess
7from tempfile import TemporaryDirectory
8import unittest
9import xml.etree.ElementTree as ET
10
11ROOT = Path(__file__).resolve().parent.parent
12compare = runpy.run_path(str(ROOT / 'tools/verify-document.py'))['compare']
13requires_pdfplumber = unittest.skipUnless(
14 find_spec('pdfplumber'), 'pdfplumber is not installed; tools/TESTING.md sets up the Python lane')
15
16
17class DocumentOracleTest(unittest.TestCase):
18 @requires_pdfplumber
19 def test_locked_table_width_requires_the_native_value(self):
20 fixture = ROOT / 'corpus/m6/native-structure-01'
21 with TemporaryDirectory() as temporary:
22 native = Path(temporary) / 'read'
23 shutil.copytree(fixture / 'read', native)
24 compare(fixture / 'notebook', native)
25 path = native / 'page-001.xml'
26 xml = ET.parse(path)
27 column = next(n for n in xml.getroot().iter()
28 if n.tag.endswith('}Column') and n.get('isLocked') == 'true')
29 column.set('width', str(float(column.get('width')) + 1))
30 xml.write(path, encoding='utf-8')
31 with self.assertRaisesRegex(AssertionError, 'Locked table column width differs'):
32 compare(fixture / 'notebook', native)
33
34 @requires_pdfplumber
35 def test_native_2010_does_not_render_the_documented_cell_shading_control(self):
36 import pdfplumber
37 fixture = ROOT / 'corpus/m6/cell-shading-control-01/read/page-001'
38 page = ET.parse(fixture.with_suffix('.xml')).getroot()
39 cells = [n for n in page.iter() if n.tag.endswith('}Cell')]
40 self.assertEqual(len(cells), 6)
41 self.assertTrue(all('shadingColor' not in n.attrib for n in cells))
42 with pdfplumber.open(fixture.with_suffix('.pdf')) as pdf:
43 table_page = pdf.pages[1]
44 self.assertIn('Cell 0,0', table_page.extract_text())
45 self.assertTrue(all(tuple(rect.get('non_stroking_color') or ()) != (1, 1, 0)
46 for rect in table_page.rects))
47
48 @requires_pdfplumber
49 def test_rust_saved_expansion_is_honored_by_a_fresh_native_cache(self):
50 import pdfplumber
51 fixture = ROOT / 'corpus/m6/rust-collapse-control-01'
52 pages = [(p, ET.parse(p).getroot()) for p in (fixture / 'read').glob('page-*.xml')]
53 path, page = next((p, r) for p, r in pages if r.get('name') == 'Paragraph controls')
54 parent, = [n for n in page.iter() if n.tag.endswith('}OE')
55 and any(c.tag.endswith('}T') and c.text == 'Collapsed parent' for c in n)]
56 self.assertIsNone(parent.get('collapsed'))
57 with pdfplumber.open(path.with_suffix('.pdf')) as pdf:
58 text = ''.join(p.extract_text() or '' for p in pdf.pages)
59 self.assertIn('Hidden child remains stored', text)
60 self.assertIn('Grandchild', text)
61
62 @requires_pdfplumber
63 def test_native_expansion_changes_the_view_without_changing_notebook_bytes(self):
64 import pdfplumber
65 original = ROOT / 'corpus/m6/native-features-01/notebook/Features.one'
66 capture = ROOT / 'corpus/m6/native-expanded-control-ui-01'
67 self.assertEqual(original.read_bytes(), (capture / 'notebook/Features.one').read_bytes())
68 for directory, collapsed in [('before-read', '1'), ('read', None)]:
69 candidates = [(p, ET.parse(p).getroot()) for p in (capture / directory).glob('page-*.xml')]
70 path, page = next((p, r) for p, r in candidates if r.get('name') == 'Paragraph controls')
71 parent, = [n for n in page.iter() if n.tag.endswith('}OE')
72 and any(c.tag.endswith('}T') and c.text == 'Collapsed parent' for c in n)]
73 self.assertEqual(parent.get('collapsed'), collapsed)
74 self.assertIn('Grandchild', ''.join(parent.itertext()))
75 with pdfplumber.open(path.with_suffix('.pdf')) as pdf:
76 text = ''.join(p.extract_text() or '' for p in pdf.pages)
77 self.assertNotIn('Grandchild', text)
78
79 def test_missing_or_empty_source_cannot_pass(self):
80 fixture = ROOT / 'corpus/m6/native-math-01/read'
81 with TemporaryDirectory() as temporary:
82 source = Path(temporary)
83 with self.assertRaises(FileNotFoundError):
84 compare(source / 'missing', fixture)
85 with self.assertRaisesRegex(AssertionError, 'No notebook sections'):
86 compare(source, fixture)
87
88 def test_equations_survive_a_fresh_native_cache(self):
89 captures = []
90 for fixture in ('native-math-01', 'native-math-cold-01'):
91 pages = [ET.parse(p).getroot() for p in (ROOT / 'corpus/m6' / fixture / 'read').glob('page-*.xml')]
92 page, = [p for p in pages if p.get('name') == 'Equation controls']
93 equations = [ET.fromstring(expression) for node in page.iter() if node.tag.endswith('}T')
94 for expression in re.findall(r'<!--\[if mathML\]>(.*?)<!\[endif\]-->', node.text or '', re.S)]
95 self.assertEqual(len(equations), 3)
96 ns = '{http://www.w3.org/1998/Math/MathML}'
97 self.assertEqual([n.tag for n in equations[0]], [ns + n for n in ('msup', 'mo', 'msup', 'mo', 'msup')])
98 self.assertEqual(''.join(equations[0].itertext()), 'x2+y2=z2')
99 self.assertEqual([n.tag for n in equations[1]], [ns + 'mfrac'])
100 self.assertEqual([''.join(n.itertext()) for n in equations[1][0]], ['a+b', 'c+d'])
101 self.assertEqual(''.join(equations[2].itertext()), '𝛼+𝛽')
102 self.assertNotIn('display', equations[2].attrib)
103 captures.append([ET.tostring(n) for n in equations])
104 self.assertEqual(*captures)
105
106 @requires_pdfplumber
107 def test_rtl_xml_order_matches_native_visual_column_order(self):
108 import pdfplumber
109 fixture = ROOT / 'corpus/m6/native-page-direction-03'
110 with pdfplumber.open(fixture / 'read/page-000.pdf') as pdf:
111 words = pdf.pages[0].extract_words()
112 right = [word for word in words if word['text'] == 'Right']
113 left = [word for word in words if word['text'] == 'Left']
114 self.assertEqual((len(right), len(left)), (1, 1))
115 self.assertLess(right[0]['x0'], left[0]['x0'])
116 with TemporaryDirectory() as temporary:
117 native = Path(temporary) / 'read'
118 shutil.copytree(fixture / 'read', native)
119 compare(fixture / 'notebook', native)
120
121 def test_changed_origin_requires_matching_native_coordinates(self):
122 fixture = ROOT / 'corpus/m6/native-origin-controls-01'
123 with TemporaryDirectory() as temporary:
124 output = Path(temporary)
125 notebook = output / 'notebook'
126 shutil.copytree(fixture / 'input/notebook', notebook)
127 shutil.copytree(fixture / 'read', output / 'read')
128 compare(notebook, output / 'read')
129 subprocess.run([ROOT / 'target/debug/examples/edit_property',
130 notebook / 'synthetic.one', output / 'changed.one',
131 '6000b', '14001d0f', '00004040', '00008040'], check=True)
132 (output / 'changed.one').replace(notebook / 'synthetic.one')
133 with self.assertRaisesRegex(AssertionError, 'coordinate differences'):
134 compare(notebook, output / 'read')
135
136
137if __name__ == '__main__':
138 unittest.main()