• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import unittest
2import Tkinter as tkinter
3import tkFont as font
4from test.test_support import requires, run_unittest
5from test_ttk.support import AbstractTkTest
6
7requires('gui')
8
9fontname = "TkDefaultFont"
10
11class FontTest(AbstractTkTest, unittest.TestCase):
12
13    @classmethod
14    def setUpClass(cls):
15        AbstractTkTest.setUpClass.__func__(cls)
16        try:
17            cls.font = font.Font(root=cls.root, name=fontname, exists=True)
18        except tkinter.TclError:
19            cls.font = font.Font(root=cls.root, name=fontname, exists=False)
20
21    def test_configure(self):
22        options = self.font.configure()
23        self.assertGreaterEqual(set(options),
24            {'family', 'size', 'weight', 'slant', 'underline', 'overstrike'})
25        for key in options:
26            self.assertEqual(self.font.cget(key), options[key])
27            self.assertEqual(self.font[key], options[key])
28        for key in 'family', 'weight', 'slant':
29            self.assertIsInstance(options[key], str)
30            self.assertIsInstance(self.font.cget(key), str)
31            self.assertIsInstance(self.font[key], str)
32        sizetype = int if self.wantobjects else str
33        for key in 'size', 'underline', 'overstrike':
34            self.assertIsInstance(options[key], sizetype)
35            self.assertIsInstance(self.font.cget(key), sizetype)
36            self.assertIsInstance(self.font[key], sizetype)
37
38    def test_actual(self):
39        options = self.font.actual()
40        self.assertGreaterEqual(set(options),
41            {'family', 'size', 'weight', 'slant', 'underline', 'overstrike'})
42        for key in options:
43            self.assertEqual(self.font.actual(key), options[key])
44        for key in 'family', 'weight', 'slant':
45            self.assertIsInstance(options[key], str)
46            self.assertIsInstance(self.font.actual(key), str)
47        sizetype = int if self.wantobjects else str
48        for key in 'size', 'underline', 'overstrike':
49            self.assertIsInstance(options[key], sizetype)
50            self.assertIsInstance(self.font.actual(key), sizetype)
51
52    def test_name(self):
53        self.assertEqual(self.font.name, fontname)
54        self.assertEqual(str(self.font), fontname)
55
56    def test_eq(self):
57        font1 = font.Font(root=self.root, name=fontname, exists=True)
58        font2 = font.Font(root=self.root, name=fontname, exists=True)
59        self.assertIsNot(font1, font2)
60        self.assertEqual(font1, font2)
61        self.assertNotEqual(font1, font1.copy())
62        self.assertNotEqual(font1, 0)
63        self.assertNotIn(font1, [0])
64
65    def test_measure(self):
66        self.assertIsInstance(self.font.measure('abc'), int)
67
68    def test_metrics(self):
69        metrics = self.font.metrics()
70        self.assertGreaterEqual(set(metrics),
71            {'ascent', 'descent', 'linespace', 'fixed'})
72        for key in metrics:
73            self.assertEqual(self.font.metrics(key), metrics[key])
74            self.assertIsInstance(metrics[key], int)
75            self.assertIsInstance(self.font.metrics(key), int)
76
77    def test_families(self):
78        families = font.families(self.root)
79        self.assertIsInstance(families, tuple)
80        self.assertTrue(families)
81        for family in families:
82            self.assertIsInstance(family, (str, unicode))
83            self.assertTrue(family)
84
85    def test_names(self):
86        names = font.names(self.root)
87        self.assertIsInstance(names, tuple)
88        self.assertTrue(names)
89        for name in names:
90            self.assertIsInstance(name, (str, unicode))
91            self.assertTrue(name)
92        self.assertIn(fontname, names)
93
94tests_gui = (FontTest, )
95
96if __name__ == "__main__":
97    run_unittest(*tests_gui)
98