Skip to content

Commit

Permalink
[3.12] gh-104855: Update Tkinter tests for Tcl/Tk 8.7 and 9.0 (GH-120824
Browse files Browse the repository at this point in the history
) (GH-120865)

The tests are now passed with the current version of Tcl/Tk under
development (8.7b1+ and 9.0b3+).

The following changes were also made to make the tests more flexible:

* Helper methods like checkParam() now interpret the expected error message
  as a regular expression instead of a literal.
* Add support of new arguments in checkEnumParam():
  - allow_empty=True skips testing with empty string;
  - fullname= specifies the name for error message if it differs from the
    option name;
  - sort=True sorts values for error message.
* Add support of the allow_empty argument in checkReliefParam():
  allow_empty=True adds an empty string to the list of accepted values.
* Attributes _clip_highlightthickness, _clip_pad and  _clip_borderwidth
  specify how negative values of options -highlightthickness, -padx, -pady
  and -borderwidth are handled.
* Use global variables for some common error messages.

(cherry picked from commit 6ad26de)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
  • Loading branch information
3 people authored Jun 22, 2024
1 parent a57d917 commit f1acb3a
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 137 deletions.
10 changes: 9 additions & 1 deletion Lib/test/test_tcl.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,18 @@ def test_evalfile_surrogates_in_result(self):
with open(filename, 'wb') as f:
f.write(b"""
set a "<\xed\xa0\xbd\xed\xb2\xbb>"
""")
if tcl_version >= (9, 0):
self.assertRaises(TclError, tcl.evalfile, filename)
else:
tcl.evalfile(filename)
self.assertEqual(tcl.eval('set a'), '<\U0001f4bb>')

with open(filename, 'wb') as f:
f.write(b"""
set b "<\\ud83d\\udcbb>"
""")
tcl.evalfile(filename)
self.assertEqual(tcl.eval('set a'), '<\U0001f4bb>')
self.assertEqual(tcl.eval('set b'), '<\U0001f4bb>')

def testEvalFileException(self):
Expand Down
47 changes: 29 additions & 18 deletions Lib/test/test_tkinter/test_geometry_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
requires('gui')


EXPECTED_FLOAT_ERRMSG = 'expected floating-point number but got "{}"'
EXPECTED_FLOAT_OR_EMPTY_ERRMSG = 'expected floating-point number (or "" )?but got "{}"'
EXPECTED_SCREEN_DISTANCE_ERRMSG = '(bad|expected) screen distance (but got )?"{}"'
EXPECTED_SCREEN_DISTANCE_OR_EMPTY_ERRMSG = '(bad|expected) screen distance (or "" but got )?"{}"'

class PackTest(AbstractWidgetTest, unittest.TestCase):

test_keys = None
Expand Down Expand Up @@ -317,7 +322,8 @@ def test_place_configure_x(self):
self.assertEqual(f2.place_info()['x'], '-10')
self.root.update()
self.assertEqual(f2.winfo_x(), 190)
with self.assertRaisesRegex(TclError, 'bad screen distance "spam"'):
with self.assertRaisesRegex(TclError,
EXPECTED_SCREEN_DISTANCE_ERRMSG.format('spam')):
f2.place_configure(in_=f, x='spam')

def test_place_configure_y(self):
Expand All @@ -334,7 +340,8 @@ def test_place_configure_y(self):
self.assertEqual(f2.place_info()['y'], '-10')
self.root.update()
self.assertEqual(f2.winfo_y(), 110)
with self.assertRaisesRegex(TclError, 'bad screen distance "spam"'):
with self.assertRaisesRegex(TclError,
EXPECTED_SCREEN_DISTANCE_ERRMSG.format('spam')):
f2.place_configure(in_=f, y='spam')

def test_place_configure_relx(self):
Expand All @@ -351,8 +358,7 @@ def test_place_configure_relx(self):
self.assertEqual(f2.place_info()['relx'], '1')
self.root.update()
self.assertEqual(f2.winfo_x(), 200)
with self.assertRaisesRegex(TclError, 'expected floating-point number '
'but got "spam"'):
with self.assertRaisesRegex(TclError, EXPECTED_FLOAT_ERRMSG.format('spam')):
f2.place_configure(in_=f, relx='spam')

def test_place_configure_rely(self):
Expand All @@ -369,8 +375,7 @@ def test_place_configure_rely(self):
self.assertEqual(f2.place_info()['rely'], '1')
self.root.update()
self.assertEqual(f2.winfo_y(), 120)
with self.assertRaisesRegex(TclError, 'expected floating-point number '
'but got "spam"'):
with self.assertRaisesRegex(TclError, EXPECTED_FLOAT_ERRMSG.format('spam')):
f2.place_configure(in_=f, rely='spam')

def test_place_configure_anchor(self):
Expand All @@ -391,7 +396,8 @@ def test_place_configure_width(self):
f2.place_configure(width='')
self.root.update()
self.assertEqual(f2.winfo_width(), 30)
with self.assertRaisesRegex(TclError, 'bad screen distance "abcd"'):
with self.assertRaisesRegex(TclError,
EXPECTED_SCREEN_DISTANCE_OR_EMPTY_ERRMSG.format('abcd')):
f2.place_configure(width='abcd')

