]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - scripts/check-cxtm.py
70ceecbb0077c4abbb84767cc81d4abafce9d5e8
[topic-maps.git] / scripts / check-cxtm.py
1 #!/usr/bin/env python3
2 """Independent raw UTF-8 comparison, XML reference integrity and C14N checks."""
3 import json
4 import pathlib
5 import sys
6 import xml.etree.ElementTree as ET
7 root = pathlib.Path(__file__).resolve().parent.parent
8 report = json.loads(pathlib.Path(sys.argv[1]).read_text())
9 assert report['kind'] == 'cxtm-byte-equality' and report['failed'] == 0
10 assert report['passed'] == len(report['checks']) == len(set(report['checks']))
11 expected_count = 28 if len(sys.argv) > 2 and sys.argv[2] == '--slice-5' else 24
12 assert len(report['cases']) == expected_count
13 for case in report['cases']:
14 actual = case['actualCXTM'].encode('utf-8')
15 expected = (root / 'vendor/cxtm/ctm/baseline' / (pathlib.Path(case['id']).name + '.cxtm')).read_bytes()
16 assert actual == expected == case['expectedCXTM'].encode('utf-8'), case['id']
17 assert case['byteCount'] == len(actual) and case['status'] == 'pass'
18 assert case['baseLocator'].endswith('/' + case['id'])
19 # CXTM 3.1 adds an LF after the root. XML reparsing drops that Infoset
20 # sibling; this check is separate from the unmodified byte comparison.
21 assert actual.endswith(b'</topicMap>\n') and not actual.startswith(b'\xef\xbb\xbf')
22 assert ET.canonicalize(case['actualCXTM']) + '\n' == case['actualCXTM'], case['id']
23 xml = ET.fromstring(actual)
24 topics = xml.findall('topic'); associations = xml.findall('association')
25 assert [t.attrib['number'] for t in topics] == list(map(str,range(1,len(topics)+1)))
26 role_refs = {}
27 for i, a in enumerate(associations,1):
28 assert a.attrib['number'] == str(i)
29 for j, r in enumerate(a.findall('role'),1):
30 assert r.attrib['number'] == str(j)
31 role_refs[f'association.{i}.role.{j}'] = r.find('player').attrib['topicref']
32 for t in topics:
33 assert [o.attrib['number'] for o in t.findall('occurrence')] == list(map(str, range(1,len(t.findall('occurrence'))+1)))
34 for r in t.findall('rolePlayed'):
35 assert role_refs[r.attrib['ref']] == t.attrib['number']
36 assert sum(len(t.findall('rolePlayed')) for t in topics) == len(role_refs)
37 for ref in xml.iter():
38 if 'topicref' in ref.attrib:
39 assert 1 <= int(ref.attrib['topicref']) <= len(topics)
40 print(f'CXTM-INDEPENDENT-PASS: {expected_count} unchanged baselines, exact UTF-8 bytes, canonical XML and reference integrity')