mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-22 21:32:31 +00:00
python-3.6.zip added from Github
README.cosmo contains the necessary links.
This commit is contained in:
parent
75fc601ff5
commit
0c4c56ff39
4219 changed files with 1968626 additions and 0 deletions
0
third_party/python/Lib/tkinter/test/test_ttk/__init__.py
vendored
Normal file
0
third_party/python/Lib/tkinter/test/test_ttk/__init__.py
vendored
Normal file
323
third_party/python/Lib/tkinter/test/test_ttk/test_extensions.py
vendored
Normal file
323
third_party/python/Lib/tkinter/test/test_ttk/test_extensions.py
vendored
Normal file
|
@ -0,0 +1,323 @@
|
|||
import sys
|
||||
import unittest
|
||||
import tkinter
|
||||
from tkinter import ttk
|
||||
from test.support import requires, run_unittest, swap_attr
|
||||
from tkinter.test.support import AbstractTkTest, destroy_default_root
|
||||
|
||||
requires('gui')
|
||||
|
||||
class LabeledScaleTest(AbstractTkTest, unittest.TestCase):
|
||||
|
||||
def tearDown(self):
|
||||
self.root.update_idletasks()
|
||||
super().tearDown()
|
||||
|
||||
def test_widget_destroy(self):
|
||||
# automatically created variable
|
||||
x = ttk.LabeledScale(self.root)
|
||||
var = x._variable._name
|
||||
x.destroy()
|
||||
self.assertRaises(tkinter.TclError, x.tk.globalgetvar, var)
|
||||
|
||||
# manually created variable
|
||||
myvar = tkinter.DoubleVar(self.root)
|
||||
name = myvar._name
|
||||
x = ttk.LabeledScale(self.root, variable=myvar)
|
||||
x.destroy()
|
||||
if self.wantobjects:
|
||||
self.assertEqual(x.tk.globalgetvar(name), myvar.get())
|
||||
else:
|
||||
self.assertEqual(float(x.tk.globalgetvar(name)), myvar.get())
|
||||
del myvar
|
||||
self.assertRaises(tkinter.TclError, x.tk.globalgetvar, name)
|
||||
|
||||
# checking that the tracing callback is properly removed
|
||||
myvar = tkinter.IntVar(self.root)
|
||||
# LabeledScale will start tracing myvar
|
||||
x = ttk.LabeledScale(self.root, variable=myvar)
|
||||
x.destroy()
|
||||
# Unless the tracing callback was removed, creating a new
|
||||
# LabeledScale with the same var will cause an error now. This
|
||||
# happens because the variable will be set to (possibly) a new
|
||||
# value which causes the tracing callback to be called and then
|
||||
# it tries calling instance attributes not yet defined.
|
||||
ttk.LabeledScale(self.root, variable=myvar)
|
||||
if hasattr(sys, 'last_type'):
|
||||
self.assertNotEqual(sys.last_type, tkinter.TclError)
|
||||
|
||||
|
||||
def test_initialization_no_master(self):
|
||||
# no master passing
|
||||
with swap_attr(tkinter, '_default_root', None), \
|
||||
swap_attr(tkinter, '_support_default_root', True):
|
||||
try:
|
||||
x = ttk.LabeledScale()
|
||||
self.assertIsNotNone(tkinter._default_root)
|
||||
self.assertEqual(x.master, tkinter._default_root)
|
||||
self.assertEqual(x.tk, tkinter._default_root.tk)
|
||||
x.destroy()
|
||||
finally:
|
||||
destroy_default_root()
|
||||
|
||||
def test_initialization(self):
|
||||
# master passing
|
||||
master = tkinter.Frame(self.root)
|
||||
x = ttk.LabeledScale(master)
|
||||
self.assertEqual(x.master, master)
|
||||
x.destroy()
|
||||
|
||||
# variable initialization/passing
|
||||
passed_expected = (('0', 0), (0, 0), (10, 10),
|
||||
(-1, -1), (sys.maxsize + 1, sys.maxsize + 1),
|
||||
(2.5, 2), ('2.5', 2))
|
||||
for pair in passed_expected:
|
||||
x = ttk.LabeledScale(self.root, from_=pair[0])
|
||||
self.assertEqual(x.value, pair[1])
|
||||
x.destroy()
|
||||
x = ttk.LabeledScale(self.root, from_=None)
|
||||
self.assertRaises((ValueError, tkinter.TclError), x._variable.get)
|
||||
x.destroy()
|
||||
# variable should have its default value set to the from_ value
|
||||
myvar = tkinter.DoubleVar(self.root, value=20)
|
||||
x = ttk.LabeledScale(self.root, variable=myvar)
|
||||
self.assertEqual(x.value, 0)
|
||||
x.destroy()
|
||||
# check that it is really using a DoubleVar
|
||||
x = ttk.LabeledScale(self.root, variable=myvar, from_=0.5)
|
||||
self.assertEqual(x.value, 0.5)
|
||||
self.assertEqual(x._variable._name, myvar._name)
|
||||
x.destroy()
|
||||
|
||||
# widget positionment
|
||||
def check_positions(scale, scale_pos, label, label_pos):
|
||||
self.assertEqual(scale.pack_info()['side'], scale_pos)
|
||||
self.assertEqual(label.place_info()['anchor'], label_pos)
|
||||
x = ttk.LabeledScale(self.root, compound='top')
|
||||
check_positions(x.scale, 'bottom', x.label, 'n')
|
||||
x.destroy()
|
||||
x = ttk.LabeledScale(self.root, compound='bottom')
|
||||
check_positions(x.scale, 'top', x.label, 's')
|
||||
x.destroy()
|
||||
# invert default positions
|
||||
x = ttk.LabeledScale(self.root, compound='unknown')
|
||||
check_positions(x.scale, 'top', x.label, 's')
|
||||
x.destroy()
|
||||
x = ttk.LabeledScale(self.root) # take default positions
|
||||
check_positions(x.scale, 'bottom', x.label, 'n')
|
||||
x.destroy()
|
||||
|
||||
# extra, and invalid, kwargs
|
||||
self.assertRaises(tkinter.TclError, ttk.LabeledScale, master, a='b')
|
||||
|
||||
|
||||
def test_horizontal_range(self):
|
||||
lscale = ttk.LabeledScale(self.root, from_=0, to=10)
|
||||
lscale.pack()
|
||||
lscale.wait_visibility()
|
||||
lscale.update()
|
||||
|
||||
linfo_1 = lscale.label.place_info()
|
||||
prev_xcoord = lscale.scale.coords()[0]
|
||||
self.assertEqual(prev_xcoord, int(linfo_1['x']))
|
||||
# change range to: from -5 to 5. This should change the x coord of
|
||||
# the scale widget, since 0 is at the middle of the new
|
||||
# range.
|
||||
lscale.scale.configure(from_=-5, to=5)
|
||||
# The following update is needed since the test doesn't use mainloop,
|
||||
# at the same time this shouldn't affect test outcome
|
||||
lscale.update()
|
||||
curr_xcoord = lscale.scale.coords()[0]
|
||||
self.assertNotEqual(prev_xcoord, curr_xcoord)
|
||||
# the label widget should have been repositioned too
|
||||
linfo_2 = lscale.label.place_info()
|
||||
self.assertEqual(lscale.label['text'], 0 if self.wantobjects else '0')
|
||||
self.assertEqual(curr_xcoord, int(linfo_2['x']))
|
||||
# change the range back
|
||||
lscale.scale.configure(from_=0, to=10)
|
||||
self.assertNotEqual(prev_xcoord, curr_xcoord)
|
||||
self.assertEqual(prev_xcoord, int(linfo_1['x']))
|
||||
|
||||
lscale.destroy()
|
||||
|
||||
|
||||
def test_variable_change(self):
|
||||
x = ttk.LabeledScale(self.root)
|
||||
x.pack()
|
||||
x.wait_visibility()
|
||||
x.update()
|
||||
|
||||
curr_xcoord = x.scale.coords()[0]
|
||||
newval = x.value + 1
|
||||
x.value = newval
|
||||
# The following update is needed since the test doesn't use mainloop,
|
||||
# at the same time this shouldn't affect test outcome
|
||||
x.update()
|
||||
self.assertEqual(x.value, newval)
|
||||
self.assertEqual(x.label['text'],
|
||||
newval if self.wantobjects else str(newval))
|
||||
self.assertEqual(float(x.scale.get()), newval)
|
||||
self.assertGreater(x.scale.coords()[0], curr_xcoord)
|
||||
self.assertEqual(x.scale.coords()[0],
|
||||
int(x.label.place_info()['x']))
|
||||
|
||||
# value outside range
|
||||
if self.wantobjects:
|
||||
conv = lambda x: x
|
||||
else:
|
||||
conv = int
|
||||
x.value = conv(x.scale['to']) + 1 # no changes shouldn't happen
|
||||
x.update()
|
||||
self.assertEqual(x.value, newval)
|
||||
self.assertEqual(conv(x.label['text']), newval)
|
||||
self.assertEqual(float(x.scale.get()), newval)
|
||||
self.assertEqual(x.scale.coords()[0],
|
||||
int(x.label.place_info()['x']))
|
||||
|
||||
# non-integer value
|
||||
x.value = newval = newval + 1.5
|
||||
x.update()
|
||||
self.assertEqual(x.value, int(newval))
|
||||
self.assertEqual(conv(x.label['text']), int(newval))
|
||||
self.assertEqual(float(x.scale.get()), newval)
|
||||
|
||||
x.destroy()
|
||||
|
||||
|
||||
def test_resize(self):
|
||||
x = ttk.LabeledScale(self.root)
|
||||
x.pack(expand=True, fill='both')
|
||||
x.wait_visibility()
|
||||
x.update()
|
||||
|
||||
width, height = x.master.winfo_width(), x.master.winfo_height()
|
||||
width_new, height_new = width * 2, height * 2
|
||||
|
||||
x.value = 3
|
||||
x.update()
|
||||
x.master.wm_geometry("%dx%d" % (width_new, height_new))
|
||||
self.assertEqual(int(x.label.place_info()['x']),
|
||||
x.scale.coords()[0])
|
||||
|
||||
# Reset geometry
|
||||
x.master.wm_geometry("%dx%d" % (width, height))
|
||||
x.destroy()
|
||||
|
||||
|
||||
class OptionMenuTest(AbstractTkTest, unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.textvar = tkinter.StringVar(self.root)
|
||||
|
||||
def tearDown(self):
|
||||
del self.textvar
|
||||
super().tearDown()
|
||||
|
||||
|
||||
def test_widget_destroy(self):
|
||||
var = tkinter.StringVar(self.root)
|
||||
optmenu = ttk.OptionMenu(self.root, var)
|
||||
name = var._name
|
||||
optmenu.update_idletasks()
|
||||
optmenu.destroy()
|
||||
self.assertEqual(optmenu.tk.globalgetvar(name), var.get())
|
||||
del var
|
||||
self.assertRaises(tkinter.TclError, optmenu.tk.globalgetvar, name)
|
||||
|
||||
|
||||
def test_initialization(self):
|
||||
self.assertRaises(tkinter.TclError,
|
||||
ttk.OptionMenu, self.root, self.textvar, invalid='thing')
|
||||
|
||||
optmenu = ttk.OptionMenu(self.root, self.textvar, 'b', 'a', 'b')
|
||||
self.assertEqual(optmenu._variable.get(), 'b')
|
||||
|
||||
self.assertTrue(optmenu['menu'])
|
||||
self.assertTrue(optmenu['textvariable'])
|
||||
|
||||
optmenu.destroy()
|
||||
|
||||
|
||||
def test_menu(self):
|
||||
items = ('a', 'b', 'c')
|
||||
default = 'a'
|
||||
optmenu = ttk.OptionMenu(self.root, self.textvar, default, *items)
|
||||
found_default = False
|
||||
for i in range(len(items)):
|
||||
value = optmenu['menu'].entrycget(i, 'value')
|
||||
self.assertEqual(value, items[i])
|
||||
if value == default:
|
||||
found_default = True
|
||||
self.assertTrue(found_default)
|
||||
optmenu.destroy()
|
||||
|
||||
# default shouldn't be in menu if it is not part of values
|
||||
default = 'd'
|
||||
optmenu = ttk.OptionMenu(self.root, self.textvar, default, *items)
|
||||
curr = None
|
||||
i = 0
|
||||
while True:
|
||||
last, curr = curr, optmenu['menu'].entryconfigure(i, 'value')
|
||||
if last == curr:
|
||||
# no more menu entries
|
||||
break
|
||||
self.assertNotEqual(curr, default)
|
||||
i += 1
|
||||
self.assertEqual(i, len(items))
|
||||
|
||||
# check that variable is updated correctly
|
||||
optmenu.pack()
|
||||
optmenu.wait_visibility()
|
||||
optmenu['menu'].invoke(0)
|
||||
self.assertEqual(optmenu._variable.get(), items[0])
|
||||
|
||||
# changing to an invalid index shouldn't change the variable
|
||||
self.assertRaises(tkinter.TclError, optmenu['menu'].invoke, -1)
|
||||
self.assertEqual(optmenu._variable.get(), items[0])
|
||||
|
||||
optmenu.destroy()
|
||||
|
||||
# specifying a callback
|
||||
success = []
|
||||
def cb_test(item):
|
||||
self.assertEqual(item, items[1])
|
||||
success.append(True)
|
||||
optmenu = ttk.OptionMenu(self.root, self.textvar, 'a', command=cb_test,
|
||||
*items)
|
||||
optmenu['menu'].invoke(1)
|
||||
if not success:
|
||||
self.fail("Menu callback not invoked")
|
||||
|
||||
optmenu.destroy()
|
||||
|
||||
def test_unique_radiobuttons(self):
|
||||
# check that radiobuttons are unique across instances (bpo25684)
|
||||
items = ('a', 'b', 'c')
|
||||
default = 'a'
|
||||
optmenu = ttk.OptionMenu(self.root, self.textvar, default, *items)
|
||||
textvar2 = tkinter.StringVar(self.root)
|
||||
optmenu2 = ttk.OptionMenu(self.root, textvar2, default, *items)
|
||||
optmenu.pack()
|
||||
optmenu.wait_visibility()
|
||||
optmenu2.pack()
|
||||
optmenu2.wait_visibility()
|
||||
optmenu['menu'].invoke(1)
|
||||
optmenu2['menu'].invoke(2)
|
||||
optmenu_stringvar_name = optmenu['menu'].entrycget(0, 'variable')
|
||||
optmenu2_stringvar_name = optmenu2['menu'].entrycget(0, 'variable')
|
||||
self.assertNotEqual(optmenu_stringvar_name,
|
||||
optmenu2_stringvar_name)
|
||||
self.assertEqual(self.root.tk.globalgetvar(optmenu_stringvar_name),
|
||||
items[1])
|
||||
self.assertEqual(self.root.tk.globalgetvar(optmenu2_stringvar_name),
|
||||
items[2])
|
||||
|
||||
optmenu.destroy()
|
||||
optmenu2.destroy()
|
||||
|
||||
|
||||
tests_gui = (LabeledScaleTest, OptionMenuTest)
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_unittest(*tests_gui)
|
461
third_party/python/Lib/tkinter/test/test_ttk/test_functions.py
vendored
Normal file
461
third_party/python/Lib/tkinter/test/test_ttk/test_functions.py
vendored
Normal file
|
@ -0,0 +1,461 @@
|
|||
# -*- encoding: utf-8 -*-
|
||||
import unittest
|
||||
from tkinter import ttk
|
||||
|
||||
class MockTkApp:
|
||||
|
||||
def splitlist(self, arg):
|
||||
if isinstance(arg, tuple):
|
||||
return arg
|
||||
return arg.split(':')
|
||||
|
||||
def wantobjects(self):
|
||||
return True
|
||||
|
||||
|
||||
class MockTclObj(object):
|
||||
typename = 'test'
|
||||
|
||||
def __init__(self, val):
|
||||
self.val = val
|
||||
|
||||
def __str__(self):
|
||||
return str(self.val)
|
||||
|
||||
|
||||
class MockStateSpec(object):
|
||||
typename = 'StateSpec'
|
||||
|
||||
def __init__(self, *args):
|
||||
self.val = args
|
||||
|
||||
def __str__(self):
|
||||
return ' '.join(self.val)
|
||||
|
||||
|
||||
class InternalFunctionsTest(unittest.TestCase):
|
||||
|
||||
def test_format_optdict(self):
|
||||
def check_against(fmt_opts, result):
|
||||
for i in range(0, len(fmt_opts), 2):
|
||||
self.assertEqual(result.pop(fmt_opts[i]), fmt_opts[i + 1])
|
||||
if result:
|
||||
self.fail("result still got elements: %s" % result)
|
||||
|
||||
# passing an empty dict should return an empty object (tuple here)
|
||||
self.assertFalse(ttk._format_optdict({}))
|
||||
|
||||
# check list formatting
|
||||
check_against(
|
||||
ttk._format_optdict({'fg': 'blue', 'padding': [1, 2, 3, 4]}),
|
||||
{'-fg': 'blue', '-padding': '1 2 3 4'})
|
||||
|
||||
# check tuple formatting (same as list)
|
||||
check_against(
|
||||
ttk._format_optdict({'test': (1, 2, '', 0)}),
|
||||
{'-test': '1 2 {} 0'})
|
||||
|
||||
# check untouched values
|
||||
check_against(
|
||||
ttk._format_optdict({'test': {'left': 'as is'}}),
|
||||
{'-test': {'left': 'as is'}})
|
||||
|
||||
# check script formatting
|
||||
check_against(
|
||||
ttk._format_optdict(
|
||||
{'test': [1, -1, '', '2m', 0], 'test2': 3,
|
||||
'test3': '', 'test4': 'abc def',
|
||||
'test5': '"abc"', 'test6': '{}',
|
||||
'test7': '} -spam {'}, script=True),
|
||||
{'-test': '{1 -1 {} 2m 0}', '-test2': '3',
|
||||
'-test3': '{}', '-test4': '{abc def}',
|
||||
'-test5': '{"abc"}', '-test6': r'\{\}',
|
||||
'-test7': r'\}\ -spam\ \{'})
|
||||
|
||||
opts = {'αβγ': True, 'á': False}
|
||||
orig_opts = opts.copy()
|
||||
# check if giving unicode keys is fine
|
||||
check_against(ttk._format_optdict(opts), {'-αβγ': True, '-á': False})
|
||||
# opts should remain unchanged
|
||||
self.assertEqual(opts, orig_opts)
|
||||
|
||||
# passing values with spaces inside a tuple/list
|
||||
check_against(
|
||||
ttk._format_optdict(
|
||||
{'option': ('one two', 'three')}),
|
||||
{'-option': '{one two} three'})
|
||||
check_against(
|
||||
ttk._format_optdict(
|
||||
{'option': ('one\ttwo', 'three')}),
|
||||
{'-option': '{one\ttwo} three'})
|
||||
|
||||
# passing empty strings inside a tuple/list
|
||||
check_against(
|
||||
ttk._format_optdict(
|
||||
{'option': ('', 'one')}),
|
||||
{'-option': '{} one'})
|
||||
|
||||
# passing values with braces inside a tuple/list
|
||||
check_against(
|
||||
ttk._format_optdict(
|
||||
{'option': ('one} {two', 'three')}),
|
||||
{'-option': r'one\}\ \{two three'})
|
||||
|
||||
# passing quoted strings inside a tuple/list
|
||||
check_against(
|
||||
ttk._format_optdict(
|
||||
{'option': ('"one"', 'two')}),
|
||||
{'-option': '{"one"} two'})
|
||||
check_against(
|
||||
ttk._format_optdict(
|
||||
{'option': ('{one}', 'two')}),
|
||||
{'-option': r'\{one\} two'})
|
||||
|
||||
# ignore an option
|
||||
amount_opts = len(ttk._format_optdict(opts, ignore=('á'))) / 2
|
||||
self.assertEqual(amount_opts, len(opts) - 1)
|
||||
|
||||
# ignore non-existing options
|
||||
amount_opts = len(ttk._format_optdict(opts, ignore=('á', 'b'))) / 2
|
||||
self.assertEqual(amount_opts, len(opts) - 1)
|
||||
|
||||
# ignore every option
|
||||
self.assertFalse(ttk._format_optdict(opts, ignore=list(opts.keys())))
|
||||
|
||||
|
||||
def test_format_mapdict(self):
|
||||
opts = {'a': [('b', 'c', 'val'), ('d', 'otherval'), ('', 'single')]}
|
||||
result = ttk._format_mapdict(opts)
|
||||
self.assertEqual(len(result), len(list(opts.keys())) * 2)
|
||||
self.assertEqual(result, ('-a', '{b c} val d otherval {} single'))
|
||||
self.assertEqual(ttk._format_mapdict(opts, script=True),
|
||||
('-a', '{{b c} val d otherval {} single}'))
|
||||
|
||||
self.assertEqual(ttk._format_mapdict({2: []}), ('-2', ''))
|
||||
|
||||
opts = {'üñíćódè': [('á', 'vãl')]}
|
||||
result = ttk._format_mapdict(opts)
|
||||
self.assertEqual(result, ('-üñíćódè', 'á vãl'))
|
||||
|
||||
# empty states
|
||||
valid = {'opt': [('', '', 'hi')]}
|
||||
self.assertEqual(ttk._format_mapdict(valid), ('-opt', '{ } hi'))
|
||||
|
||||
# when passing multiple states, they all must be strings
|
||||
invalid = {'opt': [(1, 2, 'valid val')]}
|
||||
self.assertRaises(TypeError, ttk._format_mapdict, invalid)
|
||||
invalid = {'opt': [([1], '2', 'valid val')]}
|
||||
self.assertRaises(TypeError, ttk._format_mapdict, invalid)
|
||||
# but when passing a single state, it can be anything
|
||||
valid = {'opt': [[1, 'value']]}
|
||||
self.assertEqual(ttk._format_mapdict(valid), ('-opt', '1 value'))
|
||||
# special attention to single states which evaluate to False
|
||||
for stateval in (None, 0, False, '', set()): # just some samples
|
||||
valid = {'opt': [(stateval, 'value')]}
|
||||
self.assertEqual(ttk._format_mapdict(valid),
|
||||
('-opt', '{} value'))
|
||||
|
||||
# values must be iterable
|
||||
opts = {'a': None}
|
||||
self.assertRaises(TypeError, ttk._format_mapdict, opts)
|
||||
|
||||
# items in the value must have size >= 2
|
||||
self.assertRaises(IndexError, ttk._format_mapdict,
|
||||
{'a': [('invalid', )]})
|
||||
|
||||
|
||||
def test_format_elemcreate(self):
|
||||
self.assertTrue(ttk._format_elemcreate(None), (None, ()))
|
||||
|
||||
## Testing type = image
|
||||
# image type expects at least an image name, so this should raise
|
||||
# IndexError since it tries to access the index 0 of an empty tuple
|
||||
self.assertRaises(IndexError, ttk._format_elemcreate, 'image')
|
||||
|
||||
# don't format returned values as a tcl script
|
||||
# minimum acceptable for image type
|
||||
self.assertEqual(ttk._format_elemcreate('image', False, 'test'),
|
||||
("test ", ()))
|
||||
# specifying a state spec
|
||||
self.assertEqual(ttk._format_elemcreate('image', False, 'test',
|
||||
('', 'a')), ("test {} a", ()))
|
||||
# state spec with multiple states
|
||||
self.assertEqual(ttk._format_elemcreate('image', False, 'test',
|
||||
('a', 'b', 'c')), ("test {a b} c", ()))
|
||||
# state spec and options
|
||||
self.assertEqual(ttk._format_elemcreate('image', False, 'test',
|
||||
('a', 'b'), a='x'), ("test a b", ("-a", "x")))
|
||||
# format returned values as a tcl script
|
||||
# state spec with multiple states and an option with a multivalue
|
||||
self.assertEqual(ttk._format_elemcreate('image', True, 'test',
|
||||
('a', 'b', 'c', 'd'), x=[2, 3]), ("{test {a b c} d}", "-x {2 3}"))
|
||||
|
||||
## Testing type = vsapi
|
||||
# vsapi type expects at least a class name and a part_id, so this
|
||||
# should raise a ValueError since it tries to get two elements from
|
||||
# an empty tuple
|
||||
self.assertRaises(ValueError, ttk._format_elemcreate, 'vsapi')
|
||||
|
||||
# don't format returned values as a tcl script
|
||||
# minimum acceptable for vsapi
|
||||
self.assertEqual(ttk._format_elemcreate('vsapi', False, 'a', 'b'),
|
||||
("a b ", ()))
|
||||
# now with a state spec with multiple states
|
||||
self.assertEqual(ttk._format_elemcreate('vsapi', False, 'a', 'b',
|
||||
('a', 'b', 'c')), ("a b {a b} c", ()))
|
||||
# state spec and option
|
||||
self.assertEqual(ttk._format_elemcreate('vsapi', False, 'a', 'b',
|
||||
('a', 'b'), opt='x'), ("a b a b", ("-opt", "x")))
|
||||
# format returned values as a tcl script
|
||||
# state spec with a multivalue and an option
|
||||
self.assertEqual(ttk._format_elemcreate('vsapi', True, 'a', 'b',
|
||||
('a', 'b', [1, 2]), opt='x'), ("{a b {a b} {1 2}}", "-opt x"))
|
||||
|
||||
# Testing type = from
|
||||
# from type expects at least a type name
|
||||
self.assertRaises(IndexError, ttk._format_elemcreate, 'from')
|
||||
|
||||
self.assertEqual(ttk._format_elemcreate('from', False, 'a'),
|
||||
('a', ()))
|
||||
self.assertEqual(ttk._format_elemcreate('from', False, 'a', 'b'),
|
||||
('a', ('b', )))
|
||||
self.assertEqual(ttk._format_elemcreate('from', True, 'a', 'b'),
|
||||
('{a}', 'b'))
|
||||
|
||||
|
||||
def test_format_layoutlist(self):
|
||||
def sample(indent=0, indent_size=2):
|
||||
return ttk._format_layoutlist(
|
||||
[('a', {'other': [1, 2, 3], 'children':
|
||||
[('b', {'children':
|
||||
[('c', {'children':
|
||||
[('d', {'nice': 'opt'})], 'something': (1, 2)
|
||||
})]
|
||||
})]
|
||||
})], indent=indent, indent_size=indent_size)[0]
|
||||
|
||||
def sample_expected(indent=0, indent_size=2):
|
||||
spaces = lambda amount=0: ' ' * (amount + indent)
|
||||
return (
|
||||
"%sa -other {1 2 3} -children {\n"
|
||||
"%sb -children {\n"
|
||||
"%sc -something {1 2} -children {\n"
|
||||
"%sd -nice opt\n"
|
||||
"%s}\n"
|
||||
"%s}\n"
|
||||
"%s}" % (spaces(), spaces(indent_size),
|
||||
spaces(2 * indent_size), spaces(3 * indent_size),
|
||||
spaces(2 * indent_size), spaces(indent_size), spaces()))
|
||||
|
||||
# empty layout
|
||||
self.assertEqual(ttk._format_layoutlist([])[0], '')
|
||||
|
||||
# _format_layoutlist always expects the second item (in every item)
|
||||
# to act like a dict (except when the value evaluates to False).
|
||||
self.assertRaises(AttributeError,
|
||||
ttk._format_layoutlist, [('a', 'b')])
|
||||
|
||||
smallest = ttk._format_layoutlist([('a', None)], indent=0)
|
||||
self.assertEqual(smallest,
|
||||
ttk._format_layoutlist([('a', '')], indent=0))
|
||||
self.assertEqual(smallest[0], 'a')
|
||||
|
||||
# testing indentation levels
|
||||
self.assertEqual(sample(), sample_expected())
|
||||
for i in range(4):
|
||||
self.assertEqual(sample(i), sample_expected(i))
|
||||
self.assertEqual(sample(i, i), sample_expected(i, i))
|
||||
|
||||
# invalid layout format, different kind of exceptions will be
|
||||
# raised by internal functions
|
||||
|
||||
# plain wrong format
|
||||
self.assertRaises(ValueError, ttk._format_layoutlist,
|
||||
['bad', 'format'])
|
||||
# will try to use iteritems in the 'bad' string
|
||||
self.assertRaises(AttributeError, ttk._format_layoutlist,
|
||||
[('name', 'bad')])
|
||||
# bad children formatting
|
||||
self.assertRaises(ValueError, ttk._format_layoutlist,
|
||||
[('name', {'children': {'a': None}})])
|
||||
|
||||
|
||||
def test_script_from_settings(self):
|
||||
# empty options
|
||||
self.assertFalse(ttk._script_from_settings({'name':
|
||||
{'configure': None, 'map': None, 'element create': None}}))
|
||||
|
||||
# empty layout
|
||||
self.assertEqual(
|
||||
ttk._script_from_settings({'name': {'layout': None}}),
|
||||
"ttk::style layout name {\nnull\n}")
|
||||
|
||||
configdict = {'αβγ': True, 'á': False}
|
||||
self.assertTrue(
|
||||
ttk._script_from_settings({'name': {'configure': configdict}}))
|
||||
|
||||
mapdict = {'üñíćódè': [('á', 'vãl')]}
|
||||
self.assertTrue(
|
||||
ttk._script_from_settings({'name': {'map': mapdict}}))
|
||||
|
||||
# invalid image element
|
||||
self.assertRaises(IndexError,
|
||||
ttk._script_from_settings, {'name': {'element create': ['image']}})
|
||||
|
||||
# minimal valid image
|
||||
self.assertTrue(ttk._script_from_settings({'name':
|
||||
{'element create': ['image', 'name']}}))
|
||||
|
||||
image = {'thing': {'element create':
|
||||
['image', 'name', ('state1', 'state2', 'val')]}}
|
||||
self.assertEqual(ttk._script_from_settings(image),
|
||||
"ttk::style element create thing image {name {state1 state2} val} ")
|
||||
|
||||
image['thing']['element create'].append({'opt': 30})
|
||||
self.assertEqual(ttk._script_from_settings(image),
|
||||
"ttk::style element create thing image {name {state1 state2} val} "
|
||||
"-opt 30")
|
||||
|
||||
image['thing']['element create'][-1]['opt'] = [MockTclObj(3),
|
||||
MockTclObj('2m')]
|
||||
self.assertEqual(ttk._script_from_settings(image),
|
||||
"ttk::style element create thing image {name {state1 state2} val} "
|
||||
"-opt {3 2m}")
|
||||
|
||||
|
||||
def test_tclobj_to_py(self):
|
||||
self.assertEqual(
|
||||
ttk._tclobj_to_py((MockStateSpec('a', 'b'), 'val')),
|
||||
[('a', 'b', 'val')])
|
||||
self.assertEqual(
|
||||
ttk._tclobj_to_py([MockTclObj('1'), 2, MockTclObj('3m')]),
|
||||
[1, 2, '3m'])
|
||||
|
||||
|
||||
def test_list_from_statespec(self):
|
||||
def test_it(sspec, value, res_value, states):
|
||||
self.assertEqual(ttk._list_from_statespec(
|
||||
(sspec, value)), [states + (res_value, )])
|
||||
|
||||
states_even = tuple('state%d' % i for i in range(6))
|
||||
statespec = MockStateSpec(*states_even)
|
||||
test_it(statespec, 'val', 'val', states_even)
|
||||
test_it(statespec, MockTclObj('val'), 'val', states_even)
|
||||
|
||||
states_odd = tuple('state%d' % i for i in range(5))
|
||||
statespec = MockStateSpec(*states_odd)
|
||||
test_it(statespec, 'val', 'val', states_odd)
|
||||
|
||||
test_it(('a', 'b', 'c'), MockTclObj('val'), 'val', ('a', 'b', 'c'))
|
||||
|
||||
|
||||
def test_list_from_layouttuple(self):
|
||||
tk = MockTkApp()
|
||||
|
||||
# empty layout tuple
|
||||
self.assertFalse(ttk._list_from_layouttuple(tk, ()))
|
||||
|
||||
# shortest layout tuple
|
||||
self.assertEqual(ttk._list_from_layouttuple(tk, ('name', )),
|
||||
[('name', {})])
|
||||
|
||||
# not so interesting ltuple
|
||||
sample_ltuple = ('name', '-option', 'value')
|
||||
self.assertEqual(ttk._list_from_layouttuple(tk, sample_ltuple),
|
||||
[('name', {'option': 'value'})])
|
||||
|
||||
# empty children
|
||||
self.assertEqual(ttk._list_from_layouttuple(tk,
|
||||
('something', '-children', ())),
|
||||
[('something', {'children': []})]
|
||||
)
|
||||
|
||||
# more interesting ltuple
|
||||
ltuple = (
|
||||
'name', '-option', 'niceone', '-children', (
|
||||
('otherone', '-children', (
|
||||
('child', )), '-otheropt', 'othervalue'
|
||||
)
|
||||
)
|
||||
)
|
||||
self.assertEqual(ttk._list_from_layouttuple(tk, ltuple),
|
||||
[('name', {'option': 'niceone', 'children':
|
||||
[('otherone', {'otheropt': 'othervalue', 'children':
|
||||
[('child', {})]
|
||||
})]
|
||||
})]
|
||||
)
|
||||
|
||||
# bad tuples
|
||||
self.assertRaises(ValueError, ttk._list_from_layouttuple, tk,
|
||||
('name', 'no_minus'))
|
||||
self.assertRaises(ValueError, ttk._list_from_layouttuple, tk,
|
||||
('name', 'no_minus', 'value'))
|
||||
self.assertRaises(ValueError, ttk._list_from_layouttuple, tk,
|
||||
('something', '-children')) # no children
|
||||
|
||||
|
||||
def test_val_or_dict(self):
|
||||
def func(res, opt=None, val=None):
|
||||
if opt is None:
|
||||
return res
|
||||
if val is None:
|
||||
return "test val"
|
||||
return (opt, val)
|
||||
|
||||
tk = MockTkApp()
|
||||
tk.call = func
|
||||
|
||||
self.assertEqual(ttk._val_or_dict(tk, {}, '-test:3'),
|
||||
{'test': '3'})
|
||||
self.assertEqual(ttk._val_or_dict(tk, {}, ('-test', 3)),
|
||||
{'test': 3})
|
||||
|
||||
self.assertEqual(ttk._val_or_dict(tk, {'test': None}, 'x:y'),
|
||||
'test val')
|
||||
|
||||
self.assertEqual(ttk._val_or_dict(tk, {'test': 3}, 'x:y'),
|
||||
{'test': 3})
|
||||
|
||||
|
||||
def test_convert_stringval(self):
|
||||
tests = (
|
||||
(0, 0), ('09', 9), ('a', 'a'), ('áÚ', 'áÚ'), ([], '[]'),
|
||||
(None, 'None')
|
||||
)
|
||||
for orig, expected in tests:
|
||||
self.assertEqual(ttk._convert_stringval(orig), expected)
|
||||
|
||||
|
||||
class TclObjsToPyTest(unittest.TestCase):
|
||||
|
||||
def test_unicode(self):
|
||||
adict = {'opt': 'välúè'}
|
||||
self.assertEqual(ttk.tclobjs_to_py(adict), {'opt': 'välúè'})
|
||||
|
||||
adict['opt'] = MockTclObj(adict['opt'])
|
||||
self.assertEqual(ttk.tclobjs_to_py(adict), {'opt': 'välúè'})
|
||||
|
||||
def test_multivalues(self):
|
||||
adict = {'opt': [1, 2, 3, 4]}
|
||||
self.assertEqual(ttk.tclobjs_to_py(adict), {'opt': [1, 2, 3, 4]})
|
||||
|
||||
adict['opt'] = [1, 'xm', 3]
|
||||
self.assertEqual(ttk.tclobjs_to_py(adict), {'opt': [1, 'xm', 3]})
|
||||
|
||||
adict['opt'] = (MockStateSpec('a', 'b'), 'válũè')
|
||||
self.assertEqual(ttk.tclobjs_to_py(adict),
|
||||
{'opt': [('a', 'b', 'válũè')]})
|
||||
|
||||
self.assertEqual(ttk.tclobjs_to_py({'x': ['y z']}),
|
||||
{'x': ['y z']})
|
||||
|
||||
def test_nosplit(self):
|
||||
self.assertEqual(ttk.tclobjs_to_py({'text': 'some text'}),
|
||||
{'text': 'some text'})
|
||||
|
||||
tests_nogui = (InternalFunctionsTest, TclObjsToPyTest)
|
||||
|
||||
if __name__ == "__main__":
|
||||
from test.support import run_unittest
|
||||
run_unittest(*tests_nogui)
|
92
third_party/python/Lib/tkinter/test/test_ttk/test_style.py
vendored
Normal file
92
third_party/python/Lib/tkinter/test/test_ttk/test_style.py
vendored
Normal file
|
@ -0,0 +1,92 @@
|
|||
import unittest
|
||||
import tkinter
|
||||
from tkinter import ttk
|
||||
from test.support import requires, run_unittest
|
||||
from tkinter.test.support import AbstractTkTest
|
||||
|
||||
requires('gui')
|
||||
|
||||
class StyleTest(AbstractTkTest, unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.style = ttk.Style(self.root)
|
||||
|
||||
|
||||
def test_configure(self):
|
||||
style = self.style
|
||||
style.configure('TButton', background='yellow')
|
||||
self.assertEqual(style.configure('TButton', 'background'),
|
||||
'yellow')
|
||||
self.assertIsInstance(style.configure('TButton'), dict)
|
||||
|
||||
|
||||
def test_map(self):
|
||||
style = self.style
|
||||
style.map('TButton', background=[('active', 'background', 'blue')])
|
||||
self.assertEqual(style.map('TButton', 'background'),
|
||||
[('active', 'background', 'blue')] if self.wantobjects else
|
||||
[('active background', 'blue')])
|
||||
self.assertIsInstance(style.map('TButton'), dict)
|
||||
|
||||
|
||||
def test_lookup(self):
|
||||
style = self.style
|
||||
style.configure('TButton', background='yellow')
|
||||
style.map('TButton', background=[('active', 'background', 'blue')])
|
||||
|
||||
self.assertEqual(style.lookup('TButton', 'background'), 'yellow')
|
||||
self.assertEqual(style.lookup('TButton', 'background',
|
||||
['active', 'background']), 'blue')
|
||||
self.assertEqual(style.lookup('TButton', 'optionnotdefined',
|
||||
default='iknewit'), 'iknewit')
|
||||
|
||||
|
||||
def test_layout(self):
|
||||
style = self.style
|
||||
self.assertRaises(tkinter.TclError, style.layout, 'NotALayout')
|
||||
tv_style = style.layout('Treeview')
|
||||
|
||||
# "erase" Treeview layout
|
||||
style.layout('Treeview', '')
|
||||
self.assertEqual(style.layout('Treeview'),
|
||||
[('null', {'sticky': 'nswe'})]
|
||||
)
|
||||
|
||||
# restore layout
|
||||
style.layout('Treeview', tv_style)
|
||||
self.assertEqual(style.layout('Treeview'), tv_style)
|
||||
|
||||
# should return a list
|
||||
self.assertIsInstance(style.layout('TButton'), list)
|
||||
|
||||
# correct layout, but "option" doesn't exist as option
|
||||
self.assertRaises(tkinter.TclError, style.layout, 'Treeview',
|
||||
[('name', {'option': 'inexistent'})])
|
||||
|
||||
|
||||
def test_theme_use(self):
|
||||
self.assertRaises(tkinter.TclError, self.style.theme_use,
|
||||
'nonexistingname')
|
||||
|
||||
curr_theme = self.style.theme_use()
|
||||
new_theme = None
|
||||
for theme in self.style.theme_names():
|
||||
if theme != curr_theme:
|
||||
new_theme = theme
|
||||
self.style.theme_use(theme)
|
||||
break
|
||||
else:
|
||||
# just one theme available, can't go on with tests
|
||||
return
|
||||
|
||||
self.assertFalse(curr_theme == new_theme)
|
||||
self.assertFalse(new_theme != self.style.theme_use())
|
||||
|
||||
self.style.theme_use(curr_theme)
|
||||
|
||||
|
||||
tests_gui = (StyleTest, )
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_unittest(*tests_gui)
|
1721
third_party/python/Lib/tkinter/test/test_ttk/test_widgets.py
vendored
Normal file
1721
third_party/python/Lib/tkinter/test/test_ttk/test_widgets.py
vendored
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue