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
This commit is contained in:
Ryan Roden-Corrent 2018-10-03 17:41:48 -04:00
parent 46683b82e7
commit 59f9d31d4b
No known key found for this signature in database
GPG Key ID: 4E5072F68872BC04
2 changed files with 16 additions and 9 deletions

View File

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

View File

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