string, bool, function tests wip
This commit is contained in:
parent
7a19718751
commit
d50c0c6559
4 changed files with 85 additions and 2 deletions
|
|
@ -47,6 +47,10 @@ NUMERICS_ONLY_ARGS = InvalidSyntaxException(
|
||||||
NUMERICS_INVALID_CHAR = InvalidSyntaxException("Invalid value when parsing a numeric")
|
NUMERICS_INVALID_CHAR = InvalidSyntaxException("Invalid value when parsing a numeric")
|
||||||
|
|
||||||
|
|
||||||
|
STRINGS_ONLY_ARGS = InvalidSyntaxException(
|
||||||
|
"Strings can only be used as arguments to functions, maps, or arrays"
|
||||||
|
)
|
||||||
|
|
||||||
def UNEXPECTED_CHAR_ARGUMENT(parser: ArgumentParser):
|
def UNEXPECTED_CHAR_ARGUMENT(parser: ArgumentParser):
|
||||||
return InvalidSyntaxException(f"Unexpected character when parsing {parser.value} arguments")
|
return InvalidSyntaxException(f"Unexpected character when parsing {parser.value} arguments")
|
||||||
|
|
||||||
|
|
@ -72,6 +76,8 @@ def _is_variable_start(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 == "-"
|
||||||
|
|
||||||
|
def _is_string_start(char: str) -> bool:
|
||||||
|
return char in ["'", '"']
|
||||||
|
|
||||||
def _is_breakable(char: str) -> bool:
|
def _is_breakable(char: str) -> bool:
|
||||||
return char in ["}", ",", ")", "]"] or char.isspace()
|
return char in ["}", ",", ")", "]"] or char.isspace()
|
||||||
|
|
@ -214,7 +220,9 @@ class _Parser:
|
||||||
"""
|
"""
|
||||||
string_value = ""
|
string_value = ""
|
||||||
open_quotation_char = self._read()
|
open_quotation_char = self._read()
|
||||||
assert open_quotation_char in ["'", '"']
|
|
||||||
|
if not _is_string_start(open_quotation_char):
|
||||||
|
raise UNREACHABLE
|
||||||
|
|
||||||
while ch := self._read():
|
while ch := self._read():
|
||||||
if ch == open_quotation_char:
|
if ch == open_quotation_char:
|
||||||
|
|
@ -235,7 +243,7 @@ class _Parser:
|
||||||
if (self._read(increment_pos=False, length=5) or "").lower() == "false":
|
if (self._read(increment_pos=False, length=5) or "").lower() == "false":
|
||||||
self._pos += 5
|
self._pos += 5
|
||||||
return Boolean(value=False)
|
return Boolean(value=False)
|
||||||
if self._read(increment_pos=False) in ["'", '"']:
|
if _is_string_start(self._read(increment_pos=False)):
|
||||||
return self._parse_string()
|
return self._parse_string()
|
||||||
if self._read(increment_pos=False) == "[":
|
if self._read(increment_pos=False) == "[":
|
||||||
self._pos += 1
|
self._pos += 1
|
||||||
|
|
@ -416,6 +424,8 @@ class _Parser:
|
||||||
self._ast.append(self._parse_variable())
|
self._ast.append(self._parse_variable())
|
||||||
elif _is_numeric_start(ch1):
|
elif _is_numeric_start(ch1):
|
||||||
raise NUMERICS_ONLY_ARGS
|
raise NUMERICS_ONLY_ARGS
|
||||||
|
elif _is_string_start(ch1):
|
||||||
|
raise STRINGS_ONLY_ARGS
|
||||||
else:
|
else:
|
||||||
raise UNEXPECTED_CHAR_ARGUMENT(parser=ArgumentParser.SCRIPT)
|
raise UNEXPECTED_CHAR_ARGUMENT(parser=ArgumentParser.SCRIPT)
|
||||||
elif bracket_counter == 0:
|
elif bracket_counter == 0:
|
||||||
|
|
|
||||||
20
tests/unit/script/types/test_bool.py
Normal file
20
tests/unit/script/types/test_bool.py
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
import re
|
||||||
|
from typing import Tuple
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from ytdl_sub.script.parser import NUMERICS_INVALID_CHAR, STRINGS_ONLY_ARGS
|
||||||
|
from ytdl_sub.script.parser import NUMERICS_ONLY_ARGS
|
||||||
|
from ytdl_sub.script.parser import UNEXPECTED_CHAR_ARGUMENT
|
||||||
|
from ytdl_sub.script.parser import UNEXPECTED_COMMA_ARGUMENT
|
||||||
|
from ytdl_sub.script.parser import ArgumentParser
|
||||||
|
from ytdl_sub.script.script import Script
|
||||||
|
from ytdl_sub.script.types.array import ResolvedArray
|
||||||
|
from ytdl_sub.script.types.resolvable import Float
|
||||||
|
from ytdl_sub.script.types.resolvable import Integer
|
||||||
|
from ytdl_sub.script.types.resolvable import String
|
||||||
|
from ytdl_sub.script.utils.exceptions import InvalidSyntaxException
|
||||||
|
|
||||||
|
|
||||||
|
class TestBool:
|
||||||
|
pass
|
||||||
20
tests/unit/script/types/test_function.py
Normal file
20
tests/unit/script/types/test_function.py
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
import re
|
||||||
|
from typing import Tuple
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from ytdl_sub.script.parser import NUMERICS_INVALID_CHAR, STRINGS_ONLY_ARGS
|
||||||
|
from ytdl_sub.script.parser import NUMERICS_ONLY_ARGS
|
||||||
|
from ytdl_sub.script.parser import UNEXPECTED_CHAR_ARGUMENT
|
||||||
|
from ytdl_sub.script.parser import UNEXPECTED_COMMA_ARGUMENT
|
||||||
|
from ytdl_sub.script.parser import ArgumentParser
|
||||||
|
from ytdl_sub.script.script import Script
|
||||||
|
from ytdl_sub.script.types.array import ResolvedArray
|
||||||
|
from ytdl_sub.script.types.resolvable import Float
|
||||||
|
from ytdl_sub.script.types.resolvable import Integer
|
||||||
|
from ytdl_sub.script.types.resolvable import String
|
||||||
|
from ytdl_sub.script.utils.exceptions import InvalidSyntaxException
|
||||||
|
|
||||||
|
|
||||||
|
class TestFunction:
|
||||||
|
pass
|
||||||
33
tests/unit/script/types/test_string.py
Normal file
33
tests/unit/script/types/test_string.py
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
import re
|
||||||
|
from typing import Tuple
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from ytdl_sub.script.parser import NUMERICS_INVALID_CHAR, STRINGS_ONLY_ARGS
|
||||||
|
from ytdl_sub.script.parser import NUMERICS_ONLY_ARGS
|
||||||
|
from ytdl_sub.script.parser import UNEXPECTED_CHAR_ARGUMENT
|
||||||
|
from ytdl_sub.script.parser import UNEXPECTED_COMMA_ARGUMENT
|
||||||
|
from ytdl_sub.script.parser import ArgumentParser
|
||||||
|
from ytdl_sub.script.script import Script
|
||||||
|
from ytdl_sub.script.types.array import ResolvedArray
|
||||||
|
from ytdl_sub.script.types.resolvable import Float
|
||||||
|
from ytdl_sub.script.types.resolvable import Integer
|
||||||
|
from ytdl_sub.script.types.resolvable import String
|
||||||
|
from ytdl_sub.script.utils.exceptions import InvalidSyntaxException
|
||||||
|
|
||||||
|
|
||||||
|
class TestString:
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"string",
|
||||||
|
[
|
||||||
|
"{'323'}",
|
||||||
|
"{ \"4253\" }",
|
||||||
|
"{\"hi\"}",
|
||||||
|
"{ \"asfsd\" }",
|
||||||
|
"{\"sdfasf\"}",
|
||||||
|
"{ '3fsdf' }",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_string_not_arg(self, string: str):
|
||||||
|
with pytest.raises(InvalidSyntaxException, match=re.escape(str(STRINGS_ONLY_ARGS))):
|
||||||
|
Script({"string": string}).resolve()
|
||||||
Loading…
Reference in a new issue