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