Commit 8066261b authored by Lucas Bourneuf's avatar Lucas Bourneuf

test for modifications of the options, which is now properly supported

parent 3ad4af1c
......@@ -246,9 +246,9 @@ def build_script_from_module(module, *, defaults:dict={}) -> Script or ScriptErr
if hasattr(module, 'run_on'):
run_on_func = {'run_on': module.run_on}
run_on_func = module.run_on
args = inspect.getfullargspec(module.run_on)
# print('\nSCRIPT ARGS:', module.NAME, args)
# print('\nSCRIPT ARGS:', getattr(module, 'NAME', 'unamed'), args)
# Return type
if inspect.isgeneratorfunction(module.run_on):
......@@ -267,8 +267,13 @@ def build_script_from_module(module, *, defaults:dict={}) -> Script or ScriptErr
elif first_arg == 'models':
input_mode = iter
else:
bad_script_error(module, "run_on first arg must be either 'context' or"
" 'models', not a {}".format(first_arg))
bad_script_error(module, "run_on first (and only) positional argument"
f" must be either 'context' or 'models', not a {first_arg}")
if len(args.args) != 1:
# TODO: allows to use regular arguments instead of keyword only
bad_script_error(module, f"run_on must have only one positional "
f"argument, not {len(args.args)}")
# detect options
options = [] # list of (arg name, arg type, default, description)
......@@ -296,7 +301,7 @@ def build_script_from_module(module, *, defaults:dict={}) -> Script or ScriptErr
else: # no run_on function… the source code and the language will be enough, we hope…
options, default_options = (), {}
input_mode = str
run_on_func = {}
run_on_func = None
# source code data
source_code = getattr(module, 'source_code', None)
......@@ -322,7 +327,7 @@ def build_script_from_module(module, *, defaults:dict={}) -> Script or ScriptErr
source_code=source_code,
language=language,
erase_context=bool(getattr(module, 'ERASE_CONTEXT', defaults.get('erase_context', False))),
**run_on_func
run_on=run_on_func,
)
......
import pytest
import itertools
from collections import defaultdict
from pydot import graph_from_dot_data
......@@ -90,3 +91,28 @@ def test_python_builder_with_modifications():
assert script.run_on('a :- b.') == 'b.\na :- b.'
# name is conserved between switches
assert script.name == 'test-python'
def test_python_builder_with_args_and_modifications():
script = biseau.module_loader.build_script_from_json({
'name': 'test-python',
'python': 'def run_on(context, *, name:str="a"):\n yield f"{name}."\n',
})
assert script.language == 'python'
assert script.options_values == {}
assert script.run_on('') == 'a.\n'
script.options_values['name'] = 'b'
assert script.run_on('') == 'b.\n'
script.options_values['name'] = 'a'
assert script.run_on('') == 'a.\n'
assert script.options == (('name', str, 'a', ''),)
script.source_code = 'def run_on(context, *, new_name:str="a"):\n yield f"{new_name}."\n'
assert script.options == (('new_name', str, 'a', ''),)
assert script.run_on('') == 'a.\n'
script.options_values['new_name'] = 'b'
# let's try to set an unexisting option
script.options_values['name'] = 'c'
with pytest.raises(TypeError) as err:
script.run_on('') == 'b.\n'
err.value == "run_on() got an unexpected keyword argument 'name'"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment