Added type_util tests.

This commit is contained in:
Fabio Caccamo 2020-02-06 15:15:10 +01:00
parent eb676bcc8f
commit 78c20ca2ce
2 changed files with 253 additions and 46 deletions

View File

@ -37,8 +37,8 @@ def is_float(val):
return isinstance(val, float)
# def is_function(val):
# return callable(val)
def is_function(val):
return callable(val)
def is_integer(val):
@ -46,8 +46,8 @@ def is_integer(val):
def is_json_serializable(val):
json_types = (bool, dict, float, ) + integer_types + \
(list, tuple, set, ) + string_types
json_types = (type(None), bool, dict, float, list, tuple, set, ) + \
integer_types + string_types
return isinstance(val, json_types)
@ -59,21 +59,21 @@ def is_list_or_tuple(val):
return isinstance(val, (list, tuple, ))
# def is_none(val):
# return val is None
def is_none(val):
return val is None
# def is_not_none(val):
# return val is not None
def is_not_none(val):
return val is not None
# def is_set(val):
# return isinstance(val, set)
def is_set(val):
return isinstance(val, set)
def is_string(val):
return isinstance(val, string_types)
# def is_tuple(val):
# return isinstance(val, tuple)
def is_tuple(val):
return isinstance(val, tuple)

View File

