array functions, need to validate lambda arg numb

This commit is contained in:
Jesse Bannon 2023-11-23 22:20:07 -08:00
parent 01fcad2561
commit fb02479108
2 changed files with 88 additions and 18 deletions

View file

@ -17,7 +17,7 @@ class ArrayFunctions:
for array in arrays: for array in arrays:
output.extend(array.value) output.extend(array.value)
return Array(output) return ResolvedArray(output)
@staticmethod @staticmethod
def array_at(array: Array, idx: Integer) -> Resolvable: def array_at(array: Array, idx: Integer) -> Resolvable:
@ -38,14 +38,14 @@ class ArrayFunctions:
else: else:
output.append(elem) output.append(elem)
return Array(output) return ResolvedArray(output)
@staticmethod @staticmethod
def array_reverse(array: Array) -> Array: def array_reverse(array: Array) -> Array:
""" """
Reverse an Array. Reverse an Array.
""" """
return Array(list(reversed(array.value))) return ResolvedArray(list(reversed(array.value)))
# pylint: disable=unused-argument # pylint: disable=unused-argument

View file

@ -3,18 +3,88 @@ from ytdl_sub.script.script import Script
class TestArrayFunctions: class TestArrayFunctions:
def test_array_extend(self): def test_array_extend(self):
assert Script( output = (
Script(
{ {
"array1": "{['a', 3.14]}", "array1": "{['a']}",
"array2": "{['b', 8.8]}", "array2": "{['b']}",
"array3": "{['c', 3.17]}", "array3": "{['c']}",
"array_extended_output": "{%array_extend(array1, array2, array3)}", "output": "{%array_extend(array1, array2, array3)}",
} }
).resolve(update=True).get("array_extended_output").native == [ )
"a", .resolve(update=True)
3.14, .get("output")
"b", .native
8.8, )
"c", assert output == ["a", "b", "c"]
3.17,
] def test_array_at(self):
output = (
Script(
{
"array1": "{['a', 'b', 'c']}",
"output": "{%array_at(array1, 1)}",
}
)
.resolve(update=True)
.get("output")
.native
)
assert output == "b"
def test_array_flatten(self):
output = (
Script(
{
"array1": "{['a', ['b'], [['c']]]}",
"output": "{%array_flatten(array1)}",
}
)
.resolve(update=True)
.get("output")
.native
)
assert output == ["a", "b", "c"]
def test_array_reverse(self):
output = (
Script(
{
"array1": "{['a', 'b', 'c']}",
"output": "{%array_reverse(array1)}",
}
)
.resolve(update=True)
.get("output")
.native
)
assert output == ["c", "b", "a"]
def test_array_apply(self):
output = (
Script(
{
"array1": "{['a', 'b', 'c']}",
"output": "{%array_apply(array1, %capitalize)}",
}
)
.resolve(update=True)
.get("output")
.native
)
assert output == ["A", "B", "C"]
def test_array_enumerate(self):
output = (
Script(
{
"%enumerate_output": "{[$0, $1]}",
"array1": "{['a', 'b', 'c']}",
"output": "{%array_apply(array1, %enumerate_output)}",
}
)
.resolve(update=True)
.get("output")
.native
)
assert output == [[0, "a"], [1, "b"], [2, "c"]]