From 82d08fa4df2925913433daaa5fad4e3387830569 Mon Sep 17 00:00:00 2001 From: Brian Guarraci Date: Fri, 21 Jul 2023 17:46:48 -0600 Subject: [PATCH] add unit test for klong_exports --- klongpy/sys_fn.py | 6 +++++- tests/plugins/custom_export/__init__.py | 4 ++++ tests/plugins/custom_export/exports.py | 6 ++++++ tests/test_sys_fn.py | 8 ++++++++ 4 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 tests/plugins/custom_export/__init__.py create mode 100644 tests/plugins/custom_export/exports.py diff --git a/klongpy/sys_fn.py b/klongpy/sys_fn.py index 6691c5d..03130e1 100644 --- a/klongpy/sys_fn.py +++ b/klongpy/sys_fn.py @@ -343,7 +343,11 @@ def hello(): print(f".py: {e}") raise RuntimeError("module could not be imported: {x}") try: - import_items = filter(lambda p: not (p[0].startswith("__")), module.__dict__.items()) + klong_exports = module.__dict__.get("klong_exports") + if klong_exports is None: + import_items = filter(lambda p: not (p[0].startswith("__")), module.__dict__.items()) + else: + import_items = klong_exports.items() if import_items is not None: ctx = klong._context.pop() try: diff --git a/tests/plugins/custom_export/__init__.py b/tests/plugins/custom_export/__init__.py new file mode 100644 index 0000000..aebece2 --- /dev/null +++ b/tests/plugins/custom_export/__init__.py @@ -0,0 +1,4 @@ +from .exports import command_map + +klong_exports = command_map() + diff --git a/tests/plugins/custom_export/exports.py b/tests/plugins/custom_export/exports.py new file mode 100644 index 0000000..efc4ae1 --- /dev/null +++ b/tests/plugins/custom_export/exports.py @@ -0,0 +1,6 @@ + +def hello(): + return "hello, world!" + +def command_map(): + return {".hello": hello} diff --git a/tests/test_sys_fn.py b/tests/test_sys_fn.py index f5a1b72..2cc3beb 100644 --- a/tests/test_sys_fn.py +++ b/tests/test_sys_fn.py @@ -252,6 +252,14 @@ def test_sys_python_load_custom_module(self): finally: sys.path.pop() + def test_sys_python_load_custom_export(self): + tests_dir = os.path.dirname(os.path.abspath(__file__)) + plugins_dir = os.path.join(tests_dir, "plugins") + fpath = os.path.join(plugins_dir, "custom_export") + klong = KlongInterpreter() + self.assertEqual(klong(f'.py("{fpath}")'),1) + self.assertEqual(klong('.hello()'),"hello, world!") + def test_eval_sys_random_number(self): r = eval_sys_random_number() r2 = eval_sys_random_number()