@ -2,6 +2,9 @@
from benedict.utils import type_util
from datetime import datetime
from decimal import Decimal
import unittest
@ -12,69 +15,273 @@ class type_util_test_case(unittest.TestCase):
pass
def test_is_bool(self):
# TODO
pass
f = type_util.is_bool
self.assertFalse(f(None))
self.assertTrue(f(True))
self.assertTrue(f(False))
self.assertFalse(f(int(0)))
self.assertFalse(f(float(0.5)))
self.assertFalse(f(Decimal(0.5)))
self.assertFalse(f(datetime.now()))
self.assertFalse(f((0, 1, 2, )))
self.assertFalse(f([0, 1, 2]))
self.assertFalse(f(set([0, 1, 2])))
self.assertFalse(f({'a':0, 'b':1, 'c':2}))
self.assertFalse(f('hello world'))
self.assertFalse(f(lambda a: a))
def test_is_collection(self):
# TODO
pass
f = type_util.is_collection
self.assertFalse(f(None))
self.assertFalse(f(True))
self.assertFalse(f(False))
self.assertFalse(f(int(0)))
self.assertFalse(f(float(0.5)))
self.assertFalse(f(Decimal(0.5)))
self.assertFalse(f(datetime.now()))
self.assertTrue(f((0, 1, 2, )))
self.assertTrue(f([0, 1, 2]))
self.assertTrue(f(set([0, 1, 2])))
self.assertTrue(f({'a':0, 'b':1, 'c':2}))
self.assertFalse(f('hello world'))
self.assertFalse(f(lambda a: a))
def test_is_datetime(self):
# TODO
pass
f = type_util.is_datetime
self.assertFalse(f(None))
self.assertFalse(f(True))
self.assertFalse(f(False))
self.assertFalse(f(int(0)))
self.assertFalse(f(float(0.5)))
self.assertFalse(f(Decimal(0.5)))
self.assertTrue(f(datetime.now()))
self.assertFalse(f((0, 1, 2, )))
self.assertFalse(f([0, 1, 2]))
self.assertFalse(f(set([0, 1, 2])))
self.assertFalse(f({'a':0, 'b':1, 'c':2}))
self.assertFalse(f('hello world'))
self.assertFalse(f(lambda a: a))
def test_is_decimal(self):
# TODO
pass
f = type_util.is_decimal
self.assertFalse(f(None))
self.assertFalse(f(True))
self.assertFalse(f(False))
self.assertFalse(f(int(0)))
self.assertFalse(f(float(0.5)))
self.assertTrue(f(Decimal(0.5)))
self.assertFalse(f(datetime.now()))
self.assertFalse(f((0, 1, 2, )))
self.assertFalse(f([0, 1, 2]))
self.assertFalse(f(set([0, 1, 2])))
self.assertFalse(f({'a':0, 'b':1, 'c':2}))
self.assertFalse(f('hello world'))
self.assertFalse(f(lambda a: a))
def test_is_dict(self):
# TODO
pass
f = type_util.is_dict
self.assertFalse(f(None))
self.assertFalse(f(True))
self.assertFalse(f(False))
self.assertFalse(f(int(0)))
self.assertFalse(f(float(0.5)))
self.assertFalse(f(Decimal(0.5)))
self.assertFalse(f(datetime.now()))
self.assertFalse(f((0, 1, 2, )))
self.assertFalse(f([0, 1, 2]))
self.assertFalse(f(set([0, 1, 2])))
self.assertTrue(f({'a':0, 'b':1, 'c':2}))
self.assertFalse(f('hello world'))
self.assertFalse(f(lambda a: a))
def test_is_dict_or_list(self):
# TODO
pass
f = type_util.is_dict_or_list
self.assertFalse(f(None))
self.assertFalse(f(True))
self.assertFalse(f(False))
self.assertFalse(f(int(0)))
self.assertFalse(f(float(0.5)))
self.assertFalse(f(Decimal(0.5)))
self.assertFalse(f(datetime.now()))
self.assertFalse(f((0, 1, 2, )))
self.assertTrue(f([0, 1, 2]))
self.assertFalse(f(set([0, 1, 2])))
self.assertTrue(f({'a':0, 'b':1, 'c':2}))
self.assertFalse(f('hello world'))
self.assertFalse(f(lambda a: a))
def test_is_float(self):
# TODO
pass
f = type_util.is_float
self.assertFalse(f(None))
self.assertFalse(f(True))
self.assertFalse(f(False))
self.assertFalse(f(int(0)))
self.assertTrue(f(float(0.5)))
self.assertFalse(f(Decimal(0.5)))
self.assertFalse(f(datetime.now()))
self.assertFalse(f((0, 1, 2, )))
self.assertFalse(f([0, 1, 2]))
self.assertFalse(f(set([0, 1, 2])))
self.assertFalse(f({'a':0, 'b':1, 'c':2}))
self.assertFalse(f('hello world'))
self.assertFalse(f(lambda a: a))
def test_is_function(self):
# TODO
pass
f = type_util.is_function
self.assertFalse(f(None))
self.assertFalse(f(True))
self.assertFalse(f(False))
self.assertFalse(f(int(0)))
self.assertFalse(f(float(0.5)))
self.assertFalse(f(Decimal(0.5)))
self.assertFalse(f(datetime.now()))
self.assertFalse(f((0, 1, 2, )))
self.assertFalse(f([0, 1, 2]))
self.assertFalse(f(set([0, 1, 2])))
self.assertFalse(f({'a':0, 'b':1, 'c':2}))
self.assertFalse(f('hello world'))
self.assertTrue(f(lambda a: a))
def test_is_integer(self):
# TODO
pass
f = type_util.is_integer
self.assertFalse(f(None))
self.assertTrue(f(True))
self.assertTrue(f(False))
self.assertTrue(f(int(0)))
self.assertFalse(f(float(0.5)))
self.assertFalse(f(Decimal(0.5)))
self.assertFalse(f(datetime.now()))
self.assertFalse(f((0, 1, 2, )))
self.assertFalse(f([0, 1, 2]))
self.assertFalse(f(set([0, 1, 2])))
self.assertFalse(f({'a':0, 'b':1, 'c':2}))
self.assertFalse(f('hello world'))
self.assertFalse(f(lambda a: a))
def test_is_json_serializable(self):
# TODO
pass
f = type_util.is_json_serializable
self.assertTrue(f(None))
self.assertTrue(f(True))
self.assertTrue(f(False))
self.assertTrue(f(int(0)))
self.assertTrue(f(float(0.5)))
self.assertFalse(f(Decimal(0.5)))
self.assertFalse(f(datetime.now()))
self.assertTrue(f((0, 1, 2, )))
self.assertTrue(f([0, 1, 2]))
self.assertTrue(f(set([0, 1, 2])))
self.assertTrue(f({'a':0, 'b':1, 'c':2}))
self.assertTrue(f('hello world'))
self.assertFalse(f(lambda a: a))
def test_is_list(self):
# TODO
pass
f = type_util.is_list
self.assertFalse(f(None))
self.assertFalse(f(True))
self.assertFalse(f(False))
self.assertFalse(f(int(0)))
self.assertFalse(f(float(0.5)))
self.assertFalse(f(Decimal(0.5)))
self.assertFalse(f(datetime.now()))
self.assertFalse(f((0, 1, 2, )))
self.assertTrue(f([0, 1, 2]))
self.assertFalse(f(set([0, 1, 2])))
self.assertFalse(f({'a':0, 'b':1, 'c':2}))
self.assertFalse(f('hello world'))
self.assertFalse(f(lambda a: a))
def test_is_list_or_tuple(self):
# TODO
pass
f = type_util.is_list_or_tuple
self.assertFalse(f(None))
self.assertFalse(f(True))
self.assertFalse(f(False))
self.assertFalse(f(int(0)))
self.assertFalse(f(float(0.5)))
self.assertFalse(f(Decimal(0.5)))
self.assertFalse(f(datetime.now()))
self.assertTrue(f((0, 1, 2, )))
self.assertTrue(f([0, 1, 2]))
self.assertFalse(f(set([0, 1, 2])))
self.assertFalse(f({'a':0, 'b':1, 'c':2}))
self.assertFalse(f('hello world'))
self.assertFalse(f(lambda a: a))
def test_is_none(self):
# TODO
pass
f = type_util.is_none
self.assertTrue(f(None))
self.assertFalse(f(True))
self.assertFalse(f(False))
self.assertFalse(f(int(0)))
self.assertFalse(f(float(0.5)))
self.assertFalse(f(Decimal(0.5)))
self.assertFalse(f(datetime.now()))
self.assertFalse(f((0, 1, 2, )))
self.assertFalse(f([0, 1, 2]))
self.assertFalse(f(set([0, 1, 2])))
self.assertFalse(f({'a':0, 'b':1, 'c':2}))
self.assertFalse(f('hello world'))
self.assertFalse(f(lambda a: a))
def test_is_not_none(self):
# TODO
pass
f = type_util.is_not_none
self.assertFalse(f(None))
self.assertTrue(f(True))
self.assertTrue(f(False))
self.assertTrue(f(int(0)))
self.assertTrue(f(float(0.5)))
self.assertTrue(f(Decimal(0.5)))
self.assertTrue(f(datetime.now()))
self.assertTrue(f((0, 1, 2, )))
self.assertTrue(f([0, 1, 2]))
self.assertTrue(f(set([0, 1, 2])))
self.assertTrue(f({'a':0, 'b':1, 'c':2}))
self.assertTrue(f('hello world'))
self.assertTrue(f(lambda a: a))
def test_is_set(self):
# TODO
pass
f = type_util.is_set
self.assertFalse(f(None))
self.assertFalse(f(True))
self.assertFalse(f(False))
self.assertFalse(f(int(0)))
self.assertFalse(f(float(0.5)))
self.assertFalse(f(Decimal(0.5)))
self.assertFalse(f(datetime.now()))
self.assertFalse(f((0, 1, 2, )))
self.assertFalse(f([0, 1, 2]))
self.assertTrue(f(set([0, 1, 2])))
self.assertFalse(f({'a':0, 'b':1, 'c':2}))
self.assertFalse(f('hello world'))
self.assertFalse(f(lambda a: a))
def test_is_string(self):
# TODO
pass
f = type_util.is_string
self.assertFalse(f(None))
self.assertFalse(f(True))
self.assertFalse(f(False))
self.assertFalse(f(int(0)))
self.assertFalse(f(float(0.5)))
self.assertFalse(f(Decimal(0.5)))
self.assertFalse(f(datetime.now()))
self.assertFalse(f((0, 1, 2, )))
self.assertFalse(f([0, 1, 2]))
self.assertFalse(f(set([0, 1, 2])))
self.assertFalse(f({'a':0, 'b':1, 'c':2}))
self.assertTrue(f('hello world'))
self.assertFalse(f(lambda a: a))
def test_is_tuple(self):
# TODO
pass
f = type_util.is_tuple
self.assertFalse(f(None))
self.assertFalse(f(True))
self.assertFalse(f(False))
self.assertFalse(f(int(0)))
self.assertFalse(f(float(0.5)))
self.assertFalse(f(Decimal(0.5)))
self.assertFalse(f(datetime.now()))
self.assertTrue(f((0, 1, 2, )))
self.assertFalse(f([0, 1, 2]))
self.assertFalse(f(set([0, 1, 2])))
self.assertFalse(f({'a':0, 'b':1, 'c':2}))
self.assertFalse(f('hello world'))
self.assertFalse(f(lambda a: a))