]>
dreyeck.freedombox.rocks Git - topic-maps.git/blob - scripts/check-cxtm.py
2 """Independent raw UTF-8 comparison, XML reference integrity and C14N checks."""
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
= {'--slice-19c': 180, '--slice-19b': 171, '--slice-18': 167, '--slice-17b': 110, '--slice-17a': 97, '--slice-16b': 96, '--slice-15': 85, '--slice-14': 64, '--slice-13': 61, '--slice-12a': 58, '--slice-5': 28, '--slice-6': 33, '--slice-7': 39, '--slice-8': 46, '--slice-9': 51, '--slice-10': 55, '--slice-11': 56}.get(sys
.argv
[2] if len(sys
.argv
) > 2 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 expected
== case
['expectedCXTM'].encode('utf-8')
17 discrepancy
= expected_count
>= 55 and (case
['id'] == 'ctm/in/occurrence-datetime2.ctm' or expected_count
>= 56 and case
['id'] == 'ctm/in/occurrence-datetime-timezone2.ctm')
18 if expected_count
>= 180 and case
['id'] in {'ctm/in/include-template.ctm', 'ctm/in/include-wildcard2.ctm'}:
19 assert actual
!= expected
and case
['status'] == 'fail'
20 if case
['id'].endswith('include-template.ctm'):
21 assert b
'<locator>include-template.sub#occtype</locator>' not in actual
22 assert b
'<locator>include-template.sub#occtype</locator>' in expected
23 assert (len(actual
), len(expected
)) == (353, 401)
25 assert b
'<locator>include-wildcard2.sub#$__1.foo</locator>' in actual
26 assert b
'<locator>include-wildcard2.sub#$__1.foo</locator>' not in expected
27 assert (len(actual
), len(expected
)) == (719, 669)
29 assert actual
!= expected
and case
['status'] == 'fail'
30 if case
['id'].endswith('timezone2.ctm'):
31 assert b
'<value>2010-12-31T22:00:00Z</value>' in actual
32 assert b
'<value>2011-01-01T00:00:00+02:00</value>' in expected
34 assert b
'<value>1976-09-19T12:02:00</value>' in actual
35 assert b
'<value>1976-09-19T12:01:60</value>' in expected
37 assert actual
== expected
and case
['status'] == 'pass', case
['id']
38 assert case
['byteCount'] == len(actual
)
39 assert case
['baseLocator'].endswith('/' + case
['id'])
40 # CXTM 3.1 adds an LF after the root. XML reparsing drops that Infoset
41 # sibling; this check is separate from the unmodified byte comparison.
42 assert actual
.endswith(b
'</topicMap>\n') and not actual
.startswith(b
'\xef\xbb\xbf')
43 assert ET
.canonicalize(case
['actualCXTM']) + '\n' == case
['actualCXTM'], case
['id']
44 xml
= ET
.fromstring(actual
)
45 topics
= xml
.findall('topic'); associations
= xml
.findall('association')
46 assert [t
.attrib
['number'] for t
in topics
] == list(map(str,range(1,len(topics
)+1)))
48 for i
, a
in enumerate(associations
,1):
49 assert a
.attrib
['number'] == str(i
)
50 for j
, r
in enumerate(a
.findall('role'),1):
51 assert r
.attrib
['number'] == str(j
)
52 role_refs
[f
'association.{i}.role.{j}'] = r
.find('player').attrib
['topicref']
54 assert [o
.attrib
['number'] for o
in t
.findall('occurrence')] == list(map(str, range(1,len(t
.findall('occurrence'))+1)))
55 for n
in t
.findall('name'):
56 assert [v
.attrib
['number'] for v
in n
.findall('variant')] == list(map(str, range(1,len(n
.findall('variant'))+1)))
57 for v
in n
.findall('variant'):
58 assert [c
.tag
for c
in v
] == ['value', 'datatype', 'scope']
59 assert v
.find('scope/scopingTopic') is not None
60 for r
in t
.findall('rolePlayed'):
61 assert role_refs
[r
.attrib
['ref']] == t
.attrib
['number']
62 assert sum(len(t
.findall('rolePlayed')) for t
in topics
) == len(role_refs
)
63 for ref
in xml
.iter():
64 if 'reifier' in ref
.attrib
:
65 assert 1 <= int(ref
.attrib
['reifier']) <= len(topics
)
66 if 'topicref' in ref
.attrib
:
67 assert 1 <= int(ref
.attrib
['topicref']) <= len(topics
)
68 passes
= sum(c
['status'] == 'pass' for c
in report
['cases'])
69 fails
= sum(c
['status'] == 'fail' for c
in report
['cases'])
70 print(f
'CXTM-INDEPENDENT-PASS: {passes} byte-exact unchanged baselines; {fails} explicit normative discrepancy; canonical XML and reference integrity')