From 59f9d31d4b5183945bc57a65f6e21651372aff9d Mon Sep 17 00:00:00 2001 From: Ryan Roden-Corrent Date: Wed, 3 Oct 2018 17:41:48 -0400 Subject: [PATCH] Fix up configtypes based on code review. - Remove stray statements - add an early bail-out if we can't parse a color function - add more test cases --- qutebrowser/config/configtypes.py | 15 ++++++++------- tests/unit/config/test_configtypes.py | 10 ++++++++-- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/qutebrowser/config/configtypes.py b/qutebrowser/config/configtypes.py index 842eeaf29..74809d994 100644 --- a/qutebrowser/config/configtypes.py +++ b/qutebrowser/config/configtypes.py @@ -915,7 +915,6 @@ class QtColor(BaseType): if val.endswith('%'): val = val[:-1] mult = 255.0 / 100 - return int(float(val) * 255.0 / 100.0) try: return int(float(val) * mult) @@ -927,19 +926,21 @@ class QtColor(BaseType): if not value: return None - if value.endswith(')'): + if '(' in value and value.endswith(')'): openparen = value.index('(') kind = value[:openparen] vals = value[openparen+1:-1].split(',') vals = [self._parse_value(v) for v in vals] if kind == 'rgba' and len(vals) == 4: return QColor.fromRgb(*vals) - if kind == 'rgb' and len(vals) == 3: - return QColor.fromRgb(*vals) - if kind == 'hsva' and len(vals) == 4: - return QColor.fromHsv(*vals) - if kind == 'hsv' and len(vals) == 3: + elif kind == 'rgb' and len(vals) == 3: + return QColor.fromRgb(*vals) + elif kind == 'hsva' and len(vals) == 4: + return QColor.fromHsv(*vals) + elif kind == 'hsv' and len(vals) == 3: return QColor.fromHsv(*vals) + else: + raise configexc.ValidationError(value, "must be a valid color") color = QColor(value) if color.isValid(): diff --git a/tests/unit/config/test_configtypes.py b/tests/unit/config/test_configtypes.py index 83835faa0..ffdc71d97 100644 --- a/tests/unit/config/test_configtypes.py +++ b/tests/unit/config/test_configtypes.py @@ -1242,11 +1242,10 @@ class TestQtColor: # however this is consistent with Qt's CSS parser # https://bugreports.qt.io/browse/QTBUG-70897 ('hsv(10%,10%,10%)', QColor.fromHsv(25, 25, 25)), + ('hsva(10%,20%,30%,40%)', QColor.fromHsv(25, 51, 76, 102)), ]) def test_valid(self, val, expected): act = configtypes.QtColor().to_py(val) - print(expected.hue(), expected.saturation(), expected.value(), expected.alpha()) - print(act.hue(), act.saturation(), act.value(), act.alpha()) assert configtypes.QtColor().to_py(val) == expected @pytest.mark.parametrize('val', [ @@ -1257,6 +1256,13 @@ class TestQtColor: '42', 'foo(1, 2, 3)', 'rgb(1, 2, 3', + 'rgb)', + 'rgb(1, 2, 3))', + 'rgb((1, 2, 3)', + 'rgb()', + 'rgb(1, 2, 3, 4)', + 'rgba(1, 2, 3)', + 'rgb(10%%, 0, 0)', ]) def test_invalid(self, val): with pytest.raises(configexc.ValidationError):