Skip to content

Commit 5da89cc

Browse files
gh-151678: Add tests for tkinter font, image, variable, Misc and Wm methods
Fill the remaining gaps in these already-thorough tests: * 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. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 2e5843e commit 5da89cc

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
@@ -7,6 +7,7 @@
77
from tkinter import TclError
88
import enum
99
from test import support
10+
from test.support import os_helper
1011
from test.test_tkinter.support import setUpModule # noqa: F401
1112
from test.test_tkinter.support import (AbstractTkTest, AbstractDefaultRootTest,
1213
requires_tk, get_tk_patchlevel)
@@ -357,6 +358,19 @@ def test_option(self):
357358
self.root.option_clear()
358359
self.assertEqual(b.option_get('background', 'Background'), '')
359360

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

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

820866
t.destroy()
821867

868+
def test_wm_iconphoto(self):
869+
t = tkinter.Toplevel(self.root)
870+
img = tkinter.PhotoImage(master=t, width=16, height=16)
871+
t.wm_iconphoto(False, img) # No exception.
872+
t.wm_iconphoto(True, img)
873+
self.assertRaises(tkinter.TclError, t.wm_iconphoto, False, 'spam')
874+
822875
def test_wm_title(self):
823876
t = tkinter.Toplevel(self.root)
824877
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)