diff options
-rw-r--r-- | ipalib/ipa_types.py | 6 | ||||
-rw-r--r-- | ipalib/tests/test_ipa_types.py | 18 |
2 files changed, 21 insertions, 3 deletions
diff --git a/ipalib/ipa_types.py b/ipalib/ipa_types.py index 8d30aaa4..fe35a9ac 100644 --- a/ipalib/ipa_types.py +++ b/ipalib/ipa_types.py @@ -52,11 +52,11 @@ class Type(ReadOnly): """ def __init__(self, type_): + if type(type_) is not type: + raise TypeError('%r is not %r' % (type(type_), type)) allowed = (bool, int, float, unicode) if type_ not in allowed: - raise ValueError( - 'type_ must be in %r, got %r' % (type_, allowed) - ) + raise ValueError('not an allowed type: %r' % type_) self.type = type_ lock(self) diff --git a/ipalib/tests/test_ipa_types.py b/ipalib/tests/test_ipa_types.py index 4ca44121..f594aabf 100644 --- a/ipalib/tests/test_ipa_types.py +++ b/ipalib/tests/test_ipa_types.py @@ -69,6 +69,24 @@ class test_Type(ClassChecker): def test_class(self): assert self.cls.__bases__ == (plugable.ReadOnly,) + def test_init(self): + okay = (bool, int, float, unicode) + for t in okay: + o = self.cls(t) + assert o.__islocked__() is True + assert read_only(o, 'type') is t + assert read_only(o, 'name') is 'Type' + + type_errors = (None, True, 8, 8.0, u'hello') + for t in type_errors: + e = raises(TypeError, self.cls, t) + assert str(e) == '%r is not %r' % (type(t), type) + + value_errors = (long, complex, str, tuple, list, dict, set, frozenset) + for t in value_errors: + e = raises(ValueError, self.cls, t) + assert str(e) == 'not an allowed type: %r' % t + class test_Int(ClassChecker): _cls = ipa_types.Int |