consolidate cast tests
This commit is contained in:
parent
4946c03abb
commit
552bc6c05e
9 changed files with 63 additions and 56 deletions
|
|
@ -5,10 +5,16 @@ from ytdl_sub.script.types.resolvable import Boolean
|
||||||
class BooleanFunctions:
|
class BooleanFunctions:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def bool(value: AnyType) -> Boolean:
|
def bool(value: AnyType) -> Boolean:
|
||||||
|
"""
|
||||||
|
Cast any type to a boolean
|
||||||
|
"""
|
||||||
return Boolean(bool(value.value))
|
return Boolean(bool(value.value))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def equals(left: AnyType, right: AnyType) -> Boolean:
|
def equals(left: AnyType, right: AnyType) -> Boolean:
|
||||||
|
"""
|
||||||
|
Returns True if left equals right. False otherwise.
|
||||||
|
"""
|
||||||
return Boolean(left.value == right.value)
|
return Boolean(left.value == right.value)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,11 @@
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
from ytdl_sub.script.types.resolvable import AnyType
|
from ytdl_sub.script.types.resolvable import AnyType
|
||||||
|
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 Integer
|
from ytdl_sub.script.types.resolvable import Integer
|
||||||
from ytdl_sub.script.types.resolvable import Numeric
|
from ytdl_sub.script.types.resolvable import Numeric
|
||||||
|
from ytdl_sub.script.types.resolvable import String
|
||||||
|
|
||||||
|
|
||||||
def _to_numeric(value: int | float) -> Numeric:
|
def _to_numeric(value: int | float) -> Numeric:
|
||||||
|
|
@ -12,11 +16,11 @@ def _to_numeric(value: int | float) -> Numeric:
|
||||||
|
|
||||||
class NumericFunctions:
|
class NumericFunctions:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def float(value: AnyType) -> Float:
|
def float(value: Union[Float, Integer, Boolean, String]) -> Float:
|
||||||
return Float(value=float(value.value))
|
return Float(value=float(value.value))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def int(value: AnyType) -> Integer:
|
def int(value: Union[Float, Integer, Boolean, String]) -> Integer:
|
||||||
return Integer(value=int(value.value))
|
return Integer(value=int(value.value))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
@ -36,8 +40,8 @@ class NumericFunctions:
|
||||||
return _to_numeric(left.value / right.value)
|
return _to_numeric(left.value / right.value)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def mod(value: Integer, modulo: Integer) -> Integer:
|
def mod(value: Numeric, modulo: Numeric) -> Numeric:
|
||||||
return Integer(value=value.value % modulo.value)
|
return _to_numeric(value=value.value % modulo.value)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def max(left: Numeric, right: Numeric) -> Numeric:
|
def max(left: Numeric, right: Numeric) -> Numeric:
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ from ytdl_sub.script.types.resolvable import Resolvable
|
||||||
from ytdl_sub.script.types.variable import Variable
|
from ytdl_sub.script.types.variable import Variable
|
||||||
from ytdl_sub.script.utils.exceptions import UNREACHABLE
|
from ytdl_sub.script.utils.exceptions import UNREACHABLE
|
||||||
|
|
||||||
|
# pylint: disable=missing-raises-doc
|
||||||
|
|
||||||
def is_union(arg_type: Type) -> bool:
|
def is_union(arg_type: Type) -> bool:
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -24,18 +24,6 @@ class TestArray:
|
||||||
"array": String('str: ["a", 3.14]')
|
"array": String('str: ["a", 3.14]')
|
||||||
}
|
}
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
|
||||||
"array, expected_bool",
|
|
||||||
[
|
|
||||||
("{%bool([])}", False),
|
|
||||||
("{%bool([False])}", True),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
def test_return_as_bool(self, array: str, expected_bool: bool):
|
|
||||||
assert Script({"array_as_bool": array}).resolve() == {
|
|
||||||
"array_as_bool": Boolean(expected_bool)
|
|
||||||
}
|
|
||||||
|
|
||||||
def test_nested_array(self):
|
def test_nested_array(self):
|
||||||
assert Script(
|
assert Script(
|
||||||
{"array": "{['level1', ['level2', ['level3', 'level3'], 'level2'], 'level1']}"}
|
{"array": "{['level1', ['level2', ['level3', 'level3'], 'level2'], 'level1']}"}
|
||||||
|
|
|
||||||
|
|
@ -37,3 +37,23 @@ class TestBool:
|
||||||
"boolean": Boolean(expected_boolean),
|
"boolean": Boolean(expected_boolean),
|
||||||
"as_string": String(str(expected_boolean)),
|
"as_string": String(str(expected_boolean)),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"to_cast, expected_bool",
|
||||||
|
[
|
||||||
|
("{%bool(False)}", False),
|
||||||
|
("{%bool(True)}", True),
|
||||||
|
("{%bool(0)}", False),
|
||||||
|
("{%bool(1)}", True),
|
||||||
|
("{%bool(0.0)}", False),
|
||||||
|
("{%bool(0.1)}", True),
|
||||||
|
("{%bool('')}", False),
|
||||||
|
("{%bool('false')}", True),
|
||||||
|
("{%bool([])}", False),
|
||||||
|
("{%bool([False])}", True),
|
||||||
|
("{%bool({})}", False),
|
||||||
|
("{%bool({'key': 'value'})}", True),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_cast_as_bool(self, to_cast: str, expected_bool: bool):
|
||||||
|
assert Script({"as_bool": to_cast}).resolve() == {"as_bool": Boolean(expected_bool)}
|
||||||
|
|
|
||||||
|
|
@ -44,16 +44,6 @@ class TestFloat:
|
||||||
"as_string": String(str(expected_float)),
|
"as_string": String(str(expected_float)),
|
||||||
}
|
}
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
|
||||||
"float_, expected_bool",
|
|
||||||
[
|
|
||||||
("{%bool(0.0)}", False),
|
|
||||||
("{%bool(0.1)}", True),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
def test_return_as_bool(self, float_: str, expected_bool: bool):
|
|
||||||
assert Script({"as_bool": float_}).resolve() == {"as_bool": Boolean(expected_bool)}
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"float_",
|
"float_",
|
||||||
[
|
[
|
||||||
|
|
@ -68,3 +58,17 @@ class TestFloat:
|
||||||
def test_invalid_float(self, float_: str):
|
def test_invalid_float(self, float_: str):
|
||||||
with pytest.raises(InvalidSyntaxException, match=re.escape(str(NUMERICS_INVALID_CHAR))):
|
with pytest.raises(InvalidSyntaxException, match=re.escape(str(NUMERICS_INVALID_CHAR))):
|
||||||
Script({"float": float_}).resolve()
|
Script({"float": float_}).resolve()
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"to_cast, expected_float",
|
||||||
|
[
|
||||||
|
("{%float(5)}", 5.0),
|
||||||
|
("{%float(0.9)}", 0.9),
|
||||||
|
("{%float(-3.00)}", -3.0),
|
||||||
|
("{%float(True)}", 1.0),
|
||||||
|
("{%float(False)}", 0.0),
|
||||||
|
("{%float('142.43')}", 142.43),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_cast_as_float(self, to_cast: str, expected_float: float):
|
||||||
|
assert Script({"as_float": to_cast}).resolve() == {"as_float": Float(expected_float)}
|
||||||
|
|
|
||||||
|
|
@ -44,16 +44,6 @@ class TestInteger:
|
||||||
"as_string": String(str(expected_integer)),
|
"as_string": String(str(expected_integer)),
|
||||||
}
|
}
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
|
||||||
"integer, expected_bool",
|
|
||||||
[
|
|
||||||
("{%bool(0)}", False),
|
|
||||||
("{%bool(1)}", True),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
def test_return_as_bool(self, integer: str, expected_bool: bool):
|
|
||||||
assert Script({"as_bool": integer}).resolve() == {"as_bool": Boolean(expected_bool)}
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"integer",
|
"integer",
|
||||||
[
|
[
|
||||||
|
|
@ -68,3 +58,17 @@ class TestInteger:
|
||||||
def test_invalid_integer(self, integer: str):
|
def test_invalid_integer(self, integer: str):
|
||||||
with pytest.raises(InvalidSyntaxException, match=re.escape(str(NUMERICS_INVALID_CHAR))):
|
with pytest.raises(InvalidSyntaxException, match=re.escape(str(NUMERICS_INVALID_CHAR))):
|
||||||
Script({"integer": integer}).resolve()
|
Script({"integer": integer}).resolve()
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"to_cast, expected_int",
|
||||||
|
[
|
||||||
|
("{%int(5)}", 5),
|
||||||
|
("{%int(0.9)}", 0),
|
||||||
|
("{%int(-3.00)}", -3),
|
||||||
|
("{%int(True)}", 1),
|
||||||
|
("{%int(False)}", 0),
|
||||||
|
("{%int('142')}", 142),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_cast_as_integer(self, to_cast: str, expected_int: int):
|
||||||
|
assert Script({"as_int": to_cast}).resolve() == {"as_int": Integer(expected_int)}
|
||||||
|
|
|
||||||
|
|
@ -28,16 +28,6 @@ class TestMap:
|
||||||
"map": String('json: {"a": 3.14}')
|
"map": String('json: {"a": 3.14}')
|
||||||
}
|
}
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
|
||||||
"map_, expected_bool",
|
|
||||||
[
|
|
||||||
("{%bool({})}", False),
|
|
||||||
("{%bool({'key': 'value'})}", True),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
def test_return_as_bool(self, map_: str, expected_bool: bool):
|
|
||||||
assert Script({"as_bool": map_}).resolve() == {"as_bool": Boolean(expected_bool)}
|
|
||||||
|
|
||||||
def test_nested_map(self):
|
def test_nested_map(self):
|
||||||
map_str = """{
|
map_str = """{
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -49,16 +49,6 @@ class TestString:
|
||||||
def test_string(self, string: str, expected_string: str):
|
def test_string(self, string: str, expected_string: str):
|
||||||
assert Script({"string": string}).resolve() == {"string": String(expected_string)}
|
assert Script({"string": string}).resolve() == {"string": String(expected_string)}
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
|
||||||
"string, expected_bool",
|
|
||||||
[
|
|
||||||
("{%bool('')}", False),
|
|
||||||
("{%bool('false')}", True),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
def test_return_as_bool(self, string: str, expected_bool: bool):
|
|
||||||
assert Script({"as_bool": string}).resolve() == {"as_bool": Boolean(expected_bool)}
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"string",
|
"string",
|
||||||
[
|
[
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue