Merge LengthList into List

This commit is contained in:
Marshall Lochbaum 2016-07-27 10:20:10 -04:00
parent a3c7ed51d4
commit b4fec256dc
4 changed files with 25 additions and 68 deletions

View File

@ -684,8 +684,8 @@ def data(readonly=False):
('object-cache-capacities', ('object-cache-capacities',
SettingValue( SettingValue(
typ.LengthList(typ.WebKitBytes(maxsize=MAXVALS['int'], typ.List(typ.WebKitBytes(maxsize=MAXVALS['int'],
none_ok=True), none_ok=True, length=3), ''), none_ok=True), none_ok=True, length=3), ''),
"The capacities for the global memory cache for dead objects " "The capacities for the global memory cache for dead objects "
"such as stylesheets or scripts. Syntax: cacheMinDeadCapacity, " "such as stylesheets or scripts. Syntax: cacheMinDeadCapacity, "
"cacheMaxDead, totalCapacity.\n\n" "cacheMaxDead, totalCapacity.\n\n"

View File

@ -303,9 +303,10 @@ class List(BaseType):
"""Base class for a (string-)list setting.""" """Base class for a (string-)list setting."""
def __init__(self, inner_type, none_ok=False): def __init__(self, inner_type, none_ok=False, length=None):
super().__init__(none_ok) super().__init__(none_ok)
self.inner_type = inner_type self.inner_type = inner_type
self.length = length
def transform(self, value): def transform(self, value):
if not value: if not value:
@ -318,7 +319,11 @@ class List(BaseType):
self._basic_validation(value) self._basic_validation(value)
if not value: if not value:
return return
for val in value.split(','): vals = value.split(',')
if self.length is not None and len(vals) != self.length:
raise configexc.ValidationError(value, "Exactly {} values need to "
"be set!".format(self.length))
for val in vals:
self.inner_type.validate(val.strip()) self.inner_type.validate(val.strip())
@ -337,23 +342,6 @@ class BaseList(List):
self._basic_validation(value) self._basic_validation(value)
class LengthList(List):
"""Base class for a list with length checking."""
def __init__(self, inner_type, length=None, none_ok=False):
super().__init__(inner_type, none_ok)
self.length = length
def validate(self, value):
super().validate(value)
if not value:
return
if self.length is not None and value.count(',')+1 != self.length:
raise configexc.ValidationError(value, "Exactly {} values need to "
"be set!".format(self.length))
class FlagList(BaseList): class FlagList(BaseList):
"""Base class for a list setting that contains one or more flags. """Base class for a list setting that contains one or more flags.
@ -1135,7 +1123,7 @@ PaddingValues = collections.namedtuple('PaddingValues', ['top', 'bottom',
'left', 'right']) 'left', 'right'])
class Padding(LengthList): class Padding(List):
"""Setting for paddings around elements.""" """Setting for paddings around elements."""

View File

@ -361,8 +361,9 @@ class ListSubclass(configtypes.List):
Valid values are 'foo', 'bar' and 'baz'. Valid values are 'foo', 'bar' and 'baz'.
""" """
def __init__(self, none_ok_inner=False, none_ok_outer=False): def __init__(self, none_ok_inner=False, none_ok_outer=False, length=None):
super().__init__(configtypes.BaseType(none_ok_inner), none_ok_outer) super().__init__(configtypes.BaseType(none_ok_inner),
none_ok=none_ok_outer, length=length)
self.inner_type.valid_values = configtypes.ValidValues('foo', self.inner_type.valid_values = configtypes.ValidValues('foo',
'bar', 'baz') 'bar', 'baz')
@ -375,8 +376,7 @@ class TestList:
def klass(self): def klass(self):
return ListSubclass return ListSubclass
@pytest.mark.parametrize('val', @pytest.mark.parametrize('val', ['', 'foo', 'foo,bar', 'foo, bar'])
['', 'foo', 'foo,bar', 'foo, bar'])
def test_validate_valid(self, klass, val): def test_validate_valid(self, klass, val):
klass(none_ok_outer=True).validate(val) klass(none_ok_outer=True).validate(val)
@ -388,8 +388,18 @@ class TestList:
def test_invalid_empty_value_none_ok(self, klass): def test_invalid_empty_value_none_ok(self, klass):
with pytest.raises(configexc.ValidationError): with pytest.raises(configexc.ValidationError):
klass(none_ok_outer=True).validate('foo,,bar') klass(none_ok_outer=True).validate('foo,,bar')
with pytest.raises(configexc.ValidationError):
klass(none_ok_inner=True).validate('') klass(none_ok_inner=True).validate('')
@pytest.mark.parametrize('val', ['', 'foo,bar', 'foo, bar'])
def test_validate_length(self, klass, val):
klass(none_ok_outer=True, length=2).validate(val)
@pytest.mark.parametrize('val', ['bar', 'foo,bar', 'foo,bar,foo,bar'])
def test_wrong_length(self, klass, val):
with pytest.raises(configexc.ValidationError):
klass(length=3).validate(val)
@pytest.mark.parametrize('val, expected', [ @pytest.mark.parametrize('val, expected', [
('foo', ['foo']), ('foo', ['foo']),
('foo,bar,baz', ['foo', 'bar', 'baz']), ('foo,bar,baz', ['foo', 'bar', 'baz']),
@ -400,47 +410,6 @@ class TestList:
assert klass().transform(val) == expected assert klass().transform(val) == expected
class LengthListSubclass(configtypes.LengthList):
"""A subclass of LengthList which we use in tests."""
def __init__(self, length=None, none_ok_inner=False, none_ok_outer=False):
super().__init__(configtypes.Int(none_ok=none_ok_inner),
length=length, none_ok=none_ok_outer)
class TestLengthList:
"""Test List with length."""
@pytest.fixture
def klass(self):
return LengthListSubclass
@pytest.mark.parametrize('val', ['', '0,1,2', '-5,4,2'])
def test_validate_valid(self, klass, val):
klass(none_ok_outer=True, length=3).validate(val)
@pytest.mark.parametrize('val', ['', '1,,4'])
def test_validate_invalid(self, klass, val):
with pytest.raises(configexc.ValidationError):
klass().validate(val)
def test_invalid_empty_value_none_ok(self, klass):
with pytest.raises(configexc.ValidationError):
klass(none_ok_outer=True).validate('1,,4')
klass(none_ok_inner=True).validate('')
@pytest.mark.parametrize('val', ['-8', '0,-1', '1,2,3,4,5'])
def test_no_length_given(self, klass, val):
klass().validate(val)
@pytest.mark.parametrize('val', ['-8', '0,-1', '1,2,3,4,5'])
def test_wrong_length(self, klass, val):
with pytest.raises(configexc.ValidationError):
klass(length=3).validate(val)
class FlagListSubclass(configtypes.FlagList): class FlagListSubclass(configtypes.FlagList):
"""A subclass of FlagList which we use in tests. """A subclass of FlagList which we use in tests.

View File

@ -36,7 +36,7 @@ def gen_classes():
pass pass
elif member is configtypes.MappingType: elif member is configtypes.MappingType:
pass pass
elif member in [configtypes.List, configtypes.LengthList]: elif member is configtypes.List:
pass pass
elif member is configtypes.FormatString: elif member is configtypes.FormatString:
yield functools.partial(member, fields=['a', 'b']) yield functools.partial(member, fields=['a', 'b'])