variable does not exist
This commit is contained in:
parent
1ccee475c3
commit
120c965d4f
4 changed files with 62 additions and 1 deletions
|
|
@ -8,6 +8,7 @@ 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
|
||||||
from ytdl_sub.script.utils.exceptions import UNREACHABLE
|
from ytdl_sub.script.utils.exceptions import UNREACHABLE
|
||||||
from ytdl_sub.script.utils.exceptions import CycleDetected
|
from ytdl_sub.script.utils.exceptions import CycleDetected
|
||||||
|
from ytdl_sub.script.utils.exceptions import VariableDoesNotExist
|
||||||
|
|
||||||
# pylint: disable=missing-raises-doc
|
# pylint: disable=missing-raises-doc
|
||||||
|
|
||||||
|
|
@ -38,6 +39,11 @@ class Script:
|
||||||
deps: List[str],
|
deps: List[str],
|
||||||
) -> None:
|
) -> None:
|
||||||
for dep in variable_dependency.variables:
|
for dep in variable_dependency.variables:
|
||||||
|
if dep.name not in self._variables:
|
||||||
|
raise VariableDoesNotExist(
|
||||||
|
f"{variable_name} uses the variable {dep.name} which does not exist."
|
||||||
|
)
|
||||||
|
|
||||||
if variable_name in deps + [dep.name]:
|
if variable_name in deps + [dep.name]:
|
||||||
cycle_deps = [variable_name] + deps + [dep.name]
|
cycle_deps = [variable_name] + deps + [dep.name]
|
||||||
cycle_deps_str = " -> ".join(cycle_deps)
|
cycle_deps_str = " -> ".join(cycle_deps)
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,10 @@ class FunctionDoesNotExist(UserException):
|
||||||
"""Tried to use a function that does not exist"""
|
"""Tried to use a function that does not exist"""
|
||||||
|
|
||||||
|
|
||||||
|
class VariableDoesNotExist(UserException):
|
||||||
|
"""Tried to use a variable that does not exist"""
|
||||||
|
|
||||||
|
|
||||||
class CycleDetected(UserException):
|
class CycleDetected(UserException):
|
||||||
"""A cycle exists within a user's script"""
|
"""A cycle exists within a user's script"""
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ import pytest
|
||||||
from ytdl_sub.script.parser import STRINGS_NOT_CLOSED
|
from ytdl_sub.script.parser import STRINGS_NOT_CLOSED
|
||||||
from ytdl_sub.script.parser import STRINGS_ONLY_ARGS
|
from ytdl_sub.script.parser import STRINGS_ONLY_ARGS
|
||||||
from ytdl_sub.script.script import Script
|
from ytdl_sub.script.script import Script
|
||||||
from ytdl_sub.script.types.resolvable import Boolean
|
|
||||||
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
|
||||||
|
|
||||||
|
|
|
||||||
52
tests/unit/script/types/test_variable.py
Normal file
52
tests/unit/script/types/test_variable.py
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
import re
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from ytdl_sub.script.script import Script
|
||||||
|
from ytdl_sub.script.types.resolvable import String
|
||||||
|
from ytdl_sub.script.utils.exceptions import CycleDetected
|
||||||
|
from ytdl_sub.script.utils.exceptions import VariableDoesNotExist
|
||||||
|
|
||||||
|
|
||||||
|
class TestVariable:
|
||||||
|
def test_simple(self):
|
||||||
|
assert Script({"a": "a", "b": "{b_}", "b_": "b"}).resolve() == {
|
||||||
|
"a": String("a"),
|
||||||
|
"b": String("b"),
|
||||||
|
"b_": String("b"),
|
||||||
|
}
|
||||||
|
|
||||||
|
def test_multiple_variables(self):
|
||||||
|
assert Script({"a": "a", "b": "b", "b_": " {a} {b} "}).resolve() == {
|
||||||
|
"a": String("a"),
|
||||||
|
"b": String("b"),
|
||||||
|
"b_": String(" a b "),
|
||||||
|
}
|
||||||
|
|
||||||
|
def test_simple_with_function(self):
|
||||||
|
assert Script({"a": "a", "b": "{%capitalize(b_)}", "b_": "b"}).resolve() == {
|
||||||
|
"a": String("a"),
|
||||||
|
"b": String("B"),
|
||||||
|
"b_": String("b"),
|
||||||
|
}
|
||||||
|
|
||||||
|
def test_simple_cycle(self):
|
||||||
|
with pytest.raises(
|
||||||
|
CycleDetected,
|
||||||
|
match=re.escape("Cycle detected within these variables: a -> b -> a"),
|
||||||
|
):
|
||||||
|
Script({"a": "{b}", "b": "{a}"}).resolve()
|
||||||
|
|
||||||
|
def test_simple_cycle_with_function(self):
|
||||||
|
with pytest.raises(
|
||||||
|
CycleDetected,
|
||||||
|
match=re.escape("Cycle detected within these variables: b -> b_ -> b"),
|
||||||
|
):
|
||||||
|
Script({"b": "{%capitalize(b_)}", "b_": "{b}"}).resolve()
|
||||||
|
|
||||||
|
def test_undefined_variable(self):
|
||||||
|
with pytest.raises(
|
||||||
|
VariableDoesNotExist,
|
||||||
|
match=re.escape("b uses the variable c which does not exist."),
|
||||||
|
):
|
||||||
|
Script({"a": "a", "b": "{c}"}).resolve()
|
||||||
Loading…
Reference in a new issue