map_contains
This commit is contained in:
parent
689dfe8673
commit
f9545a7e03
2 changed files with 30 additions and 0 deletions
|
|
@ -2,6 +2,7 @@ from typing import Optional
|
|||
|
||||
from ytdl_sub.script.types.map import Map
|
||||
from ytdl_sub.script.types.resolvable import AnyArgument
|
||||
from ytdl_sub.script.types.resolvable import Boolean
|
||||
from ytdl_sub.script.types.resolvable import Hashable
|
||||
from ytdl_sub.script.utils.exceptions import KeyDoesNotExistRuntimeException
|
||||
|
||||
|
|
@ -21,3 +22,10 @@ class MapFunctions:
|
|||
f"Tried to call %map_get with key {key.value}, but it does not exist"
|
||||
)
|
||||
return mapping.value[key]
|
||||
|
||||
@staticmethod
|
||||
def map_contains(mapping: Map, key: Hashable) -> Boolean:
|
||||
"""
|
||||
Returns True if the key is in the Map. False otherwise.
|
||||
"""
|
||||
return Boolean(key in mapping.value)
|
||||
|
|
|
|||
|
|
@ -46,3 +46,25 @@ class TestMapFunctions:
|
|||
"output": "{%map_get(input_map, 'dne')}",
|
||||
}
|
||||
).resolve()
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"contains_value, expected_value",
|
||||
[
|
||||
("'key'", True),
|
||||
("'dne'", False),
|
||||
("%string(%array_at(['dne', 'key'], 1))", True),
|
||||
],
|
||||
)
|
||||
def test_map_contains(self, contains_value: str, expected_value: bool):
|
||||
output = (
|
||||
Script(
|
||||
{
|
||||
"input_map": "{{'key': 'value'}}",
|
||||
"output": f"{{%map_contains(input_map, {contains_value})}}",
|
||||
}
|
||||
)
|
||||
.resolve(update=True)
|
||||
.get("output")
|
||||
.native
|
||||
)
|
||||
assert output == expected_value
|
||||
|
|
|
|||
Loading…
Reference in a new issue