Skip to content

Commit

Permalink
add unit test for klong_exports
Browse files Browse the repository at this point in the history
  • Loading branch information
briangu committed Jul 21, 2023
1 parent fd8a758 commit 82d08fa
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion klongpy/sys_fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions tests/plugins/custom_export/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .exports import command_map

klong_exports = command_map()

6 changes: 6 additions & 0 deletions tests/plugins/custom_export/exports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

def hello():
return "hello, world!"

def command_map():
return {".hello": hello}
8 changes: 8 additions & 0 deletions tests/test_sys_fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 82d08fa

Please sign in to comment.