210: Type.__init__() now takes the type as the first argument, does not use subclass attribute; updated Int, Unicode, and their unit tests accordingly

This commit is contained in:
Jason Gerard DeRose 2008-08-28 02:02:03 +00:00
parent 2b01bdc112
commit 039b9a2a9b
2 changed files with 21 additions and 17 deletions

View File

@ -51,7 +51,14 @@ class Type(ReadOnly):
Base class for all IPA types.
"""
type = None # Override in base class
def __init__(self, type_):
allowed = (bool, int, float, unicode)
if type_ not in allowed:
raise ValueError(
'type_ must be in %r, got %r' % (type_, allowed)
)
self.type = type_
lock(self)
def __get_name(self):
"""
@ -75,13 +82,11 @@ class Type(ReadOnly):
class Int(Type):
type = int
def __init__(self, min_value=None, max_value=None):
check_min_max(min_value, max_value, 'min_value', 'max_value')
self.min_value = min_value
self.max_value = max_value
lock(self)
super(Int, self).__init__(int)
def validate(self, value):
if type(value) is not self.type:
@ -93,8 +98,6 @@ class Int(Type):
class Unicode(Type):
type = unicode
def __init__(self, min_length=None, max_length=None, pattern=None):
check_min_max(min_length, max_length, 'min_length', 'max_length')
if min_length is not None and min_length < 0:
@ -112,7 +115,7 @@ class Unicode(Type):
self.regex = None
else:
self.regex = re.compile(pattern)
lock(self)
super(Unicode, self).__init__(unicode)
def validate(self, value):
if type(value) is not self.type:

View File

@ -75,13 +75,14 @@ class test_Int(ClassChecker):
def test_class(self):
assert self.cls.__bases__ == (ipa_types.Type,)
assert self.cls.type is int
def test_init(self):
o = self.cls()
assert o.name == 'Int'
assert o.min_value is None
assert o.max_value is None
assert o.__islocked__() is True
assert read_only(o, 'type') is int
assert read_only(o, 'name') == 'Int'
assert read_only(o, 'min_value') is None
assert read_only(o, 'max_value') is None
okay = [
(None, -5),
@ -122,7 +123,6 @@ class test_Int(ClassChecker):
'min_value > max_value: min_value=%d, max_value=%d' % (l, h)
)
def test_call(self):
o = self.cls()
@ -168,15 +168,16 @@ class test_Unicode(ClassChecker):
def test_class(self):
assert self.cls.__bases__ == (ipa_types.Type,)
assert self.cls.type is unicode
def test_init(self):
o = self.cls()
assert o.__islocked__() is True
assert read_only(o, 'type') is unicode
assert read_only(o, 'name') == 'Unicode'
assert o.min_length is None
assert o.max_length is None
assert o.pattern is None
assert o.regex is None
assert read_only(o, 'min_length') is None
assert read_only(o, 'max_length') is None
assert read_only(o, 'pattern') is None
assert read_only(o, 'regex') is None
# Test min_length, max_length:
okay = (