Skip to content

Commit

Permalink
more complete tests for api
Browse files Browse the repository at this point in the history
  • Loading branch information
trolldbois committed Apr 19, 2023
1 parent 12fd510 commit f4683c1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -91,7 +91,7 @@ versions - clang2py:2.3.3 clang:11.1.0 python-clang:11.0
print(py_module2.struct_coordinates(1,2)) # <struct_coordinates object at 0xabcde12345>


Look at `test/test_example_script.py` for more advanced Library usage
Look at `test/test_api.py` for more advanced Library usage


### Use ctypeslib2 on the command line
Expand Down
18 changes: 16 additions & 2 deletions ctypeslib/codegen/codegenerator.py
Expand Up @@ -1148,19 +1148,33 @@ def translate(input_io, cfg=None):
return util.ADict(namespace)


def translate_files(source_files, outfile, cfg: config.CodegenConfig):
def translate_files(source_files, outfile=None, cfg: config.CodegenConfig=None):
"""
Translate the content of source_files in python code in outfile
source_files: list of filenames or single filename
"""
cfg = cfg or config.CodegenConfig()
translator = CodeTranslator(cfg)
translator.preload_dlls()
if isinstance(source_files, list):
translator.parse_input_files(source_files)
else:
translator.parse_input_file(source_files)
log.debug("Input was parsed")
translator.generate_code(outfile)
if outfile:
translator.generate_code(outfile)
else:
output = io.StringIO()
translator.generate_code(output)
output.seek(0)
# inject generated code in python namespace
ignore_coding = output.readline()
# exec ofi.getvalue() in namespace
output = ''.join(output.readlines())
namespace = {}
exec(output, namespace)
return util.ADict(namespace)

return

24 changes: 24 additions & 0 deletions test/test_api.py
@@ -1,3 +1,4 @@
import tempfile
import unittest
import io

Expand Down Expand Up @@ -56,6 +57,29 @@ def test_basic_use_io(self):
self.assertEqual(py_namespace.i, 12)
self.assertEqual(py_namespace.c2, ['a', 'b', 'c'])

def test_basic_file_io(self):
py_namespace = ctypeslib.translate_files('test/data/test-library.c')
self.assertIn("a", py_namespace)
self.assertEqual(py_namespace.a, 0)

def test_advanced_file_io(self):
cfg = config.CodegenConfig()
cfg.clang_opts.append('-I/home/jal/Code/ctypeslib/test/data/')
py_namespace = ctypeslib.translate_files('test/data/test-enum.c', cfg=cfg)
self.assertIn("ZERO", py_namespace)
self.assertIn("myEnum", py_namespace)
self.assertEqual(py_namespace.ZERO, 0)

def test_advanced_file_io_to_file(self):
cfg = config.CodegenConfig()
cfg.clang_opts.append('-I/home/jal/Code/ctypeslib/test/data/')
with tempfile.NamedTemporaryFile(suffix=".py", mode='w+') as tmpfile:
ctypeslib.translate_files('test/data/test-enum.c', outfile=tmpfile, cfg=cfg)
tmpfile.seek(0)
output = tmpfile.read()
self.assertIn("ZERO = 0", output)
self.assertIn("myEnum", output)


class ConfigTest(unittest.TestCase):
def setUp(self) -> None:
Expand Down

0 comments on commit f4683c1

Please sign in to comment.