[DEV] Add type-check functions (#905)

This commit is contained in:
Jesse Bannon 2024-01-18 23:24:10 -08:00 committed by GitHub
parent 29398862da
commit 213580ee84
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 138 additions and 2 deletions

View file

@ -172,6 +172,41 @@ gte
:description:
``>=`` operator. Returns True if left >= right. False otherwise.
is_array
~~~~~~~~
:spec: ``is_array(value: AnyArgument) -> Boolean``
:description:
Returns True if a value is a Map. False otherwise.
is_bool
~~~~~~~
:spec: ``is_bool(value: AnyArgument) -> Boolean``
:description:
Returns True if a value is a Float. False otherwise.
is_float
~~~~~~~~
:spec: ``is_float(value: AnyArgument) -> Boolean``
:description:
Returns True if a value is a Float. False otherwise.
is_int
~~~~~~
:spec: ``is_int(value: AnyArgument) -> Boolean``
:description:
Returns True if a value is an Integer. False otherwise.
is_map
~~~~~~
:spec: ``is_map(value: AnyArgument) -> Boolean``
:description:
Returns True if a value is a Map. False otherwise.
is_null
~~~~~~~
:spec: ``is_null(value: AnyArgument) -> Boolean``
@ -179,6 +214,20 @@ is_null
:description:
Returns True if a value is null (i.e. an empty string). False otherwise.
is_numeric
~~~~~~~~~~
:spec: ``is_numeric(value: AnyArgument) -> Boolean``
:description:
Returns True if a value is either an Integer or Float. False otherwise.
is_string
~~~~~~~~~
:spec: ``is_string(value: AnyArgument) -> Boolean``
:description:
Returns True if a value is a String. False otherwise.
lt
~~
:spec: ``lt(left: AnyArgument, right: AnyArgument) -> Boolean``

View file

@ -1,5 +1,9 @@
from ytdl_sub.script.types.array import Array
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 Float
from ytdl_sub.script.types.resolvable import Integer
from ytdl_sub.script.types.resolvable import String
# pylint: disable=invalid-name
@ -107,3 +111,59 @@ class BooleanFunctions:
Returns True if a value is null (i.e. an empty string). False otherwise.
"""
return Boolean(isinstance(value, String) and value.value == "")
@staticmethod
def is_map(value: AnyArgument) -> Boolean:
"""
:description:
Returns True if a value is a Map. False otherwise.
"""
return Boolean(isinstance(value, Map))
@staticmethod
def is_array(value: AnyArgument) -> Boolean:
"""
:description:
Returns True if a value is a Map. False otherwise.
"""
return Boolean(isinstance(value, Array))
@staticmethod
def is_string(value: AnyArgument) -> Boolean:
"""
:description:
Returns True if a value is a String. False otherwise.
"""
return Boolean(isinstance(value, String))
@staticmethod
def is_numeric(value: AnyArgument) -> Boolean:
"""
:description:
Returns True if a value is either an Integer or Float. False otherwise.
"""
return Boolean(isinstance(value, (Integer, Float)))
@staticmethod
def is_int(value: AnyArgument) -> Boolean:
"""
:description:
Returns True if a value is an Integer. False otherwise.
"""
return Boolean(isinstance(value, Integer))
@staticmethod
def is_float(value: AnyArgument) -> Boolean:
"""
:description:
Returns True if a value is a Float. False otherwise.
"""
return Boolean(isinstance(value, Float))
@staticmethod
def is_bool(value: AnyArgument) -> Boolean:
"""
:description:
Returns True if a value is a Float. False otherwise.
"""
return Boolean(isinstance(value, Boolean))

View file

@ -1,8 +1,6 @@
import pytest
from unit.script.conftest import single_variable_output
from ytdl_sub.script.script import Script
class TestBooleanFunctions:
@pytest.mark.parametrize(
@ -140,3 +138,32 @@ class TestBooleanFunctions:
def test_is_null(self, value: str, expected_output: bool):
output = single_variable_output(f"{{%is_null({value})}}")
assert output == expected_output
def test_is_array(self):
assert single_variable_output("{ %is_array( [] ) }") is True
assert single_variable_output("{ %is_array( {} ) }") is False
def test_is_map(self):
assert single_variable_output("{ %is_map( {} ) }") is True
assert single_variable_output("{ %is_map( [] ) }") is False
def test_is_string(self):
assert single_variable_output("{ %is_string( 'hi' ) }") is True
assert single_variable_output("{ %is_string( False ) }") is False
def test_is_bool(self):
assert single_variable_output("{ %is_bool( True ) }") is True
assert single_variable_output("{ %is_bool( 0 ) }") is False
def test_is_int(self):
assert single_variable_output("{ %is_int( 2 ) }") is True
assert single_variable_output("{ %is_int( 2.3 ) }") is False
def test_is_float(self):
assert single_variable_output("{ %is_float( 3.14 ) }") is True
assert single_variable_output("{ %is_float( 4 ) }") is False
def test_is_numeric(self):
assert single_variable_output("{ %is_numeric( 4 ) }") is True
assert single_variable_output("{ %is_numeric( 2.34 ) }") is True
assert single_variable_output("{ %is_numeric( '3.12' ) }") is False