def test_prototypes(): prototype_source = io.StringIO(SOURCE.replace("{}", ";")) # Prototypes should be ignored by default. self = cslug.Types(io.StringIO(), prototype_source) assert self._types_from_source()["functions"] == {} # Unless they are passed as headers to intentionally include prototypes. self = cslug.Types(io.StringIO(), headers=prototype_source) assert self._types_from_source()["functions"] == PARSED_FUNCTIONS
def test_strict(strict): class FakeDLL(object): exists = ctypes.PYFUNCTYPE(int)() self = cslug.Types( io.StringIO(), headers=io.StringIO(""" float exists(int x); float doesnt_exist(int x); double also_doesnt_exist(float y); """)) self.init_from_source() dll = FakeDLL() if not strict: # Should skip over non-existent functions without complaint. self.apply(dll) else: # Should raise an error. with pytest.raises( AttributeError, match=r".* \['doesnt_exist', 'also_doesnt_exist'] .*"): self.apply(dll, strict=True) assert dll.exists.argtypes == [ctypes.c_int] assert dll.exists.restype == ctypes.c_float assert not hasattr(dll, "doesnt_exist") assert not hasattr(dll, "also_doesnt_exist")
def test_struct_prototypes(): # Check structs are correctly linked. struct_prototypes = io.StringIO(STRUCT_METHODS.replace("{}", ";")) self = cslug.Types(io.StringIO(), headers=[struct_prototypes, io.StringIO(STRUCT_TEXT)]) self.init_from_source() assert self.structs == PARSED_STRUCTS assert self.functions == PARSED_STRUCT_METHODS # The same test but pass the struct definition as a true source. # Should make no difference. struct_prototypes = io.StringIO(STRUCT_METHODS.replace("{}", ";")) self = cslug.Types(io.StringIO(), io.StringIO(STRUCT_TEXT), headers=struct_prototypes) self.init_from_source() assert self.structs == PARSED_STRUCTS assert self.functions == PARSED_STRUCT_METHODS
def test_functions(modifier, compact): with warnings.catch_warnings(): warnings.filterwarnings("ignore", category=cslug.exceptions.TypeParseWarning) self = cslug.Types(io.StringIO(), io.StringIO(modifier(SOURCE)), compact=compact) self.make() assert self.functions == PARSED_FUNCTIONS assert json.loads( self.json_path.getvalue())["functions"] == PARSED_FUNCTIONS from_json = cslug.Types(io.StringIO(self.json_path.getvalue())) from_json.init_from_json() assert from_json.types == self.types json_: str = self.json_path.getvalue() if compact: assert not re.search(r"\s", json_) else: assert json_.endswith("\n") and not json_.endswith("\n\n")
def test_struct(): self = cslug.Types(io.StringIO(), io.StringIO(STRUCT_TEXT + STRUCT_METHODS)) self.make() assert self.structs == PARSED_STRUCTS assert self.functions == PARSED_STRUCT_METHODS
def test_ignores_prototypes(modifier): self = cslug.Types(io.StringIO(), io.StringIO(modifier(SOURCE))) self.init_from_source() assert not self.functions