From 8d07ec2235aa0bdb55baf3f9f484fc166635b809 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 5 Aug 2014 17:08:16 +0200 Subject: [PATCH] test_conftypes: Add (broken) Font tests. --- qutebrowser/test/config/test_conftypes.py | 142 ++++++++++++++++++++++ 1 file changed, 142 insertions(+) diff --git a/qutebrowser/test/config/test_conftypes.py b/qutebrowser/test/config/test_conftypes.py index 78e227d81..c1a2b4cf8 100644 --- a/qutebrowser/test/config/test_conftypes.py +++ b/qutebrowser/test/config/test_conftypes.py @@ -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()