def test_place_configure_height(self):
Expand All @@ -402,7 +408,8 @@ def test_place_configure_height(self):
f2.place_configure(height='')
self.root.update()
self.assertEqual(f2.winfo_height(), 60)
with self.assertRaisesRegex(TclError, 'bad screen distance "abcd"'):
with self.assertRaisesRegex(TclError,
EXPECTED_SCREEN_DISTANCE_OR_EMPTY_ERRMSG.format('abcd')):
f2.place_configure(height='abcd')

def test_place_configure_relwidth(self):
Expand All @@ -413,8 +420,7 @@ def test_place_configure_relwidth(self):
f2.place_configure(relwidth='')
self.root.update()
self.assertEqual(f2.winfo_width(), 30)
with self.assertRaisesRegex(TclError, 'expected floating-point number '
'but got "abcd"'):
with self.assertRaisesRegex(TclError, EXPECTED_FLOAT_OR_EMPTY_ERRMSG.format('abcd')):
f2.place_configure(relwidth='abcd')

def test_place_configure_relheight(self):
Expand All @@ -425,8 +431,7 @@ def test_place_configure_relheight(self):
f2.place_configure(relheight='')
self.root.update()
self.assertEqual(f2.winfo_height(), 60)
with self.assertRaisesRegex(TclError, 'expected floating-point number '
'but got "abcd"'):
with self.assertRaisesRegex(TclError, EXPECTED_FLOAT_OR_EMPTY_ERRMSG.format('abcd')):
f2.place_configure(relheight='abcd')

def test_place_configure_bordermode(self):
Expand Down Expand Up @@ -629,7 +634,8 @@ def test_grid_columnconfigure(self):
self.assertEqual(self.root.grid_columnconfigure(0, 'weight'), 4)

def test_grid_columnconfigure_minsize(self):
with self.assertRaisesRegex(TclError, 'bad screen distance "foo"'):
with self.assertRaisesRegex(TclError,
EXPECTED_SCREEN_DISTANCE_ERRMSG.format('foo')):
self.root.grid_columnconfigure(0, minsize='foo')
self.root.grid_columnconfigure(0, minsize=10)
self.assertEqual(self.root.grid_columnconfigure(0, 'minsize'), 10)
Expand All @@ -646,7 +652,8 @@ def test_grid_columnconfigure_weight(self):
self.assertEqual(self.root.grid_columnconfigure(0)['weight'], 3)

def test_grid_columnconfigure_pad(self):
with self.assertRaisesRegex(TclError, 'bad screen distance "foo"'):
with self.assertRaisesRegex(TclError,
EXPECTED_SCREEN_DISTANCE_ERRMSG.format('foo')):
self.root.grid_columnconfigure(0, pad='foo')
with self.assertRaisesRegex(TclError, 'invalid arg "-pad": '
'should be non-negative'):
Expand Down Expand Up @@ -683,7 +690,8 @@ def test_grid_rowconfigure(self):
self.assertEqual(self.root.grid_rowconfigure(0, 'weight'), 4)

def test_grid_rowconfigure_minsize(self):
with self.assertRaisesRegex(TclError, 'bad screen distance "foo"'):
with self.assertRaisesRegex(TclError,
EXPECTED_SCREEN_DISTANCE_ERRMSG.format('foo')):
self.root.grid_rowconfigure(0, minsize='foo')
self.root.grid_rowconfigure(0, minsize=10)
self.assertEqual(self.root.grid_rowconfigure(0, 'minsize'), 10)
Expand All @@ -700,7 +708,8 @@ def test_grid_rowconfigure_weight(self):
self.assertEqual(self.root.grid_rowconfigure(0)['weight'], 3)

def test_grid_rowconfigure_pad(self):
with self.assertRaisesRegex(TclError, 'bad screen distance "foo"'):
with self.assertRaisesRegex(TclError,
EXPECTED_SCREEN_DISTANCE_ERRMSG.format('foo')):
self.root.grid_rowconfigure(0, pad='foo')
with self.assertRaisesRegex(TclError, 'invalid arg "-pad": '
'should be non-negative'):
Expand Down Expand Up @@ -818,9 +827,11 @@ def test_grid_location(self):
self.root.grid_location(0)
with self.assertRaises(TypeError):
self.root.grid_location(0, 0, 0)
with self.assertRaisesRegex(TclError, 'bad screen distance "x"'):
with self.assertRaisesRegex(TclError,
EXPECTED_SCREEN_DISTANCE_ERRMSG.format('x')):
self.root.grid_location('x', 'y')
with self.assertRaisesRegex(TclError, 'bad screen distance "y"'):
with self.assertRaisesRegex(TclError,
EXPECTED_SCREEN_DISTANCE_ERRMSG.format('y')):
self.root.grid_location('1c', 'y')
t = self.root
# de-maximize
Expand Down
4 changes: 3 additions & 1 deletion Lib/test/test_tkinter/test_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from tkinter import (Variable, StringVar, IntVar, DoubleVar, BooleanVar, Tcl,
TclError)
from test.support import ALWAYS_EQ
from test.test_tkinter.support import AbstractDefaultRootTest
from test.test_tkinter.support import AbstractDefaultRootTest, tcl_version


class Var(Variable):
Expand Down Expand Up @@ -112,6 +112,8 @@ def test_initialize(self):
self.assertTrue(v.side_effect)

def test_trace_old(self):
if tcl_version >= (9, 0):
self.skipTest('requires Tcl version < 9.0')
# Old interface
v = Variable(self.root)
vname = str(v)
Expand Down
Loading

0 comments on commit f1acb3a

Please sign in to comment.