From 083baf1222ea9f95c71b5eecf1360adfefae7deb Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Tue, 26 Jul 2016 23:05:42 -0400 Subject: [PATCH] Remove unnecessary List subclasses --- qutebrowser/config/configdata.py | 8 +- qutebrowser/config/configtypes.py | 45 +------ tests/unit/config/test_configtypes.py | 168 -------------------------- 3 files changed, 9 insertions(+), 212 deletions(-) diff --git a/qutebrowser/config/configdata.py b/qutebrowser/config/configdata.py index 238aad78e..84c670c60 100644 --- a/qutebrowser/config/configdata.py +++ b/qutebrowser/config/configdata.py @@ -254,7 +254,7 @@ def data(readonly=False): ('ui', sect.KeyValue( ('zoom-levels', - SettingValue(typ.PercList(minval=0), + SettingValue(typ.List(typ.Perc(minval=0)), '25%,33%,50%,67%,75%,90%,100%,110%,125%,150%,175%,' '200%,250%,300%,400%,500%'), "The available zoom levels, separated by commas."), @@ -826,7 +826,7 @@ def data(readonly=False): ('host-block-lists', SettingValue( - typ.UrlList(none_ok=True), + typ.List(typ.Url(), none_ok=True), 'http://www.malwaredomainlist.com/hostslist/hosts.txt,' 'http://someonewhocares.org/hosts/hosts,' 'http://winhelp2002.mvps.org/hosts.zip,' @@ -916,13 +916,13 @@ def data(readonly=False): "auto-follow."), ('next-regexes', - SettingValue(typ.RegexList(flags=re.IGNORECASE), + SettingValue(typ.List(typ.Regex(flags=re.IGNORECASE)), r'\bnext\b,\bmore\b,\bnewer\b,\b[>→≫]\b,\b(>>|»)\b,' r'\bcontinue\b'), "A comma-separated list of regexes to use for 'next' links."), ('prev-regexes', - SettingValue(typ.RegexList(flags=re.IGNORECASE), + SettingValue(typ.List(typ.Regex(flags=re.IGNORECASE)), r'\bprev(ious)?\b,\bback\b,\bolder\b,\b[<←≪]\b,' r'\b(<<|«)\b'), "A comma-separated list of regexes to use for 'prev' links."), diff --git a/qutebrowser/config/configtypes.py b/qutebrowser/config/configtypes.py index 62a72587e..05fa6a15d 100644 --- a/qutebrowser/config/configtypes.py +++ b/qutebrowser/config/configtypes.py @@ -461,15 +461,6 @@ class Int(BaseType): "smaller!".format(self.maxval)) -class IntList(List): - - """Base class for an int-list setting.""" - - def __init__(self, none_ok=False, valid_values=None): - super().__init__(Int(none_ok=none_ok), none_ok=none_ok) - self.inner_type.valid_values = valid_values - - class Float(BaseType): """Base class for a float setting. @@ -550,19 +541,6 @@ class Perc(BaseType): "less!".format(self.maxval)) -class PercList(List): - - """Base class for a list of percentages. - - Attributes: - minval: Minimum value (inclusive). - maxval: Maximum value (inclusive). - """ - - def __init__(self, minval=None, maxval=None, none_ok=False): - super().__init__(Perc(minval, maxval, none_ok), none_ok=none_ok) - - class PercOrInt(BaseType): """Percentage or integer. @@ -845,14 +823,6 @@ class Regex(BaseType): return re.compile(value, self.flags) -class RegexList(List): - - """A list of regexes.""" - - def __init__(self, flags=0, none_ok=False): - super().__init__(Regex(flags, none_ok), none_ok=none_ok) - - class File(BaseType): """A file on the local filesystem.""" @@ -1171,10 +1141,14 @@ PaddingValues = collections.namedtuple('PaddingValues', ['top', 'bottom', 'left', 'right']) -class Padding(IntList): +class Padding(List): """Setting for paddings around elements.""" + def __init__(self, none_ok=False, valid_values=None): + super().__init__(Int(none_ok=none_ok), none_ok=none_ok) + self.inner_type.valid_values = valid_values + def validate(self, value): self._basic_validation(value) if not value: @@ -1351,15 +1325,6 @@ class Url(BaseType): "{}".format(val.errorString())) -class UrlList(List): - - """A list of URLs.""" - - def __init__(self, none_ok=False, valid_values=None): - super().__init__(Url(), none_ok) - - - class HeaderDict(BaseType): """A JSON-like dictionary for custom HTTP headers.""" diff --git a/tests/unit/config/test_configtypes.py b/tests/unit/config/test_configtypes.py index e0c8293cb..5537d45ea 100644 --- a/tests/unit/config/test_configtypes.py +++ b/tests/unit/config/test_configtypes.py @@ -595,37 +595,6 @@ class TestInt: assert klass(none_ok=True).transform(val) == expected -class TestIntList: - - """Test IntList.""" - - @pytest.fixture - def klass(self): - return configtypes.IntList - - @pytest.mark.parametrize('val', ['', '1,2', '1', '23,1337']) - def test_validate_valid(self, klass, val): - klass(none_ok=True).validate(val) - - @pytest.mark.parametrize('val', ['', '1,,2', '23,foo,1337']) - def test_validate_invalid(self, klass, val): - with pytest.raises(configexc.ValidationError): - klass().validate(val) - - def test_invalid_empty_value_none_ok(self, klass): - klass(none_ok=True).validate('1,,2') - - @pytest.mark.parametrize('val, expected', [ - ('1', [1]), - ('23,42', [23, 42]), - ('', None), - ('1,,2', [1, None, 2]), - ('23, 42', [23, 42]), - ]) - def test_transform(self, klass, val, expected): - assert klass().transform(val) == expected - - class TestFloat: """Test Float.""" @@ -718,52 +687,6 @@ class TestPerc: assert klass().transform(val) == expected -class TestPercList: - - """Test PercList.""" - - @pytest.fixture - def klass(self): - return configtypes.PercList - - def test_minval_gt_maxval(self, klass): - with pytest.raises(ValueError): - klass(minval=2, maxval=1) - - @pytest.mark.parametrize('kwargs, val', [ - ({}, '23%,42%,1337%'), - ({'minval': 2}, '2%,3%'), - ({'maxval': 2}, '1%,2%'), - ({'minval': 2, 'maxval': 3}, '2%,3%'), - ({'none_ok': True}, '42%,,23%'), - ({'none_ok': True}, ''), - ]) - def test_validate_valid(self, klass, kwargs, val): - klass(**kwargs).validate(val) - - @pytest.mark.parametrize('kwargs, val', [ - ({}, '23%,42,1337%'), - ({'minval': 2}, '1%,2%'), - ({'maxval': 2}, '2%,3%'), - ({'minval': 2, 'maxval': 3}, '1%,2%'), - ({'minval': 2, 'maxval': 3}, '3%,4%'), - ({}, '42%,,23%'), - ({}, ''), - ]) - def test_validate_invalid(self, klass, kwargs, val): - with pytest.raises(configexc.ValidationError): - klass(**kwargs).validate(val) - - @pytest.mark.parametrize('val, expected', [ - ('', None), - ('1337%', [1337]), - ('23%,42%,1337%', [23, 42, 1337]), - ('23%,,42%', [23, None, 42]), - ]) - def test_transform(self, klass, val, expected): - assert klass().transform(val) == expected - - class TestPercOrInt: """Test PercOrInt.""" @@ -1239,59 +1162,6 @@ class TestRegex: klass().validate('foo') -class TestRegexList: - - """Test RegexList.""" - - @pytest.fixture - def klass(self): - return configtypes.RegexList - - @pytest.mark.parametrize('val', [ - r'(foo|bar),[abcd]?,1337{42}', - r'(foo|bar),,1337{42}', - r'', - ]) - def test_validate_valid(self, klass, val): - klass(none_ok=True).validate(val) - - @pytest.mark.parametrize('val', [ - r'(foo|bar),,1337{42}', - r'', - r'(foo|bar),((),1337{42}', - r'(' * 500, - ], ids=['empty value', 'empty', 'unmatched parens', 'too many parens']) - def test_validate_invalid(self, klass, val): - with pytest.raises(configexc.ValidationError): - klass().validate(val) - - @pytest.mark.parametrize('val', [ - r'foo\Xbar', - r'foo\Cbar', - ]) - def test_validate_maybe_valid(self, klass, val): - """Those values are valid on some Python versions (and systems?). - - On others, they raise a DeprecationWarning because of an invalid - escape. This tests makes sure this gets translated to a - ValidationError. - """ - try: - klass().validate(val) - except configexc.ValidationError: - pass - - @pytest.mark.parametrize('val, expected', [ - ('foo', [RegexEq('foo')]), - ('foo,bar,baz', [RegexEq('foo'), RegexEq('bar'), - RegexEq('baz')]), - ('foo,,bar', [RegexEq('foo'), None, RegexEq('bar')]), - ('', None), - ]) - def test_transform(self, klass, val, expected): - assert klass().transform(val) == expected - - def unrequired_class(**kwargs): return configtypes.File(required=False, **kwargs) @@ -1976,44 +1846,6 @@ class TestEncoding: assert klass().transform(val) == expected -class TestUrlList: - - """Test UrlList.""" - - TESTS = { - 'http://qutebrowser.org/': [QUrl('http://qutebrowser.org/')], - 'http://qutebrowser.org/,http://heise.de/': - [QUrl('http://qutebrowser.org/'), QUrl('http://heise.de/')], - '': None, - } - - @pytest.fixture - def klass(self): - return configtypes.UrlList - - @pytest.mark.parametrize('val', sorted(TESTS)) - def test_validate_valid(self, klass, val): - klass(none_ok=True).validate(val) - - @pytest.mark.parametrize('val', [ - '', - 'foo,,bar', - '+', # invalid URL with QUrl.fromUserInput - ]) - def test_validate_invalid(self, klass, val): - with pytest.raises(configexc.ValidationError): - klass().validate(val) - - def test_validate_empty_item(self, klass): - """Test validate with empty item and none_ok = False.""" - with pytest.raises(configexc.ValidationError): - klass().validate('foo,,bar') - - @pytest.mark.parametrize('val, expected', sorted(TESTS.items())) - def test_transform_single(self, klass, val, expected): - assert klass().transform(val) == expected - - class TestSessionName: """Test SessionName."""