[BUGFIX] Custom function ordering (#1104)
Fixes a bug where custom functions would throw an error if they were used out-of-order from their definition
This commit is contained in:
parent
43c10c19e0
commit
1bdc65f2e1
2 changed files with 25 additions and 4 deletions
|
|
@ -490,15 +490,18 @@ class Script:
|
||||||
name: definition for name, definition in variables.items() if not _is_function(name)
|
name: definition for name, definition in variables.items() if not _is_function(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
custom_function_names = set(self._functions.keys()) | functions_to_add.keys()
|
||||||
|
variable_names = (
|
||||||
|
set(self._variables.keys()) | variables_to_add.keys() | (unresolvable or set())
|
||||||
|
)
|
||||||
|
|
||||||
for definitions in [functions_to_add, variables_to_add]:
|
for definitions in [functions_to_add, variables_to_add]:
|
||||||
for name, definition in definitions.items():
|
for name, definition in definitions.items():
|
||||||
parsed = parse(
|
parsed = parse(
|
||||||
text=definition,
|
text=definition,
|
||||||
name=name,
|
name=name,
|
||||||
custom_function_names=set(self._functions.keys()),
|
custom_function_names=custom_function_names,
|
||||||
variable_names=set(self._variables.keys())
|
variable_names=variable_names,
|
||||||
.union(variables.keys())
|
|
||||||
.union(unresolvable or set()),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if parsed.maybe_resolvable is None:
|
if parsed.maybe_resolvable is None:
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,24 @@ class TestCustomFunction:
|
||||||
}
|
}
|
||||||
).resolve() == ScriptOutput({"output": Integer(9)})
|
).resolve() == ScriptOutput({"output": Integer(9)})
|
||||||
|
|
||||||
|
def test_custom_functions_any_order_via_add(self):
|
||||||
|
assert Script({}).add(
|
||||||
|
{
|
||||||
|
"%custom_cubed": "{%mul(%custom_square($0),$0)}",
|
||||||
|
"%custom_square": "{%mul($0, $0)}",
|
||||||
|
"output": "{%custom_cubed(3)}",
|
||||||
|
}
|
||||||
|
).resolve() == ScriptOutput({"output": Integer(27)})
|
||||||
|
|
||||||
|
def test_custom_functions_any_order_via_init(self):
|
||||||
|
assert Script(
|
||||||
|
{
|
||||||
|
"%custom_cubed": "{%mul(%custom_square($0),$0)}",
|
||||||
|
"%custom_square": "{%mul($0, $0)}",
|
||||||
|
"output": "{%custom_cubed(3)}",
|
||||||
|
}
|
||||||
|
).resolve() == ScriptOutput({"output": Integer(27)})
|
||||||
|
|
||||||
def test_custom_function_cycle(self):
|
def test_custom_function_cycle(self):
|
||||||
with pytest.raises(
|
with pytest.raises(
|
||||||
CycleDetected, match=re.escape("The custom function %cycle_func cannot call itself.")
|
CycleDetected, match=re.escape("The custom function %cycle_func cannot call itself.")
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue