def test_match_type_mixed(self): datatype = Union( str, # XXX add dedicated enums Enum(int, (1, 2, 3)), Basic, Array(str), Array(int), Union(int, bool), ) param = UnionParameter(datatype) tests = [ ('spam', SimpleParameter.HANDLER(str)), (2, EnumParameter.HANDLER(int, (1, 2, 3))), (BASIC_FULL, ComplexParameter(Basic).match_type(BASIC_FULL)), (['spam'], ArrayParameter.HANDLER(Array(str))), ([], ArrayParameter.HANDLER(Array(str))), ([10], ArrayParameter.HANDLER(Array(int))), (10, SimpleParameter.HANDLER(int)), (True, SimpleParameter.HANDLER(bool)), # no match (Integer(2), None), ([True], None), ({}, None), ] for value, expected in tests: with self.subTest(value): handler = param.match_type(value) self.assertEqual(handler, expected)
def test_supported(self): handler = DatatypeHandler(str) tests = [ (Parameter(str), Parameter(str)), (handler, Parameter(str, handler)), (Fields(Field('spam')), ComplexParameter(Fields(Field('spam')))), (Field('spam'), SimpleParameter(str)), (Field('spam', str, enum={'a'}), EnumParameter(str, {'a'})), (ANY, NoopParameter()), (None, SingletonParameter(None)), (str, SimpleParameter(str)), (int, SimpleParameter(int)), (bool, SimpleParameter(bool)), (Enum(str, {'a'}), EnumParameter(str, {'a'})), (Union(str, int), UnionParameter(Union(str, int))), ({str, int}, UnionParameter(Union(str, int))), (frozenset([str, int]), UnionParameter(Union(str, int))), (Array(str), ArrayParameter(Array(str))), ([str], ArrayParameter(Array(str))), ((str, ), ArrayParameter(Array(str))), (Basic, ComplexParameter(Basic)), ] for datatype, expected in tests: with self.subTest(datatype): param = param_from_datatype(datatype) self.assertEqual(param, expected)
def test_match_type_all_simple(self): tests = [ 'spam', 10, True, ] datatype = Union(str, int, bool) param = UnionParameter(datatype) for value in tests: with self.subTest(value): handler = param.match_type(value) self.assertIs(type(handler), SimpleParameter.HANDLER) self.assertIs(handler.datatype, type(value))
def test_match_type_no_match(self): param = UnionParameter(Union(int, str)) values = [ BASIC_FULL, ['spam'], True, Integer(2), [10], {}, ] for value in values: with self.subTest(value): handler = param.match_type(value) self.assertIs(handler, None)
def test_transform_datatype_simple(self): datatypes = [ REF, TYPE_REFERENCE, ANY, None, int, str, bool, {str, int}, frozenset([str, int]), [str], (str, ), Parameter(object()), DatatypeHandler(str), Arg(SimpleParameter(str), 'spam'), SimpleParameter(str), UnionParameter(Union(str, int)), ArrayParameter(Array(str)), ComplexParameter(Fields()), NOT_SET, object(), object, type, ] for expected in datatypes: transformed = [] op = (lambda dt: transformed.append(dt) or dt) with self.subTest(expected): datatype = _transform_datatype(expected, op) self.assertIs(datatype, expected) self.assertEqual(transformed, [expected])
def test_match_type_catchall(self): NOOP = DatatypeHandler(ANY) param = UnionParameter(Union(int, str, ANY)) tests = [ ('spam', SimpleParameter.HANDLER(str)), (10, SimpleParameter.HANDLER(int)), # catchall (BASIC_FULL, NOOP), (['spam'], NOOP), (True, NOOP), (Integer(2), NOOP), ([10], NOOP), ({}, NOOP), ] for value, expected in tests: with self.subTest(value): handler = param.match_type(value) self.assertEqual(handler, expected)
def test_normalize_datatype(self): class Spam: @classmethod def normalize(cls): return OKAY OKAY = object() NOOP = object() param = SimpleParameter(str) tests = [ # explicitly handled (REF, TYPE_REFERENCE), (TYPE_REFERENCE, NOOP), (ANY, NOOP), (None, NOOP), (int, NOOP), (str, NOOP), (bool, NOOP), (Enum(str, ('spam', )), NOOP), (Union(str, int), NOOP), ({str, int}, Union(str, int)), (frozenset([str, int]), Union(str, int)), (Array(str), NOOP), ([str], Array(str)), ((str, ), Array(str)), (Mapping(str), NOOP), ({ str: str }, Mapping(str)), # others (Field('spam'), NOOP), (Fields(Field('spam')), NOOP), (param, NOOP), (DatatypeHandler(str), NOOP), (Arg(param, 'spam'), NOOP), (SimpleParameter(str), NOOP), (UnionParameter(Union(str)), NOOP), (ArrayParameter(Array(str)), NOOP), (ComplexParameter(Fields()), NOOP), (NOT_SET, NOOP), (object(), NOOP), (object, NOOP), (type, NOOP), (Spam, OKAY), ] for datatype, expected in tests: if expected is NOOP: expected = datatype with self.subTest(datatype): datatype = _normalize_datatype(datatype) self.assertEqual(datatype, expected)