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