test_conftypes: Add (broken) Font tests.

This commit is contained in:
Florian Bruhin 2014-08-05 17:08:16 +02:00
parent 0e645d9361
commit 8d07ec2235

View File

@ -25,6 +25,37 @@ from qutebrowser.test.stubs import FakeCmdUtils, FakeCommand
from PyQt5.QtGui import QColor, QFont
class Font(QFont):
"""A QFont with a nicer repr()."""
def __repr__(self):
return 'Font("{}", {}, {}, {})'.format(
self.family(), self.pointSize(), self.weight(),
self.style() & QFont.StyleItalic)
def __eq__(self, other):
"""The default operator= of QFont seems to be rather strict."""
return (self.family() == other.family() and
self.pointSize() == other.pointSize() and
self.weight() == other.weight() and
self.style() == other.style())
@classmethod
def fromdesc(cls, desc):
"""Get a Font based on a font description."""
style, weight, ptsize, pxsize, family = desc
f = cls()
f.setStyle(style)
f.setWeight(weight)
if ptsize is not None and ptsize != -1:
f.setPointSize(ptsize)
if pxsize is not None and ptsize != -1:
f.setPixelSize(pxsize)
f.setFamily(family)
return f
class ValidValuesTest(unittest.TestCase):
"""Test ValidValues."""
@ -949,6 +980,15 @@ class QtColorTests(unittest.TestCase):
with self.assertRaises(conftypes.ValidationError, msg=val):
self.t.validate(val)
def test_transform(self):
"""Test transform."""
for v in self.VALID:
self.assertEqual(self.t.transform(v), QColor(v), v)
def test_transform_empty(self):
"""Test transform with an empty value."""
self.assertIsNone(self.t.transform(''))
class CssColorTests(QtColorTests):
@ -964,6 +1004,11 @@ class CssColorTests(QtColorTests):
t = conftypes.CssColor(none_ok=True)
t.validate('')
def test_transform(self):
"""Make sure transform doesn't alter the value."""
for v in self.VALID:
self.assertEqual(self.t.transform(v), v, v)
class QssColorTests(QtColorTests):
@ -990,6 +1035,103 @@ class QssColorTests(QtColorTests):
t = conftypes.QssColor(none_ok=True)
t.validate('')
def test_transform(self):
"""Make sure transform doesn't alter the value."""
for v in self.VALID:
self.assertEqual(self.t.transform(v), v, v)
class FontTests(unittest.TestCase):
"""Test Font."""
TESTS = {
# (style, weight, pointsize, pixelsize, family
'"Foobar Neue"':
(QFont.StyleNormal, QFont.Normal, -1, -1, 'Foobar Neue'),
'10pt "Foobar Neue"':
(QFont.StyleNormal, QFont.Normal, 10, None, 'Foobar Neue'),
'10PT "Foobar Neue"':
(QFont.StyleNormal, QFont.Normal, 10, None, 'Foobar Neue'),
'10.5pt "Foobar Neue"':
(QFont.StyleNormal, QFont.Normal, 10.5, None, 'Foobar Neue'),
'10px "Foobar Neue"':
(QFont.StyleNormal, QFont.Normal, None, 10, 'Foobar Neue'),
'10PX "Foobar Neue"':
(QFont.StyleNormal, QFont.Normal, None, 10, 'Foobar Neue'),
'bold "Foobar Neue"':
(QFont.StyleNormal, QFont.Bold, -1, -1, 'Foobar Neue'),
'italic "Foobar Neue"':
(QFont.StyleItalic, QFont.Normal, -1, -1, 'Foobar Neue'),
'oblique "Foobar Neue"':
(QFont.StyleOblique, QFont.Normal, -1, -1, 'Foobar Neue'),
'normal bold "Foobar Neue"':
(QFont.StyleNormal, QFont.Bold, -1, -1, 'Foobar Neue'),
'bold italic "Foobar Neue"':
(QFont.StyleItalic, QFont.Bold, -1, -1, 'Foobar Neue'),
'bold 10pt "Foobar Neue"':
(QFont.StyleNormal, QFont.Bold, 10, None, 'Foobar Neue'),
'italic 10pt "Foobar Neue"':
(QFont.StyleItalic, QFont.Normal, 10, None, 'Foobar Neue'),
'oblique 10pt "Foobar Neue"':
(QFont.StyleOblique, QFont.Normal, 10, None, 'Foobar Neue'),
'normal bold 10pt "Foobar Neue"':
(QFont.StyleNormal, QFont.Bold, 10, None, 'Foobar Neue'),
'bold italic 10pt "Foobar Neue"':
(QFont.StyleItalic, QFont.Bold, 10, None, 'Foobar Neue'),
}
INVALID = ['green "Foobar Neue"', 'italic green "Foobar Neue"',
'bold bold "Foobar Neue"', 'bold italic "Foobar Neue"'
'bold', '10pt 20px "Foobar Neue"'
]
def setUp(self):
self.t = conftypes.Font()
self.t2 = conftypes.QtFont()
def test_validate_empty(self):
"""Test validate with an empty string."""
with self.assertRaises(conftypes.ValidationError):
self.t.validate('')
def test_validate_empty_none_ok(self):
"""Test validate with an empty string and none_ok=True."""
t = conftypes.QssColor(none_ok=True)
t2 = conftypes.QtColor(none_ok=True)
t.validate('')
t2.validate('')
def test_validate_valid(self):
"""Test validate with valid values."""
for val in self.TESTS:
self.t.validate(val)
self.t2.validate(val)
# FIXME
@unittest.expectedFailure
def test_validate_invalid(self):
"""Test validate with invalid values."""
for val in self.INVALID:
with self.assertRaises(conftypes.ValidationError, msg=val):
self.t.validate(val)
with self.assertRaises(conftypes.ValidationError, msg=val):
self.t2.validate(val)
# FIXME
@unittest.expectedFailure
def test_transform(self):
"""Test transform."""
for string, desc in self.TESTS.items():
self.assertEqual(self.t.transform(string), string, string)
self.assertEqual(Font(self.t2.transform(string)),
Font.fromdesc(desc), string)
def test_transform_empty(self):
"""Test transform with an empty value."""
self.assertIsNone(self.t.transform(''))
self.assertIsNone(self.t2.transform(''))
if __name__ == '__main__':
unittest.main()