From b4fec256dce123f1f8b70caa96c4a18096afb0f8 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Wed, 27 Jul 2016 10:20:10 -0400 Subject: [PATCH] Merge LengthList into List --- qutebrowser/config/configdata.py | 4 +- qutebrowser/config/configtypes.py | 28 +++------ tests/unit/config/test_configtypes.py | 59 +++++-------------- .../config/test_configtypes_hypothesis.py | 2 +- 4 files changed, 25 insertions(+), 68 deletions(-) diff --git a/qutebrowser/config/configdata.py b/qutebrowser/config/configdata.py index 59998c090..16b76a681 100644 --- a/qutebrowser/config/configdata.py +++ b/qutebrowser/config/configdata.py @@ -684,8 +684,8 @@ def data(readonly=False): ('object-cache-capacities', SettingValue( - typ.LengthList(typ.WebKitBytes(maxsize=MAXVALS['int'], - none_ok=True), none_ok=True, length=3), ''), + typ.List(typ.WebKitBytes(maxsize=MAXVALS['int'], + none_ok=True), none_ok=True, length=3), ''), "The capacities for the global memory cache for dead objects " "such as stylesheets or scripts. Syntax: cacheMinDeadCapacity, " "cacheMaxDead, totalCapacity.\n\n" diff --git a/qutebrowser/config/configtypes.py b/qutebrowser/config/configtypes.py index 0ed6ddb5e..c30f582dd 100644 --- a/qutebrowser/config/configtypes.py +++ b/qutebrowser/config/configtypes.py @@ -303,9 +303,10 @@ class List(BaseType): """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) self.inner_type = inner_type + self.length = length def transform(self, value): if not value: @@ -318,7 +319,11 @@ class List(BaseType): self._basic_validation(value) if not value: 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()) @@ -337,23 +342,6 @@ class BaseList(List): 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): """Base class for a list setting that contains one or more flags. @@ -1135,7 +1123,7 @@ PaddingValues = collections.namedtuple('PaddingValues', ['top', 'bottom', 'left', 'right']) -class Padding(LengthList): +class Padding(List): """Setting for paddings around elements.""" diff --git a/tests/unit/config/test_configtypes.py b/tests/unit/config/test_configtypes.py index 32600e026..cfb338643 100644 --- a/tests/unit/config/test_configtypes.py +++ b/tests/unit/config/test_configtypes.py @@ -361,8 +361,9 @@ class ListSubclass(configtypes.List): Valid values are 'foo', 'bar' and 'baz'. """ - def __init__(self, none_ok_inner=False, none_ok_outer=False): - super().__init__(configtypes.BaseType(none_ok_inner), none_ok_outer) + def __init__(self, none_ok_inner=False, none_ok_outer=False, length=None): + super().__init__(configtypes.BaseType(none_ok_inner), + none_ok=none_ok_outer, length=length) self.inner_type.valid_values = configtypes.ValidValues('foo', 'bar', 'baz') @@ -375,8 +376,7 @@ class TestList: def klass(self): return ListSubclass - @pytest.mark.parametrize('val', - ['', 'foo', 'foo,bar', 'foo, bar']) + @pytest.mark.parametrize('val', ['', 'foo', 'foo,bar', 'foo, bar']) def test_validate_valid(self, klass, val): klass(none_ok_outer=True).validate(val) @@ -388,8 +388,18 @@ class TestList: def test_invalid_empty_value_none_ok(self, klass): with pytest.raises(configexc.ValidationError): klass(none_ok_outer=True).validate('foo,,bar') + with pytest.raises(configexc.ValidationError): 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', [ ('foo', ['foo']), ('foo,bar,baz', ['foo', 'bar', 'baz']), @@ -400,47 +410,6 @@ class TestList: 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): """A subclass of FlagList which we use in tests. diff --git a/tests/unit/config/test_configtypes_hypothesis.py b/tests/unit/config/test_configtypes_hypothesis.py index a575cae3a..a5b40c417 100644 --- a/tests/unit/config/test_configtypes_hypothesis.py +++ b/tests/unit/config/test_configtypes_hypothesis.py @@ -36,7 +36,7 @@ def gen_classes(): pass elif member is configtypes.MappingType: pass - elif member in [configtypes.List, configtypes.LengthList]: + elif member is configtypes.List: pass elif member is configtypes.FormatString: yield functools.partial(member, fields=['a', 'b'])