map, array funcs

This commit is contained in:
Jesse Bannon 2023-12-16 22:33:12 -08:00
parent 3c6a6fb230
commit e3ab942389
3 changed files with 67 additions and 1 deletions

View file

@ -117,7 +117,7 @@ def _is_null(string: Optional[str]) -> bool:
def _is_breakable(char: str) -> bool: def _is_breakable(char: str) -> bool:
return char in ["}", ",", ")", "]"] or char.isspace() return char in ["}", ",", ")", "]", ":"] or char.isspace()
def _is_boolean_true(string: Optional[str]) -> bool: def _is_boolean_true(string: Optional[str]) -> bool:

View file

@ -1,6 +1,8 @@
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.script import Script
from ytdl_sub.script.utils.exceptions import FunctionRuntimeException
class TestArrayFunctions: class TestArrayFunctions:
@ -70,3 +72,27 @@ class TestArrayFunctions:
.native .native
) )
assert output == [[0, "a"], [1, "b"], [2, "c"]] assert output == [[0, "a"], [1, "b"], [2, "c"]]
def test_cast_array(self):
output = (
Script(
{
"map_test": "{ {'key': [1, 2, 3]} }",
"output": "{ %array( %map_get(map_test, 'key') )}",
}
)
.resolve(update=True)
.get("output")
.native
)
assert output == [1, 2, 3]
def test_array_size(self):
output = single_variable_output("{%array_size([1, 2, 3])}")
assert output == 3
def test_cast_array_errors_cannot_cast(self):
with pytest.raises(
FunctionRuntimeException, match="Tried and failed to cast Integer as an Array"
):
single_variable_output("{%array(1)}")

View file

@ -1,9 +1,12 @@
import re import re
import pytest import pytest
from unit.script.conftest import single_variable_output
from ytdl_sub.script.script import Script from ytdl_sub.script.script import Script
from ytdl_sub.script.utils.exceptions import FunctionRuntimeException
from ytdl_sub.script.utils.exceptions import KeyDoesNotExistRuntimeException from ytdl_sub.script.utils.exceptions import KeyDoesNotExistRuntimeException
from ytdl_sub.script.utils.exceptions import KeyNotHashableRuntimeException
class TestMapFunctions: class TestMapFunctions:
@ -47,6 +50,19 @@ class TestMapFunctions:
} }
).resolve() ).resolve()
def test_map_get_errors_key_not_hashable(self):
with pytest.raises(
KeyNotHashableRuntimeException,
match=re.escape("Tried to use Array as a Map key, but it is not hashable."),
):
Script(
{
"non_hashable_key": "{%array([1, 2, ['nest']])}",
"input_map": "{{'key': 'value'}}",
"output": "{ %map_get( input_map, %array_at(non_hashable_key, 2)) }",
}
).resolve()
@pytest.mark.parametrize( @pytest.mark.parametrize(
"contains_value, expected_value", "contains_value, expected_value",
[ [
@ -98,3 +114,27 @@ class TestMapFunctions:
.native .native
) )
assert output == [[0, "KEY1", "value1"], [1, "KEY2", "value2"]] assert output == [[0, "KEY1", "value1"], [1, "KEY2", "value2"]]
def test_map_size(self):
output = single_variable_output("{%map_size({'key': 'value', 1: 3})}")
assert output == 2
def test_cast_map(self):
output = (
Script(
{
"array_test": "{ [1, 2, {'key': 'value'}] }",
"output": "{ %map( %array_at(array_test, 2) )}",
}
)
.resolve(update=True)
.get("output")
.native
)
assert output == {"key": "value"}
def test_cast_map_errors_cannot_cast(self):
with pytest.raises(
FunctionRuntimeException, match="Tried and failed to cast Integer as a Map"
):
single_variable_output("{%map(1)}")