Debian Codegraph @

tests/test_wellformed.py

raw
1 import re
2 import sys
3
4 import pytest
5 from axisregistry import AxisRegistry, AxisProto
6
7 if sys.version_info < (3, 10):
8 from importlib_resources import files
9 else:
10 from importlib.resources import files
11
12 registry = AxisRegistry()
13
14 OPTIONAL_FIELDS = ["illustration_url", "is_parametric"]
15
16
17 @pytest.mark.parametrize("axis_tag", registry.keys())
18 def test_proto_wellformed(axis_tag):
19 axis = registry[axis_tag]
20 raw_fields = dict([(k.name, v) for k, v in axis.ListFields()])
21 for field in AxisProto.DESCRIPTOR.fields:
22 field_name = field.name
23 if field_name in OPTIONAL_FIELDS:
24 continue
25 assert (
26 field_name in raw_fields
27 ), f"Non-optional field {field_name} is missing in {axis_tag}"
28 assert raw_fields[field_name] is not None, field_name
29
30
31 proto_files = [
32 file
33 for file in files("axisregistry.data").iterdir()
34 if file.name.endswith(".textproto")
35 ]
36
37
38 @pytest.mark.parametrize("proto_file", proto_files)
39 def test_proto_multiline_string(proto_file):
40 with open(proto_file, "r") as f:
41 content = f.read()
42 # Check for multiline strings which do not have a space either at the
43 # end of the line or at the beginning of the next line
44 pattern = re.compile(r'".*\S"\n\s*"\S+')
45 matches = pattern.findall(content)
46 if matches:
47 matches = [re.sub(r'"\n\s+"', "", match) for match in matches]
48 # Assert that there are no such matches
49 assert (
50 not matches
51 ), f"Badly-spaced multiline strings found in {proto_file.name}: {', '.join(matches)}"