332: Param.normalize() now returns None if multivalue and len() == 0

This commit is contained in:
Jason Gerard DeRose 2008-09-24 06:25:12 +00:00
parent d56f4c643b
commit 6bedb15674
2 changed files with 17 additions and 10 deletions

View File

@ -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:

View File

@ -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