Issue #21522: Added Tkinter tests for Listbox.itemconfigure(),

PanedWindow.paneconfigure(), and Menu.entryconfigure().
This commit is contained in:
Serhiy Storchaka 2014-05-23 14:09:34 +03:00
commit 475620187b
2 changed files with 187 additions and 0 deletions

View File

@ -1,5 +1,6 @@
import unittest
import tkinter
from tkinter import TclError
import os
import sys
from test.support import requires
@ -730,6 +731,61 @@ class ListboxTest(AbstractWidgetTest, unittest.TestCase):
widget = self.create()
self.checkEnumParam(widget, 'state', 'disabled', 'normal')
def test_itemconfigure(self):
widget = self.create()
with self.assertRaisesRegex(TclError, 'item number "0" out of range'):
widget.itemconfigure(0)
colors = 'red orange yellow green blue white violet'.split()
widget.insert('end', *colors)
for i, color in enumerate(colors):
widget.itemconfigure(i, background=color)
with self.assertRaises(TypeError):
widget.itemconfigure()
with self.assertRaisesRegex(TclError, 'bad listbox index "red"'):
widget.itemconfigure('red')
self.assertEqual(widget.itemconfigure(0, 'background'),
('background', 'background', 'Background', '', 'red'))
self.assertEqual(widget.itemconfigure('end', 'background'),
('background', 'background', 'Background', '', 'violet'))
self.assertEqual(widget.itemconfigure('@0,0', 'background'),
('background', 'background', 'Background', '', 'red'))
d = widget.itemconfigure(0)
self.assertIsInstance(d, dict)
for k, v in d.items():
self.assertIn(len(v), (2, 5))
if len(v) == 5:
self.assertEqual(v, widget.itemconfigure(0, k))
self.assertEqual(v[4], widget.itemcget(0, k))
def check_itemconfigure(self, name, value):
widget = self.create()
widget.insert('end', 'a', 'b', 'c', 'd')
widget.itemconfigure(0, **{name: value})
self.assertEqual(widget.itemconfigure(0, name)[4], value)
self.assertEqual(widget.itemcget(0, name), value)
with self.assertRaisesRegex(TclError, 'unknown color name "spam"'):
widget.itemconfigure(0, **{name: 'spam'})
def test_itemconfigure_background(self):
self.check_itemconfigure('background', '#ff0000')
def test_itemconfigure_bg(self):
self.check_itemconfigure('bg', '#ff0000')
def test_itemconfigure_fg(self):
self.check_itemconfigure('fg', '#110022')
def test_itemconfigure_foreground(self):
self.check_itemconfigure('foreground', '#110022')
def test_itemconfigure_selectbackground(self):
self.check_itemconfigure('selectbackground', '#110022')
def test_itemconfigure_selectforeground(self):
self.check_itemconfigure('selectforeground', '#654321')
@add_standard_options(PixelSizeTests, StandardOptionsTests)
class ScaleTest(AbstractWidgetTest, unittest.TestCase):
OPTIONS = (
@ -887,6 +943,98 @@ class PanedWindowTest(AbstractWidgetTest, unittest.TestCase):
self.checkPixelsParam(widget, 'width', 402, 403.4, 404.6, -402, 0, '5i',
conv=noconv)
def create2(self):
p = self.create()
b = tkinter.Button(p)
c = tkinter.Button(p)
p.add(b)
p.add(c)
return p, b, c
def test_paneconfigure(self):
p, b, c = self.create2()
self.assertRaises(TypeError, p.paneconfigure)
d = p.paneconfigure(b)
self.assertIsInstance(d, dict)
for k, v in d.items():
self.assertEqual(len(v), 5)
self.assertEqual(v, p.paneconfigure(b, k))
self.assertEqual(v[4], p.panecget(b, k))
def check_paneconfigure(self, p, b, name, value, expected):
if not self.wantobjects:
expected = str(expected)
p.paneconfigure(b, **{name: value})
self.assertEqual(p.paneconfigure(b, name)[4], expected)
self.assertEqual(p.panecget(b, name), expected)
def check_paneconfigure_bad(self, p, b, name, msg):
with self.assertRaisesRegex(TclError, msg):
p.paneconfigure(b, **{name: 'badValue'})
def test_paneconfigure_after(self):
p, b, c = self.create2()
self.check_paneconfigure(p, b, 'after', c, str(c))
self.check_paneconfigure_bad(p, b, 'after',
'bad window path name "badValue"')
def test_paneconfigure_before(self):
p, b, c = self.create2()
self.check_paneconfigure(p, b, 'before', c, str(c))
self.check_paneconfigure_bad(p, b, 'before',
'bad window path name "badValue"')
def test_paneconfigure_height(self):
p, b, c = self.create2()
self.check_paneconfigure(p, b, 'height', 10, 10)
self.check_paneconfigure_bad(p, b, 'height',
'bad screen distance "badValue"')
def test_paneconfigure_hide(self):
p, b, c = self.create2()
self.check_paneconfigure(p, b, 'hide', False, 0)
self.check_paneconfigure_bad(p, b, 'hide',
'expected boolean value but got "badValue"')
def test_paneconfigure_minsize(self):
p, b, c = self.create2()
self.check_paneconfigure(p, b, 'minsize', 10, 10)
self.check_paneconfigure_bad(p, b, 'minsize',
'bad screen distance "badValue"')
def test_paneconfigure_padx(self):
p, b, c = self.create2()
self.check_paneconfigure(p, b, 'padx', 1.3, 1)
self.check_paneconfigure_bad(p, b, 'padx',
'bad screen distance "badValue"')
def test_paneconfigure_pady(self):
p, b, c = self.create2()
self.check_paneconfigure(p, b, 'pady', 1.3, 1)
self.check_paneconfigure_bad(p, b, 'pady',
'bad screen distance "badValue"')
def test_paneconfigure_sticky(self):
p, b, c = self.create2()
self.check_paneconfigure(p, b, 'sticky', 'nsew', 'nesw')
self.check_paneconfigure_bad(p, b, 'sticky',
'bad stickyness value "badValue": must '
'be a string containing zero or more of '
'n, e, s, and w')
def test_paneconfigure_stretch(self):
p, b, c = self.create2()
self.check_paneconfigure(p, b, 'stretch', 'alw', 'always')
self.check_paneconfigure_bad(p, b, 'stretch',
'bad stretch "badValue": must be '
'always, first, last, middle, or never')
def test_paneconfigure_width(self):
p, b, c = self.create2()
self.check_paneconfigure(p, b, 'width', 10, 10)
self.check_paneconfigure_bad(p, b, 'width',
'bad screen distance "badValue"')
@add_standard_options(StandardOptionsTests)
class MenuTest(AbstractWidgetTest, unittest.TestCase):
@ -923,6 +1071,39 @@ class MenuTest(AbstractWidgetTest, unittest.TestCase):
self.checkEnumParam(widget, 'type',
'normal', 'tearoff', 'menubar')
def test_entryconfigure(self):
m1 = self.create()
m1.add_command(label='test')
self.assertRaises(TypeError, m1.entryconfigure)
with self.assertRaisesRegex(TclError, 'bad menu entry index "foo"'):
m1.entryconfigure('foo')
d = m1.entryconfigure(1)
self.assertIsInstance(d, dict)
for k, v in d.items():
self.assertIsInstance(k, str)
self.assertIsInstance(v, tuple)
self.assertEqual(len(v), 5)
self.assertEqual(v[0], k)
self.assertEqual(m1.entrycget(1, k), v[4])
m1.destroy()
def test_entryconfigure_label(self):
m1 = self.create()
m1.add_command(label='test')
self.assertEqual(m1.entrycget(1, 'label'), 'test')
m1.entryconfigure(1, label='changed')
self.assertEqual(m1.entrycget(1, 'label'), 'changed')
def test_entryconfigure_variable(self):
m1 = self.create()
v1 = tkinter.BooleanVar(self.root)
v2 = tkinter.BooleanVar(self.root)
m1.add_checkbutton(variable=v1, onvalue=True, offvalue=False,
label='Nonsense')
self.assertEqual(str(m1.entrycget(1, 'variable')), str(v1))
m1.entryconfigure(1, variable=v2)
self.assertEqual(str(m1.entrycget(1, 'variable')), str(v2))
@add_standard_options(PixelSizeTests, StandardOptionsTests)
class MessageTest(AbstractWidgetTest, unittest.TestCase):

View File

@ -15,6 +15,12 @@ Core and Builtins
time issue noticeable when compiling code with a large number of "and"
and "or" operators.
Tests
-----
- Issue #21522: Added Tkinter tests for Listbox.itemconfigure(),
PanedWindow.paneconfigure(), and Menu.entryconfigure().
- Issue #21418: Fix a crash in the builtin function super() when called without
argument and without current frame (ex: embedded Python).