Skip to content
Snippets Groups Projects
Commit 64ceee4b authored by Derval Guillaume's avatar Derval Guillaume
Browse files

Check parsing results

parent 0261aa86
No related branches found
No related tags found
No related merge requests found
import types
import typing
from dataclasses import is_dataclass, fields
from gboml.ast import *
class NotOfType(Exception): pass
class InvalidContent(Exception): pass
def _check_type(value, typ):
if isinstance(typ, str):
typ = eval(typ)
if isinstance(typ, types.GenericAlias) and typ.__origin__ == list:
subtyp = typing.get_args(typ)[0]
if not isinstance(value, list):
raise NotOfType(f"{value} is not an instance of {typ}")
for x in value:
_check_type(x, subtyp)
return
if isinstance(typ, types.UnionType):
for subtyp in typing.get_args(typ):
try:
_check_type(value, subtyp)
return
except NotOfType as e:
pass
raise NotOfType(f"{value} is not an instance of {typ}")
if typ == typing.Optional[typing.Any]:
return
if typ is typing.Any:
if typ is not None:
return
raise NotOfType(f"{value} is not an instance of {typ}")
if is_dataclass(typ):
if not isinstance(value, typ):
raise NotOfType(f"{value} is not an instance of {typ}")
check(value)
return
try:
if not isinstance(value, typ):
raise NotOfType(f"{value} is not an instance of {typ}")
except TypeError:
print(typ)
return
def check(t: GBOMLObject):
""" Check that a GBOMLObject is well-formed"""
try:
for f in fields(t):
value = getattr(t, f.name)
typ = f.type
_check_type(value, typ)
except NotOfType as e:
raise InvalidContent(str(e) + f" in {t}")
import unittest
from pathlib import Path
from gboml.ast.check import check
from gboml.parsing import parse_file
instance_dir = Path(__file__).parent / "instances"
......@@ -24,7 +25,7 @@ class TestParsing(unittest.TestCase):
""" Checks if the file (doesn't) parses """
for filename in should_pass:
with self.subTest(filename=filename):
parse_file(str(filename))
check(parse_file(str(filename)))
@unittest.expectedFailure
def test_not_parse(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment