Skip to content

Commit 8300d72

Browse files
serhiy-storchakaclaude
authored andcommitted
gh-151678: Add tests for tkinter font, image, variable, Misc and Wm methods (GH-151751)
* font: copy(), the config alias, the displayof argument of measure and metrics, and the errors raised for invalid options and a wrong number of arguments; * image: the cget method and the config alias, and the errors raised by transparency_get and transparency_set; * variable: that initialize is an alias of set and the deprecated trace is an alias of trace_variable; * Misc: tk_focusNext, tk_focusPrev, tk_strictMotif, tk_bisque and option_readfile; * Wm: wm_iconphoto. (cherry picked from commit 66cc048) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent c9ff76f commit 8300d72

4 files changed

Lines changed: 102 additions & 0 deletions

File tree

Lib/test/test_tkinter/test_font.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def setUpClass(cls):
2020
cls.font = font.Font(root=cls.root, name=fontname, exists=False)
2121

2222
def test_configure(self):
23+
self.assertEqual(self.font.config, self.font.configure)
2324
options = self.font.configure()
2425
self.assertGreaterEqual(set(options),
2526
{'family', 'size', 'weight', 'slant', 'underline', 'overstrike'})
@@ -35,6 +36,26 @@ def test_configure(self):
3536
self.assertIsInstance(options[key], sizetype)
3637
self.assertIsInstance(self.font.cget(key), sizetype)
3738
self.assertIsInstance(self.font[key], sizetype)
39+
self.assertRaisesRegex(tkinter.TclError, 'bad option "-spam"',
40+
self.font.cget, 'spam')
41+
self.assertRaisesRegex(tkinter.TclError, 'bad option "-spam"',
42+
self.font.configure, spam='x')
43+
self.assertRaises(TypeError, self.font.cget)
44+
self.assertRaises(TypeError, self.font.cget, 'size', 'weight')
45+
46+
def test_copy(self):
47+
f = font.Font(root=self.root, family='Times', size=10, weight='bold')
48+
copied = f.copy()
49+
self.assertIsInstance(copied, font.Font)
50+
self.assertIsNot(copied, f)
51+
self.assertNotEqual(copied.name, f.name)
52+
self.assertEqual(copied.actual(), f.actual())
53+
# The copy is independent of the original.
54+
sizetype = int if self.wantobjects else str
55+
copied.configure(size=20)
56+
self.assertEqual(f.cget('size'), sizetype(10))
57+
self.assertEqual(copied.cget('size'), sizetype(20))
58+
self.assertRaises(TypeError, f.copy, 'x')
3859

3960
def test_unicode_family(self):
4061
family = 'MS \u30b4\u30b7\u30c3\u30af'
@@ -59,6 +80,9 @@ def test_actual(self):
5980
for key in 'size', 'underline', 'overstrike':
6081
self.assertIsInstance(options[key], sizetype)
6182
self.assertIsInstance(self.font.actual(key), sizetype)
83+
self.assertRaisesRegex(tkinter.TclError, 'bad option "-spam"',
84+
self.font.actual, 'spam')
85+
self.assertRaises(TypeError, self.font.actual, 'size', 'weight', 'slant')
6286

6387
def test_name(self):
6488
self.assertEqual(self.font.name, fontname)
@@ -82,15 +106,24 @@ def test_equality(self):
82106

83107
def test_measure(self):
84108
self.assertIsInstance(self.font.measure('abc'), int)
109+
self.assertEqual(self.font.measure(''), 0)
110+
self.assertIsInstance(
111+
self.font.measure('abc', displayof=self.root), int)
112+
self.assertRaises(TypeError, self.font.measure)
113+
self.assertRaises(TypeError, self.font.measure, 'a', 'b', 'c')
85114

86115
def test_metrics(self):
87116
metrics = self.font.metrics()
88117
self.assertGreaterEqual(set(metrics),
89118
{'ascent', 'descent', 'linespace', 'fixed'})
90119
for key in metrics:
91120
self.assertEqual(self.font.metrics(key), metrics[key])
121+
self.assertEqual(self.font.metrics(key, displayof=self.root),
122+
metrics[key])
92123
self.assertIsInstance(metrics[key], int)
93124
self.assertIsInstance(self.font.metrics(key), int)
125+
self.assertRaisesRegex(tkinter.TclError, 'bad metric "-spam"',
126+
self.font.metrics, 'spam')
94127

95128
def test_families(self):
96129
families = font.families(self.root)

Lib/test/test_tkinter/test_images.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,11 @@ def test_configure_width_height(self):
272272
image.configure(height=10)
273273
self.assertEqual(image['width'], '20')
274274
self.assertEqual(image['height'], '10')
275+
self.assertEqual(image.cget('width'), image['width'])
276+
self.assertEqual(image.cget('height'), image['height'])
277+
self.assertRaises(TypeError, image.cget)
278+
self.assertRaises(TypeError, image.cget, 'width', 'height')
279+
self.assertEqual(image.config, image.configure)
275280
self.assertEqual(image.width(), 20)
276281
self.assertEqual(image.height(), 10)
277282

@@ -648,6 +653,14 @@ def test_transparency(self):
648653
self.assertEqual(image.transparency_get(4, 6), True)
649654
image.transparency_set(4, 6, False)
650655
self.assertEqual(image.transparency_get(4, 6), False)
656+
self.assertRaises(tkinter.TclError, image.transparency_get, -1, 0)
657+
self.assertRaises(tkinter.TclError, image.transparency_get, 16, 0)
658+
self.assertRaises(tkinter.TclError, image.transparency_set, -1, 0, True)
659+
self.assertRaises(tkinter.TclError, image.transparency_set, 16, 0, True)
660+
self.assertRaises(TypeError, image.transparency_get, 0)
661+
self.assertRaises(TypeError, image.transparency_get, 0, 0, 0)
662+
self.assertRaises(TypeError, image.transparency_set, 0, 0)
663+
self.assertRaises(TypeError, image.transparency_set, 0, 0, True, 0)
651664

