passthrough assert
This commit is contained in:
parent
c0f6eca3c6
commit
80c32335fb
5 changed files with 45 additions and 20 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
from ytdl_sub.script.types.resolvable import AnyArgument
|
from ytdl_sub.script.types.resolvable import AnyArgument
|
||||||
from ytdl_sub.script.types.resolvable import Boolean
|
from ytdl_sub.script.types.resolvable import ReturnableArgument
|
||||||
from ytdl_sub.script.types.resolvable import String
|
from ytdl_sub.script.types.resolvable import String
|
||||||
from ytdl_sub.script.utils.exceptions import UserThrownRuntimeError
|
from ytdl_sub.script.utils.exceptions import UserThrownRuntimeError
|
||||||
|
|
||||||
|
|
@ -13,10 +13,11 @@ class ErrorFunctions:
|
||||||
raise UserThrownRuntimeError(error_message)
|
raise UserThrownRuntimeError(error_message)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def assert_(condition: Boolean, assert_message: String) -> Boolean:
|
def assert_(value: ReturnableArgument, assert_message: String) -> ReturnableArgument:
|
||||||
"""
|
"""
|
||||||
Explicitly throw an error with the provided assert message if ``condition`` is False.
|
Explicitly throw an error with the provided assert message if ``value`` evaluates to False.
|
||||||
|
If it evaluates to True, it will return ``value``.
|
||||||
"""
|
"""
|
||||||
if not condition.value:
|
if not bool(value.value):
|
||||||
raise UserThrownRuntimeError(assert_message)
|
raise UserThrownRuntimeError(assert_message)
|
||||||
return condition
|
return value
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@ from typing import Set
|
||||||
from ytdl_sub.script.functions import Functions
|
from ytdl_sub.script.functions import Functions
|
||||||
from ytdl_sub.script.parser import parse
|
from ytdl_sub.script.parser import parse
|
||||||
from ytdl_sub.script.types.resolvable import Lambda
|
from ytdl_sub.script.types.resolvable import Lambda
|
||||||
from ytdl_sub.script.types.resolvable import NamedArgument
|
|
||||||
from ytdl_sub.script.types.resolvable import Resolvable
|
from ytdl_sub.script.types.resolvable import Resolvable
|
||||||
from ytdl_sub.script.types.syntax_tree import SyntaxTree
|
from ytdl_sub.script.types.syntax_tree import SyntaxTree
|
||||||
from ytdl_sub.script.types.variable import Variable
|
from ytdl_sub.script.types.variable import Variable
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,15 @@ class Argument(NamedType, ABC):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class ValueArgument(Argument, ABC):
|
||||||
|
"""
|
||||||
|
Argument that has a value
|
||||||
|
"""
|
||||||
|
|
||||||
|
value: Any
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
class NamedArgument(Argument, ABC):
|
class NamedArgument(Argument, ABC):
|
||||||
"""
|
"""
|
||||||
|
|
@ -41,34 +50,32 @@ class NamedArgument(Argument, ABC):
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
class ReturnableArgument(NamedType, ABC):
|
class ReturnableArgument(ValueArgument, NamedType, ABC):
|
||||||
"""
|
"""
|
||||||
AnyType to express generics in functions that are part of the return type
|
AnyType to express generics in functions that are part of the return type
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
class ReturnableArgumentA(NamedType, ABC):
|
class ReturnableArgumentA(ValueArgument, NamedType, ABC):
|
||||||
"""
|
"""
|
||||||
AnyType to express generics in functions when more than one are present (i.e. `if`)
|
AnyType to express generics in functions when more than one are present (i.e. `if`)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
class ReturnableArgumentB(NamedType, ABC):
|
class ReturnableArgumentB(ValueArgument, NamedType, ABC):
|
||||||
"""
|
"""
|
||||||
AnyType to express generics in functions when more than one are present (i.e. `if`)
|
AnyType to express generics in functions when more than one are present (i.e. `if`)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
class AnyArgument(Argument, ReturnableArgument, ReturnableArgumentA, ReturnableArgumentB, ABC):
|
class AnyArgument(ReturnableArgument, ReturnableArgumentA, ReturnableArgumentB, ABC):
|
||||||
"""
|
"""
|
||||||
Human-readable name for FutureResolvable
|
Human-readable name for FutureResolvable
|
||||||
"""
|
"""
|
||||||
|
|
||||||
value: Any
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
class Resolvable(AnyArgument, ABC):
|
class Resolvable(AnyArgument, ABC):
|
||||||
|
|
|
||||||
26
tests/unit/script/functions/test_error_functions.py
Normal file
26
tests/unit/script/functions/test_error_functions.py
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
import re
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from ytdl_sub.script.script import Script
|
||||||
|
from ytdl_sub.script.utils.exceptions import UserThrownRuntimeError
|
||||||
|
|
||||||
|
|
||||||
|
class TestErrorFunctions:
|
||||||
|
def test_user_throw(self):
|
||||||
|
with pytest.raises(UserThrownRuntimeError, match=re.escape("test this error message")):
|
||||||
|
Script({"throw_error": "{%throw('test this error message')}"}).resolve()
|
||||||
|
|
||||||
|
def test_user_assert_raises(self):
|
||||||
|
with pytest.raises(UserThrownRuntimeError, match=re.escape("test this error message")):
|
||||||
|
Script({"throw_error": "{%assert(False, 'test this error message')}"}).resolve()
|
||||||
|
|
||||||
|
def test_user_assert_passthrough(self):
|
||||||
|
output = (
|
||||||
|
Script({"output": "{%assert(['a'], 'test this error message')}"})
|
||||||
|
.resolve(update=True)
|
||||||
|
.get("output")
|
||||||
|
.native
|
||||||
|
)
|
||||||
|
|
||||||
|
assert output == ["a"]
|
||||||
|
|
@ -93,14 +93,6 @@ class TestFunction:
|
||||||
):
|
):
|
||||||
Script({"divide_by_zero": "{%div(8820, 0)}"}).resolve()
|
Script({"divide_by_zero": "{%div(8820, 0)}"}).resolve()
|
||||||
|
|
||||||
def test_user_throw(self):
|
|
||||||
with pytest.raises(UserThrownRuntimeError, match=re.escape("test this error message")):
|
|
||||||
Script({"throw_error": "{%throw('test this error message')}"}).resolve()
|
|
||||||
|
|
||||||
def test_user_assert(self):
|
|
||||||
with pytest.raises(UserThrownRuntimeError, match=re.escape("test this error message")):
|
|
||||||
Script({"throw_error": "{%assert(False, 'test this error message')}"}).resolve()
|
|
||||||
|
|
||||||
def test_function_does_not_exist(self):
|
def test_function_does_not_exist(self):
|
||||||
with pytest.raises(
|
with pytest.raises(
|
||||||
FunctionDoesNotExist,
|
FunctionDoesNotExist,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue