1import unittest 2import tkinter 3from tkinter import font 4from test.support import requires, run_unittest, gc_collect, ALWAYS_EQ 5from tkinter.test.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_unicode_family(self): 39 family = 'MS \u30b4\u30b7\u30c3\u30af' 40 try: 41 f = font.Font(root=self.root, family=family, exists=True) 42 except tkinter.TclError: 43 f = font.Font(root=self.root, family=family, exists=False) 44 self.assertEqual(f.cget('family'), family) 45 del f 46 gc_collect() 47 48 def test_actual(self): 49 options = self.font.actual() 50 self.assertGreaterEqual(set(options), 51 {'family', 'size', 'weight', 'slant', 'underline', 'overstrike'}) 52 for key in options: 53 self.assertEqual(self.font.actual(key), options[key]) 54 for key in 'family', 'weight', 'slant': 55 self.assertIsInstance(options[key], str) 56 self.assertIsInstance(self.font.actual(key), str) 57 sizetype = int if self.wantobjects else str 58 for key in 'size', 'underline', 'overstrike': 59 self.assertIsInstance(options[key], sizetype) 60 self.assertIsInstance(self.font.actual(key), sizetype) 61 62 def test_name(self): 63 self.assertEqual(self.font.name, fontname) 64 self.assertEqual(str(self.font), fontname) 65 66 def test_eq(self): 67 font1 = font.Font(root=self.root, name=fontname, exists=True) 68 font2 = font.Font(root=self.root, name=fontname, exists=True) 69 self.assertIsNot(font1, font2) 70 self.assertEqual(font1, font2) 71 self.assertNotEqual(font1, font1.copy()) 72 self.assertNotEqual(font1, 0) 73 self.assertEqual(font1, ALWAYS_EQ) 74 75 def test_measure(self): 76 self.assertIsInstance(self.font.measure('abc'), int) 77 78 def test_metrics(self): 79 metrics = self.font.metrics() 80 self.assertGreaterEqual(set(metrics), 81 {'ascent', 'descent', 'linespace', 'fixed'}) 82 for key in metrics: 83 self.assertEqual(self.font.metrics(key), metrics[key]) 84 self.assertIsInstance(metrics[key], int) 85 self.assertIsInstance(self.font.metrics(key), int) 86 87 def test_families(self): 88 families = font.families(self.root) 89 self.assertIsInstance(families, tuple) 90 self.assertTrue(families) 91 for family in families: 92 self.assertIsInstance(family, str) 93 self.assertTrue(family) 94 95 def test_names(self): 96 names = font.names(self.root) 97 self.assertIsInstance(names, tuple) 98 self.assertTrue(names) 99 for name in names: 100 self.assertIsInstance(name, str) 101 self.assertTrue(name) 102 self.assertIn(fontname, names) 103 104tests_gui = (FontTest, ) 105 106if __name__ == "__main__": 107 run_unittest(*tests_gui) 108