diff --git a/src/ytdl_sub/script/parser.py b/src/ytdl_sub/script/parser.py index 7711ae9a..82b66bd9 100644 --- a/src/ytdl_sub/script/parser.py +++ b/src/ytdl_sub/script/parser.py @@ -117,7 +117,7 @@ def _is_null(string: Optional[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: diff --git a/tests/unit/script/functions/test_array_functions.py b/tests/unit/script/functions/test_array_functions.py index a85e38ce..7ad81b5d 100644 --- a/tests/unit/script/functions/test_array_functions.py +++ b/tests/unit/script/functions/test_array_functions.py @@ -1,6 +1,8 @@ +import pytest from unit.script.conftest import single_variable_output from ytdl_sub.script.script import Script +from ytdl_sub.script.utils.exceptions import FunctionRuntimeException class TestArrayFunctions: @@ -70,3 +72,27 @@ class TestArrayFunctions: .native ) 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)}") diff --git a/tests/unit/script/functions/test_map_functions.py b/tests/unit/script/functions/test_map_functions.py index 174824b3..709445e6 100644 --- a/tests/unit/script/functions/test_map_functions.py +++ b/tests/unit/script/functions/test_map_functions.py @@ -1,9 +1,12 @@ import re import pytest +from unit.script.conftest import single_variable_output 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 KeyNotHashableRuntimeException class TestMapFunctions: @@ -47,6 +50,19 @@ class TestMapFunctions: } ).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( "contains_value, expected_value", [ @@ -98,3 +114,27 @@ class TestMapFunctions: .native ) 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)}")