652665

653666
if __name__ == "__main__":

Lib/test/test_tkinter/test_misc.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from tkinter import TclError
55
import enum
66
from test import support
7+
from test.support import os_helper
78
from test.test_tkinter.support import setUpModule # noqa: F401
89
from test.test_tkinter.support import (AbstractTkTest, AbstractDefaultRootTest,
910
requires_tk, get_tk_patchlevel)
@@ -354,6 +355,19 @@ def test_option(self):
354355
self.root.option_clear()
355356
self.assertEqual(b.option_get('background', 'Background'), '')
356357

358+
def test_option_readfile(self):
359+
self.addCleanup(self.root.option_clear)
360+
self.addCleanup(os_helper.unlink, os_helper.TESTFN)
361+
with open(os_helper.TESTFN, 'w') as f:
362+
f.write('*Button.background: red\n')
363+
self.root.option_readfile(os_helper.TESTFN)
364+
b = tkinter.Button(self.root)
365+
self.assertEqual(b.option_get('background', 'Background'), 'red')
366+
self.assertRaises(TclError, self.root.option_readfile,
367+
os_helper.TESTFN + '.nonexistent')
368+
self.assertRaises(TypeError, self.root.option_readfile)
369+
self.assertRaises(TypeError, self.root.option_readfile, 'a', 'b', 'c')
370+
357371
def test_nametowidget(self):
358372
b = tkinter.Button(self.root, name='btn')
359373
self.assertIs(self.root.nametowidget('btn'), b)
@@ -414,6 +428,38 @@ def test_bell(self):
414428
self.root.bell() # No exception.
415429
self.root.bell(displayof=self.root)
416430

431+
def test_tk_focusNext_focusPrev(self):
432+
f = tkinter.Frame(self.root)
433+
f.pack()
434+
entries = [tkinter.Entry(f) for _ in range(3)]
435+
for entry in entries:
436+
entry.pack()
437+
# tk_focusNext skips widgets that are not viewable.
438+
entries[-1].wait_visibility()
439+
self.assertIs(entries[0].tk_focusNext(), entries[1])
440+
self.assertIs(entries[1].tk_focusNext(), entries[2])
441+
self.assertIs(entries[2].tk_focusPrev(), entries[1])
442+
self.assertIs(entries[1].tk_focusPrev(), entries[0])
443+
self.assertRaises(TypeError, entries[0].tk_focusNext, 'x')
444+
self.assertRaises(TypeError, entries[0].tk_focusPrev, 'x')
445+
446+
def test_tk_strictMotif(self):
447+
self.addCleanup(self.root.tk_strictMotif, False)
448+
self.assertIs(self.root.tk_strictMotif(), False)
449+
self.assertIs(self.root.tk_strictMotif(True), True)
450+
self.assertIs(self.root.tk_strictMotif(), True)
451+
self.assertIs(self.root.tk_strictMotif(False), False)
452+
self.assertRaises(TypeError, self.root.tk_strictMotif, 1, 2)
453+
454+
def test_tk_bisque(self):
455+
# tk_bisque resets the color palette; use a separate root so that
456+
# the shared one is not affected.
457+
root = tkinter.Tk()
458+
self.addCleanup(root.destroy)
459+
root.tk_bisque()
460+
self.assertEqual(root['background'], '#ffe4c4')
461+
self.assertRaises(TypeError, root.tk_bisque, 'x')
462+
417463
def test_event_repr_defaults(self):
418464
e = tkinter.Event()
419465
e.serial = 12345
@@ -790,6 +836,13 @@ def test_wm_iconbitmap(self):
790836

791837
t.destroy()
792838

839+
def test_wm_iconphoto(self):
840+
t = tkinter.Toplevel(self.root)
841+
img = tkinter.PhotoImage(master=t, width=16, height=16)
842+
t.wm_iconphoto(False, img) # No exception.
843+
t.wm_iconphoto(True, img)
844+
self.assertRaises(tkinter.TclError, t.wm_iconphoto, False, 'spam')
845+
793846
def test_wm_title(self):
794847
t = tkinter.Toplevel(self.root)
795848
t.title('Hello')

Lib/test/test_tkinter/test_variables.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,15 @@ def test_initialize(self):
111111
self.assertFalse(v.side_effect)
112112
v.set("value")
113113
self.assertTrue(v.side_effect)
114+
self.assertEqual(Variable.initialize, Variable.set)
114115

115116
def test_trace_old(self):
116117
if tcl_version >= (9, 0):
117118
self.skipTest('requires Tcl version < 9.0')
118119
# Old interface
119120
v = Variable(self.root)
120121
vname = str(v)
122+
self.assertEqual(v.trace, v.trace_variable)
121123
trace = []
122124
def read_tracer(*args):
123125
trace.append(('read',) + args)
@@ -328,6 +330,7 @@ def test_set(self):
328330
self.assertEqual(self.root.globalgetvar("name"), false)
329331
v.set("on")
330332
self.assertEqual(self.root.globalgetvar("name"), true)
333+
self.assertEqual(BooleanVar.initialize, BooleanVar.set)
331334

332335
def test_invalid_value_domain(self):
333336
false = 0 if self.root.wantobjects() else "0"

0 commit comments

Comments
 (0)