def test_bad_choices(self): class String(str): pass with self.assertRaises(ValueError): Enum(str, 'spam') with self.assertRaises(TypeError): Enum(str, ()) with self.assertRaises(ValueError): Enum(str, ('spam', 10)) with self.assertRaises(ValueError): Enum(str, ('spam', String))
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_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)
def test_enum(self): field = Field('spam', str, enum=('a', 'b', 'c')) self.assertEqual(field.datatype, Enum(str, ('a', 'b', 'c')))
def test_bad_datatype(self): with self.assertRaises(ValueError): Enum('spam', ('spam', 'eggs')) with self.assertRaises(ValueError): Enum(dict, ('spam', 'eggs'))
def test_attrs(self): enum = Enum(str, ('spam', 'eggs')) datatype, choices = enum self.assertIs(datatype, str) self.assertEqual(choices, frozenset(['spam', 'eggs']))