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_validate_use_coerced(self): handler = FakeHandler() other = Arg(Parameter(str, handler), 'spam', handler, israw=False) arg = Arg(Parameter(str, self.handler), other, self.handler, israw=False) arg.validate() self.assertEqual(self.stub.calls, []) self.assertEqual(handler.stub.calls, [ ('validate', ('spam',), {}), ])
def test_as_data_use_coerced(self): handler = FakeHandler() other = Arg(Parameter(str, handler), 'spam', handler, israw=False) handler.returns.as_data = 'spam' arg = Arg(Parameter(str, self.handler), other, self.handler, israw=False) data = arg.as_data(other) self.assertEqual(data, 'spam') self.assertEqual(self.stub.calls, []) self.assertEqual(handler.stub.calls, [ ('validate', ('spam',), {}), ('as_data', ('spam',), {}), ])
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 setUp(self): super().setUp() self.stub = Stub() self.handler = FakeHandler(str, self.stub) self.param = Parameter(str, self.handler)
def test_match_type_matched(self): param = Parameter(str, self.handler) matched = param.match_type('spam') self.assertIs(matched, self.handler) self.assertEqual(self.stub.calls, [])
def test_match_type_no_match(self): param = Parameter(str) matched = param.match_type('spam') self.assertIs(matched, None) self.assertEqual(self.stub.calls, [])
def test_bind_no_match(self): param = Parameter(str) arg = param.bind('spam') self.assertIs(arg, None) self.assertEqual(self.stub.calls, [])
def test_bind_matched(self): param = Parameter(str, self.handler) arg = param.bind('spam') self.assertEqual(arg, Arg(param, 'spam', self.handler)) self.assertEqual(self.stub.calls, [])