Skip to content

Commit c991cd1

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 ba0cae1 commit c991cd1

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
@@ -21,6 +21,7 @@ def setUpClass(cls):
2121
cls.font = font.Font(root=cls.root, name=fontname, exists=False)
2222

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

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

6488
def test_name(self):
6589
self.assertEqual(self.font.name, fontname)
@@ -83,15 +107,24 @@ def test_equality(self):
83107

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

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

96129
def test_families(self):
97130
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
@@ -288,6 +288,11 @@ def test_configure_width_height(self):
288288
image.configure(height=10)
289289
self.assertEqual(image['width'], '20')
290290
self.assertEqual(image['height'], '10')
291+
self.assertEqual(image.cget('width'), image['width'])
292+
self.assertEqual(image.cget('height'), image['height'])
293+
self.assertRaises(TypeError, image.cget)
294+
self.assertRaises(TypeError, image.cget, 'width', 'height')
295+
self.assertEqual(image.config, image.configure)
291296
self.assertEqual(image.width(), 20)
292297
self.assertEqual(image.height(), 10)
293298

@@ -656,6 +661,14 @@ def test_transparency(self):
656661
self.assertEqual(image.transparency_get(4, 6), True)
657662
image.transparency_set(4, 6, False)
658663
self.assertEqual(image.transparency_get(4, 6), False)
664+
self.assertRaises(tkinter.TclError, image.transparency_get, -1, 0)
665+
self.assertRaises(tkinter.TclError, image.transparency_get, 16, 0)
666+
self.assertRaises(tkinter.TclError, image.transparency_set, -1, 0, True)
667+
self.assertRaises(tkinter.TclError, image.transparency_set, 16, 0, True)
668+
self.assertRaises(TypeError, image.transparency_get, 0)
669+
self.assertRaises(TypeError, image.transparency_get, 0, 0, 0)
670+
self.assertRaises(TypeError, image.transparency_set, 0, 0)
671+
self.assertRaises(TypeError, image.transparency_set, 0, 0, True, 0)
659672

660673

661674
if __name__ == "__main__":

Lib/test/test_tkinter/test_misc.py

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

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

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

803849
t.destroy()
804850

851+
def test_wm_iconphoto(self):
852+
t = tkinter.Toplevel(self.root)
853+
img = tkinter.PhotoImage(master=t, width=16, height=16)
854+
t.wm_iconphoto(False, img) # No exception.
855+
t.wm_iconphoto(True, img)
856+
self.assertRaises(tkinter.TclError, t.wm_iconphoto, False, 'spam')
857+
805858
def test_wm_title(self):
806859
t = tkinter.Toplevel(self.root)
807860
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)