diff options
-rw-r--r-- | ipalib/frontend.py | 15 | ||||
-rw-r--r-- | ipalib/tests/test_frontend.py | 12 |
2 files changed, 17 insertions, 10 deletions
diff --git a/ipalib/frontend.py b/ipalib/frontend.py index 59cdf69f..5971f9df 100644 --- a/ipalib/frontend.py +++ b/ipalib/frontend.py @@ -105,6 +105,15 @@ class Param(plugable.ReadOnly): self.rules = (type_.validate,) + rules lock(self) + def __if_multivalue(self, value, scalar): + if self.multivalue: + if type(value) in (tuple, list): + if len(value) == 0: + return None + return tuple(scalar(v) for v in value) + return (scalar(value),) # tuple + return scalar(value) + def __normalize_scalar(self, value): if not isinstance(value, basestring): return value @@ -131,11 +140,7 @@ class Param(plugable.ReadOnly): """ if self.__normalize is None: return value - if self.multivalue: - if type(value) in (tuple, list): - return tuple(self.__normalize_scalar(v) for v in value) - return (self.__normalize_scalar(value),) # tuple - return self.__normalize_scalar(value) + return self.__if_multivalue(value, self.__normalize_scalar) def __convert_scalar(self, value, index=None): if value is None: diff --git a/ipalib/tests/test_frontend.py b/ipalib/tests/test_frontend.py index c473ad58..63cc9214 100644 --- a/ipalib/tests/test_frontend.py +++ b/ipalib/tests/test_frontend.py @@ -111,9 +111,9 @@ class test_DefaultFrom(ClassChecker): assert o(**kw_copy) is None -class test_Option(ClassChecker): +class test_Param(ClassChecker): """ - Tests the `frontend.Param` class. + Test the `frontend.Param` class. """ _cls = frontend.Param @@ -122,7 +122,7 @@ class test_Option(ClassChecker): def test_init(self): """ - Tests the `frontend.Param.__init__` method. + Test the `frontend.Param.__init__` method. """ name = 'sn' type_ = ipa_types.Unicode() @@ -139,7 +139,7 @@ class test_Option(ClassChecker): def test_convert(self): """ - Tests the `frontend.Param.convert` method. + Test the `frontend.Param.convert` method. """ name = 'some_number' type_ = ipa_types.Int() @@ -184,7 +184,7 @@ class test_Option(ClassChecker): def test_normalize(self): """ - Tests the `frontend.Param.normalize` method. + Test the `frontend.Param.normalize` method. """ name = 'sn' t = ipa_types.Unicode() @@ -212,6 +212,8 @@ class test_Option(ClassChecker): # Scenario 4: multivalue=True, normalize=callback o = self.cls(name, t, multivalue=True, normalize=callback) + assert o.normalize([]) is None + assert o.normalize(tuple()) is None for value in [(u'Hello',), (u'hello',), 'Hello', ['Hello']]: # Okay assert o.normalize(value) == (u'hello',) fail = 42 # Not basestring |