more tests
This commit is contained in:
parent
68bc4008d9
commit
5815349914
6 changed files with 43 additions and 8 deletions
|
|
@ -101,7 +101,7 @@ def _is_function_name_char(char: str) -> bool:
|
||||||
|
|
||||||
|
|
||||||
def _is_numeric_start(char: str) -> bool:
|
def _is_numeric_start(char: str) -> bool:
|
||||||
return char.isnumeric() or char == "-"
|
return char.isnumeric() or char in (".", "-")
|
||||||
|
|
||||||
|
|
||||||
def _is_string_start_single_char(char: Optional[str]) -> bool:
|
def _is_string_start_single_char(char: Optional[str]) -> bool:
|
||||||
|
|
@ -222,8 +222,9 @@ class _Parser:
|
||||||
variable_start_pos = self._pos
|
variable_start_pos = self._pos
|
||||||
while ch := self._read(increment_pos=False):
|
while ch := self._read(increment_pos=False):
|
||||||
if ch.isspace() and not var_name:
|
if ch.isspace() and not var_name:
|
||||||
self._pos += 1
|
raise InvalidCustomFunctionArgumentName(
|
||||||
continue
|
"Custom function arguments, denoted by $, cannot have a space proceeding it."
|
||||||
|
)
|
||||||
if _is_breakable(ch):
|
if _is_breakable(ch):
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
@ -457,7 +458,7 @@ class _Parser:
|
||||||
while ch := self._read(increment_pos=False):
|
while ch := self._read(increment_pos=False):
|
||||||
if ch == "}":
|
if ch == "}":
|
||||||
if key is not None:
|
if key is not None:
|
||||||
raise MAP_KEY_WITH_NO_VALUE
|
raise MAP_KEY_WITH_NO_VALUE # key args are parsed immediately
|
||||||
|
|
||||||
self._pos += 1
|
self._pos += 1
|
||||||
return UnresolvedMap(value=output)
|
return UnresolvedMap(value=output)
|
||||||
|
|
@ -466,7 +467,7 @@ class _Parser:
|
||||||
if in_comma:
|
if in_comma:
|
||||||
raise _UNEXPECTED_COMMA_ARGUMENT(ParsedArgType.MAP_KEY)
|
raise _UNEXPECTED_COMMA_ARGUMENT(ParsedArgType.MAP_KEY)
|
||||||
if key is not None:
|
if key is not None:
|
||||||
raise MAP_KEY_WITH_NO_VALUE
|
raise MAP_KEY_WITH_NO_VALUE # key args are parsed immediately
|
||||||
if not output:
|
if not output:
|
||||||
raise _UNEXPECTED_COMMA_ARGUMENT(ParsedArgType.MAP_KEY)
|
raise _UNEXPECTED_COMMA_ARGUMENT(ParsedArgType.MAP_KEY)
|
||||||
in_comma = True
|
in_comma = True
|
||||||
|
|
@ -496,7 +497,7 @@ class _Parser:
|
||||||
if isinstance(key, NonHashable):
|
if isinstance(key, NonHashable):
|
||||||
raise MAP_KEY_NOT_HASHABLE
|
raise MAP_KEY_NOT_HASHABLE
|
||||||
if len(value_args) > 1:
|
if len(value_args) > 1:
|
||||||
raise UNREACHABLE
|
raise MAP_KEY_MULTIPLE_VALUES
|
||||||
|
|
||||||
output[key] = value_args[0]
|
output[key] = value_args[0]
|
||||||
key = None
|
key = None
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,11 @@ class VariableDependency(ABC):
|
||||||
@property
|
@property
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def _iterable_arguments(self) -> List[Argument]:
|
def _iterable_arguments(self) -> List[Argument]:
|
||||||
pass
|
"""
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
Any arguments in the VariableDependency that may or may not need to be resolved.
|
||||||
|
"""
|
||||||
|
|
||||||
def _recurse_get(self, ttype: Type[TypeT], subclass: bool = False) -> List[TypeT]:
|
def _recurse_get(self, ttype: Type[TypeT], subclass: bool = False) -> List[TypeT]:
|
||||||
output: List[TypeT] = []
|
output: List[TypeT] = []
|
||||||
|
|
|
||||||
|
|
@ -219,3 +219,17 @@ class TestCustomFunction:
|
||||||
"output": "{%mul(%func1(1), 1)}",
|
"output": "{%mul(%func1(1), 1)}",
|
||||||
}
|
}
|
||||||
).resolve()
|
).resolve()
|
||||||
|
|
||||||
|
def test_function_argument_errors_has_spaces(self):
|
||||||
|
with pytest.raises(
|
||||||
|
InvalidCustomFunctionArgumentName,
|
||||||
|
match=re.escape(
|
||||||
|
"Custom function arguments, denoted by $, cannot have a space proceeding it."
|
||||||
|
),
|
||||||
|
):
|
||||||
|
Script(
|
||||||
|
{
|
||||||
|
"%func1": "{%mul($ 1, $0)}",
|
||||||
|
"output": "{%mul(%func1(1), 1)}",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,6 @@ from ytdl_sub.script.parser import NUMERICS_INVALID_CHAR
|
||||||
from ytdl_sub.script.parser import NUMERICS_ONLY_ARGS
|
from ytdl_sub.script.parser import NUMERICS_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.resolvable import Boolean
|
|
||||||
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.utils.exceptions import InvalidSyntaxException
|
from ytdl_sub.script.utils.exceptions import InvalidSyntaxException
|
||||||
|
|
@ -37,6 +36,8 @@ class TestFloat:
|
||||||
("{%float( -1.535 )}", -1.535),
|
("{%float( -1.535 )}", -1.535),
|
||||||
("{%float(0001.)}", 1.0),
|
("{%float(0001.)}", 1.0),
|
||||||
("{%float( 0001. )}", 1.0),
|
("{%float( 0001. )}", 1.0),
|
||||||
|
("{%float(.2)}", 0.2),
|
||||||
|
("{%float(-.1)}", -0.1),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_float(self, float_: str, expected_float: int):
|
def test_float(self, float_: str, expected_float: int):
|
||||||
|
|
@ -56,6 +57,7 @@ class TestFloat:
|
||||||
"{%add(0, -1.0. )}",
|
"{%add(0, -1.0. )}",
|
||||||
"{%add(0,0001.a)}",
|
"{%add(0,0001.a)}",
|
||||||
"{%add(0, 0001.- )}",
|
"{%add(0, 0001.- )}",
|
||||||
|
"{%add(0, ..3)}",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_invalid_float(self, float_: str):
|
def test_invalid_float(self, float_: str):
|
||||||
|
|
|
||||||
|
|
@ -101,6 +101,7 @@ class TestMap:
|
||||||
"{{'key': }}",
|
"{{'key': }}",
|
||||||
"{{'key':}}",
|
"{{'key':}}",
|
||||||
"{{'key': 'value', 'key2':}}",
|
"{{'key': 'value', 'key2':}}",
|
||||||
|
"{{'key1': 'value1','value2'}}",
|
||||||
"{{'key': 'value', 'key2': }}",
|
"{{'key': 'value', 'key2': }}",
|
||||||
"{{ 'key': 'value', 'key2':\n}}",
|
"{{ 'key': 'value', 'key2':\n}}",
|
||||||
"{{ 'key': ,\n}}",
|
"{{ 'key': ,\n}}",
|
||||||
|
|
@ -138,6 +139,16 @@ class TestMap:
|
||||||
with pytest.raises(InvalidSyntaxException, match=re.escape(str(MAP_KEY_MULTIPLE_VALUES))):
|
with pytest.raises(InvalidSyntaxException, match=re.escape(str(MAP_KEY_MULTIPLE_VALUES))):
|
||||||
Script({"dict": value}).resolve()
|
Script({"dict": value}).resolve()
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"value",
|
||||||
|
[
|
||||||
|
"{{'key1': 'value1',}}",
|
||||||
|
"{{'key1': 'value1' , }}",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_map_trailing_comma_okay(self, value: str):
|
||||||
|
assert Script({"dict": value}).resolve().get_native("dict") == {"key1": "value1"}
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"value",
|
"value",
|
||||||
[
|
[
|
||||||
|
|
|
||||||
|
|
@ -50,6 +50,9 @@ class TestString:
|
||||||
def test_string(self, string: str, expected_string: str):
|
def test_string(self, string: str, expected_string: str):
|
||||||
assert Script({"out": string}).resolve() == ScriptOutput({"out": String(expected_string)})
|
assert Script({"out": string}).resolve() == ScriptOutput({"out": String(expected_string)})
|
||||||
|
|
||||||
|
def test_null_is_empty_string(self):
|
||||||
|
assert Script({"out": "{%string(null)}"}).resolve() == ScriptOutput({"out": String("")})
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"string",
|
"string",
|
||||||
[
|
[
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue