unit tests
This commit is contained in:
parent
d54d168bee
commit
a758120af0
4 changed files with 103 additions and 0 deletions
|
|
@ -3,6 +3,9 @@ import re
|
||||||
import pytest
|
import pytest
|
||||||
from unit.script.conftest import single_variable_output
|
from unit.script.conftest import single_variable_output
|
||||||
|
|
||||||
|
from ytdl_sub.script.script import Script
|
||||||
|
from ytdl_sub.script.types.syntax_tree import SyntaxTree
|
||||||
|
from ytdl_sub.script.types.variable import Variable
|
||||||
from ytdl_sub.script.utils.exceptions import FunctionRuntimeException
|
from ytdl_sub.script.utils.exceptions import FunctionRuntimeException
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -135,3 +138,29 @@ class TestConditionalFunction:
|
||||||
):
|
):
|
||||||
output = single_variable_output(function_str)
|
output = single_variable_output(function_str)
|
||||||
assert output == expected_output
|
assert output == expected_output
|
||||||
|
|
||||||
|
def test_if_partial_resolve(self):
|
||||||
|
assert (
|
||||||
|
Script(
|
||||||
|
{
|
||||||
|
"aa": "a",
|
||||||
|
"bb": "unresolvable!",
|
||||||
|
"cc": "{%if( true, aa, bb )}",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.resolve_partial(unresolvable={"bb"})
|
||||||
|
.get("cc")
|
||||||
|
.native
|
||||||
|
== "a"
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_if_partial_resolve_unresolved(self):
|
||||||
|
assert Script(
|
||||||
|
{
|
||||||
|
"aa": "a",
|
||||||
|
"bb": "unresolvable!",
|
||||||
|
"cc": "{%if( false, aa, bb )}",
|
||||||
|
}
|
||||||
|
).resolve_partial(unresolvable={"bb"}).definition_of("cc") == SyntaxTree(
|
||||||
|
ast=[Variable("bb")]
|
||||||
|
)
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,8 @@ from ytdl_sub.script.script_output import ScriptOutput
|
||||||
from ytdl_sub.script.types.array import Array
|
from ytdl_sub.script.types.array import Array
|
||||||
from ytdl_sub.script.types.resolvable import Float
|
from ytdl_sub.script.types.resolvable import Float
|
||||||
from ytdl_sub.script.types.resolvable import String
|
from ytdl_sub.script.types.resolvable import String
|
||||||
|
from ytdl_sub.script.types.syntax_tree import SyntaxTree
|
||||||
|
from ytdl_sub.script.types.variable import Variable
|
||||||
from ytdl_sub.script.utils.exceptions import InvalidSyntaxException
|
from ytdl_sub.script.utils.exceptions import InvalidSyntaxException
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -117,3 +119,29 @@ class TestArray:
|
||||||
).resolve() == ScriptOutput(
|
).resolve() == ScriptOutput(
|
||||||
{"aa": String("a"), "bb": String("b"), "cc": String('return ["a", "b"]')}
|
{"aa": String("a"), "bb": String("b"), "cc": String('return ["a", "b"]')}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_partial_resolve(self):
|
||||||
|
assert (
|
||||||
|
Script(
|
||||||
|
{
|
||||||
|
"aa": "a",
|
||||||
|
"bb": "unresolvable!",
|
||||||
|
"cc": "{%array_at( [aa, bb], 0 )}",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.resolve_partial(unresolvable={"bb"})
|
||||||
|
.get("cc")
|
||||||
|
.native
|
||||||
|
== "a"
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_partial_resolve_unresolved(self):
|
||||||
|
assert Script(
|
||||||
|
{
|
||||||
|
"aa": "a",
|
||||||
|
"bb": "unresolvable!",
|
||||||
|
"cc": "{%array_at( [aa, bb], 1 )}",
|
||||||
|
}
|
||||||
|
).resolve_partial(unresolvable={"bb"}).definition_of("cc") == SyntaxTree(
|
||||||
|
ast=[Variable("bb")]
|
||||||
|
)
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,9 @@ import pytest
|
||||||
from ytdl_sub.script.parser import CUSTOM_FUNCTION_ARGUMENTS_ONLY_ARGS
|
from ytdl_sub.script.parser import CUSTOM_FUNCTION_ARGUMENTS_ONLY_ARGS
|
||||||
from ytdl_sub.script.script import Script
|
from ytdl_sub.script.script import Script
|
||||||
from ytdl_sub.script.script_output import ScriptOutput
|
from ytdl_sub.script.script_output import ScriptOutput
|
||||||
|
from ytdl_sub.script.types.function import CustomFunction
|
||||||
from ytdl_sub.script.types.resolvable import Integer
|
from ytdl_sub.script.types.resolvable import Integer
|
||||||
|
from ytdl_sub.script.types.syntax_tree import SyntaxTree
|
||||||
from ytdl_sub.script.utils.exceptions import CycleDetected
|
from ytdl_sub.script.utils.exceptions import CycleDetected
|
||||||
from ytdl_sub.script.utils.exceptions import FunctionDoesNotExist
|
from ytdl_sub.script.utils.exceptions import FunctionDoesNotExist
|
||||||
from ytdl_sub.script.utils.exceptions import InvalidCustomFunctionArgumentName
|
from ytdl_sub.script.utils.exceptions import InvalidCustomFunctionArgumentName
|
||||||
|
|
@ -251,3 +253,30 @@ class TestCustomFunction:
|
||||||
"output": "{%mul(%func1(1), 1)}",
|
"output": "{%mul(%func1(1), 1)}",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_partial_resolve_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_partial()
|
||||||
|
.get("output")
|
||||||
|
.native
|
||||||
|
== 27
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_partial_resolve_unresolved(self):
|
||||||
|
assert Script(
|
||||||
|
{
|
||||||
|
"aa": "nope",
|
||||||
|
"%custom_cubed": "{%mul(%custom_square($0),$0)}",
|
||||||
|
"%custom_square": "{%mul($0, aa)}",
|
||||||
|
"output": "{%custom_cubed(3)}",
|
||||||
|
}
|
||||||
|
).resolve_partial(unresolvable={"aa"}).definition_of("output") == SyntaxTree(
|
||||||
|
ast=[CustomFunction(name="custom_cubed", args=[Integer(3)])]
|
||||||
|
)
|
||||||
|
|
|
||||||
|
|
@ -177,3 +177,20 @@ class TestLambdaFunctionIncompatibleNumArguments:
|
||||||
match=re.escape("Cycle detected within these variables: two -> %times_two -> two"),
|
match=re.escape("Cycle detected within these variables: two -> %times_two -> two"),
|
||||||
):
|
):
|
||||||
Script({"%times_two": "{%mul($0, two)}", "two": "{%times_two(2)}"})
|
Script({"%times_two": "{%mul($0, two)}", "two": "{%times_two(2)}"})
|
||||||
|
|
||||||
|
def test_partial_resolve_nested_lambda_custom_functions_within_custom_functions(self):
|
||||||
|
assert (
|
||||||
|
Script(
|
||||||
|
{
|
||||||
|
"%nest4": "{%mul($0, 2)}",
|
||||||
|
"%nest3": "{%array_at(%array_apply([$0], %nest4), 0)}",
|
||||||
|
"%nest2": "{%array_at(%array_apply([$0], %nest3), 0)}",
|
||||||
|
"%nest1": "{%array_at(%array_apply([$0], %nest2), 0)}",
|
||||||
|
"output": "{%array_at(%array_apply([2], %nest1), 0)}",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.resolve_partial()
|
||||||
|
.get("output")
|
||||||
|
.native
|
||||||
|
== 4
|
||||||
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue