diff --git a/src/ytdl_sub/script/parser.py b/src/ytdl_sub/script/parser.py index bd545735..8aea41ff 100644 --- a/src/ytdl_sub/script/parser.py +++ b/src/ytdl_sub/script/parser.py @@ -13,6 +13,7 @@ from ytdl_sub.script.types.resolvable import String from ytdl_sub.script.types.syntax_tree import SyntaxTree from ytdl_sub.script.types.variable import FunctionArgument from ytdl_sub.script.types.variable import Variable +from ytdl_sub.script.utils.exceptions import InvalidSyntaxException from ytdl_sub.utils.exceptions import StringFormattingException from ytdl_sub.validators.string_formatter_validators import is_valid_source_variable_name @@ -229,7 +230,7 @@ class _Parser: while ch := self._read(increment_pos=False): if ch == "}": if key is not None: - raise StringFormattingException("Key with no value") + raise InvalidSyntaxException("Map has a key with no value") self._pos += 1 return UnresolvedMap(value=output) @@ -237,7 +238,7 @@ class _Parser: if in_comma: raise StringFormattingException("Comma followed by comma") if key is not None: - raise StringFormattingException("key followed by comma") + raise InvalidSyntaxException("Map has a key with no value") if output is None: raise StringFormattingException("Empty dict with comma") in_comma = True @@ -252,7 +253,7 @@ class _Parser: self._pos += 1 value_args = self._parse_args(breaking_chars=",}") if len(value_args) != 1: - raise StringFormattingException("Lazy parsing, no value") + raise InvalidSyntaxException("Map has a key with no value") output[key] = value_args[0] key = None diff --git a/src/ytdl_sub/script/utils/__init__.py b/src/ytdl_sub/script/utils/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/ytdl_sub/script/utils/exceptions.py b/src/ytdl_sub/script/utils/exceptions.py new file mode 100644 index 00000000..2a4df9f9 --- /dev/null +++ b/src/ytdl_sub/script/utils/exceptions.py @@ -0,0 +1,5 @@ +from ytdl_sub.utils.exceptions import ValidationException + + +class InvalidSyntaxException(ValidationException): + """Syntax is incorrect""" diff --git a/tests/unit/script/types/test_map.py b/tests/unit/script/types/test_map.py index 4e316a91..73a4e28a 100644 --- a/tests/unit/script/types/test_map.py +++ b/tests/unit/script/types/test_map.py @@ -1,7 +1,12 @@ +import re + +import pytest + from ytdl_sub.script.script import Script from ytdl_sub.script.types.map import ResolvedMap from ytdl_sub.script.types.resolvable import Float from ytdl_sub.script.types.resolvable import String +from ytdl_sub.script.utils.exceptions import InvalidSyntaxException class TestMap: @@ -54,3 +59,18 @@ class TestMap: def test_empty_map(self): assert Script({"map": "{{}}"}).resolve() == {"map": ResolvedMap({})} + + @pytest.mark.parametrize( + "value", + [ + "{{'key': }}", + "{{'key':}}", + "{{'key': 'value', 'key2':}}", + "{{'key': 'value', 'key2': }}", + "{{ 'key': 'value', 'key2':\n}}", + "{{ 'key': ,\n}}", + ], + ) + def test_key_has_no_value(self, value: str): + with pytest.raises(InvalidSyntaxException, match=re.escape("Map has a key with no value")): + Script({"map": value}).resolve()